blob: fad16901c5f2a032019b5a73d412aa9b510b6f45 [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>
10#include <command.h>
Lukas Auerc3b1a992018-11-22 11:26:32 +010011#include <dm.h>
Simon Glass807765b2019-12-28 10:44:54 -070012#include <fdt_support.h>
Simon Glassdb41d652019-12-28 10:45:07 -070013#include <hang.h>
Lukas Auerc3b1a992018-11-22 11:26:32 +010014#include <dm/root.h>
Rick Chen8bbb2902017-12-26 13:55:49 +080015#include <image.h>
Rick Chen8bbb2902017-12-26 13:55:49 +080016#include <asm/byteorder.h>
Bin Menged49ba42018-09-26 06:55:16 -070017#include <asm/csr.h>
Lukas Auerf28ad252019-03-17 19:28:38 +010018#include <asm/smp.h>
Bin Mengb1893a92018-10-15 02:20:59 -070019#include <dm/device.h>
20#include <dm/root.h>
21#include <u-boot/zlib.h>
Rick Chen8bbb2902017-12-26 13:55:49 +080022
23DECLARE_GLOBAL_DATA_PTR;
24
Alexander Grafb66babd2018-04-23 07:59:46 +020025__weak void board_quiesce_devices(void)
26{
27}
28
Rick Chen8bbb2902017-12-26 13:55:49 +080029int arch_fixup_fdt(void *blob)
30{
31 return 0;
32}
33
Lukas Auerc3b1a992018-11-22 11:26:32 +010034/**
35 * announce_and_cleanup() - Print message and prepare for kernel boot
36 *
37 * @fake: non-zero to do everything except actually boot
38 */
39static void announce_and_cleanup(int fake)
Rick Chen8bbb2902017-12-26 13:55:49 +080040{
Lukas Auerc3b1a992018-11-22 11:26:32 +010041 printf("\nStarting kernel ...%s\n\n", fake ?
42 "(fake run for tracing)" : "");
43 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
44#ifdef CONFIG_BOOTSTAGE_FDT
45 bootstage_fdt_add_report();
Rick Chen8bbb2902017-12-26 13:55:49 +080046#endif
Lukas Auerc3b1a992018-11-22 11:26:32 +010047#ifdef CONFIG_BOOTSTAGE_REPORT
48 bootstage_report();
49#endif
Rick Chen8bbb2902017-12-26 13:55:49 +080050
Lukas Auerc3b1a992018-11-22 11:26:32 +010051#ifdef CONFIG_USB_DEVICE
52 udc_disconnect();
53#endif
54
55 board_quiesce_devices();
Rick Chen8bbb2902017-12-26 13:55:49 +080056
Bin Mengb1893a92018-10-15 02:20:59 -070057 /*
58 * Call remove function of all devices with a removal flag set.
59 * This may be useful for last-stage operations, like cancelling
60 * of DMA operation or releasing device internal buffers.
61 */
62 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
63
Rick Chen8bbb2902017-12-26 13:55:49 +080064 cleanup_before_linux();
Lukas Auerc3b1a992018-11-22 11:26:32 +010065}
Bin Menged49ba42018-09-26 06:55:16 -070066
Lukas Auerc3b1a992018-11-22 11:26:32 +010067static void boot_prep_linux(bootm_headers_t *images)
68{
69 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
70#ifdef CONFIG_OF_LIBFDT
71 debug("using: FDT\n");
72 if (image_setup_linux(images)) {
73 printf("FDT creation failed! hanging...");
74 hang();
75 }
76#endif
77 } else {
78 printf("Device tree not found or missing FDT support\n");
79 hang();
80 }
81}
Rick Chen22b7e6f2018-03-13 14:59:41 +080082
Lukas Auerc3b1a992018-11-22 11:26:32 +010083static void boot_jump_linux(bootm_headers_t *images, int flag)
84{
85 void (*kernel)(ulong hart, void *dtb);
86 int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
Lukas Auerf28ad252019-03-17 19:28:38 +010087#ifdef CONFIG_SMP
88 int ret;
89#endif
Rick Chen8bbb2902017-12-26 13:55:49 +080090
Lukas Auerc3b1a992018-11-22 11:26:32 +010091 kernel = (void (*)(ulong, void *))images->ep;
92
93 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
94
Bin Meng08337cd2018-12-21 07:13:41 -080095 debug("## Transferring control to kernel (at address %08lx) ...\n",
Lukas Auerc3b1a992018-11-22 11:26:32 +010096 (ulong)kernel);
97
98 announce_and_cleanup(fake);
99
100 if (!fake) {
Lukas Auerf28ad252019-03-17 19:28:38 +0100101 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
102#ifdef CONFIG_SMP
103 ret = smp_call_function(images->ep,
Lukas Auer90ae2812019-12-08 23:28:51 +0100104 (ulong)images->ft_addr, 0, 0);
Lukas Auerf28ad252019-03-17 19:28:38 +0100105 if (ret)
106 hang();
107#endif
Bin Meng3c850992018-12-12 06:12:46 -0800108 kernel(gd->arch.boot_hart, images->ft_addr);
Lukas Auerf28ad252019-03-17 19:28:38 +0100109 }
Lukas Auerc3b1a992018-11-22 11:26:32 +0100110 }
111}
112
113int do_bootm_linux(int flag, int argc, char * const argv[],
114 bootm_headers_t *images)
115{
116 /* No need for those on RISC-V */
117 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
118 return -1;
119
120 if (flag & BOOTM_STATE_OS_PREP) {
121 boot_prep_linux(images);
122 return 0;
123 }
124
125 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
126 boot_jump_linux(images, flag);
127 return 0;
128 }
129
130 boot_prep_linux(images);
131 boot_jump_linux(images, flag);
132 return 0;
Rick Chen8bbb2902017-12-26 13:55:49 +0800133}
Bin Meng08337cd2018-12-21 07:13:41 -0800134
135int do_bootm_vxworks(int flag, int argc, char * const argv[],
136 bootm_headers_t *images)
137{
138 return do_bootm_linux(flag, argc, argv, images);
139}