1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * drivers/char/hw_random/ixp4xx-rng.c
4 *
5 * RNG driver for Intel IXP4xx family of NPUs
6 *
7 * Author: Deepak Saxena <dsaxena@plexity.net>
8 *
9 * Copyright 2005 (c) MontaVista Software, Inc.
10 *
11 * Fixes by Michael Buesch
12 */
13
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/platform_device.h>
19 #include <linux/init.h>
20 #include <linux/bitops.h>
21 #include <linux/hw_random.h>
22 #include <linux/of.h>
23 #include <linux/soc/ixp4xx/cpu.h>
24
25 #include <asm/io.h>
26
ixp4xx_rng_data_read(struct hwrng * rng,u32 * buffer)27 static int ixp4xx_rng_data_read(struct hwrng *rng, u32 *buffer)
28 {
29 void __iomem * rng_base = (void __iomem *)rng->priv;
30
31 *buffer = __raw_readl(rng_base);
32
33 return 4;
34 }
35
36 static struct hwrng ixp4xx_rng_ops = {
37 .name = "ixp4xx",
38 .data_read = ixp4xx_rng_data_read,
39 };
40
ixp4xx_rng_probe(struct platform_device * pdev)41 static int ixp4xx_rng_probe(struct platform_device *pdev)
42 {
43 void __iomem * rng_base;
44 struct device *dev = &pdev->dev;
45 struct resource *res;
46
47 if (!cpu_is_ixp46x()) /* includes IXP455 */
48 return -ENOSYS;
49
50 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
51 rng_base = devm_ioremap_resource(dev, res);
52 if (IS_ERR(rng_base))
53 return PTR_ERR(rng_base);
54
55 ixp4xx_rng_ops.priv = (unsigned long)rng_base;
56 return devm_hwrng_register(dev, &ixp4xx_rng_ops);
57 }
58
59 static const struct of_device_id ixp4xx_rng_of_match[] = {
60 {
61 .compatible = "intel,ixp46x-rng",
62 },
63 {},
64 };
65 MODULE_DEVICE_TABLE(of, ixp4xx_rng_of_match);
66
67 static struct platform_driver ixp4xx_rng_driver = {
68 .driver = {
69 .name = "ixp4xx-hwrandom",
70 .of_match_table = ixp4xx_rng_of_match,
71 },
72 .probe = ixp4xx_rng_probe,
73 };
74 module_platform_driver(ixp4xx_rng_driver);
75
76 MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>");
77 MODULE_DESCRIPTION("H/W Pseudo-Random Number Generator (RNG) driver for IXP45x/46x");
78 MODULE_LICENSE("GPL");
79