2 * Copyright (c) 1994,1995 Stefan Esser, Wolfgang StanglMeier
3 * Copyright (c) 2000 Michael Smith <msmith@freebsd.org>
4 * Copyright (c) 2000 BSDi
5 * Copyright (c) 2001, 2003 Thomas Moestl <tmm@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * from: FreeBSD: src/sys/dev/pci/pci_pci.c,v 1.3 2000/12/13
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 * Support for the Sun APB (Advanced PCI Bridge) PCI-PCI bridge.
39 * This bridge does not fully comply to the PCI bridge specification, and is
40 * therefore not supported by the generic driver.
41 * We can use some of the pcib methods anyway.
44 #include "opt_ofw_pci.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/module.h>
52 #include <sys/sysctl.h>
54 #include <dev/ofw/ofw_bus.h>
55 #include <dev/ofw/openfirm.h>
57 #include <machine/bus.h>
58 #include <machine/resource.h>
60 #include <dev/pci/pcireg.h>
61 #include <dev/pci/pcivar.h>
62 #include <dev/pci/pcib_private.h>
66 #include <sparc64/pci/ofw_pci.h>
67 #include <sparc64/pci/ofw_pcib_subr.h>
70 * Bridge-specific data.
73 struct ofw_pcib_gen_softc sc_bsc;
78 static device_probe_t apb_probe;
79 static device_attach_t apb_attach;
80 static bus_alloc_resource_t apb_alloc_resource;
81 static bus_adjust_resource_t apb_adjust_resource;
83 static device_method_t apb_methods[] = {
84 /* Device interface */
85 DEVMETHOD(device_probe, apb_probe),
86 DEVMETHOD(device_attach, apb_attach),
89 DEVMETHOD(bus_alloc_resource, apb_alloc_resource),
90 DEVMETHOD(bus_adjust_resource, apb_adjust_resource),
91 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
94 DEVMETHOD(pcib_route_interrupt, ofw_pcib_gen_route_interrupt),
95 DEVMETHOD(pcib_request_feature, pcib_request_feature_allow),
97 /* ofw_bus interface */
98 DEVMETHOD(ofw_bus_get_node, ofw_pcib_gen_get_node),
103 static devclass_t pcib_devclass;
105 DEFINE_CLASS_1(pcib, apb_driver, apb_methods, sizeof(struct apb_softc),
107 EARLY_DRIVER_MODULE(apb, pci, apb_driver, pcib_devclass, 0, 0, BUS_PASS_BUS);
108 MODULE_DEPEND(apb, pci, 1, 1, 1);
110 /* APB specific registers */
111 #define APBR_IOMAP 0xde
112 #define APBR_MEMMAP 0xdf
114 /* Definitions for the mapping registers */
115 #define APB_IO_SCALE 0x200000
116 #define APB_MEM_SCALE 0x20000000
119 * Generic device interface
122 apb_probe(device_t dev)
125 if (pci_get_vendor(dev) == 0x108e && /* Sun */
126 pci_get_device(dev) == 0x5000) { /* APB */
127 device_set_desc(dev, "APB PCI-PCI bridge");
134 apb_map_print(uint8_t map, rman_res_t scale)
138 for (first = 1, i = 0; i < 8; i++) {
139 if ((map & (1 << i)) != 0) {
140 printf("%s0x%jx-0x%jx", first ? "" : ", ",
141 i * scale, (i + 1) * scale - 1);
148 apb_checkrange(uint8_t map, rman_res_t scale, rman_res_t start, rman_res_t end)
157 if ((map & (1 << i)) == 0)
163 apb_attach(device_t dev)
165 struct apb_softc *sc;
166 struct sysctl_ctx_list *sctx;
167 struct sysctl_oid *soid;
169 sc = device_get_softc(dev);
172 * Get current bridge configuration.
174 sc->sc_bsc.ops_pcib_sc.domain = pci_get_domain(dev);
175 sc->sc_bsc.ops_pcib_sc.pribus = pci_get_bus(dev);
176 pci_write_config(dev, PCIR_PRIBUS_1, sc->sc_bsc.ops_pcib_sc.pribus, 1);
177 sc->sc_bsc.ops_pcib_sc.bus.sec =
178 pci_read_config(dev, PCIR_SECBUS_1, 1);
179 sc->sc_bsc.ops_pcib_sc.bus.sub =
180 pci_read_config(dev, PCIR_SUBBUS_1, 1);
181 sc->sc_bsc.ops_pcib_sc.bridgectl =
182 pci_read_config(dev, PCIR_BRIDGECTL_1, 2);
183 sc->sc_iomap = pci_read_config(dev, APBR_IOMAP, 1);
184 sc->sc_memmap = pci_read_config(dev, APBR_MEMMAP, 1);
187 * Setup SYSCTL reporting nodes.
189 sctx = device_get_sysctl_ctx(dev);
190 soid = device_get_sysctl_tree(dev);
191 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain",
192 CTLFLAG_RD, &sc->sc_bsc.ops_pcib_sc.domain, 0,
194 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus",
195 CTLFLAG_RD, &sc->sc_bsc.ops_pcib_sc.pribus, 0,
196 "Primary bus number");
197 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus",
198 CTLFLAG_RD, &sc->sc_bsc.ops_pcib_sc.bus.sec, 0,
199 "Secondary bus number");
200 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus",
201 CTLFLAG_RD, &sc->sc_bsc.ops_pcib_sc.bus.sub, 0,
202 "Subordinate bus number");
204 ofw_pcib_gen_setup(dev);
207 device_printf(dev, " domain %d\n",
208 sc->sc_bsc.ops_pcib_sc.domain);
209 device_printf(dev, " secondary bus %d\n",
210 sc->sc_bsc.ops_pcib_sc.bus.sec);
211 device_printf(dev, " subordinate bus %d\n",
212 sc->sc_bsc.ops_pcib_sc.bus.sub);
213 device_printf(dev, " I/O decode ");
214 apb_map_print(sc->sc_iomap, APB_IO_SCALE);
216 device_printf(dev, " memory decode ");
217 apb_map_print(sc->sc_memmap, APB_MEM_SCALE);
221 device_add_child(dev, "pci", -1);
222 return (bus_generic_attach(dev));
226 * We have to trap resource allocation requests and ensure that the bridge
227 * is set up to, or capable of handling them.
229 static struct resource *
230 apb_alloc_resource(device_t dev, device_t child, int type, int *rid,
231 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
233 struct apb_softc *sc;
235 sc = device_get_softc(dev);
238 * If this is a "default" allocation against this rid, we can't work
239 * out where it's coming from (we should actually never see these) so
240 * we just have to punt.
242 if (RMAN_IS_DEFAULT_RANGE(start, end)) {
243 device_printf(dev, "can't decode default resource id %d for "
244 "%s, bypassing\n", *rid, device_get_nameunit(child));
249 * Fail the allocation for this range if it's not supported.
250 * XXX we should probably just fix up the bridge decode and
255 if (!apb_checkrange(sc->sc_iomap, APB_IO_SCALE, start, end)) {
256 device_printf(dev, "device %s requested unsupported "
257 "I/O range 0x%jx-0x%jx\n",
258 device_get_nameunit(child), start, end);
262 device_printf(sc->sc_bsc.ops_pcib_sc.dev, "device "
263 "%s requested decoded I/O range 0x%jx-0x%jx\n",
264 device_get_nameunit(child), start, end);
267 if (!apb_checkrange(sc->sc_memmap, APB_MEM_SCALE, start,
269 device_printf(dev, "device %s requested unsupported "
270 "memory range 0x%jx-0x%jx\n",
271 device_get_nameunit(child), start, end);
275 device_printf(sc->sc_bsc.ops_pcib_sc.dev, "device "
276 "%s requested decoded memory range 0x%jx-0x%jx\n",
277 device_get_nameunit(child), start, end);
283 * Bridge is OK decoding this resource, so pass it up.
285 return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
290 apb_adjust_resource(device_t dev, device_t child, int type,
291 struct resource *r, rman_res_t start, rman_res_t end)
293 struct apb_softc *sc;
295 sc = device_get_softc(dev);
298 if (!apb_checkrange(sc->sc_iomap, APB_IO_SCALE, start, end))
302 if (!apb_checkrange(sc->sc_memmap, APB_MEM_SCALE, start, end))
306 return (bus_generic_adjust_resource(dev, child, type, r, start, end));