atkbdc_isa.c revision 129880
143105Sdfr/*- 243105Sdfr * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp> 343105Sdfr * All rights reserved. 443105Sdfr * 543105Sdfr * Redistribution and use in source and binary forms, with or without 643105Sdfr * modification, are permitted provided that the following conditions 743105Sdfr * are met: 843105Sdfr * 1. Redistributions of source code must retain the above copyright 943105Sdfr * notice, this list of conditions and the following disclaimer as 1043105Sdfr * the first lines of this file unmodified. 1143105Sdfr * 2. Redistributions in binary form must reproduce the above copyright 1243105Sdfr * notice, this list of conditions and the following disclaimer in the 1343105Sdfr * documentation and/or other materials provided with the distribution. 1443105Sdfr * 1543105Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 1643105Sdfr * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1743105Sdfr * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1843105Sdfr * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 1943105Sdfr * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2043105Sdfr * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2143105Sdfr * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2243105Sdfr * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2343105Sdfr * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2443105Sdfr * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2543105Sdfr */ 2643105Sdfr 27116181Sobrien#include <sys/cdefs.h> 28116181Sobrien__FBSDID("$FreeBSD: head/sys/dev/atkbdc/atkbdc_isa.c 129880 2004-05-30 20:27:19Z phk $"); 29116181Sobrien 3043105Sdfr#include "opt_kbd.h" 3143105Sdfr 3243105Sdfr#include <sys/param.h> 3343105Sdfr#include <sys/systm.h> 3443105Sdfr#include <sys/kernel.h> 35129880Sphk#include <sys/module.h> 3643105Sdfr#include <sys/bus.h> 3743105Sdfr#include <sys/malloc.h> 3847400Sdfr#include <machine/bus_pio.h> 3947400Sdfr#include <machine/bus.h> 40159541Simp#include <machine/resource.h> 4143105Sdfr#include <sys/rman.h> 42147271Smarius 43147271Smarius#include <dev/kbd/atkbdcreg.h> 4443105Sdfr 4543105Sdfr#include <isa/isareg.h> 4643105Sdfr#include <isa/isavar.h> 4743105Sdfr 48147271Smariusstatic MALLOC_DEFINE(M_ATKBDDEV, "atkbddev", "AT Keyboard device"); 49147271Smarius 50188160Simp/* children */ 51188160Simptypedef struct atkbdc_device { 5243105Sdfr struct resource_list resources; 53147271Smarius int rid; 54147271Smarius u_int32_t vendorid; 55147271Smarius u_int32_t serial; 5647296Syokota u_int32_t logicalid; 5747296Syokota u_int32_t compatid; 5843105Sdfr} atkbdc_device_t; 59147271Smarius 6043105Sdfr/* kbdc */ 6143105Sdfrstatic devclass_t atkbdc_devclass; 6243105Sdfr 6383147Syokotastatic int atkbdc_probe(device_t dev); 64147271Smariusstatic int atkbdc_attach(device_t dev); 65147271Smariusstatic device_t atkbdc_add_child(device_t bus, int order, char *name, 6643105Sdfr int unit); 6743105Sdfrstatic int atkbdc_print_child(device_t bus, device_t dev); 6883147Syokotastatic int atkbdc_read_ivar(device_t bus, device_t dev, int index, 6983147Syokota uintptr_t *val); 7083147Syokotastatic int atkbdc_write_ivar(device_t bus, device_t dev, int index, 7143105Sdfr uintptr_t val); 7243105Sdfrstatic struct resource_list 7343105Sdfr *atkbdc_get_resource_list (device_t bus, device_t dev); 7443105Sdfrstatic struct resource 7543105Sdfr *atkbdc_alloc_resource(device_t bus, device_t dev, int type, 7643105Sdfr int *rid, u_long start, u_long end, 77147271Smarius u_long count, u_int flags); 7843105Sdfrstatic int atkbdc_release_resource(device_t bus, device_t dev, int type, 79147271Smarius int rid, struct resource *res); 8043105Sdfr 8143105Sdfrstatic device_method_t atkbdc_methods[] = { 8243105Sdfr DEVMETHOD(device_probe, atkbdc_probe), 8358271Syokota DEVMETHOD(device_attach, atkbdc_attach), 8458271Syokota DEVMETHOD(device_suspend, bus_generic_suspend), 8558271Syokota DEVMETHOD(device_resume, bus_generic_resume), 8658271Syokota 8758271Syokota DEVMETHOD(bus_add_child, atkbdc_add_child), 8843105Sdfr DEVMETHOD(bus_print_child, atkbdc_print_child), 89147271Smarius DEVMETHOD(bus_read_ivar, atkbdc_read_ivar), 9043105Sdfr DEVMETHOD(bus_write_ivar, atkbdc_write_ivar), 9158271Syokota DEVMETHOD(bus_get_resource_list,atkbdc_get_resource_list), 9258271Syokota DEVMETHOD(bus_alloc_resource, atkbdc_alloc_resource), 9383147Syokota DEVMETHOD(bus_release_resource, atkbdc_release_resource), 9483147Syokota DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 9558271Syokota DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 9658271Syokota DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 97158041Ssobomax DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 98158041Ssobomax DEVMETHOD(bus_delete_resource, bus_generic_rl_delete_resource), 99158041Ssobomax DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 100158041Ssobomax DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 101158041Ssobomax 102158041Ssobomax { 0, 0 } 10343105Sdfr}; 10458271Syokota 10558271Syokotastatic driver_t atkbdc_driver = { 10658271Syokota ATKBDC_DRIVER_NAME, 10747618Sdfr atkbdc_methods, 10858271Syokota sizeof(atkbdc_softc_t *), 10958271Syokota}; 11083147Syokota 11183147Syokotastatic struct isa_pnp_id atkbdc_ids[] = { 11283147Syokota { 0x0303d041, "Keyboard controller (i8042)" }, /* PNP0303 */ 11383147Syokota { 0 } 11483147Syokota}; 115160091Sjkim 116160091Sjkimstatic int 117160091Sjkimatkbdc_probe(device_t dev) 118160091Sjkim{ 119160091Sjkim struct resource *port0; 12083147Syokota struct resource *port1; 12183147Syokota u_long start; 12247400Sdfr u_long count; 12383147Syokota int error; 12483147Syokota int rid; 125160091Sjkim 126160091Sjkim /* check PnP IDs */ 127160091Sjkim if (ISA_PNP_PROBE(device_get_parent(dev), dev, atkbdc_ids) == ENXIO) 128160091Sjkim return ENXIO; 129160091Sjkim 13083147Syokota device_set_desc(dev, "Keyboard controller (i8042)"); 131127135Snjl 13258271Syokota /* 13347400Sdfr * Adjust I/O port resources. 13483147Syokota * The AT keyboard controller uses two ports (a command/data port 13583147Syokota * 0x60 and a status port 0x64), which may be given to us in 13658271Syokota * one resource (0x60 through 0x64) or as two separate resources 13783147Syokota * (0x60 and 0x64). Furthermore, /boot/device.hints may contain 138127135Snjl * just one port, 0x60. We shall adjust resource settings 13958271Syokota * so that these two ports are available as two separate resources. 14058271Syokota */ 14158271Syokota device_quiet(dev); 14258271Syokota rid = 0; 143158041Ssobomax if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &start, &count) != 0) 144158041Ssobomax return ENXIO; 145158041Ssobomax if (count > 1) /* adjust the count */ 146158041Ssobomax bus_set_resource(dev, SYS_RES_IOPORT, rid, start, 1); 147158041Ssobomax port0 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 148158041Ssobomax if (port0 == NULL) 149158041Ssobomax return ENXIO; 150158041Ssobomax rid = 1; 151158041Ssobomax if (bus_get_resource(dev, SYS_RES_IOPORT, rid, NULL, NULL) != 0) 152158041Ssobomax bus_set_resource(dev, SYS_RES_IOPORT, 1, 153158041Ssobomax start + KBD_STATUS_PORT, 1); 154158041Ssobomax port1 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 155158041Ssobomax if (port1 == NULL) { 156158041Ssobomax bus_release_resource(dev, SYS_RES_IOPORT, 0, port0); 157158041Ssobomax return ENXIO; 158158041Ssobomax } 159158041Ssobomax device_verbose(dev); 160158041Ssobomax 161158041Ssobomax error = atkbdc_probe_unit(device_get_unit(dev), port0, port1); 162158041Ssobomax if (error == 0) 163158041Ssobomax bus_generic_probe(dev); 164158041Ssobomax 165158041Ssobomax bus_release_resource(dev, SYS_RES_IOPORT, 0, port0); 166158041Ssobomax bus_release_resource(dev, SYS_RES_IOPORT, 1, port1); 167158041Ssobomax 16883147Syokota return error; 16958271Syokota} 17058271Syokota 17183147Syokotastatic int 17283147Syokotaatkbdc_attach(device_t dev) 17358271Syokota{ 17458271Syokota atkbdc_softc_t *sc; 17558271Syokota int unit; 17658271Syokota int error; 17743105Sdfr int rid; 17843105Sdfr 17943105Sdfr unit = device_get_unit(dev); 18043105Sdfr sc = *(atkbdc_softc_t **)device_get_softc(dev); 181147271Smarius if (sc == NULL) { 18243105Sdfr /* 18343105Sdfr * We have to maintain two copies of the kbdc_softc struct, 18483147Syokota * as the low-level console needs to have access to the 18547296Syokota * keyboard controller before kbdc is probed and attached. 18648341Syokota * kbdc_soft[] contains the default entry for that purpose. 18743105Sdfr * See atkbdc.c. XXX 18847296Syokota */ 18943105Sdfr sc = atkbdc_get_softc(unit); 19047296Syokota if (sc == NULL) 19147296Syokota return ENOMEM; 19247296Syokota } 19347296Syokota 194147271Smarius rid = 0; 19547296Syokota sc->port0 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, 19647296Syokota RF_ACTIVE); 19747296Syokota if (sc->port0 == NULL) 19847296Syokota return ENXIO; 19947296Syokota rid = 1; 20047296Syokota sc->port1 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, 20147296Syokota RF_ACTIVE); 20243105Sdfr if (sc->port1 == NULL) { 20348341Syokota bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port0); 204127135Snjl return ENXIO; 205127135Snjl } 20658271Syokota 20748341Syokota error = atkbdc_attach_unit(unit, sc, sc->port0, sc->port1); 20858271Syokota if (error) { 209127135Snjl bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port0); 210127135Snjl bus_release_resource(dev, SYS_RES_IOPORT, 1, sc->port1); 21158271Syokota return error; 21258271Syokota } 21358271Syokota *(atkbdc_softc_t **)device_get_softc(dev) = sc; 21458271Syokota 21558271Syokota bus_generic_attach(dev); 21658271Syokota 21758271Syokota return 0; 21858271Syokota} 21958271Syokota 22047296Syokotastatic device_t 22158271Syokotaatkbdc_add_child(device_t bus, int order, char *name, int unit) 22247296Syokota{ 22347296Syokota atkbdc_device_t *ivar; 22483147Syokota device_t child; 22543105Sdfr int t; 22683147Syokota 22783147Syokota ivar = malloc(sizeof(struct atkbdc_device), M_ATKBDDEV, 22883147Syokota M_NOWAIT | M_ZERO); 22983147Syokota if (!ivar) 230188160Simp return NULL; 23183147Syokota 23283147Syokota child = device_add_child_ordered(bus, order, name, unit); 23383147Syokota if (child == NULL) { 23483147Syokota free(ivar, M_ATKBDDEV); 23583147Syokota return child; 23683147Syokota } 23783147Syokota 23883147Syokota resource_list_init(&ivar->resources); 23983147Syokota ivar->rid = order; 24083147Syokota 24183931Syokota /* 24283147Syokota * If the device is not created by the PnP BIOS or ACPI, 24383147Syokota * refer to device hints for IRQ. 24483147Syokota */ 24583147Syokota if (ISA_PNP_PROBE(device_get_parent(bus), bus, atkbdc_ids) != 0) { 24683147Syokota if (resource_int_value(name, unit, "irq", &t) != 0) 24783147Syokota t = -1; 24883147Syokota } else { 24983147Syokota t = bus_get_resource_start(bus, SYS_RES_IRQ, ivar->rid); 25043105Sdfr } 25183147Syokota if (t > 0) 25283147Syokota resource_list_add(&ivar->resources, SYS_RES_IRQ, ivar->rid, 25343105Sdfr t, t, 1); 25483147Syokota 25583147Syokota if (resource_int_value(name, unit, "flags", &t) == 0) 25683147Syokota device_set_flags(child, t); 25783147Syokota if (resource_disabled(name, unit)) 25883147Syokota device_disable(child); 25983147Syokota 26083147Syokota device_set_ivars(child, ivar); 26183147Syokota 26283147Syokota return child; 26343105Sdfr} 264117167Sjhb 26583147Syokotastatic int 26643105Sdfratkbdc_print_child(device_t bus, device_t dev) 26783147Syokota{ 26883147Syokota atkbdc_device_t *kbdcdev; 26983147Syokota u_long irq; 27043105Sdfr int flags; 27143105Sdfr int retval = 0; 272147271Smarius 273147271Smarius kbdcdev = (atkbdc_device_t *)device_get_ivars(dev); 274 275 retval += bus_print_child_header(bus, dev); 276 flags = device_get_flags(dev); 277 if (flags != 0) 278 retval += printf(" flags 0x%x", flags); 279 irq = bus_get_resource_start(dev, SYS_RES_IRQ, kbdcdev->rid); 280 if (irq != 0) 281 retval += printf(" irq %ld", irq); 282 retval += bus_print_child_footer(bus, dev); 283 284 return (retval); 285} 286 287static int 288atkbdc_read_ivar(device_t bus, device_t dev, int index, uintptr_t *val) 289{ 290 atkbdc_device_t *ivar; 291 292 ivar = (atkbdc_device_t *)device_get_ivars(dev); 293 switch (index) { 294 case KBDC_IVAR_VENDORID: 295 *val = (u_long)ivar->vendorid; 296 break; 297 case KBDC_IVAR_SERIAL: 298 *val = (u_long)ivar->serial; 299 break; 300 case KBDC_IVAR_LOGICALID: 301 *val = (u_long)ivar->logicalid; 302 break; 303 case KBDC_IVAR_COMPATID: 304 *val = (u_long)ivar->compatid; 305 break; 306 default: 307 return ENOENT; 308 } 309 return 0; 310} 311 312static int 313atkbdc_write_ivar(device_t bus, device_t dev, int index, uintptr_t val) 314{ 315 atkbdc_device_t *ivar; 316 317 ivar = (atkbdc_device_t *)device_get_ivars(dev); 318 switch (index) { 319 case KBDC_IVAR_VENDORID: 320 ivar->vendorid = (u_int32_t)val; 321 break; 322 case KBDC_IVAR_SERIAL: 323 ivar->serial = (u_int32_t)val; 324 break; 325 case KBDC_IVAR_LOGICALID: 326 ivar->logicalid = (u_int32_t)val; 327 break; 328 case KBDC_IVAR_COMPATID: 329 ivar->compatid = (u_int32_t)val; 330 break; 331 default: 332 return ENOENT; 333 } 334 return 0; 335} 336 337static struct resource_list 338*atkbdc_get_resource_list (device_t bus, device_t dev) 339{ 340 atkbdc_device_t *ivar; 341 342 ivar = (atkbdc_device_t *)device_get_ivars(dev); 343 return &ivar->resources; 344} 345 346static struct resource 347*atkbdc_alloc_resource(device_t bus, device_t dev, int type, int *rid, 348 u_long start, u_long end, u_long count, u_int flags) 349{ 350 atkbdc_device_t *ivar; 351 352 ivar = (atkbdc_device_t *)device_get_ivars(dev); 353 return resource_list_alloc(&ivar->resources, bus, dev, type, rid, 354 start, end, count, flags); 355} 356 357static int 358atkbdc_release_resource(device_t bus, device_t dev, int type, int rid, 359 struct resource *res) 360{ 361 atkbdc_device_t *ivar; 362 363 ivar = (atkbdc_device_t *)device_get_ivars(dev); 364 return resource_list_release(&ivar->resources, bus, dev, type, rid, 365 res); 366} 367 368DRIVER_MODULE(atkbdc, isa, atkbdc_driver, atkbdc_devclass, 0, 0); 369DRIVER_MODULE(atkbdc, acpi, atkbdc_driver, atkbdc_devclass, 0, 0); 370