blob: 8dd18205403c6c5b00f131d4d57f61051323411f [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>
Simon Glass52f24232020-05-10 11:40:00 -060010#include <bootstage.h>
Rick Chen8bbb2902017-12-26 13:55:49 +080011#include <command.h>
Lukas Auerc3b1a992018-11-22 11:26:32 +010012#include <dm.h>
Simon Glass807765b2019-12-28 10:44:54 -070013#include <fdt_support.h>
Simon Glassdb41d652019-12-28 10:45:07 -070014#include <hang.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Simon Glass401d1c42020-10-30 21:38:53 -060016#include <asm/global_data.h>
Lukas Auerc3b1a992018-11-22 11:26:32 +010017#include <dm/root.h>
Rick Chen8bbb2902017-12-26 13:55:49 +080018#include <image.h>
Rick Chen8bbb2902017-12-26 13:55:49 +080019#include <asm/byteorder.h>
Bin Menged49ba42018-09-26 06:55:16 -070020#include <asm/csr.h>
Lukas Auerf28ad252019-03-17 19:28:38 +010021#include <asm/smp.h>
Bin Mengb1893a92018-10-15 02:20:59 -070022#include <dm/device.h>
23#include <dm/root.h>
24#include <u-boot/zlib.h>
Rick Chen8bbb2902017-12-26 13:55:49 +080025
26DECLARE_GLOBAL_DATA_PTR;
27
Alexander Grafb66babd2018-04-23 07:59:46 +020028__weak void board_quiesce_devices(void)
29{
30}
31
Lukas Auerc3b1a992018-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 Chen8bbb2902017-12-26 13:55:49 +080038{
Lukas Auerc3b1a992018-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();
Rick Chen8bbb2902017-12-26 13:55:49 +080044#endif
Lukas Auerc3b1a992018-11-22 11:26:32 +010045#ifdef CONFIG_BOOTSTAGE_REPORT
46 bootstage_report();
47#endif
Rick Chen8bbb2902017-12-26 13:55:49 +080048
Lukas Auerc3b1a992018-11-22 11:26:32 +010049#ifdef CONFIG_USB_DEVICE
50 udc_disconnect();
51#endif
52
53 board_quiesce_devices();
Rick Chen8bbb2902017-12-26 13:55:49 +080054
Bin Mengb1893a92018-10-15 02:20:59 -070055 /*
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 Chen8bbb2902017-12-26 13:55:49 +080062 cleanup_before_linux();
Lukas Auerc3b1a992018-11-22 11:26:32 +010063}
Bin Menged49ba42018-09-26 06:55:16 -070064
Lukas Auerc3b1a992018-11-22 11:26:32 +010065static void boot_prep_linux(bootm_headers_t *images)
66{
67 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
68#ifdef CONFIG_OF_LIBFDT
69 debug("using: FDT\n");
70 if (image_setup_linux(images)) {
71 printf("FDT creation failed! hanging...");
72 hang();
73 }
74#endif
75 } else {
76 printf("Device tree not found or missing FDT support\n");
77 hang();
78 }
79}
Rick Chen22b7e6f2018-03-13 14:59:41 +080080
Lukas Auerc3b1a992018-11-22 11:26:32 +010081static void boot_jump_linux(bootm_headers_t *images, int flag)
82{
83 void (*kernel)(ulong hart, void *dtb);
84 int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
Lukas Auerf28ad252019-03-17 19:28:38 +010085#ifdef CONFIG_SMP
86 int ret;
87#endif
Rick Chen8bbb2902017-12-26 13:55:49 +080088
Lukas Auerc3b1a992018-11-22 11:26:32 +010089 kernel = (void (*)(ulong, void *))images->ep;
90
91 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
92
Bin Meng08337cd2018-12-21 07:13:41 -080093 debug("## Transferring control to kernel (at address %08lx) ...\n",
Lukas Auerc3b1a992018-11-22 11:26:32 +010094 (ulong)kernel);
95
96 announce_and_cleanup(fake);
97
98 if (!fake) {
Lukas Auerf28ad252019-03-17 19:28:38 +010099 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
100#ifdef CONFIG_SMP
101 ret = smp_call_function(images->ep,
Lukas Auer90ae2812019-12-08 23:28:51 +0100102 (ulong)images->ft_addr, 0, 0);
Lukas Auerf28ad252019-03-17 19:28:38 +0100103 if (ret)
104 hang();
105#endif
Bin Meng3c850992018-12-12 06:12:46 -0800106 kernel(gd->arch.boot_hart, images->ft_addr);
Lukas Auerf28ad252019-03-17 19:28:38 +0100107 }
Lukas Auerc3b1a992018-11-22 11:26:32 +0100108 }
109}
110
Simon Glass09140112020-05-10 11:40:03 -0600111int do_bootm_linux(int flag, int argc, char *const argv[],
Lukas Auerc3b1a992018-11-22 11:26:32 +0100112 bootm_headers_t *images)
113{
114 /* No need for those on RISC-V */
115 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
116 return -1;
117
118 if (flag & BOOTM_STATE_OS_PREP) {
119 boot_prep_linux(images);
120 return 0;
121 }
122
123 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
124 boot_jump_linux(images, flag);
125 return 0;
126 }
127
128 boot_prep_linux(images);
129 boot_jump_linux(images, flag);
130 return 0;
Rick Chen8bbb2902017-12-26 13:55:49 +0800131}
Bin Meng08337cd2018-12-21 07:13:41 -0800132
Simon Glass09140112020-05-10 11:40:03 -0600133int do_bootm_vxworks(int flag, int argc, char *const argv[],
Bin Meng08337cd2018-12-21 07:13:41 -0800134 bootm_headers_t *images)
135{
136 return do_bootm_linux(flag, argc, argv, images);
137}