tsi108_i2c.c 7.4 KB

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