tsi108_i2c.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 = *(u32 *) (CFG_TSI108_CSR_BASE + chan_offset + I2C_CNTRL2);
  72. if (0 == (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_START))) {
  73. if (0 == (temp &
  74. (I2C_CNTRL2_I2C_CFGERR |
  75. I2C_CNTRL2_I2C_TO_ERR))
  76. ) {
  77. op_status = TSI108_I2C_SUCCESS;
  78. temp = *(u32 *) (CFG_TSI108_CSR_BASE +
  79. chan_offset +
  80. I2C_RD_DATA);
  81. *buffer = (u8) (temp & 0xFF);
  82. } else {
  83. /* report HW error */
  84. op_status = TSI108_I2C_IF_ERROR;
  85. DPRINT (("I2C HW error reported: 0x%02x\n", temp));
  86. }
  87. break;
  88. }
  89. } while (to_count--);
  90. } else {
  91. op_status = TSI108_I2C_IF_BUSY;
  92. DPRINT (("I2C Transaction start failed: 0x%02x\n", temp));
  93. }
  94. DPRINT (("I2C read_byte() status: 0x%02x\n", op_status));
  95. return op_status;
  96. }
  97. /*
  98. * I2C Read interface as defined in "include/i2c.h" :
  99. * chip_addr: I2C chip address, range 0..127
  100. * (to read from SPD channel EEPROM use (0xD0 ... 0xD7)
  101. * NOTE: The bit 7 in the chip_addr serves as a channel select.
  102. * This hack is for enabling "isdram" command on Tsi108 boards
  103. * without changes to common code. Used for I2C reads only.
  104. * byte_addr: Memory or register address within the chip
  105. * alen: Number of bytes to use for addr (typically 1, 2 for larger
  106. * memories, 0 for register type devices with only one
  107. * register)
  108. * buffer: Pointer to destination buffer for data to be read
  109. * len: How many bytes to read
  110. *
  111. * Returns: 0 on success, not 0 on failure
  112. */
  113. int i2c_read (uchar chip_addr, uint byte_addr, int alen,
  114. uchar * buffer, int len)
  115. {
  116. u32 op_status = TSI108_I2C_PARAM_ERR;
  117. u32 i2c_if = 0;
  118. /* Hack to support second (SPD) I2C controller (SPD EEPROM read only).*/
  119. if (0xD0 == (chip_addr & ~0x07)) {
  120. i2c_if = 1;
  121. chip_addr &= 0x7F;
  122. }
  123. /* Check for valid I2C address */
  124. if (chip_addr <= 0x7F && (byte_addr + len) <= (0x01 << (alen * 8))) {
  125. while (len--) {
  126. op_status = i2c_read_byte(i2c_if, chip_addr, byte_addr++, buffer++);
  127. if (TSI108_I2C_SUCCESS != op_status) {
  128. DPRINT (("I2C read_byte() failed: 0x%02x (%d left)\n", op_status, len));
  129. break;
  130. }
  131. }
  132. }
  133. DPRINT (("I2C read() status: 0x%02x\n", op_status));
  134. return op_status;
  135. }
  136. /* I2C write helper function */
  137. static int i2c_write_byte (uchar chip_addr,/* I2C device address on the bus */
  138. uint byte_addr, /* Byte address within I2C device */
  139. uchar * buffer /* pointer to data buffer */
  140. )
  141. {
  142. u32 temp;
  143. u32 to_count = I2C_DELAY;
  144. u32 op_status = TSI108_I2C_TIMEOUT_ERR;
  145. /* Check if I2C operation is in progress */
  146. temp = *(u32 *) (CFG_TSI108_CSR_BASE + TSI108_I2C_OFFSET + I2C_CNTRL2);
  147. if (0 == (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_WR_STATUS | I2C_CNTRL2_START))) {
  148. /* Place data into the I2C Tx Register */
  149. *(u32 *) (CFG_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
  150. I2C_TX_DATA) = (u32) * buffer;
  151. /* Set device address and operation */
  152. temp =
  153. I2C_CNTRL1_I2CWRITE | (byte_addr << 16) |
  154. ((chip_addr & 0x07) << 8) | ((chip_addr >> 3) & 0x0F);
  155. *(u32 *) (CFG_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
  156. I2C_CNTRL1) = temp;
  157. /* Issue the write command (at this moment all other parameters
  158. * are 0 (size = 1 byte, lane = 0)
  159. */
  160. *(u32 *) (CFG_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
  161. I2C_CNTRL2) = (I2C_CNTRL2_START);
  162. op_status = TSI108_I2C_TIMEOUT_ERR;
  163. /* Wait until operation completed */
  164. do {
  165. /* Read I2C operation status */
  166. temp = *(u32 *) (CFG_TSI108_CSR_BASE + TSI108_I2C_OFFSET + I2C_CNTRL2);
  167. if (0 == (temp & (I2C_CNTRL2_WR_STATUS | I2C_CNTRL2_START))) {
  168. if (0 == (temp &
  169. (I2C_CNTRL2_I2C_CFGERR |
  170. I2C_CNTRL2_I2C_TO_ERR))) {
  171. op_status = TSI108_I2C_SUCCESS;
  172. } else {
  173. /* report detected HW error */
  174. op_status = TSI108_I2C_IF_ERROR;
  175. DPRINT (("I2C HW error reported: 0x%02x\n", temp));
  176. }
  177. break;
  178. }
  179. } while (to_count--);
  180. } else {
  181. op_status = TSI108_I2C_IF_BUSY;
  182. DPRINT (("I2C Transaction start failed: 0x%02x\n", temp));
  183. }
  184. return op_status;
  185. }
  186. /*
  187. * I2C Write interface as defined in "include/i2c.h" :
  188. * chip_addr: I2C chip address, range 0..127
  189. * byte_addr: Memory or register address within the chip
  190. * alen: Number of bytes to use for addr (typically 1, 2 for larger
  191. * memories, 0 for register type devices with only one
  192. * register)
  193. * buffer: Pointer to data to be written
  194. * len: How many bytes to write
  195. *
  196. * Returns: 0 on success, not 0 on failure
  197. */
  198. int i2c_write (uchar chip_addr, uint byte_addr, int alen, uchar * buffer,
  199. int len)
  200. {
  201. u32 op_status = TSI108_I2C_PARAM_ERR;
  202. /* Check for valid I2C address */
  203. if (chip_addr <= 0x7F && (byte_addr + len) <= (0x01 << (alen * 8))) {
  204. while (len--) {
  205. op_status =
  206. i2c_write_byte (chip_addr, byte_addr++, buffer++);
  207. if (TSI108_I2C_SUCCESS != op_status) {
  208. DPRINT (("I2C write_byte() failed: 0x%02x (%d left)\n", op_status, len));
  209. break;
  210. }
  211. }
  212. }
  213. return op_status;
  214. }
  215. /*
  216. * I2C interface function as defined in "include/i2c.h".
  217. * Probe the given I2C chip address by reading single byte from offset 0.
  218. * Returns 0 if a chip responded, not 0 on failure.
  219. */
  220. int i2c_probe (uchar chip)
  221. {
  222. u32 tmp;
  223. /*
  224. * Try to read the first location of the chip.
  225. * The Tsi108 HW doesn't support sending just the chip address
  226. * and checkong for an <ACK> back.
  227. */
  228. return i2c_read (chip, 0, 1, (char *)&tmp, 1);
  229. }
  230. #endif /* (CONFIG_COMMANDS & CFG_CMD_I2C) */
  231. #endif /* CONFIG_TSI108_I2C */