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