#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ofw_consinit.c,v 1.28 2026/06/30 22:28:00 rkujawa Exp $");
#include "adb.h"
#include "adbkbd.h"
#include "akbd.h"
#include "isa.h"
#include "ofb.h"
#include "pckbc.h"
#include "ukbd.h"
#include "wsdisplay.h"
#include "zsc.h"
#include "zstty.h"
#include "opt_pscons.h"
#include <sys/param.h>
#include <sys/buf.h>
#include <sys/tty.h>
#include <prop/proplib.h>
#include <machine/autoconf.h>
#include <machine/trap.h>
#include <sys/bus.h>
#include <powerpc/ofw_cons.h>
#include <powerpc/ofw_machdep.h>
#include <dev/cons.h>
#include <dev/ofw/openfirm.h>
#include <dev/wscons/wsksymvar.h>
#include <dev/wscons/wscons_callbacks.h>
#if NZSC > 0
#include <machine/z8530var.h>
#endif
#if (NADB > 0)
#include <macppc/dev/adbvar.h>
#endif
#if (NUKBD > 0)
#include <dev/usb/ukbdvar.h>
struct usb_kbd_ihandles {
struct usb_kbd_ihandles *next;
int ihandle;
};
#endif
#if (NZSTTY > 0)
#include <dev/ic/z8530reg.h>
extern struct consdev consdev_zs;
#endif
#ifdef PSC_CONSOLE
#include <powerpc/mpc5200/pscvar.h>
#endif
#if (NPCKBC > 0)
#include <dev/isa/isareg.h>
#include <dev/ic/i8042reg.h>
#include <dev/ic/pckbcvar.h>
#endif
extern int console_node;
int ofkbd_ihandle = -1;
static void ofwoea_cnprobe_keyboard(void);
#ifdef OFDEBUG
#define OFPRINTF ofprint
#else
#define OFPRINTF while(0) printf
#endif
bool ofwoea_use_serial_console;
static struct consdev *selected_serial_consdev;
static int (*selected_keyboard)(void);
#if NPCKBC > 0
static int
ofwoea_pckbd_cnattach(void)
{
return pckbc_cnattach(&genppc_isa_io_space_tag, IO_KBD, KBCMDP,
PCKBC_KBD_SLOT, 0);
}
#endif
void
ofwoea_cnprobe(void)
{
char name[32];
OFPRINTF("console node: %08x\n", console_node);
if (console_node == -1)
return;
memset(name, 0, sizeof(name));
if (OF_getprop(console_node, "device_type", name, sizeof(name)) == -1)
return;
OFPRINTF("console type: %s\n", name);
if (strcmp(name, "serial") == 0) {
ofwoea_use_serial_console = true;
#ifdef PMAC_G5
extern struct consdev failsafe_cons;
selected_serial_consdev = &failsafe_cons;
aprint_verbose("Early G5 console selected "
"(keeping OF console for now)\n");
return;
#endif
#ifdef PSC_CONSOLE
selected_serial_consdev = &psccons;
return;
#endif
#if (NZSTTY > 0) && !defined(MAMBO)
OF_getprop(console_node, "name", name, sizeof(name));
if (strcmp(name, "ch-a") == 0 || strcmp(name, "ch-b") == 0) {
selected_serial_consdev = &consdev_zs;
}
return;
#endif
return;
}
ofwoea_cnprobe_keyboard();
}
static void
ofwoea_cnprobe_keyboard(void)
{
extern int ofw_stdin;
int node, kstdin = ofw_stdin;
char name[16];
#if (NAKBD > 0) || (NADBKBD > 0)
int akbd;
#endif
#if NUKBD > 0
struct usb_kbd_ihandles *ukbds;
int ukbd;
#endif
node = OF_instance_to_package(kstdin);
memset(name, 0, sizeof(name));
OF_getprop(node, "name", name, sizeof(name));
if (strcmp(name, "keyboard") != 0) {
ofprint("WARNING: stdin is not a keyboard: %s\n", name);
return;
}
memset(name, 0, sizeof(name));
OF_getprop(OF_parent(node), "name", name, sizeof(name));
#if NAKBD > 0
if (strcmp(name, "adb") == 0) {
ofprint("console keyboard type: ADB\n");
selected_keyboard = akbd_cnattach;
goto kbd_found;
}
#endif
#if NADBKBD > 0
if (strcmp(name, "adb") == 0) {
ofprint("console keyboard type: ADB\n");
selected_keyboard = adbkbd_cnattach;
goto kbd_found;
}
#endif
#if NPCKBC > 0
if (strcmp(name, "isa") == 0) {
ofprint("console keyboard type: PC Keyboard\n");
selected_keyboard = ofwoea_pckbd_cnattach;
goto kbd_found;
}
#endif
if (strcmp(name, "pseudo-hid") == 0) {
int adb_node;
adb_node = OF_finddevice("/pci/mac-io/via-pmu/adb");
if (adb_node > 0) {
ofprint("ADB support found\n");
#if NAKBD > 0
selected_keyboard = akbd_cnattach;
#endif
#if NADBKBD > 0
selected_keyboard = adbkbd_cnattach;
#endif
} else {
ofprint("No ADB support present, assuming USB "
"keyboard\n");
#if NUKBD > 0
selected_keyboard = ukbd_cnattach;
#endif
}
goto kbd_found;
}
#if NUKBD > 0
if (OF_call_method("`usb-kbd-ihandles", kstdin, 0, 1, &ukbds) >= 0 &&
ukbds != NULL && ukbds->ihandle != 0 &&
OF_instance_to_package(ukbds->ihandle) != -1) {
ofprint("usb-kbd-ihandles matches\n");
ofprint("console keyboard type: USB\n");
selected_keyboard = ukbd_cnattach;
goto kbd_found;
}
if (OF_call_method("`usb-kbd-ihandle", kstdin, 0, 1, &ukbd) >= 0 &&
ukbd != 0 &&
OF_instance_to_package(ukbd) != -1) {
ofprint("usb-kbd-ihandle matches\n");
ofprint("console keyboard type: USB\n");
kstdin = ukbd;
selected_keyboard = ukbd_cnattach;
goto kbd_found;
}
#endif
#if (NAKBD > 0) || (NADBKBD > 0)
if (OF_call_method("`adb-kbd-ihandle", kstdin, 0, 1, &akbd) >= 0 &&
akbd != 0 &&
OF_instance_to_package(akbd) != -1) {
ofprint("adb-kbd-ihandle matches\n");
ofprint("console keyboard type: ADB\n");
kstdin = akbd;
#if NAKBD > 0
selected_keyboard = akbd_cnattach;
#endif
#if NADBKBD > 0
selected_keyboard = adbkbd_cnattach;
#endif
goto kbd_found;
}
#endif
#if NUKBD > 0
ofprint("defaulting to USB...");
ofprint("console keyboard type: USB\n");
selected_keyboard = ukbd_cnattach;
goto kbd_found;
#endif
ofprint("no console keyboard\n");
return;
kbd_found:
ofkbd_ihandle = kstdin;
}
int
ofkbd_cngetc(dev_t dev)
{
u_char c = '\0';
int len;
KASSERT(ofkbd_ihandle != -1);
do {
len = OF_read(ofkbd_ihandle, &c, 1);
} while (len != 1);
return c;
}
void
cninit(void)
{
if (ofwoea_use_serial_console) {
if (selected_serial_consdev != NULL) {
cn_tab = selected_serial_consdev;
(*cn_tab->cn_probe)(cn_tab);
(*cn_tab->cn_init)(cn_tab);
}
return;
}
#if NWSDISPLAY > 0
rascons_cnattach();
#endif
if (selected_keyboard != NULL) {
(*selected_keyboard)();
#if NWSDISPLAY > 0
wsdisplay_set_cons_kbd(ofkbd_cngetc, NULL, NULL);
#endif
}
}
void
ofwoea_consinit(void)
{
static int initted = 0;
if (initted)
return;
initted = 1;
cninit();
}