1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <device/mmio.h>
5 #include <console/console.h>
7 #include <soc/addressmap.h>
12 enum rk3288_crypto_interrupt_bits
{
21 struct rk3288_crypto
{
31 u8 _res0
[0x80 - 0x24];
39 u8 _res1
[0x100 - 0xe8];
46 u8 _res2
[0x180 - 0x138];
52 u8 _res3
[0x200 - 0x1c0];
55 } *crypto
= (void *)CRYPTO_BASE
;
56 check_member(rk3288_crypto
, trng_dout
[7], 0x220);
58 vb2_error_t
vb2ex_hwcrypto_digest_init(enum vb2_hash_algorithm hash_alg
,
61 if (hash_alg
!= VB2_HASH_SHA256
|| !data_size
)
62 return VB2_ERROR_EX_HWCRYPTO_UNSUPPORTED
;
64 write32(&crypto
->ctrl
, RK_SETBITS(1 << 6)); /* Assert HASH_FLUSH */
65 udelay(1); /* for 10+ cycles to */
66 write32(&crypto
->ctrl
, RK_CLRBITS(1 << 6)); /* clear out old hash */
68 /* Enable DMA byte swapping for little-endian bus (Byteswap_??FIFO) */
69 write32(&crypto
->conf
, 1 << 5 | 1 << 4 | 1 << 3);
71 write32(&crypto
->intena
, HRDMA_ERR
| HRDMA_DONE
); /* enable interrupt */
73 write32(&crypto
->hash_msg_len
, data_size
); /* program total size */
74 write32(&crypto
->hash_ctrl
, 1 << 3 | 0x2); /* swap DOUT, SHA256 */
76 printk(BIOS_DEBUG
, "Initialized RK3288 HW crypto for %u byte SHA256\n",
81 vb2_error_t
vb2ex_hwcrypto_digest_extend(const uint8_t *buf
, uint32_t size
)
85 write32(&crypto
->intsts
, HRDMA_ERR
| HRDMA_DONE
); /* clear interrupts */
87 /* NOTE: This assumes that the DMA is reading from uncached SRAM. */
88 write32(&crypto
->hrdmas
, (uint32_t)buf
);
89 write32(&crypto
->hrdmal
, size
/ sizeof(uint32_t));
90 write32(&crypto
->ctrl
, RK_SETBITS(1 << 3)); /* Set HASH_START */
92 intsts
= read32(&crypto
->intsts
);
93 if (intsts
& HRDMA_ERR
) {
94 printk(BIOS_ERR
, "DMA error during HW crypto\n");
95 return VB2_ERROR_UNKNOWN
;
97 } while (!(intsts
& HRDMA_DONE
)); /* wait for DMA to finish */
102 vb2_error_t
vb2ex_hwcrypto_digest_finalize(uint8_t *digest
,
103 uint32_t digest_size
)
105 uint32_t *dest
= (uint32_t *)digest
;
106 uint32_t *src
= crypto
->hash_dout
;
107 assert(digest_size
== sizeof(crypto
->hash_dout
));
109 while (!(read32(&crypto
->hash_sts
) & 0x1))
110 /* wait for crypto engine to set HASH_DONE bit */;
112 while ((uint8_t *)dest
< digest
+ digest_size
)
113 *dest
++ = read32(src
++);