]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/dev/atkbdc/atkbdc_isa.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / dev / atkbdc / 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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
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/module.h>
36 #include <sys/bus.h>
37 #include <sys/malloc.h>
38 #include <machine/resource.h>
39 #include <sys/rman.h>
40 #include <machine/bus.h>
41
42 #include <dev/atkbdc/atkbdc_subr.h>
43 #include <dev/atkbdc/atkbdcreg.h>
44
45 #include <isa/isareg.h>
46 #include <isa/isavar.h>
47
48 static int      atkbdc_isa_probe(device_t dev);
49 static int      atkbdc_isa_attach(device_t dev);
50 static device_t atkbdc_isa_add_child(device_t bus, u_int order, const char *name,
51                     int unit);
52 static struct resource *atkbdc_isa_alloc_resource(device_t dev, device_t child,
53                     int type, int *rid, u_long start, u_long end,
54                     u_long count, u_int flags);
55 static int      atkbdc_isa_release_resource(device_t dev, device_t child,
56                     int type, int rid, struct resource *r);
57
58 static device_method_t atkbdc_isa_methods[] = {
59         DEVMETHOD(device_probe,         atkbdc_isa_probe),
60         DEVMETHOD(device_attach,        atkbdc_isa_attach),
61         DEVMETHOD(device_suspend,       bus_generic_suspend),
62         DEVMETHOD(device_resume,        bus_generic_resume),
63
64         DEVMETHOD(bus_add_child,        atkbdc_isa_add_child),
65         DEVMETHOD(bus_print_child,      atkbdc_print_child),
66         DEVMETHOD(bus_read_ivar,        atkbdc_read_ivar),
67         DEVMETHOD(bus_write_ivar,       atkbdc_write_ivar),
68         DEVMETHOD(bus_get_resource_list,atkbdc_get_resource_list),
69         DEVMETHOD(bus_alloc_resource,   atkbdc_isa_alloc_resource),
70         DEVMETHOD(bus_release_resource, atkbdc_isa_release_resource),
71         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
72         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
73         DEVMETHOD(bus_get_resource,     bus_generic_rl_get_resource),
74         DEVMETHOD(bus_set_resource,     bus_generic_rl_set_resource),
75         DEVMETHOD(bus_delete_resource,  bus_generic_rl_delete_resource),
76         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
77         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
78
79         { 0, 0 }
80 };
81
82 static driver_t atkbdc_isa_driver = {
83         ATKBDC_DRIVER_NAME,
84         atkbdc_isa_methods,
85         sizeof(atkbdc_softc_t *),
86 };
87
88 static struct isa_pnp_id atkbdc_ids[] = {
89         { 0x0303d041, "Keyboard controller (i8042)" },  /* PNP0303 */
90         { 0 }
91 };
92
93 static int
94 atkbdc_isa_probe(device_t dev)
95 {
96         struct resource *port0;
97         struct resource *port1;
98         u_long          start;
99         u_long          count;
100         int             error;
101         int             rid;
102 #if defined(__i386__) || defined(__amd64__)
103         bus_space_tag_t tag;
104         bus_space_handle_t ioh1;
105         volatile int    i;
106         register_t      flags;
107 #endif
108
109         /* check PnP IDs */
110         if (ISA_PNP_PROBE(device_get_parent(dev), dev, atkbdc_ids) == ENXIO)
111                 return ENXIO;
112
113         device_set_desc(dev, "Keyboard controller (i8042)");
114
115         /*
116          * Adjust I/O port resources.
117          * The AT keyboard controller uses two ports (a command/data port
118          * 0x60 and a status port 0x64), which may be given to us in 
119          * one resource (0x60 through 0x64) or as two separate resources
120          * (0x60 and 0x64). Some brain-damaged ACPI BIOS has reversed
121          * command/data port and status port. Furthermore, /boot/device.hints
122          * may contain just one port, 0x60. We shall adjust resource settings
123          * so that these two ports are available as two separate resources
124          * in correct order.
125          */
126         device_quiet(dev);
127         rid = 0;
128         if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &start, &count) != 0)
129                 return ENXIO;
130         if (start == IO_KBD + KBD_STATUS_PORT) {
131                 start = IO_KBD;
132                 count++;
133         }
134         if (count > 1)  /* adjust the count and/or start port */
135                 bus_set_resource(dev, SYS_RES_IOPORT, rid, start, 1);
136         port0 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
137         if (port0 == NULL)
138                 return ENXIO;
139         rid = 1;
140         if (bus_get_resource(dev, SYS_RES_IOPORT, rid, NULL, NULL) != 0)
141                 bus_set_resource(dev, SYS_RES_IOPORT, 1,
142                                  start + KBD_STATUS_PORT, 1);
143         port1 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
144         if (port1 == NULL) {
145                 bus_release_resource(dev, SYS_RES_IOPORT, 0, port0);
146                 return ENXIO;
147         }
148
149 #if defined(__i386__) || defined(__amd64__)
150         /*
151          * Check if we really have AT keyboard controller. Poll status
152          * register until we get "all clear" indication. If no such
153          * indication comes, it probably means that there is no AT
154          * keyboard controller present. Give up in such case. Check relies
155          * on the fact that reading from non-existing in/out port returns
156          * 0xff on i386. May or may not be true on other platforms.
157          */
158         tag = rman_get_bustag(port0);
159         ioh1 = rman_get_bushandle(port1);
160         flags = intr_disable();
161         for (i = 0; i != 65535; i++) {
162                 if ((bus_space_read_1(tag, ioh1, 0) & 0x2) == 0)
163                         break;
164         }
165         intr_restore(flags);
166         if (i == 65535) {
167                 bus_release_resource(dev, SYS_RES_IOPORT, 0, port0);
168                 bus_release_resource(dev, SYS_RES_IOPORT, 1, port1);
169                 if (bootverbose)
170                         device_printf(dev, "AT keyboard controller not found\n");
171                 return ENXIO;
172         }
173 #endif
174
175         device_verbose(dev);
176
177         error = atkbdc_probe_unit(device_get_unit(dev), port0, port1);
178
179         bus_release_resource(dev, SYS_RES_IOPORT, 0, port0);
180         bus_release_resource(dev, SYS_RES_IOPORT, 1, port1);
181
182         return error;
183 }
184
185 static int
186 atkbdc_isa_attach(device_t dev)
187 {
188         atkbdc_softc_t  *sc;
189         int             unit;
190         int             error;
191         int             rid;
192
193         unit = device_get_unit(dev);
194         sc = *(atkbdc_softc_t **)device_get_softc(dev);
195         if (sc == NULL) {
196                 /*
197                  * We have to maintain two copies of the kbdc_softc struct,
198                  * as the low-level console needs to have access to the
199                  * keyboard controller before kbdc is probed and attached.
200                  * kbdc_soft[] contains the default entry for that purpose.
201                  * See atkbdc.c. XXX
202                  */
203                 sc = atkbdc_get_softc(unit);
204                 if (sc == NULL)
205                         return ENOMEM;
206         }
207
208         rid = 0;
209         sc->retry = 5000;
210         sc->port0 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
211                                            RF_ACTIVE);
212         if (sc->port0 == NULL)
213                 return ENXIO;
214         rid = 1;
215         sc->port1 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
216                                            RF_ACTIVE);
217         if (sc->port1 == NULL) {
218                 bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port0);
219                 return ENXIO;
220         }
221
222         /*
223          * If the device is not created by the PnP BIOS or ACPI, then
224          * the hint for the IRQ is on the child atkbd device, not the
225          * keyboard controller, so this can fail.
226          */
227         rid = 0;
228         sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
229
230         error = atkbdc_attach_unit(unit, sc, sc->port0, sc->port1);
231         if (error) {
232                 bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port0);
233                 bus_release_resource(dev, SYS_RES_IOPORT, 1, sc->port1);
234                 if (sc->irq != NULL)
235                         bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
236                 return error;
237         }
238         *(atkbdc_softc_t **)device_get_softc(dev) = sc;
239
240         bus_generic_probe(dev);
241         bus_generic_attach(dev);
242
243         return 0;
244 }
245
246 static device_t
247 atkbdc_isa_add_child(device_t bus, u_int order, const char *name, int unit)
248 {
249         atkbdc_device_t *ivar;
250         atkbdc_softc_t  *sc;
251         device_t        child;
252         int             t;
253
254         sc = *(atkbdc_softc_t **)device_get_softc(bus);
255         ivar = malloc(sizeof(struct atkbdc_device), M_ATKBDDEV,
256                 M_NOWAIT | M_ZERO);
257         if (!ivar)
258                 return NULL;
259
260         child = device_add_child_ordered(bus, order, name, unit);
261         if (child == NULL) {
262                 free(ivar, M_ATKBDDEV);
263                 return child;
264         }
265
266         resource_list_init(&ivar->resources);
267         ivar->rid = order;
268
269         /*
270          * If the device is not created by the PnP BIOS or ACPI, refer
271          * to device hints for IRQ.  We always populate the resource
272          * list entry so we can use a standard bus_get_resource()
273          * method.
274          */
275         if (order == KBDC_RID_KBD) {
276                 if (sc->irq == NULL) {
277                         if (resource_int_value(name, unit, "irq", &t) != 0)
278                                 t = -1;
279                 } else
280                         t = rman_get_start(sc->irq);
281                 if (t > 0)
282                         resource_list_add(&ivar->resources, SYS_RES_IRQ,
283                             ivar->rid, t, t, 1);
284         }
285
286         if (resource_disabled(name, unit))
287                 device_disable(child);
288
289         device_set_ivars(child, ivar);
290
291         return child;
292 }
293
294 struct resource *
295 atkbdc_isa_alloc_resource(device_t dev, device_t child, int type, int *rid,
296     u_long start, u_long end, u_long count, u_int flags)
297 {
298         atkbdc_softc_t  *sc;
299         
300         sc = *(atkbdc_softc_t **)device_get_softc(dev);
301         if (type == SYS_RES_IRQ && *rid == KBDC_RID_KBD && sc->irq != NULL)
302                 return (sc->irq);
303         return (bus_generic_rl_alloc_resource(dev, child, type, rid, start,
304             end, count, flags));
305 }
306
307 static int
308 atkbdc_isa_release_resource(device_t dev, device_t child, int type, int rid,
309     struct resource *r)
310 {
311         atkbdc_softc_t  *sc;
312         
313         sc = *(atkbdc_softc_t **)device_get_softc(dev);
314         if (type == SYS_RES_IRQ && rid == KBDC_RID_KBD && r == sc->irq)
315                 return (0);
316         return (bus_generic_rl_release_resource(dev, child, type, rid, r));
317 }
318
319 DRIVER_MODULE(atkbdc, isa, atkbdc_isa_driver, atkbdc_devclass, 0, 0);
320 DRIVER_MODULE(atkbdc, acpi, atkbdc_isa_driver, atkbdc_devclass, 0, 0);