i2c.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 "serial.h"
  15. #include "timer.h"
  16. #define DW_IC_CON 0x00
  17. #define DW_IC_TAR 0x04
  18. #define DW_IC_DATA_CMD 0x10
  19. #define DW_IC_SS_SCL_HCNT 0x14
  20. #define DW_IC_SS_SCL_LCNT 0x18
  21. #define DW_IC_FS_SCL_HCNT 0x1c
  22. #define DW_IC_FS_SCL_LCNT 0x20
  23. #define DW_IC_RAW_INTR_STAT 0x34
  24. #define DW_IC_RX_TL 0x38
  25. #define DW_IC_TX_TL 0x3c
  26. #define DW_IC_CLR_INTR 0x40
  27. #define DW_IC_CLR_TX_ABRT 0x54
  28. #define DW_IC_ENABLE 0x6c
  29. #define DW_IC_STATUS 0x70
  30. #define DW_IC_TX_ABRT_SOURCE 0x80
  31. #define DW_IC_TXFLR 0x74
  32. #define DW_IC_RXFLR 0x78
  33. #define DW_IC_ENABLE_STATUS 0x9c
  34. #define DW_IC_FS_SPKLEN 0xa0
  35. #define DW_IC_SDA_HOLD 0x7c
  36. #define DW_IC_COMP_VERSION 0xf8
  37. #define DW_IC_COMP_TYPE 0xfc
  38. #define DW_IC_COMP_TYPE_VALUE 0x44570140u
  39. #define DW_LPSS_RESET 0x804
  40. #define IC_CON_MASTER 0x00000001u
  41. #define IC_CON_SPEED_FS 0x00000004u
  42. #define IC_CON_RESTART_EN 0x00000020u
  43. #define IC_CON_SLAVE_DISABLE 0x00000040u
  44. #define IC_DATA_CMD_READ 0x00000100u
  45. #define IC_DATA_CMD_STOP 0x00000200u
  46. #define IC_DATA_CMD_RESTART 0x00000400u
  47. #define IC_STATUS_TFNF 0x00000002u
  48. #define IC_STATUS_RFNE 0x00000008u
  49. #define IC_STATUS_TFE 0x00000004u
  50. #define IC_STATUS_MST_ACTIVITY 0x00000020u
  51. #define IC_ENABLE_EN 0x00000001u
  52. #define IC_ENABLE_ABORT 0x00000002u
  53. #define IC_RAW_TX_ABRT 0x00000040u
  54. unsigned int i2c_last_abort;
  55. static unsigned int dw_rd(unsigned int base, unsigned int off)
  56. {
  57. return *(volatile unsigned int *)(base + off);
  58. }
  59. static void dw_wr(unsigned int base, unsigned int off, unsigned int val)
  60. {
  61. *(volatile unsigned int *)(base + off) = val;
  62. }
  63. static void dw_delay_ms(unsigned int ms)
  64. {
  65. unsigned int t0 = get_ticks_ms();
  66. while (get_ticks_ms() - t0 < ms)
  67. ;
  68. }
  69. /* Wait until (status & mask) == want. A bus abort ends the wait early so a
  70. * missing device cannot burn the whole timeout. */
  71. static int dw_wait(unsigned int base, unsigned int off, unsigned int mask,
  72. unsigned int want, unsigned int ms)
  73. {
  74. unsigned int t0 = get_ticks_ms();
  75. while (get_ticks_ms() - t0 < ms) {
  76. if ((dw_rd(base, off) & mask) == want)
  77. return 0;
  78. if (dw_rd(base, DW_IC_RAW_INTR_STAT) & IC_RAW_TX_ABRT)
  79. return -1;
  80. }
  81. return -1;
  82. }
  83. static void dw_clear_irq(unsigned int base)
  84. {
  85. (void)dw_rd(base, DW_IC_CLR_TX_ABRT);
  86. (void)dw_rd(base, DW_IC_CLR_INTR);
  87. }
  88. /* Stop anything in flight and put the controller back in the idle state. */
  89. static void dw_recover(unsigned int base)
  90. {
  91. dw_wr(base, DW_IC_ENABLE, IC_ENABLE_EN | IC_ENABLE_ABORT);
  92. dw_delay_ms(1);
  93. dw_wr(base, DW_IC_ENABLE, 0);
  94. dw_clear_irq(base);
  95. dw_wr(base, DW_IC_ENABLE, IC_ENABLE_EN);
  96. }
  97. unsigned int i2c_dw_comp_type(unsigned int base)
  98. {
  99. return dw_rd(base, DW_IC_COMP_TYPE);
  100. }
  101. void i2c_dw_reset_release(unsigned int base)
  102. {
  103. /* The LPSS private block is at 0x200 and its reset register at 0x204
  104. * (Linux intel-lpss LPSS_PRIV_RESETS). 0x800 is the iDMA64 block,
  105. * not a second private block, so it is left alone. The Linux
  106. * intel-lpss driver asserts the reset first and then releases it
  107. * (FUNC | IDMA bits); a block left in functional reset keeps the
  108. * register file alive but never clocks the I2C master FSM, which
  109. * looks exactly like "commands sit in the TX FIFO forever". */
  110. dw_wr(base, 0x204, 0);
  111. dw_delay_ms(1);
  112. dw_wr(base, 0x204, 7);
  113. dw_delay_ms(1);
  114. }
  115. int i2c_dw_present(unsigned int base)
  116. {
  117. return i2c_dw_comp_type(base) == DW_IC_COMP_TYPE_VALUE;
  118. }
  119. void i2c_dw_dump(unsigned int base)
  120. {
  121. serial_puts(SERIAL_COM1, "touchpad: i2c con=");
  122. serial_puthex(SERIAL_COM1, dw_rd(base, DW_IC_CON));
  123. serial_puts(SERIAL_COM1, " tar=");
  124. serial_puthex(SERIAL_COM1, dw_rd(base, DW_IC_TAR));
  125. serial_puts(SERIAL_COM1, " status=");
  126. serial_puthex(SERIAL_COM1, dw_rd(base, DW_IC_STATUS));
  127. serial_puts(SERIAL_COM1, " en=");
  128. serial_puthex(SERIAL_COM1, dw_rd(base, DW_IC_ENABLE));
  129. serial_puts(SERIAL_COM1, " enst=");
  130. serial_puthex(SERIAL_COM1, dw_rd(base, DW_IC_ENABLE_STATUS));
  131. serial_puts(SERIAL_COM1, " abrt=");
  132. serial_puthex(SERIAL_COM1, dw_rd(base, DW_IC_TX_ABRT_SOURCE));
  133. serial_puts(SERIAL_COM1, " sda=");
  134. serial_puthex(SERIAL_COM1, dw_rd(base, DW_IC_SDA_HOLD));
  135. serial_puts(SERIAL_COM1, " spk=");
  136. serial_puthex(SERIAL_COM1, dw_rd(base, DW_IC_FS_SPKLEN));
  137. serial_puts(SERIAL_COM1, " ss=");
  138. serial_puthex(SERIAL_COM1, dw_rd(base, DW_IC_SS_SCL_HCNT));
  139. serial_putc(SERIAL_COM1, '/');
  140. serial_puthex(SERIAL_COM1, dw_rd(base, DW_IC_SS_SCL_LCNT));
  141. serial_puts(SERIAL_COM1, " fs=");
  142. serial_puthex(SERIAL_COM1, dw_rd(base, DW_IC_FS_SCL_HCNT));
  143. serial_putc(SERIAL_COM1, '/');
  144. serial_puthex(SERIAL_COM1, dw_rd(base, DW_IC_FS_SCL_LCNT));
  145. serial_putc(SERIAL_COM1, '\n');
  146. }
  147. int i2c_dw_init(unsigned int base)
  148. {
  149. unsigned int t0;
  150. if (!i2c_dw_present(base))
  151. return -1;
  152. /* Release the LPSS block from reset. */
  153. i2c_dw_reset_release(base);
  154. dw_wr(base, DW_IC_ENABLE, 0);
  155. t0 = get_ticks_ms();
  156. while (get_ticks_ms() - t0 < 20) {
  157. if (!(dw_rd(base, DW_IC_ENABLE_STATUS) & 1))
  158. break;
  159. }
  160. dw_wr(base, DW_IC_CON, IC_CON_MASTER | IC_CON_SPEED_FS |
  161. IC_CON_RESTART_EN | IC_CON_SLAVE_DISABLE);
  162. /* 400 kHz at 133 MHz; 300 kHz at 100 MHz. The LPSS clock is one
  163. * of those, so the bus never exceeds the touchpad's 400 kHz. */
  164. dw_wr(base, DW_IC_FS_SCL_HCNT, 112);
  165. dw_wr(base, DW_IC_FS_SCL_LCNT, 211);
  166. dw_wr(base, DW_IC_FS_SPKLEN, 1);
  167. /* Sunrise Point-LP declares an i2c-sda-hold-time-ns of 230; at
  168. * 120 MHz that is ~28 IC clocks. The Synaptics touchpad NAKs its
  169. * address without a hold time, while the Braswell Elan tolerates
  170. * the reset value. */
  171. dw_wr(base, DW_IC_SDA_HOLD, 30);
  172. dw_wr(base, DW_IC_RX_TL, 0);
  173. dw_wr(base, DW_IC_TX_TL, 0);
  174. dw_clear_irq(base);
  175. dw_wr(base, DW_IC_ENABLE, IC_ENABLE_EN);
  176. t0 = get_ticks_ms();
  177. while (get_ticks_ms() - t0 < 20) {
  178. if (dw_rd(base, DW_IC_ENABLE_STATUS) & 1)
  179. return 0;
  180. }
  181. return -1;
  182. }
  183. int i2c_dw_transfer(unsigned int base, unsigned int addr,
  184. const unsigned char *wr, unsigned int wrlen,
  185. unsigned char *rd, unsigned int rdlen)
  186. {
  187. unsigned int i, cmd, t0, deadline;
  188. if (!wrlen && !rdlen)
  189. return 0;
  190. /* Worst case at 300 kHz, plus slack for clock stretching. Keep it
  191. * short: a failed poll must not block the housekeeping loop. */
  192. deadline = 10 + (wrlen + rdlen);
  193. /* IC_TAR may only change while the controller is disabled (unless
  194. * the IP was built with IC_DYNAMIC_TAR_UPDATE); Linux's
  195. * i2c_dw_xfer_init() disables it around the write as well. */
  196. if ((dw_rd(base, DW_IC_TAR) & 0x3ff) != (addr & 0x7f)) {
  197. dw_wr(base, DW_IC_ENABLE, 0);
  198. t0 = get_ticks_ms();
  199. while ((dw_rd(base, DW_IC_ENABLE_STATUS) & 1) &&
  200. get_ticks_ms() - t0 < 5)
  201. ;
  202. dw_wr(base, DW_IC_TAR, addr & 0x7f);
  203. dw_wr(base, DW_IC_ENABLE, IC_ENABLE_EN);
  204. }
  205. dw_clear_irq(base);
  206. i2c_last_abort = 0;
  207. for (i = 0; i < wrlen; i++) {
  208. if (dw_wait(base, DW_IC_STATUS, IC_STATUS_TFNF,
  209. IC_STATUS_TFNF, deadline))
  210. goto fail;
  211. cmd = wr[i];
  212. if (i + 1 == wrlen && !rdlen)
  213. cmd |= IC_DATA_CMD_STOP;
  214. dw_wr(base, DW_IC_DATA_CMD, cmd);
  215. }
  216. for (i = 0; i < rdlen; i++) {
  217. if (dw_wait(base, DW_IC_STATUS, IC_STATUS_TFNF,
  218. IC_STATUS_TFNF, deadline))
  219. goto fail;
  220. cmd = IC_DATA_CMD_READ;
  221. if (i == 0 && wrlen)
  222. cmd |= IC_DATA_CMD_RESTART;
  223. if (i + 1 == rdlen)
  224. cmd |= IC_DATA_CMD_STOP;
  225. dw_wr(base, DW_IC_DATA_CMD, cmd);
  226. if (dw_wait(base, DW_IC_STATUS, IC_STATUS_RFNE,
  227. IC_STATUS_RFNE, deadline))
  228. goto fail;
  229. rd[i] = (unsigned char)(dw_rd(base, DW_IC_DATA_CMD) & 0xff);
  230. }
  231. if (!rdlen) {
  232. /* A write-only transfer is done when the controller has
  233. * consumed every command (TX FIFO empty). MST_ACTIVITY is
  234. * already low before the transfer starts, so checking it
  235. * alone would report a hung FIFO as success. */
  236. t0 = get_ticks_ms();
  237. while (get_ticks_ms() - t0 < deadline) {
  238. if (dw_rd(base, DW_IC_STATUS) & IC_STATUS_TFE)
  239. break;
  240. if (dw_rd(base, DW_IC_RAW_INTR_STAT) & IC_RAW_TX_ABRT)
  241. goto fail;
  242. }
  243. if (!(dw_rd(base, DW_IC_STATUS) & IC_STATUS_TFE))
  244. goto fail;
  245. dw_clear_irq(base);
  246. return 0;
  247. }
  248. /* Wait for the STOP to finish. */
  249. t0 = get_ticks_ms();
  250. while (get_ticks_ms() - t0 < deadline) {
  251. if (!(dw_rd(base, DW_IC_STATUS) & IC_STATUS_MST_ACTIVITY))
  252. break;
  253. }
  254. if (dw_rd(base, DW_IC_RAW_INTR_STAT) & IC_RAW_TX_ABRT)
  255. goto fail;
  256. dw_clear_irq(base);
  257. return 0;
  258. fail:
  259. i2c_last_abort = dw_rd(base, DW_IC_TX_ABRT_SOURCE);
  260. dw_recover(base);
  261. return -1;
  262. }