musb_otg_atmelarm.c revision 187170
1/* $FreeBSD: head/sys/dev/usb2/controller/musb2_otg_atmelarm.c 187170 2009-01-13 19:02:40Z thompsa $ */
2/*-
3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <dev/usb2/include/usb2_mfunc.h>
28#include <dev/usb2/include/usb2_defs.h>
29#include <dev/usb2/include/usb2_standard.h>
30
31#include <dev/usb2/core/usb2_core.h>
32#include <dev/usb2/core/usb2_busdma.h>
33#include <dev/usb2/core/usb2_process.h>
34#include <dev/usb2/core/usb2_config_td.h>
35#include <dev/usb2/core/usb2_sw_transfer.h>
36#include <dev/usb2/core/usb2_util.h>
37
38#include <dev/usb2/controller/usb2_controller.h>
39#include <dev/usb2/controller/usb2_bus.h>
40#include <dev/usb2/controller/musb2_otg.h>
41
42#include <sys/rman.h>
43
44static device_probe_t musbotg_probe;
45static device_attach_t musbotg_attach;
46static device_detach_t musbotg_detach;
47static device_shutdown_t musbotg_shutdown;
48
49struct musbotg_super_softc {
50	struct musbotg_softc sc_otg;	/* must be first */
51};
52
53static void
54musbotg_vbus_interrupt(struct musbotg_super_softc *sc)
55{
56	uint8_t vbus_val = 1;		/* fake VBUS on - TODO */
57
58	/* just forward it */
59
60	(sc->sc_otg.sc_bus.methods->vbus_interrupt)
61	    (&sc->sc_otg.sc_bus, vbus_val);
62}
63
64static void
65musbotg_clocks_on(void *arg)
66{
67#if 0
68	struct musbotg_super_softc *sc = arg;
69
70#endif
71}
72
73static void
74musbotg_clocks_off(void *arg)
75{
76#if 0
77	struct musbotg_super_softc *sc = arg;
78
79#endif
80}
81
82static int
83musbotg_probe(device_t dev)
84{
85	device_set_desc(dev, "MUSB OTG integrated USB controller");
86	return (0);
87}
88
89static int
90musbotg_attach(device_t dev)
91{
92	struct musbotg_super_softc *sc = device_get_softc(dev);
93	int err;
94	int rid;
95
96	if (sc == NULL) {
97		return (ENXIO);
98	}
99	/* setup MUSB OTG USB controller interface softc */
100
101	sc->sc_otg.sc_clocks_on = &musbotg_clocks_on;
102	sc->sc_otg.sc_clocks_off = &musbotg_clocks_off;
103	sc->sc_otg.sc_clocks_arg = sc;
104
105	/* initialise some bus fields */
106	sc->sc_otg.sc_bus.parent = dev;
107	sc->sc_otg.sc_bus.devices = sc->sc_otg.sc_devices;
108	sc->sc_otg.sc_bus.devices_max = MUSB2_MAX_DEVICES;
109
110	/* get all DMA memory */
111	if (usb2_bus_mem_alloc_all(&sc->sc_otg.sc_bus,
112	    USB_GET_DMA_TAG(dev), NULL)) {
113		return (ENOMEM);
114	}
115	rid = 0;
116	sc->sc_otg.sc_io_res =
117	    bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
118
119	if (!(sc->sc_otg.sc_io_res)) {
120		err = ENOMEM;
121		goto error;
122	}
123	sc->sc_otg.sc_io_tag = rman_get_bustag(sc->sc_otg.sc_io_res);
124	sc->sc_otg.sc_io_hdl = rman_get_bushandle(sc->sc_otg.sc_io_res);
125	sc->sc_otg.sc_io_size = rman_get_size(sc->sc_otg.sc_io_res);
126
127	rid = 0;
128	sc->sc_otg.sc_irq_res =
129	    bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
130	if (!(sc->sc_otg.sc_irq_res)) {
131		goto error;
132	}
133	sc->sc_otg.sc_bus.bdev = device_add_child(dev, "usbus", -1);
134	if (!(sc->sc_otg.sc_bus.bdev)) {
135		goto error;
136	}
137	device_set_ivars(sc->sc_otg.sc_bus.bdev, &sc->sc_otg.sc_bus);
138
139	err = usb2_config_td_setup(&sc->sc_otg.sc_config_td, sc,
140	    &sc->sc_otg.sc_bus.bus_mtx, NULL, 0, 4);
141	if (err) {
142		device_printf(dev, "could not setup config thread!\n");
143		goto error;
144	}
145#if (__FreeBSD_version >= 700031)
146	err = bus_setup_intr(dev, sc->sc_otg.sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
147	    NULL, (void *)musbotg_interrupt, sc, &sc->sc_otg.sc_intr_hdl);
148#else
149	err = bus_setup_intr(dev, sc->sc_otg.sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
150	    (void *)musbotg_interrupt, sc, &sc->sc_otg.sc_intr_hdl);
151#endif
152	if (err) {
153		sc->sc_otg.sc_intr_hdl = NULL;
154		goto error;
155	}
156	err = musbotg_init(&sc->sc_otg);
157	if (!err) {
158		err = device_probe_and_attach(sc->sc_otg.sc_bus.bdev);
159	}
160	if (err) {
161		goto error;
162	} else {
163		/* poll VBUS one time */
164		musbotg_vbus_interrupt(sc);
165	}
166	return (0);
167
168error:
169	musbotg_detach(dev);
170	return (ENXIO);
171}
172
173static int
174musbotg_detach(device_t dev)
175{
176	struct musbotg_super_softc *sc = device_get_softc(dev);
177	device_t bdev;
178	int err;
179
180	if (sc->sc_otg.sc_bus.bdev) {
181		bdev = sc->sc_otg.sc_bus.bdev;
182		device_detach(bdev);
183		device_delete_child(dev, bdev);
184	}
185	/* during module unload there are lots of children leftover */
186	device_delete_all_children(dev);
187
188	if (sc->sc_otg.sc_irq_res && sc->sc_otg.sc_intr_hdl) {
189		/*
190		 * only call musbotg_uninit() after musbotg_init()
191		 */
192		musbotg_uninit(&sc->sc_otg);
193
194		err = bus_teardown_intr(dev, sc->sc_otg.sc_irq_res,
195		    sc->sc_otg.sc_intr_hdl);
196		sc->sc_otg.sc_intr_hdl = NULL;
197	}
198	/* free IRQ channel, if any */
199	if (sc->sc_otg.sc_irq_res) {
200		bus_release_resource(dev, SYS_RES_IRQ, 0,
201		    sc->sc_otg.sc_irq_res);
202		sc->sc_otg.sc_irq_res = NULL;
203	}
204	/* free memory resource, if any */
205	if (sc->sc_otg.sc_io_res) {
206		bus_release_resource(dev, SYS_RES_MEMORY, 0,
207		    sc->sc_otg.sc_io_res);
208		sc->sc_otg.sc_io_res = NULL;
209	}
210	usb2_config_td_unsetup(&sc->sc_otg.sc_config_td);
211
212	usb2_bus_mem_free_all(&sc->sc_otg.sc_bus, NULL);
213
214	return (0);
215}
216
217static int
218musbotg_shutdown(device_t dev)
219{
220	struct musbotg_super_softc *sc = device_get_softc(dev);
221	int err;
222
223	err = bus_generic_shutdown(dev);
224	if (err)
225		return (err);
226
227	musbotg_uninit(&sc->sc_otg);
228
229	return (0);
230}
231
232static device_method_t musbotg_methods[] = {
233	/* Device interface */
234	DEVMETHOD(device_probe, musbotg_probe),
235	DEVMETHOD(device_attach, musbotg_attach),
236	DEVMETHOD(device_detach, musbotg_detach),
237	DEVMETHOD(device_shutdown, musbotg_shutdown),
238
239	/* Bus interface */
240	DEVMETHOD(bus_print_child, bus_generic_print_child),
241
242	{0, 0}
243};
244
245static driver_t musbotg_driver = {
246	"musbotg",
247	musbotg_methods,
248	sizeof(struct musbotg_super_softc),
249};
250
251static devclass_t musbotg_devclass;
252
253DRIVER_MODULE(musbotg, atmelarm, musbotg_driver, musbotg_devclass, 0, 0);
254MODULE_DEPEND(musbotg, usb2_controller, 1, 1, 1);
255MODULE_DEPEND(musbotg, usb2_core, 1, 1, 1);
256