]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/mips/atheros/ar71xx_ohci.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / mips / atheros / ar71xx_ohci.c
1 /*-
2  * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/rman.h>
35 #include <sys/condvar.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38
39 #include <dev/usb/usb.h>
40 #include <dev/usb/usbdi.h>
41
42 #include <dev/usb/usb_core.h>
43 #include <dev/usb/usb_busdma.h>
44 #include <dev/usb/usb_process.h>
45 #include <dev/usb/usb_util.h>
46
47 #include <dev/usb/usb_controller.h>
48 #include <dev/usb/usb_bus.h>
49 #include <dev/usb/controller/ohci.h>
50 #include <dev/usb/controller/ohcireg.h>
51
52 static int ar71xx_ohci_attach(device_t dev);
53 static int ar71xx_ohci_detach(device_t dev);
54 static int ar71xx_ohci_probe(device_t dev);
55
56 struct ar71xx_ohci_softc
57 {
58         struct ohci_softc sc_ohci;
59 };
60
61 static int
62 ar71xx_ohci_probe(device_t dev)
63 {
64         device_set_desc(dev, "AR71XX integrated OHCI controller");
65         return (BUS_PROBE_DEFAULT);
66 }
67
68 static int
69 ar71xx_ohci_attach(device_t dev)
70 {
71         struct ar71xx_ohci_softc *sc = device_get_softc(dev);
72         int err;
73         int rid;
74
75         /* initialise some bus fields */
76         sc->sc_ohci.sc_bus.parent = dev;
77         sc->sc_ohci.sc_bus.devices = sc->sc_ohci.sc_devices;
78         sc->sc_ohci.sc_bus.devices_max = OHCI_MAX_DEVICES;
79
80         /* get all DMA memory */
81         if (usb_bus_mem_alloc_all(&sc->sc_ohci.sc_bus,
82             USB_GET_DMA_TAG(dev), &ohci_iterate_hw_softc)) {
83                 return (ENOMEM);
84         }
85
86         sc->sc_ohci.sc_dev = dev;
87
88         rid = 0;
89         sc->sc_ohci.sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
90             RF_ACTIVE);
91         if (sc->sc_ohci.sc_io_res == NULL) {
92                 err = ENOMEM;
93                 goto error;
94         }
95         sc->sc_ohci.sc_io_tag = rman_get_bustag(sc->sc_ohci.sc_io_res);
96         sc->sc_ohci.sc_io_hdl = rman_get_bushandle(sc->sc_ohci.sc_io_res);
97         sc->sc_ohci.sc_io_size = rman_get_size(sc->sc_ohci.sc_io_res);
98
99         rid = 0;
100         sc->sc_ohci.sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
101             RF_ACTIVE);
102         if (sc->sc_ohci.sc_irq_res == NULL) {
103                 err = ENOMEM;
104                 goto error;
105         }
106         sc->sc_ohci.sc_bus.bdev = device_add_child(dev, "usbus", -1);
107         if (sc->sc_ohci.sc_bus.bdev == NULL) {
108                 err = ENOMEM;
109                 goto error;
110         }
111         device_set_ivars(sc->sc_ohci.sc_bus.bdev, &sc->sc_ohci.sc_bus);
112
113         err = bus_setup_intr(dev, sc->sc_ohci.sc_irq_res, 
114             INTR_TYPE_BIO | INTR_MPSAFE, NULL, 
115             (driver_intr_t *)ohci_interrupt, sc, &sc->sc_ohci.sc_intr_hdl);
116         if (err) {
117                 err = ENXIO;
118                 goto error;
119         }
120
121         strlcpy(sc->sc_ohci.sc_vendor, "Atheros", sizeof(sc->sc_ohci.sc_vendor));
122
123         bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl, OHCI_CONTROL, 0);
124
125         err = ohci_init(&sc->sc_ohci);
126         if (!err)
127                 err = device_probe_and_attach(sc->sc_ohci.sc_bus.bdev);
128
129         if (err)
130                 goto error;
131         return (0);
132
133 error:
134         if (err) {
135                 ar71xx_ohci_detach(dev);
136                 return (err);
137         }
138         return (err);
139 }
140
141 static int
142 ar71xx_ohci_detach(device_t dev)
143 {
144         struct ar71xx_ohci_softc *sc = device_get_softc(dev);
145         device_t bdev;
146
147         if (sc->sc_ohci.sc_bus.bdev) {
148                 bdev = sc->sc_ohci.sc_bus.bdev;
149                 device_detach(bdev);
150                 device_delete_child(dev, bdev);
151         }
152         /* during module unload there are lots of children leftover */
153         device_delete_children(dev);
154
155         /*
156          * Put the controller into reset, then disable clocks and do
157          * the MI tear down.  We have to disable the clocks/hardware
158          * after we do the rest of the teardown.  We also disable the
159          * clocks in the opposite order we acquire them, but that
160          * doesn't seem to be absolutely necessary.  We free up the
161          * clocks after we disable them, so the system could, in
162          * theory, reuse them.
163          */
164         bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl,
165             OHCI_CONTROL, 0);
166
167         if (sc->sc_ohci.sc_intr_hdl) {
168                 bus_teardown_intr(dev, sc->sc_ohci.sc_irq_res, sc->sc_ohci.sc_intr_hdl);
169                 sc->sc_ohci.sc_intr_hdl = NULL;
170         }
171
172         if (sc->sc_ohci.sc_irq_res && sc->sc_ohci.sc_intr_hdl) {
173                 /*
174                  * only call ohci_detach() after ohci_init()
175                  */
176                 ohci_detach(&sc->sc_ohci);
177
178                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_ohci.sc_irq_res);
179                 sc->sc_ohci.sc_irq_res = NULL;
180         }
181         if (sc->sc_ohci.sc_io_res) {
182                 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_ohci.sc_io_res);
183                 sc->sc_ohci.sc_io_res = NULL;
184                 sc->sc_ohci.sc_io_tag = 0;
185                 sc->sc_ohci.sc_io_hdl = 0;
186         }
187         usb_bus_mem_free_all(&sc->sc_ohci.sc_bus, &ohci_iterate_hw_softc);
188
189         return (0);
190 }
191
192 static device_method_t ohci_methods[] = {
193         /* Device interface */
194         DEVMETHOD(device_probe, ar71xx_ohci_probe),
195         DEVMETHOD(device_attach, ar71xx_ohci_attach),
196         DEVMETHOD(device_detach, ar71xx_ohci_detach),
197         DEVMETHOD(device_suspend, bus_generic_suspend),
198         DEVMETHOD(device_resume, bus_generic_resume),
199         DEVMETHOD(device_shutdown, bus_generic_shutdown),
200
201         DEVMETHOD_END
202 };
203
204 static driver_t ohci_driver = {
205         .name = "ohci",
206         .methods = ohci_methods,
207         .size = sizeof(struct ar71xx_ohci_softc),
208 };
209
210 static devclass_t ohci_devclass;
211
212 DRIVER_MODULE(ohci, apb, ohci_driver, ohci_devclass, 0, 0);