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 <linux/delay.h>
9 #include <linux/errno.h>
11 #include <linux/sizes.h>
12 #include <linux/types.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 */
38 #define IOBUS_FREQ 100000000
40 struct uniphier_i2c_priv {
42 struct uniphier_i2c_regs __iomem *regs; /* register base */
43 unsigned long input_clk; /* master clock (Hz) */
44 unsigned long wait_us; /* wait for every byte transfer (us) */
47 static int uniphier_i2c_probe(struct udevice *dev)
50 struct uniphier_i2c_priv *priv = dev_get_priv(dev);
52 addr = devfdt_get_addr(dev);
53 if (addr == FDT_ADDR_T_NONE)
56 priv->regs = devm_ioremap(dev, addr, SZ_64);
60 priv->input_clk = IOBUS_FREQ;
65 writel(0x3, &priv->regs->brst);
70 static int send_and_recv_byte(struct uniphier_i2c_priv *priv, u32 dtrm)
72 writel(dtrm, &priv->regs->dtrm);
75 * This controller only provides interruption to inform the completion
76 * of each byte transfer. (No status register to poll it.)
77 * Unfortunately, U-Boot does not have a good support of interrupt.
80 udelay(priv->wait_us);
82 return readl(&priv->regs->drec);
85 static int send_byte(struct uniphier_i2c_priv *priv, u32 dtrm, bool *stop)
90 drec = send_and_recv_byte(priv, dtrm);
92 if (drec & I2C_DREC_LAB) {
93 dev_dbg(priv->dev, "uniphier_i2c: bus arbitration failed\n");
97 if (drec & I2C_DREC_LRB) {
98 dev_dbg(priv->dev, "uniphier_i2c: slave did not return ACK\n");
104 static int uniphier_i2c_transmit(struct uniphier_i2c_priv *priv, uint addr,
105 uint len, const u8 *buf, bool *stop)
109 dev_dbg(priv->dev, "%s: addr = %x, len = %d\n", __func__, addr, len);
111 ret = send_byte(priv, I2C_DTRM_STA | I2C_DTRM_NACK | addr << 1, stop);
116 ret = send_byte(priv, I2C_DTRM_NACK | *buf++, stop);
123 writel(I2C_DTRM_STO | I2C_DTRM_NACK, &priv->regs->dtrm);
128 static int uniphier_i2c_receive(struct uniphier_i2c_priv *priv, uint addr,
129 uint len, u8 *buf, bool *stop)
133 dev_dbg(priv->dev, "%s: addr = %x, len = %d\n", __func__, addr, len);
135 ret = send_byte(priv, I2C_DTRM_STA | I2C_DTRM_NACK |
136 I2C_DTRM_RD | addr << 1, stop);
141 *buf++ = send_and_recv_byte(priv, len ? 0 : I2C_DTRM_NACK);
145 writel(I2C_DTRM_STO | I2C_DTRM_NACK, &priv->regs->dtrm);
150 static int uniphier_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
154 struct uniphier_i2c_priv *priv = dev_get_priv(bus);
157 for (; nmsgs > 0; nmsgs--, msg++) {
158 /* If next message is read, skip the stop condition */
159 stop = nmsgs > 1 && msg[1].flags & I2C_M_RD ? false : true;
161 if (msg->flags & I2C_M_RD)
162 ret = uniphier_i2c_receive(priv, msg->addr, msg->len,
165 ret = uniphier_i2c_transmit(priv, msg->addr, msg->len,
175 static int uniphier_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
177 struct uniphier_i2c_priv *priv = dev_get_priv(bus);
179 /* max supported frequency is 400 kHz */
183 /* bus reset: make sure the bus is idle when change the frequency */
184 writel(0x1, &priv->regs->brst);
186 writel((priv->input_clk / speed / 2 << 16) | (priv->input_clk / speed),
189 writel(0x3, &priv->regs->brst);
192 * Theoretically, each byte can be transferred in
193 * 1000000 * 9 / speed usec. For safety, wait more than double.
195 priv->wait_us = 20000000 / speed;
201 static const struct dm_i2c_ops uniphier_i2c_ops = {
202 .xfer = uniphier_i2c_xfer,
203 .set_bus_speed = uniphier_i2c_set_bus_speed,
206 static const struct udevice_id uniphier_i2c_of_match[] = {
207 { .compatible = "socionext,uniphier-i2c" },
211 U_BOOT_DRIVER(uniphier_i2c) = {
212 .name = "uniphier-i2c",
214 .of_match = uniphier_i2c_of_match,
215 .probe = uniphier_i2c_probe,
216 .priv_auto_alloc_size = sizeof(struct uniphier_i2c_priv),
217 .ops = &uniphier_i2c_ops,