hw/mips/cps: Expose input clock and connect it to CPU cores
[qemu/ar7.git] / hw / timer / armv7m_systick.c
bloba8cec7eb56b1a10a4e09014b5a8f811957876f72
1 /*
2 * ARMv7M SysTick timer
4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
6 * Copyright (c) 2017 Linaro Ltd
7 * Written by Peter Maydell
9 * This code is licensed under the GPL (version 2 or later).
12 #include "qemu/osdep.h"
13 #include "hw/timer/armv7m_systick.h"
14 #include "migration/vmstate.h"
15 #include "hw/irq.h"
16 #include "hw/sysbus.h"
17 #include "qemu/timer.h"
18 #include "qemu/log.h"
19 #include "qemu/module.h"
20 #include "trace.h"
22 /* qemu timers run at 1GHz. We want something closer to 1MHz. */
23 #define SYSTICK_SCALE 1000ULL
25 #define SYSTICK_ENABLE (1 << 0)
26 #define SYSTICK_TICKINT (1 << 1)
27 #define SYSTICK_CLKSOURCE (1 << 2)
28 #define SYSTICK_COUNTFLAG (1 << 16)
30 int system_clock_scale;
32 /* Conversion factor from qemu timer to SysTick frequencies. */
33 static inline int64_t systick_scale(SysTickState *s)
35 if (s->control & SYSTICK_CLKSOURCE) {
36 return system_clock_scale;
37 } else {
38 return 1000;
42 static void systick_reload(SysTickState *s, int reset)
44 /* The Cortex-M3 Devices Generic User Guide says that "When the
45 * ENABLE bit is set to 1, the counter loads the RELOAD value from the
46 * SYST RVR register and then counts down". So, we need to check the
47 * ENABLE bit before reloading the value.
49 trace_systick_reload();
51 if ((s->control & SYSTICK_ENABLE) == 0) {
52 return;
55 if (reset) {
56 s->tick = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
58 s->tick += (s->reload + 1) * systick_scale(s);
59 timer_mod(s->timer, s->tick);
62 static void systick_timer_tick(void *opaque)
64 SysTickState *s = (SysTickState *)opaque;
66 trace_systick_timer_tick();
68 s->control |= SYSTICK_COUNTFLAG;
69 if (s->control & SYSTICK_TICKINT) {
70 /* Tell the NVIC to pend the SysTick exception */
71 qemu_irq_pulse(s->irq);
73 if (s->reload == 0) {
74 s->control &= ~SYSTICK_ENABLE;
75 } else {
76 systick_reload(s, 0);
80 static MemTxResult systick_read(void *opaque, hwaddr addr, uint64_t *data,
81 unsigned size, MemTxAttrs attrs)
83 SysTickState *s = opaque;
84 uint32_t val;
86 if (attrs.user) {
87 /* Generate BusFault for unprivileged accesses */
88 return MEMTX_ERROR;
91 switch (addr) {
92 case 0x0: /* SysTick Control and Status. */
93 val = s->control;
94 s->control &= ~SYSTICK_COUNTFLAG;
95 break;
96 case 0x4: /* SysTick Reload Value. */
97 val = s->reload;
98 break;
99 case 0x8: /* SysTick Current Value. */
101 int64_t t;
103 if ((s->control & SYSTICK_ENABLE) == 0) {
104 val = 0;
105 break;
107 t = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
108 if (t >= s->tick) {
109 val = 0;
110 break;
112 val = ((s->tick - (t + 1)) / systick_scale(s)) + 1;
113 /* The interrupt in triggered when the timer reaches zero.
114 However the counter is not reloaded until the next clock
115 tick. This is a hack to return zero during the first tick. */
116 if (val > s->reload) {
117 val = 0;
119 break;
121 case 0xc: /* SysTick Calibration Value. */
122 val = 10000;
123 break;
124 default:
125 val = 0;
126 qemu_log_mask(LOG_GUEST_ERROR,
127 "SysTick: Bad read offset 0x%" HWADDR_PRIx "\n", addr);
128 break;
131 trace_systick_read(addr, val, size);
132 *data = val;
133 return MEMTX_OK;
136 static MemTxResult systick_write(void *opaque, hwaddr addr,
137 uint64_t value, unsigned size,
138 MemTxAttrs attrs)
140 SysTickState *s = opaque;
142 if (attrs.user) {
143 /* Generate BusFault for unprivileged accesses */
144 return MEMTX_ERROR;
147 trace_systick_write(addr, value, size);
149 switch (addr) {
150 case 0x0: /* SysTick Control and Status. */
152 uint32_t oldval = s->control;
154 s->control &= 0xfffffff8;
155 s->control |= value & 7;
156 if ((oldval ^ value) & SYSTICK_ENABLE) {
157 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
158 if (value & SYSTICK_ENABLE) {
159 if (s->tick) {
160 s->tick += now;
161 timer_mod(s->timer, s->tick);
162 } else {
163 systick_reload(s, 1);
165 } else {
166 timer_del(s->timer);
167 s->tick -= now;
168 if (s->tick < 0) {
169 s->tick = 0;
172 } else if ((oldval ^ value) & SYSTICK_CLKSOURCE) {
173 /* This is a hack. Force the timer to be reloaded
174 when the reference clock is changed. */
175 systick_reload(s, 1);
177 break;
179 case 0x4: /* SysTick Reload Value. */
180 s->reload = value;
181 break;
182 case 0x8: /* SysTick Current Value. Writes reload the timer. */
183 systick_reload(s, 1);
184 s->control &= ~SYSTICK_COUNTFLAG;
185 break;
186 default:
187 qemu_log_mask(LOG_GUEST_ERROR,
188 "SysTick: Bad write offset 0x%" HWADDR_PRIx "\n", addr);
190 return MEMTX_OK;
193 static const MemoryRegionOps systick_ops = {
194 .read_with_attrs = systick_read,
195 .write_with_attrs = systick_write,
196 .endianness = DEVICE_NATIVE_ENDIAN,
197 .valid.min_access_size = 4,
198 .valid.max_access_size = 4,
201 static void systick_reset(DeviceState *dev)
203 SysTickState *s = SYSTICK(dev);
206 * Forgetting to set system_clock_scale is always a board code
207 * bug. We can't check this earlier because for some boards
208 * (like stellaris) it is not yet configured at the point where
209 * the systick device is realized.
211 assert(system_clock_scale != 0);
213 s->control = 0;
214 s->reload = 0;
215 s->tick = 0;
216 timer_del(s->timer);
219 static void systick_instance_init(Object *obj)
221 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
222 SysTickState *s = SYSTICK(obj);
224 memory_region_init_io(&s->iomem, obj, &systick_ops, s, "systick", 0xe0);
225 sysbus_init_mmio(sbd, &s->iomem);
226 sysbus_init_irq(sbd, &s->irq);
229 static void systick_realize(DeviceState *dev, Error **errp)
231 SysTickState *s = SYSTICK(dev);
232 s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, systick_timer_tick, s);
235 static const VMStateDescription vmstate_systick = {
236 .name = "armv7m_systick",
237 .version_id = 1,
238 .minimum_version_id = 1,
239 .fields = (VMStateField[]) {
240 VMSTATE_UINT32(control, SysTickState),
241 VMSTATE_UINT32(reload, SysTickState),
242 VMSTATE_INT64(tick, SysTickState),
243 VMSTATE_TIMER_PTR(timer, SysTickState),
244 VMSTATE_END_OF_LIST()
248 static void systick_class_init(ObjectClass *klass, void *data)
250 DeviceClass *dc = DEVICE_CLASS(klass);
252 dc->vmsd = &vmstate_systick;
253 dc->reset = systick_reset;
254 dc->realize = systick_realize;
257 static const TypeInfo armv7m_systick_info = {
258 .name = TYPE_SYSTICK,
259 .parent = TYPE_SYS_BUS_DEVICE,
260 .instance_init = systick_instance_init,
261 .instance_size = sizeof(SysTickState),
262 .class_init = systick_class_init,
265 static void armv7m_systick_register_types(void)
267 type_register_static(&armv7m_systick_info);
270 type_init(armv7m_systick_register_types)