at91dci_atmelarm.c revision 196219
1#include <sys/cdefs.h>
2__FBSDID("$FreeBSD: head/sys/dev/usb/controller/at91dci_atmelarm.c 196219 2009-08-14 20:03:53Z jhb $");
3
4/*-
5 * Copyright (c) 2007-2008 Hans Petter Selasky. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/stdint.h>
30#include <sys/stddef.h>
31#include <sys/param.h>
32#include <sys/queue.h>
33#include <sys/types.h>
34#include <sys/systm.h>
35#include <sys/kernel.h>
36#include <sys/bus.h>
37#include <sys/linker_set.h>
38#include <sys/module.h>
39#include <sys/lock.h>
40#include <sys/mutex.h>
41#include <sys/condvar.h>
42#include <sys/sysctl.h>
43#include <sys/sx.h>
44#include <sys/unistd.h>
45#include <sys/callout.h>
46#include <sys/malloc.h>
47#include <sys/priv.h>
48
49#include <dev/usb/usb.h>
50#include <dev/usb/usbdi.h>
51
52#include <dev/usb/usb_core.h>
53#include <dev/usb/usb_busdma.h>
54#include <dev/usb/usb_process.h>
55#include <dev/usb/usb_util.h>
56
57#include <dev/usb/usb_controller.h>
58#include <dev/usb/usb_bus.h>
59#include <dev/usb/controller/at91dci.h>
60
61#include <sys/rman.h>
62
63#include <arm/at91/at91_pmcvar.h>
64#include <arm/at91/at91rm92reg.h>
65#include <arm/at91/at91_pio_rm9200.h>
66#include <arm/at91/at91_piovar.h>
67
68#define	MEM_RID	0
69
70/* Pin Definitions - do they belong here or somewhere else ? */
71
72#define	VBUS_MASK	AT91C_PIO_PB24
73#define	VBUS_BASE	AT91RM92_PIOB_BASE
74
75#define	PULLUP_MASK	AT91C_PIO_PB22
76#define	PULLUP_BASE	AT91RM92_PIOB_BASE
77
78static device_probe_t at91_udp_probe;
79static device_attach_t at91_udp_attach;
80static device_detach_t at91_udp_detach;
81static device_shutdown_t at91_udp_shutdown;
82
83struct at91_udp_softc {
84	struct at91dci_softc sc_dci;	/* must be first */
85	struct at91_pmc_clock *sc_iclk;
86	struct at91_pmc_clock *sc_fclk;
87	struct resource *sc_vbus_irq_res;
88	void   *sc_vbus_intr_hdl;
89};
90
91static void
92at91_vbus_poll(struct at91_udp_softc *sc)
93{
94	uint32_t temp;
95	uint8_t vbus_val;
96
97	/* XXX temporary clear interrupts here */
98
99	temp = at91_pio_gpio_clear_interrupt(VBUS_BASE);
100
101	/* just forward it */
102
103	vbus_val = at91_pio_gpio_get(VBUS_BASE, VBUS_MASK);
104	at91dci_vbus_interrupt(&sc->sc_dci, vbus_val);
105}
106
107static void
108at91_udp_clocks_on(void *arg)
109{
110	struct at91_udp_softc *sc = arg;
111
112	at91_pmc_clock_enable(sc->sc_iclk);
113	at91_pmc_clock_enable(sc->sc_fclk);
114}
115
116static void
117at91_udp_clocks_off(void *arg)
118{
119	struct at91_udp_softc *sc = arg;
120
121	at91_pmc_clock_disable(sc->sc_fclk);
122	at91_pmc_clock_disable(sc->sc_iclk);
123}
124
125static void
126at91_udp_pull_up(void *arg)
127{
128	at91_pio_gpio_set(PULLUP_BASE, PULLUP_MASK);
129}
130
131static void
132at91_udp_pull_down(void *arg)
133{
134	at91_pio_gpio_clear(PULLUP_BASE, PULLUP_MASK);
135}
136
137static int
138at91_udp_probe(device_t dev)
139{
140	device_set_desc(dev, "AT91 integrated AT91_UDP controller");
141	return (0);
142}
143
144static int
145at91_udp_attach(device_t dev)
146{
147	struct at91_udp_softc *sc = device_get_softc(dev);
148	int err;
149	int rid;
150
151	/* setup AT9100 USB device controller interface softc */
152
153	sc->sc_dci.sc_clocks_on = &at91_udp_clocks_on;
154	sc->sc_dci.sc_clocks_off = &at91_udp_clocks_off;
155	sc->sc_dci.sc_clocks_arg = sc;
156	sc->sc_dci.sc_pull_up = &at91_udp_pull_up;
157	sc->sc_dci.sc_pull_down = &at91_udp_pull_down;
158	sc->sc_dci.sc_pull_arg = sc;
159
160	/* initialise some bus fields */
161	sc->sc_dci.sc_bus.parent = dev;
162	sc->sc_dci.sc_bus.devices = sc->sc_dci.sc_devices;
163	sc->sc_dci.sc_bus.devices_max = AT91_MAX_DEVICES;
164
165	/* get all DMA memory */
166	if (usb_bus_mem_alloc_all(&sc->sc_dci.sc_bus,
167	    USB_GET_DMA_TAG(dev), NULL)) {
168		return (ENOMEM);
169	}
170	/*
171	 * configure VBUS input pin, enable deglitch and enable
172	 * interrupt :
173	 */
174	at91_pio_use_gpio(VBUS_BASE, VBUS_MASK);
175	at91_pio_gpio_input(VBUS_BASE, VBUS_MASK);
176	at91_pio_gpio_set_deglitch(VBUS_BASE, VBUS_MASK, 1);
177	at91_pio_gpio_set_interrupt(VBUS_BASE, VBUS_MASK, 1);
178
179	/*
180	 * configure PULLUP output pin :
181	 */
182	at91_pio_use_gpio(PULLUP_BASE, PULLUP_MASK);
183	at91_pio_gpio_output(PULLUP_BASE, PULLUP_MASK, 0);
184
185	at91_udp_pull_down(sc);
186
187	/* wait 10ms for pulldown to stabilise */
188	usb_pause_mtx(NULL, hz / 100);
189
190	sc->sc_iclk = at91_pmc_clock_ref("udc_clk");
191	sc->sc_fclk = at91_pmc_clock_ref("udpck");
192
193	rid = MEM_RID;
194	sc->sc_dci.sc_io_res =
195	    bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
196
197	if (!(sc->sc_dci.sc_io_res)) {
198		err = ENOMEM;
199		goto error;
200	}
201	sc->sc_dci.sc_io_tag = rman_get_bustag(sc->sc_dci.sc_io_res);
202	sc->sc_dci.sc_io_hdl = rman_get_bushandle(sc->sc_dci.sc_io_res);
203	sc->sc_dci.sc_io_size = rman_get_size(sc->sc_dci.sc_io_res);
204
205	rid = 0;
206	sc->sc_dci.sc_irq_res =
207	    bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
208	if (!(sc->sc_dci.sc_irq_res)) {
209		goto error;
210	}
211	rid = 1;
212	sc->sc_vbus_irq_res =
213	    bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
214	if (!(sc->sc_vbus_irq_res)) {
215		goto error;
216	}
217	sc->sc_dci.sc_bus.bdev = device_add_child(dev, "usbus", -1);
218	if (!(sc->sc_dci.sc_bus.bdev)) {
219		goto error;
220	}
221	device_set_ivars(sc->sc_dci.sc_bus.bdev, &sc->sc_dci.sc_bus);
222
223#if (__FreeBSD_version >= 700031)
224	err = bus_setup_intr(dev, sc->sc_dci.sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
225	    NULL, (driver_intr_t *)at91dci_interrupt, sc, &sc->sc_dci.sc_intr_hdl);
226#else
227	err = bus_setup_intr(dev, sc->sc_dci.sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
228	    (driver_intr_t *)at91dci_interrupt, sc, &sc->sc_dci.sc_intr_hdl);
229#endif
230	if (err) {
231		sc->sc_dci.sc_intr_hdl = NULL;
232		goto error;
233	}
234#if (__FreeBSD_version >= 700031)
235	err = bus_setup_intr(dev, sc->sc_vbus_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
236	    NULL, (driver_intr_t *)at91_vbus_poll, sc, &sc->sc_vbus_intr_hdl);
237#else
238	err = bus_setup_intr(dev, sc->sc_vbus_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
239	    (driver_intr_t *)at91_vbus_poll, sc, &sc->sc_vbus_intr_hdl);
240#endif
241	if (err) {
242		sc->sc_vbus_intr_hdl = NULL;
243		goto error;
244	}
245	err = at91dci_init(&sc->sc_dci);
246	if (!err) {
247		err = device_probe_and_attach(sc->sc_dci.sc_bus.bdev);
248	}
249	if (err) {
250		goto error;
251	} else {
252		/* poll VBUS one time */
253		at91_vbus_poll(sc);
254	}
255	return (0);
256
257error:
258	at91_udp_detach(dev);
259	return (ENXIO);
260}
261
262static int
263at91_udp_detach(device_t dev)
264{
265	struct at91_udp_softc *sc = device_get_softc(dev);
266	device_t bdev;
267	int err;
268
269	if (sc->sc_dci.sc_bus.bdev) {
270		bdev = sc->sc_dci.sc_bus.bdev;
271		device_detach(bdev);
272		device_delete_child(dev, bdev);
273	}
274	/* during module unload there are lots of children leftover */
275	device_delete_all_children(dev);
276
277	/* disable Transceiver */
278	AT91_UDP_WRITE_4(&sc->sc_dci, AT91_UDP_TXVC, AT91_UDP_TXVC_DIS);
279
280	/* disable and clear all interrupts */
281	AT91_UDP_WRITE_4(&sc->sc_dci, AT91_UDP_IDR, 0xFFFFFFFF);
282	AT91_UDP_WRITE_4(&sc->sc_dci, AT91_UDP_ICR, 0xFFFFFFFF);
283
284	/* disable VBUS interrupt */
285	at91_pio_gpio_set_interrupt(VBUS_BASE, VBUS_MASK, 0);
286
287	if (sc->sc_vbus_irq_res && sc->sc_vbus_intr_hdl) {
288		err = bus_teardown_intr(dev, sc->sc_vbus_irq_res,
289		    sc->sc_vbus_intr_hdl);
290		sc->sc_vbus_intr_hdl = NULL;
291	}
292	if (sc->sc_vbus_irq_res) {
293		bus_release_resource(dev, SYS_RES_IRQ, 1,
294		    sc->sc_vbus_irq_res);
295		sc->sc_vbus_irq_res = NULL;
296	}
297	if (sc->sc_dci.sc_irq_res && sc->sc_dci.sc_intr_hdl) {
298		/*
299		 * only call at91_udp_uninit() after at91_udp_init()
300		 */
301		at91dci_uninit(&sc->sc_dci);
302
303		err = bus_teardown_intr(dev, sc->sc_dci.sc_irq_res,
304		    sc->sc_dci.sc_intr_hdl);
305		sc->sc_dci.sc_intr_hdl = NULL;
306	}
307	if (sc->sc_dci.sc_irq_res) {
308		bus_release_resource(dev, SYS_RES_IRQ, 0,
309		    sc->sc_dci.sc_irq_res);
310		sc->sc_dci.sc_irq_res = NULL;
311	}
312	if (sc->sc_dci.sc_io_res) {
313		bus_release_resource(dev, SYS_RES_MEMORY, MEM_RID,
314		    sc->sc_dci.sc_io_res);
315		sc->sc_dci.sc_io_res = NULL;
316	}
317	usb_bus_mem_free_all(&sc->sc_dci.sc_bus, NULL);
318
319	/* disable clocks */
320	at91_pmc_clock_disable(sc->sc_iclk);
321	at91_pmc_clock_disable(sc->sc_fclk);
322	at91_pmc_clock_deref(sc->sc_fclk);
323	at91_pmc_clock_deref(sc->sc_iclk);
324
325	return (0);
326}
327
328static int
329at91_udp_shutdown(device_t dev)
330{
331	struct at91_udp_softc *sc = device_get_softc(dev);
332	int err;
333
334	err = bus_generic_shutdown(dev);
335	if (err)
336		return (err);
337
338	at91dci_uninit(&sc->sc_dci);
339
340	return (0);
341}
342
343static device_method_t at91_udp_methods[] = {
344	/* Device interface */
345	DEVMETHOD(device_probe, at91_udp_probe),
346	DEVMETHOD(device_attach, at91_udp_attach),
347	DEVMETHOD(device_detach, at91_udp_detach),
348	DEVMETHOD(device_shutdown, at91_udp_shutdown),
349
350	/* Bus interface */
351	DEVMETHOD(bus_print_child, bus_generic_print_child),
352
353	{0, 0}
354};
355
356static driver_t at91_udp_driver = {
357	"at91_udp",
358	at91_udp_methods,
359	sizeof(struct at91_udp_softc),
360};
361
362static devclass_t at91_udp_devclass;
363
364DRIVER_MODULE(at91_udp, atmelarm, at91_udp_driver, at91_udp_devclass, 0, 0);
365