blob: b5356ed87e70a485ec4696807b8e03941685f7ee [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090012#include <linux/libfdt.h>
Masahiro Yamada609cd532017-10-13 19:21:55 +090013#include <stdio.h>
Masahiro Yamada609cd532017-10-13 19:21:55 +090014#include <linux/printk.h>
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090015
Masahiro Yamadafb092032017-02-14 01:24:26 +090016#include "init.h"
Masahiro Yamada5fe0e332016-02-02 21:11:31 +090017
Masahiro Yamadabdd79552019-07-10 20:07:47 +090018static void uniphier_set_env_fdt_file(void)
Masahiro Yamadabf6e3fc2015-12-17 18:00:37 +090019{
20 DECLARE_GLOBAL_DATA_PTR;
Masahiro Yamada8e3e0fe2016-04-21 14:43:17 +090021 const char *compat;
22 char dtb_name[256];
Masahiro Yamadaadb349d2016-10-17 22:18:02 +090023 int buf_len = sizeof(dtb_name);
Masahiro Yamadabdd79552019-07-10 20:07:47 +090024 int ret;
Masahiro Yamadabf6e3fc2015-12-17 18:00:37 +090025
Masahiro Yamada018f2fb2018-05-17 19:55:20 +090026 if (env_get("fdtfile"))
Masahiro Yamadabdd79552019-07-10 20:07:47 +090027 return; /* do nothing if it is already set */
Masahiro Yamadaafbde6f2016-06-07 21:03:44 +090028
Simon Glassb0ea7402016-10-02 17:59:28 -060029 compat = fdt_stringlist_get(gd->fdt_blob, 0, "compatible", 0, NULL);
30 if (!compat)
Masahiro Yamadabdd79552019-07-10 20:07:47 +090031 goto fail;
Masahiro Yamada8e3e0fe2016-04-21 14:43:17 +090032
Masahiro Yamadaadb349d2016-10-17 22:18:02 +090033 /* rip off the vendor prefix "socionext," */
34 compat = strchr(compat, ',');
35 if (!compat)
Masahiro Yamadabdd79552019-07-10 20:07:47 +090036 goto fail;
Masahiro Yamadaadb349d2016-10-17 22:18:02 +090037 compat++;
Masahiro Yamada8e3e0fe2016-04-21 14:43:17 +090038
Masahiro Yamadaadb349d2016-10-17 22:18:02 +090039 strncpy(dtb_name, compat, buf_len);
Masahiro Yamada8e3e0fe2016-04-21 14:43:17 +090040 buf_len -= strlen(compat);
41
42 strncat(dtb_name, ".dtb", buf_len);
43
Masahiro Yamadabdd79552019-07-10 20:07:47 +090044 ret = env_set("fdtfile", dtb_name);
45 if (ret)
46 goto fail;
47
48 return;
49fail:
50 pr_warn("\"fdt_file\" environment variable was not set correctly\n");
Masahiro Yamadabf6e3fc2015-12-17 18:00:37 +090051}
52
Masahiro Yamada1fedafb2019-07-10 20:07:48 +090053static void uniphier_set_env_addr(const char *env, const char *offset_env)
54{
Masahiro Yamadae6eea832020-07-30 18:28:06 +090055 DECLARE_GLOBAL_DATA_PTR;
Masahiro Yamada1fedafb2019-07-10 20:07:48 +090056 unsigned long offset = 0;
57 const char *str;
58 char *end;
59 int ret;
60
61 if (env_get(env))
62 return; /* do nothing if it is already set */
63
64 if (offset_env) {
65 str = env_get(offset_env);
66 if (!str)
67 goto fail;
68
Simon Glass3ff49ec2021-07-24 09:03:29 -060069 offset = hextoul(str, &end);
Masahiro Yamada1fedafb2019-07-10 20:07:48 +090070 if (*end)
71 goto fail;
72 }
73
74 ret = env_set_hex(env, gd->ram_base + offset);
75 if (ret)
76 goto fail;
77
78 return;
79
80fail:
81 pr_warn("\"%s\" environment variable was not set correctly\n", env);
82}
83
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090084int board_late_init(void)
85{
86 puts("MODE: ");
87
Masahiro Yamadafb092032017-02-14 01:24:26 +090088 switch (uniphier_boot_device_raw()) {
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090089 case BOOT_DEVICE_MMC1:
Masahiro Yamadaaf198102017-04-20 16:54:43 +090090 printf("eMMC Boot");
Masahiro Yamada93270eb2018-12-19 20:03:13 +090091 env_set("bootdev", "emmc");
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090092 break;
Masahiro Yamadac85f81b2019-07-10 20:07:39 +090093 case BOOT_DEVICE_MMC2:
94 printf("SD Boot");
95 env_set("bootdev", "sd");
96 break;
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090097 case BOOT_DEVICE_NAND:
Masahiro Yamadaaf198102017-04-20 16:54:43 +090098 printf("NAND Boot");
Masahiro Yamada93270eb2018-12-19 20:03:13 +090099 env_set("bootdev", "nand");
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900100 break;
101 case BOOT_DEVICE_NOR:
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900102 printf("NOR Boot");
Masahiro Yamada93270eb2018-12-19 20:03:13 +0900103 env_set("bootdev", "nor");
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900104 break;
Masahiro Yamada5fe0e332016-02-02 21:11:31 +0900105 case BOOT_DEVICE_USB:
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900106 printf("USB Boot");
Masahiro Yamada93270eb2018-12-19 20:03:13 +0900107 env_set("bootdev", "usb");
Masahiro Yamada5fe0e332016-02-02 21:11:31 +0900108 break;
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900109 default:
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900110 printf("Unknown");
Masahiro Yamada5572b0a2016-04-21 14:43:16 +0900111 break;
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900112 }
113
Masahiro Yamadaaf198102017-04-20 16:54:43 +0900114 if (uniphier_have_internal_stm())
115 printf(" (STM: %s)",
116 uniphier_boot_from_backend() ? "OFF" : "ON");
117
118 printf("\n");
119
Masahiro Yamadabdd79552019-07-10 20:07:47 +0900120 uniphier_set_env_fdt_file();
Masahiro Yamadabf6e3fc2015-12-17 18:00:37 +0900121
Masahiro Yamadaa74f0c32019-07-10 20:07:49 +0900122 uniphier_set_env_addr("dram_base", NULL);
123
Masahiro Yamada1fedafb2019-07-10 20:07:48 +0900124 uniphier_set_env_addr("loadaddr", "loadaddr_offset");
125
Masahiro Yamadaa4677b12019-07-10 20:07:50 +0900126 uniphier_set_env_addr("kernel_addr_r", "kernel_addr_r_offset");
127 uniphier_set_env_addr("ramdisk_addr_r", "ramdisk_addr_r_offset");
128 uniphier_set_env_addr("fdt_addr_r", "fdt_addr_r_offset");
129
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900130 return 0;
131}