2 * Copyright (C) 2014-2015 Masahiro Yamada <yamada.masahiro@socionext.com>
4 * SPDX-License-Identifier: GPL-2.0+
8 #include <linux/types.h>
10 #include <asm/errno.h>
11 #include <dm/device.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 struct uniphier_i2c_regs {
20 u32 dtrm; /* data transmission */
21 #define I2C_DTRM_STA (1 << 10)
22 #define I2C_DTRM_STO (1 << 9)
23 #define I2C_DTRM_NACK (1 << 8)
24 #define I2C_DTRM_RD (1 << 0)
25 u32 drec; /* data reception */
26 #define I2C_DREC_STS (1 << 12)
27 #define I2C_DREC_LRB (1 << 11)
28 #define I2C_DREC_LAB (1 << 9)
29 u32 myad; /* slave address */
30 u32 clk; /* clock frequency control */
31 u32 brst; /* bus reset */
32 #define I2C_BRST_FOEN (1 << 1)
33 #define I2C_BRST_BRST (1 << 0)
34 u32 hold; /* hold time control */
35 u32 bsts; /* bus status monitor */
36 u32 noise; /* noise filter control */
37 u32 setup; /* setup time control */
40 #define IOBUS_FREQ 100000000
42 struct uniphier_i2c_dev {
43 struct uniphier_i2c_regs __iomem *regs; /* register base */
44 unsigned long input_clk; /* master clock (Hz) */
45 unsigned long wait_us; /* wait for every byte transfer (us) */
48 static int uniphier_i2c_probe(struct udevice *dev)
52 struct uniphier_i2c_dev *priv = dev_get_priv(dev);
54 addr = fdtdec_get_addr_size(gd->fdt_blob, dev->of_offset, "reg", &size);
56 priv->regs = map_sysmem(addr, size);
61 priv->input_clk = IOBUS_FREQ;
64 writel(0x3, &priv->regs->brst);
69 static int uniphier_i2c_remove(struct udevice *dev)
71 struct uniphier_i2c_dev *priv = dev_get_priv(dev);
73 unmap_sysmem(priv->regs);
78 static int send_and_recv_byte(struct uniphier_i2c_dev *dev, u32 dtrm)
80 writel(dtrm, &dev->regs->dtrm);
83 * This controller only provides interruption to inform the completion
84 * of each byte transfer. (No status register to poll it.)
85 * Unfortunately, U-Boot does not have a good support of interrupt.
90 return readl(&dev->regs->drec);
93 static int send_byte(struct uniphier_i2c_dev *dev, u32 dtrm, bool *stop)
98 drec = send_and_recv_byte(dev, dtrm);
100 if (drec & I2C_DREC_LAB) {
101 debug("uniphier_i2c: bus arbitration failed\n");
105 if (drec & I2C_DREC_LRB) {
106 debug("uniphier_i2c: slave did not return ACK\n");
112 static int uniphier_i2c_transmit(struct uniphier_i2c_dev *dev, uint addr,
113 uint len, const u8 *buf, bool *stop)
117 debug("%s: addr = %x, len = %d\n", __func__, addr, len);
119 ret = send_byte(dev, I2C_DTRM_STA | I2C_DTRM_NACK | addr << 1, stop);
124 ret = send_byte(dev, I2C_DTRM_NACK | *buf++, stop);
131 writel(I2C_DTRM_STO | I2C_DTRM_NACK, &dev->regs->dtrm);
136 static int uniphier_i2c_receive(struct uniphier_i2c_dev *dev, uint addr,
137 uint len, u8 *buf, bool *stop)
141 debug("%s: addr = %x, len = %d\n", __func__, addr, len);
143 ret = send_byte(dev, I2C_DTRM_STA | I2C_DTRM_NACK |
144 I2C_DTRM_RD | addr << 1, stop);
149 *buf++ = send_and_recv_byte(dev, len ? 0 : I2C_DTRM_NACK);
153 writel(I2C_DTRM_STO | I2C_DTRM_NACK, &dev->regs->dtrm);
158 static int uniphier_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
162 struct uniphier_i2c_dev *dev = dev_get_priv(bus);
165 for (; nmsgs > 0; nmsgs--, msg++) {
166 /* If next message is read, skip the stop condition */
167 stop = nmsgs > 1 && msg[1].flags & I2C_M_RD ? false : true;
169 if (msg->flags & I2C_M_RD)
170 ret = uniphier_i2c_receive(dev, msg->addr, msg->len,
173 ret = uniphier_i2c_transmit(dev, msg->addr, msg->len,
183 static int uniphier_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
185 struct uniphier_i2c_dev *priv = dev_get_priv(bus);
187 /* max supported frequency is 400 kHz */
191 /* bus reset: make sure the bus is idle when change the frequency */
192 writel(0x1, &priv->regs->brst);
194 writel((priv->input_clk / speed / 2 << 16) | (priv->input_clk / speed),
197 writel(0x3, &priv->regs->brst);
200 * Theoretically, each byte can be transferred in
201 * 1000000 * 9 / speed usec. For safety, wait more than double.
203 priv->wait_us = 20000000 / speed;
209 static const struct dm_i2c_ops uniphier_i2c_ops = {
210 .xfer = uniphier_i2c_xfer,
211 .set_bus_speed = uniphier_i2c_set_bus_speed,
214 static const struct udevice_id uniphier_i2c_of_match[] = {
215 { .compatible = "socionext,uniphier-i2c" },
219 U_BOOT_DRIVER(uniphier_i2c) = {
220 .name = "uniphier-i2c",
222 .of_match = uniphier_i2c_of_match,
223 .probe = uniphier_i2c_probe,
224 .remove = uniphier_i2c_remove,
225 .priv_auto_alloc_size = sizeof(struct uniphier_i2c_dev),
226 .ops = &uniphier_i2c_ops,