soc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * Copyright 2018-2021 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_tzc380.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. 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. static struct soc_type soc_list[] = {
  67. SOC_ENTRY(LS1023A, LS1023A, 1, 2),
  68. SOC_ENTRY(LS1023AE, LS1023AE, 1, 2),
  69. SOC_ENTRY(LS1023A_P23, LS1023A_P23, 1, 2),
  70. SOC_ENTRY(LS1023AE_P23, LS1023AE_P23, 1, 2),
  71. SOC_ENTRY(LS1043A, LS1043A, 1, 4),
  72. SOC_ENTRY(LS1043AE, LS1043AE, 1, 4),
  73. SOC_ENTRY(LS1043A_P23, LS1043A_P23, 1, 4),
  74. SOC_ENTRY(LS1043AE_P23, LS1043AE_P23, 1, 4),
  75. };
  76. #ifdef POLICY_FUSE_PROVISION
  77. static gpio_init_info_t gpio_init_data = {
  78. .gpio1_base_addr = NXP_GPIO1_ADDR,
  79. .gpio2_base_addr = NXP_GPIO2_ADDR,
  80. .gpio3_base_addr = NXP_GPIO3_ADDR,
  81. .gpio4_base_addr = NXP_GPIO4_ADDR,
  82. };
  83. #endif
  84. /*
  85. * Function to set the base counter frequency at
  86. * the first entry of the Frequency Mode Table,
  87. * at CNTFID0 (0x20 offset).
  88. *
  89. * Set the value of the pirmary core register cntfrq_el0.
  90. */
  91. static void set_base_freq_CNTFID0(void)
  92. {
  93. /*
  94. * Below register specifies the base frequency of the system counter.
  95. * As per NXP Board Manuals:
  96. * The system counter always works with SYS_REF_CLK/4 frequency clock.
  97. *
  98. */
  99. unsigned int counter_base_frequency = get_sys_clk()/4;
  100. /*
  101. * Setting the frequency in the Frequency modes table.
  102. *
  103. * Note: The value for ls1046ardb board at this offset
  104. * is not RW as stated. This offset have the
  105. * fixed value of 100000400 Hz.
  106. *
  107. * The below code line has no effect.
  108. * Keeping it for other platforms where it has effect.
  109. */
  110. mmio_write_32(NXP_TIMER_ADDR + CNTFID_OFF, counter_base_frequency);
  111. write_cntfrq_el0(counter_base_frequency);
  112. }
  113. void soc_preload_setup(void)
  114. {
  115. }
  116. /*******************************************************************************
  117. * This function implements soc specific erratas
  118. * This is called before DDR is initialized or MMU is enabled
  119. ******************************************************************************/
  120. void soc_early_init(void)
  121. {
  122. uint8_t num_clusters, cores_per_cluster;
  123. dram_regions_info_t *dram_regions_info = get_dram_regions_info();
  124. #ifdef CONFIG_OCRAM_ECC_EN
  125. ocram_init(NXP_OCRAM_ADDR, NXP_OCRAM_SIZE);
  126. #endif
  127. dcfg_init(&dcfg_init_data);
  128. #ifdef POLICY_FUSE_PROVISION
  129. gpio_init(&gpio_init_data);
  130. sec_init(NXP_CAAM_ADDR);
  131. #endif
  132. #if LOG_LEVEL > 0
  133. /* Initialize the console to provide early debug support */
  134. plat_console_init(NXP_CONSOLE_ADDR,
  135. NXP_UART_CLK_DIVIDER, NXP_CONSOLE_BAUDRATE);
  136. #endif
  137. set_base_freq_CNTFID0();
  138. /* Enable snooping on SEC read and write transactions */
  139. scfg_setbits32((void *)(NXP_SCFG_ADDR + SCFG_SNPCNFGCR_OFFSET),
  140. SCFG_SNPCNFGCR_SECRDSNP | SCFG_SNPCNFGCR_SECWRSNP);
  141. /*
  142. * Initialize Interconnect for this cluster during cold boot.
  143. * No need for locks as no other CPU is active.
  144. */
  145. cci_init(NXP_CCI_ADDR, cci_map, ARRAY_SIZE(cci_map));
  146. /*
  147. * Enable Interconnect coherency for the primary CPU's cluster.
  148. */
  149. get_cluster_info(soc_list, ARRAY_SIZE(soc_list), &num_clusters, &cores_per_cluster);
  150. plat_ls_interconnect_enter_coherency(num_clusters);
  151. /*
  152. * Unlock write access for SMMU SMMU_CBn_ACTLR in all Non-secure contexts.
  153. */
  154. smmu_cache_unlock(NXP_SMMU_ADDR);
  155. INFO("SMMU Cache Unlocking is Configured.\n");
  156. #if TRUSTED_BOARD_BOOT
  157. uint32_t mode;
  158. sfp_init(NXP_SFP_ADDR);
  159. /*
  160. * For secure boot disable SMMU.
  161. * Later when platform security policy comes in picture,
  162. * this might get modified based on the policy
  163. */
  164. if (check_boot_mode_secure(&mode) == true) {
  165. bypass_smmu(NXP_SMMU_ADDR);
  166. }
  167. /*
  168. * For Mbedtls currently crypto is not supported via CAAM
  169. * enable it when that support is there. In tbbr.mk
  170. * the CAAM_INTEG is set as 0.
  171. */
  172. #ifndef MBEDTLS_X509
  173. /* Initialize the crypto accelerator if enabled */
  174. if (is_sec_enabled() == false) {
  175. INFO("SEC is disabled.\n");
  176. } else {
  177. sec_init(NXP_CAAM_ADDR);
  178. }
  179. #endif
  180. #elif defined(POLICY_FUSE_PROVISION)
  181. gpio_init(&gpio_init_data);
  182. sfp_init(NXP_SFP_ADDR);
  183. sec_init(NXP_CAAM_ADDR);
  184. #endif
  185. soc_errata();
  186. /*
  187. * Initialize system level generic timer for Layerscape Socs.
  188. */
  189. delay_timer_init(NXP_TIMER_ADDR);
  190. #ifdef DDR_INIT
  191. i2c_init(NXP_I2C_ADDR);
  192. dram_regions_info->total_dram_size = init_ddr();
  193. #endif
  194. }
  195. void soc_bl2_prepare_exit(void)
  196. {
  197. #if defined(NXP_SFP_ENABLED) && defined(DISABLE_FUSE_WRITE)
  198. set_sfp_wr_disable();
  199. #endif
  200. }
  201. /*****************************************************************************
  202. * This function returns the boot device based on RCW_SRC
  203. ****************************************************************************/
  204. enum boot_device get_boot_dev(void)
  205. {
  206. enum boot_device src = BOOT_DEVICE_NONE;
  207. uint32_t porsr1;
  208. uint32_t rcw_src, val;
  209. porsr1 = read_reg_porsr1();
  210. rcw_src = (porsr1 & PORSR1_RCW_MASK) >> PORSR1_RCW_SHIFT;
  211. val = rcw_src & RCW_SRC_NAND_MASK;
  212. if (val == RCW_SRC_NAND_VAL) {
  213. val = rcw_src & NAND_RESERVED_MASK;
  214. if ((val != NAND_RESERVED_1) && (val != NAND_RESERVED_2)) {
  215. src = BOOT_DEVICE_IFC_NAND;
  216. INFO("RCW BOOT SRC is IFC NAND\n");
  217. }
  218. } else {
  219. /* RCW SRC NOR */
  220. val = rcw_src & RCW_SRC_NOR_MASK;
  221. if (val == NOR_8B_VAL || val == NOR_16B_VAL) {
  222. src = BOOT_DEVICE_IFC_NOR;
  223. INFO("RCW BOOT SRC is IFC NOR\n");
  224. } else {
  225. switch (rcw_src) {
  226. case QSPI_VAL1:
  227. case QSPI_VAL2:
  228. src = BOOT_DEVICE_QSPI;
  229. INFO("RCW BOOT SRC is QSPI\n");
  230. break;
  231. case SD_VAL:
  232. src = BOOT_DEVICE_EMMC;
  233. INFO("RCW BOOT SRC is SD/EMMC\n");
  234. break;
  235. default:
  236. src = BOOT_DEVICE_NONE;
  237. }
  238. }
  239. }
  240. return src;
  241. }
  242. /* This function sets up access permissions on memory regions */
  243. void soc_mem_access(void)
  244. {
  245. struct tzc380_reg tzc380_reg_list[MAX_NUM_TZC_REGION];
  246. int dram_idx, index = 0U;
  247. dram_regions_info_t *info_dram_regions = get_dram_regions_info();
  248. for (dram_idx = 0U; dram_idx < info_dram_regions->num_dram_regions;
  249. dram_idx++) {
  250. if (info_dram_regions->region[dram_idx].size == 0) {
  251. ERROR("DDR init failure, or");
  252. ERROR("DRAM regions not populated correctly.\n");
  253. break;
  254. }
  255. index = populate_tzc380_reg_list(tzc380_reg_list,
  256. dram_idx, index,
  257. info_dram_regions->region[dram_idx].addr,
  258. info_dram_regions->region[dram_idx].size,
  259. NXP_SECURE_DRAM_SIZE, NXP_SP_SHRD_DRAM_SIZE);
  260. }
  261. mem_access_setup(NXP_TZC_ADDR, index, tzc380_reg_list);
  262. /* Configure CSU secure access register to disable TZASC bypass mux */
  263. mmio_write_32((uintptr_t)(NXP_CSU_ADDR +
  264. CSU_SEC_ACCESS_REG_OFFSET),
  265. bswap32(TZASC_BYPASS_MUX_DISABLE));
  266. }
  267. #else
  268. const unsigned char _power_domain_tree_desc[] = {1, 1, 4};
  269. CASSERT(NUMBER_OF_CLUSTERS && NUMBER_OF_CLUSTERS <= 256,
  270. assert_invalid_ls1043_cluster_count);
  271. /* This function returns the SoC topology */
  272. const unsigned char *plat_get_power_domain_tree_desc(void)
  273. {
  274. return _power_domain_tree_desc;
  275. }
  276. /*
  277. * This function returns the core count within the cluster corresponding to
  278. * `mpidr`.
  279. */
  280. unsigned int plat_ls_get_cluster_core_count(u_register_t mpidr)
  281. {
  282. return CORES_PER_CLUSTER;
  283. }
  284. void soc_early_platform_setup2(void)
  285. {
  286. dcfg_init(&dcfg_init_data);
  287. /* Initialize system level generic timer for Socs */
  288. delay_timer_init(NXP_TIMER_ADDR);
  289. #if LOG_LEVEL > 0
  290. /* Initialize the console to provide early debug support */
  291. plat_console_init(NXP_CONSOLE_ADDR,
  292. NXP_UART_CLK_DIVIDER, NXP_CONSOLE_BAUDRATE);
  293. #endif
  294. }
  295. /*
  296. * For LS1043a rev1.0, GIC base address align with 4k.
  297. * For LS1043a rev1.1, if DCFG_GIC400_ALIGN[GIC_ADDR_BIT]
  298. * is set, GIC base address align with 4K, or else align
  299. * with 64k.
  300. */
  301. void get_gic_offset(uint32_t *gicc_base, uint32_t *gicd_base)
  302. {
  303. uint32_t *ccsr_svr = (uint32_t *)(NXP_DCFG_ADDR + DCFG_SVR_OFFSET);
  304. uint32_t *gic_align = (uint32_t *)(NXP_SCFG_ADDR +
  305. SCFG_GIC400_ADDR_ALIGN_OFFSET);
  306. uint32_t val;
  307. val = be32toh(mmio_read_32((uintptr_t)ccsr_svr));
  308. if ((val & 0xff) == REV1_1) {
  309. val = be32toh(mmio_read_32((uintptr_t)gic_align));
  310. if (val & (1L << GIC_ADDR_BIT)) {
  311. *gicc_base = NXP_GICC_4K_ADDR;
  312. *gicd_base = NXP_GICD_4K_ADDR;
  313. } else {
  314. *gicc_base = NXP_GICC_64K_ADDR;
  315. *gicd_base = NXP_GICD_64K_ADDR;
  316. }
  317. } else {
  318. *gicc_base = NXP_GICC_4K_ADDR;
  319. *gicd_base = NXP_GICD_4K_ADDR;
  320. }
  321. }
  322. void soc_platform_setup(void)
  323. {
  324. /* Initialize the GIC driver, cpu and distributor interfaces */
  325. static uint32_t target_mask_array[PLATFORM_CORE_COUNT];
  326. /*
  327. * On a GICv2 system, the Group 1 secure interrupts are treated
  328. * as Group 0 interrupts.
  329. */
  330. static interrupt_prop_t ls_interrupt_props[] = {
  331. PLAT_LS_G1S_IRQ_PROPS(GICV2_INTR_GROUP0),
  332. PLAT_LS_G0_IRQ_PROPS(GICV2_INTR_GROUP0)
  333. };
  334. static uint32_t gicc_base, gicd_base;
  335. get_gic_offset(&gicc_base, &gicd_base);
  336. plat_ls_gic_driver_init(gicd_base, gicc_base,
  337. PLATFORM_CORE_COUNT,
  338. ls_interrupt_props,
  339. ARRAY_SIZE(ls_interrupt_props),
  340. target_mask_array);
  341. plat_ls_gic_init();
  342. enable_init_timer();
  343. }
  344. /* This function initializes the soc from the BL31 module */
  345. void soc_init(void)
  346. {
  347. /* low-level init of the soc */
  348. soc_init_lowlevel();
  349. _init_global_data();
  350. soc_init_percpu();
  351. _initialize_psci();
  352. /*
  353. * Initialize the interconnect during cold boot.
  354. * No need for locks as no other CPU is active.
  355. */
  356. cci_init(NXP_CCI_ADDR, cci_map, ARRAY_SIZE(cci_map));
  357. /*
  358. * Enable coherency in interconnect for the primary CPU's cluster.
  359. * Earlier bootloader stages might already do this but we can't
  360. * assume so. No harm in executing this code twice.
  361. */
  362. cci_enable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(read_mpidr()));
  363. /* Init CSU to enable non-secure access to peripherals */
  364. enable_layerscape_ns_access(ns_dev, ARRAY_SIZE(ns_dev), NXP_CSU_ADDR);
  365. /* Initialize the crypto accelerator if enabled */
  366. if (is_sec_enabled() == false) {
  367. INFO("SEC is disabled.\n");
  368. } else {
  369. sec_init(NXP_CAAM_ADDR);
  370. }
  371. }
  372. void soc_runtime_setup(void)
  373. {
  374. }
  375. #endif