xref: /freebsd/sys/isa/syscons_isa.c (revision d056fa046c6a91b90cd98165face0e42a33a5173)
1 /*-
2  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer as
10  *    the first lines of this file unmodified.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_syscons.h"
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/cons.h>
38 #include <sys/kbio.h>
39 #include <sys/consio.h>
40 #include <sys/sysctl.h>
41 
42 #if defined(__i386__) || defined(__amd64__)
43 
44 #include <machine/clock.h>
45 #include <machine/md_var.h>
46 #include <machine/ppireg.h>
47 #include <machine/timerreg.h>
48 #include <machine/pc/bios.h>
49 
50 #include <vm/vm.h>
51 #include <vm/pmap.h>
52 #include <vm/vm_param.h>
53 
54 #define BIOS_CLKED	(1 << 6)
55 #define BIOS_NLKED	(1 << 5)
56 #define BIOS_SLKED	(1 << 4)
57 #define BIOS_ALKED	0
58 
59 #endif
60 
61 #include <dev/syscons/syscons.h>
62 
63 #include <isa/isavar.h>
64 
65 #include "opt_xbox.h"
66 
67 #ifdef XBOX
68 #include <machine/xbox.h>
69 #endif
70 
71 static devclass_t	sc_devclass;
72 
73 static sc_softc_t main_softc;
74 #ifdef SC_NO_SUSPEND_VTYSWITCH
75 static int sc_no_suspend_vtswitch = 1;
76 #else
77 static int sc_no_suspend_vtswitch = 0;
78 #endif
79 static int sc_cur_scr;
80 
81 TUNABLE_INT("hw.syscons.sc_no_suspend_vtswitch", (int *)&sc_no_suspend_vtswitch);
82 SYSCTL_DECL(_hw_syscons);
83 SYSCTL_INT(_hw_syscons, OID_AUTO, sc_no_suspend_vtswitch, CTLFLAG_RW,
84 	&sc_no_suspend_vtswitch, 0, "Disable VT switch before suspend.");
85 
86 static void
87 scidentify (driver_t *driver, device_t parent)
88 {
89 	BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "sc", 0);
90 }
91 
92 static int
93 scprobe(device_t dev)
94 {
95 	/* No pnp support */
96 	if (isa_get_vendorid(dev))
97 		return (ENXIO);
98 
99 	device_set_desc(dev, "System console");
100 	return sc_probe_unit(device_get_unit(dev), device_get_flags(dev));
101 }
102 
103 static int
104 scattach(device_t dev)
105 {
106 	return sc_attach_unit(device_get_unit(dev), device_get_flags(dev));
107 }
108 
109 static int
110 scsuspend(device_t dev)
111 {
112 	int		retry = 10;
113 	static int	dummy;
114 	sc_softc_t	*sc;
115 
116 	sc = &main_softc;
117 
118 	if (sc->cur_scp == NULL)
119 		return (0);
120 
121 	sc_cur_scr = sc->cur_scp->index;
122 
123 	if (sc_no_suspend_vtswitch)
124 		return (0);
125 
126 	do {
127 		sc_switch_scr(sc, 0);
128 		if (!sc->switch_in_progress) {
129 			break;
130 		}
131 		tsleep(&dummy, 0, "scsuspend", 100);
132 	} while (retry--);
133 
134 	return (0);
135 }
136 
137 static int
138 scresume(device_t dev)
139 {
140 	sc_softc_t	*sc;
141 
142 	if (sc_no_suspend_vtswitch)
143 		return (0);
144 
145 	sc = &main_softc;
146 	sc_switch_scr(sc, sc_cur_scr);
147 
148 	return (0);
149 }
150 
151 int
152 sc_max_unit(void)
153 {
154 	return devclass_get_maxunit(sc_devclass);
155 }
156 
157 sc_softc_t
158 *sc_get_softc(int unit, int flags)
159 {
160 	sc_softc_t *sc;
161 
162 	if (unit < 0)
163 		return NULL;
164 	if (flags & SC_KERNEL_CONSOLE) {
165 		/* FIXME: clear if it is wired to another unit! */
166 		sc = &main_softc;
167 	} else {
168 	        sc = (sc_softc_t *)device_get_softc(devclass_get_device(sc_devclass, unit));
169 		if (sc == NULL)
170 			return NULL;
171 	}
172 	sc->unit = unit;
173 	if (!(sc->flags & SC_INIT_DONE)) {
174 		sc->keyboard = -1;
175 		sc->adapter = -1;
176 		sc->cursor_char = SC_CURSOR_CHAR;
177 		sc->mouse_char = SC_MOUSE_CHAR;
178 	}
179 	return sc;
180 }
181 
182 sc_softc_t
183 *sc_find_softc(struct video_adapter *adp, struct keyboard *kbd)
184 {
185 	sc_softc_t *sc;
186 	int units;
187 	int i;
188 
189 	sc = &main_softc;
190 	if (((adp == NULL) || (adp == sc->adp))
191 	    && ((kbd == NULL) || (kbd == sc->kbd)))
192 		return sc;
193 	units = devclass_get_maxunit(sc_devclass);
194 	for (i = 0; i < units; ++i) {
195 	        sc = (sc_softc_t *)device_get_softc(devclass_get_device(sc_devclass, i));
196 		if (sc == NULL)
197 			continue;
198 		if (((adp == NULL) || (adp == sc->adp))
199 		    && ((kbd == NULL) || (kbd == sc->kbd)))
200 			return sc;
201 	}
202 	return NULL;
203 }
204 
205 int
206 sc_get_cons_priority(int *unit, int *flags)
207 {
208 	const char *at;
209 	int u, f;
210 
211 #ifdef XBOX
212 	/*
213 	 * The XBox Loader does not support hints, which makes our initial
214 	 * console probe fail. Therefore, if an XBox is found, we hardcode the
215 	 * existence of the console, as it is always there anyway.
216 	 */
217 	if (arch_i386_is_xbox) {
218 		*unit = 0;
219 		*flags = SC_KERNEL_CONSOLE;
220 		return CN_INTERNAL;
221 	}
222 #endif
223 
224 	*unit = -1;
225 	for (u = 0; u < 16; u++) {
226 		if (resource_disabled(SC_DRIVER_NAME, u))
227 			continue;
228 		if (resource_string_value(SC_DRIVER_NAME, u, "at", &at) != 0)
229 			continue;
230 		if (resource_int_value(SC_DRIVER_NAME, u, "flags", &f) != 0)
231 			f = 0;
232 		if (f & SC_KERNEL_CONSOLE) {
233 			/* the user designates this unit to be the console */
234 			*unit = u;
235 			*flags = f;
236 			break;
237 		}
238 		if (*unit < 0) {
239 			/* ...otherwise remember the first found unit */
240 			*unit = u;
241 			*flags = f;
242 		}
243 	}
244 	if (*unit < 0)
245 		return CN_DEAD;
246 #if 0
247 	return ((*flags & SC_KERNEL_CONSOLE) ? CN_INTERNAL : CN_NORMAL);
248 #endif
249 	return CN_INTERNAL;
250 }
251 
252 void
253 sc_get_bios_values(bios_values_t *values)
254 {
255 #if defined(__i386__) || defined(__amd64__)
256 	u_int8_t shift;
257 
258 	values->cursor_start = *(u_int8_t *)BIOS_PADDRTOVADDR(0x461);
259 	values->cursor_end = *(u_int8_t *)BIOS_PADDRTOVADDR(0x460);
260 	shift = *(u_int8_t *)BIOS_PADDRTOVADDR(0x417);
261 	values->shift_state = ((shift & BIOS_CLKED) ? CLKED : 0)
262 			       | ((shift & BIOS_NLKED) ? NLKED : 0)
263 			       | ((shift & BIOS_SLKED) ? SLKED : 0)
264 			       | ((shift & BIOS_ALKED) ? ALKED : 0);
265 #else
266 	values->cursor_start = 0;
267 	values->cursor_end = 32;
268 	values->shift_state = 0;
269 #endif
270 	values->bell_pitch = BELL_PITCH;
271 }
272 
273 int
274 sc_tone(int herz)
275 {
276 #if defined(__i386__) || defined(__amd64__)
277 	if (herz) {
278 		/* set command for counter 2, 2 byte write */
279 		if (timer_spkr_acquire())
280 			return EBUSY;
281 		/* set pitch */
282 		spkr_set_pitch(timer_freq / herz);
283 		/* enable counter 2 output to speaker */
284 		ppi_spkr_on();
285 	} else {
286 		/* disable counter 2 output to speaker */
287 		ppi_spkr_off();
288 		timer_spkr_release();
289 	}
290 #endif
291 
292 	return 0;
293 }
294 
295 static device_method_t sc_methods[] = {
296 	DEVMETHOD(device_identify,	scidentify),
297 	DEVMETHOD(device_probe,         scprobe),
298 	DEVMETHOD(device_attach,        scattach),
299 	DEVMETHOD(device_suspend,       scsuspend),
300 	DEVMETHOD(device_resume,        scresume),
301 	{ 0, 0 }
302 };
303 
304 static driver_t sc_driver = {
305 	SC_DRIVER_NAME,
306 	sc_methods,
307 	sizeof(sc_softc_t),
308 };
309 
310 DRIVER_MODULE(sc, isa, sc_driver, sc_devclass, 0, 0);
311