2 * nRF51 System-on-Chip Timer peripheral
4 * Reference Manual: http://infocenter.nordicsemi.com/pdf/nRF51_RM_v3.0.pdf
5 * Product Spec: http://infocenter.nordicsemi.com/pdf/nRF51822_PS_v3.1.pdf
7 * Copyright 2018 Steffen Görtz <contrib@steffen-goertz.de>
8 * Copyright (c) 2019 Red Hat, Inc.
10 * This code is licensed under the GPL version 2 or later. See
11 * the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
16 #include "qemu/module.h"
17 #include "hw/arm/nrf51.h"
19 #include "hw/timer/nrf51_timer.h"
20 #include "migration/vmstate.h"
23 #define TIMER_CLK_FREQ 16000000UL
25 static uint32_t const bitwidths
[] = {16, 8, 24, 32};
27 static uint32_t ns_to_ticks(NRF51TimerState
*s
, int64_t ns
)
29 uint32_t freq
= TIMER_CLK_FREQ
>> s
->prescaler
;
31 return muldiv64(ns
, freq
, NANOSECONDS_PER_SECOND
);
34 static int64_t ticks_to_ns(NRF51TimerState
*s
, uint32_t ticks
)
36 uint32_t freq
= TIMER_CLK_FREQ
>> s
->prescaler
;
38 return muldiv64(ticks
, NANOSECONDS_PER_SECOND
, freq
);
41 /* Returns number of ticks since last call */
42 static uint32_t update_counter(NRF51TimerState
*s
, int64_t now
)
44 uint32_t ticks
= ns_to_ticks(s
, now
- s
->update_counter_ns
);
46 s
->counter
= (s
->counter
+ ticks
) % BIT(bitwidths
[s
->bitmode
]);
47 s
->update_counter_ns
= now
;
51 /* Assumes s->counter is up-to-date */
52 static void rearm_timer(NRF51TimerState
*s
, int64_t now
)
54 int64_t min_ns
= INT64_MAX
;
57 for (i
= 0; i
< NRF51_TIMER_REG_COUNT
; i
++) {
60 if (s
->events_compare
[i
]) {
61 continue; /* already expired, ignore it for now */
64 if (s
->cc
[i
] <= s
->counter
) {
65 delta_ns
= ticks_to_ns(s
, BIT(bitwidths
[s
->bitmode
]) -
66 s
->counter
+ s
->cc
[i
]);
68 delta_ns
= ticks_to_ns(s
, s
->cc
[i
] - s
->counter
);
71 if (delta_ns
< min_ns
) {
76 if (min_ns
!= INT64_MAX
) {
77 timer_mod_ns(&s
->timer
, now
+ min_ns
);
81 static void update_irq(NRF51TimerState
*s
)
86 for (i
= 0; i
< NRF51_TIMER_REG_COUNT
; i
++) {
87 flag
|= s
->events_compare
[i
] && extract32(s
->inten
, 16 + i
, 1);
89 qemu_set_irq(s
->irq
, flag
);
92 static void timer_expire(void *opaque
)
94 NRF51TimerState
*s
= NRF51_TIMER(opaque
);
95 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
96 uint32_t cc_remaining
[NRF51_TIMER_REG_COUNT
];
97 bool should_stop
= false;
101 for (i
= 0; i
< NRF51_TIMER_REG_COUNT
; i
++) {
102 if (s
->cc
[i
] > s
->counter
) {
103 cc_remaining
[i
] = s
->cc
[i
] - s
->counter
;
105 cc_remaining
[i
] = BIT(bitwidths
[s
->bitmode
]) -
106 s
->counter
+ s
->cc
[i
];
110 ticks
= update_counter(s
, now
);
112 for (i
= 0; i
< NRF51_TIMER_REG_COUNT
; i
++) {
113 if (cc_remaining
[i
] <= ticks
) {
114 s
->events_compare
[i
] = 1;
116 if (s
->shorts
& BIT(i
)) {
117 s
->timer_start_ns
= now
;
118 s
->update_counter_ns
= s
->timer_start_ns
;
122 should_stop
|= s
->shorts
& BIT(i
+ 8);
130 timer_del(&s
->timer
);
136 static void counter_compare(NRF51TimerState
*s
)
138 uint32_t counter
= s
->counter
;
141 for (i
= 0; i
< NRF51_TIMER_REG_COUNT
; i
++) {
142 if (counter
== s
->cc
[i
]) {
143 s
->events_compare
[i
] = 1;
145 if (s
->shorts
& BIT(i
)) {
152 static uint64_t nrf51_timer_read(void *opaque
, hwaddr offset
, unsigned int size
)
154 NRF51TimerState
*s
= NRF51_TIMER(opaque
);
158 case NRF51_TIMER_EVENT_COMPARE_0
... NRF51_TIMER_EVENT_COMPARE_3
:
159 r
= s
->events_compare
[(offset
- NRF51_TIMER_EVENT_COMPARE_0
) / 4];
161 case NRF51_TIMER_REG_SHORTS
:
164 case NRF51_TIMER_REG_INTENSET
:
167 case NRF51_TIMER_REG_INTENCLR
:
170 case NRF51_TIMER_REG_MODE
:
173 case NRF51_TIMER_REG_BITMODE
:
176 case NRF51_TIMER_REG_PRESCALER
:
179 case NRF51_TIMER_REG_CC0
... NRF51_TIMER_REG_CC3
:
180 r
= s
->cc
[(offset
- NRF51_TIMER_REG_CC0
) / 4];
183 qemu_log_mask(LOG_GUEST_ERROR
,
184 "%s: bad read offset 0x%" HWADDR_PRIx
"\n",
188 trace_nrf51_timer_read(offset
, r
, size
);
193 static void nrf51_timer_write(void *opaque
, hwaddr offset
,
194 uint64_t value
, unsigned int size
)
196 NRF51TimerState
*s
= NRF51_TIMER(opaque
);
197 uint64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
200 trace_nrf51_timer_write(offset
, value
, size
);
203 case NRF51_TIMER_TASK_START
:
204 if (value
== NRF51_TRIGGER_TASK
&& s
->mode
== NRF51_TIMER_TIMER
) {
206 s
->timer_start_ns
= now
- ticks_to_ns(s
, s
->counter
);
207 s
->update_counter_ns
= s
->timer_start_ns
;
211 case NRF51_TIMER_TASK_STOP
:
212 case NRF51_TIMER_TASK_SHUTDOWN
:
213 if (value
== NRF51_TRIGGER_TASK
) {
215 timer_del(&s
->timer
);
218 case NRF51_TIMER_TASK_COUNT
:
219 if (value
== NRF51_TRIGGER_TASK
&& s
->mode
== NRF51_TIMER_COUNTER
) {
220 s
->counter
= (s
->counter
+ 1) % BIT(bitwidths
[s
->bitmode
]);
224 case NRF51_TIMER_TASK_CLEAR
:
225 if (value
== NRF51_TRIGGER_TASK
) {
226 s
->timer_start_ns
= now
;
227 s
->update_counter_ns
= s
->timer_start_ns
;
234 case NRF51_TIMER_TASK_CAPTURE_0
... NRF51_TIMER_TASK_CAPTURE_3
:
235 if (value
== NRF51_TRIGGER_TASK
) {
237 timer_expire(s
); /* update counter and all state */
240 idx
= (offset
- NRF51_TIMER_TASK_CAPTURE_0
) / 4;
241 s
->cc
[idx
] = s
->counter
;
244 case NRF51_TIMER_EVENT_COMPARE_0
... NRF51_TIMER_EVENT_COMPARE_3
:
245 if (value
== NRF51_EVENT_CLEAR
) {
246 s
->events_compare
[(offset
- NRF51_TIMER_EVENT_COMPARE_0
) / 4] = 0;
249 timer_expire(s
); /* update counter and all state */
253 case NRF51_TIMER_REG_SHORTS
:
254 s
->shorts
= value
& NRF51_TIMER_REG_SHORTS_MASK
;
256 case NRF51_TIMER_REG_INTENSET
:
257 s
->inten
|= value
& NRF51_TIMER_REG_INTEN_MASK
;
259 case NRF51_TIMER_REG_INTENCLR
:
260 s
->inten
&= ~(value
& NRF51_TIMER_REG_INTEN_MASK
);
262 case NRF51_TIMER_REG_MODE
:
265 case NRF51_TIMER_REG_BITMODE
:
266 if (s
->mode
== NRF51_TIMER_TIMER
&& s
->running
) {
267 qemu_log_mask(LOG_GUEST_ERROR
,
268 "%s: erroneous change of BITMODE while timer is running\n",
271 s
->bitmode
= value
& NRF51_TIMER_REG_BITMODE_MASK
;
273 case NRF51_TIMER_REG_PRESCALER
:
274 if (s
->mode
== NRF51_TIMER_TIMER
&& s
->running
) {
275 qemu_log_mask(LOG_GUEST_ERROR
,
276 "%s: erroneous change of PRESCALER while timer is running\n",
279 s
->prescaler
= value
& NRF51_TIMER_REG_PRESCALER_MASK
;
281 case NRF51_TIMER_REG_CC0
... NRF51_TIMER_REG_CC3
:
283 timer_expire(s
); /* update counter */
286 idx
= (offset
- NRF51_TIMER_REG_CC0
) / 4;
287 s
->cc
[idx
] = value
% BIT(bitwidths
[s
->bitmode
]);
294 qemu_log_mask(LOG_GUEST_ERROR
,
295 "%s: bad write offset 0x%" HWADDR_PRIx
"\n",
302 static const MemoryRegionOps rng_ops
= {
303 .read
= nrf51_timer_read
,
304 .write
= nrf51_timer_write
,
305 .endianness
= DEVICE_LITTLE_ENDIAN
,
306 .impl
.min_access_size
= 4,
307 .impl
.max_access_size
= 4,
310 static void nrf51_timer_init(Object
*obj
)
312 NRF51TimerState
*s
= NRF51_TIMER(obj
);
313 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
315 memory_region_init_io(&s
->iomem
, obj
, &rng_ops
, s
,
316 TYPE_NRF51_TIMER
, NRF51_TIMER_SIZE
);
317 sysbus_init_mmio(sbd
, &s
->iomem
);
318 sysbus_init_irq(sbd
, &s
->irq
);
320 timer_init_ns(&s
->timer
, QEMU_CLOCK_VIRTUAL
, timer_expire
, s
);
323 static void nrf51_timer_reset(DeviceState
*dev
)
325 NRF51TimerState
*s
= NRF51_TIMER(dev
);
327 timer_del(&s
->timer
);
328 s
->timer_start_ns
= 0x00;
329 s
->update_counter_ns
= 0x00;
333 memset(s
->events_compare
, 0x00, sizeof(s
->events_compare
));
334 memset(s
->cc
, 0x00, sizeof(s
->cc
));
343 static int nrf51_timer_post_load(void *opaque
, int version_id
)
345 NRF51TimerState
*s
= NRF51_TIMER(opaque
);
347 if (s
->running
&& s
->mode
== NRF51_TIMER_TIMER
) {
353 static const VMStateDescription vmstate_nrf51_timer
= {
354 .name
= TYPE_NRF51_TIMER
,
356 .post_load
= nrf51_timer_post_load
,
357 .fields
= (VMStateField
[]) {
358 VMSTATE_TIMER(timer
, NRF51TimerState
),
359 VMSTATE_INT64(timer_start_ns
, NRF51TimerState
),
360 VMSTATE_INT64(update_counter_ns
, NRF51TimerState
),
361 VMSTATE_UINT32(counter
, NRF51TimerState
),
362 VMSTATE_BOOL(running
, NRF51TimerState
),
363 VMSTATE_UINT8_ARRAY(events_compare
, NRF51TimerState
,
364 NRF51_TIMER_REG_COUNT
),
365 VMSTATE_UINT32_ARRAY(cc
, NRF51TimerState
, NRF51_TIMER_REG_COUNT
),
366 VMSTATE_UINT32(shorts
, NRF51TimerState
),
367 VMSTATE_UINT32(inten
, NRF51TimerState
),
368 VMSTATE_UINT32(mode
, NRF51TimerState
),
369 VMSTATE_UINT32(bitmode
, NRF51TimerState
),
370 VMSTATE_UINT32(prescaler
, NRF51TimerState
),
371 VMSTATE_END_OF_LIST()
375 static void nrf51_timer_class_init(ObjectClass
*klass
, void *data
)
377 DeviceClass
*dc
= DEVICE_CLASS(klass
);
379 dc
->reset
= nrf51_timer_reset
;
380 dc
->vmsd
= &vmstate_nrf51_timer
;
383 static const TypeInfo nrf51_timer_info
= {
384 .name
= TYPE_NRF51_TIMER
,
385 .parent
= TYPE_SYS_BUS_DEVICE
,
386 .instance_size
= sizeof(NRF51TimerState
),
387 .instance_init
= nrf51_timer_init
,
388 .class_init
= nrf51_timer_class_init
391 static void nrf51_timer_register_types(void)
393 type_register_static(&nrf51_timer_info
);
396 type_init(nrf51_timer_register_types
)