1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * Copyright (c) 2016 BayLibre, SAS.
4 * Author: Neil Armstrong <narmstrong@baylibre.com>
5 * Copyright (C) 2014 Amlogic, Inc.
8 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/hw_random.h>
12 #include <linux/slab.h>
13 #include <linux/types.h>
15 #include <linux/clk.h>
19 struct meson_rng_data
{
21 struct platform_device
*pdev
;
26 static int meson_rng_read(struct hwrng
*rng
, void *buf
, size_t max
, bool wait
)
28 struct meson_rng_data
*data
=
29 container_of(rng
, struct meson_rng_data
, rng
);
31 *(u32
*)buf
= readl_relaxed(data
->base
+ RNG_DATA
);
36 static void meson_rng_clk_disable(void *data
)
38 clk_disable_unprepare(data
);
41 static int meson_rng_probe(struct platform_device
*pdev
)
43 struct device
*dev
= &pdev
->dev
;
44 struct meson_rng_data
*data
;
47 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
53 data
->base
= devm_platform_ioremap_resource(pdev
, 0);
54 if (IS_ERR(data
->base
))
55 return PTR_ERR(data
->base
);
57 data
->core_clk
= devm_clk_get(dev
, "core");
58 if (IS_ERR(data
->core_clk
))
59 data
->core_clk
= NULL
;
62 ret
= clk_prepare_enable(data
->core_clk
);
65 ret
= devm_add_action_or_reset(dev
, meson_rng_clk_disable
,
71 data
->rng
.name
= pdev
->name
;
72 data
->rng
.read
= meson_rng_read
;
74 platform_set_drvdata(pdev
, data
);
76 return devm_hwrng_register(dev
, &data
->rng
);
79 static const struct of_device_id meson_rng_of_match
[] = {
80 { .compatible
= "amlogic,meson-rng", },
83 MODULE_DEVICE_TABLE(of
, meson_rng_of_match
);
85 static struct platform_driver meson_rng_driver
= {
86 .probe
= meson_rng_probe
,
89 .of_match_table
= meson_rng_of_match
,
93 module_platform_driver(meson_rng_driver
);
95 MODULE_DESCRIPTION("Meson H/W Random Number Generator driver");
96 MODULE_AUTHOR("Lawrence Mok <lawrence.mok@amlogic.com>");
97 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
98 MODULE_LICENSE("Dual BSD/GPL");