]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/at91/at91_ohci_fdt.c
MFV r315633, 315635:
[FreeBSD/FreeBSD.git] / sys / arm / at91 / at91_ohci_fdt.c
1 /*-
2  * Copyright (c) 2006 M. Warner Losh.  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 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
27
28 #include <sys/stdint.h>
29 #include <sys/stddef.h>
30 #include <sys/param.h>
31 #include <sys/queue.h>
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/bus.h>
36 #include <sys/module.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 #include <sys/condvar.h>
40 #include <sys/sysctl.h>
41 #include <sys/sx.h>
42 #include <sys/unistd.h>
43 #include <sys/callout.h>
44 #include <sys/malloc.h>
45 #include <sys/priv.h>
46
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usbdi.h>
49
50 #include <dev/usb/usb_core.h>
51 #include <dev/usb/usb_busdma.h>
52 #include <dev/usb/usb_process.h>
53 #include <dev/usb/usb_util.h>
54
55 #include <dev/usb/usb_controller.h>
56 #include <dev/usb/usb_bus.h>
57 #include <dev/usb/controller/ohci.h>
58 #include <dev/usb/controller/ohcireg.h>
59
60 #include <sys/rman.h>
61
62 #include <arm/at91/at91_pmcvar.h>
63
64 #include <dev/ofw/ofw_bus.h>
65 #include <dev/ofw/ofw_bus_subr.h>
66
67 #define MEM_RID 0
68
69 static device_probe_t ohci_at91_fdt_probe;
70 static device_attach_t ohci_at91_fdt_attach;
71 static device_detach_t ohci_at91_fdt_detach;
72
73 struct at91_ohci_softc {
74         struct ohci_softc sc_ohci;      /* must be first */
75         struct at91_pmc_clock *mclk;
76         struct at91_pmc_clock *iclk;
77         struct at91_pmc_clock *fclk;
78 };
79
80 static int
81 ohci_at91_fdt_probe(device_t dev)
82 {
83         if (!ofw_bus_is_compatible(dev, "atmel,at91rm9200-ohci"))
84                 return (ENXIO);
85         device_set_desc(dev, "AT91 integrated OHCI controller");
86
87         return (BUS_PROBE_DEFAULT);
88 }
89
90 static int
91 ohci_at91_fdt_attach(device_t dev)
92 {
93         struct at91_ohci_softc *sc = device_get_softc(dev);
94         int err;
95         int rid;
96
97         /* initialise some bus fields */
98         sc->sc_ohci.sc_bus.parent = dev;
99         sc->sc_ohci.sc_bus.devices = sc->sc_ohci.sc_devices;
100         sc->sc_ohci.sc_bus.devices_max = OHCI_MAX_DEVICES;
101         sc->sc_ohci.sc_bus.dma_bits = 32;
102
103         /* get all DMA memory */
104         if (usb_bus_mem_alloc_all(&sc->sc_ohci.sc_bus,
105             USB_GET_DMA_TAG(dev), &ohci_iterate_hw_softc)) {
106                 return (ENOMEM);
107         }
108         sc->mclk = at91_pmc_clock_ref("mck");
109         sc->iclk = at91_pmc_clock_ref("ohci_clk");
110         sc->fclk = at91_pmc_clock_ref("uhpck");
111
112         sc->sc_ohci.sc_dev = dev;
113
114         rid = MEM_RID;
115         sc->sc_ohci.sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
116             &rid, RF_ACTIVE);
117
118         if (!(sc->sc_ohci.sc_io_res)) {
119                 err = ENOMEM;
120                 goto error;
121         }
122         sc->sc_ohci.sc_io_tag = rman_get_bustag(sc->sc_ohci.sc_io_res);
123         sc->sc_ohci.sc_io_hdl = rman_get_bushandle(sc->sc_ohci.sc_io_res);
124         sc->sc_ohci.sc_io_size = rman_get_size(sc->sc_ohci.sc_io_res);
125
126         rid = 0;
127         sc->sc_ohci.sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
128             RF_ACTIVE);
129         if (!(sc->sc_ohci.sc_irq_res)) {
130                 goto error;
131         }
132         sc->sc_ohci.sc_bus.bdev = device_add_child(dev, "usbus", -1);
133         if (!(sc->sc_ohci.sc_bus.bdev)) {
134                 goto error;
135         }
136         device_set_ivars(sc->sc_ohci.sc_bus.bdev, &sc->sc_ohci.sc_bus);
137
138         strlcpy(sc->sc_ohci.sc_vendor, "Atmel", sizeof(sc->sc_ohci.sc_vendor));
139
140         err = bus_setup_intr(dev, sc->sc_ohci.sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
141             NULL, (driver_intr_t *)ohci_interrupt, sc, &sc->sc_ohci.sc_intr_hdl);
142         if (err) {
143                 sc->sc_ohci.sc_intr_hdl = NULL;
144                 goto error;
145         }
146         /*
147          * turn on the clocks from the AT91's point of view.  Keep the unit in reset.
148          */
149         at91_pmc_clock_enable(sc->mclk);
150         at91_pmc_clock_enable(sc->iclk);
151         at91_pmc_clock_enable(sc->fclk);
152         bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl,
153             OHCI_CONTROL, 0);
154
155         err = ohci_init(&sc->sc_ohci);
156         if (!err) {
157                 err = device_probe_and_attach(sc->sc_ohci.sc_bus.bdev);
158         }
159         if (err) {
160                 goto error;
161         }
162         return (0);
163
164 error:
165         ohci_at91_fdt_detach(dev);
166         return (ENXIO);
167 }
168
169 static int
170 ohci_at91_fdt_detach(device_t dev)
171 {
172         struct at91_ohci_softc *sc = device_get_softc(dev);
173         int err;
174
175         /* during module unload there are lots of children leftover */
176         device_delete_children(dev);
177
178         if (sc->sc_ohci.sc_io_res != NULL) {
179                 /*
180                  * Put the controller into reset, then disable clocks and do
181                  * the MI tear down.  We have to disable the clocks/hardware
182                  * after we do the rest of the teardown.  We also disable the
183                  * clocks in the opposite order we acquire them, but that
184                  * doesn't seem to be absolutely necessary.  We free up the
185                  * clocks after we disable them, so the system could, in
186                  * theory, reuse them.
187                  */
188                 bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl,
189                                   OHCI_CONTROL, 0);
190
191                 at91_pmc_clock_disable(sc->fclk);
192                 at91_pmc_clock_disable(sc->iclk);
193                 at91_pmc_clock_disable(sc->mclk);
194                 at91_pmc_clock_deref(sc->fclk);
195                 at91_pmc_clock_deref(sc->iclk);
196                 at91_pmc_clock_deref(sc->mclk);
197
198                 if (sc->sc_ohci.sc_irq_res && sc->sc_ohci.sc_intr_hdl) {
199                         /*
200                          * only call ohci_detach() after ohci_init()
201                          */
202                         ohci_detach(&sc->sc_ohci);
203
204                         err = bus_teardown_intr(dev, sc->sc_ohci.sc_irq_res,
205                             sc->sc_ohci.sc_intr_hdl);
206                         sc->sc_ohci.sc_intr_hdl = NULL;
207                 }
208                 if (sc->sc_ohci.sc_irq_res) {
209                         bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_ohci.sc_irq_res);
210                         sc->sc_ohci.sc_irq_res = NULL;
211                 }
212                 if (sc->sc_ohci.sc_io_res) {
213                         bus_release_resource(dev, SYS_RES_MEMORY, MEM_RID,
214                                              sc->sc_ohci.sc_io_res);
215                         sc->sc_ohci.sc_io_res = NULL;
216                 }
217         }
218         usb_bus_mem_free_all(&sc->sc_ohci.sc_bus, &ohci_iterate_hw_softc);
219
220         return (0);
221 }
222
223 static device_method_t ohci_methods[] = {
224         /* Device interface */
225         DEVMETHOD(device_probe, ohci_at91_fdt_probe),
226         DEVMETHOD(device_attach, ohci_at91_fdt_attach),
227         DEVMETHOD(device_detach, ohci_at91_fdt_detach),
228         DEVMETHOD(device_suspend, bus_generic_suspend),
229         DEVMETHOD(device_resume, bus_generic_resume),
230         DEVMETHOD(device_shutdown, bus_generic_shutdown),
231
232         DEVMETHOD_END
233 };
234
235 static driver_t ohci_driver = {
236         .name = "ohci",
237         .methods = ohci_methods,
238         .size = sizeof(struct at91_ohci_softc),
239 };
240
241 static devclass_t ohci_devclass;
242
243 DRIVER_MODULE(ohci, simplebus, ohci_driver, ohci_devclass, 0, 0);
244 MODULE_DEPEND(ohci, usb, 1, 1, 1);