2 * Copyright (c) 1998, 2001 Nicolas Souchu
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * I2C support for the bti2c chipset.
33 * From brooktree848.c <fsmp@freefall.org>
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/systm.h>
42 #include <sys/module.h>
46 #if __FreeBSD_version < 500014
47 #include <sys/select.h>
49 #include <sys/selinfo.h>
52 #if (__FreeBSD_version < 500000)
53 #include <pci/pcivar.h>
54 #include <pci/pcireg.h>
56 #include <dev/pci/pcivar.h>
57 #include <dev/pci/pcireg.h>
60 #include <machine/bus.h>
63 #include <dev/bktr/ioctl_meteor.h>
64 #include <dev/bktr/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
65 #include <dev/bktr/bktr_reg.h>
66 #include <dev/bktr/bktr_i2c.h>
68 #include <dev/smbus/smbconf.h>
69 #include <dev/iicbus/iiconf.h>
71 /* Compilation is void if BKTR_USE_FREEBSD_SMBUS is not
72 * defined. This allows bktr owners to have smbus active for there
73 * motherboard and still use their bktr without smbus.
75 #if defined(BKTR_USE_FREEBSD_SMBUS)
77 #define BTI2C_DEBUG(x) if (bti2c_debug) (x)
78 static int bti2c_debug = 0;
81 * Call this to pass the address of the bktr device to the
82 * bti2c_i2c layer and initialize all the I2C bus architecture
84 int bt848_i2c_attach(device_t dev)
86 struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev);
87 struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc;
89 sc->smbus = device_add_child(dev, "smbus", -1);
90 sc->iicbb = device_add_child(dev, "iicbb", -1);
92 if (!sc->iicbb || !sc->smbus)
95 bus_generic_attach(dev);
100 int bt848_i2c_detach(device_t dev)
102 struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev);
103 struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc;
106 if ((error = bus_generic_detach(dev)))
109 if (sc->iicbb && (error = device_delete_child(dev, sc->iicbb)))
112 if (sc->smbus && (error = device_delete_child(dev, sc->smbus)))
119 int bti2c_smb_callback(device_t dev, int index, void *data)
121 struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev);
122 struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc;
125 /* test each time if we already have/haven't the iicbus
129 case SMB_REQUEST_BUS:
131 if (!sc->bus_owned) {
137 case SMB_RELEASE_BUS:
152 int bti2c_iic_callback(device_t dev, int index, caddr_t *data)
154 struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev);
155 struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc;
158 /* test each time if we already have/haven't the smbus
162 case IIC_REQUEST_BUS:
164 if (!sc->bus_owned) {
170 case IIC_RELEASE_BUS:
185 int bti2c_iic_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr)
188 *oldaddr = 0; /* XXX */
190 return (IIC_ENOADDR);
193 void bti2c_iic_setsda(device_t dev, int val)
195 struct bktr_softc *sc = (struct bktr_softc *)device_get_softc(dev);
198 clock = INL(sc, BKTR_I2C_DATA_CTL) & 0x2;
201 OUTL(sc, BKTR_I2C_DATA_CTL, clock | 1);
203 OUTL(sc, BKTR_I2C_DATA_CTL, clock);
208 void bti2c_iic_setscl(device_t dev, int val)
210 struct bktr_softc *sc = (struct bktr_softc *)device_get_softc(dev);
213 data = INL(sc, BKTR_I2C_DATA_CTL) & 0x1;
216 OUTL(sc, BKTR_I2C_DATA_CTL, 0x2 | data);
218 OUTL(sc, BKTR_I2C_DATA_CTL, data);
224 bti2c_iic_getsda(device_t dev)
226 struct bktr_softc *sc = (struct bktr_softc *)device_get_softc(dev);
228 return (INL(sc,BKTR_I2C_DATA_CTL) & 0x1);
232 bti2c_iic_getscl(device_t dev)
238 bti2c_write(struct bktr_softc *sc, u_long data)
242 /* clear status bits */
243 OUTL(sc, BKTR_INT_STAT, (BT848_INT_RACK | BT848_INT_I2CDONE));
245 BTI2C_DEBUG(printf("w%lx", data));
247 /* write the address and data */
248 OUTL(sc, BKTR_I2C_DATA_CTL, data);
250 /* wait for completion */
251 for ( x = 0x7fffffff; x; --x ) { /* safety valve */
252 if ( INL(sc, BKTR_INT_STAT) & BT848_INT_I2CDONE )
257 if ( !x || !( INL(sc, BKTR_INT_STAT) & BT848_INT_RACK) ) {
258 BTI2C_DEBUG(printf("%c%c", (!x)?'+':'-',
259 (!( INL(sc, BKTR_INT_STAT) & BT848_INT_RACK))?'+':'-'));
262 BTI2C_DEBUG(printf("+"));
269 bti2c_smb_writeb(device_t dev, u_char slave, char cmd, char byte)
271 struct bktr_softc *sc = (struct bktr_softc *)device_get_softc(dev);
274 data = ((slave & 0xff) << 24) | ((byte & 0xff) << 16) | (u_char)cmd;
276 return (bti2c_write(sc, data));
280 * byte1 becomes low byte of word
281 * byte2 becomes high byte of word
284 bti2c_smb_writew(device_t dev, u_char slave, char cmd, short word)
286 struct bktr_softc *sc = (struct bktr_softc *)device_get_softc(dev);
290 low = (char)(word & 0xff);
291 high = (char)((word & 0xff00) >> 8);
293 data = ((slave & 0xff) << 24) | ((low & 0xff) << 16) |
294 ((high & 0xff) << 8) | BT848_DATA_CTL_I2CW3B | (u_char)cmd;
296 return (bti2c_write(sc, data));
300 * The Bt878 and Bt879 differed on the treatment of i2c commands
303 bti2c_smb_readb(device_t dev, u_char slave, char cmd, char *byte)
305 struct bktr_softc *sc = (struct bktr_softc *)device_get_softc(dev);
308 /* clear status bits */
309 OUTL(sc,BKTR_INT_STAT, (BT848_INT_RACK | BT848_INT_I2CDONE));
311 OUTL(sc,BKTR_I2C_DATA_CTL, ((slave & 0xff) << 24) | (u_char)cmd);;
313 BTI2C_DEBUG(printf("r%lx/", (u_long)(((slave & 0xff) << 24) | (u_char)cmd)));
315 /* wait for completion */
316 for ( x = 0x7fffffff; x; --x ) { /* safety valve */
317 if ( INL(sc,BKTR_INT_STAT) & BT848_INT_I2CDONE )
322 if ( !x || !(INL(sc,BKTR_INT_STAT) & BT848_INT_RACK) ) {
323 BTI2C_DEBUG(printf("r%c%c", (!x)?'+':'-',
324 (!( INL(sc,BKTR_INT_STAT) & BT848_INT_RACK))?'+':'-'));
328 *byte = (char)((INL(sc,BKTR_I2C_DATA_CTL) >> 8) & 0xff);
329 BTI2C_DEBUG(printf("r%x+", *byte));
334 DRIVER_MODULE(smbus, bktr, smbus_driver, smbus_devclass, 0, 0);
336 #endif /* defined(BKTR_USE_FREEBSD_SMBUS) */