secure.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) 2016-2019, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <arch_helpers.h>
  8. #include <common/debug.h>
  9. #include <drivers/delay_timer.h>
  10. #include <plat_private.h>
  11. #include <secure.h>
  12. #include <soc.h>
  13. static void sgrf_ddr_rgn_global_bypass(uint32_t bypass)
  14. {
  15. if (bypass)
  16. /* set bypass (non-secure regions) for whole ddr regions */
  17. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(21),
  18. SGRF_DDR_RGN_BYPS);
  19. else
  20. /* cancel bypass for whole ddr regions */
  21. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(21),
  22. SGRF_DDR_RGN_NO_BYPS);
  23. }
  24. /**
  25. * There are 8 + 1 regions for DDR secure control:
  26. * DDR_RGN_0 ~ DDR_RGN_7: Per DDR_RGNs grain size is 1MB
  27. * DDR_RGN_X - the memories of exclude DDR_RGN_0 ~ DDR_RGN_7
  28. *
  29. * SGRF_SOC_CON6 - start address of RGN_0 + control
  30. * SGRF_SOC_CON7 - end address of RGN_0
  31. * ...
  32. * SGRF_SOC_CON20 - start address of the RGN_7 + control
  33. * SGRF_SOC_CON21 - end address of the RGN_7 + RGN_X control
  34. *
  35. * @rgn - the DDR regions 0 ~ 7 which are can be configured.
  36. * @st - start address to set as secure
  37. * @sz - length of area to set as secure
  38. * The @st_mb and @ed_mb indicate the start and end addresses for which to set
  39. * the security, and the unit is megabyte. When the st_mb == 0, ed_mb == 0, the
  40. * address range 0x0 ~ 0xfffff is secure.
  41. *
  42. * For example, if we would like to set the range [0, 32MB) is security via
  43. * DDR_RGN0, then rgn == 0, st_mb == 0, ed_mb == 31.
  44. */
  45. static void sgrf_ddr_rgn_config(uint32_t rgn, uintptr_t st, size_t sz)
  46. {
  47. uintptr_t ed = st + sz;
  48. uintptr_t st_mb, ed_mb;
  49. assert(rgn <= 7);
  50. assert(st < ed);
  51. /* check aligned 1MB */
  52. assert(st % SIZE_M(1) == 0);
  53. assert(ed % SIZE_M(1) == 0);
  54. st_mb = st / SIZE_M(1);
  55. ed_mb = ed / SIZE_M(1);
  56. /* set ddr region addr start */
  57. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(6 + (rgn * 2)),
  58. BITS_WITH_WMASK(st_mb, SGRF_DDR_RGN_ADDR_WMSK, 0));
  59. /* set ddr region addr end */
  60. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(6 + (rgn * 2) + 1),
  61. BITS_WITH_WMASK((ed_mb - 1), SGRF_DDR_RGN_ADDR_WMSK, 0));
  62. /* select region security */
  63. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(6 + (rgn * 2)),
  64. SGRF_DDR_RGN_SECURE_SEL);
  65. /* enable region security */
  66. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(6 + (rgn * 2)),
  67. SGRF_DDR_RGN_SECURE_EN);
  68. }
  69. void secure_watchdog_gate(void)
  70. {
  71. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(0), SGRF_PCLK_WDT_GATE);
  72. }
  73. void secure_watchdog_ungate(void)
  74. {
  75. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(0), SGRF_PCLK_WDT_UNGATE);
  76. }
  77. __pmusramfunc void sram_secure_timer_init(void)
  78. {
  79. mmio_write_32(STIMER1_BASE + TIMER_CONTROL_REG, 0);
  80. mmio_write_32(STIMER1_BASE + TIMER_LOAD_COUNT0, 0xffffffff);
  81. mmio_write_32(STIMER1_BASE + TIMER_LOAD_COUNT1, 0xffffffff);
  82. /* auto reload & enable the timer */
  83. mmio_write_32(STIMER1_BASE + TIMER_CONTROL_REG, TIMER_EN);
  84. }
  85. void secure_gic_init(void)
  86. {
  87. /* (re-)enable non-secure access to the gic*/
  88. mmio_write_32(CORE_AXI_BUS_BASE + CORE_AXI_SECURITY0,
  89. AXI_SECURITY0_GIC);
  90. }
  91. void secure_timer_init(void)
  92. {
  93. mmio_write_32(STIMER1_BASE + TIMER_CONTROL_REG, 0);
  94. mmio_write_32(STIMER1_BASE + TIMER_LOAD_COUNT0, 0xffffffff);
  95. mmio_write_32(STIMER1_BASE + TIMER_LOAD_COUNT1, 0xffffffff);
  96. /* auto reload & enable the timer */
  97. mmio_write_32(STIMER1_BASE + TIMER_CONTROL_REG, TIMER_EN);
  98. }
  99. void secure_sgrf_init(void)
  100. {
  101. /*
  102. * We use the first sram part to talk to the bootrom,
  103. * so make it secure.
  104. */
  105. mmio_write_32(TZPC_BASE + TZPC_R0SIZE, TZPC_SRAM_SECURE_4K(1));
  106. secure_gic_init();
  107. /* set all master ip to non-secure */
  108. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(2), SGRF_SOC_CON2_MST_NS);
  109. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(3), SGRF_SOC_CON3_MST_NS);
  110. /* setting all configurable ip into non-secure */
  111. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(4),
  112. SGRF_SOC_CON4_SECURE_WMSK /*TODO:|SGRF_STIMER_SECURE*/);
  113. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(5), SGRF_SOC_CON5_SECURE_WMSK);
  114. /* secure dma to non-secure */
  115. mmio_write_32(TZPC_BASE + TZPC_DECPROT1SET, 0xff);
  116. mmio_write_32(TZPC_BASE + TZPC_DECPROT2SET, 0xff);
  117. mmio_write_32(SGRF_BASE + SGRF_BUSDMAC_CON(1), 0x3800);
  118. dsb();
  119. /* rst dma1 */
  120. mmio_write_32(CRU_BASE + CRU_SOFTRSTS_CON(1),
  121. RST_DMA1_MSK | (RST_DMA1_MSK << 16));
  122. /* rst dma2 */
  123. mmio_write_32(CRU_BASE + CRU_SOFTRSTS_CON(4),
  124. RST_DMA2_MSK | (RST_DMA2_MSK << 16));
  125. dsb();
  126. /* release dma1 rst*/
  127. mmio_write_32(CRU_BASE + CRU_SOFTRSTS_CON(1), (RST_DMA1_MSK << 16));
  128. /* release dma2 rst*/
  129. mmio_write_32(CRU_BASE + CRU_SOFTRSTS_CON(4), (RST_DMA2_MSK << 16));
  130. }
  131. void secure_sgrf_ddr_rgn_init(void)
  132. {
  133. sgrf_ddr_rgn_config(0, TZRAM_BASE, TZRAM_SIZE);
  134. sgrf_ddr_rgn_global_bypass(0);
  135. }