ihs_i2c.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * (C) Copyright 2013
  3. * Dirk Eibach, Guntermann & Drunck GmbH, eibach@gdsys.de
  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 <common.h>
  11. #include <i2c.h>
  12. #include <gdsys_fpga.h>
  13. #include <asm/unaligned.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. #ifdef CONFIG_SYS_I2C_IHS_DUAL
  16. #define I2C_SET_REG(fld, val) \
  17. do { \
  18. if (I2C_ADAP_HWNR & 0x10) \
  19. FPGA_SET_REG(I2C_ADAP_HWNR & 0xf, i2c1.fld, val); \
  20. else \
  21. FPGA_SET_REG(I2C_ADAP_HWNR, i2c0.fld, val); \
  22. } while (0)
  23. #else
  24. #define I2C_SET_REG(fld, val) \
  25. FPGA_SET_REG(I2C_ADAP_HWNR, i2c0.fld, val)
  26. #endif
  27. #ifdef CONFIG_SYS_I2C_IHS_DUAL
  28. #define I2C_GET_REG(fld, val) \
  29. do { \
  30. if (I2C_ADAP_HWNR & 0x10) \
  31. FPGA_GET_REG(I2C_ADAP_HWNR & 0xf, i2c1.fld, val); \
  32. else \
  33. FPGA_GET_REG(I2C_ADAP_HWNR, i2c0.fld, val); \
  34. } while (0)
  35. #else
  36. #define I2C_GET_REG(fld, val) \
  37. FPGA_GET_REG(I2C_ADAP_HWNR, i2c0.fld, val)
  38. #endif
  39. enum {
  40. I2CINT_ERROR_EV = BIT(13),
  41. I2CINT_TRANSMIT_EV = BIT(14),
  42. I2CINT_RECEIVE_EV = BIT(15),
  43. };
  44. enum {
  45. I2CMB_READ = 0 << 10,
  46. I2CMB_WRITE = 1 << 10,
  47. I2CMB_1BYTE = 0 << 11,
  48. I2CMB_2BYTE = 1 << 11,
  49. I2CMB_DONT_HOLD_BUS = 0 << 13,
  50. I2CMB_HOLD_BUS = 1 << 13,
  51. I2CMB_NATIVE = 2 << 14,
  52. };
  53. enum {
  54. I2COP_WRITE = 0,
  55. I2COP_READ = 1,
  56. };
  57. static int wait_for_int(bool read)
  58. {
  59. u16 val;
  60. uint ctr = 0;
  61. I2C_GET_REG(interrupt_status, &val);
  62. /* Wait until error or receive/transmit interrupt was raised */
  63. while (!(val & (I2CINT_ERROR_EV
  64. | (read ? I2CINT_RECEIVE_EV : I2CINT_TRANSMIT_EV)))) {
  65. udelay(10);
  66. if (ctr++ > 5000)
  67. return 1;
  68. I2C_GET_REG(interrupt_status, &val);
  69. }
  70. return (val & I2CINT_ERROR_EV) ? 1 : 0;
  71. }
  72. static int ihs_i2c_transfer(uchar chip, uchar *buffer, int len, bool read,
  73. bool is_last)
  74. {
  75. u16 val;
  76. /* Clear interrupt status */
  77. I2C_SET_REG(interrupt_status, I2CINT_ERROR_EV
  78. | I2CINT_RECEIVE_EV | I2CINT_TRANSMIT_EV);
  79. I2C_GET_REG(interrupt_status, &val);
  80. /* If we want to write and have data, write the bytes to the mailbox */
  81. if (!read && len) {
  82. val = buffer[0];
  83. if (len > 1)
  84. val |= buffer[1] << 8;
  85. I2C_SET_REG(write_mailbox_ext, val);
  86. }
  87. I2C_SET_REG(write_mailbox,
  88. I2CMB_NATIVE
  89. | (read ? 0 : I2CMB_WRITE)
  90. | (chip << 1)
  91. | ((len > 1) ? I2CMB_2BYTE : 0)
  92. | (is_last ? 0 : I2CMB_HOLD_BUS));
  93. if (wait_for_int(read))
  94. return 1;
  95. /* If we want to read, get the bytes from the mailbox */
  96. if (read) {
  97. I2C_GET_REG(read_mailbox_ext, &val);
  98. buffer[0] = val & 0xff;
  99. if (len > 1)
  100. buffer[1] = val >> 8;
  101. }
  102. return 0;
  103. }
  104. static int ihs_i2c_address(uchar chip, u8 *addr, int alen, bool hold_bus)
  105. {
  106. while (alen) {
  107. int transfer = min(alen, 2);
  108. bool is_last = alen <= transfer;
  109. if (ihs_i2c_transfer(chip, addr, transfer, I2COP_WRITE,
  110. hold_bus ? false : is_last))
  111. return 1;
  112. alen -= transfer;
  113. }
  114. return 0;
  115. }
  116. static int ihs_i2c_access(struct i2c_adapter *adap, uchar chip, u8 *addr,
  117. int alen, uchar *buffer, int len, int read)
  118. {
  119. /* Don't hold the bus if length of data to send/receive is zero */
  120. if (len <= 0 || ihs_i2c_address(chip, addr, alen, len))
  121. return 1;
  122. while (len) {
  123. int transfer = min(len, 2);
  124. bool is_last = len <= transfer;
  125. if (ihs_i2c_transfer(chip, buffer, transfer, read,
  126. is_last))
  127. return 2;
  128. buffer += transfer;
  129. len -= transfer;
  130. }
  131. return 0;
  132. }
  133. static void ihs_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
  134. {
  135. #ifdef CONFIG_SYS_I2C_INIT_BOARD
  136. /*
  137. * Call board specific i2c bus reset routine before accessing the
  138. * environment, which might be in a chip on that bus. For details
  139. * about this problem see doc/I2C_Edge_Conditions.
  140. */
  141. i2c_init_board();
  142. #endif
  143. }
  144. static int ihs_i2c_probe(struct i2c_adapter *adap, uchar chip)
  145. {
  146. uchar buffer[2];
  147. if (ihs_i2c_transfer(chip, buffer, 0, I2COP_READ, true))
  148. return 1;
  149. return 0;
  150. }
  151. static int ihs_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
  152. int alen, uchar *buffer, int len)
  153. {
  154. u8 addr_bytes[4];
  155. put_unaligned_le32(addr, addr_bytes);
  156. return ihs_i2c_access(adap, chip, addr_bytes, alen, buffer, len,
  157. I2COP_READ);
  158. }
  159. static int ihs_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
  160. int alen, uchar *buffer, int len)
  161. {
  162. u8 addr_bytes[4];
  163. put_unaligned_le32(addr, addr_bytes);
  164. return ihs_i2c_access(adap, chip, addr_bytes, alen, buffer, len,
  165. I2COP_WRITE);
  166. }
  167. static unsigned int ihs_i2c_set_bus_speed(struct i2c_adapter *adap,
  168. unsigned int speed)
  169. {
  170. if (speed != adap->speed)
  171. return 1;
  172. return speed;
  173. }
  174. /*
  175. * Register IHS i2c adapters
  176. */
  177. #ifdef CONFIG_SYS_I2C_IHS_CH0
  178. U_BOOT_I2C_ADAP_COMPLETE(ihs0, ihs_i2c_init, ihs_i2c_probe,
  179. ihs_i2c_read, ihs_i2c_write,
  180. ihs_i2c_set_bus_speed,
  181. CONFIG_SYS_I2C_IHS_SPEED_0,
  182. CONFIG_SYS_I2C_IHS_SLAVE_0, 0)
  183. #ifdef CONFIG_SYS_I2C_IHS_DUAL
  184. U_BOOT_I2C_ADAP_COMPLETE(ihs0_1, ihs_i2c_init, ihs_i2c_probe,
  185. ihs_i2c_read, ihs_i2c_write,
  186. ihs_i2c_set_bus_speed,
  187. CONFIG_SYS_I2C_IHS_SPEED_0_1,
  188. CONFIG_SYS_I2C_IHS_SLAVE_0_1, 16)
  189. #endif
  190. #endif
  191. #ifdef CONFIG_SYS_I2C_IHS_CH1
  192. U_BOOT_I2C_ADAP_COMPLETE(ihs1, ihs_i2c_init, ihs_i2c_probe,
  193. ihs_i2c_read, ihs_i2c_write,
  194. ihs_i2c_set_bus_speed,
  195. CONFIG_SYS_I2C_IHS_SPEED_1,
  196. CONFIG_SYS_I2C_IHS_SLAVE_1, 1)
  197. #ifdef CONFIG_SYS_I2C_IHS_DUAL
  198. U_BOOT_I2C_ADAP_COMPLETE(ihs1_1, ihs_i2c_init, ihs_i2c_probe,
  199. ihs_i2c_read, ihs_i2c_write,
  200. ihs_i2c_set_bus_speed,
  201. CONFIG_SYS_I2C_IHS_SPEED_1_1,
  202. CONFIG_SYS_I2C_IHS_SLAVE_1_1, 17)
  203. #endif
  204. #endif
  205. #ifdef CONFIG_SYS_I2C_IHS_CH2
  206. U_BOOT_I2C_ADAP_COMPLETE(ihs2, ihs_i2c_init, ihs_i2c_probe,
  207. ihs_i2c_read, ihs_i2c_write,
  208. ihs_i2c_set_bus_speed,
  209. CONFIG_SYS_I2C_IHS_SPEED_2,
  210. CONFIG_SYS_I2C_IHS_SLAVE_2, 2)
  211. #ifdef CONFIG_SYS_I2C_IHS_DUAL
  212. U_BOOT_I2C_ADAP_COMPLETE(ihs2_1, ihs_i2c_init, ihs_i2c_probe,
  213. ihs_i2c_read, ihs_i2c_write,
  214. ihs_i2c_set_bus_speed,
  215. CONFIG_SYS_I2C_IHS_SPEED_2_1,
  216. CONFIG_SYS_I2C_IHS_SLAVE_2_1, 18)
  217. #endif
  218. #endif
  219. #ifdef CONFIG_SYS_I2C_IHS_CH3
  220. U_BOOT_I2C_ADAP_COMPLETE(ihs3, ihs_i2c_init, ihs_i2c_probe,
  221. ihs_i2c_read, ihs_i2c_write,
  222. ihs_i2c_set_bus_speed,
  223. CONFIG_SYS_I2C_IHS_SPEED_3,
  224. CONFIG_SYS_I2C_IHS_SLAVE_3, 3)
  225. #ifdef CONFIG_SYS_I2C_IHS_DUAL
  226. U_BOOT_I2C_ADAP_COMPLETE(ihs3_1, ihs_i2c_init, ihs_i2c_probe,
  227. ihs_i2c_read, ihs_i2c_write,
  228. ihs_i2c_set_bus_speed,
  229. CONFIG_SYS_I2C_IHS_SPEED_3_1,
  230. CONFIG_SYS_I2C_IHS_SLAVE_3_1, 19)
  231. #endif
  232. #endif