i2c-designware-core.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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_HS_SCL_HCNT 0x24
  45. #define DW_IC_HS_SCL_LCNT 0x28
  46. #define DW_IC_INTR_STAT 0x2c
  47. #define DW_IC_INTR_MASK 0x30
  48. #define DW_IC_RAW_INTR_STAT 0x34
  49. #define DW_IC_RX_TL 0x38
  50. #define DW_IC_TX_TL 0x3c
  51. #define DW_IC_CLR_INTR 0x40
  52. #define DW_IC_CLR_RX_UNDER 0x44
  53. #define DW_IC_CLR_RX_OVER 0x48
  54. #define DW_IC_CLR_TX_OVER 0x4c
  55. #define DW_IC_CLR_RD_REQ 0x50
  56. #define DW_IC_CLR_TX_ABRT 0x54
  57. #define DW_IC_CLR_RX_DONE 0x58
  58. #define DW_IC_CLR_ACTIVITY 0x5c
  59. #define DW_IC_CLR_STOP_DET 0x60
  60. #define DW_IC_CLR_START_DET 0x64
  61. #define DW_IC_CLR_GEN_CALL 0x68
  62. #define DW_IC_ENABLE 0x6c
  63. #define DW_IC_STATUS 0x70
  64. #define DW_IC_TXFLR 0x74
  65. #define DW_IC_RXFLR 0x78
  66. #define DW_IC_SDA_HOLD 0x7c
  67. #define DW_IC_TX_ABRT_SOURCE 0x80
  68. #define DW_IC_ENABLE_STATUS 0x9c
  69. #define DW_IC_COMP_PARAM_1 0xf4
  70. #define DW_IC_COMP_VERSION 0xf8
  71. #define DW_IC_SDA_HOLD_MIN_VERS 0x3131312A
  72. #define DW_IC_COMP_TYPE 0xfc
  73. #define DW_IC_COMP_TYPE_VALUE 0x44570140
  74. #define DW_IC_INTR_RX_UNDER 0x001
  75. #define DW_IC_INTR_RX_OVER 0x002
  76. #define DW_IC_INTR_RX_FULL 0x004
  77. #define DW_IC_INTR_TX_OVER 0x008
  78. #define DW_IC_INTR_TX_EMPTY 0x010
  79. #define DW_IC_INTR_RD_REQ 0x020
  80. #define DW_IC_INTR_TX_ABRT 0x040
  81. #define DW_IC_INTR_RX_DONE 0x080
  82. #define DW_IC_INTR_ACTIVITY 0x100
  83. #define DW_IC_INTR_STOP_DET 0x200
  84. #define DW_IC_INTR_START_DET 0x400
  85. #define DW_IC_INTR_GEN_CALL 0x800
  86. #define DW_IC_INTR_DEFAULT_MASK (DW_IC_INTR_RX_FULL | \
  87. DW_IC_INTR_TX_EMPTY | \
  88. DW_IC_INTR_TX_ABRT | \
  89. DW_IC_INTR_STOP_DET)
  90. #define DW_IC_STATUS_ACTIVITY 0x1
  91. #define DW_IC_SDA_HOLD_RX_SHIFT 16
  92. #define DW_IC_SDA_HOLD_RX_MASK GENMASK(23, DW_IC_SDA_HOLD_RX_SHIFT)
  93. #define DW_IC_ERR_TX_ABRT 0x1
  94. #define DW_IC_TAR_10BITADDR_MASTER BIT(12)
  95. #define DW_IC_COMP_PARAM_1_SPEED_MODE_HIGH (BIT(2) | BIT(3))
  96. #define DW_IC_COMP_PARAM_1_SPEED_MODE_MASK GENMASK(3, 2)
  97. /*
  98. * status codes
  99. */
  100. #define STATUS_IDLE 0x0
  101. #define STATUS_WRITE_IN_PROGRESS 0x1
  102. #define STATUS_READ_IN_PROGRESS 0x2
  103. #define TIMEOUT 20 /* ms */
  104. /*
  105. * hardware abort codes from the DW_IC_TX_ABRT_SOURCE register
  106. *
  107. * only expected abort codes are listed here
  108. * refer to the datasheet for the full list
  109. */
  110. #define ABRT_7B_ADDR_NOACK 0
  111. #define ABRT_10ADDR1_NOACK 1
  112. #define ABRT_10ADDR2_NOACK 2
  113. #define ABRT_TXDATA_NOACK 3
  114. #define ABRT_GCALL_NOACK 4
  115. #define ABRT_GCALL_READ 5
  116. #define ABRT_SBYTE_ACKDET 7
  117. #define ABRT_SBYTE_NORSTRT 9
  118. #define ABRT_10B_RD_NORSTRT 10
  119. #define ABRT_MASTER_DIS 11
  120. #define ARB_LOST 12
  121. #define DW_IC_TX_ABRT_7B_ADDR_NOACK (1UL << ABRT_7B_ADDR_NOACK)
  122. #define DW_IC_TX_ABRT_10ADDR1_NOACK (1UL << ABRT_10ADDR1_NOACK)
  123. #define DW_IC_TX_ABRT_10ADDR2_NOACK (1UL << ABRT_10ADDR2_NOACK)
  124. #define DW_IC_TX_ABRT_TXDATA_NOACK (1UL << ABRT_TXDATA_NOACK)
  125. #define DW_IC_TX_ABRT_GCALL_NOACK (1UL << ABRT_GCALL_NOACK)
  126. #define DW_IC_TX_ABRT_GCALL_READ (1UL << ABRT_GCALL_READ)
  127. #define DW_IC_TX_ABRT_SBYTE_ACKDET (1UL << ABRT_SBYTE_ACKDET)
  128. #define DW_IC_TX_ABRT_SBYTE_NORSTRT (1UL << ABRT_SBYTE_NORSTRT)
  129. #define DW_IC_TX_ABRT_10B_RD_NORSTRT (1UL << ABRT_10B_RD_NORSTRT)
  130. #define DW_IC_TX_ABRT_MASTER_DIS (1UL << ABRT_MASTER_DIS)
  131. #define DW_IC_TX_ARB_LOST (1UL << ARB_LOST)
  132. #define DW_IC_TX_ABRT_NOACK (DW_IC_TX_ABRT_7B_ADDR_NOACK | \
  133. DW_IC_TX_ABRT_10ADDR1_NOACK | \
  134. DW_IC_TX_ABRT_10ADDR2_NOACK | \
  135. DW_IC_TX_ABRT_TXDATA_NOACK | \
  136. DW_IC_TX_ABRT_GCALL_NOACK)
  137. static char *abort_sources[] = {
  138. [ABRT_7B_ADDR_NOACK] =
  139. "slave address not acknowledged (7bit mode)",
  140. [ABRT_10ADDR1_NOACK] =
  141. "first address byte not acknowledged (10bit mode)",
  142. [ABRT_10ADDR2_NOACK] =
  143. "second address byte not acknowledged (10bit mode)",
  144. [ABRT_TXDATA_NOACK] =
  145. "data not acknowledged",
  146. [ABRT_GCALL_NOACK] =
  147. "no acknowledgement for a general call",
  148. [ABRT_GCALL_READ] =
  149. "read after general call",
  150. [ABRT_SBYTE_ACKDET] =
  151. "start byte acknowledged",
  152. [ABRT_SBYTE_NORSTRT] =
  153. "trying to send start byte when restart is disabled",
  154. [ABRT_10B_RD_NORSTRT] =
  155. "trying to read when restart is disabled (10bit mode)",
  156. [ABRT_MASTER_DIS] =
  157. "trying to use disabled adapter",
  158. [ARB_LOST] =
  159. "lost arbitration",
  160. };
  161. static u32 dw_readl(struct dw_i2c_dev *dev, int offset)
  162. {
  163. u32 value;
  164. if (dev->flags & ACCESS_16BIT)
  165. value = readw_relaxed(dev->base + offset) |
  166. (readw_relaxed(dev->base + offset + 2) << 16);
  167. else
  168. value = readl_relaxed(dev->base + offset);
  169. if (dev->flags & ACCESS_SWAP)
  170. return swab32(value);
  171. else
  172. return value;
  173. }
  174. static void dw_writel(struct dw_i2c_dev *dev, u32 b, int offset)
  175. {
  176. if (dev->flags & ACCESS_SWAP)
  177. b = swab32(b);
  178. if (dev->flags & ACCESS_16BIT) {
  179. writew_relaxed((u16)b, dev->base + offset);
  180. writew_relaxed((u16)(b >> 16), dev->base + offset + 2);
  181. } else {
  182. writel_relaxed(b, dev->base + offset);
  183. }
  184. }
  185. static u32
  186. i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset)
  187. {
  188. /*
  189. * DesignWare I2C core doesn't seem to have solid strategy to meet
  190. * the tHD;STA timing spec. Configuring _HCNT based on tHIGH spec
  191. * will result in violation of the tHD;STA spec.
  192. */
  193. if (cond)
  194. /*
  195. * Conditional expression:
  196. *
  197. * IC_[FS]S_SCL_HCNT + (1+4+3) >= IC_CLK * tHIGH
  198. *
  199. * This is based on the DW manuals, and represents an ideal
  200. * configuration. The resulting I2C bus speed will be
  201. * faster than any of the others.
  202. *
  203. * If your hardware is free from tHD;STA issue, try this one.
  204. */
  205. return (ic_clk * tSYMBOL + 500000) / 1000000 - 8 + offset;
  206. else
  207. /*
  208. * Conditional expression:
  209. *
  210. * IC_[FS]S_SCL_HCNT + 3 >= IC_CLK * (tHD;STA + tf)
  211. *
  212. * This is just experimental rule; the tHD;STA period turned
  213. * out to be proportinal to (_HCNT + 3). With this setting,
  214. * we could meet both tHIGH and tHD;STA timing specs.
  215. *
  216. * If unsure, you'd better to take this alternative.
  217. *
  218. * The reason why we need to take into account "tf" here,
  219. * is the same as described in i2c_dw_scl_lcnt().
  220. */
  221. return (ic_clk * (tSYMBOL + tf) + 500000) / 1000000
  222. - 3 + offset;
  223. }
  224. static u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset)
  225. {
  226. /*
  227. * Conditional expression:
  228. *
  229. * IC_[FS]S_SCL_LCNT + 1 >= IC_CLK * (tLOW + tf)
  230. *
  231. * DW I2C core starts counting the SCL CNTs for the LOW period
  232. * of the SCL clock (tLOW) as soon as it pulls the SCL line.
  233. * In order to meet the tLOW timing spec, we need to take into
  234. * account the fall time of SCL signal (tf). Default tf value
  235. * should be 0.3 us, for safety.
  236. */
  237. return ((ic_clk * (tLOW + tf) + 500000) / 1000000) - 1 + offset;
  238. }
  239. static void __i2c_dw_enable(struct dw_i2c_dev *dev, bool enable)
  240. {
  241. dw_writel(dev, enable, DW_IC_ENABLE);
  242. }
  243. static void __i2c_dw_enable_and_wait(struct dw_i2c_dev *dev, bool enable)
  244. {
  245. int timeout = 100;
  246. do {
  247. __i2c_dw_enable(dev, enable);
  248. if ((dw_readl(dev, DW_IC_ENABLE_STATUS) & 1) == enable)
  249. return;
  250. /*
  251. * Wait 10 times the signaling period of the highest I2C
  252. * transfer supported by the driver (for 400KHz this is
  253. * 25us) as described in the DesignWare I2C databook.
  254. */
  255. usleep_range(25, 250);
  256. } while (timeout--);
  257. dev_warn(dev->dev, "timeout in %sabling adapter\n",
  258. enable ? "en" : "dis");
  259. }
  260. static unsigned long i2c_dw_clk_rate(struct dw_i2c_dev *dev)
  261. {
  262. /*
  263. * Clock is not necessary if we got LCNT/HCNT values directly from
  264. * the platform code.
  265. */
  266. if (WARN_ON_ONCE(!dev->get_clk_rate_khz))
  267. return 0;
  268. return dev->get_clk_rate_khz(dev);
  269. }
  270. static int i2c_dw_acquire_lock(struct dw_i2c_dev *dev)
  271. {
  272. int ret;
  273. if (!dev->acquire_lock)
  274. return 0;
  275. ret = dev->acquire_lock(dev);
  276. if (!ret)
  277. return 0;
  278. dev_err(dev->dev, "couldn't acquire bus ownership\n");
  279. return ret;
  280. }
  281. static void i2c_dw_release_lock(struct dw_i2c_dev *dev)
  282. {
  283. if (dev->release_lock)
  284. dev->release_lock(dev);
  285. }
  286. /**
  287. * i2c_dw_init() - initialize the designware i2c master hardware
  288. * @dev: device private data
  289. *
  290. * This functions configures and enables the I2C master.
  291. * This function is called during I2C init function, and in case of timeout at
  292. * run time.
  293. */
  294. int i2c_dw_init(struct dw_i2c_dev *dev)
  295. {
  296. u32 hcnt, lcnt;
  297. u32 reg, comp_param1;
  298. u32 sda_falling_time, scl_falling_time;
  299. int ret;
  300. ret = i2c_dw_acquire_lock(dev);
  301. if (ret)
  302. return ret;
  303. reg = dw_readl(dev, DW_IC_COMP_TYPE);
  304. if (reg == ___constant_swab32(DW_IC_COMP_TYPE_VALUE)) {
  305. /* Configure register endianess access */
  306. dev->flags |= ACCESS_SWAP;
  307. } else if (reg == (DW_IC_COMP_TYPE_VALUE & 0x0000ffff)) {
  308. /* Configure register access mode 16bit */
  309. dev->flags |= ACCESS_16BIT;
  310. } else if (reg != DW_IC_COMP_TYPE_VALUE) {
  311. dev_err(dev->dev, "Unknown Synopsys component type: "
  312. "0x%08x\n", reg);
  313. i2c_dw_release_lock(dev);
  314. return -ENODEV;
  315. }
  316. comp_param1 = dw_readl(dev, DW_IC_COMP_PARAM_1);
  317. /* Disable the adapter */
  318. __i2c_dw_enable_and_wait(dev, false);
  319. /* set standard and fast speed deviders for high/low periods */
  320. sda_falling_time = dev->sda_falling_time ?: 300; /* ns */
  321. scl_falling_time = dev->scl_falling_time ?: 300; /* ns */
  322. /* Set SCL timing parameters for standard-mode */
  323. if (dev->ss_hcnt && dev->ss_lcnt) {
  324. hcnt = dev->ss_hcnt;
  325. lcnt = dev->ss_lcnt;
  326. } else {
  327. hcnt = i2c_dw_scl_hcnt(i2c_dw_clk_rate(dev),
  328. 4000, /* tHD;STA = tHIGH = 4.0 us */
  329. sda_falling_time,
  330. 0, /* 0: DW default, 1: Ideal */
  331. 0); /* No offset */
  332. lcnt = i2c_dw_scl_lcnt(i2c_dw_clk_rate(dev),
  333. 4700, /* tLOW = 4.7 us */
  334. scl_falling_time,
  335. 0); /* No offset */
  336. }
  337. dw_writel(dev, hcnt, DW_IC_SS_SCL_HCNT);
  338. dw_writel(dev, lcnt, DW_IC_SS_SCL_LCNT);
  339. dev_dbg(dev->dev, "Standard-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
  340. /* Set SCL timing parameters for fast-mode or fast-mode plus */
  341. if ((dev->clk_freq == 1000000) && dev->fp_hcnt && dev->fp_lcnt) {
  342. hcnt = dev->fp_hcnt;
  343. lcnt = dev->fp_lcnt;
  344. } else if (dev->fs_hcnt && dev->fs_lcnt) {
  345. hcnt = dev->fs_hcnt;
  346. lcnt = dev->fs_lcnt;
  347. } else {
  348. hcnt = i2c_dw_scl_hcnt(i2c_dw_clk_rate(dev),
  349. 600, /* tHD;STA = tHIGH = 0.6 us */
  350. sda_falling_time,
  351. 0, /* 0: DW default, 1: Ideal */
  352. 0); /* No offset */
  353. lcnt = i2c_dw_scl_lcnt(i2c_dw_clk_rate(dev),
  354. 1300, /* tLOW = 1.3 us */
  355. scl_falling_time,
  356. 0); /* No offset */
  357. }
  358. dw_writel(dev, hcnt, DW_IC_FS_SCL_HCNT);
  359. dw_writel(dev, lcnt, DW_IC_FS_SCL_LCNT);
  360. dev_dbg(dev->dev, "Fast-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
  361. if ((dev->master_cfg & DW_IC_CON_SPEED_MASK) ==
  362. DW_IC_CON_SPEED_HIGH) {
  363. if ((comp_param1 & DW_IC_COMP_PARAM_1_SPEED_MODE_MASK)
  364. != DW_IC_COMP_PARAM_1_SPEED_MODE_HIGH) {
  365. dev_err(dev->dev, "High Speed not supported!\n");
  366. dev->master_cfg &= ~DW_IC_CON_SPEED_MASK;
  367. dev->master_cfg |= DW_IC_CON_SPEED_FAST;
  368. } else if (dev->hs_hcnt && dev->hs_lcnt) {
  369. hcnt = dev->hs_hcnt;
  370. lcnt = dev->hs_lcnt;
  371. dw_writel(dev, hcnt, DW_IC_HS_SCL_HCNT);
  372. dw_writel(dev, lcnt, DW_IC_HS_SCL_LCNT);
  373. dev_dbg(dev->dev, "HighSpeed-mode HCNT:LCNT = %d:%d\n",
  374. hcnt, lcnt);
  375. }
  376. }
  377. /* Configure SDA Hold Time if required */
  378. reg = dw_readl(dev, DW_IC_COMP_VERSION);
  379. if (reg >= DW_IC_SDA_HOLD_MIN_VERS) {
  380. if (!dev->sda_hold_time) {
  381. /* Keep previous hold time setting if no one set it */
  382. dev->sda_hold_time = dw_readl(dev, DW_IC_SDA_HOLD);
  383. }
  384. /*
  385. * Workaround for avoiding TX arbitration lost in case I2C
  386. * slave pulls SDA down "too quickly" after falling egde of
  387. * SCL by enabling non-zero SDA RX hold. Specification says it
  388. * extends incoming SDA low to high transition while SCL is
  389. * high but it apprears to help also above issue.
  390. */
  391. if (!(dev->sda_hold_time & DW_IC_SDA_HOLD_RX_MASK))
  392. dev->sda_hold_time |= 1 << DW_IC_SDA_HOLD_RX_SHIFT;
  393. dw_writel(dev, dev->sda_hold_time, DW_IC_SDA_HOLD);
  394. } else {
  395. dev_warn(dev->dev,
  396. "Hardware too old to adjust SDA hold time.\n");
  397. }
  398. /* Configure Tx/Rx FIFO threshold levels */
  399. dw_writel(dev, dev->tx_fifo_depth / 2, DW_IC_TX_TL);
  400. dw_writel(dev, 0, DW_IC_RX_TL);
  401. /* configure the i2c master */
  402. dw_writel(dev, dev->master_cfg , DW_IC_CON);
  403. i2c_dw_release_lock(dev);
  404. return 0;
  405. }
  406. EXPORT_SYMBOL_GPL(i2c_dw_init);
  407. /*
  408. * Waiting for bus not busy
  409. */
  410. static int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev)
  411. {
  412. int timeout = TIMEOUT;
  413. while (dw_readl(dev, DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) {
  414. if (timeout <= 0) {
  415. dev_warn(dev->dev, "timeout waiting for bus ready\n");
  416. return -ETIMEDOUT;
  417. }
  418. timeout--;
  419. usleep_range(1000, 1100);
  420. }
  421. return 0;
  422. }
  423. static void i2c_dw_xfer_init(struct dw_i2c_dev *dev)
  424. {
  425. struct i2c_msg *msgs = dev->msgs;
  426. u32 ic_con, ic_tar = 0;
  427. /* Disable the adapter */
  428. __i2c_dw_enable_and_wait(dev, false);
  429. /* if the slave address is ten bit address, enable 10BITADDR */
  430. ic_con = dw_readl(dev, DW_IC_CON);
  431. if (msgs[dev->msg_write_idx].flags & I2C_M_TEN) {
  432. ic_con |= DW_IC_CON_10BITADDR_MASTER;
  433. /*
  434. * If I2C_DYNAMIC_TAR_UPDATE is set, the 10-bit addressing
  435. * mode has to be enabled via bit 12 of IC_TAR register.
  436. * We set it always as I2C_DYNAMIC_TAR_UPDATE can't be
  437. * detected from registers.
  438. */
  439. ic_tar = DW_IC_TAR_10BITADDR_MASTER;
  440. } else {
  441. ic_con &= ~DW_IC_CON_10BITADDR_MASTER;
  442. }
  443. dw_writel(dev, ic_con, DW_IC_CON);
  444. /*
  445. * Set the slave (target) address and enable 10-bit addressing mode
  446. * if applicable.
  447. */
  448. dw_writel(dev, msgs[dev->msg_write_idx].addr | ic_tar, DW_IC_TAR);
  449. /* enforce disabled interrupts (due to HW issues) */
  450. i2c_dw_disable_int(dev);
  451. /* Enable the adapter */
  452. __i2c_dw_enable(dev, true);
  453. /* Clear and enable interrupts */
  454. dw_readl(dev, DW_IC_CLR_INTR);
  455. dw_writel(dev, DW_IC_INTR_DEFAULT_MASK, DW_IC_INTR_MASK);
  456. }
  457. /*
  458. * Initiate (and continue) low level master read/write transaction.
  459. * This function is only called from i2c_dw_isr, and pumping i2c_msg
  460. * messages into the tx buffer. Even if the size of i2c_msg data is
  461. * longer than the size of the tx buffer, it handles everything.
  462. */
  463. static void
  464. i2c_dw_xfer_msg(struct dw_i2c_dev *dev)
  465. {
  466. struct i2c_msg *msgs = dev->msgs;
  467. u32 intr_mask;
  468. int tx_limit, rx_limit;
  469. u32 addr = msgs[dev->msg_write_idx].addr;
  470. u32 buf_len = dev->tx_buf_len;
  471. u8 *buf = dev->tx_buf;
  472. bool need_restart = false;
  473. intr_mask = DW_IC_INTR_DEFAULT_MASK;
  474. for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) {
  475. u32 flags = msgs[dev->msg_write_idx].flags;
  476. /*
  477. * if target address has changed, we need to
  478. * reprogram the target address in the i2c
  479. * adapter when we are done with this transfer
  480. */
  481. if (msgs[dev->msg_write_idx].addr != addr) {
  482. dev_err(dev->dev,
  483. "%s: invalid target address\n", __func__);
  484. dev->msg_err = -EINVAL;
  485. break;
  486. }
  487. if (msgs[dev->msg_write_idx].len == 0) {
  488. dev_err(dev->dev,
  489. "%s: invalid message length\n", __func__);
  490. dev->msg_err = -EINVAL;
  491. break;
  492. }
  493. if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {
  494. /* new i2c_msg */
  495. buf = msgs[dev->msg_write_idx].buf;
  496. buf_len = msgs[dev->msg_write_idx].len;
  497. /* If both IC_EMPTYFIFO_HOLD_MASTER_EN and
  498. * IC_RESTART_EN are set, we must manually
  499. * set restart bit between messages.
  500. */
  501. if ((dev->master_cfg & DW_IC_CON_RESTART_EN) &&
  502. (dev->msg_write_idx > 0))
  503. need_restart = true;
  504. }
  505. tx_limit = dev->tx_fifo_depth - dw_readl(dev, DW_IC_TXFLR);
  506. rx_limit = dev->rx_fifo_depth - dw_readl(dev, DW_IC_RXFLR);
  507. while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) {
  508. u32 cmd = 0;
  509. /*
  510. * If IC_EMPTYFIFO_HOLD_MASTER_EN is set we must
  511. * manually set the stop bit. However, it cannot be
  512. * detected from the registers so we set it always
  513. * when writing/reading the last byte.
  514. */
  515. /*
  516. * i2c-core.c always sets the buffer length of
  517. * I2C_FUNC_SMBUS_BLOCK_DATA to 1. The length will
  518. * be adjusted when receiving the first byte.
  519. * Thus we can't stop the transaction here.
  520. */
  521. if (dev->msg_write_idx == dev->msgs_num - 1 &&
  522. buf_len == 1 && !(flags & I2C_M_RECV_LEN))
  523. cmd |= BIT(9);
  524. if (need_restart) {
  525. cmd |= BIT(10);
  526. need_restart = false;
  527. }
  528. if (msgs[dev->msg_write_idx].flags & I2C_M_RD) {
  529. /* avoid rx buffer overrun */
  530. if (dev->rx_outstanding >= dev->rx_fifo_depth)
  531. break;
  532. dw_writel(dev, cmd | 0x100, DW_IC_DATA_CMD);
  533. rx_limit--;
  534. dev->rx_outstanding++;
  535. } else
  536. dw_writel(dev, cmd | *buf++, DW_IC_DATA_CMD);
  537. tx_limit--; buf_len--;
  538. }
  539. dev->tx_buf = buf;
  540. dev->tx_buf_len = buf_len;
  541. /*
  542. * Because we don't know the buffer length in the
  543. * I2C_FUNC_SMBUS_BLOCK_DATA case, we can't stop
  544. * the transaction here.
  545. */
  546. if (buf_len > 0 || flags & I2C_M_RECV_LEN) {
  547. /* more bytes to be written */
  548. dev->status |= STATUS_WRITE_IN_PROGRESS;
  549. break;
  550. } else
  551. dev->status &= ~STATUS_WRITE_IN_PROGRESS;
  552. }
  553. /*
  554. * If i2c_msg index search is completed, we don't need TX_EMPTY
  555. * interrupt any more.
  556. */
  557. if (dev->msg_write_idx == dev->msgs_num)
  558. intr_mask &= ~DW_IC_INTR_TX_EMPTY;
  559. if (dev->msg_err)
  560. intr_mask = 0;
  561. dw_writel(dev, intr_mask, DW_IC_INTR_MASK);
  562. }
  563. static u8
  564. i2c_dw_recv_len(struct dw_i2c_dev *dev, u8 len)
  565. {
  566. struct i2c_msg *msgs = dev->msgs;
  567. u32 flags = msgs[dev->msg_read_idx].flags;
  568. /*
  569. * Adjust the buffer length and mask the flag
  570. * after receiving the first byte.
  571. */
  572. len += (flags & I2C_CLIENT_PEC) ? 2 : 1;
  573. dev->tx_buf_len = len - min_t(u8, len, dev->rx_outstanding);
  574. msgs[dev->msg_read_idx].len = len;
  575. msgs[dev->msg_read_idx].flags &= ~I2C_M_RECV_LEN;
  576. return len;
  577. }
  578. static void
  579. i2c_dw_read(struct dw_i2c_dev *dev)
  580. {
  581. struct i2c_msg *msgs = dev->msgs;
  582. int rx_valid;
  583. for (; dev->msg_read_idx < dev->msgs_num; dev->msg_read_idx++) {
  584. u32 len;
  585. u8 *buf;
  586. if (!(msgs[dev->msg_read_idx].flags & I2C_M_RD))
  587. continue;
  588. if (!(dev->status & STATUS_READ_IN_PROGRESS)) {
  589. len = msgs[dev->msg_read_idx].len;
  590. buf = msgs[dev->msg_read_idx].buf;
  591. } else {
  592. len = dev->rx_buf_len;
  593. buf = dev->rx_buf;
  594. }
  595. rx_valid = dw_readl(dev, DW_IC_RXFLR);
  596. for (; len > 0 && rx_valid > 0; len--, rx_valid--) {
  597. u32 flags = msgs[dev->msg_read_idx].flags;
  598. *buf = dw_readl(dev, DW_IC_DATA_CMD);
  599. /* Ensure length byte is a valid value */
  600. if (flags & I2C_M_RECV_LEN &&
  601. *buf <= I2C_SMBUS_BLOCK_MAX && *buf > 0) {
  602. len = i2c_dw_recv_len(dev, *buf);
  603. }
  604. buf++;
  605. dev->rx_outstanding--;
  606. }
  607. if (len > 0) {
  608. dev->status |= STATUS_READ_IN_PROGRESS;
  609. dev->rx_buf_len = len;
  610. dev->rx_buf = buf;
  611. return;
  612. } else
  613. dev->status &= ~STATUS_READ_IN_PROGRESS;
  614. }
  615. }
  616. static int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev)
  617. {
  618. unsigned long abort_source = dev->abort_source;
  619. int i;
  620. if (abort_source & DW_IC_TX_ABRT_NOACK) {
  621. for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  622. dev_dbg(dev->dev,
  623. "%s: %s\n", __func__, abort_sources[i]);
  624. return -EREMOTEIO;
  625. }
  626. for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  627. dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]);
  628. if (abort_source & DW_IC_TX_ARB_LOST)
  629. return -EAGAIN;
  630. else if (abort_source & DW_IC_TX_ABRT_GCALL_READ)
  631. return -EINVAL; /* wrong msgs[] data */
  632. else
  633. return -EIO;
  634. }
  635. /*
  636. * Prepare controller for a transaction and call i2c_dw_xfer_msg
  637. */
  638. static int
  639. i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  640. {
  641. struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
  642. int ret;
  643. dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
  644. pm_runtime_get_sync(dev->dev);
  645. reinit_completion(&dev->cmd_complete);
  646. dev->msgs = msgs;
  647. dev->msgs_num = num;
  648. dev->cmd_err = 0;
  649. dev->msg_write_idx = 0;
  650. dev->msg_read_idx = 0;
  651. dev->msg_err = 0;
  652. dev->status = STATUS_IDLE;
  653. dev->abort_source = 0;
  654. dev->rx_outstanding = 0;
  655. ret = i2c_dw_acquire_lock(dev);
  656. if (ret)
  657. goto done_nolock;
  658. ret = i2c_dw_wait_bus_not_busy(dev);
  659. if (ret < 0)
  660. goto done;
  661. /* start the transfers */
  662. i2c_dw_xfer_init(dev);
  663. /* wait for tx to complete */
  664. if (!wait_for_completion_timeout(&dev->cmd_complete, adap->timeout)) {
  665. dev_err(dev->dev, "controller timed out\n");
  666. /* i2c_dw_init implicitly disables the adapter */
  667. i2c_dw_init(dev);
  668. ret = -ETIMEDOUT;
  669. goto done;
  670. }
  671. /*
  672. * We must disable the adapter before returning and signaling the end
  673. * of the current transfer. Otherwise the hardware might continue
  674. * generating interrupts which in turn causes a race condition with
  675. * the following transfer. Needs some more investigation if the
  676. * additional interrupts are a hardware bug or this driver doesn't
  677. * handle them correctly yet.
  678. */
  679. __i2c_dw_enable(dev, false);
  680. if (dev->msg_err) {
  681. ret = dev->msg_err;
  682. goto done;
  683. }
  684. /* no error */
  685. if (likely(!dev->cmd_err && !dev->status)) {
  686. ret = num;
  687. goto done;
  688. }
  689. /* We have an error */
  690. if (dev->cmd_err == DW_IC_ERR_TX_ABRT) {
  691. ret = i2c_dw_handle_tx_abort(dev);
  692. goto done;
  693. }
  694. if (dev->status)
  695. dev_err(dev->dev,
  696. "transfer terminated early - interrupt latency too high?\n");
  697. ret = -EIO;
  698. done:
  699. i2c_dw_release_lock(dev);
  700. done_nolock:
  701. pm_runtime_mark_last_busy(dev->dev);
  702. pm_runtime_put_autosuspend(dev->dev);
  703. return ret;
  704. }
  705. static u32 i2c_dw_func(struct i2c_adapter *adap)
  706. {
  707. struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
  708. return dev->functionality;
  709. }
  710. static const struct i2c_algorithm i2c_dw_algo = {
  711. .master_xfer = i2c_dw_xfer,
  712. .functionality = i2c_dw_func,
  713. };
  714. static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev)
  715. {
  716. u32 stat;
  717. /*
  718. * The IC_INTR_STAT register just indicates "enabled" interrupts.
  719. * Ths unmasked raw version of interrupt status bits are available
  720. * in the IC_RAW_INTR_STAT register.
  721. *
  722. * That is,
  723. * stat = dw_readl(IC_INTR_STAT);
  724. * equals to,
  725. * stat = dw_readl(IC_RAW_INTR_STAT) & dw_readl(IC_INTR_MASK);
  726. *
  727. * The raw version might be useful for debugging purposes.
  728. */
  729. stat = dw_readl(dev, DW_IC_INTR_STAT);
  730. /*
  731. * Do not use the IC_CLR_INTR register to clear interrupts, or
  732. * you'll miss some interrupts, triggered during the period from
  733. * dw_readl(IC_INTR_STAT) to dw_readl(IC_CLR_INTR).
  734. *
  735. * Instead, use the separately-prepared IC_CLR_* registers.
  736. */
  737. if (stat & DW_IC_INTR_RX_UNDER)
  738. dw_readl(dev, DW_IC_CLR_RX_UNDER);
  739. if (stat & DW_IC_INTR_RX_OVER)
  740. dw_readl(dev, DW_IC_CLR_RX_OVER);
  741. if (stat & DW_IC_INTR_TX_OVER)
  742. dw_readl(dev, DW_IC_CLR_TX_OVER);
  743. if (stat & DW_IC_INTR_RD_REQ)
  744. dw_readl(dev, DW_IC_CLR_RD_REQ);
  745. if (stat & DW_IC_INTR_TX_ABRT) {
  746. /*
  747. * The IC_TX_ABRT_SOURCE register is cleared whenever
  748. * the IC_CLR_TX_ABRT is read. Preserve it beforehand.
  749. */
  750. dev->abort_source = dw_readl(dev, DW_IC_TX_ABRT_SOURCE);
  751. dw_readl(dev, DW_IC_CLR_TX_ABRT);
  752. }
  753. if (stat & DW_IC_INTR_RX_DONE)
  754. dw_readl(dev, DW_IC_CLR_RX_DONE);
  755. if (stat & DW_IC_INTR_ACTIVITY)
  756. dw_readl(dev, DW_IC_CLR_ACTIVITY);
  757. if (stat & DW_IC_INTR_STOP_DET)
  758. dw_readl(dev, DW_IC_CLR_STOP_DET);
  759. if (stat & DW_IC_INTR_START_DET)
  760. dw_readl(dev, DW_IC_CLR_START_DET);
  761. if (stat & DW_IC_INTR_GEN_CALL)
  762. dw_readl(dev, DW_IC_CLR_GEN_CALL);
  763. return stat;
  764. }
  765. /*
  766. * Interrupt service routine. This gets called whenever an I2C interrupt
  767. * occurs.
  768. */
  769. static irqreturn_t i2c_dw_isr(int this_irq, void *dev_id)
  770. {
  771. struct dw_i2c_dev *dev = dev_id;
  772. u32 stat, enabled;
  773. enabled = dw_readl(dev, DW_IC_ENABLE);
  774. stat = dw_readl(dev, DW_IC_RAW_INTR_STAT);
  775. dev_dbg(dev->dev, "%s: enabled=%#x stat=%#x\n", __func__, enabled, stat);
  776. if (!enabled || !(stat & ~DW_IC_INTR_ACTIVITY))
  777. return IRQ_NONE;
  778. stat = i2c_dw_read_clear_intrbits(dev);
  779. if (stat & DW_IC_INTR_TX_ABRT) {
  780. dev->cmd_err |= DW_IC_ERR_TX_ABRT;
  781. dev->status = STATUS_IDLE;
  782. /*
  783. * Anytime TX_ABRT is set, the contents of the tx/rx
  784. * buffers are flushed. Make sure to skip them.
  785. */
  786. dw_writel(dev, 0, DW_IC_INTR_MASK);
  787. goto tx_aborted;
  788. }
  789. if (stat & DW_IC_INTR_RX_FULL)
  790. i2c_dw_read(dev);
  791. if (stat & DW_IC_INTR_TX_EMPTY)
  792. i2c_dw_xfer_msg(dev);
  793. /*
  794. * No need to modify or disable the interrupt mask here.
  795. * i2c_dw_xfer_msg() will take care of it according to
  796. * the current transmit status.
  797. */
  798. tx_aborted:
  799. if ((stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET)) || dev->msg_err)
  800. complete(&dev->cmd_complete);
  801. else if (unlikely(dev->flags & ACCESS_INTR_MASK)) {
  802. /* workaround to trigger pending interrupt */
  803. stat = dw_readl(dev, DW_IC_INTR_MASK);
  804. i2c_dw_disable_int(dev);
  805. dw_writel(dev, stat, DW_IC_INTR_MASK);
  806. }
  807. return IRQ_HANDLED;
  808. }
  809. void i2c_dw_disable(struct dw_i2c_dev *dev)
  810. {
  811. /* Disable controller */
  812. __i2c_dw_enable_and_wait(dev, false);
  813. /* Disable all interupts */
  814. dw_writel(dev, 0, DW_IC_INTR_MASK);
  815. dw_readl(dev, DW_IC_CLR_INTR);
  816. }
  817. EXPORT_SYMBOL_GPL(i2c_dw_disable);
  818. void i2c_dw_disable_int(struct dw_i2c_dev *dev)
  819. {
  820. dw_writel(dev, 0, DW_IC_INTR_MASK);
  821. }
  822. EXPORT_SYMBOL_GPL(i2c_dw_disable_int);
  823. u32 i2c_dw_read_comp_param(struct dw_i2c_dev *dev)
  824. {
  825. return dw_readl(dev, DW_IC_COMP_PARAM_1);
  826. }
  827. EXPORT_SYMBOL_GPL(i2c_dw_read_comp_param);
  828. int i2c_dw_probe(struct dw_i2c_dev *dev)
  829. {
  830. struct i2c_adapter *adap = &dev->adapter;
  831. unsigned long irq_flags;
  832. int r;
  833. init_completion(&dev->cmd_complete);
  834. r = i2c_dw_init(dev);
  835. if (r)
  836. return r;
  837. snprintf(adap->name, sizeof(adap->name),
  838. "Synopsys DesignWare I2C adapter");
  839. adap->retries = 3;
  840. adap->algo = &i2c_dw_algo;
  841. adap->dev.parent = dev->dev;
  842. i2c_set_adapdata(adap, dev);
  843. if (dev->pm_disabled) {
  844. dev_pm_syscore_device(dev->dev, true);
  845. irq_flags = IRQF_NO_SUSPEND;
  846. } else {
  847. irq_flags = IRQF_SHARED | IRQF_COND_SUSPEND;
  848. }
  849. i2c_dw_disable_int(dev);
  850. r = devm_request_irq(dev->dev, dev->irq, i2c_dw_isr, irq_flags,
  851. dev_name(dev->dev), dev);
  852. if (r) {
  853. dev_err(dev->dev, "failure requesting irq %i: %d\n",
  854. dev->irq, r);
  855. return r;
  856. }
  857. /*
  858. * Increment PM usage count during adapter registration in order to
  859. * avoid possible spurious runtime suspend when adapter device is
  860. * registered to the device core and immediate resume in case bus has
  861. * registered I2C slaves that do I2C transfers in their probe.
  862. */
  863. pm_runtime_get_noresume(dev->dev);
  864. r = i2c_add_numbered_adapter(adap);
  865. if (r)
  866. dev_err(dev->dev, "failure adding adapter: %d\n", r);
  867. pm_runtime_put_noidle(dev->dev);
  868. return r;
  869. }
  870. EXPORT_SYMBOL_GPL(i2c_dw_probe);
  871. MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter core");
  872. MODULE_LICENSE("GPL");