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 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 36 static void meson_rng_clk_disable(void *data) 37 { 38 clk_disable_unprepare(data); 39 } 40 41 static int meson_rng_probe(struct platform_device *pdev) 42 { 43 struct device *dev = &pdev->dev; 44 struct meson_rng_data *data; 45 struct resource *res; 46 int ret; 47 48 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 49 if (!data) 50 return -ENOMEM; 51 52 data->pdev = pdev; 53 54 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 55 data->base = devm_ioremap_resource(dev, res); 56 if (IS_ERR(data->base)) 57 return PTR_ERR(data->base); 58 59 data->core_clk = devm_clk_get(dev, "core"); 60 if (IS_ERR(data->core_clk)) 61 data->core_clk = NULL; 62 63 if (data->core_clk) { 64 ret = clk_prepare_enable(data->core_clk); 65 if (ret) 66 return ret; 67 ret = devm_add_action_or_reset(dev, meson_rng_clk_disable, 68 data->core_clk); 69 if (ret) 70 return ret; 71 } 72 73 data->rng.name = pdev->name; 74 data->rng.read = meson_rng_read; 75 76 platform_set_drvdata(pdev, data); 77 78 return devm_hwrng_register(dev, &data->rng); 79 } 80 81 static const struct of_device_id meson_rng_of_match[] = { 82 { .compatible = "amlogic,meson-rng", }, 83 {}, 84 }; 85 MODULE_DEVICE_TABLE(of, meson_rng_of_match); 86 87 static struct platform_driver meson_rng_driver = { 88 .probe = meson_rng_probe, 89 .driver = { 90 .name = "meson-rng", 91 .of_match_table = meson_rng_of_match, 92 }, 93 }; 94 95 module_platform_driver(meson_rng_driver); 96 97 MODULE_DESCRIPTION("Meson H/W Random Number Generator driver"); 98 MODULE_AUTHOR("Lawrence Mok <lawrence.mok@amlogic.com>"); 99 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>"); 100 MODULE_LICENSE("Dual BSD/GPL"); 101