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