Home | History | Annotate | Line # | Download | only in pci
ppb.c revision 1.47.2.2
      1 /*	$NetBSD: ppb.c,v 1.47.2.2 2012/10/30 17:21:52 yamt 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.47.2.2 2012/10/30 17:21:52 yamt 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	PCI_PCIE_SLCSR_NOTIFY_MASK					\
     47 	(PCI_PCIE_SLCSR_ABE | PCI_PCIE_SLCSR_PFE | PCI_PCIE_SLCSR_MSE |	\
     48 	 PCI_PCIE_SLCSR_PDE | PCI_PCIE_SLCSR_CCE | PCI_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, &reg))
    109 		return; /* Not a PCIe device */
    110 
    111 	aprint_normal_dev(self, "PCI Express ");
    112 	switch (reg & PCI_PCIE_XCAP_VER_MASK) {
    113 	case PCI_PCIE_XCAP_VER_1_0:
    114 		aprint_normal("1.0");
    115 		break;
    116 	case PCI_PCIE_XCAP_VER_2_0:
    117 		aprint_normal("2.0");
    118 		break;
    119 	default:
    120 		aprint_normal_dev(self,
    121 		    "version unsupported (0x%" PRIxMAX ")\n",
    122 		    __SHIFTOUT(reg, PCI_PCIE_XCAP_VER_MASK));
    123 		return;
    124 	}
    125 	aprint_normal(" <");
    126 	switch (reg & PCI_PCIE_XCAP_TYPE_MASK) {
    127 	case PCI_PCIE_XCAP_TYPE_PCIE_DEV:
    128 		aprint_normal("PCI-E Endpoint device");
    129 		break;
    130 	case PCI_PCIE_XCAP_TYPE_PCI_DEV:
    131 		aprint_normal("Legacy PCI-E Endpoint device");
    132 		break;
    133 	case PCI_PCIE_XCAP_TYPE_ROOT:
    134 		aprint_normal("Root Port of PCI-E Root Complex");
    135 		break;
    136 	case PCI_PCIE_XCAP_TYPE_UP:
    137 		aprint_normal("Upstream Port of PCI-E Switch");
    138 		break;
    139 	case PCI_PCIE_XCAP_TYPE_DOWN:
    140 		aprint_normal("Downstream Port of PCI-E Switch");
    141 		break;
    142 	case PCI_PCIE_XCAP_TYPE_PCIE2PCI:
    143 		aprint_normal("PCI-E to PCI/PCI-X Bridge");
    144 		break;
    145 	case PCI_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, PCI_PCIE_XCAP_TYPE_MASK));
    151 		break;
    152 	}
    153 
    154 	switch (reg & PCI_PCIE_XCAP_TYPE_MASK) {
    155 	case PCI_PCIE_XCAP_TYPE_ROOT:
    156 	case PCI_PCIE_XCAP_TYPE_DOWN:
    157 	case PCI_PCIE_XCAP_TYPE_PCI2PCIE:
    158 		reg = pci_conf_read(sc->sc_pc, sc->sc_tag, off + 0x0c);
    159 		u_int mlw = (reg >> 4) & 0x1f;
    160 		u_int mls = (reg >> 0) & 0x0f;
    161 		if (mls < __arraycount(pcie_linkspeed_strings)) {
    162 			aprint_normal("> x%d @ %sGb/s\n",
    163 			    mlw, pcie_linkspeed_strings[mls]);
    164 		} else {
    165 			aprint_normal("> x%d @ %d.%dGb/s\n",
    166 			    mlw, (mls * 25) / 10, (mls * 25) % 10);
    167 		}
    168 
    169 		reg = pci_conf_read(sc->sc_pc, sc->sc_tag, off + 0x10);
    170 		if (reg & __BIT(29)) {	/* DLLA */
    171 			u_int lw = (reg >> 20) & 0x1f;
    172 			u_int ls = (reg >> 16) & 0x0f;
    173 			if (lw != mlw || ls != mls) {
    174 				if (ls < __arraycount(pcie_linkspeed_strings)) {
    175 					aprint_normal("> x%d @ %sGb/s\n",
    176 					    lw, pcie_linkspeed_strings[ls]);
    177 				} else {
    178 					aprint_normal_dev(self,
    179 					    "link is x%d @ %d.%dGb/s\n",
    180 					    lw, (ls * 25) / 10, (ls * 25) % 10);
    181 				}
    182 			}
    183 		}
    184 		break;
    185 	default:
    186 		aprint_normal(">\n");
    187 		break;
    188 	}
    189 
    190 	reg = pci_conf_read(sc->sc_pc, sc->sc_tag, off + PCI_PCIE_SLCSR);
    191 	if (reg & PCI_PCIE_SLCSR_NOTIFY_MASK) {
    192 		aprint_debug_dev(self, "disabling notification events\n");
    193 		reg &= ~PCI_PCIE_SLCSR_NOTIFY_MASK;
    194 		pci_conf_write(sc->sc_pc, sc->sc_tag,
    195 		    off + PCI_PCIE_SLCSR, reg);
    196 	}
    197 }
    198 
    199 static void
    200 ppbattach(device_t parent, device_t self, void *aux)
    201 {
    202 	struct ppb_softc *sc = device_private(self);
    203 	struct pci_attach_args *pa = aux;
    204 	pci_chipset_tag_t pc = pa->pa_pc;
    205 	struct pcibus_attach_args pba;
    206 	pcireg_t busdata;
    207 
    208 	pci_aprint_devinfo(pa, NULL);
    209 
    210 	sc->sc_pc = pc;
    211 	sc->sc_tag = pa->pa_tag;
    212 	sc->sc_dev = self;
    213 
    214 	busdata = pci_conf_read(pc, pa->pa_tag, PPB_REG_BUSINFO);
    215 
    216 	if (PPB_BUSINFO_SECONDARY(busdata) == 0) {
    217 		aprint_normal_dev(self, "not configured by system firmware\n");
    218 		return;
    219 	}
    220 
    221 	ppb_fix_pcie(self);
    222 
    223 #if 0
    224 	/*
    225 	 * XXX can't do this, because we're not given our bus number
    226 	 * (we shouldn't need it), and because we've no way to
    227 	 * decompose our tag.
    228 	 */
    229 	/* sanity check. */
    230 	if (pa->pa_bus != PPB_BUSINFO_PRIMARY(busdata))
    231 		panic("ppbattach: bus in tag (%d) != bus in reg (%d)",
    232 		    pa->pa_bus, PPB_BUSINFO_PRIMARY(busdata));
    233 #endif
    234 
    235 	if (!pmf_device_register(self, ppb_suspend, ppb_resume))
    236 		aprint_error_dev(self, "couldn't establish power handler\n");
    237 
    238 	/*
    239 	 * Attach the PCI bus than hangs off of it.
    240 	 *
    241 	 * XXX Don't pass-through Memory Read Multiple.  Should we?
    242 	 * XXX Consult the spec...
    243 	 */
    244 	pba.pba_iot = pa->pa_iot;
    245 	pba.pba_memt = pa->pa_memt;
    246 	pba.pba_dmat = pa->pa_dmat;
    247 	pba.pba_dmat64 = pa->pa_dmat64;
    248 	pba.pba_pc = pc;
    249 	pba.pba_flags = pa->pa_flags & ~PCI_FLAGS_MRM_OKAY;
    250 	pba.pba_bus = PPB_BUSINFO_SECONDARY(busdata);
    251 	pba.pba_sub = PPB_BUSINFO_SUBORDINATE(busdata);
    252 	pba.pba_bridgetag = &sc->sc_tag;
    253 	pba.pba_intrswiz = pa->pa_intrswiz;
    254 	pba.pba_intrtag = pa->pa_intrtag;
    255 
    256 	config_found_ia(self, "pcibus", &pba, pcibusprint);
    257 }
    258 
    259 static int
    260 ppbdetach(device_t self, int flags)
    261 {
    262 	int rc;
    263 
    264 	if ((rc = config_detach_children(self, flags)) != 0)
    265 		return rc;
    266 	pmf_device_deregister(self);
    267 	return 0;
    268 }
    269 
    270 static bool
    271 ppb_resume(device_t dv, const pmf_qual_t *qual)
    272 {
    273 	struct ppb_softc *sc = device_private(dv);
    274 	int off;
    275 	pcireg_t val;
    276 
    277         for (off = 0x40; off <= 0xff; off += 4) {
    278 		val = pci_conf_read(sc->sc_pc, sc->sc_tag, off);
    279 		if (val != sc->sc_pciconfext[(off - 0x40) / 4])
    280 			pci_conf_write(sc->sc_pc, sc->sc_tag, off,
    281 			    sc->sc_pciconfext[(off - 0x40)/4]);
    282 	}
    283 
    284 	ppb_fix_pcie(dv);
    285 
    286 	return true;
    287 }
    288 
    289 static bool
    290 ppb_suspend(device_t dv, const pmf_qual_t *qual)
    291 {
    292 	struct ppb_softc *sc = device_private(dv);
    293 	int off;
    294 
    295 	for (off = 0x40; off <= 0xff; off += 4)
    296 		sc->sc_pciconfext[(off - 0x40) / 4] =
    297 		    pci_conf_read(sc->sc_pc, sc->sc_tag, off);
    298 
    299 	return true;
    300 }
    301 
    302 static void
    303 ppbchilddet(device_t self, device_t child)
    304 {
    305 	/* we keep no references to child devices, so do nothing */
    306 }
    307 
    308 CFATTACH_DECL3_NEW(ppb, sizeof(struct ppb_softc),
    309     ppbmatch, ppbattach, ppbdetach, NULL, NULL, ppbchilddet,
    310     DVF_DETACH_SHUTDOWN);
    311