Home | History | Annotate | Line # | Download | only in oea
ofw_consinit.c revision 1.16
      1 /* $NetBSD: ofw_consinit.c,v 1.16 2013/05/12 13:23:08 macallan 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.16 2013/05/12 13:23:08 macallan Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/buf.h>
     37 #include <sys/tty.h>
     38 
     39 #include <prop/proplib.h>
     40 
     41 #include <machine/autoconf.h>
     42 #include <machine/trap.h>
     43 #include <sys/bus.h>
     44 
     45 #include <powerpc/ofw_cons.h>
     46 
     47 #include <dev/cons.h>
     48 #include <dev/ofw/openfirm.h>
     49 
     50 #include <dev/wscons/wsksymvar.h>
     51 #include <dev/wscons/wscons_callbacks.h>
     52 
     53 #include "akbd.h"
     54 #include "adbkbd.h"
     55 #include "wsdisplay.h"
     56 #include "ofb.h"
     57 #include "isa.h"
     58 
     59 #include "zsc.h"
     60 #if NZSC > 0
     61 #include <machine/z8530var.h>
     62 #endif
     63 
     64 #include "adb.h"
     65 #if (NADB > 0)
     66 #include <macppc/dev/adbvar.h>
     67 #endif
     68 
     69 #include "ukbd.h"
     70 #if (NUKBD > 0)
     71 #include <dev/usb/ukbdvar.h>
     72 struct usb_kbd_ihandles {
     73 	struct usb_kbd_ihandles *next;
     74 	int ihandle;
     75 };
     76 #endif
     77 
     78 #include "zstty.h"
     79 #if (NZSTTY > 0)
     80 #include <dev/ic/z8530reg.h>
     81 extern struct consdev consdev_zs;
     82 #endif
     83 
     84 #include "pckbc.h"
     85 #if (NPCKBC > 0)
     86 #include <dev/isa/isareg.h>
     87 #include <dev/ic/i8042reg.h>
     88 #include <dev/ic/pckbcvar.h>
     89 #endif
     90 
     91 int console_node = 0, console_instance = 0;
     92 
     93 int chosen, stdin, stdout;
     94 int ofkbd_ihandle;
     95 
     96 static void cninit_kd(void);
     97 static void ofwoea_bootstrap_console(void);
     98 static int ofwbootcons_cngetc(dev_t);
     99 static void ofwbootcons_cnputc(dev_t, int);
    100 
    101 /*#define OFDEBUG*/
    102 
    103 struct consdev consdev_ofwbootcons = {
    104 	NULL, NULL,
    105 	ofwbootcons_cngetc,
    106 	ofwbootcons_cnputc,
    107 	nullcnpollc,
    108 	NULL, NULL, NULL, NODEV, CN_INTERNAL,
    109 };
    110 
    111 #ifdef OFDEBUG
    112 void ofprint(const char *, ...);
    113 
    114 void ofprint(const char *blah, ...)
    115 {
    116 	va_list va;
    117 	char buf[256];
    118 	int len;
    119 
    120 	va_start(va, blah);
    121 	len = vsnprintf(buf, sizeof(buf), blah, va);
    122 	OF_write(console_instance, buf, len);
    123 }
    124 
    125 #define OFPRINTF ofprint
    126 #else
    127 #define OFPRINTF while(0) printf
    128 #endif
    129 
    130 void
    131 cninit(void)
    132 {
    133 	char name[32];
    134 
    135 	ofwoea_bootstrap_console();
    136 
    137 	OFPRINTF("console node: %08x\n", console_node);
    138 
    139 	if (console_node == -1)
    140 		goto nocons;
    141 
    142 	memset(name, 0, sizeof(name));
    143 	if (OF_getprop(console_node, "device_type", name, sizeof(name)) == -1)
    144 		goto nocons;
    145 
    146 	OFPRINTF("console type: %s\n", name);
    147 
    148 	if (strcmp(name, "serial") == 0) {
    149 		struct consdev *cp;
    150 
    151 #ifdef PMAC_G5
    152 		/* The MMU hasn't been initialized yet, use failsafe for now */
    153 		extern struct consdev failsafe_cons;
    154 		cp = &failsafe_cons;
    155 		cn_tab = cp;
    156 		(*cp->cn_probe)(cp);
    157 		(*cp->cn_init)(cp);
    158 		aprint_verbose("Early G5 console initialized\n");
    159 		return;
    160 #endif /* PMAC_G5 */
    161 
    162 #if (NZSTTY > 0) && !defined(MAMBO)
    163 		OF_getprop(console_node, "name", name, sizeof(name));
    164 		if (strcmp(name, "ch-a") == 0 || strcmp(name, "ch-b") == 0) {
    165 			cp = &consdev_zs;
    166 			(*cp->cn_probe)(cp);
    167 			(*cp->cn_init)(cp);
    168 			cn_tab = cp;
    169 		}
    170 		return;
    171 #endif /* NZTTY */
    172 
    173 		/* fallback to OFW boot console */
    174 		cp = &consdev_ofwbootcons;
    175 		cn_tab = cp;
    176 		return;
    177 	}
    178 	else
    179 		cninit_kd();
    180 nocons:
    181 	return;
    182 }
    183 
    184 
    185 static void
    186 cninit_kd(void)
    187 {
    188 	int kstdin, node;
    189 	char name[16];
    190 #if (NAKBD > 0) || (NADBKBD > 0)
    191 	int akbd;
    192 #endif
    193 #if NUKBD > 0
    194 	struct usb_kbd_ihandles *ukbds;
    195 	int ukbd;
    196 #endif
    197 
    198 	/*
    199 	 * Attach the console output now (so we can see debugging messages,
    200 	 * if any).
    201 	 */
    202 #if NWSDISPLAY > 0
    203 	rascons_cnattach();
    204 #endif
    205 
    206 	/*
    207 	 * We must determine which keyboard type we have.
    208 	 */
    209 	if (OF_getprop(chosen, "stdin", &kstdin, sizeof(kstdin))
    210 	    != sizeof(kstdin)) {
    211 		printf("WARNING: no `stdin' property in /chosen\n");
    212 		return;
    213 	}
    214 
    215 	node = OF_instance_to_package(kstdin);
    216 	memset(name, 0, sizeof(name));
    217 	OF_getprop(node, "name", name, sizeof(name));
    218 	if (strcmp(name, "keyboard") != 0) {
    219 		printf("WARNING: stdin is not a keyboard: %s\n", name);
    220 		return;
    221 	}
    222 
    223 	memset(name, 0, sizeof(name));
    224 	OF_getprop(OF_parent(node), "name", name, sizeof(name));
    225 #if NAKBD > 0
    226 	if (strcmp(name, "adb") == 0) {
    227 		printf("console keyboard type: ADB\n");
    228 		akbd_cnattach();
    229 		goto kbd_found;
    230 	}
    231 #endif
    232 #if NADBKBD > 0
    233 	if (strcmp(name, "adb") == 0) {
    234 		printf("console keyboard type: ADB\n");
    235 		adbkbd_cnattach();
    236 		goto kbd_found;
    237 	}
    238 #endif
    239 #if NPCKBC > 0
    240 	if (strcmp(name, "isa") == 0) {
    241 		printf("console keyboard type: PC Keyboard\n");
    242 		pckbc_cnattach(&genppc_isa_io_space_tag, IO_KBD, KBCMDP,
    243 		    PCKBC_KBD_SLOT, 0);
    244 		goto kbd_found;
    245 	}
    246 #endif
    247 
    248 	/*
    249 	 * It is not obviously an ADB/PC keyboard. Could be USB,
    250 	 * or ADB on some firmware versions (e.g.: iBook G4)
    251 	 * This is not enough, we have a few more problems:
    252 	 *
    253 	 *	(1) The stupid Macintosh firmware uses a
    254 	 *	    `psuedo-hid' (no typo) or `pseudo-hid',
    255 	 *	    which apparently merges all keyboards
    256 	 *	    input into a single input stream.
    257 	 *	    Because of this, we can't actually
    258 	 *	    determine which controller or keyboard
    259 	 *	    is really the console keyboard!
    260 	 *
    261 	 *	(2) Even if we could, the keyboard can be USB,
    262 	 *	    and this requires a lot of the kernel to
    263 	 *	    be running in order for it to work.
    264 	 *
    265 	 *      (3) If the keyboard is behind isa, we don't have enough
    266 	 * 	    kernel setup to use it yet, so punt to the ofroutines.
    267 	 *
    268 	 * So, what we do is this:
    269 	 *
    270 	 *	(1) First check for OpenFirmware implementation
    271 	 *	    that will not let us distinguish between
    272 	 *	    USB and ADB. In that situation, try attaching
    273 	 *	    anything as we can, and hope things get better
    274 	 *	    at autoconfiguration time.
    275 	 *
    276 	 *	(2) Assume the keyboard is USB.
    277 	 *	    Tell the ukbd driver that it is the console.
    278 	 *	    At autoconfiguration time, it will attach the
    279 	 *	    first USB keyboard instance as the console
    280 	 *	    keyboard.
    281 	 *
    282 	 *	(3) Until then, so that we have _something_, we
    283 	 *	    use the OpenFirmware I/O facilities to read
    284 	 *	    the keyboard.
    285 	 */
    286 
    287 	/*
    288 	 * stdin is /pseudo-hid/keyboard.  There is no
    289 	 * `adb-kbd-ihandle or `usb-kbd-ihandles methods
    290 	 * available. Try attaching as ADB.
    291 	 * But only if ADB support is actually present.
    292 	 *
    293 	 * XXX This must be called before pmap_bootstrap().
    294 	 */
    295 	if (strcmp(name, "pseudo-hid") == 0) {
    296 		int adb_node;
    297 
    298 		adb_node = OF_finddevice("/pci/mac-io/via-pmu/adb");
    299 		if (adb_node > 0) {
    300 			printf("ADB support found\n");
    301 #if NAKBD > 0
    302 			akbd_cnattach();
    303 #endif
    304 #if NADBKBD > 0
    305 			adbkbd_cnattach();
    306 #endif
    307 		} else {
    308 			/* must be USB */
    309 			printf("No ADB support present, assuming USB "
    310 			       "keyboard\n");
    311 #if NUKBD > 0
    312 			ukbd_cnattach();
    313 #endif
    314 		}
    315 		goto kbd_found;
    316 	}
    317 
    318 	/*
    319 	 * stdin is /psuedo-hid/keyboard.  Test `adb-kbd-ihandle and
    320 	 * `usb-kbd-ihandles to figure out the real keyboard(s).
    321 	 *
    322 	 * XXX This must be called before pmap_bootstrap().
    323 	 */
    324 
    325 #if NUKBD > 0
    326 	if (OF_call_method("`usb-kbd-ihandles", stdin, 0, 1, &ukbds) >= 0 &&
    327 	    ukbds != NULL && ukbds->ihandle != 0 &&
    328 	    OF_instance_to_package(ukbds->ihandle) != -1) {
    329 		printf("usb-kbd-ihandles matches\n");
    330 		printf("console keyboard type: USB\n");
    331 		ukbd_cnattach();
    332 		goto kbd_found;
    333 	}
    334 	/* Try old method name. */
    335 	if (OF_call_method("`usb-kbd-ihandle", kstdin, 0, 1, &ukbd) >= 0 &&
    336 	    ukbd != 0 &&
    337 	    OF_instance_to_package(ukbd) != -1) {
    338 		printf("usb-kbd-ihandle matches\n");
    339 		printf("console keyboard type: USB\n");
    340 		kstdin = ukbd;
    341 		ukbd_cnattach();
    342 		goto kbd_found;
    343 	}
    344 #endif
    345 
    346 #if (NAKBD > 0) || (NADBKBD > 0)
    347 	if (OF_call_method("`adb-kbd-ihandle", kstdin, 0, 1, &akbd) >= 0 &&
    348 	    akbd != 0 &&
    349 	    OF_instance_to_package(akbd) != -1) {
    350 		printf("adb-kbd-ihandle matches\n");
    351 		printf("console keyboard type: ADB\n");
    352 		kstdin = akbd;
    353 #if NAKBD > 0
    354 		akbd_cnattach();
    355 #endif
    356 #if NADBKBD > 0
    357 		adbkbd_cnattach();
    358 #endif
    359 		goto kbd_found;
    360 	}
    361 #endif
    362 
    363 #if NUKBD > 0
    364 	/*
    365 	 * XXX Old firmware does not have `usb-kbd-ihandles method.  Assume
    366 	 * XXX USB keyboard anyway.
    367 	 */
    368 	printf("defaulting to USB...");
    369 	printf("console keyboard type: USB\n");
    370 	ukbd_cnattach();
    371 	goto kbd_found;
    372 #endif
    373 
    374 	/*
    375 	 * No keyboard is found.  Just return.
    376 	 */
    377 	printf("no console keyboard\n");
    378 	return;
    379 
    380 kbd_found:;
    381 #if NAKBD + NUKBD + NADBKBD + NPCKBC > 0
    382 	/*
    383 	 * XXX This is a little gross, but we don't get to call
    384 	 * XXX wskbd_cnattach() twice.
    385 	 */
    386 	ofkbd_ihandle = kstdin;
    387 #if NWSDISPLAY > 0
    388 	wsdisplay_set_cons_kbd(ofkbd_cngetc, NULL, NULL);
    389 #endif
    390 #endif
    391 }
    392 
    393 /*
    394  * Bootstrap console keyboard routines, using OpenFirmware I/O.
    395  */
    396 int
    397 ofkbd_cngetc(dev_t dev)
    398 {
    399 	u_char c = '\0';
    400 	int len;
    401 
    402 	do {
    403 		len = OF_read(ofkbd_ihandle, &c, 1);
    404 	} while (len != 1);
    405 
    406 	return c;
    407 }
    408 
    409 /*
    410  * Bootstrap console support functions
    411  */
    412 
    413 static int
    414 ofwbootcons_cngetc(dev_t dev)
    415 {
    416 	unsigned char ch = '\0';
    417 	int l;
    418 
    419 	while ((l = OF_read(stdin, &ch, 1)) != 1)
    420 		if (l != -2 && l != 0)
    421 			return -1;
    422 	return ch;
    423 }
    424 
    425 static void
    426 ofwbootcons_cnputc(dev_t dev, int c)
    427 {
    428 	char ch = c;
    429 
    430 	OF_write(stdout, &ch, 1);
    431 }
    432 
    433 void
    434 ofwoea_consinit(void)
    435 {
    436 	static int initted = 0;
    437 
    438 	if (initted)
    439 		return;
    440 
    441 	initted = 1;
    442 	cninit();
    443 }
    444 
    445 static void
    446 ofwoea_bootstrap_console(void)
    447 {
    448 	int node;
    449 
    450 	chosen = OF_finddevice("/chosen");
    451 	if (chosen == -1)
    452 		goto nocons;
    453 
    454 	if (OF_getprop(chosen, "stdout", &stdout,
    455 	    sizeof(stdout)) != sizeof(stdout))
    456 		goto nocons;
    457 	if (OF_getprop(chosen, "stdin", &stdin,
    458 	    sizeof(stdin)) != sizeof(stdin))
    459 		goto nocons;
    460 	node = OF_instance_to_package(stdout);
    461 	console_node = node;
    462 	console_instance = stdout;
    463 
    464 	return;
    465 nocons:
    466 	panic("No /chosen could be found!\n");
    467 	console_node = -1;
    468 	return;
    469 }
    470