isa_common.c revision 53094
147398Sdfr/*- 247398Sdfr * Copyright (c) 1999 Doug Rabson 347398Sdfr * All rights reserved. 447398Sdfr * 547398Sdfr * Redistribution and use in source and binary forms, with or without 647398Sdfr * modification, are permitted provided that the following conditions 747398Sdfr * are met: 847398Sdfr * 1. Redistributions of source code must retain the above copyright 947398Sdfr * notice, this list of conditions and the following disclaimer. 1047398Sdfr * 2. Redistributions in binary form must reproduce the above copyright 1147398Sdfr * notice, this list of conditions and the following disclaimer in the 1247398Sdfr * documentation and/or other materials provided with the distribution. 1347398Sdfr * 1447398Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1547398Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1647398Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1747398Sdfr * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1847398Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1947398Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2047398Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2147398Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2247398Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2347398Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2447398Sdfr * SUCH DAMAGE. 2547398Sdfr * 2650477Speter * $FreeBSD: head/sys/isa/isa_common.c 53094 1999-11-11 16:48:00Z dfr $ 2747398Sdfr */ 2847398Sdfr/* 2947398Sdfr * Modifications for Intel architecture by Garrett A. Wollman. 3047398Sdfr * Copyright 1998 Massachusetts Institute of Technology 3147398Sdfr * 3247398Sdfr * Permission to use, copy, modify, and distribute this software and 3347398Sdfr * its documentation for any purpose and without fee is hereby 3447398Sdfr * granted, provided that both the above copyright notice and this 3547398Sdfr * permission notice appear in all copies, that both the above 3647398Sdfr * copyright notice and this permission notice appear in all 3747398Sdfr * supporting documentation, and that the name of M.I.T. not be used 3847398Sdfr * in advertising or publicity pertaining to distribution of the 3947398Sdfr * software without specific, written prior permission. M.I.T. makes 4047398Sdfr * no representations about the suitability of this software for any 4147398Sdfr * purpose. It is provided "as is" without express or implied 4247398Sdfr * warranty. 4347398Sdfr * 4447398Sdfr * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 4547398Sdfr * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 4647398Sdfr * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 4747398Sdfr * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 4847398Sdfr * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 4947398Sdfr * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 5047398Sdfr * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 5147398Sdfr * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 5247398Sdfr * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 5347398Sdfr * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 5447398Sdfr * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5547398Sdfr * SUCH DAMAGE. 5647398Sdfr */ 5747398Sdfr 5847398Sdfr/* 5947398Sdfr * Parts of the ISA bus implementation common to all architectures. 6047398Sdfr */ 6147398Sdfr 6247398Sdfr#include <sys/param.h> 6347398Sdfr#include <sys/systm.h> 6447398Sdfr#include <sys/kernel.h> 6547398Sdfr#include <sys/bus.h> 6647398Sdfr#include <sys/malloc.h> 6747398Sdfr#include <sys/module.h> 6847398Sdfr#include <machine/bus.h> 6947398Sdfr#include <sys/rman.h> 7047398Sdfr 7147398Sdfr#include <machine/resource.h> 7247398Sdfr 7347398Sdfr#include <isa/isavar.h> 7447398Sdfr#include <isa/isa_common.h> 7547398Sdfr#ifdef __alpha__ /* XXX workaround a stupid warning */ 7647398Sdfr#include <alpha/isa/isavar.h> 7747398Sdfr#endif 7847398Sdfr 7947398SdfrMALLOC_DEFINE(M_ISADEV, "isadev", "ISA device"); 8047398Sdfr 8147398Sdfrstatic devclass_t isa_devclass; 8253094Sdfrstatic int isa_running; 8347398Sdfr 8447398Sdfr/* 8547398Sdfr * At 'probe' time, we add all the devices which we know about to the 8647398Sdfr * bus. The generic attach routine will probe and attach them if they 8747398Sdfr * are alive. 8847398Sdfr */ 8947398Sdfrstatic int 9047398Sdfrisa_probe(device_t dev) 9147398Sdfr{ 9247398Sdfr device_set_desc(dev, "ISA bus"); 9347398Sdfr isa_init(); /* Allow machdep code to initialise */ 9453094Sdfr return 0; 9547398Sdfr} 9647398Sdfr 9747398Sdfrextern device_t isa_bus_device; 9847398Sdfr 9947398Sdfrstatic int 10047398Sdfrisa_attach(device_t dev) 10147398Sdfr{ 10247398Sdfr /* 10350769Sdfr * Arrange for isa_probe_children(dev) to be called later. XXX 10447398Sdfr */ 10547398Sdfr isa_bus_device = dev; 10647398Sdfr return 0; 10747398Sdfr} 10847398Sdfr 10947398Sdfr/* 11050769Sdfr * Find a working set of memory regions for a child using the ranges 11150769Sdfr * in *config and return the regions in *result. Returns non-zero if 11250769Sdfr * a set of ranges was found. 11350769Sdfr */ 11450769Sdfrstatic int 11550769Sdfrisa_find_memory(device_t child, 11650769Sdfr struct isa_config *config, 11750769Sdfr struct isa_config *result) 11850769Sdfr{ 11950769Sdfr int success, i; 12050769Sdfr struct resource *res[ISA_NMEM]; 12150769Sdfr 12250769Sdfr /* 12350769Sdfr * First clear out any existing resource definitions. 12450769Sdfr */ 12550769Sdfr for (i = 0; i < ISA_NMEM; i++) { 12652174Sdfr bus_delete_resource(child, SYS_RES_MEMORY, i); 12750769Sdfr res[i] = NULL; 12850769Sdfr } 12950769Sdfr 13050769Sdfr success = 1; 13150769Sdfr result->ic_nmem = config->ic_nmem; 13250769Sdfr for (i = 0; i < config->ic_nmem; i++) { 13350769Sdfr u_int32_t start, end, size, align; 13450769Sdfr for (start = config->ic_mem[i].ir_start, 13550769Sdfr end = config->ic_mem[i].ir_end, 13650769Sdfr size = config->ic_mem[i].ir_size, 13750769Sdfr align = config->ic_mem[i].ir_align; 13850769Sdfr start + size - 1 <= end; 13950769Sdfr start += align) { 14052174Sdfr bus_set_resource(child, SYS_RES_MEMORY, i, 14150769Sdfr start, size); 14250769Sdfr res[i] = bus_alloc_resource(child, 14350769Sdfr SYS_RES_MEMORY, &i, 14450769Sdfr 0, ~0, 1, RF_ACTIVE); 14550769Sdfr if (res[i]) { 14650769Sdfr result->ic_mem[i].ir_start = start; 14750769Sdfr result->ic_mem[i].ir_end = start + size - 1; 14850769Sdfr result->ic_mem[i].ir_size = size; 14950769Sdfr result->ic_mem[i].ir_align = align; 15050769Sdfr break; 15150769Sdfr } 15250769Sdfr } 15350769Sdfr 15450769Sdfr /* 15550769Sdfr * If we didn't find a place for memory range i, then 15650769Sdfr * give up now. 15750769Sdfr */ 15850769Sdfr if (!res[i]) { 15950769Sdfr success = 0; 16050769Sdfr break; 16150769Sdfr } 16250769Sdfr } 16350769Sdfr 16450769Sdfr for (i = 0; i < ISA_NMEM; i++) { 16550769Sdfr if (res[i]) 16650769Sdfr bus_release_resource(child, SYS_RES_MEMORY, 16750769Sdfr i, res[i]); 16850769Sdfr } 16950769Sdfr 17050769Sdfr return success; 17150769Sdfr} 17250769Sdfr 17350769Sdfr/* 17450769Sdfr * Find a working set of port regions for a child using the ranges 17550769Sdfr * in *config and return the regions in *result. Returns non-zero if 17650769Sdfr * a set of ranges was found. 17750769Sdfr */ 17850769Sdfrstatic int 17950769Sdfrisa_find_port(device_t child, 18050769Sdfr struct isa_config *config, 18150769Sdfr struct isa_config *result) 18250769Sdfr{ 18350769Sdfr int success, i; 18450769Sdfr struct resource *res[ISA_NPORT]; 18550769Sdfr 18650769Sdfr /* 18750769Sdfr * First clear out any existing resource definitions. 18850769Sdfr */ 18950769Sdfr for (i = 0; i < ISA_NPORT; i++) { 19052174Sdfr bus_delete_resource(child, SYS_RES_IOPORT, i); 19150769Sdfr res[i] = NULL; 19250769Sdfr } 19350769Sdfr 19450769Sdfr success = 1; 19550769Sdfr result->ic_nport = config->ic_nport; 19650769Sdfr for (i = 0; i < config->ic_nport; i++) { 19750769Sdfr u_int32_t start, end, size, align; 19850769Sdfr for (start = config->ic_port[i].ir_start, 19950769Sdfr end = config->ic_port[i].ir_end, 20050769Sdfr size = config->ic_port[i].ir_size, 20150769Sdfr align = config->ic_port[i].ir_align; 20250769Sdfr start + size - 1 <= end; 20350769Sdfr start += align) { 20452174Sdfr bus_set_resource(child, SYS_RES_IOPORT, i, 20550769Sdfr start, size); 20650769Sdfr res[i] = bus_alloc_resource(child, 20750769Sdfr SYS_RES_IOPORT, &i, 20850769Sdfr 0, ~0, 1, RF_ACTIVE); 20950769Sdfr if (res[i]) { 21050769Sdfr result->ic_port[i].ir_start = start; 21150769Sdfr result->ic_port[i].ir_end = start + size - 1; 21250769Sdfr result->ic_port[i].ir_size = size; 21350769Sdfr result->ic_port[i].ir_align = align; 21450769Sdfr break; 21550769Sdfr } 21650769Sdfr } 21750769Sdfr 21850769Sdfr /* 21950769Sdfr * If we didn't find a place for port range i, then 22050769Sdfr * give up now. 22150769Sdfr */ 22250769Sdfr if (!res[i]) { 22350769Sdfr success = 0; 22450769Sdfr break; 22550769Sdfr } 22650769Sdfr } 22750769Sdfr 22850769Sdfr for (i = 0; i < ISA_NPORT; i++) { 22950769Sdfr if (res[i]) 23050769Sdfr bus_release_resource(child, SYS_RES_IOPORT, 23150769Sdfr i, res[i]); 23250769Sdfr } 23350769Sdfr 23450769Sdfr return success; 23550769Sdfr} 23650769Sdfr 23750769Sdfr/* 23850769Sdfr * Return the index of the first bit in the mask (or -1 if mask is empty. 23950769Sdfr */ 24050769Sdfrstatic int 24150769Sdfrfind_first_bit(u_int32_t mask) 24250769Sdfr{ 24350769Sdfr return ffs(mask) - 1; 24450769Sdfr} 24550769Sdfr 24650769Sdfr/* 24750769Sdfr * Return the index of the next bit in the mask, or -1 if there are no more. 24850769Sdfr */ 24950769Sdfrstatic int 25050769Sdfrfind_next_bit(u_int32_t mask, int bit) 25150769Sdfr{ 25250769Sdfr bit++; 25350769Sdfr while (bit < 32 && !(mask & (1 << bit))) 25450769Sdfr bit++; 25550769Sdfr if (bit != 32) 25650769Sdfr return bit; 25750769Sdfr return -1; 25850769Sdfr} 25950769Sdfr 26050769Sdfr/* 26150769Sdfr * Find a working set of irqs for a child using the masks in *config 26250769Sdfr * and return the regions in *result. Returns non-zero if a set of 26350769Sdfr * irqs was found. 26450769Sdfr */ 26550769Sdfrstatic int 26650769Sdfrisa_find_irq(device_t child, 26750769Sdfr struct isa_config *config, 26850769Sdfr struct isa_config *result) 26950769Sdfr{ 27050769Sdfr int success, i; 27150769Sdfr struct resource *res[ISA_NIRQ]; 27250769Sdfr 27350769Sdfr /* 27450769Sdfr * First clear out any existing resource definitions. 27550769Sdfr */ 27650769Sdfr for (i = 0; i < ISA_NIRQ; i++) { 27752174Sdfr bus_delete_resource(child, SYS_RES_IRQ, i); 27850769Sdfr res[i] = NULL; 27950769Sdfr } 28050769Sdfr 28150769Sdfr success = 1; 28250769Sdfr result->ic_nirq = config->ic_nirq; 28350769Sdfr for (i = 0; i < config->ic_nirq; i++) { 28450769Sdfr u_int32_t mask = config->ic_irqmask[i]; 28550769Sdfr int irq; 28650769Sdfr for (irq = find_first_bit(mask); 28750769Sdfr irq != -1; 28850769Sdfr irq = find_next_bit(mask, irq)) { 28952174Sdfr bus_set_resource(child, SYS_RES_IRQ, i, 29050769Sdfr irq, 1); 29150769Sdfr res[i] = bus_alloc_resource(child, 29250769Sdfr SYS_RES_IRQ, &i, 29350769Sdfr 0, ~0, 1, RF_ACTIVE); 29450769Sdfr if (res[i]) { 29550769Sdfr result->ic_irqmask[i] = (1 << irq); 29650769Sdfr break; 29750769Sdfr } 29850769Sdfr } 29950769Sdfr 30050769Sdfr /* 30150769Sdfr * If we didn't find a place for irq range i, then 30250769Sdfr * give up now. 30350769Sdfr */ 30450769Sdfr if (!res[i]) { 30550769Sdfr success = 0; 30650769Sdfr break; 30750769Sdfr } 30850769Sdfr } 30950769Sdfr 31050769Sdfr for (i = 0; i < ISA_NIRQ; i++) { 31150769Sdfr if (res[i]) 31250769Sdfr bus_release_resource(child, SYS_RES_IRQ, 31350769Sdfr i, res[i]); 31450769Sdfr } 31550769Sdfr 31650769Sdfr return success; 31750769Sdfr} 31850769Sdfr 31950769Sdfr/* 32050769Sdfr * Find a working set of drqs for a child using the masks in *config 32150769Sdfr * and return the regions in *result. Returns non-zero if a set of 32250769Sdfr * drqs was found. 32350769Sdfr */ 32450769Sdfrstatic int 32550769Sdfrisa_find_drq(device_t child, 32650769Sdfr struct isa_config *config, 32750769Sdfr struct isa_config *result) 32850769Sdfr{ 32950769Sdfr int success, i; 33050769Sdfr struct resource *res[ISA_NDRQ]; 33150769Sdfr 33250769Sdfr /* 33350769Sdfr * First clear out any existing resource definitions. 33450769Sdfr */ 33550769Sdfr for (i = 0; i < ISA_NDRQ; i++) { 33652174Sdfr bus_delete_resource(child, SYS_RES_DRQ, i); 33750769Sdfr res[i] = NULL; 33850769Sdfr } 33950769Sdfr 34050769Sdfr success = 1; 34150769Sdfr result->ic_ndrq = config->ic_ndrq; 34250769Sdfr for (i = 0; i < config->ic_ndrq; i++) { 34350769Sdfr u_int32_t mask = config->ic_drqmask[i]; 34450769Sdfr int drq; 34550769Sdfr for (drq = find_first_bit(mask); 34650769Sdfr drq != -1; 34750769Sdfr drq = find_next_bit(mask, drq)) { 34852174Sdfr bus_set_resource(child, SYS_RES_DRQ, i, 34950769Sdfr drq, 1); 35050769Sdfr res[i] = bus_alloc_resource(child, 35150769Sdfr SYS_RES_DRQ, &i, 35250769Sdfr 0, ~0, 1, RF_ACTIVE); 35350769Sdfr if (res[i]) { 35450769Sdfr result->ic_drqmask[i] = (1 << drq); 35550769Sdfr break; 35650769Sdfr } 35750769Sdfr } 35850769Sdfr 35950769Sdfr /* 36050769Sdfr * If we didn't find a place for drq range i, then 36150769Sdfr * give up now. 36250769Sdfr */ 36350769Sdfr if (!res[i]) { 36450769Sdfr success = 0; 36550769Sdfr break; 36650769Sdfr } 36750769Sdfr } 36850769Sdfr 36950769Sdfr for (i = 0; i < ISA_NDRQ; i++) { 37050769Sdfr if (res[i]) 37150769Sdfr bus_release_resource(child, SYS_RES_DRQ, 37250769Sdfr i, res[i]); 37350769Sdfr } 37450769Sdfr 37550769Sdfr return success; 37650769Sdfr} 37750769Sdfr 37850769Sdfr/* 37950769Sdfr * Attempt to find a working set of resources for a device. Return 38050769Sdfr * non-zero if a working configuration is found. 38150769Sdfr */ 38250769Sdfrstatic int 38350769Sdfrisa_assign_resources(device_t child) 38450769Sdfr{ 38550769Sdfr struct isa_device *idev = DEVTOISA(child); 38650769Sdfr struct isa_config_entry *ice; 38750769Sdfr struct isa_config config; 38850769Sdfr 38950769Sdfr bzero(&config, sizeof config); 39050769Sdfr TAILQ_FOREACH(ice, &idev->id_configs, ice_link) { 39150769Sdfr if (!isa_find_memory(child, &ice->ice_config, &config)) 39250769Sdfr continue; 39350769Sdfr if (!isa_find_port(child, &ice->ice_config, &config)) 39450769Sdfr continue; 39550769Sdfr if (!isa_find_irq(child, &ice->ice_config, &config)) 39650769Sdfr continue; 39750769Sdfr if (!isa_find_drq(child, &ice->ice_config, &config)) 39850769Sdfr continue; 39950769Sdfr 40050769Sdfr /* 40150769Sdfr * A working configuration was found enable the device 40250769Sdfr * with this configuration. 40350769Sdfr */ 40450769Sdfr if (idev->id_config_cb) { 40550769Sdfr idev->id_config_cb(idev->id_config_arg, 40650769Sdfr &config, 1); 40750769Sdfr return 1; 40850769Sdfr } 40950769Sdfr } 41050769Sdfr 41150769Sdfr /* 41250769Sdfr * Disable the device. 41350769Sdfr */ 41452174Sdfr if (device_get_desc(child)) 41552174Sdfr device_printf(child, "<%s> can't assign resources\n", 41652174Sdfr device_get_desc(child)); 41752174Sdfr else 41852174Sdfr device_printf(child, "can't assign resources\n"); 41950769Sdfr bzero(&config, sizeof config); 42050769Sdfr if (idev->id_config_cb) 42150769Sdfr idev->id_config_cb(idev->id_config_arg, &config, 0); 42250769Sdfr device_disable(child); 42350769Sdfr 42450769Sdfr return 0; 42550769Sdfr} 42650769Sdfr 42750769Sdfr/* 42850769Sdfr * Called after other devices have initialised to probe for isa devices. 42950769Sdfr */ 43050769Sdfrvoid 43150769Sdfrisa_probe_children(device_t dev) 43250769Sdfr{ 43350769Sdfr device_t *children; 43450769Sdfr int nchildren, i; 43550769Sdfr 43653094Sdfr /* 43753094Sdfr * Create all the children by calling driver's identify methods. 43853094Sdfr */ 43953094Sdfr bus_generic_probe(dev); 44053094Sdfr 44150769Sdfr if (device_get_children(dev, &children, &nchildren)) 44250769Sdfr return; 44350769Sdfr 44450769Sdfr /* 44551905Sdfr * First disable all pnp devices so that they don't get 44651905Sdfr * matched by legacy probes. 44751905Sdfr */ 44853094Sdfr if (bootverbose) 44953094Sdfr printf("isa_probe_children: disabling PnP devices\n"); 45051905Sdfr for (i = 0; i < nchildren; i++) { 45151905Sdfr device_t child = children[i]; 45251905Sdfr struct isa_device *idev = DEVTOISA(child); 45351905Sdfr struct isa_config config; 45451905Sdfr 45551905Sdfr bzero(&config, sizeof config); 45651905Sdfr if (idev->id_config_cb) 45751905Sdfr idev->id_config_cb(idev->id_config_arg, &config, 0); 45851905Sdfr } 45951905Sdfr 46051905Sdfr /* 46151905Sdfr * Next probe all non-pnp devices so that they claim their 46250769Sdfr * resources first. 46350769Sdfr */ 46453094Sdfr if (bootverbose) 46553094Sdfr printf("isa_probe_children: probing non-PnP devices\n"); 46650769Sdfr for (i = 0; i < nchildren; i++) { 46750769Sdfr device_t child = children[i]; 46850769Sdfr struct isa_device *idev = DEVTOISA(child); 46950769Sdfr 47050769Sdfr if (TAILQ_FIRST(&idev->id_configs)) 47150769Sdfr continue; 47250769Sdfr 47350769Sdfr device_probe_and_attach(child); 47450769Sdfr } 47550769Sdfr 47650769Sdfr /* 47751905Sdfr * Finally assign resource to pnp devices and probe them. 47850769Sdfr */ 47953094Sdfr if (bootverbose) 48053094Sdfr printf("isa_probe_children: probing PnP devices\n"); 48150769Sdfr for (i = 0; i < nchildren; i++) { 48250769Sdfr device_t child = children[i]; 48350769Sdfr struct isa_device* idev = DEVTOISA(child); 48450769Sdfr 48550769Sdfr if (!TAILQ_FIRST(&idev->id_configs)) 48650769Sdfr continue; 48750769Sdfr 48850769Sdfr if (isa_assign_resources(child)) { 48952174Sdfr struct resource_list *rl = &idev->id_resources; 49050769Sdfr struct resource_list_entry *rle; 49150769Sdfr 49250769Sdfr device_probe_and_attach(child); 49350769Sdfr 49450769Sdfr /* 49550769Sdfr * Claim any unallocated resources to keep other 49650769Sdfr * devices from using them. 49750769Sdfr */ 49852174Sdfr SLIST_FOREACH(rle, rl, link) { 49950769Sdfr if (!rle->res) { 50050769Sdfr int rid = rle->rid; 50152174Sdfr resource_list_alloc(rl, dev, child, 50250769Sdfr rle->type, 50350769Sdfr &rid, 50450769Sdfr 0, ~0, 1, 50550769Sdfr RF_ACTIVE); 50650769Sdfr } 50750769Sdfr } 50850769Sdfr } 50950769Sdfr } 51050769Sdfr 51150769Sdfr free(children, M_TEMP); 51253094Sdfr 51353094Sdfr isa_running = 1; 51450769Sdfr} 51550769Sdfr 51650769Sdfr/* 51747398Sdfr * Add a new child with default ivars. 51847398Sdfr */ 51947398Sdfrstatic device_t 52047578Sdfrisa_add_child(device_t dev, int order, const char *name, int unit) 52147398Sdfr{ 52247398Sdfr struct isa_device *idev; 52347398Sdfr 52447398Sdfr idev = malloc(sizeof(struct isa_device), M_ISADEV, M_NOWAIT); 52547398Sdfr if (!idev) 52647398Sdfr return 0; 52747398Sdfr bzero(idev, sizeof *idev); 52847398Sdfr 52947398Sdfr resource_list_init(&idev->id_resources); 53050769Sdfr TAILQ_INIT(&idev->id_configs); 53147398Sdfr 53247578Sdfr return device_add_child_ordered(dev, order, name, unit, idev); 53347398Sdfr} 53447398Sdfr 53547398Sdfrstatic void 53647398Sdfrisa_print_resources(struct resource_list *rl, const char *name, int type, 53752174Sdfr int count, const char *format) 53847398Sdfr{ 53950769Sdfr struct resource_list_entry *rle; 54050769Sdfr int printed; 54150769Sdfr int i; 54247398Sdfr 54350769Sdfr printed = 0; 54452174Sdfr for (i = 0; i < count; i++) { 54550769Sdfr rle = resource_list_find(rl, type, i); 54650769Sdfr if (rle) { 54750769Sdfr if (printed == 0) 54850769Sdfr printf(" %s ", name); 54950769Sdfr else if (printed > 0) 55047398Sdfr printf(","); 55150769Sdfr printed++; 55250769Sdfr printf(format, rle->start); 55350769Sdfr if (rle->count > 1) { 55447398Sdfr printf("-"); 55550769Sdfr printf(format, rle->start + rle->count - 1); 55647398Sdfr } 55750769Sdfr } else if (i > 3) { 55850769Sdfr /* check the first few regardless */ 55950769Sdfr break; 56047398Sdfr } 56147398Sdfr } 56247398Sdfr} 56347398Sdfr 56449195Smdoddstatic int 56547398Sdfrisa_print_child(device_t bus, device_t dev) 56647398Sdfr{ 56747398Sdfr struct isa_device *idev = DEVTOISA(dev); 56847398Sdfr struct resource_list *rl = &idev->id_resources; 56949195Smdodd int retval = 0; 57047398Sdfr 57149195Smdodd retval += bus_print_child_header(bus, dev); 57249195Smdodd 57351052Sdfr if (SLIST_FIRST(rl) || device_get_flags(dev)) 57449195Smdodd retval += printf(" at"); 57547398Sdfr 57652174Sdfr isa_print_resources(rl, "port", SYS_RES_IOPORT, ISA_NPORT, "%#lx"); 57752174Sdfr isa_print_resources(rl, "iomem", SYS_RES_MEMORY, ISA_NMEM, "%#lx"); 57852174Sdfr isa_print_resources(rl, "irq", SYS_RES_IRQ, ISA_NIRQ, "%ld"); 57952174Sdfr isa_print_resources(rl, "drq", SYS_RES_DRQ, ISA_NDRQ, "%ld"); 58051052Sdfr if (device_get_flags(dev)) 58151052Sdfr retval += printf(" flags %#x", device_get_flags(dev)); 58247398Sdfr 58349195Smdodd retval += bus_print_child_footer(bus, dev); 58449195Smdodd 58549195Smdodd return (retval); 58647398Sdfr} 58747398Sdfr 58847398Sdfrstatic int 58947398Sdfrisa_read_ivar(device_t bus, device_t dev, int index, uintptr_t * result) 59047398Sdfr{ 59147398Sdfr struct isa_device* idev = DEVTOISA(dev); 59247398Sdfr struct resource_list *rl = &idev->id_resources; 59347398Sdfr struct resource_list_entry *rle; 59447398Sdfr 59547398Sdfr switch (index) { 59647398Sdfr case ISA_IVAR_PORT_0: 59747398Sdfr rle = resource_list_find(rl, SYS_RES_IOPORT, 0); 59847398Sdfr if (rle) 59947398Sdfr *result = rle->start; 60047398Sdfr else 60147398Sdfr *result = -1; 60247398Sdfr break; 60347398Sdfr 60447398Sdfr case ISA_IVAR_PORT_1: 60547398Sdfr rle = resource_list_find(rl, SYS_RES_IOPORT, 1); 60647398Sdfr if (rle) 60747398Sdfr *result = rle->start; 60847398Sdfr else 60947398Sdfr *result = -1; 61047398Sdfr break; 61147398Sdfr 61247398Sdfr case ISA_IVAR_PORTSIZE_0: 61347398Sdfr rle = resource_list_find(rl, SYS_RES_IOPORT, 0); 61447398Sdfr if (rle) 61547398Sdfr *result = rle->count; 61647398Sdfr else 61747398Sdfr *result = 0; 61847398Sdfr break; 61947398Sdfr 62047398Sdfr case ISA_IVAR_PORTSIZE_1: 62147398Sdfr rle = resource_list_find(rl, SYS_RES_IOPORT, 1); 62247398Sdfr if (rle) 62347398Sdfr *result = rle->count; 62447398Sdfr else 62547398Sdfr *result = 0; 62647398Sdfr break; 62747398Sdfr 62847398Sdfr case ISA_IVAR_MADDR_0: 62947398Sdfr rle = resource_list_find(rl, SYS_RES_MEMORY, 0); 63047398Sdfr if (rle) 63147398Sdfr *result = rle->start; 63247398Sdfr else 63347398Sdfr *result = -1; 63447398Sdfr break; 63547398Sdfr 63647398Sdfr case ISA_IVAR_MADDR_1: 63747398Sdfr rle = resource_list_find(rl, SYS_RES_MEMORY, 1); 63847398Sdfr if (rle) 63947398Sdfr *result = rle->start; 64047398Sdfr else 64147398Sdfr *result = -1; 64247398Sdfr break; 64347398Sdfr 64447398Sdfr case ISA_IVAR_MSIZE_0: 64547398Sdfr rle = resource_list_find(rl, SYS_RES_MEMORY, 0); 64647398Sdfr if (rle) 64747398Sdfr *result = rle->count; 64847398Sdfr else 64947398Sdfr *result = 0; 65047398Sdfr break; 65147398Sdfr 65247398Sdfr case ISA_IVAR_MSIZE_1: 65347398Sdfr rle = resource_list_find(rl, SYS_RES_MEMORY, 1); 65447398Sdfr if (rle) 65547398Sdfr *result = rle->count; 65647398Sdfr else 65747398Sdfr *result = 0; 65847398Sdfr break; 65947398Sdfr 66047398Sdfr case ISA_IVAR_IRQ_0: 66147398Sdfr rle = resource_list_find(rl, SYS_RES_IRQ, 0); 66247398Sdfr if (rle) 66347398Sdfr *result = rle->start; 66447398Sdfr else 66547398Sdfr *result = -1; 66647398Sdfr break; 66747398Sdfr 66847398Sdfr case ISA_IVAR_IRQ_1: 66947398Sdfr rle = resource_list_find(rl, SYS_RES_IRQ, 1); 67047398Sdfr if (rle) 67147398Sdfr *result = rle->start; 67247398Sdfr else 67347398Sdfr *result = -1; 67447398Sdfr break; 67547398Sdfr 67647398Sdfr case ISA_IVAR_DRQ_0: 67747398Sdfr rle = resource_list_find(rl, SYS_RES_DRQ, 0); 67847398Sdfr if (rle) 67947398Sdfr *result = rle->start; 68047398Sdfr else 68147398Sdfr *result = -1; 68247398Sdfr break; 68347398Sdfr 68447398Sdfr case ISA_IVAR_DRQ_1: 68547398Sdfr rle = resource_list_find(rl, SYS_RES_DRQ, 1); 68647398Sdfr if (rle) 68747398Sdfr *result = rle->start; 68847398Sdfr else 68947398Sdfr *result = -1; 69047398Sdfr break; 69147398Sdfr 69247613Sdfr case ISA_IVAR_VENDORID: 69347613Sdfr *result = idev->id_vendorid; 69447613Sdfr break; 69547613Sdfr 69647613Sdfr case ISA_IVAR_SERIAL: 69747613Sdfr *result = idev->id_serial; 69847613Sdfr break; 69947613Sdfr 70047613Sdfr case ISA_IVAR_LOGICALID: 70147613Sdfr *result = idev->id_logicalid; 70247613Sdfr break; 70347613Sdfr 70447613Sdfr case ISA_IVAR_COMPATID: 70547613Sdfr *result = idev->id_compatid; 70647613Sdfr break; 70747613Sdfr 70847613Sdfr default: 70947613Sdfr return ENOENT; 71047398Sdfr } 71147613Sdfr 71247613Sdfr return 0; 71347398Sdfr} 71447398Sdfr 71547398Sdfrstatic int 71647398Sdfrisa_write_ivar(device_t bus, device_t dev, 71747398Sdfr int index, uintptr_t value) 71847398Sdfr{ 71947398Sdfr struct isa_device* idev = DEVTOISA(dev); 72047398Sdfr 72147398Sdfr switch (index) { 72247398Sdfr case ISA_IVAR_PORT_0: 72347398Sdfr case ISA_IVAR_PORT_1: 72447398Sdfr case ISA_IVAR_PORTSIZE_0: 72547398Sdfr case ISA_IVAR_PORTSIZE_1: 72647398Sdfr case ISA_IVAR_MADDR_0: 72747398Sdfr case ISA_IVAR_MADDR_1: 72847398Sdfr case ISA_IVAR_MSIZE_0: 72947398Sdfr case ISA_IVAR_MSIZE_1: 73047398Sdfr case ISA_IVAR_IRQ_0: 73147398Sdfr case ISA_IVAR_IRQ_1: 73247398Sdfr case ISA_IVAR_DRQ_0: 73347398Sdfr case ISA_IVAR_DRQ_1: 73447398Sdfr return EINVAL; 73547398Sdfr 73647613Sdfr case ISA_IVAR_VENDORID: 73747613Sdfr idev->id_vendorid = value; 73847613Sdfr break; 73947613Sdfr 74047613Sdfr case ISA_IVAR_SERIAL: 74147613Sdfr idev->id_serial = value; 74247613Sdfr break; 74347613Sdfr 74447613Sdfr case ISA_IVAR_LOGICALID: 74547613Sdfr idev->id_logicalid = value; 74647613Sdfr break; 74747613Sdfr 74847613Sdfr case ISA_IVAR_COMPATID: 74947613Sdfr idev->id_compatid = value; 75047613Sdfr break; 75147613Sdfr 75247398Sdfr default: 75347398Sdfr return (ENOENT); 75447398Sdfr } 75547613Sdfr 75647398Sdfr return (0); 75747398Sdfr} 75847398Sdfr 75950769Sdfr/* 76050769Sdfr * Free any resources which the driver missed or which we were holding for 76150769Sdfr * it (see isa_probe_children). 76250769Sdfr */ 76350769Sdfrstatic void 76450769Sdfrisa_child_detached(device_t dev, device_t child) 76550769Sdfr{ 76650769Sdfr struct isa_device* idev = DEVTOISA(child); 76752174Sdfr struct resource_list *rl = &idev->id_resources; 76850769Sdfr struct resource_list_entry *rle; 76950769Sdfr 77050769Sdfr SLIST_FOREACH(rle, &idev->id_resources, link) { 77150769Sdfr if (rle->res) 77252174Sdfr resource_list_release(rl, dev, child, 77350769Sdfr rle->type, 77450769Sdfr rle->rid, 77550769Sdfr rle->res); 77650769Sdfr } 77750769Sdfr} 77850769Sdfr 77953094Sdfrstatic void 78053094Sdfrisa_driver_added(device_t dev, driver_t *driver) 78153094Sdfr{ 78253094Sdfr device_t *children; 78353094Sdfr int nchildren, i; 78453094Sdfr 78553094Sdfr /* 78653094Sdfr * Don't do anything if drivers are dynamically 78753094Sdfr * added during autoconfiguration (cf. ymf724). 78853094Sdfr * since that would end up calling identify 78953094Sdfr * twice. 79053094Sdfr */ 79153094Sdfr if (!isa_running) 79253094Sdfr return; 79353094Sdfr 79453094Sdfr DEVICE_IDENTIFY(driver, dev); 79553094Sdfr if (device_get_children(dev, &children, &nchildren)) 79653094Sdfr return; 79753094Sdfr 79853094Sdfr for (i = 0; i < nchildren; i++) { 79953094Sdfr device_t child = children[i]; 80053094Sdfr struct isa_device *idev = DEVTOISA(child); 80153094Sdfr struct resource_list *rl = &idev->id_resources; 80253094Sdfr struct resource_list_entry *rle; 80353094Sdfr 80453094Sdfr if (device_get_state(child) != DS_NOTPRESENT) 80553094Sdfr continue; 80653094Sdfr 80753094Sdfr if (TAILQ_FIRST(&idev->id_configs)) 80853094Sdfr if (!isa_assign_resources(child)) 80953094Sdfr continue; 81053094Sdfr 81153094Sdfr device_probe_and_attach(child); 81253094Sdfr 81353094Sdfr if (TAILQ_FIRST(&idev->id_configs)) { 81453094Sdfr /* 81553094Sdfr * Claim any unallocated resources to keep other 81653094Sdfr * devices from using them. 81753094Sdfr */ 81853094Sdfr SLIST_FOREACH(rle, rl, link) { 81953094Sdfr if (!rle->res) { 82053094Sdfr int rid = rle->rid; 82153094Sdfr resource_list_alloc(rl, dev, child, 82253094Sdfr rle->type, 82353094Sdfr &rid, 82453094Sdfr 0, ~0, 1, 82553094Sdfr RF_ACTIVE); 82653094Sdfr } 82753094Sdfr } 82853094Sdfr } 82953094Sdfr } 83053094Sdfr 83153094Sdfr free(children, M_TEMP); 83253094Sdfr} 83353094Sdfr 83447398Sdfrstatic int 83547398Sdfrisa_set_resource(device_t dev, device_t child, int type, int rid, 83647398Sdfr u_long start, u_long count) 83747398Sdfr{ 83847398Sdfr struct isa_device* idev = DEVTOISA(child); 83947398Sdfr struct resource_list *rl = &idev->id_resources; 84047398Sdfr 84147398Sdfr if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY 84247398Sdfr && type != SYS_RES_IRQ && type != SYS_RES_DRQ) 84347398Sdfr return EINVAL; 84450769Sdfr if (rid < 0) 84547398Sdfr return EINVAL; 84650769Sdfr if (type == SYS_RES_IOPORT && rid >= ISA_NPORT) 84750769Sdfr return EINVAL; 84850769Sdfr if (type == SYS_RES_MEMORY && rid >= ISA_NMEM) 84950769Sdfr return EINVAL; 85050769Sdfr if (type == SYS_RES_IRQ && rid >= ISA_NIRQ) 85150769Sdfr return EINVAL; 85250769Sdfr if (type == SYS_RES_DRQ && rid >= ISA_NDRQ) 85350769Sdfr return EINVAL; 85447398Sdfr 85547398Sdfr resource_list_add(rl, type, rid, start, start + count - 1, count); 85647398Sdfr 85747398Sdfr return 0; 85847398Sdfr} 85947398Sdfr 86047398Sdfrstatic int 86147398Sdfrisa_get_resource(device_t dev, device_t child, int type, int rid, 86247398Sdfr u_long *startp, u_long *countp) 86347398Sdfr{ 86447398Sdfr struct isa_device* idev = DEVTOISA(child); 86547398Sdfr struct resource_list *rl = &idev->id_resources; 86647398Sdfr struct resource_list_entry *rle; 86747398Sdfr 86847398Sdfr rle = resource_list_find(rl, type, rid); 86947398Sdfr if (!rle) 87047398Sdfr return ENOENT; 87147398Sdfr 87247398Sdfr *startp = rle->start; 87347398Sdfr *countp = rle->count; 87447398Sdfr 87547398Sdfr return 0; 87647398Sdfr} 87747398Sdfr 87847613Sdfrstatic void 87947613Sdfrisa_delete_resource(device_t dev, device_t child, int type, int rid) 88047613Sdfr{ 88147613Sdfr struct isa_device* idev = DEVTOISA(child); 88247613Sdfr struct resource_list *rl = &idev->id_resources; 88347613Sdfr resource_list_delete(rl, type, rid); 88447613Sdfr} 88547613Sdfr 88650769Sdfrstatic int 88750769Sdfrisa_add_config(device_t dev, device_t child, 88850769Sdfr int priority, struct isa_config *config) 88950769Sdfr{ 89050769Sdfr struct isa_device* idev = DEVTOISA(child); 89150769Sdfr struct isa_config_entry *newice, *ice; 89250769Sdfr 89350769Sdfr newice = malloc(sizeof *ice, M_DEVBUF, M_NOWAIT); 89450769Sdfr if (!newice) 89550769Sdfr return ENOMEM; 89650769Sdfr 89750769Sdfr newice->ice_priority = priority; 89850769Sdfr newice->ice_config = *config; 89950769Sdfr 90050769Sdfr TAILQ_FOREACH(ice, &idev->id_configs, ice_link) { 90150769Sdfr if (ice->ice_priority > priority) 90250769Sdfr break; 90350769Sdfr } 90450769Sdfr if (ice) 90550769Sdfr TAILQ_INSERT_BEFORE(ice, newice, ice_link); 90650769Sdfr else 90750769Sdfr TAILQ_INSERT_TAIL(&idev->id_configs, newice, ice_link); 90850769Sdfr 90950769Sdfr return 0; 91050769Sdfr} 91150769Sdfr 91250769Sdfrstatic void 91350769Sdfrisa_set_config_callback(device_t dev, device_t child, 91450769Sdfr isa_config_cb *fn, void *arg) 91550769Sdfr{ 91650769Sdfr struct isa_device* idev = DEVTOISA(child); 91750769Sdfr 91850769Sdfr idev->id_config_cb = fn; 91950769Sdfr idev->id_config_arg = arg; 92050769Sdfr} 92150769Sdfr 92250769Sdfrstatic int 92350769Sdfrisa_pnp_probe(device_t dev, device_t child, struct isa_pnp_id *ids) 92450769Sdfr{ 92550769Sdfr struct isa_device* idev = DEVTOISA(child); 92650769Sdfr 92750769Sdfr if (!idev->id_vendorid) 92850769Sdfr return ENOENT; 92950769Sdfr 93050769Sdfr while (ids->ip_id) { 93150769Sdfr /* 93250769Sdfr * Really ought to support >1 compat id per device. 93350769Sdfr */ 93450769Sdfr if (idev->id_logicalid == ids->ip_id 93550769Sdfr || idev->id_compatid == ids->ip_id) { 93650910Sdfr if (ids->ip_desc) 93750910Sdfr device_set_desc(child, ids->ip_desc); 93850769Sdfr return 0; 93950769Sdfr } 94050769Sdfr ids++; 94150769Sdfr } 94250769Sdfr 94350769Sdfr return ENXIO; 94450769Sdfr} 94550769Sdfr 94647398Sdfrstatic device_method_t isa_methods[] = { 94747398Sdfr /* Device interface */ 94847398Sdfr DEVMETHOD(device_probe, isa_probe), 94947398Sdfr DEVMETHOD(device_attach, isa_attach), 95047398Sdfr DEVMETHOD(device_detach, bus_generic_detach), 95147398Sdfr DEVMETHOD(device_shutdown, bus_generic_shutdown), 95247398Sdfr DEVMETHOD(device_suspend, bus_generic_suspend), 95347398Sdfr DEVMETHOD(device_resume, bus_generic_resume), 95447398Sdfr 95547398Sdfr /* Bus interface */ 95647398Sdfr DEVMETHOD(bus_add_child, isa_add_child), 95747398Sdfr DEVMETHOD(bus_print_child, isa_print_child), 95847398Sdfr DEVMETHOD(bus_read_ivar, isa_read_ivar), 95947398Sdfr DEVMETHOD(bus_write_ivar, isa_write_ivar), 96050769Sdfr DEVMETHOD(bus_child_detached, isa_child_detached), 96153094Sdfr DEVMETHOD(bus_driver_added, isa_driver_added), 96247398Sdfr DEVMETHOD(bus_alloc_resource, isa_alloc_resource), 96347398Sdfr DEVMETHOD(bus_release_resource, isa_release_resource), 96447398Sdfr DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 96547398Sdfr DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 96647398Sdfr DEVMETHOD(bus_setup_intr, isa_setup_intr), 96747398Sdfr DEVMETHOD(bus_teardown_intr, isa_teardown_intr), 96852174Sdfr DEVMETHOD(bus_set_resource, isa_set_resource), 96952174Sdfr DEVMETHOD(bus_get_resource, isa_get_resource), 97052174Sdfr DEVMETHOD(bus_delete_resource, isa_delete_resource), 97147398Sdfr 97247398Sdfr /* ISA interface */ 97350769Sdfr DEVMETHOD(isa_add_config, isa_add_config), 97450769Sdfr DEVMETHOD(isa_set_config_callback, isa_set_config_callback), 97550769Sdfr DEVMETHOD(isa_pnp_probe, isa_pnp_probe), 97647398Sdfr 97747398Sdfr { 0, 0 } 97847398Sdfr}; 97947398Sdfr 98047398Sdfrstatic driver_t isa_driver = { 98147398Sdfr "isa", 98247398Sdfr isa_methods, 98347398Sdfr 1, /* no softc */ 98447398Sdfr}; 98547398Sdfr 98647398Sdfr/* 98747398Sdfr * ISA can be attached to a PCI-ISA bridge or directly to the nexus. 98847398Sdfr */ 98947398SdfrDRIVER_MODULE(isa, isab, isa_driver, isa_devclass, 0, 0); 99047398Sdfr#ifdef __i386__ 99147398SdfrDRIVER_MODULE(isa, nexus, isa_driver, isa_devclass, 0, 0); 99247398Sdfr#endif 99350769Sdfr 99450769Sdfr/* 99550769Sdfr * A fallback driver for reporting un-matched pnp devices. 99650769Sdfr */ 99750769Sdfr 99850769Sdfrstatic int 99950769Sdfrunknown_probe(device_t dev) 100050769Sdfr{ 100150769Sdfr /* 100250769Sdfr * Only match pnp devices. 100350769Sdfr */ 100450769Sdfr if (isa_get_vendorid(dev) != 0) 100550769Sdfr return -100; 100650769Sdfr return ENXIO; 100750769Sdfr} 100850769Sdfr 100950769Sdfrstatic int 101050769Sdfrunknown_attach(device_t dev) 101150769Sdfr{ 101250769Sdfr return 0; 101350769Sdfr} 101450769Sdfr 101550769Sdfrstatic int 101650769Sdfrunknown_detach(device_t dev) 101750769Sdfr{ 101850769Sdfr return 0; 101950769Sdfr} 102050769Sdfr 102150769Sdfrstatic device_method_t unknown_methods[] = { 102250769Sdfr /* Device interface */ 102350769Sdfr DEVMETHOD(device_probe, unknown_probe), 102450769Sdfr DEVMETHOD(device_attach, unknown_attach), 102550769Sdfr DEVMETHOD(device_detach, unknown_detach), 102650769Sdfr 102750769Sdfr { 0, 0 } 102850769Sdfr}; 102950769Sdfr 103050769Sdfrstatic driver_t unknown_driver = { 103150769Sdfr "unknown", 103250769Sdfr unknown_methods, 103350769Sdfr 1, /* no softc */ 103450769Sdfr}; 103550769Sdfr 103650769Sdfrstatic devclass_t unknown_devclass; 103750769Sdfr 103850769SdfrDRIVER_MODULE(unknown, isa, unknown_driver, unknown_devclass, 0, 0); 1039