adb.c revision 1.26 1 /* $NetBSD: adb.c,v 1.26 2009/03/14 15:36:09 dsl Exp $ */
2
3 /*-
4 * Copyright (C) 1994 Bradley A. Grantham
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Bradley A. Grantham.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: adb.c,v 1.26 2009/03/14 15:36:09 dsl Exp $");
35
36 #include <sys/param.h>
37 #include <sys/device.h>
38 #include <sys/fcntl.h>
39 #include <sys/poll.h>
40 #include <sys/select.h>
41 #include <sys/proc.h>
42 #include <sys/signalvar.h>
43 #include <sys/systm.h>
44
45 #include <machine/bus.h>
46 #include <machine/autoconf.h>
47 #include <machine/pio.h>
48
49 #include <macppc/dev/adbvar.h>
50 #include <macppc/dev/akbdvar.h>
51 #include <macppc/dev/pm_direct.h>
52 #include <macppc/dev/viareg.h>
53
54 #include <dev/clock_subr.h>
55 #include <dev/ofw/openfirm.h>
56
57 #include "aed.h"
58 #include "apm.h"
59
60 /*
61 * Function declarations.
62 */
63 static int adbmatch(struct device *, struct cfdata *, void *);
64 static void adbattach(struct device *, struct device *, void *);
65 static int adbprint(void *, const char *);
66 static void adb_todr_init(void);
67
68 /*
69 * Global variables.
70 */
71 int adb_polling = 0; /* Are we polling? (Debugger mode) */
72 int adb_initted = 0; /* adb_init() has completed successfully */
73 #ifdef ADB_DEBUG
74 int adb_debug = 0; /* Output debugging messages */
75 #endif /* ADB_DEBUG */
76
77 /*
78 * Driver definition.
79 */
80 CFATTACH_DECL(adb, sizeof(struct adb_softc),
81 adbmatch, adbattach, NULL, NULL);
82
83 static int
84 adbmatch(struct device *parent, struct cfdata *cf, void *aux)
85 {
86 struct confargs *ca = aux;
87
88 if (ca->ca_nreg < 8)
89 return 0;
90
91 if (ca->ca_nintr < 4)
92 return 0;
93
94 if (strcmp(ca->ca_name, "via-cuda") == 0)
95 return 1;
96
97 if (strcmp(ca->ca_name, "via-pmu") == 0)
98 return 1;
99
100 return 0;
101 }
102
103 static void
104 adbattach(parent, self, aux)
105 struct device *parent, *self;
106 void *aux;
107 {
108 struct adb_softc *sc = (struct adb_softc *)self;
109 struct confargs *ca = aux;
110 int irq = ca->ca_intr[0];
111 int node;
112 ADBDataBlock adbdata;
113 struct adb_attach_args aa_args;
114 int totaladbs;
115 int adbindex, adbaddr, adb_node;
116
117 extern volatile u_char *Via1Base;
118
119 ca->ca_reg[0] += ca->ca_baseaddr;
120
121 sc->sc_regbase = mapiodev(ca->ca_reg[0], ca->ca_reg[1]);
122 Via1Base = sc->sc_regbase;
123
124 if (strcmp(ca->ca_name, "via-cuda") == 0)
125 adbHardware = ADB_HW_CUDA;
126 else if (strcmp(ca->ca_name, "via-pmu") == 0)
127 adbHardware = ADB_HW_PMU;
128
129 node = of_getnode_byname(OF_parent(ca->ca_node), "extint-gpio1");
130 if (node)
131 OF_getprop(node, "interrupts", &irq, 4);
132
133 printf(" irq %d: ", irq);
134
135 adb_polling = 1;
136 adb_node = of_getnode_byname(ca->ca_node, "adb");
137 if (adb_node)
138 ADBReInit();
139
140 switch (adbHardware) {
141 case ADB_HW_CUDA:
142 intr_establish(irq, IST_LEVEL, IPL_TTY, adb_intr_cuda, sc);
143 break;
144 case ADB_HW_PMU:
145 intr_establish(irq, IST_LEVEL, IPL_TTY, pm_intr, sc);
146 pm_init();
147 break;
148 }
149
150 adb_todr_init();
151
152 #if NAPM > 0
153 /* Magic for signalling the apm driver to match. */
154 aa_args.origaddr = ADBADDR_APM;
155 aa_args.adbaddr = ADBADDR_APM;
156 aa_args.handler_id = ADBADDR_APM;
157
158 (void)config_found(self, &aa_args, NULL);
159 #endif
160
161 /*
162 * see if we're supposed to have an ADB bus
163 * since some PowerBooks don't have one and their PMUs barf on ADB
164 * commands we bail here if there's no adb node
165 */
166 if (!adb_node)
167 return;
168
169 #ifdef ADB_DEBUG
170 if (adb_debug)
171 printf("adb: done with ADBReInit\n");
172 #endif
173 totaladbs = CountADBs();
174
175 printf("%d targets\n", totaladbs);
176
177 #if NAED > 0
178 /* ADB event device for compatibility */
179 aa_args.origaddr = 0;
180 aa_args.adbaddr = 0;
181 aa_args.handler_id = 0;
182 (void)config_found(self, &aa_args, adbprint);
183 #endif
184
185 /* for each ADB device */
186 for (adbindex = 1; adbindex <= totaladbs; adbindex++) {
187 /* Get the ADB information */
188 adbaddr = GetIndADB(&adbdata, adbindex);
189
190 aa_args.origaddr = adbdata.origADBAddr;
191 aa_args.adbaddr = adbaddr;
192 aa_args.handler_id = adbdata.devType;
193
194 (void)config_found(self, &aa_args, adbprint);
195 }
196
197 if (adbHardware == ADB_HW_CUDA)
198 adb_cuda_autopoll();
199 adb_polling = 0;
200
201 }
202
203 int
204 adbprint(void *args, const char *name)
205 {
206 struct adb_attach_args *aa_args = (struct adb_attach_args *)args;
207 int rv = UNCONF;
208
209 if (name) { /* no configured device matched */
210 rv = UNSUPP; /* most ADB device types are unsupported */
211
212 /* print out what kind of ADB device we have found */
213 aprint_normal("%s addr %d: ", name, aa_args->adbaddr);
214 switch(aa_args->origaddr) {
215 #ifdef DIAGNOSTIC
216 case 0:
217 aprint_normal("ADB event device");
218 rv = UNCONF;
219 break;
220 case ADBADDR_SECURE:
221 aprint_normal("security dongle (%d)",
222 aa_args->handler_id);
223 break;
224 #endif
225 case ADBADDR_MAP:
226 aprint_normal("mapped device (%d)",
227 aa_args->handler_id);
228 rv = UNCONF;
229 break;
230 case ADBADDR_REL:
231 aprint_normal("relative positioning device (%d)",
232 aa_args->handler_id);
233 rv = UNCONF;
234 break;
235 #ifdef DIAGNOSTIC
236 case ADBADDR_ABS:
237 switch (aa_args->handler_id) {
238 case ADB_ARTPAD:
239 aprint_normal("WACOM ArtPad II");
240 break;
241 default:
242 aprint_normal("absolute positioning device (%d)",
243 aa_args->handler_id);
244 break;
245 }
246 break;
247 case ADBADDR_DATATX:
248 aprint_normal("data transfer device (modem?) (%d)",
249 aa_args->handler_id);
250 break;
251 case ADBADDR_MISC:
252 switch (aa_args->handler_id) {
253 case ADB_POWERKEY:
254 aprint_normal("Sophisticated Circuits PowerKey");
255 break;
256 default:
257 aprint_normal("misc. device (remote control?) (%d)",
258 aa_args->handler_id);
259 break;
260 }
261 break;
262 default:
263 aprint_normal("unknown type device, (handler %d)",
264 aa_args->handler_id);
265 break;
266 #endif /* DIAGNOSTIC */
267 }
268 } else /* a device matched and was configured */
269 aprint_normal(" addr %d: ", aa_args->adbaddr);
270
271 return rv;
272 }
273
274 #define DIFF19041970 2082844800
275
276 static int
277 adb_todr_get(todr_chip_handle_t tch, volatile struct timeval *tvp)
278 {
279 unsigned long sec;
280
281 if (adb_read_date_time(&sec) != 0)
282 return EIO;
283 tvp->tv_sec = sec - DIFF19041970;
284 tvp->tv_usec = 0;
285 return 0;
286 }
287
288 static int
289 adb_todr_set(todr_chip_handle_t tch, volatile struct timeval *tvp)
290 {
291 unsigned long sec;
292
293 sec = tvp->tv_sec + DIFF19041970;
294 return adb_set_date_time(sec) ? EIO : 0;
295 }
296
297 void
298 adb_todr_init(void)
299 {
300 static struct todr_chip_handle tch = {
301 .todr_gettime = adb_todr_get,
302 .todr_settime = adb_todr_set
303 };
304
305 todr_attach(&tch);
306 }
307