imx7_bl2_el3_common.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 2018-2019, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <platform_def.h>
  8. #include <arch_helpers.h>
  9. #include <common/bl_common.h>
  10. #include <common/debug.h>
  11. #include <common/desc_image_load.h>
  12. #include <drivers/mmc.h>
  13. #include <lib/xlat_tables/xlat_mmu_helpers.h>
  14. #include <lib/xlat_tables/xlat_tables_defs.h>
  15. #include <lib/mmio.h>
  16. #include <lib/optee_utils.h>
  17. #include <lib/utils.h>
  18. #include <imx_aips.h>
  19. #include <imx_caam.h>
  20. #include <imx_clock.h>
  21. #include <imx_csu.h>
  22. #include <imx_gpt.h>
  23. #include <imx_uart.h>
  24. #include <imx_snvs.h>
  25. #include <imx_wdog.h>
  26. #include <imx7_def.h>
  27. #ifndef AARCH32_SP_OPTEE
  28. #error "Must build with OPTEE support included"
  29. #endif
  30. uintptr_t plat_get_ns_image_entrypoint(void)
  31. {
  32. return IMX7_UBOOT_BASE;
  33. }
  34. static uint32_t imx7_get_spsr_for_bl32_entry(void)
  35. {
  36. return SPSR_MODE32(MODE32_svc, SPSR_T_ARM, SPSR_E_LITTLE,
  37. DISABLE_ALL_EXCEPTIONS);
  38. }
  39. static uint32_t imx7_get_spsr_for_bl33_entry(void)
  40. {
  41. return SPSR_MODE32(MODE32_svc,
  42. plat_get_ns_image_entrypoint() & 0x1,
  43. SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS);
  44. }
  45. int bl2_plat_handle_post_image_load(unsigned int image_id)
  46. {
  47. int err = 0;
  48. bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
  49. bl_mem_params_node_t *hw_cfg_mem_params = NULL;
  50. bl_mem_params_node_t *pager_mem_params = NULL;
  51. bl_mem_params_node_t *paged_mem_params = NULL;
  52. assert(bl_mem_params);
  53. switch (image_id) {
  54. case BL32_IMAGE_ID:
  55. pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
  56. assert(pager_mem_params);
  57. paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID);
  58. assert(paged_mem_params);
  59. err = parse_optee_header(&bl_mem_params->ep_info,
  60. &pager_mem_params->image_info,
  61. &paged_mem_params->image_info);
  62. if (err != 0)
  63. WARN("OPTEE header parse error.\n");
  64. /*
  65. * When ATF loads the DTB the address of the DTB is passed in
  66. * arg2, if an hw config image is present use the base address
  67. * as DTB address an pass it as arg2
  68. */
  69. hw_cfg_mem_params = get_bl_mem_params_node(HW_CONFIG_ID);
  70. bl_mem_params->ep_info.args.arg0 =
  71. bl_mem_params->ep_info.args.arg1;
  72. bl_mem_params->ep_info.args.arg1 = 0;
  73. if (hw_cfg_mem_params)
  74. bl_mem_params->ep_info.args.arg2 =
  75. hw_cfg_mem_params->image_info.image_base;
  76. else
  77. bl_mem_params->ep_info.args.arg2 = 0;
  78. bl_mem_params->ep_info.args.arg3 = 0;
  79. bl_mem_params->ep_info.spsr = imx7_get_spsr_for_bl32_entry();
  80. break;
  81. case BL33_IMAGE_ID:
  82. /* AArch32 only core: OP-TEE expects NSec EP in register LR */
  83. pager_mem_params = get_bl_mem_params_node(BL32_IMAGE_ID);
  84. assert(pager_mem_params);
  85. pager_mem_params->ep_info.lr_svc = bl_mem_params->ep_info.pc;
  86. /* BL33 expects to receive the primary CPU MPID (through r0) */
  87. bl_mem_params->ep_info.args.arg0 = 0xffff & read_mpidr();
  88. bl_mem_params->ep_info.spsr = imx7_get_spsr_for_bl33_entry();
  89. break;
  90. default:
  91. /* Do nothing in default case */
  92. break;
  93. }
  94. return err;
  95. }
  96. void bl2_el3_plat_arch_setup(void)
  97. {
  98. /* Setup the MMU here */
  99. }
  100. static void imx7_setup_system_counter(void)
  101. {
  102. unsigned long freq = SYS_COUNTER_FREQ_IN_TICKS;
  103. /* Set the frequency table index to our target frequency */
  104. write_cntfrq(freq);
  105. /* Enable system counter @ frequency table index 0, halt on debug */
  106. mmio_write_32(SYS_CNTCTL_BASE + CNTCR_OFF,
  107. CNTCR_FCREQ(0) | CNTCR_HDBG | CNTCR_EN);
  108. }
  109. static void imx7_setup_wdog_clocks(void)
  110. {
  111. uint32_t wdog_en_bits = (uint32_t)WDOG_DEFAULT_CLK_SELECT;
  112. imx_clock_set_wdog_clk_root_bits(wdog_en_bits);
  113. imx_clock_enable_wdog(0);
  114. imx_clock_enable_wdog(1);
  115. imx_clock_enable_wdog(2);
  116. imx_clock_enable_wdog(3);
  117. }
  118. /*
  119. * bl2_el3_early_platform_setup()
  120. * MMU off
  121. */
  122. void bl2_el3_early_platform_setup(u_register_t arg1, u_register_t arg2,
  123. u_register_t arg3, u_register_t arg4)
  124. {
  125. static console_t console;
  126. int console_scope = CONSOLE_FLAG_BOOT | CONSOLE_FLAG_RUNTIME;
  127. /* Initialize common components */
  128. imx_aips_init();
  129. imx_csu_init();
  130. imx_snvs_init();
  131. imx_gpt_ops_init(GPT1_BASE_ADDR);
  132. imx_clock_init();
  133. imx7_setup_system_counter();
  134. imx7_setup_wdog_clocks();
  135. /* Platform specific setup */
  136. imx7_platform_setup(arg1, arg2, arg3, arg4);
  137. /* Init UART, clock should be enabled in imx7_platform_setup() */
  138. console_imx_uart_register(PLAT_IMX7_BOOT_UART_BASE,
  139. PLAT_IMX7_BOOT_UART_CLK_IN_HZ,
  140. PLAT_IMX7_CONSOLE_BAUDRATE,
  141. &console);
  142. console_set_scope(&console, console_scope);
  143. /* Open handles to persistent storage */
  144. plat_imx_io_setup();
  145. /* Setup higher-level functionality CAAM, RTC etc */
  146. imx_caam_init();
  147. imx_wdog_init();
  148. /* Print out the expected memory map */
  149. VERBOSE("\tOPTEE 0x%08x-0x%08x\n", IMX7_OPTEE_BASE, IMX7_OPTEE_LIMIT);
  150. VERBOSE("\tATF/BL2 0x%08x-0x%08x\n", BL2_RAM_BASE, BL2_RAM_LIMIT);
  151. VERBOSE("\tSHRAM 0x%08x-0x%08x\n", SHARED_RAM_BASE, SHARED_RAM_LIMIT);
  152. VERBOSE("\tFIP 0x%08x-0x%08x\n", IMX_FIP_BASE, IMX_FIP_LIMIT);
  153. VERBOSE("\tDTB-OVERLAY 0x%08x-0x%08x\n", IMX7_DTB_OVERLAY_BASE, IMX7_DTB_OVERLAY_LIMIT);
  154. VERBOSE("\tDTB 0x%08x-0x%08x\n", IMX7_DTB_BASE, IMX7_DTB_LIMIT);
  155. VERBOSE("\tUBOOT/BL33 0x%08x-0x%08x\n", IMX7_UBOOT_BASE, IMX7_UBOOT_LIMIT);
  156. }
  157. /*
  158. * bl2_platform_setup()
  159. * MMU on - enabled by bl2_el3_plat_arch_setup()
  160. */
  161. void bl2_platform_setup(void)
  162. {
  163. }