ofw_pcibus.c revision 163146
1117119Stmm/*- 2117119Stmm * Copyright (c) 1997, Stefan Esser <se@freebsd.org> 3117119Stmm * Copyright (c) 2000, Michael Smith <msmith@freebsd.org> 4117119Stmm * Copyright (c) 2000, BSDi 5117119Stmm * Copyright (c) 2003, Thomas Moestl <tmm@FreeBSD.org> 6117119Stmm * All rights reserved. 7117119Stmm * 8117119Stmm * Redistribution and use in source and binary forms, with or without 9117119Stmm * modification, are permitted provided that the following conditions 10117119Stmm * are met: 11117119Stmm * 1. Redistributions of source code must retain the above copyright 12117119Stmm * notice unmodified, this list of conditions, and the following 13117119Stmm * disclaimer. 14117119Stmm * 2. Redistributions in binary form must reproduce the above copyright 15117119Stmm * notice, this list of conditions and the following disclaimer in the 16117119Stmm * documentation and/or other materials provided with the distribution. 17117119Stmm * 18117119Stmm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19117119Stmm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20117119Stmm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21117119Stmm * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22117119Stmm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23117119Stmm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24117119Stmm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25117119Stmm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26117119Stmm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27117119Stmm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28117119Stmm */ 29117119Stmm 30152684Smarius#include <sys/cdefs.h> 31152684Smarius__FBSDID("$FreeBSD: head/sys/sparc64/pci/ofw_pcibus.c 163146 2006-10-09 04:45:19Z kmacy $"); 32152684Smarius 33117119Stmm#include "opt_ofw_pci.h" 34133589Smarius 35117119Stmm#include <sys/param.h> 36117119Stmm#include <sys/bus.h> 37117119Stmm#include <sys/kernel.h> 38117119Stmm#include <sys/libkern.h> 39117119Stmm#include <sys/module.h> 40117119Stmm#include <sys/pciio.h> 41117119Stmm 42133589Smarius#include <dev/ofw/ofw_bus.h> 43152684Smarius#include <dev/ofw/ofw_bus_subr.h> 44133589Smarius#include <dev/ofw/ofw_pci.h> 45117119Stmm#include <dev/ofw/openfirm.h> 46117119Stmm 47117119Stmm#include <machine/bus.h> 48117119Stmm#include <machine/bus_common.h> 49163146Skmacy#ifndef SUN4V 50117119Stmm#include <machine/cache.h> 51163146Skmacy#endif 52117119Stmm#include <machine/iommureg.h> 53117119Stmm#include <machine/resource.h> 54117119Stmm 55117119Stmm#include <dev/pci/pcireg.h> 56117119Stmm#include <dev/pci/pcivar.h> 57117119Stmm#include <dev/pci/pci_private.h> 58117119Stmm 59117119Stmm#include <sparc64/pci/ofw_pci.h> 60117119Stmm 61117119Stmm#include "pcib_if.h" 62117119Stmm#include "pci_if.h" 63117119Stmm 64117119Stmm/* Helper functions. */ 65117119Stmmstatic void ofw_pcibus_setup_device(device_t, u_int, u_int, u_int); 66117119Stmm 67117119Stmm/* Methods. */ 68117119Stmmstatic device_probe_t ofw_pcibus_probe; 69117119Stmmstatic device_attach_t ofw_pcibus_attach; 70117119Stmmstatic pci_assign_interrupt_t ofw_pcibus_assign_interrupt; 71152684Smariusstatic ofw_bus_get_devinfo_t ofw_pcibus_get_devinfo; 72117119Stmm 73117119Stmmstatic device_method_t ofw_pcibus_methods[] = { 74117119Stmm /* Device interface */ 75117119Stmm DEVMETHOD(device_probe, ofw_pcibus_probe), 76117119Stmm DEVMETHOD(device_attach, ofw_pcibus_attach), 77117119Stmm 78117119Stmm /* Bus interface */ 79117119Stmm 80117119Stmm /* PCI interface */ 81117119Stmm DEVMETHOD(pci_assign_interrupt, ofw_pcibus_assign_interrupt), 82117119Stmm 83133589Smarius /* ofw_bus interface */ 84152684Smarius DEVMETHOD(ofw_bus_get_devinfo, ofw_pcibus_get_devinfo), 85152684Smarius DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 86152684Smarius DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 87152684Smarius DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 88152684Smarius DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 89152684Smarius DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 90117119Stmm 91117119Stmm { 0, 0 } 92117119Stmm}; 93117119Stmm 94117119Stmmstruct ofw_pcibus_devinfo { 95117119Stmm struct pci_devinfo opd_dinfo; 96152684Smarius struct ofw_bus_devinfo opd_obdinfo; 97117119Stmm}; 98117119Stmm 99117119Stmmstruct ofw_pcibus_softc { 100117119Stmm phandle_t ops_node; 101117119Stmm}; 102117119Stmm 103154600Sjhbstatic devclass_t pci_devclass; 104117119Stmm 105154600SjhbDEFINE_CLASS_1(pci, ofw_pcibus_driver, ofw_pcibus_methods, 106154600Sjhb sizeof(struct ofw_pcibus_softc), pci_driver); 107117119StmmDRIVER_MODULE(ofw_pcibus, pcib, ofw_pcibus_driver, pci_devclass, 0, 0); 108117119StmmMODULE_VERSION(ofw_pcibus, 1); 109117119StmmMODULE_DEPEND(ofw_pcibus, pci, 1, 1, 1); 110117119Stmm 111117119Stmmstatic int 112117119Stmmofw_pcibus_probe(device_t dev) 113117119Stmm{ 114117119Stmm 115133589Smarius if (ofw_bus_get_node(dev) == 0) 116117119Stmm return (ENXIO); 117117119Stmm device_set_desc(dev, "OFW PCI bus"); 118117119Stmm 119117119Stmm return (0); 120117119Stmm} 121117119Stmm 122117119Stmm/* 123117119Stmm * Perform miscellaneous setups the firmware usually does not do for us. 124117119Stmm */ 125117119Stmmstatic void 126117119Stmmofw_pcibus_setup_device(device_t bridge, u_int busno, u_int slot, u_int func) 127117119Stmm{ 128163146Skmacy u_int lat; 129163146Skmacy#ifndef SUN4V 130163146Skmacy u_int clnsz; 131163146Skmacy#endif 132117119Stmm /* 133117119Stmm * Initialize the latency timer register for busmaster devices to work 134117119Stmm * properly. This is another task which the firmware does not always 135117119Stmm * perform. The Min_Gnt register can be used to compute it's recommended 136117119Stmm * value: it contains the desired latency in units of 1/4 us. To 137117119Stmm * calculate the correct latency timer value, a bus clock of 33MHz and 138117119Stmm * no wait states should be assumed. 139117119Stmm */ 140117119Stmm lat = PCIB_READ_CONFIG(bridge, busno, slot, func, PCIR_MINGNT, 1) * 141117119Stmm 33 / 4; 142117119Stmm if (lat != 0) { 143117119Stmm#ifdef OFW_PCI_DEBUG 144117119Stmm device_printf(bridge, "device %d/%d/%d: latency timer %d -> " 145117119Stmm "%d\n", busno, slot, func, 146117119Stmm PCIB_READ_CONFIG(bridge, busno, slot, func, 147117119Stmm PCIR_LATTIMER, 1), lat); 148117119Stmm#endif /* OFW_PCI_DEBUG */ 149117119Stmm PCIB_WRITE_CONFIG(bridge, busno, slot, func, 150133774Smarius PCIR_LATTIMER, min(lat, 255), 1); 151117119Stmm } 152117119Stmm 153163146Skmacy#ifndef SUN4V 154117119Stmm /* 155117119Stmm * Compute a value to write into the cache line size register. 156117119Stmm * The role of the streaming cache is unclear in write invalidate 157117119Stmm * transfers, so it is made sure that it's line size is always reached. 158117119Stmm */ 159133774Smarius clnsz = max(cache.ec_linesize, STRBUF_LINESZ); 160117119Stmm KASSERT((clnsz / STRBUF_LINESZ) * STRBUF_LINESZ == clnsz && 161117119Stmm (clnsz / cache.ec_linesize) * cache.ec_linesize == clnsz && 162117119Stmm (clnsz / 4) * 4 == clnsz, ("bogus cache line size %d", clnsz)); 163117119Stmm PCIB_WRITE_CONFIG(bridge, busno, slot, func, PCIR_CACHELNSZ, 164117119Stmm clnsz / 4, 1); 165117119Stmm 166163146Skmacy#endif 167117119Stmm /* 168117119Stmm * The preset in the intline register is usually wrong. Reset it to 255, 169117119Stmm * so that the PCI code will reroute the interrupt if needed. 170117119Stmm */ 171117119Stmm PCIB_WRITE_CONFIG(bridge, busno, slot, func, PCIR_INTLINE, 172117119Stmm PCI_INVALID_IRQ, 1); 173117119Stmm} 174117119Stmm 175117119Stmmstatic int 176117119Stmmofw_pcibus_attach(device_t dev) 177117119Stmm{ 178153057Smarius device_t pcib; 179117119Stmm struct ofw_pci_register pcir; 180117119Stmm struct ofw_pcibus_devinfo *dinfo; 181117119Stmm phandle_t node, child; 182117119Stmm u_int slot, busno, func; 183117119Stmm 184153057Smarius pcib = device_get_parent(dev); 185153057Smarius 186117119Stmm /* 187117119Stmm * Ask the bridge for the bus number - in some cases, we need to 188117119Stmm * renumber buses, so the firmware information cannot be trusted. 189117119Stmm */ 190117119Stmm busno = pcib_get_bus(dev); 191117119Stmm if (bootverbose) 192117119Stmm device_printf(dev, "physical bus=%d\n", busno); 193117119Stmm 194133589Smarius node = ofw_bus_get_node(dev); 195117119Stmm for (child = OF_child(node); child != 0; child = OF_peer(child)) { 196152684Smarius if (OF_getprop(child, "reg", &pcir, sizeof(pcir)) == -1) 197140512Smarius continue; 198117119Stmm slot = OFW_PCI_PHYS_HI_DEVICE(pcir.phys_hi); 199117119Stmm func = OFW_PCI_PHYS_HI_FUNCTION(pcir.phys_hi); 200117119Stmm ofw_pcibus_setup_device(pcib, busno, slot, func); 201117119Stmm dinfo = (struct ofw_pcibus_devinfo *)pci_read_device(pcib, 202117119Stmm busno, slot, func, sizeof(*dinfo)); 203152684Smarius if (dinfo == NULL) 204152684Smarius continue; 205152684Smarius if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, child) != 206152684Smarius 0) { 207152684Smarius pci_freecfg((struct pci_devinfo *)dinfo); 208152684Smarius continue; 209152684Smarius } 210152684Smarius pci_add_child(dev, (struct pci_devinfo *)dinfo); 211117119Stmm } 212117119Stmm 213117119Stmm return (bus_generic_attach(dev)); 214117119Stmm} 215117119Stmm 216117119Stmmstatic int 217117119Stmmofw_pcibus_assign_interrupt(device_t dev, device_t child) 218117119Stmm{ 219117119Stmm ofw_pci_intr_t intr; 220117119Stmm int isz; 221117119Stmm 222152684Smarius isz = OF_getprop(ofw_bus_get_node(child), "interrupts", &intr, 223152684Smarius sizeof(intr)); 224117119Stmm if (isz != sizeof(intr)) { 225117119Stmm /* No property; our best guess is the intpin. */ 226152684Smarius intr = pci_get_intpin(child); 227117119Stmm } else if (intr >= 255) { 228117119Stmm /* 229117119Stmm * A fully specified interrupt (including IGN), as present on 230117119Stmm * SPARCengine Ultra AX and e450. Extract the INO and return it. 231117119Stmm */ 232117119Stmm return (INTINO(intr)); 233117119Stmm } 234117119Stmm /* 235117119Stmm * If we got intr from a property, it may or may not be an intpin. 236117119Stmm * For on-board devices, it frequently is not, and is completely out 237117119Stmm * of the valid intpin range. For PCI slots, it hopefully is, otherwise 238117119Stmm * we will have trouble interfacing with non-OFW buses such as cardbus. 239117119Stmm * Since we cannot tell which it is without violating layering, we 240117119Stmm * will always use the route_interrupt method, and treat exceptions on 241117119Stmm * the level they become apparent. 242117119Stmm */ 243117119Stmm return (PCIB_ROUTE_INTERRUPT(device_get_parent(dev), child, intr)); 244117119Stmm} 245117119Stmm 246152684Smariusstatic const struct ofw_bus_devinfo * 247152684Smariusofw_pcibus_get_devinfo(device_t bus, device_t dev) 248133589Smarius{ 249133589Smarius struct ofw_pcibus_devinfo *dinfo; 250133589Smarius 251133589Smarius dinfo = device_get_ivars(dev); 252152684Smarius return (&dinfo->opd_obdinfo); 253133589Smarius} 254