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 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);
143 err = ohci_init(&sc->sc_ohci);
145 sc->sc_ohci.sc_flags |= OHCI_SCFLG_DONEINIT;
146 err = device_probe_and_attach(sc->sc_ohci.sc_bus.bdev);
151 ohci_atmelarm_detach(dev);
158 ohci_atmelarm_detach(device_t dev)
160 struct at91_ohci_softc *sc = device_get_softc(dev);
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;
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.
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);
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;
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);
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;
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;
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;
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),
217 DEVMETHOD(bus_print_child, bus_generic_print_child),
222 static driver_t ohci_driver = {
225 sizeof(struct at91_ohci_softc),
228 static devclass_t ohci_devclass;
230 DRIVER_MODULE(ohci, atmelarm, ohci_driver, ohci_devclass, 0, 0);