fsl_sec_mon.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2015 Freescale Semiconductor, Inc.
  4. */
  5. #include <common.h>
  6. #include <fsl_sec_mon.h>
  7. static u32 get_sec_mon_state(void)
  8. {
  9. struct ccsr_sec_mon_regs *sec_mon_regs = (void *)
  10. (CONFIG_SYS_SEC_MON_ADDR);
  11. return sec_mon_in32(&sec_mon_regs->hp_stat) & HPSR_SSM_ST_MASK;
  12. }
  13. static int set_sec_mon_state_non_sec(void)
  14. {
  15. u32 sts;
  16. int timeout = 10;
  17. struct ccsr_sec_mon_regs *sec_mon_regs = (void *)
  18. (CONFIG_SYS_SEC_MON_ADDR);
  19. sts = get_sec_mon_state();
  20. switch (sts) {
  21. /*
  22. * If initial state is check or Non-Secure, then set the Software
  23. * Security Violation Bit and transition to Non-Secure State.
  24. */
  25. case HPSR_SSM_ST_CHECK:
  26. printf("SEC_MON state transitioning to Non Secure.\n");
  27. sec_mon_setbits32(&sec_mon_regs->hp_com, HPCOMR_SW_SV);
  28. /* polling loop till SEC_MON is in Non Secure state */
  29. while (timeout) {
  30. sts = get_sec_mon_state();
  31. if ((sts & HPSR_SSM_ST_MASK) ==
  32. HPSR_SSM_ST_NON_SECURE)
  33. break;
  34. udelay(10);
  35. timeout--;
  36. }
  37. if (timeout == 0) {
  38. printf("SEC_MON state transition timeout.\n");
  39. return -1;
  40. }
  41. break;
  42. /*
  43. * If initial state is Trusted, Secure or Soft-Fail, then first set
  44. * the Software Security Violation Bit and transition to Soft-Fail
  45. * State.
  46. */
  47. case HPSR_SSM_ST_TRUST:
  48. case HPSR_SSM_ST_SECURE:
  49. case HPSR_SSM_ST_SOFT_FAIL:
  50. printf("SEC_MON state transitioning to Soft Fail.\n");
  51. sec_mon_setbits32(&sec_mon_regs->hp_com, HPCOMR_SW_SV);
  52. /* polling loop till SEC_MON is in Soft-Fail state */
  53. while (timeout) {
  54. sts = get_sec_mon_state();
  55. if ((sts & HPSR_SSM_ST_MASK) ==
  56. HPSR_SSM_ST_SOFT_FAIL)
  57. break;
  58. udelay(10);
  59. timeout--;
  60. }
  61. if (timeout == 0) {
  62. printf("SEC_MON state transition timeout.\n");
  63. return -1;
  64. }
  65. timeout = 10;
  66. /*
  67. * If SSM Soft Fail to Non-Secure State Transition
  68. * disable is not set, then set SSM_ST bit and
  69. * transition to Non-Secure State.
  70. */
  71. if ((sec_mon_in32(&sec_mon_regs->hp_com) &
  72. HPCOMR_SSM_SFNS_DIS) == 0) {
  73. printf("SEC_MON state transitioning to Non Secure.\n");
  74. sec_mon_setbits32(&sec_mon_regs->hp_com, HPCOMR_SSM_ST);
  75. /* polling loop till SEC_MON is in Non Secure*/
  76. while (timeout) {
  77. sts = get_sec_mon_state();
  78. if ((sts & HPSR_SSM_ST_MASK) ==
  79. HPSR_SSM_ST_NON_SECURE)
  80. break;
  81. udelay(10);
  82. timeout--;
  83. }
  84. if (timeout == 0) {
  85. printf("SEC_MON state transition timeout.\n");
  86. return -1;
  87. }
  88. }
  89. break;
  90. default:
  91. printf("SEC_MON already in Non Secure state.\n");
  92. return 0;
  93. }
  94. return 0;
  95. }
  96. static int set_sec_mon_state_soft_fail(void)
  97. {
  98. u32 sts;
  99. int timeout = 10;
  100. struct ccsr_sec_mon_regs *sec_mon_regs = (void *)
  101. (CONFIG_SYS_SEC_MON_ADDR);
  102. printf("SEC_MON state transitioning to Soft Fail.\n");
  103. sec_mon_setbits32(&sec_mon_regs->hp_com, HPCOMR_SW_FSV);
  104. /* polling loop till SEC_MON is in Soft-Fail state */
  105. while (timeout) {
  106. sts = get_sec_mon_state();
  107. if ((sts & HPSR_SSM_ST_MASK) ==
  108. HPSR_SSM_ST_SOFT_FAIL)
  109. break;
  110. udelay(10);
  111. timeout--;
  112. }
  113. if (timeout == 0) {
  114. printf("SEC_MON state transition timeout.\n");
  115. return -1;
  116. }
  117. return 0;
  118. }
  119. int set_sec_mon_state(u32 state)
  120. {
  121. int ret = -1;
  122. switch (state) {
  123. case HPSR_SSM_ST_NON_SECURE:
  124. ret = set_sec_mon_state_non_sec();
  125. break;
  126. case HPSR_SSM_ST_SOFT_FAIL:
  127. ret = set_sec_mon_state_soft_fail();
  128. break;
  129. default:
  130. printf("SEC_MON state transition not supported.\n");
  131. return 0;
  132. }
  133. return ret;
  134. }