Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / video / bktr / bktr_i2c.c
1 /*-
2  * Copyright (c) 1998 Nicolas Souchu
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/bktr/bktr_i2c.c,v 1.13.2.3 2000/10/26 16:38:46 roger Exp $
27  *
28  */
29
30 /*
31  * I2C support for the bti2c chipset.
32  *
33  * From brooktree848.c <fsmp@freefall.org>
34  */
35
36 #include "bktr.h"
37
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #include <sys/module.h>
42 #include <sys/bus.h>
43 #include <sys/uio.h>
44 #include <sys/select.h>
45
46 #if (__FreeBSD_version < 500000)
47 #include <machine/clock.h>              /* for DELAY */
48 #endif
49
50 #if (__FreeBSD_version >=300000)
51 #include <machine/bus_memio.h>          /* for bus space */
52 #include <machine/bus.h>
53 #include <sys/bus.h>
54 #endif
55
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>
61
62 #include <dev/bktr/bktr_i2c.h>
63
64 #include <dev/iicbus/iiconf.h>
65 #include <dev/iicbus/iicbus.h>
66
67 #include <dev/smbus/smbconf.h>
68
69 #include "iicbb_if.h"
70 #include "smbus_if.h"
71
72
73
74 #define I2C_DELAY       40
75
76 #define BTI2C_DEBUG(x)  if (bti2c_debug) (x)
77 static int bti2c_debug = 0;
78
79 struct bti2c_softc {
80
81         bus_space_tag_t     memt;   /* Bus space register access */
82         bus_space_handle_t  memh;   /* Bus space register access */
83
84         int iic_owned;                  /* 1 if we own the iicbus */
85         int smb_owned;                  /* 1 if we own the smbbus */
86
87         device_t smbus;
88         device_t iicbus;
89 };
90
91 struct bt_data {
92         bus_space_tag_t     memt;
93         bus_space_handle_t  memh; 
94 };
95 struct bt_data btdata[NBKTR];
96
97 static int bti2c_probe(device_t);
98 static int bti2c_attach(device_t);
99
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 *);
104
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);
109
110 static devclass_t bti2c_devclass;
111
112 static device_method_t bti2c_methods[] = {
113         /* device interface */
114         DEVMETHOD(device_probe,         bti2c_probe),
115         DEVMETHOD(device_attach,        bti2c_attach),
116
117         /* bus interface */
118         DEVMETHOD(bus_print_child,      bus_generic_print_child),
119
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),
125         
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),
131         
132         { 0, 0 }
133 };
134
135 #if (__FreeBSD_version < 400000)
136 /* FreeBSD 3.x needs DRIVER_TYPE_MISC */
137 static driver_t bti2c_driver = {
138         "bti2c",
139         bti2c_methods,
140         DRIVER_TYPE_MISC,
141         sizeof(struct bti2c_softc),
142 };
143 #endif
144
145 #if (__FreeBSD_version >=400000)
146         static driver_t bti2c_driver = {
147         "bti2c",
148         bti2c_methods,
149         sizeof(struct bti2c_softc),
150 };      
151 #endif
152
153 /*
154  * Call this to pass the address of the bktr device to the
155  * bti2c_i2c layer and initialize all the I2C bus architecture
156  */
157 int
158 bt848_i2c_attach(int unit, struct bktr_softc * bktr, struct bktr_i2c_softc *i2c_sc)
159 {
160         device_t interface;
161         device_t bitbang;
162
163         btdata[unit].memh = bktr->memh;
164         btdata[unit].memt = bktr->memt;
165
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);
169 #else
170         interface = device_add_child(root_bus, "bti2c", unit);
171 #endif
172
173         /* add bit-banging generic code onto bti2c interface */
174 #if (__FreeBSD_version < 400000)
175         bitbang = device_add_child(interface, "iicbb", -1, NULL);
176 #else
177         bitbang = device_add_child(interface, "iicbb", -1);
178 #endif
179
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);
184
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);
189
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);
193
194         return (0);
195 };
196
197 /*
198  * Not a real probe, we know the device exists since the device has
199  * been added after the successfull pci probe.
200  */
201 static int
202 bti2c_probe(device_t dev)
203 {
204         device_set_desc(dev, "bt848 Hard/Soft I2C controller");
205
206         return (0);
207 }
208
209 static int
210 bti2c_attach(device_t dev)
211 {
212         struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev);
213
214         /* XXX should use ivars with pcibus or pcibus methods to access
215          * onboard memory */
216         sc->memh = btdata[device_get_unit(dev)].memh;
217         sc->memt = btdata[device_get_unit(dev)].memt;
218
219         return (0);
220 }
221
222 static int
223 bti2c_smb_callback(device_t dev, int index, caddr_t *data)
224 {
225         struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev);
226         int error = 0;
227         int how;
228
229         /* test each time if we already have/haven't the iicbus
230          * to avoid deadlocks
231          */
232         switch (index) {
233         case SMB_REQUEST_BUS:
234                 if (!sc->iic_owned) {
235                         /* request the iicbus */
236                         how = *(int *)data;
237                         error = iicbus_request_bus(sc->iicbus, dev, how);
238                         if (!error)
239                                 sc->iic_owned = 1;
240                 }
241                 break;
242
243         case SMB_RELEASE_BUS:
244                 if (sc->iic_owned) {
245                         /* release the iicbus */
246                         error = iicbus_release_bus(sc->iicbus, dev);
247                         if (!error)
248                                 sc->iic_owned = 0;
249                 }
250                 break;
251
252         default:
253                 error = EINVAL;
254         }
255
256         return (error);
257 }
258
259 static int
260 bti2c_iic_callback(device_t dev, int index, caddr_t *data)
261 {
262         struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev);
263         int error = 0;
264         int how;
265
266         /* test each time if we already have/haven't the smbus
267          * to avoid deadlocks
268          */
269         switch (index) {
270         case IIC_REQUEST_BUS:
271                 if (!sc->smb_owned) {
272                         /* request the smbus */
273                         how = *(int *)data;
274                         error = smbus_request_bus(sc->smbus, dev, how);
275                         if (!error)
276                                 sc->smb_owned = 1;
277                 }
278                 break;
279
280         case IIC_RELEASE_BUS:
281                 if (sc->smb_owned) {
282                         /* release the smbus */
283                         error = smbus_release_bus(sc->smbus, dev);
284                         if (!error)
285                                 sc->smb_owned = 0;
286                 }
287                 break;
288
289         default:
290                 error = EINVAL;
291         }
292
293         return (error);
294 }
295
296 static int
297 bti2c_iic_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr)
298 {
299         if (oldaddr)
300                 *oldaddr = 0;                   /* XXX */
301
302         return (IIC_ENOADDR);
303 }
304
305 static void
306 bti2c_iic_setlines(device_t dev, int ctrl, int data)
307 {
308         struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev);
309
310         OUTL(sc, BKTR_I2C_DATA_CTL, (ctrl << 1) | data);;
311         DELAY(I2C_DELAY);
312
313         return;
314 }
315
316 static int
317 bti2c_iic_getdataline(device_t dev)
318 {
319         struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev);
320
321         return ( INL(sc,BKTR_I2C_DATA_CTL) & 0x1);
322 }
323
324 static int
325 bti2c_write(struct bti2c_softc* bti2c_sc, u_long data)
326 {
327         u_long          x;
328
329         /* clear status bits */
330         OUTL(bti2c_sc, BKTR_INT_STAT, (BT848_INT_RACK | BT848_INT_I2CDONE));
331
332         BTI2C_DEBUG(printf("w%lx", data));
333
334         /* write the address and data */
335         OUTL(bti2c_sc, BKTR_I2C_DATA_CTL, data);
336
337         /* wait for completion */
338         for ( x = 0x7fffffff; x; --x ) {        /* safety valve */
339                 if ( INL(bti2c_sc, BKTR_INT_STAT) & BT848_INT_I2CDONE )
340                         break;
341         }
342
343         /* check for ACK */
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))?'+':'-'));
347                 return (SMB_ENOACK);
348         }
349         BTI2C_DEBUG(printf("+"));
350
351         /* return OK */
352         return( 0 );
353 }
354
355 static int
356 bti2c_smb_writeb(device_t dev, u_char slave, char cmd, char byte)
357 {
358         struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev);
359         u_long data;
360
361         data = ((slave & 0xff) << 24) | ((byte & 0xff) << 16) | (u_char)cmd;
362
363         return (bti2c_write(sc, data));
364 }
365
366 /*
367  * byte1 becomes low byte of word
368  * byte2 becomes high byte of word
369  */
370 static int
371 bti2c_smb_writew(device_t dev, u_char slave, char cmd, short word)
372 {
373         struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev);
374         u_long data;
375         char low, high;
376
377         low = (char)(word & 0xff);
378         high = (char)((word & 0xff00) >> 8);
379
380         data = ((slave & 0xff) << 24) | ((low & 0xff) << 16) |
381                 ((high & 0xff) << 8) | BT848_DATA_CTL_I2CW3B | (u_char)cmd;
382
383         return (bti2c_write(sc, data));
384 }
385
386 /*
387  * The Bt878 and Bt879 differed on the treatment of i2c commands
388  */
389 static int
390 bti2c_smb_readb(device_t dev, u_char slave, char cmd, char *byte)
391 {
392         struct bti2c_softc *sc = (struct bti2c_softc *)device_get_softc(dev);
393         u_long          x;
394
395         /* clear status bits */
396         OUTL(sc,BKTR_INT_STAT, (BT848_INT_RACK | BT848_INT_I2CDONE));
397
398         OUTL(sc,BKTR_I2C_DATA_CTL, ((slave & 0xff) << 24) | (u_char)cmd);;
399
400         BTI2C_DEBUG(printf("r%lx/", (u_long)(((slave & 0xff) << 24) | (u_char)cmd)));
401
402         /* wait for completion */
403         for ( x = 0x7fffffff; x; --x ) {        /* safety valve */
404                 if ( INL(sc,BKTR_INT_STAT) & BT848_INT_I2CDONE )
405                         break;
406         }
407
408         /* check for ACK */
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))?'+':'-'));
412                 return (SMB_ENOACK);
413         }
414
415         *byte = (char)((INL(sc,BKTR_I2C_DATA_CTL) >> 8) & 0xff);
416         BTI2C_DEBUG(printf("r%x+", *byte));
417
418         return (0);
419 }
420
421 DRIVER_MODULE(bti2c, root, bti2c_driver, bti2c_devclass, 0, 0);