bl2_plat_setup.c 27.3 KB
Newer Older
1
/*
2
 * Copyright (c) 2018-2020, Renesas Electronics Corporation. All rights reserved.
3
4
5
6
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

7
8
#include <string.h>

9
#include <libfdt.h>
10

11
#include <platform_def.h>
12
13
14
15
16
17

#include <arch_helpers.h>
#include <bl1/bl1.h>
#include <common/bl_common.h>
#include <common/debug.h>
#include <common/desc_image_load.h>
18
#include <common/image_decompress.h>
19
#include <drivers/console.h>
20
21
#include <drivers/io/io_driver.h>
#include <drivers/io/io_storage.h>
22
23
24
#include <lib/mmio.h>
#include <lib/xlat_tables/xlat_tables_defs.h>
#include <plat/common/platform.h>
25
26
27
#if RCAR_GEN3_BL33_GZIP == 1
#include <tf_gunzip.h>
#endif
28
29
30
31
32
33
34
35
36
37
38
39
40
41

#include "avs_driver.h"
#include "boot_init_dram.h"
#include "cpg_registers.h"
#include "board.h"
#include "emmc_def.h"
#include "emmc_hal.h"
#include "emmc_std.h"

#if PMIC_ROHM_BD9571 && RCAR_SYSTEM_RESET_KEEPON_DDR
#include "iic_dvfs.h"
#endif

#include "io_common.h"
42
#include "io_rcar.h"
43
44
45
46
47
48
#include "qos_init.h"
#include "rcar_def.h"
#include "rcar_private.h"
#include "rcar_version.h"
#include "rom_api.h"

49
50
51
52
53
54
55
#if RCAR_BL2_DCACHE == 1
/*
 * Following symbols are only used during plat_arch_setup() only
 * when RCAR_BL2_DCACHE is enabled.
 */
static const uint64_t BL2_RO_BASE		= BL_CODE_BASE;
static const uint64_t BL2_RO_LIMIT		= BL_CODE_END;
56
57

#if USE_COHERENT_MEM
58
59
60
61
static const uint64_t BL2_COHERENT_RAM_BASE	= BL_COHERENT_RAM_BASE;
static const uint64_t BL2_COHERENT_RAM_LIMIT	= BL_COHERENT_RAM_END;
#endif

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#endif

extern void plat_rcar_gic_driver_init(void);
extern void plat_rcar_gic_init(void);
extern void bl2_enter_bl31(const struct entry_point_info *bl_ep_info);
extern void bl2_system_cpg_init(void);
extern void bl2_secure_setting(void);
extern void bl2_cpg_init(void);
extern void rcar_io_emmc_setup(void);
extern void rcar_io_setup(void);
extern void rcar_swdt_release(void);
extern void rcar_swdt_init(void);
extern void rcar_rpc_init(void);
extern void rcar_pfc_init(void);
extern void rcar_dma_init(void);

78
79
static void bl2_init_generic_timer(void);

80
81
/* R-Car Gen3 product check */
#if (RCAR_LSI == RCAR_H3) || (RCAR_LSI == RCAR_H3N)
82
#define TARGET_PRODUCT			PRR_PRODUCT_H3
83
84
#define TARGET_NAME			"R-Car H3"
#elif RCAR_LSI == RCAR_M3
85
#define TARGET_PRODUCT			PRR_PRODUCT_M3
86
87
#define TARGET_NAME			"R-Car M3"
#elif RCAR_LSI == RCAR_M3N
88
#define TARGET_PRODUCT			PRR_PRODUCT_M3N
89
#define TARGET_NAME			"R-Car M3N"
90
#elif RCAR_LSI == RCAR_V3M
91
#define TARGET_PRODUCT			PRR_PRODUCT_V3M
92
#define TARGET_NAME			"R-Car V3M"
93
#elif RCAR_LSI == RCAR_E3
94
#define TARGET_PRODUCT			PRR_PRODUCT_E3
95
#define TARGET_NAME			"R-Car E3"
96
#elif RCAR_LSI == RCAR_D3
97
#define TARGET_PRODUCT			PRR_PRODUCT_D3
98
#define TARGET_NAME			"R-Car D3"
99
#elif RCAR_LSI == RCAR_AUTO
100
#define TARGET_NAME			"R-Car H3/M3/M3N/V3M"
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#endif

#if (RCAR_LSI == RCAR_E3)
#define GPIO_INDT			(GPIO_INDT6)
#define GPIO_BKUP_TRG_SHIFT		((uint32_t)1U<<13U)
#else
#define GPIO_INDT			(GPIO_INDT1)
#define GPIO_BKUP_TRG_SHIFT		((uint32_t)1U<<8U)
#endif

CASSERT((PARAMS_BASE + sizeof(bl2_to_bl31_params_mem_t) + 0x100)
	 < (RCAR_SHARED_MEM_BASE + RCAR_SHARED_MEM_SIZE),
	assert_bl31_params_do_not_fit_in_shared_memory);

static meminfo_t bl2_tzram_layout __aligned(CACHE_WRITEBACK_GRANULE);

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* FDT with DRAM configuration */
uint64_t fdt_blob[PAGE_SIZE_4KB / sizeof(uint64_t)];
static void *fdt = (void *)fdt_blob;

static void unsigned_num_print(unsigned long long int unum, unsigned int radix,
				char *string)
{
	/* Just need enough space to store 64 bit decimal integer */
	char num_buf[20];
	int i = 0;
	unsigned int rem;

	do {
		rem = unum % radix;
		if (rem < 0xa)
			num_buf[i] = '0' + rem;
		else
			num_buf[i] = 'a' + (rem - 0xa);
		i++;
		unum /= radix;
	} while (unum > 0U);

	while (--i >= 0)
		*string++ = num_buf[i];
141
	*string = 0;
142
143
}

144
145
146
147
148
149
150
#if (RCAR_LOSSY_ENABLE == 1)
typedef struct bl2_lossy_info {
	uint32_t magic;
	uint32_t a0;
	uint32_t b0;
} bl2_lossy_info_t;

151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
static void bl2_lossy_gen_fdt(uint32_t no, uint64_t start_addr,
			      uint64_t end_addr, uint32_t format,
			      uint32_t enable, int fcnlnode)
{
	const uint64_t fcnlsize = cpu_to_fdt64(end_addr - start_addr);
	char nodename[40] = { 0 };
	int ret, node;

	/* Ignore undefined addresses */
	if (start_addr == 0 && end_addr == 0)
		return;

	snprintf(nodename, sizeof(nodename), "lossy-decompression@");
	unsigned_num_print(start_addr, 16, nodename + strlen(nodename));

	node = ret = fdt_add_subnode(fdt, fcnlnode, nodename);
	if (ret < 0) {
		NOTICE("BL2: Cannot create FCNL node (ret=%i)\n", ret);
		panic();
	}

	ret = fdt_setprop_string(fdt, node, "compatible",
				 "renesas,lossy-decompression");
	if (ret < 0) {
		NOTICE("BL2: Cannot add FCNL compat string (ret=%i)\n", ret);
		panic();
	}

	ret = fdt_appendprop_string(fdt, node, "compatible",
				    "shared-dma-pool");
	if (ret < 0) {
		NOTICE("BL2: Cannot append FCNL compat string (ret=%i)\n", ret);
		panic();
	}

	ret = fdt_setprop_u64(fdt, node, "reg", start_addr);
	if (ret < 0) {
		NOTICE("BL2: Cannot add FCNL reg prop (ret=%i)\n", ret);
		panic();
	}

	ret = fdt_appendprop(fdt, node, "reg", &fcnlsize, sizeof(fcnlsize));
	if (ret < 0) {
		NOTICE("BL2: Cannot append FCNL reg size prop (ret=%i)\n", ret);
		panic();
	}

	ret = fdt_setprop(fdt, node, "no-map", NULL, 0);
	if (ret < 0) {
		NOTICE("BL2: Cannot add FCNL no-map prop (ret=%i)\n", ret);
		panic();
	}

	ret = fdt_setprop_u32(fdt, node, "renesas,formats", format);
	if (ret < 0) {
		NOTICE("BL2: Cannot add FCNL formats prop (ret=%i)\n", ret);
		panic();
	}
}

211
212
static void bl2_lossy_setting(uint32_t no, uint64_t start_addr,
			      uint64_t end_addr, uint32_t format,
213
			      uint32_t enable, int fcnlnode)
214
215
216
217
{
	bl2_lossy_info_t info;
	uint32_t reg;

218
219
	bl2_lossy_gen_fdt(no, start_addr, end_addr, format, enable, fcnlnode);

220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
	reg = format | (start_addr >> 20);
	mmio_write_32(AXI_DCMPAREACRA0 + 0x8 * no, reg);
	mmio_write_32(AXI_DCMPAREACRB0 + 0x8 * no, end_addr >> 20);
	mmio_write_32(AXI_DCMPAREACRA0 + 0x8 * no, reg | enable);

	info.magic = 0x12345678U;
	info.a0 = mmio_read_32(AXI_DCMPAREACRA0 + 0x8 * no);
	info.b0 = mmio_read_32(AXI_DCMPAREACRB0 + 0x8 * no);

	mmio_write_32(LOSSY_PARAMS_BASE + sizeof(info) * no, info.magic);
	mmio_write_32(LOSSY_PARAMS_BASE + sizeof(info) * no + 0x4, info.a0);
	mmio_write_32(LOSSY_PARAMS_BASE + sizeof(info) * no + 0x8, info.b0);

	NOTICE("     Entry %d: DCMPAREACRAx:0x%x DCMPAREACRBx:0x%x\n", no,
	       mmio_read_32(AXI_DCMPAREACRA0 + 0x8 * no),
	       mmio_read_32(AXI_DCMPAREACRB0 + 0x8 * no));
}
#endif

void bl2_plat_flush_bl31_params(void)
{
	uint32_t product_cut, product, cut;
	uint32_t boot_dev, boot_cpu;
	uint32_t lcs, reg, val;

	reg = mmio_read_32(RCAR_MODEMR);
	boot_dev = reg & MODEMR_BOOT_DEV_MASK;

	if (boot_dev == MODEMR_BOOT_DEV_EMMC_25X1 ||
	    boot_dev == MODEMR_BOOT_DEV_EMMC_50X8)
		emmc_terminate();

	if ((reg & MODEMR_BOOT_CPU_MASK) != MODEMR_BOOT_CPU_CR7)
		bl2_secure_setting();

	reg = mmio_read_32(RCAR_PRR);
256
257
258
	product_cut = reg & (PRR_PRODUCT_MASK | PRR_CUT_MASK);
	product = reg & PRR_PRODUCT_MASK;
	cut = reg & PRR_CUT_MASK;
259

260
	if (product == PRR_PRODUCT_M3 && PRR_PRODUCT_30 > cut)
261
262
		goto tlb;

263
	if (product == PRR_PRODUCT_H3 && PRR_PRODUCT_20 > cut)
264
265
		goto tlb;

266
	if (product == PRR_PRODUCT_D3)
267
268
		goto tlb;

269
270
271
272
273
274
275
276
277
278
	/* Disable MFIS write protection */
	mmio_write_32(MFISWPCNTR, MFISWPCNTR_PASSWORD | 1);

tlb:
	reg = mmio_read_32(RCAR_MODEMR);
	boot_cpu = reg & MODEMR_BOOT_CPU_MASK;
	if (boot_cpu != MODEMR_BOOT_CPU_CA57 &&
	    boot_cpu != MODEMR_BOOT_CPU_CA53)
		goto mmu;

279
	if (product_cut == PRR_PRODUCT_H3_CUT20) {
280
281
282
283
284
285
		mmio_write_32(IPMMUVI0_IMSCTLR, IMSCTLR_DISCACHE);
		mmio_write_32(IPMMUVI1_IMSCTLR, IMSCTLR_DISCACHE);
		mmio_write_32(IPMMUPV0_IMSCTLR, IMSCTLR_DISCACHE);
		mmio_write_32(IPMMUPV1_IMSCTLR, IMSCTLR_DISCACHE);
		mmio_write_32(IPMMUPV2_IMSCTLR, IMSCTLR_DISCACHE);
		mmio_write_32(IPMMUPV3_IMSCTLR, IMSCTLR_DISCACHE);
286
287
	} else if (product_cut == (PRR_PRODUCT_M3N | PRR_PRODUCT_10) ||
		   product_cut == (PRR_PRODUCT_M3N | PRR_PRODUCT_11)) {
288
289
		mmio_write_32(IPMMUVI0_IMSCTLR, IMSCTLR_DISCACHE);
		mmio_write_32(IPMMUPV0_IMSCTLR, IMSCTLR_DISCACHE);
290
291
	} else if ((product_cut == (PRR_PRODUCT_E3 | PRR_PRODUCT_10)) ||
		   (product_cut == (PRR_PRODUCT_E3 | PRR_PRODUCT_11))) {
292
		mmio_write_32(IPMMUVI0_IMSCTLR, IMSCTLR_DISCACHE);
293
		mmio_write_32(IPMMUVP0_IMSCTLR, IMSCTLR_DISCACHE);
294
295
296
		mmio_write_32(IPMMUPV0_IMSCTLR, IMSCTLR_DISCACHE);
	}

297
298
299
300
	if (product_cut == (PRR_PRODUCT_H3_CUT20) ||
	    product_cut == (PRR_PRODUCT_M3N | PRR_PRODUCT_10) ||
	    product_cut == (PRR_PRODUCT_M3N | PRR_PRODUCT_11) ||
	    product_cut == (PRR_PRODUCT_E3 | PRR_PRODUCT_10)) {
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
		mmio_write_32(IPMMUHC_IMSCTLR, IMSCTLR_DISCACHE);
		mmio_write_32(IPMMURT_IMSCTLR, IMSCTLR_DISCACHE);
		mmio_write_32(IPMMUMP_IMSCTLR, IMSCTLR_DISCACHE);

		mmio_write_32(IPMMUDS0_IMSCTLR, IMSCTLR_DISCACHE);
		mmio_write_32(IPMMUDS1_IMSCTLR, IMSCTLR_DISCACHE);
	}

mmu:
	mmio_write_32(IPMMUMM_IMSCTLR, IPMMUMM_IMSCTLR_ENABLE);
	mmio_write_32(IPMMUMM_IMAUXCTLR, IPMMUMM_IMAUXCTLR_NMERGE40_BIT);

	val = rcar_rom_get_lcs(&lcs);
	if (val) {
		ERROR("BL2: Failed to get the LCS. (%d)\n", val);
		panic();
	}

	if (lcs == LCS_SE)
		mmio_clrbits_32(P_ARMREG_SEC_CTRL, P_ARMREG_SEC_CTRL_PROT);

	rcar_swdt_release();
	bl2_system_cpg_init();

#if RCAR_BL2_DCACHE == 1
	/* Disable data cache (clean and invalidate) */
	disable_mmu_el3();
#endif
}

static uint32_t is_ddr_backup_mode(void)
{
#if RCAR_SYSTEM_SUSPEND
	static uint32_t reason = RCAR_COLD_BOOT;
	static uint32_t once;

#if PMIC_ROHM_BD9571 && RCAR_SYSTEM_RESET_KEEPON_DDR
	uint8_t data;
#endif
	if (once)
		return reason;

	once = 1;
	if ((mmio_read_32(GPIO_INDT) & GPIO_BKUP_TRG_SHIFT) == 0)
		return reason;

#if PMIC_ROHM_BD9571 && RCAR_SYSTEM_RESET_KEEPON_DDR
	if (rcar_iic_dvfs_receive(PMIC, REG_KEEP10, &data)) {
		ERROR("BL2: REG Keep10 READ ERROR.\n");
		panic();
	}

	if (KEEP10_MAGIC != data)
		reason = RCAR_WARM_BOOT;
#else
	reason = RCAR_WARM_BOOT;
#endif
	return reason;
#else
	return RCAR_COLD_BOOT;
#endif
}

364
365
366
367
368
369
370
#if RCAR_GEN3_BL33_GZIP == 1
void bl2_plat_preload_setup(void)
{
	image_decompress_init(BL33_COMP_BASE, BL33_COMP_SIZE, gunzip);
}
#endif

371
372
373
374
375
int bl2_plat_handle_pre_image_load(unsigned int image_id)
{
	u_register_t *boot_kind = (void *) BOOT_KIND_BASE;
	bl_mem_params_node_t *bl_mem_params;

376
377
378
379
380
381
382
383
	bl_mem_params = get_bl_mem_params_node(image_id);

#if RCAR_GEN3_BL33_GZIP == 1
	if (image_id == BL33_IMAGE_ID) {
		image_decompress_prepare(&bl_mem_params->image_info);
	}
#endif

384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
	if (image_id != BL31_IMAGE_ID)
		return 0;

	if (is_ddr_backup_mode() == RCAR_COLD_BOOT)
		goto cold_boot;

	*boot_kind  = RCAR_WARM_BOOT;
	flush_dcache_range(BOOT_KIND_BASE, sizeof(*boot_kind));

	console_flush();
	bl2_plat_flush_bl31_params();

	/* will not return */
	bl2_enter_bl31(&bl_mem_params->ep_info);

cold_boot:
	*boot_kind  = RCAR_COLD_BOOT;
	flush_dcache_range(BOOT_KIND_BASE, sizeof(*boot_kind));

	return 0;
}

406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
static uint64_t rcar_get_dest_addr_from_cert(uint32_t certid, uintptr_t *dest)
{
	uint32_t cert, len;
	int ret;

	ret = rcar_get_certificate(certid, &cert);
	if (ret) {
		ERROR("%s : cert file load error", __func__);
		return 1;
	}

	rcar_read_certificate((uint64_t) cert, &len, dest);

	return 0;
}

422
423
424
425
int bl2_plat_handle_post_image_load(unsigned int image_id)
{
	static bl2_to_bl31_params_mem_t *params;
	bl_mem_params_node_t *bl_mem_params;
426
427
	uintptr_t dest;
	int ret;
428
429
430
431
432
433
434
435
436
437

	if (!params) {
		params = (bl2_to_bl31_params_mem_t *) PARAMS_BASE;
		memset((void *)PARAMS_BASE, 0, sizeof(*params));
	}

	bl_mem_params = get_bl_mem_params_node(image_id);

	switch (image_id) {
	case BL31_IMAGE_ID:
438
439
440
441
		ret = rcar_get_dest_addr_from_cert(SOC_FW_CONTENT_CERT_ID,
						   &dest);
		if (!ret)
			bl_mem_params->image_info.image_base = dest;
442
443
		break;
	case BL32_IMAGE_ID:
444
445
446
447
448
		ret = rcar_get_dest_addr_from_cert(TRUSTED_OS_FW_CONTENT_CERT_ID,
						   &dest);
		if (!ret)
			bl_mem_params->image_info.image_base = dest;

449
450
451
452
		memcpy(&params->bl32_ep_info, &bl_mem_params->ep_info,
			sizeof(entry_point_info_t));
		break;
	case BL33_IMAGE_ID:
453
454
455
456
457
458
459
460
461
462
463
464
465
#if RCAR_GEN3_BL33_GZIP == 1
		if ((mmio_read_32(BL33_COMP_BASE) & 0xffff) == 0x8b1f) {
			/* decompress gzip-compressed image */
			ret = image_decompress(&bl_mem_params->image_info);
			if (ret != 0) {
				return ret;
			}
		} else {
			/* plain image, copy it in place */
			memcpy((void *)BL33_BASE, (void *)BL33_COMP_BASE,
				bl_mem_params->image_info.image_size);
		}
#endif
466
467
468
469
470
471
472
473
		memcpy(&params->bl33_ep_info, &bl_mem_params->ep_info,
			sizeof(entry_point_info_t));
		break;
	}

	return 0;
}

474
struct meminfo *bl2_plat_sec_mem_layout(void)
475
476
477
478
{
	return &bl2_tzram_layout;
}

Justin Chadwell's avatar
Justin Chadwell committed
479
static void bl2_populate_compatible_string(void *dt)
480
481
482
483
484
485
{
	uint32_t board_type;
	uint32_t board_rev;
	uint32_t reg;
	int ret;

486
487
488
	fdt_setprop_u32(dt, 0, "#address-cells", 2);
	fdt_setprop_u32(dt, 0, "#size-cells", 2);

489
490
491
492
	/* Populate compatible string */
	rcar_get_board_type(&board_type, &board_rev);
	switch (board_type) {
	case BOARD_SALVATOR_X:
Justin Chadwell's avatar
Justin Chadwell committed
493
		ret = fdt_setprop_string(dt, 0, "compatible",
494
495
496
					 "renesas,salvator-x");
		break;
	case BOARD_SALVATOR_XS:
Justin Chadwell's avatar
Justin Chadwell committed
497
		ret = fdt_setprop_string(dt, 0, "compatible",
498
499
500
					 "renesas,salvator-xs");
		break;
	case BOARD_STARTER_KIT:
Justin Chadwell's avatar
Justin Chadwell committed
501
		ret = fdt_setprop_string(dt, 0, "compatible",
502
503
504
					 "renesas,m3ulcb");
		break;
	case BOARD_STARTER_KIT_PRE:
Justin Chadwell's avatar
Justin Chadwell committed
505
		ret = fdt_setprop_string(dt, 0, "compatible",
506
507
					 "renesas,h3ulcb");
		break;
508
	case BOARD_EAGLE:
Justin Chadwell's avatar
Justin Chadwell committed
509
		ret = fdt_setprop_string(dt, 0, "compatible",
510
511
					 "renesas,eagle");
		break;
512
513
	case BOARD_EBISU:
	case BOARD_EBISU_4D:
Justin Chadwell's avatar
Justin Chadwell committed
514
		ret = fdt_setprop_string(dt, 0, "compatible",
515
516
					 "renesas,ebisu");
		break;
517
	case BOARD_DRAAK:
Justin Chadwell's avatar
Justin Chadwell committed
518
		ret = fdt_setprop_string(dt, 0, "compatible",
519
520
					 "renesas,draak");
		break;
521
522
523
524
525
526
527
528
529
530
531
	default:
		NOTICE("BL2: Cannot set compatible string, board unsupported\n");
		panic();
	}

	if (ret < 0) {
		NOTICE("BL2: Cannot set compatible string (ret=%i)\n", ret);
		panic();
	}

	reg = mmio_read_32(RCAR_PRR);
532
533
	switch (reg & PRR_PRODUCT_MASK) {
	case PRR_PRODUCT_H3:
Justin Chadwell's avatar
Justin Chadwell committed
534
		ret = fdt_appendprop_string(dt, 0, "compatible",
535
536
					    "renesas,r8a7795");
		break;
537
	case PRR_PRODUCT_M3:
Justin Chadwell's avatar
Justin Chadwell committed
538
		ret = fdt_appendprop_string(dt, 0, "compatible",
539
540
					    "renesas,r8a7796");
		break;
541
	case PRR_PRODUCT_M3N:
Justin Chadwell's avatar
Justin Chadwell committed
542
		ret = fdt_appendprop_string(dt, 0, "compatible",
543
544
					    "renesas,r8a77965");
		break;
545
	case PRR_PRODUCT_V3M:
Justin Chadwell's avatar
Justin Chadwell committed
546
		ret = fdt_appendprop_string(dt, 0, "compatible",
547
548
					    "renesas,r8a77970");
		break;
549
	case PRR_PRODUCT_E3:
Justin Chadwell's avatar
Justin Chadwell committed
550
		ret = fdt_appendprop_string(dt, 0, "compatible",
551
552
					    "renesas,r8a77990");
		break;
553
	case PRR_PRODUCT_D3:
Justin Chadwell's avatar
Justin Chadwell committed
554
		ret = fdt_appendprop_string(dt, 0, "compatible",
555
556
					    "renesas,r8a77995");
		break;
557
558
559
560
561
562
563
564
565
566
567
	default:
		NOTICE("BL2: Cannot set compatible string, SoC unsupported\n");
		panic();
	}

	if (ret < 0) {
		NOTICE("BL2: Cannot set compatible string (ret=%i)\n", ret);
		panic();
	}
}

568
static void bl2_add_dram_entry(uint64_t start, uint64_t size)
569
{
570
571
	char nodename[32] = { 0 };
	uint64_t fdtsize;
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
	int ret, node;

	fdtsize = cpu_to_fdt64(size);

	snprintf(nodename, sizeof(nodename), "memory@");
	unsigned_num_print(start, 16, nodename + strlen(nodename));
	node = ret = fdt_add_subnode(fdt, 0, nodename);
	if (ret < 0) {
		goto err;
	}

	ret = fdt_setprop_string(fdt, node, "device_type", "memory");
	if (ret < 0) {
		goto err;
	}

	ret = fdt_setprop_u64(fdt, node, "reg", start);
	if (ret < 0) {
		goto err;
	}

	ret = fdt_appendprop(fdt, node, "reg", &fdtsize,
			     sizeof(fdtsize));
	if (ret < 0) {
		goto err;
	}

	return;
err:
	NOTICE("BL2: Cannot add memory node [%llx - %llx] to FDT (ret=%i)\n",
		start, start + size - 1, ret);
	panic();
}

static void bl2_advertise_dram_entries(uint64_t dram_config[8])
{
	uint64_t start, size;
	int chan;
610
611
612
613
614
615
616

	for (chan = 0; chan < 4; chan++) {
		start = dram_config[2 * chan];
		size = dram_config[2 * chan + 1];
		if (!size)
			continue;

617
618
619
620
		NOTICE("BL2: CH%d: %llx - %llx, %lld %siB\n",
			chan, start, start + size - 1,
			(size >> 30) ? : size >> 20,
			(size >> 30) ? "G" : "M");
621
	}
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643

	/*
	 * We add the DT nodes in reverse order here. The fdt_add_subnode()
	 * adds the DT node before the first existing DT node, so we have
	 * to add them in reverse order to get nodes sorted by address in
	 * the resulting DT.
	 */
	for (chan = 3; chan >= 0; chan--) {
		start = dram_config[2 * chan];
		size = dram_config[2 * chan + 1];
		if (!size)
			continue;

		/*
		 * Channel 0 is mapped in 32bit space and the first
		 * 128 MiB are reserved
		 */
		if (chan == 0) {
			start = 0x48000000;
			size -= 0x8000000;
		}

644
		bl2_add_dram_entry(start, size);
645
	}
646
647
}

648
static void bl2_advertise_dram_size(uint32_t product)
649
{
650
651
652
653
654
655
656
	uint64_t dram_config[8] = {
		[0] = 0x400000000ULL,
		[2] = 0x500000000ULL,
		[4] = 0x600000000ULL,
		[6] = 0x700000000ULL,
	};

657
	switch (product) {
658
	case PRR_PRODUCT_H3:
659
660
#if (RCAR_DRAM_LPDDR4_MEMCONF == 0)
		/* 4GB(1GBx4) */
661
662
663
664
		dram_config[1] = 0x40000000ULL;
		dram_config[3] = 0x40000000ULL;
		dram_config[5] = 0x40000000ULL;
		dram_config[7] = 0x40000000ULL;
665
666
667
668
#elif (RCAR_DRAM_LPDDR4_MEMCONF == 1) && \
      (RCAR_DRAM_CHANNEL        == 5) && \
      (RCAR_DRAM_SPLIT          == 2)
		/* 4GB(2GBx2 2ch split) */
669
670
		dram_config[1] = 0x80000000ULL;
		dram_config[3] = 0x80000000ULL;
671
672
#elif (RCAR_DRAM_LPDDR4_MEMCONF == 1) && (RCAR_DRAM_CHANNEL == 15)
		/* 8GB(2GBx4: default) */
673
674
675
676
		dram_config[1] = 0x80000000ULL;
		dram_config[3] = 0x80000000ULL;
		dram_config[5] = 0x80000000ULL;
		dram_config[7] = 0x80000000ULL;
677
#endif /* RCAR_DRAM_LPDDR4_MEMCONF == 0 */
678
679
		break;

680
	case PRR_PRODUCT_M3:
681
682
683
684
685
#if (RCAR_GEN3_ULCB == 1)
		/* 2GB(1GBx2 2ch split) */
		dram_config[1] = 0x40000000ULL;
		dram_config[5] = 0x40000000ULL;
#else
686
		/* 4GB(2GBx2 2ch split) */
687
688
		dram_config[1] = 0x80000000ULL;
		dram_config[5] = 0x80000000ULL;
689
#endif
690
691
		break;

692
	case PRR_PRODUCT_M3N:
693
		/* 2GB(1GBx2) */
694
		dram_config[1] = 0x80000000ULL;
695
		break;
696

697
	case PRR_PRODUCT_V3M:
698
699
700
701
		/* 1GB(512MBx2) */
		dram_config[1] = 0x40000000ULL;
		break;

702
	case PRR_PRODUCT_E3:
703
704
#if (RCAR_DRAM_DDR3L_MEMCONF == 0)
		/* 1GB(512MBx2) */
705
		dram_config[1] = 0x40000000ULL;
706
707
#elif (RCAR_DRAM_DDR3L_MEMCONF == 1)
		/* 2GB(512MBx4) */
708
		dram_config[1] = 0x80000000ULL;
709
710
#elif (RCAR_DRAM_DDR3L_MEMCONF == 2)
		/* 4GB(1GBx4) */
711
		dram_config[1] = 0x100000000ULL;
712
#endif /* RCAR_DRAM_DDR3L_MEMCONF == 0 */
713
		break;
714

715
	case PRR_PRODUCT_D3:
716
717
718
		/* 512MB */
		dram_config[1] = 0x20000000ULL;
		break;
719
	}
720
721

	bl2_advertise_dram_entries(dram_config);
722
723
}

724
725
726
727
void bl2_el3_early_platform_setup(u_register_t arg1, u_register_t arg2,
				  u_register_t arg3, u_register_t arg4)
{
	uint32_t reg, midr, lcs, boot_dev, boot_cpu, sscg, type, rev;
728
	uint32_t product, product_cut, major, minor;
729
730
731
732
733
734
735
736
737
	int32_t ret;
	const char *str;
	const char *unknown = "unknown";
	const char *cpu_ca57 = "CA57";
	const char *cpu_ca53 = "CA53";
	const char *product_m3n = "M3N";
	const char *product_h3 = "H3";
	const char *product_m3 = "M3";
	const char *product_e3 = "E3";
738
	const char *product_d3 = "D3";
739
	const char *product_v3m = "V3M";
740
741
742
743
744
745
746
747
748
749
750
751
	const char *lcs_secure = "SE";
	const char *lcs_cm = "CM";
	const char *lcs_dm = "DM";
	const char *lcs_sd = "SD";
	const char *lcs_fa = "FA";
	const char *sscg_off = "PLL1 nonSSCG Clock select";
	const char *sscg_on = "PLL1 SSCG Clock select";
	const char *boot_hyper80 = "HyperFlash(80MHz)";
	const char *boot_qspi40 = "QSPI Flash(40MHz)";
	const char *boot_qspi80 = "QSPI Flash(80MHz)";
	const char *boot_emmc25x1 = "eMMC(25MHz x1)";
	const char *boot_emmc50x8 = "eMMC(50MHz x8)";
752
#if (RCAR_LSI == RCAR_E3) || (RCAR_LSI == RCAR_D3)
753
754
755
756
	const char *boot_hyper160 = "HyperFlash(150MHz)";
#else
	const char *boot_hyper160 = "HyperFlash(160MHz)";
#endif
757
758
759
#if (RCAR_LOSSY_ENABLE == 1)
	int fcnlnode;
#endif
760

761
762
	bl2_init_generic_timer();

763
764
765
766
767
768
769
770
771
	reg = mmio_read_32(RCAR_MODEMR);
	boot_dev = reg & MODEMR_BOOT_DEV_MASK;
	boot_cpu = reg & MODEMR_BOOT_CPU_MASK;

	bl2_cpg_init();

	if (boot_cpu == MODEMR_BOOT_CPU_CA57 ||
	    boot_cpu == MODEMR_BOOT_CPU_CA53) {
		rcar_pfc_init();
772
		rcar_console_boot_init();
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
	}

	plat_rcar_gic_driver_init();
	plat_rcar_gic_init();
	rcar_swdt_init();

	/* FIQ interrupts are taken to EL3 */
	write_scr_el3(read_scr_el3() | SCR_FIQ_BIT);

	write_daifclr(DAIF_FIQ_BIT);

	reg = read_midr();
	midr = reg & (MIDR_PN_MASK << MIDR_PN_SHIFT);
	switch (midr) {
	case MIDR_CA57:
		str = cpu_ca57;
		break;
	case MIDR_CA53:
		str = cpu_ca53;
		break;
	default:
		str = unknown;
		break;
	}

	NOTICE("BL2: R-Car Gen3 Initial Program Loader(%s) Rev.%s\n", str,
	       version_of_renesas);

	reg = mmio_read_32(RCAR_PRR);
802
803
	product_cut = reg & (PRR_PRODUCT_MASK | PRR_CUT_MASK);
	product = reg & PRR_PRODUCT_MASK;
804
805

	switch (product) {
806
	case PRR_PRODUCT_H3:
807
808
		str = product_h3;
		break;
809
	case PRR_PRODUCT_M3:
810
811
		str = product_m3;
		break;
812
	case PRR_PRODUCT_M3N:
813
814
		str = product_m3n;
		break;
815
	case PRR_PRODUCT_V3M:
816
817
		str = product_v3m;
		break;
818
	case PRR_PRODUCT_E3:
819
820
		str = product_e3;
		break;
821
	case PRR_PRODUCT_D3:
822
823
		str = product_d3;
		break;
824
825
826
827
828
	default:
		str = unknown;
		break;
	}

829
830
831
	if ((PRR_PRODUCT_M3 == product) &&
	    (PRR_PRODUCT_20 == (reg & RCAR_MAJOR_MASK))) {
		if (RCAR_M3_CUT_VER11 == (reg & PRR_CUT_MASK)) {
Marek Vasut's avatar
Marek Vasut committed
832
833
834
835
836
837
838
839
			/* M3 Ver.1.1 or Ver.1.2 */
			NOTICE("BL2: PRR is R-Car %s Ver.1.1 / Ver.1.2\n",
				str);
		} else {
			NOTICE("BL2: PRR is R-Car %s Ver.1.%d\n",
				str,
				(reg & RCAR_MINOR_MASK) + RCAR_M3_MINOR_OFFSET);
		}
840
841
842
843
844
845
846
	} else {
		major = (reg & RCAR_MAJOR_MASK) >> RCAR_MAJOR_SHIFT;
		major = major + RCAR_MAJOR_OFFSET;
		minor = reg & RCAR_MINOR_MASK;
		NOTICE("BL2: PRR is R-Car %s Ver.%d.%d\n", str, major, minor);
	}

847
	if (product == PRR_PRODUCT_E3) {
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
		reg = mmio_read_32(RCAR_MODEMR);
		sscg = reg & RCAR_SSCG_MASK;
		str = sscg == RCAR_SSCG_ENABLE ? sscg_on : sscg_off;
		NOTICE("BL2: %s\n", str);
	}

	rcar_get_board_type(&type, &rev);

	switch (type) {
	case BOARD_SALVATOR_X:
	case BOARD_KRIEK:
	case BOARD_STARTER_KIT:
	case BOARD_SALVATOR_XS:
	case BOARD_EBISU:
	case BOARD_STARTER_KIT_PRE:
	case BOARD_EBISU_4D:
864
	case BOARD_DRAAK:
865
	case BOARD_EAGLE:
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
		break;
	default:
		type = BOARD_UNKNOWN;
		break;
	}

	if (type == BOARD_UNKNOWN || rev == BOARD_REV_UNKNOWN)
		NOTICE("BL2: Board is %s Rev.---\n", GET_BOARD_NAME(type));
	else {
		NOTICE("BL2: Board is %s Rev.%d.%d\n",
		       GET_BOARD_NAME(type),
		       GET_BOARD_MAJOR(rev), GET_BOARD_MINOR(rev));
	}

#if RCAR_LSI != RCAR_AUTO
	if (product != TARGET_PRODUCT) {
		ERROR("BL2: IPL was been built for the %s.\n", TARGET_NAME);
		ERROR("BL2: Please write the correct IPL to flash memory.\n");
		panic();
	}
#endif
	rcar_avs_init();
	rcar_avs_setting();

	switch (boot_dev) {
	case MODEMR_BOOT_DEV_HYPERFLASH160:
		str = boot_hyper160;
		break;
	case MODEMR_BOOT_DEV_HYPERFLASH80:
		str = boot_hyper80;
		break;
	case MODEMR_BOOT_DEV_QSPI_FLASH40:
		str = boot_qspi40;
		break;
	case MODEMR_BOOT_DEV_QSPI_FLASH80:
		str = boot_qspi80;
		break;
	case MODEMR_BOOT_DEV_EMMC_25X1:
904
905
906
907
#if RCAR_LSI == RCAR_D3
		ERROR("BL2: Failed to Initialize. eMMC is not supported.\n");
		panic();
#endif
908
909
910
		str = boot_emmc25x1;
		break;
	case MODEMR_BOOT_DEV_EMMC_50X8:
911
912
913
914
#if RCAR_LSI == RCAR_D3
		ERROR("BL2: Failed to Initialize. eMMC is not supported.\n");
		panic();
#endif
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
		str = boot_emmc50x8;
		break;
	default:
		str = unknown;
		break;
	}
	NOTICE("BL2: Boot device is %s\n", str);

	rcar_avs_setting();
	reg = rcar_rom_get_lcs(&lcs);
	if (reg) {
		str = unknown;
		goto lcm_state;
	}

	switch (lcs) {
	case LCS_CM:
		str = lcs_cm;
		break;
	case LCS_DM:
		str = lcs_dm;
		break;
	case LCS_SD:
		str = lcs_sd;
		break;
	case LCS_SE:
		str = lcs_secure;
		break;
	case LCS_FA:
		str = lcs_fa;
		break;
	default:
		str = unknown;
		break;
	}

lcm_state:
	NOTICE("BL2: LCM state is %s\n", str);

	rcar_avs_end();
	is_ddr_backup_mode();

	bl2_tzram_layout.total_base = BL31_BASE;
	bl2_tzram_layout.total_size = BL31_LIMIT - BL31_BASE;

	if (boot_cpu == MODEMR_BOOT_CPU_CA57 ||
	    boot_cpu == MODEMR_BOOT_CPU_CA53) {
		ret = rcar_dram_init();
		if (ret) {
			NOTICE("BL2: Failed to DRAM initialize (%d).\n", ret);
			panic();
		}
		rcar_qos_init();
	}

970
971
972
973
974
975
976
	/* Set up FDT */
	ret = fdt_create_empty_tree(fdt, sizeof(fdt_blob));
	if (ret) {
		NOTICE("BL2: Cannot allocate FDT for U-Boot (ret=%i)\n", ret);
		panic();
	}

977
978
979
	/* Add platform compatible string */
	bl2_populate_compatible_string(fdt);

980
981
982
	/* Print DRAM layout */
	bl2_advertise_dram_size(product);

983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
	if (boot_dev == MODEMR_BOOT_DEV_EMMC_25X1 ||
	    boot_dev == MODEMR_BOOT_DEV_EMMC_50X8) {
		if (rcar_emmc_init() != EMMC_SUCCESS) {
			NOTICE("BL2: Failed to eMMC driver initialize.\n");
			panic();
		}
		rcar_emmc_memcard_power(EMMC_POWER_ON);
		if (rcar_emmc_mount() != EMMC_SUCCESS) {
			NOTICE("BL2: Failed to eMMC mount operation.\n");
			panic();
		}
	} else {
		rcar_rpc_init();
		rcar_dma_init();
	}

	reg = mmio_read_32(RST_WDTRSTCR);
	reg &= ~WDTRSTCR_RWDT_RSTMSK;
	reg |= WDTRSTCR_PASSWORD;
	mmio_write_32(RST_WDTRSTCR, reg);

	mmio_write_32(CPG_CPGWPR, CPGWPR_PASSWORD);
	mmio_write_32(CPG_CPGWPCR, CPGWPCR_PASSWORD);

	reg = mmio_read_32(RCAR_PRR);
	if ((reg & RCAR_CPU_MASK_CA57) == RCAR_CPU_HAVE_CA57)
		mmio_write_32(CPG_CA57DBGRCR,
			      DBGCPUPREN | mmio_read_32(CPG_CA57DBGRCR));

	if ((reg & RCAR_CPU_MASK_CA53) == RCAR_CPU_HAVE_CA53)
		mmio_write_32(CPG_CA53DBGRCR,
			      DBGCPUPREN | mmio_read_32(CPG_CA53DBGRCR));

1016
	if (product_cut == PRR_PRODUCT_H3_CUT10) {
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
		reg = mmio_read_32(CPG_PLL2CR);
		reg &= ~((uint32_t) 1 << 5);
		mmio_write_32(CPG_PLL2CR, reg);

		reg = mmio_read_32(CPG_PLL4CR);
		reg &= ~((uint32_t) 1 << 5);
		mmio_write_32(CPG_PLL4CR, reg);

		reg = mmio_read_32(CPG_PLL0CR);
		reg &= ~((uint32_t) 1 << 12);
		mmio_write_32(CPG_PLL0CR, reg);
	}
#if (RCAR_LOSSY_ENABLE == 1)
	NOTICE("BL2: Lossy Decomp areas\n");
1031
1032
1033
1034
1035
1036
1037
1038

	fcnlnode = fdt_add_subnode(fdt, 0, "reserved-memory");
	if (fcnlnode < 0) {
		NOTICE("BL2: Cannot create reserved mem node (ret=%i)\n",
			fcnlnode);
		panic();
	}

1039
	bl2_lossy_setting(0, LOSSY_ST_ADDR0, LOSSY_END_ADDR0,
1040
			  LOSSY_FMT0, LOSSY_ENA_DIS0, fcnlnode);
1041
	bl2_lossy_setting(1, LOSSY_ST_ADDR1, LOSSY_END_ADDR1,
1042
			  LOSSY_FMT1, LOSSY_ENA_DIS1, fcnlnode);
1043
	bl2_lossy_setting(2, LOSSY_ST_ADDR2, LOSSY_END_ADDR2,
1044
			  LOSSY_FMT2, LOSSY_ENA_DIS2, fcnlnode);
1045
1046
#endif

1047
1048
1049
	fdt_pack(fdt);
	NOTICE("BL2: FDT at %p\n", fdt);

1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
	if (boot_dev == MODEMR_BOOT_DEV_EMMC_25X1 ||
	    boot_dev == MODEMR_BOOT_DEV_EMMC_50X8)
		rcar_io_emmc_setup();
	else
		rcar_io_setup();
}

void bl2_el3_plat_arch_setup(void)
{
#if RCAR_BL2_DCACHE == 1
	NOTICE("BL2: D-Cache enable\n");
	rcar_configure_mmu_el3(BL2_BASE,
1062
			       BL2_END - BL2_BASE,
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
			       BL2_RO_BASE, BL2_RO_LIMIT
#if USE_COHERENT_MEM
			       , BL2_COHERENT_RAM_BASE, BL2_COHERENT_RAM_LIMIT
#endif
	    );
#endif
}

void bl2_platform_setup(void)
{

}
1075
1076
1077

static void bl2_init_generic_timer(void)
{
1078
/* FIXME: V3M 16.666 MHz ? */
1079
1080
1081
#if RCAR_LSI == RCAR_D3
	uint32_t reg_cntfid = EXTAL_DRAAK;
#elif RCAR_LSI == RCAR_E3
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
	uint32_t reg_cntfid = EXTAL_EBISU;
#else /* RCAR_LSI == RCAR_E3 */
	uint32_t reg;
	uint32_t reg_cntfid;
	uint32_t modemr;
	uint32_t modemr_pll;
	uint32_t board_type;
	uint32_t board_rev;
	uint32_t pll_table[] = {
		EXTAL_MD14_MD13_TYPE_0,	/* MD14/MD13 : 0b00 */
		EXTAL_MD14_MD13_TYPE_1,	/* MD14/MD13 : 0b01 */
		EXTAL_MD14_MD13_TYPE_2,	/* MD14/MD13 : 0b10 */
		EXTAL_MD14_MD13_TYPE_3	/* MD14/MD13 : 0b11 */
	};

	modemr = mmio_read_32(RCAR_MODEMR);
	modemr_pll = (modemr & MODEMR_BOOT_PLL_MASK);

	/* Set frequency data in CNTFID0 */
	reg_cntfid = pll_table[modemr_pll >> MODEMR_BOOT_PLL_SHIFT];
1102
	reg = mmio_read_32(RCAR_PRR) & (PRR_PRODUCT_MASK | PRR_CUT_MASK);
1103
1104
1105
1106
1107
1108
1109
1110
	switch (modemr_pll) {
	case MD14_MD13_TYPE_0:
		rcar_get_board_type(&board_type, &board_rev);
		if (BOARD_SALVATOR_XS == board_type) {
			reg_cntfid = EXTAL_SALVATOR_XS;
		}
		break;
	case MD14_MD13_TYPE_3:
1111
		if (PRR_PRODUCT_H3_CUT10 == reg) {
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
			reg_cntfid = reg_cntfid >> 1U;
		}
		break;
	default:
		/* none */
		break;
	}
#endif /* RCAR_LSI == RCAR_E3 */
	/* Update memory mapped and register based freqency */
	write_cntfrq_el0((u_register_t )reg_cntfid);
	mmio_write_32(ARM_SYS_CNTCTL_BASE + (uintptr_t)CNTFID_OFF, reg_cntfid);
	/* Enable counter */
	mmio_setbits_32(RCAR_CNTC_BASE + (uintptr_t)CNTCR_OFF,
			(uint32_t)CNTCR_EN);
}