2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1994,1995 Stefan Esser, Wolfgang StanglMeier
5 * Copyright (c) 2000 Michael Smith <msmith@freebsd.org>
6 * Copyright (c) 2000 BSDi
7 * Copyright (c) 2001, 2003 Thomas Moestl <tmm@FreeBSD.org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * from: FreeBSD: src/sys/dev/pci/pci_pci.c,v 1.3 2000/12/13
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * Support for the Sun APB (Advanced PCI Bridge) PCI-PCI bridge.
41 * This bridge does not fully comply to the PCI bridge specification, and is
42 * therefore not supported by the generic driver.
43 * We can use some of the pcib methods anyway.
46 #include "opt_ofw_pci.h"
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/module.h>
54 #include <sys/sysctl.h>
56 #include <dev/ofw/ofw_bus.h>
57 #include <dev/ofw/openfirm.h>
59 #include <machine/bus.h>
60 #include <machine/resource.h>
62 #include <dev/pci/pcireg.h>
63 #include <dev/pci/pcivar.h>
64 #include <dev/pci/pcib_private.h>
68 #include <sparc64/pci/ofw_pci.h>
69 #include <sparc64/pci/ofw_pcib_subr.h>
72 * Bridge-specific data.
75 struct ofw_pcib_gen_softc sc_bsc;
80 static device_probe_t apb_probe;
81 static device_attach_t apb_attach;
82 static bus_alloc_resource_t apb_alloc_resource;
83 static bus_adjust_resource_t apb_adjust_resource;
85 static device_method_t apb_methods[] = {
86 /* Device interface */
87 DEVMETHOD(device_probe, apb_probe),
88 DEVMETHOD(device_attach, apb_attach),
91 DEVMETHOD(bus_alloc_resource, apb_alloc_resource),
92 DEVMETHOD(bus_adjust_resource, apb_adjust_resource),
93 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
96 DEVMETHOD(pcib_route_interrupt, ofw_pcib_gen_route_interrupt),
97 DEVMETHOD(pcib_request_feature, pcib_request_feature_allow),
99 /* ofw_bus interface */
100 DEVMETHOD(ofw_bus_get_node, ofw_pcib_gen_get_node),
105 static devclass_t pcib_devclass;
107 DEFINE_CLASS_1(pcib, apb_driver, apb_methods, sizeof(struct apb_softc),
109 EARLY_DRIVER_MODULE(apb, pci, apb_driver, pcib_devclass, 0, 0, BUS_PASS_BUS);
110 MODULE_DEPEND(apb, pci, 1, 1, 1);
112 /* APB specific registers */
113 #define APBR_IOMAP 0xde
114 #define APBR_MEMMAP 0xdf
116 /* Definitions for the mapping registers */
117 #define APB_IO_SCALE 0x200000
118 #define APB_MEM_SCALE 0x20000000
121 * Generic device interface
124 apb_probe(device_t dev)
127 if (pci_get_vendor(dev) == 0x108e && /* Sun */
128 pci_get_device(dev) == 0x5000) { /* APB */
129 device_set_desc(dev, "APB PCI-PCI bridge");
136 apb_map_print(uint8_t map, rman_res_t scale)
140 for (first = 1, i = 0; i < 8; i++) {
141 if ((map & (1 << i)) != 0) {
142 printf("%s0x%jx-0x%jx", first ? "" : ", ",
143 i * scale, (i + 1) * scale - 1);
150 apb_checkrange(uint8_t map, rman_res_t scale, rman_res_t start, rman_res_t end)
159 if ((map & (1 << i)) == 0)
165 apb_attach(device_t dev)
167 struct apb_softc *sc;
168 struct sysctl_ctx_list *sctx;
169 struct sysctl_oid *soid;
171 sc = device_get_softc(dev);
174 * Get current bridge configuration.
176 sc->sc_bsc.ops_pcib_sc.domain = pci_get_domain(dev);
177 sc->sc_bsc.ops_pcib_sc.pribus = pci_get_bus(dev);
178 pci_write_config(dev, PCIR_PRIBUS_1, sc->sc_bsc.ops_pcib_sc.pribus, 1);
179 sc->sc_bsc.ops_pcib_sc.bus.sec =
180 pci_read_config(dev, PCIR_SECBUS_1, 1);
181 sc->sc_bsc.ops_pcib_sc.bus.sub =
182 pci_read_config(dev, PCIR_SUBBUS_1, 1);
183 sc->sc_bsc.ops_pcib_sc.bridgectl =
184 pci_read_config(dev, PCIR_BRIDGECTL_1, 2);
185 sc->sc_iomap = pci_read_config(dev, APBR_IOMAP, 1);
186 sc->sc_memmap = pci_read_config(dev, APBR_MEMMAP, 1);
189 * Setup SYSCTL reporting nodes.
191 sctx = device_get_sysctl_ctx(dev);
192 soid = device_get_sysctl_tree(dev);
193 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain",
194 CTLFLAG_RD, &sc->sc_bsc.ops_pcib_sc.domain, 0,
196 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus",
197 CTLFLAG_RD, &sc->sc_bsc.ops_pcib_sc.pribus, 0,
198 "Primary bus number");
199 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus",
200 CTLFLAG_RD, &sc->sc_bsc.ops_pcib_sc.bus.sec, 0,
201 "Secondary bus number");
202 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus",
203 CTLFLAG_RD, &sc->sc_bsc.ops_pcib_sc.bus.sub, 0,
204 "Subordinate bus number");
206 ofw_pcib_gen_setup(dev);
209 device_printf(dev, " domain %d\n",
210 sc->sc_bsc.ops_pcib_sc.domain);
211 device_printf(dev, " secondary bus %d\n",
212 sc->sc_bsc.ops_pcib_sc.bus.sec);
213 device_printf(dev, " subordinate bus %d\n",
214 sc->sc_bsc.ops_pcib_sc.bus.sub);
215 device_printf(dev, " I/O decode ");
216 apb_map_print(sc->sc_iomap, APB_IO_SCALE);
218 device_printf(dev, " memory decode ");
219 apb_map_print(sc->sc_memmap, APB_MEM_SCALE);
223 device_add_child(dev, "pci", -1);
224 return (bus_generic_attach(dev));
228 * We have to trap resource allocation requests and ensure that the bridge
229 * is set up to, or capable of handling them.
231 static struct resource *
232 apb_alloc_resource(device_t dev, device_t child, int type, int *rid,
233 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
235 struct apb_softc *sc;
237 sc = device_get_softc(dev);
240 * If this is a "default" allocation against this rid, we can't work
241 * out where it's coming from (we should actually never see these) so
242 * we just have to punt.
244 if (RMAN_IS_DEFAULT_RANGE(start, end)) {
245 device_printf(dev, "can't decode default resource id %d for "
246 "%s, bypassing\n", *rid, device_get_nameunit(child));
251 * Fail the allocation for this range if it's not supported.
252 * XXX we should probably just fix up the bridge decode and
257 if (!apb_checkrange(sc->sc_iomap, APB_IO_SCALE, start, end)) {
258 device_printf(dev, "device %s requested unsupported "
259 "I/O range 0x%jx-0x%jx\n",
260 device_get_nameunit(child), start, end);
264 device_printf(sc->sc_bsc.ops_pcib_sc.dev, "device "
265 "%s requested decoded I/O range 0x%jx-0x%jx\n",
266 device_get_nameunit(child), start, end);
269 if (!apb_checkrange(sc->sc_memmap, APB_MEM_SCALE, start,
271 device_printf(dev, "device %s requested unsupported "
272 "memory range 0x%jx-0x%jx\n",
273 device_get_nameunit(child), start, end);
277 device_printf(sc->sc_bsc.ops_pcib_sc.dev, "device "
278 "%s requested decoded memory range 0x%jx-0x%jx\n",
279 device_get_nameunit(child), start, end);
285 * Bridge is OK decoding this resource, so pass it up.
287 return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
292 apb_adjust_resource(device_t dev, device_t child, int type,
293 struct resource *r, rman_res_t start, rman_res_t end)
295 struct apb_softc *sc;
297 sc = device_get_softc(dev);
300 if (!apb_checkrange(sc->sc_iomap, APB_IO_SCALE, start, end))
304 if (!apb_checkrange(sc->sc_memmap, APB_MEM_SCALE, start, end))
308 return (bus_generic_adjust_resource(dev, child, type, r, start, end));