i2c-designware-core.c 24 KB

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