blob: 0d06095da11aab3b6cb74a75b060fadd5d897d1e [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>
10#include <command.h>
Lukas Auer86feab32018-11-22 11:26:32 +010011#include <dm.h>
Simon Glass3bbe70c2019-12-28 10:44:54 -070012#include <fdt_support.h>
Simon Glassf11478f2019-12-28 10:45:07 -070013#include <hang.h>
Lukas Auer86feab32018-11-22 11:26:32 +010014#include <dm/root.h>
Rick Chen6eedd922017-12-26 13:55:49 +080015#include <image.h>
Rick Chen6eedd922017-12-26 13:55:49 +080016#include <asm/byteorder.h>
Bin Meng8294bb52018-09-26 06:55:16 -070017#include <asm/csr.h>
Lukas Auerc4a6c8f2019-03-17 19:28:38 +010018#include <asm/smp.h>
Bin Meng635a2532018-10-15 02:20:59 -070019#include <dm/device.h>
20#include <dm/root.h>
21#include <u-boot/zlib.h>
Rick Chen6eedd922017-12-26 13:55:49 +080022
23DECLARE_GLOBAL_DATA_PTR;
24
Alexander Graf7ea64b02018-04-23 07:59:46 +020025__weak void board_quiesce_devices(void)
26{
27}
28
Lukas Auer86feab32018-11-22 11:26:32 +010029/**
30 * announce_and_cleanup() - Print message and prepare for kernel boot
31 *
32 * @fake: non-zero to do everything except actually boot
33 */
34static void announce_and_cleanup(int fake)
Rick Chen6eedd922017-12-26 13:55:49 +080035{
Lukas Auer86feab32018-11-22 11:26:32 +010036 printf("\nStarting kernel ...%s\n\n", fake ?
37 "(fake run for tracing)" : "");
38 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
39#ifdef CONFIG_BOOTSTAGE_FDT
40 bootstage_fdt_add_report();
41#endif
42#ifdef CONFIG_BOOTSTAGE_REPORT
43 bootstage_report();
44#endif
Rick Chen6eedd922017-12-26 13:55:49 +080045
Lukas Auer86feab32018-11-22 11:26:32 +010046#ifdef CONFIG_USB_DEVICE
47 udc_disconnect();
48#endif
Rick Chen6eedd922017-12-26 13:55:49 +080049
Lukas Auer86feab32018-11-22 11:26:32 +010050 board_quiesce_devices();
Rick Chen6eedd922017-12-26 13:55:49 +080051
Lukas Auer86feab32018-11-22 11:26:32 +010052 /*
53 * Call remove function of all devices with a removal flag set.
54 * This may be useful for last-stage operations, like cancelling
55 * of DMA operation or releasing device internal buffers.
56 */
57 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
Rick Chen6eedd922017-12-26 13:55:49 +080058
Lukas Auer86feab32018-11-22 11:26:32 +010059 cleanup_before_linux();
60}
Rick Chen6eedd922017-12-26 13:55:49 +080061
Lukas Auer86feab32018-11-22 11:26:32 +010062static void boot_prep_linux(bootm_headers_t *images)
63{
Rick Chen6eedd922017-12-26 13:55:49 +080064 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
65#ifdef CONFIG_OF_LIBFDT
66 debug("using: FDT\n");
67 if (image_setup_linux(images)) {
68 printf("FDT creation failed! hanging...");
69 hang();
70 }
71#endif
Lukas Auer86feab32018-11-22 11:26:32 +010072 } else {
73 printf("Device tree not found or missing FDT support\n");
74 hang();
Rick Chen53b47b82018-03-13 14:59:41 +080075 }
Lukas Auer86feab32018-11-22 11:26:32 +010076}
77
78static void boot_jump_linux(bootm_headers_t *images, int flag)
79{
80 void (*kernel)(ulong hart, void *dtb);
81 int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
Lukas Auerc4a6c8f2019-03-17 19:28:38 +010082#ifdef CONFIG_SMP
83 int ret;
84#endif
Rick Chen6eedd922017-12-26 13:55:49 +080085
Lukas Auer86feab32018-11-22 11:26:32 +010086 kernel = (void (*)(ulong, void *))images->ep;
Rick Chen6eedd922017-12-26 13:55:49 +080087
Lukas Auer86feab32018-11-22 11:26:32 +010088 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
Bin Meng635a2532018-10-15 02:20:59 -070089
Bin Menge63488f2018-12-21 07:13:41 -080090 debug("## Transferring control to kernel (at address %08lx) ...\n",
Lukas Auer86feab32018-11-22 11:26:32 +010091 (ulong)kernel);
Bin Meng8294bb52018-09-26 06:55:16 -070092
Lukas Auer86feab32018-11-22 11:26:32 +010093 announce_and_cleanup(fake);
Rick Chen53b47b82018-03-13 14:59:41 +080094
Lukas Auer86feab32018-11-22 11:26:32 +010095 if (!fake) {
Lukas Auerc4a6c8f2019-03-17 19:28:38 +010096 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
97#ifdef CONFIG_SMP
98 ret = smp_call_function(images->ep,
Lukas Auerc308e012019-12-08 23:28:51 +010099 (ulong)images->ft_addr, 0, 0);
Lukas Auerc4a6c8f2019-03-17 19:28:38 +0100100 if (ret)
101 hang();
102#endif
Bin Mengfda01b62018-12-12 06:12:46 -0800103 kernel(gd->arch.boot_hart, images->ft_addr);
Lukas Auerc4a6c8f2019-03-17 19:28:38 +0100104 }
Lukas Auer86feab32018-11-22 11:26:32 +0100105 }
106}
107
108int do_bootm_linux(int flag, int argc, char * const argv[],
109 bootm_headers_t *images)
110{
111 /* No need for those on RISC-V */
112 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
113 return -1;
Rick Chen6eedd922017-12-26 13:55:49 +0800114
Lukas Auer86feab32018-11-22 11:26:32 +0100115 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 Chen6eedd922017-12-26 13:55:49 +0800128}
Bin Menge63488f2018-12-21 07:13:41 -0800129
130int do_bootm_vxworks(int flag, int argc, char * const argv[],
131 bootm_headers_t *images)
132{
133 return do_bootm_linux(flag, argc, argv, images);
134}