1 /* $OpenBSD: consinit.c,v 1.6 2002/01/25 03:36:25 jason Exp $ */ 2 /* $NetBSD: consinit.c,v 1.9 2000/10/20 05:32:35 mrg Exp $ */ 3 4 /*- 5 * Copyright (c) 1999 Eduardo E. Horvath 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include "pcons.h" 33 #include "ukbd.h" 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/conf.h> 38 #include <sys/device.h> 39 #include <sys/file.h> 40 #include <sys/ioctl.h> 41 #include <sys/kernel.h> 42 #include <sys/proc.h> 43 #include <sys/tty.h> 44 #include <sys/time.h> 45 #include <sys/syslog.h> 46 47 #include <machine/autoconf.h> 48 #include <machine/openfirm.h> 49 #include <machine/bsd_openprom.h> 50 #include <machine/conf.h> 51 #include <machine/cpu.h> 52 #include <machine/eeprom.h> 53 #include <machine/psl.h> 54 #include <machine/z8530var.h> 55 #include <machine/sparc64.h> 56 57 #include <dev/cons.h> 58 59 #include <sparc64/dev/cons.h> 60 61 #include <dev/usb/ukbdvar.h> 62 63 static void prom_cnprobe __P((struct consdev *)); 64 static void prom_cninit __P((struct consdev *)); 65 int prom_cngetc __P((dev_t)); 66 static void prom_cnputc __P((dev_t, int)); 67 static void prom_cnpollc __P((dev_t, int)); 68 static void prom_cnputc __P((dev_t, int)); 69 70 int stdin = NULL, stdout = NULL; 71 72 /* 73 * The console is set to this one initially, 74 * which lets us use the PROM until consinit() 75 * is called to select a real console. 76 */ 77 struct consdev consdev_prom = { 78 prom_cnprobe, 79 prom_cninit, 80 prom_cngetc, 81 prom_cnputc, 82 prom_cnpollc, 83 NULL, 84 }; 85 86 /* 87 * The console table pointer is statically initialized 88 * to point to the PROM (output only) table, so that 89 * early calls to printf will work. 90 */ 91 struct consdev *cn_tab = &consdev_prom; 92 93 void 94 prom_cnprobe(cd) 95 struct consdev *cd; 96 { 97 #if NPCONS > 0 98 int maj; 99 100 for (maj = 0; maj < nchrdev; maj++) 101 if (cdevsw[maj].d_open == pconsopen) 102 break; 103 cd->cn_dev = makedev(maj, 0); 104 cd->cn_pri = CN_INTERNAL; 105 #endif 106 } 107 108 int 109 prom_cngetc(dev) 110 dev_t dev; 111 { 112 unsigned char ch = '\0'; 113 int l; 114 #ifdef DDB 115 static int nplus = 0; 116 #endif 117 118 while ((l = OF_read(stdin, &ch, 1)) != 1) 119 /* void */; 120 #ifdef DDB 121 if (ch == '+') { 122 if (nplus++ > 3) Debugger(); 123 } else nplus = 0; 124 #endif 125 if (ch == '\r') 126 ch = '\n'; 127 return ch; 128 } 129 130 static void 131 prom_cninit(cn) 132 struct consdev *cn; 133 { 134 if (!stdin) stdin = OF_stdin(); 135 if (!stdout) stdout = OF_stdout(); 136 } 137 138 /* 139 * PROM console output putchar. 140 */ 141 static void 142 prom_cnputc(dev, c) 143 dev_t dev; 144 int c; 145 { 146 int s; 147 char c0 = (c & 0x7f); 148 149 #if 0 150 if (!stdout) stdout = OF_stdout(); 151 #endif 152 s = splhigh(); 153 OF_write(stdout, &c0, 1); 154 splx(s); 155 } 156 157 void 158 prom_cnpollc(dev, on) 159 dev_t dev; 160 int on; 161 { 162 if (on) { 163 /* Entering debugger. */ 164 #if NFB > 0 165 fb_unblank(); 166 #endif 167 } else { 168 /* Resuming kernel. */ 169 } 170 #if NPCONS > 0 171 pcons_cnpollc(dev, on); 172 #endif 173 } 174 175 /*****************************************************************/ 176 177 #ifdef DEBUG 178 #define DBPRINT(x) prom_printf x 179 #else 180 #define DBPRINT(x) 181 #endif 182 183 /* 184 * This function replaces sys/dev/cninit.c 185 * Determine which device is the console using 186 * the PROM "input source" and "output sink". 187 */ 188 void 189 consinit() 190 { 191 register int chosen; 192 char buffer[128]; 193 extern int stdinnode, fbnode; 194 char *consname = "unknown"; 195 196 DBPRINT(("consinit()\r\n")); 197 if (cn_tab != &consdev_prom) return; 198 199 DBPRINT(("setting up stdin\r\n")); 200 chosen = OF_finddevice("/chosen"); 201 OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)); 202 DBPRINT(("stdin instance = %x\r\n", stdin)); 203 204 if ((stdinnode = OF_instance_to_package(stdin)) == 0) { 205 printf("WARNING: no PROM stdin\n"); 206 } 207 #if NUKBD > 0 208 else { 209 if (OF_getprop(stdinnode, "compatible", buffer, 210 sizeof(buffer)) != -1 && strncmp("usb", buffer, 3) == 0) 211 ukbd_cnattach(); 212 } 213 #endif 214 215 DBPRINT(("setting up stdout\r\n")); 216 OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)); 217 218 DBPRINT(("stdout instance = %x\r\n", stdout)); 219 220 if ((fbnode = OF_instance_to_package(stdout)) == 0) 221 printf("WARNING: no PROM stdout\n"); 222 223 DBPRINT(("stdout package = %x\r\n", fbnode)); 224 225 if (stdinnode && (OF_getproplen(stdinnode,"keyboard") >= 0)) { 226 consname = "keyboard/display"; 227 } else if (fbnode && 228 (OF_instance_to_path(stdin, buffer, sizeof(buffer)) >= 0)) { 229 consname = buffer; 230 } 231 printf("console is %s\n", consname); 232 233 /* Initialize PROM console */ 234 (*cn_tab->cn_probe)(cn_tab); 235 (*cn_tab->cn_init)(cn_tab); 236 } 237 238