1 // SPDX-License-Identifier: GPL-2.0+
3 * ocores-i2c.c: I2C bus driver for OpenCores I2C controller
4 * (https://opencores.org/projects/i2c)
8 * Copyright (C) 2020 SiFive, Inc.
11 * Support for the GRLIB port of the controller by
19 #include <dm/device_compat.h>
22 #include <linux/compat.h>
23 #include <linux/log2.h>
24 #include <linux/delay.h>
27 #define OCI2C_PRELOW 0
28 #define OCI2C_PREHIGH 1
29 #define OCI2C_CONTROL 2
31 #define OCI2C_CMD 4 /* write only */
32 #define OCI2C_STATUS 4 /* read only, same address as OCI2C_CMD */
34 #define OCI2C_CTRL_IEN 0x40
35 #define OCI2C_CTRL_EN 0x80
37 #define OCI2C_CMD_START 0x91
38 #define OCI2C_CMD_STOP 0x41
39 #define OCI2C_CMD_READ 0x21
40 #define OCI2C_CMD_WRITE 0x11
41 #define OCI2C_CMD_READ_ACK 0x21
42 #define OCI2C_CMD_READ_NACK 0x29
43 #define OCI2C_CMD_IACK 0x01
45 #define OCI2C_STAT_IF 0x01
46 #define OCI2C_STAT_TIP 0x02
47 #define OCI2C_STAT_ARBLOST 0x20
48 #define OCI2C_STAT_BUSY 0x40
49 #define OCI2C_STAT_NACK 0x80
60 #define OCORES_FLAG_BROKEN_IRQ BIT(1) /* Broken IRQ for FU540-C000 SoC */
62 struct ocores_i2c_bus {
70 int state; /* see STATE_ */
74 void (*setreg)(struct ocores_i2c_bus *i2c, int reg, u8 value);
75 u8 (*getreg)(struct ocores_i2c_bus *i2c, int reg);
78 DECLARE_GLOBAL_DATA_PTR;
80 /* Boolean attribute values */
86 static void oc_setreg_8(struct ocores_i2c_bus *i2c, int reg, u8 value)
88 writeb(value, i2c->base + (reg << i2c->reg_shift));
91 static void oc_setreg_16(struct ocores_i2c_bus *i2c, int reg, u8 value)
93 writew(value, i2c->base + (reg << i2c->reg_shift));
96 static void oc_setreg_32(struct ocores_i2c_bus *i2c, int reg, u8 value)
98 writel(value, i2c->base + (reg << i2c->reg_shift));
101 static void oc_setreg_16be(struct ocores_i2c_bus *i2c, int reg, u8 value)
103 out_be16(i2c->base + (reg << i2c->reg_shift), value);
106 static void oc_setreg_32be(struct ocores_i2c_bus *i2c, int reg, u8 value)
108 out_be32(i2c->base + (reg << i2c->reg_shift), value);
111 static inline u8 oc_getreg_8(struct ocores_i2c_bus *i2c, int reg)
113 return readb(i2c->base + (reg << i2c->reg_shift));
116 static inline u8 oc_getreg_16(struct ocores_i2c_bus *i2c, int reg)
118 return readw(i2c->base + (reg << i2c->reg_shift));
121 static inline u8 oc_getreg_32(struct ocores_i2c_bus *i2c, int reg)
123 return readl(i2c->base + (reg << i2c->reg_shift));
126 static inline u8 oc_getreg_16be(struct ocores_i2c_bus *i2c, int reg)
128 return in_be16(i2c->base + (reg << i2c->reg_shift));
131 static inline u8 oc_getreg_32be(struct ocores_i2c_bus *i2c, int reg)
133 return in_be32(i2c->base + (reg << i2c->reg_shift));
136 static inline void oc_setreg(struct ocores_i2c_bus *i2c, int reg, u8 value)
138 i2c->setreg(i2c, reg, value);
141 static inline u8 oc_getreg(struct ocores_i2c_bus *i2c, int reg)
143 return i2c->getreg(i2c, reg);
146 static inline u8 i2c_8bit_addr_from_msg(const struct i2c_msg *msg)
148 return (msg->addr << 1) | (msg->flags & I2C_M_RD ? 1 : 0);
151 static void ocores_process(struct ocores_i2c_bus *i2c, u8 stat)
153 struct i2c_msg *msg = i2c->msg;
155 if (i2c->state == STATE_DONE || i2c->state == STATE_ERROR) {
156 /* stop has been sent */
157 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK);
162 if (stat & OCI2C_STAT_ARBLOST) {
163 i2c->state = STATE_ERROR;
164 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
168 if (i2c->state == STATE_START || i2c->state == STATE_WRITE) {
170 (msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE;
172 if (stat & OCI2C_STAT_NACK) {
173 i2c->state = STATE_ERROR;
174 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
178 msg->buf[i2c->pos++] = oc_getreg(i2c, OCI2C_DATA);
182 if (i2c->pos == msg->len) {
188 if (i2c->nmsgs) { /* end? */
190 if (!(msg->flags & I2C_M_NOSTART)) {
191 u8 addr = i2c_8bit_addr_from_msg(msg);
193 i2c->state = STATE_START;
195 oc_setreg(i2c, OCI2C_DATA, addr);
196 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
199 i2c->state = (msg->flags & I2C_M_RD)
200 ? STATE_READ : STATE_WRITE;
202 i2c->state = STATE_DONE;
203 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
208 if (i2c->state == STATE_READ) {
209 oc_setreg(i2c, OCI2C_CMD, i2c->pos == (msg->len - 1) ?
210 OCI2C_CMD_READ_NACK : OCI2C_CMD_READ_ACK);
212 oc_setreg(i2c, OCI2C_DATA, msg->buf[i2c->pos++]);
213 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_WRITE);
217 static irqreturn_t ocores_isr(int irq, void *dev_id)
219 struct ocores_i2c_bus *i2c = dev_id;
220 u8 stat = oc_getreg(i2c, OCI2C_STATUS);
222 if (i2c->flags & OCORES_FLAG_BROKEN_IRQ) {
223 if ((stat & OCI2C_STAT_IF) && !(stat & OCI2C_STAT_BUSY))
225 } else if (!(stat & OCI2C_STAT_IF)) {
228 ocores_process(i2c, stat);
234 * Wait until something change in a given register
235 * @i2c: ocores I2C device instance
236 * @reg: register to query
237 * @mask: bitmask to apply on register value
238 * @val: expected result
239 * @msec: timeout in msec
241 * Timeout is necessary to avoid to stay here forever when the chip
242 * does not answer correctly.
244 * Return: 0 on success, -ETIMEDOUT on timeout
246 static int ocores_wait(struct ocores_i2c_bus *i2c,
247 int reg, u8 mask, u8 val,
248 const unsigned long msec)
253 u8 status = oc_getreg(i2c, reg);
255 if ((status & mask) == val)
261 if (count == (1000 * msec))
268 * Wait until is possible to process some data
269 * @i2c: ocores I2C device instance
271 * Used when the device is in polling mode (interrupts disabled).
273 * Return: 0 on success, -ETIMEDOUT on timeout
275 static int ocores_poll_wait(struct ocores_i2c_bus *i2c)
280 if (i2c->state == STATE_DONE || i2c->state == STATE_ERROR) {
281 /* transfer is over */
282 mask = OCI2C_STAT_BUSY;
284 /* on going transfer */
285 mask = OCI2C_STAT_TIP;
287 * We wait for the data to be transferred (8bit),
288 * then we start polling on the ACK/NACK bit
290 udelay((8 * 1000) / i2c->bus_clk_khz);
294 * once we are here we expect to get the expected result immediately
295 * so if after 1ms we timeout then something is broken.
297 err = ocores_wait(i2c, OCI2C_STATUS, mask, 0, 1);
299 debug("%s: STATUS timeout, bit 0x%x did not clear in 1ms\n",
305 * It handles an IRQ-less transfer
306 * @i2c: ocores I2C device instance
308 * Even if IRQ are disabled, the I2C OpenCore IP behavior is exactly the same
309 * (only that IRQ are not produced). This means that we can re-use entirely
310 * ocores_isr(), we just add our polling code around it.
312 * It can run in atomic context
314 static void ocores_process_polling(struct ocores_i2c_bus *i2c)
320 err = ocores_poll_wait(i2c);
322 i2c->state = STATE_ERROR;
326 ret = ocores_isr(-1, i2c);
327 if (ret == IRQ_NONE) {
328 break; /* all messages have been transferred */
330 if (i2c->flags & OCORES_FLAG_BROKEN_IRQ)
331 if (i2c->state == STATE_DONE)
337 static int ocores_xfer_core(struct ocores_i2c_bus *i2c,
338 struct i2c_msg *msgs, int num, bool polling)
342 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
345 oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~OCI2C_CTRL_IEN);
350 i2c->state = STATE_START;
352 oc_setreg(i2c, OCI2C_DATA, i2c_8bit_addr_from_msg(i2c->msg));
353 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
356 ocores_process_polling(i2c);
358 return (i2c->state == STATE_DONE) ? num : -EIO;
361 static int ocores_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
363 struct ocores_i2c_bus *bus = dev_get_priv(dev);
366 debug("i2c_xfer: %d messages\n", nmsgs);
368 ret = ocores_xfer_core(bus, msg, nmsgs, 1);
371 debug("i2c_write: error sending\n");
378 static int ocores_i2c_enable_clk(struct udevice *dev)
380 struct ocores_i2c_bus *bus = dev_get_priv(dev);
384 ret = clk_get_by_index(dev, 0, &bus->clk);
388 ret = clk_enable(&bus->clk);
392 clk_rate = clk_get_rate(&bus->clk);
396 bus->ip_clk_khz = clk_rate / 1000;
403 static int ocores_init(struct udevice *dev, struct ocores_i2c_bus *bus)
407 u8 ctrl = oc_getreg(bus, OCI2C_CONTROL);
409 /* make sure the device is disabled */
410 ctrl &= ~(OCI2C_CTRL_EN | OCI2C_CTRL_IEN);
411 oc_setreg(bus, OCI2C_CONTROL, ctrl);
413 prescale = (bus->ip_clk_khz / (5 * bus->bus_clk_khz)) - 1;
414 prescale = clamp(prescale, 0, 0xffff);
416 diff = bus->ip_clk_khz / (5 * (prescale + 1)) - bus->bus_clk_khz;
417 if (abs(diff) > bus->bus_clk_khz / 10) {
418 debug("Unsupported clock settings: core: %d KHz, bus: %d KHz\n",
419 bus->ip_clk_khz, bus->bus_clk_khz);
423 oc_setreg(bus, OCI2C_PRELOW, prescale & 0xff);
424 oc_setreg(bus, OCI2C_PREHIGH, prescale >> 8);
426 /* Init the device */
427 oc_setreg(bus, OCI2C_CMD, OCI2C_CMD_IACK);
428 oc_setreg(bus, OCI2C_CONTROL, ctrl | OCI2C_CTRL_EN);
434 * Read and write functions for the GRLIB port of the controller. Registers are
435 * 32-bit big endian and the PRELOW and PREHIGH registers are merged into one
436 * register. The subsequent registers have their offsets decreased accordingly.
438 static u8 oc_getreg_grlib(struct ocores_i2c_bus *i2c, int reg)
443 if (reg != OCI2C_PRELOW)
445 rd = in_be32(i2c->base + (rreg << i2c->reg_shift));
446 if (reg == OCI2C_PREHIGH)
447 return (u8)(rd >> 8);
452 static void oc_setreg_grlib(struct ocores_i2c_bus *i2c, int reg, u8 value)
457 if (reg != OCI2C_PRELOW)
459 if (reg == OCI2C_PRELOW || reg == OCI2C_PREHIGH) {
460 curr = in_be32(i2c->base + (rreg << i2c->reg_shift));
461 if (reg == OCI2C_PRELOW)
462 wr = (curr & 0xff00) | value;
464 wr = (((u32)value) << 8) | (curr & 0xff);
468 out_be32(i2c->base + (rreg << i2c->reg_shift), wr);
471 static int ocores_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
475 struct ocores_i2c_bus *bus = dev_get_priv(dev);
478 speed = speed / 1000;
480 prescale = (bus->ip_clk_khz / (5 * speed)) - 1;
481 prescale = clamp(prescale, 0, 0xffff);
483 diff = bus->ip_clk_khz / (5 * (prescale + 1)) - speed;
484 if (abs(diff) > speed / 10) {
485 debug("Unsupported clock settings: core: %d KHz, bus: %d KHz\n",
486 bus->ip_clk_khz, speed);
490 oc_setreg(bus, OCI2C_PRELOW, prescale & 0xff);
491 oc_setreg(bus, OCI2C_PREHIGH, prescale >> 8);
493 bus->bus_clk_khz = speed;
497 int ocores_i2c_get_bus_speed(struct udevice *dev)
499 struct ocores_i2c_bus *bus = dev_get_priv(dev);
501 return (bus->bus_clk_khz * 1000);
504 static const struct dm_i2c_ops ocores_i2c_ops = {
505 .xfer = ocores_i2c_xfer,
506 .set_bus_speed = ocores_i2c_set_bus_speed,
507 .get_bus_speed = ocores_i2c_get_bus_speed,
510 static int ocores_i2c_probe(struct udevice *dev)
512 struct ocores_i2c_bus *bus = dev_get_priv(dev);
513 bool clock_frequency_present;
515 u32 clock_frequency_khz;
518 bus->base = (void __iomem *)devfdt_get_addr(dev);
520 if (dev_read_u32(dev, "reg-shift", &bus->reg_shift)) {
521 /* no 'reg-shift', check for deprecated 'regstep' */
522 ret = dev_read_u32(dev, "regstep", &val);
525 "missing both reg-shift and regstep property: %d\n", ret);
528 bus->reg_shift = ilog2(val);
530 "regstep property deprecated, use reg-shift\n");
534 if (dev_read_u32(dev, "clock-frequency", &val)) {
535 bus->bus_clk_khz = 100;
536 clock_frequency_present = FALSE;
538 bus->bus_clk_khz = val / 1000;
539 clock_frequency_khz = val / 1000;
540 clock_frequency_present = TRUE;
543 ret = ocores_i2c_enable_clk(dev);
547 if (bus->ip_clk_khz == 0) {
548 if (dev_read_u32(dev, "opencores,ip-clock-frequency", &val)) {
549 if (!clock_frequency_present) {
551 "Missing required parameter 'opencores,ip-clock-frequency'\n");
552 clk_disable(&bus->clk);
556 bus->ip_clk_khz = clock_frequency_khz;
558 "Deprecated usage of the 'clock-frequency' property, please update to 'opencores,ip-clock-frequency'\n");
560 bus->ip_clk_khz = val / 1000;
561 if (clock_frequency_present)
562 bus->bus_clk_khz = clock_frequency_khz;
566 bus->reg_io_width = dev_read_u32_default(dev, "reg-io-width", 1);
568 if (dev_get_driver_data(dev) == TYPE_GRLIB) {
569 debug("GRLIB variant of i2c-ocores\n");
570 bus->setreg = oc_setreg_grlib;
571 bus->getreg = oc_getreg_grlib;
574 if (!bus->setreg || !bus->getreg) {
575 bool be = (cpu_to_be32(0x12345678) == 0x12345678);
577 switch (bus->reg_io_width) {
579 bus->setreg = oc_setreg_8;
580 bus->getreg = oc_getreg_8;
584 bus->setreg = be ? oc_setreg_16be : oc_setreg_16;
585 bus->getreg = be ? oc_getreg_16be : oc_getreg_16;
589 bus->setreg = be ? oc_setreg_32be : oc_setreg_32;
590 bus->getreg = be ? oc_getreg_32be : oc_getreg_32;
594 debug("Unsupported I/O width (%d)\n",
602 * Set OCORES_FLAG_BROKEN_IRQ to enable workaround for
603 * FU540-C000 SoC in polling mode.
604 * Since the SoC does have an interrupt, its DT has an interrupt
605 * property - But this should be bypassed as the IRQ logic in this
609 if (device_is_compatible(dev, "sifive,fu540-c000-i2c"))
610 bus->flags |= OCORES_FLAG_BROKEN_IRQ;
612 ret = ocores_init(dev, bus);
619 clk_disable(&bus->clk);
623 static const struct udevice_id ocores_i2c_ids[] = {
624 { .compatible = "opencores,i2c-ocores", .data = TYPE_OCORES },
625 { .compatible = "aeroflexgaisler,i2cmst", .data = TYPE_GRLIB },
626 { .compatible = "sifive,fu540-c000-i2c" },
627 { .compatible = "sifive,i2c0" },
630 U_BOOT_DRIVER(i2c_ocores) = {
631 .name = "i2c_ocores",
633 .of_match = ocores_i2c_ids,
634 .probe = ocores_i2c_probe,
635 .priv_auto = sizeof(struct ocores_i2c_bus),
636 .ops = &ocores_i2c_ops,