soc.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * Copyright 2018-2022 NXP
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <arch.h>
  8. #include <caam.h>
  9. #include <cassert.h>
  10. #include <cci.h>
  11. #include <common/debug.h>
  12. #include <dcfg.h>
  13. #ifdef I2C_INIT
  14. #include <i2c.h>
  15. #endif
  16. #include <lib/mmio.h>
  17. #include <lib/xlat_tables/xlat_tables_v2.h>
  18. #include <ls_interconnect.h>
  19. #ifdef POLICY_FUSE_PROVISION
  20. #include <nxp_gpio.h>
  21. #endif
  22. #include <nxp_smmu.h>
  23. #include <nxp_timer.h>
  24. #include <plat_console.h>
  25. #include <plat_gic.h>
  26. #include <plat_tzc400.h>
  27. #include <scfg.h>
  28. #if defined(NXP_SFP_ENABLED)
  29. #include <sfp.h>
  30. #endif
  31. #include <errata.h>
  32. #include <ns_access.h>
  33. #ifdef CONFIG_OCRAM_ECC_EN
  34. #include <ocram.h>
  35. #endif
  36. #include <plat_common.h>
  37. #include <platform_def.h>
  38. #include <soc.h>
  39. static dcfg_init_info_t dcfg_init_data = {
  40. .g_nxp_dcfg_addr = NXP_DCFG_ADDR,
  41. .nxp_sysclk_freq = NXP_SYSCLK_FREQ,
  42. .nxp_ddrclk_freq = NXP_DDRCLK_FREQ,
  43. .nxp_plat_clk_divider = NXP_PLATFORM_CLK_DIVIDER,
  44. };
  45. /* Function to return the SoC SYS CLK */
  46. static unsigned int get_sys_clk(void)
  47. {
  48. return NXP_SYSCLK_FREQ;
  49. }
  50. /*
  51. * Function returns the base counter frequency
  52. * after reading the first entry at CNTFID0 (0x20 offset).
  53. *
  54. * Function is used by:
  55. * 1. ARM common code for PSCI management.
  56. * 2. ARM Generic Timer init.
  57. *
  58. */
  59. unsigned int plat_get_syscnt_freq2(void)
  60. {
  61. unsigned int counter_base_frequency;
  62. counter_base_frequency = get_sys_clk() / 4;
  63. return counter_base_frequency;
  64. }
  65. #ifdef IMAGE_BL2
  66. /* Functions for BL2 */
  67. static struct soc_type soc_list[] = {
  68. SOC_ENTRY(LS1046A, LS1046A, 1, 4),
  69. SOC_ENTRY(LS1046AE, LS1046AE, 1, 4),
  70. SOC_ENTRY(LS1026A, LS1026A, 1, 2),
  71. SOC_ENTRY(LS1026AE, LS1026AE, 1, 2),
  72. };
  73. #ifdef POLICY_FUSE_PROVISION
  74. static gpio_init_info_t gpio_init_data = {
  75. .gpio1_base_addr = NXP_GPIO1_ADDR,
  76. .gpio2_base_addr = NXP_GPIO2_ADDR,
  77. .gpio3_base_addr = NXP_GPIO3_ADDR,
  78. .gpio4_base_addr = NXP_GPIO4_ADDR,
  79. };
  80. #endif
  81. /*
  82. * Function to set the base counter frequency at
  83. * the first entry of the Frequency Mode Table,
  84. * at CNTFID0 (0x20 offset).
  85. *
  86. * Set the value of the pirmary core register cntfrq_el0.
  87. */
  88. static void set_base_freq_CNTFID0(void)
  89. {
  90. /*
  91. * Below register specifies the base frequency of the system counter.
  92. * As per NXP Board Manuals:
  93. * The system counter always works with SYS_REF_CLK/4 frequency clock.
  94. */
  95. unsigned int counter_base_frequency = get_sys_clk() / 4;
  96. /* Setting the frequency in the Frequency modes table.
  97. *
  98. * Note: The value for ls1046ardb board at this offset
  99. * is not RW as stated. This offset have the
  100. * fixed value of 100000400 Hz.
  101. *
  102. * The below code line has no effect.
  103. * Keeping it for other platforms where it has effect.
  104. */
  105. mmio_write_32(NXP_TIMER_ADDR + CNTFID_OFF, counter_base_frequency);
  106. write_cntfrq_el0(counter_base_frequency);
  107. }
  108. void soc_preload_setup(void)
  109. {
  110. }
  111. /*
  112. * This function implements soc specific erratas
  113. * This is called before DDR is initialized or MMU is enabled
  114. */
  115. void soc_early_init(void)
  116. {
  117. uint8_t num_clusters, cores_per_cluster;
  118. dram_regions_info_t *dram_regions_info = get_dram_regions_info();
  119. #ifdef CONFIG_OCRAM_ECC_EN
  120. ocram_init(NXP_OCRAM_ADDR, NXP_OCRAM_SIZE);
  121. #endif
  122. dcfg_init(&dcfg_init_data);
  123. #ifdef POLICY_FUSE_PROVISION
  124. gpio_init(&gpio_init_data);
  125. sec_init(NXP_CAAM_ADDR);
  126. #endif
  127. #if LOG_LEVEL > 0
  128. /* Initialize the console to provide early debug support */
  129. plat_console_init(NXP_CONSOLE_ADDR,
  130. NXP_UART_CLK_DIVIDER, NXP_CONSOLE_BAUDRATE);
  131. #endif
  132. set_base_freq_CNTFID0();
  133. /* Enable snooping on SEC read and write transactions */
  134. scfg_setbits32((void *)(NXP_SCFG_ADDR + SCFG_SNPCNFGCR_OFFSET),
  135. SCFG_SNPCNFGCR_SECRDSNP | SCFG_SNPCNFGCR_SECWRSNP);
  136. /*
  137. * Initialize Interconnect for this cluster during cold boot.
  138. * No need for locks as no other CPU is active.
  139. */
  140. cci_init(NXP_CCI_ADDR, cci_map, ARRAY_SIZE(cci_map));
  141. /*
  142. * Enable Interconnect coherency for the primary CPU's cluster.
  143. */
  144. get_cluster_info(soc_list, ARRAY_SIZE(soc_list), &num_clusters, &cores_per_cluster);
  145. plat_ls_interconnect_enter_coherency(num_clusters);
  146. /*
  147. * Unlock write access for SMMU SMMU_CBn_ACTLR in all Non-secure contexts.
  148. */
  149. smmu_cache_unlock(NXP_SMMU_ADDR);
  150. INFO("SMMU Cache Unlocking is Configured.\n");
  151. #if TRUSTED_BOARD_BOOT
  152. uint32_t mode;
  153. sfp_init(NXP_SFP_ADDR);
  154. /*
  155. * For secure boot disable SMMU.
  156. * Later when platform security policy comes in picture,
  157. * this might get modified based on the policy
  158. */
  159. if (check_boot_mode_secure(&mode) == true) {
  160. bypass_smmu(NXP_SMMU_ADDR);
  161. }
  162. /*
  163. * For Mbedtls currently crypto is not supported via CAAM
  164. * enable it when that support is there. In tbbr.mk
  165. * the CAAM_INTEG is set as 0.
  166. */
  167. #ifndef MBEDTLS_X509
  168. /* Initialize the crypto accelerator if enabled */
  169. if (is_sec_enabled() == false) {
  170. INFO("SEC is disabled.\n");
  171. } else {
  172. sec_init(NXP_CAAM_ADDR);
  173. }
  174. #endif
  175. #elif defined(POLICY_FUSE_PROVISION)
  176. gpio_init(&gpio_init_data);
  177. sfp_init(NXP_SFP_ADDR);
  178. sec_init(NXP_CAAM_ADDR);
  179. #endif
  180. soc_errata();
  181. /* Initialize system level generic timer for Layerscape Socs. */
  182. delay_timer_init(NXP_TIMER_ADDR);
  183. #ifdef DDR_INIT
  184. i2c_init(NXP_I2C_ADDR);
  185. dram_regions_info->total_dram_size = init_ddr();
  186. #endif
  187. }
  188. void soc_bl2_prepare_exit(void)
  189. {
  190. #if defined(NXP_SFP_ENABLED) && defined(DISABLE_FUSE_WRITE)
  191. set_sfp_wr_disable();
  192. #endif
  193. }
  194. /* This function returns the boot device based on RCW_SRC */
  195. enum boot_device get_boot_dev(void)
  196. {
  197. enum boot_device src = BOOT_DEVICE_NONE;
  198. uint32_t porsr1;
  199. uint32_t rcw_src, val;
  200. porsr1 = read_reg_porsr1();
  201. rcw_src = (porsr1 & PORSR1_RCW_MASK) >> PORSR1_RCW_SHIFT;
  202. val = rcw_src & RCW_SRC_NAND_MASK;
  203. if (val == RCW_SRC_NAND_VAL) {
  204. val = rcw_src & NAND_RESERVED_MASK;
  205. if ((val != NAND_RESERVED_1) && (val != NAND_RESERVED_2)) {
  206. src = BOOT_DEVICE_IFC_NAND;
  207. INFO("RCW BOOT SRC is IFC NAND\n");
  208. }
  209. } else {
  210. /* RCW SRC NOR */
  211. val = rcw_src & RCW_SRC_NOR_MASK;
  212. if (val == NOR_8B_VAL || val == NOR_16B_VAL) {
  213. src = BOOT_DEVICE_IFC_NOR;
  214. INFO("RCW BOOT SRC is IFC NOR\n");
  215. } else {
  216. switch (rcw_src) {
  217. case QSPI_VAL1:
  218. case QSPI_VAL2:
  219. src = BOOT_DEVICE_QSPI;
  220. INFO("RCW BOOT SRC is QSPI\n");
  221. break;
  222. case SD_VAL:
  223. src = BOOT_DEVICE_EMMC;
  224. INFO("RCW BOOT SRC is SD/EMMC\n");
  225. break;
  226. default:
  227. src = BOOT_DEVICE_NONE;
  228. }
  229. }
  230. }
  231. return src;
  232. }
  233. /* This function sets up access permissions on memory regions */
  234. void soc_mem_access(void)
  235. {
  236. dram_regions_info_t *info_dram_regions = get_dram_regions_info();
  237. struct tzc400_reg tzc400_reg_list[MAX_NUM_TZC_REGION];
  238. unsigned int dram_idx, index = 0U;
  239. for (dram_idx = 0U; dram_idx < info_dram_regions->num_dram_regions;
  240. dram_idx++) {
  241. if (info_dram_regions->region[dram_idx].size == 0) {
  242. ERROR("DDR init failure, or");
  243. ERROR("DRAM regions not populated correctly.\n");
  244. break;
  245. }
  246. index = populate_tzc400_reg_list(tzc400_reg_list,
  247. dram_idx, index,
  248. info_dram_regions->region[dram_idx].addr,
  249. info_dram_regions->region[dram_idx].size,
  250. NXP_SECURE_DRAM_SIZE, NXP_SP_SHRD_DRAM_SIZE);
  251. }
  252. mem_access_setup(NXP_TZC_ADDR, index, tzc400_reg_list);
  253. }
  254. #else /* IMAGE_BL2 */
  255. /* Functions for BL31 */
  256. const unsigned char _power_domain_tree_desc[] = {1, 1, 4};
  257. CASSERT(NUMBER_OF_CLUSTERS && NUMBER_OF_CLUSTERS <= 256,
  258. assert_invalid_ls1046_cluster_count);
  259. /* This function returns the SoC topology */
  260. const unsigned char *plat_get_power_domain_tree_desc(void)
  261. {
  262. return _power_domain_tree_desc;
  263. }
  264. /*
  265. * This function returns the core count within the cluster corresponding to
  266. * `mpidr`.
  267. */
  268. unsigned int plat_ls_get_cluster_core_count(u_register_t mpidr)
  269. {
  270. return CORES_PER_CLUSTER;
  271. }
  272. void soc_early_platform_setup2(void)
  273. {
  274. dcfg_init(&dcfg_init_data);
  275. /* Initialize system level generic timer for SoCs */
  276. delay_timer_init(NXP_TIMER_ADDR);
  277. #if LOG_LEVEL > 0
  278. /* Initialize the console to provide early debug support */
  279. plat_console_init(NXP_CONSOLE_ADDR,
  280. NXP_UART_CLK_DIVIDER, NXP_CONSOLE_BAUDRATE);
  281. #endif
  282. }
  283. void soc_platform_setup(void)
  284. {
  285. static uint32_t target_mask_array[PLATFORM_CORE_COUNT];
  286. /*
  287. * On a GICv2 system, the Group 1 secure interrupts are treated
  288. * as Group 0 interrupts.
  289. */
  290. static interrupt_prop_t ls_interrupt_props[] = {
  291. PLAT_LS_G1S_IRQ_PROPS(GICV2_INTR_GROUP0),
  292. PLAT_LS_G0_IRQ_PROPS(GICV2_INTR_GROUP0)
  293. };
  294. plat_ls_gic_driver_init(
  295. #if (TEST_BL31)
  296. /* Defect in simulator - GIC base addresses (4Kb aligned) */
  297. NXP_GICD_4K_ADDR,
  298. NXP_GICC_4K_ADDR,
  299. #else
  300. NXP_GICD_64K_ADDR,
  301. NXP_GICC_64K_ADDR,
  302. #endif
  303. PLATFORM_CORE_COUNT,
  304. ls_interrupt_props,
  305. ARRAY_SIZE(ls_interrupt_props),
  306. target_mask_array);
  307. plat_ls_gic_init();
  308. enable_init_timer();
  309. }
  310. /* This function initializes the soc from the BL31 module */
  311. void soc_init(void)
  312. {
  313. /* low-level init of the soc */
  314. soc_init_lowlevel();
  315. _init_global_data();
  316. soc_init_percpu();
  317. _initialize_psci();
  318. /*
  319. * Initialize the interconnect during cold boot.
  320. * No need for locks as no other CPU is active.
  321. */
  322. cci_init(NXP_CCI_ADDR, cci_map, ARRAY_SIZE(cci_map));
  323. /*
  324. * Enable coherency in interconnect for the primary CPU's cluster.
  325. * Earlier bootloader stages might already do this but we can't
  326. * assume so. No harm in executing this code twice.
  327. */
  328. cci_enable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(read_mpidr()));
  329. /* Init CSU to enable non-secure access to peripherals */
  330. enable_layerscape_ns_access(ns_dev, ARRAY_SIZE(ns_dev), NXP_CSU_ADDR);
  331. /* Initialize the crypto accelerator if enabled */
  332. if (is_sec_enabled() == false) {
  333. INFO("SEC is disabled.\n");
  334. } else {
  335. sec_init(NXP_CAAM_ADDR);
  336. }
  337. }
  338. void soc_runtime_setup(void)
  339. {
  340. }
  341. #endif /* IMAGE_BL2 */