xref: /rk3399_ARM-atf/plat/nxp/soc-lx2160a/soc.c (revision 9616db154b0be0abe27f7d267482772b54c88664)
1 /*
2  * Copyright 2018-2021 NXP
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include <assert.h>
9 
10 #include <arch.h>
11 #include <bl31/interrupt_mgmt.h>
12 #include <caam.h>
13 #include <cassert.h>
14 #include <ccn.h>
15 #include <common/debug.h>
16 #include <dcfg.h>
17 #ifdef I2C_INIT
18 #include <i2c.h>
19 #endif
20 #include <lib/mmio.h>
21 #include <lib/xlat_tables/xlat_tables_v2.h>
22 #include <ls_interconnect.h>
23 #ifdef POLICY_FUSE_PROVISION
24 #include <nxp_gpio.h>
25 #endif
26 #if TRUSTED_BOARD_BOOT
27 #include <nxp_smmu.h>
28 #endif
29 #include <nxp_timer.h>
30 #include <plat_console.h>
31 #include <plat_gic.h>
32 #include <plat_tzc400.h>
33 #include <pmu.h>
34 #if defined(NXP_SFP_ENABLED)
35 #include <sfp.h>
36 #endif
37 
38 #include <errata.h>
39 #include <ls_interrupt_mgmt.h>
40 #include "plat_common.h"
41 #ifdef NXP_NV_SW_MAINT_LAST_EXEC_DATA
42 #include <plat_nv_storage.h>
43 #endif
44 #ifdef NXP_WARM_BOOT
45 #include <plat_warm_rst.h>
46 #endif
47 #include "platform_def.h"
48 #include "soc.h"
49 
50 static struct soc_type soc_list[] =  {
51 	SOC_ENTRY(LX2160A, LX2160A, 8, 2),
52 	SOC_ENTRY(LX2080A, LX2080A, 8, 1),
53 	SOC_ENTRY(LX2120A, LX2120A, 6, 2),
54 };
55 
56 static dcfg_init_info_t dcfg_init_data = {
57 			.g_nxp_dcfg_addr = NXP_DCFG_ADDR,
58 			.nxp_sysclk_freq = NXP_SYSCLK_FREQ,
59 			.nxp_ddrclk_freq = NXP_DDRCLK_FREQ,
60 			.nxp_plat_clk_divider = NXP_PLATFORM_CLK_DIVIDER,
61 		};
62 static const unsigned char master_to_6rn_id_map[] = {
63 	PLAT_6CLUSTER_TO_CCN_ID_MAP
64 };
65 
66 static const unsigned char master_to_rn_id_map[] = {
67 	PLAT_CLUSTER_TO_CCN_ID_MAP
68 };
69 
70 CASSERT(ARRAY_SIZE(master_to_rn_id_map) == NUMBER_OF_CLUSTERS,
71 		assert_invalid_cluster_count_for_ccn_variant);
72 
73 static const ccn_desc_t plat_six_cluster_ccn_desc = {
74 	.periphbase = NXP_CCN_ADDR,
75 	.num_masters = ARRAY_SIZE(master_to_6rn_id_map),
76 	.master_to_rn_id_map = master_to_6rn_id_map
77 };
78 
79 static const ccn_desc_t plat_ccn_desc = {
80 	.periphbase = NXP_CCN_ADDR,
81 	.num_masters = ARRAY_SIZE(master_to_rn_id_map),
82 	.master_to_rn_id_map = master_to_rn_id_map
83 };
84 
85 /*******************************************************************************
86  * This function returns the number of clusters in the SoC
87  ******************************************************************************/
88 static unsigned int get_num_cluster(void)
89 {
90 	const soc_info_t *soc_info = get_soc_info();
91 	uint32_t num_clusters = NUMBER_OF_CLUSTERS;
92 	unsigned int i;
93 
94 	for (i = 0U; i < ARRAY_SIZE(soc_list); i++) {
95 		if (soc_list[i].personality == soc_info->personality) {
96 			num_clusters = soc_list[i].num_clusters;
97 			break;
98 		}
99 	}
100 
101 	VERBOSE("NUM of cluster = 0x%x\n", num_clusters);
102 
103 	return num_clusters;
104 }
105 
106 
107 /******************************************************************************
108  * Function returns the base counter frequency
109  * after reading the first entry at CNTFID0 (0x20 offset).
110  *
111  * Function is used by:
112  *   1. ARM common code for PSCI management.
113  *   2. ARM Generic Timer init.
114  *
115  *****************************************************************************/
116 unsigned int plat_get_syscnt_freq2(void)
117 {
118 	unsigned int counter_base_frequency;
119 	/*
120 	 * Below register specifies the base frequency of the system counter.
121 	 * As per NXP Board Manuals:
122 	 * The system counter always works with SYS_REF_CLK/4 frequency clock.
123 	 *
124 	 *
125 	 */
126 	counter_base_frequency = mmio_read_32(NXP_TIMER_ADDR + CNTFID_OFF);
127 
128 	return counter_base_frequency;
129 }
130 
131 #ifdef IMAGE_BL2
132 
133 #ifdef POLICY_FUSE_PROVISION
134 static gpio_init_info_t gpio_init_data = {
135 	.gpio1_base_addr = NXP_GPIO1_ADDR,
136 	.gpio2_base_addr = NXP_GPIO2_ADDR,
137 	.gpio3_base_addr = NXP_GPIO3_ADDR,
138 	.gpio4_base_addr = NXP_GPIO4_ADDR,
139 };
140 #endif
141 
142 static void soc_interconnect_config(void)
143 {
144 	unsigned long long val = 0x0U;
145 
146 	uint32_t num_clusters = get_num_cluster();
147 
148 	if (num_clusters == 6U) {
149 		ccn_init(&plat_six_cluster_ccn_desc);
150 	} else {
151 		ccn_init(&plat_ccn_desc);
152 	}
153 
154 	/*
155 	 * Enable Interconnect coherency for the primary CPU's cluster.
156 	 */
157 	plat_ls_interconnect_enter_coherency(num_clusters);
158 
159 	val = ccn_read_node_reg(NODE_TYPE_HNI, 13, PCIeRC_RN_I_NODE_ID_OFFSET);
160 	val |= (1 << 17);
161 	ccn_write_node_reg(NODE_TYPE_HNI, 13, PCIeRC_RN_I_NODE_ID_OFFSET, val);
162 
163 	/* PCIe is Connected to RN-I 17 which is connected to HN-I 13. */
164 	val = ccn_read_node_reg(NODE_TYPE_HNI, 30, PCIeRC_RN_I_NODE_ID_OFFSET);
165 	val |= (1 << 17);
166 	ccn_write_node_reg(NODE_TYPE_HNI, 30, PCIeRC_RN_I_NODE_ID_OFFSET, val);
167 
168 	val = ccn_read_node_reg(NODE_TYPE_HNI, 13, SA_AUX_CTRL_REG_OFFSET);
169 	val |= SERIALIZE_DEV_nGnRnE_WRITES;
170 	ccn_write_node_reg(NODE_TYPE_HNI, 13, SA_AUX_CTRL_REG_OFFSET, val);
171 
172 	val = ccn_read_node_reg(NODE_TYPE_HNI, 30, SA_AUX_CTRL_REG_OFFSET);
173 	val &= ~(ENABLE_RESERVE_BIT53);
174 	val |= SERIALIZE_DEV_nGnRnE_WRITES;
175 	ccn_write_node_reg(NODE_TYPE_HNI, 30, SA_AUX_CTRL_REG_OFFSET, val);
176 
177 	val = ccn_read_node_reg(NODE_TYPE_HNI, 13, PoS_CONTROL_REG_OFFSET);
178 	val &= ~(HNI_POS_EN);
179 	ccn_write_node_reg(NODE_TYPE_HNI, 13, PoS_CONTROL_REG_OFFSET, val);
180 
181 	val = ccn_read_node_reg(NODE_TYPE_HNI, 30, PoS_CONTROL_REG_OFFSET);
182 	val &= ~(HNI_POS_EN);
183 	ccn_write_node_reg(NODE_TYPE_HNI, 30, PoS_CONTROL_REG_OFFSET, val);
184 
185 	val = ccn_read_node_reg(NODE_TYPE_HNI, 13, SA_AUX_CTRL_REG_OFFSET);
186 	val &= ~(POS_EARLY_WR_COMP_EN);
187 	ccn_write_node_reg(NODE_TYPE_HNI, 13, SA_AUX_CTRL_REG_OFFSET, val);
188 
189 	val = ccn_read_node_reg(NODE_TYPE_HNI, 30, SA_AUX_CTRL_REG_OFFSET);
190 	val &= ~(POS_EARLY_WR_COMP_EN);
191 	ccn_write_node_reg(NODE_TYPE_HNI, 30, SA_AUX_CTRL_REG_OFFSET, val);
192 
193 #if POLICY_PERF_WRIOP
194 	uint16_t wriop_rni = 0U;
195 
196 	if (POLICY_PERF_WRIOP == 1) {
197 		wriop_rni = 7U;
198 	} else if (POLICY_PERF_WRIOP == 2) {
199 		wriop_rni = 23U;
200 	} else {
201 		ERROR("Incorrect WRIOP selected.\n");
202 		panic();
203 	}
204 
205 	val = ccn_read_node_reg(NODE_TYPE_RNI, wriop_rni,
206 				SA_AUX_CTRL_REG_OFFSET);
207 	val |= ENABLE_WUO;
208 	ccn_write_node_reg(NODE_TYPE_HNI, wriop_rni, SA_AUX_CTRL_REG_OFFSET,
209 			   val);
210 #else
211 	val = ccn_read_node_reg(NODE_TYPE_RNI, 17, SA_AUX_CTRL_REG_OFFSET);
212 	val |= ENABLE_WUO;
213 	ccn_write_node_reg(NODE_TYPE_RNI, 17, SA_AUX_CTRL_REG_OFFSET, val);
214 #endif
215 }
216 
217 
218 void soc_preload_setup(void)
219 {
220 	dram_regions_info_t *info_dram_regions = get_dram_regions_info();
221 #if defined(NXP_WARM_BOOT)
222 	bool warm_reset = is_warm_boot();
223 #endif
224 	info_dram_regions->total_dram_size =
225 #if defined(NXP_WARM_BOOT)
226 						init_ddr(warm_reset);
227 #else
228 						init_ddr();
229 #endif
230 }
231 
232 /*******************************************************************************
233  * This function implements soc specific erratas
234  * This is called before DDR is initialized or MMU is enabled
235  ******************************************************************************/
236 void soc_early_init(void)
237 {
238 	dcfg_init(&dcfg_init_data);
239 #ifdef POLICY_FUSE_PROVISION
240 	gpio_init(&gpio_init_data);
241 	sec_init(NXP_CAAM_ADDR);
242 #endif
243 #if LOG_LEVEL > 0
244 	/* Initialize the console to provide early debug support */
245 	plat_console_init(NXP_CONSOLE_ADDR,
246 				NXP_UART_CLK_DIVIDER, NXP_CONSOLE_BAUDRATE);
247 #endif
248 
249 	enable_timer_base_to_cluster(NXP_PMU_ADDR);
250 	soc_interconnect_config();
251 
252 	enum  boot_device dev = get_boot_dev();
253 	/* Mark the buffer for SD in OCRAM as non secure.
254 	 * The buffer is assumed to be at end of OCRAM for
255 	 * the logic below to calculate TZPC programming
256 	 */
257 	if (dev == BOOT_DEVICE_EMMC || dev == BOOT_DEVICE_SDHC2_EMMC) {
258 		/* Calculate the region in OCRAM which is secure
259 		 * The buffer for SD needs to be marked non-secure
260 		 * to allow SD to do DMA operations on it
261 		 */
262 		uint32_t secure_region = (NXP_OCRAM_SIZE
263 						- NXP_SD_BLOCK_BUF_SIZE);
264 		uint32_t mask = secure_region/TZPC_BLOCK_SIZE;
265 
266 		mmio_write_32(NXP_OCRAM_TZPC_ADDR, mask);
267 
268 		/* Add the entry for buffer in MMU Table */
269 		mmap_add_region(NXP_SD_BLOCK_BUF_ADDR, NXP_SD_BLOCK_BUF_ADDR,
270 				NXP_SD_BLOCK_BUF_SIZE,
271 				MT_DEVICE | MT_RW | MT_NS);
272 	}
273 
274 	soc_errata();
275 
276 #if (TRUSTED_BOARD_BOOT) || defined(POLICY_FUSE_PROVISION)
277 	sfp_init(NXP_SFP_ADDR);
278 #endif
279 
280 #if TRUSTED_BOARD_BOOT
281 	uint32_t mode;
282 
283 	/* For secure boot disable SMMU.
284 	 * Later when platform security policy comes in picture,
285 	 * this might get modified based on the policy
286 	 */
287 	if (check_boot_mode_secure(&mode) == true) {
288 		bypass_smmu(NXP_SMMU_ADDR);
289 	}
290 
291 	/* For Mbedtls currently crypto is not supported via CAAM
292 	 * enable it when that support is there. In tbbr.mk
293 	 * the CAAM_INTEG is set as 0.
294 	 */
295 
296 #ifndef MBEDTLS_X509
297 	/* Initialize the crypto accelerator if enabled */
298 	if (is_sec_enabled() == false)
299 		INFO("SEC is disabled.\n");
300 	else
301 		sec_init(NXP_CAAM_ADDR);
302 #endif
303 #endif
304 
305 	/*
306 	 * Initialize system level generic timer for Layerscape Socs.
307 	 */
308 	delay_timer_init(NXP_TIMER_ADDR);
309 	i2c_init(NXP_I2C_ADDR);
310 }
311 
312 void soc_bl2_prepare_exit(void)
313 {
314 #if defined(NXP_SFP_ENABLED) && defined(DISABLE_FUSE_WRITE)
315 	set_sfp_wr_disable();
316 #endif
317 }
318 
319 /*****************************************************************************
320  * This function returns the boot device based on RCW_SRC
321  ****************************************************************************/
322 enum boot_device get_boot_dev(void)
323 {
324 	enum boot_device src = BOOT_DEVICE_NONE;
325 	uint32_t porsr1;
326 	uint32_t rcw_src;
327 
328 	porsr1 = read_reg_porsr1();
329 
330 	rcw_src = (porsr1 & PORSR1_RCW_MASK) >> PORSR1_RCW_SHIFT;
331 
332 	switch (rcw_src) {
333 	case FLEXSPI_NOR:
334 		src = BOOT_DEVICE_FLEXSPI_NOR;
335 		INFO("RCW BOOT SRC is FLEXSPI NOR\n");
336 		break;
337 	case FLEXSPI_NAND2K_VAL:
338 	case FLEXSPI_NAND4K_VAL:
339 		INFO("RCW BOOT SRC is FLEXSPI NAND\n");
340 		src = BOOT_DEVICE_FLEXSPI_NAND;
341 		break;
342 	case SDHC1_VAL:
343 		src = BOOT_DEVICE_EMMC;
344 		INFO("RCW BOOT SRC is SD\n");
345 		break;
346 	case SDHC2_VAL:
347 		src = BOOT_DEVICE_SDHC2_EMMC;
348 		INFO("RCW BOOT SRC is EMMC\n");
349 		break;
350 	default:
351 		break;
352 	}
353 
354 	return src;
355 }
356 
357 
358 void soc_mem_access(void)
359 {
360 	const devdisr5_info_t *devdisr5_info = get_devdisr5_info();
361 	dram_regions_info_t *info_dram_regions = get_dram_regions_info();
362 	struct tzc400_reg tzc400_reg_list[MAX_NUM_TZC_REGION];
363 	int dram_idx, index = 0U;
364 
365 	for (dram_idx = 0U; dram_idx < info_dram_regions->num_dram_regions;
366 	     dram_idx++) {
367 		if (info_dram_regions->region[dram_idx].size == 0) {
368 			ERROR("DDR init failure, or");
369 			ERROR("DRAM regions not populated correctly.\n");
370 			break;
371 		}
372 
373 		index = populate_tzc400_reg_list(tzc400_reg_list,
374 				dram_idx, index,
375 				info_dram_regions->region[dram_idx].addr,
376 				info_dram_regions->region[dram_idx].size,
377 				NXP_SECURE_DRAM_SIZE, NXP_SP_SHRD_DRAM_SIZE);
378 	}
379 
380 	if (devdisr5_info->ddrc1_present != 0) {
381 		INFO("DDR Controller 1.\n");
382 		mem_access_setup(NXP_TZC_ADDR, index,
383 				tzc400_reg_list);
384 		mem_access_setup(NXP_TZC3_ADDR, index,
385 				tzc400_reg_list);
386 	}
387 	if (devdisr5_info->ddrc2_present != 0) {
388 		INFO("DDR Controller 2.\n");
389 		mem_access_setup(NXP_TZC2_ADDR, index,
390 				tzc400_reg_list);
391 		mem_access_setup(NXP_TZC4_ADDR, index,
392 				tzc400_reg_list);
393 	}
394 }
395 
396 #else
397 const unsigned char _power_domain_tree_desc[] = {1, 8, 2, 2, 2, 2, 2, 2, 2, 2};
398 
399 CASSERT(NUMBER_OF_CLUSTERS && NUMBER_OF_CLUSTERS <= 256,
400 		assert_invalid_lx2160a_cluster_count);
401 
402 /******************************************************************************
403  * This function returns the SoC topology
404  ****************************************************************************/
405 
406 const unsigned char *plat_get_power_domain_tree_desc(void)
407 {
408 
409 	return _power_domain_tree_desc;
410 }
411 
412 /*******************************************************************************
413  * This function returns the core count within the cluster corresponding to
414  * `mpidr`.
415  ******************************************************************************/
416 unsigned int plat_ls_get_cluster_core_count(u_register_t mpidr)
417 {
418 	return CORES_PER_CLUSTER;
419 }
420 
421 
422 void soc_early_platform_setup2(void)
423 {
424 	dcfg_init(&dcfg_init_data);
425 	/*
426 	 * Initialize system level generic timer for Socs
427 	 */
428 	delay_timer_init(NXP_TIMER_ADDR);
429 
430 #if LOG_LEVEL > 0
431 	/* Initialize the console to provide early debug support */
432 	plat_console_init(NXP_CONSOLE_ADDR,
433 			  NXP_UART_CLK_DIVIDER, NXP_CONSOLE_BAUDRATE);
434 #endif
435 }
436 
437 void soc_platform_setup(void)
438 {
439 	/* Initialize the GIC driver, cpu and distributor interfaces */
440 	static uintptr_t target_mask_array[PLATFORM_CORE_COUNT];
441 	static interrupt_prop_t ls_interrupt_props[] = {
442 		PLAT_LS_G1S_IRQ_PROPS(INTR_GROUP1S),
443 		PLAT_LS_G0_IRQ_PROPS(INTR_GROUP0)
444 	};
445 
446 	plat_ls_gic_driver_init(NXP_GICD_ADDR, NXP_GICR_ADDR,
447 				PLATFORM_CORE_COUNT,
448 				ls_interrupt_props,
449 				ARRAY_SIZE(ls_interrupt_props),
450 				target_mask_array,
451 				plat_core_pos);
452 
453 	plat_ls_gic_init();
454 	enable_init_timer();
455 #ifdef LS_SYS_TIMCTL_BASE
456 	ls_configure_sys_timer(LS_SYS_TIMCTL_BASE,
457 			       LS_CONFIG_CNTACR,
458 			       PLAT_LS_NSTIMER_FRAME_ID);
459 #endif
460 }
461 
462 /*******************************************************************************
463  * This function initializes the soc from the BL31 module
464  ******************************************************************************/
465 void soc_init(void)
466 {
467 	 /* low-level init of the soc */
468 	soc_init_start();
469 	soc_init_percpu();
470 	_init_global_data();
471 	_initialize_psci();
472 
473 	if (ccn_get_part0_id(NXP_CCN_ADDR) != CCN_508_PART0_ID) {
474 		ERROR("Unrecognized CCN variant detected.");
475 		ERROR("Only CCN-508 is supported\n");
476 		panic();
477 	}
478 
479 	uint32_t num_clusters = get_num_cluster();
480 
481 	if (num_clusters == 6U) {
482 		ccn_init(&plat_six_cluster_ccn_desc);
483 	} else {
484 		ccn_init(&plat_ccn_desc);
485 	}
486 
487 	plat_ls_interconnect_enter_coherency(num_clusters);
488 
489 	/* Set platform security policies */
490 	_set_platform_security();
491 
492 	 /* make sure any parallel init tasks are finished */
493 	soc_init_finish();
494 
495 	/* Initialize the crypto accelerator if enabled */
496 	if (is_sec_enabled() == false) {
497 		INFO("SEC is disabled.\n");
498 	} else {
499 		sec_init(NXP_CAAM_ADDR);
500 	}
501 
502 }
503 
504 #ifdef NXP_WDOG_RESTART
505 static uint64_t wdog_interrupt_handler(uint32_t id, uint32_t flags,
506 					  void *handle, void *cookie)
507 {
508 	uint8_t data = WDOG_RESET_FLAG;
509 
510 	wr_nv_app_data(WDT_RESET_FLAG_OFFSET,
511 		       (uint8_t *)&data, sizeof(data));
512 
513 	mmio_write_32(NXP_RST_ADDR + RSTCNTL_OFFSET, SW_RST_REQ_INIT);
514 
515 	return 0;
516 }
517 #endif
518 
519 void soc_runtime_setup(void)
520 {
521 
522 #ifdef NXP_WDOG_RESTART
523 	request_intr_type_el3(BL31_NS_WDOG_WS1, wdog_interrupt_handler);
524 #endif
525 }
526 #endif
527