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