]> git.proxmox.com Git - mirror_qemu.git/blame - hw/slavio_timer.c
find -type f | xargs sed -i 's/[\t ]$//g' # on most files
[mirror_qemu.git] / hw / slavio_timer.c
CommitLineData
e80cfcfc
FB
1/*
2 * QEMU Sparc SLAVIO timer controller emulation
3 *
66321a11 4 * Copyright (c) 2003-2005 Fabrice Bellard
5fafdf24 5 *
e80cfcfc
FB
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
24#include "vl.h"
25
26//#define DEBUG_TIMER
27
66321a11
FB
28#ifdef DEBUG_TIMER
29#define DPRINTF(fmt, args...) \
30do { printf("TIMER: " fmt , ##args); } while (0)
31#else
32#define DPRINTF(fmt, args...)
33#endif
34
e80cfcfc
FB
35/*
36 * Registers of hardware timer in sun4m.
37 *
38 * This is the timer/counter part of chip STP2001 (Slave I/O), also
39 * produced as NCR89C105. See
40 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt
5fafdf24 41 *
e80cfcfc
FB
42 * The 31-bit counter is incremented every 500ns by bit 9. Bits 8..0
43 * are zero. Bit 31 is 1 when count has been reached.
44 *
ba3c64fb
FB
45 * Per-CPU timers interrupt local CPU, system timer uses normal
46 * interrupt routing.
47 *
e80cfcfc
FB
48 */
49
50typedef struct SLAVIO_TIMERState {
d7edfd27 51 qemu_irq irq;
8d05ea8a
BS
52 ptimer_state *timer;
53 uint32_t count, counthigh, reached;
54 uint64_t limit;
8d05ea8a 55 int stopped;
e80cfcfc
FB
56 int mode; // 0 = processor, 1 = user, 2 = system
57} SLAVIO_TIMERState;
58
59#define TIMER_MAXADDR 0x1f
5aca8c3b 60#define TIMER_SIZE (TIMER_MAXADDR + 1)
e80cfcfc
FB
61
62// Update count, set irq, update expire_time
8d05ea8a 63// Convert from ptimer countdown units
e80cfcfc
FB
64static void slavio_timer_get_out(SLAVIO_TIMERState *s)
65{
8d05ea8a 66 uint64_t count;
e80cfcfc 67
8d05ea8a
BS
68 count = s->limit - (ptimer_get_count(s->timer) << 9);
69 DPRINTF("get_out: limit %" PRIx64 " count %x%08x\n", s->limit, s->counthigh,
70 s->count);
71 s->count = count & 0xfffffe00;
72 s->counthigh = count >> 32;
e80cfcfc
FB
73}
74
75// timer callback
76static void slavio_timer_irq(void *opaque)
77{
78 SLAVIO_TIMERState *s = opaque;
79
e80cfcfc 80 slavio_timer_get_out(s);
8d05ea8a
BS
81 DPRINTF("callback: count %x%08x\n", s->counthigh, s->count);
82 s->reached = 0x80000000;
e80cfcfc 83 if (s->mode != 1)
d7edfd27 84 qemu_irq_raise(s->irq);
e80cfcfc
FB
85}
86
87static uint32_t slavio_timer_mem_readl(void *opaque, target_phys_addr_t addr)
88{
89 SLAVIO_TIMERState *s = opaque;
8d05ea8a 90 uint32_t saddr, ret;
e80cfcfc
FB
91
92 saddr = (addr & TIMER_MAXADDR) >> 2;
93 switch (saddr) {
94 case 0:
95 // read limit (system counter mode) or read most signifying
96 // part of counter (user mode)
97 if (s->mode != 1) {
98 // clear irq
d7edfd27 99 qemu_irq_lower(s->irq);
e80cfcfc 100 s->reached = 0;
8d05ea8a 101 ret = s->limit & 0x7fffffff;
e80cfcfc
FB
102 }
103 else {
104 slavio_timer_get_out(s);
8d05ea8a 105 ret = s->counthigh & 0x7fffffff;
e80cfcfc 106 }
8d05ea8a 107 break;
e80cfcfc
FB
108 case 1:
109 // read counter and reached bit (system mode) or read lsbits
110 // of counter (user mode)
111 slavio_timer_get_out(s);
112 if (s->mode != 1)
8d05ea8a 113 ret = (s->count & 0x7fffffff) | s->reached;
e80cfcfc 114 else
8d05ea8a
BS
115 ret = s->count;
116 break;
e80cfcfc
FB
117 case 3:
118 // read start/stop status
8d05ea8a
BS
119 ret = s->stopped;
120 break;
e80cfcfc
FB
121 case 4:
122 // read user/system mode
8d05ea8a
BS
123 ret = s->mode & 1;
124 break;
e80cfcfc 125 default:
8d05ea8a
BS
126 ret = 0;
127 break;
e80cfcfc 128 }
8d05ea8a
BS
129 DPRINTF("read " TARGET_FMT_plx " = %08x\n", addr, ret);
130
131 return ret;
e80cfcfc
FB
132}
133
134static void slavio_timer_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
135{
136 SLAVIO_TIMERState *s = opaque;
137 uint32_t saddr;
8d05ea8a 138 int reload = 0;
e80cfcfc 139
8d05ea8a 140 DPRINTF("write " TARGET_FMT_plx " %08x\n", addr, val);
e80cfcfc
FB
141 saddr = (addr & TIMER_MAXADDR) >> 2;
142 switch (saddr) {
143 case 0:
144 // set limit, reset counter
8d05ea8a 145 reload = 1;
d7edfd27 146 qemu_irq_lower(s->irq);
e80cfcfc
FB
147 // fall through
148 case 2:
149 // set limit without resetting counter
8d05ea8a
BS
150 s->limit = val & 0x7ffffe00ULL;
151 if (!s->limit)
152 s->limit = 0x7ffffe00ULL;
153 ptimer_set_limit(s->timer, s->limit >> 9, reload);
e80cfcfc
FB
154 break;
155 case 3:
156 // start/stop user counter
157 if (s->mode == 1) {
158 if (val & 1) {
8d05ea8a 159 ptimer_stop(s->timer);
e80cfcfc
FB
160 s->stopped = 1;
161 }
162 else {
8d05ea8a 163 ptimer_run(s->timer, 0);
e80cfcfc
FB
164 s->stopped = 0;
165 }
166 }
167 break;
168 case 4:
169 // bit 0: user (1) or system (0) counter mode
170 if (s->mode == 0 || s->mode == 1)
171 s->mode = val & 1;
8d05ea8a 172 if (s->mode == 1) {
d7edfd27 173 qemu_irq_lower(s->irq);
8d05ea8a
BS
174 s->limit = -1ULL;
175 }
176 ptimer_set_limit(s->timer, s->limit >> 9, 1);
e80cfcfc
FB
177 break;
178 default:
179 break;
180 }
181}
182
183static CPUReadMemoryFunc *slavio_timer_mem_read[3] = {
184 slavio_timer_mem_readl,
185 slavio_timer_mem_readl,
186 slavio_timer_mem_readl,
187};
188
189static CPUWriteMemoryFunc *slavio_timer_mem_write[3] = {
190 slavio_timer_mem_writel,
191 slavio_timer_mem_writel,
192 slavio_timer_mem_writel,
193};
194
195static void slavio_timer_save(QEMUFile *f, void *opaque)
196{
197 SLAVIO_TIMERState *s = opaque;
198
8d05ea8a 199 qemu_put_be64s(f, &s->limit);
e80cfcfc
FB
200 qemu_put_be32s(f, &s->count);
201 qemu_put_be32s(f, &s->counthigh);
d7edfd27 202 qemu_put_be32(f, 0); // Was irq
e80cfcfc
FB
203 qemu_put_be32s(f, &s->reached);
204 qemu_put_be32s(f, &s->stopped);
205 qemu_put_be32s(f, &s->mode);
8d05ea8a 206 qemu_put_ptimer(f, s->timer);
e80cfcfc
FB
207}
208
209static int slavio_timer_load(QEMUFile *f, void *opaque, int version_id)
210{
211 SLAVIO_TIMERState *s = opaque;
d7edfd27 212 uint32_t tmp;
5fafdf24 213
8d05ea8a 214 if (version_id != 2)
e80cfcfc
FB
215 return -EINVAL;
216
8d05ea8a 217 qemu_get_be64s(f, &s->limit);
e80cfcfc
FB
218 qemu_get_be32s(f, &s->count);
219 qemu_get_be32s(f, &s->counthigh);
d7edfd27 220 qemu_get_be32s(f, &tmp); // Was irq
e80cfcfc
FB
221 qemu_get_be32s(f, &s->reached);
222 qemu_get_be32s(f, &s->stopped);
223 qemu_get_be32s(f, &s->mode);
8d05ea8a
BS
224 qemu_get_ptimer(f, s->timer);
225
e80cfcfc
FB
226 return 0;
227}
228
229static void slavio_timer_reset(void *opaque)
230{
231 SLAVIO_TIMERState *s = opaque;
232
8d05ea8a 233 s->limit = 0x7ffffe00ULL;
e80cfcfc 234 s->count = 0;
e80cfcfc
FB
235 s->reached = 0;
236 s->mode &= 2;
8d05ea8a
BS
237 ptimer_set_limit(s->timer, s->limit >> 9, 1);
238 ptimer_run(s->timer, 0);
e80cfcfc 239 s->stopped = 1;
d7edfd27 240 qemu_irq_lower(s->irq);
e80cfcfc
FB
241}
242
d7edfd27 243void slavio_timer_init(target_phys_addr_t addr, qemu_irq irq, int mode)
e80cfcfc
FB
244{
245 int slavio_timer_io_memory;
246 SLAVIO_TIMERState *s;
8d05ea8a 247 QEMUBH *bh;
e80cfcfc
FB
248
249 s = qemu_mallocz(sizeof(SLAVIO_TIMERState));
250 if (!s)
251 return;
252 s->irq = irq;
253 s->mode = mode;
8d05ea8a
BS
254 bh = qemu_bh_new(slavio_timer_irq, s);
255 s->timer = ptimer_init(bh);
256 ptimer_set_period(s->timer, 500ULL);
e80cfcfc
FB
257
258 slavio_timer_io_memory = cpu_register_io_memory(0, slavio_timer_mem_read,
259 slavio_timer_mem_write, s);
5aca8c3b 260 cpu_register_physical_memory(addr, TIMER_SIZE, slavio_timer_io_memory);
8d05ea8a 261 register_savevm("slavio_timer", addr, 2, slavio_timer_save, slavio_timer_load, s);
e80cfcfc
FB
262 qemu_register_reset(slavio_timer_reset, s);
263 slavio_timer_reset(s);
264}