i2c-uniphier-f.c 8.5 KB

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