tsi108_i2c.c 7.4 KB

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