]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/bhnd/cores/usb/bhnd_ehci.c
dts: Update our device tree sources file fomr Linux 4.13
[FreeBSD/FreeBSD.git] / sys / dev / bhnd / cores / usb / bhnd_ehci.c
1 /*-
2  * Copyright (C) 2008 MARVELL INTERNATIONAL LTD.
3  * Copyright (c) 2010, Aleksandr Rybalko <ray@ddteam.net>
4  * All rights reserved.
5  *
6  * Developed by Semihalf.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of MARVELL nor the names of contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 /*
37  * BHND attachment driver for the USB Enhanced Host Controller.
38  * Ported from ZRouter with insignificant adaptations for FreeBSD11.
39  */
40
41 #include "opt_bus.h"
42
43 #include <sys/stdint.h>
44 #include <sys/stddef.h>
45 #include <sys/param.h>
46 #include <sys/queue.h>
47 #include <sys/rman.h>
48 #include <sys/types.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/bus.h>
52 #include <sys/linker_set.h>
53 #include <sys/module.h>
54 #include <sys/lock.h>
55 #include <sys/mutex.h>
56 #include <sys/condvar.h>
57 #include <sys/sysctl.h>
58 #include <sys/sx.h>
59 #include <sys/unistd.h>
60 #include <sys/callout.h>
61 #include <sys/malloc.h>
62 #include <sys/priv.h>
63
64 #include <dev/usb/usb.h>
65 #include <dev/usb/usbdi.h>
66
67 #include <dev/usb/usb_core.h>
68 #include <dev/usb/usb_busdma.h>
69 #include <dev/usb/usb_process.h>
70 #include <dev/usb/usb_util.h>
71
72 #include <dev/usb/usb_controller.h>
73 #include <dev/usb/usb_bus.h>
74 #include <dev/usb/controller/ehci.h>
75 #include <dev/usb/controller/ehcireg.h>
76
77 #include <dev/bhnd/bhnd.h>
78
79 #define EHCI_HC_DEVSTR          "Broadcom EHCI"
80
81 #define USB_BRIDGE_INTR_CAUSE   0x210
82 #define USB_BRIDGE_INTR_MASK    0x214
83
84 static device_attach_t  bhnd_ehci_attach;
85 static device_detach_t  bhnd_ehci_detach;
86
87 static int              bhnd_ehci_probe(device_t self);
88 static void             bhnd_ehci_post_reset(struct ehci_softc *ehci_softc);
89
90 static int
91 bhnd_ehci_probe(device_t self)
92 {
93
94         device_set_desc(self, EHCI_HC_DEVSTR);
95
96         return (BUS_PROBE_DEFAULT);
97 }
98
99 static void
100 bhnd_ehci_post_reset(struct ehci_softc *ehci_softc)
101 {
102         uint32_t        usbmode;
103
104         /* Force HOST mode */
105         usbmode = EOREAD4(ehci_softc, EHCI_USBMODE_NOLPM);
106         usbmode &= ~EHCI_UM_CM;
107         usbmode |= EHCI_UM_CM_HOST;
108         EOWRITE4(ehci_softc, EHCI_USBMODE_NOLPM, usbmode);
109 }
110
111 static int
112 bhnd_ehci_attach(device_t self)
113 {
114         ehci_softc_t    *sc;
115         int              err;
116         int              rid;
117
118         sc = device_get_softc(self);
119         /* initialise some bus fields */
120         sc->sc_bus.parent = self;
121         sc->sc_bus.devices = sc->sc_devices;
122         sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
123         sc->sc_bus.usbrev = USB_REV_2_0;
124         sc->sc_bus.dma_bits = 32;
125
126         /* get all DMA memory */
127         if ((err = usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(self),
128             &ehci_iterate_hw_softc)) != 0) {
129                 BHND_ERROR_DEV(self, "can't allocate DMA memory: %d", err);
130                 return (ENOMEM);
131         }
132
133         rid = 0;
134         sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, 
135             RF_ACTIVE);
136         if (!sc->sc_io_res) {
137                 BHND_ERROR_DEV(self, "Could not map memory");
138                 goto error;
139         }
140         sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
141         sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
142         sc->sc_io_size = rman_get_size(sc->sc_io_res);
143
144         rid = 0;
145         sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
146             RF_SHAREABLE | RF_ACTIVE);
147
148         if (sc->sc_irq_res == NULL) {
149                 BHND_ERROR_DEV(self, "Could not allocate error irq");
150                 bhnd_ehci_detach(self);
151                 return (ENXIO);
152         }
153
154         sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
155         if (!sc->sc_bus.bdev) {
156                 BHND_ERROR_DEV(self, "Could not add USB device");
157                 goto error;
158         }
159         device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
160         device_set_desc(sc->sc_bus.bdev, EHCI_HC_DEVSTR);
161
162         sprintf(sc->sc_vendor, "Broadcom");
163
164         err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
165             NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
166         if (err) {
167                 BHND_ERROR_DEV(self, "Could not setup irq, %d", err);
168                 sc->sc_intr_hdl = NULL;
169                 goto error;
170         }
171
172         sc->sc_flags |= EHCI_SCFLG_LOSTINTRBUG;
173         sc->sc_vendor_post_reset = bhnd_ehci_post_reset;
174
175         err = ehci_init(sc);
176         if (!err) {
177                 err = device_probe_and_attach(sc->sc_bus.bdev);
178         }
179         if (err) {
180                 BHND_ERROR_DEV(self, "USB init failed err=%d", err);
181                 goto error;
182         }
183         return (0);
184
185 error:
186         bhnd_ehci_detach(self);
187         return (ENXIO);
188 }
189
190 static int
191 bhnd_ehci_detach(device_t self)
192 {
193         ehci_softc_t    *sc;
194         int              err;
195
196         sc = device_get_softc(self);
197
198         /* during module unload there are lots of children leftover */
199         device_delete_children(self);
200
201         /*
202          * disable interrupts that might have been switched on in ehci_init
203          */
204 #ifdef notyet
205         if (sc->sc_io_res) {
206                 EWRITE4(sc, EHCI_USBINTR, 0);
207                 EWRITE4(sc, USB_BRIDGE_INTR_MASK, 0);
208         }
209 #endif
210         if (sc->sc_irq_res && sc->sc_intr_hdl) {
211                 /*
212                  * only call ehci_detach() after ehci_init()
213                  */
214                 ehci_detach(sc);
215
216                 err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
217
218                 if (err)
219                         /* XXX or should we panic? */
220                         BHND_ERROR_DEV(self, "Could not tear down irq, %d", err);
221
222                 sc->sc_intr_hdl = NULL;
223         }
224         if (sc->sc_irq_res) {
225                 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
226                 sc->sc_irq_res = NULL;
227         }
228         if (sc->sc_io_res) {
229                 bus_release_resource(self, SYS_RES_MEMORY, 0, sc->sc_io_res);
230                 sc->sc_io_res = NULL;
231         }
232         usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
233
234         return (0);
235 }
236
237 static device_method_t ehci_methods[] = {
238         /* Device interface */
239         DEVMETHOD(device_probe,         bhnd_ehci_probe),
240         DEVMETHOD(device_attach,        bhnd_ehci_attach),
241         DEVMETHOD(device_detach,        bhnd_ehci_detach),
242         DEVMETHOD(device_suspend,       bus_generic_suspend),
243         DEVMETHOD(device_resume,        bus_generic_resume),
244         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
245
246         /* Bus interface */
247         DEVMETHOD(bus_print_child,      bus_generic_print_child),
248
249         {0, 0}
250 };
251
252 static driver_t ehci_driver = {
253         "ehci",
254         ehci_methods,
255         sizeof(ehci_softc_t),
256 };
257
258 static devclass_t ehci_devclass;
259
260 DRIVER_MODULE(ehci, bhnd_usb, ehci_driver, ehci_devclass, 0, 0);
261 MODULE_DEPEND(ehci, usb, 1, 1, 1);