stm32_hash.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * Copyright (c) 2019-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 <common/sha_common_macros.h>
  12. #include <drivers/clk.h>
  13. #include <drivers/delay_timer.h>
  14. #include <drivers/st/stm32_hash.h>
  15. #include <drivers/st/stm32mp_reset.h>
  16. #include <lib/mmio.h>
  17. #include <lib/utils.h>
  18. #include <libfdt.h>
  19. #include <plat/common/platform.h>
  20. #include <platform_def.h>
  21. #if STM32_HASH_VER == 2
  22. #define DT_HASH_COMPAT "st,stm32f756-hash"
  23. #endif
  24. #if STM32_HASH_VER == 4
  25. #define DT_HASH_COMPAT "st,stm32mp13-hash"
  26. #endif
  27. #define HASH_CR 0x00U
  28. #define HASH_DIN 0x04U
  29. #define HASH_STR 0x08U
  30. #define HASH_SR 0x24U
  31. #define HASH_HREG(x) (0x310U + ((x) * 0x04U))
  32. /* Control Register */
  33. #define HASH_CR_INIT BIT(2)
  34. #define HASH_CR_DATATYPE_SHIFT U(4)
  35. #if STM32_HASH_VER == 2
  36. #define HASH_CR_ALGO_SHA1 0x0U
  37. #define HASH_CR_ALGO_MD5 BIT(7)
  38. #define HASH_CR_ALGO_SHA224 BIT(18)
  39. #define HASH_CR_ALGO_SHA256 (BIT(18) | BIT(7))
  40. #endif
  41. #if STM32_HASH_VER == 4
  42. #define HASH_CR_ALGO_SHIFT U(17)
  43. #define HASH_CR_ALGO_SHA1 (0x0U << HASH_CR_ALGO_SHIFT)
  44. #define HASH_CR_ALGO_SHA224 (0x2U << HASH_CR_ALGO_SHIFT)
  45. #define HASH_CR_ALGO_SHA256 (0x3U << HASH_CR_ALGO_SHIFT)
  46. #define HASH_CR_ALGO_SHA384 (0xCU << HASH_CR_ALGO_SHIFT)
  47. #define HASH_CR_ALGO_SHA512_224 (0xDU << HASH_CR_ALGO_SHIFT)
  48. #define HASH_CR_ALGO_SHA512_256 (0xEU << HASH_CR_ALGO_SHIFT)
  49. #define HASH_CR_ALGO_SHA512 (0xFU << HASH_CR_ALGO_SHIFT)
  50. #endif
  51. /* Status Flags */
  52. #define HASH_SR_DCIS BIT(1)
  53. #define HASH_SR_BUSY BIT(3)
  54. /* STR Register */
  55. #define HASH_STR_NBLW_MASK GENMASK(4, 0)
  56. #define HASH_STR_DCAL BIT(8)
  57. #define RESET_TIMEOUT_US_1MS 1000U
  58. #define HASH_TIMEOUT_US 10000U
  59. enum stm32_hash_data_format {
  60. HASH_DATA_32_BITS,
  61. HASH_DATA_16_BITS,
  62. HASH_DATA_8_BITS,
  63. HASH_DATA_1_BIT
  64. };
  65. struct stm32_hash_instance {
  66. uintptr_t base;
  67. unsigned int clock;
  68. size_t digest_size;
  69. };
  70. struct stm32_hash_remain {
  71. uint32_t buffer;
  72. size_t length;
  73. };
  74. /* Expect a single HASH peripheral */
  75. static struct stm32_hash_instance stm32_hash;
  76. static struct stm32_hash_remain stm32_remain;
  77. static uintptr_t hash_base(void)
  78. {
  79. return stm32_hash.base;
  80. }
  81. static int hash_wait_busy(void)
  82. {
  83. uint64_t timeout = timeout_init_us(HASH_TIMEOUT_US);
  84. while ((mmio_read_32(hash_base() + HASH_SR) & HASH_SR_BUSY) != 0U) {
  85. if (timeout_elapsed(timeout)) {
  86. ERROR("%s: busy timeout\n", __func__);
  87. return -ETIMEDOUT;
  88. }
  89. }
  90. return 0;
  91. }
  92. static int hash_wait_computation(void)
  93. {
  94. uint64_t timeout = timeout_init_us(HASH_TIMEOUT_US);
  95. while ((mmio_read_32(hash_base() + HASH_SR) & HASH_SR_DCIS) == 0U) {
  96. if (timeout_elapsed(timeout)) {
  97. ERROR("%s: busy timeout\n", __func__);
  98. return -ETIMEDOUT;
  99. }
  100. }
  101. return 0;
  102. }
  103. static int hash_write_data(uint32_t data)
  104. {
  105. int ret;
  106. ret = hash_wait_busy();
  107. if (ret != 0) {
  108. return ret;
  109. }
  110. mmio_write_32(hash_base() + HASH_DIN, data);
  111. return 0;
  112. }
  113. static void hash_hw_init(enum stm32_hash_algo_mode mode)
  114. {
  115. uint32_t reg;
  116. reg = HASH_CR_INIT | (HASH_DATA_8_BITS << HASH_CR_DATATYPE_SHIFT);
  117. switch (mode) {
  118. #if STM32_HASH_VER == 2
  119. case HASH_MD5SUM:
  120. reg |= HASH_CR_ALGO_MD5;
  121. stm32_hash.digest_size = MD5_DIGEST_SIZE;
  122. break;
  123. #endif
  124. case HASH_SHA1:
  125. reg |= HASH_CR_ALGO_SHA1;
  126. stm32_hash.digest_size = SHA1_DIGEST_SIZE;
  127. break;
  128. case HASH_SHA224:
  129. reg |= HASH_CR_ALGO_SHA224;
  130. stm32_hash.digest_size = SHA224_DIGEST_SIZE;
  131. break;
  132. #if STM32_HASH_VER == 4
  133. case HASH_SHA384:
  134. reg |= HASH_CR_ALGO_SHA384;
  135. stm32_hash.digest_size = SHA384_DIGEST_SIZE;
  136. break;
  137. case HASH_SHA512:
  138. reg |= HASH_CR_ALGO_SHA512;
  139. stm32_hash.digest_size = SHA512_DIGEST_SIZE;
  140. break;
  141. #endif
  142. /* Default selected algo is SHA256 */
  143. case HASH_SHA256:
  144. default:
  145. reg |= HASH_CR_ALGO_SHA256;
  146. stm32_hash.digest_size = SHA256_DIGEST_SIZE;
  147. break;
  148. }
  149. mmio_write_32(hash_base() + HASH_CR, reg);
  150. }
  151. static int hash_get_digest(uint8_t *digest)
  152. {
  153. int ret;
  154. uint32_t i;
  155. uint32_t dsg;
  156. ret = hash_wait_computation();
  157. if (ret != 0) {
  158. return ret;
  159. }
  160. for (i = 0U; i < (stm32_hash.digest_size / sizeof(uint32_t)); i++) {
  161. dsg = __builtin_bswap32(mmio_read_32(hash_base() +
  162. HASH_HREG(i)));
  163. memcpy(digest + (i * sizeof(uint32_t)), &dsg, sizeof(uint32_t));
  164. }
  165. /*
  166. * Clean hardware context as HASH could be used later
  167. * by non-secure software
  168. */
  169. hash_hw_init(HASH_SHA256);
  170. return 0;
  171. }
  172. int stm32_hash_update(const uint8_t *buffer, size_t length)
  173. {
  174. size_t remain_length = length;
  175. int ret = 0;
  176. if ((length == 0U) || (buffer == NULL)) {
  177. return 0;
  178. }
  179. clk_enable(stm32_hash.clock);
  180. if (stm32_remain.length != 0U) {
  181. uint32_t copysize;
  182. copysize = MIN((sizeof(uint32_t) - stm32_remain.length),
  183. length);
  184. memcpy(((uint8_t *)&stm32_remain.buffer) + stm32_remain.length,
  185. buffer, copysize);
  186. remain_length -= copysize;
  187. buffer += copysize;
  188. if (stm32_remain.length == sizeof(uint32_t)) {
  189. ret = hash_write_data(stm32_remain.buffer);
  190. if (ret != 0) {
  191. goto exit;
  192. }
  193. zeromem(&stm32_remain, sizeof(stm32_remain));
  194. }
  195. }
  196. while (remain_length / sizeof(uint32_t) != 0U) {
  197. uint32_t tmp_buf;
  198. memcpy(&tmp_buf, buffer, sizeof(uint32_t));
  199. ret = hash_write_data(tmp_buf);
  200. if (ret != 0) {
  201. goto exit;
  202. }
  203. buffer += sizeof(uint32_t);
  204. remain_length -= sizeof(uint32_t);
  205. }
  206. if (remain_length != 0U) {
  207. assert(stm32_remain.length == 0U);
  208. memcpy((uint8_t *)&stm32_remain.buffer, buffer, remain_length);
  209. stm32_remain.length = remain_length;
  210. }
  211. exit:
  212. clk_disable(stm32_hash.clock);
  213. return ret;
  214. }
  215. int stm32_hash_final(uint8_t *digest)
  216. {
  217. int ret;
  218. clk_enable(stm32_hash.clock);
  219. if (stm32_remain.length != 0U) {
  220. ret = hash_write_data(stm32_remain.buffer);
  221. if (ret != 0) {
  222. clk_disable(stm32_hash.clock);
  223. return ret;
  224. }
  225. mmio_clrsetbits_32(hash_base() + HASH_STR, HASH_STR_NBLW_MASK,
  226. 8U * stm32_remain.length);
  227. zeromem(&stm32_remain, sizeof(stm32_remain));
  228. } else {
  229. mmio_clrbits_32(hash_base() + HASH_STR, HASH_STR_NBLW_MASK);
  230. }
  231. mmio_setbits_32(hash_base() + HASH_STR, HASH_STR_DCAL);
  232. ret = hash_get_digest(digest);
  233. clk_disable(stm32_hash.clock);
  234. return ret;
  235. }
  236. int stm32_hash_final_update(const uint8_t *buffer, uint32_t length,
  237. uint8_t *digest)
  238. {
  239. int ret;
  240. ret = stm32_hash_update(buffer, length);
  241. if (ret != 0) {
  242. return ret;
  243. }
  244. return stm32_hash_final(digest);
  245. }
  246. void stm32_hash_init(enum stm32_hash_algo_mode mode)
  247. {
  248. clk_enable(stm32_hash.clock);
  249. hash_hw_init(mode);
  250. clk_disable(stm32_hash.clock);
  251. zeromem(&stm32_remain, sizeof(stm32_remain));
  252. }
  253. int stm32_hash_register(void)
  254. {
  255. struct dt_node_info hash_info;
  256. int node;
  257. for (node = dt_get_node(&hash_info, -1, DT_HASH_COMPAT);
  258. node != -FDT_ERR_NOTFOUND;
  259. node = dt_get_node(&hash_info, node, DT_HASH_COMPAT)) {
  260. if (hash_info.status != DT_DISABLED) {
  261. break;
  262. }
  263. }
  264. if (node == -FDT_ERR_NOTFOUND) {
  265. return -ENODEV;
  266. }
  267. if (hash_info.clock < 0) {
  268. return -EINVAL;
  269. }
  270. stm32_hash.base = hash_info.base;
  271. stm32_hash.clock = hash_info.clock;
  272. clk_enable(stm32_hash.clock);
  273. if (hash_info.reset >= 0) {
  274. uint32_t id = (uint32_t)hash_info.reset;
  275. if (stm32mp_reset_assert(id, RESET_TIMEOUT_US_1MS) != 0) {
  276. panic();
  277. }
  278. udelay(20);
  279. if (stm32mp_reset_deassert(id, RESET_TIMEOUT_US_1MS) != 0) {
  280. panic();
  281. }
  282. }
  283. clk_disable(stm32_hash.clock);
  284. return 0;
  285. }