etzpc.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (c) 2017-2022, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <stdint.h>
  9. #include <arch_helpers.h>
  10. #include <common/debug.h>
  11. #include <drivers/st/etzpc.h>
  12. #include <dt-bindings/soc/st,stm32-etzpc.h>
  13. #include <lib/mmio.h>
  14. #include <lib/utils_def.h>
  15. #include <libfdt.h>
  16. #include <platform_def.h>
  17. /* Device Tree related definitions */
  18. #define ETZPC_COMPAT "st,stm32-etzpc"
  19. #define ETZPC_LOCK_MASK 0x1U
  20. #define ETZPC_MODE_SHIFT 8
  21. #define ETZPC_MODE_MASK GENMASK(1, 0)
  22. #define ETZPC_ID_SHIFT 16
  23. #define ETZPC_ID_MASK GENMASK(7, 0)
  24. /* ID Registers */
  25. #define ETZPC_TZMA0_SIZE 0x000U
  26. #define ETZPC_DECPROT0 0x010U
  27. #define ETZPC_DECPROT_LOCK0 0x030U
  28. #define ETZPC_HWCFGR 0x3F0U
  29. #define ETZPC_VERR 0x3F4U
  30. /* ID Registers fields */
  31. #define ETZPC_TZMA0_SIZE_LOCK BIT(31)
  32. #define ETZPC_DECPROT0_MASK GENMASK(1, 0)
  33. #define ETZPC_HWCFGR_NUM_TZMA_SHIFT 0
  34. #define ETZPC_HWCFGR_NUM_PER_SEC_SHIFT 8
  35. #define ETZPC_HWCFGR_NUM_AHB_SEC_SHIFT 16
  36. #define ETZPC_HWCFGR_CHUNCKS1N4_SHIFT 24
  37. #define DECPROT_SHIFT 1
  38. #define IDS_PER_DECPROT_REGS 16U
  39. #define IDS_PER_DECPROT_LOCK_REGS 32U
  40. /*
  41. * etzpc_instance.
  42. * base : register base address set during init given by user
  43. * chunk_size : supported TZMA size steps
  44. * num_tzma: number of TZMA zone read from register at init
  45. * num_ahb_sec : number of securable AHB master zone read from register
  46. * num_per_sec : number of securable AHB & APB Peripherals read from register
  47. * revision : IP revision read from register at init
  48. */
  49. struct etzpc_instance {
  50. uintptr_t base;
  51. uint8_t chunck_size;
  52. uint8_t num_tzma;
  53. uint8_t num_per_sec;
  54. uint8_t num_ahb_sec;
  55. uint8_t revision;
  56. };
  57. /* Only 1 instance of the ETZPC is expected per platform */
  58. static struct etzpc_instance etzpc_dev;
  59. /*
  60. * Implementation uses uint8_t to store each securable DECPROT configuration.
  61. * When resuming from deep suspend, the DECPROT configurations are restored.
  62. */
  63. #define PERIPH_LOCK_BIT BIT(7)
  64. #define PERIPH_ATTR_MASK GENMASK(2, 0)
  65. #if ENABLE_ASSERTIONS
  66. static bool valid_decprot_id(unsigned int id)
  67. {
  68. return id < (unsigned int)etzpc_dev.num_per_sec;
  69. }
  70. static bool valid_tzma_id(unsigned int id)
  71. {
  72. return id < (unsigned int)etzpc_dev.num_tzma;
  73. }
  74. #endif
  75. /*
  76. * etzpc_configure_decprot : Load a DECPROT configuration
  77. * decprot_id : ID of the IP
  78. * decprot_attr : Restriction access attribute
  79. */
  80. void etzpc_configure_decprot(uint32_t decprot_id,
  81. enum etzpc_decprot_attributes decprot_attr)
  82. {
  83. uintptr_t offset = 4U * (decprot_id / IDS_PER_DECPROT_REGS);
  84. uint32_t shift = (decprot_id % IDS_PER_DECPROT_REGS) << DECPROT_SHIFT;
  85. uint32_t masked_decprot = (uint32_t)decprot_attr & ETZPC_DECPROT0_MASK;
  86. assert(valid_decprot_id(decprot_id));
  87. mmio_clrsetbits_32(etzpc_dev.base + ETZPC_DECPROT0 + offset,
  88. (uint32_t)ETZPC_DECPROT0_MASK << shift,
  89. masked_decprot << shift);
  90. }
  91. /*
  92. * etzpc_get_decprot : Get the DECPROT attribute
  93. * decprot_id : ID of the IP
  94. * return : Attribute of this DECPROT
  95. */
  96. enum etzpc_decprot_attributes etzpc_get_decprot(uint32_t decprot_id)
  97. {
  98. uintptr_t offset = 4U * (decprot_id / IDS_PER_DECPROT_REGS);
  99. uint32_t shift = (decprot_id % IDS_PER_DECPROT_REGS) << DECPROT_SHIFT;
  100. uintptr_t base_decprot = etzpc_dev.base + offset;
  101. uint32_t value;
  102. assert(valid_decprot_id(decprot_id));
  103. value = (mmio_read_32(base_decprot + ETZPC_DECPROT0) >> shift) &
  104. ETZPC_DECPROT0_MASK;
  105. return (enum etzpc_decprot_attributes)value;
  106. }
  107. /*
  108. * etzpc_lock_decprot : Lock access to the DECPROT attribute
  109. * decprot_id : ID of the IP
  110. */
  111. void etzpc_lock_decprot(uint32_t decprot_id)
  112. {
  113. uintptr_t offset = 4U * (decprot_id / IDS_PER_DECPROT_LOCK_REGS);
  114. uint32_t shift = BIT(decprot_id % IDS_PER_DECPROT_LOCK_REGS);
  115. uintptr_t base_decprot = etzpc_dev.base + offset;
  116. assert(valid_decprot_id(decprot_id));
  117. mmio_write_32(base_decprot + ETZPC_DECPROT_LOCK0, shift);
  118. }
  119. /*
  120. * etzpc_configure_tzma : Configure the target TZMA read only size
  121. * tzma_id : ID of the memory
  122. * tzma_value : read-only size
  123. */
  124. void etzpc_configure_tzma(uint32_t tzma_id, uint16_t tzma_value)
  125. {
  126. assert(valid_tzma_id(tzma_id));
  127. mmio_write_32(etzpc_dev.base + ETZPC_TZMA0_SIZE +
  128. (sizeof(uint32_t) * tzma_id), tzma_value);
  129. }
  130. /*
  131. * etzpc_get_tzma : Get the target TZMA read only size
  132. * tzma_id : TZMA ID
  133. * return : Size of read only size
  134. */
  135. uint16_t etzpc_get_tzma(uint32_t tzma_id)
  136. {
  137. assert(valid_tzma_id(tzma_id));
  138. return (uint16_t)mmio_read_32(etzpc_dev.base + ETZPC_TZMA0_SIZE +
  139. (sizeof(uint32_t) * tzma_id));
  140. }
  141. /*
  142. * etzpc_lock_tzma : Lock the target TZMA
  143. * tzma_id : TZMA ID
  144. */
  145. void etzpc_lock_tzma(uint32_t tzma_id)
  146. {
  147. assert(valid_tzma_id(tzma_id));
  148. mmio_setbits_32(etzpc_dev.base + ETZPC_TZMA0_SIZE +
  149. (sizeof(uint32_t) * tzma_id), ETZPC_TZMA0_SIZE_LOCK);
  150. }
  151. /*
  152. * etzpc_get_lock_tzma : Return the lock status of the target TZMA
  153. * tzma_id : TZMA ID
  154. * return : True if TZMA is locked, false otherwise
  155. */
  156. bool etzpc_get_lock_tzma(uint32_t tzma_id)
  157. {
  158. uint32_t tzma_size;
  159. assert(valid_tzma_id(tzma_id));
  160. tzma_size = mmio_read_32(etzpc_dev.base + ETZPC_TZMA0_SIZE +
  161. (sizeof(uint32_t) * tzma_id));
  162. return (tzma_size & ETZPC_TZMA0_SIZE_LOCK) != 0;
  163. }
  164. /*
  165. * etzpc_get_num_per_sec : Return the DECPROT ID limit value
  166. */
  167. uint8_t etzpc_get_num_per_sec(void)
  168. {
  169. return etzpc_dev.num_per_sec;
  170. }
  171. /*
  172. * etzpc_get_revision : Return the ETZPC IP revision
  173. */
  174. uint8_t etzpc_get_revision(void)
  175. {
  176. return etzpc_dev.revision;
  177. }
  178. /*
  179. * etzpc_get_base_address : Return the ETZPC IP base address
  180. */
  181. uintptr_t etzpc_get_base_address(void)
  182. {
  183. return etzpc_dev.base;
  184. }
  185. /*
  186. * etzpc_init : Initialize the ETZPC driver
  187. * Return 0 on success and a negative errno on failure
  188. */
  189. int etzpc_init(void)
  190. {
  191. uint32_t hwcfg;
  192. etzpc_dev.base = STM32MP1_ETZPC_BASE;
  193. hwcfg = mmio_read_32(etzpc_dev.base + ETZPC_HWCFGR);
  194. etzpc_dev.num_tzma = (uint8_t)(hwcfg >> ETZPC_HWCFGR_NUM_TZMA_SHIFT);
  195. etzpc_dev.num_per_sec = (uint8_t)(hwcfg >>
  196. ETZPC_HWCFGR_NUM_PER_SEC_SHIFT);
  197. etzpc_dev.num_ahb_sec = (uint8_t)(hwcfg >>
  198. ETZPC_HWCFGR_NUM_AHB_SEC_SHIFT);
  199. etzpc_dev.chunck_size = (uint8_t)(hwcfg >>
  200. ETZPC_HWCFGR_CHUNCKS1N4_SHIFT);
  201. etzpc_dev.revision = mmio_read_8(etzpc_dev.base + ETZPC_VERR);
  202. VERBOSE("ETZPC version 0x%x", etzpc_dev.revision);
  203. return 0;
  204. }