1 /* $OpenBSD: ehci_acpi.c,v 1.4 2024/10/09 00:38:26 jsg Exp $ */
2 /*
3 * Copyright (c) 2018 Mark Kettenis
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18 #include <sys/param.h>
19 #include <sys/systm.h>
20 #include <sys/device.h>
21
22 #include <machine/bus.h>
23 #include <machine/intr.h>
24
25 #include <dev/acpi/acpireg.h>
26 #include <dev/acpi/acpivar.h>
27 #include <dev/acpi/acpidev.h>
28 #include <dev/acpi/amltypes.h>
29 #include <dev/acpi/dsdt.h>
30
31 #include <dev/usb/usb.h>
32 #include <dev/usb/usbdi.h>
33 #include <dev/usb/usbdivar.h>
34 #include <dev/usb/usb_mem.h>
35
36 #include <dev/usb/ehcireg.h>
37 #include <dev/usb/ehcivar.h>
38
39 struct ehci_acpi_softc {
40 struct ehci_softc sc;
41 struct acpi_softc *sc_acpi;
42 struct aml_node *sc_node;
43 void *sc_ih;
44 };
45
46 int ehci_acpi_match(struct device *, void *, void *);
47 void ehci_acpi_attach(struct device *, struct device *, void *);
48
49 const struct cfattach ehci_acpi_ca = {
50 sizeof(struct ehci_acpi_softc), ehci_acpi_match, ehci_acpi_attach,
51 NULL, ehci_activate
52 };
53
54 const char *ehci_hids[] = {
55 "PNP0D20",
56 "PNP0D25",
57 NULL
58 };
59
60 int
ehci_acpi_match(struct device * parent,void * match,void * aux)61 ehci_acpi_match(struct device *parent, void *match, void *aux)
62 {
63 struct acpi_attach_args *aaa = aux;
64 struct cfdata *cf = match;
65
66 if (aaa->aaa_naddr < 1 || aaa->aaa_nirq < 1)
67 return 0;
68 return acpi_matchhids(aaa, ehci_hids, cf->cf_driver->cd_name);
69 }
70
71 void
ehci_acpi_attach(struct device * parent,struct device * self,void * aux)72 ehci_acpi_attach(struct device *parent, struct device *self, void *aux)
73 {
74 struct ehci_acpi_softc *sc = (struct ehci_acpi_softc *)self;
75 struct acpi_attach_args *aaa = aux;
76 int error;
77
78 sc->sc_acpi = (struct acpi_softc *)parent;
79 sc->sc_node = aaa->aaa_node;
80 printf(" %s", sc->sc_node->name);
81
82 printf(" addr 0x%llx/0x%llx", aaa->aaa_addr[0], aaa->aaa_size[0]);
83 printf(" irq %d", aaa->aaa_irq[0]);
84
85 sc->sc.iot = aaa->aaa_bst[0];
86 sc->sc.sc_size = aaa->aaa_size[0];
87 sc->sc.sc_bus.dmatag = aaa->aaa_dmat;
88
89 if (bus_space_map(sc->sc.iot, aaa->aaa_addr[0], aaa->aaa_size[0],
90 0, &sc->sc.ioh)) {
91 printf(": can't map registers\n");
92 return;
93 }
94
95 /* Disable interrupts, so we don't get any spurious ones. */
96 sc->sc.sc_offs = EREAD1(&sc->sc, EHCI_CAPLENGTH);
97 EOWRITE2(&sc->sc, EHCI_USBINTR, 0);
98
99 sc->sc_ih = acpi_intr_establish(aaa->aaa_irq[0], aaa->aaa_irq_flags[0],
100 IPL_USB, ehci_intr, sc, sc->sc.sc_bus.bdev.dv_xname);
101 if (sc->sc_ih == NULL) {
102 printf(": can't establish interrupt\n");
103 goto unmap;
104 }
105
106 printf("\n");
107
108 strlcpy(sc->sc.sc_vendor, "Generic", sizeof(sc->sc.sc_vendor));
109 error = ehci_init(&sc->sc);
110 if (error) {
111 printf("%s: init failed, error=%d\n",
112 sc->sc.sc_bus.bdev.dv_xname, error);
113 goto disestablish_ret;
114 }
115
116 /* Attach usb device. */
117 config_found(self, &sc->sc.sc_bus, usbctlprint);
118
119 return;
120
121 disestablish_ret:
122 acpi_intr_disestablish(sc->sc_ih);
123 unmap:
124 bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
125 return;
126 }
127