Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * PIC32 RNG driver
4 *
5 * Joshua Henderson <joshua.henderson@microchip.com>
6 * Copyright (C) 2016 Microchip Technology Inc. All rights reserved.
7 */
8
9#include <linux/clk.h>
10#include <linux/clkdev.h>
11#include <linux/err.h>
12#include <linux/hw_random.h>
13#include <linux/io.h>
14#include <linux/kernel.h>
15#include <linux/mod_devicetable.h>
16#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/slab.h>
19
20#define RNGCON 0x04
21#define TRNGEN BIT(8)
22#define TRNGMOD BIT(11)
23#define RNGSEED1 0x18
24#define RNGSEED2 0x1C
25#define RNGRCNT 0x20
26#define RCNT_MASK 0x7F
27
28struct pic32_rng {
29 void __iomem *base;
30 struct hwrng rng;
31};
32
33/*
34 * The TRNG can generate up to 24Mbps. This is a timeout that should be safe
35 * enough given the instructions in the loop and that the TRNG may not always
36 * be at maximum rate.
37 */
38#define RNG_TIMEOUT 500
39
40static int pic32_rng_init(struct hwrng *rng)
41{
42 struct pic32_rng *priv = container_of(rng, struct pic32_rng, rng);
43
44 /* enable TRNG in enhanced mode */
45 writel(TRNGEN | TRNGMOD, priv->base + RNGCON);
46 return 0;
47}
48
49static int pic32_rng_read(struct hwrng *rng, void *buf, size_t max,
50 bool wait)
51{
52 struct pic32_rng *priv = container_of(rng, struct pic32_rng, rng);
53 u64 *data = buf;
54 u32 t;
55 unsigned int timeout = RNG_TIMEOUT;
56
57 do {
58 t = readl(priv->base + RNGRCNT) & RCNT_MASK;
59 if (t == 64) {
60 /* TRNG value comes through the seed registers */
61 *data = ((u64)readl(priv->base + RNGSEED2) << 32) +
62 readl(priv->base + RNGSEED1);
63 return 8;
64 }
65 } while (wait && --timeout);
66
67 return -EIO;
68}
69
70static void pic32_rng_cleanup(struct hwrng *rng)
71{
72 struct pic32_rng *priv = container_of(rng, struct pic32_rng, rng);
73
74 writel(0, priv->base + RNGCON);
75}
76
77static int pic32_rng_probe(struct platform_device *pdev)
78{
79 struct pic32_rng *priv;
80 struct clk *clk;
81
82 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
83 if (!priv)
84 return -ENOMEM;
85
86 priv->base = devm_platform_ioremap_resource(pdev, 0);
87 if (IS_ERR(priv->base))
88 return PTR_ERR(priv->base);
89
90 clk = devm_clk_get_enabled(&pdev->dev, NULL);
91 if (IS_ERR(clk))
92 return PTR_ERR(clk);
93
94 priv->rng.name = pdev->name;
95 priv->rng.init = pic32_rng_init;
96 priv->rng.read = pic32_rng_read;
97 priv->rng.cleanup = pic32_rng_cleanup;
98
99 return devm_hwrng_register(&pdev->dev, &priv->rng);
100}
101
102static const struct of_device_id pic32_rng_of_match[] __maybe_unused = {
103 { .compatible = "microchip,pic32mzda-rng", },
104 { /* sentinel */ }
105};
106MODULE_DEVICE_TABLE(of, pic32_rng_of_match);
107
108static struct platform_driver pic32_rng_driver = {
109 .probe = pic32_rng_probe,
110 .driver = {
111 .name = "pic32-rng",
112 .of_match_table = pic32_rng_of_match,
113 },
114};
115
116module_platform_driver(pic32_rng_driver);
117
118MODULE_LICENSE("GPL");
119MODULE_AUTHOR("Joshua Henderson <joshua.henderson@microchip.com>");
120MODULE_DESCRIPTION("Microchip PIC32 RNG Driver");
1/*
2 * PIC32 RNG driver
3 *
4 * Joshua Henderson <joshua.henderson@microchip.com>
5 * Copyright (C) 2016 Microchip Technology Inc. All rights reserved.
6 *
7 * This program is free software; you can distribute it and/or modify it
8 * under the terms of the GNU General Public License (Version 2) as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17#include <linux/clk.h>
18#include <linux/clkdev.h>
19#include <linux/err.h>
20#include <linux/hw_random.h>
21#include <linux/io.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/of.h>
25#include <linux/of_device.h>
26#include <linux/platform_device.h>
27#include <linux/slab.h>
28
29#define RNGCON 0x04
30#define TRNGEN BIT(8)
31#define PRNGEN BIT(9)
32#define PRNGCONT BIT(10)
33#define TRNGMOD BIT(11)
34#define SEEDLOAD BIT(12)
35#define RNGPOLY1 0x08
36#define RNGPOLY2 0x0C
37#define RNGNUMGEN1 0x10
38#define RNGNUMGEN2 0x14
39#define RNGSEED1 0x18
40#define RNGSEED2 0x1C
41#define RNGRCNT 0x20
42#define RCNT_MASK 0x7F
43
44struct pic32_rng {
45 void __iomem *base;
46 struct hwrng rng;
47 struct clk *clk;
48};
49
50/*
51 * The TRNG can generate up to 24Mbps. This is a timeout that should be safe
52 * enough given the instructions in the loop and that the TRNG may not always
53 * be at maximum rate.
54 */
55#define RNG_TIMEOUT 500
56
57static int pic32_rng_read(struct hwrng *rng, void *buf, size_t max,
58 bool wait)
59{
60 struct pic32_rng *priv = container_of(rng, struct pic32_rng, rng);
61 u64 *data = buf;
62 u32 t;
63 unsigned int timeout = RNG_TIMEOUT;
64
65 do {
66 t = readl(priv->base + RNGRCNT) & RCNT_MASK;
67 if (t == 64) {
68 /* TRNG value comes through the seed registers */
69 *data = ((u64)readl(priv->base + RNGSEED2) << 32) +
70 readl(priv->base + RNGSEED1);
71 return 8;
72 }
73 } while (wait && --timeout);
74
75 return -EIO;
76}
77
78static int pic32_rng_probe(struct platform_device *pdev)
79{
80 struct pic32_rng *priv;
81 struct resource *res;
82 u32 v;
83 int ret;
84
85 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
86 if (!priv)
87 return -ENOMEM;
88
89 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
90 priv->base = devm_ioremap_resource(&pdev->dev, res);
91 if (IS_ERR(priv->base))
92 return PTR_ERR(priv->base);
93
94 priv->clk = devm_clk_get(&pdev->dev, NULL);
95 if (IS_ERR(priv->clk))
96 return PTR_ERR(priv->clk);
97
98 ret = clk_prepare_enable(priv->clk);
99 if (ret)
100 return ret;
101
102 /* enable TRNG in enhanced mode */
103 v = TRNGEN | TRNGMOD;
104 writel(v, priv->base + RNGCON);
105
106 priv->rng.name = pdev->name;
107 priv->rng.read = pic32_rng_read;
108
109 ret = hwrng_register(&priv->rng);
110 if (ret)
111 goto err_register;
112
113 platform_set_drvdata(pdev, priv);
114
115 return 0;
116
117err_register:
118 clk_disable_unprepare(priv->clk);
119 return ret;
120}
121
122static int pic32_rng_remove(struct platform_device *pdev)
123{
124 struct pic32_rng *rng = platform_get_drvdata(pdev);
125
126 hwrng_unregister(&rng->rng);
127 writel(0, rng->base + RNGCON);
128 clk_disable_unprepare(rng->clk);
129 return 0;
130}
131
132static const struct of_device_id pic32_rng_of_match[] = {
133 { .compatible = "microchip,pic32mzda-rng", },
134 { /* sentinel */ }
135};
136MODULE_DEVICE_TABLE(of, pic32_rng_of_match);
137
138static struct platform_driver pic32_rng_driver = {
139 .probe = pic32_rng_probe,
140 .remove = pic32_rng_remove,
141 .driver = {
142 .name = "pic32-rng",
143 .of_match_table = of_match_ptr(pic32_rng_of_match),
144 },
145};
146
147module_platform_driver(pic32_rng_driver);
148
149MODULE_LICENSE("GPL");
150MODULE_AUTHOR("Joshua Henderson <joshua.henderson@microchip.com>");
151MODULE_DESCRIPTION("Microchip PIC32 RNG Driver");