blob: 83858c1ffe5ca7c738fc6fe79fd95958910fcdb9 [file] [log] [blame]
Simon Glassb8605a12011-10-03 19:26:37 +00001/*
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
Matthias Weisser21899b12011-11-05 11:40:34 +010048#include <os.h>
49
Simon Glassb8605a12011-10-03 19:26:37 +000050DECLARE_GLOBAL_DATA_PTR;
51
Matthias Weisser21899b12011-11-05 11:40:34 +010052static gd_t gd_mem;
53
Simon Glassb8605a12011-10-03 19:26:37 +000054/************************************************************************
55 * Init Utilities *
56 ************************************************************************
57 * Some of this code should be moved into the core functions,
58 * or dropped completely,
59 * but let's get it working (again) first...
60 */
61
62static int display_banner(void)
63{
64 display_options();
65
66 return 0;
67}
68
69/**
70 * Configure and report on the DRAM configuration, which in our case is
71 * fairly simple.
72 */
73static int display_dram_config(void)
74{
75 ulong size = 0;
76 int i;
77
78 debug("RAM Configuration:\n");
79
80 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
81#ifdef DEBUG
82 printf("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
83 print_size(gd->bd->bi_dram[i].size, "\n");
84#endif
85 size += gd->bd->bi_dram[i].size;
86 }
87 puts("DRAM: ");
88 print_size(size, "\n");
89 return 0;
90}
91
92/*
93 * Breathe some life into the board...
94 *
95 * Initialize a serial port as console, and carry out some hardware
96 * tests.
97 *
98 * The first part of initialization is running from Flash memory;
99 * its main purpose is to initialize the RAM so that we
100 * can relocate the monitor code to RAM.
101 */
102
103/*
104 * All attempts to come up with a "common" initialization sequence
105 * that works for all boards and architectures failed: some of the
106 * requirements are just _too_ different. To get rid of the resulting
107 * mess of board dependent #ifdef'ed code we now make the whole
108 * initialization sequence configurable to the user.
109 *
110 * The requirements for any new initalization function is simple: it
111 * receives a pointer to the "global data" structure as it's only
112 * argument, and returns an integer return code, where 0 means
113 * "continue" and != 0 means "fatal error, hang the system".
114 */
115typedef int (init_fnc_t) (void);
116
117void __dram_init_banksize(void)
118{
119 gd->bd->bi_dram[0].start = 0;
120 gd->bd->bi_dram[0].size = gd->ram_size;
121}
122
123void dram_init_banksize(void)
124 __attribute__((weak, alias("__dram_init_banksize")));
125
126init_fnc_t *init_sequence[] = {
127#if defined(CONFIG_ARCH_CPU_INIT)
128 arch_cpu_init, /* basic arch cpu dependent setup */
129#endif
130#if defined(CONFIG_BOARD_EARLY_INIT_F)
131 board_early_init_f,
132#endif
133 timer_init, /* initialize timer */
134 env_init, /* initialize environment */
135 serial_init, /* serial communications setup */
136 console_init_f, /* stage 1 init of console */
Simon Glass70db4212012-02-15 15:51:16 -0800137 sandbox_early_getopt_check, /* process command line flags (err/help) */
Simon Glassb8605a12011-10-03 19:26:37 +0000138 display_banner, /* say that we are here */
139#if defined(CONFIG_DISPLAY_CPUINFO)
140 print_cpuinfo, /* display cpu info (and speed) */
141#endif
142#if defined(CONFIG_DISPLAY_BOARDINFO)
143 checkboard, /* display board info */
144#endif
145 dram_init, /* configure available RAM banks */
146 NULL,
147};
148
149void board_init_f(ulong bootflag)
150{
151 init_fnc_t **init_fnc_ptr;
152 uchar *mem;
153 unsigned long addr_sp, addr, size;
154
Matthias Weisser21899b12011-11-05 11:40:34 +0100155 gd = &gd_mem;
Simon Glassb8605a12011-10-03 19:26:37 +0000156 assert(gd);
157
158 memset((void *)gd, 0, sizeof(gd_t));
159
Simon Glassf7b2af02012-02-15 15:51:11 -0800160#if defined(CONFIG_OF_EMBED)
161 /* Get a pointer to the FDT */
162 gd->fdt_blob = _binary_dt_dtb_start;
163#elif defined(CONFIG_OF_SEPARATE)
164 /* FDT is at end of image */
165 gd->fdt_blob = (void *)(_end_ofs + _TEXT_BASE);
166#endif
167
Simon Glassb8605a12011-10-03 19:26:37 +0000168 for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
169 if ((*init_fnc_ptr)() != 0)
170 hang();
171 }
172
173 size = CONFIG_SYS_SDRAM_SIZE;
Matthias Weisser21899b12011-11-05 11:40:34 +0100174 mem = os_malloc(CONFIG_SYS_SDRAM_SIZE);
175
Simon Glassb8605a12011-10-03 19:26:37 +0000176 assert(mem);
177 gd->ram_buf = mem;
178 addr = (ulong)(mem + size);
179
180 /*
181 * reserve memory for malloc() arena
182 */
183 addr_sp = addr - TOTAL_MALLOC_LEN;
184 debug("Reserving %dk for malloc() at: %08lx\n",
185 TOTAL_MALLOC_LEN >> 10, addr_sp);
186 /*
187 * (permanently) allocate a Board Info struct
188 * and a permanent copy of the "global" data
189 */
190 addr_sp -= sizeof(bd_t);
191 gd->bd = (bd_t *) addr_sp;
192 debug("Reserving %zu Bytes for Board Info at: %08lx\n",
193 sizeof(bd_t), addr_sp);
194
195 /* Ram ist board specific, so move it to board code ... */
196 dram_init_banksize();
197 display_dram_config(); /* and display it */
198
199 /* We don't relocate, so just run the post-relocation code */
200 board_init_r(NULL, 0);
201
202 /* NOTREACHED - no way out of command loop except booting */
203}
204
205/************************************************************************
206 *
207 * This is the next part if the initialization sequence: we are now
208 * running from RAM and have a "normal" C environment, i. e. global
209 * data can be written, BSS has been cleared, the stack size in not
210 * that critical any more, etc.
211 *
212 ************************************************************************
213 */
214
215void board_init_r(gd_t *id, ulong dest_addr)
216{
217
218 if (id)
219 gd = id;
220
221 gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
222
Simon Glassb8605a12011-10-03 19:26:37 +0000223 serial_initialize();
Simon Glassb8605a12011-10-03 19:26:37 +0000224
225#ifdef CONFIG_POST
226 post_output_backlog();
227#endif
228
Matthias Weisser21899b12011-11-05 11:40:34 +0100229 /* The Malloc area is at the top of simulated DRAM */
230 mem_malloc_init((ulong)gd->ram_buf + gd->ram_size - TOTAL_MALLOC_LEN,
231 TOTAL_MALLOC_LEN);
Simon Glassb8605a12011-10-03 19:26:37 +0000232
233 /* initialize environment */
234 env_relocate();
235
Simon Glassb8605a12011-10-03 19:26:37 +0000236 stdio_init(); /* get the devices list going. */
237
238 jumptable_init();
239
240 console_init_r(); /* fully init console as a device */
241
242#if defined(CONFIG_DISPLAY_BOARDINFO_LATE)
243 checkboard();
244#endif
245
246#if defined(CONFIG_ARCH_MISC_INIT)
247 /* miscellaneous arch dependent initialisations */
248 arch_misc_init();
249#endif
250#if defined(CONFIG_MISC_INIT_R)
251 /* miscellaneous platform dependent initialisations */
252 misc_init_r();
253#endif
254
255 /* set up exceptions */
256 interrupt_init();
257 /* enable exceptions */
258 enable_interrupts();
259
Helmut Raiger9660e442011-10-20 04:19:47 +0000260#ifdef CONFIG_BOARD_LATE_INIT
Simon Glassb8605a12011-10-03 19:26:37 +0000261 board_late_init();
262#endif
263
264#ifdef CONFIG_POST
265 post_run(NULL, POST_RAM | post_bootmode_get(0));
266#endif
267
Simon Glassab4e07e2012-02-26 17:38:50 -0500268 sandbox_main_loop_init();
269
Simon Glassb8605a12011-10-03 19:26:37 +0000270 /*
271 * For now, run the main loop. Later we might let this be done
272 * in the main program.
273 */
274 while (1)
275 main_loop();
276
277 /* NOTREACHED - no way out of command loop except booting */
278}
279
280void hang(void)
281{
282 puts("### ERROR ### Please RESET the board ###\n");
283 for (;;)
284 ;
285}