i2c-uniphier.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (C) 2014 Panasonic Corporation
  3. * Copyright (C) 2015 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 <asm/io.h>
  11. #include <asm/errno.h>
  12. #include <dm/device.h>
  13. #include <dm/root.h>
  14. #include <i2c.h>
  15. #include <fdtdec.h>
  16. #include <mapmem.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. struct uniphier_i2c_regs {
  19. u32 dtrm; /* data transmission */
  20. #define I2C_DTRM_STA (1 << 10)
  21. #define I2C_DTRM_STO (1 << 9)
  22. #define I2C_DTRM_NACK (1 << 8)
  23. #define I2C_DTRM_RD (1 << 0)
  24. u32 drec; /* data reception */
  25. #define I2C_DREC_STS (1 << 12)
  26. #define I2C_DREC_LRB (1 << 11)
  27. #define I2C_DREC_LAB (1 << 9)
  28. u32 myad; /* slave address */
  29. u32 clk; /* clock frequency control */
  30. u32 brst; /* bus reset */
  31. #define I2C_BRST_FOEN (1 << 1)
  32. #define I2C_BRST_BRST (1 << 0)
  33. u32 hold; /* hold time control */
  34. u32 bsts; /* bus status monitor */
  35. u32 noise; /* noise filter control */
  36. u32 setup; /* setup time control */
  37. };
  38. #define IOBUS_FREQ 100000000
  39. struct uniphier_i2c_dev {
  40. struct uniphier_i2c_regs __iomem *regs; /* register base */
  41. unsigned long input_clk; /* master clock (Hz) */
  42. unsigned long wait_us; /* wait for every byte transfer (us) */
  43. };
  44. static int uniphier_i2c_probe(struct udevice *dev)
  45. {
  46. fdt_addr_t addr;
  47. fdt_size_t size;
  48. struct uniphier_i2c_dev *priv = dev_get_priv(dev);
  49. addr = fdtdec_get_addr_size(gd->fdt_blob, dev->of_offset, "reg", &size);
  50. priv->regs = map_sysmem(addr, size);
  51. if (!priv->regs)
  52. return -ENOMEM;
  53. priv->input_clk = IOBUS_FREQ;
  54. /* deassert reset */
  55. writel(0x3, &priv->regs->brst);
  56. return 0;
  57. }
  58. static int uniphier_i2c_remove(struct udevice *dev)
  59. {
  60. struct uniphier_i2c_dev *priv = dev_get_priv(dev);
  61. unmap_sysmem(priv->regs);
  62. return 0;
  63. }
  64. static int send_and_recv_byte(struct uniphier_i2c_dev *dev, u32 dtrm)
  65. {
  66. writel(dtrm, &dev->regs->dtrm);
  67. /*
  68. * This controller only provides interruption to inform the completion
  69. * of each byte transfer. (No status register to poll it.)
  70. * Unfortunately, U-Boot does not have a good support of interrupt.
  71. * Wait for a while.
  72. */
  73. udelay(dev->wait_us);
  74. return readl(&dev->regs->drec);
  75. }
  76. static int send_byte(struct uniphier_i2c_dev *dev, u32 dtrm, bool *stop)
  77. {
  78. int ret = 0;
  79. u32 drec;
  80. drec = send_and_recv_byte(dev, dtrm);
  81. if (drec & I2C_DREC_LAB) {
  82. debug("uniphier_i2c: bus arbitration failed\n");
  83. *stop = false;
  84. ret = -EREMOTEIO;
  85. }
  86. if (drec & I2C_DREC_LRB) {
  87. debug("uniphier_i2c: slave did not return ACK\n");
  88. ret = -EREMOTEIO;
  89. }
  90. return ret;
  91. }
  92. static int uniphier_i2c_transmit(struct uniphier_i2c_dev *dev, uint addr,
  93. uint len, const u8 *buf, bool *stop)
  94. {
  95. int ret;
  96. debug("%s: addr = %x, len = %d\n", __func__, addr, len);
  97. ret = send_byte(dev, I2C_DTRM_STA | I2C_DTRM_NACK | addr << 1, stop);
  98. if (ret < 0)
  99. goto fail;
  100. while (len--) {
  101. ret = send_byte(dev, I2C_DTRM_NACK | *buf++, stop);
  102. if (ret < 0)
  103. goto fail;
  104. }
  105. fail:
  106. if (*stop)
  107. writel(I2C_DTRM_STO | I2C_DTRM_NACK, &dev->regs->dtrm);
  108. return ret;
  109. }
  110. static int uniphier_i2c_receive(struct uniphier_i2c_dev *dev, uint addr,
  111. uint len, u8 *buf, bool *stop)
  112. {
  113. int ret;
  114. debug("%s: addr = %x, len = %d\n", __func__, addr, len);
  115. ret = send_byte(dev, I2C_DTRM_STA | I2C_DTRM_NACK |
  116. I2C_DTRM_RD | addr << 1, stop);
  117. if (ret < 0)
  118. goto fail;
  119. while (len--)
  120. *buf++ = send_and_recv_byte(dev, len ? 0 : I2C_DTRM_NACK);
  121. fail:
  122. if (*stop)
  123. writel(I2C_DTRM_STO | I2C_DTRM_NACK, &dev->regs->dtrm);
  124. return ret;
  125. }
  126. static int uniphier_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
  127. int nmsgs)
  128. {
  129. int ret = 0;
  130. struct uniphier_i2c_dev *dev = dev_get_priv(bus);
  131. bool stop;
  132. for (; nmsgs > 0; nmsgs--, msg++) {
  133. /* If next message is read, skip the stop condition */
  134. stop = nmsgs > 1 && msg[1].flags & I2C_M_RD ? false : true;
  135. if (msg->flags & I2C_M_RD)
  136. ret = uniphier_i2c_receive(dev, msg->addr, msg->len,
  137. msg->buf, &stop);
  138. else
  139. ret = uniphier_i2c_transmit(dev, msg->addr, msg->len,
  140. msg->buf, &stop);
  141. if (ret < 0)
  142. break;
  143. }
  144. return ret;
  145. }
  146. static int uniphier_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
  147. {
  148. struct uniphier_i2c_dev *priv = dev_get_priv(bus);
  149. /* max supported frequency is 400 kHz */
  150. if (speed > 400000)
  151. return -EINVAL;
  152. /* bus reset: make sure the bus is idle when change the frequency */
  153. writel(0x1, &priv->regs->brst);
  154. writel((priv->input_clk / speed / 2 << 16) | (priv->input_clk / speed),
  155. &priv->regs->clk);
  156. writel(0x3, &priv->regs->brst);
  157. /*
  158. * Theoretically, each byte can be transferred in
  159. * 1000000 * 9 / speed usec. For safety, wait more than double.
  160. */
  161. priv->wait_us = 20000000 / speed;
  162. return 0;
  163. }
  164. static const struct dm_i2c_ops uniphier_i2c_ops = {
  165. .xfer = uniphier_i2c_xfer,
  166. .set_bus_speed = uniphier_i2c_set_bus_speed,
  167. };
  168. static const struct udevice_id uniphier_i2c_of_match[] = {
  169. { .compatible = "socionext,uniphier-i2c" },
  170. { /* sentinel */ }
  171. };
  172. U_BOOT_DRIVER(uniphier_i2c) = {
  173. .name = "uniphier-i2c",
  174. .id = UCLASS_I2C,
  175. .of_match = uniphier_i2c_of_match,
  176. .probe = uniphier_i2c_probe,
  177. .remove = uniphier_i2c_remove,
  178. .priv_auto_alloc_size = sizeof(struct uniphier_i2c_dev),
  179. .ops = &uniphier_i2c_ops,
  180. };