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