xref: /freebsd/sys/isa/syscons_isa.c (revision b601c69bdbe8755d26570261d7fd4c02ee4eff74)
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  * $FreeBSD$
27  */
28 
29 #include "opt_syscons.h"
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/bus.h>
36 #include <sys/cons.h>
37 
38 #include <machine/console.h>
39 
40 #ifdef __i386__
41 
42 #include <machine/clock.h>
43 #include <machine/md_var.h>
44 #include <machine/pc/bios.h>
45 
46 #include <vm/vm.h>
47 #include <vm/pmap.h>
48 
49 #include <i386/isa/timerreg.h>
50 
51 #define BIOS_CLKED	(1 << 6)
52 #define BIOS_NLKED	(1 << 5)
53 #define BIOS_SLKED	(1 << 4)
54 #define BIOS_ALKED	0
55 
56 #endif /* __i386__ */
57 
58 #include <dev/syscons/syscons.h>
59 
60 #include <isa/isareg.h>
61 #include <isa/isavar.h>
62 
63 static devclass_t	sc_devclass;
64 
65 static sc_softc_t main_softc;
66 
67 static void
68 scidentify (driver_t *driver, device_t parent)
69 {
70 	BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "sc", 0);
71 }
72 
73 static int
74 scprobe(device_t dev)
75 {
76 	/* No pnp support */
77 	if (isa_get_vendorid(dev))
78 		return (ENXIO);
79 
80 	device_set_desc(dev, "System console");
81 	return sc_probe_unit(device_get_unit(dev), device_get_flags(dev));
82 }
83 
84 static int
85 scattach(device_t dev)
86 {
87 	return sc_attach_unit(device_get_unit(dev), device_get_flags(dev));
88 }
89 
90 static int
91 scresume(device_t dev)
92 {
93 	return sc_resume_unit(device_get_unit(dev));
94 }
95 
96 int
97 sc_max_unit(void)
98 {
99 	return devclass_get_maxunit(sc_devclass);
100 }
101 
102 sc_softc_t
103 *sc_get_softc(int unit, int flags)
104 {
105 	sc_softc_t *sc;
106 
107 	if (unit < 0)
108 		return NULL;
109 	if (flags & SC_KERNEL_CONSOLE) {
110 		/* FIXME: clear if it is wired to another unit! */
111 		sc = &main_softc;
112 	} else {
113 	        sc = (sc_softc_t *)device_get_softc(devclass_get_device(sc_devclass, unit));
114 		if (sc == NULL)
115 			return NULL;
116 	}
117 	sc->unit = unit;
118 	if (!(sc->flags & SC_INIT_DONE)) {
119 		sc->keyboard = -1;
120 		sc->adapter = -1;
121 		sc->cursor_char = SC_CURSOR_CHAR;
122 		sc->mouse_char = SC_MOUSE_CHAR;
123 	}
124 	return sc;
125 }
126 
127 sc_softc_t
128 *sc_find_softc(struct video_adapter *adp, struct keyboard *kbd)
129 {
130 	sc_softc_t *sc;
131 	int units;
132 	int i;
133 
134 	sc = &main_softc;
135 	if (((adp == NULL) || (adp == sc->adp))
136 	    && ((kbd == NULL) || (kbd == sc->kbd)))
137 		return sc;
138 	units = devclass_get_maxunit(sc_devclass);
139 	for (i = 0; i < units; ++i) {
140 	        sc = (sc_softc_t *)device_get_softc(devclass_get_device(sc_devclass, i));
141 		if (sc == NULL)
142 			continue;
143 		if (((adp == NULL) || (adp == sc->adp))
144 		    && ((kbd == NULL) || (kbd == sc->kbd)))
145 			return sc;
146 	}
147 	return NULL;
148 }
149 
150 int
151 sc_get_cons_priority(int *unit, int *flags)
152 {
153 	int disabled;
154 	char *at;
155 	int u, f;
156 
157 	*unit = -1;
158 	for (u = 0; u < 16; u++) {
159 		if ((resource_int_value(SC_DRIVER_NAME, u, "disabled",
160 					&disabled) == 0) && disabled)
161 			continue;
162 		if (resource_string_value(SC_DRIVER_NAME, u, "at", &at) != 0)
163 			continue;
164 		if (resource_int_value(SC_DRIVER_NAME, u, "flags", &f) != 0)
165 			f = 0;
166 		if (f & SC_KERNEL_CONSOLE) {
167 			/* the user designates this unit to be the console */
168 			*unit = u;
169 			*flags = f;
170 			break;
171 		}
172 		if (*unit < 0) {
173 			/* ...otherwise remember the first found unit */
174 			*unit = u;
175 			*flags = f;
176 		}
177 	}
178 	if (*unit < 0)
179 		return CN_DEAD;
180 #if 0
181 	return ((*flags & SC_KERNEL_CONSOLE) ? CN_INTERNAL : CN_NORMAL);
182 #endif
183 	return CN_INTERNAL;
184 }
185 
186 void
187 sc_get_bios_values(bios_values_t *values)
188 {
189 #ifdef __i386__
190 	u_int8_t shift;
191 
192 	values->cursor_start = *(u_int8_t *)BIOS_PADDRTOVADDR(0x461);
193 	values->cursor_end = *(u_int8_t *)BIOS_PADDRTOVADDR(0x460);
194 	shift = *(u_int8_t *)BIOS_PADDRTOVADDR(0x417);
195 	values->shift_state = ((shift & BIOS_CLKED) ? CLKED : 0)
196 			       | ((shift & BIOS_NLKED) ? NLKED : 0)
197 			       | ((shift & BIOS_SLKED) ? SLKED : 0)
198 			       | ((shift & BIOS_ALKED) ? ALKED : 0);
199 	values->bell_pitch = BELL_PITCH;
200 #else /* !__i386__ */
201 	values->cursor_start = 0;
202 	values->cursor_end = 32;
203 	values->shift_state = 0;
204 	values->bell_pitch = BELL_PITCH;
205 #endif /* __i386__ */
206 }
207 
208 int
209 sc_tone(int herz)
210 {
211 #ifdef __i386__
212 	int pitch;
213 
214 	if (herz) {
215 		/* set command for counter 2, 2 byte write */
216 		if (acquire_timer2(TIMER_16BIT | TIMER_SQWAVE))
217 			return EBUSY;
218 		/* set pitch */
219 		pitch = timer_freq/herz;
220 		outb(TIMER_CNTR2, pitch);
221 		outb(TIMER_CNTR2, pitch >> 8);
222 		/* enable counter 2 output to speaker */
223 		outb(IO_PPI, inb(IO_PPI) | 3);
224 	} else {
225 		/* disable counter 2 output to speaker */
226 		outb(IO_PPI, inb(IO_PPI) & 0xFC);
227 		release_timer2();
228 	}
229 #endif /* __i386__ */
230 
231 	return 0;
232 }
233 
234 static device_method_t sc_methods[] = {
235 	DEVMETHOD(device_identify,	scidentify),
236 	DEVMETHOD(device_probe,         scprobe),
237 	DEVMETHOD(device_attach,        scattach),
238 	DEVMETHOD(device_resume,        scresume),
239 	{ 0, 0 }
240 };
241 
242 static driver_t sc_driver = {
243 	SC_DRIVER_NAME,
244 	sc_methods,
245 	sizeof(sc_softc_t),
246 };
247 
248 DRIVER_MODULE(sc, isa, sc_driver, sc_devclass, 0, 0);
249