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