Olof Johansson | b8cb344 | 2007-05-09 02:33:35 -0700 | [diff] [blame] | 1 | /* |
| 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 McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 26 | #include <linux/delay.h> |
Rob Herring | 5af5073 | 2013-09-17 14:28:33 -0500 | [diff] [blame] | 27 | #include <linux/of_address.h> |
Stephen Rothwell | 98cad10 | 2008-05-23 16:20:05 +1000 | [diff] [blame] | 28 | #include <linux/of_platform.h> |
Olof Johansson | b8cb344 | 2007-05-09 02:33:35 -0700 | [diff] [blame] | 29 | #include <asm/io.h> |
| 30 | |
| 31 | #define SDCRNG_CTL_REG 0x00 |
| 32 | #define SDCRNG_CTL_FVLD_M 0x0000f000 |
| 33 | #define SDCRNG_CTL_FVLD_S 12 |
| 34 | #define SDCRNG_CTL_KSZ 0x00000800 |
| 35 | #define SDCRNG_CTL_RSRC_CRG 0x00000010 |
| 36 | #define SDCRNG_CTL_RSRC_RRG 0x00000000 |
| 37 | #define SDCRNG_CTL_CE 0x00000004 |
| 38 | #define SDCRNG_CTL_RE 0x00000002 |
| 39 | #define SDCRNG_CTL_DR 0x00000001 |
| 40 | #define SDCRNG_CTL_SELECT_RRG_RNG (SDCRNG_CTL_RE | SDCRNG_CTL_RSRC_RRG) |
| 41 | #define SDCRNG_CTL_SELECT_CRG_RNG (SDCRNG_CTL_CE | SDCRNG_CTL_RSRC_CRG) |
| 42 | #define SDCRNG_VAL_REG 0x20 |
| 43 | |
| 44 | #define MODULE_NAME "pasemi_rng" |
| 45 | |
Kamalesh Babulal | 9aa6ad3 | 2007-12-01 12:52:35 +1100 | [diff] [blame] | 46 | static int pasemi_rng_data_present(struct hwrng *rng, int wait) |
Olof Johansson | b8cb344 | 2007-05-09 02:33:35 -0700 | [diff] [blame] | 47 | { |
| 48 | void __iomem *rng_regs = (void __iomem *)rng->priv; |
Patrick McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 49 | int data, i; |
Olof Johansson | b8cb344 | 2007-05-09 02:33:35 -0700 | [diff] [blame] | 50 | |
Patrick McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 51 | for (i = 0; i < 20; i++) { |
| 52 | data = (in_le32(rng_regs + SDCRNG_CTL_REG) |
| 53 | & SDCRNG_CTL_FVLD_M) ? 1 : 0; |
| 54 | if (data || !wait) |
| 55 | break; |
| 56 | udelay(10); |
| 57 | } |
| 58 | return data; |
Olof Johansson | b8cb344 | 2007-05-09 02:33:35 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static int pasemi_rng_data_read(struct hwrng *rng, u32 *data) |
| 62 | { |
| 63 | void __iomem *rng_regs = (void __iomem *)rng->priv; |
| 64 | *data = in_le32(rng_regs + SDCRNG_VAL_REG); |
| 65 | return 4; |
| 66 | } |
| 67 | |
| 68 | static int pasemi_rng_init(struct hwrng *rng) |
| 69 | { |
| 70 | void __iomem *rng_regs = (void __iomem *)rng->priv; |
| 71 | u32 ctl; |
| 72 | |
| 73 | ctl = SDCRNG_CTL_DR | SDCRNG_CTL_SELECT_RRG_RNG | SDCRNG_CTL_KSZ; |
| 74 | out_le32(rng_regs + SDCRNG_CTL_REG, ctl); |
| 75 | out_le32(rng_regs + SDCRNG_CTL_REG, ctl & ~SDCRNG_CTL_DR); |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static void pasemi_rng_cleanup(struct hwrng *rng) |
| 81 | { |
| 82 | void __iomem *rng_regs = (void __iomem *)rng->priv; |
| 83 | u32 ctl; |
| 84 | |
| 85 | ctl = SDCRNG_CTL_RE | SDCRNG_CTL_CE; |
| 86 | out_le32(rng_regs + SDCRNG_CTL_REG, |
| 87 | in_le32(rng_regs + SDCRNG_CTL_REG) & ~ctl); |
| 88 | } |
| 89 | |
| 90 | static struct hwrng pasemi_rng = { |
| 91 | .name = MODULE_NAME, |
| 92 | .init = pasemi_rng_init, |
| 93 | .cleanup = pasemi_rng_cleanup, |
| 94 | .data_present = pasemi_rng_data_present, |
| 95 | .data_read = pasemi_rng_data_read, |
| 96 | }; |
| 97 | |
Greg Kroah-Hartman | bcd2982 | 2012-12-21 15:12:08 -0800 | [diff] [blame] | 98 | static int rng_probe(struct platform_device *ofdev) |
Olof Johansson | b8cb344 | 2007-05-09 02:33:35 -0700 | [diff] [blame] | 99 | { |
| 100 | void __iomem *rng_regs; |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 101 | struct device_node *rng_np = ofdev->dev.of_node; |
Olof Johansson | b8cb344 | 2007-05-09 02:33:35 -0700 | [diff] [blame] | 102 | struct resource res; |
| 103 | int err = 0; |
| 104 | |
| 105 | err = of_address_to_resource(rng_np, 0, &res); |
| 106 | if (err) |
| 107 | return -ENODEV; |
| 108 | |
| 109 | rng_regs = ioremap(res.start, 0x100); |
| 110 | |
| 111 | if (!rng_regs) |
| 112 | return -ENOMEM; |
| 113 | |
| 114 | pasemi_rng.priv = (unsigned long)rng_regs; |
| 115 | |
Sudip Mukherjee | 7a1ae9c | 2014-09-15 20:31:20 +0530 | [diff] [blame] | 116 | pr_info("Registering PA Semi RNG\n"); |
Olof Johansson | b8cb344 | 2007-05-09 02:33:35 -0700 | [diff] [blame] | 117 | |
| 118 | err = hwrng_register(&pasemi_rng); |
| 119 | |
| 120 | if (err) |
| 121 | iounmap(rng_regs); |
| 122 | |
| 123 | return err; |
| 124 | } |
| 125 | |
Bill Pemberton | 39af33f | 2012-11-19 13:26:26 -0500 | [diff] [blame] | 126 | static int rng_remove(struct platform_device *dev) |
Olof Johansson | b8cb344 | 2007-05-09 02:33:35 -0700 | [diff] [blame] | 127 | { |
| 128 | void __iomem *rng_regs = (void __iomem *)pasemi_rng.priv; |
| 129 | |
| 130 | hwrng_unregister(&pasemi_rng); |
| 131 | iounmap(rng_regs); |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
Fabian Frederick | da2ff52 | 2015-03-16 20:17:13 +0100 | [diff] [blame] | 136 | static const struct of_device_id rng_match[] = { |
Olof Johansson | 0d08a84 | 2007-11-04 20:57:45 -0600 | [diff] [blame] | 137 | { .compatible = "1682m-rng", }, |
| 138 | { .compatible = "pasemi,pwrficient-rng", }, |
| 139 | { }, |
Olof Johansson | b8cb344 | 2007-05-09 02:33:35 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
Grant Likely | 0000612 | 2011-02-22 19:59:54 -0700 | [diff] [blame] | 142 | static struct platform_driver rng_driver = { |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 143 | .driver = { |
| 144 | .name = "pasemi-rng", |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 145 | .of_match_table = rng_match, |
| 146 | }, |
Olof Johansson | b8cb344 | 2007-05-09 02:33:35 -0700 | [diff] [blame] | 147 | .probe = rng_probe, |
| 148 | .remove = rng_remove, |
| 149 | }; |
| 150 | |
Axel Lin | b21cb32 | 2011-11-26 21:11:06 +0800 | [diff] [blame] | 151 | module_platform_driver(rng_driver); |
Olof Johansson | b8cb344 | 2007-05-09 02:33:35 -0700 | [diff] [blame] | 152 | |
| 153 | MODULE_LICENSE("GPL"); |
| 154 | MODULE_AUTHOR("Egor Martovetsky <egor@pasemi.com>"); |
| 155 | MODULE_DESCRIPTION("H/W RNG driver for PA Semi processor"); |