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 * SPDX-License-Identifier: GPL-2.0+
11 */
12
13 #include <common.h>
14 #include <api.h>
15 /* TODO: can we just include all these headers whether needed or not? */
16 #if defined(CONFIG_CMD_BEDBUG)
17 #include <bedbug/type.h>
18 #endif
19 #include <command.h>
20 #include <console.h>
21 #include <dm.h>
22 #include <environment.h>
23 #include <fdtdec.h>
24 #include <ide.h>
25 #include <initcall.h>
26 #include <init_helpers.h>
27 #ifdef CONFIG_PS2KBD
28 #include <keyboard.h>
29 #endif
30 #if defined(CONFIG_CMD_KGDB)
31 #include <kgdb.h>
32 #endif
33 #include <malloc.h>
34 #include <mapmem.h>
35 #include <memalign.h>
36 #ifdef CONFIG_BITBANGMII
37 #include <miiphy.h>
38 #endif
39 #include <mmc.h>
40 #include <nand.h>
41 #include <of_live.h>
42 #include <onenand_uboot.h>
43 #include <scsi.h>
44 #include <serial.h>
45 #include <spi.h>
46 #include <stdio_dev.h>
47 #include <timer.h>
48 #include <trace.h>
49 #include <watchdog.h>
50 #ifdef CONFIG_ADDR_MAP
51 #include <asm/mmu.h>
52 #endif
53 #include <asm/sections.h>
54 #include <asm/system.h>
55 #include <dm/root.h>
56 #include <linux/compiler.h>
57 #include <linux/err.h>
58 #include <efi_loader.h>
59 #include <sysmem.h>
60 #include <bidram.h>
61 #include <boot_rkimg.h>
62 #include <mtd_blk.h>
63 #if defined(CONFIG_GPIO_HOG)
64 #include <asm/gpio.h>
65 #endif
66
67 DECLARE_GLOBAL_DATA_PTR;
68
69 ulong monitor_flash_len;
70
board_flash_wp_on(void)71 __weak int board_flash_wp_on(void)
72 {
73 /*
74 * Most flashes can't be detected when write protection is enabled,
75 * so provide a way to let U-Boot gracefully ignore write protected
76 * devices.
77 */
78 return 0;
79 }
80
cpu_secondary_init_r(void)81 __weak void cpu_secondary_init_r(void)
82 {
83 }
84
initr_secondary_cpu(void)85 static int initr_secondary_cpu(void)
86 {
87 /*
88 * after non-volatile devices & environment is setup and cpu code have
89 * another round to deal with any initialization that might require
90 * full access to the environment or loading of some image (firmware)
91 * from a non-volatile device
92 */
93 /* TODO: maybe define this for all archs? */
94 cpu_secondary_init_r();
95
96 return 0;
97 }
98
initr_trace(void)99 static int initr_trace(void)
100 {
101 #ifdef CONFIG_TRACE
102 trace_init(gd->trace_buff, CONFIG_TRACE_BUFFER_SIZE);
103 #endif
104
105 return 0;
106 }
107
initr_reloc(void)108 static int initr_reloc(void)
109 {
110 /* tell others: relocation done */
111 gd->flags |= GD_FLG_RELOC | GD_FLG_FULL_MALLOC_INIT;
112
113 return 0;
114 }
115
116 #ifdef CONFIG_ARM
117 /*
118 * Some of these functions are needed purely because the functions they
119 * call return void. If we change them to return 0, these stubs can go away.
120 */
121
print_cr(void)122 static void print_cr(void)
123 {
124 u32 reg;
125
126 #ifdef CONFIG_ARM64
127 reg = get_sctlr(); /* get control reg. */
128 #else
129 reg = get_cr();
130 #endif
131 puts("CR: ");
132 if (reg & CR_M)
133 puts("M/");
134 if (reg & CR_C)
135 puts("C/");
136 if (reg & CR_I)
137 puts("I");
138 putc('\n');
139 }
140
initr_caches(void)141 static int initr_caches(void)
142 {
143 /* Enable caches */
144 enable_caches();
145 print_cr();
146
147 return 0;
148 }
149 #endif
150
fixup_cpu(void)151 __weak int fixup_cpu(void)
152 {
153 return 0;
154 }
155
initr_reloc_global_data(void)156 static int initr_reloc_global_data(void)
157 {
158 #ifdef __ARM__
159 monitor_flash_len = _end - __image_copy_start;
160 #elif defined(CONFIG_NDS32) || defined(CONFIG_RISCV)
161 monitor_flash_len = (ulong)&_end - (ulong)&_start;
162 #elif !defined(CONFIG_SANDBOX) && !defined(CONFIG_NIOS2)
163 monitor_flash_len = (ulong)&__init_end - gd->relocaddr;
164 #endif
165 #if defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx)
166 /*
167 * The gd->cpu pointer is set to an address in flash before relocation.
168 * We need to update it to point to the same CPU entry in RAM.
169 * TODO: why not just add gd->reloc_ofs?
170 */
171 gd->arch.cpu += gd->relocaddr - CONFIG_SYS_MONITOR_BASE;
172
173 /*
174 * If we didn't know the cpu mask & # cores, we can save them of
175 * now rather than 'computing' them constantly
176 */
177 fixup_cpu();
178 #endif
179 #ifdef CONFIG_SYS_EXTRA_ENV_RELOC
180 /*
181 * Some systems need to relocate the env_addr pointer early because the
182 * location it points to will get invalidated before env_relocate is
183 * called. One example is on systems that might use a L2 or L3 cache
184 * in SRAM mode and initialize that cache from SRAM mode back to being
185 * a cache in cpu_init_r.
186 */
187 gd->env_addr += gd->relocaddr - CONFIG_SYS_MONITOR_BASE;
188 #endif
189 #ifdef CONFIG_OF_EMBED
190 /*
191 * The fdt_blob needs to be moved to new relocation address
192 * incase of FDT blob is embedded with in image
193 */
194 gd->fdt_blob += gd->reloc_off;
195 #endif
196 #ifdef CONFIG_EFI_LOADER
197 efi_runtime_relocate(gd->relocaddr, NULL);
198 #endif
199
200 return 0;
201 }
202
initr_serial(void)203 static int initr_serial(void)
204 {
205 serial_initialize();
206 return 0;
207 }
208
209 #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_MIPS)
initr_trap(void)210 static int initr_trap(void)
211 {
212 /*
213 * Setup trap handlers
214 */
215 #if defined(CONFIG_PPC)
216 trap_init(gd->relocaddr);
217 #else
218 trap_init(CONFIG_SYS_SDRAM_BASE);
219 #endif
220 return 0;
221 }
222 #endif
223
224 #ifdef CONFIG_ADDR_MAP
initr_addr_map(void)225 static int initr_addr_map(void)
226 {
227 init_addr_map();
228
229 return 0;
230 }
231 #endif
232
233 #ifdef CONFIG_POST
initr_post_backlog(void)234 static int initr_post_backlog(void)
235 {
236 post_output_backlog();
237 return 0;
238 }
239 #endif
240
241 #if defined(CONFIG_SYS_INIT_RAM_LOCK) && defined(CONFIG_E500)
initr_unlock_ram_in_cache(void)242 static int initr_unlock_ram_in_cache(void)
243 {
244 unlock_ram_in_cache(); /* it's time to unlock D-cache in e500 */
245 return 0;
246 }
247 #endif
248
249 #ifdef CONFIG_PCI
initr_pci(void)250 static int initr_pci(void)
251 {
252 #ifndef CONFIG_DM_PCI
253 pci_init();
254 #endif
255
256 return 0;
257 }
258 #endif
259
initr_barrier(void)260 static int initr_barrier(void)
261 {
262 #ifdef CONFIG_PPC
263 /* TODO: Can we not use dmb() macros for this? */
264 asm("sync ; isync");
265 #endif
266 return 0;
267 }
268
initr_malloc(void)269 static int initr_malloc(void)
270 {
271 ulong malloc_start;
272
273 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
274 debug("Pre-reloc malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
275 gd->malloc_ptr / 1024);
276 #endif
277 /* The malloc area is immediately below the monitor copy in DRAM */
278 malloc_start = gd->relocaddr - TOTAL_MALLOC_LEN;
279 mem_malloc_init((ulong)map_sysmem(malloc_start, TOTAL_MALLOC_LEN),
280 TOTAL_MALLOC_LEN);
281 return 0;
282 }
283
initr_console_record(void)284 static int initr_console_record(void)
285 {
286 #if defined(CONFIG_CONSOLE_RECORD)
287 int ret;
288
289 ret = console_record_init();
290 if (!ret)
291 console_record_reset_enable();
292 return ret;
293 #else
294 return 0;
295 #endif
296 }
297
298 #ifdef CONFIG_SYS_NONCACHED_MEMORY
initr_noncached(void)299 static int initr_noncached(void)
300 {
301 noncached_init();
302 return 0;
303 }
304 #endif
305
306 #ifdef CONFIG_OF_LIVE
initr_of_live(void)307 static int initr_of_live(void)
308 {
309 int ret;
310
311 bootstage_start(BOOTSTAGE_ID_ACCUM_OF_LIVE, "of_live");
312 ret = of_live_build(gd->fdt_blob, (struct device_node **)&gd->of_root);
313 bootstage_accum(BOOTSTAGE_ID_ACCUM_OF_LIVE);
314 if (ret)
315 return ret;
316
317 return 0;
318 }
319 #endif
320
321 #ifdef CONFIG_DM
initr_dm(void)322 static int initr_dm(void)
323 {
324 int ret;
325
326 /* Save the pre-reloc driver model and start a new one */
327 gd->dm_root_f = gd->dm_root;
328 gd->dm_root = NULL;
329 #ifdef CONFIG_TIMER
330 gd->timer = NULL;
331 #endif
332 bootstage_start(BOOTSTATE_ID_ACCUM_DM_R, "dm_r");
333 ret = dm_init_and_scan(false);
334 bootstage_accum(BOOTSTATE_ID_ACCUM_DM_R);
335 if (ret)
336 return ret;
337 #ifdef CONFIG_TIMER_EARLY
338 ret = dm_timer_init();
339 if (ret)
340 return ret;
341 #endif
342
343 return 0;
344 }
345 #endif
346
initr_bootstage(void)347 static int initr_bootstage(void)
348 {
349 bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_R, "board_init_r");
350
351 return 0;
352 }
353
power_init_board(void)354 __weak int power_init_board(void)
355 {
356 return 0;
357 }
358
initr_announce(void)359 static int initr_announce(void)
360 {
361 ulong addr;
362
363 #ifndef CONFIG_SKIP_RELOCATE_UBOOT
364 addr = gd->relocaddr;
365 #else
366 addr = CONFIG_SYS_TEXT_BASE;
367 #endif
368 debug("Now running in RAM - U-Boot at: %08lx\n", addr);
369
370 return 0;
371 }
372
373 #ifdef CONFIG_NEEDS_MANUAL_RELOC
initr_manual_reloc_cmdtable(void)374 static int initr_manual_reloc_cmdtable(void)
375 {
376 fixup_cmdtable(ll_entry_start(cmd_tbl_t, cmd),
377 ll_entry_count(cmd_tbl_t, cmd));
378 return 0;
379 }
380 #endif
381
382 #if defined(CONFIG_MTD_NOR_FLASH)
initr_flash(void)383 static int initr_flash(void)
384 {
385 ulong flash_size = 0;
386 bd_t *bd = gd->bd;
387
388 puts("Flash: ");
389
390 if (board_flash_wp_on())
391 printf("Uninitialized - Write Protect On\n");
392 else
393 flash_size = flash_init();
394
395 print_size(flash_size, "");
396 #ifdef CONFIG_SYS_FLASH_CHECKSUM
397 /*
398 * Compute and print flash CRC if flashchecksum is set to 'y'
399 *
400 * NOTE: Maybe we should add some WATCHDOG_RESET()? XXX
401 */
402 if (env_get_yesno("flashchecksum") == 1) {
403 printf(" CRC: %08X", crc32(0,
404 (const unsigned char *) CONFIG_SYS_FLASH_BASE,
405 flash_size));
406 }
407 #endif /* CONFIG_SYS_FLASH_CHECKSUM */
408 putc('\n');
409
410 /* update start of FLASH memory */
411 #ifdef CONFIG_SYS_FLASH_BASE
412 bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
413 #endif
414 /* size of FLASH memory (final value) */
415 bd->bi_flashsize = flash_size;
416
417 #if defined(CONFIG_SYS_UPDATE_FLASH_SIZE)
418 /* Make a update of the Memctrl. */
419 update_flash_size(flash_size);
420 #endif
421
422
423 #if defined(CONFIG_OXC) || defined(CONFIG_RMU)
424 /* flash mapped at end of memory map */
425 bd->bi_flashoffset = CONFIG_SYS_TEXT_BASE + flash_size;
426 #elif CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
427 bd->bi_flashoffset = monitor_flash_len; /* reserved area for monitor */
428 #endif
429 return 0;
430 }
431 #endif
432
433 #if defined(CONFIG_PPC) && !defined(CONFIG_DM_SPI)
initr_spi(void)434 static int initr_spi(void)
435 {
436 /* MPC8xx does this here */
437 #ifdef CONFIG_MPC8XX_SPI
438 #if !defined(CONFIG_ENV_IS_IN_EEPROM)
439 spi_init_f();
440 #endif
441 spi_init_r();
442 #endif
443 return 0;
444 }
445 #endif
446
447 #ifdef CONFIG_CMD_NAND
448 /* go init the NAND */
initr_nand(void)449 static int initr_nand(void)
450 {
451 #ifndef CONFIG_USING_KERNEL_DTB
452 puts("NAND: ");
453 nand_init();
454 printf("%lu MiB\n", nand_size() / 1024);
455 #endif
456 return 0;
457 }
458 #endif
459
460 #if defined(CONFIG_CMD_ONENAND)
461 /* go init the NAND */
initr_onenand(void)462 static int initr_onenand(void)
463 {
464 puts("NAND: ");
465 onenand_init();
466 return 0;
467 }
468 #endif
469
470 #ifdef CONFIG_MMC
initr_mmc(void)471 static int initr_mmc(void)
472 {
473 /*
474 * When CONFIG_USING_KERNEL_DTB is enabled, mmc has been initialized earlier.
475 */
476 #ifndef CONFIG_USING_KERNEL_DTB
477 puts("MMC: ");
478 mmc_initialize(gd->bd);
479 #endif
480 return 0;
481 }
482 #endif
483
484 #if !defined(CONFIG_USING_KERNEL_DTB) || !defined(CONFIG_ENV_IS_NOWHERE)
485 /*
486 * Tell if it's OK to load the environment early in boot.
487 *
488 * If CONFIG_OF_CONTROL is defined, we'll check with the FDT to see
489 * if this is OK (defaulting to saying it's OK).
490 *
491 * NOTE: Loading the environment early can be a bad idea if security is
492 * important, since no verification is done on the environment.
493 *
494 * @return 0 if environment should not be loaded, !=0 if it is ok to load
495 */
should_load_env(void)496 static int should_load_env(void)
497 {
498 #ifdef CONFIG_OF_CONTROL
499 return fdtdec_get_config_int(gd->fdt_blob, "load-environment", 1);
500 #elif defined CONFIG_DELAY_ENVIRONMENT
501 return 0;
502 #else
503 return 1;
504 #endif
505 }
506
initr_env(void)507 static int initr_env(void)
508 {
509 /* initialize environment */
510 if (should_load_env())
511 env_relocate();
512 else
513 set_default_env(NULL);
514 #ifdef CONFIG_OF_CONTROL
515 env_set_addr("fdtcontroladdr", gd->fdt_blob);
516 #endif
517
518 /* Initialize from environment */
519 load_addr = env_get_ulong("loadaddr", 16, load_addr);
520
521 return 0;
522 }
523 #endif
524
525 #ifdef CONFIG_USING_KERNEL_DTB
526 /*
527 * If !defined(CONFIG_ENV_IS_NOWHERE):
528 *
529 * Storage env or kernel dtb load depends on bootdev, while bootdev
530 * depends on env varname: devtype, devnum and rkimg_bootdev, etc.
531 * So we have to use nowhere env firstly and cover the storage env
532 * after it is loaded.
533 *
534 * Providing a minimum and necessary nowhere env for board init to
535 * avoid covering the other varnames in storage env.
536 */
initr_env_nowhere(void)537 static int initr_env_nowhere(void)
538 {
539 #ifdef CONFIG_ENV_IS_NOWHERE
540 set_default_env(NULL);
541 #if defined(CONFIG_ENVF)
542 /* init envf and partitiont from envf before any partition query action */
543 env_load();
544 #endif
545 return 0;
546 #else
547 const char env_minimum[] = {
548 ENV_MEM_LAYOUT_SETTINGS
549 #ifdef ENV_MEM_LAYOUT_SETTINGS1
550 ENV_MEM_LAYOUT_SETTINGS1
551 #endif
552 #ifdef RKIMG_DET_BOOTDEV
553 RKIMG_DET_BOOTDEV
554 #endif
555 };
556
557 return set_board_env((char *)env_minimum, ENV_SIZE, 0, true);
558 #endif
559 }
560
561 #if !defined(CONFIG_ENV_IS_NOWHERE)
562 /*
563 * storage has been initialized in board_init(), we could switch env
564 * from nowhere to storage, i.e. CONFIG_ENV_IS_IN_xxx=y.
565 */
initr_env_switch(void)566 static int initr_env_switch(void)
567 {
568 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_nowhere, 1);
569 char *data;
570 int ret;
571
572 data = env_get("bootargs");
573 if (data) {
574 env_set("bootargs_tmp", data);
575 env_set("bootargs", NULL);
576 }
577
578 /* Export nowhere env for late use */
579 ret = env_export(env_nowhere);
580 if (ret) {
581 printf("%s: export nowhere env fail, ret=%d\n", __func__, ret);
582 return -EINVAL;
583 }
584
585 /* Destroy nowhere env and import storage env */
586 initr_env();
587
588 /* Append/override nowhere env to storage env */
589 set_board_env((char *)env_nowhere->data, ENV_SIZE, H_NOCLEAR, false);
590
591 /*
592 * Restore nowhere bootargs to append/override the one in env storage.
593 *
594 * Without this, the entire "bootargs" in storage env is replaces by
595 * the one in env_nowhere->data.
596 */
597 data = env_get("bootargs_tmp");
598 if (data) {
599 env_update("bootargs", data);
600 env_set("bootargs_tmp", NULL);
601 }
602
603 return 0;
604 }
605 #endif /* CONFIG_ENV_IS_NOWHERE */
606 #endif /* CONFIG_USING_KERNEL_DTB */
607
608 #ifdef CONFIG_SYS_BOOTPARAMS_LEN
initr_malloc_bootparams(void)609 static int initr_malloc_bootparams(void)
610 {
611 gd->bd->bi_boot_params = (ulong)malloc(CONFIG_SYS_BOOTPARAMS_LEN);
612 if (!gd->bd->bi_boot_params) {
613 puts("WARNING: Cannot allocate space for boot parameters\n");
614 return -ENOMEM;
615 }
616 return 0;
617 }
618 #endif
619
initr_jumptable(void)620 static int initr_jumptable(void)
621 {
622 jumptable_init();
623 return 0;
624 }
625
626 #if defined(CONFIG_API)
initr_api(void)627 static int initr_api(void)
628 {
629 /* Initialize API */
630 api_init();
631 return 0;
632 }
633 #endif
634
635 /* enable exceptions */
636 #ifdef CONFIG_ARM
initr_enable_interrupts(void)637 static int initr_enable_interrupts(void)
638 {
639 enable_interrupts();
640 return 0;
641 }
642 #endif
643
644 #ifdef CONFIG_CMD_NET
initr_ethaddr(void)645 static int initr_ethaddr(void)
646 {
647 bd_t *bd = gd->bd;
648
649 /* kept around for legacy kernels only ... ignore the next section */
650 eth_env_get_enetaddr("ethaddr", bd->bi_enetaddr);
651 #ifdef CONFIG_HAS_ETH1
652 eth_env_get_enetaddr("eth1addr", bd->bi_enet1addr);
653 #endif
654 #ifdef CONFIG_HAS_ETH2
655 eth_env_get_enetaddr("eth2addr", bd->bi_enet2addr);
656 #endif
657 #ifdef CONFIG_HAS_ETH3
658 eth_env_get_enetaddr("eth3addr", bd->bi_enet3addr);
659 #endif
660 #ifdef CONFIG_HAS_ETH4
661 eth_env_get_enetaddr("eth4addr", bd->bi_enet4addr);
662 #endif
663 #ifdef CONFIG_HAS_ETH5
664 eth_env_get_enetaddr("eth5addr", bd->bi_enet5addr);
665 #endif
666 return 0;
667 }
668 #endif /* CONFIG_CMD_NET */
669
670 #ifdef CONFIG_CMD_KGDB
initr_kgdb(void)671 static int initr_kgdb(void)
672 {
673 puts("KGDB: ");
674 kgdb_init();
675 return 0;
676 }
677 #endif
678
679 #if defined(CONFIG_LED_STATUS)
initr_status_led(void)680 static int initr_status_led(void)
681 {
682 #if defined(CONFIG_LED_STATUS_BOOT)
683 status_led_set(CONFIG_LED_STATUS_BOOT, CONFIG_LED_STATUS_BLINKING);
684 #else
685 status_led_init();
686 #endif
687 return 0;
688 }
689 #endif
690
691 #if defined(CONFIG_SCSI) && !defined(CONFIG_DM_SCSI)
initr_scsi(void)692 static int initr_scsi(void)
693 {
694 puts("SCSI: ");
695 scsi_init();
696
697 return 0;
698 }
699 #endif
700
701 #ifdef CONFIG_BITBANGMII
initr_bbmii(void)702 static int initr_bbmii(void)
703 {
704 bb_miiphy_init();
705 return 0;
706 }
707 #endif
708
709 #ifdef CONFIG_CMD_NET
initr_net(void)710 static int initr_net(void)
711 {
712 puts("Net: ");
713 eth_initialize();
714 #if defined(CONFIG_RESET_PHY_R)
715 debug("Reset Ethernet PHY\n");
716 reset_phy();
717 #endif
718 return 0;
719 }
720 #endif
721
722 #ifdef CONFIG_POST
initr_post(void)723 static int initr_post(void)
724 {
725 post_run(NULL, POST_RAM | post_bootmode_get(0));
726 return 0;
727 }
728 #endif
729
730 #if defined(CONFIG_CMD_PCMCIA) && !defined(CONFIG_IDE)
initr_pcmcia(void)731 static int initr_pcmcia(void)
732 {
733 puts("PCMCIA:");
734 pcmcia_init();
735 return 0;
736 }
737 #endif
738
739 #if defined(CONFIG_IDE)
initr_ide(void)740 static int initr_ide(void)
741 {
742 puts("IDE: ");
743 #if defined(CONFIG_START_IDE)
744 if (board_start_ide())
745 ide_init();
746 #else
747 ide_init();
748 #endif
749 return 0;
750 }
751 #endif
752
753 #if defined(CONFIG_PRAM)
754 /*
755 * Export available size of memory for Linux, taking into account the
756 * protected RAM at top of memory
757 */
initr_mem(void)758 int initr_mem(void)
759 {
760 ulong pram = 0;
761 char memsz[32];
762
763 # ifdef CONFIG_PRAM
764 pram = env_get_ulong("pram", 10, CONFIG_PRAM);
765 # endif
766 sprintf(memsz, "%ldk", (long int) ((gd->ram_size / 1024) - pram));
767 env_set("mem", memsz);
768
769 return 0;
770 }
771 #endif
772
773 #ifdef CONFIG_CMD_BEDBUG
initr_bedbug(void)774 static int initr_bedbug(void)
775 {
776 bedbug_init();
777
778 return 0;
779 }
780 #endif
781
782 #ifdef CONFIG_PS2KBD
initr_kbd(void)783 static int initr_kbd(void)
784 {
785 puts("PS/2: ");
786 kbd_init();
787 return 0;
788 }
789 #endif
790
interrupt_debugger_init(void)791 __weak int interrupt_debugger_init(void)
792 {
793 return 0;
794 }
795
board_initr_caches_fixup(void)796 __weak int board_initr_caches_fixup(void)
797 {
798 return 0;
799 }
800
run_main_loop(void)801 static int run_main_loop(void)
802 {
803 #ifdef CONFIG_SANDBOX
804 sandbox_main_loop_init();
805 #endif
806 /* main_loop() can return to retry autoboot, if so just run it again */
807 for (;;)
808 main_loop();
809 return 0;
810 }
811
console_initr_r(void)812 static int console_initr_r(void)
813 {
814 if (smp_event1(SEVT_3, STID_16))
815 return smp_event1(SEVT_1, STID_16);
816
817 return console_init_r();
818 }
819
820 /*
821 * Over time we hope to remove these functions with code fragments and
822 * stub funtcions, and instead call the relevant function directly.
823 *
824 * We also hope to remove most of the driver-related init and do it if/when
825 * the driver is later used.
826 *
827 * TODO: perhaps reset the watchdog in the initcall function after each call?
828 */
829 static init_fnc_t init_sequence_r[] = {
830 initr_trace,
831 initr_reloc,
832 /* TODO: could x86/PPC have this also perhaps? */
833 #ifdef CONFIG_ARM
834 initr_caches,
835 /* Note: For Freescale LS2 SoCs, new MMU table is created in DDR.
836 * A temporary mapping of IFC high region is since removed,
837 * so environmental variables in NOR flash is not availble
838 * until board_init() is called below to remap IFC to high
839 * region.
840 */
841 #endif
842 initr_reloc_global_data,
843
844 /*
845 * Some platform requires to reserve memory regions for some firmware
846 * to avoid kernel touches it, but U-Boot may have communication with
847 * firmware by share memory. So that we had better reserve firmware
848 * region after the initr_caches() which enables MMU and init
849 * translation table, we need firmware region to be mapped as cacheable
850 * like other regions, otherwise there would be dcache coherence issue
851 * between firmware and U-Boot.
852 */
853 board_initr_caches_fixup,
854
855 #if defined(CONFIG_SYS_INIT_RAM_LOCK) && defined(CONFIG_E500)
856 initr_unlock_ram_in_cache,
857 #endif
858 initr_barrier,
859 initr_malloc,
860 #ifdef CONFIG_BIDRAM
861 bidram_initr,
862 #endif
863 #ifdef CONFIG_SYSMEM
864 sysmem_initr,
865 #endif
866 log_init,
867 initr_bootstage, /* Needs malloc() but has its own timer */
868 initr_console_record,
869 #ifdef CONFIG_SYS_NONCACHED_MEMORY
870 initr_noncached,
871 #endif
872 bootstage_relocate,
873
874 interrupt_init,
875 #ifdef CONFIG_ARM
876 initr_enable_interrupts,
877 #endif
878 interrupt_debugger_init,
879
880 #ifdef CONFIG_OF_LIVE
881 initr_of_live,
882 #endif
883 #ifdef CONFIG_DM
884 initr_dm,
885 #endif
886
887 #ifdef CONFIG_USING_KERNEL_DTB
888 initr_env_nowhere,
889 #endif
890 #if defined(CONFIG_BOARD_EARLY_INIT_R)
891 board_early_init_r,
892 #endif
893
894 #if defined(CONFIG_ARM) || defined(CONFIG_NDS32) || defined(CONFIG_RISCV)
895 board_init, /* Setup chipselects */
896 #endif
897 #if defined(CONFIG_USING_KERNEL_DTB) && !defined(CONFIG_ENV_IS_NOWHERE)
898 initr_env_switch,
899 #endif
900 /*
901 * TODO: printing of the clock inforamtion of the board is now
902 * implemented as part of bdinfo command. Currently only support for
903 * davinci SOC's is added. Remove this check once all the board
904 * implement this.
905 */
906 #ifdef CONFIG_CLOCKS
907 set_cpu_clk_info, /* Setup clock information */
908 #endif
909 #ifdef CONFIG_EFI_LOADER
910 efi_memory_init,
911 #endif
912 stdio_init_tables,
913 initr_serial,
914 initr_announce,
915 INIT_FUNC_WATCHDOG_RESET
916 #ifdef CONFIG_NEEDS_MANUAL_RELOC
917 initr_manual_reloc_cmdtable,
918 #endif
919 #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_MIPS)
920 initr_trap,
921 #endif
922 #ifdef CONFIG_ADDR_MAP
923 initr_addr_map,
924 #endif
925 INIT_FUNC_WATCHDOG_RESET
926 #ifdef CONFIG_POST
927 initr_post_backlog,
928 #endif
929 INIT_FUNC_WATCHDOG_RESET
930
931 #ifndef CONFIG_USING_KERNEL_DTB
932 /* init before storage(for: devtype, devnum, ...) */
933 initr_env,
934 #endif
935 #if defined(CONFIG_PCI) && defined(CONFIG_SYS_EARLY_PCI_INIT)
936 /*
937 * Do early PCI configuration _before_ the flash gets initialised,
938 * because PCU ressources are crucial for flash access on some boards.
939 */
940 initr_pci,
941 #endif
942 #ifdef CONFIG_ARCH_EARLY_INIT_R
943 arch_early_init_r,
944 #endif
945 power_init_board,
946 #ifdef CONFIG_MTD_NOR_FLASH
947 initr_flash,
948 #endif
949 INIT_FUNC_WATCHDOG_RESET
950 #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_X86)
951 /* initialize higher level parts of CPU like time base and timers */
952 cpu_init_r,
953 #endif
954 #ifdef CONFIG_PPC
955 initr_spi,
956 #endif
957 #ifdef CONFIG_CMD_NAND
958 initr_nand,
959 #endif
960 #ifdef CONFIG_CMD_ONENAND
961 initr_onenand,
962 #endif
963 #ifdef CONFIG_MMC
964 initr_mmc,
965 #endif
966
967 #ifdef CONFIG_SYS_BOOTPARAMS_LEN
968 initr_malloc_bootparams,
969 #endif
970 INIT_FUNC_WATCHDOG_RESET
971 initr_secondary_cpu,
972 #if defined(CONFIG_ID_EEPROM) || defined(CONFIG_SYS_I2C_MAC_OFFSET)
973 mac_read_from_eeprom,
974 #endif
975 INIT_FUNC_WATCHDOG_RESET
976 #if defined(CONFIG_PCI) && !defined(CONFIG_SYS_EARLY_PCI_INIT)
977 /*
978 * Do pci configuration
979 */
980 initr_pci,
981 #endif
982 stdio_add_devices,
983 initr_jumptable,
984 #ifdef CONFIG_API
985 initr_api,
986 #endif
987 console_initr_r, /* fully init console as a device */
988
989 #ifdef CONFIG_DISPLAY_BOARDINFO_LATE
990 console_announce_r,
991 show_board_info,
992 #endif
993 #ifdef CONFIG_ARCH_MISC_INIT
994 arch_misc_init, /* miscellaneous arch-dependent init */
995 #endif
996 #ifdef CONFIG_MISC_INIT_R
997 misc_init_r, /* miscellaneous platform-dependent init */
998 #endif
999 INIT_FUNC_WATCHDOG_RESET
1000 #ifdef CONFIG_CMD_KGDB
1001 initr_kgdb,
1002 #endif
1003
1004 #if defined(CONFIG_MICROBLAZE) || defined(CONFIG_M68K)
1005 timer_init, /* initialize timer */
1006 #endif
1007 #if defined(CONFIG_LED_STATUS)
1008 initr_status_led,
1009 #endif
1010 /* PPC has a udelay(20) here dating from 2002. Why? */
1011 #ifdef CONFIG_CMD_NET
1012 initr_ethaddr,
1013 #endif
1014 #if defined(CONFIG_GPIO_HOG)
1015 gpio_hog_probe_all,
1016 #endif
1017 #ifdef CONFIG_BOARD_LATE_INIT
1018 board_late_init,
1019 #endif
1020 #if defined(CONFIG_SCSI) && !defined(CONFIG_DM_SCSI)
1021 INIT_FUNC_WATCHDOG_RESET
1022 initr_scsi,
1023 #endif
1024 #ifdef CONFIG_BITBANGMII
1025 initr_bbmii,
1026 #endif
1027 #ifdef CONFIG_CMD_NET
1028 INIT_FUNC_WATCHDOG_RESET
1029 initr_net,
1030 #endif
1031 #ifdef CONFIG_POST
1032 initr_post,
1033 #endif
1034 #if defined(CONFIG_CMD_PCMCIA) && !defined(CONFIG_IDE)
1035 initr_pcmcia,
1036 #endif
1037 #if defined(CONFIG_IDE)
1038 initr_ide,
1039 #endif
1040 #ifdef CONFIG_LAST_STAGE_INIT
1041 INIT_FUNC_WATCHDOG_RESET
1042 /*
1043 * Some parts can be only initialized if all others (like
1044 * Interrupts) are up and running (i.e. the PC-style ISA
1045 * keyboard).
1046 */
1047 last_stage_init,
1048 #endif
1049 #ifdef CONFIG_CMD_BEDBUG
1050 INIT_FUNC_WATCHDOG_RESET
1051 initr_bedbug,
1052 #endif
1053 #if defined(CONFIG_PRAM)
1054 initr_mem,
1055 #endif
1056 #ifdef CONFIG_PS2KBD
1057 initr_kbd,
1058 #endif
1059 run_main_loop,
1060 };
1061
board_init_r(gd_t * new_gd,ulong dest_addr)1062 void board_init_r(gd_t *new_gd, ulong dest_addr)
1063 {
1064 /*
1065 * Set up the new global data pointer. So far only x86 does this
1066 * here.
1067 * TODO(sjg@chromium.org): Consider doing this for all archs, or
1068 * dropping the new_gd parameter.
1069 */
1070 #if CONFIG_IS_ENABLED(X86_64)
1071 arch_setup_gd(new_gd);
1072 #endif
1073
1074 #ifdef CONFIG_NEEDS_MANUAL_RELOC
1075 int i;
1076 #endif
1077
1078 #if !defined(CONFIG_X86) && !defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1079 gd = new_gd;
1080 #endif
1081 gd->flags &= ~GD_FLG_LOG_READY;
1082
1083 #ifdef CONFIG_NEEDS_MANUAL_RELOC
1084 for (i = 0; i < ARRAY_SIZE(init_sequence_r); i++)
1085 init_sequence_r[i] += gd->reloc_off;
1086 #endif
1087
1088 if (initcall_run_list(init_sequence_r))
1089 hang();
1090
1091 /* NOTREACHED - run_main_loop() does not return */
1092 hang();
1093 }
1094