2 * QEMU model of the Canon DIGIC timer block.
6 * This model is based on reverse engineering efforts
7 * made by CHDK (http://chdk.wikia.com) and
8 * Magic Lantern (http://www.magiclantern.fm) projects
11 * See "Timer/Clock Module" docs here:
12 * http://magiclantern.wikia.com/wiki/Register_Map
14 * The QEMU model of the OSTimer in PKUnity SoC by Guan Xuetao
15 * is used as a template.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
29 #include "qemu/osdep.h"
30 #include "hw/sysbus.h"
31 #include "hw/ptimer.h"
32 #include "qemu/main-loop.h"
33 #include "qemu/module.h"
36 #include "hw/timer/digic-timer.h"
37 #include "migration/vmstate.h"
39 static const VMStateDescription vmstate_digic_timer = {
40 .name = "digic.timer",
42 .minimum_version_id = 1,
43 .fields = (VMStateField[]) {
44 VMSTATE_PTIMER(ptimer, DigicTimerState),
45 VMSTATE_UINT32(control, DigicTimerState),
46 VMSTATE_UINT32(relvalue, DigicTimerState),
51 static void digic_timer_reset(DeviceState *dev)
53 DigicTimerState *s = DIGIC_TIMER(dev);
55 ptimer_stop(s->ptimer);
60 static uint64_t digic_timer_read(void *opaque, hwaddr offset, unsigned size)
62 DigicTimerState *s = opaque;
66 case DIGIC_TIMER_CONTROL:
69 case DIGIC_TIMER_RELVALUE:
72 case DIGIC_TIMER_VALUE:
73 ret = ptimer_get_count(s->ptimer) & 0xffff;
76 qemu_log_mask(LOG_UNIMP,
77 "digic-timer: read access to unknown register 0x"
78 TARGET_FMT_plx "\n", offset);
84 static void digic_timer_write(void *opaque, hwaddr offset,
85 uint64_t value, unsigned size)
87 DigicTimerState *s = opaque;
90 case DIGIC_TIMER_CONTROL:
91 if (value & DIGIC_TIMER_CONTROL_RST) {
92 digic_timer_reset((DeviceState *)s);
96 if (value & DIGIC_TIMER_CONTROL_EN) {
97 ptimer_run(s->ptimer, 0);
100 s->control = (uint32_t)value;
103 case DIGIC_TIMER_RELVALUE:
104 s->relvalue = extract32(value, 0, 16);
105 ptimer_set_limit(s->ptimer, s->relvalue, 1);
108 case DIGIC_TIMER_VALUE:
112 qemu_log_mask(LOG_UNIMP,
113 "digic-timer: read access to unknown register 0x"
114 TARGET_FMT_plx "\n", offset);
118 static const MemoryRegionOps digic_timer_ops = {
119 .read = digic_timer_read,
120 .write = digic_timer_write,
122 .min_access_size = 4,
123 .max_access_size = 4,
125 .endianness = DEVICE_NATIVE_ENDIAN,
128 static void digic_timer_init(Object *obj)
130 DigicTimerState *s = DIGIC_TIMER(obj);
132 s->ptimer = ptimer_init(NULL, PTIMER_POLICY_DEFAULT);
135 * FIXME: there is no documentation on Digic timer
136 * frequency setup so let it always run at 1 MHz
138 ptimer_set_freq(s->ptimer, 1 * 1000 * 1000);
140 memory_region_init_io(&s->iomem, OBJECT(s), &digic_timer_ops, s,
141 TYPE_DIGIC_TIMER, 0x100);
142 sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->iomem);
145 static void digic_timer_class_init(ObjectClass *klass, void *class_data)
147 DeviceClass *dc = DEVICE_CLASS(klass);
149 dc->reset = digic_timer_reset;
150 dc->vmsd = &vmstate_digic_timer;
153 static const TypeInfo digic_timer_info = {
154 .name = TYPE_DIGIC_TIMER,
155 .parent = TYPE_SYS_BUS_DEVICE,
156 .instance_size = sizeof(DigicTimerState),
157 .instance_init = digic_timer_init,
158 .class_init = digic_timer_class_init,
161 static void digic_timer_register_type(void)
163 type_register_static(&digic_timer_info);
166 type_init(digic_timer_register_type)