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