]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/arm/econa/ehci_ebus.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / arm / econa / ehci_ebus.c
1 /*-
2  * Copyright (C) 2009 Yohanes Nugroho <yohanes@gmail.com>
3  * based on ehci_mbus.c
4  * Copyright (C) 2008 MARVELL INTERNATIONAL LTD.
5  * All rights reserved.
6  *
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
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include "opt_bus.h"
38
39 #include <machine/resource.h>
40
41 #include <sys/stdint.h>
42 #include <sys/stddef.h>
43 #include <sys/param.h>
44 #include <sys/queue.h>
45 #include <sys/types.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/bus.h>
49 #include <sys/module.h>
50 #include <sys/lock.h>
51 #include <sys/mutex.h>
52 #include <sys/condvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/sx.h>
55 #include <sys/unistd.h>
56 #include <sys/callout.h>
57 #include <sys/malloc.h>
58 #include <sys/priv.h>
59
60 #include <sys/rman.h>
61
62 #include <dev/usb/usb.h>
63 #include <dev/usb/usbdi.h>
64
65 #include <dev/usb/usb_core.h>
66 #include <dev/usb/usb_busdma.h>
67 #include <dev/usb/usb_process.h>
68 #include <dev/usb/usb_util.h>
69
70 #include <dev/usb/usb_controller.h>
71 #include <dev/usb/usb_bus.h>
72 #include <dev/usb/controller/ehci.h>
73 #include <dev/usb/controller/ehcireg.h>
74
75
76 static device_attach_t ehci_ebus_attach;
77 static device_detach_t ehci_ebus_detach;
78 static device_shutdown_t ehci_ebus_shutdown;
79 static device_suspend_t ehci_ebus_suspend;
80 static device_resume_t ehci_ebus_resume;
81
82
83 static void *ih_err;
84
85 #define EHCI_HC_DEVSTR "CNS11XX USB EHCI"
86 #define USB_BRIDGE_INTR_MASK   0x214
87
88 static int
89 ehci_ebus_suspend(device_t self)
90 {
91         ehci_softc_t *sc = device_get_softc(self);
92         int err;
93
94         err = bus_generic_suspend(self);
95         if (err)
96                 return (err);
97         ehci_suspend(sc);
98         return (0);
99 }
100
101 static int
102 ehci_ebus_resume(device_t self)
103 {
104         ehci_softc_t *sc = device_get_softc(self);
105
106         ehci_resume(sc);
107
108         bus_generic_resume(self);
109
110         return (0);
111 }
112
113 static int
114 ehci_ebus_shutdown(device_t self)
115 {
116         ehci_softc_t *sc = device_get_softc(self);
117         int err;
118
119         err = bus_generic_shutdown(self);
120         if (err)
121                 return (err);
122         ehci_shutdown(sc);
123
124         return (0);
125 }
126
127 static int
128 ehci_ebus_probe(device_t self)
129 {
130
131         device_set_desc(self, EHCI_HC_DEVSTR);
132
133         return (BUS_PROBE_DEFAULT);
134 }
135
136 static int
137 ehci_ebus_attach(device_t self)
138 {
139         ehci_softc_t *sc = device_get_softc(self);
140         bus_space_handle_t bsh;
141         int err;
142         int rid;
143
144         /* initialise some bus fields */
145         sc->sc_bus.parent = self;
146         sc->sc_bus.devices = sc->sc_devices;
147         sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
148
149         /* get all DMA memory */
150         if (usb_bus_mem_alloc_all(&sc->sc_bus,
151             USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
152                 return (ENOMEM);
153         }
154
155         sc->sc_bus.usbrev = USB_REV_2_0;
156
157         rid = 0;
158         sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY,
159             &rid, RF_ACTIVE);
160         if (!sc->sc_io_res) {
161                 device_printf(self, "Could not map memory\n");
162                 goto error;
163         }
164         sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
165         bsh = rman_get_bushandle(sc->sc_io_res);
166
167         /*magic, undocumented initialization*/
168         bus_space_write_4((sc)->sc_io_tag, bsh, 0x04, 0x106);
169
170         bus_space_write_4((sc)->sc_io_tag, bsh, 0x40, (3 << 5)|0x2000);
171
172         DELAY(1000);
173
174         sc->sc_io_size =  4096;
175
176         if (bus_space_subregion(sc->sc_io_tag, bsh, 0x4000000,
177             sc->sc_io_size, &sc->sc_io_hdl) != 0)
178                 panic("%s: unable to subregion USB host registers",
179                     device_get_name(self));
180
181         rid = 0;
182         sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
183             RF_SHAREABLE | RF_ACTIVE);
184         if (sc->sc_irq_res == NULL) {
185                 device_printf(self, "Could not allocate irq\n");
186                 ehci_ebus_detach(self);
187                 return (ENXIO);
188         }
189
190         sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
191         if (!sc->sc_bus.bdev) {
192                 device_printf(self, "Could not add USB device\n");
193                 goto error;
194         }
195         device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
196         device_set_desc(sc->sc_bus.bdev, EHCI_HC_DEVSTR);
197
198         sprintf(sc->sc_vendor, "Cavium");
199
200         err = bus_setup_intr(self,sc->sc_irq_res,
201             INTR_TYPE_BIO | INTR_MPSAFE, NULL,
202             (driver_intr_t *)ehci_interrupt, sc,
203             &sc->sc_intr_hdl);
204         if (err) {
205                 device_printf(self, "Could not setup error irq, %d\n", err);
206                 ih_err = NULL;
207                 goto error;
208         }
209
210         err = ehci_init(sc);
211         if (!err) {
212                 err = device_probe_and_attach(sc->sc_bus.bdev);
213         }
214         if (err) {
215                 device_printf(self, "USB init failed err=%d\n", err);
216                 goto error;
217         }
218         return (0);
219
220 error:
221         ehci_ebus_detach(self);
222         return (ENXIO);
223 }
224
225 static int
226 ehci_ebus_detach(device_t self)
227 {
228         ehci_softc_t *sc = device_get_softc(self);
229         device_t bdev;
230         int err;
231
232         if (sc->sc_bus.bdev) {
233                 bdev = sc->sc_bus.bdev;
234                 device_detach(bdev);
235                 device_delete_child(self, bdev);
236         }
237         /* during module unload there are lots of children leftover */
238         device_delete_all_children(self);
239
240         /*
241          * disable interrupts that might have been switched on in
242          * ehci_ebus_attach()
243          */
244         if (sc->sc_io_res) {
245                 EWRITE4(sc, USB_BRIDGE_INTR_MASK, 0);
246         }
247         if (sc->sc_irq_res && sc->sc_intr_hdl) {
248                 /*
249                  * only call ehci_detach() after ehci_init()
250                  */
251                 ehci_detach(sc);
252
253                 err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
254
255                 if (err)
256                         /* XXX or should we panic? */
257                         device_printf(self, "Could not tear down irq, %d\n",
258                             err);
259                 sc->sc_intr_hdl = NULL;
260         }
261         if (sc->sc_irq_res) {
262                 bus_release_resource(self, SYS_RES_IRQ, 1, sc->sc_irq_res);
263                 sc->sc_irq_res = NULL;
264         }
265         if (sc->sc_io_res) {
266                 bus_release_resource(self, SYS_RES_MEMORY, 0,
267                     sc->sc_io_res);
268                 sc->sc_io_res = NULL;
269         }
270         usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
271
272         return (0);
273 }
274
275 static device_method_t ehci_methods[] = {
276         /* Device interface */
277         DEVMETHOD(device_probe, ehci_ebus_probe),
278         DEVMETHOD(device_attach, ehci_ebus_attach),
279         DEVMETHOD(device_detach, ehci_ebus_detach),
280         DEVMETHOD(device_suspend, ehci_ebus_suspend),
281         DEVMETHOD(device_resume, ehci_ebus_resume),
282         DEVMETHOD(device_shutdown, ehci_ebus_shutdown),
283
284         /* Bus interface */
285         DEVMETHOD(bus_print_child, bus_generic_print_child),
286
287         {0, 0}
288 };
289
290 static driver_t ehci_driver = {
291         "ehci",
292         ehci_methods,
293         sizeof(ehci_softc_t),
294 };
295
296 static devclass_t ehci_devclass;
297
298 DRIVER_MODULE(ehci, econaarm, ehci_driver, ehci_devclass, 0, 0);
299 MODULE_DEPEND(ehci, usb, 1, 1, 1);