secure.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_DDRRGN_CON0_16(16),
  18. SGRF_DDR_RGN_BYPS);
  19. else
  20. /* cancel bypass for whole ddr regions */
  21. mmio_write_32(SGRF_BASE + SGRF_DDRRGN_CON0_16(16),
  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. * DDR_RGN_0 - start address of the RGN0
  30. * DDR_RGN_8 - end address of the RGN0
  31. * DDR_RGN_1 - start address of the RGN1
  32. * DDR_RGN_9 - end address of the RGN1
  33. * ...
  34. * DDR_RGN_7 - start address of the RGN7
  35. * DDR_RGN_15 - end address of the RGN7
  36. * DDR_RGN_16 - bit 0 ~ 7 is bitmap for RGN0~7 secure,0: disable, 1: enable
  37. * bit 8 is setting for RGNx, the rest of the memory and region
  38. * which excludes RGN0~7, 0: disable, 1: enable
  39. * bit 9, the global secure configuration via bypass, 0: disable
  40. * bypass, 1: enable bypass
  41. *
  42. * @rgn - the DDR regions 0 ~ 7 which are can be configured.
  43. * @st - start address to set as secure
  44. * @sz - length of area to set as secure
  45. * The @st_mb and @ed_mb indicate the start and end addresses for which to set
  46. * the security, and the unit is megabyte. When the st_mb == 0, ed_mb == 0, the
  47. * address range 0x0 ~ 0xfffff is secure.
  48. *
  49. * For example, if we would like to set the range [0, 32MB) is security via
  50. * DDR_RGN0, then rgn == 0, st_mb == 0, ed_mb == 31.
  51. */
  52. static void sgrf_ddr_rgn_config(uint32_t rgn,
  53. uintptr_t st, size_t sz)
  54. {
  55. uintptr_t ed = st + sz;
  56. uintptr_t st_mb, ed_mb;
  57. assert(rgn <= 7);
  58. assert(st < ed);
  59. /* check aligned 1MB */
  60. assert(st % SIZE_M(1) == 0);
  61. assert(ed % SIZE_M(1) == 0);
  62. st_mb = st / SIZE_M(1);
  63. ed_mb = ed / SIZE_M(1);
  64. /* set ddr region addr start */
  65. mmio_write_32(SGRF_BASE + SGRF_DDRRGN_CON0_16(rgn),
  66. BITS_WITH_WMASK(st_mb, SGRF_DDR_RGN_0_16_WMSK, 0));
  67. /* set ddr region addr end */
  68. mmio_write_32(SGRF_BASE + SGRF_DDRRGN_CON0_16(rgn + 8),
  69. BITS_WITH_WMASK((ed_mb - 1), SGRF_DDR_RGN_0_16_WMSK, 0));
  70. mmio_write_32(SGRF_BASE + SGRF_DDRRGN_CON0_16(16),
  71. BIT_WITH_WMSK(rgn));
  72. }
  73. void secure_watchdog_gate(void)
  74. {
  75. /**
  76. * Disable CA53 and CM0 wdt pclk
  77. * BIT[8]: ca53 wdt pclk, 0: enable 1: disable
  78. * BIT[10]: cm0 wdt pclk, 0: enable 1: disable
  79. */
  80. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(3),
  81. BIT_WITH_WMSK(PCLK_WDT_CA53_GATE_SHIFT) |
  82. BIT_WITH_WMSK(PCLK_WDT_CM0_GATE_SHIFT));
  83. }
  84. __pmusramfunc void secure_watchdog_ungate(void)
  85. {
  86. /**
  87. * Enable CA53 and CM0 wdt pclk
  88. * BIT[8]: ca53 wdt pclk, 0: enable 1: disable
  89. * BIT[10]: cm0 wdt pclk, 0: enable 1: disable
  90. */
  91. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(3),
  92. WMSK_BIT(PCLK_WDT_CA53_GATE_SHIFT) |
  93. WMSK_BIT(PCLK_WDT_CM0_GATE_SHIFT));
  94. }
  95. __pmusramfunc void sram_secure_timer_init(void)
  96. {
  97. mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_END_COUNT0, 0xffffffff);
  98. mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_END_COUNT1, 0xffffffff);
  99. mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_INIT_COUNT0, 0x0);
  100. mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_INIT_COUNT0, 0x0);
  101. /* auto reload & enable the timer */
  102. mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_CONTROL_REG,
  103. TIMER_EN | TIMER_FMODE);
  104. }
  105. void secure_timer_init(void)
  106. {
  107. mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_END_COUNT0, 0xffffffff);
  108. mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_END_COUNT1, 0xffffffff);
  109. mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_INIT_COUNT0, 0x0);
  110. mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_INIT_COUNT0, 0x0);
  111. /* auto reload & enable the timer */
  112. mmio_write_32(STIMER1_CHN_BASE(5) + TIMER_CONTROL_REG,
  113. TIMER_EN | TIMER_FMODE);
  114. }
  115. void secure_sgrf_init(void)
  116. {
  117. /* security config for master */
  118. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(5),
  119. REG_SOC_WMSK | SGRF_SOC_ALLMST_NS);
  120. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(6),
  121. REG_SOC_WMSK | SGRF_SOC_ALLMST_NS);
  122. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(7),
  123. REG_SOC_WMSK | SGRF_SOC_ALLMST_NS);
  124. /* security config for slave */
  125. mmio_write_32(SGRF_BASE + SGRF_PMU_SLV_CON0_1(0),
  126. SGRF_PMU_SLV_S_CFGED |
  127. SGRF_PMU_SLV_CRYPTO1_NS);
  128. mmio_write_32(SGRF_BASE + SGRF_PMU_SLV_CON0_1(1),
  129. SGRF_SLV_S_WMSK | SGRF_PMUSRAM_S);
  130. mmio_write_32(SGRF_BASE + SGRF_SLV_SECURE_CON0_4(0),
  131. SGRF_SLV_S_WMSK | SGRF_SLV_S_ALL_NS);
  132. mmio_write_32(SGRF_BASE + SGRF_SLV_SECURE_CON0_4(1),
  133. SGRF_SLV_S_WMSK | SGRF_SLV_S_ALL_NS);
  134. mmio_write_32(SGRF_BASE + SGRF_SLV_SECURE_CON0_4(2),
  135. SGRF_SLV_S_WMSK | SGRF_SLV_S_ALL_NS);
  136. mmio_write_32(SGRF_BASE + SGRF_SLV_SECURE_CON0_4(3),
  137. SGRF_SLV_S_WMSK | SGRF_SLV_S_ALL_NS);
  138. mmio_write_32(SGRF_BASE + SGRF_SLV_SECURE_CON0_4(4),
  139. SGRF_SLV_S_WMSK | SGRF_INTSRAM_S);
  140. }
  141. void secure_sgrf_ddr_rgn_init(void)
  142. {
  143. sgrf_ddr_rgn_config(0, TZRAM_BASE, TZRAM_SIZE);
  144. sgrf_ddr_rgn_global_bypass(0);
  145. }