Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C8465C54E67 for ; Thu, 28 Mar 2024 10:25:26 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 4A31788103; Thu, 28 Mar 2024 11:25:25 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1711621525; bh=XHYuIrrEuyUQT9ExbrTHToYNr/V7eaqWtiosTixdqS4=; h=Date:Subject:To:Cc:References:From:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=UGGGE9VmEDfsXqRxDGvqx6anfOG+XhltgrrKuW/CyzAkirwBCrspsRgGtYXO4AHZC /dRCYIyjR9lWWDZaRFEWnwRBlktCoFzkU8KjuNhvLphFLxtXQ3Oe6ojpvIxtOeaIAM obvuSf2NBpVgYNgB/ghSMWBXyxzc7V6TmmQSRaPdW4JAsC+ZrvQ1MKFcfriinRHrO3 L5G3meMjhmE0P3RIVyJ8BMr0jpY7i0dWDdNuq3QoyGpIN1RwsJoLLjJ0+yO5UOEHPF Jm0az70gXZJeWJF+EjBPc2rRYTtaBSDl238tgmSBLpux3Nxza7ysoSCaE3kNm6AvU1 8ZWCPA0WdMn2w== Received: by phobos.denx.de (Postfix, from userid 109) id C5E4B88125; Thu, 28 Mar 2024 11:25:24 +0100 (CET) Received: from mout-u-204.mailbox.org (mout-u-204.mailbox.org [80.241.59.204]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id E327687E9D for ; Thu, 28 Mar 2024 11:25:22 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=sr@denx.de Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:b231:465::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-u-204.mailbox.org (Postfix) with ESMTPS id 4V506S5qx4z9spY; Thu, 28 Mar 2024 11:25:20 +0100 (CET) Message-ID: <3e30db5e-55ef-4da5-bb7e-319d3c95e8e5@denx.de> Date: Thu, 28 Mar 2024 11:25:20 +0100 MIME-Version: 1.0 Subject: Re: [PATCH u-boot-mvebu v3 17/18] misc: turris_omnia_mcu: Add support for rng provided by MCU Content-Language: en-US To: =?UTF-8?Q?Marek_Beh=C3=BAn?= Cc: u-boot@lists.denx.de References: <20240327162355.24584-1-kabel@kernel.org> <20240327162355.24584-18-kabel@kernel.org> From: Stefan Roese In-Reply-To: <20240327162355.24584-18-kabel@kernel.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 4V506S5qx4z9spY X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean On 3/27/24 17:23, Marek Behún wrote: > 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 Reviewed-by: Stefan Roese Thanks, Stefan > --- > 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 > +#include > #include > #include > #include > +#include > #include > #include > #include > #include > +#include > #include > > +#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; > } > Viele Grüße, Stefan Roese -- DENX Software Engineering GmbH, Managing Director: Erika Unter HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: sr@denx.de