blob: 14b61fc7dfda80c3e2502258ba07fda04eac8aaa [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamada5894ca02014-10-03 19:21:06 +09002/*
Masahiro Yamada40749d52016-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 Yamada5894ca02014-10-03 19:21:06 +09006 */
7
8#include <common.h>
Simon Glassc7694dd2019-08-01 09:46:46 -06009#include <env.h>
Masahiro Yamada5894ca02014-10-03 19:21:06 +090010#include <spl.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090011#include <linux/libfdt.h>
Masahiro Yamada5894ca02014-10-03 19:21:06 +090012#include <nand.h>
Masahiro Yamadadd74b942017-10-13 19:21:55 +090013#include <stdio.h>
Masahiro Yamadaf6e7f072015-05-29 17:30:00 +090014#include <linux/io.h>
Masahiro Yamadadd74b942017-10-13 19:21:55 +090015#include <linux/printk.h>
Miquel Raynala430fa02018-08-16 17:30:07 +020016#include <../drivers/mtd/nand/raw/denali.h>
Masahiro Yamada5894ca02014-10-03 19:21:06 +090017
Masahiro Yamada784548e2017-02-14 01:24:26 +090018#include "init.h"
Masahiro Yamadafec48162016-02-02 21:11:31 +090019
Masahiro Yamada5894ca02014-10-03 19:21:06 +090020static void nand_denali_wp_disable(void)
21{
22#ifdef CONFIG_NAND_DENALI
23 /*
24 * Since the boot rom enables the write protection for NAND boot mode,
25 * it must be disabled somewhere for "nand write", "nand erase", etc.
26 * The workaround is here to not disturb the Denali NAND controller
27 * driver just for a really SoC-specific thing.
28 */
29 void __iomem *denali_reg = (void __iomem *)CONFIG_SYS_NAND_REGS_BASE;
30
31 writel(WRITE_PROTECT__FLAG, denali_reg + WRITE_PROTECT);
32#endif
33}
34
Masahiro Yamada72cd83a2019-07-10 20:07:47 +090035static void uniphier_set_env_fdt_file(void)
Masahiro Yamada8ea4f492015-12-17 18:00:37 +090036{
37 DECLARE_GLOBAL_DATA_PTR;
Masahiro Yamada881aa5a2016-04-21 14:43:17 +090038 const char *compat;
39 char dtb_name[256];
Masahiro Yamada40749d52016-10-17 22:18:02 +090040 int buf_len = sizeof(dtb_name);
Masahiro Yamada72cd83a2019-07-10 20:07:47 +090041 int ret;
Masahiro Yamada8ea4f492015-12-17 18:00:37 +090042
Masahiro Yamada8c09f1f2018-05-17 19:55:20 +090043 if (env_get("fdtfile"))
Masahiro Yamada72cd83a2019-07-10 20:07:47 +090044 return; /* do nothing if it is already set */
Masahiro Yamada4565a742016-06-07 21:03:44 +090045
Simon Glassb02e4042016-10-02 17:59:28 -060046 compat = fdt_stringlist_get(gd->fdt_blob, 0, "compatible", 0, NULL);
47 if (!compat)
Masahiro Yamada72cd83a2019-07-10 20:07:47 +090048 goto fail;
Masahiro Yamada881aa5a2016-04-21 14:43:17 +090049
Masahiro Yamada40749d52016-10-17 22:18:02 +090050 /* rip off the vendor prefix "socionext," */
51 compat = strchr(compat, ',');
52 if (!compat)
Masahiro Yamada72cd83a2019-07-10 20:07:47 +090053 goto fail;
Masahiro Yamada40749d52016-10-17 22:18:02 +090054 compat++;
Masahiro Yamada881aa5a2016-04-21 14:43:17 +090055
Masahiro Yamada40749d52016-10-17 22:18:02 +090056 strncpy(dtb_name, compat, buf_len);
Masahiro Yamada881aa5a2016-04-21 14:43:17 +090057 buf_len -= strlen(compat);
58
59 strncat(dtb_name, ".dtb", buf_len);
60
Masahiro Yamada72cd83a2019-07-10 20:07:47 +090061 ret = env_set("fdtfile", dtb_name);
62 if (ret)
63 goto fail;
64
65 return;
66fail:
67 pr_warn("\"fdt_file\" environment variable was not set correctly\n");
Masahiro Yamada8ea4f492015-12-17 18:00:37 +090068}
69
Masahiro Yamada3cc936d2019-07-10 20:07:48 +090070static void uniphier_set_env_addr(const char *env, const char *offset_env)
71{
72 unsigned long offset = 0;
73 const char *str;
74 char *end;
75 int ret;
76
77 if (env_get(env))
78 return; /* do nothing if it is already set */
79
80 if (offset_env) {
81 str = env_get(offset_env);
82 if (!str)
83 goto fail;
84
85 offset = simple_strtoul(str, &end, 16);
86 if (*end)
87 goto fail;
88 }
89
90 ret = env_set_hex(env, gd->ram_base + offset);
91 if (ret)
92 goto fail;
93
94 return;
95
96fail:
97 pr_warn("\"%s\" environment variable was not set correctly\n", env);
98}
99
Masahiro Yamada5894ca02014-10-03 19:21:06 +0900100int board_late_init(void)
101{
102 puts("MODE: ");
103
Masahiro Yamada784548e2017-02-14 01:24:26 +0900104 switch (uniphier_boot_device_raw()) {
Masahiro Yamada5894ca02014-10-03 19:21:06 +0900105 case BOOT_DEVICE_MMC1:
Masahiro Yamada63754842017-04-20 16:54:43 +0900106 printf("eMMC Boot");
Masahiro Yamada6bc50a82018-12-19 20:03:13 +0900107 env_set("bootdev", "emmc");
Masahiro Yamada5894ca02014-10-03 19:21:06 +0900108 break;
Masahiro Yamadac3d8f1e2019-07-10 20:07:39 +0900109 case BOOT_DEVICE_MMC2:
110 printf("SD Boot");
111 env_set("bootdev", "sd");
112 break;
Masahiro Yamada5894ca02014-10-03 19:21:06 +0900113 case BOOT_DEVICE_NAND:
Masahiro Yamada63754842017-04-20 16:54:43 +0900114 printf("NAND Boot");
Masahiro Yamada6bc50a82018-12-19 20:03:13 +0900115 env_set("bootdev", "nand");
Masahiro Yamada5894ca02014-10-03 19:21:06 +0900116 nand_denali_wp_disable();
117 break;
118 case BOOT_DEVICE_NOR:
Masahiro Yamada63754842017-04-20 16:54:43 +0900119 printf("NOR Boot");
Masahiro Yamada6bc50a82018-12-19 20:03:13 +0900120 env_set("bootdev", "nor");
Masahiro Yamada5894ca02014-10-03 19:21:06 +0900121 break;
Masahiro Yamadafec48162016-02-02 21:11:31 +0900122 case BOOT_DEVICE_USB:
Masahiro Yamada63754842017-04-20 16:54:43 +0900123 printf("USB Boot");
Masahiro Yamada6bc50a82018-12-19 20:03:13 +0900124 env_set("bootdev", "usb");
Masahiro Yamadafec48162016-02-02 21:11:31 +0900125 break;
Masahiro Yamada5894ca02014-10-03 19:21:06 +0900126 default:
Masahiro Yamada63754842017-04-20 16:54:43 +0900127 printf("Unknown");
Masahiro Yamadad90b9742016-04-21 14:43:16 +0900128 break;
Masahiro Yamada5894ca02014-10-03 19:21:06 +0900129 }
130
Masahiro Yamada63754842017-04-20 16:54:43 +0900131 if (uniphier_have_internal_stm())
132 printf(" (STM: %s)",
133 uniphier_boot_from_backend() ? "OFF" : "ON");
134
135 printf("\n");
136
Masahiro Yamada72cd83a2019-07-10 20:07:47 +0900137 uniphier_set_env_fdt_file();
Masahiro Yamada8ea4f492015-12-17 18:00:37 +0900138
Masahiro Yamada70956782019-07-10 20:07:49 +0900139 uniphier_set_env_addr("dram_base", NULL);
140
Masahiro Yamada3cc936d2019-07-10 20:07:48 +0900141 uniphier_set_env_addr("loadaddr", "loadaddr_offset");
142
Masahiro Yamada2ce6b822019-07-10 20:07:50 +0900143 uniphier_set_env_addr("kernel_addr_r", "kernel_addr_r_offset");
144 uniphier_set_env_addr("ramdisk_addr_r", "ramdisk_addr_r_offset");
145 uniphier_set_env_addr("fdt_addr_r", "fdt_addr_r_offset");
146
Masahiro Yamada5894ca02014-10-03 19:21:06 +0900147 return 0;
148}