blob: f9e1e18ae02648045dda571a8438e5b6b58d7a58 [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
Simon Glass52f24232020-05-10 11:40:00 -06009#include <bootstage.h>
Rick Chen8bbb2902017-12-26 13:55:49 +080010#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>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Simon Glass401d1c42020-10-30 21:38:53 -060015#include <asm/global_data.h>
Lukas Auerc3b1a992018-11-22 11:26:32 +010016#include <dm/root.h>
Rick Chen8bbb2902017-12-26 13:55:49 +080017#include <image.h>
Rick Chen8bbb2902017-12-26 13:55:49 +080018#include <asm/byteorder.h>
Bin Menged49ba42018-09-26 06:55:16 -070019#include <asm/csr.h>
Lukas Auerf28ad252019-03-17 19:28:38 +010020#include <asm/smp.h>
Bin Mengb1893a92018-10-15 02:20:59 -070021#include <dm/device.h>
22#include <dm/root.h>
23#include <u-boot/zlib.h>
Rick Chen8bbb2902017-12-26 13:55:49 +080024
25DECLARE_GLOBAL_DATA_PTR;
26
Alexander Grafb66babd2018-04-23 07:59:46 +020027__weak void board_quiesce_devices(void)
28{
29}
30
Lukas Auerc3b1a992018-11-22 11:26:32 +010031/**
32 * announce_and_cleanup() - Print message and prepare for kernel boot
33 *
34 * @fake: non-zero to do everything except actually boot
35 */
36static void announce_and_cleanup(int fake)
Rick Chen8bbb2902017-12-26 13:55:49 +080037{
Lukas Auerc3b1a992018-11-22 11:26:32 +010038 printf("\nStarting kernel ...%s\n\n", fake ?
39 "(fake run for tracing)" : "");
40 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
41#ifdef CONFIG_BOOTSTAGE_FDT
42 bootstage_fdt_add_report();
Rick Chen8bbb2902017-12-26 13:55:49 +080043#endif
Chanho Park9bcf9e42023-09-06 14:18:12 +090044#if CONFIG_IS_ENABLED(BOOTSTAGE_REPORT)
Lukas Auerc3b1a992018-11-22 11:26:32 +010045 bootstage_report();
46#endif
Rick Chen8bbb2902017-12-26 13:55:49 +080047
Lukas Auerc3b1a992018-11-22 11:26:32 +010048#ifdef CONFIG_USB_DEVICE
49 udc_disconnect();
50#endif
51
52 board_quiesce_devices();
Rick Chen8bbb2902017-12-26 13:55:49 +080053
Bin Mengb1893a92018-10-15 02:20:59 -070054 /*
55 * Call remove function of all devices with a removal flag set.
56 * This may be useful for last-stage operations, like cancelling
57 * of DMA operation or releasing device internal buffers.
58 */
59 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
60
Rick Chen8bbb2902017-12-26 13:55:49 +080061 cleanup_before_linux();
Lukas Auerc3b1a992018-11-22 11:26:32 +010062}
Bin Menged49ba42018-09-26 06:55:16 -070063
Simon Glassd9d7c202022-09-06 20:26:50 -060064static void boot_prep_linux(struct bootm_headers *images)
Lukas Auerc3b1a992018-11-22 11:26:32 +010065{
Simon Glass210af542023-02-05 15:40:13 -070066 if (CONFIG_IS_ENABLED(OF_LIBFDT) && IS_ENABLED(CONFIG_LMB) && images->ft_len) {
Lukas Auerc3b1a992018-11-22 11:26:32 +010067 debug("using: FDT\n");
68 if (image_setup_linux(images)) {
69 printf("FDT creation failed! hanging...");
70 hang();
71 }
Lukas Auerc3b1a992018-11-22 11:26:32 +010072 } else {
73 printf("Device tree not found or missing FDT support\n");
74 hang();
75 }
76}
Rick Chen22b7e6f2018-03-13 14:59:41 +080077
Simon Glassd9d7c202022-09-06 20:26:50 -060078static void boot_jump_linux(struct bootm_headers *images, int flag)
Lukas Auerc3b1a992018-11-22 11:26:32 +010079{
80 void (*kernel)(ulong hart, void *dtb);
81 int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
Lukas Auerf28ad252019-03-17 19:28:38 +010082#ifdef CONFIG_SMP
83 int ret;
84#endif
Rick Chen8bbb2902017-12-26 13:55:49 +080085
Lukas Auerc3b1a992018-11-22 11:26:32 +010086 kernel = (void (*)(ulong, void *))images->ep;
87
88 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
89
Bin Meng08337cd2018-12-21 07:13:41 -080090 debug("## Transferring control to kernel (at address %08lx) ...\n",
Lukas Auerc3b1a992018-11-22 11:26:32 +010091 (ulong)kernel);
92
93 announce_and_cleanup(fake);
94
95 if (!fake) {
Simon Glass0c303f92021-09-25 19:43:21 -060096 if (CONFIG_IS_ENABLED(OF_LIBFDT) && images->ft_len) {
Lukas Auerf28ad252019-03-17 19:28:38 +010097#ifdef CONFIG_SMP
98 ret = smp_call_function(images->ep,
Lukas Auer90ae2812019-12-08 23:28:51 +010099 (ulong)images->ft_addr, 0, 0);
Lukas Auerf28ad252019-03-17 19:28:38 +0100100 if (ret)
101 hang();
102#endif
Bin Meng3c850992018-12-12 06:12:46 -0800103 kernel(gd->arch.boot_hart, images->ft_addr);
Lukas Auerf28ad252019-03-17 19:28:38 +0100104 }
Lukas Auerc3b1a992018-11-22 11:26:32 +0100105 }
106}
107
Simon Glass09140112020-05-10 11:40:03 -0600108int do_bootm_linux(int flag, int argc, char *const argv[],
Simon Glassd9d7c202022-09-06 20:26:50 -0600109 struct bootm_headers *images)
Lukas Auerc3b1a992018-11-22 11:26:32 +0100110{
111 /* No need for those on RISC-V */
112 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
113 return -1;
114
115 if (flag & BOOTM_STATE_OS_PREP) {
116 boot_prep_linux(images);
117 return 0;
118 }
119
120 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
121 boot_jump_linux(images, flag);
122 return 0;
123 }
124
125 boot_prep_linux(images);
126 boot_jump_linux(images, flag);
127 return 0;
Rick Chen8bbb2902017-12-26 13:55:49 +0800128}
Bin Meng08337cd2018-12-21 07:13:41 -0800129
Simon Glass09140112020-05-10 11:40:03 -0600130int do_bootm_vxworks(int flag, int argc, char *const argv[],
Simon Glassd9d7c202022-09-06 20:26:50 -0600131 struct bootm_headers *images)
Bin Meng08337cd2018-12-21 07:13:41 -0800132{
133 return do_bootm_linux(flag, argc, argv, images);
134}
Marek Vasuteeaa3fe2021-09-10 22:47:15 +0200135
136static ulong get_sp(void)
137{
138 ulong ret;
139
140 asm("mv %0, sp" : "=r"(ret) : );
141 return ret;
142}
143
144void arch_lmb_reserve(struct lmb *lmb)
145{
146 arch_lmb_reserve_generic(lmb, get_sp(), gd->ram_top, 4096);
147}