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