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 "qemu/osdep.h"
30 #include "hw/sysbus.h"
31 #include "hw/ptimer.h"
32 #include "qemu/module.h"
35 #include "hw/timer/digic-timer.h"
36 #include "migration/vmstate.h"
38 static const VMStateDescription vmstate_digic_timer
= {
39 .name
= "digic.timer",
41 .minimum_version_id
= 1,
42 .fields
= (const VMStateField
[]) {
43 VMSTATE_PTIMER(ptimer
, DigicTimerState
),
44 VMSTATE_UINT32(control
, DigicTimerState
),
45 VMSTATE_UINT32(relvalue
, DigicTimerState
),
50 static void digic_timer_reset(DeviceState
*dev
)
52 DigicTimerState
*s
= DIGIC_TIMER(dev
);
54 ptimer_transaction_begin(s
->ptimer
);
55 ptimer_stop(s
->ptimer
);
56 ptimer_transaction_commit(s
->ptimer
);
61 static uint64_t digic_timer_read(void *opaque
, hwaddr offset
, unsigned size
)
63 DigicTimerState
*s
= opaque
;
67 case DIGIC_TIMER_CONTROL
:
70 case DIGIC_TIMER_RELVALUE
:
73 case DIGIC_TIMER_VALUE
:
74 ret
= ptimer_get_count(s
->ptimer
) & 0xffff;
77 qemu_log_mask(LOG_UNIMP
,
78 "digic-timer: read access to unknown register 0x"
79 HWADDR_FMT_plx
"\n", offset
);
85 static void digic_timer_write(void *opaque
, hwaddr offset
,
86 uint64_t value
, unsigned size
)
88 DigicTimerState
*s
= opaque
;
91 case DIGIC_TIMER_CONTROL
:
92 if (value
& DIGIC_TIMER_CONTROL_RST
) {
93 digic_timer_reset((DeviceState
*)s
);
97 ptimer_transaction_begin(s
->ptimer
);
98 if (value
& DIGIC_TIMER_CONTROL_EN
) {
99 ptimer_run(s
->ptimer
, 0);
102 s
->control
= (uint32_t)value
;
103 ptimer_transaction_commit(s
->ptimer
);
106 case DIGIC_TIMER_RELVALUE
:
107 s
->relvalue
= extract32(value
, 0, 16);
108 ptimer_transaction_begin(s
->ptimer
);
109 ptimer_set_limit(s
->ptimer
, s
->relvalue
, 1);
110 ptimer_transaction_commit(s
->ptimer
);
113 case DIGIC_TIMER_VALUE
:
117 qemu_log_mask(LOG_UNIMP
,
118 "digic-timer: read access to unknown register 0x"
119 HWADDR_FMT_plx
"\n", offset
);
123 static const MemoryRegionOps digic_timer_ops
= {
124 .read
= digic_timer_read
,
125 .write
= digic_timer_write
,
127 .min_access_size
= 4,
128 .max_access_size
= 4,
130 .endianness
= DEVICE_NATIVE_ENDIAN
,
133 static void digic_timer_tick(void *opaque
)
135 /* Nothing to do on timer rollover */
138 static void digic_timer_init(Object
*obj
)
140 DigicTimerState
*s
= DIGIC_TIMER(obj
);
142 s
->ptimer
= ptimer_init(digic_timer_tick
, NULL
, PTIMER_POLICY_LEGACY
);
145 * FIXME: there is no documentation on Digic timer
146 * frequency setup so let it always run at 1 MHz
148 ptimer_transaction_begin(s
->ptimer
);
149 ptimer_set_freq(s
->ptimer
, 1 * 1000 * 1000);
150 ptimer_transaction_commit(s
->ptimer
);
152 memory_region_init_io(&s
->iomem
, OBJECT(s
), &digic_timer_ops
, s
,
153 TYPE_DIGIC_TIMER
, 0x100);
154 sysbus_init_mmio(SYS_BUS_DEVICE(obj
), &s
->iomem
);
157 static void digic_timer_finalize(Object
*obj
)
159 DigicTimerState
*s
= DIGIC_TIMER(obj
);
161 ptimer_free(s
->ptimer
);
164 static void digic_timer_class_init(ObjectClass
*klass
, void *class_data
)
166 DeviceClass
*dc
= DEVICE_CLASS(klass
);
168 dc
->reset
= digic_timer_reset
;
169 dc
->vmsd
= &vmstate_digic_timer
;
172 static const TypeInfo digic_timer_info
= {
173 .name
= TYPE_DIGIC_TIMER
,
174 .parent
= TYPE_SYS_BUS_DEVICE
,
175 .instance_size
= sizeof(DigicTimerState
),
176 .instance_init
= digic_timer_init
,
177 .instance_finalize
= digic_timer_finalize
,
178 .class_init
= digic_timer_class_init
,
181 static void digic_timer_register_type(void)
183 type_register_static(&digic_timer_info
);
186 type_init(digic_timer_register_type
)