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)
42 /* Check all active timers, and schedule the next timer interrupt. */
44 static void sh_timer_update(sh_timer_state
*s
)
46 int new_level
= s
->int_level
&& (s
->tcr
& TIMER_TCR_UNIE
);
48 if (new_level
!= s
->old_level
)
49 qemu_set_irq (s
->irq
, new_level
);
51 s
->old_level
= s
->int_level
;
52 s
->int_level
= new_level
;
55 static uint32_t sh_timer_read(void *opaque
, target_phys_addr_t offset
)
57 sh_timer_state
*s
= (sh_timer_state
*)opaque
;
59 switch (offset
>> 2) {
63 return ptimer_get_count(s
->timer
);
65 return s
->tcr
| (s
->int_level
? TIMER_TCR_UNF
: 0);
67 if (s
->feat
& TIMER_FEAT_CAPT
)
70 cpu_abort (cpu_single_env
, "sh_timer_read: Bad offset %x\n",
76 static void sh_timer_write(void *opaque
, target_phys_addr_t offset
,
79 sh_timer_state
*s
= (sh_timer_state
*)opaque
;
82 switch (offset
>> 2) {
85 ptimer_set_limit(s
->timer
, s
->tcor
, 0);
89 ptimer_set_count(s
->timer
, s
->tcnt
);
93 /* Pause the timer if it is running. This may cause some
94 inaccuracy dure to rounding, but avoids a whole lot of other
96 ptimer_stop(s
->timer
);
99 /* ??? Need to recalculate expiry time after changing divisor. */
100 switch (value
& TIMER_TCR_TPSC
) {
101 case 0: freq
>>= 2; break;
102 case 1: freq
>>= 4; break;
103 case 2: freq
>>= 6; break;
104 case 3: freq
>>= 8; break;
105 case 4: freq
>>= 10; break;
107 case 7: if (s
->feat
& TIMER_FEAT_EXTCLK
) break;
108 default: cpu_abort (cpu_single_env
,
109 "sh_timer_write: Reserved TPSC value\n"); break;
111 switch ((value
& TIMER_TCR_CKEG
) >> 3) {
115 case 3: if (s
->feat
& TIMER_FEAT_EXTCLK
) break;
116 default: cpu_abort (cpu_single_env
,
117 "sh_timer_write: Reserved CKEG value\n"); break;
119 switch ((value
& TIMER_TCR_ICPE
) >> 6) {
122 case 3: if (s
->feat
& TIMER_FEAT_CAPT
) break;
123 default: cpu_abort (cpu_single_env
,
124 "sh_timer_write: Reserved ICPE value\n"); break;
126 if ((value
& TIMER_TCR_UNF
) == 0)
129 value
&= ~TIMER_TCR_UNF
;
131 if ((value
& TIMER_TCR_ICPF
) && (!(s
->feat
& TIMER_FEAT_CAPT
)))
132 cpu_abort (cpu_single_env
,
133 "sh_timer_write: Reserved ICPF value\n");
135 value
&= ~TIMER_TCR_ICPF
; /* capture not supported */
137 if (value
& TIMER_TCR_RESERVED
)
138 cpu_abort (cpu_single_env
,
139 "sh_timer_write: Reserved TCR bits set\n");
141 ptimer_set_limit(s
->timer
, s
->tcor
, 0);
142 ptimer_set_freq(s
->timer
, freq
);
144 /* Restart the timer if still enabled. */
145 ptimer_run(s
->timer
, 0);
149 if (s
->feat
& TIMER_FEAT_CAPT
) {
154 cpu_abort (cpu_single_env
, "sh_timer_write: Bad offset %x\n",
160 static void sh_timer_start_stop(void *opaque
, int enable
)
162 sh_timer_state
*s
= (sh_timer_state
*)opaque
;
165 printf("sh_timer_start_stop %d (%d)\n", enable
, s
->enabled
);
168 if (s
->enabled
&& !enable
) {
169 ptimer_stop(s
->timer
);
171 if (!s
->enabled
&& enable
) {
172 ptimer_run(s
->timer
, 0);
174 s
->enabled
= !!enable
;
177 printf("sh_timer_start_stop done %d\n", s
->enabled
);
181 static void sh_timer_tick(void *opaque
)
183 sh_timer_state
*s
= (sh_timer_state
*)opaque
;
184 s
->int_level
= s
->enabled
;
188 static void *sh_timer_init(uint32_t freq
, int feat
, qemu_irq irq
)
193 s
= (sh_timer_state
*)qemu_mallocz(sizeof(sh_timer_state
));
196 s
->tcor
= 0xffffffff;
197 s
->tcnt
= 0xffffffff;
198 s
->tcpr
= 0xdeadbeef;
203 bh
= qemu_bh_new(sh_timer_tick
, s
);
204 s
->timer
= ptimer_init(bh
);
205 /* ??? Save/restore. */
217 static uint32_t tmu012_read(void *opaque
, target_phys_addr_t offset
)
219 tmu012_state
*s
= (tmu012_state
*)opaque
;
222 printf("tmu012_read 0x%lx\n", (unsigned long) offset
);
225 if (offset
>= 0x20) {
226 if (!(s
->feat
& TMU012_FEAT_3CHAN
))
227 cpu_abort (cpu_single_env
, "tmu012_write: Bad channel offset %x\n",
229 return sh_timer_read(s
->timer
[2], offset
- 0x20);
233 return sh_timer_read(s
->timer
[1], offset
- 0x14);
236 return sh_timer_read(s
->timer
[0], offset
- 0x08);
241 if ((s
->feat
& TMU012_FEAT_TOCR
) && offset
== 0)
244 cpu_abort (cpu_single_env
, "tmu012_write: Bad offset %x\n",
249 static void tmu012_write(void *opaque
, target_phys_addr_t offset
,
252 tmu012_state
*s
= (tmu012_state
*)opaque
;
255 printf("tmu012_write 0x%lx 0x%08x\n", (unsigned long) offset
, value
);
258 if (offset
>= 0x20) {
259 if (!(s
->feat
& TMU012_FEAT_3CHAN
))
260 cpu_abort (cpu_single_env
, "tmu012_write: Bad channel offset %x\n",
262 sh_timer_write(s
->timer
[2], offset
- 0x20, value
);
266 if (offset
>= 0x14) {
267 sh_timer_write(s
->timer
[1], offset
- 0x14, value
);
271 if (offset
>= 0x08) {
272 sh_timer_write(s
->timer
[0], offset
- 0x08, value
);
277 sh_timer_start_stop(s
->timer
[0], value
& (1 << 0));
278 sh_timer_start_stop(s
->timer
[1], value
& (1 << 1));
279 if (s
->feat
& TMU012_FEAT_3CHAN
)
280 sh_timer_start_stop(s
->timer
[2], value
& (1 << 2));
282 if (value
& (1 << 2))
283 cpu_abort (cpu_single_env
, "tmu012_write: Bad channel\n");
289 if ((s
->feat
& TMU012_FEAT_TOCR
) && offset
== 0) {
290 s
->tocr
= value
& (1 << 0);
294 static CPUReadMemoryFunc
*tmu012_readfn
[] = {
300 static CPUWriteMemoryFunc
*tmu012_writefn
[] = {
306 void tmu012_init(target_phys_addr_t base
, int feat
, uint32_t freq
,
307 qemu_irq ch0_irq
, qemu_irq ch1_irq
,
308 qemu_irq ch2_irq0
, qemu_irq ch2_irq1
)
312 int timer_feat
= (feat
& TMU012_FEAT_EXTCLK
) ? TIMER_FEAT_EXTCLK
: 0;
314 s
= (tmu012_state
*)qemu_mallocz(sizeof(tmu012_state
));
316 s
->timer
[0] = sh_timer_init(freq
, timer_feat
, ch0_irq
);
317 s
->timer
[1] = sh_timer_init(freq
, timer_feat
, ch1_irq
);
318 if (feat
& TMU012_FEAT_3CHAN
)
319 s
->timer
[2] = sh_timer_init(freq
, timer_feat
| TIMER_FEAT_CAPT
,
320 ch2_irq0
); /* ch2_irq1 not supported */
321 iomemtype
= cpu_register_io_memory(0, tmu012_readfn
,
323 cpu_register_physical_memory(P4ADDR(base
), 0x00001000, iomemtype
);
324 cpu_register_physical_memory(A7ADDR(base
), 0x00001000, iomemtype
);
325 /* ??? Save/restore. */