Merge tag 'efi-2020-07-rc6' of https://gitlab.denx.de/u-boot/custodians/u-boot-efi
[oweals/u-boot.git] / arch / arm / mach-uniphier / board_late_init.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2014      Panasonic Corporation
4  * Copyright (C) 2015-2016 Socionext Inc.
5  *   Author: Masahiro Yamada <yamada.masahiro@socionext.com>
6  */
7
8 #include <env.h>
9 #include <init.h>
10 #include <spl.h>
11 #include <linux/libfdt.h>
12 #include <nand.h>
13 #include <stdio.h>
14 #include <linux/io.h>
15 #include <linux/printk.h>
16
17 #include "init.h"
18
19 static void uniphier_set_env_fdt_file(void)
20 {
21         DECLARE_GLOBAL_DATA_PTR;
22         const char *compat;
23         char dtb_name[256];
24         int buf_len = sizeof(dtb_name);
25         int ret;
26
27         if (env_get("fdtfile"))
28                 return;         /* do nothing if it is already set */
29
30         compat = fdt_stringlist_get(gd->fdt_blob, 0, "compatible", 0, NULL);
31         if (!compat)
32                 goto fail;
33
34         /* rip off the vendor prefix "socionext,"  */
35         compat = strchr(compat, ',');
36         if (!compat)
37                 goto fail;
38         compat++;
39
40         strncpy(dtb_name, compat, buf_len);
41         buf_len -= strlen(compat);
42
43         strncat(dtb_name, ".dtb", buf_len);
44
45         ret = env_set("fdtfile", dtb_name);
46         if (ret)
47                 goto fail;
48
49         return;
50 fail:
51         pr_warn("\"fdt_file\" environment variable was not set correctly\n");
52 }
53
54 static void uniphier_set_env_addr(const char *env, const char *offset_env)
55 {
56         unsigned long offset = 0;
57         const char *str;
58         char *end;
59         int ret;
60
61         if (env_get(env))
62                 return;         /* do nothing if it is already set */
63
64         if (offset_env) {
65                 str = env_get(offset_env);
66                 if (!str)
67                         goto fail;
68
69                 offset = simple_strtoul(str, &end, 16);
70                 if (*end)
71                         goto fail;
72         }
73
74         ret = env_set_hex(env, gd->ram_base + offset);
75         if (ret)
76                 goto fail;
77
78         return;
79
80 fail:
81         pr_warn("\"%s\" environment variable was not set correctly\n", env);
82 }
83
84 int board_late_init(void)
85 {
86         puts("MODE:  ");
87
88         switch (uniphier_boot_device_raw()) {
89         case BOOT_DEVICE_MMC1:
90                 printf("eMMC Boot");
91                 env_set("bootdev", "emmc");
92                 break;
93         case BOOT_DEVICE_MMC2:
94                 printf("SD Boot");
95                 env_set("bootdev", "sd");
96                 break;
97         case BOOT_DEVICE_NAND:
98                 printf("NAND Boot");
99                 env_set("bootdev", "nand");
100                 break;
101         case BOOT_DEVICE_NOR:
102                 printf("NOR Boot");
103                 env_set("bootdev", "nor");
104                 break;
105         case BOOT_DEVICE_USB:
106                 printf("USB Boot");
107                 env_set("bootdev", "usb");
108                 break;
109         default:
110                 printf("Unknown");
111                 break;
112         }
113
114         if (uniphier_have_internal_stm())
115                 printf(" (STM: %s)",
116                        uniphier_boot_from_backend() ? "OFF" : "ON");
117
118         printf("\n");
119
120         uniphier_set_env_fdt_file();
121
122         uniphier_set_env_addr("dram_base", NULL);
123
124         uniphier_set_env_addr("loadaddr", "loadaddr_offset");
125
126         uniphier_set_env_addr("kernel_addr_r", "kernel_addr_r_offset");
127         uniphier_set_env_addr("ramdisk_addr_r", "ramdisk_addr_r_offset");
128         uniphier_set_env_addr("fdt_addr_r", "fdt_addr_r_offset");
129
130         return 0;
131 }