diff options
author | Florian Fainelli <f.fainelli@gmail.com> | 2017-11-08 03:44:39 +0300 |
---|---|---|
committer | Herbert Xu <herbert@gondor.apana.org.au> | 2017-11-29 08:43:43 +0300 |
commit | b788479f68263fbb191daa5b2007de45f0548a72 (patch) | |
tree | ffb82b16d7d8e3eec353f1a00a7bf595bbff7a95 /drivers/char/hw_random | |
parent | 21bb0ef43c11184961792790919d6daf9823e57a (diff) | |
download | linux-b788479f68263fbb191daa5b2007de45f0548a72.tar.xz |
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: Florian Fainelli <f.fainelli@gmail.com>
Reviewed-by: Eric Anholt <eric@anholt.net>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'drivers/char/hw_random')
-rw-r--r-- | drivers/char/hw_random/bcm2835-rng.c | 55 |
1 files changed, 34 insertions, 21 deletions
diff --git a/drivers/char/hw_random/bcm2835-rng.c b/drivers/char/hw_random/bcm2835-rng.c index a818418a7e4c..0d72147ab45b 100644 --- a/drivers/char/hw_random/bcm2835-rng.c +++ b/drivers/char/hw_random/bcm2835-rng.c @@ -29,6 +29,11 @@ #define RNG_INT_OFF 0x1 +struct bcm2835_rng_priv { + struct hwrng rng; + void __iomem *base; +}; + static void __init nsp_rng_init(void __iomem *base) { u32 val; @@ -39,34 +44,34 @@ static void __init nsp_rng_init(void __iomem *base) 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, 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 num_words, count; - while ((__raw_readl(rng_base + RNG_STATUS) >> 24) == 0) { + while ((__raw_readl(priv->base + RNG_STATUS) >> 24) == 0) { if (!wait) return 0; cpu_relax(); } - num_words = readl(rng_base + RNG_STATUS) >> 24; + num_words = readl(priv->base + RNG_STATUS) >> 24; if (num_words > max_words) num_words = max_words; 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); } -static struct hwrng bcm2835_rng_ops = { - .name = "bcm2835", - .read = bcm2835_rng_read, -}; - static const struct of_device_id bcm2835_rng_of_match[] = { { .compatible = "brcm,bcm2835-rng"}, { .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; void (*rng_setup)(void __iomem *base); const struct of_device_id *rng_id; - void __iomem *rng_base; + struct bcm2835_rng_priv *priv; struct resource *r; 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); /* map peripheral */ - rng_base = devm_ioremap_resource(dev, r); - if (IS_ERR(rng_base)) { + priv->base = devm_ioremap_resource(dev, r); + if (IS_ERR(priv->base)) { 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); if (!rng_id) @@ -101,14 +114,14 @@ static int bcm2835_rng_probe(struct platform_device *pdev) /* Check for rng init function, execute it */ rng_setup = rng_id->data; if (rng_setup) - rng_setup(rng_base); + rng_setup(priv->base); /* set warm-up count & enable */ - __raw_writel(RNG_WARMUP_COUNT, rng_base + RNG_STATUS); - __raw_writel(RNG_RBGEN, rng_base + RNG_CTRL); + __raw_writel(RNG_WARMUP_COUNT, priv->base + RNG_STATUS); + __raw_writel(RNG_RBGEN, priv->base + RNG_CTRL); /* register driver */ - err = hwrng_register(&bcm2835_rng_ops); + err = hwrng_register(&priv->rng); if (err) dev_err(dev, "hwrng registration failed\n"); else @@ -119,13 +132,13 @@ static int bcm2835_rng_probe(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 */ - __raw_writel(0, rng_base + RNG_CTRL); + __raw_writel(0, priv->base + RNG_CTRL); /* unregister driver */ - hwrng_unregister(&bcm2835_rng_ops); + hwrng_unregister(&priv->rng); return 0; } |