1 /*
2 * Copyright (c) 2021 HPMicro
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8 #ifndef HPM_BKEY_DRV_H
9 #define HPM_BKEY_DRV_H
10
11 #include "hpm_common.h"
12 #include "hpm_bkey_regs.h"
13
14 /**
15 *
16 * @brief BKEY driver APIs
17 * @defgroup bkey_interface BKEY driver APIs
18 * @ingroup io_interfaces
19 * @{
20 */
21
22 /**
23 * @brief Lock type
24 */
25 typedef enum bkey_lock_type {
26 bkey_lock_write = BKEY_ECC_WLOCK_MASK,
27 bkey_lock_read = BKEY_ECC_RLOCK_MASK,
28 bkey_lock_both = BKEY_ECC_RLOCK_MASK | BKEY_ECC_WLOCK_MASK,
29 } bkey_lock_type_t;
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 /**
36 * @brief bkey set key content
37 *
38 * Program key content
39 *
40 * @param[in] ptr BKEY base address
41 * @param[in] key Key index
42 * @param[in] start Key content data start index
43 * @param[in] data pointer of actual data to be programmed
44 * @param[in] size data total size in 32-bit
45 */
bkey_set_key_data(BKEY_Type * ptr,uint8_t key,uint8_t start,uint32_t * data,uint8_t size)46 static inline void bkey_set_key_data(BKEY_Type *ptr, uint8_t key, uint8_t start, uint32_t *data, uint8_t size)
47 {
48 for (uint8_t i = 0; i < size; i++) {
49 ptr->KEY[key].DATA[start + i] = *(data + i);
50 }
51 }
52
53 /**
54 * @brief bkey fetch key content
55 *
56 * Fetch key content
57 *
58 * @param[in] ptr BKEY base address
59 * @param[in] key key index
60 * @param[in] start key content data start index
61 * @param[in] data pointer of buffer to received key content
62 * @param[in] size data total size in 32-bit
63 */
bkey_get_key_data(BKEY_Type * ptr,uint8_t key,uint8_t start,uint32_t * data,uint8_t size)64 static inline void bkey_get_key_data(BKEY_Type *ptr, uint8_t key, uint8_t start, uint32_t *data, uint8_t size)
65 {
66 for (uint8_t i = 0; i < size; i++) {
67 *(data + i) = ptr->KEY[key].DATA[start + i];
68 }
69 }
70
71 /**
72 * @brief bkey lock key
73 *
74 * Feed correct ecc data of current key content and lock it
75 *
76 * @param[in] ptr BKEY base address
77 * @param[in] key key index
78 * @param[in] lock lock type
79 * @param[in] ecc ecc value of current key content
80 */
bkey_lock(BKEY_Type * ptr,uint8_t key,bkey_lock_type_t lock,uint16_t ecc)81 static inline void bkey_lock(BKEY_Type *ptr, uint8_t key, bkey_lock_type_t lock, uint16_t ecc)
82 {
83 ptr->ECC[key] = BKEY_ECC_ECC_SET(ecc) | lock;
84 }
85
86 /**
87 * @brief bkey select key
88 *
89 * Select which key to use
90 *
91 * @param[in] ptr BKEY base address
92 * @param[in] key key index
93 * @arg 0 select key0 in secure mode, key1 in non-secure mode
94 * @arg 1 select key1 in secure or non-secure mode
95 */
bkey_select_key(BKEY_Type * ptr,uint8_t key)96 static inline void bkey_select_key(BKEY_Type *ptr, uint8_t key)
97 {
98 ptr->SELECT = BKEY_SELECT_SELECT_SET(key);
99 }
100
101 #ifdef __cplusplus
102 }
103 #endif
104 /**
105 * @}
106 */
107 #endif /* HPM_BKEY_DRV_H */
108