Commit | Line | Data |
---|---|---|
45051539 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
b8cb3448 OJ |
2 | /* |
3 | * Copyright (C) 2006-2007 PA Semi, Inc | |
4 | * | |
5 | * Maintained by: Olof Johansson <olof@lixom.net> | |
6 | * | |
7 | * Driver for the PWRficient onchip rng | |
b8cb3448 OJ |
8 | */ |
9 | ||
10 | #include <linux/module.h> | |
11 | #include <linux/kernel.h> | |
12 | #include <linux/platform_device.h> | |
13 | #include <linux/hw_random.h> | |
984e976f | 14 | #include <linux/delay.h> |
5af50730 | 15 | #include <linux/of_address.h> |
98cad101 | 16 | #include <linux/of_platform.h> |
a826806b | 17 | #include <linux/io.h> |
b8cb3448 OJ |
18 | |
19 | #define SDCRNG_CTL_REG 0x00 | |
20 | #define SDCRNG_CTL_FVLD_M 0x0000f000 | |
21 | #define SDCRNG_CTL_FVLD_S 12 | |
22 | #define SDCRNG_CTL_KSZ 0x00000800 | |
23 | #define SDCRNG_CTL_RSRC_CRG 0x00000010 | |
24 | #define SDCRNG_CTL_RSRC_RRG 0x00000000 | |
25 | #define SDCRNG_CTL_CE 0x00000004 | |
26 | #define SDCRNG_CTL_RE 0x00000002 | |
27 | #define SDCRNG_CTL_DR 0x00000001 | |
28 | #define SDCRNG_CTL_SELECT_RRG_RNG (SDCRNG_CTL_RE | SDCRNG_CTL_RSRC_RRG) | |
29 | #define SDCRNG_CTL_SELECT_CRG_RNG (SDCRNG_CTL_CE | SDCRNG_CTL_RSRC_CRG) | |
30 | #define SDCRNG_VAL_REG 0x20 | |
31 | ||
32 | #define MODULE_NAME "pasemi_rng" | |
33 | ||
9aa6ad3e | 34 | static int pasemi_rng_data_present(struct hwrng *rng, int wait) |
b8cb3448 OJ |
35 | { |
36 | void __iomem *rng_regs = (void __iomem *)rng->priv; | |
984e976f | 37 | int data, i; |
b8cb3448 | 38 | |
984e976f PM |
39 | for (i = 0; i < 20; i++) { |
40 | data = (in_le32(rng_regs + SDCRNG_CTL_REG) | |
41 | & SDCRNG_CTL_FVLD_M) ? 1 : 0; | |
42 | if (data || !wait) | |
43 | break; | |
44 | udelay(10); | |
45 | } | |
46 | return data; | |
b8cb3448 OJ |
47 | } |
48 | ||
49 | static int pasemi_rng_data_read(struct hwrng *rng, u32 *data) | |
50 | { | |
51 | void __iomem *rng_regs = (void __iomem *)rng->priv; | |
52 | *data = in_le32(rng_regs + SDCRNG_VAL_REG); | |
53 | return 4; | |
54 | } | |
55 | ||
56 | static int pasemi_rng_init(struct hwrng *rng) | |
57 | { | |
58 | void __iomem *rng_regs = (void __iomem *)rng->priv; | |
59 | u32 ctl; | |
60 | ||
61 | ctl = SDCRNG_CTL_DR | SDCRNG_CTL_SELECT_RRG_RNG | SDCRNG_CTL_KSZ; | |
62 | out_le32(rng_regs + SDCRNG_CTL_REG, ctl); | |
63 | out_le32(rng_regs + SDCRNG_CTL_REG, ctl & ~SDCRNG_CTL_DR); | |
64 | ||
65 | return 0; | |
66 | } | |
67 | ||
68 | static void pasemi_rng_cleanup(struct hwrng *rng) | |
69 | { | |
70 | void __iomem *rng_regs = (void __iomem *)rng->priv; | |
71 | u32 ctl; | |
72 | ||
73 | ctl = SDCRNG_CTL_RE | SDCRNG_CTL_CE; | |
74 | out_le32(rng_regs + SDCRNG_CTL_REG, | |
75 | in_le32(rng_regs + SDCRNG_CTL_REG) & ~ctl); | |
76 | } | |
77 | ||
78 | static struct hwrng pasemi_rng = { | |
79 | .name = MODULE_NAME, | |
80 | .init = pasemi_rng_init, | |
81 | .cleanup = pasemi_rng_cleanup, | |
82 | .data_present = pasemi_rng_data_present, | |
83 | .data_read = pasemi_rng_data_read, | |
84 | }; | |
85 | ||
da28caaa | 86 | static int rng_probe(struct platform_device *pdev) |
b8cb3448 OJ |
87 | { |
88 | void __iomem *rng_regs; | |
da28caaa | 89 | struct resource *res; |
b8cb3448 | 90 | |
da28caaa PM |
91 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
92 | rng_regs = devm_ioremap_resource(&pdev->dev, res); | |
93 | if (IS_ERR(rng_regs)) | |
94 | return PTR_ERR(rng_regs); | |
b8cb3448 OJ |
95 | |
96 | pasemi_rng.priv = (unsigned long)rng_regs; | |
97 | ||
7a1ae9c0 | 98 | pr_info("Registering PA Semi RNG\n"); |
da28caaa | 99 | return devm_hwrng_register(&pdev->dev, &pasemi_rng); |
b8cb3448 OJ |
100 | } |
101 | ||
da2ff527 | 102 | static const struct of_device_id rng_match[] = { |
0d08a847 OJ |
103 | { .compatible = "1682m-rng", }, |
104 | { .compatible = "pasemi,pwrficient-rng", }, | |
105 | { }, | |
b8cb3448 | 106 | }; |
5f8741d1 | 107 | MODULE_DEVICE_TABLE(of, rng_match); |
b8cb3448 | 108 | |
00006124 | 109 | static struct platform_driver rng_driver = { |
4018294b GL |
110 | .driver = { |
111 | .name = "pasemi-rng", | |
4018294b GL |
112 | .of_match_table = rng_match, |
113 | }, | |
b8cb3448 | 114 | .probe = rng_probe, |
b8cb3448 OJ |
115 | }; |
116 | ||
b21cb324 | 117 | module_platform_driver(rng_driver); |
b8cb3448 OJ |
118 | |
119 | MODULE_LICENSE("GPL"); | |
120 | MODULE_AUTHOR("Egor Martovetsky <egor@pasemi.com>"); | |
121 | MODULE_DESCRIPTION("H/W RNG driver for PA Semi processor"); |