i2c-designware-core.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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->accessor_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->accessor_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->accessor_flags & ACCESS_SWAP)
  177. b = swab32(b);
  178. if (dev->accessor_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->accessor_flags |= ACCESS_SWAP;
  307. } else if (reg == (DW_IC_COMP_TYPE_VALUE & 0x0000ffff)) {
  308. /* Configure register access mode 16bit */
  309. dev->accessor_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_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. if (dev->dynamic_tar_update_enabled) {
  431. /*
  432. * If I2C_DYNAMIC_TAR_UPDATE is set, the 10-bit addressing
  433. * mode has to be enabled via bit 12 of IC_TAR register,
  434. * otherwise bit 4 of IC_CON is used.
  435. */
  436. if (msgs[dev->msg_write_idx].flags & I2C_M_TEN)
  437. ic_tar = DW_IC_TAR_10BITADDR_MASTER;
  438. } else {
  439. u32 ic_con = dw_readl(dev, DW_IC_CON);
  440. if (msgs[dev->msg_write_idx].flags & I2C_M_TEN)
  441. ic_con |= DW_IC_CON_10BITADDR_MASTER;
  442. else
  443. ic_con &= ~DW_IC_CON_10BITADDR_MASTER;
  444. dw_writel(dev, ic_con, DW_IC_CON);
  445. }
  446. /*
  447. * Set the slave (target) address and enable 10-bit addressing mode
  448. * if applicable.
  449. */
  450. dw_writel(dev, msgs[dev->msg_write_idx].addr | ic_tar, DW_IC_TAR);
  451. /* enforce disabled interrupts (due to HW issues) */
  452. i2c_dw_disable_int(dev);
  453. /* Enable the adapter */
  454. __i2c_dw_enable(dev, true);
  455. /* Clear and enable interrupts */
  456. dw_readl(dev, DW_IC_CLR_INTR);
  457. dw_writel(dev, DW_IC_INTR_DEFAULT_MASK, DW_IC_INTR_MASK);
  458. }
  459. /*
  460. * Initiate (and continue) low level master read/write transaction.
  461. * This function is only called from i2c_dw_isr, and pumping i2c_msg
  462. * messages into the tx buffer. Even if the size of i2c_msg data is
  463. * longer than the size of the tx buffer, it handles everything.
  464. */
  465. static void
  466. i2c_dw_xfer_msg(struct dw_i2c_dev *dev)
  467. {
  468. struct i2c_msg *msgs = dev->msgs;
  469. u32 intr_mask;
  470. int tx_limit, rx_limit;
  471. u32 addr = msgs[dev->msg_write_idx].addr;
  472. u32 buf_len = dev->tx_buf_len;
  473. u8 *buf = dev->tx_buf;
  474. bool need_restart = false;
  475. intr_mask = DW_IC_INTR_DEFAULT_MASK;
  476. for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) {
  477. u32 flags = msgs[dev->msg_write_idx].flags;
  478. /*
  479. * if target address has changed, we need to
  480. * reprogram the target address in the i2c
  481. * adapter when we are done with this transfer
  482. */
  483. if (msgs[dev->msg_write_idx].addr != addr) {
  484. dev_err(dev->dev,
  485. "%s: invalid target address\n", __func__);
  486. dev->msg_err = -EINVAL;
  487. break;
  488. }
  489. if (msgs[dev->msg_write_idx].len == 0) {
  490. dev_err(dev->dev,
  491. "%s: invalid message length\n", __func__);
  492. dev->msg_err = -EINVAL;
  493. break;
  494. }
  495. if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {
  496. /* new i2c_msg */
  497. buf = msgs[dev->msg_write_idx].buf;
  498. buf_len = msgs[dev->msg_write_idx].len;
  499. /* If both IC_EMPTYFIFO_HOLD_MASTER_EN and
  500. * IC_RESTART_EN are set, we must manually
  501. * set restart bit between messages.
  502. */
  503. if ((dev->master_cfg & DW_IC_CON_RESTART_EN) &&
  504. (dev->msg_write_idx > 0))
  505. need_restart = true;
  506. }
  507. tx_limit = dev->tx_fifo_depth - dw_readl(dev, DW_IC_TXFLR);
  508. rx_limit = dev->rx_fifo_depth - dw_readl(dev, DW_IC_RXFLR);
  509. while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) {
  510. u32 cmd = 0;
  511. /*
  512. * If IC_EMPTYFIFO_HOLD_MASTER_EN is set we must
  513. * manually set the stop bit. However, it cannot be
  514. * detected from the registers so we set it always
  515. * when writing/reading the last byte.
  516. */
  517. /*
  518. * i2c-core.c always sets the buffer length of
  519. * I2C_FUNC_SMBUS_BLOCK_DATA to 1. The length will
  520. * be adjusted when receiving the first byte.
  521. * Thus we can't stop the transaction here.
  522. */
  523. if (dev->msg_write_idx == dev->msgs_num - 1 &&
  524. buf_len == 1 && !(flags & I2C_M_RECV_LEN))
  525. cmd |= BIT(9);
  526. if (need_restart) {
  527. cmd |= BIT(10);
  528. need_restart = false;
  529. }
  530. if (msgs[dev->msg_write_idx].flags & I2C_M_RD) {
  531. /* avoid rx buffer overrun */
  532. if (dev->rx_outstanding >= dev->rx_fifo_depth)
  533. break;
  534. dw_writel(dev, cmd | 0x100, DW_IC_DATA_CMD);
  535. rx_limit--;
  536. dev->rx_outstanding++;
  537. } else
  538. dw_writel(dev, cmd | *buf++, DW_IC_DATA_CMD);
  539. tx_limit--; buf_len--;
  540. }
  541. dev->tx_buf = buf;
  542. dev->tx_buf_len = buf_len;
  543. /*
  544. * Because we don't know the buffer length in the
  545. * I2C_FUNC_SMBUS_BLOCK_DATA case, we can't stop
  546. * the transaction here.
  547. */
  548. if (buf_len > 0 || flags & I2C_M_RECV_LEN) {
  549. /* more bytes to be written */
  550. dev->status |= STATUS_WRITE_IN_PROGRESS;
  551. break;
  552. } else
  553. dev->status &= ~STATUS_WRITE_IN_PROGRESS;
  554. }
  555. /*
  556. * If i2c_msg index search is completed, we don't need TX_EMPTY
  557. * interrupt any more.
  558. */
  559. if (dev->msg_write_idx == dev->msgs_num)
  560. intr_mask &= ~DW_IC_INTR_TX_EMPTY;
  561. if (dev->msg_err)
  562. intr_mask = 0;
  563. dw_writel(dev, intr_mask, DW_IC_INTR_MASK);
  564. }
  565. static u8
  566. i2c_dw_recv_len(struct dw_i2c_dev *dev, u8 len)
  567. {
  568. struct i2c_msg *msgs = dev->msgs;
  569. u32 flags = msgs[dev->msg_read_idx].flags;
  570. /*
  571. * Adjust the buffer length and mask the flag
  572. * after receiving the first byte.
  573. */
  574. len += (flags & I2C_CLIENT_PEC) ? 2 : 1;
  575. dev->tx_buf_len = len - min_t(u8, len, dev->rx_outstanding);
  576. msgs[dev->msg_read_idx].len = len;
  577. msgs[dev->msg_read_idx].flags &= ~I2C_M_RECV_LEN;
  578. return len;
  579. }
  580. static void
  581. i2c_dw_read(struct dw_i2c_dev *dev)
  582. {
  583. struct i2c_msg *msgs = dev->msgs;
  584. int rx_valid;
  585. for (; dev->msg_read_idx < dev->msgs_num; dev->msg_read_idx++) {
  586. u32 len;
  587. u8 *buf;
  588. if (!(msgs[dev->msg_read_idx].flags & I2C_M_RD))
  589. continue;
  590. if (!(dev->status & STATUS_READ_IN_PROGRESS)) {
  591. len = msgs[dev->msg_read_idx].len;
  592. buf = msgs[dev->msg_read_idx].buf;
  593. } else {
  594. len = dev->rx_buf_len;
  595. buf = dev->rx_buf;
  596. }
  597. rx_valid = dw_readl(dev, DW_IC_RXFLR);
  598. for (; len > 0 && rx_valid > 0; len--, rx_valid--) {
  599. u32 flags = msgs[dev->msg_read_idx].flags;
  600. *buf = dw_readl(dev, DW_IC_DATA_CMD);
  601. /* Ensure length byte is a valid value */
  602. if (flags & I2C_M_RECV_LEN &&
  603. *buf <= I2C_SMBUS_BLOCK_MAX && *buf > 0) {
  604. len = i2c_dw_recv_len(dev, *buf);
  605. }
  606. buf++;
  607. dev->rx_outstanding--;
  608. }
  609. if (len > 0) {
  610. dev->status |= STATUS_READ_IN_PROGRESS;
  611. dev->rx_buf_len = len;
  612. dev->rx_buf = buf;
  613. return;
  614. } else
  615. dev->status &= ~STATUS_READ_IN_PROGRESS;
  616. }
  617. }
  618. static int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev)
  619. {
  620. unsigned long abort_source = dev->abort_source;
  621. int i;
  622. if (abort_source & DW_IC_TX_ABRT_NOACK) {
  623. for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  624. dev_dbg(dev->dev,
  625. "%s: %s\n", __func__, abort_sources[i]);
  626. return -EREMOTEIO;
  627. }
  628. for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  629. dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]);
  630. if (abort_source & DW_IC_TX_ARB_LOST)
  631. return -EAGAIN;
  632. else if (abort_source & DW_IC_TX_ABRT_GCALL_READ)
  633. return -EINVAL; /* wrong msgs[] data */
  634. else
  635. return -EIO;
  636. }
  637. /*
  638. * Prepare controller for a transaction and call i2c_dw_xfer_msg
  639. */
  640. static int
  641. i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  642. {
  643. struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
  644. int ret;
  645. dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
  646. pm_runtime_get_sync(dev->dev);
  647. reinit_completion(&dev->cmd_complete);
  648. dev->msgs = msgs;
  649. dev->msgs_num = num;
  650. dev->cmd_err = 0;
  651. dev->msg_write_idx = 0;
  652. dev->msg_read_idx = 0;
  653. dev->msg_err = 0;
  654. dev->status = STATUS_IDLE;
  655. dev->abort_source = 0;
  656. dev->rx_outstanding = 0;
  657. ret = i2c_dw_acquire_lock(dev);
  658. if (ret)
  659. goto done_nolock;
  660. ret = i2c_dw_wait_bus_not_busy(dev);
  661. if (ret < 0)
  662. goto done;
  663. /* start the transfers */
  664. i2c_dw_xfer_init(dev);
  665. /* wait for tx to complete */
  666. if (!wait_for_completion_timeout(&dev->cmd_complete, adap->timeout)) {
  667. dev_err(dev->dev, "controller timed out\n");
  668. /* i2c_dw_init implicitly disables the adapter */
  669. i2c_dw_init(dev);
  670. ret = -ETIMEDOUT;
  671. goto done;
  672. }
  673. /*
  674. * We must disable the adapter before returning and signaling the end
  675. * of the current transfer. Otherwise the hardware might continue
  676. * generating interrupts which in turn causes a race condition with
  677. * the following transfer. Needs some more investigation if the
  678. * additional interrupts are a hardware bug or this driver doesn't
  679. * handle them correctly yet.
  680. */
  681. __i2c_dw_enable(dev, false);
  682. if (dev->msg_err) {
  683. ret = dev->msg_err;
  684. goto done;
  685. }
  686. /* no error */
  687. if (likely(!dev->cmd_err && !dev->status)) {
  688. ret = num;
  689. goto done;
  690. }
  691. /* We have an error */
  692. if (dev->cmd_err == DW_IC_ERR_TX_ABRT) {
  693. ret = i2c_dw_handle_tx_abort(dev);
  694. goto done;
  695. }
  696. if (dev->status)
  697. dev_err(dev->dev,
  698. "transfer terminated early - interrupt latency too high?\n");
  699. ret = -EIO;
  700. done:
  701. i2c_dw_release_lock(dev);
  702. done_nolock:
  703. pm_runtime_mark_last_busy(dev->dev);
  704. pm_runtime_put_autosuspend(dev->dev);
  705. return ret;
  706. }
  707. static u32 i2c_dw_func(struct i2c_adapter *adap)
  708. {
  709. struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
  710. return dev->functionality;
  711. }
  712. static struct i2c_algorithm i2c_dw_algo = {
  713. .master_xfer = i2c_dw_xfer,
  714. .functionality = i2c_dw_func,
  715. };
  716. static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev)
  717. {
  718. u32 stat;
  719. /*
  720. * The IC_INTR_STAT register just indicates "enabled" interrupts.
  721. * Ths unmasked raw version of interrupt status bits are available
  722. * in the IC_RAW_INTR_STAT register.
  723. *
  724. * That is,
  725. * stat = dw_readl(IC_INTR_STAT);
  726. * equals to,
  727. * stat = dw_readl(IC_RAW_INTR_STAT) & dw_readl(IC_INTR_MASK);
  728. *
  729. * The raw version might be useful for debugging purposes.
  730. */
  731. stat = dw_readl(dev, DW_IC_INTR_STAT);
  732. /*
  733. * Do not use the IC_CLR_INTR register to clear interrupts, or
  734. * you'll miss some interrupts, triggered during the period from
  735. * dw_readl(IC_INTR_STAT) to dw_readl(IC_CLR_INTR).
  736. *
  737. * Instead, use the separately-prepared IC_CLR_* registers.
  738. */
  739. if (stat & DW_IC_INTR_RX_UNDER)
  740. dw_readl(dev, DW_IC_CLR_RX_UNDER);
  741. if (stat & DW_IC_INTR_RX_OVER)
  742. dw_readl(dev, DW_IC_CLR_RX_OVER);
  743. if (stat & DW_IC_INTR_TX_OVER)
  744. dw_readl(dev, DW_IC_CLR_TX_OVER);
  745. if (stat & DW_IC_INTR_RD_REQ)
  746. dw_readl(dev, DW_IC_CLR_RD_REQ);
  747. if (stat & DW_IC_INTR_TX_ABRT) {
  748. /*
  749. * The IC_TX_ABRT_SOURCE register is cleared whenever
  750. * the IC_CLR_TX_ABRT is read. Preserve it beforehand.
  751. */
  752. dev->abort_source = dw_readl(dev, DW_IC_TX_ABRT_SOURCE);
  753. dw_readl(dev, DW_IC_CLR_TX_ABRT);
  754. }
  755. if (stat & DW_IC_INTR_RX_DONE)
  756. dw_readl(dev, DW_IC_CLR_RX_DONE);
  757. if (stat & DW_IC_INTR_ACTIVITY)
  758. dw_readl(dev, DW_IC_CLR_ACTIVITY);
  759. if (stat & DW_IC_INTR_STOP_DET)
  760. dw_readl(dev, DW_IC_CLR_STOP_DET);
  761. if (stat & DW_IC_INTR_START_DET)
  762. dw_readl(dev, DW_IC_CLR_START_DET);
  763. if (stat & DW_IC_INTR_GEN_CALL)
  764. dw_readl(dev, DW_IC_CLR_GEN_CALL);
  765. return stat;
  766. }
  767. /*
  768. * Interrupt service routine. This gets called whenever an I2C interrupt
  769. * occurs.
  770. */
  771. static irqreturn_t i2c_dw_isr(int this_irq, void *dev_id)
  772. {
  773. struct dw_i2c_dev *dev = dev_id;
  774. u32 stat, enabled;
  775. enabled = dw_readl(dev, DW_IC_ENABLE);
  776. stat = dw_readl(dev, DW_IC_RAW_INTR_STAT);
  777. dev_dbg(dev->dev, "%s: enabled=%#x stat=%#x\n", __func__, enabled, stat);
  778. if (!enabled || !(stat & ~DW_IC_INTR_ACTIVITY))
  779. return IRQ_NONE;
  780. stat = i2c_dw_read_clear_intrbits(dev);
  781. if (stat & DW_IC_INTR_TX_ABRT) {
  782. dev->cmd_err |= DW_IC_ERR_TX_ABRT;
  783. dev->status = STATUS_IDLE;
  784. /*
  785. * Anytime TX_ABRT is set, the contents of the tx/rx
  786. * buffers are flushed. Make sure to skip them.
  787. */
  788. dw_writel(dev, 0, DW_IC_INTR_MASK);
  789. goto tx_aborted;
  790. }
  791. if (stat & DW_IC_INTR_RX_FULL)
  792. i2c_dw_read(dev);
  793. if (stat & DW_IC_INTR_TX_EMPTY)
  794. i2c_dw_xfer_msg(dev);
  795. /*
  796. * No need to modify or disable the interrupt mask here.
  797. * i2c_dw_xfer_msg() will take care of it according to
  798. * the current transmit status.
  799. */
  800. tx_aborted:
  801. if ((stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET)) || dev->msg_err)
  802. complete(&dev->cmd_complete);
  803. else if (unlikely(dev->accessor_flags & ACCESS_INTR_MASK)) {
  804. /* workaround to trigger pending interrupt */
  805. stat = dw_readl(dev, DW_IC_INTR_MASK);
  806. i2c_dw_disable_int(dev);
  807. dw_writel(dev, stat, DW_IC_INTR_MASK);
  808. }
  809. return IRQ_HANDLED;
  810. }
  811. void i2c_dw_disable(struct dw_i2c_dev *dev)
  812. {
  813. /* Disable controller */
  814. __i2c_dw_enable_and_wait(dev, false);
  815. /* Disable all interupts */
  816. dw_writel(dev, 0, DW_IC_INTR_MASK);
  817. dw_readl(dev, DW_IC_CLR_INTR);
  818. }
  819. EXPORT_SYMBOL_GPL(i2c_dw_disable);
  820. void i2c_dw_disable_int(struct dw_i2c_dev *dev)
  821. {
  822. dw_writel(dev, 0, DW_IC_INTR_MASK);
  823. }
  824. EXPORT_SYMBOL_GPL(i2c_dw_disable_int);
  825. u32 i2c_dw_read_comp_param(struct dw_i2c_dev *dev)
  826. {
  827. return dw_readl(dev, DW_IC_COMP_PARAM_1);
  828. }
  829. EXPORT_SYMBOL_GPL(i2c_dw_read_comp_param);
  830. int i2c_dw_probe(struct dw_i2c_dev *dev)
  831. {
  832. struct i2c_adapter *adap = &dev->adapter;
  833. int r;
  834. u32 reg;
  835. init_completion(&dev->cmd_complete);
  836. r = i2c_dw_init(dev);
  837. if (r)
  838. return r;
  839. r = i2c_dw_acquire_lock(dev);
  840. if (r)
  841. return r;
  842. /*
  843. * Test if dynamic TAR update is enabled in this controller by writing
  844. * to IC_10BITADDR_MASTER field in IC_CON: when it is enabled this
  845. * field is read-only so it should not succeed
  846. */
  847. reg = dw_readl(dev, DW_IC_CON);
  848. dw_writel(dev, reg ^ DW_IC_CON_10BITADDR_MASTER, DW_IC_CON);
  849. if ((dw_readl(dev, DW_IC_CON) & DW_IC_CON_10BITADDR_MASTER) ==
  850. (reg & DW_IC_CON_10BITADDR_MASTER)) {
  851. dev->dynamic_tar_update_enabled = true;
  852. dev_dbg(dev->dev, "Dynamic TAR update enabled");
  853. }
  854. i2c_dw_release_lock(dev);
  855. snprintf(adap->name, sizeof(adap->name),
  856. "Synopsys DesignWare I2C adapter");
  857. adap->retries = 3;
  858. adap->algo = &i2c_dw_algo;
  859. adap->dev.parent = dev->dev;
  860. i2c_set_adapdata(adap, dev);
  861. i2c_dw_disable_int(dev);
  862. r = devm_request_irq(dev->dev, dev->irq, i2c_dw_isr,
  863. IRQF_SHARED | IRQF_COND_SUSPEND,
  864. dev_name(dev->dev), dev);
  865. if (r) {
  866. dev_err(dev->dev, "failure requesting irq %i: %d\n",
  867. dev->irq, r);
  868. return r;
  869. }
  870. /*
  871. * Increment PM usage count during adapter registration in order to
  872. * avoid possible spurious runtime suspend when adapter device is
  873. * registered to the device core and immediate resume in case bus has
  874. * registered I2C slaves that do I2C transfers in their probe.
  875. */
  876. pm_runtime_get_noresume(dev->dev);
  877. r = i2c_add_numbered_adapter(adap);
  878. if (r)
  879. dev_err(dev->dev, "failure adding adapter: %d\n", r);
  880. pm_runtime_put_noidle(dev->dev);
  881. return r;
  882. }
  883. EXPORT_SYMBOL_GPL(i2c_dw_probe);
  884. MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter core");
  885. MODULE_LICENSE("GPL");