blob: 3180b24330b4d788734c90cf90ee977b71e61cc1 [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>
9#include <spl.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090010#include <linux/libfdt.h>
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090011#include <nand.h>
Masahiro Yamada609cd532017-10-13 19:21:55 +090012#include <stdio.h>
Masahiro Yamada663a23f2015-05-29 17:30:00 +090013#include <linux/io.h>
Masahiro Yamada609cd532017-10-13 19:21:55 +090014#include <linux/printk.h>
Miquel Raynal1f1ae152018-08-16 17:30:07 +020015#include <../drivers/mtd/nand/raw/denali.h>
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090016
Masahiro Yamadafb092032017-02-14 01:24:26 +090017#include "init.h"
Masahiro Yamada5fe0e332016-02-02 21:11:31 +090018
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090019static 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
Masahiro Yamadabdd79552019-07-10 20:07:47 +090034static void uniphier_set_env_fdt_file(void)
Masahiro Yamadabf6e3fc2015-12-17 18:00:37 +090035{
36 DECLARE_GLOBAL_DATA_PTR;
Masahiro Yamada8e3e0fe2016-04-21 14:43:17 +090037 const char *compat;
38 char dtb_name[256];
Masahiro Yamadaadb349d2016-10-17 22:18:02 +090039 int buf_len = sizeof(dtb_name);
Masahiro Yamadabdd79552019-07-10 20:07:47 +090040 int ret;
Masahiro Yamadabf6e3fc2015-12-17 18:00:37 +090041
Masahiro Yamada018f2fb2018-05-17 19:55:20 +090042 if (env_get("fdtfile"))
Masahiro Yamadabdd79552019-07-10 20:07:47 +090043 return; /* do nothing if it is already set */
Masahiro Yamadaafbde6f2016-06-07 21:03:44 +090044
Simon Glassb0ea7402016-10-02 17:59:28 -060045 compat = fdt_stringlist_get(gd->fdt_blob, 0, "compatible", 0, NULL);
46 if (!compat)
Masahiro Yamadabdd79552019-07-10 20:07:47 +090047 goto fail;
Masahiro Yamada8e3e0fe2016-04-21 14:43:17 +090048
Masahiro Yamadaadb349d2016-10-17 22:18:02 +090049 /* rip off the vendor prefix "socionext," */
50 compat = strchr(compat, ',');
51 if (!compat)
Masahiro Yamadabdd79552019-07-10 20:07:47 +090052 goto fail;
Masahiro Yamadaadb349d2016-10-17 22:18:02 +090053 compat++;
Masahiro Yamada8e3e0fe2016-04-21 14:43:17 +090054
Masahiro Yamadaadb349d2016-10-17 22:18:02 +090055 strncpy(dtb_name, compat, buf_len);
Masahiro Yamada8e3e0fe2016-04-21 14:43:17 +090056 buf_len -= strlen(compat);
57
58 strncat(dtb_name, ".dtb", buf_len);
59
Masahiro Yamadabdd79552019-07-10 20:07:47 +090060 ret = env_set("fdtfile", dtb_name);
61 if (ret)
62 goto fail;
63
64 return;
65fail:
66 pr_warn("\"fdt_file\" environment variable was not set correctly\n");
Masahiro Yamadabf6e3fc2015-12-17 18:00:37 +090067}
68
Masahiro Yamada1fedafb2019-07-10 20:07:48 +090069static 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
95fail:
96 pr_warn("\"%s\" environment variable was not set correctly\n", env);
97}
98
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090099int board_late_init(void)
100{
101 puts("MODE: ");
102
Masahiro Yamadafb092032017-02-14 01:24:26 +0900103 switch (uniphier_boot_device_raw()) {
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900104 case BOOT_DEVICE_MMC1:
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900105 printf("eMMC Boot");
Masahiro Yamada93270eb2018-12-19 20:03:13 +0900106 env_set("bootdev", "emmc");
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900107 break;
Masahiro Yamadac85f81b2019-07-10 20:07:39 +0900108 case BOOT_DEVICE_MMC2:
109 printf("SD Boot");
110 env_set("bootdev", "sd");
111 break;
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900112 case BOOT_DEVICE_NAND:
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900113 printf("NAND Boot");
Masahiro Yamada93270eb2018-12-19 20:03:13 +0900114 env_set("bootdev", "nand");
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900115 nand_denali_wp_disable();
116 break;
117 case BOOT_DEVICE_NOR:
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900118 printf("NOR Boot");
Masahiro Yamada93270eb2018-12-19 20:03:13 +0900119 env_set("bootdev", "nor");
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900120 break;
Masahiro Yamada5fe0e332016-02-02 21:11:31 +0900121 case BOOT_DEVICE_USB:
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900122 printf("USB Boot");
Masahiro Yamada93270eb2018-12-19 20:03:13 +0900123 env_set("bootdev", "usb");
Masahiro Yamada5fe0e332016-02-02 21:11:31 +0900124 break;
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900125 default:
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900126 printf("Unknown");
Masahiro Yamada5572b0a2016-04-21 14:43:16 +0900127 break;
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900128 }
129
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900130 if (uniphier_have_internal_stm())
131 printf(" (STM: %s)",
132 uniphier_boot_from_backend() ? "OFF" : "ON");
133
134 printf("\n");
135
Masahiro Yamadabdd79552019-07-10 20:07:47 +0900136 uniphier_set_env_fdt_file();
Masahiro Yamadabf6e3fc2015-12-17 18:00:37 +0900137
Masahiro Yamadaa74f0c32019-07-10 20:07:49 +0900138 uniphier_set_env_addr("dram_base", NULL);
139
Masahiro Yamada1fedafb2019-07-10 20:07:48 +0900140 uniphier_set_env_addr("loadaddr", "loadaddr_offset");
141
Masahiro Yamadaa4677b12019-07-10 20:07:50 +0900142 uniphier_set_env_addr("kernel_addr_r", "kernel_addr_r_offset");
143 uniphier_set_env_addr("ramdisk_addr_r", "ramdisk_addr_r_offset");
144 uniphier_set_env_addr("fdt_addr_r", "fdt_addr_r_offset");
145
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900146 return 0;
147}