xref: /netbsd-src/sys/arch/sparc64/dev/consinit.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: consinit.c,v 1.7 2000/07/10 20:24:23 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/dev/cons.h>
58 
59 static void prom_cnprobe __P((struct consdev *));
60 static void prom_cninit __P((struct consdev *));
61 int  prom_cngetc __P((dev_t));
62 static void prom_cnputc __P((dev_t, int));
63 static void prom_cnpollc __P((dev_t, int));
64 static void prom_cnputc __P((dev_t, int));
65 
66 int stdin = NULL, stdout = NULL;
67 
68 /*
69  * The console is set to this one initially,
70  * which lets us use the PROM until consinit()
71  * is called to select a real console.
72  */
73 struct consdev consdev_prom = {
74 	prom_cnprobe,
75 	prom_cninit,
76 	prom_cngetc,
77 	prom_cnputc,
78 	prom_cnpollc,
79 	NULL,
80 };
81 
82 /*
83  * The console table pointer is statically initialized
84  * to point to the PROM (output only) table, so that
85  * early calls to printf will work.
86  */
87 struct consdev *cn_tab = &consdev_prom;
88 
89 void
90 prom_cnprobe(cd)
91 	struct consdev *cd;
92 {
93 #if NPCONS > 0
94 	int maj;
95 
96 	for (maj = 0; maj < nchrdev; maj++)
97 		if (cdevsw[maj].d_open == pconsopen)
98 			break;
99 	cd->cn_dev = makedev(maj, 0);
100 	cd->cn_pri = CN_INTERNAL;
101 #endif
102 }
103 
104 int
105 prom_cngetc(dev)
106 	dev_t dev;
107 {
108 	unsigned char ch = '\0';
109 	int l;
110 #ifdef DDB
111 	static int nplus = 0;
112 #endif
113 
114 	while ((l = OF_read(stdin, &ch, 1)) != 1)
115 		/* void */;
116 #ifdef DDB
117 	if (ch == '+') {
118 		if (nplus++ > 3) Debugger();
119 	} else nplus = 0;
120 #endif
121 	if (ch == '\r')
122 		ch = '\n';
123 	return ch;
124 }
125 
126 static void
127 prom_cninit(cn)
128 	struct consdev *cn;
129 {
130 	if (!stdin) stdin = OF_stdin();
131 	if (!stdout) stdout = OF_stdout();
132 }
133 
134 /*
135  * PROM console output putchar.
136  */
137 static void
138 prom_cnputc(dev, c)
139 	dev_t dev;
140 	int c;
141 {
142 	int s;
143 	char c0 = (c & 0x7f);
144 
145 #if 0
146 	if (!stdout) stdout = OF_stdout();
147 #endif
148 	s = splhigh();
149 	OF_write(stdout, &c0, 1);
150 	splx(s);
151 }
152 
153 void
154 prom_cnpollc(dev, on)
155 	dev_t dev;
156 	int on;
157 {
158 	if (on) {
159                 /* Entering debugger. */
160 #if NFB > 0
161                 fb_unblank();
162 #endif
163 	} else {
164                 /* Resuming kernel. */
165 	}
166 #if NPCONS > 0
167 	pcons_cnpollc(dev, on);
168 #endif
169 }
170 
171 /*****************************************************************/
172 
173 #ifdef	DEBUG
174 #define	DBPRINT(x)	printf x
175 #else
176 #define	DBPRINT(x)
177 #endif
178 
179 /*
180  * This function replaces sys/dev/cninit.c
181  * Determine which device is the console using
182  * the PROM "input source" and "output sink".
183  */
184 void
185 consinit()
186 {
187 	register int chosen;
188 	char buffer[128];
189 	extern int stdinnode, fbnode;
190 	char *consname = "unknown";
191 
192 	DBPRINT(("consinit()\r\n"));
193 	if (cn_tab != &consdev_prom) return;
194 
195 	DBPRINT(("setting up stdin\r\n"));
196 	chosen = OF_finddevice("/chosen");
197 	OF_getprop(chosen, "stdin",  &stdin, sizeof(stdin));
198 	DBPRINT(("stdin instance = %x\r\n", stdin));
199 
200 	if ((stdinnode = OF_instance_to_package(stdin)) == 0) {
201 		printf("WARNING: no PROM stdin\n");
202 	}
203 
204 	DBPRINT(("setting up stdout\r\n"));
205 	OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
206 
207 	DBPRINT(("stdout instance = %x\r\n", stdout));
208 
209 	if ((fbnode = OF_instance_to_package(stdout)) == 0)
210 		printf("WARNING: no PROM stdout\n");
211 
212 	DBPRINT(("stdout package = %x\r\n", fbnode));
213 
214 	if (stdinnode && (OF_getproplen(stdinnode,"keyboard") >= 0)) {
215 #if NKBD > 0
216 		printf("cninit: kdb/display not configured\n");
217 #endif
218 		consname = "keyboard/display";
219 	} else if (fbnode &&
220 		   (OF_instance_to_path(stdinnode, buffer, sizeof(buffer) >= 0))) {
221 		consname = buffer;
222 	}
223 	printf("console is %s\n", consname);
224 
225 	/* Initialize PROM console */
226 	(*cn_tab->cn_probe)(cn_tab);
227 	(*cn_tab->cn_init)(cn_tab);
228 }
229 
230