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