1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup rtems_bsd_rtems |
---|
5 | * |
---|
6 | * @brief TODO. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (c) 2009, 2017 embedded brains GmbH. All rights reserved. |
---|
11 | * |
---|
12 | * embedded brains GmbH |
---|
13 | * Dornierstr. 4 |
---|
14 | * 82178 Puchheim |
---|
15 | * Germany |
---|
16 | * <rtems@embedded-brains.de> |
---|
17 | * |
---|
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. |
---|
38 | */ |
---|
39 | |
---|
40 | #include <machine/rtems-bsd-kernel-space.h> |
---|
41 | #include <machine/rtems-bsd-thread.h> |
---|
42 | |
---|
43 | #include <rtems/bsd/sys/param.h> |
---|
44 | #include <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> |
---|
51 | #include <machine/bus.h> |
---|
52 | |
---|
53 | #include <rtems/bsd/local/opt_platform.h> |
---|
54 | |
---|
55 | #ifdef FDT |
---|
56 | #include <dev/ofw/ofw_bus.h> |
---|
57 | #endif |
---|
58 | |
---|
59 | #include <rtems/bsd/bsd.h> |
---|
60 | #include <rtems/irq-extension.h> |
---|
61 | |
---|
62 | /* #define DISABLE_INTERRUPT_EXTENSION */ |
---|
63 | |
---|
64 | #if defined(__i386__) || defined(FDT) |
---|
65 | #define ENABLE_RESOURCE_ACTIVATE_DEACTIVATE |
---|
66 | #endif |
---|
67 | |
---|
68 | RTEMS_BSD_DECLARE_SET(nexus, rtems_bsd_device); |
---|
69 | |
---|
70 | RTEMS_BSD_DEFINE_SET(nexus, rtems_bsd_device); |
---|
71 | |
---|
72 | RTEMS_STATIC_ASSERT(SYS_RES_MEMORY == RTEMS_BSD_RES_MEMORY, RTEMS_BSD_RES_MEMORY); |
---|
73 | |
---|
74 | RTEMS_STATIC_ASSERT(SYS_RES_IRQ == RTEMS_BSD_RES_IRQ, RTEMS_BSD_RES_IRQ); |
---|
75 | |
---|
76 | static struct rman mem_rman; |
---|
77 | |
---|
78 | static struct rman irq_rman; |
---|
79 | |
---|
80 | #ifdef __i386__ |
---|
81 | static struct rman port_rman; |
---|
82 | #endif |
---|
83 | |
---|
84 | #ifndef DISABLE_INTERRUPT_EXTENSION |
---|
85 | SYSINIT_REFERENCE(irqs); |
---|
86 | #endif |
---|
87 | |
---|
88 | static int |
---|
89 | nexus_probe(device_t dev) |
---|
90 | { |
---|
91 | int err; |
---|
92 | const rtems_bsd_device *nd; |
---|
93 | |
---|
94 | device_set_desc(dev, "RTEMS Nexus device"); |
---|
95 | |
---|
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 | |
---|
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 | |
---|
126 | SET_FOREACH(nd, nexus) { |
---|
127 | device_add_child(dev, nd->name, nd->unit); |
---|
128 | } |
---|
129 | |
---|
130 | return (0); |
---|
131 | } |
---|
132 | |
---|
133 | static bool |
---|
134 | nexus_get_start(const rtems_bsd_device *nd, int type, rman_res_t *start) |
---|
135 | { |
---|
136 | u_long sr = (u_long)*start; |
---|
137 | size_t i; |
---|
138 | |
---|
139 | for (i = 0; i < nd->resource_count; ++i) { |
---|
140 | const rtems_bsd_device_resource *dr = &nd->resources[i]; |
---|
141 | |
---|
142 | if (dr->type == type && dr->start_request == sr) { |
---|
143 | *start = dr->start_actual; |
---|
144 | |
---|
145 | return (true); |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | return (false); |
---|
150 | } |
---|
151 | |
---|
152 | static struct resource * |
---|
153 | nexus_alloc_resource(device_t bus, device_t child, int type, int *rid, |
---|
154 | rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) |
---|
155 | { |
---|
156 | struct resource *res = NULL; |
---|
157 | struct rman *rm; |
---|
158 | const rtems_bsd_device *nd; |
---|
159 | |
---|
160 | switch (type) { |
---|
161 | case SYS_RES_MEMORY: |
---|
162 | rm = &mem_rman; |
---|
163 | break; |
---|
164 | case SYS_RES_IRQ: |
---|
165 | rm = &irq_rman; |
---|
166 | break; |
---|
167 | #ifdef __i386__ |
---|
168 | case SYS_RES_IOPORT: |
---|
169 | rm = &port_rman; |
---|
170 | break; |
---|
171 | #endif |
---|
172 | default: |
---|
173 | return (res); |
---|
174 | } |
---|
175 | |
---|
176 | SET_FOREACH(nd, nexus) { |
---|
177 | if (strcmp(device_get_name(child), nd->name) == 0 |
---|
178 | && device_get_unit(child) == nd->unit) { |
---|
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 | } |
---|
187 | }; |
---|
188 | |
---|
189 | return (res); |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
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); |
---|
209 | } |
---|
210 | |
---|
211 | static int |
---|
212 | nexus_release_resource(device_t bus, device_t child, int type, int rid, |
---|
213 | struct resource *res) |
---|
214 | { |
---|
215 | return (rman_release_resource(res)); |
---|
216 | } |
---|
217 | |
---|
218 | #ifdef ENABLE_RESOURCE_ACTIVATE_DEACTIVATE |
---|
219 | static int |
---|
220 | nexus_activate_resource(device_t bus, device_t child, int type, int rid, |
---|
221 | struct resource *res) |
---|
222 | { |
---|
223 | |
---|
224 | switch (type) { |
---|
225 | #ifdef __i386__ |
---|
226 | case SYS_RES_IOPORT: |
---|
227 | rman_set_bustag(res, X86_BUS_SPACE_IO); |
---|
228 | break; |
---|
229 | #endif |
---|
230 | case SYS_RES_MEMORY: |
---|
231 | #ifdef __i386__ |
---|
232 | rman_set_bustag(res, X86_BUS_SPACE_MEM); |
---|
233 | #else |
---|
234 | rman_set_bushandle(res, rman_get_start(res)); |
---|
235 | #endif |
---|
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 | |
---|
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) |
---|
258 | { |
---|
259 | struct nexus_intr *ni; |
---|
260 | int status; |
---|
261 | |
---|
262 | ni = arg; |
---|
263 | |
---|
264 | status = (*ni->filt)(ni->arg); |
---|
265 | if ((status & FILTER_SCHEDULE_THREAD) != 0) { |
---|
266 | (*ni->intr)(ni->arg); |
---|
267 | } |
---|
268 | } |
---|
269 | |
---|
270 | static int |
---|
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) |
---|
273 | { |
---|
274 | int err; |
---|
275 | #ifndef DISABLE_INTERRUPT_EXTENSION |
---|
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 | } |
---|
297 | |
---|
298 | sc = rtems_interrupt_server_handler_install(RTEMS_ID_NONE, |
---|
299 | rman_get_start(res), device_get_nameunit(child), |
---|
300 | RTEMS_INTERRUPT_SHARED, rh, ra); |
---|
301 | if (sc == RTEMS_SUCCESSFUL) { |
---|
302 | err = 0; |
---|
303 | } else { |
---|
304 | free(ni, M_TEMP); |
---|
305 | |
---|
306 | err = EINVAL; |
---|
307 | } |
---|
308 | } else { |
---|
309 | err = ENOMEM; |
---|
310 | } |
---|
311 | #else |
---|
312 | err = EINVAL; |
---|
313 | #endif |
---|
314 | |
---|
315 | return (err); |
---|
316 | } |
---|
317 | |
---|
318 | static int |
---|
319 | nexus_teardown_intr(device_t dev, device_t child, struct resource *res, |
---|
320 | void *cookie) |
---|
321 | { |
---|
322 | int err; |
---|
323 | #ifndef DISABLE_INTERRUPT_EXTENSION |
---|
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 | } |
---|
338 | |
---|
339 | sc = rtems_interrupt_server_handler_install(RTEMS_ID_NONE, |
---|
340 | rman_get_start(res), device_get_nameunit(child), |
---|
341 | RTEMS_INTERRUPT_SHARED, rh, ra); |
---|
342 | err = sc == RTEMS_SUCCESSFUL ? 0 : EINVAL; |
---|
343 | #else |
---|
344 | err = EINVAL; |
---|
345 | #endif |
---|
346 | |
---|
347 | return (err); |
---|
348 | } |
---|
349 | |
---|
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 | |
---|
356 | return ((int)bsp_fdt_map_intr(intr[0])); |
---|
357 | } |
---|
358 | #endif /* FDT */ |
---|
359 | |
---|
360 | static device_method_t nexus_methods[] = { |
---|
361 | /* Device interface */ |
---|
362 | DEVMETHOD(device_probe, nexus_probe), |
---|
363 | DEVMETHOD(device_attach, bus_generic_attach), |
---|
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), |
---|
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), |
---|
374 | #ifdef ENABLE_RESOURCE_ACTIVATE_DEACTIVATE |
---|
375 | DEVMETHOD(bus_activate_resource, nexus_activate_resource), |
---|
376 | DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource), |
---|
377 | #endif |
---|
378 | DEVMETHOD(bus_setup_intr, nexus_setup_intr), |
---|
379 | DEVMETHOD(bus_teardown_intr, nexus_teardown_intr), |
---|
380 | |
---|
381 | #ifdef FDT |
---|
382 | /* OFW interface */ |
---|
383 | DEVMETHOD(ofw_bus_map_intr, nexus_ofw_map_intr), |
---|
384 | #endif |
---|
385 | |
---|
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); |
---|