consinit.c revision 1.8 1 /* $NetBSD: consinit.c,v 1.8 2012/07/30 17:27:20 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2001 Matthew Fredette
5 * Copyright (c) 1999 Eduardo E. Horvath
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: consinit.c,v 1.8 2012/07/30 17:27:20 christos Exp $");
34
35 #include "opt_ddb.h"
36 #include "opt_kgdb.h"
37 #include "pcons.h"
38 #include "kbd.h"
39 #include "zs.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/conf.h>
44 #include <sys/device.h>
45 #include <sys/file.h>
46 #include <sys/ioctl.h>
47 #include <sys/kernel.h>
48 #include <sys/proc.h>
49 #include <sys/tty.h>
50 #include <sys/time.h>
51 #include <sys/syslog.h>
52 #include <sys/kgdb.h>
53
54 #include <machine/autoconf.h>
55 #include <machine/promlib.h>
56 #include <machine/cpu.h>
57 #include <machine/eeprom.h>
58 #include <machine/psl.h>
59 #include <machine/z8530var.h>
60
61 #include <dev/cons.h>
62
63 #include <sun2/dev/cons.h>
64
65 static void prom_cnprobe(struct consdev *);
66 static void prom_cninit(struct consdev *);
67 int prom_cngetc(dev_t);
68 static void prom_cnputc(dev_t, int);
69 static void prom_cnpollc(dev_t, int);
70 static void prom_cnputc(dev_t, int);
71
72 #ifdef PROM_OBP_V2
73 /*
74 * The following several variables are related to
75 * the configuration process, and are used in initializing
76 * the machine.
77 */
78 int prom_stdin_node; /* node ID of ROM's console input device */
79 int prom_stdout_node; /* node ID of ROM's console output device */
80 char prom_stdin_args[16];
81 char prom_stdout_args[16];
82 #endif /* PROM_OBP_V2 */
83
84 /*
85 * The console is set to this one initially,
86 * which lets us use the PROM until consinit()
87 * is called to select a real console.
88 */
89 struct consdev consdev_prom = {
90 prom_cnprobe,
91 prom_cninit,
92 prom_cngetc,
93 prom_cnputc,
94 prom_cnpollc,
95 NULL,
96 };
97
98 /*
99 * The console table pointer is statically initialized
100 * to point to the PROM (output only) table, so that
101 * early calls to printf will work. This has been moved
102 * to cpu_startup()
103 */
104 #if 0
105 struct consdev *cn_tab = &consdev_prom;
106 #endif
107
108 void
109 prom_cnprobe(struct consdev *cd)
110 {
111 #if NPCONS > 0
112 extern const struct cdevsw pcons_cdevsw;
113
114 cd->cn_dev = makedev(cdevsw_lookup_major(&pcons_cdevsw), 0);
115 cd->cn_pri = CN_INTERNAL;
116 #endif
117 }
118
119 int
120 prom_cngetc(dev_t dev)
121 {
122 int ch;
123 #ifdef DDB
124 static int nplus = 0;
125 #endif
126
127 ch = prom_getchar();
128 #ifdef DDB
129 if (ch == '+') {
130 if (nplus++ > 3) Debugger();
131 } else nplus = 0;
132 #endif
133 return ch;
134 }
135
136 static void
137 prom_cninit(struct consdev *cn)
138 {
139 }
140
141 /*
142 * PROM console output putchar.
143 */
144 static void
145 prom_cnputc(dev_t dev, int c)
146 {
147 int s;
148
149 s = splhigh();
150 prom_putchar(c);
151 splx(s);
152 }
153
154 void
155 prom_cnpollc(dev_t dev, 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 #ifdef notyet /* PROM_OBP_V2 */
179 void
180 prom_get_device_args(const char *prop, char *dev, unsigned int dev_sz,
181 char *args, unsigned int args_sz)
182 {
183 char *cp, buffer[128];
184
185 getpropstringA(prom_findroot(), (char *)prop, buffer, sizeof buffer);
186
187 /*
188 * Extract device-specific arguments from a PROM device path (if any)
189 */
190 cp = buffer + strlen(buffer);
191 while (cp >= buffer) {
192 if (*cp == ':') {
193 strncpy(args, cp+1, args_sz);
194 *cp = '\0';
195 strncpy(dev, buffer, dev_sz);
196 break;
197 }
198 cp--;
199 }
200 }
201 #endif /* PROM_OBP_V2 */
202
203 /*
204 * This function replaces sys/dev/cninit.c
205 * Determine which device is the console using
206 * the PROM "input source" and "output sink".
207 */
208 void
209 consinit(void)
210 {
211 #ifdef notyet /* PROM_OBP_V2 */
212 char buffer[128];
213 #endif /* PROM_OBP_V2 */
214 const char *consname = "unknown";
215 #if KGDB
216 #if NZS > 0
217 extern const struct cdevsw zstty_cdevsw;
218 #endif
219 #endif
220
221 DBPRINT(("consinit()\r\n"));
222 if (cn_tab != &consdev_prom) return;
223
224 switch(prom_version()) {
225 #ifdef PROM_OLDMON
226 case PROM_OLDMON:
227 case PROM_OBP_V0:
228 switch(prom_stdin()) {
229 case PROMDEV_KBD:
230 consname = "keyboard/display";
231 break;
232 case PROMDEV_TTYA:
233 consname = "ttya";
234 break;
235 case PROMDEV_TTYB:
236 consname = "ttyb";
237 break;
238 }
239 break;
240 #endif /* PROM_OLDMON */
241
242 #ifdef notyet /* PROM_OBP_V2 */
243 case PROM_OBP_V2:
244 case PROM_OBP_V3:
245 case PROM_OPENFIRM:
246
247 /* Save PROM arguments for device matching */
248 prom_get_device_args("stdin-path",
249 buffer,
250 sizeof(buffer),
251 prom_stdin_args,
252 sizeof(prom_stdin_args));
253 prom_get_device_args("stdout-path",
254 buffer,
255 sizeof(buffer),
256 prom_stdout_args,
257 sizeof(prom_stdout_args));
258
259 /*
260 * Translate the STDIO package instance (`ihandle') -- that
261 * the PROM has already opened for us -- to a device tree
262 * node (i.e. a `phandle').
263 */
264 DBPRINT(("stdin instance = %x\r\n", prom_stdin()));
265 if ((prom_stdin_node = prom_instance_to_package(prom_stdin())) == 0) {
266 printf("WARNING: no PROM stdin\n");
267 }
268 DBPRINT(("stdin package = %x\r\n", prom_stdin_node));
269
270 DBPRINT(("stdout instance = %x\r\n", prom_stdout()));
271 if ((prom_stdout_node = prom_instance_to_package(prom_stdout())) == 0) {
272 printf("WARNING: no PROM stdout\n");
273 }
274 DBPRINT(("stdout package = %x\r\n", prom_stdout_node));
275 DBPRINT(("buffer @ %p\r\n", buffer));
276
277 if (prom_stdin_node && prom_node_has_property(prom_stdin_node, "keyboard")) {
278 #if NKBD == 0
279 printf("cninit: kdb/display not configured\n");
280 #endif
281 consname = "keyboard/display";
282 } else if (prom_stdout_node)
283 consname = buffer;
284 #endif /* PROM_OBP_V2 */
285 }
286 printf("console is %s\n", consname);
287
288 /* Initialize PROM console */
289 (*cn_tab->cn_probe)(cn_tab);
290 (*cn_tab->cn_init)(cn_tab);
291
292 #ifdef KGDB
293 /* Set up KGDB */
294 #if NZS > 0
295 if (cdevsw_lookup(kgdb_dev) == &zstty_cdevsw)
296 zs_kgdb_init();
297 #endif /* NZS > 0 */
298 #endif /* KGDB */
299 }
300
301