ofw_consinit.c revision 1.23 1 /* $NetBSD: ofw_consinit.c,v 1.23 2021/02/19 18:05:42 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.23 2021/02/19 18:05:42 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;
95
96 static void cninit_kd(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 void
121 cninit(void)
122 {
123 char name[32];
124
125 OFPRINTF("console node: %08x\n", console_node);
126
127 if (console_node == -1)
128 goto nocons;
129
130 memset(name, 0, sizeof(name));
131 if (OF_getprop(console_node, "device_type", name, sizeof(name)) == -1)
132 goto nocons;
133
134 OFPRINTF("console type: %s\n", name);
135
136 if (strcmp(name, "serial") == 0) {
137 #ifdef PMAC_G5
138 /* The MMU hasn't been initialized yet, use failsafe for now */
139 extern struct consdev failsafe_cons;
140 cn_tab = &failsafe_cons;
141 (*cn_tab->cn_probe)(cn_tab);
142 (*cn_tab->cn_init)(cn_tab);
143 aprint_verbose("Early G5 console initialized\n");
144 return;
145 #endif /* PMAC_G5 */
146
147 #if (NZSTTY > 0) && !defined(MAMBO)
148 OF_getprop(console_node, "name", name, sizeof(name));
149 if (strcmp(name, "ch-a") == 0 || strcmp(name, "ch-b") == 0) {
150 cn_tab = &consdev_zs;
151 (*cn_tab->cn_probe)(cn_tab);
152 (*cn_tab->cn_init)(cn_tab);
153 }
154 return;
155 #endif /* NZTTY */
156
157 /* fallback to OFW boot console (already set) */
158 return;
159 }
160 else
161 cninit_kd();
162 nocons:
163 return;
164 }
165
166
167 static void
168 cninit_kd(void)
169 {
170 int kstdin, node;
171 char name[16];
172 #if (NAKBD > 0) || (NADBKBD > 0)
173 int akbd;
174 #endif
175 #if NUKBD > 0
176 struct usb_kbd_ihandles *ukbds;
177 int ukbd;
178 #endif
179
180 /*
181 * Attach the console output now (so we can see debugging messages,
182 * if any).
183 */
184 #if NWSDISPLAY > 0
185 rascons_cnattach();
186 #endif
187
188 /*
189 * We must determine which keyboard type we have.
190 */
191 if (OF_getprop(ofw_chosen, "stdin", &kstdin, sizeof(kstdin))
192 != sizeof(kstdin)) {
193 printf("WARNING: no `stdin' property in /chosen\n");
194 return;
195 }
196
197 node = OF_instance_to_package(kstdin);
198 memset(name, 0, sizeof(name));
199 OF_getprop(node, "name", name, sizeof(name));
200 if (strcmp(name, "keyboard") != 0) {
201 printf("WARNING: stdin is not a keyboard: %s\n", name);
202 return;
203 }
204
205 memset(name, 0, sizeof(name));
206 OF_getprop(OF_parent(node), "name", name, sizeof(name));
207 #if NAKBD > 0
208 if (strcmp(name, "adb") == 0) {
209 printf("console keyboard type: ADB\n");
210 akbd_cnattach();
211 goto kbd_found;
212 }
213 #endif
214 #if NADBKBD > 0
215 if (strcmp(name, "adb") == 0) {
216 printf("console keyboard type: ADB\n");
217 adbkbd_cnattach();
218 goto kbd_found;
219 }
220 #endif
221 #if NPCKBC > 0
222 if (strcmp(name, "isa") == 0) {
223 printf("console keyboard type: PC Keyboard\n");
224 pckbc_cnattach(&genppc_isa_io_space_tag, IO_KBD, KBCMDP,
225 PCKBC_KBD_SLOT, 0);
226 goto kbd_found;
227 }
228 #endif
229
230 /*
231 * It is not obviously an ADB/PC keyboard. Could be USB,
232 * or ADB on some firmware versions (e.g.: iBook G4)
233 * This is not enough, we have a few more problems:
234 *
235 * (1) The stupid Macintosh firmware uses a
236 * `psuedo-hid' (no typo) or `pseudo-hid',
237 * which apparently merges all keyboards
238 * input into a single input stream.
239 * Because of this, we can't actually
240 * determine which controller or keyboard
241 * is really the console keyboard!
242 *
243 * (2) Even if we could, the keyboard can be USB,
244 * and this requires a lot of the kernel to
245 * be running in order for it to work.
246 *
247 * (3) If the keyboard is behind isa, we don't have enough
248 * kernel setup to use it yet, so punt to the ofroutines.
249 *
250 * So, what we do is this:
251 *
252 * (1) First check for OpenFirmware implementation
253 * that will not let us distinguish between
254 * USB and ADB. In that situation, try attaching
255 * anything as we can, and hope things get better
256 * at autoconfiguration time.
257 *
258 * (2) Assume the keyboard is USB.
259 * Tell the ukbd driver that it is the console.
260 * At autoconfiguration time, it will attach the
261 * first USB keyboard instance as the console
262 * keyboard.
263 *
264 * (3) Until then, so that we have _something_, we
265 * use the OpenFirmware I/O facilities to read
266 * the keyboard.
267 */
268
269 /*
270 * stdin is /pseudo-hid/keyboard. There is no
271 * `adb-kbd-ihandle or `usb-kbd-ihandles methods
272 * available. Try attaching as ADB.
273 * But only if ADB support is actually present.
274 *
275 * XXX This must be called before pmap_bootstrap().
276 */
277 if (strcmp(name, "pseudo-hid") == 0) {
278 int adb_node;
279
280 adb_node = OF_finddevice("/pci/mac-io/via-pmu/adb");
281 if (adb_node > 0) {
282 printf("ADB support found\n");
283 #if NAKBD > 0
284 akbd_cnattach();
285 #endif
286 #if NADBKBD > 0
287 adbkbd_cnattach();
288 #endif
289 } else {
290 /* must be USB */
291 printf("No ADB support present, assuming USB "
292 "keyboard\n");
293 #if NUKBD > 0
294 ukbd_cnattach();
295 #endif
296 }
297 goto kbd_found;
298 }
299
300 /*
301 * stdin is /psuedo-hid/keyboard. Test `adb-kbd-ihandle and
302 * `usb-kbd-ihandles to figure out the real keyboard(s).
303 *
304 * XXX This must be called before pmap_bootstrap().
305 */
306
307 #if NUKBD > 0
308 if (OF_call_method("`usb-kbd-ihandles", kstdin, 0, 1, &ukbds) >= 0 &&
309 ukbds != NULL && ukbds->ihandle != 0 &&
310 OF_instance_to_package(ukbds->ihandle) != -1) {
311 printf("usb-kbd-ihandles matches\n");
312 printf("console keyboard type: USB\n");
313 ukbd_cnattach();
314 goto kbd_found;
315 }
316 /* Try old method name. */
317 if (OF_call_method("`usb-kbd-ihandle", kstdin, 0, 1, &ukbd) >= 0 &&
318 ukbd != 0 &&
319 OF_instance_to_package(ukbd) != -1) {
320 printf("usb-kbd-ihandle matches\n");
321 printf("console keyboard type: USB\n");
322 kstdin = ukbd;
323 ukbd_cnattach();
324 goto kbd_found;
325 }
326 #endif
327
328 #if (NAKBD > 0) || (NADBKBD > 0)
329 if (OF_call_method("`adb-kbd-ihandle", kstdin, 0, 1, &akbd) >= 0 &&
330 akbd != 0 &&
331 OF_instance_to_package(akbd) != -1) {
332 printf("adb-kbd-ihandle matches\n");
333 printf("console keyboard type: ADB\n");
334 kstdin = akbd;
335 #if NAKBD > 0
336 akbd_cnattach();
337 #endif
338 #if NADBKBD > 0
339 adbkbd_cnattach();
340 #endif
341 goto kbd_found;
342 }
343 #endif
344
345 #if NUKBD > 0
346 /*
347 * XXX Old firmware does not have `usb-kbd-ihandles method. Assume
348 * XXX USB keyboard anyway.
349 */
350 printf("defaulting to USB...");
351 printf("console keyboard type: USB\n");
352 ukbd_cnattach();
353 goto kbd_found;
354 #endif
355
356 /*
357 * No keyboard is found. Just return.
358 */
359 printf("no console keyboard\n");
360 return;
361
362 kbd_found:;
363 #if NAKBD + NUKBD + NADBKBD + NPCKBC > 0
364 /*
365 * XXX This is a little gross, but we don't get to call
366 * XXX wskbd_cnattach() twice.
367 */
368 ofkbd_ihandle = kstdin;
369 #if NWSDISPLAY > 0
370 wsdisplay_set_cons_kbd(ofkbd_cngetc, NULL, NULL);
371 #endif
372 #endif
373 }
374
375 /*
376 * Bootstrap console keyboard routines, using OpenFirmware I/O.
377 */
378 int
379 ofkbd_cngetc(dev_t dev)
380 {
381 u_char c = '\0';
382 int len;
383
384 do {
385 len = OF_read(ofkbd_ihandle, &c, 1);
386 } while (len != 1);
387
388 return c;
389 }
390
391 void
392 ofwoea_consinit(void)
393 {
394 static int initted = 0;
395
396 if (initted)
397 return;
398
399 initted = 1;
400 cninit();
401 }
402