Commit 6229c160 authored by Dmitry Torokhov's avatar Dmitry Torokhov Committed by Herbert Xu

hwrng: bcm63xx - make use of devm_hwrng_register

This change converts bcm63xx-rng to use devm* API for managing all
resources, which allows us to dispense with the rest of error handling
path and remove() function. Also we combine hwern and driver-private
data into a single allocation, use clk_prepare_enable() instead
of "naked" clk_enable() and move clock enabling/disabling into hwrnd
inti(0 and cleanup() methods so the clock stays off until rng is
used.
Signed-off-by: default avatarDmitry Torokhov <dmitry.torokhov@gmail.com>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 4d9b519c
...@@ -24,16 +24,22 @@ ...@@ -24,16 +24,22 @@
#define RNG_MASK 0x10 #define RNG_MASK 0x10
struct bcm63xx_rng_priv { struct bcm63xx_rng_priv {
struct hwrng rng;
struct clk *clk; struct clk *clk;
void __iomem *regs; void __iomem *regs;
}; };
#define to_rng_priv(rng) ((struct bcm63xx_rng_priv *)rng->priv) #define to_rng_priv(rng) container_of(rng, struct bcm63xx_rng_priv, rng)
static int bcm63xx_rng_init(struct hwrng *rng) static int bcm63xx_rng_init(struct hwrng *rng)
{ {
struct bcm63xx_rng_priv *priv = to_rng_priv(rng); struct bcm63xx_rng_priv *priv = to_rng_priv(rng);
u32 val; u32 val;
int error;
error = clk_prepare_enable(priv->clk);
if (error)
return error;
val = __raw_readl(priv->regs + RNG_CTRL); val = __raw_readl(priv->regs + RNG_CTRL);
val |= RNG_EN; val |= RNG_EN;
...@@ -50,6 +56,8 @@ static void bcm63xx_rng_cleanup(struct hwrng *rng) ...@@ -50,6 +56,8 @@ static void bcm63xx_rng_cleanup(struct hwrng *rng)
val = __raw_readl(priv->regs + RNG_CTRL); val = __raw_readl(priv->regs + RNG_CTRL);
val &= ~RNG_EN; val &= ~RNG_EN;
__raw_writel(val, priv->regs + RNG_CTRL); __raw_writel(val, priv->regs + RNG_CTRL);
clk_didsable_unprepare(prov->clk);
} }
static int bcm63xx_rng_data_present(struct hwrng *rng, int wait) static int bcm63xx_rng_data_present(struct hwrng *rng, int wait)
...@@ -79,86 +87,53 @@ static int bcm63xx_rng_probe(struct platform_device *pdev) ...@@ -79,86 +87,53 @@ static int bcm63xx_rng_probe(struct platform_device *pdev)
r = platform_get_resource(pdev, IORESOURCE_MEM, 0); r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!r) { if (!r) {
dev_err(&pdev->dev, "no iomem resource\n"); dev_err(&pdev->dev, "no iomem resource\n");
ret = -ENXIO; return -ENXIO;
goto out;
} }
priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
if (!priv) { if (!priv)
ret = -ENOMEM; return -ENOMEM;
goto out;
} priv->rng.name = pdev->name;
priv->rng.init = bcm63xx_rng_init;
rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL); priv->rng.cleanup = bcm63xx_rng_cleanup;
if (!rng) { prov->rng.data_present = bcm63xx_rng_data_present;
ret = -ENOMEM; priv->rng.data_read = bcm63xx_rng_data_read;
goto out;
priv->clk = devm_clk_get(&pdev->dev, "ipsec");
if (IS_ERR(priv->clk)) {
error = PTR_ERR(priv->clk);
dev_err(&pdev->dev, "no clock for device: %d\n", error);
return error;
} }
platform_set_drvdata(pdev, rng);
rng->priv = (unsigned long)priv;
rng->name = pdev->name;
rng->init = bcm63xx_rng_init;
rng->cleanup = bcm63xx_rng_cleanup;
rng->data_present = bcm63xx_rng_data_present;
rng->data_read = bcm63xx_rng_data_read;
clk = clk_get(&pdev->dev, "ipsec");
if (IS_ERR(clk)) {
dev_err(&pdev->dev, "no clock for device\n");
ret = PTR_ERR(clk);
goto out;
}
priv->clk = clk;
if (!devm_request_mem_region(&pdev->dev, r->start, if (!devm_request_mem_region(&pdev->dev, r->start,
resource_size(r), pdev->name)) { resource_size(r), pdev->name)) {
dev_err(&pdev->dev, "request mem failed"); dev_err(&pdev->dev, "request mem failed");
ret = -ENOMEM; return -EBUSY;
goto out;
} }
priv->regs = devm_ioremap_nocache(&pdev->dev, r->start, priv->regs = devm_ioremap_nocache(&pdev->dev, r->start,
resource_size(r)); resource_size(r));
if (!priv->regs) { if (!priv->regs) {
dev_err(&pdev->dev, "ioremap failed"); dev_err(&pdev->dev, "ioremap failed");
ret = -ENOMEM; return -ENOMEM;
goto out;
} }
clk_enable(clk); error = devm_hwrng_register(&pdev->dev, &priv->rng);
if (error) {
ret = hwrng_register(rng); dev_err(&pdev->dev, "failed to register rng device: %d\n",
if (ret) { error);
dev_err(&pdev->dev, "failed to register rng device\n"); return error;
goto out_clk_disable;
} }
dev_info(&pdev->dev, "registered RNG driver\n"); dev_info(&pdev->dev, "registered RNG driver\n");
return 0;
out_clk_disable:
clk_disable(clk);
out:
return ret;
}
static int bcm63xx_rng_remove(struct platform_device *pdev)
{
struct hwrng *rng = platform_get_drvdata(pdev);
struct bcm63xx_rng_priv *priv = to_rng_priv(rng);
hwrng_unregister(rng);
clk_disable(priv->clk);
return 0; return 0;
} }
static struct platform_driver bcm63xx_rng_driver = { static struct platform_driver bcm63xx_rng_driver = {
.probe = bcm63xx_rng_probe, .probe = bcm63xx_rng_probe,
.remove = bcm63xx_rng_remove,
.driver = { .driver = {
.name = "bcm63xx-rng", .name = "bcm63xx-rng",
}, },
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment