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 <linux/sizes.h>
11 #include <asm/errno.h>
12 #include <dm/device.h>
18 struct uniphier_fi2c_regs {
19 u32 cr; /* control register */
20 #define I2C_CR_MST (1 << 3) /* master mode */
21 #define I2C_CR_STA (1 << 2) /* start condition */
22 #define I2C_CR_STO (1 << 1) /* stop condition */
23 #define I2C_CR_NACK (1 << 0) /* not ACK */
24 u32 dttx; /* send FIFO (write-only) */
25 #define dtrx dttx /* receive FIFO (read-only) */
26 #define I2C_DTTX_CMD (1 << 8) /* send command (slave addr) */
27 #define I2C_DTTX_RD (1 << 0) /* read */
28 u32 __reserved; /* no register at offset 0x08 */
29 u32 slad; /* slave address */
30 u32 cyc; /* clock cycle control */
31 u32 lctl; /* clock low period control */
32 u32 ssut; /* restart/stop setup time control */
33 u32 dsut; /* data setup time control */
34 u32 intr; /* interrupt status */
35 u32 ie; /* interrupt enable */
36 u32 ic; /* interrupt clear */
37 #define I2C_INT_TE (1 << 9) /* TX FIFO empty */
38 #define I2C_INT_RB (1 << 4) /* received specified bytes */
39 #define I2C_INT_NA (1 << 2) /* no answer */
40 #define I2C_INT_AL (1 << 1) /* arbitration lost */
41 u32 sr; /* status register */
42 #define I2C_SR_DB (1 << 12) /* device busy */
43 #define I2C_SR_BB (1 << 8) /* bus busy */
44 #define I2C_SR_RFF (1 << 3) /* Rx FIFO full */
45 #define I2C_SR_RNE (1 << 2) /* Rx FIFO not empty */
46 #define I2C_SR_TNF (1 << 1) /* Tx FIFO not full */
47 #define I2C_SR_TFE (1 << 0) /* Tx FIFO empty */
48 u32 __reserved2; /* no register at offset 0x30 */
49 u32 rst; /* reset control */
50 #define I2C_RST_TBRST (1 << 2) /* clear Tx FIFO */
51 #define I2C_RST_RBRST (1 << 1) /* clear Rx FIFO */
52 #define I2C_RST_RST (1 << 0) /* forcible bus reset */
53 u32 bm; /* bus monitor */
54 u32 noise; /* noise filter control */
55 u32 tbc; /* Tx byte count setting */
56 u32 rbc; /* Rx byte count setting */
57 u32 tbcm; /* Tx byte count monitor */
58 u32 rbcm; /* Rx byte count monitor */
59 u32 brst; /* bus reset */
60 #define I2C_BRST_FOEN (1 << 1) /* normal operation */
61 #define I2C_BRST_RSCLO (1 << 0) /* release SCL low fixing */
64 #define FIOCLK 50000000
66 struct uniphier_fi2c_dev {
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 int poll_status(u32 __iomem *reg, u32 flag)
74 int wait = 1000000; /* 1 sec is long enough */
76 while (readl(reg) & flag) {
85 static int reset_bus(struct uniphier_fi2c_regs __iomem *regs)
89 /* bus forcible reset */
90 writel(I2C_RST_RST, ®s->rst);
91 ret = poll_status(®s->rst, I2C_RST_RST);
93 debug("error: fail to reset I2C controller\n");
98 static int check_device_busy(struct uniphier_fi2c_regs __iomem *regs)
102 ret = poll_status(®s->sr, I2C_SR_DB);
104 debug("error: device busy too long. reset...\n");
105 ret = reset_bus(regs);
111 static int uniphier_fi2c_probe(struct udevice *dev)
114 struct uniphier_fi2c_dev *priv = dev_get_priv(dev);
117 addr = dev_get_addr(dev);
118 if (addr == FDT_ADDR_T_NONE)
121 priv->regs = map_sysmem(addr, SZ_128);
125 priv->fioclk = FIOCLK;
127 /* bus forcible reset */
128 ret = reset_bus(priv->regs);
132 writel(I2C_BRST_FOEN | I2C_BRST_RSCLO, &priv->regs->brst);
137 static int uniphier_fi2c_remove(struct udevice *dev)
139 struct uniphier_fi2c_dev *priv = dev_get_priv(dev);
141 unmap_sysmem(priv->regs);
146 static int wait_for_irq(struct uniphier_fi2c_dev *dev, u32 flags,
150 unsigned long wait = dev->timeout;
151 int ret = -EREMOTEIO;
155 irq = readl(&dev->regs->intr);
156 } while (!(irq & flags) && wait--);
159 debug("error: time out\n");
163 if (irq & I2C_INT_AL) {
164 debug("error: arbitration lost\n");
169 if (irq & I2C_INT_NA) {
170 debug("error: no answer\n");
177 static int issue_stop(struct uniphier_fi2c_dev *dev, int old_ret)
181 debug("stop condition\n");
182 writel(I2C_CR_MST | I2C_CR_STO, &dev->regs->cr);
184 ret = poll_status(&dev->regs->sr, I2C_SR_DB);
186 debug("error: device busy after operation\n");
188 return old_ret ? old_ret : ret;
191 static int uniphier_fi2c_transmit(struct uniphier_fi2c_dev *dev, uint addr,
192 uint len, const u8 *buf, bool *stop)
195 const u32 irq_flags = I2C_INT_TE | I2C_INT_NA | I2C_INT_AL;
196 struct uniphier_fi2c_regs __iomem *regs = dev->regs;
198 debug("%s: addr = %x, len = %d\n", __func__, addr, len);
200 writel(I2C_DTTX_CMD | addr << 1, ®s->dttx);
202 writel(irq_flags, ®s->ie);
203 writel(irq_flags, ®s->ic);
205 debug("start condition\n");
206 writel(I2C_CR_MST | I2C_CR_STA, ®s->cr);
208 ret = wait_for_irq(dev, irq_flags, stop);
213 debug("sending %x\n", *buf);
214 writel(*buf++, ®s->dttx);
216 writel(irq_flags, ®s->ic);
218 ret = wait_for_irq(dev, irq_flags, stop);
224 writel(irq_flags, ®s->ic);
227 ret = issue_stop(dev, ret);
232 static int uniphier_fi2c_receive(struct uniphier_fi2c_dev *dev, uint addr,
233 uint len, u8 *buf, bool *stop)
236 const u32 irq_flags = I2C_INT_RB | I2C_INT_NA | I2C_INT_AL;
237 struct uniphier_fi2c_regs __iomem *regs = dev->regs;
239 debug("%s: addr = %x, len = %d\n", __func__, addr, len);
242 * In case 'len == 0', only the slave address should be sent
243 * for probing, which is covered by the transmit function.
246 return uniphier_fi2c_transmit(dev, addr, len, buf, stop);
248 writel(I2C_DTTX_CMD | I2C_DTTX_RD | addr << 1, ®s->dttx);
250 writel(0, ®s->rbc);
251 writel(irq_flags, ®s->ie);
252 writel(irq_flags, ®s->ic);
254 debug("start condition\n");
255 writel(I2C_CR_MST | I2C_CR_STA | (len == 1 ? I2C_CR_NACK : 0),
259 ret = wait_for_irq(dev, irq_flags, stop);
263 *buf++ = readl(®s->dtrx);
264 debug("received %x\n", *(buf - 1));
267 writel(I2C_CR_MST | I2C_CR_NACK, ®s->cr);
269 writel(irq_flags, ®s->ic);
273 writel(irq_flags, ®s->ic);
276 ret = issue_stop(dev, ret);
281 static int uniphier_fi2c_xfer(struct udevice *bus, struct i2c_msg *msg,
285 struct uniphier_fi2c_dev *dev = dev_get_priv(bus);
288 ret = check_device_busy(dev->regs);
292 for (; nmsgs > 0; nmsgs--, msg++) {
293 /* If next message is read, skip the stop condition */
294 stop = nmsgs > 1 && msg[1].flags & I2C_M_RD ? false : true;
296 if (msg->flags & I2C_M_RD)
297 ret = uniphier_fi2c_receive(dev, msg->addr, msg->len,
300 ret = uniphier_fi2c_transmit(dev, msg->addr, msg->len,
310 static int uniphier_fi2c_set_bus_speed(struct udevice *bus, unsigned int speed)
313 unsigned int clk_count;
314 struct uniphier_fi2c_dev *dev = dev_get_priv(bus);
315 struct uniphier_fi2c_regs __iomem *regs = dev->regs;
317 /* max supported frequency is 400 kHz */
321 ret = check_device_busy(dev->regs);
325 /* make sure the bus is idle when changing the frequency */
326 writel(I2C_BRST_RSCLO, ®s->brst);
328 clk_count = dev->fioclk / speed;
330 writel(clk_count, ®s->cyc);
331 writel(clk_count / 2, ®s->lctl);
332 writel(clk_count / 2, ®s->ssut);
333 writel(clk_count / 16, ®s->dsut);
335 writel(I2C_BRST_FOEN | I2C_BRST_RSCLO, ®s->brst);
338 * Theoretically, each byte can be transferred in
339 * 1000000 * 9 / speed usec.
340 * This time out value is long enough.
342 dev->timeout = 100000000L / speed;
347 static const struct dm_i2c_ops uniphier_fi2c_ops = {
348 .xfer = uniphier_fi2c_xfer,
349 .set_bus_speed = uniphier_fi2c_set_bus_speed,
352 static const struct udevice_id uniphier_fi2c_of_match[] = {
353 { .compatible = "socionext,uniphier-fi2c" },
357 U_BOOT_DRIVER(uniphier_fi2c) = {
358 .name = "uniphier-fi2c",
360 .of_match = uniphier_fi2c_of_match,
361 .probe = uniphier_fi2c_probe,
362 .remove = uniphier_fi2c_remove,
363 .priv_auto_alloc_size = sizeof(struct uniphier_fi2c_dev),
364 .ops = &uniphier_fi2c_ops,