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_fi2c_regs {
20 u32 cr; /* control register */
21 #define I2C_CR_MST (1 << 3) /* master mode */
22 #define I2C_CR_STA (1 << 2) /* start condition */
23 #define I2C_CR_STO (1 << 1) /* stop condition */
24 #define I2C_CR_NACK (1 << 0) /* not ACK */
25 u32 dttx; /* send FIFO (write-only) */
26 #define dtrx dttx /* receive FIFO (read-only) */
27 #define I2C_DTTX_CMD (1 << 8) /* send command (slave addr) */
28 #define I2C_DTTX_RD (1 << 0) /* read */
29 u32 __reserved; /* no register at offset 0x08 */
30 u32 slad; /* slave address */
31 u32 cyc; /* clock cycle control */
32 u32 lctl; /* clock low period control */
33 u32 ssut; /* restart/stop setup time control */
34 u32 dsut; /* data setup time control */
35 u32 intr; /* interrupt status */
36 u32 ie; /* interrupt enable */
37 u32 ic; /* interrupt clear */
38 #define I2C_INT_TE (1 << 9) /* TX FIFO empty */
39 #define I2C_INT_RB (1 << 4) /* received specified bytes */
40 #define I2C_INT_NA (1 << 2) /* no answer */
41 #define I2C_INT_AL (1 << 1) /* arbitration lost */
42 u32 sr; /* status register */
43 #define I2C_SR_DB (1 << 12) /* device busy */
44 #define I2C_SR_BB (1 << 8) /* bus busy */
45 #define I2C_SR_RFF (1 << 3) /* Rx FIFO full */
46 #define I2C_SR_RNE (1 << 2) /* Rx FIFO not empty */
47 #define I2C_SR_TNF (1 << 1) /* Tx FIFO not full */
48 #define I2C_SR_TFE (1 << 0) /* Tx FIFO empty */
49 u32 __reserved2; /* no register at offset 0x30 */
50 u32 rst; /* reset control */
51 #define I2C_RST_TBRST (1 << 2) /* clear Tx FIFO */
52 #define I2C_RST_RBRST (1 << 1) /* clear Rx FIFO */
53 #define I2C_RST_RST (1 << 0) /* forcible bus reset */
54 u32 bm; /* bus monitor */
55 u32 noise; /* noise filter control */
56 u32 tbc; /* Tx byte count setting */
57 u32 rbc; /* Rx byte count setting */
58 u32 tbcm; /* Tx byte count monitor */
59 u32 rbcm; /* Rx byte count monitor */
60 u32 brst; /* bus reset */
61 #define I2C_BRST_FOEN (1 << 1) /* normal operation */
62 #define I2C_BRST_RSCLO (1 << 0) /* release SCL low fixing */
65 #define FIOCLK 50000000
67 struct uniphier_fi2c_dev {
68 struct uniphier_fi2c_regs __iomem *regs; /* register base */
69 unsigned long fioclk; /* internal operation clock */
70 unsigned long timeout; /* time out (us) */
73 static int poll_status(u32 __iomem *reg, u32 flag)
75 int wait = 1000000; /* 1 sec is long enough */
77 while (readl(reg) & flag) {
86 static int reset_bus(struct uniphier_fi2c_regs __iomem *regs)
90 /* bus forcible reset */
91 writel(I2C_RST_RST, ®s->rst);
92 ret = poll_status(®s->rst, I2C_RST_RST);
94 debug("error: fail to reset I2C controller\n");
99 static int check_device_busy(struct uniphier_fi2c_regs __iomem *regs)
103 ret = poll_status(®s->sr, I2C_SR_DB);
105 debug("error: device busy too long. reset...\n");
106 ret = reset_bus(regs);
112 static int uniphier_fi2c_probe(struct udevice *dev)
116 struct uniphier_fi2c_dev *priv = dev_get_priv(dev);
119 addr = fdtdec_get_addr_size(gd->fdt_blob, dev->of_offset, "reg",
122 priv->regs = map_sysmem(addr, size);
127 priv->fioclk = FIOCLK;
129 /* bus forcible reset */
130 ret = reset_bus(priv->regs);
134 writel(I2C_BRST_FOEN | I2C_BRST_RSCLO, &priv->regs->brst);
139 static int uniphier_fi2c_remove(struct udevice *dev)
141 struct uniphier_fi2c_dev *priv = dev_get_priv(dev);
143 unmap_sysmem(priv->regs);
148 static int wait_for_irq(struct uniphier_fi2c_dev *dev, u32 flags,
152 unsigned long wait = dev->timeout;
153 int ret = -EREMOTEIO;
157 irq = readl(&dev->regs->intr);
158 } while (!(irq & flags) && wait--);
161 debug("error: time out\n");
165 if (irq & I2C_INT_AL) {
166 debug("error: arbitration lost\n");
171 if (irq & I2C_INT_NA) {
172 debug("error: no answer\n");
179 static int issue_stop(struct uniphier_fi2c_dev *dev, int old_ret)
183 debug("stop condition\n");
184 writel(I2C_CR_MST | I2C_CR_STO, &dev->regs->cr);
186 ret = poll_status(&dev->regs->sr, I2C_SR_DB);
188 debug("error: device busy after operation\n");
190 return old_ret ? old_ret : ret;
193 static int uniphier_fi2c_transmit(struct uniphier_fi2c_dev *dev, uint addr,
194 uint len, const u8 *buf, bool *stop)
197 const u32 irq_flags = I2C_INT_TE | I2C_INT_NA | I2C_INT_AL;
198 struct uniphier_fi2c_regs __iomem *regs = dev->regs;
200 debug("%s: addr = %x, len = %d\n", __func__, addr, len);
202 writel(I2C_DTTX_CMD | addr << 1, ®s->dttx);
204 writel(irq_flags, ®s->ie);
205 writel(irq_flags, ®s->ic);
207 debug("start condition\n");
208 writel(I2C_CR_MST | I2C_CR_STA, ®s->cr);
210 ret = wait_for_irq(dev, irq_flags, stop);
215 debug("sending %x\n", *buf);
216 writel(*buf++, ®s->dttx);
218 writel(irq_flags, ®s->ic);
220 ret = wait_for_irq(dev, irq_flags, stop);
226 writel(irq_flags, ®s->ic);
229 ret = issue_stop(dev, ret);
234 static int uniphier_fi2c_receive(struct uniphier_fi2c_dev *dev, uint addr,
235 uint len, u8 *buf, bool *stop)
238 const u32 irq_flags = I2C_INT_RB | I2C_INT_NA | I2C_INT_AL;
239 struct uniphier_fi2c_regs __iomem *regs = dev->regs;
241 debug("%s: addr = %x, len = %d\n", __func__, addr, len);
244 * In case 'len == 0', only the slave address should be sent
245 * for probing, which is covered by the transmit function.
248 return uniphier_fi2c_transmit(dev, addr, len, buf, stop);
250 writel(I2C_DTTX_CMD | I2C_DTTX_RD | addr << 1, ®s->dttx);
252 writel(0, ®s->rbc);
253 writel(irq_flags, ®s->ie);
254 writel(irq_flags, ®s->ic);
256 debug("start condition\n");
257 writel(I2C_CR_MST | I2C_CR_STA | (len == 1 ? I2C_CR_NACK : 0),
261 ret = wait_for_irq(dev, irq_flags, stop);
265 *buf++ = readl(®s->dtrx);
266 debug("received %x\n", *(buf - 1));
269 writel(I2C_CR_MST | I2C_CR_NACK, ®s->cr);
271 writel(irq_flags, ®s->ic);
275 writel(irq_flags, ®s->ic);
278 ret = issue_stop(dev, ret);
283 static int uniphier_fi2c_xfer(struct udevice *bus, struct i2c_msg *msg,
287 struct uniphier_fi2c_dev *dev = dev_get_priv(bus);
290 ret = check_device_busy(dev->regs);
294 for (; nmsgs > 0; nmsgs--, msg++) {
295 /* If next message is read, skip the stop condition */
296 stop = nmsgs > 1 && msg[1].flags & I2C_M_RD ? false : true;
298 if (msg->flags & I2C_M_RD)
299 ret = uniphier_fi2c_receive(dev, msg->addr, msg->len,
302 ret = uniphier_fi2c_transmit(dev, msg->addr, msg->len,
312 static int uniphier_fi2c_set_bus_speed(struct udevice *bus, unsigned int speed)
315 unsigned int clk_count;
316 struct uniphier_fi2c_dev *dev = dev_get_priv(bus);
317 struct uniphier_fi2c_regs __iomem *regs = dev->regs;
319 /* max supported frequency is 400 kHz */
323 ret = check_device_busy(dev->regs);
327 /* make sure the bus is idle when changing the frequency */
328 writel(I2C_BRST_RSCLO, ®s->brst);
330 clk_count = dev->fioclk / speed;
332 writel(clk_count, ®s->cyc);
333 writel(clk_count / 2, ®s->lctl);
334 writel(clk_count / 2, ®s->ssut);
335 writel(clk_count / 16, ®s->dsut);
337 writel(I2C_BRST_FOEN | I2C_BRST_RSCLO, ®s->brst);
340 * Theoretically, each byte can be transferred in
341 * 1000000 * 9 / speed usec.
342 * This time out value is long enough.
344 dev->timeout = 100000000L / speed;
349 static const struct dm_i2c_ops uniphier_fi2c_ops = {
350 .xfer = uniphier_fi2c_xfer,
351 .set_bus_speed = uniphier_fi2c_set_bus_speed,
354 static const struct udevice_id uniphier_fi2c_of_match[] = {
355 { .compatible = "socionext,uniphier-fi2c" },
359 U_BOOT_DRIVER(uniphier_fi2c) = {
360 .name = "uniphier-fi2c",
362 .of_match = uniphier_fi2c_of_match,
363 .probe = uniphier_fi2c_probe,
364 .remove = uniphier_fi2c_remove,
365 .priv_auto_alloc_size = sizeof(struct uniphier_fi2c_dev),
366 .ops = &uniphier_fi2c_ops,