2 * QEMU model of the Canon DIGIC timer block.
4 * Copyright (C) 2013 Antony Pavlov <antonynpavlov@gmail.com>
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 "hw/sysbus.h"
30 #include "hw/ptimer.h"
31 #include "qemu/main-loop.h"
33 #include "hw/timer/digic-timer.h"
35 static const VMStateDescription vmstate_digic_timer
= {
36 .name
= "digic.timer",
38 .minimum_version_id
= 1,
39 .fields
= (VMStateField
[]) {
40 VMSTATE_PTIMER(ptimer
, DigicTimerState
),
41 VMSTATE_UINT32(control
, DigicTimerState
),
42 VMSTATE_UINT32(relvalue
, DigicTimerState
),
47 static void digic_timer_reset(DeviceState
*dev
)
49 DigicTimerState
*s
= DIGIC_TIMER(dev
);
51 ptimer_stop(s
->ptimer
);
56 static uint64_t digic_timer_read(void *opaque
, hwaddr offset
, unsigned size
)
58 DigicTimerState
*s
= opaque
;
62 case DIGIC_TIMER_CONTROL
:
65 case DIGIC_TIMER_RELVALUE
:
68 case DIGIC_TIMER_VALUE
:
69 ret
= ptimer_get_count(s
->ptimer
) & 0xffff;
72 qemu_log_mask(LOG_UNIMP
,
73 "digic-timer: read access to unknown register 0x"
74 TARGET_FMT_plx
, offset
);
80 static void digic_timer_write(void *opaque
, hwaddr offset
,
81 uint64_t value
, unsigned size
)
83 DigicTimerState
*s
= opaque
;
86 case DIGIC_TIMER_CONTROL
:
87 if (value
& DIGIC_TIMER_CONTROL_RST
) {
88 digic_timer_reset((DeviceState
*)s
);
92 if (value
& DIGIC_TIMER_CONTROL_EN
) {
93 ptimer_run(s
->ptimer
, 0);
96 s
->control
= (uint32_t)value
;
99 case DIGIC_TIMER_RELVALUE
:
100 s
->relvalue
= extract32(value
, 0, 16);
101 ptimer_set_limit(s
->ptimer
, s
->relvalue
, 1);
104 case DIGIC_TIMER_VALUE
:
108 qemu_log_mask(LOG_UNIMP
,
109 "digic-timer: read access to unknown register 0x"
110 TARGET_FMT_plx
, offset
);
114 static const MemoryRegionOps digic_timer_ops
= {
115 .read
= digic_timer_read
,
116 .write
= digic_timer_write
,
118 .min_access_size
= 4,
119 .max_access_size
= 4,
121 .endianness
= DEVICE_NATIVE_ENDIAN
,
124 static void digic_timer_init(Object
*obj
)
126 DigicTimerState
*s
= DIGIC_TIMER(obj
);
128 s
->ptimer
= ptimer_init(NULL
);
131 * FIXME: there is no documentation on Digic timer
132 * frequency setup so let it always run at 1 MHz
134 ptimer_set_freq(s
->ptimer
, 1 * 1000 * 1000);
136 memory_region_init_io(&s
->iomem
, OBJECT(s
), &digic_timer_ops
, s
,
137 TYPE_DIGIC_TIMER
, 0x100);
138 sysbus_init_mmio(SYS_BUS_DEVICE(obj
), &s
->iomem
);
141 static void digic_timer_class_init(ObjectClass
*klass
, void *class_data
)
143 DeviceClass
*dc
= DEVICE_CLASS(klass
);
145 dc
->reset
= digic_timer_reset
;
146 dc
->vmsd
= &vmstate_digic_timer
;
149 static const TypeInfo digic_timer_info
= {
150 .name
= TYPE_DIGIC_TIMER
,
151 .parent
= TYPE_SYS_BUS_DEVICE
,
152 .instance_size
= sizeof(DigicTimerState
),
153 .instance_init
= digic_timer_init
,
154 .class_init
= digic_timer_class_init
,
157 static void digic_timer_register_type(void)
159 type_register_static(&digic_timer_info
);
162 type_init(digic_timer_register_type
)