arm: board: use __weak
[oweals/u-boot.git] / arch / arm / lib / board.c
1 /*
2  * (C) Copyright 2002-2006
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * (C) Copyright 2002
6  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7  * Marius Groeger <mgroeger@sysgo.de>
8  *
9  * SPDX-License-Identifier:     GPL-2.0+
10  */
11
12 /*
13  * To match the U-Boot user interface on ARM platforms to the U-Boot
14  * standard (as on PPC platforms), some messages with debug character
15  * are removed from the default U-Boot build.
16  *
17  * Define DEBUG here if you want additional info as shown below
18  * printed upon startup:
19  *
20  * U-Boot code: 00F00000 -> 00F3C774  BSS: -> 00FC3274
21  * IRQ Stack: 00ebff7c
22  * FIQ Stack: 00ebef7c
23  */
24
25 #include <common.h>
26 #include <command.h>
27 #include <environment.h>
28 #include <malloc.h>
29 #include <stdio_dev.h>
30 #include <version.h>
31 #include <net.h>
32 #include <serial.h>
33 #include <nand.h>
34 #include <onenand_uboot.h>
35 #include <mmc.h>
36 #include <scsi.h>
37 #include <libfdt.h>
38 #include <fdtdec.h>
39 #include <post.h>
40 #include <logbuff.h>
41 #include <asm/sections.h>
42
43 #ifdef CONFIG_BITBANGMII
44 #include <miiphy.h>
45 #endif
46
47 DECLARE_GLOBAL_DATA_PTR;
48
49 ulong monitor_flash_len;
50
51 #ifdef CONFIG_HAS_DATAFLASH
52 extern int  AT91F_DataflashInit(void);
53 extern void dataflash_print_info(void);
54 #endif
55
56 #if defined(CONFIG_HARD_I2C) || \
57         defined(CONFIG_SYS_I2C)
58 #include <i2c.h>
59 #endif
60
61 /************************************************************************
62  * Coloured LED functionality
63  ************************************************************************
64  * May be supplied by boards if desired
65  */
66 __weak void coloured_LED_init(void) {}
67 __weak void red_led_on(void) {}
68 __weak void red_led_off(void) {}
69 __weak void green_led_on(void) {}
70 __weak void green_led_off(void) {}
71 __weak void yellow_led_on(void) {}
72 __weak void yellow_led_off(void) {}
73 __weak void blue_led_on(void) {}
74 __weak void blue_led_off(void) {}
75
76 /*
77  ************************************************************************
78  * Init Utilities                                                       *
79  ************************************************************************
80  * Some of this code should be moved into the core functions,
81  * or dropped completely,
82  * but let's get it working (again) first...
83  */
84
85 #if defined(CONFIG_ARM_DCC) && !defined(CONFIG_BAUDRATE)
86 #define CONFIG_BAUDRATE 115200
87 #endif
88
89 static int init_baudrate(void)
90 {
91         gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
92         return 0;
93 }
94
95 static int display_banner(void)
96 {
97         printf("\n\n%s\n\n", version_string);
98         debug("U-Boot code: %08lX -> %08lX  BSS: -> %08lX\n",
99                (ulong)&_start,
100                (ulong)&__bss_start, (ulong)&__bss_end);
101 #ifdef CONFIG_MODEM_SUPPORT
102         debug("Modem Support enabled\n");
103 #endif
104 #ifdef CONFIG_USE_IRQ
105         debug("IRQ Stack: %08lx\n", IRQ_STACK_START);
106         debug("FIQ Stack: %08lx\n", FIQ_STACK_START);
107 #endif
108
109         return (0);
110 }
111
112 /*
113  * WARNING: this code looks "cleaner" than the PowerPC version, but
114  * has the disadvantage that you either get nothing, or everything.
115  * On PowerPC, you might see "DRAM: " before the system hangs - which
116  * gives a simple yet clear indication which part of the
117  * initialization if failing.
118  */
119 static int display_dram_config(void)
120 {
121         int i;
122
123 #ifdef DEBUG
124         puts("RAM Configuration:\n");
125
126         for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
127                 printf("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
128                 print_size(gd->bd->bi_dram[i].size, "\n");
129         }
130 #else
131         ulong size = 0;
132
133         for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++)
134                 size += gd->bd->bi_dram[i].size;
135
136         puts("DRAM:  ");
137         print_size(size, "\n");
138 #endif
139
140         return (0);
141 }
142
143 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
144 static int init_func_i2c(void)
145 {
146         puts("I2C:   ");
147 #ifdef CONFIG_SYS_I2C
148         i2c_init_all();
149 #else
150         i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
151 #endif
152         puts("ready\n");
153         return (0);
154 }
155 #endif
156
157 #if defined(CONFIG_CMD_PCI) || defined (CONFIG_PCI)
158 #include <pci.h>
159 static int arm_pci_init(void)
160 {
161         pci_init();
162         return 0;
163 }
164 #endif /* CONFIG_CMD_PCI || CONFIG_PCI */
165
166 /*
167  * Breathe some life into the board...
168  *
169  * Initialize a serial port as console, and carry out some hardware
170  * tests.
171  *
172  * The first part of initialization is running from Flash memory;
173  * its main purpose is to initialize the RAM so that we
174  * can relocate the monitor code to RAM.
175  */
176
177 /*
178  * All attempts to come up with a "common" initialization sequence
179  * that works for all boards and architectures failed: some of the
180  * requirements are just _too_ different. To get rid of the resulting
181  * mess of board dependent #ifdef'ed code we now make the whole
182  * initialization sequence configurable to the user.
183  *
184  * The requirements for any new initalization function is simple: it
185  * receives a pointer to the "global data" structure as it's only
186  * argument, and returns an integer return code, where 0 means
187  * "continue" and != 0 means "fatal error, hang the system".
188  */
189 typedef int (init_fnc_t) (void);
190
191 __weak void dram_init_banksize(void)
192 {
193         gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
194         gd->bd->bi_dram[0].size =  gd->ram_size;
195 }
196
197 __weak int arch_cpu_init(void)
198 {
199         return 0;
200 }
201
202 __weak int power_init_board(void)
203 {
204         return 0;
205 }
206
207         /* Record the board_init_f() bootstage (after arch_cpu_init()) */
208 static int mark_bootstage(void)
209 {
210         bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_F, "board_init_f");
211
212         return 0;
213 }
214
215 init_fnc_t *init_sequence[] = {
216         arch_cpu_init,          /* basic arch cpu dependent setup */
217         mark_bootstage,
218 #ifdef CONFIG_OF_CONTROL
219         fdtdec_check_fdt,
220 #endif
221 #if defined(CONFIG_BOARD_EARLY_INIT_F)
222         board_early_init_f,
223 #endif
224         timer_init,             /* initialize timer */
225 #ifdef CONFIG_BOARD_POSTCLK_INIT
226         board_postclk_init,
227 #endif
228 #ifdef CONFIG_FSL_ESDHC
229         get_clocks,
230 #endif
231         env_init,               /* initialize environment */
232         init_baudrate,          /* initialze baudrate settings */
233         serial_init,            /* serial communications setup */
234         console_init_f,         /* stage 1 init of console */
235         display_banner,         /* say that we are here */
236         print_cpuinfo,          /* display cpu info (and speed) */
237 #if defined(CONFIG_DISPLAY_BOARDINFO)
238         checkboard,             /* display board info */
239 #endif
240 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
241         init_func_i2c,
242 #endif
243         dram_init,              /* configure available RAM banks */
244         NULL,
245 };
246
247 void board_init_f(ulong bootflag)
248 {
249         bd_t *bd;
250         init_fnc_t **init_fnc_ptr;
251         gd_t *id;
252         ulong addr, addr_sp;
253 #ifdef CONFIG_PRAM
254         ulong reg;
255 #endif
256         void *new_fdt = NULL;
257         size_t fdt_size = 0;
258
259         memset((void *)gd, 0, sizeof(gd_t));
260
261         gd->mon_len = (ulong)&__bss_end - (ulong)_start;
262 #ifdef CONFIG_OF_EMBED
263         /* Get a pointer to the FDT */
264         gd->fdt_blob = __dtb_dt_begin;
265 #elif defined CONFIG_OF_SEPARATE
266         /* FDT is at end of image */
267         gd->fdt_blob = &_end;
268 #endif
269         /* Allow the early environment to override the fdt address */
270         gd->fdt_blob = (void *)getenv_ulong("fdtcontroladdr", 16,
271                                                 (uintptr_t)gd->fdt_blob);
272
273         for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
274                 if ((*init_fnc_ptr)() != 0) {
275                         hang ();
276                 }
277         }
278
279 #ifdef CONFIG_OF_CONTROL
280         /* For now, put this check after the console is ready */
281         if (fdtdec_prepare_fdt()) {
282                 panic("** CONFIG_OF_CONTROL defined but no FDT - please see "
283                         "doc/README.fdt-control");
284         }
285 #endif
286
287         debug("monitor len: %08lX\n", gd->mon_len);
288         /*
289          * Ram is setup, size stored in gd !!
290          */
291         debug("ramsize: %08lX\n", gd->ram_size);
292 #if defined(CONFIG_SYS_MEM_TOP_HIDE)
293         /*
294          * Subtract specified amount of memory to hide so that it won't
295          * get "touched" at all by U-Boot. By fixing up gd->ram_size
296          * the Linux kernel should now get passed the now "corrected"
297          * memory size and won't touch it either. This should work
298          * for arch/ppc and arch/powerpc. Only Linux board ports in
299          * arch/powerpc with bootwrapper support, that recalculate the
300          * memory size from the SDRAM controller setup will have to
301          * get fixed.
302          */
303         gd->ram_size -= CONFIG_SYS_MEM_TOP_HIDE;
304 #endif
305
306         addr = CONFIG_SYS_SDRAM_BASE + get_effective_memsize();
307
308 #ifdef CONFIG_LOGBUFFER
309 #ifndef CONFIG_ALT_LB_ADDR
310         /* reserve kernel log buffer */
311         addr -= (LOGBUFF_RESERVE);
312         debug("Reserving %dk for kernel logbuffer at %08lx\n", LOGBUFF_LEN,
313                 addr);
314 #endif
315 #endif
316
317 #ifdef CONFIG_PRAM
318         /*
319          * reserve protected RAM
320          */
321         reg = getenv_ulong("pram", 10, CONFIG_PRAM);
322         addr -= (reg << 10);            /* size is in kB */
323         debug("Reserving %ldk for protected RAM at %08lx\n", reg, addr);
324 #endif /* CONFIG_PRAM */
325
326 #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF))
327         /* reserve TLB table */
328         gd->arch.tlb_size = PGTABLE_SIZE;
329         addr -= gd->arch.tlb_size;
330
331         /* round down to next 64 kB limit */
332         addr &= ~(0x10000 - 1);
333
334         gd->arch.tlb_addr = addr;
335         debug("TLB table from %08lx to %08lx\n", addr, addr + gd->arch.tlb_size);
336 #endif
337
338         /* round down to next 4 kB limit */
339         addr &= ~(4096 - 1);
340         debug("Top of RAM usable for U-Boot at: %08lx\n", addr);
341
342 #ifdef CONFIG_LCD
343 #ifdef CONFIG_FB_ADDR
344         gd->fb_base = CONFIG_FB_ADDR;
345 #else
346         /* reserve memory for LCD display (always full pages) */
347         addr = lcd_setmem(addr);
348         gd->fb_base = addr;
349 #endif /* CONFIG_FB_ADDR */
350 #endif /* CONFIG_LCD */
351
352         /*
353          * reserve memory for U-Boot code, data & bss
354          * round down to next 4 kB limit
355          */
356         addr -= gd->mon_len;
357         addr &= ~(4096 - 1);
358
359         debug("Reserving %ldk for U-Boot at: %08lx\n", gd->mon_len >> 10, addr);
360
361 #ifndef CONFIG_SPL_BUILD
362         /*
363          * reserve memory for malloc() arena
364          */
365         addr_sp = addr - TOTAL_MALLOC_LEN;
366         debug("Reserving %dk for malloc() at: %08lx\n",
367                         TOTAL_MALLOC_LEN >> 10, addr_sp);
368         /*
369          * (permanently) allocate a Board Info struct
370          * and a permanent copy of the "global" data
371          */
372         addr_sp -= sizeof (bd_t);
373         bd = (bd_t *) addr_sp;
374         gd->bd = bd;
375         debug("Reserving %zu Bytes for Board Info at: %08lx\n",
376                         sizeof (bd_t), addr_sp);
377
378 #ifdef CONFIG_MACH_TYPE
379         gd->bd->bi_arch_number = CONFIG_MACH_TYPE; /* board id for Linux */
380 #endif
381
382         addr_sp -= sizeof (gd_t);
383         id = (gd_t *) addr_sp;
384         debug("Reserving %zu Bytes for Global Data at: %08lx\n",
385                         sizeof (gd_t), addr_sp);
386
387 #if defined(CONFIG_OF_SEPARATE) && defined(CONFIG_OF_CONTROL)
388         /*
389          * If the device tree is sitting immediate above our image then we
390          * must relocate it. If it is embedded in the data section, then it
391          * will be relocated with other data.
392          */
393         if (gd->fdt_blob) {
394                 fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
395
396                 addr_sp -= fdt_size;
397                 new_fdt = (void *)addr_sp;
398                 debug("Reserving %zu Bytes for FDT at: %08lx\n",
399                       fdt_size, addr_sp);
400         }
401 #endif
402
403 #ifndef CONFIG_ARM64
404         /* setup stackpointer for exeptions */
405         gd->irq_sp = addr_sp;
406 #ifdef CONFIG_USE_IRQ
407         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, addr_sp);
410 #endif
411         /* leave 3 words for abort-stack    */
412         addr_sp -= 12;
413
414         /* 8-byte alignment for ABI compliance */
415         addr_sp &= ~0x07;
416 #else   /* CONFIG_ARM64 */
417         /* 16-byte alignment for ABI compliance */
418         addr_sp &= ~0x0f;
419 #endif  /* CONFIG_ARM64 */
420 #else
421         addr_sp += 128; /* leave 32 words for abort-stack   */
422         gd->irq_sp = addr_sp;
423 #endif
424
425         debug("New Stack Pointer is: %08lx\n", addr_sp);
426
427 #ifdef CONFIG_POST
428         post_bootmode_init();
429         post_run(NULL, POST_ROM | post_bootmode_get(0));
430 #endif
431
432         /* Ram ist board specific, so move it to board code ... */
433         dram_init_banksize();
434         display_dram_config();  /* and display it */
435
436         gd->relocaddr = addr;
437         gd->start_addr_sp = addr_sp;
438         gd->reloc_off = addr - (ulong)&_start;
439         debug("relocation Offset is: %08lx\n", gd->reloc_off);
440         if (new_fdt) {
441                 memcpy(new_fdt, gd->fdt_blob, fdt_size);
442                 gd->fdt_blob = new_fdt;
443         }
444         memcpy(id, (void *)gd, sizeof(gd_t));
445 }
446
447 #if !defined(CONFIG_SYS_NO_FLASH)
448 static char *failed = "*** failed ***\n";
449 #endif
450
451 /*
452  * Tell if it's OK to load the environment early in boot.
453  *
454  * If CONFIG_OF_CONFIG is defined, we'll check with the FDT to see
455  * if this is OK (defaulting to saying it's not OK).
456  *
457  * NOTE: Loading the environment early can be a bad idea if security is
458  *       important, since no verification is done on the environment.
459  *
460  * @return 0 if environment should not be loaded, !=0 if it is ok to load
461  */
462 static int should_load_env(void)
463 {
464 #ifdef CONFIG_OF_CONTROL
465         return fdtdec_get_config_int(gd->fdt_blob, "load-environment", 1);
466 #elif defined CONFIG_DELAY_ENVIRONMENT
467         return 0;
468 #else
469         return 1;
470 #endif
471 }
472
473 #if defined(CONFIG_DISPLAY_BOARDINFO_LATE) && defined(CONFIG_OF_CONTROL)
474 static void display_fdt_model(const void *blob)
475 {
476         const char *model;
477
478         model = (char *)fdt_getprop(blob, 0, "model", NULL);
479         printf("Model: %s\n", model ? model : "<unknown>");
480 }
481 #endif
482
483 /************************************************************************
484  *
485  * This is the next part if the initialization sequence: we are now
486  * running from RAM and have a "normal" C environment, i. e. global
487  * data can be written, BSS has been cleared, the stack size in not
488  * that critical any more, etc.
489  *
490  ************************************************************************
491  */
492
493 void board_init_r(gd_t *id, ulong dest_addr)
494 {
495         ulong malloc_start;
496 #if !defined(CONFIG_SYS_NO_FLASH)
497         ulong flash_size;
498 #endif
499
500         gd->flags |= GD_FLG_RELOC;      /* tell others: relocation done */
501         bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_R, "board_init_r");
502
503         monitor_flash_len = (ulong)&__rel_dyn_end - (ulong)_start;
504
505         /* Enable caches */
506         enable_caches();
507
508         debug("monitor flash len: %08lX\n", monitor_flash_len);
509         board_init();   /* Setup chipselects */
510         /*
511          * TODO: printing of the clock inforamtion of the board is now
512          * implemented as part of bdinfo command. Currently only support for
513          * davinci SOC's is added. Remove this check once all the board
514          * implement this.
515          */
516 #ifdef CONFIG_CLOCKS
517         set_cpu_clk_info(); /* Setup clock information */
518 #endif
519         serial_initialize();
520
521         debug("Now running in RAM - U-Boot at: %08lx\n", dest_addr);
522
523 #ifdef CONFIG_LOGBUFFER
524         logbuff_init_ptrs();
525 #endif
526 #ifdef CONFIG_POST
527         post_output_backlog();
528 #endif
529
530         /* The Malloc area is immediately below the monitor copy in DRAM */
531         malloc_start = dest_addr - TOTAL_MALLOC_LEN;
532         mem_malloc_init (malloc_start, TOTAL_MALLOC_LEN);
533
534 #ifdef CONFIG_ARCH_EARLY_INIT_R
535         arch_early_init_r();
536 #endif
537         power_init_board();
538
539 #if !defined(CONFIG_SYS_NO_FLASH)
540         puts("Flash: ");
541
542         flash_size = flash_init();
543         if (flash_size > 0) {
544 # ifdef CONFIG_SYS_FLASH_CHECKSUM
545                 print_size(flash_size, "");
546                 /*
547                  * Compute and print flash CRC if flashchecksum is set to 'y'
548                  *
549                  * NOTE: Maybe we should add some WATCHDOG_RESET()? XXX
550                  */
551                 if (getenv_yesno("flashchecksum") == 1) {
552                         printf("  CRC: %08X", crc32(0,
553                                 (const unsigned char *) CONFIG_SYS_FLASH_BASE,
554                                 flash_size));
555                 }
556                 putc('\n');
557 # else  /* !CONFIG_SYS_FLASH_CHECKSUM */
558                 print_size(flash_size, "\n");
559 # endif /* CONFIG_SYS_FLASH_CHECKSUM */
560         } else {
561                 puts(failed);
562                 hang();
563         }
564 #endif
565
566 #if defined(CONFIG_CMD_NAND)
567         puts("NAND:  ");
568         nand_init();            /* go init the NAND */
569 #endif
570
571 #if defined(CONFIG_CMD_ONENAND)
572         onenand_init();
573 #endif
574
575 #ifdef CONFIG_GENERIC_MMC
576         puts("MMC:   ");
577         mmc_initialize(gd->bd);
578 #endif
579
580 #ifdef CONFIG_CMD_SCSI
581         puts("SCSI:  ");
582         scsi_init();
583 #endif
584
585 #ifdef CONFIG_HAS_DATAFLASH
586         AT91F_DataflashInit();
587         dataflash_print_info();
588 #endif
589
590         /* initialize environment */
591         if (should_load_env())
592                 env_relocate();
593         else
594                 set_default_env(NULL);
595
596 #if defined(CONFIG_CMD_PCI) || defined(CONFIG_PCI)
597         arm_pci_init();
598 #endif
599
600         stdio_init();   /* get the devices list going. */
601
602         jumptable_init();
603
604 #if defined(CONFIG_API)
605         /* Initialize API */
606         api_init();
607 #endif
608
609         console_init_r();       /* fully init console as a device */
610
611 #ifdef CONFIG_DISPLAY_BOARDINFO_LATE
612 # ifdef CONFIG_OF_CONTROL
613         /* Put this here so it appears on the LCD, now it is ready */
614         display_fdt_model(gd->fdt_blob);
615 # else
616         checkboard();
617 # endif
618 #endif
619
620 #if defined(CONFIG_ARCH_MISC_INIT)
621         /* miscellaneous arch dependent initialisations */
622         arch_misc_init();
623 #endif
624 #if defined(CONFIG_MISC_INIT_R)
625         /* miscellaneous platform dependent initialisations */
626         misc_init_r();
627 #endif
628
629          /* set up exceptions */
630         interrupt_init();
631         /* enable exceptions */
632         enable_interrupts();
633
634         /* Initialize from environment */
635         load_addr = getenv_ulong("loadaddr", 16, load_addr);
636
637 #ifdef CONFIG_BOARD_LATE_INIT
638         board_late_init();
639 #endif
640
641 #ifdef CONFIG_BITBANGMII
642         bb_miiphy_init();
643 #endif
644 #if defined(CONFIG_CMD_NET)
645         puts("Net:   ");
646         eth_initialize(gd->bd);
647 #if defined(CONFIG_RESET_PHY_R)
648         debug("Reset Ethernet PHY\n");
649         reset_phy();
650 #endif
651 #endif
652
653 #ifdef CONFIG_POST
654         post_run(NULL, POST_RAM | post_bootmode_get(0));
655 #endif
656
657 #if defined(CONFIG_PRAM) || defined(CONFIG_LOGBUFFER)
658         /*
659          * Export available size of memory for Linux,
660          * taking into account the protected RAM at top of memory
661          */
662         {
663                 ulong pram = 0;
664                 uchar memsz[32];
665
666 #ifdef CONFIG_PRAM
667                 pram = getenv_ulong("pram", 10, CONFIG_PRAM);
668 #endif
669 #ifdef CONFIG_LOGBUFFER
670 #ifndef CONFIG_ALT_LB_ADDR
671                 /* Also take the logbuffer into account (pram is in kB) */
672                 pram += (LOGBUFF_LEN + LOGBUFF_OVERHEAD) / 1024;
673 #endif
674 #endif
675                 sprintf((char *)memsz, "%ldk", (gd->ram_size / 1024) - pram);
676                 setenv("mem", (char *)memsz);
677         }
678 #endif
679
680         /* main_loop() can return to retry autoboot, if so just run it again. */
681         for (;;) {
682                 main_loop();
683         }
684
685         /* NOTREACHED - no way out of command loop except booting */
686 }