xref: /freebsd/sys/dev/usb/controller/generic_ehci.c (revision 13ea0450a9c8742119d36f3bf8f47accdce46e54)
1 /*-
2  * Copyright (c) 2012 Ganbold Tsagaankhuu <ganbold@freebsd.org>
3  * Copyright (c) 2016 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * This software was developed by Andrew Turner under
7  * sponsorship from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 /*
32  * Generic EHCI driver based on the Allwinner A10 EHCI driver
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include "opt_bus.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/bus.h>
43 #include <sys/rman.h>
44 #include <sys/condvar.h>
45 #include <sys/kernel.h>
46 #include <sys/module.h>
47 
48 #include <machine/bus.h>
49 
50 #include <dev/usb/usb.h>
51 #include <dev/usb/usbdi.h>
52 
53 #include <dev/usb/usb_core.h>
54 #include <dev/usb/usb_busdma.h>
55 #include <dev/usb/usb_process.h>
56 #include <dev/usb/usb_util.h>
57 
58 #include <dev/usb/usb_controller.h>
59 #include <dev/usb/usb_bus.h>
60 #include <dev/usb/controller/ehci.h>
61 #include <dev/usb/controller/ehcireg.h>
62 
63 #include <contrib/dev/acpica/include/acpi.h>
64 #include <contrib/dev/acpica/include/accommon.h>
65 #include <dev/acpica/acpivar.h>
66 
67 static device_attach_t generic_ehci_attach;
68 static device_detach_t generic_ehci_detach;
69 
70 static int
71 generic_ehci_probe(device_t self)
72 {
73 	ACPI_HANDLE h;
74 
75 	if ((h = acpi_get_handle(self)) == NULL ||
76 	    !acpi_MatchHid(h, "PNP0D20"))
77 		return (ENXIO);
78 
79 	device_set_desc(self, "Generic EHCI Controller");
80 	return (BUS_PROBE_DEFAULT);
81 }
82 
83 static int
84 generic_ehci_attach(device_t self)
85 {
86 	ehci_softc_t *sc = device_get_softc(self);
87 	int err;
88 	int rid;
89 
90 	/* initialise some bus fields */
91 	sc->sc_bus.parent = self;
92 	sc->sc_bus.devices = sc->sc_devices;
93 	sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
94 	sc->sc_bus.dma_bits = 32;
95 
96 	/* get all DMA memory */
97 	if (usb_bus_mem_alloc_all(&sc->sc_bus,
98 	    USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
99 		return (ENOMEM);
100 	}
101 
102 	sc->sc_bus.usbrev = USB_REV_2_0;
103 
104 	rid = 0;
105 	sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
106 	    RF_ACTIVE);
107 	if (!sc->sc_io_res) {
108 		device_printf(self, "Could not map memory\n");
109 		goto error;
110 	}
111 
112 	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
113 	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
114 	sc->sc_io_size = rman_get_size(sc->sc_io_res);
115 
116 	rid = 0;
117 	sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
118 	    RF_SHAREABLE | RF_ACTIVE);
119 	if (sc->sc_irq_res == NULL) {
120 		device_printf(self, "Could not allocate irq\n");
121 		goto error;
122 	}
123 	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
124 	if (!sc->sc_bus.bdev) {
125 		device_printf(self, "Could not add USB device\n");
126 		goto error;
127 	}
128 	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
129 
130 	strlcpy(sc->sc_vendor, "Generic", sizeof(sc->sc_vendor));
131 
132 	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
133 	    NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
134 	if (err) {
135 		device_printf(self, "Could not setup irq, %d\n", err);
136 		sc->sc_intr_hdl = NULL;
137 		goto error;
138 	}
139 
140 	sc->sc_flags |= EHCI_SCFLG_DONTRESET;
141 
142 	err = ehci_init(sc);
143 	if (!err)
144 		err = device_probe_and_attach(sc->sc_bus.bdev);
145 	if (err)
146 		goto error;
147 
148 	return (0);
149 
150 error:
151 	generic_ehci_detach(self);
152 	return (ENXIO);
153 }
154 
155 static int
156 generic_ehci_detach(device_t self)
157 {
158 	ehci_softc_t *sc = device_get_softc(self);
159 	int err;
160 
161 	/* during module unload there are lots of children leftover */
162 	device_delete_children(self);
163 
164 	if (sc->sc_irq_res && sc->sc_intr_hdl) {
165 		/*
166 		 * only call ehci_detach() after ehci_init()
167 		 */
168 		ehci_detach(sc);
169 
170 		err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
171 
172 		if (err)
173 			/* XXX or should we panic? */
174 			device_printf(self, "Could not tear down irq, %d\n",
175 			    err);
176 		sc->sc_intr_hdl = NULL;
177 	}
178 
179 	if (sc->sc_irq_res) {
180 		bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
181 		sc->sc_irq_res = NULL;
182 	}
183 	if (sc->sc_io_res) {
184 		bus_release_resource(self, SYS_RES_MEMORY, 0,
185 		    sc->sc_io_res);
186 		sc->sc_io_res = NULL;
187 	}
188 	usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
189 
190 	return (0);
191 }
192 
193 static device_method_t ehci_methods[] = {
194 	/* Device interface */
195 	DEVMETHOD(device_probe,		generic_ehci_probe),
196 	DEVMETHOD(device_attach,	generic_ehci_attach),
197 	DEVMETHOD(device_detach,	generic_ehci_detach),
198 	DEVMETHOD(device_suspend,	bus_generic_suspend),
199 	DEVMETHOD(device_resume,	bus_generic_resume),
200 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
201 
202 	DEVMETHOD_END
203 };
204 
205 static driver_t ehci_driver = {
206 	.name = "ehci",
207 	.methods = ehci_methods,
208 	.size = sizeof(ehci_softc_t),
209 };
210 
211 static devclass_t ehci_devclass;
212 
213 DRIVER_MODULE(ehci, acpi, ehci_driver, ehci_devclass, 0, 0);
214 MODULE_DEPEND(ehci, usb, 1, 1, 1);
215