]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/dev/usb/controller/at91dci_atmelarm.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / dev / usb / controller / at91dci_atmelarm.c
1 #include <sys/cdefs.h>
2 __FBSDID("$FreeBSD$");
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/module.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/condvar.h>
41 #include <sys/sysctl.h>
42 #include <sys/sx.h>
43 #include <sys/unistd.h>
44 #include <sys/callout.h>
45 #include <sys/malloc.h>
46 #include <sys/priv.h>
47
48 #include <dev/usb/usb.h>
49 #include <dev/usb/usbdi.h>
50
51 #include <dev/usb/usb_core.h>
52 #include <dev/usb/usb_busdma.h>
53 #include <dev/usb/usb_process.h>
54 #include <dev/usb/usb_util.h>
55
56 #include <dev/usb/usb_controller.h>
57 #include <dev/usb/usb_bus.h>
58 #include <dev/usb/controller/at91dci.h>
59
60 #include <sys/rman.h>
61
62 #include <arm/at91/at91_pmcvar.h>
63 #include <arm/at91/at91rm92reg.h>
64 #include <arm/at91/at91_pio_rm9200.h>
65 #include <arm/at91/at91_piovar.h>
66
67 #define MEM_RID 0
68
69 /* Pin Definitions - do they belong here or somewhere else ? */
70
71 #define VBUS_MASK       AT91C_PIO_PB24
72 #define VBUS_BASE       AT91RM92_PIOB_BASE
73
74 #define PULLUP_MASK     AT91C_PIO_PB22
75 #define PULLUP_BASE     AT91RM92_PIOB_BASE
76
77 static device_probe_t at91_udp_probe;
78 static device_attach_t at91_udp_attach;
79 static device_detach_t at91_udp_detach;
80 static device_shutdown_t at91_udp_shutdown;
81
82 struct at91_udp_softc {
83         struct at91dci_softc sc_dci;    /* must be first */
84         struct at91_pmc_clock *sc_iclk;
85         struct at91_pmc_clock *sc_fclk;
86         struct resource *sc_vbus_irq_res;
87         void   *sc_vbus_intr_hdl;
88 };
89
90 static void
91 at91_vbus_poll(struct at91_udp_softc *sc)
92 {
93         uint32_t temp;
94         uint8_t vbus_val;
95
96         /* XXX temporary clear interrupts here */
97
98         temp = at91_pio_gpio_clear_interrupt(VBUS_BASE);
99
100         /* just forward it */
101
102         vbus_val = at91_pio_gpio_get(VBUS_BASE, VBUS_MASK);
103         at91dci_vbus_interrupt(&sc->sc_dci, vbus_val);
104 }
105
106 static void
107 at91_udp_clocks_on(void *arg)
108 {
109         struct at91_udp_softc *sc = arg;
110
111         at91_pmc_clock_enable(sc->sc_iclk);
112         at91_pmc_clock_enable(sc->sc_fclk);
113 }
114
115 static void
116 at91_udp_clocks_off(void *arg)
117 {
118         struct at91_udp_softc *sc = arg;
119
120         at91_pmc_clock_disable(sc->sc_fclk);
121         at91_pmc_clock_disable(sc->sc_iclk);
122 }
123
124 static void
125 at91_udp_pull_up(void *arg)
126 {
127         at91_pio_gpio_set(PULLUP_BASE, PULLUP_MASK);
128 }
129
130 static void
131 at91_udp_pull_down(void *arg)
132 {
133         at91_pio_gpio_clear(PULLUP_BASE, PULLUP_MASK);
134 }
135
136 static int
137 at91_udp_probe(device_t dev)
138 {
139         device_set_desc(dev, "AT91 integrated AT91_UDP controller");
140         return (0);
141 }
142
143 static int
144 at91_udp_attach(device_t dev)
145 {
146         struct at91_udp_softc *sc = device_get_softc(dev);
147         int err;
148         int rid;
149
150         /* setup AT9100 USB device controller interface softc */
151
152         sc->sc_dci.sc_clocks_on = &at91_udp_clocks_on;
153         sc->sc_dci.sc_clocks_off = &at91_udp_clocks_off;
154         sc->sc_dci.sc_clocks_arg = sc;
155         sc->sc_dci.sc_pull_up = &at91_udp_pull_up;
156         sc->sc_dci.sc_pull_down = &at91_udp_pull_down;
157         sc->sc_dci.sc_pull_arg = sc;
158
159         /* initialise some bus fields */
160         sc->sc_dci.sc_bus.parent = dev;
161         sc->sc_dci.sc_bus.devices = sc->sc_dci.sc_devices;
162         sc->sc_dci.sc_bus.devices_max = AT91_MAX_DEVICES;
163
164         /* get all DMA memory */
165         if (usb_bus_mem_alloc_all(&sc->sc_dci.sc_bus,
166             USB_GET_DMA_TAG(dev), NULL)) {
167                 return (ENOMEM);
168         }
169         /*
170          * configure VBUS input pin, enable deglitch and enable
171          * interrupt :
172          */
173         at91_pio_use_gpio(VBUS_BASE, VBUS_MASK);
174         at91_pio_gpio_input(VBUS_BASE, VBUS_MASK);
175         at91_pio_gpio_set_deglitch(VBUS_BASE, VBUS_MASK, 1);
176         at91_pio_gpio_set_interrupt(VBUS_BASE, VBUS_MASK, 1);
177
178         /*
179          * configure PULLUP output pin :
180          */
181         at91_pio_use_gpio(PULLUP_BASE, PULLUP_MASK);
182         at91_pio_gpio_output(PULLUP_BASE, PULLUP_MASK, 0);
183
184         at91_udp_pull_down(sc);
185
186         /* wait 10ms for pulldown to stabilise */
187         usb_pause_mtx(NULL, hz / 100);
188
189         sc->sc_iclk = at91_pmc_clock_ref("udc_clk");
190         sc->sc_fclk = at91_pmc_clock_ref("udpck");
191
192         rid = MEM_RID;
193         sc->sc_dci.sc_io_res =
194             bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
195
196         if (!(sc->sc_dci.sc_io_res)) {
197                 err = ENOMEM;
198                 goto error;
199         }
200         sc->sc_dci.sc_io_tag = rman_get_bustag(sc->sc_dci.sc_io_res);
201         sc->sc_dci.sc_io_hdl = rman_get_bushandle(sc->sc_dci.sc_io_res);
202         sc->sc_dci.sc_io_size = rman_get_size(sc->sc_dci.sc_io_res);
203
204         rid = 0;
205         sc->sc_dci.sc_irq_res =
206             bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
207         if (!(sc->sc_dci.sc_irq_res)) {
208                 goto error;
209         }
210         rid = 1;
211         sc->sc_vbus_irq_res =
212             bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
213         if (!(sc->sc_vbus_irq_res)) {
214                 goto error;
215         }
216         sc->sc_dci.sc_bus.bdev = device_add_child(dev, "usbus", -1);
217         if (!(sc->sc_dci.sc_bus.bdev)) {
218                 goto error;
219         }
220         device_set_ivars(sc->sc_dci.sc_bus.bdev, &sc->sc_dci.sc_bus);
221
222 #if (__FreeBSD_version >= 700031)
223         err = bus_setup_intr(dev, sc->sc_dci.sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
224             NULL, (driver_intr_t *)at91dci_interrupt, sc, &sc->sc_dci.sc_intr_hdl);
225 #else
226         err = bus_setup_intr(dev, sc->sc_dci.sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
227             (driver_intr_t *)at91dci_interrupt, sc, &sc->sc_dci.sc_intr_hdl);
228 #endif
229         if (err) {
230                 sc->sc_dci.sc_intr_hdl = NULL;
231                 goto error;
232         }
233 #if (__FreeBSD_version >= 700031)
234         err = bus_setup_intr(dev, sc->sc_vbus_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
235             NULL, (driver_intr_t *)at91_vbus_poll, sc, &sc->sc_vbus_intr_hdl);
236 #else
237         err = bus_setup_intr(dev, sc->sc_vbus_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
238             (driver_intr_t *)at91_vbus_poll, sc, &sc->sc_vbus_intr_hdl);
239 #endif
240         if (err) {
241                 sc->sc_vbus_intr_hdl = NULL;
242                 goto error;
243         }
244         err = at91dci_init(&sc->sc_dci);
245         if (!err) {
246                 err = device_probe_and_attach(sc->sc_dci.sc_bus.bdev);
247         }
248         if (err) {
249                 goto error;
250         } else {
251                 /* poll VBUS one time */
252                 at91_vbus_poll(sc);
253         }
254         return (0);
255
256 error:
257         at91_udp_detach(dev);
258         return (ENXIO);
259 }
260
261 static int
262 at91_udp_detach(device_t dev)
263 {
264         struct at91_udp_softc *sc = device_get_softc(dev);
265         device_t bdev;
266         int err;
267
268         if (sc->sc_dci.sc_bus.bdev) {
269                 bdev = sc->sc_dci.sc_bus.bdev;
270                 device_detach(bdev);
271                 device_delete_child(dev, bdev);
272         }
273         /* during module unload there are lots of children leftover */
274         device_delete_all_children(dev);
275
276         /* disable Transceiver */
277         AT91_UDP_WRITE_4(&sc->sc_dci, AT91_UDP_TXVC, AT91_UDP_TXVC_DIS);
278
279         /* disable and clear all interrupts */
280         AT91_UDP_WRITE_4(&sc->sc_dci, AT91_UDP_IDR, 0xFFFFFFFF);
281         AT91_UDP_WRITE_4(&sc->sc_dci, AT91_UDP_ICR, 0xFFFFFFFF);
282
283         /* disable VBUS interrupt */
284         at91_pio_gpio_set_interrupt(VBUS_BASE, VBUS_MASK, 0);
285
286         if (sc->sc_vbus_irq_res && sc->sc_vbus_intr_hdl) {
287                 err = bus_teardown_intr(dev, sc->sc_vbus_irq_res,
288                     sc->sc_vbus_intr_hdl);
289                 sc->sc_vbus_intr_hdl = NULL;
290         }
291         if (sc->sc_vbus_irq_res) {
292                 bus_release_resource(dev, SYS_RES_IRQ, 1,
293                     sc->sc_vbus_irq_res);
294                 sc->sc_vbus_irq_res = NULL;
295         }
296         if (sc->sc_dci.sc_irq_res && sc->sc_dci.sc_intr_hdl) {
297                 /*
298                  * only call at91_udp_uninit() after at91_udp_init()
299                  */
300                 at91dci_uninit(&sc->sc_dci);
301
302                 err = bus_teardown_intr(dev, sc->sc_dci.sc_irq_res,
303                     sc->sc_dci.sc_intr_hdl);
304                 sc->sc_dci.sc_intr_hdl = NULL;
305         }
306         if (sc->sc_dci.sc_irq_res) {
307                 bus_release_resource(dev, SYS_RES_IRQ, 0,
308                     sc->sc_dci.sc_irq_res);
309                 sc->sc_dci.sc_irq_res = NULL;
310         }
311         if (sc->sc_dci.sc_io_res) {
312                 bus_release_resource(dev, SYS_RES_MEMORY, MEM_RID,
313                     sc->sc_dci.sc_io_res);
314                 sc->sc_dci.sc_io_res = NULL;
315         }
316         usb_bus_mem_free_all(&sc->sc_dci.sc_bus, NULL);
317
318         /* disable clocks */
319         at91_pmc_clock_disable(sc->sc_iclk);
320         at91_pmc_clock_disable(sc->sc_fclk);
321         at91_pmc_clock_deref(sc->sc_fclk);
322         at91_pmc_clock_deref(sc->sc_iclk);
323
324         return (0);
325 }
326
327 static int
328 at91_udp_shutdown(device_t dev)
329 {
330         struct at91_udp_softc *sc = device_get_softc(dev);
331         int err;
332
333         err = bus_generic_shutdown(dev);
334         if (err)
335                 return (err);
336
337         at91dci_uninit(&sc->sc_dci);
338
339         return (0);
340 }
341
342 static device_method_t at91_udp_methods[] = {
343         /* Device interface */
344         DEVMETHOD(device_probe, at91_udp_probe),
345         DEVMETHOD(device_attach, at91_udp_attach),
346         DEVMETHOD(device_detach, at91_udp_detach),
347         DEVMETHOD(device_shutdown, at91_udp_shutdown),
348
349         /* Bus interface */
350         DEVMETHOD(bus_print_child, bus_generic_print_child),
351
352         {0, 0}
353 };
354
355 static driver_t at91_udp_driver = {
356         "at91_udp",
357         at91_udp_methods,
358         sizeof(struct at91_udp_softc),
359 };
360
361 static devclass_t at91_udp_devclass;
362
363 DRIVER_MODULE(at91_udp, atmelarm, at91_udp_driver, at91_udp_devclass, 0, 0);