tsi108_i2c.c 8.0 KB

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