i2c-uniphier-f.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2014 Panasonic Corporation
  4. * Copyright (C) 2015-2016 Socionext Inc.
  5. * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
  6. */
  7. #include <linux/errno.h>
  8. #include <linux/io.h>
  9. #include <linux/iopoll.h>
  10. #include <linux/sizes.h>
  11. #include <linux/types.h>
  12. #include <dm.h>
  13. #include <i2c.h>
  14. #include <fdtdec.h>
  15. struct uniphier_fi2c_regs {
  16. u32 cr; /* control register */
  17. #define I2C_CR_MST (1 << 3) /* master mode */
  18. #define I2C_CR_STA (1 << 2) /* start condition */
  19. #define I2C_CR_STO (1 << 1) /* stop condition */
  20. #define I2C_CR_NACK (1 << 0) /* not ACK */
  21. u32 dttx; /* send FIFO (write-only) */
  22. #define dtrx dttx /* receive FIFO (read-only) */
  23. #define I2C_DTTX_CMD (1 << 8) /* send command (slave addr) */
  24. #define I2C_DTTX_RD (1 << 0) /* read */
  25. u32 __reserved; /* no register at offset 0x08 */
  26. u32 slad; /* slave address */
  27. u32 cyc; /* clock cycle control */
  28. u32 lctl; /* clock low period control */
  29. u32 ssut; /* restart/stop setup time control */
  30. u32 dsut; /* data setup time control */
  31. u32 intr; /* interrupt status */
  32. u32 ie; /* interrupt enable */
  33. u32 ic; /* interrupt clear */
  34. #define I2C_INT_TE (1 << 9) /* TX FIFO empty */
  35. #define I2C_INT_RB (1 << 4) /* received specified bytes */
  36. #define I2C_INT_NA (1 << 2) /* no answer */
  37. #define I2C_INT_AL (1 << 1) /* arbitration lost */
  38. u32 sr; /* status register */
  39. #define I2C_SR_DB (1 << 12) /* device busy */
  40. #define I2C_SR_BB (1 << 8) /* bus busy */
  41. #define I2C_SR_RFF (1 << 3) /* Rx FIFO full */
  42. #define I2C_SR_RNE (1 << 2) /* Rx FIFO not empty */
  43. #define I2C_SR_TNF (1 << 1) /* Tx FIFO not full */
  44. #define I2C_SR_TFE (1 << 0) /* Tx FIFO empty */
  45. u32 __reserved2; /* no register at offset 0x30 */
  46. u32 rst; /* reset control */
  47. #define I2C_RST_TBRST (1 << 2) /* clear Tx FIFO */
  48. #define I2C_RST_RBRST (1 << 1) /* clear Rx FIFO */
  49. #define I2C_RST_RST (1 << 0) /* forcible bus reset */
  50. u32 bm; /* bus monitor */
  51. u32 noise; /* noise filter control */
  52. u32 tbc; /* Tx byte count setting */
  53. u32 rbc; /* Rx byte count setting */
  54. u32 tbcm; /* Tx byte count monitor */
  55. u32 rbcm; /* Rx byte count monitor */
  56. u32 brst; /* bus reset */
  57. #define I2C_BRST_FOEN (1 << 1) /* normal operation */
  58. #define I2C_BRST_RSCLO (1 << 0) /* release SCL low fixing */
  59. };
  60. #define FIOCLK 50000000
  61. struct uniphier_fi2c_priv {
  62. struct udevice *dev;
  63. struct uniphier_fi2c_regs __iomem *regs; /* register base */
  64. unsigned long fioclk; /* internal operation clock */
  65. unsigned long timeout; /* time out (us) */
  66. };
  67. static void uniphier_fi2c_reset(struct uniphier_fi2c_priv *priv)
  68. {
  69. writel(I2C_RST_RST, &priv->regs->rst);
  70. }
  71. static int uniphier_fi2c_check_bus_busy(struct uniphier_fi2c_priv *priv)
  72. {
  73. u32 val;
  74. int ret;
  75. ret = readl_poll_timeout(&priv->regs->sr, val, !(val & I2C_SR_DB), 100);
  76. if (ret < 0) {
  77. dev_dbg(priv->dev, "error: device busy too long. reset...\n");
  78. uniphier_fi2c_reset(priv);
  79. }
  80. return ret;
  81. }
  82. static int uniphier_fi2c_probe(struct udevice *dev)
  83. {
  84. fdt_addr_t addr;
  85. struct uniphier_fi2c_priv *priv = dev_get_priv(dev);
  86. addr = devfdt_get_addr(dev);
  87. if (addr == FDT_ADDR_T_NONE)
  88. return -EINVAL;
  89. priv->regs = devm_ioremap(dev, addr, SZ_128);
  90. if (!priv->regs)
  91. return -ENOMEM;
  92. priv->fioclk = FIOCLK;
  93. priv->dev = dev;
  94. /* bus forcible reset */
  95. uniphier_fi2c_reset(priv);
  96. writel(I2C_BRST_FOEN | I2C_BRST_RSCLO, &priv->regs->brst);
  97. return 0;
  98. }
  99. static int wait_for_irq(struct uniphier_fi2c_priv *priv, u32 flags,
  100. bool *stop)
  101. {
  102. u32 irq;
  103. int ret;
  104. ret = readl_poll_timeout(&priv->regs->intr, irq, irq & flags,
  105. priv->timeout);
  106. if (ret < 0) {
  107. dev_dbg(priv->dev, "error: time out\n");
  108. return ret;
  109. }
  110. if (irq & I2C_INT_AL) {
  111. dev_dbg(priv->dev, "error: arbitration lost\n");
  112. *stop = false;
  113. return ret;
  114. }
  115. if (irq & I2C_INT_NA) {
  116. dev_dbg(priv->dev, "error: no answer\n");
  117. return ret;
  118. }
  119. return 0;
  120. }
  121. static int issue_stop(struct uniphier_fi2c_priv *priv, int old_ret)
  122. {
  123. int ret;
  124. dev_dbg(priv->dev, "stop condition\n");
  125. writel(I2C_CR_MST | I2C_CR_STO, &priv->regs->cr);
  126. ret = uniphier_fi2c_check_bus_busy(priv);
  127. if (ret < 0)
  128. dev_dbg(priv->dev, "error: device busy after operation\n");
  129. return old_ret ? old_ret : ret;
  130. }
  131. static int uniphier_fi2c_transmit(struct uniphier_fi2c_priv *priv, uint addr,
  132. uint len, const u8 *buf, bool *stop)
  133. {
  134. int ret;
  135. const u32 irq_flags = I2C_INT_TE | I2C_INT_NA | I2C_INT_AL;
  136. struct uniphier_fi2c_regs __iomem *regs = priv->regs;
  137. dev_dbg(priv->dev, "%s: addr = %x, len = %d\n", __func__, addr, len);
  138. writel(I2C_DTTX_CMD | addr << 1, &regs->dttx);
  139. writel(irq_flags, &regs->ie);
  140. writel(irq_flags, &regs->ic);
  141. dev_dbg(priv->dev, "start condition\n");
  142. writel(I2C_CR_MST | I2C_CR_STA, &regs->cr);
  143. ret = wait_for_irq(priv, irq_flags, stop);
  144. if (ret < 0)
  145. goto error;
  146. while (len--) {
  147. dev_dbg(priv->dev, "sending %x\n", *buf);
  148. writel(*buf++, &regs->dttx);
  149. writel(irq_flags, &regs->ic);
  150. ret = wait_for_irq(priv, irq_flags, stop);
  151. if (ret < 0)
  152. goto error;
  153. }
  154. error:
  155. writel(irq_flags, &regs->ic);
  156. if (*stop)
  157. ret = issue_stop(priv, ret);
  158. return ret;
  159. }
  160. static int uniphier_fi2c_receive(struct uniphier_fi2c_priv *priv, uint addr,
  161. uint len, u8 *buf, bool *stop)
  162. {
  163. int ret = 0;
  164. const u32 irq_flags = I2C_INT_RB | I2C_INT_NA | I2C_INT_AL;
  165. struct uniphier_fi2c_regs __iomem *regs = priv->regs;
  166. dev_dbg(priv->dev, "%s: addr = %x, len = %d\n", __func__, addr, len);
  167. /*
  168. * In case 'len == 0', only the slave address should be sent
  169. * for probing, which is covered by the transmit function.
  170. */
  171. if (len == 0)
  172. return uniphier_fi2c_transmit(priv, addr, len, buf, stop);
  173. writel(I2C_DTTX_CMD | I2C_DTTX_RD | addr << 1, &regs->dttx);
  174. writel(0, &regs->rbc);
  175. writel(irq_flags, &regs->ie);
  176. writel(irq_flags, &regs->ic);
  177. dev_dbg(priv->dev, "start condition\n");
  178. writel(I2C_CR_MST | I2C_CR_STA | (len == 1 ? I2C_CR_NACK : 0),
  179. &regs->cr);
  180. while (len--) {
  181. ret = wait_for_irq(priv, irq_flags, stop);
  182. if (ret < 0)
  183. goto error;
  184. *buf++ = readl(&regs->dtrx);
  185. dev_dbg(priv->dev, "received %x\n", *(buf - 1));
  186. if (len == 1)
  187. writel(I2C_CR_MST | I2C_CR_NACK, &regs->cr);
  188. writel(irq_flags, &regs->ic);
  189. }
  190. error:
  191. writel(irq_flags, &regs->ic);
  192. if (*stop)
  193. ret = issue_stop(priv, ret);
  194. return ret;
  195. }
  196. static int uniphier_fi2c_xfer(struct udevice *bus, struct i2c_msg *msg,
  197. int nmsgs)
  198. {
  199. int ret;
  200. struct uniphier_fi2c_priv *priv = dev_get_priv(bus);
  201. bool stop;
  202. ret = uniphier_fi2c_check_bus_busy(priv);
  203. if (ret < 0)
  204. return ret;
  205. for (; nmsgs > 0; nmsgs--, msg++) {
  206. /* If next message is read, skip the stop condition */
  207. stop = nmsgs > 1 && msg[1].flags & I2C_M_RD ? false : true;
  208. if (msg->flags & I2C_M_RD)
  209. ret = uniphier_fi2c_receive(priv, msg->addr, msg->len,
  210. msg->buf, &stop);
  211. else
  212. ret = uniphier_fi2c_transmit(priv, msg->addr, msg->len,
  213. msg->buf, &stop);
  214. if (ret < 0)
  215. break;
  216. }
  217. return ret;
  218. }
  219. static int uniphier_fi2c_set_bus_speed(struct udevice *bus, unsigned int speed)
  220. {
  221. int ret;
  222. unsigned int clk_count;
  223. struct uniphier_fi2c_priv *priv = dev_get_priv(bus);
  224. struct uniphier_fi2c_regs __iomem *regs = priv->regs;
  225. /* max supported frequency is 400 kHz */
  226. if (speed > 400000)
  227. return -EINVAL;
  228. ret = uniphier_fi2c_check_bus_busy(priv);
  229. if (ret < 0)
  230. return ret;
  231. /* make sure the bus is idle when changing the frequency */
  232. writel(I2C_BRST_RSCLO, &regs->brst);
  233. clk_count = priv->fioclk / speed;
  234. writel(clk_count, &regs->cyc);
  235. writel(clk_count / 2, &regs->lctl);
  236. writel(clk_count / 2, &regs->ssut);
  237. writel(clk_count / 16, &regs->dsut);
  238. writel(I2C_BRST_FOEN | I2C_BRST_RSCLO, &regs->brst);
  239. /*
  240. * Theoretically, each byte can be transferred in
  241. * 1000000 * 9 / speed usec.
  242. * This time out value is long enough.
  243. */
  244. priv->timeout = 100000000L / speed;
  245. return 0;
  246. }
  247. static const struct dm_i2c_ops uniphier_fi2c_ops = {
  248. .xfer = uniphier_fi2c_xfer,
  249. .set_bus_speed = uniphier_fi2c_set_bus_speed,
  250. };
  251. static const struct udevice_id uniphier_fi2c_of_match[] = {
  252. { .compatible = "socionext,uniphier-fi2c" },
  253. { /* sentinel */ }
  254. };
  255. U_BOOT_DRIVER(uniphier_fi2c) = {
  256. .name = "uniphier-fi2c",
  257. .id = UCLASS_I2C,
  258. .of_match = uniphier_fi2c_of_match,
  259. .probe = uniphier_fi2c_probe,
  260. .priv_auto_alloc_size = sizeof(struct uniphier_fi2c_priv),
  261. .ops = &uniphier_fi2c_ops,
  262. };