ppb.c revision 1.63.2.2 1 1.63.2.2 martin /* $NetBSD: ppb.c,v 1.63.2.2 2019/07/17 15:55:31 martin 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.63.2.2 martin __KERNEL_RCSID(0, "$NetBSD: ppb.c,v 1.63.2.2 2019/07/17 15:55:31 martin 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.57 msaitoh #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.56 msaitoh #include <dev/pci/ppbvar.h>
46 1.36 jmcneill #include <dev/pci/pcidevs.h>
47 1.1 cgd
48 1.61 msaitoh #define PCIE_SLCSR_ENABLE_MASK \
49 1.52 msaitoh (PCIE_SLCSR_ABE | PCIE_SLCSR_PFE | PCIE_SLCSR_MSE | \
50 1.59 msaitoh PCIE_SLCSR_PDE | PCIE_SLCSR_CCE | PCIE_SLCSR_HPE | \
51 1.59 msaitoh PCIE_SLCSR_DLLSCE)
52 1.44 jmcneill
53 1.61 msaitoh #define PCIE_SLCSR_STATCHG_MASK \
54 1.61 msaitoh (PCIE_SLCSR_ABP | PCIE_SLCSR_PFD | PCIE_SLCSR_MSC | \
55 1.61 msaitoh PCIE_SLCSR_PDC | PCIE_SLCSR_CC | PCIE_SLCSR_LACS)
56 1.61 msaitoh
57 1.63.2.2 martin static const char pcie_linkspeed_strings[5][5] = {
58 1.63.2.2 martin "1.25", "2.5", "5.0", "8.0", "16.0"
59 1.50 matt };
60 1.50 matt
61 1.57 msaitoh int ppb_printevent = 0; /* Print event type if the value is not 0 */
62 1.57 msaitoh
63 1.57 msaitoh static int ppbmatch(device_t, cfdata_t, void *);
64 1.57 msaitoh static void ppbattach(device_t, device_t, void *);
65 1.57 msaitoh static int ppbdetach(device_t, int);
66 1.57 msaitoh static void ppbchilddet(device_t, device_t);
67 1.60 msaitoh #ifdef PPB_USEINTR
68 1.57 msaitoh static int ppb_intr(void *);
69 1.60 msaitoh #endif
70 1.57 msaitoh static bool ppb_resume(device_t, const pmf_qual_t *);
71 1.57 msaitoh static bool ppb_suspend(device_t, const pmf_qual_t *);
72 1.57 msaitoh
73 1.57 msaitoh CFATTACH_DECL3_NEW(ppb, sizeof(struct ppb_softc),
74 1.57 msaitoh ppbmatch, ppbattach, ppbdetach, NULL, NULL, ppbchilddet,
75 1.57 msaitoh 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.50 matt #ifdef _MIPS_PADDR_T_64BIT
103 1.50 matt /* The LDT HB acts just like a PPB. */
104 1.50 matt if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SIBYTE
105 1.50 matt && PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SIBYTE_BCM1250_LDTHB)
106 1.50 matt return 1;
107 1.50 matt #endif
108 1.50 matt
109 1.39 cegger return 0;
110 1.1 cgd }
111 1.1 cgd
112 1.31 thorpej static void
113 1.57 msaitoh 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.55 msaitoh 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, ®))
121 1.35 joerg return; /* Not a PCIe device */
122 1.35 joerg
123 1.55 msaitoh capversion = PCIE_XCAP_VER(reg);
124 1.55 msaitoh devtype = PCIE_XCAP_TYPE(reg);
125 1.53 msaitoh aprint_normal_dev(self, "PCI Express capability version ");
126 1.55 msaitoh switch (capversion) {
127 1.54 msaitoh case PCIE_XCAP_VER_1:
128 1.53 msaitoh aprint_normal("1");
129 1.45 cegger break;
130 1.54 msaitoh case PCIE_XCAP_VER_2:
131 1.53 msaitoh aprint_normal("2");
132 1.44 jmcneill break;
133 1.44 jmcneill default:
134 1.55 msaitoh 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.55 msaitoh switch (devtype) {
139 1.52 msaitoh case PCIE_XCAP_TYPE_PCIE_DEV:
140 1.44 jmcneill aprint_normal("PCI-E Endpoint device");
141 1.44 jmcneill break;
142 1.52 msaitoh case PCIE_XCAP_TYPE_PCI_DEV:
143 1.44 jmcneill aprint_normal("Legacy PCI-E Endpoint device");
144 1.44 jmcneill break;
145 1.52 msaitoh 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.52 msaitoh case PCIE_XCAP_TYPE_UP:
149 1.44 jmcneill aprint_normal("Upstream Port of PCI-E Switch");
150 1.44 jmcneill break;
151 1.52 msaitoh case PCIE_XCAP_TYPE_DOWN:
152 1.44 jmcneill aprint_normal("Downstream Port of PCI-E Switch");
153 1.44 jmcneill break;
154 1.52 msaitoh 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.52 msaitoh 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.55 msaitoh aprint_normal("Device/Port Type %x", devtype);
162 1.44 jmcneill break;
163 1.44 jmcneill }
164 1.50 matt
165 1.55 msaitoh switch (devtype) {
166 1.52 msaitoh case PCIE_XCAP_TYPE_ROOT:
167 1.52 msaitoh case PCIE_XCAP_TYPE_DOWN:
168 1.52 msaitoh case PCIE_XCAP_TYPE_PCI2PCIE:
169 1.53 msaitoh reg = pci_conf_read(sc->sc_pc, sc->sc_tag, off + PCIE_LCAP);
170 1.53 msaitoh u_int mlw = __SHIFTOUT(reg, PCIE_LCAP_MAX_WIDTH);
171 1.53 msaitoh u_int mls = __SHIFTOUT(reg, PCIE_LCAP_MAX_SPEED);
172 1.53 msaitoh
173 1.50 matt if (mls < __arraycount(pcie_linkspeed_strings)) {
174 1.53 msaitoh aprint_normal("> x%d @ %sGT/s\n",
175 1.50 matt mlw, pcie_linkspeed_strings[mls]);
176 1.50 matt } else {
177 1.53 msaitoh aprint_normal("> x%d @ %d.%dGT/s\n",
178 1.50 matt mlw, (mls * 25) / 10, (mls * 25) % 10);
179 1.50 matt }
180 1.50 matt
181 1.53 msaitoh reg = pci_conf_read(sc->sc_pc, sc->sc_tag, off + PCIE_LCSR);
182 1.53 msaitoh if (reg & PCIE_LCSR_DLACTIVE) { /* DLLA */
183 1.53 msaitoh u_int lw = __SHIFTOUT(reg, PCIE_LCSR_NLW);
184 1.53 msaitoh u_int ls = __SHIFTOUT(reg, PCIE_LCSR_LINKSPEED);
185 1.53 msaitoh
186 1.50 matt if (lw != mlw || ls != mls) {
187 1.50 matt if (ls < __arraycount(pcie_linkspeed_strings)) {
188 1.51 yamt aprint_normal_dev(self,
189 1.53 msaitoh "link is x%d @ %sGT/s\n",
190 1.50 matt lw, pcie_linkspeed_strings[ls]);
191 1.50 matt } else {
192 1.50 matt aprint_normal_dev(self,
193 1.53 msaitoh "link is x%d @ %d.%dGT/s\n",
194 1.50 matt lw, (ls * 25) / 10, (ls * 25) % 10);
195 1.50 matt }
196 1.50 matt }
197 1.50 matt }
198 1.50 matt break;
199 1.50 matt default:
200 1.50 matt aprint_normal(">\n");
201 1.50 matt break;
202 1.50 matt }
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.60 msaitoh #ifdef PPB_USEINTR
213 1.57 msaitoh char const *intrstr;
214 1.57 msaitoh char intrbuf[PCI_INTRSTR_LEN];
215 1.60 msaitoh #endif
216 1.57 msaitoh pcireg_t busdata, reg;
217 1.63.2.1 martin bool second_configured = false;
218 1.1 cgd
219 1.49 drochner pci_aprint_devinfo(pa, NULL);
220 1.1 cgd
221 1.21 thorpej sc->sc_pc = pc;
222 1.21 thorpej sc->sc_tag = pa->pa_tag;
223 1.39 cegger sc->sc_dev = self;
224 1.21 thorpej
225 1.7 cgd busdata = pci_conf_read(pc, pa->pa_tag, PPB_REG_BUSINFO);
226 1.1 cgd
227 1.7 cgd if (PPB_BUSINFO_SECONDARY(busdata) == 0) {
228 1.37 dyoung aprint_normal_dev(self, "not configured by system firmware\n");
229 1.1 cgd return;
230 1.1 cgd }
231 1.1 cgd
232 1.57 msaitoh ppb_print_pcie(self);
233 1.35 joerg
234 1.1 cgd #if 0
235 1.1 cgd /*
236 1.1 cgd * XXX can't do this, because we're not given our bus number
237 1.7 cgd * (we shouldn't need it), and because we've no way to
238 1.7 cgd * decompose our tag.
239 1.1 cgd */
240 1.1 cgd /* sanity check. */
241 1.7 cgd if (pa->pa_bus != PPB_BUSINFO_PRIMARY(busdata))
242 1.1 cgd panic("ppbattach: bus in tag (%d) != bus in reg (%d)",
243 1.7 cgd pa->pa_bus, PPB_BUSINFO_PRIMARY(busdata));
244 1.1 cgd #endif
245 1.1 cgd
246 1.57 msaitoh /* Check for PCI Express capabilities and setup hotplug support. */
247 1.57 msaitoh if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PCIEXPRESS,
248 1.57 msaitoh &sc->sc_pciecapoff, ®) && (reg & PCIE_XCAP_SI)) {
249 1.61 msaitoh /*
250 1.61 msaitoh * First, disable all interrupts because BIOS might
251 1.61 msaitoh * enable them.
252 1.61 msaitoh */
253 1.61 msaitoh reg = pci_conf_read(sc->sc_pc, sc->sc_tag,
254 1.61 msaitoh sc->sc_pciecapoff + PCIE_SLCSR);
255 1.61 msaitoh if (reg & PCIE_SLCSR_ENABLE_MASK) {
256 1.61 msaitoh reg &= ~PCIE_SLCSR_ENABLE_MASK;
257 1.61 msaitoh pci_conf_write(sc->sc_pc, sc->sc_tag,
258 1.61 msaitoh sc->sc_pciecapoff + PCIE_SLCSR, reg);
259 1.61 msaitoh }
260 1.60 msaitoh #ifdef PPB_USEINTR
261 1.62 msaitoh #if 0 /* notyet */
262 1.57 msaitoh /*
263 1.57 msaitoh * XXX Initialize workqueue or something else for
264 1.57 msaitoh * HotPlug support.
265 1.57 msaitoh */
266 1.61 msaitoh #endif
267 1.57 msaitoh if (pci_intr_alloc(pa, &sc->sc_pihp, NULL, 0) == 0)
268 1.57 msaitoh sc->sc_intrhand = pci_intr_establish_xname(pc,
269 1.57 msaitoh sc->sc_pihp[0], IPL_BIO, ppb_intr, sc,
270 1.57 msaitoh device_xname(sc->sc_dev));
271 1.62 msaitoh #endif
272 1.62 msaitoh }
273 1.62 msaitoh
274 1.62 msaitoh #ifdef PPB_USEINTR
275 1.62 msaitoh if (sc->sc_intrhand != NULL) {
276 1.62 msaitoh pcireg_t slcap, slcsr, val;
277 1.62 msaitoh
278 1.62 msaitoh intrstr = pci_intr_string(pc, sc->sc_pihp[0], intrbuf,
279 1.62 msaitoh sizeof(intrbuf));
280 1.62 msaitoh aprint_normal_dev(self, "%s\n", intrstr);
281 1.57 msaitoh
282 1.62 msaitoh /* Clear any pending events */
283 1.62 msaitoh slcsr = pci_conf_read(pc, pa->pa_tag,
284 1.62 msaitoh sc->sc_pciecapoff + PCIE_SLCSR);
285 1.62 msaitoh pci_conf_write(pc, pa->pa_tag,
286 1.62 msaitoh sc->sc_pciecapoff + PCIE_SLCSR, slcsr);
287 1.57 msaitoh
288 1.62 msaitoh /* Enable interrupt. */
289 1.62 msaitoh val = 0;
290 1.62 msaitoh slcap = pci_conf_read(pc, pa->pa_tag,
291 1.62 msaitoh sc->sc_pciecapoff + PCIE_SLCAP);
292 1.62 msaitoh if (slcap & PCIE_SLCAP_ABP)
293 1.62 msaitoh val |= PCIE_SLCSR_ABE;
294 1.62 msaitoh if (slcap & PCIE_SLCAP_PCP)
295 1.62 msaitoh val |= PCIE_SLCSR_PFE;
296 1.62 msaitoh if (slcap & PCIE_SLCAP_MSP)
297 1.62 msaitoh val |= PCIE_SLCSR_MSE;
298 1.61 msaitoh #if 0
299 1.62 msaitoh /*
300 1.62 msaitoh * XXX Disable for a while because setting
301 1.62 msaitoh * PCIE_SLCSR_CCE makes break device access on
302 1.62 msaitoh * some environment.
303 1.62 msaitoh */
304 1.62 msaitoh if ((slcap & PCIE_SLCAP_NCCS) == 0)
305 1.62 msaitoh val |= PCIE_SLCSR_CCE;
306 1.62 msaitoh #endif
307 1.62 msaitoh /* Attention indicator off by default */
308 1.62 msaitoh if (slcap & PCIE_SLCAP_AIP) {
309 1.62 msaitoh val |= __SHIFTIN(PCIE_SLCSR_IND_OFF,
310 1.62 msaitoh PCIE_SLCSR_AIC);
311 1.62 msaitoh }
312 1.62 msaitoh /* Power indicator */
313 1.62 msaitoh if (slcap & PCIE_SLCAP_PIP) {
314 1.61 msaitoh /*
315 1.62 msaitoh * Indicator off:
316 1.62 msaitoh * a) card not present
317 1.62 msaitoh * b) power fault
318 1.62 msaitoh * c) MRL sensor off
319 1.61 msaitoh */
320 1.62 msaitoh if (((slcsr & PCIE_SLCSR_PDS) == 0)
321 1.62 msaitoh || ((slcsr & PCIE_SLCSR_PFD) != 0)
322 1.62 msaitoh || (((slcap & PCIE_SLCAP_MSP) != 0)
323 1.62 msaitoh && ((slcsr & PCIE_SLCSR_MS) != 0)))
324 1.57 msaitoh val |= __SHIFTIN(PCIE_SLCSR_IND_OFF,
325 1.62 msaitoh PCIE_SLCSR_PIC);
326 1.62 msaitoh else
327 1.62 msaitoh val |= __SHIFTIN(PCIE_SLCSR_IND_ON,
328 1.62 msaitoh PCIE_SLCSR_PIC);
329 1.62 msaitoh }
330 1.57 msaitoh
331 1.62 msaitoh val |= PCIE_SLCSR_DLLSCE | PCIE_SLCSR_HPE | PCIE_SLCSR_PDE;
332 1.62 msaitoh slcsr = val;
333 1.62 msaitoh pci_conf_write(pc, pa->pa_tag,
334 1.62 msaitoh sc->sc_pciecapoff + PCIE_SLCSR, slcsr);
335 1.62 msaitoh
336 1.62 msaitoh /* Attach event counters */
337 1.62 msaitoh evcnt_attach_dynamic(&sc->sc_ev_intr, EVCNT_TYPE_INTR, NULL,
338 1.62 msaitoh device_xname(sc->sc_dev), "Interrupt");
339 1.62 msaitoh evcnt_attach_dynamic(&sc->sc_ev_abp, EVCNT_TYPE_MISC, NULL,
340 1.62 msaitoh device_xname(sc->sc_dev), "Attention Button Pressed");
341 1.62 msaitoh evcnt_attach_dynamic(&sc->sc_ev_pfd, EVCNT_TYPE_MISC, NULL,
342 1.62 msaitoh device_xname(sc->sc_dev), "Power Fault Detected");
343 1.62 msaitoh evcnt_attach_dynamic(&sc->sc_ev_msc, EVCNT_TYPE_MISC, NULL,
344 1.62 msaitoh device_xname(sc->sc_dev), "MRL Sensor Changed");
345 1.62 msaitoh evcnt_attach_dynamic(&sc->sc_ev_pdc, EVCNT_TYPE_MISC, NULL,
346 1.62 msaitoh device_xname(sc->sc_dev), "Presence Detect Changed");
347 1.62 msaitoh evcnt_attach_dynamic(&sc->sc_ev_cc, EVCNT_TYPE_MISC, NULL,
348 1.62 msaitoh device_xname(sc->sc_dev), "Command Completed");
349 1.62 msaitoh evcnt_attach_dynamic(&sc->sc_ev_lacs, EVCNT_TYPE_MISC, NULL,
350 1.62 msaitoh device_xname(sc->sc_dev), "Data Link Layer State Changed");
351 1.62 msaitoh }
352 1.60 msaitoh #endif /* PPB_USEINTR */
353 1.57 msaitoh
354 1.63.2.1 martin /* Configuration test */
355 1.63.2.1 martin if (PPB_BUSINFO_SECONDARY(busdata) != 0) {
356 1.63.2.1 martin uint32_t base, limit;
357 1.63.2.1 martin
358 1.63.2.1 martin /* I/O region test */
359 1.63.2.1 martin reg = pci_conf_read(pc, pa->pa_tag, PCI_BRIDGE_STATIO_REG);
360 1.63.2.1 martin base = (reg & PCI_BRIDGE_STATIO_IOBASE_MASK) << 8;
361 1.63.2.1 martin limit = ((reg >> PCI_BRIDGE_STATIO_IOLIMIT_SHIFT)
362 1.63.2.1 martin & PCI_BRIDGE_STATIO_IOLIMIT_MASK) << 8;
363 1.63.2.1 martin limit |= 0x00000fff;
364 1.63.2.1 martin if (PCI_BRIDGE_IO_32BITS(reg)) {
365 1.63.2.1 martin reg = pci_conf_read(pc, pa->pa_tag,
366 1.63.2.1 martin PCI_BRIDGE_IOHIGH_REG);
367 1.63.2.1 martin base |= ((reg >> PCI_BRIDGE_IOHIGH_BASE_SHIFT)
368 1.63.2.1 martin & 0xffff) << 16;
369 1.63.2.1 martin limit |= ((reg >> PCI_BRIDGE_IOHIGH_LIMIT_SHIFT)
370 1.63.2.1 martin & 0xffff) << 16;
371 1.63.2.1 martin }
372 1.63.2.1 martin if (base < limit) {
373 1.63.2.1 martin second_configured = true;
374 1.63.2.1 martin goto configure;
375 1.63.2.1 martin }
376 1.63.2.1 martin
377 1.63.2.1 martin /* Non-prefetchable memory region test */
378 1.63.2.1 martin reg = pci_conf_read(pc, pa->pa_tag, PCI_BRIDGE_MEMORY_REG);
379 1.63.2.1 martin base = ((reg >> PCI_BRIDGE_MEMORY_BASE_SHIFT)
380 1.63.2.1 martin & PCI_BRIDGE_MEMORY_BASE_MASK) << 20;
381 1.63.2.1 martin limit = (((reg >> PCI_BRIDGE_MEMORY_LIMIT_SHIFT)
382 1.63.2.1 martin & PCI_BRIDGE_MEMORY_LIMIT_MASK) << 20) | 0x000fffff;
383 1.63.2.1 martin if (base < limit) {
384 1.63.2.1 martin second_configured = true;
385 1.63.2.1 martin goto configure;
386 1.63.2.1 martin }
387 1.63.2.1 martin
388 1.63.2.1 martin /* Prefetchable memory region test */
389 1.63.2.1 martin reg = pci_conf_read(pc, pa->pa_tag,
390 1.63.2.1 martin PCI_BRIDGE_PREFETCHMEM_REG);
391 1.63.2.1 martin base = ((reg >> PCI_BRIDGE_PREFETCHMEM_BASE_SHIFT)
392 1.63.2.1 martin & PCI_BRIDGE_PREFETCHMEM_BASE_MASK) << 20;
393 1.63.2.1 martin limit = (((reg >> PCI_BRIDGE_PREFETCHMEM_LIMIT_SHIFT)
394 1.63.2.1 martin & PCI_BRIDGE_PREFETCHMEM_LIMIT_MASK) << 20) | 0x000fffff;
395 1.63.2.1 martin if (PCI_BRIDGE_PREFETCHMEM_64BITS(reg)) {
396 1.63.2.1 martin reg = pci_conf_read(pc, pa->pa_tag,
397 1.63.2.1 martin PCI_BRIDGE_IOHIGH_REG);
398 1.63.2.1 martin base |= (uint64_t)pci_conf_read(pc, pa->pa_tag,
399 1.63.2.1 martin PCI_BRIDGE_PREFETCHBASE32_REG) << 32;
400 1.63.2.1 martin limit |= (uint64_t)pci_conf_read(pc, pa->pa_tag,
401 1.63.2.1 martin PCI_BRIDGE_PREFETCHLIMIT32_REG) << 32;
402 1.63.2.1 martin }
403 1.63.2.1 martin if (base < limit) {
404 1.63.2.1 martin second_configured = true;
405 1.63.2.1 martin goto configure;
406 1.63.2.1 martin }
407 1.63.2.1 martin }
408 1.63.2.1 martin
409 1.63.2.1 martin configure:
410 1.63.2.1 martin /*
411 1.63.2.1 martin * If the secondary bus is configured and the bus mastering is not
412 1.63.2.1 martin * enabled, enable it.
413 1.63.2.1 martin */
414 1.63.2.1 martin if (second_configured) {
415 1.63.2.1 martin reg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
416 1.63.2.1 martin if ((reg & PCI_COMMAND_MASTER_ENABLE) == 0)
417 1.63.2.1 martin pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
418 1.63.2.1 martin reg | PCI_COMMAND_MASTER_ENABLE);
419 1.63.2.1 martin }
420 1.63.2.1 martin
421 1.36 jmcneill if (!pmf_device_register(self, ppb_suspend, ppb_resume))
422 1.36 jmcneill aprint_error_dev(self, "couldn't establish power handler\n");
423 1.36 jmcneill
424 1.1 cgd /*
425 1.63 msaitoh * Attach the PCI bus that hangs off of it.
426 1.19 thorpej *
427 1.19 thorpej * XXX Don't pass-through Memory Read Multiple. Should we?
428 1.19 thorpej * XXX Consult the spec...
429 1.1 cgd */
430 1.12 thorpej pba.pba_iot = pa->pa_iot;
431 1.12 thorpej pba.pba_memt = pa->pa_memt;
432 1.15 mycroft pba.pba_dmat = pa->pa_dmat;
433 1.26 fvdl pba.pba_dmat64 = pa->pa_dmat64;
434 1.7 cgd pba.pba_pc = pc;
435 1.19 thorpej pba.pba_flags = pa->pa_flags & ~PCI_FLAGS_MRM_OKAY;
436 1.7 cgd pba.pba_bus = PPB_BUSINFO_SECONDARY(busdata);
437 1.47 dyoung pba.pba_sub = PPB_BUSINFO_SUBORDINATE(busdata);
438 1.21 thorpej pba.pba_bridgetag = &sc->sc_tag;
439 1.7 cgd pba.pba_intrswiz = pa->pa_intrswiz;
440 1.7 cgd pba.pba_intrtag = pa->pa_intrtag;
441 1.1 cgd
442 1.29 drochner config_found_ia(self, "pcibus", &pba, pcibusprint);
443 1.1 cgd }
444 1.31 thorpej
445 1.37 dyoung static int
446 1.37 dyoung ppbdetach(device_t self, int flags)
447 1.37 dyoung {
448 1.60 msaitoh #ifdef PPB_USEINTR
449 1.57 msaitoh struct ppb_softc *sc = device_private(self);
450 1.59 msaitoh pcireg_t slcsr;
451 1.60 msaitoh #endif
452 1.37 dyoung int rc;
453 1.37 dyoung
454 1.58 chs if ((rc = config_detach_children(self, flags)) != 0)
455 1.58 chs return rc;
456 1.58 chs
457 1.60 msaitoh #ifdef PPB_USEINTR
458 1.62 msaitoh if (sc->sc_intrhand != NULL) {
459 1.62 msaitoh /* Detach event counters */
460 1.62 msaitoh evcnt_detach(&sc->sc_ev_intr);
461 1.62 msaitoh evcnt_detach(&sc->sc_ev_abp);
462 1.62 msaitoh evcnt_detach(&sc->sc_ev_pfd);
463 1.62 msaitoh evcnt_detach(&sc->sc_ev_msc);
464 1.62 msaitoh evcnt_detach(&sc->sc_ev_pdc);
465 1.62 msaitoh evcnt_detach(&sc->sc_ev_cc);
466 1.62 msaitoh evcnt_detach(&sc->sc_ev_lacs);
467 1.62 msaitoh
468 1.62 msaitoh /* Clear any pending events and disable interrupt */
469 1.62 msaitoh slcsr = pci_conf_read(sc->sc_pc, sc->sc_tag,
470 1.62 msaitoh sc->sc_pciecapoff + PCIE_SLCSR);
471 1.62 msaitoh slcsr &= ~PCIE_SLCSR_ENABLE_MASK;
472 1.62 msaitoh pci_conf_write(sc->sc_pc, sc->sc_tag,
473 1.62 msaitoh sc->sc_pciecapoff + PCIE_SLCSR, slcsr);
474 1.59 msaitoh
475 1.62 msaitoh /* Disestablish the interrupt handler */
476 1.59 msaitoh pci_intr_disestablish(sc->sc_pc, sc->sc_intrhand);
477 1.59 msaitoh pci_intr_release(sc->sc_pc, sc->sc_pihp, 1);
478 1.59 msaitoh }
479 1.60 msaitoh #endif
480 1.59 msaitoh
481 1.37 dyoung pmf_device_deregister(self);
482 1.37 dyoung return 0;
483 1.37 dyoung }
484 1.37 dyoung
485 1.36 jmcneill static bool
486 1.42 dyoung ppb_resume(device_t dv, const pmf_qual_t *qual)
487 1.36 jmcneill {
488 1.36 jmcneill struct ppb_softc *sc = device_private(dv);
489 1.36 jmcneill int off;
490 1.36 jmcneill pcireg_t val;
491 1.36 jmcneill
492 1.36 jmcneill for (off = 0x40; off <= 0xff; off += 4) {
493 1.36 jmcneill val = pci_conf_read(sc->sc_pc, sc->sc_tag, off);
494 1.36 jmcneill if (val != sc->sc_pciconfext[(off - 0x40) / 4])
495 1.36 jmcneill pci_conf_write(sc->sc_pc, sc->sc_tag, off,
496 1.36 jmcneill sc->sc_pciconfext[(off - 0x40)/4]);
497 1.36 jmcneill }
498 1.36 jmcneill
499 1.36 jmcneill return true;
500 1.36 jmcneill }
501 1.36 jmcneill
502 1.36 jmcneill static bool
503 1.42 dyoung ppb_suspend(device_t dv, const pmf_qual_t *qual)
504 1.36 jmcneill {
505 1.36 jmcneill struct ppb_softc *sc = device_private(dv);
506 1.36 jmcneill int off;
507 1.36 jmcneill
508 1.36 jmcneill for (off = 0x40; off <= 0xff; off += 4)
509 1.36 jmcneill sc->sc_pciconfext[(off - 0x40) / 4] =
510 1.36 jmcneill pci_conf_read(sc->sc_pc, sc->sc_tag, off);
511 1.36 jmcneill
512 1.36 jmcneill return true;
513 1.36 jmcneill }
514 1.36 jmcneill
515 1.37 dyoung static void
516 1.37 dyoung ppbchilddet(device_t self, device_t child)
517 1.37 dyoung {
518 1.37 dyoung /* we keep no references to child devices, so do nothing */
519 1.37 dyoung }
520 1.37 dyoung
521 1.60 msaitoh #ifdef PPB_USEINTR
522 1.57 msaitoh static int
523 1.57 msaitoh ppb_intr(void *arg)
524 1.57 msaitoh {
525 1.57 msaitoh struct ppb_softc *sc = arg;
526 1.57 msaitoh device_t dev = sc->sc_dev;
527 1.57 msaitoh pcireg_t reg;
528 1.57 msaitoh
529 1.57 msaitoh reg = pci_conf_read(sc->sc_pc, sc->sc_tag,
530 1.57 msaitoh sc->sc_pciecapoff + PCIE_SLCSR);
531 1.57 msaitoh
532 1.61 msaitoh /*
533 1.61 msaitoh * Not me. This check is only required for INTx.
534 1.61 msaitoh * ppb_intr() would be spilted int ppb_intr_legacy() and ppb_intr_msi()
535 1.61 msaitoh */
536 1.61 msaitoh if ((reg & PCIE_SLCSR_STATCHG_MASK) == 0)
537 1.61 msaitoh return 0;
538 1.61 msaitoh
539 1.57 msaitoh /* Clear interrupts. */
540 1.57 msaitoh pci_conf_write(sc->sc_pc, sc->sc_tag,
541 1.57 msaitoh sc->sc_pciecapoff + PCIE_SLCSR, reg);
542 1.57 msaitoh
543 1.61 msaitoh sc->sc_ev_intr.ev_count++;
544 1.61 msaitoh
545 1.57 msaitoh /* Attention Button Pressed */
546 1.57 msaitoh if (reg & PCIE_SLCSR_ABP) {
547 1.57 msaitoh sc->sc_ev_abp.ev_count++;
548 1.57 msaitoh if (ppb_printevent)
549 1.57 msaitoh device_printf(dev, "Attention Button Pressed\n");
550 1.57 msaitoh }
551 1.57 msaitoh
552 1.57 msaitoh /* Power Fault Detected */
553 1.57 msaitoh if (reg & PCIE_SLCSR_PFD) {
554 1.57 msaitoh sc->sc_ev_pfd.ev_count++;
555 1.57 msaitoh if (ppb_printevent)
556 1.57 msaitoh device_printf(dev, "Power Fault Detected\n");
557 1.57 msaitoh }
558 1.57 msaitoh
559 1.57 msaitoh /* MRL Sensor Changed */
560 1.57 msaitoh if (reg & PCIE_SLCSR_MSC) {
561 1.57 msaitoh sc->sc_ev_msc.ev_count++;
562 1.57 msaitoh if (ppb_printevent)
563 1.57 msaitoh device_printf(dev, "MRL Sensor Changed\n");
564 1.57 msaitoh }
565 1.57 msaitoh
566 1.57 msaitoh /* Presence Detect Changed */
567 1.57 msaitoh if (reg & PCIE_SLCSR_PDC) {
568 1.57 msaitoh sc->sc_ev_pdc.ev_count++;
569 1.57 msaitoh if (ppb_printevent)
570 1.57 msaitoh device_printf(dev, "Presence Detect Changed\n");
571 1.57 msaitoh if (reg & PCIE_SLCSR_PDS) {
572 1.57 msaitoh /* XXX Insert */
573 1.57 msaitoh } else {
574 1.57 msaitoh /* XXX Remove */
575 1.57 msaitoh }
576 1.57 msaitoh }
577 1.57 msaitoh
578 1.57 msaitoh /* Command Completed */
579 1.57 msaitoh if (reg & PCIE_SLCSR_CC) {
580 1.57 msaitoh sc->sc_ev_cc.ev_count++;
581 1.57 msaitoh if (ppb_printevent)
582 1.57 msaitoh device_printf(dev, "Command Completed\n");
583 1.57 msaitoh }
584 1.57 msaitoh
585 1.57 msaitoh /* Data Link Layer State Changed */
586 1.57 msaitoh if (reg & PCIE_SLCSR_LACS) {
587 1.57 msaitoh sc->sc_ev_lacs.ev_count++;
588 1.57 msaitoh if (ppb_printevent)
589 1.57 msaitoh device_printf(dev, "Data Link Layer State Changed\n");
590 1.57 msaitoh }
591 1.57 msaitoh
592 1.61 msaitoh return 1;
593 1.57 msaitoh }
594 1.60 msaitoh #endif /* PPB_USEINTR */
595