xref: /netbsd-src/sys/dev/pci/ehci_pci.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: ehci_pci.c,v 1.73 2021/12/22 21:45:02 skrll 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@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.73 2021/12/22 21:45:02 skrll 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 
148 	const uint32_t hccparams = EREAD4(&sc->sc, EHCI_HCCPARAMS);
149 
150 	if (EHCI_HCC_64BIT(hccparams)) {
151 		aprint_verbose_dev(self, "64-bit DMA");
152 		if (pci_dma64_available(pa)) {
153 			sc->sc.sc_bus.ub_dmatag = pa->pa_dmat64;
154 			aprint_verbose("\n");
155 		} else {
156 			aprint_verbose(" - limited\n");
157 			sc->sc.sc_bus.ub_dmatag = pa->pa_dmat;
158 		}
159 	} else {
160 		aprint_verbose_dev(self, "32-bit DMA\n");
161 		sc->sc.sc_bus.ub_dmatag = pa->pa_dmat;
162 	}
163 
164 	/* Disable interrupts, so we don't get any spurious ones. */
165 	sc->sc.sc_offs = EREAD1(&sc->sc, EHCI_CAPLENGTH);
166 	DPRINTF(("%s: offs=%d\n", device_xname(self), sc->sc.sc_offs));
167 	EOWRITE4(&sc->sc, EHCI_USBINTR, 0);
168 
169 	/* Handle quirks */
170 	switch (quirk) {
171 	case EHCI_PCI_QUIRK_AMD_SB600:
172 		ehci_apply_amd_quirks(sc);
173 		break;
174 	case EHCI_PCI_QUIRK_AMD_SB700:
175 		if (pci_find_device(NULL, ehci_sb700_match))
176 			ehci_apply_amd_quirks(sc);
177 		break;
178 	}
179 
180 	pcireg_t intr = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
181 	int pin = PCI_INTERRUPT_PIN(intr);
182 
183 	/* Enable the device. */
184 	csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
185 	csr |= PCI_COMMAND_MASTER_ENABLE;
186 	csr &= ~(pin ? PCI_COMMAND_INTERRUPT_DISABLE : 0);
187 	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
188 
189 	/* Map and establish the interrupt. */
190 	if (pci_intr_alloc(pa, &sc->sc_pihp, NULL, 0) != 0) {
191 		aprint_error_dev(self, "couldn't map interrupt\n");
192 		goto fail;
193 	}
194 
195 	/*
196 	 * Allocate IRQ
197 	 */
198 	intrstr = pci_intr_string(pc, sc->sc_pihp[0], intrbuf, sizeof(intrbuf));
199 	sc->sc_ih = pci_intr_establish_xname(pc, sc->sc_pihp[0], IPL_USB,
200 	    ehci_intr, sc, device_xname(self));
201 	if (sc->sc_ih == NULL) {
202 		pci_intr_release(sc->sc_pc, sc->sc_pihp, 1);
203 		sc->sc_pihp = NULL;
204 
205 		aprint_error_dev(self, "couldn't establish interrupt");
206 		if (intrstr != NULL)
207 			aprint_error(" at %s", intrstr);
208 		aprint_error("\n");
209 		goto fail;
210 	}
211 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
212 
213 	switch (pci_conf_read(pc, tag, PCI_USBREV) & PCI_USBREV_MASK) {
214 	case PCI_USBREV_PRE_1_0:
215 	case PCI_USBREV_1_0:
216 	case PCI_USBREV_1_1:
217 		sc->sc.sc_bus.ub_revision = USBREV_UNKNOWN;
218 		aprint_verbose_dev(self, "pre-2.0 USB rev, device ignored\n");
219 		goto fail;
220 	case PCI_USBREV_2_0:
221 		sc->sc.sc_bus.ub_revision = USBREV_2_0;
222 		break;
223 	default:
224 		sc->sc.sc_bus.ub_revision = USBREV_UNKNOWN;
225 		break;
226 	}
227 
228 	/* Enable workaround for dropped interrupts as required */
229 	switch (PCI_VENDOR(pa->pa_id)) {
230 	case PCI_VENDOR_ATI:
231 	case PCI_VENDOR_VIATECH:
232 		sc->sc.sc_flags |= EHCIF_DROPPED_INTR_WORKAROUND;
233 		aprint_normal_dev(self, "dropped intr workaround enabled\n");
234 		break;
235 	default:
236 		break;
237 	}
238 
239 	/*
240 	 * Find companion controllers.  According to the spec they always
241 	 * have lower function numbers so they should be enumerated already.
242 	 */
243 	const u_int maxncomp = EHCI_HCS_N_CC(EREAD4(&sc->sc, EHCI_HCSPARAMS));
244 	KASSERT(maxncomp <= EHCI_COMPANION_MAX);
245 	ncomp = 0;
246 	TAILQ_FOREACH(up, &ehci_pci_alldevs, next) {
247 		if (up->bus == pa->pa_bus && up->device == pa->pa_device &&
248 		    !up->claimed) {
249 			DPRINTF(("ehci_pci_attach: companion %s\n",
250 			    device_xname(up->usb)));
251 			sc->sc.sc_comps[ncomp++] = up->usb;
252 			up->claimed = true;
253 			if (ncomp == maxncomp)
254 				break;
255 		}
256 	}
257 	sc->sc.sc_ncomp = ncomp;
258 
259 	ehci_get_ownership(&sc->sc, pc, tag);
260 
261 	int err = ehci_init(&sc->sc);
262 	if (err) {
263 		aprint_error_dev(self, "init failed, error=%d\n", err);
264 		goto fail;
265 	}
266 	sc->sc_init_state = EHCI_INIT_INITED;
267 
268 	if (!pmf_device_register1(self, ehci_pci_suspend, ehci_pci_resume,
269 	    ehci_shutdown))
270 		aprint_error_dev(self, "couldn't establish power handler\n");
271 
272 	/* Attach usb device. */
273 	sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint,
274 	    CFARGS_NONE);
275 	return;
276 
277 fail:
278 	if (sc->sc_ih) {
279 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
280 		sc->sc_ih = NULL;
281 	}
282 	if (sc->sc.sc_size) {
283 		ehci_release_ownership(&sc->sc, sc->sc_pc, sc->sc_tag);
284 		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
285 		sc->sc.sc_size = 0;
286 	}
287 }
288 
289 static int
290 ehci_pci_detach(device_t self, int flags)
291 {
292 	struct ehci_pci_softc *sc = device_private(self);
293 	int rv;
294 
295 	if (sc->sc_init_state >= EHCI_INIT_INITED) {
296 		rv = ehci_detach(&sc->sc, flags);
297 		if (rv)
298 			return rv;
299 	}
300 
301 	pmf_device_deregister(self);
302 	ehci_shutdown(self, flags);
303 
304 	/* disable interrupts */
305 	EOWRITE4(&sc->sc, EHCI_USBINTR, 0);
306 	/* XXX grotty hack to flush the write */
307 	(void)EOREAD4(&sc->sc, EHCI_USBINTR);
308 
309 	if (sc->sc_ih != NULL) {
310 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
311 		sc->sc_ih = NULL;
312 	}
313 
314 	if (sc->sc_pihp != NULL) {
315 		pci_intr_release(sc->sc_pc, sc->sc_pihp, 1);
316 		sc->sc_pihp = NULL;
317 	}
318 
319 	if (sc->sc.sc_size) {
320 		ehci_release_ownership(&sc->sc, sc->sc_pc, sc->sc_tag);
321 		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
322 		sc->sc.sc_size = 0;
323 	}
324 
325 #if 1
326 	/* XXX created in ehci.c */
327 	if (sc->sc_init_state >= EHCI_INIT_INITED) {
328 		mutex_destroy(&sc->sc.sc_lock);
329 		mutex_destroy(&sc->sc.sc_intr_lock);
330 		softint_disestablish(sc->sc.sc_doorbell_si);
331 		softint_disestablish(sc->sc.sc_pcd_si);
332 	}
333 #endif
334 
335 	return 0;
336 }
337 
338 CFATTACH_DECL3_NEW(ehci_pci, sizeof(struct ehci_pci_softc),
339     ehci_pci_match, ehci_pci_attach, ehci_pci_detach, ehci_activate, NULL,
340     ehci_childdet, DVF_DETACH_SHUTDOWN);
341 
342 #ifdef EHCI_DEBUG
343 static void
344 ehci_dump_caps(ehci_softc_t *sc, pci_chipset_tag_t pc, pcitag_t tag)
345 {
346 	uint32_t cparams, legctlsts, addr, cap, id;
347 	int maxdump = 10;
348 
349 	cparams = EREAD4(sc, EHCI_HCCPARAMS);
350 	addr = EHCI_HCC_EECP(cparams);
351 	while (addr != 0) {
352 		cap = pci_conf_read(pc, tag, addr);
353 		id = EHCI_CAP_GET_ID(cap);
354 		switch (id) {
355 		case EHCI_CAP_ID_LEGACY:
356 			legctlsts = pci_conf_read(pc, tag,
357 			    addr + PCI_EHCI_USBLEGCTLSTS);
358 			printf("ehci_dump_caps: legsup=0x%08x "
359 			       "legctlsts=0x%08x\n", cap, legctlsts);
360 			break;
361 		default:
362 			printf("ehci_dump_caps: cap=0x%08x\n", cap);
363 			break;
364 		}
365 		if (--maxdump < 0)
366 			break;
367 		addr = EHCI_CAP_GET_NEXT(cap);
368 	}
369 }
370 #endif
371 
372 static void
373 ehci_release_ownership(ehci_softc_t *sc, pci_chipset_tag_t pc, pcitag_t tag)
374 {
375 	const char *devname = device_xname(sc->sc_dev);
376 	uint32_t cparams, addr, cap;
377 	pcireg_t legsup;
378 	int maxcap = 10;
379 
380 	cparams = EREAD4(sc, EHCI_HCCPARAMS);
381 	addr = EHCI_HCC_EECP(cparams);
382 	while (addr != 0) {
383 		cap = pci_conf_read(pc, tag, addr);
384 		if (EHCI_CAP_GET_ID(cap) != EHCI_CAP_ID_LEGACY)
385 			goto next;
386 		legsup = pci_conf_read(pc, tag, addr + PCI_EHCI_USBLEGSUP);
387 		pci_conf_write(pc, tag, addr + PCI_EHCI_USBLEGSUP,
388 		    legsup & ~EHCI_LEG_HC_OS_OWNED);
389 
390 next:
391 		if (--maxcap < 0) {
392 			aprint_normal("%s: broken extended capabilities "
393 				      "ignored\n", devname);
394 			return;
395 		}
396 		addr = EHCI_CAP_GET_NEXT(cap);
397 	}
398 }
399 
400 static void
401 ehci_get_ownership(ehci_softc_t *sc, pci_chipset_tag_t pc, pcitag_t tag)
402 {
403 	const char *devname = device_xname(sc->sc_dev);
404 	uint32_t cparams, addr, cap;
405 	pcireg_t legsup;
406 	int maxcap = 10;
407 	int ms;
408 
409 #ifdef EHCI_DEBUG
410 	if (ehcidebug)
411 		ehci_dump_caps(sc, pc, tag);
412 #endif
413 	cparams = EREAD4(sc, EHCI_HCCPARAMS);
414 	addr = EHCI_HCC_EECP(cparams);
415 	while (addr != 0) {
416 		cap = pci_conf_read(pc, tag, addr);
417 		if (EHCI_CAP_GET_ID(cap) != EHCI_CAP_ID_LEGACY)
418 			goto next;
419 		legsup = pci_conf_read(pc, tag, addr + PCI_EHCI_USBLEGSUP);
420 		if (legsup & EHCI_LEG_HC_BIOS_OWNED) {
421 			/* Ask BIOS to give up ownership */
422 			pci_conf_write(pc, tag, addr + PCI_EHCI_USBLEGSUP,
423 			    legsup | EHCI_LEG_HC_OS_OWNED);
424 			for (ms = 0; ms < EHCI_MAX_BIOS_WAIT; ms++) {
425 				legsup = pci_conf_read(pc, tag,
426 				    addr + PCI_EHCI_USBLEGSUP);
427 				if (!(legsup & EHCI_LEG_HC_BIOS_OWNED))
428 					break;
429 				delay(10000);
430 			}
431 			if (ms == EHCI_MAX_BIOS_WAIT) {
432 				aprint_normal("%s: BIOS refuses to give up "
433 				    "ownership, using force\n", devname);
434 				pci_conf_write(pc, tag,
435 				    addr + PCI_EHCI_USBLEGSUP, 0);
436 			} else
437 				aprint_verbose("%s: BIOS has given up "
438 				    "ownership\n", devname);
439 		}
440 
441 		/* Disable SMIs */
442 		pci_conf_write(pc, tag, addr + PCI_EHCI_USBLEGCTLSTS, 0);
443 
444 next:
445 		if (--maxcap < 0) {
446 			aprint_normal("%s: broken extended capabilities "
447 				      "ignored\n", devname);
448 			return;
449 		}
450 		addr = EHCI_CAP_GET_NEXT(cap);
451 	}
452 
453 }
454 
455 static bool
456 ehci_pci_suspend(device_t dv, const pmf_qual_t *qual)
457 {
458 	struct ehci_pci_softc *sc = device_private(dv);
459 
460 	ehci_suspend(dv, qual);
461 	ehci_release_ownership(&sc->sc, sc->sc_pc, sc->sc_tag);
462 
463 	return true;
464 }
465 
466 static bool
467 ehci_pci_resume(device_t dv, const pmf_qual_t *qual)
468 {
469 	struct ehci_pci_softc *sc = device_private(dv);
470 
471 	ehci_get_ownership(&sc->sc, sc->sc_pc, sc->sc_tag);
472 	return ehci_resume(dv, qual);
473 }
474 
475 static int
476 ehci_sb700_match(const struct pci_attach_args *pa)
477 {
478 	if (!(PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ATI &&
479 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_SB600_SMB))
480 		return 0;
481 
482 	switch (PCI_REVISION(pa->pa_class)) {
483 	case 0x3a:
484 	case 0x3b:
485 		return 1;
486 	}
487 
488 	return 0;
489 }
490 
491 static int
492 ehci_apply_amd_quirks(struct ehci_pci_softc *sc)
493 {
494 	pcireg_t value;
495 
496 	aprint_normal_dev(sc->sc.sc_dev,
497 	    "applying AMD SB600/SB700 USB freeze workaround\n");
498 	value = pci_conf_read(sc->sc_pc, sc->sc_tag, EHCI_SBx00_WORKAROUND_REG);
499 	pci_conf_write(sc->sc_pc, sc->sc_tag, EHCI_SBx00_WORKAROUND_REG,
500 	    value | EHCI_SBx00_WORKAROUND_ENABLE);
501 
502 	return 0;
503 }
504 
505 static enum ehci_pci_quirk_flags
506 ehci_pci_lookup_quirkdata(pci_vendor_id_t vendor, pci_product_id_t product)
507 {
508 	int i;
509 
510 	for (i = 0; i < __arraycount(ehci_pci_quirks); i++) {
511 		if (vendor == ehci_pci_quirks[i].vendor &&
512 		    product == ehci_pci_quirks[i].product)
513 			return ehci_pci_quirks[i].quirks;
514 	}
515 	return 0;
516 }
517 
518