i2c.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. void i2c_dw_clock_spt(unsigned int base)
  148. {
  149. /* LPSS private 0x200: bit 0 gates the functional clock, bits 1..15
  150. * are the M divider, bits 16..30 the N divider, bit 31 updates the
  151. * divider. With the gate off the DesignWare register file still
  152. * answers (the component type reads) but the I2C FSM never drives
  153. * the bus. Linux's intel-lpss enables it before the device probe;
  154. * the BIOS leaves unused LPSS blocks gated. SPT-LP runs the LPSS
  155. * clock at 120 MHz with the unity divider (the clk_summary shows
  156. * 120000000 for i2c_designware.0 pclk), so M=N=1. */
  157. dw_wr(base, 0x200, 0x80010003u);
  158. dw_delay_ms(1);
  159. }
  160. int i2c_dw_init(unsigned int base)
  161. {
  162. unsigned int t0;
  163. if (!i2c_dw_present(base))
  164. return -1;
  165. /* Release the LPSS block from reset. */
  166. i2c_dw_reset_release(base);
  167. dw_wr(base, DW_IC_ENABLE, 0);
  168. t0 = get_ticks_ms();
  169. while (get_ticks_ms() - t0 < 20) {
  170. if (!(dw_rd(base, DW_IC_ENABLE_STATUS) & 1))
  171. break;
  172. }
  173. dw_wr(base, DW_IC_CON, IC_CON_MASTER | IC_CON_SPEED_FS |
  174. IC_CON_RESTART_EN | IC_CON_SLAVE_DISABLE);
  175. /* 400 kHz at 133 MHz; 300 kHz at 100 MHz. The LPSS clock is one
  176. * of those, so the bus never exceeds the touchpad's 400 kHz. */
  177. dw_wr(base, DW_IC_FS_SCL_HCNT, 112);
  178. dw_wr(base, DW_IC_FS_SCL_LCNT, 211);
  179. dw_wr(base, DW_IC_FS_SPKLEN, 1);
  180. /* Sunrise Point-LP declares an i2c-sda-hold-time-ns of 230; at
  181. * 120 MHz that is ~28 IC clocks. The Synaptics touchpad NAKs its
  182. * address without a hold time, while the Braswell Elan tolerates
  183. * the reset value. */
  184. dw_wr(base, DW_IC_SDA_HOLD, 30);
  185. dw_wr(base, DW_IC_RX_TL, 0);
  186. dw_wr(base, DW_IC_TX_TL, 0);
  187. dw_clear_irq(base);
  188. dw_wr(base, DW_IC_ENABLE, IC_ENABLE_EN);
  189. t0 = get_ticks_ms();
  190. while (get_ticks_ms() - t0 < 20) {
  191. if (dw_rd(base, DW_IC_ENABLE_STATUS) & 1)
  192. return 0;
  193. }
  194. return -1;
  195. }
  196. int i2c_dw_transfer(unsigned int base, unsigned int addr,
  197. const unsigned char *wr, unsigned int wrlen,
  198. unsigned char *rd, unsigned int rdlen)
  199. {
  200. unsigned int i, cmd, t0, deadline;
  201. if (!wrlen && !rdlen)
  202. return 0;
  203. /* Worst case at 300 kHz, plus slack for clock stretching. Keep it
  204. * short: a failed poll must not block the housekeeping loop. */
  205. deadline = 10 + (wrlen + rdlen);
  206. dw_wr(base, DW_IC_TAR, addr & 0x7f);
  207. dw_clear_irq(base);
  208. i2c_last_abort = 0;
  209. for (i = 0; i < wrlen; i++) {
  210. if (dw_wait(base, DW_IC_STATUS, IC_STATUS_TFNF,
  211. IC_STATUS_TFNF, deadline))
  212. goto fail;
  213. cmd = wr[i];
  214. if (i + 1 == wrlen && !rdlen)
  215. cmd |= IC_DATA_CMD_STOP;
  216. dw_wr(base, DW_IC_DATA_CMD, cmd);
  217. }
  218. for (i = 0; i < rdlen; i++) {
  219. if (dw_wait(base, DW_IC_STATUS, IC_STATUS_TFNF,
  220. IC_STATUS_TFNF, deadline))
  221. goto fail;
  222. cmd = IC_DATA_CMD_READ;
  223. if (i == 0 && wrlen)
  224. cmd |= IC_DATA_CMD_RESTART;
  225. if (i + 1 == rdlen)
  226. cmd |= IC_DATA_CMD_STOP;
  227. dw_wr(base, DW_IC_DATA_CMD, cmd);
  228. if (dw_wait(base, DW_IC_STATUS, IC_STATUS_RFNE,
  229. IC_STATUS_RFNE, deadline))
  230. goto fail;
  231. rd[i] = (unsigned char)(dw_rd(base, DW_IC_DATA_CMD) & 0xff);
  232. }
  233. if (!rdlen) {
  234. /* A write-only transfer is done when the controller has
  235. * consumed every command (TX FIFO empty). MST_ACTIVITY is
  236. * already low before the transfer starts, so checking it
  237. * alone would report a hung FIFO as success. */
  238. t0 = get_ticks_ms();
  239. while (get_ticks_ms() - t0 < deadline) {
  240. if (dw_rd(base, DW_IC_STATUS) & IC_STATUS_TFE)
  241. break;
  242. if (dw_rd(base, DW_IC_RAW_INTR_STAT) & IC_RAW_TX_ABRT)
  243. goto fail;
  244. }
  245. if (!(dw_rd(base, DW_IC_STATUS) & IC_STATUS_TFE))
  246. goto fail;
  247. dw_clear_irq(base);
  248. return 0;
  249. }
  250. /* Wait for the STOP to finish. */
  251. t0 = get_ticks_ms();
  252. while (get_ticks_ms() - t0 < deadline) {
  253. if (!(dw_rd(base, DW_IC_STATUS) & IC_STATUS_MST_ACTIVITY))
  254. break;
  255. }
  256. if (dw_rd(base, DW_IC_RAW_INTR_STAT) & IC_RAW_TX_ABRT)
  257. goto fail;
  258. dw_clear_irq(base);
  259. return 0;
  260. fail:
  261. i2c_last_abort = dw_rd(base, DW_IC_TX_ABRT_SOURCE);
  262. dw_recover(base);
  263. return -1;
  264. }