soc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * Copyright 2018-2021 NXP
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <endian.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. #include <i2c.h>
  14. #include <lib/xlat_tables/xlat_tables_v2.h>
  15. #include <ls_interconnect.h>
  16. #include <mmio.h>
  17. #ifdef POLICY_FUSE_PROVISION
  18. #include <nxp_gpio.h>
  19. #endif
  20. #if TRUSTED_BOARD_BOOT
  21. #include <nxp_smmu.h>
  22. #endif
  23. #include <nxp_timer.h>
  24. #include <plat_console.h>
  25. #include <plat_gic.h>
  26. #include <plat_tzc400.h>
  27. #include <pmu.h>
  28. #include <scfg.h>
  29. #if defined(NXP_SFP_ENABLED)
  30. #include <sfp.h>
  31. #endif
  32. #include <errata.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. static struct soc_type soc_list[] = {
  46. SOC_ENTRY(LS1017AN, LS1017AN, 1, 1),
  47. SOC_ENTRY(LS1017AE, LS1017AE, 1, 1),
  48. SOC_ENTRY(LS1018AN, LS1018AN, 1, 1),
  49. SOC_ENTRY(LS1018AE, LS1018AE, 1, 1),
  50. SOC_ENTRY(LS1027AN, LS1027AN, 1, 2),
  51. SOC_ENTRY(LS1027AE, LS1027AE, 1, 2),
  52. SOC_ENTRY(LS1028AN, LS1028AN, 1, 2),
  53. SOC_ENTRY(LS1028AE, LS1028AE, 1, 2),
  54. };
  55. CASSERT(NUMBER_OF_CLUSTERS && NUMBER_OF_CLUSTERS <= 256,
  56. assert_invalid_ls1028a_cluster_count);
  57. /*
  58. * Function returns the base counter frequency
  59. * after reading the first entry at CNTFID0 (0x20 offset).
  60. *
  61. * Function is used by:
  62. * 1. ARM common code for PSCI management.
  63. * 2. ARM Generic Timer init.
  64. *
  65. */
  66. unsigned int plat_get_syscnt_freq2(void)
  67. {
  68. unsigned int counter_base_frequency;
  69. /*
  70. * Below register specifies the base frequency of the system counter.
  71. * As per NXP Board Manuals:
  72. * The system counter always works with SYS_REF_CLK/4 frequency clock.
  73. */
  74. counter_base_frequency = mmio_read_32(NXP_TIMER_ADDR + CNTFID_OFF);
  75. return counter_base_frequency;
  76. }
  77. #ifdef IMAGE_BL2
  78. #ifdef POLICY_FUSE_PROVISION
  79. static gpio_init_info_t gpio_init_data = {
  80. .gpio1_base_addr = NXP_GPIO1_ADDR,
  81. .gpio2_base_addr = NXP_GPIO2_ADDR,
  82. .gpio3_base_addr = NXP_GPIO3_ADDR,
  83. };
  84. #endif
  85. void soc_preload_setup(void)
  86. {
  87. }
  88. void soc_early_init(void)
  89. {
  90. uint8_t num_clusters, cores_per_cluster;
  91. #ifdef CONFIG_OCRAM_ECC_EN
  92. ocram_init(NXP_OCRAM_ADDR, NXP_OCRAM_SIZE);
  93. #endif
  94. dcfg_init(&dcfg_init_data);
  95. enable_timer_base_to_cluster(NXP_PMU_ADDR);
  96. enable_core_tb(NXP_PMU_ADDR);
  97. dram_regions_info_t *dram_regions_info = get_dram_regions_info();
  98. #ifdef POLICY_FUSE_PROVISION
  99. gpio_init(&gpio_init_data);
  100. sec_init(NXP_CAAM_ADDR);
  101. #endif
  102. #if LOG_LEVEL > 0
  103. /* Initialize the console to provide early debug support */
  104. plat_console_init(NXP_CONSOLE_ADDR,
  105. NXP_UART_CLK_DIVIDER, NXP_CONSOLE_BAUDRATE);
  106. #endif
  107. enum boot_device dev = get_boot_dev();
  108. /*
  109. * Mark the buffer for SD in OCRAM as non secure.
  110. * The buffer is assumed to be at end of OCRAM for
  111. * the logic below to calculate TZPC programming
  112. */
  113. if (dev == BOOT_DEVICE_EMMC || dev == BOOT_DEVICE_SDHC2_EMMC) {
  114. /*
  115. * Calculate the region in OCRAM which is secure
  116. * The buffer for SD needs to be marked non-secure
  117. * to allow SD to do DMA operations on it
  118. */
  119. uint32_t secure_region = (NXP_OCRAM_SIZE - NXP_SD_BLOCK_BUF_SIZE);
  120. uint32_t mask = secure_region/TZPC_BLOCK_SIZE;
  121. mmio_write_32(NXP_OCRAM_TZPC_ADDR, mask);
  122. /* Add the entry for buffer in MMU Table */
  123. mmap_add_region(NXP_SD_BLOCK_BUF_ADDR, NXP_SD_BLOCK_BUF_ADDR,
  124. NXP_SD_BLOCK_BUF_SIZE, MT_DEVICE | MT_RW | MT_NS);
  125. }
  126. #if TRUSTED_BOARD_BOOT
  127. uint32_t mode;
  128. sfp_init(NXP_SFP_ADDR);
  129. /*
  130. * For secure boot disable SMMU.
  131. * Later when platform security policy comes in picture,
  132. * this might get modified based on the policy
  133. */
  134. if (check_boot_mode_secure(&mode) == true) {
  135. bypass_smmu(NXP_SMMU_ADDR);
  136. }
  137. /*
  138. * For Mbedtls currently crypto is not supported via CAAM
  139. * enable it when that support is there. In tbbr.mk
  140. * the CAAM_INTEG is set as 0.
  141. */
  142. #ifndef MBEDTLS_X509
  143. /* Initialize the crypto accelerator if enabled */
  144. if (is_sec_enabled()) {
  145. sec_init(NXP_CAAM_ADDR);
  146. } else {
  147. INFO("SEC is disabled.\n");
  148. }
  149. #endif
  150. #endif
  151. /* Set eDDRTQ for DDR performance */
  152. scfg_setbits32((void *)(NXP_SCFG_ADDR + 0x210), 0x1f1f1f1f);
  153. soc_errata();
  154. /*
  155. * Initialize Interconnect for this cluster during cold boot.
  156. * No need for locks as no other CPU is active.
  157. */
  158. cci_init(NXP_CCI_ADDR, cci_map, ARRAY_SIZE(cci_map));
  159. /*
  160. * Enable Interconnect coherency for the primary CPU's cluster.
  161. */
  162. get_cluster_info(soc_list, ARRAY_SIZE(soc_list), &num_clusters, &cores_per_cluster);
  163. plat_ls_interconnect_enter_coherency(num_clusters);
  164. delay_timer_init(NXP_TIMER_ADDR);
  165. i2c_init(NXP_I2C_ADDR);
  166. dram_regions_info->total_dram_size = init_ddr();
  167. }
  168. void soc_bl2_prepare_exit(void)
  169. {
  170. #if defined(NXP_SFP_ENABLED) && defined(DISABLE_FUSE_WRITE)
  171. set_sfp_wr_disable();
  172. #endif
  173. }
  174. /*
  175. * This function returns the boot device based on RCW_SRC
  176. */
  177. enum boot_device get_boot_dev(void)
  178. {
  179. enum boot_device src = BOOT_DEVICE_NONE;
  180. uint32_t porsr1;
  181. uint32_t rcw_src;
  182. porsr1 = read_reg_porsr1();
  183. rcw_src = (porsr1 & PORSR1_RCW_MASK) >> PORSR1_RCW_SHIFT;
  184. switch (rcw_src) {
  185. case FLEXSPI_NOR:
  186. src = BOOT_DEVICE_FLEXSPI_NOR;
  187. INFO("RCW BOOT SRC is FLEXSPI NOR\n");
  188. break;
  189. case FLEXSPI_NAND2K_VAL:
  190. case FLEXSPI_NAND4K_VAL:
  191. INFO("RCW BOOT SRC is FLEXSPI NAND\n");
  192. src = BOOT_DEVICE_FLEXSPI_NAND;
  193. break;
  194. case SDHC1_VAL:
  195. src = BOOT_DEVICE_EMMC;
  196. INFO("RCW BOOT SRC is SD\n");
  197. break;
  198. case SDHC2_VAL:
  199. src = BOOT_DEVICE_SDHC2_EMMC;
  200. INFO("RCW BOOT SRC is EMMC\n");
  201. break;
  202. default:
  203. break;
  204. }
  205. return src;
  206. }
  207. /*
  208. * This function sets up access permissions on memory regions
  209. ****************************************************************************/
  210. void soc_mem_access(void)
  211. {
  212. dram_regions_info_t *info_dram_regions = get_dram_regions_info();
  213. struct tzc400_reg tzc400_reg_list[MAX_NUM_TZC_REGION];
  214. int dram_idx = 0;
  215. /* index 0 is reserved for region-0 */
  216. int index = 1;
  217. for (dram_idx = 0; dram_idx < info_dram_regions->num_dram_regions;
  218. dram_idx++) {
  219. if (info_dram_regions->region[dram_idx].size == 0) {
  220. ERROR("DDR init failure, or");
  221. ERROR("DRAM regions not populated correctly.\n");
  222. break;
  223. }
  224. index = populate_tzc400_reg_list(tzc400_reg_list,
  225. dram_idx, index,
  226. info_dram_regions->region[dram_idx].addr,
  227. info_dram_regions->region[dram_idx].size,
  228. NXP_SECURE_DRAM_SIZE, NXP_SP_SHRD_DRAM_SIZE);
  229. }
  230. mem_access_setup(NXP_TZC_ADDR, index, tzc400_reg_list);
  231. }
  232. #else
  233. static unsigned char _power_domain_tree_desc[NUMBER_OF_CLUSTERS + 2];
  234. /*
  235. * This function dynamically constructs the topology according to
  236. * SoC Flavor and returns it.
  237. */
  238. const unsigned char *plat_get_power_domain_tree_desc(void)
  239. {
  240. uint8_t num_clusters, cores_per_cluster;
  241. unsigned int i;
  242. get_cluster_info(soc_list, ARRAY_SIZE(soc_list), &num_clusters, &cores_per_cluster);
  243. /*
  244. * The highest level is the system level. The next level is constituted
  245. * by clusters and then cores in clusters.
  246. */
  247. _power_domain_tree_desc[0] = 1;
  248. _power_domain_tree_desc[1] = num_clusters;
  249. for (i = 0; i < _power_domain_tree_desc[1]; i++)
  250. _power_domain_tree_desc[i + 2] = cores_per_cluster;
  251. return _power_domain_tree_desc;
  252. }
  253. /*
  254. * This function returns the core count within the cluster corresponding to
  255. * `mpidr`.
  256. */
  257. unsigned int plat_ls_get_cluster_core_count(u_register_t mpidr)
  258. {
  259. uint8_t num_clusters, cores_per_cluster;
  260. get_cluster_info(soc_list, ARRAY_SIZE(soc_list), &num_clusters, &cores_per_cluster);
  261. return num_clusters;
  262. }
  263. void soc_early_platform_setup2(void)
  264. {
  265. dcfg_init(&dcfg_init_data);
  266. /* Initialize system level generic timer for Socs */
  267. delay_timer_init(NXP_TIMER_ADDR);
  268. #if LOG_LEVEL > 0
  269. /* Initialize the console to provide early debug support */
  270. plat_console_init(NXP_CONSOLE_ADDR,
  271. NXP_UART_CLK_DIVIDER, NXP_CONSOLE_BAUDRATE);
  272. #endif
  273. }
  274. void soc_platform_setup(void)
  275. {
  276. /* Initialize the GIC driver, cpu and distributor interfaces */
  277. static uintptr_t target_mask_array[PLATFORM_CORE_COUNT];
  278. static interrupt_prop_t ls_interrupt_props[] = {
  279. PLAT_LS_G1S_IRQ_PROPS(INTR_GROUP1S),
  280. PLAT_LS_G0_IRQ_PROPS(INTR_GROUP0)
  281. };
  282. plat_ls_gic_driver_init(NXP_GICD_ADDR, NXP_GICR_ADDR,
  283. PLATFORM_CORE_COUNT,
  284. ls_interrupt_props,
  285. ARRAY_SIZE(ls_interrupt_props),
  286. target_mask_array,
  287. plat_core_pos);
  288. plat_ls_gic_init();
  289. enable_init_timer();
  290. }
  291. /* This function initializes the soc from the BL31 module */
  292. void soc_init(void)
  293. {
  294. uint8_t num_clusters, cores_per_cluster;
  295. get_cluster_info(soc_list, ARRAY_SIZE(soc_list), &num_clusters, &cores_per_cluster);
  296. /* Low-level init of the soc */
  297. soc_init_lowlevel();
  298. _init_global_data();
  299. soc_init_percpu();
  300. _initialize_psci();
  301. /*
  302. * Initialize Interconnect for this cluster during cold boot.
  303. * No need for locks as no other CPU is active.
  304. */
  305. cci_init(NXP_CCI_ADDR, cci_map, ARRAY_SIZE(cci_map));
  306. /* Enable Interconnect coherency for the primary CPU's cluster. */
  307. plat_ls_interconnect_enter_coherency(num_clusters);
  308. /* Set platform security policies */
  309. _set_platform_security();
  310. /* Init SEC Engine which will be used by SiP */
  311. if (is_sec_enabled()) {
  312. sec_init(NXP_CAAM_ADDR);
  313. } else {
  314. INFO("SEC is disabled.\n");
  315. }
  316. }
  317. #ifdef NXP_WDOG_RESTART
  318. static uint64_t wdog_interrupt_handler(uint32_t id, uint32_t flags,
  319. void *handle, void *cookie)
  320. {
  321. uint8_t data = WDOG_RESET_FLAG;
  322. wr_nv_app_data(WDT_RESET_FLAG_OFFSET,
  323. (uint8_t *)&data, sizeof(data));
  324. mmio_write_32(NXP_RST_ADDR + RSTCNTL_OFFSET, SW_RST_REQ_INIT);
  325. return 0;
  326. }
  327. #endif
  328. void soc_runtime_setup(void)
  329. {
  330. #ifdef NXP_WDOG_RESTART
  331. request_intr_type_el3(BL31_NS_WDOG_WS1, wdog_interrupt_handler);
  332. #endif
  333. }
  334. /* This function returns the total number of cores in the SoC. */
  335. unsigned int get_tot_num_cores(void)
  336. {
  337. uint8_t num_clusters, cores_per_cluster;
  338. get_cluster_info(soc_list, ARRAY_SIZE(soc_list), &num_clusters, &cores_per_cluster);
  339. return (num_clusters * cores_per_cluster);
  340. }
  341. /* This function returns the PMU IDLE Cluster mask. */
  342. unsigned int get_pmu_idle_cluster_mask(void)
  343. {
  344. uint8_t num_clusters, cores_per_cluster;
  345. get_cluster_info(soc_list, ARRAY_SIZE(soc_list), &num_clusters, &cores_per_cluster);
  346. return ((1 << num_clusters) - 2);
  347. }
  348. /* This function returns the PMU Flush Cluster mask. */
  349. unsigned int get_pmu_flush_cluster_mask(void)
  350. {
  351. uint8_t num_clusters, cores_per_cluster;
  352. get_cluster_info(soc_list, ARRAY_SIZE(soc_list), &num_clusters, &cores_per_cluster);
  353. return ((1 << num_clusters) - 2);
  354. }
  355. /* This function returns the PMU idle core mask. */
  356. unsigned int get_pmu_idle_core_mask(void)
  357. {
  358. return ((1 << get_tot_num_cores()) - 2);
  359. }
  360. /* Function to return the SoC SYS CLK */
  361. unsigned int get_sys_clk(void)
  362. {
  363. return NXP_SYSCLK_FREQ;
  364. }
  365. #endif