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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD: src/sys/arm/at91/ohci_atmelarm.c,v 1.5 2008/11/25 00:14:49 imp Exp $");
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/lock.h>
33 #include <sys/module.h>
34 #include <sys/mutex.h>
35 #include <sys/bus.h>
36 #include <sys/queue.h>
37 #include <machine/bus.h>
38 #include <sys/rman.h>
39 #include <machine/resource.h>
40
41 #include <dev/usb/usb.h>
42 #include <dev/usb/usbdi.h>
43 #include <dev/usb/usbdivar.h>
44 #include <dev/usb/usb_mem.h>
45
46 #include <dev/usb/ohcireg.h>
47 #include <dev/usb/ohcivar.h>
48
49 #include <arm/at91/at91_pmcvar.h>
50
51 #define MEM_RID 0
52
53 static int ohci_atmelarm_attach(device_t dev);
54 static int ohci_atmelarm_detach(device_t dev);
55
56 struct at91_ohci_softc
57 {
58 struct ohci_softc sc_ohci;
59 struct at91_pmc_clock *iclk;
60 struct at91_pmc_clock *fclk;
61 };
62
63 static int
64 ohci_atmelarm_probe(device_t dev)
65 {
66 device_set_desc(dev, "AT91 integrated ohci controller");
67 return (BUS_PROBE_DEFAULT);
68 }
69
70 static int
71 ohci_atmelarm_attach(device_t dev)
72 {
73 struct at91_ohci_softc *sc = device_get_softc(dev);
74 int err;
75 int rid;
76
77
78 sc->iclk = at91_pmc_clock_ref("ohci_clk");
79 sc->fclk = at91_pmc_clock_ref("uhpck");
80
81 rid = MEM_RID;
82 sc->sc_ohci.io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
83 RF_ACTIVE);
84 if (sc->sc_ohci.io_res == NULL) {
85 err = ENOMEM;
86 goto error;
87 }
88 sc->sc_ohci.iot = rman_get_bustag(sc->sc_ohci.io_res);
89 sc->sc_ohci.ioh = rman_get_bushandle(sc->sc_ohci.io_res);
90
91 rid = 0;
92 sc->sc_ohci.irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
93 RF_ACTIVE);
94 if (sc->sc_ohci.irq_res == NULL) {
95 err = ENOMEM;
96 goto error;
97 }
98 sc->sc_ohci.sc_bus.bdev = device_add_child(dev, "usb", -1);
99 if (sc->sc_ohci.sc_bus.bdev == NULL) {
100 err = ENOMEM;
101 goto error;
102 }
103 device_set_ivars(sc->sc_ohci.sc_bus.bdev, &sc->sc_ohci.sc_bus);
104
105 /* Allocate a parent dma tag for DMA maps */
106 err = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
107 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
108 BUS_SPACE_MAXSIZE_32BIT, USB_DMA_NSEG, BUS_SPACE_MAXSIZE_32BIT, 0,
109 NULL, NULL, &sc->sc_ohci.sc_bus.parent_dmatag);
110 if (err) {
111 device_printf(dev, "Could not allocate parent DMA tag (%d)\n",
112 err);
113 err = ENXIO;
114 goto error;
115 }
116
117 /* Allocate a dma tag for transfer buffers */
118 err = bus_dma_tag_create(sc->sc_ohci.sc_bus.parent_dmatag, 1, 0,
119 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
120 BUS_SPACE_MAXSIZE_32BIT, USB_DMA_NSEG, BUS_SPACE_MAXSIZE_32BIT, 0,
121 busdma_lock_mutex, &Giant, &sc->sc_ohci.sc_bus.buffer_dmatag);
122 if (err) {
123 device_printf(dev, "Could not allocate transfer tag (%d)\n",
124 err);
125 err = ENXIO;
126 goto error;
127 }
128
129 err = bus_setup_intr(dev, sc->sc_ohci.irq_res, INTR_TYPE_BIO, NULL,
130 ohci_intr, sc, &sc->sc_ohci.ih);
131 if (err) {
132 err = ENXIO;
133 goto error;
134 }
135 strlcpy(sc->sc_ohci.sc_vendor, "Atmel", sizeof(sc->sc_ohci.sc_vendor));
136
137 /*
138 * turn on the clocks from the AT91's point of view. Keep the unit in reset.
139 */
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);
232 Cache object: 268453606883f2a2dc6f2a22f14d128e
|