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