Skip to content

Commit a6af0fe

Browse files
committed
drivers: crypto: Add initial support for rpi_pico sha256 accelerator
Add basic support for RaspberryPi Pico's SHA256 hardware accelerator. Signed-off-by: TOKITA Hiroshi <[email protected]>
1 parent 7283565 commit a6af0fe

File tree

6 files changed

+184
-0
lines changed

6 files changed

+184
-0
lines changed

drivers/crypto/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ zephyr_library_sources_ifdef(CONFIG_CRYPTO_MCUX_DCP crypto_mcux_dcp.c)
1919
zephyr_library_sources_ifdef(CONFIG_CRYPTO_NPCX_SHA crypto_npcx_sha.c)
2020
zephyr_library_sources_ifdef(CONFIG_CRYPTO_NRF_ECB crypto_nrf_ecb.c)
2121
zephyr_library_sources_ifdef(CONFIG_CRYPTO_NXP_S32_HSE crypto_nxp_s32_hse.c)
22+
zephyr_library_sources_ifdef(CONFIG_CRYPTO_RPI_PICO_SHA256 crypto_rpi_pico_sha256.c)
2223
zephyr_library_sources_ifdef(CONFIG_CRYPTO_RTS5912_SHA crypto_rts5912_sha.c)
2324
zephyr_library_sources_ifdef(CONFIG_CRYPTO_SI32 crypto_si32.c)
2425
zephyr_library_sources_ifdef(CONFIG_CRYPTO_SMARTBOND crypto_smartbond.c)

drivers/crypto/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ source "drivers/crypto/Kconfig.mcux_dcp"
6666
source "drivers/crypto/Kconfig.npcx"
6767
source "drivers/crypto/Kconfig.nrf_ecb"
6868
source "drivers/crypto/Kconfig.nxp_s32_hse"
69+
source "drivers/crypto/Kconfig.rpi_pico"
6970
source "drivers/crypto/Kconfig.rts5912"
7071
source "drivers/crypto/Kconfig.si32"
7172
source "drivers/crypto/Kconfig.smartbond"

drivers/crypto/Kconfig.rpi_pico

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) 2025 TOKITA Hiroshi
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config CRYPTO_RPI_PICO_SHA256
5+
bool "Raspberry Pi RP2 series SHA-256 Accelerator"
6+
default y
7+
depends on DT_HAS_RASPBERRYPI_PICO_SHA256_ENABLED
8+
select PICOSDK_USE_SHA256
9+
help
10+
Enable driver for RP2 series SHA-256 accelerator
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
* Copyright (c) 2025 TOKITA Hiroshi
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#define DT_DRV_COMPAT raspberrypi_pico_sha256
8+
9+
#include <zephyr/crypto/crypto.h>
10+
#include <zephyr/kernel.h>
11+
#include <zephyr/sys/util_macro.h>
12+
#include <zephyr/sys/byteorder.h>
13+
14+
#include <pico/bootrom/lock.h>
15+
#include <pico/sha256.h>
16+
17+
#include <zephyr/logging/log.h>
18+
LOG_MODULE_REGISTER(crypto_rpi_pico_sha256, CONFIG_CRYPTO_LOG_LEVEL);
19+
20+
struct crypto_rpi_pico_sha256_data {
21+
pico_sha256_state_t state;
22+
struct k_mutex mutex;
23+
};
24+
25+
static int crypto_rpi_pico_sha256_hash_handler(struct hash_ctx *ctx, struct hash_pkt *pkt,
26+
bool finish)
27+
{
28+
struct crypto_rpi_pico_sha256_data *data = ctx->device->data;
29+
int ret;
30+
31+
ret = k_mutex_lock(&data->mutex, K_FOREVER);
32+
if (ret != 0) {
33+
LOG_ERR("Failed to lock mutex: %d", ret);
34+
return ret;
35+
}
36+
37+
if (!data->state.locked) {
38+
LOG_ERR("Invalid lock status: unlocked");
39+
ret = -EINVAL;
40+
goto end;
41+
}
42+
43+
data->state.cache_used = 0;
44+
data->state.cache.word = 0;
45+
data->state.total_data_size = 0;
46+
47+
sha256_err_not_ready_clear();
48+
sha256_set_bswap(true);
49+
sha256_start();
50+
51+
pico_sha256_update(&data->state, pkt->in_buf, pkt->in_len);
52+
53+
if (!finish) {
54+
LOG_ERR("Multipart hashing not supported yet");
55+
ret = -ENOTSUP;
56+
goto end;
57+
}
58+
59+
pico_sha256_write_padding(&data->state);
60+
sha256_wait_valid_blocking();
61+
62+
for (uint i = 0; i < 8; i++) {
63+
((uint32_t *)pkt->out_buf)[i] = BSWAP_32((uint32_t)sha256_hw->sum[i]);
64+
}
65+
66+
ret = 0;
67+
68+
end:
69+
k_mutex_unlock(&data->mutex);
70+
71+
return ret;
72+
}
73+
74+
static int crypto_rpi_pico_sha256_query_hw_caps(const struct device *dev)
75+
{
76+
return CAP_SEPARATE_IO_BUFS | CAP_SYNC_OPS;
77+
}
78+
79+
static int crypto_rpi_pico_sha256_hash_begin_session(const struct device *dev, struct hash_ctx *ctx,
80+
enum hash_algo algo)
81+
{
82+
struct crypto_rpi_pico_sha256_data *data = dev->data;
83+
int ret;
84+
85+
if (algo != CRYPTO_HASH_ALGO_SHA256) {
86+
LOG_ERR("Unsupported algo: %d", algo);
87+
return -EINVAL;
88+
}
89+
90+
if (ctx->flags & ~(crypto_rpi_pico_sha256_query_hw_caps(dev))) {
91+
LOG_ERR("Unsupported flag %x", ctx->flags);
92+
return -EINVAL;
93+
}
94+
95+
ret = k_mutex_lock(&data->mutex, K_FOREVER);
96+
if (ret != 0) {
97+
LOG_ERR("Failed to lock mutex: %d", ret);
98+
return ret;
99+
}
100+
101+
if (!bootrom_try_acquire_lock(BOOTROM_LOCK_SHA_256)) {
102+
LOG_ERR("bootrom_try_acquire_lock failed");
103+
ret = -EBUSY;
104+
goto end;
105+
}
106+
107+
data->state.locked = true;
108+
ctx->hash_hndlr = crypto_rpi_pico_sha256_hash_handler;
109+
ret = 0;
110+
111+
end:
112+
k_mutex_unlock(&data->mutex);
113+
114+
return ret;
115+
}
116+
117+
static int crypto_rpi_pico_sha256_hash_session_free(const struct device *dev, struct hash_ctx *ctx)
118+
{
119+
struct crypto_rpi_pico_sha256_data *data = dev->data;
120+
int ret;
121+
122+
ret = k_mutex_lock(&data->mutex, K_FOREVER);
123+
if (ret != 0) {
124+
LOG_ERR("Failed to lock mutex: %d", ret);
125+
return ret;
126+
}
127+
128+
bootrom_release_lock(BOOTROM_LOCK_SHA_256);
129+
data->state.locked = false;
130+
ret = 0;
131+
132+
k_mutex_unlock(&data->mutex);
133+
134+
return ret;
135+
}
136+
137+
static int crypto_rpi_pico_sha256_init(const struct device *dev)
138+
{
139+
struct crypto_rpi_pico_sha256_data *data = dev->data;
140+
141+
k_mutex_init(&data->mutex);
142+
143+
return 0;
144+
}
145+
146+
static DEVICE_API(crypto, crypto_rpi_pico_sha256_crypto_api) = {
147+
.query_hw_caps = crypto_rpi_pico_sha256_query_hw_caps,
148+
.hash_begin_session = crypto_rpi_pico_sha256_hash_begin_session,
149+
.hash_free_session = crypto_rpi_pico_sha256_hash_session_free,
150+
};
151+
152+
#define CRYPTO_RPI_PICO_SHA256_INIT(idx) \
153+
static struct crypto_rpi_pico_sha256_data crypto_rpi_pico_sha256_##idx##_data; \
154+
DEVICE_DT_INST_DEFINE(idx, crypto_rpi_pico_sha256_init, NULL, \
155+
&crypto_rpi_pico_sha256_##idx##_data, NULL, POST_KERNEL, \
156+
CONFIG_CRYPTO_INIT_PRIORITY, &crypto_rpi_pico_sha256_crypto_api);
157+
158+
DT_INST_FOREACH_STATUS_OKAY(CRYPTO_RPI_PICO_SHA256_INIT)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) 2025 TOKITA Hiroshi
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: RaspberryPi Pico SHA-256 accelerator
5+
6+
compatible: "raspberrypi,pico-sha256"
7+
8+
include: base.yaml

dts/vendor/raspberrypi/rpi_pico/rp2350.dtsi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,12 @@
395395
status = "disabled";
396396
};
397397

398+
sha256: sha256@400f8000 {
399+
compatible = "raspberrypi,pico-sha256";
400+
reg = <0x400f8000 0x200>;
401+
status = "disabled";
402+
};
403+
398404
dma: dma@50000000 {
399405
compatible = "raspberrypi,pico-dma";
400406
reg = <0x50000000 DT_SIZE_K(64)>;

0 commit comments

Comments
 (0)