1a3be63b3SDoug Rabson /*- 2a3be63b3SDoug Rabson * Copyright (c) 1999 Doug Rabson 3a3be63b3SDoug Rabson * All rights reserved. 4a3be63b3SDoug Rabson * 5a3be63b3SDoug Rabson * Redistribution and use in source and binary forms, with or without 6a3be63b3SDoug Rabson * modification, are permitted provided that the following conditions 7a3be63b3SDoug Rabson * are met: 8a3be63b3SDoug Rabson * 1. Redistributions of source code must retain the above copyright 9a3be63b3SDoug Rabson * notice, this list of conditions and the following disclaimer. 10a3be63b3SDoug Rabson * 2. Redistributions in binary form must reproduce the above copyright 11a3be63b3SDoug Rabson * notice, this list of conditions and the following disclaimer in the 12a3be63b3SDoug Rabson * documentation and/or other materials provided with the distribution. 13a3be63b3SDoug Rabson * 14a3be63b3SDoug Rabson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15a3be63b3SDoug Rabson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16a3be63b3SDoug Rabson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17a3be63b3SDoug Rabson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18a3be63b3SDoug Rabson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19a3be63b3SDoug Rabson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20a3be63b3SDoug Rabson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21a3be63b3SDoug Rabson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22a3be63b3SDoug Rabson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23a3be63b3SDoug Rabson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24a3be63b3SDoug Rabson * SUCH DAMAGE. 25a3be63b3SDoug Rabson */ 26a3be63b3SDoug Rabson /* 27a3be63b3SDoug Rabson * Modifications for Intel architecture by Garrett A. Wollman. 28a3be63b3SDoug Rabson * Copyright 1998 Massachusetts Institute of Technology 29a3be63b3SDoug Rabson * 30a3be63b3SDoug Rabson * Permission to use, copy, modify, and distribute this software and 31a3be63b3SDoug Rabson * its documentation for any purpose and without fee is hereby 32a3be63b3SDoug Rabson * granted, provided that both the above copyright notice and this 33a3be63b3SDoug Rabson * permission notice appear in all copies, that both the above 34a3be63b3SDoug Rabson * copyright notice and this permission notice appear in all 35a3be63b3SDoug Rabson * supporting documentation, and that the name of M.I.T. not be used 36a3be63b3SDoug Rabson * in advertising or publicity pertaining to distribution of the 37a3be63b3SDoug Rabson * software without specific, written prior permission. M.I.T. makes 38a3be63b3SDoug Rabson * no representations about the suitability of this software for any 39a3be63b3SDoug Rabson * purpose. It is provided "as is" without express or implied 40a3be63b3SDoug Rabson * warranty. 41a3be63b3SDoug Rabson * 42a3be63b3SDoug Rabson * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 43a3be63b3SDoug Rabson * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 44a3be63b3SDoug Rabson * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 45a3be63b3SDoug Rabson * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 46a3be63b3SDoug Rabson * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 47a3be63b3SDoug Rabson * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 48a3be63b3SDoug Rabson * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 49a3be63b3SDoug Rabson * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 50a3be63b3SDoug Rabson * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 51a3be63b3SDoug Rabson * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 52a3be63b3SDoug Rabson * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 53a3be63b3SDoug Rabson * SUCH DAMAGE. 54a3be63b3SDoug Rabson */ 55a3be63b3SDoug Rabson 56a3be63b3SDoug Rabson /* 57a3be63b3SDoug Rabson * Parts of the ISA bus implementation common to all architectures. 58a3be63b3SDoug Rabson */ 59a3be63b3SDoug Rabson 608c9bbf48SDavid E. O'Brien #include <sys/cdefs.h> 618c9bbf48SDavid E. O'Brien __FBSDID("$FreeBSD$"); 628c9bbf48SDavid E. O'Brien 63a3be63b3SDoug Rabson #include <sys/param.h> 64a3be63b3SDoug Rabson #include <sys/systm.h> 65a3be63b3SDoug Rabson #include <sys/kernel.h> 66a3be63b3SDoug Rabson #include <sys/bus.h> 67a3be63b3SDoug Rabson #include <sys/malloc.h> 68a3be63b3SDoug Rabson #include <sys/module.h> 69a3be63b3SDoug Rabson #include <machine/bus.h> 70a3be63b3SDoug Rabson #include <sys/rman.h> 71a3be63b3SDoug Rabson 72a3be63b3SDoug Rabson #include <machine/resource.h> 73a3be63b3SDoug Rabson 74a3be63b3SDoug Rabson #include <isa/isavar.h> 75a3be63b3SDoug Rabson #include <isa/isa_common.h> 76a3be63b3SDoug Rabson #ifdef __alpha__ /* XXX workaround a stupid warning */ 77a3be63b3SDoug Rabson #include <alpha/isa/isavar.h> 78a3be63b3SDoug Rabson #endif 79a3be63b3SDoug Rabson 802b2b44c9SJohn Baldwin static int isa_print_child(device_t bus, device_t dev); 812b2b44c9SJohn Baldwin 82959b7375SPoul-Henning Kamp static MALLOC_DEFINE(M_ISADEV, "isadev", "ISA device"); 83a3be63b3SDoug Rabson 84a3be63b3SDoug Rabson static devclass_t isa_devclass; 85ec22663bSDoug Rabson static int isa_running; 86a3be63b3SDoug Rabson 87a3be63b3SDoug Rabson /* 88a3be63b3SDoug Rabson * At 'probe' time, we add all the devices which we know about to the 89a3be63b3SDoug Rabson * bus. The generic attach routine will probe and attach them if they 90a3be63b3SDoug Rabson * are alive. 91a3be63b3SDoug Rabson */ 92a3be63b3SDoug Rabson static int 93a3be63b3SDoug Rabson isa_probe(device_t dev) 94a3be63b3SDoug Rabson { 95a3be63b3SDoug Rabson device_set_desc(dev, "ISA bus"); 9601f1aed2SThomas Moestl isa_init(dev); /* Allow machdep code to initialise */ 97ec22663bSDoug Rabson return 0; 98a3be63b3SDoug Rabson } 99a3be63b3SDoug Rabson 100a3be63b3SDoug Rabson extern device_t isa_bus_device; 101a3be63b3SDoug Rabson 102a3be63b3SDoug Rabson static int 103a3be63b3SDoug Rabson isa_attach(device_t dev) 104a3be63b3SDoug Rabson { 105a3be63b3SDoug Rabson /* 1064249382dSDoug Rabson * Arrange for isa_probe_children(dev) to be called later. XXX 107a3be63b3SDoug Rabson */ 108a3be63b3SDoug Rabson isa_bus_device = dev; 109a3be63b3SDoug Rabson return 0; 110a3be63b3SDoug Rabson } 111a3be63b3SDoug Rabson 112a3be63b3SDoug Rabson /* 1134249382dSDoug Rabson * Find a working set of memory regions for a child using the ranges 1144249382dSDoug Rabson * in *config and return the regions in *result. Returns non-zero if 1154249382dSDoug Rabson * a set of ranges was found. 1164249382dSDoug Rabson */ 1174249382dSDoug Rabson static int 1184249382dSDoug Rabson isa_find_memory(device_t child, 1194249382dSDoug Rabson struct isa_config *config, 1204249382dSDoug Rabson struct isa_config *result) 1214249382dSDoug Rabson { 1224249382dSDoug Rabson int success, i; 1234249382dSDoug Rabson struct resource *res[ISA_NMEM]; 1244249382dSDoug Rabson 1254249382dSDoug Rabson /* 1264249382dSDoug Rabson * First clear out any existing resource definitions. 1274249382dSDoug Rabson */ 1284249382dSDoug Rabson for (i = 0; i < ISA_NMEM; i++) { 12925afb89bSDoug Rabson bus_delete_resource(child, SYS_RES_MEMORY, i); 1304249382dSDoug Rabson res[i] = NULL; 1314249382dSDoug Rabson } 1324249382dSDoug Rabson 1334249382dSDoug Rabson success = 1; 1344249382dSDoug Rabson result->ic_nmem = config->ic_nmem; 1354249382dSDoug Rabson for (i = 0; i < config->ic_nmem; i++) { 1364249382dSDoug Rabson u_int32_t start, end, size, align; 137c3959391SKazutaka YOKOTA 138c3959391SKazutaka YOKOTA size = config->ic_mem[i].ir_size; 139c3959391SKazutaka YOKOTA 140c3959391SKazutaka YOKOTA /* the PnP device may have a null resource as filler */ 141c3959391SKazutaka YOKOTA if (size == 0) { 142c3959391SKazutaka YOKOTA result->ic_mem[i].ir_start = 0; 143c3959391SKazutaka YOKOTA result->ic_mem[i].ir_end = 0; 144c3959391SKazutaka YOKOTA result->ic_mem[i].ir_size = 0; 145c3959391SKazutaka YOKOTA result->ic_mem[i].ir_align = 0; 146c3959391SKazutaka YOKOTA continue; 147c3959391SKazutaka YOKOTA } 148c3959391SKazutaka YOKOTA 1494249382dSDoug Rabson for (start = config->ic_mem[i].ir_start, 1504249382dSDoug Rabson end = config->ic_mem[i].ir_end, 1514249382dSDoug Rabson align = config->ic_mem[i].ir_align; 1524249382dSDoug Rabson start + size - 1 <= end; 1534249382dSDoug Rabson start += align) { 15425afb89bSDoug Rabson bus_set_resource(child, SYS_RES_MEMORY, i, 1554249382dSDoug Rabson start, size); 1564249382dSDoug Rabson res[i] = bus_alloc_resource(child, 1574249382dSDoug Rabson SYS_RES_MEMORY, &i, 158208cc52fSMike Smith 0, ~0, 1, 0 /* !RF_ACTIVE */); 1594249382dSDoug Rabson if (res[i]) { 1604249382dSDoug Rabson result->ic_mem[i].ir_start = start; 1614249382dSDoug Rabson result->ic_mem[i].ir_end = start + size - 1; 1624249382dSDoug Rabson result->ic_mem[i].ir_size = size; 1634249382dSDoug Rabson result->ic_mem[i].ir_align = align; 1644249382dSDoug Rabson break; 1654249382dSDoug Rabson } 1664249382dSDoug Rabson } 1674249382dSDoug Rabson 1684249382dSDoug Rabson /* 1694249382dSDoug Rabson * If we didn't find a place for memory range i, then 1704249382dSDoug Rabson * give up now. 1714249382dSDoug Rabson */ 1724249382dSDoug Rabson if (!res[i]) { 1734249382dSDoug Rabson success = 0; 1744249382dSDoug Rabson break; 1754249382dSDoug Rabson } 1764249382dSDoug Rabson } 1774249382dSDoug Rabson 1784249382dSDoug Rabson for (i = 0; i < ISA_NMEM; i++) { 1794249382dSDoug Rabson if (res[i]) 1804249382dSDoug Rabson bus_release_resource(child, SYS_RES_MEMORY, 1814249382dSDoug Rabson i, res[i]); 1824249382dSDoug Rabson } 1834249382dSDoug Rabson 1844249382dSDoug Rabson return success; 1854249382dSDoug Rabson } 1864249382dSDoug Rabson 1874249382dSDoug Rabson /* 1884249382dSDoug Rabson * Find a working set of port regions for a child using the ranges 1894249382dSDoug Rabson * in *config and return the regions in *result. Returns non-zero if 1904249382dSDoug Rabson * a set of ranges was found. 1914249382dSDoug Rabson */ 1924249382dSDoug Rabson static int 1934249382dSDoug Rabson isa_find_port(device_t child, 1944249382dSDoug Rabson struct isa_config *config, 1954249382dSDoug Rabson struct isa_config *result) 1964249382dSDoug Rabson { 1974249382dSDoug Rabson int success, i; 1984249382dSDoug Rabson struct resource *res[ISA_NPORT]; 1994249382dSDoug Rabson 2004249382dSDoug Rabson /* 2014249382dSDoug Rabson * First clear out any existing resource definitions. 2024249382dSDoug Rabson */ 2034249382dSDoug Rabson for (i = 0; i < ISA_NPORT; i++) { 20425afb89bSDoug Rabson bus_delete_resource(child, SYS_RES_IOPORT, i); 2054249382dSDoug Rabson res[i] = NULL; 2064249382dSDoug Rabson } 2074249382dSDoug Rabson 2084249382dSDoug Rabson success = 1; 2094249382dSDoug Rabson result->ic_nport = config->ic_nport; 2104249382dSDoug Rabson for (i = 0; i < config->ic_nport; i++) { 2114249382dSDoug Rabson u_int32_t start, end, size, align; 212c3959391SKazutaka YOKOTA 213c3959391SKazutaka YOKOTA size = config->ic_port[i].ir_size; 214c3959391SKazutaka YOKOTA 215c3959391SKazutaka YOKOTA /* the PnP device may have a null resource as filler */ 216c3959391SKazutaka YOKOTA if (size == 0) { 217c3959391SKazutaka YOKOTA result->ic_port[i].ir_start = 0; 218c3959391SKazutaka YOKOTA result->ic_port[i].ir_end = 0; 219c3959391SKazutaka YOKOTA result->ic_port[i].ir_size = 0; 220c3959391SKazutaka YOKOTA result->ic_port[i].ir_align = 0; 221c3959391SKazutaka YOKOTA continue; 222c3959391SKazutaka YOKOTA } 223c3959391SKazutaka YOKOTA 2244249382dSDoug Rabson for (start = config->ic_port[i].ir_start, 2254249382dSDoug Rabson end = config->ic_port[i].ir_end, 2264249382dSDoug Rabson align = config->ic_port[i].ir_align; 2274249382dSDoug Rabson start + size - 1 <= end; 2284249382dSDoug Rabson start += align) { 22925afb89bSDoug Rabson bus_set_resource(child, SYS_RES_IOPORT, i, 2304249382dSDoug Rabson start, size); 2314249382dSDoug Rabson res[i] = bus_alloc_resource(child, 2324249382dSDoug Rabson SYS_RES_IOPORT, &i, 233208cc52fSMike Smith 0, ~0, 1, 0 /* !RF_ACTIVE */); 2344249382dSDoug Rabson if (res[i]) { 2354249382dSDoug Rabson result->ic_port[i].ir_start = start; 2364249382dSDoug Rabson result->ic_port[i].ir_end = start + size - 1; 2374249382dSDoug Rabson result->ic_port[i].ir_size = size; 2384249382dSDoug Rabson result->ic_port[i].ir_align = align; 2394249382dSDoug Rabson break; 2404249382dSDoug Rabson } 2414249382dSDoug Rabson } 2424249382dSDoug Rabson 2434249382dSDoug Rabson /* 2444249382dSDoug Rabson * If we didn't find a place for port range i, then 2454249382dSDoug Rabson * give up now. 2464249382dSDoug Rabson */ 2474249382dSDoug Rabson if (!res[i]) { 2484249382dSDoug Rabson success = 0; 2494249382dSDoug Rabson break; 2504249382dSDoug Rabson } 2514249382dSDoug Rabson } 2524249382dSDoug Rabson 2534249382dSDoug Rabson for (i = 0; i < ISA_NPORT; i++) { 2544249382dSDoug Rabson if (res[i]) 2554249382dSDoug Rabson bus_release_resource(child, SYS_RES_IOPORT, 2564249382dSDoug Rabson i, res[i]); 2574249382dSDoug Rabson } 2584249382dSDoug Rabson 2594249382dSDoug Rabson return success; 2604249382dSDoug Rabson } 2614249382dSDoug Rabson 2624249382dSDoug Rabson /* 2634249382dSDoug Rabson * Return the index of the first bit in the mask (or -1 if mask is empty. 2644249382dSDoug Rabson */ 2654249382dSDoug Rabson static int 2664249382dSDoug Rabson find_first_bit(u_int32_t mask) 2674249382dSDoug Rabson { 2684249382dSDoug Rabson return ffs(mask) - 1; 2694249382dSDoug Rabson } 2704249382dSDoug Rabson 2714249382dSDoug Rabson /* 2724249382dSDoug Rabson * Return the index of the next bit in the mask, or -1 if there are no more. 2734249382dSDoug Rabson */ 2744249382dSDoug Rabson static int 2754249382dSDoug Rabson find_next_bit(u_int32_t mask, int bit) 2764249382dSDoug Rabson { 2774249382dSDoug Rabson bit++; 2784249382dSDoug Rabson while (bit < 32 && !(mask & (1 << bit))) 2794249382dSDoug Rabson bit++; 2804249382dSDoug Rabson if (bit != 32) 2814249382dSDoug Rabson return bit; 2824249382dSDoug Rabson return -1; 2834249382dSDoug Rabson } 2844249382dSDoug Rabson 2854249382dSDoug Rabson /* 2864249382dSDoug Rabson * Find a working set of irqs for a child using the masks in *config 2874249382dSDoug Rabson * and return the regions in *result. Returns non-zero if a set of 2884249382dSDoug Rabson * irqs was found. 2894249382dSDoug Rabson */ 2904249382dSDoug Rabson static int 2914249382dSDoug Rabson isa_find_irq(device_t child, 2924249382dSDoug Rabson struct isa_config *config, 2934249382dSDoug Rabson struct isa_config *result) 2944249382dSDoug Rabson { 2954249382dSDoug Rabson int success, i; 2964249382dSDoug Rabson struct resource *res[ISA_NIRQ]; 2974249382dSDoug Rabson 2984249382dSDoug Rabson /* 2994249382dSDoug Rabson * First clear out any existing resource definitions. 3004249382dSDoug Rabson */ 3014249382dSDoug Rabson for (i = 0; i < ISA_NIRQ; i++) { 30225afb89bSDoug Rabson bus_delete_resource(child, SYS_RES_IRQ, i); 3034249382dSDoug Rabson res[i] = NULL; 3044249382dSDoug Rabson } 3054249382dSDoug Rabson 3064249382dSDoug Rabson success = 1; 3074249382dSDoug Rabson result->ic_nirq = config->ic_nirq; 3084249382dSDoug Rabson for (i = 0; i < config->ic_nirq; i++) { 3094249382dSDoug Rabson u_int32_t mask = config->ic_irqmask[i]; 3104249382dSDoug Rabson int irq; 311c3959391SKazutaka YOKOTA 312c3959391SKazutaka YOKOTA /* the PnP device may have a null resource as filler */ 313c3959391SKazutaka YOKOTA if (mask == 0) { 314c3959391SKazutaka YOKOTA result->ic_irqmask[i] = 0; 315c3959391SKazutaka YOKOTA continue; 316c3959391SKazutaka YOKOTA } 317c3959391SKazutaka YOKOTA 3184249382dSDoug Rabson for (irq = find_first_bit(mask); 3194249382dSDoug Rabson irq != -1; 3204249382dSDoug Rabson irq = find_next_bit(mask, irq)) { 32125afb89bSDoug Rabson bus_set_resource(child, SYS_RES_IRQ, i, 3224249382dSDoug Rabson irq, 1); 3234249382dSDoug Rabson res[i] = bus_alloc_resource(child, 3244249382dSDoug Rabson SYS_RES_IRQ, &i, 325208cc52fSMike Smith 0, ~0, 1, 0 /* !RF_ACTIVE */ ); 3264249382dSDoug Rabson if (res[i]) { 3274249382dSDoug Rabson result->ic_irqmask[i] = (1 << irq); 3284249382dSDoug Rabson break; 3294249382dSDoug Rabson } 3304249382dSDoug Rabson } 3314249382dSDoug Rabson 3324249382dSDoug Rabson /* 3334249382dSDoug Rabson * If we didn't find a place for irq range i, then 3344249382dSDoug Rabson * give up now. 3354249382dSDoug Rabson */ 3364249382dSDoug Rabson if (!res[i]) { 3374249382dSDoug Rabson success = 0; 3384249382dSDoug Rabson break; 3394249382dSDoug Rabson } 3404249382dSDoug Rabson } 3414249382dSDoug Rabson 3424249382dSDoug Rabson for (i = 0; i < ISA_NIRQ; i++) { 3434249382dSDoug Rabson if (res[i]) 3444249382dSDoug Rabson bus_release_resource(child, SYS_RES_IRQ, 3454249382dSDoug Rabson i, res[i]); 3464249382dSDoug Rabson } 3474249382dSDoug Rabson 3484249382dSDoug Rabson return success; 3494249382dSDoug Rabson } 3504249382dSDoug Rabson 3514249382dSDoug Rabson /* 3524249382dSDoug Rabson * Find a working set of drqs for a child using the masks in *config 3534249382dSDoug Rabson * and return the regions in *result. Returns non-zero if a set of 3544249382dSDoug Rabson * drqs was found. 3554249382dSDoug Rabson */ 3564249382dSDoug Rabson static int 3574249382dSDoug Rabson isa_find_drq(device_t child, 3584249382dSDoug Rabson struct isa_config *config, 3594249382dSDoug Rabson struct isa_config *result) 3604249382dSDoug Rabson { 3614249382dSDoug Rabson int success, i; 3624249382dSDoug Rabson struct resource *res[ISA_NDRQ]; 3634249382dSDoug Rabson 3644249382dSDoug Rabson /* 3654249382dSDoug Rabson * First clear out any existing resource definitions. 3664249382dSDoug Rabson */ 3674249382dSDoug Rabson for (i = 0; i < ISA_NDRQ; i++) { 36825afb89bSDoug Rabson bus_delete_resource(child, SYS_RES_DRQ, i); 3694249382dSDoug Rabson res[i] = NULL; 3704249382dSDoug Rabson } 3714249382dSDoug Rabson 3724249382dSDoug Rabson success = 1; 3734249382dSDoug Rabson result->ic_ndrq = config->ic_ndrq; 3744249382dSDoug Rabson for (i = 0; i < config->ic_ndrq; i++) { 3754249382dSDoug Rabson u_int32_t mask = config->ic_drqmask[i]; 3764249382dSDoug Rabson int drq; 377c3959391SKazutaka YOKOTA 378c3959391SKazutaka YOKOTA /* the PnP device may have a null resource as filler */ 379c3959391SKazutaka YOKOTA if (mask == 0) { 380c3959391SKazutaka YOKOTA result->ic_drqmask[i] = 0; 381c3959391SKazutaka YOKOTA continue; 382c3959391SKazutaka YOKOTA } 383c3959391SKazutaka YOKOTA 3844249382dSDoug Rabson for (drq = find_first_bit(mask); 3854249382dSDoug Rabson drq != -1; 3864249382dSDoug Rabson drq = find_next_bit(mask, drq)) { 38725afb89bSDoug Rabson bus_set_resource(child, SYS_RES_DRQ, i, 3884249382dSDoug Rabson drq, 1); 3894249382dSDoug Rabson res[i] = bus_alloc_resource(child, 3904249382dSDoug Rabson SYS_RES_DRQ, &i, 391208cc52fSMike Smith 0, ~0, 1, 0 /* !RF_ACTIVE */); 3924249382dSDoug Rabson if (res[i]) { 3934249382dSDoug Rabson result->ic_drqmask[i] = (1 << drq); 3944249382dSDoug Rabson break; 3954249382dSDoug Rabson } 3964249382dSDoug Rabson } 3974249382dSDoug Rabson 3984249382dSDoug Rabson /* 3994249382dSDoug Rabson * If we didn't find a place for drq range i, then 4004249382dSDoug Rabson * give up now. 4014249382dSDoug Rabson */ 4024249382dSDoug Rabson if (!res[i]) { 4034249382dSDoug Rabson success = 0; 4044249382dSDoug Rabson break; 4054249382dSDoug Rabson } 4064249382dSDoug Rabson } 4074249382dSDoug Rabson 4084249382dSDoug Rabson for (i = 0; i < ISA_NDRQ; i++) { 4094249382dSDoug Rabson if (res[i]) 4104249382dSDoug Rabson bus_release_resource(child, SYS_RES_DRQ, 4114249382dSDoug Rabson i, res[i]); 4124249382dSDoug Rabson } 4134249382dSDoug Rabson 4144249382dSDoug Rabson return success; 4154249382dSDoug Rabson } 4164249382dSDoug Rabson 4174249382dSDoug Rabson /* 4184249382dSDoug Rabson * Attempt to find a working set of resources for a device. Return 4194249382dSDoug Rabson * non-zero if a working configuration is found. 4204249382dSDoug Rabson */ 4214249382dSDoug Rabson static int 4224249382dSDoug Rabson isa_assign_resources(device_t child) 4234249382dSDoug Rabson { 4244249382dSDoug Rabson struct isa_device *idev = DEVTOISA(child); 4254249382dSDoug Rabson struct isa_config_entry *ice; 4263b82ede0SJulian Elischer struct isa_config *cfg; 4270077e822SAlfred Perlstein const char *reason; 4284249382dSDoug Rabson 4290077e822SAlfred Perlstein reason = "Empty ISA id_configs"; 4303b82ede0SJulian Elischer cfg = malloc(sizeof(struct isa_config), M_TEMP, M_NOWAIT|M_ZERO); 4313b82ede0SJulian Elischer if (cfg == NULL) 4323b82ede0SJulian Elischer return(0); 4334249382dSDoug Rabson TAILQ_FOREACH(ice, &idev->id_configs, ice_link) { 434b2e4739fSAlfred Perlstein reason = "memory"; 4353b82ede0SJulian Elischer if (!isa_find_memory(child, &ice->ice_config, cfg)) 4364249382dSDoug Rabson continue; 437b2e4739fSAlfred Perlstein reason = "port"; 4383b82ede0SJulian Elischer if (!isa_find_port(child, &ice->ice_config, cfg)) 4394249382dSDoug Rabson continue; 440b2e4739fSAlfred Perlstein reason = "irq"; 4413b82ede0SJulian Elischer if (!isa_find_irq(child, &ice->ice_config, cfg)) 4424249382dSDoug Rabson continue; 443b2e4739fSAlfred Perlstein reason = "drq"; 4443b82ede0SJulian Elischer if (!isa_find_drq(child, &ice->ice_config, cfg)) 4454249382dSDoug Rabson continue; 4464249382dSDoug Rabson 4474249382dSDoug Rabson /* 4484249382dSDoug Rabson * A working configuration was found enable the device 4494249382dSDoug Rabson * with this configuration. 4504249382dSDoug Rabson */ 451b2e4739fSAlfred Perlstein reason = "no callback"; 4524249382dSDoug Rabson if (idev->id_config_cb) { 4534249382dSDoug Rabson idev->id_config_cb(idev->id_config_arg, 4543b82ede0SJulian Elischer cfg, 1); 4553b82ede0SJulian Elischer free(cfg, M_TEMP); 4564249382dSDoug Rabson return 1; 4574249382dSDoug Rabson } 4584249382dSDoug Rabson } 4594249382dSDoug Rabson 4604249382dSDoug Rabson /* 4614249382dSDoug Rabson * Disable the device. 4624249382dSDoug Rabson */ 4632b2b44c9SJohn Baldwin bus_print_child_header(device_get_parent(child), child); 464b2e4739fSAlfred Perlstein printf(" can't assign resources (%s)\n", reason); 4652b2b44c9SJohn Baldwin if (bootverbose) 4662b2b44c9SJohn Baldwin isa_print_child(device_get_parent(child), child); 4673b82ede0SJulian Elischer bzero(cfg, sizeof (*cfg)); 4684249382dSDoug Rabson if (idev->id_config_cb) 4693b82ede0SJulian Elischer idev->id_config_cb(idev->id_config_arg, cfg, 0); 4704249382dSDoug Rabson device_disable(child); 4714249382dSDoug Rabson 4723b82ede0SJulian Elischer free(cfg, M_TEMP); 4734249382dSDoug Rabson return 0; 4744249382dSDoug Rabson } 4754249382dSDoug Rabson 4764249382dSDoug Rabson /* 477c3959391SKazutaka YOKOTA * Return non-zero if the device has a single configuration, that is, 478c3959391SKazutaka YOKOTA * a fixed set of resoruces. 479c3959391SKazutaka YOKOTA */ 480c3959391SKazutaka YOKOTA static int 481c3959391SKazutaka YOKOTA isa_has_single_config(device_t dev) 482c3959391SKazutaka YOKOTA { 483c3959391SKazutaka YOKOTA struct isa_device *idev = DEVTOISA(dev); 484c3959391SKazutaka YOKOTA struct isa_config_entry *ice; 485c3959391SKazutaka YOKOTA u_int32_t mask; 486c3959391SKazutaka YOKOTA int i; 487c3959391SKazutaka YOKOTA 488c3959391SKazutaka YOKOTA ice = TAILQ_FIRST(&idev->id_configs); 489c3959391SKazutaka YOKOTA if (TAILQ_NEXT(ice, ice_link)) 490c3959391SKazutaka YOKOTA return 0; 491c3959391SKazutaka YOKOTA 492c3959391SKazutaka YOKOTA for (i = 0; i < ice->ice_config.ic_nmem; ++i) { 493c3959391SKazutaka YOKOTA if (ice->ice_config.ic_mem[i].ir_size == 0) 494c3959391SKazutaka YOKOTA continue; 495c3959391SKazutaka YOKOTA if (ice->ice_config.ic_mem[i].ir_end != 496c3959391SKazutaka YOKOTA ice->ice_config.ic_mem[i].ir_start + 497c3959391SKazutaka YOKOTA ice->ice_config.ic_mem[i].ir_size - 1) 498c3959391SKazutaka YOKOTA return 0; 499c3959391SKazutaka YOKOTA } 500c3959391SKazutaka YOKOTA for (i = 0; i < ice->ice_config.ic_nport; ++i) { 501c3959391SKazutaka YOKOTA if (ice->ice_config.ic_port[i].ir_size == 0) 502c3959391SKazutaka YOKOTA continue; 503c3959391SKazutaka YOKOTA if (ice->ice_config.ic_port[i].ir_end != 504c3959391SKazutaka YOKOTA ice->ice_config.ic_port[i].ir_start + 505c3959391SKazutaka YOKOTA ice->ice_config.ic_port[i].ir_size - 1) 506c3959391SKazutaka YOKOTA return 0; 507c3959391SKazutaka YOKOTA } 508c3959391SKazutaka YOKOTA for (i = 0; i < ice->ice_config.ic_nirq; ++i) { 509c3959391SKazutaka YOKOTA mask = ice->ice_config.ic_irqmask[i]; 510c3959391SKazutaka YOKOTA if (mask == 0) 511c3959391SKazutaka YOKOTA continue; 512c3959391SKazutaka YOKOTA if (find_next_bit(mask, find_first_bit(mask)) != -1) 513c3959391SKazutaka YOKOTA return 0; 514c3959391SKazutaka YOKOTA } 515c3959391SKazutaka YOKOTA for (i = 0; i < ice->ice_config.ic_ndrq; ++i) { 516c3959391SKazutaka YOKOTA mask = ice->ice_config.ic_drqmask[i]; 517c3959391SKazutaka YOKOTA if (mask == 0) 518c3959391SKazutaka YOKOTA continue; 519c3959391SKazutaka YOKOTA if (find_next_bit(mask, find_first_bit(mask)) != -1) 520c3959391SKazutaka YOKOTA return 0; 521c3959391SKazutaka YOKOTA } 522c3959391SKazutaka YOKOTA return 1; 523c3959391SKazutaka YOKOTA } 524c3959391SKazutaka YOKOTA 525c3959391SKazutaka YOKOTA /* 5264249382dSDoug Rabson * Called after other devices have initialised to probe for isa devices. 5274249382dSDoug Rabson */ 5284249382dSDoug Rabson void 5294249382dSDoug Rabson isa_probe_children(device_t dev) 5304249382dSDoug Rabson { 5314249382dSDoug Rabson device_t *children; 5323b82ede0SJulian Elischer struct isa_config *cfg; 5334249382dSDoug Rabson int nchildren, i; 5344249382dSDoug Rabson 535ec22663bSDoug Rabson /* 536ec22663bSDoug Rabson * Create all the children by calling driver's identify methods. 537ec22663bSDoug Rabson */ 538ec22663bSDoug Rabson bus_generic_probe(dev); 539ec22663bSDoug Rabson 5404249382dSDoug Rabson if (device_get_children(dev, &children, &nchildren)) 5414249382dSDoug Rabson return; 5424249382dSDoug Rabson 5434249382dSDoug Rabson /* 544a2e6dbb3SDoug Rabson * First disable all pnp devices so that they don't get 545a2e6dbb3SDoug Rabson * matched by legacy probes. 546a2e6dbb3SDoug Rabson */ 547ec22663bSDoug Rabson if (bootverbose) 548ec22663bSDoug Rabson printf("isa_probe_children: disabling PnP devices\n"); 5493b82ede0SJulian Elischer 5503b82ede0SJulian Elischer cfg = malloc(sizeof(*cfg), M_TEMP, M_NOWAIT|M_ZERO); 5513b82ede0SJulian Elischer if (cfg == NULL) { 5523b82ede0SJulian Elischer free(children, M_TEMP); 5533b82ede0SJulian Elischer return; 5543b82ede0SJulian Elischer } 5553b82ede0SJulian Elischer 556a2e6dbb3SDoug Rabson for (i = 0; i < nchildren; i++) { 557a2e6dbb3SDoug Rabson device_t child = children[i]; 558a2e6dbb3SDoug Rabson struct isa_device *idev = DEVTOISA(child); 559a2e6dbb3SDoug Rabson 5603b82ede0SJulian Elischer bzero(cfg, sizeof(*cfg)); 561a2e6dbb3SDoug Rabson if (idev->id_config_cb) 5623b82ede0SJulian Elischer idev->id_config_cb(idev->id_config_arg, cfg, 0); 563a2e6dbb3SDoug Rabson } 564a2e6dbb3SDoug Rabson 5653b82ede0SJulian Elischer free(cfg, M_TEMP); 5663b82ede0SJulian Elischer 567a2e6dbb3SDoug Rabson /* 568a2e6dbb3SDoug Rabson * Next probe all non-pnp devices so that they claim their 5694249382dSDoug Rabson * resources first. 5704249382dSDoug Rabson */ 571ec22663bSDoug Rabson if (bootverbose) 572ec22663bSDoug Rabson printf("isa_probe_children: probing non-PnP devices\n"); 5734249382dSDoug Rabson for (i = 0; i < nchildren; i++) { 5744249382dSDoug Rabson device_t child = children[i]; 5754249382dSDoug Rabson struct isa_device *idev = DEVTOISA(child); 5764249382dSDoug Rabson 5774249382dSDoug Rabson if (TAILQ_FIRST(&idev->id_configs)) 5784249382dSDoug Rabson continue; 5794249382dSDoug Rabson 5804249382dSDoug Rabson device_probe_and_attach(child); 5814249382dSDoug Rabson } 5824249382dSDoug Rabson 5834249382dSDoug Rabson /* 584a2e6dbb3SDoug Rabson * Finally assign resource to pnp devices and probe them. 5854249382dSDoug Rabson */ 586ec22663bSDoug Rabson if (bootverbose) 587ec22663bSDoug Rabson printf("isa_probe_children: probing PnP devices\n"); 5884249382dSDoug Rabson for (i = 0; i < nchildren; i++) { 5894249382dSDoug Rabson device_t child = children[i]; 5904249382dSDoug Rabson struct isa_device* idev = DEVTOISA(child); 5914249382dSDoug Rabson 5924249382dSDoug Rabson if (!TAILQ_FIRST(&idev->id_configs)) 5934249382dSDoug Rabson continue; 5944249382dSDoug Rabson 5954249382dSDoug Rabson if (isa_assign_resources(child)) { 59625afb89bSDoug Rabson struct resource_list *rl = &idev->id_resources; 5974249382dSDoug Rabson struct resource_list_entry *rle; 5984249382dSDoug Rabson 5994249382dSDoug Rabson device_probe_and_attach(child); 6004249382dSDoug Rabson 6014249382dSDoug Rabson /* 6024249382dSDoug Rabson * Claim any unallocated resources to keep other 6034249382dSDoug Rabson * devices from using them. 6044249382dSDoug Rabson */ 60525afb89bSDoug Rabson SLIST_FOREACH(rle, rl, link) { 6064249382dSDoug Rabson if (!rle->res) { 6074249382dSDoug Rabson int rid = rle->rid; 60825afb89bSDoug Rabson resource_list_alloc(rl, dev, child, 6094249382dSDoug Rabson rle->type, 6104249382dSDoug Rabson &rid, 611328d36e9SDoug Rabson 0, ~0, 1, 0); 6124249382dSDoug Rabson } 6134249382dSDoug Rabson } 6144249382dSDoug Rabson } 6154249382dSDoug Rabson } 6164249382dSDoug Rabson 6174249382dSDoug Rabson free(children, M_TEMP); 618ec22663bSDoug Rabson 619ec22663bSDoug Rabson isa_running = 1; 6204249382dSDoug Rabson } 6214249382dSDoug Rabson 6224249382dSDoug Rabson /* 623a3be63b3SDoug Rabson * Add a new child with default ivars. 624a3be63b3SDoug Rabson */ 625a3be63b3SDoug Rabson static device_t 626bea6af4dSDoug Rabson isa_add_child(device_t dev, int order, const char *name, int unit) 627a3be63b3SDoug Rabson { 628fe0d4089SMatthew N. Dodd device_t child; 629a3be63b3SDoug Rabson struct isa_device *idev; 630a3be63b3SDoug Rabson 631f46708e0SPoul-Henning Kamp child = device_add_child_ordered(dev, order, name, unit); 632f46708e0SPoul-Henning Kamp if (child == NULL) 633f46708e0SPoul-Henning Kamp return (child); 634f46708e0SPoul-Henning Kamp 6357cc0979fSDavid Malone idev = malloc(sizeof(struct isa_device), M_ISADEV, M_NOWAIT | M_ZERO); 636a3be63b3SDoug Rabson if (!idev) 637a3be63b3SDoug Rabson return 0; 638a3be63b3SDoug Rabson 639a3be63b3SDoug Rabson resource_list_init(&idev->id_resources); 6404249382dSDoug Rabson TAILQ_INIT(&idev->id_configs); 641a3be63b3SDoug Rabson 642fe0d4089SMatthew N. Dodd device_set_ivars(child, idev); 643fe0d4089SMatthew N. Dodd 644f46708e0SPoul-Henning Kamp return (child); 645a3be63b3SDoug Rabson } 646a3be63b3SDoug Rabson 647328d36e9SDoug Rabson static int 648328d36e9SDoug Rabson isa_print_all_resources(device_t dev) 649a3be63b3SDoug Rabson { 650a3be63b3SDoug Rabson struct isa_device *idev = DEVTOISA(dev); 651a3be63b3SDoug Rabson struct resource_list *rl = &idev->id_resources; 65215317dd8SMatthew N. Dodd int retval = 0; 65315317dd8SMatthew N. Dodd 654062acdb7SDoug Rabson if (SLIST_FIRST(rl) || device_get_flags(dev)) 65515317dd8SMatthew N. Dodd retval += printf(" at"); 656a3be63b3SDoug Rabson 65701f1aed2SThomas Moestl retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx"); 65801f1aed2SThomas Moestl retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx"); 65901f1aed2SThomas Moestl retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 66001f1aed2SThomas Moestl retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld"); 661062acdb7SDoug Rabson if (device_get_flags(dev)) 662062acdb7SDoug Rabson retval += printf(" flags %#x", device_get_flags(dev)); 663a3be63b3SDoug Rabson 664328d36e9SDoug Rabson return retval; 665328d36e9SDoug Rabson } 666328d36e9SDoug Rabson 667328d36e9SDoug Rabson static int 668328d36e9SDoug Rabson isa_print_child(device_t bus, device_t dev) 669328d36e9SDoug Rabson { 670328d36e9SDoug Rabson int retval = 0; 671328d36e9SDoug Rabson 672328d36e9SDoug Rabson retval += bus_print_child_header(bus, dev); 673328d36e9SDoug Rabson retval += isa_print_all_resources(dev); 67415317dd8SMatthew N. Dodd retval += bus_print_child_footer(bus, dev); 67515317dd8SMatthew N. Dodd 67615317dd8SMatthew N. Dodd return (retval); 677a3be63b3SDoug Rabson } 678a3be63b3SDoug Rabson 679328d36e9SDoug Rabson static void 680328d36e9SDoug Rabson isa_probe_nomatch(device_t dev, device_t child) 681328d36e9SDoug Rabson { 6822b2b44c9SJohn Baldwin if (bootverbose) { 6832b2b44c9SJohn Baldwin bus_print_child_header(dev, child); 6842b2b44c9SJohn Baldwin printf(" failed to probe"); 685328d36e9SDoug Rabson isa_print_all_resources(child); 6862b2b44c9SJohn Baldwin bus_print_child_footer(dev, child); 6872b2b44c9SJohn Baldwin } 688328d36e9SDoug Rabson 689328d36e9SDoug Rabson return; 690328d36e9SDoug Rabson } 691328d36e9SDoug Rabson 692a3be63b3SDoug Rabson static int 693a3be63b3SDoug Rabson isa_read_ivar(device_t bus, device_t dev, int index, uintptr_t * result) 694a3be63b3SDoug Rabson { 695a3be63b3SDoug Rabson struct isa_device* idev = DEVTOISA(dev); 696a3be63b3SDoug Rabson struct resource_list *rl = &idev->id_resources; 697a3be63b3SDoug Rabson struct resource_list_entry *rle; 698a3be63b3SDoug Rabson 699a3be63b3SDoug Rabson switch (index) { 700a3be63b3SDoug Rabson case ISA_IVAR_PORT_0: 701a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_IOPORT, 0); 702a3be63b3SDoug Rabson if (rle) 703a3be63b3SDoug Rabson *result = rle->start; 704a3be63b3SDoug Rabson else 705a3be63b3SDoug Rabson *result = -1; 706a3be63b3SDoug Rabson break; 707a3be63b3SDoug Rabson 708a3be63b3SDoug Rabson case ISA_IVAR_PORT_1: 709a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_IOPORT, 1); 710a3be63b3SDoug Rabson if (rle) 711a3be63b3SDoug Rabson *result = rle->start; 712a3be63b3SDoug Rabson else 713a3be63b3SDoug Rabson *result = -1; 714a3be63b3SDoug Rabson break; 715a3be63b3SDoug Rabson 716a3be63b3SDoug Rabson case ISA_IVAR_PORTSIZE_0: 717a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_IOPORT, 0); 718a3be63b3SDoug Rabson if (rle) 719a3be63b3SDoug Rabson *result = rle->count; 720a3be63b3SDoug Rabson else 721a3be63b3SDoug Rabson *result = 0; 722a3be63b3SDoug Rabson break; 723a3be63b3SDoug Rabson 724a3be63b3SDoug Rabson case ISA_IVAR_PORTSIZE_1: 725a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_IOPORT, 1); 726a3be63b3SDoug Rabson if (rle) 727a3be63b3SDoug Rabson *result = rle->count; 728a3be63b3SDoug Rabson else 729a3be63b3SDoug Rabson *result = 0; 730a3be63b3SDoug Rabson break; 731a3be63b3SDoug Rabson 732a3be63b3SDoug Rabson case ISA_IVAR_MADDR_0: 733a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_MEMORY, 0); 734a3be63b3SDoug Rabson if (rle) 735a3be63b3SDoug Rabson *result = rle->start; 736a3be63b3SDoug Rabson else 737a3be63b3SDoug Rabson *result = -1; 738a3be63b3SDoug Rabson break; 739a3be63b3SDoug Rabson 740a3be63b3SDoug Rabson case ISA_IVAR_MADDR_1: 741a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_MEMORY, 1); 742a3be63b3SDoug Rabson if (rle) 743a3be63b3SDoug Rabson *result = rle->start; 744a3be63b3SDoug Rabson else 745a3be63b3SDoug Rabson *result = -1; 746a3be63b3SDoug Rabson break; 747a3be63b3SDoug Rabson 74863e30378SJohn Baldwin case ISA_IVAR_MEMSIZE_0: 749a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_MEMORY, 0); 750a3be63b3SDoug Rabson if (rle) 751a3be63b3SDoug Rabson *result = rle->count; 752a3be63b3SDoug Rabson else 753a3be63b3SDoug Rabson *result = 0; 754a3be63b3SDoug Rabson break; 755a3be63b3SDoug Rabson 75663e30378SJohn Baldwin case ISA_IVAR_MEMSIZE_1: 757a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_MEMORY, 1); 758a3be63b3SDoug Rabson if (rle) 759a3be63b3SDoug Rabson *result = rle->count; 760a3be63b3SDoug Rabson else 761a3be63b3SDoug Rabson *result = 0; 762a3be63b3SDoug Rabson break; 763a3be63b3SDoug Rabson 764a3be63b3SDoug Rabson case ISA_IVAR_IRQ_0: 765a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_IRQ, 0); 766a3be63b3SDoug Rabson if (rle) 767a3be63b3SDoug Rabson *result = rle->start; 768a3be63b3SDoug Rabson else 769a3be63b3SDoug Rabson *result = -1; 770a3be63b3SDoug Rabson break; 771a3be63b3SDoug Rabson 772a3be63b3SDoug Rabson case ISA_IVAR_IRQ_1: 773a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_IRQ, 1); 774a3be63b3SDoug Rabson if (rle) 775a3be63b3SDoug Rabson *result = rle->start; 776a3be63b3SDoug Rabson else 777a3be63b3SDoug Rabson *result = -1; 778a3be63b3SDoug Rabson break; 779a3be63b3SDoug Rabson 780a3be63b3SDoug Rabson case ISA_IVAR_DRQ_0: 781a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_DRQ, 0); 782a3be63b3SDoug Rabson if (rle) 783a3be63b3SDoug Rabson *result = rle->start; 784a3be63b3SDoug Rabson else 785a3be63b3SDoug Rabson *result = -1; 786a3be63b3SDoug Rabson break; 787a3be63b3SDoug Rabson 788a3be63b3SDoug Rabson case ISA_IVAR_DRQ_1: 789a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_DRQ, 1); 790a3be63b3SDoug Rabson if (rle) 791a3be63b3SDoug Rabson *result = rle->start; 792a3be63b3SDoug Rabson else 793a3be63b3SDoug Rabson *result = -1; 794a3be63b3SDoug Rabson break; 795a3be63b3SDoug Rabson 796cfa84f46SDoug Rabson case ISA_IVAR_VENDORID: 797cfa84f46SDoug Rabson *result = idev->id_vendorid; 798cfa84f46SDoug Rabson break; 799cfa84f46SDoug Rabson 800cfa84f46SDoug Rabson case ISA_IVAR_SERIAL: 801cfa84f46SDoug Rabson *result = idev->id_serial; 802cfa84f46SDoug Rabson break; 803cfa84f46SDoug Rabson 804cfa84f46SDoug Rabson case ISA_IVAR_LOGICALID: 805cfa84f46SDoug Rabson *result = idev->id_logicalid; 806cfa84f46SDoug Rabson break; 807cfa84f46SDoug Rabson 808cfa84f46SDoug Rabson case ISA_IVAR_COMPATID: 809cfa84f46SDoug Rabson *result = idev->id_compatid; 810cfa84f46SDoug Rabson break; 811cfa84f46SDoug Rabson 8127abb4662SKazutaka YOKOTA case ISA_IVAR_CONFIGATTR: 8137abb4662SKazutaka YOKOTA *result = idev->id_config_attr; 8147abb4662SKazutaka YOKOTA break; 8157abb4662SKazutaka YOKOTA 816cfa84f46SDoug Rabson default: 817a3be63b3SDoug Rabson return ENOENT; 818a3be63b3SDoug Rabson } 819a3be63b3SDoug Rabson 820cfa84f46SDoug Rabson return 0; 821cfa84f46SDoug Rabson } 822cfa84f46SDoug Rabson 823a3be63b3SDoug Rabson static int 824a3be63b3SDoug Rabson isa_write_ivar(device_t bus, device_t dev, 825a3be63b3SDoug Rabson int index, uintptr_t value) 826a3be63b3SDoug Rabson { 827a3be63b3SDoug Rabson struct isa_device* idev = DEVTOISA(dev); 828a3be63b3SDoug Rabson 829a3be63b3SDoug Rabson switch (index) { 830a3be63b3SDoug Rabson case ISA_IVAR_PORT_0: 831a3be63b3SDoug Rabson case ISA_IVAR_PORT_1: 832a3be63b3SDoug Rabson case ISA_IVAR_PORTSIZE_0: 833a3be63b3SDoug Rabson case ISA_IVAR_PORTSIZE_1: 834a3be63b3SDoug Rabson case ISA_IVAR_MADDR_0: 835a3be63b3SDoug Rabson case ISA_IVAR_MADDR_1: 83663e30378SJohn Baldwin case ISA_IVAR_MEMSIZE_0: 83763e30378SJohn Baldwin case ISA_IVAR_MEMSIZE_1: 838a3be63b3SDoug Rabson case ISA_IVAR_IRQ_0: 839a3be63b3SDoug Rabson case ISA_IVAR_IRQ_1: 840a3be63b3SDoug Rabson case ISA_IVAR_DRQ_0: 841a3be63b3SDoug Rabson case ISA_IVAR_DRQ_1: 842a3be63b3SDoug Rabson return EINVAL; 843a3be63b3SDoug Rabson 844cfa84f46SDoug Rabson case ISA_IVAR_VENDORID: 845cfa84f46SDoug Rabson idev->id_vendorid = value; 846cfa84f46SDoug Rabson break; 847cfa84f46SDoug Rabson 848cfa84f46SDoug Rabson case ISA_IVAR_SERIAL: 849cfa84f46SDoug Rabson idev->id_serial = value; 850cfa84f46SDoug Rabson break; 851cfa84f46SDoug Rabson 852cfa84f46SDoug Rabson case ISA_IVAR_LOGICALID: 853cfa84f46SDoug Rabson idev->id_logicalid = value; 854cfa84f46SDoug Rabson break; 855cfa84f46SDoug Rabson 856cfa84f46SDoug Rabson case ISA_IVAR_COMPATID: 857cfa84f46SDoug Rabson idev->id_compatid = value; 858cfa84f46SDoug Rabson break; 859cfa84f46SDoug Rabson 8607abb4662SKazutaka YOKOTA case ISA_IVAR_CONFIGATTR: 8617abb4662SKazutaka YOKOTA idev->id_config_attr = value; 8627abb4662SKazutaka YOKOTA break; 8637abb4662SKazutaka YOKOTA 864a3be63b3SDoug Rabson default: 865a3be63b3SDoug Rabson return (ENOENT); 866a3be63b3SDoug Rabson } 867cfa84f46SDoug Rabson 868a3be63b3SDoug Rabson return (0); 869a3be63b3SDoug Rabson } 870a3be63b3SDoug Rabson 8714249382dSDoug Rabson /* 8724249382dSDoug Rabson * Free any resources which the driver missed or which we were holding for 8734249382dSDoug Rabson * it (see isa_probe_children). 8744249382dSDoug Rabson */ 8754249382dSDoug Rabson static void 8764249382dSDoug Rabson isa_child_detached(device_t dev, device_t child) 8774249382dSDoug Rabson { 8784249382dSDoug Rabson struct isa_device* idev = DEVTOISA(child); 87925afb89bSDoug Rabson struct resource_list *rl = &idev->id_resources; 8804249382dSDoug Rabson struct resource_list_entry *rle; 8814249382dSDoug Rabson 882328d36e9SDoug Rabson if (TAILQ_FIRST(&idev->id_configs)) { 883328d36e9SDoug Rabson /* 884328d36e9SDoug Rabson * Claim any unallocated resources to keep other 885328d36e9SDoug Rabson * devices from using them. 886328d36e9SDoug Rabson */ 887328d36e9SDoug Rabson SLIST_FOREACH(rle, rl, link) { 888328d36e9SDoug Rabson if (!rle->res) { 889328d36e9SDoug Rabson int rid = rle->rid; 890328d36e9SDoug Rabson resource_list_alloc(rl, dev, child, 8914249382dSDoug Rabson rle->type, 892328d36e9SDoug Rabson &rid, 0, ~0, 1, 0); 893328d36e9SDoug Rabson } 894328d36e9SDoug Rabson } 8954249382dSDoug Rabson } 8964249382dSDoug Rabson } 8974249382dSDoug Rabson 898ec22663bSDoug Rabson static void 899ec22663bSDoug Rabson isa_driver_added(device_t dev, driver_t *driver) 900ec22663bSDoug Rabson { 901ec22663bSDoug Rabson device_t *children; 902ec22663bSDoug Rabson int nchildren, i; 903ec22663bSDoug Rabson 904ec22663bSDoug Rabson /* 905ec22663bSDoug Rabson * Don't do anything if drivers are dynamically 906ec22663bSDoug Rabson * added during autoconfiguration (cf. ymf724). 907ec22663bSDoug Rabson * since that would end up calling identify 908ec22663bSDoug Rabson * twice. 909ec22663bSDoug Rabson */ 910ec22663bSDoug Rabson if (!isa_running) 911ec22663bSDoug Rabson return; 912ec22663bSDoug Rabson 913ec22663bSDoug Rabson DEVICE_IDENTIFY(driver, dev); 914ec22663bSDoug Rabson if (device_get_children(dev, &children, &nchildren)) 915ec22663bSDoug Rabson return; 916ec22663bSDoug Rabson 917ec22663bSDoug Rabson for (i = 0; i < nchildren; i++) { 918ec22663bSDoug Rabson device_t child = children[i]; 919ec22663bSDoug Rabson struct isa_device *idev = DEVTOISA(child); 920ec22663bSDoug Rabson struct resource_list *rl = &idev->id_resources; 921ec22663bSDoug Rabson struct resource_list_entry *rle; 922ec22663bSDoug Rabson 923ec22663bSDoug Rabson if (device_get_state(child) != DS_NOTPRESENT) 924ec22663bSDoug Rabson continue; 925328d36e9SDoug Rabson if (!device_is_enabled(child)) 926328d36e9SDoug Rabson continue; 927328d36e9SDoug Rabson 928328d36e9SDoug Rabson /* 929328d36e9SDoug Rabson * Free resources which we were holding on behalf of 930328d36e9SDoug Rabson * the device. 931328d36e9SDoug Rabson */ 932328d36e9SDoug Rabson SLIST_FOREACH(rle, &idev->id_resources, link) { 933328d36e9SDoug Rabson if (rle->res) 934328d36e9SDoug Rabson resource_list_release(rl, dev, child, 935328d36e9SDoug Rabson rle->type, 936328d36e9SDoug Rabson rle->rid, 937328d36e9SDoug Rabson rle->res); 938328d36e9SDoug Rabson } 939ec22663bSDoug Rabson 940ec22663bSDoug Rabson if (TAILQ_FIRST(&idev->id_configs)) 941ec22663bSDoug Rabson if (!isa_assign_resources(child)) 942ec22663bSDoug Rabson continue; 943ec22663bSDoug Rabson 944ec22663bSDoug Rabson device_probe_and_attach(child); 945ec22663bSDoug Rabson 946ec22663bSDoug Rabson if (TAILQ_FIRST(&idev->id_configs)) { 947ec22663bSDoug Rabson /* 948ec22663bSDoug Rabson * Claim any unallocated resources to keep other 949ec22663bSDoug Rabson * devices from using them. 950ec22663bSDoug Rabson */ 951ec22663bSDoug Rabson SLIST_FOREACH(rle, rl, link) { 952ec22663bSDoug Rabson if (!rle->res) { 953ec22663bSDoug Rabson int rid = rle->rid; 954ec22663bSDoug Rabson resource_list_alloc(rl, dev, child, 955ec22663bSDoug Rabson rle->type, 956328d36e9SDoug Rabson &rid, 0, ~0, 1, 0); 957ec22663bSDoug Rabson } 958ec22663bSDoug Rabson } 959ec22663bSDoug Rabson } 960ec22663bSDoug Rabson } 961ec22663bSDoug Rabson 962ec22663bSDoug Rabson free(children, M_TEMP); 963ec22663bSDoug Rabson } 964ec22663bSDoug Rabson 965a3be63b3SDoug Rabson static int 966a3be63b3SDoug Rabson isa_set_resource(device_t dev, device_t child, int type, int rid, 967a3be63b3SDoug Rabson u_long start, u_long count) 968a3be63b3SDoug Rabson { 969a3be63b3SDoug Rabson struct isa_device* idev = DEVTOISA(child); 970a3be63b3SDoug Rabson struct resource_list *rl = &idev->id_resources; 971a3be63b3SDoug Rabson 972a3be63b3SDoug Rabson if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY 973a3be63b3SDoug Rabson && type != SYS_RES_IRQ && type != SYS_RES_DRQ) 974a3be63b3SDoug Rabson return EINVAL; 9754249382dSDoug Rabson if (rid < 0) 9764249382dSDoug Rabson return EINVAL; 9774249382dSDoug Rabson if (type == SYS_RES_IOPORT && rid >= ISA_NPORT) 9784249382dSDoug Rabson return EINVAL; 9794249382dSDoug Rabson if (type == SYS_RES_MEMORY && rid >= ISA_NMEM) 9804249382dSDoug Rabson return EINVAL; 9814249382dSDoug Rabson if (type == SYS_RES_IRQ && rid >= ISA_NIRQ) 9824249382dSDoug Rabson return EINVAL; 9834249382dSDoug Rabson if (type == SYS_RES_DRQ && rid >= ISA_NDRQ) 984a3be63b3SDoug Rabson return EINVAL; 985a3be63b3SDoug Rabson 986a3be63b3SDoug Rabson resource_list_add(rl, type, rid, start, start + count - 1, count); 987a3be63b3SDoug Rabson 988a3be63b3SDoug Rabson return 0; 989a3be63b3SDoug Rabson } 990a3be63b3SDoug Rabson 9911581afb3SMatthew N. Dodd static struct resource_list * 9921581afb3SMatthew N. Dodd isa_get_resource_list (device_t dev, device_t child) 993a3be63b3SDoug Rabson { 994a3be63b3SDoug Rabson struct isa_device* idev = DEVTOISA(child); 995a3be63b3SDoug Rabson struct resource_list *rl = &idev->id_resources; 996a3be63b3SDoug Rabson 9971581afb3SMatthew N. Dodd if (!rl) 9981581afb3SMatthew N. Dodd return (NULL); 999a3be63b3SDoug Rabson 10001581afb3SMatthew N. Dodd return (rl); 1001cfa84f46SDoug Rabson } 1002cfa84f46SDoug Rabson 10034249382dSDoug Rabson static int 10044249382dSDoug Rabson isa_add_config(device_t dev, device_t child, 10054249382dSDoug Rabson int priority, struct isa_config *config) 10064249382dSDoug Rabson { 10074249382dSDoug Rabson struct isa_device* idev = DEVTOISA(child); 10084249382dSDoug Rabson struct isa_config_entry *newice, *ice; 10094249382dSDoug Rabson 10104249382dSDoug Rabson newice = malloc(sizeof *ice, M_DEVBUF, M_NOWAIT); 10114249382dSDoug Rabson if (!newice) 10124249382dSDoug Rabson return ENOMEM; 10134249382dSDoug Rabson 10144249382dSDoug Rabson newice->ice_priority = priority; 10154249382dSDoug Rabson newice->ice_config = *config; 10164249382dSDoug Rabson 10174249382dSDoug Rabson TAILQ_FOREACH(ice, &idev->id_configs, ice_link) { 10184249382dSDoug Rabson if (ice->ice_priority > priority) 10194249382dSDoug Rabson break; 10204249382dSDoug Rabson } 10214249382dSDoug Rabson if (ice) 10224249382dSDoug Rabson TAILQ_INSERT_BEFORE(ice, newice, ice_link); 10234249382dSDoug Rabson else 10244249382dSDoug Rabson TAILQ_INSERT_TAIL(&idev->id_configs, newice, ice_link); 10254249382dSDoug Rabson 1026c3959391SKazutaka YOKOTA if (isa_has_single_config(child)) 1027c3959391SKazutaka YOKOTA idev->id_config_attr &= ~ISACFGATTR_MULTI; 1028c3959391SKazutaka YOKOTA else 1029c3959391SKazutaka YOKOTA idev->id_config_attr |= ISACFGATTR_MULTI; 1030c3959391SKazutaka YOKOTA 10314249382dSDoug Rabson return 0; 10324249382dSDoug Rabson } 10334249382dSDoug Rabson 10344249382dSDoug Rabson static void 10354249382dSDoug Rabson isa_set_config_callback(device_t dev, device_t child, 10364249382dSDoug Rabson isa_config_cb *fn, void *arg) 10374249382dSDoug Rabson { 10384249382dSDoug Rabson struct isa_device* idev = DEVTOISA(child); 10394249382dSDoug Rabson 10404249382dSDoug Rabson idev->id_config_cb = fn; 10414249382dSDoug Rabson idev->id_config_arg = arg; 10424249382dSDoug Rabson } 10434249382dSDoug Rabson 10444249382dSDoug Rabson static int 10454249382dSDoug Rabson isa_pnp_probe(device_t dev, device_t child, struct isa_pnp_id *ids) 10464249382dSDoug Rabson { 10474249382dSDoug Rabson struct isa_device* idev = DEVTOISA(child); 10484249382dSDoug Rabson 10494249382dSDoug Rabson if (!idev->id_vendorid) 10504249382dSDoug Rabson return ENOENT; 10514249382dSDoug Rabson 10522e5bbc3fSMike Smith while (ids && ids->ip_id) { 10534249382dSDoug Rabson /* 10544249382dSDoug Rabson * Really ought to support >1 compat id per device. 10554249382dSDoug Rabson */ 10564249382dSDoug Rabson if (idev->id_logicalid == ids->ip_id 10574249382dSDoug Rabson || idev->id_compatid == ids->ip_id) { 10582aadbbb6SDoug Rabson if (ids->ip_desc) 10594249382dSDoug Rabson device_set_desc(child, ids->ip_desc); 10604249382dSDoug Rabson return 0; 10614249382dSDoug Rabson } 10624249382dSDoug Rabson ids++; 10634249382dSDoug Rabson } 10644249382dSDoug Rabson 10654249382dSDoug Rabson return ENXIO; 10664249382dSDoug Rabson } 10674249382dSDoug Rabson 1068a3be63b3SDoug Rabson static device_method_t isa_methods[] = { 1069a3be63b3SDoug Rabson /* Device interface */ 1070a3be63b3SDoug Rabson DEVMETHOD(device_probe, isa_probe), 1071a3be63b3SDoug Rabson DEVMETHOD(device_attach, isa_attach), 1072a3be63b3SDoug Rabson DEVMETHOD(device_detach, bus_generic_detach), 1073a3be63b3SDoug Rabson DEVMETHOD(device_shutdown, bus_generic_shutdown), 1074a3be63b3SDoug Rabson DEVMETHOD(device_suspend, bus_generic_suspend), 1075a3be63b3SDoug Rabson DEVMETHOD(device_resume, bus_generic_resume), 1076a3be63b3SDoug Rabson 1077a3be63b3SDoug Rabson /* Bus interface */ 1078a3be63b3SDoug Rabson DEVMETHOD(bus_add_child, isa_add_child), 1079a3be63b3SDoug Rabson DEVMETHOD(bus_print_child, isa_print_child), 1080328d36e9SDoug Rabson DEVMETHOD(bus_probe_nomatch, isa_probe_nomatch), 1081a3be63b3SDoug Rabson DEVMETHOD(bus_read_ivar, isa_read_ivar), 1082a3be63b3SDoug Rabson DEVMETHOD(bus_write_ivar, isa_write_ivar), 10834249382dSDoug Rabson DEVMETHOD(bus_child_detached, isa_child_detached), 1084ec22663bSDoug Rabson DEVMETHOD(bus_driver_added, isa_driver_added), 1085a3be63b3SDoug Rabson DEVMETHOD(bus_setup_intr, isa_setup_intr), 1086a3be63b3SDoug Rabson DEVMETHOD(bus_teardown_intr, isa_teardown_intr), 10871581afb3SMatthew N. Dodd 10881581afb3SMatthew N. Dodd DEVMETHOD(bus_get_resource_list,isa_get_resource_list), 10891581afb3SMatthew N. Dodd DEVMETHOD(bus_alloc_resource, isa_alloc_resource), 10901581afb3SMatthew N. Dodd DEVMETHOD(bus_release_resource, isa_release_resource), 109125afb89bSDoug Rabson DEVMETHOD(bus_set_resource, isa_set_resource), 10921581afb3SMatthew N. Dodd DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 10931581afb3SMatthew N. Dodd DEVMETHOD(bus_delete_resource, bus_generic_rl_delete_resource), 10941581afb3SMatthew N. Dodd DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 10951581afb3SMatthew N. Dodd DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 1096a3be63b3SDoug Rabson 1097a3be63b3SDoug Rabson /* ISA interface */ 10984249382dSDoug Rabson DEVMETHOD(isa_add_config, isa_add_config), 10994249382dSDoug Rabson DEVMETHOD(isa_set_config_callback, isa_set_config_callback), 11004249382dSDoug Rabson DEVMETHOD(isa_pnp_probe, isa_pnp_probe), 1101a3be63b3SDoug Rabson 1102a3be63b3SDoug Rabson { 0, 0 } 1103a3be63b3SDoug Rabson }; 1104a3be63b3SDoug Rabson 1105a3be63b3SDoug Rabson static driver_t isa_driver = { 1106a3be63b3SDoug Rabson "isa", 1107a3be63b3SDoug Rabson isa_methods, 1108a3be63b3SDoug Rabson 1, /* no softc */ 1109a3be63b3SDoug Rabson }; 1110a3be63b3SDoug Rabson 1111a3be63b3SDoug Rabson /* 1112c1e309b3SJohn Baldwin * ISA can be attached to a PCI-ISA bridge or directly to the legacy device. 1113a3be63b3SDoug Rabson */ 1114a3be63b3SDoug Rabson DRIVER_MODULE(isa, isab, isa_driver, isa_devclass, 0, 0); 11155aea03b0SMike Smith DRIVER_MODULE(isa, eisab, isa_driver, isa_devclass, 0, 0); 11160eb8d2e8SPeter Wemm #if defined(__i386__) || defined(__amd64__) 1117c1e309b3SJohn Baldwin DRIVER_MODULE(isa, legacy, isa_driver, isa_devclass, 0, 0); 1118a3be63b3SDoug Rabson #endif 1119f7130d09SMatthew N. Dodd MODULE_VERSION(isa, 1); 1120c37faf26SJohn Baldwin 1121c37faf26SJohn Baldwin /* 1122c37faf26SJohn Baldwin * Code common to ISA bridges. 1123c37faf26SJohn Baldwin */ 1124c37faf26SJohn Baldwin 1125c37faf26SJohn Baldwin devclass_t isab_devclass; 1126c37faf26SJohn Baldwin 1127c37faf26SJohn Baldwin int 1128c37faf26SJohn Baldwin isab_attach(device_t dev) 1129c37faf26SJohn Baldwin { 1130c37faf26SJohn Baldwin device_t child; 1131c37faf26SJohn Baldwin 1132c37faf26SJohn Baldwin child = device_add_child(dev, "isa", 0); 1133c37faf26SJohn Baldwin if (child != NULL) 1134c37faf26SJohn Baldwin return (bus_generic_attach(dev)); 1135c37faf26SJohn Baldwin return (ENXIO); 1136c37faf26SJohn Baldwin } 1137