2 * QEMU OpenRISC timer support
4 * Copyright (c) 2011-2012 Jia Liu <proljc@gmail.com>
5 * Zhizhou Zhang <etouzh@gmail.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
23 #include "migration/vmstate.h"
24 #include "qemu/timer.h"
25 #include "sysemu/reset.h"
27 #define TIMER_PERIOD 50 /* 50 ns period for 20 MHz timer */
29 /* Tick Timer global state to allow all cores to be in sync */
30 typedef struct OR1KTimerState
{
35 static OR1KTimerState
*or1k_timer
;
37 void cpu_openrisc_count_set(OpenRISCCPU
*cpu
, uint32_t val
)
39 or1k_timer
->ttcr
= val
;
42 uint32_t cpu_openrisc_count_get(OpenRISCCPU
*cpu
)
44 return or1k_timer
->ttcr
;
47 /* Add elapsed ticks to ttcr */
48 void cpu_openrisc_count_update(OpenRISCCPU
*cpu
)
52 if (!cpu
->env
.is_counting
) {
55 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
56 or1k_timer
->ttcr
+= (uint32_t)((now
- or1k_timer
->last_clk
)
58 or1k_timer
->last_clk
= now
;
61 /* Update the next timeout time as difference between ttmr and ttcr */
62 void cpu_openrisc_timer_update(OpenRISCCPU
*cpu
)
67 if (!cpu
->env
.is_counting
) {
71 cpu_openrisc_count_update(cpu
);
72 now
= or1k_timer
->last_clk
;
74 if ((cpu
->env
.ttmr
& TTMR_TP
) <= (or1k_timer
->ttcr
& TTMR_TP
)) {
75 wait
= TTMR_TP
- (or1k_timer
->ttcr
& TTMR_TP
) + 1;
76 wait
+= cpu
->env
.ttmr
& TTMR_TP
;
78 wait
= (cpu
->env
.ttmr
& TTMR_TP
) - (or1k_timer
->ttcr
& TTMR_TP
);
80 next
= now
+ (uint64_t)wait
* TIMER_PERIOD
;
81 timer_mod(cpu
->env
.timer
, next
);
84 void cpu_openrisc_count_start(OpenRISCCPU
*cpu
)
86 cpu
->env
.is_counting
= 1;
87 cpu_openrisc_count_update(cpu
);
90 void cpu_openrisc_count_stop(OpenRISCCPU
*cpu
)
92 timer_del(cpu
->env
.timer
);
93 cpu_openrisc_count_update(cpu
);
94 cpu
->env
.is_counting
= 0;
97 static void openrisc_timer_cb(void *opaque
)
99 OpenRISCCPU
*cpu
= opaque
;
101 if ((cpu
->env
.ttmr
& TTMR_IE
) &&
102 timer_expired(cpu
->env
.timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
))) {
103 CPUState
*cs
= CPU(cpu
);
105 cpu
->env
.ttmr
|= TTMR_IP
;
106 cs
->interrupt_request
|= CPU_INTERRUPT_TIMER
;
109 switch (cpu
->env
.ttmr
& TTMR_M
) {
113 or1k_timer
->ttcr
= 0;
116 cpu_openrisc_count_stop(cpu
);
122 cpu_openrisc_timer_update(cpu
);
123 qemu_cpu_kick(CPU(cpu
));
126 /* Reset the per CPU counter state. */
127 static void openrisc_count_reset(void *opaque
)
129 OpenRISCCPU
*cpu
= opaque
;
131 if (cpu
->env
.is_counting
) {
132 cpu_openrisc_count_stop(cpu
);
134 cpu
->env
.ttmr
= 0x00000000;
137 /* Reset the global timer state. */
138 static void openrisc_timer_reset(void *opaque
)
140 or1k_timer
->ttcr
= 0x00000000;
141 or1k_timer
->last_clk
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
144 static const VMStateDescription vmstate_or1k_timer
= {
145 .name
= "or1k_timer",
147 .minimum_version_id
= 1,
148 .fields
= (const VMStateField
[]) {
149 VMSTATE_UINT32(ttcr
, OR1KTimerState
),
150 VMSTATE_UINT64(last_clk
, OR1KTimerState
),
151 VMSTATE_END_OF_LIST()
155 void cpu_openrisc_clock_init(OpenRISCCPU
*cpu
)
157 cpu
->env
.timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &openrisc_timer_cb
, cpu
);
159 qemu_register_reset(openrisc_count_reset
, cpu
);
160 if (or1k_timer
== NULL
) {
161 or1k_timer
= g_new0(OR1KTimerState
, 1);
162 qemu_register_reset(openrisc_timer_reset
, cpu
);
163 vmstate_register(NULL
, 0, &vmstate_or1k_timer
, or1k_timer
);