i2c-uniphier-f.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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/sizes.h>
  12. #include <linux/errno.h>
  13. #include <dm/device.h>
  14. #include <dm/root.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 poll_status(u32 __iomem *reg, u32 flag)
  69. {
  70. int wait = 1000000; /* 1 sec is long enough */
  71. while (readl(reg) & flag) {
  72. if (wait-- < 0)
  73. return -EREMOTEIO;
  74. udelay(1);
  75. }
  76. return 0;
  77. }
  78. static int reset_bus(struct uniphier_fi2c_regs __iomem *regs)
  79. {
  80. int ret;
  81. /* bus forcible reset */
  82. writel(I2C_RST_RST, &regs->rst);
  83. ret = poll_status(&regs->rst, I2C_RST_RST);
  84. if (ret < 0)
  85. debug("error: fail to reset I2C controller\n");
  86. return ret;
  87. }
  88. static int check_device_busy(struct uniphier_fi2c_regs __iomem *regs)
  89. {
  90. int ret;
  91. ret = poll_status(&regs->sr, I2C_SR_DB);
  92. if (ret < 0) {
  93. debug("error: device busy too long. reset...\n");
  94. ret = reset_bus(regs);
  95. }
  96. return ret;
  97. }
  98. static int uniphier_fi2c_probe(struct udevice *dev)
  99. {
  100. fdt_addr_t addr;
  101. struct uniphier_fi2c_dev *priv = dev_get_priv(dev);
  102. int ret;
  103. addr = dev_get_addr(dev);
  104. if (addr == FDT_ADDR_T_NONE)
  105. return -EINVAL;
  106. priv->regs = devm_ioremap(dev, addr, SZ_128);
  107. if (!priv->regs)
  108. return -ENOMEM;
  109. priv->fioclk = FIOCLK;
  110. /* bus forcible reset */
  111. ret = reset_bus(priv->regs);
  112. if (ret < 0)
  113. return ret;
  114. writel(I2C_BRST_FOEN | I2C_BRST_RSCLO, &priv->regs->brst);
  115. return 0;
  116. }
  117. static int wait_for_irq(struct uniphier_fi2c_dev *dev, u32 flags,
  118. bool *stop)
  119. {
  120. u32 irq;
  121. unsigned long wait = dev->timeout;
  122. int ret = -EREMOTEIO;
  123. do {
  124. udelay(1);
  125. irq = readl(&dev->regs->intr);
  126. } while (!(irq & flags) && wait--);
  127. if (wait < 0) {
  128. debug("error: time out\n");
  129. return ret;
  130. }
  131. if (irq & I2C_INT_AL) {
  132. debug("error: arbitration lost\n");
  133. *stop = false;
  134. return ret;
  135. }
  136. if (irq & I2C_INT_NA) {
  137. debug("error: no answer\n");
  138. return ret;
  139. }
  140. return 0;
  141. }
  142. static int issue_stop(struct uniphier_fi2c_dev *dev, int old_ret)
  143. {
  144. int ret;
  145. debug("stop condition\n");
  146. writel(I2C_CR_MST | I2C_CR_STO, &dev->regs->cr);
  147. ret = poll_status(&dev->regs->sr, I2C_SR_DB);
  148. if (ret < 0)
  149. debug("error: device busy after operation\n");
  150. return old_ret ? old_ret : ret;
  151. }
  152. static int uniphier_fi2c_transmit(struct uniphier_fi2c_dev *dev, uint addr,
  153. uint len, const u8 *buf, bool *stop)
  154. {
  155. int ret;
  156. const u32 irq_flags = I2C_INT_TE | I2C_INT_NA | I2C_INT_AL;
  157. struct uniphier_fi2c_regs __iomem *regs = dev->regs;
  158. debug("%s: addr = %x, len = %d\n", __func__, addr, len);
  159. writel(I2C_DTTX_CMD | addr << 1, &regs->dttx);
  160. writel(irq_flags, &regs->ie);
  161. writel(irq_flags, &regs->ic);
  162. debug("start condition\n");
  163. writel(I2C_CR_MST | I2C_CR_STA, &regs->cr);
  164. ret = wait_for_irq(dev, irq_flags, stop);
  165. if (ret < 0)
  166. goto error;
  167. while (len--) {
  168. debug("sending %x\n", *buf);
  169. writel(*buf++, &regs->dttx);
  170. writel(irq_flags, &regs->ic);
  171. ret = wait_for_irq(dev, irq_flags, stop);
  172. if (ret < 0)
  173. goto error;
  174. }
  175. error:
  176. writel(irq_flags, &regs->ic);
  177. if (*stop)
  178. ret = issue_stop(dev, ret);
  179. return ret;
  180. }
  181. static int uniphier_fi2c_receive(struct uniphier_fi2c_dev *dev, uint addr,
  182. uint len, u8 *buf, bool *stop)
  183. {
  184. int ret = 0;
  185. const u32 irq_flags = I2C_INT_RB | I2C_INT_NA | I2C_INT_AL;
  186. struct uniphier_fi2c_regs __iomem *regs = dev->regs;
  187. debug("%s: addr = %x, len = %d\n", __func__, addr, len);
  188. /*
  189. * In case 'len == 0', only the slave address should be sent
  190. * for probing, which is covered by the transmit function.
  191. */
  192. if (len == 0)
  193. return uniphier_fi2c_transmit(dev, addr, len, buf, stop);
  194. writel(I2C_DTTX_CMD | I2C_DTTX_RD | addr << 1, &regs->dttx);
  195. writel(0, &regs->rbc);
  196. writel(irq_flags, &regs->ie);
  197. writel(irq_flags, &regs->ic);
  198. debug("start condition\n");
  199. writel(I2C_CR_MST | I2C_CR_STA | (len == 1 ? I2C_CR_NACK : 0),
  200. &regs->cr);
  201. while (len--) {
  202. ret = wait_for_irq(dev, irq_flags, stop);
  203. if (ret < 0)
  204. goto error;
  205. *buf++ = readl(&regs->dtrx);
  206. debug("received %x\n", *(buf - 1));
  207. if (len == 1)
  208. writel(I2C_CR_MST | I2C_CR_NACK, &regs->cr);
  209. writel(irq_flags, &regs->ic);
  210. }
  211. error:
  212. writel(irq_flags, &regs->ic);
  213. if (*stop)
  214. ret = issue_stop(dev, ret);
  215. return ret;
  216. }
  217. static int uniphier_fi2c_xfer(struct udevice *bus, struct i2c_msg *msg,
  218. int nmsgs)
  219. {
  220. int ret;
  221. struct uniphier_fi2c_dev *dev = dev_get_priv(bus);
  222. bool stop;
  223. ret = check_device_busy(dev->regs);
  224. if (ret < 0)
  225. return ret;
  226. for (; nmsgs > 0; nmsgs--, msg++) {
  227. /* If next message is read, skip the stop condition */
  228. stop = nmsgs > 1 && msg[1].flags & I2C_M_RD ? false : true;
  229. if (msg->flags & I2C_M_RD)
  230. ret = uniphier_fi2c_receive(dev, msg->addr, msg->len,
  231. msg->buf, &stop);
  232. else
  233. ret = uniphier_fi2c_transmit(dev, msg->addr, msg->len,
  234. msg->buf, &stop);
  235. if (ret < 0)
  236. break;
  237. }
  238. return ret;
  239. }
  240. static int uniphier_fi2c_set_bus_speed(struct udevice *bus, unsigned int speed)
  241. {
  242. int ret;
  243. unsigned int clk_count;
  244. struct uniphier_fi2c_dev *dev = dev_get_priv(bus);
  245. struct uniphier_fi2c_regs __iomem *regs = dev->regs;
  246. /* max supported frequency is 400 kHz */
  247. if (speed > 400000)
  248. return -EINVAL;
  249. ret = check_device_busy(dev->regs);
  250. if (ret < 0)
  251. return ret;
  252. /* make sure the bus is idle when changing the frequency */
  253. writel(I2C_BRST_RSCLO, &regs->brst);
  254. clk_count = dev->fioclk / speed;
  255. writel(clk_count, &regs->cyc);
  256. writel(clk_count / 2, &regs->lctl);
  257. writel(clk_count / 2, &regs->ssut);
  258. writel(clk_count / 16, &regs->dsut);
  259. writel(I2C_BRST_FOEN | I2C_BRST_RSCLO, &regs->brst);
  260. /*
  261. * Theoretically, each byte can be transferred in
  262. * 1000000 * 9 / speed usec.
  263. * This time out value is long enough.
  264. */
  265. dev->timeout = 100000000L / speed;
  266. return 0;
  267. }
  268. static const struct dm_i2c_ops uniphier_fi2c_ops = {
  269. .xfer = uniphier_fi2c_xfer,
  270. .set_bus_speed = uniphier_fi2c_set_bus_speed,
  271. };
  272. static const struct udevice_id uniphier_fi2c_of_match[] = {
  273. { .compatible = "socionext,uniphier-fi2c" },
  274. { /* sentinel */ }
  275. };
  276. U_BOOT_DRIVER(uniphier_fi2c) = {
  277. .name = "uniphier-fi2c",
  278. .id = UCLASS_I2C,
  279. .of_match = uniphier_fi2c_of_match,
  280. .probe = uniphier_fi2c_probe,
  281. .priv_auto_alloc_size = sizeof(struct uniphier_fi2c_dev),
  282. .ops = &uniphier_fi2c_ops,
  283. };