xref: /rk3399_ARM-atf/plat/renesas/rcar/bl2_plat_setup.c (revision c948f77136c42a92d0bb660543a3600c36dcf7f1)
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)
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 GiB\n",
486 			chan, start, start + size - 1, size >> 30);
487 	}
488 
489 	/*
490 	 * We add the DT nodes in reverse order here. The fdt_add_subnode()
491 	 * adds the DT node before the first existing DT node, so we have
492 	 * to add them in reverse order to get nodes sorted by address in
493 	 * the resulting DT.
494 	 */
495 	for (chan = 3; chan >= 0; chan--) {
496 		start = dram_config[2 * chan];
497 		size = dram_config[2 * chan + 1];
498 		if (!size)
499 			continue;
500 
501 		/*
502 		 * Channel 0 is mapped in 32bit space and the first
503 		 * 128 MiB are reserved
504 		 */
505 		if (chan == 0) {
506 			start = 0x48000000;
507 			size -= 0x8000000;
508 		}
509 
510 		fdtsize = cpu_to_fdt64(size);
511 
512 		snprintf(nodename, sizeof(nodename), "memory@");
513 		unsigned_num_print(start, 16, nodename + strlen(nodename));
514 		node = ret = fdt_add_subnode(fdt, 0, nodename);
515 		if (ret < 0)
516 			goto err;
517 
518 		ret = fdt_setprop_string(fdt, node, "device_type", "memory");
519 		if (ret < 0)
520 			goto err;
521 
522 		ret = fdt_setprop_u64(fdt, node, "reg", start);
523 		if (ret < 0)
524 			goto err;
525 
526 		ret = fdt_appendprop(fdt, node, "reg", &fdtsize,
527 				     sizeof(fdtsize));
528 		if (ret < 0)
529 			goto err;
530 	}
531 
532 	return;
533 err:
534 	NOTICE("BL2: Cannot add memory node to FDT (ret=%i)\n", ret);
535 	panic();
536 }
537 
538 static void bl2_advertise_dram_size(uint32_t product)
539 {
540 	uint64_t dram_config[8] = {
541 		[0] = 0x400000000ULL,
542 		[2] = 0x500000000ULL,
543 		[4] = 0x600000000ULL,
544 		[6] = 0x700000000ULL,
545 	};
546 
547 	switch (product) {
548 	case RCAR_PRODUCT_H3:
549 #if (RCAR_DRAM_LPDDR4_MEMCONF == 0)
550 		/* 4GB(1GBx4) */
551 		dram_config[1] = 0x40000000ULL;
552 		dram_config[3] = 0x40000000ULL;
553 		dram_config[5] = 0x40000000ULL;
554 		dram_config[7] = 0x40000000ULL;
555 #elif (RCAR_DRAM_LPDDR4_MEMCONF == 1) && \
556       (RCAR_DRAM_CHANNEL        == 5) && \
557       (RCAR_DRAM_SPLIT          == 2)
558 		/* 4GB(2GBx2 2ch split) */
559 		dram_config[1] = 0x80000000ULL;
560 		dram_config[3] = 0x80000000ULL;
561 #elif (RCAR_DRAM_LPDDR4_MEMCONF == 1) && (RCAR_DRAM_CHANNEL == 15)
562 		/* 8GB(2GBx4: default) */
563 		dram_config[1] = 0x80000000ULL;
564 		dram_config[3] = 0x80000000ULL;
565 		dram_config[5] = 0x80000000ULL;
566 		dram_config[7] = 0x80000000ULL;
567 #endif /* RCAR_DRAM_LPDDR4_MEMCONF == 0 */
568 		break;
569 
570 	case RCAR_PRODUCT_M3:
571 		/* 4GB(2GBx2 2ch split) */
572 		dram_config[1] = 0x80000000ULL;
573 		dram_config[5] = 0x80000000ULL;
574 		break;
575 
576 	case RCAR_PRODUCT_M3N:
577 		/* 2GB(1GBx2) */
578 		dram_config[1] = 0x80000000ULL;
579 		break;
580 
581 	case RCAR_PRODUCT_E3:
582 #if (RCAR_DRAM_DDR3L_MEMCONF == 0)
583 		/* 1GB(512MBx2) */
584 		dram_config[1] = 0x40000000ULL;
585 #elif (RCAR_DRAM_DDR3L_MEMCONF == 1)
586 		/* 2GB(512MBx4) */
587 		dram_config[1] = 0x80000000ULL;
588 #elif (RCAR_DRAM_DDR3L_MEMCONF == 2)
589 		/* 4GB(1GBx4) */
590 		dram_config[1] = 0x100000000ULL;
591 #endif /* RCAR_DRAM_DDR3L_MEMCONF == 0 */
592 		break;
593 	}
594 
595 	bl2_advertise_dram_entries(dram_config);
596 }
597 
598 void bl2_el3_early_platform_setup(u_register_t arg1, u_register_t arg2,
599 				  u_register_t arg3, u_register_t arg4)
600 {
601 	uint32_t reg, midr, lcs, boot_dev, boot_cpu, sscg, type, rev;
602 	uint32_t product, product_cut, major, minor;
603 	int32_t ret;
604 	const char *str;
605 	const char *unknown = "unknown";
606 	const char *cpu_ca57 = "CA57";
607 	const char *cpu_ca53 = "CA53";
608 	const char *product_m3n = "M3N";
609 	const char *product_h3 = "H3";
610 	const char *product_m3 = "M3";
611 	const char *product_e3 = "E3";
612 	const char *lcs_secure = "SE";
613 	const char *lcs_cm = "CM";
614 	const char *lcs_dm = "DM";
615 	const char *lcs_sd = "SD";
616 	const char *lcs_fa = "FA";
617 	const char *sscg_off = "PLL1 nonSSCG Clock select";
618 	const char *sscg_on = "PLL1 SSCG Clock select";
619 	const char *boot_hyper80 = "HyperFlash(80MHz)";
620 	const char *boot_qspi40 = "QSPI Flash(40MHz)";
621 	const char *boot_qspi80 = "QSPI Flash(80MHz)";
622 	const char *boot_emmc25x1 = "eMMC(25MHz x1)";
623 	const char *boot_emmc50x8 = "eMMC(50MHz x8)";
624 #if RCAR_LSI == RCAR_E3
625 	const char *boot_hyper160 = "HyperFlash(150MHz)";
626 #else
627 	const char *boot_hyper160 = "HyperFlash(160MHz)";
628 #endif
629 #if (RCAR_LOSSY_ENABLE == 1)
630 	int fcnlnode;
631 #endif
632 
633 	bl2_init_generic_timer();
634 
635 	reg = mmio_read_32(RCAR_MODEMR);
636 	boot_dev = reg & MODEMR_BOOT_DEV_MASK;
637 	boot_cpu = reg & MODEMR_BOOT_CPU_MASK;
638 
639 	bl2_cpg_init();
640 
641 	if (boot_cpu == MODEMR_BOOT_CPU_CA57 ||
642 	    boot_cpu == MODEMR_BOOT_CPU_CA53) {
643 		rcar_pfc_init();
644 		/* console configuration (platform specific) done in driver */
645 		console_init(0, 0, 0);
646 	}
647 
648 	plat_rcar_gic_driver_init();
649 	plat_rcar_gic_init();
650 	rcar_swdt_init();
651 
652 	/* FIQ interrupts are taken to EL3 */
653 	write_scr_el3(read_scr_el3() | SCR_FIQ_BIT);
654 
655 	write_daifclr(DAIF_FIQ_BIT);
656 
657 	reg = read_midr();
658 	midr = reg & (MIDR_PN_MASK << MIDR_PN_SHIFT);
659 	switch (midr) {
660 	case MIDR_CA57:
661 		str = cpu_ca57;
662 		break;
663 	case MIDR_CA53:
664 		str = cpu_ca53;
665 		break;
666 	default:
667 		str = unknown;
668 		break;
669 	}
670 
671 	NOTICE("BL2: R-Car Gen3 Initial Program Loader(%s) Rev.%s\n", str,
672 	       version_of_renesas);
673 
674 	reg = mmio_read_32(RCAR_PRR);
675 	product_cut = reg & (RCAR_PRODUCT_MASK | RCAR_CUT_MASK);
676 	product = reg & RCAR_PRODUCT_MASK;
677 
678 	switch (product) {
679 	case RCAR_PRODUCT_H3:
680 		str = product_h3;
681 		break;
682 	case RCAR_PRODUCT_M3:
683 		str = product_m3;
684 		break;
685 	case RCAR_PRODUCT_M3N:
686 		str = product_m3n;
687 		break;
688 	case RCAR_PRODUCT_E3:
689 		str = product_e3;
690 		break;
691 	default:
692 		str = unknown;
693 		break;
694 	}
695 
696 	if (RCAR_PRODUCT_M3_CUT11 == product_cut) {
697 		NOTICE("BL2: PRR is R-Car %s Ver.1.1 / Ver.1.2\n", str);
698 	} else {
699 		major = (reg & RCAR_MAJOR_MASK) >> RCAR_MAJOR_SHIFT;
700 		major = major + RCAR_MAJOR_OFFSET;
701 		minor = reg & RCAR_MINOR_MASK;
702 		NOTICE("BL2: PRR is R-Car %s Ver.%d.%d\n", str, major, minor);
703 	}
704 
705 	if (product == RCAR_PRODUCT_E3) {
706 		reg = mmio_read_32(RCAR_MODEMR);
707 		sscg = reg & RCAR_SSCG_MASK;
708 		str = sscg == RCAR_SSCG_ENABLE ? sscg_on : sscg_off;
709 		NOTICE("BL2: %s\n", str);
710 	}
711 
712 	rcar_get_board_type(&type, &rev);
713 
714 	switch (type) {
715 	case BOARD_SALVATOR_X:
716 	case BOARD_KRIEK:
717 	case BOARD_STARTER_KIT:
718 	case BOARD_SALVATOR_XS:
719 	case BOARD_EBISU:
720 	case BOARD_STARTER_KIT_PRE:
721 	case BOARD_EBISU_4D:
722 		break;
723 	default:
724 		type = BOARD_UNKNOWN;
725 		break;
726 	}
727 
728 	if (type == BOARD_UNKNOWN || rev == BOARD_REV_UNKNOWN)
729 		NOTICE("BL2: Board is %s Rev.---\n", GET_BOARD_NAME(type));
730 	else {
731 		NOTICE("BL2: Board is %s Rev.%d.%d\n",
732 		       GET_BOARD_NAME(type),
733 		       GET_BOARD_MAJOR(rev), GET_BOARD_MINOR(rev));
734 	}
735 
736 #if RCAR_LSI != RCAR_AUTO
737 	if (product != TARGET_PRODUCT) {
738 		ERROR("BL2: IPL was been built for the %s.\n", TARGET_NAME);
739 		ERROR("BL2: Please write the correct IPL to flash memory.\n");
740 		panic();
741 	}
742 #endif
743 	rcar_avs_init();
744 	rcar_avs_setting();
745 
746 	switch (boot_dev) {
747 	case MODEMR_BOOT_DEV_HYPERFLASH160:
748 		str = boot_hyper160;
749 		break;
750 	case MODEMR_BOOT_DEV_HYPERFLASH80:
751 		str = boot_hyper80;
752 		break;
753 	case MODEMR_BOOT_DEV_QSPI_FLASH40:
754 		str = boot_qspi40;
755 		break;
756 	case MODEMR_BOOT_DEV_QSPI_FLASH80:
757 		str = boot_qspi80;
758 		break;
759 	case MODEMR_BOOT_DEV_EMMC_25X1:
760 		str = boot_emmc25x1;
761 		break;
762 	case MODEMR_BOOT_DEV_EMMC_50X8:
763 		str = boot_emmc50x8;
764 		break;
765 	default:
766 		str = unknown;
767 		break;
768 	}
769 	NOTICE("BL2: Boot device is %s\n", str);
770 
771 	rcar_avs_setting();
772 	reg = rcar_rom_get_lcs(&lcs);
773 	if (reg) {
774 		str = unknown;
775 		goto lcm_state;
776 	}
777 
778 	switch (lcs) {
779 	case LCS_CM:
780 		str = lcs_cm;
781 		break;
782 	case LCS_DM:
783 		str = lcs_dm;
784 		break;
785 	case LCS_SD:
786 		str = lcs_sd;
787 		break;
788 	case LCS_SE:
789 		str = lcs_secure;
790 		break;
791 	case LCS_FA:
792 		str = lcs_fa;
793 		break;
794 	default:
795 		str = unknown;
796 		break;
797 	}
798 
799 lcm_state:
800 	NOTICE("BL2: LCM state is %s\n", str);
801 
802 	rcar_avs_end();
803 	is_ddr_backup_mode();
804 
805 	bl2_tzram_layout.total_base = BL31_BASE;
806 	bl2_tzram_layout.total_size = BL31_LIMIT - BL31_BASE;
807 
808 	if (boot_cpu == MODEMR_BOOT_CPU_CA57 ||
809 	    boot_cpu == MODEMR_BOOT_CPU_CA53) {
810 		ret = rcar_dram_init();
811 		if (ret) {
812 			NOTICE("BL2: Failed to DRAM initialize (%d).\n", ret);
813 			panic();
814 		}
815 		rcar_qos_init();
816 	}
817 
818 	/* Set up FDT */
819 	ret = fdt_create_empty_tree(fdt, sizeof(fdt_blob));
820 	if (ret) {
821 		NOTICE("BL2: Cannot allocate FDT for U-Boot (ret=%i)\n", ret);
822 		panic();
823 	}
824 
825 	/* Add platform compatible string */
826 	bl2_populate_compatible_string(fdt);
827 
828 	/* Print DRAM layout */
829 	bl2_advertise_dram_size(product);
830 
831 	if (boot_dev == MODEMR_BOOT_DEV_EMMC_25X1 ||
832 	    boot_dev == MODEMR_BOOT_DEV_EMMC_50X8) {
833 		if (rcar_emmc_init() != EMMC_SUCCESS) {
834 			NOTICE("BL2: Failed to eMMC driver initialize.\n");
835 			panic();
836 		}
837 		rcar_emmc_memcard_power(EMMC_POWER_ON);
838 		if (rcar_emmc_mount() != EMMC_SUCCESS) {
839 			NOTICE("BL2: Failed to eMMC mount operation.\n");
840 			panic();
841 		}
842 	} else {
843 		rcar_rpc_init();
844 		rcar_dma_init();
845 	}
846 
847 	reg = mmio_read_32(RST_WDTRSTCR);
848 	reg &= ~WDTRSTCR_RWDT_RSTMSK;
849 	reg |= WDTRSTCR_PASSWORD;
850 	mmio_write_32(RST_WDTRSTCR, reg);
851 
852 	mmio_write_32(CPG_CPGWPR, CPGWPR_PASSWORD);
853 	mmio_write_32(CPG_CPGWPCR, CPGWPCR_PASSWORD);
854 
855 	reg = mmio_read_32(RCAR_PRR);
856 	if ((reg & RCAR_CPU_MASK_CA57) == RCAR_CPU_HAVE_CA57)
857 		mmio_write_32(CPG_CA57DBGRCR,
858 			      DBGCPUPREN | mmio_read_32(CPG_CA57DBGRCR));
859 
860 	if ((reg & RCAR_CPU_MASK_CA53) == RCAR_CPU_HAVE_CA53)
861 		mmio_write_32(CPG_CA53DBGRCR,
862 			      DBGCPUPREN | mmio_read_32(CPG_CA53DBGRCR));
863 
864 	if (product_cut == RCAR_PRODUCT_H3_CUT10) {
865 		reg = mmio_read_32(CPG_PLL2CR);
866 		reg &= ~((uint32_t) 1 << 5);
867 		mmio_write_32(CPG_PLL2CR, reg);
868 
869 		reg = mmio_read_32(CPG_PLL4CR);
870 		reg &= ~((uint32_t) 1 << 5);
871 		mmio_write_32(CPG_PLL4CR, reg);
872 
873 		reg = mmio_read_32(CPG_PLL0CR);
874 		reg &= ~((uint32_t) 1 << 12);
875 		mmio_write_32(CPG_PLL0CR, reg);
876 	}
877 #if (RCAR_LOSSY_ENABLE == 1)
878 	NOTICE("BL2: Lossy Decomp areas\n");
879 
880 	fcnlnode = fdt_add_subnode(fdt, 0, "reserved-memory");
881 	if (fcnlnode < 0) {
882 		NOTICE("BL2: Cannot create reserved mem node (ret=%i)\n",
883 			fcnlnode);
884 		panic();
885 	}
886 
887 	bl2_lossy_setting(0, LOSSY_ST_ADDR0, LOSSY_END_ADDR0,
888 			  LOSSY_FMT0, LOSSY_ENA_DIS0, fcnlnode);
889 	bl2_lossy_setting(1, LOSSY_ST_ADDR1, LOSSY_END_ADDR1,
890 			  LOSSY_FMT1, LOSSY_ENA_DIS1, fcnlnode);
891 	bl2_lossy_setting(2, LOSSY_ST_ADDR2, LOSSY_END_ADDR2,
892 			  LOSSY_FMT2, LOSSY_ENA_DIS2, fcnlnode);
893 #endif
894 
895 	fdt_pack(fdt);
896 	NOTICE("BL2: FDT at %p\n", fdt);
897 
898 	if (boot_dev == MODEMR_BOOT_DEV_EMMC_25X1 ||
899 	    boot_dev == MODEMR_BOOT_DEV_EMMC_50X8)
900 		rcar_io_emmc_setup();
901 	else
902 		rcar_io_setup();
903 }
904 
905 void bl2_el3_plat_arch_setup(void)
906 {
907 #if RCAR_BL2_DCACHE == 1
908 	NOTICE("BL2: D-Cache enable\n");
909 	rcar_configure_mmu_el3(BL2_BASE,
910 			       BL2_END - BL2_BASE,
911 			       BL2_RO_BASE, BL2_RO_LIMIT
912 #if USE_COHERENT_MEM
913 			       , BL2_COHERENT_RAM_BASE, BL2_COHERENT_RAM_LIMIT
914 #endif
915 	    );
916 #endif
917 }
918 
919 void bl2_platform_setup(void)
920 {
921 
922 }
923 
924 static void bl2_init_generic_timer(void)
925 {
926 #if RCAR_LSI == RCAR_E3
927 	uint32_t reg_cntfid = EXTAL_EBISU;
928 #else /* RCAR_LSI == RCAR_E3 */
929 	uint32_t reg;
930 	uint32_t reg_cntfid;
931 	uint32_t modemr;
932 	uint32_t modemr_pll;
933 	uint32_t board_type;
934 	uint32_t board_rev;
935 	uint32_t pll_table[] = {
936 		EXTAL_MD14_MD13_TYPE_0,	/* MD14/MD13 : 0b00 */
937 		EXTAL_MD14_MD13_TYPE_1,	/* MD14/MD13 : 0b01 */
938 		EXTAL_MD14_MD13_TYPE_2,	/* MD14/MD13 : 0b10 */
939 		EXTAL_MD14_MD13_TYPE_3	/* MD14/MD13 : 0b11 */
940 	};
941 
942 	modemr = mmio_read_32(RCAR_MODEMR);
943 	modemr_pll = (modemr & MODEMR_BOOT_PLL_MASK);
944 
945 	/* Set frequency data in CNTFID0 */
946 	reg_cntfid = pll_table[modemr_pll >> MODEMR_BOOT_PLL_SHIFT];
947 	reg = mmio_read_32(RCAR_PRR) & (RCAR_PRODUCT_MASK | RCAR_CUT_MASK);
948 	switch (modemr_pll) {
949 	case MD14_MD13_TYPE_0:
950 		rcar_get_board_type(&board_type, &board_rev);
951 		if (BOARD_SALVATOR_XS == board_type) {
952 			reg_cntfid = EXTAL_SALVATOR_XS;
953 		}
954 		break;
955 	case MD14_MD13_TYPE_3:
956 		if (RCAR_PRODUCT_H3_CUT10 == reg) {
957 			reg_cntfid = reg_cntfid >> 1U;
958 		}
959 		break;
960 	default:
961 		/* none */
962 		break;
963 	}
964 #endif /* RCAR_LSI == RCAR_E3 */
965 	/* Update memory mapped and register based freqency */
966 	write_cntfrq_el0((u_register_t )reg_cntfid);
967 	mmio_write_32(ARM_SYS_CNTCTL_BASE + (uintptr_t)CNTFID_OFF, reg_cntfid);
968 	/* Enable counter */
969 	mmio_setbits_32(RCAR_CNTC_BASE + (uintptr_t)CNTCR_OFF,
970 			(uint32_t)CNTCR_EN);
971 }
972