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