xref: /freebsd/sys/dev/usb/controller/generic_ehci.c (revision 1d386b48)
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 #include "opt_bus.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/rman.h>
42 #include <sys/condvar.h>
43 #include <sys/kernel.h>
44 #include <sys/module.h>
45 
46 #include <machine/bus.h>
47 
48 #include <dev/usb/usb.h>
49 #include <dev/usb/usbdi.h>
50 
51 #include <dev/usb/usb_core.h>
52 #include <dev/usb/usb_busdma.h>
53 #include <dev/usb/usb_process.h>
54 #include <dev/usb/usb_util.h>
55 
56 #include <dev/usb/usb_controller.h>
57 #include <dev/usb/usb_bus.h>
58 #include <dev/usb/controller/ehci.h>
59 #include <dev/usb/controller/ehcireg.h>
60 
61 #include "generic_ehci.h"
62 
63 int
64 generic_ehci_attach(device_t self)
65 {
66 	ehci_softc_t *sc = device_get_softc(self);
67 	int err;
68 	int rid;
69 
70 	/* initialise some bus fields */
71 	sc->sc_bus.parent = self;
72 	sc->sc_bus.devices = sc->sc_devices;
73 	sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
74 	sc->sc_bus.dma_bits = 32;
75 
76 	/* get all DMA memory */
77 	if (usb_bus_mem_alloc_all(&sc->sc_bus,
78 	    USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
79 		return (ENOMEM);
80 	}
81 
82 	sc->sc_bus.usbrev = USB_REV_2_0;
83 
84 	rid = 0;
85 	sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
86 	    RF_ACTIVE);
87 	if (!sc->sc_io_res) {
88 		device_printf(self, "Could not map memory\n");
89 		goto error;
90 	}
91 
92 	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
93 	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
94 	sc->sc_io_size = rman_get_size(sc->sc_io_res);
95 
96 	rid = 0;
97 	sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
98 	    RF_SHAREABLE | RF_ACTIVE);
99 	if (sc->sc_irq_res == NULL) {
100 		device_printf(self, "Could not allocate irq\n");
101 		goto error;
102 	}
103 	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
104 	if (!sc->sc_bus.bdev) {
105 		device_printf(self, "Could not add USB device\n");
106 		goto error;
107 	}
108 	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
109 
110 	strlcpy(sc->sc_vendor, "Generic", sizeof(sc->sc_vendor));
111 
112 	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
113 	    NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
114 	if (err) {
115 		device_printf(self, "Could not setup irq, %d\n", err);
116 		sc->sc_intr_hdl = NULL;
117 		goto error;
118 	}
119 
120 	sc->sc_flags |= EHCI_SCFLG_DONTRESET;
121 
122 	err = ehci_init(sc);
123 	if (!err)
124 		err = device_probe_and_attach(sc->sc_bus.bdev);
125 	if (err)
126 		goto error;
127 
128 	return (0);
129 
130 error:
131 	generic_ehci_detach(self);
132 	return (ENXIO);
133 }
134 
135 int
136 generic_ehci_detach(device_t self)
137 {
138 	ehci_softc_t *sc = device_get_softc(self);
139 	int err;
140 
141 	/* during module unload there are lots of children leftover */
142 	device_delete_children(self);
143 
144 	if (sc->sc_irq_res && sc->sc_intr_hdl) {
145 		/*
146 		 * only call ehci_detach() after ehci_init()
147 		 */
148 		ehci_detach(sc);
149 
150 		err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
151 
152 		if (err)
153 			/* XXX or should we panic? */
154 			device_printf(self, "Could not tear down irq, %d\n",
155 			    err);
156 		sc->sc_intr_hdl = NULL;
157 	}
158 
159 	if (sc->sc_irq_res) {
160 		bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
161 		sc->sc_irq_res = NULL;
162 	}
163 	if (sc->sc_io_res) {
164 		bus_release_resource(self, SYS_RES_MEMORY, 0,
165 		    sc->sc_io_res);
166 		sc->sc_io_res = NULL;
167 	}
168 	usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
169 
170 	return (0);
171 }
172 
173 static device_method_t ehci_methods[] = {
174 	/* Device interface */
175 	DEVMETHOD(device_attach,	generic_ehci_attach),
176 	DEVMETHOD(device_detach,	generic_ehci_detach),
177 	DEVMETHOD(device_suspend,	bus_generic_suspend),
178 	DEVMETHOD(device_resume,	bus_generic_resume),
179 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
180 
181 	DEVMETHOD_END
182 };
183 
184 driver_t generic_ehci_driver = {
185 	.name = "ehci",
186 	.methods = ehci_methods,
187 	.size = sizeof(ehci_softc_t),
188 };
189