i2c-designware-core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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 = clk_get_rate(dev->clk) / 1000;
  138. u32 ic_con, hcnt, lcnt;
  139. u32 reg;
  140. /* Configure register endianess access */
  141. reg = dw_readl(dev, DW_IC_COMP_TYPE);
  142. if (reg == ___constant_swab32(DW_IC_COMP_TYPE_VALUE)) {
  143. dev->swab = 1;
  144. reg = DW_IC_COMP_TYPE_VALUE;
  145. }
  146. if (reg != DW_IC_COMP_TYPE_VALUE) {
  147. dev_err(dev->dev, "Unknown Synopsys component type: "
  148. "0x%08x\n", reg);
  149. return -ENODEV;
  150. }
  151. /* Disable the adapter */
  152. dw_writel(dev, 0, DW_IC_ENABLE);
  153. /* set standard and fast speed deviders for high/low periods */
  154. /* Standard-mode */
  155. hcnt = i2c_dw_scl_hcnt(input_clock_khz,
  156. 40, /* tHD;STA = tHIGH = 4.0 us */
  157. 3, /* tf = 0.3 us */
  158. 0, /* 0: DW default, 1: Ideal */
  159. 0); /* No offset */
  160. lcnt = i2c_dw_scl_lcnt(input_clock_khz,
  161. 47, /* tLOW = 4.7 us */
  162. 3, /* tf = 0.3 us */
  163. 0); /* No offset */
  164. dw_writel(dev, hcnt, DW_IC_SS_SCL_HCNT);
  165. dw_writel(dev, lcnt, DW_IC_SS_SCL_LCNT);
  166. dev_dbg(dev->dev, "Standard-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
  167. /* Fast-mode */
  168. hcnt = i2c_dw_scl_hcnt(input_clock_khz,
  169. 6, /* tHD;STA = tHIGH = 0.6 us */
  170. 3, /* tf = 0.3 us */
  171. 0, /* 0: DW default, 1: Ideal */
  172. 0); /* No offset */
  173. lcnt = i2c_dw_scl_lcnt(input_clock_khz,
  174. 13, /* tLOW = 1.3 us */
  175. 3, /* tf = 0.3 us */
  176. 0); /* No offset */
  177. dw_writel(dev, hcnt, DW_IC_FS_SCL_HCNT);
  178. dw_writel(dev, lcnt, DW_IC_FS_SCL_LCNT);
  179. dev_dbg(dev->dev, "Fast-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
  180. /* Configure Tx/Rx FIFO threshold levels */
  181. dw_writel(dev, dev->tx_fifo_depth - 1, DW_IC_TX_TL);
  182. dw_writel(dev, 0, DW_IC_RX_TL);
  183. /* configure the i2c master */
  184. ic_con = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE |
  185. DW_IC_CON_RESTART_EN | DW_IC_CON_SPEED_FAST;
  186. dw_writel(dev, ic_con, DW_IC_CON);
  187. return 0;
  188. }
  189. /*
  190. * Waiting for bus not busy
  191. */
  192. static int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev)
  193. {
  194. int timeout = TIMEOUT;
  195. while (dw_readl(dev, DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) {
  196. if (timeout <= 0) {
  197. dev_warn(dev->dev, "timeout waiting for bus ready\n");
  198. return -ETIMEDOUT;
  199. }
  200. timeout--;
  201. mdelay(1);
  202. }
  203. return 0;
  204. }
  205. static void i2c_dw_xfer_init(struct dw_i2c_dev *dev)
  206. {
  207. struct i2c_msg *msgs = dev->msgs;
  208. u32 ic_con;
  209. /* Disable the adapter */
  210. dw_writel(dev, 0, DW_IC_ENABLE);
  211. /* set the slave (target) address */
  212. dw_writel(dev, msgs[dev->msg_write_idx].addr, DW_IC_TAR);
  213. /* if the slave address is ten bit address, enable 10BITADDR */
  214. ic_con = dw_readl(dev, DW_IC_CON);
  215. if (msgs[dev->msg_write_idx].flags & I2C_M_TEN)
  216. ic_con |= DW_IC_CON_10BITADDR_MASTER;
  217. else
  218. ic_con &= ~DW_IC_CON_10BITADDR_MASTER;
  219. dw_writel(dev, ic_con, DW_IC_CON);
  220. /* Enable the adapter */
  221. dw_writel(dev, 1, DW_IC_ENABLE);
  222. /* Enable interrupts */
  223. dw_writel(dev, DW_IC_INTR_DEFAULT_MASK, DW_IC_INTR_MASK);
  224. }
  225. /*
  226. * Initiate (and continue) low level master read/write transaction.
  227. * This function is only called from i2c_dw_isr, and pumping i2c_msg
  228. * messages into the tx buffer. Even if the size of i2c_msg data is
  229. * longer than the size of the tx buffer, it handles everything.
  230. */
  231. void
  232. i2c_dw_xfer_msg(struct dw_i2c_dev *dev)
  233. {
  234. struct i2c_msg *msgs = dev->msgs;
  235. u32 intr_mask;
  236. int tx_limit, rx_limit;
  237. u32 addr = msgs[dev->msg_write_idx].addr;
  238. u32 buf_len = dev->tx_buf_len;
  239. u8 *buf = dev->tx_buf;
  240. intr_mask = DW_IC_INTR_DEFAULT_MASK;
  241. for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) {
  242. /*
  243. * if target address has changed, we need to
  244. * reprogram the target address in the i2c
  245. * adapter when we are done with this transfer
  246. */
  247. if (msgs[dev->msg_write_idx].addr != addr) {
  248. dev_err(dev->dev,
  249. "%s: invalid target address\n", __func__);
  250. dev->msg_err = -EINVAL;
  251. break;
  252. }
  253. if (msgs[dev->msg_write_idx].len == 0) {
  254. dev_err(dev->dev,
  255. "%s: invalid message length\n", __func__);
  256. dev->msg_err = -EINVAL;
  257. break;
  258. }
  259. if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {
  260. /* new i2c_msg */
  261. buf = msgs[dev->msg_write_idx].buf;
  262. buf_len = msgs[dev->msg_write_idx].len;
  263. }
  264. tx_limit = dev->tx_fifo_depth - dw_readl(dev, DW_IC_TXFLR);
  265. rx_limit = dev->rx_fifo_depth - dw_readl(dev, DW_IC_RXFLR);
  266. while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) {
  267. if (msgs[dev->msg_write_idx].flags & I2C_M_RD) {
  268. dw_writel(dev, 0x100, DW_IC_DATA_CMD);
  269. rx_limit--;
  270. } else
  271. dw_writel(dev, *buf++, DW_IC_DATA_CMD);
  272. tx_limit--; buf_len--;
  273. }
  274. dev->tx_buf = buf;
  275. dev->tx_buf_len = buf_len;
  276. if (buf_len > 0) {
  277. /* more bytes to be written */
  278. dev->status |= STATUS_WRITE_IN_PROGRESS;
  279. break;
  280. } else
  281. dev->status &= ~STATUS_WRITE_IN_PROGRESS;
  282. }
  283. /*
  284. * If i2c_msg index search is completed, we don't need TX_EMPTY
  285. * interrupt any more.
  286. */
  287. if (dev->msg_write_idx == dev->msgs_num)
  288. intr_mask &= ~DW_IC_INTR_TX_EMPTY;
  289. if (dev->msg_err)
  290. intr_mask = 0;
  291. dw_writel(dev, intr_mask, DW_IC_INTR_MASK);
  292. }
  293. static void
  294. i2c_dw_read(struct dw_i2c_dev *dev)
  295. {
  296. struct i2c_msg *msgs = dev->msgs;
  297. int rx_valid;
  298. for (; dev->msg_read_idx < dev->msgs_num; dev->msg_read_idx++) {
  299. u32 len;
  300. u8 *buf;
  301. if (!(msgs[dev->msg_read_idx].flags & I2C_M_RD))
  302. continue;
  303. if (!(dev->status & STATUS_READ_IN_PROGRESS)) {
  304. len = msgs[dev->msg_read_idx].len;
  305. buf = msgs[dev->msg_read_idx].buf;
  306. } else {
  307. len = dev->rx_buf_len;
  308. buf = dev->rx_buf;
  309. }
  310. rx_valid = dw_readl(dev, DW_IC_RXFLR);
  311. for (; len > 0 && rx_valid > 0; len--, rx_valid--)
  312. *buf++ = dw_readl(dev, DW_IC_DATA_CMD);
  313. if (len > 0) {
  314. dev->status |= STATUS_READ_IN_PROGRESS;
  315. dev->rx_buf_len = len;
  316. dev->rx_buf = buf;
  317. return;
  318. } else
  319. dev->status &= ~STATUS_READ_IN_PROGRESS;
  320. }
  321. }
  322. static int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev)
  323. {
  324. unsigned long abort_source = dev->abort_source;
  325. int i;
  326. if (abort_source & DW_IC_TX_ABRT_NOACK) {
  327. for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  328. dev_dbg(dev->dev,
  329. "%s: %s\n", __func__, abort_sources[i]);
  330. return -EREMOTEIO;
  331. }
  332. for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  333. dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]);
  334. if (abort_source & DW_IC_TX_ARB_LOST)
  335. return -EAGAIN;
  336. else if (abort_source & DW_IC_TX_ABRT_GCALL_READ)
  337. return -EINVAL; /* wrong msgs[] data */
  338. else
  339. return -EIO;
  340. }
  341. /*
  342. * Prepare controller for a transaction and call i2c_dw_xfer_msg
  343. */
  344. int
  345. i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  346. {
  347. struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
  348. int ret;
  349. dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
  350. mutex_lock(&dev->lock);
  351. INIT_COMPLETION(dev->cmd_complete);
  352. dev->msgs = msgs;
  353. dev->msgs_num = num;
  354. dev->cmd_err = 0;
  355. dev->msg_write_idx = 0;
  356. dev->msg_read_idx = 0;
  357. dev->msg_err = 0;
  358. dev->status = STATUS_IDLE;
  359. dev->abort_source = 0;
  360. ret = i2c_dw_wait_bus_not_busy(dev);
  361. if (ret < 0)
  362. goto done;
  363. /* start the transfers */
  364. i2c_dw_xfer_init(dev);
  365. /* wait for tx to complete */
  366. ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete, HZ);
  367. if (ret == 0) {
  368. dev_err(dev->dev, "controller timed out\n");
  369. i2c_dw_init(dev);
  370. ret = -ETIMEDOUT;
  371. goto done;
  372. } else if (ret < 0)
  373. goto done;
  374. if (dev->msg_err) {
  375. ret = dev->msg_err;
  376. goto done;
  377. }
  378. /* no error */
  379. if (likely(!dev->cmd_err)) {
  380. /* Disable the adapter */
  381. dw_writel(dev, 0, DW_IC_ENABLE);
  382. ret = num;
  383. goto done;
  384. }
  385. /* We have an error */
  386. if (dev->cmd_err == DW_IC_ERR_TX_ABRT) {
  387. ret = i2c_dw_handle_tx_abort(dev);
  388. goto done;
  389. }
  390. ret = -EIO;
  391. done:
  392. mutex_unlock(&dev->lock);
  393. return ret;
  394. }
  395. u32 i2c_dw_func(struct i2c_adapter *adap)
  396. {
  397. return I2C_FUNC_I2C |
  398. I2C_FUNC_10BIT_ADDR |
  399. I2C_FUNC_SMBUS_BYTE |
  400. I2C_FUNC_SMBUS_BYTE_DATA |
  401. I2C_FUNC_SMBUS_WORD_DATA |
  402. I2C_FUNC_SMBUS_I2C_BLOCK;
  403. }
  404. static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev)
  405. {
  406. u32 stat;
  407. /*
  408. * The IC_INTR_STAT register just indicates "enabled" interrupts.
  409. * Ths unmasked raw version of interrupt status bits are available
  410. * in the IC_RAW_INTR_STAT register.
  411. *
  412. * That is,
  413. * stat = dw_readl(IC_INTR_STAT);
  414. * equals to,
  415. * stat = dw_readl(IC_RAW_INTR_STAT) & dw_readl(IC_INTR_MASK);
  416. *
  417. * The raw version might be useful for debugging purposes.
  418. */
  419. stat = dw_readl(dev, DW_IC_INTR_STAT);
  420. /*
  421. * Do not use the IC_CLR_INTR register to clear interrupts, or
  422. * you'll miss some interrupts, triggered during the period from
  423. * dw_readl(IC_INTR_STAT) to dw_readl(IC_CLR_INTR).
  424. *
  425. * Instead, use the separately-prepared IC_CLR_* registers.
  426. */
  427. if (stat & DW_IC_INTR_RX_UNDER)
  428. dw_readl(dev, DW_IC_CLR_RX_UNDER);
  429. if (stat & DW_IC_INTR_RX_OVER)
  430. dw_readl(dev, DW_IC_CLR_RX_OVER);
  431. if (stat & DW_IC_INTR_TX_OVER)
  432. dw_readl(dev, DW_IC_CLR_TX_OVER);
  433. if (stat & DW_IC_INTR_RD_REQ)
  434. dw_readl(dev, DW_IC_CLR_RD_REQ);
  435. if (stat & DW_IC_INTR_TX_ABRT) {
  436. /*
  437. * The IC_TX_ABRT_SOURCE register is cleared whenever
  438. * the IC_CLR_TX_ABRT is read. Preserve it beforehand.
  439. */
  440. dev->abort_source = dw_readl(dev, DW_IC_TX_ABRT_SOURCE);
  441. dw_readl(dev, DW_IC_CLR_TX_ABRT);
  442. }
  443. if (stat & DW_IC_INTR_RX_DONE)
  444. dw_readl(dev, DW_IC_CLR_RX_DONE);
  445. if (stat & DW_IC_INTR_ACTIVITY)
  446. dw_readl(dev, DW_IC_CLR_ACTIVITY);
  447. if (stat & DW_IC_INTR_STOP_DET)
  448. dw_readl(dev, DW_IC_CLR_STOP_DET);
  449. if (stat & DW_IC_INTR_START_DET)
  450. dw_readl(dev, DW_IC_CLR_START_DET);
  451. if (stat & DW_IC_INTR_GEN_CALL)
  452. dw_readl(dev, DW_IC_CLR_GEN_CALL);
  453. return stat;
  454. }
  455. /*
  456. * Interrupt service routine. This gets called whenever an I2C interrupt
  457. * occurs.
  458. */
  459. irqreturn_t i2c_dw_isr(int this_irq, void *dev_id)
  460. {
  461. struct dw_i2c_dev *dev = dev_id;
  462. u32 stat;
  463. stat = i2c_dw_read_clear_intrbits(dev);
  464. dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);
  465. if (stat & DW_IC_INTR_TX_ABRT) {
  466. dev->cmd_err |= DW_IC_ERR_TX_ABRT;
  467. dev->status = STATUS_IDLE;
  468. /*
  469. * Anytime TX_ABRT is set, the contents of the tx/rx
  470. * buffers are flushed. Make sure to skip them.
  471. */
  472. dw_writel(dev, 0, DW_IC_INTR_MASK);
  473. goto tx_aborted;
  474. }
  475. if (stat & DW_IC_INTR_RX_FULL)
  476. i2c_dw_read(dev);
  477. if (stat & DW_IC_INTR_TX_EMPTY)
  478. i2c_dw_xfer_msg(dev);
  479. /*
  480. * No need to modify or disable the interrupt mask here.
  481. * i2c_dw_xfer_msg() will take care of it according to
  482. * the current transmit status.
  483. */
  484. tx_aborted:
  485. if ((stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET)) || dev->msg_err)
  486. complete(&dev->cmd_complete);
  487. return IRQ_HANDLED;
  488. }