1998-10-31 11:31:07 +00:00
|
|
|
/*-
|
2002-03-23 15:49:15 +00:00
|
|
|
* Copyright (c) 1998, 2001 Nicolas Souchu
|
1998-10-31 11:31:07 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1999-08-28 01:08:13 +00:00
|
|
|
* $FreeBSD$
|
1998-10-31 11:31:07 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generic I2C bit-banging code
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* iicbus
|
|
|
|
* / \
|
|
|
|
* iicbb pcf
|
|
|
|
* | \
|
|
|
|
* bti2c lpbb
|
|
|
|
*
|
|
|
|
* From Linux I2C generic interface
|
|
|
|
* (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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 <dev/iicbus/iiconf.h>
|
|
|
|
#include <dev/iicbus/iicbus.h>
|
|
|
|
|
|
|
|
#include <dev/smbus/smbconf.h>
|
|
|
|
|
|
|
|
#include "iicbus_if.h"
|
|
|
|
#include "iicbb_if.h"
|
|
|
|
|
|
|
|
struct iicbb_softc {
|
2002-03-23 15:49:15 +00:00
|
|
|
device_t iicbus;
|
1998-10-31 11:31:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int iicbb_probe(device_t);
|
|
|
|
static int iicbb_attach(device_t);
|
2002-03-23 15:49:15 +00:00
|
|
|
static int iicbb_detach(device_t);
|
1999-07-29 01:03:04 +00:00
|
|
|
static int iicbb_print_child(device_t, device_t);
|
1998-10-31 11:31:07 +00:00
|
|
|
|
|
|
|
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, 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 device_method_t iicbb_methods[] = {
|
|
|
|
/* device interface */
|
|
|
|
DEVMETHOD(device_probe, iicbb_probe),
|
|
|
|
DEVMETHOD(device_attach, iicbb_attach),
|
2002-03-23 15:49:15 +00:00
|
|
|
DEVMETHOD(device_detach, iicbb_detach),
|
1998-10-31 11:31:07 +00:00
|
|
|
|
|
|
|
/* bus interface */
|
|
|
|
DEVMETHOD(bus_print_child, iicbb_print_child),
|
|
|
|
|
|
|
|
/* iicbus interface */
|
|
|
|
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),
|
|
|
|
|
|
|
|
{ 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static driver_t iicbb_driver = {
|
|
|
|
"iicbb",
|
|
|
|
iicbb_methods,
|
|
|
|
sizeof(struct iicbb_softc),
|
|
|
|
};
|
|
|
|
|
|
|
|
static devclass_t iicbb_devclass;
|
|
|
|
|
|
|
|
static int iicbb_probe(device_t dev)
|
|
|
|
{
|
2002-03-23 15:49:15 +00:00
|
|
|
device_set_desc(dev, "I2C bit-banging driver");
|
1998-10-31 11:31:07 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int iicbb_attach(device_t dev)
|
|
|
|
{
|
2002-03-23 15:49:15 +00:00
|
|
|
struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
|
|
|
|
|
|
|
|
bzero(sc, sizeof(struct iicbb_softc));
|
|
|
|
|
|
|
|
sc->iicbus = device_add_child(dev, "iicbus", -1);
|
|
|
|
|
|
|
|
if (!sc->iicbus)
|
|
|
|
return (ENXIO);
|
|
|
|
|
|
|
|
bus_generic_attach(dev);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int iicbb_detach(device_t dev)
|
|
|
|
{
|
|
|
|
struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
|
|
|
|
|
|
|
|
if (sc->iicbus) {
|
|
|
|
bus_generic_detach(dev);
|
|
|
|
device_delete_child(dev, sc->iicbus);
|
|
|
|
}
|
|
|
|
|
1998-10-31 11:31:07 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1999-07-29 01:03:04 +00:00
|
|
|
static int
|
1998-10-31 11:31:07 +00:00
|
|
|
iicbb_print_child(device_t bus, device_t dev)
|
|
|
|
{
|
|
|
|
int error;
|
1999-07-29 01:03:04 +00:00
|
|
|
int retval = 0;
|
1998-10-31 11:31:07 +00:00
|
|
|
u_char oldaddr;
|
|
|
|
|
1999-07-29 01:03:04 +00:00
|
|
|
retval += bus_print_child_header(bus, dev);
|
1998-10-31 11:31:07 +00:00
|
|
|
/* retrieve the interface I2C address */
|
1998-11-04 22:07:24 +00:00
|
|
|
error = IICBB_RESET(device_get_parent(bus), IIC_FASTEST, 0, &oldaddr);
|
1998-10-31 11:31:07 +00:00
|
|
|
if (error == IIC_ENOADDR) {
|
1999-07-29 01:03:04 +00:00
|
|
|
retval += printf(" on %s master-only\n",
|
|
|
|
device_get_nameunit(bus));
|
1998-10-31 11:31:07 +00:00
|
|
|
} else {
|
|
|
|
/* restore the address */
|
1998-11-04 22:07:24 +00:00
|
|
|
IICBB_RESET(device_get_parent(bus), IIC_FASTEST, oldaddr, NULL);
|
1998-10-31 11:31:07 +00:00
|
|
|
|
1999-07-29 01:03:04 +00:00
|
|
|
retval += printf(" on %s addr 0x%x\n",
|
|
|
|
device_get_nameunit(bus), oldaddr & 0xff);
|
1998-10-31 11:31:07 +00:00
|
|
|
}
|
|
|
|
|
1999-07-29 01:03:04 +00:00
|
|
|
return (retval);
|
1998-10-31 11:31:07 +00:00
|
|
|
}
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
#define IIC_DELAY 10
|
|
|
|
|
|
|
|
#define I2C_SETSDA(dev,val) do { \
|
|
|
|
IICBB_SETSDA(device_get_parent(dev), val); \
|
|
|
|
DELAY(IIC_DELAY); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define I2C_SETSCL(dev,val) do { \
|
|
|
|
iicbb_setscl(dev, val, 100); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define I2C_SET(dev,ctrl,data) do { \
|
|
|
|
I2C_SETSCL(dev, ctrl); \
|
|
|
|
I2C_SETSDA(dev, data); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define I2C_GETSDA(dev) (IICBB_GETSDA(device_get_parent(dev)))
|
1998-10-31 11:31:07 +00:00
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
#define I2C_GETSCL(dev) (IICBB_GETSCL(device_get_parent(dev)))
|
1998-10-31 11:31:07 +00:00
|
|
|
|
|
|
|
static int i2c_debug = 0;
|
2002-03-23 15:49:15 +00:00
|
|
|
#define I2C_DEBUG(x) do { \
|
|
|
|
if (i2c_debug) (x); \
|
|
|
|
} while (0)
|
1998-10-31 11:31:07 +00:00
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
#define I2C_LOG(format,args...) do { \
|
|
|
|
printf(format, args); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
static void iicbb_setscl(device_t dev, int val, int timeout)
|
|
|
|
{
|
|
|
|
int k = 0;
|
|
|
|
|
|
|
|
IICBB_SETSCL(device_get_parent(dev), val);
|
|
|
|
DELAY(IIC_DELAY);
|
|
|
|
|
|
|
|
while (val && !I2C_GETSCL(dev) && k++ < timeout) {
|
|
|
|
IICBB_SETSCL(device_get_parent(dev), val);
|
|
|
|
DELAY(IIC_DELAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void iicbb_one(device_t dev, int timeout)
|
1998-10-31 11:31:07 +00:00
|
|
|
{
|
|
|
|
I2C_SET(dev,0,1);
|
|
|
|
I2C_SET(dev,1,1);
|
|
|
|
I2C_SET(dev,0,1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
static void iicbb_zero(device_t dev, int timeout)
|
1998-10-31 11:31:07 +00:00
|
|
|
{
|
|
|
|
I2C_SET(dev,0,0);
|
|
|
|
I2C_SET(dev,1,0);
|
|
|
|
I2C_SET(dev,0,0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Waiting for ACKNOWLEDGE.
|
|
|
|
*
|
|
|
|
* When a chip is being addressed or has received data it will issue an
|
|
|
|
* ACKNOWLEDGE pulse. Therefore the MASTER must release the DATA line
|
|
|
|
* (set it to high level) and then release the CLOCK line.
|
|
|
|
* Now it must wait for the SLAVE to pull the DATA line low.
|
|
|
|
* Actually on the bus this looks like a START condition so nothing happens
|
|
|
|
* because of the fact that the IC's that have not been addressed are doing
|
|
|
|
* nothing.
|
|
|
|
*
|
|
|
|
* When the SLAVE has pulled this line low the MASTER will take the CLOCK
|
|
|
|
* line low and then the SLAVE will release the SDA (data) line.
|
|
|
|
*/
|
|
|
|
static int iicbb_ack(device_t dev, int timeout)
|
|
|
|
{
|
|
|
|
int noack;
|
2002-03-23 15:49:15 +00:00
|
|
|
int k = 0;
|
1998-10-31 11:31:07 +00:00
|
|
|
|
|
|
|
I2C_SET(dev,0,1);
|
|
|
|
I2C_SET(dev,1,1);
|
|
|
|
do {
|
2002-03-23 15:49:15 +00:00
|
|
|
noack = I2C_GETSDA(dev);
|
1998-10-31 11:31:07 +00:00
|
|
|
if (!noack)
|
|
|
|
break;
|
2002-03-23 15:49:15 +00:00
|
|
|
DELAY(10);
|
|
|
|
k += 10;
|
|
|
|
} while (k < timeout);
|
1998-10-31 11:31:07 +00:00
|
|
|
|
|
|
|
I2C_SET(dev,0,1);
|
|
|
|
I2C_DEBUG(printf("%c ",noack?'-':'+'));
|
|
|
|
|
|
|
|
return (noack);
|
|
|
|
}
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
static void iicbb_sendbyte(device_t dev, u_char data, int timeout)
|
1998-10-31 11:31:07 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
for (i=7; i>=0; i--) {
|
|
|
|
if (data&(1<<i)) {
|
|
|
|
iicbb_one(dev, timeout);
|
|
|
|
} else {
|
|
|
|
iicbb_zero(dev, timeout);
|
|
|
|
}
|
|
|
|
}
|
1998-10-31 11:31:07 +00:00
|
|
|
I2C_DEBUG(printf("w%02x",(int)data));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
static u_char iicbb_readbyte(device_t dev, int last, int timeout)
|
1998-10-31 11:31:07 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned char data=0;
|
|
|
|
|
|
|
|
I2C_SET(dev,0,1);
|
|
|
|
for (i=7; i>=0; i--)
|
|
|
|
{
|
|
|
|
I2C_SET(dev,1,1);
|
2002-03-23 15:49:15 +00:00
|
|
|
if (I2C_GETSDA(dev))
|
1998-10-31 11:31:07 +00:00
|
|
|
data |= (1<<i);
|
|
|
|
I2C_SET(dev,0,1);
|
|
|
|
}
|
2002-03-23 15:49:15 +00:00
|
|
|
if (last) {
|
|
|
|
iicbb_one(dev, timeout);
|
|
|
|
} else {
|
|
|
|
iicbb_zero(dev, timeout);
|
|
|
|
}
|
1998-10-31 11:31:07 +00:00
|
|
|
I2C_DEBUG(printf("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)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
I2C_DEBUG(printf("<"));
|
|
|
|
|
|
|
|
I2C_SET(dev,1,1);
|
|
|
|
I2C_SET(dev,1,0);
|
|
|
|
I2C_SET(dev,0,0);
|
|
|
|
|
|
|
|
/* send address */
|
2002-03-23 15:49:15 +00:00
|
|
|
iicbb_sendbyte(dev, slave, timeout);
|
1998-10-31 11:31:07 +00:00
|
|
|
|
|
|
|
/* check for ack */
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
I2C_SET(dev,0,0);
|
|
|
|
I2C_SET(dev,1,0);
|
|
|
|
I2C_SET(dev,1,1);
|
|
|
|
I2C_DEBUG(printf(">"));
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int iicbb_write(device_t dev, char * buf, int len, int *sent,
|
|
|
|
int timeout)
|
|
|
|
{
|
|
|
|
int bytes, error = 0;
|
|
|
|
|
|
|
|
bytes = 0;
|
|
|
|
while (len) {
|
|
|
|
/* send byte */
|
2002-03-23 15:49:15 +00:00
|
|
|
iicbb_sendbyte(dev,(u_char)*buf++, timeout);
|
1998-10-31 11:31:07 +00:00
|
|
|
|
|
|
|
/* check for ack */
|
|
|
|
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) {
|
|
|
|
/* XXX should insert delay here */
|
2002-03-23 15:49:15 +00:00
|
|
|
*buf++ = (char)iicbb_readbyte(dev, (len == 1) ? last : 0, delay);
|
1998-10-31 11:31:07 +00:00
|
|
|
|
|
|
|
bytes ++;
|
|
|
|
len --;
|
|
|
|
}
|
|
|
|
|
|
|
|
*read = bytes;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
DRIVER_MODULE(iicbb, bktr, iicbb_driver, iicbb_devclass, 0, 0);
|
1998-10-31 11:31:07 +00:00
|
|
|
DRIVER_MODULE(iicbb, lpbb, iicbb_driver, iicbb_devclass, 0, 0);
|
2002-03-23 15:49:15 +00:00
|
|
|
DRIVER_MODULE(iicbb, viapm, iicbb_driver, iicbb_devclass, 0, 0);
|
|
|
|
|
|
|
|
MODULE_DEPEND(iicbb, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
|
|
|
|
MODULE_VERSION(iicbb, IICBB_MODVER);
|