Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 2 | /* |
Masahiro Yamada | adb349d | 2016-10-17 22:18:02 +0900 | [diff] [blame] | 3 | * Copyright (C) 2014 Panasonic Corporation |
| 4 | * Copyright (C) 2015-2016 Socionext Inc. |
| 5 | * Author: Masahiro Yamada <yamada.masahiro@socionext.com> |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 6 | */ |
| 7 | |
Simon Glass | 313112a | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 8 | #include <env.h> |
Simon Glass | a7b5130 | 2019-11-14 12:57:46 -0700 | [diff] [blame] | 9 | #include <init.h> |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 10 | #include <spl.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 11 | #include <asm/global_data.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 12 | #include <linux/libfdt.h> |
Masahiro Yamada | 609cd53 | 2017-10-13 19:21:55 +0900 | [diff] [blame] | 13 | #include <stdio.h> |
Masahiro Yamada | 609cd53 | 2017-10-13 19:21:55 +0900 | [diff] [blame] | 14 | #include <linux/printk.h> |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 15 | |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 16 | #include "init.h" |
Masahiro Yamada | 5fe0e33 | 2016-02-02 21:11:31 +0900 | [diff] [blame] | 17 | |
Masahiro Yamada | bdd7955 | 2019-07-10 20:07:47 +0900 | [diff] [blame] | 18 | static void uniphier_set_env_fdt_file(void) |
Masahiro Yamada | bf6e3fc | 2015-12-17 18:00:37 +0900 | [diff] [blame] | 19 | { |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
Masahiro Yamada | 8e3e0fe | 2016-04-21 14:43:17 +0900 | [diff] [blame] | 21 | const char *compat; |
| 22 | char dtb_name[256]; |
Masahiro Yamada | adb349d | 2016-10-17 22:18:02 +0900 | [diff] [blame] | 23 | int buf_len = sizeof(dtb_name); |
Masahiro Yamada | bdd7955 | 2019-07-10 20:07:47 +0900 | [diff] [blame] | 24 | int ret; |
Masahiro Yamada | bf6e3fc | 2015-12-17 18:00:37 +0900 | [diff] [blame] | 25 | |
Masahiro Yamada | 018f2fb | 2018-05-17 19:55:20 +0900 | [diff] [blame] | 26 | if (env_get("fdtfile")) |
Masahiro Yamada | bdd7955 | 2019-07-10 20:07:47 +0900 | [diff] [blame] | 27 | return; /* do nothing if it is already set */ |
Masahiro Yamada | afbde6f | 2016-06-07 21:03:44 +0900 | [diff] [blame] | 28 | |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 29 | compat = fdt_stringlist_get(gd->fdt_blob, 0, "compatible", 0, NULL); |
| 30 | if (!compat) |
Masahiro Yamada | bdd7955 | 2019-07-10 20:07:47 +0900 | [diff] [blame] | 31 | goto fail; |
Masahiro Yamada | 8e3e0fe | 2016-04-21 14:43:17 +0900 | [diff] [blame] | 32 | |
Masahiro Yamada | adb349d | 2016-10-17 22:18:02 +0900 | [diff] [blame] | 33 | /* rip off the vendor prefix "socionext," */ |
| 34 | compat = strchr(compat, ','); |
| 35 | if (!compat) |
Masahiro Yamada | bdd7955 | 2019-07-10 20:07:47 +0900 | [diff] [blame] | 36 | goto fail; |
Masahiro Yamada | adb349d | 2016-10-17 22:18:02 +0900 | [diff] [blame] | 37 | compat++; |
Masahiro Yamada | 8e3e0fe | 2016-04-21 14:43:17 +0900 | [diff] [blame] | 38 | |
Masahiro Yamada | adb349d | 2016-10-17 22:18:02 +0900 | [diff] [blame] | 39 | strncpy(dtb_name, compat, buf_len); |
Masahiro Yamada | 8e3e0fe | 2016-04-21 14:43:17 +0900 | [diff] [blame] | 40 | buf_len -= strlen(compat); |
| 41 | |
| 42 | strncat(dtb_name, ".dtb", buf_len); |
| 43 | |
Masahiro Yamada | bdd7955 | 2019-07-10 20:07:47 +0900 | [diff] [blame] | 44 | ret = env_set("fdtfile", dtb_name); |
| 45 | if (ret) |
| 46 | goto fail; |
| 47 | |
| 48 | return; |
| 49 | fail: |
| 50 | pr_warn("\"fdt_file\" environment variable was not set correctly\n"); |
Masahiro Yamada | bf6e3fc | 2015-12-17 18:00:37 +0900 | [diff] [blame] | 51 | } |
| 52 | |
Masahiro Yamada | 1fedafb | 2019-07-10 20:07:48 +0900 | [diff] [blame] | 53 | static void uniphier_set_env_addr(const char *env, const char *offset_env) |
| 54 | { |
Masahiro Yamada | e6eea83 | 2020-07-30 18:28:06 +0900 | [diff] [blame] | 55 | DECLARE_GLOBAL_DATA_PTR; |
Masahiro Yamada | 1fedafb | 2019-07-10 20:07:48 +0900 | [diff] [blame] | 56 | 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 | |
| 69 | offset = simple_strtoul(str, &end, 16); |
| 70 | 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 | |
| 80 | fail: |
| 81 | pr_warn("\"%s\" environment variable was not set correctly\n", env); |
| 82 | } |
| 83 | |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 84 | int board_late_init(void) |
| 85 | { |
| 86 | puts("MODE: "); |
| 87 | |
Masahiro Yamada | fb09203 | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 88 | switch (uniphier_boot_device_raw()) { |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 89 | case BOOT_DEVICE_MMC1: |
Masahiro Yamada | af19810 | 2017-04-20 16:54:43 +0900 | [diff] [blame] | 90 | printf("eMMC Boot"); |
Masahiro Yamada | 93270eb | 2018-12-19 20:03:13 +0900 | [diff] [blame] | 91 | env_set("bootdev", "emmc"); |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 92 | break; |
Masahiro Yamada | c85f81b | 2019-07-10 20:07:39 +0900 | [diff] [blame] | 93 | case BOOT_DEVICE_MMC2: |
| 94 | printf("SD Boot"); |
| 95 | env_set("bootdev", "sd"); |
| 96 | break; |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 97 | case BOOT_DEVICE_NAND: |
Masahiro Yamada | af19810 | 2017-04-20 16:54:43 +0900 | [diff] [blame] | 98 | printf("NAND Boot"); |
Masahiro Yamada | 93270eb | 2018-12-19 20:03:13 +0900 | [diff] [blame] | 99 | env_set("bootdev", "nand"); |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 100 | break; |
| 101 | case BOOT_DEVICE_NOR: |
Masahiro Yamada | af19810 | 2017-04-20 16:54:43 +0900 | [diff] [blame] | 102 | printf("NOR Boot"); |
Masahiro Yamada | 93270eb | 2018-12-19 20:03:13 +0900 | [diff] [blame] | 103 | env_set("bootdev", "nor"); |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 104 | break; |
Masahiro Yamada | 5fe0e33 | 2016-02-02 21:11:31 +0900 | [diff] [blame] | 105 | case BOOT_DEVICE_USB: |
Masahiro Yamada | af19810 | 2017-04-20 16:54:43 +0900 | [diff] [blame] | 106 | printf("USB Boot"); |
Masahiro Yamada | 93270eb | 2018-12-19 20:03:13 +0900 | [diff] [blame] | 107 | env_set("bootdev", "usb"); |
Masahiro Yamada | 5fe0e33 | 2016-02-02 21:11:31 +0900 | [diff] [blame] | 108 | break; |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 109 | default: |
Masahiro Yamada | af19810 | 2017-04-20 16:54:43 +0900 | [diff] [blame] | 110 | printf("Unknown"); |
Masahiro Yamada | 5572b0a | 2016-04-21 14:43:16 +0900 | [diff] [blame] | 111 | break; |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 112 | } |
| 113 | |
Masahiro Yamada | af19810 | 2017-04-20 16:54:43 +0900 | [diff] [blame] | 114 | if (uniphier_have_internal_stm()) |
| 115 | printf(" (STM: %s)", |
| 116 | uniphier_boot_from_backend() ? "OFF" : "ON"); |
| 117 | |
| 118 | printf("\n"); |
| 119 | |
Masahiro Yamada | bdd7955 | 2019-07-10 20:07:47 +0900 | [diff] [blame] | 120 | uniphier_set_env_fdt_file(); |
Masahiro Yamada | bf6e3fc | 2015-12-17 18:00:37 +0900 | [diff] [blame] | 121 | |
Masahiro Yamada | a74f0c3 | 2019-07-10 20:07:49 +0900 | [diff] [blame] | 122 | uniphier_set_env_addr("dram_base", NULL); |
| 123 | |
Masahiro Yamada | 1fedafb | 2019-07-10 20:07:48 +0900 | [diff] [blame] | 124 | uniphier_set_env_addr("loadaddr", "loadaddr_offset"); |
| 125 | |
Masahiro Yamada | a4677b1 | 2019-07-10 20:07:50 +0900 | [diff] [blame] | 126 | 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 Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 130 | return 0; |
| 131 | } |