blob: 3feb02071209b1e959d8ca1706e03be7928f7caf [file] [log] [blame]
wdenkbb1b8262003-03-27 12:09:35 +00001/*
2 * (C) Copyright 2003
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkbb1b8262003-03-27 12:09:35 +00006 */
7
8#include <common.h>
9#include <command.h>
10#include <malloc.h>
Joe Hershberger7fe0ac82012-12-11 17:52:50 +000011#include <serial.h>
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020012#include <stdio_dev.h>
wdenkbb1b8262003-03-27 12:09:35 +000013#include <version.h>
14#include <net.h>
15#include <environment.h>
Jason McMullan7d32b002008-05-30 00:53:37 +090016#include <nand.h>
Stefan Roese1af2a0d2008-11-12 13:18:19 +010017#include <onenand_uboot.h>
Jason McMullan733ad782008-05-30 00:53:37 +090018#include <spi.h>
wdenkbb1b8262003-03-27 12:09:35 +000019
Luigi 'Comio' Mantellini01db1552009-10-10 12:42:21 +020020#ifdef CONFIG_BITBANGMII
21#include <miiphy.h>
22#endif
23
Wolfgang Denk117b0b12005-12-01 02:15:07 +010024DECLARE_GLOBAL_DATA_PTR;
25
wdenkb9a83a92003-05-30 12:48:29 +000026ulong monitor_flash_len;
27
wdenkbb1b8262003-03-27 12:09:35 +000028static char *failed = "*** failed ***\n";
29
Stefan Roese2b1c8152008-11-12 13:18:02 +010030int __board_early_init_f(void)
31{
32 /*
33 * Nothing to do in this dummy implementation
34 */
35 return 0;
36}
Wolfgang Denk11f98842011-12-08 02:01:49 +000037int board_early_init_f(void)
38 __attribute__((weak, alias("__board_early_init_f")));
Stefan Roese2b1c8152008-11-12 13:18:02 +010039
Wolfgang Denk11f98842011-12-08 02:01:49 +000040static int init_func_ram(void)
wdenkbb1b8262003-03-27 12:09:35 +000041{
wdenkbb1b8262003-03-27 12:09:35 +000042#ifdef CONFIG_BOARD_TYPES
43 int board_type = gd->board_type;
44#else
45 int board_type = 0; /* use dummy arg */
46#endif
Wolfgang Denk11f98842011-12-08 02:01:49 +000047 puts("DRAM: ");
wdenkbb1b8262003-03-27 12:09:35 +000048
Wolfgang Denk11f98842011-12-08 02:01:49 +000049 gd->ram_size = initdram(board_type);
50 if (gd->ram_size > 0) {
51 print_size(gd->ram_size, "\n");
52 return 0;
wdenkbb1b8262003-03-27 12:09:35 +000053 }
Wolfgang Denk11f98842011-12-08 02:01:49 +000054 puts(failed);
55 return 1;
wdenkbb1b8262003-03-27 12:09:35 +000056}
57
58static int display_banner(void)
59{
60
Wolfgang Denk11f98842011-12-08 02:01:49 +000061 printf("\n\n%s\n\n", version_string);
62 return 0;
wdenkbb1b8262003-03-27 12:09:35 +000063}
64
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020065#ifndef CONFIG_SYS_NO_FLASH
wdenkbb1b8262003-03-27 12:09:35 +000066static void display_flash_config(ulong size)
67{
Wolfgang Denk11f98842011-12-08 02:01:49 +000068 puts("Flash: ");
69 print_size(size, "\n");
wdenkbb1b8262003-03-27 12:09:35 +000070}
Jean-Christophe PLAGNIOL-VILLARD3bd04ce2008-02-17 16:58:04 +010071#endif
wdenkbb1b8262003-03-27 12:09:35 +000072
Wolfgang Denk11f98842011-12-08 02:01:49 +000073static int init_baudrate(void)
wdenkbb1b8262003-03-27 12:09:35 +000074{
Simon Glass8ccbef92011-10-13 14:43:11 +000075 gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
76 return 0;
wdenkbb1b8262003-03-27 12:09:35 +000077}
78
79
80/*
81 * Breath some life into the board...
82 *
83 * The first part of initialization is running from Flash memory;
84 * its main purpose is to initialize the RAM so that we
85 * can relocate the monitor code to RAM.
86 */
87
88/*
89 * All attempts to come up with a "common" initialization sequence
90 * that works for all boards and architectures failed: some of the
91 * requirements are just _too_ different. To get rid of the resulting
92 * mess of board dependend #ifdef'ed code we now make the whole
93 * initialization sequence configurable to the user.
94 *
95 * The requirements for any new initalization function is simple: it
96 * receives a pointer to the "global data" structure as it's only
97 * argument, and returns an integer return code, where 0 means
98 * "continue" and != 0 means "fatal error, hang the system".
99 */
Wolfgang Denk11f98842011-12-08 02:01:49 +0000100typedef int (init_fnc_t)(void);
wdenkbb1b8262003-03-27 12:09:35 +0000101
102init_fnc_t *init_sequence[] = {
Stefan Roese2b1c8152008-11-12 13:18:02 +0100103 board_early_init_f,
wdenkbb1b8262003-03-27 12:09:35 +0000104 timer_init,
105 env_init, /* initialize environment */
Wolfgang Denk11f98842011-12-08 02:01:49 +0000106 init_baudrate, /* initialize baudrate settings */
wdenkbb1b8262003-03-27 12:09:35 +0000107 serial_init, /* serial communications setup */
108 console_init_f,
109 display_banner, /* say that we are here */
wdenk8dba0502003-03-31 16:34:49 +0000110 checkboard,
wdenkbb1b8262003-03-27 12:09:35 +0000111 init_func_ram,
112 NULL,
113};
114
115
116void board_init_f(ulong bootflag)
117{
wdenkbb1b8262003-03-27 12:09:35 +0000118 gd_t gd_data, *id;
119 bd_t *bd;
120 init_fnc_t **init_fnc_ptr;
Daniel Schwierzeckbd4277d2013-02-12 22:22:12 +0100121 ulong addr, addr_sp, len;
Wolfgang Denk117b0b12005-12-01 02:15:07 +0100122 ulong *s;
wdenkbb1b8262003-03-27 12:09:35 +0000123
wdenkcd914452004-05-29 16:53:29 +0000124 /* Pointer is writable since we allocated a register for it.
125 */
wdenkbb1b8262003-03-27 12:09:35 +0000126 gd = &gd_data;
wdenk3337af42004-07-01 20:28:03 +0000127 /* compiler optimization barrier needed for GCC >= 3.4 */
Wolfgang Denk11f98842011-12-08 02:01:49 +0000128 __asm__ __volatile__("" : : : "memory");
wdenk3337af42004-07-01 20:28:03 +0000129
Wolfgang Denk11f98842011-12-08 02:01:49 +0000130 memset((void *)gd, 0, sizeof(gd_t));
wdenkbb1b8262003-03-27 12:09:35 +0000131
132 for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
Wolfgang Denk11f98842011-12-08 02:01:49 +0000133 if ((*init_fnc_ptr)() != 0)
134 hang();
wdenkbb1b8262003-03-27 12:09:35 +0000135 }
136
137 /*
138 * Now that we have DRAM mapped and working, we can
139 * relocate the code and continue running from DRAM.
140 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200141 addr = CONFIG_SYS_SDRAM_BASE + gd->ram_size;
wdenkbb1b8262003-03-27 12:09:35 +0000142
wdenkcd914452004-05-29 16:53:29 +0000143 /* We can reserve some RAM "on top" here.
144 */
wdenkbb1b8262003-03-27 12:09:35 +0000145
wdenkcd914452004-05-29 16:53:29 +0000146 /* round down to next 4 kB limit.
147 */
wdenkbb1b8262003-03-27 12:09:35 +0000148 addr &= ~(4096 - 1);
Wolfgang Denk11f98842011-12-08 02:01:49 +0000149 debug("Top of RAM usable for U-Boot at: %08lx\n", addr);
wdenk57b2d802003-06-27 21:31:46 +0000150
wdenkcd914452004-05-29 16:53:29 +0000151 /* Reserve memory for U-Boot code, data & bss
152 * round down to next 16 kB limit
153 */
Daniel Schwierzeckbd4277d2013-02-12 22:22:12 +0100154 len = bss_end() - CONFIG_SYS_MONITOR_BASE;
wdenkbb1b8262003-03-27 12:09:35 +0000155 addr -= len;
wdenkb9a83a92003-05-30 12:48:29 +0000156 addr &= ~(16 * 1024 - 1);
wdenkbb1b8262003-03-27 12:09:35 +0000157
Wolfgang Denk11f98842011-12-08 02:01:49 +0000158 debug("Reserving %ldk for U-Boot at: %08lx\n", len >> 10, addr);
wdenkbb1b8262003-03-27 12:09:35 +0000159
wdenkcd914452004-05-29 16:53:29 +0000160 /* Reserve memory for malloc() arena.
161 */
wdenkbb1b8262003-03-27 12:09:35 +0000162 addr_sp = addr - TOTAL_MALLOC_LEN;
Wolfgang Denk11f98842011-12-08 02:01:49 +0000163 debug("Reserving %dk for malloc() at: %08lx\n",
wdenkbb1b8262003-03-27 12:09:35 +0000164 TOTAL_MALLOC_LEN >> 10, addr_sp);
wdenkbb1b8262003-03-27 12:09:35 +0000165
166 /*
167 * (permanently) allocate a Board Info struct
168 * and a permanent copy of the "global" data
169 */
170 addr_sp -= sizeof(bd_t);
171 bd = (bd_t *)addr_sp;
172 gd->bd = bd;
Wolfgang Denk11f98842011-12-08 02:01:49 +0000173 debug("Reserving %zu Bytes for Board Info at: %08lx\n",
wdenkbb1b8262003-03-27 12:09:35 +0000174 sizeof(bd_t), addr_sp);
wdenkcd914452004-05-29 16:53:29 +0000175
wdenkbb1b8262003-03-27 12:09:35 +0000176 addr_sp -= sizeof(gd_t);
177 id = (gd_t *)addr_sp;
Wolfgang Denk11f98842011-12-08 02:01:49 +0000178 debug("Reserving %zu Bytes for Global Data at: %08lx\n",
179 sizeof(gd_t), addr_sp);
wdenkbb1b8262003-03-27 12:09:35 +0000180
Jean-Christophe PLAGNIOL-VILLARD3bd04ce2008-02-17 16:58:04 +0100181 /* Reserve memory for boot params.
wdenkcd914452004-05-29 16:53:29 +0000182 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200183 addr_sp -= CONFIG_SYS_BOOTPARAMS_LEN;
wdenkbb1b8262003-03-27 12:09:35 +0000184 bd->bi_boot_params = addr_sp;
Wolfgang Denk11f98842011-12-08 02:01:49 +0000185 debug("Reserving %dk for boot params() at: %08lx\n",
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200186 CONFIG_SYS_BOOTPARAMS_LEN >> 10, addr_sp);
wdenkbb1b8262003-03-27 12:09:35 +0000187
188 /*
189 * Finally, we set up a new (bigger) stack.
190 *
191 * Leave some safety gap for SP, force alignment on 16 byte boundary
192 * Clear initial stack frame
193 */
194 addr_sp -= 16;
195 addr_sp &= ~0xF;
Wolfgang Denk117b0b12005-12-01 02:15:07 +0100196 s = (ulong *)addr_sp;
197 *s-- = 0;
198 *s-- = 0;
199 addr_sp = (ulong)s;
Wolfgang Denk11f98842011-12-08 02:01:49 +0000200 debug("Stack Pointer at: %08lx\n", addr_sp);
wdenkcd914452004-05-29 16:53:29 +0000201
wdenkbb1b8262003-03-27 12:09:35 +0000202 /*
203 * Save local variables to board info struct
204 */
Wolfgang Denk11f98842011-12-08 02:01:49 +0000205 bd->bi_memstart = CONFIG_SYS_SDRAM_BASE; /* start of DRAM */
206 bd->bi_memsize = gd->ram_size; /* size of DRAM in bytes */
wdenkbb1b8262003-03-27 12:09:35 +0000207
Wolfgang Denk11f98842011-12-08 02:01:49 +0000208 memcpy(id, (void *)gd, sizeof(gd_t));
wdenkb02744a2003-04-05 00:53:31 +0000209
Wolfgang Denk11f98842011-12-08 02:01:49 +0000210 relocate_code(addr_sp, id, addr);
wdenkbb1b8262003-03-27 12:09:35 +0000211
212 /* NOTREACHED - relocate_code() does not return */
213}
Wolfgang Denk11f98842011-12-08 02:01:49 +0000214
215/*
wdenkbb1b8262003-03-27 12:09:35 +0000216 * This is the next part if the initialization sequence: we are now
217 * running from RAM and have a "normal" C environment, i. e. global
218 * data can be written, BSS has been cleared, the stack size in not
219 * that critical any more, etc.
wdenkbb1b8262003-03-27 12:09:35 +0000220 */
221
Wolfgang Denk11f98842011-12-08 02:01:49 +0000222void board_init_r(gd_t *id, ulong dest_addr)
wdenkbb1b8262003-03-27 12:09:35 +0000223{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200224#ifndef CONFIG_SYS_NO_FLASH
wdenkbb1b8262003-03-27 12:09:35 +0000225 ulong size;
Jean-Christophe PLAGNIOL-VILLARD3bd04ce2008-02-17 16:58:04 +0100226#endif
wdenkbb1b8262003-03-27 12:09:35 +0000227 bd_t *bd;
wdenkbb1b8262003-03-27 12:09:35 +0000228
229 gd = id;
230 gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
231
Wolfgang Denk11f98842011-12-08 02:01:49 +0000232 debug("Now running in RAM - U-Boot at: %08lx\n", dest_addr);
wdenkbb1b8262003-03-27 12:09:35 +0000233
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200234 gd->reloc_off = dest_addr - CONFIG_SYS_MONITOR_BASE;
wdenkbb1b8262003-03-27 12:09:35 +0000235
Daniel Schwierzeckbd4277d2013-02-12 22:22:12 +0100236 monitor_flash_len = image_copy_end() - dest_addr;
wdenkb9a83a92003-05-30 12:48:29 +0000237
Joe Hershberger7fe0ac82012-12-11 17:52:50 +0000238 serial_initialize();
239
Jean-Christophe PLAGNIOL-VILLARD3bd04ce2008-02-17 16:58:04 +0100240 bd = gd->bd;
241
Peter Tyser781c9b82009-08-21 23:05:21 -0500242 /* The Malloc area is immediately below the monitor copy in DRAM */
Peter Tysered527702009-08-21 23:05:20 -0500243 mem_malloc_init(CONFIG_SYS_MONITOR_BASE + gd->reloc_off -
244 TOTAL_MALLOC_LEN, TOTAL_MALLOC_LEN);
Stefan Roeseb81464e2009-05-11 15:50:12 +0200245
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200246#ifndef CONFIG_SYS_NO_FLASH
wdenkbb1b8262003-03-27 12:09:35 +0000247 /* configure available FLASH banks */
248 size = flash_init();
Wolfgang Denk11f98842011-12-08 02:01:49 +0000249 display_flash_config(size);
Daniel Schwierzeck0d15d2a2012-04-02 02:57:54 +0000250 bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
Jean-Christophe PLAGNIOL-VILLARD3bd04ce2008-02-17 16:58:04 +0100251 bd->bi_flashsize = size;
wdenkbb1b8262003-03-27 12:09:35 +0000252
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200253#if CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
wdenkb9a83a92003-05-30 12:48:29 +0000254 bd->bi_flashoffset = monitor_flash_len; /* reserved area for U-Boot */
wdenkbb1b8262003-03-27 12:09:35 +0000255#else
256 bd->bi_flashoffset = 0;
257#endif
Daniel Schwierzeck0d15d2a2012-04-02 02:57:54 +0000258#else
259 bd->bi_flashstart = 0;
260 bd->bi_flashsize = 0;
261 bd->bi_flashoffset = 0;
262#endif
wdenkbb1b8262003-03-27 12:09:35 +0000263
Stefan Roese1af2a0d2008-11-12 13:18:19 +0100264#ifdef CONFIG_CMD_NAND
Wolfgang Denk11f98842011-12-08 02:01:49 +0000265 puts("NAND: ");
266 nand_init(); /* go init the NAND */
Stefan Roese1af2a0d2008-11-12 13:18:19 +0100267#endif
268
269#if defined(CONFIG_CMD_ONENAND)
270 onenand_init();
271#endif
272
wdenkbb1b8262003-03-27 12:09:35 +0000273 /* relocate environment function pointers etc. */
274 env_relocate();
275
wdenk5401de42004-02-07 01:27:10 +0000276#if defined(CONFIG_PCI)
277 /*
278 * Do pci configuration
279 */
280 pci_init();
281#endif
282
wdenkbb1b8262003-03-27 12:09:35 +0000283/** leave this here (after malloc(), environment and PCI are working) **/
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200284 /* Initialize stdio devices */
Wolfgang Denk11f98842011-12-08 02:01:49 +0000285 stdio_init();
wdenkbb1b8262003-03-27 12:09:35 +0000286
Wolfgang Denk11f98842011-12-08 02:01:49 +0000287 jumptable_init();
wdenkbb1b8262003-03-27 12:09:35 +0000288
289 /* Initialize the console (after the relocation and devices init) */
Wolfgang Denk11f98842011-12-08 02:01:49 +0000290 console_init_r();
wdenkbb1b8262003-03-27 12:09:35 +0000291/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
292
wdenkb02744a2003-04-05 00:53:31 +0000293 /* Initialize from environment */
Simon Glass8ccbef92011-10-13 14:43:11 +0000294 load_addr = getenv_ulong("loadaddr", 16, load_addr);
wdenkb02744a2003-04-05 00:53:31 +0000295
Jason McMullan733ad782008-05-30 00:53:37 +0900296#ifdef CONFIG_CMD_SPI
Wolfgang Denk11f98842011-12-08 02:01:49 +0000297 puts("SPI: ");
298 spi_init(); /* go init the SPI */
299 puts("ready\n");
Jason McMullan733ad782008-05-30 00:53:37 +0900300#endif
301
wdenkb02744a2003-04-05 00:53:31 +0000302#if defined(CONFIG_MISC_INIT_R)
303 /* miscellaneous platform dependent initialisations */
Wolfgang Denk11f98842011-12-08 02:01:49 +0000304 misc_init_r();
wdenkb02744a2003-04-05 00:53:31 +0000305#endif
306
Luigi 'Comio' Mantellini01db1552009-10-10 12:42:21 +0200307#ifdef CONFIG_BITBANGMII
308 bb_miiphy_init();
309#endif
Jon Loeliger6b233052007-07-09 18:02:11 -0500310#if defined(CONFIG_CMD_NET)
Wolfgang Denk11f98842011-12-08 02:01:49 +0000311 puts("Net: ");
wdenkbb1b8262003-03-27 12:09:35 +0000312 eth_initialize(gd->bd);
313#endif
314
315 /* main_loop() can return to retry autoboot, if so just run it again. */
Wolfgang Denk11f98842011-12-08 02:01:49 +0000316 for (;;)
317 main_loop();
wdenkbb1b8262003-03-27 12:09:35 +0000318
319 /* NOTREACHED - no way out of command loop except booting */
320}