xref: /openbsd/sys/dev/acpi/ohci_acpi.c (revision 4bdff4be)
1 /*	$OpenBSD: ohci_acpi.c,v 1.2 2022/04/06 18:59:27 naddy 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/ohcireg.h>
37 #include <dev/usb/ohcivar.h>
38 
39 struct ohci_acpi_softc {
40 	struct ohci_softc sc;
41 	struct acpi_softc *sc_acpi;
42 	struct aml_node *sc_node;
43 	void		*sc_ih;
44 };
45 
46 int	ohci_acpi_match(struct device *, void *, void *);
47 void	ohci_acpi_attach(struct device *, struct device *, void *);
48 
49 const struct cfattach ohci_acpi_ca = {
50 	sizeof(struct ohci_acpi_softc), ohci_acpi_match, ohci_acpi_attach
51 };
52 
53 void	ohci_acpi_attach_deferred(struct device *);
54 
55 int
56 ohci_acpi_match(struct device *parent, void *match, void *aux)
57 {
58 	struct acpi_attach_args *aaa = aux;
59 
60 	return acpi_matchcls(aaa, PCI_CLASS_SERIALBUS,
61 	    PCI_SUBCLASS_SERIALBUS_USB, PCI_INTERFACE_OHCI);
62 }
63 
64 void
65 ohci_acpi_attach(struct device *parent, struct device *self, void *aux)
66 {
67 	struct ohci_acpi_softc *sc = (struct ohci_acpi_softc *)self;
68 	struct acpi_attach_args *aaa = aux;
69 
70 	sc->sc_acpi = (struct acpi_softc *)parent;
71 	sc->sc_node = aaa->aaa_node;
72 	printf(" %s", sc->sc_node->name);
73 
74 	printf(" addr 0x%llx/0x%llx", aaa->aaa_addr[0], aaa->aaa_size[0]);
75 	printf(" irq %d", aaa->aaa_irq[0]);
76 
77 	sc->sc.iot = aaa->aaa_bst[0];
78 	sc->sc.sc_size = aaa->aaa_size[0];
79 	sc->sc.sc_bus.dmatag = aaa->aaa_dmat;
80 
81 	if (bus_space_map(sc->sc.iot, aaa->aaa_addr[0], aaa->aaa_size[0],
82 	    0, &sc->sc.ioh)) {
83 		printf(": can't map registers\n");
84 		return;
85 	}
86 
87 	/* Record what interrupts were enabled by SMM/BIOS. */
88 	sc->sc.sc_intre = bus_space_read_4(sc->sc.iot, sc->sc.ioh,
89 	    OHCI_INTERRUPT_ENABLE);
90 
91 	/* Disable interrupts, so we don't get any spurious ones. */
92 	bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE,
93 	    OHCI_MIE);
94 
95 	bus_space_barrier(sc->sc.iot, sc->sc.ioh, 0, sc->sc.sc_size,
96 	    BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE);
97 	bus_space_write_4(sc->sc.iot, sc->sc.ioh,
98 	    OHCI_INTERRUPT_DISABLE, OHCI_MIE);
99 
100 	/* Map and establish the interrupt. */
101 	sc->sc_ih = acpi_intr_establish(aaa->aaa_irq[0], aaa->aaa_irq_flags[0],
102 	    IPL_USB, ohci_intr, sc, sc->sc.sc_bus.bdev.dv_xname);
103 	if (sc->sc_ih == NULL) {
104 		printf(": can't establish interrupt\n");
105 		goto unmap;
106 	}
107 
108 	printf(": ");
109 
110 	strlcpy(sc->sc.sc_vendor, "Generic", sizeof(sc->sc.sc_vendor));
111 
112 	/* Display revision and perform legacy emulation handover. */
113 	if (ohci_checkrev(&sc->sc) != USBD_NORMAL_COMPLETION ||
114 	    ohci_handover(&sc->sc) != USBD_NORMAL_COMPLETION)
115 		goto disestablish_ret;
116 
117 	/* Ignore interrupts for now */
118 	sc->sc.sc_bus.dying = 1;
119 
120 	config_defer(self, ohci_acpi_attach_deferred);
121 	return;
122 
123 disestablish_ret:
124 	acpi_intr_disestablish(sc->sc_ih);
125 unmap:
126 	bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
127 	return;
128 }
129 
130 void
131 ohci_acpi_attach_deferred(struct device *self)
132 {
133 	struct ohci_acpi_softc *sc = (struct ohci_acpi_softc *)self;
134 	usbd_status r;
135 	int s;
136 
137 	s = splusb();
138 	sc->sc.sc_bus.dying = 0;
139 	r = ohci_init(&sc->sc);
140 	splx(s);
141 
142 	if (r != USBD_NORMAL_COMPLETION) {
143 		printf("%s: init failed, error=%d\n",
144 		    sc->sc.sc_bus.bdev.dv_xname, r);
145 		acpi_intr_disestablish(sc->sc_ih);
146 		sc->sc_ih = NULL;
147 		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
148 		sc->sc.sc_size = 0;
149 		return;
150 	}
151 
152 	/* Attach usb device. */
153 	config_found(self, &sc->sc.sc_bus, usbctlprint);
154 }
155