blob: c4137ded834bf7e6d5c3c2a350227e8f00a9bc8b [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
9#include <common.h>
Simon Glass1ea97892020-05-10 11:40:00 -060010#include <bootstage.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>
Lukas Auer86feab32018-11-22 11:26:32 +010016#include <dm/root.h>
Rick Chen6eedd922017-12-26 13:55:49 +080017#include <image.h>
Rick Chen6eedd922017-12-26 13:55:49 +080018#include <asm/byteorder.h>
Bin Meng8294bb52018-09-26 06:55:16 -070019#include <asm/csr.h>
Lukas Auerc4a6c8f2019-03-17 19:28:38 +010020#include <asm/smp.h>
Bin Meng635a2532018-10-15 02:20:59 -070021#include <dm/device.h>
22#include <dm/root.h>
23#include <u-boot/zlib.h>
Rick Chen6eedd922017-12-26 13:55:49 +080024
25DECLARE_GLOBAL_DATA_PTR;
26
Alexander Graf7ea64b02018-04-23 07:59:46 +020027__weak void board_quiesce_devices(void)
28{
29}
30
Lukas Auer86feab32018-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 Chen6eedd922017-12-26 13:55:49 +080037{
Lukas Auer86feab32018-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();
43#endif
44#ifdef CONFIG_BOOTSTAGE_REPORT
45 bootstage_report();
46#endif
Rick Chen6eedd922017-12-26 13:55:49 +080047
Lukas Auer86feab32018-11-22 11:26:32 +010048#ifdef CONFIG_USB_DEVICE
49 udc_disconnect();
50#endif
Rick Chen6eedd922017-12-26 13:55:49 +080051
Lukas Auer86feab32018-11-22 11:26:32 +010052 board_quiesce_devices();
Rick Chen6eedd922017-12-26 13:55:49 +080053
Lukas Auer86feab32018-11-22 11:26:32 +010054 /*
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);
Rick Chen6eedd922017-12-26 13:55:49 +080060
Lukas Auer86feab32018-11-22 11:26:32 +010061 cleanup_before_linux();
62}
Rick Chen6eedd922017-12-26 13:55:49 +080063
Lukas Auer86feab32018-11-22 11:26:32 +010064static void boot_prep_linux(bootm_headers_t *images)
65{
Rick Chen6eedd922017-12-26 13:55:49 +080066 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
67#ifdef CONFIG_OF_LIBFDT
68 debug("using: FDT\n");
69 if (image_setup_linux(images)) {
70 printf("FDT creation failed! hanging...");
71 hang();
72 }
73#endif
Lukas Auer86feab32018-11-22 11:26:32 +010074 } else {
75 printf("Device tree not found or missing FDT support\n");
76 hang();
Rick Chen53b47b82018-03-13 14:59:41 +080077 }
Lukas Auer86feab32018-11-22 11:26:32 +010078}
79
80static void boot_jump_linux(bootm_headers_t *images, int flag)
81{
82 void (*kernel)(ulong hart, void *dtb);
83 int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
Lukas Auerc4a6c8f2019-03-17 19:28:38 +010084#ifdef CONFIG_SMP
85 int ret;
86#endif
Rick Chen6eedd922017-12-26 13:55:49 +080087
Lukas Auer86feab32018-11-22 11:26:32 +010088 kernel = (void (*)(ulong, void *))images->ep;
Rick Chen6eedd922017-12-26 13:55:49 +080089
Lukas Auer86feab32018-11-22 11:26:32 +010090 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
Bin Meng635a2532018-10-15 02:20:59 -070091
Bin Menge63488f2018-12-21 07:13:41 -080092 debug("## Transferring control to kernel (at address %08lx) ...\n",
Lukas Auer86feab32018-11-22 11:26:32 +010093 (ulong)kernel);
Bin Meng8294bb52018-09-26 06:55:16 -070094
Lukas Auer86feab32018-11-22 11:26:32 +010095 announce_and_cleanup(fake);
Rick Chen53b47b82018-03-13 14:59:41 +080096
Lukas Auer86feab32018-11-22 11:26:32 +010097 if (!fake) {
Lukas Auerc4a6c8f2019-03-17 19:28:38 +010098 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
99#ifdef CONFIG_SMP
100 ret = smp_call_function(images->ep,
Lukas Auerc308e012019-12-08 23:28:51 +0100101 (ulong)images->ft_addr, 0, 0);
Lukas Auerc4a6c8f2019-03-17 19:28:38 +0100102 if (ret)
103 hang();
104#endif
Bin Mengfda01b62018-12-12 06:12:46 -0800105 kernel(gd->arch.boot_hart, images->ft_addr);
Lukas Auerc4a6c8f2019-03-17 19:28:38 +0100106 }
Lukas Auer86feab32018-11-22 11:26:32 +0100107 }
108}
109
Simon Glassed38aef2020-05-10 11:40:03 -0600110int do_bootm_linux(int flag, int argc, char *const argv[],
Lukas Auer86feab32018-11-22 11:26:32 +0100111 bootm_headers_t *images)
112{
113 /* 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 Glassed38aef2020-05-10 11:40:03 -0600132int do_bootm_vxworks(int flag, int argc, char *const argv[],
Bin Menge63488f2018-12-21 07:13:41 -0800133 bootm_headers_t *images)
134{
135 return do_bootm_linux(flag, argc, argv, images);
136}