ofw_consinit.c revision 1.25 1 /* $NetBSD: ofw_consinit.c,v 1.25 2022/02/13 12:24:24 martin 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.25 2022/02/13 12:24:24 martin 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 bool ofwoea_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 ofwoea_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 "
158 "(keeping OF console for now)\n");
159 return;
160 #endif /* PMAC_G5 */
161
162 #if (NZSTTY > 0) && !defined(MAMBO)
163 OF_getprop(console_node, "name", name, sizeof(name));
164 if (strcmp(name, "ch-a") == 0 || strcmp(name, "ch-b") == 0) {
165 selected_serial_consdev = &consdev_zs;
166 }
167 return;
168 #endif /* NZTTY */
169
170 /* fallback to OFW boot console (already set) */
171 return;
172 }
173
174 /*
175 * We're going to use a display console. Probe for the keyboard
176 * we'll use.
177 */
178 ofwoea_cnprobe_keyboard();
179 }
180
181 /*
182 * XXX This routine is a complete disaster, filled with platform-specific
183 * XXX stuff. Fix, plz.
184 */
185 static void
186 ofwoea_cnprobe_keyboard(void)
187 {
188 extern int ofw_stdin;
189
190 int node, kstdin = ofw_stdin;
191 char name[16];
192 #if (NAKBD > 0) || (NADBKBD > 0)
193 int akbd;
194 #endif
195 #if NUKBD > 0
196 struct usb_kbd_ihandles *ukbds;
197 int ukbd;
198 #endif
199
200 /*
201 * We must determine which keyboard type we have.
202 */
203 node = OF_instance_to_package(kstdin);
204 memset(name, 0, sizeof(name));
205 OF_getprop(node, "name", name, sizeof(name));
206 if (strcmp(name, "keyboard") != 0) {
207 printf("WARNING: stdin is not a keyboard: %s\n", name);
208 return;
209 }
210
211 memset(name, 0, sizeof(name));
212 OF_getprop(OF_parent(node), "name", name, sizeof(name));
213 #if NAKBD > 0
214 if (strcmp(name, "adb") == 0) {
215 printf("console keyboard type: ADB\n");
216 selected_keyboard = akbd_cnattach;
217 goto kbd_found;
218 }
219 #endif
220 #if NADBKBD > 0
221 if (strcmp(name, "adb") == 0) {
222 printf("console keyboard type: ADB\n");
223 selected_keyboard = adbkbd_cnattach;
224 goto kbd_found;
225 }
226 #endif
227 #if NPCKBC > 0
228 if (strcmp(name, "isa") == 0) {
229 printf("console keyboard type: PC Keyboard\n");
230 selected_keyboard = ofwoea_pckbd_cnattach;
231 goto kbd_found;
232 }
233 #endif
234
235 /*
236 * It is not obviously an ADB/PC keyboard. Could be USB,
237 * or ADB on some firmware versions (e.g.: iBook G4)
238 * This is not enough, we have a few more problems:
239 *
240 * (1) The stupid Macintosh firmware uses a
241 * `psuedo-hid' (no typo) or `pseudo-hid',
242 * which apparently merges all keyboards
243 * input into a single input stream.
244 * Because of this, we can't actually
245 * determine which controller or keyboard
246 * is really the console keyboard!
247 *
248 * (2) Even if we could, the keyboard can be USB,
249 * and this requires a lot of the kernel to
250 * be running in order for it to work.
251 *
252 * (3) If the keyboard is behind isa, we don't have enough
253 * kernel setup to use it yet, so punt to the ofroutines.
254 *
255 * So, what we do is this:
256 *
257 * (1) First check for OpenFirmware implementation
258 * that will not let us distinguish between
259 * USB and ADB. In that situation, try attaching
260 * anything as we can, and hope things get better
261 * at autoconfiguration time.
262 *
263 * (2) Assume the keyboard is USB.
264 * Tell the ukbd driver that it is the console.
265 * At autoconfiguration time, it will attach the
266 * first USB keyboard instance as the console
267 * keyboard.
268 *
269 * (3) Until then, so that we have _something_, we
270 * use the OpenFirmware I/O facilities to read
271 * the keyboard.
272 */
273
274 /*
275 * stdin is /pseudo-hid/keyboard. There is no
276 * `adb-kbd-ihandle or `usb-kbd-ihandles methods
277 * available. Try attaching as ADB.
278 * But only if ADB support is actually present.
279 *
280 * XXX This must be called before pmap_bootstrap().
281 */
282 if (strcmp(name, "pseudo-hid") == 0) {
283 int adb_node;
284
285 adb_node = OF_finddevice("/pci/mac-io/via-pmu/adb");
286 if (adb_node > 0) {
287 printf("ADB support found\n");
288 #if NAKBD > 0
289 selected_keyboard = akbd_cnattach;
290 #endif
291 #if NADBKBD > 0
292 selected_keyboard = adbkbd_cnattach;
293 #endif
294 } else {
295 /* must be USB */
296 printf("No ADB support present, assuming USB "
297 "keyboard\n");
298 #if NUKBD > 0
299 selected_keyboard = ukbd_cnattach;
300 #endif
301 }
302 goto kbd_found;
303 }
304
305 /*
306 * stdin is /psuedo-hid/keyboard. Test `adb-kbd-ihandle and
307 * `usb-kbd-ihandles to figure out the real keyboard(s).
308 *
309 * XXX This must be called before pmap_bootstrap().
310 */
311
312 #if NUKBD > 0
313 if (OF_call_method("`usb-kbd-ihandles", kstdin, 0, 1, &ukbds) >= 0 &&
314 ukbds != NULL && ukbds->ihandle != 0 &&
315 OF_instance_to_package(ukbds->ihandle) != -1) {
316 printf("usb-kbd-ihandles matches\n");
317 printf("console keyboard type: USB\n");
318 selected_keyboard = ukbd_cnattach;
319 goto kbd_found;
320 }
321 /* Try old method name. */
322 if (OF_call_method("`usb-kbd-ihandle", kstdin, 0, 1, &ukbd) >= 0 &&
323 ukbd != 0 &&
324 OF_instance_to_package(ukbd) != -1) {
325 printf("usb-kbd-ihandle matches\n");
326 printf("console keyboard type: USB\n");
327 kstdin = ukbd;
328 selected_keyboard = ukbd_cnattach;
329 goto kbd_found;
330 }
331 #endif
332
333 #if (NAKBD > 0) || (NADBKBD > 0)
334 if (OF_call_method("`adb-kbd-ihandle", kstdin, 0, 1, &akbd) >= 0 &&
335 akbd != 0 &&
336 OF_instance_to_package(akbd) != -1) {
337 printf("adb-kbd-ihandle matches\n");
338 printf("console keyboard type: ADB\n");
339 kstdin = akbd;
340 #if NAKBD > 0
341 selected_keyboard = akbd_cnattach;
342 #endif
343 #if NADBKBD > 0
344 selected_keyboard = adbkbd_cnattach;
345 #endif
346 goto kbd_found;
347 }
348 #endif
349
350 #if NUKBD > 0
351 /*
352 * XXX Old firmware does not have `usb-kbd-ihandles method. Assume
353 * XXX USB keyboard anyway.
354 */
355 printf("defaulting to USB...");
356 printf("console keyboard type: USB\n");
357 selected_keyboard = ukbd_cnattach;
358 goto kbd_found;
359 #endif
360
361 /*
362 * No keyboard is found. Just return.
363 */
364 printf("no console keyboard\n");
365 return;
366
367 kbd_found:
368 ofkbd_ihandle = kstdin;
369 }
370
371 /*
372 * Bootstrap console keyboard routines, using OpenFirmware I/O.
373 */
374 int
375 ofkbd_cngetc(dev_t dev)
376 {
377 u_char c = '\0';
378 int len;
379
380 KASSERT(ofkbd_ihandle != -1);
381
382 do {
383 len = OF_read(ofkbd_ihandle, &c, 1);
384 } while (len != 1);
385
386 return c;
387 }
388
389 void
390 cninit(void)
391 {
392 if (ofwoea_use_serial_console) {
393 if (selected_serial_consdev != NULL) {
394 cn_tab = selected_serial_consdev;
395 (*cn_tab->cn_probe)(cn_tab);
396 (*cn_tab->cn_init)(cn_tab);
397 }
398 return;
399 }
400
401 #if NWSDISPLAY > 0
402 rascons_cnattach();
403 #endif
404 if (selected_keyboard != NULL) {
405 (*selected_keyboard)();
406
407 #if NWSDISPLAY > 0
408 /*
409 * XXX This is a little gross, but we don't get to call
410 * XXX wskbd_cnattach() twice.
411 */
412 wsdisplay_set_cons_kbd(ofkbd_cngetc, NULL, NULL);
413 #endif
414 }
415 }
416
417 void
418 ofwoea_consinit(void)
419 {
420 static int initted = 0;
421
422 if (initted)
423 return;
424
425 initted = 1;
426 cninit();
427 }
428