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