1998-10-31 11:26:38 +00:00
|
|
|
/*-
|
2002-03-23 15:49:15 +00:00
|
|
|
* Copyright (c) 1998, 2001 Nicolas Souchu
|
1998-10-31 11:26:38 +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.
|
|
|
|
*/
|
|
|
|
|
2003-08-24 17:55:58 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1998-10-31 11:26:38 +00:00
|
|
|
/*
|
|
|
|
* I2C support for the bti2c chipset.
|
|
|
|
*
|
|
|
|
* From brooktree848.c <fsmp@freefall.org>
|
|
|
|
*/
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
#include "opt_bktr.h"
|
|
|
|
|
1998-10-31 11:26:38 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
2002-03-23 15:49:15 +00:00
|
|
|
#include <sys/malloc.h>
|
1998-10-31 11:26:38 +00:00
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/bus.h>
|
|
|
|
#include <sys/uio.h>
|
2001-01-20 02:24:07 +00:00
|
|
|
|
2003-12-08 07:59:18 +00:00
|
|
|
#if __FreeBSD_version < 500014
|
1999-08-30 09:28:47 +00:00
|
|
|
#include <sys/select.h>
|
2003-12-08 07:59:18 +00:00
|
|
|
#else
|
|
|
|
#include <sys/selinfo.h>
|
2001-01-20 02:24:07 +00:00
|
|
|
#endif
|
1998-10-31 11:26:38 +00:00
|
|
|
|
2000-10-19 07:33:28 +00:00
|
|
|
#if (__FreeBSD_version < 500000)
|
|
|
|
#include <machine/clock.h> /* for DELAY */
|
2003-08-22 05:54:52 +00:00
|
|
|
#include <pci/pcivar.h>
|
|
|
|
#include <pci/pcireg.h>
|
|
|
|
#else
|
|
|
|
#include <dev/pci/pcivar.h>
|
|
|
|
#include <dev/pci/pcireg.h>
|
2000-10-19 07:33:28 +00:00
|
|
|
#endif
|
1998-10-31 11:26:38 +00:00
|
|
|
|
2000-04-04 16:54:13 +00:00
|
|
|
#include <machine/bus.h>
|
|
|
|
#include <sys/bus.h>
|
|
|
|
|
2003-12-08 07:59:18 +00:00
|
|
|
#include <dev/bktr/ioctl_meteor.h>
|
|
|
|
#include <dev/bktr/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
|
2002-03-23 15:49:15 +00:00
|
|
|
#include <dev/bktr/bktr_reg.h>
|
1999-09-22 06:46:08 +00:00
|
|
|
#include <dev/bktr/bktr_i2c.h>
|
1998-10-31 11:26:38 +00:00
|
|
|
|
|
|
|
#include <dev/smbus/smbconf.h>
|
2002-03-23 15:49:15 +00:00
|
|
|
#include <dev/iicbus/iiconf.h>
|
1998-10-31 11:26:38 +00:00
|
|
|
|
|
|
|
#define I2C_DELAY 40
|
|
|
|
|
2002-03-25 21:22:35 +00:00
|
|
|
/* Compilation is void if BKTR_USE_FREEBSD_SMBUS is not
|
|
|
|
* defined. This allows bktr owners to have smbus active for there
|
|
|
|
* motherboard and still use their bktr without smbus.
|
|
|
|
*/
|
|
|
|
#if defined(BKTR_USE_FREEBSD_SMBUS)
|
|
|
|
|
1998-10-31 11:26:38 +00:00
|
|
|
#define BTI2C_DEBUG(x) if (bti2c_debug) (x)
|
|
|
|
static int bti2c_debug = 0;
|
|
|
|
|
|
|
|
/*
|
2000-04-04 16:54:13 +00:00
|
|
|
* Call this to pass the address of the bktr device to the
|
1998-10-31 11:26:38 +00:00
|
|
|
* bti2c_i2c layer and initialize all the I2C bus architecture
|
|
|
|
*/
|
2002-03-23 15:49:15 +00:00
|
|
|
int bt848_i2c_attach(device_t dev)
|
1998-10-31 11:26:38 +00:00
|
|
|
{
|
2002-03-23 15:49:15 +00:00
|
|
|
struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev);
|
|
|
|
struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc;
|
1998-10-31 11:26:38 +00:00
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
sc->smbus = device_add_child(dev, "smbus", -1);
|
|
|
|
sc->iicbb = device_add_child(dev, "iicbb", -1);
|
1998-10-31 11:26:38 +00:00
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
if (!sc->iicbb || !sc->smbus)
|
|
|
|
return ENXIO;
|
1998-10-31 11:26:38 +00:00
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
bus_generic_attach(dev);
|
1998-10-31 11:26:38 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
};
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
int bt848_i2c_detach(device_t dev)
|
1998-10-31 11:26:38 +00:00
|
|
|
{
|
2002-03-23 15:49:15 +00:00
|
|
|
struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev);
|
|
|
|
struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc;
|
|
|
|
int error = 0;
|
1998-10-31 11:26:38 +00:00
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
if ((error = bus_generic_detach(dev)))
|
|
|
|
goto error;
|
1998-10-31 11:26:38 +00:00
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
if (sc->iicbb && (error = device_delete_child(dev, sc->iicbb)))
|
|
|
|
goto error;
|
1998-10-31 11:26:38 +00:00
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
if (sc->smbus && (error = device_delete_child(dev, sc->smbus)))
|
|
|
|
goto error;
|
1998-10-31 11:26:38 +00:00
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
error:
|
|
|
|
return (error);
|
1998-10-31 11:26:38 +00:00
|
|
|
}
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
int bti2c_smb_callback(device_t dev, int index, caddr_t *data)
|
1998-10-31 11:26:38 +00:00
|
|
|
{
|
2002-03-23 15:49:15 +00:00
|
|
|
struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev);
|
|
|
|
struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc;
|
1998-10-31 11:26:38 +00:00
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
/* test each time if we already have/haven't the iicbus
|
|
|
|
* to avoid deadlocks
|
|
|
|
*/
|
|
|
|
switch (index) {
|
|
|
|
case SMB_REQUEST_BUS:
|
2002-03-23 15:49:15 +00:00
|
|
|
/* XXX test & set */
|
|
|
|
if (!sc->bus_owned) {
|
|
|
|
sc->bus_owned = 1;
|
|
|
|
} else
|
|
|
|
error = EWOULDBLOCK;
|
1998-10-31 11:26:38 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SMB_RELEASE_BUS:
|
2002-03-23 15:49:15 +00:00
|
|
|
/* XXX test & set */
|
|
|
|
if (sc->bus_owned) {
|
|
|
|
sc->bus_owned = 0;
|
|
|
|
} else
|
|
|
|
error = EINVAL;
|
1998-10-31 11:26:38 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
int bti2c_iic_callback(device_t dev, int index, caddr_t *data)
|
1998-10-31 11:26:38 +00:00
|
|
|
{
|
2002-03-23 15:49:15 +00:00
|
|
|
struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev);
|
|
|
|
struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc;
|
1998-10-31 11:26:38 +00:00
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
/* test each time if we already have/haven't the smbus
|
|
|
|
* to avoid deadlocks
|
|
|
|
*/
|
|
|
|
switch (index) {
|
|
|
|
case IIC_REQUEST_BUS:
|
2002-03-23 15:49:15 +00:00
|
|
|
/* XXX test & set */
|
|
|
|
if (!sc->bus_owned) {
|
|
|
|
sc->bus_owned = 1;
|
|
|
|
} else
|
|
|
|
error = EWOULDBLOCK;
|
1998-10-31 11:26:38 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IIC_RELEASE_BUS:
|
2002-03-23 15:49:15 +00:00
|
|
|
/* XXX test & set */
|
|
|
|
if (sc->bus_owned) {
|
|
|
|
sc->bus_owned = 0;
|
|
|
|
} else
|
|
|
|
error = EINVAL;
|
1998-10-31 11:26:38 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
int bti2c_iic_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr)
|
1998-10-31 11:26:38 +00:00
|
|
|
{
|
|
|
|
if (oldaddr)
|
|
|
|
*oldaddr = 0; /* XXX */
|
|
|
|
|
|
|
|
return (IIC_ENOADDR);
|
|
|
|
}
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
void bti2c_iic_setsda(device_t dev, int val)
|
1998-10-31 11:26:38 +00:00
|
|
|
{
|
2002-03-23 15:49:15 +00:00
|
|
|
struct bktr_softc *sc = (struct bktr_softc *)device_get_softc(dev);
|
|
|
|
int clock;
|
|
|
|
|
|
|
|
clock = INL(sc, BKTR_I2C_DATA_CTL) & 0x2;
|
|
|
|
|
|
|
|
if (val)
|
|
|
|
OUTL(sc, BKTR_I2C_DATA_CTL, clock | 1);
|
|
|
|
else
|
|
|
|
OUTL(sc, BKTR_I2C_DATA_CTL, clock);
|
1998-10-31 11:26:38 +00:00
|
|
|
|
|
|
|
DELAY(I2C_DELAY);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
void bti2c_iic_setscl(device_t dev, int val)
|
1998-10-31 11:26:38 +00:00
|
|
|
{
|
2002-03-23 15:49:15 +00:00
|
|
|
struct bktr_softc *sc = (struct bktr_softc *)device_get_softc(dev);
|
|
|
|
int data;
|
|
|
|
|
|
|
|
data = INL(sc, BKTR_I2C_DATA_CTL) & 0x1;
|
|
|
|
|
|
|
|
if (val)
|
|
|
|
OUTL(sc, BKTR_I2C_DATA_CTL, 0x2 | data);
|
|
|
|
else
|
|
|
|
OUTL(sc, BKTR_I2C_DATA_CTL, data);
|
1998-10-31 11:26:38 +00:00
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
DELAY(I2C_DELAY);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
bti2c_iic_getsda(device_t dev)
|
|
|
|
{
|
|
|
|
struct bktr_softc *sc = (struct bktr_softc *)device_get_softc(dev);
|
|
|
|
|
|
|
|
return (INL(sc,BKTR_I2C_DATA_CTL) & 0x1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
bti2c_iic_getscl(device_t dev)
|
|
|
|
{
|
|
|
|
return (0);
|
1998-10-31 11:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-03-23 15:49:15 +00:00
|
|
|
bti2c_write(struct bktr_softc *sc, u_long data)
|
1998-10-31 11:26:38 +00:00
|
|
|
{
|
|
|
|
u_long x;
|
|
|
|
|
|
|
|
/* clear status bits */
|
2002-03-23 15:49:15 +00:00
|
|
|
OUTL(sc, BKTR_INT_STAT, (BT848_INT_RACK | BT848_INT_I2CDONE));
|
1998-10-31 11:26:38 +00:00
|
|
|
|
|
|
|
BTI2C_DEBUG(printf("w%lx", data));
|
|
|
|
|
|
|
|
/* write the address and data */
|
2002-03-23 15:49:15 +00:00
|
|
|
OUTL(sc, BKTR_I2C_DATA_CTL, data);
|
1998-10-31 11:26:38 +00:00
|
|
|
|
|
|
|
/* wait for completion */
|
|
|
|
for ( x = 0x7fffffff; x; --x ) { /* safety valve */
|
2002-03-23 15:49:15 +00:00
|
|
|
if ( INL(sc, BKTR_INT_STAT) & BT848_INT_I2CDONE )
|
1998-10-31 11:26:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check for ACK */
|
2002-03-23 15:49:15 +00:00
|
|
|
if ( !x || !( INL(sc, BKTR_INT_STAT) & BT848_INT_RACK) ) {
|
1998-10-31 11:26:38 +00:00
|
|
|
BTI2C_DEBUG(printf("%c%c", (!x)?'+':'-',
|
2002-03-23 15:49:15 +00:00
|
|
|
(!( INL(sc, BKTR_INT_STAT) & BT848_INT_RACK))?'+':'-'));
|
1998-10-31 11:26:38 +00:00
|
|
|
return (SMB_ENOACK);
|
|
|
|
}
|
|
|
|
BTI2C_DEBUG(printf("+"));
|
|
|
|
|
|
|
|
/* return OK */
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
int
|
1998-10-31 11:26:38 +00:00
|
|
|
bti2c_smb_writeb(device_t dev, u_char slave, char cmd, char byte)
|
|
|
|
{
|
2002-03-23 15:49:15 +00:00
|
|
|
struct bktr_softc *sc = (struct bktr_softc *)device_get_softc(dev);
|
1998-10-31 11:26:38 +00:00
|
|
|
u_long data;
|
|
|
|
|
1998-11-07 14:30:48 +00:00
|
|
|
data = ((slave & 0xff) << 24) | ((byte & 0xff) << 16) | (u_char)cmd;
|
1998-10-31 11:26:38 +00:00
|
|
|
|
2000-04-04 16:54:13 +00:00
|
|
|
return (bti2c_write(sc, data));
|
1998-10-31 11:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* byte1 becomes low byte of word
|
|
|
|
* byte2 becomes high byte of word
|
|
|
|
*/
|
2002-03-23 15:49:15 +00:00
|
|
|
int
|
1998-10-31 11:26:38 +00:00
|
|
|
bti2c_smb_writew(device_t dev, u_char slave, char cmd, short word)
|
|
|
|
{
|
2002-03-23 15:49:15 +00:00
|
|
|
struct bktr_softc *sc = (struct bktr_softc *)device_get_softc(dev);
|
1998-10-31 11:26:38 +00:00
|
|
|
u_long data;
|
|
|
|
char low, high;
|
|
|
|
|
|
|
|
low = (char)(word & 0xff);
|
|
|
|
high = (char)((word & 0xff00) >> 8);
|
|
|
|
|
|
|
|
data = ((slave & 0xff) << 24) | ((low & 0xff) << 16) |
|
1998-11-07 14:30:48 +00:00
|
|
|
((high & 0xff) << 8) | BT848_DATA_CTL_I2CW3B | (u_char)cmd;
|
1998-10-31 11:26:38 +00:00
|
|
|
|
2000-04-04 16:54:13 +00:00
|
|
|
return (bti2c_write(sc, data));
|
1998-10-31 11:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The Bt878 and Bt879 differed on the treatment of i2c commands
|
|
|
|
*/
|
2002-03-23 15:49:15 +00:00
|
|
|
int
|
1998-10-31 11:26:38 +00:00
|
|
|
bti2c_smb_readb(device_t dev, u_char slave, char cmd, char *byte)
|
|
|
|
{
|
2002-03-23 15:49:15 +00:00
|
|
|
struct bktr_softc *sc = (struct bktr_softc *)device_get_softc(dev);
|
1998-10-31 11:26:38 +00:00
|
|
|
u_long x;
|
|
|
|
|
|
|
|
/* clear status bits */
|
2000-04-04 16:54:13 +00:00
|
|
|
OUTL(sc,BKTR_INT_STAT, (BT848_INT_RACK | BT848_INT_I2CDONE));
|
1998-10-31 11:26:38 +00:00
|
|
|
|
2000-04-04 16:54:13 +00:00
|
|
|
OUTL(sc,BKTR_I2C_DATA_CTL, ((slave & 0xff) << 24) | (u_char)cmd);;
|
1998-10-31 11:26:38 +00:00
|
|
|
|
1998-11-07 14:30:48 +00:00
|
|
|
BTI2C_DEBUG(printf("r%lx/", (u_long)(((slave & 0xff) << 24) | (u_char)cmd)));
|
1998-10-31 11:26:38 +00:00
|
|
|
|
|
|
|
/* wait for completion */
|
|
|
|
for ( x = 0x7fffffff; x; --x ) { /* safety valve */
|
2000-04-04 16:54:13 +00:00
|
|
|
if ( INL(sc,BKTR_INT_STAT) & BT848_INT_I2CDONE )
|
1998-10-31 11:26:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check for ACK */
|
2000-04-04 16:54:13 +00:00
|
|
|
if ( !x || !(INL(sc,BKTR_INT_STAT) & BT848_INT_RACK) ) {
|
1998-10-31 11:26:38 +00:00
|
|
|
BTI2C_DEBUG(printf("r%c%c", (!x)?'+':'-',
|
2000-04-04 16:54:13 +00:00
|
|
|
(!( INL(sc,BKTR_INT_STAT) & BT848_INT_RACK))?'+':'-'));
|
1998-10-31 11:26:38 +00:00
|
|
|
return (SMB_ENOACK);
|
|
|
|
}
|
|
|
|
|
2000-04-04 16:54:13 +00:00
|
|
|
*byte = (char)((INL(sc,BKTR_I2C_DATA_CTL) >> 8) & 0xff);
|
1998-10-31 11:26:38 +00:00
|
|
|
BTI2C_DEBUG(printf("r%x+", *byte));
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
2002-03-25 21:22:35 +00:00
|
|
|
|
|
|
|
#endif /* defined(BKTR_USE_FREEBSD_SMBUS) */
|