]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/rmi/xls_ehci.c
Improve the Xen para-virtualized device infrastructure of FreeBSD:
[FreeBSD/FreeBSD.git] / sys / mips / rmi / xls_ehci.c
1 /*-
2  * Copyright (c) 1998 The NetBSD Foundation, Inc.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The NetBSD Foundation
6  * by Lennart Augustsson (augustss@carlstedt.se) at
7  * Carlstedt Research & Technology.
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include "opt_bus.h"
35
36 #include <sys/stdint.h>
37 #include <sys/stddef.h>
38 #include <sys/param.h>
39 #include <sys/queue.h>
40 #include <sys/types.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/bus.h>
44 #include <sys/linker_set.h>
45 #include <sys/module.h>
46 #include <sys/lock.h>
47 #include <sys/mutex.h>
48 #include <sys/condvar.h>
49 #include <sys/sysctl.h>
50 #include <sys/sx.h>
51 #include <sys/unistd.h>
52 #include <sys/callout.h>
53 #include <sys/malloc.h>
54 #include <sys/priv.h>
55
56 #include <sys/rman.h>
57 #include <machine/resource.h>
58
59 #include <dev/usb/usb.h>
60 #include <dev/usb/usbdi.h>
61
62 #include <dev/usb/usb_core.h>
63 #include <dev/usb/usb_busdma.h>
64 #include <dev/usb/usb_process.h>
65 #include <dev/usb/usb_util.h>
66
67 #include <dev/usb/usb_controller.h>
68 #include <dev/usb/usb_bus.h>
69 #include <dev/usb/controller/ehci.h>
70 #include <dev/usb/controller/ehcireg.h>
71 #include <mips/rmi/pic.h>
72
73 static int ehci_xls_attach(device_t self);
74 static int ehci_xls_detach(device_t self);
75 static int ehci_xls_shutdown(device_t self);
76 static int ehci_xls_suspend(device_t self);
77 static int ehci_xls_resume(device_t self);
78
79 static int
80 ehci_xls_suspend(device_t self)
81 {
82         ehci_softc_t *sc = device_get_softc(self);
83         int err;
84
85         err = bus_generic_suspend(self);
86         if (err)
87                 return (err);
88         ehci_suspend(sc);
89         return (0);
90 }
91
92 static int
93 ehci_xls_resume(device_t self)
94 {
95         ehci_softc_t *sc = device_get_softc(self);
96
97         ehci_resume(sc);
98
99         bus_generic_resume(self);
100
101         return (0);
102 }
103
104 static int
105 ehci_xls_shutdown(device_t self)
106 {
107         ehci_softc_t *sc = device_get_softc(self);
108         int err;
109
110         err = bus_generic_shutdown(self);
111         if (err)
112                 return (err);
113         ehci_shutdown(sc);
114
115         return (0);
116 }
117
118 static const char *xlr_usb_dev_desc = "RMI XLR USB 2.0 controller";
119 static const char *xlr_vendor_desc = "RMI Corp";
120
121 static int
122 ehci_xls_probe(device_t self)
123 {
124         /* TODO see if usb is enabled on the board */
125         device_set_desc(self, xlr_usb_dev_desc);
126         return BUS_PROBE_DEFAULT;
127 }
128
129 static int
130 ehci_xls_attach(device_t self)
131 {
132         ehci_softc_t *sc = device_get_softc(self);
133         int err;
134         int rid;
135
136         sc->sc_bus.parent = self;
137         sc->sc_bus.devices = sc->sc_devices;
138         sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
139
140         /* get all DMA memory */
141         if (usb_bus_mem_alloc_all(&sc->sc_bus,
142             USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
143                 return (ENOMEM);
144         }
145
146         rid = 0;
147         sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
148             RF_ACTIVE);
149         if (!sc->sc_io_res) {
150                 device_printf(self, "Could not map memory\n");
151                 goto error;
152         }
153         sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
154         sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
155         printf("IO Resource tag %lx, hdl %lx, size %lx\n",
156                (u_long)sc->sc_io_tag, (u_long)sc->sc_io_hdl, 
157                (u_long)sc->sc_io_size);
158
159         rid = 0;
160         sc->sc_irq_res = bus_alloc_resource(self, SYS_RES_IRQ, &rid,
161             PIC_USB_IRQ, PIC_USB_IRQ, 1, RF_SHAREABLE | RF_ACTIVE);
162         if (sc->sc_irq_res == NULL) {
163                 device_printf(self, "Could not allocate irq\n");
164                 goto error;
165         }
166
167         sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
168         if (!sc->sc_bus.bdev) {
169                 device_printf(self, "Could not add USB device\n");
170                 goto error;
171         }
172         device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
173         device_set_desc(sc->sc_bus.bdev, xlr_usb_dev_desc);
174
175         sprintf(sc->sc_vendor, xlr_vendor_desc);
176
177         err = bus_setup_intr(self, sc->sc_irq_res,
178             INTR_TYPE_BIO | INTR_MPSAFE, NULL,
179             (driver_intr_t *) ehci_interrupt, sc, &sc->sc_intr_hdl);
180         if (err) {
181                 device_printf(self, "Could not setup irq, %d\n", err);
182                 sc->sc_intr_hdl = NULL;
183                 goto error;
184         }
185
186         err = ehci_init(sc);
187         if (err) {
188                 device_printf(self, "USB init failed err=%d\n", err);
189                 goto error;
190         }
191
192         err = device_probe_and_attach(sc->sc_bus.bdev);
193         if (err) {
194                 device_printf(self, "USB probe and attach failed err=%d\n", err);
195                 goto error;
196         }
197
198         return (0);
199
200 error:
201         ehci_xls_detach(self);
202         return (ENXIO);
203 }
204
205 static int
206 ehci_xls_detach(device_t self)
207 {
208         ehci_softc_t *sc = device_get_softc(self);
209         device_t bdev;
210         int err;
211
212         if (sc->sc_bus.bdev) {
213                 bdev = sc->sc_bus.bdev;
214                 device_detach(bdev);
215                 device_delete_child(self, bdev);
216         }
217         /* during module unload there are lots of children leftover */
218         device_delete_all_children(self);
219
220         /*
221          * disable interrupts that might have been switched on in ehci_init
222          */
223         if (sc->sc_io_res) {
224                 EWRITE4(sc, EHCI_USBINTR, 0);
225         }
226
227         if (sc->sc_irq_res && sc->sc_intr_hdl) {
228                 ehci_detach(sc);
229
230                 err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
231                 if (err)
232                         device_printf(self, "Could not tear down irq, %d\n",
233                             err);
234                 sc->sc_intr_hdl = 0;
235         }
236
237         if (sc->sc_irq_res) {
238                 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
239                 sc->sc_irq_res = NULL;
240         }
241         if (sc->sc_io_res) {
242                 bus_release_resource(self, SYS_RES_MEMORY, 0,
243                     sc->sc_io_res);
244                 sc->sc_io_res = NULL;
245                 sc->sc_io_tag = 0;
246                 sc->sc_io_hdl = 0;
247         }
248
249         usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
250
251         return (0);
252 }
253
254 static device_method_t ehci_methods[] = {
255         /* Device interface */
256         DEVMETHOD(device_probe, ehci_xls_probe),
257         DEVMETHOD(device_attach, ehci_xls_attach),
258         DEVMETHOD(device_detach, ehci_xls_detach),
259         DEVMETHOD(device_suspend, ehci_xls_suspend),
260         DEVMETHOD(device_resume, ehci_xls_resume),
261         DEVMETHOD(device_shutdown, ehci_xls_shutdown),
262
263         /* Bus interface */
264         DEVMETHOD(bus_print_child, bus_generic_print_child),
265
266         {0, 0}
267 };
268
269 static driver_t ehci_driver = {
270         "ehci",
271         ehci_methods,
272         sizeof(struct ehci_softc),
273 };
274
275 static devclass_t ehci_devclass;
276
277 DRIVER_MODULE(ehci, iodi, ehci_driver, ehci_devclass, 0, 0);
278 MODULE_DEPEND(ehci, usb, 1, 1, 1);