i2c-designware-core.c 21 KB

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