Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Nobuhiro Iwamatsu | a541344 | 2014-12-02 16:52:20 +0900 | [diff] [blame] | 2 | /* |
Nobuhiro Iwamatsu | 8040395 | 2016-04-01 03:51:33 +0900 | [diff] [blame] | 3 | * board/renesas/rcar-common/common.c |
Nobuhiro Iwamatsu | a541344 | 2014-12-02 16:52:20 +0900 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2013 Renesas Electronics Corporation |
| 6 | * Copyright (C) 2013 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com> |
Nobuhiro Iwamatsu | 4dc515a | 2016-04-01 03:51:34 +0900 | [diff] [blame] | 7 | * Copyright (C) 2015 Nobuhiro Iwamatsu <iwamatsu@nigauri.org> |
Nobuhiro Iwamatsu | a541344 | 2014-12-02 16:52:20 +0900 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
Marek Vasut | 45eaf05 | 2019-07-09 01:46:35 +0200 | [diff] [blame] | 11 | #include <dm.h> |
Geert Uytterhoeven | af90acf | 2022-03-29 14:19:08 +0200 | [diff] [blame] | 12 | #include <fdt_support.h> |
Marek Vasut | 78cea93 | 2023-05-31 20:10:28 +0200 | [diff] [blame] | 13 | #include <hang.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 14 | #include <init.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 15 | #include <asm/global_data.h> |
Marek Vasut | 78cea93 | 2023-05-31 20:10:28 +0200 | [diff] [blame] | 16 | #include <asm/io.h> |
Marek Vasut | 45eaf05 | 2019-07-09 01:46:35 +0200 | [diff] [blame] | 17 | #include <dm/uclass-internal.h> |
Nobuhiro Iwamatsu | a541344 | 2014-12-02 16:52:20 +0900 | [diff] [blame] | 18 | #include <asm/arch/rmobile.h> |
Simon Glass | 2dc9c34 | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 19 | #include <linux/libfdt.h> |
Marek Vasut | 276a1d8 | 2019-05-19 23:25:16 +0200 | [diff] [blame] | 20 | |
Hai Pham | 9046b5f | 2023-02-28 22:22:03 +0100 | [diff] [blame] | 21 | #ifdef CONFIG_RCAR_64 |
Marek Vasut | 276a1d8 | 2019-05-19 23:25:16 +0200 | [diff] [blame] | 22 | |
| 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
Geert Uytterhoeven | af90acf | 2022-03-29 14:19:08 +0200 | [diff] [blame] | 25 | /* If the firmware passed a device tree use it for e.g. U-Boot DRAM setup. */ |
Marek Vasut | 276a1d8 | 2019-05-19 23:25:16 +0200 | [diff] [blame] | 26 | extern u64 rcar_atf_boot_args[]; |
| 27 | |
Geert Uytterhoeven | af90acf | 2022-03-29 14:19:08 +0200 | [diff] [blame] | 28 | #define FDT_RPC_PATH "/soc/spi@ee200000" |
| 29 | |
Marek Vasut | da8646a | 2020-04-11 20:50:24 +0200 | [diff] [blame] | 30 | int fdtdec_board_setup(const void *fdt_blob) |
Marek Vasut | 276a1d8 | 2019-05-19 23:25:16 +0200 | [diff] [blame] | 31 | { |
Marek Vasut | da8646a | 2020-04-11 20:50:24 +0200 | [diff] [blame] | 32 | void *atf_fdt_blob = (void *)(rcar_atf_boot_args[1]); |
Marek Vasut | 276a1d8 | 2019-05-19 23:25:16 +0200 | [diff] [blame] | 33 | |
Marek Vasut | 276a1d8 | 2019-05-19 23:25:16 +0200 | [diff] [blame] | 34 | if (fdt_magic(atf_fdt_blob) == FDT_MAGIC) |
Marek Vasut | da8646a | 2020-04-11 20:50:24 +0200 | [diff] [blame] | 35 | fdt_overlay_apply_node((void *)fdt_blob, 0, atf_fdt_blob, 0); |
Marek Vasut | 276a1d8 | 2019-05-19 23:25:16 +0200 | [diff] [blame] | 36 | |
Marek Vasut | da8646a | 2020-04-11 20:50:24 +0200 | [diff] [blame] | 37 | return 0; |
Marek Vasut | 276a1d8 | 2019-05-19 23:25:16 +0200 | [diff] [blame] | 38 | } |
| 39 | |
Marek Vasut | da8646a | 2020-04-11 20:50:24 +0200 | [diff] [blame] | 40 | int dram_init(void) |
Marek Vasut | 276a1d8 | 2019-05-19 23:25:16 +0200 | [diff] [blame] | 41 | { |
Michal Simek | c7ac6ac | 2020-07-10 13:16:48 +0200 | [diff] [blame] | 42 | return fdtdec_setup_mem_size_base(); |
Marek Vasut | da8646a | 2020-04-11 20:50:24 +0200 | [diff] [blame] | 43 | } |
Marek Vasut | 276a1d8 | 2019-05-19 23:25:16 +0200 | [diff] [blame] | 44 | |
Marek Vasut | da8646a | 2020-04-11 20:50:24 +0200 | [diff] [blame] | 45 | int dram_init_banksize(void) |
| 46 | { |
Michal Simek | c7ac6ac | 2020-07-10 13:16:48 +0200 | [diff] [blame] | 47 | fdtdec_setup_memory_banksize(); |
Marek Vasut | 276a1d8 | 2019-05-19 23:25:16 +0200 | [diff] [blame] | 48 | |
| 49 | return 0; |
| 50 | } |
Marek Vasut | ce52852 | 2021-04-03 16:58:49 +0200 | [diff] [blame] | 51 | |
Marek Vasut | 9c6ad45 | 2023-05-31 20:33:04 +0200 | [diff] [blame^] | 52 | int __weak board_init(void) |
| 53 | { |
| 54 | return 0; |
| 55 | } |
| 56 | |
Marek Vasut | 78cea93 | 2023-05-31 20:10:28 +0200 | [diff] [blame] | 57 | #if defined(CONFIG_RCAR_GEN3) |
| 58 | #define RST_BASE 0xE6160000 |
| 59 | #define RST_CA57RESCNT (RST_BASE + 0x40) |
| 60 | #define RST_CA53RESCNT (RST_BASE + 0x44) |
| 61 | #define RST_RSTOUTCR (RST_BASE + 0x58) |
| 62 | #define RST_CA57_CODE 0xA5A5000F |
| 63 | #define RST_CA53_CODE 0x5A5A000F |
| 64 | |
| 65 | void __weak reset_cpu(void) |
| 66 | { |
| 67 | unsigned long midr, cputype; |
| 68 | |
| 69 | asm volatile("mrs %0, midr_el1" : "=r" (midr)); |
| 70 | cputype = (midr >> 4) & 0xfff; |
| 71 | |
| 72 | if (cputype == 0xd03) |
| 73 | writel(RST_CA53_CODE, RST_CA53RESCNT); |
| 74 | else if (cputype == 0xd07) |
| 75 | writel(RST_CA57_CODE, RST_CA57RESCNT); |
| 76 | else |
| 77 | hang(); |
| 78 | } |
| 79 | #elif defined(CONFIG_RCAR_GEN4) |
| 80 | #define RST_BASE 0xE6160000 /* Domain0 */ |
| 81 | #define RST_SRESCR0 (RST_BASE + 0x18) |
| 82 | #define RST_SPRES 0x5AA58000 |
| 83 | |
| 84 | void __weak reset_cpu(void) |
| 85 | { |
| 86 | writel(RST_SPRES, RST_SRESCR0); |
| 87 | } |
| 88 | #else |
| 89 | #error Neither CONFIG_RCAR_GEN3 nor CONFIG_RCAR_GEN4 are set |
| 90 | #endif |
| 91 | |
Marek Vasut | ce52852 | 2021-04-03 16:58:49 +0200 | [diff] [blame] | 92 | #if defined(CONFIG_OF_BOARD_SETUP) |
| 93 | static int is_mem_overlap(void *blob, int first_mem_node, int curr_mem_node) |
| 94 | { |
| 95 | struct fdt_resource first_mem_res, curr_mem_res; |
| 96 | int curr_mem_reg, first_mem_reg = 0; |
| 97 | int ret; |
| 98 | |
| 99 | for (;;) { |
| 100 | ret = fdt_get_resource(blob, first_mem_node, "reg", |
| 101 | first_mem_reg++, &first_mem_res); |
| 102 | if (ret) /* No more entries, no overlap found */ |
| 103 | return 0; |
| 104 | |
| 105 | curr_mem_reg = 0; |
| 106 | for (;;) { |
| 107 | ret = fdt_get_resource(blob, curr_mem_node, "reg", |
| 108 | curr_mem_reg++, &curr_mem_res); |
| 109 | if (ret) /* No more entries, check next tuple */ |
| 110 | break; |
| 111 | |
| 112 | if (curr_mem_res.end < first_mem_res.start) |
| 113 | continue; |
| 114 | |
| 115 | if (curr_mem_res.start >= first_mem_res.end) |
| 116 | continue; |
| 117 | |
Hai Pham | 2f688c2 | 2023-02-28 00:00:01 +0100 | [diff] [blame] | 118 | log_debug("Overlap found: 0x%llx..0x%llx / 0x%llx..0x%llx\n", |
| 119 | first_mem_res.start, first_mem_res.end, |
| 120 | curr_mem_res.start, curr_mem_res.end); |
Marek Vasut | ce52852 | 2021-04-03 16:58:49 +0200 | [diff] [blame] | 121 | |
| 122 | return 1; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
Geert Uytterhoeven | af90acf | 2022-03-29 14:19:08 +0200 | [diff] [blame] | 129 | static void scrub_duplicate_memory(void *blob) |
Marek Vasut | ce52852 | 2021-04-03 16:58:49 +0200 | [diff] [blame] | 130 | { |
| 131 | /* |
| 132 | * Scrub duplicate /memory@* node entries here. Some R-Car DTs might |
| 133 | * contain multiple /memory@* nodes, however fdt_fixup_memory_banks() |
| 134 | * either generates single /memory node or updates the first /memory |
| 135 | * node. Any remaining memory nodes are thus potential duplicates. |
| 136 | * |
| 137 | * However, it is not possible to delete all the memory nodes right |
| 138 | * away, since some of those might not be DRAM memory nodes, but some |
| 139 | * sort of other memory. Thus, delete only the memory nodes which are |
| 140 | * in the R-Car3 DBSC ranges. |
| 141 | */ |
| 142 | int mem = 0, first_mem_node = 0; |
| 143 | |
| 144 | for (;;) { |
| 145 | mem = fdt_node_offset_by_prop_value(blob, mem, |
| 146 | "device_type", "memory", 7); |
| 147 | if (mem < 0) |
| 148 | break; |
| 149 | if (!fdtdec_get_is_enabled(blob, mem)) |
| 150 | continue; |
| 151 | |
| 152 | /* First memory node, patched by U-Boot */ |
| 153 | if (!first_mem_node) { |
| 154 | first_mem_node = mem; |
| 155 | continue; |
| 156 | } |
| 157 | |
| 158 | /* Check the remaining nodes and delete duplicates */ |
| 159 | if (!is_mem_overlap(blob, first_mem_node, mem)) |
| 160 | continue; |
| 161 | |
| 162 | /* Delete duplicate node, start again */ |
| 163 | fdt_del_node(blob, mem); |
| 164 | first_mem_node = 0; |
| 165 | mem = 0; |
| 166 | } |
Geert Uytterhoeven | af90acf | 2022-03-29 14:19:08 +0200 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static void update_rpc_status(void *blob) |
| 170 | { |
| 171 | void *atf_fdt_blob = (void *)(rcar_atf_boot_args[1]); |
| 172 | int offset, enabled; |
| 173 | |
| 174 | /* |
| 175 | * Check if the DT fragment received from TF-A had its RPC-IF device node |
| 176 | * enabled. |
| 177 | */ |
| 178 | if (fdt_magic(atf_fdt_blob) != FDT_MAGIC) |
| 179 | return; |
| 180 | |
| 181 | offset = fdt_path_offset(atf_fdt_blob, FDT_RPC_PATH); |
| 182 | if (offset < 0) |
| 183 | return; |
| 184 | |
| 185 | enabled = fdtdec_get_is_enabled(atf_fdt_blob, offset); |
| 186 | if (!enabled) |
| 187 | return; |
| 188 | |
| 189 | /* |
| 190 | * Find the RPC-IF device node, and enable it if it has a flash subnode. |
| 191 | */ |
| 192 | offset = fdt_path_offset(blob, FDT_RPC_PATH); |
| 193 | if (offset < 0) |
| 194 | return; |
| 195 | |
| 196 | if (fdt_subnode_offset(blob, offset, "flash") < 0) |
| 197 | return; |
| 198 | |
| 199 | fdt_status_okay(blob, offset); |
| 200 | } |
| 201 | |
| 202 | int ft_board_setup(void *blob, struct bd_info *bd) |
| 203 | { |
| 204 | scrub_duplicate_memory(blob); |
| 205 | update_rpc_status(blob); |
Marek Vasut | ce52852 | 2021-04-03 16:58:49 +0200 | [diff] [blame] | 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | #endif |
Marek Vasut | 276a1d8 | 2019-05-19 23:25:16 +0200 | [diff] [blame] | 210 | #endif |