From: Om Prakash Singh <quic_omprsing@quicinc.com>
To: <quic_omprsing@quicinc.com>
Cc: <neil.armstrong@linaro.org>, <konrad.dybcio@linaro.org>,
<agross@kernel.org>, <andersson@kernel.org>,
<conor+dt@kernel.org>, <davem@davemloft.net>,
<devicetree@vger.kernel.org>, <herbert@gondor.apana.org.au>,
<krzysztof.kozlowski+dt@linaro.org>,
<linux-arm-msm@vger.kernel.org>, <linux-crypto@vger.kernel.org>,
<linux-kernel@vger.kernel.org>, <marijn.suijten@somainline.org>,
<robh+dt@kernel.org>, <vkoul@kernel.org>
Subject: [PATCH] crypto: qcom-rng: Add hwrng support
Date: Fri, 1 Sep 2023 18:45:02 +0530 [thread overview]
Message-ID: <20230901131502.1549809-1-quic_omprsing@quicinc.com> (raw)
This is follow patch on top of [1] to add hwrng support for newer
platform with trng capability.
[1] https://lore.kernel.org/lkml/20230824-topic-sm8550-rng-v2-4-dfcafbb16a3e@linaro.org/
Signed-off-by: Om Prakash Singh <quic_omprsing@quicinc.com>
---
drivers/crypto/qcom-rng.c | 72 ++++++++++++++++++++++++++++++++++-----
1 file changed, 63 insertions(+), 9 deletions(-)
diff --git a/drivers/crypto/qcom-rng.c b/drivers/crypto/qcom-rng.c
index fb54b8cfc35f..f78ffdcc66ec 100644
--- a/drivers/crypto/qcom-rng.c
+++ b/drivers/crypto/qcom-rng.c
@@ -13,6 +13,7 @@
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
+#include <linux/hw_random.h>
/* Device specific register offsets */
#define PRNG_DATA_OUT 0x0000
@@ -32,13 +33,18 @@ struct qcom_rng {
struct mutex lock;
void __iomem *base;
struct clk *clk;
- unsigned int skip_init;
+ struct qcom_rng_of_data *of_data;
};
struct qcom_rng_ctx {
struct qcom_rng *rng;
};
+struct qcom_rng_of_data {
+ bool skip_init;
+ bool hwrng_support;
+};
+
static struct qcom_rng *qcom_rng_dev;
static int qcom_rng_read(struct qcom_rng *rng, u8 *data, unsigned int max)
@@ -70,7 +76,7 @@ static int qcom_rng_read(struct qcom_rng *rng, u8 *data, unsigned int max)
}
} while (currsize < max);
- return 0;
+ return currsize;
}
static int qcom_rng_generate(struct crypto_rng *tfm,
@@ -79,7 +85,8 @@ static int qcom_rng_generate(struct crypto_rng *tfm,
{
struct qcom_rng_ctx *ctx = crypto_rng_ctx(tfm);
struct qcom_rng *rng = ctx->rng;
- int ret;
+ int ret = -EFAULT;
+ int read_size = 0;
ret = clk_prepare_enable(rng->clk);
if (ret)
@@ -87,11 +94,14 @@ static int qcom_rng_generate(struct crypto_rng *tfm,
mutex_lock(&rng->lock);
- ret = qcom_rng_read(rng, dstn, dlen);
+ read_size = qcom_rng_read(rng, dstn, dlen);
mutex_unlock(&rng->lock);
clk_disable_unprepare(rng->clk);
+ if (read_size == dlen)
+ ret = 0;
+
return ret;
}
@@ -101,6 +111,16 @@ static int qcom_rng_seed(struct crypto_rng *tfm, const u8 *seed,
return 0;
}
+static int qcom_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
+{
+ int ret;
+ struct qcom_rng *qrng;
+
+ qrng = (struct qcom_rng *)rng->priv;
+ ret = qcom_rng_read(qrng, data, max);
+ return ret;
+}
+
static int qcom_rng_enable(struct qcom_rng *rng)
{
u32 val;
@@ -136,7 +156,7 @@ static int qcom_rng_init(struct crypto_tfm *tfm)
ctx->rng = qcom_rng_dev;
- if (!ctx->rng->skip_init)
+ if (!ctx->rng->of_data->skip_init)
return qcom_rng_enable(ctx->rng);
return 0;
@@ -157,9 +177,16 @@ static struct rng_alg qcom_rng_alg = {
}
};
+static struct hwrng qcom_hwrng = {
+ .name = "qcom-hwrng",
+ .read = qcom_hwrng_read,
+ .quality = 1024,
+};
+
static int qcom_rng_probe(struct platform_device *pdev)
{
struct qcom_rng *rng;
+ const struct qcom_rng_of_data *data;
int ret;
rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL);
@@ -177,7 +204,8 @@ static int qcom_rng_probe(struct platform_device *pdev)
if (IS_ERR(rng->clk))
return PTR_ERR(rng->clk);
- rng->skip_init = (unsigned long)device_get_match_data(&pdev->dev);
+ data = of_device_get_match_data(&pdev->dev);
+ rng->of_data = (struct qcom_rng_of_data *)data;
qcom_rng_dev = rng;
ret = crypto_register_rng(&qcom_rng_alg);
@@ -185,6 +213,14 @@ static int qcom_rng_probe(struct platform_device *pdev)
dev_err(&pdev->dev, "Register crypto rng failed: %d\n", ret);
qcom_rng_dev = NULL;
}
+ if (rng->of_data->hwrng_support) {
+ qcom_hwrng.priv = (unsigned long)qcom_rng_dev;
+ ret = hwrng_register(&qcom_hwrng);
+ if (ret) {
+ dev_err(&pdev->dev, "Register hwrng failed: %d\n", ret);
+ qcom_rng_dev = NULL;
+ }
+ }
return ret;
}
@@ -193,11 +229,29 @@ static int qcom_rng_remove(struct platform_device *pdev)
{
crypto_unregister_rng(&qcom_rng_alg);
+ if (qcom_rng_dev->of_data->hwrng_support)
+ hwrng_unregister(&qcom_hwrng);
+
qcom_rng_dev = NULL;
return 0;
}
+struct qcom_rng_of_data qcom_prng_of_data = {
+ .skip_init = false,
+ .hwrng_support = false,
+};
+
+struct qcom_rng_of_data qcom_prng_ee_of_data = {
+ .skip_init = true,
+ .hwrng_support = false,
+};
+
+struct qcom_rng_of_data qcom_trng_of_data = {
+ .skip_init = true,
+ .hwrng_support = true,
+};
+
static const struct acpi_device_id __maybe_unused qcom_rng_acpi_match[] = {
{ .id = "QCOM8160", .driver_data = 1 },
{}
@@ -205,9 +259,9 @@ static const struct acpi_device_id __maybe_unused qcom_rng_acpi_match[] = {
MODULE_DEVICE_TABLE(acpi, qcom_rng_acpi_match);
static const struct of_device_id __maybe_unused qcom_rng_of_match[] = {
- { .compatible = "qcom,prng", .data = (void *)0},
- { .compatible = "qcom,prng-ee", .data = (void *)1},
- { .compatible = "qcom,trng", .data = (void *)1},
+ { .compatible = "qcom,prng", .data = &qcom_prng_of_data },
+ { .compatible = "qcom,prng-ee", .data = &qcom_prng_ee_of_data },
+ { .compatible = "qcom,trng", .data = &qcom_trng_of_data },
{}
};
MODULE_DEVICE_TABLE(of, qcom_rng_of_match);
--
2.25.1
next reply other threads:[~2023-09-01 13:15 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-01 13:15 Om Prakash Singh [this message]
2023-09-01 14:46 ` Bjorn Andersson
2023-09-05 2:50 ` Om Prakash Singh
2023-09-05 8:33 ` Konrad Dybcio
2023-09-03 17:33 ` Krzysztof Kozlowski
2023-09-05 6:24 ` [PATCH V2] crypto: qcom-rng - Add hw_random interface support Om Prakash Singh
2023-09-05 8:34 ` Konrad Dybcio
2023-09-20 3:04 ` Om Prakash Singh
2023-09-22 15:16 ` Bjorn Andersson
2023-09-25 11:10 ` Om Prakash Singh
2023-09-15 10:19 ` [PATCH] crypto: qcom-rng: Add hwrng support Herbert Xu
2023-09-20 2:56 ` [PATCH V2] crypto: qcom-rng - Add hw_random interface support Om Prakash Singh
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230901131502.1549809-1-quic_omprsing@quicinc.com \
--to=quic_omprsing@quicinc.com \
--cc=agross@kernel.org \
--cc=andersson@kernel.org \
--cc=conor+dt@kernel.org \
--cc=davem@davemloft.net \
--cc=devicetree@vger.kernel.org \
--cc=herbert@gondor.apana.org.au \
--cc=konrad.dybcio@linaro.org \
--cc=krzysztof.kozlowski+dt@linaro.org \
--cc=linux-arm-msm@vger.kernel.org \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=marijn.suijten@somainline.org \
--cc=neil.armstrong@linaro.org \
--cc=robh+dt@kernel.org \
--cc=vkoul@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®