soc.c 14 KB

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