Home | History | Annotate | Line # | Download | only in dev
consinit.c revision 1.5
      1 /*	$NetBSD: consinit.c,v 1.5 2000/05/19 05:26:17 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 
     55 #include <dev/cons.h>
     56 
     57 #include <sparc64/sparc64/vaddrs.h>
     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 
    112 	while ((l = OF_read(stdin, &ch, 1)) != 1)
    113 		/* void */;
    114 	if (ch == '\r')
    115 		ch = '\n';
    116 	return ch;
    117 }
    118 
    119 static void
    120 prom_cninit(cn)
    121 	struct consdev *cn;
    122 {
    123 	if (!stdin) stdin = OF_stdin();
    124 	if (!stdout) stdout = OF_stdout();
    125 }
    126 
    127 /*
    128  * PROM console output putchar.
    129  */
    130 static void
    131 prom_cnputc(dev, c)
    132 	dev_t dev;
    133 	int c;
    134 {
    135 	int s;
    136 	char c0 = (c & 0x7f);
    137 
    138 #if 0
    139 	if (!stdout) stdout = OF_stdout();
    140 #endif
    141 	s = splhigh();
    142 	OF_write(stdout, &c0, 1);
    143 	splx(s);
    144 }
    145 
    146 void
    147 prom_cnpollc(dev, on)
    148 	dev_t dev;
    149 	int on;
    150 {
    151 	if (on) {
    152                 /* Entering debugger. */
    153 #if NFB > 0
    154                 fb_unblank();
    155 #endif
    156 	} else {
    157                 /* Resuming kernel. */
    158 	}
    159 #if NPCONS > 0
    160 	pcons_cnpollc(dev, on);
    161 #endif
    162 }
    163 
    164 /*****************************************************************/
    165 
    166 #ifdef	DEBUG
    167 #define	DBPRINT(x)	printf x
    168 #else
    169 #define	DBPRINT(x)
    170 #endif
    171 
    172 /*
    173  * This function replaces sys/dev/cninit.c
    174  * Determine which device is the console using
    175  * the PROM "input source" and "output sink".
    176  */
    177 void
    178 consinit()
    179 {
    180 	register int chosen;
    181 	char buffer[128];
    182 	extern int stdinnode, fbnode;
    183 	char *consname = "unknown";
    184 
    185 	DBPRINT(("consinit()\r\n"));
    186 	if (cn_tab != &consdev_prom) return;
    187 
    188 	DBPRINT(("setting up stdin\r\n"));
    189 	chosen = OF_finddevice("/chosen");
    190 	OF_getprop(chosen, "stdin",  &stdin, sizeof(stdin));
    191 	DBPRINT(("stdin instance = %x\r\n", stdin));
    192 
    193 	if ((stdinnode = OF_instance_to_package(stdin)) == 0) {
    194 		printf("WARNING: no PROM stdin\n");
    195 	}
    196 
    197 	DBPRINT(("setting up stdout\r\n"));
    198 	OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
    199 
    200 	DBPRINT(("stdout instance = %x\r\n", stdout));
    201 
    202 	if ((fbnode = OF_instance_to_package(stdout)) == 0)
    203 		printf("WARNING: no PROM stdout\n");
    204 
    205 	DBPRINT(("stdout package = %x\r\n", fbnode));
    206 
    207 	if (stdinnode && (OF_getproplen(stdinnode,"keyboard") >= 0)) {
    208 #if NKBD > 0
    209 		printf("cninit: kdb/display not configured\n");
    210 #endif
    211 		consname = "keyboard/display";
    212 	} else if (fbnode &&
    213 		   (OF_instance_to_path(stdinnode, buffer, sizeof(buffer) >= 0))) {
    214 		consname = buffer;
    215 	}
    216 	printf("console is %s\n", consname);
    217 
    218 	/* Initialize PROM console */
    219 	(*cn_tab->cn_probe)(cn_tab);
    220 	(*cn_tab->cn_init)(cn_tab);
    221 }
    222 
    223