xref: /netbsd-src/sys/arch/sparc64/dev/consinit.c (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
1 /*	$NetBSD: consinit.c,v 1.21 2006/02/13 21:47:11 cdi 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.21 2006/02/13 21:47:11 cdi Exp $");
33 
34 #include "opt_ddb.h"
35 #include "pcons.h"
36 #include "ukbd.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/conf.h>
41 #include <sys/device.h>
42 #include <sys/file.h>
43 #include <sys/ioctl.h>
44 #include <sys/kernel.h>
45 #include <sys/proc.h>
46 #include <sys/tty.h>
47 #include <sys/time.h>
48 #include <sys/syslog.h>
49 
50 #include <machine/autoconf.h>
51 #include <machine/openfirm.h>
52 #include <machine/bsd_openprom.h>
53 #include <machine/cpu.h>
54 #include <machine/eeprom.h>
55 #include <machine/psl.h>
56 #include <machine/z8530var.h>
57 #include <machine/sparc64.h>
58 
59 #include <dev/cons.h>
60 
61 #include <sparc64/dev/cons.h>
62 
63 #include <dev/usb/ukbdvar.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 
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(struct consdev *cd)
94 {
95 #if NPCONS > 0
96 	int maj;
97 	extern const struct cdevsw pcons_cdevsw;
98 
99 	maj = cdevsw_lookup_major(&pcons_cdevsw);
100 	cd->cn_dev = makedev(maj, 0);
101 	cd->cn_pri = CN_INTERNAL;
102 #endif
103 }
104 
105 int
106 prom_cngetc(dev_t dev)
107 {
108 	unsigned char ch = '\0';
109 	int l;
110 #ifdef DDB
111 	static int nplus = 0;
112 #endif
113 
114 	while ((l = prom_read(prom_stdin(), &ch, 1)) != 1)
115 		/* void */;
116 #ifdef DDB
117 	if (ch == '+') {
118 		if (nplus++ > 3) Debugger();
119 	} else nplus = 0;
120 #endif
121 	if (ch == '\r')
122 		ch = '\n';
123 	return ch;
124 }
125 
126 static void
127 prom_cninit(struct consdev *cn)
128 {
129 }
130 
131 /*
132  * PROM console output putchar.
133  */
134 static void
135 prom_cnputc(dev_t dev, int c)
136 {
137 	int s;
138 	char c0 = (c & 0x7f);
139 
140 	s = splhigh();
141 	prom_write(prom_stdout(), &c0, 1);
142 	splx(s);
143 }
144 
145 void
146 prom_cnpollc(dev_t dev, int on)
147 {
148 	if (on) {
149                 /* Entering debugger. */
150 #if NFB > 0
151                 fb_unblank();
152 #endif
153 	} else {
154                 /* Resuming kernel. */
155 	}
156 #if NPCONS > 0
157 	pcons_cnpollc(dev, on);
158 #endif
159 }
160 
161 /*****************************************************************/
162 
163 #ifdef	DEBUG
164 #define	DBPRINT(x)	prom_printf x
165 #else
166 #define	DBPRINT(x)
167 #endif
168 
169 int prom_stdin_node;
170 int prom_stdout_node;
171 
172 /*
173  * This function replaces sys/dev/cninit.c
174  * Determine which device is the console using
175  * the PROM "input source" and "output sink".
176  */
177 void
178 consinit()
179 {
180 	int chosen;
181 	char buffer[128];
182 	const char *consname = "unknown";
183 
184 	DBPRINT(("consinit()\r\n"));
185 
186 	if (cn_tab != &consdev_prom)
187 		return;
188 
189 	chosen = prom_finddevice("/chosen");
190 
191 	if ((prom_stdin_node = prom_instance_to_package(prom_stdin())) == 0) {
192 		printf("WARNING: no PROM stdin\n");
193 	}
194 	DBPRINT(("stdin node = %x\r\n", prom_stdin_node));
195 
196 	if ((prom_stdout_node = prom_instance_to_package(prom_stdout())) == 0)
197 		printf("WARNING: no PROM stdout\n");
198 	DBPRINT(("stdout package = %x\r\n", prom_stdout_node));
199 
200 	DBPRINT(("buffer @ %p\r\n", buffer));
201 
202 	if (prom_stdin_node != 0 &&
203 	    (prom_getproplen(prom_stdin_node, "keyboard") >= 0)) {
204 #if NUKBD > 0
205 		if ((OF_instance_to_path(prom_stdin(), buffer, sizeof(buffer)) >= 0) &&
206 		    (strstr(buffer, "/usb@") != NULL)) {
207 			/*
208 		 	* If we have a USB keyboard, it will show up as (e.g.)
209 		 	*   /pci@1f,0/usb@c,3/keyboard@1	(Blade 100)
210 		 	*/
211 			consname = "usb-keyboard/display";
212 			ukbd_cnattach();
213 		} else
214 #endif
215 			consname = "sun-keyboard/display";
216 	} else if (prom_stdin_node != 0 &&
217 		   (OF_instance_to_path(prom_stdin(), buffer, sizeof(buffer)) >= 0)) {
218 		consname = buffer;
219 	}
220 	printf("console is %s\n", consname);
221 
222 	/* Initialize PROM console */
223 	(*cn_tab->cn_probe)(cn_tab);
224 	(*cn_tab->cn_init)(cn_tab);
225 }
226 
227