Simon Glass | ca96f65 | 2011-10-03 19:26:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The Chromium OS Authors. |
| 3 | * |
| 4 | * (C) Copyright 2002-2006 |
| 5 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 6 | * |
| 7 | * (C) Copyright 2002 |
| 8 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 9 | * Marius Groeger <mgroeger@sysgo.de> |
| 10 | * |
| 11 | * See file CREDITS for list of people who contributed to this |
| 12 | * project. |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of |
| 17 | * the License, or (at your option) any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program; if not, write to the Free Software |
| 26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 27 | * MA 02111-1307 USA |
| 28 | */ |
| 29 | |
| 30 | /* |
| 31 | * This file was taken from ARM and changed to remove things we don't |
| 32 | * need. This is most of it, so have tried to avoid being over-zealous! |
| 33 | * For example, we want to have an emulation of the 'DRAM' used by |
| 34 | * U-Boot. |
| 35 | * |
| 36 | * has been talk upstream of unifying the architectures w.r.t board.c, |
| 37 | * so the less change here the better. |
| 38 | */ |
| 39 | |
| 40 | #include <common.h> |
| 41 | #include <command.h> |
| 42 | #include <malloc.h> |
| 43 | #include <stdio_dev.h> |
| 44 | #include <timestamp.h> |
| 45 | #include <version.h> |
| 46 | #include <serial.h> |
| 47 | |
| 48 | DECLARE_GLOBAL_DATA_PTR; |
| 49 | |
| 50 | /************************************************************************ |
| 51 | * Init Utilities * |
| 52 | ************************************************************************ |
| 53 | * Some of this code should be moved into the core functions, |
| 54 | * or dropped completely, |
| 55 | * but let's get it working (again) first... |
| 56 | */ |
| 57 | |
| 58 | static int display_banner(void) |
| 59 | { |
| 60 | display_options(); |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Configure and report on the DRAM configuration, which in our case is |
| 67 | * fairly simple. |
| 68 | */ |
| 69 | static int display_dram_config(void) |
| 70 | { |
| 71 | ulong size = 0; |
| 72 | int i; |
| 73 | |
| 74 | debug("RAM Configuration:\n"); |
| 75 | |
| 76 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 77 | #ifdef DEBUG |
| 78 | printf("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start); |
| 79 | print_size(gd->bd->bi_dram[i].size, "\n"); |
| 80 | #endif |
| 81 | size += gd->bd->bi_dram[i].size; |
| 82 | } |
| 83 | puts("DRAM: "); |
| 84 | print_size(size, "\n"); |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * Breathe some life into the board... |
| 90 | * |
| 91 | * Initialize a serial port as console, and carry out some hardware |
| 92 | * tests. |
| 93 | * |
| 94 | * The first part of initialization is running from Flash memory; |
| 95 | * its main purpose is to initialize the RAM so that we |
| 96 | * can relocate the monitor code to RAM. |
| 97 | */ |
| 98 | |
| 99 | /* |
| 100 | * All attempts to come up with a "common" initialization sequence |
| 101 | * that works for all boards and architectures failed: some of the |
| 102 | * requirements are just _too_ different. To get rid of the resulting |
| 103 | * mess of board dependent #ifdef'ed code we now make the whole |
| 104 | * initialization sequence configurable to the user. |
| 105 | * |
| 106 | * The requirements for any new initalization function is simple: it |
| 107 | * receives a pointer to the "global data" structure as it's only |
| 108 | * argument, and returns an integer return code, where 0 means |
| 109 | * "continue" and != 0 means "fatal error, hang the system". |
| 110 | */ |
| 111 | typedef int (init_fnc_t) (void); |
| 112 | |
| 113 | void __dram_init_banksize(void) |
| 114 | { |
| 115 | gd->bd->bi_dram[0].start = 0; |
| 116 | gd->bd->bi_dram[0].size = gd->ram_size; |
| 117 | } |
| 118 | |
| 119 | void dram_init_banksize(void) |
| 120 | __attribute__((weak, alias("__dram_init_banksize"))); |
| 121 | |
| 122 | init_fnc_t *init_sequence[] = { |
| 123 | #if defined(CONFIG_ARCH_CPU_INIT) |
| 124 | arch_cpu_init, /* basic arch cpu dependent setup */ |
| 125 | #endif |
| 126 | #if defined(CONFIG_BOARD_EARLY_INIT_F) |
| 127 | board_early_init_f, |
| 128 | #endif |
| 129 | timer_init, /* initialize timer */ |
| 130 | env_init, /* initialize environment */ |
| 131 | serial_init, /* serial communications setup */ |
| 132 | console_init_f, /* stage 1 init of console */ |
| 133 | display_banner, /* say that we are here */ |
| 134 | #if defined(CONFIG_DISPLAY_CPUINFO) |
| 135 | print_cpuinfo, /* display cpu info (and speed) */ |
| 136 | #endif |
| 137 | #if defined(CONFIG_DISPLAY_BOARDINFO) |
| 138 | checkboard, /* display board info */ |
| 139 | #endif |
| 140 | dram_init, /* configure available RAM banks */ |
| 141 | NULL, |
| 142 | }; |
| 143 | |
| 144 | void board_init_f(ulong bootflag) |
| 145 | { |
| 146 | init_fnc_t **init_fnc_ptr; |
| 147 | uchar *mem; |
| 148 | unsigned long addr_sp, addr, size; |
| 149 | |
| 150 | gd = malloc(sizeof(gd_t)); |
| 151 | assert(gd); |
| 152 | |
| 153 | memset((void *)gd, 0, sizeof(gd_t)); |
| 154 | |
| 155 | for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) { |
| 156 | if ((*init_fnc_ptr)() != 0) |
| 157 | hang(); |
| 158 | } |
| 159 | |
| 160 | size = CONFIG_SYS_SDRAM_SIZE; |
| 161 | mem = malloc(size); |
| 162 | assert(mem); |
| 163 | gd->ram_buf = mem; |
| 164 | addr = (ulong)(mem + size); |
| 165 | |
| 166 | /* |
| 167 | * reserve memory for malloc() arena |
| 168 | */ |
| 169 | addr_sp = addr - TOTAL_MALLOC_LEN; |
| 170 | debug("Reserving %dk for malloc() at: %08lx\n", |
| 171 | TOTAL_MALLOC_LEN >> 10, addr_sp); |
| 172 | /* |
| 173 | * (permanently) allocate a Board Info struct |
| 174 | * and a permanent copy of the "global" data |
| 175 | */ |
| 176 | addr_sp -= sizeof(bd_t); |
| 177 | gd->bd = (bd_t *) addr_sp; |
| 178 | debug("Reserving %zu Bytes for Board Info at: %08lx\n", |
| 179 | sizeof(bd_t), addr_sp); |
| 180 | |
| 181 | /* Ram ist board specific, so move it to board code ... */ |
| 182 | dram_init_banksize(); |
| 183 | display_dram_config(); /* and display it */ |
| 184 | |
| 185 | /* We don't relocate, so just run the post-relocation code */ |
| 186 | board_init_r(NULL, 0); |
| 187 | |
| 188 | /* NOTREACHED - no way out of command loop except booting */ |
| 189 | } |
| 190 | |
| 191 | /************************************************************************ |
| 192 | * |
| 193 | * This is the next part if the initialization sequence: we are now |
| 194 | * running from RAM and have a "normal" C environment, i. e. global |
| 195 | * data can be written, BSS has been cleared, the stack size in not |
| 196 | * that critical any more, etc. |
| 197 | * |
| 198 | ************************************************************************ |
| 199 | */ |
| 200 | |
| 201 | void board_init_r(gd_t *id, ulong dest_addr) |
| 202 | { |
| 203 | |
| 204 | if (id) |
| 205 | gd = id; |
| 206 | |
| 207 | gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */ |
| 208 | |
| 209 | #ifdef CONFIG_SERIAL_MULTI |
| 210 | serial_initialize(); |
| 211 | #endif |
| 212 | |
| 213 | #ifdef CONFIG_POST |
| 214 | post_output_backlog(); |
| 215 | #endif |
| 216 | |
| 217 | #if 0 /* Sandbox uses system malloc for now */ |
| 218 | /* The Malloc area is immediately below the monitor copy in DRAM */ |
| 219 | malloc_start = dest_addr - TOTAL_MALLOC_LEN; |
| 220 | mem_malloc_init(malloc_start, TOTAL_MALLOC_LEN); |
| 221 | #endif |
| 222 | |
| 223 | /* initialize environment */ |
| 224 | env_relocate(); |
| 225 | |
| 226 | /* IP Address */ |
| 227 | gd->bd->bi_ip_addr = getenv_IPaddr("ipaddr"); |
| 228 | |
| 229 | stdio_init(); /* get the devices list going. */ |
| 230 | |
| 231 | jumptable_init(); |
| 232 | |
| 233 | console_init_r(); /* fully init console as a device */ |
| 234 | |
| 235 | #if defined(CONFIG_DISPLAY_BOARDINFO_LATE) |
| 236 | checkboard(); |
| 237 | #endif |
| 238 | |
| 239 | #if defined(CONFIG_ARCH_MISC_INIT) |
| 240 | /* miscellaneous arch dependent initialisations */ |
| 241 | arch_misc_init(); |
| 242 | #endif |
| 243 | #if defined(CONFIG_MISC_INIT_R) |
| 244 | /* miscellaneous platform dependent initialisations */ |
| 245 | misc_init_r(); |
| 246 | #endif |
| 247 | |
| 248 | /* set up exceptions */ |
| 249 | interrupt_init(); |
| 250 | /* enable exceptions */ |
| 251 | enable_interrupts(); |
| 252 | |
Helmut Raiger | d5a184b | 2011-10-20 04:19:47 +0000 | [diff] [blame] | 253 | #ifdef CONFIG_BOARD_LATE_INIT |
Simon Glass | ca96f65 | 2011-10-03 19:26:37 +0000 | [diff] [blame] | 254 | board_late_init(); |
| 255 | #endif |
| 256 | |
| 257 | #ifdef CONFIG_POST |
| 258 | post_run(NULL, POST_RAM | post_bootmode_get(0)); |
| 259 | #endif |
| 260 | |
| 261 | /* |
| 262 | * For now, run the main loop. Later we might let this be done |
| 263 | * in the main program. |
| 264 | */ |
| 265 | while (1) |
| 266 | main_loop(); |
| 267 | |
| 268 | /* NOTREACHED - no way out of command loop except booting */ |
| 269 | } |
| 270 | |
| 271 | void hang(void) |
| 272 | { |
| 273 | puts("### ERROR ### Please RESET the board ###\n"); |
| 274 | for (;;) |
| 275 | ; |
| 276 | } |