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: releng/10.0/sys/mips/rmi/xls_ehci.c 228483 2011-12-14 00:28:54Z hselasky $");
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/module.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/condvar.h>
48 #include <sys/sysctl.h>
49 #include <sys/sx.h>
50 #include <sys/unistd.h>
51 #include <sys/callout.h>
52 #include <sys/malloc.h>
53 #include <sys/priv.h>
54
55 #include <sys/rman.h>
56 #include <machine/resource.h>
57
58 #include <dev/usb/usb.h>
59 #include <dev/usb/usbdi.h>
60
61 #include <dev/usb/usb_core.h>
62 #include <dev/usb/usb_busdma.h>
63 #include <dev/usb/usb_process.h>
64 #include <dev/usb/usb_util.h>
65
66 #include <dev/usb/usb_controller.h>
67 #include <dev/usb/usb_bus.h>
68 #include <dev/usb/controller/ehci.h>
69 #include <dev/usb/controller/ehcireg.h>
70 #include <mips/rmi/pic.h>
71
72 static device_attach_t ehci_xls_attach;
73 static device_detach_t ehci_xls_detach;
74
75 static const char *xlr_usb_dev_desc = "RMI XLR USB 2.0 controller";
76 static const char *xlr_vendor_desc = "RMI Corp";
77
78 static int
79 ehci_xls_probe(device_t self)
80 {
81 /* TODO see if usb is enabled on the board */
82 device_set_desc(self, xlr_usb_dev_desc);
83 return BUS_PROBE_DEFAULT;
84 }
85
86 static int
87 ehci_xls_attach(device_t self)
88 {
89 ehci_softc_t *sc = device_get_softc(self);
90 int err;
91 int rid;
92
93 sc->sc_bus.parent = self;
94 sc->sc_bus.devices = sc->sc_devices;
95 sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
96
97 /* get all DMA memory */
98 if (usb_bus_mem_alloc_all(&sc->sc_bus,
99 USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
100 return (ENOMEM);
101 }
102
103 rid = 0;
104 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
105 RF_ACTIVE);
106 if (!sc->sc_io_res) {
107 device_printf(self, "Could not map memory\n");
108 goto error;
109 }
110 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
111 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
112 printf("IO Resource tag %lx, hdl %lx, size %lx\n",
113 (u_long)sc->sc_io_tag, (u_long)sc->sc_io_hdl,
114 (u_long)sc->sc_io_size);
115
116 rid = 0;
117 sc->sc_irq_res = bus_alloc_resource(self, SYS_RES_IRQ, &rid,
118 PIC_USB_IRQ, PIC_USB_IRQ, 1, RF_SHAREABLE | RF_ACTIVE);
119 if (sc->sc_irq_res == NULL) {
120 device_printf(self, "Could not allocate irq\n");
121 goto error;
122 }
123
124 sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
125 if (!sc->sc_bus.bdev) {
126 device_printf(self, "Could not add USB device\n");
127 goto error;
128 }
129 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
130 device_set_desc(sc->sc_bus.bdev, xlr_usb_dev_desc);
131
132 sprintf(sc->sc_vendor, xlr_vendor_desc);
133
134 err = bus_setup_intr(self, sc->sc_irq_res,
135 INTR_TYPE_BIO | INTR_MPSAFE, NULL,
136 (driver_intr_t *) ehci_interrupt, sc, &sc->sc_intr_hdl);
137 if (err) {
138 device_printf(self, "Could not setup irq, %d\n", err);
139 sc->sc_intr_hdl = NULL;
140 goto error;
141 }
142
143 err = ehci_init(sc);
144 if (err) {
145 device_printf(self, "USB init failed err=%d\n", err);
146 goto error;
147 }
148
149 err = device_probe_and_attach(sc->sc_bus.bdev);
150 if (err) {
151 device_printf(self, "USB probe and attach failed err=%d\n", err);
152 goto error;
153 }
154
155 return (0);
156
157 error:
158 ehci_xls_detach(self);
159 return (ENXIO);
160 }
161
162 static int
163 ehci_xls_detach(device_t self)
164 {
165 ehci_softc_t *sc = device_get_softc(self);
166 device_t bdev;
167 int err;
168
169 if (sc->sc_bus.bdev) {
170 bdev = sc->sc_bus.bdev;
171 device_detach(bdev);
172 device_delete_child(self, bdev);
173 }
174 /* during module unload there are lots of children leftover */
175 device_delete_children(self);
176
177 if (sc->sc_irq_res && sc->sc_intr_hdl) {
178 ehci_detach(sc);
179
180 err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
181 if (err)
182 device_printf(self, "Could not tear down irq, %d\n",
183 err);
184 sc->sc_intr_hdl = 0;
185 }
186
187 if (sc->sc_irq_res) {
188 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
189 sc->sc_irq_res = NULL;
190 }
191 if (sc->sc_io_res) {
192 bus_release_resource(self, SYS_RES_MEMORY, 0,
193 sc->sc_io_res);
194 sc->sc_io_res = NULL;
195 sc->sc_io_tag = 0;
196 sc->sc_io_hdl = 0;
197 }
198
199 usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
200
201 return (0);
202 }
203
204 static device_method_t ehci_methods[] = {
205 /* Device interface */
206 DEVMETHOD(device_probe, ehci_xls_probe),
207 DEVMETHOD(device_attach, ehci_xls_attach),
208 DEVMETHOD(device_detach, ehci_xls_detach),
209 DEVMETHOD(device_suspend, bus_generic_suspend),
210 DEVMETHOD(device_resume, bus_generic_resume),
211 DEVMETHOD(device_shutdown, bus_generic_shutdown),
212
213 DEVMETHOD_END
214 };
215
216 static driver_t ehci_driver = {
217 .name = "ehci",
218 .methods = ehci_methods,
219 .size = sizeof(struct ehci_softc),
220 };
221
222 static devclass_t ehci_devclass;
223
224 DRIVER_MODULE(ehci, iodi, ehci_driver, ehci_devclass, 0, 0);
225 MODULE_DEPEND(ehci, usb, 1, 1, 1);
Cache object: 78e7ff90c064187f606ea834959ad1fc
|