]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/dev/usb/controller/ehci_ixp4xx.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / dev / usb / controller / ehci_ixp4xx.c
1 /*-
2  * Copyright (c) 2008 Sam Leffler.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
23  */
24
25 /*
26  * IXP435 attachment driver for the USB Enhanced Host Controller.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include "opt_bus.h"
33
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>
41 #include <sys/bus.h>
42 #include <sys/linker_set.h>
43 #include <sys/module.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/condvar.h>
47 #include <sys/sysctl.h>
48 #include <sys/sx.h>
49 #include <sys/unistd.h>
50 #include <sys/callout.h>
51 #include <sys/malloc.h>
52 #include <sys/priv.h>
53
54 #include <dev/usb/usb.h>
55 #include <dev/usb/usbdi.h>
56
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>
61
62 #include <dev/usb/usb_controller.h>
63 #include <dev/usb/usb_bus.h>
64 #include <dev/usb/controller/ehci.h>
65 #include <dev/usb/controller/ehcireg.h>
66
67 #include <arm/xscale/ixp425/ixp425reg.h>
68 #include <arm/xscale/ixp425/ixp425var.h>
69
70 #define EHCI_VENDORID_IXP4XX    0x42fa05
71 #define EHCI_HC_DEVSTR          "IXP4XX Integrated USB 2.0 controller"
72
73 struct ixp_ehci_softc {
74         ehci_softc_t            base;   /* storage for EHCI code */
75         bus_space_tag_t         iot;
76         bus_space_handle_t      ioh;
77         struct bus_space        tag;    /* tag for private bus space ops */
78 };
79
80 static device_attach_t ehci_ixp_attach;
81 static device_detach_t ehci_ixp_detach;
82 static device_shutdown_t ehci_ixp_shutdown;
83 static device_suspend_t ehci_ixp_suspend;
84 static device_resume_t ehci_ixp_resume;
85
86 static uint8_t ehci_bs_r_1(void *, bus_space_handle_t, bus_size_t);
87 static void ehci_bs_w_1(void *, bus_space_handle_t, bus_size_t, u_int8_t);
88 static uint16_t ehci_bs_r_2(void *, bus_space_handle_t, bus_size_t);
89 static void ehci_bs_w_2(void *, bus_space_handle_t, bus_size_t, uint16_t);
90 static uint32_t ehci_bs_r_4(void *, bus_space_handle_t, bus_size_t);
91 static void ehci_bs_w_4(void *, bus_space_handle_t, bus_size_t, uint32_t);
92
93 static int
94 ehci_ixp_suspend(device_t self)
95 {
96         ehci_softc_t *sc = device_get_softc(self);
97         int err;
98
99         err = bus_generic_suspend(self);
100         if (err)
101                 return (err);
102         ehci_suspend(sc);
103         return (0);
104 }
105
106 static int
107 ehci_ixp_resume(device_t self)
108 {
109         ehci_softc_t *sc = device_get_softc(self);
110
111         ehci_resume(sc);
112
113         bus_generic_resume(self);
114
115         return (0);
116 }
117
118 static int
119 ehci_ixp_shutdown(device_t self)
120 {
121         ehci_softc_t *sc = device_get_softc(self);
122         int err;
123
124         err = bus_generic_shutdown(self);
125         if (err)
126                 return (err);
127         ehci_shutdown(sc);
128
129         return (0);
130 }
131
132 static int
133 ehci_ixp_probe(device_t self)
134 {
135
136         device_set_desc(self, EHCI_HC_DEVSTR);
137
138         return (BUS_PROBE_DEFAULT);
139 }
140
141 static int
142 ehci_ixp_attach(device_t self)
143 {
144         struct ixp_ehci_softc *isc = device_get_softc(self);
145         ehci_softc_t *sc = &isc->base;
146         int err;
147         int rid;
148
149         /* initialise some bus fields */
150         sc->sc_bus.parent = self;
151         sc->sc_bus.devices = sc->sc_devices;
152         sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
153
154         /* get all DMA memory */
155         if (usb_bus_mem_alloc_all(&sc->sc_bus,
156             USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
157                 return (ENOMEM);
158         }
159
160         /* NB: hints fix the memory location and irq */
161
162         rid = 0;
163         sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE);
164         if (!sc->sc_io_res) {
165                 device_printf(self, "Could not map memory\n");
166                 goto error;
167         }
168
169         /*
170          * Craft special resource for bus space ops that handle
171          * byte-alignment of non-word addresses.  Also, since
172          * we're already intercepting bus space ops we handle
173          * the register window offset that could otherwise be
174          * done with bus_space_subregion.
175          */
176         isc->iot = rman_get_bustag(sc->sc_io_res);
177         isc->tag.bs_cookie = isc->iot;
178         /* read single */
179         isc->tag.bs_r_1 = ehci_bs_r_1,
180         isc->tag.bs_r_2 = ehci_bs_r_2,
181         isc->tag.bs_r_4 = ehci_bs_r_4,
182         /* write (single) */
183         isc->tag.bs_w_1 = ehci_bs_w_1,
184         isc->tag.bs_w_2 = ehci_bs_w_2,
185         isc->tag.bs_w_4 = ehci_bs_w_4,
186
187         sc->sc_io_tag = &isc->tag;
188         sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
189         sc->sc_io_size = IXP435_USB1_SIZE - 0x100;
190
191         rid = 0;
192         sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
193             RF_ACTIVE);
194         if (sc->sc_irq_res == NULL) {
195                 device_printf(self, "Could not allocate irq\n");
196                 goto error;
197         }
198         sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
199         if (!sc->sc_bus.bdev) {
200                 device_printf(self, "Could not add USB device\n");
201                 goto error;
202         }
203         device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
204         device_set_desc(sc->sc_bus.bdev, EHCI_HC_DEVSTR);
205
206         sprintf(sc->sc_vendor, "Intel");
207
208
209         err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
210             NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
211         if (err) {
212                 device_printf(self, "Could not setup irq, %d\n", err);
213                 sc->sc_intr_hdl = NULL;
214                 goto error;
215         }
216
217         /*
218          * Arrange to force Host mode, select big-endian byte alignment,
219          * and arrange to not terminate reset operations (the adapter
220          * will ignore it if we do but might as well save a reg write).
221          * Also, the controller has an embedded Transaction Translator
222          * which means port speed must be read from the Port Status
223          * register following a port enable.
224          */
225         sc->sc_flags |= EHCI_SCFLG_TT
226                      | EHCI_SCFLG_SETMODE
227                      | EHCI_SCFLG_BIGEDESC
228                      | EHCI_SCFLG_BIGEMMIO
229                      | EHCI_SCFLG_NORESTERM
230                      ;
231
232         err = ehci_init(sc);
233         if (!err) {
234                 err = device_probe_and_attach(sc->sc_bus.bdev);
235         }
236         if (err) {
237                 device_printf(self, "USB init failed err=%d\n", err);
238                 goto error;
239         }
240         return (0);
241
242 error:
243         ehci_ixp_detach(self);
244         return (ENXIO);
245 }
246
247 static int
248 ehci_ixp_detach(device_t self)
249 {
250         struct ixp_ehci_softc *isc = device_get_softc(self);
251         ehci_softc_t *sc = &isc->base;
252         device_t bdev;
253         int err;
254
255         if (sc->sc_bus.bdev) {
256                 bdev = sc->sc_bus.bdev;
257                 device_detach(bdev);
258                 device_delete_child(self, bdev);
259         }
260         /* during module unload there are lots of children leftover */
261         device_delete_all_children(self);
262
263         /*
264          * disable interrupts that might have been switched on in ehci_init
265          */
266         if (sc->sc_io_res) {
267                 EWRITE4(sc, EHCI_USBINTR, 0);
268         }
269
270         if (sc->sc_irq_res && sc->sc_intr_hdl) {
271                 /*
272                  * only call ehci_detach() after ehci_init()
273                  */
274                 ehci_detach(sc);
275
276                 err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
277
278                 if (err)
279                         /* XXX or should we panic? */
280                         device_printf(self, "Could not tear down irq, %d\n",
281                             err);
282                 sc->sc_intr_hdl = NULL;
283         }
284
285         if (sc->sc_irq_res) {
286                 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
287                 sc->sc_irq_res = NULL;
288         }
289         if (sc->sc_io_res) {
290                 bus_release_resource(self, SYS_RES_MEMORY, 0,
291                     sc->sc_io_res);
292                 sc->sc_io_res = NULL;
293         }
294         usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
295
296         return (0);
297 }
298
299 /*
300  * Bus space accessors for PIO operations.
301  */
302
303 static uint8_t
304 ehci_bs_r_1(void *t, bus_space_handle_t h, bus_size_t o)
305 {
306         return bus_space_read_1((bus_space_tag_t) t, h,
307             0x100 + (o &~ 3) + (3 - (o & 3)));
308 }
309
310 static void
311 ehci_bs_w_1(void *t, bus_space_handle_t h, bus_size_t o, u_int8_t v)
312 {
313         panic("%s", __func__);
314 }
315
316 static uint16_t
317 ehci_bs_r_2(void *t, bus_space_handle_t h, bus_size_t o)
318 {
319         return bus_space_read_2((bus_space_tag_t) t, h,
320             0x100 + (o &~ 3) + (2 - (o & 3)));
321 }
322
323 static void
324 ehci_bs_w_2(void *t, bus_space_handle_t h, bus_size_t o, uint16_t v)
325 {
326         panic("%s", __func__);
327 }
328
329 static uint32_t
330 ehci_bs_r_4(void *t, bus_space_handle_t h, bus_size_t o)
331 {
332         return bus_space_read_4((bus_space_tag_t) t, h, 0x100 + o);
333 }
334
335 static void
336 ehci_bs_w_4(void *t, bus_space_handle_t h, bus_size_t o, uint32_t v)
337 {
338         bus_space_write_4((bus_space_tag_t) t, h, 0x100 + o, v);
339 }
340
341 static device_method_t ehci_methods[] = {
342         /* Device interface */
343         DEVMETHOD(device_probe, ehci_ixp_probe),
344         DEVMETHOD(device_attach, ehci_ixp_attach),
345         DEVMETHOD(device_detach, ehci_ixp_detach),
346         DEVMETHOD(device_suspend, ehci_ixp_suspend),
347         DEVMETHOD(device_resume, ehci_ixp_resume),
348         DEVMETHOD(device_shutdown, ehci_ixp_shutdown),
349
350         /* Bus interface */
351         DEVMETHOD(bus_print_child, bus_generic_print_child),
352
353         {0, 0}
354 };
355
356 static driver_t ehci_driver = {
357         "ehci",
358         ehci_methods,
359         sizeof(struct ixp_ehci_softc),
360 };
361
362 static devclass_t ehci_devclass;
363
364 DRIVER_MODULE(ehci, ixp, ehci_driver, ehci_devclass, 0, 0);
365 MODULE_DEPEND(ehci, usb, 1, 1, 1);