i2c-designware-core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*
  2. * Synopsys DesignWare I2C adapter driver (master only).
  3. *
  4. * Based on the TI DAVINCI I2C adapter driver.
  5. *
  6. * Copyright (C) 2006 Texas Instruments.
  7. * Copyright (C) 2007 MontaVista Software Inc.
  8. * Copyright (C) 2009 Provigent Ltd.
  9. *
  10. * ----------------------------------------------------------------------------
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. * ----------------------------------------------------------------------------
  26. *
  27. */
  28. #include <linux/clk.h>
  29. #include <linux/errno.h>
  30. #include <linux/err.h>
  31. #include <linux/i2c.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/io.h>
  34. #include <linux/delay.h>
  35. #include "i2c-designware-core.h"
  36. static char *abort_sources[] = {
  37. [ABRT_7B_ADDR_NOACK] =
  38. "slave address not acknowledged (7bit mode)",
  39. [ABRT_10ADDR1_NOACK] =
  40. "first address byte not acknowledged (10bit mode)",
  41. [ABRT_10ADDR2_NOACK] =
  42. "second address byte not acknowledged (10bit mode)",
  43. [ABRT_TXDATA_NOACK] =
  44. "data not acknowledged",
  45. [ABRT_GCALL_NOACK] =
  46. "no acknowledgement for a general call",
  47. [ABRT_GCALL_READ] =
  48. "read after general call",
  49. [ABRT_SBYTE_ACKDET] =
  50. "start byte acknowledged",
  51. [ABRT_SBYTE_NORSTRT] =
  52. "trying to send start byte when restart is disabled",
  53. [ABRT_10B_RD_NORSTRT] =
  54. "trying to read when restart is disabled (10bit mode)",
  55. [ABRT_MASTER_DIS] =
  56. "trying to use disabled adapter",
  57. [ARB_LOST] =
  58. "lost arbitration",
  59. };
  60. u32 dw_readl(struct dw_i2c_dev *dev, int offset)
  61. {
  62. u32 value = readl(dev->base + offset);
  63. if (dev->swab)
  64. return swab32(value);
  65. else
  66. return value;
  67. }
  68. void dw_writel(struct dw_i2c_dev *dev, u32 b, int offset)
  69. {
  70. if (dev->swab)
  71. b = swab32(b);
  72. writel(b, dev->base + offset);
  73. }
  74. static u32
  75. i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset)
  76. {
  77. /*
  78. * DesignWare I2C core doesn't seem to have solid strategy to meet
  79. * the tHD;STA timing spec. Configuring _HCNT based on tHIGH spec
  80. * will result in violation of the tHD;STA spec.
  81. */
  82. if (cond)
  83. /*
  84. * Conditional expression:
  85. *
  86. * IC_[FS]S_SCL_HCNT + (1+4+3) >= IC_CLK * tHIGH
  87. *
  88. * This is based on the DW manuals, and represents an ideal
  89. * configuration. The resulting I2C bus speed will be
  90. * faster than any of the others.
  91. *
  92. * If your hardware is free from tHD;STA issue, try this one.
  93. */
  94. return (ic_clk * tSYMBOL + 5000) / 10000 - 8 + offset;
  95. else
  96. /*
  97. * Conditional expression:
  98. *
  99. * IC_[FS]S_SCL_HCNT + 3 >= IC_CLK * (tHD;STA + tf)
  100. *
  101. * This is just experimental rule; the tHD;STA period turned
  102. * out to be proportinal to (_HCNT + 3). With this setting,
  103. * we could meet both tHIGH and tHD;STA timing specs.
  104. *
  105. * If unsure, you'd better to take this alternative.
  106. *
  107. * The reason why we need to take into account "tf" here,
  108. * is the same as described in i2c_dw_scl_lcnt().
  109. */
  110. return (ic_clk * (tSYMBOL + tf) + 5000) / 10000 - 3 + offset;
  111. }
  112. static u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset)
  113. {
  114. /*
  115. * Conditional expression:
  116. *
  117. * IC_[FS]S_SCL_LCNT + 1 >= IC_CLK * (tLOW + tf)
  118. *
  119. * DW I2C core starts counting the SCL CNTs for the LOW period
  120. * of the SCL clock (tLOW) as soon as it pulls the SCL line.
  121. * In order to meet the tLOW timing spec, we need to take into
  122. * account the fall time of SCL signal (tf). Default tf value
  123. * should be 0.3 us, for safety.
  124. */
  125. return ((ic_clk * (tLOW + tf) + 5000) / 10000) - 1 + offset;
  126. }
  127. /**
  128. * i2c_dw_init() - initialize the designware i2c master hardware
  129. * @dev: device private data
  130. *
  131. * This functions configures and enables the I2C master.
  132. * This function is called during I2C init function, and in case of timeout at
  133. * run time.
  134. */
  135. int i2c_dw_init(struct dw_i2c_dev *dev)
  136. {
  137. u32 input_clock_khz;
  138. u32 ic_con, hcnt, lcnt;
  139. u32 reg;
  140. input_clock_khz = dev->get_clk_rate_khz(dev);
  141. /* Configure register endianess access */
  142. reg = dw_readl(dev, DW_IC_COMP_TYPE);
  143. if (reg == ___constant_swab32(DW_IC_COMP_TYPE_VALUE)) {
  144. dev->swab = 1;
  145. reg = DW_IC_COMP_TYPE_VALUE;
  146. }
  147. if (reg != DW_IC_COMP_TYPE_VALUE) {
  148. dev_err(dev->dev, "Unknown Synopsys component type: "
  149. "0x%08x\n", reg);
  150. return -ENODEV;
  151. }
  152. /* Disable the adapter */
  153. dw_writel(dev, 0, DW_IC_ENABLE);
  154. /* set standard and fast speed deviders for high/low periods */
  155. /* Standard-mode */
  156. hcnt = i2c_dw_scl_hcnt(input_clock_khz,
  157. 40, /* tHD;STA = tHIGH = 4.0 us */
  158. 3, /* tf = 0.3 us */
  159. 0, /* 0: DW default, 1: Ideal */
  160. 0); /* No offset */
  161. lcnt = i2c_dw_scl_lcnt(input_clock_khz,
  162. 47, /* tLOW = 4.7 us */
  163. 3, /* tf = 0.3 us */
  164. 0); /* No offset */
  165. dw_writel(dev, hcnt, DW_IC_SS_SCL_HCNT);
  166. dw_writel(dev, lcnt, DW_IC_SS_SCL_LCNT);
  167. dev_dbg(dev->dev, "Standard-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
  168. /* Fast-mode */
  169. hcnt = i2c_dw_scl_hcnt(input_clock_khz,
  170. 6, /* tHD;STA = tHIGH = 0.6 us */
  171. 3, /* tf = 0.3 us */
  172. 0, /* 0: DW default, 1: Ideal */
  173. 0); /* No offset */
  174. lcnt = i2c_dw_scl_lcnt(input_clock_khz,
  175. 13, /* tLOW = 1.3 us */
  176. 3, /* tf = 0.3 us */
  177. 0); /* No offset */
  178. dw_writel(dev, hcnt, DW_IC_FS_SCL_HCNT);
  179. dw_writel(dev, lcnt, DW_IC_FS_SCL_LCNT);
  180. dev_dbg(dev->dev, "Fast-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
  181. /* Configure Tx/Rx FIFO threshold levels */
  182. dw_writel(dev, dev->tx_fifo_depth - 1, DW_IC_TX_TL);
  183. dw_writel(dev, 0, DW_IC_RX_TL);
  184. /* configure the i2c master */
  185. ic_con = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE |
  186. DW_IC_CON_RESTART_EN | DW_IC_CON_SPEED_FAST;
  187. dw_writel(dev, ic_con, DW_IC_CON);
  188. return 0;
  189. }
  190. /*
  191. * Waiting for bus not busy
  192. */
  193. static int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev)
  194. {
  195. int timeout = TIMEOUT;
  196. while (dw_readl(dev, DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) {
  197. if (timeout <= 0) {
  198. dev_warn(dev->dev, "timeout waiting for bus ready\n");
  199. return -ETIMEDOUT;
  200. }
  201. timeout--;
  202. mdelay(1);
  203. }
  204. return 0;
  205. }
  206. static void i2c_dw_xfer_init(struct dw_i2c_dev *dev)
  207. {
  208. struct i2c_msg *msgs = dev->msgs;
  209. u32 ic_con;
  210. /* Disable the adapter */
  211. dw_writel(dev, 0, DW_IC_ENABLE);
  212. /* set the slave (target) address */
  213. dw_writel(dev, msgs[dev->msg_write_idx].addr, DW_IC_TAR);
  214. /* if the slave address is ten bit address, enable 10BITADDR */
  215. ic_con = dw_readl(dev, DW_IC_CON);
  216. if (msgs[dev->msg_write_idx].flags & I2C_M_TEN)
  217. ic_con |= DW_IC_CON_10BITADDR_MASTER;
  218. else
  219. ic_con &= ~DW_IC_CON_10BITADDR_MASTER;
  220. dw_writel(dev, ic_con, DW_IC_CON);
  221. /* Enable the adapter */
  222. dw_writel(dev, 1, DW_IC_ENABLE);
  223. /* Enable interrupts */
  224. dw_writel(dev, DW_IC_INTR_DEFAULT_MASK, DW_IC_INTR_MASK);
  225. }
  226. /*
  227. * Initiate (and continue) low level master read/write transaction.
  228. * This function is only called from i2c_dw_isr, and pumping i2c_msg
  229. * messages into the tx buffer. Even if the size of i2c_msg data is
  230. * longer than the size of the tx buffer, it handles everything.
  231. */
  232. void
  233. i2c_dw_xfer_msg(struct dw_i2c_dev *dev)
  234. {
  235. struct i2c_msg *msgs = dev->msgs;
  236. u32 intr_mask;
  237. int tx_limit, rx_limit;
  238. u32 addr = msgs[dev->msg_write_idx].addr;
  239. u32 buf_len = dev->tx_buf_len;
  240. u8 *buf = dev->tx_buf;
  241. intr_mask = DW_IC_INTR_DEFAULT_MASK;
  242. for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) {
  243. /*
  244. * if target address has changed, we need to
  245. * reprogram the target address in the i2c
  246. * adapter when we are done with this transfer
  247. */
  248. if (msgs[dev->msg_write_idx].addr != addr) {
  249. dev_err(dev->dev,
  250. "%s: invalid target address\n", __func__);
  251. dev->msg_err = -EINVAL;
  252. break;
  253. }
  254. if (msgs[dev->msg_write_idx].len == 0) {
  255. dev_err(dev->dev,
  256. "%s: invalid message length\n", __func__);
  257. dev->msg_err = -EINVAL;
  258. break;
  259. }
  260. if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {
  261. /* new i2c_msg */
  262. buf = msgs[dev->msg_write_idx].buf;
  263. buf_len = msgs[dev->msg_write_idx].len;
  264. }
  265. tx_limit = dev->tx_fifo_depth - dw_readl(dev, DW_IC_TXFLR);
  266. rx_limit = dev->rx_fifo_depth - dw_readl(dev, DW_IC_RXFLR);
  267. while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) {
  268. if (msgs[dev->msg_write_idx].flags & I2C_M_RD) {
  269. dw_writel(dev, 0x100, DW_IC_DATA_CMD);
  270. rx_limit--;
  271. } else
  272. dw_writel(dev, *buf++, DW_IC_DATA_CMD);
  273. tx_limit--; buf_len--;
  274. }
  275. dev->tx_buf = buf;
  276. dev->tx_buf_len = buf_len;
  277. if (buf_len > 0) {
  278. /* more bytes to be written */
  279. dev->status |= STATUS_WRITE_IN_PROGRESS;
  280. break;
  281. } else
  282. dev->status &= ~STATUS_WRITE_IN_PROGRESS;
  283. }
  284. /*
  285. * If i2c_msg index search is completed, we don't need TX_EMPTY
  286. * interrupt any more.
  287. */
  288. if (dev->msg_write_idx == dev->msgs_num)
  289. intr_mask &= ~DW_IC_INTR_TX_EMPTY;
  290. if (dev->msg_err)
  291. intr_mask = 0;
  292. dw_writel(dev, intr_mask, DW_IC_INTR_MASK);
  293. }
  294. static void
  295. i2c_dw_read(struct dw_i2c_dev *dev)
  296. {
  297. struct i2c_msg *msgs = dev->msgs;
  298. int rx_valid;
  299. for (; dev->msg_read_idx < dev->msgs_num; dev->msg_read_idx++) {
  300. u32 len;
  301. u8 *buf;
  302. if (!(msgs[dev->msg_read_idx].flags & I2C_M_RD))
  303. continue;
  304. if (!(dev->status & STATUS_READ_IN_PROGRESS)) {
  305. len = msgs[dev->msg_read_idx].len;
  306. buf = msgs[dev->msg_read_idx].buf;
  307. } else {
  308. len = dev->rx_buf_len;
  309. buf = dev->rx_buf;
  310. }
  311. rx_valid = dw_readl(dev, DW_IC_RXFLR);
  312. for (; len > 0 && rx_valid > 0; len--, rx_valid--)
  313. *buf++ = dw_readl(dev, DW_IC_DATA_CMD);
  314. if (len > 0) {
  315. dev->status |= STATUS_READ_IN_PROGRESS;
  316. dev->rx_buf_len = len;
  317. dev->rx_buf = buf;
  318. return;
  319. } else
  320. dev->status &= ~STATUS_READ_IN_PROGRESS;
  321. }
  322. }
  323. static int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev)
  324. {
  325. unsigned long abort_source = dev->abort_source;
  326. int i;
  327. if (abort_source & DW_IC_TX_ABRT_NOACK) {
  328. for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  329. dev_dbg(dev->dev,
  330. "%s: %s\n", __func__, abort_sources[i]);
  331. return -EREMOTEIO;
  332. }
  333. for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  334. dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]);
  335. if (abort_source & DW_IC_TX_ARB_LOST)
  336. return -EAGAIN;
  337. else if (abort_source & DW_IC_TX_ABRT_GCALL_READ)
  338. return -EINVAL; /* wrong msgs[] data */
  339. else
  340. return -EIO;
  341. }
  342. /*
  343. * Prepare controller for a transaction and call i2c_dw_xfer_msg
  344. */
  345. int
  346. i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  347. {
  348. struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
  349. int ret;
  350. dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
  351. mutex_lock(&dev->lock);
  352. INIT_COMPLETION(dev->cmd_complete);
  353. dev->msgs = msgs;
  354. dev->msgs_num = num;
  355. dev->cmd_err = 0;
  356. dev->msg_write_idx = 0;
  357. dev->msg_read_idx = 0;
  358. dev->msg_err = 0;
  359. dev->status = STATUS_IDLE;
  360. dev->abort_source = 0;
  361. ret = i2c_dw_wait_bus_not_busy(dev);
  362. if (ret < 0)
  363. goto done;
  364. /* start the transfers */
  365. i2c_dw_xfer_init(dev);
  366. /* wait for tx to complete */
  367. ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete, HZ);
  368. if (ret == 0) {
  369. dev_err(dev->dev, "controller timed out\n");
  370. i2c_dw_init(dev);
  371. ret = -ETIMEDOUT;
  372. goto done;
  373. } else if (ret < 0)
  374. goto done;
  375. if (dev->msg_err) {
  376. ret = dev->msg_err;
  377. goto done;
  378. }
  379. /* no error */
  380. if (likely(!dev->cmd_err)) {
  381. /* Disable the adapter */
  382. dw_writel(dev, 0, DW_IC_ENABLE);
  383. ret = num;
  384. goto done;
  385. }
  386. /* We have an error */
  387. if (dev->cmd_err == DW_IC_ERR_TX_ABRT) {
  388. ret = i2c_dw_handle_tx_abort(dev);
  389. goto done;
  390. }
  391. ret = -EIO;
  392. done:
  393. mutex_unlock(&dev->lock);
  394. return ret;
  395. }
  396. u32 i2c_dw_func(struct i2c_adapter *adap)
  397. {
  398. struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
  399. return dev->functionality;
  400. }
  401. static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev)
  402. {
  403. u32 stat;
  404. /*
  405. * The IC_INTR_STAT register just indicates "enabled" interrupts.
  406. * Ths unmasked raw version of interrupt status bits are available
  407. * in the IC_RAW_INTR_STAT register.
  408. *
  409. * That is,
  410. * stat = dw_readl(IC_INTR_STAT);
  411. * equals to,
  412. * stat = dw_readl(IC_RAW_INTR_STAT) & dw_readl(IC_INTR_MASK);
  413. *
  414. * The raw version might be useful for debugging purposes.
  415. */
  416. stat = dw_readl(dev, DW_IC_INTR_STAT);
  417. /*
  418. * Do not use the IC_CLR_INTR register to clear interrupts, or
  419. * you'll miss some interrupts, triggered during the period from
  420. * dw_readl(IC_INTR_STAT) to dw_readl(IC_CLR_INTR).
  421. *
  422. * Instead, use the separately-prepared IC_CLR_* registers.
  423. */
  424. if (stat & DW_IC_INTR_RX_UNDER)
  425. dw_readl(dev, DW_IC_CLR_RX_UNDER);
  426. if (stat & DW_IC_INTR_RX_OVER)
  427. dw_readl(dev, DW_IC_CLR_RX_OVER);
  428. if (stat & DW_IC_INTR_TX_OVER)
  429. dw_readl(dev, DW_IC_CLR_TX_OVER);
  430. if (stat & DW_IC_INTR_RD_REQ)
  431. dw_readl(dev, DW_IC_CLR_RD_REQ);
  432. if (stat & DW_IC_INTR_TX_ABRT) {
  433. /*
  434. * The IC_TX_ABRT_SOURCE register is cleared whenever
  435. * the IC_CLR_TX_ABRT is read. Preserve it beforehand.
  436. */
  437. dev->abort_source = dw_readl(dev, DW_IC_TX_ABRT_SOURCE);
  438. dw_readl(dev, DW_IC_CLR_TX_ABRT);
  439. }
  440. if (stat & DW_IC_INTR_RX_DONE)
  441. dw_readl(dev, DW_IC_CLR_RX_DONE);
  442. if (stat & DW_IC_INTR_ACTIVITY)
  443. dw_readl(dev, DW_IC_CLR_ACTIVITY);
  444. if (stat & DW_IC_INTR_STOP_DET)
  445. dw_readl(dev, DW_IC_CLR_STOP_DET);
  446. if (stat & DW_IC_INTR_START_DET)
  447. dw_readl(dev, DW_IC_CLR_START_DET);
  448. if (stat & DW_IC_INTR_GEN_CALL)
  449. dw_readl(dev, DW_IC_CLR_GEN_CALL);
  450. return stat;
  451. }
  452. /*
  453. * Interrupt service routine. This gets called whenever an I2C interrupt
  454. * occurs.
  455. */
  456. irqreturn_t i2c_dw_isr(int this_irq, void *dev_id)
  457. {
  458. struct dw_i2c_dev *dev = dev_id;
  459. u32 stat;
  460. stat = i2c_dw_read_clear_intrbits(dev);
  461. dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);
  462. if (stat & DW_IC_INTR_TX_ABRT) {
  463. dev->cmd_err |= DW_IC_ERR_TX_ABRT;
  464. dev->status = STATUS_IDLE;
  465. /*
  466. * Anytime TX_ABRT is set, the contents of the tx/rx
  467. * buffers are flushed. Make sure to skip them.
  468. */
  469. dw_writel(dev, 0, DW_IC_INTR_MASK);
  470. goto tx_aborted;
  471. }
  472. if (stat & DW_IC_INTR_RX_FULL)
  473. i2c_dw_read(dev);
  474. if (stat & DW_IC_INTR_TX_EMPTY)
  475. i2c_dw_xfer_msg(dev);
  476. /*
  477. * No need to modify or disable the interrupt mask here.
  478. * i2c_dw_xfer_msg() will take care of it according to
  479. * the current transmit status.
  480. */
  481. tx_aborted:
  482. if ((stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET)) || dev->msg_err)
  483. complete(&dev->cmd_complete);
  484. return IRQ_HANDLED;
  485. }