Merge from vendor branch GDB:
[dragonfly.git] / sys / dev / misc / lpbb / lpbb.c
1 /*-
2  * Copyright (c) 1998 Nicolas Souchu, Marc Bouget
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/ppbus/lpbb.c,v 1.11.2.1 2000/05/24 00:20:57 n_hibma Exp $
27  * $DragonFly: src/sys/dev/misc/lpbb/lpbb.c,v 1.3 2003/08/07 21:16:56 dillon Exp $
28  *
29  */
30
31 /*
32  * I2C Bit-Banging over parallel port
33  *
34  * See the Official Philips interface description in lpbb(4)
35  */
36
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/systm.h>
40 #include <sys/module.h>
41 #include <sys/bus.h>
42 #include <sys/uio.h>
43
44 #include <machine/clock.h>
45
46 #include <bus/ppbus/ppbconf.h>
47 #include "ppbus_if.h"
48 #include <bus/ppbus/ppbio.h>
49
50 #include <bus/iicbus/iiconf.h>
51 #include <bus/iicbus/iicbus.h>
52
53 #include "iicbb_if.h"
54
55 static int lpbb_detect(device_t dev);
56
57 static void
58 lpbb_identify(driver_t *driver, device_t parent)
59 {
60
61         BUS_ADD_CHILD(parent, 0, "lpbb", 0);
62 }
63
64 static int
65 lpbb_probe(device_t dev)
66 {
67
68         /* Perhaps call this during identify instead? */
69         if (!lpbb_detect(dev))
70                 return (ENXIO);
71
72         device_set_desc(dev, "Parallel I2C bit-banging interface");
73
74         return (0);
75 }
76
77 static int
78 lpbb_attach(device_t dev)
79 {
80         device_t bitbang, iicbus;
81         
82         /* add generic bit-banging code */
83         bitbang = device_add_child(dev, "iicbb", -1);
84
85         /* add the iicbus to the tree */
86         iicbus = iicbus_alloc_bus(bitbang);
87
88         device_probe_and_attach(bitbang);
89
90         /* XXX should be in iicbb_attach! */
91         device_probe_and_attach(iicbus);
92
93         return (0);
94 }
95
96 static int
97 lpbb_callback(device_t dev, int index, caddr_t *data)
98 {
99         device_t ppbus = device_get_parent(dev);
100         int error = 0;
101         int how;
102
103         switch (index) {
104         case IIC_REQUEST_BUS:
105                 /* request the ppbus */
106                 how = *(int *)data;
107                 error = ppb_request_bus(ppbus, dev, how);
108                 break;
109
110         case IIC_RELEASE_BUS:
111                 /* release the ppbus */
112                 error = ppb_release_bus(ppbus, dev);
113                 break;
114
115         default:
116                 error = EINVAL;
117         }
118
119         return (error);
120 }
121
122 #define SDA_out 0x80
123 #define SCL_out 0x08
124 #define SDA_in  0x80
125 #define SCL_in  0x08
126 #define ALIM    0x20
127 #define I2CKEY  0x50
128
129 static int getSDA(device_t ppbus)
130 {
131         if((ppb_rstr(ppbus)&SDA_in)==SDA_in)
132                 return 1;                   
133         else                                
134                 return 0;                   
135 }
136
137 static void setSDA(device_t ppbus, char val)
138 {
139         if(val==0)
140                 ppb_wdtr(ppbus, (u_char)SDA_out);
141         else                            
142                 ppb_wdtr(ppbus, (u_char)~SDA_out);
143 }
144
145 static void setSCL(device_t ppbus, unsigned char val)
146 {
147         if(val==0)
148                 ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus)&~SCL_out));
149         else                                               
150                 ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus)|SCL_out)); 
151 }
152
153 static int lpbb_detect(device_t dev)
154 {
155         device_t ppbus = device_get_parent(dev);
156
157         if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) {
158                 device_printf(dev, "can't allocate ppbus\n");
159                 return (0);
160         }
161
162         /* reset bus */
163         setSDA(ppbus, 1);
164         setSCL(ppbus, 1);
165
166         if ((ppb_rstr(ppbus) & I2CKEY) ||
167                 ((ppb_rstr(ppbus) & ALIM) != ALIM)) {
168
169                 ppb_release_bus(ppbus, dev);
170                 return (0);
171         }
172
173         ppb_release_bus(ppbus, dev);
174
175         return (1);
176 }
177
178 static int
179 lpbb_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr)
180 {
181         device_t ppbus = device_get_parent(dev);
182
183         /* reset bus */
184         setSDA(ppbus, 1);
185         setSCL(ppbus, 1);
186
187         return (IIC_ENOADDR);
188 }
189
190 static void
191 lpbb_setlines(device_t dev, int ctrl, int data)
192 {
193         device_t ppbus = device_get_parent(dev);
194
195         setSCL(ppbus, ctrl);
196         setSDA(ppbus, data);
197 }
198
199 static int
200 lpbb_getdataline(device_t dev)
201 {
202         device_t ppbus = device_get_parent(dev);
203
204         return (getSDA(ppbus));
205 }
206
207 static devclass_t lpbb_devclass;
208
209 static device_method_t lpbb_methods[] = {
210         /* device interface */
211         DEVMETHOD(device_identify,      lpbb_identify),
212         DEVMETHOD(device_probe,         lpbb_probe),
213         DEVMETHOD(device_attach,        lpbb_attach),
214
215         /* bus interface */
216         DEVMETHOD(bus_print_child,      bus_generic_print_child),
217
218         /* iicbb interface */
219         DEVMETHOD(iicbb_callback,       lpbb_callback),
220         DEVMETHOD(iicbb_setlines,       lpbb_setlines),
221         DEVMETHOD(iicbb_getdataline,    lpbb_getdataline),
222         DEVMETHOD(iicbb_reset,          lpbb_reset),
223
224         { 0, 0 }
225 };
226
227 static driver_t lpbb_driver = {
228         "lpbb",
229         lpbb_methods,
230         1,
231 };
232
233 DRIVER_MODULE(lpbb, ppbus, lpbb_driver, lpbb_devclass, 0, 0);