blob: 82502972eec5148b8a36b45eef412a3ec2adb48c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Rick Chen6eedd922017-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 Chen6eedd922017-12-26 13:55:49 +08007 */
8
Simon Glass1ea97892020-05-10 11:40:00 -06009#include <bootstage.h>
Simon Glass0726d9d2023-12-15 20:14:13 -070010#include <bootm.h>
Rick Chen6eedd922017-12-26 13:55:49 +080011#include <command.h>
Lukas Auer86feab32018-11-22 11:26:32 +010012#include <dm.h>
Simon Glass3bbe70c2019-12-28 10:44:54 -070013#include <fdt_support.h>
Simon Glassf11478f2019-12-28 10:45:07 -070014#include <hang.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
Lukas Auer86feab32018-11-22 11:26:32 +010017#include <dm/root.h>
Rick Chen6eedd922017-12-26 13:55:49 +080018#include <image.h>
Rick Chen6eedd922017-12-26 13:55:49 +080019#include <asm/byteorder.h>
Bin Meng8294bb52018-09-26 06:55:16 -070020#include <asm/csr.h>
Lukas Auerc4a6c8f2019-03-17 19:28:38 +010021#include <asm/smp.h>
Bin Meng635a2532018-10-15 02:20:59 -070022#include <dm/device.h>
23#include <dm/root.h>
24#include <u-boot/zlib.h>
Rick Chen6eedd922017-12-26 13:55:49 +080025
26DECLARE_GLOBAL_DATA_PTR;
27
Alexander Graf7ea64b02018-04-23 07:59:46 +020028__weak void board_quiesce_devices(void)
29{
30}
31
Lukas Auer86feab32018-11-22 11:26:32 +010032/**
33 * announce_and_cleanup() - Print message and prepare for kernel boot
34 *
35 * @fake: non-zero to do everything except actually boot
36 */
37static void announce_and_cleanup(int fake)
Rick Chen6eedd922017-12-26 13:55:49 +080038{
Lukas Auer86feab32018-11-22 11:26:32 +010039 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();
44#endif
Chanho Park19f63612023-09-06 14:18:12 +090045#if CONFIG_IS_ENABLED(BOOTSTAGE_REPORT)
Lukas Auer86feab32018-11-22 11:26:32 +010046 bootstage_report();
47#endif
Rick Chen6eedd922017-12-26 13:55:49 +080048
Lukas Auer86feab32018-11-22 11:26:32 +010049#ifdef CONFIG_USB_DEVICE
50 udc_disconnect();
51#endif
Rick Chen6eedd922017-12-26 13:55:49 +080052
Lukas Auer86feab32018-11-22 11:26:32 +010053 board_quiesce_devices();
Rick Chen6eedd922017-12-26 13:55:49 +080054
Lukas Auer86feab32018-11-22 11:26:32 +010055 /*
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);
Rick Chen6eedd922017-12-26 13:55:49 +080061
Lukas Auer86feab32018-11-22 11:26:32 +010062 cleanup_before_linux();
63}
Rick Chen6eedd922017-12-26 13:55:49 +080064
Simon Glassdf00afa2022-09-06 20:26:50 -060065static void boot_prep_linux(struct bootm_headers *images)
Lukas Auer86feab32018-11-22 11:26:32 +010066{
Simon Glassae7ed572023-02-05 15:40:13 -070067 if (CONFIG_IS_ENABLED(OF_LIBFDT) && IS_ENABLED(CONFIG_LMB) && images->ft_len) {
Rick Chen6eedd922017-12-26 13:55:49 +080068 debug("using: FDT\n");
69 if (image_setup_linux(images)) {
70 printf("FDT creation failed! hanging...");
71 hang();
72 }
Lukas Auer86feab32018-11-22 11:26:32 +010073 } else {
74 printf("Device tree not found or missing FDT support\n");
75 hang();
Rick Chen53b47b82018-03-13 14:59:41 +080076 }
Lukas Auer86feab32018-11-22 11:26:32 +010077}
78
Simon Glassdf00afa2022-09-06 20:26:50 -060079static void boot_jump_linux(struct bootm_headers *images, int flag)
Lukas Auer86feab32018-11-22 11:26:32 +010080{
81 void (*kernel)(ulong hart, void *dtb);
82 int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
Lukas Auerc4a6c8f2019-03-17 19:28:38 +010083#ifdef CONFIG_SMP
84 int ret;
85#endif
Rick Chen6eedd922017-12-26 13:55:49 +080086
Lukas Auer86feab32018-11-22 11:26:32 +010087 kernel = (void (*)(ulong, void *))images->ep;
Rick Chen6eedd922017-12-26 13:55:49 +080088
Lukas Auer86feab32018-11-22 11:26:32 +010089 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
Bin Meng635a2532018-10-15 02:20:59 -070090
Bin Menge63488f2018-12-21 07:13:41 -080091 debug("## Transferring control to kernel (at address %08lx) ...\n",
Lukas Auer86feab32018-11-22 11:26:32 +010092 (ulong)kernel);
Bin Meng8294bb52018-09-26 06:55:16 -070093
Lukas Auer86feab32018-11-22 11:26:32 +010094 announce_and_cleanup(fake);
Rick Chen53b47b82018-03-13 14:59:41 +080095
Lukas Auer86feab32018-11-22 11:26:32 +010096 if (!fake) {
Simon Glass85c057e2021-09-25 19:43:21 -060097 if (CONFIG_IS_ENABLED(OF_LIBFDT) && images->ft_len) {
Lukas Auerc4a6c8f2019-03-17 19:28:38 +010098#ifdef CONFIG_SMP
99 ret = smp_call_function(images->ep,
Lukas Auerc308e012019-12-08 23:28:51 +0100100 (ulong)images->ft_addr, 0, 0);
Lukas Auerc4a6c8f2019-03-17 19:28:38 +0100101 if (ret)
102 hang();
103#endif
Bin Mengfda01b62018-12-12 06:12:46 -0800104 kernel(gd->arch.boot_hart, images->ft_addr);
Lukas Auerc4a6c8f2019-03-17 19:28:38 +0100105 }
Lukas Auer86feab32018-11-22 11:26:32 +0100106 }
107}
108
Simon Glass0726d9d2023-12-15 20:14:13 -0700109int do_bootm_linux(int flag, struct bootm_info *bmi)
Lukas Auer86feab32018-11-22 11:26:32 +0100110{
Simon Glass0726d9d2023-12-15 20:14:13 -0700111 struct bootm_headers *images = bmi->images;
112
Lukas Auer86feab32018-11-22 11:26:32 +0100113 /* No need for those on RISC-V */
114 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
115 return -1;
Rick Chen6eedd922017-12-26 13:55:49 +0800116
Lukas Auer86feab32018-11-22 11:26:32 +0100117 if (flag & BOOTM_STATE_OS_PREP) {
118 boot_prep_linux(images);
119 return 0;
120 }
121
122 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
123 boot_jump_linux(images, flag);
124 return 0;
125 }
126
127 boot_prep_linux(images);
128 boot_jump_linux(images, flag);
129 return 0;
Rick Chen6eedd922017-12-26 13:55:49 +0800130}
Bin Menge63488f2018-12-21 07:13:41 -0800131
Simon Glass0726d9d2023-12-15 20:14:13 -0700132int do_bootm_vxworks(int flag, struct bootm_info *bmi)
Bin Menge63488f2018-12-21 07:13:41 -0800133{
Simon Glass0726d9d2023-12-15 20:14:13 -0700134 return do_bootm_linux(flag, bmi);
Bin Menge63488f2018-12-21 07:13:41 -0800135}