Home | History | Annotate | Line # | Download | only in oea
ofw_consinit.c revision 1.20
      1 /* $NetBSD: ofw_consinit.c,v 1.20 2021/02/19 05:21:39 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Tim Rightnour
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: ofw_consinit.c,v 1.20 2021/02/19 05:21:39 thorpej Exp $");
     34 
     35 #include "adb.h"
     36 #include "adbkbd.h"
     37 #include "akbd.h"
     38 #include "isa.h"
     39 #include "ofb.h"
     40 #include "pckbc.h"
     41 #include "ukbd.h"
     42 #include "wsdisplay.h"
     43 #include "zsc.h"
     44 #include "zstty.h"
     45 
     46 #include <sys/param.h>
     47 #include <sys/buf.h>
     48 #include <sys/tty.h>
     49 
     50 #include <prop/proplib.h>
     51 
     52 #include <machine/autoconf.h>
     53 #include <machine/trap.h>
     54 #include <sys/bus.h>
     55 
     56 #include <powerpc/ofw_cons.h>
     57 #include <powerpc/ofw_machdep.h>
     58 
     59 #include <dev/cons.h>
     60 #include <dev/ofw/openfirm.h>
     61 
     62 #include <dev/wscons/wsksymvar.h>
     63 #include <dev/wscons/wscons_callbacks.h>
     64 
     65 #if NZSC > 0
     66 #include <machine/z8530var.h>
     67 #endif
     68 
     69 #if (NADB > 0)
     70 #include <macppc/dev/adbvar.h>
     71 #endif
     72 
     73 #if (NUKBD > 0)
     74 #include <dev/usb/ukbdvar.h>
     75 struct usb_kbd_ihandles {
     76 	struct usb_kbd_ihandles *next;
     77 	int ihandle;
     78 };
     79 #endif
     80 
     81 #if (NZSTTY > 0)
     82 #include <dev/ic/z8530reg.h>
     83 extern struct consdev consdev_zs;
     84 #endif
     85 
     86 #if (NPCKBC > 0)
     87 #include <dev/isa/isareg.h>
     88 #include <dev/ic/i8042reg.h>
     89 #include <dev/ic/pckbcvar.h>
     90 #endif
     91 
     92 extern int console_node, console_instance;
     93 
     94 int ofkbd_ihandle;
     95 
     96 static void cninit_kd(void);
     97 
     98 /*#define OFDEBUG*/
     99 
    100 #ifdef OFDEBUG
    101 void ofprint(const char *, ...);
    102 
    103 void ofprint(const char *blah, ...)
    104 {
    105 	va_list va;
    106 	char buf[256];
    107 	int len;
    108 
    109 	va_start(va, blah);
    110 	len = vsnprintf(buf, sizeof(buf), blah, va);
    111 	va_end(va);
    112 	OF_write(console_instance, buf, len);
    113 }
    114 
    115 #define OFPRINTF ofprint
    116 #else
    117 #define OFPRINTF while(0) printf
    118 #endif
    119 
    120 void
    121 cninit(void)
    122 {
    123 	char name[32];
    124 
    125 	OFPRINTF("console node: %08x\n", console_node);
    126 
    127 	if (console_node <= 0)
    128 		goto nocons;
    129 
    130 	memset(name, 0, sizeof(name));
    131 	if (OF_getprop(console_node, "device_type", name, sizeof(name)) == -1)
    132 		goto nocons;
    133 
    134 	OFPRINTF("console type: %s\n", name);
    135 
    136 	if (strcmp(name, "serial") == 0) {
    137 		struct consdev *cp;
    138 
    139 #ifdef PMAC_G5
    140 		/* The MMU hasn't been initialized yet, use failsafe for now */
    141 		extern struct consdev failsafe_cons;
    142 		cp = &failsafe_cons;
    143 		cn_tab = cp;
    144 		(*cp->cn_probe)(cp);
    145 		(*cp->cn_init)(cp);
    146 		aprint_verbose("Early G5 console initialized\n");
    147 		return;
    148 #endif /* PMAC_G5 */
    149 
    150 #if (NZSTTY > 0) && !defined(MAMBO)
    151 		OF_getprop(console_node, "name", name, sizeof(name));
    152 		if (strcmp(name, "ch-a") == 0 || strcmp(name, "ch-b") == 0) {
    153 			cp = &consdev_zs;
    154 			(*cp->cn_probe)(cp);
    155 			(*cp->cn_init)(cp);
    156 			cn_tab = cp;
    157 		}
    158 		return;
    159 #endif /* NZTTY */
    160 
    161 		/* fallback to OFW boot console (already set) */
    162 		return;
    163 	}
    164 	else
    165 		cninit_kd();
    166 nocons:
    167 	return;
    168 }
    169 
    170 
    171 static void
    172 cninit_kd(void)
    173 {
    174 	int kstdin, node;
    175 	char name[16];
    176 #if (NAKBD > 0) || (NADBKBD > 0)
    177 	int akbd;
    178 #endif
    179 #if NUKBD > 0
    180 	struct usb_kbd_ihandles *ukbds;
    181 	int ukbd;
    182 #endif
    183 
    184 	/*
    185 	 * Attach the console output now (so we can see debugging messages,
    186 	 * if any).
    187 	 */
    188 #if NWSDISPLAY > 0
    189 	rascons_cnattach();
    190 #endif
    191 
    192 	/*
    193 	 * We must determine which keyboard type we have.
    194 	 */
    195 	if (OF_getprop(ofw_chosen, "stdin", &kstdin, sizeof(kstdin))
    196 	    != sizeof(kstdin)) {
    197 		printf("WARNING: no `stdin' property in /chosen\n");
    198 		return;
    199 	}
    200 
    201 	node = OF_instance_to_package(kstdin);
    202 	memset(name, 0, sizeof(name));
    203 	OF_getprop(node, "name", name, sizeof(name));
    204 	if (strcmp(name, "keyboard") != 0) {
    205 		printf("WARNING: stdin is not a keyboard: %s\n", name);
    206 		return;
    207 	}
    208 
    209 	memset(name, 0, sizeof(name));
    210 	OF_getprop(OF_parent(node), "name", name, sizeof(name));
    211 #if NAKBD > 0
    212 	if (strcmp(name, "adb") == 0) {
    213 		printf("console keyboard type: ADB\n");
    214 		akbd_cnattach();
    215 		goto kbd_found;
    216 	}
    217 #endif
    218 #if NADBKBD > 0
    219 	if (strcmp(name, "adb") == 0) {
    220 		printf("console keyboard type: ADB\n");
    221 		adbkbd_cnattach();
    222 		goto kbd_found;
    223 	}
    224 #endif
    225 #if NPCKBC > 0
    226 	if (strcmp(name, "isa") == 0) {
    227 		printf("console keyboard type: PC Keyboard\n");
    228 		pckbc_cnattach(&genppc_isa_io_space_tag, IO_KBD, KBCMDP,
    229 		    PCKBC_KBD_SLOT, 0);
    230 		goto kbd_found;
    231 	}
    232 #endif
    233 
    234 	/*
    235 	 * It is not obviously an ADB/PC keyboard. Could be USB,
    236 	 * or ADB on some firmware versions (e.g.: iBook G4)
    237 	 * This is not enough, we have a few more problems:
    238 	 *
    239 	 *	(1) The stupid Macintosh firmware uses a
    240 	 *	    `psuedo-hid' (no typo) or `pseudo-hid',
    241 	 *	    which apparently merges all keyboards
    242 	 *	    input into a single input stream.
    243 	 *	    Because of this, we can't actually
    244 	 *	    determine which controller or keyboard
    245 	 *	    is really the console keyboard!
    246 	 *
    247 	 *	(2) Even if we could, the keyboard can be USB,
    248 	 *	    and this requires a lot of the kernel to
    249 	 *	    be running in order for it to work.
    250 	 *
    251 	 *      (3) If the keyboard is behind isa, we don't have enough
    252 	 * 	    kernel setup to use it yet, so punt to the ofroutines.
    253 	 *
    254 	 * So, what we do is this:
    255 	 *
    256 	 *	(1) First check for OpenFirmware implementation
    257 	 *	    that will not let us distinguish between
    258 	 *	    USB and ADB. In that situation, try attaching
    259 	 *	    anything as we can, and hope things get better
    260 	 *	    at autoconfiguration time.
    261 	 *
    262 	 *	(2) Assume the keyboard is USB.
    263 	 *	    Tell the ukbd driver that it is the console.
    264 	 *	    At autoconfiguration time, it will attach the
    265 	 *	    first USB keyboard instance as the console
    266 	 *	    keyboard.
    267 	 *
    268 	 *	(3) Until then, so that we have _something_, we
    269 	 *	    use the OpenFirmware I/O facilities to read
    270 	 *	    the keyboard.
    271 	 */
    272 
    273 	/*
    274 	 * stdin is /pseudo-hid/keyboard.  There is no
    275 	 * `adb-kbd-ihandle or `usb-kbd-ihandles methods
    276 	 * available. Try attaching as ADB.
    277 	 * But only if ADB support is actually present.
    278 	 *
    279 	 * XXX This must be called before pmap_bootstrap().
    280 	 */
    281 	if (strcmp(name, "pseudo-hid") == 0) {
    282 		int adb_node;
    283 
    284 		adb_node = OF_finddevice("/pci/mac-io/via-pmu/adb");
    285 		if (adb_node > 0) {
    286 			printf("ADB support found\n");
    287 #if NAKBD > 0
    288 			akbd_cnattach();
    289 #endif
    290 #if NADBKBD > 0
    291 			adbkbd_cnattach();
    292 #endif
    293 		} else {
    294 			/* must be USB */
    295 			printf("No ADB support present, assuming USB "
    296 			       "keyboard\n");
    297 #if NUKBD > 0
    298 			ukbd_cnattach();
    299 #endif
    300 		}
    301 		goto kbd_found;
    302 	}
    303 
    304 	/*
    305 	 * stdin is /psuedo-hid/keyboard.  Test `adb-kbd-ihandle and
    306 	 * `usb-kbd-ihandles to figure out the real keyboard(s).
    307 	 *
    308 	 * XXX This must be called before pmap_bootstrap().
    309 	 */
    310 
    311 #if NUKBD > 0
    312 	if (OF_call_method("`usb-kbd-ihandles", kstdin, 0, 1, &ukbds) >= 0 &&
    313 	    ukbds != NULL && ukbds->ihandle != 0 &&
    314 	    OF_instance_to_package(ukbds->ihandle) != -1) {
    315 		printf("usb-kbd-ihandles matches\n");
    316 		printf("console keyboard type: USB\n");
    317 		ukbd_cnattach();
    318 		goto kbd_found;
    319 	}
    320 	/* Try old method name. */
    321 	if (OF_call_method("`usb-kbd-ihandle", kstdin, 0, 1, &ukbd) >= 0 &&
    322 	    ukbd != 0 &&
    323 	    OF_instance_to_package(ukbd) != -1) {
    324 		printf("usb-kbd-ihandle matches\n");
    325 		printf("console keyboard type: USB\n");
    326 		kstdin = ukbd;
    327 		ukbd_cnattach();
    328 		goto kbd_found;
    329 	}
    330 #endif
    331 
    332 #if (NAKBD > 0) || (NADBKBD > 0)
    333 	if (OF_call_method("`adb-kbd-ihandle", kstdin, 0, 1, &akbd) >= 0 &&
    334 	    akbd != 0 &&
    335 	    OF_instance_to_package(akbd) != -1) {
    336 		printf("adb-kbd-ihandle matches\n");
    337 		printf("console keyboard type: ADB\n");
    338 		kstdin = akbd;
    339 #if NAKBD > 0
    340 		akbd_cnattach();
    341 #endif
    342 #if NADBKBD > 0
    343 		adbkbd_cnattach();
    344 #endif
    345 		goto kbd_found;
    346 	}
    347 #endif
    348 
    349 #if NUKBD > 0
    350 	/*
    351 	 * XXX Old firmware does not have `usb-kbd-ihandles method.  Assume
    352 	 * XXX USB keyboard anyway.
    353 	 */
    354 	printf("defaulting to USB...");
    355 	printf("console keyboard type: USB\n");
    356 	ukbd_cnattach();
    357 	goto kbd_found;
    358 #endif
    359 
    360 	/*
    361 	 * No keyboard is found.  Just return.
    362 	 */
    363 	printf("no console keyboard\n");
    364 	return;
    365 
    366 kbd_found:;
    367 #if NAKBD + NUKBD + NADBKBD + NPCKBC > 0
    368 	/*
    369 	 * XXX This is a little gross, but we don't get to call
    370 	 * XXX wskbd_cnattach() twice.
    371 	 */
    372 	ofkbd_ihandle = kstdin;
    373 #if NWSDISPLAY > 0
    374 	wsdisplay_set_cons_kbd(ofkbd_cngetc, NULL, NULL);
    375 #endif
    376 #endif
    377 }
    378 
    379 /*
    380  * Bootstrap console keyboard routines, using OpenFirmware I/O.
    381  */
    382 int
    383 ofkbd_cngetc(dev_t dev)
    384 {
    385 	u_char c = '\0';
    386 	int len;
    387 
    388 	do {
    389 		len = OF_read(ofkbd_ihandle, &c, 1);
    390 	} while (len != 1);
    391 
    392 	return c;
    393 }
    394 
    395 void
    396 ofwoea_consinit(void)
    397 {
    398 	static int initted = 0;
    399 
    400 	if (initted)
    401 		return;
    402 
    403 	initted = 1;
    404 	cninit();
    405 }
    406