[cf8e1d4] | 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> |
---|
[5892207] | 39 | #include <sys/param.h> |
---|
[cf8e1d4] | 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> |
---|
[5892207] | 46 | #include <sys/lock.h> |
---|
[cf8e1d4] | 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> |
---|
[5892207] | 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__ */ |
---|
[cf8e1d4] | 87 | |
---|
| 88 | static device_probe_t saf1761_otg_fdt_probe; |
---|
| 89 | static device_attach_t saf1761_otg_fdt_attach; |
---|
| 90 | static device_detach_t saf1761_otg_fdt_detach; |
---|
| 91 | |
---|
| 92 | static 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 | |
---|
| 104 | static driver_t saf1761_otg_driver = { |
---|
| 105 | .name = "saf1761otg", |
---|
| 106 | .methods = saf1761_otg_methods, |
---|
| 107 | .size = sizeof(struct saf1761_otg_softc), |
---|
| 108 | }; |
---|
| 109 | |
---|
| 110 | static devclass_t saf1761_otg_devclass; |
---|
| 111 | |
---|
[5892207] | 112 | #ifndef __rtems__ |
---|
[cf8e1d4] | 113 | DRIVER_MODULE(saf1761otg, simplebus, saf1761_otg_driver, saf1761_otg_devclass, 0, 0); |
---|
[5892207] | 114 | #else /* __rtems__ */ |
---|
| 115 | DRIVER_MODULE(saf1761otg, nexus, saf1761_otg_driver, saf1761_otg_devclass, 0, 0); |
---|
| 116 | #endif /* __rtems__ */ |
---|
[cf8e1d4] | 117 | MODULE_DEPEND(saf1761otg, usb, 1, 1, 1); |
---|
| 118 | |
---|
| 119 | static int |
---|
| 120 | saf1761_otg_fdt_probe(device_t dev) |
---|
| 121 | { |
---|
[5892207] | 122 | #ifndef __rtems__ |
---|
[cf8e1d4] | 123 | if (!ofw_bus_status_okay(dev)) |
---|
| 124 | return (ENXIO); |
---|
| 125 | |
---|
| 126 | if (!ofw_bus_is_compatible(dev, "nxp,usb-isp1761")) |
---|
| 127 | return (ENXIO); |
---|
[5892207] | 128 | #else /* __rtems__ */ |
---|
| 129 | if (device_get_unit(dev) != 0) |
---|
| 130 | return (ENXIO); |
---|
| 131 | #endif /* __rtems__ */ |
---|
[cf8e1d4] | 132 | |
---|
| 133 | device_set_desc(dev, "ISP1761/SAF1761 DCI USB 2.0 Device Controller"); |
---|
| 134 | |
---|
| 135 | return (0); |
---|
| 136 | } |
---|
| 137 | |
---|
[1e77a45] | 138 | #ifdef __rtems__ |
---|
| 139 | static void |
---|
| 140 | saf1761_otg_filter_interrupt_wrapper(void *arg) |
---|
| 141 | { |
---|
| 142 | struct saf1761_otg_softc *sc = arg; |
---|
| 143 | int status; |
---|
| 144 | |
---|
| 145 | status = saf1761_otg_filter_interrupt(sc); |
---|
| 146 | if ((status & FILTER_SCHEDULE_THREAD) != 0) { |
---|
| 147 | rtems_interrupt_server_request_submit(&sc->sc_irq_srv_req); |
---|
| 148 | } |
---|
| 149 | } |
---|
| 150 | #endif /* __rtems__ */ |
---|
[cf8e1d4] | 151 | static int |
---|
| 152 | saf1761_otg_fdt_attach(device_t dev) |
---|
| 153 | { |
---|
| 154 | struct saf1761_otg_softc *sc = device_get_softc(dev); |
---|
| 155 | char param[24]; |
---|
| 156 | int err; |
---|
| 157 | int rid; |
---|
[1e77a45] | 158 | #ifdef __rtems__ |
---|
| 159 | rtems_status_code status; |
---|
| 160 | #endif /* __rtems__ */ |
---|
[cf8e1d4] | 161 | |
---|
[5892207] | 162 | #ifndef __rtems__ |
---|
[cf8e1d4] | 163 | /* get configuration from FDT */ |
---|
| 164 | |
---|
| 165 | /* get bus-width, if any */ |
---|
| 166 | if (OF_getprop(ofw_bus_get_node(dev), "bus-width", |
---|
| 167 | ¶m, sizeof(param)) > 0) { |
---|
| 168 | param[sizeof(param) - 1] = 0; |
---|
| 169 | if (strcmp(param, "32") == 0) |
---|
| 170 | sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DATA_BUS_WIDTH; |
---|
| 171 | } else { |
---|
| 172 | /* assume 32-bit data bus */ |
---|
| 173 | sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DATA_BUS_WIDTH; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | /* get analog over-current setting */ |
---|
| 177 | if (OF_getprop(ofw_bus_get_node(dev), "analog-oc", |
---|
| 178 | ¶m, sizeof(param)) > 0) { |
---|
| 179 | sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_ANA_DIGI_OC; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | /* get DACK polarity */ |
---|
| 183 | if (OF_getprop(ofw_bus_get_node(dev), "dack-polarity", |
---|
| 184 | ¶m, sizeof(param)) > 0) { |
---|
| 185 | sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DACK_POL; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | /* get DREQ polarity */ |
---|
| 189 | if (OF_getprop(ofw_bus_get_node(dev), "dreq-polarity", |
---|
| 190 | ¶m, sizeof(param)) > 0) { |
---|
| 191 | sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DREQ_POL; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | /* get IRQ polarity */ |
---|
| 195 | if (OF_getprop(ofw_bus_get_node(dev), "int-polarity", |
---|
| 196 | ¶m, sizeof(param)) > 0) { |
---|
| 197 | sc->sc_interrupt_cfg |= SOTG_INTERRUPT_CFG_INTPOL; |
---|
| 198 | sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_INTR_POL; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | /* get IRQ level triggering */ |
---|
| 202 | if (OF_getprop(ofw_bus_get_node(dev), "int-level", |
---|
| 203 | ¶m, sizeof(param)) > 0) { |
---|
| 204 | sc->sc_interrupt_cfg |= SOTG_INTERRUPT_CFG_INTLVL; |
---|
| 205 | sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_INTR_LEVEL; |
---|
| 206 | } |
---|
[5892207] | 207 | #else /* __rtems__ */ |
---|
| 208 | /* set 16-bit data bus */ |
---|
| 209 | sc->sc_hw_mode = 0; |
---|
| 210 | #endif /* __rtems__ */ |
---|
[cf8e1d4] | 211 | |
---|
| 212 | /* initialise some bus fields */ |
---|
| 213 | sc->sc_bus.parent = dev; |
---|
| 214 | sc->sc_bus.devices = sc->sc_devices; |
---|
| 215 | sc->sc_bus.devices_max = SOTG_MAX_DEVICES; |
---|
| 216 | sc->sc_bus.dma_bits = 32; |
---|
| 217 | |
---|
| 218 | /* get all DMA memory */ |
---|
| 219 | if (usb_bus_mem_alloc_all(&sc->sc_bus, |
---|
| 220 | USB_GET_DMA_TAG(dev), NULL)) { |
---|
| 221 | return (ENOMEM); |
---|
| 222 | } |
---|
| 223 | rid = 0; |
---|
| 224 | sc->sc_io_res = |
---|
| 225 | bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); |
---|
| 226 | |
---|
| 227 | if (sc->sc_io_res == NULL) |
---|
| 228 | goto error; |
---|
| 229 | |
---|
| 230 | sc->sc_io_tag = rman_get_bustag(sc->sc_io_res); |
---|
| 231 | sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res); |
---|
| 232 | sc->sc_io_size = rman_get_size(sc->sc_io_res); |
---|
| 233 | |
---|
| 234 | /* try to allocate the HC interrupt first */ |
---|
| 235 | rid = 1; |
---|
| 236 | sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, |
---|
| 237 | RF_SHAREABLE | RF_ACTIVE); |
---|
| 238 | if (sc->sc_irq_res == NULL) { |
---|
| 239 | /* try to allocate a common IRQ second */ |
---|
| 240 | rid = 0; |
---|
| 241 | sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, |
---|
| 242 | RF_SHAREABLE | RF_ACTIVE); |
---|
| 243 | if (sc->sc_irq_res == NULL) |
---|
| 244 | goto error; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | sc->sc_bus.bdev = device_add_child(dev, "usbus", -1); |
---|
| 248 | if (sc->sc_bus.bdev == NULL) |
---|
| 249 | goto error; |
---|
| 250 | |
---|
| 251 | device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); |
---|
| 252 | |
---|
[5892207] | 253 | #ifdef __rtems__ |
---|
| 254 | #ifdef LIBBSP_ARM_ATSAM_BSP_H |
---|
| 255 | const Pin saf_irq = {PIO_PC16, PIOC, ID_PIOC, PIO_INPUT, PIO_PULLUP | PIO_IT_LOW_LEVEL}; |
---|
| 256 | /* Activate pin interrupt. Add a default handler that just clears the |
---|
| 257 | * status. */ |
---|
| 258 | PIO_Configure(&saf_irq, 1); |
---|
| 259 | PIO_EnableIt(&saf_irq); |
---|
| 260 | #endif /* LIBBSP_ARM_ATSAM_BSP_H */ |
---|
| 261 | #endif /* __rtems__ */ |
---|
[1e77a45] | 262 | #ifndef __rtems__ |
---|
[cf8e1d4] | 263 | err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_TTY | INTR_MPSAFE, |
---|
| 264 | &saf1761_otg_filter_interrupt, &saf1761_otg_interrupt, sc, &sc->sc_intr_hdl); |
---|
[1e77a45] | 265 | #else /* __rtems__ */ |
---|
| 266 | rtems_interrupt_server_request_initialize( |
---|
| 267 | RTEMS_INTERRUPT_SERVER_DEFAULT, &sc->sc_irq_srv_req, |
---|
| 268 | saf1761_otg_interrupt, sc); |
---|
| 269 | rtems_interrupt_server_request_set_vector(&sc->sc_irq_srv_req, |
---|
| 270 | rman_get_start(sc->sc_irq_res)); |
---|
| 271 | status = rtems_interrupt_handler_install( |
---|
| 272 | rman_get_start(sc->sc_irq_res), device_get_nameunit(dev), |
---|
| 273 | RTEMS_INTERRUPT_SHARED, saf1761_otg_filter_interrupt_wrapper, sc); |
---|
| 274 | err = (status == RTEMS_SUCCESSFUL) ? 0 : EINVAL; |
---|
| 275 | #endif /* __rtems__ */ |
---|
[cf8e1d4] | 276 | if (err) { |
---|
| 277 | sc->sc_intr_hdl = NULL; |
---|
| 278 | goto error; |
---|
| 279 | } |
---|
| 280 | err = saf1761_otg_init(sc); |
---|
| 281 | if (err) { |
---|
| 282 | device_printf(dev, "Init failed\n"); |
---|
| 283 | goto error; |
---|
| 284 | } |
---|
| 285 | err = device_probe_and_attach(sc->sc_bus.bdev); |
---|
| 286 | if (err) { |
---|
| 287 | device_printf(dev, "USB probe and attach failed\n"); |
---|
| 288 | goto error; |
---|
| 289 | } |
---|
| 290 | return (0); |
---|
| 291 | |
---|
| 292 | error: |
---|
| 293 | saf1761_otg_fdt_detach(dev); |
---|
| 294 | return (ENXIO); |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | static int |
---|
| 298 | saf1761_otg_fdt_detach(device_t dev) |
---|
| 299 | { |
---|
| 300 | struct saf1761_otg_softc *sc = device_get_softc(dev); |
---|
| 301 | int err; |
---|
| 302 | |
---|
| 303 | /* during module unload there are lots of children leftover */ |
---|
| 304 | device_delete_children(dev); |
---|
| 305 | |
---|
| 306 | if (sc->sc_irq_res && sc->sc_intr_hdl) { |
---|
| 307 | /* |
---|
| 308 | * Only call uninit() after init() |
---|
| 309 | */ |
---|
| 310 | saf1761_otg_uninit(sc); |
---|
| 311 | |
---|
| 312 | err = bus_teardown_intr(dev, sc->sc_irq_res, |
---|
| 313 | sc->sc_intr_hdl); |
---|
| 314 | sc->sc_intr_hdl = NULL; |
---|
| 315 | } |
---|
| 316 | if (sc->sc_irq_res) { |
---|
| 317 | bus_release_resource(dev, SYS_RES_IRQ, 0, |
---|
| 318 | sc->sc_irq_res); |
---|
| 319 | sc->sc_irq_res = NULL; |
---|
| 320 | } |
---|
| 321 | if (sc->sc_io_res) { |
---|
| 322 | bus_release_resource(dev, SYS_RES_MEMORY, 0, |
---|
| 323 | sc->sc_io_res); |
---|
| 324 | sc->sc_io_res = NULL; |
---|
| 325 | } |
---|
| 326 | usb_bus_mem_free_all(&sc->sc_bus, NULL); |
---|
| 327 | |
---|
| 328 | return (0); |
---|
| 329 | } |
---|