Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Rick Chen | 8bbb290 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 2 | /* |
| 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 Chen | 8bbb290 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Simon Glass | 52f2423 | 2020-05-10 11:40:00 -0600 | [diff] [blame] | 10 | #include <bootstage.h> |
Rick Chen | 8bbb290 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 11 | #include <command.h> |
Lukas Auer | c3b1a99 | 2018-11-22 11:26:32 +0100 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | 807765b | 2019-12-28 10:44:54 -0700 | [diff] [blame] | 13 | #include <fdt_support.h> |
Simon Glass | db41d65 | 2019-12-28 10:45:07 -0700 | [diff] [blame] | 14 | #include <hang.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 16 | #include <asm/global_data.h> |
Lukas Auer | c3b1a99 | 2018-11-22 11:26:32 +0100 | [diff] [blame] | 17 | #include <dm/root.h> |
Rick Chen | 8bbb290 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 18 | #include <image.h> |
Rick Chen | 8bbb290 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 19 | #include <asm/byteorder.h> |
Bin Meng | ed49ba4 | 2018-09-26 06:55:16 -0700 | [diff] [blame] | 20 | #include <asm/csr.h> |
Lukas Auer | f28ad25 | 2019-03-17 19:28:38 +0100 | [diff] [blame] | 21 | #include <asm/smp.h> |
Bin Meng | b1893a9 | 2018-10-15 02:20:59 -0700 | [diff] [blame] | 22 | #include <dm/device.h> |
| 23 | #include <dm/root.h> |
| 24 | #include <u-boot/zlib.h> |
Rick Chen | 8bbb290 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 25 | |
| 26 | DECLARE_GLOBAL_DATA_PTR; |
| 27 | |
Alexander Graf | b66babd | 2018-04-23 07:59:46 +0200 | [diff] [blame] | 28 | __weak void board_quiesce_devices(void) |
| 29 | { |
| 30 | } |
| 31 | |
Lukas Auer | c3b1a99 | 2018-11-22 11:26:32 +0100 | [diff] [blame] | 32 | /** |
| 33 | * announce_and_cleanup() - Print message and prepare for kernel boot |
| 34 | * |
| 35 | * @fake: non-zero to do everything except actually boot |
| 36 | */ |
| 37 | static void announce_and_cleanup(int fake) |
Rick Chen | 8bbb290 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 38 | { |
Lukas Auer | c3b1a99 | 2018-11-22 11:26:32 +0100 | [diff] [blame] | 39 | printf("\nStarting kernel ...%s\n\n", fake ? |
| 40 | "(fake run for tracing)" : ""); |
| 41 | bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel"); |
| 42 | #ifdef CONFIG_BOOTSTAGE_FDT |
| 43 | bootstage_fdt_add_report(); |
Rick Chen | 8bbb290 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 44 | #endif |
Lukas Auer | c3b1a99 | 2018-11-22 11:26:32 +0100 | [diff] [blame] | 45 | #ifdef CONFIG_BOOTSTAGE_REPORT |
| 46 | bootstage_report(); |
| 47 | #endif |
Rick Chen | 8bbb290 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 48 | |
Lukas Auer | c3b1a99 | 2018-11-22 11:26:32 +0100 | [diff] [blame] | 49 | #ifdef CONFIG_USB_DEVICE |
| 50 | udc_disconnect(); |
| 51 | #endif |
| 52 | |
| 53 | board_quiesce_devices(); |
Rick Chen | 8bbb290 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 54 | |
Bin Meng | b1893a9 | 2018-10-15 02:20:59 -0700 | [diff] [blame] | 55 | /* |
| 56 | * Call remove function of all devices with a removal flag set. |
| 57 | * This may be useful for last-stage operations, like cancelling |
| 58 | * of DMA operation or releasing device internal buffers. |
| 59 | */ |
| 60 | dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL); |
| 61 | |
Rick Chen | 8bbb290 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 62 | cleanup_before_linux(); |
Lukas Auer | c3b1a99 | 2018-11-22 11:26:32 +0100 | [diff] [blame] | 63 | } |
Bin Meng | ed49ba4 | 2018-09-26 06:55:16 -0700 | [diff] [blame] | 64 | |
Simon Glass | d9d7c20 | 2022-09-06 20:26:50 -0600 | [diff] [blame] | 65 | static void boot_prep_linux(struct bootm_headers *images) |
Lukas Auer | c3b1a99 | 2018-11-22 11:26:32 +0100 | [diff] [blame] | 66 | { |
Simon Glass | 210af54 | 2023-02-05 15:40:13 -0700 | [diff] [blame] | 67 | if (CONFIG_IS_ENABLED(OF_LIBFDT) && IS_ENABLED(CONFIG_LMB) && images->ft_len) { |
Lukas Auer | c3b1a99 | 2018-11-22 11:26:32 +0100 | [diff] [blame] | 68 | debug("using: FDT\n"); |
| 69 | if (image_setup_linux(images)) { |
| 70 | printf("FDT creation failed! hanging..."); |
| 71 | hang(); |
| 72 | } |
Lukas Auer | c3b1a99 | 2018-11-22 11:26:32 +0100 | [diff] [blame] | 73 | } else { |
| 74 | printf("Device tree not found or missing FDT support\n"); |
| 75 | hang(); |
| 76 | } |
| 77 | } |
Rick Chen | 22b7e6f | 2018-03-13 14:59:41 +0800 | [diff] [blame] | 78 | |
Simon Glass | d9d7c20 | 2022-09-06 20:26:50 -0600 | [diff] [blame] | 79 | static void boot_jump_linux(struct bootm_headers *images, int flag) |
Lukas Auer | c3b1a99 | 2018-11-22 11:26:32 +0100 | [diff] [blame] | 80 | { |
| 81 | void (*kernel)(ulong hart, void *dtb); |
| 82 | int fake = (flag & BOOTM_STATE_OS_FAKE_GO); |
Lukas Auer | f28ad25 | 2019-03-17 19:28:38 +0100 | [diff] [blame] | 83 | #ifdef CONFIG_SMP |
| 84 | int ret; |
| 85 | #endif |
Rick Chen | 8bbb290 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 86 | |
Lukas Auer | c3b1a99 | 2018-11-22 11:26:32 +0100 | [diff] [blame] | 87 | kernel = (void (*)(ulong, void *))images->ep; |
| 88 | |
| 89 | bootstage_mark(BOOTSTAGE_ID_RUN_OS); |
| 90 | |
Bin Meng | 08337cd | 2018-12-21 07:13:41 -0800 | [diff] [blame] | 91 | debug("## Transferring control to kernel (at address %08lx) ...\n", |
Lukas Auer | c3b1a99 | 2018-11-22 11:26:32 +0100 | [diff] [blame] | 92 | (ulong)kernel); |
| 93 | |
| 94 | announce_and_cleanup(fake); |
| 95 | |
| 96 | if (!fake) { |
Simon Glass | 0c303f9 | 2021-09-25 19:43:21 -0600 | [diff] [blame] | 97 | if (CONFIG_IS_ENABLED(OF_LIBFDT) && images->ft_len) { |
Lukas Auer | f28ad25 | 2019-03-17 19:28:38 +0100 | [diff] [blame] | 98 | #ifdef CONFIG_SMP |
| 99 | ret = smp_call_function(images->ep, |
Lukas Auer | 90ae281 | 2019-12-08 23:28:51 +0100 | [diff] [blame] | 100 | (ulong)images->ft_addr, 0, 0); |
Lukas Auer | f28ad25 | 2019-03-17 19:28:38 +0100 | [diff] [blame] | 101 | if (ret) |
| 102 | hang(); |
| 103 | #endif |
Bin Meng | 3c85099 | 2018-12-12 06:12:46 -0800 | [diff] [blame] | 104 | kernel(gd->arch.boot_hart, images->ft_addr); |
Lukas Auer | f28ad25 | 2019-03-17 19:28:38 +0100 | [diff] [blame] | 105 | } |
Lukas Auer | c3b1a99 | 2018-11-22 11:26:32 +0100 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 109 | int do_bootm_linux(int flag, int argc, char *const argv[], |
Simon Glass | d9d7c20 | 2022-09-06 20:26:50 -0600 | [diff] [blame] | 110 | struct bootm_headers *images) |
Lukas Auer | c3b1a99 | 2018-11-22 11:26:32 +0100 | [diff] [blame] | 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 Chen | 8bbb290 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 129 | } |
Bin Meng | 08337cd | 2018-12-21 07:13:41 -0800 | [diff] [blame] | 130 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 131 | int do_bootm_vxworks(int flag, int argc, char *const argv[], |
Simon Glass | d9d7c20 | 2022-09-06 20:26:50 -0600 | [diff] [blame] | 132 | struct bootm_headers *images) |
Bin Meng | 08337cd | 2018-12-21 07:13:41 -0800 | [diff] [blame] | 133 | { |
| 134 | return do_bootm_linux(flag, argc, argv, images); |
| 135 | } |
Marek Vasut | eeaa3fe | 2021-09-10 22:47:15 +0200 | [diff] [blame] | 136 | |
| 137 | static ulong get_sp(void) |
| 138 | { |
| 139 | ulong ret; |
| 140 | |
| 141 | asm("mv %0, sp" : "=r"(ret) : ); |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | void arch_lmb_reserve(struct lmb *lmb) |
| 146 | { |
| 147 | arch_lmb_reserve_generic(lmb, get_sp(), gd->ram_top, 4096); |
| 148 | } |