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
;
53 /* Control registers. */
56 uint32_t rw_tmr0_ctrl
;
60 uint32_t rw_tmr1_ctrl
;
62 uint32_t rw_intr_mask
;
65 uint32_t r_masked_intr
;
68 static uint32_t timer_rinvalid (void *opaque
, target_phys_addr_t addr
)
70 struct fs_timer_t
*t
= opaque
;
71 CPUState
*env
= t
->env
;
72 cpu_abort(env
, "Unsupported short access. reg=%x pc=%x.\n",
77 static uint32_t timer_readl (void *opaque
, target_phys_addr_t addr
)
79 struct fs_timer_t
*t
= opaque
;
80 D(CPUState
*env
= t
->env
);
83 /* Make addr relative to this instances base. */
89 D(printf ("R_TMR1_DATA\n"));
92 r
= qemu_get_clock(vm_clock
) * 10;
98 r
= t
->r_intr
& t
->rw_intr_mask
;
101 D(printf ("%s %x p=%x\n", __func__
, addr
, env
->pc
));
108 timer_winvalid (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
110 struct fs_timer_t
*t
= opaque
;
111 CPUState
*env
= t
->env
;
112 cpu_abort(env
, "Unsupported short access. reg=%x pc=%x.\n",
116 #define TIMER_SLOWDOWN 1
117 static void update_ctrl(struct fs_timer_t
*t
)
121 unsigned int freq_hz
;
124 op
= t
->rw_tmr0_ctrl
& 3;
125 freq
= t
->rw_tmr0_ctrl
>> 2;
132 D(printf ("extern or disabled timer clock?\n"));
134 case 4: freq_hz
= 29493000; break;
135 case 5: freq_hz
= 32000000; break;
136 case 6: freq_hz
= 32768000; break;
137 case 7: freq_hz
= 100000000; break;
143 D(printf ("freq_hz=%d div=%d\n", freq_hz
, t
->rw_tmr0_div
));
144 div
= t
->rw_tmr0_div
* TIMER_SLOWDOWN
;
147 ptimer_set_freq(t
->ptimer
, freq_hz
);
148 ptimer_set_limit(t
->ptimer
, div
, 0);
154 ptimer_set_limit(t
->ptimer
, div
, 1);
155 ptimer_run(t
->ptimer
, 1);
159 ptimer_stop(t
->ptimer
);
163 ptimer_run(t
->ptimer
, 0);
171 static void timer_update_irq(struct fs_timer_t
*t
)
173 t
->r_intr
&= ~(t
->rw_ack_intr
);
174 t
->r_masked_intr
= t
->r_intr
& t
->rw_intr_mask
;
176 D(printf("%s: masked_intr=%x\n", __func__
, t
->r_masked_intr
));
177 if (t
->r_masked_intr
& 1)
178 qemu_irq_raise(t
->irq
[0]);
180 qemu_irq_lower(t
->irq
[0]);
183 static void timer_hit(void *opaque
)
185 struct fs_timer_t
*t
= opaque
;
191 timer_writel (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
193 struct fs_timer_t
*t
= opaque
;
194 CPUState
*env
= t
->env
;
196 /* Make addr relative to this instances base. */
201 t
->rw_tmr0_div
= value
;
204 D(printf ("RW_TMR0_CTRL=%x\n", value
));
205 t
->rw_tmr0_ctrl
= value
;
209 t
->rw_tmr1_div
= value
;
212 D(printf ("RW_TMR1_CTRL=%x\n", value
));
215 D(printf ("RW_INTR_MASK=%x\n", value
));
216 t
->rw_intr_mask
= value
;
220 D(printf ("RW_WD_CTRL=%x\n", value
));
223 t
->rw_ack_intr
= value
;
228 printf ("%s %x %x pc=%x\n",
229 __func__
, addr
, value
, env
->pc
);
234 static CPUReadMemoryFunc
*timer_read
[] = {
240 static CPUWriteMemoryFunc
*timer_write
[] = {
246 void etraxfs_timer_init(CPUState
*env
, qemu_irq
*irqs
,
247 target_phys_addr_t base
)
249 static struct fs_timer_t
*t
;
252 t
= qemu_mallocz(sizeof *t
);
256 t
->bh
= qemu_bh_new(timer_hit
, t
);
257 t
->ptimer
= ptimer_init(t
->bh
);
262 timer_regs
= cpu_register_io_memory(0, timer_read
, timer_write
, t
);
263 cpu_register_physical_memory (base
, 0x5c, timer_regs
);