1 /* $NetBSD: ppb.c,v 1.54 2014/09/24 10:57:03 msaitoh Exp $ */ 2 3 /* 4 * Copyright (c) 1996, 1998 Christopher G. Demetriou. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Christopher G. Demetriou 17 * for the NetBSD Project. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: ppb.c,v 1.54 2014/09/24 10:57:03 msaitoh Exp $"); 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/kernel.h> 39 #include <sys/device.h> 40 41 #include <dev/pci/pcireg.h> 42 #include <dev/pci/pcivar.h> 43 #include <dev/pci/ppbreg.h> 44 #include <dev/pci/pcidevs.h> 45 46 #define PCIE_SLCSR_NOTIFY_MASK \ 47 (PCIE_SLCSR_ABE | PCIE_SLCSR_PFE | PCIE_SLCSR_MSE | \ 48 PCIE_SLCSR_PDE | PCIE_SLCSR_CCE | PCIE_SLCSR_HPE) 49 50 struct ppb_softc { 51 device_t sc_dev; /* generic device glue */ 52 pci_chipset_tag_t sc_pc; /* our PCI chipset... */ 53 pcitag_t sc_tag; /* ...and tag. */ 54 55 pcireg_t sc_pciconfext[48]; 56 }; 57 58 static const char pcie_linkspeed_strings[4][5] = { 59 "1.25", "2.5", "5.0", "8.0", 60 }; 61 62 static bool ppb_resume(device_t, const pmf_qual_t *); 63 static bool ppb_suspend(device_t, const pmf_qual_t *); 64 65 static int 66 ppbmatch(device_t parent, cfdata_t match, void *aux) 67 { 68 struct pci_attach_args *pa = aux; 69 70 /* 71 * Check the ID register to see that it's a PCI bridge. 72 * If it is, we assume that we can deal with it; it _should_ 73 * work in a standardized way... 74 */ 75 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE && 76 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_BRIDGE_PCI) 77 return 1; 78 79 #ifdef __powerpc__ 80 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_PROCESSOR && 81 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_PROCESSOR_POWERPC) { 82 pcireg_t bhlc = pci_conf_read(pa->pa_pc, pa->pa_tag, 83 PCI_BHLC_REG); 84 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_FREESCALE 85 && PCI_HDRTYPE(bhlc) == PCI_HDRTYPE_RC) 86 return 1; 87 } 88 #endif 89 90 #ifdef _MIPS_PADDR_T_64BIT 91 /* The LDT HB acts just like a PPB. */ 92 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SIBYTE 93 && PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SIBYTE_BCM1250_LDTHB) 94 return 1; 95 #endif 96 97 return 0; 98 } 99 100 static void 101 ppb_fix_pcie(device_t self) 102 { 103 struct ppb_softc *sc = device_private(self); 104 pcireg_t reg; 105 int off; 106 107 if (!pci_get_capability(sc->sc_pc, sc->sc_tag, PCI_CAP_PCIEXPRESS, 108 &off, ®)) 109 return; /* Not a PCIe device */ 110 111 aprint_normal_dev(self, "PCI Express capability version "); 112 switch (reg & PCIE_XCAP_VER_MASK) { 113 case PCIE_XCAP_VER_1: 114 aprint_normal("1"); 115 break; 116 case PCIE_XCAP_VER_2: 117 aprint_normal("2"); 118 break; 119 default: 120 aprint_normal_dev(self, 121 "unsupported (0x%" PRIxMAX ")\n", 122 __SHIFTOUT(reg, PCIE_XCAP_VER_MASK)); 123 return; 124 } 125 aprint_normal(" <"); 126 switch (reg & PCIE_XCAP_TYPE_MASK) { 127 case PCIE_XCAP_TYPE_PCIE_DEV: 128 aprint_normal("PCI-E Endpoint device"); 129 break; 130 case PCIE_XCAP_TYPE_PCI_DEV: 131 aprint_normal("Legacy PCI-E Endpoint device"); 132 break; 133 case PCIE_XCAP_TYPE_ROOT: 134 aprint_normal("Root Port of PCI-E Root Complex"); 135 break; 136 case PCIE_XCAP_TYPE_UP: 137 aprint_normal("Upstream Port of PCI-E Switch"); 138 break; 139 case PCIE_XCAP_TYPE_DOWN: 140 aprint_normal("Downstream Port of PCI-E Switch"); 141 break; 142 case PCIE_XCAP_TYPE_PCIE2PCI: 143 aprint_normal("PCI-E to PCI/PCI-X Bridge"); 144 break; 145 case PCIE_XCAP_TYPE_PCI2PCIE: 146 aprint_normal("PCI/PCI-X to PCI-E Bridge"); 147 break; 148 default: 149 aprint_normal("Device/Port Type 0x%" PRIxMAX, 150 __SHIFTOUT(reg, PCIE_XCAP_TYPE_MASK)); 151 break; 152 } 153 154 switch (reg & PCIE_XCAP_TYPE_MASK) { 155 case PCIE_XCAP_TYPE_ROOT: 156 case PCIE_XCAP_TYPE_DOWN: 157 case PCIE_XCAP_TYPE_PCI2PCIE: 158 reg = pci_conf_read(sc->sc_pc, sc->sc_tag, off + PCIE_LCAP); 159 u_int mlw = __SHIFTOUT(reg, PCIE_LCAP_MAX_WIDTH); 160 u_int mls = __SHIFTOUT(reg, PCIE_LCAP_MAX_SPEED); 161 162 if (mls < __arraycount(pcie_linkspeed_strings)) { 163 aprint_normal("> x%d @ %sGT/s\n", 164 mlw, pcie_linkspeed_strings[mls]); 165 } else { 166 aprint_normal("> x%d @ %d.%dGT/s\n", 167 mlw, (mls * 25) / 10, (mls * 25) % 10); 168 } 169 170 reg = pci_conf_read(sc->sc_pc, sc->sc_tag, off + PCIE_LCSR); 171 if (reg & PCIE_LCSR_DLACTIVE) { /* DLLA */ 172 u_int lw = __SHIFTOUT(reg, PCIE_LCSR_NLW); 173 u_int ls = __SHIFTOUT(reg, PCIE_LCSR_LINKSPEED); 174 175 if (lw != mlw || ls != mls) { 176 if (ls < __arraycount(pcie_linkspeed_strings)) { 177 aprint_normal_dev(self, 178 "link is x%d @ %sGT/s\n", 179 lw, pcie_linkspeed_strings[ls]); 180 } else { 181 aprint_normal_dev(self, 182 "link is x%d @ %d.%dGT/s\n", 183 lw, (ls * 25) / 10, (ls * 25) % 10); 184 } 185 } 186 } 187 break; 188 default: 189 aprint_normal(">\n"); 190 break; 191 } 192 193 reg = pci_conf_read(sc->sc_pc, sc->sc_tag, off + PCIE_SLCSR); 194 if (reg & PCIE_SLCSR_NOTIFY_MASK) { 195 aprint_debug_dev(self, "disabling notification events\n"); 196 reg &= ~PCIE_SLCSR_NOTIFY_MASK; 197 pci_conf_write(sc->sc_pc, sc->sc_tag, 198 off + PCIE_SLCSR, reg); 199 } 200 } 201 202 static void 203 ppbattach(device_t parent, device_t self, void *aux) 204 { 205 struct ppb_softc *sc = device_private(self); 206 struct pci_attach_args *pa = aux; 207 pci_chipset_tag_t pc = pa->pa_pc; 208 struct pcibus_attach_args pba; 209 pcireg_t busdata; 210 211 pci_aprint_devinfo(pa, NULL); 212 213 sc->sc_pc = pc; 214 sc->sc_tag = pa->pa_tag; 215 sc->sc_dev = self; 216 217 busdata = pci_conf_read(pc, pa->pa_tag, PPB_REG_BUSINFO); 218 219 if (PPB_BUSINFO_SECONDARY(busdata) == 0) { 220 aprint_normal_dev(self, "not configured by system firmware\n"); 221 return; 222 } 223 224 ppb_fix_pcie(self); 225 226 #if 0 227 /* 228 * XXX can't do this, because we're not given our bus number 229 * (we shouldn't need it), and because we've no way to 230 * decompose our tag. 231 */ 232 /* sanity check. */ 233 if (pa->pa_bus != PPB_BUSINFO_PRIMARY(busdata)) 234 panic("ppbattach: bus in tag (%d) != bus in reg (%d)", 235 pa->pa_bus, PPB_BUSINFO_PRIMARY(busdata)); 236 #endif 237 238 if (!pmf_device_register(self, ppb_suspend, ppb_resume)) 239 aprint_error_dev(self, "couldn't establish power handler\n"); 240 241 /* 242 * Attach the PCI bus than hangs off of it. 243 * 244 * XXX Don't pass-through Memory Read Multiple. Should we? 245 * XXX Consult the spec... 246 */ 247 pba.pba_iot = pa->pa_iot; 248 pba.pba_memt = pa->pa_memt; 249 pba.pba_dmat = pa->pa_dmat; 250 pba.pba_dmat64 = pa->pa_dmat64; 251 pba.pba_pc = pc; 252 pba.pba_flags = pa->pa_flags & ~PCI_FLAGS_MRM_OKAY; 253 pba.pba_bus = PPB_BUSINFO_SECONDARY(busdata); 254 pba.pba_sub = PPB_BUSINFO_SUBORDINATE(busdata); 255 pba.pba_bridgetag = &sc->sc_tag; 256 pba.pba_intrswiz = pa->pa_intrswiz; 257 pba.pba_intrtag = pa->pa_intrtag; 258 259 config_found_ia(self, "pcibus", &pba, pcibusprint); 260 } 261 262 static int 263 ppbdetach(device_t self, int flags) 264 { 265 int rc; 266 267 if ((rc = config_detach_children(self, flags)) != 0) 268 return rc; 269 pmf_device_deregister(self); 270 return 0; 271 } 272 273 static bool 274 ppb_resume(device_t dv, const pmf_qual_t *qual) 275 { 276 struct ppb_softc *sc = device_private(dv); 277 int off; 278 pcireg_t val; 279 280 for (off = 0x40; off <= 0xff; off += 4) { 281 val = pci_conf_read(sc->sc_pc, sc->sc_tag, off); 282 if (val != sc->sc_pciconfext[(off - 0x40) / 4]) 283 pci_conf_write(sc->sc_pc, sc->sc_tag, off, 284 sc->sc_pciconfext[(off - 0x40)/4]); 285 } 286 287 ppb_fix_pcie(dv); 288 289 return true; 290 } 291 292 static bool 293 ppb_suspend(device_t dv, const pmf_qual_t *qual) 294 { 295 struct ppb_softc *sc = device_private(dv); 296 int off; 297 298 for (off = 0x40; off <= 0xff; off += 4) 299 sc->sc_pciconfext[(off - 0x40) / 4] = 300 pci_conf_read(sc->sc_pc, sc->sc_tag, off); 301 302 return true; 303 } 304 305 static void 306 ppbchilddet(device_t self, device_t child) 307 { 308 /* we keep no references to child devices, so do nothing */ 309 } 310 311 CFATTACH_DECL3_NEW(ppb, sizeof(struct ppb_softc), 312 ppbmatch, ppbattach, ppbdetach, NULL, NULL, ppbchilddet, 313 DVF_DETACH_SHUTDOWN); 314