wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 1 | /* |
Graeme Russ | dbf7115 | 2011-04-13 19:43:26 +1000 | [diff] [blame] | 2 | * (C) Copyright 2008-2011 |
| 3 | * Graeme Russ, <graeme.russ@gmail.com> |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 4 | * |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 5 | * (C) Copyright 2002 |
Albert ARIBAUD | fa82f87 | 2011-08-04 18:45:45 +0200 | [diff] [blame] | 6 | * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se> |
Graeme Russ | dbf7115 | 2011-04-13 19:43:26 +1000 | [diff] [blame] | 7 | * |
| 8 | * (C) Copyright 2002 |
| 9 | * Wolfgang Denk, DENX Software Engineering, <wd@denx.de> |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 10 | * |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 11 | * (C) Copyright 2002 |
| 12 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 13 | * Marius Groeger <mgroeger@sysgo.de> |
| 14 | * |
| 15 | * See file CREDITS for list of people who contributed to this |
| 16 | * project. |
| 17 | * |
| 18 | * This program is free software; you can redistribute it and/or |
| 19 | * modify it under the terms of the GNU General Public License as |
| 20 | * published by the Free Software Foundation; either version 2 of |
| 21 | * the License, or (at your option) any later version. |
| 22 | * |
| 23 | * This program is distributed in the hope that it will be useful, |
| 24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 26 | * GNU General Public License for more details. |
| 27 | * |
| 28 | * You should have received a copy of the GNU General Public License |
| 29 | * along with this program; if not, write to the Free Software |
| 30 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 31 | * MA 02111-1307 USA |
| 32 | */ |
| 33 | |
| 34 | #include <common.h> |
| 35 | #include <watchdog.h> |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 36 | #include <stdio_dev.h> |
Graeme Russ | fea2572 | 2011-04-13 19:43:28 +1000 | [diff] [blame] | 37 | #include <asm/u-boot-x86.h> |
Graeme Russ | 9e6c572 | 2011-12-31 22:58:15 +1100 | [diff] [blame] | 38 | #include <asm/processor.h> |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 39 | |
Graeme Russ | d47ab0e | 2011-12-23 16:51:29 +1100 | [diff] [blame^] | 40 | #include <asm/init_helpers.h> |
| 41 | #include <asm/init_wrappers.h> |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 42 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 43 | /* |
| 44 | * Breath some life into the board... |
| 45 | * |
| 46 | * Initialize an SMC for serial comms, and carry out some hardware |
| 47 | * tests. |
| 48 | * |
| 49 | * The first part of initialization is running from Flash memory; |
| 50 | * its main purpose is to initialize the RAM so that we |
| 51 | * can relocate the monitor code to RAM. |
| 52 | */ |
| 53 | |
| 54 | /* |
| 55 | * All attempts to come up with a "common" initialization sequence |
| 56 | * that works for all boards and architectures failed: some of the |
| 57 | * requirements are just _too_ different. To get rid of the resulting |
| 58 | * mess of board dependend #ifdef'ed code we now make the whole |
| 59 | * initialization sequence configurable to the user. |
| 60 | * |
| 61 | * The requirements for any new initalization function is simple: it |
| 62 | * receives a pointer to the "global data" structure as it's only |
| 63 | * argument, and returns an integer return code, where 0 means |
| 64 | * "continue" and != 0 means "fatal error, hang the system". |
| 65 | */ |
| 66 | typedef int (init_fnc_t) (void); |
| 67 | |
Graeme Russ | e4f78d7 | 2011-02-12 15:12:10 +1100 | [diff] [blame] | 68 | static int calculate_relocation_address(void); |
Graeme Russ | 9e6c572 | 2011-12-31 22:58:15 +1100 | [diff] [blame] | 69 | static int copy_gd_to_ram(void); |
Graeme Russ | e4f78d7 | 2011-02-12 15:12:10 +1100 | [diff] [blame] | 70 | |
| 71 | init_fnc_t *init_sequence_f[] = { |
| 72 | cpu_init_f, |
| 73 | board_early_init_f, |
| 74 | env_init, |
Graeme Russ | d47ab0e | 2011-12-23 16:51:29 +1100 | [diff] [blame^] | 75 | init_baudrate_f, |
Graeme Russ | e4f78d7 | 2011-02-12 15:12:10 +1100 | [diff] [blame] | 76 | serial_init, |
| 77 | console_init_f, |
| 78 | dram_init_f, |
| 79 | calculate_relocation_address, |
Graeme Russ | e4f78d7 | 2011-02-12 15:12:10 +1100 | [diff] [blame] | 80 | |
| 81 | NULL, |
| 82 | }; |
| 83 | |
| 84 | init_fnc_t *init_sequence_r[] = { |
Graeme Russ | d47ab0e | 2011-12-23 16:51:29 +1100 | [diff] [blame^] | 85 | init_bd_struct_r, |
| 86 | mem_malloc_init_r, |
| 87 | cpu_init_r, |
| 88 | board_early_init_r, |
| 89 | dram_init, |
| 90 | interrupt_init, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 91 | timer_init, |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 92 | display_banner, |
| 93 | display_dram_config, |
Graeme Russ | d47ab0e | 2011-12-23 16:51:29 +1100 | [diff] [blame^] | 94 | #ifdef CONFIG_SERIAL_MULTI |
| 95 | serial_initialize_r, |
| 96 | #endif |
| 97 | #ifndef CONFIG_SYS_NO_FLASH |
| 98 | flash_init_r, |
| 99 | #endif |
| 100 | env_relocate_r, |
| 101 | #ifdef CONFIG_CMD_NET |
| 102 | init_ip_address_r, |
| 103 | #endif |
| 104 | #ifdef CONFIG_PCI |
| 105 | pci_init_r, |
| 106 | #endif |
| 107 | stdio_init, |
| 108 | jumptable_init_r, |
| 109 | console_init_r, |
| 110 | #ifdef CONFIG_MISC_INIT_R |
| 111 | misc_init_r, |
| 112 | #endif |
| 113 | #if defined(CONFIG_CMD_PCMCIA) && !defined(CONFIG_CMD_IDE) |
| 114 | pci_init_r, |
| 115 | #endif |
| 116 | #if defined(CONFIG_CMD_KGDB) |
| 117 | kgdb_init_r, |
| 118 | #endif |
| 119 | enable_interrupts_r, |
| 120 | #ifdef CONFIG_STATUS_LED |
| 121 | status_led_set_r, |
| 122 | #endif |
| 123 | set_load_addr_r, |
| 124 | #if defined(CONFIG_CMD_NET) |
| 125 | set_bootfile_r, |
| 126 | #endif |
| 127 | #if defined(CONFIG_CMD_IDE) |
| 128 | ide_init_r, |
| 129 | #endif |
| 130 | #if defined(CONFIG_CMD_SCSI) |
| 131 | scsi_init_r, |
| 132 | #endif |
| 133 | #if defined(CONFIG_CMD_DOC) |
| 134 | doc_init_r, |
| 135 | #endif |
| 136 | #ifdef CONFIG_BITBANGMII |
| 137 | bb_miiphy_init_r, |
| 138 | #endif |
| 139 | #if defined(CONFIG_CMD_NET) |
| 140 | eth_initialize_r, |
| 141 | #ifdef CONFIG_RESET_PHY_R |
| 142 | reset_phy_r, |
| 143 | #endif |
| 144 | #endif |
| 145 | #ifdef CONFIG_LAST_STAGE_INIT |
| 146 | last_stage_init, |
| 147 | #endif |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 148 | NULL, |
| 149 | }; |
| 150 | |
Graeme Russ | d47ab0e | 2011-12-23 16:51:29 +1100 | [diff] [blame^] | 151 | static void do_init_loop(init_fnc_t **init_fnc_ptr) |
| 152 | { |
| 153 | for (; *init_fnc_ptr; ++init_fnc_ptr) { |
| 154 | WATCHDOG_RESET(); |
| 155 | if ((*init_fnc_ptr)() != 0) |
| 156 | hang(); |
| 157 | } |
| 158 | } |
| 159 | |
Graeme Russ | 71a5404 | 2011-02-12 15:12:06 +1100 | [diff] [blame] | 160 | static int calculate_relocation_address(void) |
| 161 | { |
Graeme Russ | 303418c | 2011-11-08 02:33:21 +0000 | [diff] [blame] | 162 | ulong text_start = (ulong)&__text_start; |
| 163 | ulong bss_end = (ulong)&__bss_end; |
| 164 | ulong dest_addr; |
Graeme Russ | 303418c | 2011-11-08 02:33:21 +0000 | [diff] [blame] | 165 | |
| 166 | /* |
Graeme Russ | 240ab5a | 2012-01-01 15:57:02 +1100 | [diff] [blame] | 167 | * NOTE: All destination address are rounded down to 16-byte |
| 168 | * boundary to satisfy various worst-case alignment |
| 169 | * requirements |
Graeme Russ | 303418c | 2011-11-08 02:33:21 +0000 | [diff] [blame] | 170 | */ |
Graeme Russ | 240ab5a | 2012-01-01 15:57:02 +1100 | [diff] [blame] | 171 | |
Graeme Russ | 9e6c572 | 2011-12-31 22:58:15 +1100 | [diff] [blame] | 172 | /* Global Data is at top of available memory */ |
Graeme Russ | 240ab5a | 2012-01-01 15:57:02 +1100 | [diff] [blame] | 173 | dest_addr = gd->ram_size; |
Graeme Russ | 9e6c572 | 2011-12-31 22:58:15 +1100 | [diff] [blame] | 174 | dest_addr -= GENERATED_GBL_DATA_SIZE; |
| 175 | dest_addr &= ~15; |
| 176 | gd->new_gd_addr = dest_addr; |
| 177 | |
| 178 | /* GDT is below Global Data */ |
| 179 | dest_addr -= X86_GDT_SIZE; |
| 180 | dest_addr &= ~15; |
| 181 | gd->gdt_addr = dest_addr; |
| 182 | |
| 183 | /* Stack is below GDT */ |
Graeme Russ | 240ab5a | 2012-01-01 15:57:02 +1100 | [diff] [blame] | 184 | gd->start_addr_sp = dest_addr; |
| 185 | |
| 186 | /* U-Boot is below the stack */ |
| 187 | dest_addr -= CONFIG_SYS_STACK_SIZE; |
| 188 | dest_addr -= (bss_end - text_start); |
Graeme Russ | 303418c | 2011-11-08 02:33:21 +0000 | [diff] [blame] | 189 | dest_addr &= ~15; |
Graeme Russ | 303418c | 2011-11-08 02:33:21 +0000 | [diff] [blame] | 190 | gd->relocaddr = dest_addr; |
Graeme Russ | 240ab5a | 2012-01-01 15:57:02 +1100 | [diff] [blame] | 191 | gd->reloc_off = (dest_addr - text_start); |
Graeme Russ | 71a5404 | 2011-02-12 15:12:06 +1100 | [diff] [blame] | 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
Graeme Russ | d47ab0e | 2011-12-23 16:51:29 +1100 | [diff] [blame^] | 196 | /* Perform all steps necessary to get RAM initialised ready for relocation */ |
Graeme Russ | 96cd664 | 2011-02-12 15:11:54 +1100 | [diff] [blame] | 197 | void board_init_f(ulong boot_flags) |
Graeme Russ | 1c409bc | 2009-11-24 20:04:21 +1100 | [diff] [blame] | 198 | { |
Graeme Russ | dbf7115 | 2011-04-13 19:43:26 +1000 | [diff] [blame] | 199 | gd->flags = boot_flags; |
| 200 | |
Graeme Russ | d47ab0e | 2011-12-23 16:51:29 +1100 | [diff] [blame^] | 201 | do_init_loop(init_sequence_f); |
Graeme Russ | f2ff75c | 2010-10-07 20:03:33 +1100 | [diff] [blame] | 202 | |
Graeme Russ | f48dd6f | 2012-01-01 15:06:39 +1100 | [diff] [blame] | 203 | /* |
| 204 | * SDRAM is now initialised, U-Boot has been copied into SDRAM, |
| 205 | * the BSS has been cleared etc. The final stack can now be setup |
| 206 | * in SDRAM. Code execution will continue (momentarily) in Flash, |
| 207 | * but with the stack in SDRAM and Global Data in temporary memory |
| 208 | * (CPU cache) |
| 209 | */ |
| 210 | board_init_f_r_trampoline(gd->start_addr_sp); |
Graeme Russ | 161b358 | 2010-10-07 20:03:29 +1100 | [diff] [blame] | 211 | |
Graeme Russ | f48dd6f | 2012-01-01 15:06:39 +1100 | [diff] [blame] | 212 | /* NOTREACHED - board_init_f_r_trampoline() does not return */ |
| 213 | while (1) |
| 214 | ; |
| 215 | } |
| 216 | |
| 217 | void board_init_f_r(void) |
| 218 | { |
Graeme Russ | 98f1fa9 | 2011-12-27 22:46:42 +1100 | [diff] [blame] | 219 | if (copy_gd_to_ram() != 0) |
| 220 | hang(); |
| 221 | |
| 222 | if (init_cache() != 0) |
| 223 | hang(); |
| 224 | |
Graeme Russ | b156ff0 | 2011-12-23 15:57:58 +1100 | [diff] [blame] | 225 | relocate_code(0, gd, 0); |
Graeme Russ | 98f1fa9 | 2011-12-27 22:46:42 +1100 | [diff] [blame] | 226 | |
Graeme Russ | b156ff0 | 2011-12-23 15:57:58 +1100 | [diff] [blame] | 227 | /* NOTREACHED - relocate_code() does not return */ |
Graeme Russ | 83088af | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 228 | while (1) |
| 229 | ; |
Graeme Russ | 1c409bc | 2009-11-24 20:04:21 +1100 | [diff] [blame] | 230 | } |
| 231 | |
Graeme Russ | 9e6c572 | 2011-12-31 22:58:15 +1100 | [diff] [blame] | 232 | static int copy_gd_to_ram(void) |
| 233 | { |
| 234 | gd_t *ram_gd; |
| 235 | |
| 236 | /* |
| 237 | * Global data is still in temporary memory (the CPU cache). |
| 238 | * calculate_relocation_address() has set gd->new_gd_addr to |
| 239 | * where the global data lives in RAM but getting it there |
| 240 | * safely is a bit tricky due to the 'F-Segment Hack' that |
| 241 | * we need to use for x86 |
| 242 | */ |
| 243 | ram_gd = (gd_t *)gd->new_gd_addr; |
| 244 | memcpy((void *)ram_gd, gd, sizeof(gd_t)); |
| 245 | |
| 246 | /* |
| 247 | * Reload the Global Descriptor Table so FS points to the |
| 248 | * in-RAM copy of Global Data (calculate_relocation_address() |
| 249 | * has already calculated the in-RAM location of the GDT) |
| 250 | */ |
| 251 | ram_gd->gd_addr = (ulong)ram_gd; |
| 252 | init_gd(ram_gd, (u64 *)gd->gdt_addr); |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
Graeme Russ | 2fb1bc4 | 2010-04-24 00:05:44 +1000 | [diff] [blame] | 257 | void board_init_r(gd_t *id, ulong dest_addr) |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 258 | { |
Graeme Russ | d47ab0e | 2011-12-23 16:51:29 +1100 | [diff] [blame^] | 259 | gd->flags |= GD_FLG_RELOC; |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 260 | |
wdenk | 93f6a67 | 2004-07-01 20:28:03 +0000 | [diff] [blame] | 261 | /* compiler optimization barrier needed for GCC >= 3.4 */ |
Graeme Russ | 83088af | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 262 | __asm__ __volatile__("" : : : "memory"); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 263 | |
Graeme Russ | d47ab0e | 2011-12-23 16:51:29 +1100 | [diff] [blame^] | 264 | do_init_loop(init_sequence_r); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 265 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 266 | /* main_loop() can return to retry autoboot, if so just run it again. */ |
Graeme Russ | 83088af | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 267 | for (;;) |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 268 | main_loop(); |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 269 | |
| 270 | /* NOTREACHED - no way out of command loop except booting */ |
| 271 | } |
| 272 | |
Graeme Russ | 83088af | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 273 | void hang(void) |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 274 | { |
Graeme Russ | 83088af | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 275 | puts("### ERROR ### Please RESET the board ###\n"); |
| 276 | for (;;) |
| 277 | ; |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 278 | } |