i2c-designware-common.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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->sda_hold_time) {
  186. dev_warn(dev->dev,
  187. "Hardware too old to adjust SDA hold time.\n");
  188. dev->sda_hold_time = 0;
  189. }
  190. i2c_dw_release_lock(dev);
  191. return 0;
  192. }
  193. void __i2c_dw_disable(struct dw_i2c_dev *dev)
  194. {
  195. int timeout = 100;
  196. do {
  197. __i2c_dw_disable_nowait(dev);
  198. /*
  199. * The enable status register may be unimplemented, but
  200. * in that case this test reads zero and exits the loop.
  201. */
  202. if ((dw_readl(dev, DW_IC_ENABLE_STATUS) & 1) == 0)
  203. return;
  204. /*
  205. * Wait 10 times the signaling period of the highest I2C
  206. * transfer supported by the driver (for 400KHz this is
  207. * 25us) as described in the DesignWare I2C databook.
  208. */
  209. usleep_range(25, 250);
  210. } while (timeout--);
  211. dev_warn(dev->dev, "timeout in disabling adapter\n");
  212. }
  213. unsigned long i2c_dw_clk_rate(struct dw_i2c_dev *dev)
  214. {
  215. /*
  216. * Clock is not necessary if we got LCNT/HCNT values directly from
  217. * the platform code.
  218. */
  219. if (WARN_ON_ONCE(!dev->get_clk_rate_khz))
  220. return 0;
  221. return dev->get_clk_rate_khz(dev);
  222. }
  223. int i2c_dw_prepare_clk(struct dw_i2c_dev *dev, bool prepare)
  224. {
  225. if (IS_ERR(dev->clk))
  226. return PTR_ERR(dev->clk);
  227. if (prepare)
  228. return clk_prepare_enable(dev->clk);
  229. clk_disable_unprepare(dev->clk);
  230. return 0;
  231. }
  232. EXPORT_SYMBOL_GPL(i2c_dw_prepare_clk);
  233. int i2c_dw_acquire_lock(struct dw_i2c_dev *dev)
  234. {
  235. int ret;
  236. if (!dev->acquire_lock)
  237. return 0;
  238. ret = dev->acquire_lock(dev);
  239. if (!ret)
  240. return 0;
  241. dev_err(dev->dev, "couldn't acquire bus ownership\n");
  242. return ret;
  243. }
  244. void i2c_dw_release_lock(struct dw_i2c_dev *dev)
  245. {
  246. if (dev->release_lock)
  247. dev->release_lock(dev);
  248. }
  249. /*
  250. * Waiting for bus not busy
  251. */
  252. int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev)
  253. {
  254. int timeout = TIMEOUT;
  255. while (dw_readl(dev, DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) {
  256. if (timeout <= 0) {
  257. dev_warn(dev->dev, "timeout waiting for bus ready\n");
  258. i2c_recover_bus(&dev->adapter);
  259. if (dw_readl(dev, DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY)
  260. return -ETIMEDOUT;
  261. return 0;
  262. }
  263. timeout--;
  264. usleep_range(1000, 1100);
  265. }
  266. return 0;
  267. }
  268. int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev)
  269. {
  270. unsigned long abort_source = dev->abort_source;
  271. int i;
  272. if (abort_source & DW_IC_TX_ABRT_NOACK) {
  273. for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  274. dev_dbg(dev->dev,
  275. "%s: %s\n", __func__, abort_sources[i]);
  276. return -EREMOTEIO;
  277. }
  278. for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  279. dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]);
  280. if (abort_source & DW_IC_TX_ARB_LOST)
  281. return -EAGAIN;
  282. else if (abort_source & DW_IC_TX_ABRT_GCALL_READ)
  283. return -EINVAL; /* wrong msgs[] data */
  284. else
  285. return -EIO;
  286. }
  287. u32 i2c_dw_func(struct i2c_adapter *adap)
  288. {
  289. struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
  290. return dev->functionality;
  291. }
  292. void i2c_dw_disable(struct dw_i2c_dev *dev)
  293. {
  294. /* Disable controller */
  295. __i2c_dw_disable(dev);
  296. /* Disable all interupts */
  297. dw_writel(dev, 0, DW_IC_INTR_MASK);
  298. dw_readl(dev, DW_IC_CLR_INTR);
  299. }
  300. void i2c_dw_disable_int(struct dw_i2c_dev *dev)
  301. {
  302. dw_writel(dev, 0, DW_IC_INTR_MASK);
  303. }
  304. u32 i2c_dw_read_comp_param(struct dw_i2c_dev *dev)
  305. {
  306. return dw_readl(dev, DW_IC_COMP_PARAM_1);
  307. }
  308. EXPORT_SYMBOL_GPL(i2c_dw_read_comp_param);
  309. MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter core");
  310. MODULE_LICENSE("GPL");