summaryrefslogtreecommitdiffstats
path: root/bsps/arm/imxrt/mcux-sdk/drivers/iee/fsl_iee.c
blob: 43ae8f16eaa0d83c9340c5a57f5840228b2f4522 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * Copyright (c) 2016, Freescale Semiconductor, Inc.
 * Copyright 2017-2021 NXP
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "fsl_iee.h"

/*******************************************************************************
 * Definitions
 ******************************************************************************/

/* Component ID definition, used by tools. */
#ifndef FSL_COMPONENT_ID
#define FSL_COMPONENT_ID "platform.drivers.iee"
#endif

/*******************************************************************************
 * Prototypes
 ******************************************************************************/

/*******************************************************************************
 * Code
 ******************************************************************************/
/*!
 * brief Resets IEE module to factory default values.
 *
 * This function performs hardware reset of IEE module. Attributes and keys of all regions are cleared.
 *
 * param base IEER peripheral address.
 */
void IEE_Init(IEE_Type *base)
{
#if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
    /* Enable IEE clock. */
    CLOCK_EnableClock(kCLOCK_Iee);
#endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */

    /* Reset IEE module and wait the reset operation done. */
    base->GCFG |= IEE_GCFG_RST_MASK;
}

/*!
 * brief Loads default values to the IEE configuration structure.
 *
 * Loads default values to the IEE region configuration structure. The default values are as follows.
 * code
 *   config->bypass = kIEE_AesUseMdField;
 *   config->mode = kIEE_ModeNone;
 *   config->keySize = kIEE_AesCTR128XTS256;
 *   config->pageOffset = 0U;
 * endcode
 *
 * param config Configuration for the selected IEE region.
 */
void IEE_GetDefaultConfig(iee_config_t *config)
{
    /* Initializes the configure structure to zero. */
    (void)memset(config, 0, sizeof(*config));

    config->bypass     = kIEE_AesUseMdField;
    config->mode       = kIEE_ModeNone;
    config->keySize    = kIEE_AesCTR128XTS256;
    config->pageOffset = 0U;
}

/*!
 * brief Sets the IEE module according to the configuration structure.
 *
 * This function configures IEE region according to configuration structure.
 *
 * param base IEE peripheral address.
 * param region Selection of the IEE region to be configured.
 * param config Configuration for the selected IEE region.
 */
void IEE_SetRegionConfig(IEE_Type *base, iee_region_t region, iee_config_t *config)
{
    base->REGX[region].REGATTR =
        IEE_REGATTR_BYP(config->bypass) | IEE_REGATTR_MD(config->mode) | IEE_REGATTR_KS(config->keySize);
#if (defined(FSL_IEE_USE_PAGE_OFFSET) && (FSL_IEE_USE_PAGE_OFFSET > 0U))
    base->REGX[region].REGPO = IEE_REGPO_PGOFF(config->pageOffset);
#endif /* FSL_IEE_USE_PAGE_OFFSET */
}

/*!
 * brief Sets the IEE module key.
 *
 * This function sets specified AES key for the given region.
 *
 * param base IEE peripheral address.
 * param region Selection of the IEE region to be configured.
 * param keyNum Selection of AES KEY1 or KEY2.
 * param key AES key.
 * param keySize Size of AES key.
 */
status_t IEE_SetRegionKey(
    IEE_Type *base, iee_region_t region, iee_aes_key_num_t keyNum, const uint8_t *key, size_t keySize)
{
    register const uint32_t *from32  = (const uint32_t *)(uintptr_t)key;
    register volatile uint32_t *to32 = NULL;

    if (keyNum == kIEE_AesKey1)
    {
        to32 = &base->REGX[region].REGKEY1[0];
    }

    else if (keyNum == kIEE_AesKey2)
    {
        to32 = &base->REGX[region].REGKEY2[0];
    }
    else
    {
        return kStatus_InvalidArgument;
    }

    while (keySize >= sizeof(uint32_t))
    {
        *to32 = *from32;
        keySize -= sizeof(uint32_t);
        from32++;
        to32++;
    }

    return kStatus_Success;
}

/*!
 * brief Lock the IEE region configuration.
 *
 * IEE region Key, Offset and Attribute registers are locked.
 * Only system reset can clear the Lock bit.
 *
 * param base IEE peripheral address.
 * param region Selection of the IEE region to be locked.
 */
void IEE_LockRegionConfig(IEE_Type *base, iee_region_t region)
{
    base->GCFG |= (uint32_t)(0x1UL << (uint32_t)region);
}