2 * Xilinx Zynq cadence TTC model
4 * Copyright (c) 2011 Xilinx Inc.
6 * Copyright (c) 2012 PetaLogix Pty Ltd.
7 * Written By Haibing Ma
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
21 #include "hw/sysbus.h"
22 #include "migration/vmstate.h"
23 #include "qemu/module.h"
24 #include "qemu/timer.h"
26 #ifdef CADENCE_TTC_ERR_DEBUG
27 #define DB_PRINT(...) do { \
28 fprintf(stderr, ": %s: ", __func__); \
29 fprintf(stderr, ## __VA_ARGS__); \
35 #define COUNTER_INTR_IV 0x00000001
36 #define COUNTER_INTR_M1 0x00000002
37 #define COUNTER_INTR_M2 0x00000004
38 #define COUNTER_INTR_M3 0x00000008
39 #define COUNTER_INTR_OV 0x00000010
40 #define COUNTER_INTR_EV 0x00000020
42 #define COUNTER_CTRL_DIS 0x00000001
43 #define COUNTER_CTRL_INT 0x00000002
44 #define COUNTER_CTRL_DEC 0x00000004
45 #define COUNTER_CTRL_MATCH 0x00000008
46 #define COUNTER_CTRL_RST 0x00000010
48 #define CLOCK_CTRL_PS_EN 0x00000001
49 #define CLOCK_CTRL_PS_V 0x0000001e
58 uint16_t reg_interval;
59 uint16_t reg_match[3];
62 uint32_t reg_event_ctrl;
66 unsigned int cpu_time_valid;
71 #define TYPE_CADENCE_TTC "cadence_ttc"
72 #define CADENCE_TTC(obj) \
73 OBJECT_CHECK(CadenceTTCState, (obj), TYPE_CADENCE_TTC)
75 typedef struct CadenceTTCState {
76 SysBusDevice parent_obj;
79 CadenceTimerState timer[3];
82 static void cadence_timer_update(CadenceTimerState *s)
84 qemu_set_irq(s->irq, !!(s->reg_intr & s->reg_intr_en));
87 static CadenceTimerState *cadence_timer_from_addr(void *opaque,
91 CadenceTTCState *s = (CadenceTTCState *)opaque;
93 index = (offset >> 2) % 3;
95 return &s->timer[index];
98 static uint64_t cadence_timer_get_ns(CadenceTimerState *s, uint64_t timer_steps)
100 /* timer_steps has max value of 0x100000000. double check it
101 * (or overflow can happen below) */
102 assert(timer_steps <= 1ULL << 32);
104 uint64_t r = timer_steps * 1000000000ULL;
105 if (s->reg_clock & CLOCK_CTRL_PS_EN) {
106 r >>= 16 - (((s->reg_clock & CLOCK_CTRL_PS_V) >> 1) + 1);
110 r /= (uint64_t)s->freq;
114 static uint64_t cadence_timer_get_steps(CadenceTimerState *s, uint64_t ns)
116 uint64_t to_divide = 1000000000ULL;
119 /* for very large intervals (> 8s) do some division first to stop
120 * overflow (costs some prescision) */
121 while (r >= 8ULL << 30 && to_divide > 1) {
126 /* keep early-dividing as needed */
127 while (r >= 8ULL << 30 && to_divide > 1) {
131 r *= (uint64_t)s->freq;
132 if (s->reg_clock & CLOCK_CTRL_PS_EN) {
133 r /= 1 << (((s->reg_clock & CLOCK_CTRL_PS_V) >> 1) + 1);
140 /* determine if x is in between a and b, exclusive of a, inclusive of b */
142 static inline int64_t is_between(int64_t x, int64_t a, int64_t b)
145 return x > a && x <= b;
147 return x < a && x >= b;
150 static void cadence_timer_run(CadenceTimerState *s)
153 int64_t event_interval, next_value;
155 assert(s->cpu_time_valid); /* cadence_timer_sync must be called first */
157 if (s->reg_count & COUNTER_CTRL_DIS) {
158 s->cpu_time_valid = 0;
162 { /* figure out what's going to happen next (rollover or match) */
163 int64_t interval = (uint64_t)((s->reg_count & COUNTER_CTRL_INT) ?
164 (int64_t)s->reg_interval + 1 : 0x10000ULL) << 16;
165 next_value = (s->reg_count & COUNTER_CTRL_DEC) ? -1ULL : interval;
166 for (i = 0; i < 3; ++i) {
167 int64_t cand = (uint64_t)s->reg_match[i] << 16;
168 if (is_between(cand, (uint64_t)s->reg_value, next_value)) {
173 DB_PRINT("next timer event value: %09llx\n",
174 (unsigned long long)next_value);
176 event_interval = next_value - (int64_t)s->reg_value;
177 event_interval = (event_interval < 0) ? -event_interval : event_interval;
179 timer_mod(s->timer, s->cpu_time +
180 cadence_timer_get_ns(s, event_interval));
183 static void cadence_timer_sync(CadenceTimerState *s)
187 int64_t interval = ((s->reg_count & COUNTER_CTRL_INT) ?
188 (int64_t)s->reg_interval + 1 : 0x10000ULL) << 16;
189 uint64_t old_time = s->cpu_time;
191 s->cpu_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
192 DB_PRINT("cpu time: %lld ns\n", (long long)old_time);
194 if (!s->cpu_time_valid || old_time == s->cpu_time) {
195 s->cpu_time_valid = 1;
199 r = (int64_t)cadence_timer_get_steps(s, s->cpu_time - old_time);
200 x = (int64_t)s->reg_value + ((s->reg_count & COUNTER_CTRL_DEC) ? -r : r);
202 for (i = 0; i < 3; ++i) {
203 int64_t m = (int64_t)s->reg_match[i] << 16;
207 /* check to see if match event has occurred. check m +/- interval
208 * to account for match events in wrap around cases */
209 if (is_between(m, s->reg_value, x) ||
210 is_between(m + interval, s->reg_value, x) ||
211 is_between(m - interval, s->reg_value, x)) {
212 s->reg_intr |= (2 << i);
215 if ((x < 0) || (x >= interval)) {
216 s->reg_intr |= (s->reg_count & COUNTER_CTRL_INT) ?
217 COUNTER_INTR_IV : COUNTER_INTR_OV;
222 s->reg_value = (uint32_t)(x % interval);
223 cadence_timer_update(s);
226 static void cadence_timer_tick(void *opaque)
228 CadenceTimerState *s = opaque;
231 cadence_timer_sync(s);
232 cadence_timer_run(s);
235 static uint32_t cadence_ttc_read_imp(void *opaque, hwaddr offset)
237 CadenceTimerState *s = cadence_timer_from_addr(opaque, offset);
240 cadence_timer_sync(s);
241 cadence_timer_run(s);
244 case 0x00: /* clock control */
249 case 0x0c: /* counter control */
254 case 0x18: /* counter value */
257 return (uint16_t)(s->reg_value >> 16);
259 case 0x24: /* reg_interval counter */
262 return s->reg_interval;
264 case 0x30: /* match 1 counter */
267 return s->reg_match[0];
269 case 0x3c: /* match 2 counter */
272 return s->reg_match[1];
274 case 0x48: /* match 3 counter */
277 return s->reg_match[2];
279 case 0x54: /* interrupt register */
282 /* cleared after read */
285 cadence_timer_update(s);
288 case 0x60: /* interrupt enable */
291 return s->reg_intr_en;
296 return s->reg_event_ctrl;
308 static uint64_t cadence_ttc_read(void *opaque, hwaddr offset,
311 uint32_t ret = cadence_ttc_read_imp(opaque, offset);
313 DB_PRINT("addr: %08x data: %08x\n", (unsigned)offset, (unsigned)ret);
317 static void cadence_ttc_write(void *opaque, hwaddr offset,
318 uint64_t value, unsigned size)
320 CadenceTimerState *s = cadence_timer_from_addr(opaque, offset);
322 DB_PRINT("addr: %08x data %08x\n", (unsigned)offset, (unsigned)value);
324 cadence_timer_sync(s);
327 case 0x00: /* clock control */
330 s->reg_clock = value & 0x3F;
333 case 0x0c: /* counter control */
336 if (value & COUNTER_CTRL_RST) {
339 s->reg_count = value & 0x3f & ~COUNTER_CTRL_RST;
342 case 0x24: /* interval register */
345 s->reg_interval = value & 0xffff;
348 case 0x30: /* match register */
351 s->reg_match[0] = value & 0xffff;
354 case 0x3c: /* match register */
357 s->reg_match[1] = value & 0xffff;
360 case 0x48: /* match register */
363 s->reg_match[2] = value & 0xffff;
366 case 0x54: /* interrupt register */
371 case 0x60: /* interrupt enable */
374 s->reg_intr_en = value & 0x3f;
377 case 0x6c: /* event control */
380 s->reg_event_ctrl = value & 0x07;
387 cadence_timer_run(s);
388 cadence_timer_update(s);
391 static const MemoryRegionOps cadence_ttc_ops = {
392 .read = cadence_ttc_read,
393 .write = cadence_ttc_write,
394 .endianness = DEVICE_NATIVE_ENDIAN,
397 static void cadence_timer_reset(CadenceTimerState *s)
402 static void cadence_timer_init(uint32_t freq, CadenceTimerState *s)
404 memset(s, 0, sizeof(CadenceTimerState));
407 cadence_timer_reset(s);
409 s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, cadence_timer_tick, s);
412 static void cadence_ttc_init(Object *obj)
414 CadenceTTCState *s = CADENCE_TTC(obj);
417 for (i = 0; i < 3; ++i) {
418 cadence_timer_init(133000000, &s->timer[i]);
419 sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->timer[i].irq);
422 memory_region_init_io(&s->iomem, obj, &cadence_ttc_ops, s,
424 sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->iomem);
427 static int cadence_timer_pre_save(void *opaque)
429 cadence_timer_sync((CadenceTimerState *)opaque);
434 static int cadence_timer_post_load(void *opaque, int version_id)
436 CadenceTimerState *s = opaque;
438 s->cpu_time_valid = 0;
439 cadence_timer_sync(s);
440 cadence_timer_run(s);
441 cadence_timer_update(s);
445 static const VMStateDescription vmstate_cadence_timer = {
446 .name = "cadence_timer",
448 .minimum_version_id = 1,
449 .pre_save = cadence_timer_pre_save,
450 .post_load = cadence_timer_post_load,
451 .fields = (VMStateField[]) {
452 VMSTATE_UINT32(reg_clock, CadenceTimerState),
453 VMSTATE_UINT32(reg_count, CadenceTimerState),
454 VMSTATE_UINT32(reg_value, CadenceTimerState),
455 VMSTATE_UINT16(reg_interval, CadenceTimerState),
456 VMSTATE_UINT16_ARRAY(reg_match, CadenceTimerState, 3),
457 VMSTATE_UINT32(reg_intr, CadenceTimerState),
458 VMSTATE_UINT32(reg_intr_en, CadenceTimerState),
459 VMSTATE_UINT32(reg_event_ctrl, CadenceTimerState),
460 VMSTATE_UINT32(reg_event, CadenceTimerState),
461 VMSTATE_END_OF_LIST()
465 static const VMStateDescription vmstate_cadence_ttc = {
466 .name = "cadence_TTC",
468 .minimum_version_id = 1,
469 .fields = (VMStateField[]) {
470 VMSTATE_STRUCT_ARRAY(timer, CadenceTTCState, 3, 0,
471 vmstate_cadence_timer,
473 VMSTATE_END_OF_LIST()
477 static void cadence_ttc_class_init(ObjectClass *klass, void *data)
479 DeviceClass *dc = DEVICE_CLASS(klass);
481 dc->vmsd = &vmstate_cadence_ttc;
484 static const TypeInfo cadence_ttc_info = {
485 .name = TYPE_CADENCE_TTC,
486 .parent = TYPE_SYS_BUS_DEVICE,
487 .instance_size = sizeof(CadenceTTCState),
488 .instance_init = cadence_ttc_init,
489 .class_init = cadence_ttc_class_init,
492 static void cadence_ttc_register_types(void)
494 type_register_static(&cadence_ttc_info);
497 type_init(cadence_ttc_register_types)