Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2015 Freescale Semiconductor, Inc. |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <fsl_sec_mon.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 8 | #include <linux/delay.h> |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 9 | |
Sumit Garg | bc17f98 | 2016-08-31 08:54:15 -0400 | [diff] [blame] | 10 | static u32 get_sec_mon_state(void) |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 11 | { |
| 12 | struct ccsr_sec_mon_regs *sec_mon_regs = (void *) |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 13 | (CFG_SYS_SEC_MON_ADDR); |
Sumit Garg | bc17f98 | 2016-08-31 08:54:15 -0400 | [diff] [blame] | 14 | return sec_mon_in32(&sec_mon_regs->hp_stat) & HPSR_SSM_ST_MASK; |
| 15 | } |
| 16 | |
| 17 | static int set_sec_mon_state_non_sec(void) |
| 18 | { |
| 19 | u32 sts; |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 20 | int timeout = 10; |
Sumit Garg | bc17f98 | 2016-08-31 08:54:15 -0400 | [diff] [blame] | 21 | struct ccsr_sec_mon_regs *sec_mon_regs = (void *) |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 22 | (CFG_SYS_SEC_MON_ADDR); |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 23 | |
Sumit Garg | bc17f98 | 2016-08-31 08:54:15 -0400 | [diff] [blame] | 24 | sts = get_sec_mon_state(); |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 25 | |
Sumit Garg | bc17f98 | 2016-08-31 08:54:15 -0400 | [diff] [blame] | 26 | switch (sts) { |
| 27 | /* |
| 28 | * If initial state is check or Non-Secure, then set the Software |
| 29 | * Security Violation Bit and transition to Non-Secure State. |
| 30 | */ |
| 31 | case HPSR_SSM_ST_CHECK: |
| 32 | printf("SEC_MON state transitioning to Non Secure.\n"); |
| 33 | sec_mon_setbits32(&sec_mon_regs->hp_com, HPCOMR_SW_SV); |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 34 | |
Sumit Garg | bc17f98 | 2016-08-31 08:54:15 -0400 | [diff] [blame] | 35 | /* polling loop till SEC_MON is in Non Secure state */ |
| 36 | while (timeout) { |
| 37 | sts = get_sec_mon_state(); |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 38 | |
Sumit Garg | bc17f98 | 2016-08-31 08:54:15 -0400 | [diff] [blame] | 39 | if ((sts & HPSR_SSM_ST_MASK) == |
| 40 | HPSR_SSM_ST_NON_SECURE) |
| 41 | break; |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 42 | |
Sumit Garg | bc17f98 | 2016-08-31 08:54:15 -0400 | [diff] [blame] | 43 | udelay(10); |
| 44 | timeout--; |
| 45 | } |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 46 | |
Sumit Garg | bc17f98 | 2016-08-31 08:54:15 -0400 | [diff] [blame] | 47 | if (timeout == 0) { |
| 48 | printf("SEC_MON state transition timeout.\n"); |
| 49 | return -1; |
| 50 | } |
| 51 | break; |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 52 | |
Sumit Garg | bc17f98 | 2016-08-31 08:54:15 -0400 | [diff] [blame] | 53 | /* |
| 54 | * If initial state is Trusted, Secure or Soft-Fail, then first set |
| 55 | * the Software Security Violation Bit and transition to Soft-Fail |
| 56 | * State. |
| 57 | */ |
| 58 | case HPSR_SSM_ST_TRUST: |
| 59 | case HPSR_SSM_ST_SECURE: |
| 60 | case HPSR_SSM_ST_SOFT_FAIL: |
| 61 | printf("SEC_MON state transitioning to Soft Fail.\n"); |
| 62 | sec_mon_setbits32(&sec_mon_regs->hp_com, HPCOMR_SW_SV); |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 63 | |
Sumit Garg | bc17f98 | 2016-08-31 08:54:15 -0400 | [diff] [blame] | 64 | /* polling loop till SEC_MON is in Soft-Fail state */ |
| 65 | while (timeout) { |
| 66 | sts = get_sec_mon_state(); |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 67 | |
Sumit Garg | bc17f98 | 2016-08-31 08:54:15 -0400 | [diff] [blame] | 68 | if ((sts & HPSR_SSM_ST_MASK) == |
| 69 | HPSR_SSM_ST_SOFT_FAIL) |
| 70 | break; |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 71 | |
Sumit Garg | bc17f98 | 2016-08-31 08:54:15 -0400 | [diff] [blame] | 72 | udelay(10); |
| 73 | timeout--; |
| 74 | } |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 75 | |
Sumit Garg | bc17f98 | 2016-08-31 08:54:15 -0400 | [diff] [blame] | 76 | if (timeout == 0) { |
| 77 | printf("SEC_MON state transition timeout.\n"); |
| 78 | return -1; |
| 79 | } |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 80 | |
Sumit Garg | bc17f98 | 2016-08-31 08:54:15 -0400 | [diff] [blame] | 81 | timeout = 10; |
| 82 | |
| 83 | /* |
| 84 | * If SSM Soft Fail to Non-Secure State Transition |
| 85 | * disable is not set, then set SSM_ST bit and |
| 86 | * transition to Non-Secure State. |
| 87 | */ |
| 88 | if ((sec_mon_in32(&sec_mon_regs->hp_com) & |
| 89 | HPCOMR_SSM_SFNS_DIS) == 0) { |
| 90 | printf("SEC_MON state transitioning to Non Secure.\n"); |
| 91 | sec_mon_setbits32(&sec_mon_regs->hp_com, HPCOMR_SSM_ST); |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 92 | |
Sumit Garg | bc17f98 | 2016-08-31 08:54:15 -0400 | [diff] [blame] | 93 | /* polling loop till SEC_MON is in Non Secure*/ |
| 94 | while (timeout) { |
| 95 | sts = get_sec_mon_state(); |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 96 | |
Sumit Garg | bc17f98 | 2016-08-31 08:54:15 -0400 | [diff] [blame] | 97 | if ((sts & HPSR_SSM_ST_MASK) == |
| 98 | HPSR_SSM_ST_NON_SECURE) |
| 99 | break; |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 100 | |
Sumit Garg | bc17f98 | 2016-08-31 08:54:15 -0400 | [diff] [blame] | 101 | udelay(10); |
| 102 | timeout--; |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | if (timeout == 0) { |
| 106 | printf("SEC_MON state transition timeout.\n"); |
| 107 | return -1; |
| 108 | } |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 109 | } |
Sumit Garg | bc17f98 | 2016-08-31 08:54:15 -0400 | [diff] [blame] | 110 | break; |
| 111 | default: |
| 112 | printf("SEC_MON already in Non Secure state.\n"); |
| 113 | return 0; |
| 114 | } |
| 115 | return 0; |
| 116 | } |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 117 | |
Sumit Garg | bc17f98 | 2016-08-31 08:54:15 -0400 | [diff] [blame] | 118 | static int set_sec_mon_state_soft_fail(void) |
| 119 | { |
| 120 | u32 sts; |
| 121 | int timeout = 10; |
| 122 | struct ccsr_sec_mon_regs *sec_mon_regs = (void *) |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 123 | (CFG_SYS_SEC_MON_ADDR); |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 124 | |
Sumit Garg | bc17f98 | 2016-08-31 08:54:15 -0400 | [diff] [blame] | 125 | printf("SEC_MON state transitioning to Soft Fail.\n"); |
| 126 | sec_mon_setbits32(&sec_mon_regs->hp_com, HPCOMR_SW_FSV); |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 127 | |
Sumit Garg | bc17f98 | 2016-08-31 08:54:15 -0400 | [diff] [blame] | 128 | /* polling loop till SEC_MON is in Soft-Fail state */ |
| 129 | while (timeout) { |
| 130 | sts = get_sec_mon_state(); |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 131 | |
Sumit Garg | bc17f98 | 2016-08-31 08:54:15 -0400 | [diff] [blame] | 132 | if ((sts & HPSR_SSM_ST_MASK) == |
| 133 | HPSR_SSM_ST_SOFT_FAIL) |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 134 | break; |
Sumit Garg | bc17f98 | 2016-08-31 08:54:15 -0400 | [diff] [blame] | 135 | |
| 136 | udelay(10); |
| 137 | timeout--; |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 138 | } |
| 139 | |
Sumit Garg | bc17f98 | 2016-08-31 08:54:15 -0400 | [diff] [blame] | 140 | if (timeout == 0) { |
| 141 | printf("SEC_MON state transition timeout.\n"); |
| 142 | return -1; |
| 143 | } |
gaurav rana | 9aaea44 | 2015-02-27 09:44:22 +0530 | [diff] [blame] | 144 | return 0; |
| 145 | } |
Sumit Garg | bc17f98 | 2016-08-31 08:54:15 -0400 | [diff] [blame] | 146 | |
| 147 | int set_sec_mon_state(u32 state) |
| 148 | { |
| 149 | int ret = -1; |
| 150 | |
| 151 | switch (state) { |
| 152 | case HPSR_SSM_ST_NON_SECURE: |
| 153 | ret = set_sec_mon_state_non_sec(); |
| 154 | break; |
| 155 | case HPSR_SSM_ST_SOFT_FAIL: |
| 156 | ret = set_sec_mon_state_soft_fail(); |
| 157 | break; |
| 158 | default: |
| 159 | printf("SEC_MON state transition not supported.\n"); |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | return ret; |
| 164 | } |