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 <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/openfirm.h>
55 #include <machine/bus.h>
56 #include <machine/ofw_bus.h>
57 #include <machine/resource.h>
59 #include <dev/pci/pcireg.h>
60 #include <dev/pci/pcivar.h>
61 #include <dev/pci/pcib_private.h>
65 #include <sparc64/pci/ofw_pci.h>
66 #include <sparc64/pci/ofw_pcib_subr.h>
69 * Bridge-specific data.
72 struct ofw_pcib_gen_softc sc_bsc;
77 static device_probe_t apb_probe;
78 static device_attach_t apb_attach;
79 static bus_alloc_resource_t apb_alloc_resource;
81 static device_method_t apb_methods[] = {
82 /* Device interface */
83 DEVMETHOD(device_probe, apb_probe),
84 DEVMETHOD(device_attach, apb_attach),
85 DEVMETHOD(device_shutdown, bus_generic_shutdown),
86 DEVMETHOD(device_suspend, bus_generic_suspend),
87 DEVMETHOD(device_resume, bus_generic_resume),
90 DEVMETHOD(bus_print_child, bus_generic_print_child),
91 DEVMETHOD(bus_read_ivar, pcib_read_ivar),
92 DEVMETHOD(bus_write_ivar, pcib_write_ivar),
93 DEVMETHOD(bus_alloc_resource, apb_alloc_resource),
94 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
95 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
96 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
97 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
98 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
101 DEVMETHOD(pcib_maxslots, pcib_maxslots),
102 DEVMETHOD(pcib_read_config, pcib_read_config),
103 DEVMETHOD(pcib_write_config, pcib_write_config),
104 DEVMETHOD(pcib_route_interrupt, ofw_pcib_gen_route_interrupt),
106 /* ofw_bus interface */
107 DEVMETHOD(ofw_bus_get_node, ofw_pcib_gen_get_node),
112 static devclass_t pcib_devclass;
114 DEFINE_CLASS_0(pcib, apb_driver, apb_methods, sizeof(struct apb_softc));
115 DRIVER_MODULE(apb, pci, apb_driver, pcib_devclass, 0, 0);
117 /* APB specific registers */
118 #define APBR_IOMAP 0xde
119 #define APBR_MEMMAP 0xdf
121 /* Definitions for the mapping registers */
122 #define APB_IO_SCALE 0x200000
123 #define APB_MEM_SCALE 0x20000000
126 * Generic device interface
129 apb_probe(device_t dev)
132 if (pci_get_vendor(dev) == 0x108e && /* Sun */
133 pci_get_device(dev) == 0x5000) { /* APB */
134 device_set_desc(dev, "APB PCI-PCI bridge");
141 apb_map_print(uint8_t map, u_long scale)
145 for (first = 1, i = 0; i < 8; i++) {
146 if ((map & (1 << i)) != 0) {
147 printf("%s0x%lx-0x%lx", first ? "" : ", ",
148 i * scale, (i + 1) * scale - 1);
155 apb_checkrange(uint8_t map, u_long scale, u_long start, u_long end)
164 if ((map & (1 << i)) == 0)
170 apb_attach(device_t dev)
172 struct apb_softc *sc;
174 sc = device_get_softc(dev);
177 * Get current bridge configuration.
179 sc->sc_bsc.ops_pcib_sc.domain = pci_get_domain(dev);
180 sc->sc_bsc.ops_pcib_sc.secbus =
181 pci_read_config(dev, PCIR_SECBUS_1, 1);
182 sc->sc_bsc.ops_pcib_sc.subbus =
183 pci_read_config(dev, PCIR_SUBBUS_1, 1);
184 sc->sc_iomap = pci_read_config(dev, APBR_IOMAP, 1);
185 sc->sc_memmap = pci_read_config(dev, APBR_MEMMAP, 1);
186 ofw_pcib_gen_setup(dev);
189 device_printf(dev, " domain %d\n",
190 sc->sc_bsc.ops_pcib_sc.domain);
191 device_printf(dev, " secondary bus %d\n",
192 sc->sc_bsc.ops_pcib_sc.secbus);
193 device_printf(dev, " subordinate bus %d\n",
194 sc->sc_bsc.ops_pcib_sc.subbus);
195 device_printf(dev, " I/O decode ");
196 apb_map_print(sc->sc_iomap, APB_IO_SCALE);
198 device_printf(dev, " memory decode ");
199 apb_map_print(sc->sc_memmap, APB_MEM_SCALE);
203 device_add_child(dev, "pci", -1);
204 return (bus_generic_attach(dev));
208 * We have to trap resource allocation requests and ensure that the bridge
209 * is set up to, or capable of handling them.
211 static struct resource *
212 apb_alloc_resource(device_t dev, device_t child, int type, int *rid,
213 u_long start, u_long end, u_long count, u_int flags)
215 struct apb_softc *sc;
217 sc = device_get_softc(dev);
220 * If this is a "default" allocation against this rid, we can't work
221 * out where it's coming from (we should actually never see these) so
222 * we just have to punt.
224 if (start == 0 && end == ~0) {
225 device_printf(dev, "can't decode default resource id %d for "
226 "%s%d, bypassing\n", *rid, device_get_name(child),
227 device_get_unit(child));
232 * Fail the allocation for this range if it's not supported.
233 * XXX we should probably just fix up the bridge decode and
238 if (!apb_checkrange(sc->sc_iomap, APB_IO_SCALE, start, end)) {
239 device_printf(dev, "device %s%d requested unsupported "
240 "I/O range 0x%lx-0x%lx\n", device_get_name(child),
241 device_get_unit(child), start, end);
245 device_printf(sc->sc_bsc.ops_pcib_sc.dev, "device "
246 "%s%d requested decoded I/O range 0x%lx-0x%lx\n",
247 device_get_name(child), device_get_unit(child),
252 if (!apb_checkrange(sc->sc_memmap, APB_MEM_SCALE, start, end)) {
253 device_printf(dev, "device %s%d requested unsupported "
254 "memory range 0x%lx-0x%lx\n",
255 device_get_name(child), device_get_unit(child),
260 device_printf(sc->sc_bsc.ops_pcib_sc.dev, "device "
261 "%s%d requested decoded memory range 0x%lx-0x%lx\n",
262 device_get_name(child), device_get_unit(child),
272 * Bridge is OK decoding this resource, so pass it up.
274 return (bus_generic_alloc_resource(dev, child, type, rid, start, end,