adb.c revision 1.61 1 1.61 nat /* $NetBSD: adb.c,v 1.61 2024/09/18 01:29:02 nat Exp $ */
2 1.1 briggs
3 1.25 ender /*
4 1.1 briggs * Copyright (C) 1994 Bradley A. Grantham
5 1.1 briggs * All rights reserved.
6 1.1 briggs *
7 1.1 briggs * Redistribution and use in source and binary forms, with or without
8 1.1 briggs * modification, are permitted provided that the following conditions
9 1.1 briggs * are met:
10 1.1 briggs * 1. Redistributions of source code must retain the above copyright
11 1.1 briggs * notice, this list of conditions and the following disclaimer.
12 1.1 briggs * 2. Redistributions in binary form must reproduce the above copyright
13 1.25 ender * notice, this list of conditions and the following disclaimer in the
14 1.1 briggs * documentation and/or other materials provided with the distribution.
15 1.1 briggs *
16 1.1 briggs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 briggs * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 briggs * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 briggs * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 briggs * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 briggs * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 briggs * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 briggs * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 briggs * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 1.1 briggs * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 briggs */
27 1.45 lukem
28 1.45 lukem #include <sys/cdefs.h>
29 1.61 nat __KERNEL_RCSID(0, "$NetBSD: adb.c,v 1.61 2024/09/18 01:29:02 nat Exp $");
30 1.1 briggs
31 1.20 scottr #include "opt_adb.h"
32 1.20 scottr
33 1.1 briggs #include <sys/param.h>
34 1.1 briggs #include <sys/device.h>
35 1.1 briggs #include <sys/fcntl.h>
36 1.10 scottr #include <sys/poll.h>
37 1.1 briggs #include <sys/select.h>
38 1.1 briggs #include <sys/proc.h>
39 1.7 briggs #include <sys/signalvar.h>
40 1.1 briggs #include <sys/systm.h>
41 1.51 ad #include <sys/cpu.h>
42 1.51 ad #include <sys/intr.h>
43 1.2 briggs
44 1.7 briggs #include <machine/autoconf.h>
45 1.2 briggs
46 1.17 scottr #include <mac68k/mac68k/macrom.h>
47 1.17 scottr #include <mac68k/dev/adbvar.h>
48 1.34 scottr #include <mac68k/dev/akbdvar.h>
49 1.25 ender
50 1.25 ender #include "aed.h" /* ADB Event Device for compatibility */
51 1.1 briggs
52 1.1 briggs /*
53 1.2 briggs * Function declarations.
54 1.1 briggs */
55 1.54 matt static int adbmatch(device_t, cfdata_t, void *);
56 1.54 matt static void adbattach(device_t, device_t, void *);
57 1.46 chs static int adbprint(void *, const char *);
58 1.54 matt void adb_config_interrupts(device_t);
59 1.25 ender
60 1.46 chs extern void adb_jadbproc(void);
61 1.1 briggs
62 1.2 briggs /*
63 1.2 briggs * Global variables.
64 1.2 briggs */
65 1.30 scottr int adb_polling = 0; /* Are we polling? (Debugger mode) */
66 1.20 scottr #ifdef ADB_DEBUG
67 1.20 scottr int adb_debug = 0; /* Output debugging messages */
68 1.20 scottr #endif /* ADB_DEBUG */
69 1.1 briggs
70 1.25 ender extern struct mac68k_machine_S mac68k_machine;
71 1.25 ender extern int adbHardware;
72 1.25 ender extern char *adbHardwareDescr[];
73 1.25 ender
74 1.1 briggs /*
75 1.25 ender * Driver definition.
76 1.1 briggs */
77 1.54 matt CFATTACH_DECL_NEW(adb, 0,
78 1.43 thorpej adbmatch, adbattach, NULL, NULL);
79 1.7 briggs
80 1.7 briggs static int
81 1.54 matt adbmatch(device_t parent, cfdata_t cf, void *aux)
82 1.7 briggs {
83 1.54 matt static bool adb_matched;
84 1.18 scottr
85 1.18 scottr /* Allow only one instance. */
86 1.18 scottr if (adb_matched)
87 1.18 scottr return (0);
88 1.18 scottr
89 1.54 matt adb_matched = true;
90 1.18 scottr return (1);
91 1.7 briggs }
92 1.1 briggs
93 1.1 briggs static void
94 1.54 matt adbattach(device_t parent, device_t self, void *aux)
95 1.1 briggs {
96 1.50 tsutsui
97 1.61 nat switch (mac68k_machine.machineid) {
98 1.61 nat case MACH_MACPB140:
99 1.61 nat case MACH_MACPB145:
100 1.61 nat case MACH_MACPB160:
101 1.61 nat case MACH_MACPB165:
102 1.61 nat case MACH_MACPB165C:
103 1.61 nat case MACH_MACPB170:
104 1.61 nat case MACH_MACPB180:
105 1.61 nat case MACH_MACPB180C:
106 1.61 nat case MACH_MACPB150:
107 1.61 nat case MACH_MACPB210:
108 1.61 nat case MACH_MACPB230:
109 1.61 nat case MACH_MACPB250:
110 1.61 nat case MACH_MACPB270:
111 1.61 nat case MACH_MACPB280:
112 1.61 nat case MACH_MACPB280C:
113 1.61 nat /* Reqired on machines with in-built trackballs. */
114 1.61 nat printf("... Waiting 5 seconds for adb devices to "
115 1.61 nat "settle.");
116 1.61 nat kpause("adb-slp", false, mstohz(5000), NULL);
117 1.61 nat break;
118 1.61 nat default:
119 1.61 nat break;
120 1.61 nat }
121 1.60 nat
122 1.51 ad adb_softintr_cookie = softint_establish(SOFTINT_SERIAL,
123 1.50 tsutsui (void (*)(void *))adb_soft_intr, NULL);
124 1.29 scottr printf("\n");
125 1.29 scottr
126 1.29 scottr /*
127 1.29 scottr * Defer configuration until interrupts are enabled.
128 1.29 scottr */
129 1.29 scottr config_interrupts(self, adb_config_interrupts);
130 1.29 scottr }
131 1.29 scottr
132 1.29 scottr void
133 1.54 matt adb_config_interrupts(device_t self)
134 1.29 scottr {
135 1.25 ender ADBDataBlock adbdata;
136 1.25 ender struct adb_attach_args aa_args;
137 1.25 ender int totaladbs;
138 1.25 ender int adbindex, adbaddr;
139 1.25 ender
140 1.55 chs printf("%s", device_xname(self));
141 1.31 scottr adb_polling = 1;
142 1.29 scottr
143 1.25 ender #ifdef MRG_ADB
144 1.25 ender if (!mrg_romready()) {
145 1.29 scottr printf(": no ROM ADB driver in this kernel for this machine\n");
146 1.25 ender return;
147 1.25 ender }
148 1.1 briggs
149 1.25 ender #ifdef ADB_DEBUG
150 1.25 ender if (adb_debug)
151 1.25 ender printf("adb: call mrg_initadbintr\n");
152 1.25 ender #endif
153 1.1 briggs
154 1.25 ender mrg_initadbintr(); /* Mac ROM Glue okay to do ROM intr */
155 1.25 ender #ifdef ADB_DEBUG
156 1.25 ender if (adb_debug)
157 1.25 ender printf("adb: returned from mrg_initadbintr\n");
158 1.25 ender #endif
159 1.10 scottr
160 1.25 ender /* ADBReInit pre/post-processing */
161 1.25 ender JADBProc = adb_jadbproc;
162 1.1 briggs
163 1.25 ender /* Initialize ADB */
164 1.25 ender #ifdef ADB_DEBUG
165 1.25 ender if (adb_debug)
166 1.25 ender printf("adb: calling ADBAlternateInit.\n");
167 1.10 scottr #endif
168 1.1 briggs
169 1.28 scottr printf(" (mrg)");
170 1.25 ender ADBAlternateInit();
171 1.25 ender #else
172 1.25 ender ADBReInit();
173 1.25 ender printf(" (direct, %s)", adbHardwareDescr[adbHardware]);
174 1.25 ender #endif /* MRG_ADB */
175 1.25 ender
176 1.25 ender #ifdef ADB_DEBUG
177 1.25 ender if (adb_debug)
178 1.25 ender printf("adb: done with ADBReInit\n");
179 1.25 ender #endif
180 1.1 briggs
181 1.25 ender totaladbs = CountADBs();
182 1.1 briggs
183 1.33 scottr printf(": %d target%s\n", totaladbs, (totaladbs == 1) ? "" : "s");
184 1.1 briggs
185 1.25 ender #if NAED > 0
186 1.25 ender /* ADB event device for compatibility */
187 1.25 ender aa_args.origaddr = 0;
188 1.25 ender aa_args.adbaddr = 0;
189 1.25 ender aa_args.handler_id = 0;
190 1.59 thorpej (void)config_found(self, &aa_args, adbprint, CFARGS_NONE);
191 1.25 ender #endif
192 1.1 briggs
193 1.25 ender /* for each ADB device */
194 1.25 ender for (adbindex = 1; adbindex <= totaladbs; adbindex++) {
195 1.25 ender /* Get the ADB information */
196 1.25 ender adbaddr = GetIndADB(&adbdata, adbindex);
197 1.1 briggs
198 1.27 ender aa_args.origaddr = (int)(adbdata.origADBAddr);
199 1.25 ender aa_args.adbaddr = adbaddr;
200 1.27 ender aa_args.handler_id = (int)(adbdata.devType);
201 1.2 briggs
202 1.59 thorpej (void)config_found(self, &aa_args, adbprint, CFARGS_NONE);
203 1.1 briggs }
204 1.30 scottr adb_polling = 0;
205 1.1 briggs }
206 1.1 briggs
207 1.1 briggs
208 1.25 ender int
209 1.46 chs adbprint(void *args, const char *name)
210 1.1 briggs {
211 1.25 ender struct adb_attach_args *aa_args = (struct adb_attach_args *)args;
212 1.25 ender int rv = UNCONF;
213 1.1 briggs
214 1.25 ender if (name) { /* no configured device matched */
215 1.25 ender rv = UNSUPP; /* most ADB device types are unsupported */
216 1.2 briggs
217 1.25 ender /* print out what kind of ADB device we have found */
218 1.44 thorpej aprint_normal("%s addr %d: ", name, aa_args->adbaddr);
219 1.25 ender switch(aa_args->origaddr) {
220 1.57 martin case 0:
221 1.25 ender #ifdef DIAGNOSTIC
222 1.44 thorpej aprint_normal("ADB event device");
223 1.57 martin #endif
224 1.25 ender rv = UNCONF;
225 1.25 ender break;
226 1.57 martin #ifdef DIAGNOSTIC
227 1.25 ender case ADBADDR_SECURE:
228 1.44 thorpej aprint_normal("security dongle (%d)",
229 1.44 thorpej aa_args->handler_id);
230 1.25 ender break;
231 1.22 scottr #endif
232 1.25 ender case ADBADDR_MAP:
233 1.44 thorpej aprint_normal("mapped device (%d)",
234 1.44 thorpej aa_args->handler_id);
235 1.25 ender rv = UNCONF;
236 1.25 ender break;
237 1.25 ender case ADBADDR_REL:
238 1.44 thorpej aprint_normal("relative positioning device (%d)",
239 1.25 ender aa_args->handler_id);
240 1.25 ender rv = UNCONF;
241 1.25 ender break;
242 1.25 ender #ifdef DIAGNOSTIC
243 1.25 ender case ADBADDR_ABS:
244 1.25 ender switch (aa_args->handler_id) {
245 1.25 ender case ADB_ARTPAD:
246 1.44 thorpej aprint_normal("WACOM ArtPad II");
247 1.25 ender break;
248 1.25 ender default:
249 1.44 thorpej aprint_normal("absolute positioning device (%d)",
250 1.25 ender aa_args->handler_id);
251 1.25 ender break;
252 1.25 ender }
253 1.15 scottr break;
254 1.25 ender case ADBADDR_DATATX:
255 1.44 thorpej aprint_normal("data transfer device (modem?) (%d)",
256 1.25 ender aa_args->handler_id);
257 1.25 ender break;
258 1.25 ender case ADBADDR_MISC:
259 1.25 ender switch (aa_args->handler_id) {
260 1.25 ender case ADB_POWERKEY:
261 1.44 thorpej aprint_normal("Sophisticated Circuits PowerKey");
262 1.25 ender break;
263 1.25 ender default:
264 1.44 thorpej aprint_normal("misc. device (remote control?) (%d)",
265 1.25 ender aa_args->handler_id);
266 1.25 ender break;
267 1.25 ender }
268 1.8 briggs break;
269 1.8 briggs default:
270 1.44 thorpej aprint_normal("unknown type device, (handler %d)",
271 1.25 ender aa_args->handler_id);
272 1.8 briggs break;
273 1.25 ender #endif /* DIAGNOSTIC */
274 1.4 briggs }
275 1.25 ender } else /* a device matched and was configured */
276 1.44 thorpej aprint_normal(" addr %d: ", aa_args->adbaddr);
277 1.2 briggs
278 1.25 ender return (rv);
279 1.37 scottr }
280 1.37 scottr
281 1.37 scottr
282 1.37 scottr /*
283 1.37 scottr * adb_op_sync
284 1.37 scottr *
285 1.37 scottr * This routine does exactly what the adb_op routine does, except that after
286 1.37 scottr * the adb_op is called, it waits until the return value is present before
287 1.37 scottr * returning.
288 1.37 scottr *
289 1.37 scottr * NOTE: The user specified compRout is ignored, since this routine specifies
290 1.56 snj * its own to adb_op, which is why you really called this in the first place
291 1.37 scottr * anyway.
292 1.37 scottr */
293 1.37 scottr int
294 1.37 scottr adb_op_sync(Ptr buffer, Ptr compRout, Ptr data, short command)
295 1.37 scottr {
296 1.37 scottr int result;
297 1.37 scottr volatile int flag = 0;
298 1.37 scottr
299 1.47 jmc result = ADBOp(buffer, (void *)adb_op_comprout, __UNVOLATILE(&flag),
300 1.47 jmc command); /* send command */
301 1.37 scottr if (result == 0) { /* send ok? */
302 1.52 scottr adb_spin(&flag);
303 1.37 scottr if (!flag)
304 1.37 scottr result = -2;
305 1.37 scottr }
306 1.37 scottr
307 1.37 scottr return result;
308 1.37 scottr }
309 1.37 scottr
310 1.52 scottr /*
311 1.52 scottr * adb_spin
312 1.52 scottr *
313 1.52 scottr * Implements a spin-wait with timeout to be used for synchronous
314 1.52 scottr * operations on the ADB bus.
315 1.52 scottr *
316 1.52 scottr * Total time to wait is calculated as follows:
317 1.52 scottr * - Tlt (stop to start time): 260 usec
318 1.52 scottr * - start bit: 100 usec
319 1.52 scottr * - up to 8 data bytes: 64 * 100 usec = 6400 usec
320 1.52 scottr * - stop bit (with SRQ): 140 usec
321 1.52 scottr * Total: 6900 usec
322 1.52 scottr *
323 1.52 scottr * This is the total time allowed by the specification. Any device that
324 1.52 scottr * doesn't conform to this will fail to operate properly on some Apple
325 1.52 scottr * systems. In spite of this we double the time to wait; Cuda-based
326 1.52 scottr * systems apparently queue commands and allow the main CPU to continue
327 1.52 scottr * processing (how radical!). To be safe, allow time for two complete
328 1.52 scottr * ADB transactions to occur.
329 1.52 scottr */
330 1.52 scottr void
331 1.52 scottr adb_spin(volatile int *fp)
332 1.52 scottr {
333 1.52 scottr int tmout;
334 1.52 scottr
335 1.52 scottr for (tmout = 13800; *fp == 0 && tmout >= 10; tmout -= 10)
336 1.52 scottr delay(10);
337 1.52 scottr if (*fp == 0 && tmout > 0)
338 1.52 scottr delay(tmout);
339 1.52 scottr }
340 1.52 scottr
341 1.37 scottr
342 1.37 scottr /*
343 1.37 scottr * adb_op_comprout
344 1.37 scottr *
345 1.37 scottr * This function is used by the adb_op_sync routine so it knows when the
346 1.37 scottr * function is done.
347 1.37 scottr */
348 1.37 scottr void
349 1.37 scottr adb_op_comprout(void)
350 1.37 scottr {
351 1.49 perry __asm("movw #1,%a2@ | update flag value");
352 1.1 briggs }
353