2 * SuperH Timer modules.
4 * Copyright (c) 2007 Magnus Damm
5 * Based on arm_timer.c by Paul Brook
6 * Copyright (c) 2005-2006 CodeSourcery.
8 * This code is licenced under the GPL.
13 #include "qemu-timer.h"
17 #define TIMER_TCR_TPSC (7 << 0)
18 #define TIMER_TCR_CKEG (3 << 3)
19 #define TIMER_TCR_UNIE (1 << 5)
20 #define TIMER_TCR_ICPE (3 << 6)
21 #define TIMER_TCR_UNF (1 << 8)
22 #define TIMER_TCR_ICPF (1 << 9)
23 #define TIMER_TCR_RESERVED (0x3f << 10)
25 #define TIMER_FEAT_CAPT (1 << 0)
26 #define TIMER_FEAT_EXTCLK (1 << 1)
47 /* Check all active timers, and schedule the next timer interrupt. */
49 static void sh_timer_update(sh_timer_state
*s
)
51 int new_level
= s
->int_level
&& (s
->tcr
& TIMER_TCR_UNIE
);
53 if (new_level
!= s
->old_level
)
54 qemu_set_irq (s
->irq
, new_level
);
56 s
->old_level
= s
->int_level
;
57 s
->int_level
= new_level
;
60 static uint32_t sh_timer_read(void *opaque
, target_phys_addr_t offset
)
62 sh_timer_state
*s
= (sh_timer_state
*)opaque
;
64 switch (offset
>> 2) {
68 return ptimer_get_count(s
->timer
);
70 return s
->tcr
| (s
->int_level
? TIMER_TCR_UNF
: 0);
72 if (s
->feat
& TIMER_FEAT_CAPT
)
75 cpu_abort (cpu_single_env
, "sh_timer_read: Bad offset %x\n",
81 static void sh_timer_write(void *opaque
, target_phys_addr_t offset
,
84 sh_timer_state
*s
= (sh_timer_state
*)opaque
;
87 switch (offset
>> 2) {
90 ptimer_set_limit(s
->timer
, s
->tcor
, 0);
94 ptimer_set_count(s
->timer
, s
->tcnt
);
98 /* Pause the timer if it is running. This may cause some
99 inaccuracy dure to rounding, but avoids a whole lot of other
101 ptimer_stop(s
->timer
);
104 /* ??? Need to recalculate expiry time after changing divisor. */
105 switch (value
& TIMER_TCR_TPSC
) {
106 case 0: freq
>>= 2; break;
107 case 1: freq
>>= 4; break;
108 case 2: freq
>>= 6; break;
109 case 3: freq
>>= 8; break;
110 case 4: freq
>>= 10; break;
112 case 7: if (s
->feat
& TIMER_FEAT_EXTCLK
) break;
113 default: cpu_abort (cpu_single_env
,
114 "sh_timer_write: Reserved TPSC value\n"); break;
116 switch ((value
& TIMER_TCR_CKEG
) >> 3) {
120 case 3: if (s
->feat
& TIMER_FEAT_EXTCLK
) break;
121 default: cpu_abort (cpu_single_env
,
122 "sh_timer_write: Reserved CKEG value\n"); break;
124 switch ((value
& TIMER_TCR_ICPE
) >> 6) {
127 case 3: if (s
->feat
& TIMER_FEAT_CAPT
) break;
128 default: cpu_abort (cpu_single_env
,
129 "sh_timer_write: Reserved ICPE value\n"); break;
131 if ((value
& TIMER_TCR_UNF
) == 0)
134 value
&= ~TIMER_TCR_UNF
;
136 if ((value
& TIMER_TCR_ICPF
) && (!(s
->feat
& TIMER_FEAT_CAPT
)))
137 cpu_abort (cpu_single_env
,
138 "sh_timer_write: Reserved ICPF value\n");
140 value
&= ~TIMER_TCR_ICPF
; /* capture not supported */
142 if (value
& TIMER_TCR_RESERVED
)
143 cpu_abort (cpu_single_env
,
144 "sh_timer_write: Reserved TCR bits set\n");
146 ptimer_set_limit(s
->timer
, s
->tcor
, 0);
147 ptimer_set_freq(s
->timer
, freq
);
149 /* Restart the timer if still enabled. */
150 ptimer_run(s
->timer
, 0);
154 if (s
->feat
& TIMER_FEAT_CAPT
) {
159 cpu_abort (cpu_single_env
, "sh_timer_write: Bad offset %x\n",
165 static void sh_timer_start_stop(void *opaque
, int enable
)
167 sh_timer_state
*s
= (sh_timer_state
*)opaque
;
170 printf("sh_timer_start_stop %d (%d)\n", enable
, s
->enabled
);
173 if (s
->enabled
&& !enable
) {
174 ptimer_stop(s
->timer
);
176 if (!s
->enabled
&& enable
) {
177 ptimer_run(s
->timer
, 0);
179 s
->enabled
= !!enable
;
182 printf("sh_timer_start_stop done %d\n", s
->enabled
);
186 static void sh_timer_tick(void *opaque
)
188 sh_timer_state
*s
= (sh_timer_state
*)opaque
;
189 s
->int_level
= s
->enabled
;
193 static void *sh_timer_init(uint32_t freq
, int feat
, qemu_irq irq
)
198 s
= (sh_timer_state
*)qemu_mallocz(sizeof(sh_timer_state
));
201 s
->tcor
= 0xffffffff;
202 s
->tcnt
= 0xffffffff;
203 s
->tcpr
= 0xdeadbeef;
208 bh
= qemu_bh_new(sh_timer_tick
, s
);
209 s
->timer
= ptimer_init(bh
);
211 sh_timer_write(s
, OFFSET_TCOR
>> 2, s
->tcor
);
212 sh_timer_write(s
, OFFSET_TCNT
>> 2, s
->tcnt
);
213 sh_timer_write(s
, OFFSET_TCPR
>> 2, s
->tcpr
);
214 sh_timer_write(s
, OFFSET_TCR
>> 2, s
->tcpr
);
215 /* ??? Save/restore. */
227 static uint32_t tmu012_read(void *opaque
, target_phys_addr_t offset
)
229 tmu012_state
*s
= (tmu012_state
*)opaque
;
232 printf("tmu012_read 0x%lx\n", (unsigned long) offset
);
235 if (offset
>= 0x20) {
236 if (!(s
->feat
& TMU012_FEAT_3CHAN
))
237 cpu_abort (cpu_single_env
, "tmu012_write: Bad channel offset %x\n",
239 return sh_timer_read(s
->timer
[2], offset
- 0x20);
243 return sh_timer_read(s
->timer
[1], offset
- 0x14);
246 return sh_timer_read(s
->timer
[0], offset
- 0x08);
251 if ((s
->feat
& TMU012_FEAT_TOCR
) && offset
== 0)
254 cpu_abort (cpu_single_env
, "tmu012_write: Bad offset %x\n",
259 static void tmu012_write(void *opaque
, target_phys_addr_t offset
,
262 tmu012_state
*s
= (tmu012_state
*)opaque
;
265 printf("tmu012_write 0x%lx 0x%08x\n", (unsigned long) offset
, value
);
268 if (offset
>= 0x20) {
269 if (!(s
->feat
& TMU012_FEAT_3CHAN
))
270 cpu_abort (cpu_single_env
, "tmu012_write: Bad channel offset %x\n",
272 sh_timer_write(s
->timer
[2], offset
- 0x20, value
);
276 if (offset
>= 0x14) {
277 sh_timer_write(s
->timer
[1], offset
- 0x14, value
);
281 if (offset
>= 0x08) {
282 sh_timer_write(s
->timer
[0], offset
- 0x08, value
);
287 sh_timer_start_stop(s
->timer
[0], value
& (1 << 0));
288 sh_timer_start_stop(s
->timer
[1], value
& (1 << 1));
289 if (s
->feat
& TMU012_FEAT_3CHAN
)
290 sh_timer_start_stop(s
->timer
[2], value
& (1 << 2));
292 if (value
& (1 << 2))
293 cpu_abort (cpu_single_env
, "tmu012_write: Bad channel\n");
299 if ((s
->feat
& TMU012_FEAT_TOCR
) && offset
== 0) {
300 s
->tocr
= value
& (1 << 0);
304 static CPUReadMemoryFunc
*tmu012_readfn
[] = {
310 static CPUWriteMemoryFunc
*tmu012_writefn
[] = {
316 void tmu012_init(target_phys_addr_t base
, int feat
, uint32_t freq
,
317 qemu_irq ch0_irq
, qemu_irq ch1_irq
,
318 qemu_irq ch2_irq0
, qemu_irq ch2_irq1
)
322 int timer_feat
= (feat
& TMU012_FEAT_EXTCLK
) ? TIMER_FEAT_EXTCLK
: 0;
324 s
= (tmu012_state
*)qemu_mallocz(sizeof(tmu012_state
));
326 s
->timer
[0] = sh_timer_init(freq
, timer_feat
, ch0_irq
);
327 s
->timer
[1] = sh_timer_init(freq
, timer_feat
, ch1_irq
);
328 if (feat
& TMU012_FEAT_3CHAN
)
329 s
->timer
[2] = sh_timer_init(freq
, timer_feat
| TIMER_FEAT_CAPT
,
330 ch2_irq0
); /* ch2_irq1 not supported */
331 iomemtype
= cpu_register_io_memory(0, tmu012_readfn
,
333 cpu_register_physical_memory(P4ADDR(base
), 0x00001000, iomemtype
);
334 cpu_register_physical_memory(A7ADDR(base
), 0x00001000, iomemtype
);
335 /* ??? Save/restore. */