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