]> git.proxmox.com Git - mirror_qemu.git/blame - hw/timer/arm_timer.c
Merge remote-tracking branch 'remotes/ericb/tags/pull-nbd-2018-11-19' into staging
[mirror_qemu.git] / hw / timer / arm_timer.c
CommitLineData
5fafdf24 1/*
cdbdb648
PB
2 * ARM PrimeCell Timer modules.
3 *
4 * Copyright (c) 2005-2006 CodeSourcery.
5 * Written by Paul Brook
6 *
8e31bf38 7 * This code is licensed under the GPL.
cdbdb648
PB
8 */
9
8ef94f0b 10#include "qemu/osdep.h"
83c9f4ca 11#include "hw/sysbus.h"
1de7afc9 12#include "qemu/timer.h"
104a26a2 13#include "qemu-common.h"
83c9f4ca
PB
14#include "hw/qdev.h"
15#include "hw/ptimer.h"
6a1751b7 16#include "qemu/main-loop.h"
03dd024f 17#include "qemu/log.h"
cdbdb648
PB
18
19/* Common timer implementation. */
20
21#define TIMER_CTRL_ONESHOT (1 << 0)
22#define TIMER_CTRL_32BIT (1 << 1)
23#define TIMER_CTRL_DIV1 (0 << 2)
24#define TIMER_CTRL_DIV16 (1 << 2)
25#define TIMER_CTRL_DIV256 (2 << 2)
26#define TIMER_CTRL_IE (1 << 5)
27#define TIMER_CTRL_PERIODIC (1 << 6)
28#define TIMER_CTRL_ENABLE (1 << 7)
29
30typedef struct {
423f0742 31 ptimer_state *timer;
cdbdb648 32 uint32_t control;
cdbdb648 33 uint32_t limit;
cdbdb648
PB
34 int freq;
35 int int_level;
d537cf6c 36 qemu_irq irq;
cdbdb648
PB
37} arm_timer_state;
38
cdbdb648
PB
39/* Check all active timers, and schedule the next timer interrupt. */
40
423f0742 41static void arm_timer_update(arm_timer_state *s)
cdbdb648 42{
cdbdb648
PB
43 /* Update interrupts. */
44 if (s->int_level && (s->control & TIMER_CTRL_IE)) {
d537cf6c 45 qemu_irq_raise(s->irq);
cdbdb648 46 } else {
d537cf6c 47 qemu_irq_lower(s->irq);
cdbdb648 48 }
cdbdb648
PB
49}
50
a8170e5e 51static uint32_t arm_timer_read(void *opaque, hwaddr offset)
cdbdb648
PB
52{
53 arm_timer_state *s = (arm_timer_state *)opaque;
54
55 switch (offset >> 2) {
56 case 0: /* TimerLoad */
57 case 6: /* TimerBGLoad */
58 return s->limit;
59 case 1: /* TimerValue */
423f0742 60 return ptimer_get_count(s->timer);
cdbdb648
PB
61 case 2: /* TimerControl */
62 return s->control;
63 case 4: /* TimerRIS */
64 return s->int_level;
65 case 5: /* TimerMIS */
66 if ((s->control & TIMER_CTRL_IE) == 0)
67 return 0;
68 return s->int_level;
69 default:
edb94a41
PM
70 qemu_log_mask(LOG_GUEST_ERROR,
71 "%s: Bad offset %x\n", __func__, (int)offset);
cdbdb648
PB
72 return 0;
73 }
74}
75
423f0742
PB
76/* Reset the timer limit after settings have changed. */
77static void arm_timer_recalibrate(arm_timer_state *s, int reload)
78{
79 uint32_t limit;
80
a9cf98d9 81 if ((s->control & (TIMER_CTRL_PERIODIC | TIMER_CTRL_ONESHOT)) == 0) {
423f0742
PB
82 /* Free running. */
83 if (s->control & TIMER_CTRL_32BIT)
84 limit = 0xffffffff;
85 else
86 limit = 0xffff;
87 } else {
88 /* Periodic. */
89 limit = s->limit;
90 }
91 ptimer_set_limit(s->timer, limit, reload);
92}
93
a8170e5e 94static void arm_timer_write(void *opaque, hwaddr offset,
cdbdb648
PB
95 uint32_t value)
96{
97 arm_timer_state *s = (arm_timer_state *)opaque;
423f0742 98 int freq;
cdbdb648 99
cdbdb648
PB
100 switch (offset >> 2) {
101 case 0: /* TimerLoad */
102 s->limit = value;
423f0742 103 arm_timer_recalibrate(s, 1);
cdbdb648
PB
104 break;
105 case 1: /* TimerValue */
106 /* ??? Linux seems to want to write to this readonly register.
107 Ignore it. */
108 break;
109 case 2: /* TimerControl */
110 if (s->control & TIMER_CTRL_ENABLE) {
111 /* Pause the timer if it is running. This may cause some
112 inaccuracy dure to rounding, but avoids a whole lot of other
113 messyness. */
423f0742 114 ptimer_stop(s->timer);
cdbdb648
PB
115 }
116 s->control = value;
423f0742 117 freq = s->freq;
cdbdb648
PB
118 /* ??? Need to recalculate expiry time after changing divisor. */
119 switch ((value >> 2) & 3) {
423f0742
PB
120 case 1: freq >>= 4; break;
121 case 2: freq >>= 8; break;
cdbdb648 122 }
d6759902 123 arm_timer_recalibrate(s, s->control & TIMER_CTRL_ENABLE);
423f0742 124 ptimer_set_freq(s->timer, freq);
cdbdb648
PB
125 if (s->control & TIMER_CTRL_ENABLE) {
126 /* Restart the timer if still enabled. */
423f0742 127 ptimer_run(s->timer, (s->control & TIMER_CTRL_ONESHOT) != 0);
cdbdb648
PB
128 }
129 break;
130 case 3: /* TimerIntClr */
131 s->int_level = 0;
132 break;
133 case 6: /* TimerBGLoad */
134 s->limit = value;
423f0742 135 arm_timer_recalibrate(s, 0);
cdbdb648
PB
136 break;
137 default:
edb94a41
PM
138 qemu_log_mask(LOG_GUEST_ERROR,
139 "%s: Bad offset %x\n", __func__, (int)offset);
cdbdb648 140 }
423f0742 141 arm_timer_update(s);
cdbdb648
PB
142}
143
144static void arm_timer_tick(void *opaque)
145{
423f0742
PB
146 arm_timer_state *s = (arm_timer_state *)opaque;
147 s->int_level = 1;
148 arm_timer_update(s);
cdbdb648
PB
149}
150
eecd33a5
JQ
151static const VMStateDescription vmstate_arm_timer = {
152 .name = "arm_timer",
153 .version_id = 1,
154 .minimum_version_id = 1,
8f1e884b 155 .fields = (VMStateField[]) {
eecd33a5
JQ
156 VMSTATE_UINT32(control, arm_timer_state),
157 VMSTATE_UINT32(limit, arm_timer_state),
158 VMSTATE_INT32(int_level, arm_timer_state),
159 VMSTATE_PTIMER(timer, arm_timer_state),
160 VMSTATE_END_OF_LIST()
161 }
162};
23e39294 163
6a824ec3 164static arm_timer_state *arm_timer_init(uint32_t freq)
cdbdb648
PB
165{
166 arm_timer_state *s;
423f0742 167 QEMUBH *bh;
cdbdb648 168
7267c094 169 s = (arm_timer_state *)g_malloc0(sizeof(arm_timer_state));
423f0742 170 s->freq = freq;
cdbdb648 171 s->control = TIMER_CTRL_IE;
cdbdb648 172
423f0742 173 bh = qemu_bh_new(arm_timer_tick, s);
e7ea81c3 174 s->timer = ptimer_init(bh, PTIMER_POLICY_DEFAULT);
eecd33a5 175 vmstate_register(NULL, -1, &vmstate_arm_timer, s);
cdbdb648
PB
176 return s;
177}
178
179/* ARM PrimeCell SP804 dual timer module.
7b4252e8
PC
180 * Docs at
181 * http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0271d/index.html
182*/
cdbdb648 183
0c88dea5
AF
184#define TYPE_SP804 "sp804"
185#define SP804(obj) OBJECT_CHECK(SP804State, (obj), TYPE_SP804)
186
1024d7f0 187typedef struct SP804State {
0c88dea5
AF
188 SysBusDevice parent_obj;
189
e219dea2 190 MemoryRegion iomem;
6a824ec3 191 arm_timer_state *timer[2];
104a26a2 192 uint32_t freq0, freq1;
cdbdb648 193 int level[2];
d537cf6c 194 qemu_irq irq;
1024d7f0 195} SP804State;
cdbdb648 196
7b4252e8
PC
197static const uint8_t sp804_ids[] = {
198 /* Timer ID */
199 0x04, 0x18, 0x14, 0,
200 /* PrimeCell ID */
201 0xd, 0xf0, 0x05, 0xb1
202};
203
d537cf6c 204/* Merge the IRQs from the two component devices. */
cdbdb648
PB
205static void sp804_set_irq(void *opaque, int irq, int level)
206{
1024d7f0 207 SP804State *s = (SP804State *)opaque;
cdbdb648
PB
208
209 s->level[irq] = level;
d537cf6c 210 qemu_set_irq(s->irq, s->level[0] || s->level[1]);
cdbdb648
PB
211}
212
a8170e5e 213static uint64_t sp804_read(void *opaque, hwaddr offset,
e219dea2 214 unsigned size)
cdbdb648 215{
1024d7f0 216 SP804State *s = (SP804State *)opaque;
cdbdb648 217
cdbdb648
PB
218 if (offset < 0x20) {
219 return arm_timer_read(s->timer[0], offset);
7b4252e8
PC
220 }
221 if (offset < 0x40) {
cdbdb648
PB
222 return arm_timer_read(s->timer[1], offset - 0x20);
223 }
7b4252e8
PC
224
225 /* TimerPeriphID */
226 if (offset >= 0xfe0 && offset <= 0xffc) {
227 return sp804_ids[(offset - 0xfe0) >> 2];
228 }
229
230 switch (offset) {
231 /* Integration Test control registers, which we won't support */
232 case 0xf00: /* TimerITCR */
233 case 0xf04: /* TimerITOP (strictly write only but..) */
edb94a41
PM
234 qemu_log_mask(LOG_UNIMP,
235 "%s: integration test registers unimplemented\n",
236 __func__);
7b4252e8
PC
237 return 0;
238 }
239
edb94a41
PM
240 qemu_log_mask(LOG_GUEST_ERROR,
241 "%s: Bad offset %x\n", __func__, (int)offset);
7b4252e8 242 return 0;
cdbdb648
PB
243}
244
a8170e5e 245static void sp804_write(void *opaque, hwaddr offset,
e219dea2 246 uint64_t value, unsigned size)
cdbdb648 247{
1024d7f0 248 SP804State *s = (SP804State *)opaque;
cdbdb648 249
cdbdb648
PB
250 if (offset < 0x20) {
251 arm_timer_write(s->timer[0], offset, value);
7b4252e8
PC
252 return;
253 }
254
255 if (offset < 0x40) {
cdbdb648 256 arm_timer_write(s->timer[1], offset - 0x20, value);
7b4252e8 257 return;
cdbdb648 258 }
7b4252e8
PC
259
260 /* Technically we could be writing to the Test Registers, but not likely */
edb94a41
PM
261 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %x\n",
262 __func__, (int)offset);
cdbdb648
PB
263}
264
e219dea2
AK
265static const MemoryRegionOps sp804_ops = {
266 .read = sp804_read,
267 .write = sp804_write,
268 .endianness = DEVICE_NATIVE_ENDIAN,
cdbdb648
PB
269};
270
81986ac4
JQ
271static const VMStateDescription vmstate_sp804 = {
272 .name = "sp804",
273 .version_id = 1,
274 .minimum_version_id = 1,
8f1e884b 275 .fields = (VMStateField[]) {
1024d7f0 276 VMSTATE_INT32_ARRAY(level, SP804State, 2),
81986ac4
JQ
277 VMSTATE_END_OF_LIST()
278 }
279};
23e39294 280
0d175e74 281static void sp804_init(Object *obj)
cdbdb648 282{
0d175e74
XZ
283 SP804State *s = SP804(obj);
284 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
cdbdb648 285
0c88dea5 286 sysbus_init_irq(sbd, &s->irq);
0d175e74
XZ
287 memory_region_init_io(&s->iomem, obj, &sp804_ops, s,
288 "sp804", 0x1000);
289 sysbus_init_mmio(sbd, &s->iomem);
290}
291
292static void sp804_realize(DeviceState *dev, Error **errp)
293{
294 SP804State *s = SP804(dev);
295
104a26a2
ML
296 s->timer[0] = arm_timer_init(s->freq0);
297 s->timer[1] = arm_timer_init(s->freq1);
b6412724
SZ
298 s->timer[0]->irq = qemu_allocate_irq(sp804_set_irq, s, 0);
299 s->timer[1]->irq = qemu_allocate_irq(sp804_set_irq, s, 1);
cdbdb648
PB
300}
301
cdbdb648
PB
302/* Integrator/CP timer module. */
303
e2051b42
AF
304#define TYPE_INTEGRATOR_PIT "integrator_pit"
305#define INTEGRATOR_PIT(obj) \
306 OBJECT_CHECK(icp_pit_state, (obj), TYPE_INTEGRATOR_PIT)
307
cdbdb648 308typedef struct {
e2051b42
AF
309 SysBusDevice parent_obj;
310
e219dea2 311 MemoryRegion iomem;
6a824ec3 312 arm_timer_state *timer[3];
cdbdb648
PB
313} icp_pit_state;
314
a8170e5e 315static uint64_t icp_pit_read(void *opaque, hwaddr offset,
e219dea2 316 unsigned size)
cdbdb648
PB
317{
318 icp_pit_state *s = (icp_pit_state *)opaque;
319 int n;
320
321 /* ??? Don't know the PrimeCell ID for this device. */
cdbdb648 322 n = offset >> 8;
ee71c984 323 if (n > 2) {
edb94a41 324 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad timer %d\n", __func__, n);
cba933b2 325 return 0;
2ac71179 326 }
cdbdb648
PB
327
328 return arm_timer_read(s->timer[n], offset & 0xff);
329}
330
a8170e5e 331static void icp_pit_write(void *opaque, hwaddr offset,
e219dea2 332 uint64_t value, unsigned size)
cdbdb648
PB
333{
334 icp_pit_state *s = (icp_pit_state *)opaque;
335 int n;
336
cdbdb648 337 n = offset >> 8;
ee71c984 338 if (n > 2) {
edb94a41 339 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad timer %d\n", __func__, n);
cba933b2 340 return;
2ac71179 341 }
cdbdb648
PB
342
343 arm_timer_write(s->timer[n], offset & 0xff, value);
344}
345
e219dea2
AK
346static const MemoryRegionOps icp_pit_ops = {
347 .read = icp_pit_read,
348 .write = icp_pit_write,
349 .endianness = DEVICE_NATIVE_ENDIAN,
cdbdb648
PB
350};
351
0d175e74 352static void icp_pit_init(Object *obj)
cdbdb648 353{
0d175e74
XZ
354 icp_pit_state *s = INTEGRATOR_PIT(obj);
355 SysBusDevice *dev = SYS_BUS_DEVICE(obj);
cdbdb648 356
cdbdb648 357 /* Timer 0 runs at the system clock speed (40MHz). */
6a824ec3 358 s->timer[0] = arm_timer_init(40000000);
cdbdb648 359 /* The other two timers run at 1MHz. */
6a824ec3
PB
360 s->timer[1] = arm_timer_init(1000000);
361 s->timer[2] = arm_timer_init(1000000);
362
363 sysbus_init_irq(dev, &s->timer[0]->irq);
364 sysbus_init_irq(dev, &s->timer[1]->irq);
365 sysbus_init_irq(dev, &s->timer[2]->irq);
cdbdb648 366
0d175e74 367 memory_region_init_io(&s->iomem, obj, &icp_pit_ops, s,
853dca12 368 "icp_pit", 0x1000);
750ecd44 369 sysbus_init_mmio(dev, &s->iomem);
23e39294
PB
370 /* This device has no state to save/restore. The component timers will
371 save themselves. */
999e12bb
AL
372}
373
8c43a6f0 374static const TypeInfo icp_pit_info = {
e2051b42 375 .name = TYPE_INTEGRATOR_PIT,
39bffca2
AL
376 .parent = TYPE_SYS_BUS_DEVICE,
377 .instance_size = sizeof(icp_pit_state),
0d175e74 378 .instance_init = icp_pit_init,
39bffca2
AL
379};
380
381static Property sp804_properties[] = {
1024d7f0
AF
382 DEFINE_PROP_UINT32("freq0", SP804State, freq0, 1000000),
383 DEFINE_PROP_UINT32("freq1", SP804State, freq1, 1000000),
39bffca2 384 DEFINE_PROP_END_OF_LIST(),
999e12bb
AL
385};
386
387static void sp804_class_init(ObjectClass *klass, void *data)
388{
39bffca2 389 DeviceClass *k = DEVICE_CLASS(klass);
999e12bb 390
0d175e74 391 k->realize = sp804_realize;
39bffca2 392 k->props = sp804_properties;
d712a5a2 393 k->vmsd = &vmstate_sp804;
999e12bb
AL
394}
395
8c43a6f0 396static const TypeInfo sp804_info = {
0c88dea5 397 .name = TYPE_SP804,
39bffca2 398 .parent = TYPE_SYS_BUS_DEVICE,
1024d7f0 399 .instance_size = sizeof(SP804State),
0d175e74 400 .instance_init = sp804_init,
39bffca2 401 .class_init = sp804_class_init,
999e12bb
AL
402};
403
83f7d43a 404static void arm_timer_register_types(void)
6a824ec3 405{
39bffca2
AL
406 type_register_static(&icp_pit_info);
407 type_register_static(&sp804_info);
6a824ec3
PB
408}
409
83f7d43a 410type_init(arm_timer_register_types)