xref: /freebsd/sys/dev/pci/isa_pci.c (revision 3b2956781048f300c081952150d515573a274620)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1994,1995 Stefan Esser, Wolfgang StanglMeier
5  * Copyright (c) 2000 Michael Smith <msmith@freebsd.org>
6  * Copyright (c) 2000 BSDi
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 /*
37  * PCI:ISA bridge support
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
44 #include <sys/bus.h>
45 
46 #include <isa/isavar.h>
47 #include <dev/pci/pcivar.h>
48 #include <dev/pci/pcireg.h>
49 
50 #include <machine/bus.h>
51 #include <sys/rman.h>
52 #include <machine/resource.h>
53 
54 static int	isab_pci_probe(device_t dev);
55 static int	isab_pci_attach(device_t dev);
56 static struct resource *	isab_pci_alloc_resource(device_t dev,
57     device_t child, int type, int *rid, rman_res_t start, rman_res_t end,
58     rman_res_t count, u_int flags);
59 static int	isab_pci_release_resource(device_t dev, device_t child,
60     int type, int rid, struct resource *r);
61 
62 static device_method_t isab_methods[] = {
63     /* Device interface */
64     DEVMETHOD(device_probe,		isab_pci_probe),
65     DEVMETHOD(device_attach,		isab_pci_attach),
66     DEVMETHOD(device_detach,		bus_generic_detach),
67     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
68     DEVMETHOD(device_suspend,		bus_generic_suspend),
69     DEVMETHOD(device_resume,		bus_generic_resume),
70 
71     /* Bus interface */
72     DEVMETHOD(bus_add_child,		bus_generic_add_child),
73     DEVMETHOD(bus_alloc_resource,	isab_pci_alloc_resource),
74     DEVMETHOD(bus_release_resource,	isab_pci_release_resource),
75     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
76     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
77     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
78     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
79 
80     DEVMETHOD_END
81 };
82 
83 struct isab_pci_resource {
84 	struct resource	*ip_res;
85 	int	ip_refs;
86 };
87 
88 struct isab_pci_softc {
89 	struct isab_pci_resource isab_pci_res[PCIR_MAX_BAR_0 + 1];
90 };
91 
92 static driver_t isab_driver = {
93     "isab",
94     isab_methods,
95     sizeof(struct isab_pci_softc),
96 };
97 
98 DRIVER_MODULE(isab, pci, isab_driver, 0, 0);
99 
100 /*
101  * XXX we need to add a quirk list here for bridges that don't correctly
102  *     report themselves.
103  */
104 static int
105 isab_pci_probe(device_t dev)
106 {
107     int		matched = 0;
108 
109     /*
110      * Try for a generic match based on class/subclass.
111      */
112     if ((pci_get_class(dev) == PCIC_BRIDGE) &&
113 	(pci_get_subclass(dev) == PCIS_BRIDGE_ISA)) {
114 	matched = 1;
115     } else {
116 	/*
117 	 * These are devices that we *know* are PCI:ISA bridges.
118 	 * Sometimes, however, they don't report themselves as
119 	 * such.  Check in case one of them is pretending to be
120 	 * something else.
121 	 */
122 	switch (pci_get_devid(dev)) {
123 	case 0x04848086:	/* Intel 82378ZB/82378IB */
124 	case 0x122e8086:	/* Intel 82371FB */
125 	case 0x70008086:	/* Intel 82371SB */
126 	case 0x71108086:	/* Intel 82371AB */
127 	case 0x71988086:	/* Intel 82443MX */
128 	case 0x24108086:	/* Intel 82801AA (ICH) */
129 	case 0x24208086:	/* Intel 82801AB (ICH0) */
130 	case 0x24408086:	/* Intel 82801AB (ICH2) */
131 	case 0x00061004:	/* VLSI 82C593 */
132 	case 0x05861106:	/* VIA 82C586 */
133 	case 0x05961106:	/* VIA 82C596 */
134 	case 0x06861106:	/* VIA 82C686 */
135 	case 0x153310b9:	/* AcerLabs M1533 */
136 	case 0x154310b9:	/* AcerLabs M1543 */
137 	case 0x00081039:	/* SiS 85c503 */
138 	case 0x00001078:	/* Cyrix Cx5510 */
139 	case 0x01001078:	/* Cyrix Cx5530 */
140 	case 0xc7001045:	/* OPTi 82C700 (FireStar) */
141 	case 0x886a1060:	/* UMC UM8886 ISA */
142 	case 0x02001166:	/* ServerWorks IB6566 PCI */
143 	    if (bootverbose)
144 		printf("PCI-ISA bridge with incorrect subclass 0x%x\n",
145 		       pci_get_subclass(dev));
146 	    matched = 1;
147 	    break;
148 
149 	default:
150 	    break;
151 	}
152     }
153 
154     if (matched) {
155 	device_set_desc(dev, "PCI-ISA bridge");
156 	return(-10000);
157     }
158     return(ENXIO);
159 }
160 
161 static int
162 isab_pci_attach(device_t dev)
163 {
164 
165 	bus_generic_probe(dev);
166 	return (isab_attach(dev));
167 }
168 
169 static struct resource *
170 isab_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
171     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
172 {
173 	struct isab_pci_softc *sc;
174 	int bar;
175 
176 	if (device_get_parent(child) != dev)
177 		return bus_generic_alloc_resource(dev, child, type, rid, start,
178 		    end, count, flags);
179 
180 	switch (type) {
181 	case SYS_RES_MEMORY:
182 	case SYS_RES_IOPORT:
183 		/*
184 		 * For BARs, we cache the resource so that we only allocate it
185 		 * from the PCI bus once.
186 		 */
187 		bar = PCI_RID2BAR(*rid);
188 		if (bar < 0 || bar > PCIR_MAX_BAR_0)
189 			return (NULL);
190 		sc = device_get_softc(dev);
191 		if (sc->isab_pci_res[bar].ip_res == NULL)
192 			sc->isab_pci_res[bar].ip_res = bus_alloc_resource(dev, type,
193 			    rid, start, end, count, flags);
194 		if (sc->isab_pci_res[bar].ip_res != NULL)
195 			sc->isab_pci_res[bar].ip_refs++;
196 		return (sc->isab_pci_res[bar].ip_res);
197 	}
198 
199 	return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child, type, rid,
200 		start, end, count, flags));
201 }
202 
203 static int
204 isab_pci_release_resource(device_t dev, device_t child, int type, int rid,
205     struct resource *r)
206 {
207 	struct isab_pci_softc *sc;
208 	int bar, error;
209 
210 	if (device_get_parent(child) != dev)
211 		return bus_generic_release_resource(dev, child, type, rid, r);
212 
213 	switch (type) {
214 	case SYS_RES_MEMORY:
215 	case SYS_RES_IOPORT:
216 		/*
217 		 * For BARs, we release the resource from the PCI bus
218 		 * when the last child reference goes away.
219 		 */
220 		bar = PCI_RID2BAR(rid);
221 		if (bar < 0 || bar > PCIR_MAX_BAR_0)
222 			return (EINVAL);
223 		sc = device_get_softc(dev);
224 		if (sc->isab_pci_res[bar].ip_res == NULL)
225 			return (EINVAL);
226 		KASSERT(sc->isab_pci_res[bar].ip_res == r,
227 		    ("isa_pci resource mismatch"));
228 		if (sc->isab_pci_res[bar].ip_refs > 1) {
229 			sc->isab_pci_res[bar].ip_refs--;
230 			return (0);
231 		}
232 		KASSERT(sc->isab_pci_res[bar].ip_refs > 0,
233 		    ("isa_pci resource reference count underflow"));
234 		error = bus_release_resource(dev, type, rid, r);
235 		if (error == 0) {
236 			sc->isab_pci_res[bar].ip_res = NULL;
237 			sc->isab_pci_res[bar].ip_refs = 0;
238 		}
239 		return (error);
240 	}
241 
242 	return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child, type,
243 		rid, r));
244 }
245