Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp27995074rwd; Tue, 4 Jul 2023 10:58:31 -0700 (PDT) X-Google-Smtp-Source: APBJJlExJsdNDyNvUdNrvVo8uLTh2b0ZSVhlL1pqICUubKKNKrhJvZ9GALcMrrpCTWr1b2uAwrUq X-Received: by 2002:a17:902:b210:b0:1b1:9272:55e2 with SMTP id t16-20020a170902b21000b001b1927255e2mr13902037plr.3.1688493511260; Tue, 04 Jul 2023 10:58:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688493511; cv=none; d=google.com; s=arc-20160816; b=therb3sCEeAcXAhPPr6HYIgDMbiZBO+w9G86FoVO4KVfedfVhMsc1EIzjgLFR9InLB dv1nuLl9QUv346iBoFKtbeIfXxiWqDCVpoytYrHWZw0gU5iyCCwTbUljzaMLywF/9N35 f15ig+4BU2jDjfhtTkL2kVpL4WLggDM1PmgE7Q0DWSBG6QqTJ4dp1tADDNq5OxtoL2Fw xEdTYI4bpt/vLuUI6Ju0Mu8ooUV5kzT+YetLsqFiv/eQxbwbRhXtsBCcmW+8ugcc9WSh 1pjTREClo1JlIkMhSYaq6V0hXc3WRMbAiiMB3qJJ3wGo0qYou/bXO7LcvSdO5wmjc4rT GTqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=L0mlBbI5UB7EJYxVbUVi/DPMd4wx2nWfxdmLFKK5fP4=; fh=lO4wmMQhvm8y/jaHDv9dt1CS5yb5ZTFKyt5xlt/d/d8=; b=0bsxRYJ79iNL4dHsP+uH8A+DgeR74vW6KXJIRq5dUOL8iAAQZ+9lm3uKRHNYqCvem/ XP2JUAbX43v2hVPjnnh7D0yfeDZ+nT3s4DasF3wzaByAT0ZRcb7n5OB6Ym6xXb/6srMe tTTqCiGuxJbNM298u0Hvil93Y6nr9mm/cZu39XMmX4Lw1g6sVUsyVSM8uzJI0fXPM6i9 tcH4Kta8UOspfqbL07/E2yaPfJXuMgdeApSmdTP77K63nx1+Rkrtrqh8Cvr4TIHLcBtf REm8/xMGY2gHkeuacJUx5j9e13GPgp/SD0mnNGgZdLz3ZUFBOsB1G3dDHp1kT7WbirIQ oR/A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gf16-20020a17090ac7d000b00262d9b8a137si19500202pjb.98.2023.07.04.10.58.13; Tue, 04 Jul 2023 10:58:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231642AbjGDRd5 (ORCPT + 99 others); Tue, 4 Jul 2023 13:33:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231455AbjGDRdy (ORCPT ); Tue, 4 Jul 2023 13:33:54 -0400 Received: from viti.kaiser.cx (viti.kaiser.cx [IPv6:2a01:238:43fe:e600:cd0c:bd4a:7a3:8e9f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E9C94DA; Tue, 4 Jul 2023 10:33:52 -0700 (PDT) Received: from ipservice-092-217-072-126.092.217.pools.vodafone-ip.de ([92.217.72.126] helo=martin-debian-2.paytec.ch) by viti.kaiser.cx with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.89) (envelope-from ) id 1qGjuP-0000yT-Uj; Tue, 04 Jul 2023 19:33:50 +0200 From: Martin Kaiser To: Herbert Xu Cc: Joshua Henderson , linux-crypto@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Martin Kaiser Subject: [PATCH 4/4] hwrng: pic32 - enable TRNG only while it's used Date: Tue, 4 Jul 2023 19:32:03 +0200 Message-Id: <20230704173203.70706-5-martin@kaiser.cx> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230704173203.70706-1-martin@kaiser.cx> References: <20230704173203.70706-1-martin@kaiser.cx> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The probe function enables the TRNG hardware before registering the driver. If registration fails, probe returns an error, but the TRNG remains enabled. Define init and cleanup functions, enable and disable the hardware there. Signed-off-by: Martin Kaiser --- drivers/char/hw_random/pic32-rng.c | 41 ++++++++++++++---------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/drivers/char/hw_random/pic32-rng.c b/drivers/char/hw_random/pic32-rng.c index c1b3f5915f03..1902f4389a3f 100644 --- a/drivers/char/hw_random/pic32-rng.c +++ b/drivers/char/hw_random/pic32-rng.c @@ -38,6 +38,15 @@ struct pic32_rng { */ #define RNG_TIMEOUT 500 +static int pic32_rng_init(struct hwrng *rng) +{ + struct pic32_rng *priv = container_of(rng, struct pic32_rng, rng); + + /* enable TRNG in enhanced mode */ + writel(TRNGEN | TRNGMOD, priv->base + RNGCON); + return 0; +} + static int pic32_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait) { @@ -59,12 +68,17 @@ static int pic32_rng_read(struct hwrng *rng, void *buf, size_t max, return -EIO; } +static void pic32_rng_cleanup(struct hwrng *rng) +{ + struct pic32_rng *priv = container_of(rng, struct pic32_rng, rng); + + writel(0, priv->base + RNGCON); +} + static int pic32_rng_probe(struct platform_device *pdev) { struct pic32_rng *priv; struct clk *clk; - u32 v; - int ret; priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); if (!priv) @@ -78,28 +92,12 @@ static int pic32_rng_probe(struct platform_device *pdev) if (IS_ERR(clk)) return PTR_ERR(clk); - /* enable TRNG in enhanced mode */ - v = TRNGEN | TRNGMOD; - writel(v, priv->base + RNGCON); - priv->rng.name = pdev->name; + priv->rng.init = pic32_rng_init; priv->rng.read = pic32_rng_read; + priv->rng.cleanup = pic32_rng_cleanup; - ret = devm_hwrng_register(&pdev->dev, &priv->rng); - if (ret) - return ret; - - platform_set_drvdata(pdev, priv); - - return 0; -} - -static int pic32_rng_remove(struct platform_device *pdev) -{ - struct pic32_rng *rng = platform_get_drvdata(pdev); - - writel(0, rng->base + RNGCON); - return 0; + return devm_hwrng_register(&pdev->dev, &priv->rng); } static const struct of_device_id pic32_rng_of_match[] __maybe_unused = { @@ -110,7 +108,6 @@ MODULE_DEVICE_TABLE(of, pic32_rng_of_match); static struct platform_driver pic32_rng_driver = { .probe = pic32_rng_probe, - .remove = pic32_rng_remove, .driver = { .name = "pic32-rng", .of_match_table = of_match_ptr(pic32_rng_of_match), -- 2.30.2