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 hw_error("sh_timer_read: Bad offset %x\n", (int)offset
);
80 static void sh_timer_write(void *opaque
, target_phys_addr_t offset
,
83 sh_timer_state
*s
= (sh_timer_state
*)opaque
;
86 switch (offset
>> 2) {
89 ptimer_set_limit(s
->timer
, s
->tcor
, 0);
93 ptimer_set_count(s
->timer
, s
->tcnt
);
97 /* Pause the timer if it is running. This may cause some
98 inaccuracy dure to rounding, but avoids a whole lot of other
100 ptimer_stop(s
->timer
);
103 /* ??? Need to recalculate expiry time after changing divisor. */
104 switch (value
& TIMER_TCR_TPSC
) {
105 case 0: freq
>>= 2; break;
106 case 1: freq
>>= 4; break;
107 case 2: freq
>>= 6; break;
108 case 3: freq
>>= 8; break;
109 case 4: freq
>>= 10; break;
111 case 7: if (s
->feat
& TIMER_FEAT_EXTCLK
) break;
112 default: hw_error("sh_timer_write: Reserved TPSC value\n"); break;
114 switch ((value
& TIMER_TCR_CKEG
) >> 3) {
118 case 3: if (s
->feat
& TIMER_FEAT_EXTCLK
) break;
119 default: hw_error("sh_timer_write: Reserved CKEG value\n"); break;
121 switch ((value
& TIMER_TCR_ICPE
) >> 6) {
124 case 3: if (s
->feat
& TIMER_FEAT_CAPT
) break;
125 default: hw_error("sh_timer_write: Reserved ICPE value\n"); break;
127 if ((value
& TIMER_TCR_UNF
) == 0)
130 value
&= ~TIMER_TCR_UNF
;
132 if ((value
& TIMER_TCR_ICPF
) && (!(s
->feat
& TIMER_FEAT_CAPT
)))
133 hw_error("sh_timer_write: Reserved ICPF value\n");
135 value
&= ~TIMER_TCR_ICPF
; /* capture not supported */
137 if (value
& TIMER_TCR_RESERVED
)
138 hw_error("sh_timer_write: Reserved TCR bits set\n");
140 ptimer_set_limit(s
->timer
, s
->tcor
, 0);
141 ptimer_set_freq(s
->timer
, freq
);
143 /* Restart the timer if still enabled. */
144 ptimer_run(s
->timer
, 0);
148 if (s
->feat
& TIMER_FEAT_CAPT
) {
153 hw_error("sh_timer_write: Bad offset %x\n", (int)offset
);
158 static void sh_timer_start_stop(void *opaque
, int enable
)
160 sh_timer_state
*s
= (sh_timer_state
*)opaque
;
163 printf("sh_timer_start_stop %d (%d)\n", enable
, s
->enabled
);
166 if (s
->enabled
&& !enable
) {
167 ptimer_stop(s
->timer
);
169 if (!s
->enabled
&& enable
) {
170 ptimer_run(s
->timer
, 0);
172 s
->enabled
= !!enable
;
175 printf("sh_timer_start_stop done %d\n", s
->enabled
);
179 static void sh_timer_tick(void *opaque
)
181 sh_timer_state
*s
= (sh_timer_state
*)opaque
;
182 s
->int_level
= s
->enabled
;
186 static void *sh_timer_init(uint32_t freq
, int feat
, qemu_irq irq
)
191 s
= (sh_timer_state
*)qemu_mallocz(sizeof(sh_timer_state
));
194 s
->tcor
= 0xffffffff;
195 s
->tcnt
= 0xffffffff;
196 s
->tcpr
= 0xdeadbeef;
201 bh
= qemu_bh_new(sh_timer_tick
, s
);
202 s
->timer
= ptimer_init(bh
);
204 sh_timer_write(s
, OFFSET_TCOR
>> 2, s
->tcor
);
205 sh_timer_write(s
, OFFSET_TCNT
>> 2, s
->tcnt
);
206 sh_timer_write(s
, OFFSET_TCPR
>> 2, s
->tcpr
);
207 sh_timer_write(s
, OFFSET_TCR
>> 2, s
->tcpr
);
208 /* ??? Save/restore. */
220 static uint32_t tmu012_read(void *opaque
, target_phys_addr_t offset
)
222 tmu012_state
*s
= (tmu012_state
*)opaque
;
225 printf("tmu012_read 0x%lx\n", (unsigned long) offset
);
228 if (offset
>= 0x20) {
229 if (!(s
->feat
& TMU012_FEAT_3CHAN
))
230 hw_error("tmu012_write: Bad channel offset %x\n", (int)offset
);
231 return sh_timer_read(s
->timer
[2], offset
- 0x20);
235 return sh_timer_read(s
->timer
[1], offset
- 0x14);
238 return sh_timer_read(s
->timer
[0], offset
- 0x08);
243 if ((s
->feat
& TMU012_FEAT_TOCR
) && offset
== 0)
246 hw_error("tmu012_write: Bad offset %x\n", (int)offset
);
250 static void tmu012_write(void *opaque
, target_phys_addr_t offset
,
253 tmu012_state
*s
= (tmu012_state
*)opaque
;
256 printf("tmu012_write 0x%lx 0x%08x\n", (unsigned long) offset
, value
);
259 if (offset
>= 0x20) {
260 if (!(s
->feat
& TMU012_FEAT_3CHAN
))
261 hw_error("tmu012_write: Bad channel offset %x\n", (int)offset
);
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 hw_error("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. */