Merge https://gitlab.denx.de/u-boot/custodians/u-boot-riscv
[oweals/u-boot.git] / common / spl / spl_opensbi.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2019 Fraunhofer AISEC,
4  * Lukas Auer <lukas.auer@aisec.fraunhofer.de>
5  *
6  * Based on common/spl/spl_atf.c
7  */
8 #include <common.h>
9 #include <errno.h>
10 #include <spl.h>
11 #include <asm/smp.h>
12 #include <opensbi.h>
13
14 DECLARE_GLOBAL_DATA_PTR;
15
16 struct fw_dynamic_info opensbi_info;
17
18 static int spl_opensbi_find_uboot_node(void *blob, int *uboot_node)
19 {
20         int fit_images_node, node;
21         const char *fit_os;
22
23         fit_images_node = fdt_path_offset(blob, "/fit-images");
24         if (fit_images_node < 0)
25                 return -ENODEV;
26
27         fdt_for_each_subnode(node, blob, fit_images_node) {
28                 fit_os = fdt_getprop(blob, node, FIT_OS_PROP, NULL);
29                 if (!fit_os)
30                         continue;
31
32                 if (genimg_get_os_id(fit_os) == IH_OS_U_BOOT) {
33                         *uboot_node = node;
34                         return 0;
35                 }
36         }
37
38         return -ENODEV;
39 }
40
41 void spl_invoke_opensbi(struct spl_image_info *spl_image)
42 {
43         int ret, uboot_node;
44         ulong uboot_entry;
45         void (*opensbi_entry)(ulong hartid, ulong dtb, ulong info);
46
47         if (!spl_image->fdt_addr) {
48                 pr_err("No device tree specified in SPL image\n");
49                 hang();
50         }
51
52         /* Find U-Boot image in /fit-images */
53         ret = spl_opensbi_find_uboot_node(spl_image->fdt_addr, &uboot_node);
54         if (ret) {
55                 pr_err("Can't find U-Boot node, %d", ret);
56                 hang();
57         }
58
59         /* Get U-Boot entry point */
60         uboot_entry = fdt_getprop_u32(spl_image->fdt_addr, uboot_node,
61                                       "entry-point");
62         if (uboot_entry == FDT_ERROR)
63                 uboot_entry = fdt_getprop_u32(spl_image->fdt_addr, uboot_node,
64                                               "load-addr");
65
66         /* Prepare obensbi_info object */
67         opensbi_info.magic = FW_DYNAMIC_INFO_MAGIC_VALUE;
68         opensbi_info.version = FW_DYNAMIC_INFO_VERSION;
69         opensbi_info.next_addr = uboot_entry;
70         opensbi_info.next_mode = FW_DYNAMIC_INFO_NEXT_MODE_S;
71         opensbi_info.options = SBI_SCRATCH_NO_BOOT_PRINTS;
72
73         opensbi_entry = (void (*)(ulong, ulong, ulong))spl_image->entry_point;
74         invalidate_icache_all();
75
76 #ifdef CONFIG_SMP
77         ret = smp_call_function((ulong)spl_image->entry_point,
78                                 (ulong)spl_image->fdt_addr,
79                                 (ulong)&opensbi_info);
80         if (ret)
81                 hang();
82 #endif
83         opensbi_entry(gd->arch.boot_hart, (ulong)spl_image->fdt_addr,
84                       (ulong)&opensbi_info);
85 }