hikey_bl2_setup.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Copyright (c) 2017-2021, 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> /* also includes hikey_def.h and hikey_layout.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/mmc.h>
  17. #include <drivers/synopsys/dw_mmc.h>
  18. #include <lib/mmio.h>
  19. #ifdef SPD_opteed
  20. #include <lib/optee_utils.h>
  21. #endif
  22. #include <plat/common/platform.h>
  23. #include <hi6220.h>
  24. #include <hisi_mcu.h>
  25. #include <hisi_sram_map.h>
  26. #include "hikey_private.h"
  27. #define BL2_RW_BASE (BL_CODE_END)
  28. static meminfo_t bl2_el3_tzram_layout;
  29. static console_t console;
  30. static struct mmc_device_info mmc_info;
  31. enum {
  32. BOOT_MODE_RECOVERY = 0,
  33. BOOT_MODE_NORMAL,
  34. BOOT_MODE_MASK = 1,
  35. };
  36. /*******************************************************************************
  37. * Transfer SCP_BL2 from Trusted RAM using the SCP Download protocol.
  38. * Return 0 on success, -1 otherwise.
  39. ******************************************************************************/
  40. int plat_hikey_bl2_handle_scp_bl2(image_info_t *scp_bl2_image_info)
  41. {
  42. /* Enable MCU SRAM */
  43. hisi_mcu_enable_sram();
  44. /* Load MCU binary into SRAM */
  45. hisi_mcu_load_image(scp_bl2_image_info->image_base,
  46. scp_bl2_image_info->image_size);
  47. /* Let MCU running */
  48. hisi_mcu_start_run();
  49. INFO("%s: MCU PC is at 0x%x\n",
  50. __func__, mmio_read_32(AO_SC_MCU_SUBSYS_STAT2));
  51. INFO("%s: AO_SC_PERIPH_CLKSTAT4 is 0x%x\n",
  52. __func__, mmio_read_32(AO_SC_PERIPH_CLKSTAT4));
  53. return 0;
  54. }
  55. /*******************************************************************************
  56. * Gets SPSR for BL32 entry
  57. ******************************************************************************/
  58. uint32_t hikey_get_spsr_for_bl32_entry(void)
  59. {
  60. /*
  61. * The Secure Payload Dispatcher service is responsible for
  62. * setting the SPSR prior to entry into the BL3-2 image.
  63. */
  64. return 0;
  65. }
  66. /*******************************************************************************
  67. * Gets SPSR for BL33 entry
  68. ******************************************************************************/
  69. #ifdef __aarch64__
  70. uint32_t hikey_get_spsr_for_bl33_entry(void)
  71. {
  72. unsigned int mode;
  73. uint32_t spsr;
  74. /* Figure out what mode we enter the non-secure world in */
  75. mode = (el_implemented(2) != EL_IMPL_NONE) ? MODE_EL2 : MODE_EL1;
  76. /*
  77. * TODO: Consider the possibility of specifying the SPSR in
  78. * the FIP ToC and allowing the platform to have a say as
  79. * well.
  80. */
  81. spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
  82. return spsr;
  83. }
  84. #else
  85. uint32_t hikey_get_spsr_for_bl33_entry(void)
  86. {
  87. unsigned int hyp_status, mode, spsr;
  88. hyp_status = GET_VIRT_EXT(read_id_pfr1());
  89. mode = (hyp_status) ? MODE32_hyp : MODE32_svc;
  90. /*
  91. * TODO: Consider the possibility of specifying the SPSR in
  92. * the FIP ToC and allowing the platform to have a say as
  93. * well.
  94. */
  95. spsr = SPSR_MODE32(mode, plat_get_ns_image_entrypoint() & 0x1,
  96. SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS);
  97. return spsr;
  98. }
  99. #endif /* __aarch64__ */
  100. int bl2_plat_handle_pre_image_load(unsigned int image_id)
  101. {
  102. return hikey_set_fip_addr(image_id, "fastboot");
  103. }
  104. int hikey_bl2_handle_post_image_load(unsigned int image_id)
  105. {
  106. int err = 0;
  107. bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
  108. #ifdef SPD_opteed
  109. bl_mem_params_node_t *pager_mem_params = NULL;
  110. bl_mem_params_node_t *paged_mem_params = NULL;
  111. #endif
  112. assert(bl_mem_params);
  113. switch (image_id) {
  114. #ifdef __aarch64__
  115. case BL32_IMAGE_ID:
  116. #ifdef SPD_opteed
  117. pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
  118. assert(pager_mem_params);
  119. paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID);
  120. assert(paged_mem_params);
  121. err = parse_optee_header(&bl_mem_params->ep_info,
  122. &pager_mem_params->image_info,
  123. &paged_mem_params->image_info);
  124. if (err != 0) {
  125. WARN("OPTEE header parse error.\n");
  126. }
  127. #endif
  128. bl_mem_params->ep_info.spsr = hikey_get_spsr_for_bl32_entry();
  129. break;
  130. #endif
  131. case BL33_IMAGE_ID:
  132. /* BL33 expects to receive the primary CPU MPID (through r0) */
  133. bl_mem_params->ep_info.args.arg0 = 0xffff & read_mpidr();
  134. bl_mem_params->ep_info.spsr = hikey_get_spsr_for_bl33_entry();
  135. break;
  136. #ifdef SCP_BL2_BASE
  137. case SCP_BL2_IMAGE_ID:
  138. /* The subsequent handling of SCP_BL2 is platform specific */
  139. err = plat_hikey_bl2_handle_scp_bl2(&bl_mem_params->image_info);
  140. if (err) {
  141. WARN("Failure in platform-specific handling of SCP_BL2 image.\n");
  142. }
  143. break;
  144. #endif
  145. default:
  146. /* Do nothing in default case */
  147. break;
  148. }
  149. return err;
  150. }
  151. /*******************************************************************************
  152. * This function can be used by the platforms to update/use image
  153. * information for given `image_id`.
  154. ******************************************************************************/
  155. int bl2_plat_handle_post_image_load(unsigned int image_id)
  156. {
  157. return hikey_bl2_handle_post_image_load(image_id);
  158. }
  159. static void reset_dwmmc_clk(void)
  160. {
  161. unsigned int data;
  162. /* disable mmc0 bus clock */
  163. mmio_write_32(PERI_SC_PERIPH_CLKDIS0, PERI_CLK0_MMC0);
  164. do {
  165. data = mmio_read_32(PERI_SC_PERIPH_CLKSTAT0);
  166. } while (data & PERI_CLK0_MMC0);
  167. /* enable mmc0 bus clock */
  168. mmio_write_32(PERI_SC_PERIPH_CLKEN0, PERI_CLK0_MMC0);
  169. do {
  170. data = mmio_read_32(PERI_SC_PERIPH_CLKSTAT0);
  171. } while (!(data & PERI_CLK0_MMC0));
  172. /* reset mmc0 clock domain */
  173. mmio_write_32(PERI_SC_PERIPH_RSTEN0, PERI_RST0_MMC0);
  174. /* bypass mmc0 clock phase */
  175. data = mmio_read_32(PERI_SC_PERIPH_CTRL2);
  176. data |= 3;
  177. mmio_write_32(PERI_SC_PERIPH_CTRL2, data);
  178. /* disable low power */
  179. data = mmio_read_32(PERI_SC_PERIPH_CTRL13);
  180. data |= 1 << 3;
  181. mmio_write_32(PERI_SC_PERIPH_CTRL13, data);
  182. do {
  183. data = mmio_read_32(PERI_SC_PERIPH_RSTSTAT0);
  184. } while (!(data & PERI_RST0_MMC0));
  185. /* unreset mmc0 clock domain */
  186. mmio_write_32(PERI_SC_PERIPH_RSTDIS0, PERI_RST0_MMC0);
  187. do {
  188. data = mmio_read_32(PERI_SC_PERIPH_RSTSTAT0);
  189. } while (data & PERI_RST0_MMC0);
  190. }
  191. static void hikey_boardid_init(void)
  192. {
  193. u_register_t midr;
  194. midr = read_midr();
  195. mmio_write_32(MEMORY_AXI_CHIP_ADDR, midr);
  196. INFO("[BDID] [%x] midr: 0x%x\n", MEMORY_AXI_CHIP_ADDR,
  197. (unsigned int)midr);
  198. mmio_write_32(MEMORY_AXI_BOARD_TYPE_ADDR, 0);
  199. mmio_write_32(MEMORY_AXI_BOARD_ID_ADDR, 0x2b);
  200. mmio_write_32(ACPU_ARM64_FLAGA, 0x1234);
  201. mmio_write_32(ACPU_ARM64_FLAGB, 0x5678);
  202. }
  203. static void hikey_sd_init(void)
  204. {
  205. /* switch pinmux to SD */
  206. mmio_write_32(IOMG_SD_CLK, IOMG_MUX_FUNC0);
  207. mmio_write_32(IOMG_SD_CMD, IOMG_MUX_FUNC0);
  208. mmio_write_32(IOMG_SD_DATA0, IOMG_MUX_FUNC0);
  209. mmio_write_32(IOMG_SD_DATA1, IOMG_MUX_FUNC0);
  210. mmio_write_32(IOMG_SD_DATA2, IOMG_MUX_FUNC0);
  211. mmio_write_32(IOMG_SD_DATA3, IOMG_MUX_FUNC0);
  212. mmio_write_32(IOCG_SD_CLK, IOCG_INPUT_16MA);
  213. mmio_write_32(IOCG_SD_CMD, IOCG_INPUT_12MA);
  214. mmio_write_32(IOCG_SD_DATA0, IOCG_INPUT_12MA);
  215. mmio_write_32(IOCG_SD_DATA1, IOCG_INPUT_12MA);
  216. mmio_write_32(IOCG_SD_DATA2, IOCG_INPUT_12MA);
  217. mmio_write_32(IOCG_SD_DATA3, IOCG_INPUT_12MA);
  218. /* set SD Card detect as nopull */
  219. mmio_write_32(IOCG_GPIO8, 0);
  220. }
  221. static void hikey_jumper_init(void)
  222. {
  223. /* set jumper detect as nopull */
  224. mmio_write_32(IOCG_GPIO24, 0);
  225. /* set jumper detect as GPIO */
  226. mmio_write_32(IOMG_GPIO24, IOMG_MUX_FUNC0);
  227. }
  228. void bl2_el3_early_platform_setup(u_register_t arg1, u_register_t arg2,
  229. u_register_t arg3, u_register_t arg4)
  230. {
  231. /* Initialize the console to provide early debug support */
  232. console_pl011_register(CONSOLE_BASE, PL011_UART_CLK_IN_HZ,
  233. PL011_BAUDRATE, &console);
  234. /*
  235. * Allow BL2 to see the whole Trusted RAM.
  236. */
  237. bl2_el3_tzram_layout.total_base = BL2_RW_BASE;
  238. bl2_el3_tzram_layout.total_size = BL31_LIMIT - BL2_RW_BASE;
  239. }
  240. void bl2_el3_plat_arch_setup(void)
  241. {
  242. hikey_init_mmu_el3(bl2_el3_tzram_layout.total_base,
  243. bl2_el3_tzram_layout.total_size,
  244. BL_CODE_BASE,
  245. BL_CODE_END,
  246. BL_COHERENT_RAM_BASE,
  247. BL_COHERENT_RAM_END);
  248. }
  249. void bl2_platform_setup(void)
  250. {
  251. dw_mmc_params_t params;
  252. hikey_sp804_init();
  253. hikey_gpio_init();
  254. hikey_pmussi_init();
  255. hikey_hi6553_init();
  256. /* Clear SRAM since it'll be used by MCU right now. */
  257. memset((void *)SRAM_BASE, 0, SRAM_SIZE);
  258. dsb();
  259. hikey_ddr_init(DDR_FREQ_800M);
  260. hikey_security_setup();
  261. hikey_boardid_init();
  262. init_acpu_dvfs();
  263. hikey_rtc_init();
  264. hikey_sd_init();
  265. hikey_jumper_init();
  266. hikey_mmc_pll_init();
  267. /* Clean SRAM before MCU used */
  268. clean_dcache_range(SRAM_BASE, SRAM_SIZE);
  269. reset_dwmmc_clk();
  270. memset(&params, 0, sizeof(dw_mmc_params_t));
  271. params.reg_base = DWMMC0_BASE;
  272. params.desc_base = HIKEY_MMC_DESC_BASE;
  273. params.desc_size = 1 << 20;
  274. params.clk_rate = 24 * 1000 * 1000;
  275. params.bus_width = MMC_BUS_WIDTH_8;
  276. params.flags = MMC_FLAG_CMD23;
  277. mmc_info.mmc_dev_type = MMC_IS_EMMC;
  278. dw_mmc_init(&params, &mmc_info);
  279. hikey_io_setup();
  280. }