2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright 2003 by Peter Grehan. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/module.h>
38 #include <sys/kernel.h>
42 #include <dev/ofw/openfirm.h>
43 #include <dev/ofw/ofw_pci.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46 #include <dev/ofw/ofwpci.h>
48 #include <dev/pci/pcivar.h>
49 #include <dev/pci/pcireg.h>
51 #include <machine/bus.h>
52 #include <machine/intr_machdep.h>
53 #include <machine/md_var.h>
54 #include <machine/pio.h>
55 #include <machine/resource.h>
57 #include <powerpc/powermac/gracklevar.h>
67 static int grackle_probe(device_t);
68 static int grackle_attach(device_t);
73 static u_int32_t grackle_read_config(device_t, u_int, u_int, u_int,
75 static void grackle_write_config(device_t, u_int, u_int, u_int,
76 u_int, u_int32_t, int);
81 static int grackle_enable_config(struct grackle_softc *, u_int,
83 static void grackle_disable_config(struct grackle_softc *);
84 static int badaddr(void *, size_t);
89 static device_method_t grackle_methods[] = {
90 /* Device interface */
91 DEVMETHOD(device_probe, grackle_probe),
92 DEVMETHOD(device_attach, grackle_attach),
95 DEVMETHOD(pcib_read_config, grackle_read_config),
96 DEVMETHOD(pcib_write_config, grackle_write_config),
101 static devclass_t grackle_devclass;
102 DEFINE_CLASS_1(pcib, grackle_driver, grackle_methods,
103 sizeof(struct grackle_softc), ofw_pci_driver);
104 DRIVER_MODULE(grackle, ofwbus, grackle_driver, grackle_devclass, 0, 0);
107 grackle_probe(device_t dev)
109 const char *type, *compatible;
111 type = ofw_bus_get_type(dev);
112 compatible = ofw_bus_get_compat(dev);
114 if (type == NULL || compatible == NULL)
117 if (strcmp(type, "pci") != 0 || strcmp(compatible, "grackle") != 0)
120 device_set_desc(dev, "MPC106 (Grackle) Host-PCI bridge");
125 grackle_attach(device_t dev)
127 struct grackle_softc *sc;
129 sc = device_get_softc(dev);
132 * The Grackle PCI config addr/data registers are actually in
133 * PCI space, but since they are needed to actually probe the
134 * PCI bus, use the fact that they are also available directly
135 * on the processor bus and map them
137 sc->sc_addr = (vm_offset_t)pmap_mapdev(GRACKLE_ADDR, PAGE_SIZE);
138 sc->sc_data = (vm_offset_t)pmap_mapdev(GRACKLE_DATA, PAGE_SIZE);
140 return (ofw_pci_attach(dev));
144 grackle_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
147 struct grackle_softc *sc;
149 u_int32_t retval = 0xffffffff;
151 sc = device_get_softc(dev);
152 caoff = sc->sc_data + (reg & 0x03);
154 if (grackle_enable_config(sc, bus, slot, func, reg) != 0) {
157 * Config probes to non-existent devices on the
158 * secondary bus generates machine checks. Be sure
162 if (badaddr((void *)sc->sc_data, 4)) {
169 retval = (in8rb(caoff));
172 retval = (in16rb(caoff));
175 retval = (in32rb(caoff));
179 grackle_disable_config(sc);
185 grackle_write_config(device_t dev, u_int bus, u_int slot, u_int func,
186 u_int reg, u_int32_t val, int width)
188 struct grackle_softc *sc;
191 sc = device_get_softc(dev);
192 caoff = sc->sc_data + (reg & 0x03);
194 if (grackle_enable_config(sc, bus, slot, func, reg)) {
210 grackle_disable_config(sc);
214 grackle_enable_config(struct grackle_softc *sc, u_int bus, u_int slot,
215 u_int func, u_int reg)
220 * Unlike UniNorth, the format of the config word is the same
221 * for local (0) and remote busses.
223 cfgval = (bus << 16) | (slot << 11) | (func << 8) | (reg & 0xFC)
224 | GRACKLE_CFG_ENABLE;
226 out32rb(sc->sc_addr, cfgval);
227 (void) in32rb(sc->sc_addr);
233 grackle_disable_config(struct grackle_softc *sc)
236 * Clear the GRACKLE_CFG_ENABLE bit to prevent stray
237 * accesses from causing config cycles
239 out32rb(sc->sc_addr, 0);
243 badaddr(void *addr, size_t size)
246 jmp_buf env, *oldfaultbuf;
249 /* Get rid of any stale machine checks that have been waiting. */
250 __asm __volatile ("sync; isync");
254 oldfaultbuf = td->td_pcb->pcb_onfault;
255 td->td_pcb->pcb_onfault = &env;
257 td->td_pcb->pcb_onfault = oldfaultbuf;
258 __asm __volatile ("sync");
262 __asm __volatile ("sync");
266 x = *(volatile int8_t *)addr;
269 x = *(volatile int16_t *)addr;
272 x = *(volatile int32_t *)addr;
275 panic("badaddr: invalid size (%zd)", size);
278 /* Make sure we took the machine check, if we caused one. */
279 __asm __volatile ("sync; isync");
281 td->td_pcb->pcb_onfault = oldfaultbuf;
282 __asm __volatile ("sync"); /* To be sure. */
288 * Driver to swallow Grackle host bridges from the PCI bus side.
291 grackle_hb_probe(device_t dev)
294 if (pci_get_devid(dev) == 0x00021057) {
295 device_set_desc(dev, "Grackle Host to PCI bridge");
304 grackle_hb_attach(device_t dev)
310 static device_method_t grackle_hb_methods[] = {
311 /* Device interface */
312 DEVMETHOD(device_probe, grackle_hb_probe),
313 DEVMETHOD(device_attach, grackle_hb_attach),
318 static driver_t grackle_hb_driver = {
323 static devclass_t grackle_hb_devclass;
325 DRIVER_MODULE(grackle_hb, pci, grackle_hb_driver, grackle_hb_devclass, 0, 0);