1 | /* |
---|
2 | * COPYRIGHT (c) 1989-2008. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be |
---|
6 | * found in the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.org/license/LICENSE. |
---|
8 | */ |
---|
9 | |
---|
10 | /* #define BSP_GET_WORK_AREA_DEBUG */ |
---|
11 | #include <bsp.h> |
---|
12 | #include <bsp/bootcard.h> |
---|
13 | |
---|
14 | #include <rtems/sysinit.h> |
---|
15 | |
---|
16 | #ifdef BSP_GET_WORK_AREA_DEBUG |
---|
17 | #include <rtems/bspIo.h> |
---|
18 | #endif |
---|
19 | |
---|
20 | /* |
---|
21 | * These are provided by the linkcmds. |
---|
22 | */ |
---|
23 | extern char WorkAreaBase[]; |
---|
24 | extern char HeapSize[]; |
---|
25 | extern char RamSize[]; |
---|
26 | |
---|
27 | /* rudimentary multiboot info */ |
---|
28 | struct multiboot_info { |
---|
29 | uint32_t flags; /* start.S only raises flags for items actually */ |
---|
30 | /* saved; this allows us to check for the size */ |
---|
31 | /* of the data structure. */ |
---|
32 | uint32_t mem_lower; /* avail kB in lower memory */ |
---|
33 | uint32_t mem_upper; /* avail kB in lower memory */ |
---|
34 | /* ... (unimplemented) */ |
---|
35 | }; |
---|
36 | |
---|
37 | extern struct multiboot_info _boot_multiboot_info; |
---|
38 | |
---|
39 | /* |
---|
40 | * This is the first address of the memory we can use for the RTEMS |
---|
41 | * Work Area. |
---|
42 | */ |
---|
43 | static uintptr_t rtemsWorkAreaStart; |
---|
44 | |
---|
45 | /* |
---|
46 | * Board's memory size easily be overridden by application. |
---|
47 | */ |
---|
48 | uint32_t bsp_mem_size = 0; |
---|
49 | |
---|
50 | static void bsp_size_memory(void) |
---|
51 | { |
---|
52 | uintptr_t topAddr; |
---|
53 | |
---|
54 | /* Set the value of start of free memory. */ |
---|
55 | rtemsWorkAreaStart = (uint32_t)WorkAreaBase; |
---|
56 | |
---|
57 | /* Align the RTEMS Work Area at beginning of free memory. */ |
---|
58 | if (rtemsWorkAreaStart & (CPU_ALIGNMENT - 1)) /* not aligned => align it */ |
---|
59 | rtemsWorkAreaStart = (rtemsWorkAreaStart+CPU_ALIGNMENT) & ~(CPU_ALIGNMENT-1); |
---|
60 | |
---|
61 | /* The memory detection algorithm is very crude; try |
---|
62 | * to use multiboot info, if possible (set from start.S) |
---|
63 | */ |
---|
64 | if ( ((uintptr_t)RamSize == (uintptr_t) 0xFFFFFFFF) && |
---|
65 | (_boot_multiboot_info.flags & 1) && |
---|
66 | _boot_multiboot_info.mem_upper ) { |
---|
67 | topAddr = _boot_multiboot_info.mem_upper * 1024; |
---|
68 | #ifdef BSP_GET_WORK_AREA_DEBUG |
---|
69 | printk( "Multiboot info says we have 0x%08x\n", topAddr ); |
---|
70 | #endif |
---|
71 | } else if ( (uintptr_t) RamSize == (uintptr_t) 0xFFFFFFFF ) { |
---|
72 | uintptr_t lowest; |
---|
73 | uint32_t val; |
---|
74 | int i; |
---|
75 | |
---|
76 | /* |
---|
77 | * We have to dynamically size memory. Memory size can be anything |
---|
78 | * between no less than 2M and 2048M. If we can write a value to |
---|
79 | * an address and read the same value back, then the memory is there. |
---|
80 | * |
---|
81 | * WARNING: This can detect memory which should be reserved for |
---|
82 | * graphics controllers which share the CPU's RAM. |
---|
83 | */ |
---|
84 | |
---|
85 | /* find the lowest 1M boundary to probe */ |
---|
86 | lowest = ((rtemsWorkAreaStart + (1<<20)) >> 20) + 1; |
---|
87 | if ( lowest < 2 ) |
---|
88 | lowest = 2; |
---|
89 | |
---|
90 | for (i=2048; i>=lowest; i--) { |
---|
91 | topAddr = i*1024*1024 - 4; |
---|
92 | *(volatile uint32_t*)topAddr = topAddr; |
---|
93 | } |
---|
94 | |
---|
95 | for(i=lowest; i<=2048; i++) { |
---|
96 | topAddr = i*1024*1024 - 4; |
---|
97 | val = *(volatile uint32_t*)topAddr; |
---|
98 | if (val != topAddr) { |
---|
99 | break; |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | topAddr = (i-1)*1024*1024; |
---|
104 | #ifdef BSP_GET_WORK_AREA_DEBUG |
---|
105 | printk( "Dynamically sized to 0x%08x\n", topAddr ); |
---|
106 | #endif |
---|
107 | } else { |
---|
108 | topAddr = (uintptr_t) RamSize; |
---|
109 | #ifdef BSP_GET_WORK_AREA_DEBUG |
---|
110 | printk( "hardcoded to 0x%08x\n", topAddr ); |
---|
111 | #endif |
---|
112 | } |
---|
113 | |
---|
114 | bsp_mem_size = topAddr; |
---|
115 | } |
---|
116 | |
---|
117 | static Memory_Area _Memory_Areas[ 1 ]; |
---|
118 | |
---|
119 | static void bsp_memory_initialize( void ) |
---|
120 | { |
---|
121 | /* |
---|
122 | * We need to determine how much memory there is in the system. |
---|
123 | */ |
---|
124 | bsp_size_memory(); |
---|
125 | |
---|
126 | _Memory_Initialize_by_size( |
---|
127 | &_Memory_Areas[0], |
---|
128 | (void *) rtemsWorkAreaStart, |
---|
129 | (uintptr_t) bsp_mem_size - (uintptr_t) rtemsWorkAreaStart |
---|
130 | ); |
---|
131 | } |
---|
132 | |
---|
133 | RTEMS_SYSINIT_ITEM( |
---|
134 | bsp_memory_initialize, |
---|
135 | RTEMS_SYSINIT_MEMORY, |
---|
136 | RTEMS_SYSINIT_ORDER_MIDDLE |
---|
137 | ); |
---|
138 | |
---|
139 | static const Memory_Information _Memory_Information = |
---|
140 | MEMORY_INFORMATION_INITIALIZER( _Memory_Areas ); |
---|
141 | |
---|
142 | const Memory_Information *_Memory_Get( void ) |
---|
143 | { |
---|
144 | return &_Memory_Information; |
---|
145 | } |
---|