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