blob: 793283058c359c1872e04339b69dc91d67f5a6cc [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +09002/*
Masahiro Yamadaadb349d2016-10-17 22:18:02 +09003 * Copyright (C) 2014 Panasonic Corporation
4 * Copyright (C) 2015-2016 Socionext Inc.
5 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +09006 */
7
8#include <common.h>
Simon Glass313112a2019-08-01 09:46:46 -06009#include <env.h>
Simon Glassa7b51302019-11-14 12:57:46 -070010#include <init.h>
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090011#include <spl.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090012#include <linux/libfdt.h>
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090013#include <nand.h>
Masahiro Yamada609cd532017-10-13 19:21:55 +090014#include <stdio.h>
Masahiro Yamada663a23f2015-05-29 17:30:00 +090015#include <linux/io.h>
Masahiro Yamada609cd532017-10-13 19:21:55 +090016#include <linux/printk.h>
Miquel Raynal1f1ae152018-08-16 17:30:07 +020017#include <../drivers/mtd/nand/raw/denali.h>
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090018
Masahiro Yamadafb092032017-02-14 01:24:26 +090019#include "init.h"
Masahiro Yamada5fe0e332016-02-02 21:11:31 +090020
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090021static 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
Masahiro Yamadabdd79552019-07-10 20:07:47 +090036static void uniphier_set_env_fdt_file(void)
Masahiro Yamadabf6e3fc2015-12-17 18:00:37 +090037{
38 DECLARE_GLOBAL_DATA_PTR;
Masahiro Yamada8e3e0fe2016-04-21 14:43:17 +090039 const char *compat;
40 char dtb_name[256];
Masahiro Yamadaadb349d2016-10-17 22:18:02 +090041 int buf_len = sizeof(dtb_name);
Masahiro Yamadabdd79552019-07-10 20:07:47 +090042 int ret;
Masahiro Yamadabf6e3fc2015-12-17 18:00:37 +090043
Masahiro Yamada018f2fb2018-05-17 19:55:20 +090044 if (env_get("fdtfile"))
Masahiro Yamadabdd79552019-07-10 20:07:47 +090045 return; /* do nothing if it is already set */
Masahiro Yamadaafbde6f2016-06-07 21:03:44 +090046
Simon Glassb0ea7402016-10-02 17:59:28 -060047 compat = fdt_stringlist_get(gd->fdt_blob, 0, "compatible", 0, NULL);
48 if (!compat)
Masahiro Yamadabdd79552019-07-10 20:07:47 +090049 goto fail;
Masahiro Yamada8e3e0fe2016-04-21 14:43:17 +090050
Masahiro Yamadaadb349d2016-10-17 22:18:02 +090051 /* rip off the vendor prefix "socionext," */
52 compat = strchr(compat, ',');
53 if (!compat)
Masahiro Yamadabdd79552019-07-10 20:07:47 +090054 goto fail;
Masahiro Yamadaadb349d2016-10-17 22:18:02 +090055 compat++;
Masahiro Yamada8e3e0fe2016-04-21 14:43:17 +090056
Masahiro Yamadaadb349d2016-10-17 22:18:02 +090057 strncpy(dtb_name, compat, buf_len);
Masahiro Yamada8e3e0fe2016-04-21 14:43:17 +090058 buf_len -= strlen(compat);
59
60 strncat(dtb_name, ".dtb", buf_len);
61
Masahiro Yamadabdd79552019-07-10 20:07:47 +090062 ret = env_set("fdtfile", dtb_name);
63 if (ret)
64 goto fail;
65
66 return;
67fail:
68 pr_warn("\"fdt_file\" environment variable was not set correctly\n");
Masahiro Yamadabf6e3fc2015-12-17 18:00:37 +090069}
70
Masahiro Yamada1fedafb2019-07-10 20:07:48 +090071static 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
97fail:
98 pr_warn("\"%s\" environment variable was not set correctly\n", env);
99}
100
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900101int board_late_init(void)
102{
103 puts("MODE: ");
104
Masahiro Yamadafb092032017-02-14 01:24:26 +0900105 switch (uniphier_boot_device_raw()) {
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900106 case BOOT_DEVICE_MMC1:
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900107 printf("eMMC Boot");
Masahiro Yamada93270eb2018-12-19 20:03:13 +0900108 env_set("bootdev", "emmc");
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900109 break;
Masahiro Yamadac85f81b2019-07-10 20:07:39 +0900110 case BOOT_DEVICE_MMC2:
111 printf("SD Boot");
112 env_set("bootdev", "sd");
113 break;
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900114 case BOOT_DEVICE_NAND:
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900115 printf("NAND Boot");
Masahiro Yamada93270eb2018-12-19 20:03:13 +0900116 env_set("bootdev", "nand");
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900117 nand_denali_wp_disable();
118 break;
119 case BOOT_DEVICE_NOR:
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900120 printf("NOR Boot");
Masahiro Yamada93270eb2018-12-19 20:03:13 +0900121 env_set("bootdev", "nor");
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900122 break;
Masahiro Yamada5fe0e332016-02-02 21:11:31 +0900123 case BOOT_DEVICE_USB:
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900124 printf("USB Boot");
Masahiro Yamada93270eb2018-12-19 20:03:13 +0900125 env_set("bootdev", "usb");
Masahiro Yamada5fe0e332016-02-02 21:11:31 +0900126 break;
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900127 default:
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900128 printf("Unknown");
Masahiro Yamada5572b0a2016-04-21 14:43:16 +0900129 break;
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900130 }
131
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900132 if (uniphier_have_internal_stm())
133 printf(" (STM: %s)",
134 uniphier_boot_from_backend() ? "OFF" : "ON");
135
136 printf("\n");
137
Masahiro Yamadabdd79552019-07-10 20:07:47 +0900138 uniphier_set_env_fdt_file();
Masahiro Yamadabf6e3fc2015-12-17 18:00:37 +0900139
Masahiro Yamadaa74f0c32019-07-10 20:07:49 +0900140 uniphier_set_env_addr("dram_base", NULL);
141
Masahiro Yamada1fedafb2019-07-10 20:07:48 +0900142 uniphier_set_env_addr("loadaddr", "loadaddr_offset");
143
Masahiro Yamadaa4677b12019-07-10 20:07:50 +0900144 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
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900148 return 0;
149}