diff options
Diffstat (limited to 'sys/dev')
-rw-r--r-- | sys/dev/bktr/bktr_i2c.c | 300 | ||||
-rw-r--r-- | sys/dev/bktr/bktr_i2c.h | 17 | ||||
-rw-r--r-- | sys/dev/bktr/bktr_os.c | 38 | ||||
-rw-r--r-- | sys/dev/bktr/bktr_reg.h | 18 | ||||
-rw-r--r-- | sys/dev/iicbus/if_ic.c | 10 | ||||
-rw-r--r-- | sys/dev/iicbus/iic.c | 69 | ||||
-rw-r--r-- | sys/dev/iicbus/iicbb.c | 126 | ||||
-rw-r--r-- | sys/dev/iicbus/iicbb_if.m | 27 | ||||
-rw-r--r-- | sys/dev/iicbus/iicbus.c | 166 | ||||
-rw-r--r-- | sys/dev/iicbus/iiconf.c | 33 | ||||
-rw-r--r-- | sys/dev/iicbus/iiconf.h | 12 | ||||
-rw-r--r-- | sys/dev/iicbus/iicsmb.c | 106 | ||||
-rw-r--r-- | sys/dev/ppbus/lpbb.c | 71 | ||||
-rw-r--r-- | sys/dev/smbus/smb.c | 54 | ||||
-rw-r--r-- | sys/dev/smbus/smbconf.c | 34 | ||||
-rw-r--r-- | sys/dev/smbus/smbconf.h | 5 | ||||
-rw-r--r-- | sys/dev/smbus/smbus.c | 74 |
17 files changed, 533 insertions, 627 deletions
diff --git a/sys/dev/bktr/bktr_i2c.c b/sys/dev/bktr/bktr_i2c.c index 4f61e5c..20342d1 100644 --- a/sys/dev/bktr/bktr_i2c.c +++ b/sys/dev/bktr/bktr_i2c.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 1998 Nicolas Souchu + * Copyright (c) 1998, 2001 Nicolas Souchu * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -33,10 +33,13 @@ * From brooktree848.c <fsmp@freefall.org> */ +#include "opt_bktr.h" + #include "bktr.h" #include <sys/param.h> #include <sys/kernel.h> +#include <sys/malloc.h> #include <sys/systm.h> #include <sys/module.h> #include <sys/bus.h> @@ -62,196 +65,91 @@ #include <pci/pcireg.h> #include <machine/ioctl_meteor.h> #include <machine/ioctl_bt848.h> /* extensions to ioctl_meteor.h */ -#include <dev/bktr/bktr_reg.h> +#include <dev/bktr/bktr_reg.h> #include <dev/bktr/bktr_i2c.h> -#include <dev/iicbus/iiconf.h> -#include <dev/iicbus/iicbus.h> - #include <dev/smbus/smbconf.h> - -#include "iicbb_if.h" -#include "smbus_if.h" - - +#include <dev/iicbus/iiconf.h> #define I2C_DELAY 40 #define BTI2C_DEBUG(x) if (bti2c_debug) (x) static int bti2c_debug = 0; -struct bti2c_softc { - - bus_space_tag_t memt; /* Bus space register access */ - bus_space_handle_t memh; /* Bus space register access */ - - int iic_owned; /* 1 if we own the iicbus */ - int smb_owned; /* 1 if we own the smbbus */ - - device_t smbus; - device_t iicbus; -}; - -struct bt_data { - bus_space_tag_t memt; - bus_space_handle_t memh; -}; -struct bt_data btdata[NBKTR]; - -static int bti2c_probe(device_t); -static int bti2c_attach(device_t); - -static int bti2c_iic_callback(device_t, int, caddr_t *); -static void bti2c_iic_setlines(device_t, int, int); -static int bti2c_iic_getdataline(device_t); -static int bti2c_iic_reset(device_t, u_char, u_char, u_char *); - -static int bti2c_smb_callback(device_t, int, caddr_t *); -static int bti2c_smb_writeb(device_t dev, u_char slave, char cmd, char byte); -static int bti2c_smb_writew(device_t dev, u_char slave, char cmd, short word); -static int bti2c_smb_readb(device_t dev, u_char slave, char cmd, char *byte); - -static devclass_t bti2c_devclass; - -static device_method_t bti2c_methods[] = { - /* device interface */ - DEVMETHOD(device_probe, bti2c_probe), - DEVMETHOD(device_attach, bti2c_attach), - - /* bus interface */ - DEVMETHOD(bus_print_child, bus_generic_print_child), - - /* iicbb interface */ - DEVMETHOD(iicbb_callback, bti2c_iic_callback), - DEVMETHOD(iicbb_setlines, bti2c_iic_setlines), - DEVMETHOD(iicbb_getdataline, bti2c_iic_getdataline), - DEVMETHOD(iicbb_reset, bti2c_iic_reset), - - /* smbus interface */ - DEVMETHOD(smbus_callback, bti2c_smb_callback), - DEVMETHOD(smbus_writeb, bti2c_smb_writeb), - DEVMETHOD(smbus_writew, bti2c_smb_writew), - DEVMETHOD(smbus_readb, bti2c_smb_readb), - - { 0, 0 } -}; - -#if (__FreeBSD_version < 400000) -/* FreeBSD 3.x needs DRIVER_TYPE_MISC */ -static driver_t bti2c_driver = { - "bti2c", - bti2c_methods, - DRIVER_TYPE_MISC, - sizeof(struct bti2c_softc), -}; -#endif - -#if (__FreeBSD_version >=400000) - static driver_t bti2c_driver = { - "bti2c", - bti2c_methods, - sizeof(struct bti2c_softc), -}; -#endif - /* * Call this to pass the address of the bktr device to the * bti2c_i2c layer and initialize all the I2C bus architecture */ -int -bt848_i2c_attach(int unit, struct bktr_softc * bktr, struct bktr_i2c_softc *i2c_sc) +int bt848_i2c_attach(device_t dev) { - device_t interface; - device_t bitbang; + struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev); + struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc; - btdata[unit].memh = bktr->memh; - btdata[unit].memt = bktr->memt; + device_t *list; + int count; - /* XXX add the I2C interface to the root_bus until pcibus is ready */ -#if (__FreeBSD_version < 400000) - interface = device_add_child(root_bus, "bti2c", unit, NULL); -#else - interface = device_add_child(root_bus, "bti2c", unit); -#endif + sc->smbus = device_add_child(dev, "smbus", -1); + sc->iicbb = device_add_child(dev, "iicbb", -1); - /* add bit-banging generic code onto bti2c interface */ -#if (__FreeBSD_version < 400000) - bitbang = device_add_child(interface, "iicbb", -1, NULL); -#else - bitbang = device_add_child(interface, "iicbb", -1); -#endif - - /* probe and attach the interface, we need it NOW - * bit-banging code is also probed and attached */ - device_probe_and_attach(interface); - device_probe_and_attach(bitbang); + if (!sc->iicbb || !sc->smbus) + return ENXIO; - /* smb and i2c interfaces are available for the bt848 chip - * connect bit-banging generic code to an iicbus */ - if ((i2c_sc->iicbus = iicbus_alloc_bus(bitbang))) - device_probe_and_attach(i2c_sc->iicbus); + bus_generic_attach(dev); - /* hardware i2c is actually smb over the bti2c interface */ - if ((i2c_sc->smbus = smbus_alloc_bus(interface))) - device_probe_and_attach(i2c_sc->smbus); + /* the iicbus is the first child of device iicbb */ + device_get_children(sc->iicbb, &list, &count); + if (count) { + sc->iicbus = list[0]; + free(list, M_TEMP); + } return (0); }; -/* - * Not a real probe, we know the device exists since the device has - * been added after the successfull pci probe. - */ -static int -bti2c_probe(device_t dev) +int bt848_i2c_detach(device_t dev) { - device_set_desc(dev, "bt848 Hard/Soft I2C controller"); + struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev); + struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc; + int error = 0; - return (0); -} + if ((error = bus_generic_detach(dev))) + goto error; -static int -bti2c_attach(device_t dev) -{ - struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev); + if (sc->iicbb && (error = device_delete_child(dev, sc->iicbb))) + goto error; - /* XXX should use ivars with pcibus or pcibus methods to access - * onboard memory */ - sc->memh = btdata[device_get_unit(dev)].memh; - sc->memt = btdata[device_get_unit(dev)].memt; + if (sc->smbus && (error = device_delete_child(dev, sc->smbus))) + goto error; - return (0); +error: + return (error); } -static int -bti2c_smb_callback(device_t dev, int index, caddr_t *data) +int bti2c_smb_callback(device_t dev, int index, caddr_t *data) { - struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev); + struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev); + struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc; int error = 0; - int how; /* test each time if we already have/haven't the iicbus * to avoid deadlocks */ switch (index) { case SMB_REQUEST_BUS: - if (!sc->iic_owned) { - /* request the iicbus */ - how = *(int *)data; - error = iicbus_request_bus(sc->iicbus, dev, how); - if (!error) - sc->iic_owned = 1; - } + /* XXX test & set */ + if (!sc->bus_owned) { + sc->bus_owned = 1; + } else + error = EWOULDBLOCK; break; case SMB_RELEASE_BUS: - if (sc->iic_owned) { - /* release the iicbus */ - error = iicbus_release_bus(sc->iicbus, dev); - if (!error) - sc->iic_owned = 0; - } + /* XXX test & set */ + if (sc->bus_owned) { + sc->bus_owned = 0; + } else + error = EINVAL; break; default: @@ -261,34 +159,30 @@ bti2c_smb_callback(device_t dev, int index, caddr_t *data) return (error); } -static int -bti2c_iic_callback(device_t dev, int index, caddr_t *data) +int bti2c_iic_callback(device_t dev, int index, caddr_t *data) { - struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev); + struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev); + struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc; int error = 0; - int how; /* test each time if we already have/haven't the smbus * to avoid deadlocks */ switch (index) { case IIC_REQUEST_BUS: - if (!sc->smb_owned) { - /* request the smbus */ - how = *(int *)data; - error = smbus_request_bus(sc->smbus, dev, how); - if (!error) - sc->smb_owned = 1; - } + /* XXX test & set */ + if (!sc->bus_owned) { + sc->bus_owned = 1; + } else + error = EWOULDBLOCK; break; case IIC_RELEASE_BUS: - if (sc->smb_owned) { - /* release the smbus */ - error = smbus_release_bus(sc->smbus, dev); - if (!error) - sc->smb_owned = 0; - } + /* XXX test & set */ + if (sc->bus_owned) { + sc->bus_owned = 0; + } else + error = EINVAL; break; default: @@ -298,8 +192,7 @@ bti2c_iic_callback(device_t dev, int index, caddr_t *data) return (error); } -static int -bti2c_iic_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr) +int bti2c_iic_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr) { if (oldaddr) *oldaddr = 0; /* XXX */ @@ -307,48 +200,77 @@ bti2c_iic_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr) return (IIC_ENOADDR); } -static void -bti2c_iic_setlines(device_t dev, int ctrl, int data) +void bti2c_iic_setsda(device_t dev, int val) { - struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev); + 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); - OUTL(sc, BKTR_I2C_DATA_CTL, (ctrl << 1) | data);; DELAY(I2C_DELAY); return; } -static int -bti2c_iic_getdataline(device_t dev) +void bti2c_iic_setscl(device_t dev, int val) { - struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev); + 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); - return ( INL(sc,BKTR_I2C_DATA_CTL) & 0x1); + 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); } static int -bti2c_write(struct bti2c_softc* bti2c_sc, u_long data) +bti2c_write(struct bktr_softc *sc, u_long data) { u_long x; /* clear status bits */ - OUTL(bti2c_sc, BKTR_INT_STAT, (BT848_INT_RACK | BT848_INT_I2CDONE)); + OUTL(sc, BKTR_INT_STAT, (BT848_INT_RACK | BT848_INT_I2CDONE)); BTI2C_DEBUG(printf("w%lx", data)); /* write the address and data */ - OUTL(bti2c_sc, BKTR_I2C_DATA_CTL, data); + OUTL(sc, BKTR_I2C_DATA_CTL, data); /* wait for completion */ for ( x = 0x7fffffff; x; --x ) { /* safety valve */ - if ( INL(bti2c_sc, BKTR_INT_STAT) & BT848_INT_I2CDONE ) + if ( INL(sc, BKTR_INT_STAT) & BT848_INT_I2CDONE ) break; } /* check for ACK */ - if ( !x || !( INL(bti2c_sc, BKTR_INT_STAT) & BT848_INT_RACK) ) { + if ( !x || !( INL(sc, BKTR_INT_STAT) & BT848_INT_RACK) ) { BTI2C_DEBUG(printf("%c%c", (!x)?'+':'-', - (!( INL(bti2c_sc, BKTR_INT_STAT) & BT848_INT_RACK))?'+':'-')); + (!( INL(sc, BKTR_INT_STAT) & BT848_INT_RACK))?'+':'-')); return (SMB_ENOACK); } BTI2C_DEBUG(printf("+")); @@ -357,10 +279,10 @@ bti2c_write(struct bti2c_softc* bti2c_sc, u_long data) return( 0 ); } -static int +int bti2c_smb_writeb(device_t dev, u_char slave, char cmd, char byte) { - struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev); + struct bktr_softc *sc = (struct bktr_softc *)device_get_softc(dev); u_long data; data = ((slave & 0xff) << 24) | ((byte & 0xff) << 16) | (u_char)cmd; @@ -372,10 +294,10 @@ bti2c_smb_writeb(device_t dev, u_char slave, char cmd, char byte) * byte1 becomes low byte of word * byte2 becomes high byte of word */ -static int +int bti2c_smb_writew(device_t dev, u_char slave, char cmd, short word) { - struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev); + struct bktr_softc *sc = (struct bktr_softc *)device_get_softc(dev); u_long data; char low, high; @@ -391,10 +313,10 @@ bti2c_smb_writew(device_t dev, u_char slave, char cmd, short word) /* * The Bt878 and Bt879 differed on the treatment of i2c commands */ -static int +int bti2c_smb_readb(device_t dev, u_char slave, char cmd, char *byte) { - struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev); + struct bktr_softc *sc = (struct bktr_softc *)device_get_softc(dev); u_long x; /* clear status bits */ @@ -422,5 +344,3 @@ bti2c_smb_readb(device_t dev, u_char slave, char cmd, char *byte) return (0); } - -DRIVER_MODULE(bti2c, root, bti2c_driver, bti2c_devclass, 0, 0); diff --git a/sys/dev/bktr/bktr_i2c.h b/sys/dev/bktr/bktr_i2c.h index f687e4c..a833351 100644 --- a/sys/dev/bktr/bktr_i2c.h +++ b/sys/dev/bktr/bktr_i2c.h @@ -1,5 +1,5 @@ /*- - * Copyright (c) 1998 Nicolas Souchu + * Copyright (c) 1998, 2001 Nicolas Souchu * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -29,6 +29,19 @@ #ifndef _BT848_I2C_H #define _BT848_I2C_H -extern int bt848_i2c_attach(int, struct bktr_softc *bktr, struct bktr_i2c_softc *); +extern int bt848_i2c_attach(device_t); +extern int bt848_i2c_detach(device_t); + +extern int bti2c_iic_callback(device_t, int, caddr_t *); +extern void bti2c_iic_setsda(device_t, int); +extern void bti2c_iic_setscl(device_t, int); +extern int bti2c_iic_getsda(device_t); +extern int bti2c_iic_getscl(device_t); +extern int bti2c_iic_reset(device_t, u_char, u_char, u_char *); + +extern int bti2c_smb_callback(device_t, int, caddr_t *); +extern int bti2c_smb_writeb(device_t dev, u_char slave, char cmd, char byte); +extern int bti2c_smb_writew(device_t dev, u_char slave, char cmd, short word); +extern int bti2c_smb_readb(device_t dev, u_char slave, char cmd, char *byte); #endif diff --git a/sys/dev/bktr/bktr_os.c b/sys/dev/bktr/bktr_os.c index 0de052f..8cc1368 100644 --- a/sys/dev/bktr/bktr_os.c +++ b/sys/dev/bktr/bktr_os.c @@ -83,7 +83,7 @@ #include <vm/pmap.h> #include <vm/vm_extern.h> -#if (__FreeBSD_version >=400000) || (NSMBUS > 0) +#if (__FreeBSD_version >=400000) #include <sys/bus.h> /* used by smbus and newbus */ #endif @@ -190,13 +190,16 @@ int bktr_debug = 0; #include <dev/bktr/bktr_audio.h> #include <dev/bktr/bktr_core.h> #include <dev/bktr/bktr_os.h> + #if defined(BKTR_USE_FREEBSD_SMBUS) #include <dev/bktr/bktr_i2c.h> + +#include "iicbb_if.h" +#include "smbus_if.h" #endif #endif - /****************************/ /* *** FreeBSD 4.x code *** */ /****************************/ @@ -215,6 +218,22 @@ static device_method_t bktr_methods[] = { DEVMETHOD(device_detach, bktr_detach), DEVMETHOD(device_shutdown, bktr_shutdown), +#if defined(BKTR_USE_FREEBSD_SMBUS) + /* iicbb interface */ + DEVMETHOD(iicbb_callback, bti2c_iic_callback), + DEVMETHOD(iicbb_setsda, bti2c_iic_setsda), + DEVMETHOD(iicbb_setscl, bti2c_iic_setscl), + DEVMETHOD(iicbb_getsda, bti2c_iic_getsda), + DEVMETHOD(iicbb_getscl, bti2c_iic_getscl), + DEVMETHOD(iicbb_reset, bti2c_iic_reset), + + /* smbus interface */ + DEVMETHOD(smbus_callback, bti2c_smb_callback), + DEVMETHOD(smbus_writeb, bti2c_smb_writeb), + DEVMETHOD(smbus_writew, bti2c_smb_writew), + DEVMETHOD(smbus_readb, bti2c_smb_readb), +#endif + { 0, 0 } }; @@ -392,14 +411,11 @@ bktr_attach( device_t dev ) #endif pci_write_config(dev, 0x40, fun, 2); - - /* XXX call bt848_i2c dependent attach() routine */ #if defined(BKTR_USE_FREEBSD_SMBUS) - if (bt848_i2c_attach(unit, bktr, &bktr->i2c_sc)) + if (bt848_i2c_attach(dev)) printf("bktr%d: i2c_attach: can't attach\n", unit); #endif - /* * PCI latency timer. 32 is a good value for 4 bus mastering slots, if * you have more than four, then 16 would probably be a better value. @@ -477,6 +493,11 @@ bktr_detach( device_t dev ) OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED); OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED); +#if defined(BKTR_USE_FREEBSD_SMBUS) + if (bt848_i2c_detach(dev)) + printf("bktr%d: i2c_attach: can't attach\n", unit); +#endif + /* Note: We do not free memory for RISC programs, grab buffer, vbi buffers */ /* The memory is retained by the bktr_mem module so we can unload and */ /* then reload the main bktr driver module */ @@ -965,14 +986,11 @@ bktr_attach( pcici_t tag, int unit ) #endif pci_conf_write(tag, 0x40, fun); - - /* XXX call bt848_i2c dependent attach() routine */ #if defined(BKTR_USE_FREEBSD_SMBUS) - if (bt848_i2c_attach(unit, bktr, &bktr->i2c_sc)) + if (bt848_i2c_attach(dev)) printf("bktr%d: i2c_attach: can't attach\n", unit); #endif - /* * PCI latency timer. 32 is a good value for 4 bus mastering slots, if * you have more than four, then 16 would probably be a better value. diff --git a/sys/dev/bktr/bktr_reg.h b/sys/dev/bktr/bktr_reg.h index ab9bd6e..874a367 100644 --- a/sys/dev/bktr/bktr_reg.h +++ b/sys/dev/bktr/bktr_reg.h @@ -34,17 +34,6 @@ * */ -#ifdef __FreeBSD__ -# if (__FreeBSD_version >= 310000) -# include "smbus.h" -# else -# define NSMBUS 0 /* FreeBSD before 3.1 does not have SMBUS */ -# endif -# if (NSMBUS > 0) -# define BKTR_USE_FREEBSD_SMBUS -# endif -#endif - #ifdef __NetBSD__ #include <machine/bus.h> /* struct device */ #include <sys/device.h> @@ -457,6 +446,9 @@ struct format_params { #if defined(BKTR_USE_FREEBSD_SMBUS) struct bktr_i2c_softc { + int bus_owned; + + device_t iicbb; device_t iicbus; device_t smbus; }; @@ -556,9 +548,9 @@ struct bktr_softc { bus_space_handle_t memh; /* Bus space register access functions */ bus_size_t obmemsz;/* Size of card (bytes) */ #endif - #if (NSMBUS > 0) +#if defined(BKTR_USE_FREEBSD_SMBUS) struct bktr_i2c_softc i2c_sc; /* bt848_i2c device */ - #endif +#endif char bktr_xname[7]; /* device name and unit number */ #endif diff --git a/sys/dev/iicbus/if_ic.c b/sys/dev/iicbus/if_ic.c index 2db1841..28b3d1a 100644 --- a/sys/dev/iicbus/if_ic.c +++ b/sys/dev/iicbus/if_ic.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 1998 Nicolas Souchu + * Copyright (c) 1998, 2001 Nicolas Souchu * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -30,7 +30,6 @@ * I2C bus IP driver */ -#ifdef _KERNEL #include <sys/param.h> #include <sys/systm.h> #include <sys/mbuf.h> @@ -47,7 +46,6 @@ #include <net/if_types.h> #include <net/netisr.h> -#endif #include <sys/mbuf.h> #include <sys/socket.h> #include <net/netisr.h> @@ -65,6 +63,8 @@ #include "iicbus_if.h" +#define PCF_MASTER_ADDRESS 0xaa + #define ICHDRLEN sizeof(u_int) #define ICMTU 1500 /* default mtu */ @@ -130,7 +130,7 @@ icattach(device_t dev) struct ic_softc *sc = (struct ic_softc *)device_get_softc(dev); struct ifnet *ifp = &sc->ic_if; - sc->ic_addr = iicbus_get_addr(dev); + sc->ic_addr = PCF_MASTER_ADDRESS; /* XXX only PCF masters */ ifp->if_softc = sc; ifp->if_name = "ic"; @@ -448,3 +448,5 @@ error: } DRIVER_MODULE(ic, iicbus, ic_driver, ic_devclass, 0, 0); +MODULE_DEPEND(ic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER); +MODULE_VERSION(ic, 1); diff --git a/sys/dev/iicbus/iic.c b/sys/dev/iicbus/iic.c index a8d731e..37d0660 100644 --- a/sys/dev/iicbus/iic.c +++ b/sys/dev/iicbus/iic.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 1998 Nicolas Souchu + * Copyright (c) 1998, 2001 Nicolas Souchu * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -47,11 +47,13 @@ struct iic_softc { - u_char sc_addr; /* address on iicbus */ + u_char sc_addr; /* 7 bit address on iicbus */ int sc_count; /* >0 if device opened */ char sc_buffer[BUFSIZE]; /* output buffer */ char sc_inbuf[BUFSIZE]; /* input buffer */ + + dev_t sc_devnode; }; #define IIC_SOFTC(unit) \ @@ -62,13 +64,17 @@ struct iic_softc { static int iic_probe(device_t); static int iic_attach(device_t); +static int iic_detach(device_t); +static void iic_identify(driver_t *driver, device_t parent); static devclass_t iic_devclass; static device_method_t iic_methods[] = { /* device interface */ + DEVMETHOD(device_identify, iic_identify), DEVMETHOD(device_probe, iic_probe), DEVMETHOD(device_attach, iic_attach), + DEVMETHOD(device_detach, iic_detach), /* iicbus interface */ DEVMETHOD(iicbus_intr, iicbus_generic_intr), @@ -105,30 +111,44 @@ static struct cdevsw iic_cdevsw = { /* flags */ 0, }; -/* - * iicprobe() - */ +static void +iic_identify(driver_t *driver, device_t parent) +{ + BUS_ADD_CHILD(parent, 0, "iic", 0); +} + static int iic_probe(device_t dev) { - struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev); - - sc->sc_addr = iicbus_get_addr(dev); - - /* XXX detect chip with start/stop conditions */ - + device_set_desc(dev, "I2C generic I/O"); return (0); } -/* - * iicattach() - */ static int iic_attach(device_t dev) { - make_dev(&iic_cdevsw, device_get_unit(dev), /* XXX cleanup */ + struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev); + + if (!sc) + return (ENOMEM); + + bzero(sc, sizeof(struct iic_softc)); + + sc->sc_devnode = make_dev(&iic_cdevsw, device_get_unit(dev), UID_ROOT, GID_WHEEL, 0600, "iic%d", device_get_unit(dev)); + + return (0); +} + +static int +iic_detach(device_t dev) +{ + struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev); + + if (sc->sc_devnode) + destroy_dev(sc->sc_devnode); + return (0); } @@ -174,7 +194,7 @@ iicwrite(dev_t dev, struct uio * uio, int ioflag) struct iic_softc *sc = IIC_SOFTC(minor(dev)); int sent, error, count; - if (!sc || !iicdev) + if (!sc || !iicdev || !sc->sc_addr) return (EINVAL); if (sc->sc_count == 0) @@ -202,7 +222,7 @@ iicread(dev_t dev, struct uio * uio, int ioflag) int len, error = 0; int bufsize; - if (!sc || !iicdev) + if (!sc || !iicdev || !sc->sc_addr) return (EINVAL); if (sc->sc_count == 0) @@ -246,6 +266,15 @@ iicioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td) switch (cmd) { case I2CSTART: error = iicbus_start(parent, s->slave, 0); + + /* + * Implicitly set the chip addr to the slave addr passed as + * parameter. Consequently, start/stop shall be called before + * the read or the write of a block. + */ + if (!error) + sc->sc_addr = s->slave; + break; case I2CSTOP: @@ -257,11 +286,11 @@ iicioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td) break; case I2CWRITE: - error = iicbus_write(parent, s->buf, s->count, &count, 0); + error = iicbus_write(parent, s->buf, s->count, &count, 10); break; case I2CREAD: - error = iicbus_read(parent, s->buf, s->count, &count, s->last, 0); + error = iicbus_read(parent, s->buf, s->count, &count, s->last, 10); break; default: @@ -274,3 +303,5 @@ iicioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td) } DRIVER_MODULE(iic, iicbus, iic_driver, iic_devclass, 0, 0); +MODULE_DEPEND(iic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER); +MODULE_VERSION(iic, 1); diff --git a/sys/dev/iicbus/iicbb.c b/sys/dev/iicbus/iicbb.c index 56bc390..436626c 100644 --- a/sys/dev/iicbus/iicbb.c +++ b/sys/dev/iicbus/iicbb.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 1998 Nicolas Souchu + * Copyright (c) 1998, 2001 Nicolas Souchu * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -41,7 +41,6 @@ * From Linux I2C generic interface * (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de> * - * TODO: port Peter's generic bit-banging code <dufault@hda.com> */ #include <sys/param.h> @@ -61,11 +60,12 @@ #include "iicbb_if.h" struct iicbb_softc { - int dummy; + device_t iicbus; }; static int iicbb_probe(device_t); static int iicbb_attach(device_t); +static int iicbb_detach(device_t); static int iicbb_print_child(device_t, device_t); static int iicbb_callback(device_t, int, caddr_t); @@ -79,7 +79,7 @@ static device_method_t iicbb_methods[] = { /* device interface */ DEVMETHOD(device_probe, iicbb_probe), DEVMETHOD(device_attach, iicbb_attach), - DEVMETHOD(device_detach, bus_generic_detach), + DEVMETHOD(device_detach, iicbb_detach), /* bus interface */ DEVMETHOD(bus_print_child, iicbb_print_child), @@ -106,13 +106,36 @@ static devclass_t iicbb_devclass; static int iicbb_probe(device_t dev) { - device_set_desc(dev, "I2C generic bit-banging driver"); + 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); + + 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); + } + return (0); } @@ -140,15 +163,51 @@ iicbb_print_child(device_t bus, device_t dev) return (retval); } -#define I2C_SET(dev,ctrl,data) \ - IICBB_SETLINES(device_get_parent(dev), ctrl, data) +#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))) -#define I2C_GET(dev) (IICBB_GETDATALINE(device_get_parent(dev))) +#define I2C_GETSCL(dev) (IICBB_GETSCL(device_get_parent(dev))) static int i2c_debug = 0; -#define I2C_DEBUG(x) if (i2c_debug) (x) +#define I2C_DEBUG(x) do { \ + if (i2c_debug) (x); \ + } while (0) -static void iicbb_one(device_t dev) +#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) { I2C_SET(dev,0,1); I2C_SET(dev,1,1); @@ -156,7 +215,7 @@ static void iicbb_one(device_t dev) return; } -static void iicbb_zero(device_t dev) +static void iicbb_zero(device_t dev, int timeout) { I2C_SET(dev,0,0); I2C_SET(dev,1,0); @@ -181,17 +240,17 @@ static void iicbb_zero(device_t dev) static int iicbb_ack(device_t dev, int timeout) { int noack; - int k = timeout/10; + int k = 0; I2C_SET(dev,0,1); I2C_SET(dev,1,1); - do { - noack = I2C_GET(dev); + noack = I2C_GETSDA(dev); if (!noack) break; - DELAY(10); /* XXX wait 10us */ - } while (k--); + DELAY(10); + k += 10; + } while (k < timeout); I2C_SET(dev,0,1); I2C_DEBUG(printf("%c ",noack?'-':'+')); @@ -199,18 +258,22 @@ static int iicbb_ack(device_t dev, int timeout) return (noack); } -static void iicbb_sendbyte(device_t dev, u_char data) +static void iicbb_sendbyte(device_t dev, u_char data, int timeout) { int i; - I2C_SET(dev,0,0); - for (i=7; i>=0; i--) - (data&(1<<i)) ? iicbb_one(dev) : iicbb_zero(dev); + for (i=7; i>=0; i--) { + if (data&(1<<i)) { + iicbb_one(dev, timeout); + } else { + iicbb_zero(dev, timeout); + } + } I2C_DEBUG(printf("w%02x",(int)data)); return; } -static u_char iicbb_readbyte(device_t dev, int last) +static u_char iicbb_readbyte(device_t dev, int last, int timeout) { int i; unsigned char data=0; @@ -219,11 +282,15 @@ static u_char iicbb_readbyte(device_t dev, int last) for (i=7; i>=0; i--) { I2C_SET(dev,1,1); - if (I2C_GET(dev)) + if (I2C_GETSDA(dev)) data |= (1<<i); I2C_SET(dev,0,1); } - last ? iicbb_one(dev) : iicbb_zero(dev); + if (last) { + iicbb_one(dev, timeout); + } else { + iicbb_zero(dev, timeout); + } I2C_DEBUG(printf("r%02x%c ",(int)data,last?'-':'+')); return data; } @@ -244,13 +311,12 @@ static int iicbb_start(device_t dev, u_char slave, int timeout) I2C_DEBUG(printf("<")); - I2C_SET(dev,0,1); I2C_SET(dev,1,1); I2C_SET(dev,1,0); I2C_SET(dev,0,0); /* send address */ - iicbb_sendbyte(dev, slave); + iicbb_sendbyte(dev, slave, timeout); /* check for ack */ if (iicbb_ack(dev, timeout)) { @@ -282,7 +348,7 @@ static int iicbb_write(device_t dev, char * buf, int len, int *sent, bytes = 0; while (len) { /* send byte */ - iicbb_sendbyte(dev,(u_char)*buf++); + iicbb_sendbyte(dev,(u_char)*buf++, timeout); /* check for ack */ if (iicbb_ack(dev, timeout)) { @@ -306,7 +372,7 @@ static int iicbb_read(device_t dev, char * buf, int len, int *read, bytes = 0; while (len) { /* XXX should insert delay here */ - *buf++ = (char)iicbb_readbyte(dev, (len == 1) ? last : 0); + *buf++ = (char)iicbb_readbyte(dev, (len == 1) ? last : 0, delay); bytes ++; len --; @@ -316,5 +382,9 @@ static int iicbb_read(device_t dev, char * buf, int len, int *read, return (0); } -DRIVER_MODULE(iicbb, bti2c, iicbb_driver, iicbb_devclass, 0, 0); +DRIVER_MODULE(iicbb, bktr, iicbb_driver, iicbb_devclass, 0, 0); DRIVER_MODULE(iicbb, lpbb, iicbb_driver, iicbb_devclass, 0, 0); +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); diff --git a/sys/dev/iicbus/iicbb_if.m b/sys/dev/iicbus/iicbb_if.m index 94baa41..5642741 100644 --- a/sys/dev/iicbus/iicbb_if.m +++ b/sys/dev/iicbus/iicbb_if.m @@ -40,19 +40,34 @@ METHOD int callback { }; # -# Set I2C bus lines +# Set I2C bus data line # -METHOD void setlines { +METHOD void setsda { device_t dev; - int ctrl; - int data; + int val; }; # -# Get I2C bus lines +# Set I2C bus clock line +# +METHOD void setscl { + device_t dev; + int val; +}; + +# +# Get I2C bus data line +# +# +METHOD int getsda { + device_t dev; +}; + +# +# Get I2C bus clock line # # -METHOD int getdataline { +METHOD int getscl { device_t dev; }; diff --git a/sys/dev/iicbus/iicbus.c b/sys/dev/iicbus/iicbus.c index 1a20543..f20c658 100644 --- a/sys/dev/iicbus/iicbus.c +++ b/sys/dev/iicbus/iicbus.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 1998 Nicolas Souchu + * Copyright (c) 1998, 2001 Nicolas Souchu * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -45,45 +45,6 @@ #define DEVTOIICBUS(dev) ((struct iicbus_device*)device_get_ivars(dev)) -/* - * structure used to attach devices to the I2C bus - */ -struct iicbus_device { - const char *iicd_name; /* device name */ - int iicd_class; /* driver or slave device class */ - const char *iicd_desc; /* device descriptor */ - u_char iicd_addr; /* address of the device */ - int iicd_waitack; /* wait for ack timeout or delay */ - int iicd_alive; /* 1 if device found */ -}; - -/* - * Common I2C addresses - */ -#define I2C_GENERAL_CALL 0x0 -#define PCF_MASTER_ADDRESS 0xaa -#define FIRST_SLAVE_ADDR 0x2 - -#define LAST_SLAVE_ADDR 255 - -#define IICBUS_UNKNOWN_CLASS 0 -#define IICBUS_DEVICE_CLASS 1 -#define IICBUS_DRIVER_CLASS 2 - -/* - * list of known devices - * - * XXX only one smb driver should exist for each I2C interface - */ -static struct iicbus_device iicbus_children[] = { - { "iicsmb", IICBUS_DRIVER_CLASS, "I2C to SMB bridge" }, - { "iic", IICBUS_DRIVER_CLASS, "I2C general purpose I/O" }, -#if 0 - { "ic", IICBUS_DEVICE_CLASS, "network interface", PCF_MASTER_ADDRESS }, -#endif - { NULL, 0 } -}; - static devclass_t iicbus_devclass; /* @@ -91,21 +52,19 @@ static devclass_t iicbus_devclass; */ static int iicbus_probe(device_t); static int iicbus_attach(device_t); -static int iicbus_print_child(device_t, device_t); -static int iicbus_read_ivar(device_t , device_t, int, u_long *); -static int iicbus_write_ivar(device_t , device_t, int, u_long); +static int iicbus_detach(device_t); +static int iicbus_add_child(device_t dev, int order, const char *name, int unit); static device_method_t iicbus_methods[] = { /* device interface */ DEVMETHOD(device_probe, iicbus_probe), DEVMETHOD(device_attach, iicbus_attach), - DEVMETHOD(device_detach, bus_generic_detach), - DEVMETHOD(device_shutdown, bus_generic_shutdown), + DEVMETHOD(device_detach, iicbus_detach), /* bus interface */ - DEVMETHOD(bus_print_child, iicbus_print_child), - DEVMETHOD(bus_read_ivar, iicbus_read_ivar), - DEVMETHOD(bus_write_ivar, iicbus_write_ivar), + DEVMETHOD(bus_add_child, iicbus_add_child), + DEVMETHOD(bus_driver_added, bus_generic_driver_added), + DEVMETHOD(bus_print_child, bus_generic_print_child), { 0, 0 } }; @@ -154,9 +113,6 @@ iic_probe_device(device_t dev, u_char addr) static int iicbus_attach(device_t dev) { - struct iicbus_device *iicdev; - device_t child; - iicbus_reset(dev, IIC_FASTEST, 0, NULL); /* device probing is meaningless since the bus is supposed to be @@ -175,42 +131,34 @@ iicbus_attach(device_t dev) } printf("\n"); #endif + + /* attach any known device */ + device_add_child(dev, NULL, -1); - /* attach known devices */ - for (iicdev = iicbus_children; iicdev->iicd_name; iicdev++) { - switch (iicdev->iicd_class) { - case IICBUS_DEVICE_CLASS: - /* check if the devclass exists */ - if (devclass_find(iicdev->iicd_name)) - iicdev->iicd_alive = 1; - else if (bootverbose) - printf("iicbus: %s devclass not found\n", - iicdev->iicd_name); - break; - - case IICBUS_DRIVER_CLASS: - /* check if the devclass exists */ - if (devclass_find(iicdev->iicd_name)) - iicdev->iicd_alive = 1; - else if (bootverbose) - printf("iicbus: %s devclass not found\n", - iicdev->iicd_name); - break; - - default: - panic("%s: unknown class!", __func__); - } - - if (iicdev->iicd_alive) { - child = device_add_child(dev, iicdev->iicd_name, -1); - device_set_ivars(child, iicdev); - device_set_desc(child, iicdev->iicd_desc); - } - } bus_generic_attach(dev); return (0); } + +static int +iicbus_detach(device_t dev) +{ + iicbus_reset(dev, IIC_FASTEST, 0, NULL); + + bus_generic_detach(dev); + + return (0); +} + +static int +iicbus_add_child(device_t dev, int order, const char *name, int unit) +{ + device_add_child_ordered(dev, order, name, unit); + + bus_generic_attach(dev); + + return (0); +} int iicbus_generic_intr(device_t dev, int event, char *buf) @@ -230,59 +178,7 @@ iicbus_null_repeated_start(device_t dev, u_char addr) return (IIC_ENOTSUPP); } -static int -iicbus_print_child(device_t bus, device_t dev) -{ - struct iicbus_device* iicdev = DEVTOIICBUS(dev); - int retval = 0; - - retval += bus_print_child_header(bus, dev); - - switch (iicdev->iicd_class) { - case IICBUS_DEVICE_CLASS: - retval += printf(" on %s addr 0x%x\n", - device_get_nameunit(bus), iicdev->iicd_addr); - break; - - case IICBUS_DRIVER_CLASS: - retval += bus_print_child_footer(bus, dev); - break; - - default: - panic("%s: unknown class!", __func__); - } - - return (retval); -} - -static int -iicbus_read_ivar(device_t bus, device_t dev, int index, u_long* result) -{ - struct iicbus_device* iicdev = DEVTOIICBUS(dev); - - switch (index) { - case IICBUS_IVAR_ADDR: - *result = (u_long)iicdev->iicd_addr; - break; - - default: - return (ENOENT); - } - - return (0); -} - -static int -iicbus_write_ivar(device_t bus, device_t dev, int index, u_long val) -{ - switch (index) { - default: - return (ENOENT); - } - - return (0); -} - DRIVER_MODULE(iicbus, pcf, iicbus_driver, iicbus_devclass, 0, 0); DRIVER_MODULE(iicbus, iicbb, iicbus_driver, iicbus_devclass, 0, 0); DRIVER_MODULE(iicbus, bti2c, iicbus_driver, iicbus_devclass, 0, 0); +MODULE_VERSION(iicbus, IICBUS_MODVER); diff --git a/sys/dev/iicbus/iiconf.c b/sys/dev/iicbus/iiconf.c index c3156d2..8927792 100644 --- a/sys/dev/iicbus/iiconf.c +++ b/sys/dev/iicbus/iiconf.c @@ -50,22 +50,6 @@ iicbus_intr(device_t bus, int event, char *buf) return; } -/* - * iicbus_alloc_bus() - * - * Allocate a new bus connected to the given parent device - */ -device_t -iicbus_alloc_bus(device_t parent) -{ - device_t child; - - /* add the bus to the parent */ - child = device_add_child(parent, "iicbus", -1); - - return (child); -} - static int iicbus_poll(struct iicbus_softc *sc, int how) { @@ -346,20 +330,3 @@ iicbus_block_read(device_t bus, u_char slave, char *buf, int len, int *read) return (error); } - -/* - * iicbus_get_addr() - * - * Get the I2C 7 bits address of the device - */ -u_char -iicbus_get_addr(device_t dev) -{ - uintptr_t addr; - device_t parent = device_get_parent(dev); - - BUS_READ_IVAR(parent, dev, IICBUS_IVAR_ADDR, &addr); - - return ((u_char)addr); -} - diff --git a/sys/dev/iicbus/iiconf.h b/sys/dev/iicbus/iiconf.h index 03a4a76..a9e6d76 100644 --- a/sys/dev/iicbus/iiconf.h +++ b/sys/dev/iicbus/iiconf.h @@ -1,5 +1,5 @@ /*- - * Copyright (c) 1998 Nicolas Souchu + * Copyright (c) 1998, 2001 Nicolas Souchu * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -127,4 +127,14 @@ extern int iicbus_block_read(device_t, u_char, char *, int, int *); extern u_char iicbus_get_addr(device_t); +#define IICBUS_MODVER 1 +#define IICBUS_MINVER 1 +#define IICBUS_MAXVER 1 +#define IICBUS_PREFVER IICBUS_MODVER + +#define IICBB_MODVER 1 +#define IICBB_MINVER 1 +#define IICBB_MAXVER 1 +#define IICBB_PREFVER IICBB_MODVER + #endif diff --git a/sys/dev/iicbus/iicsmb.c b/sys/dev/iicbus/iicsmb.c index a38845bdb..9bc4d86 100644 --- a/sys/dev/iicbus/iicsmb.c +++ b/sys/dev/iicbus/iicsmb.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 1998 Nicolas Souchu + * Copyright (c) 1998, 2001 Nicolas Souchu * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -79,6 +79,8 @@ struct iicsmb_softc { static int iicsmb_probe(device_t); static int iicsmb_attach(device_t); +static int iicsmb_detach(device_t); +static void iicsmb_identify(driver_t *driver, device_t parent); static void iicsmb_intr(device_t dev, int event, char *buf); static int iicsmb_callback(device_t dev, int index, caddr_t data); @@ -97,11 +99,13 @@ static devclass_t iicsmb_devclass; static device_method_t iicsmb_methods[] = { /* device interface */ + DEVMETHOD(device_identify, iicsmb_identify), DEVMETHOD(device_probe, iicsmb_probe), DEVMETHOD(device_attach, iicsmb_attach), - DEVMETHOD(device_detach, bus_generic_detach), + DEVMETHOD(device_detach, iicsmb_detach), /* bus interface */ + DEVMETHOD(bus_driver_added, bus_generic_driver_added), DEVMETHOD(bus_print_child, bus_generic_print_child), /* iicbus interface */ @@ -129,16 +133,18 @@ static driver_t iicsmb_driver = { sizeof(struct iicsmb_softc), }; +#define IICBUS_TIMEOUT 100 /* us */ + +static void +iicsmb_identify(driver_t *driver, device_t parent) +{ + BUS_ADD_CHILD(parent, 0, "iicsmb", 0); +} + static int iicsmb_probe(device_t dev) { - struct iicsmb_softc *sc = (struct iicsmb_softc *)device_get_softc(dev); - - sc->smbus = smbus_alloc_bus(dev); - - if (!sc->smbus) - return (EINVAL); /* XXX don't know what to return else */ - + device_set_desc(dev, "SMBus over I2C bridge"); return (0); } @@ -147,8 +153,25 @@ iicsmb_attach(device_t dev) { struct iicsmb_softc *sc = (struct iicsmb_softc *)device_get_softc(dev); + bzero(sc, sizeof(*sc)); + + sc->smbus = device_add_child(dev, "smbus", -1); + /* probe and attach the smbus */ - device_probe_and_attach(sc->smbus); + bus_generic_attach(dev); + + return (0); +} + +static int +iicsmb_detach(device_t dev) +{ + struct iicsmb_softc *sc = (struct iicsmb_softc *)device_get_softc(dev); + + bus_generic_detach(dev); + if (sc->smbus) { + device_delete_child(dev, sc->smbus); + } return (0); } @@ -259,11 +282,11 @@ iicsmb_quick(device_t dev, u_char slave, int how) switch (how) { case SMB_QWRITE: - error = iicbus_start(parent, slave & ~LSB, 0); + error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT); break; case SMB_QREAD: - error = iicbus_start(parent, slave | LSB, 0); + error = iicbus_start(parent, slave | LSB, IICBUS_TIMEOUT); break; default: @@ -283,10 +306,10 @@ iicsmb_sendb(device_t dev, u_char slave, char byte) device_t parent = device_get_parent(dev); int error, sent; - error = iicbus_start(parent, slave & ~LSB, 0); + error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT); if (!error) { - error = iicbus_write(parent, &byte, 1, &sent, 0); + error = iicbus_write(parent, &byte, 1, &sent, IICBUS_TIMEOUT); iicbus_stop(parent); } @@ -303,7 +326,7 @@ iicsmb_recvb(device_t dev, u_char slave, char *byte) error = iicbus_start(parent, slave | LSB, 0); if (!error) { - error = iicbus_read(parent, byte, 1, &read, IIC_LAST_READ, 0); + error = iicbus_read(parent, byte, 1, &read, IIC_LAST_READ, IICBUS_TIMEOUT); iicbus_stop(parent); } @@ -320,8 +343,8 @@ iicsmb_writeb(device_t dev, u_char slave, char cmd, char byte) error = iicbus_start(parent, slave & ~LSB, 0); if (!error) { - if (!(error = iicbus_write(parent, &cmd, 1, &sent, 0))) - error = iicbus_write(parent, &byte, 1, &sent, 0); + if (!(error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT))) + error = iicbus_write(parent, &byte, 1, &sent, IICBUS_TIMEOUT); iicbus_stop(parent); } @@ -341,9 +364,9 @@ iicsmb_writew(device_t dev, u_char slave, char cmd, short word) error = iicbus_start(parent, slave & ~LSB, 0); if (!error) { - if (!(error = iicbus_write(parent, &cmd, 1, &sent, 0))) - if (!(error = iicbus_write(parent, &low, 1, &sent, 0))) - error = iicbus_write(parent, &high, 1, &sent, 0); + if (!(error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT))) + if (!(error = iicbus_write(parent, &low, 1, &sent, IICBUS_TIMEOUT))) + error = iicbus_write(parent, &high, 1, &sent, IICBUS_TIMEOUT); iicbus_stop(parent); } @@ -357,16 +380,16 @@ iicsmb_readb(device_t dev, u_char slave, char cmd, char *byte) device_t parent = device_get_parent(dev); int error, sent, read; - if ((error = iicbus_start(parent, slave & ~LSB, 0))) + if ((error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT))) return (error); - if ((error = iicbus_write(parent, &cmd, 1, &sent, 0))) + if ((error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT))) goto error; - if ((error = iicbus_repeated_start(parent, slave | LSB, 0))) + if ((error = iicbus_repeated_start(parent, slave | LSB, IICBUS_TIMEOUT))) goto error; - if ((error = iicbus_read(parent, byte, 1, &read, IIC_LAST_READ, 0))) + if ((error = iicbus_read(parent, byte, 1, &read, IIC_LAST_READ, IICBUS_TIMEOUT))) goto error; error: @@ -384,16 +407,16 @@ iicsmb_readw(device_t dev, u_char slave, char cmd, short *word) int error, sent, read; char buf[2]; - if ((error = iicbus_start(parent, slave & ~LSB, 0))) + if ((error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT))) return (error); - if ((error = iicbus_write(parent, &cmd, 1, &sent, 0))) + if ((error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT))) goto error; - if ((error = iicbus_repeated_start(parent, slave | LSB, 0))) + if ((error = iicbus_repeated_start(parent, slave | LSB, IICBUS_TIMEOUT))) goto error; - if ((error = iicbus_read(parent, buf, 2, &read, IIC_LAST_READ, 0))) + if ((error = iicbus_read(parent, buf, 2, &read, IIC_LAST_READ, IICBUS_TIMEOUT))) goto error; /* first, receive low, then high byte */ @@ -411,23 +434,23 @@ iicsmb_pcall(device_t dev, u_char slave, char cmd, short sdata, short *rdata) int error, sent, read; char buf[2]; - if ((error = iicbus_start(parent, slave & ~LSB, 0))) + if ((error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT))) return (error); - if ((error = iicbus_write(parent, &cmd, 1, &sent, 0))) + if ((error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT))) goto error; /* first, send low, then high byte */ buf[0] = (char)(sdata & 0xff); buf[1] = (char)((sdata & 0xff00) >> 8); - if ((error = iicbus_write(parent, buf, 2, &sent, 0))) + if ((error = iicbus_write(parent, buf, 2, &sent, IICBUS_TIMEOUT))) goto error; - if ((error = iicbus_repeated_start(parent, slave | LSB, 0))) + if ((error = iicbus_repeated_start(parent, slave | LSB, IICBUS_TIMEOUT))) goto error; - if ((error = iicbus_read(parent, buf, 2, &read, IIC_LAST_READ, 0))) + if ((error = iicbus_read(parent, buf, 2, &read, IIC_LAST_READ, IICBUS_TIMEOUT))) goto error; /* first, receive low, then high byte */ @@ -444,13 +467,13 @@ iicsmb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf) device_t parent = device_get_parent(dev); int error, sent; - if ((error = iicbus_start(parent, slave & ~LSB, 0))) + if ((error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT))) goto error; - if ((error = iicbus_write(parent, &cmd, 1, &sent, 0))) + if ((error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT))) goto error; - if ((error = iicbus_write(parent, buf, (int)count, &sent, 0))) + if ((error = iicbus_write(parent, buf, (int)count, &sent, IICBUS_TIMEOUT))) goto error; if ((error = iicbus_stop(parent))) @@ -466,17 +489,17 @@ iicsmb_bread(device_t dev, u_char slave, char cmd, u_char count, char *buf) device_t parent = device_get_parent(dev); int error, sent, read; - if ((error = iicbus_start(parent, slave & ~LSB, 0))) + if ((error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT))) return (error); - if ((error = iicbus_write(parent, &cmd, 1, &sent, 0))) + if ((error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT))) goto error; - if ((error = iicbus_repeated_start(parent, slave | LSB, 0))) + if ((error = iicbus_repeated_start(parent, slave | LSB, IICBUS_TIMEOUT))) goto error; if ((error = iicbus_read(parent, buf, (int)count, &read, - IIC_LAST_READ, 0))) + IIC_LAST_READ, IICBUS_TIMEOUT))) goto error; error: @@ -485,3 +508,6 @@ error: } DRIVER_MODULE(iicsmb, iicbus, iicsmb_driver, iicsmb_devclass, 0, 0); +MODULE_DEPEND(iicsmb, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER); +MODULE_DEPEND(iicsmb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER); +MODULE_VERSION(iicsmb, 1); diff --git a/sys/dev/ppbus/lpbb.c b/sys/dev/ppbus/lpbb.c index 5fd99df..0284f07 100644 --- a/sys/dev/ppbus/lpbb.c +++ b/sys/dev/ppbus/lpbb.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 1998 Nicolas Souchu, Marc Bouget + * Copyright (c) 1998, 2001 Nicolas Souchu, Marc Bouget * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -75,19 +75,12 @@ lpbb_probe(device_t dev) static int lpbb_attach(device_t dev) { - device_t bitbang, iicbus; + device_t bitbang; /* add generic bit-banging code */ bitbang = device_add_child(dev, "iicbb", -1); - - /* add the iicbus to the tree */ - iicbus = iicbus_alloc_bus(bitbang); - device_probe_and_attach(bitbang); - /* XXX should be in iicbb_attach! */ - device_probe_and_attach(iicbus); - return (0); } @@ -124,24 +117,30 @@ lpbb_callback(device_t dev, int index, caddr_t *data) #define ALIM 0x20 #define I2CKEY 0x50 -static int getSDA(device_t ppbus) +static int lpbb_getscl(device_t dev) +{ + return ((ppb_rstr(device_get_parent(dev)) & SCL_in) == SCL_in); +} + +static int lpbb_getsda(device_t dev) { - if((ppb_rstr(ppbus)&SDA_in)==SDA_in) - return 1; - else - return 0; + return ((ppb_rstr(device_get_parent(dev)) & SDA_in) == SDA_in); } -static void setSDA(device_t ppbus, char val) +static void lpbb_setsda(device_t dev, char val) { + device_t ppbus = device_get_parent(dev); + if(val==0) ppb_wdtr(ppbus, (u_char)SDA_out); else ppb_wdtr(ppbus, (u_char)~SDA_out); } -static void setSCL(device_t ppbus, unsigned char val) +static void lpbb_setscl(device_t dev, unsigned char val) { + device_t ppbus = device_get_parent(dev); + if(val==0) ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus)&~SCL_out)); else @@ -158,8 +157,8 @@ static int lpbb_detect(device_t dev) } /* reset bus */ - setSDA(ppbus, 1); - setSCL(ppbus, 1); + lpbb_setsda(dev, 1); + lpbb_setscl(dev, 1); if ((ppb_rstr(ppbus) & I2CKEY) || ((ppb_rstr(ppbus) & ALIM) != ALIM)) { @@ -178,28 +177,18 @@ lpbb_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr) { device_t ppbus = device_get_parent(dev); - /* reset bus */ - setSDA(ppbus, 1); - setSCL(ppbus, 1); - - return (IIC_ENOADDR); -} - -static void -lpbb_setlines(device_t dev, int ctrl, int data) -{ - device_t ppbus = device_get_parent(dev); + if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) { + device_printf(dev, "can't allocate ppbus\n"); + return (0); + } - setSCL(ppbus, ctrl); - setSDA(ppbus, data); -} + /* reset bus */ + lpbb_setsda(dev, 1); + lpbb_setscl(dev, 1); -static int -lpbb_getdataline(device_t dev) -{ - device_t ppbus = device_get_parent(dev); + ppb_release_bus(ppbus, dev); - return (getSDA(ppbus)); + return (IIC_ENOADDR); } static devclass_t lpbb_devclass; @@ -215,8 +204,10 @@ static device_method_t lpbb_methods[] = { /* iicbb interface */ DEVMETHOD(iicbb_callback, lpbb_callback), - DEVMETHOD(iicbb_setlines, lpbb_setlines), - DEVMETHOD(iicbb_getdataline, lpbb_getdataline), + DEVMETHOD(iicbb_setsda, lpbb_setsda), + DEVMETHOD(iicbb_setscl, lpbb_setscl), + DEVMETHOD(iicbb_getsda, lpbb_getsda), + DEVMETHOD(iicbb_getscl, lpbb_getscl), DEVMETHOD(iicbb_reset, lpbb_reset), { 0, 0 } @@ -229,3 +220,5 @@ static driver_t lpbb_driver = { }; DRIVER_MODULE(lpbb, ppbus, lpbb_driver, lpbb_devclass, 0, 0); +MODULE_DEPEND(lpbb, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER); +MODULE_VERSION(lpbb, 1); diff --git a/sys/dev/smbus/smb.c b/sys/dev/smbus/smb.c index beb3c4a..e07d4b4 100644 --- a/sys/dev/smbus/smb.c +++ b/sys/dev/smbus/smb.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 1998 Nicolas Souchu + * Copyright (c) 1998, 2001 Nicolas Souchu * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -46,13 +46,8 @@ struct smb_softc { - int sc_addr; /* address on smbus */ int sc_count; /* >0 if device opened */ - - char *sc_cp; /* output buffer pointer */ - - char sc_buffer[BUFSIZE]; /* output buffer */ - char sc_inbuf[BUFSIZE]; /* input buffer */ + dev_t sc_devnode; }; #define IIC_SOFTC(unit) \ @@ -63,13 +58,17 @@ struct smb_softc { static int smb_probe(device_t); static int smb_attach(device_t); +static int smb_detach(device_t); +static void smb_identify(driver_t *driver, device_t parent); static devclass_t smb_devclass; static device_method_t smb_methods[] = { /* device interface */ + DEVMETHOD(device_identify, smb_identify), DEVMETHOD(device_probe, smb_probe), DEVMETHOD(device_attach, smb_attach), + DEVMETHOD(device_detach, smb_detach), /* smbus interface */ DEVMETHOD(smbus_intr, smbus_generic_intr), @@ -106,30 +105,45 @@ static struct cdevsw smb_cdevsw = { /* flags */ 0, }; -/* - * smbprobe() - */ +static void +smb_identify(driver_t *driver, device_t parent) +{ + BUS_ADD_CHILD(parent, 0, "smb", 0); +} + static int smb_probe(device_t dev) { - struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev); - - sc->sc_addr = smbus_get_addr(dev); - - /* XXX detect chip with start/stop conditions */ + device_set_desc(dev, "SMBus generic I/O"); return (0); } -/* - * smbattach() - */ static int smb_attach(device_t dev) { - make_dev(&smb_cdevsw, device_get_unit(dev), /* XXX cleanup */ + struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev); + + if (!sc) + return (ENOMEM); + + bzero(sc, sizeof(struct smb_softc *)); + + sc->sc_devnode = make_dev(&smb_cdevsw, device_get_unit(dev), UID_ROOT, GID_WHEEL, 0600, "smb%d", device_get_unit(dev)); + + return (0); +} + +static int +smb_detach(device_t dev) +{ + struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev); + + if (sc->sc_devnode) + destroy_dev(sc->sc_devnode); + return (0); } @@ -267,3 +281,5 @@ smbioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td) } DRIVER_MODULE(smb, smbus, smb_driver, smb_devclass, 0, 0); +MODULE_DEPEND(smb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER); +MODULE_VERSION(smb, 1); diff --git a/sys/dev/smbus/smbconf.c b/sys/dev/smbus/smbconf.c index b91f3d3..9e9a2cb 100644 --- a/sys/dev/smbus/smbconf.c +++ b/sys/dev/smbus/smbconf.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 1998 Nicolas Souchu + * Copyright (c) 1998, 2001 Nicolas Souchu * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -78,22 +78,6 @@ smbus_error(int smb_error) return (error); } -/* - * smbus_alloc_bus() - * - * Allocate a new bus connected to the given parent device - */ -device_t -smbus_alloc_bus(device_t parent) -{ - device_t child; - - /* add the bus to the parent */ - child = device_add_child(parent, "smbus", -1); - - return (child); -} - static int smbus_poll(struct smbus_softc *sc, int how) { @@ -190,19 +174,3 @@ smbus_release_bus(device_t bus, device_t dev) return (0); } - -/* - * smbus_get_addr() - * - * Get the I2C 7 bits address of the device - */ -u_char -smbus_get_addr(device_t dev) -{ - uintptr_t addr; - device_t parent = device_get_parent(dev); - - BUS_READ_IVAR(parent, dev, SMBUS_IVAR_ADDR, &addr); - - return ((u_char)addr); -} diff --git a/sys/dev/smbus/smbconf.h b/sys/dev/smbus/smbconf.h index 40289f7..64b0a33 100644 --- a/sys/dev/smbus/smbconf.h +++ b/sys/dev/smbus/smbconf.h @@ -101,4 +101,9 @@ extern u_char smbus_get_addr(device_t); #define smbus_bread(bus,slave,cmd,count,buf) \ (SMBUS_BREAD(device_get_parent(bus), slave, cmd, count, buf)) +#define SMBUS_MODVER 1 +#define SMBUS_MINVER 1 +#define SMBUS_MAXVER 1 +#define SMBUS_PREFVER SMBUS_MODVER + #endif diff --git a/sys/dev/smbus/smbus.c b/sys/dev/smbus/smbus.c index 4650608..99b7ca3 100644 --- a/sys/dev/smbus/smbus.c +++ b/sys/dev/smbus/smbus.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 1998 Nicolas Souchu + * Copyright (c) 1998, 2001 Nicolas Souchu * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -41,22 +41,6 @@ #define DEVTOSMBUS(dev) ((struct smbus_device*)device_get_ivars(dev)) -/* - * structure used to attach devices to the I2C bus - */ -struct smbus_device { - const char *smbd_name; /* device name */ - const char *smbd_desc; /* device descriptor */ -}; - -/* - * list of known devices - */ -struct smbus_device smbus_children[] = { - { "smb", "SMBus general purpose I/O" }, - { NULL, 0 } -}; - static devclass_t smbus_devclass; /* @@ -64,22 +48,17 @@ static devclass_t smbus_devclass; */ static int smbus_probe(device_t); static int smbus_attach(device_t); - -#if 0 -static int smbus_read_ivar(device_t , device_t, int, u_long *); -#endif +static int smbus_add_child(device_t dev, int order, const char *name, int unit); static device_method_t smbus_methods[] = { /* device interface */ DEVMETHOD(device_probe, smbus_probe), DEVMETHOD(device_attach, smbus_attach), DEVMETHOD(device_detach, bus_generic_detach), - DEVMETHOD(device_shutdown, bus_generic_shutdown), /* bus interface */ + DEVMETHOD(bus_add_child, smbus_add_child), DEVMETHOD(bus_print_child, bus_generic_print_child), - DEVMETHOD(bus_read_ivar, bus_generic_read_ivar), - DEVMETHOD(bus_write_ivar, bus_generic_write_ivar), { 0, 0 } }; @@ -106,48 +85,33 @@ smbus_probe(device_t dev) static int smbus_attach(device_t dev) { - struct smbus_device *smbdev; - - /* add known devices */ - for (smbdev = smbus_children; smbdev->smbd_name; smbdev++) { - device_t child; - - if (devclass_find(smbdev->smbd_name)) { - child = device_add_child(dev, smbdev->smbd_name, -1); - device_set_ivars(child, smbdev); - device_set_desc(child, smbdev->smbd_desc); - } else if (bootverbose) - printf("smbus: %s devclass not found\n", - smbdev->smbd_name); - } + device_add_child(dev, NULL, -1); bus_generic_attach(dev); return (0); } -void -smbus_generic_intr(device_t dev, u_char devaddr, char low, char high) +static int +smbus_add_child(device_t dev, int order, const char *name, int unit) { - return; + device_add_child_ordered(dev, order, name, unit); + + bus_generic_attach(dev); + + return (0); } -#if 0 -static int -smbus_read_ivar(device_t bus, device_t dev, int index, u_long* result) +void +smbus_generic_intr(device_t dev, u_char devaddr, char low, char high) { - struct smbus_device* smbdev = DEVTOSMBUS(dev); - - switch (index) { - default: - break; - } - return (ENOENT); + return; } -#endif DRIVER_MODULE(smbus, iicsmb, smbus_driver, smbus_devclass, 0, 0); -DRIVER_MODULE(smbus, bti2c, smbus_driver, smbus_devclass, 0, 0); +DRIVER_MODULE(smbus, bktr, smbus_driver, smbus_devclass, 0, 0); DRIVER_MODULE(smbus, intsmb, smbus_driver, smbus_devclass, 0, 0); -DRIVER_MODULE(smbus, alsmb, smbus_driver, smbus_devclass, 0, 0); +DRIVER_MODULE(smbus, alpm, smbus_driver, smbus_devclass, 0, 0); DRIVER_MODULE(smbus, ichsmb, smbus_driver, smbus_devclass, 0, 0); -DRIVER_MODULE(smbus, amdsmb, smbus_driver, smbus_devclass, 0, 0); +DRIVER_MODULE(smbus, amdpm, smbus_driver, smbus_devclass, 0, 0); +DRIVER_MODULE(smbus, viapropm, smbus_driver, smbus_devclass, 0, 0); +MODULE_VERSION(smbus, SMBUS_MODVER); |