i2c-designware-core.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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/export.h>
  29. #include <linux/clk.h>
  30. #include <linux/errno.h>
  31. #include <linux/err.h>
  32. #include <linux/i2c.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/io.h>
  35. #include <linux/pm_runtime.h>
  36. #include <linux/delay.h>
  37. #include "i2c-designware-core.h"
  38. /*
  39. * Registers offset
  40. */
  41. #define DW_IC_CON 0x0
  42. #define DW_IC_TAR 0x4
  43. #define DW_IC_DATA_CMD 0x10
  44. #define DW_IC_SS_SCL_HCNT 0x14
  45. #define DW_IC_SS_SCL_LCNT 0x18
  46. #define DW_IC_FS_SCL_HCNT 0x1c
  47. #define DW_IC_FS_SCL_LCNT 0x20
  48. #define DW_IC_INTR_STAT 0x2c
  49. #define DW_IC_INTR_MASK 0x30
  50. #define DW_IC_RAW_INTR_STAT 0x34
  51. #define DW_IC_RX_TL 0x38
  52. #define DW_IC_TX_TL 0x3c
  53. #define DW_IC_CLR_INTR 0x40
  54. #define DW_IC_CLR_RX_UNDER 0x44
  55. #define DW_IC_CLR_RX_OVER 0x48
  56. #define DW_IC_CLR_TX_OVER 0x4c
  57. #define DW_IC_CLR_RD_REQ 0x50
  58. #define DW_IC_CLR_TX_ABRT 0x54
  59. #define DW_IC_CLR_RX_DONE 0x58
  60. #define DW_IC_CLR_ACTIVITY 0x5c
  61. #define DW_IC_CLR_STOP_DET 0x60
  62. #define DW_IC_CLR_START_DET 0x64
  63. #define DW_IC_CLR_GEN_CALL 0x68
  64. #define DW_IC_ENABLE 0x6c
  65. #define DW_IC_STATUS 0x70
  66. #define DW_IC_TXFLR 0x74
  67. #define DW_IC_RXFLR 0x78
  68. #define DW_IC_TX_ABRT_SOURCE 0x80
  69. #define DW_IC_COMP_PARAM_1 0xf4
  70. #define DW_IC_COMP_TYPE 0xfc
  71. #define DW_IC_COMP_TYPE_VALUE 0x44570140
  72. #define DW_IC_INTR_RX_UNDER 0x001
  73. #define DW_IC_INTR_RX_OVER 0x002
  74. #define DW_IC_INTR_RX_FULL 0x004
  75. #define DW_IC_INTR_TX_OVER 0x008
  76. #define DW_IC_INTR_TX_EMPTY 0x010
  77. #define DW_IC_INTR_RD_REQ 0x020
  78. #define DW_IC_INTR_TX_ABRT 0x040
  79. #define DW_IC_INTR_RX_DONE 0x080
  80. #define DW_IC_INTR_ACTIVITY 0x100
  81. #define DW_IC_INTR_STOP_DET 0x200
  82. #define DW_IC_INTR_START_DET 0x400
  83. #define DW_IC_INTR_GEN_CALL 0x800
  84. #define DW_IC_INTR_DEFAULT_MASK (DW_IC_INTR_RX_FULL | \
  85. DW_IC_INTR_TX_EMPTY | \
  86. DW_IC_INTR_TX_ABRT | \
  87. DW_IC_INTR_STOP_DET)
  88. #define DW_IC_STATUS_ACTIVITY 0x1
  89. #define DW_IC_ERR_TX_ABRT 0x1
  90. /*
  91. * status codes
  92. */
  93. #define STATUS_IDLE 0x0
  94. #define STATUS_WRITE_IN_PROGRESS 0x1
  95. #define STATUS_READ_IN_PROGRESS 0x2
  96. #define TIMEOUT 20 /* ms */
  97. /*
  98. * hardware abort codes from the DW_IC_TX_ABRT_SOURCE register
  99. *
  100. * only expected abort codes are listed here
  101. * refer to the datasheet for the full list
  102. */
  103. #define ABRT_7B_ADDR_NOACK 0
  104. #define ABRT_10ADDR1_NOACK 1
  105. #define ABRT_10ADDR2_NOACK 2
  106. #define ABRT_TXDATA_NOACK 3
  107. #define ABRT_GCALL_NOACK 4
  108. #define ABRT_GCALL_READ 5
  109. #define ABRT_SBYTE_ACKDET 7
  110. #define ABRT_SBYTE_NORSTRT 9
  111. #define ABRT_10B_RD_NORSTRT 10
  112. #define ABRT_MASTER_DIS 11
  113. #define ARB_LOST 12
  114. #define DW_IC_TX_ABRT_7B_ADDR_NOACK (1UL << ABRT_7B_ADDR_NOACK)
  115. #define DW_IC_TX_ABRT_10ADDR1_NOACK (1UL << ABRT_10ADDR1_NOACK)
  116. #define DW_IC_TX_ABRT_10ADDR2_NOACK (1UL << ABRT_10ADDR2_NOACK)
  117. #define DW_IC_TX_ABRT_TXDATA_NOACK (1UL << ABRT_TXDATA_NOACK)
  118. #define DW_IC_TX_ABRT_GCALL_NOACK (1UL << ABRT_GCALL_NOACK)
  119. #define DW_IC_TX_ABRT_GCALL_READ (1UL << ABRT_GCALL_READ)
  120. #define DW_IC_TX_ABRT_SBYTE_ACKDET (1UL << ABRT_SBYTE_ACKDET)
  121. #define DW_IC_TX_ABRT_SBYTE_NORSTRT (1UL << ABRT_SBYTE_NORSTRT)
  122. #define DW_IC_TX_ABRT_10B_RD_NORSTRT (1UL << ABRT_10B_RD_NORSTRT)
  123. #define DW_IC_TX_ABRT_MASTER_DIS (1UL << ABRT_MASTER_DIS)
  124. #define DW_IC_TX_ARB_LOST (1UL << ARB_LOST)
  125. #define DW_IC_TX_ABRT_NOACK (DW_IC_TX_ABRT_7B_ADDR_NOACK | \
  126. DW_IC_TX_ABRT_10ADDR1_NOACK | \
  127. DW_IC_TX_ABRT_10ADDR2_NOACK | \
  128. DW_IC_TX_ABRT_TXDATA_NOACK | \
  129. DW_IC_TX_ABRT_GCALL_NOACK)
  130. static char *abort_sources[] = {
  131. [ABRT_7B_ADDR_NOACK] =
  132. "slave address not acknowledged (7bit mode)",
  133. [ABRT_10ADDR1_NOACK] =
  134. "first address byte not acknowledged (10bit mode)",
  135. [ABRT_10ADDR2_NOACK] =
  136. "second address byte not acknowledged (10bit mode)",
  137. [ABRT_TXDATA_NOACK] =
  138. "data not acknowledged",
  139. [ABRT_GCALL_NOACK] =
  140. "no acknowledgement for a general call",
  141. [ABRT_GCALL_READ] =
  142. "read after general call",
  143. [ABRT_SBYTE_ACKDET] =
  144. "start byte acknowledged",
  145. [ABRT_SBYTE_NORSTRT] =
  146. "trying to send start byte when restart is disabled",
  147. [ABRT_10B_RD_NORSTRT] =
  148. "trying to read when restart is disabled (10bit mode)",
  149. [ABRT_MASTER_DIS] =
  150. "trying to use disabled adapter",
  151. [ARB_LOST] =
  152. "lost arbitration",
  153. };
  154. u32 dw_readl(struct dw_i2c_dev *dev, int offset)
  155. {
  156. u32 value = readl(dev->base + offset);
  157. if (dev->swab)
  158. return swab32(value);
  159. else
  160. return value;
  161. }
  162. void dw_writel(struct dw_i2c_dev *dev, u32 b, int offset)
  163. {
  164. if (dev->swab)
  165. b = swab32(b);
  166. writel(b, dev->base + offset);
  167. }
  168. static u32
  169. i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset)
  170. {
  171. /*
  172. * DesignWare I2C core doesn't seem to have solid strategy to meet
  173. * the tHD;STA timing spec. Configuring _HCNT based on tHIGH spec
  174. * will result in violation of the tHD;STA spec.
  175. */
  176. if (cond)
  177. /*
  178. * Conditional expression:
  179. *
  180. * IC_[FS]S_SCL_HCNT + (1+4+3) >= IC_CLK * tHIGH
  181. *
  182. * This is based on the DW manuals, and represents an ideal
  183. * configuration. The resulting I2C bus speed will be
  184. * faster than any of the others.
  185. *
  186. * If your hardware is free from tHD;STA issue, try this one.
  187. */
  188. return (ic_clk * tSYMBOL + 5000) / 10000 - 8 + offset;
  189. else
  190. /*
  191. * Conditional expression:
  192. *
  193. * IC_[FS]S_SCL_HCNT + 3 >= IC_CLK * (tHD;STA + tf)
  194. *
  195. * This is just experimental rule; the tHD;STA period turned
  196. * out to be proportinal to (_HCNT + 3). With this setting,
  197. * we could meet both tHIGH and tHD;STA timing specs.
  198. *
  199. * If unsure, you'd better to take this alternative.
  200. *
  201. * The reason why we need to take into account "tf" here,
  202. * is the same as described in i2c_dw_scl_lcnt().
  203. */
  204. return (ic_clk * (tSYMBOL + tf) + 5000) / 10000 - 3 + offset;
  205. }
  206. static u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset)
  207. {
  208. /*
  209. * Conditional expression:
  210. *
  211. * IC_[FS]S_SCL_LCNT + 1 >= IC_CLK * (tLOW + tf)
  212. *
  213. * DW I2C core starts counting the SCL CNTs for the LOW period
  214. * of the SCL clock (tLOW) as soon as it pulls the SCL line.
  215. * In order to meet the tLOW timing spec, we need to take into
  216. * account the fall time of SCL signal (tf). Default tf value
  217. * should be 0.3 us, for safety.
  218. */
  219. return ((ic_clk * (tLOW + tf) + 5000) / 10000) - 1 + offset;
  220. }
  221. /**
  222. * i2c_dw_init() - initialize the designware i2c master hardware
  223. * @dev: device private data
  224. *
  225. * This functions configures and enables the I2C master.
  226. * This function is called during I2C init function, and in case of timeout at
  227. * run time.
  228. */
  229. int i2c_dw_init(struct dw_i2c_dev *dev)
  230. {
  231. u32 input_clock_khz;
  232. u32 hcnt, lcnt;
  233. u32 reg;
  234. input_clock_khz = dev->get_clk_rate_khz(dev);
  235. /* Configure register endianess access */
  236. reg = dw_readl(dev, DW_IC_COMP_TYPE);
  237. if (reg == ___constant_swab32(DW_IC_COMP_TYPE_VALUE)) {
  238. dev->swab = 1;
  239. reg = DW_IC_COMP_TYPE_VALUE;
  240. }
  241. if (reg != DW_IC_COMP_TYPE_VALUE) {
  242. dev_err(dev->dev, "Unknown Synopsys component type: "
  243. "0x%08x\n", reg);
  244. return -ENODEV;
  245. }
  246. /* Disable the adapter */
  247. dw_writel(dev, 0, DW_IC_ENABLE);
  248. /* set standard and fast speed deviders for high/low periods */
  249. /* Standard-mode */
  250. hcnt = i2c_dw_scl_hcnt(input_clock_khz,
  251. 40, /* tHD;STA = tHIGH = 4.0 us */
  252. 3, /* tf = 0.3 us */
  253. 0, /* 0: DW default, 1: Ideal */
  254. 0); /* No offset */
  255. lcnt = i2c_dw_scl_lcnt(input_clock_khz,
  256. 47, /* tLOW = 4.7 us */
  257. 3, /* tf = 0.3 us */
  258. 0); /* No offset */
  259. dw_writel(dev, hcnt, DW_IC_SS_SCL_HCNT);
  260. dw_writel(dev, lcnt, DW_IC_SS_SCL_LCNT);
  261. dev_dbg(dev->dev, "Standard-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
  262. /* Fast-mode */
  263. hcnt = i2c_dw_scl_hcnt(input_clock_khz,
  264. 6, /* tHD;STA = tHIGH = 0.6 us */
  265. 3, /* tf = 0.3 us */
  266. 0, /* 0: DW default, 1: Ideal */
  267. 0); /* No offset */
  268. lcnt = i2c_dw_scl_lcnt(input_clock_khz,
  269. 13, /* tLOW = 1.3 us */
  270. 3, /* tf = 0.3 us */
  271. 0); /* No offset */
  272. dw_writel(dev, hcnt, DW_IC_FS_SCL_HCNT);
  273. dw_writel(dev, lcnt, DW_IC_FS_SCL_LCNT);
  274. dev_dbg(dev->dev, "Fast-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
  275. /* Configure Tx/Rx FIFO threshold levels */
  276. dw_writel(dev, dev->tx_fifo_depth - 1, DW_IC_TX_TL);
  277. dw_writel(dev, 0, DW_IC_RX_TL);
  278. /* configure the i2c master */
  279. dw_writel(dev, dev->master_cfg , DW_IC_CON);
  280. return 0;
  281. }
  282. EXPORT_SYMBOL_GPL(i2c_dw_init);
  283. /*
  284. * Waiting for bus not busy
  285. */
  286. static int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev)
  287. {
  288. int timeout = TIMEOUT;
  289. while (dw_readl(dev, DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) {
  290. if (timeout <= 0) {
  291. dev_warn(dev->dev, "timeout waiting for bus ready\n");
  292. return -ETIMEDOUT;
  293. }
  294. timeout--;
  295. mdelay(1);
  296. }
  297. return 0;
  298. }
  299. static void i2c_dw_xfer_init(struct dw_i2c_dev *dev)
  300. {
  301. struct i2c_msg *msgs = dev->msgs;
  302. u32 ic_con;
  303. /* Disable the adapter */
  304. dw_writel(dev, 0, DW_IC_ENABLE);
  305. /* set the slave (target) address */
  306. dw_writel(dev, msgs[dev->msg_write_idx].addr, DW_IC_TAR);
  307. /* if the slave address is ten bit address, enable 10BITADDR */
  308. ic_con = dw_readl(dev, DW_IC_CON);
  309. if (msgs[dev->msg_write_idx].flags & I2C_M_TEN)
  310. ic_con |= DW_IC_CON_10BITADDR_MASTER;
  311. else
  312. ic_con &= ~DW_IC_CON_10BITADDR_MASTER;
  313. dw_writel(dev, ic_con, DW_IC_CON);
  314. /* enforce disabled interrupts (due to HW issues) */
  315. i2c_dw_disable_int(dev);
  316. /* Enable the adapter */
  317. dw_writel(dev, 1, DW_IC_ENABLE);
  318. /* Clear and enable interrupts */
  319. i2c_dw_clear_int(dev);
  320. dw_writel(dev, DW_IC_INTR_DEFAULT_MASK, DW_IC_INTR_MASK);
  321. }
  322. /*
  323. * Initiate (and continue) low level master read/write transaction.
  324. * This function is only called from i2c_dw_isr, and pumping i2c_msg
  325. * messages into the tx buffer. Even if the size of i2c_msg data is
  326. * longer than the size of the tx buffer, it handles everything.
  327. */
  328. void
  329. i2c_dw_xfer_msg(struct dw_i2c_dev *dev)
  330. {
  331. struct i2c_msg *msgs = dev->msgs;
  332. u32 intr_mask;
  333. int tx_limit, rx_limit;
  334. u32 addr = msgs[dev->msg_write_idx].addr;
  335. u32 buf_len = dev->tx_buf_len;
  336. u8 *buf = dev->tx_buf;
  337. intr_mask = DW_IC_INTR_DEFAULT_MASK;
  338. for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) {
  339. /*
  340. * if target address has changed, we need to
  341. * reprogram the target address in the i2c
  342. * adapter when we are done with this transfer
  343. */
  344. if (msgs[dev->msg_write_idx].addr != addr) {
  345. dev_err(dev->dev,
  346. "%s: invalid target address\n", __func__);
  347. dev->msg_err = -EINVAL;
  348. break;
  349. }
  350. if (msgs[dev->msg_write_idx].len == 0) {
  351. dev_err(dev->dev,
  352. "%s: invalid message length\n", __func__);
  353. dev->msg_err = -EINVAL;
  354. break;
  355. }
  356. if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {
  357. /* new i2c_msg */
  358. buf = msgs[dev->msg_write_idx].buf;
  359. buf_len = msgs[dev->msg_write_idx].len;
  360. }
  361. tx_limit = dev->tx_fifo_depth - dw_readl(dev, DW_IC_TXFLR);
  362. rx_limit = dev->rx_fifo_depth - dw_readl(dev, DW_IC_RXFLR);
  363. while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) {
  364. if (msgs[dev->msg_write_idx].flags & I2C_M_RD) {
  365. dw_writel(dev, 0x100, DW_IC_DATA_CMD);
  366. rx_limit--;
  367. } else
  368. dw_writel(dev, *buf++, DW_IC_DATA_CMD);
  369. tx_limit--; buf_len--;
  370. }
  371. dev->tx_buf = buf;
  372. dev->tx_buf_len = buf_len;
  373. if (buf_len > 0) {
  374. /* more bytes to be written */
  375. dev->status |= STATUS_WRITE_IN_PROGRESS;
  376. break;
  377. } else
  378. dev->status &= ~STATUS_WRITE_IN_PROGRESS;
  379. }
  380. /*
  381. * If i2c_msg index search is completed, we don't need TX_EMPTY
  382. * interrupt any more.
  383. */
  384. if (dev->msg_write_idx == dev->msgs_num)
  385. intr_mask &= ~DW_IC_INTR_TX_EMPTY;
  386. if (dev->msg_err)
  387. intr_mask = 0;
  388. dw_writel(dev, intr_mask, DW_IC_INTR_MASK);
  389. }
  390. static void
  391. i2c_dw_read(struct dw_i2c_dev *dev)
  392. {
  393. struct i2c_msg *msgs = dev->msgs;
  394. int rx_valid;
  395. for (; dev->msg_read_idx < dev->msgs_num; dev->msg_read_idx++) {
  396. u32 len;
  397. u8 *buf;
  398. if (!(msgs[dev->msg_read_idx].flags & I2C_M_RD))
  399. continue;
  400. if (!(dev->status & STATUS_READ_IN_PROGRESS)) {
  401. len = msgs[dev->msg_read_idx].len;
  402. buf = msgs[dev->msg_read_idx].buf;
  403. } else {
  404. len = dev->rx_buf_len;
  405. buf = dev->rx_buf;
  406. }
  407. rx_valid = dw_readl(dev, DW_IC_RXFLR);
  408. for (; len > 0 && rx_valid > 0; len--, rx_valid--)
  409. *buf++ = dw_readl(dev, DW_IC_DATA_CMD);
  410. if (len > 0) {
  411. dev->status |= STATUS_READ_IN_PROGRESS;
  412. dev->rx_buf_len = len;
  413. dev->rx_buf = buf;
  414. return;
  415. } else
  416. dev->status &= ~STATUS_READ_IN_PROGRESS;
  417. }
  418. }
  419. static int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev)
  420. {
  421. unsigned long abort_source = dev->abort_source;
  422. int i;
  423. if (abort_source & DW_IC_TX_ABRT_NOACK) {
  424. for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  425. dev_dbg(dev->dev,
  426. "%s: %s\n", __func__, abort_sources[i]);
  427. return -EREMOTEIO;
  428. }
  429. for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  430. dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]);
  431. if (abort_source & DW_IC_TX_ARB_LOST)
  432. return -EAGAIN;
  433. else if (abort_source & DW_IC_TX_ABRT_GCALL_READ)
  434. return -EINVAL; /* wrong msgs[] data */
  435. else
  436. return -EIO;
  437. }
  438. /*
  439. * Prepare controller for a transaction and call i2c_dw_xfer_msg
  440. */
  441. int
  442. i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  443. {
  444. struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
  445. int ret;
  446. dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
  447. mutex_lock(&dev->lock);
  448. pm_runtime_get_sync(dev->dev);
  449. INIT_COMPLETION(dev->cmd_complete);
  450. dev->msgs = msgs;
  451. dev->msgs_num = num;
  452. dev->cmd_err = 0;
  453. dev->msg_write_idx = 0;
  454. dev->msg_read_idx = 0;
  455. dev->msg_err = 0;
  456. dev->status = STATUS_IDLE;
  457. dev->abort_source = 0;
  458. ret = i2c_dw_wait_bus_not_busy(dev);
  459. if (ret < 0)
  460. goto done;
  461. /* start the transfers */
  462. i2c_dw_xfer_init(dev);
  463. /* wait for tx to complete */
  464. ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete, HZ);
  465. if (ret == 0) {
  466. dev_err(dev->dev, "controller timed out\n");
  467. i2c_dw_init(dev);
  468. ret = -ETIMEDOUT;
  469. goto done;
  470. } else if (ret < 0)
  471. goto done;
  472. if (dev->msg_err) {
  473. ret = dev->msg_err;
  474. goto done;
  475. }
  476. /* no error */
  477. if (likely(!dev->cmd_err)) {
  478. /* Disable the adapter */
  479. dw_writel(dev, 0, DW_IC_ENABLE);
  480. ret = num;
  481. goto done;
  482. }
  483. /* We have an error */
  484. if (dev->cmd_err == DW_IC_ERR_TX_ABRT) {
  485. ret = i2c_dw_handle_tx_abort(dev);
  486. goto done;
  487. }
  488. ret = -EIO;
  489. done:
  490. pm_runtime_put(dev->dev);
  491. mutex_unlock(&dev->lock);
  492. return ret;
  493. }
  494. EXPORT_SYMBOL_GPL(i2c_dw_xfer);
  495. u32 i2c_dw_func(struct i2c_adapter *adap)
  496. {
  497. struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
  498. return dev->functionality;
  499. }
  500. EXPORT_SYMBOL_GPL(i2c_dw_func);
  501. static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev)
  502. {
  503. u32 stat;
  504. /*
  505. * The IC_INTR_STAT register just indicates "enabled" interrupts.
  506. * Ths unmasked raw version of interrupt status bits are available
  507. * in the IC_RAW_INTR_STAT register.
  508. *
  509. * That is,
  510. * stat = dw_readl(IC_INTR_STAT);
  511. * equals to,
  512. * stat = dw_readl(IC_RAW_INTR_STAT) & dw_readl(IC_INTR_MASK);
  513. *
  514. * The raw version might be useful for debugging purposes.
  515. */
  516. stat = dw_readl(dev, DW_IC_INTR_STAT);
  517. /*
  518. * Do not use the IC_CLR_INTR register to clear interrupts, or
  519. * you'll miss some interrupts, triggered during the period from
  520. * dw_readl(IC_INTR_STAT) to dw_readl(IC_CLR_INTR).
  521. *
  522. * Instead, use the separately-prepared IC_CLR_* registers.
  523. */
  524. if (stat & DW_IC_INTR_RX_UNDER)
  525. dw_readl(dev, DW_IC_CLR_RX_UNDER);
  526. if (stat & DW_IC_INTR_RX_OVER)
  527. dw_readl(dev, DW_IC_CLR_RX_OVER);
  528. if (stat & DW_IC_INTR_TX_OVER)
  529. dw_readl(dev, DW_IC_CLR_TX_OVER);
  530. if (stat & DW_IC_INTR_RD_REQ)
  531. dw_readl(dev, DW_IC_CLR_RD_REQ);
  532. if (stat & DW_IC_INTR_TX_ABRT) {
  533. /*
  534. * The IC_TX_ABRT_SOURCE register is cleared whenever
  535. * the IC_CLR_TX_ABRT is read. Preserve it beforehand.
  536. */
  537. dev->abort_source = dw_readl(dev, DW_IC_TX_ABRT_SOURCE);
  538. dw_readl(dev, DW_IC_CLR_TX_ABRT);
  539. }
  540. if (stat & DW_IC_INTR_RX_DONE)
  541. dw_readl(dev, DW_IC_CLR_RX_DONE);
  542. if (stat & DW_IC_INTR_ACTIVITY)
  543. dw_readl(dev, DW_IC_CLR_ACTIVITY);
  544. if (stat & DW_IC_INTR_STOP_DET)
  545. dw_readl(dev, DW_IC_CLR_STOP_DET);
  546. if (stat & DW_IC_INTR_START_DET)
  547. dw_readl(dev, DW_IC_CLR_START_DET);
  548. if (stat & DW_IC_INTR_GEN_CALL)
  549. dw_readl(dev, DW_IC_CLR_GEN_CALL);
  550. return stat;
  551. }
  552. /*
  553. * Interrupt service routine. This gets called whenever an I2C interrupt
  554. * occurs.
  555. */
  556. irqreturn_t i2c_dw_isr(int this_irq, void *dev_id)
  557. {
  558. struct dw_i2c_dev *dev = dev_id;
  559. u32 stat, enabled;
  560. enabled = dw_readl(dev, DW_IC_ENABLE);
  561. stat = dw_readl(dev, DW_IC_RAW_INTR_STAT);
  562. dev_dbg(dev->dev, "%s: %s enabled= 0x%x stat=0x%x\n", __func__,
  563. dev->adapter.name, enabled, stat);
  564. if (!enabled || !(stat & ~DW_IC_INTR_ACTIVITY))
  565. return IRQ_NONE;
  566. stat = i2c_dw_read_clear_intrbits(dev);
  567. if (stat & DW_IC_INTR_TX_ABRT) {
  568. dev->cmd_err |= DW_IC_ERR_TX_ABRT;
  569. dev->status = STATUS_IDLE;
  570. /*
  571. * Anytime TX_ABRT is set, the contents of the tx/rx
  572. * buffers are flushed. Make sure to skip them.
  573. */
  574. dw_writel(dev, 0, DW_IC_INTR_MASK);
  575. goto tx_aborted;
  576. }
  577. if (stat & DW_IC_INTR_RX_FULL)
  578. i2c_dw_read(dev);
  579. if (stat & DW_IC_INTR_TX_EMPTY)
  580. i2c_dw_xfer_msg(dev);
  581. /*
  582. * No need to modify or disable the interrupt mask here.
  583. * i2c_dw_xfer_msg() will take care of it according to
  584. * the current transmit status.
  585. */
  586. tx_aborted:
  587. if ((stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET)) || dev->msg_err)
  588. complete(&dev->cmd_complete);
  589. return IRQ_HANDLED;
  590. }
  591. EXPORT_SYMBOL_GPL(i2c_dw_isr);
  592. void i2c_dw_enable(struct dw_i2c_dev *dev)
  593. {
  594. /* Enable the adapter */
  595. dw_writel(dev, 1, DW_IC_ENABLE);
  596. }
  597. EXPORT_SYMBOL_GPL(i2c_dw_enable);
  598. u32 i2c_dw_is_enabled(struct dw_i2c_dev *dev)
  599. {
  600. return dw_readl(dev, DW_IC_ENABLE);
  601. }
  602. EXPORT_SYMBOL_GPL(i2c_dw_is_enabled);
  603. void i2c_dw_disable(struct dw_i2c_dev *dev)
  604. {
  605. /* Disable controller */
  606. dw_writel(dev, 0, DW_IC_ENABLE);
  607. /* Disable all interupts */
  608. dw_writel(dev, 0, DW_IC_INTR_MASK);
  609. dw_readl(dev, DW_IC_CLR_INTR);
  610. }
  611. EXPORT_SYMBOL_GPL(i2c_dw_disable);
  612. void i2c_dw_clear_int(struct dw_i2c_dev *dev)
  613. {
  614. dw_readl(dev, DW_IC_CLR_INTR);
  615. }
  616. EXPORT_SYMBOL_GPL(i2c_dw_clear_int);
  617. void i2c_dw_disable_int(struct dw_i2c_dev *dev)
  618. {
  619. dw_writel(dev, 0, DW_IC_INTR_MASK);
  620. }
  621. EXPORT_SYMBOL_GPL(i2c_dw_disable_int);
  622. u32 i2c_dw_read_comp_param(struct dw_i2c_dev *dev)
  623. {
  624. return dw_readl(dev, DW_IC_COMP_PARAM_1);
  625. }
  626. EXPORT_SYMBOL_GPL(i2c_dw_read_comp_param);