blob: c5482403a29ee9d1f435b095b418c8dfbb89e59f [file] [log] [blame]
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02001/*
Takuya Sakatacb9efd82021-12-01 13:42:54 +09002 * Copyright (c) 2018-2023, Renesas Electronics Corporation. All rights reserved.
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Scott Brandene5dcf982020-08-25 13:49:32 -07007#include <inttypes.h>
8#include <stdint.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00009#include <string.h>
10
Marek Vasut93c85fc2018-10-02 20:45:18 +020011#include <libfdt.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +020013#include <platform_def.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000014
15#include <arch_helpers.h>
16#include <bl1/bl1.h>
17#include <common/bl_common.h>
18#include <common/debug.h>
19#include <common/desc_image_load.h>
Marek Vasutb25ee352021-02-13 19:09:29 +010020#include <common/image_decompress.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000021#include <drivers/console.h>
Toshiyuki Ogasahara04f16282019-12-13 14:43:52 +090022#include <drivers/io/io_driver.h>
23#include <drivers/io/io_storage.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000024#include <lib/mmio.h>
25#include <lib/xlat_tables/xlat_tables_defs.h>
26#include <plat/common/platform.h>
Marek Vasutb25ee352021-02-13 19:09:29 +010027#if RCAR_GEN3_BL33_GZIP == 1
28#include <tf_gunzip.h>
29#endif
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +020030
31#include "avs_driver.h"
32#include "boot_init_dram.h"
33#include "cpg_registers.h"
34#include "board.h"
35#include "emmc_def.h"
36#include "emmc_hal.h"
37#include "emmc_std.h"
38
39#if PMIC_ROHM_BD9571 && RCAR_SYSTEM_RESET_KEEPON_DDR
40#include "iic_dvfs.h"
41#endif
42
43#include "io_common.h"
Toshiyuki Ogasahara04f16282019-12-13 14:43:52 +090044#include "io_rcar.h"
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +020045#include "qos_init.h"
46#include "rcar_def.h"
47#include "rcar_private.h"
48#include "rcar_version.h"
49#include "rom_api.h"
50
Madhukar Pappireddyfc9b4112019-12-23 14:49:52 -060051/*
Toshiyuki Ogasahara18fb4d82021-07-12 18:33:42 +090052 * Following symbols are only used during plat_arch_setup()
Madhukar Pappireddyfc9b4112019-12-23 14:49:52 -060053 */
54static const uint64_t BL2_RO_BASE = BL_CODE_BASE;
55static const uint64_t BL2_RO_LIMIT = BL_CODE_END;
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +020056
57#if USE_COHERENT_MEM
Madhukar Pappireddyfc9b4112019-12-23 14:49:52 -060058static const uint64_t BL2_COHERENT_RAM_BASE = BL_COHERENT_RAM_BASE;
59static const uint64_t BL2_COHERENT_RAM_LIMIT = BL_COHERENT_RAM_END;
60#endif
61
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +020062extern void plat_rcar_gic_driver_init(void);
63extern void plat_rcar_gic_init(void);
64extern void bl2_enter_bl31(const struct entry_point_info *bl_ep_info);
65extern void bl2_system_cpg_init(void);
66extern void bl2_secure_setting(void);
Toshiyuki Ogasahara22c44b92021-07-12 18:39:52 +090067extern void bl2_ram_security_setting_finish(void);
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +020068extern void bl2_cpg_init(void);
69extern void rcar_io_emmc_setup(void);
70extern void rcar_io_setup(void);
71extern void rcar_swdt_release(void);
72extern void rcar_swdt_init(void);
73extern void rcar_rpc_init(void);
74extern void rcar_pfc_init(void);
75extern void rcar_dma_init(void);
76
Marek Vasut1eca7782018-12-28 20:12:13 +010077static void bl2_init_generic_timer(void);
78
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +020079/* R-Car Gen3 product check */
80#if (RCAR_LSI == RCAR_H3) || (RCAR_LSI == RCAR_H3N)
Marek Vasut9cadc782019-08-06 19:13:22 +020081#define TARGET_PRODUCT PRR_PRODUCT_H3
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +020082#define TARGET_NAME "R-Car H3"
83#elif RCAR_LSI == RCAR_M3
Marek Vasut9cadc782019-08-06 19:13:22 +020084#define TARGET_PRODUCT PRR_PRODUCT_M3
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +020085#define TARGET_NAME "R-Car M3"
86#elif RCAR_LSI == RCAR_M3N
Marek Vasut9cadc782019-08-06 19:13:22 +020087#define TARGET_PRODUCT PRR_PRODUCT_M3N
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +020088#define TARGET_NAME "R-Car M3N"
Valentine Barshakf2184142018-10-30 02:06:17 +030089#elif RCAR_LSI == RCAR_V3M
Marek Vasut9cadc782019-08-06 19:13:22 +020090#define TARGET_PRODUCT PRR_PRODUCT_V3M
Valentine Barshakf2184142018-10-30 02:06:17 +030091#define TARGET_NAME "R-Car V3M"
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +020092#elif RCAR_LSI == RCAR_E3
Marek Vasut9cadc782019-08-06 19:13:22 +020093#define TARGET_PRODUCT PRR_PRODUCT_E3
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +020094#define TARGET_NAME "R-Car E3"
Marek Vasut4ae342c2019-01-05 13:56:03 +010095#elif RCAR_LSI == RCAR_D3
Marek Vasut9cadc782019-08-06 19:13:22 +020096#define TARGET_PRODUCT PRR_PRODUCT_D3
Marek Vasut4ae342c2019-01-05 13:56:03 +010097#define TARGET_NAME "R-Car D3"
Marek Vasut94cc0f82018-12-28 20:11:26 +010098#elif RCAR_LSI == RCAR_AUTO
Valentine Barshakf2184142018-10-30 02:06:17 +030099#define TARGET_NAME "R-Car H3/M3/M3N/V3M"
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200100#endif
101
102#if (RCAR_LSI == RCAR_E3)
103#define GPIO_INDT (GPIO_INDT6)
104#define GPIO_BKUP_TRG_SHIFT ((uint32_t)1U<<13U)
105#else
106#define GPIO_INDT (GPIO_INDT1)
107#define GPIO_BKUP_TRG_SHIFT ((uint32_t)1U<<8U)
108#endif
109
110CASSERT((PARAMS_BASE + sizeof(bl2_to_bl31_params_mem_t) + 0x100)
111 < (RCAR_SHARED_MEM_BASE + RCAR_SHARED_MEM_SIZE),
112 assert_bl31_params_do_not_fit_in_shared_memory);
113
114static meminfo_t bl2_tzram_layout __aligned(CACHE_WRITEBACK_GRANULE);
115
Marek Vasut93c85fc2018-10-02 20:45:18 +0200116/* FDT with DRAM configuration */
117uint64_t fdt_blob[PAGE_SIZE_4KB / sizeof(uint64_t)];
118static void *fdt = (void *)fdt_blob;
119
120static void unsigned_num_print(unsigned long long int unum, unsigned int radix,
121 char *string)
122{
123 /* Just need enough space to store 64 bit decimal integer */
124 char num_buf[20];
125 int i = 0;
126 unsigned int rem;
127
128 do {
129 rem = unum % radix;
130 if (rem < 0xa)
131 num_buf[i] = '0' + rem;
132 else
133 num_buf[i] = 'a' + (rem - 0xa);
134 i++;
135 unum /= radix;
136 } while (unum > 0U);
137
138 while (--i >= 0)
139 *string++ = num_buf[i];
Marek Vasut64299332020-04-11 19:02:29 +0200140 *string = 0;
Marek Vasut93c85fc2018-10-02 20:45:18 +0200141}
142
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200143#if (RCAR_LOSSY_ENABLE == 1)
144typedef struct bl2_lossy_info {
145 uint32_t magic;
146 uint32_t a0;
147 uint32_t b0;
148} bl2_lossy_info_t;
149
Marek Vasut4d693c22018-10-11 16:53:58 +0200150static void bl2_lossy_gen_fdt(uint32_t no, uint64_t start_addr,
151 uint64_t end_addr, uint32_t format,
152 uint32_t enable, int fcnlnode)
153{
154 const uint64_t fcnlsize = cpu_to_fdt64(end_addr - start_addr);
155 char nodename[40] = { 0 };
156 int ret, node;
157
158 /* Ignore undefined addresses */
159 if (start_addr == 0 && end_addr == 0)
160 return;
161
162 snprintf(nodename, sizeof(nodename), "lossy-decompression@");
163 unsigned_num_print(start_addr, 16, nodename + strlen(nodename));
164
165 node = ret = fdt_add_subnode(fdt, fcnlnode, nodename);
166 if (ret < 0) {
167 NOTICE("BL2: Cannot create FCNL node (ret=%i)\n", ret);
168 panic();
169 }
170
171 ret = fdt_setprop_string(fdt, node, "compatible",
172 "renesas,lossy-decompression");
173 if (ret < 0) {
174 NOTICE("BL2: Cannot add FCNL compat string (ret=%i)\n", ret);
175 panic();
176 }
177
178 ret = fdt_appendprop_string(fdt, node, "compatible",
179 "shared-dma-pool");
180 if (ret < 0) {
181 NOTICE("BL2: Cannot append FCNL compat string (ret=%i)\n", ret);
182 panic();
183 }
184
185 ret = fdt_setprop_u64(fdt, node, "reg", start_addr);
186 if (ret < 0) {
187 NOTICE("BL2: Cannot add FCNL reg prop (ret=%i)\n", ret);
188 panic();
189 }
190
191 ret = fdt_appendprop(fdt, node, "reg", &fcnlsize, sizeof(fcnlsize));
192 if (ret < 0) {
193 NOTICE("BL2: Cannot append FCNL reg size prop (ret=%i)\n", ret);
194 panic();
195 }
196
197 ret = fdt_setprop(fdt, node, "no-map", NULL, 0);
198 if (ret < 0) {
199 NOTICE("BL2: Cannot add FCNL no-map prop (ret=%i)\n", ret);
200 panic();
201 }
202
203 ret = fdt_setprop_u32(fdt, node, "renesas,formats", format);
204 if (ret < 0) {
205 NOTICE("BL2: Cannot add FCNL formats prop (ret=%i)\n", ret);
206 panic();
207 }
208}
209
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200210static void bl2_lossy_setting(uint32_t no, uint64_t start_addr,
211 uint64_t end_addr, uint32_t format,
Marek Vasut4d693c22018-10-11 16:53:58 +0200212 uint32_t enable, int fcnlnode)
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200213{
214 bl2_lossy_info_t info;
215 uint32_t reg;
216
Marek Vasut4d693c22018-10-11 16:53:58 +0200217 bl2_lossy_gen_fdt(no, start_addr, end_addr, format, enable, fcnlnode);
218
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200219 reg = format | (start_addr >> 20);
220 mmio_write_32(AXI_DCMPAREACRA0 + 0x8 * no, reg);
221 mmio_write_32(AXI_DCMPAREACRB0 + 0x8 * no, end_addr >> 20);
222 mmio_write_32(AXI_DCMPAREACRA0 + 0x8 * no, reg | enable);
223
224 info.magic = 0x12345678U;
225 info.a0 = mmio_read_32(AXI_DCMPAREACRA0 + 0x8 * no);
226 info.b0 = mmio_read_32(AXI_DCMPAREACRB0 + 0x8 * no);
227
228 mmio_write_32(LOSSY_PARAMS_BASE + sizeof(info) * no, info.magic);
229 mmio_write_32(LOSSY_PARAMS_BASE + sizeof(info) * no + 0x4, info.a0);
230 mmio_write_32(LOSSY_PARAMS_BASE + sizeof(info) * no + 0x8, info.b0);
231
232 NOTICE(" Entry %d: DCMPAREACRAx:0x%x DCMPAREACRBx:0x%x\n", no,
233 mmio_read_32(AXI_DCMPAREACRA0 + 0x8 * no),
234 mmio_read_32(AXI_DCMPAREACRB0 + 0x8 * no));
235}
Detlev Casanova8348acd2022-12-01 17:57:31 -0500236
237static int bl2_create_reserved_memory(void)
238{
239 int ret;
240
241 int fcnlnode = fdt_add_subnode(fdt, 0, "reserved-memory");
242 if (fcnlnode < 0) {
243 NOTICE("BL2: Cannot create reserved mem node (ret=%i)\n",
244 fcnlnode);
245 panic();
246 }
247
248 ret = fdt_setprop(fdt, fcnlnode, "ranges", NULL, 0);
249 if (ret < 0) {
250 NOTICE("BL2: Cannot add FCNL ranges prop (ret=%i)\n", ret);
251 panic();
252 }
253
254 ret = fdt_setprop_u32(fdt, fcnlnode, "#address-cells", 2);
255 if (ret < 0) {
256 NOTICE("BL2: Cannot add FCNL #address-cells prop (ret=%i)\n", ret);
257 panic();
258 }
259
260 ret = fdt_setprop_u32(fdt, fcnlnode, "#size-cells", 2);
261 if (ret < 0) {
262 NOTICE("BL2: Cannot add FCNL #size-cells prop (ret=%i)\n", ret);
263 panic();
264 }
265
266 return fcnlnode;
267}
268
269static void bl2_create_fcnl_reserved_memory(void)
270{
271 int fcnlnode;
272
273 NOTICE("BL2: Lossy Decomp areas\n");
274
275 fcnlnode = bl2_create_reserved_memory();
276
277 bl2_lossy_setting(0, LOSSY_ST_ADDR0, LOSSY_END_ADDR0,
278 LOSSY_FMT0, LOSSY_ENA_DIS0, fcnlnode);
279 bl2_lossy_setting(1, LOSSY_ST_ADDR1, LOSSY_END_ADDR1,
280 LOSSY_FMT1, LOSSY_ENA_DIS1, fcnlnode);
281 bl2_lossy_setting(2, LOSSY_ST_ADDR2, LOSSY_END_ADDR2,
282 LOSSY_FMT2, LOSSY_ENA_DIS2, fcnlnode);
283}
284#else
285static void bl2_create_fcnl_reserved_memory(void) {}
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200286#endif
287
288void bl2_plat_flush_bl31_params(void)
289{
290 uint32_t product_cut, product, cut;
291 uint32_t boot_dev, boot_cpu;
292 uint32_t lcs, reg, val;
293
294 reg = mmio_read_32(RCAR_MODEMR);
295 boot_dev = reg & MODEMR_BOOT_DEV_MASK;
296
297 if (boot_dev == MODEMR_BOOT_DEV_EMMC_25X1 ||
298 boot_dev == MODEMR_BOOT_DEV_EMMC_50X8)
299 emmc_terminate();
300
301 if ((reg & MODEMR_BOOT_CPU_MASK) != MODEMR_BOOT_CPU_CR7)
302 bl2_secure_setting();
303
304 reg = mmio_read_32(RCAR_PRR);
Marek Vasut9cadc782019-08-06 19:13:22 +0200305 product_cut = reg & (PRR_PRODUCT_MASK | PRR_CUT_MASK);
306 product = reg & PRR_PRODUCT_MASK;
307 cut = reg & PRR_CUT_MASK;
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200308
Marek Vasut9cadc782019-08-06 19:13:22 +0200309 if (product == PRR_PRODUCT_M3 && PRR_PRODUCT_30 > cut)
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200310 goto tlb;
311
Marek Vasut9cadc782019-08-06 19:13:22 +0200312 if (product == PRR_PRODUCT_H3 && PRR_PRODUCT_20 > cut)
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200313 goto tlb;
314
315 /* Disable MFIS write protection */
316 mmio_write_32(MFISWPCNTR, MFISWPCNTR_PASSWORD | 1);
317
318tlb:
319 reg = mmio_read_32(RCAR_MODEMR);
320 boot_cpu = reg & MODEMR_BOOT_CPU_MASK;
321 if (boot_cpu != MODEMR_BOOT_CPU_CA57 &&
322 boot_cpu != MODEMR_BOOT_CPU_CA53)
323 goto mmu;
324
Marek Vasut9cadc782019-08-06 19:13:22 +0200325 if (product_cut == PRR_PRODUCT_H3_CUT20) {
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200326 mmio_write_32(IPMMUVI0_IMSCTLR, IMSCTLR_DISCACHE);
327 mmio_write_32(IPMMUVI1_IMSCTLR, IMSCTLR_DISCACHE);
328 mmio_write_32(IPMMUPV0_IMSCTLR, IMSCTLR_DISCACHE);
329 mmio_write_32(IPMMUPV1_IMSCTLR, IMSCTLR_DISCACHE);
330 mmio_write_32(IPMMUPV2_IMSCTLR, IMSCTLR_DISCACHE);
331 mmio_write_32(IPMMUPV3_IMSCTLR, IMSCTLR_DISCACHE);
Marek Vasut9cadc782019-08-06 19:13:22 +0200332 } else if (product_cut == (PRR_PRODUCT_M3N | PRR_PRODUCT_10) ||
333 product_cut == (PRR_PRODUCT_M3N | PRR_PRODUCT_11)) {
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200334 mmio_write_32(IPMMUVI0_IMSCTLR, IMSCTLR_DISCACHE);
335 mmio_write_32(IPMMUPV0_IMSCTLR, IMSCTLR_DISCACHE);
Marek Vasut9cadc782019-08-06 19:13:22 +0200336 } else if ((product_cut == (PRR_PRODUCT_E3 | PRR_PRODUCT_10)) ||
337 (product_cut == (PRR_PRODUCT_E3 | PRR_PRODUCT_11))) {
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200338 mmio_write_32(IPMMUVI0_IMSCTLR, IMSCTLR_DISCACHE);
Marek Vasute6208012018-12-31 16:48:04 +0100339 mmio_write_32(IPMMUVP0_IMSCTLR, IMSCTLR_DISCACHE);
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200340 mmio_write_32(IPMMUPV0_IMSCTLR, IMSCTLR_DISCACHE);
341 }
342
Marek Vasut9cadc782019-08-06 19:13:22 +0200343 if (product_cut == (PRR_PRODUCT_H3_CUT20) ||
344 product_cut == (PRR_PRODUCT_M3N | PRR_PRODUCT_10) ||
345 product_cut == (PRR_PRODUCT_M3N | PRR_PRODUCT_11) ||
346 product_cut == (PRR_PRODUCT_E3 | PRR_PRODUCT_10)) {
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200347 mmio_write_32(IPMMUHC_IMSCTLR, IMSCTLR_DISCACHE);
348 mmio_write_32(IPMMURT_IMSCTLR, IMSCTLR_DISCACHE);
349 mmio_write_32(IPMMUMP_IMSCTLR, IMSCTLR_DISCACHE);
350
351 mmio_write_32(IPMMUDS0_IMSCTLR, IMSCTLR_DISCACHE);
352 mmio_write_32(IPMMUDS1_IMSCTLR, IMSCTLR_DISCACHE);
353 }
354
355mmu:
356 mmio_write_32(IPMMUMM_IMSCTLR, IPMMUMM_IMSCTLR_ENABLE);
357 mmio_write_32(IPMMUMM_IMAUXCTLR, IPMMUMM_IMAUXCTLR_NMERGE40_BIT);
358
359 val = rcar_rom_get_lcs(&lcs);
360 if (val) {
361 ERROR("BL2: Failed to get the LCS. (%d)\n", val);
362 panic();
363 }
364
365 if (lcs == LCS_SE)
366 mmio_clrbits_32(P_ARMREG_SEC_CTRL, P_ARMREG_SEC_CTRL_PROT);
367
368 rcar_swdt_release();
369 bl2_system_cpg_init();
370
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200371 /* Disable data cache (clean and invalidate) */
372 disable_mmu_el3();
Toshiyuki Ogasaharae91e7282021-07-12 18:46:47 +0900373#if RCAR_BL2_DCACHE == 1
374 dcsw_op_all(DCCISW);
375#endif
376 tlbialle3();
377 disable_mmu_icache_el3();
378 plat_invalidate_icache();
379 dsbsy();
380 isb();
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200381}
382
383static uint32_t is_ddr_backup_mode(void)
384{
385#if RCAR_SYSTEM_SUSPEND
386 static uint32_t reason = RCAR_COLD_BOOT;
387 static uint32_t once;
388
389#if PMIC_ROHM_BD9571 && RCAR_SYSTEM_RESET_KEEPON_DDR
390 uint8_t data;
391#endif
392 if (once)
393 return reason;
394
395 once = 1;
396 if ((mmio_read_32(GPIO_INDT) & GPIO_BKUP_TRG_SHIFT) == 0)
397 return reason;
398
399#if PMIC_ROHM_BD9571 && RCAR_SYSTEM_RESET_KEEPON_DDR
400 if (rcar_iic_dvfs_receive(PMIC, REG_KEEP10, &data)) {
401 ERROR("BL2: REG Keep10 READ ERROR.\n");
402 panic();
403 }
404
405 if (KEEP10_MAGIC != data)
406 reason = RCAR_WARM_BOOT;
407#else
408 reason = RCAR_WARM_BOOT;
409#endif
410 return reason;
411#else
412 return RCAR_COLD_BOOT;
413#endif
414}
415
Marek Vasutb25ee352021-02-13 19:09:29 +0100416#if RCAR_GEN3_BL33_GZIP == 1
417void bl2_plat_preload_setup(void)
418{
419 image_decompress_init(BL33_COMP_BASE, BL33_COMP_SIZE, gunzip);
420}
421#endif
422
Takuya Sakatacb9efd82021-12-01 13:42:54 +0900423static uint64_t check_secure_load_area(uintptr_t base, uint32_t size,
424 uintptr_t dest, uint32_t len)
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200425{
Takuya Sakatacb9efd82021-12-01 13:42:54 +0900426 uintptr_t free_end, requested_end;
Marek Vasutb25ee352021-02-13 19:09:29 +0100427
Takuya Sakatacb9efd82021-12-01 13:42:54 +0900428 /*
429 * Handle corner cases first.
430 *
431 * The order of the 2 tests is important, because if there's no space
432 * left (i.e. free_size == 0) but we don't ask for any memory
433 * (i.e. size == 0) then we should report that the memory is free.
434 */
435 if (len == 0U) {
436 WARN("BL2: load data size is zero\n");
437 return 0; /* A zero-byte region is always free */
Marek Vasutb25ee352021-02-13 19:09:29 +0100438 }
Takuya Sakatacb9efd82021-12-01 13:42:54 +0900439 if (size == 0U) {
440 goto err;
441 }
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200442
Takuya Sakatacb9efd82021-12-01 13:42:54 +0900443 /*
444 * Check that the end addresses don't overflow.
445 * If they do, consider that this memory region is not free, as this
446 * is an invalid scenario.
447 */
448 if (check_uptr_overflow(base, size - 1U)) {
449 goto err;
450 }
451 free_end = base + (size - 1U);
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200452
Takuya Sakatacb9efd82021-12-01 13:42:54 +0900453 if (check_uptr_overflow(dest, len - 1U)) {
454 goto err;
455 }
456 requested_end = dest + (len - 1U);
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200457
Takuya Sakatacb9efd82021-12-01 13:42:54 +0900458 /*
459 * Finally, check that the requested memory region lies within the free
460 * region.
461 */
462 if ((dest < base) || (requested_end > free_end)) {
463 goto err;
464 }
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200465
466 return 0;
Takuya Sakatacb9efd82021-12-01 13:42:54 +0900467
468err:
469 ERROR("BL2: load data is outside the loadable area.\n");
470 ERROR("BL2: dst=0x%lx, len=%d(0x%x)\n", dest, len, len);
471 return 1;
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200472}
473
Takuya Sakatacb9efd82021-12-01 13:42:54 +0900474static uint64_t rcar_get_dest_addr_from_cert(uint32_t certid, uintptr_t *dest,
475 uint32_t *len)
Toshiyuki Ogasahara04f16282019-12-13 14:43:52 +0900476{
Takuya Sakatacb9efd82021-12-01 13:42:54 +0900477 uint32_t cert;
Toshiyuki Ogasahara04f16282019-12-13 14:43:52 +0900478 int ret;
479
480 ret = rcar_get_certificate(certid, &cert);
481 if (ret) {
482 ERROR("%s : cert file load error", __func__);
483 return 1;
484 }
485
Takuya Sakatacb9efd82021-12-01 13:42:54 +0900486 rcar_read_certificate((uint64_t) cert, len, dest);
Toshiyuki Ogasahara04f16282019-12-13 14:43:52 +0900487
488 return 0;
489}
490
Takuya Sakatacb9efd82021-12-01 13:42:54 +0900491int bl2_plat_handle_pre_image_load(unsigned int image_id)
492{
493 u_register_t *boot_kind = (void *) BOOT_KIND_BASE;
494 bl_mem_params_node_t *bl_mem_params;
495 uintptr_t dev_handle;
496 uintptr_t image_spec;
497 uintptr_t dest;
498 uint32_t len;
499 uint64_t ui64_ret;
500 int iret;
501
502 bl_mem_params = get_bl_mem_params_node(image_id);
503 if (bl_mem_params == NULL) {
504 ERROR("BL2: Failed to get loading parameter.\n");
505 return 1;
506 }
507
508 switch (image_id) {
509 case BL31_IMAGE_ID:
510 if (is_ddr_backup_mode() == RCAR_COLD_BOOT) {
511 iret = plat_get_image_source(image_id, &dev_handle,
512 &image_spec);
513 if (iret != 0) {
514 return 1;
515 }
516
517 ui64_ret = rcar_get_dest_addr_from_cert(
518 SOC_FW_CONTENT_CERT_ID, &dest, &len);
519 if (ui64_ret != 0U) {
520 return 1;
521 }
522
523 ui64_ret = check_secure_load_area(
524 BL31_BASE, BL31_LIMIT - BL31_BASE,
525 dest, len);
526 if (ui64_ret != 0U) {
527 return 1;
528 }
529
530 *boot_kind = RCAR_COLD_BOOT;
531 flush_dcache_range(BOOT_KIND_BASE, sizeof(*boot_kind));
532
533 bl_mem_params->image_info.image_base = dest;
534 bl_mem_params->image_info.image_size = len;
535 } else {
536 *boot_kind = RCAR_WARM_BOOT;
537 flush_dcache_range(BOOT_KIND_BASE, sizeof(*boot_kind));
538
539 console_flush();
540 bl2_plat_flush_bl31_params();
541
542 /* will not return */
543 bl2_enter_bl31(&bl_mem_params->ep_info);
544 }
545
546 return 0;
547#ifndef SPD_NONE
548 case BL32_IMAGE_ID:
549 ui64_ret = rcar_get_dest_addr_from_cert(
550 TRUSTED_OS_FW_CONTENT_CERT_ID, &dest, &len);
551 if (ui64_ret != 0U) {
552 return 1;
553 }
554
555 ui64_ret = check_secure_load_area(
556 BL32_BASE, BL32_LIMIT - BL32_BASE, dest, len);
557 if (ui64_ret != 0U) {
558 return 1;
559 }
560
561 bl_mem_params->image_info.image_base = dest;
562 bl_mem_params->image_info.image_size = len;
563
564 return 0;
565#endif
566 case BL33_IMAGE_ID:
567 /* case of image_id == BL33_IMAGE_ID */
568 ui64_ret = rcar_get_dest_addr_from_cert(
569 NON_TRUSTED_FW_CONTENT_CERT_ID,
570 &dest, &len);
571
572 if (ui64_ret != 0U) {
573 return 1;
574 }
575
576#if RCAR_GEN3_BL33_GZIP == 1
577 image_decompress_prepare(&bl_mem_params->image_info);
578#endif
579
580 return 0;
581 default:
582 return 1;
583 }
584
585 return 0;
586}
587
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200588int bl2_plat_handle_post_image_load(unsigned int image_id)
589{
590 static bl2_to_bl31_params_mem_t *params;
591 bl_mem_params_node_t *bl_mem_params;
592
593 if (!params) {
594 params = (bl2_to_bl31_params_mem_t *) PARAMS_BASE;
595 memset((void *)PARAMS_BASE, 0, sizeof(*params));
596 }
597
598 bl_mem_params = get_bl_mem_params_node(image_id);
Takuya Sakatacb9efd82021-12-01 13:42:54 +0900599 if (!bl_mem_params) {
600 ERROR("BL2: Failed to get loading parameter.\n");
601 return 1;
602 }
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200603
604 switch (image_id) {
605 case BL31_IMAGE_ID:
Takuya Sakatacb9efd82021-12-01 13:42:54 +0900606 bl_mem_params->ep_info.pc = bl_mem_params->image_info.image_base;
607 return 0;
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200608 case BL32_IMAGE_ID:
Takuya Sakatacb9efd82021-12-01 13:42:54 +0900609 bl_mem_params->ep_info.pc = bl_mem_params->image_info.image_base;
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200610 memcpy(&params->bl32_ep_info, &bl_mem_params->ep_info,
611 sizeof(entry_point_info_t));
Takuya Sakatacb9efd82021-12-01 13:42:54 +0900612 return 0;
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200613 case BL33_IMAGE_ID:
Marek Vasutb25ee352021-02-13 19:09:29 +0100614#if RCAR_GEN3_BL33_GZIP == 1
Takuya Sakatacb9efd82021-12-01 13:42:54 +0900615 int ret;
Marek Vasutb25ee352021-02-13 19:09:29 +0100616 if ((mmio_read_32(BL33_COMP_BASE) & 0xffff) == 0x8b1f) {
617 /* decompress gzip-compressed image */
618 ret = image_decompress(&bl_mem_params->image_info);
619 if (ret != 0) {
620 return ret;
621 }
622 } else {
623 /* plain image, copy it in place */
624 memcpy((void *)BL33_BASE, (void *)BL33_COMP_BASE,
625 bl_mem_params->image_info.image_size);
626 }
627#endif
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200628 memcpy(&params->bl33_ep_info, &bl_mem_params->ep_info,
629 sizeof(entry_point_info_t));
Takuya Sakatacb9efd82021-12-01 13:42:54 +0900630 return 0;
631 default:
632 return 1;
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200633 }
634
635 return 0;
636}
637
Marek Vasutc7077c62018-12-26 15:57:08 +0100638struct meminfo *bl2_plat_sec_mem_layout(void)
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +0200639{
640 return &bl2_tzram_layout;
641}
642
Justin Chadwell7d0e3ba2019-09-17 15:21:50 +0100643static void bl2_populate_compatible_string(void *dt)
Marek Vasuta987b002018-10-11 16:15:41 +0200644{
645 uint32_t board_type;
646 uint32_t board_rev;
647 uint32_t reg;
648 int ret;
649
Marek Vasut688251a2020-01-06 03:26:43 +0100650 fdt_setprop_u32(dt, 0, "#address-cells", 2);
651 fdt_setprop_u32(dt, 0, "#size-cells", 2);
652
Marek Vasuta987b002018-10-11 16:15:41 +0200653 /* Populate compatible string */
654 rcar_get_board_type(&board_type, &board_rev);
655 switch (board_type) {
656 case BOARD_SALVATOR_X:
Justin Chadwell7d0e3ba2019-09-17 15:21:50 +0100657 ret = fdt_setprop_string(dt, 0, "compatible",
Marek Vasuta987b002018-10-11 16:15:41 +0200658 "renesas,salvator-x");
659 break;
660 case BOARD_SALVATOR_XS:
Justin Chadwell7d0e3ba2019-09-17 15:21:50 +0100661 ret = fdt_setprop_string(dt, 0, "compatible",
Marek Vasuta987b002018-10-11 16:15:41 +0200662 "renesas,salvator-xs");
663 break;
664 case BOARD_STARTER_KIT:
Justin Chadwell7d0e3ba2019-09-17 15:21:50 +0100665 ret = fdt_setprop_string(dt, 0, "compatible",
Marek Vasuta987b002018-10-11 16:15:41 +0200666 "renesas,m3ulcb");
667 break;
668 case BOARD_STARTER_KIT_PRE:
Justin Chadwell7d0e3ba2019-09-17 15:21:50 +0100669 ret = fdt_setprop_string(dt, 0, "compatible",
Marek Vasuta987b002018-10-11 16:15:41 +0200670 "renesas,h3ulcb");
671 break;
Valentine Barshakf2184142018-10-30 02:06:17 +0300672 case BOARD_EAGLE:
Justin Chadwell7d0e3ba2019-09-17 15:21:50 +0100673 ret = fdt_setprop_string(dt, 0, "compatible",
Valentine Barshakf2184142018-10-30 02:06:17 +0300674 "renesas,eagle");
675 break;
Marek Vasuta987b002018-10-11 16:15:41 +0200676 case BOARD_EBISU:
677 case BOARD_EBISU_4D:
Justin Chadwell7d0e3ba2019-09-17 15:21:50 +0100678 ret = fdt_setprop_string(dt, 0, "compatible",
Marek Vasuta987b002018-10-11 16:15:41 +0200679 "renesas,ebisu");
680 break;
Marek Vasut4ae342c2019-01-05 13:56:03 +0100681 case BOARD_DRAAK:
Justin Chadwell7d0e3ba2019-09-17 15:21:50 +0100682 ret = fdt_setprop_string(dt, 0, "compatible",
Marek Vasut4ae342c2019-01-05 13:56:03 +0100683 "renesas,draak");
684 break;
Marek Vasuta987b002018-10-11 16:15:41 +0200685 default:
686 NOTICE("BL2: Cannot set compatible string, board unsupported\n");
687 panic();
688 }
689
690 if (ret < 0) {
691 NOTICE("BL2: Cannot set compatible string (ret=%i)\n", ret);
692 panic();
693 }
694
695 reg = mmio_read_32(RCAR_PRR);
Marek Vasut9cadc782019-08-06 19:13:22 +0200696 switch (reg & PRR_PRODUCT_MASK) {
697 case PRR_PRODUCT_H3:
Justin Chadwell7d0e3ba2019-09-17 15:21:50 +0100698 ret = fdt_appendprop_string(dt, 0, "compatible",
Marek Vasuta987b002018-10-11 16:15:41 +0200699 "renesas,r8a7795");
700 break;
Marek Vasut9cadc782019-08-06 19:13:22 +0200701 case PRR_PRODUCT_M3:
Justin Chadwell7d0e3ba2019-09-17 15:21:50 +0100702 ret = fdt_appendprop_string(dt, 0, "compatible",
Marek Vasuta987b002018-10-11 16:15:41 +0200703 "renesas,r8a7796");
704 break;
Marek Vasut9cadc782019-08-06 19:13:22 +0200705 case PRR_PRODUCT_M3N:
Justin Chadwell7d0e3ba2019-09-17 15:21:50 +0100706 ret = fdt_appendprop_string(dt, 0, "compatible",
Marek Vasuta987b002018-10-11 16:15:41 +0200707 "renesas,r8a77965");
708 break;
Marek Vasut9cadc782019-08-06 19:13:22 +0200709 case PRR_PRODUCT_V3M:
Justin Chadwell7d0e3ba2019-09-17 15:21:50 +0100710 ret = fdt_appendprop_string(dt, 0, "compatible",
Valentine Barshakf2184142018-10-30 02:06:17 +0300711 "renesas,r8a77970");
712 break;
Marek Vasut9cadc782019-08-06 19:13:22 +0200713 case PRR_PRODUCT_E3:
Justin Chadwell7d0e3ba2019-09-17 15:21:50 +0100714 ret = fdt_appendprop_string(dt, 0, "compatible",
Marek Vasuta987b002018-10-11 16:15:41 +0200715 "renesas,r8a77990");
716 break;
Marek Vasut9cadc782019-08-06 19:13:22 +0200717 case PRR_PRODUCT_D3:
Justin Chadwell7d0e3ba2019-09-17 15:21:50 +0100718 ret = fdt_appendprop_string(dt, 0, "compatible",
Marek Vasut4ae342c2019-01-05 13:56:03 +0100719 "renesas,r8a77995");
720 break;
Marek Vasuta987b002018-10-11 16:15:41 +0200721 default:
722 NOTICE("BL2: Cannot set compatible string, SoC unsupported\n");
723 panic();
724 }
725
726 if (ret < 0) {
727 NOTICE("BL2: Cannot set compatible string (ret=%i)\n", ret);
728 panic();
729 }
730}
731
Marek Vasute7618e72021-07-10 17:59:05 +0200732static void bl2_add_rpc_node(void)
733{
734#if (RCAR_RPC_HYPERFLASH_LOCKED == 0)
735 int ret, node;
736
737 node = ret = fdt_add_subnode(fdt, 0, "soc");
738 if (ret < 0) {
739 goto err;
740 }
741
Geert Uytterhoeven5480bd12022-03-23 14:21:31 +0100742 node = ret = fdt_add_subnode(fdt, node, "spi@ee200000");
Marek Vasute7618e72021-07-10 17:59:05 +0200743 if (ret < 0) {
744 goto err;
745 }
746
747 ret = fdt_setprop_string(fdt, node, "status", "okay");
748 if (ret < 0) {
749 goto err;
750 }
751
752 return;
753err:
754 NOTICE("BL2: Cannot add RPC node to FDT (ret=%i)\n", ret);
755 panic();
756#endif
757}
758
Marek Vasut9bbee5b2024-06-02 21:09:59 +0200759static void bl2_add_kaslr_seed(void)
760{
761 uint32_t cnt, isr, prr;
762 uint64_t seed;
763 int ret, node;
764
765 /* SCEG is only available on H3/M3-W/M3-N */
766 prr = mmio_read_32(RCAR_PRR);
767 switch (prr & PRR_PRODUCT_MASK) {
768 case PRR_PRODUCT_H3:
769 case PRR_PRODUCT_M3:
770 case PRR_PRODUCT_M3N:
771 break;
772 default:
773 return;
774 }
775
776 mmio_write_32(RCAR_CC63_BASE + CC63_TRNG_SW_RESET_REG_ADDR,
777 CC63_TRNG_SW_RESET_REG_SET);
778
779 do {
780 mmio_write_32(RCAR_CC63_BASE + CC63_TRNG_CLK_ENABLE_REG_ADDR,
781 CC63_TRNG_CLK_ENABLE_REG_SET);
782 mmio_write_32(RCAR_CC63_BASE + CC63_TRNG_SAMPLE_CNT1_REG_ADDR,
783 CC63_TRNG_SAMPLE_CNT1_REG_SAMPLE_COUNT);
784 cnt = mmio_read_32(RCAR_CC63_BASE + CC63_TRNG_SAMPLE_CNT1_REG_ADDR);
785 } while (cnt != CC63_TRNG_SAMPLE_CNT1_REG_SAMPLE_COUNT);
786
787 mmio_write_32(RCAR_CC63_BASE + CC63_TRNG_CONFIG_REG_ADDR,
788 CC63_TRNG_CONFIG_REG_ROSC_MAX_LENGTH);
789 mmio_write_32(RCAR_CC63_BASE + CC63_TRNG_DEBUG_CONTROL_REG_ADDR,
790 CC63_TRNG_DEBUG_CONTROL_REG_80090B);
791 mmio_write_32(RCAR_CC63_BASE + CC63_TRNG_SOURCE_ENABLE_REG_ADDR,
792 CC63_TRNG_SOURCE_ENABLE_REG_SET);
793
794 do {
795 isr = mmio_read_32(RCAR_CC63_BASE + CC63_TRNG_ISR_REG_ADDR);
796 if ((isr & CC63_TRNG_ISR_REG_AUTOCORR_ERR) != 0U) {
797 panic();
798 }
799 } while ((isr & CC63_TRNG_ISR_REG_EHR_VALID) == 0U);
800
801 mmio_write_32(RCAR_CC63_BASE + CC63_TRNG_ICR_REG_ADDR, UINT32_MAX);
802 seed = mmio_read_64(RCAR_CC63_BASE + CC63_TRNG_EHR_DATA_ADDR_0_REG_ADDR);
803 mmio_write_32(RCAR_CC63_BASE + CC63_TRNG_SOURCE_ENABLE_REG_ADDR,
804 CC63_TRNG_SOURCE_ENABLE_REG_CLR);
805
806 node = ret = fdt_add_subnode(fdt, 0, "chosen");
807 if (ret < 0) {
808 goto err;
809 }
810
811 ret = fdt_setprop_u64(fdt, node, "kaslr-seed", seed);
812 if (ret < 0) {
813 goto err;
814 }
815
816 return;
817err:
818 NOTICE("BL2: Cannot add KASLR seed to FDT (ret=%i)\n", ret);
819 panic();
820}
821
Marek Vasut5ca408a2021-04-16 21:25:27 +0200822static void bl2_add_dram_entry(uint64_t start, uint64_t size)
Marek Vasut6a6881a2018-10-02 20:43:09 +0200823{
Marek Vasut93c85fc2018-10-02 20:45:18 +0200824 char nodename[32] = { 0 };
Marek Vasut93c85fc2018-10-02 20:45:18 +0200825 uint64_t fdtsize;
Marek Vasut5ca408a2021-04-16 21:25:27 +0200826 int ret, node;
827
828 fdtsize = cpu_to_fdt64(size);
829
830 snprintf(nodename, sizeof(nodename), "memory@");
831 unsigned_num_print(start, 16, nodename + strlen(nodename));
832 node = ret = fdt_add_subnode(fdt, 0, nodename);
833 if (ret < 0) {
834 goto err;
835 }
836
837 ret = fdt_setprop_string(fdt, node, "device_type", "memory");
838 if (ret < 0) {
839 goto err;
840 }
841
842 ret = fdt_setprop_u64(fdt, node, "reg", start);
843 if (ret < 0) {
844 goto err;
845 }
846
847 ret = fdt_appendprop(fdt, node, "reg", &fdtsize,
848 sizeof(fdtsize));
849 if (ret < 0) {
850 goto err;
851 }
852
853 return;
854err:
Scott Brandene5dcf982020-08-25 13:49:32 -0700855 NOTICE("BL2: Cannot add memory node [%" PRIx64 " - %" PRIx64 "] to FDT (ret=%i)\n",
Marek Vasut5ca408a2021-04-16 21:25:27 +0200856 start, start + size - 1, ret);
857 panic();
858}
859
860static void bl2_advertise_dram_entries(uint64_t dram_config[8])
861{
Marek Vasut9601d5a2021-04-16 21:39:36 +0200862 uint64_t start, size, size32;
Marek Vasut5ca408a2021-04-16 21:25:27 +0200863 int chan;
Marek Vasut6a6881a2018-10-02 20:43:09 +0200864
865 for (chan = 0; chan < 4; chan++) {
866 start = dram_config[2 * chan];
867 size = dram_config[2 * chan + 1];
868 if (!size)
869 continue;
870
Scott Brandene5dcf982020-08-25 13:49:32 -0700871 NOTICE("BL2: CH%d: %" PRIx64 " - %" PRIx64 ", %" PRId64 " %siB\n",
Marek Vasut89c17512019-03-30 04:01:41 +0100872 chan, start, start + size - 1,
873 (size >> 30) ? : size >> 20,
874 (size >> 30) ? "G" : "M");
Marek Vasut6a6881a2018-10-02 20:43:09 +0200875 }
Marek Vasut93c85fc2018-10-02 20:45:18 +0200876
877 /*
878 * We add the DT nodes in reverse order here. The fdt_add_subnode()
879 * adds the DT node before the first existing DT node, so we have
880 * to add them in reverse order to get nodes sorted by address in
881 * the resulting DT.
882 */
883 for (chan = 3; chan >= 0; chan--) {
884 start = dram_config[2 * chan];
885 size = dram_config[2 * chan + 1];
886 if (!size)
887 continue;
888
889 /*
890 * Channel 0 is mapped in 32bit space and the first
Marek Vasut9601d5a2021-04-16 21:39:36 +0200891 * 128 MiB are reserved and the maximum size is 2GiB.
Marek Vasut93c85fc2018-10-02 20:45:18 +0200892 */
893 if (chan == 0) {
Marek Vasut9601d5a2021-04-16 21:39:36 +0200894 /* Limit the 32bit entry to 2 GiB - 128 MiB */
895 size32 = size - 0x8000000U;
896 if (size32 >= 0x78000000U) {
897 size32 = 0x78000000U;
898 }
899
900 /* Emit 32bit entry, up to 2 GiB - 128 MiB long. */
901 bl2_add_dram_entry(0x48000000, size32);
902
903 /*
904 * If channel 0 is less than 2 GiB long, the
905 * entire memory fits into the 32bit space entry,
906 * so move on to the next channel.
907 */
908 if (size <= 0x80000000U) {
909 continue;
910 }
911
912 /*
913 * If channel 0 is more than 2 GiB long, emit
914 * another entry which covers the rest of the
915 * memory in channel 0, in the 64bit space.
916 *
917 * Start of this new entry is at 2 GiB offset
918 * from the beginning of the 64bit channel 0
919 * address, size is 2 GiB shorter than total
920 * size of the channel.
921 */
922 start += 0x80000000U;
923 size -= 0x80000000U;
Marek Vasut93c85fc2018-10-02 20:45:18 +0200924 }
925
Marek Vasut5ca408a2021-04-16 21:25:27 +0200926 bl2_add_dram_entry(start, size);
Marek Vasut93c85fc2018-10-02 20:45:18 +0200927 }
Marek Vasut6a6881a2018-10-02 20:43:09 +0200928}
929
Marek Vasutb0e13592018-10-02 14:53:27 +0200930static void bl2_advertise_dram_size(uint32_t product)
Marek Vasut673bc322018-10-02 13:33:32 +0200931{
Marek Vasut6a6881a2018-10-02 20:43:09 +0200932 uint64_t dram_config[8] = {
933 [0] = 0x400000000ULL,
934 [2] = 0x500000000ULL,
935 [4] = 0x600000000ULL,
936 [6] = 0x700000000ULL,
937 };
Toshiyuki Ogasahara58ff4b22021-07-12 19:05:06 +0900938 uint32_t cut = mmio_read_32(RCAR_PRR) & PRR_CUT_MASK;
Marek Vasut6a6881a2018-10-02 20:43:09 +0200939
Marek Vasut9963f702018-10-02 15:09:04 +0200940 switch (product) {
Marek Vasut9cadc782019-08-06 19:13:22 +0200941 case PRR_PRODUCT_H3:
Marek Vasut673bc322018-10-02 13:33:32 +0200942#if (RCAR_DRAM_LPDDR4_MEMCONF == 0)
943 /* 4GB(1GBx4) */
Marek Vasut6a6881a2018-10-02 20:43:09 +0200944 dram_config[1] = 0x40000000ULL;
945 dram_config[3] = 0x40000000ULL;
946 dram_config[5] = 0x40000000ULL;
947 dram_config[7] = 0x40000000ULL;
Marek Vasut673bc322018-10-02 13:33:32 +0200948#elif (RCAR_DRAM_LPDDR4_MEMCONF == 1) && \
949 (RCAR_DRAM_CHANNEL == 5) && \
950 (RCAR_DRAM_SPLIT == 2)
951 /* 4GB(2GBx2 2ch split) */
Marek Vasut6a6881a2018-10-02 20:43:09 +0200952 dram_config[1] = 0x80000000ULL;
953 dram_config[3] = 0x80000000ULL;
Marek Vasut673bc322018-10-02 13:33:32 +0200954#elif (RCAR_DRAM_LPDDR4_MEMCONF == 1) && (RCAR_DRAM_CHANNEL == 15)
955 /* 8GB(2GBx4: default) */
Marek Vasut6a6881a2018-10-02 20:43:09 +0200956 dram_config[1] = 0x80000000ULL;
957 dram_config[3] = 0x80000000ULL;
958 dram_config[5] = 0x80000000ULL;
959 dram_config[7] = 0x80000000ULL;
Marek Vasut673bc322018-10-02 13:33:32 +0200960#endif /* RCAR_DRAM_LPDDR4_MEMCONF == 0 */
Marek Vasut9963f702018-10-02 15:09:04 +0200961 break;
Marek Vasut673bc322018-10-02 13:33:32 +0200962
Marek Vasut9cadc782019-08-06 19:13:22 +0200963 case PRR_PRODUCT_M3:
Toshiyuki Ogasahara58ff4b22021-07-12 19:05:06 +0900964 if (cut < PRR_PRODUCT_30) {
Marek Vasut0208c942019-03-09 16:10:59 +0100965#if (RCAR_GEN3_ULCB == 1)
Toshiyuki Ogasahara58ff4b22021-07-12 19:05:06 +0900966 /* 2GB(1GBx2 2ch split) */
967 dram_config[1] = 0x40000000ULL;
968 dram_config[5] = 0x40000000ULL;
Marek Vasut0208c942019-03-09 16:10:59 +0100969#else
Toshiyuki Ogasahara58ff4b22021-07-12 19:05:06 +0900970 /* 4GB(2GBx2 2ch split) */
971 dram_config[1] = 0x80000000ULL;
972 dram_config[5] = 0x80000000ULL;
Marek Vasut0208c942019-03-09 16:10:59 +0100973#endif
Toshiyuki Ogasahara58ff4b22021-07-12 19:05:06 +0900974 } else {
975 /* 8GB(2GBx4 2ch split) */
976 dram_config[1] = 0x100000000ULL;
977 dram_config[5] = 0x100000000ULL;
978 }
Marek Vasut9963f702018-10-02 15:09:04 +0200979 break;
980
Marek Vasut9cadc782019-08-06 19:13:22 +0200981 case PRR_PRODUCT_M3N:
Toshiyuki Ogasahara67e19522020-12-15 18:22:16 +0900982#if (RCAR_DRAM_LPDDR4_MEMCONF == 2)
983 /* 4GB(4GBx1) */
984 dram_config[1] = 0x100000000ULL;
985#elif (RCAR_DRAM_LPDDR4_MEMCONF == 1)
Marek Vasut9963f702018-10-02 15:09:04 +0200986 /* 2GB(1GBx2) */
Marek Vasut6a6881a2018-10-02 20:43:09 +0200987 dram_config[1] = 0x80000000ULL;
Toshiyuki Ogasahara67e19522020-12-15 18:22:16 +0900988#endif
Marek Vasut9963f702018-10-02 15:09:04 +0200989 break;
990
Marek Vasut9cadc782019-08-06 19:13:22 +0200991 case PRR_PRODUCT_V3M:
Valentine Barshakf2184142018-10-30 02:06:17 +0300992 /* 1GB(512MBx2) */
993 dram_config[1] = 0x40000000ULL;
994 break;
995
Marek Vasut9cadc782019-08-06 19:13:22 +0200996 case PRR_PRODUCT_E3:
Marek Vasut673bc322018-10-02 13:33:32 +0200997#if (RCAR_DRAM_DDR3L_MEMCONF == 0)
998 /* 1GB(512MBx2) */
Marek Vasut6a6881a2018-10-02 20:43:09 +0200999 dram_config[1] = 0x40000000ULL;
Marek Vasut673bc322018-10-02 13:33:32 +02001000#elif (RCAR_DRAM_DDR3L_MEMCONF == 1)
1001 /* 2GB(512MBx4) */
Marek Vasut6a6881a2018-10-02 20:43:09 +02001002 dram_config[1] = 0x80000000ULL;
Marek Vasut8cb12ec2018-10-02 13:51:19 +02001003#elif (RCAR_DRAM_DDR3L_MEMCONF == 2)
1004 /* 4GB(1GBx4) */
Marek Vasut6a6881a2018-10-02 20:43:09 +02001005 dram_config[1] = 0x100000000ULL;
Marek Vasut673bc322018-10-02 13:33:32 +02001006#endif /* RCAR_DRAM_DDR3L_MEMCONF == 0 */
Marek Vasut9963f702018-10-02 15:09:04 +02001007 break;
Marek Vasut4ae342c2019-01-05 13:56:03 +01001008
Marek Vasut9cadc782019-08-06 19:13:22 +02001009 case PRR_PRODUCT_D3:
Marek Vasut4ae342c2019-01-05 13:56:03 +01001010 /* 512MB */
1011 dram_config[1] = 0x20000000ULL;
1012 break;
Marek Vasut673bc322018-10-02 13:33:32 +02001013 }
Marek Vasut6a6881a2018-10-02 20:43:09 +02001014
1015 bl2_advertise_dram_entries(dram_config);
Marek Vasut673bc322018-10-02 13:33:32 +02001016}
1017
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02001018void bl2_el3_early_platform_setup(u_register_t arg1, u_register_t arg2,
1019 u_register_t arg3, u_register_t arg4)
1020{
1021 uint32_t reg, midr, lcs, boot_dev, boot_cpu, sscg, type, rev;
Marek Vasutb0e13592018-10-02 14:53:27 +02001022 uint32_t product, product_cut, major, minor;
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02001023 int32_t ret;
1024 const char *str;
1025 const char *unknown = "unknown";
1026 const char *cpu_ca57 = "CA57";
1027 const char *cpu_ca53 = "CA53";
1028 const char *product_m3n = "M3N";
1029 const char *product_h3 = "H3";
1030 const char *product_m3 = "M3";
1031 const char *product_e3 = "E3";
Marek Vasut4ae342c2019-01-05 13:56:03 +01001032 const char *product_d3 = "D3";
Valentine Barshakf2184142018-10-30 02:06:17 +03001033 const char *product_v3m = "V3M";
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02001034 const char *lcs_secure = "SE";
1035 const char *lcs_cm = "CM";
1036 const char *lcs_dm = "DM";
1037 const char *lcs_sd = "SD";
1038 const char *lcs_fa = "FA";
1039 const char *sscg_off = "PLL1 nonSSCG Clock select";
1040 const char *sscg_on = "PLL1 SSCG Clock select";
1041 const char *boot_hyper80 = "HyperFlash(80MHz)";
1042 const char *boot_qspi40 = "QSPI Flash(40MHz)";
1043 const char *boot_qspi80 = "QSPI Flash(80MHz)";
1044 const char *boot_emmc25x1 = "eMMC(25MHz x1)";
1045 const char *boot_emmc50x8 = "eMMC(50MHz x8)";
Marek Vasut4ae342c2019-01-05 13:56:03 +01001046#if (RCAR_LSI == RCAR_E3) || (RCAR_LSI == RCAR_D3)
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02001047 const char *boot_hyper160 = "HyperFlash(150MHz)";
1048#else
1049 const char *boot_hyper160 = "HyperFlash(160MHz)";
1050#endif
1051
Marek Vasut1eca7782018-12-28 20:12:13 +01001052 bl2_init_generic_timer();
1053
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02001054 reg = mmio_read_32(RCAR_MODEMR);
1055 boot_dev = reg & MODEMR_BOOT_DEV_MASK;
1056 boot_cpu = reg & MODEMR_BOOT_CPU_MASK;
1057
1058 bl2_cpg_init();
1059
1060 if (boot_cpu == MODEMR_BOOT_CPU_CA57 ||
1061 boot_cpu == MODEMR_BOOT_CPU_CA53) {
1062 rcar_pfc_init();
Marek Vasut0aa268e2019-05-18 19:29:16 +02001063 rcar_console_boot_init();
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02001064 }
1065
1066 plat_rcar_gic_driver_init();
1067 plat_rcar_gic_init();
1068 rcar_swdt_init();
1069
1070 /* FIQ interrupts are taken to EL3 */
1071 write_scr_el3(read_scr_el3() | SCR_FIQ_BIT);
1072
1073 write_daifclr(DAIF_FIQ_BIT);
1074
1075 reg = read_midr();
1076 midr = reg & (MIDR_PN_MASK << MIDR_PN_SHIFT);
1077 switch (midr) {
1078 case MIDR_CA57:
1079 str = cpu_ca57;
1080 break;
1081 case MIDR_CA53:
1082 str = cpu_ca53;
1083 break;
1084 default:
1085 str = unknown;
1086 break;
1087 }
1088
1089 NOTICE("BL2: R-Car Gen3 Initial Program Loader(%s) Rev.%s\n", str,
1090 version_of_renesas);
1091
1092 reg = mmio_read_32(RCAR_PRR);
Marek Vasut9cadc782019-08-06 19:13:22 +02001093 product_cut = reg & (PRR_PRODUCT_MASK | PRR_CUT_MASK);
1094 product = reg & PRR_PRODUCT_MASK;
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02001095
1096 switch (product) {
Marek Vasut9cadc782019-08-06 19:13:22 +02001097 case PRR_PRODUCT_H3:
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02001098 str = product_h3;
1099 break;
Marek Vasut9cadc782019-08-06 19:13:22 +02001100 case PRR_PRODUCT_M3:
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02001101 str = product_m3;
1102 break;
Marek Vasut9cadc782019-08-06 19:13:22 +02001103 case PRR_PRODUCT_M3N:
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02001104 str = product_m3n;
1105 break;
Marek Vasut9cadc782019-08-06 19:13:22 +02001106 case PRR_PRODUCT_V3M:
Valentine Barshakf2184142018-10-30 02:06:17 +03001107 str = product_v3m;
1108 break;
Marek Vasut9cadc782019-08-06 19:13:22 +02001109 case PRR_PRODUCT_E3:
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02001110 str = product_e3;
1111 break;
Marek Vasut9cadc782019-08-06 19:13:22 +02001112 case PRR_PRODUCT_D3:
Marek Vasut4ae342c2019-01-05 13:56:03 +01001113 str = product_d3;
1114 break;
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02001115 default:
1116 str = unknown;
1117 break;
1118 }
1119
Marek Vasut9cadc782019-08-06 19:13:22 +02001120 if ((PRR_PRODUCT_M3 == product) &&
1121 (PRR_PRODUCT_20 == (reg & RCAR_MAJOR_MASK))) {
1122 if (RCAR_M3_CUT_VER11 == (reg & PRR_CUT_MASK)) {
Marek Vasut3af20052019-02-25 14:57:08 +01001123 /* M3 Ver.1.1 or Ver.1.2 */
1124 NOTICE("BL2: PRR is R-Car %s Ver.1.1 / Ver.1.2\n",
1125 str);
1126 } else {
1127 NOTICE("BL2: PRR is R-Car %s Ver.1.%d\n",
1128 str,
1129 (reg & RCAR_MINOR_MASK) + RCAR_M3_MINOR_OFFSET);
1130 }
Toshiyuki Ogasaharac3c52272021-07-12 19:18:57 +09001131 } else if (product == PRR_PRODUCT_D3) {
1132 if (RCAR_D3_CUT_VER10 == (reg & PRR_CUT_MASK)) {
1133 NOTICE("BL2: PRR is R-Car %s Ver.1.0\n", str);
1134 } else if (RCAR_D3_CUT_VER11 == (reg & PRR_CUT_MASK)) {
1135 NOTICE("BL2: PRR is R-Car %s Ver.1.1\n", str);
1136 } else {
1137 NOTICE("BL2: PRR is R-Car %s Ver.X.X\n", str);
1138 }
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02001139 } else {
1140 major = (reg & RCAR_MAJOR_MASK) >> RCAR_MAJOR_SHIFT;
1141 major = major + RCAR_MAJOR_OFFSET;
1142 minor = reg & RCAR_MINOR_MASK;
1143 NOTICE("BL2: PRR is R-Car %s Ver.%d.%d\n", str, major, minor);
1144 }
1145
Toshiyuki Ogasahara9be13a92021-07-12 19:19:39 +09001146 if (PRR_PRODUCT_E3 == product || PRR_PRODUCT_D3 == product) {
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02001147 reg = mmio_read_32(RCAR_MODEMR);
1148 sscg = reg & RCAR_SSCG_MASK;
1149 str = sscg == RCAR_SSCG_ENABLE ? sscg_on : sscg_off;
1150 NOTICE("BL2: %s\n", str);
1151 }
1152
1153 rcar_get_board_type(&type, &rev);
1154
1155 switch (type) {
1156 case BOARD_SALVATOR_X:
1157 case BOARD_KRIEK:
1158 case BOARD_STARTER_KIT:
1159 case BOARD_SALVATOR_XS:
1160 case BOARD_EBISU:
1161 case BOARD_STARTER_KIT_PRE:
1162 case BOARD_EBISU_4D:
Marek Vasut4ae342c2019-01-05 13:56:03 +01001163 case BOARD_DRAAK:
Valentine Barshakf2184142018-10-30 02:06:17 +03001164 case BOARD_EAGLE:
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02001165 break;
1166 default:
1167 type = BOARD_UNKNOWN;
1168 break;
1169 }
1170
1171 if (type == BOARD_UNKNOWN || rev == BOARD_REV_UNKNOWN)
1172 NOTICE("BL2: Board is %s Rev.---\n", GET_BOARD_NAME(type));
1173 else {
1174 NOTICE("BL2: Board is %s Rev.%d.%d\n",
1175 GET_BOARD_NAME(type),
1176 GET_BOARD_MAJOR(rev), GET_BOARD_MINOR(rev));
1177 }
1178
1179#if RCAR_LSI != RCAR_AUTO
1180 if (product != TARGET_PRODUCT) {
1181 ERROR("BL2: IPL was been built for the %s.\n", TARGET_NAME);
1182 ERROR("BL2: Please write the correct IPL to flash memory.\n");
1183 panic();
1184 }
1185#endif
1186 rcar_avs_init();
1187 rcar_avs_setting();
1188
1189 switch (boot_dev) {
1190 case MODEMR_BOOT_DEV_HYPERFLASH160:
1191 str = boot_hyper160;
1192 break;
1193 case MODEMR_BOOT_DEV_HYPERFLASH80:
1194 str = boot_hyper80;
1195 break;
1196 case MODEMR_BOOT_DEV_QSPI_FLASH40:
1197 str = boot_qspi40;
1198 break;
1199 case MODEMR_BOOT_DEV_QSPI_FLASH80:
1200 str = boot_qspi80;
1201 break;
1202 case MODEMR_BOOT_DEV_EMMC_25X1:
Marek Vasut4ae342c2019-01-05 13:56:03 +01001203#if RCAR_LSI == RCAR_D3
1204 ERROR("BL2: Failed to Initialize. eMMC is not supported.\n");
1205 panic();
1206#endif
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02001207 str = boot_emmc25x1;
1208 break;
1209 case MODEMR_BOOT_DEV_EMMC_50X8:
1210 str = boot_emmc50x8;
1211 break;
1212 default:
1213 str = unknown;
1214 break;
1215 }
1216 NOTICE("BL2: Boot device is %s\n", str);
1217
1218 rcar_avs_setting();
1219 reg = rcar_rom_get_lcs(&lcs);
1220 if (reg) {
1221 str = unknown;
1222 goto lcm_state;
1223 }
1224
1225 switch (lcs) {
1226 case LCS_CM:
1227 str = lcs_cm;
1228 break;
1229 case LCS_DM:
1230 str = lcs_dm;
1231 break;
1232 case LCS_SD:
1233 str = lcs_sd;
1234 break;
1235 case LCS_SE:
1236 str = lcs_secure;
1237 break;
1238 case LCS_FA:
1239 str = lcs_fa;
1240 break;
1241 default:
1242 str = unknown;
1243 break;
1244 }
1245
1246lcm_state:
1247 NOTICE("BL2: LCM state is %s\n", str);
1248
1249 rcar_avs_end();
1250 is_ddr_backup_mode();
1251
1252 bl2_tzram_layout.total_base = BL31_BASE;
1253 bl2_tzram_layout.total_size = BL31_LIMIT - BL31_BASE;
1254
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02001255 if (boot_cpu == MODEMR_BOOT_CPU_CA57 ||
1256 boot_cpu == MODEMR_BOOT_CPU_CA53) {
1257 ret = rcar_dram_init();
1258 if (ret) {
1259 NOTICE("BL2: Failed to DRAM initialize (%d).\n", ret);
1260 panic();
1261 }
1262 rcar_qos_init();
1263 }
1264
Marek Vasut93c85fc2018-10-02 20:45:18 +02001265 /* Set up FDT */
1266 ret = fdt_create_empty_tree(fdt, sizeof(fdt_blob));
1267 if (ret) {
1268 NOTICE("BL2: Cannot allocate FDT for U-Boot (ret=%i)\n", ret);
1269 panic();
1270 }
1271
Marek Vasuta987b002018-10-11 16:15:41 +02001272 /* Add platform compatible string */
1273 bl2_populate_compatible_string(fdt);
1274
Marek Vasute7618e72021-07-10 17:59:05 +02001275 /* Enable RPC if unlocked */
1276 bl2_add_rpc_node();
1277
Marek Vasut63659fd2018-10-02 15:12:15 +02001278 /* Print DRAM layout */
1279 bl2_advertise_dram_size(product);
1280
Marek Vasut9bbee5b2024-06-02 21:09:59 +02001281 /* Add KASLR seed */
1282 bl2_add_kaslr_seed();
1283
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02001284 if (boot_dev == MODEMR_BOOT_DEV_EMMC_25X1 ||
1285 boot_dev == MODEMR_BOOT_DEV_EMMC_50X8) {
1286 if (rcar_emmc_init() != EMMC_SUCCESS) {
1287 NOTICE("BL2: Failed to eMMC driver initialize.\n");
1288 panic();
1289 }
1290 rcar_emmc_memcard_power(EMMC_POWER_ON);
1291 if (rcar_emmc_mount() != EMMC_SUCCESS) {
1292 NOTICE("BL2: Failed to eMMC mount operation.\n");
1293 panic();
1294 }
1295 } else {
1296 rcar_rpc_init();
1297 rcar_dma_init();
1298 }
1299
1300 reg = mmio_read_32(RST_WDTRSTCR);
1301 reg &= ~WDTRSTCR_RWDT_RSTMSK;
1302 reg |= WDTRSTCR_PASSWORD;
1303 mmio_write_32(RST_WDTRSTCR, reg);
1304
1305 mmio_write_32(CPG_CPGWPR, CPGWPR_PASSWORD);
1306 mmio_write_32(CPG_CPGWPCR, CPGWPCR_PASSWORD);
1307
1308 reg = mmio_read_32(RCAR_PRR);
1309 if ((reg & RCAR_CPU_MASK_CA57) == RCAR_CPU_HAVE_CA57)
1310 mmio_write_32(CPG_CA57DBGRCR,
1311 DBGCPUPREN | mmio_read_32(CPG_CA57DBGRCR));
1312
1313 if ((reg & RCAR_CPU_MASK_CA53) == RCAR_CPU_HAVE_CA53)
1314 mmio_write_32(CPG_CA53DBGRCR,
1315 DBGCPUPREN | mmio_read_32(CPG_CA53DBGRCR));
1316
Marek Vasut9cadc782019-08-06 19:13:22 +02001317 if (product_cut == PRR_PRODUCT_H3_CUT10) {
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02001318 reg = mmio_read_32(CPG_PLL2CR);
1319 reg &= ~((uint32_t) 1 << 5);
1320 mmio_write_32(CPG_PLL2CR, reg);
1321
1322 reg = mmio_read_32(CPG_PLL4CR);
1323 reg &= ~((uint32_t) 1 << 5);
1324 mmio_write_32(CPG_PLL4CR, reg);
1325
1326 reg = mmio_read_32(CPG_PLL0CR);
1327 reg &= ~((uint32_t) 1 << 12);
1328 mmio_write_32(CPG_PLL0CR, reg);
1329 }
Marek Vasut4d693c22018-10-11 16:53:58 +02001330
Detlev Casanova8348acd2022-12-01 17:57:31 -05001331 bl2_create_fcnl_reserved_memory();
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02001332
Marek Vasut93c85fc2018-10-02 20:45:18 +02001333 fdt_pack(fdt);
1334 NOTICE("BL2: FDT at %p\n", fdt);
1335
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02001336 if (boot_dev == MODEMR_BOOT_DEV_EMMC_25X1 ||
1337 boot_dev == MODEMR_BOOT_DEV_EMMC_50X8)
1338 rcar_io_emmc_setup();
1339 else
1340 rcar_io_setup();
1341}
1342
1343void bl2_el3_plat_arch_setup(void)
1344{
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02001345 rcar_configure_mmu_el3(BL2_BASE,
Marek Vasut2e032c02018-12-26 15:57:08 +01001346 BL2_END - BL2_BASE,
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02001347 BL2_RO_BASE, BL2_RO_LIMIT
1348#if USE_COHERENT_MEM
1349 , BL2_COHERENT_RAM_BASE, BL2_COHERENT_RAM_LIMIT
1350#endif
1351 );
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02001352}
1353
Toshiyuki Ogasahara22c44b92021-07-12 18:39:52 +09001354void bl2_el3_plat_prepare_exit(void)
1355{
1356 bl2_ram_security_setting_finish();
1357}
1358
Jorge Ramirez-Ortizbf084dc2018-09-23 09:36:13 +02001359void bl2_platform_setup(void)
1360{
1361
1362}
Marek Vasut1eca7782018-12-28 20:12:13 +01001363
1364static void bl2_init_generic_timer(void)
1365{
Valentine Barshakf2184142018-10-30 02:06:17 +03001366/* FIXME: V3M 16.666 MHz ? */
Marek Vasut4ae342c2019-01-05 13:56:03 +01001367#if RCAR_LSI == RCAR_D3
1368 uint32_t reg_cntfid = EXTAL_DRAAK;
1369#elif RCAR_LSI == RCAR_E3
Marek Vasut1eca7782018-12-28 20:12:13 +01001370 uint32_t reg_cntfid = EXTAL_EBISU;
1371#else /* RCAR_LSI == RCAR_E3 */
1372 uint32_t reg;
1373 uint32_t reg_cntfid;
1374 uint32_t modemr;
1375 uint32_t modemr_pll;
1376 uint32_t board_type;
1377 uint32_t board_rev;
1378 uint32_t pll_table[] = {
1379 EXTAL_MD14_MD13_TYPE_0, /* MD14/MD13 : 0b00 */
1380 EXTAL_MD14_MD13_TYPE_1, /* MD14/MD13 : 0b01 */
1381 EXTAL_MD14_MD13_TYPE_2, /* MD14/MD13 : 0b10 */
1382 EXTAL_MD14_MD13_TYPE_3 /* MD14/MD13 : 0b11 */
1383 };
1384
1385 modemr = mmio_read_32(RCAR_MODEMR);
1386 modemr_pll = (modemr & MODEMR_BOOT_PLL_MASK);
1387
1388 /* Set frequency data in CNTFID0 */
1389 reg_cntfid = pll_table[modemr_pll >> MODEMR_BOOT_PLL_SHIFT];
Marek Vasut9cadc782019-08-06 19:13:22 +02001390 reg = mmio_read_32(RCAR_PRR) & (PRR_PRODUCT_MASK | PRR_CUT_MASK);
Marek Vasut1eca7782018-12-28 20:12:13 +01001391 switch (modemr_pll) {
1392 case MD14_MD13_TYPE_0:
1393 rcar_get_board_type(&board_type, &board_rev);
1394 if (BOARD_SALVATOR_XS == board_type) {
1395 reg_cntfid = EXTAL_SALVATOR_XS;
1396 }
1397 break;
1398 case MD14_MD13_TYPE_3:
Marek Vasut9cadc782019-08-06 19:13:22 +02001399 if (PRR_PRODUCT_H3_CUT10 == reg) {
Marek Vasut1eca7782018-12-28 20:12:13 +01001400 reg_cntfid = reg_cntfid >> 1U;
1401 }
1402 break;
1403 default:
1404 /* none */
1405 break;
1406 }
1407#endif /* RCAR_LSI == RCAR_E3 */
Elyes Haouas2be03c02023-02-13 09:14:48 +01001408 /* Update memory mapped and register based frequency */
Marek Vasut1eca7782018-12-28 20:12:13 +01001409 write_cntfrq_el0((u_register_t )reg_cntfid);
1410 mmio_write_32(ARM_SYS_CNTCTL_BASE + (uintptr_t)CNTFID_OFF, reg_cntfid);
1411 /* Enable counter */
1412 mmio_setbits_32(RCAR_CNTC_BASE + (uintptr_t)CNTCR_OFF,
1413 (uint32_t)CNTCR_EN);
1414}