1 /***********************************************************************************************************************
2 * Copyright [2020-2023] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved.
3 *
4 * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products
5 * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are
6 * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use
7 * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property
8 * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas
9 * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION
10 * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT
11 * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES
12 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR
13 * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM
14 * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION
15 * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING,
16 * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS,
17 * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY
18 * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS.
19 **********************************************************************************************************************/
20
21 /***********************************************************************************************************************
22 * Includes <System Includes> , "Project Includes"
23 **********************************************************************************************************************/
24 #include "bsp_api.h"
25
26 /***********************************************************************************************************************
27 * Macro definitions
28 **********************************************************************************************************************/
29
30 /* Key code for writing PRCR register. */
31 #define BSP_PRV_PRCR_KEY (0xA500U)
32
33 /***********************************************************************************************************************
34 * Typedef definitions
35 **********************************************************************************************************************/
36
37 /***********************************************************************************************************************
38 * Exported global variables (to be accessed by other files)
39 **********************************************************************************************************************/
40
41 /***********************************************************************************************************************
42 * Private global variables and functions
43 **********************************************************************************************************************/
44
45 /** Used for holding reference counters for protection bits. */
46 volatile uint16_t g_protect_counters[4] BSP_SECTION_EARLY_INIT;
47
48 /** Masks for setting or clearing the PRCR register. Use -1 for size because PWPR in MPC is used differently. */
49 static const uint16_t g_prcr_masks[] =
50 {
51 0x0001U, /* PRC0. */
52 0x0002U, /* PRC1. */
53 0x0008U, /* PRC3. */
54 0x0010U, /* PRC4. */
55 };
56
57 /*******************************************************************************************************************//**
58 * @addtogroup BSP_MCU
59 *
60 * @{
61 **********************************************************************************************************************/
62
63 /*******************************************************************************************************************//**
64 * Enable register protection. Registers that are protected cannot be written to. Register protection is
65 * enabled by using the Protect Register (PRCR) and the MPC's Write-Protect Register (PWPR).
66 *
67 * @param[in] regs_to_protect Registers which have write protection enabled.
68 **********************************************************************************************************************/
R_BSP_RegisterProtectEnable(bsp_reg_protect_t regs_to_protect)69 void R_BSP_RegisterProtectEnable (bsp_reg_protect_t regs_to_protect)
70 {
71 /** Get/save the current state of interrupts */
72 FSP_CRITICAL_SECTION_DEFINE;
73 FSP_CRITICAL_SECTION_ENTER;
74
75 /* Is it safe to disable write access? */
76 if (0U != g_protect_counters[regs_to_protect])
77 {
78 /* Decrement the protect counter */
79 g_protect_counters[regs_to_protect]--;
80 }
81
82 /* Is it safe to disable write access? */
83 if (0U == g_protect_counters[regs_to_protect])
84 {
85 /** Enable protection using PRCR register.
86 *
87 * When writing to the PRCR register the upper 8-bits must be the correct key. Set lower bits to 0 to
88 * disable writes. */
89 #if BSP_TZ_NONSECURE_BUILD && BSP_FEATURE_TZ_VERSION == 2
90 R_SYSTEM->PRCR_NS = ((R_SYSTEM->PRCR_NS | BSP_PRV_PRCR_KEY) & (uint16_t) (~g_prcr_masks[regs_to_protect]));
91 #else
92 R_SYSTEM->PRCR = ((R_SYSTEM->PRCR | BSP_PRV_PRCR_KEY) & (uint16_t) (~g_prcr_masks[regs_to_protect]));
93 #endif
94 }
95
96 /** Restore the interrupt state */
97 FSP_CRITICAL_SECTION_EXIT;
98 }
99
100 /*******************************************************************************************************************//**
101 * Disable register protection. Registers that are protected cannot be written to. Register protection is
102 * disabled by using the Protect Register (PRCR) and the MPC's Write-Protect Register (PWPR).
103 *
104 * @param[in] regs_to_unprotect Registers which have write protection disabled.
105 **********************************************************************************************************************/
R_BSP_RegisterProtectDisable(bsp_reg_protect_t regs_to_unprotect)106 void R_BSP_RegisterProtectDisable (bsp_reg_protect_t regs_to_unprotect)
107 {
108 /** Get/save the current state of interrupts */
109 FSP_CRITICAL_SECTION_DEFINE;
110 FSP_CRITICAL_SECTION_ENTER;
111
112 /* If this is first entry then disable protection. */
113 if (0U == g_protect_counters[regs_to_unprotect])
114 {
115 /** Disable protection using PRCR register.
116 *
117 * When writing to the PRCR register the upper 8-bits must be the correct key. Set lower bits to 0 to
118 * disable writes. */
119 #if BSP_TZ_NONSECURE_BUILD && BSP_FEATURE_TZ_VERSION == 2
120 R_SYSTEM->PRCR_NS = ((R_SYSTEM->PRCR_NS | BSP_PRV_PRCR_KEY) | g_prcr_masks[regs_to_unprotect]);
121 #else
122 R_SYSTEM->PRCR = ((R_SYSTEM->PRCR | BSP_PRV_PRCR_KEY) | g_prcr_masks[regs_to_unprotect]);
123 #endif
124 }
125
126 /** Increment the protect counter */
127 g_protect_counters[regs_to_unprotect]++;
128
129 /** Restore the interrupt state */
130 FSP_CRITICAL_SECTION_EXIT;
131 }
132
133 /** @} (end addtogroup BSP_MCU) */
134