xref: /netbsd-src/sys/dev/pci/ppb.c (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1 /*	$NetBSD: ppb.c,v 1.47 2011/10/21 21:35:28 dyoung 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 2011/10/21 21:35:28 dyoung 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 		break;
105 	case PCI_PCIE_XCAP_VER_2_0:
106 		aprint_normal("2.0");
107 		break;
108 	default:
109 		aprint_normal_dev(self,
110 		    "version unsupported (0x%" PRIxMAX ")\n",
111 		    __SHIFTOUT(reg, PCI_PCIE_XCAP_VER_MASK));
112 		return;
113 	}
114 	aprint_normal(" <");
115 	switch (reg & PCI_PCIE_XCAP_TYPE_MASK) {
116 	case PCI_PCIE_XCAP_TYPE_PCIE_DEV:
117 		aprint_normal("PCI-E Endpoint device");
118 		break;
119 	case PCI_PCIE_XCAP_TYPE_PCI_DEV:
120 		aprint_normal("Legacy PCI-E Endpoint device");
121 		break;
122 	case PCI_PCIE_XCAP_TYPE_ROOT:
123 		aprint_normal("Root Port of PCI-E Root Complex");
124 		break;
125 	case PCI_PCIE_XCAP_TYPE_UP:
126 		aprint_normal("Upstream Port of PCI-E Switch");
127 		break;
128 	case PCI_PCIE_XCAP_TYPE_DOWN:
129 		aprint_normal("Downstream Port of PCI-E Switch");
130 		break;
131 	case PCI_PCIE_XCAP_TYPE_PCIE2PCI:
132 		aprint_normal("PCI-E to PCI/PCI-X Bridge");
133 		break;
134 	case PCI_PCIE_XCAP_TYPE_PCI2PCIE:
135 		aprint_normal("PCI/PCI-X to PCI-E Bridge");
136 		break;
137 	default:
138 		aprint_normal("Device/Port Type 0x%" PRIxMAX,
139 		    __SHIFTOUT(reg, PCI_PCIE_XCAP_TYPE_MASK));
140 		break;
141 	}
142 	aprint_normal(">\n");
143 
144 	reg = pci_conf_read(sc->sc_pc, sc->sc_tag, off + PCI_PCIE_SLCSR);
145 	if (reg & PCI_PCIE_SLCSR_NOTIFY_MASK) {
146 		aprint_debug_dev(self, "disabling notification events\n");
147 		reg &= ~PCI_PCIE_SLCSR_NOTIFY_MASK;
148 		pci_conf_write(sc->sc_pc, sc->sc_tag,
149 		    off + PCI_PCIE_SLCSR, reg);
150 	}
151 }
152 
153 static void
154 ppbattach(device_t parent, device_t self, void *aux)
155 {
156 	struct ppb_softc *sc = device_private(self);
157 	struct pci_attach_args *pa = aux;
158 	pci_chipset_tag_t pc = pa->pa_pc;
159 	struct pcibus_attach_args pba;
160 	pcireg_t busdata;
161 	char devinfo[256];
162 
163 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
164 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo,
165 	    PCI_REVISION(pa->pa_class));
166 	aprint_naive("\n");
167 
168 	sc->sc_pc = pc;
169 	sc->sc_tag = pa->pa_tag;
170 	sc->sc_dev = self;
171 
172 	busdata = pci_conf_read(pc, pa->pa_tag, PPB_REG_BUSINFO);
173 
174 	if (PPB_BUSINFO_SECONDARY(busdata) == 0) {
175 		aprint_normal_dev(self, "not configured by system firmware\n");
176 		return;
177 	}
178 
179 	ppb_fix_pcie(self);
180 
181 #if 0
182 	/*
183 	 * XXX can't do this, because we're not given our bus number
184 	 * (we shouldn't need it), and because we've no way to
185 	 * decompose our tag.
186 	 */
187 	/* sanity check. */
188 	if (pa->pa_bus != PPB_BUSINFO_PRIMARY(busdata))
189 		panic("ppbattach: bus in tag (%d) != bus in reg (%d)",
190 		    pa->pa_bus, PPB_BUSINFO_PRIMARY(busdata));
191 #endif
192 
193 	if (!pmf_device_register(self, ppb_suspend, ppb_resume))
194 		aprint_error_dev(self, "couldn't establish power handler\n");
195 
196 	/*
197 	 * Attach the PCI bus than hangs off of it.
198 	 *
199 	 * XXX Don't pass-through Memory Read Multiple.  Should we?
200 	 * XXX Consult the spec...
201 	 */
202 	pba.pba_iot = pa->pa_iot;
203 	pba.pba_memt = pa->pa_memt;
204 	pba.pba_dmat = pa->pa_dmat;
205 	pba.pba_dmat64 = pa->pa_dmat64;
206 	pba.pba_pc = pc;
207 	pba.pba_flags = pa->pa_flags & ~PCI_FLAGS_MRM_OKAY;
208 	pba.pba_bus = PPB_BUSINFO_SECONDARY(busdata);
209 	pba.pba_sub = PPB_BUSINFO_SUBORDINATE(busdata);
210 	pba.pba_bridgetag = &sc->sc_tag;
211 	pba.pba_intrswiz = pa->pa_intrswiz;
212 	pba.pba_intrtag = pa->pa_intrtag;
213 
214 	config_found_ia(self, "pcibus", &pba, pcibusprint);
215 }
216 
217 static int
218 ppbdetach(device_t self, int flags)
219 {
220 	int rc;
221 
222 	if ((rc = config_detach_children(self, flags)) != 0)
223 		return rc;
224 	pmf_device_deregister(self);
225 	return 0;
226 }
227 
228 static bool
229 ppb_resume(device_t dv, const pmf_qual_t *qual)
230 {
231 	struct ppb_softc *sc = device_private(dv);
232 	int off;
233 	pcireg_t val;
234 
235         for (off = 0x40; off <= 0xff; off += 4) {
236 		val = pci_conf_read(sc->sc_pc, sc->sc_tag, off);
237 		if (val != sc->sc_pciconfext[(off - 0x40) / 4])
238 			pci_conf_write(sc->sc_pc, sc->sc_tag, off,
239 			    sc->sc_pciconfext[(off - 0x40)/4]);
240 	}
241 
242 	ppb_fix_pcie(dv);
243 
244 	return true;
245 }
246 
247 static bool
248 ppb_suspend(device_t dv, const pmf_qual_t *qual)
249 {
250 	struct ppb_softc *sc = device_private(dv);
251 	int off;
252 
253 	for (off = 0x40; off <= 0xff; off += 4)
254 		sc->sc_pciconfext[(off - 0x40) / 4] =
255 		    pci_conf_read(sc->sc_pc, sc->sc_tag, off);
256 
257 	return true;
258 }
259 
260 static void
261 ppbchilddet(device_t self, device_t child)
262 {
263 	/* we keep no references to child devices, so do nothing */
264 }
265 
266 CFATTACH_DECL3_NEW(ppb, sizeof(struct ppb_softc),
267     ppbmatch, ppbattach, ppbdetach, NULL, NULL, ppbchilddet,
268     DVF_DETACH_SHUTDOWN);
269