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