consinit.c revision 1.13 1 /* $NetBSD: consinit.c,v 1.13 2003/07/15 03:36:04 lukem 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.13 2003/07/15 03:36:04 lukem 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 = NULL, stdout = NULL;
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 /*
182 * This function replaces sys/dev/cninit.c
183 * Determine which device is the console using
184 * the PROM "input source" and "output sink".
185 */
186 void
187 consinit()
188 {
189 register int chosen;
190 char buffer[128];
191 extern int stdinnode, fbnode;
192 char *consname = "unknown";
193
194 DBPRINT(("consinit()\r\n"));
195 if (cn_tab != &consdev_prom) return;
196
197 DBPRINT(("setting up stdin\r\n"));
198 chosen = OF_finddevice("/chosen");
199 DBPRINT(("chosen = %x, stdin @ %p\r\n", chosen, &stdin));
200 OF_getprop(chosen, "stdin", &stdin, sizeof(stdin));
201 DBPRINT(("stdin instance = %x\r\n", stdin));
202
203 if ((stdinnode = OF_instance_to_package(stdin)) == 0) {
204 printf("WARNING: no PROM stdin\n");
205 }
206
207 DBPRINT(("stdin node = %x\r\n", stdinnode));
208 DBPRINT(("setting up stdout\r\n"));
209 OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
210
211 DBPRINT(("stdout instance = %x\r\n", stdout));
212
213 if ((fbnode = OF_instance_to_package(stdout)) == 0)
214 printf("WARNING: no PROM stdout\n");
215
216 DBPRINT(("stdout package = %x\r\n", fbnode));
217 DBPRINT(("buffer @ %p\r\n", buffer));
218
219 if (stdinnode && (OF_getproplen(stdinnode,"keyboard") >= 0)) {
220 #if NKBD > 0
221 printf("cninit: kdb/display not configured\n");
222 #endif
223 consname = "keyboard/display";
224 } else if (fbnode &&
225 (OF_instance_to_path(stdin, buffer, sizeof(buffer)) >= 0)) {
226 consname = buffer;
227 }
228 printf("console is %s\n", consname);
229
230 /* Initialize PROM console */
231 (*cn_tab->cn_probe)(cn_tab);
232 (*cn_tab->cn_init)(cn_tab);
233 }
234
235