When enabling A20, put upper limit on amount of time we wait for the
[dragonfly.git] / sys / dev / misc / atkbdc_layer / atkbdc_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/atkbdc_isa.c,v 1.14.2.1 2000/03/31 12:52:05 yokota Exp $
27  * $DragonFly: src/sys/dev/misc/atkbdc_layer/atkbdc_isa.c,v 1.8 2007/04/22 10:43:00 y0netan1 Exp $
28  */
29
30 #include "opt_kbd.h"
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/bus.h>
36 #include <sys/malloc.h>
37 #include <sys/rman.h>
38
39 #include <dev/misc/kbd/atkbdcreg.h>
40
41 #include <bus/isa/isareg.h>
42 #include <bus/isa/isavar.h>
43
44 MALLOC_DEFINE(M_ATKBDDEV, "atkbddev", "AT Keyboard device");
45
46 /* children */
47 typedef struct atkbdc_device {
48         int flags;      /* configuration flags */
49         int irq;        /* ISA IRQ mask */
50         u_int32_t vendorid;
51         u_int32_t serial;
52         u_int32_t logicalid;
53         u_int32_t compatid;
54 } atkbdc_device_t;
55
56 /* kbdc */
57 devclass_t atkbdc_devclass;
58
59 static int      atkbdc_probe(device_t dev);
60 static int      atkbdc_attach(device_t dev);
61 static int      atkbdc_print_child(device_t bus, device_t dev);
62 static int      atkbdc_read_ivar(device_t bus, device_t dev, int index,
63                                  u_long *val);
64 static int      atkbdc_write_ivar(device_t bus, device_t dev, int index,
65                                   u_long val);
66
67 static device_method_t atkbdc_methods[] = {
68         DEVMETHOD(device_probe,         atkbdc_probe),
69         DEVMETHOD(device_attach,        atkbdc_attach),
70         DEVMETHOD(device_suspend,       bus_generic_suspend),
71         DEVMETHOD(device_resume,        bus_generic_resume),
72
73         DEVMETHOD(bus_print_child,      atkbdc_print_child),
74         DEVMETHOD(bus_read_ivar,        atkbdc_read_ivar),
75         DEVMETHOD(bus_write_ivar,       atkbdc_write_ivar),
76         DEVMETHOD(bus_alloc_resource,   bus_generic_alloc_resource),
77         DEVMETHOD(bus_release_resource, bus_generic_release_resource),
78         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
79         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
80         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
81         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
82
83         { 0, 0 }
84 };
85
86 static driver_t atkbdc_driver = {
87         ATKBDC_DRIVER_NAME,
88         atkbdc_methods,
89         sizeof(atkbdc_softc_t *),
90 };
91
92 static struct isa_pnp_id atkbdc_ids[] = {
93         { 0x0303d041, "Keyboard controller (i8042)" },  /* PNP0303 */
94         { 0 }
95 };
96
97 static int
98 atkbdc_probe(device_t dev)
99 {
100         struct resource *port0;
101         struct resource *port1;
102         int             error;
103         int             rid;
104 #if defined(__i386__)
105         bus_space_tag_t tag;
106         bus_space_handle_t ioh1;
107         volatile int    i;
108 #endif
109
110         /* check PnP IDs */
111         if (ISA_PNP_PROBE(device_get_parent(dev), dev, atkbdc_ids) == ENXIO)
112                 return ENXIO;
113
114         device_set_desc(dev, "Keyboard controller (i8042)");
115
116         rid = 0;
117         port0 = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1,
118                                    RF_ACTIVE);
119         if (port0 == NULL)
120                 return ENXIO;
121         /* XXX */
122         if (bus_get_resource_start(dev, SYS_RES_IOPORT, 1) <= 0) {
123                 bus_set_resource(dev, SYS_RES_IOPORT, 1,
124                                  rman_get_start(port0) + KBD_STATUS_PORT, 1);
125         }
126         rid = 1;
127         port1 = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1,
128                                    RF_ACTIVE);
129         if (port1 == NULL) {
130                 bus_release_resource(dev, SYS_RES_IOPORT, 0, port0);
131                 return ENXIO;
132         }
133
134 #if defined(__i386__)
135         /*
136          * Check if we really have AT keyboard controller. Poll status
137          * register until we get "all clear" indication. If no such
138          * indication comes, it probably means that there is no AT
139          * keyboard controller present. Give up in such case. Check relies
140          * on the fact that reading from non-existing in/out port returns
141          * 0xff on i386. May or may not be true on other platforms.
142          */
143         tag = rman_get_bustag(port0);
144         ioh1 = rman_get_bushandle(port1);
145         for (i = 65536; i != 0; --i) {
146                 if ((bus_space_read_1(tag, ioh1, 0) & 0x2) == 0)
147                         break;
148         }
149         if (i == 0) {
150                 bus_release_resource(dev, SYS_RES_IOPORT, 0, port0);
151                 bus_release_resource(dev, SYS_RES_IOPORT, 1, port1);
152                 return ENXIO;
153         }
154 #endif
155
156         error = atkbdc_probe_unit(device_get_unit(dev), port0, port1);
157
158         bus_release_resource(dev, SYS_RES_IOPORT, 0, port0);
159         bus_release_resource(dev, SYS_RES_IOPORT, 1, port1);
160
161         return error;
162 }
163
164 static void
165 atkbdc_add_device(device_t dev, const char *name, int unit)
166 {
167         atkbdc_device_t *kdev;
168         device_t        child;
169         int             t;
170
171         if (resource_int_value(name, unit, "disabled", &t) == 0 && t != 0)
172                 return;
173
174         kdev = kmalloc(sizeof(struct atkbdc_device), M_ATKBDDEV, 
175                         M_WAITOK | M_ZERO);
176
177         if (resource_int_value(name, unit, "irq", &t) == 0)
178                 kdev->irq = t;
179         else
180                 kdev->irq = -1;
181
182         if (resource_int_value(name, unit, "flags", &t) == 0)
183                 kdev->flags = t;
184         else
185                 kdev->flags = 0;
186
187         child = device_add_child(dev, name, unit);
188         device_set_ivars(child, kdev);
189 }
190
191 static int
192 atkbdc_attach(device_t dev)
193 {
194         atkbdc_softc_t  *sc;
195         int             unit;
196         int             error;
197         int             rid;
198         int             i;
199
200         unit = device_get_unit(dev);
201         sc = *(atkbdc_softc_t **)device_get_softc(dev);
202         if (sc == NULL) {
203                 /*
204                  * We have to maintain two copies of the kbdc_softc struct,
205                  * as the low-level console needs to have access to the
206                  * keyboard controller before kbdc is probed and attached. 
207                  * kbdc_soft[] contains the default entry for that purpose.
208                  * See atkbdc.c. XXX
209                  */
210                 sc = atkbdc_get_softc(unit);
211                 if (sc == NULL)
212                         return ENOMEM;
213         }
214
215         rid = 0;
216         sc->port0 = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1,
217                                        RF_ACTIVE);
218         if (sc->port0 == NULL)
219                 return ENXIO;
220         rid = 1;
221         sc->port1 = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1,
222                                        RF_ACTIVE);
223         if (sc->port1 == NULL) {
224                 bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port0);
225                 return ENXIO;
226         }
227
228         error = atkbdc_attach_unit(unit, sc, sc->port0, sc->port1);
229         if (error) {
230                 bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port0);
231                 bus_release_resource(dev, SYS_RES_IOPORT, 1, sc->port1);
232                 return error;
233         }
234         *(atkbdc_softc_t **)device_get_softc(dev) = sc;
235
236         /*
237          * Add all devices configured to be attached to atkbdc0.
238          */
239         for (i = resource_query_string(-1, "at", device_get_nameunit(dev));
240              i != -1;
241              i = resource_query_string(i, "at", device_get_nameunit(dev))) {
242                 atkbdc_add_device(dev, resource_query_name(i),
243                                   resource_query_unit(i));
244         }
245
246         /*
247          * and atkbdc?
248          */
249         for (i = resource_query_string(-1, "at", device_get_name(dev));
250              i != -1;
251              i = resource_query_string(i, "at", device_get_name(dev))) {
252                 atkbdc_add_device(dev, resource_query_name(i),
253                                   resource_query_unit(i));
254         }
255
256         bus_generic_attach(dev);
257
258         return 0;
259 }
260
261 static int
262 atkbdc_print_child(device_t bus, device_t dev)
263 {
264         atkbdc_device_t *kbdcdev;
265         int retval = 0;
266
267         kbdcdev = (atkbdc_device_t *)device_get_ivars(dev);
268
269         retval += bus_print_child_header(bus, dev);
270         if (kbdcdev->flags != 0)
271                 retval += kprintf(" flags 0x%x", kbdcdev->flags);
272         if (kbdcdev->irq != -1)
273                 retval += kprintf(" irq %d", kbdcdev->irq);
274         retval += bus_print_child_footer(bus, dev);
275
276         return (retval);
277 }
278
279 static int
280 atkbdc_read_ivar(device_t bus, device_t dev, int index, u_long *val)
281 {
282         atkbdc_device_t *ivar;
283
284         ivar = (atkbdc_device_t *)device_get_ivars(dev);
285         switch (index) {
286         case KBDC_IVAR_IRQ:
287                 *val = (u_long)ivar->irq;
288                 break;
289         case KBDC_IVAR_FLAGS:
290                 *val = (u_long)ivar->flags;
291                 break;
292         case KBDC_IVAR_VENDORID:
293                 *val = (u_long)ivar->vendorid;
294                 break;
295         case KBDC_IVAR_SERIAL:
296                 *val = (u_long)ivar->serial;
297                 break;
298         case KBDC_IVAR_LOGICALID:
299                 *val = (u_long)ivar->logicalid;
300                 break;
301         case KBDC_IVAR_COMPATID:
302                 *val = (u_long)ivar->compatid;
303                 break;
304         default:
305                 return ENOENT;
306         }
307         return 0;
308 }
309
310 static int
311 atkbdc_write_ivar(device_t bus, device_t dev, int index, u_long val)
312 {
313         atkbdc_device_t *ivar;
314
315         ivar = (atkbdc_device_t *)device_get_ivars(dev);
316         switch (index) {
317         case KBDC_IVAR_IRQ:
318                 ivar->irq = (int)val;
319                 break;
320         case KBDC_IVAR_FLAGS:
321                 ivar->flags = (int)val;
322                 break;
323         case KBDC_IVAR_VENDORID:
324                 ivar->vendorid = (u_int32_t)val;
325                 break;
326         case KBDC_IVAR_SERIAL:
327                 ivar->serial = (u_int32_t)val;
328                 break;
329         case KBDC_IVAR_LOGICALID:
330                 ivar->logicalid = (u_int32_t)val;
331                 break;
332         case KBDC_IVAR_COMPATID:
333                 ivar->compatid = (u_int32_t)val;
334                 break;
335         default:
336                 return ENOENT;
337         }
338         return 0;
339 }
340
341 DRIVER_MODULE(atkbdc, isa, atkbdc_driver, atkbdc_devclass, 0, 0);