ARM: uniphier: set dram_base environment variable
[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 <common.h>
9 #include <spl.h>
10 #include <linux/libfdt.h>
11 #include <nand.h>
12 #include <stdio.h>
13 #include <linux/io.h>
14 #include <linux/printk.h>
15 #include <../drivers/mtd/nand/raw/denali.h>
16
17 #include "init.h"
18
19 static void nand_denali_wp_disable(void)
20 {
21 #ifdef CONFIG_NAND_DENALI
22         /*
23          * Since the boot rom enables the write protection for NAND boot mode,
24          * it must be disabled somewhere for "nand write", "nand erase", etc.
25          * The workaround is here to not disturb the Denali NAND controller
26          * driver just for a really SoC-specific thing.
27          */
28         void __iomem *denali_reg = (void __iomem *)CONFIG_SYS_NAND_REGS_BASE;
29
30         writel(WRITE_PROTECT__FLAG, denali_reg + WRITE_PROTECT);
31 #endif
32 }
33
34 static void uniphier_set_env_fdt_file(void)
35 {
36         DECLARE_GLOBAL_DATA_PTR;
37         const char *compat;
38         char dtb_name[256];
39         int buf_len = sizeof(dtb_name);
40         int ret;
41
42         if (env_get("fdtfile"))
43                 return;         /* do nothing if it is already set */
44
45         compat = fdt_stringlist_get(gd->fdt_blob, 0, "compatible", 0, NULL);
46         if (!compat)
47                 goto fail;
48
49         /* rip off the vendor prefix "socionext,"  */
50         compat = strchr(compat, ',');
51         if (!compat)
52                 goto fail;
53         compat++;
54
55         strncpy(dtb_name, compat, buf_len);
56         buf_len -= strlen(compat);
57
58         strncat(dtb_name, ".dtb", buf_len);
59
60         ret = env_set("fdtfile", dtb_name);
61         if (ret)
62                 goto fail;
63
64         return;
65 fail:
66         pr_warn("\"fdt_file\" environment variable was not set correctly\n");
67 }
68
69 static void uniphier_set_env_addr(const char *env, const char *offset_env)
70 {
71         unsigned long offset = 0;
72         const char *str;
73         char *end;
74         int ret;
75
76         if (env_get(env))
77                 return;         /* do nothing if it is already set */
78
79         if (offset_env) {
80                 str = env_get(offset_env);
81                 if (!str)
82                         goto fail;
83
84                 offset = simple_strtoul(str, &end, 16);
85                 if (*end)
86                         goto fail;
87         }
88
89         ret = env_set_hex(env, gd->ram_base + offset);
90         if (ret)
91                 goto fail;
92
93         return;
94
95 fail:
96         pr_warn("\"%s\" environment variable was not set correctly\n", env);
97 }
98
99 int board_late_init(void)
100 {
101         puts("MODE:  ");
102
103         switch (uniphier_boot_device_raw()) {
104         case BOOT_DEVICE_MMC1:
105                 printf("eMMC Boot");
106                 env_set("bootdev", "emmc");
107                 break;
108         case BOOT_DEVICE_MMC2:
109                 printf("SD Boot");
110                 env_set("bootdev", "sd");
111                 break;
112         case BOOT_DEVICE_NAND:
113                 printf("NAND Boot");
114                 env_set("bootdev", "nand");
115                 nand_denali_wp_disable();
116                 break;
117         case BOOT_DEVICE_NOR:
118                 printf("NOR Boot");
119                 env_set("bootdev", "nor");
120                 break;
121         case BOOT_DEVICE_USB:
122                 printf("USB Boot");
123                 env_set("bootdev", "usb");
124                 break;
125         default:
126                 printf("Unknown");
127                 break;
128         }
129
130         if (uniphier_have_internal_stm())
131                 printf(" (STM: %s)",
132                        uniphier_boot_from_backend() ? "OFF" : "ON");
133
134         printf("\n");
135
136         uniphier_set_env_fdt_file();
137
138         uniphier_set_env_addr("dram_base", NULL);
139
140         uniphier_set_env_addr("loadaddr", "loadaddr_offset");
141
142         return 0;
143 }