Home | History | Annotate | Line # | Download | only in pci
ppb.c revision 1.49.6.3
      1  1.49.6.2       tls /*	$NetBSD: ppb.c,v 1.49.6.3 2017/12/03 11:37:28 jdolecek Exp $	*/
      2       1.1       cgd 
      3       1.1       cgd /*
      4      1.17       cgd  * Copyright (c) 1996, 1998 Christopher G. Demetriou.  All rights reserved.
      5       1.1       cgd  *
      6       1.1       cgd  * Redistribution and use in source and binary forms, with or without
      7       1.1       cgd  * modification, are permitted provided that the following conditions
      8       1.1       cgd  * are met:
      9       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     10       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     11       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     12       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     13       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     14       1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     15       1.1       cgd  *    must display the following acknowledgement:
     16       1.1       cgd  *      This product includes software developed by Christopher G. Demetriou
     17       1.1       cgd  *	for the NetBSD Project.
     18       1.1       cgd  * 4. The name of the author may not be used to endorse or promote products
     19       1.1       cgd  *    derived from this software without specific prior written permission
     20       1.1       cgd  *
     21       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22       1.1       cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23       1.1       cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24       1.1       cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25       1.1       cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26       1.1       cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27       1.1       cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28       1.1       cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29       1.1       cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30       1.1       cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31       1.1       cgd  */
     32      1.20     lukem 
     33      1.20     lukem #include <sys/cdefs.h>
     34  1.49.6.2       tls __KERNEL_RCSID(0, "$NetBSD: ppb.c,v 1.49.6.3 2017/12/03 11:37:28 jdolecek Exp $");
     35       1.1       cgd 
     36       1.1       cgd #include <sys/param.h>
     37       1.1       cgd #include <sys/systm.h>
     38       1.1       cgd #include <sys/kernel.h>
     39       1.1       cgd #include <sys/device.h>
     40  1.49.6.3  jdolecek #include <sys/evcnt.h>
     41       1.1       cgd 
     42       1.1       cgd #include <dev/pci/pcireg.h>
     43       1.1       cgd #include <dev/pci/pcivar.h>
     44       1.1       cgd #include <dev/pci/ppbreg.h>
     45  1.49.6.3  jdolecek #include <dev/pci/ppbvar.h>
     46      1.36  jmcneill #include <dev/pci/pcidevs.h>
     47       1.1       cgd 
     48  1.49.6.3  jdolecek #define	PCIE_SLCSR_ENABLE_MASK					\
     49  1.49.6.2       tls 	(PCIE_SLCSR_ABE | PCIE_SLCSR_PFE | PCIE_SLCSR_MSE |	\
     50  1.49.6.3  jdolecek 	 PCIE_SLCSR_PDE | PCIE_SLCSR_CCE | PCIE_SLCSR_HPE |	\
     51  1.49.6.3  jdolecek 	 PCIE_SLCSR_DLLSCE)
     52      1.44  jmcneill 
     53  1.49.6.3  jdolecek #define	PCIE_SLCSR_STATCHG_MASK					\
     54  1.49.6.3  jdolecek 	(PCIE_SLCSR_ABP | PCIE_SLCSR_PFD | PCIE_SLCSR_MSC |	\
     55  1.49.6.3  jdolecek 	 PCIE_SLCSR_PDC | PCIE_SLCSR_CC | PCIE_SLCSR_LACS)
     56      1.21   thorpej 
     57  1.49.6.1       tls static const char pcie_linkspeed_strings[4][5] = {
     58  1.49.6.1       tls 	"1.25", "2.5", "5.0", "8.0",
     59  1.49.6.1       tls };
     60  1.49.6.1       tls 
     61  1.49.6.3  jdolecek int	ppb_printevent = 0; /* Print event type if the value is not 0 */
     62  1.49.6.3  jdolecek 
     63  1.49.6.3  jdolecek static int	ppbmatch(device_t, cfdata_t, void *);
     64  1.49.6.3  jdolecek static void	ppbattach(device_t, device_t, void *);
     65  1.49.6.3  jdolecek static int	ppbdetach(device_t, int);
     66  1.49.6.3  jdolecek static void	ppbchilddet(device_t, device_t);
     67  1.49.6.3  jdolecek #ifdef PPB_USEINTR
     68  1.49.6.3  jdolecek static int	ppb_intr(void *);
     69  1.49.6.3  jdolecek #endif
     70  1.49.6.3  jdolecek static bool	ppb_resume(device_t, const pmf_qual_t *);
     71  1.49.6.3  jdolecek static bool	ppb_suspend(device_t, const pmf_qual_t *);
     72  1.49.6.3  jdolecek 
     73  1.49.6.3  jdolecek CFATTACH_DECL3_NEW(ppb, sizeof(struct ppb_softc),
     74  1.49.6.3  jdolecek     ppbmatch, ppbattach, ppbdetach, NULL, NULL, ppbchilddet,
     75  1.49.6.3  jdolecek     DVF_DETACH_SHUTDOWN);
     76      1.36  jmcneill 
     77      1.31   thorpej static int
     78      1.39    cegger ppbmatch(device_t parent, cfdata_t match, void *aux)
     79       1.1       cgd {
     80       1.1       cgd 	struct pci_attach_args *pa = aux;
     81       1.1       cgd 
     82       1.1       cgd 	/*
     83       1.1       cgd 	 * Check the ID register to see that it's a PCI bridge.
     84       1.1       cgd 	 * If it is, we assume that we can deal with it; it _should_
     85       1.1       cgd 	 * work in a standardized way...
     86       1.1       cgd 	 */
     87       1.1       cgd 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE &&
     88       1.1       cgd 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_BRIDGE_PCI)
     89      1.39    cegger 		return 1;
     90       1.1       cgd 
     91      1.43      matt #ifdef __powerpc__
     92      1.43      matt 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_PROCESSOR &&
     93      1.43      matt 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_PROCESSOR_POWERPC) {
     94      1.43      matt 		pcireg_t bhlc = pci_conf_read(pa->pa_pc, pa->pa_tag,
     95      1.43      matt 		    PCI_BHLC_REG);
     96      1.43      matt 		if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_FREESCALE
     97      1.43      matt 		    && PCI_HDRTYPE(bhlc) == PCI_HDRTYPE_RC)
     98      1.43      matt 		return 1;
     99      1.43      matt 	}
    100      1.43      matt #endif
    101      1.43      matt 
    102  1.49.6.1       tls #ifdef _MIPS_PADDR_T_64BIT
    103  1.49.6.1       tls 	/* The LDT HB acts just like a PPB.  */
    104  1.49.6.1       tls 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SIBYTE
    105  1.49.6.1       tls 	    && PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SIBYTE_BCM1250_LDTHB)
    106  1.49.6.1       tls 		return 1;
    107  1.49.6.1       tls #endif
    108  1.49.6.1       tls 
    109      1.39    cegger 	return 0;
    110       1.1       cgd }
    111       1.1       cgd 
    112      1.31   thorpej static void
    113  1.49.6.3  jdolecek ppb_print_pcie(device_t self)
    114      1.35     joerg {
    115      1.36  jmcneill 	struct ppb_softc *sc = device_private(self);
    116      1.35     joerg 	pcireg_t reg;
    117  1.49.6.3  jdolecek 	int off, capversion, devtype;
    118      1.35     joerg 
    119      1.36  jmcneill 	if (!pci_get_capability(sc->sc_pc, sc->sc_tag, PCI_CAP_PCIEXPRESS,
    120      1.35     joerg 				&off, &reg))
    121      1.35     joerg 		return; /* Not a PCIe device */
    122      1.35     joerg 
    123  1.49.6.3  jdolecek 	capversion = PCIE_XCAP_VER(reg);
    124  1.49.6.3  jdolecek 	devtype = PCIE_XCAP_TYPE(reg);
    125  1.49.6.3  jdolecek 	aprint_normal_dev(self, "PCI Express capability version ");
    126  1.49.6.3  jdolecek 	switch (capversion) {
    127  1.49.6.3  jdolecek 	case PCIE_XCAP_VER_1:
    128  1.49.6.3  jdolecek 		aprint_normal("1");
    129      1.45    cegger 		break;
    130  1.49.6.3  jdolecek 	case PCIE_XCAP_VER_2:
    131  1.49.6.3  jdolecek 		aprint_normal("2");
    132      1.44  jmcneill 		break;
    133      1.44  jmcneill 	default:
    134  1.49.6.3  jdolecek 		aprint_normal_dev(self, "unsupported (%d)\n", capversion);
    135      1.35     joerg 		return;
    136      1.35     joerg 	}
    137      1.44  jmcneill 	aprint_normal(" <");
    138  1.49.6.3  jdolecek 	switch (devtype) {
    139  1.49.6.2       tls 	case PCIE_XCAP_TYPE_PCIE_DEV:
    140      1.44  jmcneill 		aprint_normal("PCI-E Endpoint device");
    141      1.44  jmcneill 		break;
    142  1.49.6.2       tls 	case PCIE_XCAP_TYPE_PCI_DEV:
    143      1.44  jmcneill 		aprint_normal("Legacy PCI-E Endpoint device");
    144      1.44  jmcneill 		break;
    145  1.49.6.2       tls 	case PCIE_XCAP_TYPE_ROOT:
    146      1.44  jmcneill 		aprint_normal("Root Port of PCI-E Root Complex");
    147      1.44  jmcneill 		break;
    148  1.49.6.2       tls 	case PCIE_XCAP_TYPE_UP:
    149      1.44  jmcneill 		aprint_normal("Upstream Port of PCI-E Switch");
    150      1.44  jmcneill 		break;
    151  1.49.6.2       tls 	case PCIE_XCAP_TYPE_DOWN:
    152      1.44  jmcneill 		aprint_normal("Downstream Port of PCI-E Switch");
    153      1.44  jmcneill 		break;
    154  1.49.6.2       tls 	case PCIE_XCAP_TYPE_PCIE2PCI:
    155      1.44  jmcneill 		aprint_normal("PCI-E to PCI/PCI-X Bridge");
    156      1.44  jmcneill 		break;
    157  1.49.6.2       tls 	case PCIE_XCAP_TYPE_PCI2PCIE:
    158      1.44  jmcneill 		aprint_normal("PCI/PCI-X to PCI-E Bridge");
    159      1.44  jmcneill 		break;
    160      1.44  jmcneill 	default:
    161  1.49.6.3  jdolecek 		aprint_normal("Device/Port Type %x", devtype);
    162      1.44  jmcneill 		break;
    163      1.44  jmcneill 	}
    164  1.49.6.1       tls 
    165  1.49.6.3  jdolecek 	switch (devtype) {
    166  1.49.6.2       tls 	case PCIE_XCAP_TYPE_ROOT:
    167  1.49.6.2       tls 	case PCIE_XCAP_TYPE_DOWN:
    168  1.49.6.2       tls 	case PCIE_XCAP_TYPE_PCI2PCIE:
    169  1.49.6.3  jdolecek 		reg = pci_conf_read(sc->sc_pc, sc->sc_tag, off + PCIE_LCAP);
    170  1.49.6.3  jdolecek 		u_int mlw = __SHIFTOUT(reg, PCIE_LCAP_MAX_WIDTH);
    171  1.49.6.3  jdolecek 		u_int mls = __SHIFTOUT(reg, PCIE_LCAP_MAX_SPEED);
    172  1.49.6.3  jdolecek 
    173  1.49.6.1       tls 		if (mls < __arraycount(pcie_linkspeed_strings)) {
    174  1.49.6.3  jdolecek 			aprint_normal("> x%d @ %sGT/s\n",
    175  1.49.6.1       tls 			    mlw, pcie_linkspeed_strings[mls]);
    176  1.49.6.1       tls 		} else {
    177  1.49.6.3  jdolecek 			aprint_normal("> x%d @ %d.%dGT/s\n",
    178  1.49.6.1       tls 			    mlw, (mls * 25) / 10, (mls * 25) % 10);
    179  1.49.6.1       tls 		}
    180  1.49.6.1       tls 
    181  1.49.6.3  jdolecek 		reg = pci_conf_read(sc->sc_pc, sc->sc_tag, off + PCIE_LCSR);
    182  1.49.6.3  jdolecek 		if (reg & PCIE_LCSR_DLACTIVE) {	/* DLLA */
    183  1.49.6.3  jdolecek 			u_int lw = __SHIFTOUT(reg, PCIE_LCSR_NLW);
    184  1.49.6.3  jdolecek 			u_int ls = __SHIFTOUT(reg, PCIE_LCSR_LINKSPEED);
    185  1.49.6.3  jdolecek 
    186  1.49.6.1       tls 			if (lw != mlw || ls != mls) {
    187  1.49.6.1       tls 				if (ls < __arraycount(pcie_linkspeed_strings)) {
    188  1.49.6.2       tls 					aprint_normal_dev(self,
    189  1.49.6.3  jdolecek 					    "link is x%d @ %sGT/s\n",
    190  1.49.6.1       tls 					    lw, pcie_linkspeed_strings[ls]);
    191  1.49.6.1       tls 				} else {
    192  1.49.6.1       tls 					aprint_normal_dev(self,
    193  1.49.6.3  jdolecek 					    "link is x%d @ %d.%dGT/s\n",
    194  1.49.6.1       tls 					    lw, (ls * 25) / 10, (ls * 25) % 10);
    195  1.49.6.1       tls 				}
    196  1.49.6.1       tls 			}
    197  1.49.6.1       tls 		}
    198  1.49.6.1       tls 		break;
    199  1.49.6.1       tls 	default:
    200  1.49.6.1       tls 		aprint_normal(">\n");
    201  1.49.6.1       tls 		break;
    202  1.49.6.1       tls 	}
    203      1.35     joerg }
    204      1.35     joerg 
    205      1.35     joerg static void
    206      1.37    dyoung ppbattach(device_t parent, device_t self, void *aux)
    207       1.1       cgd {
    208      1.37    dyoung 	struct ppb_softc *sc = device_private(self);
    209       1.1       cgd 	struct pci_attach_args *pa = aux;
    210       1.7       cgd 	pci_chipset_tag_t pc = pa->pa_pc;
    211       1.1       cgd 	struct pcibus_attach_args pba;
    212  1.49.6.3  jdolecek #ifdef PPB_USEINTR
    213  1.49.6.3  jdolecek 	char const *intrstr;
    214  1.49.6.3  jdolecek 	char intrbuf[PCI_INTRSTR_LEN];
    215  1.49.6.3  jdolecek #endif
    216  1.49.6.3  jdolecek 	pcireg_t busdata, reg;
    217       1.1       cgd 
    218      1.49  drochner 	pci_aprint_devinfo(pa, NULL);
    219       1.1       cgd 
    220      1.21   thorpej 	sc->sc_pc = pc;
    221      1.21   thorpej 	sc->sc_tag = pa->pa_tag;
    222      1.39    cegger 	sc->sc_dev = self;
    223      1.21   thorpej 
    224       1.7       cgd 	busdata = pci_conf_read(pc, pa->pa_tag, PPB_REG_BUSINFO);
    225       1.1       cgd 
    226       1.7       cgd 	if (PPB_BUSINFO_SECONDARY(busdata) == 0) {
    227      1.37    dyoung 		aprint_normal_dev(self, "not configured by system firmware\n");
    228       1.1       cgd 		return;
    229       1.1       cgd 	}
    230       1.1       cgd 
    231  1.49.6.3  jdolecek 	ppb_print_pcie(self);
    232      1.35     joerg 
    233       1.1       cgd #if 0
    234       1.1       cgd 	/*
    235       1.1       cgd 	 * XXX can't do this, because we're not given our bus number
    236       1.7       cgd 	 * (we shouldn't need it), and because we've no way to
    237       1.7       cgd 	 * decompose our tag.
    238       1.1       cgd 	 */
    239       1.1       cgd 	/* sanity check. */
    240       1.7       cgd 	if (pa->pa_bus != PPB_BUSINFO_PRIMARY(busdata))
    241       1.1       cgd 		panic("ppbattach: bus in tag (%d) != bus in reg (%d)",
    242       1.7       cgd 		    pa->pa_bus, PPB_BUSINFO_PRIMARY(busdata));
    243       1.1       cgd #endif
    244       1.1       cgd 
    245  1.49.6.3  jdolecek 	/* Check for PCI Express capabilities and setup hotplug support. */
    246  1.49.6.3  jdolecek 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PCIEXPRESS,
    247  1.49.6.3  jdolecek 	    &sc->sc_pciecapoff, &reg) && (reg & PCIE_XCAP_SI)) {
    248  1.49.6.3  jdolecek 		/*
    249  1.49.6.3  jdolecek 		 * First, disable all interrupts because BIOS might
    250  1.49.6.3  jdolecek 		 * enable them.
    251  1.49.6.3  jdolecek 		 */
    252  1.49.6.3  jdolecek 		reg = pci_conf_read(sc->sc_pc, sc->sc_tag,
    253  1.49.6.3  jdolecek 		    sc->sc_pciecapoff + PCIE_SLCSR);
    254  1.49.6.3  jdolecek 		if (reg & PCIE_SLCSR_ENABLE_MASK) {
    255  1.49.6.3  jdolecek 			reg &= ~PCIE_SLCSR_ENABLE_MASK;
    256  1.49.6.3  jdolecek 			pci_conf_write(sc->sc_pc, sc->sc_tag,
    257  1.49.6.3  jdolecek 			    sc->sc_pciecapoff + PCIE_SLCSR, reg);
    258  1.49.6.3  jdolecek 		}
    259  1.49.6.3  jdolecek #ifdef PPB_USEINTR
    260  1.49.6.3  jdolecek #if 0 /* notyet */
    261  1.49.6.3  jdolecek 		/*
    262  1.49.6.3  jdolecek 		 * XXX Initialize workqueue or something else for
    263  1.49.6.3  jdolecek 		 * HotPlug support.
    264  1.49.6.3  jdolecek 		 */
    265  1.49.6.3  jdolecek #endif
    266  1.49.6.3  jdolecek 		if (pci_intr_alloc(pa, &sc->sc_pihp, NULL, 0) == 0)
    267  1.49.6.3  jdolecek 			sc->sc_intrhand = pci_intr_establish_xname(pc,
    268  1.49.6.3  jdolecek 			    sc->sc_pihp[0], IPL_BIO, ppb_intr, sc,
    269  1.49.6.3  jdolecek 			    device_xname(sc->sc_dev));
    270  1.49.6.3  jdolecek #endif
    271  1.49.6.3  jdolecek 	}
    272  1.49.6.3  jdolecek 
    273  1.49.6.3  jdolecek #ifdef PPB_USEINTR
    274  1.49.6.3  jdolecek 	if (sc->sc_intrhand != NULL) {
    275  1.49.6.3  jdolecek 		pcireg_t slcap, slcsr, val;
    276  1.49.6.3  jdolecek 
    277  1.49.6.3  jdolecek 		intrstr = pci_intr_string(pc, sc->sc_pihp[0], intrbuf,
    278  1.49.6.3  jdolecek 		    sizeof(intrbuf));
    279  1.49.6.3  jdolecek 		aprint_normal_dev(self, "%s\n", intrstr);
    280  1.49.6.3  jdolecek 
    281  1.49.6.3  jdolecek 		/* Clear any pending events */
    282  1.49.6.3  jdolecek 		slcsr = pci_conf_read(pc, pa->pa_tag,
    283  1.49.6.3  jdolecek 		    sc->sc_pciecapoff + PCIE_SLCSR);
    284  1.49.6.3  jdolecek 		pci_conf_write(pc, pa->pa_tag,
    285  1.49.6.3  jdolecek 		    sc->sc_pciecapoff + PCIE_SLCSR, slcsr);
    286  1.49.6.3  jdolecek 
    287  1.49.6.3  jdolecek 		/* Enable interrupt. */
    288  1.49.6.3  jdolecek 		val = 0;
    289  1.49.6.3  jdolecek 		slcap = pci_conf_read(pc, pa->pa_tag,
    290  1.49.6.3  jdolecek 		    sc->sc_pciecapoff + PCIE_SLCAP);
    291  1.49.6.3  jdolecek 		if (slcap & PCIE_SLCAP_ABP)
    292  1.49.6.3  jdolecek 			val |= PCIE_SLCSR_ABE;
    293  1.49.6.3  jdolecek 		if (slcap & PCIE_SLCAP_PCP)
    294  1.49.6.3  jdolecek 			val |= PCIE_SLCSR_PFE;
    295  1.49.6.3  jdolecek 		if (slcap & PCIE_SLCAP_MSP)
    296  1.49.6.3  jdolecek 			val |= PCIE_SLCSR_MSE;
    297  1.49.6.3  jdolecek #if 0
    298  1.49.6.3  jdolecek 		/*
    299  1.49.6.3  jdolecek 		 * XXX Disable for a while because setting
    300  1.49.6.3  jdolecek 		 * PCIE_SLCSR_CCE makes break device access on
    301  1.49.6.3  jdolecek 		 * some environment.
    302  1.49.6.3  jdolecek 		 */
    303  1.49.6.3  jdolecek 		if ((slcap & PCIE_SLCAP_NCCS) == 0)
    304  1.49.6.3  jdolecek 			val |= PCIE_SLCSR_CCE;
    305  1.49.6.3  jdolecek #endif
    306  1.49.6.3  jdolecek 		/* Attention indicator off by default */
    307  1.49.6.3  jdolecek 		if (slcap & PCIE_SLCAP_AIP) {
    308  1.49.6.3  jdolecek 			val |= __SHIFTIN(PCIE_SLCSR_IND_OFF,
    309  1.49.6.3  jdolecek 			    PCIE_SLCSR_AIC);
    310  1.49.6.3  jdolecek 		}
    311  1.49.6.3  jdolecek 		/* Power indicator */
    312  1.49.6.3  jdolecek 		if (slcap & PCIE_SLCAP_PIP) {
    313  1.49.6.3  jdolecek 			/*
    314  1.49.6.3  jdolecek 			 * Indicator off:
    315  1.49.6.3  jdolecek 			 *  a) card not present
    316  1.49.6.3  jdolecek 			 *  b) power fault
    317  1.49.6.3  jdolecek 			 *  c) MRL sensor off
    318  1.49.6.3  jdolecek 			 */
    319  1.49.6.3  jdolecek 			if (((slcsr & PCIE_SLCSR_PDS) == 0)
    320  1.49.6.3  jdolecek 			    || ((slcsr & PCIE_SLCSR_PFD) != 0)
    321  1.49.6.3  jdolecek 			    || (((slcap & PCIE_SLCAP_MSP) != 0)
    322  1.49.6.3  jdolecek 				&& ((slcsr & PCIE_SLCSR_MS) != 0)))
    323  1.49.6.3  jdolecek 				val |= __SHIFTIN(PCIE_SLCSR_IND_OFF,
    324  1.49.6.3  jdolecek 				    PCIE_SLCSR_PIC);
    325  1.49.6.3  jdolecek 			else
    326  1.49.6.3  jdolecek 				val |= __SHIFTIN(PCIE_SLCSR_IND_ON,
    327  1.49.6.3  jdolecek 				    PCIE_SLCSR_PIC);
    328  1.49.6.3  jdolecek 		}
    329  1.49.6.3  jdolecek 
    330  1.49.6.3  jdolecek 		val |= PCIE_SLCSR_DLLSCE | PCIE_SLCSR_HPE | PCIE_SLCSR_PDE;
    331  1.49.6.3  jdolecek 		slcsr = val;
    332  1.49.6.3  jdolecek 		pci_conf_write(pc, pa->pa_tag,
    333  1.49.6.3  jdolecek 		    sc->sc_pciecapoff + PCIE_SLCSR, slcsr);
    334  1.49.6.3  jdolecek 
    335  1.49.6.3  jdolecek 		/* Attach event counters */
    336  1.49.6.3  jdolecek 		evcnt_attach_dynamic(&sc->sc_ev_intr, EVCNT_TYPE_INTR, NULL,
    337  1.49.6.3  jdolecek 		    device_xname(sc->sc_dev), "Interrupt");
    338  1.49.6.3  jdolecek 		evcnt_attach_dynamic(&sc->sc_ev_abp, EVCNT_TYPE_MISC, NULL,
    339  1.49.6.3  jdolecek 		    device_xname(sc->sc_dev), "Attention Button Pressed");
    340  1.49.6.3  jdolecek 		evcnt_attach_dynamic(&sc->sc_ev_pfd, EVCNT_TYPE_MISC, NULL,
    341  1.49.6.3  jdolecek 		    device_xname(sc->sc_dev), "Power Fault Detected");
    342  1.49.6.3  jdolecek 		evcnt_attach_dynamic(&sc->sc_ev_msc, EVCNT_TYPE_MISC, NULL,
    343  1.49.6.3  jdolecek 		    device_xname(sc->sc_dev), "MRL Sensor Changed");
    344  1.49.6.3  jdolecek 		evcnt_attach_dynamic(&sc->sc_ev_pdc, EVCNT_TYPE_MISC, NULL,
    345  1.49.6.3  jdolecek 		    device_xname(sc->sc_dev), "Presence Detect Changed");
    346  1.49.6.3  jdolecek 		evcnt_attach_dynamic(&sc->sc_ev_cc, EVCNT_TYPE_MISC, NULL,
    347  1.49.6.3  jdolecek 		    device_xname(sc->sc_dev), "Command Completed");
    348  1.49.6.3  jdolecek 		evcnt_attach_dynamic(&sc->sc_ev_lacs, EVCNT_TYPE_MISC, NULL,
    349  1.49.6.3  jdolecek 		    device_xname(sc->sc_dev), "Data Link Layer State Changed");
    350  1.49.6.3  jdolecek 	}
    351  1.49.6.3  jdolecek #endif /* PPB_USEINTR */
    352  1.49.6.3  jdolecek 
    353      1.36  jmcneill 	if (!pmf_device_register(self, ppb_suspend, ppb_resume))
    354      1.36  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    355      1.36  jmcneill 
    356       1.1       cgd 	/*
    357  1.49.6.3  jdolecek 	 * Attach the PCI bus that hangs off of it.
    358      1.19   thorpej 	 *
    359      1.19   thorpej 	 * XXX Don't pass-through Memory Read Multiple.  Should we?
    360      1.19   thorpej 	 * XXX Consult the spec...
    361       1.1       cgd 	 */
    362      1.12   thorpej 	pba.pba_iot = pa->pa_iot;
    363      1.12   thorpej 	pba.pba_memt = pa->pa_memt;
    364      1.15   mycroft 	pba.pba_dmat = pa->pa_dmat;
    365      1.26      fvdl 	pba.pba_dmat64 = pa->pa_dmat64;
    366       1.7       cgd 	pba.pba_pc = pc;
    367      1.19   thorpej 	pba.pba_flags = pa->pa_flags & ~PCI_FLAGS_MRM_OKAY;
    368       1.7       cgd 	pba.pba_bus = PPB_BUSINFO_SECONDARY(busdata);
    369      1.47    dyoung 	pba.pba_sub = PPB_BUSINFO_SUBORDINATE(busdata);
    370      1.21   thorpej 	pba.pba_bridgetag = &sc->sc_tag;
    371       1.7       cgd 	pba.pba_intrswiz = pa->pa_intrswiz;
    372       1.7       cgd 	pba.pba_intrtag = pa->pa_intrtag;
    373       1.1       cgd 
    374      1.29  drochner 	config_found_ia(self, "pcibus", &pba, pcibusprint);
    375       1.1       cgd }
    376      1.31   thorpej 
    377      1.37    dyoung static int
    378      1.37    dyoung ppbdetach(device_t self, int flags)
    379      1.37    dyoung {
    380  1.49.6.3  jdolecek #ifdef PPB_USEINTR
    381  1.49.6.3  jdolecek 	struct ppb_softc *sc = device_private(self);
    382  1.49.6.3  jdolecek 	pcireg_t slcsr;
    383  1.49.6.3  jdolecek #endif
    384      1.37    dyoung 	int rc;
    385      1.37    dyoung 
    386      1.37    dyoung 	if ((rc = config_detach_children(self, flags)) != 0)
    387      1.37    dyoung 		return rc;
    388  1.49.6.3  jdolecek 
    389  1.49.6.3  jdolecek #ifdef PPB_USEINTR
    390  1.49.6.3  jdolecek 	if (sc->sc_intrhand != NULL) {
    391  1.49.6.3  jdolecek 		/* Detach event counters */
    392  1.49.6.3  jdolecek 		evcnt_detach(&sc->sc_ev_intr);
    393  1.49.6.3  jdolecek 		evcnt_detach(&sc->sc_ev_abp);
    394  1.49.6.3  jdolecek 		evcnt_detach(&sc->sc_ev_pfd);
    395  1.49.6.3  jdolecek 		evcnt_detach(&sc->sc_ev_msc);
    396  1.49.6.3  jdolecek 		evcnt_detach(&sc->sc_ev_pdc);
    397  1.49.6.3  jdolecek 		evcnt_detach(&sc->sc_ev_cc);
    398  1.49.6.3  jdolecek 		evcnt_detach(&sc->sc_ev_lacs);
    399  1.49.6.3  jdolecek 
    400  1.49.6.3  jdolecek 		/* Clear any pending events and disable interrupt */
    401  1.49.6.3  jdolecek 		slcsr = pci_conf_read(sc->sc_pc, sc->sc_tag,
    402  1.49.6.3  jdolecek 		    sc->sc_pciecapoff + PCIE_SLCSR);
    403  1.49.6.3  jdolecek 		slcsr &= ~PCIE_SLCSR_ENABLE_MASK;
    404  1.49.6.3  jdolecek 		pci_conf_write(sc->sc_pc, sc->sc_tag,
    405  1.49.6.3  jdolecek 		    sc->sc_pciecapoff + PCIE_SLCSR, slcsr);
    406  1.49.6.3  jdolecek 
    407  1.49.6.3  jdolecek 		/* Disestablish the interrupt handler */
    408  1.49.6.3  jdolecek 		pci_intr_disestablish(sc->sc_pc, sc->sc_intrhand);
    409  1.49.6.3  jdolecek 		pci_intr_release(sc->sc_pc, sc->sc_pihp, 1);
    410  1.49.6.3  jdolecek 	}
    411  1.49.6.3  jdolecek #endif
    412  1.49.6.3  jdolecek 
    413      1.37    dyoung 	pmf_device_deregister(self);
    414      1.37    dyoung 	return 0;
    415      1.37    dyoung }
    416      1.37    dyoung 
    417      1.36  jmcneill static bool
    418      1.42    dyoung ppb_resume(device_t dv, const pmf_qual_t *qual)
    419      1.36  jmcneill {
    420      1.36  jmcneill 	struct ppb_softc *sc = device_private(dv);
    421      1.36  jmcneill 	int off;
    422      1.36  jmcneill 	pcireg_t val;
    423      1.36  jmcneill 
    424      1.36  jmcneill         for (off = 0x40; off <= 0xff; off += 4) {
    425      1.36  jmcneill 		val = pci_conf_read(sc->sc_pc, sc->sc_tag, off);
    426      1.36  jmcneill 		if (val != sc->sc_pciconfext[(off - 0x40) / 4])
    427      1.36  jmcneill 			pci_conf_write(sc->sc_pc, sc->sc_tag, off,
    428      1.36  jmcneill 			    sc->sc_pciconfext[(off - 0x40)/4]);
    429      1.36  jmcneill 	}
    430      1.36  jmcneill 
    431      1.36  jmcneill 	return true;
    432      1.36  jmcneill }
    433      1.36  jmcneill 
    434      1.36  jmcneill static bool
    435      1.42    dyoung ppb_suspend(device_t dv, const pmf_qual_t *qual)
    436      1.36  jmcneill {
    437      1.36  jmcneill 	struct ppb_softc *sc = device_private(dv);
    438      1.36  jmcneill 	int off;
    439      1.36  jmcneill 
    440      1.36  jmcneill 	for (off = 0x40; off <= 0xff; off += 4)
    441      1.36  jmcneill 		sc->sc_pciconfext[(off - 0x40) / 4] =
    442      1.36  jmcneill 		    pci_conf_read(sc->sc_pc, sc->sc_tag, off);
    443      1.36  jmcneill 
    444      1.36  jmcneill 	return true;
    445      1.36  jmcneill }
    446      1.36  jmcneill 
    447      1.37    dyoung static void
    448      1.37    dyoung ppbchilddet(device_t self, device_t child)
    449      1.37    dyoung {
    450      1.37    dyoung 	/* we keep no references to child devices, so do nothing */
    451      1.37    dyoung }
    452      1.37    dyoung 
    453  1.49.6.3  jdolecek #ifdef PPB_USEINTR
    454  1.49.6.3  jdolecek static int
    455  1.49.6.3  jdolecek ppb_intr(void *arg)
    456  1.49.6.3  jdolecek {
    457  1.49.6.3  jdolecek 	struct ppb_softc *sc = arg;
    458  1.49.6.3  jdolecek 	device_t dev = sc->sc_dev;
    459  1.49.6.3  jdolecek 	pcireg_t reg;
    460  1.49.6.3  jdolecek 
    461  1.49.6.3  jdolecek 	reg = pci_conf_read(sc->sc_pc, sc->sc_tag,
    462  1.49.6.3  jdolecek 	    sc->sc_pciecapoff + PCIE_SLCSR);
    463  1.49.6.3  jdolecek 
    464  1.49.6.3  jdolecek 	/*
    465  1.49.6.3  jdolecek 	 * Not me. This check is only required for INTx.
    466  1.49.6.3  jdolecek 	 * ppb_intr() would be spilted int ppb_intr_legacy() and ppb_intr_msi()
    467  1.49.6.3  jdolecek 	 */
    468  1.49.6.3  jdolecek 	if ((reg & PCIE_SLCSR_STATCHG_MASK) == 0)
    469  1.49.6.3  jdolecek 		return 0;
    470  1.49.6.3  jdolecek 
    471  1.49.6.3  jdolecek 	/* Clear interrupts. */
    472  1.49.6.3  jdolecek 	pci_conf_write(sc->sc_pc, sc->sc_tag,
    473  1.49.6.3  jdolecek 	    sc->sc_pciecapoff + PCIE_SLCSR, reg);
    474  1.49.6.3  jdolecek 
    475  1.49.6.3  jdolecek 	sc->sc_ev_intr.ev_count++;
    476  1.49.6.3  jdolecek 
    477  1.49.6.3  jdolecek 	/* Attention Button Pressed */
    478  1.49.6.3  jdolecek 	if (reg & PCIE_SLCSR_ABP) {
    479  1.49.6.3  jdolecek 		sc->sc_ev_abp.ev_count++;
    480  1.49.6.3  jdolecek 		if (ppb_printevent)
    481  1.49.6.3  jdolecek 			device_printf(dev, "Attention Button Pressed\n");
    482  1.49.6.3  jdolecek 	}
    483  1.49.6.3  jdolecek 
    484  1.49.6.3  jdolecek 	/* Power Fault Detected */
    485  1.49.6.3  jdolecek 	if (reg & PCIE_SLCSR_PFD) {
    486  1.49.6.3  jdolecek 		sc->sc_ev_pfd.ev_count++;
    487  1.49.6.3  jdolecek 		if (ppb_printevent)
    488  1.49.6.3  jdolecek 			device_printf(dev, "Power Fault Detected\n");
    489  1.49.6.3  jdolecek 	}
    490  1.49.6.3  jdolecek 
    491  1.49.6.3  jdolecek 	/* MRL Sensor Changed */
    492  1.49.6.3  jdolecek 	if (reg & PCIE_SLCSR_MSC) {
    493  1.49.6.3  jdolecek 		sc->sc_ev_msc.ev_count++;
    494  1.49.6.3  jdolecek 		if (ppb_printevent)
    495  1.49.6.3  jdolecek 			device_printf(dev, "MRL Sensor Changed\n");
    496  1.49.6.3  jdolecek 	}
    497  1.49.6.3  jdolecek 
    498  1.49.6.3  jdolecek 	/* Presence Detect Changed */
    499  1.49.6.3  jdolecek 	if (reg & PCIE_SLCSR_PDC) {
    500  1.49.6.3  jdolecek 		sc->sc_ev_pdc.ev_count++;
    501  1.49.6.3  jdolecek 		if (ppb_printevent)
    502  1.49.6.3  jdolecek 			device_printf(dev, "Presence Detect Changed\n");
    503  1.49.6.3  jdolecek 		if (reg & PCIE_SLCSR_PDS) {
    504  1.49.6.3  jdolecek 			/* XXX Insert */
    505  1.49.6.3  jdolecek 		} else {
    506  1.49.6.3  jdolecek 			/* XXX Remove */
    507  1.49.6.3  jdolecek 		}
    508  1.49.6.3  jdolecek 	}
    509  1.49.6.3  jdolecek 
    510  1.49.6.3  jdolecek 	/* Command Completed */
    511  1.49.6.3  jdolecek 	if (reg & PCIE_SLCSR_CC) {
    512  1.49.6.3  jdolecek 		sc->sc_ev_cc.ev_count++;
    513  1.49.6.3  jdolecek 		if (ppb_printevent)
    514  1.49.6.3  jdolecek 			device_printf(dev, "Command Completed\n");
    515  1.49.6.3  jdolecek 	}
    516  1.49.6.3  jdolecek 
    517  1.49.6.3  jdolecek 	/* Data Link Layer State Changed */
    518  1.49.6.3  jdolecek 	if (reg & PCIE_SLCSR_LACS) {
    519  1.49.6.3  jdolecek 		sc->sc_ev_lacs.ev_count++;
    520  1.49.6.3  jdolecek 		if (ppb_printevent)
    521  1.49.6.3  jdolecek 			device_printf(dev, "Data Link Layer State Changed\n");
    522  1.49.6.3  jdolecek 	}
    523  1.49.6.3  jdolecek 
    524  1.49.6.3  jdolecek 	return 1;
    525  1.49.6.3  jdolecek }
    526  1.49.6.3  jdolecek #endif /* PPB_USEINTR */
    527