]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/at91/ohci_atmelarm.c
This commit was generated by cvs2svn to compensate for changes in r172958,
[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/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 //      bus_space_write_4(sc->sc_ohci.iot, sc->sc_ohci.ioh, OHCI_CONTROL, 0);
140         at91_pmc_clock_enable(sc->iclk);
141         at91_pmc_clock_enable(sc->fclk);
142         bus_space_write_4(sc->sc_ohci.iot, sc->sc_ohci.ioh, OHCI_CONTROL, 0);
143
144         err = ohci_init(&sc->sc_ohci);
145         if (!err) {
146                 sc->sc_ohci.sc_flags |= OHCI_SCFLG_DONEINIT;
147                 err = device_probe_and_attach(sc->sc_ohci.sc_bus.bdev);
148         }
149
150 error:;
151         if (err) {
152                 ohci_atmelarm_detach(dev);
153                 return (err);
154         }
155         return (err);
156 }
157
158 static int
159 ohci_atmelarm_detach(device_t dev)
160 {
161         struct at91_ohci_softc *sc = device_get_softc(dev);
162
163         if (sc->sc_ohci.sc_flags & OHCI_SCFLG_DONEINIT) {
164                 ohci_detach(&sc->sc_ohci, 0);
165                 sc->sc_ohci.sc_flags &= ~OHCI_SCFLG_DONEINIT;
166         }
167
168         /*
169          * Put the controller into reset, then disable clocks and do
170          * the MI tear down.  We have to disable the clocks/hardware
171          * after we do the rest of the teardown.  We also disable the
172          * clocks in the opposite order we acquire them, but that
173          * doesn't seem to be absolutely necessary.  We free up the
174          * clocks after we disable them, so the system could, in
175          * theory, reuse them.
176          */
177         bus_space_write_4(sc->sc_ohci.iot, sc->sc_ohci.ioh, OHCI_CONTROL, 0);
178         at91_pmc_clock_disable(sc->fclk);
179         at91_pmc_clock_disable(sc->iclk);
180         at91_pmc_clock_deref(sc->fclk);
181         at91_pmc_clock_deref(sc->iclk);
182
183         if (sc->sc_ohci.ih) {
184                 bus_teardown_intr(dev, sc->sc_ohci.irq_res, sc->sc_ohci.ih);
185                 sc->sc_ohci.ih = NULL;
186         }
187
188         if (sc->sc_ohci.sc_bus.parent_dmatag != NULL)
189                 bus_dma_tag_destroy(sc->sc_ohci.sc_bus.parent_dmatag);
190         if (sc->sc_ohci.sc_bus.buffer_dmatag != NULL)
191                 bus_dma_tag_destroy(sc->sc_ohci.sc_bus.buffer_dmatag);
192
193         if (sc->sc_ohci.sc_bus.bdev) {
194                 device_delete_child(dev, sc->sc_ohci.sc_bus.bdev);
195                 sc->sc_ohci.sc_bus.bdev = NULL;
196         }
197         if (sc->sc_ohci.irq_res) {
198                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_ohci.irq_res);
199                 sc->sc_ohci.irq_res = NULL;
200         }
201         if (sc->sc_ohci.io_res) {
202                 bus_release_resource(dev, SYS_RES_MEMORY, MEM_RID, sc->sc_ohci.io_res);
203                 sc->sc_ohci.io_res = NULL;
204                 sc->sc_ohci.iot = 0;
205                 sc->sc_ohci.ioh = 0;
206         }
207         return (0);
208 }
209
210 static device_method_t ohci_methods[] = {
211         /* Device interface */
212         DEVMETHOD(device_probe, ohci_atmelarm_probe),
213         DEVMETHOD(device_attach, ohci_atmelarm_attach),
214         DEVMETHOD(device_detach, ohci_atmelarm_detach),
215         DEVMETHOD(device_shutdown, bus_generic_shutdown),
216
217         /* Bus interface */
218         DEVMETHOD(bus_print_child, bus_generic_print_child),
219
220         {0, 0}
221 };
222
223 static driver_t ohci_driver = {
224         "ohci",
225         ohci_methods,
226         sizeof(struct at91_ohci_softc),
227 };
228
229 static devclass_t ohci_devclass;
230
231 DRIVER_MODULE(ohci, atmelarm, ohci_driver, ohci_devclass, 0, 0);