Peter Korsgaard | 677d3e2 | 2011-10-06 19:56:30 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 Peter Korsgaard <jacmet@sunsite.dk> |
| 3 | * |
| 4 | * This file is licensed under the terms of the GNU General Public |
| 5 | * License version 2. This program is licensed "as is" without any |
| 6 | * warranty of any kind, whether express or implied. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/err.h> |
| 13 | #include <linux/clk.h> |
| 14 | #include <linux/io.h> |
| 15 | #include <linux/hw_random.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | |
| 18 | #define TRNG_CR 0x00 |
| 19 | #define TRNG_ISR 0x1c |
| 20 | #define TRNG_ODATA 0x50 |
| 21 | |
| 22 | #define TRNG_KEY 0x524e4700 /* RNG */ |
| 23 | |
| 24 | struct atmel_trng { |
| 25 | struct clk *clk; |
| 26 | void __iomem *base; |
| 27 | struct hwrng rng; |
| 28 | }; |
| 29 | |
| 30 | static int atmel_trng_read(struct hwrng *rng, void *buf, size_t max, |
| 31 | bool wait) |
| 32 | { |
| 33 | struct atmel_trng *trng = container_of(rng, struct atmel_trng, rng); |
| 34 | u32 *data = buf; |
| 35 | |
| 36 | /* data ready? */ |
Peter Korsgaard | c475c06 | 2012-06-12 08:27:04 +0800 | [diff] [blame] | 37 | if (readl(trng->base + TRNG_ISR) & 1) { |
Peter Korsgaard | 677d3e2 | 2011-10-06 19:56:30 +0200 | [diff] [blame] | 38 | *data = readl(trng->base + TRNG_ODATA); |
Peter Korsgaard | 121daad | 2012-05-31 20:53:08 +1000 | [diff] [blame] | 39 | /* |
| 40 | ensure data ready is only set again AFTER the next data |
| 41 | word is ready in case it got set between checking ISR |
| 42 | and reading ODATA, so we don't risk re-reading the |
| 43 | same word |
| 44 | */ |
| 45 | readl(trng->base + TRNG_ISR); |
Peter Korsgaard | 677d3e2 | 2011-10-06 19:56:30 +0200 | [diff] [blame] | 46 | return 4; |
| 47 | } else |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static int atmel_trng_probe(struct platform_device *pdev) |
| 52 | { |
| 53 | struct atmel_trng *trng; |
| 54 | struct resource *res; |
| 55 | int ret; |
| 56 | |
Peter Korsgaard | 677d3e2 | 2011-10-06 19:56:30 +0200 | [diff] [blame] | 57 | trng = devm_kzalloc(&pdev->dev, sizeof(*trng), GFP_KERNEL); |
| 58 | if (!trng) |
| 59 | return -ENOMEM; |
| 60 | |
Jingoo Han | bfaff75 | 2014-02-12 14:17:08 +0900 | [diff] [blame] | 61 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 62 | trng->base = devm_ioremap_resource(&pdev->dev, res); |
| 63 | if (IS_ERR(trng->base)) |
| 64 | return PTR_ERR(trng->base); |
Peter Korsgaard | 677d3e2 | 2011-10-06 19:56:30 +0200 | [diff] [blame] | 65 | |
Jingoo Han | 0c0becd | 2014-02-27 14:00:09 +0900 | [diff] [blame] | 66 | trng->clk = devm_clk_get(&pdev->dev, NULL); |
Peter Korsgaard | 677d3e2 | 2011-10-06 19:56:30 +0200 | [diff] [blame] | 67 | if (IS_ERR(trng->clk)) |
| 68 | return PTR_ERR(trng->clk); |
| 69 | |
| 70 | ret = clk_enable(trng->clk); |
| 71 | if (ret) |
Jingoo Han | 0c0becd | 2014-02-27 14:00:09 +0900 | [diff] [blame] | 72 | return ret; |
Peter Korsgaard | 677d3e2 | 2011-10-06 19:56:30 +0200 | [diff] [blame] | 73 | |
| 74 | writel(TRNG_KEY | 1, trng->base + TRNG_CR); |
| 75 | trng->rng.name = pdev->name; |
| 76 | trng->rng.read = atmel_trng_read; |
| 77 | |
| 78 | ret = hwrng_register(&trng->rng); |
| 79 | if (ret) |
| 80 | goto err_register; |
| 81 | |
| 82 | platform_set_drvdata(pdev, trng); |
| 83 | |
| 84 | return 0; |
| 85 | |
| 86 | err_register: |
| 87 | clk_disable(trng->clk); |
Peter Korsgaard | 677d3e2 | 2011-10-06 19:56:30 +0200 | [diff] [blame] | 88 | return ret; |
| 89 | } |
| 90 | |
Bill Pemberton | 39af33f | 2012-11-19 13:26:26 -0500 | [diff] [blame] | 91 | static int atmel_trng_remove(struct platform_device *pdev) |
Peter Korsgaard | 677d3e2 | 2011-10-06 19:56:30 +0200 | [diff] [blame] | 92 | { |
| 93 | struct atmel_trng *trng = platform_get_drvdata(pdev); |
| 94 | |
| 95 | hwrng_unregister(&trng->rng); |
| 96 | |
| 97 | writel(TRNG_KEY, trng->base + TRNG_CR); |
| 98 | clk_disable(trng->clk); |
Peter Korsgaard | 677d3e2 | 2011-10-06 19:56:30 +0200 | [diff] [blame] | 99 | |
Peter Korsgaard | 677d3e2 | 2011-10-06 19:56:30 +0200 | [diff] [blame] | 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | #ifdef CONFIG_PM |
| 104 | static int atmel_trng_suspend(struct device *dev) |
| 105 | { |
| 106 | struct atmel_trng *trng = dev_get_drvdata(dev); |
| 107 | |
| 108 | clk_disable(trng->clk); |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int atmel_trng_resume(struct device *dev) |
| 114 | { |
| 115 | struct atmel_trng *trng = dev_get_drvdata(dev); |
| 116 | |
| 117 | return clk_enable(trng->clk); |
| 118 | } |
| 119 | |
| 120 | static const struct dev_pm_ops atmel_trng_pm_ops = { |
| 121 | .suspend = atmel_trng_suspend, |
| 122 | .resume = atmel_trng_resume, |
| 123 | }; |
| 124 | #endif /* CONFIG_PM */ |
| 125 | |
| 126 | static struct platform_driver atmel_trng_driver = { |
| 127 | .probe = atmel_trng_probe, |
Greg Kroah-Hartman | bcd2982 | 2012-12-21 15:12:08 -0800 | [diff] [blame] | 128 | .remove = atmel_trng_remove, |
Peter Korsgaard | 677d3e2 | 2011-10-06 19:56:30 +0200 | [diff] [blame] | 129 | .driver = { |
| 130 | .name = "atmel-trng", |
| 131 | .owner = THIS_MODULE, |
| 132 | #ifdef CONFIG_PM |
| 133 | .pm = &atmel_trng_pm_ops, |
| 134 | #endif /* CONFIG_PM */ |
| 135 | }, |
| 136 | }; |
| 137 | |
Axel Lin | b21cb32 | 2011-11-26 21:11:06 +0800 | [diff] [blame] | 138 | module_platform_driver(atmel_trng_driver); |
Peter Korsgaard | 677d3e2 | 2011-10-06 19:56:30 +0200 | [diff] [blame] | 139 | |
| 140 | MODULE_LICENSE("GPL"); |
| 141 | MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>"); |
| 142 | MODULE_DESCRIPTION("Atmel true random number generator driver"); |