2 * Copyright (C) 2008-2009 Semihalf, Michal Hajduk
3 * Copyright (c) 2012, 2013 The FreeBSD Foundation
4 * Copyright (c) 2015 Ian Lepore <ian@FreeBSD.org>
7 * Portions of this software were developed by Oleksandr Rybalko
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * I2C driver for Freescale i.MX hardware.
35 * Note that the hardware is capable of running as both a master and a slave.
36 * This driver currently implements only master-mode operations.
38 * This driver supports multi-master i2c buses, by detecting bus arbitration
39 * loss and returning IIC_EBUSBSY status. Notably, it does not do any kind of
40 * retries if some other master jumps onto the bus and interrupts one of our
41 * transfer cycles resulting in arbitration loss in mid-transfer. The caller
42 * must handle retries in a way that makes sense for the slave being addressed.
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
49 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/limits.h>
54 #include <sys/module.h>
55 #include <sys/resource.h>
56 #include <sys/sysctl.h>
58 #include <machine/bus.h>
59 #include <machine/resource.h>
62 #include <arm/freescale/imx/imx_ccmvar.h>
64 #include <dev/iicbus/iiconf.h>
65 #include <dev/iicbus/iicbus.h>
66 #include <dev/iicbus/iic_recover_bus.h>
67 #include "iicbus_if.h"
69 #include <dev/ofw/openfirm.h>
70 #include <dev/ofw/ofw_bus.h>
71 #include <dev/ofw/ofw_bus_subr.h>
73 #include <dev/fdt/fdt_pinctrl.h>
74 #include <dev/gpio/gpiobusvar.h>
76 #define I2C_ADDR_REG 0x00 /* I2C slave address register */
77 #define I2C_FDR_REG 0x04 /* I2C frequency divider register */
78 #define I2C_CONTROL_REG 0x08 /* I2C control register */
79 #define I2C_STATUS_REG 0x0C /* I2C status register */
80 #define I2C_DATA_REG 0x10 /* I2C data register */
81 #define I2C_DFSRR_REG 0x14 /* I2C Digital Filter Sampling rate */
83 #define I2CCR_MEN (1 << 7) /* Module enable */
84 #define I2CCR_MSTA (1 << 5) /* Master/slave mode */
85 #define I2CCR_MTX (1 << 4) /* Transmit/receive mode */
86 #define I2CCR_TXAK (1 << 3) /* Transfer acknowledge */
87 #define I2CCR_RSTA (1 << 2) /* Repeated START */
89 #define I2CSR_MCF (1 << 7) /* Data transfer */
90 #define I2CSR_MASS (1 << 6) /* Addressed as a slave */
91 #define I2CSR_MBB (1 << 5) /* Bus busy */
92 #define I2CSR_MAL (1 << 4) /* Arbitration lost */
93 #define I2CSR_SRW (1 << 2) /* Slave read/write */
94 #define I2CSR_MIF (1 << 1) /* Module interrupt */
95 #define I2CSR_RXAK (1 << 0) /* Received acknowledge */
97 #define I2C_BAUD_RATE_FAST 0x31
98 #define I2C_BAUD_RATE_DEF 0x3F
99 #define I2C_DFSSR_DIV 0x10
102 * A table of available divisors and the associated coded values to put in the
103 * FDR register to achieve that divisor.. There is no algorithmic relationship I
104 * can see between divisors and the codes that go into the register. The table
105 * begins and ends with entries that handle insane configuration values.
111 static struct clkdiv clkdiv_table[] = {
112 { 0, 0x20 }, { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 },
113 { 28, 0x23 }, { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 },
114 { 40, 0x26 }, { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 },
115 { 52, 0x05 }, { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2a },
116 { 72, 0x2b }, { 80, 0x2c }, { 88, 0x09 }, { 96, 0x2d },
117 { 104, 0x0a }, { 112, 0x2e }, { 128, 0x2f }, { 144, 0x0c },
118 { 160, 0x30 }, { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0f },
119 { 256, 0x33 }, { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 },
120 { 448, 0x36 }, { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 },
121 { 640, 0x38 }, { 768, 0x39 }, { 896, 0x3a }, { 960, 0x17 },
122 { 1024, 0x3b }, { 1152, 0x18 }, { 1280, 0x3c }, { 1536, 0x3d },
123 { 1792, 0x3e }, { 1920, 0x1b }, { 2048, 0x3f }, { 2304, 0x1c },
124 { 2560, 0x1d }, { 3072, 0x1e }, { 3840, 0x1f }, {UINT_MAX, 0x1f}
127 static struct ofw_compat_data compat_data[] = {
128 {"fsl,imx6q-i2c", 1},
136 struct resource *res;
138 sbintime_t byte_time_sbt;
140 gpio_pin_t rb_sclpin;
141 gpio_pin_t rb_sdapin;
146 #define DEVICE_DEBUGF(sc, lvl, fmt, args...) \
147 if ((lvl) <= (sc)->debug) \
148 device_printf((sc)->dev, fmt, ##args)
150 #define DEBUGF(sc, lvl, fmt, args...) \
151 if ((lvl) <= (sc)->debug) \
154 static phandle_t i2c_get_node(device_t, device_t);
155 static int i2c_probe(device_t);
156 static int i2c_attach(device_t);
157 static int i2c_detach(device_t);
159 static int i2c_repeated_start(device_t, u_char, int);
160 static int i2c_start(device_t, u_char, int);
161 static int i2c_stop(device_t);
162 static int i2c_reset(device_t, u_char, u_char, u_char *);
163 static int i2c_read(device_t, char *, int, int *, int, int);
164 static int i2c_write(device_t, const char *, int, int *, int);
166 static device_method_t i2c_methods[] = {
167 DEVMETHOD(device_probe, i2c_probe),
168 DEVMETHOD(device_attach, i2c_attach),
169 DEVMETHOD(device_detach, i2c_detach),
172 DEVMETHOD(ofw_bus_get_node, i2c_get_node),
174 DEVMETHOD(iicbus_callback, iicbus_null_callback),
175 DEVMETHOD(iicbus_repeated_start, i2c_repeated_start),
176 DEVMETHOD(iicbus_start, i2c_start),
177 DEVMETHOD(iicbus_stop, i2c_stop),
178 DEVMETHOD(iicbus_reset, i2c_reset),
179 DEVMETHOD(iicbus_read, i2c_read),
180 DEVMETHOD(iicbus_write, i2c_write),
181 DEVMETHOD(iicbus_transfer, iicbus_transfer_gen),
186 static driver_t i2c_driver = {
189 sizeof(struct i2c_softc),
191 static devclass_t i2c_devclass;
193 DRIVER_MODULE(imx_i2c, simplebus, i2c_driver, i2c_devclass, 0, 0);
194 DRIVER_MODULE(ofw_iicbus, imx_i2c, ofw_iicbus_driver, ofw_iicbus_devclass, 0, 0);
195 MODULE_DEPEND(imx_i2c, iicbus, 1, 1, 1);
196 MODULE_DEPEND(imx_i2c, ofw_iicbus, 1, 1, 1);
197 SIMPLEBUS_PNP_INFO(compat_data);
200 i2c_get_node(device_t bus, device_t dev)
203 * Share controller node with iicbus device
205 return ofw_bus_get_node(bus);
209 i2c_write_reg(struct i2c_softc *sc, bus_size_t off, uint8_t val)
212 bus_write_1(sc->res, off, val);
215 static __inline uint8_t
216 i2c_read_reg(struct i2c_softc *sc, bus_size_t off)
219 return (bus_read_1(sc->res, off));
223 i2c_flag_set(struct i2c_softc *sc, bus_size_t off, uint8_t mask)
227 status = i2c_read_reg(sc, off);
229 i2c_write_reg(sc, off, status);
232 /* Wait for bus to become busy or not-busy. */
234 wait_for_busbusy(struct i2c_softc *sc, int wantbusy)
240 srb = i2c_read_reg(sc, I2C_STATUS_REG) & I2CSR_MBB;
241 if ((srb && wantbusy) || (!srb && !wantbusy))
245 return (IIC_ETIMEOUT);
248 /* Wait for transfer to complete, optionally check RXAK. */
250 wait_for_xfer(struct i2c_softc *sc, int checkack)
255 * Sleep for about the time it takes to transfer a byte (with precision
256 * set to tolerate 5% oversleep). We calculate the approximate byte
257 * transfer time when we set the bus speed divisor. Slaves are allowed
258 * to do clock-stretching so the actual transfer time can be larger, but
259 * this gets the bulk of the waiting out of the way without tying up the
260 * processor the whole time.
262 pause_sbt("imxi2c", sc->byte_time_sbt, sc->byte_time_sbt / 20, 0);
266 sr = i2c_read_reg(sc, I2C_STATUS_REG);
267 if (sr & I2CSR_MIF) {
269 return (IIC_EBUSERR);
270 else if (checkack && (sr & I2CSR_RXAK))
277 return (IIC_ETIMEOUT);
281 * Implement the error handling shown in the state diagram of the imx6 reference
282 * manual. If there was an error, then:
283 * - Clear master mode (MSTA and MTX).
284 * - Wait for the bus to become free or for a timeout to happen.
285 * - Disable the controller.
288 i2c_error_handler(struct i2c_softc *sc, int error)
292 i2c_write_reg(sc, I2C_STATUS_REG, 0);
293 i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN);
294 wait_for_busbusy(sc, false);
295 i2c_write_reg(sc, I2C_CONTROL_REG, 0);
301 i2c_recover_getsda(void *ctx)
305 gpio_pin_is_active(((struct i2c_softc *)ctx)->rb_sdapin, &active);
310 i2c_recover_setsda(void *ctx, int value)
313 gpio_pin_set_active(((struct i2c_softc *)ctx)->rb_sdapin, value);
317 i2c_recover_getscl(void *ctx)
321 gpio_pin_is_active(((struct i2c_softc *)ctx)->rb_sclpin, &active);
327 i2c_recover_setscl(void *ctx, int value)
330 gpio_pin_set_active(((struct i2c_softc *)ctx)->rb_sclpin, value);
334 i2c_recover_bus(struct i2c_softc *sc)
336 struct iicrb_pin_access pins;
340 * If we have gpio pinmux config, reconfigure the pins to gpio mode,
341 * invoke iic_recover_bus which checks for a hung bus and bitbangs a
342 * recovery sequence if necessary, then configure the pins back to i2c
345 if (sc->rb_pinctl_idx == 0)
348 fdt_pinctrl_configure(sc->dev, sc->rb_pinctl_idx);
351 pins.getsda = i2c_recover_getsda;
352 pins.setsda = i2c_recover_setsda;
353 pins.getscl = i2c_recover_getscl;
354 pins.setscl = i2c_recover_setscl;
355 err = iic_recover_bus(&pins);
357 fdt_pinctrl_configure(sc->dev, 0);
363 i2c_probe(device_t dev)
366 if (!ofw_bus_status_okay(dev))
369 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
372 device_set_desc(dev, "Freescale i.MX I2C");
374 return (BUS_PROBE_DEFAULT);
378 i2c_attach(device_t dev)
381 struct i2c_softc *sc;
385 sc = device_get_softc(dev);
389 sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
391 if (sc->res == NULL) {
392 device_printf(dev, "could not allocate resources");
396 sc->iicbus = device_add_child(dev, "iicbus", -1);
397 if (sc->iicbus == NULL) {
398 device_printf(dev, "could not add iicbus child");
402 /* Set up debug-enable sysctl. */
403 SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->dev),
404 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
405 OID_AUTO, "debug", CTLFLAG_RWTUN, &sc->debug, 0,
406 "Enable debug; 1=reads/writes, 2=add starts/stops");
409 * Set up for bus recovery using gpio pins, if the pinctrl and gpio
410 * properties are present. This is optional. If all the config data is
411 * not in place, we just don't do gpio bitbang bus recovery.
413 node = ofw_bus_get_node(sc->dev);
415 err = gpio_pin_get_by_ofw_property(dev, node, "scl-gpios",
419 err = gpio_pin_get_by_ofw_property(dev, node, "sda-gpios",
425 * Preset the gpio pins to output high (idle bus state). The signal
426 * won't actually appear on the pins until the bus recovery code changes
427 * the pinmux config from i2c to gpio.
429 gpio_pin_setflags(sc->rb_sclpin, GPIO_PIN_OUTPUT);
430 gpio_pin_setflags(sc->rb_sdapin, GPIO_PIN_OUTPUT);
431 gpio_pin_set_active(sc->rb_sclpin, true);
432 gpio_pin_set_active(sc->rb_sdapin, true);
435 * Obtain the index of pinctrl node for bus recovery using gpio pins,
436 * then confirm that pinctrl properties exist for that index and for the
437 * default pinctrl-0. If sc->rb_pinctl_idx is non-zero, the reset code
438 * will also do a bus recovery, so setting this value must be last.
440 err = ofw_bus_find_string_index(node, "pinctrl-names", "gpio", &cfgidx);
442 snprintf(wrkstr, sizeof(wrkstr), "pinctrl-%d", cfgidx);
443 if (OF_hasprop(node, "pinctrl-0") && OF_hasprop(node, wrkstr))
444 sc->rb_pinctl_idx = cfgidx;
449 /* We don't do a hardware reset here because iicbus_attach() does it. */
451 /* Probe and attach the iicbus when interrupts are available. */
452 config_intrhook_oneshot((ich_func_t)bus_generic_attach, dev);
457 i2c_detach(device_t dev)
459 struct i2c_softc *sc;
462 sc = device_get_softc(dev);
464 if ((error = bus_generic_detach(sc->dev)) != 0) {
465 device_printf(sc->dev, "cannot detach child devices\n");
469 if (sc->iicbus != NULL)
470 device_delete_child(dev, sc->iicbus);
472 /* Release bus-recover pins; gpio_pin_release() handles NULL args. */
473 gpio_pin_release(sc->rb_sclpin);
474 gpio_pin_release(sc->rb_sdapin);
477 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->res);
483 i2c_repeated_start(device_t dev, u_char slave, int timeout)
485 struct i2c_softc *sc;
488 sc = device_get_softc(dev);
490 if ((i2c_read_reg(sc, I2C_STATUS_REG) & I2CSR_MBB) == 0) {
491 return (IIC_EBUSERR);
495 * Set repeated start condition, delay (per reference manual, min 156nS)
496 * before writing slave address, wait for ack after write.
498 i2c_flag_set(sc, I2C_CONTROL_REG, I2CCR_RSTA);
500 i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
501 i2c_write_reg(sc, I2C_DATA_REG, slave);
503 DEVICE_DEBUGF(sc, 2, "rstart 0x%02x\n", sc->slave);
504 error = wait_for_xfer(sc, true);
505 return (i2c_error_handler(sc, error));
509 i2c_start_ll(device_t dev, u_char slave, int timeout)
511 struct i2c_softc *sc;
514 sc = device_get_softc(dev);
516 i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN);
517 DELAY(10); /* Delay for controller to sample bus state. */
518 if (i2c_read_reg(sc, I2C_STATUS_REG) & I2CSR_MBB) {
519 return (i2c_error_handler(sc, IIC_EBUSERR));
521 i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN | I2CCR_MSTA | I2CCR_MTX);
522 if ((error = wait_for_busbusy(sc, true)) != IIC_NOERR)
523 return (i2c_error_handler(sc, error));
524 i2c_write_reg(sc, I2C_STATUS_REG, 0);
525 i2c_write_reg(sc, I2C_DATA_REG, slave);
527 DEVICE_DEBUGF(sc, 2, "start 0x%02x\n", sc->slave);
528 error = wait_for_xfer(sc, true);
529 return (i2c_error_handler(sc, error));
533 i2c_start(device_t dev, u_char slave, int timeout)
535 struct i2c_softc *sc;
538 sc = device_get_softc(dev);
541 * Invoke the low-level code to put the bus into master mode and address
542 * the given slave. If that fails, idle the controller and attempt a
543 * bus recovery, and then try again one time. Signaling a start and
544 * addressing the slave is the only operation that a low-level driver
545 * can safely retry without any help from the upper layers that know
546 * more about the slave device.
548 if ((error = i2c_start_ll(dev, slave, timeout)) != 0) {
549 i2c_write_reg(sc, I2C_CONTROL_REG, 0x0);
550 if ((error = i2c_recover_bus(sc)) != 0)
552 error = i2c_start_ll(dev, slave, timeout);
558 i2c_stop(device_t dev)
560 struct i2c_softc *sc;
562 sc = device_get_softc(dev);
564 i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN);
565 wait_for_busbusy(sc, false);
566 i2c_write_reg(sc, I2C_CONTROL_REG, 0);
567 DEVICE_DEBUGF(sc, 2, "stop 0x%02x\n", sc->slave);
572 i2c_reset(device_t dev, u_char speed, u_char addr, u_char *oldadr)
574 struct i2c_softc *sc;
575 u_int busfreq, div, i, ipgfreq;
577 sc = device_get_softc(dev);
579 DEVICE_DEBUGF(sc, 1, "reset\n");
582 * Look up the divisor that gives the nearest speed that doesn't exceed
583 * the configured value for the bus.
585 ipgfreq = imx_ccm_ipg_hz();
586 busfreq = IICBUS_GET_FREQUENCY(sc->iicbus, speed);
587 div = howmany(ipgfreq, busfreq);
588 for (i = 0; i < nitems(clkdiv_table); i++) {
589 if (clkdiv_table[i].divisor >= div)
594 * Calculate roughly how long it will take to transfer a byte (which
595 * requires 9 clock cycles) at the new bus speed. This value is used to
596 * pause() while waiting for transfer-complete. With a 66MHz IPG clock
597 * and the actual i2c bus speeds that leads to, for nominal 100KHz and
598 * 400KHz bus speeds the transfer times are roughly 104uS and 22uS.
600 busfreq = ipgfreq / clkdiv_table[i].divisor;
601 sc->byte_time_sbt = SBT_1US * (9000000 / busfreq);
604 * Disable the controller (do the reset), and set the new clock divisor.
606 i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
607 i2c_write_reg(sc, I2C_CONTROL_REG, 0x0);
608 i2c_write_reg(sc, I2C_FDR_REG, (uint8_t)clkdiv_table[i].regcode);
611 * Now that the controller is idle, perform bus recovery. If the bus
612 * isn't hung, this a fairly fast no-op.
614 return (i2c_recover_bus(sc));
618 i2c_read(device_t dev, char *buf, int len, int *read, int last, int delay)
620 struct i2c_softc *sc;
623 sc = device_get_softc(dev);
626 DEVICE_DEBUGF(sc, 1, "read 0x%02x len %d: ", sc->slave, len);
629 i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
630 I2CCR_MSTA | I2CCR_TXAK);
632 i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
634 /* Dummy read to prime the receiver. */
635 i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
636 i2c_read_reg(sc, I2C_DATA_REG);
641 while (*read < len) {
642 if ((error = wait_for_xfer(sc, false)) != IIC_NOERR)
644 i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
646 if (*read == len - 2) {
647 /* NO ACK on last byte */
648 i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
649 I2CCR_MSTA | I2CCR_TXAK);
650 } else if (*read == len - 1) {
651 /* Transfer done, signal stop. */
652 i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
654 wait_for_busbusy(sc, false);
657 reg = i2c_read_reg(sc, I2C_DATA_REG);
658 DEBUGF(sc, 1, "0x%02x ", reg);
664 return (i2c_error_handler(sc, error));
668 i2c_write(device_t dev, const char *buf, int len, int *sent, int timeout)
670 struct i2c_softc *sc;
673 sc = device_get_softc(dev);
677 DEVICE_DEBUGF(sc, 1, "write 0x%02x len %d: ", sc->slave, len);
678 while (*sent < len) {
679 DEBUGF(sc, 1, "0x%02x ", *buf);
680 i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
681 i2c_write_reg(sc, I2C_DATA_REG, *buf++);
682 if ((error = wait_for_xfer(sc, true)) != IIC_NOERR)
687 return (i2c_error_handler(sc, error));