blob: 6d947c6c285338293e913841a54e3ca9b668d8a4 [file] [log] [blame]
wdenk2262cfe2002-11-18 00:14:45 +00001/*
Graeme Russdbf71152011-04-13 19:43:26 +10002 * (C) Copyright 2008-2011
3 * Graeme Russ, <graeme.russ@gmail.com>
wdenk8bde7f72003-06-27 21:31:46 +00004 *
wdenk2262cfe2002-11-18 00:14:45 +00005 * (C) Copyright 2002
Graeme Russdbf71152011-04-13 19:43:26 +10006 * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
7 *
8 * (C) Copyright 2002
9 * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
wdenk8bde7f72003-06-27 21:31:46 +000010 *
wdenk2262cfe2002-11-18 00:14:45 +000011 * (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>
36#include <command.h>
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020037#include <stdio_dev.h>
Peter Tyser561858e2008-11-03 09:30:59 -060038#include <timestamp.h>
wdenk2262cfe2002-11-18 00:14:45 +000039#include <version.h>
40#include <malloc.h>
wdenk2262cfe2002-11-18 00:14:45 +000041#include <net.h>
42#include <ide.h>
Graeme Russbf165002010-04-24 00:05:47 +100043#include <serial.h>
wdenk7a8e9bed2003-05-31 18:35:21 +000044#include <asm/u-boot-i386.h>
Graeme Russ1c409bc2009-11-24 20:04:21 +110045#include <elf.h>
wdenk2262cfe2002-11-18 00:14:45 +000046
Luigi 'Comio' Mantellini310cecb2009-10-10 12:42:21 +020047#ifdef CONFIG_BITBANGMII
48#include <miiphy.h>
49#endif
50
Graeme Russ2e2613d2011-02-12 15:11:50 +110051/*
52 * Pointer to initial global data area
53 *
54 * Here we initialize it.
55 */
56#undef XTRN_DECLARE_GLOBAL_DATA_PTR
57#define XTRN_DECLARE_GLOBAL_DATA_PTR /* empty = allocate here */
58DECLARE_GLOBAL_DATA_PTR = (gd_t *) (CONFIG_SYS_INIT_GD_ADDR);
59
Wolfgang Denkd87080b2006-03-31 18:32:53 +020060
Graeme Russ1c409bc2009-11-24 20:04:21 +110061/* Exports from the Linker Script */
Graeme Russ067f9b12010-10-07 20:03:31 +110062extern ulong __text_start;
Graeme Russ22191422010-10-07 20:03:32 +110063extern ulong __data_end;
Graeme Russ067f9b12010-10-07 20:03:31 +110064extern ulong __rel_dyn_start;
65extern ulong __rel_dyn_end;
66extern ulong __bss_start;
Graeme Russf2ff75c2010-10-07 20:03:33 +110067extern ulong __bss_end;
Graeme Russ2fb1bc42010-04-24 00:05:44 +100068
wdenk2262cfe2002-11-18 00:14:45 +000069const char version_string[] =
Peter Tyser561858e2008-11-03 09:30:59 -060070 U_BOOT_VERSION" (" U_BOOT_DATE " - " U_BOOT_TIME ")";
wdenk2262cfe2002-11-18 00:14:45 +000071
wdenk2262cfe2002-11-18 00:14:45 +000072/************************************************************************
73 * Init Utilities *
74 ************************************************************************
75 * Some of this code should be moved into the core functions,
76 * or dropped completely,
77 * but let's get it working (again) first...
78 */
wdenk2262cfe2002-11-18 00:14:45 +000079static int init_baudrate (void)
80{
wdenk7a8e9bed2003-05-31 18:35:21 +000081 char tmp[64]; /* long enough for environment variables */
Wolfgang Denkcdb74972010-07-24 21:55:43 +020082 int i = getenv_f("baudrate", tmp, 64);
wdenk2262cfe2002-11-18 00:14:45 +000083
wdenk7a8e9bed2003-05-31 18:35:21 +000084 gd->baudrate = (i != 0)
wdenk2262cfe2002-11-18 00:14:45 +000085 ? (int) simple_strtoul (tmp, NULL, 10)
86 : CONFIG_BAUDRATE;
87
88 return (0);
89}
90
91static int display_banner (void)
92{
93
94 printf ("\n\n%s\n\n", version_string);
Graeme Russ1c409bc2009-11-24 20:04:21 +110095/*
wdenk2262cfe2002-11-18 00:14:45 +000096 printf ("U-Boot code: %08lX -> %08lX data: %08lX -> %08lX\n"
97 " BSS: %08lX -> %08lX stack: %08lX -> %08lX\n",
98 i386boot_start, i386boot_romdata_start-1,
99 i386boot_romdata_dest, i386boot_romdata_dest+i386boot_romdata_size-1,
100 i386boot_bss_start, i386boot_bss_start+i386boot_bss_size-1,
wdenk8bde7f72003-06-27 21:31:46 +0000101 i386boot_bss_start+i386boot_bss_size,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200102 i386boot_bss_start+i386boot_bss_size+CONFIG_SYS_STACK_SIZE-1);
wdenk8bde7f72003-06-27 21:31:46 +0000103
Graeme Russ1c409bc2009-11-24 20:04:21 +1100104*/
wdenk2262cfe2002-11-18 00:14:45 +0000105
106 return (0);
107}
108
wdenk2262cfe2002-11-18 00:14:45 +0000109static int display_dram_config (void)
110{
wdenk2262cfe2002-11-18 00:14:45 +0000111 int i;
112
113 puts ("DRAM Configuration:\n");
114
115 for (i=0; i<CONFIG_NR_DRAM_BANKS; i++) {
116 printf ("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
117 print_size (gd->bd->bi_dram[i].size, "\n");
118 }
wdenk8bde7f72003-06-27 21:31:46 +0000119
wdenk2262cfe2002-11-18 00:14:45 +0000120 return (0);
121}
122
123static void display_flash_config (ulong size)
124{
125 puts ("Flash: ");
126 print_size (size, "\n");
127}
128
wdenk2262cfe2002-11-18 00:14:45 +0000129/*
130 * Breath some life into the board...
131 *
132 * Initialize an SMC for serial comms, and carry out some hardware
133 * tests.
134 *
135 * The first part of initialization is running from Flash memory;
136 * its main purpose is to initialize the RAM so that we
137 * can relocate the monitor code to RAM.
138 */
139
140/*
141 * All attempts to come up with a "common" initialization sequence
142 * that works for all boards and architectures failed: some of the
143 * requirements are just _too_ different. To get rid of the resulting
144 * mess of board dependend #ifdef'ed code we now make the whole
145 * initialization sequence configurable to the user.
146 *
147 * The requirements for any new initalization function is simple: it
148 * receives a pointer to the "global data" structure as it's only
149 * argument, and returns an integer return code, where 0 means
150 * "continue" and != 0 means "fatal error, hang the system".
151 */
152typedef int (init_fnc_t) (void);
153
Graeme Russe4f78d72011-02-12 15:12:10 +1100154static int calculate_relocation_address(void);
155static int copy_uboot_to_ram(void);
156static int clear_bss(void);
157static int do_elf_reloc_fixups(void);
158
159init_fnc_t *init_sequence_f[] = {
160 cpu_init_f,
161 board_early_init_f,
162 env_init,
163 init_baudrate,
164 serial_init,
165 console_init_f,
166 dram_init_f,
167 calculate_relocation_address,
168 copy_uboot_to_ram,
169 clear_bss,
170 do_elf_reloc_fixups,
171
172 NULL,
173};
174
175init_fnc_t *init_sequence_r[] = {
Graeme Russ1c409bc2009-11-24 20:04:21 +1100176 cpu_init_r, /* basic cpu dependent setup */
177 board_early_init_r, /* basic board dependent setup */
wdenk2262cfe2002-11-18 00:14:45 +0000178 dram_init, /* configure available RAM banks */
wdenk2262cfe2002-11-18 00:14:45 +0000179 interrupt_init, /* set up exceptions */
wdenk8bde7f72003-06-27 21:31:46 +0000180 timer_init,
wdenk2262cfe2002-11-18 00:14:45 +0000181 display_banner,
182 display_dram_config,
183
184 NULL,
185};
186
Graeme Russ3ef96de2008-09-07 07:08:42 +1000187gd_t *gd;
wdenk2262cfe2002-11-18 00:14:45 +0000188
Graeme Russ71a54042011-02-12 15:12:06 +1100189static int calculate_relocation_address(void)
190{
191 void *text_start = &__text_start;
192 void *bss_end = &__bss_end;
193 void *dest_addr;
194 ulong rel_offset;
195
196 /* Calculate destination RAM Address and relocation offset */
197 dest_addr = (void *)gd->ram_size;
198 dest_addr -= CONFIG_SYS_STACK_SIZE;
199 dest_addr -= (bss_end - text_start);
200 rel_offset = dest_addr - text_start;
201
202 gd->start_addr_sp = gd->ram_size;
203 gd->relocaddr = (ulong)dest_addr;
204 gd->reloc_off = rel_offset;
205
206 return 0;
207}
208
209static int copy_uboot_to_ram(void)
210{
211 ulong *dst_addr = (ulong *)gd->relocaddr;
212 ulong *src_addr = (ulong *)&__text_start;
213 ulong *end_addr = (ulong *)&__data_end;
214
215 while (src_addr < end_addr)
216 *dst_addr++ = *src_addr++;
217
218 return 0;
219}
220
221static int clear_bss(void)
222{
223 void *bss_start = &__bss_start;
224 void *bss_end = &__bss_end;
225
226 ulong *dst_addr = (ulong *)(bss_start + gd->reloc_off);
227 ulong *end_addr = (ulong *)(bss_end + gd->reloc_off);;
228
229 while (dst_addr < end_addr)
230 *dst_addr++ = 0x00000000;
231
232 return 0;
233}
234
235static int do_elf_reloc_fixups(void)
236{
237 Elf32_Rel *re_src = (Elf32_Rel *)(&__rel_dyn_start);
238 Elf32_Rel *re_end = (Elf32_Rel *)(&__rel_dyn_end);
239
240 do {
241 if (re_src->r_offset >= CONFIG_SYS_TEXT_BASE)
242 if (*(Elf32_Addr *)(re_src->r_offset + gd->reloc_off) >= CONFIG_SYS_TEXT_BASE)
243 *(Elf32_Addr *)(re_src->r_offset + gd->reloc_off) += gd->reloc_off;
244 } while (re_src++ < re_end);
245
246 return 0;
247}
248
Graeme Russdbf71152011-04-13 19:43:26 +1000249/* Load U-Boot into RAM, initialize BSS, perform relocation adjustments */
Graeme Russ96cd6642011-02-12 15:11:54 +1100250void board_init_f(ulong boot_flags)
Graeme Russ1c409bc2009-11-24 20:04:21 +1100251{
Graeme Russe4f78d72011-02-12 15:12:10 +1100252 init_fnc_t **init_fnc_ptr;
Graeme Russa3824142011-02-12 15:12:08 +1100253
Graeme Russdbf71152011-04-13 19:43:26 +1000254 gd->flags = boot_flags;
255
Graeme Russe4f78d72011-02-12 15:12:10 +1100256 for (init_fnc_ptr = init_sequence_f; *init_fnc_ptr; ++init_fnc_ptr) {
257 if ((*init_fnc_ptr)() != 0)
258 hang();
259 }
Graeme Russf2ff75c2010-10-07 20:03:33 +1100260
Graeme Russ2e2613d2011-02-12 15:11:50 +1100261 gd->flags |= GD_FLG_RELOC;
Graeme Russ161b3582010-10-07 20:03:29 +1100262
Graeme Russ2fb1bc42010-04-24 00:05:44 +1000263 /* Enter the relocated U-Boot! */
Graeme Russ71a54042011-02-12 15:12:06 +1100264 relocate_code(gd->start_addr_sp, gd, gd->relocaddr);
Graeme Russ161b3582010-10-07 20:03:29 +1100265
Graeme Russfb002902011-02-12 15:11:58 +1100266 /* NOTREACHED - relocate_code() does not return */
Graeme Russ1c409bc2009-11-24 20:04:21 +1100267 while(1);
268}
269
Graeme Russ2fb1bc42010-04-24 00:05:44 +1000270void board_init_r(gd_t *id, ulong dest_addr)
wdenk2262cfe2002-11-18 00:14:45 +0000271{
wdenk2262cfe2002-11-18 00:14:45 +0000272 char *s;
wdenk2262cfe2002-11-18 00:14:45 +0000273 ulong size;
wdenk2262cfe2002-11-18 00:14:45 +0000274 static bd_t bd_data;
Graeme Russ2e2613d2011-02-12 15:11:50 +1100275 static gd_t gd_data;
wdenk2262cfe2002-11-18 00:14:45 +0000276 init_fnc_t **init_fnc_ptr;
wdenk8bde7f72003-06-27 21:31:46 +0000277
wdenk2262cfe2002-11-18 00:14:45 +0000278 show_boot_progress(0x21);
279
Graeme Russ2e2613d2011-02-12 15:11:50 +1100280 /* Global data pointer is now writable */
281 gd = &gd_data;
282 memcpy(gd, id, sizeof(gd_t));
283
wdenk93f6a672004-07-01 20:28:03 +0000284 /* compiler optimization barrier needed for GCC >= 3.4 */
285 __asm__ __volatile__("": : :"memory");
wdenk8bde7f72003-06-27 21:31:46 +0000286
wdenk2262cfe2002-11-18 00:14:45 +0000287 gd->bd = &bd_data;
288 memset (gd->bd, 0, sizeof (bd_t));
289 show_boot_progress(0x22);
290
wdenk7a8e9bed2003-05-31 18:35:21 +0000291 gd->baudrate = CONFIG_BAUDRATE;
wdenk8bde7f72003-06-27 21:31:46 +0000292
Graeme Russ2fb1bc42010-04-24 00:05:44 +1000293 mem_malloc_init((((ulong)dest_addr - CONFIG_SYS_MALLOC_LEN)+3)&~3,
Graeme Russ1c409bc2009-11-24 20:04:21 +1100294 CONFIG_SYS_MALLOC_LEN);
295
Graeme Russe4f78d72011-02-12 15:12:10 +1100296 for (init_fnc_ptr = init_sequence_r; *init_fnc_ptr; ++init_fnc_ptr) {
297 if ((*init_fnc_ptr)() != 0)
wdenk2262cfe2002-11-18 00:14:45 +0000298 hang ();
wdenk2262cfe2002-11-18 00:14:45 +0000299 }
300 show_boot_progress(0x23);
301
Graeme Russbf165002010-04-24 00:05:47 +1000302#ifdef CONFIG_SERIAL_MULTI
303 serial_initialize();
304#endif
wdenk2262cfe2002-11-18 00:14:45 +0000305 /* configure available FLASH banks */
306 size = flash_init();
307 display_flash_config(size);
308 show_boot_progress(0x24);
309
310 show_boot_progress(0x25);
311
312 /* initialize environment */
313 env_relocate ();
314 show_boot_progress(0x26);
315
316
Graeme Russ535ad2d2010-04-24 00:05:36 +1000317#ifdef CONFIG_CMD_NET
wdenk2262cfe2002-11-18 00:14:45 +0000318 /* IP Address */
319 bd_data.bi_ip_addr = getenv_IPaddr ("ipaddr");
Graeme Russ535ad2d2010-04-24 00:05:36 +1000320#endif
wdenk2262cfe2002-11-18 00:14:45 +0000321
wdenk2262cfe2002-11-18 00:14:45 +0000322#if defined(CONFIG_PCI)
323 /*
324 * Do pci configuration
325 */
326 pci_init();
327#endif
328
329 show_boot_progress(0x27);
330
331
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200332 stdio_init ();
wdenk2262cfe2002-11-18 00:14:45 +0000333
wdenk27b207f2003-07-24 23:38:38 +0000334 jumptable_init ();
wdenk8bde7f72003-06-27 21:31:46 +0000335
wdenk2262cfe2002-11-18 00:14:45 +0000336 /* Initialize the console (after the relocation and devices init) */
337 console_init_r();
wdenk2262cfe2002-11-18 00:14:45 +0000338
339#ifdef CONFIG_MISC_INIT_R
340 /* miscellaneous platform dependent initialisations */
341 misc_init_r();
342#endif
343
Jon Loeliger67350562007-07-09 18:05:38 -0500344#if defined(CONFIG_CMD_PCMCIA) && !defined(CONFIG_CMD_IDE)
wdenk2262cfe2002-11-18 00:14:45 +0000345 WATCHDOG_RESET();
346 puts ("PCMCIA:");
347 pcmcia_init();
348#endif
349
Jon Loeliger67350562007-07-09 18:05:38 -0500350#if defined(CONFIG_CMD_KGDB)
wdenk2262cfe2002-11-18 00:14:45 +0000351 WATCHDOG_RESET();
352 puts("KGDB: ");
353 kgdb_init();
354#endif
355
356 /* enable exceptions */
wdenk7a8e9bed2003-05-31 18:35:21 +0000357 enable_interrupts();
wdenk2262cfe2002-11-18 00:14:45 +0000358 show_boot_progress(0x28);
359
wdenk2262cfe2002-11-18 00:14:45 +0000360#ifdef CONFIG_STATUS_LED
361 status_led_set (STATUS_LED_BOOT, STATUS_LED_BLINKING);
362#endif
363
wdenk7a8e9bed2003-05-31 18:35:21 +0000364 udelay(20);
wdenk2262cfe2002-11-18 00:14:45 +0000365
366 set_timer (0);
367
368 /* Initialize from environment */
369 if ((s = getenv ("loadaddr")) != NULL) {
370 load_addr = simple_strtoul (s, NULL, 16);
371 }
Jon Loeliger67350562007-07-09 18:05:38 -0500372#if defined(CONFIG_CMD_NET)
wdenk2262cfe2002-11-18 00:14:45 +0000373 if ((s = getenv ("bootfile")) != NULL) {
374 copy_filename (BootFile, s, sizeof (BootFile));
375 }
Jon Loeligerb3aff0c2007-07-10 11:19:50 -0500376#endif
wdenk2262cfe2002-11-18 00:14:45 +0000377
378 WATCHDOG_RESET();
379
Jon Loeliger67350562007-07-09 18:05:38 -0500380#if defined(CONFIG_CMD_IDE)
wdenk2262cfe2002-11-18 00:14:45 +0000381 WATCHDOG_RESET();
382 puts("IDE: ");
383 ide_init();
Jon Loeligerb3aff0c2007-07-10 11:19:50 -0500384#endif
wdenk2262cfe2002-11-18 00:14:45 +0000385
Jon Loeliger67350562007-07-09 18:05:38 -0500386#if defined(CONFIG_CMD_SCSI)
wdenk2262cfe2002-11-18 00:14:45 +0000387 WATCHDOG_RESET();
388 puts("SCSI: ");
389 scsi_init();
390#endif
391
Jon Loeliger67350562007-07-09 18:05:38 -0500392#if defined(CONFIG_CMD_DOC)
wdenk2262cfe2002-11-18 00:14:45 +0000393 WATCHDOG_RESET();
wdenk7a8e9bed2003-05-31 18:35:21 +0000394 puts("DOC: ");
wdenk2262cfe2002-11-18 00:14:45 +0000395 doc_init();
396#endif
397
Luigi 'Comio' Mantellini310cecb2009-10-10 12:42:21 +0200398#ifdef CONFIG_BITBANGMII
399 bb_miiphy_init();
400#endif
Jon Loeliger67350562007-07-09 18:05:38 -0500401#if defined(CONFIG_CMD_NET)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200402#if defined(CONFIG_NET_MULTI)
wdenk2262cfe2002-11-18 00:14:45 +0000403 WATCHDOG_RESET();
404 puts("Net: ");
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200405#endif
wdenk2262cfe2002-11-18 00:14:45 +0000406 eth_initialize(gd->bd);
407#endif
408
Jon Loeliger67350562007-07-09 18:05:38 -0500409#if ( defined(CONFIG_CMD_NET)) && (0)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200410 WATCHDOG_RESET();
411# ifdef DEBUG
412 puts ("Reset Ethernet PHY\n");
413# endif
414 reset_phy();
415#endif
416
wdenk2262cfe2002-11-18 00:14:45 +0000417#ifdef CONFIG_LAST_STAGE_INIT
418 WATCHDOG_RESET();
419 /*
420 * Some parts can be only initialized if all others (like
421 * Interrupts) are up and running (i.e. the PC-style ISA
422 * keyboard).
423 */
424 last_stage_init();
425#endif
426
427
wdenk2262cfe2002-11-18 00:14:45 +0000428#ifdef CONFIG_POST
429 post_run (NULL, POST_RAM | post_bootmode_get(0));
wdenk2262cfe2002-11-18 00:14:45 +0000430#endif
wdenk8bde7f72003-06-27 21:31:46 +0000431
432
wdenk2262cfe2002-11-18 00:14:45 +0000433 show_boot_progress(0x29);
wdenk8bde7f72003-06-27 21:31:46 +0000434
wdenk2262cfe2002-11-18 00:14:45 +0000435 /* main_loop() can return to retry autoboot, if so just run it again. */
436 for (;;) {
wdenk7a8e9bed2003-05-31 18:35:21 +0000437 main_loop();
wdenk2262cfe2002-11-18 00:14:45 +0000438 }
439
440 /* NOTREACHED - no way out of command loop except booting */
441}
442
443void hang (void)
444{
445 puts ("### ERROR ### Please RESET the board ###\n");
446 for (;;);
447}
Mike Frysingera4986452008-04-13 19:42:19 -0400448
Graeme Russe69c0cb2010-08-22 16:25:58 +1000449unsigned long do_go_exec (ulong (*entry)(int, char * const []), int argc, char * const argv[])
Mike Frysingera4986452008-04-13 19:42:19 -0400450{
Graeme Russe69c0cb2010-08-22 16:25:58 +1000451 unsigned long ret = 0;
452 char **argv_tmp;
Graeme Russ9e08efc2010-04-24 00:05:39 +1000453
Graeme Russe69c0cb2010-08-22 16:25:58 +1000454 /*
455 * x86 does not use a dedicated register to pass the pointer to
456 * the global_data, so it is instead passed as argv[-1]. By using
457 * argv[-1], the called 'Application' can use the contents of
458 * argv natively. However, to safely use argv[-1] a new copy of
459 * argv is needed with the extra element
460 */
461 argv_tmp = malloc(sizeof(char *) * (argc + 1));
462
463 if (argv_tmp) {
464 argv_tmp[0] = (char *)gd;
465
466 memcpy(&argv_tmp[1], argv, (size_t)(sizeof(char *) * argc));
467
468 ret = (entry) (argc, &argv_tmp[1]);
469 free(argv_tmp);
470 }
471
472 return ret;
Mike Frysingera4986452008-04-13 19:42:19 -0400473}
Graeme Russ79ea6b82010-04-24 00:05:48 +1000474
475void setup_pcat_compatibility(void)
476 __attribute__((weak, alias("__setup_pcat_compatibility")));
477
478void __setup_pcat_compatibility(void)
479{
480}