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/errno.h>
10 #include <linux/iopoll.h>
11 #include <linux/sizes.h>
12 #include <linux/types.h>
17 struct uniphier_fi2c_regs {
18 u32 cr; /* control register */
19 #define I2C_CR_MST (1 << 3) /* master mode */
20 #define I2C_CR_STA (1 << 2) /* start condition */
21 #define I2C_CR_STO (1 << 1) /* stop condition */
22 #define I2C_CR_NACK (1 << 0) /* not ACK */
23 u32 dttx; /* send FIFO (write-only) */
24 #define dtrx dttx /* receive FIFO (read-only) */
25 #define I2C_DTTX_CMD (1 << 8) /* send command (slave addr) */
26 #define I2C_DTTX_RD (1 << 0) /* read */
27 u32 __reserved; /* no register at offset 0x08 */
28 u32 slad; /* slave address */
29 u32 cyc; /* clock cycle control */
30 u32 lctl; /* clock low period control */
31 u32 ssut; /* restart/stop setup time control */
32 u32 dsut; /* data setup time control */
33 u32 intr; /* interrupt status */
34 u32 ie; /* interrupt enable */
35 u32 ic; /* interrupt clear */
36 #define I2C_INT_TE (1 << 9) /* TX FIFO empty */
37 #define I2C_INT_RB (1 << 4) /* received specified bytes */
38 #define I2C_INT_NA (1 << 2) /* no answer */
39 #define I2C_INT_AL (1 << 1) /* arbitration lost */
40 u32 sr; /* status register */
41 #define I2C_SR_DB (1 << 12) /* device busy */
42 #define I2C_SR_BB (1 << 8) /* bus busy */
43 #define I2C_SR_RFF (1 << 3) /* Rx FIFO full */
44 #define I2C_SR_RNE (1 << 2) /* Rx FIFO not empty */
45 #define I2C_SR_TNF (1 << 1) /* Tx FIFO not full */
46 #define I2C_SR_TFE (1 << 0) /* Tx FIFO empty */
47 u32 __reserved2; /* no register at offset 0x30 */
48 u32 rst; /* reset control */
49 #define I2C_RST_TBRST (1 << 2) /* clear Tx FIFO */
50 #define I2C_RST_RBRST (1 << 1) /* clear Rx FIFO */
51 #define I2C_RST_RST (1 << 0) /* forcible bus reset */
52 u32 bm; /* bus monitor */
53 u32 noise; /* noise filter control */
54 u32 tbc; /* Tx byte count setting */
55 u32 rbc; /* Rx byte count setting */
56 u32 tbcm; /* Tx byte count monitor */
57 u32 rbcm; /* Rx byte count monitor */
58 u32 brst; /* bus reset */
59 #define I2C_BRST_FOEN (1 << 1) /* normal operation */
60 #define I2C_BRST_RSCLO (1 << 0) /* release SCL low fixing */
63 #define FIOCLK 50000000
65 struct uniphier_fi2c_priv {
67 struct uniphier_fi2c_regs __iomem *regs; /* register base */
68 unsigned long fioclk; /* internal operation clock */
69 unsigned long timeout; /* time out (us) */
72 static void uniphier_fi2c_reset(struct uniphier_fi2c_priv *priv)
74 writel(I2C_RST_RST, &priv->regs->rst);
77 static int uniphier_fi2c_check_bus_busy(struct uniphier_fi2c_priv *priv)
82 ret = readl_poll_timeout(&priv->regs->sr, val, !(val & I2C_SR_DB), 100);
84 dev_dbg(priv->dev, "error: device busy too long. reset...\n");
85 uniphier_fi2c_reset(priv);
91 static int uniphier_fi2c_probe(struct udevice *dev)
94 struct uniphier_fi2c_priv *priv = dev_get_priv(dev);
96 addr = devfdt_get_addr(dev);
97 if (addr == FDT_ADDR_T_NONE)
100 priv->regs = devm_ioremap(dev, addr, SZ_128);
104 priv->fioclk = FIOCLK;
108 /* bus forcible reset */
109 uniphier_fi2c_reset(priv);
111 writel(I2C_BRST_FOEN | I2C_BRST_RSCLO, &priv->regs->brst);
116 static int wait_for_irq(struct uniphier_fi2c_priv *priv, u32 flags,
122 ret = readl_poll_timeout(&priv->regs->intr, irq, irq & flags,
125 dev_dbg(priv->dev, "error: time out\n");
129 if (irq & I2C_INT_AL) {
130 dev_dbg(priv->dev, "error: arbitration lost\n");
135 if (irq & I2C_INT_NA) {
136 dev_dbg(priv->dev, "error: no answer\n");
143 static int issue_stop(struct uniphier_fi2c_priv *priv, int old_ret)
147 dev_dbg(priv->dev, "stop condition\n");
148 writel(I2C_CR_MST | I2C_CR_STO, &priv->regs->cr);
150 ret = uniphier_fi2c_check_bus_busy(priv);
152 dev_dbg(priv->dev, "error: device busy after operation\n");
154 return old_ret ? old_ret : ret;
157 static int uniphier_fi2c_transmit(struct uniphier_fi2c_priv *priv, uint addr,
158 uint len, const u8 *buf, bool *stop)
161 const u32 irq_flags = I2C_INT_TE | I2C_INT_NA | I2C_INT_AL;
162 struct uniphier_fi2c_regs __iomem *regs = priv->regs;
164 dev_dbg(priv->dev, "%s: addr = %x, len = %d\n", __func__, addr, len);
166 writel(I2C_DTTX_CMD | addr << 1, ®s->dttx);
168 writel(irq_flags, ®s->ie);
169 writel(irq_flags, ®s->ic);
171 dev_dbg(priv->dev, "start condition\n");
172 writel(I2C_CR_MST | I2C_CR_STA, ®s->cr);
174 ret = wait_for_irq(priv, irq_flags, stop);
179 dev_dbg(priv->dev, "sending %x\n", *buf);
180 writel(*buf++, ®s->dttx);
182 writel(irq_flags, ®s->ic);
184 ret = wait_for_irq(priv, irq_flags, stop);
190 writel(irq_flags, ®s->ic);
193 ret = issue_stop(priv, ret);
198 static int uniphier_fi2c_receive(struct uniphier_fi2c_priv *priv, uint addr,
199 uint len, u8 *buf, bool *stop)
202 const u32 irq_flags = I2C_INT_RB | I2C_INT_NA | I2C_INT_AL;
203 struct uniphier_fi2c_regs __iomem *regs = priv->regs;
205 dev_dbg(priv->dev, "%s: addr = %x, len = %d\n", __func__, addr, len);
208 * In case 'len == 0', only the slave address should be sent
209 * for probing, which is covered by the transmit function.
212 return uniphier_fi2c_transmit(priv, addr, len, buf, stop);
214 writel(I2C_DTTX_CMD | I2C_DTTX_RD | addr << 1, ®s->dttx);
216 writel(0, ®s->rbc);
217 writel(irq_flags, ®s->ie);
218 writel(irq_flags, ®s->ic);
220 dev_dbg(priv->dev, "start condition\n");
221 writel(I2C_CR_MST | I2C_CR_STA | (len == 1 ? I2C_CR_NACK : 0),
225 ret = wait_for_irq(priv, irq_flags, stop);
229 *buf++ = readl(®s->dtrx);
230 dev_dbg(priv->dev, "received %x\n", *(buf - 1));
233 writel(I2C_CR_MST | I2C_CR_NACK, ®s->cr);
235 writel(irq_flags, ®s->ic);
239 writel(irq_flags, ®s->ic);
242 ret = issue_stop(priv, ret);
247 static int uniphier_fi2c_xfer(struct udevice *bus, struct i2c_msg *msg,
251 struct uniphier_fi2c_priv *priv = dev_get_priv(bus);
254 ret = uniphier_fi2c_check_bus_busy(priv);
258 for (; nmsgs > 0; nmsgs--, msg++) {
259 /* If next message is read, skip the stop condition */
260 stop = nmsgs > 1 && msg[1].flags & I2C_M_RD ? false : true;
262 if (msg->flags & I2C_M_RD)
263 ret = uniphier_fi2c_receive(priv, msg->addr, msg->len,
266 ret = uniphier_fi2c_transmit(priv, msg->addr, msg->len,
276 static int uniphier_fi2c_set_bus_speed(struct udevice *bus, unsigned int speed)
279 unsigned int clk_count;
280 struct uniphier_fi2c_priv *priv = dev_get_priv(bus);
281 struct uniphier_fi2c_regs __iomem *regs = priv->regs;
283 /* max supported frequency is 400 kHz */
287 ret = uniphier_fi2c_check_bus_busy(priv);
291 /* make sure the bus is idle when changing the frequency */
292 writel(I2C_BRST_RSCLO, ®s->brst);
294 clk_count = priv->fioclk / speed;
296 writel(clk_count, ®s->cyc);
297 writel(clk_count / 2, ®s->lctl);
298 writel(clk_count / 2, ®s->ssut);
299 writel(clk_count / 16, ®s->dsut);
301 writel(I2C_BRST_FOEN | I2C_BRST_RSCLO, ®s->brst);
304 * Theoretically, each byte can be transferred in
305 * 1000000 * 9 / speed usec.
306 * This time out value is long enough.
308 priv->timeout = 100000000L / speed;
313 static const struct dm_i2c_ops uniphier_fi2c_ops = {
314 .xfer = uniphier_fi2c_xfer,
315 .set_bus_speed = uniphier_fi2c_set_bus_speed,
318 static const struct udevice_id uniphier_fi2c_of_match[] = {
319 { .compatible = "socionext,uniphier-fi2c" },
323 U_BOOT_DRIVER(uniphier_fi2c) = {
324 .name = "uniphier-fi2c",
326 .of_match = uniphier_fi2c_of_match,
327 .probe = uniphier_fi2c_probe,
328 .priv_auto_alloc_size = sizeof(struct uniphier_fi2c_priv),
329 .ops = &uniphier_fi2c_ops,