2 * Copyright (c) 1998 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
26 * $FreeBSD: src/sys/dev/bktr/bktr_i2c.c,v 1.13.2.3 2000/10/26 16:38:46 roger Exp $
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/systm.h>
41 #include <sys/module.h>
44 #include <sys/select.h>
46 #if (__FreeBSD_version < 500000)
47 #include <machine/clock.h> /* for DELAY */
50 #if (__FreeBSD_version >=300000)
51 #include <machine/bus_memio.h> /* for bus space */
52 #include <machine/bus.h>
56 #include <pci/pcivar.h>
57 #include <pci/pcireg.h>
58 #include <machine/ioctl_meteor.h>
59 #include <machine/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
60 #include <dev/bktr/bktr_reg.h>
62 #include <dev/bktr/bktr_i2c.h>
64 #include <dev/iicbus/iiconf.h>
65 #include <dev/iicbus/iicbus.h>
67 #include <dev/smbus/smbconf.h>
76 #define BTI2C_DEBUG(x) if (bti2c_debug) (x)
77 static int bti2c_debug = 0;
81 bus_space_tag_t memt; /* Bus space register access */
82 bus_space_handle_t memh; /* Bus space register access */
84 int iic_owned; /* 1 if we own the iicbus */
85 int smb_owned; /* 1 if we own the smbbus */
93 bus_space_handle_t memh;
95 struct bt_data btdata[NBKTR];
97 static int bti2c_probe(device_t);
98 static int bti2c_attach(device_t);
100 static int bti2c_iic_callback(device_t, int, caddr_t *);
101 static void bti2c_iic_setlines(device_t, int, int);
102 static int bti2c_iic_getdataline(device_t);
103 static int bti2c_iic_reset(device_t, u_char, u_char, u_char *);
105 static int bti2c_smb_callback(device_t, int, caddr_t *);
106 static int bti2c_smb_writeb(device_t dev, u_char slave, char cmd, char byte);
107 static int bti2c_smb_writew(device_t dev, u_char slave, char cmd, short word);
108 static int bti2c_smb_readb(device_t dev, u_char slave, char cmd, char *byte);
110 static devclass_t bti2c_devclass;
112 static device_method_t bti2c_methods[] = {
113 /* device interface */
114 DEVMETHOD(device_probe, bti2c_probe),
115 DEVMETHOD(device_attach, bti2c_attach),
118 DEVMETHOD(bus_print_child, bus_generic_print_child),
120 /* iicbb interface */
121 DEVMETHOD(iicbb_callback, bti2c_iic_callback),
122 DEVMETHOD(iicbb_setlines, bti2c_iic_setlines),
123 DEVMETHOD(iicbb_getdataline, bti2c_iic_getdataline),
124 DEVMETHOD(iicbb_reset, bti2c_iic_reset),
126 /* smbus interface */
127 DEVMETHOD(smbus_callback, bti2c_smb_callback),
128 DEVMETHOD(smbus_writeb, bti2c_smb_writeb),
129 DEVMETHOD(smbus_writew, bti2c_smb_writew),
130 DEVMETHOD(smbus_readb, bti2c_smb_readb),
135 #if (__FreeBSD_version < 400000)
136 /* FreeBSD 3.x needs DRIVER_TYPE_MISC */
137 static driver_t bti2c_driver = {
141 sizeof(struct bti2c_softc),
145 #if (__FreeBSD_version >=400000)
146 static driver_t bti2c_driver = {
149 sizeof(struct bti2c_softc),
154 * Call this to pass the address of the bktr device to the
155 * bti2c_i2c layer and initialize all the I2C bus architecture
158 bt848_i2c_attach(int unit, struct bktr_softc * bktr, struct bktr_i2c_softc *i2c_sc)
163 btdata[unit].memh = bktr->memh;
164 btdata[unit].memt = bktr->memt;
166 /* XXX add the I2C interface to the root_bus until pcibus is ready */
167 #if (__FreeBSD_version < 400000)
168 interface = device_add_child(root_bus, "bti2c", unit, NULL);
170 interface = device_add_child(root_bus, "bti2c", unit);
173 /* add bit-banging generic code onto bti2c interface */
174 #if (__FreeBSD_version < 400000)
175 bitbang = device_add_child(interface, "iicbb", -1, NULL);
177 bitbang = device_add_child(interface, "iicbb", -1);
180 /* probe and attach the interface, we need it NOW
181 * bit-banging code is also probed and attached */
182 device_probe_and_attach(interface);
183 device_probe_and_attach(bitbang);
185 /* smb and i2c interfaces are available for the bt848 chip
186 * connect bit-banging generic code to an iicbus */
187 if ((i2c_sc->iicbus = iicbus_alloc_bus(bitbang)))
188 device_probe_and_attach(i2c_sc->iicbus);
190 /* hardware i2c is actually smb over the bti2c interface */
191 if ((i2c_sc->smbus = smbus_alloc_bus(interface)))
192 device_probe_and_attach(i2c_sc->smbus);
198 * Not a real probe, we know the device exists since the device has
199 * been added after the successfull pci probe.
202 bti2c_probe(device_t dev)
204 device_set_desc(dev, "bt848 Hard/Soft I2C controller");
210 bti2c_attach(device_t dev)
212 struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev);
214 /* XXX should use ivars with pcibus or pcibus methods to access
216 sc->memh = btdata[device_get_unit(dev)].memh;
217 sc->memt = btdata[device_get_unit(dev)].memt;
223 bti2c_smb_callback(device_t dev, int index, caddr_t *data)
225 struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev);
229 /* test each time if we already have/haven't the iicbus
233 case SMB_REQUEST_BUS:
234 if (!sc->iic_owned) {
235 /* request the iicbus */
237 error = iicbus_request_bus(sc->iicbus, dev, how);
243 case SMB_RELEASE_BUS:
245 /* release the iicbus */
246 error = iicbus_release_bus(sc->iicbus, dev);
260 bti2c_iic_callback(device_t dev, int index, caddr_t *data)
262 struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev);
266 /* test each time if we already have/haven't the smbus
270 case IIC_REQUEST_BUS:
271 if (!sc->smb_owned) {
272 /* request the smbus */
274 error = smbus_request_bus(sc->smbus, dev, how);
280 case IIC_RELEASE_BUS:
282 /* release the smbus */
283 error = smbus_release_bus(sc->smbus, dev);
297 bti2c_iic_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr)
300 *oldaddr = 0; /* XXX */
302 return (IIC_ENOADDR);
306 bti2c_iic_setlines(device_t dev, int ctrl, int data)
308 struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev);
310 OUTL(sc, BKTR_I2C_DATA_CTL, (ctrl << 1) | data);;
317 bti2c_iic_getdataline(device_t dev)
319 struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev);
321 return ( INL(sc,BKTR_I2C_DATA_CTL) & 0x1);
325 bti2c_write(struct bti2c_softc* bti2c_sc, u_long data)
329 /* clear status bits */
330 OUTL(bti2c_sc, BKTR_INT_STAT, (BT848_INT_RACK | BT848_INT_I2CDONE));
332 BTI2C_DEBUG(printf("w%lx", data));
334 /* write the address and data */
335 OUTL(bti2c_sc, BKTR_I2C_DATA_CTL, data);
337 /* wait for completion */
338 for ( x = 0x7fffffff; x; --x ) { /* safety valve */
339 if ( INL(bti2c_sc, BKTR_INT_STAT) & BT848_INT_I2CDONE )
344 if ( !x || !( INL(bti2c_sc, BKTR_INT_STAT) & BT848_INT_RACK) ) {
345 BTI2C_DEBUG(printf("%c%c", (!x)?'+':'-',
346 (!( INL(bti2c_sc, BKTR_INT_STAT) & BT848_INT_RACK))?'+':'-'));
349 BTI2C_DEBUG(printf("+"));
356 bti2c_smb_writeb(device_t dev, u_char slave, char cmd, char byte)
358 struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev);
361 data = ((slave & 0xff) << 24) | ((byte & 0xff) << 16) | (u_char)cmd;
363 return (bti2c_write(sc, data));
367 * byte1 becomes low byte of word
368 * byte2 becomes high byte of word
371 bti2c_smb_writew(device_t dev, u_char slave, char cmd, short word)
373 struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev);
377 low = (char)(word & 0xff);
378 high = (char)((word & 0xff00) >> 8);
380 data = ((slave & 0xff) << 24) | ((low & 0xff) << 16) |
381 ((high & 0xff) << 8) | BT848_DATA_CTL_I2CW3B | (u_char)cmd;
383 return (bti2c_write(sc, data));
387 * The Bt878 and Bt879 differed on the treatment of i2c commands
390 bti2c_smb_readb(device_t dev, u_char slave, char cmd, char *byte)
392 struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev);
395 /* clear status bits */
396 OUTL(sc,BKTR_INT_STAT, (BT848_INT_RACK | BT848_INT_I2CDONE));
398 OUTL(sc,BKTR_I2C_DATA_CTL, ((slave & 0xff) << 24) | (u_char)cmd);;
400 BTI2C_DEBUG(printf("r%lx/", (u_long)(((slave & 0xff) << 24) | (u_char)cmd)));
402 /* wait for completion */
403 for ( x = 0x7fffffff; x; --x ) { /* safety valve */
404 if ( INL(sc,BKTR_INT_STAT) & BT848_INT_I2CDONE )
409 if ( !x || !(INL(sc,BKTR_INT_STAT) & BT848_INT_RACK) ) {
410 BTI2C_DEBUG(printf("r%c%c", (!x)?'+':'-',
411 (!( INL(sc,BKTR_INT_STAT) & BT848_INT_RACK))?'+':'-'));
415 *byte = (char)((INL(sc,BKTR_I2C_DATA_CTL) >> 8) & 0xff);
416 BTI2C_DEBUG(printf("r%x+", *byte));
421 DRIVER_MODULE(bti2c, root, bti2c_driver, bti2c_devclass, 0, 0);