Home | History | Annotate | Line # | Download | only in pci
ppb.c revision 1.44
      1 /*	$NetBSD: ppb.c,v 1.44 2011/01/10 12:23:21 jmcneill 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.44 2011/01/10 12:23:21 jmcneill 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 bool		ppb_resume(device_t, const pmf_qual_t *);
     59 static bool		ppb_suspend(device_t, const pmf_qual_t *);
     60 
     61 static int
     62 ppbmatch(device_t parent, cfdata_t match, void *aux)
     63 {
     64 	struct pci_attach_args *pa = aux;
     65 
     66 	/*
     67 	 * Check the ID register to see that it's a PCI bridge.
     68 	 * If it is, we assume that we can deal with it; it _should_
     69 	 * work in a standardized way...
     70 	 */
     71 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE &&
     72 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_BRIDGE_PCI)
     73 		return 1;
     74 
     75 #ifdef __powerpc__
     76 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_PROCESSOR &&
     77 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_PROCESSOR_POWERPC) {
     78 		pcireg_t bhlc = pci_conf_read(pa->pa_pc, pa->pa_tag,
     79 		    PCI_BHLC_REG);
     80 		if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_FREESCALE
     81 		    && PCI_HDRTYPE(bhlc) == PCI_HDRTYPE_RC)
     82 		return 1;
     83 	}
     84 #endif
     85 
     86 	return 0;
     87 }
     88 
     89 static void
     90 ppb_fix_pcie(device_t self)
     91 {
     92 	struct ppb_softc *sc = device_private(self);
     93 	pcireg_t reg;
     94 	int off;
     95 
     96 	if (!pci_get_capability(sc->sc_pc, sc->sc_tag, PCI_CAP_PCIEXPRESS,
     97 				&off, &reg))
     98 		return; /* Not a PCIe device */
     99 
    100 	aprint_normal_dev(self, "PCI Express ");
    101 	switch (reg & PCI_PCIE_XCAP_VER_MASK) {
    102 	case PCI_PCIE_XCAP_VER_1_0:
    103 		aprint_normal("1.0");
    104 	case PCI_PCIE_XCAP_VER_2_0:
    105 		aprint_normal("2.0");
    106 		break;
    107 	default:
    108 		aprint_normal_dev(self, "version unsupported (0x%x)\n",
    109 		    (reg & PCI_PCIE_XCAP_VER_MASK) >> 16);
    110 		return;
    111 	}
    112 	aprint_normal(" <");
    113 	switch (reg & PCI_PCIE_XCAP_TYPE_MASK) {
    114 	case PCI_PCIE_XCAP_TYPE_PCIE_DEV:
    115 		aprint_normal("PCI-E Endpoint device");
    116 		break;
    117 	case PCI_PCIE_XCAP_TYPE_PCI_DEV:
    118 		aprint_normal("Legacy PCI-E Endpoint device");
    119 		break;
    120 	case PCI_PCIE_XCAP_TYPE_ROOT:
    121 		aprint_normal("Root Port of PCI-E Root Complex");
    122 		break;
    123 	case PCI_PCIE_XCAP_TYPE_UP:
    124 		aprint_normal("Upstream Port of PCI-E Switch");
    125 		break;
    126 	case PCI_PCIE_XCAP_TYPE_DOWN:
    127 		aprint_normal("Downstream Port of PCI-E Switch");
    128 		break;
    129 	case PCI_PCIE_XCAP_TYPE_PCIE2PCI:
    130 		aprint_normal("PCI-E to PCI/PCI-X Bridge");
    131 		break;
    132 	case PCI_PCIE_XCAP_TYPE_PCI2PCIE:
    133 		aprint_normal("PCI/PCI-X to PCI-E Bridge");
    134 		break;
    135 	default:
    136 		aprint_normal("Device/Port Type 0x%x",
    137 		    (reg & PCI_PCIE_XCAP_TYPE_MASK) >> 20);
    138 		break;
    139 	}
    140 	aprint_normal(">\n");
    141 
    142 	reg = pci_conf_read(sc->sc_pc, sc->sc_tag, off + PCI_PCIE_SLCSR);
    143 	if (reg & PCI_PCIE_SLCSR_NOTIFY_MASK) {
    144 		aprint_debug_dev(self, "disabling notification events\n");
    145 		reg &= ~PCI_PCIE_SLCSR_NOTIFY_MASK;
    146 		pci_conf_write(sc->sc_pc, sc->sc_tag,
    147 		    off + PCI_PCIE_SLCSR, reg);
    148 	}
    149 }
    150 
    151 static void
    152 ppbattach(device_t parent, device_t self, void *aux)
    153 {
    154 	struct ppb_softc *sc = device_private(self);
    155 	struct pci_attach_args *pa = aux;
    156 	pci_chipset_tag_t pc = pa->pa_pc;
    157 	struct pcibus_attach_args pba;
    158 	pcireg_t busdata;
    159 	char devinfo[256];
    160 
    161 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    162 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo,
    163 	    PCI_REVISION(pa->pa_class));
    164 	aprint_naive("\n");
    165 
    166 	sc->sc_pc = pc;
    167 	sc->sc_tag = pa->pa_tag;
    168 	sc->sc_dev = self;
    169 
    170 	busdata = pci_conf_read(pc, pa->pa_tag, PPB_REG_BUSINFO);
    171 
    172 	if (PPB_BUSINFO_SECONDARY(busdata) == 0) {
    173 		aprint_normal_dev(self, "not configured by system firmware\n");
    174 		return;
    175 	}
    176 
    177 	ppb_fix_pcie(self);
    178 
    179 #if 0
    180 	/*
    181 	 * XXX can't do this, because we're not given our bus number
    182 	 * (we shouldn't need it), and because we've no way to
    183 	 * decompose our tag.
    184 	 */
    185 	/* sanity check. */
    186 	if (pa->pa_bus != PPB_BUSINFO_PRIMARY(busdata))
    187 		panic("ppbattach: bus in tag (%d) != bus in reg (%d)",
    188 		    pa->pa_bus, PPB_BUSINFO_PRIMARY(busdata));
    189 #endif
    190 
    191 	if (!pmf_device_register(self, ppb_suspend, ppb_resume))
    192 		aprint_error_dev(self, "couldn't establish power handler\n");
    193 
    194 	/*
    195 	 * Attach the PCI bus than hangs off of it.
    196 	 *
    197 	 * XXX Don't pass-through Memory Read Multiple.  Should we?
    198 	 * XXX Consult the spec...
    199 	 */
    200 	pba.pba_iot = pa->pa_iot;
    201 	pba.pba_memt = pa->pa_memt;
    202 	pba.pba_dmat = pa->pa_dmat;
    203 	pba.pba_dmat64 = pa->pa_dmat64;
    204 	pba.pba_pc = pc;
    205 	pba.pba_flags = pa->pa_flags & ~PCI_FLAGS_MRM_OKAY;
    206 	pba.pba_bus = PPB_BUSINFO_SECONDARY(busdata);
    207 	pba.pba_bridgetag = &sc->sc_tag;
    208 	pba.pba_intrswiz = pa->pa_intrswiz;
    209 	pba.pba_intrtag = pa->pa_intrtag;
    210 
    211 	config_found_ia(self, "pcibus", &pba, pcibusprint);
    212 }
    213 
    214 static int
    215 ppbdetach(device_t self, int flags)
    216 {
    217 	int rc;
    218 
    219 	if ((rc = config_detach_children(self, flags)) != 0)
    220 		return rc;
    221 	pmf_device_deregister(self);
    222 	return 0;
    223 }
    224 
    225 static bool
    226 ppb_resume(device_t dv, const pmf_qual_t *qual)
    227 {
    228 	struct ppb_softc *sc = device_private(dv);
    229 	int off;
    230 	pcireg_t val;
    231 
    232         for (off = 0x40; off <= 0xff; off += 4) {
    233 		val = pci_conf_read(sc->sc_pc, sc->sc_tag, off);
    234 		if (val != sc->sc_pciconfext[(off - 0x40) / 4])
    235 			pci_conf_write(sc->sc_pc, sc->sc_tag, off,
    236 			    sc->sc_pciconfext[(off - 0x40)/4]);
    237 	}
    238 
    239 	ppb_fix_pcie(dv);
    240 
    241 	return true;
    242 }
    243 
    244 static bool
    245 ppb_suspend(device_t dv, const pmf_qual_t *qual)
    246 {
    247 	struct ppb_softc *sc = device_private(dv);
    248 	int off;
    249 
    250 	for (off = 0x40; off <= 0xff; off += 4)
    251 		sc->sc_pciconfext[(off - 0x40) / 4] =
    252 		    pci_conf_read(sc->sc_pc, sc->sc_tag, off);
    253 
    254 	return true;
    255 }
    256 
    257 static void
    258 ppbchilddet(device_t self, device_t child)
    259 {
    260 	/* we keep no references to child devices, so do nothing */
    261 }
    262 
    263 CFATTACH_DECL3_NEW(ppb, sizeof(struct ppb_softc),
    264     ppbmatch, ppbattach, ppbdetach, NULL, NULL, ppbchilddet,
    265     DVF_DETACH_SHUTDOWN);
    266