Fix qemu_ld64 constraint list
[qemu/mini2440/sniper_sniper_test.git] / hw / slavio_timer.c
blobf091fbe5f9490e71d65af20f51cc531e4b371c87
1 /*
2 * QEMU Sparc SLAVIO timer controller emulation
4 * Copyright (c) 2003-2005 Fabrice Bellard
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
22 * THE SOFTWARE.
24 #include "hw.h"
25 #include "sun4m.h"
26 #include "qemu-timer.h"
28 //#define DEBUG_TIMER
30 #ifdef DEBUG_TIMER
31 #define DPRINTF(fmt, args...) \
32 do { printf("TIMER: " fmt , ##args); } while (0)
33 #else
34 #define DPRINTF(fmt, args...) do {} while (0)
35 #endif
38 * Registers of hardware timer in sun4m.
40 * This is the timer/counter part of chip STP2001 (Slave I/O), also
41 * produced as NCR89C105. See
42 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt
44 * The 31-bit counter is incremented every 500ns by bit 9. Bits 8..0
45 * are zero. Bit 31 is 1 when count has been reached.
47 * Per-CPU timers interrupt local CPU, system timer uses normal
48 * interrupt routing.
52 #define MAX_CPUS 16
54 typedef struct SLAVIO_TIMERState {
55 qemu_irq irq;
56 ptimer_state *timer;
57 uint32_t count, counthigh, reached;
58 uint64_t limit;
59 // processor only
60 uint32_t running;
61 struct SLAVIO_TIMERState *master;
62 uint32_t slave_index;
63 // system only
64 uint32_t num_slaves;
65 struct SLAVIO_TIMERState *slave[MAX_CPUS];
66 uint32_t slave_mode;
67 } SLAVIO_TIMERState;
69 #define TIMER_MAXADDR 0x1f
70 #define SYS_TIMER_SIZE 0x14
71 #define CPU_TIMER_SIZE 0x10
73 #define SYS_TIMER_OFFSET 0x10000ULL
74 #define CPU_TIMER_OFFSET(cpu) (0x1000ULL * cpu)
76 #define TIMER_LIMIT 0
77 #define TIMER_COUNTER 1
78 #define TIMER_COUNTER_NORST 2
79 #define TIMER_STATUS 3
80 #define TIMER_MODE 4
82 #define TIMER_COUNT_MASK32 0xfffffe00
83 #define TIMER_LIMIT_MASK32 0x7fffffff
84 #define TIMER_MAX_COUNT64 0x7ffffffffffffe00ULL
85 #define TIMER_MAX_COUNT32 0x7ffffe00ULL
86 #define TIMER_REACHED 0x80000000
87 #define TIMER_PERIOD 500ULL // 500ns
88 #define LIMIT_TO_PERIODS(l) ((l) >> 9)
89 #define PERIODS_TO_LIMIT(l) ((l) << 9)
91 static int slavio_timer_is_user(SLAVIO_TIMERState *s)
93 return s->master && (s->master->slave_mode & (1 << s->slave_index));
96 // Update count, set irq, update expire_time
97 // Convert from ptimer countdown units
98 static void slavio_timer_get_out(SLAVIO_TIMERState *s)
100 uint64_t count, limit;
102 if (s->limit == 0) /* free-run processor or system counter */
103 limit = TIMER_MAX_COUNT32;
104 else
105 limit = s->limit;
107 if (s->timer)
108 count = limit - PERIODS_TO_LIMIT(ptimer_get_count(s->timer));
109 else
110 count = 0;
112 DPRINTF("get_out: limit %" PRIx64 " count %x%08x\n", s->limit,
113 s->counthigh, s->count);
114 s->count = count & TIMER_COUNT_MASK32;
115 s->counthigh = count >> 32;
118 // timer callback
119 static void slavio_timer_irq(void *opaque)
121 SLAVIO_TIMERState *s = opaque;
123 slavio_timer_get_out(s);
124 DPRINTF("callback: count %x%08x\n", s->counthigh, s->count);
125 s->reached = TIMER_REACHED;
126 if (!slavio_timer_is_user(s))
127 qemu_irq_raise(s->irq);
130 static uint32_t slavio_timer_mem_readl(void *opaque, target_phys_addr_t addr)
132 SLAVIO_TIMERState *s = opaque;
133 uint32_t saddr, ret;
135 saddr = (addr & TIMER_MAXADDR) >> 2;
136 switch (saddr) {
137 case TIMER_LIMIT:
138 // read limit (system counter mode) or read most signifying
139 // part of counter (user mode)
140 if (slavio_timer_is_user(s)) {
141 // read user timer MSW
142 slavio_timer_get_out(s);
143 ret = s->counthigh | s->reached;
144 } else {
145 // read limit
146 // clear irq
147 qemu_irq_lower(s->irq);
148 s->reached = 0;
149 ret = s->limit & TIMER_LIMIT_MASK32;
151 break;
152 case TIMER_COUNTER:
153 // read counter and reached bit (system mode) or read lsbits
154 // of counter (user mode)
155 slavio_timer_get_out(s);
156 if (slavio_timer_is_user(s)) // read user timer LSW
157 ret = s->count & TIMER_MAX_COUNT64;
158 else // read limit
159 ret = (s->count & TIMER_MAX_COUNT32) | s->reached;
160 break;
161 case TIMER_STATUS:
162 // only available in processor counter/timer
163 // read start/stop status
164 ret = s->running;
165 break;
166 case TIMER_MODE:
167 // only available in system counter
168 // read user/system mode
169 ret = s->slave_mode;
170 break;
171 default:
172 DPRINTF("invalid read address " TARGET_FMT_plx "\n", addr);
173 ret = 0;
174 break;
176 DPRINTF("read " TARGET_FMT_plx " = %08x\n", addr, ret);
178 return ret;
181 static void slavio_timer_mem_writel(void *opaque, target_phys_addr_t addr,
182 uint32_t val)
184 SLAVIO_TIMERState *s = opaque;
185 uint32_t saddr;
187 DPRINTF("write " TARGET_FMT_plx " %08x\n", addr, val);
188 saddr = (addr & TIMER_MAXADDR) >> 2;
189 switch (saddr) {
190 case TIMER_LIMIT:
191 if (slavio_timer_is_user(s)) {
192 uint64_t count;
194 // set user counter MSW, reset counter
195 s->limit = TIMER_MAX_COUNT64;
196 s->counthigh = val & (TIMER_MAX_COUNT64 >> 32);
197 s->reached = 0;
198 count = ((uint64_t)s->counthigh << 32) | s->count;
199 DPRINTF("processor %d user timer set to %016llx\n", s->slave_index,
200 count);
201 if (s->timer)
202 ptimer_set_count(s->timer, LIMIT_TO_PERIODS(s->limit - count));
203 } else {
204 // set limit, reset counter
205 qemu_irq_lower(s->irq);
206 s->limit = val & TIMER_MAX_COUNT32;
207 if (s->timer) {
208 if (s->limit == 0) /* free-run */
209 ptimer_set_limit(s->timer,
210 LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
211 else
212 ptimer_set_limit(s->timer, LIMIT_TO_PERIODS(s->limit), 1);
215 break;
216 case TIMER_COUNTER:
217 if (slavio_timer_is_user(s)) {
218 uint64_t count;
220 // set user counter LSW, reset counter
221 s->limit = TIMER_MAX_COUNT64;
222 s->count = val & TIMER_MAX_COUNT64;
223 s->reached = 0;
224 count = ((uint64_t)s->counthigh) << 32 | s->count;
225 DPRINTF("processor %d user timer set to %016llx\n", s->slave_index,
226 count);
227 if (s->timer)
228 ptimer_set_count(s->timer, LIMIT_TO_PERIODS(s->limit - count));
229 } else
230 DPRINTF("not user timer\n");
231 break;
232 case TIMER_COUNTER_NORST:
233 // set limit without resetting counter
234 s->limit = val & TIMER_MAX_COUNT32;
235 if (s->timer) {
236 if (s->limit == 0) /* free-run */
237 ptimer_set_limit(s->timer,
238 LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 0);
239 else
240 ptimer_set_limit(s->timer, LIMIT_TO_PERIODS(s->limit), 0);
242 break;
243 case TIMER_STATUS:
244 if (slavio_timer_is_user(s)) {
245 // start/stop user counter
246 if ((val & 1) && !s->running) {
247 DPRINTF("processor %d user timer started\n", s->slave_index);
248 if (s->timer)
249 ptimer_run(s->timer, 0);
250 s->running = 1;
251 } else if (!(val & 1) && s->running) {
252 DPRINTF("processor %d user timer stopped\n", s->slave_index);
253 if (s->timer)
254 ptimer_stop(s->timer);
255 s->running = 0;
258 break;
259 case TIMER_MODE:
260 if (s->master == NULL) {
261 unsigned int i;
263 for (i = 0; i < s->num_slaves; i++) {
264 unsigned int processor = 1 << i;
266 // check for a change in timer mode for this processor
267 if ((val & processor) != (s->slave_mode & processor)) {
268 if (val & processor) { // counter -> user timer
269 qemu_irq_lower(s->slave[i]->irq);
270 // counters are always running
271 ptimer_stop(s->slave[i]->timer);
272 s->slave[i]->running = 0;
273 // user timer limit is always the same
274 s->slave[i]->limit = TIMER_MAX_COUNT64;
275 ptimer_set_limit(s->slave[i]->timer,
276 LIMIT_TO_PERIODS(s->slave[i]->limit),
278 // set this processors user timer bit in config
279 // register
280 s->slave_mode |= processor;
281 DPRINTF("processor %d changed from counter to user "
282 "timer\n", s->slave[i]->slave_index);
283 } else { // user timer -> counter
284 // stop the user timer if it is running
285 if (s->slave[i]->running)
286 ptimer_stop(s->slave[i]->timer);
287 // start the counter
288 ptimer_run(s->slave[i]->timer, 0);
289 s->slave[i]->running = 1;
290 // clear this processors user timer bit in config
291 // register
292 s->slave_mode &= ~processor;
293 DPRINTF("processor %d changed from user timer to "
294 "counter\n", s->slave[i]->slave_index);
298 } else
299 DPRINTF("not system timer\n");
300 break;
301 default:
302 DPRINTF("invalid write address " TARGET_FMT_plx "\n", addr);
303 break;
307 static CPUReadMemoryFunc *slavio_timer_mem_read[3] = {
308 NULL,
309 NULL,
310 slavio_timer_mem_readl,
313 static CPUWriteMemoryFunc *slavio_timer_mem_write[3] = {
314 NULL,
315 NULL,
316 slavio_timer_mem_writel,
319 static void slavio_timer_save(QEMUFile *f, void *opaque)
321 SLAVIO_TIMERState *s = opaque;
323 qemu_put_be64s(f, &s->limit);
324 qemu_put_be32s(f, &s->count);
325 qemu_put_be32s(f, &s->counthigh);
326 qemu_put_be32s(f, &s->reached);
327 qemu_put_be32s(f, &s->running);
328 if (s->timer)
329 qemu_put_ptimer(f, s->timer);
332 static int slavio_timer_load(QEMUFile *f, void *opaque, int version_id)
334 SLAVIO_TIMERState *s = opaque;
336 if (version_id != 3)
337 return -EINVAL;
339 qemu_get_be64s(f, &s->limit);
340 qemu_get_be32s(f, &s->count);
341 qemu_get_be32s(f, &s->counthigh);
342 qemu_get_be32s(f, &s->reached);
343 qemu_get_be32s(f, &s->running);
344 if (s->timer)
345 qemu_get_ptimer(f, s->timer);
347 return 0;
350 static void slavio_timer_reset(void *opaque)
352 SLAVIO_TIMERState *s = opaque;
354 s->limit = 0;
355 s->count = 0;
356 s->reached = 0;
357 s->slave_mode = 0;
358 if (!s->master || s->slave_index < s->master->num_slaves) {
359 ptimer_set_limit(s->timer, LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
360 ptimer_run(s->timer, 0);
362 s->running = 1;
363 qemu_irq_lower(s->irq);
366 static SLAVIO_TIMERState *slavio_timer_init(target_phys_addr_t addr,
367 qemu_irq irq,
368 SLAVIO_TIMERState *master,
369 uint32_t slave_index)
371 int slavio_timer_io_memory;
372 SLAVIO_TIMERState *s;
373 QEMUBH *bh;
375 s = qemu_mallocz(sizeof(SLAVIO_TIMERState));
376 if (!s)
377 return s;
378 s->irq = irq;
379 s->master = master;
380 s->slave_index = slave_index;
381 if (!master || slave_index < master->num_slaves) {
382 bh = qemu_bh_new(slavio_timer_irq, s);
383 s->timer = ptimer_init(bh);
384 ptimer_set_period(s->timer, TIMER_PERIOD);
387 slavio_timer_io_memory = cpu_register_io_memory(0, slavio_timer_mem_read,
388 slavio_timer_mem_write, s);
389 if (master)
390 cpu_register_physical_memory(addr, CPU_TIMER_SIZE,
391 slavio_timer_io_memory);
392 else
393 cpu_register_physical_memory(addr, SYS_TIMER_SIZE,
394 slavio_timer_io_memory);
395 register_savevm("slavio_timer", addr, 3, slavio_timer_save,
396 slavio_timer_load, s);
397 qemu_register_reset(slavio_timer_reset, s);
398 slavio_timer_reset(s);
400 return s;
403 void slavio_timer_init_all(target_phys_addr_t base, qemu_irq master_irq,
404 qemu_irq *cpu_irqs, unsigned int num_cpus)
406 SLAVIO_TIMERState *master;
407 unsigned int i;
409 master = slavio_timer_init(base + SYS_TIMER_OFFSET, master_irq, NULL, 0);
411 master->num_slaves = num_cpus;
413 for (i = 0; i < MAX_CPUS; i++) {
414 master->slave[i] = slavio_timer_init(base + (target_phys_addr_t)
415 CPU_TIMER_OFFSET(i),
416 cpu_irqs[i], master, i);