i2c-designware-core.c 18 KB

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