2 * Copyright (c) 2006 M. Warner Losh. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/mutex.h>
35 #include <sys/queue.h>
36 #include <machine/bus.h>
38 #include <machine/resource.h>
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>
45 #include <dev/usb/ohcireg.h>
46 #include <dev/usb/ohcivar.h>
48 #include <arm/at91/at91_pmcvar.h>
52 static int ohci_atmelarm_attach(device_t dev);
53 static int ohci_atmelarm_detach(device_t dev);
55 struct at91_ohci_softc
57 struct ohci_softc sc_ohci;
58 struct at91_pmc_clock *iclk;
59 struct at91_pmc_clock *fclk;
63 ohci_atmelarm_probe(device_t dev)
65 device_set_desc(dev, "AT91 integrated ohci controller");
66 return (BUS_PROBE_DEFAULT);
70 ohci_atmelarm_attach(device_t dev)
72 struct at91_ohci_softc *sc = device_get_softc(dev);
77 sc->iclk = at91_pmc_clock_ref("ohci_clk");
78 sc->fclk = at91_pmc_clock_ref("uhpck");
81 sc->sc_ohci.io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
83 if (sc->sc_ohci.io_res == NULL) {
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);
91 sc->sc_ohci.irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
93 if (sc->sc_ohci.irq_res == NULL) {
97 sc->sc_ohci.sc_bus.bdev = device_add_child(dev, "usb", -1);
98 if (sc->sc_ohci.sc_bus.bdev == NULL) {
102 device_set_ivars(sc->sc_ohci.sc_bus.bdev, &sc->sc_ohci.sc_bus);
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);
110 device_printf(dev, "Could not allocate parent DMA tag (%d)\n",
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);
122 device_printf(dev, "Could not allocate transfer tag (%d)\n",
128 err = bus_setup_intr(dev, sc->sc_ohci.irq_res, INTR_TYPE_BIO, NULL,
129 ohci_intr, sc, &sc->sc_ohci.ih);
134 strlcpy(sc->sc_ohci.sc_vendor, "Atmel", sizeof(sc->sc_ohci.sc_vendor));
137 * turn on the clocks from the AT91's point of view. Keep the unit in reset.
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);
144 err = ohci_init(&sc->sc_ohci);
146 sc->sc_ohci.sc_flags |= OHCI_SCFLG_DONEINIT;
147 err = device_probe_and_attach(sc->sc_ohci.sc_bus.bdev);
152 ohci_atmelarm_detach(dev);
159 ohci_atmelarm_detach(device_t dev)
161 struct at91_ohci_softc *sc = device_get_softc(dev);
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;
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.
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);
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;
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);
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;
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;
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;
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),
218 DEVMETHOD(bus_print_child, bus_generic_print_child),
223 static driver_t ohci_driver = {
226 sizeof(struct at91_ohci_softc),
229 static devclass_t ohci_devclass;
231 DRIVER_MODULE(ohci, atmelarm, ohci_driver, ohci_devclass, 0, 0);