i2c.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* Polled Synopsys DesignWare APB I2C master (Braswell LPSS).
  2. *
  3. * Only master transfers are implemented. The bus runs in fast mode (the
  4. * SCL counts below give 300-400 kHz depending on the 100/133 MHz LPSS
  5. * clock), which is within the touchpad's 400 kHz rating and keeps a 34-byte
  6. * report read short.
  7. *
  8. * Register offsets follow the DesignWare databook; the component type at
  9. * 0xFC is the usual "DW" signature (0x44570140). The Intel LPSS private
  10. * block is at 0x200 with its reset register at 0x204; firmware has already
  11. * released the controller, but the I2C FSM only drains its FIFOs after an
  12. * explicit assert/release cycle, so i2c_dw_init() always does one. */
  13. #include "i2c.h"
  14. #include "timer.h"
  15. #define DW_IC_CON 0x00
  16. #define DW_IC_TAR 0x04
  17. #define DW_IC_DATA_CMD 0x10
  18. #define DW_IC_SS_SCL_HCNT 0x14
  19. #define DW_IC_SS_SCL_LCNT 0x18
  20. #define DW_IC_FS_SCL_HCNT 0x1c
  21. #define DW_IC_FS_SCL_LCNT 0x20
  22. #define DW_IC_RAW_INTR_STAT 0x34
  23. #define DW_IC_RX_TL 0x38
  24. #define DW_IC_TX_TL 0x3c
  25. #define DW_IC_CLR_INTR 0x40
  26. #define DW_IC_CLR_TX_ABRT 0x54
  27. #define DW_IC_ENABLE 0x6c
  28. #define DW_IC_STATUS 0x70
  29. #define DW_IC_TX_ABRT_SOURCE 0x80
  30. #define DW_IC_TXFLR 0x74
  31. #define DW_IC_RXFLR 0x78
  32. #define DW_IC_ENABLE_STATUS 0x9c
  33. #define DW_IC_FS_SPKLEN 0xa0
  34. #define DW_IC_COMP_VERSION 0xf8
  35. #define DW_IC_COMP_TYPE 0xfc
  36. #define DW_IC_COMP_TYPE_VALUE 0x44570140u
  37. #define DW_LPSS_RESET 0x804
  38. #define IC_CON_MASTER 0x00000001u
  39. #define IC_CON_SPEED_FS 0x00000004u
  40. #define IC_CON_RESTART_EN 0x00000020u
  41. #define IC_CON_SLAVE_DISABLE 0x00000040u
  42. #define IC_DATA_CMD_READ 0x00000100u
  43. #define IC_DATA_CMD_STOP 0x00000200u
  44. #define IC_DATA_CMD_RESTART 0x00000400u
  45. #define IC_STATUS_TFNF 0x00000002u
  46. #define IC_STATUS_RFNE 0x00000004u
  47. #define IC_STATUS_TFE 0x00000004u
  48. #define IC_STATUS_MST_ACTIVITY 0x00000020u
  49. #define IC_ENABLE_EN 0x00000001u
  50. #define IC_ENABLE_ABORT 0x00000002u
  51. #define IC_RAW_TX_ABRT 0x00000040u
  52. unsigned int i2c_last_abort;
  53. static unsigned int dw_rd(unsigned int base, unsigned int off)
  54. {
  55. return *(volatile unsigned int *)(base + off);
  56. }
  57. static void dw_wr(unsigned int base, unsigned int off, unsigned int val)
  58. {
  59. *(volatile unsigned int *)(base + off) = val;
  60. }
  61. static void dw_delay_ms(unsigned int ms)
  62. {
  63. unsigned int t0 = get_ticks_ms();
  64. while (get_ticks_ms() - t0 < ms)
  65. ;
  66. }
  67. /* Wait until (status & mask) == want. A bus abort ends the wait early so a
  68. * missing device cannot burn the whole timeout. */
  69. static int dw_wait(unsigned int base, unsigned int off, unsigned int mask,
  70. unsigned int want, unsigned int ms)
  71. {
  72. unsigned int t0 = get_ticks_ms();
  73. while (get_ticks_ms() - t0 < ms) {
  74. if ((dw_rd(base, off) & mask) == want)
  75. return 0;
  76. if (dw_rd(base, DW_IC_RAW_INTR_STAT) & IC_RAW_TX_ABRT)
  77. return -1;
  78. }
  79. return -1;
  80. }
  81. static void dw_clear_irq(unsigned int base)
  82. {
  83. (void)dw_rd(base, DW_IC_CLR_TX_ABRT);
  84. (void)dw_rd(base, DW_IC_CLR_INTR);
  85. }
  86. /* Stop anything in flight and put the controller back in the idle state. */
  87. static void dw_recover(unsigned int base)
  88. {
  89. dw_wr(base, DW_IC_ENABLE, IC_ENABLE_EN | IC_ENABLE_ABORT);
  90. dw_delay_ms(1);
  91. dw_wr(base, DW_IC_ENABLE, 0);
  92. dw_clear_irq(base);
  93. dw_wr(base, DW_IC_ENABLE, IC_ENABLE_EN);
  94. }
  95. unsigned int i2c_dw_comp_type(unsigned int base)
  96. {
  97. return dw_rd(base, DW_IC_COMP_TYPE);
  98. }
  99. void i2c_dw_reset_release(unsigned int base)
  100. {
  101. /* The LPSS private block is at 0x200 and its reset register at 0x204
  102. * (Linux intel-lpss LPSS_PRIV_RESETS). 0x800 is the iDMA64 block,
  103. * not a second private block, so it is left alone. The Linux
  104. * intel-lpss driver asserts the reset first and then releases it
  105. * (FUNC | IDMA bits); a block left in functional reset keeps the
  106. * register file alive but never clocks the I2C master FSM, which
  107. * looks exactly like "commands sit in the TX FIFO forever". */
  108. dw_wr(base, 0x204, 0);
  109. dw_delay_ms(1);
  110. dw_wr(base, 0x204, 7);
  111. dw_delay_ms(1);
  112. }
  113. int i2c_dw_present(unsigned int base)
  114. {
  115. return i2c_dw_comp_type(base) == DW_IC_COMP_TYPE_VALUE;
  116. }
  117. int i2c_dw_init(unsigned int base)
  118. {
  119. unsigned int t0;
  120. if (!i2c_dw_present(base))
  121. return -1;
  122. /* Release the LPSS block from reset. */
  123. i2c_dw_reset_release(base);
  124. dw_wr(base, DW_IC_ENABLE, 0);
  125. t0 = get_ticks_ms();
  126. while (get_ticks_ms() - t0 < 20) {
  127. if (!(dw_rd(base, DW_IC_ENABLE_STATUS) & 1))
  128. break;
  129. }
  130. dw_wr(base, DW_IC_CON, IC_CON_MASTER | IC_CON_SPEED_FS |
  131. IC_CON_RESTART_EN | IC_CON_SLAVE_DISABLE);
  132. /* 400 kHz at 133 MHz; 300 kHz at 100 MHz. The LPSS clock is one
  133. * of those, so the bus never exceeds the touchpad's 400 kHz. */
  134. dw_wr(base, DW_IC_FS_SCL_HCNT, 112);
  135. dw_wr(base, DW_IC_FS_SCL_LCNT, 211);
  136. dw_wr(base, DW_IC_FS_SPKLEN, 1);
  137. dw_wr(base, DW_IC_RX_TL, 0);
  138. dw_wr(base, DW_IC_TX_TL, 0);
  139. dw_clear_irq(base);
  140. dw_wr(base, DW_IC_ENABLE, IC_ENABLE_EN);
  141. t0 = get_ticks_ms();
  142. while (get_ticks_ms() - t0 < 20) {
  143. if (dw_rd(base, DW_IC_ENABLE_STATUS) & 1)
  144. return 0;
  145. }
  146. return -1;
  147. }
  148. int i2c_dw_transfer(unsigned int base, unsigned int addr,
  149. const unsigned char *wr, unsigned int wrlen,
  150. unsigned char *rd, unsigned int rdlen)
  151. {
  152. unsigned int i, cmd, t0, deadline;
  153. if (!wrlen && !rdlen)
  154. return 0;
  155. /* Worst case at 300 kHz, plus slack for clock stretching. Keep it
  156. * short: a failed poll must not block the housekeeping loop. */
  157. deadline = 10 + (wrlen + rdlen);
  158. dw_wr(base, DW_IC_TAR, addr & 0x7f);
  159. dw_clear_irq(base);
  160. i2c_last_abort = 0;
  161. for (i = 0; i < wrlen; i++) {
  162. if (dw_wait(base, DW_IC_STATUS, IC_STATUS_TFNF,
  163. IC_STATUS_TFNF, deadline))
  164. goto fail;
  165. cmd = wr[i];
  166. if (i + 1 == wrlen && !rdlen)
  167. cmd |= IC_DATA_CMD_STOP;
  168. dw_wr(base, DW_IC_DATA_CMD, cmd);
  169. }
  170. for (i = 0; i < rdlen; i++) {
  171. if (dw_wait(base, DW_IC_STATUS, IC_STATUS_TFNF,
  172. IC_STATUS_TFNF, deadline))
  173. goto fail;
  174. cmd = IC_DATA_CMD_READ;
  175. if (i == 0 && wrlen)
  176. cmd |= IC_DATA_CMD_RESTART;
  177. if (i + 1 == rdlen)
  178. cmd |= IC_DATA_CMD_STOP;
  179. dw_wr(base, DW_IC_DATA_CMD, cmd);
  180. if (dw_wait(base, DW_IC_STATUS, IC_STATUS_RFNE,
  181. IC_STATUS_RFNE, deadline))
  182. goto fail;
  183. rd[i] = (unsigned char)(dw_rd(base, DW_IC_DATA_CMD) & 0xff);
  184. }
  185. if (!rdlen) {
  186. /* A write-only transfer is done when the controller has
  187. * consumed every command (TX FIFO empty). MST_ACTIVITY is
  188. * already low before the transfer starts, so checking it
  189. * alone would report a hung FIFO as success. */
  190. t0 = get_ticks_ms();
  191. while (get_ticks_ms() - t0 < deadline) {
  192. if (dw_rd(base, DW_IC_STATUS) & IC_STATUS_TFE)
  193. break;
  194. if (dw_rd(base, DW_IC_RAW_INTR_STAT) & IC_RAW_TX_ABRT)
  195. goto fail;
  196. }
  197. if (!(dw_rd(base, DW_IC_STATUS) & IC_STATUS_TFE))
  198. goto fail;
  199. dw_clear_irq(base);
  200. return 0;
  201. }
  202. /* Wait for the STOP to finish. */
  203. t0 = get_ticks_ms();
  204. while (get_ticks_ms() - t0 < deadline) {
  205. if (!(dw_rd(base, DW_IC_STATUS) & IC_STATUS_MST_ACTIVITY))
  206. break;
  207. }
  208. if (dw_rd(base, DW_IC_RAW_INTR_STAT) & IC_RAW_TX_ABRT)
  209. goto fail;
  210. dw_clear_irq(base);
  211. return 0;
  212. fail:
  213. i2c_last_abort = dw_rd(base, DW_IC_TX_ABRT_SOURCE);
  214. dw_recover(base);
  215. return -1;
  216. }