i2c-designware-core.c 22 KB

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