blob: 560cb914c569a0d1140798baa71c1c9d05016bd9 [file] [log] [blame]
Jia Liu99f575e2012-07-20 15:50:47 +08001/*
2 * QEMU OpenRISC timer support
3 *
4 * Copyright (c) 2011-2012 Jia Liu <proljc@gmail.com>
5 * Zhizhou Zhang <etouzh@gmail.com>
6 *
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 of the License, or (at your option) any later version.
11 *
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.
16 *
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/>.
19 */
20
21#include "cpu.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010022#include "hw/hw.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010023#include "qemu/timer.h"
Jia Liu99f575e2012-07-20 15:50:47 +080024
Laurent Vivierccaf1742015-08-25 17:17:15 +020025#define TIMER_PERIOD 50 /* 50 ns period for 20 MHz timer */
Jia Liu99f575e2012-07-20 15:50:47 +080026
27/* The time when TTCR changes */
28static uint64_t last_clk;
29static int is_counting;
30
31void cpu_openrisc_count_update(OpenRISCCPU *cpu)
32{
Sebastian Macked5155212013-10-22 02:12:41 +020033 uint64_t now;
Jia Liu99f575e2012-07-20 15:50:47 +080034
Jia Liu99f575e2012-07-20 15:50:47 +080035 if (!is_counting) {
Jia Liu99f575e2012-07-20 15:50:47 +080036 return;
37 }
Sebastian Macked5155212013-10-22 02:12:41 +020038 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Laurent Vivierccaf1742015-08-25 17:17:15 +020039 cpu->env.ttcr += (uint32_t)((now - last_clk) / TIMER_PERIOD);
Jia Liu99f575e2012-07-20 15:50:47 +080040 last_clk = now;
Sebastian Macked5155212013-10-22 02:12:41 +020041}
42
43void cpu_openrisc_timer_update(OpenRISCCPU *cpu)
44{
45 uint32_t wait;
46 uint64_t now, next;
47
48 if (!is_counting) {
49 return;
50 }
51
52 cpu_openrisc_count_update(cpu);
53 now = last_clk;
Jia Liu99f575e2012-07-20 15:50:47 +080054
55 if ((cpu->env.ttmr & TTMR_TP) <= (cpu->env.ttcr & TTMR_TP)) {
56 wait = TTMR_TP - (cpu->env.ttcr & TTMR_TP) + 1;
57 wait += cpu->env.ttmr & TTMR_TP;
58 } else {
59 wait = (cpu->env.ttmr & TTMR_TP) - (cpu->env.ttcr & TTMR_TP);
60 }
Laurent Vivierccaf1742015-08-25 17:17:15 +020061 next = now + (uint64_t)wait * TIMER_PERIOD;
Alex Blighbc72ad62013-08-21 16:03:08 +010062 timer_mod(cpu->env.timer, next);
Jia Liu99f575e2012-07-20 15:50:47 +080063}
64
65void cpu_openrisc_count_start(OpenRISCCPU *cpu)
66{
67 is_counting = 1;
68 cpu_openrisc_count_update(cpu);
69}
70
71void cpu_openrisc_count_stop(OpenRISCCPU *cpu)
72{
Sebastian Macked5155212013-10-22 02:12:41 +020073 timer_del(cpu->env.timer);
Jia Liu99f575e2012-07-20 15:50:47 +080074 cpu_openrisc_count_update(cpu);
Sebastian Macked5155212013-10-22 02:12:41 +020075 is_counting = 0;
Jia Liu99f575e2012-07-20 15:50:47 +080076}
77
78static void openrisc_timer_cb(void *opaque)
79{
80 OpenRISCCPU *cpu = opaque;
81
82 if ((cpu->env.ttmr & TTMR_IE) &&
Alex Blighbc72ad62013-08-21 16:03:08 +010083 timer_expired(cpu->env.timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL))) {
Andreas Färber259186a2013-01-17 18:51:17 +010084 CPUState *cs = CPU(cpu);
85
Jia Liu99f575e2012-07-20 15:50:47 +080086 cpu->env.ttmr |= TTMR_IP;
Andreas Färber259186a2013-01-17 18:51:17 +010087 cs->interrupt_request |= CPU_INTERRUPT_TIMER;
Jia Liu99f575e2012-07-20 15:50:47 +080088 }
89
90 switch (cpu->env.ttmr & TTMR_M) {
91 case TIMER_NONE:
92 break;
93 case TIMER_INTR:
94 cpu->env.ttcr = 0;
Jia Liu99f575e2012-07-20 15:50:47 +080095 break;
96 case TIMER_SHOT:
97 cpu_openrisc_count_stop(cpu);
98 break;
99 case TIMER_CONT:
Jia Liu99f575e2012-07-20 15:50:47 +0800100 break;
101 }
Sebastian Macked5155212013-10-22 02:12:41 +0200102
103 cpu_openrisc_timer_update(cpu);
Jia Liu99f575e2012-07-20 15:50:47 +0800104}
105
106void cpu_openrisc_clock_init(OpenRISCCPU *cpu)
107{
Alex Blighbc72ad62013-08-21 16:03:08 +0100108 cpu->env.timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, &openrisc_timer_cb, cpu);
Jia Liu99f575e2012-07-20 15:50:47 +0800109 cpu->env.ttmr = 0x00000000;
110 cpu->env.ttcr = 0x00000000;
111}