adb.c revision 1.20 1 /* $NetBSD: adb.c,v 1.20 2006/09/13 03:37:20 gdamore 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.20 2006/09/13 03:37:20 gdamore 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/autoconf.h>
46
47 #include <macppc/dev/adbvar.h>
48 #include <macppc/dev/akbdvar.h>
49 #include <macppc/dev/pm_direct.h>
50 #include <macppc/dev/viareg.h>
51
52 #include <dev/clock_subr.h>
53 #include <dev/ofw/openfirm.h>
54
55 #include "aed.h"
56 #include "apm.h"
57
58 /*
59 * Function declarations.
60 */
61 static int adbmatch __P((struct device *, struct cfdata *, void *));
62 static void adbattach __P((struct device *, struct device *, void *));
63 static int adbprint __P((void *, const char *));
64 static void adb_todr_init(void);
65
66 /*
67 * Global variables.
68 */
69 int adb_polling = 0; /* Are we polling? (Debugger mode) */
70 int adb_initted = 0; /* adb_init() has completed successfully */
71 #ifdef ADB_DEBUG
72 int adb_debug = 0; /* Output debugging messages */
73 #endif /* ADB_DEBUG */
74
75 /*
76 * Driver definition.
77 */
78 CFATTACH_DECL(adb, sizeof(struct adb_softc),
79 adbmatch, adbattach, NULL, NULL);
80
81 static int
82 adbmatch(parent, cf, aux)
83 struct device *parent;
84 struct cfdata *cf;
85 void *aux;
86 {
87 struct confargs *ca = aux;
88
89 if (ca->ca_nreg < 8)
90 return 0;
91
92 if (ca->ca_nintr < 4)
93 return 0;
94
95 if (strcmp(ca->ca_name, "via-cuda") == 0)
96 return 1;
97
98 if (strcmp(ca->ca_name, "via-pmu") == 0)
99 return 1;
100
101 return 0;
102 }
103
104 static void
105 adbattach(parent, self, aux)
106 struct device *parent, *self;
107 void *aux;
108 {
109 struct adb_softc *sc = (struct adb_softc *)self;
110 struct confargs *ca = aux;
111 int irq = ca->ca_intr[0];
112 int node;
113 ADBDataBlock adbdata;
114 struct adb_attach_args aa_args;
115 int totaladbs;
116 int adbindex, adbaddr;
117
118 extern volatile u_char *Via1Base;
119
120 ca->ca_reg[0] += ca->ca_baseaddr;
121
122 sc->sc_regbase = mapiodev(ca->ca_reg[0], ca->ca_reg[1]);
123 Via1Base = sc->sc_regbase;
124
125 if (strcmp(ca->ca_name, "via-cuda") == 0)
126 adbHardware = ADB_HW_CUDA;
127 else if (strcmp(ca->ca_name, "via-pmu") == 0)
128 adbHardware = ADB_HW_PMU;
129
130 node = getnodebyname(OF_parent(ca->ca_node), "extint-gpio1");
131 if (node)
132 OF_getprop(node, "interrupts", &irq, 4);
133
134 printf(" irq %d: ", irq);
135
136 adb_polling = 1;
137 ADBReInit();
138
139 switch (adbHardware) {
140 case ADB_HW_CUDA:
141 intr_establish(irq, IST_LEVEL, IPL_HIGH, adb_intr_cuda, sc);
142 break;
143 case ADB_HW_PMU:
144 intr_establish(irq, IST_LEVEL, IPL_HIGH, pm_intr, sc);
145 pm_init();
146 break;
147 }
148
149 #ifdef ADB_DEBUG
150 if (adb_debug)
151 printf("adb: done with ADBReInit\n");
152 #endif
153 totaladbs = CountADBs();
154
155 printf("%d targets\n", totaladbs);
156
157 #if NAED > 0
158 /* ADB event device for compatibility */
159 aa_args.origaddr = 0;
160 aa_args.adbaddr = 0;
161 aa_args.handler_id = 0;
162 (void)config_found(self, &aa_args, adbprint);
163 #endif
164
165 /* for each ADB device */
166 for (adbindex = 1; adbindex <= totaladbs; adbindex++) {
167 /* Get the ADB information */
168 adbaddr = GetIndADB(&adbdata, adbindex);
169
170 aa_args.origaddr = adbdata.origADBAddr;
171 aa_args.adbaddr = adbaddr;
172 aa_args.handler_id = adbdata.devType;
173
174 (void)config_found(self, &aa_args, adbprint);
175 }
176
177 #if NAPM > 0
178 /* Magic for signalling the apm driver to match. */
179 aa_args.origaddr = ADBADDR_APM;
180 aa_args.adbaddr = ADBADDR_APM;
181 aa_args.handler_id = ADBADDR_APM;
182
183 (void)config_found(self, &aa_args, NULL);
184 #endif
185
186 if (adbHardware == ADB_HW_CUDA)
187 adb_cuda_autopoll();
188 adb_polling = 0;
189
190 adb_todr_init();
191 }
192
193 int
194 adbprint(args, name)
195 void *args;
196 const char *name;
197 {
198 struct adb_attach_args *aa_args = (struct adb_attach_args *)args;
199 int rv = UNCONF;
200
201 if (name) { /* no configured device matched */
202 rv = UNSUPP; /* most ADB device types are unsupported */
203
204 /* print out what kind of ADB device we have found */
205 aprint_normal("%s addr %d: ", name, aa_args->adbaddr);
206 switch(aa_args->origaddr) {
207 #ifdef DIAGNOSTIC
208 case 0:
209 aprint_normal("ADB event device");
210 rv = UNCONF;
211 break;
212 case ADBADDR_SECURE:
213 aprint_normal("security dongle (%d)",
214 aa_args->handler_id);
215 break;
216 #endif
217 case ADBADDR_MAP:
218 aprint_normal("mapped device (%d)",
219 aa_args->handler_id);
220 rv = UNCONF;
221 break;
222 case ADBADDR_REL:
223 aprint_normal("relative positioning device (%d)",
224 aa_args->handler_id);
225 rv = UNCONF;
226 break;
227 #ifdef DIAGNOSTIC
228 case ADBADDR_ABS:
229 switch (aa_args->handler_id) {
230 case ADB_ARTPAD:
231 aprint_normal("WACOM ArtPad II");
232 break;
233 default:
234 aprint_normal("absolute positioning device (%d)",
235 aa_args->handler_id);
236 break;
237 }
238 break;
239 case ADBADDR_DATATX:
240 aprint_normal("data transfer device (modem?) (%d)",
241 aa_args->handler_id);
242 break;
243 case ADBADDR_MISC:
244 switch (aa_args->handler_id) {
245 case ADB_POWERKEY:
246 aprint_normal("Sophisticated Circuits PowerKey");
247 break;
248 default:
249 aprint_normal("misc. device (remote control?) (%d)",
250 aa_args->handler_id);
251 break;
252 }
253 break;
254 default:
255 aprint_normal("unknown type device, (handler %d)",
256 aa_args->handler_id);
257 break;
258 #endif /* DIAGNOSTIC */
259 }
260 } else /* a device matched and was configured */
261 aprint_normal(" addr %d: ", aa_args->adbaddr);
262
263 return rv;
264 }
265
266 #define DIFF19041970 2082844800
267
268 static int
269 adb_todr_get(todr_chip_handle_t tch, volatile struct timeval *tvp)
270 {
271 unsigned long sec;
272
273 if (adb_read_date_time(&sec) != 0)
274 return EIO;
275 tvp->tv_sec = sec - DIFF19041970;
276 tvp->tv_usec = 0;
277 return 0;
278 }
279
280 static int
281 adb_todr_set(todr_chip_handle_t tch, volatile struct timeval *tvp)
282 {
283 unsigned long sec;
284
285 sec = tvp->tv_sec + DIFF19041970;
286 return adb_set_date_time(sec) ? EIO : 0;
287 }
288
289 void
290 adb_todr_init(void)
291 {
292 static struct todr_chip_handle tch = {
293 .todr_gettime = adb_todr_get,
294 .todr_settime = adb_todr_set
295 };
296
297 todr_attach(&tch);
298 }
299