i2c-designware-core.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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;
  157. if (dev->accessor_flags & ACCESS_16BIT)
  158. value = readw(dev->base + offset) |
  159. (readw(dev->base + offset + 2) << 16);
  160. else
  161. value = readl(dev->base + offset);
  162. if (dev->accessor_flags & ACCESS_SWAP)
  163. return swab32(value);
  164. else
  165. return value;
  166. }
  167. void dw_writel(struct dw_i2c_dev *dev, u32 b, int offset)
  168. {
  169. if (dev->accessor_flags & ACCESS_SWAP)
  170. b = swab32(b);
  171. if (dev->accessor_flags & ACCESS_16BIT) {
  172. writew((u16)b, dev->base + offset);
  173. writew((u16)(b >> 16), dev->base + offset + 2);
  174. } else {
  175. writel(b, dev->base + offset);
  176. }
  177. }
  178. static u32
  179. i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset)
  180. {
  181. /*
  182. * DesignWare I2C core doesn't seem to have solid strategy to meet
  183. * the tHD;STA timing spec. Configuring _HCNT based on tHIGH spec
  184. * will result in violation of the tHD;STA spec.
  185. */
  186. if (cond)
  187. /*
  188. * Conditional expression:
  189. *
  190. * IC_[FS]S_SCL_HCNT + (1+4+3) >= IC_CLK * tHIGH
  191. *
  192. * This is based on the DW manuals, and represents an ideal
  193. * configuration. The resulting I2C bus speed will be
  194. * faster than any of the others.
  195. *
  196. * If your hardware is free from tHD;STA issue, try this one.
  197. */
  198. return (ic_clk * tSYMBOL + 5000) / 10000 - 8 + offset;
  199. else
  200. /*
  201. * Conditional expression:
  202. *
  203. * IC_[FS]S_SCL_HCNT + 3 >= IC_CLK * (tHD;STA + tf)
  204. *
  205. * This is just experimental rule; the tHD;STA period turned
  206. * out to be proportinal to (_HCNT + 3). With this setting,
  207. * we could meet both tHIGH and tHD;STA timing specs.
  208. *
  209. * If unsure, you'd better to take this alternative.
  210. *
  211. * The reason why we need to take into account "tf" here,
  212. * is the same as described in i2c_dw_scl_lcnt().
  213. */
  214. return (ic_clk * (tSYMBOL + tf) + 5000) / 10000 - 3 + offset;
  215. }
  216. static u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset)
  217. {
  218. /*
  219. * Conditional expression:
  220. *
  221. * IC_[FS]S_SCL_LCNT + 1 >= IC_CLK * (tLOW + tf)
  222. *
  223. * DW I2C core starts counting the SCL CNTs for the LOW period
  224. * of the SCL clock (tLOW) as soon as it pulls the SCL line.
  225. * In order to meet the tLOW timing spec, we need to take into
  226. * account the fall time of SCL signal (tf). Default tf value
  227. * should be 0.3 us, for safety.
  228. */
  229. return ((ic_clk * (tLOW + tf) + 5000) / 10000) - 1 + offset;
  230. }
  231. /**
  232. * i2c_dw_init() - initialize the designware i2c master hardware
  233. * @dev: device private data
  234. *
  235. * This functions configures and enables the I2C master.
  236. * This function is called during I2C init function, and in case of timeout at
  237. * run time.
  238. */
  239. int i2c_dw_init(struct dw_i2c_dev *dev)
  240. {
  241. u32 input_clock_khz;
  242. u32 hcnt, lcnt;
  243. u32 reg;
  244. input_clock_khz = dev->get_clk_rate_khz(dev);
  245. reg = dw_readl(dev, DW_IC_COMP_TYPE);
  246. if (reg == ___constant_swab32(DW_IC_COMP_TYPE_VALUE)) {
  247. /* Configure register endianess access */
  248. dev->accessor_flags |= ACCESS_SWAP;
  249. } else if (reg == (DW_IC_COMP_TYPE_VALUE & 0x0000ffff)) {
  250. /* Configure register access mode 16bit */
  251. dev->accessor_flags |= ACCESS_16BIT;
  252. } else if (reg != DW_IC_COMP_TYPE_VALUE) {
  253. dev_err(dev->dev, "Unknown Synopsys component type: "
  254. "0x%08x\n", reg);
  255. return -ENODEV;
  256. }
  257. /* Disable the adapter */
  258. dw_writel(dev, 0, DW_IC_ENABLE);
  259. /* set standard and fast speed deviders for high/low periods */
  260. /* Standard-mode */
  261. hcnt = i2c_dw_scl_hcnt(input_clock_khz,
  262. 40, /* tHD;STA = tHIGH = 4.0 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. 47, /* tLOW = 4.7 us */
  268. 3, /* tf = 0.3 us */
  269. 0); /* No offset */
  270. dw_writel(dev, hcnt, DW_IC_SS_SCL_HCNT);
  271. dw_writel(dev, lcnt, DW_IC_SS_SCL_LCNT);
  272. dev_dbg(dev->dev, "Standard-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
  273. /* Fast-mode */
  274. hcnt = i2c_dw_scl_hcnt(input_clock_khz,
  275. 6, /* tHD;STA = tHIGH = 0.6 us */
  276. 3, /* tf = 0.3 us */
  277. 0, /* 0: DW default, 1: Ideal */
  278. 0); /* No offset */
  279. lcnt = i2c_dw_scl_lcnt(input_clock_khz,
  280. 13, /* tLOW = 1.3 us */
  281. 3, /* tf = 0.3 us */
  282. 0); /* No offset */
  283. dw_writel(dev, hcnt, DW_IC_FS_SCL_HCNT);
  284. dw_writel(dev, lcnt, DW_IC_FS_SCL_LCNT);
  285. dev_dbg(dev->dev, "Fast-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
  286. /* Configure Tx/Rx FIFO threshold levels */
  287. dw_writel(dev, dev->tx_fifo_depth - 1, DW_IC_TX_TL);
  288. dw_writel(dev, 0, DW_IC_RX_TL);
  289. /* configure the i2c master */
  290. dw_writel(dev, dev->master_cfg , DW_IC_CON);
  291. return 0;
  292. }
  293. EXPORT_SYMBOL_GPL(i2c_dw_init);
  294. /*
  295. * Waiting for bus not busy
  296. */
  297. static int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev)
  298. {
  299. int timeout = TIMEOUT;
  300. while (dw_readl(dev, DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) {
  301. if (timeout <= 0) {
  302. dev_warn(dev->dev, "timeout waiting for bus ready\n");
  303. return -ETIMEDOUT;
  304. }
  305. timeout--;
  306. mdelay(1);
  307. }
  308. return 0;
  309. }
  310. static void i2c_dw_xfer_init(struct dw_i2c_dev *dev)
  311. {
  312. struct i2c_msg *msgs = dev->msgs;
  313. u32 ic_con;
  314. /* Disable the adapter */
  315. dw_writel(dev, 0, DW_IC_ENABLE);
  316. /* set the slave (target) address */
  317. dw_writel(dev, msgs[dev->msg_write_idx].addr, DW_IC_TAR);
  318. /* if the slave address is ten bit address, enable 10BITADDR */
  319. ic_con = dw_readl(dev, DW_IC_CON);
  320. if (msgs[dev->msg_write_idx].flags & I2C_M_TEN)
  321. ic_con |= DW_IC_CON_10BITADDR_MASTER;
  322. else
  323. ic_con &= ~DW_IC_CON_10BITADDR_MASTER;
  324. dw_writel(dev, ic_con, DW_IC_CON);
  325. /* Enable the adapter */
  326. dw_writel(dev, 1, DW_IC_ENABLE);
  327. /* Enable interrupts */
  328. dw_writel(dev, DW_IC_INTR_DEFAULT_MASK, DW_IC_INTR_MASK);
  329. }
  330. /*
  331. * Initiate (and continue) low level master read/write transaction.
  332. * This function is only called from i2c_dw_isr, and pumping i2c_msg
  333. * messages into the tx buffer. Even if the size of i2c_msg data is
  334. * longer than the size of the tx buffer, it handles everything.
  335. */
  336. static void
  337. i2c_dw_xfer_msg(struct dw_i2c_dev *dev)
  338. {
  339. struct i2c_msg *msgs = dev->msgs;
  340. u32 intr_mask;
  341. int tx_limit, rx_limit;
  342. u32 addr = msgs[dev->msg_write_idx].addr;
  343. u32 buf_len = dev->tx_buf_len;
  344. u8 *buf = dev->tx_buf;
  345. intr_mask = DW_IC_INTR_DEFAULT_MASK;
  346. for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) {
  347. /*
  348. * if target address has changed, we need to
  349. * reprogram the target address in the i2c
  350. * adapter when we are done with this transfer
  351. */
  352. if (msgs[dev->msg_write_idx].addr != addr) {
  353. dev_err(dev->dev,
  354. "%s: invalid target address\n", __func__);
  355. dev->msg_err = -EINVAL;
  356. break;
  357. }
  358. if (msgs[dev->msg_write_idx].len == 0) {
  359. dev_err(dev->dev,
  360. "%s: invalid message length\n", __func__);
  361. dev->msg_err = -EINVAL;
  362. break;
  363. }
  364. if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {
  365. /* new i2c_msg */
  366. buf = msgs[dev->msg_write_idx].buf;
  367. buf_len = msgs[dev->msg_write_idx].len;
  368. }
  369. tx_limit = dev->tx_fifo_depth - dw_readl(dev, DW_IC_TXFLR);
  370. rx_limit = dev->rx_fifo_depth - dw_readl(dev, DW_IC_RXFLR);
  371. while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) {
  372. if (msgs[dev->msg_write_idx].flags & I2C_M_RD) {
  373. dw_writel(dev, 0x100, DW_IC_DATA_CMD);
  374. rx_limit--;
  375. } else
  376. dw_writel(dev, *buf++, DW_IC_DATA_CMD);
  377. tx_limit--; buf_len--;
  378. }
  379. dev->tx_buf = buf;
  380. dev->tx_buf_len = buf_len;
  381. if (buf_len > 0) {
  382. /* more bytes to be written */
  383. dev->status |= STATUS_WRITE_IN_PROGRESS;
  384. break;
  385. } else
  386. dev->status &= ~STATUS_WRITE_IN_PROGRESS;
  387. }
  388. /*
  389. * If i2c_msg index search is completed, we don't need TX_EMPTY
  390. * interrupt any more.
  391. */
  392. if (dev->msg_write_idx == dev->msgs_num)
  393. intr_mask &= ~DW_IC_INTR_TX_EMPTY;
  394. if (dev->msg_err)
  395. intr_mask = 0;
  396. dw_writel(dev, intr_mask, DW_IC_INTR_MASK);
  397. }
  398. static void
  399. i2c_dw_read(struct dw_i2c_dev *dev)
  400. {
  401. struct i2c_msg *msgs = dev->msgs;
  402. int rx_valid;
  403. for (; dev->msg_read_idx < dev->msgs_num; dev->msg_read_idx++) {
  404. u32 len;
  405. u8 *buf;
  406. if (!(msgs[dev->msg_read_idx].flags & I2C_M_RD))
  407. continue;
  408. if (!(dev->status & STATUS_READ_IN_PROGRESS)) {
  409. len = msgs[dev->msg_read_idx].len;
  410. buf = msgs[dev->msg_read_idx].buf;
  411. } else {
  412. len = dev->rx_buf_len;
  413. buf = dev->rx_buf;
  414. }
  415. rx_valid = dw_readl(dev, DW_IC_RXFLR);
  416. for (; len > 0 && rx_valid > 0; len--, rx_valid--)
  417. *buf++ = dw_readl(dev, DW_IC_DATA_CMD);
  418. if (len > 0) {
  419. dev->status |= STATUS_READ_IN_PROGRESS;
  420. dev->rx_buf_len = len;
  421. dev->rx_buf = buf;
  422. return;
  423. } else
  424. dev->status &= ~STATUS_READ_IN_PROGRESS;
  425. }
  426. }
  427. static int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev)
  428. {
  429. unsigned long abort_source = dev->abort_source;
  430. int i;
  431. if (abort_source & DW_IC_TX_ABRT_NOACK) {
  432. for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  433. dev_dbg(dev->dev,
  434. "%s: %s\n", __func__, abort_sources[i]);
  435. return -EREMOTEIO;
  436. }
  437. for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  438. dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]);
  439. if (abort_source & DW_IC_TX_ARB_LOST)
  440. return -EAGAIN;
  441. else if (abort_source & DW_IC_TX_ABRT_GCALL_READ)
  442. return -EINVAL; /* wrong msgs[] data */
  443. else
  444. return -EIO;
  445. }
  446. /*
  447. * Prepare controller for a transaction and call i2c_dw_xfer_msg
  448. */
  449. int
  450. i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  451. {
  452. struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
  453. int ret;
  454. dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
  455. mutex_lock(&dev->lock);
  456. pm_runtime_get_sync(dev->dev);
  457. INIT_COMPLETION(dev->cmd_complete);
  458. dev->msgs = msgs;
  459. dev->msgs_num = num;
  460. dev->cmd_err = 0;
  461. dev->msg_write_idx = 0;
  462. dev->msg_read_idx = 0;
  463. dev->msg_err = 0;
  464. dev->status = STATUS_IDLE;
  465. dev->abort_source = 0;
  466. ret = i2c_dw_wait_bus_not_busy(dev);
  467. if (ret < 0)
  468. goto done;
  469. /* start the transfers */
  470. i2c_dw_xfer_init(dev);
  471. /* wait for tx to complete */
  472. ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete, HZ);
  473. if (ret == 0) {
  474. dev_err(dev->dev, "controller timed out\n");
  475. i2c_dw_init(dev);
  476. ret = -ETIMEDOUT;
  477. goto done;
  478. } else if (ret < 0)
  479. goto done;
  480. if (dev->msg_err) {
  481. ret = dev->msg_err;
  482. goto done;
  483. }
  484. /* no error */
  485. if (likely(!dev->cmd_err)) {
  486. /* Disable the adapter */
  487. dw_writel(dev, 0, DW_IC_ENABLE);
  488. ret = num;
  489. goto done;
  490. }
  491. /* We have an error */
  492. if (dev->cmd_err == DW_IC_ERR_TX_ABRT) {
  493. ret = i2c_dw_handle_tx_abort(dev);
  494. goto done;
  495. }
  496. ret = -EIO;
  497. done:
  498. pm_runtime_put(dev->dev);
  499. mutex_unlock(&dev->lock);
  500. return ret;
  501. }
  502. EXPORT_SYMBOL_GPL(i2c_dw_xfer);
  503. u32 i2c_dw_func(struct i2c_adapter *adap)
  504. {
  505. struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
  506. return dev->functionality;
  507. }
  508. EXPORT_SYMBOL_GPL(i2c_dw_func);
  509. static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev)
  510. {
  511. u32 stat;
  512. /*
  513. * The IC_INTR_STAT register just indicates "enabled" interrupts.
  514. * Ths unmasked raw version of interrupt status bits are available
  515. * in the IC_RAW_INTR_STAT register.
  516. *
  517. * That is,
  518. * stat = dw_readl(IC_INTR_STAT);
  519. * equals to,
  520. * stat = dw_readl(IC_RAW_INTR_STAT) & dw_readl(IC_INTR_MASK);
  521. *
  522. * The raw version might be useful for debugging purposes.
  523. */
  524. stat = dw_readl(dev, DW_IC_INTR_STAT);
  525. /*
  526. * Do not use the IC_CLR_INTR register to clear interrupts, or
  527. * you'll miss some interrupts, triggered during the period from
  528. * dw_readl(IC_INTR_STAT) to dw_readl(IC_CLR_INTR).
  529. *
  530. * Instead, use the separately-prepared IC_CLR_* registers.
  531. */
  532. if (stat & DW_IC_INTR_RX_UNDER)
  533. dw_readl(dev, DW_IC_CLR_RX_UNDER);
  534. if (stat & DW_IC_INTR_RX_OVER)
  535. dw_readl(dev, DW_IC_CLR_RX_OVER);
  536. if (stat & DW_IC_INTR_TX_OVER)
  537. dw_readl(dev, DW_IC_CLR_TX_OVER);
  538. if (stat & DW_IC_INTR_RD_REQ)
  539. dw_readl(dev, DW_IC_CLR_RD_REQ);
  540. if (stat & DW_IC_INTR_TX_ABRT) {
  541. /*
  542. * The IC_TX_ABRT_SOURCE register is cleared whenever
  543. * the IC_CLR_TX_ABRT is read. Preserve it beforehand.
  544. */
  545. dev->abort_source = dw_readl(dev, DW_IC_TX_ABRT_SOURCE);
  546. dw_readl(dev, DW_IC_CLR_TX_ABRT);
  547. }
  548. if (stat & DW_IC_INTR_RX_DONE)
  549. dw_readl(dev, DW_IC_CLR_RX_DONE);
  550. if (stat & DW_IC_INTR_ACTIVITY)
  551. dw_readl(dev, DW_IC_CLR_ACTIVITY);
  552. if (stat & DW_IC_INTR_STOP_DET)
  553. dw_readl(dev, DW_IC_CLR_STOP_DET);
  554. if (stat & DW_IC_INTR_START_DET)
  555. dw_readl(dev, DW_IC_CLR_START_DET);
  556. if (stat & DW_IC_INTR_GEN_CALL)
  557. dw_readl(dev, DW_IC_CLR_GEN_CALL);
  558. return stat;
  559. }
  560. /*
  561. * Interrupt service routine. This gets called whenever an I2C interrupt
  562. * occurs.
  563. */
  564. irqreturn_t i2c_dw_isr(int this_irq, void *dev_id)
  565. {
  566. struct dw_i2c_dev *dev = dev_id;
  567. u32 stat, enabled;
  568. enabled = dw_readl(dev, DW_IC_ENABLE);
  569. stat = dw_readl(dev, DW_IC_RAW_INTR_STAT);
  570. dev_dbg(dev->dev, "%s: %s enabled= 0x%x stat=0x%x\n", __func__,
  571. dev->adapter.name, enabled, stat);
  572. if (!enabled || !(stat & ~DW_IC_INTR_ACTIVITY))
  573. return IRQ_NONE;
  574. stat = i2c_dw_read_clear_intrbits(dev);
  575. if (stat & DW_IC_INTR_TX_ABRT) {
  576. dev->cmd_err |= DW_IC_ERR_TX_ABRT;
  577. dev->status = STATUS_IDLE;
  578. /*
  579. * Anytime TX_ABRT is set, the contents of the tx/rx
  580. * buffers are flushed. Make sure to skip them.
  581. */
  582. dw_writel(dev, 0, DW_IC_INTR_MASK);
  583. goto tx_aborted;
  584. }
  585. if (stat & DW_IC_INTR_RX_FULL)
  586. i2c_dw_read(dev);
  587. if (stat & DW_IC_INTR_TX_EMPTY)
  588. i2c_dw_xfer_msg(dev);
  589. /*
  590. * No need to modify or disable the interrupt mask here.
  591. * i2c_dw_xfer_msg() will take care of it according to
  592. * the current transmit status.
  593. */
  594. tx_aborted:
  595. if ((stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET)) || dev->msg_err)
  596. complete(&dev->cmd_complete);
  597. return IRQ_HANDLED;
  598. }
  599. EXPORT_SYMBOL_GPL(i2c_dw_isr);
  600. void i2c_dw_enable(struct dw_i2c_dev *dev)
  601. {
  602. /* Enable the adapter */
  603. dw_writel(dev, 1, DW_IC_ENABLE);
  604. }
  605. EXPORT_SYMBOL_GPL(i2c_dw_enable);
  606. u32 i2c_dw_is_enabled(struct dw_i2c_dev *dev)
  607. {
  608. return dw_readl(dev, DW_IC_ENABLE);
  609. }
  610. EXPORT_SYMBOL_GPL(i2c_dw_is_enabled);
  611. void i2c_dw_disable(struct dw_i2c_dev *dev)
  612. {
  613. /* Disable controller */
  614. dw_writel(dev, 0, DW_IC_ENABLE);
  615. /* Disable all interupts */
  616. dw_writel(dev, 0, DW_IC_INTR_MASK);
  617. dw_readl(dev, DW_IC_CLR_INTR);
  618. }
  619. EXPORT_SYMBOL_GPL(i2c_dw_disable);
  620. void i2c_dw_clear_int(struct dw_i2c_dev *dev)
  621. {
  622. dw_readl(dev, DW_IC_CLR_INTR);
  623. }
  624. EXPORT_SYMBOL_GPL(i2c_dw_clear_int);
  625. void i2c_dw_disable_int(struct dw_i2c_dev *dev)
  626. {
  627. dw_writel(dev, 0, DW_IC_INTR_MASK);
  628. }
  629. EXPORT_SYMBOL_GPL(i2c_dw_disable_int);
  630. u32 i2c_dw_read_comp_param(struct dw_i2c_dev *dev)
  631. {
  632. return dw_readl(dev, DW_IC_COMP_PARAM_1);
  633. }
  634. EXPORT_SYMBOL_GPL(i2c_dw_read_comp_param);