consinit.c revision 1.16 1 /* $NetBSD: consinit.c,v 1.16 2004/03/19 21:10:31 petrov 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 <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: consinit.c,v 1.16 2004/03/19 21:10:31 petrov Exp $");
33
34 #include "opt_ddb.h"
35 #include "pcons.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/conf.h>
40 #include <sys/device.h>
41 #include <sys/file.h>
42 #include <sys/ioctl.h>
43 #include <sys/kernel.h>
44 #include <sys/proc.h>
45 #include <sys/tty.h>
46 #include <sys/time.h>
47 #include <sys/syslog.h>
48
49 #include <machine/autoconf.h>
50 #include <machine/openfirm.h>
51 #include <machine/bsd_openprom.h>
52 #include <machine/cpu.h>
53 #include <machine/eeprom.h>
54 #include <machine/psl.h>
55 #include <machine/z8530var.h>
56 #include <machine/sparc64.h>
57
58 #include <dev/cons.h>
59
60 #include <sparc64/dev/cons.h>
61
62 static void prom_cnprobe __P((struct consdev *));
63 static void prom_cninit __P((struct consdev *));
64 int prom_cngetc __P((dev_t));
65 static void prom_cnputc __P((dev_t, int));
66 static void prom_cnpollc __P((dev_t, int));
67 static void prom_cnputc __P((dev_t, int));
68
69 int stdin = 0, stdout = 0;
70
71 /*
72 * The console is set to this one initially,
73 * which lets us use the PROM until consinit()
74 * is called to select a real console.
75 */
76 struct consdev consdev_prom = {
77 prom_cnprobe,
78 prom_cninit,
79 prom_cngetc,
80 prom_cnputc,
81 prom_cnpollc,
82 NULL,
83 };
84
85 /*
86 * The console table pointer is statically initialized
87 * to point to the PROM (output only) table, so that
88 * early calls to printf will work.
89 */
90 struct consdev *cn_tab = &consdev_prom;
91
92 void
93 prom_cnprobe(cd)
94 struct consdev *cd;
95 {
96 #if NPCONS > 0
97 int maj;
98 extern const struct cdevsw pcons_cdevsw;
99
100 maj = cdevsw_lookup_major(&pcons_cdevsw);
101 cd->cn_dev = makedev(maj, 0);
102 cd->cn_pri = CN_INTERNAL;
103 #endif
104 }
105
106 int
107 prom_cngetc(dev)
108 dev_t dev;
109 {
110 unsigned char ch = '\0';
111 int l;
112 #ifdef DDB
113 static int nplus = 0;
114 #endif
115
116 while ((l = OF_read(stdin, &ch, 1)) != 1)
117 /* void */;
118 #ifdef DDB
119 if (ch == '+') {
120 if (nplus++ > 3) Debugger();
121 } else nplus = 0;
122 #endif
123 if (ch == '\r')
124 ch = '\n';
125 return ch;
126 }
127
128 static void
129 prom_cninit(cn)
130 struct consdev *cn;
131 {
132 if (!stdin) stdin = OF_stdin();
133 if (!stdout) stdout = OF_stdout();
134 }
135
136 /*
137 * PROM console output putchar.
138 */
139 static void
140 prom_cnputc(dev, c)
141 dev_t dev;
142 int c;
143 {
144 int s;
145 char c0 = (c & 0x7f);
146
147 #if 0
148 if (!stdout) stdout = OF_stdout();
149 #endif
150 s = splhigh();
151 OF_write(stdout, &c0, 1);
152 splx(s);
153 }
154
155 void
156 prom_cnpollc(dev, on)
157 dev_t dev;
158 int on;
159 {
160 if (on) {
161 /* Entering debugger. */
162 #if NFB > 0
163 fb_unblank();
164 #endif
165 } else {
166 /* Resuming kernel. */
167 }
168 #if NPCONS > 0
169 pcons_cnpollc(dev, on);
170 #endif
171 }
172
173 /*****************************************************************/
174
175 #ifdef DEBUG
176 #define DBPRINT(x) prom_printf x
177 #else
178 #define DBPRINT(x)
179 #endif
180
181 int prom_stdin_node;
182 int prom_stdout_node;
183
184 /*
185 * This function replaces sys/dev/cninit.c
186 * Determine which device is the console using
187 * the PROM "input source" and "output sink".
188 */
189 void
190 consinit()
191 {
192 int chosen;
193 char buffer[128];
194 char *consname = "unknown";
195
196 DBPRINT(("consinit()\r\n"));
197
198 if (cn_tab != &consdev_prom)
199 return;
200
201 chosen = OF_finddevice("/chosen");
202
203 DBPRINT(("setting up stdin\r\n"));
204 DBPRINT(("chosen = %x, stdin @ %p\r\n", chosen, &stdin));
205 OF_getprop(chosen, "stdin", &stdin, sizeof(stdin));
206 DBPRINT(("stdin instance = %x\r\n", stdin));
207
208 if ((prom_stdin_node = OF_instance_to_package(stdin)) == 0) {
209 printf("WARNING: no PROM stdin\n");
210 }
211
212 DBPRINT(("stdin node = %x\r\n", prom_stdin_node));
213
214 DBPRINT(("setting up stdout\r\n"));
215 OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
216 DBPRINT(("stdout instance = %x\r\n", stdout));
217
218 if ((prom_stdout_node = OF_instance_to_package(stdout)) == 0)
219 printf("WARNING: no PROM stdout\n");
220
221 DBPRINT(("stdout package = %x\r\n", prom_stdout_node));
222
223 DBPRINT(("buffer @ %p\r\n", buffer));
224
225 if (prom_stdin_node != 0 &&
226 (OF_getproplen(prom_stdin_node, "keyboard") >= 0)) {
227 #if NKBD > 0
228 printf("cninit: kdb/display not configured\n");
229 #endif
230 consname = "keyboard/display";
231 } else if (prom_stdout_node != 0 &&
232 (OF_instance_to_path(stdin, buffer, sizeof(buffer)) >= 0)) {
233 consname = buffer;
234 }
235 printf("console is %s\n", consname);
236
237 /* Initialize PROM console */
238 (*cn_tab->cn_probe)(cn_tab);
239 (*cn_tab->cn_init)(cn_tab);
240 }
241
242