Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp565104ybe; Fri, 13 Sep 2019 02:28:53 -0700 (PDT) X-Google-Smtp-Source: APXvYqxy+cFnz/Y4wofhn7D8kZJd6xJylvbPdJZC0Gf+DzAAgsr8n63N2Es1Beeki8qQLRG0t/s7 X-Received: by 2002:a17:906:9713:: with SMTP id k19mr37684407ejx.303.1568366933153; Fri, 13 Sep 2019 02:28:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568366933; cv=none; d=google.com; s=arc-20160816; b=pEK0xalhWAIHPqxYJpEAEvBaZOnP10X3Mpmp6rR88GwvmmpgXrnI7HfuHl/tIVVbd4 Eb3rOM3mTASNjK4ag7s0gmRaSshXikkoHs/NiQwvwna6AhB74zU0v1pYJ7ZC3f9TY2r4 sT0iDd71UnV/qY9PuyG2SfqZqVL3QqFUkOGV7Ap2VelzyYQ9CesF01JNYC/gtx4XkEPp xKAX4vLbKNELL+uRnJpxjGaKwxwy/1SckRal/kCpySQ7gheHsnQJuXzkwnkaL/wYZIb0 XVuhLuCPBpRvyP62pbb2SH6MrRSeXyOXmaifSuKfzYCQM8sZ2GxKTK/L8gCkfmpwI9pY Q1GQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=KdWl9728cR3NNiz9k+CiAkkKPRvWRTcoTTHEAo/qVZc=; b=xp/qyj2ydlP+zh1ioZ22ec9MrA4jdy7gbVPqC8O1gKqKte1pL34BG/RdDR5JXZh2gU Z7fGrCbQByXr5vS1OJAU3kGx7SjmChVl87BRlI74tBILuKxeCP9aWX3N0IWG+doiBewH F5nCWN8krVAf3fQXk/SdVKI8rDRW8jkXDEPVQuLrQHhQl1WBe+OuyjjzyjiC6Qhx717+ SLR39+ZvOvhfhgQoAKdUdb0HHojYd6fv31i1NffHyv3/KYJb67eRFmovq0SLmlXo0YeL ieWHJusB7ls/W32ipnAKSI+BkYjXrwjAABUOCvn1vR4EvrQU5ZmeByuparg+a5bF1PM0 8DlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=QJ2EQbmd; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k4si13896690ejd.325.2019.09.13.02.28.27; Fri, 13 Sep 2019 02:28:53 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=QJ2EQbmd; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387623AbfIMJ2M (ORCPT + 99 others); Fri, 13 Sep 2019 05:28:12 -0400 Received: from mail-wr1-f66.google.com ([209.85.221.66]:39480 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387896AbfIMJ2L (ORCPT ); Fri, 13 Sep 2019 05:28:11 -0400 Received: by mail-wr1-f66.google.com with SMTP id r3so1228237wrj.6 for ; Fri, 13 Sep 2019 02:28:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=KdWl9728cR3NNiz9k+CiAkkKPRvWRTcoTTHEAo/qVZc=; b=QJ2EQbmdNJEE9UMfwolykX8/l+ediIZaceAqN/IPpUYMTVgSE54U4qllmgRK3Ux0Zt 0dMb6acTKqq+xSxWQ1c8QiCU+xtyZ9W2+IsHEcdwvoWCTdHOhlFalGEk3sLBEA8tyk7O q+UJO5xzm95lhGqDnvqYjGB2OKxAxguQ5FmXKe1ui6GE5P/B2N59jSn1XdcyHOV5VlBQ DhWfn/E7VNn6/Dwe9vExWiTu85nA99BxTpmylJmHXHJkAyraYOHDJ3XIWPKg2s7mra3O 40sEk0ZoIndUm0C87iC/mOGsJ2JeVmYYqNVDoNgnjRNe8k4lU3zy/lq1rFPwI/iUg4M7 Ccrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=KdWl9728cR3NNiz9k+CiAkkKPRvWRTcoTTHEAo/qVZc=; b=OX1IOGrtdO84egued19Oaw8CPLQwiSgmkv9SKqXyOHzm0RzYRWClZQXS/KxcV7XhlX lwdM9BI2evV1q9GRK7JtSZEilP09wLeDJMrCftQdfGYjO/5WhwWlEG5I1yXWuISQowUZ xolDxPSQIG6ghCi3YLtWPfVZ5egp24/cXkKYa90FPx7Qat2bKGaxfnohICBoElRGG1jD c532BiMJk+suTDthMFxAyb3THaQvdYPcM4HC53iWnw+PhYcqYyszAPqC0a3EwXPxHgQt s4M8Np7kEbSah3WrUUdsmilGuxFWu6E+KhhM0UQRbZn8G4xqHWH5CdlpxxqX/Dsq7Dm9 aLyg== X-Gm-Message-State: APjAAAV5SV2e2WtP5x82YfDOFYpUe+efpHRvL2bn8mFQjmEGgSwRcUYE 0Wce+HUHSL8+Y5CT7AfYDcu9yQ== X-Received: by 2002:adf:e4ce:: with SMTP id v14mr6314573wrm.15.1568366887451; Fri, 13 Sep 2019 02:28:07 -0700 (PDT) Received: from holly.lan (cpc141214-aztw34-2-0-cust773.18-1.cable.virginm.net. [86.9.19.6]) by smtp.gmail.com with ESMTPSA id s10sm3659226wmf.48.2019.09.13.02.28.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Sep 2019 02:28:06 -0700 (PDT) Date: Fri, 13 Sep 2019 10:28:05 +0100 From: Daniel Thompson To: Tomer Maimon Cc: mpm@selenic.com, herbert@gondor.apana.org.au, arnd@arndb.de, gregkh@linuxfoundation.org, robh+dt@kernel.org, mark.rutland@arm.com, avifishman70@gmail.com, tali.perry1@gmail.com, venture@google.com, yuenn@google.com, benjaminfair@google.com, sumit.garg@linaro.org, jens.wiklander@linaro.org, vkoul@kernel.org, tglx@linutronix.de, joel@jms.id.au, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, openbmc@lists.ozlabs.org Subject: Re: [PATCH v3 2/2] hwrng: npcm: add NPCM RNG driver Message-ID: <20190913092805.4bemarnbh6jo3lxh@holly.lan> References: <20190912090149.7521-1-tmaimon77@gmail.com> <20190912090149.7521-3-tmaimon77@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190912090149.7521-3-tmaimon77@gmail.com> User-Agent: NeoMutt/20180716 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org On Thu, Sep 12, 2019 at 12:01:49PM +0300, Tomer Maimon wrote: > Add Nuvoton NPCM BMC Random Number Generator(RNG) driver. > > Signed-off-by: Tomer Maimon Reviewed-by: Daniel Thompson Note, you are welcome to preseve this if you have to respin and change directory based on Vinod's feedback... > --- > drivers/char/hw_random/Kconfig | 13 +++ > drivers/char/hw_random/Makefile | 1 + > drivers/char/hw_random/npcm-rng.c | 186 ++++++++++++++++++++++++++++++ > 3 files changed, 200 insertions(+) > create mode 100644 drivers/char/hw_random/npcm-rng.c > > diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig > index 59f25286befe..87a1c30e7958 100644 > --- a/drivers/char/hw_random/Kconfig > +++ b/drivers/char/hw_random/Kconfig > @@ -440,6 +440,19 @@ config HW_RANDOM_OPTEE > > If unsure, say Y. > > +config HW_RANDOM_NPCM > + tristate "NPCM Random Number Generator support" > + depends on ARCH_NPCM || COMPILE_TEST > + default HW_RANDOM > + help > + This driver provides support for the Random Number > + Generator hardware available in Nuvoton NPCM SoCs. > + > + To compile this driver as a module, choose M here: the > + module will be called npcm-rng. > + > + If unsure, say Y. > + > endif # HW_RANDOM > > config UML_RANDOM > diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile > index 7c9ef4a7667f..17b6d4e6d591 100644 > --- a/drivers/char/hw_random/Makefile > +++ b/drivers/char/hw_random/Makefile > @@ -39,3 +39,4 @@ obj-$(CONFIG_HW_RANDOM_MTK) += mtk-rng.o > obj-$(CONFIG_HW_RANDOM_S390) += s390-trng.o > obj-$(CONFIG_HW_RANDOM_KEYSTONE) += ks-sa-rng.o > obj-$(CONFIG_HW_RANDOM_OPTEE) += optee-rng.o > +obj-$(CONFIG_HW_RANDOM_NPCM) += npcm-rng.o > diff --git a/drivers/char/hw_random/npcm-rng.c b/drivers/char/hw_random/npcm-rng.c > new file mode 100644 > index 000000000000..b7c8c7e13a49 > --- /dev/null > +++ b/drivers/char/hw_random/npcm-rng.c > @@ -0,0 +1,186 @@ > +// SPDX-License-Identifier: GPL-2.0 > +// Copyright (c) 2019 Nuvoton Technology corporation. > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#define NPCM_RNGCS_REG 0x00 /* Control and status register */ > +#define NPCM_RNGD_REG 0x04 /* Data register */ > +#define NPCM_RNGMODE_REG 0x08 /* Mode register */ > + > +#define NPCM_RNG_CLK_SET_25MHZ GENMASK(4, 3) /* 20-25 MHz */ > +#define NPCM_RNG_DATA_VALID BIT(1) > +#define NPCM_RNG_ENABLE BIT(0) > +#define NPCM_RNG_M1ROSEL BIT(1) > + > +#define NPCM_RNG_TIMEOUT_USEC 20000 > +#define NPCM_RNG_POLL_USEC 1000 > + > +#define to_npcm_rng(p) container_of(p, struct npcm_rng, rng) > + > +struct npcm_rng { > + void __iomem *base; > + struct hwrng rng; > +}; > + > +static int npcm_rng_init(struct hwrng *rng) > +{ > + struct npcm_rng *priv = to_npcm_rng(rng); > + > + writel(NPCM_RNG_CLK_SET_25MHZ | NPCM_RNG_ENABLE, > + priv->base + NPCM_RNGCS_REG); > + > + return 0; > +} > + > +static void npcm_rng_cleanup(struct hwrng *rng) > +{ > + struct npcm_rng *priv = to_npcm_rng(rng); > + > + writel(NPCM_RNG_CLK_SET_25MHZ, priv->base + NPCM_RNGCS_REG); > +} > + > +static int npcm_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait) > +{ > + struct npcm_rng *priv = to_npcm_rng(rng); > + int retval = 0; > + int ready; > + > + pm_runtime_get_sync((struct device *)priv->rng.priv); > + > + while (max >= sizeof(u32)) { > + if (wait) { > + if (readl_poll_timeout(priv->base + NPCM_RNGCS_REG, > + ready, > + ready & NPCM_RNG_DATA_VALID, > + NPCM_RNG_POLL_USEC, > + NPCM_RNG_TIMEOUT_USEC)) > + break; > + } else { > + if ((readl(priv->base + NPCM_RNGCS_REG) & > + NPCM_RNG_DATA_VALID) == 0) > + break; > + } > + > + *(u32 *)buf = readl(priv->base + NPCM_RNGD_REG); > + retval += sizeof(u32); > + buf += sizeof(u32); > + max -= sizeof(u32); > + } > + > + pm_runtime_mark_last_busy((struct device *)priv->rng.priv); > + pm_runtime_put_sync_autosuspend((struct device *)priv->rng.priv); > + > + return retval || !wait ? retval : -EIO; > +} > + > +static int npcm_rng_probe(struct platform_device *pdev) > +{ > + struct npcm_rng *priv; > + struct resource *res; > + int ret; > + > + priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); > + if (!priv) > + return -ENOMEM; > + > + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); > + priv->base = devm_ioremap_resource(&pdev->dev, res); > + if (IS_ERR(priv->base)) > + return PTR_ERR(priv->base); > + > + dev_set_drvdata(&pdev->dev, priv); > + pm_runtime_set_autosuspend_delay(&pdev->dev, 100); > + pm_runtime_use_autosuspend(&pdev->dev); > + pm_runtime_enable(&pdev->dev); > + > +#ifndef CONFIG_PM > + priv->rng.init = npcm_rng_init; > + priv->rng.cleanup = npcm_rng_cleanup; > +#endif > + priv->rng.name = pdev->name; > + priv->rng.read = npcm_rng_read; > + priv->rng.priv = (unsigned long)&pdev->dev; > + priv->rng.quality = 1000; > + > + writel(NPCM_RNG_M1ROSEL, priv->base + NPCM_RNGMODE_REG); > + > + ret = devm_hwrng_register(&pdev->dev, &priv->rng); > + if (ret) { > + dev_err(&pdev->dev, "Failed to register rng device: %d\n", > + ret); > + pm_runtime_disable(&pdev->dev); > + pm_runtime_set_suspended(&pdev->dev); > + return ret; > + } > + > + return 0; > +} > + > +static int npcm_rng_remove(struct platform_device *pdev) > +{ > + struct npcm_rng *priv = platform_get_drvdata(pdev); > + > + devm_hwrng_unregister(&pdev->dev, &priv->rng); > + pm_runtime_disable(&pdev->dev); > + pm_runtime_set_suspended(&pdev->dev); > + > + return 0; > +} > + > +#ifdef CONFIG_PM > +static int npcm_rng_runtime_suspend(struct device *dev) > +{ > + struct npcm_rng *priv = dev_get_drvdata(dev); > + > + npcm_rng_cleanup(&priv->rng); > + > + return 0; > +} > + > +static int npcm_rng_runtime_resume(struct device *dev) > +{ > + struct npcm_rng *priv = dev_get_drvdata(dev); > + > + return npcm_rng_init(&priv->rng); > +} > +#endif > + > +static const struct dev_pm_ops npcm_rng_pm_ops = { > + SET_RUNTIME_PM_OPS(npcm_rng_runtime_suspend, > + npcm_rng_runtime_resume, NULL) > + SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, > + pm_runtime_force_resume) > +}; > + > +static const struct of_device_id rng_dt_id[] = { > + { .compatible = "nuvoton,npcm750-rng", }, > + {}, > +}; > +MODULE_DEVICE_TABLE(of, rng_dt_id); > + > +static struct platform_driver npcm_rng_driver = { > + .driver = { > + .name = "npcm-rng", > + .pm = &npcm_rng_pm_ops, > + .of_match_table = of_match_ptr(rng_dt_id), > + }, > + .probe = npcm_rng_probe, > + .remove = npcm_rng_remove, > +}; > + > +module_platform_driver(npcm_rng_driver); > + > +MODULE_DESCRIPTION("Nuvoton NPCM Random Number Generator Driver"); > +MODULE_AUTHOR("Tomer Maimon "); > +MODULE_LICENSE("GPL v2"); > -- > 2.18.0 >