From patchwork Thu Apr 4 07:51:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marek_Beh=C3=BAn?= X-Patchwork-Id: 1919657 X-Patchwork-Delegate: sr@denx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20201202 header.b=WqcpnD22; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4V9DQx3Fdnz1yZH for ; Thu, 4 Apr 2024 18:54:17 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id F2C5188409; Thu, 4 Apr 2024 09:51:37 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="WqcpnD22"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 45DAA883BE; Thu, 4 Apr 2024 09:51:37 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.2 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) (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 04832883E0 for ; Thu, 4 Apr 2024 09:51:35 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=kabel@kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 5B458CE2FAD; Thu, 4 Apr 2024 07:51:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CE692C433F1; Thu, 4 Apr 2024 07:51:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712217092; bh=z2URJtOfj1dPCX5JjcGHp6CF+3pnyfMzHeWK0BDBRds=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WqcpnD22bhBTvS/DJ+2OfrYbNi0tx5TPYrEHkEnQjtK5K/13LgEGcIB9DX585WYDJ UPDOR8xnZOgPZCqekkUF7ZnXuM3ia4i0LMzxyoeKBN4BPQeIpfbEG4VwAW8UkfvHj+ JkC5F9FpndUgo5qNyybCRWTr4Cj0V1ANFxH9dOCHDU6JXR2LAheAekAPByc5DFkA43 yI+iI/mdMtGEvcQYdYIrATRrxV1Pa2x9/sZHFEter/8xUQxWSUFYYwFZJHQ9ogOC4k FjXMSAF8AZMgEVcgUHwuBKz7k7cC6BcHwnYEA+4IoF6uL9UIcr1LoxkAoCebFkfo9b Ko0XSJXNZhwjw== From: =?utf-8?q?Marek_Beh=C3=BAn?= To: Stefan Roese Cc: u-boot@lists.denx.de, =?utf-8?q?Marek_Beh=C3=BAn?= Subject: [PATCH u-boot-mvebu v4 17/18] misc: turris_omnia_mcu: Add support for rng provided by MCU Date: Thu, 4 Apr 2024 09:51:06 +0200 Message-ID: <20240404075107.19636-18-kabel@kernel.org> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240404075107.19636-1-kabel@kernel.org> References: <20240404075107.19636-1-kabel@kernel.org> MIME-Version: 1.0 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 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 --- 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 041de538fa..6b06888454 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; }