1ca2c1931SNathan Whitehorn /*- 271e3c308SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 371e3c308SPedro F. Giffuni * 4ca2c1931SNathan Whitehorn * Copyright (C) 2002 Benno Rice. 5ca2c1931SNathan Whitehorn * All rights reserved. 6ca2c1931SNathan Whitehorn * 7ca2c1931SNathan Whitehorn * Redistribution and use in source and binary forms, with or without 8ca2c1931SNathan Whitehorn * modification, are permitted provided that the following conditions 9ca2c1931SNathan Whitehorn * are met: 10ca2c1931SNathan Whitehorn * 1. Redistributions of source code must retain the above copyright 11ca2c1931SNathan Whitehorn * notice, this list of conditions and the following disclaimer. 12ca2c1931SNathan Whitehorn * 2. Redistributions in binary form must reproduce the above copyright 13ca2c1931SNathan Whitehorn * notice, this list of conditions and the following disclaimer in the 14ca2c1931SNathan Whitehorn * documentation and/or other materials provided with the distribution. 15ca2c1931SNathan Whitehorn * 16ca2c1931SNathan Whitehorn * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR 17ca2c1931SNathan Whitehorn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18ca2c1931SNathan Whitehorn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19ca2c1931SNathan Whitehorn * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20ca2c1931SNathan Whitehorn * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21ca2c1931SNathan Whitehorn * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22ca2c1931SNathan Whitehorn * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23ca2c1931SNathan Whitehorn * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24ca2c1931SNathan Whitehorn * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25ca2c1931SNathan Whitehorn * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26ca2c1931SNathan Whitehorn */ 27ca2c1931SNathan Whitehorn 284b7ec270SMarius Strobl #include <sys/cdefs.h> 294b7ec270SMarius Strobl __FBSDID("$FreeBSD$"); 304b7ec270SMarius Strobl 31ca2c1931SNathan Whitehorn #include <sys/param.h> 32ca2c1931SNathan Whitehorn #include <sys/systm.h> 33ca2c1931SNathan Whitehorn #include <sys/module.h> 34ca2c1931SNathan Whitehorn #include <sys/bus.h> 35ca2c1931SNathan Whitehorn #include <sys/conf.h> 36ca2c1931SNathan Whitehorn #include <sys/kernel.h> 37a1df0d95SLandon J. Fuller #include <sys/lock.h> 38a1df0d95SLandon J. Fuller #include <sys/mutex.h> 39c43a8674SZbigniew Bodek #include <sys/rman.h> 40ca2c1931SNathan Whitehorn 41ca2c1931SNathan Whitehorn #include <dev/ofw/openfirm.h> 4236e9c2ceSZbigniew Bodek #include <dev/ofw/ofw_pci.h> 43ca2c1931SNathan Whitehorn #include <dev/ofw/ofw_bus.h> 44ca2c1931SNathan Whitehorn #include <dev/ofw/ofw_bus_subr.h> 45c43a8674SZbigniew Bodek #include <dev/ofw/ofwpci.h> 46ca2c1931SNathan Whitehorn 47ca2c1931SNathan Whitehorn #include <dev/pci/pcivar.h> 48ca2c1931SNathan Whitehorn #include <dev/pci/pcireg.h> 49ca2c1931SNathan Whitehorn 50ca2c1931SNathan Whitehorn #include <machine/bus.h> 51ca2c1931SNathan Whitehorn #include <machine/intr_machdep.h> 52ca2c1931SNathan Whitehorn #include <machine/md_var.h> 53ca2c1931SNathan Whitehorn #include <machine/pio.h> 54ca2c1931SNathan Whitehorn #include <machine/resource.h> 55ca2c1931SNathan Whitehorn 56ca2c1931SNathan Whitehorn #include <powerpc/powermac/uninorthvar.h> 57ca2c1931SNathan Whitehorn 58ca2c1931SNathan Whitehorn #include <vm/vm.h> 59ca2c1931SNathan Whitehorn #include <vm/pmap.h> 60ca2c1931SNathan Whitehorn 61ca2c1931SNathan Whitehorn #include "pcib_if.h" 62ca2c1931SNathan Whitehorn 63ca2c1931SNathan Whitehorn #define UNINORTH_DEBUG 0 64ca2c1931SNathan Whitehorn 65ca2c1931SNathan Whitehorn /* 66ca2c1931SNathan Whitehorn * Device interface. 67ca2c1931SNathan Whitehorn */ 68ca2c1931SNathan Whitehorn static int uninorth_probe(device_t); 69ca2c1931SNathan Whitehorn static int uninorth_attach(device_t); 70ca2c1931SNathan Whitehorn 71ca2c1931SNathan Whitehorn /* 72ca2c1931SNathan Whitehorn * pcib interface. 73ca2c1931SNathan Whitehorn */ 74ca2c1931SNathan Whitehorn static u_int32_t uninorth_read_config(device_t, u_int, u_int, u_int, 75ca2c1931SNathan Whitehorn u_int, int); 76ca2c1931SNathan Whitehorn static void uninorth_write_config(device_t, u_int, u_int, u_int, 77ca2c1931SNathan Whitehorn u_int, u_int32_t, int); 78ca2c1931SNathan Whitehorn 79ca2c1931SNathan Whitehorn /* 80ca2c1931SNathan Whitehorn * Local routines. 81ca2c1931SNathan Whitehorn */ 82ca2c1931SNathan Whitehorn static int uninorth_enable_config(struct uninorth_softc *, u_int, 83ca2c1931SNathan Whitehorn u_int, u_int, u_int); 84ca2c1931SNathan Whitehorn 85ca2c1931SNathan Whitehorn /* 86ca2c1931SNathan Whitehorn * Driver methods. 87ca2c1931SNathan Whitehorn */ 88ca2c1931SNathan Whitehorn static device_method_t uninorth_methods[] = { 89ca2c1931SNathan Whitehorn /* Device interface */ 90ca2c1931SNathan Whitehorn DEVMETHOD(device_probe, uninorth_probe), 91ca2c1931SNathan Whitehorn DEVMETHOD(device_attach, uninorth_attach), 92ca2c1931SNathan Whitehorn 93ca2c1931SNathan Whitehorn /* pcib interface */ 94ca2c1931SNathan Whitehorn DEVMETHOD(pcib_read_config, uninorth_read_config), 95ca2c1931SNathan Whitehorn DEVMETHOD(pcib_write_config, uninorth_write_config), 96ca2c1931SNathan Whitehorn 974b7ec270SMarius Strobl DEVMETHOD_END 98ca2c1931SNathan Whitehorn }; 99ca2c1931SNathan Whitehorn 100ca2c1931SNathan Whitehorn static devclass_t uninorth_devclass; 101ca2c1931SNathan Whitehorn 1029a35e64eSNathan Whitehorn DEFINE_CLASS_1(pcib, uninorth_driver, uninorth_methods, 103*24042910SMarcin Wojtas sizeof(struct uninorth_softc), ofw_pcib_driver); 104bfed756aSJustin Hibbits EARLY_DRIVER_MODULE(uninorth, ofwbus, uninorth_driver, uninorth_devclass, 0, 0, 105bfed756aSJustin Hibbits BUS_PASS_BUS); 106ca2c1931SNathan Whitehorn 107ca2c1931SNathan Whitehorn static int 108ca2c1931SNathan Whitehorn uninorth_probe(device_t dev) 109ca2c1931SNathan Whitehorn { 110ca2c1931SNathan Whitehorn const char *type, *compatible; 111ca2c1931SNathan Whitehorn 112ca2c1931SNathan Whitehorn type = ofw_bus_get_type(dev); 113ca2c1931SNathan Whitehorn compatible = ofw_bus_get_compat(dev); 114ca2c1931SNathan Whitehorn 115ca2c1931SNathan Whitehorn if (type == NULL || compatible == NULL) 116ca2c1931SNathan Whitehorn return (ENXIO); 117ca2c1931SNathan Whitehorn 118ca2c1931SNathan Whitehorn if (strcmp(type, "pci") != 0) 119ca2c1931SNathan Whitehorn return (ENXIO); 120ca2c1931SNathan Whitehorn 121ca2c1931SNathan Whitehorn if (strcmp(compatible, "uni-north") == 0) { 122ca2c1931SNathan Whitehorn device_set_desc(dev, "Apple UniNorth Host-PCI bridge"); 123ca2c1931SNathan Whitehorn return (0); 124ca2c1931SNathan Whitehorn } else if (strcmp(compatible, "u3-agp") == 0) { 125ca2c1931SNathan Whitehorn device_set_desc(dev, "Apple U3 Host-AGP bridge"); 126ca2c1931SNathan Whitehorn return (0); 127ca2c1931SNathan Whitehorn } else if (strcmp(compatible, "u4-pcie") == 0) { 128ca2c1931SNathan Whitehorn device_set_desc(dev, "IBM CPC945 PCI Express Root"); 129ca2c1931SNathan Whitehorn return (0); 130ca2c1931SNathan Whitehorn } 131ca2c1931SNathan Whitehorn 132ca2c1931SNathan Whitehorn return (ENXIO); 133ca2c1931SNathan Whitehorn } 134ca2c1931SNathan Whitehorn 135ca2c1931SNathan Whitehorn static int 136ca2c1931SNathan Whitehorn uninorth_attach(device_t dev) 137ca2c1931SNathan Whitehorn { 138ca2c1931SNathan Whitehorn struct uninorth_softc *sc; 139ca2c1931SNathan Whitehorn const char *compatible; 140a1df0d95SLandon J. Fuller const char *name; 141ca2c1931SNathan Whitehorn phandle_t node; 142ffce55feSNathan Whitehorn uint32_t reg[3]; 143ffce55feSNathan Whitehorn uint64_t regbase; 144ffce55feSNathan Whitehorn cell_t acells; 145a1df0d95SLandon J. Fuller int unit; 146ca2c1931SNathan Whitehorn 147ca2c1931SNathan Whitehorn node = ofw_bus_get_node(dev); 148ca2c1931SNathan Whitehorn sc = device_get_softc(dev); 149a1df0d95SLandon J. Fuller name = device_get_name(dev); 150a1df0d95SLandon J. Fuller unit = device_get_unit(dev); 151ca2c1931SNathan Whitehorn 152ca2c1931SNathan Whitehorn if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8) 153ca2c1931SNathan Whitehorn return (ENXIO); 154ca2c1931SNathan Whitehorn 155ca2c1931SNathan Whitehorn sc->sc_ver = 0; 156ca2c1931SNathan Whitehorn compatible = ofw_bus_get_compat(dev); 157ca2c1931SNathan Whitehorn if (strcmp(compatible, "u3-agp") == 0) 158ca2c1931SNathan Whitehorn sc->sc_ver = 3; 159ca2c1931SNathan Whitehorn if (strcmp(compatible, "u4-pcie") == 0) 160ca2c1931SNathan Whitehorn sc->sc_ver = 4; 161ca2c1931SNathan Whitehorn 162ffce55feSNathan Whitehorn acells = 1; 163ffce55feSNathan Whitehorn OF_getprop(OF_parent(node), "#address-cells", &acells, sizeof(acells)); 164ffce55feSNathan Whitehorn 165ffce55feSNathan Whitehorn regbase = reg[0]; 166ffce55feSNathan Whitehorn if (acells == 2) { 167ffce55feSNathan Whitehorn regbase <<= 32; 168ffce55feSNathan Whitehorn regbase |= reg[1]; 169ca2c1931SNathan Whitehorn } 170ca2c1931SNathan Whitehorn 171ffce55feSNathan Whitehorn sc->sc_addr = (vm_offset_t)pmap_mapdev(regbase + 0x800000, PAGE_SIZE); 172ffce55feSNathan Whitehorn sc->sc_data = (vm_offset_t)pmap_mapdev(regbase + 0xc00000, PAGE_SIZE); 173ffce55feSNathan Whitehorn 174a1df0d95SLandon J. Fuller if (resource_int_value(name, unit, "skipslot", &sc->sc_skipslot) != 0) 175a1df0d95SLandon J. Fuller sc->sc_skipslot = -1; 176a1df0d95SLandon J. Fuller 177a1df0d95SLandon J. Fuller mtx_init(&sc->sc_cfg_mtx, "uninorth pcicfg", NULL, MTX_SPIN); 178a1df0d95SLandon J. Fuller 179*24042910SMarcin Wojtas return (ofw_pcib_attach(dev)); 180ca2c1931SNathan Whitehorn } 181ca2c1931SNathan Whitehorn 182ca2c1931SNathan Whitehorn static u_int32_t 183ca2c1931SNathan Whitehorn uninorth_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg, 184ca2c1931SNathan Whitehorn int width) 185ca2c1931SNathan Whitehorn { 186ca2c1931SNathan Whitehorn struct uninorth_softc *sc; 187ca2c1931SNathan Whitehorn vm_offset_t caoff; 188a1df0d95SLandon J. Fuller u_int32_t val; 189ca2c1931SNathan Whitehorn 190ca2c1931SNathan Whitehorn sc = device_get_softc(dev); 191ca2c1931SNathan Whitehorn caoff = sc->sc_data + (reg & 0x07); 192a1df0d95SLandon J. Fuller val = 0xffffffff; 193ca2c1931SNathan Whitehorn 194a1df0d95SLandon J. Fuller mtx_lock_spin(&sc->sc_cfg_mtx); 195ca2c1931SNathan Whitehorn if (uninorth_enable_config(sc, bus, slot, func, reg) != 0) { 196ca2c1931SNathan Whitehorn switch (width) { 197ca2c1931SNathan Whitehorn case 1: 198a1df0d95SLandon J. Fuller val = in8rb(caoff); 199ca2c1931SNathan Whitehorn break; 200ca2c1931SNathan Whitehorn case 2: 201a1df0d95SLandon J. Fuller val = in16rb(caoff); 202ca2c1931SNathan Whitehorn break; 203ca2c1931SNathan Whitehorn case 4: 204a1df0d95SLandon J. Fuller val = in32rb(caoff); 205ca2c1931SNathan Whitehorn break; 206ca2c1931SNathan Whitehorn } 207ca2c1931SNathan Whitehorn } 208a1df0d95SLandon J. Fuller mtx_unlock_spin(&sc->sc_cfg_mtx); 209ca2c1931SNathan Whitehorn 210a1df0d95SLandon J. Fuller return (val); 211ca2c1931SNathan Whitehorn } 212ca2c1931SNathan Whitehorn 213ca2c1931SNathan Whitehorn static void 214ca2c1931SNathan Whitehorn uninorth_write_config(device_t dev, u_int bus, u_int slot, u_int func, 215ca2c1931SNathan Whitehorn u_int reg, u_int32_t val, int width) 216ca2c1931SNathan Whitehorn { 217ca2c1931SNathan Whitehorn struct uninorth_softc *sc; 218ca2c1931SNathan Whitehorn vm_offset_t caoff; 219ca2c1931SNathan Whitehorn 220ca2c1931SNathan Whitehorn sc = device_get_softc(dev); 221ca2c1931SNathan Whitehorn caoff = sc->sc_data + (reg & 0x07); 222ca2c1931SNathan Whitehorn 223a1df0d95SLandon J. Fuller mtx_lock_spin(&sc->sc_cfg_mtx); 224ca2c1931SNathan Whitehorn if (uninorth_enable_config(sc, bus, slot, func, reg)) { 225ca2c1931SNathan Whitehorn switch (width) { 226ca2c1931SNathan Whitehorn case 1: 227ca2c1931SNathan Whitehorn out8rb(caoff, val); 228ca2c1931SNathan Whitehorn break; 229ca2c1931SNathan Whitehorn case 2: 230ca2c1931SNathan Whitehorn out16rb(caoff, val); 231ca2c1931SNathan Whitehorn break; 232ca2c1931SNathan Whitehorn case 4: 233ca2c1931SNathan Whitehorn out32rb(caoff, val); 234ca2c1931SNathan Whitehorn break; 235ca2c1931SNathan Whitehorn } 236ca2c1931SNathan Whitehorn } 237a1df0d95SLandon J. Fuller mtx_unlock_spin(&sc->sc_cfg_mtx); 238ca2c1931SNathan Whitehorn } 239ca2c1931SNathan Whitehorn 240ca2c1931SNathan Whitehorn static int 241ca2c1931SNathan Whitehorn uninorth_enable_config(struct uninorth_softc *sc, u_int bus, u_int slot, 242ca2c1931SNathan Whitehorn u_int func, u_int reg) 243ca2c1931SNathan Whitehorn { 244ca2c1931SNathan Whitehorn uint32_t cfgval; 245ca2c1931SNathan Whitehorn 246a1df0d95SLandon J. Fuller mtx_assert(&sc->sc_cfg_mtx, MA_OWNED); 247a1df0d95SLandon J. Fuller 248a1df0d95SLandon J. Fuller if (sc->sc_skipslot == slot) 249ca2c1931SNathan Whitehorn return (0); 250ca2c1931SNathan Whitehorn 251ca2c1931SNathan Whitehorn /* 252ca2c1931SNathan Whitehorn * Issue type 0 configuration space accesses for the root bus. 253ca2c1931SNathan Whitehorn * 254ca2c1931SNathan Whitehorn * NOTE: On U4, issue only type 1 accesses. There is a secret 255ca2c1931SNathan Whitehorn * PCI Express <-> PCI Express bridge not present in the device tree, 256ca2c1931SNathan Whitehorn * and we need to route all of our configuration space through it. 257ca2c1931SNathan Whitehorn */ 2589a35e64eSNathan Whitehorn if (sc->pci_sc.sc_bus == bus && sc->sc_ver < 4) { 259ca2c1931SNathan Whitehorn /* 260ca2c1931SNathan Whitehorn * No slots less than 11 on the primary bus on U3 and lower 261ca2c1931SNathan Whitehorn */ 262ca2c1931SNathan Whitehorn if (slot < 11) 263ca2c1931SNathan Whitehorn return (0); 264ca2c1931SNathan Whitehorn 265ca2c1931SNathan Whitehorn cfgval = (1 << slot) | (func << 8) | (reg & 0xfc); 266ca2c1931SNathan Whitehorn } else { 267ca2c1931SNathan Whitehorn cfgval = (bus << 16) | (slot << 11) | (func << 8) | 268ca2c1931SNathan Whitehorn (reg & 0xfc) | 1; 269ca2c1931SNathan Whitehorn } 270ca2c1931SNathan Whitehorn 271ca2c1931SNathan Whitehorn /* Set extended register bits on U4 */ 272ca2c1931SNathan Whitehorn if (sc->sc_ver == 4) 273ca2c1931SNathan Whitehorn cfgval |= (reg >> 8) << 28; 274ca2c1931SNathan Whitehorn 275ca2c1931SNathan Whitehorn do { 276ca2c1931SNathan Whitehorn out32rb(sc->sc_addr, cfgval); 277ca2c1931SNathan Whitehorn } while (in32rb(sc->sc_addr) != cfgval); 278ca2c1931SNathan Whitehorn 279ca2c1931SNathan Whitehorn return (1); 280ca2c1931SNathan Whitehorn } 281