4 * Copyright (c) 2007 Edgar E. Iglesias, Axis Communications AB.
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
27 #include "qemu-timer.h"
31 #define R_TIME 0xb001e038
32 #define RW_TMR0_DIV 0xb001e000
33 #define R_TMR0_DATA 0xb001e004
34 #define RW_TMR0_CTRL 0xb001e008
35 #define RW_TMR1_DIV 0xb001e010
36 #define R_TMR1_DATA 0xb001e014
37 #define RW_TMR1_CTRL 0xb001e018
39 #define RW_WD_CTRL 0xb001e040
40 #define RW_INTR_MASK 0xb001e048
41 #define RW_ACK_INTR 0xb001e04c
42 #define R_INTR 0xb001e050
43 #define R_MASKED_INTR 0xb001e054
55 uint32_t rw_intr_mask;
60 static struct fs_timer_t timer[2];
62 static inline int timer_index(target_phys_addr_t addr)
65 if (addr >= 0xb005e000)
70 /* diff two timevals. Return a single int in us. */
71 int diff_timeval_us(struct timeval *a, struct timeval *b)
75 /* assume these values are signed. */
76 diff = (a->tv_sec - b->tv_sec) * 1000 * 1000;
77 diff += (a->tv_usec - b->tv_usec);
81 static uint32_t timer_readb (void *opaque, target_phys_addr_t addr)
87 D(printf ("%s %x pc=%x\n", __func__, addr, env->pc));
90 static uint32_t timer_readw (void *opaque, target_phys_addr_t addr)
96 D(printf ("%s %x pc=%x\n", __func__, addr, env->pc));
100 static uint32_t timer_readl (void *opaque, target_phys_addr_t addr)
102 CPUState *env = opaque;
104 int t = timer_index(addr);
110 D(printf ("R_TMR1_DATA\n"));
115 gettimeofday(&now, NULL);
116 if (!(timer[t].last.tv_sec == 0
117 && timer[t].last.tv_usec == 0)) {
118 r = diff_timeval_us(&now, &timer[t].last);
119 r *= 1000; /* convert to ns. */
120 r++; /* make sure we increase for each call. */
127 r = timer[t].rw_intr_mask;
130 r = timer[t].r_intr & timer[t].rw_intr_mask;
133 D(printf ("%s %x p=%x\n", __func__, addr, env->pc));
140 timer_writeb (void *opaque, target_phys_addr_t addr, uint32_t value)
144 D(printf ("%s %x %x pc=%x\n", __func__, addr, value, env->pc));
147 timer_writew (void *opaque, target_phys_addr_t addr, uint32_t value)
151 D(printf ("%s %x %x pc=%x\n", __func__, addr, value, env->pc));
154 static void write_ctrl(struct fs_timer_t *t, uint32_t v)
168 D(printf ("extern or disabled timer clock?\n"));
170 case 4: freq_hz = 29493000; break;
171 case 5: freq_hz = 32000000; break;
172 case 6: freq_hz = 32768000; break;
173 case 7: freq_hz = 100000000; break;
179 D(printf ("freq_hz=%d limit=%d\n", freq_hz, t->limit));
184 ptimer_set_period(t->ptimer, freq_hz / t->scale);
190 D(printf ("limit=%d %d\n",
191 t->limit, t->limit/t->scale));
192 ptimer_set_limit(t->ptimer, t->limit / t->scale, 1);
195 ptimer_stop(t->ptimer);
198 ptimer_run(t->ptimer, 0);
206 static void timer_ack_irq(struct fs_timer_t *t)
208 if (!(t->r_intr & t->mask & t->rw_intr_mask))
209 qemu_irq_lower(t->irq[0]);
213 timer_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
215 CPUState *env = opaque;
216 int t = timer_index(addr);
218 D(printf ("%s %x %x pc=%x\n",
219 __func__, addr, value, env->pc));
223 D(printf ("RW_TMR0_DIV=%x\n", value));
224 timer[t].limit = value;
227 D(printf ("RW_TMR0_CTRL=%x\n", value));
228 write_ctrl(&timer[t], value);
231 D(printf ("RW_TMR1_DIV=%x\n", value));
234 D(printf ("RW_TMR1_CTRL=%x\n", value));
237 D(printf ("RW_INTR_MASK=%x\n", value));
238 timer[t].rw_intr_mask = value;
241 D(printf ("RW_WD_CTRL=%x\n", value));
244 timer[t].r_intr &= ~value;
245 timer_ack_irq(&timer[t]);
248 printf ("%s %x %x pc=%x\n",
249 __func__, addr, value, env->pc);
254 static CPUReadMemoryFunc *timer_read[] = {
260 static CPUWriteMemoryFunc *timer_write[] = {
266 static void timer_irq(void *opaque)
268 struct fs_timer_t *t = opaque;
269 t->r_intr |= t->mask;
270 if (t->mask & t->rw_intr_mask) {
271 D(printf("%s raise\n", __func__));
272 qemu_irq_raise(t->irq[0]);
276 void etraxfs_timer_init(CPUState *env, qemu_irq *irqs)
280 timer[0].bh = qemu_bh_new(timer_irq, &timer[0]);
281 timer[0].ptimer = ptimer_init(timer[0].bh);
282 timer[0].irq = irqs + 26;
286 timer[1].bh = qemu_bh_new(timer_irq, &timer[1]);
287 timer[1].ptimer = ptimer_init(timer[1].bh);
288 timer[1].irq = irqs + 26;
292 timer_regs = cpu_register_io_memory(0, timer_read, timer_write, env);
293 cpu_register_physical_memory (0xb001e000, 0x5c, timer_regs);
294 cpu_register_physical_memory (0xb005e000, 0x5c, timer_regs);