1 /*
2  * Copyright 2019-2021 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef FSL_OTFAD_H_
10 #define FSL_OTFAD_H_
11 
12 #include "fsl_common.h"
13 
14 /*!
15  * @addtogroup otfad
16  * @{
17  */
18 
19 /******************************************************************************
20  * Definitions.
21  *****************************************************************************/
22 
23 /*! @name Driver version */
24 /*! @{ */
25 /*! @brief Driver version. */
26 #define FSL_OTFAD_DRIVER_VERSION (MAKE_VERSION(2U, 1U, 3U))
27 /*! @} */
28 
29 /*! @brief Status codes for the OTFAD driver. */
30 enum
31 {
32     kStatus_OTFAD_ResRegAccessMode = MAKE_STATUS(kStatusGroup_OTFAD, 0), /*!< Restricted register mode */
33     kStatus_OTFAD_AddressError     = MAKE_STATUS(kStatusGroup_OTFAD, 1), /*!< End address less than start address */
34     kStatus_OTFAD_RegionOverlap =
35         MAKE_STATUS(kStatusGroup_OTFAD, 2), /*!< the OTFAD does not support any form of memory region overlap,
36                                                  for system accesses that hit in multiple contexts or no contexts,
37                                                  the fetched data is simply bypassed */
38     kStatus_OTFAD_RegionMiss =
39         MAKE_STATUS(kStatusGroup_OTFAD, 3), /*!< For accesses that hit in a single context, but not the selected one */
40 };
41 
42 /*! @brief OTFAD context type */
43 enum
44 {
45     kOTFAD_Context_0 = 0U, /*!< context 0 */
46     kOTFAD_Context_1 = 1U, /*!< context 1 */
47     kOTFAD_Context_2 = 2U, /*!< context 2 */
48     kOTFAD_Context_3 = 3U, /*!< context 3 */
49 };
50 
51 /*! @brief OTFAD operate mode */
52 enum
53 {
54     kOTFAD_NRM = 0x00U, /*!< Normal Mode */
55     kOTFAD_SVM = 0x02U, /*!< Security Violation Mode */
56     kOTFAD_LDM = 0x03U, /*!< Logically Disabled Mode */
57 };
58 
59 /*! @brief OTFAD encryption configuration structure */
60 typedef struct _otfad_encryption_config
61 {
62     bool valid;           /*!< The context is valid or not */
63     bool AESdecryption;   /*!< AES decryption enable */
64     uint8_t readOnly;     /*!< read write attribute for the entire set of context registers */
65     uint8_t contextIndex; /*!< OTFAD context index */
66     uint32_t startAddr;   /*!< Start address*/
67     uint32_t endAddr;     /*!< End address */
68     uint32_t key[4];      /*!< Encryption key */
69     uint32_t counter[2];  /*!< Encryption counter */
70 } otfad_encryption_config_t;
71 
72 /*! @brief OTFAD configuration structure */
73 typedef struct _otfad_config
74 {
75 #if defined(FSL_FEATURE_OTFAD_HAS_HAS_IRQ_ENABLE) && (FSL_FEATURE_OTFAD_HAS_HAS_IRQ_ENABLE > 0)
76     bool enableIntRequest; /*!< Interrupt request enable */
77 #endif                     /* FSL_FEATURE_OTFAD_HAS_HAS_IRQ_ENABLE */
78 #if defined(FSL_FEATURE_OTFAD_HAS_FORCE_ERR) && (FSL_FEATURE_OTFAD_HAS_FORCE_ERR > 0)
79     bool forceError; /*!< Forces the OTFAD's key blob error flag (SR[KBERR]) to be asserted */
80 #endif               /* FSL_FEATURE_OTFAD_HAS_FORCE_ERR */
81     bool forceSVM;   /*!< Force entry into SVM after a write */
82     bool forceLDM;   /*!< Force entry into LDM after a write  */
83 #if defined(FSL_FEATURE_OTFAD_HAS_KEYBLOB_PROCESSING) && (FSL_FEATURE_OTFAD_HAS_KEYBLOB_PROCESSING > 0)
84     bool keyBlobScramble;        /*!< Key blob KEK scrambling */
85     bool keyBlobProcess;         /*!< Key blob processing */
86     bool startKeyBlobProcessing; /*!< key blob processing is initiated */
87 #endif                           /* FSL_FEATURE_OTFAD_HAS_KEYBLOB_PROCESSING */
88     bool restrictedRegAccess;    /*!< Restricted register access enable */
89     bool enableOTFAD;            /*!< OTFAD has decryption enabled */
90     void *flexspiBaseAddr;       /*! Driver Base address. */
91 } otfad_config_t;
92 
93 /*******************************************************************************
94  * APIs
95  ******************************************************************************/
96 
97 #if defined(__cplusplus)
98 extern "C" {
99 #endif
100 
101 /*!
102  * @name Initialization and deinitialization
103  * @{
104  */
105 
106 /*!
107  * @brief OTFAD module initialization function.
108  *
109  * @param config OTFAD configuration.
110  */
111 void OTFAD_GetDefaultConfig(otfad_config_t *config);
112 
113 /*!
114  * @brief OTFAD module initialization function.
115  *
116  * @param base OTFAD base address.
117  * @param config OTFAD configuration.
118  */
119 
120 #if defined(DOXYGEN_OUTPUT) && DOXYGEN_OUTPUT
121 void OTFAD_Init(OTFAD_Type *base, const otfad_config_t *config);
122 #else
123 AT_QUICKACCESS_SECTION_CODE(void OTFAD_Init(OTFAD_Type *base, const otfad_config_t *config));
124 #endif
125 
126 /*!
127  * @brief Deinitializes the OTFAD.
128  *
129  */
130 #if defined(DOXYGEN_OUTPUT) && DOXYGEN_OUTPUT
131 void OTFAD_Deinit(OTFAD_Type *base);
132 #else
133 AT_QUICKACCESS_SECTION_CODE(void OTFAD_Deinit(OTFAD_Type *base));
134 #endif
135 
136 /*! @} */
137 
138 /*!
139  * @name Status
140  * @{
141  */
142 /*!
143  * @brief OTFAD module get operate mode.
144  *
145  * @param base OTFAD base address.
146  */
OTFAD_GetOperateMode(OTFAD_Type * base)147 static inline uint32_t OTFAD_GetOperateMode(OTFAD_Type *base)
148 {
149     return (base->SR & OTFAD_SR_MODE_MASK) >> OTFAD_SR_MODE_SHIFT;
150 }
151 
152 /*!
153  * @brief OTFAD module get status.
154  *
155  * @param base OTFAD base address.
156  */
OTFAD_GetStatus(OTFAD_Type * base)157 static inline uint32_t OTFAD_GetStatus(OTFAD_Type *base)
158 {
159     return base->SR;
160 }
161 
162 /*! @} */
163 
164 /*!
165  * @name functional
166  * @{
167  */
168 
169 /*!
170  * @brief OTFAD module set encryption configuration
171  *
172  * @param base  OTFAD base address.
173  * @param config  encryption configuration.
174  *
175  * Note: if enable keyblob process, the first 256 bytes external memory is use for keyblob data,
176  *       so this region shouldn't be in OTFAD region.
177  */
178 status_t OTFAD_SetEncryptionConfig(OTFAD_Type *base, const otfad_encryption_config_t *config);
179 
180 /*!
181  * @brief OTFAD module get encryption configuration
182  *
183  * @param base  OTFAD base address.
184  * @param config  encryption configuration.
185  *
186  * Note: if enable keyblob process, the first 256 bytes external memory is use for keyblob data,
187  *       so this region shouldn't be in OTFAD region.
188  */
189 status_t OTFAD_GetEncryptionConfig(OTFAD_Type *base, otfad_encryption_config_t *config);
190 
191 /*!
192  * @brief OTFAD module do hit determination.
193  *
194  * @param base  OTFAD base address.
195  * @param address  the physical address space assigned to the QuadSPI(FlexSPI) module.
196  * @param contextIndex  hitted context region index.
197  * @return  status, such as kStatus_Success or kStatus_OTFAD_ResRegAccessMode.
198  */
199 status_t OTFAD_HitDetermination(OTFAD_Type *base, uint32_t address, uint8_t *contextIndex);
200 
201 /*! @} */
202 
203 #if defined(__cplusplus)
204 }
205 #endif
206 
207 /*! @}*/
208 
209 #endif
210