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 * 26c3aac50fSPeter Wemm * $FreeBSD$ 27a3be63b3SDoug Rabson */ 28a3be63b3SDoug Rabson /* 29a3be63b3SDoug Rabson * Modifications for Intel architecture by Garrett A. Wollman. 30a3be63b3SDoug Rabson * Copyright 1998 Massachusetts Institute of Technology 31a3be63b3SDoug Rabson * 32a3be63b3SDoug Rabson * Permission to use, copy, modify, and distribute this software and 33a3be63b3SDoug Rabson * its documentation for any purpose and without fee is hereby 34a3be63b3SDoug Rabson * granted, provided that both the above copyright notice and this 35a3be63b3SDoug Rabson * permission notice appear in all copies, that both the above 36a3be63b3SDoug Rabson * copyright notice and this permission notice appear in all 37a3be63b3SDoug Rabson * supporting documentation, and that the name of M.I.T. not be used 38a3be63b3SDoug Rabson * in advertising or publicity pertaining to distribution of the 39a3be63b3SDoug Rabson * software without specific, written prior permission. M.I.T. makes 40a3be63b3SDoug Rabson * no representations about the suitability of this software for any 41a3be63b3SDoug Rabson * purpose. It is provided "as is" without express or implied 42a3be63b3SDoug Rabson * warranty. 43a3be63b3SDoug Rabson * 44a3be63b3SDoug Rabson * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 45a3be63b3SDoug Rabson * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 46a3be63b3SDoug Rabson * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 47a3be63b3SDoug Rabson * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 48a3be63b3SDoug Rabson * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 49a3be63b3SDoug Rabson * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 50a3be63b3SDoug Rabson * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 51a3be63b3SDoug Rabson * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 52a3be63b3SDoug Rabson * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 53a3be63b3SDoug Rabson * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 54a3be63b3SDoug Rabson * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 55a3be63b3SDoug Rabson * SUCH DAMAGE. 56a3be63b3SDoug Rabson */ 57a3be63b3SDoug Rabson 58a3be63b3SDoug Rabson /* 59a3be63b3SDoug Rabson * Parts of the ISA bus implementation common to all architectures. 60a3be63b3SDoug Rabson */ 61a3be63b3SDoug Rabson 62a3be63b3SDoug Rabson #include <sys/param.h> 63a3be63b3SDoug Rabson #include <sys/systm.h> 64a3be63b3SDoug Rabson #include <sys/kernel.h> 65a3be63b3SDoug Rabson #include <sys/bus.h> 66a3be63b3SDoug Rabson #include <sys/malloc.h> 67a3be63b3SDoug Rabson #include <sys/module.h> 68a3be63b3SDoug Rabson #include <machine/bus.h> 69a3be63b3SDoug Rabson #include <sys/rman.h> 70a3be63b3SDoug Rabson 71a3be63b3SDoug Rabson #include <machine/resource.h> 72a3be63b3SDoug Rabson 73a3be63b3SDoug Rabson #include <isa/isavar.h> 74a3be63b3SDoug Rabson #include <isa/isa_common.h> 75a3be63b3SDoug Rabson #ifdef __alpha__ /* XXX workaround a stupid warning */ 76a3be63b3SDoug Rabson #include <alpha/isa/isavar.h> 77a3be63b3SDoug Rabson #endif 78a3be63b3SDoug Rabson 792b2b44c9SJohn Baldwin static int isa_print_child(device_t bus, device_t dev); 802b2b44c9SJohn Baldwin 81959b7375SPoul-Henning Kamp static MALLOC_DEFINE(M_ISADEV, "isadev", "ISA device"); 82a3be63b3SDoug Rabson 83a3be63b3SDoug Rabson static devclass_t isa_devclass; 84ec22663bSDoug Rabson static int isa_running; 85a3be63b3SDoug Rabson 86a3be63b3SDoug Rabson /* 87a3be63b3SDoug Rabson * At 'probe' time, we add all the devices which we know about to the 88a3be63b3SDoug Rabson * bus. The generic attach routine will probe and attach them if they 89a3be63b3SDoug Rabson * are alive. 90a3be63b3SDoug Rabson */ 91a3be63b3SDoug Rabson static int 92a3be63b3SDoug Rabson isa_probe(device_t dev) 93a3be63b3SDoug Rabson { 94a3be63b3SDoug Rabson device_set_desc(dev, "ISA bus"); 95a3be63b3SDoug Rabson isa_init(); /* Allow machdep code to initialise */ 96ec22663bSDoug Rabson return 0; 97a3be63b3SDoug Rabson } 98a3be63b3SDoug Rabson 99a3be63b3SDoug Rabson extern device_t isa_bus_device; 100a3be63b3SDoug Rabson 101a3be63b3SDoug Rabson static int 102a3be63b3SDoug Rabson isa_attach(device_t dev) 103a3be63b3SDoug Rabson { 104a3be63b3SDoug Rabson /* 1054249382dSDoug Rabson * Arrange for isa_probe_children(dev) to be called later. XXX 106a3be63b3SDoug Rabson */ 107a3be63b3SDoug Rabson isa_bus_device = dev; 108a3be63b3SDoug Rabson return 0; 109a3be63b3SDoug Rabson } 110a3be63b3SDoug Rabson 111a3be63b3SDoug Rabson /* 1124249382dSDoug Rabson * Find a working set of memory regions for a child using the ranges 1134249382dSDoug Rabson * in *config and return the regions in *result. Returns non-zero if 1144249382dSDoug Rabson * a set of ranges was found. 1154249382dSDoug Rabson */ 1164249382dSDoug Rabson static int 1174249382dSDoug Rabson isa_find_memory(device_t child, 1184249382dSDoug Rabson struct isa_config *config, 1194249382dSDoug Rabson struct isa_config *result) 1204249382dSDoug Rabson { 1214249382dSDoug Rabson int success, i; 1224249382dSDoug Rabson struct resource *res[ISA_NMEM]; 1234249382dSDoug Rabson 1244249382dSDoug Rabson /* 1254249382dSDoug Rabson * First clear out any existing resource definitions. 1264249382dSDoug Rabson */ 1274249382dSDoug Rabson for (i = 0; i < ISA_NMEM; i++) { 12825afb89bSDoug Rabson bus_delete_resource(child, SYS_RES_MEMORY, i); 1294249382dSDoug Rabson res[i] = NULL; 1304249382dSDoug Rabson } 1314249382dSDoug Rabson 1324249382dSDoug Rabson success = 1; 1334249382dSDoug Rabson result->ic_nmem = config->ic_nmem; 1344249382dSDoug Rabson for (i = 0; i < config->ic_nmem; i++) { 1354249382dSDoug Rabson u_int32_t start, end, size, align; 136c3959391SKazutaka YOKOTA 137c3959391SKazutaka YOKOTA size = config->ic_mem[i].ir_size; 138c3959391SKazutaka YOKOTA 139c3959391SKazutaka YOKOTA /* the PnP device may have a null resource as filler */ 140c3959391SKazutaka YOKOTA if (size == 0) { 141c3959391SKazutaka YOKOTA result->ic_mem[i].ir_start = 0; 142c3959391SKazutaka YOKOTA result->ic_mem[i].ir_end = 0; 143c3959391SKazutaka YOKOTA result->ic_mem[i].ir_size = 0; 144c3959391SKazutaka YOKOTA result->ic_mem[i].ir_align = 0; 145c3959391SKazutaka YOKOTA continue; 146c3959391SKazutaka YOKOTA } 147c3959391SKazutaka YOKOTA 1484249382dSDoug Rabson for (start = config->ic_mem[i].ir_start, 1494249382dSDoug Rabson end = config->ic_mem[i].ir_end, 1504249382dSDoug Rabson align = config->ic_mem[i].ir_align; 1514249382dSDoug Rabson start + size - 1 <= end; 1524249382dSDoug Rabson start += align) { 15325afb89bSDoug Rabson bus_set_resource(child, SYS_RES_MEMORY, i, 1544249382dSDoug Rabson start, size); 1554249382dSDoug Rabson res[i] = bus_alloc_resource(child, 1564249382dSDoug Rabson SYS_RES_MEMORY, &i, 157208cc52fSMike Smith 0, ~0, 1, 0 /* !RF_ACTIVE */); 1584249382dSDoug Rabson if (res[i]) { 1594249382dSDoug Rabson result->ic_mem[i].ir_start = start; 1604249382dSDoug Rabson result->ic_mem[i].ir_end = start + size - 1; 1614249382dSDoug Rabson result->ic_mem[i].ir_size = size; 1624249382dSDoug Rabson result->ic_mem[i].ir_align = align; 1634249382dSDoug Rabson break; 1644249382dSDoug Rabson } 1654249382dSDoug Rabson } 1664249382dSDoug Rabson 1674249382dSDoug Rabson /* 1684249382dSDoug Rabson * If we didn't find a place for memory range i, then 1694249382dSDoug Rabson * give up now. 1704249382dSDoug Rabson */ 1714249382dSDoug Rabson if (!res[i]) { 1724249382dSDoug Rabson success = 0; 1734249382dSDoug Rabson break; 1744249382dSDoug Rabson } 1754249382dSDoug Rabson } 1764249382dSDoug Rabson 1774249382dSDoug Rabson for (i = 0; i < ISA_NMEM; i++) { 1784249382dSDoug Rabson if (res[i]) 1794249382dSDoug Rabson bus_release_resource(child, SYS_RES_MEMORY, 1804249382dSDoug Rabson i, res[i]); 1814249382dSDoug Rabson } 1824249382dSDoug Rabson 1834249382dSDoug Rabson return success; 1844249382dSDoug Rabson } 1854249382dSDoug Rabson 1864249382dSDoug Rabson /* 1874249382dSDoug Rabson * Find a working set of port regions for a child using the ranges 1884249382dSDoug Rabson * in *config and return the regions in *result. Returns non-zero if 1894249382dSDoug Rabson * a set of ranges was found. 1904249382dSDoug Rabson */ 1914249382dSDoug Rabson static int 1924249382dSDoug Rabson isa_find_port(device_t child, 1934249382dSDoug Rabson struct isa_config *config, 1944249382dSDoug Rabson struct isa_config *result) 1954249382dSDoug Rabson { 1964249382dSDoug Rabson int success, i; 1974249382dSDoug Rabson struct resource *res[ISA_NPORT]; 1984249382dSDoug Rabson 1994249382dSDoug Rabson /* 2004249382dSDoug Rabson * First clear out any existing resource definitions. 2014249382dSDoug Rabson */ 2024249382dSDoug Rabson for (i = 0; i < ISA_NPORT; i++) { 20325afb89bSDoug Rabson bus_delete_resource(child, SYS_RES_IOPORT, i); 2044249382dSDoug Rabson res[i] = NULL; 2054249382dSDoug Rabson } 2064249382dSDoug Rabson 2074249382dSDoug Rabson success = 1; 2084249382dSDoug Rabson result->ic_nport = config->ic_nport; 2094249382dSDoug Rabson for (i = 0; i < config->ic_nport; i++) { 2104249382dSDoug Rabson u_int32_t start, end, size, align; 211c3959391SKazutaka YOKOTA 212c3959391SKazutaka YOKOTA size = config->ic_port[i].ir_size; 213c3959391SKazutaka YOKOTA 214c3959391SKazutaka YOKOTA /* the PnP device may have a null resource as filler */ 215c3959391SKazutaka YOKOTA if (size == 0) { 216c3959391SKazutaka YOKOTA result->ic_port[i].ir_start = 0; 217c3959391SKazutaka YOKOTA result->ic_port[i].ir_end = 0; 218c3959391SKazutaka YOKOTA result->ic_port[i].ir_size = 0; 219c3959391SKazutaka YOKOTA result->ic_port[i].ir_align = 0; 220c3959391SKazutaka YOKOTA continue; 221c3959391SKazutaka YOKOTA } 222c3959391SKazutaka YOKOTA 2234249382dSDoug Rabson for (start = config->ic_port[i].ir_start, 2244249382dSDoug Rabson end = config->ic_port[i].ir_end, 2254249382dSDoug Rabson align = config->ic_port[i].ir_align; 2264249382dSDoug Rabson start + size - 1 <= end; 2274249382dSDoug Rabson start += align) { 22825afb89bSDoug Rabson bus_set_resource(child, SYS_RES_IOPORT, i, 2294249382dSDoug Rabson start, size); 2304249382dSDoug Rabson res[i] = bus_alloc_resource(child, 2314249382dSDoug Rabson SYS_RES_IOPORT, &i, 232208cc52fSMike Smith 0, ~0, 1, 0 /* !RF_ACTIVE */); 2334249382dSDoug Rabson if (res[i]) { 2344249382dSDoug Rabson result->ic_port[i].ir_start = start; 2354249382dSDoug Rabson result->ic_port[i].ir_end = start + size - 1; 2364249382dSDoug Rabson result->ic_port[i].ir_size = size; 2374249382dSDoug Rabson result->ic_port[i].ir_align = align; 2384249382dSDoug Rabson break; 2394249382dSDoug Rabson } 2404249382dSDoug Rabson } 2414249382dSDoug Rabson 2424249382dSDoug Rabson /* 2434249382dSDoug Rabson * If we didn't find a place for port range i, then 2444249382dSDoug Rabson * give up now. 2454249382dSDoug Rabson */ 2464249382dSDoug Rabson if (!res[i]) { 2474249382dSDoug Rabson success = 0; 2484249382dSDoug Rabson break; 2494249382dSDoug Rabson } 2504249382dSDoug Rabson } 2514249382dSDoug Rabson 2524249382dSDoug Rabson for (i = 0; i < ISA_NPORT; i++) { 2534249382dSDoug Rabson if (res[i]) 2544249382dSDoug Rabson bus_release_resource(child, SYS_RES_IOPORT, 2554249382dSDoug Rabson i, res[i]); 2564249382dSDoug Rabson } 2574249382dSDoug Rabson 2584249382dSDoug Rabson return success; 2594249382dSDoug Rabson } 2604249382dSDoug Rabson 2614249382dSDoug Rabson /* 2624249382dSDoug Rabson * Return the index of the first bit in the mask (or -1 if mask is empty. 2634249382dSDoug Rabson */ 2644249382dSDoug Rabson static int 2654249382dSDoug Rabson find_first_bit(u_int32_t mask) 2664249382dSDoug Rabson { 2674249382dSDoug Rabson return ffs(mask) - 1; 2684249382dSDoug Rabson } 2694249382dSDoug Rabson 2704249382dSDoug Rabson /* 2714249382dSDoug Rabson * Return the index of the next bit in the mask, or -1 if there are no more. 2724249382dSDoug Rabson */ 2734249382dSDoug Rabson static int 2744249382dSDoug Rabson find_next_bit(u_int32_t mask, int bit) 2754249382dSDoug Rabson { 2764249382dSDoug Rabson bit++; 2774249382dSDoug Rabson while (bit < 32 && !(mask & (1 << bit))) 2784249382dSDoug Rabson bit++; 2794249382dSDoug Rabson if (bit != 32) 2804249382dSDoug Rabson return bit; 2814249382dSDoug Rabson return -1; 2824249382dSDoug Rabson } 2834249382dSDoug Rabson 2844249382dSDoug Rabson /* 2854249382dSDoug Rabson * Find a working set of irqs for a child using the masks in *config 2864249382dSDoug Rabson * and return the regions in *result. Returns non-zero if a set of 2874249382dSDoug Rabson * irqs was found. 2884249382dSDoug Rabson */ 2894249382dSDoug Rabson static int 2904249382dSDoug Rabson isa_find_irq(device_t child, 2914249382dSDoug Rabson struct isa_config *config, 2924249382dSDoug Rabson struct isa_config *result) 2934249382dSDoug Rabson { 2944249382dSDoug Rabson int success, i; 2954249382dSDoug Rabson struct resource *res[ISA_NIRQ]; 2964249382dSDoug Rabson 2974249382dSDoug Rabson /* 2984249382dSDoug Rabson * First clear out any existing resource definitions. 2994249382dSDoug Rabson */ 3004249382dSDoug Rabson for (i = 0; i < ISA_NIRQ; i++) { 30125afb89bSDoug Rabson bus_delete_resource(child, SYS_RES_IRQ, i); 3024249382dSDoug Rabson res[i] = NULL; 3034249382dSDoug Rabson } 3044249382dSDoug Rabson 3054249382dSDoug Rabson success = 1; 3064249382dSDoug Rabson result->ic_nirq = config->ic_nirq; 3074249382dSDoug Rabson for (i = 0; i < config->ic_nirq; i++) { 3084249382dSDoug Rabson u_int32_t mask = config->ic_irqmask[i]; 3094249382dSDoug Rabson int irq; 310c3959391SKazutaka YOKOTA 311c3959391SKazutaka YOKOTA /* the PnP device may have a null resource as filler */ 312c3959391SKazutaka YOKOTA if (mask == 0) { 313c3959391SKazutaka YOKOTA result->ic_irqmask[i] = 0; 314c3959391SKazutaka YOKOTA continue; 315c3959391SKazutaka YOKOTA } 316c3959391SKazutaka YOKOTA 3174249382dSDoug Rabson for (irq = find_first_bit(mask); 3184249382dSDoug Rabson irq != -1; 3194249382dSDoug Rabson irq = find_next_bit(mask, irq)) { 32025afb89bSDoug Rabson bus_set_resource(child, SYS_RES_IRQ, i, 3214249382dSDoug Rabson irq, 1); 3224249382dSDoug Rabson res[i] = bus_alloc_resource(child, 3234249382dSDoug Rabson SYS_RES_IRQ, &i, 324208cc52fSMike Smith 0, ~0, 1, 0 /* !RF_ACTIVE */ ); 3254249382dSDoug Rabson if (res[i]) { 3264249382dSDoug Rabson result->ic_irqmask[i] = (1 << irq); 3274249382dSDoug Rabson break; 3284249382dSDoug Rabson } 3294249382dSDoug Rabson } 3304249382dSDoug Rabson 3314249382dSDoug Rabson /* 3324249382dSDoug Rabson * If we didn't find a place for irq range i, then 3334249382dSDoug Rabson * give up now. 3344249382dSDoug Rabson */ 3354249382dSDoug Rabson if (!res[i]) { 3364249382dSDoug Rabson success = 0; 3374249382dSDoug Rabson break; 3384249382dSDoug Rabson } 3394249382dSDoug Rabson } 3404249382dSDoug Rabson 3414249382dSDoug Rabson for (i = 0; i < ISA_NIRQ; i++) { 3424249382dSDoug Rabson if (res[i]) 3434249382dSDoug Rabson bus_release_resource(child, SYS_RES_IRQ, 3444249382dSDoug Rabson i, res[i]); 3454249382dSDoug Rabson } 3464249382dSDoug Rabson 3474249382dSDoug Rabson return success; 3484249382dSDoug Rabson } 3494249382dSDoug Rabson 3504249382dSDoug Rabson /* 3514249382dSDoug Rabson * Find a working set of drqs for a child using the masks in *config 3524249382dSDoug Rabson * and return the regions in *result. Returns non-zero if a set of 3534249382dSDoug Rabson * drqs was found. 3544249382dSDoug Rabson */ 3554249382dSDoug Rabson static int 3564249382dSDoug Rabson isa_find_drq(device_t child, 3574249382dSDoug Rabson struct isa_config *config, 3584249382dSDoug Rabson struct isa_config *result) 3594249382dSDoug Rabson { 3604249382dSDoug Rabson int success, i; 3614249382dSDoug Rabson struct resource *res[ISA_NDRQ]; 3624249382dSDoug Rabson 3634249382dSDoug Rabson /* 3644249382dSDoug Rabson * First clear out any existing resource definitions. 3654249382dSDoug Rabson */ 3664249382dSDoug Rabson for (i = 0; i < ISA_NDRQ; i++) { 36725afb89bSDoug Rabson bus_delete_resource(child, SYS_RES_DRQ, i); 3684249382dSDoug Rabson res[i] = NULL; 3694249382dSDoug Rabson } 3704249382dSDoug Rabson 3714249382dSDoug Rabson success = 1; 3724249382dSDoug Rabson result->ic_ndrq = config->ic_ndrq; 3734249382dSDoug Rabson for (i = 0; i < config->ic_ndrq; i++) { 3744249382dSDoug Rabson u_int32_t mask = config->ic_drqmask[i]; 3754249382dSDoug Rabson int drq; 376c3959391SKazutaka YOKOTA 377c3959391SKazutaka YOKOTA /* the PnP device may have a null resource as filler */ 378c3959391SKazutaka YOKOTA if (mask == 0) { 379c3959391SKazutaka YOKOTA result->ic_drqmask[i] = 0; 380c3959391SKazutaka YOKOTA continue; 381c3959391SKazutaka YOKOTA } 382c3959391SKazutaka YOKOTA 3834249382dSDoug Rabson for (drq = find_first_bit(mask); 3844249382dSDoug Rabson drq != -1; 3854249382dSDoug Rabson drq = find_next_bit(mask, drq)) { 38625afb89bSDoug Rabson bus_set_resource(child, SYS_RES_DRQ, i, 3874249382dSDoug Rabson drq, 1); 3884249382dSDoug Rabson res[i] = bus_alloc_resource(child, 3894249382dSDoug Rabson SYS_RES_DRQ, &i, 390208cc52fSMike Smith 0, ~0, 1, 0 /* !RF_ACTIVE */); 3914249382dSDoug Rabson if (res[i]) { 3924249382dSDoug Rabson result->ic_drqmask[i] = (1 << drq); 3934249382dSDoug Rabson break; 3944249382dSDoug Rabson } 3954249382dSDoug Rabson } 3964249382dSDoug Rabson 3974249382dSDoug Rabson /* 3984249382dSDoug Rabson * If we didn't find a place for drq range i, then 3994249382dSDoug Rabson * give up now. 4004249382dSDoug Rabson */ 4014249382dSDoug Rabson if (!res[i]) { 4024249382dSDoug Rabson success = 0; 4034249382dSDoug Rabson break; 4044249382dSDoug Rabson } 4054249382dSDoug Rabson } 4064249382dSDoug Rabson 4074249382dSDoug Rabson for (i = 0; i < ISA_NDRQ; i++) { 4084249382dSDoug Rabson if (res[i]) 4094249382dSDoug Rabson bus_release_resource(child, SYS_RES_DRQ, 4104249382dSDoug Rabson i, res[i]); 4114249382dSDoug Rabson } 4124249382dSDoug Rabson 4134249382dSDoug Rabson return success; 4144249382dSDoug Rabson } 4154249382dSDoug Rabson 4164249382dSDoug Rabson /* 4174249382dSDoug Rabson * Attempt to find a working set of resources for a device. Return 4184249382dSDoug Rabson * non-zero if a working configuration is found. 4194249382dSDoug Rabson */ 4204249382dSDoug Rabson static int 4214249382dSDoug Rabson isa_assign_resources(device_t child) 4224249382dSDoug Rabson { 4234249382dSDoug Rabson struct isa_device *idev = DEVTOISA(child); 4244249382dSDoug Rabson struct isa_config_entry *ice; 4253b82ede0SJulian Elischer struct isa_config *cfg; 4264249382dSDoug Rabson 4273b82ede0SJulian Elischer cfg = malloc(sizeof(struct isa_config), M_TEMP, M_NOWAIT|M_ZERO); 4283b82ede0SJulian Elischer if (cfg == NULL) 4293b82ede0SJulian Elischer return(0); 4304249382dSDoug Rabson TAILQ_FOREACH(ice, &idev->id_configs, ice_link) { 4313b82ede0SJulian Elischer if (!isa_find_memory(child, &ice->ice_config, cfg)) 4324249382dSDoug Rabson continue; 4333b82ede0SJulian Elischer if (!isa_find_port(child, &ice->ice_config, cfg)) 4344249382dSDoug Rabson continue; 4353b82ede0SJulian Elischer if (!isa_find_irq(child, &ice->ice_config, cfg)) 4364249382dSDoug Rabson continue; 4373b82ede0SJulian Elischer if (!isa_find_drq(child, &ice->ice_config, cfg)) 4384249382dSDoug Rabson continue; 4394249382dSDoug Rabson 4404249382dSDoug Rabson /* 4414249382dSDoug Rabson * A working configuration was found enable the device 4424249382dSDoug Rabson * with this configuration. 4434249382dSDoug Rabson */ 4444249382dSDoug Rabson if (idev->id_config_cb) { 4454249382dSDoug Rabson idev->id_config_cb(idev->id_config_arg, 4463b82ede0SJulian Elischer cfg, 1); 4473b82ede0SJulian Elischer free(cfg, M_TEMP); 4484249382dSDoug Rabson return 1; 4494249382dSDoug Rabson } 4504249382dSDoug Rabson } 4514249382dSDoug Rabson 4524249382dSDoug Rabson /* 4534249382dSDoug Rabson * Disable the device. 4544249382dSDoug Rabson */ 4552b2b44c9SJohn Baldwin bus_print_child_header(device_get_parent(child), child); 4562b2b44c9SJohn Baldwin printf(" can't assign resources\n"); 4572b2b44c9SJohn Baldwin if (bootverbose) 4582b2b44c9SJohn Baldwin isa_print_child(device_get_parent(child), child); 4593b82ede0SJulian Elischer bzero(cfg, sizeof (*cfg)); 4604249382dSDoug Rabson if (idev->id_config_cb) 4613b82ede0SJulian Elischer idev->id_config_cb(idev->id_config_arg, cfg, 0); 4624249382dSDoug Rabson device_disable(child); 4634249382dSDoug Rabson 4643b82ede0SJulian Elischer free(cfg, M_TEMP); 4654249382dSDoug Rabson return 0; 4664249382dSDoug Rabson } 4674249382dSDoug Rabson 4684249382dSDoug Rabson /* 469c3959391SKazutaka YOKOTA * Return non-zero if the device has a single configuration, that is, 470c3959391SKazutaka YOKOTA * a fixed set of resoruces. 471c3959391SKazutaka YOKOTA */ 472c3959391SKazutaka YOKOTA static int 473c3959391SKazutaka YOKOTA isa_has_single_config(device_t dev) 474c3959391SKazutaka YOKOTA { 475c3959391SKazutaka YOKOTA struct isa_device *idev = DEVTOISA(dev); 476c3959391SKazutaka YOKOTA struct isa_config_entry *ice; 477c3959391SKazutaka YOKOTA u_int32_t mask; 478c3959391SKazutaka YOKOTA int i; 479c3959391SKazutaka YOKOTA 480c3959391SKazutaka YOKOTA ice = TAILQ_FIRST(&idev->id_configs); 481c3959391SKazutaka YOKOTA if (TAILQ_NEXT(ice, ice_link)) 482c3959391SKazutaka YOKOTA return 0; 483c3959391SKazutaka YOKOTA 484c3959391SKazutaka YOKOTA for (i = 0; i < ice->ice_config.ic_nmem; ++i) { 485c3959391SKazutaka YOKOTA if (ice->ice_config.ic_mem[i].ir_size == 0) 486c3959391SKazutaka YOKOTA continue; 487c3959391SKazutaka YOKOTA if (ice->ice_config.ic_mem[i].ir_end != 488c3959391SKazutaka YOKOTA ice->ice_config.ic_mem[i].ir_start + 489c3959391SKazutaka YOKOTA ice->ice_config.ic_mem[i].ir_size - 1) 490c3959391SKazutaka YOKOTA return 0; 491c3959391SKazutaka YOKOTA } 492c3959391SKazutaka YOKOTA for (i = 0; i < ice->ice_config.ic_nport; ++i) { 493c3959391SKazutaka YOKOTA if (ice->ice_config.ic_port[i].ir_size == 0) 494c3959391SKazutaka YOKOTA continue; 495c3959391SKazutaka YOKOTA if (ice->ice_config.ic_port[i].ir_end != 496c3959391SKazutaka YOKOTA ice->ice_config.ic_port[i].ir_start + 497c3959391SKazutaka YOKOTA ice->ice_config.ic_port[i].ir_size - 1) 498c3959391SKazutaka YOKOTA return 0; 499c3959391SKazutaka YOKOTA } 500c3959391SKazutaka YOKOTA for (i = 0; i < ice->ice_config.ic_nirq; ++i) { 501c3959391SKazutaka YOKOTA mask = ice->ice_config.ic_irqmask[i]; 502c3959391SKazutaka YOKOTA if (mask == 0) 503c3959391SKazutaka YOKOTA continue; 504c3959391SKazutaka YOKOTA if (find_next_bit(mask, find_first_bit(mask)) != -1) 505c3959391SKazutaka YOKOTA return 0; 506c3959391SKazutaka YOKOTA } 507c3959391SKazutaka YOKOTA for (i = 0; i < ice->ice_config.ic_ndrq; ++i) { 508c3959391SKazutaka YOKOTA mask = ice->ice_config.ic_drqmask[i]; 509c3959391SKazutaka YOKOTA if (mask == 0) 510c3959391SKazutaka YOKOTA continue; 511c3959391SKazutaka YOKOTA if (find_next_bit(mask, find_first_bit(mask)) != -1) 512c3959391SKazutaka YOKOTA return 0; 513c3959391SKazutaka YOKOTA } 514c3959391SKazutaka YOKOTA return 1; 515c3959391SKazutaka YOKOTA } 516c3959391SKazutaka YOKOTA 517c3959391SKazutaka YOKOTA /* 5184249382dSDoug Rabson * Called after other devices have initialised to probe for isa devices. 5194249382dSDoug Rabson */ 5204249382dSDoug Rabson void 5214249382dSDoug Rabson isa_probe_children(device_t dev) 5224249382dSDoug Rabson { 5234249382dSDoug Rabson device_t *children; 5243b82ede0SJulian Elischer struct isa_config *cfg; 5254249382dSDoug Rabson int nchildren, i; 5264249382dSDoug Rabson 527ec22663bSDoug Rabson /* 528ec22663bSDoug Rabson * Create all the children by calling driver's identify methods. 529ec22663bSDoug Rabson */ 530ec22663bSDoug Rabson bus_generic_probe(dev); 531ec22663bSDoug Rabson 5324249382dSDoug Rabson if (device_get_children(dev, &children, &nchildren)) 5334249382dSDoug Rabson return; 5344249382dSDoug Rabson 5354249382dSDoug Rabson /* 536a2e6dbb3SDoug Rabson * First disable all pnp devices so that they don't get 537a2e6dbb3SDoug Rabson * matched by legacy probes. 538a2e6dbb3SDoug Rabson */ 539ec22663bSDoug Rabson if (bootverbose) 540ec22663bSDoug Rabson printf("isa_probe_children: disabling PnP devices\n"); 5413b82ede0SJulian Elischer 5423b82ede0SJulian Elischer cfg = malloc(sizeof(*cfg), M_TEMP, M_NOWAIT|M_ZERO); 5433b82ede0SJulian Elischer if (cfg == NULL) { 5443b82ede0SJulian Elischer free(children, M_TEMP); 5453b82ede0SJulian Elischer return; 5463b82ede0SJulian Elischer } 5473b82ede0SJulian Elischer 548a2e6dbb3SDoug Rabson for (i = 0; i < nchildren; i++) { 549a2e6dbb3SDoug Rabson device_t child = children[i]; 550a2e6dbb3SDoug Rabson struct isa_device *idev = DEVTOISA(child); 551a2e6dbb3SDoug Rabson 5523b82ede0SJulian Elischer bzero(cfg, sizeof(*cfg)); 553a2e6dbb3SDoug Rabson if (idev->id_config_cb) 5543b82ede0SJulian Elischer idev->id_config_cb(idev->id_config_arg, cfg, 0); 555a2e6dbb3SDoug Rabson } 556a2e6dbb3SDoug Rabson 5573b82ede0SJulian Elischer free(cfg, M_TEMP); 5583b82ede0SJulian Elischer 559a2e6dbb3SDoug Rabson /* 560a2e6dbb3SDoug Rabson * Next probe all non-pnp devices so that they claim their 5614249382dSDoug Rabson * resources first. 5624249382dSDoug Rabson */ 563ec22663bSDoug Rabson if (bootverbose) 564ec22663bSDoug Rabson printf("isa_probe_children: probing non-PnP devices\n"); 5654249382dSDoug Rabson for (i = 0; i < nchildren; i++) { 5664249382dSDoug Rabson device_t child = children[i]; 5674249382dSDoug Rabson struct isa_device *idev = DEVTOISA(child); 5684249382dSDoug Rabson 5694249382dSDoug Rabson if (TAILQ_FIRST(&idev->id_configs)) 5704249382dSDoug Rabson continue; 5714249382dSDoug Rabson 5724249382dSDoug Rabson device_probe_and_attach(child); 5734249382dSDoug Rabson } 5744249382dSDoug Rabson 5754249382dSDoug Rabson /* 576a2e6dbb3SDoug Rabson * Finally assign resource to pnp devices and probe them. 5774249382dSDoug Rabson */ 578ec22663bSDoug Rabson if (bootverbose) 579ec22663bSDoug Rabson printf("isa_probe_children: probing PnP devices\n"); 5804249382dSDoug Rabson for (i = 0; i < nchildren; i++) { 5814249382dSDoug Rabson device_t child = children[i]; 5824249382dSDoug Rabson struct isa_device* idev = DEVTOISA(child); 5834249382dSDoug Rabson 5844249382dSDoug Rabson if (!TAILQ_FIRST(&idev->id_configs)) 5854249382dSDoug Rabson continue; 5864249382dSDoug Rabson 5874249382dSDoug Rabson if (isa_assign_resources(child)) { 58825afb89bSDoug Rabson struct resource_list *rl = &idev->id_resources; 5894249382dSDoug Rabson struct resource_list_entry *rle; 5904249382dSDoug Rabson 5914249382dSDoug Rabson device_probe_and_attach(child); 5924249382dSDoug Rabson 5934249382dSDoug Rabson /* 5944249382dSDoug Rabson * Claim any unallocated resources to keep other 5954249382dSDoug Rabson * devices from using them. 5964249382dSDoug Rabson */ 59725afb89bSDoug Rabson SLIST_FOREACH(rle, rl, link) { 5984249382dSDoug Rabson if (!rle->res) { 5994249382dSDoug Rabson int rid = rle->rid; 60025afb89bSDoug Rabson resource_list_alloc(rl, dev, child, 6014249382dSDoug Rabson rle->type, 6024249382dSDoug Rabson &rid, 603328d36e9SDoug Rabson 0, ~0, 1, 0); 6044249382dSDoug Rabson } 6054249382dSDoug Rabson } 6064249382dSDoug Rabson } 6074249382dSDoug Rabson } 6084249382dSDoug Rabson 6094249382dSDoug Rabson free(children, M_TEMP); 610ec22663bSDoug Rabson 611ec22663bSDoug Rabson isa_running = 1; 6124249382dSDoug Rabson } 6134249382dSDoug Rabson 6144249382dSDoug Rabson /* 615a3be63b3SDoug Rabson * Add a new child with default ivars. 616a3be63b3SDoug Rabson */ 617a3be63b3SDoug Rabson static device_t 618bea6af4dSDoug Rabson isa_add_child(device_t dev, int order, const char *name, int unit) 619a3be63b3SDoug Rabson { 620fe0d4089SMatthew N. Dodd device_t child; 621a3be63b3SDoug Rabson struct isa_device *idev; 622a3be63b3SDoug Rabson 6237cc0979fSDavid Malone idev = malloc(sizeof(struct isa_device), M_ISADEV, M_NOWAIT | M_ZERO); 624a3be63b3SDoug Rabson if (!idev) 625a3be63b3SDoug Rabson return 0; 626a3be63b3SDoug Rabson 627a3be63b3SDoug Rabson resource_list_init(&idev->id_resources); 6284249382dSDoug Rabson TAILQ_INIT(&idev->id_configs); 629a3be63b3SDoug Rabson 630fe0d4089SMatthew N. Dodd child = device_add_child_ordered(dev, order, name, unit); 631fe0d4089SMatthew N. Dodd device_set_ivars(child, idev); 632fe0d4089SMatthew N. Dodd 633fe0d4089SMatthew N. Dodd return child; 634a3be63b3SDoug Rabson } 635a3be63b3SDoug Rabson 636328d36e9SDoug Rabson static int 637a3be63b3SDoug Rabson isa_print_resources(struct resource_list *rl, const char *name, int type, 63825afb89bSDoug Rabson int count, const char *format) 639a3be63b3SDoug Rabson { 6404249382dSDoug Rabson struct resource_list_entry *rle; 6414249382dSDoug Rabson int printed; 642328d36e9SDoug Rabson int i, retval = 0;; 643a3be63b3SDoug Rabson 6444249382dSDoug Rabson printed = 0; 64525afb89bSDoug Rabson for (i = 0; i < count; i++) { 6464249382dSDoug Rabson rle = resource_list_find(rl, type, i); 6474249382dSDoug Rabson if (rle) { 6484249382dSDoug Rabson if (printed == 0) 649328d36e9SDoug Rabson retval += printf(" %s ", name); 6504249382dSDoug Rabson else if (printed > 0) 651328d36e9SDoug Rabson retval += printf(","); 6524249382dSDoug Rabson printed++; 653328d36e9SDoug Rabson retval += printf(format, rle->start); 6544249382dSDoug Rabson if (rle->count > 1) { 655328d36e9SDoug Rabson retval += printf("-"); 656328d36e9SDoug Rabson retval += printf(format, 657328d36e9SDoug Rabson rle->start + rle->count - 1); 658a3be63b3SDoug Rabson } 6594249382dSDoug Rabson } else if (i > 3) { 6604249382dSDoug Rabson /* check the first few regardless */ 6614249382dSDoug Rabson break; 662a3be63b3SDoug Rabson } 663a3be63b3SDoug Rabson } 664328d36e9SDoug Rabson return retval; 665a3be63b3SDoug Rabson } 666a3be63b3SDoug Rabson 66715317dd8SMatthew N. Dodd static int 668328d36e9SDoug Rabson isa_print_all_resources(device_t dev) 669a3be63b3SDoug Rabson { 670a3be63b3SDoug Rabson struct isa_device *idev = DEVTOISA(dev); 671a3be63b3SDoug Rabson struct resource_list *rl = &idev->id_resources; 67215317dd8SMatthew N. Dodd int retval = 0; 67315317dd8SMatthew N. Dodd 674062acdb7SDoug Rabson if (SLIST_FIRST(rl) || device_get_flags(dev)) 67515317dd8SMatthew N. Dodd retval += printf(" at"); 676a3be63b3SDoug Rabson 677328d36e9SDoug Rabson retval += isa_print_resources(rl, "port", SYS_RES_IOPORT, 678328d36e9SDoug Rabson ISA_NPORT, "%#lx"); 679328d36e9SDoug Rabson retval += isa_print_resources(rl, "iomem", SYS_RES_MEMORY, 680328d36e9SDoug Rabson ISA_NMEM, "%#lx"); 681328d36e9SDoug Rabson retval += isa_print_resources(rl, "irq", SYS_RES_IRQ, 682328d36e9SDoug Rabson ISA_NIRQ, "%ld"); 683328d36e9SDoug Rabson retval += isa_print_resources(rl, "drq", SYS_RES_DRQ, 684328d36e9SDoug Rabson ISA_NDRQ, "%ld"); 685062acdb7SDoug Rabson if (device_get_flags(dev)) 686062acdb7SDoug Rabson retval += printf(" flags %#x", device_get_flags(dev)); 687a3be63b3SDoug Rabson 688328d36e9SDoug Rabson return retval; 689328d36e9SDoug Rabson } 690328d36e9SDoug Rabson 691328d36e9SDoug Rabson static int 692328d36e9SDoug Rabson isa_print_child(device_t bus, device_t dev) 693328d36e9SDoug Rabson { 694328d36e9SDoug Rabson int retval = 0; 695328d36e9SDoug Rabson 696328d36e9SDoug Rabson retval += bus_print_child_header(bus, dev); 697328d36e9SDoug Rabson retval += isa_print_all_resources(dev); 69815317dd8SMatthew N. Dodd retval += bus_print_child_footer(bus, dev); 69915317dd8SMatthew N. Dodd 70015317dd8SMatthew N. Dodd return (retval); 701a3be63b3SDoug Rabson } 702a3be63b3SDoug Rabson 703328d36e9SDoug Rabson static void 704328d36e9SDoug Rabson isa_probe_nomatch(device_t dev, device_t child) 705328d36e9SDoug Rabson { 7062b2b44c9SJohn Baldwin if (bootverbose) { 7072b2b44c9SJohn Baldwin bus_print_child_header(dev, child); 7082b2b44c9SJohn Baldwin printf(" failed to probe"); 709328d36e9SDoug Rabson isa_print_all_resources(child); 7102b2b44c9SJohn Baldwin bus_print_child_footer(dev, child); 7112b2b44c9SJohn Baldwin } 712328d36e9SDoug Rabson 713328d36e9SDoug Rabson return; 714328d36e9SDoug Rabson } 715328d36e9SDoug Rabson 716a3be63b3SDoug Rabson static int 717a3be63b3SDoug Rabson isa_read_ivar(device_t bus, device_t dev, int index, uintptr_t * result) 718a3be63b3SDoug Rabson { 719a3be63b3SDoug Rabson struct isa_device* idev = DEVTOISA(dev); 720a3be63b3SDoug Rabson struct resource_list *rl = &idev->id_resources; 721a3be63b3SDoug Rabson struct resource_list_entry *rle; 722a3be63b3SDoug Rabson 723a3be63b3SDoug Rabson switch (index) { 724a3be63b3SDoug Rabson case ISA_IVAR_PORT_0: 725a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_IOPORT, 0); 726a3be63b3SDoug Rabson if (rle) 727a3be63b3SDoug Rabson *result = rle->start; 728a3be63b3SDoug Rabson else 729a3be63b3SDoug Rabson *result = -1; 730a3be63b3SDoug Rabson break; 731a3be63b3SDoug Rabson 732a3be63b3SDoug Rabson case ISA_IVAR_PORT_1: 733a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_IOPORT, 1); 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_PORTSIZE_0: 741a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_IOPORT, 0); 742a3be63b3SDoug Rabson if (rle) 743a3be63b3SDoug Rabson *result = rle->count; 744a3be63b3SDoug Rabson else 745a3be63b3SDoug Rabson *result = 0; 746a3be63b3SDoug Rabson break; 747a3be63b3SDoug Rabson 748a3be63b3SDoug Rabson case ISA_IVAR_PORTSIZE_1: 749a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_IOPORT, 1); 750a3be63b3SDoug Rabson if (rle) 751a3be63b3SDoug Rabson *result = rle->count; 752a3be63b3SDoug Rabson else 753a3be63b3SDoug Rabson *result = 0; 754a3be63b3SDoug Rabson break; 755a3be63b3SDoug Rabson 756a3be63b3SDoug Rabson case ISA_IVAR_MADDR_0: 757a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_MEMORY, 0); 758a3be63b3SDoug Rabson if (rle) 759a3be63b3SDoug Rabson *result = rle->start; 760a3be63b3SDoug Rabson else 761a3be63b3SDoug Rabson *result = -1; 762a3be63b3SDoug Rabson break; 763a3be63b3SDoug Rabson 764a3be63b3SDoug Rabson case ISA_IVAR_MADDR_1: 765a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_MEMORY, 1); 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_MSIZE_0: 773a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_MEMORY, 0); 774a3be63b3SDoug Rabson if (rle) 775a3be63b3SDoug Rabson *result = rle->count; 776a3be63b3SDoug Rabson else 777a3be63b3SDoug Rabson *result = 0; 778a3be63b3SDoug Rabson break; 779a3be63b3SDoug Rabson 780a3be63b3SDoug Rabson case ISA_IVAR_MSIZE_1: 781a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_MEMORY, 1); 782a3be63b3SDoug Rabson if (rle) 783a3be63b3SDoug Rabson *result = rle->count; 784a3be63b3SDoug Rabson else 785a3be63b3SDoug Rabson *result = 0; 786a3be63b3SDoug Rabson break; 787a3be63b3SDoug Rabson 788a3be63b3SDoug Rabson case ISA_IVAR_IRQ_0: 789a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_IRQ, 0); 790a3be63b3SDoug Rabson if (rle) 791a3be63b3SDoug Rabson *result = rle->start; 792a3be63b3SDoug Rabson else 793a3be63b3SDoug Rabson *result = -1; 794a3be63b3SDoug Rabson break; 795a3be63b3SDoug Rabson 796a3be63b3SDoug Rabson case ISA_IVAR_IRQ_1: 797a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_IRQ, 1); 798a3be63b3SDoug Rabson if (rle) 799a3be63b3SDoug Rabson *result = rle->start; 800a3be63b3SDoug Rabson else 801a3be63b3SDoug Rabson *result = -1; 802a3be63b3SDoug Rabson break; 803a3be63b3SDoug Rabson 804a3be63b3SDoug Rabson case ISA_IVAR_DRQ_0: 805a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_DRQ, 0); 806a3be63b3SDoug Rabson if (rle) 807a3be63b3SDoug Rabson *result = rle->start; 808a3be63b3SDoug Rabson else 809a3be63b3SDoug Rabson *result = -1; 810a3be63b3SDoug Rabson break; 811a3be63b3SDoug Rabson 812a3be63b3SDoug Rabson case ISA_IVAR_DRQ_1: 813a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_DRQ, 1); 814a3be63b3SDoug Rabson if (rle) 815a3be63b3SDoug Rabson *result = rle->start; 816a3be63b3SDoug Rabson else 817a3be63b3SDoug Rabson *result = -1; 818a3be63b3SDoug Rabson break; 819a3be63b3SDoug Rabson 820cfa84f46SDoug Rabson case ISA_IVAR_VENDORID: 821cfa84f46SDoug Rabson *result = idev->id_vendorid; 822cfa84f46SDoug Rabson break; 823cfa84f46SDoug Rabson 824cfa84f46SDoug Rabson case ISA_IVAR_SERIAL: 825cfa84f46SDoug Rabson *result = idev->id_serial; 826cfa84f46SDoug Rabson break; 827cfa84f46SDoug Rabson 828cfa84f46SDoug Rabson case ISA_IVAR_LOGICALID: 829cfa84f46SDoug Rabson *result = idev->id_logicalid; 830cfa84f46SDoug Rabson break; 831cfa84f46SDoug Rabson 832cfa84f46SDoug Rabson case ISA_IVAR_COMPATID: 833cfa84f46SDoug Rabson *result = idev->id_compatid; 834cfa84f46SDoug Rabson break; 835cfa84f46SDoug Rabson 8367abb4662SKazutaka YOKOTA case ISA_IVAR_CONFIGATTR: 8377abb4662SKazutaka YOKOTA *result = idev->id_config_attr; 8387abb4662SKazutaka YOKOTA break; 8397abb4662SKazutaka YOKOTA 840cfa84f46SDoug Rabson default: 841a3be63b3SDoug Rabson return ENOENT; 842a3be63b3SDoug Rabson } 843a3be63b3SDoug Rabson 844cfa84f46SDoug Rabson return 0; 845cfa84f46SDoug Rabson } 846cfa84f46SDoug Rabson 847a3be63b3SDoug Rabson static int 848a3be63b3SDoug Rabson isa_write_ivar(device_t bus, device_t dev, 849a3be63b3SDoug Rabson int index, uintptr_t value) 850a3be63b3SDoug Rabson { 851a3be63b3SDoug Rabson struct isa_device* idev = DEVTOISA(dev); 852a3be63b3SDoug Rabson 853a3be63b3SDoug Rabson switch (index) { 854a3be63b3SDoug Rabson case ISA_IVAR_PORT_0: 855a3be63b3SDoug Rabson case ISA_IVAR_PORT_1: 856a3be63b3SDoug Rabson case ISA_IVAR_PORTSIZE_0: 857a3be63b3SDoug Rabson case ISA_IVAR_PORTSIZE_1: 858a3be63b3SDoug Rabson case ISA_IVAR_MADDR_0: 859a3be63b3SDoug Rabson case ISA_IVAR_MADDR_1: 860a3be63b3SDoug Rabson case ISA_IVAR_MSIZE_0: 861a3be63b3SDoug Rabson case ISA_IVAR_MSIZE_1: 862a3be63b3SDoug Rabson case ISA_IVAR_IRQ_0: 863a3be63b3SDoug Rabson case ISA_IVAR_IRQ_1: 864a3be63b3SDoug Rabson case ISA_IVAR_DRQ_0: 865a3be63b3SDoug Rabson case ISA_IVAR_DRQ_1: 866a3be63b3SDoug Rabson return EINVAL; 867a3be63b3SDoug Rabson 868cfa84f46SDoug Rabson case ISA_IVAR_VENDORID: 869cfa84f46SDoug Rabson idev->id_vendorid = value; 870cfa84f46SDoug Rabson break; 871cfa84f46SDoug Rabson 872cfa84f46SDoug Rabson case ISA_IVAR_SERIAL: 873cfa84f46SDoug Rabson idev->id_serial = value; 874cfa84f46SDoug Rabson break; 875cfa84f46SDoug Rabson 876cfa84f46SDoug Rabson case ISA_IVAR_LOGICALID: 877cfa84f46SDoug Rabson idev->id_logicalid = value; 878cfa84f46SDoug Rabson break; 879cfa84f46SDoug Rabson 880cfa84f46SDoug Rabson case ISA_IVAR_COMPATID: 881cfa84f46SDoug Rabson idev->id_compatid = value; 882cfa84f46SDoug Rabson break; 883cfa84f46SDoug Rabson 8847abb4662SKazutaka YOKOTA case ISA_IVAR_CONFIGATTR: 8857abb4662SKazutaka YOKOTA idev->id_config_attr = value; 8867abb4662SKazutaka YOKOTA break; 8877abb4662SKazutaka YOKOTA 888a3be63b3SDoug Rabson default: 889a3be63b3SDoug Rabson return (ENOENT); 890a3be63b3SDoug Rabson } 891cfa84f46SDoug Rabson 892a3be63b3SDoug Rabson return (0); 893a3be63b3SDoug Rabson } 894a3be63b3SDoug Rabson 8954249382dSDoug Rabson /* 8964249382dSDoug Rabson * Free any resources which the driver missed or which we were holding for 8974249382dSDoug Rabson * it (see isa_probe_children). 8984249382dSDoug Rabson */ 8994249382dSDoug Rabson static void 9004249382dSDoug Rabson isa_child_detached(device_t dev, device_t child) 9014249382dSDoug Rabson { 9024249382dSDoug Rabson struct isa_device* idev = DEVTOISA(child); 90325afb89bSDoug Rabson struct resource_list *rl = &idev->id_resources; 9044249382dSDoug Rabson struct resource_list_entry *rle; 9054249382dSDoug Rabson 906328d36e9SDoug Rabson if (TAILQ_FIRST(&idev->id_configs)) { 907328d36e9SDoug Rabson /* 908328d36e9SDoug Rabson * Claim any unallocated resources to keep other 909328d36e9SDoug Rabson * devices from using them. 910328d36e9SDoug Rabson */ 911328d36e9SDoug Rabson SLIST_FOREACH(rle, rl, link) { 912328d36e9SDoug Rabson if (!rle->res) { 913328d36e9SDoug Rabson int rid = rle->rid; 914328d36e9SDoug Rabson resource_list_alloc(rl, dev, child, 9154249382dSDoug Rabson rle->type, 916328d36e9SDoug Rabson &rid, 0, ~0, 1, 0); 917328d36e9SDoug Rabson } 918328d36e9SDoug Rabson } 9194249382dSDoug Rabson } 9204249382dSDoug Rabson } 9214249382dSDoug Rabson 922ec22663bSDoug Rabson static void 923ec22663bSDoug Rabson isa_driver_added(device_t dev, driver_t *driver) 924ec22663bSDoug Rabson { 925ec22663bSDoug Rabson device_t *children; 926ec22663bSDoug Rabson int nchildren, i; 927ec22663bSDoug Rabson 928ec22663bSDoug Rabson /* 929ec22663bSDoug Rabson * Don't do anything if drivers are dynamically 930ec22663bSDoug Rabson * added during autoconfiguration (cf. ymf724). 931ec22663bSDoug Rabson * since that would end up calling identify 932ec22663bSDoug Rabson * twice. 933ec22663bSDoug Rabson */ 934ec22663bSDoug Rabson if (!isa_running) 935ec22663bSDoug Rabson return; 936ec22663bSDoug Rabson 937ec22663bSDoug Rabson DEVICE_IDENTIFY(driver, dev); 938ec22663bSDoug Rabson if (device_get_children(dev, &children, &nchildren)) 939ec22663bSDoug Rabson return; 940ec22663bSDoug Rabson 941ec22663bSDoug Rabson for (i = 0; i < nchildren; i++) { 942ec22663bSDoug Rabson device_t child = children[i]; 943ec22663bSDoug Rabson struct isa_device *idev = DEVTOISA(child); 944ec22663bSDoug Rabson struct resource_list *rl = &idev->id_resources; 945ec22663bSDoug Rabson struct resource_list_entry *rle; 946ec22663bSDoug Rabson 947ec22663bSDoug Rabson if (device_get_state(child) != DS_NOTPRESENT) 948ec22663bSDoug Rabson continue; 949328d36e9SDoug Rabson if (!device_is_enabled(child)) 950328d36e9SDoug Rabson continue; 951328d36e9SDoug Rabson 952328d36e9SDoug Rabson /* 953328d36e9SDoug Rabson * Free resources which we were holding on behalf of 954328d36e9SDoug Rabson * the device. 955328d36e9SDoug Rabson */ 956328d36e9SDoug Rabson SLIST_FOREACH(rle, &idev->id_resources, link) { 957328d36e9SDoug Rabson if (rle->res) 958328d36e9SDoug Rabson resource_list_release(rl, dev, child, 959328d36e9SDoug Rabson rle->type, 960328d36e9SDoug Rabson rle->rid, 961328d36e9SDoug Rabson rle->res); 962328d36e9SDoug Rabson } 963ec22663bSDoug Rabson 964ec22663bSDoug Rabson if (TAILQ_FIRST(&idev->id_configs)) 965ec22663bSDoug Rabson if (!isa_assign_resources(child)) 966ec22663bSDoug Rabson continue; 967ec22663bSDoug Rabson 968ec22663bSDoug Rabson device_probe_and_attach(child); 969ec22663bSDoug Rabson 970ec22663bSDoug Rabson if (TAILQ_FIRST(&idev->id_configs)) { 971ec22663bSDoug Rabson /* 972ec22663bSDoug Rabson * Claim any unallocated resources to keep other 973ec22663bSDoug Rabson * devices from using them. 974ec22663bSDoug Rabson */ 975ec22663bSDoug Rabson SLIST_FOREACH(rle, rl, link) { 976ec22663bSDoug Rabson if (!rle->res) { 977ec22663bSDoug Rabson int rid = rle->rid; 978ec22663bSDoug Rabson resource_list_alloc(rl, dev, child, 979ec22663bSDoug Rabson rle->type, 980328d36e9SDoug Rabson &rid, 0, ~0, 1, 0); 981ec22663bSDoug Rabson } 982ec22663bSDoug Rabson } 983ec22663bSDoug Rabson } 984ec22663bSDoug Rabson } 985ec22663bSDoug Rabson 986ec22663bSDoug Rabson free(children, M_TEMP); 987ec22663bSDoug Rabson } 988ec22663bSDoug Rabson 989a3be63b3SDoug Rabson static int 990a3be63b3SDoug Rabson isa_set_resource(device_t dev, device_t child, int type, int rid, 991a3be63b3SDoug Rabson u_long start, u_long count) 992a3be63b3SDoug Rabson { 993a3be63b3SDoug Rabson struct isa_device* idev = DEVTOISA(child); 994a3be63b3SDoug Rabson struct resource_list *rl = &idev->id_resources; 995a3be63b3SDoug Rabson 996a3be63b3SDoug Rabson if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY 997a3be63b3SDoug Rabson && type != SYS_RES_IRQ && type != SYS_RES_DRQ) 998a3be63b3SDoug Rabson return EINVAL; 9994249382dSDoug Rabson if (rid < 0) 10004249382dSDoug Rabson return EINVAL; 10014249382dSDoug Rabson if (type == SYS_RES_IOPORT && rid >= ISA_NPORT) 10024249382dSDoug Rabson return EINVAL; 10034249382dSDoug Rabson if (type == SYS_RES_MEMORY && rid >= ISA_NMEM) 10044249382dSDoug Rabson return EINVAL; 10054249382dSDoug Rabson if (type == SYS_RES_IRQ && rid >= ISA_NIRQ) 10064249382dSDoug Rabson return EINVAL; 10074249382dSDoug Rabson if (type == SYS_RES_DRQ && rid >= ISA_NDRQ) 1008a3be63b3SDoug Rabson return EINVAL; 1009a3be63b3SDoug Rabson 1010a3be63b3SDoug Rabson resource_list_add(rl, type, rid, start, start + count - 1, count); 1011a3be63b3SDoug Rabson 1012a3be63b3SDoug Rabson return 0; 1013a3be63b3SDoug Rabson } 1014a3be63b3SDoug Rabson 10151581afb3SMatthew N. Dodd static struct resource_list * 10161581afb3SMatthew N. Dodd isa_get_resource_list (device_t dev, device_t child) 1017a3be63b3SDoug Rabson { 1018a3be63b3SDoug Rabson struct isa_device* idev = DEVTOISA(child); 1019a3be63b3SDoug Rabson struct resource_list *rl = &idev->id_resources; 1020a3be63b3SDoug Rabson 10211581afb3SMatthew N. Dodd if (!rl) 10221581afb3SMatthew N. Dodd return (NULL); 1023a3be63b3SDoug Rabson 10241581afb3SMatthew N. Dodd return (rl); 1025cfa84f46SDoug Rabson } 1026cfa84f46SDoug Rabson 10274249382dSDoug Rabson static int 10284249382dSDoug Rabson isa_add_config(device_t dev, device_t child, 10294249382dSDoug Rabson int priority, struct isa_config *config) 10304249382dSDoug Rabson { 10314249382dSDoug Rabson struct isa_device* idev = DEVTOISA(child); 10324249382dSDoug Rabson struct isa_config_entry *newice, *ice; 10334249382dSDoug Rabson 10344249382dSDoug Rabson newice = malloc(sizeof *ice, M_DEVBUF, M_NOWAIT); 10354249382dSDoug Rabson if (!newice) 10364249382dSDoug Rabson return ENOMEM; 10374249382dSDoug Rabson 10384249382dSDoug Rabson newice->ice_priority = priority; 10394249382dSDoug Rabson newice->ice_config = *config; 10404249382dSDoug Rabson 10414249382dSDoug Rabson TAILQ_FOREACH(ice, &idev->id_configs, ice_link) { 10424249382dSDoug Rabson if (ice->ice_priority > priority) 10434249382dSDoug Rabson break; 10444249382dSDoug Rabson } 10454249382dSDoug Rabson if (ice) 10464249382dSDoug Rabson TAILQ_INSERT_BEFORE(ice, newice, ice_link); 10474249382dSDoug Rabson else 10484249382dSDoug Rabson TAILQ_INSERT_TAIL(&idev->id_configs, newice, ice_link); 10494249382dSDoug Rabson 1050c3959391SKazutaka YOKOTA if (isa_has_single_config(child)) 1051c3959391SKazutaka YOKOTA idev->id_config_attr &= ~ISACFGATTR_MULTI; 1052c3959391SKazutaka YOKOTA else 1053c3959391SKazutaka YOKOTA idev->id_config_attr |= ISACFGATTR_MULTI; 1054c3959391SKazutaka YOKOTA 10554249382dSDoug Rabson return 0; 10564249382dSDoug Rabson } 10574249382dSDoug Rabson 10584249382dSDoug Rabson static void 10594249382dSDoug Rabson isa_set_config_callback(device_t dev, device_t child, 10604249382dSDoug Rabson isa_config_cb *fn, void *arg) 10614249382dSDoug Rabson { 10624249382dSDoug Rabson struct isa_device* idev = DEVTOISA(child); 10634249382dSDoug Rabson 10644249382dSDoug Rabson idev->id_config_cb = fn; 10654249382dSDoug Rabson idev->id_config_arg = arg; 10664249382dSDoug Rabson } 10674249382dSDoug Rabson 10684249382dSDoug Rabson static int 10694249382dSDoug Rabson isa_pnp_probe(device_t dev, device_t child, struct isa_pnp_id *ids) 10704249382dSDoug Rabson { 10714249382dSDoug Rabson struct isa_device* idev = DEVTOISA(child); 10724249382dSDoug Rabson 10734249382dSDoug Rabson if (!idev->id_vendorid) 10744249382dSDoug Rabson return ENOENT; 10754249382dSDoug Rabson 10762e5bbc3fSMike Smith while (ids && ids->ip_id) { 10774249382dSDoug Rabson /* 10784249382dSDoug Rabson * Really ought to support >1 compat id per device. 10794249382dSDoug Rabson */ 10804249382dSDoug Rabson if (idev->id_logicalid == ids->ip_id 10814249382dSDoug Rabson || idev->id_compatid == ids->ip_id) { 10822aadbbb6SDoug Rabson if (ids->ip_desc) 10834249382dSDoug Rabson device_set_desc(child, ids->ip_desc); 10844249382dSDoug Rabson return 0; 10854249382dSDoug Rabson } 10864249382dSDoug Rabson ids++; 10874249382dSDoug Rabson } 10884249382dSDoug Rabson 10894249382dSDoug Rabson return ENXIO; 10904249382dSDoug Rabson } 10914249382dSDoug Rabson 1092a3be63b3SDoug Rabson static device_method_t isa_methods[] = { 1093a3be63b3SDoug Rabson /* Device interface */ 1094a3be63b3SDoug Rabson DEVMETHOD(device_probe, isa_probe), 1095a3be63b3SDoug Rabson DEVMETHOD(device_attach, isa_attach), 1096a3be63b3SDoug Rabson DEVMETHOD(device_detach, bus_generic_detach), 1097a3be63b3SDoug Rabson DEVMETHOD(device_shutdown, bus_generic_shutdown), 1098a3be63b3SDoug Rabson DEVMETHOD(device_suspend, bus_generic_suspend), 1099a3be63b3SDoug Rabson DEVMETHOD(device_resume, bus_generic_resume), 1100a3be63b3SDoug Rabson 1101a3be63b3SDoug Rabson /* Bus interface */ 1102a3be63b3SDoug Rabson DEVMETHOD(bus_add_child, isa_add_child), 1103a3be63b3SDoug Rabson DEVMETHOD(bus_print_child, isa_print_child), 1104328d36e9SDoug Rabson DEVMETHOD(bus_probe_nomatch, isa_probe_nomatch), 1105a3be63b3SDoug Rabson DEVMETHOD(bus_read_ivar, isa_read_ivar), 1106a3be63b3SDoug Rabson DEVMETHOD(bus_write_ivar, isa_write_ivar), 11074249382dSDoug Rabson DEVMETHOD(bus_child_detached, isa_child_detached), 1108ec22663bSDoug Rabson DEVMETHOD(bus_driver_added, isa_driver_added), 1109a3be63b3SDoug Rabson DEVMETHOD(bus_setup_intr, isa_setup_intr), 1110a3be63b3SDoug Rabson DEVMETHOD(bus_teardown_intr, isa_teardown_intr), 11111581afb3SMatthew N. Dodd 11121581afb3SMatthew N. Dodd DEVMETHOD(bus_get_resource_list,isa_get_resource_list), 11131581afb3SMatthew N. Dodd DEVMETHOD(bus_alloc_resource, isa_alloc_resource), 11141581afb3SMatthew N. Dodd DEVMETHOD(bus_release_resource, isa_release_resource), 111525afb89bSDoug Rabson DEVMETHOD(bus_set_resource, isa_set_resource), 11161581afb3SMatthew N. Dodd DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 11171581afb3SMatthew N. Dodd DEVMETHOD(bus_delete_resource, bus_generic_rl_delete_resource), 11181581afb3SMatthew N. Dodd DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 11191581afb3SMatthew N. Dodd DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 1120a3be63b3SDoug Rabson 1121a3be63b3SDoug Rabson /* ISA interface */ 11224249382dSDoug Rabson DEVMETHOD(isa_add_config, isa_add_config), 11234249382dSDoug Rabson DEVMETHOD(isa_set_config_callback, isa_set_config_callback), 11244249382dSDoug Rabson DEVMETHOD(isa_pnp_probe, isa_pnp_probe), 1125a3be63b3SDoug Rabson 1126a3be63b3SDoug Rabson { 0, 0 } 1127a3be63b3SDoug Rabson }; 1128a3be63b3SDoug Rabson 1129a3be63b3SDoug Rabson static driver_t isa_driver = { 1130a3be63b3SDoug Rabson "isa", 1131a3be63b3SDoug Rabson isa_methods, 1132a3be63b3SDoug Rabson 1, /* no softc */ 1133a3be63b3SDoug Rabson }; 1134a3be63b3SDoug Rabson 1135a3be63b3SDoug Rabson /* 1136a3be63b3SDoug Rabson * ISA can be attached to a PCI-ISA bridge or directly to the nexus. 1137a3be63b3SDoug Rabson */ 1138a3be63b3SDoug Rabson DRIVER_MODULE(isa, isab, isa_driver, isa_devclass, 0, 0); 11395aea03b0SMike Smith DRIVER_MODULE(isa, eisab, isa_driver, isa_devclass, 0, 0); 1140a3be63b3SDoug Rabson #ifdef __i386__ 1141a3be63b3SDoug Rabson DRIVER_MODULE(isa, nexus, isa_driver, isa_devclass, 0, 0); 1142a3be63b3SDoug Rabson #endif 1143