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.
11 #include "qemu/osdep.h"
12 #include "exec/memory.h"
15 #include "hw/sh4/sh.h"
16 #include "hw/timer/tmu012.h"
17 #include "hw/ptimer.h"
20 #define TIMER_TCR_TPSC (7 << 0)
21 #define TIMER_TCR_CKEG (3 << 3)
22 #define TIMER_TCR_UNIE (1 << 5)
23 #define TIMER_TCR_ICPE (3 << 6)
24 #define TIMER_TCR_UNF (1 << 8)
25 #define TIMER_TCR_ICPF (1 << 9)
26 #define TIMER_TCR_RESERVED (0x3f << 10)
28 #define TIMER_FEAT_CAPT (1 << 0)
29 #define TIMER_FEAT_EXTCLK (1 << 1)
50 /* Check all active timers, and schedule the next timer interrupt. */
52 static void sh_timer_update(SHTimerState
*s
)
54 int new_level
= s
->int_level
&& (s
->tcr
& TIMER_TCR_UNIE
);
56 if (new_level
!= s
->old_level
) {
57 qemu_set_irq(s
->irq
, new_level
);
59 s
->old_level
= s
->int_level
;
60 s
->int_level
= new_level
;
63 static uint32_t sh_timer_read(void *opaque
, hwaddr offset
)
65 SHTimerState
*s
= opaque
;
67 switch (offset
>> 2) {
71 return ptimer_get_count(s
->timer
);
73 return s
->tcr
| (s
->int_level
? TIMER_TCR_UNF
: 0);
75 if (s
->feat
& TIMER_FEAT_CAPT
) {
79 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset 0x%" HWADDR_PRIx
"\n",
84 static void sh_timer_write(void *opaque
, hwaddr offset
, uint32_t value
)
86 SHTimerState
*s
= opaque
;
89 switch (offset
>> 2) {
92 ptimer_transaction_begin(s
->timer
);
93 ptimer_set_limit(s
->timer
, s
->tcor
, 0);
94 ptimer_transaction_commit(s
->timer
);
98 ptimer_transaction_begin(s
->timer
);
99 ptimer_set_count(s
->timer
, s
->tcnt
);
100 ptimer_transaction_commit(s
->timer
);
103 ptimer_transaction_begin(s
->timer
);
106 * Pause the timer if it is running. This may cause some inaccuracy
107 * due to rounding, but avoids a whole lot of other messiness
109 ptimer_stop(s
->timer
);
112 /* ??? Need to recalculate expiry time after changing divisor. */
113 switch (value
& TIMER_TCR_TPSC
) {
131 if (s
->feat
& TIMER_FEAT_EXTCLK
) {
136 qemu_log_mask(LOG_GUEST_ERROR
,
137 "%s: Reserved TPSC value\n", __func__
);
139 switch ((value
& TIMER_TCR_CKEG
) >> 3) {
145 if (s
->feat
& TIMER_FEAT_EXTCLK
) {
150 qemu_log_mask(LOG_GUEST_ERROR
,
151 "%s: Reserved CKEG value\n", __func__
);
153 switch ((value
& TIMER_TCR_ICPE
) >> 6) {
158 if (s
->feat
& TIMER_FEAT_CAPT
) {
163 qemu_log_mask(LOG_GUEST_ERROR
,
164 "%s: Reserved ICPE value\n", __func__
);
166 if ((value
& TIMER_TCR_UNF
) == 0) {
170 value
&= ~TIMER_TCR_UNF
;
172 if ((value
& TIMER_TCR_ICPF
) && (!(s
->feat
& TIMER_FEAT_CAPT
))) {
173 qemu_log_mask(LOG_GUEST_ERROR
,
174 "%s: Reserved ICPF value\n", __func__
);
177 value
&= ~TIMER_TCR_ICPF
; /* capture not supported */
179 if (value
& TIMER_TCR_RESERVED
) {
180 qemu_log_mask(LOG_GUEST_ERROR
,
181 "%s: Reserved TCR bits set\n", __func__
);
184 ptimer_set_limit(s
->timer
, s
->tcor
, 0);
185 ptimer_set_freq(s
->timer
, freq
);
187 /* Restart the timer if still enabled. */
188 ptimer_run(s
->timer
, 0);
190 ptimer_transaction_commit(s
->timer
);
193 if (s
->feat
& TIMER_FEAT_CAPT
) {
199 qemu_log_mask(LOG_GUEST_ERROR
,
200 "%s: Bad offset 0x%" HWADDR_PRIx
"\n", __func__
, offset
);
205 static void sh_timer_start_stop(void *opaque
, int enable
)
207 SHTimerState
*s
= opaque
;
209 trace_sh_timer_start_stop(enable
, s
->enabled
);
210 ptimer_transaction_begin(s
->timer
);
211 if (s
->enabled
&& !enable
) {
212 ptimer_stop(s
->timer
);
214 if (!s
->enabled
&& enable
) {
215 ptimer_run(s
->timer
, 0);
217 ptimer_transaction_commit(s
->timer
);
218 s
->enabled
= !!enable
;
221 static void sh_timer_tick(void *opaque
)
223 SHTimerState
*s
= opaque
;
224 s
->int_level
= s
->enabled
;
228 static void *sh_timer_init(uint32_t freq
, int feat
, qemu_irq irq
)
232 s
= g_malloc0(sizeof(*s
));
235 s
->tcor
= 0xffffffff;
236 s
->tcnt
= 0xffffffff;
237 s
->tcpr
= 0xdeadbeef;
242 s
->timer
= ptimer_init(sh_timer_tick
, s
, PTIMER_POLICY_DEFAULT
);
244 sh_timer_write(s
, OFFSET_TCOR
>> 2, s
->tcor
);
245 sh_timer_write(s
, OFFSET_TCNT
>> 2, s
->tcnt
);
246 sh_timer_write(s
, OFFSET_TCPR
>> 2, s
->tcpr
);
247 sh_timer_write(s
, OFFSET_TCR
>> 2, s
->tcpr
);
248 /* ??? Save/restore. */
254 MemoryRegion iomem_p4
;
255 MemoryRegion iomem_a7
;
263 static uint64_t tmu012_read(void *opaque
, hwaddr offset
, unsigned size
)
265 tmu012_state
*s
= opaque
;
267 trace_sh_timer_read(offset
);
268 if (offset
>= 0x20) {
269 if (!(s
->feat
& TMU012_FEAT_3CHAN
)) {
270 qemu_log_mask(LOG_GUEST_ERROR
,
271 "%s: Bad channel offset 0x%" HWADDR_PRIx
"\n",
274 return sh_timer_read(s
->timer
[2], offset
- 0x20);
277 if (offset
>= 0x14) {
278 return sh_timer_read(s
->timer
[1], offset
- 0x14);
280 if (offset
>= 0x08) {
281 return sh_timer_read(s
->timer
[0], offset
- 0x08);
286 if ((s
->feat
& TMU012_FEAT_TOCR
) && offset
== 0) {
290 qemu_log_mask(LOG_GUEST_ERROR
,
291 "%s: Bad offset 0x%" HWADDR_PRIx
"\n", __func__
, offset
);
295 static void tmu012_write(void *opaque
, hwaddr offset
,
296 uint64_t value
, unsigned size
)
298 tmu012_state
*s
= opaque
;
300 trace_sh_timer_write(offset
, value
);
301 if (offset
>= 0x20) {
302 if (!(s
->feat
& TMU012_FEAT_3CHAN
)) {
303 qemu_log_mask(LOG_GUEST_ERROR
,
304 "%s: Bad channel offset 0x%" HWADDR_PRIx
"\n",
307 sh_timer_write(s
->timer
[2], offset
- 0x20, value
);
311 if (offset
>= 0x14) {
312 sh_timer_write(s
->timer
[1], offset
- 0x14, value
);
316 if (offset
>= 0x08) {
317 sh_timer_write(s
->timer
[0], offset
- 0x08, value
);
322 sh_timer_start_stop(s
->timer
[0], value
& (1 << 0));
323 sh_timer_start_stop(s
->timer
[1], value
& (1 << 1));
324 if (s
->feat
& TMU012_FEAT_3CHAN
) {
325 sh_timer_start_stop(s
->timer
[2], value
& (1 << 2));
327 if (value
& (1 << 2)) {
328 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad channel\n", __func__
);
336 if ((s
->feat
& TMU012_FEAT_TOCR
) && offset
== 0) {
337 s
->tocr
= value
& (1 << 0);
341 static const MemoryRegionOps tmu012_ops
= {
343 .write
= tmu012_write
,
344 .endianness
= DEVICE_NATIVE_ENDIAN
,
347 void tmu012_init(MemoryRegion
*sysmem
, hwaddr base
, int feat
, uint32_t freq
,
348 qemu_irq ch0_irq
, qemu_irq ch1_irq
,
349 qemu_irq ch2_irq0
, qemu_irq ch2_irq1
)
352 int timer_feat
= (feat
& TMU012_FEAT_EXTCLK
) ? TIMER_FEAT_EXTCLK
: 0;
354 s
= g_malloc0(sizeof(*s
));
356 s
->timer
[0] = sh_timer_init(freq
, timer_feat
, ch0_irq
);
357 s
->timer
[1] = sh_timer_init(freq
, timer_feat
, ch1_irq
);
358 if (feat
& TMU012_FEAT_3CHAN
) {
359 s
->timer
[2] = sh_timer_init(freq
, timer_feat
| TIMER_FEAT_CAPT
,
360 ch2_irq0
); /* ch2_irq1 not supported */
363 memory_region_init_io(&s
->iomem
, NULL
, &tmu012_ops
, s
, "timer", 0x30);
365 memory_region_init_alias(&s
->iomem_p4
, NULL
, "timer-p4",
366 &s
->iomem
, 0, memory_region_size(&s
->iomem
));
367 memory_region_add_subregion(sysmem
, P4ADDR(base
), &s
->iomem_p4
);
369 memory_region_init_alias(&s
->iomem_a7
, NULL
, "timer-a7",
370 &s
->iomem
, 0, memory_region_size(&s
->iomem
));
371 memory_region_add_subregion(sysmem
, A7ADDR(base
), &s
->iomem_a7
);
372 /* ??? Save/restore. */