Linux-Crypto Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH 0/2] hw_random: hisilicon - add HiSilicon TRNG V2 support
@ 2019-10-31  8:34 Zaibo Xu
  2019-10-31  8:34 ` [PATCH 1/2] hw_random: add HiSilicon TRNG driver support Zaibo Xu
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Zaibo Xu @ 2019-10-31  8:34 UTC (permalink / raw)
  To: herbert, mpm
  Cc: linux-crypto, jonathan.cameron, wangzhou1, linuxarm,
	wangkefeng.wang, qianweili, forest.zhouchang

This adds HiSilicon True Random Number Generator (TRNG) version 2
driver in hw_random subsystem.

Zaibo Xu (2):
  hw_random: add HiSilicon TRNG driver support
  MAINTAINERS: Add maintainer for HiSilicon TRNG V2 driver

 MAINTAINERS                           |  5 ++
 drivers/char/hw_random/Kconfig        | 13 +++++
 drivers/char/hw_random/Makefile       |  1 +
 drivers/char/hw_random/hisi-trng-v2.c | 99 +++++++++++++++++++++++++++++++++++
 4 files changed, 118 insertions(+)
 create mode 100644 drivers/char/hw_random/hisi-trng-v2.c

-- 
2.8.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] hw_random: add HiSilicon TRNG driver support
  2019-10-31  8:34 [PATCH 0/2] hw_random: hisilicon - add HiSilicon TRNG V2 support Zaibo Xu
@ 2019-10-31  8:34 ` Zaibo Xu
  2019-10-31  8:34 ` [PATCH 2/2] MAINTAINERS: Add maintainer for HiSilicon TRNG V2 driver Zaibo Xu
  2019-11-08 15:20 ` [PATCH 0/2] hw_random: hisilicon - add HiSilicon TRNG V2 support Herbert Xu
  2 siblings, 0 replies; 4+ messages in thread
From: Zaibo Xu @ 2019-10-31  8:34 UTC (permalink / raw)
  To: herbert, mpm
  Cc: linux-crypto, jonathan.cameron, wangzhou1, linuxarm,
	wangkefeng.wang, qianweili, forest.zhouchang

This series adds HiSilicon true random number generator(TRNG)
driver in hw_random subsystem.

Signed-off-by: Zaibo Xu <xuzaibo@huawei.com>
Signed-off-by: Weili Qian <qianweili@huawei.com>
---
 drivers/char/hw_random/Kconfig        | 13 +++++
 drivers/char/hw_random/Makefile       |  1 +
 drivers/char/hw_random/hisi-trng-v2.c | 99 +++++++++++++++++++++++++++++++++++
 3 files changed, 113 insertions(+)
 create mode 100644 drivers/char/hw_random/hisi-trng-v2.c

diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
index 87a1c30..7c7fecf 100644
--- a/drivers/char/hw_random/Kconfig
+++ b/drivers/char/hw_random/Kconfig
@@ -308,6 +308,19 @@ config HW_RANDOM_HISI
 
 	  If unsure, say Y.
 
+config HW_RANDOM_HISI_V2
+	tristate "HiSilicon True Random Number Generator V2 support"
+	depends on HW_RANDOM && ARM64 && ACPI
+	default HW_RANDOM
+	help
+	  This driver provides kernel-side support for the True Random Number
+	  Generator V2 hardware found on HiSilicon Hi1620 SoC.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called hisi-trng-v2.
+
+	  If unsure, say Y.
+
 config HW_RANDOM_ST
 	tristate "ST Microelectronics HW Random Number Generator support"
 	depends on HW_RANDOM && ARCH_STI
diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
index 17b6d4e..a7801b4 100644
--- a/drivers/char/hw_random/Makefile
+++ b/drivers/char/hw_random/Makefile
@@ -27,6 +27,7 @@ obj-$(CONFIG_HW_RANDOM_NOMADIK) += nomadik-rng.o
 obj-$(CONFIG_HW_RANDOM_PSERIES) += pseries-rng.o
 obj-$(CONFIG_HW_RANDOM_POWERNV) += powernv-rng.o
 obj-$(CONFIG_HW_RANDOM_HISI)	+= hisi-rng.o
+obj-$(CONFIG_HW_RANDOM_HISI_V2) += hisi-trng-v2.o
 obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o
 obj-$(CONFIG_HW_RANDOM_IPROC_RNG200) += iproc-rng200.o
 obj-$(CONFIG_HW_RANDOM_ST) += st-rng.o
diff --git a/drivers/char/hw_random/hisi-trng-v2.c b/drivers/char/hw_random/hisi-trng-v2.c
new file mode 100644
index 0000000..6a65b82
--- /dev/null
+++ b/drivers/char/hw_random/hisi-trng-v2.c
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2019 HiSilicon Limited. */
+
+#include <linux/acpi.h>
+#include <linux/err.h>
+#include <linux/hw_random.h>
+#include <linux/io.h>
+#include <linux/iopoll.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/random.h>
+
+#define HISI_TRNG_REG		0x00F0
+#define HISI_TRNG_BYTES		4
+#define HISI_TRNG_QUALITY	512
+#define SLEEP_US		10
+#define TIMEOUT_US		10000
+
+struct hisi_trng {
+	void __iomem *base;
+	struct hwrng rng;
+};
+
+static int hisi_trng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
+{
+	struct hisi_trng *trng;
+	int currsize = 0;
+	u32 val = 0;
+	u32 ret;
+
+	trng = container_of(rng, struct hisi_trng, rng);
+
+	do {
+		ret = readl_poll_timeout(trng->base + HISI_TRNG_REG, val,
+					 val, SLEEP_US, TIMEOUT_US);
+		if (ret)
+			return currsize;
+
+		if (max - currsize >= HISI_TRNG_BYTES) {
+			memcpy(buf + currsize, &val, HISI_TRNG_BYTES);
+			currsize += HISI_TRNG_BYTES;
+			if (currsize == max)
+				return currsize;
+			continue;
+		}
+
+		/* copy remaining bytes */
+		memcpy(buf + currsize, &val, max - currsize);
+		currsize = max;
+	} while (currsize < max);
+
+	return currsize;
+}
+
+static int hisi_trng_probe(struct platform_device *pdev)
+{
+	struct hisi_trng *trng;
+	int ret;
+
+	trng = devm_kzalloc(&pdev->dev, sizeof(*trng), GFP_KERNEL);
+	if (!trng)
+		return -ENOMEM;
+
+	trng->base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(trng->base))
+		return PTR_ERR(trng->base);
+
+	trng->rng.name = pdev->name;
+	trng->rng.read = hisi_trng_read;
+	trng->rng.quality = HISI_TRNG_QUALITY;
+
+	ret = devm_hwrng_register(&pdev->dev, &trng->rng);
+	if (ret)
+		dev_err(&pdev->dev, "failed to register hwrng!\n");
+
+	return ret;
+}
+
+static const struct acpi_device_id hisi_trng_acpi_match[] = {
+	{ "HISI02B3", 0 },
+	{ }
+};
+MODULE_DEVICE_TABLE(acpi, hisi_trng_acpi_match);
+
+static struct platform_driver hisi_trng_driver = {
+	.probe		= hisi_trng_probe,
+	.driver		= {
+		.name	= "hisi-trng-v2",
+		.acpi_match_table = ACPI_PTR(hisi_trng_acpi_match),
+	},
+};
+
+module_platform_driver(hisi_trng_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_AUTHOR("Weili Qian <qianweili@huawei.com>");
+MODULE_AUTHOR("Zaibo Xu <xuzaibo@huawei.com>");
+MODULE_DESCRIPTION("HiSilicon true random number generator V2 driver");
-- 
2.8.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/2] MAINTAINERS: Add maintainer for HiSilicon TRNG V2 driver
  2019-10-31  8:34 [PATCH 0/2] hw_random: hisilicon - add HiSilicon TRNG V2 support Zaibo Xu
  2019-10-31  8:34 ` [PATCH 1/2] hw_random: add HiSilicon TRNG driver support Zaibo Xu
@ 2019-10-31  8:34 ` Zaibo Xu
  2019-11-08 15:20 ` [PATCH 0/2] hw_random: hisilicon - add HiSilicon TRNG V2 support Herbert Xu
  2 siblings, 0 replies; 4+ messages in thread
From: Zaibo Xu @ 2019-10-31  8:34 UTC (permalink / raw)
  To: herbert, mpm
  Cc: linux-crypto, jonathan.cameron, wangzhou1, linuxarm,
	wangkefeng.wang, qianweili, forest.zhouchang

Here adds maintainer information for HiSilicon TRNG V2 driver.

Signed-off-by: Zaibo Xu <xuzaibo@huawei.com>
---
 MAINTAINERS | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index bbf8985..342345d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -7399,6 +7399,11 @@ W:	http://www.hisilicon.com
 S:	Maintained
 F:	drivers/net/ethernet/hisilicon/hns3/
 
+HISILICON TRUE RANDOM NUMBER GENERATOR V2 SUPPORT
+M:	Zaibo Xu <xuzaibo@huawei.com>
+S:	Maintained
+F:	drivers/char/hw_random/hisi-trng-v2.c
+
 HISILICON LPC BUS DRIVER
 M:	john.garry@huawei.com
 W:	http://www.hisilicon.com
-- 
2.8.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 0/2] hw_random: hisilicon - add HiSilicon TRNG V2 support
  2019-10-31  8:34 [PATCH 0/2] hw_random: hisilicon - add HiSilicon TRNG V2 support Zaibo Xu
  2019-10-31  8:34 ` [PATCH 1/2] hw_random: add HiSilicon TRNG driver support Zaibo Xu
  2019-10-31  8:34 ` [PATCH 2/2] MAINTAINERS: Add maintainer for HiSilicon TRNG V2 driver Zaibo Xu
@ 2019-11-08 15:20 ` Herbert Xu
  2 siblings, 0 replies; 4+ messages in thread
From: Herbert Xu @ 2019-11-08 15:20 UTC (permalink / raw)
  To: Zaibo Xu
  Cc: mpm, linux-crypto, jonathan.cameron, wangzhou1, linuxarm,
	wangkefeng.wang, qianweili, forest.zhouchang

On Thu, Oct 31, 2019 at 04:34:28PM +0800, Zaibo Xu wrote:
> This adds HiSilicon True Random Number Generator (TRNG) version 2
> driver in hw_random subsystem.
> 
> Zaibo Xu (2):
>   hw_random: add HiSilicon TRNG driver support
>   MAINTAINERS: Add maintainer for HiSilicon TRNG V2 driver
> 
>  MAINTAINERS                           |  5 ++
>  drivers/char/hw_random/Kconfig        | 13 +++++
>  drivers/char/hw_random/Makefile       |  1 +
>  drivers/char/hw_random/hisi-trng-v2.c | 99 +++++++++++++++++++++++++++++++++++
>  4 files changed, 118 insertions(+)
>  create mode 100644 drivers/char/hw_random/hisi-trng-v2.c

All applied.  Thanks.
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-31  8:34 [PATCH 0/2] hw_random: hisilicon - add HiSilicon TRNG V2 support Zaibo Xu
2019-10-31  8:34 ` [PATCH 1/2] hw_random: add HiSilicon TRNG driver support Zaibo Xu
2019-10-31  8:34 ` [PATCH 2/2] MAINTAINERS: Add maintainer for HiSilicon TRNG V2 driver Zaibo Xu
2019-11-08 15:20 ` [PATCH 0/2] hw_random: hisilicon - add HiSilicon TRNG V2 support Herbert Xu

Linux-Crypto Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-crypto/0 linux-crypto/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-crypto linux-crypto/ https://lore.kernel.org/linux-crypto \
		linux-crypto@vger.kernel.org
	public-inbox-index linux-crypto

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-crypto


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git