i2c-uniphier.c 5.4 KB

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