tsi108_i2c.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. #ifdef CONFIG_TSI108_I2C
  27. #include <tsi108.h>
  28. #if (CONFIG_COMMANDS & CFG_CMD_I2C)
  29. #define I2C_DELAY 100000
  30. #undef DEBUG_I2C
  31. #ifdef DEBUG_I2C
  32. #define DPRINT(x) printf (x)
  33. #else
  34. #define DPRINT(x)
  35. #endif
  36. /* All functions assume that Tsi108 I2C block is the only master on the bus */
  37. /* I2C read helper function */
  38. static int i2c_read_byte (
  39. uint i2c_chan, /* I2C channel number: 0 - main, 1 - SDC SPD */
  40. uchar chip_addr,/* I2C device address on the bus */
  41. uint byte_addr, /* Byte address within I2C device */
  42. uchar * buffer /* pointer to data buffer */
  43. )
  44. {
  45. u32 temp;
  46. u32 to_count = I2C_DELAY;
  47. u32 op_status = TSI108_I2C_TIMEOUT_ERR;
  48. u32 chan_offset = TSI108_I2C_OFFSET;
  49. DPRINT (("I2C read_byte() %d 0x%02x 0x%02x\n",
  50. i2c_chan, chip_addr, byte_addr));
  51. if (0 != i2c_chan)
  52. chan_offset = TSI108_I2C_SDRAM_OFFSET;
  53. /* Check if I2C operation is in progress */
  54. temp = *(u32 *) (CFG_TSI108_CSR_BASE + chan_offset + I2C_CNTRL2);
  55. if (0 == (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_WR_STATUS |
  56. I2C_CNTRL2_START))) {
  57. /* Set device address and operation (read = 0) */
  58. temp = (byte_addr << 16) | ((chip_addr & 0x07) << 8) |
  59. ((chip_addr >> 3) & 0x0F);
  60. *(u32 *) (CFG_TSI108_CSR_BASE + chan_offset + I2C_CNTRL1) =
  61. temp;
  62. /* Issue the read command
  63. * (at this moment all other parameters are 0
  64. * (size = 1 byte, lane = 0)
  65. */
  66. *(u32 *) (CFG_TSI108_CSR_BASE + chan_offset + I2C_CNTRL2) =
  67. (I2C_CNTRL2_START);
  68. /* Wait until operation completed */
  69. do {
  70. /* Read I2C operation status */
  71. temp =
  72. *(u32 *) (CFG_TSI108_CSR_BASE + chan_offset +
  73. I2C_CNTRL2);
  74. if (0 ==
  75. (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_START)))
  76. {
  77. if (0 ==
  78. (temp &
  79. (I2C_CNTRL2_I2C_CFGERR |
  80. I2C_CNTRL2_I2C_TO_ERR))
  81. ) {
  82. op_status = TSI108_I2C_SUCCESS;
  83. temp = *(u32 *) (CFG_TSI108_CSR_BASE +
  84. chan_offset +
  85. I2C_RD_DATA);
  86. *buffer = (u8) (temp & 0xFF);
  87. } else {
  88. /* report HW error */
  89. op_status = TSI108_I2C_IF_ERROR;
  90. DPRINT (("I2C HW error reported: 0x%02x\n", temp));
  91. }
  92. break;
  93. }
  94. } while (to_count--);
  95. } else {
  96. op_status = TSI108_I2C_IF_BUSY;
  97. DPRINT (("I2C Transaction start failed: 0x%02x\n", temp));
  98. }
  99. DPRINT (("I2C read_byte() status: 0x%02x\n", op_status));
  100. return op_status;
  101. }
  102. /*
  103. * I2C Read interface as defined in "include/i2c.h" :
  104. * chip_addr: I2C chip address, range 0..127
  105. * (to read from SPD channel EEPROM use (0xD0 ... 0xD7)
  106. * NOTE: The bit 7 in the chip_addr serves as a channel select.
  107. * This hack is for enabling "isdram" command on Tsi108 boards
  108. * without changes to common code. Used for I2C reads only.
  109. * byte_addr: Memory or register address within the chip
  110. * alen: Number of bytes to use for addr (typically 1, 2 for larger
  111. * memories, 0 for register type devices with only one
  112. * register)
  113. * buffer: Pointer to destination buffer for data to be read
  114. * len: How many bytes to read
  115. *
  116. * Returns: 0 on success, not 0 on failure
  117. */
  118. int i2c_read (uchar chip_addr, uint byte_addr, int alen,
  119. uchar * buffer, int len)
  120. {
  121. u32 op_status = TSI108_I2C_PARAM_ERR;
  122. u32 i2c_if = 0;
  123. /* Hack to support second (SPD) I2C controller (SPD EEPROM read only).*/
  124. if (0xD0 == (chip_addr & ~0x07)) {
  125. i2c_if = 1;
  126. chip_addr &= 0x7F;
  127. }
  128. /* Check for valid I2C address */
  129. if (chip_addr <= 0x7F && (byte_addr + len) <= (0x01 << (alen * 8))) {
  130. while (len--) {
  131. op_status =
  132. i2c_read_byte(i2c_if, chip_addr, byte_addr++,
  133. 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 *) (CFG_TSI108_CSR_BASE + TSI108_I2C_OFFSET + I2C_CNTRL2);
  154. if (0 ==
  155. (temp &
  156. (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_WR_STATUS | I2C_CNTRL2_START)))
  157. {
  158. /* Place data into the I2C Tx Register */
  159. *(u32 *) (CFG_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
  160. I2C_TX_DATA) = (u32) * buffer;
  161. /* Set device address and operation */
  162. temp =
  163. I2C_CNTRL1_I2CWRITE | (byte_addr << 16) |
  164. ((chip_addr & 0x07) << 8) | ((chip_addr >> 3) & 0x0F);
  165. *(u32 *) (CFG_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
  166. I2C_CNTRL1) = temp;
  167. /* Issue the write command (at this moment all other parameters
  168. * are 0 (size = 1 byte, lane = 0)
  169. */
  170. *(u32 *) (CFG_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
  171. I2C_CNTRL2) = (I2C_CNTRL2_START);
  172. op_status = TSI108_I2C_TIMEOUT_ERR;
  173. /* Wait until operation completed */
  174. do {
  175. /* Read I2C operation status */
  176. temp =
  177. *(u32 *) (CFG_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
  178. I2C_CNTRL2);
  179. if (0 ==
  180. (temp & (I2C_CNTRL2_WR_STATUS | I2C_CNTRL2_START)))
  181. {
  182. if (0 ==
  183. (temp &
  184. (I2C_CNTRL2_I2C_CFGERR |
  185. I2C_CNTRL2_I2C_TO_ERR))) {
  186. op_status = TSI108_I2C_SUCCESS;
  187. } else {
  188. /* report detected HW error */
  189. op_status = TSI108_I2C_IF_ERROR;
  190. DPRINT (("I2C HW error reported: 0x%02x\n", temp));
  191. }
  192. break;
  193. }
  194. } while (to_count--);
  195. } else {
  196. op_status = TSI108_I2C_IF_BUSY;
  197. DPRINT (("I2C Transaction start failed: 0x%02x\n", temp));
  198. }
  199. return op_status;
  200. }
  201. /*
  202. * I2C Write interface as defined in "include/i2c.h" :
  203. * chip_addr: I2C chip address, range 0..127
  204. * byte_addr: Memory or register address within the chip
  205. * alen: Number of bytes to use for addr (typically 1, 2 for larger
  206. * memories, 0 for register type devices with only one
  207. * register)
  208. * buffer: Pointer to data to be written
  209. * len: How many bytes to write
  210. *
  211. * Returns: 0 on success, not 0 on failure
  212. */
  213. int i2c_write (uchar chip_addr, uint byte_addr, int alen, uchar * buffer,
  214. int len)
  215. {
  216. u32 op_status = TSI108_I2C_PARAM_ERR;
  217. /* Check for valid I2C address */
  218. if (chip_addr <= 0x7F && (byte_addr + len) <= (0x01 << (alen * 8))) {
  219. while (len--) {
  220. op_status =
  221. i2c_write_byte (chip_addr, byte_addr++, buffer++);
  222. if (TSI108_I2C_SUCCESS != op_status) {
  223. DPRINT (("I2C write_byte() failed: 0x%02x (%d left)\n", op_status, len));
  224. break;
  225. }
  226. }
  227. }
  228. return op_status;
  229. }
  230. /*
  231. * I2C interface function as defined in "include/i2c.h".
  232. * Probe the given I2C chip address by reading single byte from offset 0.
  233. * Returns 0 if a chip responded, not 0 on failure.
  234. */
  235. int i2c_probe (uchar chip)
  236. {
  237. u32 tmp;
  238. /*
  239. * Try to read the first location of the chip.
  240. * The Tsi108 HW doesn't support sending just the chip address
  241. * and checkong for an <ACK> back.
  242. */
  243. return i2c_read (chip, 0, 1, (char *)&tmp, 1);
  244. }
  245. #endif /* (CONFIG_COMMANDS & CFG_CMD_I2C) */
  246. #endif /* CONFIG_TSI108_I2C */