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)
39 struct intc_source
*irq
;
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 sh_intc_toggle_source(s
->irq
, 0, new_level
? 1 : -1);
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
, struct intc_source
*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. */
214 target_phys_addr_t base
;
218 static uint32_t tmu012_read(void *opaque
, target_phys_addr_t offset
)
220 tmu012_state
*s
= (tmu012_state
*)opaque
;
223 printf("tmu012_read 0x%lx\n", (unsigned long) offset
);
227 if (offset
>= 0x20) {
228 if (!(s
->feat
& TMU012_FEAT_3CHAN
))
229 cpu_abort (cpu_single_env
, "tmu012_write: Bad channel offset %x\n",
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 cpu_abort (cpu_single_env
, "tmu012_write: Bad offset %x\n",
251 static void tmu012_write(void *opaque
, target_phys_addr_t offset
,
254 tmu012_state
*s
= (tmu012_state
*)opaque
;
257 printf("tmu012_write 0x%lx 0x%08x\n", (unsigned long) offset
, value
);
261 if (offset
>= 0x20) {
262 if (!(s
->feat
& TMU012_FEAT_3CHAN
))
263 cpu_abort (cpu_single_env
, "tmu012_write: Bad channel offset %x\n",
265 sh_timer_write(s
->timer
[2], offset
- 0x20, value
);
269 if (offset
>= 0x14) {
270 sh_timer_write(s
->timer
[1], offset
- 0x14, value
);
274 if (offset
>= 0x08) {
275 sh_timer_write(s
->timer
[0], offset
- 0x08, value
);
280 sh_timer_start_stop(s
->timer
[0], value
& (1 << 0));
281 sh_timer_start_stop(s
->timer
[1], value
& (1 << 1));
282 if (s
->feat
& TMU012_FEAT_3CHAN
)
283 sh_timer_start_stop(s
->timer
[2], value
& (1 << 2));
285 if (value
& (1 << 2))
286 cpu_abort (cpu_single_env
, "tmu012_write: Bad channel\n");
292 if ((s
->feat
& TMU012_FEAT_TOCR
) && offset
== 0) {
293 s
->tocr
= value
& (1 << 0);
297 static CPUReadMemoryFunc
*tmu012_readfn
[] = {
303 static CPUWriteMemoryFunc
*tmu012_writefn
[] = {
309 void tmu012_init(target_phys_addr_t base
, int feat
, uint32_t freq
,
310 struct intc_source
*ch0_irq
, struct intc_source
*ch1_irq
,
311 struct intc_source
*ch2_irq0
, struct intc_source
*ch2_irq1
)
315 int timer_feat
= (feat
& TMU012_FEAT_EXTCLK
) ? TIMER_FEAT_EXTCLK
: 0;
317 s
= (tmu012_state
*)qemu_mallocz(sizeof(tmu012_state
));
320 s
->timer
[0] = sh_timer_init(freq
, timer_feat
, ch0_irq
);
321 s
->timer
[1] = sh_timer_init(freq
, timer_feat
, ch1_irq
);
322 if (feat
& TMU012_FEAT_3CHAN
)
323 s
->timer
[2] = sh_timer_init(freq
, timer_feat
| TIMER_FEAT_CAPT
,
324 ch2_irq0
); /* ch2_irq1 not supported */
325 iomemtype
= cpu_register_io_memory(0, tmu012_readfn
,
327 cpu_register_physical_memory(base
, 0x00001000, iomemtype
);
328 /* ??? Save/restore. */