-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patha10_crypto.c
268 lines (220 loc) · 6.5 KB
/
a10_crypto.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*-
* Copyright (c) 2016 Ganbold Tsagaankhuu <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Allwinner A10/A20 Security System/Crypto accelerator
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/endian.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/rman.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/sysctl.h>
#include <machine/bus.h>
#include <sys/random.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <dev/extres/clk/clk.h>
#include <dev/extres/hwreset/hwreset.h>
#include <arm/allwinner/a10_crypto.h>
static struct ofw_compat_data compat_data[] = {
{ "allwinner,sun4i-a10-crypto", 1 },
{ NULL, 0 }
};
struct a10_crypto_softc {
device_t dev;
struct resource *res;
struct callout co;
int ticks;
uint32_t sc_seed[A10_SS_FIFO_WORDS];
uint32_t sc_buf[A10_SS_FIFO_WORDS];
};
static struct resource_spec a10_crypto_spec[] = {
{ SYS_RES_MEMORY, 0, RF_ACTIVE },
{ -1, 0 }
};
#define RD4(sc, reg) bus_read_4((sc)->res, (reg))
#define WR4(sc, reg, val) bus_write_4((sc)->res, (reg), (val))
static void
a10_rng_start(struct a10_crypto_softc *sc)
{
uint32_t prng_mode;
int i;
prng_mode = A10_SS_OP_PRNG | A10_SS_PRNG_CONTINUE | A10_SS_ENABLED;
/* Write PRNG mode */
WR4(sc, A10_SS_CTL, prng_mode);
/* Write the seed */
for (i = 0; i < A10_SS_FIFO_WORDS; i++)
WR4(sc, A10_SS_KEY0 + i * 4, sc->sc_seed[i]);
/* Start PRNG */
WR4(sc, A10_SS_CTL, prng_mode | A10_SS_PRNG_START);
}
static void
a10_rng_stop(struct a10_crypto_softc *sc)
{
uint32_t ctrl;
/* Disable PRNG */
ctrl = RD4(sc, A10_SS_CTL);
ctrl &= ~A10_SS_PRNG_START;
WR4(sc, A10_SS_CTL, ctrl);
/* Disable and flush FIFO */
WR4(sc, A10_SS_CTL, A10_SS_DISABLED);
}
static void
a10_rng_harvest(void *arg)
{
struct a10_crypto_softc *sc = arg;
uint32_t val;
int i;
/* Start PRNG */
a10_rng_start(sc);
/* Read random data */
bus_read_multi_4(sc->res, A10_SS_TXFIFO, sc->sc_buf,
A10_SS_DATA_LEN / sizeof(uint32_t));
/* Update the seed */
for (i = 0; i < A10_SS_FIFO_WORDS; i++) {
val = RD4(sc, A10_SS_KEY0 + i * 4);
sc->sc_seed[i] = val;
}
/* Disable and flush FIFO */
WR4(sc, A10_SS_CTL, A10_SS_DISABLED);
random_harvest_queue(sc->sc_buf, sizeof(sc->sc_buf),
sizeof(sc->sc_buf) * NBBY / 2, RANDOM_PURE_ALLWINNER);
callout_reset(&sc->co, sc->ticks, a10_rng_harvest, sc);
}
static int
a10_crypto_probe(device_t dev)
{
if (!ofw_bus_status_okay(dev))
return (ENXIO);
if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
return (ENXIO);
device_set_desc(dev, "A10/A20 Security System/Crypto accelerator");
return (BUS_PROBE_DEFAULT);
}
static int
a10_crypto_attach(device_t dev)
{
struct a10_crypto_softc *sc;
hwreset_t rst_ahb;
clk_t clk_ss, clk_gate;
int err, i;
uint32_t val = 0;
sc = device_get_softc(dev);
clk_ss = clk_gate = NULL;
rst_ahb = NULL;
if (bus_alloc_resources(dev, a10_crypto_spec, &sc->res) != 0) {
device_printf(dev, "cannot allocate resources for device\n");
return (ENXIO);
}
/* De-assert reset */
if (hwreset_get_by_ofw_name(dev, 0, "ahb", &rst_ahb) == 0) {
err = hwreset_deassert(rst_ahb);
if (err != 0) {
device_printf(dev, "cannot de-assert reset\n");
goto error;
}
}
/* Get clocks and enable them */
err = clk_get_by_ofw_name(dev, 0, "ahb", &clk_gate);
if (err != 0) {
device_printf(dev, "Cannot get gate clock\n");
goto error;
}
err = clk_get_by_ofw_name(dev, 0, "mod", &clk_ss);
if (err != 0) {
device_printf(dev, "Cannot get SS clock\n");
goto error;
}
err = clk_enable(clk_gate);
if (err != 0) {
device_printf(dev, "Cannot enable clk gate\n");
goto error;
}
err = clk_enable(clk_ss);
if (err != 0) {
device_printf(dev, "Cannot enable SS clock\n");
goto error;
}
/*
* "Die Bonding ID"
*/
WR4(sc, A10_SS_CTL, A10_SS_ENABLED);
val = RD4(sc, A10_SS_CTL);
val >>= DIE_ID_START_BIT;
val &= DIE_ID_MASK;
device_printf(dev, "Die ID %d\n", val);
WR4(sc, A10_SS_CTL, 0);
/* Install a periodic collector for the RNG */
if (hz > 100)
sc->ticks = hz / 100;
else
sc->ticks = 1;
/* Generate the seed fist */
for (i = 0; i < A10_SS_FIFO_WORDS; i++)
sc->sc_seed[i] = arc4random();
callout_init(&sc->co, 1);
callout_reset(&sc->co, sc->ticks, a10_rng_harvest, sc);
return (0);
error:
if (clk_gate != NULL)
clk_release(clk_gate);
if (clk_ss != NULL)
clk_release(clk_ss);
if (rst_ahb != NULL)
hwreset_release(rst_ahb);
bus_release_resources(dev, a10_crypto_spec, &sc->res);
return (ENXIO);
}
static int
a10_crypto_detach(device_t dev)
{
struct a10_crypto_softc *sc = device_get_softc(dev);
a10_rng_stop(sc);
callout_drain(&sc->co);
bus_release_resources(dev, a10_crypto_spec, &sc->res);
return (0);
}
static device_method_t a10_crypto_methods[] = {
/* Device interface */
DEVMETHOD(device_probe, a10_crypto_probe),
DEVMETHOD(device_attach, a10_crypto_attach),
DEVMETHOD(device_detach, a10_crypto_detach),
DEVMETHOD_END
};
static driver_t a10_crypto_driver = {
"a10_crypto",
a10_crypto_methods,
sizeof(struct a10_crypto_softc),
};
static devclass_t a10_crypto_devclass;
DRIVER_MODULE(a10_crypto, simplebus, a10_crypto_driver, a10_crypto_devclass, 0, 0);
MODULE_VERSION(a10_crypto, 1);
MODULE_DEPEND(a10_crypto, crypto, 1, 1, 1);
MODULE_DEPEND(a10_crypto, randomdev, 1, 1, 1);