Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Masahiro Yamada | 5894ca0 | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 2 | /* |
Masahiro Yamada | 40749d5 | 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 | 5894ca0 | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | c7694dd | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 9 | #include <env.h> |
Masahiro Yamada | 5894ca0 | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 10 | #include <spl.h> |
Masahiro Yamada | b08c8c4 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 11 | #include <linux/libfdt.h> |
Masahiro Yamada | 5894ca0 | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 12 | #include <nand.h> |
Masahiro Yamada | dd74b94 | 2017-10-13 19:21:55 +0900 | [diff] [blame] | 13 | #include <stdio.h> |
Masahiro Yamada | f6e7f07 | 2015-05-29 17:30:00 +0900 | [diff] [blame] | 14 | #include <linux/io.h> |
Masahiro Yamada | dd74b94 | 2017-10-13 19:21:55 +0900 | [diff] [blame] | 15 | #include <linux/printk.h> |
Miquel Raynal | a430fa0 | 2018-08-16 17:30:07 +0200 | [diff] [blame] | 16 | #include <../drivers/mtd/nand/raw/denali.h> |
Masahiro Yamada | 5894ca0 | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 17 | |
Masahiro Yamada | 784548e | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 18 | #include "init.h" |
Masahiro Yamada | fec4816 | 2016-02-02 21:11:31 +0900 | [diff] [blame] | 19 | |
Masahiro Yamada | 5894ca0 | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 20 | static void nand_denali_wp_disable(void) |
| 21 | { |
| 22 | #ifdef CONFIG_NAND_DENALI |
| 23 | /* |
| 24 | * Since the boot rom enables the write protection for NAND boot mode, |
| 25 | * it must be disabled somewhere for "nand write", "nand erase", etc. |
| 26 | * The workaround is here to not disturb the Denali NAND controller |
| 27 | * driver just for a really SoC-specific thing. |
| 28 | */ |
| 29 | void __iomem *denali_reg = (void __iomem *)CONFIG_SYS_NAND_REGS_BASE; |
| 30 | |
| 31 | writel(WRITE_PROTECT__FLAG, denali_reg + WRITE_PROTECT); |
| 32 | #endif |
| 33 | } |
| 34 | |
Masahiro Yamada | 72cd83a | 2019-07-10 20:07:47 +0900 | [diff] [blame] | 35 | static void uniphier_set_env_fdt_file(void) |
Masahiro Yamada | 8ea4f49 | 2015-12-17 18:00:37 +0900 | [diff] [blame] | 36 | { |
| 37 | DECLARE_GLOBAL_DATA_PTR; |
Masahiro Yamada | 881aa5a | 2016-04-21 14:43:17 +0900 | [diff] [blame] | 38 | const char *compat; |
| 39 | char dtb_name[256]; |
Masahiro Yamada | 40749d5 | 2016-10-17 22:18:02 +0900 | [diff] [blame] | 40 | int buf_len = sizeof(dtb_name); |
Masahiro Yamada | 72cd83a | 2019-07-10 20:07:47 +0900 | [diff] [blame] | 41 | int ret; |
Masahiro Yamada | 8ea4f49 | 2015-12-17 18:00:37 +0900 | [diff] [blame] | 42 | |
Masahiro Yamada | 8c09f1f | 2018-05-17 19:55:20 +0900 | [diff] [blame] | 43 | if (env_get("fdtfile")) |
Masahiro Yamada | 72cd83a | 2019-07-10 20:07:47 +0900 | [diff] [blame] | 44 | return; /* do nothing if it is already set */ |
Masahiro Yamada | 4565a74 | 2016-06-07 21:03:44 +0900 | [diff] [blame] | 45 | |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 46 | compat = fdt_stringlist_get(gd->fdt_blob, 0, "compatible", 0, NULL); |
| 47 | if (!compat) |
Masahiro Yamada | 72cd83a | 2019-07-10 20:07:47 +0900 | [diff] [blame] | 48 | goto fail; |
Masahiro Yamada | 881aa5a | 2016-04-21 14:43:17 +0900 | [diff] [blame] | 49 | |
Masahiro Yamada | 40749d5 | 2016-10-17 22:18:02 +0900 | [diff] [blame] | 50 | /* rip off the vendor prefix "socionext," */ |
| 51 | compat = strchr(compat, ','); |
| 52 | if (!compat) |
Masahiro Yamada | 72cd83a | 2019-07-10 20:07:47 +0900 | [diff] [blame] | 53 | goto fail; |
Masahiro Yamada | 40749d5 | 2016-10-17 22:18:02 +0900 | [diff] [blame] | 54 | compat++; |
Masahiro Yamada | 881aa5a | 2016-04-21 14:43:17 +0900 | [diff] [blame] | 55 | |
Masahiro Yamada | 40749d5 | 2016-10-17 22:18:02 +0900 | [diff] [blame] | 56 | strncpy(dtb_name, compat, buf_len); |
Masahiro Yamada | 881aa5a | 2016-04-21 14:43:17 +0900 | [diff] [blame] | 57 | buf_len -= strlen(compat); |
| 58 | |
| 59 | strncat(dtb_name, ".dtb", buf_len); |
| 60 | |
Masahiro Yamada | 72cd83a | 2019-07-10 20:07:47 +0900 | [diff] [blame] | 61 | ret = env_set("fdtfile", dtb_name); |
| 62 | if (ret) |
| 63 | goto fail; |
| 64 | |
| 65 | return; |
| 66 | fail: |
| 67 | pr_warn("\"fdt_file\" environment variable was not set correctly\n"); |
Masahiro Yamada | 8ea4f49 | 2015-12-17 18:00:37 +0900 | [diff] [blame] | 68 | } |
| 69 | |
Masahiro Yamada | 3cc936d | 2019-07-10 20:07:48 +0900 | [diff] [blame] | 70 | static void uniphier_set_env_addr(const char *env, const char *offset_env) |
| 71 | { |
| 72 | unsigned long offset = 0; |
| 73 | const char *str; |
| 74 | char *end; |
| 75 | int ret; |
| 76 | |
| 77 | if (env_get(env)) |
| 78 | return; /* do nothing if it is already set */ |
| 79 | |
| 80 | if (offset_env) { |
| 81 | str = env_get(offset_env); |
| 82 | if (!str) |
| 83 | goto fail; |
| 84 | |
| 85 | offset = simple_strtoul(str, &end, 16); |
| 86 | if (*end) |
| 87 | goto fail; |
| 88 | } |
| 89 | |
| 90 | ret = env_set_hex(env, gd->ram_base + offset); |
| 91 | if (ret) |
| 92 | goto fail; |
| 93 | |
| 94 | return; |
| 95 | |
| 96 | fail: |
| 97 | pr_warn("\"%s\" environment variable was not set correctly\n", env); |
| 98 | } |
| 99 | |
Masahiro Yamada | 5894ca0 | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 100 | int board_late_init(void) |
| 101 | { |
| 102 | puts("MODE: "); |
| 103 | |
Masahiro Yamada | 784548e | 2017-02-14 01:24:26 +0900 | [diff] [blame] | 104 | switch (uniphier_boot_device_raw()) { |
Masahiro Yamada | 5894ca0 | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 105 | case BOOT_DEVICE_MMC1: |
Masahiro Yamada | 6375484 | 2017-04-20 16:54:43 +0900 | [diff] [blame] | 106 | printf("eMMC Boot"); |
Masahiro Yamada | 6bc50a8 | 2018-12-19 20:03:13 +0900 | [diff] [blame] | 107 | env_set("bootdev", "emmc"); |
Masahiro Yamada | 5894ca0 | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 108 | break; |
Masahiro Yamada | c3d8f1e | 2019-07-10 20:07:39 +0900 | [diff] [blame] | 109 | case BOOT_DEVICE_MMC2: |
| 110 | printf("SD Boot"); |
| 111 | env_set("bootdev", "sd"); |
| 112 | break; |
Masahiro Yamada | 5894ca0 | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 113 | case BOOT_DEVICE_NAND: |
Masahiro Yamada | 6375484 | 2017-04-20 16:54:43 +0900 | [diff] [blame] | 114 | printf("NAND Boot"); |
Masahiro Yamada | 6bc50a8 | 2018-12-19 20:03:13 +0900 | [diff] [blame] | 115 | env_set("bootdev", "nand"); |
Masahiro Yamada | 5894ca0 | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 116 | nand_denali_wp_disable(); |
| 117 | break; |
| 118 | case BOOT_DEVICE_NOR: |
Masahiro Yamada | 6375484 | 2017-04-20 16:54:43 +0900 | [diff] [blame] | 119 | printf("NOR Boot"); |
Masahiro Yamada | 6bc50a8 | 2018-12-19 20:03:13 +0900 | [diff] [blame] | 120 | env_set("bootdev", "nor"); |
Masahiro Yamada | 5894ca0 | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 121 | break; |
Masahiro Yamada | fec4816 | 2016-02-02 21:11:31 +0900 | [diff] [blame] | 122 | case BOOT_DEVICE_USB: |
Masahiro Yamada | 6375484 | 2017-04-20 16:54:43 +0900 | [diff] [blame] | 123 | printf("USB Boot"); |
Masahiro Yamada | 6bc50a8 | 2018-12-19 20:03:13 +0900 | [diff] [blame] | 124 | env_set("bootdev", "usb"); |
Masahiro Yamada | fec4816 | 2016-02-02 21:11:31 +0900 | [diff] [blame] | 125 | break; |
Masahiro Yamada | 5894ca0 | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 126 | default: |
Masahiro Yamada | 6375484 | 2017-04-20 16:54:43 +0900 | [diff] [blame] | 127 | printf("Unknown"); |
Masahiro Yamada | d90b974 | 2016-04-21 14:43:16 +0900 | [diff] [blame] | 128 | break; |
Masahiro Yamada | 5894ca0 | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 129 | } |
| 130 | |
Masahiro Yamada | 6375484 | 2017-04-20 16:54:43 +0900 | [diff] [blame] | 131 | if (uniphier_have_internal_stm()) |
| 132 | printf(" (STM: %s)", |
| 133 | uniphier_boot_from_backend() ? "OFF" : "ON"); |
| 134 | |
| 135 | printf("\n"); |
| 136 | |
Masahiro Yamada | 72cd83a | 2019-07-10 20:07:47 +0900 | [diff] [blame] | 137 | uniphier_set_env_fdt_file(); |
Masahiro Yamada | 8ea4f49 | 2015-12-17 18:00:37 +0900 | [diff] [blame] | 138 | |
Masahiro Yamada | 7095678 | 2019-07-10 20:07:49 +0900 | [diff] [blame] | 139 | uniphier_set_env_addr("dram_base", NULL); |
| 140 | |
Masahiro Yamada | 3cc936d | 2019-07-10 20:07:48 +0900 | [diff] [blame] | 141 | uniphier_set_env_addr("loadaddr", "loadaddr_offset"); |
| 142 | |
Masahiro Yamada | 2ce6b82 | 2019-07-10 20:07:50 +0900 | [diff] [blame] | 143 | uniphier_set_env_addr("kernel_addr_r", "kernel_addr_r_offset"); |
| 144 | uniphier_set_env_addr("ramdisk_addr_r", "ramdisk_addr_r_offset"); |
| 145 | uniphier_set_env_addr("fdt_addr_r", "fdt_addr_r_offset"); |
| 146 | |
Masahiro Yamada | 5894ca0 | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 147 | return 0; |
| 148 | } |