Path: blob/main/sys/powerpc/powermac/uninorthpci.c
39536 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (C) 2002 Benno Rice.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR16* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES17* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.18* IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,19* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,20* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;21* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,22* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR23* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF24* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.25*/2627#include <sys/param.h>28#include <sys/systm.h>29#include <sys/module.h>30#include <sys/bus.h>31#include <sys/conf.h>32#include <sys/kernel.h>33#include <sys/lock.h>34#include <sys/mutex.h>35#include <sys/rman.h>3637#include <dev/ofw/openfirm.h>38#include <dev/ofw/ofw_pci.h>39#include <dev/ofw/ofw_bus.h>40#include <dev/ofw/ofw_bus_subr.h>41#include <dev/ofw/ofwpci.h>4243#include <dev/pci/pcivar.h>44#include <dev/pci/pcireg.h>4546#include <machine/bus.h>47#include <machine/intr_machdep.h>48#include <machine/md_var.h>49#include <machine/pio.h>50#include <machine/resource.h>5152#include <powerpc/powermac/uninorthvar.h>5354#include <vm/vm.h>55#include <vm/pmap.h>5657#include "pcib_if.h"5859#define UNINORTH_DEBUG 06061/*62* Device interface.63*/64static int uninorth_probe(device_t);65static int uninorth_attach(device_t);6667/*68* pcib interface.69*/70static u_int32_t uninorth_read_config(device_t, u_int, u_int, u_int,71u_int, int);72static void uninorth_write_config(device_t, u_int, u_int, u_int,73u_int, u_int32_t, int);7475/*76* Local routines.77*/78static int uninorth_enable_config(struct uninorth_softc *, u_int,79u_int, u_int, u_int);8081/*82* Driver methods.83*/84static device_method_t uninorth_methods[] = {85/* Device interface */86DEVMETHOD(device_probe, uninorth_probe),87DEVMETHOD(device_attach, uninorth_attach),8889/* pcib interface */90DEVMETHOD(pcib_read_config, uninorth_read_config),91DEVMETHOD(pcib_write_config, uninorth_write_config),9293DEVMETHOD_END94};9596DEFINE_CLASS_1(pcib, uninorth_driver, uninorth_methods,97sizeof(struct uninorth_softc), ofw_pcib_driver);98EARLY_DRIVER_MODULE(uninorth, ofwbus, uninorth_driver, 0, 0, BUS_PASS_BUS);99100static int101uninorth_probe(device_t dev)102{103const char *type, *compatible;104105type = ofw_bus_get_type(dev);106compatible = ofw_bus_get_compat(dev);107108if (type == NULL || compatible == NULL)109return (ENXIO);110111if (strcmp(type, "pci") != 0)112return (ENXIO);113114if (strcmp(compatible, "uni-north") == 0) {115device_set_desc(dev, "Apple UniNorth Host-PCI bridge");116return (0);117} else if (strcmp(compatible, "u3-agp") == 0) {118device_set_desc(dev, "Apple U3 Host-AGP bridge");119return (0);120} else if (strcmp(compatible, "u4-pcie") == 0) {121device_set_desc(dev, "IBM CPC945 PCI Express Root");122return (0);123}124125return (ENXIO);126}127128static int129uninorth_attach(device_t dev)130{131struct uninorth_softc *sc;132const char *compatible;133const char *name;134phandle_t node;135uint32_t reg[3];136uint64_t regbase;137cell_t acells;138int unit;139140node = ofw_bus_get_node(dev);141sc = device_get_softc(dev);142name = device_get_name(dev);143unit = device_get_unit(dev);144145if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8)146return (ENXIO);147148sc->sc_ver = 0;149compatible = ofw_bus_get_compat(dev);150if (strcmp(compatible, "u3-agp") == 0)151sc->sc_ver = 3;152if (strcmp(compatible, "u4-pcie") == 0)153sc->sc_ver = 4;154155acells = 1;156OF_getprop(OF_parent(node), "#address-cells", &acells, sizeof(acells));157158regbase = reg[0];159if (acells == 2) {160regbase <<= 32;161regbase |= reg[1];162}163164sc->sc_addr = (vm_offset_t)pmap_mapdev(regbase + 0x800000, PAGE_SIZE);165sc->sc_data = (vm_offset_t)pmap_mapdev(regbase + 0xc00000, PAGE_SIZE);166167if (resource_int_value(name, unit, "skipslot", &sc->sc_skipslot) != 0)168sc->sc_skipslot = -1;169170mtx_init(&sc->sc_cfg_mtx, "uninorth pcicfg", NULL, MTX_SPIN);171172return (ofw_pcib_attach(dev));173}174175static u_int32_t176uninorth_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,177int width)178{179struct uninorth_softc *sc;180vm_offset_t caoff;181u_int32_t val;182183sc = device_get_softc(dev);184caoff = sc->sc_data + (reg & 0x07);185val = 0xffffffff;186187mtx_lock_spin(&sc->sc_cfg_mtx);188if (uninorth_enable_config(sc, bus, slot, func, reg) != 0) {189switch (width) {190case 1:191val = in8rb(caoff);192break;193case 2:194val = in16rb(caoff);195break;196case 4:197val = in32rb(caoff);198break;199}200}201mtx_unlock_spin(&sc->sc_cfg_mtx);202203return (val);204}205206static void207uninorth_write_config(device_t dev, u_int bus, u_int slot, u_int func,208u_int reg, u_int32_t val, int width)209{210struct uninorth_softc *sc;211vm_offset_t caoff;212213sc = device_get_softc(dev);214caoff = sc->sc_data + (reg & 0x07);215216mtx_lock_spin(&sc->sc_cfg_mtx);217if (uninorth_enable_config(sc, bus, slot, func, reg)) {218switch (width) {219case 1:220out8rb(caoff, val);221break;222case 2:223out16rb(caoff, val);224break;225case 4:226out32rb(caoff, val);227break;228}229}230mtx_unlock_spin(&sc->sc_cfg_mtx);231}232233static int234uninorth_enable_config(struct uninorth_softc *sc, u_int bus, u_int slot,235u_int func, u_int reg)236{237uint32_t cfgval;238239mtx_assert(&sc->sc_cfg_mtx, MA_OWNED);240241if (sc->sc_skipslot == slot)242return (0);243244/*245* Issue type 0 configuration space accesses for the root bus.246*247* NOTE: On U4, issue only type 1 accesses. There is a secret248* PCI Express <-> PCI Express bridge not present in the device tree,249* and we need to route all of our configuration space through it.250*/251if (sc->pci_sc.sc_bus == bus && sc->sc_ver < 4) {252/*253* No slots less than 11 on the primary bus on U3 and lower254*/255if (slot < 11)256return (0);257258cfgval = (1 << slot) | (func << 8) | (reg & 0xfc);259} else {260cfgval = (bus << 16) | (slot << 11) | (func << 8) |261(reg & 0xfc) | 1;262}263264/* Set extended register bits on U4 */265if (sc->sc_ver == 4)266cfgval |= (reg >> 8) << 28;267268do {269out32rb(sc->sc_addr, cfgval);270} while (in32rb(sc->sc_addr) != cfgval);271272return (1);273}274275276