ehci_pci.c revision 1.70 1 /* $NetBSD: ehci_pci.c,v 1.70 2019/06/13 17:33:34 maxv Exp $ */
2
3 /*
4 * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart (at) augustsson.net).
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: ehci_pci.c,v 1.70 2019/06/13 17:33:34 maxv Exp $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
39 #include <sys/proc.h>
40 #include <sys/queue.h>
41
42 #include <sys/bus.h>
43
44 #include <dev/pci/pcidevs.h>
45 #include <dev/pci/pcivar.h>
46 #include <dev/pci/usb_pci.h>
47
48 #include <dev/usb/usb.h>
49 #include <dev/usb/usbdi.h>
50 #include <dev/usb/usbdivar.h>
51 #include <dev/usb/usb_mem.h>
52
53 #include <dev/usb/ehcireg.h>
54 #include <dev/usb/ehcivar.h>
55
56 #ifdef EHCI_DEBUG
57 #define DPRINTF(x) if (ehcidebug) printf x
58 extern int ehcidebug;
59 #else
60 #define DPRINTF(x)
61 #endif
62
63 enum ehci_pci_quirk_flags {
64 EHCI_PCI_QUIRK_AMD_SB600 = 0x1, /* always need a quirk */
65 EHCI_PCI_QUIRK_AMD_SB700 = 0x2, /* depends on the SMB revision */
66 };
67
68 static const struct pci_quirkdata ehci_pci_quirks[] = {
69 { PCI_VENDOR_ATI, PCI_PRODUCT_ATI_SB600_USB_EHCI,
70 EHCI_PCI_QUIRK_AMD_SB600 },
71 { PCI_VENDOR_ATI, PCI_PRODUCT_ATI_SB700_USB_EHCI,
72 EHCI_PCI_QUIRK_AMD_SB700 },
73 };
74
75 static void ehci_release_ownership(ehci_softc_t *, pci_chipset_tag_t, pcitag_t);
76 static void ehci_get_ownership(ehci_softc_t *, pci_chipset_tag_t, pcitag_t);
77 static bool ehci_pci_suspend(device_t, const pmf_qual_t *);
78 static bool ehci_pci_resume(device_t, const pmf_qual_t *);
79
80 struct ehci_pci_softc {
81 ehci_softc_t sc;
82 pci_chipset_tag_t sc_pc;
83 pcitag_t sc_tag;
84 pci_intr_handle_t *sc_pihp;
85 void *sc_ih; /* interrupt vectoring */
86 enum {
87 EHCI_INIT_NONE,
88 EHCI_INIT_INITED
89 } sc_init_state;
90 };
91
92 static int ehci_sb700_match(const struct pci_attach_args *);
93 static int ehci_apply_amd_quirks(struct ehci_pci_softc *);
94 static enum ehci_pci_quirk_flags ehci_pci_lookup_quirkdata(pci_vendor_id_t,
95 pci_product_id_t);
96
97 #define EHCI_MAX_BIOS_WAIT 100 /* ms*10 */
98 #define EHCI_SBx00_WORKAROUND_REG 0x50
99 #define EHCI_SBx00_WORKAROUND_ENABLE __BIT(27)
100
101 static int
102 ehci_pci_match(device_t parent, cfdata_t match, void *aux)
103 {
104 struct pci_attach_args *pa = (struct pci_attach_args *) aux;
105
106 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS &&
107 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB &&
108 PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_EHCI)
109 return 1;
110
111 return 0;
112 }
113
114 static void
115 ehci_pci_attach(device_t parent, device_t self, void *aux)
116 {
117 struct ehci_pci_softc *sc = device_private(self);
118 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
119 pci_chipset_tag_t pc = pa->pa_pc;
120 pcitag_t tag = pa->pa_tag;
121 char intrbuf[PCI_INTRSTR_LEN];
122 char const *intrstr;
123 struct usb_pci *up;
124 int ncomp, quirk;
125 pcireg_t csr;
126
127 sc->sc_init_state = EHCI_INIT_NONE;
128 sc->sc.sc_dev = self;
129 sc->sc.sc_bus.ub_hcpriv = sc;
130
131 pci_aprint_devinfo(pa, "USB controller");
132
133 /* Check for quirks */
134 quirk = ehci_pci_lookup_quirkdata(PCI_VENDOR(pa->pa_id),
135 PCI_PRODUCT(pa->pa_id));
136
137 /* Map I/O registers */
138 if (pci_mapreg_map(pa, PCI_CBMEM, PCI_MAPREG_TYPE_MEM, 0,
139 &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
140 sc->sc.sc_size = 0;
141 aprint_error_dev(self, "can't map memory space\n");
142 return;
143 }
144
145 sc->sc_pc = pc;
146 sc->sc_tag = tag;
147 sc->sc.sc_bus.ub_dmatag = pa->pa_dmat;
148
149 /* Disable interrupts, so we don't get any spurious ones. */
150 sc->sc.sc_offs = EREAD1(&sc->sc, EHCI_CAPLENGTH);
151 DPRINTF(("%s: offs=%d\n", device_xname(self), sc->sc.sc_offs));
152 EOWRITE4(&sc->sc, EHCI_USBINTR, 0);
153
154 /* Handle quirks */
155 switch (quirk) {
156 case EHCI_PCI_QUIRK_AMD_SB600:
157 ehci_apply_amd_quirks(sc);
158 break;
159 case EHCI_PCI_QUIRK_AMD_SB700:
160 if (pci_find_device(NULL, ehci_sb700_match))
161 ehci_apply_amd_quirks(sc);
162 break;
163 }
164
165 pcireg_t intr = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
166 int pin = PCI_INTERRUPT_PIN(intr);
167
168 /* Enable the device. */
169 csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
170 csr |= PCI_COMMAND_MASTER_ENABLE;
171 csr &= ~(pin ? PCI_COMMAND_INTERRUPT_DISABLE : 0);
172 pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
173
174 /* Map and establish the interrupt. */
175 if (pci_intr_alloc(pa, &sc->sc_pihp, NULL, 0) != 0) {
176 aprint_error_dev(self, "couldn't map interrupt\n");
177 goto fail;
178 }
179
180 /*
181 * Allocate IRQ
182 */
183 intrstr = pci_intr_string(pc, sc->sc_pihp[0], intrbuf, sizeof(intrbuf));
184 sc->sc_ih = pci_intr_establish_xname(pc, sc->sc_pihp[0], IPL_USB,
185 ehci_intr, sc, device_xname(self));
186 if (sc->sc_ih == NULL) {
187 pci_intr_release(sc->sc_pc, sc->sc_pihp, 1);
188 sc->sc_pihp = NULL;
189
190 aprint_error_dev(self, "couldn't establish interrupt");
191 if (intrstr != NULL)
192 aprint_error(" at %s", intrstr);
193 aprint_error("\n");
194 goto fail;
195 }
196 aprint_normal_dev(self, "interrupting at %s\n", intrstr);
197
198 switch (pci_conf_read(pc, tag, PCI_USBREV) & PCI_USBREV_MASK) {
199 case PCI_USBREV_PRE_1_0:
200 case PCI_USBREV_1_0:
201 case PCI_USBREV_1_1:
202 sc->sc.sc_bus.ub_revision = USBREV_UNKNOWN;
203 aprint_verbose_dev(self, "pre-2.0 USB rev, device ignored\n");
204 goto fail;
205 case PCI_USBREV_2_0:
206 sc->sc.sc_bus.ub_revision = USBREV_2_0;
207 break;
208 default:
209 sc->sc.sc_bus.ub_revision = USBREV_UNKNOWN;
210 break;
211 }
212
213 /* Enable workaround for dropped interrupts as required */
214 switch (PCI_VENDOR(pa->pa_id)) {
215 case PCI_VENDOR_ATI:
216 case PCI_VENDOR_VIATECH:
217 sc->sc.sc_flags |= EHCIF_DROPPED_INTR_WORKAROUND;
218 aprint_normal_dev(self, "dropped intr workaround enabled\n");
219 break;
220 default:
221 break;
222 }
223
224 /*
225 * Find companion controllers. According to the spec they always
226 * have lower function numbers so they should be enumerated already.
227 */
228 const u_int maxncomp = EHCI_HCS_N_CC(EREAD4(&sc->sc, EHCI_HCSPARAMS));
229 KASSERT(maxncomp <= EHCI_COMPANION_MAX);
230 ncomp = 0;
231 TAILQ_FOREACH(up, &ehci_pci_alldevs, next) {
232 if (up->bus == pa->pa_bus && up->device == pa->pa_device &&
233 !up->claimed) {
234 DPRINTF(("ehci_pci_attach: companion %s\n",
235 device_xname(up->usb)));
236 sc->sc.sc_comps[ncomp++] = up->usb;
237 up->claimed = true;
238 if (ncomp == maxncomp)
239 break;
240 }
241 }
242 sc->sc.sc_ncomp = ncomp;
243
244 ehci_get_ownership(&sc->sc, pc, tag);
245
246 int err = ehci_init(&sc->sc);
247 if (err) {
248 aprint_error_dev(self, "init failed, error=%d\n", err);
249 goto fail;
250 }
251 sc->sc_init_state = EHCI_INIT_INITED;
252
253 if (!pmf_device_register1(self, ehci_pci_suspend, ehci_pci_resume,
254 ehci_shutdown))
255 aprint_error_dev(self, "couldn't establish power handler\n");
256
257 /* Attach usb device. */
258 sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint);
259 return;
260
261 fail:
262 if (sc->sc_ih) {
263 pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
264 sc->sc_ih = NULL;
265 }
266 if (sc->sc.sc_size) {
267 ehci_release_ownership(&sc->sc, sc->sc_pc, sc->sc_tag);
268 bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
269 sc->sc.sc_size = 0;
270 }
271 }
272
273 static int
274 ehci_pci_detach(device_t self, int flags)
275 {
276 struct ehci_pci_softc *sc = device_private(self);
277 int rv;
278
279 if (sc->sc_init_state >= EHCI_INIT_INITED) {
280 rv = ehci_detach(&sc->sc, flags);
281 if (rv)
282 return rv;
283 }
284
285 pmf_device_deregister(self);
286 ehci_shutdown(self, flags);
287
288 /* disable interrupts */
289 EOWRITE4(&sc->sc, EHCI_USBINTR, 0);
290 /* XXX grotty hack to flush the write */
291 (void)EOREAD4(&sc->sc, EHCI_USBINTR);
292
293 if (sc->sc_ih != NULL) {
294 pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
295 sc->sc_ih = NULL;
296 }
297
298 if (sc->sc_pihp != NULL) {
299 pci_intr_release(sc->sc_pc, sc->sc_pihp, 1);
300 sc->sc_pihp = NULL;
301 }
302
303 if (sc->sc.sc_size) {
304 ehci_release_ownership(&sc->sc, sc->sc_pc, sc->sc_tag);
305 bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
306 sc->sc.sc_size = 0;
307 }
308
309 #if 1
310 /* XXX created in ehci.c */
311 if (sc->sc_init_state >= EHCI_INIT_INITED) {
312 mutex_destroy(&sc->sc.sc_lock);
313 mutex_destroy(&sc->sc.sc_intr_lock);
314 softint_disestablish(sc->sc.sc_doorbell_si);
315 softint_disestablish(sc->sc.sc_pcd_si);
316 }
317 #endif
318
319 return 0;
320 }
321
322 CFATTACH_DECL3_NEW(ehci_pci, sizeof(struct ehci_pci_softc),
323 ehci_pci_match, ehci_pci_attach, ehci_pci_detach, ehci_activate, NULL,
324 ehci_childdet, DVF_DETACH_SHUTDOWN);
325
326 #ifdef EHCI_DEBUG
327 static void
328 ehci_dump_caps(ehci_softc_t *sc, pci_chipset_tag_t pc, pcitag_t tag)
329 {
330 uint32_t cparams, legctlsts, addr, cap, id;
331 int maxdump = 10;
332
333 cparams = EREAD4(sc, EHCI_HCCPARAMS);
334 addr = EHCI_HCC_EECP(cparams);
335 while (addr != 0) {
336 cap = pci_conf_read(pc, tag, addr);
337 id = EHCI_CAP_GET_ID(cap);
338 switch (id) {
339 case EHCI_CAP_ID_LEGACY:
340 legctlsts = pci_conf_read(pc, tag,
341 addr + PCI_EHCI_USBLEGCTLSTS);
342 printf("ehci_dump_caps: legsup=0x%08x "
343 "legctlsts=0x%08x\n", cap, legctlsts);
344 break;
345 default:
346 printf("ehci_dump_caps: cap=0x%08x\n", cap);
347 break;
348 }
349 if (--maxdump < 0)
350 break;
351 addr = EHCI_CAP_GET_NEXT(cap);
352 }
353 }
354 #endif
355
356 static void
357 ehci_release_ownership(ehci_softc_t *sc, pci_chipset_tag_t pc, pcitag_t tag)
358 {
359 const char *devname = device_xname(sc->sc_dev);
360 uint32_t cparams, addr, cap;
361 pcireg_t legsup;
362 int maxcap = 10;
363
364 cparams = EREAD4(sc, EHCI_HCCPARAMS);
365 addr = EHCI_HCC_EECP(cparams);
366 while (addr != 0) {
367 cap = pci_conf_read(pc, tag, addr);
368 if (EHCI_CAP_GET_ID(cap) != EHCI_CAP_ID_LEGACY)
369 goto next;
370 legsup = pci_conf_read(pc, tag, addr + PCI_EHCI_USBLEGSUP);
371 pci_conf_write(pc, tag, addr + PCI_EHCI_USBLEGSUP,
372 legsup & ~EHCI_LEG_HC_OS_OWNED);
373
374 next:
375 if (--maxcap < 0) {
376 aprint_normal("%s: broken extended capabilities "
377 "ignored\n", devname);
378 return;
379 }
380 addr = EHCI_CAP_GET_NEXT(cap);
381 }
382 }
383
384 static void
385 ehci_get_ownership(ehci_softc_t *sc, pci_chipset_tag_t pc, pcitag_t tag)
386 {
387 const char *devname = device_xname(sc->sc_dev);
388 uint32_t cparams, addr, cap;
389 pcireg_t legsup;
390 int maxcap = 10;
391 int ms;
392
393 #ifdef EHCI_DEBUG
394 if (ehcidebug)
395 ehci_dump_caps(sc, pc, tag);
396 #endif
397 cparams = EREAD4(sc, EHCI_HCCPARAMS);
398 addr = EHCI_HCC_EECP(cparams);
399 while (addr != 0) {
400 cap = pci_conf_read(pc, tag, addr);
401 if (EHCI_CAP_GET_ID(cap) != EHCI_CAP_ID_LEGACY)
402 goto next;
403 legsup = pci_conf_read(pc, tag, addr + PCI_EHCI_USBLEGSUP);
404 if (legsup & EHCI_LEG_HC_BIOS_OWNED) {
405 /* Ask BIOS to give up ownership */
406 pci_conf_write(pc, tag, addr + PCI_EHCI_USBLEGSUP,
407 legsup | EHCI_LEG_HC_OS_OWNED);
408 for (ms = 0; ms < EHCI_MAX_BIOS_WAIT; ms++) {
409 legsup = pci_conf_read(pc, tag,
410 addr + PCI_EHCI_USBLEGSUP);
411 if (!(legsup & EHCI_LEG_HC_BIOS_OWNED))
412 break;
413 delay(10000);
414 }
415 if (ms == EHCI_MAX_BIOS_WAIT) {
416 aprint_normal("%s: BIOS refuses to give up "
417 "ownership, using force\n", devname);
418 pci_conf_write(pc, tag,
419 addr + PCI_EHCI_USBLEGSUP, 0);
420 } else
421 aprint_verbose("%s: BIOS has given up "
422 "ownership\n", devname);
423 }
424
425 /* Disable SMIs */
426 pci_conf_write(pc, tag, addr + PCI_EHCI_USBLEGCTLSTS, 0);
427
428 next:
429 if (--maxcap < 0) {
430 aprint_normal("%s: broken extended capabilities "
431 "ignored\n", devname);
432 return;
433 }
434 addr = EHCI_CAP_GET_NEXT(cap);
435 }
436
437 }
438
439 static bool
440 ehci_pci_suspend(device_t dv, const pmf_qual_t *qual)
441 {
442 struct ehci_pci_softc *sc = device_private(dv);
443
444 ehci_suspend(dv, qual);
445 ehci_release_ownership(&sc->sc, sc->sc_pc, sc->sc_tag);
446
447 return true;
448 }
449
450 static bool
451 ehci_pci_resume(device_t dv, const pmf_qual_t *qual)
452 {
453 struct ehci_pci_softc *sc = device_private(dv);
454
455 ehci_get_ownership(&sc->sc, sc->sc_pc, sc->sc_tag);
456 return ehci_resume(dv, qual);
457 }
458
459 static int
460 ehci_sb700_match(const struct pci_attach_args *pa)
461 {
462 if (!(PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ATI &&
463 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_SB600_SMB))
464 return 0;
465
466 switch (PCI_REVISION(pa->pa_class)) {
467 case 0x3a:
468 case 0x3b:
469 return 1;
470 }
471
472 return 0;
473 }
474
475 static int
476 ehci_apply_amd_quirks(struct ehci_pci_softc *sc)
477 {
478 pcireg_t value;
479
480 aprint_normal_dev(sc->sc.sc_dev,
481 "applying AMD SB600/SB700 USB freeze workaround\n");
482 value = pci_conf_read(sc->sc_pc, sc->sc_tag, EHCI_SBx00_WORKAROUND_REG);
483 pci_conf_write(sc->sc_pc, sc->sc_tag, EHCI_SBx00_WORKAROUND_REG,
484 value | EHCI_SBx00_WORKAROUND_ENABLE);
485
486 return 0;
487 }
488
489 static enum ehci_pci_quirk_flags
490 ehci_pci_lookup_quirkdata(pci_vendor_id_t vendor, pci_product_id_t product)
491 {
492 int i;
493
494 for (i = 0; i < __arraycount(ehci_pci_quirks); i++) {
495 if (vendor == ehci_pci_quirks[i].vendor &&
496 product == ehci_pci_quirks[i].product)
497 return ehci_pci_quirks[i].quirks;
498 }
499 return 0;
500 }
501
502