Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b788479f authored by Florian Fainelli's avatar Florian Fainelli Committed by Herbert Xu
Browse files

hwrng: bcm2835 - Define a driver private context



Instead of making hwrng::priv host the base register address, define a
driver private context, make it per platform device instance and pass it
down the different functions.

Signed-off-by: default avatarFlorian Fainelli <f.fainelli@gmail.com>
Reviewed-by: default avatarEric Anholt <eric@anholt.net>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 21bb0ef4
Loading
Loading
Loading
Loading
+34 −21
Original line number Original line Diff line number Diff line
@@ -29,6 +29,11 @@


#define RNG_INT_OFF	0x1
#define RNG_INT_OFF	0x1


struct bcm2835_rng_priv {
	struct hwrng rng;
	void __iomem *base;
};

static void __init nsp_rng_init(void __iomem *base)
static void __init nsp_rng_init(void __iomem *base)
{
{
	u32 val;
	u32 val;
@@ -39,34 +44,34 @@ static void __init nsp_rng_init(void __iomem *base)
	writel(val, base + RNG_INT_MASK);
	writel(val, base + RNG_INT_MASK);
}
}


static inline struct bcm2835_rng_priv *to_rng_priv(struct hwrng *rng)
{
	return container_of(rng, struct bcm2835_rng_priv, rng);
}

static int bcm2835_rng_read(struct hwrng *rng, void *buf, size_t max,
static int bcm2835_rng_read(struct hwrng *rng, void *buf, size_t max,
			       bool wait)
			       bool wait)
{
{
	void __iomem *rng_base = (void __iomem *)rng->priv;
	struct bcm2835_rng_priv *priv = to_rng_priv(rng);
	u32 max_words = max / sizeof(u32);
	u32 max_words = max / sizeof(u32);
	u32 num_words, count;
	u32 num_words, count;


	while ((__raw_readl(rng_base + RNG_STATUS) >> 24) == 0) {
	while ((__raw_readl(priv->base + RNG_STATUS) >> 24) == 0) {
		if (!wait)
		if (!wait)
			return 0;
			return 0;
		cpu_relax();
		cpu_relax();
	}
	}


	num_words = readl(rng_base + RNG_STATUS) >> 24;
	num_words = readl(priv->base + RNG_STATUS) >> 24;
	if (num_words > max_words)
	if (num_words > max_words)
		num_words = max_words;
		num_words = max_words;


	for (count = 0; count < num_words; count++)
	for (count = 0; count < num_words; count++)
		((u32 *)buf)[count] = readl(rng_base + RNG_DATA);
		((u32 *)buf)[count] = readl(priv->base + RNG_DATA);


	return num_words * sizeof(u32);
	return num_words * sizeof(u32);
}
}


static struct hwrng bcm2835_rng_ops = {
	.name	= "bcm2835",
	.read	= bcm2835_rng_read,
};

static const struct of_device_id bcm2835_rng_of_match[] = {
static const struct of_device_id bcm2835_rng_of_match[] = {
	{ .compatible = "brcm,bcm2835-rng"},
	{ .compatible = "brcm,bcm2835-rng"},
	{ .compatible = "brcm,bcm-nsp-rng", .data = nsp_rng_init},
	{ .compatible = "brcm,bcm-nsp-rng", .data = nsp_rng_init},
@@ -80,19 +85,27 @@ static int bcm2835_rng_probe(struct platform_device *pdev)
	struct device_node *np = dev->of_node;
	struct device_node *np = dev->of_node;
	void (*rng_setup)(void __iomem *base);
	void (*rng_setup)(void __iomem *base);
	const struct of_device_id *rng_id;
	const struct of_device_id *rng_id;
	void __iomem *rng_base;
	struct bcm2835_rng_priv *priv;
	struct resource *r;
	struct resource *r;
	int err;
	int err;


	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	platform_set_drvdata(pdev, priv);

	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);


	/* map peripheral */
	/* map peripheral */
	rng_base = devm_ioremap_resource(dev, r);
	priv->base = devm_ioremap_resource(dev, r);
	if (IS_ERR(rng_base)) {
	if (IS_ERR(priv->base)) {
		dev_err(dev, "failed to remap rng regs");
		dev_err(dev, "failed to remap rng regs");
		return PTR_ERR(rng_base);
		return PTR_ERR(priv->base);
	}
	}
	bcm2835_rng_ops.priv = (unsigned long)rng_base;

	priv->rng.name = "bcm2835-rng";
	priv->rng.read = bcm2835_rng_read;


	rng_id = of_match_node(bcm2835_rng_of_match, np);
	rng_id = of_match_node(bcm2835_rng_of_match, np);
	if (!rng_id)
	if (!rng_id)
@@ -101,14 +114,14 @@ static int bcm2835_rng_probe(struct platform_device *pdev)
	/* Check for rng init function, execute it */
	/* Check for rng init function, execute it */
	rng_setup = rng_id->data;
	rng_setup = rng_id->data;
	if (rng_setup)
	if (rng_setup)
		rng_setup(rng_base);
		rng_setup(priv->base);


	/* set warm-up count & enable */
	/* set warm-up count & enable */
	__raw_writel(RNG_WARMUP_COUNT, rng_base + RNG_STATUS);
	__raw_writel(RNG_WARMUP_COUNT, priv->base + RNG_STATUS);
	__raw_writel(RNG_RBGEN, rng_base + RNG_CTRL);
	__raw_writel(RNG_RBGEN, priv->base + RNG_CTRL);


	/* register driver */
	/* register driver */
	err = hwrng_register(&bcm2835_rng_ops);
	err = hwrng_register(&priv->rng);
	if (err)
	if (err)
		dev_err(dev, "hwrng registration failed\n");
		dev_err(dev, "hwrng registration failed\n");
	else
	else
@@ -119,13 +132,13 @@ static int bcm2835_rng_probe(struct platform_device *pdev)


static int bcm2835_rng_remove(struct platform_device *pdev)
static int bcm2835_rng_remove(struct platform_device *pdev)
{
{
	void __iomem *rng_base = (void __iomem *)bcm2835_rng_ops.priv;
	struct bcm2835_rng_priv *priv = platform_get_drvdata(pdev);


	/* disable rng hardware */
	/* disable rng hardware */
	__raw_writel(0, rng_base + RNG_CTRL);
	__raw_writel(0, priv->base + RNG_CTRL);


	/* unregister driver */
	/* unregister driver */
	hwrng_unregister(&bcm2835_rng_ops);
	hwrng_unregister(&priv->rng);


	return 0;
	return 0;
}
}