i2c.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 at 0x800 holds the reset release register; 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. /* Bay Trail/Braswell put the LPSS private block at 0x800 (reset at
  102. * 0x804); newer LPSS layouts put it at 0x200 (reset at 0x204). The
  103. * Linux intel-lpss driver asserts the reset first and then releases
  104. * it, so do both here: a block left in functional reset keeps the
  105. * register file alive but never clocks the I2C master FSM, which
  106. * looks exactly like "commands sit in the TX FIFO forever". */
  107. dw_wr(base, 0x804, 0);
  108. dw_wr(base, 0x204, 0);
  109. dw_delay_ms(1);
  110. dw_wr(base, 0x804, 3);
  111. dw_wr(base, 0x204, 3);
  112. dw_delay_ms(1);
  113. }
  114. int i2c_dw_present(unsigned int base)
  115. {
  116. return i2c_dw_comp_type(base) == DW_IC_COMP_TYPE_VALUE;
  117. }
  118. int i2c_dw_init(unsigned int base)
  119. {
  120. unsigned int t0;
  121. if (!i2c_dw_present(base))
  122. return -1;
  123. /* Release the LPSS block from reset. */
  124. i2c_dw_reset_release(base);
  125. dw_wr(base, DW_IC_ENABLE, 0);
  126. t0 = get_ticks_ms();
  127. while (get_ticks_ms() - t0 < 20) {
  128. if (!(dw_rd(base, DW_IC_ENABLE_STATUS) & 1))
  129. break;
  130. }
  131. dw_wr(base, DW_IC_CON, IC_CON_MASTER | IC_CON_SPEED_FS |
  132. IC_CON_RESTART_EN | IC_CON_SLAVE_DISABLE);
  133. /* 400 kHz at 133 MHz; 300 kHz at 100 MHz. The LPSS clock is one
  134. * of those, so the bus never exceeds the touchpad's 400 kHz. */
  135. dw_wr(base, DW_IC_FS_SCL_HCNT, 112);
  136. dw_wr(base, DW_IC_FS_SCL_LCNT, 211);
  137. dw_wr(base, DW_IC_FS_SPKLEN, 1);
  138. dw_wr(base, DW_IC_RX_TL, 0);
  139. dw_wr(base, DW_IC_TX_TL, 0);
  140. dw_clear_irq(base);
  141. dw_wr(base, DW_IC_ENABLE, IC_ENABLE_EN);
  142. t0 = get_ticks_ms();
  143. while (get_ticks_ms() - t0 < 20) {
  144. if (dw_rd(base, DW_IC_ENABLE_STATUS) & 1)
  145. return 0;
  146. }
  147. return -1;
  148. }
  149. int i2c_dw_transfer(unsigned int base, unsigned int addr,
  150. const unsigned char *wr, unsigned int wrlen,
  151. unsigned char *rd, unsigned int rdlen)
  152. {
  153. unsigned int i, cmd, t0, deadline;
  154. if (!wrlen && !rdlen)
  155. return 0;
  156. /* Worst case at 300 kHz, plus slack for clock stretching. Keep it
  157. * short: a failed poll must not block the housekeeping loop. */
  158. deadline = 10 + (wrlen + rdlen);
  159. dw_wr(base, DW_IC_TAR, addr & 0x7f);
  160. dw_clear_irq(base);
  161. i2c_last_abort = 0;
  162. for (i = 0; i < wrlen; i++) {
  163. if (dw_wait(base, DW_IC_STATUS, IC_STATUS_TFNF,
  164. IC_STATUS_TFNF, deadline))
  165. goto fail;
  166. cmd = wr[i];
  167. if (i + 1 == wrlen && !rdlen)
  168. cmd |= IC_DATA_CMD_STOP;
  169. dw_wr(base, DW_IC_DATA_CMD, cmd);
  170. }
  171. for (i = 0; i < rdlen; i++) {
  172. if (dw_wait(base, DW_IC_STATUS, IC_STATUS_TFNF,
  173. IC_STATUS_TFNF, deadline))
  174. goto fail;
  175. cmd = IC_DATA_CMD_READ;
  176. if (i == 0 && wrlen)
  177. cmd |= IC_DATA_CMD_RESTART;
  178. if (i + 1 == rdlen)
  179. cmd |= IC_DATA_CMD_STOP;
  180. dw_wr(base, DW_IC_DATA_CMD, cmd);
  181. if (dw_wait(base, DW_IC_STATUS, IC_STATUS_RFNE,
  182. IC_STATUS_RFNE, deadline))
  183. goto fail;
  184. rd[i] = (unsigned char)(dw_rd(base, DW_IC_DATA_CMD) & 0xff);
  185. }
  186. if (!rdlen) {
  187. /* A write-only transfer is done when the controller has
  188. * consumed every command (TX FIFO empty). MST_ACTIVITY is
  189. * already low before the transfer starts, so checking it
  190. * alone would report a hung FIFO as success. */
  191. t0 = get_ticks_ms();
  192. while (get_ticks_ms() - t0 < deadline) {
  193. if (dw_rd(base, DW_IC_STATUS) & IC_STATUS_TFE)
  194. break;
  195. if (dw_rd(base, DW_IC_RAW_INTR_STAT) & IC_RAW_TX_ABRT)
  196. goto fail;
  197. }
  198. if (!(dw_rd(base, DW_IC_STATUS) & IC_STATUS_TFE))
  199. goto fail;
  200. dw_clear_irq(base);
  201. return 0;
  202. }
  203. /* Wait for the STOP to finish. */
  204. t0 = get_ticks_ms();
  205. while (get_ticks_ms() - t0 < deadline) {
  206. if (!(dw_rd(base, DW_IC_STATUS) & IC_STATUS_MST_ACTIVITY))
  207. break;
  208. }
  209. if (dw_rd(base, DW_IC_RAW_INTR_STAT) & IC_RAW_TX_ABRT)
  210. goto fail;
  211. dw_clear_irq(base);
  212. return 0;
  213. fail:
  214. i2c_last_abort = dw_rd(base, DW_IC_TX_ABRT_SOURCE);
  215. dw_recover(base);
  216. return -1;
  217. }