tsi108_i2c.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. #ifdef CONFIG_TSI108_I2C
  26. #include <common.h>
  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. }
  54. /* Check if I2C operation is in progress */
  55. temp = *(u32 *) (CFG_TSI108_CSR_BASE + chan_offset + I2C_CNTRL2);
  56. if (0 == (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_WR_STATUS |
  57. I2C_CNTRL2_START))
  58. ) {
  59. /* Set device address and operation (read = 0) */
  60. temp = (byte_addr << 16) | ((chip_addr & 0x07) << 8) |
  61. ((chip_addr >> 3) & 0x0F);
  62. *(u32 *) (CFG_TSI108_CSR_BASE + chan_offset + I2C_CNTRL1) =
  63. temp;
  64. /* Issue the read command
  65. * (at this moment all other parameters are 0
  66. * (size = 1 byte, lane = 0)
  67. */
  68. *(u32 *) (CFG_TSI108_CSR_BASE + chan_offset + I2C_CNTRL2) =
  69. (I2C_CNTRL2_START);
  70. /* Wait until operation completed */
  71. do {
  72. /* Read I2C operation status */
  73. temp =
  74. *(u32 *) (CFG_TSI108_CSR_BASE + chan_offset +
  75. I2C_CNTRL2);
  76. if (0 ==
  77. (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_START)))
  78. {
  79. if (0 ==
  80. (temp &
  81. (I2C_CNTRL2_I2C_CFGERR |
  82. I2C_CNTRL2_I2C_TO_ERR))
  83. ) {
  84. op_status = TSI108_I2C_SUCCESS;
  85. temp = *(u32 *) (CFG_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 "isdram" 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, uchar * buffer, int len)
  121. {
  122. u32 op_status = TSI108_I2C_PARAM_ERR;
  123. u32 i2c_if = 0;
  124. /* Hack to support second (SPD) I2C controller (SPD EEPROM read only).*/
  125. if (0xD0 == (chip_addr & ~0x07)) {
  126. i2c_if = 1;
  127. chip_addr &= 0x7F;
  128. }
  129. /* Check for valid I2C address */
  130. if (chip_addr <= 0x7F && (byte_addr + len) <= (0x01 << (alen * 8))) {
  131. while (len--) {
  132. op_status =
  133. i2c_read_byte(i2c_if, chip_addr, byte_addr++,
  134. buffer++);
  135. if (TSI108_I2C_SUCCESS != op_status) {
  136. DPRINT(("I2C read_byte() failed: 0x%02x (%d left)\n", op_status, len));
  137. break;
  138. }
  139. }
  140. }
  141. DPRINT(("I2C read() status: 0x%02x\n", op_status));
  142. return op_status;
  143. }
  144. /* I2C write helper function */
  145. static int i2c_write_byte(uchar chip_addr,/* I2C device address on the bus */
  146. uint byte_addr, /* Byte address within I2C device */
  147. uchar * buffer /* pointer to data buffer */
  148. )
  149. {
  150. u32 temp;
  151. u32 to_count = I2C_DELAY;
  152. u32 op_status = TSI108_I2C_TIMEOUT_ERR;
  153. /* Check if I2C operation is in progress */
  154. temp = *(u32 *) (CFG_TSI108_CSR_BASE + TSI108_I2C_OFFSET + I2C_CNTRL2);
  155. if (0 ==
  156. (temp &
  157. (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_WR_STATUS | I2C_CNTRL2_START)))
  158. {
  159. /* Place data into the I2C Tx Register */
  160. *(u32 *) (CFG_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
  161. I2C_TX_DATA) = (u32) * buffer;
  162. /* Set device address and operation */
  163. temp =
  164. I2C_CNTRL1_I2CWRITE | (byte_addr << 16) |
  165. ((chip_addr & 0x07) << 8) | ((chip_addr >> 3) & 0x0F);
  166. *(u32 *) (CFG_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
  167. I2C_CNTRL1) = temp;
  168. /* Issue the write command (at this moment all other parameters
  169. * are 0 (size = 1 byte, lane = 0)
  170. */
  171. *(u32 *) (CFG_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
  172. I2C_CNTRL2) = (I2C_CNTRL2_START);
  173. op_status = TSI108_I2C_TIMEOUT_ERR;
  174. /* Wait until operation completed */
  175. do {
  176. // Read I2C operation status
  177. temp =
  178. *(u32 *) (CFG_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
  179. I2C_CNTRL2);
  180. if (0 ==
  181. (temp & (I2C_CNTRL2_WR_STATUS | I2C_CNTRL2_START)))
  182. {
  183. if (0 ==
  184. (temp &
  185. (I2C_CNTRL2_I2C_CFGERR |
  186. I2C_CNTRL2_I2C_TO_ERR))) {
  187. op_status = TSI108_I2C_SUCCESS;
  188. } else {
  189. /* report detected HW error */
  190. op_status = TSI108_I2C_IF_ERROR;
  191. DPRINT(("I2C HW error reported: 0x%02x\n", temp));
  192. }
  193. break;
  194. }
  195. } while (to_count--);
  196. } else {
  197. op_status = TSI108_I2C_IF_BUSY;
  198. DPRINT(("I2C Transaction start failed: 0x%02x\n", temp));
  199. }
  200. return op_status;
  201. }
  202. /*
  203. * I2C Write interface as defined in "include/i2c.h" :
  204. * chip_addr: I2C chip address, range 0..127
  205. * byte_addr: Memory or register address within the chip
  206. * alen: Number of bytes to use for addr (typically 1, 2 for larger
  207. * memories, 0 for register type devices with only one
  208. * register)
  209. * buffer: Pointer to data to be written
  210. * len: How many bytes to write
  211. *
  212. * Returns: 0 on success, not 0 on failure
  213. */
  214. int i2c_write(uchar chip_addr, uint byte_addr, int alen, uchar * buffer,
  215. int len)
  216. {
  217. u32 op_status = TSI108_I2C_PARAM_ERR;
  218. /* Check for valid I2C address */
  219. if (chip_addr <= 0x7F && (byte_addr + len) <= (0x01 << (alen * 8))) {
  220. while (len--) {
  221. op_status =
  222. i2c_write_byte(chip_addr, byte_addr++, buffer++);
  223. if (TSI108_I2C_SUCCESS != op_status) {
  224. DPRINT(("I2C write_byte() failed: 0x%02x (%d left)\n", op_status, len));
  225. break;
  226. }
  227. }
  228. }
  229. return op_status;
  230. }
  231. /*
  232. * I2C interface function as defined in "include/i2c.h".
  233. * Probe the given I2C chip address by reading single byte from offset 0.
  234. * Returns 0 if a chip responded, not 0 on failure.
  235. */
  236. int i2c_probe(uchar chip)
  237. {
  238. u32 tmp;
  239. /*
  240. * Try to read the first location of the chip.
  241. * The Tsi108 HW doesn't support sending just the chip address
  242. * and checkong for an <ACK> back.
  243. */
  244. return i2c_read(chip, 0, 1, (char *)&tmp, 1);
  245. }
  246. #endif /* (CONFIG_COMMANDS & CFG_CMD_I2C) */
  247. #endif /* CONFIG_TSI108_I2C */