mirror of https://lore.kernel.org/linux-amlogic/
 help / color / mirror / Atom feed
From: Martin Kaiser <lists@kaiser.cx>
To: Alexey Romanov <avromanov@sberdevices.ru>
Cc: narmstrong@baylibre.com, neil.armstrong@linaro.org,
	olivia@selenic.com, herbert@gondor.apana.org.au,
	robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org,
	conor+dt@kernel.org, khilman@baylibre.com, jbrunet@baylibre.com,
	martin.blumenstingl@googlemail.com, f.fainelli@gmail.com,
	hkallweit1@gmail.com, linux-arm-kernel@lists.infradead.org,
	linux-amlogic@lists.infradead.org, linux-kernel@vger.kernel.org,
	devicetree@vger.kernel.org, linux-crypto@vger.kernel.org,
	kernel@sberdevices.ru
Subject: Re: [PATCH v1 1/3] drivers: rng: add check status bit feature
Date: Tue, 25 Jul 2023 21:59:01 +0200	[thread overview]
Message-ID: <20230725195901.n2klvgz7outqaatk@viti.kaiser.cx> (raw)
In-Reply-To: <20230725141252.98848-2-avromanov@sberdevices.ru>

Alexey Romanov (avromanov@sberdevices.ru) wrote:

> For some Amlogic SOC's, mechanism to obtain random number
> has been changed. For example, S4 now uses status bit waiting algo.

> Signed-off-by: Alexey Romanov <avromanov@sberdevices.ru>
> ---
>  drivers/char/hw_random/meson-rng.c | 77 ++++++++++++++++++++++++++++--
>  1 file changed, 74 insertions(+), 3 deletions(-)

> diff --git a/drivers/char/hw_random/meson-rng.c b/drivers/char/hw_random/meson-rng.c
> index a4eb8e35f13d..c6d7349630a1 100644
> --- a/drivers/char/hw_random/meson-rng.c
> +++ b/drivers/char/hw_random/meson-rng.c
> @@ -14,19 +14,65 @@
>  #include <linux/of.h>
>  #include <linux/clk.h>

> -#define RNG_DATA 0x00
> +struct meson_rng_priv {
> +	bool check_status_bit;
> +	unsigned int data_offset;
> +	unsigned int cfg_offset;
> +};

>  struct meson_rng_data {
>  	void __iomem *base;
>  	struct hwrng rng;
> +	struct device *dev;
> +	const struct meson_rng_priv *priv;
>  };

> +#define RUN_BIT			0
> +#define SEED_READY_STS_BIT	31
> +#define RETRY_CNT		100
> +
> +static int meson_rng_wait_status(void __iomem *cfg_addr, int bit)
> +{
> +	u32 status;
> +	u32 cnt = 0;
> +
> +	do {
> +		status = readl_relaxed(cfg_addr) & BIT(bit);
> +		cpu_relax();
> +	} while (status && (cnt++ < RETRY_CNT));
> +

Could you use readl_relaxed_poll_timeout here instead of open coding the
loop?

> +	if (status)
> +		return -EBUSY;
> +
> +	return 0;
> +}
> +
>  static int meson_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
>  {
>  	struct meson_rng_data *data =
>  			container_of(rng, struct meson_rng_data, rng);
> +	const struct meson_rng_priv *priv = data->priv;
> +
> +	if (priv->check_status_bit) {
> +		void __iomem *cfg_addr = data->base + priv->cfg_offset;
> +		int err;
> +
> +		writel_relaxed(readl_relaxed(cfg_addr) | BIT(SEED_READY_STS_BIT), cfg_addr);

> -	*(u32 *)buf = readl_relaxed(data->base + RNG_DATA);
> +		err = meson_rng_wait_status(cfg_addr, SEED_READY_STS_BIT);
> +		if (err) {
> +			dev_err(data->dev, "Seed isn't ready, try again\n");
> +			return err;
> +		}
> +
> +		err = meson_rng_wait_status(cfg_addr, RUN_BIT);
> +		if (err) {
> +			dev_err(data->dev, "Can't get random number, try again\n");
> +			return err;
> +		}
> +	}
> +
> +	*(u32 *)buf = readl_relaxed(data->base + priv->data_offset);

>  	return sizeof(u32);
>  }
> @@ -41,6 +87,10 @@ static int meson_rng_probe(struct platform_device *pdev)
>  	if (!data)
>  		return -ENOMEM;

> +	data->priv = device_get_match_data(&pdev->dev);
> +	if (!data->priv)
> +		return -ENODEV;
> +
>  	data->base = devm_platform_ioremap_resource(pdev, 0);
>  	if (IS_ERR(data->base))
>  		return PTR_ERR(data->base);
> @@ -53,11 +103,32 @@ static int meson_rng_probe(struct platform_device *pdev)
>  	data->rng.name = pdev->name;
>  	data->rng.read = meson_rng_read;

> +	data->dev = &pdev->dev;
> +
>  	return devm_hwrng_register(dev, &data->rng);
>  }

> +static const struct meson_rng_priv meson_rng_priv = {
> +	.check_status_bit = false,
> +	.data_offset = 0x0,
> +	.cfg_offset = 0x0,
> +};
> +
> +static const struct meson_rng_priv meson_rng_priv_s4 = {
> +	.check_status_bit = true,
> +	.data_offset = 0x8,
> +	.cfg_offset = 0x0,
> +};
> +
>  static const struct of_device_id meson_rng_of_match[] = {
> -	{ .compatible = "amlogic,meson-rng", },
> +	{
> +		.compatible = "amlogic,meson-rng",
> +		.data = (void *)&meson_rng_priv,
> +	},
> +	{
> +		.compatible = "amlogic,meson-rng-s4",
> +		.data = (void *)&meson_rng_priv_s4,
> +	},
>  	{},
>  };
>  MODULE_DEVICE_TABLE(of, meson_rng_of_match);
> -- 
> 2.38.1


_______________________________________________
linux-amlogic mailing list
linux-amlogic@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-amlogic

  reply	other threads:[~2023-07-25 20:00 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-25 14:12 [PATCH v1 0/3] Meson S4 HW RNG Support Alexey Romanov
2023-07-25 14:12 ` [PATCH v1 1/3] drivers: rng: add check status bit feature Alexey Romanov
2023-07-25 19:59   ` Martin Kaiser [this message]
2023-07-26  7:52     ` Alexey Romanov
2023-07-26 11:54       ` Martin Kaiser
2023-07-27 19:14   ` Martin Blumenstingl
2023-07-25 14:12 ` [PATCH v1 2/3] dt-bindings: rng: meson: add meson-rng-s4 compatible Alexey Romanov
2023-07-25 18:13   ` Conor Dooley
2023-07-25 14:12 ` [PATCH v1 3/3] arch/arm64: dts: meson-s4: add hwrng node Alexey Romanov

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=20230725195901.n2klvgz7outqaatk@viti.kaiser.cx \
    --to=lists@kaiser.cx \
    --cc=avromanov@sberdevices.ru \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=f.fainelli@gmail.com \
    --cc=herbert@gondor.apana.org.au \
    --cc=hkallweit1@gmail.com \
    --cc=jbrunet@baylibre.com \
    --cc=kernel@sberdevices.ru \
    --cc=khilman@baylibre.com \
    --cc=krzysztof.kozlowski+dt@linaro.org \
    --cc=linux-amlogic@lists.infradead.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.blumenstingl@googlemail.com \
    --cc=narmstrong@baylibre.com \
    --cc=neil.armstrong@linaro.org \
    --cc=olivia@selenic.com \
    --cc=robh+dt@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®