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 $
27 * $DragonFly: src/sys/dev/video/bktr/bktr_i2c.c,v 1.4 2004/02/13 01:45:15 joerg Exp $
32 * I2C support for the bti2c chipset.
34 * From brooktree848.c <fsmp@freefall.org>
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/systm.h>
42 #include <sys/module.h>
45 #include <sys/select.h>
47 #if defined(__DragonFly__) || (__FreeBSD_version < 500000)
48 #include <machine/clock.h> /* for DELAY */
51 #if defined(__DragonFly__) || (__FreeBSD_version >=300000)
52 #include <machine/bus_memio.h> /* for bus space */
53 #include <machine/bus.h>
57 #include <bus/pci/pcivar.h>
58 #include <bus/pci/pcireg.h>
59 #include <machine/ioctl_meteor.h>
60 #include <machine/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
65 #include <bus/iicbus/iiconf.h>
66 #include <bus/iicbus/iicbus.h>
68 #include <bus/smbus/smbconf.h>
77 #define BTI2C_DEBUG(x) if (bti2c_debug) (x)
78 static int bti2c_debug = 0;
82 bus_space_tag_t memt; /* Bus space register access */
83 bus_space_handle_t memh; /* Bus space register access */
85 int iic_owned; /* 1 if we own the iicbus */
86 int smb_owned; /* 1 if we own the smbbus */
94 bus_space_handle_t memh;
96 struct bt_data btdata[NBKTR];
98 static int bti2c_probe(device_t);
99 static int bti2c_attach(device_t);
101 static int bti2c_iic_callback(device_t, int, caddr_t *);
102 static void bti2c_iic_setlines(device_t, int, int);
103 static int bti2c_iic_getdataline(device_t);
104 static int bti2c_iic_reset(device_t, u_char, u_char, u_char *);
106 static int bti2c_smb_callback(device_t, int, caddr_t *);
107 static int bti2c_smb_writeb(device_t dev, u_char slave, char cmd, char byte);
108 static int bti2c_smb_writew(device_t dev, u_char slave, char cmd, short word);
109 static int bti2c_smb_readb(device_t dev, u_char slave, char cmd, char *byte);
111 static devclass_t bti2c_devclass;
113 static device_method_t bti2c_methods[] = {
114 /* device interface */
115 DEVMETHOD(device_probe, bti2c_probe),
116 DEVMETHOD(device_attach, bti2c_attach),
119 DEVMETHOD(bus_print_child, bus_generic_print_child),
121 /* iicbb interface */
122 DEVMETHOD(iicbb_callback, bti2c_iic_callback),
123 DEVMETHOD(iicbb_setlines, bti2c_iic_setlines),
124 DEVMETHOD(iicbb_getdataline, bti2c_iic_getdataline),
125 DEVMETHOD(iicbb_reset, bti2c_iic_reset),
127 /* smbus interface */
128 DEVMETHOD(smbus_callback, bti2c_smb_callback),
129 DEVMETHOD(smbus_writeb, bti2c_smb_writeb),
130 DEVMETHOD(smbus_writew, bti2c_smb_writew),
131 DEVMETHOD(smbus_readb, bti2c_smb_readb),
136 #if defined(__FreeBSD__) && (__FreeBSD_version < 400000)
137 /* FreeBSD 3.x needs DRIVER_TYPE_MISC */
138 static driver_t bti2c_driver = {
142 sizeof(struct bti2c_softc),
146 #if defined(__DragonFly__) || (__FreeBSD_version >=400000)
147 static driver_t bti2c_driver = {
150 sizeof(struct bti2c_softc),
155 * Call this to pass the address of the bktr device to the
156 * bti2c_i2c layer and initialize all the I2C bus architecture
159 bt848_i2c_attach(int unit, struct bktr_softc * bktr, struct bktr_i2c_softc *i2c_sc)
164 btdata[unit].memh = bktr->memh;
165 btdata[unit].memt = bktr->memt;
167 /* XXX add the I2C interface to the root_bus until pcibus is ready */
168 #if defined(__FreeBSD__) && (__FreeBSD_version < 400000)
169 interface = device_add_child(root_bus, "bti2c", unit, NULL);
171 interface = device_add_child(root_bus, "bti2c", unit);
174 /* add bit-banging generic code onto bti2c interface */
175 #if defined(__FreeBSD__) && (__FreeBSD_version < 400000)
176 bitbang = device_add_child(interface, "iicbb", -1, NULL);
178 bitbang = device_add_child(interface, "iicbb", -1);
181 /* probe and attach the interface, we need it NOW
182 * bit-banging code is also probed and attached */
183 device_probe_and_attach(interface);
184 device_probe_and_attach(bitbang);
186 /* smb and i2c interfaces are available for the bt848 chip
187 * connect bit-banging generic code to an iicbus */
188 if ((i2c_sc->iicbus = iicbus_alloc_bus(bitbang)))
189 device_probe_and_attach(i2c_sc->iicbus);
191 /* hardware i2c is actually smb over the bti2c interface */
192 if ((i2c_sc->smbus = smbus_alloc_bus(interface)))
193 device_probe_and_attach(i2c_sc->smbus);
199 * Not a real probe, we know the device exists since the device has
200 * been added after the successfull pci probe.
203 bti2c_probe(device_t dev)
205 device_set_desc(dev, "bt848 Hard/Soft I2C controller");
211 bti2c_attach(device_t dev)
213 struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev);
215 /* XXX should use ivars with pcibus or pcibus methods to access
217 sc->memh = btdata[device_get_unit(dev)].memh;
218 sc->memt = btdata[device_get_unit(dev)].memt;
224 bti2c_smb_callback(device_t dev, int index, caddr_t *data)
226 struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev);
230 /* test each time if we already have/haven't the iicbus
234 case SMB_REQUEST_BUS:
235 if (!sc->iic_owned) {
236 /* request the iicbus */
238 error = iicbus_request_bus(sc->iicbus, dev, how);
244 case SMB_RELEASE_BUS:
246 /* release the iicbus */
247 error = iicbus_release_bus(sc->iicbus, dev);
261 bti2c_iic_callback(device_t dev, int index, caddr_t *data)
263 struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev);
267 /* test each time if we already have/haven't the smbus
271 case IIC_REQUEST_BUS:
272 if (!sc->smb_owned) {
273 /* request the smbus */
275 error = smbus_request_bus(sc->smbus, dev, how);
281 case IIC_RELEASE_BUS:
283 /* release the smbus */
284 error = smbus_release_bus(sc->smbus, dev);
298 bti2c_iic_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr)
301 *oldaddr = 0; /* XXX */
303 return (IIC_ENOADDR);
307 bti2c_iic_setlines(device_t dev, int ctrl, int data)
309 struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev);
311 OUTL(sc, BKTR_I2C_DATA_CTL, (ctrl << 1) | data);;
318 bti2c_iic_getdataline(device_t dev)
320 struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev);
322 return ( INL(sc,BKTR_I2C_DATA_CTL) & 0x1);
326 bti2c_write(struct bti2c_softc* bti2c_sc, u_long data)
330 /* clear status bits */
331 OUTL(bti2c_sc, BKTR_INT_STAT, (BT848_INT_RACK | BT848_INT_I2CDONE));
333 BTI2C_DEBUG(printf("w%lx", data));
335 /* write the address and data */
336 OUTL(bti2c_sc, BKTR_I2C_DATA_CTL, data);
338 /* wait for completion */
339 for ( x = 0x7fffffff; x; --x ) { /* safety valve */
340 if ( INL(bti2c_sc, BKTR_INT_STAT) & BT848_INT_I2CDONE )
345 if ( !x || !( INL(bti2c_sc, BKTR_INT_STAT) & BT848_INT_RACK) ) {
346 BTI2C_DEBUG(printf("%c%c", (!x)?'+':'-',
347 (!( INL(bti2c_sc, BKTR_INT_STAT) & BT848_INT_RACK))?'+':'-'));
350 BTI2C_DEBUG(printf("+"));
357 bti2c_smb_writeb(device_t dev, u_char slave, char cmd, char byte)
359 struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev);
362 data = ((slave & 0xff) << 24) | ((byte & 0xff) << 16) | (u_char)cmd;
364 return (bti2c_write(sc, data));
368 * byte1 becomes low byte of word
369 * byte2 becomes high byte of word
372 bti2c_smb_writew(device_t dev, u_char slave, char cmd, short word)
374 struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev);
378 low = (char)(word & 0xff);
379 high = (char)((word & 0xff00) >> 8);
381 data = ((slave & 0xff) << 24) | ((low & 0xff) << 16) |
382 ((high & 0xff) << 8) | BT848_DATA_CTL_I2CW3B | (u_char)cmd;
384 return (bti2c_write(sc, data));
388 * The Bt878 and Bt879 differed on the treatment of i2c commands
391 bti2c_smb_readb(device_t dev, u_char slave, char cmd, char *byte)
393 struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev);
396 /* clear status bits */
397 OUTL(sc,BKTR_INT_STAT, (BT848_INT_RACK | BT848_INT_I2CDONE));
399 OUTL(sc,BKTR_I2C_DATA_CTL, ((slave & 0xff) << 24) | (u_char)cmd);;
401 BTI2C_DEBUG(printf("r%lx/", (u_long)(((slave & 0xff) << 24) | (u_char)cmd)));
403 /* wait for completion */
404 for ( x = 0x7fffffff; x; --x ) { /* safety valve */
405 if ( INL(sc,BKTR_INT_STAT) & BT848_INT_I2CDONE )
410 if ( !x || !(INL(sc,BKTR_INT_STAT) & BT848_INT_RACK) ) {
411 BTI2C_DEBUG(printf("r%c%c", (!x)?'+':'-',
412 (!( INL(sc,BKTR_INT_STAT) & BT848_INT_RACK))?'+':'-'));
416 *byte = (char)((INL(sc,BKTR_I2C_DATA_CTL) >> 8) & 0xff);
417 BTI2C_DEBUG(printf("r%x+", *byte));
422 DRIVER_MODULE(bti2c, root, bti2c_driver, bti2c_devclass, 0, 0);