1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2014 Panasonic Corporation
4 * Copyright (C) 2015-2016 Socionext Inc.
5 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
8 #include <dm/device_compat.h>
9 #include <linux/delay.h>
10 #include <linux/errno.h>
12 #include <linux/sizes.h>
13 #include <linux/types.h>
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 */
39 #define IOBUS_FREQ 100000000
41 struct uniphier_i2c_priv {
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)
51 struct uniphier_i2c_priv *priv = dev_get_priv(dev);
53 addr = dev_read_addr(dev);
54 if (addr == FDT_ADDR_T_NONE)
57 priv->regs = devm_ioremap(dev, addr, SZ_64);
61 priv->input_clk = IOBUS_FREQ;
66 writel(0x3, &priv->regs->brst);
71 static int send_and_recv_byte(struct uniphier_i2c_priv *priv, u32 dtrm)
73 writel(dtrm, &priv->regs->dtrm);
76 * This controller only provides interruption to inform the completion
77 * of each byte transfer. (No status register to poll it.)
78 * Unfortunately, U-Boot does not have a good support of interrupt.
81 udelay(priv->wait_us);
83 return readl(&priv->regs->drec);
86 static int send_byte(struct uniphier_i2c_priv *priv, u32 dtrm, bool *stop)
91 drec = send_and_recv_byte(priv, dtrm);
93 if (drec & I2C_DREC_LAB) {
94 dev_dbg(priv->dev, "uniphier_i2c: bus arbitration failed\n");
98 if (drec & I2C_DREC_LRB) {
99 dev_dbg(priv->dev, "uniphier_i2c: slave did not return ACK\n");
105 static int uniphier_i2c_transmit(struct uniphier_i2c_priv *priv, uint addr,
106 uint len, const u8 *buf, bool *stop)
110 dev_dbg(priv->dev, "%s: addr = %x, len = %d\n", __func__, addr, len);
112 ret = send_byte(priv, I2C_DTRM_STA | I2C_DTRM_NACK | addr << 1, stop);
117 ret = send_byte(priv, I2C_DTRM_NACK | *buf++, stop);
124 writel(I2C_DTRM_STO | I2C_DTRM_NACK, &priv->regs->dtrm);
129 static int uniphier_i2c_receive(struct uniphier_i2c_priv *priv, uint addr,
130 uint len, u8 *buf, bool *stop)
134 dev_dbg(priv->dev, "%s: addr = %x, len = %d\n", __func__, addr, len);
136 ret = send_byte(priv, I2C_DTRM_STA | I2C_DTRM_NACK |
137 I2C_DTRM_RD | addr << 1, stop);
142 *buf++ = send_and_recv_byte(priv, len ? 0 : I2C_DTRM_NACK);
146 writel(I2C_DTRM_STO | I2C_DTRM_NACK, &priv->regs->dtrm);
151 static int uniphier_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
155 struct uniphier_i2c_priv *priv = dev_get_priv(bus);
158 for (; nmsgs > 0; nmsgs--, msg++) {
159 /* If next message is read, skip the stop condition */
160 stop = nmsgs > 1 && msg[1].flags & I2C_M_RD ? false : true;
162 if (msg->flags & I2C_M_RD)
163 ret = uniphier_i2c_receive(priv, msg->addr, msg->len,
166 ret = uniphier_i2c_transmit(priv, msg->addr, msg->len,
176 static int uniphier_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
178 struct uniphier_i2c_priv *priv = dev_get_priv(bus);
180 /* max supported frequency is 400 kHz */
181 if (speed > I2C_SPEED_FAST_RATE)
184 /* bus reset: make sure the bus is idle when change the frequency */
185 writel(0x1, &priv->regs->brst);
187 writel((priv->input_clk / speed / 2 << 16) | (priv->input_clk / speed),
190 writel(0x3, &priv->regs->brst);
193 * Theoretically, each byte can be transferred in
194 * 1000000 * 9 / speed usec. For safety, wait more than double.
196 priv->wait_us = 20000000 / speed;
202 static const struct dm_i2c_ops uniphier_i2c_ops = {
203 .xfer = uniphier_i2c_xfer,
204 .set_bus_speed = uniphier_i2c_set_bus_speed,
207 static const struct udevice_id uniphier_i2c_of_match[] = {
208 { .compatible = "socionext,uniphier-i2c" },
212 U_BOOT_DRIVER(uniphier_i2c) = {
213 .name = "uniphier-i2c",
215 .of_match = uniphier_i2c_of_match,
216 .probe = uniphier_i2c_probe,
217 .priv_auto = sizeof(struct uniphier_i2c_priv),
218 .ops = &uniphier_i2c_ops,