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