Skip to contentSkip to conversation

hwrng: pic32 - some simple cleanups

PATCH In review Martin Kaiser 2 participants 6 messages Jul 4, 2023 – Jul 14, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover hwrng: pic32 - some simple cleanups

Here's a series with simple clenaups to the pic32 driver. The patches were compile tested only.

Martin Kaiser (4):
hwrng: pic32 - enable compile-testing
hwrng: pic32 - use devm_clk_get_enabled
hwrng: pic32 - remove unused defines
hwrng: pic32 - enable TRNG only while it's used

 drivers/char/hw_random/Kconfig     |  2 +-
 drivers/char/hw_random/pic32-rng.c | 71 +++++++++++-------------------
 2 files changed, 27 insertions(+), 46 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/4 hwrng: pic32 - enable compile-testing

Enable compile testing for the pic32 driver.

Remove the dependency on HW_RANDOM. The pic32 config section is under "if HW_RANDOM".

---
 drivers/char/hw_random/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/hw_random/Kconfig +1 -1
index e0b3786ca51b..1aeba12391a1 100644
@@ -385,7 +385,7 @@ config HW_RANDOM_STM32
 
 config HW_RANDOM_PIC32
 	tristate "Microchip PIC32 Random Number Generator support"
-	depends on HW_RANDOM && MACH_PIC32
+	depends on MACH_PIC32 || COMPILE_TEST
 	default y
 	help
 	  This driver provides kernel-side support for the Random Number
Signature · 1 line
2.30.2
#2 lore raw

PATCH 2/4 hwrng: pic32 - use devm_clk_get_enabled

In reply to Martin Kaiser

Use devm_clk_get_enabled in the pic32 driver. Ensure that the clock is enabled as long as the driver is registered with the hwrng core.

Fixes 7ea39973d1e5 ("hwrng: pic32 - Use device-managed registration API")
---
 drivers/char/hw_random/pic32-rng.c | 19 +++++--------------
 1 file changed, 5 insertions(+), 14 deletions(-)
drivers/char/hw_random/pic32-rng.c +5 -14
index 99c8bd0859a1..e04a054e8930 100644
@@ -36,7 +36,6 @@
 struct pic32_rng {
 	void __iomem	*base;
 	struct hwrng	rng;
-	struct clk	*clk;
 };
 
 /*
@@ -70,6 +69,7 @@ static int pic32_rng_read(struct hwrng *rng, void *buf, size_t max,
 static int pic32_rng_probe(struct platform_device *pdev)
 {
 	struct pic32_rng *priv;
+	struct clk *clk;
 	u32 v;
 	int ret;
 
@@ -81,13 +81,9 @@ static int pic32_rng_probe(struct platform_device *pdev)
 	if (IS_ERR(priv->base))
 		return PTR_ERR(priv->base);
 
-	priv->clk = devm_clk_get(&pdev->dev, NULL);
-	if (IS_ERR(priv->clk))
-		return PTR_ERR(priv->clk);
-
-	ret = clk_prepare_enable(priv->clk);
-	if (ret)
-		return ret;
+	clk = devm_clk_get_enabled(&pdev->dev, NULL);
+	if (IS_ERR(clk))
+		return PTR_ERR(clk);
 
 	/* enable TRNG in enhanced mode */
 	v = TRNGEN | TRNGMOD;
@@ -98,15 +94,11 @@ static int pic32_rng_probe(struct platform_device *pdev)
 
 	ret = devm_hwrng_register(&pdev->dev, &priv->rng);
 	if (ret)
-		goto err_register;
+		return ret;
 
 	platform_set_drvdata(pdev, priv);
 
 	return 0;
-
-err_register:
-	clk_disable_unprepare(priv->clk);
-	return ret;
 }
 
 static int pic32_rng_remove(struct platform_device *pdev)
@@ -114,7 +106,6 @@ static int pic32_rng_remove(struct platform_device *pdev)
 	struct pic32_rng *rng = platform_get_drvdata(pdev);
 
 	writel(0, rng->base + RNGCON);
-	clk_disable_unprepare(rng->clk);
 	return 0;
 }
 
Signature · 1 line
2.30.2
#3 lore raw

PATCH 3/4 hwrng: pic32 - remove unused defines

In reply to Martin Kaiser

Remove some unused defines and fix the indentation.

---
 drivers/char/hw_random/pic32-rng.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
drivers/char/hw_random/pic32-rng.c +3 -10
index e04a054e8930..c1b3f5915f03 100644
@@ -19,19 +19,12 @@
 #include <linux/slab.h>
 
 #define RNGCON		0x04
-#define  TRNGEN		BIT(8)
-#define  PRNGEN		BIT(9)
-#define  PRNGCONT	BIT(10)
-#define  TRNGMOD	BIT(11)
-#define  SEEDLOAD	BIT(12)
-#define RNGPOLY1	0x08
-#define RNGPOLY2	0x0C
-#define RNGNUMGEN1	0x10
-#define RNGNUMGEN2	0x14
+#define TRNGEN		BIT(8)
+#define TRNGMOD		BIT(11)
 #define RNGSEED1	0x18
 #define RNGSEED2	0x1C
 #define RNGRCNT		0x20
-#define  RCNT_MASK	0x7F
+#define RCNT_MASK	0x7F
 
 struct pic32_rng {
 	void __iomem	*base;
Signature · 1 line
2.30.2
#4 lore raw

PATCH 4/4 hwrng: pic32 - enable TRNG only while it's used

In reply to Martin Kaiser

The probe function enables the TRNG hardware before registering the driver. If registration fails, probe returns an error, but the TRNG remains enabled.

Define init and cleanup functions, enable and disable the hardware there.

---
 drivers/char/hw_random/pic32-rng.c | 41 ++++++++++++++----------------
 1 file changed, 19 insertions(+), 22 deletions(-)
drivers/char/hw_random/pic32-rng.c +19 -22
index c1b3f5915f03..1902f4389a3f 100644
@@ -38,6 +38,15 @@ struct pic32_rng {
  */
 #define RNG_TIMEOUT 500
 
+static int pic32_rng_init(struct hwrng *rng)
+{
+	struct pic32_rng *priv = container_of(rng, struct pic32_rng, rng);
+
+	/* enable TRNG in enhanced mode */
+	writel(TRNGEN | TRNGMOD, priv->base + RNGCON);
+	return 0;
+}
+
 static int pic32_rng_read(struct hwrng *rng, void *buf, size_t max,
 			  bool wait)
 {
@@ -59,12 +68,17 @@ static int pic32_rng_read(struct hwrng *rng, void *buf, size_t max,
 	return -EIO;
 }
 
+static void pic32_rng_cleanup(struct hwrng *rng)
+{
+	struct pic32_rng *priv = container_of(rng, struct pic32_rng, rng);
+
+	writel(0, priv->base + RNGCON);
+}
+
 static int pic32_rng_probe(struct platform_device *pdev)
 {
 	struct pic32_rng *priv;
 	struct clk *clk;
-	u32 v;
-	int ret;
 
 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
 	if (!priv)
@@ -78,28 +92,12 @@ static int pic32_rng_probe(struct platform_device *pdev)
 	if (IS_ERR(clk))
 		return PTR_ERR(clk);
 
-	/* enable TRNG in enhanced mode */
-	v = TRNGEN | TRNGMOD;
-	writel(v, priv->base + RNGCON);
-
 	priv->rng.name = pdev->name;
+	priv->rng.init = pic32_rng_init;
 	priv->rng.read = pic32_rng_read;
+	priv->rng.cleanup = pic32_rng_cleanup;
 
-	ret = devm_hwrng_register(&pdev->dev, &priv->rng);
-	if (ret)
-		return ret;
-
-	platform_set_drvdata(pdev, priv);
-
-	return 0;
-}
-
-static int pic32_rng_remove(struct platform_device *pdev)
-{
-	struct pic32_rng *rng = platform_get_drvdata(pdev);
-
-	writel(0, rng->base + RNGCON);
-	return 0;
+	return devm_hwrng_register(&pdev->dev, &priv->rng);
 }
 
 static const struct of_device_id pic32_rng_of_match[] __maybe_unused = {
@@ -110,7 +108,6 @@ MODULE_DEVICE_TABLE(of, pic32_rng_of_match);
 
 static struct platform_driver pic32_rng_driver = {
 	.probe		= pic32_rng_probe,
-	.remove		= pic32_rng_remove,
 	.driver		= {
 		.name	= "pic32-rng",
 		.of_match_table = of_match_ptr(pic32_rng_of_match),
Signature · 1 line
2.30.2
maintainer
#5 lore raw
In reply to Martin Kaiser
Quoted 9 lines · Martin Kaiser

Here's a series with simple clenaups to the pic32 driver. The patches were compile tested only.

Martin Kaiser (4):
hwrng: pic32 - enable compile-testing
hwrng: pic32 - use devm_clk_get_enabled
hwrng: pic32 - remove unused defines
hwrng: pic32 - enable TRNG only while it's used

Quoted 6 lines
 drivers/char/hw_random/Kconfig     |  2 +-
 drivers/char/hw_random/pic32-rng.c | 71 +++++++++++-------------------
 2 files changed, 27 insertions(+), 46 deletions(-)
Signature · 1 line
2.30.2

All applied. Thanks.

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt