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