public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: "Marek Behún" <kabel@kernel.org>
To: Stefan Roese <sr@denx.de>
Cc: u-boot@lists.denx.de, "Marek Behún" <kabel@kernel.org>
Subject: [PATCH u-boot-mvebu v3 17/18] misc: turris_omnia_mcu: Add support for rng provided by MCU
Date: Wed, 27 Mar 2024 17:23:54 +0100	[thread overview]
Message-ID: <20240327162355.24584-18-kabel@kernel.org> (raw)
In-Reply-To: <20240327162355.24584-1-kabel@kernel.org>

Add support for true random number generator provided by the MCU on
Turris Omnia. The MCU firmware supports TRNG if the FEAT_TRNG bit is set
in features. In that case we bind the rng driver.

Signed-off-by: Marek Behún <kabel@kernel.org>
---
 configs/turris_omnia_defconfig  |  1 +
 drivers/misc/Kconfig            |  1 +
 drivers/misc/turris_omnia_mcu.c | 57 +++++++++++++++++++++++++++++++++
 3 files changed, 59 insertions(+)

diff --git a/configs/turris_omnia_defconfig b/configs/turris_omnia_defconfig
index 0df0f3c90b..4c21635ec9 100644
--- a/configs/turris_omnia_defconfig
+++ b/configs/turris_omnia_defconfig
@@ -107,6 +107,7 @@ CONFIG_NVME_PCI=y
 CONFIG_PCI_MVEBU=y
 CONFIG_PINCTRL=y
 CONFIG_PINCTRL_ARMADA_38X=y
+CONFIG_DM_RNG=y
 CONFIG_DM_RTC=y
 CONFIG_RTC_ARMADA38X=y
 CONFIG_SERIAL_PROBE_ALL=y
diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
index 844a21be47..a08f02196f 100644
--- a/drivers/misc/Kconfig
+++ b/drivers/misc/Kconfig
@@ -509,6 +509,7 @@ config TURRIS_OMNIA_MCU
 	bool "Enable Turris Omnia MCU driver"
 	depends on DM_I2C
 	depends on DM_GPIO
+	depends on DM_RNG
 	depends on SYSRESET
 	default y if TARGET_TURRIS_OMNIA
 	help
diff --git a/drivers/misc/turris_omnia_mcu.c b/drivers/misc/turris_omnia_mcu.c
index 77a0424d61..6b2f17c000 100644
--- a/drivers/misc/turris_omnia_mcu.c
+++ b/drivers/misc/turris_omnia_mcu.c
@@ -5,15 +5,20 @@
  */
 
 #include <common.h>
+#include <console.h>
 #include <dm.h>
 #include <dm/lists.h>
 #include <i2c.h>
+#include <rng.h>
 #include <sysreset.h>
 #include <turris-omnia-mcu-interface.h>
 #include <asm/byteorder.h>
 #include <asm/gpio.h>
+#include <linux/delay.h>
 #include <linux/log2.h>
 
+#define CMD_TRNG_MAX_ENTROPY_LEN	64
+
 struct turris_omnia_mcu_info {
 	u32 features;
 };
@@ -282,6 +287,49 @@ U_BOOT_DRIVER(turris_omnia_mcu_sysreset) = {
 	.ops		= &omnia_sysreset_ops,
 };
 
+static int omnia_rng_read(struct udevice *dev, void *data, size_t count)
+{
+	u8 buf[1 + CMD_TRNG_MAX_ENTROPY_LEN];
+	size_t len;
+	int ret;
+
+	while (count) {
+		ret = dm_i2c_read(dev->parent, CMD_TRNG_COLLECT_ENTROPY, buf,
+				  sizeof(buf));
+		if (ret)
+			return ret;
+
+		len = min_t(size_t, buf[0],
+			    min_t(size_t, CMD_TRNG_MAX_ENTROPY_LEN, count));
+
+		if (!len) {
+			/* wait 500ms (fail if interrupted), then try again */
+			for (int i = 0; i < 5; ++i) {
+				mdelay(100);
+				if (ctrlc())
+					return -EINTR;
+			}
+			continue;
+		}
+
+		memcpy(data, &buf[1], len);
+		data += len;
+		count -= len;
+	}
+
+	return 0;
+}
+
+static const struct dm_rng_ops omnia_rng_ops = {
+	.read		= omnia_rng_read,
+};
+
+U_BOOT_DRIVER(turris_omnia_mcu_trng) = {
+	.name		= "turris-omnia-mcu-trng",
+	.id		= UCLASS_RNG,
+	.ops		= &omnia_rng_ops,
+};
+
 static int turris_omnia_mcu_bind(struct udevice *dev)
 {
 	/* bind MCU GPIOs as a child device */
@@ -336,6 +384,15 @@ static int turris_omnia_mcu_probe(struct udevice *dev)
 			return ret;
 	}
 
+	/* bind rng if trng is supported */
+	if (info->features & FEAT_TRNG) {
+		ret = device_bind_driver_to_node(dev, "turris-omnia-mcu-trng",
+						 "turris-omnia-mcu-trng",
+						 dev_ofnode(dev), NULL);
+		if (ret < 0)
+			return ret;
+	}
+
 	return 0;
 }
 
-- 
2.43.2


  parent reply	other threads:[~2024-03-27 16:27 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-27 16:23 [PATCH u-boot-mvebu v3 00/18] Turris Omnia - New board revision support Marek Behún
2024-03-27 16:23 ` [PATCH u-boot-mvebu v3 01/18] arm: mvebu: turris_omnia: Enable LTO by default on Turris Omnia Marek Behún
2024-03-28  9:53   ` Stefan Roese
2024-03-27 16:23 ` [PATCH u-boot-mvebu v3 02/18] arm: mvebu: turris_omnia: Add header containing MCU command interface and use it Marek Behún
2024-03-28  9:53   ` Stefan Roese
2024-03-27 16:23 ` [PATCH u-boot-mvebu v3 03/18] arm: mvebu: turris_{omnia, mox}: Don't print model two times Marek Behún
2024-03-28  9:53   ` Stefan Roese
2024-03-27 16:23 ` [PATCH u-boot-mvebu v3 04/18] arm: mvebu: turris_omnia: Update MCU status and features reading Marek Behún
2024-03-28  9:54   ` Stefan Roese
2024-03-27 16:23 ` [PATCH u-boot-mvebu v3 05/18] arm: mvebu: turris_omnia: Implement getting board information from MCU Marek Behún
2024-03-28  9:56   ` Stefan Roese
2024-03-28 11:17     ` Marek Behún
2024-03-28 12:53       ` Stefan Roese
2024-03-27 16:23 ` [PATCH u-boot-mvebu v3 06/18] arm: mvebu: turris_omnia: Print board ECDSA public key if available Marek Behún
2024-03-28  9:56   ` Stefan Roese
2024-03-27 16:23 ` [PATCH u-boot-mvebu v3 07/18] arm: mvebu: turris_omnia: Disable Atmel SHA node if not present Marek Behún
2024-03-28  9:56   ` Stefan Roese
2024-03-27 16:23 ` [PATCH u-boot-mvebu v3 08/18] arm: mvebu: spl: Do not build mvebu-reset in SPL Marek Behún
2024-03-28  9:58   ` Stefan Roese
2024-03-27 16:23 ` [PATCH u-boot-mvebu v3 09/18] arm: mvebu: system-controller: Rework to use UCLASS_SYSCON Marek Behún
2024-03-28  9:59   ` Stefan Roese
2024-03-27 16:23 ` [PATCH u-boot-mvebu v3 10/18] arm: mvebu: system-controller: Select mvebu-reset if DM_RESET && PCI_MVEBU Marek Behún
2024-03-28 10:00   ` Stefan Roese
2024-03-27 16:23 ` [PATCH u-boot-mvebu v3 11/18] arm: mvebu: system-controller: Add support for SYSRESET Marek Behún
2024-03-28 10:04   ` Stefan Roese
2024-03-28 11:21     ` Marek Behún
2024-03-28 13:01       ` Stefan Roese
2024-03-28 14:18         ` Marek Behún
2024-03-27 16:23 ` [PATCH u-boot-mvebu v3 12/18] gpio: turris_omnia_mcu: Use byteorder conversion functions Marek Behún
2024-03-28 10:12   ` Stefan Roese
2024-03-27 16:23 ` [PATCH u-boot-mvebu v3 13/18] gpio: turris_omnia_mcu: Update firmware features reading Marek Behún
2024-03-28 10:15   ` Stefan Roese
2024-03-27 16:23 ` [PATCH u-boot-mvebu v3 14/18] gpio: turris_omnia_mcu: Add support for system power off via sysreset Marek Behún
2024-03-28 10:18   ` Stefan Roese
2024-03-27 16:23 ` [PATCH u-boot-mvebu v3 15/18] arm: mvebu: turris_omnia: Enable poweroff command via sysreset in defconfig Marek Behún
2024-03-28 10:18   ` Stefan Roese
2024-03-27 16:23 ` [PATCH u-boot-mvebu v3 16/18] cmd: rng: Print "Abort" on -EINTR Marek Behún
2024-03-28 10:22   ` Stefan Roese
2024-03-27 16:23 ` Marek Behún [this message]
2024-03-28 10:25   ` [PATCH u-boot-mvebu v3 17/18] misc: turris_omnia_mcu: Add support for rng provided by MCU Stefan Roese
2024-03-27 16:23 ` [PATCH u-boot-mvebu v3 18/18] arm: mvebu: turris_omnia: Enable rng command in defconfig Marek Behún
2024-03-28 10:25   ` Stefan Roese
2024-04-04  6:38 ` [PATCH u-boot-mvebu v3 00/18] Turris Omnia - New board revision support Stefan Roese
2024-04-04  7:38   ` Marek Behún

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240327162355.24584-18-kabel@kernel.org \
    --to=kabel@kernel.org \
    --cc=sr@denx.de \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox