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 RW_TMR0_DIV 0x00
32 #define R_TMR0_DATA 0x04
33 #define RW_TMR0_CTRL 0x08
34 #define RW_TMR1_DIV 0x10
35 #define R_TMR1_DATA 0x14
36 #define RW_TMR1_CTRL 0x18
38 #define RW_WD_CTRL 0x40
39 #define RW_INTR_MASK 0x48
40 #define RW_ACK_INTR 0x4c
42 #define R_MASKED_INTR 0x54
47 target_phys_addr_t base;
56 uint32_t rw_intr_mask;
61 /* diff two timevals. Return a single int in us. */
62 int diff_timeval_us(struct timeval *a, struct timeval *b)
66 /* assume these values are signed. */
67 diff = (a->tv_sec - b->tv_sec) * 1000 * 1000;
68 diff += (a->tv_usec - b->tv_usec);
72 static uint32_t timer_rinvalid (void *opaque, target_phys_addr_t addr)
74 struct fs_timer_t *t = opaque;
75 CPUState *env = t->env;
76 cpu_abort(env, "Unsupported short access. reg=%x pc=%x.\n",
81 static uint32_t timer_readl (void *opaque, target_phys_addr_t addr)
83 struct fs_timer_t *t = opaque;
84 D(CPUState *env = t->env);
87 /* Make addr relative to this instances base. */
93 D(printf ("R_TMR1_DATA\n"));
98 gettimeofday(&now, NULL);
99 if (!(t->last.tv_sec == 0
100 && t->last.tv_usec == 0)) {
101 r = diff_timeval_us(&now, &t->last);
102 r *= 1000; /* convert to ns. */
103 r++; /* make sure we increase for each call. */
113 r = t->r_intr & t->rw_intr_mask;
116 D(printf ("%s %x p=%x\n", __func__, addr, env->pc));
123 timer_winvalid (void *opaque, target_phys_addr_t addr, uint32_t value)
125 struct fs_timer_t *t = opaque;
126 CPUState *env = t->env;
127 cpu_abort(env, "Unsupported short access. reg=%x pc=%x.\n",
131 static void write_ctrl(struct fs_timer_t *t, uint32_t v)
145 D(printf ("extern or disabled timer clock?\n"));
147 case 4: freq_hz = 29493000; break;
148 case 5: freq_hz = 32000000; break;
149 case 6: freq_hz = 32768000; break;
150 case 7: freq_hz = 100000000; break;
156 D(printf ("freq_hz=%d limit=%d\n", freq_hz, t->limit));
161 ptimer_set_period(t->ptimer, freq_hz / t->scale);
167 D(printf ("limit=%d %d\n",
168 t->limit, t->limit/t->scale));
169 ptimer_set_limit(t->ptimer, t->limit / t->scale, 1);
172 ptimer_stop(t->ptimer);
175 ptimer_run(t->ptimer, 0);
183 static void timer_ack_irq(struct fs_timer_t *t)
185 if (!(t->r_intr & t->mask & t->rw_intr_mask))
186 qemu_irq_lower(t->irq[0]);
190 timer_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
192 struct fs_timer_t *t = opaque;
193 CPUState *env = t->env;
195 D(printf ("%s %x %x pc=%x\n",
196 __func__, addr, value, env->pc));
197 /* Make addr relative to this instances base. */
202 D(printf ("RW_TMR0_DIV=%x\n", value));
206 D(printf ("RW_TMR0_CTRL=%x\n", value));
207 write_ctrl(t, value);
210 D(printf ("RW_TMR1_DIV=%x\n", value));
213 D(printf ("RW_TMR1_CTRL=%x\n", value));
216 D(printf ("RW_INTR_MASK=%x\n", value));
217 t->rw_intr_mask = value;
220 D(printf ("RW_WD_CTRL=%x\n", value));
227 printf ("%s %x %x pc=%x\n",
228 __func__, addr, value, env->pc);
233 static CPUReadMemoryFunc *timer_read[] = {
239 static CPUWriteMemoryFunc *timer_write[] = {
245 static void timer_irq(void *opaque)
247 struct fs_timer_t *t = opaque;
248 t->r_intr |= t->mask;
249 if (t->mask & t->rw_intr_mask) {
250 D(printf("%s raise\n", __func__));
251 qemu_irq_raise(t->irq[0]);
255 void etraxfs_timer_init(CPUState *env, qemu_irq *irqs,
256 target_phys_addr_t base)
258 static struct fs_timer_t *t;
261 t = qemu_mallocz(sizeof *t);
265 t->bh = qemu_bh_new(timer_irq, t);
266 t->ptimer = ptimer_init(t->bh);
272 timer_regs = cpu_register_io_memory(0, timer_read, timer_write, t);
273 cpu_register_physical_memory (base, 0x5c, timer_regs);