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