1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup rtems_bsd_rtems |
---|
5 | * |
---|
6 | * @brief TODO. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (c) 2009, 2010 embedded brains GmbH. All rights reserved. |
---|
11 | * |
---|
12 | * embedded brains GmbH |
---|
13 | * Obere Lagerstr. 30 |
---|
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 <freebsd/machine/rtems-bsd-config.h> |
---|
41 | #include <freebsd/machine/rtems-bsd-sysinit.h> |
---|
42 | |
---|
43 | #include <freebsd/sys/param.h> |
---|
44 | #include <freebsd/sys/types.h> |
---|
45 | #include <freebsd/sys/systm.h> |
---|
46 | #include <freebsd/sys/bus.h> |
---|
47 | #include <freebsd/sys/kernel.h> |
---|
48 | #include <freebsd/sys/module.h> |
---|
49 | #include <freebsd/sys/rman.h> |
---|
50 | #include <freebsd/sys/malloc.h> |
---|
51 | |
---|
52 | #include <bsp.h> |
---|
53 | #include <freebsd/machine/rtems-bsd-devicet.h> |
---|
54 | #include <bsp/irq.h> |
---|
55 | #include <rtems/irq.h> |
---|
56 | #include <freebsd/machine/bus.h> |
---|
57 | |
---|
58 | /* XXX Note: These defines should be moved. */ |
---|
59 | #if defined(__i386__) |
---|
60 | #define BUS_SPACE_IO I386_BUS_SPACE_IO |
---|
61 | #define BUS_SPACE_MEM I386_BUS_SPACE_MEM |
---|
62 | #elif defined(__amd64__) |
---|
63 | #define BUS_SPACE_IO AMD64_BUS_SPACE_IO |
---|
64 | #define BUS_SPACE_MEM AMD64_BUS_SPACE_MEM |
---|
65 | #else |
---|
66 | #warning "Bus space information not implemented for this architecture!!" |
---|
67 | #warning "Defaulting Bus space information!!" |
---|
68 | #define BUS_SPACE_IO 0 |
---|
69 | #define BUS_SPACE_MEM 1 |
---|
70 | #endif |
---|
71 | |
---|
72 | /* XXX - Just a guess */ |
---|
73 | #define NUM_IO_INTS 30 |
---|
74 | |
---|
75 | #define DEVTONX(dev) ((struct nexus_device *)device_get_ivars(dev)) |
---|
76 | |
---|
77 | static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device"); |
---|
78 | struct rman irq_rman, drq_rman, port_rman, mem_rman; |
---|
79 | struct nexus_device { |
---|
80 | struct resource_list nx_resources; |
---|
81 | }; |
---|
82 | |
---|
83 | void |
---|
84 | nexus_init_resources(void) |
---|
85 | { |
---|
86 | int irq; |
---|
87 | |
---|
88 | /* |
---|
89 | * XXX working notes: |
---|
90 | * |
---|
91 | * - IRQ resource creation should be moved to the PIC/APIC driver. |
---|
92 | * - DRQ resource creation should be moved to the DMAC driver. |
---|
93 | * - The above should be sorted to probe earlier than any child busses. |
---|
94 | * |
---|
95 | * - Leave I/O and memory creation here, as child probes may need them. |
---|
96 | * (especially eg. ACPI) |
---|
97 | */ |
---|
98 | |
---|
99 | /* |
---|
100 | * IRQ's are on the mainboard on old systems, but on the ISA part |
---|
101 | * of PCI->ISA bridges. There would be multiple sets of IRQs on |
---|
102 | * multi-ISA-bus systems. PCI interrupts are routed to the ISA |
---|
103 | * component, so in a way, PCI can be a partial child of an ISA bus(!). |
---|
104 | * APIC interrupts are global though. |
---|
105 | */ |
---|
106 | irq_rman.rm_start = 0; |
---|
107 | irq_rman.rm_type = RMAN_ARRAY; |
---|
108 | irq_rman.rm_descr = "Interrupt request lines"; |
---|
109 | irq_rman.rm_end = NUM_IO_INTS - 1; |
---|
110 | if (rman_init(&irq_rman)) |
---|
111 | panic("nexus_init_resources irq_rman"); |
---|
112 | /* |
---|
113 | * We search for regions of existing IRQs and add those to the IRQ |
---|
114 | * resource manager. |
---|
115 | */ |
---|
116 | for (irq = 0; irq < NUM_IO_INTS; irq++) |
---|
117 | if (rman_manage_region(&irq_rman, irq, irq) != 0) |
---|
118 | panic("nexus_init_resources irq_rmand"); |
---|
119 | |
---|
120 | /* |
---|
121 | * ISA DMA on PCI systems is implemented in the ISA part of each |
---|
122 | * PCI->ISA bridge and the channels can be duplicated if there are |
---|
123 | * multiple bridges. (eg: laptops with docking stations) |
---|
124 | */ |
---|
125 | drq_rman.rm_start = 0; |
---|
126 | #ifdef PC98 |
---|
127 | drq_rman.rm_end = 3; |
---|
128 | #else |
---|
129 | drq_rman.rm_end = 7; |
---|
130 | #endif |
---|
131 | drq_rman.rm_type = RMAN_ARRAY; |
---|
132 | drq_rman.rm_descr = "DMA request lines"; |
---|
133 | /* XXX drq 0 not available on some machines */ |
---|
134 | if (rman_init(&drq_rman) |
---|
135 | || rman_manage_region(&drq_rman, |
---|
136 | drq_rman.rm_start, drq_rman.rm_end)) |
---|
137 | panic("nexus_init_resources drq_rman"); |
---|
138 | |
---|
139 | /* |
---|
140 | * However, IO ports and Memory truely are global at this level, |
---|
141 | * as are APIC interrupts (however many IO APICS there turn out |
---|
142 | * to be on large systems..) |
---|
143 | */ |
---|
144 | port_rman.rm_start = 0; |
---|
145 | port_rman.rm_end = 0xffff; |
---|
146 | port_rman.rm_type = RMAN_ARRAY; |
---|
147 | port_rman.rm_descr = "I/O ports"; |
---|
148 | if (rman_init(&port_rman) |
---|
149 | || rman_manage_region(&port_rman, 0, 0xffff)) |
---|
150 | panic("nexus_init_resources port_rman"); |
---|
151 | |
---|
152 | mem_rman.rm_start = 0; |
---|
153 | mem_rman.rm_end = ~0u; |
---|
154 | mem_rman.rm_type = RMAN_ARRAY; |
---|
155 | mem_rman.rm_descr = "I/O memory addresses"; |
---|
156 | if (rman_init(&mem_rman) |
---|
157 | || rman_manage_region(&mem_rman, 0, ~0)) |
---|
158 | panic("nexus_init_resources mem_rman"); |
---|
159 | } |
---|
160 | |
---|
161 | static int |
---|
162 | nexus_attach(device_t dev) |
---|
163 | { |
---|
164 | |
---|
165 | nexus_init_resources(); |
---|
166 | bus_generic_probe(dev); |
---|
167 | |
---|
168 | /* |
---|
169 | * Explicitly add the legacy0 device here. Other platform |
---|
170 | * types (such as ACPI), use their own nexus(4) subclass |
---|
171 | * driver to override this routine and add their own root bus. |
---|
172 | */ |
---|
173 | if (BUS_ADD_CHILD(dev, 10, "legacy", 0) == NULL) |
---|
174 | panic("legacy: could not attach"); |
---|
175 | bus_generic_attach(dev); |
---|
176 | return 0; |
---|
177 | } |
---|
178 | |
---|
179 | static int |
---|
180 | nexus_probe(device_t dev) |
---|
181 | { |
---|
182 | size_t unit = 0; |
---|
183 | #if 0 |
---|
184 | /* FIXME */ |
---|
185 | for (unit = 0; _bsd_nexus_devices [unit] != NULL; ++unit) { |
---|
186 | device_add_child(dev, _bsd_nexus_devices [unit], unit); |
---|
187 | } |
---|
188 | #endif |
---|
189 | device_set_desc(dev, "RTEMS Nexus device"); |
---|
190 | |
---|
191 | return (0); |
---|
192 | } |
---|
193 | static device_t |
---|
194 | nexus_add_child(device_t bus, u_int order, const char *name, int unit) |
---|
195 | { |
---|
196 | device_t child; |
---|
197 | struct nexus_device *ndev; |
---|
198 | |
---|
199 | ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO); |
---|
200 | if (!ndev) |
---|
201 | return(0); |
---|
202 | resource_list_init(&ndev->nx_resources); |
---|
203 | |
---|
204 | child = device_add_child_ordered(bus, order, name, unit); |
---|
205 | |
---|
206 | /* should we free this in nexus_child_detached? */ |
---|
207 | device_set_ivars(child, ndev); |
---|
208 | |
---|
209 | return(child); |
---|
210 | } |
---|
211 | |
---|
212 | /* |
---|
213 | * Allocate a resource on behalf of child. NB: child is usually going to be a |
---|
214 | * child of one of our descendants, not a direct child of nexus0. |
---|
215 | * (Exceptions include npx.) |
---|
216 | */ |
---|
217 | static struct resource * |
---|
218 | nexus_alloc_resource(device_t bus, device_t child, int type, int *rid, |
---|
219 | u_long start, u_long end, u_long count, u_int flags) |
---|
220 | { |
---|
221 | struct nexus_device *ndev = DEVTONX(child); |
---|
222 | struct resource *rv; |
---|
223 | struct resource_list_entry *rle; |
---|
224 | struct rman *rm; |
---|
225 | int needactivate = flags & RF_ACTIVE; |
---|
226 | |
---|
227 | /* |
---|
228 | * If this is an allocation of the "default" range for a given RID, and |
---|
229 | * we know what the resources for this device are (ie. they aren't maintained |
---|
230 | * by a child bus), then work out the start/end values. |
---|
231 | */ |
---|
232 | if ((start == 0UL) && (end == ~0UL) && (count == 1)) { |
---|
233 | if (ndev == NULL) |
---|
234 | return(NULL); |
---|
235 | rle = resource_list_find(&ndev->nx_resources, type, *rid); |
---|
236 | if (rle == NULL) |
---|
237 | return(NULL); |
---|
238 | start = rle->start; |
---|
239 | end = rle->end; |
---|
240 | count = rle->count; |
---|
241 | } |
---|
242 | |
---|
243 | flags &= ~RF_ACTIVE; |
---|
244 | |
---|
245 | switch (type) { |
---|
246 | case SYS_RES_IRQ: |
---|
247 | rm = &irq_rman; |
---|
248 | break; |
---|
249 | |
---|
250 | case SYS_RES_DRQ: |
---|
251 | rm = &drq_rman; |
---|
252 | break; |
---|
253 | |
---|
254 | case SYS_RES_IOPORT: |
---|
255 | rm = &port_rman; |
---|
256 | break; |
---|
257 | |
---|
258 | case SYS_RES_MEMORY: |
---|
259 | rm = &mem_rman; |
---|
260 | break; |
---|
261 | |
---|
262 | default: |
---|
263 | return 0; |
---|
264 | } |
---|
265 | |
---|
266 | rv = rman_reserve_resource(rm, start, end, count, flags, child); |
---|
267 | if (rv == 0) |
---|
268 | return 0; |
---|
269 | rman_set_rid(rv, *rid); |
---|
270 | |
---|
271 | if (needactivate) { |
---|
272 | if (bus_activate_resource(child, type, *rid, rv)) { |
---|
273 | rman_release_resource(rv); |
---|
274 | return 0; |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | return rv; |
---|
279 | } |
---|
280 | |
---|
281 | static int |
---|
282 | nexus_activate_resource(device_t bus, device_t child, int type, int rid, |
---|
283 | struct resource *r) |
---|
284 | { |
---|
285 | #ifdef PC98 |
---|
286 | bus_space_handle_t bh; |
---|
287 | int error; |
---|
288 | #endif |
---|
289 | void *vaddr; |
---|
290 | |
---|
291 | /* |
---|
292 | * If this is a memory resource, map it into the kernel. |
---|
293 | */ |
---|
294 | switch (type) { |
---|
295 | case SYS_RES_IOPORT: |
---|
296 | #ifdef PC98 |
---|
297 | error = i386_bus_space_handle_alloc(I386_BUS_SPACE_IO, |
---|
298 | rman_get_start(r), rman_get_size(r), &bh); |
---|
299 | if (error) |
---|
300 | return (error); |
---|
301 | rman_set_bushandle(r, bh); |
---|
302 | #else |
---|
303 | rman_set_bushandle(r, rman_get_start(r)); |
---|
304 | #endif |
---|
305 | rman_set_bustag(r, BUS_SPACE_IO); |
---|
306 | break; |
---|
307 | case SYS_RES_MEMORY: |
---|
308 | #ifdef PC98 |
---|
309 | error = i386_bus_space_handle_alloc(I386_BUS_SPACE_MEM, |
---|
310 | rman_get_start(r), rman_get_size(r), &bh); |
---|
311 | if (error) |
---|
312 | return (error); |
---|
313 | #endif |
---|
314 | rman_set_bustag(r, BUS_SPACE_MEM); |
---|
315 | #ifdef PC98 |
---|
316 | /* PC-98: the type of bus_space_handle_t is the structure. */ |
---|
317 | bh->bsh_base = (bus_addr_t) vaddr; |
---|
318 | rman_set_bushandle(r, bh); |
---|
319 | #else |
---|
320 | /* IBM-PC: the type of bus_space_handle_t is u_int */ |
---|
321 | rman_set_bushandle(r, (bus_space_handle_t) vaddr); |
---|
322 | #endif |
---|
323 | } |
---|
324 | return (rman_activate_resource(r)); |
---|
325 | } |
---|
326 | |
---|
327 | static int |
---|
328 | nexus_deactivate_resource(device_t bus, device_t child, int type, int rid, |
---|
329 | struct resource *r) |
---|
330 | { |
---|
331 | |
---|
332 | #ifdef PC98 |
---|
333 | if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) { |
---|
334 | bus_space_handle_t bh; |
---|
335 | |
---|
336 | bh = rman_get_bushandle(r); |
---|
337 | i386_bus_space_handle_free(rman_get_bustag(r), bh, bh->bsh_sz); |
---|
338 | } |
---|
339 | #endif |
---|
340 | return (rman_deactivate_resource(r)); |
---|
341 | } |
---|
342 | |
---|
343 | static int |
---|
344 | nexus_release_resource(device_t bus, device_t child, int type, int rid, |
---|
345 | struct resource *r) |
---|
346 | { |
---|
347 | if (rman_get_flags(r) & RF_ACTIVE) { |
---|
348 | int error = bus_deactivate_resource(child, type, rid, r); |
---|
349 | if (error) |
---|
350 | return error; |
---|
351 | } |
---|
352 | return (rman_release_resource(r)); |
---|
353 | } |
---|
354 | |
---|
355 | static void noop(const rtems_irq_connect_data *unused) {}; |
---|
356 | static int noop1(const rtems_irq_connect_data *unused) { return 0;}; |
---|
357 | |
---|
358 | static int |
---|
359 | bspExtInstallSharedISR(int irqLine, void (*isr)(void *), void * uarg, int flags) |
---|
360 | { |
---|
361 | return rtems_interrupt_handler_install( |
---|
362 | irqLine, |
---|
363 | "BSD Interrupt", |
---|
364 | RTEMS_INTERRUPT_SHARED, |
---|
365 | isr, |
---|
366 | uarg |
---|
367 | ); |
---|
368 | } |
---|
369 | |
---|
370 | int |
---|
371 | intr_add_handler(const char *name, int vector, driver_filter_t filter, |
---|
372 | driver_intr_t handler, void *arg, enum intr_type flags, void **cookiep) |
---|
373 | { |
---|
374 | int rval; |
---|
375 | |
---|
376 | rval = bspExtInstallSharedISR(vector, handler, arg, 0); |
---|
377 | return rval; |
---|
378 | } |
---|
379 | |
---|
380 | static int |
---|
381 | nexus_setup_intr(device_t bus, device_t child, struct resource *irq, |
---|
382 | int flags, driver_filter_t filter, void (*ihand)(void *), |
---|
383 | void *arg, void **cookiep) |
---|
384 | { |
---|
385 | int error; |
---|
386 | |
---|
387 | /* somebody tried to setup an irq that failed to allocate! */ |
---|
388 | if (irq == NULL) |
---|
389 | panic("nexus_setup_intr: NULL irq resource!"); |
---|
390 | |
---|
391 | *cookiep = 0; |
---|
392 | if ((rman_get_flags(irq) & RF_SHAREABLE) == 0) |
---|
393 | flags |= INTR_EXCL; |
---|
394 | |
---|
395 | /* |
---|
396 | * We depend here on rman_activate_resource() being idempotent. |
---|
397 | */ |
---|
398 | error = rman_activate_resource(irq); |
---|
399 | if (error) |
---|
400 | return (error); |
---|
401 | |
---|
402 | error = intr_add_handler(device_get_nameunit(child), |
---|
403 | rman_get_start(irq), filter, ihand, arg, flags, cookiep); |
---|
404 | |
---|
405 | return (error); |
---|
406 | } |
---|
407 | |
---|
408 | |
---|
409 | static device_method_t nexus_methods [] = { |
---|
410 | /* Device interface */ |
---|
411 | DEVMETHOD(device_probe, nexus_probe), |
---|
412 | DEVMETHOD(device_attach, nexus_attach), |
---|
413 | DEVMETHOD(device_detach, bus_generic_detach), |
---|
414 | DEVMETHOD(device_shutdown, bus_generic_shutdown), |
---|
415 | DEVMETHOD(device_suspend, bus_generic_suspend), |
---|
416 | DEVMETHOD(device_resume, bus_generic_resume), |
---|
417 | |
---|
418 | /* Bus interface */ |
---|
419 | DEVMETHOD(bus_print_child, bus_generic_print_child), |
---|
420 | DEVMETHOD(bus_add_child, nexus_add_child), |
---|
421 | DEVMETHOD(bus_alloc_resource, nexus_alloc_resource), |
---|
422 | DEVMETHOD(bus_release_resource, nexus_release_resource), |
---|
423 | DEVMETHOD(bus_activate_resource, nexus_activate_resource), |
---|
424 | DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource), |
---|
425 | DEVMETHOD(bus_setup_intr, nexus_setup_intr), |
---|
426 | |
---|
427 | { 0, 0 } |
---|
428 | }; |
---|
429 | |
---|
430 | static driver_t nexus_driver = { |
---|
431 | .name = "nexus", |
---|
432 | .methods = nexus_methods, |
---|
433 | .size = 0 |
---|
434 | }; |
---|
435 | |
---|
436 | static devclass_t nexus_devclass; |
---|
437 | |
---|
438 | DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0); |
---|