2 * QEMU lowRISC Ibex Timer device
4 * Copyright (c) 2021 Western Digital
6 * For details check the documentation here:
7 * https://docs.opentitan.org/hw/ip/rv_timer/doc/
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #include "qemu/osdep.h"
30 #include "qemu/timer.h"
31 #include "hw/timer/ibex_timer.h"
33 #include "hw/qdev-properties.h"
34 #include "target/riscv/cpu.h"
35 #include "migration/vmstate.h"
37 REG32(ALERT_TEST
, 0x00)
38 FIELD(ALERT_TEST
, FATAL_FAULT
, 0, 1)
40 FIELD(CTRL
, ACTIVE
, 0, 1)
42 FIELD(CFG0
, PRESCALE
, 0, 12)
43 FIELD(CFG0
, STEP
, 16, 8)
46 REG32(COMPARE_LOWER0
, 0x10C)
47 REG32(COMPARE_UPPER0
, 0x110)
48 REG32(INTR_ENABLE
, 0x114)
49 FIELD(INTR_ENABLE
, IE_0
, 0, 1)
50 REG32(INTR_STATE
, 0x118)
51 FIELD(INTR_STATE
, IS_0
, 0, 1)
52 REG32(INTR_TEST
, 0x11C)
53 FIELD(INTR_TEST
, T_0
, 0, 1)
55 static uint64_t cpu_riscv_read_rtc(uint32_t timebase_freq
)
57 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
58 timebase_freq
, NANOSECONDS_PER_SECOND
);
61 static void ibex_timer_update_irqs(IbexTimerState
*s
)
63 uint64_t value
= s
->timer_compare_lower0
|
64 ((uint64_t)s
->timer_compare_upper0
<< 32);
66 uint64_t now
= cpu_riscv_read_rtc(s
->timebase_freq
);
68 if (!(s
->timer_ctrl
& R_CTRL_ACTIVE_MASK
)) {
69 /* Timer isn't active */
73 /* Update the CPUs mtimecmp */
76 if (s
->mtimecmp
<= now
) {
78 * If the mtimecmp was in the past raise the interrupt now.
80 qemu_irq_raise(s
->m_timer_irq
);
81 if (s
->timer_intr_enable
& R_INTR_ENABLE_IE_0_MASK
) {
82 s
->timer_intr_state
|= R_INTR_STATE_IS_0_MASK
;
83 qemu_set_irq(s
->irq
, true);
88 /* Setup a timer to trigger the interrupt in the future */
89 qemu_irq_lower(s
->m_timer_irq
);
90 qemu_set_irq(s
->irq
, false);
92 diff
= s
->mtimecmp
- now
;
93 next
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
95 NANOSECONDS_PER_SECOND
,
98 if (next
< qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
)) {
99 /* We overflowed the timer, just set it as large as we can */
100 timer_mod(s
->mtimer
, 0x7FFFFFFFFFFFFFFF);
102 timer_mod(s
->mtimer
, next
);
106 static void ibex_timer_cb(void *opaque
)
108 IbexTimerState
*s
= opaque
;
110 qemu_irq_raise(s
->m_timer_irq
);
111 if (s
->timer_intr_enable
& R_INTR_ENABLE_IE_0_MASK
) {
112 s
->timer_intr_state
|= R_INTR_STATE_IS_0_MASK
;
113 qemu_set_irq(s
->irq
, true);
117 static void ibex_timer_reset(DeviceState
*dev
)
119 IbexTimerState
*s
= IBEX_TIMER(dev
);
121 s
->mtimer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
125 s
->timer_ctrl
= 0x00000000;
126 s
->timer_cfg0
= 0x00010000;
127 s
->timer_compare_lower0
= 0xFFFFFFFF;
128 s
->timer_compare_upper0
= 0xFFFFFFFF;
129 s
->timer_intr_enable
= 0x00000000;
130 s
->timer_intr_state
= 0x00000000;
132 ibex_timer_update_irqs(s
);
135 static uint64_t ibex_timer_read(void *opaque
, hwaddr addr
,
138 IbexTimerState
*s
= opaque
;
139 uint64_t now
= cpu_riscv_read_rtc(s
->timebase_freq
);
140 uint64_t retvalue
= 0;
144 qemu_log_mask(LOG_GUEST_ERROR
,
145 "Attempted to read ALERT_TEST, a write only register");
148 retvalue
= s
->timer_ctrl
;
151 retvalue
= s
->timer_cfg0
;
157 retvalue
= now
>> 32;
159 case R_COMPARE_LOWER0
:
160 retvalue
= s
->timer_compare_lower0
;
162 case R_COMPARE_UPPER0
:
163 retvalue
= s
->timer_compare_upper0
;
166 retvalue
= s
->timer_intr_enable
;
169 retvalue
= s
->timer_intr_state
;
172 qemu_log_mask(LOG_GUEST_ERROR
,
173 "Attempted to read INTR_TEST, a write only register");
176 qemu_log_mask(LOG_GUEST_ERROR
,
177 "%s: Bad offset 0x%"HWADDR_PRIx
"\n", __func__
, addr
);
184 static void ibex_timer_write(void *opaque
, hwaddr addr
,
185 uint64_t val64
, unsigned int size
)
187 IbexTimerState
*s
= opaque
;
188 uint32_t val
= val64
;
192 qemu_log_mask(LOG_UNIMP
, "Alert triggering not supported");
198 qemu_log_mask(LOG_UNIMP
, "Changing prescale or step not supported");
202 qemu_log_mask(LOG_UNIMP
, "Changing timer value is not supported");
205 qemu_log_mask(LOG_UNIMP
, "Changing timer value is not supported");
207 case R_COMPARE_LOWER0
:
208 s
->timer_compare_lower0
= val
;
209 ibex_timer_update_irqs(s
);
211 case R_COMPARE_UPPER0
:
212 s
->timer_compare_upper0
= val
;
213 ibex_timer_update_irqs(s
);
216 s
->timer_intr_enable
= val
;
219 /* Write 1 to clear */
220 s
->timer_intr_state
&= ~val
;
223 if (s
->timer_intr_enable
& val
& R_INTR_ENABLE_IE_0_MASK
) {
224 s
->timer_intr_state
|= R_INTR_STATE_IS_0_MASK
;
225 qemu_set_irq(s
->irq
, true);
229 qemu_log_mask(LOG_GUEST_ERROR
,
230 "%s: Bad offset 0x%"HWADDR_PRIx
"\n", __func__
, addr
);
234 static const MemoryRegionOps ibex_timer_ops
= {
235 .read
= ibex_timer_read
,
236 .write
= ibex_timer_write
,
237 .endianness
= DEVICE_NATIVE_ENDIAN
,
238 .impl
.min_access_size
= 4,
239 .impl
.max_access_size
= 4,
242 static int ibex_timer_post_load(void *opaque
, int version_id
)
244 IbexTimerState
*s
= opaque
;
246 ibex_timer_update_irqs(s
);
250 static const VMStateDescription vmstate_ibex_timer
= {
251 .name
= TYPE_IBEX_TIMER
,
253 .minimum_version_id
= 2,
254 .post_load
= ibex_timer_post_load
,
255 .fields
= (VMStateField
[]) {
256 VMSTATE_UINT32(timer_ctrl
, IbexTimerState
),
257 VMSTATE_UINT32(timer_cfg0
, IbexTimerState
),
258 VMSTATE_UINT32(timer_compare_lower0
, IbexTimerState
),
259 VMSTATE_UINT32(timer_compare_upper0
, IbexTimerState
),
260 VMSTATE_UINT32(timer_intr_enable
, IbexTimerState
),
261 VMSTATE_UINT32(timer_intr_state
, IbexTimerState
),
262 VMSTATE_END_OF_LIST()
266 static Property ibex_timer_properties
[] = {
267 DEFINE_PROP_UINT32("timebase-freq", IbexTimerState
, timebase_freq
, 10000),
268 DEFINE_PROP_END_OF_LIST(),
271 static void ibex_timer_init(Object
*obj
)
273 IbexTimerState
*s
= IBEX_TIMER(obj
);
275 sysbus_init_irq(SYS_BUS_DEVICE(obj
), &s
->irq
);
277 memory_region_init_io(&s
->mmio
, obj
, &ibex_timer_ops
, s
,
278 TYPE_IBEX_TIMER
, 0x400);
279 sysbus_init_mmio(SYS_BUS_DEVICE(obj
), &s
->mmio
);
282 static void ibex_timer_realize(DeviceState
*dev
, Error
**errp
)
284 IbexTimerState
*s
= IBEX_TIMER(dev
);
286 qdev_init_gpio_out(dev
, &s
->m_timer_irq
, 1);
290 static void ibex_timer_class_init(ObjectClass
*klass
, void *data
)
292 DeviceClass
*dc
= DEVICE_CLASS(klass
);
294 dc
->reset
= ibex_timer_reset
;
295 dc
->vmsd
= &vmstate_ibex_timer
;
296 dc
->realize
= ibex_timer_realize
;
297 device_class_set_props(dc
, ibex_timer_properties
);
300 static const TypeInfo ibex_timer_info
= {
301 .name
= TYPE_IBEX_TIMER
,
302 .parent
= TYPE_SYS_BUS_DEVICE
,
303 .instance_size
= sizeof(IbexTimerState
),
304 .instance_init
= ibex_timer_init
,
305 .class_init
= ibex_timer_class_init
,
308 static void ibex_timer_register_types(void)
310 type_register_static(&ibex_timer_info
);
313 type_init(ibex_timer_register_types
)