[a9153ec] | 1 | /** |
---|
| 2 | * @file |
---|
| 3 | * |
---|
| 4 | * @ingroup rtems_bsd_rtems |
---|
| 5 | * |
---|
| 6 | * @brief TODO. |
---|
| 7 | */ |
---|
| 8 | |
---|
| 9 | /* |
---|
[369e2c4] | 10 | * Copyright (c) 2009-2013 embedded brains GmbH. All rights reserved. |
---|
[a9153ec] | 11 | * |
---|
| 12 | * embedded brains GmbH |
---|
[369e2c4] | 13 | * Dornierstr. 4 |
---|
[a9153ec] | 14 | * 82178 Puchheim |
---|
| 15 | * Germany |
---|
| 16 | * <rtems@embedded-brains.de> |
---|
| 17 | * |
---|
[8420b94] | 18 | * Redistribution and use in source and binary forms, with or without |
---|
| 19 | * modification, are permitted provided that the following conditions |
---|
| 20 | * are met: |
---|
| 21 | * 1. Redistributions of source code must retain the above copyright |
---|
| 22 | * notice, this list of conditions and the following disclaimer. |
---|
| 23 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
| 24 | * notice, this list of conditions and the following disclaimer in the |
---|
| 25 | * documentation and/or other materials provided with the distribution. |
---|
| 26 | * |
---|
| 27 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
| 28 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 30 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
| 31 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
| 32 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
| 33 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
| 34 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
| 35 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
| 36 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
| 37 | * SUCH DAMAGE. |
---|
[a9153ec] | 38 | */ |
---|
| 39 | |
---|
[f244de9] | 40 | #include <machine/rtems-bsd-kernel-space.h> |
---|
[e51bc97] | 41 | #include <machine/rtems-bsd-thread.h> |
---|
[e599318] | 42 | |
---|
| 43 | #include <rtems/bsd/sys/param.h> |
---|
| 44 | #include <rtems/bsd/sys/types.h> |
---|
| 45 | #include <sys/systm.h> |
---|
| 46 | #include <sys/bus.h> |
---|
| 47 | #include <sys/kernel.h> |
---|
| 48 | #include <sys/module.h> |
---|
| 49 | #include <sys/rman.h> |
---|
| 50 | #include <sys/malloc.h> |
---|
[1e36900] | 51 | |
---|
[369e2c4] | 52 | #include <rtems/bsd/bsd.h> |
---|
[5cf6031] | 53 | #include <rtems/irq-extension.h> |
---|
[1e36900] | 54 | |
---|
[e96e008] | 55 | /* #define DISABLE_INTERRUPT_EXTENSION */ |
---|
| 56 | |
---|
[369e2c4] | 57 | RTEMS_STATIC_ASSERT(SYS_RES_MEMORY == RTEMS_BSD_RES_MEMORY, RTEMS_BSD_RES_MEMORY); |
---|
[1e36900] | 58 | |
---|
[369e2c4] | 59 | RTEMS_STATIC_ASSERT(SYS_RES_IRQ == RTEMS_BSD_RES_IRQ, RTEMS_BSD_RES_IRQ); |
---|
[1e36900] | 60 | |
---|
[369e2c4] | 61 | static struct rman mem_rman; |
---|
[1e36900] | 62 | |
---|
[369e2c4] | 63 | static struct rman irq_rman; |
---|
[a9153ec] | 64 | |
---|
| 65 | static int |
---|
| 66 | nexus_probe(device_t dev) |
---|
| 67 | { |
---|
[e51bc97] | 68 | rtems_status_code status; |
---|
[369e2c4] | 69 | int err; |
---|
| 70 | size_t i; |
---|
[e599318] | 71 | |
---|
[a9153ec] | 72 | device_set_desc(dev, "RTEMS Nexus device"); |
---|
| 73 | |
---|
[e96e008] | 74 | #ifndef DISABLE_INTERRUPT_EXTENSION |
---|
[e51bc97] | 75 | status = rtems_interrupt_server_initialize( |
---|
| 76 | BSD_TASK_PRIORITY_INTERRUPT, |
---|
| 77 | BSD_MINIMUM_TASK_STACK_SIZE, |
---|
| 78 | RTEMS_DEFAULT_MODES, |
---|
| 79 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
| 80 | NULL |
---|
| 81 | ); |
---|
| 82 | BSD_ASSERT(status == RTEMS_SUCCESSFUL); |
---|
[e96e008] | 83 | #endif |
---|
[e51bc97] | 84 | |
---|
[369e2c4] | 85 | mem_rman.rm_start = 0; |
---|
| 86 | mem_rman.rm_end = ~0UL; |
---|
| 87 | mem_rman.rm_type = RMAN_ARRAY; |
---|
| 88 | mem_rman.rm_descr = "I/O memory addresses"; |
---|
| 89 | err = rman_init(&mem_rman) != 0; |
---|
| 90 | BSD_ASSERT(err == 0); |
---|
| 91 | err = rman_manage_region(&mem_rman, mem_rman.rm_start, mem_rman.rm_end); |
---|
| 92 | BSD_ASSERT(err == 0); |
---|
| 93 | |
---|
| 94 | irq_rman.rm_start = 0; |
---|
| 95 | irq_rman.rm_end = ~0UL; |
---|
| 96 | irq_rman.rm_type = RMAN_ARRAY; |
---|
| 97 | irq_rman.rm_descr = "Interrupt vectors"; |
---|
| 98 | err = rman_init(&irq_rman) != 0; |
---|
| 99 | BSD_ASSERT(err == 0); |
---|
| 100 | err = rman_manage_region(&irq_rman, irq_rman.rm_start, irq_rman.rm_end); |
---|
| 101 | BSD_ASSERT(err == 0); |
---|
| 102 | |
---|
| 103 | for (i = 0; i < rtems_bsd_nexus_device_count; ++i) { |
---|
| 104 | const rtems_bsd_device *nd = &rtems_bsd_nexus_devices[i]; |
---|
| 105 | |
---|
| 106 | device_add_child(dev, nd->name, nd->unit); |
---|
| 107 | } |
---|
| 108 | |
---|
[a9153ec] | 109 | return (0); |
---|
| 110 | } |
---|
[369e2c4] | 111 | |
---|
| 112 | static bool |
---|
| 113 | nexus_get_start(const rtems_bsd_device *nd, int type, u_long *start) |
---|
[1e36900] | 114 | { |
---|
[369e2c4] | 115 | u_long sr = *start; |
---|
| 116 | size_t i; |
---|
[1e36900] | 117 | |
---|
[369e2c4] | 118 | for (i = 0; i < nd->resource_count; ++i) { |
---|
| 119 | const rtems_bsd_device_resource *dr = &nd->resources[i]; |
---|
[1e36900] | 120 | |
---|
[369e2c4] | 121 | if (dr->type == type && dr->start_request == sr) { |
---|
| 122 | *start = dr->start_actual; |
---|
[1e36900] | 123 | |
---|
[369e2c4] | 124 | return (true); |
---|
| 125 | } |
---|
| 126 | } |
---|
[1e36900] | 127 | |
---|
[369e2c4] | 128 | return (false); |
---|
[1e36900] | 129 | } |
---|
| 130 | |
---|
| 131 | static struct resource * |
---|
| 132 | nexus_alloc_resource(device_t bus, device_t child, int type, int *rid, |
---|
[369e2c4] | 133 | u_long start, u_long end, u_long count, u_int flags) |
---|
[1e36900] | 134 | { |
---|
[369e2c4] | 135 | struct resource *rv; |
---|
| 136 | struct rman *rm; |
---|
| 137 | size_t i; |
---|
[1e36900] | 138 | |
---|
| 139 | switch (type) { |
---|
[369e2c4] | 140 | case SYS_RES_MEMORY: |
---|
| 141 | rm = &mem_rman; |
---|
| 142 | break; |
---|
[1e36900] | 143 | case SYS_RES_IRQ: |
---|
| 144 | rm = &irq_rman; |
---|
| 145 | break; |
---|
[369e2c4] | 146 | default: |
---|
| 147 | return (NULL); |
---|
| 148 | } |
---|
[1e36900] | 149 | |
---|
[369e2c4] | 150 | for (i = 0; i < rtems_bsd_nexus_device_count; ++i) { |
---|
| 151 | const rtems_bsd_device *nd = &rtems_bsd_nexus_devices[i]; |
---|
[1e36900] | 152 | |
---|
[369e2c4] | 153 | if (strcmp(device_get_name(child), nd->name) == 0 |
---|
| 154 | && device_get_unit(child) == nd->unit) { |
---|
| 155 | if (!nexus_get_start(nd, type, &start)) { |
---|
| 156 | return (NULL); |
---|
| 157 | }; |
---|
| 158 | } else { |
---|
| 159 | return (NULL); |
---|
| 160 | } |
---|
[1e36900] | 161 | } |
---|
| 162 | |
---|
| 163 | rv = rman_reserve_resource(rm, start, end, count, flags, child); |
---|
[369e2c4] | 164 | if (rv != NULL) { |
---|
| 165 | rman_set_rid(rv, *rid); |
---|
| 166 | rman_set_bushandle(rv, rman_get_start(rv)); |
---|
[1e36900] | 167 | } |
---|
| 168 | |
---|
[369e2c4] | 169 | return (rv); |
---|
[1e36900] | 170 | } |
---|
| 171 | |
---|
| 172 | static int |
---|
[369e2c4] | 173 | nexus_release_resource(device_t bus, device_t child, int type, int rid, |
---|
| 174 | struct resource *res) |
---|
[1e36900] | 175 | { |
---|
[369e2c4] | 176 | return (rman_release_resource(res)); |
---|
[1e36900] | 177 | } |
---|
| 178 | |
---|
[369e2c4] | 179 | struct nexus_intr { |
---|
| 180 | driver_filter_t *filt; |
---|
| 181 | driver_intr_t *intr; |
---|
| 182 | void *arg; |
---|
| 183 | }; |
---|
| 184 | |
---|
| 185 | static void |
---|
| 186 | nexus_intr_with_filter(void *arg) |
---|
[1e36900] | 187 | { |
---|
[369e2c4] | 188 | struct nexus_intr *ni; |
---|
| 189 | int status; |
---|
[1e36900] | 190 | |
---|
[369e2c4] | 191 | ni = arg; |
---|
[1e36900] | 192 | |
---|
[369e2c4] | 193 | status = (*ni->filt)(ni->arg); |
---|
| 194 | if ((status & FILTER_SCHEDULE_THREAD) != 0) { |
---|
| 195 | (*ni->intr)(ni->arg); |
---|
[1e36900] | 196 | } |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | static int |
---|
[369e2c4] | 200 | nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags, |
---|
| 201 | driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep) |
---|
[1e36900] | 202 | { |
---|
[369e2c4] | 203 | int err; |
---|
[e96e008] | 204 | #ifndef DISABLE_INTERRUPT_EXTENSION |
---|
[369e2c4] | 205 | struct nexus_intr *ni; |
---|
| 206 | |
---|
| 207 | ni = malloc(sizeof(*ni), M_TEMP, M_WAITOK); |
---|
| 208 | if (ni != NULL) { |
---|
| 209 | rtems_status_code sc; |
---|
| 210 | rtems_interrupt_handler rh; |
---|
| 211 | void *ra; |
---|
| 212 | |
---|
| 213 | ni->filt = filt; |
---|
| 214 | ni->intr = intr; |
---|
| 215 | ni->arg = arg; |
---|
| 216 | |
---|
| 217 | *cookiep = ni; |
---|
| 218 | |
---|
| 219 | if (filt == NULL) { |
---|
| 220 | rh = intr; |
---|
| 221 | ra = arg; |
---|
| 222 | } else { |
---|
| 223 | rh = nexus_intr_with_filter; |
---|
| 224 | ra = ni; |
---|
| 225 | } |
---|
[1e36900] | 226 | |
---|
[369e2c4] | 227 | sc = rtems_interrupt_server_handler_install(RTEMS_ID_NONE, |
---|
| 228 | rman_get_start(res), device_get_nameunit(child), |
---|
| 229 | RTEMS_INTERRUPT_UNIQUE, rh, ra); |
---|
| 230 | if (sc == RTEMS_SUCCESSFUL) { |
---|
| 231 | err = 0; |
---|
| 232 | } else { |
---|
| 233 | free(ni, M_TEMP); |
---|
[2f50f0a] | 234 | |
---|
[369e2c4] | 235 | err = EINVAL; |
---|
| 236 | } |
---|
| 237 | } else { |
---|
| 238 | err = ENOMEM; |
---|
| 239 | } |
---|
[e96e008] | 240 | #else |
---|
| 241 | err = EINVAL; |
---|
| 242 | #endif |
---|
[369e2c4] | 243 | |
---|
| 244 | return (err); |
---|
[2f50f0a] | 245 | } |
---|
| 246 | |
---|
| 247 | static int |
---|
[369e2c4] | 248 | nexus_teardown_intr(device_t dev, device_t child, struct resource *res, |
---|
| 249 | void *cookie) |
---|
[2f50f0a] | 250 | { |
---|
[369e2c4] | 251 | int err; |
---|
[e96e008] | 252 | #ifndef DISABLE_INTERRUPT_EXTENSION |
---|
[369e2c4] | 253 | struct nexus_intr *ni; |
---|
| 254 | rtems_status_code sc; |
---|
| 255 | rtems_interrupt_handler rh; |
---|
| 256 | void *ra; |
---|
| 257 | |
---|
| 258 | ni = cookie; |
---|
| 259 | |
---|
| 260 | if (ni->filt == NULL) { |
---|
| 261 | rh = ni->intr; |
---|
| 262 | ra = ni->arg; |
---|
| 263 | } else { |
---|
| 264 | rh = nexus_intr_with_filter; |
---|
| 265 | ra = ni->arg; |
---|
| 266 | } |
---|
[2f50f0a] | 267 | |
---|
[369e2c4] | 268 | sc = rtems_interrupt_server_handler_install(RTEMS_ID_NONE, |
---|
| 269 | rman_get_start(res), device_get_nameunit(child), |
---|
| 270 | RTEMS_INTERRUPT_UNIQUE, rh, ra); |
---|
| 271 | err = sc == RTEMS_SUCCESSFUL ? 0 : EINVAL; |
---|
[e96e008] | 272 | #else |
---|
| 273 | err = EINVAL; |
---|
| 274 | #endif |
---|
[2f50f0a] | 275 | |
---|
[369e2c4] | 276 | return (err); |
---|
[2f50f0a] | 277 | } |
---|
| 278 | |
---|
[369e2c4] | 279 | static device_method_t nexus_methods[] = { |
---|
[a9153ec] | 280 | /* Device interface */ |
---|
| 281 | DEVMETHOD(device_probe, nexus_probe), |
---|
[369e2c4] | 282 | DEVMETHOD(device_attach, bus_generic_attach), |
---|
[a9153ec] | 283 | DEVMETHOD(device_detach, bus_generic_detach), |
---|
| 284 | DEVMETHOD(device_shutdown, bus_generic_shutdown), |
---|
| 285 | DEVMETHOD(device_suspend, bus_generic_suspend), |
---|
| 286 | DEVMETHOD(device_resume, bus_generic_resume), |
---|
| 287 | |
---|
| 288 | /* Bus interface */ |
---|
| 289 | DEVMETHOD(bus_print_child, bus_generic_print_child), |
---|
[369e2c4] | 290 | DEVMETHOD(bus_add_child, bus_generic_add_child), |
---|
| 291 | DEVMETHOD(bus_alloc_resource, nexus_alloc_resource), |
---|
| 292 | DEVMETHOD(bus_release_resource, nexus_release_resource), |
---|
| 293 | DEVMETHOD(bus_setup_intr, nexus_setup_intr), |
---|
| 294 | DEVMETHOD(bus_teardown_intr, nexus_teardown_intr), |
---|
[a9153ec] | 295 | |
---|
| 296 | { 0, 0 } |
---|
| 297 | }; |
---|
| 298 | |
---|
| 299 | static driver_t nexus_driver = { |
---|
| 300 | .name = "nexus", |
---|
| 301 | .methods = nexus_methods, |
---|
| 302 | .size = 0 |
---|
| 303 | }; |
---|
| 304 | |
---|
| 305 | static devclass_t nexus_devclass; |
---|
| 306 | |
---|
| 307 | DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0); |
---|