[ef37cb6] | 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 |
---|
[c499856] | 7 | * http://www.rtems.org/license/LICENSE. |
---|
[ef37cb6] | 8 | */ |
---|
| 9 | |
---|
[9559674] | 10 | /* #define BSP_GET_WORK_AREA_DEBUG */ |
---|
[ef37cb6] | 11 | #include <bsp.h> |
---|
| 12 | #include <bsp/bootcard.h> |
---|
| 13 | |
---|
[eea21eac] | 14 | #include <rtems/sysinit.h> |
---|
| 15 | |
---|
[9559674] | 16 | #ifdef BSP_GET_WORK_AREA_DEBUG |
---|
| 17 | #include <rtems/bspIo.h> |
---|
| 18 | #endif |
---|
| 19 | |
---|
[ef37cb6] | 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 | |
---|
[c2966871] | 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 | |
---|
[ef37cb6] | 45 | /* |
---|
| 46 | * Board's memory size easily be overridden by application. |
---|
| 47 | */ |
---|
[e0d83f69] | 48 | uint32_t bsp_mem_size = 0; |
---|
[ef37cb6] | 49 | |
---|
[37030e3] | 50 | static void bsp_size_memory(void) |
---|
[ef37cb6] | 51 | { |
---|
| 52 | uintptr_t topAddr; |
---|
| 53 | |
---|
[c2966871] | 54 | /* Set the value of start of free memory. */ |
---|
[a13b89b] | 55 | rtemsWorkAreaStart = (uint32_t)WorkAreaBase; |
---|
[ef37cb6] | 56 | |
---|
[c2966871] | 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); |
---|
[ef37cb6] | 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 ) { |
---|
[c2966871] | 67 | topAddr = _boot_multiboot_info.mem_upper * 1024; |
---|
[9559674] | 68 | #ifdef BSP_GET_WORK_AREA_DEBUG |
---|
[c2966871] | 69 | printk( "Multiboot info says we have 0x%08x\n", topAddr ); |
---|
[9559674] | 70 | #endif |
---|
[c2966871] | 71 | } else if ( (uintptr_t) RamSize == (uintptr_t) 0xFFFFFFFF ) { |
---|
| 72 | uintptr_t lowest; |
---|
| 73 | uint32_t val; |
---|
| 74 | int i; |
---|
[ef37cb6] | 75 | |
---|
| 76 | /* |
---|
| 77 | * We have to dynamically size memory. Memory size can be anything |
---|
[c2966871] | 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. |
---|
[ef37cb6] | 83 | */ |
---|
[c2966871] | 84 | |
---|
| 85 | /* find the lowest 1M boundary to probe */ |
---|
| 86 | lowest = ((rtemsWorkAreaStart + (1<<20)) >> 20) + 1; |
---|
| 87 | if ( lowest < 2 ) |
---|
| 88 | lowest = 2; |
---|
| 89 | |
---|
[ef37cb6] | 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; |
---|
[9559674] | 97 | val = *(volatile uint32_t*)topAddr; |
---|
[ef37cb6] | 98 | if (val != topAddr) { |
---|
| 99 | break; |
---|
| 100 | } |
---|
| 101 | } |
---|
[359e537] | 102 | |
---|
[c2966871] | 103 | topAddr = (i-1)*1024*1024; |
---|
[9559674] | 104 | #ifdef BSP_GET_WORK_AREA_DEBUG |
---|
| 105 | printk( "Dynamically sized to 0x%08x\n", topAddr ); |
---|
| 106 | #endif |
---|
[ef37cb6] | 107 | } else { |
---|
| 108 | topAddr = (uintptr_t) RamSize; |
---|
[9559674] | 109 | #ifdef BSP_GET_WORK_AREA_DEBUG |
---|
| 110 | printk( "hardcoded to 0x%08x\n", topAddr ); |
---|
| 111 | #endif |
---|
[ef37cb6] | 112 | } |
---|
| 113 | |
---|
| 114 | bsp_mem_size = topAddr; |
---|
| 115 | } |
---|
[c2966871] | 116 | |
---|
[eea21eac] | 117 | static Memory_Area _Memory_Areas[ 1 ]; |
---|
[47a3cd8] | 118 | |
---|
[eea21eac] | 119 | static void bsp_memory_initialize( void ) |
---|
| 120 | { |
---|
[37030e3] | 121 | /* |
---|
| 122 | * We need to determine how much memory there is in the system. |
---|
| 123 | */ |
---|
| 124 | bsp_size_memory(); |
---|
| 125 | |
---|
[eea21eac] | 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; |
---|
[ef37cb6] | 145 | } |
---|