]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/usb/controller/at91dci_atmelarm.c
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.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_pioreg.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 ? -- YES! */
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
81 struct at91_udp_softc {
82         struct at91dci_softc sc_dci;    /* must be first */
83         struct at91_pmc_clock *sc_mclk;
84         struct at91_pmc_clock *sc_iclk;
85         struct at91_pmc_clock *sc_fclk;
86         struct callout sc_vbus;
87 };
88
89 static void
90 at91_vbus_poll(struct at91_udp_softc *sc)
91 {
92         uint8_t vbus_val;
93
94         vbus_val = at91_pio_gpio_get(VBUS_BASE, VBUS_MASK) != 0;
95         at91dci_vbus_interrupt(&sc->sc_dci, vbus_val);
96
97         callout_reset(&sc->sc_vbus, hz, (void *)&at91_vbus_poll, sc);
98 }
99
100 static void
101 at91_udp_clocks_on(void *arg)
102 {
103         struct at91_udp_softc *sc = arg;
104
105         at91_pmc_clock_enable(sc->sc_mclk);
106         at91_pmc_clock_enable(sc->sc_iclk);
107         at91_pmc_clock_enable(sc->sc_fclk);
108 }
109
110 static void
111 at91_udp_clocks_off(void *arg)
112 {
113         struct at91_udp_softc *sc = arg;
114
115         at91_pmc_clock_disable(sc->sc_fclk);
116         at91_pmc_clock_disable(sc->sc_iclk);
117         at91_pmc_clock_disable(sc->sc_mclk);
118 }
119
120 static void
121 at91_udp_pull_up(void *arg)
122 {
123         at91_pio_gpio_set(PULLUP_BASE, PULLUP_MASK);
124 }
125
126 static void
127 at91_udp_pull_down(void *arg)
128 {
129         at91_pio_gpio_clear(PULLUP_BASE, PULLUP_MASK);
130 }
131
132 static int
133 at91_udp_probe(device_t dev)
134 {
135         device_set_desc(dev, "AT91 integrated AT91_UDP controller");
136         return (0);
137 }
138
139 static int
140 at91_udp_attach(device_t dev)
141 {
142         struct at91_udp_softc *sc = device_get_softc(dev);
143         int err;
144         int rid;
145
146         /* setup AT9100 USB device controller interface softc */
147
148         sc->sc_dci.sc_clocks_on = &at91_udp_clocks_on;
149         sc->sc_dci.sc_clocks_off = &at91_udp_clocks_off;
150         sc->sc_dci.sc_clocks_arg = sc;
151         sc->sc_dci.sc_pull_up = &at91_udp_pull_up;
152         sc->sc_dci.sc_pull_down = &at91_udp_pull_down;
153         sc->sc_dci.sc_pull_arg = sc;
154
155         /* initialise some bus fields */
156         sc->sc_dci.sc_bus.parent = dev;
157         sc->sc_dci.sc_bus.devices = sc->sc_dci.sc_devices;
158         sc->sc_dci.sc_bus.devices_max = AT91_MAX_DEVICES;
159
160         /* get all DMA memory */
161         if (usb_bus_mem_alloc_all(&sc->sc_dci.sc_bus,
162             USB_GET_DMA_TAG(dev), NULL)) {
163                 return (ENOMEM);
164         }
165         callout_init_mtx(&sc->sc_vbus, &sc->sc_dci.sc_bus.bus_mtx, 0);
166
167         /*
168          * configure VBUS input pin, enable deglitch and enable
169          * interrupt :
170          */
171         at91_pio_use_gpio(VBUS_BASE, VBUS_MASK);
172         at91_pio_gpio_input(VBUS_BASE, VBUS_MASK);
173         at91_pio_gpio_set_deglitch(VBUS_BASE, VBUS_MASK, 1);
174         at91_pio_gpio_set_interrupt(VBUS_BASE, VBUS_MASK, 0);
175
176         /*
177          * configure PULLUP output pin :
178          */
179         at91_pio_use_gpio(PULLUP_BASE, PULLUP_MASK);
180         at91_pio_gpio_output(PULLUP_BASE, PULLUP_MASK, 0);
181
182         at91_udp_pull_down(sc);
183
184         /* wait 10ms for pulldown to stabilise */
185         usb_pause_mtx(NULL, hz / 100);
186
187         sc->sc_mclk = at91_pmc_clock_ref("mck");
188         sc->sc_iclk = at91_pmc_clock_ref("udc_clk");
189         sc->sc_fclk = at91_pmc_clock_ref("udpck");
190
191         rid = MEM_RID;
192         sc->sc_dci.sc_io_res =
193             bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
194
195         if (!(sc->sc_dci.sc_io_res)) {
196                 err = ENOMEM;
197                 goto error;
198         }
199         sc->sc_dci.sc_io_tag = rman_get_bustag(sc->sc_dci.sc_io_res);
200         sc->sc_dci.sc_io_hdl = rman_get_bushandle(sc->sc_dci.sc_io_res);
201         sc->sc_dci.sc_io_size = rman_get_size(sc->sc_dci.sc_io_res);
202
203         rid = 0;
204         sc->sc_dci.sc_irq_res =
205             bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
206         if (!(sc->sc_dci.sc_irq_res)) {
207                 goto error;
208         }
209         sc->sc_dci.sc_bus.bdev = device_add_child(dev, "usbus", -1);
210         if (!(sc->sc_dci.sc_bus.bdev)) {
211                 goto error;
212         }
213         device_set_ivars(sc->sc_dci.sc_bus.bdev, &sc->sc_dci.sc_bus);
214
215 #if (__FreeBSD_version >= 700031)
216         err = bus_setup_intr(dev, sc->sc_dci.sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
217             NULL, (driver_intr_t *)at91dci_interrupt, sc, &sc->sc_dci.sc_intr_hdl);
218 #else
219         err = bus_setup_intr(dev, sc->sc_dci.sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
220             (driver_intr_t *)at91dci_interrupt, sc, &sc->sc_dci.sc_intr_hdl);
221 #endif
222         if (err) {
223                 sc->sc_dci.sc_intr_hdl = NULL;
224                 goto error;
225         }
226
227         err = at91dci_init(&sc->sc_dci);
228         if (!err) {
229                 err = device_probe_and_attach(sc->sc_dci.sc_bus.bdev);
230         }
231         if (err) {
232                 goto error;
233         } else {
234                 /* poll VBUS one time */
235                 USB_BUS_LOCK(&sc->sc_dci.sc_bus);
236                 at91_vbus_poll(sc);
237                 USB_BUS_UNLOCK(&sc->sc_dci.sc_bus);
238         }
239         return (0);
240
241 error:
242         at91_udp_detach(dev);
243         return (ENXIO);
244 }
245
246 static int
247 at91_udp_detach(device_t dev)
248 {
249         struct at91_udp_softc *sc = device_get_softc(dev);
250         device_t bdev;
251         int err;
252
253         if (sc->sc_dci.sc_bus.bdev) {
254                 bdev = sc->sc_dci.sc_bus.bdev;
255                 device_detach(bdev);
256                 device_delete_child(dev, bdev);
257         }
258         /* during module unload there are lots of children leftover */
259         device_delete_children(dev);
260
261         USB_BUS_LOCK(&sc->sc_dci.sc_bus);
262         callout_stop(&sc->sc_vbus);
263         USB_BUS_UNLOCK(&sc->sc_dci.sc_bus);
264
265         callout_drain(&sc->sc_vbus);
266
267         /* disable Transceiver */
268         AT91_UDP_WRITE_4(&sc->sc_dci, AT91_UDP_TXVC, AT91_UDP_TXVC_DIS);
269
270         /* disable and clear all interrupts */
271         AT91_UDP_WRITE_4(&sc->sc_dci, AT91_UDP_IDR, 0xFFFFFFFF);
272         AT91_UDP_WRITE_4(&sc->sc_dci, AT91_UDP_ICR, 0xFFFFFFFF);
273
274         if (sc->sc_dci.sc_irq_res && sc->sc_dci.sc_intr_hdl) {
275                 /*
276                  * only call at91_udp_uninit() after at91_udp_init()
277                  */
278                 at91dci_uninit(&sc->sc_dci);
279
280                 err = bus_teardown_intr(dev, sc->sc_dci.sc_irq_res,
281                     sc->sc_dci.sc_intr_hdl);
282                 sc->sc_dci.sc_intr_hdl = NULL;
283         }
284         if (sc->sc_dci.sc_irq_res) {
285                 bus_release_resource(dev, SYS_RES_IRQ, 0,
286                     sc->sc_dci.sc_irq_res);
287                 sc->sc_dci.sc_irq_res = NULL;
288         }
289         if (sc->sc_dci.sc_io_res) {
290                 bus_release_resource(dev, SYS_RES_MEMORY, MEM_RID,
291                     sc->sc_dci.sc_io_res);
292                 sc->sc_dci.sc_io_res = NULL;
293         }
294         usb_bus_mem_free_all(&sc->sc_dci.sc_bus, NULL);
295
296         /* disable clocks */
297         at91_pmc_clock_disable(sc->sc_iclk);
298         at91_pmc_clock_disable(sc->sc_fclk);
299         at91_pmc_clock_disable(sc->sc_mclk);
300         at91_pmc_clock_deref(sc->sc_fclk);
301         at91_pmc_clock_deref(sc->sc_iclk);
302         at91_pmc_clock_deref(sc->sc_mclk);
303
304         return (0);
305 }
306
307 static device_method_t at91_udp_methods[] = {
308         /* Device interface */
309         DEVMETHOD(device_probe, at91_udp_probe),
310         DEVMETHOD(device_attach, at91_udp_attach),
311         DEVMETHOD(device_detach, at91_udp_detach),
312         DEVMETHOD(device_suspend, bus_generic_suspend),
313         DEVMETHOD(device_resume, bus_generic_resume),
314         DEVMETHOD(device_shutdown, bus_generic_shutdown),
315
316         DEVMETHOD_END
317 };
318
319 static driver_t at91_udp_driver = {
320         .name = "at91_udp",
321         .methods = at91_udp_methods,
322         .size = sizeof(struct at91_udp_softc),
323 };
324
325 static devclass_t at91_udp_devclass;
326
327 DRIVER_MODULE(at91_udp, atmelarm, at91_udp_driver, at91_udp_devclass, 0, 0);