xref: /netbsd/sys/arch/sparc64/dev/consinit.c (revision c4a72b64)
1 /*	$NetBSD: consinit.c,v 1.12 2002/09/06 13:18:43 gehenna 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/cpu.h>
50 #include <machine/eeprom.h>
51 #include <machine/psl.h>
52 #include <machine/z8530var.h>
53 #include <machine/sparc64.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 	extern const struct cdevsw pcons_cdevsw;
96 
97 	maj = cdevsw_lookup_major(&pcons_cdevsw);
98 	cd->cn_dev = makedev(maj, 0);
99 	cd->cn_pri = CN_INTERNAL;
100 #endif
101 }
102 
103 int
104 prom_cngetc(dev)
105 	dev_t dev;
106 {
107 	unsigned char ch = '\0';
108 	int l;
109 #ifdef DDB
110 	static int nplus = 0;
111 #endif
112 
113 	while ((l = OF_read(stdin, &ch, 1)) != 1)
114 		/* void */;
115 #ifdef DDB
116 	if (ch == '+') {
117 		if (nplus++ > 3) Debugger();
118 	} else nplus = 0;
119 #endif
120 	if (ch == '\r')
121 		ch = '\n';
122 	return ch;
123 }
124 
125 static void
126 prom_cninit(cn)
127 	struct consdev *cn;
128 {
129 	if (!stdin) stdin = OF_stdin();
130 	if (!stdout) stdout = OF_stdout();
131 }
132 
133 /*
134  * PROM console output putchar.
135  */
136 static void
137 prom_cnputc(dev, c)
138 	dev_t dev;
139 	int c;
140 {
141 	int s;
142 	char c0 = (c & 0x7f);
143 
144 #if 0
145 	if (!stdout) stdout = OF_stdout();
146 #endif
147 	s = splhigh();
148 	OF_write(stdout, &c0, 1);
149 	splx(s);
150 }
151 
152 void
153 prom_cnpollc(dev, on)
154 	dev_t dev;
155 	int on;
156 {
157 	if (on) {
158                 /* Entering debugger. */
159 #if NFB > 0
160                 fb_unblank();
161 #endif
162 	} else {
163                 /* Resuming kernel. */
164 	}
165 #if NPCONS > 0
166 	pcons_cnpollc(dev, on);
167 #endif
168 }
169 
170 /*****************************************************************/
171 
172 #ifdef	DEBUG
173 #define	DBPRINT(x)	prom_printf x
174 #else
175 #define	DBPRINT(x)
176 #endif
177 
178 /*
179  * This function replaces sys/dev/cninit.c
180  * Determine which device is the console using
181  * the PROM "input source" and "output sink".
182  */
183 void
184 consinit()
185 {
186 	register int chosen;
187 	char buffer[128];
188 	extern int stdinnode, fbnode;
189 	char *consname = "unknown";
190 
191 	DBPRINT(("consinit()\r\n"));
192 	if (cn_tab != &consdev_prom) return;
193 
194 	DBPRINT(("setting up stdin\r\n"));
195 	chosen = OF_finddevice("/chosen");
196 	DBPRINT(("chosen = %x, stdin @ %p\r\n", chosen, &stdin));
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(("stdin node = %x\r\n", stdinnode));
205 	DBPRINT(("setting up stdout\r\n"));
206 	OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
207 
208 	DBPRINT(("stdout instance = %x\r\n", stdout));
209 
210 	if ((fbnode = OF_instance_to_package(stdout)) == 0)
211 		printf("WARNING: no PROM stdout\n");
212 
213 	DBPRINT(("stdout package = %x\r\n", fbnode));
214 	DBPRINT(("buffer @ %p\r\n", buffer));
215 
216 	if (stdinnode && (OF_getproplen(stdinnode,"keyboard") >= 0)) {
217 #if NKBD > 0
218 		printf("cninit: kdb/display not configured\n");
219 #endif
220 		consname = "keyboard/display";
221 	} else if (fbnode &&
222 		(OF_instance_to_path(stdin, buffer, sizeof(buffer)) >= 0)) {
223 		consname = buffer;
224 	}
225 	printf("console is %s\n", consname);
226 
227 	/* Initialize PROM console */
228 	(*cn_tab->cn_probe)(cn_tab);
229 	(*cn_tab->cn_init)(cn_tab);
230 }
231 
232