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"
21 #define TIMER_TCR_TPSC (7 << 0)
22 #define TIMER_TCR_CKEG (3 << 3)
23 #define TIMER_TCR_UNIE (1 << 5)
24 #define TIMER_TCR_ICPE (3 << 6)
25 #define TIMER_TCR_UNF (1 << 8)
26 #define TIMER_TCR_ICPF (1 << 9)
27 #define TIMER_TCR_RESERVED (0x3f << 10)
29 #define TIMER_FEAT_CAPT (1 << 0)
30 #define TIMER_FEAT_EXTCLK (1 << 1)
51 /* Check all active timers, and schedule the next timer interrupt. */
53 static void sh_timer_update(sh_timer_state
*s
)
55 int new_level
= s
->int_level
&& (s
->tcr
& TIMER_TCR_UNIE
);
57 if (new_level
!= s
->old_level
) {
58 qemu_set_irq(s
->irq
, new_level
);
60 s
->old_level
= s
->int_level
;
61 s
->int_level
= new_level
;
64 static uint32_t sh_timer_read(void *opaque
, hwaddr offset
)
66 sh_timer_state
*s
= (sh_timer_state
*)opaque
;
68 switch (offset
>> 2) {
72 return ptimer_get_count(s
->timer
);
74 return s
->tcr
| (s
->int_level
? TIMER_TCR_UNF
: 0);
76 if (s
->feat
& TIMER_FEAT_CAPT
) {
81 hw_error("sh_timer_read: Bad offset %x\n", (int)offset
);
86 static void sh_timer_write(void *opaque
, hwaddr offset
,
89 sh_timer_state
*s
= (sh_timer_state
*)opaque
;
92 switch (offset
>> 2) {
95 ptimer_transaction_begin(s
->timer
);
96 ptimer_set_limit(s
->timer
, s
->tcor
, 0);
97 ptimer_transaction_commit(s
->timer
);
101 ptimer_transaction_begin(s
->timer
);
102 ptimer_set_count(s
->timer
, s
->tcnt
);
103 ptimer_transaction_commit(s
->timer
);
106 ptimer_transaction_begin(s
->timer
);
109 * Pause the timer if it is running. This may cause some inaccuracy
110 * due to rounding, but avoids a whole lot of other messiness
112 ptimer_stop(s
->timer
);
115 /* ??? Need to recalculate expiry time after changing divisor. */
116 switch (value
& TIMER_TCR_TPSC
) {
134 if (s
->feat
& TIMER_FEAT_EXTCLK
) {
139 hw_error("sh_timer_write: Reserved TPSC value\n");
141 switch ((value
& TIMER_TCR_CKEG
) >> 3) {
147 if (s
->feat
& TIMER_FEAT_EXTCLK
) {
152 hw_error("sh_timer_write: Reserved CKEG value\n");
154 switch ((value
& TIMER_TCR_ICPE
) >> 6) {
159 if (s
->feat
& TIMER_FEAT_CAPT
) {
164 hw_error("sh_timer_write: Reserved ICPE value\n");
166 if ((value
& TIMER_TCR_UNF
) == 0) {
170 value
&= ~TIMER_TCR_UNF
;
172 if ((value
& TIMER_TCR_ICPF
) && (!(s
->feat
& TIMER_FEAT_CAPT
))) {
173 hw_error("sh_timer_write: Reserved ICPF value\n");
176 value
&= ~TIMER_TCR_ICPF
; /* capture not supported */
178 if (value
& TIMER_TCR_RESERVED
) {
179 hw_error("sh_timer_write: Reserved TCR bits set\n");
182 ptimer_set_limit(s
->timer
, s
->tcor
, 0);
183 ptimer_set_freq(s
->timer
, freq
);
185 /* Restart the timer if still enabled. */
186 ptimer_run(s
->timer
, 0);
188 ptimer_transaction_commit(s
->timer
);
191 if (s
->feat
& TIMER_FEAT_CAPT
) {
197 hw_error("sh_timer_write: Bad offset %x\n", (int)offset
);
202 static void sh_timer_start_stop(void *opaque
, int enable
)
204 sh_timer_state
*s
= (sh_timer_state
*)opaque
;
207 printf("sh_timer_start_stop %d (%d)\n", 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 printf("sh_timer_start_stop done %d\n", s
->enabled
);
225 static void sh_timer_tick(void *opaque
)
227 sh_timer_state
*s
= (sh_timer_state
*)opaque
;
228 s
->int_level
= s
->enabled
;
232 static void *sh_timer_init(uint32_t freq
, int feat
, qemu_irq irq
)
236 s
= g_malloc0(sizeof(*s
));
239 s
->tcor
= 0xffffffff;
240 s
->tcnt
= 0xffffffff;
241 s
->tcpr
= 0xdeadbeef;
246 s
->timer
= ptimer_init(sh_timer_tick
, s
, PTIMER_POLICY_DEFAULT
);
248 sh_timer_write(s
, OFFSET_TCOR
>> 2, s
->tcor
);
249 sh_timer_write(s
, OFFSET_TCNT
>> 2, s
->tcnt
);
250 sh_timer_write(s
, OFFSET_TCPR
>> 2, s
->tcpr
);
251 sh_timer_write(s
, OFFSET_TCR
>> 2, s
->tcpr
);
252 /* ??? Save/restore. */
258 MemoryRegion iomem_p4
;
259 MemoryRegion iomem_a7
;
267 static uint64_t tmu012_read(void *opaque
, hwaddr offset
,
270 tmu012_state
*s
= (tmu012_state
*)opaque
;
273 printf("tmu012_read 0x%lx\n", (unsigned long) offset
);
276 if (offset
>= 0x20) {
277 if (!(s
->feat
& TMU012_FEAT_3CHAN
)) {
278 hw_error("tmu012_write: Bad channel offset %x\n", (int)offset
);
280 return sh_timer_read(s
->timer
[2], offset
- 0x20);
283 if (offset
>= 0x14) {
284 return sh_timer_read(s
->timer
[1], offset
- 0x14);
286 if (offset
>= 0x08) {
287 return sh_timer_read(s
->timer
[0], offset
- 0x08);
292 if ((s
->feat
& TMU012_FEAT_TOCR
) && offset
== 0) {
296 hw_error("tmu012_write: Bad offset %x\n", (int)offset
);
300 static void tmu012_write(void *opaque
, hwaddr offset
,
301 uint64_t value
, unsigned size
)
303 tmu012_state
*s
= (tmu012_state
*)opaque
;
306 printf("tmu012_write 0x%lx 0x%08x\n", (unsigned long) offset
, value
);
309 if (offset
>= 0x20) {
310 if (!(s
->feat
& TMU012_FEAT_3CHAN
)) {
311 hw_error("tmu012_write: Bad channel offset %x\n", (int)offset
);
313 sh_timer_write(s
->timer
[2], offset
- 0x20, value
);
317 if (offset
>= 0x14) {
318 sh_timer_write(s
->timer
[1], offset
- 0x14, value
);
322 if (offset
>= 0x08) {
323 sh_timer_write(s
->timer
[0], offset
- 0x08, value
);
328 sh_timer_start_stop(s
->timer
[0], value
& (1 << 0));
329 sh_timer_start_stop(s
->timer
[1], value
& (1 << 1));
330 if (s
->feat
& TMU012_FEAT_3CHAN
) {
331 sh_timer_start_stop(s
->timer
[2], value
& (1 << 2));
333 if (value
& (1 << 2)) {
334 hw_error("tmu012_write: Bad channel\n");
342 if ((s
->feat
& TMU012_FEAT_TOCR
) && offset
== 0) {
343 s
->tocr
= value
& (1 << 0);
347 static const MemoryRegionOps tmu012_ops
= {
349 .write
= tmu012_write
,
350 .endianness
= DEVICE_NATIVE_ENDIAN
,
353 void tmu012_init(MemoryRegion
*sysmem
, hwaddr base
,
354 int feat
, uint32_t freq
,
355 qemu_irq ch0_irq
, qemu_irq ch1_irq
,
356 qemu_irq ch2_irq0
, qemu_irq ch2_irq1
)
359 int timer_feat
= (feat
& TMU012_FEAT_EXTCLK
) ? TIMER_FEAT_EXTCLK
: 0;
361 s
= g_malloc0(sizeof(*s
));
363 s
->timer
[0] = sh_timer_init(freq
, timer_feat
, ch0_irq
);
364 s
->timer
[1] = sh_timer_init(freq
, timer_feat
, ch1_irq
);
365 if (feat
& TMU012_FEAT_3CHAN
) {
366 s
->timer
[2] = sh_timer_init(freq
, timer_feat
| TIMER_FEAT_CAPT
,
367 ch2_irq0
); /* ch2_irq1 not supported */
370 memory_region_init_io(&s
->iomem
, NULL
, &tmu012_ops
, s
,
371 "timer", 0x100000000ULL
);
373 memory_region_init_alias(&s
->iomem_p4
, NULL
, "timer-p4",
374 &s
->iomem
, 0, 0x1000);
375 memory_region_add_subregion(sysmem
, P4ADDR(base
), &s
->iomem_p4
);
377 memory_region_init_alias(&s
->iomem_a7
, NULL
, "timer-a7",
378 &s
->iomem
, 0, 0x1000);
379 memory_region_add_subregion(sysmem
, A7ADDR(base
), &s
->iomem_a7
);
380 /* ??? Save/restore. */