4 * Copyright (c) 2014 Alistair Francis <alistair@alistair23.me>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include "hw/timer/stm32f2xx_timer.h"
28 #ifndef STM_TIMER_ERR_DEBUG
29 #define STM_TIMER_ERR_DEBUG 0
32 #define DB_PRINT_L(lvl, fmt, args...) do { \
33 if (STM_TIMER_ERR_DEBUG >= lvl) { \
34 qemu_log("%s: " fmt, __func__, ## args); \
38 #define DB_PRINT(fmt, args...) DB_PRINT_L(1, fmt, ## args)
40 static void stm32f2xx_timer_set_alarm(STM32F2XXTimerState
*s
, int64_t now
);
42 static void stm32f2xx_timer_interrupt(void *opaque
)
44 STM32F2XXTimerState
*s
= opaque
;
46 DB_PRINT("Interrupt\n");
48 if (s
->tim_dier
& TIM_DIER_UIE
&& s
->tim_cr1
& TIM_CR1_CEN
) {
50 qemu_irq_pulse(s
->irq
);
51 stm32f2xx_timer_set_alarm(s
, s
->hit_time
);
55 static inline int64_t stm32f2xx_ns_to_ticks(STM32F2XXTimerState
*s
, int64_t t
)
57 return muldiv64(t
, s
->freq_hz
, 1000000000ULL) / (s
->tim_psc
+ 1);
60 static void stm32f2xx_timer_set_alarm(STM32F2XXTimerState
*s
, int64_t now
)
65 if (s
->tim_arr
== 0) {
69 DB_PRINT("Alarm set at: 0x%x\n", s
->tim_cr1
);
71 now_ticks
= stm32f2xx_ns_to_ticks(s
, now
);
72 ticks
= s
->tim_arr
- (now_ticks
- s
->tick_offset
);
74 DB_PRINT("Alarm set in %d ticks\n", (int) ticks
);
76 s
->hit_time
= muldiv64((ticks
+ (uint64_t) now_ticks
) * (s
->tim_psc
+ 1),
77 1000000000ULL, s
->freq_hz
);
79 timer_mod(s
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + s
->hit_time
);
80 DB_PRINT("Wait Time: %" PRId64
" ticks\n", s
->hit_time
);
83 static void stm32f2xx_timer_reset(DeviceState
*dev
)
85 STM32F2XXTimerState
*s
= STM32F2XXTIMER(dev
);
86 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
107 s
->tick_offset
= stm32f2xx_ns_to_ticks(s
, now
);
110 static uint64_t stm32f2xx_timer_read(void *opaque
, hwaddr offset
,
113 STM32F2XXTimerState
*s
= opaque
;
115 DB_PRINT("Read 0x%"HWADDR_PRIx
"\n", offset
);
137 return stm32f2xx_ns_to_ticks(s
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
)) -
158 qemu_log_mask(LOG_GUEST_ERROR
,
159 "%s: Bad offset 0x%"HWADDR_PRIx
"\n", __func__
, offset
);
165 static void stm32f2xx_timer_write(void *opaque
, hwaddr offset
,
166 uint64_t val64
, unsigned size
)
168 STM32F2XXTimerState
*s
= opaque
;
169 uint32_t value
= val64
;
170 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
171 uint32_t timer_val
= 0;
173 DB_PRINT("Write 0x%x, 0x%"HWADDR_PRIx
"\n", value
, offset
);
189 /* This is set by hardware and cleared by software */
194 if (s
->tim_egr
& TIM_EGR_UG
) {
200 s
->tim_ccmr1
= value
;
203 s
->tim_ccmr2
= value
;
209 timer_val
= stm32f2xx_ns_to_ticks(s
, now
) - s
->tick_offset
;
218 stm32f2xx_timer_set_alarm(s
, now
);
242 qemu_log_mask(LOG_GUEST_ERROR
,
243 "%s: Bad offset 0x%"HWADDR_PRIx
"\n", __func__
, offset
);
247 /* This means that a register write has affected the timer in a way that
248 * requires a refresh of both tick_offset and the alarm.
250 s
->tick_offset
= stm32f2xx_ns_to_ticks(s
, now
) - timer_val
;
251 stm32f2xx_timer_set_alarm(s
, now
);
254 static const MemoryRegionOps stm32f2xx_timer_ops
= {
255 .read
= stm32f2xx_timer_read
,
256 .write
= stm32f2xx_timer_write
,
257 .endianness
= DEVICE_NATIVE_ENDIAN
,
260 static const VMStateDescription vmstate_stm32f2xx_timer
= {
261 .name
= TYPE_STM32F2XX_TIMER
,
263 .minimum_version_id
= 1,
264 .fields
= (VMStateField
[]) {
265 VMSTATE_INT64(tick_offset
, STM32F2XXTimerState
),
266 VMSTATE_UINT32(tim_cr1
, STM32F2XXTimerState
),
267 VMSTATE_UINT32(tim_cr2
, STM32F2XXTimerState
),
268 VMSTATE_UINT32(tim_smcr
, STM32F2XXTimerState
),
269 VMSTATE_UINT32(tim_dier
, STM32F2XXTimerState
),
270 VMSTATE_UINT32(tim_sr
, STM32F2XXTimerState
),
271 VMSTATE_UINT32(tim_egr
, STM32F2XXTimerState
),
272 VMSTATE_UINT32(tim_ccmr1
, STM32F2XXTimerState
),
273 VMSTATE_UINT32(tim_ccmr2
, STM32F2XXTimerState
),
274 VMSTATE_UINT32(tim_ccer
, STM32F2XXTimerState
),
275 VMSTATE_UINT32(tim_psc
, STM32F2XXTimerState
),
276 VMSTATE_UINT32(tim_arr
, STM32F2XXTimerState
),
277 VMSTATE_UINT32(tim_ccr1
, STM32F2XXTimerState
),
278 VMSTATE_UINT32(tim_ccr2
, STM32F2XXTimerState
),
279 VMSTATE_UINT32(tim_ccr3
, STM32F2XXTimerState
),
280 VMSTATE_UINT32(tim_ccr4
, STM32F2XXTimerState
),
281 VMSTATE_UINT32(tim_dcr
, STM32F2XXTimerState
),
282 VMSTATE_UINT32(tim_dmar
, STM32F2XXTimerState
),
283 VMSTATE_UINT32(tim_or
, STM32F2XXTimerState
),
284 VMSTATE_END_OF_LIST()
288 static Property stm32f2xx_timer_properties
[] = {
289 DEFINE_PROP_UINT64("clock-frequency", struct STM32F2XXTimerState
,
290 freq_hz
, 1000000000),
291 DEFINE_PROP_END_OF_LIST(),
294 static void stm32f2xx_timer_init(Object
*obj
)
296 STM32F2XXTimerState
*s
= STM32F2XXTIMER(obj
);
298 sysbus_init_irq(SYS_BUS_DEVICE(obj
), &s
->irq
);
300 memory_region_init_io(&s
->iomem
, obj
, &stm32f2xx_timer_ops
, s
,
301 "stm32f2xx_timer", 0x4000);
302 sysbus_init_mmio(SYS_BUS_DEVICE(obj
), &s
->iomem
);
304 s
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, stm32f2xx_timer_interrupt
, s
);
307 static void stm32f2xx_timer_class_init(ObjectClass
*klass
, void *data
)
309 DeviceClass
*dc
= DEVICE_CLASS(klass
);
311 dc
->reset
= stm32f2xx_timer_reset
;
312 dc
->props
= stm32f2xx_timer_properties
;
313 dc
->vmsd
= &vmstate_stm32f2xx_timer
;
316 static const TypeInfo stm32f2xx_timer_info
= {
317 .name
= TYPE_STM32F2XX_TIMER
,
318 .parent
= TYPE_SYS_BUS_DEVICE
,
319 .instance_size
= sizeof(STM32F2XXTimerState
),
320 .instance_init
= stm32f2xx_timer_init
,
321 .class_init
= stm32f2xx_timer_class_init
,
324 static void stm32f2xx_timer_register_types(void)
326 type_register_static(&stm32f2xx_timer_info
);
329 type_init(stm32f2xx_timer_register_types
)