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 licensed under the GPL.
13 #include "qemu/timer.h"
14 #include "exec/address-spaces.h"
15 #include "hw/ptimer.h"
19 #define TIMER_TCR_TPSC (7 << 0)
20 #define TIMER_TCR_CKEG (3 << 3)
21 #define TIMER_TCR_UNIE (1 << 5)
22 #define TIMER_TCR_ICPE (3 << 6)
23 #define TIMER_TCR_UNF (1 << 8)
24 #define TIMER_TCR_ICPF (1 << 9)
25 #define TIMER_TCR_RESERVED (0x3f << 10)
27 #define TIMER_FEAT_CAPT (1 << 0)
28 #define TIMER_FEAT_EXTCLK (1 << 1)
49 /* Check all active timers, and schedule the next timer interrupt. */
51 static void sh_timer_update(sh_timer_state
*s
)
53 int new_level
= s
->int_level
&& (s
->tcr
& TIMER_TCR_UNIE
);
55 if (new_level
!= s
->old_level
)
56 qemu_set_irq (s
->irq
, new_level
);
58 s
->old_level
= s
->int_level
;
59 s
->int_level
= new_level
;
62 static uint32_t sh_timer_read(void *opaque
, hwaddr offset
)
64 sh_timer_state
*s
= (sh_timer_state
*)opaque
;
66 switch (offset
>> 2) {
70 return ptimer_get_count(s
->timer
);
72 return s
->tcr
| (s
->int_level
? TIMER_TCR_UNF
: 0);
74 if (s
->feat
& TIMER_FEAT_CAPT
)
77 hw_error("sh_timer_read: Bad offset %x\n", (int)offset
);
82 static void sh_timer_write(void *opaque
, hwaddr offset
,
85 sh_timer_state
*s
= (sh_timer_state
*)opaque
;
88 switch (offset
>> 2) {
91 ptimer_set_limit(s
->timer
, s
->tcor
, 0);
95 ptimer_set_count(s
->timer
, s
->tcnt
);
99 /* Pause the timer if it is running. This may cause some
100 inaccuracy dure to rounding, but avoids a whole lot of other
102 ptimer_stop(s
->timer
);
105 /* ??? Need to recalculate expiry time after changing divisor. */
106 switch (value
& TIMER_TCR_TPSC
) {
107 case 0: freq
>>= 2; break;
108 case 1: freq
>>= 4; break;
109 case 2: freq
>>= 6; break;
110 case 3: freq
>>= 8; break;
111 case 4: freq
>>= 10; break;
113 case 7: if (s
->feat
& TIMER_FEAT_EXTCLK
) break;
114 default: hw_error("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: hw_error("sh_timer_write: Reserved CKEG value\n"); break;
123 switch ((value
& TIMER_TCR_ICPE
) >> 6) {
126 case 3: if (s
->feat
& TIMER_FEAT_CAPT
) break;
127 default: hw_error("sh_timer_write: Reserved ICPE value\n"); break;
129 if ((value
& TIMER_TCR_UNF
) == 0)
132 value
&= ~TIMER_TCR_UNF
;
134 if ((value
& TIMER_TCR_ICPF
) && (!(s
->feat
& TIMER_FEAT_CAPT
)))
135 hw_error("sh_timer_write: Reserved ICPF value\n");
137 value
&= ~TIMER_TCR_ICPF
; /* capture not supported */
139 if (value
& TIMER_TCR_RESERVED
)
140 hw_error("sh_timer_write: Reserved TCR bits set\n");
142 ptimer_set_limit(s
->timer
, s
->tcor
, 0);
143 ptimer_set_freq(s
->timer
, freq
);
145 /* Restart the timer if still enabled. */
146 ptimer_run(s
->timer
, 0);
150 if (s
->feat
& TIMER_FEAT_CAPT
) {
155 hw_error("sh_timer_write: Bad offset %x\n", (int)offset
);
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
*)g_malloc0(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
);
206 sh_timer_write(s
, OFFSET_TCOR
>> 2, s
->tcor
);
207 sh_timer_write(s
, OFFSET_TCNT
>> 2, s
->tcnt
);
208 sh_timer_write(s
, OFFSET_TCPR
>> 2, s
->tcpr
);
209 sh_timer_write(s
, OFFSET_TCR
>> 2, s
->tcpr
);
210 /* ??? Save/restore. */
216 MemoryRegion iomem_p4
;
217 MemoryRegion iomem_a7
;
225 static uint64_t tmu012_read(void *opaque
, hwaddr offset
,
228 tmu012_state
*s
= (tmu012_state
*)opaque
;
231 printf("tmu012_read 0x%lx\n", (unsigned long) offset
);
234 if (offset
>= 0x20) {
235 if (!(s
->feat
& TMU012_FEAT_3CHAN
))
236 hw_error("tmu012_write: Bad channel offset %x\n", (int)offset
);
237 return sh_timer_read(s
->timer
[2], offset
- 0x20);
241 return sh_timer_read(s
->timer
[1], offset
- 0x14);
244 return sh_timer_read(s
->timer
[0], offset
- 0x08);
249 if ((s
->feat
& TMU012_FEAT_TOCR
) && offset
== 0)
252 hw_error("tmu012_write: Bad offset %x\n", (int)offset
);
256 static void tmu012_write(void *opaque
, hwaddr offset
,
257 uint64_t value
, unsigned size
)
259 tmu012_state
*s
= (tmu012_state
*)opaque
;
262 printf("tmu012_write 0x%lx 0x%08x\n", (unsigned long) offset
, value
);
265 if (offset
>= 0x20) {
266 if (!(s
->feat
& TMU012_FEAT_3CHAN
))
267 hw_error("tmu012_write: Bad channel offset %x\n", (int)offset
);
268 sh_timer_write(s
->timer
[2], offset
- 0x20, value
);
272 if (offset
>= 0x14) {
273 sh_timer_write(s
->timer
[1], offset
- 0x14, value
);
277 if (offset
>= 0x08) {
278 sh_timer_write(s
->timer
[0], offset
- 0x08, value
);
283 sh_timer_start_stop(s
->timer
[0], value
& (1 << 0));
284 sh_timer_start_stop(s
->timer
[1], value
& (1 << 1));
285 if (s
->feat
& TMU012_FEAT_3CHAN
)
286 sh_timer_start_stop(s
->timer
[2], value
& (1 << 2));
288 if (value
& (1 << 2))
289 hw_error("tmu012_write: Bad channel\n");
295 if ((s
->feat
& TMU012_FEAT_TOCR
) && offset
== 0) {
296 s
->tocr
= value
& (1 << 0);
300 static const MemoryRegionOps tmu012_ops
= {
302 .write
= tmu012_write
,
303 .endianness
= DEVICE_NATIVE_ENDIAN
,
306 void tmu012_init(MemoryRegion
*sysmem
, hwaddr base
,
307 int feat
, uint32_t freq
,
308 qemu_irq ch0_irq
, qemu_irq ch1_irq
,
309 qemu_irq ch2_irq0
, qemu_irq ch2_irq1
)
312 int timer_feat
= (feat
& TMU012_FEAT_EXTCLK
) ? TIMER_FEAT_EXTCLK
: 0;
314 s
= (tmu012_state
*)g_malloc0(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 */
322 memory_region_init_io(&s
->iomem
, &tmu012_ops
, s
,
323 "timer", 0x100000000ULL
);
325 memory_region_init_alias(&s
->iomem_p4
, "timer-p4",
326 &s
->iomem
, 0, 0x1000);
327 memory_region_add_subregion(sysmem
, P4ADDR(base
), &s
->iomem_p4
);
329 memory_region_init_alias(&s
->iomem_a7
, "timer-a7",
330 &s
->iomem
, 0, 0x1000);
331 memory_region_add_subregion(sysmem
, A7ADDR(base
), &s
->iomem_a7
);
332 /* ??? Save/restore. */