i2c-designware-core.c 20 KB

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