Skip to content

Commit cd9c258

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 1090d22 commit cd9c258

File tree

6 files changed

+186
-1
lines changed

6 files changed

+186
-1
lines changed

drivers/crypto/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ zephyr_library_sources_ifdef(CONFIG_CRYPTO_MCHP_XEC_SYMCR crypto_mchp_xec_
1616
zephyr_library_sources_ifdef(CONFIG_CRYPTO_MCUX_DCP crypto_mcux_dcp.c)
1717
zephyr_library_sources_ifdef(CONFIG_CRYPTO_NPCX_SHA crypto_npcx_sha.c)
1818
zephyr_library_sources_ifdef(CONFIG_CRYPTO_NRF_ECB crypto_nrf_ecb.c)
19+
zephyr_library_sources_ifdef(CONFIG_CRYPTO_RPI_PICO_SHA256 crypto_rpi_pico_sha256.c)
1920
zephyr_library_sources_ifdef(CONFIG_CRYPTO_RTS5912_SHA crypto_rts5912_sha.c)
2021
zephyr_library_sources_ifdef(CONFIG_CRYPTO_SI32 crypto_si32.c)
2122
zephyr_library_sources_ifdef(CONFIG_CRYPTO_SMARTBOND crypto_smartbond.c)

drivers/crypto/Kconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ source "drivers/crypto/Kconfig.it8xxx2"
5757
source "drivers/crypto/Kconfig.mcux_dcp"
5858
source "drivers/crypto/Kconfig.npcx"
5959
source "drivers/crypto/Kconfig.nrf_ecb"
60+
source "drivers/crypto/Kconfig.rpi_pico"
61+
source "drivers/crypto/Kconfig.rts5912"
6062
source "drivers/crypto/Kconfig.si32"
6163
source "drivers/crypto/Kconfig.smartbond"
62-
source "drivers/crypto/Kconfig.rts5912"
6364
source "drivers/crypto/Kconfig.stm32"
6465
source "drivers/crypto/Kconfig.xec"
6566
# zephyr-keep-sorted-stop

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: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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+
30+
if (!data->state.locked) {
31+
LOG_ERR("Invalid lock status: unlocked");
32+
return -EINVAL;
33+
}
34+
35+
data->state.cache_used = 0;
36+
data->state.cache.word = 0;
37+
data->state.total_data_size = 0;
38+
39+
sha256_err_not_ready_clear();
40+
sha256_set_bswap(true);
41+
sha256_start();
42+
43+
pico_sha256_update(&data->state, pkt->in_buf, pkt->in_len);
44+
45+
if (!finish) {
46+
LOG_ERR("Multipart hashing not supported yet");
47+
return -ENOTSUP;
48+
}
49+
50+
pico_sha256_write_padding(&data->state);
51+
sha256_wait_valid_blocking();
52+
53+
for (uint i = 0; i < 8; i++) {
54+
((uint32_t *)pkt->out_buf)[i] = BSWAP_32((uint32_t)sha256_hw->sum[i]);
55+
}
56+
57+
return 0;
58+
}
59+
60+
static int crypto_rpi_pico_sha256_query_hw_caps(const struct device *dev)
61+
{
62+
return CAP_SEPARATE_IO_BUFS | CAP_SYNC_OPS;
63+
}
64+
65+
static int crypto_rpi_pico_sha256_hash_begin_session(const struct device *dev, struct hash_ctx *ctx,
66+
enum hash_algo algo)
67+
{
68+
struct crypto_rpi_pico_sha256_data *data = dev->data;
69+
int ret;
70+
71+
ret = k_mutex_lock(&data->mutex, K_FOREVER);
72+
if (ret != 0) {
73+
LOG_ERR("Failed to lock mutex: %d", ret);
74+
return ret;
75+
}
76+
77+
if (data->state.locked) {
78+
LOG_DBG("Hash engine busy");
79+
ret = -EBUSY;
80+
goto end;
81+
}
82+
83+
if (algo != CRYPTO_HASH_ALGO_SHA256) {
84+
LOG_ERR("Unsupported algo: %d", algo);
85+
ret = -EINVAL;
86+
goto end;
87+
}
88+
89+
if (ctx->flags & ~(crypto_rpi_pico_sha256_query_hw_caps(dev))) {
90+
LOG_ERR("Unsupported flag %x", ctx->flags);
91+
ret = -EINVAL;
92+
goto end;
93+
}
94+
95+
if (!bootrom_try_acquire_lock(BOOTROM_LOCK_SHA_256)) {
96+
LOG_ERR("bootrom_try_acquire_lock failed");
97+
ret = -EBUSY;
98+
goto end;
99+
}
100+
101+
data->state.locked = true;
102+
ctx->hash_hndlr = crypto_rpi_pico_sha256_hash_handler;
103+
ret = 0;
104+
105+
end:
106+
k_mutex_unlock(&data->mutex);
107+
108+
return ret;
109+
}
110+
111+
static int crypto_rpi_pico_sha256_hash_session_free(const struct device *dev, struct hash_ctx *ctx)
112+
{
113+
struct crypto_rpi_pico_sha256_data *data = dev->data;
114+
int ret;
115+
116+
ret = k_mutex_lock(&data->mutex, K_FOREVER);
117+
if (ret != 0) {
118+
LOG_ERR("Failed to lock mutex: %d", ret);
119+
return ret;
120+
}
121+
122+
if (!data->state.locked) {
123+
LOG_ERR("Invalid lock status: unlocked");
124+
ret = -EINVAL;
125+
goto end;
126+
}
127+
128+
bootrom_release_lock(BOOTROM_LOCK_SHA_256);
129+
data->state.locked = false;
130+
ret = 0;
131+
132+
end:
133+
k_mutex_unlock(&data->mutex);
134+
135+
return ret;
136+
}
137+
138+
static int crypto_rpi_pico_sha256_init(const struct device *dev)
139+
{
140+
struct crypto_rpi_pico_sha256_data *data = dev->data;
141+
142+
k_mutex_init(&data->mutex);
143+
144+
return 0;
145+
}
146+
147+
static DEVICE_API(crypto, crypto_rpi_pico_sha256_crypto_api) = {
148+
.query_hw_caps = crypto_rpi_pico_sha256_query_hw_caps,
149+
.hash_begin_session = crypto_rpi_pico_sha256_hash_begin_session,
150+
.hash_free_session = crypto_rpi_pico_sha256_hash_session_free,
151+
};
152+
153+
#define CRYPTO_RPI_PICO_SHA256_INIT(idx) \
154+
static struct crypto_rpi_pico_sha256_data crypto_rpi_pico_sha256_##idx##_data; \
155+
DEVICE_DT_INST_DEFINE(idx, crypto_rpi_pico_sha256_init, NULL, \
156+
&crypto_rpi_pico_sha256_##idx##_data, NULL, POST_KERNEL, \
157+
CONFIG_CRYPTO_INIT_PRIORITY, &crypto_rpi_pico_sha256_crypto_api);
158+
159+
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)