blob: c6df5b29af08f7f7ba99f6439b0d5d6f120379c7 [file] [log] [blame]
Olof Johanssonb8cb3442007-05-09 02:33:35 -07001/*
2 * Copyright (C) 2006-2007 PA Semi, Inc
3 *
4 * Maintained by: Olof Johansson <olof@lixom.net>
5 *
6 * Driver for the PWRficient onchip rng
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/platform_device.h>
25#include <linux/hw_random.h>
Patrick McHardy984e9762007-11-21 12:24:45 +080026#include <linux/delay.h>
Stephen Rothwell98cad102008-05-23 16:20:05 +100027#include <linux/of_platform.h>
Olof Johanssonb8cb3442007-05-09 02:33:35 -070028#include <asm/io.h>
29
30#define SDCRNG_CTL_REG 0x00
31#define SDCRNG_CTL_FVLD_M 0x0000f000
32#define SDCRNG_CTL_FVLD_S 12
33#define SDCRNG_CTL_KSZ 0x00000800
34#define SDCRNG_CTL_RSRC_CRG 0x00000010
35#define SDCRNG_CTL_RSRC_RRG 0x00000000
36#define SDCRNG_CTL_CE 0x00000004
37#define SDCRNG_CTL_RE 0x00000002
38#define SDCRNG_CTL_DR 0x00000001
39#define SDCRNG_CTL_SELECT_RRG_RNG (SDCRNG_CTL_RE | SDCRNG_CTL_RSRC_RRG)
40#define SDCRNG_CTL_SELECT_CRG_RNG (SDCRNG_CTL_CE | SDCRNG_CTL_RSRC_CRG)
41#define SDCRNG_VAL_REG 0x20
42
43#define MODULE_NAME "pasemi_rng"
44
Kamalesh Babulal9aa6ad32007-12-01 12:52:35 +110045static int pasemi_rng_data_present(struct hwrng *rng, int wait)
Olof Johanssonb8cb3442007-05-09 02:33:35 -070046{
47 void __iomem *rng_regs = (void __iomem *)rng->priv;
Patrick McHardy984e9762007-11-21 12:24:45 +080048 int data, i;
Olof Johanssonb8cb3442007-05-09 02:33:35 -070049
Patrick McHardy984e9762007-11-21 12:24:45 +080050 for (i = 0; i < 20; i++) {
51 data = (in_le32(rng_regs + SDCRNG_CTL_REG)
52 & SDCRNG_CTL_FVLD_M) ? 1 : 0;
53 if (data || !wait)
54 break;
55 udelay(10);
56 }
57 return data;
Olof Johanssonb8cb3442007-05-09 02:33:35 -070058}
59
60static int pasemi_rng_data_read(struct hwrng *rng, u32 *data)
61{
62 void __iomem *rng_regs = (void __iomem *)rng->priv;
63 *data = in_le32(rng_regs + SDCRNG_VAL_REG);
64 return 4;
65}
66
67static int pasemi_rng_init(struct hwrng *rng)
68{
69 void __iomem *rng_regs = (void __iomem *)rng->priv;
70 u32 ctl;
71
72 ctl = SDCRNG_CTL_DR | SDCRNG_CTL_SELECT_RRG_RNG | SDCRNG_CTL_KSZ;
73 out_le32(rng_regs + SDCRNG_CTL_REG, ctl);
74 out_le32(rng_regs + SDCRNG_CTL_REG, ctl & ~SDCRNG_CTL_DR);
75
76 return 0;
77}
78
79static void pasemi_rng_cleanup(struct hwrng *rng)
80{
81 void __iomem *rng_regs = (void __iomem *)rng->priv;
82 u32 ctl;
83
84 ctl = SDCRNG_CTL_RE | SDCRNG_CTL_CE;
85 out_le32(rng_regs + SDCRNG_CTL_REG,
86 in_le32(rng_regs + SDCRNG_CTL_REG) & ~ctl);
87}
88
89static struct hwrng pasemi_rng = {
90 .name = MODULE_NAME,
91 .init = pasemi_rng_init,
92 .cleanup = pasemi_rng_cleanup,
93 .data_present = pasemi_rng_data_present,
94 .data_read = pasemi_rng_data_read,
95};
96
Greg Kroah-Hartmanbcd29822012-12-21 15:12:08 -080097static int rng_probe(struct platform_device *ofdev)
Olof Johanssonb8cb3442007-05-09 02:33:35 -070098{
99 void __iomem *rng_regs;
Grant Likely61c7a082010-04-13 16:12:29 -0700100 struct device_node *rng_np = ofdev->dev.of_node;
Olof Johanssonb8cb3442007-05-09 02:33:35 -0700101 struct resource res;
102 int err = 0;
103
104 err = of_address_to_resource(rng_np, 0, &res);
105 if (err)
106 return -ENODEV;
107
108 rng_regs = ioremap(res.start, 0x100);
109
110 if (!rng_regs)
111 return -ENOMEM;
112
113 pasemi_rng.priv = (unsigned long)rng_regs;
114
115 printk(KERN_INFO "Registering PA Semi RNG\n");
116
117 err = hwrng_register(&pasemi_rng);
118
119 if (err)
120 iounmap(rng_regs);
121
122 return err;
123}
124
Bill Pemberton39af33f2012-11-19 13:26:26 -0500125static int rng_remove(struct platform_device *dev)
Olof Johanssonb8cb3442007-05-09 02:33:35 -0700126{
127 void __iomem *rng_regs = (void __iomem *)pasemi_rng.priv;
128
129 hwrng_unregister(&pasemi_rng);
130 iounmap(rng_regs);
131
132 return 0;
133}
134
135static struct of_device_id rng_match[] = {
Olof Johansson0d08a842007-11-04 20:57:45 -0600136 { .compatible = "1682m-rng", },
137 { .compatible = "pasemi,pwrficient-rng", },
138 { },
Olof Johanssonb8cb3442007-05-09 02:33:35 -0700139};
140
Grant Likely00006122011-02-22 19:59:54 -0700141static struct platform_driver rng_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700142 .driver = {
143 .name = "pasemi-rng",
144 .owner = THIS_MODULE,
145 .of_match_table = rng_match,
146 },
Olof Johanssonb8cb3442007-05-09 02:33:35 -0700147 .probe = rng_probe,
148 .remove = rng_remove,
149};
150
Axel Linb21cb322011-11-26 21:11:06 +0800151module_platform_driver(rng_driver);
Olof Johanssonb8cb3442007-05-09 02:33:35 -0700152
153MODULE_LICENSE("GPL");
154MODULE_AUTHOR("Egor Martovetsky <egor@pasemi.com>");
155MODULE_DESCRIPTION("H/W RNG driver for PA Semi processor");