at91_ohci.c revision 190755
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: head/sys/dev/usb/controller/ohci_atmelarm.c 190755 2009-04-06 00:32:54Z thompsa $");
27
28#include <dev/usb/usb_mfunc.h>
29#include <dev/usb/usb.h>
30
31#include <dev/usb/usb_core.h>
32#include <dev/usb/usb_busdma.h>
33#include <dev/usb/usb_process.h>
34#include <dev/usb/usb_util.h>
35
36#include <dev/usb/usb_controller.h>
37#include <dev/usb/usb_bus.h>
38#include <dev/usb/controller/ohci.h>
39
40#include <sys/rman.h>
41
42#include <arm/at91/at91_pmcvar.h>
43
44#define	MEM_RID	0
45
46static device_probe_t ohci_atmelarm_probe;
47static device_attach_t ohci_atmelarm_attach;
48static device_detach_t ohci_atmelarm_detach;
49
50struct at91_ohci_softc {
51	struct ohci_softc sc_ohci;	/* must be first */
52	struct at91_pmc_clock *iclk;
53	struct at91_pmc_clock *fclk;
54};
55
56static int
57ohci_atmelarm_probe(device_t dev)
58{
59	device_set_desc(dev, "AT91 integrated OHCI controller");
60	return (BUS_PROBE_DEFAULT);
61}
62
63static int
64ohci_atmelarm_attach(device_t dev)
65{
66	struct at91_ohci_softc *sc = device_get_softc(dev);
67	int err;
68	int rid;
69
70	/* initialise some bus fields */
71	sc->sc_ohci.sc_bus.parent = dev;
72	sc->sc_ohci.sc_bus.devices = sc->sc_ohci.sc_devices;
73	sc->sc_ohci.sc_bus.devices_max = OHCI_MAX_DEVICES;
74
75	/* get all DMA memory */
76	if (usb2_bus_mem_alloc_all(&sc->sc_ohci.sc_bus,
77	    USB_GET_DMA_TAG(dev), &ohci_iterate_hw_softc)) {
78		return (ENOMEM);
79	}
80	sc->iclk = at91_pmc_clock_ref("ohci_clk");
81	sc->fclk = at91_pmc_clock_ref("uhpck");
82
83	sc->sc_ohci.sc_dev = dev;
84
85	rid = MEM_RID;
86	sc->sc_ohci.sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
87	    &rid, RF_ACTIVE);
88
89	if (!(sc->sc_ohci.sc_io_res)) {
90		err = ENOMEM;
91		goto error;
92	}
93	sc->sc_ohci.sc_io_tag = rman_get_bustag(sc->sc_ohci.sc_io_res);
94	sc->sc_ohci.sc_io_hdl = rman_get_bushandle(sc->sc_ohci.sc_io_res);
95	sc->sc_ohci.sc_io_size = rman_get_size(sc->sc_ohci.sc_io_res);
96
97	rid = 0;
98	sc->sc_ohci.sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
99	    RF_ACTIVE);
100	if (!(sc->sc_ohci.sc_irq_res)) {
101		goto error;
102	}
103	sc->sc_ohci.sc_bus.bdev = device_add_child(dev, "usbus", -1);
104	if (!(sc->sc_ohci.sc_bus.bdev)) {
105		goto error;
106	}
107	device_set_ivars(sc->sc_ohci.sc_bus.bdev, &sc->sc_ohci.sc_bus);
108
109	strlcpy(sc->sc_ohci.sc_vendor, "Atmel", sizeof(sc->sc_ohci.sc_vendor));
110
111#if (__FreeBSD_version >= 700031)
112	err = bus_setup_intr(dev, sc->sc_ohci.sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
113	    NULL, (driver_intr_t *)ohci_interrupt, sc, &sc->sc_ohci.sc_intr_hdl);
114#else
115	err = bus_setup_intr(dev, sc->sc_ohci.sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
116	    (driver_intr_t *)ohci_interrupt, sc, &sc->sc_ohci.sc_intr_hdl);
117#endif
118	if (err) {
119		sc->sc_ohci.sc_intr_hdl = NULL;
120		goto error;
121	}
122	/*
123	 * turn on the clocks from the AT91's point of view.  Keep the unit in reset.
124	 */
125	at91_pmc_clock_enable(sc->iclk);
126	at91_pmc_clock_enable(sc->fclk);
127	bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl,
128	    OHCI_CONTROL, 0);
129
130	err = ohci_init(&sc->sc_ohci);
131	if (!err) {
132		err = device_probe_and_attach(sc->sc_ohci.sc_bus.bdev);
133	}
134	if (err) {
135		goto error;
136	}
137	return (0);
138
139error:
140	ohci_atmelarm_detach(dev);
141	return (ENXIO);
142}
143
144static int
145ohci_atmelarm_detach(device_t dev)
146{
147	struct at91_ohci_softc *sc = device_get_softc(dev);
148	device_t bdev;
149	int err;
150
151	if (sc->sc_ohci.sc_bus.bdev) {
152		bdev = sc->sc_ohci.sc_bus.bdev;
153		device_detach(bdev);
154		device_delete_child(dev, bdev);
155	}
156	/* during module unload there are lots of children leftover */
157	device_delete_all_children(dev);
158
159	/*
160	 * Put the controller into reset, then disable clocks and do
161	 * the MI tear down.  We have to disable the clocks/hardware
162	 * after we do the rest of the teardown.  We also disable the
163	 * clocks in the opposite order we acquire them, but that
164	 * doesn't seem to be absolutely necessary.  We free up the
165	 * clocks after we disable them, so the system could, in
166	 * theory, reuse them.
167	 */
168	bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl,
169	    OHCI_CONTROL, 0);
170
171	at91_pmc_clock_disable(sc->fclk);
172	at91_pmc_clock_disable(sc->iclk);
173	at91_pmc_clock_deref(sc->fclk);
174	at91_pmc_clock_deref(sc->iclk);
175
176	if (sc->sc_ohci.sc_irq_res && sc->sc_ohci.sc_intr_hdl) {
177		/*
178		 * only call ohci_detach() after ohci_init()
179		 */
180		ohci_detach(&sc->sc_ohci);
181
182		err = bus_teardown_intr(dev, sc->sc_ohci.sc_irq_res, sc->sc_ohci.sc_intr_hdl);
183		sc->sc_ohci.sc_intr_hdl = NULL;
184	}
185	if (sc->sc_ohci.sc_irq_res) {
186		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_ohci.sc_irq_res);
187		sc->sc_ohci.sc_irq_res = NULL;
188	}
189	if (sc->sc_ohci.sc_io_res) {
190		bus_release_resource(dev, SYS_RES_MEMORY, MEM_RID,
191		    sc->sc_ohci.sc_io_res);
192		sc->sc_ohci.sc_io_res = NULL;
193	}
194	usb2_bus_mem_free_all(&sc->sc_ohci.sc_bus, &ohci_iterate_hw_softc);
195
196	return (0);
197}
198
199static device_method_t ohci_methods[] = {
200	/* Device interface */
201	DEVMETHOD(device_probe, ohci_atmelarm_probe),
202	DEVMETHOD(device_attach, ohci_atmelarm_attach),
203	DEVMETHOD(device_detach, ohci_atmelarm_detach),
204	DEVMETHOD(device_shutdown, bus_generic_shutdown),
205
206	/* Bus interface */
207	DEVMETHOD(bus_print_child, bus_generic_print_child),
208
209	{0, 0}
210};
211
212static driver_t ohci_driver = {
213	"ohci",
214	ohci_methods,
215	sizeof(struct at91_ohci_softc),
216};
217
218static devclass_t ohci_devclass;
219
220DRIVER_MODULE(ohci, atmelarm, ohci_driver, ohci_devclass, 0, 0);
221MODULE_DEPEND(ohci, usb, 1, 1, 1);
222