blob: c24aba1dc9f3db7a945872f96b5d4e6e88d90034 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Rick Chen8bbb2902017-12-26 13:55:49 +08002/*
3 * Copyright (C) 2011 Andes Technology Corporation
4 * Shawn Lin, Andes Technology Corporation <nobuhiro@andestech.com>
5 * Macpaul Lin, Andes Technology Corporation <macpaul@andestech.com>
6 * Rick Chen, Andes Technology Corporation <rick@andestech.com>
Rick Chen8bbb2902017-12-26 13:55:49 +08007 */
8
9#include <common.h>
Simon Glass52f24232020-05-10 11:40:00 -060010#include <bootstage.h>
Rick Chen8bbb2902017-12-26 13:55:49 +080011#include <command.h>
Lukas Auerc3b1a992018-11-22 11:26:32 +010012#include <dm.h>
Simon Glass807765b2019-12-28 10:44:54 -070013#include <fdt_support.h>
Simon Glassdb41d652019-12-28 10:45:07 -070014#include <hang.h>
Lukas Auerc3b1a992018-11-22 11:26:32 +010015#include <dm/root.h>
Rick Chen8bbb2902017-12-26 13:55:49 +080016#include <image.h>
Rick Chen8bbb2902017-12-26 13:55:49 +080017#include <asm/byteorder.h>
Bin Menged49ba42018-09-26 06:55:16 -070018#include <asm/csr.h>
Lukas Auerf28ad252019-03-17 19:28:38 +010019#include <asm/smp.h>
Bin Mengb1893a92018-10-15 02:20:59 -070020#include <dm/device.h>
21#include <dm/root.h>
22#include <u-boot/zlib.h>
Rick Chen8bbb2902017-12-26 13:55:49 +080023
24DECLARE_GLOBAL_DATA_PTR;
25
Alexander Grafb66babd2018-04-23 07:59:46 +020026__weak void board_quiesce_devices(void)
27{
28}
29
Lukas Auerc3b1a992018-11-22 11:26:32 +010030/**
31 * announce_and_cleanup() - Print message and prepare for kernel boot
32 *
33 * @fake: non-zero to do everything except actually boot
34 */
35static void announce_and_cleanup(int fake)
Rick Chen8bbb2902017-12-26 13:55:49 +080036{
Lukas Auerc3b1a992018-11-22 11:26:32 +010037 printf("\nStarting kernel ...%s\n\n", fake ?
38 "(fake run for tracing)" : "");
39 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
40#ifdef CONFIG_BOOTSTAGE_FDT
41 bootstage_fdt_add_report();
Rick Chen8bbb2902017-12-26 13:55:49 +080042#endif
Lukas Auerc3b1a992018-11-22 11:26:32 +010043#ifdef CONFIG_BOOTSTAGE_REPORT
44 bootstage_report();
45#endif
Rick Chen8bbb2902017-12-26 13:55:49 +080046
Lukas Auerc3b1a992018-11-22 11:26:32 +010047#ifdef CONFIG_USB_DEVICE
48 udc_disconnect();
49#endif
50
51 board_quiesce_devices();
Rick Chen8bbb2902017-12-26 13:55:49 +080052
Bin Mengb1893a92018-10-15 02:20:59 -070053 /*
54 * Call remove function of all devices with a removal flag set.
55 * This may be useful for last-stage operations, like cancelling
56 * of DMA operation or releasing device internal buffers.
57 */
58 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
59
Rick Chen8bbb2902017-12-26 13:55:49 +080060 cleanup_before_linux();
Lukas Auerc3b1a992018-11-22 11:26:32 +010061}
Bin Menged49ba42018-09-26 06:55:16 -070062
Lukas Auerc3b1a992018-11-22 11:26:32 +010063static void boot_prep_linux(bootm_headers_t *images)
64{
65 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
66#ifdef CONFIG_OF_LIBFDT
67 debug("using: FDT\n");
68 if (image_setup_linux(images)) {
69 printf("FDT creation failed! hanging...");
70 hang();
71 }
72#endif
73 } else {
74 printf("Device tree not found or missing FDT support\n");
75 hang();
76 }
77}
Rick Chen22b7e6f2018-03-13 14:59:41 +080078
Lukas Auerc3b1a992018-11-22 11:26:32 +010079static void boot_jump_linux(bootm_headers_t *images, int flag)
80{
81 void (*kernel)(ulong hart, void *dtb);
82 int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
Lukas Auerf28ad252019-03-17 19:28:38 +010083#ifdef CONFIG_SMP
84 int ret;
85#endif
Rick Chen8bbb2902017-12-26 13:55:49 +080086
Lukas Auerc3b1a992018-11-22 11:26:32 +010087 kernel = (void (*)(ulong, void *))images->ep;
88
89 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
90
Bin Meng08337cd2018-12-21 07:13:41 -080091 debug("## Transferring control to kernel (at address %08lx) ...\n",
Lukas Auerc3b1a992018-11-22 11:26:32 +010092 (ulong)kernel);
93
94 announce_and_cleanup(fake);
95
96 if (!fake) {
Lukas Auerf28ad252019-03-17 19:28:38 +010097 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
98#ifdef CONFIG_SMP
99 ret = smp_call_function(images->ep,
Lukas Auer90ae2812019-12-08 23:28:51 +0100100 (ulong)images->ft_addr, 0, 0);
Lukas Auerf28ad252019-03-17 19:28:38 +0100101 if (ret)
102 hang();
103#endif
Bin Meng3c850992018-12-12 06:12:46 -0800104 kernel(gd->arch.boot_hart, images->ft_addr);
Lukas Auerf28ad252019-03-17 19:28:38 +0100105 }
Lukas Auerc3b1a992018-11-22 11:26:32 +0100106 }
107}
108
109int do_bootm_linux(int flag, int argc, char * const argv[],
110 bootm_headers_t *images)
111{
112 /* No need for those on RISC-V */
113 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
114 return -1;
115
116 if (flag & BOOTM_STATE_OS_PREP) {
117 boot_prep_linux(images);
118 return 0;
119 }
120
121 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
122 boot_jump_linux(images, flag);
123 return 0;
124 }
125
126 boot_prep_linux(images);
127 boot_jump_linux(images, flag);
128 return 0;
Rick Chen8bbb2902017-12-26 13:55:49 +0800129}
Bin Meng08337cd2018-12-21 07:13:41 -0800130
131int do_bootm_vxworks(int flag, int argc, char * const argv[],
132 bootm_headers_t *images)
133{
134 return do_bootm_linux(flag, argc, argv, images);
135}