1 /* $NetBSD: ofw_consinit.c,v 1.7 2008/04/28 20:23:32 martin Exp $ */
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: ofw_consinit.c,v 1.7 2008/04/28 20:23:32 martin Exp $");
35 #include <sys/param.h>
39 #include <machine/autoconf.h>
40 #include <machine/trap.h>
41 #include <machine/bus.h>
43 #include <powerpc/ofw_cons.h>
46 #include <dev/ofw/openfirm.h>
48 #include <dev/wscons/wsksymvar.h>
49 #include <dev/wscons/wscons_callbacks.h>
51 #include <machine/stdarg.h>
55 #include "wsdisplay.h"
61 #include <machine/z8530var.h>
66 #include <macppc/dev/adbvar.h>
71 #include <dev/usb/ukbdvar.h>
72 struct usb_kbd_ihandles
{
73 struct usb_kbd_ihandles
*next
;
80 #include <dev/ic/z8530reg.h>
81 extern struct consdev consdev_zs
;
86 #include <dev/isa/isareg.h>
87 #include <dev/ic/i8042reg.h>
88 #include <dev/ic/pckbcvar.h>
91 extern int console_node
, console_instance
;
93 int chosen
, stdin
, stdout
;
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);
103 struct consdev consdev_ofwbootcons
= {
108 NULL
, NULL
, NULL
, NODEV
, CN_INTERNAL
,
112 void ofprint(const char *, ...);
114 void ofprint(const char *blah
, ...)
121 len
= vsnprintf(buf
, sizeof(buf
), blah
, va
);
122 OF_write(console_instance
, buf
, len
);
125 #define OFPRINTF ofprint
127 #define OFPRINTF while(0) printf
135 ofwoea_bootstrap_console();
137 OFPRINTF("console node: %08x\n", console_node
);
139 if (console_node
== -1)
142 memset(name
, 0, sizeof(name
));
143 if (OF_getprop(console_node
, "device_type", name
, sizeof(name
)) == -1)
146 OFPRINTF("console type: %s\n", name
);
148 if (strcmp(name
, "serial") == 0) {
152 /* The MMU hasn't been initialized yet, use failsafe for now */
157 aprint_verbose("Early G5 console initialized\n");
161 #if (NZSTTY > 0) && !defined(MAMBO)
162 OF_getprop(console_node
, "name", name
, sizeof(name
));
163 if (strcmp(name
, "ch-a") == 0 || strcmp(name
, "ch-b") == 0) {
172 /* fallback to OFW boot console */
173 cp
= &consdev_ofwbootcons
;
189 #if (NAKBD > 0) || (NADBKBD > 0)
193 struct usb_kbd_ihandles
*ukbds
;
198 * Attach the console output now (so we can see debugging messages,
206 * We must determine which keyboard type we have.
208 if (OF_getprop(chosen
, "stdin", &kstdin
, sizeof(kstdin
))
210 printf("WARNING: no `stdin' property in /chosen\n");
214 node
= OF_instance_to_package(kstdin
);
215 memset(name
, 0, sizeof(name
));
216 OF_getprop(node
, "name", name
, sizeof(name
));
217 if (strcmp(name
, "keyboard") != 0) {
218 printf("WARNING: stdin is not a keyboard: %s\n", name
);
223 memset(name
, 0, sizeof(name
));
224 OF_getprop(OF_parent(node
), "name", name
, sizeof(name
));
225 if (strcmp(name
, "adb") == 0) {
226 printf("console keyboard type: ADB\n");
232 memset(name
, 0, sizeof(name
));
233 OF_getprop(OF_parent(node
), "name", name
, sizeof(name
));
234 if (strcmp(name
, "adb") == 0) {
235 printf("console keyboard type: ADB\n");
241 memset(name
, 0, sizeof(name
));
242 OF_getprop(OF_parent(node
), "name", name
, sizeof(name
));
243 if (strcmp(name
, "keyboard") == 0) {
244 printf("console keyboard type: PC Keyboard\n");
245 pckbc_cnattach(&genppc_isa_io_space_tag
, IO_KBD
, KBCMDP
,
252 * It is not obviously an ADB/PC keyboard. Could be USB,
253 * or ADB on some firmware versions (e.g.: iBook G4)
254 * This is not enough, we have a few more problems:
256 * (1) The stupid Macintosh firmware uses a
257 * `psuedo-hid' (no typo) or `pseudo-hid',
258 * which apparently merges all keyboards
259 * input into a single input stream.
260 * Because of this, we can't actually
261 * determine which controller or keyboard
262 * is really the console keyboard!
264 * (2) Even if we could, the keyboard can be USB,
265 * and this requires a lot of the kernel to
266 * be running in order for it to work.
268 * (3) If the keyboard is behind isa, we don't have enough
269 * kernel setup to use it yet, so punt to the ofroutines.
271 * So, what we do is this:
273 * (1) First check for OpenFirmware implementation
274 * that will not let us distinguish between
275 * USB and ADB. In that situation, try attaching
276 * anything as we can, and hope things get better
277 * at autoconfiguration time.
279 * (2) Assume the keyboard is USB.
280 * Tell the ukbd driver that it is the console.
281 * At autoconfiguration time, it will attach the
282 * first USB keyboard instance as the console
285 * (3) Until then, so that we have _something_, we
286 * use the OpenFirmware I/O facilities to read
291 * stdin is /pseudo-hid/keyboard. There is no
292 * `adb-kbd-ihandle or `usb-kbd-ihandles methods
293 * available. Try attaching as ADB.
294 * But only if ADB support is actually present.
296 * XXX This must be called before pmap_bootstrap().
298 if (strcmp(name
, "pseudo-hid") == 0) {
301 adb_node
= OF_finddevice("/pci/mac-io/via-pmu/adb");
303 printf("ADB support found\n");
312 printf("No ADB support present, assuming USB "
322 * stdin is /psuedo-hid/keyboard. Test `adb-kbd-ihandle and
323 * `usb-kbd-ihandles to figure out the real keyboard(s).
325 * XXX This must be called before pmap_bootstrap().
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");
337 /* Try old method name. */
338 if (OF_call_method("`usb-kbd-ihandle", kstdin
, 0, 1, &ukbd
) >= 0 &&
340 OF_instance_to_package(ukbd
) != -1) {
341 printf("usb-kbd-ihandle matches\n");
342 printf("console keyboard type: USB\n");
349 #if (NAKBD > 0) || (NADBKBD > 0)
350 if (OF_call_method("`adb-kbd-ihandle", kstdin
, 0, 1, &akbd
) >= 0 &&
352 OF_instance_to_package(akbd
) != -1) {
353 printf("adb-kbd-ihandle matches\n");
354 printf("console keyboard type: ADB\n");
368 * XXX Old firmware does not have `usb-kbd-ihandles method. Assume
369 * XXX USB keyboard anyway.
371 printf("defaulting to USB...");
372 printf("console keyboard type: USB\n");
378 * No keyboard is found. Just return.
380 printf("no console keyboard\n");
384 #if NAKBD + NUKBD + NADBKBD + NPCKBC > 0
386 * XXX This is a little gross, but we don't get to call
387 * XXX wskbd_cnattach() twice.
389 ofkbd_ihandle
= kstdin
;
391 wsdisplay_set_cons_kbd(ofkbd_cngetc
, NULL
, NULL
);
397 * Bootstrap console keyboard routines, using OpenFirmware I/O.
400 ofkbd_cngetc(dev_t dev
)
406 len
= OF_read(ofkbd_ihandle
, &c
, 1);
413 * Bootstrap console support functions
417 ofwbootcons_cngetc(dev_t dev
)
419 unsigned char ch
= '\0';
422 while ((l
= OF_read(stdin
, &ch
, 1)) != 1)
423 if (l
!= -2 && l
!= 0)
429 ofwbootcons_cnputc(dev_t dev
, int c
)
433 OF_write(stdout
, &ch
, 1);
437 ofwoea_consinit(void)
439 static int initted
= 0;
449 ofwoea_bootstrap_console(void)
453 chosen
= OF_finddevice("/chosen");
457 if (OF_getprop(chosen
, "stdout", &stdout
,
458 sizeof(stdout
)) != sizeof(stdout
))
460 if (OF_getprop(chosen
, "stdin", &stdin
,
461 sizeof(stdin
)) != sizeof(stdin
))
463 node
= OF_instance_to_package(stdout
);
465 console_instance
= stdout
;
469 panic("No /chosen could be found!\n");