hikey960_bl2_setup.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Copyright (c) 2017-2022, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #include <platform_def.h>
  10. #include <arch_helpers.h>
  11. #include <common/bl_common.h>
  12. #include <common/debug.h>
  13. #include <common/desc_image_load.h>
  14. #include <drivers/arm/pl011.h>
  15. #include <drivers/delay_timer.h>
  16. #include <drivers/dw_ufs.h>
  17. #include <drivers/generic_delay_timer.h>
  18. #include <drivers/partition/partition.h>
  19. #include <drivers/ufs.h>
  20. #include <lib/mmio.h>
  21. #ifdef SPD_opteed
  22. #include <lib/optee_utils.h>
  23. #endif
  24. #include <hi3660.h>
  25. #include "hikey960_def.h"
  26. #include "hikey960_private.h"
  27. #define BL2_RW_BASE (BL_CODE_END)
  28. /* BL2 platform parameters passed to BL31 */
  29. static plat_params_from_bl2_t plat_params_from_bl2;
  30. static meminfo_t bl2_el3_tzram_layout;
  31. static console_t console;
  32. extern int load_lpm3(void);
  33. enum {
  34. BOOT_MODE_RECOVERY = 0,
  35. BOOT_MODE_NORMAL,
  36. BOOT_MODE_MASK = 1,
  37. };
  38. /*******************************************************************************
  39. * Transfer SCP_BL2 from Trusted RAM using the SCP Download protocol.
  40. * Return 0 on success, -1 otherwise.
  41. ******************************************************************************/
  42. int plat_hikey960_bl2_handle_scp_bl2(image_info_t *scp_bl2_image_info)
  43. {
  44. int i;
  45. int *buf;
  46. assert(scp_bl2_image_info->image_size < SCP_BL2_SIZE);
  47. INFO("BL2: Initiating SCP_BL2 transfer to SCP\n");
  48. INFO("BL2: SCP_BL2: 0x%lx@0x%x\n",
  49. scp_bl2_image_info->image_base,
  50. scp_bl2_image_info->image_size);
  51. buf = (int *)scp_bl2_image_info->image_base;
  52. INFO("BL2: SCP_BL2 HEAD:\n");
  53. for (i = 0; i < 64; i += 4)
  54. INFO("BL2: SCP_BL2 0x%x 0x%x 0x%x 0x%x\n",
  55. buf[i], buf[i+1], buf[i+2], buf[i+3]);
  56. buf = (int *)(scp_bl2_image_info->image_base +
  57. scp_bl2_image_info->image_size - 256);
  58. INFO("BL2: SCP_BL2 TAIL:\n");
  59. for (i = 0; i < 64; i += 4)
  60. INFO("BL2: SCP_BL2 0x%x 0x%x 0x%x 0x%x\n",
  61. buf[i], buf[i+1], buf[i+2], buf[i+3]);
  62. INFO("BL2: SCP_BL2 transferred to SCP\n");
  63. load_lpm3();
  64. (void)buf;
  65. return 0;
  66. }
  67. static void hikey960_ufs_reset(void)
  68. {
  69. unsigned int data, mask;
  70. mmio_write_32(CRG_PERDIS7_REG, 1 << 14);
  71. mmio_clrbits_32(UFS_SYS_PHY_CLK_CTRL_REG, BIT_SYSCTRL_REF_CLOCK_EN);
  72. do {
  73. data = mmio_read_32(UFS_SYS_PHY_CLK_CTRL_REG);
  74. } while (data & BIT_SYSCTRL_REF_CLOCK_EN);
  75. /* use abb clk */
  76. mmio_clrbits_32(UFS_SYS_UFS_SYSCTRL_REG, BIT_UFS_REFCLK_SRC_SE1);
  77. mmio_clrbits_32(UFS_SYS_PHY_ISO_EN_REG, BIT_UFS_REFCLK_ISO_EN);
  78. mmio_write_32(PCTRL_PERI_CTRL3_REG, (1 << 0) | (1 << 16));
  79. mdelay(1);
  80. mmio_write_32(CRG_PEREN7_REG, 1 << 14);
  81. mmio_setbits_32(UFS_SYS_PHY_CLK_CTRL_REG, BIT_SYSCTRL_REF_CLOCK_EN);
  82. mmio_write_32(CRG_PERRSTEN3_REG, PERI_UFS_BIT);
  83. do {
  84. data = mmio_read_32(CRG_PERRSTSTAT3_REG);
  85. } while ((data & PERI_UFS_BIT) == 0);
  86. mmio_setbits_32(UFS_SYS_PSW_POWER_CTRL_REG, BIT_UFS_PSW_MTCMOS_EN);
  87. mdelay(1);
  88. mmio_setbits_32(UFS_SYS_HC_LP_CTRL_REG, BIT_SYSCTRL_PWR_READY);
  89. mmio_write_32(UFS_SYS_UFS_DEVICE_RESET_CTRL_REG,
  90. MASK_UFS_DEVICE_RESET);
  91. /* clear SC_DIV_UFS_PERIBUS */
  92. mask = SC_DIV_UFS_PERIBUS << 16;
  93. mmio_write_32(CRG_CLKDIV17_REG, mask);
  94. /* set SC_DIV_UFSPHY_CFG(3) */
  95. mask = SC_DIV_UFSPHY_CFG_MASK << 16;
  96. data = SC_DIV_UFSPHY_CFG(3);
  97. mmio_write_32(CRG_CLKDIV16_REG, mask | data);
  98. data = mmio_read_32(UFS_SYS_PHY_CLK_CTRL_REG);
  99. data &= ~MASK_SYSCTRL_CFG_CLOCK_FREQ;
  100. data |= 0x39;
  101. mmio_write_32(UFS_SYS_PHY_CLK_CTRL_REG, data);
  102. mmio_clrbits_32(UFS_SYS_PHY_CLK_CTRL_REG, MASK_SYSCTRL_REF_CLOCK_SEL);
  103. mmio_setbits_32(UFS_SYS_CLOCK_GATE_BYPASS_REG,
  104. MASK_UFS_CLK_GATE_BYPASS);
  105. mmio_setbits_32(UFS_SYS_UFS_SYSCTRL_REG, MASK_UFS_SYSCTRL_BYPASS);
  106. mmio_setbits_32(UFS_SYS_PSW_CLK_CTRL_REG, BIT_SYSCTRL_PSW_CLK_EN);
  107. mmio_clrbits_32(UFS_SYS_PSW_POWER_CTRL_REG, BIT_UFS_PSW_ISO_CTRL);
  108. mmio_clrbits_32(UFS_SYS_PHY_ISO_EN_REG, BIT_UFS_PHY_ISO_CTRL);
  109. mmio_clrbits_32(UFS_SYS_HC_LP_CTRL_REG, BIT_SYSCTRL_LP_ISOL_EN);
  110. mmio_write_32(CRG_PERRSTDIS3_REG, PERI_ARST_UFS_BIT);
  111. mmio_setbits_32(UFS_SYS_RESET_CTRL_EN_REG, BIT_SYSCTRL_LP_RESET_N);
  112. mdelay(1);
  113. mmio_write_32(UFS_SYS_UFS_DEVICE_RESET_CTRL_REG,
  114. MASK_UFS_DEVICE_RESET | BIT_UFS_DEVICE_RESET);
  115. mdelay(20);
  116. mmio_write_32(UFS_SYS_UFS_DEVICE_RESET_CTRL_REG,
  117. 0x03300330);
  118. mmio_write_32(CRG_PERRSTDIS3_REG, PERI_UFS_BIT);
  119. do {
  120. data = mmio_read_32(CRG_PERRSTSTAT3_REG);
  121. } while (data & PERI_UFS_BIT);
  122. }
  123. static void hikey960_init_ufs(void)
  124. {
  125. dw_ufs_params_t ufs_params;
  126. memset(&ufs_params, 0, sizeof(ufs_params_t));
  127. ufs_params.reg_base = UFS_REG_BASE;
  128. ufs_params.desc_base = HIKEY960_UFS_DESC_BASE;
  129. ufs_params.desc_size = HIKEY960_UFS_DESC_SIZE;
  130. hikey960_ufs_reset();
  131. dw_ufs_init(&ufs_params);
  132. }
  133. /*******************************************************************************
  134. * Gets SPSR for BL32 entry
  135. ******************************************************************************/
  136. uint32_t hikey960_get_spsr_for_bl32_entry(void)
  137. {
  138. /*
  139. * The Secure Payload Dispatcher service is responsible for
  140. * setting the SPSR prior to entry into the BL3-2 image.
  141. */
  142. return 0;
  143. }
  144. /*******************************************************************************
  145. * Gets SPSR for BL33 entry
  146. ******************************************************************************/
  147. #ifdef __aarch64__
  148. uint32_t hikey960_get_spsr_for_bl33_entry(void)
  149. {
  150. unsigned int mode;
  151. uint32_t spsr;
  152. /* Figure out what mode we enter the non-secure world in */
  153. mode = (el_implemented(2) != EL_IMPL_NONE) ? MODE_EL2 : MODE_EL1;
  154. /*
  155. * TODO: Consider the possibility of specifying the SPSR in
  156. * the FIP ToC and allowing the platform to have a say as
  157. * well.
  158. */
  159. spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
  160. return spsr;
  161. }
  162. #else
  163. uint32_t hikey960_get_spsr_for_bl33_entry(void)
  164. {
  165. unsigned int hyp_status, mode, spsr;
  166. hyp_status = GET_VIRT_EXT(read_id_pfr1());
  167. mode = (hyp_status) ? MODE32_hyp : MODE32_svc;
  168. /*
  169. * TODO: Consider the possibility of specifying the SPSR in
  170. * the FIP ToC and allowing the platform to have a say as
  171. * well.
  172. */
  173. spsr = SPSR_MODE32(mode, plat_get_ns_image_entrypoint() & 0x1,
  174. SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS);
  175. return spsr;
  176. }
  177. #endif /* __aarch64__ */
  178. int hikey960_bl2_handle_post_image_load(unsigned int image_id)
  179. {
  180. int err = 0;
  181. bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
  182. #ifdef SPD_opteed
  183. bl_mem_params_node_t *pager_mem_params = NULL;
  184. bl_mem_params_node_t *paged_mem_params = NULL;
  185. #endif
  186. assert(bl_mem_params);
  187. switch (image_id) {
  188. case BL31_IMAGE_ID:
  189. /* Pass BL2 platform parameter to BL31 */
  190. bl_mem_params->ep_info.args.arg1 = (uint64_t) &plat_params_from_bl2;
  191. break;
  192. #ifdef __aarch64__
  193. case BL32_IMAGE_ID:
  194. #ifdef SPD_opteed
  195. pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
  196. assert(pager_mem_params);
  197. paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID);
  198. assert(paged_mem_params);
  199. err = parse_optee_header(&bl_mem_params->ep_info,
  200. &pager_mem_params->image_info,
  201. &paged_mem_params->image_info);
  202. if (err != 0) {
  203. WARN("OPTEE header parse error.\n");
  204. }
  205. #endif
  206. bl_mem_params->ep_info.spsr = hikey960_get_spsr_for_bl32_entry();
  207. break;
  208. #endif
  209. case BL33_IMAGE_ID:
  210. /* BL33 expects to receive the primary CPU MPID (through r0) */
  211. bl_mem_params->ep_info.args.arg0 = 0xffff & read_mpidr();
  212. bl_mem_params->ep_info.spsr = hikey960_get_spsr_for_bl33_entry();
  213. break;
  214. #ifdef SCP_BL2_BASE
  215. case SCP_BL2_IMAGE_ID:
  216. /* The subsequent handling of SCP_BL2 is platform specific */
  217. err = plat_hikey960_bl2_handle_scp_bl2(&bl_mem_params->image_info);
  218. if (err) {
  219. WARN("Failure in platform-specific handling of SCP_BL2 image.\n");
  220. }
  221. break;
  222. #endif
  223. default:
  224. /* Do nothing in default case */
  225. break;
  226. }
  227. return err;
  228. }
  229. /*******************************************************************************
  230. * This function can be used by the platforms to update/use image
  231. * information for given `image_id`.
  232. ******************************************************************************/
  233. int bl2_plat_handle_pre_image_load(unsigned int image_id)
  234. {
  235. return hikey960_set_fip_addr(image_id, "fip");
  236. }
  237. int bl2_plat_handle_post_image_load(unsigned int image_id)
  238. {
  239. return hikey960_bl2_handle_post_image_load(image_id);
  240. }
  241. void bl2_el3_early_platform_setup(u_register_t arg1, u_register_t arg2,
  242. u_register_t arg3, u_register_t arg4)
  243. {
  244. unsigned int id, uart_base;
  245. generic_delay_timer_init();
  246. hikey960_read_boardid(&id);
  247. if (id == 5300)
  248. uart_base = PL011_UART5_BASE;
  249. else
  250. uart_base = PL011_UART6_BASE;
  251. /* Initialize the console to provide early debug support */
  252. console_pl011_register(uart_base, PL011_UART_CLK_IN_HZ,
  253. PL011_BAUDRATE, &console);
  254. /*
  255. * Allow BL2 to see the whole Trusted RAM.
  256. */
  257. bl2_el3_tzram_layout.total_base = BL2_RW_BASE;
  258. bl2_el3_tzram_layout.total_size = BL31_LIMIT - BL2_RW_BASE;
  259. }
  260. void bl2_el3_plat_arch_setup(void)
  261. {
  262. hikey960_init_mmu_el3(bl2_el3_tzram_layout.total_base,
  263. bl2_el3_tzram_layout.total_size,
  264. BL_CODE_BASE,
  265. BL_CODE_END,
  266. BL_COHERENT_RAM_BASE,
  267. BL_COHERENT_RAM_END);
  268. }
  269. void bl2_platform_setup(void)
  270. {
  271. int ret;
  272. /* disable WDT0 */
  273. if (mmio_read_32(WDT0_REG_BASE + WDT_LOCK_OFFSET) == WDT_LOCKED) {
  274. mmio_write_32(WDT0_REG_BASE + WDT_LOCK_OFFSET, WDT_UNLOCK);
  275. mmio_write_32(WDT0_REG_BASE + WDT_CONTROL_OFFSET, 0);
  276. mmio_write_32(WDT0_REG_BASE + WDT_LOCK_OFFSET, 0);
  277. }
  278. hikey960_clk_init();
  279. hikey960_pmu_init();
  280. hikey960_regulator_enable();
  281. hikey960_tzc_init();
  282. hikey960_peri_init();
  283. hikey960_pinmux_init();
  284. hikey960_gpio_init();
  285. hikey960_init_ufs();
  286. hikey960_io_setup();
  287. /* Read serial number from storage */
  288. plat_params_from_bl2.fastboot_serno = 0;
  289. ret = hikey960_load_serialno(&plat_params_from_bl2.fastboot_serno);
  290. if (ret != 0) {
  291. ERROR("BL2: could not read serial number\n");
  292. }
  293. INFO("BL2: fastboot_serno %lx\n", plat_params_from_bl2.fastboot_serno);
  294. flush_dcache_range((uintptr_t)&plat_params_from_bl2, sizeof(plat_params_from_bl2_t));
  295. }