ofw_consinit.c revision 1.6.14.1 1 /* $NetBSD: ofw_consinit.c,v 1.6.14.1 2008/06/02 13:22:32 mjf 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.6.14.1 2008/06/02 13:22:32 mjf Exp $");
34
35 #include <sys/param.h>
36 #include <sys/buf.h>
37 #include <sys/tty.h>
38
39 #include <machine/autoconf.h>
40 #include <machine/trap.h>
41 #include <machine/bus.h>
42
43 #include <powerpc/ofw_cons.h>
44
45 #include <dev/cons.h>
46 #include <dev/ofw/openfirm.h>
47
48 #include <dev/wscons/wsksymvar.h>
49 #include <dev/wscons/wscons_callbacks.h>
50
51 #include <machine/stdarg.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 extern int console_node, console_instance;
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 cp = &failsafe_cons;
154 cn_tab = cp;
155 (*cp->cn_probe)(cp);
156 (*cp->cn_init)(cp);
157 aprint_verbose("Early G5 console initialized\n");
158 return;
159 #endif /* PMAC_G5 */
160
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) {
164 cp = &consdev_zs;
165 (*cp->cn_probe)(cp);
166 (*cp->cn_init)(cp);
167 cn_tab = cp;
168 }
169 return;
170 #endif /* NZTTY */
171
172 /* fallback to OFW boot console */
173 cp = &consdev_ofwbootcons;
174 cn_tab = cp;
175 return;
176 }
177 else
178 cninit_kd();
179 nocons:
180 return;
181 }
182
183
184 static void
185 cninit_kd(void)
186 {
187 int kstdin, node;
188 char name[16];
189 #if (NAKBD > 0) || (NADBKBD > 0)
190 int akbd;
191 #endif
192 #if NUKBD > 0
193 struct usb_kbd_ihandles *ukbds;
194 int ukbd;
195 #endif
196
197 /*
198 * Attach the console output now (so we can see debugging messages,
199 * if any).
200 */
201 #if NWSDISPLAY > 0
202 rascons_cnattach();
203 #endif
204
205 /*
206 * We must determine which keyboard type we have.
207 */
208 if (OF_getprop(chosen, "stdin", &kstdin, sizeof(kstdin))
209 != sizeof(kstdin)) {
210 printf("WARNING: no `stdin' property in /chosen\n");
211 return;
212 }
213
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);
219 return;
220 }
221
222 #if NAKBD > 0
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");
227 akbd_cnattach();
228 goto kbd_found;
229 }
230 #endif
231 #if NADBKBD > 0
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");
236 adbkbd_cnattach();
237 goto kbd_found;
238 }
239 #endif
240 #if NPCKBC > 0
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,
246 PCKBC_KBD_SLOT);
247 goto kbd_found;
248 }
249 #endif
250
251 /*
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:
255 *
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!
263 *
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.
267 *
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.
270 *
271 * So, what we do is this:
272 *
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.
278 *
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
283 * keyboard.
284 *
285 * (3) Until then, so that we have _something_, we
286 * use the OpenFirmware I/O facilities to read
287 * the keyboard.
288 */
289
290 /*
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 *
295 * XXX This must be called before pmap_bootstrap().
296 */
297 if (strcmp(name, "pseudo-hid") == 0) {
298 printf("console keyboard type: unknown, assuming ADB\n");
299 #if NAKBD > 0
300 akbd_cnattach();
301 #endif
302 #if NADBKBD > 0
303 adbkbd_cnattach();
304 #endif
305 goto kbd_found;
306 }
307
308 /*
309 * stdin is /psuedo-hid/keyboard. Test `adb-kbd-ihandle and
310 * `usb-kbd-ihandles to figure out the real keyboard(s).
311 *
312 * XXX This must be called before pmap_bootstrap().
313 */
314
315 #if NUKBD > 0
316 if (OF_call_method("`usb-kbd-ihandles", stdin, 0, 1, &ukbds) >= 0 &&
317 ukbds != NULL && ukbds->ihandle != 0 &&
318 OF_instance_to_package(ukbds->ihandle) != -1) {
319 printf("usb-kbd-ihandles matches\n");
320 printf("console keyboard type: USB\n");
321 ukbd_cnattach();
322 goto kbd_found;
323 }
324 /* Try old method name. */
325 if (OF_call_method("`usb-kbd-ihandle", kstdin, 0, 1, &ukbd) >= 0 &&
326 ukbd != 0 &&
327 OF_instance_to_package(ukbd) != -1) {
328 printf("usb-kbd-ihandle matches\n");
329 printf("console keyboard type: USB\n");
330 kstdin = ukbd;
331 ukbd_cnattach();
332 goto kbd_found;
333 }
334 #endif
335
336 #if (NAKBD > 0) || (NADBKBD > 0)
337 if (OF_call_method("`adb-kbd-ihandle", kstdin, 0, 1, &akbd) >= 0 &&
338 akbd != 0 &&
339 OF_instance_to_package(akbd) != -1) {
340 printf("adb-kbd-ihandle matches\n");
341 printf("console keyboard type: ADB\n");
342 kstdin = akbd;
343 #if NAKBD > 0
344 akbd_cnattach();
345 #endif
346 #if NADBKBD > 0
347 adbkbd_cnattach();
348 #endif
349 goto kbd_found;
350 }
351 #endif
352
353 #if NUKBD > 0
354 /*
355 * XXX Old firmware does not have `usb-kbd-ihandles method. Assume
356 * XXX USB keyboard anyway.
357 */
358 printf("defaulting to USB...");
359 printf("console keyboard type: USB\n");
360 ukbd_cnattach();
361 goto kbd_found;
362 #endif
363
364 /*
365 * No keyboard is found. Just return.
366 */
367 printf("no console keyboard\n");
368 return;
369
370 kbd_found:;
371 #if NAKBD + NUKBD + NADBKBD > 0
372 /*
373 * XXX This is a little gross, but we don't get to call
374 * XXX wskbd_cnattach() twice.
375 */
376 ofkbd_ihandle = kstdin;
377 #if NWSDISPLAY > 0
378 wsdisplay_set_cons_kbd(ofkbd_cngetc, NULL, NULL);
379 #endif
380 #endif
381 }
382
383 /*
384 * Bootstrap console keyboard routines, using OpenFirmware I/O.
385 */
386 int
387 ofkbd_cngetc(dev_t dev)
388 {
389 u_char c = '\0';
390 int len;
391
392 do {
393 len = OF_read(ofkbd_ihandle, &c, 1);
394 } while (len != 1);
395
396 return c;
397 }
398
399 /*
400 * Bootstrap console support functions
401 */
402
403 static int
404 ofwbootcons_cngetc(dev_t dev)
405 {
406 unsigned char ch = '\0';
407 int l;
408
409 while ((l = OF_read(stdin, &ch, 1)) != 1)
410 if (l != -2 && l != 0)
411 return -1;
412 return ch;
413 }
414
415 static void
416 ofwbootcons_cnputc(dev_t dev, int c)
417 {
418 char ch = c;
419
420 OF_write(stdout, &ch, 1);
421 }
422
423 void
424 ofwoea_consinit(void)
425 {
426 static int initted = 0;
427
428 if (initted)
429 return;
430
431 initted = 1;
432 cninit();
433 }
434
435 static void
436 ofwoea_bootstrap_console(void)
437 {
438 int node;
439
440 chosen = OF_finddevice("/chosen");
441 if (chosen == -1)
442 goto nocons;
443
444 if (OF_getprop(chosen, "stdout", &stdout,
445 sizeof(stdout)) != sizeof(stdout))
446 goto nocons;
447 if (OF_getprop(chosen, "stdin", &stdin,
448 sizeof(stdin)) != sizeof(stdin))
449 goto nocons;
450 node = OF_instance_to_package(stdout);
451 console_node = node;
452 console_instance = stdout;
453
454 return;
455 nocons:
456 panic("No /chosen could be found!\n");
457 console_node = -1;
458 return;
459 }
460