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 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(dev, "core"); 58 if (IS_ERR(data->core_clk)) 59 data->core_clk = NULL; 60 61 if (data->core_clk) { 62 ret = clk_prepare_enable(data->core_clk); 63 if (ret) 64 return ret; 65 ret = devm_add_action_or_reset(dev, meson_rng_clk_disable, 66 data->core_clk); 67 if (ret) 68 return ret; 69 } 70 71 data->rng.name = pdev->name; 72 data->rng.read = meson_rng_read; 73 74 platform_set_drvdata(pdev, data); 75 76 return devm_hwrng_register(dev, &data->rng); 77 } 78 79 static const struct of_device_id meson_rng_of_match[] = { 80 { .compatible = "amlogic,meson-rng", }, 81 {}, 82 }; 83 MODULE_DEVICE_TABLE(of, meson_rng_of_match); 84 85 static struct platform_driver meson_rng_driver = { 86 .probe = meson_rng_probe, 87 .driver = { 88 .name = "meson-rng", 89 .of_match_table = meson_rng_of_match, 90 }, 91 }; 92 93 module_platform_driver(meson_rng_driver); 94 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"); 99