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