adb.c revision 1.57 1 1.57 martin /* $NetBSD: adb.c,v 1.57 2018/10/19 18:21:55 martin 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.57 martin __KERNEL_RCSID(0, "$NetBSD: adb.c,v 1.57 2018/10/19 18:21:55 martin 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.51 ad adb_softintr_cookie = softint_establish(SOFTINT_SERIAL,
98 1.50 tsutsui (void (*)(void *))adb_soft_intr, NULL);
99 1.29 scottr printf("\n");
100 1.29 scottr
101 1.29 scottr /*
102 1.29 scottr * Defer configuration until interrupts are enabled.
103 1.29 scottr */
104 1.29 scottr config_interrupts(self, adb_config_interrupts);
105 1.29 scottr }
106 1.29 scottr
107 1.29 scottr void
108 1.54 matt adb_config_interrupts(device_t self)
109 1.29 scottr {
110 1.25 ender ADBDataBlock adbdata;
111 1.25 ender struct adb_attach_args aa_args;
112 1.25 ender int totaladbs;
113 1.25 ender int adbindex, adbaddr;
114 1.25 ender
115 1.55 chs printf("%s", device_xname(self));
116 1.31 scottr adb_polling = 1;
117 1.29 scottr
118 1.25 ender #ifdef MRG_ADB
119 1.25 ender if (!mrg_romready()) {
120 1.29 scottr printf(": no ROM ADB driver in this kernel for this machine\n");
121 1.25 ender return;
122 1.25 ender }
123 1.1 briggs
124 1.25 ender #ifdef ADB_DEBUG
125 1.25 ender if (adb_debug)
126 1.25 ender printf("adb: call mrg_initadbintr\n");
127 1.25 ender #endif
128 1.1 briggs
129 1.25 ender mrg_initadbintr(); /* Mac ROM Glue okay to do ROM intr */
130 1.25 ender #ifdef ADB_DEBUG
131 1.25 ender if (adb_debug)
132 1.25 ender printf("adb: returned from mrg_initadbintr\n");
133 1.25 ender #endif
134 1.10 scottr
135 1.25 ender /* ADBReInit pre/post-processing */
136 1.25 ender JADBProc = adb_jadbproc;
137 1.1 briggs
138 1.25 ender /* Initialize ADB */
139 1.25 ender #ifdef ADB_DEBUG
140 1.25 ender if (adb_debug)
141 1.25 ender printf("adb: calling ADBAlternateInit.\n");
142 1.10 scottr #endif
143 1.1 briggs
144 1.28 scottr printf(" (mrg)");
145 1.25 ender ADBAlternateInit();
146 1.25 ender #else
147 1.25 ender ADBReInit();
148 1.25 ender printf(" (direct, %s)", adbHardwareDescr[adbHardware]);
149 1.25 ender #endif /* MRG_ADB */
150 1.25 ender
151 1.25 ender #ifdef ADB_DEBUG
152 1.25 ender if (adb_debug)
153 1.25 ender printf("adb: done with ADBReInit\n");
154 1.25 ender #endif
155 1.1 briggs
156 1.25 ender totaladbs = CountADBs();
157 1.1 briggs
158 1.33 scottr printf(": %d target%s\n", totaladbs, (totaladbs == 1) ? "" : "s");
159 1.1 briggs
160 1.25 ender #if NAED > 0
161 1.25 ender /* ADB event device for compatibility */
162 1.25 ender aa_args.origaddr = 0;
163 1.25 ender aa_args.adbaddr = 0;
164 1.25 ender aa_args.handler_id = 0;
165 1.29 scottr (void)config_found(self, &aa_args, adbprint);
166 1.25 ender #endif
167 1.1 briggs
168 1.25 ender /* for each ADB device */
169 1.25 ender for (adbindex = 1; adbindex <= totaladbs; adbindex++) {
170 1.25 ender /* Get the ADB information */
171 1.25 ender adbaddr = GetIndADB(&adbdata, adbindex);
172 1.1 briggs
173 1.27 ender aa_args.origaddr = (int)(adbdata.origADBAddr);
174 1.25 ender aa_args.adbaddr = adbaddr;
175 1.27 ender aa_args.handler_id = (int)(adbdata.devType);
176 1.2 briggs
177 1.29 scottr (void)config_found(self, &aa_args, adbprint);
178 1.1 briggs }
179 1.30 scottr adb_polling = 0;
180 1.1 briggs }
181 1.1 briggs
182 1.1 briggs
183 1.25 ender int
184 1.46 chs adbprint(void *args, const char *name)
185 1.1 briggs {
186 1.25 ender struct adb_attach_args *aa_args = (struct adb_attach_args *)args;
187 1.25 ender int rv = UNCONF;
188 1.1 briggs
189 1.25 ender if (name) { /* no configured device matched */
190 1.25 ender rv = UNSUPP; /* most ADB device types are unsupported */
191 1.2 briggs
192 1.25 ender /* print out what kind of ADB device we have found */
193 1.44 thorpej aprint_normal("%s addr %d: ", name, aa_args->adbaddr);
194 1.25 ender switch(aa_args->origaddr) {
195 1.57 martin case 0:
196 1.25 ender #ifdef DIAGNOSTIC
197 1.44 thorpej aprint_normal("ADB event device");
198 1.57 martin #endif
199 1.25 ender rv = UNCONF;
200 1.25 ender break;
201 1.57 martin #ifdef DIAGNOSTIC
202 1.25 ender case ADBADDR_SECURE:
203 1.44 thorpej aprint_normal("security dongle (%d)",
204 1.44 thorpej aa_args->handler_id);
205 1.25 ender break;
206 1.22 scottr #endif
207 1.25 ender case ADBADDR_MAP:
208 1.44 thorpej aprint_normal("mapped device (%d)",
209 1.44 thorpej aa_args->handler_id);
210 1.25 ender rv = UNCONF;
211 1.25 ender break;
212 1.25 ender case ADBADDR_REL:
213 1.44 thorpej aprint_normal("relative positioning device (%d)",
214 1.25 ender aa_args->handler_id);
215 1.25 ender rv = UNCONF;
216 1.25 ender break;
217 1.25 ender #ifdef DIAGNOSTIC
218 1.25 ender case ADBADDR_ABS:
219 1.25 ender switch (aa_args->handler_id) {
220 1.25 ender case ADB_ARTPAD:
221 1.44 thorpej aprint_normal("WACOM ArtPad II");
222 1.25 ender break;
223 1.25 ender default:
224 1.44 thorpej aprint_normal("absolute positioning device (%d)",
225 1.25 ender aa_args->handler_id);
226 1.25 ender break;
227 1.25 ender }
228 1.15 scottr break;
229 1.25 ender case ADBADDR_DATATX:
230 1.44 thorpej aprint_normal("data transfer device (modem?) (%d)",
231 1.25 ender aa_args->handler_id);
232 1.25 ender break;
233 1.25 ender case ADBADDR_MISC:
234 1.25 ender switch (aa_args->handler_id) {
235 1.25 ender case ADB_POWERKEY:
236 1.44 thorpej aprint_normal("Sophisticated Circuits PowerKey");
237 1.25 ender break;
238 1.25 ender default:
239 1.44 thorpej aprint_normal("misc. device (remote control?) (%d)",
240 1.25 ender aa_args->handler_id);
241 1.25 ender break;
242 1.25 ender }
243 1.8 briggs break;
244 1.8 briggs default:
245 1.44 thorpej aprint_normal("unknown type device, (handler %d)",
246 1.25 ender aa_args->handler_id);
247 1.8 briggs break;
248 1.25 ender #endif /* DIAGNOSTIC */
249 1.4 briggs }
250 1.25 ender } else /* a device matched and was configured */
251 1.44 thorpej aprint_normal(" addr %d: ", aa_args->adbaddr);
252 1.2 briggs
253 1.25 ender return (rv);
254 1.37 scottr }
255 1.37 scottr
256 1.37 scottr
257 1.37 scottr /*
258 1.37 scottr * adb_op_sync
259 1.37 scottr *
260 1.37 scottr * This routine does exactly what the adb_op routine does, except that after
261 1.37 scottr * the adb_op is called, it waits until the return value is present before
262 1.37 scottr * returning.
263 1.37 scottr *
264 1.37 scottr * NOTE: The user specified compRout is ignored, since this routine specifies
265 1.56 snj * its own to adb_op, which is why you really called this in the first place
266 1.37 scottr * anyway.
267 1.37 scottr */
268 1.37 scottr int
269 1.37 scottr adb_op_sync(Ptr buffer, Ptr compRout, Ptr data, short command)
270 1.37 scottr {
271 1.37 scottr int result;
272 1.37 scottr volatile int flag = 0;
273 1.37 scottr
274 1.47 jmc result = ADBOp(buffer, (void *)adb_op_comprout, __UNVOLATILE(&flag),
275 1.47 jmc command); /* send command */
276 1.37 scottr if (result == 0) { /* send ok? */
277 1.52 scottr adb_spin(&flag);
278 1.37 scottr if (!flag)
279 1.37 scottr result = -2;
280 1.37 scottr }
281 1.37 scottr
282 1.37 scottr return result;
283 1.37 scottr }
284 1.37 scottr
285 1.52 scottr /*
286 1.52 scottr * adb_spin
287 1.52 scottr *
288 1.52 scottr * Implements a spin-wait with timeout to be used for synchronous
289 1.52 scottr * operations on the ADB bus.
290 1.52 scottr *
291 1.52 scottr * Total time to wait is calculated as follows:
292 1.52 scottr * - Tlt (stop to start time): 260 usec
293 1.52 scottr * - start bit: 100 usec
294 1.52 scottr * - up to 8 data bytes: 64 * 100 usec = 6400 usec
295 1.52 scottr * - stop bit (with SRQ): 140 usec
296 1.52 scottr * Total: 6900 usec
297 1.52 scottr *
298 1.52 scottr * This is the total time allowed by the specification. Any device that
299 1.52 scottr * doesn't conform to this will fail to operate properly on some Apple
300 1.52 scottr * systems. In spite of this we double the time to wait; Cuda-based
301 1.52 scottr * systems apparently queue commands and allow the main CPU to continue
302 1.52 scottr * processing (how radical!). To be safe, allow time for two complete
303 1.52 scottr * ADB transactions to occur.
304 1.52 scottr */
305 1.52 scottr void
306 1.52 scottr adb_spin(volatile int *fp)
307 1.52 scottr {
308 1.52 scottr int tmout;
309 1.52 scottr
310 1.52 scottr for (tmout = 13800; *fp == 0 && tmout >= 10; tmout -= 10)
311 1.52 scottr delay(10);
312 1.52 scottr if (*fp == 0 && tmout > 0)
313 1.52 scottr delay(tmout);
314 1.52 scottr }
315 1.52 scottr
316 1.37 scottr
317 1.37 scottr /*
318 1.37 scottr * adb_op_comprout
319 1.37 scottr *
320 1.37 scottr * This function is used by the adb_op_sync routine so it knows when the
321 1.37 scottr * function is done.
322 1.37 scottr */
323 1.37 scottr void
324 1.37 scottr adb_op_comprout(void)
325 1.37 scottr {
326 1.49 perry __asm("movw #1,%a2@ | update flag value");
327 1.1 briggs }
328