1 /*
2 * Copyright (c) 2016, Freescale Semiconductor, Inc.
3 * Copyright 2017-2021 NXP
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9 #include "fsl_iee.h"
10
11 /*******************************************************************************
12 * Definitions
13 ******************************************************************************/
14
15 /* Component ID definition, used by tools. */
16 #ifndef FSL_COMPONENT_ID
17 #define FSL_COMPONENT_ID "platform.drivers.iee"
18 #endif
19
20 /*******************************************************************************
21 * Prototypes
22 ******************************************************************************/
23
24 /*******************************************************************************
25 * Code
26 ******************************************************************************/
27 /*!
28 * brief Resets IEE module to factory default values.
29 *
30 * This function performs hardware reset of IEE module. Attributes and keys of all regions are cleared.
31 *
32 * param base IEER peripheral address.
33 */
IEE_Init(IEE_Type * base)34 void IEE_Init(IEE_Type *base)
35 {
36 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
37 /* Enable IEE clock. */
38 CLOCK_EnableClock(kCLOCK_Iee);
39 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
40
41 /* Reset IEE module and wait the reset operation done. */
42 base->GCFG |= IEE_GCFG_RST_MASK;
43 }
44
45 /*!
46 * brief Loads default values to the IEE configuration structure.
47 *
48 * Loads default values to the IEE region configuration structure. The default values are as follows.
49 * code
50 * config->bypass = kIEE_AesUseMdField;
51 * config->mode = kIEE_ModeNone;
52 * config->keySize = kIEE_AesCTR128XTS256;
53 * config->pageOffset = 0U;
54 * endcode
55 *
56 * param config Configuration for the selected IEE region.
57 */
IEE_GetDefaultConfig(iee_config_t * config)58 void IEE_GetDefaultConfig(iee_config_t *config)
59 {
60 /* Initializes the configure structure to zero. */
61 (void)memset(config, 0, sizeof(*config));
62
63 config->bypass = kIEE_AesUseMdField;
64 config->mode = kIEE_ModeNone;
65 config->keySize = kIEE_AesCTR128XTS256;
66 config->pageOffset = 0U;
67 }
68
69 /*!
70 * brief Sets the IEE module according to the configuration structure.
71 *
72 * This function configures IEE region according to configuration structure.
73 *
74 * param base IEE peripheral address.
75 * param region Selection of the IEE region to be configured.
76 * param config Configuration for the selected IEE region.
77 */
IEE_SetRegionConfig(IEE_Type * base,iee_region_t region,iee_config_t * config)78 void IEE_SetRegionConfig(IEE_Type *base, iee_region_t region, iee_config_t *config)
79 {
80 base->REGX[region].REGATTR =
81 IEE_REGATTR_BYP(config->bypass) | IEE_REGATTR_MD(config->mode) | IEE_REGATTR_KS(config->keySize);
82 #if (defined(FSL_IEE_USE_PAGE_OFFSET) && (FSL_IEE_USE_PAGE_OFFSET > 0U))
83 base->REGX[region].REGPO = IEE_REGPO_PGOFF(config->pageOffset);
84 #endif /* FSL_IEE_USE_PAGE_OFFSET */
85 }
86
87 /*!
88 * brief Sets the IEE module key.
89 *
90 * This function sets specified AES key for the given region.
91 *
92 * param base IEE peripheral address.
93 * param region Selection of the IEE region to be configured.
94 * param keyNum Selection of AES KEY1 or KEY2.
95 * param key AES key.
96 * param keySize Size of AES key.
97 */
IEE_SetRegionKey(IEE_Type * base,iee_region_t region,iee_aes_key_num_t keyNum,const uint8_t * key,size_t keySize)98 status_t IEE_SetRegionKey(
99 IEE_Type *base, iee_region_t region, iee_aes_key_num_t keyNum, const uint8_t *key, size_t keySize)
100 {
101 register const uint32_t *from32 = (const uint32_t *)(uintptr_t)key;
102 register volatile uint32_t *to32 = NULL;
103
104 if (keyNum == kIEE_AesKey1)
105 {
106 to32 = &base->REGX[region].REGKEY1[0];
107 }
108
109 else if (keyNum == kIEE_AesKey2)
110 {
111 to32 = &base->REGX[region].REGKEY2[0];
112 }
113 else
114 {
115 return kStatus_InvalidArgument;
116 }
117
118 while (keySize >= sizeof(uint32_t))
119 {
120 *to32 = *from32;
121 keySize -= sizeof(uint32_t);
122 from32++;
123 to32++;
124 }
125
126 return kStatus_Success;
127 }
128
129 /*!
130 * brief Lock the IEE region configuration.
131 *
132 * IEE region Key, Offset and Attribute registers are locked.
133 * Only system reset can clear the Lock bit.
134 *
135 * param base IEE peripheral address.
136 * param region Selection of the IEE region to be locked.
137 */
IEE_LockRegionConfig(IEE_Type * base,iee_region_t region)138 void IEE_LockRegionConfig(IEE_Type *base, iee_region_t region)
139 {
140 base->GCFG |= (uint32_t)(0x1UL << (uint32_t)region);
141 }
142