blob: 8c5e5bb58f6561926060620e91e946026e531624 [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
Simon Glass313112a2019-08-01 09:46:46 -06008#include <env.h>
Simon Glassa7b51302019-11-14 12:57:46 -07009#include <init.h>
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090010#include <spl.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090011#include <linux/libfdt.h>
Masahiro Yamada609cd532017-10-13 19:21:55 +090012#include <stdio.h>
Masahiro Yamada609cd532017-10-13 19:21:55 +090013#include <linux/printk.h>
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090014
Masahiro Yamadafb092032017-02-14 01:24:26 +090015#include "init.h"
Masahiro Yamada5fe0e332016-02-02 21:11:31 +090016
Masahiro Yamadabdd79552019-07-10 20:07:47 +090017static void uniphier_set_env_fdt_file(void)
Masahiro Yamadabf6e3fc2015-12-17 18:00:37 +090018{
19 DECLARE_GLOBAL_DATA_PTR;
Masahiro Yamada8e3e0fe2016-04-21 14:43:17 +090020 const char *compat;
21 char dtb_name[256];
Masahiro Yamadaadb349d2016-10-17 22:18:02 +090022 int buf_len = sizeof(dtb_name);
Masahiro Yamadabdd79552019-07-10 20:07:47 +090023 int ret;
Masahiro Yamadabf6e3fc2015-12-17 18:00:37 +090024
Masahiro Yamada018f2fb2018-05-17 19:55:20 +090025 if (env_get("fdtfile"))
Masahiro Yamadabdd79552019-07-10 20:07:47 +090026 return; /* do nothing if it is already set */
Masahiro Yamadaafbde6f2016-06-07 21:03:44 +090027
Simon Glassb0ea7402016-10-02 17:59:28 -060028 compat = fdt_stringlist_get(gd->fdt_blob, 0, "compatible", 0, NULL);
29 if (!compat)
Masahiro Yamadabdd79552019-07-10 20:07:47 +090030 goto fail;
Masahiro Yamada8e3e0fe2016-04-21 14:43:17 +090031
Masahiro Yamadaadb349d2016-10-17 22:18:02 +090032 /* rip off the vendor prefix "socionext," */
33 compat = strchr(compat, ',');
34 if (!compat)
Masahiro Yamadabdd79552019-07-10 20:07:47 +090035 goto fail;
Masahiro Yamadaadb349d2016-10-17 22:18:02 +090036 compat++;
Masahiro Yamada8e3e0fe2016-04-21 14:43:17 +090037
Masahiro Yamadaadb349d2016-10-17 22:18:02 +090038 strncpy(dtb_name, compat, buf_len);
Masahiro Yamada8e3e0fe2016-04-21 14:43:17 +090039 buf_len -= strlen(compat);
40
41 strncat(dtb_name, ".dtb", buf_len);
42
Masahiro Yamadabdd79552019-07-10 20:07:47 +090043 ret = env_set("fdtfile", dtb_name);
44 if (ret)
45 goto fail;
46
47 return;
48fail:
49 pr_warn("\"fdt_file\" environment variable was not set correctly\n");
Masahiro Yamadabf6e3fc2015-12-17 18:00:37 +090050}
51
Masahiro Yamada1fedafb2019-07-10 20:07:48 +090052static void uniphier_set_env_addr(const char *env, const char *offset_env)
53{
Masahiro Yamadae6eea832020-07-30 18:28:06 +090054 DECLARE_GLOBAL_DATA_PTR;
Masahiro Yamada1fedafb2019-07-10 20:07:48 +090055 unsigned long offset = 0;
56 const char *str;
57 char *end;
58 int ret;
59
60 if (env_get(env))
61 return; /* do nothing if it is already set */
62
63 if (offset_env) {
64 str = env_get(offset_env);
65 if (!str)
66 goto fail;
67
68 offset = simple_strtoul(str, &end, 16);
69 if (*end)
70 goto fail;
71 }
72
73 ret = env_set_hex(env, gd->ram_base + offset);
74 if (ret)
75 goto fail;
76
77 return;
78
79fail:
80 pr_warn("\"%s\" environment variable was not set correctly\n", env);
81}
82
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090083int board_late_init(void)
84{
85 puts("MODE: ");
86
Masahiro Yamadafb092032017-02-14 01:24:26 +090087 switch (uniphier_boot_device_raw()) {
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090088 case BOOT_DEVICE_MMC1:
Masahiro Yamadaaf198102017-04-20 16:54:43 +090089 printf("eMMC Boot");
Masahiro Yamada93270eb2018-12-19 20:03:13 +090090 env_set("bootdev", "emmc");
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090091 break;
Masahiro Yamadac85f81b2019-07-10 20:07:39 +090092 case BOOT_DEVICE_MMC2:
93 printf("SD Boot");
94 env_set("bootdev", "sd");
95 break;
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090096 case BOOT_DEVICE_NAND:
Masahiro Yamadaaf198102017-04-20 16:54:43 +090097 printf("NAND Boot");
Masahiro Yamada93270eb2018-12-19 20:03:13 +090098 env_set("bootdev", "nand");
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090099 break;
100 case BOOT_DEVICE_NOR:
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900101 printf("NOR Boot");
Masahiro Yamada93270eb2018-12-19 20:03:13 +0900102 env_set("bootdev", "nor");
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900103 break;
Masahiro Yamada5fe0e332016-02-02 21:11:31 +0900104 case BOOT_DEVICE_USB:
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900105 printf("USB Boot");
Masahiro Yamada93270eb2018-12-19 20:03:13 +0900106 env_set("bootdev", "usb");
Masahiro Yamada5fe0e332016-02-02 21:11:31 +0900107 break;
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900108 default:
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900109 printf("Unknown");
Masahiro Yamada5572b0a2016-04-21 14:43:16 +0900110 break;
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900111 }
112
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900113 if (uniphier_have_internal_stm())
114 printf(" (STM: %s)",
115 uniphier_boot_from_backend() ? "OFF" : "ON");
116
117 printf("\n");
118
Masahiro Yamadabdd79552019-07-10 20:07:47 +0900119 uniphier_set_env_fdt_file();
Masahiro Yamadabf6e3fc2015-12-17 18:00:37 +0900120
Masahiro Yamadaa74f0c32019-07-10 20:07:49 +0900121 uniphier_set_env_addr("dram_base", NULL);
122
Masahiro Yamada1fedafb2019-07-10 20:07:48 +0900123 uniphier_set_env_addr("loadaddr", "loadaddr_offset");
124
Masahiro Yamadaa4677b12019-07-10 20:07:50 +0900125 uniphier_set_env_addr("kernel_addr_r", "kernel_addr_r_offset");
126 uniphier_set_env_addr("ramdisk_addr_r", "ramdisk_addr_r_offset");
127 uniphier_set_env_addr("fdt_addr_r", "fdt_addr_r_offset");
128
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900129 return 0;
130}