[a9153ec] | 1 | /** |
---|
| 2 | * @file |
---|
| 3 | * |
---|
| 4 | * @ingroup rtems_bsd_rtems |
---|
| 5 | * |
---|
| 6 | * @brief TODO. |
---|
| 7 | */ |
---|
| 8 | |
---|
| 9 | /* |
---|
[c1205ee] | 10 | * Copyright (c) 2009, 2017 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 | |
---|
[0237319] | 43 | #include <sys/param.h> |
---|
[3d1e767] | 44 | #include <sys/types.h> |
---|
[e599318] | 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> |
---|
[36a16f5c] | 51 | #include <machine/bus.h> |
---|
[1e36900] | 52 | |
---|
[c1205ee] | 53 | #include <rtems/bsd/local/opt_platform.h> |
---|
| 54 | |
---|
| 55 | #ifdef FDT |
---|
| 56 | #include <dev/ofw/ofw_bus.h> |
---|
| 57 | #endif |
---|
| 58 | |
---|
[369e2c4] | 59 | #include <rtems/bsd/bsd.h> |
---|
[5cf6031] | 60 | #include <rtems/irq-extension.h> |
---|
[1e36900] | 61 | |
---|
[e96e008] | 62 | /* #define DISABLE_INTERRUPT_EXTENSION */ |
---|
| 63 | |
---|
[c1205ee] | 64 | #if defined(__i386__) || defined(FDT) |
---|
| 65 | #define ENABLE_RESOURCE_ACTIVATE_DEACTIVATE |
---|
| 66 | #endif |
---|
| 67 | |
---|
[6cfc98d] | 68 | RTEMS_BSD_DECLARE_SET(nexus, rtems_bsd_device); |
---|
| 69 | |
---|
| 70 | RTEMS_BSD_DEFINE_SET(nexus, rtems_bsd_device); |
---|
| 71 | |
---|
[369e2c4] | 72 | RTEMS_STATIC_ASSERT(SYS_RES_MEMORY == RTEMS_BSD_RES_MEMORY, RTEMS_BSD_RES_MEMORY); |
---|
[1e36900] | 73 | |
---|
[369e2c4] | 74 | RTEMS_STATIC_ASSERT(SYS_RES_IRQ == RTEMS_BSD_RES_IRQ, RTEMS_BSD_RES_IRQ); |
---|
[1e36900] | 75 | |
---|
[369e2c4] | 76 | static struct rman mem_rman; |
---|
[1e36900] | 77 | |
---|
[369e2c4] | 78 | static struct rman irq_rman; |
---|
[a9153ec] | 79 | |
---|
[36a16f5c] | 80 | #ifdef __i386__ |
---|
| 81 | static struct rman port_rman; |
---|
| 82 | #endif |
---|
| 83 | |
---|
[00c8792] | 84 | #ifndef DISABLE_INTERRUPT_EXTENSION |
---|
| 85 | SYSINIT_REFERENCE(irqs); |
---|
| 86 | #endif |
---|
| 87 | |
---|
[a9153ec] | 88 | static int |
---|
| 89 | nexus_probe(device_t dev) |
---|
| 90 | { |
---|
[369e2c4] | 91 | int err; |
---|
[6cfc98d] | 92 | const rtems_bsd_device *nd; |
---|
[e599318] | 93 | |
---|
[a9153ec] | 94 | device_set_desc(dev, "RTEMS Nexus device"); |
---|
| 95 | |
---|
[369e2c4] | 96 | mem_rman.rm_start = 0; |
---|
| 97 | mem_rman.rm_end = ~0UL; |
---|
| 98 | mem_rman.rm_type = RMAN_ARRAY; |
---|
| 99 | mem_rman.rm_descr = "I/O memory addresses"; |
---|
| 100 | err = rman_init(&mem_rman) != 0; |
---|
| 101 | BSD_ASSERT(err == 0); |
---|
| 102 | err = rman_manage_region(&mem_rman, mem_rman.rm_start, mem_rman.rm_end); |
---|
| 103 | BSD_ASSERT(err == 0); |
---|
| 104 | |
---|
| 105 | irq_rman.rm_start = 0; |
---|
| 106 | irq_rman.rm_end = ~0UL; |
---|
| 107 | irq_rman.rm_type = RMAN_ARRAY; |
---|
| 108 | irq_rman.rm_descr = "Interrupt vectors"; |
---|
| 109 | err = rman_init(&irq_rman) != 0; |
---|
| 110 | BSD_ASSERT(err == 0); |
---|
| 111 | err = rman_manage_region(&irq_rman, irq_rman.rm_start, irq_rman.rm_end); |
---|
| 112 | BSD_ASSERT(err == 0); |
---|
| 113 | |
---|
[36a16f5c] | 114 | #ifdef __i386__ |
---|
| 115 | port_rman.rm_start = 0; |
---|
| 116 | port_rman.rm_end = 0xffff; |
---|
| 117 | port_rman.rm_type = RMAN_ARRAY; |
---|
| 118 | port_rman.rm_descr = "I/O ports"; |
---|
| 119 | err = rman_init(&port_rman) != 0; |
---|
| 120 | BSD_ASSERT(err == 0); |
---|
| 121 | err = rman_manage_region(&port_rman, port_rman.rm_start, |
---|
| 122 | port_rman.rm_end); |
---|
| 123 | BSD_ASSERT(err == 0); |
---|
| 124 | #endif |
---|
| 125 | |
---|
[6cfc98d] | 126 | SET_FOREACH(nd, nexus) { |
---|
[369e2c4] | 127 | device_add_child(dev, nd->name, nd->unit); |
---|
| 128 | } |
---|
| 129 | |
---|
[a9153ec] | 130 | return (0); |
---|
| 131 | } |
---|
[369e2c4] | 132 | |
---|
| 133 | static bool |
---|
[c40e45b] | 134 | nexus_get_start(const rtems_bsd_device *nd, int type, rman_res_t *start) |
---|
[1e36900] | 135 | { |
---|
[c40e45b] | 136 | u_long sr = (u_long)*start; |
---|
[369e2c4] | 137 | size_t i; |
---|
[1e36900] | 138 | |
---|
[369e2c4] | 139 | for (i = 0; i < nd->resource_count; ++i) { |
---|
| 140 | const rtems_bsd_device_resource *dr = &nd->resources[i]; |
---|
[1e36900] | 141 | |
---|
[369e2c4] | 142 | if (dr->type == type && dr->start_request == sr) { |
---|
| 143 | *start = dr->start_actual; |
---|
[1e36900] | 144 | |
---|
[369e2c4] | 145 | return (true); |
---|
| 146 | } |
---|
| 147 | } |
---|
[1e36900] | 148 | |
---|
[369e2c4] | 149 | return (false); |
---|
[1e36900] | 150 | } |
---|
| 151 | |
---|
| 152 | static struct resource * |
---|
| 153 | nexus_alloc_resource(device_t bus, device_t child, int type, int *rid, |
---|
[c40e45b] | 154 | rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) |
---|
[1e36900] | 155 | { |
---|
[36a16f5c] | 156 | struct resource *res = NULL; |
---|
[369e2c4] | 157 | struct rman *rm; |
---|
[6cfc98d] | 158 | const rtems_bsd_device *nd; |
---|
[1e36900] | 159 | |
---|
| 160 | switch (type) { |
---|
[369e2c4] | 161 | case SYS_RES_MEMORY: |
---|
| 162 | rm = &mem_rman; |
---|
| 163 | break; |
---|
[1e36900] | 164 | case SYS_RES_IRQ: |
---|
| 165 | rm = &irq_rman; |
---|
| 166 | break; |
---|
[36a16f5c] | 167 | #ifdef __i386__ |
---|
| 168 | case SYS_RES_IOPORT: |
---|
| 169 | rm = &port_rman; |
---|
| 170 | break; |
---|
| 171 | #endif |
---|
[369e2c4] | 172 | default: |
---|
[36a16f5c] | 173 | return (res); |
---|
[369e2c4] | 174 | } |
---|
[1e36900] | 175 | |
---|
[6cfc98d] | 176 | SET_FOREACH(nd, nexus) { |
---|
[369e2c4] | 177 | if (strcmp(device_get_name(child), nd->name) == 0 |
---|
| 178 | && device_get_unit(child) == nd->unit) { |
---|
[d43544e] | 179 | if (nexus_get_start(nd, type, &start)) { |
---|
| 180 | res = rman_reserve_resource(rm, start, end, |
---|
| 181 | count, flags, child); |
---|
| 182 | if (res != NULL) { |
---|
| 183 | rman_set_rid(res, *rid); |
---|
| 184 | rman_set_bushandle(res, |
---|
| 185 | rman_get_start(res)); |
---|
| 186 | } |
---|
[369e2c4] | 187 | }; |
---|
[1e36900] | 188 | |
---|
[d43544e] | 189 | return (res); |
---|
| 190 | } |
---|
[1e36900] | 191 | } |
---|
| 192 | |
---|
[36a16f5c] | 193 | #ifdef __i386__ |
---|
| 194 | /* |
---|
| 195 | * FIXME: This is a quick and dirty hack. Simply reserve resources of |
---|
| 196 | * this kind. See also pci_reserve_map(). |
---|
| 197 | */ |
---|
| 198 | if (start + count - end <= 1UL) { |
---|
| 199 | res = rman_reserve_resource(rm, start, end, count, flags, |
---|
| 200 | child); |
---|
| 201 | if (res != NULL) { |
---|
| 202 | rman_set_rid(res, *rid); |
---|
| 203 | rman_set_bushandle(res, rman_get_start(res)); |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | #endif |
---|
| 207 | |
---|
| 208 | return (res); |
---|
[1e36900] | 209 | } |
---|
| 210 | |
---|
| 211 | static int |
---|
[369e2c4] | 212 | nexus_release_resource(device_t bus, device_t child, int type, int rid, |
---|
| 213 | struct resource *res) |
---|
[1e36900] | 214 | { |
---|
[369e2c4] | 215 | return (rman_release_resource(res)); |
---|
[1e36900] | 216 | } |
---|
| 217 | |
---|
[c1205ee] | 218 | #ifdef ENABLE_RESOURCE_ACTIVATE_DEACTIVATE |
---|
[36a16f5c] | 219 | static int |
---|
| 220 | nexus_activate_resource(device_t bus, device_t child, int type, int rid, |
---|
| 221 | struct resource *res) |
---|
| 222 | { |
---|
[c1205ee] | 223 | |
---|
[36a16f5c] | 224 | switch (type) { |
---|
[c1205ee] | 225 | #ifdef __i386__ |
---|
[36a16f5c] | 226 | case SYS_RES_IOPORT: |
---|
| 227 | rman_set_bustag(res, X86_BUS_SPACE_IO); |
---|
| 228 | break; |
---|
[c1205ee] | 229 | #endif |
---|
[36a16f5c] | 230 | case SYS_RES_MEMORY: |
---|
[c1205ee] | 231 | #ifdef __i386__ |
---|
[36a16f5c] | 232 | rman_set_bustag(res, X86_BUS_SPACE_MEM); |
---|
[c1205ee] | 233 | #else |
---|
| 234 | rman_set_bushandle(res, rman_get_start(res)); |
---|
| 235 | #endif |
---|
[36a16f5c] | 236 | break; |
---|
| 237 | } |
---|
| 238 | return (rman_activate_resource(res)); |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | static int |
---|
| 242 | nexus_deactivate_resource(device_t bus, device_t child, int type, int rid, |
---|
| 243 | struct resource *res) |
---|
| 244 | { |
---|
| 245 | |
---|
| 246 | return (rman_deactivate_resource(res)); |
---|
| 247 | } |
---|
| 248 | #endif |
---|
| 249 | |
---|
[369e2c4] | 250 | struct nexus_intr { |
---|
| 251 | driver_filter_t *filt; |
---|
| 252 | driver_intr_t *intr; |
---|
| 253 | void *arg; |
---|
| 254 | }; |
---|
| 255 | |
---|
| 256 | static void |
---|
| 257 | nexus_intr_with_filter(void *arg) |
---|
[1e36900] | 258 | { |
---|
[369e2c4] | 259 | struct nexus_intr *ni; |
---|
| 260 | int status; |
---|
[1e36900] | 261 | |
---|
[369e2c4] | 262 | ni = arg; |
---|
[1e36900] | 263 | |
---|
[369e2c4] | 264 | status = (*ni->filt)(ni->arg); |
---|
| 265 | if ((status & FILTER_SCHEDULE_THREAD) != 0) { |
---|
| 266 | (*ni->intr)(ni->arg); |
---|
[1e36900] | 267 | } |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | static int |
---|
[369e2c4] | 271 | nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags, |
---|
| 272 | driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep) |
---|
[1e36900] | 273 | { |
---|
[369e2c4] | 274 | int err; |
---|
[e96e008] | 275 | #ifndef DISABLE_INTERRUPT_EXTENSION |
---|
[369e2c4] | 276 | struct nexus_intr *ni; |
---|
| 277 | |
---|
| 278 | ni = malloc(sizeof(*ni), M_TEMP, M_WAITOK); |
---|
| 279 | if (ni != NULL) { |
---|
| 280 | rtems_status_code sc; |
---|
| 281 | rtems_interrupt_handler rh; |
---|
| 282 | void *ra; |
---|
| 283 | |
---|
| 284 | ni->filt = filt; |
---|
| 285 | ni->intr = intr; |
---|
| 286 | ni->arg = arg; |
---|
| 287 | |
---|
| 288 | *cookiep = ni; |
---|
| 289 | |
---|
| 290 | if (filt == NULL) { |
---|
| 291 | rh = intr; |
---|
| 292 | ra = arg; |
---|
| 293 | } else { |
---|
| 294 | rh = nexus_intr_with_filter; |
---|
| 295 | ra = ni; |
---|
| 296 | } |
---|
[1e36900] | 297 | |
---|
[369e2c4] | 298 | sc = rtems_interrupt_server_handler_install(RTEMS_ID_NONE, |
---|
| 299 | rman_get_start(res), device_get_nameunit(child), |
---|
[b23da31] | 300 | RTEMS_INTERRUPT_SHARED, rh, ra); |
---|
[369e2c4] | 301 | if (sc == RTEMS_SUCCESSFUL) { |
---|
| 302 | err = 0; |
---|
| 303 | } else { |
---|
| 304 | free(ni, M_TEMP); |
---|
[2f50f0a] | 305 | |
---|
[369e2c4] | 306 | err = EINVAL; |
---|
| 307 | } |
---|
| 308 | } else { |
---|
| 309 | err = ENOMEM; |
---|
| 310 | } |
---|
[e96e008] | 311 | #else |
---|
| 312 | err = EINVAL; |
---|
| 313 | #endif |
---|
[369e2c4] | 314 | |
---|
| 315 | return (err); |
---|
[2f50f0a] | 316 | } |
---|
| 317 | |
---|
| 318 | static int |
---|
[369e2c4] | 319 | nexus_teardown_intr(device_t dev, device_t child, struct resource *res, |
---|
| 320 | void *cookie) |
---|
[2f50f0a] | 321 | { |
---|
[369e2c4] | 322 | int err; |
---|
[e96e008] | 323 | #ifndef DISABLE_INTERRUPT_EXTENSION |
---|
[369e2c4] | 324 | struct nexus_intr *ni; |
---|
| 325 | rtems_status_code sc; |
---|
| 326 | rtems_interrupt_handler rh; |
---|
| 327 | void *ra; |
---|
| 328 | |
---|
| 329 | ni = cookie; |
---|
| 330 | |
---|
| 331 | if (ni->filt == NULL) { |
---|
| 332 | rh = ni->intr; |
---|
| 333 | ra = ni->arg; |
---|
| 334 | } else { |
---|
| 335 | rh = nexus_intr_with_filter; |
---|
| 336 | ra = ni->arg; |
---|
| 337 | } |
---|
[2f50f0a] | 338 | |
---|
[369e2c4] | 339 | sc = rtems_interrupt_server_handler_install(RTEMS_ID_NONE, |
---|
| 340 | rman_get_start(res), device_get_nameunit(child), |
---|
[b23da31] | 341 | RTEMS_INTERRUPT_SHARED, rh, ra); |
---|
[369e2c4] | 342 | err = sc == RTEMS_SUCCESSFUL ? 0 : EINVAL; |
---|
[e96e008] | 343 | #else |
---|
| 344 | err = EINVAL; |
---|
| 345 | #endif |
---|
[2f50f0a] | 346 | |
---|
[369e2c4] | 347 | return (err); |
---|
[2f50f0a] | 348 | } |
---|
| 349 | |
---|
[c1205ee] | 350 | #ifdef FDT |
---|
| 351 | static int |
---|
| 352 | nexus_ofw_map_intr(device_t dev, device_t child, phandle_t iparent, int icells, |
---|
| 353 | pcell_t *intr) |
---|
| 354 | { |
---|
| 355 | |
---|
[b469163] | 356 | return ((int)bsp_fdt_map_intr(intr, (size_t)icells)); |
---|
[c1205ee] | 357 | } |
---|
| 358 | #endif /* FDT */ |
---|
| 359 | |
---|
[369e2c4] | 360 | static device_method_t nexus_methods[] = { |
---|
[a9153ec] | 361 | /* Device interface */ |
---|
| 362 | DEVMETHOD(device_probe, nexus_probe), |
---|
[369e2c4] | 363 | DEVMETHOD(device_attach, bus_generic_attach), |
---|
[a9153ec] | 364 | DEVMETHOD(device_detach, bus_generic_detach), |
---|
| 365 | DEVMETHOD(device_shutdown, bus_generic_shutdown), |
---|
| 366 | DEVMETHOD(device_suspend, bus_generic_suspend), |
---|
| 367 | DEVMETHOD(device_resume, bus_generic_resume), |
---|
| 368 | |
---|
| 369 | /* Bus interface */ |
---|
| 370 | DEVMETHOD(bus_print_child, bus_generic_print_child), |
---|
[369e2c4] | 371 | DEVMETHOD(bus_add_child, bus_generic_add_child), |
---|
| 372 | DEVMETHOD(bus_alloc_resource, nexus_alloc_resource), |
---|
| 373 | DEVMETHOD(bus_release_resource, nexus_release_resource), |
---|
[c1205ee] | 374 | #ifdef ENABLE_RESOURCE_ACTIVATE_DEACTIVATE |
---|
[36a16f5c] | 375 | DEVMETHOD(bus_activate_resource, nexus_activate_resource), |
---|
| 376 | DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource), |
---|
| 377 | #endif |
---|
[369e2c4] | 378 | DEVMETHOD(bus_setup_intr, nexus_setup_intr), |
---|
| 379 | DEVMETHOD(bus_teardown_intr, nexus_teardown_intr), |
---|
[a9153ec] | 380 | |
---|
[c1205ee] | 381 | #ifdef FDT |
---|
| 382 | /* OFW interface */ |
---|
| 383 | DEVMETHOD(ofw_bus_map_intr, nexus_ofw_map_intr), |
---|
| 384 | #endif |
---|
| 385 | |
---|
[a9153ec] | 386 | { 0, 0 } |
---|
| 387 | }; |
---|
| 388 | |
---|
| 389 | static driver_t nexus_driver = { |
---|
| 390 | .name = "nexus", |
---|
| 391 | .methods = nexus_methods, |
---|
| 392 | .size = 0 |
---|
| 393 | }; |
---|
| 394 | |
---|
| 395 | static devclass_t nexus_devclass; |
---|
| 396 | |
---|
| 397 | DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0); |
---|