devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/7] hwrng: exynos: Add support for Exynos850
@ 2024-06-18 20:45 Sam Protsenko
  2024-06-18 20:45 ` [PATCH v2 1/7] dt-bindings: rng: Add Exynos850 support to exynos-trng Sam Protsenko
                   ` (6 more replies)
  0 siblings, 7 replies; 21+ messages in thread
From: Sam Protsenko @ 2024-06-18 20:45 UTC (permalink / raw)
  To: Łukasz Stelmach, Krzysztof Kozlowski, Rob Herring,
	Conor Dooley
  Cc: Anand Moon, Olivia Mackall, Herbert Xu, Alim Akhtar,
	linux-samsung-soc, linux-crypto, devicetree, linux-arm-kernel,
	linux-kernel

Exynos850 has True Random Number Generator (TRNG) block which is very
similar to Exynos5250 for which the driver already exists
(exynos-trng.c). There are two differences though:
  1. Additional SSS PCLK clock has to be enabled to make TRNG registers
     accessible.
  2. All SSS registers (including TRNG area) are protected with
     TrustZone and can only be accessed from EL3 monitor. So the
     corresponding SMC calls have to be used instead to interact with
     TRNG block.

This patch series enables TRNG support on Exynos850 SoC. It was tested
on the E850-96 board running Debian rootfs like this:

    8<-------------------------------------------------------------->8
    # cat /sys/devices/virtual/misc/hw_random/rng_current
    12081400.rng

    # dd if=/dev/hwrng bs=100000 count=1 > /dev/null
    ...
    122KB/s

    # apt install rng-tools5
    # rngtest -c 1000 < /dev/hwrng
    ...
    rngtest: starting FIPS tests...
    rngtest: bits received from input: 20000032
    rngtest: FIPS 140-2 successes: 1000
    rngtest: FIPS 140-2 failures: 0
    rngtest: FIPS 140-2(2001-10-10) Monobit: 0
    rngtest: FIPS 140-2(2001-10-10) Poker: 0
    rngtest: FIPS 140-2(2001-10-10) Runs: 0
    rngtest: FIPS 140-2(2001-10-10) Long run: 0
    rngtest: FIPS 140-2(2001-10-10) Continuous run: 0
    rngtest: input channel speed: (min=941.855; avg=965.515;
             max=968.236)Kibits/s
    rngtest: FIPS tests speed: (min=49.542; avg=52.886;
             max=53.577)Mibits/s
    rngtest: Program run time: 20590194 microseconds
    8<-------------------------------------------------------------->8

Changes in v2:
  - Removed the patch for renaming the dt-bindings doc file
  - Added the patch for using devm_clk_get_enabled() to get the clock
  - Addressed all review comments for v1 series

Sam Protsenko (7):
  dt-bindings: rng: Add Exynos850 support to exynos-trng
  hwrng: exynos: Improve coding style
  hwrng: exynos: Use devm_clk_get_enabled() to get the clock
  hwrng: exynos: Implement bus clock control
  hwrng: exynos: Add SMC based TRNG operation
  hwrng: exynos: Enable Exynos850 support
  arm64: dts: exynos850: Enable TRNG

 .../bindings/rng/samsung,exynos5250-trng.yaml |  40 +++-
 arch/arm64/boot/dts/exynos/exynos850.dtsi     |   8 +
 drivers/char/hw_random/exynos-trng.c          | 216 +++++++++++++-----
 3 files changed, 206 insertions(+), 58 deletions(-)

-- 
2.39.2


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

* [PATCH v2 1/7] dt-bindings: rng: Add Exynos850 support to exynos-trng
  2024-06-18 20:45 [PATCH v2 0/7] hwrng: exynos: Add support for Exynos850 Sam Protsenko
@ 2024-06-18 20:45 ` Sam Protsenko
  2024-06-20  7:27   ` Krzysztof Kozlowski
  2024-06-18 20:45 ` [PATCH v2 2/7] hwrng: exynos: Improve coding style Sam Protsenko
                   ` (5 subsequent siblings)
  6 siblings, 1 reply; 21+ messages in thread
From: Sam Protsenko @ 2024-06-18 20:45 UTC (permalink / raw)
  To: Łukasz Stelmach, Krzysztof Kozlowski, Rob Herring,
	Conor Dooley
  Cc: Anand Moon, Olivia Mackall, Herbert Xu, Alim Akhtar,
	linux-samsung-soc, linux-crypto, devicetree, linux-arm-kernel,
	linux-kernel

The TRNG block in Exynos850 is pretty much the same as in Exynos5250,
but there are two clocks that has to be controlled to make it work:
  1. Functional (operating) clock: called ACLK in Exynos850, the same as
     "secss" clock in Exynos5250
  2. Interface (bus) clock: called PCLK in Exynos850. It has to be
     enabled in order to access TRNG registers

Document Exynos850 compatible and the related clock changes.

Signed-off-by: Sam Protsenko <semen.protsenko@linaro.org>
---
Changes in v2:
  - Removed example added in v1

 .../bindings/rng/samsung,exynos5250-trng.yaml | 40 +++++++++++++++++--
 1 file changed, 36 insertions(+), 4 deletions(-)

diff --git a/Documentation/devicetree/bindings/rng/samsung,exynos5250-trng.yaml b/Documentation/devicetree/bindings/rng/samsung,exynos5250-trng.yaml
index 765d9f9edd6e..1a71935d8a19 100644
--- a/Documentation/devicetree/bindings/rng/samsung,exynos5250-trng.yaml
+++ b/Documentation/devicetree/bindings/rng/samsung,exynos5250-trng.yaml
@@ -12,14 +12,17 @@ maintainers:
 
 properties:
   compatible:
-    const: samsung,exynos5250-trng
+    enum:
+      - samsung,exynos5250-trng
+      - samsung,exynos850-trng
 
   clocks:
-    maxItems: 1
+    minItems: 1
+    maxItems: 2
 
   clock-names:
-    items:
-      - const: secss
+    minItems: 1
+    maxItems: 2
 
   reg:
     maxItems: 1
@@ -30,6 +33,35 @@ required:
   - clock-names
   - reg
 
+allOf:
+  - if:
+      properties:
+        compatible:
+          contains:
+            const: samsung,exynos850-trng
+
+    then:
+      properties:
+        clocks:
+          items:
+            - description: SSS (Security Sub System) operating clock
+            - description: SSS (Security Sub System) bus clock
+
+        clock-names:
+          items:
+            - const: secss
+            - const: pclk
+
+    else:
+      properties:
+        clocks:
+          items:
+            - description: SSS (Security Sub System) operating clock
+
+        clock-names:
+          items:
+            - const: secss
+
 additionalProperties: false
 
 examples:
-- 
2.39.2


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

* [PATCH v2 2/7] hwrng: exynos: Improve coding style
  2024-06-18 20:45 [PATCH v2 0/7] hwrng: exynos: Add support for Exynos850 Sam Protsenko
  2024-06-18 20:45 ` [PATCH v2 1/7] dt-bindings: rng: Add Exynos850 support to exynos-trng Sam Protsenko
@ 2024-06-18 20:45 ` Sam Protsenko
       [not found]   ` <CGME20240620085239eucas1p1d49a6bae77e1fc857e4459f87c619a83@eucas1p1.samsung.com>
  2024-06-18 20:45 ` [PATCH v2 3/7] hwrng: exynos: Use devm_clk_get_enabled() to get the clock Sam Protsenko
                   ` (4 subsequent siblings)
  6 siblings, 1 reply; 21+ messages in thread
From: Sam Protsenko @ 2024-06-18 20:45 UTC (permalink / raw)
  To: Łukasz Stelmach, Krzysztof Kozlowski, Rob Herring,
	Conor Dooley
  Cc: Anand Moon, Olivia Mackall, Herbert Xu, Alim Akhtar,
	linux-samsung-soc, linux-crypto, devicetree, linux-arm-kernel,
	linux-kernel

Fix obvious style issues. Some of those were found with checkpatch, and
some just contradict the kernel coding style guide.

No functional change.

Signed-off-by: Sam Protsenko <semen.protsenko@linaro.org>
Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
---
Changes in v2:
  - Added Krzysztof's R-b tag

 drivers/char/hw_random/exynos-trng.c | 61 +++++++++++++---------------
 1 file changed, 29 insertions(+), 32 deletions(-)

diff --git a/drivers/char/hw_random/exynos-trng.c b/drivers/char/hw_random/exynos-trng.c
index 0ed5d22fe667..88a5088ed34d 100644
--- a/drivers/char/hw_random/exynos-trng.c
+++ b/drivers/char/hw_random/exynos-trng.c
@@ -23,45 +23,41 @@
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
 
-#define EXYNOS_TRNG_CLKDIV         (0x0)
-
-#define EXYNOS_TRNG_CTRL           (0x20)
-#define EXYNOS_TRNG_CTRL_RNGEN     BIT(31)
-
-#define EXYNOS_TRNG_POST_CTRL      (0x30)
-#define EXYNOS_TRNG_ONLINE_CTRL    (0x40)
-#define EXYNOS_TRNG_ONLINE_STAT    (0x44)
-#define EXYNOS_TRNG_ONLINE_MAXCHI2 (0x48)
-#define EXYNOS_TRNG_FIFO_CTRL      (0x50)
-#define EXYNOS_TRNG_FIFO_0         (0x80)
-#define EXYNOS_TRNG_FIFO_1         (0x84)
-#define EXYNOS_TRNG_FIFO_2         (0x88)
-#define EXYNOS_TRNG_FIFO_3         (0x8c)
-#define EXYNOS_TRNG_FIFO_4         (0x90)
-#define EXYNOS_TRNG_FIFO_5         (0x94)
-#define EXYNOS_TRNG_FIFO_6         (0x98)
-#define EXYNOS_TRNG_FIFO_7         (0x9c)
-#define EXYNOS_TRNG_FIFO_LEN       (8)
-#define EXYNOS_TRNG_CLOCK_RATE     (500000)
-
+#define EXYNOS_TRNG_CLKDIV		0x0
+
+#define EXYNOS_TRNG_CTRL		0x20
+#define EXYNOS_TRNG_CTRL_RNGEN		BIT(31)
+
+#define EXYNOS_TRNG_POST_CTRL		0x30
+#define EXYNOS_TRNG_ONLINE_CTRL		0x40
+#define EXYNOS_TRNG_ONLINE_STAT		0x44
+#define EXYNOS_TRNG_ONLINE_MAXCHI2	0x48
+#define EXYNOS_TRNG_FIFO_CTRL		0x50
+#define EXYNOS_TRNG_FIFO_0		0x80
+#define EXYNOS_TRNG_FIFO_1		0x84
+#define EXYNOS_TRNG_FIFO_2		0x88
+#define EXYNOS_TRNG_FIFO_3		0x8c
+#define EXYNOS_TRNG_FIFO_4		0x90
+#define EXYNOS_TRNG_FIFO_5		0x94
+#define EXYNOS_TRNG_FIFO_6		0x98
+#define EXYNOS_TRNG_FIFO_7		0x9c
+#define EXYNOS_TRNG_FIFO_LEN		8
+#define EXYNOS_TRNG_CLOCK_RATE		500000
 
 struct exynos_trng_dev {
-	struct device    *dev;
-	void __iomem     *mem;
-	struct clk       *clk;
-	struct hwrng rng;
+	struct device	*dev;
+	void __iomem	*mem;
+	struct clk	*clk;
+	struct hwrng	rng;
 };
 
 static int exynos_trng_do_read(struct hwrng *rng, void *data, size_t max,
 			       bool wait)
 {
-	struct exynos_trng_dev *trng;
+	struct exynos_trng_dev *trng = (struct exynos_trng_dev *)rng->priv;
 	int val;
 
 	max = min_t(size_t, max, (EXYNOS_TRNG_FIFO_LEN * 4));
-
-	trng = (struct exynos_trng_dev *)rng->priv;
-
 	writel_relaxed(max * 8, trng->mem + EXYNOS_TRNG_FIFO_CTRL);
 	val = readl_poll_timeout(trng->mem + EXYNOS_TRNG_FIFO_CTRL, val,
 				 val == 0, 200, 1000000);
@@ -122,7 +118,7 @@ static int exynos_trng_probe(struct platform_device *pdev)
 
 	trng->rng.init = exynos_trng_init;
 	trng->rng.read = exynos_trng_do_read;
-	trng->rng.priv = (unsigned long) trng;
+	trng->rng.priv = (unsigned long)trng;
 
 	platform_set_drvdata(pdev, trng);
 	trng->dev = &pdev->dev;
@@ -175,7 +171,7 @@ static int exynos_trng_probe(struct platform_device *pdev)
 
 static void exynos_trng_remove(struct platform_device *pdev)
 {
-	struct exynos_trng_dev *trng =  platform_get_drvdata(pdev);
+	struct exynos_trng_dev *trng = platform_get_drvdata(pdev);
 
 	clk_disable_unprepare(trng->clk);
 
@@ -204,7 +200,7 @@ static int exynos_trng_resume(struct device *dev)
 }
 
 static DEFINE_SIMPLE_DEV_PM_OPS(exynos_trng_pm_ops, exynos_trng_suspend,
-			 exynos_trng_resume);
+				exynos_trng_resume);
 
 static const struct of_device_id exynos_trng_dt_match[] = {
 	{
@@ -225,6 +221,7 @@ static struct platform_driver exynos_trng_driver = {
 };
 
 module_platform_driver(exynos_trng_driver);
+
 MODULE_AUTHOR("Łukasz Stelmach");
 MODULE_DESCRIPTION("H/W TRNG driver for Exynos chips");
 MODULE_LICENSE("GPL v2");
-- 
2.39.2


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

* [PATCH v2 3/7] hwrng: exynos: Use devm_clk_get_enabled() to get the clock
  2024-06-18 20:45 [PATCH v2 0/7] hwrng: exynos: Add support for Exynos850 Sam Protsenko
  2024-06-18 20:45 ` [PATCH v2 1/7] dt-bindings: rng: Add Exynos850 support to exynos-trng Sam Protsenko
  2024-06-18 20:45 ` [PATCH v2 2/7] hwrng: exynos: Improve coding style Sam Protsenko
@ 2024-06-18 20:45 ` Sam Protsenko
  2024-06-20  7:29   ` Krzysztof Kozlowski
  2024-06-20  9:27   ` Anand Moon
  2024-06-18 20:45 ` [PATCH v2 4/7] hwrng: exynos: Implement bus clock control Sam Protsenko
                   ` (3 subsequent siblings)
  6 siblings, 2 replies; 21+ messages in thread
From: Sam Protsenko @ 2024-06-18 20:45 UTC (permalink / raw)
  To: Łukasz Stelmach, Krzysztof Kozlowski, Rob Herring,
	Conor Dooley
  Cc: Anand Moon, Olivia Mackall, Herbert Xu, Alim Akhtar,
	linux-samsung-soc, linux-crypto, devicetree, linux-arm-kernel,
	linux-kernel

Use devm_clk_get_enabled() helper instead of calling devm_clk_get() and
then clk_prepare_enable(). It simplifies the error handling and makes
the code more compact. Also use dev_err_probe() to handle possible
-EPROBE_DEFER errors if the clock is not available yet.

Signed-off-by: Sam Protsenko <semen.protsenko@linaro.org>
---
Changes in v2:
  - No changes (it's a new patch added in v2)

 drivers/char/hw_random/exynos-trng.c | 21 ++++-----------------
 1 file changed, 4 insertions(+), 17 deletions(-)

diff --git a/drivers/char/hw_random/exynos-trng.c b/drivers/char/hw_random/exynos-trng.c
index 88a5088ed34d..91c210d80a3d 100644
--- a/drivers/char/hw_random/exynos-trng.c
+++ b/drivers/char/hw_random/exynos-trng.c
@@ -134,32 +134,23 @@ static int exynos_trng_probe(struct platform_device *pdev)
 		goto err_pm_get;
 	}
 
-	trng->clk = devm_clk_get(&pdev->dev, "secss");
+	trng->clk = devm_clk_get_enabled(&pdev->dev, "secss");
 	if (IS_ERR(trng->clk)) {
-		ret = PTR_ERR(trng->clk);
-		dev_err(&pdev->dev, "Could not get clock.\n");
-		goto err_clock;
-	}
-
-	ret = clk_prepare_enable(trng->clk);
-	if (ret) {
-		dev_err(&pdev->dev, "Could not enable the clk.\n");
+		ret = dev_err_probe(&pdev->dev, PTR_ERR(trng->clk),
+				    "Could not get clock");
 		goto err_clock;
 	}
 
 	ret = devm_hwrng_register(&pdev->dev, &trng->rng);
 	if (ret) {
 		dev_err(&pdev->dev, "Could not register hwrng device.\n");
-		goto err_register;
+		goto err_clock;
 	}
 
 	dev_info(&pdev->dev, "Exynos True Random Number Generator.\n");
 
 	return 0;
 
-err_register:
-	clk_disable_unprepare(trng->clk);
-
 err_clock:
 	pm_runtime_put_noidle(&pdev->dev);
 
@@ -171,10 +162,6 @@ static int exynos_trng_probe(struct platform_device *pdev)
 
 static void exynos_trng_remove(struct platform_device *pdev)
 {
-	struct exynos_trng_dev *trng = platform_get_drvdata(pdev);
-
-	clk_disable_unprepare(trng->clk);
-
 	pm_runtime_put_sync(&pdev->dev);
 	pm_runtime_disable(&pdev->dev);
 }
-- 
2.39.2


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

* [PATCH v2 4/7] hwrng: exynos: Implement bus clock control
  2024-06-18 20:45 [PATCH v2 0/7] hwrng: exynos: Add support for Exynos850 Sam Protsenko
                   ` (2 preceding siblings ...)
  2024-06-18 20:45 ` [PATCH v2 3/7] hwrng: exynos: Use devm_clk_get_enabled() to get the clock Sam Protsenko
@ 2024-06-18 20:45 ` Sam Protsenko
  2024-06-20  7:29   ` Krzysztof Kozlowski
  2024-06-20  9:27   ` Anand Moon
  2024-06-18 20:45 ` [PATCH v2 5/7] hwrng: exynos: Add SMC based TRNG operation Sam Protsenko
                   ` (2 subsequent siblings)
  6 siblings, 2 replies; 21+ messages in thread
From: Sam Protsenko @ 2024-06-18 20:45 UTC (permalink / raw)
  To: Łukasz Stelmach, Krzysztof Kozlowski, Rob Herring,
	Conor Dooley
  Cc: Anand Moon, Olivia Mackall, Herbert Xu, Alim Akhtar,
	linux-samsung-soc, linux-crypto, devicetree, linux-arm-kernel,
	linux-kernel

Some SoCs like Exynos850 might require the SSS bus clock (PCLK) to be
enabled in order to access TRNG registers. Add and handle the optional
PCLK clock accordingly to make it possible.

Signed-off-by: Sam Protsenko <semen.protsenko@linaro.org>
---
Changes in v2:
  - Used devm_clk_get_optional_enabled() to avoid calling
    clk_prepare_enable() for PCLK

 drivers/char/hw_random/exynos-trng.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/drivers/char/hw_random/exynos-trng.c b/drivers/char/hw_random/exynos-trng.c
index 91c210d80a3d..99a0b271ffb7 100644
--- a/drivers/char/hw_random/exynos-trng.c
+++ b/drivers/char/hw_random/exynos-trng.c
@@ -47,7 +47,8 @@
 struct exynos_trng_dev {
 	struct device	*dev;
 	void __iomem	*mem;
-	struct clk	*clk;
+	struct clk	*clk;	/* operating clock */
+	struct clk	*pclk;	/* bus clock */
 	struct hwrng	rng;
 };
 
@@ -141,6 +142,13 @@ static int exynos_trng_probe(struct platform_device *pdev)
 		goto err_clock;
 	}
 
+	trng->pclk = devm_clk_get_optional_enabled(&pdev->dev, "pclk");
+	if (IS_ERR(trng->pclk)) {
+		ret = dev_err_probe(&pdev->dev, PTR_ERR(trng->pclk),
+				    "Could not get pclk");
+		goto err_clock;
+	}
+
 	ret = devm_hwrng_register(&pdev->dev, &trng->rng);
 	if (ret) {
 		dev_err(&pdev->dev, "Could not register hwrng device.\n");
-- 
2.39.2


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

* [PATCH v2 5/7] hwrng: exynos: Add SMC based TRNG operation
  2024-06-18 20:45 [PATCH v2 0/7] hwrng: exynos: Add support for Exynos850 Sam Protsenko
                   ` (3 preceding siblings ...)
  2024-06-18 20:45 ` [PATCH v2 4/7] hwrng: exynos: Implement bus clock control Sam Protsenko
@ 2024-06-18 20:45 ` Sam Protsenko
       [not found]   ` <CGME20240620134610eucas1p2fbcd7218bc220bf568ea117acf2f4781@eucas1p2.samsung.com>
  2024-06-18 20:45 ` [PATCH v2 6/7] hwrng: exynos: Enable Exynos850 support Sam Protsenko
  2024-06-18 20:45 ` [PATCH v2 7/7] arm64: dts: exynos850: Enable TRNG Sam Protsenko
  6 siblings, 1 reply; 21+ messages in thread
From: Sam Protsenko @ 2024-06-18 20:45 UTC (permalink / raw)
  To: Łukasz Stelmach, Krzysztof Kozlowski, Rob Herring,
	Conor Dooley
  Cc: Anand Moon, Olivia Mackall, Herbert Xu, Alim Akhtar,
	linux-samsung-soc, linux-crypto, devicetree, linux-arm-kernel,
	linux-kernel

On some Exynos chips like Exynos850 the access to Security Sub System
(SSS) registers is protected with TrustZone, and therefore only possible
from EL3 monitor software. The Linux kernel is running in EL1, so the
only way for the driver to obtain TRNG data is via SMC calls to EL3
monitor. Implement such SMC operation and use it when EXYNOS_SMC flag is
set in the corresponding chip driver data.

Signed-off-by: Sam Protsenko <semen.protsenko@linaro.org>
Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
---
Changes in v2:
  - Used the "reversed Christmas tree" style in the variable declaration
    block in exynos_trng_do_read_smc()
  - Renamed .quirks to .flags in the driver structure
  - Added Krzysztof's R-b tag

 drivers/char/hw_random/exynos-trng.c | 133 +++++++++++++++++++++++++--
 1 file changed, 123 insertions(+), 10 deletions(-)

diff --git a/drivers/char/hw_random/exynos-trng.c b/drivers/char/hw_random/exynos-trng.c
index 99a0b271ffb7..497d6018c6ba 100644
--- a/drivers/char/hw_random/exynos-trng.c
+++ b/drivers/char/hw_random/exynos-trng.c
@@ -10,6 +10,7 @@
  * Krzysztof Kozłowski <krzk@kernel.org>
  */
 
+#include <linux/arm-smccc.h>
 #include <linux/clk.h>
 #include <linux/crypto.h>
 #include <linux/delay.h>
@@ -22,6 +23,7 @@
 #include <linux/mod_devicetable.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
+#include <linux/property.h>
 
 #define EXYNOS_TRNG_CLKDIV		0x0
 
@@ -44,16 +46,41 @@
 #define EXYNOS_TRNG_FIFO_LEN		8
 #define EXYNOS_TRNG_CLOCK_RATE		500000
 
+/* Driver feature flags */
+#define EXYNOS_SMC			BIT(0)
+
+#define EXYNOS_SMC_CALL_VAL(func_num)			\
+	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,		\
+			   ARM_SMCCC_SMC_32,		\
+			   ARM_SMCCC_OWNER_SIP,		\
+			   func_num)
+
+/* SMC command for DTRNG access */
+#define SMC_CMD_RANDOM			EXYNOS_SMC_CALL_VAL(0x1012)
+
+/* SMC_CMD_RANDOM: arguments */
+#define HWRNG_INIT			0x0
+#define HWRNG_EXIT			0x1
+#define HWRNG_GET_DATA			0x2
+#define HWRNG_RESUME			0x3
+
+/* SMC_CMD_RANDOM: return values */
+#define HWRNG_RET_OK			0x0
+#define HWRNG_RET_RETRY_ERROR		0x2
+
+#define HWRNG_MAX_TRIES			100
+
 struct exynos_trng_dev {
 	struct device	*dev;
 	void __iomem	*mem;
 	struct clk	*clk;	/* operating clock */
 	struct clk	*pclk;	/* bus clock */
 	struct hwrng	rng;
+	unsigned long	flags;
 };
 
-static int exynos_trng_do_read(struct hwrng *rng, void *data, size_t max,
-			       bool wait)
+static int exynos_trng_do_read_reg(struct hwrng *rng, void *data, size_t max,
+				   bool wait)
 {
 	struct exynos_trng_dev *trng = (struct exynos_trng_dev *)rng->priv;
 	int val;
@@ -70,7 +97,40 @@ static int exynos_trng_do_read(struct hwrng *rng, void *data, size_t max,
 	return max;
 }
 
-static int exynos_trng_init(struct hwrng *rng)
+static int exynos_trng_do_read_smc(struct hwrng *rng, void *data, size_t max,
+				   bool wait)
+{
+	struct arm_smccc_res res;
+	unsigned int copied = 0;
+	u32 *buf = data;
+	int tries = 0;
+
+	while (copied < max) {
+		arm_smccc_smc(SMC_CMD_RANDOM, HWRNG_GET_DATA, 0, 0, 0, 0, 0, 0,
+			      &res);
+		switch (res.a0) {
+		case HWRNG_RET_OK:
+			*buf++ = res.a2;
+			*buf++ = res.a3;
+			copied += 8;
+			tries = 0;
+			break;
+		case HWRNG_RET_RETRY_ERROR:
+			if (!wait)
+				return copied;
+			if (++tries >= HWRNG_MAX_TRIES)
+				return copied;
+			cond_resched();
+			break;
+		default:
+			return -EIO;
+		}
+	}
+
+	return copied;
+}
+
+static int exynos_trng_init_reg(struct hwrng *rng)
 {
 	struct exynos_trng_dev *trng = (struct exynos_trng_dev *)rng->priv;
 	unsigned long sss_rate;
@@ -103,6 +163,17 @@ static int exynos_trng_init(struct hwrng *rng)
 	return 0;
 }
 
+static int exynos_trng_init_smc(struct hwrng *rng)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_smc(SMC_CMD_RANDOM, HWRNG_INIT, 0, 0, 0, 0, 0, 0, &res);
+	if (res.a0 != HWRNG_RET_OK)
+		return -EIO;
+
+	return 0;
+}
+
 static int exynos_trng_probe(struct platform_device *pdev)
 {
 	struct exynos_trng_dev *trng;
@@ -112,21 +183,29 @@ static int exynos_trng_probe(struct platform_device *pdev)
 	if (!trng)
 		return ret;
 
+	platform_set_drvdata(pdev, trng);
+	trng->dev = &pdev->dev;
+
+	trng->flags = (unsigned long)device_get_match_data(&pdev->dev);
+
 	trng->rng.name = devm_kstrdup(&pdev->dev, dev_name(&pdev->dev),
 				      GFP_KERNEL);
 	if (!trng->rng.name)
 		return ret;
 
-	trng->rng.init = exynos_trng_init;
-	trng->rng.read = exynos_trng_do_read;
 	trng->rng.priv = (unsigned long)trng;
 
-	platform_set_drvdata(pdev, trng);
-	trng->dev = &pdev->dev;
+	if (trng->flags & EXYNOS_SMC) {
+		trng->rng.init = exynos_trng_init_smc;
+		trng->rng.read = exynos_trng_do_read_smc;
+	} else {
+		trng->rng.init = exynos_trng_init_reg;
+		trng->rng.read = exynos_trng_do_read_reg;
 
-	trng->mem = devm_platform_ioremap_resource(pdev, 0);
-	if (IS_ERR(trng->mem))
-		return PTR_ERR(trng->mem);
+		trng->mem = devm_platform_ioremap_resource(pdev, 0);
+		if (IS_ERR(trng->mem))
+			return PTR_ERR(trng->mem);
+	}
 
 	pm_runtime_enable(&pdev->dev);
 	ret = pm_runtime_resume_and_get(&pdev->dev);
@@ -170,12 +249,31 @@ static int exynos_trng_probe(struct platform_device *pdev)
 
 static void exynos_trng_remove(struct platform_device *pdev)
 {
+	struct exynos_trng_dev *trng = platform_get_drvdata(pdev);
+
+	if (trng->flags & EXYNOS_SMC) {
+		struct arm_smccc_res res;
+
+		arm_smccc_smc(SMC_CMD_RANDOM, HWRNG_EXIT, 0, 0, 0, 0, 0, 0,
+			      &res);
+	}
+
 	pm_runtime_put_sync(&pdev->dev);
 	pm_runtime_disable(&pdev->dev);
 }
 
 static int exynos_trng_suspend(struct device *dev)
 {
+	struct exynos_trng_dev *trng = dev_get_drvdata(dev);
+	struct arm_smccc_res res;
+
+	if (trng->flags & EXYNOS_SMC) {
+		arm_smccc_smc(SMC_CMD_RANDOM, HWRNG_EXIT, 0, 0, 0, 0, 0, 0,
+			      &res);
+		if (res.a0 != HWRNG_RET_OK)
+			return -EIO;
+	}
+
 	pm_runtime_put_sync(dev);
 
 	return 0;
@@ -183,6 +281,7 @@ static int exynos_trng_suspend(struct device *dev)
 
 static int exynos_trng_resume(struct device *dev)
 {
+	struct exynos_trng_dev *trng = dev_get_drvdata(dev);
 	int ret;
 
 	ret = pm_runtime_resume_and_get(dev);
@@ -191,6 +290,20 @@ static int exynos_trng_resume(struct device *dev)
 		return ret;
 	}
 
+	if (trng->flags & EXYNOS_SMC) {
+		struct arm_smccc_res res;
+
+		arm_smccc_smc(SMC_CMD_RANDOM, HWRNG_RESUME, 0, 0, 0, 0, 0, 0,
+			      &res);
+		if (res.a0 != HWRNG_RET_OK)
+			return -EIO;
+
+		arm_smccc_smc(SMC_CMD_RANDOM, HWRNG_INIT, 0, 0, 0, 0, 0, 0,
+			      &res);
+		if (res.a0 != HWRNG_RET_OK)
+			return -EIO;
+	}
+
 	return 0;
 }
 
-- 
2.39.2


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

* [PATCH v2 6/7] hwrng: exynos: Enable Exynos850 support
  2024-06-18 20:45 [PATCH v2 0/7] hwrng: exynos: Add support for Exynos850 Sam Protsenko
                   ` (4 preceding siblings ...)
  2024-06-18 20:45 ` [PATCH v2 5/7] hwrng: exynos: Add SMC based TRNG operation Sam Protsenko
@ 2024-06-18 20:45 ` Sam Protsenko
  2024-06-20  7:29   ` Krzysztof Kozlowski
       [not found]   ` <CGME20240620084749eucas1p18f46cc9aa0983afb75e1677b399d12f8@eucas1p1.samsung.com>
  2024-06-18 20:45 ` [PATCH v2 7/7] arm64: dts: exynos850: Enable TRNG Sam Protsenko
  6 siblings, 2 replies; 21+ messages in thread
From: Sam Protsenko @ 2024-06-18 20:45 UTC (permalink / raw)
  To: Łukasz Stelmach, Krzysztof Kozlowski, Rob Herring,
	Conor Dooley
  Cc: Anand Moon, Olivia Mackall, Herbert Xu, Alim Akhtar,
	linux-samsung-soc, linux-crypto, devicetree, linux-arm-kernel,
	linux-kernel

Add Exynos850 compatible and its driver data. It's only possible to
access TRNG block via SMC calls in Exynos850, so specify that fact using
EXYNOS_SMC flag in the driver data.

Signed-off-by: Sam Protsenko <semen.protsenko@linaro.org>
---
Changes in v2:
  - Changed QUIRK_SMC to EXYNOS_SMC to reflect the name change in the
    previous patch

 drivers/char/hw_random/exynos-trng.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/char/hw_random/exynos-trng.c b/drivers/char/hw_random/exynos-trng.c
index 497d6018c6ba..841598037cce 100644
--- a/drivers/char/hw_random/exynos-trng.c
+++ b/drivers/char/hw_random/exynos-trng.c
@@ -313,6 +313,9 @@ static DEFINE_SIMPLE_DEV_PM_OPS(exynos_trng_pm_ops, exynos_trng_suspend,
 static const struct of_device_id exynos_trng_dt_match[] = {
 	{
 		.compatible = "samsung,exynos5250-trng",
+	}, {
+		.compatible = "samsung,exynos850-trng",
+		.data = (void *)EXYNOS_SMC,
 	},
 	{ },
 };
-- 
2.39.2


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

* [PATCH v2 7/7] arm64: dts: exynos850: Enable TRNG
  2024-06-18 20:45 [PATCH v2 0/7] hwrng: exynos: Add support for Exynos850 Sam Protsenko
                   ` (5 preceding siblings ...)
  2024-06-18 20:45 ` [PATCH v2 6/7] hwrng: exynos: Enable Exynos850 support Sam Protsenko
@ 2024-06-18 20:45 ` Sam Protsenko
  2024-06-20  7:31   ` Krzysztof Kozlowski
  2024-07-01 12:27   ` (subset) " Krzysztof Kozlowski
  6 siblings, 2 replies; 21+ messages in thread
From: Sam Protsenko @ 2024-06-18 20:45 UTC (permalink / raw)
  To: Łukasz Stelmach, Krzysztof Kozlowski, Rob Herring,
	Conor Dooley
  Cc: Anand Moon, Olivia Mackall, Herbert Xu, Alim Akhtar,
	linux-samsung-soc, linux-crypto, devicetree, linux-arm-kernel,
	linux-kernel

Add True Random Number Generator (TRNG) node to Exynos850 SoC dtsi.

Signed-off-by: Sam Protsenko <semen.protsenko@linaro.org>
---
Changes in v2:
  - (no changes)

 arch/arm64/boot/dts/exynos/exynos850.dtsi | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/arch/arm64/boot/dts/exynos/exynos850.dtsi b/arch/arm64/boot/dts/exynos/exynos850.dtsi
index 0706c8534ceb..f1c8b4613cbc 100644
--- a/arch/arm64/boot/dts/exynos/exynos850.dtsi
+++ b/arch/arm64/boot/dts/exynos/exynos850.dtsi
@@ -416,6 +416,14 @@ pinctrl_core: pinctrl@12070000 {
 			interrupts = <GIC_SPI 451 IRQ_TYPE_LEVEL_HIGH>;
 		};
 
+		trng: rng@12081400 {
+			compatible = "samsung,exynos850-trng";
+			reg = <0x12081400 0x100>;
+			clocks = <&cmu_core CLK_GOUT_SSS_ACLK>,
+				 <&cmu_core CLK_GOUT_SSS_PCLK>;
+			clock-names = "secss", "pclk";
+		};
+
 		pinctrl_hsi: pinctrl@13430000 {
 			compatible = "samsung,exynos850-pinctrl";
 			reg = <0x13430000 0x1000>;
-- 
2.39.2


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

* Re: [PATCH v2 1/7] dt-bindings: rng: Add Exynos850 support to exynos-trng
  2024-06-18 20:45 ` [PATCH v2 1/7] dt-bindings: rng: Add Exynos850 support to exynos-trng Sam Protsenko
@ 2024-06-20  7:27   ` Krzysztof Kozlowski
  0 siblings, 0 replies; 21+ messages in thread
From: Krzysztof Kozlowski @ 2024-06-20  7:27 UTC (permalink / raw)
  To: Sam Protsenko, Łukasz Stelmach, Krzysztof Kozlowski,
	Rob Herring, Conor Dooley
  Cc: Anand Moon, Olivia Mackall, Herbert Xu, Alim Akhtar,
	linux-samsung-soc, linux-crypto, devicetree, linux-arm-kernel,
	linux-kernel

On 18/06/2024 22:45, Sam Protsenko wrote:
> The TRNG block in Exynos850 is pretty much the same as in Exynos5250,
> but there are two clocks that has to be controlled to make it work:
>   1. Functional (operating) clock: called ACLK in Exynos850, the same as
>      "secss" clock in Exynos5250
>   2. Interface (bus) clock: called PCLK in Exynos850. It has to be
>      enabled in order to access TRNG registers
> 
> Document Exynos850 compatible and the related clock changes.
> 
> Signed-off-by: Sam Protsenko <semen.protsenko@linaro.org>
> ---


Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>

Best regards,
Krzysztof


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

* Re: [PATCH v2 3/7] hwrng: exynos: Use devm_clk_get_enabled() to get the clock
  2024-06-18 20:45 ` [PATCH v2 3/7] hwrng: exynos: Use devm_clk_get_enabled() to get the clock Sam Protsenko
@ 2024-06-20  7:29   ` Krzysztof Kozlowski
  2024-06-20  9:27   ` Anand Moon
  1 sibling, 0 replies; 21+ messages in thread
From: Krzysztof Kozlowski @ 2024-06-20  7:29 UTC (permalink / raw)
  To: Sam Protsenko, Łukasz Stelmach, Krzysztof Kozlowski,
	Rob Herring, Conor Dooley
  Cc: Anand Moon, Olivia Mackall, Herbert Xu, Alim Akhtar,
	linux-samsung-soc, linux-crypto, devicetree, linux-arm-kernel,
	linux-kernel

On 18/06/2024 22:45, Sam Protsenko wrote:
> Use devm_clk_get_enabled() helper instead of calling devm_clk_get() and
> then clk_prepare_enable(). It simplifies the error handling and makes
> the code more compact. Also use dev_err_probe() to handle possible
> -EPROBE_DEFER errors if the clock is not available yet.
> 
> Signed-off-by: Sam Protsenko <semen.protsenko@linaro.org>
> ---

Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>

Best regards,
Krzysztof


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

* Re: [PATCH v2 4/7] hwrng: exynos: Implement bus clock control
  2024-06-18 20:45 ` [PATCH v2 4/7] hwrng: exynos: Implement bus clock control Sam Protsenko
@ 2024-06-20  7:29   ` Krzysztof Kozlowski
  2024-06-20  9:27   ` Anand Moon
  1 sibling, 0 replies; 21+ messages in thread
From: Krzysztof Kozlowski @ 2024-06-20  7:29 UTC (permalink / raw)
  To: Sam Protsenko, Łukasz Stelmach, Krzysztof Kozlowski,
	Rob Herring, Conor Dooley
  Cc: Anand Moon, Olivia Mackall, Herbert Xu, Alim Akhtar,
	linux-samsung-soc, linux-crypto, devicetree, linux-arm-kernel,
	linux-kernel

On 18/06/2024 22:45, Sam Protsenko wrote:
> Some SoCs like Exynos850 might require the SSS bus clock (PCLK) to be
> enabled in order to access TRNG registers. Add and handle the optional
> PCLK clock accordingly to make it possible.
> 
> Signed-off-by: Sam Protsenko <semen.protsenko@linaro.org>
> ---

Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>

Best regards,
Krzysztof


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

* Re: [PATCH v2 6/7] hwrng: exynos: Enable Exynos850 support
  2024-06-18 20:45 ` [PATCH v2 6/7] hwrng: exynos: Enable Exynos850 support Sam Protsenko
@ 2024-06-20  7:29   ` Krzysztof Kozlowski
       [not found]   ` <CGME20240620084749eucas1p18f46cc9aa0983afb75e1677b399d12f8@eucas1p1.samsung.com>
  1 sibling, 0 replies; 21+ messages in thread
From: Krzysztof Kozlowski @ 2024-06-20  7:29 UTC (permalink / raw)
  To: Sam Protsenko, Łukasz Stelmach, Krzysztof Kozlowski,
	Rob Herring, Conor Dooley
  Cc: Anand Moon, Olivia Mackall, Herbert Xu, Alim Akhtar,
	linux-samsung-soc, linux-crypto, devicetree, linux-arm-kernel,
	linux-kernel

On 18/06/2024 22:45, Sam Protsenko wrote:
> Add Exynos850 compatible and its driver data. It's only possible to
> access TRNG block via SMC calls in Exynos850, so specify that fact using
> EXYNOS_SMC flag in the driver data.
> 
> Signed-off-by: Sam Protsenko <semen.protsenko@linaro.org>
> ---

Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>

Best regards,
Krzysztof


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

* Re: [PATCH v2 7/7] arm64: dts: exynos850: Enable TRNG
  2024-06-18 20:45 ` [PATCH v2 7/7] arm64: dts: exynos850: Enable TRNG Sam Protsenko
@ 2024-06-20  7:31   ` Krzysztof Kozlowski
  2024-06-20 21:51     ` Sam Protsenko
  2024-07-01 12:27   ` (subset) " Krzysztof Kozlowski
  1 sibling, 1 reply; 21+ messages in thread
From: Krzysztof Kozlowski @ 2024-06-20  7:31 UTC (permalink / raw)
  To: Sam Protsenko, Łukasz Stelmach, Krzysztof Kozlowski,
	Rob Herring, Conor Dooley
  Cc: Anand Moon, Olivia Mackall, Herbert Xu, Alim Akhtar,
	linux-samsung-soc, linux-crypto, devicetree, linux-arm-kernel,
	linux-kernel

On 18/06/2024 22:45, Sam Protsenko wrote:
> Add True Random Number Generator (TRNG) node to Exynos850 SoC dtsi.
> 
> Signed-off-by: Sam Protsenko <semen.protsenko@linaro.org>
> ---
> Changes in v2:
>   - (no changes)
> 

That's a patch for Samsung soc. I'll take it once binding is accepted.
If you send any new version of the patchset, please do not include DTS,
so the crypto maintainer could apply entire set easier.

Best regards,
Krzysztof


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

* Re: [PATCH v2 6/7] hwrng: exynos: Enable Exynos850 support
       [not found]   ` <CGME20240620084749eucas1p18f46cc9aa0983afb75e1677b399d12f8@eucas1p1.samsung.com>
@ 2024-06-20  8:47     ` Lukasz Stelmach
  0 siblings, 0 replies; 21+ messages in thread
From: Lukasz Stelmach @ 2024-06-20  8:47 UTC (permalink / raw)
  To: Sam Protsenko
  Cc: Krzysztof Kozlowski, Rob Herring, Conor Dooley, Anand Moon,
	Olivia Mackall, Herbert Xu, Alim Akhtar, linux-samsung-soc,
	linux-crypto, devicetree, linux-arm-kernel, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1208 bytes --]

It was <2024-06-18 wto 15:45>, when Sam Protsenko wrote:
> Add Exynos850 compatible and its driver data. It's only possible to
> access TRNG block via SMC calls in Exynos850, so specify that fact using
> EXYNOS_SMC flag in the driver data.
>
> Signed-off-by: Sam Protsenko <semen.protsenko@linaro.org>
> ---
> Changes in v2:
>   - Changed QUIRK_SMC to EXYNOS_SMC to reflect the name change in the
>     previous patch
>
>  drivers/char/hw_random/exynos-trng.c | 3 +++
>  1 file changed, 3 insertions(+)
>

Acked-by: Łukasz Stelmach <l.stelmach@samsung.com>

> diff --git a/drivers/char/hw_random/exynos-trng.c b/drivers/char/hw_random/exynos-trng.c
> index 497d6018c6ba..841598037cce 100644
> --- a/drivers/char/hw_random/exynos-trng.c
> +++ b/drivers/char/hw_random/exynos-trng.c
> @@ -313,6 +313,9 @@ static DEFINE_SIMPLE_DEV_PM_OPS(exynos_trng_pm_ops, exynos_trng_suspend,
>  static const struct of_device_id exynos_trng_dt_match[] = {
>  	{
>  		.compatible = "samsung,exynos5250-trng",
> +	}, {
> +		.compatible = "samsung,exynos850-trng",
> +		.data = (void *)EXYNOS_SMC,
>  	},
>  	{ },
>  };

-- 
Łukasz Stelmach
Samsung R&D Institute Poland
Samsung Electronics

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 487 bytes --]

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

* Re: [PATCH v2 2/7] hwrng: exynos: Improve coding style
       [not found]   ` <CGME20240620085239eucas1p1d49a6bae77e1fc857e4459f87c619a83@eucas1p1.samsung.com>
@ 2024-06-20  8:52     ` Lukasz Stelmach
  0 siblings, 0 replies; 21+ messages in thread
From: Lukasz Stelmach @ 2024-06-20  8:52 UTC (permalink / raw)
  To: Sam Protsenko
  Cc: Krzysztof Kozlowski, Rob Herring, Conor Dooley, Anand Moon,
	Olivia Mackall, Herbert Xu, Alim Akhtar, linux-samsung-soc,
	linux-crypto, devicetree, linux-arm-kernel, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 4554 bytes --]

It was <2024-06-18 wto 15:45>, when Sam Protsenko wrote:
> Fix obvious style issues. Some of those were found with checkpatch, and
> some just contradict the kernel coding style guide.
>
> No functional change.
>
> Signed-off-by: Sam Protsenko <semen.protsenko@linaro.org>
> Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
> ---
> Changes in v2:
>   - Added Krzysztof's R-b tag
>
>  drivers/char/hw_random/exynos-trng.c | 61 +++++++++++++---------------
>  1 file changed, 29 insertions(+), 32 deletions(-)
>

Acked-by: Łukasz Stelmach <l.stelmach@samsung.com>

> diff --git a/drivers/char/hw_random/exynos-trng.c b/drivers/char/hw_random/exynos-trng.c
> index 0ed5d22fe667..88a5088ed34d 100644
> --- a/drivers/char/hw_random/exynos-trng.c
> +++ b/drivers/char/hw_random/exynos-trng.c
> @@ -23,45 +23,41 @@
>  #include <linux/platform_device.h>
>  #include <linux/pm_runtime.h>
>  
> -#define EXYNOS_TRNG_CLKDIV         (0x0)
> -
> -#define EXYNOS_TRNG_CTRL           (0x20)
> -#define EXYNOS_TRNG_CTRL_RNGEN     BIT(31)
> -
> -#define EXYNOS_TRNG_POST_CTRL      (0x30)
> -#define EXYNOS_TRNG_ONLINE_CTRL    (0x40)
> -#define EXYNOS_TRNG_ONLINE_STAT    (0x44)
> -#define EXYNOS_TRNG_ONLINE_MAXCHI2 (0x48)
> -#define EXYNOS_TRNG_FIFO_CTRL      (0x50)
> -#define EXYNOS_TRNG_FIFO_0         (0x80)
> -#define EXYNOS_TRNG_FIFO_1         (0x84)
> -#define EXYNOS_TRNG_FIFO_2         (0x88)
> -#define EXYNOS_TRNG_FIFO_3         (0x8c)
> -#define EXYNOS_TRNG_FIFO_4         (0x90)
> -#define EXYNOS_TRNG_FIFO_5         (0x94)
> -#define EXYNOS_TRNG_FIFO_6         (0x98)
> -#define EXYNOS_TRNG_FIFO_7         (0x9c)
> -#define EXYNOS_TRNG_FIFO_LEN       (8)
> -#define EXYNOS_TRNG_CLOCK_RATE     (500000)
> -
> +#define EXYNOS_TRNG_CLKDIV		0x0
> +
> +#define EXYNOS_TRNG_CTRL		0x20
> +#define EXYNOS_TRNG_CTRL_RNGEN		BIT(31)
> +
> +#define EXYNOS_TRNG_POST_CTRL		0x30
> +#define EXYNOS_TRNG_ONLINE_CTRL		0x40
> +#define EXYNOS_TRNG_ONLINE_STAT		0x44
> +#define EXYNOS_TRNG_ONLINE_MAXCHI2	0x48
> +#define EXYNOS_TRNG_FIFO_CTRL		0x50
> +#define EXYNOS_TRNG_FIFO_0		0x80
> +#define EXYNOS_TRNG_FIFO_1		0x84
> +#define EXYNOS_TRNG_FIFO_2		0x88
> +#define EXYNOS_TRNG_FIFO_3		0x8c
> +#define EXYNOS_TRNG_FIFO_4		0x90
> +#define EXYNOS_TRNG_FIFO_5		0x94
> +#define EXYNOS_TRNG_FIFO_6		0x98
> +#define EXYNOS_TRNG_FIFO_7		0x9c
> +#define EXYNOS_TRNG_FIFO_LEN		8
> +#define EXYNOS_TRNG_CLOCK_RATE		500000
>  
>  struct exynos_trng_dev {
> -	struct device    *dev;
> -	void __iomem     *mem;
> -	struct clk       *clk;
> -	struct hwrng rng;
> +	struct device	*dev;
> +	void __iomem	*mem;
> +	struct clk	*clk;
> +	struct hwrng	rng;
>  };
>  
>  static int exynos_trng_do_read(struct hwrng *rng, void *data, size_t max,
>  			       bool wait)
>  {
> -	struct exynos_trng_dev *trng;
> +	struct exynos_trng_dev *trng = (struct exynos_trng_dev *)rng->priv;
>  	int val;
>  
>  	max = min_t(size_t, max, (EXYNOS_TRNG_FIFO_LEN * 4));
> -
> -	trng = (struct exynos_trng_dev *)rng->priv;
> -
>  	writel_relaxed(max * 8, trng->mem + EXYNOS_TRNG_FIFO_CTRL);
>  	val = readl_poll_timeout(trng->mem + EXYNOS_TRNG_FIFO_CTRL, val,
>  				 val == 0, 200, 1000000);
> @@ -122,7 +118,7 @@ static int exynos_trng_probe(struct platform_device *pdev)
>  
>  	trng->rng.init = exynos_trng_init;
>  	trng->rng.read = exynos_trng_do_read;
> -	trng->rng.priv = (unsigned long) trng;
> +	trng->rng.priv = (unsigned long)trng;
>  
>  	platform_set_drvdata(pdev, trng);
>  	trng->dev = &pdev->dev;
> @@ -175,7 +171,7 @@ static int exynos_trng_probe(struct platform_device *pdev)
>  
>  static void exynos_trng_remove(struct platform_device *pdev)
>  {
> -	struct exynos_trng_dev *trng =  platform_get_drvdata(pdev);
> +	struct exynos_trng_dev *trng = platform_get_drvdata(pdev);
>  
>  	clk_disable_unprepare(trng->clk);
>  
> @@ -204,7 +200,7 @@ static int exynos_trng_resume(struct device *dev)
>  }
>  
>  static DEFINE_SIMPLE_DEV_PM_OPS(exynos_trng_pm_ops, exynos_trng_suspend,
> -			 exynos_trng_resume);
> +				exynos_trng_resume);
>  
>  static const struct of_device_id exynos_trng_dt_match[] = {
>  	{
> @@ -225,6 +221,7 @@ static struct platform_driver exynos_trng_driver = {
>  };
>  
>  module_platform_driver(exynos_trng_driver);
> +
>  MODULE_AUTHOR("Łukasz Stelmach");
>  MODULE_DESCRIPTION("H/W TRNG driver for Exynos chips");
>  MODULE_LICENSE("GPL v2");

-- 
Łukasz Stelmach
Samsung R&D Institute Poland
Samsung Electronics

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 487 bytes --]

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

* Re: [PATCH v2 3/7] hwrng: exynos: Use devm_clk_get_enabled() to get the clock
  2024-06-18 20:45 ` [PATCH v2 3/7] hwrng: exynos: Use devm_clk_get_enabled() to get the clock Sam Protsenko
  2024-06-20  7:29   ` Krzysztof Kozlowski
@ 2024-06-20  9:27   ` Anand Moon
  1 sibling, 0 replies; 21+ messages in thread
From: Anand Moon @ 2024-06-20  9:27 UTC (permalink / raw)
  To: Sam Protsenko
  Cc: Łukasz Stelmach, Krzysztof Kozlowski, Rob Herring,
	Conor Dooley, Olivia Mackall, Herbert Xu, Alim Akhtar,
	linux-samsung-soc, linux-crypto, devicetree, linux-arm-kernel,
	linux-kernel

Hi Sam,

On Wed, 19 Jun 2024 at 02:15, Sam Protsenko <semen.protsenko@linaro.org> wrote:
>
> Use devm_clk_get_enabled() helper instead of calling devm_clk_get() and
> then clk_prepare_enable(). It simplifies the error handling and makes
> the code more compact. Also use dev_err_probe() to handle possible
> -EPROBE_DEFER errors if the clock is not available yet.
>
> Signed-off-by: Sam Protsenko <semen.protsenko@linaro.org>
> ---
> Changes in v2:
>   - No changes (it's a new patch added in v2)
>

Reviewed-by: Anand Moon <linux.amoon@gmail.com>

Thanks

-Anand

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

* Re: [PATCH v2 4/7] hwrng: exynos: Implement bus clock control
  2024-06-18 20:45 ` [PATCH v2 4/7] hwrng: exynos: Implement bus clock control Sam Protsenko
  2024-06-20  7:29   ` Krzysztof Kozlowski
@ 2024-06-20  9:27   ` Anand Moon
  1 sibling, 0 replies; 21+ messages in thread
From: Anand Moon @ 2024-06-20  9:27 UTC (permalink / raw)
  To: Sam Protsenko
  Cc: Łukasz Stelmach, Krzysztof Kozlowski, Rob Herring,
	Conor Dooley, Olivia Mackall, Herbert Xu, Alim Akhtar,
	linux-samsung-soc, linux-crypto, devicetree, linux-arm-kernel,
	linux-kernel

Hi Sam,

On Wed, 19 Jun 2024 at 02:15, Sam Protsenko <semen.protsenko@linaro.org> wrote:
>
> Some SoCs like Exynos850 might require the SSS bus clock (PCLK) to be
> enabled in order to access TRNG registers. Add and handle the optional
> PCLK clock accordingly to make it possible.
>
> Signed-off-by: Sam Protsenko <semen.protsenko@linaro.org>
> ---
> Changes in v2:
>   - Used devm_clk_get_optional_enabled() to avoid calling
>     clk_prepare_enable() for PCLK
>

Reviewed-by: Anand Moon <linux.amoon@gmail.com>

Thanks

-Anand

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

* Re: [PATCH v2 5/7] hwrng: exynos: Add SMC based TRNG operation
       [not found]   ` <CGME20240620134610eucas1p2fbcd7218bc220bf568ea117acf2f4781@eucas1p2.samsung.com>
@ 2024-06-20 13:46     ` Lukasz Stelmach
  2024-06-20 21:39       ` Sam Protsenko
  0 siblings, 1 reply; 21+ messages in thread
From: Lukasz Stelmach @ 2024-06-20 13:46 UTC (permalink / raw)
  To: Sam Protsenko
  Cc: Krzysztof Kozlowski, Rob Herring, Conor Dooley, Anand Moon,
	Olivia Mackall, Herbert Xu, Alim Akhtar, linux-samsung-soc,
	linux-crypto, devicetree, linux-arm-kernel, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 2116 bytes --]

It was <2024-06-18 wto 15:45>, when Sam Protsenko wrote:
> On some Exynos chips like Exynos850 the access to Security Sub System
> (SSS) registers is protected with TrustZone, and therefore only possible
> from EL3 monitor software. The Linux kernel is running in EL1, so the
> only way for the driver to obtain TRNG data is via SMC calls to EL3
> monitor. Implement such SMC operation and use it when EXYNOS_SMC flag is
> set in the corresponding chip driver data.
>
> Signed-off-by: Sam Protsenko <semen.protsenko@linaro.org>
> Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
> ---
> Changes in v2:
>   - Used the "reversed Christmas tree" style in the variable declaration
>     block in exynos_trng_do_read_smc()
>   - Renamed .quirks to .flags in the driver structure
>   - Added Krzysztof's R-b tag
>
>  drivers/char/hw_random/exynos-trng.c | 133 +++++++++++++++++++++++++--
>  1 file changed, 123 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/char/hw_random/exynos-trng.c b/drivers/char/hw_random/exynos-trng.c
> index 99a0b271ffb7..497d6018c6ba 100644
> --- a/drivers/char/hw_random/exynos-trng.c
> +++ b/drivers/char/hw_random/exynos-trng.c
> @@ -10,6 +10,7 @@
>   * Krzysztof Kozłowski <krzk@kernel.org>
>   */

[...]

> +static int exynos_trng_init_smc(struct hwrng *rng)
> +{
> +	struct arm_smccc_res res;
> +
> +	arm_smccc_smc(SMC_CMD_RANDOM, HWRNG_INIT, 0, 0, 0, 0, 0, 0, &res);
> +	if (res.a0 != HWRNG_RET_OK)
> +		return -EIO;
> +
> +	return 0;
> +}
> +

Does this driver requiers some vendor-specifig bootloading code?
I am testing the code on a WinLink E850-96 board booted with the
upstream u-boot and it fails during init (res0.a is -1).

[    1.883413] exynos-trng 12081400.rng: Could not register hwrng device
[    1.893394] exynos-trng 12081400.rng: probe with driver exynos-trng failed with error -5

If an additional code outside the kernel is required for this to run,
then maybe the error message should reflect that.

Kind regards,
-- 
Łukasz Stelmach
Samsung R&D Institute Poland
Samsung Electronics

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 487 bytes --]

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

* Re: [PATCH v2 5/7] hwrng: exynos: Add SMC based TRNG operation
  2024-06-20 13:46     ` Lukasz Stelmach
@ 2024-06-20 21:39       ` Sam Protsenko
  0 siblings, 0 replies; 21+ messages in thread
From: Sam Protsenko @ 2024-06-20 21:39 UTC (permalink / raw)
  To: Lukasz Stelmach
  Cc: Krzysztof Kozlowski, Rob Herring, Conor Dooley, Anand Moon,
	Olivia Mackall, Herbert Xu, Alim Akhtar, linux-samsung-soc,
	linux-crypto, devicetree, linux-arm-kernel, linux-kernel

On Thu, Jun 20, 2024 at 8:46 AM Lukasz Stelmach <l.stelmach@samsung.com> wrote:
>
> It was <2024-06-18 wto 15:45>, when Sam Protsenko wrote:
> > On some Exynos chips like Exynos850 the access to Security Sub System
> > (SSS) registers is protected with TrustZone, and therefore only possible
> > from EL3 monitor software. The Linux kernel is running in EL1, so the
> > only way for the driver to obtain TRNG data is via SMC calls to EL3
> > monitor. Implement such SMC operation and use it when EXYNOS_SMC flag is
> > set in the corresponding chip driver data.
> >
> > Signed-off-by: Sam Protsenko <semen.protsenko@linaro.org>
> > Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
> > ---
> > Changes in v2:
> >   - Used the "reversed Christmas tree" style in the variable declaration
> >     block in exynos_trng_do_read_smc()
> >   - Renamed .quirks to .flags in the driver structure
> >   - Added Krzysztof's R-b tag
> >
> >  drivers/char/hw_random/exynos-trng.c | 133 +++++++++++++++++++++++++--
> >  1 file changed, 123 insertions(+), 10 deletions(-)
> >
> > diff --git a/drivers/char/hw_random/exynos-trng.c b/drivers/char/hw_random/exynos-trng.c
> > index 99a0b271ffb7..497d6018c6ba 100644
> > --- a/drivers/char/hw_random/exynos-trng.c
> > +++ b/drivers/char/hw_random/exynos-trng.c
> > @@ -10,6 +10,7 @@
> >   * Krzysztof Kozłowski <krzk@kernel.org>
> >   */
>
> [...]
>
> > +static int exynos_trng_init_smc(struct hwrng *rng)
> > +{
> > +     struct arm_smccc_res res;
> > +
> > +     arm_smccc_smc(SMC_CMD_RANDOM, HWRNG_INIT, 0, 0, 0, 0, 0, 0, &res);
> > +     if (res.a0 != HWRNG_RET_OK)
> > +             return -EIO;
> > +
> > +     return 0;
> > +}
> > +
>
> Does this driver requiers some vendor-specifig bootloading code?
> I am testing the code on a WinLink E850-96 board booted with the
> upstream u-boot and it fails during init (res0.a is -1).
>

This series was only tested (and works fine) with LittleKernel based
bootloader [1]. It's officially recommended and the only feature
complete bootloader at the moment. And you are right, the reason why
TRNG probe fails when you boot the kernel from U-Boot is that the LDFW
(Loadable Firmware) loading is not implemented in U-Boot right now,
which makes HWRNG_INIT SMC command fail and return -1. In fact, I'm
adding LDFW loading in U-Boot right now and expect it to be ready in 1
week or so. For now, can you please check with LK [1] instead? I'm
happy to help if you have any related questions.

> [    1.883413] exynos-trng 12081400.rng: Could not register hwrng device
> [    1.893394] exynos-trng 12081400.rng: probe with driver exynos-trng failed with error -5
>
> If an additional code outside the kernel is required for this to run,
> then maybe the error message should reflect that.
>

Good idea! Will send v3 soon, with proper error message added.

Thanks!

[1] https://gitlab.com/Linaro/96boards/e850-96/lk

> Kind regards,
> --
> Łukasz Stelmach
> Samsung R&D Institute Poland
> Samsung Electronics

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

* Re: [PATCH v2 7/7] arm64: dts: exynos850: Enable TRNG
  2024-06-20  7:31   ` Krzysztof Kozlowski
@ 2024-06-20 21:51     ` Sam Protsenko
  0 siblings, 0 replies; 21+ messages in thread
From: Sam Protsenko @ 2024-06-20 21:51 UTC (permalink / raw)
  To: Krzysztof Kozlowski
  Cc: Łukasz Stelmach, Krzysztof Kozlowski, Rob Herring,
	Conor Dooley, Anand Moon, Olivia Mackall, Herbert Xu, Alim Akhtar,
	linux-samsung-soc, linux-crypto, devicetree, linux-arm-kernel,
	linux-kernel

On Thu, Jun 20, 2024 at 2:31 AM Krzysztof Kozlowski <krzk@kernel.org> wrote:
>
> On 18/06/2024 22:45, Sam Protsenko wrote:
> > Add True Random Number Generator (TRNG) node to Exynos850 SoC dtsi.
> >
> > Signed-off-by: Sam Protsenko <semen.protsenko@linaro.org>
> > ---
> > Changes in v2:
> >   - (no changes)
> >
>
> That's a patch for Samsung soc. I'll take it once binding is accepted.
> If you send any new version of the patchset, please do not include DTS,
> so the crypto maintainer could apply entire set easier.
>

Thanks, Krzysztof! I'm going to send v3 soon, so I'll remove this
patch from the series.

> Best regards,
> Krzysztof
>

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

* Re: (subset) [PATCH v2 7/7] arm64: dts: exynos850: Enable TRNG
  2024-06-18 20:45 ` [PATCH v2 7/7] arm64: dts: exynos850: Enable TRNG Sam Protsenko
  2024-06-20  7:31   ` Krzysztof Kozlowski
@ 2024-07-01 12:27   ` Krzysztof Kozlowski
  1 sibling, 0 replies; 21+ messages in thread
From: Krzysztof Kozlowski @ 2024-07-01 12:27 UTC (permalink / raw)
  To: Łukasz Stelmach, Krzysztof Kozlowski, Rob Herring,
	Conor Dooley, Sam Protsenko
  Cc: Anand Moon, Olivia Mackall, Herbert Xu, Alim Akhtar,
	linux-samsung-soc, linux-crypto, devicetree, linux-arm-kernel,
	linux-kernel


On Tue, 18 Jun 2024 15:45:23 -0500, Sam Protsenko wrote:
> Add True Random Number Generator (TRNG) node to Exynos850 SoC dtsi.
> 
> 

Applied, thanks!

[7/7] arm64: dts: exynos850: Enable TRNG
      https://git.kernel.org/krzk/linux/c/64c7ea42fcc2b972fc8d108642f4b8fabf0999c3

Best regards,
-- 
Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>


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

end of thread, other threads:[~2024-07-01 12:27 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-18 20:45 [PATCH v2 0/7] hwrng: exynos: Add support for Exynos850 Sam Protsenko
2024-06-18 20:45 ` [PATCH v2 1/7] dt-bindings: rng: Add Exynos850 support to exynos-trng Sam Protsenko
2024-06-20  7:27   ` Krzysztof Kozlowski
2024-06-18 20:45 ` [PATCH v2 2/7] hwrng: exynos: Improve coding style Sam Protsenko
     [not found]   ` <CGME20240620085239eucas1p1d49a6bae77e1fc857e4459f87c619a83@eucas1p1.samsung.com>
2024-06-20  8:52     ` Lukasz Stelmach
2024-06-18 20:45 ` [PATCH v2 3/7] hwrng: exynos: Use devm_clk_get_enabled() to get the clock Sam Protsenko
2024-06-20  7:29   ` Krzysztof Kozlowski
2024-06-20  9:27   ` Anand Moon
2024-06-18 20:45 ` [PATCH v2 4/7] hwrng: exynos: Implement bus clock control Sam Protsenko
2024-06-20  7:29   ` Krzysztof Kozlowski
2024-06-20  9:27   ` Anand Moon
2024-06-18 20:45 ` [PATCH v2 5/7] hwrng: exynos: Add SMC based TRNG operation Sam Protsenko
     [not found]   ` <CGME20240620134610eucas1p2fbcd7218bc220bf568ea117acf2f4781@eucas1p2.samsung.com>
2024-06-20 13:46     ` Lukasz Stelmach
2024-06-20 21:39       ` Sam Protsenko
2024-06-18 20:45 ` [PATCH v2 6/7] hwrng: exynos: Enable Exynos850 support Sam Protsenko
2024-06-20  7:29   ` Krzysztof Kozlowski
     [not found]   ` <CGME20240620084749eucas1p18f46cc9aa0983afb75e1677b399d12f8@eucas1p1.samsung.com>
2024-06-20  8:47     ` Lukasz Stelmach
2024-06-18 20:45 ` [PATCH v2 7/7] arm64: dts: exynos850: Enable TRNG Sam Protsenko
2024-06-20  7:31   ` Krzysztof Kozlowski
2024-06-20 21:51     ` Sam Protsenko
2024-07-01 12:27   ` (subset) " Krzysztof Kozlowski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).