common: Move device-tree setup functions to fdt_support.h
[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 <command.h>
11 #include <dm.h>
12 #include <fdt_support.h>
13 #include <dm/root.h>
14 #include <image.h>
15 #include <asm/byteorder.h>
16 #include <asm/csr.h>
17 #include <asm/smp.h>
18 #include <dm/device.h>
19 #include <dm/root.h>
20 #include <u-boot/zlib.h>
21
22 DECLARE_GLOBAL_DATA_PTR;
23
24 __weak void board_quiesce_devices(void)
25 {
26 }
27
28 int arch_fixup_fdt(void *blob)
29 {
30         return 0;
31 }
32
33 /**
34  * announce_and_cleanup() - Print message and prepare for kernel boot
35  *
36  * @fake: non-zero to do everything except actually boot
37  */
38 static void announce_and_cleanup(int fake)
39 {
40         printf("\nStarting kernel ...%s\n\n", fake ?
41                 "(fake run for tracing)" : "");
42         bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
43 #ifdef CONFIG_BOOTSTAGE_FDT
44         bootstage_fdt_add_report();
45 #endif
46 #ifdef CONFIG_BOOTSTAGE_REPORT
47         bootstage_report();
48 #endif
49
50 #ifdef CONFIG_USB_DEVICE
51         udc_disconnect();
52 #endif
53
54         board_quiesce_devices();
55
56         /*
57          * Call remove function of all devices with a removal flag set.
58          * This may be useful for last-stage operations, like cancelling
59          * of DMA operation or releasing device internal buffers.
60          */
61         dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
62
63         cleanup_before_linux();
64 }
65
66 static void boot_prep_linux(bootm_headers_t *images)
67 {
68         if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
69 #ifdef CONFIG_OF_LIBFDT
70                 debug("using: FDT\n");
71                 if (image_setup_linux(images)) {
72                         printf("FDT creation failed! hanging...");
73                         hang();
74                 }
75 #endif
76         } else {
77                 printf("Device tree not found or missing FDT support\n");
78                 hang();
79         }
80 }
81
82 static void boot_jump_linux(bootm_headers_t *images, int flag)
83 {
84         void (*kernel)(ulong hart, void *dtb);
85         int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
86 #ifdef CONFIG_SMP
87         int ret;
88 #endif
89
90         kernel = (void (*)(ulong, void *))images->ep;
91
92         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
93
94         debug("## Transferring control to kernel (at address %08lx) ...\n",
95               (ulong)kernel);
96
97         announce_and_cleanup(fake);
98
99         if (!fake) {
100                 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
101 #ifdef CONFIG_SMP
102                         ret = smp_call_function(images->ep,
103                                                 (ulong)images->ft_addr, 0, 0);
104                         if (ret)
105                                 hang();
106 #endif
107                         kernel(gd->arch.boot_hart, images->ft_addr);
108                 }
109         }
110 }
111
112 int do_bootm_linux(int flag, int argc, char * const argv[],
113                    bootm_headers_t *images)
114 {
115         /* No need for those on RISC-V */
116         if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
117                 return -1;
118
119         if (flag & BOOTM_STATE_OS_PREP) {
120                 boot_prep_linux(images);
121                 return 0;
122         }
123
124         if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
125                 boot_jump_linux(images, flag);
126                 return 0;
127         }
128
129         boot_prep_linux(images);
130         boot_jump_linux(images, flag);
131         return 0;
132 }
133
134 int do_bootm_vxworks(int flag, int argc, char * const argv[],
135                      bootm_headers_t *images)
136 {
137         return do_bootm_linux(flag, argc, argv, images);
138 }