ofw_consinit.c revision 1.24 1 /* $NetBSD: ofw_consinit.c,v 1.24 2021/03/05 18:10:06 thorpej 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.24 2021/03/05 18:10:06 thorpej Exp $");
34
35 #include "adb.h"
36 #include "adbkbd.h"
37 #include "akbd.h"
38 #include "isa.h"
39 #include "ofb.h"
40 #include "pckbc.h"
41 #include "ukbd.h"
42 #include "wsdisplay.h"
43 #include "zsc.h"
44 #include "zstty.h"
45
46 #include <sys/param.h>
47 #include <sys/buf.h>
48 #include <sys/tty.h>
49
50 #include <prop/proplib.h>
51
52 #include <machine/autoconf.h>
53 #include <machine/trap.h>
54 #include <sys/bus.h>
55
56 #include <powerpc/ofw_cons.h>
57 #include <powerpc/ofw_machdep.h>
58
59 #include <dev/cons.h>
60 #include <dev/ofw/openfirm.h>
61
62 #include <dev/wscons/wsksymvar.h>
63 #include <dev/wscons/wscons_callbacks.h>
64
65 #if NZSC > 0
66 #include <machine/z8530var.h>
67 #endif
68
69 #if (NADB > 0)
70 #include <macppc/dev/adbvar.h>
71 #endif
72
73 #if (NUKBD > 0)
74 #include <dev/usb/ukbdvar.h>
75 struct usb_kbd_ihandles {
76 struct usb_kbd_ihandles *next;
77 int ihandle;
78 };
79 #endif
80
81 #if (NZSTTY > 0)
82 #include <dev/ic/z8530reg.h>
83 extern struct consdev consdev_zs;
84 #endif
85
86 #if (NPCKBC > 0)
87 #include <dev/isa/isareg.h>
88 #include <dev/ic/i8042reg.h>
89 #include <dev/ic/pckbcvar.h>
90 #endif
91
92 extern int console_node, console_instance;
93
94 int ofkbd_ihandle = -1;
95
96 static void ofwoea_cnprobe_keyboard(void);
97
98 /*#define OFDEBUG*/
99
100 #ifdef OFDEBUG
101 void ofprint(const char *, ...);
102
103 void ofprint(const char *blah, ...)
104 {
105 va_list va;
106 char buf[256];
107 int len;
108
109 va_start(va, blah);
110 len = vsnprintf(buf, sizeof(buf), blah, va);
111 va_end(va);
112 OF_write(console_instance, buf, len);
113 }
114
115 #define OFPRINTF ofprint
116 #else
117 #define OFPRINTF while(0) printf
118 #endif
119
120 static bool use_serial_console;
121 static struct consdev *selected_serial_consdev;
122
123 static int (*selected_keyboard)(void);
124
125 /* XXX Gross. */
126 #if NPCKBC > 0
127 static int
128 ofwoea_pckbd_cnattach(void)
129 {
130 return pckbc_cnattach(&genppc_isa_io_space_tag, IO_KBD, KBCMDP,
131 PCKBC_KBD_SLOT, 0);
132 }
133 #endif
134
135 void
136 ofwoea_cnprobe(void)
137 {
138 char name[32];
139
140 OFPRINTF("console node: %08x\n", console_node);
141
142 if (console_node == -1)
143 return;
144
145 memset(name, 0, sizeof(name));
146 if (OF_getprop(console_node, "device_type", name, sizeof(name)) == -1)
147 return;
148
149 OFPRINTF("console type: %s\n", name);
150
151 if (strcmp(name, "serial") == 0) {
152 use_serial_console = true;
153 #ifdef PMAC_G5
154 /* The MMU hasn't been initialized yet, use failsafe for now */
155 extern struct consdev failsafe_cons;
156 selected_serial_consdev = &failsafe_cons;
157 aprint_verbose("Early G5 console selected\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 selected_serial_consdev = &consdev_zs;
165 }
166 return;
167 #endif /* NZTTY */
168
169 /* fallback to OFW boot console (already set) */
170 return;
171 }
172
173 /*
174 * We're going to use a display console. Probe for the keyboard
175 * we'll use.
176 */
177 ofwoea_cnprobe_keyboard();
178 }
179
180 /*
181 * XXX This routine is a complete disaster, filled with platform-specific
182 * XXX stuff. Fix, plz.
183 */
184 static void
185 ofwoea_cnprobe_keyboard(void)
186 {
187 extern int ofw_stdin;
188
189 int node, kstdin = ofw_stdin;
190 char name[16];
191 #if (NAKBD > 0) || (NADBKBD > 0)
192 int akbd;
193 #endif
194 #if NUKBD > 0
195 struct usb_kbd_ihandles *ukbds;
196 int ukbd;
197 #endif
198
199 /*
200 * We must determine which keyboard type we have.
201 */
202 node = OF_instance_to_package(kstdin);
203 memset(name, 0, sizeof(name));
204 OF_getprop(node, "name", name, sizeof(name));
205 if (strcmp(name, "keyboard") != 0) {
206 printf("WARNING: stdin is not a keyboard: %s\n", name);
207 return;
208 }
209
210 memset(name, 0, sizeof(name));
211 OF_getprop(OF_parent(node), "name", name, sizeof(name));
212 #if NAKBD > 0
213 if (strcmp(name, "adb") == 0) {
214 printf("console keyboard type: ADB\n");
215 selected_keyboard = akbd_cnattach;
216 goto kbd_found;
217 }
218 #endif
219 #if NADBKBD > 0
220 if (strcmp(name, "adb") == 0) {
221 printf("console keyboard type: ADB\n");
222 selected_keyboard = adbkbd_cnattach;
223 goto kbd_found;
224 }
225 #endif
226 #if NPCKBC > 0
227 if (strcmp(name, "isa") == 0) {
228 printf("console keyboard type: PC Keyboard\n");
229 selected_keyboard = ofwoea_pckbd_cnattach;
230 goto kbd_found;
231 }
232 #endif
233
234 /*
235 * It is not obviously an ADB/PC keyboard. Could be USB,
236 * or ADB on some firmware versions (e.g.: iBook G4)
237 * This is not enough, we have a few more problems:
238 *
239 * (1) The stupid Macintosh firmware uses a
240 * `psuedo-hid' (no typo) or `pseudo-hid',
241 * which apparently merges all keyboards
242 * input into a single input stream.
243 * Because of this, we can't actually
244 * determine which controller or keyboard
245 * is really the console keyboard!
246 *
247 * (2) Even if we could, the keyboard can be USB,
248 * and this requires a lot of the kernel to
249 * be running in order for it to work.
250 *
251 * (3) If the keyboard is behind isa, we don't have enough
252 * kernel setup to use it yet, so punt to the ofroutines.
253 *
254 * So, what we do is this:
255 *
256 * (1) First check for OpenFirmware implementation
257 * that will not let us distinguish between
258 * USB and ADB. In that situation, try attaching
259 * anything as we can, and hope things get better
260 * at autoconfiguration time.
261 *
262 * (2) Assume the keyboard is USB.
263 * Tell the ukbd driver that it is the console.
264 * At autoconfiguration time, it will attach the
265 * first USB keyboard instance as the console
266 * keyboard.
267 *
268 * (3) Until then, so that we have _something_, we
269 * use the OpenFirmware I/O facilities to read
270 * the keyboard.
271 */
272
273 /*
274 * stdin is /pseudo-hid/keyboard. There is no
275 * `adb-kbd-ihandle or `usb-kbd-ihandles methods
276 * available. Try attaching as ADB.
277 * But only if ADB support is actually present.
278 *
279 * XXX This must be called before pmap_bootstrap().
280 */
281 if (strcmp(name, "pseudo-hid") == 0) {
282 int adb_node;
283
284 adb_node = OF_finddevice("/pci/mac-io/via-pmu/adb");
285 if (adb_node > 0) {
286 printf("ADB support found\n");
287 #if NAKBD > 0
288 selected_keyboard = akbd_cnattach;
289 #endif
290 #if NADBKBD > 0
291 selected_keyboard = adbkbd_cnattach;
292 #endif
293 } else {
294 /* must be USB */
295 printf("No ADB support present, assuming USB "
296 "keyboard\n");
297 #if NUKBD > 0
298 selected_keyboard = ukbd_cnattach;
299 #endif
300 }
301 goto kbd_found;
302 }
303
304 /*
305 * stdin is /psuedo-hid/keyboard. Test `adb-kbd-ihandle and
306 * `usb-kbd-ihandles to figure out the real keyboard(s).
307 *
308 * XXX This must be called before pmap_bootstrap().
309 */
310
311 #if NUKBD > 0
312 if (OF_call_method("`usb-kbd-ihandles", kstdin, 0, 1, &ukbds) >= 0 &&
313 ukbds != NULL && ukbds->ihandle != 0 &&
314 OF_instance_to_package(ukbds->ihandle) != -1) {
315 printf("usb-kbd-ihandles matches\n");
316 printf("console keyboard type: USB\n");
317 selected_keyboard = ukbd_cnattach;
318 goto kbd_found;
319 }
320 /* Try old method name. */
321 if (OF_call_method("`usb-kbd-ihandle", kstdin, 0, 1, &ukbd) >= 0 &&
322 ukbd != 0 &&
323 OF_instance_to_package(ukbd) != -1) {
324 printf("usb-kbd-ihandle matches\n");
325 printf("console keyboard type: USB\n");
326 kstdin = ukbd;
327 selected_keyboard = ukbd_cnattach;
328 goto kbd_found;
329 }
330 #endif
331
332 #if (NAKBD > 0) || (NADBKBD > 0)
333 if (OF_call_method("`adb-kbd-ihandle", kstdin, 0, 1, &akbd) >= 0 &&
334 akbd != 0 &&
335 OF_instance_to_package(akbd) != -1) {
336 printf("adb-kbd-ihandle matches\n");
337 printf("console keyboard type: ADB\n");
338 kstdin = akbd;
339 #if NAKBD > 0
340 selected_keyboard = akbd_cnattach;
341 #endif
342 #if NADBKBD > 0
343 selected_keyboard = adbkbd_cnattach;
344 #endif
345 goto kbd_found;
346 }
347 #endif
348
349 #if NUKBD > 0
350 /*
351 * XXX Old firmware does not have `usb-kbd-ihandles method. Assume
352 * XXX USB keyboard anyway.
353 */
354 printf("defaulting to USB...");
355 printf("console keyboard type: USB\n");
356 selected_keyboard = ukbd_cnattach;
357 goto kbd_found;
358 #endif
359
360 /*
361 * No keyboard is found. Just return.
362 */
363 printf("no console keyboard\n");
364 return;
365
366 kbd_found:
367 ofkbd_ihandle = kstdin;
368 }
369
370 /*
371 * Bootstrap console keyboard routines, using OpenFirmware I/O.
372 */
373 int
374 ofkbd_cngetc(dev_t dev)
375 {
376 u_char c = '\0';
377 int len;
378
379 KASSERT(ofkbd_ihandle != -1);
380
381 do {
382 len = OF_read(ofkbd_ihandle, &c, 1);
383 } while (len != 1);
384
385 return c;
386 }
387
388 void
389 cninit(void)
390 {
391 if (use_serial_console) {
392 if (selected_serial_consdev != NULL) {
393 cn_tab = selected_serial_consdev;
394 (*cn_tab->cn_probe)(cn_tab);
395 (*cn_tab->cn_init)(cn_tab);
396 }
397 return;
398 }
399
400 #if NWSDISPLAY > 0
401 rascons_cnattach();
402 #endif
403 if (selected_keyboard != NULL) {
404 (*selected_keyboard)();
405
406 #if NWSDISPLAY > 0
407 /*
408 * XXX This is a little gross, but we don't get to call
409 * XXX wskbd_cnattach() twice.
410 */
411 wsdisplay_set_cons_kbd(ofkbd_cngetc, NULL, NULL);
412 #endif
413 }
414 }
415
416 void
417 ofwoea_consinit(void)
418 {
419 static int initted = 0;
420
421 if (initted)
422 return;
423
424 initted = 1;
425 cninit();
426 }
427