i2c-designware-common.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Synopsys DesignWare I2C adapter driver.
  4. *
  5. * Based on the TI DAVINCI I2C adapter driver.
  6. *
  7. * Copyright (C) 2006 Texas Instruments.
  8. * Copyright (C) 2007 MontaVista Software Inc.
  9. * Copyright (C) 2009 Provigent Ltd.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/export.h>
  14. #include <linux/errno.h>
  15. #include <linux/err.h>
  16. #include <linux/i2c.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/module.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/swab.h>
  22. #include "i2c-designware-core.h"
  23. static char *abort_sources[] = {
  24. [ABRT_7B_ADDR_NOACK] =
  25. "slave address not acknowledged (7bit mode)",
  26. [ABRT_10ADDR1_NOACK] =
  27. "first address byte not acknowledged (10bit mode)",
  28. [ABRT_10ADDR2_NOACK] =
  29. "second address byte not acknowledged (10bit mode)",
  30. [ABRT_TXDATA_NOACK] =
  31. "data not acknowledged",
  32. [ABRT_GCALL_NOACK] =
  33. "no acknowledgement for a general call",
  34. [ABRT_GCALL_READ] =
  35. "read after general call",
  36. [ABRT_SBYTE_ACKDET] =
  37. "start byte acknowledged",
  38. [ABRT_SBYTE_NORSTRT] =
  39. "trying to send start byte when restart is disabled",
  40. [ABRT_10B_RD_NORSTRT] =
  41. "trying to read when restart is disabled (10bit mode)",
  42. [ABRT_MASTER_DIS] =
  43. "trying to use disabled adapter",
  44. [ARB_LOST] =
  45. "lost arbitration",
  46. [ABRT_SLAVE_FLUSH_TXFIFO] =
  47. "read command so flush old data in the TX FIFO",
  48. [ABRT_SLAVE_ARBLOST] =
  49. "slave lost the bus while transmitting data to a remote master",
  50. [ABRT_SLAVE_RD_INTX] =
  51. "incorrect slave-transmitter mode configuration",
  52. };
  53. u32 dw_readl(struct dw_i2c_dev *dev, int offset)
  54. {
  55. u32 value;
  56. if (dev->flags & ACCESS_16BIT)
  57. value = readw_relaxed(dev->base + offset) |
  58. (readw_relaxed(dev->base + offset + 2) << 16);
  59. else
  60. value = readl_relaxed(dev->base + offset);
  61. if (dev->flags & ACCESS_SWAP)
  62. return swab32(value);
  63. else
  64. return value;
  65. }
  66. void dw_writel(struct dw_i2c_dev *dev, u32 b, int offset)
  67. {
  68. if (dev->flags & ACCESS_SWAP)
  69. b = swab32(b);
  70. if (dev->flags & ACCESS_16BIT) {
  71. writew_relaxed((u16)b, dev->base + offset);
  72. writew_relaxed((u16)(b >> 16), dev->base + offset + 2);
  73. } else {
  74. writel_relaxed(b, dev->base + offset);
  75. }
  76. }
  77. /**
  78. * i2c_dw_set_reg_access() - Set register access flags
  79. * @dev: device private data
  80. *
  81. * Autodetects needed register access mode and sets access flags accordingly.
  82. * This must be called before doing any other register access.
  83. */
  84. int i2c_dw_set_reg_access(struct dw_i2c_dev *dev)
  85. {
  86. u32 reg;
  87. int ret;
  88. ret = i2c_dw_acquire_lock(dev);
  89. if (ret)
  90. return ret;
  91. reg = dw_readl(dev, DW_IC_COMP_TYPE);
  92. i2c_dw_release_lock(dev);
  93. if (reg == swab32(DW_IC_COMP_TYPE_VALUE)) {
  94. /* Configure register endianess access */
  95. dev->flags |= ACCESS_SWAP;
  96. } else if (reg == (DW_IC_COMP_TYPE_VALUE & 0x0000ffff)) {
  97. /* Configure register access mode 16bit */
  98. dev->flags |= ACCESS_16BIT;
  99. } else if (reg != DW_IC_COMP_TYPE_VALUE) {
  100. dev_err(dev->dev,
  101. "Unknown Synopsys component type: 0x%08x\n", reg);
  102. return -ENODEV;
  103. }
  104. return 0;
  105. }
  106. u32 i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset)
  107. {
  108. /*
  109. * DesignWare I2C core doesn't seem to have solid strategy to meet
  110. * the tHD;STA timing spec. Configuring _HCNT based on tHIGH spec
  111. * will result in violation of the tHD;STA spec.
  112. */
  113. if (cond)
  114. /*
  115. * Conditional expression:
  116. *
  117. * IC_[FS]S_SCL_HCNT + (1+4+3) >= IC_CLK * tHIGH
  118. *
  119. * This is based on the DW manuals, and represents an ideal
  120. * configuration. The resulting I2C bus speed will be
  121. * faster than any of the others.
  122. *
  123. * If your hardware is free from tHD;STA issue, try this one.
  124. */
  125. return (ic_clk * tSYMBOL + 500000) / 1000000 - 8 + offset;
  126. else
  127. /*
  128. * Conditional expression:
  129. *
  130. * IC_[FS]S_SCL_HCNT + 3 >= IC_CLK * (tHD;STA + tf)
  131. *
  132. * This is just experimental rule; the tHD;STA period turned
  133. * out to be proportinal to (_HCNT + 3). With this setting,
  134. * we could meet both tHIGH and tHD;STA timing specs.
  135. *
  136. * If unsure, you'd better to take this alternative.
  137. *
  138. * The reason why we need to take into account "tf" here,
  139. * is the same as described in i2c_dw_scl_lcnt().
  140. */
  141. return (ic_clk * (tSYMBOL + tf) + 500000) / 1000000
  142. - 3 + offset;
  143. }
  144. u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset)
  145. {
  146. /*
  147. * Conditional expression:
  148. *
  149. * IC_[FS]S_SCL_LCNT + 1 >= IC_CLK * (tLOW + tf)
  150. *
  151. * DW I2C core starts counting the SCL CNTs for the LOW period
  152. * of the SCL clock (tLOW) as soon as it pulls the SCL line.
  153. * In order to meet the tLOW timing spec, we need to take into
  154. * account the fall time of SCL signal (tf). Default tf value
  155. * should be 0.3 us, for safety.
  156. */
  157. return ((ic_clk * (tLOW + tf) + 500000) / 1000000) - 1 + offset;
  158. }
  159. int i2c_dw_set_sda_hold(struct dw_i2c_dev *dev)
  160. {
  161. u32 reg;
  162. int ret;
  163. ret = i2c_dw_acquire_lock(dev);
  164. if (ret)
  165. return ret;
  166. /* Configure SDA Hold Time if required */
  167. reg = dw_readl(dev, DW_IC_COMP_VERSION);
  168. if (reg >= DW_IC_SDA_HOLD_MIN_VERS) {
  169. if (!dev->sda_hold_time) {
  170. /* Keep previous hold time setting if no one set it */
  171. dev->sda_hold_time = dw_readl(dev, DW_IC_SDA_HOLD);
  172. }
  173. /*
  174. * Workaround for avoiding TX arbitration lost in case I2C
  175. * slave pulls SDA down "too quickly" after falling egde of
  176. * SCL by enabling non-zero SDA RX hold. Specification says it
  177. * extends incoming SDA low to high transition while SCL is
  178. * high but it apprears to help also above issue.
  179. */
  180. if (!(dev->sda_hold_time & DW_IC_SDA_HOLD_RX_MASK))
  181. dev->sda_hold_time |= 1 << DW_IC_SDA_HOLD_RX_SHIFT;
  182. dev_dbg(dev->dev, "SDA Hold Time TX:RX = %d:%d\n",
  183. dev->sda_hold_time & ~(u32)DW_IC_SDA_HOLD_RX_MASK,
  184. dev->sda_hold_time >> DW_IC_SDA_HOLD_RX_SHIFT);
  185. } else if (dev->set_sda_hold_time) {
  186. dev->set_sda_hold_time(dev);
  187. } else if (dev->sda_hold_time) {
  188. dev_warn(dev->dev,
  189. "Hardware too old to adjust SDA hold time.\n");
  190. dev->sda_hold_time = 0;
  191. }
  192. i2c_dw_release_lock(dev);
  193. return 0;
  194. }
  195. void __i2c_dw_disable(struct dw_i2c_dev *dev)
  196. {
  197. int timeout = 100;
  198. do {
  199. __i2c_dw_disable_nowait(dev);
  200. /*
  201. * The enable status register may be unimplemented, but
  202. * in that case this test reads zero and exits the loop.
  203. */
  204. if ((dw_readl(dev, DW_IC_ENABLE_STATUS) & 1) == 0)
  205. return;
  206. /*
  207. * Wait 10 times the signaling period of the highest I2C
  208. * transfer supported by the driver (for 400KHz this is
  209. * 25us) as described in the DesignWare I2C databook.
  210. */
  211. usleep_range(25, 250);
  212. } while (timeout--);
  213. dev_warn(dev->dev, "timeout in disabling adapter\n");
  214. }
  215. unsigned long i2c_dw_clk_rate(struct dw_i2c_dev *dev)
  216. {
  217. /*
  218. * Clock is not necessary if we got LCNT/HCNT values directly from
  219. * the platform code.
  220. */
  221. if (WARN_ON_ONCE(!dev->get_clk_rate_khz))
  222. return 0;
  223. return dev->get_clk_rate_khz(dev);
  224. }
  225. int i2c_dw_prepare_clk(struct dw_i2c_dev *dev, bool prepare)
  226. {
  227. if (IS_ERR(dev->clk))
  228. return PTR_ERR(dev->clk);
  229. if (prepare)
  230. return clk_prepare_enable(dev->clk);
  231. clk_disable_unprepare(dev->clk);
  232. return 0;
  233. }
  234. EXPORT_SYMBOL_GPL(i2c_dw_prepare_clk);
  235. int i2c_dw_acquire_lock(struct dw_i2c_dev *dev)
  236. {
  237. int ret;
  238. if (!dev->acquire_lock)
  239. return 0;
  240. ret = dev->acquire_lock();
  241. if (!ret)
  242. return 0;
  243. dev_err(dev->dev, "couldn't acquire bus ownership\n");
  244. return ret;
  245. }
  246. void i2c_dw_release_lock(struct dw_i2c_dev *dev)
  247. {
  248. if (dev->release_lock)
  249. dev->release_lock();
  250. }
  251. /*
  252. * Waiting for bus not busy
  253. */
  254. int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev)
  255. {
  256. int timeout = TIMEOUT;
  257. while (dw_readl(dev, DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) {
  258. if (timeout <= 0) {
  259. dev_warn(dev->dev, "timeout waiting for bus ready\n");
  260. i2c_recover_bus(&dev->adapter);
  261. if (dw_readl(dev, DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY)
  262. return -ETIMEDOUT;
  263. return 0;
  264. }
  265. timeout--;
  266. usleep_range(1000, 1100);
  267. }
  268. return 0;
  269. }
  270. int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev)
  271. {
  272. unsigned long abort_source = dev->abort_source;
  273. int i;
  274. if (abort_source & DW_IC_TX_ABRT_NOACK) {
  275. for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  276. dev_dbg(dev->dev,
  277. "%s: %s\n", __func__, abort_sources[i]);
  278. return -EREMOTEIO;
  279. }
  280. for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  281. dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]);
  282. if (abort_source & DW_IC_TX_ARB_LOST)
  283. return -EAGAIN;
  284. else if (abort_source & DW_IC_TX_ABRT_GCALL_READ)
  285. return -EINVAL; /* wrong msgs[] data */
  286. else
  287. return -EIO;
  288. }
  289. u32 i2c_dw_func(struct i2c_adapter *adap)
  290. {
  291. struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
  292. return dev->functionality;
  293. }
  294. void i2c_dw_disable(struct dw_i2c_dev *dev)
  295. {
  296. /* Disable controller */
  297. __i2c_dw_disable(dev);
  298. /* Disable all interupts */
  299. dw_writel(dev, 0, DW_IC_INTR_MASK);
  300. dw_readl(dev, DW_IC_CLR_INTR);
  301. }
  302. void i2c_dw_disable_int(struct dw_i2c_dev *dev)
  303. {
  304. dw_writel(dev, 0, DW_IC_INTR_MASK);
  305. }
  306. u32 i2c_dw_read_comp_param(struct dw_i2c_dev *dev)
  307. {
  308. return dw_readl(dev, DW_IC_COMP_PARAM_1);
  309. }
  310. EXPORT_SYMBOL_GPL(i2c_dw_read_comp_param);
  311. MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter core");
  312. MODULE_LICENSE("GPL");