ihs_i2c.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * (C) Copyright 2013
  3. * Dirk Eibach, Guntermann & Drunck GmbH, eibach@gdsys.de
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <i2c.h>
  9. #include <gdsys_fpga.h>
  10. DECLARE_GLOBAL_DATA_PTR;
  11. enum {
  12. I2CINT_ERROR_EV = 1 << 13,
  13. I2CINT_TRANSMIT_EV = 1 << 14,
  14. I2CINT_RECEIVE_EV = 1 << 15,
  15. };
  16. enum {
  17. I2CMB_WRITE = 1 << 10,
  18. I2CMB_2BYTE = 1 << 11,
  19. I2CMB_HOLD_BUS = 1 << 13,
  20. I2CMB_NATIVE = 2 << 14,
  21. };
  22. static int wait_for_int(bool read)
  23. {
  24. u16 val;
  25. unsigned int ctr = 0;
  26. FPGA_GET_REG(I2C_ADAP_HWNR, i2c.interrupt_status, &val);
  27. while (!(val & (I2CINT_ERROR_EV
  28. | (read ? I2CINT_RECEIVE_EV : I2CINT_TRANSMIT_EV)))) {
  29. udelay(10);
  30. if (ctr++ > 5000) {
  31. return 1;
  32. }
  33. FPGA_GET_REG(I2C_ADAP_HWNR, i2c.interrupt_status, &val);
  34. }
  35. return (val & I2CINT_ERROR_EV) ? 1 : 0;
  36. }
  37. static int ihs_i2c_transfer(uchar chip, uchar *buffer, int len, bool read,
  38. bool is_last)
  39. {
  40. u16 val;
  41. FPGA_SET_REG(I2C_ADAP_HWNR, i2c.interrupt_status, I2CINT_ERROR_EV
  42. | I2CINT_RECEIVE_EV | I2CINT_TRANSMIT_EV);
  43. FPGA_GET_REG(I2C_ADAP_HWNR, i2c.interrupt_status, &val);
  44. if (!read && len) {
  45. val = buffer[0];
  46. if (len > 1)
  47. val |= buffer[1] << 8;
  48. FPGA_SET_REG(I2C_ADAP_HWNR, i2c.write_mailbox_ext, val);
  49. }
  50. FPGA_SET_REG(I2C_ADAP_HWNR, i2c.write_mailbox,
  51. I2CMB_NATIVE
  52. | (read ? 0 : I2CMB_WRITE)
  53. | (chip << 1)
  54. | ((len > 1) ? I2CMB_2BYTE : 0)
  55. | (is_last ? 0 : I2CMB_HOLD_BUS));
  56. if (wait_for_int(read))
  57. return 1;
  58. if (read) {
  59. FPGA_GET_REG(I2C_ADAP_HWNR, i2c.read_mailbox_ext, &val);
  60. buffer[0] = val & 0xff;
  61. if (len > 1)
  62. buffer[1] = val >> 8;
  63. }
  64. return 0;
  65. }
  66. static int ihs_i2c_address(uchar chip, uint addr, int alen, bool hold_bus)
  67. {
  68. int shift = (alen-1) * 8;
  69. while (alen) {
  70. int transfer = min(alen, 2);
  71. uchar buf[2];
  72. bool is_last = alen <= transfer;
  73. buf[0] = addr >> shift;
  74. if (alen > 1)
  75. buf[1] = addr >> (shift - 8);
  76. if (ihs_i2c_transfer(chip, buf, transfer, false,
  77. hold_bus ? false : is_last))
  78. return 1;
  79. shift -= 16;
  80. alen -= transfer;
  81. }
  82. return 0;
  83. }
  84. static int ihs_i2c_access(struct i2c_adapter *adap, uchar chip, uint addr,
  85. int alen, uchar *buffer, int len, bool read)
  86. {
  87. if (len <= 0)
  88. return 1;
  89. if (ihs_i2c_address(chip, addr, alen, !read))
  90. return 1;
  91. while (len) {
  92. int transfer = min(len, 2);
  93. if (ihs_i2c_transfer(chip, buffer, transfer, read,
  94. len <= transfer))
  95. return 1;
  96. buffer += transfer;
  97. addr += transfer;
  98. len -= transfer;
  99. }
  100. return 0;
  101. }
  102. static void ihs_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
  103. {
  104. #ifdef CONFIG_SYS_I2C_INIT_BOARD
  105. /*
  106. * Call board specific i2c bus reset routine before accessing the
  107. * environment, which might be in a chip on that bus. For details
  108. * about this problem see doc/I2C_Edge_Conditions.
  109. */
  110. i2c_init_board();
  111. #endif
  112. }
  113. static int ihs_i2c_probe(struct i2c_adapter *adap, uchar chip)
  114. {
  115. uchar buffer[2];
  116. if (ihs_i2c_transfer(chip, buffer, 0, true, true))
  117. return 1;
  118. return 0;
  119. }
  120. static int ihs_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
  121. int alen, uchar *buffer, int len)
  122. {
  123. return ihs_i2c_access(adap, chip, addr, alen, buffer, len, true);
  124. }
  125. static int ihs_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
  126. int alen, uchar *buffer, int len)
  127. {
  128. return ihs_i2c_access(adap, chip, addr, alen, buffer, len, false);
  129. }
  130. static unsigned int ihs_i2c_set_bus_speed(struct i2c_adapter *adap,
  131. unsigned int speed)
  132. {
  133. if (speed != adap->speed)
  134. return 1;
  135. return speed;
  136. }
  137. /*
  138. * Register IHS i2c adapters
  139. */
  140. #ifdef CONFIG_SYS_I2C_IHS_CH0
  141. U_BOOT_I2C_ADAP_COMPLETE(ihs0, ihs_i2c_init, ihs_i2c_probe,
  142. ihs_i2c_read, ihs_i2c_write,
  143. ihs_i2c_set_bus_speed,
  144. CONFIG_SYS_I2C_IHS_SPEED_0,
  145. CONFIG_SYS_I2C_IHS_SLAVE_0, 0)
  146. #endif
  147. #ifdef CONFIG_SYS_I2C_IHS_CH1
  148. U_BOOT_I2C_ADAP_COMPLETE(ihs1, ihs_i2c_init, ihs_i2c_probe,
  149. ihs_i2c_read, ihs_i2c_write,
  150. ihs_i2c_set_bus_speed,
  151. CONFIG_SYS_I2C_IHS_SPEED_1,
  152. CONFIG_SYS_I2C_IHS_SLAVE_1, 1)
  153. #endif
  154. #ifdef CONFIG_SYS_I2C_IHS_CH2
  155. U_BOOT_I2C_ADAP_COMPLETE(ihs2, ihs_i2c_init, ihs_i2c_probe,
  156. ihs_i2c_read, ihs_i2c_write,
  157. ihs_i2c_set_bus_speed,
  158. CONFIG_SYS_I2C_IHS_SPEED_2,
  159. CONFIG_SYS_I2C_IHS_SLAVE_2, 2)
  160. #endif
  161. #ifdef CONFIG_SYS_I2C_IHS_CH3
  162. U_BOOT_I2C_ADAP_COMPLETE(ihs3, ihs_i2c_init, ihs_i2c_probe,
  163. ihs_i2c_read, ihs_i2c_write,
  164. ihs_i2c_set_bus_speed,
  165. CONFIG_SYS_I2C_IHS_SPEED_3,
  166. CONFIG_SYS_I2C_IHS_SLAVE_3, 3)
  167. #endif