blob: f5de26678a625484ea4c55f620feab3d97a50f91 [file] [log] [blame]
Macpaul Lin463d47f2011-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>
39
40DECLARE_GLOBAL_DATA_PTR;
41
42ulong monitor_flash_len;
43
44/*
45 * Init Utilities
46 */
47
48#if !defined(CONFIG_BAUDRATE)
49#define CONFIG_BAUDRATE 38400
50#endif
51static int init_baudrate(void)
52{
Macpaul Lin569bc622011-10-24 16:43:17 +080053 gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
Macpaul Lin463d47f2011-10-11 22:33:19 +000054 return 0;
55}
56
57/*
58 * WARNING: this code looks "cleaner" than the PowerPC version, but
59 * has the disadvantage that you either get nothing, or everything.
60 * On PowerPC, you might see "DRAM: " before the system hangs - which
61 * gives a simple yet clear indication which part of the
62 * initialization if failing.
63 */
64static int display_dram_config(void)
65{
66 int i;
67
68#ifdef DEBUG
69 puts("RAM Configuration:\n");
70
71 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
72 printf("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
73 print_size(gd->bd->bi_dram[i].size, "\n");
74 }
75#else
76 ulong size = 0;
77
78 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++)
79 size += gd->bd->bi_dram[i].size;
80
81 puts("DRAM: ");
82 print_size(size, "\n");
83#endif
84
85 return 0;
86}
87
88#ifndef CONFIG_SYS_NO_FLASH
89static void display_flash_config(ulong size)
90{
91 puts("Flash: ");
92 print_size(size, "\n");
93}
94#endif /* CONFIG_SYS_NO_FLASH */
95
96#if defined(CONFIG_CMD_PCI) || defined(CONFIG_PCI)
97#include <pci.h>
98static int nds32_pci_init(void)
99{
100 pci_init();
101 return 0;
102}
103#endif /* CONFIG_CMD_PCI || CONFIG_PCI */
104
105#if defined(CONFIG_PMU) || defined(CONFIG_PCU)
106static int pmu_init(void)
107{
108#if defined(CONFIG_FTPMU010_POWER)
109#ifdef __NDS32_N1213_43U1H__ /* AG101: internal definition in toolchain */
110 ftpmu010_sdram_clk_disable(CONFIG_SYS_FTPMU010_PDLLCR0_HCLKOUTDIS);
111 ftpmu010_mfpsr_select_dev(FTPMU010_MFPSR_AC97CLKSEL);
112 ftpmu010_sdramhtc_set(CONFIG_SYS_FTPMU010_SDRAMHTC);
113#endif /* __NDS32_N1213_43U1H__ */
114#endif
115 return 0;
116}
117#endif
118
119/*
120 * Breathe some life into the board...
121 *
122 * Initialize a serial port as console, and carry out some hardware
123 * tests.
124 *
125 * The first part of initialization is running from Flash memory;
126 * its main purpose is to initialize the RAM so that we
127 * can relocate the monitor code to RAM.
128 */
129
130/*
131 * All attempts to come up with a "common" initialization sequence
132 * that works for all boards and architectures failed: some of the
133 * requirements are just _too_ different. To get rid of the resulting
134 * mess of board dependent #ifdef'ed code we now make the whole
135 * initialization sequence configurable to the user.
136 *
137 * The requirements for any new initalization function is simple: it
138 * receives a pointer to the "global data" structure as it's only
139 * argument, and returns an integer return code, where 0 means
140 * "continue" and != 0 means "fatal error, hang the system".
141 */
142typedef int (init_fnc_t)(void);
143
144void __dram_init_banksize(void)
145{
146 gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
147 gd->bd->bi_dram[0].size = gd->ram_size;
148}
149void dram_init_banksize(void)
150 __attribute__((weak, alias("__dram_init_banksize")));
151
152init_fnc_t *init_sequence[] = {
153#if defined(CONFIG_ARCH_CPU_INIT)
154 arch_cpu_init, /* basic arch cpu dependent setup */
155#endif
156#if defined(CONFIG_PMU) || defined(CONFIG_PCU)
157#ifndef CONFIG_SKIP_LOWLEVEL_INIT
158 pmu_init,
159#endif
160#endif
161 board_init, /* basic board dependent setup */
162#if defined(CONFIG_USE_IRQ)
163 interrupt_init, /* set up exceptions */
164#endif
165 timer_init, /* initialize timer */
166 env_init, /* initialize environment */
167 init_baudrate, /* initialze baudrate settings */
168 serial_init, /* serial communications setup */
169 console_init_f, /* stage 1 init of console */
170#if defined(CONFIG_DISPLAY_BOARDINFO)
171 checkboard, /* display board info */
172#endif
173#if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
174 init_func_i2c,
175#endif
176 dram_init, /* configure available RAM banks */
177 display_dram_config,
178 NULL,
179};
180
181void board_init_f(ulong bootflag)
182{
183 bd_t *bd;
184 init_fnc_t **init_fnc_ptr;
185 gd_t *id;
186 ulong addr, addr_sp;
187
188 /* Pointer is writable since we allocated a register for it */
189 gd = (gd_t *) ((CONFIG_SYS_INIT_SP_ADDR) & ~0x07);
190
191 memset((void *)gd, 0, GENERATED_GBL_DATA_SIZE);
192
193 gd->mon_len = (unsigned int)(&__bss_end__) - (unsigned int)(&_start);
194
195 for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
196 if ((*init_fnc_ptr)() != 0)
197 hang();
198 }
199
200 debug("monitor len: %08lX\n", gd->mon_len);
201 /*
202 * Ram is setup, size stored in gd !!
203 */
204 debug("ramsize: %08lX\n", gd->ram_size);
205
206 addr = CONFIG_SYS_SDRAM_BASE + gd->ram_size;
207
208#if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF))
209 /* reserve TLB table */
210 addr -= (4096 * 4);
211
212 /* round down to next 64 kB limit */
213 addr &= ~(0x10000 - 1);
214
215 gd->tlb_addr = addr;
216 debug("TLB table at: %08lx\n", addr);
217#endif
218
219 /* round down to next 4 kB limit */
220 addr &= ~(4096 - 1);
221 debug("Top of RAM usable for U-Boot at: %08lx\n", addr);
222
223#ifdef CONFIG_LCD
224#ifdef CONFIG_FB_ADDR
225 gd->fb_base = CONFIG_FB_ADDR;
226#else
227 /* reserve memory for LCD display (always full pages) */
228 addr = lcd_setmem(addr);
229 gd->fb_base = addr;
230#endif /* CONFIG_FB_ADDR */
231#endif /* CONFIG_LCD */
232
233 /*
234 * reserve memory for U-Boot code, data & bss
235 * round down to next 4 kB limit
236 */
237 addr -= gd->mon_len;
238 addr &= ~(4096 - 1);
239
240 debug("Reserving %ldk for U-Boot at: %08lx\n", gd->mon_len >> 10, addr);
241
242 /*
243 * reserve memory for malloc() arena
244 */
245 addr_sp = addr - TOTAL_MALLOC_LEN;
246 debug("Reserving %dk for malloc() at: %08lx\n",
247 TOTAL_MALLOC_LEN >> 10, addr_sp);
248 /*
249 * (permanently) allocate a Board Info struct
250 * and a permanent copy of the "global" data
251 */
252 addr_sp -= GENERATED_BD_INFO_SIZE;
253 bd = (bd_t *) addr_sp;
254 gd->bd = bd;
255 memset((void *)bd, 0, GENERATED_BD_INFO_SIZE);
256 debug("Reserving %zu Bytes for Board Info at: %08lx\n",
257 GENERATED_BD_INFO_SIZE, addr_sp);
258
259 addr_sp -= GENERATED_GBL_DATA_SIZE;
260 id = (gd_t *) addr_sp;
261 debug("Reserving %zu Bytes for Global Data at: %08lx\n",
262 GENERATED_GBL_DATA_SIZE, addr_sp);
263
264 /* setup stackpointer for exeptions */
265 gd->irq_sp = addr_sp;
266#ifdef CONFIG_USE_IRQ
267 addr_sp -= (CONFIG_STACKSIZE_IRQ + CONFIG_STACKSIZE_FIQ);
268 debug("Reserving %zu Bytes for IRQ stack at: %08lx\n",
269 CONFIG_STACKSIZE_IRQ+CONFIG_STACKSIZE_FIQ, addr_sp);
270#endif
271 /* leave 3 words for abort-stack */
272 addr_sp -= 12;
273
274 /* 8-byte alignment for ABI compliance */
275 addr_sp &= ~0x07;
276 debug("New Stack Pointer is: %08lx\n", addr_sp);
277
278 gd->bd->bi_baudrate = gd->baudrate;
279 /* Ram isn't board specific, so move it to board code ... */
280 dram_init_banksize();
281 display_dram_config(); /* and display it */
282
283 gd->relocaddr = addr;
284 gd->start_addr_sp = addr_sp;
285
286 gd->reloc_off = addr - _TEXT_BASE;
287
288 debug("relocation Offset is: %08lx\n", gd->reloc_off);
289 memcpy(id, (void *)gd, GENERATED_GBL_DATA_SIZE);
290
291 relocate_code(addr_sp, id, addr);
292
293 /* NOTREACHED - relocate_code() does not return */
294}
295
296/*
297 * This is the next part if the initialization sequence: we are now
298 * running from RAM and have a "normal" C environment, i. e. global
299 * data can be written, BSS has been cleared, the stack size in not
300 * that critical any more, etc.
301 */
302void board_init_r(gd_t *id, ulong dest_addr)
303{
Macpaul Lin463d47f2011-10-11 22:33:19 +0000304 bd_t *bd;
305 ulong malloc_start;
306
307 extern void malloc_bin_reloc(void);
308
309 gd = id;
310 bd = gd->bd;
311
312 gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
313
314 monitor_flash_len = &_end - &_start;
315 debug("monitor flash len: %08lX\n", monitor_flash_len);
316
317 board_init(); /* Setup chipselects */
318
319#if defined(CONFIG_NEEDS_MANUAL_RELOC)
320 /*
321 * We have to relocate the command table manually
322 */
323 fixup_cmdtable(&__u_boot_cmd_start,
324 (ulong)(&__u_boot_cmd_end - &__u_boot_cmd_start));
325#endif /* defined(CONFIG_NEEDS_MANUAL_RELOC) */
326
327#ifdef CONFIG_SERIAL_MULTI
328 serial_initialize();
329#endif
330
331 debug("Now running in RAM - U-Boot at: %08lx\n", dest_addr);
332
333 /* The Malloc area is immediately below the monitor copy in DRAM */
334 malloc_start = dest_addr - TOTAL_MALLOC_LEN;
335 mem_malloc_init(malloc_start, TOTAL_MALLOC_LEN);
336 malloc_bin_reloc();
337
338#ifndef CONFIG_SYS_NO_FLASH
339 /* configure available FLASH banks */
340 gd->bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
341 gd->bd->bi_flashsize = flash_init();
342 gd->bd->bi_flashoffset = CONFIG_SYS_FLASH_BASE + gd->bd->bi_flashsize;
343
344 if (gd->bd->bi_flashsize)
345 display_flash_config(gd->bd->bi_flashsize);
346#endif /* CONFIG_SYS_NO_FLASH */
347
348#if defined(CONFIG_CMD_NAND)
349 puts("NAND: ");
350 nand_init(); /* go init the NAND */
351#endif
352
Macpaul Lina2308542011-11-18 17:01:31 +0800353#if defined(CONFIG_CMD_IDE)
354 puts("IDE: ");
355 ide_init();
356#endif
357
Macpaul Lin463d47f2011-10-11 22:33:19 +0000358#ifdef CONFIG_GENERIC_MMC
359 puts("MMC: ");
360 mmc_initialize(gd->bd);
361#endif
362
363 /* initialize environment */
364 env_relocate();
365
366#if defined(CONFIG_CMD_PCI) || defined(CONFIG_PCI)
Macpaul Lin4ef80672011-11-25 17:14:51 +0800367 puts("PCI: ");
Macpaul Lin463d47f2011-10-11 22:33:19 +0000368 nds32_pci_init();
369#endif
370
Macpaul Lin463d47f2011-10-11 22:33:19 +0000371 stdio_init(); /* get the devices list going. */
372
373 jumptable_init();
374
375#if defined(CONFIG_API)
376 /* Initialize API */
377 api_init();
378#endif
379
380 console_init_r(); /* fully init console as a device */
381
382#if defined(CONFIG_ARCH_MISC_INIT)
383 /* miscellaneous arch dependent initialisations */
384 arch_misc_init();
385#endif
386#if defined(CONFIG_MISC_INIT_R)
387 /* miscellaneous platform dependent initialisations */
388 misc_init_r();
389#endif
390
391#if defined(CONFIG_USE_IRQ)
392 /* set up exceptions */
393 interrupt_init();
394 /* enable exceptions */
395 enable_interrupts();
396#endif
397
398 /* Initialize from environment */
Macpaul Lin569bc622011-10-24 16:43:17 +0800399 load_addr = getenv_ulong("loadaddr", 16, load_addr);
Macpaul Lin463d47f2011-10-11 22:33:19 +0000400
Macpaul Lin463d47f2011-10-11 22:33:19 +0000401#ifdef BOARD_LATE_INIT
402 board_late_init();
403#endif
404
405#if defined(CONFIG_CMD_NET)
406 puts("Net: ");
407
408 eth_initialize(gd->bd);
409#if defined(CONFIG_RESET_PHY_R)
410 debug("Reset Ethernet PHY\n");
411 reset_phy();
412#endif
413#endif
414
415 /* main_loop() can return to retry autoboot, if so just run it again. */
416 for (;;)
417 main_loop();
418
419 /* NOTREACHED - no way out of command loop except booting */
420}
421
422void hang(void)
423{
424 puts("### ERROR ### Please RESET the board ###\n");
425 for (;;)
426 ;
427}