blob: 9e6ba15acab5a0401e28614a12192083b99378dd [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
30/*
Jean-Christophe PLAGNIOL-VILLARD089dbb72007-11-13 09:11:05 +010031 * mips_io_port_base is the begin of the address space to which x86 style
32 * I/O ports are mapped.
33 */
Daniel Schwierzeck689eecd2012-12-12 13:13:48 +010034const unsigned long mips_io_port_base = -1;
wdenkbb1b8262003-03-27 12:09:35 +000035
Stefan Roese2b1c8152008-11-12 13:18:02 +010036int __board_early_init_f(void)
37{
38 /*
39 * Nothing to do in this dummy implementation
40 */
41 return 0;
42}
Wolfgang Denk11f98842011-12-08 02:01:49 +000043int board_early_init_f(void)
44 __attribute__((weak, alias("__board_early_init_f")));
Stefan Roese2b1c8152008-11-12 13:18:02 +010045
Wolfgang Denk11f98842011-12-08 02:01:49 +000046static int init_func_ram(void)
wdenkbb1b8262003-03-27 12:09:35 +000047{
wdenkbb1b8262003-03-27 12:09:35 +000048#ifdef CONFIG_BOARD_TYPES
49 int board_type = gd->board_type;
50#else
51 int board_type = 0; /* use dummy arg */
52#endif
Wolfgang Denk11f98842011-12-08 02:01:49 +000053 puts("DRAM: ");
wdenkbb1b8262003-03-27 12:09:35 +000054
Wolfgang Denk11f98842011-12-08 02:01:49 +000055 gd->ram_size = initdram(board_type);
56 if (gd->ram_size > 0) {
57 print_size(gd->ram_size, "\n");
58 return 0;
wdenkbb1b8262003-03-27 12:09:35 +000059 }
Wolfgang Denk11f98842011-12-08 02:01:49 +000060 puts(failed);
61 return 1;
wdenkbb1b8262003-03-27 12:09:35 +000062}
63
64static int display_banner(void)
65{
66
Wolfgang Denk11f98842011-12-08 02:01:49 +000067 printf("\n\n%s\n\n", version_string);
68 return 0;
wdenkbb1b8262003-03-27 12:09:35 +000069}
70
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020071#ifndef CONFIG_SYS_NO_FLASH
wdenkbb1b8262003-03-27 12:09:35 +000072static void display_flash_config(ulong size)
73{
Wolfgang Denk11f98842011-12-08 02:01:49 +000074 puts("Flash: ");
75 print_size(size, "\n");
wdenkbb1b8262003-03-27 12:09:35 +000076}
Jean-Christophe PLAGNIOL-VILLARD3bd04ce2008-02-17 16:58:04 +010077#endif
wdenkbb1b8262003-03-27 12:09:35 +000078
Wolfgang Denk11f98842011-12-08 02:01:49 +000079static int init_baudrate(void)
wdenkbb1b8262003-03-27 12:09:35 +000080{
Simon Glass8ccbef92011-10-13 14:43:11 +000081 gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
82 return 0;
wdenkbb1b8262003-03-27 12:09:35 +000083}
84
85
86/*
87 * Breath some life into the board...
88 *
89 * The first part of initialization is running from Flash memory;
90 * its main purpose is to initialize the RAM so that we
91 * can relocate the monitor code to RAM.
92 */
93
94/*
95 * All attempts to come up with a "common" initialization sequence
96 * that works for all boards and architectures failed: some of the
97 * requirements are just _too_ different. To get rid of the resulting
98 * mess of board dependend #ifdef'ed code we now make the whole
99 * initialization sequence configurable to the user.
100 *
101 * The requirements for any new initalization function is simple: it
102 * receives a pointer to the "global data" structure as it's only
103 * argument, and returns an integer return code, where 0 means
104 * "continue" and != 0 means "fatal error, hang the system".
105 */
Wolfgang Denk11f98842011-12-08 02:01:49 +0000106typedef int (init_fnc_t)(void);
wdenkbb1b8262003-03-27 12:09:35 +0000107
108init_fnc_t *init_sequence[] = {
Stefan Roese2b1c8152008-11-12 13:18:02 +0100109 board_early_init_f,
wdenkbb1b8262003-03-27 12:09:35 +0000110 timer_init,
111 env_init, /* initialize environment */
wdenk67f13362003-12-27 19:24:54 +0000112#ifdef CONFIG_INCA_IP
Wolfgang Denk11f98842011-12-08 02:01:49 +0000113 incaip_set_cpuclk, /* set cpu clock according to env. variable */
wdenk67f13362003-12-27 19:24:54 +0000114#endif
Wolfgang Denk11f98842011-12-08 02:01:49 +0000115 init_baudrate, /* initialize baudrate settings */
wdenkbb1b8262003-03-27 12:09:35 +0000116 serial_init, /* serial communications setup */
117 console_init_f,
118 display_banner, /* say that we are here */
wdenk8dba0502003-03-31 16:34:49 +0000119 checkboard,
wdenkbb1b8262003-03-27 12:09:35 +0000120 init_func_ram,
121 NULL,
122};
123
124
125void board_init_f(ulong bootflag)
126{
wdenkbb1b8262003-03-27 12:09:35 +0000127 gd_t gd_data, *id;
128 bd_t *bd;
129 init_fnc_t **init_fnc_ptr;
Daniel Schwierzeckbd4277d2013-02-12 22:22:12 +0100130 ulong addr, addr_sp, len;
Wolfgang Denk117b0b12005-12-01 02:15:07 +0100131 ulong *s;
wdenkbb1b8262003-03-27 12:09:35 +0000132
wdenkcd914452004-05-29 16:53:29 +0000133 /* Pointer is writable since we allocated a register for it.
134 */
wdenkbb1b8262003-03-27 12:09:35 +0000135 gd = &gd_data;
wdenk3337af42004-07-01 20:28:03 +0000136 /* compiler optimization barrier needed for GCC >= 3.4 */
Wolfgang Denk11f98842011-12-08 02:01:49 +0000137 __asm__ __volatile__("" : : : "memory");
wdenk3337af42004-07-01 20:28:03 +0000138
Wolfgang Denk11f98842011-12-08 02:01:49 +0000139 memset((void *)gd, 0, sizeof(gd_t));
wdenkbb1b8262003-03-27 12:09:35 +0000140
141 for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
Wolfgang Denk11f98842011-12-08 02:01:49 +0000142 if ((*init_fnc_ptr)() != 0)
143 hang();
wdenkbb1b8262003-03-27 12:09:35 +0000144 }
145
146 /*
147 * Now that we have DRAM mapped and working, we can
148 * relocate the code and continue running from DRAM.
149 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200150 addr = CONFIG_SYS_SDRAM_BASE + gd->ram_size;
wdenkbb1b8262003-03-27 12:09:35 +0000151
wdenkcd914452004-05-29 16:53:29 +0000152 /* We can reserve some RAM "on top" here.
153 */
wdenkbb1b8262003-03-27 12:09:35 +0000154
wdenkcd914452004-05-29 16:53:29 +0000155 /* round down to next 4 kB limit.
156 */
wdenkbb1b8262003-03-27 12:09:35 +0000157 addr &= ~(4096 - 1);
Wolfgang Denk11f98842011-12-08 02:01:49 +0000158 debug("Top of RAM usable for U-Boot at: %08lx\n", addr);
wdenk57b2d802003-06-27 21:31:46 +0000159
wdenkcd914452004-05-29 16:53:29 +0000160 /* Reserve memory for U-Boot code, data & bss
161 * round down to next 16 kB limit
162 */
Daniel Schwierzeckbd4277d2013-02-12 22:22:12 +0100163 len = bss_end() - CONFIG_SYS_MONITOR_BASE;
wdenkbb1b8262003-03-27 12:09:35 +0000164 addr -= len;
wdenkb9a83a92003-05-30 12:48:29 +0000165 addr &= ~(16 * 1024 - 1);
wdenkbb1b8262003-03-27 12:09:35 +0000166
Wolfgang Denk11f98842011-12-08 02:01:49 +0000167 debug("Reserving %ldk for U-Boot at: %08lx\n", len >> 10, addr);
wdenkbb1b8262003-03-27 12:09:35 +0000168
wdenkcd914452004-05-29 16:53:29 +0000169 /* Reserve memory for malloc() arena.
170 */
wdenkbb1b8262003-03-27 12:09:35 +0000171 addr_sp = addr - TOTAL_MALLOC_LEN;
Wolfgang Denk11f98842011-12-08 02:01:49 +0000172 debug("Reserving %dk for malloc() at: %08lx\n",
wdenkbb1b8262003-03-27 12:09:35 +0000173 TOTAL_MALLOC_LEN >> 10, addr_sp);
wdenkbb1b8262003-03-27 12:09:35 +0000174
175 /*
176 * (permanently) allocate a Board Info struct
177 * and a permanent copy of the "global" data
178 */
179 addr_sp -= sizeof(bd_t);
180 bd = (bd_t *)addr_sp;
181 gd->bd = bd;
Wolfgang Denk11f98842011-12-08 02:01:49 +0000182 debug("Reserving %zu Bytes for Board Info at: %08lx\n",
wdenkbb1b8262003-03-27 12:09:35 +0000183 sizeof(bd_t), addr_sp);
wdenkcd914452004-05-29 16:53:29 +0000184
wdenkbb1b8262003-03-27 12:09:35 +0000185 addr_sp -= sizeof(gd_t);
186 id = (gd_t *)addr_sp;
Wolfgang Denk11f98842011-12-08 02:01:49 +0000187 debug("Reserving %zu Bytes for Global Data at: %08lx\n",
188 sizeof(gd_t), addr_sp);
wdenkbb1b8262003-03-27 12:09:35 +0000189
Jean-Christophe PLAGNIOL-VILLARD3bd04ce2008-02-17 16:58:04 +0100190 /* Reserve memory for boot params.
wdenkcd914452004-05-29 16:53:29 +0000191 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200192 addr_sp -= CONFIG_SYS_BOOTPARAMS_LEN;
wdenkbb1b8262003-03-27 12:09:35 +0000193 bd->bi_boot_params = addr_sp;
Wolfgang Denk11f98842011-12-08 02:01:49 +0000194 debug("Reserving %dk for boot params() at: %08lx\n",
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200195 CONFIG_SYS_BOOTPARAMS_LEN >> 10, addr_sp);
wdenkbb1b8262003-03-27 12:09:35 +0000196
197 /*
198 * Finally, we set up a new (bigger) stack.
199 *
200 * Leave some safety gap for SP, force alignment on 16 byte boundary
201 * Clear initial stack frame
202 */
203 addr_sp -= 16;
204 addr_sp &= ~0xF;
Wolfgang Denk117b0b12005-12-01 02:15:07 +0100205 s = (ulong *)addr_sp;
206 *s-- = 0;
207 *s-- = 0;
208 addr_sp = (ulong)s;
Wolfgang Denk11f98842011-12-08 02:01:49 +0000209 debug("Stack Pointer at: %08lx\n", addr_sp);
wdenkcd914452004-05-29 16:53:29 +0000210
wdenkbb1b8262003-03-27 12:09:35 +0000211 /*
212 * Save local variables to board info struct
213 */
Wolfgang Denk11f98842011-12-08 02:01:49 +0000214 bd->bi_memstart = CONFIG_SYS_SDRAM_BASE; /* start of DRAM */
215 bd->bi_memsize = gd->ram_size; /* size of DRAM in bytes */
wdenkbb1b8262003-03-27 12:09:35 +0000216 bd->bi_baudrate = gd->baudrate; /* Console Baudrate */
217
Wolfgang Denk11f98842011-12-08 02:01:49 +0000218 memcpy(id, (void *)gd, sizeof(gd_t));
wdenkb02744a2003-04-05 00:53:31 +0000219
Wolfgang Denk11f98842011-12-08 02:01:49 +0000220 relocate_code(addr_sp, id, addr);
wdenkbb1b8262003-03-27 12:09:35 +0000221
222 /* NOTREACHED - relocate_code() does not return */
223}
Wolfgang Denk11f98842011-12-08 02:01:49 +0000224
225/*
wdenkbb1b8262003-03-27 12:09:35 +0000226 * This is the next part if the initialization sequence: we are now
227 * running from RAM and have a "normal" C environment, i. e. global
228 * data can be written, BSS has been cleared, the stack size in not
229 * that critical any more, etc.
wdenkbb1b8262003-03-27 12:09:35 +0000230 */
231
Wolfgang Denk11f98842011-12-08 02:01:49 +0000232void board_init_r(gd_t *id, ulong dest_addr)
wdenkbb1b8262003-03-27 12:09:35 +0000233{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200234#ifndef CONFIG_SYS_NO_FLASH
wdenkbb1b8262003-03-27 12:09:35 +0000235 ulong size;
Jean-Christophe PLAGNIOL-VILLARD3bd04ce2008-02-17 16:58:04 +0100236#endif
wdenkbb1b8262003-03-27 12:09:35 +0000237 bd_t *bd;
wdenkbb1b8262003-03-27 12:09:35 +0000238
239 gd = id;
240 gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
241
Wolfgang Denk11f98842011-12-08 02:01:49 +0000242 debug("Now running in RAM - U-Boot at: %08lx\n", dest_addr);
wdenkbb1b8262003-03-27 12:09:35 +0000243
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200244 gd->reloc_off = dest_addr - CONFIG_SYS_MONITOR_BASE;
wdenkbb1b8262003-03-27 12:09:35 +0000245
Daniel Schwierzeckbd4277d2013-02-12 22:22:12 +0100246 monitor_flash_len = image_copy_end() - dest_addr;
wdenkb9a83a92003-05-30 12:48:29 +0000247
Joe Hershberger7fe0ac82012-12-11 17:52:50 +0000248 serial_initialize();
249
Jean-Christophe PLAGNIOL-VILLARD3bd04ce2008-02-17 16:58:04 +0100250 bd = gd->bd;
251
Peter Tyser781c9b82009-08-21 23:05:21 -0500252 /* The Malloc area is immediately below the monitor copy in DRAM */
Peter Tysered527702009-08-21 23:05:20 -0500253 mem_malloc_init(CONFIG_SYS_MONITOR_BASE + gd->reloc_off -
254 TOTAL_MALLOC_LEN, TOTAL_MALLOC_LEN);
Stefan Roeseb81464e2009-05-11 15:50:12 +0200255
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200256#ifndef CONFIG_SYS_NO_FLASH
wdenkbb1b8262003-03-27 12:09:35 +0000257 /* configure available FLASH banks */
258 size = flash_init();
Wolfgang Denk11f98842011-12-08 02:01:49 +0000259 display_flash_config(size);
Daniel Schwierzeck0d15d2a2012-04-02 02:57:54 +0000260 bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
Jean-Christophe PLAGNIOL-VILLARD3bd04ce2008-02-17 16:58:04 +0100261 bd->bi_flashsize = size;
wdenkbb1b8262003-03-27 12:09:35 +0000262
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200263#if CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
wdenkb9a83a92003-05-30 12:48:29 +0000264 bd->bi_flashoffset = monitor_flash_len; /* reserved area for U-Boot */
wdenkbb1b8262003-03-27 12:09:35 +0000265#else
266 bd->bi_flashoffset = 0;
267#endif
Daniel Schwierzeck0d15d2a2012-04-02 02:57:54 +0000268#else
269 bd->bi_flashstart = 0;
270 bd->bi_flashsize = 0;
271 bd->bi_flashoffset = 0;
272#endif
wdenkbb1b8262003-03-27 12:09:35 +0000273
Stefan Roese1af2a0d2008-11-12 13:18:19 +0100274#ifdef CONFIG_CMD_NAND
Wolfgang Denk11f98842011-12-08 02:01:49 +0000275 puts("NAND: ");
276 nand_init(); /* go init the NAND */
Stefan Roese1af2a0d2008-11-12 13:18:19 +0100277#endif
278
279#if defined(CONFIG_CMD_ONENAND)
280 onenand_init();
281#endif
282
wdenkbb1b8262003-03-27 12:09:35 +0000283 /* relocate environment function pointers etc. */
284 env_relocate();
285
wdenk5401de42004-02-07 01:27:10 +0000286#if defined(CONFIG_PCI)
287 /*
288 * Do pci configuration
289 */
290 pci_init();
291#endif
292
wdenkbb1b8262003-03-27 12:09:35 +0000293/** leave this here (after malloc(), environment and PCI are working) **/
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200294 /* Initialize stdio devices */
Wolfgang Denk11f98842011-12-08 02:01:49 +0000295 stdio_init();
wdenkbb1b8262003-03-27 12:09:35 +0000296
Wolfgang Denk11f98842011-12-08 02:01:49 +0000297 jumptable_init();
wdenkbb1b8262003-03-27 12:09:35 +0000298
299 /* Initialize the console (after the relocation and devices init) */
Wolfgang Denk11f98842011-12-08 02:01:49 +0000300 console_init_r();
wdenkbb1b8262003-03-27 12:09:35 +0000301/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
302
wdenkb02744a2003-04-05 00:53:31 +0000303 /* Initialize from environment */
Simon Glass8ccbef92011-10-13 14:43:11 +0000304 load_addr = getenv_ulong("loadaddr", 16, load_addr);
wdenkb02744a2003-04-05 00:53:31 +0000305
Jason McMullan733ad782008-05-30 00:53:37 +0900306#ifdef CONFIG_CMD_SPI
Wolfgang Denk11f98842011-12-08 02:01:49 +0000307 puts("SPI: ");
308 spi_init(); /* go init the SPI */
309 puts("ready\n");
Jason McMullan733ad782008-05-30 00:53:37 +0900310#endif
311
wdenkb02744a2003-04-05 00:53:31 +0000312#if defined(CONFIG_MISC_INIT_R)
313 /* miscellaneous platform dependent initialisations */
Wolfgang Denk11f98842011-12-08 02:01:49 +0000314 misc_init_r();
wdenkb02744a2003-04-05 00:53:31 +0000315#endif
316
Luigi 'Comio' Mantellini01db1552009-10-10 12:42:21 +0200317#ifdef CONFIG_BITBANGMII
318 bb_miiphy_init();
319#endif
Jon Loeliger6b233052007-07-09 18:02:11 -0500320#if defined(CONFIG_CMD_NET)
Wolfgang Denk11f98842011-12-08 02:01:49 +0000321 puts("Net: ");
wdenkbb1b8262003-03-27 12:09:35 +0000322 eth_initialize(gd->bd);
323#endif
324
325 /* main_loop() can return to retry autoboot, if so just run it again. */
Wolfgang Denk11f98842011-12-08 02:01:49 +0000326 for (;;)
327 main_loop();
wdenkbb1b8262003-03-27 12:09:35 +0000328
329 /* NOTREACHED - no way out of command loop except booting */
330}