secure.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2019, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <ddr_parameter.h>
  8. #include <plat_private.h>
  9. #include <secure.h>
  10. #include <px30_def.h>
  11. /**
  12. * There are 8 regions for DDR security control
  13. * @rgn - the DDR regions 0 ~ 7 which are can be configured.
  14. * @st - start address to set as secure
  15. * @sz - length of area to set as secure
  16. * The internal unit is megabytes, so memory areas need to be aligned
  17. * to megabyte borders.
  18. */
  19. static void secure_ddr_region(uint32_t rgn,
  20. uintptr_t st, size_t sz)
  21. {
  22. uintptr_t ed = st + sz;
  23. uintptr_t st_mb, ed_mb;
  24. uint32_t val;
  25. assert(rgn <= 7);
  26. assert(st < ed);
  27. /* check aligned 1MB */
  28. assert(st % SIZE_M(1) == 0);
  29. assert(ed % SIZE_M(1) == 0);
  30. st_mb = st / SIZE_M(1);
  31. ed_mb = ed / SIZE_M(1);
  32. /* map top and base */
  33. mmio_write_32(FIREWALL_DDR_BASE +
  34. FIREWALL_DDR_FW_DDR_RGN(rgn),
  35. RG_MAP_SECURE(ed_mb, st_mb));
  36. /* enable secure */
  37. val = mmio_read_32(FIREWALL_DDR_BASE + FIREWALL_DDR_FW_DDR_CON_REG);
  38. val |= BIT(rgn);
  39. mmio_write_32(FIREWALL_DDR_BASE +
  40. FIREWALL_DDR_FW_DDR_CON_REG, val);
  41. }
  42. void secure_timer_init(void)
  43. {
  44. mmio_write_32(STIMER_CHN_BASE(1) + TIMER_CONTROL_REG,
  45. TIMER_DIS);
  46. mmio_write_32(STIMER_CHN_BASE(1) + TIMER_LOAD_COUNT0, 0xffffffff);
  47. mmio_write_32(STIMER_CHN_BASE(1) + TIMER_LOAD_COUNT1, 0xffffffff);
  48. /* auto reload & enable the timer */
  49. mmio_write_32(STIMER_CHN_BASE(1) + TIMER_CONTROL_REG,
  50. TIMER_EN | TIMER_FMODE);
  51. }
  52. void sgrf_init(void)
  53. {
  54. #ifdef PLAT_RK_SECURE_DDR_MINILOADER
  55. uint32_t i;
  56. struct param_ddr_usage usg;
  57. /* general secure regions */
  58. usg = ddr_region_usage_parse(DDR_PARAM_BASE,
  59. PLAT_MAX_DDR_CAPACITY_MB);
  60. /* region-0 for TF-A, region-1 for optional OP-TEE */
  61. assert(usg.s_nr < 7);
  62. for (i = 0; i < usg.s_nr; i++)
  63. secure_ddr_region(7 - i, usg.s_top[i], usg.s_base[i]);
  64. #endif
  65. /* secure the trustzone ram */
  66. secure_ddr_region(0, TZRAM_BASE, TZRAM_SIZE);
  67. /* set all slave ip into no-secure, except stimer */
  68. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(4), SGRF_SLV_S_ALL_NS);
  69. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(5), SGRF_SLV_S_ALL_NS);
  70. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(6), SGRF_SLV_S_ALL_NS);
  71. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(7), SGRF_SLV_S_ALL_NS);
  72. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(8), 0x00030000);
  73. /* set master crypto to no-secure, dcf to secure */
  74. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(3), 0x000f0003);
  75. /* set DMAC into no-secure */
  76. mmio_write_32(SGRF_BASE + SGRF_DMAC_CON(0), DMA_IRQ_BOOT_NS);
  77. mmio_write_32(SGRF_BASE + SGRF_DMAC_CON(1), DMA_PERI_CH_NS_15_0);
  78. mmio_write_32(SGRF_BASE + SGRF_DMAC_CON(2), DMA_PERI_CH_NS_19_16);
  79. mmio_write_32(SGRF_BASE + SGRF_DMAC_CON(3), DMA_MANAGER_BOOT_NS);
  80. /* soft reset dma before use */
  81. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(1), DMA_SOFTRST_REQ);
  82. udelay(5);
  83. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(1), DMA_SOFTRST_RLS);
  84. }