ihs_i2c.c 5.9 KB

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