Skip to content

Commit f541476

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 80655ed commit f541476

File tree

6 files changed

+163
-2
lines changed

6 files changed

+163
-2
lines changed

drivers/crypto/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ zephyr_library_sources_ifdef(CONFIG_CRYPTO_MCHP_XEC_SYMCR crypto_mchp_xec_
1414
zephyr_library_sources_ifdef(CONFIG_CRYPTO_MCUX_DCP crypto_mcux_dcp.c)
1515
zephyr_library_sources_ifdef(CONFIG_CRYPTO_NPCX_SHA crypto_npcx_sha.c)
1616
zephyr_library_sources_ifdef(CONFIG_CRYPTO_NRF_ECB crypto_nrf_ecb.c)
17-
zephyr_library_sources_ifdef(CONFIG_CRYPTO_RTS5912_SHA crypto_rts5912_sha.c)
17+
zephyr_library_sources_ifdef(CONFIG_CRYPTO_RPI_PICO_SHA256 crypto_rpi_pico_sha256.c)
18+
zephyr_library_sources_ifdef(CONFIG_CRYPTO_RTS5912_SHA crypto_rts5912_sha.c)
1819
zephyr_library_sources_ifdef(CONFIG_CRYPTO_SI32 crypto_si32.c)
1920
zephyr_library_sources_ifdef(CONFIG_CRYPTO_SMARTBOND crypto_smartbond.c)
2021
zephyr_library_sources_ifdef(CONFIG_CRYPTO_STM32 crypto_stm32.c)

drivers/crypto/Kconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ source "drivers/crypto/Kconfig.it8xxx2"
5656
source "drivers/crypto/Kconfig.mcux_dcp"
5757
source "drivers/crypto/Kconfig.npcx"
5858
source "drivers/crypto/Kconfig.nrf_ecb"
59+
source "drivers/crypto/Kconfig.rpi_pico"
60+
source "drivers/crypto/Kconfig.rts5912"
5961
source "drivers/crypto/Kconfig.si32"
6062
source "drivers/crypto/Kconfig.smartbond"
61-
source "drivers/crypto/Kconfig.rts5912"
6263
source "drivers/crypto/Kconfig.stm32"
6364
source "drivers/crypto/Kconfig.xec"
6465
# 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: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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_spinlock lock;
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+
k_spinlock_key_t key;
70+
int ret;
71+
72+
if (data->state.locked) {
73+
LOG_ERR("Invalid lock status: locked");
74+
return -EINVAL;
75+
}
76+
77+
if (algo != CRYPTO_HASH_ALGO_SHA256) {
78+
LOG_ERR("Unsupported algo: %d", algo);
79+
return -EINVAL;
80+
}
81+
82+
if (ctx->flags & ~(crypto_rpi_pico_sha256_query_hw_caps(dev))) {
83+
LOG_ERR("Unsupported flag %x", ctx->flags);
84+
return -EINVAL;
85+
}
86+
87+
key = k_spin_lock(&data->lock);
88+
89+
ret = bootrom_try_acquire_lock(BOOTROM_LOCK_SHA_256);
90+
if (!ret) {
91+
LOG_ERR("bootrom_try_acquire_lock failed");
92+
k_spin_unlock(&data->lock, key);
93+
return -EBUSY;
94+
}
95+
96+
data->state.locked = true;
97+
98+
k_spin_unlock(&data->lock, key);
99+
100+
ctx->hash_hndlr = crypto_rpi_pico_sha256_hash_handler;
101+
102+
return 0;
103+
}
104+
105+
static int crypto_rpi_pico_sha256_hash_session_free(const struct device *dev, struct hash_ctx *ctx)
106+
{
107+
struct crypto_rpi_pico_sha256_data *data = dev->data;
108+
k_spinlock_key_t key;
109+
110+
if (!data->state.locked) {
111+
LOG_ERR("Invalid lock status: unlocked");
112+
return -EINVAL;
113+
}
114+
115+
key = k_spin_lock(&data->lock);
116+
bootrom_release_lock(BOOTROM_LOCK_SHA_256);
117+
data->state.locked = false;
118+
k_spin_unlock(&data->lock, key);
119+
120+
return 0;
121+
}
122+
123+
static DEVICE_API(crypto, crypto_rpi_pico_sha256_crypto_api) = {
124+
.query_hw_caps = crypto_rpi_pico_sha256_query_hw_caps,
125+
.hash_begin_session = crypto_rpi_pico_sha256_hash_begin_session,
126+
.hash_free_session = crypto_rpi_pico_sha256_hash_session_free,
127+
};
128+
129+
#define CRYPTO_RPI_PICO_SHA256_INIT(idx) \
130+
static struct crypto_rpi_pico_sha256_data crypto_rpi_pico_sha256_##idx##_data; \
131+
DEVICE_DT_INST_DEFINE(idx, NULL, NULL, &crypto_rpi_pico_sha256_##idx##_data, NULL, \
132+
POST_KERNEL, CONFIG_CRYPTO_INIT_PRIORITY, \
133+
&crypto_rpi_pico_sha256_crypto_api);
134+
135+
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)