i2c-uniphier-f.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Copyright (C) 2014 Panasonic Corporation
  3. * Author: Masahiro Yamada <yamada.m@jp.panasonic.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <linux/types.h>
  9. #include <asm/io.h>
  10. #include <asm/errno.h>
  11. #include <dm/device.h>
  12. #include <dm/root.h>
  13. #include <i2c.h>
  14. #include <fdtdec.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 uniphier_fi2c_child_pre_probe(struct udevice *dev)
  123. {
  124. struct dm_i2c_chip *i2c_chip = dev_get_parentdata(dev);
  125. if (dev->of_offset == -1)
  126. return 0;
  127. return i2c_chip_ofdata_to_platdata(gd->fdt_blob, dev->of_offset,
  128. i2c_chip);
  129. }
  130. static int wait_for_irq(struct uniphier_fi2c_dev *dev, u32 flags,
  131. bool *stop)
  132. {
  133. u32 irq;
  134. unsigned long wait = dev->timeout;
  135. int ret = -EREMOTEIO;
  136. do {
  137. udelay(1);
  138. irq = readl(&dev->regs->intr);
  139. } while (!(irq & flags) && wait--);
  140. if (wait < 0) {
  141. debug("error: time out\n");
  142. return ret;
  143. }
  144. if (irq & I2C_INT_AL) {
  145. debug("error: arbitration lost\n");
  146. *stop = false;
  147. return ret;
  148. }
  149. if (irq & I2C_INT_NA) {
  150. debug("error: no answer\n");
  151. return ret;
  152. }
  153. return 0;
  154. }
  155. static int issue_stop(struct uniphier_fi2c_dev *dev, int old_ret)
  156. {
  157. int ret;
  158. debug("stop condition\n");
  159. writel(I2C_CR_MST | I2C_CR_STO, &dev->regs->cr);
  160. ret = poll_status(&dev->regs->sr, I2C_SR_DB);
  161. if (ret < 0)
  162. debug("error: device busy after operation\n");
  163. return old_ret ? old_ret : ret;
  164. }
  165. static int uniphier_fi2c_transmit(struct uniphier_fi2c_dev *dev, uint addr,
  166. uint len, const u8 *buf, bool *stop)
  167. {
  168. int ret;
  169. const u32 irq_flags = I2C_INT_TE | I2C_INT_NA | I2C_INT_AL;
  170. struct uniphier_fi2c_regs __iomem *regs = dev->regs;
  171. debug("%s: addr = %x, len = %d\n", __func__, addr, len);
  172. writel(I2C_DTTX_CMD | addr << 1, &regs->dttx);
  173. writel(irq_flags, &regs->ie);
  174. writel(irq_flags, &regs->ic);
  175. debug("start condition\n");
  176. writel(I2C_CR_MST | I2C_CR_STA, &regs->cr);
  177. ret = wait_for_irq(dev, irq_flags, stop);
  178. if (ret < 0)
  179. goto error;
  180. while (len--) {
  181. debug("sending %x\n", *buf);
  182. writel(*buf++, &regs->dttx);
  183. writel(irq_flags, &regs->ic);
  184. ret = wait_for_irq(dev, irq_flags, stop);
  185. if (ret < 0)
  186. goto error;
  187. }
  188. error:
  189. writel(irq_flags, &regs->ic);
  190. if (*stop)
  191. ret = issue_stop(dev, ret);
  192. return ret;
  193. }
  194. static int uniphier_fi2c_receive(struct uniphier_fi2c_dev *dev, uint addr,
  195. uint len, u8 *buf, bool *stop)
  196. {
  197. int ret = 0;
  198. const u32 irq_flags = I2C_INT_RB | I2C_INT_NA | I2C_INT_AL;
  199. struct uniphier_fi2c_regs __iomem *regs = dev->regs;
  200. debug("%s: addr = %x, len = %d\n", __func__, addr, len);
  201. /*
  202. * In case 'len == 0', only the slave address should be sent
  203. * for probing, which is covered by the transmit function.
  204. */
  205. if (len == 0)
  206. return uniphier_fi2c_transmit(dev, addr, len, buf, stop);
  207. writel(I2C_DTTX_CMD | I2C_DTTX_RD | addr << 1, &regs->dttx);
  208. writel(0, &regs->rbc);
  209. writel(irq_flags, &regs->ie);
  210. writel(irq_flags, &regs->ic);
  211. debug("start condition\n");
  212. writel(I2C_CR_MST | I2C_CR_STA | (len == 1 ? I2C_CR_NACK : 0),
  213. &regs->cr);
  214. while (len--) {
  215. ret = wait_for_irq(dev, irq_flags, stop);
  216. if (ret < 0)
  217. goto error;
  218. *buf++ = readl(&regs->dtrx);
  219. debug("received %x\n", *(buf - 1));
  220. if (len == 1)
  221. writel(I2C_CR_MST | I2C_CR_NACK, &regs->cr);
  222. writel(irq_flags, &regs->ic);
  223. }
  224. error:
  225. writel(irq_flags, &regs->ic);
  226. if (*stop)
  227. ret = issue_stop(dev, ret);
  228. return ret;
  229. }
  230. static int uniphier_fi2c_xfer(struct udevice *bus, struct i2c_msg *msg,
  231. int nmsgs)
  232. {
  233. int ret;
  234. struct uniphier_fi2c_dev *dev = dev_get_priv(bus);
  235. bool stop;
  236. ret = check_device_busy(dev->regs);
  237. if (ret < 0)
  238. return ret;
  239. for (; nmsgs > 0; nmsgs--, msg++) {
  240. /* If next message is read, skip the stop condition */
  241. stop = nmsgs > 1 && msg[1].flags & I2C_M_RD ? false : true;
  242. if (msg->flags & I2C_M_RD)
  243. ret = uniphier_fi2c_receive(dev, msg->addr, msg->len,
  244. msg->buf, &stop);
  245. else
  246. ret = uniphier_fi2c_transmit(dev, msg->addr, msg->len,
  247. msg->buf, &stop);
  248. if (ret < 0)
  249. break;
  250. }
  251. return ret;
  252. }
  253. static int uniphier_fi2c_set_bus_speed(struct udevice *bus, unsigned int speed)
  254. {
  255. int ret;
  256. unsigned int clk_count;
  257. struct uniphier_fi2c_dev *dev = dev_get_priv(bus);
  258. struct uniphier_fi2c_regs __iomem *regs = dev->regs;
  259. /* max supported frequency is 400 kHz */
  260. if (speed > 400000)
  261. return -EINVAL;
  262. ret = check_device_busy(dev->regs);
  263. if (ret < 0)
  264. return ret;
  265. /* make sure the bus is idle when changing the frequency */
  266. writel(I2C_BRST_RSCLO, &regs->brst);
  267. clk_count = dev->fioclk / speed;
  268. writel(clk_count, &regs->cyc);
  269. writel(clk_count / 2, &regs->lctl);
  270. writel(clk_count / 2, &regs->ssut);
  271. writel(clk_count / 16, &regs->dsut);
  272. writel(I2C_BRST_FOEN | I2C_BRST_RSCLO, &regs->brst);
  273. /*
  274. * Theoretically, each byte can be transferred in
  275. * 1000000 * 9 / speed usec.
  276. * This time out value is long enough.
  277. */
  278. dev->timeout = 100000000L / speed;
  279. return 0;
  280. }
  281. static const struct dm_i2c_ops uniphier_fi2c_ops = {
  282. .xfer = uniphier_fi2c_xfer,
  283. .set_bus_speed = uniphier_fi2c_set_bus_speed,
  284. };
  285. static const struct udevice_id uniphier_fi2c_of_match[] = {
  286. { .compatible = "panasonic,uniphier-fi2c" },
  287. {},
  288. };
  289. U_BOOT_DRIVER(uniphier_fi2c) = {
  290. .name = "uniphier-fi2c",
  291. .id = UCLASS_I2C,
  292. .of_match = uniphier_fi2c_of_match,
  293. .probe = uniphier_fi2c_probe,
  294. .remove = uniphier_fi2c_remove,
  295. .per_child_auto_alloc_size = sizeof(struct dm_i2c_chip),
  296. .child_pre_probe = uniphier_fi2c_child_pre_probe,
  297. .priv_auto_alloc_size = sizeof(struct uniphier_fi2c_dev),
  298. .ops = &uniphier_fi2c_ops,
  299. };