ihs_i2c.c 4.4 KB

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