| Atsushi Nemoto | 049a947 | 2009-06-02 23:54:21 +0900 | [diff] [blame] | 1 | /* | 
|  | 2 | * RNG driver for TX4939 Random Number Generators (RNG) | 
|  | 3 | * | 
|  | 4 | * Copyright (C) 2009 Atsushi Nemoto <anemo@mba.ocn.ne.jp> | 
|  | 5 | * | 
|  | 6 | * This file is subject to the terms and conditions of the GNU General Public | 
|  | 7 | * License.  See the file "COPYING" in the main directory of this archive | 
|  | 8 | * for more details. | 
|  | 9 | */ | 
|  | 10 | #include <linux/module.h> | 
|  | 11 | #include <linux/kernel.h> | 
|  | 12 | #include <linux/init.h> | 
|  | 13 | #include <linux/delay.h> | 
|  | 14 | #include <linux/io.h> | 
|  | 15 | #include <linux/platform_device.h> | 
|  | 16 | #include <linux/hw_random.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/gfp.h> | 
| Atsushi Nemoto | 049a947 | 2009-06-02 23:54:21 +0900 | [diff] [blame] | 18 |  | 
|  | 19 | #define TX4939_RNG_RCSR		0x00000000 | 
|  | 20 | #define TX4939_RNG_ROR(n)	(0x00000018 + (n) * 8) | 
|  | 21 |  | 
|  | 22 | #define TX4939_RNG_RCSR_INTE	0x00000008 | 
|  | 23 | #define TX4939_RNG_RCSR_RST	0x00000004 | 
|  | 24 | #define TX4939_RNG_RCSR_FIN	0x00000002 | 
|  | 25 | #define TX4939_RNG_RCSR_ST	0x00000001 | 
|  | 26 |  | 
|  | 27 | struct tx4939_rng { | 
|  | 28 | struct hwrng rng; | 
|  | 29 | void __iomem *base; | 
|  | 30 | u64 databuf[3]; | 
|  | 31 | unsigned int data_avail; | 
|  | 32 | }; | 
|  | 33 |  | 
|  | 34 | static void rng_io_start(void) | 
|  | 35 | { | 
|  | 36 | #ifndef CONFIG_64BIT | 
|  | 37 | /* | 
|  | 38 | * readq is reading a 64-bit register using a 64-bit load.  On | 
|  | 39 | * a 32-bit kernel however interrupts or any other processor | 
|  | 40 | * exception would clobber the upper 32-bit of the processor | 
|  | 41 | * register so interrupts need to be disabled. | 
|  | 42 | */ | 
|  | 43 | local_irq_disable(); | 
|  | 44 | #endif | 
|  | 45 | } | 
|  | 46 |  | 
|  | 47 | static void rng_io_end(void) | 
|  | 48 | { | 
|  | 49 | #ifndef CONFIG_64BIT | 
|  | 50 | local_irq_enable(); | 
|  | 51 | #endif | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | static u64 read_rng(void __iomem *base, unsigned int offset) | 
|  | 55 | { | 
|  | 56 | return ____raw_readq(base + offset); | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 | static void write_rng(u64 val, void __iomem *base, unsigned int offset) | 
|  | 60 | { | 
|  | 61 | return ____raw_writeq(val, base + offset); | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | static int tx4939_rng_data_present(struct hwrng *rng, int wait) | 
|  | 65 | { | 
|  | 66 | struct tx4939_rng *rngdev = container_of(rng, struct tx4939_rng, rng); | 
|  | 67 | int i; | 
|  | 68 |  | 
|  | 69 | if (rngdev->data_avail) | 
|  | 70 | return rngdev->data_avail; | 
|  | 71 | for (i = 0; i < 20; i++) { | 
|  | 72 | rng_io_start(); | 
|  | 73 | if (!(read_rng(rngdev->base, TX4939_RNG_RCSR) | 
|  | 74 | & TX4939_RNG_RCSR_ST)) { | 
|  | 75 | rngdev->databuf[0] = | 
|  | 76 | read_rng(rngdev->base, TX4939_RNG_ROR(0)); | 
|  | 77 | rngdev->databuf[1] = | 
|  | 78 | read_rng(rngdev->base, TX4939_RNG_ROR(1)); | 
|  | 79 | rngdev->databuf[2] = | 
|  | 80 | read_rng(rngdev->base, TX4939_RNG_ROR(2)); | 
|  | 81 | rngdev->data_avail = | 
|  | 82 | sizeof(rngdev->databuf) / sizeof(u32); | 
|  | 83 | /* Start RNG */ | 
|  | 84 | write_rng(TX4939_RNG_RCSR_ST, | 
|  | 85 | rngdev->base, TX4939_RNG_RCSR); | 
|  | 86 | wait = 0; | 
|  | 87 | } | 
|  | 88 | rng_io_end(); | 
|  | 89 | if (!wait) | 
|  | 90 | break; | 
|  | 91 | /* 90 bus clock cycles by default for generation */ | 
|  | 92 | ndelay(90 * 5); | 
|  | 93 | } | 
|  | 94 | return rngdev->data_avail; | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | static int tx4939_rng_data_read(struct hwrng *rng, u32 *buffer) | 
|  | 98 | { | 
|  | 99 | struct tx4939_rng *rngdev = container_of(rng, struct tx4939_rng, rng); | 
|  | 100 |  | 
|  | 101 | rngdev->data_avail--; | 
|  | 102 | *buffer = *((u32 *)&rngdev->databuf + rngdev->data_avail); | 
|  | 103 | return sizeof(u32); | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | static int __init tx4939_rng_probe(struct platform_device *dev) | 
|  | 107 | { | 
|  | 108 | struct tx4939_rng *rngdev; | 
|  | 109 | struct resource *r; | 
|  | 110 | int i; | 
|  | 111 |  | 
|  | 112 | r = platform_get_resource(dev, IORESOURCE_MEM, 0); | 
|  | 113 | if (!r) | 
|  | 114 | return -EBUSY; | 
|  | 115 | rngdev = devm_kzalloc(&dev->dev, sizeof(*rngdev), GFP_KERNEL); | 
|  | 116 | if (!rngdev) | 
|  | 117 | return -ENOMEM; | 
|  | 118 | if (!devm_request_mem_region(&dev->dev, r->start, resource_size(r), | 
|  | 119 | dev_name(&dev->dev))) | 
|  | 120 | return -EBUSY; | 
|  | 121 | rngdev->base = devm_ioremap(&dev->dev, r->start, resource_size(r)); | 
|  | 122 | if (!rngdev->base) | 
|  | 123 | return -EBUSY; | 
|  | 124 |  | 
|  | 125 | rngdev->rng.name = dev_name(&dev->dev); | 
|  | 126 | rngdev->rng.data_present = tx4939_rng_data_present; | 
|  | 127 | rngdev->rng.data_read = tx4939_rng_data_read; | 
|  | 128 |  | 
|  | 129 | rng_io_start(); | 
|  | 130 | /* Reset RNG */ | 
|  | 131 | write_rng(TX4939_RNG_RCSR_RST, rngdev->base, TX4939_RNG_RCSR); | 
|  | 132 | write_rng(0, rngdev->base, TX4939_RNG_RCSR); | 
|  | 133 | /* Start RNG */ | 
|  | 134 | write_rng(TX4939_RNG_RCSR_ST, rngdev->base, TX4939_RNG_RCSR); | 
|  | 135 | rng_io_end(); | 
|  | 136 | /* | 
|  | 137 | * Drop first two results.  From the datasheet: | 
|  | 138 | * The quality of the random numbers generated immediately | 
|  | 139 | * after reset can be insufficient.  Therefore, do not use | 
|  | 140 | * random numbers obtained from the first and second | 
|  | 141 | * generations; use the ones from the third or subsequent | 
|  | 142 | * generation. | 
|  | 143 | */ | 
|  | 144 | for (i = 0; i < 2; i++) { | 
|  | 145 | rngdev->data_avail = 0; | 
|  | 146 | if (!tx4939_rng_data_present(&rngdev->rng, 1)) | 
|  | 147 | return -EIO; | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | platform_set_drvdata(dev, rngdev); | 
|  | 151 | return hwrng_register(&rngdev->rng); | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | static int __exit tx4939_rng_remove(struct platform_device *dev) | 
|  | 155 | { | 
|  | 156 | struct tx4939_rng *rngdev = platform_get_drvdata(dev); | 
|  | 157 |  | 
|  | 158 | hwrng_unregister(&rngdev->rng); | 
|  | 159 | platform_set_drvdata(dev, NULL); | 
|  | 160 | return 0; | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | static struct platform_driver tx4939_rng_driver = { | 
|  | 164 | .driver		= { | 
|  | 165 | .name	= "tx4939-rng", | 
|  | 166 | .owner	= THIS_MODULE, | 
|  | 167 | }, | 
|  | 168 | .remove = tx4939_rng_remove, | 
|  | 169 | }; | 
|  | 170 |  | 
|  | 171 | static int __init tx4939rng_init(void) | 
|  | 172 | { | 
|  | 173 | return platform_driver_probe(&tx4939_rng_driver, tx4939_rng_probe); | 
|  | 174 | } | 
|  | 175 |  | 
|  | 176 | static void __exit tx4939rng_exit(void) | 
|  | 177 | { | 
|  | 178 | platform_driver_unregister(&tx4939_rng_driver); | 
|  | 179 | } | 
|  | 180 |  | 
|  | 181 | module_init(tx4939rng_init); | 
|  | 182 | module_exit(tx4939rng_exit); | 
|  | 183 |  | 
|  | 184 | MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver for TX4939"); | 
|  | 185 | MODULE_LICENSE("GPL"); |