i2c-designware-core.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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. static u32 dw_readl(struct dw_i2c_dev *dev, int offset)
  156. {
  157. u32 value;
  158. if (dev->accessor_flags & ACCESS_16BIT)
  159. value = readw_relaxed(dev->base + offset) |
  160. (readw_relaxed(dev->base + offset + 2) << 16);
  161. else
  162. value = readl_relaxed(dev->base + offset);
  163. if (dev->accessor_flags & ACCESS_SWAP)
  164. return swab32(value);
  165. else
  166. return value;
  167. }
  168. static 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_relaxed((u16)b, dev->base + offset);
  174. writew_relaxed((u16)(b >> 16), dev->base + offset + 2);
  175. } else {
  176. writel_relaxed(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. int ret;
  265. if (dev->acquire_lock) {
  266. ret = dev->acquire_lock(dev);
  267. if (ret) {
  268. dev_err(dev->dev, "couldn't acquire bus ownership\n");
  269. return ret;
  270. }
  271. }
  272. input_clock_khz = dev->get_clk_rate_khz(dev);
  273. reg = dw_readl(dev, DW_IC_COMP_TYPE);
  274. if (reg == ___constant_swab32(DW_IC_COMP_TYPE_VALUE)) {
  275. /* Configure register endianess access */
  276. dev->accessor_flags |= ACCESS_SWAP;
  277. } else if (reg == (DW_IC_COMP_TYPE_VALUE & 0x0000ffff)) {
  278. /* Configure register access mode 16bit */
  279. dev->accessor_flags |= ACCESS_16BIT;
  280. } else if (reg != DW_IC_COMP_TYPE_VALUE) {
  281. dev_err(dev->dev, "Unknown Synopsys component type: "
  282. "0x%08x\n", reg);
  283. if (dev->release_lock)
  284. dev->release_lock(dev);
  285. return -ENODEV;
  286. }
  287. /* Disable the adapter */
  288. __i2c_dw_enable(dev, false);
  289. /* set standard and fast speed deviders for high/low periods */
  290. sda_falling_time = dev->sda_falling_time ?: 300; /* ns */
  291. scl_falling_time = dev->scl_falling_time ?: 300; /* ns */
  292. /* Set SCL timing parameters for standard-mode */
  293. if (dev->ss_hcnt && dev->ss_lcnt) {
  294. hcnt = dev->ss_hcnt;
  295. lcnt = dev->ss_lcnt;
  296. } else {
  297. hcnt = i2c_dw_scl_hcnt(input_clock_khz,
  298. 4000, /* tHD;STA = tHIGH = 4.0 us */
  299. sda_falling_time,
  300. 0, /* 0: DW default, 1: Ideal */
  301. 0); /* No offset */
  302. lcnt = i2c_dw_scl_lcnt(input_clock_khz,
  303. 4700, /* tLOW = 4.7 us */
  304. scl_falling_time,
  305. 0); /* No offset */
  306. }
  307. dw_writel(dev, hcnt, DW_IC_SS_SCL_HCNT);
  308. dw_writel(dev, lcnt, DW_IC_SS_SCL_LCNT);
  309. dev_dbg(dev->dev, "Standard-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
  310. /* Set SCL timing parameters for fast-mode */
  311. if (dev->fs_hcnt && dev->fs_lcnt) {
  312. hcnt = dev->fs_hcnt;
  313. lcnt = dev->fs_lcnt;
  314. } else {
  315. hcnt = i2c_dw_scl_hcnt(input_clock_khz,
  316. 600, /* tHD;STA = tHIGH = 0.6 us */
  317. sda_falling_time,
  318. 0, /* 0: DW default, 1: Ideal */
  319. 0); /* No offset */
  320. lcnt = i2c_dw_scl_lcnt(input_clock_khz,
  321. 1300, /* tLOW = 1.3 us */
  322. scl_falling_time,
  323. 0); /* No offset */
  324. }
  325. dw_writel(dev, hcnt, DW_IC_FS_SCL_HCNT);
  326. dw_writel(dev, lcnt, DW_IC_FS_SCL_LCNT);
  327. dev_dbg(dev->dev, "Fast-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
  328. /* Configure SDA Hold Time if required */
  329. if (dev->sda_hold_time) {
  330. reg = dw_readl(dev, DW_IC_COMP_VERSION);
  331. if (reg >= DW_IC_SDA_HOLD_MIN_VERS)
  332. dw_writel(dev, dev->sda_hold_time, DW_IC_SDA_HOLD);
  333. else
  334. dev_warn(dev->dev,
  335. "Hardware too old to adjust SDA hold time.");
  336. }
  337. /* Configure Tx/Rx FIFO threshold levels */
  338. dw_writel(dev, dev->tx_fifo_depth / 2, DW_IC_TX_TL);
  339. dw_writel(dev, 0, DW_IC_RX_TL);
  340. /* configure the i2c master */
  341. dw_writel(dev, dev->master_cfg , DW_IC_CON);
  342. if (dev->release_lock)
  343. dev->release_lock(dev);
  344. return 0;
  345. }
  346. EXPORT_SYMBOL_GPL(i2c_dw_init);
  347. /*
  348. * Waiting for bus not busy
  349. */
  350. static int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev)
  351. {
  352. int timeout = TIMEOUT;
  353. while (dw_readl(dev, DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) {
  354. if (timeout <= 0) {
  355. dev_warn(dev->dev, "timeout waiting for bus ready\n");
  356. return -ETIMEDOUT;
  357. }
  358. timeout--;
  359. usleep_range(1000, 1100);
  360. }
  361. return 0;
  362. }
  363. static void i2c_dw_xfer_init(struct dw_i2c_dev *dev)
  364. {
  365. struct i2c_msg *msgs = dev->msgs;
  366. u32 ic_con, ic_tar = 0;
  367. /* Disable the adapter */
  368. __i2c_dw_enable(dev, false);
  369. /* if the slave address is ten bit address, enable 10BITADDR */
  370. ic_con = dw_readl(dev, DW_IC_CON);
  371. if (msgs[dev->msg_write_idx].flags & I2C_M_TEN) {
  372. ic_con |= DW_IC_CON_10BITADDR_MASTER;
  373. /*
  374. * If I2C_DYNAMIC_TAR_UPDATE is set, the 10-bit addressing
  375. * mode has to be enabled via bit 12 of IC_TAR register.
  376. * We set it always as I2C_DYNAMIC_TAR_UPDATE can't be
  377. * detected from registers.
  378. */
  379. ic_tar = DW_IC_TAR_10BITADDR_MASTER;
  380. } else {
  381. ic_con &= ~DW_IC_CON_10BITADDR_MASTER;
  382. }
  383. dw_writel(dev, ic_con, DW_IC_CON);
  384. /*
  385. * Set the slave (target) address and enable 10-bit addressing mode
  386. * if applicable.
  387. */
  388. dw_writel(dev, msgs[dev->msg_write_idx].addr | ic_tar, DW_IC_TAR);
  389. /* enforce disabled interrupts (due to HW issues) */
  390. i2c_dw_disable_int(dev);
  391. /* Enable the adapter */
  392. __i2c_dw_enable(dev, true);
  393. /* Clear and enable interrupts */
  394. dw_readl(dev, DW_IC_CLR_INTR);
  395. dw_writel(dev, DW_IC_INTR_DEFAULT_MASK, DW_IC_INTR_MASK);
  396. }
  397. /*
  398. * Initiate (and continue) low level master read/write transaction.
  399. * This function is only called from i2c_dw_isr, and pumping i2c_msg
  400. * messages into the tx buffer. Even if the size of i2c_msg data is
  401. * longer than the size of the tx buffer, it handles everything.
  402. */
  403. static void
  404. i2c_dw_xfer_msg(struct dw_i2c_dev *dev)
  405. {
  406. struct i2c_msg *msgs = dev->msgs;
  407. u32 intr_mask;
  408. int tx_limit, rx_limit;
  409. u32 addr = msgs[dev->msg_write_idx].addr;
  410. u32 buf_len = dev->tx_buf_len;
  411. u8 *buf = dev->tx_buf;
  412. bool need_restart = false;
  413. intr_mask = DW_IC_INTR_DEFAULT_MASK;
  414. for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) {
  415. /*
  416. * if target address has changed, we need to
  417. * reprogram the target address in the i2c
  418. * adapter when we are done with this transfer
  419. */
  420. if (msgs[dev->msg_write_idx].addr != addr) {
  421. dev_err(dev->dev,
  422. "%s: invalid target address\n", __func__);
  423. dev->msg_err = -EINVAL;
  424. break;
  425. }
  426. if (msgs[dev->msg_write_idx].len == 0) {
  427. dev_err(dev->dev,
  428. "%s: invalid message length\n", __func__);
  429. dev->msg_err = -EINVAL;
  430. break;
  431. }
  432. if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {
  433. /* new i2c_msg */
  434. buf = msgs[dev->msg_write_idx].buf;
  435. buf_len = msgs[dev->msg_write_idx].len;
  436. /* If both IC_EMPTYFIFO_HOLD_MASTER_EN and
  437. * IC_RESTART_EN are set, we must manually
  438. * set restart bit between messages.
  439. */
  440. if ((dev->master_cfg & DW_IC_CON_RESTART_EN) &&
  441. (dev->msg_write_idx > 0))
  442. need_restart = true;
  443. }
  444. tx_limit = dev->tx_fifo_depth - dw_readl(dev, DW_IC_TXFLR);
  445. rx_limit = dev->rx_fifo_depth - dw_readl(dev, DW_IC_RXFLR);
  446. while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) {
  447. u32 cmd = 0;
  448. /*
  449. * If IC_EMPTYFIFO_HOLD_MASTER_EN is set we must
  450. * manually set the stop bit. However, it cannot be
  451. * detected from the registers so we set it always
  452. * when writing/reading the last byte.
  453. */
  454. if (dev->msg_write_idx == dev->msgs_num - 1 &&
  455. buf_len == 1)
  456. cmd |= BIT(9);
  457. if (need_restart) {
  458. cmd |= BIT(10);
  459. need_restart = false;
  460. }
  461. if (msgs[dev->msg_write_idx].flags & I2C_M_RD) {
  462. /* avoid rx buffer overrun */
  463. if (rx_limit - dev->rx_outstanding <= 0)
  464. break;
  465. dw_writel(dev, cmd | 0x100, DW_IC_DATA_CMD);
  466. rx_limit--;
  467. dev->rx_outstanding++;
  468. } else
  469. dw_writel(dev, cmd | *buf++, DW_IC_DATA_CMD);
  470. tx_limit--; buf_len--;
  471. }
  472. dev->tx_buf = buf;
  473. dev->tx_buf_len = buf_len;
  474. if (buf_len > 0) {
  475. /* more bytes to be written */
  476. dev->status |= STATUS_WRITE_IN_PROGRESS;
  477. break;
  478. } else
  479. dev->status &= ~STATUS_WRITE_IN_PROGRESS;
  480. }
  481. /*
  482. * If i2c_msg index search is completed, we don't need TX_EMPTY
  483. * interrupt any more.
  484. */
  485. if (dev->msg_write_idx == dev->msgs_num)
  486. intr_mask &= ~DW_IC_INTR_TX_EMPTY;
  487. if (dev->msg_err)
  488. intr_mask = 0;
  489. dw_writel(dev, intr_mask, DW_IC_INTR_MASK);
  490. }
  491. static void
  492. i2c_dw_read(struct dw_i2c_dev *dev)
  493. {
  494. struct i2c_msg *msgs = dev->msgs;
  495. int rx_valid;
  496. for (; dev->msg_read_idx < dev->msgs_num; dev->msg_read_idx++) {
  497. u32 len;
  498. u8 *buf;
  499. if (!(msgs[dev->msg_read_idx].flags & I2C_M_RD))
  500. continue;
  501. if (!(dev->status & STATUS_READ_IN_PROGRESS)) {
  502. len = msgs[dev->msg_read_idx].len;
  503. buf = msgs[dev->msg_read_idx].buf;
  504. } else {
  505. len = dev->rx_buf_len;
  506. buf = dev->rx_buf;
  507. }
  508. rx_valid = dw_readl(dev, DW_IC_RXFLR);
  509. for (; len > 0 && rx_valid > 0; len--, rx_valid--) {
  510. *buf++ = dw_readl(dev, DW_IC_DATA_CMD);
  511. dev->rx_outstanding--;
  512. }
  513. if (len > 0) {
  514. dev->status |= STATUS_READ_IN_PROGRESS;
  515. dev->rx_buf_len = len;
  516. dev->rx_buf = buf;
  517. return;
  518. } else
  519. dev->status &= ~STATUS_READ_IN_PROGRESS;
  520. }
  521. }
  522. static int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev)
  523. {
  524. unsigned long abort_source = dev->abort_source;
  525. int i;
  526. if (abort_source & DW_IC_TX_ABRT_NOACK) {
  527. for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  528. dev_dbg(dev->dev,
  529. "%s: %s\n", __func__, abort_sources[i]);
  530. return -EREMOTEIO;
  531. }
  532. for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  533. dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]);
  534. if (abort_source & DW_IC_TX_ARB_LOST)
  535. return -EAGAIN;
  536. else if (abort_source & DW_IC_TX_ABRT_GCALL_READ)
  537. return -EINVAL; /* wrong msgs[] data */
  538. else
  539. return -EIO;
  540. }
  541. /*
  542. * Prepare controller for a transaction and call i2c_dw_xfer_msg
  543. */
  544. static int
  545. i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  546. {
  547. struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
  548. int ret;
  549. dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
  550. mutex_lock(&dev->lock);
  551. pm_runtime_get_sync(dev->dev);
  552. reinit_completion(&dev->cmd_complete);
  553. dev->msgs = msgs;
  554. dev->msgs_num = num;
  555. dev->cmd_err = 0;
  556. dev->msg_write_idx = 0;
  557. dev->msg_read_idx = 0;
  558. dev->msg_err = 0;
  559. dev->status = STATUS_IDLE;
  560. dev->abort_source = 0;
  561. dev->rx_outstanding = 0;
  562. if (dev->acquire_lock) {
  563. ret = dev->acquire_lock(dev);
  564. if (ret) {
  565. dev_err(dev->dev, "couldn't acquire bus ownership\n");
  566. goto done_nolock;
  567. }
  568. }
  569. ret = i2c_dw_wait_bus_not_busy(dev);
  570. if (ret < 0)
  571. goto done;
  572. /* start the transfers */
  573. i2c_dw_xfer_init(dev);
  574. /* wait for tx to complete */
  575. if (!wait_for_completion_timeout(&dev->cmd_complete, HZ)) {
  576. dev_err(dev->dev, "controller timed out\n");
  577. /* i2c_dw_init implicitly disables the adapter */
  578. i2c_dw_init(dev);
  579. ret = -ETIMEDOUT;
  580. goto done;
  581. }
  582. /*
  583. * We must disable the adapter before unlocking the &dev->lock mutex
  584. * below. Otherwise the hardware might continue generating interrupts
  585. * which in turn causes a race condition with the following transfer.
  586. * Needs some more investigation if the additional interrupts are
  587. * a hardware bug or this driver doesn't handle them correctly yet.
  588. */
  589. __i2c_dw_enable(dev, false);
  590. if (dev->msg_err) {
  591. ret = dev->msg_err;
  592. goto done;
  593. }
  594. /* no error */
  595. if (likely(!dev->cmd_err)) {
  596. ret = num;
  597. goto done;
  598. }
  599. /* We have an error */
  600. if (dev->cmd_err == DW_IC_ERR_TX_ABRT) {
  601. ret = i2c_dw_handle_tx_abort(dev);
  602. goto done;
  603. }
  604. ret = -EIO;
  605. done:
  606. if (dev->release_lock)
  607. dev->release_lock(dev);
  608. done_nolock:
  609. pm_runtime_mark_last_busy(dev->dev);
  610. pm_runtime_put_autosuspend(dev->dev);
  611. mutex_unlock(&dev->lock);
  612. return ret;
  613. }
  614. static u32 i2c_dw_func(struct i2c_adapter *adap)
  615. {
  616. struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
  617. return dev->functionality;
  618. }
  619. static struct i2c_algorithm i2c_dw_algo = {
  620. .master_xfer = i2c_dw_xfer,
  621. .functionality = i2c_dw_func,
  622. };
  623. static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev)
  624. {
  625. u32 stat;
  626. /*
  627. * The IC_INTR_STAT register just indicates "enabled" interrupts.
  628. * Ths unmasked raw version of interrupt status bits are available
  629. * in the IC_RAW_INTR_STAT register.
  630. *
  631. * That is,
  632. * stat = dw_readl(IC_INTR_STAT);
  633. * equals to,
  634. * stat = dw_readl(IC_RAW_INTR_STAT) & dw_readl(IC_INTR_MASK);
  635. *
  636. * The raw version might be useful for debugging purposes.
  637. */
  638. stat = dw_readl(dev, DW_IC_INTR_STAT);
  639. /*
  640. * Do not use the IC_CLR_INTR register to clear interrupts, or
  641. * you'll miss some interrupts, triggered during the period from
  642. * dw_readl(IC_INTR_STAT) to dw_readl(IC_CLR_INTR).
  643. *
  644. * Instead, use the separately-prepared IC_CLR_* registers.
  645. */
  646. if (stat & DW_IC_INTR_RX_UNDER)
  647. dw_readl(dev, DW_IC_CLR_RX_UNDER);
  648. if (stat & DW_IC_INTR_RX_OVER)
  649. dw_readl(dev, DW_IC_CLR_RX_OVER);
  650. if (stat & DW_IC_INTR_TX_OVER)
  651. dw_readl(dev, DW_IC_CLR_TX_OVER);
  652. if (stat & DW_IC_INTR_RD_REQ)
  653. dw_readl(dev, DW_IC_CLR_RD_REQ);
  654. if (stat & DW_IC_INTR_TX_ABRT) {
  655. /*
  656. * The IC_TX_ABRT_SOURCE register is cleared whenever
  657. * the IC_CLR_TX_ABRT is read. Preserve it beforehand.
  658. */
  659. dev->abort_source = dw_readl(dev, DW_IC_TX_ABRT_SOURCE);
  660. dw_readl(dev, DW_IC_CLR_TX_ABRT);
  661. }
  662. if (stat & DW_IC_INTR_RX_DONE)
  663. dw_readl(dev, DW_IC_CLR_RX_DONE);
  664. if (stat & DW_IC_INTR_ACTIVITY)
  665. dw_readl(dev, DW_IC_CLR_ACTIVITY);
  666. if (stat & DW_IC_INTR_STOP_DET)
  667. dw_readl(dev, DW_IC_CLR_STOP_DET);
  668. if (stat & DW_IC_INTR_START_DET)
  669. dw_readl(dev, DW_IC_CLR_START_DET);
  670. if (stat & DW_IC_INTR_GEN_CALL)
  671. dw_readl(dev, DW_IC_CLR_GEN_CALL);
  672. return stat;
  673. }
  674. /*
  675. * Interrupt service routine. This gets called whenever an I2C interrupt
  676. * occurs.
  677. */
  678. static irqreturn_t i2c_dw_isr(int this_irq, void *dev_id)
  679. {
  680. struct dw_i2c_dev *dev = dev_id;
  681. u32 stat, enabled;
  682. enabled = dw_readl(dev, DW_IC_ENABLE);
  683. stat = dw_readl(dev, DW_IC_RAW_INTR_STAT);
  684. dev_dbg(dev->dev, "%s: enabled=%#x stat=%#x\n", __func__, enabled, stat);
  685. if (!enabled || !(stat & ~DW_IC_INTR_ACTIVITY))
  686. return IRQ_NONE;
  687. stat = i2c_dw_read_clear_intrbits(dev);
  688. if (stat & DW_IC_INTR_TX_ABRT) {
  689. dev->cmd_err |= DW_IC_ERR_TX_ABRT;
  690. dev->status = STATUS_IDLE;
  691. /*
  692. * Anytime TX_ABRT is set, the contents of the tx/rx
  693. * buffers are flushed. Make sure to skip them.
  694. */
  695. dw_writel(dev, 0, DW_IC_INTR_MASK);
  696. goto tx_aborted;
  697. }
  698. if (stat & DW_IC_INTR_RX_FULL)
  699. i2c_dw_read(dev);
  700. if (stat & DW_IC_INTR_TX_EMPTY)
  701. i2c_dw_xfer_msg(dev);
  702. /*
  703. * No need to modify or disable the interrupt mask here.
  704. * i2c_dw_xfer_msg() will take care of it according to
  705. * the current transmit status.
  706. */
  707. tx_aborted:
  708. if ((stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET)) || dev->msg_err)
  709. complete(&dev->cmd_complete);
  710. else if (unlikely(dev->accessor_flags & ACCESS_INTR_MASK)) {
  711. /* workaround to trigger pending interrupt */
  712. stat = dw_readl(dev, DW_IC_INTR_MASK);
  713. i2c_dw_disable_int(dev);
  714. dw_writel(dev, stat, DW_IC_INTR_MASK);
  715. }
  716. return IRQ_HANDLED;
  717. }
  718. void i2c_dw_disable(struct dw_i2c_dev *dev)
  719. {
  720. /* Disable controller */
  721. __i2c_dw_enable(dev, false);
  722. /* Disable all interupts */
  723. dw_writel(dev, 0, DW_IC_INTR_MASK);
  724. dw_readl(dev, DW_IC_CLR_INTR);
  725. }
  726. EXPORT_SYMBOL_GPL(i2c_dw_disable);
  727. void i2c_dw_disable_int(struct dw_i2c_dev *dev)
  728. {
  729. dw_writel(dev, 0, DW_IC_INTR_MASK);
  730. }
  731. EXPORT_SYMBOL_GPL(i2c_dw_disable_int);
  732. u32 i2c_dw_read_comp_param(struct dw_i2c_dev *dev)
  733. {
  734. return dw_readl(dev, DW_IC_COMP_PARAM_1);
  735. }
  736. EXPORT_SYMBOL_GPL(i2c_dw_read_comp_param);
  737. int i2c_dw_probe(struct dw_i2c_dev *dev)
  738. {
  739. struct i2c_adapter *adap = &dev->adapter;
  740. int r;
  741. init_completion(&dev->cmd_complete);
  742. mutex_init(&dev->lock);
  743. r = i2c_dw_init(dev);
  744. if (r)
  745. return r;
  746. snprintf(adap->name, sizeof(adap->name),
  747. "Synopsys DesignWare I2C adapter");
  748. adap->algo = &i2c_dw_algo;
  749. adap->dev.parent = dev->dev;
  750. i2c_set_adapdata(adap, dev);
  751. i2c_dw_disable_int(dev);
  752. r = devm_request_irq(dev->dev, dev->irq, i2c_dw_isr, IRQF_SHARED,
  753. dev_name(dev->dev), dev);
  754. if (r) {
  755. dev_err(dev->dev, "failure requesting irq %i: %d\n",
  756. dev->irq, r);
  757. return r;
  758. }
  759. r = i2c_add_numbered_adapter(adap);
  760. if (r)
  761. dev_err(dev->dev, "failure adding adapter: %d\n", r);
  762. return r;
  763. }
  764. EXPORT_SYMBOL_GPL(i2c_dw_probe);
  765. MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter core");
  766. MODULE_LICENSE("GPL");