.. | .. |
---|
14 | 14 | #include <linux/clk.h> |
---|
15 | 15 | #include <linux/io.h> |
---|
16 | 16 | #include <linux/hw_random.h> |
---|
| 17 | +#include <linux/of_device.h> |
---|
17 | 18 | #include <linux/platform_device.h> |
---|
18 | 19 | |
---|
19 | 20 | #define TRNG_CR 0x00 |
---|
| 21 | +#define TRNG_MR 0x04 |
---|
20 | 22 | #define TRNG_ISR 0x1c |
---|
21 | 23 | #define TRNG_ODATA 0x50 |
---|
22 | 24 | |
---|
23 | 25 | #define TRNG_KEY 0x524e4700 /* RNG */ |
---|
| 26 | + |
---|
| 27 | +#define TRNG_HALFR BIT(0) /* generate RN every 168 cycles */ |
---|
| 28 | + |
---|
| 29 | +struct atmel_trng_data { |
---|
| 30 | + bool has_half_rate; |
---|
| 31 | +}; |
---|
24 | 32 | |
---|
25 | 33 | struct atmel_trng { |
---|
26 | 34 | struct clk *clk; |
---|
.. | .. |
---|
62 | 70 | static int atmel_trng_probe(struct platform_device *pdev) |
---|
63 | 71 | { |
---|
64 | 72 | struct atmel_trng *trng; |
---|
65 | | - struct resource *res; |
---|
| 73 | + const struct atmel_trng_data *data; |
---|
66 | 74 | int ret; |
---|
67 | 75 | |
---|
68 | 76 | trng = devm_kzalloc(&pdev->dev, sizeof(*trng), GFP_KERNEL); |
---|
69 | 77 | if (!trng) |
---|
70 | 78 | return -ENOMEM; |
---|
71 | 79 | |
---|
72 | | - res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
---|
73 | | - trng->base = devm_ioremap_resource(&pdev->dev, res); |
---|
| 80 | + trng->base = devm_platform_ioremap_resource(pdev, 0); |
---|
74 | 81 | if (IS_ERR(trng->base)) |
---|
75 | 82 | return PTR_ERR(trng->base); |
---|
76 | 83 | |
---|
77 | 84 | trng->clk = devm_clk_get(&pdev->dev, NULL); |
---|
78 | 85 | if (IS_ERR(trng->clk)) |
---|
79 | 86 | return PTR_ERR(trng->clk); |
---|
| 87 | + data = of_device_get_match_data(&pdev->dev); |
---|
| 88 | + if (!data) |
---|
| 89 | + return -ENODEV; |
---|
| 90 | + |
---|
| 91 | + if (data->has_half_rate) { |
---|
| 92 | + unsigned long rate = clk_get_rate(trng->clk); |
---|
| 93 | + |
---|
| 94 | + /* if peripheral clk is above 100MHz, set HALFR */ |
---|
| 95 | + if (rate > 100000000) |
---|
| 96 | + writel(TRNG_HALFR, trng->base + TRNG_MR); |
---|
| 97 | + } |
---|
80 | 98 | |
---|
81 | 99 | ret = clk_prepare_enable(trng->clk); |
---|
82 | 100 | if (ret) |
---|
.. | .. |
---|
86 | 104 | trng->rng.name = pdev->name; |
---|
87 | 105 | trng->rng.read = atmel_trng_read; |
---|
88 | 106 | |
---|
89 | | - ret = hwrng_register(&trng->rng); |
---|
| 107 | + ret = devm_hwrng_register(&pdev->dev, &trng->rng); |
---|
90 | 108 | if (ret) |
---|
91 | 109 | goto err_register; |
---|
92 | 110 | |
---|
.. | .. |
---|
96 | 114 | |
---|
97 | 115 | err_register: |
---|
98 | 116 | clk_disable_unprepare(trng->clk); |
---|
| 117 | + atmel_trng_disable(trng); |
---|
99 | 118 | return ret; |
---|
100 | 119 | } |
---|
101 | 120 | |
---|
.. | .. |
---|
103 | 122 | { |
---|
104 | 123 | struct atmel_trng *trng = platform_get_drvdata(pdev); |
---|
105 | 124 | |
---|
106 | | - hwrng_unregister(&trng->rng); |
---|
107 | 125 | |
---|
108 | 126 | atmel_trng_disable(trng); |
---|
109 | 127 | clk_disable_unprepare(trng->clk); |
---|
.. | .. |
---|
142 | 160 | }; |
---|
143 | 161 | #endif /* CONFIG_PM */ |
---|
144 | 162 | |
---|
| 163 | +static const struct atmel_trng_data at91sam9g45_config = { |
---|
| 164 | + .has_half_rate = false, |
---|
| 165 | +}; |
---|
| 166 | + |
---|
| 167 | +static const struct atmel_trng_data sam9x60_config = { |
---|
| 168 | + .has_half_rate = true, |
---|
| 169 | +}; |
---|
| 170 | + |
---|
145 | 171 | static const struct of_device_id atmel_trng_dt_ids[] = { |
---|
146 | | - { .compatible = "atmel,at91sam9g45-trng" }, |
---|
147 | | - { /* sentinel */ } |
---|
| 172 | + { |
---|
| 173 | + .compatible = "atmel,at91sam9g45-trng", |
---|
| 174 | + .data = &at91sam9g45_config, |
---|
| 175 | + }, { |
---|
| 176 | + .compatible = "microchip,sam9x60-trng", |
---|
| 177 | + .data = &sam9x60_config, |
---|
| 178 | + }, { |
---|
| 179 | + /* sentinel */ |
---|
| 180 | + } |
---|
148 | 181 | }; |
---|
149 | 182 | MODULE_DEVICE_TABLE(of, atmel_trng_dt_ids); |
---|
150 | 183 | |
---|