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 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the NetBSD
20 * Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD: releng/8.4/sys/mips/rmi/xls_ehci.c 230714 2012-01-29 01:22:48Z marius $");
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/types.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/bus.h>
51 #include <sys/module.h>
52 #include <sys/lock.h>
53 #include <sys/mutex.h>
54 #include <sys/condvar.h>
55 #include <sys/sysctl.h>
56 #include <sys/sx.h>
57 #include <sys/unistd.h>
58 #include <sys/callout.h>
59 #include <sys/malloc.h>
60 #include <sys/priv.h>
61
62 #include <sys/rman.h>
63 #include <machine/resource.h>
64
65 #include <dev/usb/usb.h>
66 #include <dev/usb/usbdi.h>
67
68 #include <dev/usb/usb_core.h>
69 #include <dev/usb/usb_busdma.h>
70 #include <dev/usb/usb_process.h>
71 #include <dev/usb/usb_util.h>
72
73 #include <dev/usb/usb_controller.h>
74 #include <dev/usb/usb_bus.h>
75 #include <dev/usb/controller/ehci.h>
76 #include <dev/usb/controller/ehcireg.h>
77 #include <mips/rmi/pic.h>
78
79 static device_attach_t ehci_xls_attach;
80 static device_detach_t ehci_xls_detach;
81
82 static const char *xlr_usb_dev_desc = "RMI XLR USB 2.0 controller";
83 static const char *xlr_vendor_desc = "RMI Corp";
84
85 static int
86 ehci_xls_probe(device_t self)
87 {
88 /* TODO see if usb is enabled on the board */
89 device_set_desc(self, xlr_usb_dev_desc);
90 return BUS_PROBE_DEFAULT;
91 }
92
93 static int
94 ehci_xls_attach(device_t self)
95 {
96 ehci_softc_t *sc = device_get_softc(self);
97 int err;
98 int rid;
99
100 sc->sc_bus.parent = self;
101 sc->sc_bus.devices = sc->sc_devices;
102 sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
103
104 /* get all DMA memory */
105 if (usb_bus_mem_alloc_all(&sc->sc_bus,
106 USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
107 return (ENOMEM);
108 }
109
110 rid = 0;
111 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
112 RF_ACTIVE);
113 if (!sc->sc_io_res) {
114 device_printf(self, "Could not map memory\n");
115 goto error;
116 }
117 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
118 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
119 printf("IO Resource tag %lx, hdl %lx, size %lx\n",
120 (u_long)sc->sc_io_tag, (u_long)sc->sc_io_hdl,
121 (u_long)sc->sc_io_size);
122
123 rid = 0;
124 sc->sc_irq_res = bus_alloc_resource(self, SYS_RES_IRQ, &rid,
125 PIC_USB_IRQ, PIC_USB_IRQ, 1, RF_SHAREABLE | RF_ACTIVE);
126 if (sc->sc_irq_res == NULL) {
127 device_printf(self, "Could not allocate irq\n");
128 goto error;
129 }
130
131 sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
132 if (!sc->sc_bus.bdev) {
133 device_printf(self, "Could not add USB device\n");
134 goto error;
135 }
136 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
137 device_set_desc(sc->sc_bus.bdev, xlr_usb_dev_desc);
138
139 sprintf(sc->sc_vendor, xlr_vendor_desc);
140
141 err = bus_setup_intr(self, sc->sc_irq_res,
142 INTR_TYPE_BIO | INTR_MPSAFE, NULL,
143 (driver_intr_t *) ehci_interrupt, sc, &sc->sc_intr_hdl);
144 if (err) {
145 device_printf(self, "Could not setup irq, %d\n", err);
146 sc->sc_intr_hdl = NULL;
147 goto error;
148 }
149
150 err = ehci_init(sc);
151 if (err) {
152 device_printf(self, "USB init failed err=%d\n", err);
153 goto error;
154 }
155
156 err = device_probe_and_attach(sc->sc_bus.bdev);
157 if (err) {
158 device_printf(self, "USB probe and attach failed err=%d\n", err);
159 goto error;
160 }
161
162 return (0);
163
164 error:
165 ehci_xls_detach(self);
166 return (ENXIO);
167 }
168
169 static int
170 ehci_xls_detach(device_t self)
171 {
172 ehci_softc_t *sc = device_get_softc(self);
173 device_t bdev;
174 int err;
175
176 if (sc->sc_bus.bdev) {
177 bdev = sc->sc_bus.bdev;
178 device_detach(bdev);
179 device_delete_child(self, bdev);
180 }
181 /* during module unload there are lots of children leftover */
182 device_delete_all_children(self);
183
184 if (sc->sc_irq_res && sc->sc_intr_hdl) {
185 ehci_detach(sc);
186
187 err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
188 if (err)
189 device_printf(self, "Could not tear down irq, %d\n",
190 err);
191 sc->sc_intr_hdl = 0;
192 }
193
194 if (sc->sc_irq_res) {
195 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
196 sc->sc_irq_res = NULL;
197 }
198 if (sc->sc_io_res) {
199 bus_release_resource(self, SYS_RES_MEMORY, 0,
200 sc->sc_io_res);
201 sc->sc_io_res = NULL;
202 sc->sc_io_tag = 0;
203 sc->sc_io_hdl = 0;
204 }
205
206 usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
207
208 return (0);
209 }
210
211 static device_method_t ehci_methods[] = {
212 /* Device interface */
213 DEVMETHOD(device_probe, ehci_xls_probe),
214 DEVMETHOD(device_attach, ehci_xls_attach),
215 DEVMETHOD(device_detach, ehci_xls_detach),
216 DEVMETHOD(device_suspend, bus_generic_suspend),
217 DEVMETHOD(device_resume, bus_generic_resume),
218 DEVMETHOD(device_shutdown, bus_generic_shutdown),
219
220 DEVMETHOD_END
221 };
222
223 static driver_t ehci_driver = {
224 .name = "ehci",
225 .methods = ehci_methods,
226 .size = sizeof(struct ehci_softc),
227 };
228
229 static devclass_t ehci_devclass;
230
231 DRIVER_MODULE(ehci, iodi, ehci_driver, ehci_devclass, 0, 0);
232 MODULE_DEPEND(ehci, usb, 1, 1, 1);
Cache object: c4e4c83f2d64b197d4e53deef1e32587
|