rcar_i2c.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * drivers/i2c/rcar_i2c.c
  4. *
  5. * Copyright (C) 2018 Marek Vasut <marek.vasut@gmail.com>
  6. *
  7. * Clock configuration based on Linux i2c-rcar.c:
  8. * Copyright (C) 2014-15 Wolfram Sang <wsa@sang-engineering.com>
  9. * Copyright (C) 2011-2015 Renesas Electronics Corporation
  10. * Copyright (C) 2012-14 Renesas Solutions Corp.
  11. * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  12. */
  13. #include <common.h>
  14. #include <clk.h>
  15. #include <dm.h>
  16. #include <i2c.h>
  17. #include <asm/io.h>
  18. #include <wait_bit.h>
  19. #define RCAR_I2C_ICSCR 0x00
  20. #define RCAR_I2C_ICMCR 0x04
  21. #define RCAR_I2C_ICMCR_MDBS BIT(7)
  22. #define RCAR_I2C_ICMCR_FSCL BIT(6)
  23. #define RCAR_I2C_ICMCR_FSDA BIT(5)
  24. #define RCAR_I2C_ICMCR_OBPC BIT(4)
  25. #define RCAR_I2C_ICMCR_MIE BIT(3)
  26. #define RCAR_I2C_ICMCR_TSBE BIT(2)
  27. #define RCAR_I2C_ICMCR_FSB BIT(1)
  28. #define RCAR_I2C_ICMCR_ESG BIT(0)
  29. #define RCAR_I2C_ICSSR 0x08
  30. #define RCAR_I2C_ICMSR 0x0c
  31. #define RCAR_I2C_ICMSR_MASK 0x7f
  32. #define RCAR_I2C_ICMSR_MNR BIT(6)
  33. #define RCAR_I2C_ICMSR_MAL BIT(5)
  34. #define RCAR_I2C_ICMSR_MST BIT(4)
  35. #define RCAR_I2C_ICMSR_MDE BIT(3)
  36. #define RCAR_I2C_ICMSR_MDT BIT(2)
  37. #define RCAR_I2C_ICMSR_MDR BIT(1)
  38. #define RCAR_I2C_ICMSR_MAT BIT(0)
  39. #define RCAR_I2C_ICSIER 0x10
  40. #define RCAR_I2C_ICMIER 0x14
  41. #define RCAR_I2C_ICCCR 0x18
  42. #define RCAR_I2C_ICCCR_SCGD_OFF 3
  43. #define RCAR_I2C_ICSAR 0x1c
  44. #define RCAR_I2C_ICMAR 0x20
  45. #define RCAR_I2C_ICRXD_ICTXD 0x24
  46. struct rcar_i2c_priv {
  47. void __iomem *base;
  48. struct clk clk;
  49. u32 intdelay;
  50. u32 icccr;
  51. };
  52. static int rcar_i2c_finish(struct udevice *dev)
  53. {
  54. struct rcar_i2c_priv *priv = dev_get_priv(dev);
  55. int ret;
  56. ret = wait_for_bit_le32(priv->base + RCAR_I2C_ICMSR, RCAR_I2C_ICMSR_MST,
  57. true, 10, true);
  58. writel(0, priv->base + RCAR_I2C_ICSSR);
  59. writel(0, priv->base + RCAR_I2C_ICMSR);
  60. writel(0, priv->base + RCAR_I2C_ICMCR);
  61. return ret;
  62. }
  63. static void rcar_i2c_recover(struct udevice *dev)
  64. {
  65. struct rcar_i2c_priv *priv = dev_get_priv(dev);
  66. u32 mcr = RCAR_I2C_ICMCR_MDBS | RCAR_I2C_ICMCR_OBPC;
  67. u32 mcra = mcr | RCAR_I2C_ICMCR_FSDA;
  68. int i;
  69. /* Send 9 SCL pulses */
  70. for (i = 0; i < 9; i++) {
  71. writel(mcra | RCAR_I2C_ICMCR_FSCL, priv->base + RCAR_I2C_ICMCR);
  72. udelay(5);
  73. writel(mcra, priv->base + RCAR_I2C_ICMCR);
  74. udelay(5);
  75. }
  76. /* Send stop condition */
  77. udelay(5);
  78. writel(mcra, priv->base + RCAR_I2C_ICMCR);
  79. udelay(5);
  80. writel(mcr, priv->base + RCAR_I2C_ICMCR);
  81. udelay(5);
  82. writel(mcr | RCAR_I2C_ICMCR_FSCL, priv->base + RCAR_I2C_ICMCR);
  83. udelay(5);
  84. writel(mcra | RCAR_I2C_ICMCR_FSCL, priv->base + RCAR_I2C_ICMCR);
  85. udelay(5);
  86. }
  87. static int rcar_i2c_set_addr(struct udevice *dev, u8 chip, u8 read)
  88. {
  89. struct rcar_i2c_priv *priv = dev_get_priv(dev);
  90. u32 mask = RCAR_I2C_ICMSR_MAT |
  91. (read ? RCAR_I2C_ICMSR_MDR : RCAR_I2C_ICMSR_MDE);
  92. u32 val;
  93. int ret;
  94. writel(0, priv->base + RCAR_I2C_ICMIER);
  95. writel(RCAR_I2C_ICMCR_MDBS, priv->base + RCAR_I2C_ICMCR);
  96. writel(0, priv->base + RCAR_I2C_ICMSR);
  97. writel(priv->icccr, priv->base + RCAR_I2C_ICCCR);
  98. ret = wait_for_bit_le32(priv->base + RCAR_I2C_ICMCR,
  99. RCAR_I2C_ICMCR_FSDA, false, 2, true);
  100. if (ret) {
  101. rcar_i2c_recover(dev);
  102. val = readl(priv->base + RCAR_I2C_ICMSR);
  103. if (val & RCAR_I2C_ICMCR_FSDA) {
  104. dev_err(dev, "Bus busy, aborting\n");
  105. return ret;
  106. }
  107. }
  108. writel((chip << 1) | read, priv->base + RCAR_I2C_ICMAR);
  109. writel(0, priv->base + RCAR_I2C_ICMSR);
  110. writel(RCAR_I2C_ICMCR_MDBS | RCAR_I2C_ICMCR_MIE | RCAR_I2C_ICMCR_ESG,
  111. priv->base + RCAR_I2C_ICMCR);
  112. ret = wait_for_bit_le32(priv->base + RCAR_I2C_ICMSR, mask,
  113. true, 100, true);
  114. if (ret)
  115. return ret;
  116. /* Check NAK */
  117. if (readl(priv->base + RCAR_I2C_ICMSR) & RCAR_I2C_ICMSR_MNR)
  118. return -EREMOTEIO;
  119. return 0;
  120. }
  121. static int rcar_i2c_read_common(struct udevice *dev, struct i2c_msg *msg)
  122. {
  123. struct rcar_i2c_priv *priv = dev_get_priv(dev);
  124. u32 icmcr = RCAR_I2C_ICMCR_MDBS | RCAR_I2C_ICMCR_MIE;
  125. int i, ret = -EREMOTEIO;
  126. ret = rcar_i2c_set_addr(dev, msg->addr, 1);
  127. if (ret)
  128. return ret;
  129. for (i = 0; i < msg->len; i++) {
  130. if (msg->len - 1 == i)
  131. icmcr |= RCAR_I2C_ICMCR_FSB;
  132. writel(icmcr, priv->base + RCAR_I2C_ICMCR);
  133. writel(~RCAR_I2C_ICMSR_MDR, priv->base + RCAR_I2C_ICMSR);
  134. ret = wait_for_bit_le32(priv->base + RCAR_I2C_ICMSR,
  135. RCAR_I2C_ICMSR_MDR, true, 100, true);
  136. if (ret)
  137. return ret;
  138. msg->buf[i] = readl(priv->base + RCAR_I2C_ICRXD_ICTXD) & 0xff;
  139. }
  140. writel(~RCAR_I2C_ICMSR_MDR, priv->base + RCAR_I2C_ICMSR);
  141. return rcar_i2c_finish(dev);
  142. }
  143. static int rcar_i2c_write_common(struct udevice *dev, struct i2c_msg *msg)
  144. {
  145. struct rcar_i2c_priv *priv = dev_get_priv(dev);
  146. u32 icmcr = RCAR_I2C_ICMCR_MDBS | RCAR_I2C_ICMCR_MIE;
  147. int i, ret = -EREMOTEIO;
  148. ret = rcar_i2c_set_addr(dev, msg->addr, 0);
  149. if (ret)
  150. return ret;
  151. for (i = 0; i < msg->len; i++) {
  152. writel(msg->buf[i], priv->base + RCAR_I2C_ICRXD_ICTXD);
  153. writel(icmcr, priv->base + RCAR_I2C_ICMCR);
  154. writel(~RCAR_I2C_ICMSR_MDE, priv->base + RCAR_I2C_ICMSR);
  155. ret = wait_for_bit_le32(priv->base + RCAR_I2C_ICMSR,
  156. RCAR_I2C_ICMSR_MDE, true, 100, true);
  157. if (ret)
  158. return ret;
  159. }
  160. writel(~RCAR_I2C_ICMSR_MDE, priv->base + RCAR_I2C_ICMSR);
  161. icmcr |= RCAR_I2C_ICMCR_FSB;
  162. writel(icmcr, priv->base + RCAR_I2C_ICMCR);
  163. return rcar_i2c_finish(dev);
  164. }
  165. static int rcar_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
  166. {
  167. int ret;
  168. for (; nmsgs > 0; nmsgs--, msg++) {
  169. if (msg->flags & I2C_M_RD)
  170. ret = rcar_i2c_read_common(dev, msg);
  171. else
  172. ret = rcar_i2c_write_common(dev, msg);
  173. if (ret)
  174. return -EREMOTEIO;
  175. }
  176. return ret;
  177. }
  178. static int rcar_i2c_probe_chip(struct udevice *dev, uint addr, uint flags)
  179. {
  180. struct rcar_i2c_priv *priv = dev_get_priv(dev);
  181. int ret;
  182. /* Ignore address 0, slave address */
  183. if (addr == 0)
  184. return -EINVAL;
  185. ret = rcar_i2c_set_addr(dev, addr, 1);
  186. writel(0, priv->base + RCAR_I2C_ICMSR);
  187. return ret;
  188. }
  189. static int rcar_i2c_set_speed(struct udevice *dev, uint bus_freq_hz)
  190. {
  191. struct rcar_i2c_priv *priv = dev_get_priv(dev);
  192. u32 scgd, cdf, round, ick, sum, scl;
  193. unsigned long rate;
  194. /*
  195. * calculate SCL clock
  196. * see
  197. * ICCCR
  198. *
  199. * ick = clkp / (1 + CDF)
  200. * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
  201. *
  202. * ick : I2C internal clock < 20 MHz
  203. * ticf : I2C SCL falling time
  204. * tr : I2C SCL rising time
  205. * intd : LSI internal delay
  206. * clkp : peripheral_clk
  207. * F[] : integer up-valuation
  208. */
  209. rate = clk_get_rate(&priv->clk);
  210. cdf = rate / 20000000;
  211. if (cdf >= 8) {
  212. dev_err(dev, "Input clock %lu too high\n", rate);
  213. return -EIO;
  214. }
  215. ick = rate / (cdf + 1);
  216. /*
  217. * it is impossible to calculate large scale
  218. * number on u32. separate it
  219. *
  220. * F[(ticf + tr + intd) * ick] with sum = (ticf + tr + intd)
  221. * = F[sum * ick / 1000000000]
  222. * = F[(ick / 1000000) * sum / 1000]
  223. */
  224. sum = 35 + 200 + priv->intdelay;
  225. round = (ick + 500000) / 1000000 * sum;
  226. round = (round + 500) / 1000;
  227. /*
  228. * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
  229. *
  230. * Calculation result (= SCL) should be less than
  231. * bus_speed for hardware safety
  232. *
  233. * We could use something along the lines of
  234. * div = ick / (bus_speed + 1) + 1;
  235. * scgd = (div - 20 - round + 7) / 8;
  236. * scl = ick / (20 + (scgd * 8) + round);
  237. * (not fully verified) but that would get pretty involved
  238. */
  239. for (scgd = 0; scgd < 0x40; scgd++) {
  240. scl = ick / (20 + (scgd * 8) + round);
  241. if (scl <= bus_freq_hz)
  242. goto scgd_find;
  243. }
  244. dev_err(dev, "it is impossible to calculate best SCL\n");
  245. return -EIO;
  246. scgd_find:
  247. dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n",
  248. scl, bus_freq_hz, clk_get_rate(&priv->clk), round, cdf, scgd);
  249. priv->icccr = (scgd << RCAR_I2C_ICCCR_SCGD_OFF) | cdf;
  250. writel(priv->icccr, priv->base + RCAR_I2C_ICCCR);
  251. return 0;
  252. }
  253. static int rcar_i2c_probe(struct udevice *dev)
  254. {
  255. struct rcar_i2c_priv *priv = dev_get_priv(dev);
  256. int ret;
  257. priv->base = dev_read_addr_ptr(dev);
  258. priv->intdelay = dev_read_u32_default(dev,
  259. "i2c-scl-internal-delay-ns", 5);
  260. ret = clk_get_by_index(dev, 0, &priv->clk);
  261. if (ret)
  262. return ret;
  263. ret = clk_enable(&priv->clk);
  264. if (ret)
  265. return ret;
  266. /* reset slave mode */
  267. writel(0, priv->base + RCAR_I2C_ICSIER);
  268. writel(0, priv->base + RCAR_I2C_ICSAR);
  269. writel(0, priv->base + RCAR_I2C_ICSCR);
  270. writel(0, priv->base + RCAR_I2C_ICSSR);
  271. /* reset master mode */
  272. writel(0, priv->base + RCAR_I2C_ICMIER);
  273. writel(0, priv->base + RCAR_I2C_ICMCR);
  274. writel(0, priv->base + RCAR_I2C_ICMSR);
  275. writel(0, priv->base + RCAR_I2C_ICMAR);
  276. ret = rcar_i2c_set_speed(dev, 100000);
  277. if (ret)
  278. clk_disable(&priv->clk);
  279. return ret;
  280. }
  281. static const struct dm_i2c_ops rcar_i2c_ops = {
  282. .xfer = rcar_i2c_xfer,
  283. .probe_chip = rcar_i2c_probe_chip,
  284. .set_bus_speed = rcar_i2c_set_speed,
  285. };
  286. static const struct udevice_id rcar_i2c_ids[] = {
  287. { .compatible = "renesas,rcar-gen2-i2c" },
  288. { }
  289. };
  290. U_BOOT_DRIVER(i2c_rcar) = {
  291. .name = "i2c_rcar",
  292. .id = UCLASS_I2C,
  293. .of_match = rcar_i2c_ids,
  294. .probe = rcar_i2c_probe,
  295. .priv_auto_alloc_size = sizeof(struct rcar_i2c_priv),
  296. .ops = &rcar_i2c_ops,
  297. };