i2c-designware-core.c 18 KB

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