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