tsi108_i2c.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * (C) Copyright 2004 Tundra Semiconductor Corp.
  3. * Author: Alex Bounine
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <config.h>
  8. #include <common.h>
  9. #include <tsi108.h>
  10. #if defined(CONFIG_CMD_I2C)
  11. #define I2C_DELAY 100000
  12. #undef DEBUG_I2C
  13. #ifdef DEBUG_I2C
  14. #define DPRINT(x) printf (x)
  15. #else
  16. #define DPRINT(x)
  17. #endif
  18. /* All functions assume that Tsi108 I2C block is the only master on the bus */
  19. /* I2C read helper function */
  20. void i2c_init(int speed, int slaveaddr)
  21. {
  22. /*
  23. * The TSI108 has a fixed I2C clock rate and doesn't support slave
  24. * operation. This function only exists as a stub to fit into the
  25. * U-Boot I2C API.
  26. */
  27. }
  28. static int i2c_read_byte (
  29. uint i2c_chan, /* I2C channel number: 0 - main, 1 - SDC SPD */
  30. uchar chip_addr,/* I2C device address on the bus */
  31. uint byte_addr, /* Byte address within I2C device */
  32. uchar * buffer /* pointer to data buffer */
  33. )
  34. {
  35. u32 temp;
  36. u32 to_count = I2C_DELAY;
  37. u32 op_status = TSI108_I2C_TIMEOUT_ERR;
  38. u32 chan_offset = TSI108_I2C_OFFSET;
  39. DPRINT (("I2C read_byte() %d 0x%02x 0x%02x\n",
  40. i2c_chan, chip_addr, byte_addr));
  41. if (0 != i2c_chan)
  42. chan_offset = TSI108_I2C_SDRAM_OFFSET;
  43. /* Check if I2C operation is in progress */
  44. temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL2);
  45. if (0 == (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_WR_STATUS |
  46. I2C_CNTRL2_START))) {
  47. /* Set device address and operation (read = 0) */
  48. temp = (byte_addr << 16) | ((chip_addr & 0x07) << 8) |
  49. ((chip_addr >> 3) & 0x0F);
  50. *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL1) =
  51. temp;
  52. /* Issue the read command
  53. * (at this moment all other parameters are 0
  54. * (size = 1 byte, lane = 0)
  55. */
  56. *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL2) =
  57. (I2C_CNTRL2_START);
  58. /* Wait until operation completed */
  59. do {
  60. /* Read I2C operation status */
  61. temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL2);
  62. if (0 == (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_START))) {
  63. if (0 == (temp &
  64. (I2C_CNTRL2_I2C_CFGERR |
  65. I2C_CNTRL2_I2C_TO_ERR))
  66. ) {
  67. op_status = TSI108_I2C_SUCCESS;
  68. temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE +
  69. chan_offset +
  70. I2C_RD_DATA);
  71. *buffer = (u8) (temp & 0xFF);
  72. } else {
  73. /* report HW error */
  74. op_status = TSI108_I2C_IF_ERROR;
  75. DPRINT (("I2C HW error reported: 0x%02x\n", temp));
  76. }
  77. break;
  78. }
  79. } while (to_count--);
  80. } else {
  81. op_status = TSI108_I2C_IF_BUSY;
  82. DPRINT (("I2C Transaction start failed: 0x%02x\n", temp));
  83. }
  84. DPRINT (("I2C read_byte() status: 0x%02x\n", op_status));
  85. return op_status;
  86. }
  87. /*
  88. * I2C Read interface as defined in "include/i2c.h" :
  89. * chip_addr: I2C chip address, range 0..127
  90. * (to read from SPD channel EEPROM use (0xD0 ... 0xD7)
  91. * NOTE: The bit 7 in the chip_addr serves as a channel select.
  92. * This hack is for enabling "i2c sdram" command on Tsi108 boards
  93. * without changes to common code. Used for I2C reads only.
  94. * byte_addr: Memory or register address within the chip
  95. * alen: Number of bytes to use for addr (typically 1, 2 for larger
  96. * memories, 0 for register type devices with only one
  97. * register)
  98. * buffer: Pointer to destination buffer for data to be read
  99. * len: How many bytes to read
  100. *
  101. * Returns: 0 on success, not 0 on failure
  102. */
  103. int i2c_read (uchar chip_addr, uint byte_addr, int alen,
  104. uchar * buffer, int len)
  105. {
  106. u32 op_status = TSI108_I2C_PARAM_ERR;
  107. u32 i2c_if = 0;
  108. /* Hack to support second (SPD) I2C controller (SPD EEPROM read only).*/
  109. if (0xD0 == (chip_addr & ~0x07)) {
  110. i2c_if = 1;
  111. chip_addr &= 0x7F;
  112. }
  113. /* Check for valid I2C address */
  114. if (chip_addr <= 0x7F && (byte_addr + len) <= (0x01 << (alen * 8))) {
  115. while (len--) {
  116. op_status = i2c_read_byte(i2c_if, chip_addr, byte_addr++, buffer++);
  117. if (TSI108_I2C_SUCCESS != op_status) {
  118. DPRINT (("I2C read_byte() failed: 0x%02x (%d left)\n", op_status, len));
  119. break;
  120. }
  121. }
  122. }
  123. DPRINT (("I2C read() status: 0x%02x\n", op_status));
  124. return op_status;
  125. }
  126. /* I2C write helper function */
  127. static int i2c_write_byte (uchar chip_addr,/* I2C device address on the bus */
  128. uint byte_addr, /* Byte address within I2C device */
  129. uchar * buffer /* pointer to data buffer */
  130. )
  131. {
  132. u32 temp;
  133. u32 to_count = I2C_DELAY;
  134. u32 op_status = TSI108_I2C_TIMEOUT_ERR;
  135. /* Check if I2C operation is in progress */
  136. temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET + I2C_CNTRL2);
  137. if (0 == (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_WR_STATUS | I2C_CNTRL2_START))) {
  138. /* Place data into the I2C Tx Register */
  139. *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
  140. I2C_TX_DATA) = (u32) * buffer;
  141. /* Set device address and operation */
  142. temp =
  143. I2C_CNTRL1_I2CWRITE | (byte_addr << 16) |
  144. ((chip_addr & 0x07) << 8) | ((chip_addr >> 3) & 0x0F);
  145. *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
  146. I2C_CNTRL1) = temp;
  147. /* Issue the write command (at this moment all other parameters
  148. * are 0 (size = 1 byte, lane = 0)
  149. */
  150. *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
  151. I2C_CNTRL2) = (I2C_CNTRL2_START);
  152. op_status = TSI108_I2C_TIMEOUT_ERR;
  153. /* Wait until operation completed */
  154. do {
  155. /* Read I2C operation status */
  156. temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET + I2C_CNTRL2);
  157. if (0 == (temp & (I2C_CNTRL2_WR_STATUS | I2C_CNTRL2_START))) {
  158. if (0 == (temp &
  159. (I2C_CNTRL2_I2C_CFGERR |
  160. I2C_CNTRL2_I2C_TO_ERR))) {
  161. op_status = TSI108_I2C_SUCCESS;
  162. } else {
  163. /* report detected HW error */
  164. op_status = TSI108_I2C_IF_ERROR;
  165. DPRINT (("I2C HW error reported: 0x%02x\n", temp));
  166. }
  167. break;
  168. }
  169. } while (to_count--);
  170. } else {
  171. op_status = TSI108_I2C_IF_BUSY;
  172. DPRINT (("I2C Transaction start failed: 0x%02x\n", temp));
  173. }
  174. return op_status;
  175. }
  176. /*
  177. * I2C Write interface as defined in "include/i2c.h" :
  178. * chip_addr: I2C chip address, range 0..127
  179. * byte_addr: Memory or register address within the chip
  180. * alen: Number of bytes to use for addr (typically 1, 2 for larger
  181. * memories, 0 for register type devices with only one
  182. * register)
  183. * buffer: Pointer to data to be written
  184. * len: How many bytes to write
  185. *
  186. * Returns: 0 on success, not 0 on failure
  187. */
  188. int i2c_write (uchar chip_addr, uint byte_addr, int alen, uchar * buffer,
  189. int len)
  190. {
  191. u32 op_status = TSI108_I2C_PARAM_ERR;
  192. /* Check for valid I2C address */
  193. if (chip_addr <= 0x7F && (byte_addr + len) <= (0x01 << (alen * 8))) {
  194. while (len--) {
  195. op_status =
  196. i2c_write_byte (chip_addr, byte_addr++, buffer++);
  197. if (TSI108_I2C_SUCCESS != op_status) {
  198. DPRINT (("I2C write_byte() failed: 0x%02x (%d left)\n", op_status, len));
  199. break;
  200. }
  201. }
  202. }
  203. return op_status;
  204. }
  205. /*
  206. * I2C interface function as defined in "include/i2c.h".
  207. * Probe the given I2C chip address by reading single byte from offset 0.
  208. * Returns 0 if a chip responded, not 0 on failure.
  209. */
  210. int i2c_probe (uchar chip)
  211. {
  212. u32 tmp;
  213. /*
  214. * Try to read the first location of the chip.
  215. * The Tsi108 HW doesn't support sending just the chip address
  216. * and checkong for an <ACK> back.
  217. */
  218. return i2c_read (chip, 0, 1, (uchar *)&tmp, 1);
  219. }
  220. #endif