imx8mq_bl31_setup.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright (c) 2018-2023, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <stdbool.h>
  8. #include <platform_def.h>
  9. #include <arch_helpers.h>
  10. #include <common/bl_common.h>
  11. #include <common/debug.h>
  12. #include <context.h>
  13. #include <drivers/arm/tzc380.h>
  14. #include <drivers/console.h>
  15. #include <drivers/generic_delay_timer.h>
  16. #include <lib/el3_runtime/context_mgmt.h>
  17. #include <lib/mmio.h>
  18. #include <lib/xlat_tables/xlat_tables_v2.h>
  19. #include <plat/common/platform.h>
  20. #include <dram.h>
  21. #include <gpc.h>
  22. #include <imx_aipstz.h>
  23. #include <imx_uart.h>
  24. #include <imx8m_caam.h>
  25. #include <imx8m_ccm.h>
  26. #include <plat_imx8.h>
  27. #define TRUSTY_PARAMS_LEN_BYTES (4096*2)
  28. /*
  29. * Avoid the pointer dereference of the canonical mmio_read_8() implementation.
  30. * This prevents the compiler from mis-interpreting the MMIO access as an
  31. * illegal memory access to a very low address (the IMX ROM is mapped at 0).
  32. */
  33. static uint8_t mmio_read_8_ldrb(uintptr_t address)
  34. {
  35. uint8_t reg;
  36. __asm__ volatile ("ldrb %w0, [%1]" : "=r" (reg) : "r" (address));
  37. return reg;
  38. }
  39. static const mmap_region_t imx_mmap[] = {
  40. MAP_REGION_FLAT(GPV_BASE, GPV_SIZE, MT_DEVICE | MT_RW), /* GPV map */
  41. MAP_REGION_FLAT(IMX_ROM_BASE, IMX_ROM_SIZE, MT_MEMORY | MT_RO), /* ROM map */
  42. MAP_REGION_FLAT(IMX_AIPS_BASE, IMX_AIPS_SIZE, MT_DEVICE | MT_RW), /* AIPS map */
  43. MAP_REGION_FLAT(IMX_GIC_BASE, IMX_GIC_SIZE, MT_DEVICE | MT_RW), /* GIC map */
  44. MAP_REGION_FLAT(IMX_DDRPHY_BASE, IMX_DDR_IPS_SIZE, MT_DEVICE | MT_RW), /* DDRMIX map */
  45. MAP_REGION_FLAT(IMX_DRAM_BASE, IMX_DRAM_SIZE, MT_MEMORY | MT_RW | MT_NS),
  46. {0},
  47. };
  48. static const struct aipstz_cfg aipstz[] = {
  49. {AIPSTZ1_BASE, 0x77777777, 0x77777777, .opacr = {0x0, 0x0, 0x0, 0x0, 0x0}, },
  50. {AIPSTZ2_BASE, 0x77777777, 0x77777777, .opacr = {0x0, 0x0, 0x0, 0x0, 0x0}, },
  51. {AIPSTZ3_BASE, 0x77777777, 0x77777777, .opacr = {0x0, 0x0, 0x0, 0x0, 0x0}, },
  52. {AIPSTZ4_BASE, 0x77777777, 0x77777777, .opacr = {0x0, 0x0, 0x0, 0x0, 0x0}, },
  53. {0},
  54. };
  55. static entry_point_info_t bl32_image_ep_info;
  56. static entry_point_info_t bl33_image_ep_info;
  57. static uint32_t imx_soc_revision;
  58. int imx_soc_info_handler(uint32_t smc_fid, u_register_t x1, u_register_t x2,
  59. u_register_t x3)
  60. {
  61. return imx_soc_revision;
  62. }
  63. #define ANAMIX_DIGPROG 0x6c
  64. #define ROM_SOC_INFO_A0 0x800
  65. #define ROM_SOC_INFO_B0 0x83C
  66. #define OCOTP_SOC_INFO_B1 0x40
  67. static void imx8mq_soc_info_init(void)
  68. {
  69. uint32_t rom_version;
  70. uint32_t ocotp_val;
  71. imx_soc_revision = mmio_read_32(IMX_ANAMIX_BASE + ANAMIX_DIGPROG);
  72. rom_version = mmio_read_8_ldrb(IMX_ROM_BASE + ROM_SOC_INFO_A0);
  73. if (rom_version == 0x10)
  74. return;
  75. rom_version = mmio_read_8_ldrb(IMX_ROM_BASE + ROM_SOC_INFO_B0);
  76. if (rom_version == 0x20) {
  77. imx_soc_revision &= ~0xff;
  78. imx_soc_revision |= rom_version;
  79. return;
  80. }
  81. /* 0xff0055aa is magic number for B1 */
  82. ocotp_val = mmio_read_32(IMX_OCOTP_BASE + OCOTP_SOC_INFO_B1);
  83. if (ocotp_val == 0xff0055aa) {
  84. imx_soc_revision &= ~0xff;
  85. if (rom_version == 0x22) {
  86. imx_soc_revision |= 0x22;
  87. } else {
  88. imx_soc_revision |= 0x21;
  89. }
  90. return;
  91. }
  92. }
  93. /* get SPSR for BL33 entry */
  94. static uint32_t get_spsr_for_bl33_entry(void)
  95. {
  96. unsigned long el_status;
  97. unsigned long mode;
  98. uint32_t spsr;
  99. /* figure out what mode we enter the non-secure world */
  100. el_status = read_id_aa64pfr0_el1() >> ID_AA64PFR0_EL2_SHIFT;
  101. el_status &= ID_AA64PFR0_ELX_MASK;
  102. mode = (el_status) ? MODE_EL2 : MODE_EL1;
  103. spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
  104. return spsr;
  105. }
  106. static void bl31_tz380_setup(void)
  107. {
  108. unsigned int val;
  109. val = mmio_read_32(IMX_IOMUX_GPR_BASE + IOMUXC_GPR10);
  110. if ((val & GPR_TZASC_EN) != GPR_TZASC_EN)
  111. return;
  112. tzc380_init(IMX_TZASC_BASE);
  113. /*
  114. * Need to substact offset 0x40000000 from CPU address when
  115. * programming tzasc region for i.mx8mq. Enable 1G-5G S/NS RW
  116. */
  117. tzc380_configure_region(0, 0x00000000, TZC_ATTR_REGION_SIZE(TZC_REGION_SIZE_4G) |
  118. TZC_ATTR_REGION_EN_MASK | TZC_ATTR_SP_ALL);
  119. }
  120. void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
  121. u_register_t arg2, u_register_t arg3)
  122. {
  123. unsigned int console_base = IMX_BOOT_UART_BASE;
  124. static console_t console;
  125. int i;
  126. /* enable CSU NS access permission */
  127. for (i = 0; i < 64; i++) {
  128. mmio_write_32(IMX_CSU_BASE + i * 4, 0xffffffff);
  129. }
  130. imx_aipstz_init(aipstz);
  131. if (console_base == 0U) {
  132. console_base = imx8m_uart_get_base();
  133. }
  134. console_imx_uart_register(console_base, IMX_BOOT_UART_CLK_IN_HZ,
  135. IMX_CONSOLE_BAUDRATE, &console);
  136. /* This console is only used for boot stage */
  137. console_set_scope(&console, CONSOLE_FLAG_BOOT);
  138. imx8m_caam_init();
  139. /*
  140. * tell BL3-1 where the non-secure software image is located
  141. * and the entry state information.
  142. */
  143. bl33_image_ep_info.pc = PLAT_NS_IMAGE_OFFSET;
  144. bl33_image_ep_info.spsr = get_spsr_for_bl33_entry();
  145. SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
  146. #if defined(SPD_opteed) || defined(SPD_trusty)
  147. /* Populate entry point information for BL32 */
  148. SET_PARAM_HEAD(&bl32_image_ep_info, PARAM_EP, VERSION_1, 0);
  149. SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE);
  150. bl32_image_ep_info.pc = BL32_BASE;
  151. bl32_image_ep_info.spsr = 0;
  152. /* Pass TEE base and size to bl33 */
  153. bl33_image_ep_info.args.arg1 = BL32_BASE;
  154. bl33_image_ep_info.args.arg2 = BL32_SIZE;
  155. #ifdef SPD_trusty
  156. bl32_image_ep_info.args.arg0 = BL32_SIZE;
  157. bl32_image_ep_info.args.arg1 = BL32_BASE;
  158. #else
  159. /* Make sure memory is clean */
  160. mmio_write_32(BL32_FDT_OVERLAY_ADDR, 0);
  161. bl33_image_ep_info.args.arg3 = BL32_FDT_OVERLAY_ADDR;
  162. bl32_image_ep_info.args.arg3 = BL32_FDT_OVERLAY_ADDR;
  163. #endif
  164. #endif
  165. bl31_tz380_setup();
  166. }
  167. void bl31_plat_arch_setup(void)
  168. {
  169. const mmap_region_t bl_regions[] = {
  170. MAP_REGION_FLAT(BL31_START, BL31_SIZE,
  171. MT_MEMORY | MT_RW | MT_SECURE),
  172. MAP_REGION_FLAT(BL_CODE_BASE, BL_CODE_END - BL_CODE_BASE,
  173. MT_MEMORY | MT_RO | MT_SECURE),
  174. #if USE_COHERENT_MEM
  175. MAP_REGION_FLAT(BL_COHERENT_RAM_BASE,
  176. BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE,
  177. MT_DEVICE | MT_RW | MT_SECURE),
  178. #endif
  179. /* Map TEE memory */
  180. MAP_REGION_FLAT(BL32_BASE, BL32_SIZE, MT_MEMORY | MT_RW),
  181. {0},
  182. };
  183. setup_page_tables(bl_regions, imx_mmap);
  184. /* enable the MMU */
  185. enable_mmu_el3(0);
  186. }
  187. void bl31_platform_setup(void)
  188. {
  189. generic_delay_timer_init();
  190. /* init the GICv3 cpu and distributor interface */
  191. plat_gic_driver_init();
  192. plat_gic_init();
  193. /* determine SOC revision for erratas */
  194. imx8mq_soc_info_init();
  195. /* gpc init */
  196. imx_gpc_init();
  197. dram_info_init(SAVED_DRAM_TIMING_BASE);
  198. }
  199. entry_point_info_t *bl31_plat_get_next_image_ep_info(unsigned int type)
  200. {
  201. if (type == NON_SECURE)
  202. return &bl33_image_ep_info;
  203. if (type == SECURE)
  204. return &bl32_image_ep_info;
  205. return NULL;
  206. }
  207. unsigned int plat_get_syscnt_freq2(void)
  208. {
  209. return COUNTER_FREQUENCY;
  210. }
  211. #ifdef SPD_trusty
  212. void plat_trusty_set_boot_args(aapcs64_params_t *args)
  213. {
  214. args->arg0 = BL32_SIZE;
  215. args->arg1 = BL32_BASE;
  216. args->arg2 = TRUSTY_PARAMS_LEN_BYTES;
  217. }
  218. #endif