1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3 * Copyright (c) 2016 BayLibre, SAS.
4 * Author: Neil Armstrong <narmstrong@baylibre.com>
5 * Copyright (C) 2014 Amlogic, Inc.
6 */
7 #include <linux/err.h>
8 #include <linux/module.h>
9 #include <linux/io.h>
10 #include <linux/platform_device.h>
11 #include <linux/hw_random.h>
12 #include <linux/slab.h>
13 #include <linux/types.h>
14 #include <linux/of.h>
15 #include <linux/clk.h>
16
17 #define RNG_DATA 0x00
18
19 struct meson_rng_data {
20 void __iomem *base;
21 struct platform_device *pdev;
22 struct hwrng rng;
23 struct clk *core_clk;
24 };
25
meson_rng_read(struct hwrng * rng,void * buf,size_t max,bool wait)26 static int meson_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
27 {
28 struct meson_rng_data *data =
29 container_of(rng, struct meson_rng_data, rng);
30
31 *(u32 *)buf = readl_relaxed(data->base + RNG_DATA);
32
33 return sizeof(u32);
34 }
35
meson_rng_clk_disable(void * data)36 static void meson_rng_clk_disable(void *data)
37 {
38 clk_disable_unprepare(data);
39 }
40
meson_rng_probe(struct platform_device * pdev)41 static int meson_rng_probe(struct platform_device *pdev)
42 {
43 struct device *dev = &pdev->dev;
44 struct meson_rng_data *data;
45 int ret;
46
47 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
48 if (!data)
49 return -ENOMEM;
50
51 data->pdev = pdev;
52
53 data->base = devm_platform_ioremap_resource(pdev, 0);
54 if (IS_ERR(data->base))
55 return PTR_ERR(data->base);
56
57 data->core_clk = devm_clk_get_optional(dev, "core");
58 if (IS_ERR(data->core_clk))
59 return dev_err_probe(dev, PTR_ERR(data->core_clk),
60 "Failed to get core clock\n");
61
62 if (data->core_clk) {
63 ret = clk_prepare_enable(data->core_clk);
64 if (ret)
65 return ret;
66 ret = devm_add_action_or_reset(dev, meson_rng_clk_disable,
67 data->core_clk);
68 if (ret)
69 return ret;
70 }
71
72 data->rng.name = pdev->name;
73 data->rng.read = meson_rng_read;
74
75 platform_set_drvdata(pdev, data);
76
77 return devm_hwrng_register(dev, &data->rng);
78 }
79
80 static const struct of_device_id meson_rng_of_match[] = {
81 { .compatible = "amlogic,meson-rng", },
82 {},
83 };
84 MODULE_DEVICE_TABLE(of, meson_rng_of_match);
85
86 static struct platform_driver meson_rng_driver = {
87 .probe = meson_rng_probe,
88 .driver = {
89 .name = "meson-rng",
90 .of_match_table = meson_rng_of_match,
91 },
92 };
93
94 module_platform_driver(meson_rng_driver);
95
96 MODULE_DESCRIPTION("Meson H/W Random Number Generator driver");
97 MODULE_AUTHOR("Lawrence Mok <lawrence.mok@amlogic.com>");
98 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
99 MODULE_LICENSE("Dual BSD/GPL");
100