i2c-uniphier.c 5.0 KB

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