blob: 1157d8c5035f7e2eb73dd862dfdc3e2a2234ed35 [file] [log] [blame]
Macpaul Lin0d1f4742011-10-11 22:33:19 +00001/*
2 * (C) Copyright 2002-2006
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * Copyright (C) 2011 Andes Technology Corporation
6 * Shawn Lin, Andes Technology Corporation <nobuhiro@andestech.com>
7 * Macpaul Lin, Andes Technology Corporation <macpaul@andestech.com>
8 *
9 * See file CREDITS for list of people who contributed to this
10 * project.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * MA 02111-1307 USA
26 */
27
28#include <common.h>
29#include <command.h>
30#include <malloc.h>
31#include <stdio_dev.h>
32#include <timestamp.h>
33#include <version.h>
34#include <net.h>
35#include <serial.h>
36#include <nand.h>
37#include <onenand_uboot.h>
38#include <mmc.h>
Kuan-Yu Kuob73752a2013-04-23 07:47:47 +000039#include <asm/sections.h>
Macpaul Lin0d1f4742011-10-11 22:33:19 +000040
41DECLARE_GLOBAL_DATA_PTR;
42
43ulong monitor_flash_len;
44
45/*
46 * Init Utilities
47 */
48
49#if !defined(CONFIG_BAUDRATE)
50#define CONFIG_BAUDRATE 38400
51#endif
52static int init_baudrate(void)
53{
Macpaul Lin48260952011-10-24 16:43:17 +080054 gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
Macpaul Lin0d1f4742011-10-11 22:33:19 +000055 return 0;
56}
57
58/*
59 * WARNING: this code looks "cleaner" than the PowerPC version, but
60 * has the disadvantage that you either get nothing, or everything.
61 * On PowerPC, you might see "DRAM: " before the system hangs - which
62 * gives a simple yet clear indication which part of the
63 * initialization if failing.
64 */
65static int display_dram_config(void)
66{
67 int i;
68
69#ifdef DEBUG
70 puts("RAM Configuration:\n");
71
72 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
73 printf("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
74 print_size(gd->bd->bi_dram[i].size, "\n");
75 }
76#else
77 ulong size = 0;
78
79 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++)
80 size += gd->bd->bi_dram[i].size;
81
82 puts("DRAM: ");
83 print_size(size, "\n");
84#endif
85
86 return 0;
87}
88
89#ifndef CONFIG_SYS_NO_FLASH
90static void display_flash_config(ulong size)
91{
92 puts("Flash: ");
93 print_size(size, "\n");
94}
95#endif /* CONFIG_SYS_NO_FLASH */
96
97#if defined(CONFIG_CMD_PCI) || defined(CONFIG_PCI)
98#include <pci.h>
99static int nds32_pci_init(void)
100{
101 pci_init();
102 return 0;
103}
104#endif /* CONFIG_CMD_PCI || CONFIG_PCI */
105
106#if defined(CONFIG_PMU) || defined(CONFIG_PCU)
Mike Frysinger1ab1e612012-08-06 13:46:37 +0000107#ifndef CONFIG_SKIP_LOWLEVEL_INIT
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000108static int pmu_init(void)
109{
110#if defined(CONFIG_FTPMU010_POWER)
111#ifdef __NDS32_N1213_43U1H__ /* AG101: internal definition in toolchain */
112 ftpmu010_sdram_clk_disable(CONFIG_SYS_FTPMU010_PDLLCR0_HCLKOUTDIS);
113 ftpmu010_mfpsr_select_dev(FTPMU010_MFPSR_AC97CLKSEL);
114 ftpmu010_sdramhtc_set(CONFIG_SYS_FTPMU010_SDRAMHTC);
115#endif /* __NDS32_N1213_43U1H__ */
116#endif
117 return 0;
118}
119#endif
Mike Frysinger1ab1e612012-08-06 13:46:37 +0000120#endif
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000121
122/*
123 * Breathe some life into the board...
124 *
125 * Initialize a serial port as console, and carry out some hardware
126 * tests.
127 *
128 * The first part of initialization is running from Flash memory;
129 * its main purpose is to initialize the RAM so that we
130 * can relocate the monitor code to RAM.
131 */
132
133/*
134 * All attempts to come up with a "common" initialization sequence
135 * that works for all boards and architectures failed: some of the
136 * requirements are just _too_ different. To get rid of the resulting
137 * mess of board dependent #ifdef'ed code we now make the whole
138 * initialization sequence configurable to the user.
139 *
140 * The requirements for any new initalization function is simple: it
141 * receives a pointer to the "global data" structure as it's only
142 * argument, and returns an integer return code, where 0 means
143 * "continue" and != 0 means "fatal error, hang the system".
144 */
145typedef int (init_fnc_t)(void);
146
147void __dram_init_banksize(void)
148{
149 gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
150 gd->bd->bi_dram[0].size = gd->ram_size;
151}
152void dram_init_banksize(void)
153 __attribute__((weak, alias("__dram_init_banksize")));
154
155init_fnc_t *init_sequence[] = {
156#if defined(CONFIG_ARCH_CPU_INIT)
157 arch_cpu_init, /* basic arch cpu dependent setup */
158#endif
159#if defined(CONFIG_PMU) || defined(CONFIG_PCU)
160#ifndef CONFIG_SKIP_LOWLEVEL_INIT
161 pmu_init,
162#endif
163#endif
164 board_init, /* basic board dependent setup */
165#if defined(CONFIG_USE_IRQ)
166 interrupt_init, /* set up exceptions */
167#endif
168 timer_init, /* initialize timer */
169 env_init, /* initialize environment */
170 init_baudrate, /* initialze baudrate settings */
171 serial_init, /* serial communications setup */
172 console_init_f, /* stage 1 init of console */
173#if defined(CONFIG_DISPLAY_BOARDINFO)
174 checkboard, /* display board info */
175#endif
176#if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
177 init_func_i2c,
178#endif
179 dram_init, /* configure available RAM banks */
180 display_dram_config,
181 NULL,
182};
183
184void board_init_f(ulong bootflag)
185{
186 bd_t *bd;
187 init_fnc_t **init_fnc_ptr;
188 gd_t *id;
189 ulong addr, addr_sp;
190
191 /* Pointer is writable since we allocated a register for it */
192 gd = (gd_t *) ((CONFIG_SYS_INIT_SP_ADDR) & ~0x07);
193
194 memset((void *)gd, 0, GENERATED_GBL_DATA_SIZE);
195
Simon Glassed70c8f2013-03-14 06:54:53 +0000196 gd->mon_len = (unsigned int)(&__bss_end) - (unsigned int)(&_start);
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000197
198 for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
199 if ((*init_fnc_ptr)() != 0)
200 hang();
201 }
202
203 debug("monitor len: %08lX\n", gd->mon_len);
204 /*
205 * Ram is setup, size stored in gd !!
206 */
207 debug("ramsize: %08lX\n", gd->ram_size);
208
209 addr = CONFIG_SYS_SDRAM_BASE + gd->ram_size;
210
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000211 /* round down to next 4 kB limit */
212 addr &= ~(4096 - 1);
213 debug("Top of RAM usable for U-Boot at: %08lx\n", addr);
214
215#ifdef CONFIG_LCD
216#ifdef CONFIG_FB_ADDR
217 gd->fb_base = CONFIG_FB_ADDR;
218#else
219 /* reserve memory for LCD display (always full pages) */
220 addr = lcd_setmem(addr);
221 gd->fb_base = addr;
222#endif /* CONFIG_FB_ADDR */
223#endif /* CONFIG_LCD */
224
225 /*
226 * reserve memory for U-Boot code, data & bss
227 * round down to next 4 kB limit
228 */
229 addr -= gd->mon_len;
230 addr &= ~(4096 - 1);
231
232 debug("Reserving %ldk for U-Boot at: %08lx\n", gd->mon_len >> 10, addr);
233
234 /*
235 * reserve memory for malloc() arena
236 */
237 addr_sp = addr - TOTAL_MALLOC_LEN;
238 debug("Reserving %dk for malloc() at: %08lx\n",
239 TOTAL_MALLOC_LEN >> 10, addr_sp);
240 /*
241 * (permanently) allocate a Board Info struct
242 * and a permanent copy of the "global" data
243 */
244 addr_sp -= GENERATED_BD_INFO_SIZE;
245 bd = (bd_t *) addr_sp;
246 gd->bd = bd;
247 memset((void *)bd, 0, GENERATED_BD_INFO_SIZE);
248 debug("Reserving %zu Bytes for Board Info at: %08lx\n",
249 GENERATED_BD_INFO_SIZE, addr_sp);
250
251 addr_sp -= GENERATED_GBL_DATA_SIZE;
252 id = (gd_t *) addr_sp;
253 debug("Reserving %zu Bytes for Global Data at: %08lx\n",
254 GENERATED_GBL_DATA_SIZE, addr_sp);
255
256 /* setup stackpointer for exeptions */
257 gd->irq_sp = addr_sp;
258#ifdef CONFIG_USE_IRQ
259 addr_sp -= (CONFIG_STACKSIZE_IRQ + CONFIG_STACKSIZE_FIQ);
260 debug("Reserving %zu Bytes for IRQ stack at: %08lx\n",
261 CONFIG_STACKSIZE_IRQ+CONFIG_STACKSIZE_FIQ, addr_sp);
262#endif
263 /* leave 3 words for abort-stack */
264 addr_sp -= 12;
265
266 /* 8-byte alignment for ABI compliance */
267 addr_sp &= ~0x07;
268 debug("New Stack Pointer is: %08lx\n", addr_sp);
269
270 gd->bd->bi_baudrate = gd->baudrate;
271 /* Ram isn't board specific, so move it to board code ... */
272 dram_init_banksize();
273 display_dram_config(); /* and display it */
274
275 gd->relocaddr = addr;
276 gd->start_addr_sp = addr_sp;
277
278 gd->reloc_off = addr - _TEXT_BASE;
279
280 debug("relocation Offset is: %08lx\n", gd->reloc_off);
281 memcpy(id, (void *)gd, GENERATED_GBL_DATA_SIZE);
282
283 relocate_code(addr_sp, id, addr);
284
285 /* NOTREACHED - relocate_code() does not return */
286}
287
288/*
289 * This is the next part if the initialization sequence: we are now
290 * running from RAM and have a "normal" C environment, i. e. global
291 * data can be written, BSS has been cleared, the stack size in not
292 * that critical any more, etc.
293 */
294void board_init_r(gd_t *id, ulong dest_addr)
295{
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000296 bd_t *bd;
297 ulong malloc_start;
298
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000299 gd = id;
300 bd = gd->bd;
301
302 gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
303
Kuan-Yu Kuob73752a2013-04-23 07:47:47 +0000304 monitor_flash_len = (ulong)&_end - (ulong)&_start;
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000305 debug("monitor flash len: %08lX\n", monitor_flash_len);
306
307 board_init(); /* Setup chipselects */
308
309#if defined(CONFIG_NEEDS_MANUAL_RELOC)
310 /*
311 * We have to relocate the command table manually
312 */
Marek Vasut69c5e342012-10-12 10:27:04 +0000313 fixup_cmdtable(ll_entry_start(cmd_tbl_t, cmd),
314 ll_entry_count(cmd_tbl_t, cmd));
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000315#endif /* defined(CONFIG_NEEDS_MANUAL_RELOC) */
316
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000317 serial_initialize();
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000318
319 debug("Now running in RAM - U-Boot at: %08lx\n", dest_addr);
320
321 /* The Malloc area is immediately below the monitor copy in DRAM */
322 malloc_start = dest_addr - TOTAL_MALLOC_LEN;
323 mem_malloc_init(malloc_start, TOTAL_MALLOC_LEN);
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000324
325#ifndef CONFIG_SYS_NO_FLASH
326 /* configure available FLASH banks */
327 gd->bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
328 gd->bd->bi_flashsize = flash_init();
329 gd->bd->bi_flashoffset = CONFIG_SYS_FLASH_BASE + gd->bd->bi_flashsize;
330
331 if (gd->bd->bi_flashsize)
332 display_flash_config(gd->bd->bi_flashsize);
333#endif /* CONFIG_SYS_NO_FLASH */
334
335#if defined(CONFIG_CMD_NAND)
336 puts("NAND: ");
337 nand_init(); /* go init the NAND */
338#endif
339
Macpaul Linb381d912011-11-18 17:01:31 +0800340#if defined(CONFIG_CMD_IDE)
341 puts("IDE: ");
342 ide_init();
343#endif
344
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000345#ifdef CONFIG_GENERIC_MMC
346 puts("MMC: ");
347 mmc_initialize(gd->bd);
348#endif
349
350 /* initialize environment */
351 env_relocate();
352
353#if defined(CONFIG_CMD_PCI) || defined(CONFIG_PCI)
Macpaul Line474e772011-11-25 17:14:51 +0800354 puts("PCI: ");
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000355 nds32_pci_init();
356#endif
357
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000358 stdio_init(); /* get the devices list going. */
359
360 jumptable_init();
361
362#if defined(CONFIG_API)
363 /* Initialize API */
364 api_init();
365#endif
366
367 console_init_r(); /* fully init console as a device */
368
369#if defined(CONFIG_ARCH_MISC_INIT)
370 /* miscellaneous arch dependent initialisations */
371 arch_misc_init();
372#endif
373#if defined(CONFIG_MISC_INIT_R)
374 /* miscellaneous platform dependent initialisations */
375 misc_init_r();
376#endif
377
378#if defined(CONFIG_USE_IRQ)
379 /* set up exceptions */
380 interrupt_init();
381 /* enable exceptions */
382 enable_interrupts();
383#endif
384
385 /* Initialize from environment */
Macpaul Lin48260952011-10-24 16:43:17 +0800386 load_addr = getenv_ulong("loadaddr", 16, load_addr);
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000387
Nobuhiro Iwamatsu4298c932012-04-17 16:41:14 +0000388#ifdef CONFIG_BOARD_LATE_INIT
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000389 board_late_init();
390#endif
391
392#if defined(CONFIG_CMD_NET)
393 puts("Net: ");
394
395 eth_initialize(gd->bd);
396#if defined(CONFIG_RESET_PHY_R)
397 debug("Reset Ethernet PHY\n");
398 reset_phy();
399#endif
400#endif
401
402 /* main_loop() can return to retry autoboot, if so just run it again. */
403 for (;;)
404 main_loop();
405
406 /* NOTREACHED - no way out of command loop except booting */
407}