#include <sys/cdefs.h>
#include "opt_platform.h"
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/sysctl.h>
#include <sys/uio.h>
#include <machine/cpu.h>
#ifdef FDT
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <dev/fdt/fdt_common.h>
#endif
#include <dev/iicbus/iiconf.h>
#include <dev/iicbus/iicbus.h>
#include <dev/smbus/smbconf.h>
#include "iicbus_if.h"
#include "iicbb_if.h"
#define DEFAULT_SCL_LOW_TIMEOUT (25 * 1000)
struct iicbb_softc {
device_t iicbus;
u_int udelay;
u_int io_latency;
u_int scl_low_timeout;
};
static int iicbb_attach(device_t);
static void iicbb_child_detached(device_t, device_t);
static int iicbb_print_child(device_t, device_t);
static int iicbb_probe(device_t);
static int iicbb_callback(device_t, int, caddr_t);
static int iicbb_start(device_t, u_char, int);
static int iicbb_repstart(device_t, u_char, int);
static int iicbb_stop(device_t);
static int iicbb_write(device_t, const char *, int, int *, int);
static int iicbb_read(device_t, char *, int, int *, int, int);
static int iicbb_reset(device_t, u_char, u_char, u_char *);
static int iicbb_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs);
static void iicbb_set_speed(struct iicbb_softc *sc, u_char);
#ifdef FDT
static phandle_t iicbb_get_node(device_t, device_t);
#endif
static device_method_t iicbb_methods[] = {
DEVMETHOD(device_probe, iicbb_probe),
DEVMETHOD(device_attach, iicbb_attach),
DEVMETHOD(device_detach, bus_generic_detach),
DEVMETHOD(bus_child_detached, iicbb_child_detached),
DEVMETHOD(bus_print_child, iicbb_print_child),
DEVMETHOD(iicbus_callback, iicbb_callback),
DEVMETHOD(iicbus_start, iicbb_start),
DEVMETHOD(iicbus_repeated_start, iicbb_repstart),
DEVMETHOD(iicbus_stop, iicbb_stop),
DEVMETHOD(iicbus_write, iicbb_write),
DEVMETHOD(iicbus_read, iicbb_read),
DEVMETHOD(iicbus_reset, iicbb_reset),
DEVMETHOD(iicbus_transfer, iicbb_transfer),
#ifdef FDT
DEVMETHOD(ofw_bus_get_node, iicbb_get_node),
#endif
DEVMETHOD_END
};
driver_t iicbb_driver = {
"iicbb",
iicbb_methods,
sizeof(struct iicbb_softc),
};
static int
iicbb_probe(device_t dev)
{
device_set_desc(dev, "I2C bit-banging driver");
return (0);
}
static int
iicbb_attach(device_t dev)
{
struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
sc->iicbus = device_add_child(dev, "iicbus", DEVICE_UNIT_ANY);
if (!sc->iicbus)
return (ENXIO);
sc->scl_low_timeout = DEFAULT_SCL_LOW_TIMEOUT;
SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
"delay", CTLFLAG_RD, &sc->udelay,
0, "Signal change delay controlled by bus frequency, microseconds");
SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
"scl_low_timeout", CTLFLAG_RWTUN, &sc->scl_low_timeout,
0, "SCL low timeout, microseconds");
SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
"io_latency", CTLFLAG_RWTUN, &sc->io_latency,
0, "Estimate of pin toggling latency, microseconds");
bus_attach_children(dev);
return (0);
}
#ifdef FDT
static phandle_t
iicbb_get_node(device_t bus, device_t dev)
{
return (ofw_bus_get_node(bus));
}
#endif
static void
iicbb_child_detached( device_t dev, device_t child )
{
struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
if (child == sc->iicbus)
sc->iicbus = NULL;
}
static int
iicbb_print_child(device_t bus, device_t dev)
{
int error;
int retval = 0;
u_char oldaddr;
retval += bus_print_child_header(bus, dev);
error = IICBB_RESET(device_get_parent(bus), IIC_FASTEST, 0, &oldaddr);
if (error == IIC_ENOADDR) {
retval += printf(" on %s master-only\n",
device_get_nameunit(bus));
} else {
IICBB_RESET(device_get_parent(bus), IIC_FASTEST, oldaddr, NULL);
retval += printf(" on %s addr 0x%x\n",
device_get_nameunit(bus), oldaddr & 0xff);
}
return (retval);
}
#define IICBB_DEBUG
#ifdef IICBB_DEBUG
static int i2c_debug = 0;
SYSCTL_DECL(_hw_i2c);
SYSCTL_INT(_hw_i2c, OID_AUTO, iicbb_debug, CTLFLAG_RWTUN,
&i2c_debug, 0, "Enable i2c bit-banging driver debug");
#define I2C_DEBUG(x) do { \
if (i2c_debug) (x); \
} while (0)
#else
#define I2C_DEBUG(x)
#endif
#define I2C_GETSDA(dev) (IICBB_GETSDA(device_get_parent(dev)))
#define I2C_SETSDA(dev, x) (IICBB_SETSDA(device_get_parent(dev), x))
#define I2C_GETSCL(dev) (IICBB_GETSCL(device_get_parent(dev)))
#define I2C_SETSCL(dev, x) (IICBB_SETSCL(device_get_parent(dev), x))
static int
iicbb_waitforscl(device_t dev)
{
struct iicbb_softc *sc = device_get_softc(dev);
sbintime_t fast_timeout;
sbintime_t now, timeout;
now = sbinuptime();
fast_timeout = now + SBT_1MS;
timeout = now + sc->scl_low_timeout * SBT_1US;
do {
if (I2C_GETSCL(dev))
return (0);
cpu_spinwait();
now = sbinuptime();
} while (now < fast_timeout);
do {
I2C_DEBUG(printf("."));
pause_sbt("iicbb-scl-low", SBT_1MS, 0, C_PREL(2));
if (I2C_GETSCL(dev))
return (0);
now = sbinuptime();
} while (now < timeout);
I2C_DEBUG(printf("*"));
return (IIC_ETIMEOUT);
}
static int
iicbb_clockin(device_t dev, int sda)
{
I2C_SETSDA(dev, sda);
I2C_SETSCL(dev, 1);
return (iicbb_waitforscl(dev));
}
static void
iicbb_clockout(device_t dev)
{
struct iicbb_softc *sc = device_get_softc(dev);
I2C_SETSCL(dev, 0);
DELAY(sc->udelay);
}
static int
iicbb_sendbit(device_t dev, int bit)
{
struct iicbb_softc *sc = device_get_softc(dev);
int err;
err = iicbb_clockin(dev, bit);
if (err != 0)
return (err);
DELAY(sc->udelay);
iicbb_clockout(dev);
return (0);
}
static int
iicbb_getack(device_t dev)
{
struct iicbb_softc *sc = device_get_softc(dev);
int noack, err;
int t = 0;
err = iicbb_clockin(dev, 1);
if (err != 0) {
I2C_DEBUG(printf("! "));
return (err);
}
do {
noack = I2C_GETSDA(dev);
if (!noack)
break;
DELAY(1);
t++;
} while(t < sc->udelay);
DELAY(sc->udelay - t);
iicbb_clockout(dev);
I2C_DEBUG(printf("%c ", noack ? '-' : '+'));
return (noack ? IIC_ENOACK : 0);
}
static int
iicbb_sendbyte(device_t dev, uint8_t data)
{
int err, i;
for (i = 7; i >= 0; i--) {
err = iicbb_sendbit(dev, (data & (1 << i)) != 0);
if (err != 0) {
I2C_DEBUG(printf("w!"));
return (err);
}
}
I2C_DEBUG(printf("w%02x", data));
return (0);
}
static int
iicbb_readbyte(device_t dev, bool last, uint8_t *data)
{
struct iicbb_softc *sc = device_get_softc(dev);
int i, err;
*data = 0;
I2C_SETSDA(dev, 1);
for (i = 7; i >= 0; i--) {
I2C_SETSCL(dev, 1);
err = iicbb_waitforscl(dev);
if (err != 0) {
I2C_DEBUG(printf("r! "));
return (err);
}
DELAY((sc->udelay + 1) / 2);
if (I2C_GETSDA(dev))
*data |= 1 << i;
DELAY((sc->udelay + 1) / 2);
iicbb_clockout(dev);
}
iicbb_sendbit(dev, last);
I2C_DEBUG(printf("r%02x%c ", *data, last ? '-' : '+'));
return (0);
}
static int
iicbb_callback(device_t dev, int index, caddr_t data)
{
return (IICBB_CALLBACK(device_get_parent(dev), index, data));
}
static int
iicbb_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
{
iicbb_set_speed(device_get_softc(dev), speed);
return (IICBB_RESET(device_get_parent(dev), speed, addr, oldaddr));
}
static int
iicbb_start_impl(device_t dev, u_char slave, bool repstart)
{
struct iicbb_softc *sc = device_get_softc(dev);
int error;
if (!repstart) {
I2C_DEBUG(printf("<<"));
if (iicbb_waitforscl(dev) != 0) {
I2C_DEBUG(printf("C!\n"));
return (IIC_EBUSERR);
}
} else {
I2C_DEBUG(printf("<"));
error = iicbb_clockin(dev, 1);
if (error != 0)
return (error);
DELAY((sc->udelay + 1) / 2);
}
if (!I2C_GETSDA(dev)) {
I2C_DEBUG(printf("D!\n"));
return (IIC_EBUSERR);
}
I2C_SETSDA(dev, 0);
DELAY((sc->udelay + 1) / 2);
iicbb_clockout(dev);
error = iicbb_sendbyte(dev, slave);
if (error == 0)
error = iicbb_getack(dev);
if (error != 0)
(void)iicbb_stop(dev);
return (error);
}
static int
iicbb_start(device_t dev, u_char slave, int timeout)
{
return (iicbb_start_impl(dev, slave, false));
}
static int
iicbb_repstart(device_t dev, u_char slave, int timeout)
{
return (iicbb_start_impl(dev, slave, true));
}
static int
iicbb_stop(device_t dev)
{
struct iicbb_softc *sc = device_get_softc(dev);
int err = 0;
err = iicbb_clockin(dev, 0);
if (err != 0)
return (err);
DELAY((sc->udelay + 1) / 2);
I2C_SETSDA(dev, 1);
DELAY((sc->udelay + 1) / 2);
I2C_DEBUG(printf("%s>>", err != 0 ? "!" : ""));
I2C_DEBUG(printf("\n"));
return (err);
}
static int
iicbb_write(device_t dev, const char *buf, int len, int *sent, int timeout)
{
int bytes, error = 0;
bytes = 0;
while (len > 0) {
iicbb_sendbyte(dev, (uint8_t)*buf++);
error = iicbb_getack(dev);
if (error != 0)
break;
bytes++;
len--;
}
*sent = bytes;
return (error);
}
static int
iicbb_read(device_t dev, char *buf, int len, int *read, int last, int delay)
{
int bytes = 0;
int err = 0;
while (len > 0) {
err = iicbb_readbyte(dev, (len == 1) ? last : 0,
(uint8_t *)buf);
if (err != 0)
break;
buf++;
bytes++;
len--;
}
*read = bytes;
return (err);
}
static int
iicbb_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
{
int error;
error = IICBB_PRE_XFER(device_get_parent(dev));
if (error)
return (error);
error = iicbus_transfer_gen(dev, msgs, nmsgs);
IICBB_POST_XFER(device_get_parent(dev));
return (error);
}
static void
iicbb_set_speed(struct iicbb_softc *sc, u_char speed)
{
u_int busfreq;
int period;
busfreq = IICBUS_GET_FREQUENCY(sc->iicbus, speed);
period = 1000000 / 2 / busfreq;
period -= sc->io_latency;
sc->udelay = MAX(period, 1);
}
DRIVER_MODULE(iicbus, iicbb, iicbus_driver, 0, 0);
MODULE_DEPEND(iicbb, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
MODULE_VERSION(iicbb, IICBB_MODVER);