2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1998, 2001 Nicolas Souchu
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 * Generic I2C bit-banging code
43 * From Linux I2C generic interface
44 * (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>
48 #include "opt_platform.h"
50 #include <sys/param.h>
51 #include <sys/kernel.h>
52 #include <sys/systm.h>
53 #include <sys/module.h>
55 #include <sys/sysctl.h>
59 #include <dev/ofw/ofw_bus.h>
60 #include <dev/ofw/ofw_bus_subr.h>
61 #include <dev/fdt/fdt_common.h>
64 #include <dev/iicbus/iiconf.h>
65 #include <dev/iicbus/iicbus.h>
67 #include <dev/smbus/smbconf.h>
69 #include "iicbus_if.h"
72 /* Based on the SMBus specification. */
73 #define DEFAULT_SCL_LOW_TIMEOUT (25 * 1000)
77 u_int udelay; /* signal toggle delay in usec */
78 u_int io_latency; /* approximate pin toggling latency */
79 u_int scl_low_timeout;
82 static int iicbb_attach(device_t);
83 static void iicbb_child_detached(device_t, device_t);
84 static int iicbb_detach(device_t);
85 static int iicbb_print_child(device_t, device_t);
86 static int iicbb_probe(device_t);
88 static int iicbb_callback(device_t, int, caddr_t);
89 static int iicbb_start(device_t, u_char, int);
90 static int iicbb_repstart(device_t, u_char, int);
91 static int iicbb_stop(device_t);
92 static int iicbb_write(device_t, const char *, int, int *, int);
93 static int iicbb_read(device_t, char *, int, int *, int, int);
94 static int iicbb_reset(device_t, u_char, u_char, u_char *);
95 static int iicbb_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs);
96 static void iicbb_set_speed(struct iicbb_softc *sc, u_char);
98 static phandle_t iicbb_get_node(device_t, device_t);
101 static device_method_t iicbb_methods[] = {
102 /* device interface */
103 DEVMETHOD(device_probe, iicbb_probe),
104 DEVMETHOD(device_attach, iicbb_attach),
105 DEVMETHOD(device_detach, iicbb_detach),
108 DEVMETHOD(bus_child_detached, iicbb_child_detached),
109 DEVMETHOD(bus_print_child, iicbb_print_child),
111 /* iicbus interface */
112 DEVMETHOD(iicbus_callback, iicbb_callback),
113 DEVMETHOD(iicbus_start, iicbb_start),
114 DEVMETHOD(iicbus_repeated_start, iicbb_repstart),
115 DEVMETHOD(iicbus_stop, iicbb_stop),
116 DEVMETHOD(iicbus_write, iicbb_write),
117 DEVMETHOD(iicbus_read, iicbb_read),
118 DEVMETHOD(iicbus_reset, iicbb_reset),
119 DEVMETHOD(iicbus_transfer, iicbb_transfer),
122 /* ofw_bus interface */
123 DEVMETHOD(ofw_bus_get_node, iicbb_get_node),
129 driver_t iicbb_driver = {
132 sizeof(struct iicbb_softc),
135 devclass_t iicbb_devclass;
138 iicbb_probe(device_t dev)
140 device_set_desc(dev, "I2C bit-banging driver");
146 iicbb_attach(device_t dev)
148 struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
150 sc->iicbus = device_add_child(dev, "iicbus", -1);
154 sc->scl_low_timeout = DEFAULT_SCL_LOW_TIMEOUT;
156 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
157 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
158 "delay", CTLFLAG_RD, &sc->udelay,
159 0, "Signal change delay controlled by bus frequency, microseconds");
161 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
162 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
163 "scl_low_timeout", CTLFLAG_RWTUN, &sc->scl_low_timeout,
164 0, "SCL low timeout, microseconds");
165 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
166 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
167 "io_latency", CTLFLAG_RWTUN, &sc->io_latency,
168 0, "Estimate of pin toggling latency, microseconds");
170 bus_generic_attach(dev);
175 iicbb_detach(device_t dev)
178 bus_generic_detach(dev);
179 device_delete_children(dev);
186 iicbb_get_node(device_t bus, device_t dev)
189 /* We only have one child, the I2C bus, which needs our own node. */
190 return (ofw_bus_get_node(bus));
195 iicbb_child_detached( device_t dev, device_t child )
197 struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
199 if (child == sc->iicbus)
204 iicbb_print_child(device_t bus, device_t dev)
210 retval += bus_print_child_header(bus, dev);
211 /* retrieve the interface I2C address */
212 error = IICBB_RESET(device_get_parent(bus), IIC_FASTEST, 0, &oldaddr);
213 if (error == IIC_ENOADDR) {
214 retval += printf(" on %s master-only\n",
215 device_get_nameunit(bus));
217 /* restore the address */
218 IICBB_RESET(device_get_parent(bus), IIC_FASTEST, oldaddr, NULL);
220 retval += printf(" on %s addr 0x%x\n",
221 device_get_nameunit(bus), oldaddr & 0xff);
229 static int i2c_debug = 0;
231 SYSCTL_DECL(_hw_i2c);
232 SYSCTL_INT(_hw_i2c, OID_AUTO, iicbb_debug, CTLFLAG_RWTUN,
233 &i2c_debug, 0, "Enable i2c bit-banging driver debug");
235 #define I2C_DEBUG(x) do { \
236 if (i2c_debug) (x); \
242 #define I2C_GETSDA(dev) (IICBB_GETSDA(device_get_parent(dev)))
243 #define I2C_SETSDA(dev, x) (IICBB_SETSDA(device_get_parent(dev), x))
244 #define I2C_GETSCL(dev) (IICBB_GETSCL(device_get_parent(dev)))
245 #define I2C_SETSCL(dev, x) (IICBB_SETSCL(device_get_parent(dev), x))
248 iicbb_waitforscl(device_t dev)
250 struct iicbb_softc *sc = device_get_softc(dev);
251 sbintime_t fast_timeout;
252 sbintime_t now, timeout;
254 /* Spin for up to 1 ms, then switch to pause. */
256 fast_timeout = now + SBT_1MS;
257 timeout = now + sc->scl_low_timeout * SBT_1US;
262 } while (now < fast_timeout);
264 I2C_DEBUG(printf("."));
265 pause_sbt("iicbb-scl-low", SBT_1MS, C_PREL(8), 0);
269 } while (now < timeout);
271 I2C_DEBUG(printf("*"));
272 return (IIC_ETIMEOUT);
275 /* Start the high phase of the clock. */
277 iicbb_clockin(device_t dev, int sda)
281 * Precondition: SCL is low.
283 * - set SDA to the value;
284 * - release SCL and wait until it's high.
285 * The caller is responsible for keeping SCL high for udelay.
287 * There should be a data set-up time, 250 ns minimum, between setting
288 * SDA and raising SCL. It's expected that the I/O access latency will
289 * naturally provide that delay.
291 I2C_SETSDA(dev, sda);
293 return (iicbb_waitforscl(dev));
297 * End the high phase of the clock and wait out the low phase
298 * as nothing interesting happens during it anyway.
301 iicbb_clockout(device_t dev)
303 struct iicbb_softc *sc = device_get_softc(dev);
306 * Precondition: SCL is high.
308 * - pull SCL low and hold for udelay.
315 iicbb_sendbit(device_t dev, int bit)
317 struct iicbb_softc *sc = device_get_softc(dev);
320 err = iicbb_clockin(dev, bit);
329 * Waiting for ACKNOWLEDGE.
331 * When a chip is being addressed or has received data it will issue an
332 * ACKNOWLEDGE pulse. Therefore the MASTER must release the DATA line
333 * (set it to high level) and then release the CLOCK line.
334 * Now it must wait for the SLAVE to pull the DATA line low.
335 * Actually on the bus this looks like a START condition so nothing happens
336 * because of the fact that the IC's that have not been addressed are doing
339 * When the SLAVE has pulled this line low the MASTER will take the CLOCK
340 * line low and then the SLAVE will release the SDA (data) line.
343 iicbb_getack(device_t dev)
345 struct iicbb_softc *sc = device_get_softc(dev);
349 /* Release SDA so that the slave can drive it. */
350 err = iicbb_clockin(dev, 1);
352 I2C_DEBUG(printf("! "));
356 /* Sample SDA until ACK (low) or udelay runs out. */
357 for (t = 0; t < sc->udelay; t++) {
358 noack = I2C_GETSDA(dev);
364 DELAY(sc->udelay - t);
367 I2C_DEBUG(printf("%c ", noack ? '-' : '+'));
368 return (noack ? IIC_ENOACK : 0);
372 iicbb_sendbyte(device_t dev, uint8_t data)
376 for (i = 7; i >= 0; i--) {
377 err = iicbb_sendbit(dev, (data & (1 << i)) != 0);
379 I2C_DEBUG(printf("w!"));
383 I2C_DEBUG(printf("w%02x", data));
388 iicbb_readbyte(device_t dev, bool last, uint8_t *data)
390 struct iicbb_softc *sc = device_get_softc(dev);
394 * Release SDA so that the slave can drive it.
395 * We do not use iicbb_clockin() here because we need to release SDA
396 * only once and then we just pulse the SCL.
400 for (i = 7; i >= 0; i--) {
402 err = iicbb_waitforscl(dev);
404 I2C_DEBUG(printf("r! "));
407 DELAY((sc->udelay + 1) / 2);
410 DELAY((sc->udelay + 1) / 2);
415 * Send master->slave ACK (low) for more data,
416 * NoACK (high) otherwise.
418 iicbb_sendbit(dev, last);
419 I2C_DEBUG(printf("r%02x%c ", *data, last ? '-' : '+'));
424 iicbb_callback(device_t dev, int index, caddr_t data)
426 return (IICBB_CALLBACK(device_get_parent(dev), index, data));
430 iicbb_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
432 iicbb_set_speed(device_get_softc(dev), speed);
433 return (IICBB_RESET(device_get_parent(dev), speed, addr, oldaddr));
437 iicbb_start_impl(device_t dev, u_char slave, bool repstart)
439 struct iicbb_softc *sc = device_get_softc(dev);
443 I2C_DEBUG(printf("<<"));
445 /* SCL must be high on the idle bus. */
446 if (iicbb_waitforscl(dev) != 0) {
447 I2C_DEBUG(printf("C!\n"));
448 return (IIC_EBUSERR);
451 I2C_DEBUG(printf("<"));
452 error = iicbb_clockin(dev, 1);
456 /* SDA will go low in the middle of the SCL high phase. */
457 DELAY((sc->udelay + 1) / 2);
461 * SDA must be high after the earlier stop condition or the end
462 * of Ack/NoAck pulse.
464 if (!I2C_GETSDA(dev)) {
465 I2C_DEBUG(printf("D!\n"));
466 return (IIC_EBUSERR);
469 /* Start: SDA high->low. */
472 /* Wait the second half of the SCL high phase. */
473 DELAY((sc->udelay + 1) / 2);
475 /* Pull SCL low to keep the bus reserved. */
479 error = iicbb_sendbyte(dev, slave);
483 error = iicbb_getack(dev);
485 (void)iicbb_stop(dev);
489 /* NB: the timeout is ignored. */
491 iicbb_start(device_t dev, u_char slave, int timeout)
493 return (iicbb_start_impl(dev, slave, false));
496 /* NB: the timeout is ignored. */
498 iicbb_repstart(device_t dev, u_char slave, int timeout)
500 return (iicbb_start_impl(dev, slave, true));
504 iicbb_stop(device_t dev)
506 struct iicbb_softc *sc = device_get_softc(dev);
510 * Stop: SDA goes from low to high in the middle of the SCL high phase.
512 err = iicbb_clockin(dev, 0);
515 DELAY((sc->udelay + 1) / 2);
517 DELAY((sc->udelay + 1) / 2);
519 I2C_DEBUG(printf("%s>>", err != 0 ? "!" : ""));
520 I2C_DEBUG(printf("\n"));
524 /* NB: the timeout is ignored. */
526 iicbb_write(device_t dev, const char *buf, int len, int *sent, int timeout)
528 int bytes, error = 0;
533 iicbb_sendbyte(dev, (uint8_t)*buf++);
536 error = iicbb_getack(dev);
547 /* NB: whatever delay is, it's ignored. */
549 iicbb_read(device_t dev, char *buf, int len, int *read, int last, int delay)
555 err = iicbb_readbyte(dev, (len == 1) ? last : 0,
569 iicbb_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
573 error = IICBB_PRE_XFER(device_get_parent(dev));
577 error = iicbus_transfer_gen(dev, msgs, nmsgs);
579 IICBB_POST_XFER(device_get_parent(dev));
584 iicbb_set_speed(struct iicbb_softc *sc, u_char speed)
590 * udelay is half a period, the clock is held high or low for this long.
592 busfreq = IICBUS_GET_FREQUENCY(sc->iicbus, speed);
593 period = 1000000 / 2 / busfreq; /* Hz -> uS */
594 period -= sc->io_latency;
595 sc->udelay = MAX(period, 1);
598 DRIVER_MODULE(iicbus, iicbb, iicbus_driver, iicbus_devclass, 0, 0);
600 MODULE_DEPEND(iicbb, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
601 MODULE_VERSION(iicbb, IICBB_MODVER);