blob: 378aad0c9c4566819c91595d62ccafa72b757b05 [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>
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090017
Masahiro Yamadafb092032017-02-14 01:24:26 +090018#include "init.h"
Masahiro Yamada5fe0e332016-02-02 21:11:31 +090019
Masahiro Yamadabdd79552019-07-10 20:07:47 +090020static void uniphier_set_env_fdt_file(void)
Masahiro Yamadabf6e3fc2015-12-17 18:00:37 +090021{
22 DECLARE_GLOBAL_DATA_PTR;
Masahiro Yamada8e3e0fe2016-04-21 14:43:17 +090023 const char *compat;
24 char dtb_name[256];
Masahiro Yamadaadb349d2016-10-17 22:18:02 +090025 int buf_len = sizeof(dtb_name);
Masahiro Yamadabdd79552019-07-10 20:07:47 +090026 int ret;
Masahiro Yamadabf6e3fc2015-12-17 18:00:37 +090027
Masahiro Yamada018f2fb2018-05-17 19:55:20 +090028 if (env_get("fdtfile"))
Masahiro Yamadabdd79552019-07-10 20:07:47 +090029 return; /* do nothing if it is already set */
Masahiro Yamadaafbde6f2016-06-07 21:03:44 +090030
Simon Glassb0ea7402016-10-02 17:59:28 -060031 compat = fdt_stringlist_get(gd->fdt_blob, 0, "compatible", 0, NULL);
32 if (!compat)
Masahiro Yamadabdd79552019-07-10 20:07:47 +090033 goto fail;
Masahiro Yamada8e3e0fe2016-04-21 14:43:17 +090034
Masahiro Yamadaadb349d2016-10-17 22:18:02 +090035 /* rip off the vendor prefix "socionext," */
36 compat = strchr(compat, ',');
37 if (!compat)
Masahiro Yamadabdd79552019-07-10 20:07:47 +090038 goto fail;
Masahiro Yamadaadb349d2016-10-17 22:18:02 +090039 compat++;
Masahiro Yamada8e3e0fe2016-04-21 14:43:17 +090040
Masahiro Yamadaadb349d2016-10-17 22:18:02 +090041 strncpy(dtb_name, compat, buf_len);
Masahiro Yamada8e3e0fe2016-04-21 14:43:17 +090042 buf_len -= strlen(compat);
43
44 strncat(dtb_name, ".dtb", buf_len);
45
Masahiro Yamadabdd79552019-07-10 20:07:47 +090046 ret = env_set("fdtfile", dtb_name);
47 if (ret)
48 goto fail;
49
50 return;
51fail:
52 pr_warn("\"fdt_file\" environment variable was not set correctly\n");
Masahiro Yamadabf6e3fc2015-12-17 18:00:37 +090053}
54
Masahiro Yamada1fedafb2019-07-10 20:07:48 +090055static void uniphier_set_env_addr(const char *env, const char *offset_env)
56{
57 unsigned long offset = 0;
58 const char *str;
59 char *end;
60 int ret;
61
62 if (env_get(env))
63 return; /* do nothing if it is already set */
64
65 if (offset_env) {
66 str = env_get(offset_env);
67 if (!str)
68 goto fail;
69
70 offset = simple_strtoul(str, &end, 16);
71 if (*end)
72 goto fail;
73 }
74
75 ret = env_set_hex(env, gd->ram_base + offset);
76 if (ret)
77 goto fail;
78
79 return;
80
81fail:
82 pr_warn("\"%s\" environment variable was not set correctly\n", env);
83}
84
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090085int board_late_init(void)
86{
87 puts("MODE: ");
88
Masahiro Yamadafb092032017-02-14 01:24:26 +090089 switch (uniphier_boot_device_raw()) {
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090090 case BOOT_DEVICE_MMC1:
Masahiro Yamadaaf198102017-04-20 16:54:43 +090091 printf("eMMC Boot");
Masahiro Yamada93270eb2018-12-19 20:03:13 +090092 env_set("bootdev", "emmc");
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090093 break;
Masahiro Yamadac85f81b2019-07-10 20:07:39 +090094 case BOOT_DEVICE_MMC2:
95 printf("SD Boot");
96 env_set("bootdev", "sd");
97 break;
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090098 case BOOT_DEVICE_NAND:
Masahiro Yamadaaf198102017-04-20 16:54:43 +090099 printf("NAND Boot");
Masahiro Yamada93270eb2018-12-19 20:03:13 +0900100 env_set("bootdev", "nand");
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900101 break;
102 case BOOT_DEVICE_NOR:
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900103 printf("NOR Boot");
Masahiro Yamada93270eb2018-12-19 20:03:13 +0900104 env_set("bootdev", "nor");
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900105 break;
Masahiro Yamada5fe0e332016-02-02 21:11:31 +0900106 case BOOT_DEVICE_USB:
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900107 printf("USB Boot");
Masahiro Yamada93270eb2018-12-19 20:03:13 +0900108 env_set("bootdev", "usb");
Masahiro Yamada5fe0e332016-02-02 21:11:31 +0900109 break;
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900110 default:
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900111 printf("Unknown");
Masahiro Yamada5572b0a2016-04-21 14:43:16 +0900112 break;
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900113 }
114
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900115 if (uniphier_have_internal_stm())
116 printf(" (STM: %s)",
117 uniphier_boot_from_backend() ? "OFF" : "ON");
118
119 printf("\n");
120
Masahiro Yamadabdd79552019-07-10 20:07:47 +0900121 uniphier_set_env_fdt_file();
Masahiro Yamadabf6e3fc2015-12-17 18:00:37 +0900122
Masahiro Yamadaa74f0c32019-07-10 20:07:49 +0900123 uniphier_set_env_addr("dram_base", NULL);
124
Masahiro Yamada1fedafb2019-07-10 20:07:48 +0900125 uniphier_set_env_addr("loadaddr", "loadaddr_offset");
126
Masahiro Yamadaa4677b12019-07-10 20:07:50 +0900127 uniphier_set_env_addr("kernel_addr_r", "kernel_addr_r_offset");
128 uniphier_set_env_addr("ramdisk_addr_r", "ramdisk_addr_r_offset");
129 uniphier_set_env_addr("fdt_addr_r", "fdt_addr_r_offset");
130
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900131 return 0;
132}