xref: /rk3399_rockchip-uboot/common/board_f.c (revision 632efa744013205c5f9a2a5fe21bfd0f968d00c7)
1 /*
2  * Copyright (c) 2011 The Chromium OS Authors.
3  * (C) Copyright 2002-2006
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  *
6  * (C) Copyright 2002
7  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8  * Marius Groeger <mgroeger@sysgo.de>
9  *
10  * See file CREDITS for list of people who contributed to this
11  * project.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License as
15  * published by the Free Software Foundation; either version 2 of
16  * the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26  * MA 02111-1307 USA
27  */
28 
29 #include <common.h>
30 #include <linux/compiler.h>
31 #include <version.h>
32 #include <environment.h>
33 #include <fdtdec.h>
34 #include <initcall.h>
35 #include <logbuff.h>
36 #include <post.h>
37 #include <asm/io.h>
38 #include <asm/sections.h>
39 #include <linux/compiler.h>
40 
41 /*
42  * Pointer to initial global data area
43  *
44  * Here we initialize it if needed.
45  */
46 #ifdef XTRN_DECLARE_GLOBAL_DATA_PTR
47 #undef	XTRN_DECLARE_GLOBAL_DATA_PTR
48 #define XTRN_DECLARE_GLOBAL_DATA_PTR	/* empty = allocate here */
49 DECLARE_GLOBAL_DATA_PTR = (gd_t *) (CONFIG_SYS_INIT_GD_ADDR);
50 #else
51 DECLARE_GLOBAL_DATA_PTR;
52 #endif
53 
54 /*
55  * sjg: IMO this code should be
56  * refactored to a single function, something like:
57  *
58  * void led_set_state(enum led_colour_t colour, int on);
59  */
60 /************************************************************************
61  * Coloured LED functionality
62  ************************************************************************
63  * May be supplied by boards if desired
64  */
65 inline void __coloured_LED_init(void) {}
66 void coloured_LED_init(void)
67 	__attribute__((weak, alias("__coloured_LED_init")));
68 inline void __red_led_on(void) {}
69 void red_led_on(void) __attribute__((weak, alias("__red_led_on")));
70 inline void __red_led_off(void) {}
71 void red_led_off(void) __attribute__((weak, alias("__red_led_off")));
72 inline void __green_led_on(void) {}
73 void green_led_on(void) __attribute__((weak, alias("__green_led_on")));
74 inline void __green_led_off(void) {}
75 void green_led_off(void) __attribute__((weak, alias("__green_led_off")));
76 inline void __yellow_led_on(void) {}
77 void yellow_led_on(void) __attribute__((weak, alias("__yellow_led_on")));
78 inline void __yellow_led_off(void) {}
79 void yellow_led_off(void) __attribute__((weak, alias("__yellow_led_off")));
80 inline void __blue_led_on(void) {}
81 void blue_led_on(void) __attribute__((weak, alias("__blue_led_on")));
82 inline void __blue_led_off(void) {}
83 void blue_led_off(void) __attribute__((weak, alias("__blue_led_off")));
84 
85 /*
86  * Why is gd allocated a register? Prior to reloc it might be better to
87  * just pass it around to each function in this file?
88  *
89  * After reloc one could argue that it is hardly used and doesn't need
90  * to be in a register. Or if it is it should perhaps hold pointers to all
91  * global data for all modules, so that post-reloc we can avoid the massive
92  * literal pool we get on ARM. Or perhaps just encourage each module to use
93  * a structure...
94  */
95 
96 /*
97  * Could the CONFIG_SPL_BUILD infection become a flag in gd?
98  */
99 
100 static int init_baud_rate(void)
101 {
102 	gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
103 	return 0;
104 }
105 
106 static int display_text_info(void)
107 {
108 	ulong bss_start, bss_end;
109 
110 #ifdef CONFIG_SYS_SYM_OFFSETS
111 	bss_start = _bss_start_ofs + _TEXT_BASE;
112 	bss_end = _bss_end_ofs + _TEXT_BASE;
113 #else
114 	bss_start = (ulong)&__bss_start;
115 	bss_end = (ulong)&__bss_end;
116 #endif
117 	debug("U-Boot code: %08X -> %08lX  BSS: -> %08lX\n",
118 	      CONFIG_SYS_TEXT_BASE, bss_start, bss_end);
119 
120 #ifdef CONFIG_MODEM_SUPPORT
121 	debug("Modem Support enabled\n");
122 #endif
123 #ifdef CONFIG_USE_IRQ
124 	debug("IRQ Stack: %08lx\n", IRQ_STACK_START);
125 	debug("FIQ Stack: %08lx\n", FIQ_STACK_START);
126 #endif
127 
128 	return 0;
129 }
130 
131 static int announce_dram_init(void)
132 {
133 	puts("DRAM:  ");
134 	return 0;
135 }
136 
137 static int show_dram_config(void)
138 {
139 	ulong size;
140 
141 #ifdef CONFIG_NR_DRAM_BANKS
142 	int i;
143 
144 	debug("\nRAM Configuration:\n");
145 	for (i = size = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
146 		size += gd->bd->bi_dram[i].size;
147 		debug("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
148 #ifdef DEBUG
149 		print_size(gd->bd->bi_dram[i].size, "\n");
150 #endif
151 	}
152 	debug("\nDRAM:  ");
153 #else
154 	size = gd->ram_size;
155 #endif
156 
157 	print_size(size, "\n");
158 
159 	return 0;
160 }
161 
162 void __dram_init_banksize(void)
163 {
164 #if defined(CONFIG_NR_DRAM_BANKS) && defined(CONFIG_SYS_SDRAM_BASE)
165 	gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
166 	gd->bd->bi_dram[0].size = get_effective_memsize();
167 #endif
168 }
169 
170 void dram_init_banksize(void)
171 	__attribute__((weak, alias("__dram_init_banksize")));
172 
173 static int zero_global_data(void)
174 {
175 	memset((void *)gd, '\0', sizeof(gd_t));
176 
177 	return 0;
178 }
179 
180 static int setup_mon_len(void)
181 {
182 #ifdef CONFIG_SYS_SYM_OFFSETS
183 	gd->mon_len = _bss_end_ofs;
184 #else
185 	gd->mon_len = (ulong)&__bss_end - (ulong)&__text_start;
186 #endif
187 	return 0;
188 }
189 
190 __weak int arch_cpu_init(void)
191 {
192 	return 0;
193 }
194 
195 static int setup_fdt(void)
196 {
197 #ifdef CONFIG_OF_EMBED
198 	/* Get a pointer to the FDT */
199 	gd->fdt_blob = _binary_dt_dtb_start;
200 #elif defined CONFIG_OF_SEPARATE
201 	/* FDT is at end of image */
202 # ifdef CONFIG_SYS_SYM_OFFSETS
203 	gd->fdt_blob = (void *)(_end_ofs + CONFIG_SYS_TEXT_BASE);
204 # else
205 	gd->fdt_blob = (ulong *)&_end;
206 # endif
207 #endif
208 	/* Allow the early environment to override the fdt address */
209 	gd->fdt_blob = (void *)getenv_ulong("fdtcontroladdr", 16,
210 						(uintptr_t)gd->fdt_blob);
211 	return 0;
212 }
213 
214 /* Get the top of usable RAM */
215 __weak ulong board_get_usable_ram_top(ulong total_size)
216 {
217 	return gd->ram_top;
218 }
219 
220 static int setup_dest_addr(void)
221 {
222 	debug("Monitor len: %08lX\n", gd->mon_len);
223 	/*
224 	 * Ram is setup, size stored in gd !!
225 	 */
226 	debug("Ram size: %08lX\n", (ulong)gd->ram_size);
227 #if defined(CONFIG_SYS_MEM_TOP_HIDE)
228 	/*
229 	 * Subtract specified amount of memory to hide so that it won't
230 	 * get "touched" at all by U-Boot. By fixing up gd->ram_size
231 	 * the Linux kernel should now get passed the now "corrected"
232 	 * memory size and won't touch it either. This should work
233 	 * for arch/ppc and arch/powerpc. Only Linux board ports in
234 	 * arch/powerpc with bootwrapper support, that recalculate the
235 	 * memory size from the SDRAM controller setup will have to
236 	 * get fixed.
237 	 */
238 	gd->ram_size -= CONFIG_SYS_MEM_TOP_HIDE;
239 #endif
240 #ifdef CONFIG_SYS_SDRAM_BASE
241 	gd->ram_top = CONFIG_SYS_SDRAM_BASE;
242 #endif
243 	gd->ram_top = board_get_usable_ram_top(gd->mon_len);
244 	gd->dest_addr = gd->ram_top;
245 	debug("Ram top: %08lX\n", (ulong)gd->ram_top);
246 	gd->dest_addr_sp = gd->dest_addr;
247 	return 0;
248 }
249 
250 #if defined(CONFIG_LOGBUFFER) && !defined(CONFIG_ALT_LB_ADDR)
251 static int reserve_logbuffer(void)
252 {
253 	/* reserve kernel log buffer */
254 	gd->dest_addr -= LOGBUFF_RESERVE;
255 	debug("Reserving %dk for kernel logbuffer at %08lx\n", LOGBUFF_LEN,
256 		gd->dest_addr);
257 	return 0;
258 }
259 #endif
260 
261 #ifdef CONFIG_PRAM
262 /* reserve protected RAM */
263 static int reserve_pram(void)
264 {
265 	ulong reg;
266 
267 	reg = getenv_ulong("pram", 10, CONFIG_PRAM);
268 	gd->dest_addr -= (reg << 10);		/* size is in kB */
269 	debug("Reserving %ldk for protected RAM at %08lx\n", reg,
270 	      gd->dest_addr);
271 	return 0;
272 }
273 #endif /* CONFIG_PRAM */
274 
275 /* Round memory pointer down to next 4 kB limit */
276 static int reserve_round_4k(void)
277 {
278 	gd->dest_addr &= ~(4096 - 1);
279 	return 0;
280 }
281 
282 #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
283 		defined(CONFIG_ARM)
284 static int reserve_mmu(void)
285 {
286 	/* reserve TLB table */
287 	gd->arch.tlb_size = 4096 * 4;
288 	gd->dest_addr -= gd->arch.tlb_size;
289 
290 	/* round down to next 64 kB limit */
291 	gd->dest_addr &= ~(0x10000 - 1);
292 
293 	gd->arch.tlb_addr = gd->dest_addr;
294 	debug("TLB table from %08lx to %08lx\n", gd->arch.tlb_addr,
295 	      gd->arch.tlb_addr + gd->arch.tlb_size);
296 	return 0;
297 }
298 #endif
299 
300 #ifdef CONFIG_LCD
301 static int reserve_lcd(void)
302 {
303 #ifdef CONFIG_FB_ADDR
304 	gd->fb_base = CONFIG_FB_ADDR;
305 #else
306 	/* reserve memory for LCD display (always full pages) */
307 	gd->dest_addr = lcd_setmem(gd->dest_addr);
308 	gd->fb_base = gd->dest_addr;
309 #endif /* CONFIG_FB_ADDR */
310 	return 0;
311 }
312 #endif /* CONFIG_LCD */
313 
314 static int reserve_uboot(void)
315 {
316 	/*
317 	 * reserve memory for U-Boot code, data & bss
318 	 * round down to next 4 kB limit
319 	 */
320 	gd->dest_addr -= gd->mon_len;
321 	gd->dest_addr &= ~(4096 - 1);
322 
323 	debug("Reserving %ldk for U-Boot at: %08lx\n", gd->mon_len >> 10,
324 	      gd->dest_addr);
325 	return 0;
326 }
327 
328 #ifndef CONFIG_SPL_BUILD
329 /* reserve memory for malloc() area */
330 static int reserve_malloc(void)
331 {
332 	gd->dest_addr_sp = gd->dest_addr - TOTAL_MALLOC_LEN;
333 	debug("Reserving %dk for malloc() at: %08lx\n",
334 			TOTAL_MALLOC_LEN >> 10, gd->dest_addr_sp);
335 	return 0;
336 }
337 
338 /* (permanently) allocate a Board Info struct */
339 static int reserve_board(void)
340 {
341 	gd->dest_addr_sp -= sizeof(bd_t);
342 	gd->bd = (bd_t *)gd->dest_addr_sp;
343 	memset(gd->bd, '\0', sizeof(bd_t));
344 	debug("Reserving %zu Bytes for Board Info at: %08lx\n",
345 			sizeof(bd_t), gd->dest_addr_sp);
346 	return 0;
347 }
348 #endif
349 
350 static int setup_machine(void)
351 {
352 #ifdef CONFIG_MACH_TYPE
353 	gd->bd->bi_arch_number = CONFIG_MACH_TYPE; /* board id for Linux */
354 #endif
355 	return 0;
356 }
357 
358 static int reserve_global_data(void)
359 {
360 	gd->dest_addr_sp -= sizeof(gd_t);
361 	gd->new_gd = (gd_t *)gd->dest_addr_sp;
362 	debug("Reserving %zu Bytes for Global Data at: %08lx\n",
363 			sizeof(gd_t), gd->dest_addr_sp);
364 	return 0;
365 }
366 
367 static int reserve_fdt(void)
368 {
369 	/*
370 	 * If the device tree is sitting immediate above our image then we
371 	 * must relocate it. If it is embedded in the data section, then it
372 	 * will be relocated with other data.
373 	 */
374 	if (gd->fdt_blob) {
375 		gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
376 
377 		gd->dest_addr_sp -= gd->fdt_size;
378 		gd->new_fdt = (void *)gd->dest_addr_sp;
379 		debug("Reserving %lu Bytes for FDT at: %p\n",
380 		      gd->fdt_size, gd->new_fdt);
381 	}
382 
383 	return 0;
384 }
385 
386 static int reserve_stacks(void)
387 {
388 #ifdef CONFIG_SPL_BUILD
389 # ifdef CONFIG_ARM
390 	gd->dest_addr_sp -= 128;	/* leave 32 words for abort-stack */
391 	gd->irq_sp = gd->dest_addr_sp;
392 # endif
393 #else
394 
395 	/* setup stack pointer for exceptions */
396 	gd->dest_addr_sp -= 16;
397 	gd->dest_addr_sp &= ~0xf;
398 	gd->irq_sp = gd->dest_addr_sp;
399 
400 	/*
401 	 * Handle architecture-specific things here
402 	 * TODO(sjg@chromium.org): Perhaps create arch_reserve_stack()
403 	 * to handle this and put in arch/xxx/lib/stack.c
404 	 */
405 # ifdef CONFIG_ARM
406 #  ifdef CONFIG_USE_IRQ
407 	gd->dest_addr_sp -= (CONFIG_STACKSIZE_IRQ + CONFIG_STACKSIZE_FIQ);
408 	debug("Reserving %zu Bytes for IRQ stack at: %08lx\n",
409 		CONFIG_STACKSIZE_IRQ + CONFIG_STACKSIZE_FIQ, gd->dest_addr_sp);
410 
411 	/* 8-byte alignment for ARM ABI compliance */
412 	gd->dest_addr_sp &= ~0x07;
413 #  endif
414 	/* leave 3 words for abort-stack, plus 1 for alignment */
415 	gd->dest_addr_sp -= 16;
416 # endif /* Architecture specific code */
417 
418 	return 0;
419 #endif
420 }
421 
422 static int display_new_sp(void)
423 {
424 	debug("New Stack Pointer is: %08lx\n", gd->dest_addr_sp);
425 
426 	return 0;
427 }
428 
429 #ifdef CONFIG_POST
430 static int init_post(void)
431 {
432 	post_bootmode_init();
433 	post_run(NULL, POST_ROM | post_bootmode_get(0));
434 
435 	return 0;
436 }
437 #endif
438 
439 static int setup_baud_rate(void)
440 {
441 	/* Ick, can we get rid of this line? */
442 	gd->bd->bi_baudrate = gd->baudrate;
443 
444 	return 0;
445 }
446 
447 static int setup_dram_config(void)
448 {
449 	/* Ram is board specific, so move it to board code ... */
450 	dram_init_banksize();
451 
452 	return 0;
453 }
454 
455 static int reloc_fdt(void)
456 {
457 	if (gd->new_fdt) {
458 		memcpy(gd->new_fdt, gd->fdt_blob, gd->fdt_size);
459 		gd->fdt_blob = gd->new_fdt;
460 	}
461 
462 	return 0;
463 }
464 
465 static int setup_reloc(void)
466 {
467 	gd->relocaddr = gd->dest_addr;
468 	gd->start_addr_sp = gd->dest_addr_sp;
469 	gd->reloc_off = gd->dest_addr - CONFIG_SYS_TEXT_BASE;
470 	memcpy(gd->new_gd, (char *)gd, sizeof(gd_t));
471 
472 	debug("Relocation Offset is: %08lx\n", gd->reloc_off);
473 	debug("Relocating to %08lx, new gd at %p, sp at %08lx\n",
474 	      gd->dest_addr, gd->new_gd, gd->dest_addr_sp);
475 
476 	return 0;
477 }
478 
479 /* ARM calls relocate_code from its crt0.S */
480 #if !defined(CONFIG_ARM)
481 
482 static int jump_to_copy(void)
483 {
484 	relocate_code(gd->dest_addr_sp, gd->new_gd, gd->dest_addr);
485 
486 	return 0;
487 }
488 #endif
489 
490 /* Record the board_init_f() bootstage (after arch_cpu_init()) */
491 static int mark_bootstage(void)
492 {
493 	bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_F, "board_init_f");
494 
495 	return 0;
496 }
497 
498 static init_fnc_t init_sequence_f[] = {
499 	zero_global_data,
500 	setup_fdt,
501 	setup_mon_len,
502 	arch_cpu_init,		/* basic arch cpu dependent setup */
503 	mark_bootstage,
504 #ifdef CONFIG_OF_CONTROL
505 	fdtdec_check_fdt,
506 #endif
507 #if defined(CONFIG_BOARD_EARLY_INIT_F)
508 	board_early_init_f,
509 #endif
510 	timer_init,		/* initialize timer */
511 #ifdef CONFIG_BOARD_POSTCLK_INIT
512 	board_postclk_init,
513 #endif
514 #ifdef CONFIG_FSL_ESDHC
515 	get_clocks,
516 #endif
517 	env_init,		/* initialize environment */
518 	init_baud_rate,		/* initialze baudrate settings */
519 	serial_init,		/* serial communications setup */
520 	console_init_f,		/* stage 1 init of console */
521 	display_options,	/* say that we are here */
522 	display_text_info,	/* show debugging info if required */
523 #if defined(CONFIG_DISPLAY_CPUINFO)
524 	print_cpuinfo,		/* display cpu info (and speed) */
525 #endif
526 #if defined(CONFIG_DISPLAY_BOARDINFO)
527 	checkboard,		/* display board info */
528 #endif
529 	announce_dram_init,
530 	/* TODO: unify all these dram functions? */
531 #ifdef CONFIG_ARM
532 	dram_init,		/* configure available RAM banks */
533 #endif
534 #ifdef CONFIG_POST
535 	init_post,
536 #endif
537 	/*
538 	 * Now that we have DRAM mapped and working, we can
539 	 * relocate the code and continue running from DRAM.
540 	 *
541 	 * Reserve memory at end of RAM for (top down in that order):
542 	 *  - area that won't get touched by U-Boot and Linux (optional)
543 	 *  - kernel log buffer
544 	 *  - protected RAM
545 	 *  - LCD framebuffer
546 	 *  - monitor code
547 	 *  - board info struct
548 	 */
549 	setup_dest_addr,
550 #if defined(CONFIG_LOGBUFFER) && !defined(CONFIG_ALT_LB_ADDR)
551 	reserve_logbuffer,
552 #endif
553 #ifdef CONFIG_PRAM
554 	reserve_pram,
555 #endif
556 	reserve_round_4k,
557 #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
558 		defined(CONFIG_ARM)
559 	reserve_mmu,
560 #endif
561 #ifdef CONFIG_LCD
562 	reserve_lcd,
563 #endif
564 	reserve_uboot,
565 #ifndef CONFIG_SPL_BUILD
566 	reserve_malloc,
567 	reserve_board,
568 #endif
569 	setup_machine,
570 	reserve_global_data,
571 	reserve_fdt,
572 	reserve_stacks,
573 	setup_dram_config,
574 	show_dram_config,
575 	setup_baud_rate,
576 	display_new_sp,
577 	reloc_fdt,
578 	setup_reloc,
579 #ifndef CONFIG_ARM
580 	jump_to_copy,
581 #endif
582 	NULL,
583 };
584 
585 void board_init_f(ulong boot_flags)
586 {
587 	gd_t data;
588 
589 	gd = &data;
590 
591 	gd->flags = boot_flags;
592 
593 	if (initcall_run_list(init_sequence_f))
594 		hang();
595 
596 #ifndef CONFIG_ARM
597 	/* NOTREACHED - jump_to_copy() does not return */
598 	hang();
599 #endif
600 }
601 
602 void hang(void)
603 {
604 	puts("### ERROR ### Please RESET the board ###\n");
605 	for (;;);
606 }
607