1 /*--------------------------------------------------------------------------*/
2 /* Copyright 2020 NXP                                                       */
3 /*                                                                          */
4 /* NXP Confidential. This software is owned or controlled by NXP and may    */
5 /* only be used strictly in accordance with the applicable license terms.   */
6 /* By expressly accepting such terms or by downloading, installing,         */
7 /* activating and/or otherwise using the software, you are agreeing that    */
8 /* you have read, and that you agree to comply with and are bound by, such  */
9 /* license terms. If you do not agree to be bound by the applicable license */
10 /* terms, then you may not retain, install, activate or otherwise use the   */
11 /* software.                                                                */
12 /*--------------------------------------------------------------------------*/
13 
14 /** @file  mcuxClCss_Internal.h
15  *  @brief Provide macros for mcuxClCss internal use.
16  * This header declares internal macros to deduplicate code and support for internal use only. */
17 
18 #ifndef MCUXCLCSS_INTERNAL_H_
19 #define MCUXCLCSS_INTERNAL_H_
20 
21 #include <platform_specific_headers.h>
22 
23 /** Asserts the correctness of the supplied parameters*/
24 #define MCUXCLCSS_INPUT_PARAM_CHECK(x) if((x)) { return MCUXCLCSS_STATUS_SW_INVALID_PARAM; }
25 #define MCUXCLCSS_INPUT_PARAM_CHECK_PROTECTED(funcid, x) if((x)) { MCUX_CSSL_FP_FUNCTION_EXIT(funcid, MCUXCLCSS_STATUS_SW_INVALID_PARAM); }
26 
27 #define MCUXCLCSS_REVERSEFETCH_ENABLE         ((uint32_t) 1U) ///< Reverse fetch enabled
28 #define MCUXCLCSS_REVERSEFETCH_DISABLE        ((uint32_t) 0U) ///< Reverse fetch disabled
29 
30 #define CSS_CMD_BIG_ENDIAN ((uint8_t) 0x01U)    ///< CSS command option specifying big-endian byte order
31 #define CSS_CMD_LITTLE_ENDIAN ((uint8_t) 0x00U) ///< CSS command option specifying little-endian byte order
32 
33 // Utility code of mcuxClCss implementation
34 
35 /** Tests if the CSS is in BUSY state.
36  * @retval @c true if the CSS is in BUSY state */
37 #define MCUXCLCSS_ISBUSY (1U == IP_CSS->CSS_STATUS_b.CSS_BUSY)
38 
39 /** Sets the variable-size input buffer from which the input 0 of the CSS operation will be transferred via DMA. */
40 #define MCUXCLCSS_SETCSSINPUT0(pInput, inputSize)                          \
41 {                                                                         \
42     IP_CSS->CSS_DMA_SRC0_b.ADDR_SRC0 = (uint32_t) ((uintptr_t) (pInput)); \
43     IP_CSS->CSS_DMA_SRC0_LEN_b.SIZE_SRC0_LEN = (uint32_t) (inputSize);    \
44 }
45 
46 /** Sets the fixed-size input buffer from which the input 0 of the CSS operation will be transferred via DMA. */
47 #define MCUXCLCSS_SETCSSINPUT0_FIXEDSIZE(pInput)                           \
48 {                                                                         \
49     IP_CSS->CSS_DMA_SRC0_b.ADDR_SRC0 = (uint32_t) ((uintptr_t) (pInput)); \
50 }
51 
52 /** Sets the variable-size input buffer from which the input 1 of the CSS operation will be transferred via DMA. */
53 #define MCUXCLCSS_SETCSSINPUT1(pInput, inputSize)                          \
54 {                                                                         \
55     IP_CSS->CSS_DMA_SRC2_b.ADDR_SRC1 = (uint32_t) ((uintptr_t) (pInput)); \
56     IP_CSS->CSS_DMA_SRC2_LEN_b.SIZE_SRC1_LEN = (uint32_t) (inputSize);    \
57 }
58 
59 /** Sets the fixed-size input buffer from which the input 1 of the CSS operation will be transferred via DMA. */
60 #define MCUXCLCSS_SETCSSINPUT1_FIXEDSIZE(pInput)                           \
61 {                                                                         \
62     IP_CSS->CSS_DMA_SRC1_b.ADDR_SRC1 = (uint32_t) ((uintptr_t) (pInput)); \
63 }
64 
65 /** Sets the variable-size input buffer from which the input 2 of the CSS operation will be transferred via DMA. */
66 #define MCUXCLCSS_SETCSSINPUT2(pInput, inputSize)                          \
67 {                                                                         \
68     IP_CSS->CSS_DMA_SRC2_b.ADDR_SRC2 = (uint32_t) ((uintptr_t) (pInput)); \
69     IP_CSS->CSS_DMA_SRC2_LEN_b.SIZE_SRC2_LEN = (uint32_t) (inputSize);    \
70 }
71 
72 /** Sets the fixed-size input buffer from which the input 2 of the CSS operation will be transferred via DMA. */
73 #define MCUXCLCSS_SETCSSINPUT2_FIXEDSIZE(pInput)                           \
74 {                                                                         \
75     IP_CSS->CSS_DMA_SRC2_b.ADDR_SRC2 = (uint32_t) ((uintptr_t) (pInput)); \
76 }
77 
78 /** Sets the variable-size output buffer to which the result of the CSS operation will be transferred via DMA. */
79 #define MCUXCLCSS_SETCSSOUTPUT(pOutput, outputSize)                         \
80 {                                                                          \
81     IP_CSS->CSS_DMA_RES0_b.ADDR_RES0 = (uint32_t) ((uintptr_t) (pOutput)); \
82     IP_CSS->CSS_DMA_RES0_LEN = (uint32_t) (outputSize);                    \
83 }
84 
85 /** Sets the output buffer to which the result of the CSS operation will be transferred via DMA. */
86 #define MCUXCLCSS_SETCSSOUTPUT_FIXEDSIZE(pOutput)                           \
87 {                                                                          \
88     IP_CSS->CSS_DMA_RES0_b.ADDR_RES0 = (uint32_t) ((uintptr_t) (pOutput)); \
89 }
90 
91 /** Sets the CSS keystore index 0, for commands that access a single key. */
92 #define MCUXCLCSS_SETKEYSTOREINDEX0(index)          \
93 {                                                  \
94     IP_CSS->CSS_KIDX0_b.KIDX0 = (uint8_t) (index); \
95 }
96 
97 /** Sets the CSS keystore index 1, for commands that access 2 keys. */
98 #define MCUXCLCSS_SETKEYSTOREINDEX1(index)          \
99 {                                                  \
100     IP_CSS->CSS_KIDX1_b.KIDX1 = (uint8_t) (index); \
101 }
102 
103 #ifdef CSS_KIDX2_KIDX2_Pos
104 /** Sets the CSS keystore index 2, for commands that access 3 keys. */
105 #define MCUXCLCSS_SETKEYSTOREINDEX2(index)          \
106 {                                                  \
107     IP_CSS->CSS_KIDX2_b.KIDX2 = (uint8_t) (index); \
108 }
109 #endif /* CSS_KIDX2_KIDX2_Pos */
110 
111 /** Sets the CSS requested key properties, for commands that create a key. */
112 #define MCUXCLCSS_SETREQUESTEDKEYPROPERTIES(properties)       \
113 {                                                            \
114     IP_CSS->CSS_KPROPIN_b.KPROPIN = (properties).word.value; \
115 }
116 
117 /** Starts a CSS command. */
118 #define MCUXCLCSS_STARTCOMMAND(command, cmdcfg0, byteOrder)       \
119 {                                                                \
120     IP_CSS->CSS_CMDCFG0_b.CMDCFG0 = (cmdcfg0);                   \
121     uint32_t ctrl = 0U;                                          \
122     ctrl |= ((uint32_t) (command)) << CSS_CTRL_CSS_CMD_Pos;      \
123     ctrl |= 0x01U << CSS_CTRL_CSS_START_Pos;                     \
124     ctrl |= 0x01U << CSS_CTRL_CSS_EN_Pos;                        \
125     ctrl |= ((uint32_t) (byteOrder)) << CSS_CTRL_BYTE_ORDER_Pos; \
126     IP_CSS->CSS_CTRL = ctrl;                                     \
127 }
128 
129 #endif /* MCUXCLCSS_INTERNAL_H_ */
130