atkbdc_isa.c revision 302408
1204076Spjd/*- 2204076Spjd * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp> 3204076Spjd * All rights reserved. 4204076Spjd * 5204076Spjd * Redistribution and use in source and binary forms, with or without 6204076Spjd * modification, are permitted provided that the following conditions 7204076Spjd * are met: 8204076Spjd * 1. Redistributions of source code must retain the above copyright 9204076Spjd * notice, this list of conditions and the following disclaimer as 10204076Spjd * the first lines of this file unmodified. 11204076Spjd * 2. Redistributions in binary form must reproduce the above copyright 12204076Spjd * notice, this list of conditions and the following disclaimer in the 13204076Spjd * documentation and/or other materials provided with the distribution. 14204076Spjd * 15204076Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 16204076Spjd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17204076Spjd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18204076Spjd * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 19204076Spjd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20204076Spjd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21204076Spjd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22204076Spjd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23204076Spjd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24204076Spjd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25204076Spjd */ 26204076Spjd 27204076Spjd#include <sys/cdefs.h> 28204076Spjd__FBSDID("$FreeBSD: stable/11/sys/dev/atkbdc/atkbdc_isa.c 294883 2016-01-27 02:23:54Z jhibbits $"); 29204076Spjd 30204076Spjd#include "opt_kbd.h" 31204076Spjd 32204076Spjd#include <sys/param.h> 33204076Spjd#include <sys/systm.h> 34204076Spjd#include <sys/kernel.h> 35204076Spjd#include <sys/module.h> 36204076Spjd#include <sys/bus.h> 37204076Spjd#include <sys/malloc.h> 38204076Spjd#include <machine/resource.h> 39204076Spjd#include <sys/rman.h> 40204076Spjd#include <machine/bus.h> 41204076Spjd 42204076Spjd#include <dev/atkbdc/atkbdc_subr.h> 43204076Spjd#include <dev/atkbdc/atkbdcreg.h> 44204076Spjd 45204076Spjd#include <isa/isareg.h> 46225787Spjd#include <isa/isavar.h> 47204076Spjd 48225787Spjdstatic int atkbdc_isa_probe(device_t dev); 49225787Spjdstatic int atkbdc_isa_attach(device_t dev); 50225787Spjdstatic device_t atkbdc_isa_add_child(device_t bus, u_int order, const char *name, 51225787Spjd int unit); 52225787Spjdstatic struct resource *atkbdc_isa_alloc_resource(device_t dev, device_t child, 53225787Spjd int type, int *rid, rman_res_t start, rman_res_t end, 54225787Spjd rman_res_t count, u_int flags); 55225787Spjdstatic int atkbdc_isa_release_resource(device_t dev, device_t child, 56225787Spjd int type, int rid, struct resource *r); 57225787Spjd 58214283Spjdstatic device_method_t atkbdc_isa_methods[] = { 59214283Spjd DEVMETHOD(device_probe, atkbdc_isa_probe), 60214282Spjd DEVMETHOD(device_attach, atkbdc_isa_attach), 61214282Spjd DEVMETHOD(device_suspend, bus_generic_suspend), 62214282Spjd DEVMETHOD(device_resume, bus_generic_resume), 63214282Spjd 64214282Spjd DEVMETHOD(bus_add_child, atkbdc_isa_add_child), 65214282Spjd DEVMETHOD(bus_print_child, atkbdc_print_child), 66214282Spjd DEVMETHOD(bus_read_ivar, atkbdc_read_ivar), 67214282Spjd DEVMETHOD(bus_write_ivar, atkbdc_write_ivar), 68214282Spjd DEVMETHOD(bus_get_resource_list,atkbdc_get_resource_list), 69214282Spjd DEVMETHOD(bus_alloc_resource, atkbdc_isa_alloc_resource), 70214282Spjd DEVMETHOD(bus_release_resource, atkbdc_isa_release_resource), 71214282Spjd DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 72214282Spjd DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 73214282Spjd DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 74214282Spjd DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 75214282Spjd DEVMETHOD(bus_delete_resource, bus_generic_rl_delete_resource), 76214282Spjd DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 77214282Spjd DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 78214282Spjd 79214282Spjd { 0, 0 } 80214282Spjd}; 81214282Spjd 82214282Spjdstatic driver_t atkbdc_isa_driver = { 83214282Spjd ATKBDC_DRIVER_NAME, 84214282Spjd atkbdc_isa_methods, 85214282Spjd sizeof(atkbdc_softc_t *), 86214282Spjd}; 87204076Spjd 88204076Spjdstatic struct isa_pnp_id atkbdc_ids[] = { 89204076Spjd { 0x0303d041, "Keyboard controller (i8042)" }, /* PNP0303 */ 90204076Spjd { 0x0b03d041, "Keyboard controller (i8042)" }, /* PNP030B */ 91204076Spjd { 0x2003d041, "Keyboard controller (i8042)" }, /* PNP0320 */ 92204076Spjd { 0 } 93204076Spjd}; 94204076Spjd 95204076Spjdstatic int 96204076Spjdatkbdc_isa_probe(device_t dev) 97204076Spjd{ 98204076Spjd struct resource *port0; 99204076Spjd struct resource *port1; 100204076Spjd rman_res_t start; 101204076Spjd rman_res_t count; 102204076Spjd int error; 103204076Spjd int rid; 104204076Spjd#if defined(__i386__) || defined(__amd64__) 105204076Spjd bus_space_tag_t tag; 106204076Spjd bus_space_handle_t ioh1; 107204076Spjd volatile int i; 108204076Spjd register_t flags; 109204076Spjd#endif 110225787Spjd 111225787Spjd /* check PnP IDs */ 112204076Spjd if (ISA_PNP_PROBE(device_get_parent(dev), dev, atkbdc_ids) == ENXIO) 113204076Spjd return ENXIO; 114204076Spjd 115204076Spjd device_set_desc(dev, "Keyboard controller (i8042)"); 116204076Spjd 117204076Spjd /* 118204076Spjd * Adjust I/O port resources. 119204076Spjd * The AT keyboard controller uses two ports (a command/data port 120204076Spjd * 0x60 and a status port 0x64), which may be given to us in 121204076Spjd * one resource (0x60 through 0x64) or as two separate resources 122204076Spjd * (0x60 and 0x64). Some brain-damaged ACPI BIOS has reversed 123204076Spjd * command/data port and status port. Furthermore, /boot/device.hints 124204076Spjd * may contain just one port, 0x60. We shall adjust resource settings 125204076Spjd * so that these two ports are available as two separate resources 126204076Spjd * in correct order. 127204076Spjd */ 128204076Spjd device_quiet(dev); 129204076Spjd rid = 0; 130204076Spjd if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &start, &count) != 0) 131204076Spjd return ENXIO; 132204076Spjd if (start == IO_KBD + KBD_STATUS_PORT) { 133204076Spjd start = IO_KBD; 134204076Spjd count++; 135204076Spjd } 136204076Spjd if (count > 1) /* adjust the count and/or start port */ 137204076Spjd bus_set_resource(dev, SYS_RES_IOPORT, rid, start, 1); 138204076Spjd port0 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 139204076Spjd if (port0 == NULL) 140204076Spjd return ENXIO; 141204076Spjd rid = 1; 142204076Spjd if (bus_get_resource(dev, SYS_RES_IOPORT, rid, NULL, NULL) != 0) 143204076Spjd bus_set_resource(dev, SYS_RES_IOPORT, 1, 144204076Spjd start + KBD_STATUS_PORT, 1); 145204076Spjd port1 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 146204076Spjd if (port1 == NULL) { 147204076Spjd bus_release_resource(dev, SYS_RES_IOPORT, 0, port0); 148204076Spjd return ENXIO; 149204076Spjd } 150204076Spjd 151204076Spjd#if defined(__i386__) || defined(__amd64__) 152204076Spjd /* 153204076Spjd * Check if we really have AT keyboard controller. Poll status 154204076Spjd * register until we get "all clear" indication. If no such 155204076Spjd * indication comes, it probably means that there is no AT 156204076Spjd * keyboard controller present. Give up in such case. Check relies 157204076Spjd * on the fact that reading from non-existing in/out port returns 158204076Spjd * 0xff on i386. May or may not be true on other platforms. 159204076Spjd */ 160204076Spjd tag = rman_get_bustag(port0); 161204076Spjd ioh1 = rman_get_bushandle(port1); 162204076Spjd flags = intr_disable(); 163204076Spjd for (i = 0; i != 65535; i++) { 164204076Spjd if ((bus_space_read_1(tag, ioh1, 0) & 0x2) == 0) 165204076Spjd break; 166204076Spjd } 167204076Spjd intr_restore(flags); 168204076Spjd if (i == 65535) { 169204076Spjd bus_release_resource(dev, SYS_RES_IOPORT, 0, port0); 170204076Spjd bus_release_resource(dev, SYS_RES_IOPORT, 1, port1); 171204076Spjd if (bootverbose) 172204076Spjd device_printf(dev, "AT keyboard controller not found\n"); 173204076Spjd return ENXIO; 174204076Spjd } 175204076Spjd#endif 176204076Spjd 177204076Spjd device_verbose(dev); 178204076Spjd 179204076Spjd error = atkbdc_probe_unit(device_get_unit(dev), port0, port1); 180204076Spjd 181204076Spjd bus_release_resource(dev, SYS_RES_IOPORT, 0, port0); 182204076Spjd bus_release_resource(dev, SYS_RES_IOPORT, 1, port1); 183204076Spjd 184204076Spjd return error; 185204076Spjd} 186204076Spjd 187204076Spjdstatic int 188204076Spjdatkbdc_isa_attach(device_t dev) 189204076Spjd{ 190204076Spjd atkbdc_softc_t *sc; 191204076Spjd int unit; 192204076Spjd int error; 193204076Spjd int rid; 194204076Spjd 195204076Spjd unit = device_get_unit(dev); 196204076Spjd sc = *(atkbdc_softc_t **)device_get_softc(dev); 197204076Spjd if (sc == NULL) { 198204076Spjd /* 199204076Spjd * We have to maintain two copies of the kbdc_softc struct, 200204076Spjd * as the low-level console needs to have access to the 201204076Spjd * keyboard controller before kbdc is probed and attached. 202204076Spjd * kbdc_soft[] contains the default entry for that purpose. 203204076Spjd * See atkbdc.c. XXX 204204076Spjd */ 205204076Spjd sc = atkbdc_get_softc(unit); 206204076Spjd if (sc == NULL) 207204076Spjd return ENOMEM; 208204076Spjd } 209204076Spjd 210204076Spjd rid = 0; 211204076Spjd sc->retry = 5000; 212225787Spjd sc->port0 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, 213204076Spjd RF_ACTIVE); 214204076Spjd if (sc->port0 == NULL) 215204076Spjd return ENXIO; 216204076Spjd rid = 1; 217204076Spjd sc->port1 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, 218204076Spjd RF_ACTIVE); 219204076Spjd if (sc->port1 == NULL) { 220204076Spjd bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port0); 221204076Spjd return ENXIO; 222204076Spjd } 223204076Spjd 224204076Spjd /* 225204076Spjd * If the device is not created by the PnP BIOS or ACPI, then 226204076Spjd * the hint for the IRQ is on the child atkbd device, not the 227204076Spjd * keyboard controller, so this can fail. 228204076Spjd */ 229204076Spjd rid = 0; 230204076Spjd sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE); 231204076Spjd 232204076Spjd error = atkbdc_attach_unit(unit, sc, sc->port0, sc->port1); 233204076Spjd if (error) { 234204076Spjd bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port0); 235204076Spjd bus_release_resource(dev, SYS_RES_IOPORT, 1, sc->port1); 236204076Spjd if (sc->irq != NULL) 237204076Spjd bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq); 238204076Spjd return error; 239204076Spjd } 240204076Spjd *(atkbdc_softc_t **)device_get_softc(dev) = sc; 241204076Spjd 242204076Spjd bus_generic_probe(dev); 243204076Spjd bus_generic_attach(dev); 244204076Spjd 245204076Spjd return 0; 246204076Spjd} 247204076Spjd 248204076Spjdstatic device_t 249204076Spjdatkbdc_isa_add_child(device_t bus, u_int order, const char *name, int unit) 250204076Spjd{ 251204076Spjd atkbdc_device_t *ivar; 252204076Spjd atkbdc_softc_t *sc; 253204076Spjd device_t child; 254204076Spjd int t; 255204076Spjd 256204076Spjd sc = *(atkbdc_softc_t **)device_get_softc(bus); 257204076Spjd ivar = malloc(sizeof(struct atkbdc_device), M_ATKBDDEV, 258204076Spjd M_NOWAIT | M_ZERO); 259204076Spjd if (!ivar) 260204076Spjd return NULL; 261204076Spjd 262204076Spjd child = device_add_child_ordered(bus, order, name, unit); 263204076Spjd if (child == NULL) { 264204076Spjd free(ivar, M_ATKBDDEV); 265204076Spjd return child; 266204076Spjd } 267204076Spjd 268204076Spjd resource_list_init(&ivar->resources); 269204076Spjd ivar->rid = order; 270204076Spjd 271204076Spjd /* 272204076Spjd * If the device is not created by the PnP BIOS or ACPI, refer 273204076Spjd * to device hints for IRQ. We always populate the resource 274204076Spjd * list entry so we can use a standard bus_get_resource() 275204076Spjd * method. 276204076Spjd */ 277204076Spjd if (order == KBDC_RID_KBD) { 278204076Spjd if (sc->irq == NULL) { 279204076Spjd if (resource_int_value(name, unit, "irq", &t) != 0) 280204076Spjd t = -1; 281204076Spjd } else 282204076Spjd t = rman_get_start(sc->irq); 283204076Spjd if (t > 0) 284204076Spjd resource_list_add(&ivar->resources, SYS_RES_IRQ, 285204076Spjd ivar->rid, t, t, 1); 286204076Spjd } 287204076Spjd 288204076Spjd if (resource_disabled(name, unit)) 289204076Spjd device_disable(child); 290204076Spjd 291204076Spjd device_set_ivars(child, ivar); 292204076Spjd 293204076Spjd return child; 294204076Spjd} 295204076Spjd 296204076Spjdstruct resource * 297204076Spjdatkbdc_isa_alloc_resource(device_t dev, device_t child, int type, int *rid, 298204076Spjd rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 299204076Spjd{ 300204076Spjd atkbdc_softc_t *sc; 301204076Spjd 302204076Spjd sc = *(atkbdc_softc_t **)device_get_softc(dev); 303204076Spjd if (type == SYS_RES_IRQ && *rid == KBDC_RID_KBD && sc->irq != NULL) 304204076Spjd return (sc->irq); 305204076Spjd return (bus_generic_rl_alloc_resource(dev, child, type, rid, start, 306204076Spjd end, count, flags)); 307204076Spjd} 308204076Spjd 309204076Spjdstatic int 310204076Spjdatkbdc_isa_release_resource(device_t dev, device_t child, int type, int rid, 311204076Spjd struct resource *r) 312204076Spjd{ 313204076Spjd atkbdc_softc_t *sc; 314204076Spjd 315204076Spjd sc = *(atkbdc_softc_t **)device_get_softc(dev); 316204076Spjd if (type == SYS_RES_IRQ && rid == KBDC_RID_KBD && r == sc->irq) 317204076Spjd return (0); 318204076Spjd return (bus_generic_rl_release_resource(dev, child, type, rid, r)); 319204076Spjd} 320225787Spjd 321204076SpjdDRIVER_MODULE(atkbdc, isa, atkbdc_isa_driver, atkbdc_devclass, 0, 0); 322204076SpjdDRIVER_MODULE(atkbdc, acpi, atkbdc_isa_driver, atkbdc_devclass, 0, 0); 323204076Spjd