#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/uio.h>
#include <bus/iicbus/iiconf.h>
#include <bus/iicbus/iicbus.h>
#include <bus/smbus/smbconf.h>
#include "iicbus_if.h"
#include "iicbb_if.h"
struct iicbb_softc {
device_t iicbus;
int udelay;
};
static int iicbb_attach(device_t);
static void iicbb_child_detached(device_t, device_t);
static int iicbb_detach(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_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 device_method_t iicbb_methods[] = {
DEVMETHOD(device_probe, iicbb_probe),
DEVMETHOD(device_attach, iicbb_attach),
DEVMETHOD(device_detach, iicbb_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_start),
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),
DEVMETHOD_END
};
driver_t iicbb_driver = {
"iicbb",
iicbb_methods,
sizeof(struct iicbb_softc),
};
devclass_t iicbb_devclass;
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", -1);
if (!sc->iicbus)
return (ENXIO);
sc->udelay = 10;
bus_generic_attach(dev);
return (0);
}
static int
iicbb_detach(device_t dev)
{
struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
device_t child;
child = sc->iicbus;
bus_generic_detach(dev);
if (child)
device_delete_child(dev, child);
return (0);
}
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 += kprintf(" on %s master-only\n",
device_get_nameunit(bus));
} else {
IICBB_RESET(device_get_parent(bus), IIC_FASTEST, oldaddr, NULL);
retval += kprintf(" on %s addr 0x%x\n",
device_get_nameunit(bus), oldaddr & 0xff);
}
return (retval);
}
#define I2C_SETSDA(sc,dev,val) do { \
IICBB_SETSDA(device_get_parent(dev), val); \
DELAY(sc->udelay); \
} while (0)
#define I2C_SETSCL(dev,val) do { \
iicbb_setscl(dev, val, 100); \
} while (0)
#define I2C_SET(sc,dev,ctrl,data) do { \
I2C_SETSCL(dev, ctrl); \
I2C_SETSDA(sc, dev, data); \
} while (0)
#define I2C_GETSDA(dev) (IICBB_GETSDA(device_get_parent(dev)))
#define I2C_GETSCL(dev) (IICBB_GETSCL(device_get_parent(dev)))
static int i2c_debug = 0;
#define I2C_DEBUG(x) do { \
if (i2c_debug) (x); \
} while (0)
#define I2C_LOG(format,args...) do { \
kprintf(format, args); \
} while (0)
static void
iicbb_setscl(device_t dev, int val, int timeout)
{
struct iicbb_softc *sc = device_get_softc(dev);
int k = 0;
IICBB_SETSCL(device_get_parent(dev), val);
DELAY(sc->udelay);
while (val && !I2C_GETSCL(dev) && k++ < timeout) {
IICBB_SETSCL(device_get_parent(dev), val);
DELAY(sc->udelay);
}
return;
}
static void
iicbb_one(device_t dev, int timeout)
{
struct iicbb_softc *sc = device_get_softc(dev);
I2C_SET(sc,dev,0,1);
I2C_SET(sc,dev,1,1);
I2C_SET(sc,dev,0,1);
return;
}
static void
iicbb_zero(device_t dev, int timeout)
{
struct iicbb_softc *sc = device_get_softc(dev);
I2C_SET(sc,dev,0,0);
I2C_SET(sc,dev,1,0);
I2C_SET(sc,dev,0,0);
return;
}
static int
iicbb_ack(device_t dev, int timeout)
{
struct iicbb_softc *sc = device_get_softc(dev);
int noack;
int k = 0;
I2C_SET(sc,dev,0,1);
I2C_SET(sc,dev,1,1);
do {
noack = I2C_GETSDA(dev);
if (!noack)
break;
DELAY(1);
k++;
} while (k < timeout);
I2C_SET(sc,dev,0,1);
I2C_DEBUG(kprintf("%c ",noack?'-':'+'));
return (noack);
}
static void
iicbb_sendbyte(device_t dev, u_char data, int timeout)
{
int i;
for (i=7; i>=0; i--) {
if (data&(1<<i)) {
iicbb_one(dev, timeout);
} else {
iicbb_zero(dev, timeout);
}
}
I2C_DEBUG(kprintf("w%02x",(int)data));
return;
}
static u_char
iicbb_readbyte(device_t dev, int last, int timeout)
{
struct iicbb_softc *sc = device_get_softc(dev);
int i;
unsigned char data=0;
I2C_SET(sc,dev,0,1);
for (i=7; i>=0; i--)
{
I2C_SET(sc,dev,1,1);
if (I2C_GETSDA(dev))
data |= (1<<i);
I2C_SET(sc,dev,0,1);
}
if (last) {
iicbb_one(dev, timeout);
} else {
iicbb_zero(dev, timeout);
}
I2C_DEBUG(kprintf("r%02x%c ",(int)data,last?'-':'+'));
return data;
}
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)
{
return (IICBB_RESET(device_get_parent(dev), speed, addr, oldaddr));
}
static int
iicbb_start(device_t dev, u_char slave, int timeout)
{
struct iicbb_softc *sc = device_get_softc(dev);
int error;
I2C_DEBUG(kprintf("<"));
I2C_SET(sc,dev,1,1);
I2C_SET(sc,dev,1,0);
I2C_SET(sc,dev,0,0);
iicbb_sendbyte(dev, slave, timeout);
if (iicbb_ack(dev, timeout)) {
error = IIC_ENOACK;
goto error;
}
return(0);
error:
iicbb_stop(dev);
return (error);
}
static int
iicbb_stop(device_t dev)
{
struct iicbb_softc *sc = device_get_softc(dev);
I2C_SET(sc,dev,0,0);
I2C_SET(sc,dev,1,0);
I2C_SET(sc,dev,1,1);
I2C_DEBUG(kprintf(">"));
I2C_DEBUG(kprintf("\n"));
return (0);
}
static int
iicbb_write(device_t dev, const char *buf, int len, int *sent, int timeout)
{
int bytes, error = 0;
bytes = 0;
while (len) {
iicbb_sendbyte(dev,(u_char)*buf++, timeout);
if (iicbb_ack(dev, timeout)) {
error = IIC_ENOACK;
goto error;
}
bytes ++;
len --;
}
error:
*sent = bytes;
return (error);
}
static int
iicbb_read(device_t dev, char * buf, int len, int *read, int last, int delay)
{
int bytes;
bytes = 0;
while (len) {
*buf++ = (char)iicbb_readbyte(dev, (len == 1) ? last : 0, delay);
bytes ++;
len --;
}
*read = bytes;
return (0);
}
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);
}
DRIVER_MODULE(iicbb, bti2c, iicbb_driver, iicbb_devclass, NULL, NULL);
DRIVER_MODULE(iicbb, cxm_iic, iicbb_driver, iicbb_devclass, NULL, NULL);
DRIVER_MODULE(iicbb, intel_iic, iicbb_driver, iicbb_devclass, NULL, NULL);
DRIVER_MODULE(iicbb, lpbb, iicbb_driver, iicbb_devclass, NULL, NULL);
DRIVER_MODULE(iicbb, viapm, iicbb_driver, iicbb_devclass, NULL, NULL);
MODULE_DEPEND(iicbb, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
MODULE_VERSION(iicbb, IICBB_MODVER);