]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/controller/generic_ohci.c
Add driver for "generic-ohci" as defined by FDT.
[FreeBSD/FreeBSD.git] / sys / dev / usb / controller / generic_ohci.c
1 /*-
2  * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
3  * Copyright (c) 2016 Emmanuel Vadot <manu@freebsd.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 /*
29  * Generic OHCI driver based on AT91 OHCI
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bus.h>
38 #include <sys/rman.h>
39 #include <sys/condvar.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42
43 #include <machine/bus.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usbdi.h>
49
50 #include <dev/usb/usb_core.h>
51 #include <dev/usb/usb_busdma.h>
52 #include <dev/usb/usb_process.h>
53 #include <dev/usb/usb_util.h>
54
55 #include <dev/usb/usb_controller.h>
56 #include <dev/usb/usb_bus.h>
57 #include <dev/usb/controller/ohci.h>
58 #include <dev/usb/controller/ohcireg.h>
59
60 #ifdef EXT_RESOURCES
61 #include <dev/extres/clk/clk.h>
62 #include <dev/extres/hwreset/hwreset.h>
63 #endif
64
65 #include "generic_usb_if.h"
66
67 #ifdef EXT_RESOURCES
68 struct clk_list {
69         TAILQ_ENTRY(clk_list)   next;
70         clk_t                   clk;
71 };
72 #endif
73
74 struct generic_ohci_softc {
75         ohci_softc_t    ohci_sc;
76
77 #ifdef EXT_RESOURCES
78         hwreset_t       rst;
79         TAILQ_HEAD(, clk_list)  clk_list;
80 #endif
81 };
82
83 static int generic_ohci_detach(device_t);
84
85 static int
86 generic_ohci_probe(device_t dev)
87 {
88
89         if (!ofw_bus_status_okay(dev))
90                 return (ENXIO);
91
92         if (!ofw_bus_is_compatible(dev, "generic-ohci"))
93                 return (ENXIO);
94
95         device_set_desc(dev, "Generic OHCI Controller");
96
97         return (BUS_PROBE_DEFAULT);
98 }
99
100 static int
101 generic_ohci_attach(device_t dev)
102 {
103         struct generic_ohci_softc *sc = device_get_softc(dev);
104         int err, rid;
105 #ifdef EXT_RESOURCES
106         int off;
107         struct clk_list *clkp;
108         clk_t clk;
109 #endif
110
111         sc->ohci_sc.sc_bus.parent = dev;
112         sc->ohci_sc.sc_bus.devices = sc->ohci_sc.sc_devices;
113         sc->ohci_sc.sc_bus.devices_max = OHCI_MAX_DEVICES;
114         sc->ohci_sc.sc_bus.dma_bits = 32;
115
116         /* get all DMA memory */
117         if (usb_bus_mem_alloc_all(&sc->ohci_sc.sc_bus,
118             USB_GET_DMA_TAG(dev), &ohci_iterate_hw_softc)) {
119                 return (ENOMEM);
120         }
121
122         rid = 0;
123         sc->ohci_sc.sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
124             &rid, RF_ACTIVE);
125         if (sc->ohci_sc.sc_io_res == 0) {
126                 err = ENOMEM;
127                 goto error;
128         }
129
130         sc->ohci_sc.sc_io_tag = rman_get_bustag(sc->ohci_sc.sc_io_res);
131         sc->ohci_sc.sc_io_hdl = rman_get_bushandle(sc->ohci_sc.sc_io_res);
132         sc->ohci_sc.sc_io_size = rman_get_size(sc->ohci_sc.sc_io_res);
133
134         rid = 0;
135         sc->ohci_sc.sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
136             RF_ACTIVE);
137         if (sc->ohci_sc.sc_irq_res == 0) {
138                 err = ENXIO;
139                 goto error;
140         }
141         sc->ohci_sc.sc_bus.bdev = device_add_child(dev, "usbus", -1);
142         if (sc->ohci_sc.sc_bus.bdev == 0) {
143                 err = ENXIO;
144                 goto error;
145         }
146         device_set_ivars(sc->ohci_sc.sc_bus.bdev, &sc->ohci_sc.sc_bus);
147
148         strlcpy(sc->ohci_sc.sc_vendor, "Generic",
149             sizeof(sc->ohci_sc.sc_vendor));
150
151         err = bus_setup_intr(dev, sc->ohci_sc.sc_irq_res,
152             INTR_TYPE_BIO | INTR_MPSAFE, NULL,
153             (driver_intr_t *)ohci_interrupt, sc, &sc->ohci_sc.sc_intr_hdl);
154         if (err) {
155                 sc->ohci_sc.sc_intr_hdl = NULL;
156                 goto error;
157         }
158
159 #ifdef EXT_RESOURCES
160         TAILQ_INIT(&sc->clk_list);
161         /* Enable clock */
162         for (off = 0; clk_get_by_ofw_index(dev, off, &clk) == 0; off++) {
163                 err = clk_enable(clk);
164                 if (err != 0) {
165                         device_printf(dev, "Could not enable clock %s\n",
166                             clk_get_name(clk));
167                         goto error;
168                 }
169                 clkp = malloc(sizeof(clkp), M_DEVBUF, M_WAITOK | M_ZERO);
170                 clkp->clk = clk;
171                 TAILQ_INSERT_TAIL(&sc->clk_list, clkp, next);
172         }
173
174         /* De-assert reset */
175         if (hwreset_get_by_ofw_idx(dev, 0, &sc->rst) == 0) {
176                 err = hwreset_deassert(sc->rst);
177                 if (err != 0) {
178                         device_printf(dev, "Could not de-assert reset %d\n",
179                             off);
180                         goto error;
181                 }
182         }
183 #endif
184
185         if (GENERIC_USB_INIT(dev) != 0) {
186                 err = ENXIO;
187                 goto error;
188         }
189
190         err = ohci_init(&sc->ohci_sc);
191         if (err == 0)
192                 err = device_probe_and_attach(sc->ohci_sc.sc_bus.bdev);
193         if (err)
194                 goto error;
195
196         return (0);
197 error:
198         generic_ohci_detach(dev);
199         return (err);
200 }
201
202 static int
203 generic_ohci_detach(device_t dev)
204 {
205         struct generic_ohci_softc *sc = device_get_softc(dev);
206         device_t bdev;
207         int err;
208 #ifdef EXT_RESOURCES
209         struct clk_list *clk, *clk_tmp;
210 #endif
211
212         if (sc->ohci_sc.sc_bus.bdev) {
213                 bdev = sc->ohci_sc.sc_bus.bdev;
214                 device_detach(bdev);
215                 device_delete_child(dev, bdev);
216         }
217
218         /* during module unload there are lots of children leftover */
219         device_delete_children(dev);
220
221         /*
222          * Put the controller into reset, then disable clocks and do
223          * the MI tear down.  We have to disable the clocks/hardware
224          * after we do the rest of the teardown.  We also disable the
225          * clocks in the opposite order we acquire them, but that
226          * doesn't seem to be absolutely necessary.  We free up the
227          * clocks after we disable them, so the system could, in
228          * theory, reuse them.
229          */
230         bus_space_write_4(sc->ohci_sc.sc_io_tag, sc->ohci_sc.sc_io_hdl,
231             OHCI_CONTROL, 0);
232
233         if (sc->ohci_sc.sc_irq_res && sc->ohci_sc.sc_intr_hdl) {
234                 /*
235                  * only call ohci_detach() after ohci_init()
236                  */
237                 ohci_detach(&sc->ohci_sc);
238
239                 err = bus_teardown_intr(dev, sc->ohci_sc.sc_irq_res,
240                     sc->ohci_sc.sc_intr_hdl);
241                 sc->ohci_sc.sc_intr_hdl = NULL;
242         }
243         if (sc->ohci_sc.sc_irq_res) {
244                 bus_release_resource(dev, SYS_RES_IRQ, 0,
245                     sc->ohci_sc.sc_irq_res);
246                 sc->ohci_sc.sc_irq_res = NULL;
247         }
248         if (sc->ohci_sc.sc_io_res) {
249                 bus_release_resource(dev, SYS_RES_MEMORY, 0,
250                     sc->ohci_sc.sc_io_res);
251                 sc->ohci_sc.sc_io_res = NULL;
252         }
253         usb_bus_mem_free_all(&sc->ohci_sc.sc_bus, &ohci_iterate_hw_softc);
254
255 #ifdef EXT_RESOURCES
256         /* Disable clock */
257         TAILQ_FOREACH_SAFE(clk, &sc->clk_list, next, clk_tmp) {
258                 err = clk_disable(clk->clk);
259                 if (err != 0)
260                         device_printf(dev, "Could not disable clock %s\n",
261                             clk_get_name(clk->clk));
262                 err = clk_release(clk->clk);
263                 if (err != 0)
264                         device_printf(dev, "Could not release clock %s\n",
265                             clk_get_name(clk->clk));
266                 TAILQ_REMOVE(&sc->clk_list, clk, next);
267                 free(clk, M_DEVBUF);
268         }
269
270         /* De-assert reset */
271         if (sc->rst) {
272                 err = hwreset_assert(sc->rst);
273                 if (err != 0)
274                         device_printf(dev, "Could not assert reset\n");
275                 hwreset_release(sc->rst);
276         }
277 #endif
278
279         if (GENERIC_USB_DEINIT(dev) != 0)
280                 return (ENXIO);
281
282         return (0);
283 }
284
285 static device_method_t generic_ohci_methods[] = {
286         /* Device interface */
287         DEVMETHOD(device_probe, generic_ohci_probe),
288         DEVMETHOD(device_attach, generic_ohci_attach),
289         DEVMETHOD(device_detach, generic_ohci_detach),
290
291         DEVMETHOD(device_suspend, bus_generic_suspend),
292         DEVMETHOD(device_resume, bus_generic_resume),
293         DEVMETHOD(device_shutdown, bus_generic_shutdown),
294
295         DEVMETHOD_END
296 };
297
298 driver_t generic_ohci_driver = {
299         .name = "ohci",
300         .methods = generic_ohci_methods,
301         .size = sizeof(struct generic_ohci_softc),
302 };
303
304 static devclass_t generic_ohci_devclass;
305
306 DRIVER_MODULE(ohci, simplebus, generic_ohci_driver,
307     generic_ohci_devclass, 0, 0);
308 MODULE_DEPEND(ohci, usb, 1, 1, 1);