i2c-uniphier-f.c 8.2 KB

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