Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 1 | /* |
| 2 | * drivers/char/hw_random/timeriomem-rng.c |
| 3 | * |
| 4 | * Copyright (C) 2009 Alexander Clouter <alex@digriz.org.uk> |
| 5 | * |
| 6 | * Derived from drivers/char/hw_random/omap-rng.c |
| 7 | * Copyright 2005 (c) MontaVista Software, Inc. |
| 8 | * Author: Deepak Saxena <dsaxena@plexity.net> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | * |
| 14 | * Overview: |
| 15 | * This driver is useful for platforms that have an IO range that provides |
| 16 | * periodic random data from a single IO memory address. All the platform |
| 17 | * has to do is provide the address and 'wait time' that new data becomes |
| 18 | * available. |
| 19 | * |
| 20 | * TODO: add support for reading sizes other than 32bits and masking |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/platform_device.h> |
Alexander Clouter | b149a30 | 2013-03-31 17:34:51 +0100 | [diff] [blame] | 26 | #include <linux/of.h> |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 27 | #include <linux/hw_random.h> |
| 28 | #include <linux/io.h> |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 29 | #include <linux/slab.h> |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 30 | #include <linux/timeriomem-rng.h> |
| 31 | #include <linux/jiffies.h> |
| 32 | #include <linux/sched.h> |
| 33 | #include <linux/timer.h> |
| 34 | #include <linux/completion.h> |
| 35 | |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 36 | struct timeriomem_rng_private_data { |
| 37 | void __iomem *io_base; |
| 38 | unsigned int expires; |
| 39 | unsigned int period; |
| 40 | unsigned int present:1; |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 41 | |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 42 | struct timer_list timer; |
| 43 | struct completion completion; |
| 44 | |
| 45 | struct hwrng timeriomem_rng_ops; |
| 46 | }; |
| 47 | |
| 48 | #define to_rng_priv(rng) \ |
| 49 | ((struct timeriomem_rng_private_data *)rng->priv) |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 50 | |
| 51 | /* |
| 52 | * have data return 1, however return 0 if we have nothing |
| 53 | */ |
| 54 | static int timeriomem_rng_data_present(struct hwrng *rng, int wait) |
| 55 | { |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 56 | struct timeriomem_rng_private_data *priv = to_rng_priv(rng); |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 57 | |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 58 | if (!wait || priv->present) |
| 59 | return priv->present; |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 60 | |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 61 | wait_for_completion(&priv->completion); |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 62 | |
| 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | static int timeriomem_rng_data_read(struct hwrng *rng, u32 *data) |
| 67 | { |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 68 | struct timeriomem_rng_private_data *priv = to_rng_priv(rng); |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 69 | unsigned long cur; |
| 70 | s32 delay; |
| 71 | |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 72 | *data = readl(priv->io_base); |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 73 | |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 74 | cur = jiffies; |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 75 | |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 76 | delay = cur - priv->expires; |
| 77 | delay = priv->period - (delay % priv->period); |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 78 | |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 79 | priv->expires = cur + delay; |
| 80 | priv->present = 0; |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 81 | |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 82 | reinit_completion(&priv->completion); |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 83 | mod_timer(&priv->timer, priv->expires); |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 84 | |
| 85 | return 4; |
| 86 | } |
| 87 | |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 88 | static void timeriomem_rng_trigger(unsigned long data) |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 89 | { |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 90 | struct timeriomem_rng_private_data *priv |
| 91 | = (struct timeriomem_rng_private_data *)data; |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 92 | |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 93 | priv->present = 1; |
| 94 | complete(&priv->completion); |
| 95 | } |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 96 | |
Greg Kroah-Hartman | bcd2982 | 2012-12-21 15:12:08 -0800 | [diff] [blame] | 97 | static int timeriomem_rng_probe(struct platform_device *pdev) |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 98 | { |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 99 | struct timeriomem_rng_data *pdata = pdev->dev.platform_data; |
| 100 | struct timeriomem_rng_private_data *priv; |
Alexander Clouter | 08ced85 | 2009-06-03 19:28:03 +1000 | [diff] [blame] | 101 | struct resource *res; |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 102 | int err = 0; |
| 103 | int period; |
| 104 | |
Alexander Clouter | b149a30 | 2013-03-31 17:34:51 +0100 | [diff] [blame] | 105 | if (!pdev->dev.of_node && !pdata) { |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 106 | dev_err(&pdev->dev, "timeriomem_rng_data is missing\n"); |
| 107 | return -EINVAL; |
| 108 | } |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 109 | |
Alexander Clouter | 3341323 | 2009-03-27 12:59:54 +0800 | [diff] [blame] | 110 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Alexander Clouter | 3341323 | 2009-03-27 12:59:54 +0800 | [diff] [blame] | 111 | if (!res) |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 112 | return -ENXIO; |
Alexander Clouter | 3341323 | 2009-03-27 12:59:54 +0800 | [diff] [blame] | 113 | |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 114 | if (res->start % 4 != 0 || resource_size(res) != 4) { |
| 115 | dev_err(&pdev->dev, |
| 116 | "address must be four bytes wide and aligned\n"); |
| 117 | return -EINVAL; |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 118 | } |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 119 | |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 120 | /* Allocate memory for the device structure (and zero it) */ |
Jingoo Han | 93b7f9c | 2014-02-27 14:07:52 +0900 | [diff] [blame] | 121 | priv = devm_kzalloc(&pdev->dev, |
| 122 | sizeof(struct timeriomem_rng_private_data), GFP_KERNEL); |
Jingoo Han | 7bad94a | 2014-04-29 17:16:42 +0900 | [diff] [blame] | 123 | if (!priv) |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 124 | return -ENOMEM; |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 125 | |
| 126 | platform_set_drvdata(pdev, priv); |
| 127 | |
Alexander Clouter | b149a30 | 2013-03-31 17:34:51 +0100 | [diff] [blame] | 128 | if (pdev->dev.of_node) { |
| 129 | int i; |
| 130 | |
| 131 | if (!of_property_read_u32(pdev->dev.of_node, |
| 132 | "period", &i)) |
| 133 | period = i; |
| 134 | else { |
| 135 | dev_err(&pdev->dev, "missing period\n"); |
Jingoo Han | 93b7f9c | 2014-02-27 14:07:52 +0900 | [diff] [blame] | 136 | return -EINVAL; |
Alexander Clouter | b149a30 | 2013-03-31 17:34:51 +0100 | [diff] [blame] | 137 | } |
Jingoo Han | 93b7f9c | 2014-02-27 14:07:52 +0900 | [diff] [blame] | 138 | } else { |
Alexander Clouter | b149a30 | 2013-03-31 17:34:51 +0100 | [diff] [blame] | 139 | period = pdata->period; |
Jingoo Han | 93b7f9c | 2014-02-27 14:07:52 +0900 | [diff] [blame] | 140 | } |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 141 | |
| 142 | priv->period = usecs_to_jiffies(period); |
| 143 | if (priv->period < 1) { |
| 144 | dev_err(&pdev->dev, "period is less than one jiffy\n"); |
Jingoo Han | 93b7f9c | 2014-02-27 14:07:52 +0900 | [diff] [blame] | 145 | return -EINVAL; |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | priv->expires = jiffies; |
| 149 | priv->present = 1; |
| 150 | |
| 151 | init_completion(&priv->completion); |
| 152 | complete(&priv->completion); |
| 153 | |
| 154 | setup_timer(&priv->timer, timeriomem_rng_trigger, (unsigned long)priv); |
| 155 | |
| 156 | priv->timeriomem_rng_ops.name = dev_name(&pdev->dev); |
| 157 | priv->timeriomem_rng_ops.data_present = timeriomem_rng_data_present; |
| 158 | priv->timeriomem_rng_ops.data_read = timeriomem_rng_data_read; |
| 159 | priv->timeriomem_rng_ops.priv = (unsigned long)priv; |
| 160 | |
Jingoo Han | 93b7f9c | 2014-02-27 14:07:52 +0900 | [diff] [blame] | 161 | priv->io_base = devm_ioremap_resource(&pdev->dev, res); |
| 162 | if (IS_ERR(priv->io_base)) { |
| 163 | err = PTR_ERR(priv->io_base); |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 164 | goto out_timer; |
| 165 | } |
| 166 | |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 167 | err = hwrng_register(&priv->timeriomem_rng_ops); |
| 168 | if (err) { |
| 169 | dev_err(&pdev->dev, "problem registering\n"); |
Jingoo Han | 93b7f9c | 2014-02-27 14:07:52 +0900 | [diff] [blame] | 170 | goto out_timer; |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 171 | } |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 172 | |
| 173 | dev_info(&pdev->dev, "32bits from 0x%p @ %dus\n", |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 174 | priv->io_base, period); |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 175 | |
| 176 | return 0; |
Alexander Clouter | 3341323 | 2009-03-27 12:59:54 +0800 | [diff] [blame] | 177 | |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 178 | out_timer: |
| 179 | del_timer_sync(&priv->timer); |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 180 | return err; |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 181 | } |
| 182 | |
Bill Pemberton | 39af33f | 2012-11-19 13:26:26 -0500 | [diff] [blame] | 183 | static int timeriomem_rng_remove(struct platform_device *pdev) |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 184 | { |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 185 | struct timeriomem_rng_private_data *priv = platform_get_drvdata(pdev); |
Alexander Clouter | 1907da7 | 2013-03-31 17:34:50 +0100 | [diff] [blame] | 186 | |
| 187 | hwrng_unregister(&priv->timeriomem_rng_ops); |
| 188 | |
| 189 | del_timer_sync(&priv->timer); |
Alexander Clouter | 3341323 | 2009-03-27 12:59:54 +0800 | [diff] [blame] | 190 | |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 191 | return 0; |
| 192 | } |
| 193 | |
Alexander Clouter | b149a30 | 2013-03-31 17:34:51 +0100 | [diff] [blame] | 194 | static const struct of_device_id timeriomem_rng_match[] = { |
| 195 | { .compatible = "timeriomem_rng" }, |
| 196 | {}, |
| 197 | }; |
| 198 | MODULE_DEVICE_TABLE(of, timeriomem_rng_match); |
| 199 | |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 200 | static struct platform_driver timeriomem_rng_driver = { |
| 201 | .driver = { |
| 202 | .name = "timeriomem_rng", |
Alexander Clouter | b149a30 | 2013-03-31 17:34:51 +0100 | [diff] [blame] | 203 | .of_match_table = timeriomem_rng_match, |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 204 | }, |
| 205 | .probe = timeriomem_rng_probe, |
Greg Kroah-Hartman | bcd2982 | 2012-12-21 15:12:08 -0800 | [diff] [blame] | 206 | .remove = timeriomem_rng_remove, |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 207 | }; |
| 208 | |
Axel Lin | b21cb32 | 2011-11-26 21:11:06 +0800 | [diff] [blame] | 209 | module_platform_driver(timeriomem_rng_driver); |
Alexander Clouter | 9c3c133 | 2009-02-22 12:03:56 +0800 | [diff] [blame] | 210 | |
| 211 | MODULE_LICENSE("GPL"); |
| 212 | MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>"); |
| 213 | MODULE_DESCRIPTION("Timer IOMEM H/W RNG driver"); |