xref: /rk3399_ARM-atf/plat/renesas/rcar/bl2_plat_setup.c (revision 5b4f022be4f0c72709e3a526574de1d79b385a4c)
1 /*
2  * Copyright (c) 2018, Renesas Electronics Corporation. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <string.h>
8 
9 #include <libfdt.h>
10 
11 #include <platform_def.h>
12 
13 #include <arch_helpers.h>
14 #include <bl1/bl1.h>
15 #include <common/bl_common.h>
16 #include <common/debug.h>
17 #include <common/desc_image_load.h>
18 #include <drivers/console.h>
19 #include <lib/mmio.h>
20 #include <lib/xlat_tables/xlat_tables_defs.h>
21 #include <plat/common/platform.h>
22 
23 #include "avs_driver.h"
24 #include "boot_init_dram.h"
25 #include "cpg_registers.h"
26 #include "board.h"
27 #include "emmc_def.h"
28 #include "emmc_hal.h"
29 #include "emmc_std.h"
30 
31 #if PMIC_ROHM_BD9571 && RCAR_SYSTEM_RESET_KEEPON_DDR
32 #include "iic_dvfs.h"
33 #endif
34 
35 #include "io_common.h"
36 #include "qos_init.h"
37 #include "rcar_def.h"
38 #include "rcar_private.h"
39 #include "rcar_version.h"
40 #include "rom_api.h"
41 
42 IMPORT_SYM(unsigned long, __RO_START__, BL2_RO_BASE)
43 IMPORT_SYM(unsigned long, __RO_END__, BL2_RO_LIMIT)
44 
45 #if USE_COHERENT_MEM
46 IMPORT_SYM(unsigned long, __COHERENT_RAM_START__, BL2_COHERENT_RAM_BASE)
47 IMPORT_SYM(unsigned long, __COHERENT_RAM_END__, BL2_COHERENT_RAM_LIMIT)
48 #endif
49 
50 extern void plat_rcar_gic_driver_init(void);
51 extern void plat_rcar_gic_init(void);
52 extern void bl2_enter_bl31(const struct entry_point_info *bl_ep_info);
53 extern void bl2_system_cpg_init(void);
54 extern void bl2_secure_setting(void);
55 extern void bl2_cpg_init(void);
56 extern void rcar_io_emmc_setup(void);
57 extern void rcar_io_setup(void);
58 extern void rcar_swdt_release(void);
59 extern void rcar_swdt_init(void);
60 extern void rcar_rpc_init(void);
61 extern void rcar_pfc_init(void);
62 extern void rcar_dma_init(void);
63 
64 static void bl2_init_generic_timer(void);
65 
66 /* R-Car Gen3 product check */
67 #if (RCAR_LSI == RCAR_H3) || (RCAR_LSI == RCAR_H3N)
68 #define TARGET_PRODUCT			RCAR_PRODUCT_H3
69 #define TARGET_NAME			"R-Car H3"
70 #elif RCAR_LSI == RCAR_M3
71 #define TARGET_PRODUCT			RCAR_PRODUCT_M3
72 #define TARGET_NAME			"R-Car M3"
73 #elif RCAR_LSI == RCAR_M3N
74 #define TARGET_PRODUCT			RCAR_PRODUCT_M3N
75 #define TARGET_NAME			"R-Car M3N"
76 #elif RCAR_LSI == RCAR_E3
77 #define TARGET_PRODUCT			RCAR_PRODUCT_E3
78 #define TARGET_NAME			"R-Car E3"
79 #elif RCAR_LSI == RCAR_AUTO
80 #define TARGET_NAME			"R-Car H3/M3/M3N"
81 #endif
82 
83 #if (RCAR_LSI == RCAR_E3)
84 #define GPIO_INDT			(GPIO_INDT6)
85 #define GPIO_BKUP_TRG_SHIFT		((uint32_t)1U<<13U)
86 #else
87 #define GPIO_INDT			(GPIO_INDT1)
88 #define GPIO_BKUP_TRG_SHIFT		((uint32_t)1U<<8U)
89 #endif
90 
91 CASSERT((PARAMS_BASE + sizeof(bl2_to_bl31_params_mem_t) + 0x100)
92 	 < (RCAR_SHARED_MEM_BASE + RCAR_SHARED_MEM_SIZE),
93 	assert_bl31_params_do_not_fit_in_shared_memory);
94 
95 static meminfo_t bl2_tzram_layout __aligned(CACHE_WRITEBACK_GRANULE);
96 
97 /* FDT with DRAM configuration */
98 uint64_t fdt_blob[PAGE_SIZE_4KB / sizeof(uint64_t)];
99 static void *fdt = (void *)fdt_blob;
100 
101 static void unsigned_num_print(unsigned long long int unum, unsigned int radix,
102 				char *string)
103 {
104 	/* Just need enough space to store 64 bit decimal integer */
105 	char num_buf[20];
106 	int i = 0;
107 	unsigned int rem;
108 
109 	do {
110 		rem = unum % radix;
111 		if (rem < 0xa)
112 			num_buf[i] = '0' + rem;
113 		else
114 			num_buf[i] = 'a' + (rem - 0xa);
115 		i++;
116 		unum /= radix;
117 	} while (unum > 0U);
118 
119 	while (--i >= 0)
120 		*string++ = num_buf[i];
121 }
122 
123 #if (RCAR_LOSSY_ENABLE == 1)
124 typedef struct bl2_lossy_info {
125 	uint32_t magic;
126 	uint32_t a0;
127 	uint32_t b0;
128 } bl2_lossy_info_t;
129 
130 static void bl2_lossy_gen_fdt(uint32_t no, uint64_t start_addr,
131 			      uint64_t end_addr, uint32_t format,
132 			      uint32_t enable, int fcnlnode)
133 {
134 	const uint64_t fcnlsize = cpu_to_fdt64(end_addr - start_addr);
135 	char nodename[40] = { 0 };
136 	int ret, node;
137 
138 	/* Ignore undefined addresses */
139 	if (start_addr == 0 && end_addr == 0)
140 		return;
141 
142 	snprintf(nodename, sizeof(nodename), "lossy-decompression@");
143 	unsigned_num_print(start_addr, 16, nodename + strlen(nodename));
144 
145 	node = ret = fdt_add_subnode(fdt, fcnlnode, nodename);
146 	if (ret < 0) {
147 		NOTICE("BL2: Cannot create FCNL node (ret=%i)\n", ret);
148 		panic();
149 	}
150 
151 	ret = fdt_setprop_string(fdt, node, "compatible",
152 				 "renesas,lossy-decompression");
153 	if (ret < 0) {
154 		NOTICE("BL2: Cannot add FCNL compat string (ret=%i)\n", ret);
155 		panic();
156 	}
157 
158 	ret = fdt_appendprop_string(fdt, node, "compatible",
159 				    "shared-dma-pool");
160 	if (ret < 0) {
161 		NOTICE("BL2: Cannot append FCNL compat string (ret=%i)\n", ret);
162 		panic();
163 	}
164 
165 	ret = fdt_setprop_u64(fdt, node, "reg", start_addr);
166 	if (ret < 0) {
167 		NOTICE("BL2: Cannot add FCNL reg prop (ret=%i)\n", ret);
168 		panic();
169 	}
170 
171 	ret = fdt_appendprop(fdt, node, "reg", &fcnlsize, sizeof(fcnlsize));
172 	if (ret < 0) {
173 		NOTICE("BL2: Cannot append FCNL reg size prop (ret=%i)\n", ret);
174 		panic();
175 	}
176 
177 	ret = fdt_setprop(fdt, node, "no-map", NULL, 0);
178 	if (ret < 0) {
179 		NOTICE("BL2: Cannot add FCNL no-map prop (ret=%i)\n", ret);
180 		panic();
181 	}
182 
183 	ret = fdt_setprop_u32(fdt, node, "renesas,formats", format);
184 	if (ret < 0) {
185 		NOTICE("BL2: Cannot add FCNL formats prop (ret=%i)\n", ret);
186 		panic();
187 	}
188 }
189 
190 static void bl2_lossy_setting(uint32_t no, uint64_t start_addr,
191 			      uint64_t end_addr, uint32_t format,
192 			      uint32_t enable, int fcnlnode)
193 {
194 	bl2_lossy_info_t info;
195 	uint32_t reg;
196 
197 	bl2_lossy_gen_fdt(no, start_addr, end_addr, format, enable, fcnlnode);
198 
199 	reg = format | (start_addr >> 20);
200 	mmio_write_32(AXI_DCMPAREACRA0 + 0x8 * no, reg);
201 	mmio_write_32(AXI_DCMPAREACRB0 + 0x8 * no, end_addr >> 20);
202 	mmio_write_32(AXI_DCMPAREACRA0 + 0x8 * no, reg | enable);
203 
204 	info.magic = 0x12345678U;
205 	info.a0 = mmio_read_32(AXI_DCMPAREACRA0 + 0x8 * no);
206 	info.b0 = mmio_read_32(AXI_DCMPAREACRB0 + 0x8 * no);
207 
208 	mmio_write_32(LOSSY_PARAMS_BASE + sizeof(info) * no, info.magic);
209 	mmio_write_32(LOSSY_PARAMS_BASE + sizeof(info) * no + 0x4, info.a0);
210 	mmio_write_32(LOSSY_PARAMS_BASE + sizeof(info) * no + 0x8, info.b0);
211 
212 	NOTICE("     Entry %d: DCMPAREACRAx:0x%x DCMPAREACRBx:0x%x\n", no,
213 	       mmio_read_32(AXI_DCMPAREACRA0 + 0x8 * no),
214 	       mmio_read_32(AXI_DCMPAREACRB0 + 0x8 * no));
215 }
216 #endif
217 
218 void bl2_plat_flush_bl31_params(void)
219 {
220 	uint32_t product_cut, product, cut;
221 	uint32_t boot_dev, boot_cpu;
222 	uint32_t lcs, reg, val;
223 
224 	reg = mmio_read_32(RCAR_MODEMR);
225 	boot_dev = reg & MODEMR_BOOT_DEV_MASK;
226 
227 	if (boot_dev == MODEMR_BOOT_DEV_EMMC_25X1 ||
228 	    boot_dev == MODEMR_BOOT_DEV_EMMC_50X8)
229 		emmc_terminate();
230 
231 	if ((reg & MODEMR_BOOT_CPU_MASK) != MODEMR_BOOT_CPU_CR7)
232 		bl2_secure_setting();
233 
234 	reg = mmio_read_32(RCAR_PRR);
235 	product_cut = reg & (RCAR_PRODUCT_MASK | RCAR_CUT_MASK);
236 	product = reg & RCAR_PRODUCT_MASK;
237 	cut = reg & RCAR_CUT_MASK;
238 
239 	if (product == RCAR_PRODUCT_M3 && RCAR_CUT_VER30 > cut)
240 		goto tlb;
241 
242 	if (product == RCAR_PRODUCT_H3 && RCAR_CUT_VER20 > cut)
243 		goto tlb;
244 
245 	/* Disable MFIS write protection */
246 	mmio_write_32(MFISWPCNTR, MFISWPCNTR_PASSWORD | 1);
247 
248 tlb:
249 	reg = mmio_read_32(RCAR_MODEMR);
250 	boot_cpu = reg & MODEMR_BOOT_CPU_MASK;
251 	if (boot_cpu != MODEMR_BOOT_CPU_CA57 &&
252 	    boot_cpu != MODEMR_BOOT_CPU_CA53)
253 		goto mmu;
254 
255 	if (product_cut == RCAR_PRODUCT_H3_CUT20) {
256 		mmio_write_32(IPMMUVI0_IMSCTLR, IMSCTLR_DISCACHE);
257 		mmio_write_32(IPMMUVI1_IMSCTLR, IMSCTLR_DISCACHE);
258 		mmio_write_32(IPMMUPV0_IMSCTLR, IMSCTLR_DISCACHE);
259 		mmio_write_32(IPMMUPV1_IMSCTLR, IMSCTLR_DISCACHE);
260 		mmio_write_32(IPMMUPV2_IMSCTLR, IMSCTLR_DISCACHE);
261 		mmio_write_32(IPMMUPV3_IMSCTLR, IMSCTLR_DISCACHE);
262 	} else if (product_cut == (RCAR_PRODUCT_M3N | RCAR_CUT_VER10) ||
263 		   product_cut == (RCAR_PRODUCT_M3N | RCAR_CUT_VER11)) {
264 		mmio_write_32(IPMMUVI0_IMSCTLR, IMSCTLR_DISCACHE);
265 		mmio_write_32(IPMMUPV0_IMSCTLR, IMSCTLR_DISCACHE);
266 	} else if ((product_cut == (RCAR_PRODUCT_E3 | RCAR_CUT_VER10)) ||
267 		   (product_cut == (RCAR_PRODUCT_E3 | RCAR_CUT_VER11))) {
268 		mmio_write_32(IPMMUVI0_IMSCTLR, IMSCTLR_DISCACHE);
269 		mmio_write_32(IPMMUVP0_IMSCTLR, IMSCTLR_DISCACHE);
270 		mmio_write_32(IPMMUPV0_IMSCTLR, IMSCTLR_DISCACHE);
271 	}
272 
273 	if (product_cut == (RCAR_PRODUCT_H3_CUT20) ||
274 	    product_cut == (RCAR_PRODUCT_M3N | RCAR_CUT_VER10) ||
275 	    product_cut == (RCAR_PRODUCT_M3N | RCAR_CUT_VER11) ||
276 	    product_cut == (RCAR_PRODUCT_E3 | RCAR_CUT_VER10)) {
277 		mmio_write_32(IPMMUHC_IMSCTLR, IMSCTLR_DISCACHE);
278 		mmio_write_32(IPMMURT_IMSCTLR, IMSCTLR_DISCACHE);
279 		mmio_write_32(IPMMUMP_IMSCTLR, IMSCTLR_DISCACHE);
280 
281 		mmio_write_32(IPMMUDS0_IMSCTLR, IMSCTLR_DISCACHE);
282 		mmio_write_32(IPMMUDS1_IMSCTLR, IMSCTLR_DISCACHE);
283 	}
284 
285 mmu:
286 	mmio_write_32(IPMMUMM_IMSCTLR, IPMMUMM_IMSCTLR_ENABLE);
287 	mmio_write_32(IPMMUMM_IMAUXCTLR, IPMMUMM_IMAUXCTLR_NMERGE40_BIT);
288 
289 	val = rcar_rom_get_lcs(&lcs);
290 	if (val) {
291 		ERROR("BL2: Failed to get the LCS. (%d)\n", val);
292 		panic();
293 	}
294 
295 	if (lcs == LCS_SE)
296 		mmio_clrbits_32(P_ARMREG_SEC_CTRL, P_ARMREG_SEC_CTRL_PROT);
297 
298 	rcar_swdt_release();
299 	bl2_system_cpg_init();
300 
301 #if RCAR_BL2_DCACHE == 1
302 	/* Disable data cache (clean and invalidate) */
303 	disable_mmu_el3();
304 #endif
305 }
306 
307 static uint32_t is_ddr_backup_mode(void)
308 {
309 #if RCAR_SYSTEM_SUSPEND
310 	static uint32_t reason = RCAR_COLD_BOOT;
311 	static uint32_t once;
312 
313 #if PMIC_ROHM_BD9571 && RCAR_SYSTEM_RESET_KEEPON_DDR
314 	uint8_t data;
315 #endif
316 	if (once)
317 		return reason;
318 
319 	once = 1;
320 	if ((mmio_read_32(GPIO_INDT) & GPIO_BKUP_TRG_SHIFT) == 0)
321 		return reason;
322 
323 #if PMIC_ROHM_BD9571 && RCAR_SYSTEM_RESET_KEEPON_DDR
324 	if (rcar_iic_dvfs_receive(PMIC, REG_KEEP10, &data)) {
325 		ERROR("BL2: REG Keep10 READ ERROR.\n");
326 		panic();
327 	}
328 
329 	if (KEEP10_MAGIC != data)
330 		reason = RCAR_WARM_BOOT;
331 #else
332 	reason = RCAR_WARM_BOOT;
333 #endif
334 	return reason;
335 #else
336 	return RCAR_COLD_BOOT;
337 #endif
338 }
339 
340 int bl2_plat_handle_pre_image_load(unsigned int image_id)
341 {
342 	u_register_t *boot_kind = (void *) BOOT_KIND_BASE;
343 	bl_mem_params_node_t *bl_mem_params;
344 
345 	if (image_id != BL31_IMAGE_ID)
346 		return 0;
347 
348 	bl_mem_params = get_bl_mem_params_node(image_id);
349 
350 	if (is_ddr_backup_mode() == RCAR_COLD_BOOT)
351 		goto cold_boot;
352 
353 	*boot_kind  = RCAR_WARM_BOOT;
354 	flush_dcache_range(BOOT_KIND_BASE, sizeof(*boot_kind));
355 
356 	console_flush();
357 	bl2_plat_flush_bl31_params();
358 
359 	/* will not return */
360 	bl2_enter_bl31(&bl_mem_params->ep_info);
361 
362 cold_boot:
363 	*boot_kind  = RCAR_COLD_BOOT;
364 	flush_dcache_range(BOOT_KIND_BASE, sizeof(*boot_kind));
365 
366 	return 0;
367 }
368 
369 int bl2_plat_handle_post_image_load(unsigned int image_id)
370 {
371 	static bl2_to_bl31_params_mem_t *params;
372 	bl_mem_params_node_t *bl_mem_params;
373 
374 	if (!params) {
375 		params = (bl2_to_bl31_params_mem_t *) PARAMS_BASE;
376 		memset((void *)PARAMS_BASE, 0, sizeof(*params));
377 	}
378 
379 	bl_mem_params = get_bl_mem_params_node(image_id);
380 
381 	switch (image_id) {
382 	case BL31_IMAGE_ID:
383 		break;
384 	case BL32_IMAGE_ID:
385 		memcpy(&params->bl32_ep_info, &bl_mem_params->ep_info,
386 			sizeof(entry_point_info_t));
387 		break;
388 	case BL33_IMAGE_ID:
389 		memcpy(&params->bl33_ep_info, &bl_mem_params->ep_info,
390 			sizeof(entry_point_info_t));
391 		break;
392 	}
393 
394 	return 0;
395 }
396 
397 struct meminfo *bl2_plat_sec_mem_layout(void)
398 {
399 	return &bl2_tzram_layout;
400 }
401 
402 static void bl2_populate_compatible_string(void *fdt)
403 {
404 	uint32_t board_type;
405 	uint32_t board_rev;
406 	uint32_t reg;
407 	int ret;
408 
409 	/* Populate compatible string */
410 	rcar_get_board_type(&board_type, &board_rev);
411 	switch (board_type) {
412 	case BOARD_SALVATOR_X:
413 		ret = fdt_setprop_string(fdt, 0, "compatible",
414 					 "renesas,salvator-x");
415 		break;
416 	case BOARD_SALVATOR_XS:
417 		ret = fdt_setprop_string(fdt, 0, "compatible",
418 					 "renesas,salvator-xs");
419 		break;
420 	case BOARD_STARTER_KIT:
421 		ret = fdt_setprop_string(fdt, 0, "compatible",
422 					 "renesas,m3ulcb");
423 		break;
424 	case BOARD_STARTER_KIT_PRE:
425 		ret = fdt_setprop_string(fdt, 0, "compatible",
426 					 "renesas,h3ulcb");
427 		break;
428 	case BOARD_EBISU:
429 	case BOARD_EBISU_4D:
430 		ret = fdt_setprop_string(fdt, 0, "compatible",
431 					 "renesas,ebisu");
432 		break;
433 	default:
434 		NOTICE("BL2: Cannot set compatible string, board unsupported\n");
435 		panic();
436 	}
437 
438 	if (ret < 0) {
439 		NOTICE("BL2: Cannot set compatible string (ret=%i)\n", ret);
440 		panic();
441 	}
442 
443 	reg = mmio_read_32(RCAR_PRR);
444 	switch (reg & RCAR_PRODUCT_MASK) {
445 	case RCAR_PRODUCT_H3:
446 		ret = fdt_appendprop_string(fdt, 0, "compatible",
447 					    "renesas,r8a7795");
448 		break;
449 	case RCAR_PRODUCT_M3:
450 		ret = fdt_appendprop_string(fdt, 0, "compatible",
451 					    "renesas,r8a7796");
452 		break;
453 	case RCAR_PRODUCT_M3N:
454 		ret = fdt_appendprop_string(fdt, 0, "compatible",
455 					    "renesas,r8a77965");
456 		break;
457 	case RCAR_PRODUCT_E3:
458 		ret = fdt_appendprop_string(fdt, 0, "compatible",
459 					    "renesas,r8a77990");
460 		break;
461 	default:
462 		NOTICE("BL2: Cannot set compatible string, SoC unsupported\n");
463 		panic();
464 	}
465 
466 	if (ret < 0) {
467 		NOTICE("BL2: Cannot set compatible string (ret=%i)\n", ret);
468 		panic();
469 	}
470 }
471 
472 static void bl2_advertise_dram_entries(uint64_t dram_config[8])
473 {
474 	char nodename[32] = { 0 };
475 	uint64_t start, size;
476 	uint64_t fdtsize;
477 	int ret, node, chan;
478 
479 	for (chan = 0; chan < 4; chan++) {
480 		start = dram_config[2 * chan];
481 		size = dram_config[2 * chan + 1];
482 		if (!size)
483 			continue;
484 
485 		NOTICE("BL2: CH%d: %llx - %llx, %lld %siB\n",
486 			chan, start, start + size - 1,
487 			(size >> 30) ? : size >> 20,
488 			(size >> 30) ? "G" : "M");
489 	}
490 
491 	/*
492 	 * We add the DT nodes in reverse order here. The fdt_add_subnode()
493 	 * adds the DT node before the first existing DT node, so we have
494 	 * to add them in reverse order to get nodes sorted by address in
495 	 * the resulting DT.
496 	 */
497 	for (chan = 3; chan >= 0; chan--) {
498 		start = dram_config[2 * chan];
499 		size = dram_config[2 * chan + 1];
500 		if (!size)
501 			continue;
502 
503 		/*
504 		 * Channel 0 is mapped in 32bit space and the first
505 		 * 128 MiB are reserved
506 		 */
507 		if (chan == 0) {
508 			start = 0x48000000;
509 			size -= 0x8000000;
510 		}
511 
512 		fdtsize = cpu_to_fdt64(size);
513 
514 		snprintf(nodename, sizeof(nodename), "memory@");
515 		unsigned_num_print(start, 16, nodename + strlen(nodename));
516 		node = ret = fdt_add_subnode(fdt, 0, nodename);
517 		if (ret < 0)
518 			goto err;
519 
520 		ret = fdt_setprop_string(fdt, node, "device_type", "memory");
521 		if (ret < 0)
522 			goto err;
523 
524 		ret = fdt_setprop_u64(fdt, node, "reg", start);
525 		if (ret < 0)
526 			goto err;
527 
528 		ret = fdt_appendprop(fdt, node, "reg", &fdtsize,
529 				     sizeof(fdtsize));
530 		if (ret < 0)
531 			goto err;
532 	}
533 
534 	return;
535 err:
536 	NOTICE("BL2: Cannot add memory node to FDT (ret=%i)\n", ret);
537 	panic();
538 }
539 
540 static void bl2_advertise_dram_size(uint32_t product)
541 {
542 	uint64_t dram_config[8] = {
543 		[0] = 0x400000000ULL,
544 		[2] = 0x500000000ULL,
545 		[4] = 0x600000000ULL,
546 		[6] = 0x700000000ULL,
547 	};
548 
549 	switch (product) {
550 	case RCAR_PRODUCT_H3:
551 #if (RCAR_DRAM_LPDDR4_MEMCONF == 0)
552 		/* 4GB(1GBx4) */
553 		dram_config[1] = 0x40000000ULL;
554 		dram_config[3] = 0x40000000ULL;
555 		dram_config[5] = 0x40000000ULL;
556 		dram_config[7] = 0x40000000ULL;
557 #elif (RCAR_DRAM_LPDDR4_MEMCONF == 1) && \
558       (RCAR_DRAM_CHANNEL        == 5) && \
559       (RCAR_DRAM_SPLIT          == 2)
560 		/* 4GB(2GBx2 2ch split) */
561 		dram_config[1] = 0x80000000ULL;
562 		dram_config[3] = 0x80000000ULL;
563 #elif (RCAR_DRAM_LPDDR4_MEMCONF == 1) && (RCAR_DRAM_CHANNEL == 15)
564 		/* 8GB(2GBx4: default) */
565 		dram_config[1] = 0x80000000ULL;
566 		dram_config[3] = 0x80000000ULL;
567 		dram_config[5] = 0x80000000ULL;
568 		dram_config[7] = 0x80000000ULL;
569 #endif /* RCAR_DRAM_LPDDR4_MEMCONF == 0 */
570 		break;
571 
572 	case RCAR_PRODUCT_M3:
573 #if (RCAR_GEN3_ULCB == 1)
574 		/* 2GB(1GBx2 2ch split) */
575 		dram_config[1] = 0x40000000ULL;
576 		dram_config[5] = 0x40000000ULL;
577 #else
578 		/* 4GB(2GBx2 2ch split) */
579 		dram_config[1] = 0x80000000ULL;
580 		dram_config[5] = 0x80000000ULL;
581 #endif
582 		break;
583 
584 	case RCAR_PRODUCT_M3N:
585 		/* 2GB(1GBx2) */
586 		dram_config[1] = 0x80000000ULL;
587 		break;
588 
589 	case RCAR_PRODUCT_E3:
590 #if (RCAR_DRAM_DDR3L_MEMCONF == 0)
591 		/* 1GB(512MBx2) */
592 		dram_config[1] = 0x40000000ULL;
593 #elif (RCAR_DRAM_DDR3L_MEMCONF == 1)
594 		/* 2GB(512MBx4) */
595 		dram_config[1] = 0x80000000ULL;
596 #elif (RCAR_DRAM_DDR3L_MEMCONF == 2)
597 		/* 4GB(1GBx4) */
598 		dram_config[1] = 0x100000000ULL;
599 #endif /* RCAR_DRAM_DDR3L_MEMCONF == 0 */
600 		break;
601 	}
602 
603 	bl2_advertise_dram_entries(dram_config);
604 }
605 
606 void bl2_el3_early_platform_setup(u_register_t arg1, u_register_t arg2,
607 				  u_register_t arg3, u_register_t arg4)
608 {
609 	uint32_t reg, midr, lcs, boot_dev, boot_cpu, sscg, type, rev;
610 	uint32_t product, product_cut, major, minor;
611 	int32_t ret;
612 	const char *str;
613 	const char *unknown = "unknown";
614 	const char *cpu_ca57 = "CA57";
615 	const char *cpu_ca53 = "CA53";
616 	const char *product_m3n = "M3N";
617 	const char *product_h3 = "H3";
618 	const char *product_m3 = "M3";
619 	const char *product_e3 = "E3";
620 	const char *lcs_secure = "SE";
621 	const char *lcs_cm = "CM";
622 	const char *lcs_dm = "DM";
623 	const char *lcs_sd = "SD";
624 	const char *lcs_fa = "FA";
625 	const char *sscg_off = "PLL1 nonSSCG Clock select";
626 	const char *sscg_on = "PLL1 SSCG Clock select";
627 	const char *boot_hyper80 = "HyperFlash(80MHz)";
628 	const char *boot_qspi40 = "QSPI Flash(40MHz)";
629 	const char *boot_qspi80 = "QSPI Flash(80MHz)";
630 	const char *boot_emmc25x1 = "eMMC(25MHz x1)";
631 	const char *boot_emmc50x8 = "eMMC(50MHz x8)";
632 #if RCAR_LSI == RCAR_E3
633 	const char *boot_hyper160 = "HyperFlash(150MHz)";
634 #else
635 	const char *boot_hyper160 = "HyperFlash(160MHz)";
636 #endif
637 #if (RCAR_LOSSY_ENABLE == 1)
638 	int fcnlnode;
639 #endif
640 
641 	bl2_init_generic_timer();
642 
643 	reg = mmio_read_32(RCAR_MODEMR);
644 	boot_dev = reg & MODEMR_BOOT_DEV_MASK;
645 	boot_cpu = reg & MODEMR_BOOT_CPU_MASK;
646 
647 	bl2_cpg_init();
648 
649 	if (boot_cpu == MODEMR_BOOT_CPU_CA57 ||
650 	    boot_cpu == MODEMR_BOOT_CPU_CA53) {
651 		rcar_pfc_init();
652 		/* console configuration (platform specific) done in driver */
653 		console_init(0, 0, 0);
654 	}
655 
656 	plat_rcar_gic_driver_init();
657 	plat_rcar_gic_init();
658 	rcar_swdt_init();
659 
660 	/* FIQ interrupts are taken to EL3 */
661 	write_scr_el3(read_scr_el3() | SCR_FIQ_BIT);
662 
663 	write_daifclr(DAIF_FIQ_BIT);
664 
665 	reg = read_midr();
666 	midr = reg & (MIDR_PN_MASK << MIDR_PN_SHIFT);
667 	switch (midr) {
668 	case MIDR_CA57:
669 		str = cpu_ca57;
670 		break;
671 	case MIDR_CA53:
672 		str = cpu_ca53;
673 		break;
674 	default:
675 		str = unknown;
676 		break;
677 	}
678 
679 	NOTICE("BL2: R-Car Gen3 Initial Program Loader(%s) Rev.%s\n", str,
680 	       version_of_renesas);
681 
682 	reg = mmio_read_32(RCAR_PRR);
683 	product_cut = reg & (RCAR_PRODUCT_MASK | RCAR_CUT_MASK);
684 	product = reg & RCAR_PRODUCT_MASK;
685 
686 	switch (product) {
687 	case RCAR_PRODUCT_H3:
688 		str = product_h3;
689 		break;
690 	case RCAR_PRODUCT_M3:
691 		str = product_m3;
692 		break;
693 	case RCAR_PRODUCT_M3N:
694 		str = product_m3n;
695 		break;
696 	case RCAR_PRODUCT_E3:
697 		str = product_e3;
698 		break;
699 	default:
700 		str = unknown;
701 		break;
702 	}
703 
704 	if ((RCAR_PRODUCT_M3 == product) &&
705 	    (RCAR_CUT_VER20 == (reg & RCAR_MAJOR_MASK))) {
706 		if (RCAR_M3_CUT_VER11 == (reg & RCAR_CUT_MASK)) {
707 			/* M3 Ver.1.1 or Ver.1.2 */
708 			NOTICE("BL2: PRR is R-Car %s Ver.1.1 / Ver.1.2\n",
709 				str);
710 		} else {
711 			NOTICE("BL2: PRR is R-Car %s Ver.1.%d\n",
712 				str,
713 				(reg & RCAR_MINOR_MASK) + RCAR_M3_MINOR_OFFSET);
714 		}
715 	} else {
716 		major = (reg & RCAR_MAJOR_MASK) >> RCAR_MAJOR_SHIFT;
717 		major = major + RCAR_MAJOR_OFFSET;
718 		minor = reg & RCAR_MINOR_MASK;
719 		NOTICE("BL2: PRR is R-Car %s Ver.%d.%d\n", str, major, minor);
720 	}
721 
722 	if (product == RCAR_PRODUCT_E3) {
723 		reg = mmio_read_32(RCAR_MODEMR);
724 		sscg = reg & RCAR_SSCG_MASK;
725 		str = sscg == RCAR_SSCG_ENABLE ? sscg_on : sscg_off;
726 		NOTICE("BL2: %s\n", str);
727 	}
728 
729 	rcar_get_board_type(&type, &rev);
730 
731 	switch (type) {
732 	case BOARD_SALVATOR_X:
733 	case BOARD_KRIEK:
734 	case BOARD_STARTER_KIT:
735 	case BOARD_SALVATOR_XS:
736 	case BOARD_EBISU:
737 	case BOARD_STARTER_KIT_PRE:
738 	case BOARD_EBISU_4D:
739 		break;
740 	default:
741 		type = BOARD_UNKNOWN;
742 		break;
743 	}
744 
745 	if (type == BOARD_UNKNOWN || rev == BOARD_REV_UNKNOWN)
746 		NOTICE("BL2: Board is %s Rev.---\n", GET_BOARD_NAME(type));
747 	else {
748 		NOTICE("BL2: Board is %s Rev.%d.%d\n",
749 		       GET_BOARD_NAME(type),
750 		       GET_BOARD_MAJOR(rev), GET_BOARD_MINOR(rev));
751 	}
752 
753 #if RCAR_LSI != RCAR_AUTO
754 	if (product != TARGET_PRODUCT) {
755 		ERROR("BL2: IPL was been built for the %s.\n", TARGET_NAME);
756 		ERROR("BL2: Please write the correct IPL to flash memory.\n");
757 		panic();
758 	}
759 #endif
760 	rcar_avs_init();
761 	rcar_avs_setting();
762 
763 	switch (boot_dev) {
764 	case MODEMR_BOOT_DEV_HYPERFLASH160:
765 		str = boot_hyper160;
766 		break;
767 	case MODEMR_BOOT_DEV_HYPERFLASH80:
768 		str = boot_hyper80;
769 		break;
770 	case MODEMR_BOOT_DEV_QSPI_FLASH40:
771 		str = boot_qspi40;
772 		break;
773 	case MODEMR_BOOT_DEV_QSPI_FLASH80:
774 		str = boot_qspi80;
775 		break;
776 	case MODEMR_BOOT_DEV_EMMC_25X1:
777 		str = boot_emmc25x1;
778 		break;
779 	case MODEMR_BOOT_DEV_EMMC_50X8:
780 		str = boot_emmc50x8;
781 		break;
782 	default:
783 		str = unknown;
784 		break;
785 	}
786 	NOTICE("BL2: Boot device is %s\n", str);
787 
788 	rcar_avs_setting();
789 	reg = rcar_rom_get_lcs(&lcs);
790 	if (reg) {
791 		str = unknown;
792 		goto lcm_state;
793 	}
794 
795 	switch (lcs) {
796 	case LCS_CM:
797 		str = lcs_cm;
798 		break;
799 	case LCS_DM:
800 		str = lcs_dm;
801 		break;
802 	case LCS_SD:
803 		str = lcs_sd;
804 		break;
805 	case LCS_SE:
806 		str = lcs_secure;
807 		break;
808 	case LCS_FA:
809 		str = lcs_fa;
810 		break;
811 	default:
812 		str = unknown;
813 		break;
814 	}
815 
816 lcm_state:
817 	NOTICE("BL2: LCM state is %s\n", str);
818 
819 	rcar_avs_end();
820 	is_ddr_backup_mode();
821 
822 	bl2_tzram_layout.total_base = BL31_BASE;
823 	bl2_tzram_layout.total_size = BL31_LIMIT - BL31_BASE;
824 
825 	if (boot_cpu == MODEMR_BOOT_CPU_CA57 ||
826 	    boot_cpu == MODEMR_BOOT_CPU_CA53) {
827 		ret = rcar_dram_init();
828 		if (ret) {
829 			NOTICE("BL2: Failed to DRAM initialize (%d).\n", ret);
830 			panic();
831 		}
832 		rcar_qos_init();
833 	}
834 
835 	/* Set up FDT */
836 	ret = fdt_create_empty_tree(fdt, sizeof(fdt_blob));
837 	if (ret) {
838 		NOTICE("BL2: Cannot allocate FDT for U-Boot (ret=%i)\n", ret);
839 		panic();
840 	}
841 
842 	/* Add platform compatible string */
843 	bl2_populate_compatible_string(fdt);
844 
845 	/* Print DRAM layout */
846 	bl2_advertise_dram_size(product);
847 
848 	if (boot_dev == MODEMR_BOOT_DEV_EMMC_25X1 ||
849 	    boot_dev == MODEMR_BOOT_DEV_EMMC_50X8) {
850 		if (rcar_emmc_init() != EMMC_SUCCESS) {
851 			NOTICE("BL2: Failed to eMMC driver initialize.\n");
852 			panic();
853 		}
854 		rcar_emmc_memcard_power(EMMC_POWER_ON);
855 		if (rcar_emmc_mount() != EMMC_SUCCESS) {
856 			NOTICE("BL2: Failed to eMMC mount operation.\n");
857 			panic();
858 		}
859 	} else {
860 		rcar_rpc_init();
861 		rcar_dma_init();
862 	}
863 
864 	reg = mmio_read_32(RST_WDTRSTCR);
865 	reg &= ~WDTRSTCR_RWDT_RSTMSK;
866 	reg |= WDTRSTCR_PASSWORD;
867 	mmio_write_32(RST_WDTRSTCR, reg);
868 
869 	mmio_write_32(CPG_CPGWPR, CPGWPR_PASSWORD);
870 	mmio_write_32(CPG_CPGWPCR, CPGWPCR_PASSWORD);
871 
872 	reg = mmio_read_32(RCAR_PRR);
873 	if ((reg & RCAR_CPU_MASK_CA57) == RCAR_CPU_HAVE_CA57)
874 		mmio_write_32(CPG_CA57DBGRCR,
875 			      DBGCPUPREN | mmio_read_32(CPG_CA57DBGRCR));
876 
877 	if ((reg & RCAR_CPU_MASK_CA53) == RCAR_CPU_HAVE_CA53)
878 		mmio_write_32(CPG_CA53DBGRCR,
879 			      DBGCPUPREN | mmio_read_32(CPG_CA53DBGRCR));
880 
881 	if (product_cut == RCAR_PRODUCT_H3_CUT10) {
882 		reg = mmio_read_32(CPG_PLL2CR);
883 		reg &= ~((uint32_t) 1 << 5);
884 		mmio_write_32(CPG_PLL2CR, reg);
885 
886 		reg = mmio_read_32(CPG_PLL4CR);
887 		reg &= ~((uint32_t) 1 << 5);
888 		mmio_write_32(CPG_PLL4CR, reg);
889 
890 		reg = mmio_read_32(CPG_PLL0CR);
891 		reg &= ~((uint32_t) 1 << 12);
892 		mmio_write_32(CPG_PLL0CR, reg);
893 	}
894 #if (RCAR_LOSSY_ENABLE == 1)
895 	NOTICE("BL2: Lossy Decomp areas\n");
896 
897 	fcnlnode = fdt_add_subnode(fdt, 0, "reserved-memory");
898 	if (fcnlnode < 0) {
899 		NOTICE("BL2: Cannot create reserved mem node (ret=%i)\n",
900 			fcnlnode);
901 		panic();
902 	}
903 
904 	bl2_lossy_setting(0, LOSSY_ST_ADDR0, LOSSY_END_ADDR0,
905 			  LOSSY_FMT0, LOSSY_ENA_DIS0, fcnlnode);
906 	bl2_lossy_setting(1, LOSSY_ST_ADDR1, LOSSY_END_ADDR1,
907 			  LOSSY_FMT1, LOSSY_ENA_DIS1, fcnlnode);
908 	bl2_lossy_setting(2, LOSSY_ST_ADDR2, LOSSY_END_ADDR2,
909 			  LOSSY_FMT2, LOSSY_ENA_DIS2, fcnlnode);
910 #endif
911 
912 	fdt_pack(fdt);
913 	NOTICE("BL2: FDT at %p\n", fdt);
914 
915 	if (boot_dev == MODEMR_BOOT_DEV_EMMC_25X1 ||
916 	    boot_dev == MODEMR_BOOT_DEV_EMMC_50X8)
917 		rcar_io_emmc_setup();
918 	else
919 		rcar_io_setup();
920 }
921 
922 void bl2_el3_plat_arch_setup(void)
923 {
924 #if RCAR_BL2_DCACHE == 1
925 	NOTICE("BL2: D-Cache enable\n");
926 	rcar_configure_mmu_el3(BL2_BASE,
927 			       BL2_END - BL2_BASE,
928 			       BL2_RO_BASE, BL2_RO_LIMIT
929 #if USE_COHERENT_MEM
930 			       , BL2_COHERENT_RAM_BASE, BL2_COHERENT_RAM_LIMIT
931 #endif
932 	    );
933 #endif
934 }
935 
936 void bl2_platform_setup(void)
937 {
938 
939 }
940 
941 static void bl2_init_generic_timer(void)
942 {
943 #if RCAR_LSI == RCAR_E3
944 	uint32_t reg_cntfid = EXTAL_EBISU;
945 #else /* RCAR_LSI == RCAR_E3 */
946 	uint32_t reg;
947 	uint32_t reg_cntfid;
948 	uint32_t modemr;
949 	uint32_t modemr_pll;
950 	uint32_t board_type;
951 	uint32_t board_rev;
952 	uint32_t pll_table[] = {
953 		EXTAL_MD14_MD13_TYPE_0,	/* MD14/MD13 : 0b00 */
954 		EXTAL_MD14_MD13_TYPE_1,	/* MD14/MD13 : 0b01 */
955 		EXTAL_MD14_MD13_TYPE_2,	/* MD14/MD13 : 0b10 */
956 		EXTAL_MD14_MD13_TYPE_3	/* MD14/MD13 : 0b11 */
957 	};
958 
959 	modemr = mmio_read_32(RCAR_MODEMR);
960 	modemr_pll = (modemr & MODEMR_BOOT_PLL_MASK);
961 
962 	/* Set frequency data in CNTFID0 */
963 	reg_cntfid = pll_table[modemr_pll >> MODEMR_BOOT_PLL_SHIFT];
964 	reg = mmio_read_32(RCAR_PRR) & (RCAR_PRODUCT_MASK | RCAR_CUT_MASK);
965 	switch (modemr_pll) {
966 	case MD14_MD13_TYPE_0:
967 		rcar_get_board_type(&board_type, &board_rev);
968 		if (BOARD_SALVATOR_XS == board_type) {
969 			reg_cntfid = EXTAL_SALVATOR_XS;
970 		}
971 		break;
972 	case MD14_MD13_TYPE_3:
973 		if (RCAR_PRODUCT_H3_CUT10 == reg) {
974 			reg_cntfid = reg_cntfid >> 1U;
975 		}
976 		break;
977 	default:
978 		/* none */
979 		break;
980 	}
981 #endif /* RCAR_LSI == RCAR_E3 */
982 	/* Update memory mapped and register based freqency */
983 	write_cntfrq_el0((u_register_t )reg_cntfid);
984 	mmio_write_32(ARM_SYS_CNTCTL_BASE + (uintptr_t)CNTFID_OFF, reg_cntfid);
985 	/* Enable counter */
986 	mmio_setbits_32(RCAR_CNTC_BASE + (uintptr_t)CNTCR_OFF,
987 			(uint32_t)CNTCR_EN);
988 }
989