2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2002 Benno Rice.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
36 #include <sys/kernel.h>
38 #include <sys/mutex.h>
41 #include <dev/ofw/openfirm.h>
42 #include <dev/ofw/ofw_pci.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45 #include <dev/ofw/ofwpci.h>
47 #include <dev/pci/pcivar.h>
48 #include <dev/pci/pcireg.h>
50 #include <machine/bus.h>
51 #include <machine/intr_machdep.h>
52 #include <machine/md_var.h>
53 #include <machine/pio.h>
54 #include <machine/resource.h>
56 #include <powerpc/powermac/uninorthvar.h>
63 #define UNINORTH_DEBUG 0
68 static int uninorth_probe(device_t);
69 static int uninorth_attach(device_t);
74 static u_int32_t uninorth_read_config(device_t, u_int, u_int, u_int,
76 static void uninorth_write_config(device_t, u_int, u_int, u_int,
77 u_int, u_int32_t, int);
82 static int uninorth_enable_config(struct uninorth_softc *, u_int,
88 static device_method_t uninorth_methods[] = {
89 /* Device interface */
90 DEVMETHOD(device_probe, uninorth_probe),
91 DEVMETHOD(device_attach, uninorth_attach),
94 DEVMETHOD(pcib_read_config, uninorth_read_config),
95 DEVMETHOD(pcib_write_config, uninorth_write_config),
100 static devclass_t uninorth_devclass;
102 DEFINE_CLASS_1(pcib, uninorth_driver, uninorth_methods,
103 sizeof(struct uninorth_softc), ofw_pci_driver);
104 EARLY_DRIVER_MODULE(uninorth, ofwbus, uninorth_driver, uninorth_devclass, 0, 0,
108 uninorth_probe(device_t dev)
110 const char *type, *compatible;
112 type = ofw_bus_get_type(dev);
113 compatible = ofw_bus_get_compat(dev);
115 if (type == NULL || compatible == NULL)
118 if (strcmp(type, "pci") != 0)
121 if (strcmp(compatible, "uni-north") == 0) {
122 device_set_desc(dev, "Apple UniNorth Host-PCI bridge");
124 } else if (strcmp(compatible, "u3-agp") == 0) {
125 device_set_desc(dev, "Apple U3 Host-AGP bridge");
127 } else if (strcmp(compatible, "u4-pcie") == 0) {
128 device_set_desc(dev, "IBM CPC945 PCI Express Root");
136 uninorth_attach(device_t dev)
138 struct uninorth_softc *sc;
139 const char *compatible;
147 node = ofw_bus_get_node(dev);
148 sc = device_get_softc(dev);
149 name = device_get_name(dev);
150 unit = device_get_unit(dev);
152 if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8)
156 compatible = ofw_bus_get_compat(dev);
157 if (strcmp(compatible, "u3-agp") == 0)
159 if (strcmp(compatible, "u4-pcie") == 0)
163 OF_getprop(OF_parent(node), "#address-cells", &acells, sizeof(acells));
171 sc->sc_addr = (vm_offset_t)pmap_mapdev(regbase + 0x800000, PAGE_SIZE);
172 sc->sc_data = (vm_offset_t)pmap_mapdev(regbase + 0xc00000, PAGE_SIZE);
174 if (resource_int_value(name, unit, "skipslot", &sc->sc_skipslot) != 0)
175 sc->sc_skipslot = -1;
177 mtx_init(&sc->sc_cfg_mtx, "uninorth pcicfg", NULL, MTX_SPIN);
179 return (ofw_pci_attach(dev));
183 uninorth_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
186 struct uninorth_softc *sc;
190 sc = device_get_softc(dev);
191 caoff = sc->sc_data + (reg & 0x07);
194 mtx_lock_spin(&sc->sc_cfg_mtx);
195 if (uninorth_enable_config(sc, bus, slot, func, reg) != 0) {
208 mtx_unlock_spin(&sc->sc_cfg_mtx);
214 uninorth_write_config(device_t dev, u_int bus, u_int slot, u_int func,
215 u_int reg, u_int32_t val, int width)
217 struct uninorth_softc *sc;
220 sc = device_get_softc(dev);
221 caoff = sc->sc_data + (reg & 0x07);
223 mtx_lock_spin(&sc->sc_cfg_mtx);
224 if (uninorth_enable_config(sc, bus, slot, func, reg)) {
237 mtx_unlock_spin(&sc->sc_cfg_mtx);
241 uninorth_enable_config(struct uninorth_softc *sc, u_int bus, u_int slot,
242 u_int func, u_int reg)
246 mtx_assert(&sc->sc_cfg_mtx, MA_OWNED);
248 if (sc->sc_skipslot == slot)
252 * Issue type 0 configuration space accesses for the root bus.
254 * NOTE: On U4, issue only type 1 accesses. There is a secret
255 * PCI Express <-> PCI Express bridge not present in the device tree,
256 * and we need to route all of our configuration space through it.
258 if (sc->pci_sc.sc_bus == bus && sc->sc_ver < 4) {
260 * No slots less than 11 on the primary bus on U3 and lower
265 cfgval = (1 << slot) | (func << 8) | (reg & 0xfc);
267 cfgval = (bus << 16) | (slot << 11) | (func << 8) |
271 /* Set extended register bits on U4 */
273 cfgval |= (reg >> 8) << 28;
276 out32rb(sc->sc_addr, cfgval);
277 } while (in32rb(sc->sc_addr) != cfgval);