hikey960_bl1_setup.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (c) 2017-2018, 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 <bl1/tbbr/tbbr_img_desc.h>
  12. #include <common/bl_common.h>
  13. #include <common/debug.h>
  14. #include <common/interrupt_props.h>
  15. #include <drivers/arm/gicv2.h>
  16. #include <drivers/arm/pl011.h>
  17. #include <drivers/delay_timer.h>
  18. #include <drivers/dw_ufs.h>
  19. #include <drivers/generic_delay_timer.h>
  20. #include <drivers/ufs.h>
  21. #include <lib/mmio.h>
  22. #include <plat/common/platform.h>
  23. #include <hi3660.h>
  24. #include "hikey960_def.h"
  25. #include "hikey960_private.h"
  26. enum {
  27. BOOT_MODE_RECOVERY = 0,
  28. BOOT_MODE_NORMAL,
  29. BOOT_MODE_MASK = 1,
  30. };
  31. /*
  32. * Declarations of linker defined symbols which will help us find the layout
  33. * of trusted RAM
  34. */
  35. /* Data structure which holds the extents of the trusted RAM for BL1 */
  36. static meminfo_t bl1_tzram_layout;
  37. static console_t console;
  38. /******************************************************************************
  39. * On a GICv2 system, the Group 1 secure interrupts are treated as Group 0
  40. * interrupts.
  41. *****************************************************************************/
  42. static const interrupt_prop_t g0_interrupt_props[] = {
  43. INTR_PROP_DESC(IRQ_SEC_PHY_TIMER, GIC_HIGHEST_SEC_PRIORITY,
  44. GICV2_INTR_GROUP0, GIC_INTR_CFG_LEVEL),
  45. INTR_PROP_DESC(IRQ_SEC_SGI_0, GIC_HIGHEST_SEC_PRIORITY,
  46. GICV2_INTR_GROUP0, GIC_INTR_CFG_LEVEL),
  47. };
  48. const gicv2_driver_data_t hikey960_gic_data = {
  49. .gicd_base = GICD_REG_BASE,
  50. .gicc_base = GICC_REG_BASE,
  51. .interrupt_props = g0_interrupt_props,
  52. .interrupt_props_num = ARRAY_SIZE(g0_interrupt_props),
  53. };
  54. meminfo_t *bl1_plat_sec_mem_layout(void)
  55. {
  56. return &bl1_tzram_layout;
  57. }
  58. /*
  59. * Perform any BL1 specific platform actions.
  60. */
  61. void bl1_early_platform_setup(void)
  62. {
  63. unsigned int id, uart_base;
  64. generic_delay_timer_init();
  65. hikey960_read_boardid(&id);
  66. if (id == 5300)
  67. uart_base = PL011_UART5_BASE;
  68. else
  69. uart_base = PL011_UART6_BASE;
  70. /* Initialize the console to provide early debug support */
  71. console_pl011_register(uart_base, PL011_UART_CLK_IN_HZ,
  72. PL011_BAUDRATE, &console);
  73. /* Allow BL1 to see the whole Trusted RAM */
  74. bl1_tzram_layout.total_base = BL1_RW_BASE;
  75. bl1_tzram_layout.total_size = BL1_RW_SIZE;
  76. INFO("BL1: 0x%lx - 0x%lx [size = %lu]\n", BL1_RAM_BASE, BL1_RAM_LIMIT,
  77. BL1_RAM_LIMIT - BL1_RAM_BASE); /* bl1_size */
  78. }
  79. /*
  80. * Perform the very early platform specific architecture setup here. At the
  81. * moment this only does basic initialization. Later architectural setup
  82. * (bl1_arch_setup()) does not do anything platform specific.
  83. */
  84. void bl1_plat_arch_setup(void)
  85. {
  86. hikey960_init_mmu_el3(bl1_tzram_layout.total_base,
  87. bl1_tzram_layout.total_size,
  88. BL1_RO_BASE,
  89. BL1_RO_LIMIT,
  90. BL_COHERENT_RAM_BASE,
  91. BL_COHERENT_RAM_END);
  92. }
  93. static void hikey960_ufs_reset(void)
  94. {
  95. unsigned int data, mask;
  96. mmio_write_32(CRG_PERDIS7_REG, 1 << 14);
  97. mmio_clrbits_32(UFS_SYS_PHY_CLK_CTRL_REG, BIT_SYSCTRL_REF_CLOCK_EN);
  98. do {
  99. data = mmio_read_32(UFS_SYS_PHY_CLK_CTRL_REG);
  100. } while (data & BIT_SYSCTRL_REF_CLOCK_EN);
  101. /* use abb clk */
  102. mmio_clrbits_32(UFS_SYS_UFS_SYSCTRL_REG, BIT_UFS_REFCLK_SRC_SE1);
  103. mmio_clrbits_32(UFS_SYS_PHY_ISO_EN_REG, BIT_UFS_REFCLK_ISO_EN);
  104. mmio_write_32(PCTRL_PERI_CTRL3_REG, (1 << 0) | (1 << 16));
  105. mdelay(1);
  106. mmio_write_32(CRG_PEREN7_REG, 1 << 14);
  107. mmio_setbits_32(UFS_SYS_PHY_CLK_CTRL_REG, BIT_SYSCTRL_REF_CLOCK_EN);
  108. mmio_write_32(CRG_PERRSTEN3_REG, PERI_UFS_BIT);
  109. do {
  110. data = mmio_read_32(CRG_PERRSTSTAT3_REG);
  111. } while ((data & PERI_UFS_BIT) == 0);
  112. mmio_setbits_32(UFS_SYS_PSW_POWER_CTRL_REG, BIT_UFS_PSW_MTCMOS_EN);
  113. mdelay(1);
  114. mmio_setbits_32(UFS_SYS_HC_LP_CTRL_REG, BIT_SYSCTRL_PWR_READY);
  115. mmio_write_32(UFS_SYS_UFS_DEVICE_RESET_CTRL_REG,
  116. MASK_UFS_DEVICE_RESET);
  117. /* clear SC_DIV_UFS_PERIBUS */
  118. mask = SC_DIV_UFS_PERIBUS << 16;
  119. mmio_write_32(CRG_CLKDIV17_REG, mask);
  120. /* set SC_DIV_UFSPHY_CFG(3) */
  121. mask = SC_DIV_UFSPHY_CFG_MASK << 16;
  122. data = SC_DIV_UFSPHY_CFG(3);
  123. mmio_write_32(CRG_CLKDIV16_REG, mask | data);
  124. data = mmio_read_32(UFS_SYS_PHY_CLK_CTRL_REG);
  125. data &= ~MASK_SYSCTRL_CFG_CLOCK_FREQ;
  126. data |= 0x39;
  127. mmio_write_32(UFS_SYS_PHY_CLK_CTRL_REG, data);
  128. mmio_clrbits_32(UFS_SYS_PHY_CLK_CTRL_REG, MASK_SYSCTRL_REF_CLOCK_SEL);
  129. mmio_setbits_32(UFS_SYS_CLOCK_GATE_BYPASS_REG,
  130. MASK_UFS_CLK_GATE_BYPASS);
  131. mmio_setbits_32(UFS_SYS_UFS_SYSCTRL_REG, MASK_UFS_SYSCTRL_BYPASS);
  132. mmio_setbits_32(UFS_SYS_PSW_CLK_CTRL_REG, BIT_SYSCTRL_PSW_CLK_EN);
  133. mmio_clrbits_32(UFS_SYS_PSW_POWER_CTRL_REG, BIT_UFS_PSW_ISO_CTRL);
  134. mmio_clrbits_32(UFS_SYS_PHY_ISO_EN_REG, BIT_UFS_PHY_ISO_CTRL);
  135. mmio_clrbits_32(UFS_SYS_HC_LP_CTRL_REG, BIT_SYSCTRL_LP_ISOL_EN);
  136. mmio_write_32(CRG_PERRSTDIS3_REG, PERI_ARST_UFS_BIT);
  137. mmio_setbits_32(UFS_SYS_RESET_CTRL_EN_REG, BIT_SYSCTRL_LP_RESET_N);
  138. mdelay(1);
  139. mmio_write_32(UFS_SYS_UFS_DEVICE_RESET_CTRL_REG,
  140. MASK_UFS_DEVICE_RESET | BIT_UFS_DEVICE_RESET);
  141. mdelay(20);
  142. mmio_write_32(UFS_SYS_UFS_DEVICE_RESET_CTRL_REG,
  143. 0x03300330);
  144. mmio_write_32(CRG_PERRSTDIS3_REG, PERI_UFS_BIT);
  145. do {
  146. data = mmio_read_32(CRG_PERRSTSTAT3_REG);
  147. } while (data & PERI_UFS_BIT);
  148. }
  149. static void hikey960_ufs_init(void)
  150. {
  151. dw_ufs_params_t ufs_params;
  152. memset(&ufs_params, 0, sizeof(ufs_params));
  153. ufs_params.reg_base = UFS_REG_BASE;
  154. ufs_params.desc_base = HIKEY960_UFS_DESC_BASE;
  155. ufs_params.desc_size = HIKEY960_UFS_DESC_SIZE;
  156. if ((ufs_params.flags & UFS_FLAGS_SKIPINIT) == 0)
  157. hikey960_ufs_reset();
  158. dw_ufs_init(&ufs_params);
  159. }
  160. /*
  161. * Function which will perform any remaining platform-specific setup that can
  162. * occur after the MMU and data cache have been enabled.
  163. */
  164. void bl1_platform_setup(void)
  165. {
  166. hikey960_clk_init();
  167. hikey960_pmu_init();
  168. hikey960_regulator_enable();
  169. hikey960_tzc_init();
  170. hikey960_peri_init();
  171. hikey960_ufs_init();
  172. hikey960_pinmux_init();
  173. hikey960_gpio_init();
  174. hikey960_io_setup();
  175. }
  176. /*
  177. * The following function checks if Firmware update is needed,
  178. * by checking if TOC in FIP image is valid or not.
  179. */
  180. unsigned int bl1_plat_get_next_image_id(void)
  181. {
  182. unsigned int mode, ret;
  183. mode = mmio_read_32(SCTRL_BAK_DATA0_REG);
  184. switch (mode & BOOT_MODE_MASK) {
  185. case BOOT_MODE_RECOVERY:
  186. ret = NS_BL1U_IMAGE_ID;
  187. break;
  188. default:
  189. WARN("Invalid boot mode is found:%d\n", mode);
  190. panic();
  191. }
  192. return ret;
  193. }
  194. image_desc_t *bl1_plat_get_image_desc(unsigned int image_id)
  195. {
  196. unsigned int index = 0;
  197. while (bl1_tbbr_image_descs[index].image_id != INVALID_IMAGE_ID) {
  198. if (bl1_tbbr_image_descs[index].image_id == image_id)
  199. return &bl1_tbbr_image_descs[index];
  200. index++;
  201. }
  202. return NULL;
  203. }
  204. void bl1_plat_set_ep_info(unsigned int image_id,
  205. entry_point_info_t *ep_info)
  206. {
  207. unsigned int data = 0;
  208. uintptr_t tmp = HIKEY960_NS_TMP_OFFSET;
  209. if (image_id != NS_BL1U_IMAGE_ID)
  210. panic();
  211. /* Copy NS BL1U from 0x1AC1_8000 to 0x1AC9_8000 */
  212. memcpy((void *)tmp, (void *)HIKEY960_NS_IMAGE_OFFSET,
  213. NS_BL1U_SIZE);
  214. memcpy((void *)NS_BL1U_BASE, (void *)tmp, NS_BL1U_SIZE);
  215. inv_dcache_range(NS_BL1U_BASE, NS_BL1U_SIZE);
  216. /* Initialize the GIC driver, cpu and distributor interfaces */
  217. gicv2_driver_init(&hikey960_gic_data);
  218. gicv2_distif_init();
  219. gicv2_pcpu_distif_init();
  220. gicv2_cpuif_enable();
  221. /* CNTFRQ is read-only in EL1 */
  222. write_cntfrq_el0(plat_get_syscnt_freq2());
  223. data = read_cpacr_el1();
  224. do {
  225. data |= 3 << 20;
  226. write_cpacr_el1(data);
  227. data = read_cpacr_el1();
  228. } while ((data & (3 << 20)) != (3 << 20));
  229. INFO("cpacr_el1:0x%x\n", data);
  230. ep_info->args.arg0 = 0xffff & read_mpidr();
  231. ep_info->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX,
  232. DISABLE_ALL_EXCEPTIONS);
  233. }