Merge tag 'u-boot-atmel-fixes-2020.07-a' of https://gitlab.denx.de/u-boot/custodians...
[oweals/u-boot.git] / arch / riscv / lib / bootm.c
1 // SPDX-License-Identifier: GPL-2.0+
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>
7  */
8
9 #include <common.h>
10 #include <bootstage.h>
11 #include <command.h>
12 #include <dm.h>
13 #include <fdt_support.h>
14 #include <hang.h>
15 #include <log.h>
16 #include <dm/root.h>
17 #include <image.h>
18 #include <asm/byteorder.h>
19 #include <asm/csr.h>
20 #include <asm/smp.h>
21 #include <dm/device.h>
22 #include <dm/root.h>
23 #include <u-boot/zlib.h>
24
25 DECLARE_GLOBAL_DATA_PTR;
26
27 __weak void board_quiesce_devices(void)
28 {
29 }
30
31 /**
32  * announce_and_cleanup() - Print message and prepare for kernel boot
33  *
34  * @fake: non-zero to do everything except actually boot
35  */
36 static void announce_and_cleanup(int fake)
37 {
38         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
47
48 #ifdef CONFIG_USB_DEVICE
49         udc_disconnect();
50 #endif
51
52         board_quiesce_devices();
53
54         /*
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);
60
61         cleanup_before_linux();
62 }
63
64 static void boot_prep_linux(bootm_headers_t *images)
65 {
66         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
74         } else {
75                 printf("Device tree not found or missing FDT support\n");
76                 hang();
77         }
78 }
79
80 static 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);
84 #ifdef CONFIG_SMP
85         int ret;
86 #endif
87
88         kernel = (void (*)(ulong, void *))images->ep;
89
90         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
91
92         debug("## Transferring control to kernel (at address %08lx) ...\n",
93               (ulong)kernel);
94
95         announce_and_cleanup(fake);
96
97         if (!fake) {
98                 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
99 #ifdef CONFIG_SMP
100                         ret = smp_call_function(images->ep,
101                                                 (ulong)images->ft_addr, 0, 0);
102                         if (ret)
103                                 hang();
104 #endif
105                         kernel(gd->arch.boot_hart, images->ft_addr);
106                 }
107         }
108 }
109
110 int do_bootm_linux(int flag, int argc, char *const argv[],
111                    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;
116
117         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;
130 }
131
132 int do_bootm_vxworks(int flag, int argc, char *const argv[],
133                      bootm_headers_t *images)
134 {
135         return do_bootm_linux(flag, argc, argv, images);
136 }