1 /***************************************************************************//**
2 * \file cy_prot.c
3 * \version 1.100
4 *
5 * \brief
6 * Provides an API implementation of the Protection Unit driver
7 *
8 ********************************************************************************
9 * \copyright
10 * Copyright (c) (2016-2022), Cypress Semiconductor Corporation
11 * (an Infineon company) or an affiliate of Cypress Semiconductor Corporation.
12 * SPDX-License-Identifier: Apache-2.0
13 *
14 * Licensed under the Apache License, Version 2.0 (the "License");
15 * you may not use this file except in compliance with the License.
16 * You may obtain a copy of the License at
17 *
18 *     http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS,
22 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
25 *******************************************************************************/
26 
27 #include "cy_device.h"
28 
29 #if defined (CY_IP_M4CPUSS) || defined (CY_IP_M7CPUSS)
30 
31 #include "cy_prot.h"
32 
33 #if defined(CY_DEVICE_SECURE) && defined(CY_DEVICE_PSOC6ABLE2)
34     #include "cy_pra.h"
35 #endif /* defined(CY_DEVICE_SECURE) && defined(CY_DEVICE_PSOC6ABLE2) */
36 
37 #if defined(__cplusplus)
38 extern "C" {
39 #endif
40 
41 #if defined (CY_IP_MXPERI_VERSION) && (CY_IP_MXPERI_VERSION == 3U)
42 #define PERI_MS_PPU_PR_V2_MS_ATT0_PC0_NS_Msk          (PERI_MS_PPU_PR_MS_ATT0_PC0_NS_Msk)
43 #define PERI_MS_PPU_PR_V2_MS_ATT0_PC1_UR_Pos          (PERI_MS_PPU_PR_MS_ATT0_PC1_UR_Pos)
44 #define PERI_MS_PPU_PR_V2_SL_ADDR_ADDR30_Pos          (PERI_MS_PPU_PR_SL_ADDR_ADDR30_Pos)
45 #define PERI_MS_PPU_PR_V2_SL_ADDR_ADDR30_Msk          (PERI_MS_PPU_PR_SL_ADDR_ADDR30_Msk)
46 #define PERI_MS_PPU_PR_V2_SL_SIZE_REGION_SIZE_Pos     (PERI_MS_PPU_PR_SL_SIZE_REGION_SIZE_Pos)
47 #define PERI_MS_PPU_PR_V2_SL_SIZE_REGION_SIZE_Msk     (PERI_MS_PPU_PR_SL_SIZE_REGION_SIZE_Msk)
48 #define PERI_MS_PPU_PR_V2_SL_SIZE_VALID_Pos           (PERI_MS_PPU_PR_SL_SIZE_VALID_Pos)
49 #define PERI_MS_PPU_PR_V2_SL_SIZE_VALID_Msk           (PERI_MS_PPU_PR_SL_SIZE_VALID_Msk)
50 #endif
51 
52 static bool Prot_IsSmpuStructDisabled(uint32_t smpuStcIndex);
53 #if defined (CY_IP_M4CPUSS) && defined (CY_IP_MXPERI_VERSION) && (CY_IP_MXPERI_VERSION == 1u)
54 static bool Prot_IsPpuProgStructDisabled(uint32_t ppuStcIndex);
55 #endif
56 #if defined (CY_IP_MXPERI_VERSION) && (CY_IP_MXPERI_VERSION > 1U)
57 static cy_en_prot_status_t Prot_ConfigPpuAtt(volatile uint32_t * reg, uint16_t pcMask,
58                                        cy_en_prot_perm_t userPermission, cy_en_prot_perm_t privPermission, bool secure);
59 #endif
60 
61 /* Define to enable all attributes for SMPU slave structure */
62 #define PROT_SMPU_STRUCT_ATT0_ENABLE_ALL_MASK      (PROT_SMPU_SMPU_STRUCT_ATT0_UR_Msk | \
63                                                     PROT_SMPU_SMPU_STRUCT_ATT0_UW_Msk | \
64                                                     PROT_SMPU_SMPU_STRUCT_ATT0_UX_Msk | \
65                                                     PROT_SMPU_SMPU_STRUCT_ATT0_PR_Msk | \
66                                                     PROT_SMPU_SMPU_STRUCT_ATT0_PW_Msk | \
67                                                     PROT_SMPU_SMPU_STRUCT_ATT0_PX_Msk | \
68                                                     PROT_SMPU_SMPU_STRUCT_ATT0_NS_Msk)
69 
70  /* Define to enable all attributes for programmable PPU slave structure */
71 #define PROT_PERI_PPU_PR_ATT0_ENABLE_ALL_MASK      (PERI_PPU_PR_ATT0_UR_Msk | \
72                                                     PERI_PPU_PR_ATT0_UW_Msk | \
73                                                     PERI_PPU_PR_ATT0_UX_Msk | \
74                                                     PERI_PPU_PR_ATT0_PR_Msk | \
75                                                     PERI_PPU_PR_ATT0_PW_Msk | \
76                                                     PERI_PPU_PR_ATT0_PX_Msk | \
77                                                     PERI_PPU_PR_ATT0_NS_Msk)
78 
79 #define PROT_PERI_PPU_PROG_PC1_PC3_MASK            (0xffffffe0UL)  /* PC1 - PC3 bits mask */
80 
81 /*******************************************************************************
82 * Function Name: Cy_Prot_ConfigBusMaster
83 ****************************************************************************//**
84 *
85 * Configures the allowed protection contexts, security (secure/non-secure)
86 * and privilege level of the bus transaction created by the specified master.
87 *
88 * \note This function is applicable for both CPUSS ver_1 and ver_2.
89 *
90 * \param busMaster
91 * Indicates which master needs to be configured. Refer to the CPUSS_MS_ID_X
92 * defines in the device config header file.
93 *
94 * \param privileged
95 * Boolean to define the privilege level of all subsequent bus transfers.
96 * True - privileged, False - not privileged.
97 * Note that this is an inherited value. If not inherited, then this bit will
98 * be used.
99 *
100 * \param secure
101 * Security setting for the master. True - Secure, False - Not secure.
102 *
103 * \param pcMask
104 * This is a 16 bit value of the allowed contexts, it is an OR'ed (|) field of the
105 * provided defines in cy_prot.h. For example: (CY_PROT_PCMASK1 | CY_PROT_PCMASK3 | CY_PROT_PCMASK4)
106 * \note The function accepts pcMask values from CY_PROT_PCMASK1 to CY_PROT_PCMASK15.
107 * But each device has its own number of available protection contexts.
108 * That number is defined by PERI_PC_NR in the config file.
109 *
110 * \return
111 * Status of the function call.
112 *
113 *   Status               | Description
114 *   ------------         | -----------
115 *   CY_PROT_SUCCESS      | The function completed successfully.
116 *   CY_PROT_FAILURE      | The resource is locked.
117 *
118 * \funcusage
119 * \snippet prot/snippet/main.c snippet_Cy_Prot_ConfigBusMaster
120 *
121 *******************************************************************************/
Cy_Prot_ConfigBusMaster(en_prot_master_t busMaster,bool privileged,bool secure,uint32_t pcMask)122 cy_en_prot_status_t Cy_Prot_ConfigBusMaster(en_prot_master_t busMaster, bool privileged, bool secure, uint32_t pcMask)
123 {
124     cy_en_prot_status_t status = CY_PROT_SUCCESS;
125     uint32_t regVal;
126     volatile uint32_t *addrMsCtl; /* addrMsCtl is pointer to a register that is volatile by
127                                    * nature as can be changed outside of firmware control.
128                                    */
129 
130     CY_ASSERT_L1(CY_PROT_IS_BUS_MASTER_VALID(busMaster));
131     CY_ASSERT_L2(CY_PROT_IS_PC_MASK_VALID(pcMask));
132 
133     /* Get the address of Master x protection context control register (MSx_CTL) */
134     addrMsCtl = (uint32_t *)(CY_PROT_BASE + (uint32_t)((uint32_t)busMaster << CY_PROT_MSX_CTL_SHIFT));
135 
136     /* Get bitfields for MSx_CTL */
137     regVal = _VAL2FLD(PROT_SMPU_MS0_CTL_NS, !secure)   /* Security setting */
138             | _VAL2FLD(PROT_SMPU_MS0_CTL_P, privileged) /* Privileged setting */
139             | _VAL2FLD(PROT_SMPU_MS0_CTL_PC_MASK_15_TO_1, pcMask); /* Protection context mask */
140 
141     /* Set the value of MSx_CTL */
142     *addrMsCtl = regVal;
143 
144     /* Check if the MSx_CTL register is successfully updated with the new register value.
145      * The register will not be updated for the invalid master-protection context.
146      */
147     status = (*addrMsCtl != regVal) ? CY_PROT_FAILURE : CY_PROT_SUCCESS;
148 
149     return status;
150 }
151 
152 
153 /*******************************************************************************
154 * Function Name: Cy_Prot_SetActivePC
155 ****************************************************************************//**
156 *
157 * Sets the current/active protection context of the specified bus master.
158 *
159 * Allowed PC values are 1-15. If this value is not inherited from another bus
160 * master, the value set through this function is used.
161 *
162 * \note This function is applicable for both CPUSS ver_1 and ver_2.
163 *
164 * \note The function does not have access and returns an error for the secure
165 * CYB06xx7 devices.
166 *
167 * \param busMaster
168 * The bus master to configure. Refer to the CPUSS_MS_ID_X defines in the device
169 * config header file.
170 *
171 * \param pc
172 * Active protection context of the specified master \ref cy_en_prot_pc_t.
173 * \note that only those protection contexts allowed by the pcMask (which was
174 * configured in \ref Cy_Prot_ConfigBusMaster) will take effect.
175 * \note The function accepts pcMask values from CY_PROT_PC1 to CY_PROT_PC15.
176 * But each device has its own number of available protection contexts.
177 * That number is defined by PERI_PC_NR in the config file.
178 *
179 * \return
180 * Status of the function call.
181 *
182 *   Status               | Description
183 *   ------------         | -----------
184 *   CY_PROT_SUCCESS      | The function completed successfully.
185 *   CY_PROT_FAILURE      | The resource is locked.
186 *
187 * \funcusage
188 * \snippet prot/snippet/main.c snippet_Cy_Prot_SetActivePC
189 *
190 *******************************************************************************/
Cy_Prot_SetActivePC(en_prot_master_t busMaster,uint32_t pc)191 cy_en_prot_status_t Cy_Prot_SetActivePC(en_prot_master_t busMaster, uint32_t pc)
192 {
193     cy_en_prot_status_t status;
194 
195     CY_ASSERT_L1(CY_PROT_IS_BUS_MASTER_VALID(busMaster));
196     CY_ASSERT_L2(CY_PROT_IS_PC_VALID(pc));
197 
198     #if CY_CPU_CORTEX_M4 && defined(CY_DEVICE_SECURE) && defined(CY_DEVICE_PSOC6ABLE2)
199         status = CY_PROT_UNAVAILABLE;
200         CY_UNUSED_PARAMETER(busMaster);
201         CY_UNUSED_PARAMETER(pc);
202     #else
203         PROT_MPU_MS_CTL(busMaster) = _VAL2FLD(PROT_MPU_MS_CTL_PC, pc) | _VAL2FLD(PROT_MPU_MS_CTL_PC_SAVED, pc);
204         status = (_FLD2VAL(PROT_MPU_MS_CTL_PC, PROT_MPU_MS_CTL(busMaster)) != pc) ? CY_PROT_FAILURE : CY_PROT_SUCCESS;
205     #endif
206 
207     return status;
208 }
209 
210 
211 /*******************************************************************************
212 * Function Name: Cy_Prot_GetActivePC
213 ****************************************************************************//**
214 *
215 * \note This function is applicable for both CPUSS ver_1 and ver_2.
216 *
217 * Returns the active protection context of a master.
218 *
219 * \param busMaster
220 * The bus master, whose protection context is being read. Refer to the
221 * CPUSS_MS_ID_X defines in the device config header file.
222 *
223 * \return
224 * Active protection context of the master \ref cy_en_prot_pc_t.
225 *
226 * \funcusage
227 * \snippet prot/snippet/main.c snippet_Cy_Prot_SetActivePC
228 *
229 *******************************************************************************/
Cy_Prot_GetActivePC(en_prot_master_t busMaster)230 uint32_t Cy_Prot_GetActivePC(en_prot_master_t busMaster)
231 {
232     CY_ASSERT_L1(CY_PROT_IS_BUS_MASTER_VALID(busMaster));
233 
234     #if CY_CPU_CORTEX_M4 && defined(CY_DEVICE_SECURE) && defined(CY_DEVICE_PSOC6ABLE2)
235         return ((uint32_t)_FLD2VAL(PROT_MPU_MS_CTL_PC, CY_PRA_REG32_GET((CY_PRA_INDX_PROT_MPU_MS_CTL + (uint16_t) busMaster))));
236     #else
237         return ((uint32_t)_FLD2VAL(PROT_MPU_MS_CTL_PC, PROT_MPU_MS_CTL(busMaster)));
238     #endif
239 }
240 
241 
242 /*******************************************************************************
243 * Function Name: Cy_Prot_ConfigMpuStruct
244 ****************************************************************************//**
245 *
246 * This function configures a memory protection unit (MPU) struct with its
247 * protection attributes.
248 *
249 * The protection structs act like the gatekeepers for a master's accesses to
250 * memory, allowing only the permitted transactions to go through.
251 *
252 * \note This function is applicable for both CPUSS ver_1 and ver_2.
253 *
254 * \param base
255 * The base address for the MPU struct being configured.
256 *
257 * \param config
258 * Initialization structure containing all the protection attributes.
259 *
260 * \return
261 * Status of the function call.
262 *
263 *   Status               | Description
264 *   ------------         | -----------
265 *   CY_PROT_SUCCESS      | The MPU struct was configured.
266 *   CY_PROT_FAILURE      | Configuration failed due to a protection violation.
267 *
268 * \funcusage
269 * \snippet prot/snippet/main.c snippet_Cy_Prot_ConfigMpuStruct
270 *
271 *******************************************************************************/
Cy_Prot_ConfigMpuStruct(PROT_MPU_MPU_STRUCT_Type * base,const cy_stc_mpu_cfg_t * config)272 cy_en_prot_status_t Cy_Prot_ConfigMpuStruct(PROT_MPU_MPU_STRUCT_Type* base, const cy_stc_mpu_cfg_t* config)
273 {
274     cy_en_prot_status_t status;
275     uint32_t addrReg;
276     uint32_t attReg;
277 
278     CY_ASSERT_L1(NULL != base);
279     CY_ASSERT_L3(CY_PROT_IS_MPU_PERM_VALID(config->userPermission));
280     CY_ASSERT_L3(CY_PROT_IS_MPU_PERM_VALID(config->privPermission));
281     CY_ASSERT_L3(CY_PROT_IS_REGION_SIZE_VALID(config->regionSize));
282 
283     addrReg = _VAL2FLD(PROT_MPU_MPU_STRUCT_ADDR_SUBREGION_DISABLE, config->subregions)
284               | _VAL2FLD(PROT_MPU_MPU_STRUCT_ADDR_ADDR24, (uint32_t)((uint32_t)config->address >> CY_PROT_ADDR_SHIFT));
285     attReg  = ((uint32_t)config->userPermission & CY_PROT_ATT_PERMISSION_MASK)
286               | (((uint32_t)config->privPermission & CY_PROT_ATT_PERMISSION_MASK) << CY_PROT_ATT_PRIV_PERMISSION_SHIFT)
287               | _VAL2FLD(PROT_MPU_MPU_STRUCT_ATT_NS, !(config->secure))
288               | _VAL2FLD(PROT_MPU_MPU_STRUCT_ATT_REGION_SIZE, config->regionSize);
289     PROT_MPU_MPU_STRUCT_ATT(base) = attReg;
290     PROT_MPU_MPU_STRUCT_ADDR(base) = addrReg;
291     status = ((PROT_MPU_MPU_STRUCT_ADDR(base) != addrReg) || (PROT_MPU_MPU_STRUCT_ATT(base) != attReg)) ? CY_PROT_FAILURE : CY_PROT_SUCCESS;
292 
293     return status;
294 }
295 
296 
297 /*******************************************************************************
298 * Function Name: Cy_Prot_EnableMpuStruct
299 ****************************************************************************//**
300 *
301 * Enables the MPU struct, which allows the MPU protection attributes to
302 * take effect.
303 *
304 * \note This function is applicable for both CPUSS ver_1 and ver_2.
305 *
306 * \param base
307 * The base address of the MPU struct being configured.
308 *
309 * \return
310 * Status of the function call.
311 *
312 *   Status               | Description
313 *   ------------         | -----------
314 *   CY_PROT_SUCCESS      | The MPU struct was enabled.
315 *   CY_PROT_FAILURE      | The MPU struct is disabled and possibly locked.
316 *
317 * \funcusage
318 * \snippet prot/snippet/main.c snippet_Cy_Prot_EnableMpuStruct
319 *
320 *******************************************************************************/
Cy_Prot_EnableMpuStruct(PROT_MPU_MPU_STRUCT_Type * base)321 cy_en_prot_status_t Cy_Prot_EnableMpuStruct(PROT_MPU_MPU_STRUCT_Type* base)
322 {
323     cy_en_prot_status_t status;
324 
325     CY_ASSERT_L1(NULL != base);
326 
327     PROT_MPU_MPU_STRUCT_ATT(base) |= _VAL2FLD(PROT_MPU_MPU_STRUCT_ATT_ENABLED, CY_PROT_STRUCT_ENABLE);
328     status = (_FLD2VAL(PROT_MPU_MPU_STRUCT_ATT_ENABLED, PROT_MPU_MPU_STRUCT_ATT(base)) != CY_PROT_STRUCT_ENABLE) ?
329                 CY_PROT_FAILURE : CY_PROT_SUCCESS;
330 
331     return status;
332 }
333 
334 
335 /*******************************************************************************
336 * Function Name: Cy_Prot_DisableMpuStruct
337 ****************************************************************************//**
338 *
339 * Disables the MPU struct, which prevents the MPU protection attributes
340 * from taking effect.
341 *
342 * \note This function is applicable for both CPUSS ver_1 and ver_2.
343 *
344 * \param base
345 * The base address of the MPU struct being configured.
346 *
347 * \return
348 * Status of the function call.
349 *
350 *   Status               | Description
351 *   ------------         | -----------
352 *   CY_PROT_SUCCESS      | The MPU struct was disabled.
353 *   CY_PROT_FAILURE      | The MPU struct is enabled and possibly locked.
354 *
355 * \funcusage
356 * \snippet prot/snippet/main.c snippet_Cy_Prot_DisableMpuStruct
357 *
358 *******************************************************************************/
Cy_Prot_DisableMpuStruct(PROT_MPU_MPU_STRUCT_Type * base)359 cy_en_prot_status_t Cy_Prot_DisableMpuStruct(PROT_MPU_MPU_STRUCT_Type* base)
360 {
361     cy_en_prot_status_t status;
362 
363     CY_ASSERT_L1(NULL != base);
364 
365     PROT_MPU_MPU_STRUCT_ATT(base) &= ~_VAL2FLD(PROT_MPU_MPU_STRUCT_ATT_ENABLED, CY_PROT_STRUCT_ENABLE);
366     status = (_FLD2VAL(PROT_MPU_MPU_STRUCT_ATT_ENABLED, PROT_MPU_MPU_STRUCT_ATT(base)) == CY_PROT_STRUCT_ENABLE) ?
367                CY_PROT_FAILURE : CY_PROT_SUCCESS;
368 
369     return status;
370 }
371 
372 
373 /*******************************************************************************
374 * Function Name: Cy_Prot_ConfigSmpuMasterStruct
375 ****************************************************************************//**
376 *
377 * Configures a Shared Memory Protection Unit (SMPU) master protection
378 * struct with its protection attributes.
379 *
380 * This function configures the master struct governing the corresponding slave
381 * struct pair. It is a mechanism to protect the slave SMPU struct. Since the
382 * memory location of the slave struct is known, the address, regionSize and
383 * subregions of the configuration struct are not applicable.
384 *
385 * Note that only the user/privileged write permissions are configurable. The
386 * read and execute permissions are read-only and cannot be configured.
387 *
388 * \note This function is applicable for both CPUSS ver_1 and ver_2.
389 *
390 * \param base
391 * The register base address of the protection struct being configured.
392 *
393 * \param config
394 * Initialization structure with all the protection attributes.
395 *
396 * \return
397 * Status of the function call.
398 *
399 *   Status               | Description
400 *   ------------         | -----------
401 *   CY_PROT_SUCCESS      | SMPU master struct was successfully configured.
402 *   CY_PROT_FAILURE      | The resource is locked.
403 *   CY_PROT_BAD_PARAM    | An incorrect/invalid parameter was passed.
404 *
405 * \funcusage
406 * \snippet prot/snippet/main.c snippet_Cy_Prot_ConfigSmpuMasterStruct
407 *
408 *******************************************************************************/
Cy_Prot_ConfigSmpuMasterStruct(PROT_SMPU_SMPU_STRUCT_Type * base,const cy_stc_smpu_cfg_t * config)409 cy_en_prot_status_t Cy_Prot_ConfigSmpuMasterStruct(PROT_SMPU_SMPU_STRUCT_Type* base, const cy_stc_smpu_cfg_t* config)
410 {
411     cy_en_prot_status_t status;
412     uint32_t attReg;
413 
414     CY_ASSERT_L1(NULL != base);
415     CY_ASSERT_L2(CY_PROT_IS_PC_MASK_VALID(config->pcMask));
416     CY_ASSERT_L3(CY_PROT_IS_SMPU_MS_PERM_VALID(config->userPermission));
417     CY_ASSERT_L3(CY_PROT_IS_SMPU_MS_PERM_VALID(config->privPermission));
418 
419     if(((uint32_t)config->pcMask & CY_PROT_SMPU_PC_LIMIT_MASK) != 0UL)
420     {
421         /* PC mask out of range - not supported in device */
422         status = CY_PROT_BAD_PARAM;
423     }
424     else
425     {
426         /* ADDR1 is read only. Only configure ATT1 */
427         attReg = ((uint32_t)config->userPermission & CY_PROT_ATT_PERMISSION_MASK)
428                     | (uint32_t)(((uint32_t)config->privPermission & CY_PROT_ATT_PERMISSION_MASK) << CY_PROT_ATT_PRIV_PERMISSION_SHIFT)
429                     | _VAL2FLD(PROT_SMPU_SMPU_STRUCT_ATT1_NS, !(config->secure))
430                     | _VAL2FLD(PROT_SMPU_SMPU_STRUCT_ATT1_PC_MASK_15_TO_1, config->pcMask)
431                     /* No region size - read only for master structs */
432                     | _VAL2FLD(PROT_SMPU_SMPU_STRUCT_ATT1_PC_MATCH, config->pcMatch);
433         if ((attReg & CY_PROT_SMPU_ATT1_MASK) != attReg)
434         {
435             /* Invalid parameter was passed */
436             status = CY_PROT_BAD_PARAM;
437         }
438         else
439         {
440             PROT_SMPU_SMPU_STRUCT_ATT1(base) = attReg;
441             status = ((PROT_SMPU_SMPU_STRUCT_ATT1(base) & CY_PROT_SMPU_ATT1_MASK) != attReg) ? CY_PROT_FAILURE : CY_PROT_SUCCESS;
442         }
443     }
444 
445     return status;
446 }
447 
448 
449 /*******************************************************************************
450 * Function Name: Cy_Prot_ConfigSmpuSlaveStruct
451 ****************************************************************************//**
452 *
453 * Configures a Shared Memory Protection Unit (SMPU) slave protection
454 * struct with its protection attributes.
455 *
456 * This function configures the slave struct of an SMPU pair, which can protect
457 * any memory region in a device from invalid bus master accesses.
458 *
459 * \note This function is applicable for both CPUSS ver_1 and ver_2.
460 *
461 * \param base
462 * The register base address of the protection structure being configured.
463 *
464 * \param config
465 * Initialization structure with all the protection attributes.
466 *
467 * \return
468 * Status of the function call.
469 *
470 *   Status               | Description
471 *   ------------         | -----------
472 *   CY_PROT_SUCCESS      | SMPU slave struct was successfully configured.
473 *   CY_PROT_FAILURE      | The resource is locked.
474 *   CY_PROT_BAD_PARAM    | An incorrect/invalid parameter was passed.
475 *
476 * \funcusage
477 * \snippet prot/snippet/main.c snippet_Cy_Prot_ConfigSmpuSlaveStruct
478 *
479 *******************************************************************************/
Cy_Prot_ConfigSmpuSlaveStruct(PROT_SMPU_SMPU_STRUCT_Type * base,const cy_stc_smpu_cfg_t * config)480 cy_en_prot_status_t Cy_Prot_ConfigSmpuSlaveStruct(PROT_SMPU_SMPU_STRUCT_Type* base, const cy_stc_smpu_cfg_t* config)
481 {
482     cy_en_prot_status_t status = CY_PROT_SUCCESS;
483     uint32_t addrReg;
484     uint32_t attReg;
485 
486     CY_ASSERT_L1(NULL != base);
487     CY_ASSERT_L2(CY_PROT_IS_PC_MASK_VALID(config->pcMask));
488     CY_ASSERT_L3(CY_PROT_IS_SMPU_SL_PERM_VALID(config->userPermission));
489     CY_ASSERT_L3(CY_PROT_IS_SMPU_SL_PERM_VALID(config->privPermission));
490     CY_ASSERT_L3(CY_PROT_IS_REGION_SIZE_VALID(config->regionSize));
491 
492     if(((uint32_t)config->pcMask & CY_PROT_SMPU_PC_LIMIT_MASK) != 0UL)
493     {
494         /* PC mask out of range - not supported in device */
495         status = CY_PROT_BAD_PARAM;
496     }
497     else
498     {
499         addrReg= _VAL2FLD(PROT_SMPU_SMPU_STRUCT_ADDR0_SUBREGION_DISABLE, config->subregions)
500                     | _VAL2FLD(PROT_SMPU_SMPU_STRUCT_ADDR0_ADDR24, (uint32_t)((uint32_t)config->address >> CY_PROT_ADDR_SHIFT));
501         attReg= ((uint32_t)config->userPermission & CY_PROT_ATT_PERMISSION_MASK)
502                     | (uint32_t)(((uint32_t)config->privPermission & CY_PROT_ATT_PERMISSION_MASK) << CY_PROT_ATT_PRIV_PERMISSION_SHIFT)
503                     | _VAL2FLD(PROT_SMPU_SMPU_STRUCT_ATT0_NS, !(config->secure))
504                     | _VAL2FLD(PROT_SMPU_SMPU_STRUCT_ATT0_PC_MASK_15_TO_1, config->pcMask)
505                     | _VAL2FLD(PROT_SMPU_SMPU_STRUCT_ATT0_REGION_SIZE, config->regionSize)
506                     | _VAL2FLD(PROT_SMPU_SMPU_STRUCT_ATT0_PC_MATCH, config->pcMatch);
507         PROT_SMPU_SMPU_STRUCT_ATT0(base) = attReg;
508         PROT_SMPU_SMPU_STRUCT_ADDR0(base) = addrReg;
509         status = ((PROT_SMPU_SMPU_STRUCT_ADDR0(base) != addrReg) || ((PROT_SMPU_SMPU_STRUCT_ATT0(base) & CY_PROT_SMPU_ATT0_MASK) != attReg))
510             ? CY_PROT_FAILURE : CY_PROT_SUCCESS;
511     }
512 
513     return status;
514 }
515 
516 
517 /*******************************************************************************
518 * Function Name: Cy_Prot_EnableSmpuMasterStruct
519 ****************************************************************************//**
520 *
521 * Enables the Master SMPU structure.
522 *
523 * This is an SMPU master struct enable function. The SMPU protection settings
524 * will take effect after successful completion of this function call.
525 *
526 * \note This function is applicable for both CPUSS ver_1 and ver_2.
527 *
528 * \param base
529 * The base address for the protection unit structure being configured.
530 *
531 * \return
532 * Status of the function call.
533 *
534 *   Status               | Description
535 *   ------------         | -----------
536 *   CY_PROT_SUCCESS      | The Master PU struct was enabled.
537 *   CY_PROT_FAILURE      | The Master PU struct is disabled and possibly locked.
538 *
539 * \funcusage
540 * \snippet prot/snippet/main.c snippet_Cy_Prot_EnableSmpuMasterStruct
541 *
542 *******************************************************************************/
Cy_Prot_EnableSmpuMasterStruct(PROT_SMPU_SMPU_STRUCT_Type * base)543 cy_en_prot_status_t Cy_Prot_EnableSmpuMasterStruct(PROT_SMPU_SMPU_STRUCT_Type* base)
544 {
545     cy_en_prot_status_t status;
546 
547     CY_ASSERT_L1(NULL != base);
548 
549     PROT_SMPU_SMPU_STRUCT_ATT1(base) |= _VAL2FLD(PROT_SMPU_SMPU_STRUCT_ATT1_ENABLED, CY_PROT_STRUCT_ENABLE);
550     status = (_FLD2VAL(PROT_SMPU_SMPU_STRUCT_ATT1_ENABLED, PROT_SMPU_SMPU_STRUCT_ATT1(base)) != CY_PROT_STRUCT_ENABLE) ?
551                 CY_PROT_FAILURE : CY_PROT_SUCCESS;
552 
553     return status;
554 }
555 
556 
557 /*******************************************************************************
558 * Function Name: Cy_Prot_DisableSmpuMasterStruct
559 ****************************************************************************//**
560 *
561 * Disables the Master SMPU structure.
562 *
563 * This is an SMPU master struct disable function. The SMPU protection settings
564 * will seize to take effect after successful completion of this function call.
565 *
566 * \note This function is applicable for both CPUSS ver_1 and ver_2.
567 *
568 * \param base
569 * The base address for the protection unit structure being configured.
570 *
571 * \return
572 * Status of the function call.
573 *
574 *   Status               | Description
575 *   ------------         | -----------
576 *   CY_PROT_SUCCESS      | The Master PU struct was disabled.
577 *   CY_PROT_FAILURE      | The Master PU struct is enabled and possibly locked.
578 *
579 * \funcusage
580 * \snippet prot/snippet/main.c snippet_Cy_Prot_DisableSmpuMasterStruct
581 *
582 *******************************************************************************/
Cy_Prot_DisableSmpuMasterStruct(PROT_SMPU_SMPU_STRUCT_Type * base)583 cy_en_prot_status_t Cy_Prot_DisableSmpuMasterStruct(PROT_SMPU_SMPU_STRUCT_Type* base)
584 {
585     cy_en_prot_status_t status;
586 
587     CY_ASSERT_L1(NULL != base);
588 
589     PROT_SMPU_SMPU_STRUCT_ATT1(base) &= ~_VAL2FLD(PROT_SMPU_SMPU_STRUCT_ATT1_ENABLED, CY_PROT_STRUCT_ENABLE);
590     status = (_FLD2VAL(PROT_SMPU_SMPU_STRUCT_ATT1_ENABLED, PROT_SMPU_SMPU_STRUCT_ATT1(base)) == CY_PROT_STRUCT_ENABLE) ?
591                 CY_PROT_FAILURE : CY_PROT_SUCCESS;
592 
593     return status;
594 }
595 
596 
597 /*******************************************************************************
598 * Function Name: Cy_Prot_EnableSmpuSlaveStruct
599 ****************************************************************************//**
600 *
601 * Enables the Slave SMPU structure.
602 *
603 * This is an SMPU slave struct enable function. The SMPU protection settings
604 * will take effect after successful completion of this function call.
605 *
606 * \note This function is applicable for both CPUSS ver_1 and ver_2.
607 *
608 * \param base
609 * The base address for the protection unit structure being configured.
610 *
611 * \return
612 * Status of the function call.
613 *
614 *   Status               | Description
615 *   ------------         | -----------
616 *   CY_PROT_SUCCESS      | The Slave PU struct was enabled.
617 *   CY_PROT_FAILURE      | The Slave PU struct is disabled and possibly locked.
618 *
619 * \funcusage
620 * \snippet prot/snippet/main.c snippet_Cy_Prot_EnableSmpuSlaveStruct
621 *
622 *******************************************************************************/
Cy_Prot_EnableSmpuSlaveStruct(PROT_SMPU_SMPU_STRUCT_Type * base)623 cy_en_prot_status_t Cy_Prot_EnableSmpuSlaveStruct(PROT_SMPU_SMPU_STRUCT_Type* base)
624 {
625     cy_en_prot_status_t status;
626 
627     CY_ASSERT_L1(NULL != base);
628 
629     PROT_SMPU_SMPU_STRUCT_ATT0(base) |= _VAL2FLD(PROT_SMPU_SMPU_STRUCT_ATT0_ENABLED, CY_PROT_STRUCT_ENABLE);
630     status = (_FLD2VAL(PROT_SMPU_SMPU_STRUCT_ATT0_ENABLED, PROT_SMPU_SMPU_STRUCT_ATT0(base)) != CY_PROT_STRUCT_ENABLE) ?
631                 CY_PROT_FAILURE : CY_PROT_SUCCESS;
632 
633     return status;
634 }
635 
636 
637 /*******************************************************************************
638 * Function Name: Cy_Prot_DisableSmpuSlaveStruct
639 ****************************************************************************//**
640 *
641 * Disables the Slave SMPU structure.
642 *
643 * This is an SMPU slave struct disable function. The SMPU protection settings
644 * will seize to take effect after successful completion of this function call.
645 *
646 * \note This function is applicable for both CPUSS ver_1 and ver_2.
647 *
648 * \param base
649 * The base address for the protection unit structure being configured.
650 *
651 * \return
652 * Status of the function call.
653 *
654 *   Status               | Description
655 *   ------------         | -----------
656 *   CY_PROT_SUCCESS      | The Slave PU struct was disabled.
657 *   CY_PROT_FAILURE      | The Slave PU struct is enabled and possibly locked.
658 *
659 * \funcusage
660 * \snippet prot/snippet/main.c snippet_Cy_Prot_DisableSmpuSlaveStruct
661 *
662 *******************************************************************************/
Cy_Prot_DisableSmpuSlaveStruct(PROT_SMPU_SMPU_STRUCT_Type * base)663 cy_en_prot_status_t Cy_Prot_DisableSmpuSlaveStruct(PROT_SMPU_SMPU_STRUCT_Type* base)
664 {
665     cy_en_prot_status_t status;
666 
667     CY_ASSERT_L1(NULL != base);
668 
669     PROT_SMPU_SMPU_STRUCT_ATT0(base) &= ~_VAL2FLD(PROT_SMPU_SMPU_STRUCT_ATT0_ENABLED, CY_PROT_STRUCT_ENABLE);
670     status = (_FLD2VAL(PROT_SMPU_SMPU_STRUCT_ATT0_ENABLED, PROT_SMPU_SMPU_STRUCT_ATT0(base)) == CY_PROT_STRUCT_ENABLE) ?
671                 CY_PROT_FAILURE : CY_PROT_SUCCESS;
672 
673     return status;
674 }
675 
676 
677 /*******************************************************************************
678 * Function Name: Cy_Prot_GetSmpuStruct
679 ****************************************************************************//**
680 *
681 * Functions returns a pointer of the requested unused SMPU structure. It
682 * searches the SMPU structures until it finds one that both the slave and master
683 * sections are disabled. After an available structure is located, function
684 * enables the slave structure and set the ATT0[7:0] bits to 0xFF, to make sure
685 * that a subsequent call will not see this as an available (unused) SMPU.
686 *
687 * It is up to the user to implement, if needed, a system in which a semaphore
688 * will lock-out all but one CPU from calling this function at once.
689 *
690 * \note This function is applicable for both CPUSS ver_1 and ver_2.
691 *
692 * \param base
693 * The base address for the SMPU structure returned if an unused structure was
694 * found. If an empty structure was not found, the returned pointer is NULL.
695 *
696 * \param reqMode
697 * This parameter (request mode) selects how the user wants to select a SMPU
698 * structure.
699 *
700 *   reqMode                 | Description
701 * --------------------------| -----------------------------
702 * CY_PROT_REQMODE_HIGHPRIOR | Return the SMPU structure with the highest priority.
703 * CY_PROT_REQMODE_LOWPRIOR  | Return the SMPU structure with the lowest priority.
704 * CY_PROT_REQMODE_INDEX     | Return the SMPU structure with the specific index.
705 *
706 * \param smpuIndex
707 * This is the index of the requested SMPU structure. It is only used if the
708 * request mode is reqMode = CY_PROT_REQMODE_INDEX.
709 *
710 * \return
711 * Status of the function call.
712 *
713 *   Status              | Description
714 * ----------------------| ---------------------------------------
715 * CY_PROT_SUCCESS       | The Slave PU struct was disabled.
716 * CY_PROT_FAILURE       | The Master or Slave SMPU struct is disabled and possibly locked.
717 * CY_PROT_UNAVAILABLE   | The requested structure in use or there were no unused structures.
718 *
719 * \funcusage
720 * \snippet prot/snippet/main.c snippet_Cy_Prot_GetSmpuStruct
721 *
722 *******************************************************************************/
Cy_Prot_GetSmpuStruct(PROT_SMPU_SMPU_STRUCT_Type ** base,cy_en_prot_req_mode_t reqMode,uint32_t smpuIndex)723 cy_en_prot_status_t Cy_Prot_GetSmpuStruct(PROT_SMPU_SMPU_STRUCT_Type** base,
724                                           cy_en_prot_req_mode_t reqMode, uint32_t smpuIndex)
725 {
726     CY_ASSERT_L3(CY_PROT_IS_SMPU_REQ_MODE_VALID(reqMode));
727     CY_ASSERT_L2(CY_PROT_IS_SMPU_IDX_VALID(smpuIndex));
728 
729     cy_en_prot_status_t status = CY_PROT_UNAVAILABLE;
730     int32_t stcIdx = (int32_t)smpuIndex;
731 
732     *base = NULL;
733 
734     switch (reqMode)
735     {
736         /* The SMPU priority goes from PROT_SMPU_STRUCT_HIGHEST_PR
737         *  (highest priority) to 0 (lowest priority)
738         */
739         case CY_PROT_REQMODE_HIGHPRIOR:
740 
741             stcIdx = PROT_SMPU_STRUCT_WTH_HIGHEST_PR;
742             do
743             {
744                 if (Prot_IsSmpuStructDisabled((uint32_t)stcIdx))
745                 {
746                     status = CY_PROT_SUCCESS;
747                 }
748                 else
749                 {
750                     --stcIdx;
751                 }
752             } while ((stcIdx >= 0) && (CY_PROT_SUCCESS != status));
753             break;
754 
755         case CY_PROT_REQMODE_LOWPRIOR:
756             stcIdx = 0;
757             do
758             {
759                 if (Prot_IsSmpuStructDisabled((uint32_t)stcIdx))
760                 {
761                     status = CY_PROT_SUCCESS;
762                 }
763                 else
764                 {
765                     ++stcIdx;
766                 }
767             } while ((stcIdx <= PROT_SMPU_STRUCT_WTH_HIGHEST_PR) && (CY_PROT_SUCCESS != status));
768             break;
769 
770         case CY_PROT_REQMODE_INDEX:
771             if (Prot_IsSmpuStructDisabled((uint32_t)stcIdx))
772             {
773                 status = CY_PROT_SUCCESS;
774             }
775             break;
776 
777         default:
778             /* Unknown request mode */
779             break;
780     }
781 
782     /* Proceed if required structure is found */
783     if (CY_PROT_SUCCESS == status)
784     {
785         /* Enable Slave SMPU struct */
786         PROT_SMPU_SMPU_STRUCT_IDX_ATT0(stcIdx) |= _VAL2FLD(PROT_SMPU_SMPU_STRUCT_ATT0_ENABLED, CY_PROT_STRUCT_ENABLE);
787 
788         status =
789         (_FLD2VAL(PROT_SMPU_SMPU_STRUCT_ATT0_ENABLED, PROT_SMPU_SMPU_STRUCT_IDX_ATT0(stcIdx)) != CY_PROT_STRUCT_ENABLE) ?
790         CY_PROT_FAILURE : CY_PROT_SUCCESS;
791 
792         /* Enable all attributes only if Slave struct was enabled */
793         if (CY_PROT_SUCCESS == status)
794         {
795             *base = (PROT_SMPU_SMPU_STRUCT_Type* ) PROT_SMPU_SMPU_STRUCT_IDX(stcIdx);
796             PROT_SMPU_SMPU_STRUCT_IDX_ATT0(stcIdx) |= PROT_SMPU_STRUCT_ATT0_ENABLE_ALL_MASK;
797         }
798     }
799     return status;
800 }
801 
802 #if (defined (CY_IP_MXPERI_VERSION) && (CY_IP_MXPERI_VERSION > 1U)) || defined (CY_DOXYGEN)
803 /*******************************************************************************
804 * Function Name: Prot_ConfigPpuAtt
805 ********************************************************************************
806 *
807 * An internal function to hold the common code for
808 * Cy_Prot_ConfigPpu[Prog/Fixed][Master/Slave]Att API functions
809 *
810 * \note This function is applicable for CPUSS ver_2 only.
811 *
812 * \param reg
813 * The register to update attributes in.
814 *
815 * \param pcMask
816 * The protection context mask. It specifies the protection context or a set of
817 * multiple protection contexts to be configured.
818 * It is a value of OR'd (|) items of \ref cy_en_prot_pcmask_t.
819 * For example: (\ref CY_PROT_PCMASK1 | \ref CY_PROT_PCMASK3 | \ref CY_PROT_PCMASK4).
820 * \note The function accepts pcMask values from \ref CY_PROT_PCMASK1 to \ref CY_PROT_PCMASK15.
821 * But each device has its own number of available protection contexts.
822 * That number is defined by PERI_PC_NR in the config file.
823 *
824 * \param userPermission
825 * The user permissions for the region.
826 *
827 * \param privPermission
828 * The privileged permissions for the region.
829 *
830 * \param secure
831 * Non Secure = false, Secure = true
832 *
833 * \return
834 * The status of the function call.
835 *
836 *   Status              | Description
837 * ----------------------| ---------------------------------------
838 * CY_PROT_SUCCESS       | The attributes were set up.
839 * CY_PROT_FAILURE       | The attributes were not set up because the structure is possibly locked.
840 * CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
841 *
842 *******************************************************************************/
Prot_ConfigPpuAtt(volatile uint32_t * reg,uint16_t pcMask,cy_en_prot_perm_t userPermission,cy_en_prot_perm_t privPermission,bool secure)843 static cy_en_prot_status_t Prot_ConfigPpuAtt(volatile uint32_t * reg, uint16_t pcMask,
844                                        cy_en_prot_perm_t userPermission, cy_en_prot_perm_t privPermission, bool secure)
845 {
846     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
847 
848     CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 14.3','Intentional check for the macro CY_PERI_V1.');
849     if(CY_PERI_V1 == 0U)
850     {
851         uint32_t tmpMask = (uint32_t)pcMask << CY_PROT_PCMASK_CHECK;
852         uint32_t tmpMask2;
853         uint32_t attReg;
854         int32_t regIdx;
855         uint32_t fldIdx;
856 
857         status = CY_PROT_SUCCESS;
858 
859         /* Populate the ATT values */
860         /* Note that we have to start with the higher PCs to avoid locking ourselves out */
861         for(regIdx = ((int32_t)CY_PROT_ATT_REGS_MAX - (int32_t)1U); regIdx >= 0; regIdx--)
862         {
863             tmpMask2 = (tmpMask >> (CY_PROT_ATT_PC_MAX * (uint32_t)regIdx)) & 0xFUL;
864 
865             /* Get the attributes register value */
866             attReg = reg[regIdx];
867 
868             for(fldIdx = 0UL; fldIdx < CY_PROT_ATT_PC_MAX; fldIdx++)
869             {
870                 if((tmpMask2 & CY_PROT_PCMASK_CHECK) == CY_PROT_STRUCT_ENABLE)
871                 {
872                     /* Reset the bitfield for the PCx attributes */
873                     attReg &= ~((_VAL2FLD(CY_PROT_ATT_PERI_USER_PERM, CY_PROT_PERM_RW) |
874                                  _VAL2FLD(CY_PROT_ATT_PERI_PRIV_PERM, CY_PROT_PERM_RW) |
875                                  PERI_MS_PPU_PR_V2_MS_ATT0_PC0_NS_Msk) <<
876                                 (PERI_MS_PPU_PR_V2_MS_ATT0_PC1_UR_Pos * fldIdx));
877 
878                     /* Set the bitfield for the PCx attributes */
879                     attReg |= (_VAL2FLD(CY_PROT_ATT_PERI_USER_PERM, userPermission) |
880                                _VAL2FLD(CY_PROT_ATT_PERI_PRIV_PERM, privPermission) |
881                               _BOOL2FLD(PERI_MS_PPU_PR_V2_MS_ATT0_PC0_NS, !secure)) <<
882                                (PERI_MS_PPU_PR_V2_MS_ATT0_PC1_UR_Pos * fldIdx);
883                 }
884                 tmpMask2 = tmpMask2 >> CY_PROT_PCMASK_CHECK;
885             }
886 
887             /* Update the attributes register */
888             reg[regIdx] = attReg;
889 
890             /* Check the result */
891             if (0UL == (uint32_t)regIdx)
892             {
893                 if ((reg[regIdx] & PROT_PERI_PPU_PROG_PC1_PC3_MASK) != (attReg & PROT_PERI_PPU_PROG_PC1_PC3_MASK))
894                 {
895                     status = CY_PROT_FAILURE;
896                 }
897             }
898             else if (reg[regIdx] != attReg)
899             {
900                 status = CY_PROT_FAILURE;
901             }
902             else
903             {
904                 /* CY_PROT_SUCCESS */
905             }
906         }
907     }
908 
909     return status;
910 }
911 
912 /*******************************************************************************
913 * Function Name: Cy_Prot_ConfigPpuProgMasterAtt
914 ****************************************************************************//**
915 *
916 * Configures the protection-structure attributes of the
917 * Programmable Peripheral Protection Unit (PPU PROG) master.
918 *
919 * This function configures the master structure governing the corresponding slave
920 * structure pair. It is a mechanism to protect the slave PPU PROG structure.
921 * The memory location of the slave structure is known, so the address, regionSize, and
922 * sub-regions of the configuration structure are not applicable.
923 *
924 * \note This function is applicable for CPUSS ver_2 only.
925 *
926 * \param base
927 * The register base address of the protection structure is being configured.
928 *
929 * \param pcMask
930 * The protection context mask. It specifies the protection context or a set of
931 * multiple protection contexts to be configured.
932 * It is a value of OR'd (|) items of \ref cy_en_prot_pcmask_t.
933 * \note The function accepts pcMask values from \ref CY_PROT_PCMASK1 to
934 * \ref CY_PROT_PCMASK15. But each device has its own number of available protection
935 * contexts. That number is defined by PERI_PC_NR in the config file. If pcMask
936 * is passed beyond the range of available protection context, the bits above PERI_PC_NR
937 * will be ignored.
938 *
939 * \param userPermission
940 * The user permission setting. The CY_PROT_PERM_R or CY_PROT_PERM_RW values
941 * are valid for the master.
942 *
943 * \param privPermission
944 * The privileged permission setting. CY_PROT_PERM_R or CY_PROT_PERM_RW values
945 * are valid for the master.
946 *
947 * \param secure
948 * The secure flag.
949 *
950 * \return
951 * The status of the function call.
952 *
953 *   Status              | Description
954 * ----------------------| ---------------------------------------
955 * CY_PROT_SUCCESS       | The attributes were set up.
956 * CY_PROT_FAILURE       | The attributes were not set up because the structure is possibly locked.
957 * CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
958 *
959 * \note Only the user's/privileged Write permissions are configurable. The
960 * Read permissions are read-only and cannot be configured.
961 *
962 * \note PC0 accesses are read-only and are always enabled.
963 *
964 * \funcusage
965 * \snippet prot/snippet/main.c snippet_Cy_Prot_ConfigPpuProgMasterAtt
966 *
967 *******************************************************************************/
Cy_Prot_ConfigPpuProgMasterAtt(PERI_MS_PPU_PR_Type * base,uint16_t pcMask,cy_en_prot_perm_t userPermission,cy_en_prot_perm_t privPermission,bool secure)968 cy_en_prot_status_t Cy_Prot_ConfigPpuProgMasterAtt(PERI_MS_PPU_PR_Type* base, uint16_t pcMask,
969                                        cy_en_prot_perm_t userPermission, cy_en_prot_perm_t privPermission, bool secure)
970 {
971     /* The parameter checks */
972     CY_ASSERT_L1(NULL != base);
973     CY_ASSERT_L3(CY_PROT_IS_PROG_MS_PERM_VALID(userPermission));
974     CY_ASSERT_L3(CY_PROT_IS_PROG_MS_PERM_VALID(privPermission));
975 
976     pcMask &= ((uint16_t) CY_PROT_PCMASK_VALID);
977 
978     CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 18.1','Checked manually, base pointer will not exceed register range.');
979     return (Prot_ConfigPpuAtt(PERI_MS_PPU_PR_MS_ATT(base), pcMask, userPermission, privPermission, secure));
980 }
981 
982 /*******************************************************************************
983 * Function Name: Cy_Prot_ConfigPpuProgSlaveAddr
984 ****************************************************************************//**
985 *
986 * Configures the protection-structure address settings  of the
987 * Programmable Peripheral Protection Unit (PPU PROG) slave.
988 *
989 * This function configures the slave structure of the PPU PROG pair, which can
990 * protect any peripheral memory region in a device from an invalid bus-master
991 * access.
992 *
993 * \note This function is applicable for CPUSS ver_2 only.
994 *
995 * \param base
996 * The register base address of the protection structure is being configured.
997 *
998 * \param address
999 * The address.
1000 *
1001 * \param regionSize
1002 * The region size.
1003 *
1004 * \return
1005 * The status of the function call.
1006 *
1007 *   Status              | Description
1008 * ----------------------| ---------------------------------------
1009 * CY_PROT_SUCCESS       | The address settings were set up.
1010 * CY_PROT_FAILURE       | The address settings were not set up because the structure is possibly locked.
1011 * CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
1012 *
1013 * \note PC0 accesses are Read-only and are always enabled.
1014 *
1015 * \funcusage
1016 * \ref Cy_Prot_ConfigPpuProgSlaveAtt
1017 *
1018 *******************************************************************************/
Cy_Prot_ConfigPpuProgSlaveAddr(PERI_MS_PPU_PR_Type * base,uint32_t address,cy_en_prot_size_t regionSize)1019 cy_en_prot_status_t Cy_Prot_ConfigPpuProgSlaveAddr(PERI_MS_PPU_PR_Type* base, uint32_t address,
1020                                                                    cy_en_prot_size_t regionSize)
1021 {
1022     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
1023 
1024     /* The parameter checks */
1025     CY_ASSERT_L1(NULL != base);
1026     CY_ASSERT_L3(CY_PROT_IS_PPU_V2_SIZE_VALID(regionSize));
1027 
1028     PERI_MS_PPU_PR_SL_ADDR(base) = address & PERI_MS_PPU_PR_V2_SL_ADDR_ADDR30_Msk;
1029     PERI_MS_PPU_PR_SL_SIZE(base) = _CLR_SET_FLD32U((PERI_MS_PPU_PR_SL_SIZE(base)), PERI_MS_PPU_PR_V2_SL_SIZE_REGION_SIZE, regionSize);
1030 
1031     status = ((PERI_MS_PPU_PR_SL_ADDR(base) != (address & PERI_MS_PPU_PR_V2_SL_ADDR_ADDR30_Msk)) ||
1032                 (_FLD2VAL(PERI_MS_PPU_PR_V2_SL_SIZE_REGION_SIZE, PERI_MS_PPU_PR_SL_SIZE(base)) != (uint32_t)regionSize)) ?
1033                 CY_PROT_FAILURE : CY_PROT_SUCCESS;
1034 
1035     return status;
1036 }
1037 
1038 /*******************************************************************************
1039 * Function Name: Cy_Prot_ConfigPpuProgSlaveAtt
1040 ****************************************************************************//**
1041 *
1042 * Configures the protection structure with its protection attributes of the
1043 * Programmable Peripheral Protection Unit (PPU PROG) slave.
1044 *
1045 * This function configures the slave structure of the PPU PROG pair, which can
1046 * protect any peripheral memory region in a device from invalid bus-master
1047 * access.
1048 *
1049 * \note This function is applicable for CPUSS ver_2 only.
1050 *
1051 * \param base
1052 * The register base address of the protection structure is being configured.
1053 *
1054 * \param pcMask
1055 * The protection context mask. It specifies the protection context or a set of
1056 * multiple protection contexts to be configured.
1057 * It is a value of OR'd (|) items of \ref cy_en_prot_pcmask_t.
1058 * For example: (\ref CY_PROT_PCMASK1 | \ref CY_PROT_PCMASK3 | \ref CY_PROT_PCMASK4).
1059 * \note The function accepts pcMask values from \ref CY_PROT_PCMASK1 to \ref CY_PROT_PCMASK15.
1060 * But each device has its own number of available protection contexts.
1061 * That number is defined by PERI_PC_NR in the config file.
1062 *
1063 * \param userPermission
1064 * The user permission setting.
1065 *
1066 * \param privPermission
1067 * The privileged permission setting.
1068 *
1069 * \param secure
1070 * The secure flag.
1071 *
1072 * \return
1073 * The status of the function call.
1074 *
1075 *   Status              | Description
1076 * ----------------------| ---------------------------------------
1077 * CY_PROT_SUCCESS       | The attributes were set up.
1078 * CY_PROT_FAILURE       | The attributes were not set up because the structure is possibly locked.
1079 * CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
1080 *
1081 * \note PC0 accesses are read-only and are always enabled.
1082 *
1083 * \funcusage
1084 * \snippet prot/snippet/main.c snippet_Cy_Prot_ConfigPpuProgSlaveAtt
1085 *
1086 *******************************************************************************/
Cy_Prot_ConfigPpuProgSlaveAtt(PERI_MS_PPU_PR_Type * base,uint16_t pcMask,cy_en_prot_perm_t userPermission,cy_en_prot_perm_t privPermission,bool secure)1087 cy_en_prot_status_t Cy_Prot_ConfigPpuProgSlaveAtt(PERI_MS_PPU_PR_Type* base, uint16_t pcMask,
1088                                        cy_en_prot_perm_t userPermission, cy_en_prot_perm_t privPermission, bool secure)
1089 {
1090     /* The parameter checks */
1091     CY_ASSERT_L1(NULL != base);
1092     CY_ASSERT_L3(CY_PROT_IS_PROG_SL_PERM_VALID(userPermission));
1093     CY_ASSERT_L3(CY_PROT_IS_PROG_SL_PERM_VALID(privPermission));
1094 
1095     CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 18.1','Checked manually, base pointer will not exceed register range.');
1096     return (Prot_ConfigPpuAtt(PERI_MS_PPU_PR_SL_ATT(base), pcMask, userPermission, privPermission, secure));
1097 }
1098 
1099 /*******************************************************************************
1100 * Function Name: Cy_Prot_EnablePpuProgSlaveRegion
1101 ****************************************************************************//**
1102 *
1103 * Enables the Slave PPU PROG structure.
1104 *
1105 * This is the PPU PROG slave-structure enable function. The PPU PROG protection
1106 * settings will take effect after a successful completion of this function call.
1107 *
1108 * \note This function is applicable for CPUSS ver_2 only.
1109 *
1110 * \param base
1111 * The base address for the protection unit structure is being configured.
1112 *
1113 * \return
1114 * The status of the function call.
1115 *
1116 *   Status              | Description
1117 * ----------------------| ---------------------------------------
1118 * CY_PROT_SUCCESS       | The structure was enabled.
1119 * CY_PROT_FAILURE       | The structure is disabled and possibly locked.
1120 * CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
1121 *
1122 * \funcusage
1123 * \ref Cy_Prot_ConfigPpuProgSlaveAtt
1124 *
1125 *******************************************************************************/
Cy_Prot_EnablePpuProgSlaveRegion(PERI_MS_PPU_PR_Type * base)1126 cy_en_prot_status_t Cy_Prot_EnablePpuProgSlaveRegion(PERI_MS_PPU_PR_Type* base)
1127 {
1128     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
1129 
1130     CY_ASSERT_L1(NULL != base);
1131 
1132     PERI_MS_PPU_PR_SL_SIZE(base) =
1133     _CLR_SET_FLD32U((PERI_MS_PPU_PR_SL_SIZE(base)), PERI_MS_PPU_PR_V2_SL_SIZE_VALID, CY_PROT_STRUCT_ENABLE);
1134 
1135     status = (_FLD2VAL(PERI_MS_PPU_PR_V2_SL_SIZE_VALID, PERI_MS_PPU_PR_SL_SIZE(base)) != CY_PROT_STRUCT_ENABLE) ?
1136     CY_PROT_FAILURE : CY_PROT_SUCCESS;
1137 
1138     return status;
1139 }
1140 
1141 
1142 /*******************************************************************************
1143 * Function Name: Cy_Prot_DisablePpuProgSlaveRegion
1144 ****************************************************************************//**
1145 *
1146 * Disables the Slave PPU PROG structure.
1147 *
1148 * This is the PPU PROG slave-structure disable function. The PPU PROG protection
1149 * settings will seize to take effect after successful completion of this
1150 * function call.
1151 *
1152 * \note This function is applicable for CPUSS ver_2 only.
1153 *
1154 * \param base
1155 * The base address for the protection unit structure is being configured.
1156 *
1157 * \return
1158 * The status of the function call.
1159 *
1160 *   Status              | Description
1161 * ----------------------| ---------------------------------------
1162 * CY_PROT_SUCCESS       | The slave PPU PROG structure was disabled.
1163 * CY_PROT_FAILURE       | The structure is enabled and possibly locked.
1164 * CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
1165 *
1166 * \funcusage
1167 * \snippet prot/snippet/main.c snippet_Cy_Prot_DisablePpuProgSlaveRegion
1168 *
1169 *******************************************************************************/
Cy_Prot_DisablePpuProgSlaveRegion(PERI_MS_PPU_PR_Type * base)1170 cy_en_prot_status_t Cy_Prot_DisablePpuProgSlaveRegion(PERI_MS_PPU_PR_Type* base)
1171 {
1172     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
1173 
1174     CY_ASSERT_L1(NULL != base);
1175 
1176     PERI_MS_PPU_PR_SL_SIZE(base) =
1177     _CLR_SET_FLD32U((PERI_MS_PPU_PR_SL_SIZE(base)), PERI_MS_PPU_PR_V2_SL_SIZE_VALID, CY_PROT_STRUCT_DISABLE);
1178 
1179     status = (_FLD2VAL(PERI_MS_PPU_PR_V2_SL_SIZE_VALID, PERI_MS_PPU_PR_SL_SIZE(base)) != CY_PROT_STRUCT_DISABLE) ?
1180     CY_PROT_FAILURE : CY_PROT_SUCCESS;
1181 
1182     return status;
1183 }
1184 
1185 /*******************************************************************************
1186 * Function Name: Cy_Prot_ConfigPpuFixedMasterAtt
1187 ****************************************************************************//**
1188 *
1189 * Configures the protection structure with its protection attributes of the
1190 * Fixed Peripheral Protection Unit (PPU FIXED) master.
1191 *
1192 * This function configures the master structure governing the corresponding slave
1193 * structure pair. It is a mechanism to protect the slave PPU FIXED structure.
1194 * The memory location of the slave structure is known, so the address, region size
1195 * and sub-regions of the configuration structure are not applicable.
1196 *
1197 * \note This function is applicable for CPUSS ver_2 only.
1198 *
1199 * \param base
1200 * The register base address of the protection structure is being configured.
1201 *
1202 * \param pcMask
1203 * The protection context mask. It specifies the protection context or a set of
1204 * multiple protection contexts to be configured.
1205 * It is a value of OR'd (|) items of \ref cy_en_prot_pcmask_t.
1206 * \note The function accepts pcMask values from \ref CY_PROT_PCMASK1 to
1207 * \ref CY_PROT_PCMASK15. But each device has its own number of available protection
1208 * contexts. That number is defined by PERI_PC_NR in the config file. If pcMask
1209 * is passed beyond the range of available protection context, the bits above PERI_PC_NR
1210 * will be ignored.
1211 *
1212 * \param userPermission
1213 * The user permission setting. The CY_PROT_PERM_R or CY_PROT_PERM_RW values
1214 * are valid for the master.
1215 *
1216 * \param privPermission
1217 * The privileged permission setting. The CY_PROT_PERM_R or CY_PROT_PERM_RW values
1218 * are valid for the master.
1219 *
1220 * \param secure
1221 * The secure flag.
1222 *
1223 * \return
1224 * The status of the function call.
1225 *
1226 *   Status              | Description
1227 * ----------------------| ---------------------------------------
1228 * CY_PROT_SUCCESS       | The attributes were set up.
1229 * CY_PROT_FAILURE       | The attributes were not setup and the structure is possibly locked.
1230 * CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
1231 *
1232 * \note Only the user/privileged write permissions are configurable. The
1233 * read permissions are read-only and cannot be configured.
1234 *
1235 * \note PC0 accesses are read-only and are always enabled.
1236 *
1237 * \funcusage
1238 * \snippet prot/snippet/main.c snippet_Cy_Prot_ConfigPpuFixedMasterAtt
1239 *
1240 *******************************************************************************/
Cy_Prot_ConfigPpuFixedMasterAtt(PERI_MS_PPU_FX_Type * base,uint16_t pcMask,cy_en_prot_perm_t userPermission,cy_en_prot_perm_t privPermission,bool secure)1241 cy_en_prot_status_t Cy_Prot_ConfigPpuFixedMasterAtt(PERI_MS_PPU_FX_Type* base, uint16_t pcMask,
1242                                       cy_en_prot_perm_t userPermission, cy_en_prot_perm_t privPermission, bool secure)
1243 {
1244     /* The parameter checks */
1245     CY_ASSERT_L1(NULL != base);
1246     CY_ASSERT_L3(CY_PROT_IS_FIXED_MS_MS_PERM_VALID(userPermission));
1247     CY_ASSERT_L3(CY_PROT_IS_FIXED_MS_MS_PERM_VALID(privPermission));
1248 
1249     pcMask &= ((uint16_t) CY_PROT_PCMASK_VALID);
1250 
1251     CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 18.1','Checked manually, base pointer will not exceed register range.');
1252     return (Prot_ConfigPpuAtt(PERI_MS_PPU_FX_MS_ATT(base), pcMask, userPermission, privPermission, secure));
1253 }
1254 
1255 
1256 /*******************************************************************************
1257 * Function Name: Cy_Prot_ConfigPpuFixedSlaveAtt
1258 ****************************************************************************//**
1259 *
1260 * Configures the protection structure with its protection attributes of
1261 * the Fixed Peripheral Protection Unit (PPU FIXED) slave.
1262 *
1263 * This function configures the slave structure of the PPU FIXED pair, which can
1264 * protect any peripheral memory region in a device from invalid bus-master
1265 * access.
1266 *
1267 * \note This function is applicable for CPUSS ver_2 only.
1268 *
1269 * \param base
1270 * The register base address of the protection structure is being configured.
1271 *
1272 * \param pcMask
1273 * The protection context mask. It specifies the protection context or a set of
1274 * multiple protection contexts to be configured.
1275 * It is a value of OR'd (|) items of \ref cy_en_prot_pcmask_t.
1276 * For example: (\ref CY_PROT_PCMASK1 | \ref CY_PROT_PCMASK3 | \ref CY_PROT_PCMASK4).
1277 * \note The function accepts pcMask values from \ref CY_PROT_PCMASK1 to \ref CY_PROT_PCMASK15.
1278 * But each device has its own number of available protection contexts.
1279 * That number is defined by PERI_PC_NR in the config file.
1280 *
1281 * \param userPermission
1282 * The user permission setting.
1283 *
1284 * \param privPermission
1285 * The privileged permission setting.
1286 *
1287 * \param secure
1288 * The secure flag.
1289 *
1290 * \return
1291 * The status of the function call.
1292 *
1293 *   Status              | Description
1294 * ----------------------| ---------------------------------------
1295 * CY_PROT_SUCCESS       | The attributes were set up.
1296 * CY_PROT_FAILURE       | The attributes were not setup and the structure is possibly locked.
1297 * CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
1298 *
1299 * \note PC0 accesses are read-only and are always enabled.
1300 *
1301 * \funcusage
1302 * \snippet prot/snippet/main.c snippet_Cy_Prot_ConfigPpuFixedSlaveAtt
1303 *
1304 *******************************************************************************/
Cy_Prot_ConfigPpuFixedSlaveAtt(PERI_MS_PPU_FX_Type * base,uint16_t pcMask,cy_en_prot_perm_t userPermission,cy_en_prot_perm_t privPermission,bool secure)1305 cy_en_prot_status_t Cy_Prot_ConfigPpuFixedSlaveAtt(PERI_MS_PPU_FX_Type* base, uint16_t pcMask,
1306                            cy_en_prot_perm_t userPermission, cy_en_prot_perm_t privPermission, bool secure)
1307 {
1308     /* The parameter checks */
1309     CY_ASSERT_L1(NULL != base);
1310     CY_ASSERT_L3(CY_PROT_IS_FIXED_MS_PERM_VALID(userPermission));
1311     CY_ASSERT_L3(CY_PROT_IS_FIXED_MS_PERM_VALID(privPermission));
1312 
1313     CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 18.1','Checked manually, base pointer will not exceed register range.');
1314     return (Prot_ConfigPpuAtt(PERI_MS_PPU_FX_SL_ATT(base), pcMask, userPermission, privPermission, secure));
1315 }
1316 #endif /* defined (CY_IP_MXPERI_VERSION) && (CY_IP_MXPERI_VERSION > 1U) */
1317 
1318 #if (defined (CY_IP_M4CPUSS) && defined (CY_IP_MXPERI_VERSION) && (CY_IP_MXPERI_VERSION == 1U)) || defined (CY_DOXYGEN)
1319 /*******************************************************************************
1320 * Function Name: Cy_Prot_ConfigPpuProgMasterStruct
1321 ****************************************************************************//**
1322 *
1323 * Configures a Programmable Peripheral Protection Unit (PPU PROG) master
1324 * protection struct with its protection attributes.
1325 *
1326 * This function configures the master struct governing the corresponding slave
1327 * struct pair. It is a mechanism to protect the slave PPU PROG struct. Since
1328 * the memory location of the slave struct is known, the address, regionSize and
1329 * subregions of the configuration struct are not applicable.
1330 *
1331 * Note that only the user/privileged write permissions are configurable. The
1332 * read and execute permissions are read-only and cannot be configured.
1333 *
1334 * \note This function is applicable for CPUSS ver_1 only.
1335 *
1336 * \param base
1337 * The register base address of the protection struct being configured.
1338 *
1339 * \param config
1340 * Initialization structure with all the protection attributes.
1341 *
1342 * \return
1343 * Status of the function call.
1344 *
1345 *  Status                | Description
1346 *  ------------          | -----------
1347 *  CY_PROT_SUCCESS       | PPU PROG master struct was successfully configured.
1348 *  CY_PROT_FAILURE       | The resource is locked.
1349 *  CY_PROT_BAD_PARAM     | An incorrect/invalid parameter was passed.
1350 *  CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
1351 *
1352 * \funcusage
1353 * \snippet prot/snippet/main.c snippet_Cy_Prot_ConfigPpuProgMasterStruct
1354 *
1355 *******************************************************************************/
Cy_Prot_ConfigPpuProgMasterStruct(PERI_PPU_PR_Type * base,const cy_stc_ppu_prog_cfg_t * config)1356 cy_en_prot_status_t Cy_Prot_ConfigPpuProgMasterStruct(PERI_PPU_PR_Type* base, const cy_stc_ppu_prog_cfg_t* config)
1357 {
1358     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
1359     uint32_t attReg;
1360 
1361     CY_ASSERT_L1(NULL != base);
1362     CY_ASSERT_L3(CY_PROT_IS_PROG_MS_PERM_VALID(config->userPermission));
1363     CY_ASSERT_L3(CY_PROT_IS_PROG_MS_PERM_VALID(config->privPermission));
1364 
1365     if (CY_PERI_V1 != 0U)
1366     {
1367         if(((uint32_t)config->pcMask & CY_PROT_PPU_PROG_PC_LIMIT_MASK) != 0UL)
1368         {
1369             /* PC mask out of range - not supported in device */
1370             status = CY_PROT_BAD_PARAM;
1371         }
1372         else
1373         {
1374             /* ADDR1 is read only. Only configure ATT1 */
1375             attReg = ((uint32_t)config->userPermission & CY_PROT_ATT_PERMISSION_MASK)
1376                         | (uint32_t)(((uint32_t)config->privPermission & CY_PROT_ATT_PERMISSION_MASK) << CY_PROT_ATT_PRIV_PERMISSION_SHIFT)
1377                         | _VAL2FLD(PERI_PPU_PR_ATT1_NS, !(config->secure))
1378                         | _VAL2FLD(PERI_PPU_PR_ATT1_PC_MASK_15_TO_1, config->pcMask)
1379                         /* No region size - read only for master structs */
1380                         | _VAL2FLD(PERI_PPU_PR_ATT1_PC_MATCH, config->pcMatch);
1381             if ((attReg & CY_PROT_PPU_PROG_ATT1_MASK) != attReg)
1382             {
1383                 /* Invalid parameter was passed */
1384                 status = CY_PROT_BAD_PARAM;
1385             }
1386             else
1387             {
1388                 PERI_PPU_PR_ATT1(base) = attReg;
1389                 status = ((PERI_PPU_PR_ATT1(base) & CY_PROT_PPU_PROG_ATT1_MASK) != attReg) ? CY_PROT_FAILURE : CY_PROT_SUCCESS;
1390             }
1391         }
1392     }
1393 
1394     return status;
1395 }
1396 
1397 /*******************************************************************************
1398 * Function Name: Cy_Prot_ConfigPpuProgSlaveStruct
1399 ****************************************************************************//**
1400 *
1401 * Configures a Programmable Peripheral Protection Unit (PPU PROG) slave
1402 * protection struct with its protection attributes.
1403 *
1404 * This function configures the slave struct of a PPU PROG pair, which can
1405 * protect any peripheral memory region in a device from invalid bus master
1406 * accesses.
1407 *
1408 * Note that the user/privileged execute accesses are read-only and are always
1409 * enabled.
1410 *
1411 * \note This function is applicable for CPUSS ver_1 only.
1412 *
1413 * \param base
1414 * The register base address of the protection structure being configured.
1415 *
1416 * \param config
1417 * Initialization structure with all the protection attributes.
1418 *
1419 * \return
1420 * Status of the function call.
1421 *
1422 *   Status               | Description
1423 *   ------------         | -----------
1424 *  CY_PROT_SUCCESS       | PPU PROG slave struct was successfully configured.
1425 *  CY_PROT_FAILURE       | The resource is locked.
1426 *  CY_PROT_BAD_PARAM     | An incorrect/invalid parameter was passed.
1427 *  CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
1428 *
1429 * \funcusage
1430 * \snippet prot/snippet/main.c snippet_Cy_Prot_ConfigPpuProgSlaveStruct
1431 *
1432 *******************************************************************************/
Cy_Prot_ConfigPpuProgSlaveStruct(PERI_PPU_PR_Type * base,const cy_stc_ppu_prog_cfg_t * config)1433 cy_en_prot_status_t Cy_Prot_ConfigPpuProgSlaveStruct(PERI_PPU_PR_Type* base, const cy_stc_ppu_prog_cfg_t* config)
1434 {
1435     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
1436     uint32_t addrReg;
1437     uint32_t attReg;
1438 
1439     CY_ASSERT_L1(NULL != base);
1440     CY_ASSERT_L3(CY_PROT_IS_PROG_SL_PERM_VALID(config->userPermission));
1441     CY_ASSERT_L3(CY_PROT_IS_PROG_SL_PERM_VALID(config->privPermission));
1442     CY_ASSERT_L3(CY_PROT_IS_REGION_SIZE_VALID(config->regionSize));
1443 
1444     if (CY_PERI_V1 != 0U)
1445     {
1446         if(((uint32_t)config->pcMask & CY_PROT_PPU_PROG_PC_LIMIT_MASK) != 0UL)
1447         {
1448             /* PC mask out of range - not supported in device */
1449             status = CY_PROT_BAD_PARAM;
1450         }
1451         else
1452         {
1453             addrReg= _VAL2FLD(PERI_PPU_PR_ADDR0_SUBREGION_DISABLE, config->subregions)
1454                         | _VAL2FLD(PERI_PPU_PR_ADDR0_ADDR24, (uint32_t)((uint32_t)config->address >> CY_PROT_ADDR_SHIFT));
1455             attReg= ((uint32_t)config->userPermission & CY_PROT_ATT_PERMISSION_MASK)
1456                         | (uint32_t)(((uint32_t)config->privPermission & CY_PROT_ATT_PERMISSION_MASK) << CY_PROT_ATT_PRIV_PERMISSION_SHIFT)
1457                         | _VAL2FLD(PERI_PPU_PR_ATT0_NS, !(config->secure))
1458                         | _VAL2FLD(PERI_PPU_PR_ATT0_PC_MASK_15_TO_1, config->pcMask)
1459                         | _VAL2FLD(PERI_PPU_PR_ATT0_REGION_SIZE, config->regionSize)
1460                         | _VAL2FLD(PERI_PPU_PR_ATT0_PC_MATCH, config->pcMatch);
1461             if ((attReg & CY_PROT_PPU_PROG_ATT0_MASK) != attReg)
1462             {
1463                 /* Invalid parameter was passed */
1464                 status = CY_PROT_BAD_PARAM;
1465             }
1466             else
1467             {
1468                 PERI_PPU_PR_ATT0(base) = attReg;
1469                 PERI_PPU_PR_ADDR0(base) = addrReg;
1470                 status = ((PERI_PPU_PR_ADDR0(base) != addrReg) || ((PERI_PPU_PR_ATT0(base) & CY_PROT_PPU_PROG_ATT0_MASK) != attReg))
1471                             ? CY_PROT_FAILURE : CY_PROT_SUCCESS;
1472             }
1473         }
1474     }
1475 
1476     return status;
1477 }
1478 
1479 
1480 /*******************************************************************************
1481 * Function Name: Cy_Prot_EnablePpuProgMasterStruct
1482 ****************************************************************************//**
1483 *
1484 * Enables the Master PPU PROG structure.
1485 *
1486 * This is a PPU PROG master struct enable function. The PPU PROG protection
1487 * settings will take effect after successful completion of this function call.
1488 *
1489 * \note This function is applicable for CPUSS ver_1 only.
1490 *
1491 * \param base
1492 * The base address for the protection unit structure being configured.
1493 *
1494 * \return
1495 * Status of the function call.
1496 *
1497 *   Status                | Description
1498 *   ------------          | -----------
1499 *   CY_PROT_SUCCESS       | The Master PU struct was enabled.
1500 *   CY_PROT_FAILURE       | The Master PU struct is disabled and possibly locked.
1501 *   CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
1502 *
1503 * \funcusage
1504 * \snippet prot/snippet/main.c snippet_Cy_Prot_EnablePpuProgMasterStruct
1505 *
1506 *******************************************************************************/
Cy_Prot_EnablePpuProgMasterStruct(PERI_PPU_PR_Type * base)1507 cy_en_prot_status_t Cy_Prot_EnablePpuProgMasterStruct(PERI_PPU_PR_Type* base)
1508 {
1509     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
1510 
1511     CY_ASSERT_L1(NULL != base);
1512 
1513     if (CY_PERI_V1 != 0U)
1514     {
1515         PERI_PPU_PR_ATT1(base) |= _VAL2FLD(PERI_PPU_PR_ATT1_ENABLED, CY_PROT_STRUCT_ENABLE);
1516         status = (_FLD2VAL(PERI_PPU_PR_ATT1_ENABLED, PERI_PPU_PR_ATT1(base)) != CY_PROT_STRUCT_ENABLE) ?
1517                     CY_PROT_FAILURE : CY_PROT_SUCCESS;
1518     }
1519 
1520     return status;
1521 }
1522 
1523 
1524 /*******************************************************************************
1525 * Function Name: Cy_Prot_DisablePpuProgMasterStruct
1526 ****************************************************************************//**
1527 *
1528 * Disables the Master PPU PROG structure.
1529 *
1530 * This is a PPU PROG master struct disable function. The PPU PROG protection
1531 * settings will seize to take effect after successful completion of this
1532 * function call.
1533 *
1534 * \note This function is applicable for CPUSS ver_1 only.
1535 *
1536 * \param base
1537 * The base address for the protection unit structure being configured.
1538 *
1539 * \return
1540 * Status of the function call.
1541 *
1542 *   Status                | Description
1543 *   ------------          | -----------
1544 *   CY_PROT_SUCCESS       | The Master PU struct was disabled.
1545 *   CY_PROT_FAILURE       | The Master PU struct is enabled and possibly locked.
1546 *   CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
1547 *
1548 * \funcusage
1549 * \snippet prot/snippet/main.c snippet_Cy_Prot_DisablePpuProgMasterStruct
1550 *
1551 *******************************************************************************/
Cy_Prot_DisablePpuProgMasterStruct(PERI_PPU_PR_Type * base)1552 cy_en_prot_status_t Cy_Prot_DisablePpuProgMasterStruct(PERI_PPU_PR_Type* base)
1553 {
1554     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
1555 
1556     CY_ASSERT_L1(NULL != base);
1557 
1558     if (CY_PERI_V1 != 0U)
1559     {
1560         PERI_PPU_PR_ATT1(base) &= ~_VAL2FLD(PERI_PPU_PR_ATT1_ENABLED, CY_PROT_STRUCT_ENABLE);
1561         status = (_FLD2VAL(PERI_PPU_PR_ATT1_ENABLED, PERI_PPU_PR_ATT1(base)) == CY_PROT_STRUCT_ENABLE) ?
1562                     CY_PROT_FAILURE : CY_PROT_SUCCESS;
1563     }
1564 
1565     return status;
1566 }
1567 
1568 
1569 /*******************************************************************************
1570 * Function Name: Cy_Prot_EnablePpuProgSlaveStruct
1571 ****************************************************************************//**
1572 *
1573 * Enables the Slave PPU PROG structure.
1574 *
1575 * This is a PPU PROG slave struct enable function. The PPU PROG protection
1576 * settings will take effect after successful completion of this function call.
1577 *
1578 * \note This function is applicable for CPUSS ver_1 only.
1579 *
1580 * \param base
1581 * The base address for the protection unit structure being configured.
1582 *
1583 * \return
1584 * Status of the function call.
1585 *
1586 *   Status                | Description
1587 *   ------------          | -----------
1588 *   CY_PROT_SUCCESS       | The Slave PU struct was enabled.
1589 *   CY_PROT_FAILURE       | The Slave PU struct is disabled and possibly locked.
1590 *   CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
1591 *
1592 * \funcusage
1593 * \snippet prot/snippet/main.c snippet_Cy_Prot_EnablePpuProgSlaveStruct
1594 *
1595 *******************************************************************************/
Cy_Prot_EnablePpuProgSlaveStruct(PERI_PPU_PR_Type * base)1596 cy_en_prot_status_t Cy_Prot_EnablePpuProgSlaveStruct(PERI_PPU_PR_Type* base)
1597 {
1598     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
1599 
1600     CY_ASSERT_L1(NULL != base);
1601 
1602     if (CY_PERI_V1 != 0U)
1603     {
1604         PERI_PPU_PR_ATT0(base) |= _VAL2FLD(PERI_PPU_PR_ATT0_ENABLED, CY_PROT_STRUCT_ENABLE);
1605         status = (_FLD2VAL(PERI_PPU_PR_ATT0_ENABLED, PERI_PPU_PR_ATT0(base)) != CY_PROT_STRUCT_ENABLE) ?
1606                     CY_PROT_FAILURE : CY_PROT_SUCCESS;
1607     }
1608 
1609     return status;
1610 }
1611 
1612 
1613 /*******************************************************************************
1614 * Function Name: Cy_Prot_DisablePpuProgSlaveStruct
1615 ****************************************************************************//**
1616 *
1617 * Disables the Slave PPU PROG structure.
1618 *
1619 * This is a PPU PROG slave struct disable function. The PPU PROG protection
1620 * settings will seize to take effect after successful completion of this
1621 * function call.
1622 *
1623 * \note This function is applicable for CPUSS ver_1 only.
1624 *
1625 * \param base
1626 * The base address for the protection unit structure being configured.
1627 *
1628 * \return
1629 * Status of the function call.
1630 *
1631 *   Status                | Description
1632 *   ------------          | -----------
1633 *   CY_PROT_SUCCESS       | The Slave PU struct was disabled.
1634 *   CY_PROT_FAILURE       | The Slave PU struct is enabled and possibly locked.
1635 *   CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
1636 *
1637 * \funcusage
1638 * \snippet prot/snippet/main.c snippet_Cy_Prot_DisablePpuProgSlaveStruct
1639 *
1640 *******************************************************************************/
Cy_Prot_DisablePpuProgSlaveStruct(PERI_PPU_PR_Type * base)1641 cy_en_prot_status_t Cy_Prot_DisablePpuProgSlaveStruct(PERI_PPU_PR_Type* base)
1642 {
1643     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
1644 
1645     CY_ASSERT_L1(NULL != base);
1646 
1647     if (CY_PERI_V1 != 0U)
1648     {
1649         PERI_PPU_PR_ATT0(base) &= ~_VAL2FLD(PERI_PPU_PR_ATT0_ENABLED, CY_PROT_STRUCT_ENABLE);
1650         status = (_FLD2VAL(PERI_PPU_PR_ATT0_ENABLED, PERI_PPU_PR_ATT0(base)) == CY_PROT_STRUCT_ENABLE) ?
1651                     CY_PROT_FAILURE : CY_PROT_SUCCESS;
1652     }
1653 
1654     return status;
1655 }
1656 
1657 
1658 /*******************************************************************************
1659 * Function Name: Cy_Prot_GetPpuProgStruct
1660 ****************************************************************************//**
1661 *
1662 * Functions returns a pointer of the requested unused Programmable PPU
1663 * structure. Function searches the Programmable PPU structure until it finds
1664 * one that both the slave and master sections are disabled. After an available
1665 * structure is located, function enables the slave structure and enables all
1666 * attributes, to make sure that a subsequent call will not see this
1667 * as an available (unused) Programmable PPU.
1668 *
1669 * It is up to the user to implement, if needed, a system in which a semaphore
1670 * will lock-out all but one CPU from calling this function at once.
1671 *
1672 * \note This function is applicable for CPUSS ver_1 only.
1673 *
1674 * \param base
1675 * The base address for the Programmable PPU structure returned if an unused
1676 * structure was found. If an empty structure was not found, the returned
1677 * pointer is NULL.
1678 *
1679 * \param reqMode
1680 * This parameter (request mode) selects how the user wants to select a
1681 * Programmable PPU structure.
1682 *
1683 *   reqMode                 | Description
1684 * --------------------------| -----------------------------
1685 * CY_PROT_REQMODE_HIGHPRIOR | Return the Programmable PPU structure with the highest priority.
1686 * CY_PROT_REQMODE_LOWPRIOR  | Return the Programmable PPU structure with the lowest priority.
1687 * CY_PROT_REQMODE_INDEX     | Return the Programmable PPU structure with the specific index.
1688 *
1689 * \param ppuProgIndex
1690 * This is the index of the requested Programmable PPU structure. It is only
1691 * used if the request mode is reqMode = CY_PROT_REQMODE_INDEX.
1692 *
1693 * \return
1694 * Status of the function call.
1695 *
1696 *   Status              | Description
1697 * ----------------------| ---------------------------------------
1698 * CY_PROT_SUCCESS       | The Slave PU struct was disabled.
1699 * CY_PROT_FAILURE       | The Master or Slave Programmable PPU struct is disabled and possibly locked.
1700 * CY_PROT_UNAVAILABLE   | The requested structure in use or there were no unused structures.
1701 * CY_PROT_INVALID_STATE | Function was called on unsupported PERI HW version.
1702 *
1703 * \funcusage
1704 * \snippet prot/snippet/main.c snippet_Cy_Prot_GetPpuProgStruct
1705 *
1706 *******************************************************************************/
Cy_Prot_GetPpuProgStruct(PERI_PPU_PR_Type ** base,cy_en_prot_req_mode_t reqMode,uint32_t ppuProgIndex)1707 cy_en_prot_status_t Cy_Prot_GetPpuProgStruct(PERI_PPU_PR_Type** base, cy_en_prot_req_mode_t reqMode, uint32_t ppuProgIndex)
1708 {
1709     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
1710 
1711     if (CY_PERI_V1 != 0U)
1712     {
1713         CY_ASSERT_L3(CY_PROT_IS_PPU_PROG_REQ_MODE_VALID(reqMode));
1714         CY_ASSERT_L2(CY_PROT_IS_PPU_PROG_IDX_VALID(ppuProgIndex));
1715 
1716         status = CY_PROT_UNAVAILABLE;
1717         int32_t stcIdx = (int32_t) ppuProgIndex;
1718 
1719         *base = NULL;
1720 
1721         switch (reqMode)
1722         {
1723             /* Programmed structures priority goes from 0 (highest) to
1724             *  PROT_PPU_PROG_STRUCT_WTH_LOWEST_PR (lowest)
1725             */
1726             case CY_PROT_REQMODE_LOWPRIOR:
1727 
1728                 stcIdx = PROT_PPU_PROG_STRUCT_WTH_LOWEST_PR;
1729                 do
1730                 {
1731                     if (Prot_IsPpuProgStructDisabled((uint32_t)stcIdx))
1732                     {
1733                         status = CY_PROT_SUCCESS;
1734                     }
1735                     else
1736                     {
1737                         --stcIdx;
1738                     }
1739                 } while ((stcIdx >= 0) && (CY_PROT_SUCCESS != status));
1740                 break;
1741 
1742             /* Programmed structures priority goes from 0 (highest) to
1743             *  PROT_PPU_PROG_STRUCT_WTH_LOWEST_PR (lowest)
1744             */
1745             case CY_PROT_REQMODE_HIGHPRIOR:
1746                 stcIdx = 0;
1747                 do
1748                 {
1749                     if (Prot_IsPpuProgStructDisabled((uint32_t)stcIdx))
1750                     {
1751                         status = CY_PROT_SUCCESS;
1752                     }
1753                     else
1754                     {
1755                         ++stcIdx;
1756                     }
1757                 } while ((stcIdx <= PROT_PPU_PROG_STRUCT_WTH_LOWEST_PR) && (CY_PROT_SUCCESS != status));
1758                 break;
1759 
1760             case CY_PROT_REQMODE_INDEX:
1761 
1762                 if (Prot_IsPpuProgStructDisabled((uint32_t)stcIdx))
1763                 {
1764                     status = CY_PROT_SUCCESS;
1765                 }
1766                 break;
1767 
1768             default:
1769                 /* Unknown request mode */
1770                 break;
1771         }
1772 
1773         /* Enable Programmable PPU struct */
1774         if (CY_PROT_SUCCESS == status)
1775         {
1776             PROT_PERI_PPU_PR_STRUCT_IDX_ATT0(stcIdx) |= _VAL2FLD(PERI_PPU_PR_ATT0_ENABLED, CY_PROT_STRUCT_ENABLE);
1777 
1778             status =
1779             (_FLD2VAL(PERI_PPU_PR_ATT0_ENABLED, PROT_PERI_PPU_PR_STRUCT_IDX_ATT0(stcIdx)) != CY_PROT_STRUCT_ENABLE) ?
1780              CY_PROT_FAILURE : CY_PROT_SUCCESS;
1781 
1782             /* Enable all attributes only if Slave struct was enabled */
1783             if (CY_PROT_SUCCESS == status)
1784             {
1785                 *base = (PERI_PPU_PR_Type*) PROT_PERI_PPU_PR_STRUCT_IDX(stcIdx);
1786                 PROT_PERI_PPU_PR_STRUCT_IDX_ATT0(stcIdx) |= PROT_PERI_PPU_PR_ATT0_ENABLE_ALL_MASK;
1787             }
1788         }
1789     }
1790 
1791     return status;
1792 }
1793 
1794 
1795 
1796 /*******************************************************************************
1797 * Function Name: Cy_Prot_ConfigPpuFixedGrMasterStruct
1798 ****************************************************************************//**
1799 *
1800 * Configures a Fixed Peripheral Group Protection Unit (PPU GR) master
1801 * protection struct with its protection attributes.
1802 *
1803 * This function configures the master struct governing the corresponding slave
1804 * struct pair. It is a mechanism to protect the slave PPU GR struct. Since
1805 * the memory location of the slave struct is known, the address, regionSize and
1806 * subregions of the configuration struct are not applicable.
1807 *
1808 * Note that only the user/privileged write permissions are configurable. The
1809 * read and execute permissions are read-only and cannot be configured.
1810 *
1811 * \note This function is applicable for CPUSS ver_1 only.
1812 *
1813 * \param base
1814 * The register base address of the protection struct being configured.
1815 *
1816 * \param config
1817 * Initialization structure with all the protection attributes.
1818 *
1819 * \return
1820 * Status of the function call.
1821 *
1822 *   Status                | Description
1823 *   ------------          | -----------
1824 *   CY_PROT_SUCCESS       | PPU GR master struct was successfully configured.
1825 *   CY_PROT_FAILURE       | The resource is locked.
1826 *   CY_PROT_BAD_PARAM     | An incorrect/invalid parameter was passed.
1827 *   CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
1828 *
1829 * \funcusage
1830 * \snippet prot/snippet/main.c snippet_Cy_Prot_ConfigPpuFixedGrMasterStruct
1831 *
1832 *******************************************************************************/
Cy_Prot_ConfigPpuFixedGrMasterStruct(PERI_PPU_GR_Type * base,const cy_stc_ppu_gr_cfg_t * config)1833 cy_en_prot_status_t Cy_Prot_ConfigPpuFixedGrMasterStruct(PERI_PPU_GR_Type* base, const cy_stc_ppu_gr_cfg_t* config)
1834 {
1835     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
1836     uint32_t attReg;
1837 
1838     CY_ASSERT_L1(NULL != base);
1839     CY_ASSERT_L3(CY_PROT_IS_FIXED_MS_PERM_VALID(config->userPermission));
1840     CY_ASSERT_L3(CY_PROT_IS_FIXED_MS_PERM_VALID(config->privPermission));
1841 
1842     if (CY_PERI_V1 != 0U)
1843     {
1844         if(((uint32_t)config->pcMask & CY_PROT_PPU_FIXED_PC_LIMIT_MASK) != 0UL)
1845         {
1846             /* PC mask out of range - not supported in device */
1847             status = CY_PROT_BAD_PARAM;
1848         }
1849         else
1850         {
1851             /* ADDR1 is read only. Only configure ATT1 */
1852             attReg = (((uint32_t)config->userPermission & CY_PROT_ATT_PERMISSION_MASK))
1853                         | (uint32_t)(((uint32_t)config->privPermission & CY_PROT_ATT_PERMISSION_MASK) << CY_PROT_ATT_PRIV_PERMISSION_SHIFT)
1854                         | _VAL2FLD(PERI_PPU_GR_ATT1_NS, !(config->secure))
1855                         | _VAL2FLD(PERI_PPU_GR_ATT1_PC_MASK_15_TO_1, config->pcMask)
1856                         /* No region size - read only for master structs */
1857                         | _VAL2FLD(PERI_PPU_GR_ATT1_PC_MATCH, config->pcMatch);
1858             if ((attReg & CY_PROT_PPU_GR_ATT1_MASK) != attReg)
1859             {
1860                 /* Invalid parameter was passed */
1861                 status = CY_PROT_BAD_PARAM;
1862             }
1863             else
1864             {
1865                 PERI_PPU_GR_ATT1(base) = attReg;
1866                 status = ((PERI_PPU_GR_ATT1(base) & CY_PROT_PPU_GR_ATT1_MASK) != attReg) ? CY_PROT_FAILURE : CY_PROT_SUCCESS;
1867             }
1868         }
1869     }
1870 
1871     return status;
1872 }
1873 
1874 
1875 /*******************************************************************************
1876 * Function Name: Cy_Prot_ConfigPpuFixedGrSlaveStruct
1877 ****************************************************************************//**
1878 *
1879 * Configures a Fixed Peripheral Group Protection Unit (PPU GR) slave
1880 * protection struct with its protection attributes.
1881 *
1882 * This function configures the slave struct of a PPU GR pair, which can
1883 * protect an entire peripheral MMIO group from invalid bus master accesses.
1884 * Refer to the device Technical Reference manual for details on peripheral
1885 * MMIO grouping and which peripherals belong to which groups.
1886 *
1887 * Each fixed PPU GR is devoted to a defined MMIO group. Hence the address,
1888 * regionSize and subregions of the configuration struct are not applicable.
1889 *
1890 * Note that the user/privileged execute accesses are read-only and are always
1891 * enabled.
1892 *
1893 * \note This function is applicable for CPUSS ver_1 only.
1894 *
1895 * \param base
1896 * The register base address of the protection structure being configured.
1897 *
1898 * \param config
1899 * Initialization structure with all the protection attributes.
1900 *
1901 * \return
1902 * Status of the function call.
1903 *
1904 *   Status                | Description
1905 *   ------------          | -----------
1906 *   CY_PROT_SUCCESS       | PPU GR slave struct was successfully configured.
1907 *   CY_PROT_FAILURE       | The resource is locked.
1908 *   CY_PROT_BAD_PARAM     | An incorrect/invalid parameter was passed.
1909 *   CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
1910 *
1911 * \funcusage
1912 * \snippet prot/snippet/main.c snippet_Cy_Prot_ConfigPpuFixedGrSlaveStruct
1913 *
1914 *******************************************************************************/
Cy_Prot_ConfigPpuFixedGrSlaveStruct(PERI_PPU_GR_Type * base,const cy_stc_ppu_gr_cfg_t * config)1915 cy_en_prot_status_t Cy_Prot_ConfigPpuFixedGrSlaveStruct(PERI_PPU_GR_Type* base, const cy_stc_ppu_gr_cfg_t* config)
1916 {
1917     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
1918     uint32_t attReg;
1919 
1920     CY_ASSERT_L1(NULL != base);
1921     CY_ASSERT_L3(CY_PROT_IS_FIXED_SL_PERM_VALID(config->userPermission));
1922     CY_ASSERT_L3(CY_PROT_IS_FIXED_SL_PERM_VALID(config->privPermission));
1923 
1924     if (CY_PERI_V1 != 0U)
1925     {
1926         if(((uint32_t)config->pcMask & CY_PROT_PPU_FIXED_PC_LIMIT_MASK) != 0UL)
1927         {
1928             /* PC mask out of range - not supported in device */
1929             status = CY_PROT_BAD_PARAM;
1930         }
1931         else
1932         {
1933             /* ADDR0 is read only. Only configure ATT0 */
1934             attReg = (uint32_t)(((uint32_t)config->userPermission & CY_PROT_ATT_PERMISSION_MASK))
1935                         | (uint32_t)(((uint32_t)config->privPermission & CY_PROT_ATT_PERMISSION_MASK) << CY_PROT_ATT_PRIV_PERMISSION_SHIFT)
1936                         | _VAL2FLD(PERI_PPU_GR_ATT0_NS, !(config->secure))
1937                         | _VAL2FLD(PERI_PPU_GR_ATT0_PC_MASK_15_TO_1, config->pcMask)
1938                         /* No region size - read only */
1939                         | _VAL2FLD(PERI_PPU_GR_ATT0_PC_MATCH, config->pcMatch);
1940             if ((attReg & CY_PROT_PPU_GR_ATT0_MASK) != attReg)
1941             {
1942                 /* Invalid parameter was passed */
1943                 status = CY_PROT_BAD_PARAM;
1944             }
1945             else
1946             {
1947                 PERI_PPU_GR_ATT0(base) = attReg;
1948                 status = ((PERI_PPU_GR_ATT0(base) & CY_PROT_PPU_GR_ATT0_MASK) != attReg) ? CY_PROT_FAILURE : CY_PROT_SUCCESS;
1949             }
1950         }
1951     }
1952 
1953     return status;
1954 }
1955 
1956 
1957 /*******************************************************************************
1958 * Function Name: Cy_Prot_EnablePpuFixedGrMasterStruct
1959 ****************************************************************************//**
1960 *
1961 * Enables the Master PPU GR structure.
1962 *
1963 * This is a PPU GR master struct enable function. The PPU GR protection
1964 * settings will take effect after successful completion of this function call.
1965 *
1966 * \note This function is applicable for CPUSS ver_1 only.
1967 *
1968 * \param base
1969 * The base address for the protection unit structure being configured.
1970 *
1971 * \return
1972 * Status of the function call.
1973 *
1974 *   Status                | Description
1975 *   ------------          | -----------
1976 *   CY_PROT_SUCCESS       | The Master PU struct was enabled.
1977 *   CY_PROT_FAILURE       | The Master PU struct is disabled and possibly locked.
1978 *   CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
1979 *
1980 * \funcusage
1981 * \snippet prot/snippet/main.c snippet_Cy_Prot_EnablePpuFixedGrMasterStruct
1982 *
1983 *******************************************************************************/
Cy_Prot_EnablePpuFixedGrMasterStruct(PERI_PPU_GR_Type * base)1984 cy_en_prot_status_t Cy_Prot_EnablePpuFixedGrMasterStruct(PERI_PPU_GR_Type* base)
1985 {
1986     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
1987 
1988     CY_ASSERT_L1(NULL != base);
1989 
1990     if (CY_PERI_V1 != 0U)
1991     {
1992         PERI_PPU_GR_ATT1(base) |= _VAL2FLD(PERI_PPU_GR_ATT1_ENABLED, CY_PROT_STRUCT_ENABLE);
1993         status = (_FLD2VAL(PERI_PPU_GR_ATT1_ENABLED, PERI_PPU_GR_ATT1(base)) != CY_PROT_STRUCT_ENABLE) ?
1994                     CY_PROT_FAILURE : CY_PROT_SUCCESS;
1995     }
1996 
1997     return status;
1998 }
1999 
2000 
2001 /*******************************************************************************
2002 * Function Name: Cy_Prot_DisablePpuFixedGrMasterStruct
2003 ****************************************************************************//**
2004 *
2005 * Disables the Master PPU GR structure.
2006 *
2007 * This is a PPU GR master struct disable function. The PPU GR protection
2008 * settings will seize to take effect after successful completion of this
2009 * function call.
2010 *
2011 * \note This function is applicable for CPUSS ver_1 only.
2012 *
2013 * \param base
2014 * The base address for the protection unit structure being configured.
2015 *
2016 * \return
2017 * Status of the function call.
2018 *
2019 *   Status                | Description
2020 *   ------------          | -----------
2021 *   CY_PROT_SUCCESS       | The Master PU struct was disabled.
2022 *   CY_PROT_FAILURE       | The Master PU struct is enabled and possibly locked.
2023 *   CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
2024 *
2025 * \funcusage
2026 * \snippet prot/snippet/main.c snippet_Cy_Prot_DisablePpuFixedGrMasterStruct
2027 *
2028 *******************************************************************************/
Cy_Prot_DisablePpuFixedGrMasterStruct(PERI_PPU_GR_Type * base)2029 cy_en_prot_status_t Cy_Prot_DisablePpuFixedGrMasterStruct(PERI_PPU_GR_Type* base)
2030 {
2031     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
2032 
2033     CY_ASSERT_L1(NULL != base);
2034 
2035     if (CY_PERI_V1 != 0U)
2036     {
2037         PERI_PPU_GR_ATT1(base) &= ~_VAL2FLD(PERI_PPU_GR_ATT1_ENABLED, CY_PROT_STRUCT_ENABLE);
2038         status = (_FLD2VAL(PERI_PPU_GR_ATT1_ENABLED, PERI_PPU_GR_ATT1(base)) == CY_PROT_STRUCT_ENABLE) ?
2039                     CY_PROT_FAILURE : CY_PROT_SUCCESS;
2040     }
2041 
2042     return status;
2043 }
2044 
2045 
2046 /*******************************************************************************
2047 * Function Name: Cy_Prot_EnablePpuFixedGrSlaveStruct
2048 ****************************************************************************//**
2049 *
2050 * Enables the Slave PPU GR structure.
2051 *
2052 * This is a PPU GR slave struct enable function. The PPU GR protection
2053 * settings will take effect after successful completion of this function call.
2054 *
2055 * \note This function is applicable for CPUSS ver_1 only.
2056 *
2057 * \param base
2058 * The base address for the protection unit structure being configured.
2059 *
2060 * \return
2061 * Status of the function call.
2062 *
2063 *   Status                | Description
2064 *   ------------          | -----------
2065 *   CY_PROT_SUCCESS       | The Slave PU struct was enabled.
2066 *   CY_PROT_FAILURE       | The Slave PU struct is disabled and possibly locked.
2067 *   CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
2068 *
2069 * \funcusage
2070 * \snippet prot/snippet/main.c snippet_Cy_Prot_EnablePpuFixedGrSlaveStruct
2071 *
2072 *******************************************************************************/
Cy_Prot_EnablePpuFixedGrSlaveStruct(PERI_PPU_GR_Type * base)2073 cy_en_prot_status_t Cy_Prot_EnablePpuFixedGrSlaveStruct(PERI_PPU_GR_Type* base)
2074 {
2075     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
2076 
2077     CY_ASSERT_L1(NULL != base);
2078 
2079     if (CY_PERI_V1 != 0U)
2080     {
2081         PERI_PPU_GR_ATT0(base) |= _VAL2FLD(PERI_PPU_GR_ATT0_ENABLED, CY_PROT_STRUCT_ENABLE);
2082         status = (_FLD2VAL(PERI_PPU_GR_ATT0_ENABLED, PERI_PPU_GR_ATT0(base)) != CY_PROT_STRUCT_ENABLE) ?
2083                     CY_PROT_FAILURE : CY_PROT_SUCCESS;
2084     }
2085 
2086     return status;
2087 }
2088 
2089 
2090 /*******************************************************************************
2091 * Function Name: Cy_Prot_DisablePpuFixedGrSlaveStruct
2092 ****************************************************************************//**
2093 *
2094 * Disables the Slave PPU GR structure.
2095 *
2096 * This is a PPU GR slave struct disable function. The PPU GR protection
2097 * settings will seize to take effect after successful completion of this
2098 * function call.
2099 *
2100 * \note This function is applicable for CPUSS ver_1 only.
2101 *
2102 * \param base
2103 * The base address for the protection unit structure being configured.
2104 *
2105 * \return
2106 * Status of the function call.
2107 *
2108 *   Status                | Description
2109 *   ------------          | -----------
2110 *   CY_PROT_SUCCESS       | The Slave PU struct was disabled.
2111 *   CY_PROT_FAILURE       | The Slave PU struct is enabled and possibly locked.
2112 *   CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
2113 *
2114 * \funcusage
2115 * \snippet prot/snippet/main.c snippet_Cy_Prot_DisablePpuFixedGrSlaveStruct
2116 *
2117 *******************************************************************************/
Cy_Prot_DisablePpuFixedGrSlaveStruct(PERI_PPU_GR_Type * base)2118 cy_en_prot_status_t Cy_Prot_DisablePpuFixedGrSlaveStruct(PERI_PPU_GR_Type* base)
2119 {
2120     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
2121 
2122     CY_ASSERT_L1(NULL != base);
2123 
2124     if (CY_PERI_V1 != 0U)
2125     {
2126         PERI_PPU_GR_ATT0(base) &= ~_VAL2FLD(PERI_PPU_GR_ATT0_ENABLED, CY_PROT_STRUCT_ENABLE);
2127         status = (_FLD2VAL(PERI_PPU_GR_ATT0_ENABLED, PERI_PPU_GR_ATT0(base)) == CY_PROT_STRUCT_ENABLE) ?
2128                     CY_PROT_FAILURE : CY_PROT_SUCCESS;
2129     }
2130 
2131     return status;
2132 }
2133 
2134 
2135 /*******************************************************************************
2136 * Function Name: Cy_Prot_ConfigPpuFixedSlMasterStruct
2137 ****************************************************************************//**
2138 *
2139 * Configures a Fixed Peripheral Slave Protection Unit (PPU SL) master
2140 * protection struct with its protection attributes.
2141 *
2142 * This function configures the master struct governing the corresponding slave
2143 * struct pair. It is a mechanism to protect the slave PPU SL struct. Since
2144 * the memory location of the slave struct is known, the address, regionSize and
2145 * subregions of the configuration struct are not applicable.
2146 *
2147 * Note that only the user/privileged write permissions are configurable. The
2148 * read and execute permissions are read-only and cannot be configured.
2149 *
2150 * \note This function is applicable for CPUSS ver_1 only.
2151 *
2152 * \param base
2153 * The register base address of the protection struct being configured.
2154 *
2155 * \param config
2156 * Initialization structure with all the protection attributes.
2157 *
2158 * \return
2159 * Status of the function call.
2160 *
2161 *   Status                | Description
2162 *   ------------          | -----------
2163 *   CY_PROT_SUCCESS       | PPU SL master struct was successfully configured.
2164 *   CY_PROT_FAILURE       | The resource is locked.
2165 *   CY_PROT_BAD_PARAM     | An incorrect/invalid parameter was passed.
2166 *   CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
2167 *
2168 * \funcusage
2169 * \snippet prot/snippet/main.c snippet_Cy_Prot_ConfigPpuFixedSlMasterStruct
2170 *
2171 *******************************************************************************/
Cy_Prot_ConfigPpuFixedSlMasterStruct(PERI_GR_PPU_SL_Type * base,const cy_stc_ppu_sl_cfg_t * config)2172 cy_en_prot_status_t Cy_Prot_ConfigPpuFixedSlMasterStruct(PERI_GR_PPU_SL_Type* base, const cy_stc_ppu_sl_cfg_t* config)
2173 {
2174     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
2175     uint32_t attReg;
2176 
2177     CY_ASSERT_L1(NULL != base);
2178     CY_ASSERT_L3(CY_PROT_IS_FIXED_MS_PERM_VALID(config->userPermission));
2179     CY_ASSERT_L3(CY_PROT_IS_FIXED_MS_PERM_VALID(config->privPermission));
2180 
2181     if (CY_PERI_V1 != 0U)
2182     {
2183         if(((uint32_t)config->pcMask & CY_PROT_PPU_FIXED_PC_LIMIT_MASK) != 0UL)
2184         {
2185             /* PC mask out of range - not supported in device */
2186             status = CY_PROT_BAD_PARAM;
2187         }
2188         else
2189         {
2190             /* ADDR1 is read only. Only configure ATT1 */
2191             attReg = ((uint32_t)config->userPermission & CY_PROT_ATT_PERMISSION_MASK)
2192                         | (uint32_t)(((uint32_t)config->privPermission & CY_PROT_ATT_PERMISSION_MASK) << CY_PROT_ATT_PRIV_PERMISSION_SHIFT)
2193                         | _VAL2FLD(PERI_GR_PPU_SL_ATT1_NS, !(config->secure))
2194                         | _VAL2FLD(PERI_GR_PPU_SL_ATT1_PC_MASK_15_TO_1, config->pcMask)
2195                         /* No region size - read only for master structs */
2196                         | _VAL2FLD(PERI_GR_PPU_SL_ATT1_PC_MATCH, config->pcMatch);
2197             if ((attReg & CY_PROT_PPU_SL_ATT1_MASK) != attReg)
2198             {
2199                 /* Invalid parameter was passed */
2200                 status = CY_PROT_BAD_PARAM;
2201             }
2202             else
2203             {
2204                 PERI_GR_PPU_SL_ATT1(base) = attReg;
2205                 status = ((PERI_GR_PPU_SL_ATT1(base) & CY_PROT_PPU_SL_ATT1_MASK) != attReg) ? CY_PROT_FAILURE : CY_PROT_SUCCESS;
2206             }
2207         }
2208     }
2209 
2210     return status;
2211 }
2212 
2213 
2214 /*******************************************************************************
2215 * Function Name: Cy_Prot_ConfigPpuFixedSlSlaveStruct
2216 ****************************************************************************//**
2217 *
2218 * Configures a Fixed Peripheral Slave Protection Unit (PPU SL) slave
2219 * protection struct with its protection attributes.
2220 *
2221 * This function configures the slave struct of a PPU SL pair, which can
2222 * protect an entire peripheral slave instance from invalid bus master accesses.
2223 * For example, TCPWM0, TCPWM1, SCB0 and SCB1 etc.
2224 *
2225 * Each fixed PPU SL is devoted to a defined peripheral slave. Hence the address,
2226 * regionSize and subregions of the configuration struct are not applicable.
2227 *
2228 * Note that the user/privileged execute accesses are read-only and are always
2229 * enabled.
2230 *
2231 * \note This function is applicable for CPUSS ver_1 only.
2232 *
2233 * \param base
2234 * The register base address of the protection structure being configured.
2235 *
2236 * \param config
2237 * Initialization structure with all the protection attributes.
2238 *
2239 * \return
2240 * Status of the function call.
2241 *
2242 *   Status                | Description
2243 *   ------------          | -----------
2244 *   CY_PROT_SUCCESS       | PPU SL slave struct was successfully configured.
2245 *   CY_PROT_FAILURE       | The resource is locked.
2246 *   CY_PROT_BAD_PARAM     | An incorrect/invalid parameter was passed.
2247 *   CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
2248 *
2249 * \funcusage
2250 * \snippet prot/snippet/main.c snippet_Cy_Prot_ConfigPpuFixedSlSlaveStruct
2251 *
2252 *******************************************************************************/
Cy_Prot_ConfigPpuFixedSlSlaveStruct(PERI_GR_PPU_SL_Type * base,const cy_stc_ppu_sl_cfg_t * config)2253 cy_en_prot_status_t Cy_Prot_ConfigPpuFixedSlSlaveStruct(PERI_GR_PPU_SL_Type* base, const cy_stc_ppu_sl_cfg_t* config)
2254 {
2255     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
2256     uint32_t attReg;
2257 
2258     CY_ASSERT_L1(NULL != base);
2259     CY_ASSERT_L3(CY_PROT_IS_FIXED_SL_PERM_VALID(config->userPermission));
2260     CY_ASSERT_L3(CY_PROT_IS_FIXED_SL_PERM_VALID(config->privPermission));
2261 
2262     if (CY_PERI_V1 != 0U)
2263     {
2264         if(((uint32_t)config->pcMask & CY_PROT_PPU_FIXED_PC_LIMIT_MASK) != 0UL)
2265         {
2266             /* PC mask out of range - not supported in device */
2267             status = CY_PROT_BAD_PARAM;
2268         }
2269         else
2270         {
2271             /* ADDR0 is read only. Only configure ATT0 */
2272             attReg = ((uint32_t)config->userPermission & CY_PROT_ATT_PERMISSION_MASK)
2273                         | (uint32_t)(((uint32_t)config->privPermission & CY_PROT_ATT_PERMISSION_MASK) << CY_PROT_ATT_PRIV_PERMISSION_SHIFT)
2274                         | _VAL2FLD(PERI_GR_PPU_SL_ATT0_NS, !(config->secure))
2275                         | _VAL2FLD(PERI_GR_PPU_SL_ATT0_PC_MASK_15_TO_1, config->pcMask)
2276                         /* No region size - read only */
2277                         | _VAL2FLD(PERI_GR_PPU_SL_ATT0_PC_MATCH, config->pcMatch);
2278             if ((attReg & CY_PROT_PPU_SL_ATT0_MASK) != attReg)
2279             {
2280                 /* Invalid parameter was passed */
2281                 status = CY_PROT_BAD_PARAM;
2282             }
2283             else
2284             {
2285                 PERI_GR_PPU_SL_ATT0(base) = attReg;
2286                 status = ((PERI_GR_PPU_SL_ATT0(base) & CY_PROT_PPU_SL_ATT0_MASK) != attReg) ? CY_PROT_FAILURE : CY_PROT_SUCCESS;
2287             }
2288         }
2289     }
2290 
2291     return status;
2292 }
2293 
2294 
2295 /*******************************************************************************
2296 * Function Name: Cy_Prot_EnablePpuFixedSlMasterStruct
2297 ****************************************************************************//**
2298 *
2299 * Enables the Master PPU SL structure.
2300 *
2301 * This is a PPU SL master struct enable function. The PPU SL protection
2302 * settings will take effect after successful completion of this function call.
2303 *
2304 * \note This function is applicable for CPUSS ver_1 only.
2305 *
2306 * \param base
2307 * The base address for the protection unit structure being configured.
2308 *
2309 * \return
2310 * Status of the function call.
2311 *
2312 *   Status                | Description
2313 *   ------------          | -----------
2314 *   CY_PROT_SUCCESS       | The Master PU struct was enabled.
2315 *   CY_PROT_FAILURE       | The Master PU struct is disabled and possibly locked.
2316 *   CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
2317 *
2318 * \funcusage
2319 * \snippet prot/snippet/main.c snippet_Cy_Prot_EnablePpuFixedSlMasterStruct
2320 *
2321 *******************************************************************************/
Cy_Prot_EnablePpuFixedSlMasterStruct(PERI_GR_PPU_SL_Type * base)2322 cy_en_prot_status_t Cy_Prot_EnablePpuFixedSlMasterStruct(PERI_GR_PPU_SL_Type* base)
2323 {
2324     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
2325 
2326     CY_ASSERT_L1(NULL != base);
2327 
2328     if (CY_PERI_V1 != 0U)
2329     {
2330         PERI_GR_PPU_SL_ATT1(base) |= _VAL2FLD(PERI_GR_PPU_SL_ATT1_ENABLED, CY_PROT_STRUCT_ENABLE);
2331         status = (_FLD2VAL(PERI_GR_PPU_SL_ATT1_ENABLED, PERI_GR_PPU_SL_ATT1(base)) != CY_PROT_STRUCT_ENABLE) ?
2332                     CY_PROT_FAILURE : CY_PROT_SUCCESS;
2333     }
2334 
2335     return status;
2336 }
2337 
2338 
2339 /*******************************************************************************
2340 * Function Name: Cy_Prot_DisablePpuFixedSlMasterStruct
2341 ****************************************************************************//**
2342 *
2343 * Disables the Master PPU SL structure.
2344 *
2345 * This is a PPU SL master struct disable function. The PPU SL protection
2346 * settings will seize to take effect after successful completion of this
2347 * function call.
2348 *
2349 * \note This function is applicable for CPUSS ver_1 only.
2350 *
2351 * \param base
2352 * The base address for the protection unit structure being configured.
2353 *
2354 * \return
2355 * Status of the function call.
2356 *
2357 *   Status                | Description
2358 *   ------------          | -----------
2359 *   CY_PROT_SUCCESS       | The Master PU struct was disabled.
2360 *   CY_PROT_FAILURE       | The Master PU struct is enabled and possibly locked.
2361 *   CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
2362 *
2363 * \funcusage
2364 * \snippet prot/snippet/main.c snippet_Cy_Prot_DisablePpuFixedSlMasterStruct
2365 *
2366 *******************************************************************************/
Cy_Prot_DisablePpuFixedSlMasterStruct(PERI_GR_PPU_SL_Type * base)2367 cy_en_prot_status_t Cy_Prot_DisablePpuFixedSlMasterStruct(PERI_GR_PPU_SL_Type* base)
2368 {
2369     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
2370 
2371     CY_ASSERT_L1(NULL != base);
2372 
2373     if (CY_PERI_V1 != 0U)
2374     {
2375         PERI_GR_PPU_SL_ATT1(base) &= ~_VAL2FLD(PERI_GR_PPU_SL_ATT1_ENABLED, CY_PROT_STRUCT_ENABLE);
2376         status = (_FLD2VAL(PERI_GR_PPU_SL_ATT1_ENABLED, PERI_GR_PPU_SL_ATT1(base)) == CY_PROT_STRUCT_ENABLE) ?
2377                     CY_PROT_FAILURE : CY_PROT_SUCCESS;
2378     }
2379 
2380     return status;
2381 }
2382 
2383 
2384 /*******************************************************************************
2385 * Function Name: Cy_Prot_EnablePpuFixedSlSlaveStruct
2386 ****************************************************************************//**
2387 *
2388 * Enables the Slave PPU SL structure.
2389 *
2390 * This is a PPU SL slave struct enable function. The PPU SL protection
2391 * settings will take effect after successful completion of this function call.
2392 *
2393 * \note This function is applicable for CPUSS ver_1 only.
2394 *
2395 * \param base
2396 * The base address for the protection unit structure being configured.
2397 *
2398 * \return
2399 * Status of the function call.
2400 *
2401 *   Status                | Description
2402 *   ------------          | -----------
2403 *   CY_PROT_SUCCESS       | The Slave PU struct was enabled.
2404 *   CY_PROT_FAILURE       | The Slave PU struct is disabled and possibly locked.
2405 *   CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
2406 *
2407 * \funcusage
2408 * \snippet prot/snippet/main.c snippet_Cy_Prot_EnablePpuFixedSlSlaveStruct
2409 *
2410 *******************************************************************************/
Cy_Prot_EnablePpuFixedSlSlaveStruct(PERI_GR_PPU_SL_Type * base)2411 cy_en_prot_status_t Cy_Prot_EnablePpuFixedSlSlaveStruct(PERI_GR_PPU_SL_Type* base)
2412 {
2413     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
2414 
2415     CY_ASSERT_L1(NULL != base);
2416 
2417     if (CY_PERI_V1 != 0U)
2418     {
2419         PERI_GR_PPU_SL_ATT0(base) |= _VAL2FLD(PERI_GR_PPU_SL_ATT0_ENABLED, CY_PROT_STRUCT_ENABLE);
2420         status = (_FLD2VAL(PERI_GR_PPU_SL_ATT0_ENABLED, PERI_GR_PPU_SL_ATT0(base)) != CY_PROT_STRUCT_ENABLE) ?
2421                     CY_PROT_FAILURE : CY_PROT_SUCCESS;
2422     }
2423 
2424     return status;
2425 }
2426 
2427 
2428 /*******************************************************************************
2429 * Function Name: Cy_Prot_DisablePpuFixedSlSlaveStruct
2430 ****************************************************************************//**
2431 *
2432 * Disables the Slave PPU SL structure.
2433 *
2434 * This is a PPU SL slave struct disable function. The PPU SL protection
2435 * settings will seize to take effect after successful completion of this
2436 * function call.
2437 *
2438 * \note This function is applicable for CPUSS ver_1 only.
2439 *
2440 * \param base
2441 * The base address for the protection unit structure being configured.
2442 *
2443 * \return
2444 * Status of the function call.
2445 *
2446 *   Status                | Description
2447 *   ------------          | -----------
2448 *   CY_PROT_SUCCESS       | The Slave PU struct was enabled.
2449 *   CY_PROT_FAILURE       | The Slave PU struct is disabled and possibly locked.
2450 *   CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
2451 *
2452 * \funcusage
2453 * \snippet prot/snippet/main.c snippet_Cy_Prot_DisablePpuFixedSlSlaveStruct
2454 *
2455 *******************************************************************************/
Cy_Prot_DisablePpuFixedSlSlaveStruct(PERI_GR_PPU_SL_Type * base)2456 cy_en_prot_status_t Cy_Prot_DisablePpuFixedSlSlaveStruct(PERI_GR_PPU_SL_Type* base)
2457 {
2458     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
2459 
2460     CY_ASSERT_L1(NULL != base);
2461 
2462     if (CY_PERI_V1 != 0U)
2463     {
2464         PERI_GR_PPU_SL_ATT0(base) &= ~_VAL2FLD(PERI_GR_PPU_SL_ATT0_ENABLED, CY_PROT_STRUCT_ENABLE);
2465         status = (_FLD2VAL(PERI_GR_PPU_SL_ATT0_ENABLED, PERI_GR_PPU_SL_ATT0(base)) == CY_PROT_STRUCT_ENABLE) ?
2466                     CY_PROT_FAILURE : CY_PROT_SUCCESS;
2467     }
2468 
2469     return status;
2470 }
2471 
2472 
2473 /*******************************************************************************
2474 * Function Name: Cy_Prot_ConfigPpuFixedRgMasterStruct
2475 ****************************************************************************//**
2476 *
2477 * Configures a Fixed Peripheral Region Protection Unit (PPU RG) master
2478 * protection struct with its protection attributes.
2479 *
2480 * This function configures the master struct governing the corresponding slave
2481 * struct pair. It is a mechanism to protect the slave PPU RG struct. Since
2482 * the memory location of the slave struct is known, the address, regionSize and
2483 * subregions of the configuration struct are not applicable.
2484 *
2485 * Note that only the user/privileged write permissions are configurable. The
2486 * read and execute permissions are read-only and cannot be configured.
2487 *
2488 * \note This function is applicable for CPUSS ver_1 only.
2489 *
2490 * \param base
2491 * The register base address of the protection struct being configured.
2492 *
2493 * \param config
2494 * Initialization structure with all the protection attributes.
2495 *
2496 * \return
2497 * Status of the function call.
2498 *
2499 *   Status                | Description
2500 *   ------------          | -----------
2501 *   CY_PROT_SUCCESS       | PPU RG master struct was successfully configured.
2502 *   CY_PROT_FAILURE       | The resource is locked.
2503 *   CY_PROT_BAD_PARAM     | An incorrect/invalid parameter was passed.
2504 *   CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
2505 *
2506 * \funcusage
2507 * \snippet prot/snippet/main.c snippet_Cy_Prot_ConfigPpuFixedRgMasterStruct
2508 *
2509 *******************************************************************************/
Cy_Prot_ConfigPpuFixedRgMasterStruct(PERI_GR_PPU_RG_Type * base,const cy_stc_ppu_rg_cfg_t * config)2510 cy_en_prot_status_t Cy_Prot_ConfigPpuFixedRgMasterStruct(PERI_GR_PPU_RG_Type* base, const cy_stc_ppu_rg_cfg_t* config)
2511 {
2512     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
2513     uint32_t attReg;
2514 
2515     CY_ASSERT_L1(NULL != base);
2516     CY_ASSERT_L3(CY_PROT_IS_FIXED_MS_PERM_VALID(config->userPermission));
2517     CY_ASSERT_L3(CY_PROT_IS_FIXED_MS_PERM_VALID(config->privPermission));
2518 
2519     if (CY_PERI_V1 != 0U)
2520     {
2521         if(((uint32_t)config->pcMask & CY_PROT_PPU_FIXED_PC_LIMIT_MASK) != 0UL)
2522         {
2523             /* PC mask out of range - not supported in device */
2524             status = CY_PROT_BAD_PARAM;
2525         }
2526         else
2527         {
2528             /* ADDR1 is read only. Only configure ATT1 */
2529             attReg = ((uint32_t)config->userPermission & CY_PROT_ATT_PERMISSION_MASK)
2530                         | (uint32_t)(((uint32_t)config->privPermission & CY_PROT_ATT_PERMISSION_MASK) << CY_PROT_ATT_PRIV_PERMISSION_SHIFT)
2531                         | _VAL2FLD(PERI_GR_PPU_RG_ATT1_NS, !(config->secure))
2532                         | _VAL2FLD(PERI_GR_PPU_RG_ATT1_PC_MASK_15_TO_1, config->pcMask)
2533                         /* No region size - read only for master structs */
2534                         | _VAL2FLD(PERI_GR_PPU_RG_ATT1_PC_MATCH, config->pcMatch);
2535             if ((attReg & CY_PROT_PPU_RG_ATT1_MASK) != attReg)
2536             {
2537                 /* Invalid parameter was passed */
2538                 status = CY_PROT_BAD_PARAM;
2539             }
2540             else
2541             {
2542                 PERI_GR_PPU_RG_ATT1(base) = attReg;
2543                 status = ((PERI_GR_PPU_RG_ATT1(base) & CY_PROT_PPU_RG_ATT1_MASK) != attReg) ? CY_PROT_FAILURE : CY_PROT_SUCCESS;
2544             }
2545         }
2546     }
2547 
2548     return status;
2549 }
2550 
2551 
2552 /*******************************************************************************
2553 * Function Name: Cy_Prot_ConfigPpuFixedRgSlaveStruct
2554 ****************************************************************************//**
2555 *
2556 * Configures a Fixed Peripheral Region Protection Unit (PPU RG) slave
2557 * protection struct with its protection attributes.
2558 *
2559 * This function configures the slave struct of a PPU RG pair, which can
2560 * protect specified regions of peripheral instances. For example, individual
2561 * DW channel structs, SMPU structs, and IPC structs etc.
2562 *
2563 * Each fixed PPU RG is devoted to a defined peripheral region. Hence the address,
2564 * regionSize and subregions of the configuration struct are not applicable.
2565 *
2566 * Note that the user/privileged execute accesses are read-only and are always
2567 * enabled.
2568 *
2569 * \note This function is applicable for CPUSS ver_1 only.
2570 *
2571 * \param base
2572 * The register base address of the protection structure being configured.
2573 *
2574 * \param config
2575 * Initialization structure with all the protection attributes.
2576 *
2577 * \return
2578 * Status of the function call.
2579 *
2580 *   Status                | Description
2581 *   ------------          | -----------
2582 *   CY_PROT_SUCCESS       | PPU RG slave struct was successfully configured.
2583 *   CY_PROT_FAILURE       | The resource is locked.
2584 *   CY_PROT_BAD_PARAM     | An incorrect/invalid parameter was passed.
2585 *   CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
2586 *
2587 * \funcusage
2588 * \snippet prot/snippet/main.c snippet_Cy_Prot_ConfigPpuFixedRgSlaveStruct
2589 *
2590 *******************************************************************************/
Cy_Prot_ConfigPpuFixedRgSlaveStruct(PERI_GR_PPU_RG_Type * base,const cy_stc_ppu_rg_cfg_t * config)2591 cy_en_prot_status_t Cy_Prot_ConfigPpuFixedRgSlaveStruct(PERI_GR_PPU_RG_Type* base, const cy_stc_ppu_rg_cfg_t* config)
2592 {
2593     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
2594     uint32_t attReg;
2595 
2596     CY_ASSERT_L1(NULL != base);
2597     CY_ASSERT_L3(CY_PROT_IS_FIXED_SL_PERM_VALID(config->userPermission));
2598     CY_ASSERT_L3(CY_PROT_IS_FIXED_SL_PERM_VALID(config->privPermission));
2599 
2600     if (CY_PERI_V1 != 0U)
2601     {
2602         if(((uint32_t)config->pcMask & CY_PROT_PPU_FIXED_PC_LIMIT_MASK) != 0UL)
2603         {
2604             /* PC mask out of range - not supported in device */
2605             status = CY_PROT_BAD_PARAM;
2606         }
2607         else
2608         {
2609             /* ADDR0 is read only. Only configure ATT0 */
2610             attReg = ((uint32_t)config->userPermission & CY_PROT_ATT_PERMISSION_MASK)
2611                         | (uint32_t)(((uint32_t)config->privPermission & CY_PROT_ATT_PERMISSION_MASK) << CY_PROT_ATT_PRIV_PERMISSION_SHIFT)
2612                         | _VAL2FLD(PERI_GR_PPU_RG_ATT0_NS, !(config->secure))
2613                         | _VAL2FLD(PERI_GR_PPU_RG_ATT0_PC_MASK_15_TO_1, config->pcMask)
2614                         /* No region size - read only */
2615                         | _VAL2FLD(PERI_GR_PPU_RG_ATT0_PC_MATCH, config->pcMatch);
2616             if ((attReg & CY_PROT_PPU_RG_ATT0_MASK) != attReg)
2617             {
2618                 /* Invalid parameter was passed */
2619                 status = CY_PROT_BAD_PARAM;
2620             }
2621             else
2622             {
2623                 PERI_GR_PPU_RG_ATT0(base) = attReg;
2624                 status = ((PERI_GR_PPU_RG_ATT0(base) & CY_PROT_PPU_RG_ATT0_MASK) != attReg) ? CY_PROT_FAILURE : CY_PROT_SUCCESS;
2625             }
2626         }
2627     }
2628 
2629     return status;
2630 }
2631 
2632 
2633 /*******************************************************************************
2634 * Function Name: Cy_Prot_EnablePpuFixedRgMasterStruct
2635 ****************************************************************************//**
2636 *
2637 * Enables the Master PPU RG structure.
2638 *
2639 * This is a PPU RG master struct enable function. The PPU RG protection
2640 * settings will take effect after successful completion of this function call.
2641 *
2642 * \note This function is applicable for CPUSS ver_1 only.
2643 *
2644 * \param base
2645 * The base address for the protection unit structure being configured.
2646 *
2647 * \return
2648 * Status of the function call.
2649 *
2650 *   Status                | Description
2651 *   ------------          | -----------
2652 *   CY_PROT_SUCCESS       | The Master PU struct was enabled.
2653 *   CY_PROT_FAILURE       | The Master PU struct is disabled and possibly locked.
2654 *   CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
2655 *
2656 * \funcusage
2657 * \snippet prot/snippet/main.c snippet_Cy_Prot_EnablePpuFixedRgMasterStruct
2658 *
2659 *******************************************************************************/
Cy_Prot_EnablePpuFixedRgMasterStruct(PERI_GR_PPU_RG_Type * base)2660 cy_en_prot_status_t Cy_Prot_EnablePpuFixedRgMasterStruct(PERI_GR_PPU_RG_Type* base)
2661 {
2662     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
2663 
2664     if (CY_PERI_V1 != 0U)
2665     {
2666         PERI_GR_PPU_RG_ATT1(base) |= _VAL2FLD(PERI_GR_PPU_RG_ATT1_ENABLED, CY_PROT_STRUCT_ENABLE);
2667         status = (_FLD2VAL(PERI_GR_PPU_RG_ATT1_ENABLED, PERI_GR_PPU_RG_ATT1(base)) != CY_PROT_STRUCT_ENABLE) ?
2668                     CY_PROT_FAILURE : CY_PROT_SUCCESS;
2669     }
2670 
2671     return status;
2672 }
2673 
2674 
2675 /*******************************************************************************
2676 * Function Name: Cy_Prot_DisablePpuFixedRgMasterStruct
2677 ****************************************************************************//**
2678 *
2679 * Disables the Master PPU RG structure.
2680 *
2681 * This is a PPU RG master struct disable function. The PPU RG protection
2682 * settings will seize to take effect after successful completion of this
2683 * function call.
2684 *
2685 * \note This function is applicable for CPUSS ver_1 only.
2686 *
2687 * \param base
2688 * The base address for the protection unit structure being configured.
2689 *
2690 * \return
2691 * Status of the function call.
2692 *
2693 *   Status                | Description
2694 *   ------------          | -----------
2695 *   CY_PROT_SUCCESS       | The Master PU struct was disabled.
2696 *   CY_PROT_FAILURE       | The Master PU struct is enabled and possibly locked.
2697 *   CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
2698 *
2699 * \funcusage
2700 * \snippet prot/snippet/main.c snippet_Cy_Prot_DisablePpuFixedRgMasterStruct
2701 *
2702 *******************************************************************************/
Cy_Prot_DisablePpuFixedRgMasterStruct(PERI_GR_PPU_RG_Type * base)2703 cy_en_prot_status_t Cy_Prot_DisablePpuFixedRgMasterStruct(PERI_GR_PPU_RG_Type* base)
2704 {
2705     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
2706 
2707     if (CY_PERI_V1 != 0U)
2708     {
2709         PERI_GR_PPU_RG_ATT1(base) &= ~_VAL2FLD(PERI_GR_PPU_RG_ATT1_ENABLED, CY_PROT_STRUCT_ENABLE);
2710         status = (_FLD2VAL(PERI_GR_PPU_RG_ATT1_ENABLED, PERI_GR_PPU_RG_ATT1(base)) == CY_PROT_STRUCT_ENABLE) ?
2711                     CY_PROT_FAILURE : CY_PROT_SUCCESS;
2712     }
2713 
2714     return status;
2715 }
2716 
2717 
2718 /*******************************************************************************
2719 * Function Name: Cy_Prot_EnablePpuFixedRgSlaveStruct
2720 ****************************************************************************//**
2721 *
2722 * Enables the Slave PPU RG structure.
2723 *
2724 * This is a PPU RG slave struct enable function. The PPU RG protection
2725 * settings will take effect after successful completion of this function call.
2726 *
2727 * \note This function is applicable for CPUSS ver_1 only.
2728 *
2729 * \param base
2730 * The base address for the protection unit structure being configured.
2731 *
2732 * \return
2733 * Status of the function call.
2734 *
2735 *   Status                | Description
2736 *   ------------          | -----------
2737 *   CY_PROT_SUCCESS       | The Slave PU struct was enabled.
2738 *   CY_PROT_FAILURE       | The Slave PU struct is disabled and possibly locked.
2739 *   CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
2740 *
2741 * \funcusage
2742 * \snippet prot/snippet/main.c snippet_Cy_Prot_EnablePpuFixedRgSlaveStruct
2743 *
2744 *******************************************************************************/
Cy_Prot_EnablePpuFixedRgSlaveStruct(PERI_GR_PPU_RG_Type * base)2745 cy_en_prot_status_t Cy_Prot_EnablePpuFixedRgSlaveStruct(PERI_GR_PPU_RG_Type* base)
2746 {
2747     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
2748 
2749     if (CY_PERI_V1 != 0U)
2750     {
2751         PERI_GR_PPU_RG_ATT0(base) |= _VAL2FLD(PERI_GR_PPU_RG_ATT0_ENABLED, CY_PROT_STRUCT_ENABLE);
2752         status = (_FLD2VAL(PERI_GR_PPU_RG_ATT0_ENABLED, PERI_GR_PPU_RG_ATT0(base)) != CY_PROT_STRUCT_ENABLE) ?
2753                     CY_PROT_FAILURE : CY_PROT_SUCCESS;
2754     }
2755 
2756     return status;
2757 }
2758 
2759 
2760 /*******************************************************************************
2761 * Function Name: Cy_Prot_DisablePpuFixedRgSlaveStruct
2762 ****************************************************************************//**
2763 *
2764 * Disables the Slave PPU RG structure.
2765 *
2766 * This is a PPU RG slave struct disable function. The PPU RG protection
2767 * settings will seize to take effect after successful completion of this
2768 * function call.
2769 *
2770 * \note This function is applicable for CPUSS ver_1 only.
2771 *
2772 * \param base
2773 * The base address for the protection unit structure being configured.
2774 *
2775 * \return
2776 * Status of the function call.
2777 *
2778 *   Status                | Description
2779 *   ------------          | -----------
2780 *   CY_PROT_SUCCESS       | The Slave PU struct was disabled.
2781 *   CY_PROT_FAILURE       | The Slave PU struct is enabled and possibly locked.
2782 *   CY_PROT_INVALID_STATE | The function was called on the device with an unsupported PERI HW version.
2783 *
2784 * \funcusage
2785 * \snippet prot/snippet/main.c snippet_Cy_Prot_DisablePpuFixedRgSlaveStruct
2786 *
2787 *******************************************************************************/
Cy_Prot_DisablePpuFixedRgSlaveStruct(PERI_GR_PPU_RG_Type * base)2788 cy_en_prot_status_t Cy_Prot_DisablePpuFixedRgSlaveStruct(PERI_GR_PPU_RG_Type* base)
2789 {
2790     cy_en_prot_status_t status = CY_PROT_INVALID_STATE;
2791 
2792     if (CY_PERI_V1 != 0U)
2793     {
2794         PERI_GR_PPU_RG_ATT0(base) &= ~_VAL2FLD(PERI_GR_PPU_RG_ATT0_ENABLED, CY_PROT_STRUCT_ENABLE);
2795         status = (_FLD2VAL(PERI_GR_PPU_RG_ATT0_ENABLED, PERI_GR_PPU_RG_ATT0(base)) == CY_PROT_STRUCT_ENABLE) ?
2796                     CY_PROT_FAILURE : CY_PROT_SUCCESS;
2797     }
2798 
2799     return status;
2800 }
2801 
2802 #endif /* (CY_IP_M4CPUSS) && defined (CY_IP_MXPERI_VERSION) && (CY_IP_MXPERI_VERSION == 1U) */
2803 
2804 /*******************************************************************************
2805 * Function Name: Prot_IsSmpuStructDisabled
2806 ****************************************************************************//**
2807 *
2808 * This function returns the SMPU disabled status.
2809 *
2810 * \note This function is applicable for both CPUSS ver_1 and ver_2.
2811 *
2812 * \param smpuStcIndex
2813 * index of the SMPU structure.
2814 *
2815 * \return
2816 * true if both Slave and Master structures are disabled.
2817 * false if Master or/and Slave structure is/are enabled.
2818 *
2819 *******************************************************************************/
Prot_IsSmpuStructDisabled(uint32_t smpuStcIndex)2820 static bool Prot_IsSmpuStructDisabled(uint32_t smpuStcIndex)
2821 {
2822     return ((!_FLD2BOOL(PROT_SMPU_SMPU_STRUCT_ATT0_ENABLED, PROT_SMPU_SMPU_STRUCT_IDX_ATT0(smpuStcIndex))) &&
2823             (!_FLD2BOOL(PROT_SMPU_SMPU_STRUCT_ATT1_ENABLED, PROT_SMPU_SMPU_STRUCT_IDX_ATT1(smpuStcIndex))));
2824 }
2825 
2826 #if defined (CY_IP_M4CPUSS) && defined (CY_IP_MXPERI_VERSION) && (CY_IP_MXPERI_VERSION == 1u)
2827 /*******************************************************************************
2828 * Function Name: Prot_IsPpoProgStructDisabled
2829 ****************************************************************************//**
2830 *
2831 * This function returns the Peripheral PPU disabled status.
2832 *
2833 * \note This function is applicable for CPUSS ver_1.
2834 *
2835 * \param ppuStcIndex
2836 * index of the Prot_IsPpoProgStructDisabled structure.
2837 *
2838 * \return
2839 * true if both Slave and Master structures are disabled.
2840 * false if Master or/and Slave structure is/are enabled.
2841 *
2842 *******************************************************************************/
Prot_IsPpuProgStructDisabled(uint32_t ppuStcIndex)2843 static bool Prot_IsPpuProgStructDisabled(uint32_t ppuStcIndex)
2844 {
2845     return ((!_FLD2BOOL(PERI_PPU_PR_ATT0_ENABLED, PROT_PERI_PPU_PR_STRUCT_IDX_ATT0(ppuStcIndex))) &&
2846             (!_FLD2BOOL(PERI_PPU_PR_ATT1_ENABLED, PROT_PERI_PPU_PR_STRUCT_IDX_ATT1(ppuStcIndex))));
2847 }
2848 #endif /* defined (CY_IP_M4CPUSS) && defined (CY_IP_MXPERI_VERSION) && (CY_IP_MXPERI_VERSION == 1u) */
2849 
2850 #if defined(__cplusplus)
2851 }
2852 #endif
2853 
2854 #endif
2855 /* [] END OF FILE */
2856