hpm_bkey_drv.h 2.3 KB

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