Skip to content

Commit 41bb142

Browse files
elkabloarndb
authored andcommitted
platform: cznic: turris-omnia-mcu: Add support for MCU provided TRNG
Add support for true random number generator provided by the MCU. New Omnia boards come without the Atmel SHA204-A chip. Instead the crypto functionality is provided by new microcontroller, which has a TRNG peripheral. Signed-off-by: Marek Behún <[email protected]> Acked-by: Bartosz Golaszewski <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnd Bergmann <[email protected]>
1 parent ab89fb5 commit 41bb142

File tree

6 files changed

+120
-2
lines changed

6 files changed

+120
-2
lines changed

drivers/platform/cznic/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ config TURRIS_OMNIA_MCU
1818
depends on I2C
1919
select GPIOLIB
2020
select GPIOLIB_IRQCHIP
21+
select HW_RANDOM
2122
select RTC_CLASS
2223
select WATCHDOG_CORE
2324
help
@@ -27,6 +28,7 @@ config TURRIS_OMNIA_MCU
2728
- board poweroff into true low power mode (with voltage regulators
2829
disabled) and the ability to configure wake up from this mode (via
2930
rtcwake)
31+
- true random number generator (if available on the MCU)
3032
- MCU watchdog
3133
- GPIO pins
3234
- to get front button press events (the front button can be

drivers/platform/cznic/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ obj-$(CONFIG_TURRIS_OMNIA_MCU) += turris-omnia-mcu.o
44
turris-omnia-mcu-y := turris-omnia-mcu-base.o
55
turris-omnia-mcu-y += turris-omnia-mcu-gpio.o
66
turris-omnia-mcu-y += turris-omnia-mcu-sys-off-wakeup.o
7+
turris-omnia-mcu-y += turris-omnia-mcu-trng.o
78
turris-omnia-mcu-y += turris-omnia-mcu-watchdog.o

drivers/platform/cznic/turris-omnia-mcu-base.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,11 @@ static int omnia_mcu_probe(struct i2c_client *client)
381381
if (err)
382382
return err;
383383

384-
return omnia_mcu_register_gpiochip(mcu);
384+
err = omnia_mcu_register_gpiochip(mcu);
385+
if (err)
386+
return err;
387+
388+
return omnia_mcu_register_trng(mcu);
385389
}
386390

387391
static const struct of_device_id of_omnia_mcu_match[] = {

drivers/platform/cznic/turris-omnia-mcu-gpio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ static const struct omnia_gpio omnia_gpios[64] = {
195195
};
196196

197197
/* mapping from interrupts to indexes of GPIOs in the omnia_gpios array */
198-
static const u8 omnia_int_to_gpio_idx[32] = {
198+
const u8 omnia_int_to_gpio_idx[32] = {
199199
[__bf_shf(OMNIA_INT_CARD_DET)] = 4,
200200
[__bf_shf(OMNIA_INT_MSATA_IND)] = 5,
201201
[__bf_shf(OMNIA_INT_USB30_OVC)] = 6,
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* CZ.NIC's Turris Omnia MCU TRNG driver
4+
*
5+
* 2024 by Marek Behún <[email protected]>
6+
*/
7+
8+
#include <linux/bitfield.h>
9+
#include <linux/completion.h>
10+
#include <linux/container_of.h>
11+
#include <linux/errno.h>
12+
#include <linux/gpio/consumer.h>
13+
#include <linux/gpio/driver.h>
14+
#include <linux/hw_random.h>
15+
#include <linux/i2c.h>
16+
#include <linux/interrupt.h>
17+
#include <linux/minmax.h>
18+
#include <linux/string.h>
19+
#include <linux/types.h>
20+
21+
#include <linux/turris-omnia-mcu-interface.h>
22+
#include "turris-omnia-mcu.h"
23+
24+
#define OMNIA_CMD_TRNG_MAX_ENTROPY_LEN 64
25+
26+
static irqreturn_t omnia_trng_irq_handler(int irq, void *dev_id)
27+
{
28+
struct omnia_mcu *mcu = dev_id;
29+
30+
complete(&mcu->trng_entropy_ready);
31+
32+
return IRQ_HANDLED;
33+
}
34+
35+
static int omnia_trng_read(struct hwrng *rng, void *data, size_t max, bool wait)
36+
{
37+
struct omnia_mcu *mcu = container_of(rng, struct omnia_mcu, trng);
38+
u8 reply[1 + OMNIA_CMD_TRNG_MAX_ENTROPY_LEN];
39+
int err, bytes;
40+
41+
if (!wait && !completion_done(&mcu->trng_entropy_ready))
42+
return 0;
43+
44+
do {
45+
if (wait_for_completion_interruptible(&mcu->trng_entropy_ready))
46+
return -ERESTARTSYS;
47+
48+
err = omnia_cmd_read(mcu->client,
49+
OMNIA_CMD_TRNG_COLLECT_ENTROPY,
50+
reply, sizeof(reply));
51+
if (err)
52+
return err;
53+
54+
bytes = min3(reply[0], max, OMNIA_CMD_TRNG_MAX_ENTROPY_LEN);
55+
} while (wait && !bytes);
56+
57+
memcpy(data, &reply[1], bytes);
58+
59+
return bytes;
60+
}
61+
62+
int omnia_mcu_register_trng(struct omnia_mcu *mcu)
63+
{
64+
struct device *dev = &mcu->client->dev;
65+
u8 irq_idx, dummy;
66+
int irq, err;
67+
68+
if (!(mcu->features & OMNIA_FEAT_TRNG))
69+
return 0;
70+
71+
irq_idx = omnia_int_to_gpio_idx[__bf_shf(OMNIA_INT_TRNG)];
72+
irq = gpiod_to_irq(gpio_device_get_desc(mcu->gc.gpiodev, irq_idx));
73+
if (!irq)
74+
return dev_err_probe(dev, -ENXIO, "Cannot get TRNG IRQ\n");
75+
76+
/*
77+
* If someone else cleared the TRNG interrupt but did not read the
78+
* entropy, a new interrupt won't be generated, and entropy collection
79+
* will be stuck. Ensure an interrupt will be generated by executing
80+
* the collect entropy command (and discarding the result).
81+
*/
82+
err = omnia_cmd_read(mcu->client, OMNIA_CMD_TRNG_COLLECT_ENTROPY,
83+
&dummy, 1);
84+
if (err)
85+
return err;
86+
87+
init_completion(&mcu->trng_entropy_ready);
88+
89+
err = devm_request_threaded_irq(dev, irq, NULL, omnia_trng_irq_handler,
90+
IRQF_ONESHOT, "turris-omnia-mcu-trng",
91+
mcu);
92+
if (err)
93+
return dev_err_probe(dev, err, "Cannot request TRNG IRQ\n");
94+
95+
mcu->trng.name = "turris-omnia-mcu-trng";
96+
mcu->trng.read = omnia_trng_read;
97+
98+
err = devm_hwrng_register(dev, &mcu->trng);
99+
if (err)
100+
return dev_err_probe(dev, err, "Cannot register TRNG\n");
101+
102+
return 0;
103+
}

drivers/platform/cznic/turris-omnia-mcu.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#define __TURRIS_OMNIA_MCU_H
1010

1111
#include <linux/bitops.h>
12+
#include <linux/completion.h>
1213
#include <linux/gpio/driver.h>
14+
#include <linux/hw_random.h>
1315
#include <linux/if_ether.h>
1416
#include <linux/mutex.h>
1517
#include <linux/types.h>
@@ -47,6 +49,10 @@ struct omnia_mcu {
4749

4850
/* MCU watchdog */
4951
struct watchdog_device wdt;
52+
53+
/* true random number generator */
54+
struct hwrng trng;
55+
struct completion trng_entropy_ready;
5056
};
5157

5258
int omnia_cmd_write_read(const struct i2c_client *client,
@@ -176,11 +182,13 @@ static inline int omnia_cmd_read_u8(const struct i2c_client *client, u8 cmd,
176182
return omnia_cmd_read(client, cmd, reply, sizeof(*reply));
177183
}
178184

185+
extern const u8 omnia_int_to_gpio_idx[32];
179186
extern const struct attribute_group omnia_mcu_gpio_group;
180187
extern const struct attribute_group omnia_mcu_poweroff_group;
181188

182189
int omnia_mcu_register_gpiochip(struct omnia_mcu *mcu);
183190
int omnia_mcu_register_sys_off_and_wakeup(struct omnia_mcu *mcu);
191+
int omnia_mcu_register_trng(struct omnia_mcu *mcu);
184192
int omnia_mcu_register_watchdog(struct omnia_mcu *mcu);
185193

186194
#endif /* __TURRIS_OMNIA_MCU_H */

0 commit comments

Comments
 (0)