]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/at91/ohci_atmelarm.c
This commit was generated by cvs2svn to compensate for changes in r168609,
[FreeBSD/FreeBSD.git] / sys / arm / at91 / ohci_atmelarm.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/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
31 #include <sys/module.h>
32 #include <sys/bus.h>
33 #include <sys/queue.h>
34 #include <machine/bus.h>
35 #include <sys/rman.h>
36 #include <machine/resource.h>
37
38 #include <dev/usb/usb.h>
39 #include <dev/usb/usbdi.h>
40 #include <dev/usb/usbdivar.h>
41 #include <dev/usb/usb_mem.h>
42
43 #include <dev/usb/ohcireg.h>
44 #include <dev/usb/ohcivar.h>
45
46 #include <arm/at91/at91_pmcvar.h>
47
48 #define MEM_RID 0
49
50 static int ohci_atmelarm_attach(device_t dev);
51 static int ohci_atmelarm_detach(device_t dev);
52
53 struct at91_ohci_softc
54 {
55         struct ohci_softc sc_ohci;
56         struct at91_pmc_clock *iclk;
57         struct at91_pmc_clock *fclk;
58 };
59
60 static int
61 ohci_atmelarm_probe(device_t dev)
62 {
63         device_set_desc(dev, "AT91 integrated ohci controller");
64         return (BUS_PROBE_DEFAULT);
65 }
66
67 static int
68 ohci_atmelarm_attach(device_t dev)
69 {
70         struct at91_ohci_softc *sc = device_get_softc(dev);
71         int err;
72         int rid;
73
74         
75         sc->iclk = at91_pmc_clock_ref("ohci_clk");
76         sc->fclk = at91_pmc_clock_ref("uhpck");
77
78         rid = MEM_RID;
79         sc->sc_ohci.io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
80             RF_ACTIVE);
81         if (sc->sc_ohci.io_res == NULL) {
82                 err = ENOMEM;
83                 goto error;
84         }
85         sc->sc_ohci.iot = rman_get_bustag(sc->sc_ohci.io_res);
86         sc->sc_ohci.ioh = rman_get_bushandle(sc->sc_ohci.io_res);
87
88         rid = 0;
89         sc->sc_ohci.irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
90             RF_ACTIVE);
91         if (sc->sc_ohci.irq_res == NULL) {
92                 err = ENOMEM;
93                 goto error;
94         }
95         sc->sc_ohci.sc_bus.bdev = device_add_child(dev, "usb", -1);
96         if (sc->sc_ohci.sc_bus.bdev == NULL) {
97                 err = ENOMEM;
98                 goto error;
99         }
100         device_set_ivars(sc->sc_ohci.sc_bus.bdev, &sc->sc_ohci.sc_bus);
101
102         err = bus_setup_intr(dev, sc->sc_ohci.irq_res, INTR_TYPE_BIO, NULL, 
103             ohci_intr, sc, &sc->sc_ohci.ih);
104         if (err) {
105                 err = ENXIO;
106                 goto error;
107         }
108         strlcpy(sc->sc_ohci.sc_vendor, "Atmel", sizeof(sc->sc_ohci.sc_vendor));
109
110         /*
111          * turn on the clocks from the AT91's point of view.  Keep the unit in reset.
112          */
113 //      bus_space_write_4(sc->sc_ohci.iot, sc->sc_ohci.ioh, OHCI_CONTROL, 0);
114         at91_pmc_clock_enable(sc->iclk);
115         at91_pmc_clock_enable(sc->fclk);
116         bus_space_write_4(sc->sc_ohci.iot, sc->sc_ohci.ioh, OHCI_CONTROL, 0);
117
118         err = ohci_init(&sc->sc_ohci);
119         if (!err) {
120                 sc->sc_ohci.sc_flags |= OHCI_SCFLG_DONEINIT;
121                 err = device_probe_and_attach(sc->sc_ohci.sc_bus.bdev);
122         }
123
124 error:;
125         if (err) {
126                 ohci_atmelarm_detach(dev);
127                 return (err);
128         }
129         return (err);
130 }
131
132 static int
133 ohci_atmelarm_detach(device_t dev)
134 {
135         struct at91_ohci_softc *sc = device_get_softc(dev);
136
137         if (sc->sc_ohci.sc_flags & OHCI_SCFLG_DONEINIT) {
138                 ohci_detach(&sc->sc_ohci, 0);
139                 sc->sc_ohci.sc_flags &= ~OHCI_SCFLG_DONEINIT;
140         }
141
142         /*
143          * Put the controller into reset, then disable clocks and do
144          * the MI tear down.  We have to disable the clocks/hardware
145          * after we do the rest of the teardown.  We also disable the
146          * clocks in the opposite order we acquire them, but that
147          * doesn't seem to be absolutely necessary.  We free up the
148          * clocks after we disable them, so the system could, in
149          * theory, reuse them.
150          */
151         bus_space_write_4(sc->sc_ohci.iot, sc->sc_ohci.ioh, OHCI_CONTROL, 0);
152         at91_pmc_clock_disable(sc->fclk);
153         at91_pmc_clock_disable(sc->iclk);
154         at91_pmc_clock_deref(sc->fclk);
155         at91_pmc_clock_deref(sc->iclk);
156
157         if (sc->sc_ohci.ih) {
158                 bus_teardown_intr(dev, sc->sc_ohci.irq_res, sc->sc_ohci.ih);
159                 sc->sc_ohci.ih = NULL;
160         }
161         if (sc->sc_ohci.sc_bus.bdev) {
162                 device_delete_child(dev, sc->sc_ohci.sc_bus.bdev);
163                 sc->sc_ohci.sc_bus.bdev = NULL;
164         }
165         if (sc->sc_ohci.irq_res) {
166                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_ohci.irq_res);
167                 sc->sc_ohci.irq_res = NULL;
168         }
169         if (sc->sc_ohci.io_res) {
170                 bus_release_resource(dev, SYS_RES_MEMORY, MEM_RID, sc->sc_ohci.io_res);
171                 sc->sc_ohci.io_res = NULL;
172                 sc->sc_ohci.iot = 0;
173                 sc->sc_ohci.ioh = 0;
174         }
175         return (0);
176 }
177
178 static device_method_t ohci_methods[] = {
179         /* Device interface */
180         DEVMETHOD(device_probe, ohci_atmelarm_probe),
181         DEVMETHOD(device_attach, ohci_atmelarm_attach),
182         DEVMETHOD(device_detach, ohci_atmelarm_detach),
183         DEVMETHOD(device_shutdown, bus_generic_shutdown),
184
185         /* Bus interface */
186         DEVMETHOD(bus_print_child, bus_generic_print_child),
187
188         {0, 0}
189 };
190
191 static driver_t ohci_driver = {
192         "ohci",
193         ohci_methods,
194         sizeof(struct at91_ohci_softc),
195 };
196
197 static devclass_t ohci_devclass;
198
199 DRIVER_MODULE(ohci, atmelarm, ohci_driver, ohci_devclass, 0, 0);