[PATCH] hw_random: mediatek: Setup default RNG quality

2018-01-09 Thread sean.wang
From: Sean Wang 

When hw_random device's quality is non-zero, it will automatically fill
the kernel's entropy pool at boot.  For the purpose, one conservative
quality value is being picked up as the default value.

Signed-off-by: Sean Wang 
---
 drivers/char/hw_random/mtk-rng.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/char/hw_random/mtk-rng.c b/drivers/char/hw_random/mtk-rng.c
index 8da7bcf..7f99cd5 100644
--- a/drivers/char/hw_random/mtk-rng.c
+++ b/drivers/char/hw_random/mtk-rng.c
@@ -135,6 +135,7 @@ static int mtk_rng_probe(struct platform_device *pdev)
 #endif
priv->rng.read = mtk_rng_read;
priv->rng.priv = (unsigned long)>dev;
+   priv->rng.quality = 900;
 
priv->clk = devm_clk_get(>dev, "rng");
if (IS_ERR(priv->clk)) {
-- 
2.7.4



[PATCH v2 1/3] dt-bindings: rng: add MediaTek MT7622 Hardware Random Generator bindings

2017-06-12 Thread sean.wang
From: Sean Wang 

Document the bindings used by MediaTek MT7622 SoC hardware random number
generator.

Signed-off-by: Sean Wang 
---
 Documentation/devicetree/bindings/rng/mtk-rng.txt | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/Documentation/devicetree/bindings/rng/mtk-rng.txt 
b/Documentation/devicetree/bindings/rng/mtk-rng.txt
index a6d62a2..366b99b 100644
--- a/Documentation/devicetree/bindings/rng/mtk-rng.txt
+++ b/Documentation/devicetree/bindings/rng/mtk-rng.txt
@@ -2,7 +2,9 @@ Device-Tree bindings for Mediatek random number generator
 found in Mediatek SoC family
 
 Required properties:
-- compatible   : Should be "mediatek,mt7623-rng"
+- compatible   : Should be
+   "mediatek,mt7622-rng",  "mediatek,mt7623-rng" : for 
MT7622
+   "mediatek,mt7623-rng" : for MT7623
 - clocks   : list of clock specifiers, corresponding to
  entries in clock-names property;
 - clock-names  : Should contain "rng" entries;
-- 
1.9.1



[PATCH v2 0/3] add support of hardware random generator on MediaTek MT7622

2017-06-12 Thread sean.wang
From: Sean Wang 

Changes since v1:
- update the bindings with the specific "mediatek,mt7622-rng"
  instead of the generic one as "mediatek,generic-rng"

The series add support of hardware RNG on MediaTek MT7622 and
, runtime PM support and add me as the maintainer for the existing
and following chipset.

Sean Wang (3):
  dt-bindings: rng: add MediaTek MT7622 Hardware Random Generator
bindings
  hwrng: mtk - add runtime PM support
  MAINTAINERS: add entry for MediaTek Random Number Generator

 Documentation/devicetree/bindings/rng/mtk-rng.txt |  4 ++-
 MAINTAINERS   |  5 +++
 drivers/char/hw_random/mtk-rng.c  | 42 +++
 3 files changed, 50 insertions(+), 1 deletion(-)

-- 
1.9.1



[PATCH v2 3/3] MAINTAINERS: add entry for MediaTek Random Number Generator

2017-06-12 Thread sean.wang
From: Sean Wang 

I work for MediaTek on maintaining the MediaTek RNG driver
for the existing SoCs and adding support for the following
SoCs.

Signed-off-by: Sean Wang 
---
 MAINTAINERS | 5 +
 1 file changed, 5 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index cf86519..da473d0 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -8262,6 +8262,11 @@ L:   linux-wirel...@vger.kernel.org
 S: Maintained
 F: drivers/net/wireless/mediatek/mt7601u/
 
+MEDIATEK RANDOM NUMBER GENERATOR SUPPORT
+M:  Sean Wang 
+S:  Maintained
+F:  drivers/char/hw_random/mtk-rng.c
+
 MEGACHIPS STDP-GE-B850V3-FW LVDS/DP++ BRIDGES
 M: Peter Senna Tschudin 
 M: Martin Donnelly 
-- 
1.9.1



[PATCH v2 2/3] hwrng: mtk - add runtime PM support

2017-06-12 Thread sean.wang
From: Sean Wang 

Add runtime PM support.  There will be the benefit on SoCs where the
clock to the RNG used can be shutdown.

Signed-off-by: Sean Wang 
---
 drivers/char/hw_random/mtk-rng.c | 42 
 1 file changed, 42 insertions(+)

diff --git a/drivers/char/hw_random/mtk-rng.c b/drivers/char/hw_random/mtk-rng.c
index df8eb54..8da7bcf 100644
--- a/drivers/char/hw_random/mtk-rng.c
+++ b/drivers/char/hw_random/mtk-rng.c
@@ -25,6 +25,10 @@
 #include 
 #include 
 #include 
+#include 
+
+/* Runtime PM autosuspend timeout: */
+#define RNG_AUTOSUSPEND_TIMEOUT100
 
 #define USEC_POLL  2
 #define TIMEOUT_POLL   20
@@ -90,6 +94,8 @@ static int mtk_rng_read(struct hwrng *rng, void *buf, size_t 
max, bool wait)
struct mtk_rng *priv = to_mtk_rng(rng);
int retval = 0;
 
+   pm_runtime_get_sync((struct device *)priv->rng.priv);
+
while (max >= sizeof(u32)) {
if (!mtk_rng_wait_ready(rng, wait))
break;
@@ -100,6 +106,9 @@ static int mtk_rng_read(struct hwrng *rng, void *buf, 
size_t max, bool wait)
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;
 }
 
@@ -120,9 +129,12 @@ static int mtk_rng_probe(struct platform_device *pdev)
return -ENOMEM;
 
priv->rng.name = pdev->name;
+#ifndef CONFIG_PM
priv->rng.init = mtk_rng_init;
priv->rng.cleanup = mtk_rng_cleanup;
+#endif
priv->rng.read = mtk_rng_read;
+   priv->rng.priv = (unsigned long)>dev;
 
priv->clk = devm_clk_get(>dev, "rng");
if (IS_ERR(priv->clk)) {
@@ -142,11 +154,40 @@ static int mtk_rng_probe(struct platform_device *pdev)
return ret;
}
 
+   dev_set_drvdata(>dev, priv);
+   pm_runtime_set_autosuspend_delay(>dev, RNG_AUTOSUSPEND_TIMEOUT);
+   pm_runtime_use_autosuspend(>dev);
+   pm_runtime_enable(>dev);
+
dev_info(>dev, "registered RNG driver\n");
 
return 0;
 }
 
+#ifdef CONFIG_PM
+static int mtk_rng_runtime_suspend(struct device *dev)
+{
+   struct mtk_rng *priv = dev_get_drvdata(dev);
+
+   mtk_rng_cleanup(>rng);
+
+   return 0;
+}
+
+static int mtk_rng_runtime_resume(struct device *dev)
+{
+   struct mtk_rng *priv = dev_get_drvdata(dev);
+
+   return mtk_rng_init(>rng);
+}
+
+static UNIVERSAL_DEV_PM_OPS(mtk_rng_pm_ops, mtk_rng_runtime_suspend,
+   mtk_rng_runtime_resume, NULL);
+#define MTK_RNG_PM_OPS (_rng_pm_ops)
+#else  /* CONFIG_PM */
+#define MTK_RNG_PM_OPS NULL
+#endif /* CONFIG_PM */
+
 static const struct of_device_id mtk_rng_match[] = {
{ .compatible = "mediatek,mt7623-rng" },
{},
@@ -157,6 +198,7 @@ static int mtk_rng_probe(struct platform_device *pdev)
.probe  = mtk_rng_probe,
.driver = {
.name = MTK_RNG_DEV,
+   .pm = MTK_RNG_PM_OPS,
.of_match_table = mtk_rng_match,
},
 };
-- 
1.9.1



[PATCH 1/4] dt-bindings: rng: add generic bindings for MediaTek SoCs

2017-05-31 Thread sean.wang
From: Sean Wang 

Add the generic binding for allowing the support of RNG on MediaTek SoCs
such as MT7622.

Signed-off-by: Sean Wang 
---
 Documentation/devicetree/bindings/rng/mtk-rng.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/Documentation/devicetree/bindings/rng/mtk-rng.txt 
b/Documentation/devicetree/bindings/rng/mtk-rng.txt
index a6d62a2..0772913 100644
--- a/Documentation/devicetree/bindings/rng/mtk-rng.txt
+++ b/Documentation/devicetree/bindings/rng/mtk-rng.txt
@@ -2,7 +2,8 @@ Device-Tree bindings for Mediatek random number generator
 found in Mediatek SoC family
 
 Required properties:
-- compatible   : Should be "mediatek,mt7623-rng"
+- compatible   : Should be "mediatek,generic-rng" or
+   "mediatek,mt7623-rng".
 - clocks   : list of clock specifiers, corresponding to
  entries in clock-names property;
 - clock-names  : Should contain "rng" entries;
-- 
1.9.1



[PATCH 2/4] hwrng: mtk - add support for MT7622 SoC

2017-05-31 Thread sean.wang
From: Sean Wang 

Add "mediatek,generic-rng" allowing most of RNG on MediaTek SoCs to be
compatible with the driver including MT7622 SoC.

Signed-off-by: Sean Wang 
---
 drivers/char/hw_random/mtk-rng.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/char/hw_random/mtk-rng.c b/drivers/char/hw_random/mtk-rng.c
index df8eb54..3a1e1f1 100644
--- a/drivers/char/hw_random/mtk-rng.c
+++ b/drivers/char/hw_random/mtk-rng.c
@@ -148,6 +148,7 @@ static int mtk_rng_probe(struct platform_device *pdev)
 }
 
 static const struct of_device_id mtk_rng_match[] = {
+   { .compatible = "mediatek,generic-rng" },
{ .compatible = "mediatek,mt7623-rng" },
{},
 };
-- 
1.9.1



[PATCH 4/4] MAINTAINERS: add entry for MediaTek Random Number Generator

2017-05-31 Thread sean.wang
From: Sean Wang 

I work for MediaTek on maintaining the MediaTek RNG driver
for the existing SoCs and adding support for the following
SoCs.

Signed-off-by: Sean Wang 
---
 MAINTAINERS | 5 +
 1 file changed, 5 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index cf86519..da473d0 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -8262,6 +8262,11 @@ L:   linux-wirel...@vger.kernel.org
 S: Maintained
 F: drivers/net/wireless/mediatek/mt7601u/
 
+MEDIATEK RANDOM NUMBER GENERATOR SUPPORT
+M:  Sean Wang 
+S:  Maintained
+F:  drivers/char/hw_random/mtk-rng.c
+
 MEGACHIPS STDP-GE-B850V3-FW LVDS/DP++ BRIDGES
 M: Peter Senna Tschudin 
 M: Martin Donnelly 
-- 
1.9.1



[PATCH 3/4] hwrng: mtk - add runtime PM support

2017-05-31 Thread sean.wang
From: Sean Wang 

Add runtime PM support.  There will be the benefit on SoCs where the
clock to the RNG used can be shutdown.

Signed-off-by: Sean Wang 
---
 drivers/char/hw_random/mtk-rng.c | 42 
 1 file changed, 42 insertions(+)

diff --git a/drivers/char/hw_random/mtk-rng.c b/drivers/char/hw_random/mtk-rng.c
index 3a1e1f1..59ded4e 100644
--- a/drivers/char/hw_random/mtk-rng.c
+++ b/drivers/char/hw_random/mtk-rng.c
@@ -25,6 +25,10 @@
 #include 
 #include 
 #include 
+#include 
+
+/* Runtime PM autosuspend timeout: */
+#define RNG_AUTOSUSPEND_TIMEOUT100
 
 #define USEC_POLL  2
 #define TIMEOUT_POLL   20
@@ -90,6 +94,8 @@ static int mtk_rng_read(struct hwrng *rng, void *buf, size_t 
max, bool wait)
struct mtk_rng *priv = to_mtk_rng(rng);
int retval = 0;
 
+   pm_runtime_get_sync((struct device *)priv->rng.priv);
+
while (max >= sizeof(u32)) {
if (!mtk_rng_wait_ready(rng, wait))
break;
@@ -100,6 +106,9 @@ static int mtk_rng_read(struct hwrng *rng, void *buf, 
size_t max, bool wait)
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;
 }
 
@@ -120,9 +129,12 @@ static int mtk_rng_probe(struct platform_device *pdev)
return -ENOMEM;
 
priv->rng.name = pdev->name;
+#ifndef CONFIG_PM
priv->rng.init = mtk_rng_init;
priv->rng.cleanup = mtk_rng_cleanup;
+#endif
priv->rng.read = mtk_rng_read;
+   priv->rng.priv = (unsigned long)>dev;
 
priv->clk = devm_clk_get(>dev, "rng");
if (IS_ERR(priv->clk)) {
@@ -142,11 +154,40 @@ static int mtk_rng_probe(struct platform_device *pdev)
return ret;
}
 
+   dev_set_drvdata(>dev, priv);
+   pm_runtime_set_autosuspend_delay(>dev, RNG_AUTOSUSPEND_TIMEOUT);
+   pm_runtime_use_autosuspend(>dev);
+   pm_runtime_enable(>dev);
+
dev_info(>dev, "registered RNG driver\n");
 
return 0;
 }
 
+#ifdef CONFIG_PM
+static int mtk_rng_runtime_suspend(struct device *dev)
+{
+   struct mtk_rng *priv = dev_get_drvdata(dev);
+
+   mtk_rng_cleanup(>rng);
+
+   return 0;
+}
+
+static int mtk_rng_runtime_resume(struct device *dev)
+{
+   struct mtk_rng *priv = dev_get_drvdata(dev);
+
+   return mtk_rng_init(>rng);
+}
+
+static UNIVERSAL_DEV_PM_OPS(mtk_rng_pm_ops, mtk_rng_runtime_suspend,
+   mtk_rng_runtime_resume, NULL);
+#define MTK_RNG_PM_OPS (_rng_pm_ops)
+#else  /* CONFIG_PM */
+#define MTK_RNG_PM_OPS NULL
+#endif /* CONFIG_PM */
+
 static const struct of_device_id mtk_rng_match[] = {
{ .compatible = "mediatek,generic-rng" },
{ .compatible = "mediatek,mt7623-rng" },
@@ -158,6 +199,7 @@ static int mtk_rng_probe(struct platform_device *pdev)
.probe  = mtk_rng_probe,
.driver = {
.name = MTK_RNG_DEV,
+   .pm = MTK_RNG_PM_OPS,
.of_match_table = mtk_rng_match,
},
 };
-- 
1.9.1



[PATCH v2 2/2] hwrng: mtk: Add driver for hardware random generator on MT7623 SoC

2017-04-20 Thread sean.wang
From: Sean Wang 

This patch adds support for hardware random generator on MT7623 SoC
and should also work on other similar Mediatek SoCs. Currently,
the driver is already tested successfully with rng-tools.

Signed-off-by: Sean Wang 
Reviewed-by: PrasannaKumar Muralidharan 
---
 drivers/char/hw_random/Kconfig   |  14 
 drivers/char/hw_random/Makefile  |   1 +
 drivers/char/hw_random/mtk-rng.c | 168 +++
 3 files changed, 183 insertions(+)
 create mode 100644 drivers/char/hw_random/mtk-rng.c

diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
index 0cafe08..df5e7c2 100644
--- a/drivers/char/hw_random/Kconfig
+++ b/drivers/char/hw_random/Kconfig
@@ -423,6 +423,20 @@ config HW_RANDOM_CAVIUM
 
  If unsure, say Y.
 
+config HW_RANDOM_MTK
+   tristate "Mediatek Random Number Generator support"
+   depends on HW_RANDOM
+   depends on ARCH_MEDIATEK || COMPILE_TEST
+   default y
+   ---help---
+ This driver provides kernel-side support for the Random Number
+ Generator hardware found on Mediatek SoCs.
+
+ To compile this driver as a module, choose M here. the
+ module will be called mtk-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 5f52b1e..59eacb7 100644
--- a/drivers/char/hw_random/Makefile
+++ b/drivers/char/hw_random/Makefile
@@ -36,3 +36,4 @@ obj-$(CONFIG_HW_RANDOM_STM32) += stm32-rng.o
 obj-$(CONFIG_HW_RANDOM_PIC32) += pic32-rng.o
 obj-$(CONFIG_HW_RANDOM_MESON) += meson-rng.o
 obj-$(CONFIG_HW_RANDOM_CAVIUM) += cavium-rng.o cavium-rng-vf.o
+obj-$(CONFIG_HW_RANDOM_MTK)+= mtk-rng.o
diff --git a/drivers/char/hw_random/mtk-rng.c b/drivers/char/hw_random/mtk-rng.c
new file mode 100644
index 000..df8eb54
--- /dev/null
+++ b/drivers/char/hw_random/mtk-rng.c
@@ -0,0 +1,168 @@
+/*
+ * Driver for Mediatek Hardware Random Number Generator
+ *
+ * Copyright (C) 2017 Sean Wang 
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+#define MTK_RNG_DEV KBUILD_MODNAME
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define USEC_POLL  2
+#define TIMEOUT_POLL   20
+
+#define RNG_CTRL   0x00
+#define RNG_EN BIT(0)
+#define RNG_READY  BIT(31)
+
+#define RNG_DATA   0x08
+
+#define to_mtk_rng(p)  container_of(p, struct mtk_rng, rng)
+
+struct mtk_rng {
+   void __iomem *base;
+   struct clk *clk;
+   struct hwrng rng;
+};
+
+static int mtk_rng_init(struct hwrng *rng)
+{
+   struct mtk_rng *priv = to_mtk_rng(rng);
+   u32 val;
+   int err;
+
+   err = clk_prepare_enable(priv->clk);
+   if (err)
+   return err;
+
+   val = readl(priv->base + RNG_CTRL);
+   val |= RNG_EN;
+   writel(val, priv->base + RNG_CTRL);
+
+   return 0;
+}
+
+static void mtk_rng_cleanup(struct hwrng *rng)
+{
+   struct mtk_rng *priv = to_mtk_rng(rng);
+   u32 val;
+
+   val = readl(priv->base + RNG_CTRL);
+   val &= ~RNG_EN;
+   writel(val, priv->base + RNG_CTRL);
+
+   clk_disable_unprepare(priv->clk);
+}
+
+static bool mtk_rng_wait_ready(struct hwrng *rng, bool wait)
+{
+   struct mtk_rng *priv = to_mtk_rng(rng);
+   int ready;
+
+   ready = readl(priv->base + RNG_CTRL) & RNG_READY;
+   if (!ready && wait)
+   readl_poll_timeout_atomic(priv->base + RNG_CTRL, ready,
+ ready & RNG_READY, USEC_POLL,
+ TIMEOUT_POLL);
+   return !!ready;
+}
+
+static int mtk_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
+{
+   struct mtk_rng *priv = to_mtk_rng(rng);
+   int retval = 0;
+
+   while (max >= sizeof(u32)) {
+   if (!mtk_rng_wait_ready(rng, wait))
+   break;
+
+   *(u32 *)buf = readl(priv->base + RNG_DATA);
+   retval += sizeof(u32);
+   buf += sizeof(u32);
+   max -= sizeof(u32);
+   }
+
+   return retval || !wait ? retval : -EIO;
+}
+
+static int mtk_rng_probe(struct platform_device *pdev)
+{
+   struct resource *res;
+   int ret;
+   struct mtk_rng *priv;
+
+   

[PATCH v2 1/2] dt-bindings: hwrng: Add Mediatek hardware random generator bindings

2017-04-20 Thread sean.wang
From: Sean Wang 

Document the devicetree bindings for Mediatek random number
generator which could be found on MT7623 SoC or other similar
Mediatek SoCs.

Signed-off-by: Sean Wang 
Acked-by: Rob Herring 
---
 Documentation/devicetree/bindings/rng/mtk-rng.txt | 18 ++
 1 file changed, 18 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rng/mtk-rng.txt

diff --git a/Documentation/devicetree/bindings/rng/mtk-rng.txt 
b/Documentation/devicetree/bindings/rng/mtk-rng.txt
new file mode 100644
index 000..a6d62a2
--- /dev/null
+++ b/Documentation/devicetree/bindings/rng/mtk-rng.txt
@@ -0,0 +1,18 @@
+Device-Tree bindings for Mediatek random number generator
+found in Mediatek SoC family
+
+Required properties:
+- compatible   : Should be "mediatek,mt7623-rng"
+- clocks   : list of clock specifiers, corresponding to
+ entries in clock-names property;
+- clock-names  : Should contain "rng" entries;
+- reg  : Specifies base physical address and size of the registers
+
+Example:
+
+rng: rng@1020f000 {
+   compatible = "mediatek,mt7623-rng";
+   reg = <0 0x1020f000 0 0x1000>;
+   clocks = < CLK_INFRA_TRNG>;
+   clock-names = "rng";
+};
-- 
1.9.1



[PATCH v2 0/2] hwrng: mtk: add support for hardware random generator on MT7623 SoC

2017-04-20 Thread sean.wang
From: Sean Wang 

This patchset introduces support for Mediatek hardware random generator (RNG)
Currently, the driver is already tested successfully with rng-tools on MT7623
SoC. And it should also be workable on other similar Mediatek SoCs.

Changes since v1:
- remove unnecessary warning message
- remove mistakenly changed line
- refine macro definition with keeping one space between name and define
- remove redundant platform_get_resource() call

Sean Wang (2):
  dt-bindings: hwrng: Add Mediatek hardware random generator bindings
  hwrng: mtk: Add driver for hardware random generator on MT7623 SoC

 Documentation/devicetree/bindings/rng/mtk-rng.txt |  18 +++
 drivers/char/hw_random/Kconfig|  14 ++
 drivers/char/hw_random/Makefile   |   1 +
 drivers/char/hw_random/mtk-rng.c  | 168 ++
 4 files changed, 201 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rng/mtk-rng.txt
 create mode 100644 drivers/char/hw_random/mtk-rng.c

-- 
1.9.1



[PATCH 1/2] dt-bindings: hwrng: Add Mediatek hardware random generator bindings

2017-04-13 Thread sean.wang
From: Sean Wang 

Document the devicetree bindings for Mediatek random number
generator which could be found on MT7623 SoC or other similar
Mediatek SoCs.

Signed-off-by: Sean Wang 
---
 Documentation/devicetree/bindings/rng/mtk-rng.txt | 18 ++
 1 file changed, 18 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rng/mtk-rng.txt

diff --git a/Documentation/devicetree/bindings/rng/mtk-rng.txt 
b/Documentation/devicetree/bindings/rng/mtk-rng.txt
new file mode 100644
index 000..a6d62a2
--- /dev/null
+++ b/Documentation/devicetree/bindings/rng/mtk-rng.txt
@@ -0,0 +1,18 @@
+Device-Tree bindings for Mediatek random number generator
+found in Mediatek SoC family
+
+Required properties:
+- compatible   : Should be "mediatek,mt7623-rng"
+- clocks   : list of clock specifiers, corresponding to
+ entries in clock-names property;
+- clock-names  : Should contain "rng" entries;
+- reg  : Specifies base physical address and size of the registers
+
+Example:
+
+rng: rng@1020f000 {
+   compatible = "mediatek,mt7623-rng";
+   reg = <0 0x1020f000 0 0x1000>;
+   clocks = < CLK_INFRA_TRNG>;
+   clock-names = "rng";
+};
-- 
1.9.1



[PATCH 2/2] hwrng: mtk: Add driver for hardware random generator on MT7623 SoC

2017-04-13 Thread sean.wang
From: Sean Wang 

This patch adds support for hardware random generator on MT7623 SoC
and should also work on other similar Mediatek SoCs. Currently,
the driver is already tested successfully with rng-tools.

Signed-off-by: Sean Wang 
---
 drivers/char/hw_random/Kconfig   |  16 +++-
 drivers/char/hw_random/Makefile  |   2 +-
 drivers/char/hw_random/mtk-rng.c | 174 +++
 3 files changed, 190 insertions(+), 2 deletions(-)
 create mode 100644 drivers/char/hw_random/mtk-rng.c

diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
index 0cafe08..af782ce 100644
--- a/drivers/char/hw_random/Kconfig
+++ b/drivers/char/hw_random/Kconfig
@@ -419,10 +419,24 @@ config HW_RANDOM_CAVIUM
  Generator hardware found on Cavium SoCs.
 
  To compile this driver as a module, choose M here: the
- module will be called cavium_rng.
+ module will be called mtk-rng.
 
  If unsure, say Y.
 
+config HW_RANDOM_MTK
+   tristate "Mediatek Random Number Generator support"
+   depends on HW_RANDOM
+   depends on ARCH_MEDIATEK || COMPILE_TEST
+   default y
+   ---help---
+ This driver provides kernel-side support for the Random Number
+ Generator hardware found on Mediatek SoCs.
+
+ To compile this driver as a module, choose M here. the
+ module will be called mtk-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 5f52b1e..68be716 100644
--- a/drivers/char/hw_random/Makefile
+++ b/drivers/char/hw_random/Makefile
@@ -1,7 +1,6 @@
 #
 # Makefile for HW Random Number Generator (RNG) device drivers.
 #
-
 obj-$(CONFIG_HW_RANDOM) += rng-core.o
 rng-core-y := core.o
 obj-$(CONFIG_HW_RANDOM_TIMERIOMEM) += timeriomem-rng.o
@@ -36,3 +35,4 @@ obj-$(CONFIG_HW_RANDOM_STM32) += stm32-rng.o
 obj-$(CONFIG_HW_RANDOM_PIC32) += pic32-rng.o
 obj-$(CONFIG_HW_RANDOM_MESON) += meson-rng.o
 obj-$(CONFIG_HW_RANDOM_CAVIUM) += cavium-rng.o cavium-rng-vf.o
+obj-$(CONFIG_HW_RANDOM_MTK)+= mtk-rng.o
diff --git a/drivers/char/hw_random/mtk-rng.c b/drivers/char/hw_random/mtk-rng.c
new file mode 100644
index 000..6561ee0
--- /dev/null
+++ b/drivers/char/hw_random/mtk-rng.c
@@ -0,0 +1,174 @@
+/*
+ * Driver for Mediatek Hardware Random Number Generator
+ *
+ * Copyright (C) 2017 Sean Wang 
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+#define MTK_RNG_DEV KBUILD_MODNAME
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define USEC_POLL  2
+#define TIMEOUT_POLL   20
+
+#define RNG_CTRL   0x00
+#define  RNG_ENBIT(0)
+#define  RNG_READY BIT(31)
+
+#define RNG_DATA   0x08
+
+#define to_mtk_rng(p)  container_of(p, struct mtk_rng, rng)
+
+struct mtk_rng {
+   struct device   *dev;
+   void __iomem *base;
+   struct clk *clk;
+   struct hwrng rng;
+};
+
+static int mtk_rng_init(struct hwrng *rng)
+{
+   struct mtk_rng *priv = to_mtk_rng(rng);
+   u32 val;
+   int err;
+
+   err = clk_prepare_enable(priv->clk);
+   if (err)
+   return err;
+
+   val = readl(priv->base + RNG_CTRL);
+   val |= RNG_EN;
+   writel(val, priv->base + RNG_CTRL);
+
+   return 0;
+}
+
+static void mtk_rng_cleanup(struct hwrng *rng)
+{
+   struct mtk_rng *priv = to_mtk_rng(rng);
+   u32 val;
+
+   val = readl(priv->base + RNG_CTRL);
+   val &= ~RNG_EN;
+   writel(val, priv->base + RNG_CTRL);
+
+   clk_disable_unprepare(priv->clk);
+}
+
+static bool mtk_rng_wait_ready(struct hwrng *rng, bool wait)
+{
+   struct mtk_rng *priv = to_mtk_rng(rng);
+   int ready;
+
+   ready = readl(priv->base + RNG_CTRL) & RNG_READY;
+   if (!ready && wait)
+   readl_poll_timeout_atomic(priv->base + RNG_CTRL, ready,
+ ready & RNG_READY, USEC_POLL,
+ TIMEOUT_POLL);
+   return !!ready;
+}
+
+static int mtk_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
+{
+   struct mtk_rng *priv = to_mtk_rng(rng);
+   int retval = 0;
+
+   while (max >= sizeof(u32)) {
+   if (!mtk_rng_wait_ready(rng, wait))
+  

[PATCH 0/2] hwrng: mtk: add support for hardware random generator on MT7623 SoC

2017-04-13 Thread sean.wang
From: Sean Wang 

This patchset introduces support for Mediatek hardware random generator (RNG)
Currently, the driver is already tested successfully with rng-tools on MT7623
SoC. And it should also be workable on other similar Mediatek SoCs.

SoC that also works on other similar SoCs. 
Sean Wang (2):
  dt-bindings: hwrng: Add Mediatek hardware random generator bindings
  hwrng: mtk: Add driver for hardware random generator on MT7623 SoC

 Documentation/devicetree/bindings/rng/mtk-rng.txt |  18 +++
 drivers/char/hw_random/Kconfig|  16 +-
 drivers/char/hw_random/Makefile   |   2 +-
 drivers/char/hw_random/mtk-rng.c  | 174 ++
 4 files changed, 208 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/rng/mtk-rng.txt
 create mode 100644 drivers/char/hw_random/mtk-rng.c

-- 
1.9.1