2 * nRF51 Random Number Generator
4 * Reference Manual: http://infocenter.nordicsemi.com/pdf/nRF51_RM_v3.0.1.pdf
6 * Copyright 2018 Steffen Görtz <contrib@steffen-goertz.de>
8 * This code is licensed under the GPL version 2 or later. See
9 * the COPYING file in the top-level directory.
12 #include "qemu/osdep.h"
14 #include "qemu/module.h"
15 #include "qapi/error.h"
16 #include "hw/arm/nrf51.h"
18 #include "hw/misc/nrf51_rng.h"
19 #include "hw/qdev-properties.h"
20 #include "migration/vmstate.h"
21 #include "qemu/guest-random.h"
23 static void update_irq(NRF51RNGState
*s
)
25 bool irq
= s
->interrupt_enabled
&& s
->event_valrdy
;
26 qemu_set_irq(s
->irq
, irq
);
29 static uint64_t rng_read(void *opaque
, hwaddr offset
, unsigned int size
)
31 NRF51RNGState
*s
= NRF51_RNG(opaque
);
35 case NRF51_RNG_EVENT_VALRDY
:
38 case NRF51_RNG_REG_SHORTS
:
39 r
= s
->shortcut_stop_on_valrdy
;
41 case NRF51_RNG_REG_INTEN
:
42 case NRF51_RNG_REG_INTENSET
:
43 case NRF51_RNG_REG_INTENCLR
:
44 r
= s
->interrupt_enabled
;
46 case NRF51_RNG_REG_CONFIG
:
47 r
= s
->filter_enabled
;
49 case NRF51_RNG_REG_VALUE
:
54 qemu_log_mask(LOG_GUEST_ERROR
,
55 "%s: bad read offset 0x%" HWADDR_PRIx
"\n",
62 static int64_t calc_next_timeout(NRF51RNGState
*s
)
64 int64_t timeout
= qemu_clock_get_us(QEMU_CLOCK_VIRTUAL
);
65 if (s
->filter_enabled
) {
66 timeout
+= s
->period_filtered_us
;
68 timeout
+= s
->period_unfiltered_us
;
75 static void rng_update_timer(NRF51RNGState
*s
)
78 timer_mod(&s
->timer
, calc_next_timeout(s
));
85 static void rng_write(void *opaque
, hwaddr offset
,
86 uint64_t value
, unsigned int size
)
88 NRF51RNGState
*s
= NRF51_RNG(opaque
);
91 case NRF51_RNG_TASK_START
:
92 if (value
== NRF51_TRIGGER_TASK
) {
97 case NRF51_RNG_TASK_STOP
:
98 if (value
== NRF51_TRIGGER_TASK
) {
103 case NRF51_RNG_EVENT_VALRDY
:
104 if (value
== NRF51_EVENT_CLEAR
) {
108 case NRF51_RNG_REG_SHORTS
:
109 s
->shortcut_stop_on_valrdy
=
110 (value
& BIT_MASK(NRF51_RNG_REG_SHORTS_VALRDY_STOP
)) ? 1 : 0;
112 case NRF51_RNG_REG_INTEN
:
113 s
->interrupt_enabled
=
114 (value
& BIT_MASK(NRF51_RNG_REG_INTEN_VALRDY
)) ? 1 : 0;
116 case NRF51_RNG_REG_INTENSET
:
117 if (value
& BIT_MASK(NRF51_RNG_REG_INTEN_VALRDY
)) {
118 s
->interrupt_enabled
= 1;
121 case NRF51_RNG_REG_INTENCLR
:
122 if (value
& BIT_MASK(NRF51_RNG_REG_INTEN_VALRDY
)) {
123 s
->interrupt_enabled
= 0;
126 case NRF51_RNG_REG_CONFIG
:
128 (value
& BIT_MASK(NRF51_RNG_REG_CONFIG_DECEN
)) ? 1 : 0;
132 qemu_log_mask(LOG_GUEST_ERROR
,
133 "%s: bad write offset 0x%" HWADDR_PRIx
"\n",
140 static const MemoryRegionOps rng_ops
= {
143 .endianness
= DEVICE_LITTLE_ENDIAN
,
144 .impl
.min_access_size
= 4,
145 .impl
.max_access_size
= 4
148 static void nrf51_rng_timer_expire(void *opaque
)
150 NRF51RNGState
*s
= NRF51_RNG(opaque
);
152 qemu_guest_getrandom_nofail(&s
->value
, 1);
155 qemu_set_irq(s
->eep_valrdy
, 1);
157 if (s
->shortcut_stop_on_valrdy
) {
165 static void nrf51_rng_tep_start(void *opaque
, int n
, int level
)
167 NRF51RNGState
*s
= NRF51_RNG(opaque
);
175 static void nrf51_rng_tep_stop(void *opaque
, int n
, int level
)
177 NRF51RNGState
*s
= NRF51_RNG(opaque
);
186 static void nrf51_rng_init(Object
*obj
)
188 NRF51RNGState
*s
= NRF51_RNG(obj
);
189 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
191 memory_region_init_io(&s
->mmio
, obj
, &rng_ops
, s
,
192 TYPE_NRF51_RNG
, NRF51_RNG_SIZE
);
193 sysbus_init_mmio(sbd
, &s
->mmio
);
195 timer_init_us(&s
->timer
, QEMU_CLOCK_VIRTUAL
, nrf51_rng_timer_expire
, s
);
197 sysbus_init_irq(sbd
, &s
->irq
);
200 qdev_init_gpio_in_named(DEVICE(s
), nrf51_rng_tep_start
, "tep_start", 1);
201 qdev_init_gpio_in_named(DEVICE(s
), nrf51_rng_tep_stop
, "tep_stop", 1);
204 qdev_init_gpio_out_named(DEVICE(s
), &s
->eep_valrdy
, "eep_valrdy", 1);
207 static void nrf51_rng_reset(DeviceState
*dev
)
209 NRF51RNGState
*s
= NRF51_RNG(dev
);
214 s
->shortcut_stop_on_valrdy
= 0;
215 s
->interrupt_enabled
= 0;
216 s
->filter_enabled
= 0;
222 static Property nrf51_rng_properties
[] = {
223 DEFINE_PROP_UINT16("period_unfiltered_us", NRF51RNGState
,
224 period_unfiltered_us
, 167),
225 DEFINE_PROP_UINT16("period_filtered_us", NRF51RNGState
,
226 period_filtered_us
, 660),
227 DEFINE_PROP_END_OF_LIST(),
230 static const VMStateDescription vmstate_rng
= {
231 .name
= "nrf51_soc.rng",
233 .minimum_version_id
= 1,
234 .fields
= (VMStateField
[]) {
235 VMSTATE_UINT32(active
, NRF51RNGState
),
236 VMSTATE_UINT32(event_valrdy
, NRF51RNGState
),
237 VMSTATE_UINT32(shortcut_stop_on_valrdy
, NRF51RNGState
),
238 VMSTATE_UINT32(interrupt_enabled
, NRF51RNGState
),
239 VMSTATE_UINT32(filter_enabled
, NRF51RNGState
),
240 VMSTATE_END_OF_LIST()
244 static void nrf51_rng_class_init(ObjectClass
*klass
, void *data
)
246 DeviceClass
*dc
= DEVICE_CLASS(klass
);
248 dc
->props
= nrf51_rng_properties
;
249 dc
->vmsd
= &vmstate_rng
;
250 dc
->reset
= nrf51_rng_reset
;
253 static const TypeInfo nrf51_rng_info
= {
254 .name
= TYPE_NRF51_RNG
,
255 .parent
= TYPE_SYS_BUS_DEVICE
,
256 .instance_size
= sizeof(NRF51RNGState
),
257 .instance_init
= nrf51_rng_init
,
258 .class_init
= nrf51_rng_class_init
261 static void nrf51_rng_register_types(void)
263 type_register_static(&nrf51_rng_info
);
266 type_init(nrf51_rng_register_types
)