2 * Copyright (c) 2008 Sam Leffler. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * IXP435 attachment driver for the USB Enhanced Host Controller.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
34 #include <sys/stdint.h>
35 #include <sys/stddef.h>
36 #include <sys/param.h>
37 #include <sys/queue.h>
38 #include <sys/types.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
42 #include <sys/linker_set.h>
43 #include <sys/module.h>
45 #include <sys/mutex.h>
46 #include <sys/condvar.h>
47 #include <sys/sysctl.h>
49 #include <sys/unistd.h>
50 #include <sys/callout.h>
51 #include <sys/malloc.h>
54 #include <dev/usb/usb.h>
55 #include <dev/usb/usbdi.h>
57 #include <dev/usb/usb_core.h>
58 #include <dev/usb/usb_busdma.h>
59 #include <dev/usb/usb_process.h>
60 #include <dev/usb/usb_util.h>
62 #include <dev/usb/usb_controller.h>
63 #include <dev/usb/usb_bus.h>
64 #include <dev/usb/controller/ehci.h>
66 #include <arm/xscale/ixp425/ixp425reg.h>
67 #include <arm/xscale/ixp425/ixp425var.h>
69 #define EHCI_VENDORID_IXP4XX 0x42fa05
70 #define EHCI_HC_DEVSTR "IXP4XX Integrated USB 2.0 controller"
72 struct ixp_ehci_softc {
73 ehci_softc_t base; /* storage for EHCI code */
75 bus_space_handle_t ioh;
76 struct bus_space tag; /* tag for private bus space ops */
79 static device_attach_t ehci_ixp_attach;
80 static device_detach_t ehci_ixp_detach;
81 static device_shutdown_t ehci_ixp_shutdown;
82 static device_suspend_t ehci_ixp_suspend;
83 static device_resume_t ehci_ixp_resume;
85 static uint8_t ehci_bs_r_1(void *, bus_space_handle_t, bus_size_t);
86 static void ehci_bs_w_1(void *, bus_space_handle_t, bus_size_t, u_int8_t);
87 static uint16_t ehci_bs_r_2(void *, bus_space_handle_t, bus_size_t);
88 static void ehci_bs_w_2(void *, bus_space_handle_t, bus_size_t, uint16_t);
89 static uint32_t ehci_bs_r_4(void *, bus_space_handle_t, bus_size_t);
90 static void ehci_bs_w_4(void *, bus_space_handle_t, bus_size_t, uint32_t);
93 ehci_ixp_suspend(device_t self)
95 ehci_softc_t *sc = device_get_softc(self);
98 err = bus_generic_suspend(self);
106 ehci_ixp_resume(device_t self)
108 ehci_softc_t *sc = device_get_softc(self);
112 bus_generic_resume(self);
118 ehci_ixp_shutdown(device_t self)
120 ehci_softc_t *sc = device_get_softc(self);
123 err = bus_generic_shutdown(self);
132 ehci_ixp_probe(device_t self)
135 device_set_desc(self, EHCI_HC_DEVSTR);
137 return (BUS_PROBE_DEFAULT);
141 ehci_ixp_attach(device_t self)
143 struct ixp_ehci_softc *isc = device_get_softc(self);
144 ehci_softc_t *sc = &isc->base;
148 /* initialise some bus fields */
149 sc->sc_bus.parent = self;
150 sc->sc_bus.devices = sc->sc_devices;
151 sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
153 /* get all DMA memory */
154 if (usb_bus_mem_alloc_all(&sc->sc_bus,
155 USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
159 sc->sc_bus.usbrev = USB_REV_2_0;
161 /* NB: hints fix the memory location and irq */
164 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE);
165 if (!sc->sc_io_res) {
166 device_printf(self, "Could not map memory\n");
171 * Craft special resource for bus space ops that handle
172 * byte-alignment of non-word addresses. Also, since
173 * we're already intercepting bus space ops we handle
174 * the register window offset that could otherwise be
175 * done with bus_space_subregion.
177 isc->iot = rman_get_bustag(sc->sc_io_res);
178 isc->tag.bs_cookie = isc->iot;
180 isc->tag.bs_r_1 = ehci_bs_r_1,
181 isc->tag.bs_r_2 = ehci_bs_r_2,
182 isc->tag.bs_r_4 = ehci_bs_r_4,
184 isc->tag.bs_w_1 = ehci_bs_w_1,
185 isc->tag.bs_w_2 = ehci_bs_w_2,
186 isc->tag.bs_w_4 = ehci_bs_w_4,
188 sc->sc_io_tag = &isc->tag;
189 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
190 sc->sc_io_size = IXP435_USB1_SIZE - 0x100;
193 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
195 if (sc->sc_irq_res == NULL) {
196 device_printf(self, "Could not allocate irq\n");
199 sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
200 if (!sc->sc_bus.bdev) {
201 device_printf(self, "Could not add USB device\n");
204 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
205 device_set_desc(sc->sc_bus.bdev, EHCI_HC_DEVSTR);
207 sprintf(sc->sc_vendor, "Intel");
210 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
211 NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
213 device_printf(self, "Could not setup irq, %d\n", err);
214 sc->sc_intr_hdl = NULL;
219 * Arrange to force Host mode, select big-endian byte alignment,
220 * and arrange to not terminate reset operations (the adapter
221 * will ignore it if we do but might as well save a reg write).
222 * Also, the controller has an embedded Transaction Translator
223 * which means port speed must be read from the Port Status
224 * register following a port enable.
226 sc->sc_flags |= EHCI_SCFLG_TT
228 | EHCI_SCFLG_BIGEDESC
229 | EHCI_SCFLG_BIGEMMIO
230 | EHCI_SCFLG_NORESTERM
232 (void) ehci_reset(sc);
236 err = device_probe_and_attach(sc->sc_bus.bdev);
239 device_printf(self, "USB init failed err=%d\n", err);
245 ehci_ixp_detach(self);
250 ehci_ixp_detach(device_t self)
252 struct ixp_ehci_softc *isc = device_get_softc(self);
253 ehci_softc_t *sc = &isc->base;
257 if (sc->sc_bus.bdev) {
258 bdev = sc->sc_bus.bdev;
260 device_delete_child(self, bdev);
262 /* during module unload there are lots of children leftover */
263 device_delete_all_children(self);
266 * disable interrupts that might have been switched on in ehci_init
269 EWRITE4(sc, EHCI_USBINTR, 0);
272 if (sc->sc_irq_res && sc->sc_intr_hdl) {
274 * only call ehci_detach() after ehci_init()
278 err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
281 /* XXX or should we panic? */
282 device_printf(self, "Could not tear down irq, %d\n",
284 sc->sc_intr_hdl = NULL;
287 if (sc->sc_irq_res) {
288 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
289 sc->sc_irq_res = NULL;
292 bus_release_resource(self, SYS_RES_MEMORY, 0,
294 sc->sc_io_res = NULL;
296 usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
302 * Bus space accessors for PIO operations.
306 ehci_bs_r_1(void *t, bus_space_handle_t h, bus_size_t o)
308 return bus_space_read_1((bus_space_tag_t) t, h,
309 0x100 + (o &~ 3) + (3 - (o & 3)));
313 ehci_bs_w_1(void *t, bus_space_handle_t h, bus_size_t o, u_int8_t v)
315 panic("%s", __func__);
319 ehci_bs_r_2(void *t, bus_space_handle_t h, bus_size_t o)
321 return bus_space_read_2((bus_space_tag_t) t, h,
322 0x100 + (o &~ 3) + (2 - (o & 3)));
326 ehci_bs_w_2(void *t, bus_space_handle_t h, bus_size_t o, uint16_t v)
328 panic("%s", __func__);
332 ehci_bs_r_4(void *t, bus_space_handle_t h, bus_size_t o)
334 return bus_space_read_4((bus_space_tag_t) t, h, 0x100 + o);
338 ehci_bs_w_4(void *t, bus_space_handle_t h, bus_size_t o, uint32_t v)
340 bus_space_write_4((bus_space_tag_t) t, h, 0x100 + o, v);
343 static device_method_t ehci_methods[] = {
344 /* Device interface */
345 DEVMETHOD(device_probe, ehci_ixp_probe),
346 DEVMETHOD(device_attach, ehci_ixp_attach),
347 DEVMETHOD(device_detach, ehci_ixp_detach),
348 DEVMETHOD(device_suspend, ehci_ixp_suspend),
349 DEVMETHOD(device_resume, ehci_ixp_resume),
350 DEVMETHOD(device_shutdown, ehci_ixp_shutdown),
353 DEVMETHOD(bus_print_child, bus_generic_print_child),
358 static driver_t ehci_driver = {
361 sizeof(struct ixp_ehci_softc),
364 static devclass_t ehci_devclass;
366 DRIVER_MODULE(ehci, ixp, ehci_driver, ehci_devclass, 0, 0);
367 MODULE_DEPEND(ehci, usb, 1, 1, 1);