source: rtems-libbsd/freebsd/sys/dev/usb/controller/saf1761_otg_fdt.c @ f9d4e1d

55-freebsd-126-freebsd-12
Last change on this file since f9d4e1d was 5892207, checked in by Christian Mauderer <Christian.Mauderer@…>, on 11/07/17 at 14:15:38

saf1761_otg: Port to RTEMS.

  • Property mode set to 100644
File size: 8.4 KB
Line 
1#include <machine/rtems-bsd-kernel-space.h>
2
3/* $FreeBSD$ */
4/*-
5 * Copyright (c) 2014 Hans Petter Selasky <hselasky@FreeBSD.org>
6 * All rights reserved.
7 *
8 * This software was developed by SRI International and the University of
9 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
10 * ("CTSRD"), as part of the DARPA CRASH research programme.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifdef USB_GLOBAL_INCLUDE_FILE
35#include USB_GLOBAL_INCLUDE_FILE
36#else
37#include <sys/stdint.h>
38#include <sys/stddef.h>
39#include <sys/param.h>
40#include <sys/queue.h>
41#include <sys/types.h>
42#include <sys/systm.h>
43#include <sys/kernel.h>
44#include <sys/bus.h>
45#include <sys/module.h>
46#include <sys/lock.h>
47#include <sys/mutex.h>
48#include <sys/condvar.h>
49#include <sys/sysctl.h>
50#include <sys/sx.h>
51#include <rtems/bsd/sys/unistd.h>
52#include <sys/callout.h>
53#include <sys/malloc.h>
54#include <sys/priv.h>
55#include <sys/rman.h>
56
57#include <dev/fdt/fdt_common.h>
58
59#include <dev/ofw/openfirm.h>
60#include <dev/ofw/ofw_bus.h>
61#include <dev/ofw/ofw_bus_subr.h>
62
63#include <dev/usb/usb.h>
64#include <dev/usb/usbdi.h>
65
66#include <dev/usb/usb_core.h>
67#include <dev/usb/usb_busdma.h>
68#include <dev/usb/usb_process.h>
69#include <dev/usb/usb_transfer.h>
70#include <dev/usb/usb_device.h>
71#include <dev/usb/usb_hub.h>
72#include <dev/usb/usb_util.h>
73
74#include <dev/usb/usb_controller.h>
75#include <dev/usb/usb_bus.h>
76#endif                                  /* USB_GLOBAL_INCLUDE_FILE */
77
78#include <dev/usb/controller/saf1761_otg.h>
79#include <dev/usb/controller/saf1761_otg_reg.h>
80#ifdef __rtems__
81#include <rtems.h>
82#include <bsp.h>
83#ifdef LIBBSP_ARM_ATSAM_BSP_H
84#include <bsp/pin-config.h>
85#endif /* LIBBSP_ARM_ATSAM_BSP_H */
86#endif /* __rtems__ */
87
88static device_probe_t saf1761_otg_fdt_probe;
89static device_attach_t saf1761_otg_fdt_attach;
90static device_detach_t saf1761_otg_fdt_detach;
91
92static device_method_t saf1761_otg_methods[] = {
93        /* Device interface */
94        DEVMETHOD(device_probe, saf1761_otg_fdt_probe),
95        DEVMETHOD(device_attach, saf1761_otg_fdt_attach),
96        DEVMETHOD(device_detach, saf1761_otg_fdt_detach),
97        DEVMETHOD(device_suspend, bus_generic_suspend),
98        DEVMETHOD(device_resume, bus_generic_resume),
99        DEVMETHOD(device_shutdown, bus_generic_shutdown),
100
101        DEVMETHOD_END
102};
103
104static driver_t saf1761_otg_driver = {
105        .name = "saf1761otg",
106        .methods = saf1761_otg_methods,
107        .size = sizeof(struct saf1761_otg_softc),
108};
109
110static devclass_t saf1761_otg_devclass;
111
112#ifndef __rtems__
113DRIVER_MODULE(saf1761otg, simplebus, saf1761_otg_driver, saf1761_otg_devclass, 0, 0);
114#else /* __rtems__ */
115DRIVER_MODULE(saf1761otg, nexus, saf1761_otg_driver, saf1761_otg_devclass, 0, 0);
116#endif /* __rtems__ */
117MODULE_DEPEND(saf1761otg, usb, 1, 1, 1);
118
119static int
120saf1761_otg_fdt_probe(device_t dev)
121{
122#ifndef __rtems__
123        if (!ofw_bus_status_okay(dev))
124                return (ENXIO);
125
126        if (!ofw_bus_is_compatible(dev, "nxp,usb-isp1761"))
127                return (ENXIO);
128#else /* __rtems__ */
129        if (device_get_unit(dev) != 0)
130                return (ENXIO);
131#endif /* __rtems__ */
132
133        device_set_desc(dev, "ISP1761/SAF1761 DCI USB 2.0 Device Controller");
134
135        return (0);
136}
137
138static int
139saf1761_otg_fdt_attach(device_t dev)
140{
141        struct saf1761_otg_softc *sc = device_get_softc(dev);
142        char param[24];
143        int err;
144        int rid;
145
146#ifndef __rtems__
147        /* get configuration from FDT */
148
149        /* get bus-width, if any */
150        if (OF_getprop(ofw_bus_get_node(dev), "bus-width",
151            &param, sizeof(param)) > 0) {
152                param[sizeof(param) - 1] = 0;
153                if (strcmp(param, "32") == 0)
154                        sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DATA_BUS_WIDTH;
155        } else {
156                /* assume 32-bit data bus */
157                sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DATA_BUS_WIDTH;
158        }
159
160        /* get analog over-current setting */
161        if (OF_getprop(ofw_bus_get_node(dev), "analog-oc",
162            &param, sizeof(param)) > 0) {
163                sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_ANA_DIGI_OC;
164        }
165
166        /* get DACK polarity */
167        if (OF_getprop(ofw_bus_get_node(dev), "dack-polarity",
168            &param, sizeof(param)) > 0) {
169                sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DACK_POL;
170        }
171
172        /* get DREQ polarity */
173        if (OF_getprop(ofw_bus_get_node(dev), "dreq-polarity",
174            &param, sizeof(param)) > 0) {
175                sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DREQ_POL;
176        }
177
178        /* get IRQ polarity */
179        if (OF_getprop(ofw_bus_get_node(dev), "int-polarity",
180            &param, sizeof(param)) > 0) {
181                sc->sc_interrupt_cfg |= SOTG_INTERRUPT_CFG_INTPOL;
182                sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_INTR_POL;
183        }
184
185        /* get IRQ level triggering */
186        if (OF_getprop(ofw_bus_get_node(dev), "int-level",
187            &param, sizeof(param)) > 0) {
188                sc->sc_interrupt_cfg |= SOTG_INTERRUPT_CFG_INTLVL;
189                sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_INTR_LEVEL;
190        }
191#else /* __rtems__ */
192        /* set 16-bit data bus */
193        sc->sc_hw_mode = 0;
194#endif /* __rtems__ */
195
196        /* initialise some bus fields */
197        sc->sc_bus.parent = dev;
198        sc->sc_bus.devices = sc->sc_devices;
199        sc->sc_bus.devices_max = SOTG_MAX_DEVICES;
200        sc->sc_bus.dma_bits = 32;
201
202        /* get all DMA memory */
203        if (usb_bus_mem_alloc_all(&sc->sc_bus,
204            USB_GET_DMA_TAG(dev), NULL)) {
205                return (ENOMEM);
206        }
207        rid = 0;
208        sc->sc_io_res =
209            bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
210
211        if (sc->sc_io_res == NULL)
212                goto error;
213
214        sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
215        sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
216        sc->sc_io_size = rman_get_size(sc->sc_io_res);
217
218        /* try to allocate the HC interrupt first */
219        rid = 1;
220        sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
221            RF_SHAREABLE | RF_ACTIVE);
222        if (sc->sc_irq_res == NULL) {
223                /* try to allocate a common IRQ second */
224                rid = 0;
225                sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
226                    RF_SHAREABLE | RF_ACTIVE);
227                if (sc->sc_irq_res == NULL)
228                        goto error;
229        }
230
231        sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
232        if (sc->sc_bus.bdev == NULL)
233                goto error;
234
235        device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
236
237#ifdef __rtems__
238#ifdef LIBBSP_ARM_ATSAM_BSP_H
239        const Pin saf_irq = {PIO_PC16, PIOC, ID_PIOC, PIO_INPUT, PIO_PULLUP | PIO_IT_LOW_LEVEL};
240        /* Activate pin interrupt. Add a default handler that just clears the
241         * status. */
242        PIO_Configure(&saf_irq, 1);
243        PIO_EnableIt(&saf_irq);
244#endif /* LIBBSP_ARM_ATSAM_BSP_H */
245#endif /* __rtems__ */
246        err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_TTY | INTR_MPSAFE,
247            &saf1761_otg_filter_interrupt, &saf1761_otg_interrupt, sc, &sc->sc_intr_hdl);
248        if (err) {
249                sc->sc_intr_hdl = NULL;
250                goto error;
251        }
252        err = saf1761_otg_init(sc);
253        if (err) {
254                device_printf(dev, "Init failed\n");
255                goto error;
256        }
257        err = device_probe_and_attach(sc->sc_bus.bdev);
258        if (err) {
259                device_printf(dev, "USB probe and attach failed\n");
260                goto error;
261        }
262        return (0);
263
264error:
265        saf1761_otg_fdt_detach(dev);
266        return (ENXIO);
267}
268
269static int
270saf1761_otg_fdt_detach(device_t dev)
271{
272        struct saf1761_otg_softc *sc = device_get_softc(dev);
273        int err;
274
275        /* during module unload there are lots of children leftover */
276        device_delete_children(dev);
277
278        if (sc->sc_irq_res && sc->sc_intr_hdl) {
279                /*
280                 * Only call uninit() after init()
281                 */
282                saf1761_otg_uninit(sc);
283
284                err = bus_teardown_intr(dev, sc->sc_irq_res,
285                    sc->sc_intr_hdl);
286                sc->sc_intr_hdl = NULL;
287        }
288        if (sc->sc_irq_res) {
289                bus_release_resource(dev, SYS_RES_IRQ, 0,
290                    sc->sc_irq_res);
291                sc->sc_irq_res = NULL;
292        }
293        if (sc->sc_io_res) {
294                bus_release_resource(dev, SYS_RES_MEMORY, 0,
295                    sc->sc_io_res);
296                sc->sc_io_res = NULL;
297        }
298        usb_bus_mem_free_all(&sc->sc_bus, NULL);
299
300        return (0);
301}
Note: See TracBrowser for help on using the repository browser.