1a3be63b3SDoug Rabson /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause AND MIT
3fe267a55SPedro F. Giffuni *
4a3be63b3SDoug Rabson * Copyright (c) 1999 Doug Rabson
5a3be63b3SDoug Rabson * All rights reserved.
6a3be63b3SDoug Rabson *
7a3be63b3SDoug Rabson * Redistribution and use in source and binary forms, with or without
8a3be63b3SDoug Rabson * modification, are permitted provided that the following conditions
9a3be63b3SDoug Rabson * are met:
10a3be63b3SDoug Rabson * 1. Redistributions of source code must retain the above copyright
11a3be63b3SDoug Rabson * notice, this list of conditions and the following disclaimer.
12a3be63b3SDoug Rabson * 2. Redistributions in binary form must reproduce the above copyright
13a3be63b3SDoug Rabson * notice, this list of conditions and the following disclaimer in the
14a3be63b3SDoug Rabson * documentation and/or other materials provided with the distribution.
15a3be63b3SDoug Rabson *
16a3be63b3SDoug Rabson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17a3be63b3SDoug Rabson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18a3be63b3SDoug Rabson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19a3be63b3SDoug Rabson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20a3be63b3SDoug Rabson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21a3be63b3SDoug Rabson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22a3be63b3SDoug Rabson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23a3be63b3SDoug Rabson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24a3be63b3SDoug Rabson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25a3be63b3SDoug Rabson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26a3be63b3SDoug Rabson * SUCH DAMAGE.
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
628c9bbf48SDavid E. O'Brien #include <sys/cdefs.h>
63acb8c149SMarius Strobl #include "opt_isa.h"
64acb8c149SMarius Strobl
65a3be63b3SDoug Rabson #include <sys/param.h>
66a3be63b3SDoug Rabson #include <sys/systm.h>
67a3be63b3SDoug Rabson #include <sys/kernel.h>
68a3be63b3SDoug Rabson #include <sys/bus.h>
697dcb3b12SWarner Losh #include <sys/endian.h>
70a3be63b3SDoug Rabson #include <sys/malloc.h>
71a3be63b3SDoug Rabson #include <sys/module.h>
72a3be63b3SDoug Rabson #include <machine/bus.h>
73a3be63b3SDoug Rabson #include <sys/rman.h>
74ddfc9c4cSWarner Losh #include <sys/sbuf.h>
757dcb3b12SWarner Losh #include <sys/sysctl.h>
76a3be63b3SDoug Rabson
77a3be63b3SDoug Rabson #include <machine/resource.h>
78a3be63b3SDoug Rabson
79a3be63b3SDoug Rabson #include <isa/isavar.h>
80a3be63b3SDoug Rabson #include <isa/isa_common.h>
81a3be63b3SDoug Rabson
822b2b44c9SJohn Baldwin static int isa_print_child(device_t bus, device_t dev);
832b2b44c9SJohn Baldwin
84959b7375SPoul-Henning Kamp static MALLOC_DEFINE(M_ISADEV, "isadev", "ISA device");
85a3be63b3SDoug Rabson
86ec22663bSDoug Rabson static int isa_running;
87a3be63b3SDoug Rabson
88a3be63b3SDoug Rabson /*
89a3be63b3SDoug Rabson * At 'probe' time, we add all the devices which we know about to the
90a3be63b3SDoug Rabson * bus. The generic attach routine will probe and attach them if they
91a3be63b3SDoug Rabson * are alive.
92a3be63b3SDoug Rabson */
93a3be63b3SDoug Rabson static int
isa_probe(device_t dev)94a3be63b3SDoug Rabson isa_probe(device_t dev)
95a3be63b3SDoug Rabson {
96a3be63b3SDoug Rabson device_set_desc(dev, "ISA bus");
9701f1aed2SThomas Moestl isa_init(dev); /* Allow machdep code to initialise */
9809aafea1SWarner Losh return (0);
99a3be63b3SDoug Rabson }
100a3be63b3SDoug Rabson
101a3be63b3SDoug Rabson extern device_t isa_bus_device;
102a3be63b3SDoug Rabson
103a3be63b3SDoug Rabson static int
isa_attach(device_t dev)104a3be63b3SDoug Rabson isa_attach(device_t dev)
105a3be63b3SDoug Rabson {
106a3be63b3SDoug Rabson /*
1074249382dSDoug Rabson * Arrange for isa_probe_children(dev) to be called later. XXX
108a3be63b3SDoug Rabson */
109a3be63b3SDoug Rabson isa_bus_device = dev;
11009aafea1SWarner Losh return (0);
111a3be63b3SDoug Rabson }
112a3be63b3SDoug Rabson
113a3be63b3SDoug Rabson /*
1144249382dSDoug Rabson * Find a working set of memory regions for a child using the ranges
1154249382dSDoug Rabson * in *config and return the regions in *result. Returns non-zero if
1164249382dSDoug Rabson * a set of ranges was found.
1174249382dSDoug Rabson */
1184249382dSDoug Rabson static int
isa_find_memory(device_t child,struct isa_config * config,struct isa_config * result)11909aafea1SWarner Losh isa_find_memory(device_t child, 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++) {
13609aafea1SWarner Losh uint32_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;
152784d07b4SWarner Losh start + size - 1 <= end && start + size > start;
15390dea4f9SJohn Baldwin start += MAX(align, 1)) {
15425afb89bSDoug Rabson bus_set_resource(child, SYS_RES_MEMORY, i,
1554249382dSDoug Rabson start, size);
15643cd6160SJustin Hibbits res[i] = bus_alloc_resource_any(child,
15743cd6160SJustin Hibbits SYS_RES_MEMORY, &i,
1589b01161eSWarner Losh rman_make_alignment_flags(align) /* !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
18409aafea1SWarner Losh 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
isa_find_port(device_t child,struct isa_config * config,struct isa_config * result)19309aafea1SWarner Losh isa_find_port(device_t child, 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++) {
21009aafea1SWarner Losh uint32_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);
23043cd6160SJustin Hibbits res[i] = bus_alloc_resource_any(child,
23143cd6160SJustin Hibbits SYS_RES_IOPORT, &i,
2329b01161eSWarner Losh rman_make_alignment_flags(align) /* !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
find_first_bit(uint32_t mask)26509aafea1SWarner Losh find_first_bit(uint32_t mask)
2664249382dSDoug Rabson {
26709aafea1SWarner Losh 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
find_next_bit(uint32_t mask,int bit)27409aafea1SWarner Losh find_next_bit(uint32_t mask, int bit)
2754249382dSDoug Rabson {
276c2cbd7ffSDoug Moore return (find_first_bit(mask & (-2 << bit)));
2774249382dSDoug Rabson }
2784249382dSDoug Rabson
2794249382dSDoug Rabson /*
2804249382dSDoug Rabson * Find a working set of irqs for a child using the masks in *config
2814249382dSDoug Rabson * and return the regions in *result. Returns non-zero if a set of
2824249382dSDoug Rabson * irqs was found.
2834249382dSDoug Rabson */
2844249382dSDoug Rabson static int
isa_find_irq(device_t child,struct isa_config * config,struct isa_config * result)28509aafea1SWarner Losh isa_find_irq(device_t child, struct isa_config *config,
2864249382dSDoug Rabson struct isa_config *result)
2874249382dSDoug Rabson {
2884249382dSDoug Rabson int success, i;
2894249382dSDoug Rabson struct resource *res[ISA_NIRQ];
2904249382dSDoug Rabson
2914249382dSDoug Rabson /*
2924249382dSDoug Rabson * First clear out any existing resource definitions.
2934249382dSDoug Rabson */
2944249382dSDoug Rabson for (i = 0; i < ISA_NIRQ; i++) {
29525afb89bSDoug Rabson bus_delete_resource(child, SYS_RES_IRQ, i);
2964249382dSDoug Rabson res[i] = NULL;
2974249382dSDoug Rabson }
2984249382dSDoug Rabson
2994249382dSDoug Rabson success = 1;
3004249382dSDoug Rabson result->ic_nirq = config->ic_nirq;
3014249382dSDoug Rabson for (i = 0; i < config->ic_nirq; i++) {
30209aafea1SWarner Losh uint32_t mask = config->ic_irqmask[i];
3034249382dSDoug Rabson int irq;
304c3959391SKazutaka YOKOTA
305c3959391SKazutaka YOKOTA /* the PnP device may have a null resource as filler */
306c3959391SKazutaka YOKOTA if (mask == 0) {
307c3959391SKazutaka YOKOTA result->ic_irqmask[i] = 0;
308c3959391SKazutaka YOKOTA continue;
309c3959391SKazutaka YOKOTA }
310c3959391SKazutaka YOKOTA
3114249382dSDoug Rabson for (irq = find_first_bit(mask);
3124249382dSDoug Rabson irq != -1;
3134249382dSDoug Rabson irq = find_next_bit(mask, irq)) {
31425afb89bSDoug Rabson bus_set_resource(child, SYS_RES_IRQ, i,
3154249382dSDoug Rabson irq, 1);
3165f96beb9SNate Lawson res[i] = bus_alloc_resource_any(child,
3174249382dSDoug Rabson SYS_RES_IRQ, &i,
3185f96beb9SNate Lawson 0 /* !RF_ACTIVE */ );
3194249382dSDoug Rabson if (res[i]) {
3204249382dSDoug Rabson result->ic_irqmask[i] = (1 << irq);
3214249382dSDoug Rabson break;
3224249382dSDoug Rabson }
3234249382dSDoug Rabson }
3244249382dSDoug Rabson
3254249382dSDoug Rabson /*
3264249382dSDoug Rabson * If we didn't find a place for irq range i, then
3274249382dSDoug Rabson * give up now.
3284249382dSDoug Rabson */
3294249382dSDoug Rabson if (!res[i]) {
3304249382dSDoug Rabson success = 0;
3314249382dSDoug Rabson break;
3324249382dSDoug Rabson }
3334249382dSDoug Rabson }
3344249382dSDoug Rabson
3354249382dSDoug Rabson for (i = 0; i < ISA_NIRQ; i++) {
3364249382dSDoug Rabson if (res[i])
3374249382dSDoug Rabson bus_release_resource(child, SYS_RES_IRQ,
3384249382dSDoug Rabson i, res[i]);
3394249382dSDoug Rabson }
3404249382dSDoug Rabson
34109aafea1SWarner Losh return (success);
3424249382dSDoug Rabson }
3434249382dSDoug Rabson
3444249382dSDoug Rabson /*
3454249382dSDoug Rabson * Find a working set of drqs for a child using the masks in *config
3464249382dSDoug Rabson * and return the regions in *result. Returns non-zero if a set of
3474249382dSDoug Rabson * drqs was found.
3484249382dSDoug Rabson */
3494249382dSDoug Rabson static int
isa_find_drq(device_t child,struct isa_config * config,struct isa_config * result)35009aafea1SWarner Losh isa_find_drq(device_t child, struct isa_config *config,
3514249382dSDoug Rabson struct isa_config *result)
3524249382dSDoug Rabson {
3534249382dSDoug Rabson int success, i;
3544249382dSDoug Rabson struct resource *res[ISA_NDRQ];
3554249382dSDoug Rabson
3564249382dSDoug Rabson /*
3574249382dSDoug Rabson * First clear out any existing resource definitions.
3584249382dSDoug Rabson */
3594249382dSDoug Rabson for (i = 0; i < ISA_NDRQ; i++) {
36025afb89bSDoug Rabson bus_delete_resource(child, SYS_RES_DRQ, i);
3614249382dSDoug Rabson res[i] = NULL;
3624249382dSDoug Rabson }
3634249382dSDoug Rabson
3644249382dSDoug Rabson success = 1;
3654249382dSDoug Rabson result->ic_ndrq = config->ic_ndrq;
3664249382dSDoug Rabson for (i = 0; i < config->ic_ndrq; i++) {
36709aafea1SWarner Losh uint32_t mask = config->ic_drqmask[i];
3684249382dSDoug Rabson int drq;
369c3959391SKazutaka YOKOTA
370c3959391SKazutaka YOKOTA /* the PnP device may have a null resource as filler */
371c3959391SKazutaka YOKOTA if (mask == 0) {
372c3959391SKazutaka YOKOTA result->ic_drqmask[i] = 0;
373c3959391SKazutaka YOKOTA continue;
374c3959391SKazutaka YOKOTA }
375c3959391SKazutaka YOKOTA
3764249382dSDoug Rabson for (drq = find_first_bit(mask);
3774249382dSDoug Rabson drq != -1;
3784249382dSDoug Rabson drq = find_next_bit(mask, drq)) {
37925afb89bSDoug Rabson bus_set_resource(child, SYS_RES_DRQ, i,
3804249382dSDoug Rabson drq, 1);
3815f96beb9SNate Lawson res[i] = bus_alloc_resource_any(child,
3824249382dSDoug Rabson SYS_RES_DRQ, &i,
3835f96beb9SNate Lawson 0 /* !RF_ACTIVE */);
3844249382dSDoug Rabson if (res[i]) {
3854249382dSDoug Rabson result->ic_drqmask[i] = (1 << drq);
3864249382dSDoug Rabson break;
3874249382dSDoug Rabson }
3884249382dSDoug Rabson }
3894249382dSDoug Rabson
3904249382dSDoug Rabson /*
3914249382dSDoug Rabson * If we didn't find a place for drq range i, then
3924249382dSDoug Rabson * give up now.
3934249382dSDoug Rabson */
3944249382dSDoug Rabson if (!res[i]) {
3954249382dSDoug Rabson success = 0;
3964249382dSDoug Rabson break;
3974249382dSDoug Rabson }
3984249382dSDoug Rabson }
3994249382dSDoug Rabson
4004249382dSDoug Rabson for (i = 0; i < ISA_NDRQ; i++) {
4014249382dSDoug Rabson if (res[i])
4024249382dSDoug Rabson bus_release_resource(child, SYS_RES_DRQ,
4034249382dSDoug Rabson i, res[i]);
4044249382dSDoug Rabson }
4054249382dSDoug Rabson
40609aafea1SWarner Losh return (success);
4074249382dSDoug Rabson }
4084249382dSDoug Rabson
4094249382dSDoug Rabson /*
4104249382dSDoug Rabson * Attempt to find a working set of resources for a device. Return
4114249382dSDoug Rabson * non-zero if a working configuration is found.
4124249382dSDoug Rabson */
4134249382dSDoug Rabson static int
isa_assign_resources(device_t child)4144249382dSDoug Rabson isa_assign_resources(device_t child)
4154249382dSDoug Rabson {
4164249382dSDoug Rabson struct isa_device *idev = DEVTOISA(child);
4174249382dSDoug Rabson struct isa_config_entry *ice;
4183b82ede0SJulian Elischer struct isa_config *cfg;
4190077e822SAlfred Perlstein const char *reason;
4204249382dSDoug Rabson
4210077e822SAlfred Perlstein reason = "Empty ISA id_configs";
4223b82ede0SJulian Elischer cfg = malloc(sizeof(struct isa_config), M_TEMP, M_NOWAIT|M_ZERO);
4233b82ede0SJulian Elischer if (cfg == NULL)
4243b82ede0SJulian Elischer return(0);
4254249382dSDoug Rabson TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
426b2e4739fSAlfred Perlstein reason = "memory";
4273b82ede0SJulian Elischer if (!isa_find_memory(child, &ice->ice_config, cfg))
4284249382dSDoug Rabson continue;
429b2e4739fSAlfred Perlstein reason = "port";
4303b82ede0SJulian Elischer if (!isa_find_port(child, &ice->ice_config, cfg))
4314249382dSDoug Rabson continue;
432b2e4739fSAlfred Perlstein reason = "irq";
4333b82ede0SJulian Elischer if (!isa_find_irq(child, &ice->ice_config, cfg))
4344249382dSDoug Rabson continue;
435b2e4739fSAlfred Perlstein reason = "drq";
4363b82ede0SJulian Elischer if (!isa_find_drq(child, &ice->ice_config, cfg))
4374249382dSDoug Rabson continue;
4384249382dSDoug Rabson
4394249382dSDoug Rabson /*
4404249382dSDoug Rabson * A working configuration was found enable the device
4414249382dSDoug Rabson * with this configuration.
4424249382dSDoug Rabson */
443b2e4739fSAlfred Perlstein reason = "no callback";
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);
44809aafea1SWarner Losh 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);
456b2e4739fSAlfred Perlstein printf(" can't assign resources (%s)\n", reason);
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);
46509aafea1SWarner Losh return (0);
4664249382dSDoug Rabson }
4674249382dSDoug Rabson
4680d484d24SJohn Baldwin /*
4690d484d24SJohn Baldwin * Claim any unallocated resources to keep other devices from using
4700d484d24SJohn Baldwin * them.
4710d484d24SJohn Baldwin */
4720d484d24SJohn Baldwin static void
isa_claim_resources(device_t dev,device_t child)4730d484d24SJohn Baldwin isa_claim_resources(device_t dev, device_t child)
4740d484d24SJohn Baldwin {
4750d484d24SJohn Baldwin struct isa_device *idev = DEVTOISA(child);
4760d484d24SJohn Baldwin struct resource_list *rl = &idev->id_resources;
4770d484d24SJohn Baldwin struct resource_list_entry *rle;
4780d484d24SJohn Baldwin int rid;
4790d484d24SJohn Baldwin
4800d484d24SJohn Baldwin STAILQ_FOREACH(rle, rl, link) {
4810d484d24SJohn Baldwin if (!rle->res) {
4820d484d24SJohn Baldwin rid = rle->rid;
4830d484d24SJohn Baldwin resource_list_alloc(rl, dev, child, rle->type, &rid,
484534ccd7bSJustin Hibbits 0, ~0, 1, 0);
4850d484d24SJohn Baldwin }
4860d484d24SJohn Baldwin }
4870d484d24SJohn Baldwin }
4880d484d24SJohn Baldwin
4890d484d24SJohn Baldwin /*
4900d484d24SJohn Baldwin * Called after other devices have initialised to probe for isa devices.
4910d484d24SJohn Baldwin */
4924249382dSDoug Rabson void
isa_probe_children(device_t dev)4934249382dSDoug Rabson isa_probe_children(device_t dev)
4944249382dSDoug Rabson {
4950d484d24SJohn Baldwin struct isa_device *idev;
4960d484d24SJohn Baldwin device_t *children, child;
4973b82ede0SJulian Elischer struct isa_config *cfg;
4987dcb3b12SWarner Losh int nchildren, i, err;
4994249382dSDoug Rabson
500ec22663bSDoug Rabson /*
5010d484d24SJohn Baldwin * Create all the non-hinted children by calling drivers'
5020d484d24SJohn Baldwin * identify methods.
503ec22663bSDoug Rabson */
504ec22663bSDoug Rabson bus_generic_probe(dev);
505ec22663bSDoug Rabson
5064249382dSDoug Rabson if (device_get_children(dev, &children, &nchildren))
5074249382dSDoug Rabson return;
5084249382dSDoug Rabson
5094249382dSDoug Rabson /*
510a2e6dbb3SDoug Rabson * First disable all pnp devices so that they don't get
511a2e6dbb3SDoug Rabson * matched by legacy probes.
512a2e6dbb3SDoug Rabson */
513ec22663bSDoug Rabson if (bootverbose)
514ec22663bSDoug Rabson printf("isa_probe_children: disabling PnP devices\n");
5153b82ede0SJulian Elischer
5163b82ede0SJulian Elischer cfg = malloc(sizeof(*cfg), M_TEMP, M_NOWAIT|M_ZERO);
5173b82ede0SJulian Elischer if (cfg == NULL) {
5183b82ede0SJulian Elischer free(children, M_TEMP);
5193b82ede0SJulian Elischer return;
5203b82ede0SJulian Elischer }
5213b82ede0SJulian Elischer
522a2e6dbb3SDoug Rabson for (i = 0; i < nchildren; i++) {
5230d484d24SJohn Baldwin idev = DEVTOISA(children[i]);
524a2e6dbb3SDoug Rabson
5253b82ede0SJulian Elischer bzero(cfg, sizeof(*cfg));
526a2e6dbb3SDoug Rabson if (idev->id_config_cb)
5273b82ede0SJulian Elischer idev->id_config_cb(idev->id_config_arg, cfg, 0);
528a2e6dbb3SDoug Rabson }
529a2e6dbb3SDoug Rabson
5303b82ede0SJulian Elischer free(cfg, M_TEMP);
5313b82ede0SJulian Elischer
532a2e6dbb3SDoug Rabson /*
5330d484d24SJohn Baldwin * Next, probe all the PnP BIOS devices so they can subsume any
5340d484d24SJohn Baldwin * hints.
5354249382dSDoug Rabson */
5360d484d24SJohn Baldwin for (i = 0; i < nchildren; i++) {
5370d484d24SJohn Baldwin child = children[i];
5380d484d24SJohn Baldwin idev = DEVTOISA(child);
5390d484d24SJohn Baldwin
5400d484d24SJohn Baldwin if (idev->id_order > ISA_ORDER_PNPBIOS)
5410d484d24SJohn Baldwin continue;
5420d484d24SJohn Baldwin if (!TAILQ_EMPTY(&idev->id_configs) &&
5430d484d24SJohn Baldwin !isa_assign_resources(child))
5440d484d24SJohn Baldwin continue;
5450d484d24SJohn Baldwin
5460d484d24SJohn Baldwin if (device_probe_and_attach(child) == 0)
5470d484d24SJohn Baldwin isa_claim_resources(dev, child);
5480d484d24SJohn Baldwin }
5490d484d24SJohn Baldwin free(children, M_TEMP);
5500d484d24SJohn Baldwin
5510d484d24SJohn Baldwin /*
5520d484d24SJohn Baldwin * Next, enumerate hinted devices and probe all non-pnp devices so
5530d484d24SJohn Baldwin * that they claim their resources first.
5540d484d24SJohn Baldwin */
5550d484d24SJohn Baldwin bus_enumerate_hinted_children(dev);
5560d484d24SJohn Baldwin if (device_get_children(dev, &children, &nchildren))
5570d484d24SJohn Baldwin return;
558ec22663bSDoug Rabson if (bootverbose)
559ec22663bSDoug Rabson printf("isa_probe_children: probing non-PnP devices\n");
5604249382dSDoug Rabson for (i = 0; i < nchildren; i++) {
5610d484d24SJohn Baldwin child = children[i];
5620d484d24SJohn Baldwin idev = DEVTOISA(child);
5634249382dSDoug Rabson
5640d484d24SJohn Baldwin if (device_is_attached(child) ||
5650d484d24SJohn Baldwin !TAILQ_EMPTY(&idev->id_configs))
5664249382dSDoug Rabson continue;
5674249382dSDoug Rabson
5687dcb3b12SWarner Losh err = device_probe_and_attach(child);
5697dcb3b12SWarner Losh if (err == 0 && idev->id_vendorid == 0 &&
5709e1472caSWarner Losh strcmp(kern_ident, "GENERIC") == 0 &&
5719e1472caSWarner Losh device_is_attached(child))
5727dcb3b12SWarner Losh device_printf(child,
57398a670cdSZhenlei Huang "non-PNP ISA device will be removed from GENERIC in FreeBSD 15.\n");
5744249382dSDoug Rabson }
5754249382dSDoug Rabson
5764249382dSDoug Rabson /*
577a2e6dbb3SDoug Rabson * Finally assign resource to pnp devices and probe them.
5784249382dSDoug Rabson */
579ec22663bSDoug Rabson if (bootverbose)
580ec22663bSDoug Rabson printf("isa_probe_children: probing PnP devices\n");
5814249382dSDoug Rabson for (i = 0; i < nchildren; i++) {
5820d484d24SJohn Baldwin child = children[i];
5830d484d24SJohn Baldwin idev = DEVTOISA(child);
5844249382dSDoug Rabson
5850d484d24SJohn Baldwin if (device_is_attached(child) || TAILQ_EMPTY(&idev->id_configs))
5864249382dSDoug Rabson continue;
5874249382dSDoug Rabson
5884249382dSDoug Rabson if (isa_assign_resources(child)) {
5894249382dSDoug Rabson device_probe_and_attach(child);
5900d484d24SJohn Baldwin isa_claim_resources(dev, child);
5914249382dSDoug Rabson }
5924249382dSDoug Rabson }
5934249382dSDoug Rabson
5944249382dSDoug Rabson free(children, M_TEMP);
595ec22663bSDoug Rabson
596ec22663bSDoug Rabson isa_running = 1;
5974249382dSDoug Rabson }
5984249382dSDoug Rabson
5994249382dSDoug Rabson /*
600a3be63b3SDoug Rabson * Add a new child with default ivars.
601a3be63b3SDoug Rabson */
602a3be63b3SDoug Rabson static device_t
isa_add_child(device_t dev,u_int order,const char * name,int unit)6033d844eddSAndriy Gapon isa_add_child(device_t dev, u_int order, const char *name, int unit)
604a3be63b3SDoug Rabson {
605fe0d4089SMatthew N. Dodd device_t child;
606a3be63b3SDoug Rabson struct isa_device *idev;
607a3be63b3SDoug Rabson
608f46708e0SPoul-Henning Kamp child = device_add_child_ordered(dev, order, name, unit);
609f46708e0SPoul-Henning Kamp if (child == NULL)
610f46708e0SPoul-Henning Kamp return (child);
611f46708e0SPoul-Henning Kamp
6127cc0979fSDavid Malone idev = malloc(sizeof(struct isa_device), M_ISADEV, M_NOWAIT | M_ZERO);
613a3be63b3SDoug Rabson if (!idev)
61409aafea1SWarner Losh return (0);
615a3be63b3SDoug Rabson
616a3be63b3SDoug Rabson resource_list_init(&idev->id_resources);
6174249382dSDoug Rabson TAILQ_INIT(&idev->id_configs);
6180d484d24SJohn Baldwin idev->id_order = order;
619a3be63b3SDoug Rabson
620fe0d4089SMatthew N. Dodd device_set_ivars(child, idev);
621fe0d4089SMatthew N. Dodd
622f46708e0SPoul-Henning Kamp return (child);
623a3be63b3SDoug Rabson }
624a3be63b3SDoug Rabson
625*c8fa5488SJohn Baldwin static void
isa_child_deleted(device_t dev,device_t child)626*c8fa5488SJohn Baldwin isa_child_deleted(device_t dev, device_t child)
627*c8fa5488SJohn Baldwin {
628*c8fa5488SJohn Baldwin free(device_get_ivars(child), M_ISADEV);
629*c8fa5488SJohn Baldwin }
630*c8fa5488SJohn Baldwin
631328d36e9SDoug Rabson static int
isa_print_all_resources(device_t dev)632328d36e9SDoug Rabson isa_print_all_resources(device_t dev)
633a3be63b3SDoug Rabson {
634a3be63b3SDoug Rabson struct isa_device *idev = DEVTOISA(dev);
635a3be63b3SDoug Rabson struct resource_list *rl = &idev->id_resources;
63615317dd8SMatthew N. Dodd int retval = 0;
63715317dd8SMatthew N. Dodd
63836fed965SWarner Losh if (STAILQ_FIRST(rl) || device_get_flags(dev))
63915317dd8SMatthew N. Dodd retval += printf(" at");
640a3be63b3SDoug Rabson
641f8fd3fb5SJustin Hibbits retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#jx");
642f8fd3fb5SJustin Hibbits retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#jx");
643f8fd3fb5SJustin Hibbits retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
644f8fd3fb5SJustin Hibbits retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%jd");
645062acdb7SDoug Rabson if (device_get_flags(dev))
646062acdb7SDoug Rabson retval += printf(" flags %#x", device_get_flags(dev));
647a81804d9SWarner Losh if (idev->id_vendorid)
648a81804d9SWarner Losh retval += printf(" pnpid %s", pnp_eisaformat(idev->id_vendorid));
649a3be63b3SDoug Rabson
65009aafea1SWarner Losh return (retval);
651328d36e9SDoug Rabson }
652328d36e9SDoug Rabson
653328d36e9SDoug Rabson static int
isa_print_child(device_t bus,device_t dev)654328d36e9SDoug Rabson isa_print_child(device_t bus, device_t dev)
655328d36e9SDoug Rabson {
656328d36e9SDoug Rabson int retval = 0;
657328d36e9SDoug Rabson
658328d36e9SDoug Rabson retval += bus_print_child_header(bus, dev);
659328d36e9SDoug Rabson retval += isa_print_all_resources(dev);
66015317dd8SMatthew N. Dodd retval += bus_print_child_footer(bus, dev);
66115317dd8SMatthew N. Dodd
66215317dd8SMatthew N. Dodd return (retval);
663a3be63b3SDoug Rabson }
664a3be63b3SDoug Rabson
665328d36e9SDoug Rabson static void
isa_probe_nomatch(device_t dev,device_t child)666328d36e9SDoug Rabson isa_probe_nomatch(device_t dev, device_t child)
667328d36e9SDoug Rabson {
6682b2b44c9SJohn Baldwin if (bootverbose) {
6692b2b44c9SJohn Baldwin bus_print_child_header(dev, child);
6702b2b44c9SJohn Baldwin printf(" failed to probe");
671328d36e9SDoug Rabson isa_print_all_resources(child);
6722b2b44c9SJohn Baldwin bus_print_child_footer(dev, child);
6732b2b44c9SJohn Baldwin }
674328d36e9SDoug Rabson
675328d36e9SDoug Rabson return;
676328d36e9SDoug Rabson }
677328d36e9SDoug Rabson
678a3be63b3SDoug Rabson static int
isa_read_ivar(device_t bus,device_t dev,int index,uintptr_t * result)679a3be63b3SDoug Rabson isa_read_ivar(device_t bus, device_t dev, int index, uintptr_t * result)
680a3be63b3SDoug Rabson {
681a3be63b3SDoug Rabson struct isa_device* idev = DEVTOISA(dev);
682a3be63b3SDoug Rabson struct resource_list *rl = &idev->id_resources;
683a3be63b3SDoug Rabson struct resource_list_entry *rle;
684a3be63b3SDoug Rabson
685a3be63b3SDoug Rabson switch (index) {
686a3be63b3SDoug Rabson case ISA_IVAR_PORT_0:
687a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
688a3be63b3SDoug Rabson if (rle)
689a3be63b3SDoug Rabson *result = rle->start;
690a3be63b3SDoug Rabson else
691a3be63b3SDoug Rabson *result = -1;
692a3be63b3SDoug Rabson break;
693a3be63b3SDoug Rabson
694a3be63b3SDoug Rabson case ISA_IVAR_PORT_1:
695a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
696a3be63b3SDoug Rabson if (rle)
697a3be63b3SDoug Rabson *result = rle->start;
698a3be63b3SDoug Rabson else
699a3be63b3SDoug Rabson *result = -1;
700a3be63b3SDoug Rabson break;
701a3be63b3SDoug Rabson
702a3be63b3SDoug Rabson case ISA_IVAR_PORTSIZE_0:
703a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
704a3be63b3SDoug Rabson if (rle)
705a3be63b3SDoug Rabson *result = rle->count;
706a3be63b3SDoug Rabson else
707a3be63b3SDoug Rabson *result = 0;
708a3be63b3SDoug Rabson break;
709a3be63b3SDoug Rabson
710a3be63b3SDoug Rabson case ISA_IVAR_PORTSIZE_1:
711a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
712a3be63b3SDoug Rabson if (rle)
713a3be63b3SDoug Rabson *result = rle->count;
714a3be63b3SDoug Rabson else
715a3be63b3SDoug Rabson *result = 0;
716a3be63b3SDoug Rabson break;
717a3be63b3SDoug Rabson
718a3be63b3SDoug Rabson case ISA_IVAR_MADDR_0:
719a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
720a3be63b3SDoug Rabson if (rle)
721a3be63b3SDoug Rabson *result = rle->start;
722a3be63b3SDoug Rabson else
723a3be63b3SDoug Rabson *result = -1;
724a3be63b3SDoug Rabson break;
725a3be63b3SDoug Rabson
726a3be63b3SDoug Rabson case ISA_IVAR_MADDR_1:
727a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
728a3be63b3SDoug Rabson if (rle)
729a3be63b3SDoug Rabson *result = rle->start;
730a3be63b3SDoug Rabson else
731a3be63b3SDoug Rabson *result = -1;
732a3be63b3SDoug Rabson break;
733a3be63b3SDoug Rabson
73463e30378SJohn Baldwin case ISA_IVAR_MEMSIZE_0:
735a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
736a3be63b3SDoug Rabson if (rle)
737a3be63b3SDoug Rabson *result = rle->count;
738a3be63b3SDoug Rabson else
739a3be63b3SDoug Rabson *result = 0;
740a3be63b3SDoug Rabson break;
741a3be63b3SDoug Rabson
74263e30378SJohn Baldwin case ISA_IVAR_MEMSIZE_1:
743a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
744a3be63b3SDoug Rabson if (rle)
745a3be63b3SDoug Rabson *result = rle->count;
746a3be63b3SDoug Rabson else
747a3be63b3SDoug Rabson *result = 0;
748a3be63b3SDoug Rabson break;
749a3be63b3SDoug Rabson
750a3be63b3SDoug Rabson case ISA_IVAR_IRQ_0:
751a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_IRQ, 0);
752a3be63b3SDoug Rabson if (rle)
753a3be63b3SDoug Rabson *result = rle->start;
754a3be63b3SDoug Rabson else
755a3be63b3SDoug Rabson *result = -1;
756a3be63b3SDoug Rabson break;
757a3be63b3SDoug Rabson
758a3be63b3SDoug Rabson case ISA_IVAR_IRQ_1:
759a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_IRQ, 1);
760a3be63b3SDoug Rabson if (rle)
761a3be63b3SDoug Rabson *result = rle->start;
762a3be63b3SDoug Rabson else
763a3be63b3SDoug Rabson *result = -1;
764a3be63b3SDoug Rabson break;
765a3be63b3SDoug Rabson
766a3be63b3SDoug Rabson case ISA_IVAR_DRQ_0:
767a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_DRQ, 0);
768a3be63b3SDoug Rabson if (rle)
769a3be63b3SDoug Rabson *result = rle->start;
770a3be63b3SDoug Rabson else
771a3be63b3SDoug Rabson *result = -1;
772a3be63b3SDoug Rabson break;
773a3be63b3SDoug Rabson
774a3be63b3SDoug Rabson case ISA_IVAR_DRQ_1:
775a3be63b3SDoug Rabson rle = resource_list_find(rl, SYS_RES_DRQ, 1);
776a3be63b3SDoug Rabson if (rle)
777a3be63b3SDoug Rabson *result = rle->start;
778a3be63b3SDoug Rabson else
779a3be63b3SDoug Rabson *result = -1;
780a3be63b3SDoug Rabson break;
781a3be63b3SDoug Rabson
782cfa84f46SDoug Rabson case ISA_IVAR_VENDORID:
783cfa84f46SDoug Rabson *result = idev->id_vendorid;
784cfa84f46SDoug Rabson break;
785cfa84f46SDoug Rabson
786cfa84f46SDoug Rabson case ISA_IVAR_SERIAL:
787cfa84f46SDoug Rabson *result = idev->id_serial;
788cfa84f46SDoug Rabson break;
789cfa84f46SDoug Rabson
790cfa84f46SDoug Rabson case ISA_IVAR_LOGICALID:
791cfa84f46SDoug Rabson *result = idev->id_logicalid;
792cfa84f46SDoug Rabson break;
793cfa84f46SDoug Rabson
794cfa84f46SDoug Rabson case ISA_IVAR_COMPATID:
795cfa84f46SDoug Rabson *result = idev->id_compatid;
796cfa84f46SDoug Rabson break;
797cfa84f46SDoug Rabson
7987abb4662SKazutaka YOKOTA case ISA_IVAR_CONFIGATTR:
7997abb4662SKazutaka YOKOTA *result = idev->id_config_attr;
8007abb4662SKazutaka YOKOTA break;
8017abb4662SKazutaka YOKOTA
802132580b5SWarner Losh case ISA_IVAR_PNP_CSN:
803132580b5SWarner Losh *result = idev->id_pnp_csn;
804132580b5SWarner Losh break;
805132580b5SWarner Losh
806132580b5SWarner Losh case ISA_IVAR_PNP_LDN:
807132580b5SWarner Losh *result = idev->id_pnp_ldn;
808132580b5SWarner Losh break;
809132580b5SWarner Losh
810132580b5SWarner Losh case ISA_IVAR_PNPBIOS_HANDLE:
811132580b5SWarner Losh *result = idev->id_pnpbios_handle;
812132580b5SWarner Losh break;
813132580b5SWarner Losh
814cfa84f46SDoug Rabson default:
81509aafea1SWarner Losh return (ENOENT);
816a3be63b3SDoug Rabson }
817a3be63b3SDoug Rabson
81809aafea1SWarner Losh return (0);
819cfa84f46SDoug Rabson }
820cfa84f46SDoug Rabson
821a3be63b3SDoug Rabson static int
isa_write_ivar(device_t bus,device_t dev,int index,uintptr_t value)82209aafea1SWarner Losh isa_write_ivar(device_t bus, device_t dev, int index, uintptr_t value)
823a3be63b3SDoug Rabson {
824a3be63b3SDoug Rabson struct isa_device* idev = DEVTOISA(dev);
825a3be63b3SDoug Rabson
826a3be63b3SDoug Rabson switch (index) {
827a3be63b3SDoug Rabson case ISA_IVAR_PORT_0:
828a3be63b3SDoug Rabson case ISA_IVAR_PORT_1:
829a3be63b3SDoug Rabson case ISA_IVAR_PORTSIZE_0:
830a3be63b3SDoug Rabson case ISA_IVAR_PORTSIZE_1:
831a3be63b3SDoug Rabson case ISA_IVAR_MADDR_0:
832a3be63b3SDoug Rabson case ISA_IVAR_MADDR_1:
83363e30378SJohn Baldwin case ISA_IVAR_MEMSIZE_0:
83463e30378SJohn Baldwin case ISA_IVAR_MEMSIZE_1:
835a3be63b3SDoug Rabson case ISA_IVAR_IRQ_0:
836a3be63b3SDoug Rabson case ISA_IVAR_IRQ_1:
837a3be63b3SDoug Rabson case ISA_IVAR_DRQ_0:
838a3be63b3SDoug Rabson case ISA_IVAR_DRQ_1:
83909aafea1SWarner Losh return (EINVAL);
840a3be63b3SDoug Rabson
841cfa84f46SDoug Rabson case ISA_IVAR_VENDORID:
842cfa84f46SDoug Rabson idev->id_vendorid = value;
843cfa84f46SDoug Rabson break;
844cfa84f46SDoug Rabson
845cfa84f46SDoug Rabson case ISA_IVAR_SERIAL:
846cfa84f46SDoug Rabson idev->id_serial = value;
847cfa84f46SDoug Rabson break;
848cfa84f46SDoug Rabson
849cfa84f46SDoug Rabson case ISA_IVAR_LOGICALID:
850cfa84f46SDoug Rabson idev->id_logicalid = value;
851cfa84f46SDoug Rabson break;
852cfa84f46SDoug Rabson
853cfa84f46SDoug Rabson case ISA_IVAR_COMPATID:
854cfa84f46SDoug Rabson idev->id_compatid = value;
855cfa84f46SDoug Rabson break;
856cfa84f46SDoug Rabson
8577abb4662SKazutaka YOKOTA case ISA_IVAR_CONFIGATTR:
8587abb4662SKazutaka YOKOTA idev->id_config_attr = value;
8597abb4662SKazutaka YOKOTA break;
8607abb4662SKazutaka YOKOTA
861a3be63b3SDoug Rabson default:
862a3be63b3SDoug Rabson return (ENOENT);
863a3be63b3SDoug Rabson }
864cfa84f46SDoug Rabson
865a3be63b3SDoug Rabson return (0);
866a3be63b3SDoug Rabson }
867a3be63b3SDoug Rabson
8684249382dSDoug Rabson /*
8694249382dSDoug Rabson * Free any resources which the driver missed or which we were holding for
8704249382dSDoug Rabson * it (see isa_probe_children).
8714249382dSDoug Rabson */
8724249382dSDoug Rabson static void
isa_child_detached(device_t dev,device_t child)8734249382dSDoug Rabson isa_child_detached(device_t dev, device_t child)
8744249382dSDoug Rabson {
8754249382dSDoug Rabson struct isa_device* idev = DEVTOISA(child);
8764249382dSDoug Rabson
8770d484d24SJohn Baldwin if (TAILQ_FIRST(&idev->id_configs))
8780d484d24SJohn Baldwin isa_claim_resources(dev, child);
8794249382dSDoug Rabson }
8804249382dSDoug Rabson
881ec22663bSDoug Rabson static void
isa_driver_added(device_t dev,driver_t * driver)882ec22663bSDoug Rabson isa_driver_added(device_t dev, driver_t *driver)
883ec22663bSDoug Rabson {
884ec22663bSDoug Rabson device_t *children;
885ec22663bSDoug Rabson int nchildren, i;
886ec22663bSDoug Rabson
887ec22663bSDoug Rabson /*
888ec22663bSDoug Rabson * Don't do anything if drivers are dynamically
889ec22663bSDoug Rabson * added during autoconfiguration (cf. ymf724).
890ec22663bSDoug Rabson * since that would end up calling identify
891ec22663bSDoug Rabson * twice.
892ec22663bSDoug Rabson */
893ec22663bSDoug Rabson if (!isa_running)
894ec22663bSDoug Rabson return;
895ec22663bSDoug Rabson
896ec22663bSDoug Rabson DEVICE_IDENTIFY(driver, dev);
897ec22663bSDoug Rabson if (device_get_children(dev, &children, &nchildren))
898ec22663bSDoug Rabson return;
899ec22663bSDoug Rabson
900ec22663bSDoug Rabson for (i = 0; i < nchildren; i++) {
901ec22663bSDoug Rabson device_t child = children[i];
902ec22663bSDoug Rabson struct isa_device *idev = DEVTOISA(child);
903ec22663bSDoug Rabson struct resource_list *rl = &idev->id_resources;
904ec22663bSDoug Rabson struct resource_list_entry *rle;
905ec22663bSDoug Rabson
906ec22663bSDoug Rabson if (device_get_state(child) != DS_NOTPRESENT)
907ec22663bSDoug Rabson continue;
908328d36e9SDoug Rabson if (!device_is_enabled(child))
909328d36e9SDoug Rabson continue;
910328d36e9SDoug Rabson
911328d36e9SDoug Rabson /*
912328d36e9SDoug Rabson * Free resources which we were holding on behalf of
913328d36e9SDoug Rabson * the device.
914328d36e9SDoug Rabson */
91536fed965SWarner Losh STAILQ_FOREACH(rle, &idev->id_resources, link) {
916328d36e9SDoug Rabson if (rle->res)
917328d36e9SDoug Rabson resource_list_release(rl, dev, child,
918328d36e9SDoug Rabson rle->res);
919328d36e9SDoug Rabson }
920ec22663bSDoug Rabson
921ec22663bSDoug Rabson if (TAILQ_FIRST(&idev->id_configs))
922ec22663bSDoug Rabson if (!isa_assign_resources(child))
923ec22663bSDoug Rabson continue;
924ec22663bSDoug Rabson
925ec22663bSDoug Rabson device_probe_and_attach(child);
926ec22663bSDoug Rabson
9270d484d24SJohn Baldwin if (TAILQ_FIRST(&idev->id_configs))
9280d484d24SJohn Baldwin isa_claim_resources(dev, child);
929ec22663bSDoug Rabson }
930ec22663bSDoug Rabson
931ec22663bSDoug Rabson free(children, M_TEMP);
932ec22663bSDoug Rabson }
933ec22663bSDoug Rabson
934a3be63b3SDoug Rabson static int
isa_set_resource(device_t dev,device_t child,int type,int rid,rman_res_t start,rman_res_t count)935a3be63b3SDoug Rabson isa_set_resource(device_t dev, device_t child, int type, int rid,
9362dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t count)
937a3be63b3SDoug Rabson {
938a3be63b3SDoug Rabson struct isa_device* idev = DEVTOISA(child);
939a3be63b3SDoug Rabson struct resource_list *rl = &idev->id_resources;
940a3be63b3SDoug Rabson
941a3be63b3SDoug Rabson if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
942a3be63b3SDoug Rabson && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
94309aafea1SWarner Losh return (EINVAL);
9444249382dSDoug Rabson if (rid < 0)
94509aafea1SWarner Losh return (EINVAL);
9464249382dSDoug Rabson if (type == SYS_RES_IOPORT && rid >= ISA_NPORT)
94709aafea1SWarner Losh return (EINVAL);
9484249382dSDoug Rabson if (type == SYS_RES_MEMORY && rid >= ISA_NMEM)
94909aafea1SWarner Losh return (EINVAL);
9504249382dSDoug Rabson if (type == SYS_RES_IRQ && rid >= ISA_NIRQ)
95109aafea1SWarner Losh return (EINVAL);
9524249382dSDoug Rabson if (type == SYS_RES_DRQ && rid >= ISA_NDRQ)
95309aafea1SWarner Losh return (EINVAL);
954a3be63b3SDoug Rabson
955a3be63b3SDoug Rabson resource_list_add(rl, type, rid, start, start + count - 1, count);
956a3be63b3SDoug Rabson
95709aafea1SWarner Losh return (0);
958a3be63b3SDoug Rabson }
959a3be63b3SDoug Rabson
9601581afb3SMatthew N. Dodd static struct resource_list *
isa_get_resource_list(device_t dev,device_t child)9611581afb3SMatthew N. Dodd isa_get_resource_list (device_t dev, device_t child)
962a3be63b3SDoug Rabson {
963a3be63b3SDoug Rabson struct isa_device* idev = DEVTOISA(child);
964a3be63b3SDoug Rabson struct resource_list *rl = &idev->id_resources;
965a3be63b3SDoug Rabson
9661581afb3SMatthew N. Dodd if (!rl)
9671581afb3SMatthew N. Dodd return (NULL);
968a3be63b3SDoug Rabson
9691581afb3SMatthew N. Dodd return (rl);
970cfa84f46SDoug Rabson }
971cfa84f46SDoug Rabson
9724249382dSDoug Rabson static int
isa_add_config(device_t dev,device_t child,int priority,struct isa_config * config)97309aafea1SWarner Losh isa_add_config(device_t dev, device_t child, int priority,
97409aafea1SWarner Losh struct isa_config *config)
9754249382dSDoug Rabson {
9764249382dSDoug Rabson struct isa_device* idev = DEVTOISA(child);
9774249382dSDoug Rabson struct isa_config_entry *newice, *ice;
9784249382dSDoug Rabson
9794249382dSDoug Rabson newice = malloc(sizeof *ice, M_DEVBUF, M_NOWAIT);
9804249382dSDoug Rabson if (!newice)
98109aafea1SWarner Losh return (ENOMEM);
9824249382dSDoug Rabson
9834249382dSDoug Rabson newice->ice_priority = priority;
9844249382dSDoug Rabson newice->ice_config = *config;
9854249382dSDoug Rabson
9864249382dSDoug Rabson TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
9874249382dSDoug Rabson if (ice->ice_priority > priority)
9884249382dSDoug Rabson break;
9894249382dSDoug Rabson }
9904249382dSDoug Rabson if (ice)
9914249382dSDoug Rabson TAILQ_INSERT_BEFORE(ice, newice, ice_link);
9924249382dSDoug Rabson else
9934249382dSDoug Rabson TAILQ_INSERT_TAIL(&idev->id_configs, newice, ice_link);
9944249382dSDoug Rabson
99509aafea1SWarner Losh return (0);
9964249382dSDoug Rabson }
9974249382dSDoug Rabson
9984249382dSDoug Rabson static void
isa_set_config_callback(device_t dev,device_t child,isa_config_cb * fn,void * arg)99909aafea1SWarner Losh isa_set_config_callback(device_t dev, device_t child, isa_config_cb *fn,
100009aafea1SWarner Losh void *arg)
10014249382dSDoug Rabson {
10024249382dSDoug Rabson struct isa_device* idev = DEVTOISA(child);
10034249382dSDoug Rabson
10044249382dSDoug Rabson idev->id_config_cb = fn;
10054249382dSDoug Rabson idev->id_config_arg = arg;
10064249382dSDoug Rabson }
10074249382dSDoug Rabson
10084249382dSDoug Rabson static int
isa_pnp_probe(device_t dev,device_t child,struct isa_pnp_id * ids)10094249382dSDoug Rabson isa_pnp_probe(device_t dev, device_t child, struct isa_pnp_id *ids)
10104249382dSDoug Rabson {
10114249382dSDoug Rabson struct isa_device* idev = DEVTOISA(child);
10124249382dSDoug Rabson
10134249382dSDoug Rabson if (!idev->id_vendorid)
101409aafea1SWarner Losh return (ENOENT);
10154249382dSDoug Rabson
10162e5bbc3fSMike Smith while (ids && ids->ip_id) {
10174249382dSDoug Rabson /*
10184249382dSDoug Rabson * Really ought to support >1 compat id per device.
10194249382dSDoug Rabson */
10204249382dSDoug Rabson if (idev->id_logicalid == ids->ip_id
10214249382dSDoug Rabson || idev->id_compatid == ids->ip_id) {
10222aadbbb6SDoug Rabson if (ids->ip_desc)
10234249382dSDoug Rabson device_set_desc(child, ids->ip_desc);
102409aafea1SWarner Losh return (0);
10254249382dSDoug Rabson }
10264249382dSDoug Rabson ids++;
10274249382dSDoug Rabson }
10284249382dSDoug Rabson
102909aafea1SWarner Losh return (ENXIO);
10304249382dSDoug Rabson }
10314249382dSDoug Rabson
1032a81804d9SWarner Losh static int
isa_child_pnpinfo(device_t bus,device_t child,struct sbuf * sb)1033ddfc9c4cSWarner Losh isa_child_pnpinfo(device_t bus, device_t child, struct sbuf *sb)
1034a81804d9SWarner Losh {
1035a81804d9SWarner Losh struct isa_device *idev = DEVTOISA(child);
1036a81804d9SWarner Losh
1037a81804d9SWarner Losh if (idev->id_vendorid)
1038ddfc9c4cSWarner Losh sbuf_printf(sb, "pnpid=%s",
1039a81804d9SWarner Losh pnp_eisaformat(idev->id_vendorid));
1040a81804d9SWarner Losh return (0);
1041a81804d9SWarner Losh }
1042a81804d9SWarner Losh
1043a81804d9SWarner Losh static int
isa_child_location(device_t bus,device_t child,struct sbuf * sb)1044ddfc9c4cSWarner Losh isa_child_location(device_t bus, device_t child, struct sbuf *sb)
1045a81804d9SWarner Losh {
1046132580b5SWarner Losh #if 0
1047132580b5SWarner Losh /* id_pnphandle isn't there yet */
1048132580b5SWarner Losh struct isa_device *idev = DEVTOISA(child);
1049132580b5SWarner Losh
1050132580b5SWarner Losh if (idev->id_vendorid)
1051ddfc9c4cSWarner Losh sbuf_printf(sbuf, "pnphandle=%d", idev->id_pnphandle);
1052132580b5SWarner Losh #endif
1053a81804d9SWarner Losh return (0);
1054a81804d9SWarner Losh }
1055a81804d9SWarner Losh
1056a3be63b3SDoug Rabson static device_method_t isa_methods[] = {
1057a3be63b3SDoug Rabson /* Device interface */
1058a3be63b3SDoug Rabson DEVMETHOD(device_probe, isa_probe),
1059a3be63b3SDoug Rabson DEVMETHOD(device_attach, isa_attach),
1060a3be63b3SDoug Rabson DEVMETHOD(device_detach, bus_generic_detach),
1061a3be63b3SDoug Rabson DEVMETHOD(device_shutdown, bus_generic_shutdown),
1062a3be63b3SDoug Rabson DEVMETHOD(device_suspend, bus_generic_suspend),
1063a3be63b3SDoug Rabson DEVMETHOD(device_resume, bus_generic_resume),
1064a3be63b3SDoug Rabson
1065a3be63b3SDoug Rabson /* Bus interface */
1066a3be63b3SDoug Rabson DEVMETHOD(bus_add_child, isa_add_child),
1067*c8fa5488SJohn Baldwin DEVMETHOD(bus_child_deleted, isa_child_deleted),
1068a3be63b3SDoug Rabson DEVMETHOD(bus_print_child, isa_print_child),
1069328d36e9SDoug Rabson DEVMETHOD(bus_probe_nomatch, isa_probe_nomatch),
1070a3be63b3SDoug Rabson DEVMETHOD(bus_read_ivar, isa_read_ivar),
1071a3be63b3SDoug Rabson DEVMETHOD(bus_write_ivar, isa_write_ivar),
10724249382dSDoug Rabson DEVMETHOD(bus_child_detached, isa_child_detached),
1073ec22663bSDoug Rabson DEVMETHOD(bus_driver_added, isa_driver_added),
1074f9a94737SJohn Baldwin DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
1075f9a94737SJohn Baldwin DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
10761581afb3SMatthew N. Dodd
10771581afb3SMatthew N. Dodd DEVMETHOD(bus_get_resource_list,isa_get_resource_list),
10781581afb3SMatthew N. Dodd DEVMETHOD(bus_alloc_resource, isa_alloc_resource),
10791581afb3SMatthew N. Dodd DEVMETHOD(bus_release_resource, isa_release_resource),
108025afb89bSDoug Rabson DEVMETHOD(bus_set_resource, isa_set_resource),
10811581afb3SMatthew N. Dodd DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
10821581afb3SMatthew N. Dodd DEVMETHOD(bus_delete_resource, bus_generic_rl_delete_resource),
10831581afb3SMatthew N. Dodd DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
10841581afb3SMatthew N. Dodd DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
1085ddfc9c4cSWarner Losh DEVMETHOD(bus_child_pnpinfo, isa_child_pnpinfo),
1086ddfc9c4cSWarner Losh DEVMETHOD(bus_child_location, isa_child_location),
10870d484d24SJohn Baldwin DEVMETHOD(bus_hinted_child, isa_hinted_child),
10880d484d24SJohn Baldwin DEVMETHOD(bus_hint_device_unit, isa_hint_device_unit),
1089a3be63b3SDoug Rabson
1090a3be63b3SDoug Rabson /* ISA interface */
10914249382dSDoug Rabson DEVMETHOD(isa_add_config, isa_add_config),
10924249382dSDoug Rabson DEVMETHOD(isa_set_config_callback, isa_set_config_callback),
10934249382dSDoug Rabson DEVMETHOD(isa_pnp_probe, isa_pnp_probe),
1094a3be63b3SDoug Rabson
1095a3be63b3SDoug Rabson { 0, 0 }
1096a3be63b3SDoug Rabson };
1097a3be63b3SDoug Rabson
10980d484d24SJohn Baldwin DEFINE_CLASS_0(isa, isa_driver, isa_methods, 0);
1099a3be63b3SDoug Rabson
1100a3be63b3SDoug Rabson /*
1101812fb8f2SWarner Losh * ISA can be attached to a PCI-ISA bridge, or other locations on some
1102812fb8f2SWarner Losh * platforms.
1103a3be63b3SDoug Rabson */
110409fd3b43SJohn Baldwin DRIVER_MODULE(isa, isab, isa_driver, 0, 0);
110509fd3b43SJohn Baldwin DRIVER_MODULE(isa, eisab, isa_driver, 0, 0);
1106f7130d09SMatthew N. Dodd MODULE_VERSION(isa, 1);
1107c37faf26SJohn Baldwin
1108c37faf26SJohn Baldwin /*
1109c37faf26SJohn Baldwin * Code common to ISA bridges.
1110c37faf26SJohn Baldwin */
1111c37faf26SJohn Baldwin
1112c37faf26SJohn Baldwin int
isab_attach(device_t dev)1113c37faf26SJohn Baldwin isab_attach(device_t dev)
1114c37faf26SJohn Baldwin {
1115c37faf26SJohn Baldwin device_t child;
1116c37faf26SJohn Baldwin
1117c37faf26SJohn Baldwin child = device_add_child(dev, "isa", 0);
1118c37faf26SJohn Baldwin if (child != NULL)
1119c37faf26SJohn Baldwin return (bus_generic_attach(dev));
1120c37faf26SJohn Baldwin return (ENXIO);
1121c37faf26SJohn Baldwin }
11227dcb3b12SWarner Losh
11237dcb3b12SWarner Losh char *
pnp_eisaformat(uint32_t id)11247dcb3b12SWarner Losh pnp_eisaformat(uint32_t id)
11257dcb3b12SWarner Losh {
11267dcb3b12SWarner Losh uint8_t *data;
11277dcb3b12SWarner Losh static char idbuf[8];
11287dcb3b12SWarner Losh const char hextoascii[] = "0123456789abcdef";
11297dcb3b12SWarner Losh
11307dcb3b12SWarner Losh id = htole32(id);
11317dcb3b12SWarner Losh data = (uint8_t *)&id;
11327dcb3b12SWarner Losh idbuf[0] = '@' + ((data[0] & 0x7c) >> 2);
11337dcb3b12SWarner Losh idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5));
11347dcb3b12SWarner Losh idbuf[2] = '@' + (data[1] & 0x1f);
11357dcb3b12SWarner Losh idbuf[3] = hextoascii[(data[2] >> 4)];
11367dcb3b12SWarner Losh idbuf[4] = hextoascii[(data[2] & 0xf)];
11377dcb3b12SWarner Losh idbuf[5] = hextoascii[(data[3] >> 4)];
11387dcb3b12SWarner Losh idbuf[6] = hextoascii[(data[3] & 0xf)];
11397dcb3b12SWarner Losh idbuf[7] = 0;
11407dcb3b12SWarner Losh return(idbuf);
11417dcb3b12SWarner Losh }
1142