xref: /qemu/hw/timer/sh_timer.c (revision 65307c7792a50bffe036423ec21107f4fb9c74e3)
1cd1a3f68Sths /*
2cd1a3f68Sths  * SuperH Timer modules.
3cd1a3f68Sths  *
4cd1a3f68Sths  * Copyright (c) 2007 Magnus Damm
5cd1a3f68Sths  * Based on arm_timer.c by Paul Brook
6cd1a3f68Sths  * Copyright (c) 2005-2006 CodeSourcery.
7cd1a3f68Sths  *
88e31bf38SMatthew Fernandez  * This code is licensed under the GPL.
9cd1a3f68Sths  */
10cd1a3f68Sths 
11282bc81eSPeter Maydell #include "qemu/osdep.h"
1295f4dc44SPhilippe Mathieu-Daudé #include "exec/memory.h"
1383c9f4caSPaolo Bonzini #include "hw/hw.h"
1464552b6bSMarkus Armbruster #include "hw/irq.h"
150d09e41aSPaolo Bonzini #include "hw/sh4/sh.h"
1695f4dc44SPhilippe Mathieu-Daudé #include "hw/timer/tmu012.h"
1783c9f4caSPaolo Bonzini #include "hw/ptimer.h"
18ad52cfc1SBALATON Zoltan #include "trace.h"
19cd1a3f68Sths 
20cd1a3f68Sths #define TIMER_TCR_TPSC          (7 << 0)
21cd1a3f68Sths #define TIMER_TCR_CKEG          (3 << 3)
22cd1a3f68Sths #define TIMER_TCR_UNIE          (1 << 5)
23cd1a3f68Sths #define TIMER_TCR_ICPE          (3 << 6)
24cd1a3f68Sths #define TIMER_TCR_UNF           (1 << 8)
25cd1a3f68Sths #define TIMER_TCR_ICPF          (1 << 9)
26cd1a3f68Sths #define TIMER_TCR_RESERVED      (0x3f << 10)
27cd1a3f68Sths 
28cd1a3f68Sths #define TIMER_FEAT_CAPT   (1 << 0)
29cd1a3f68Sths #define TIMER_FEAT_EXTCLK (1 << 1)
30cd1a3f68Sths 
31e7786f27Saurel32 #define OFFSET_TCOR   0
32e7786f27Saurel32 #define OFFSET_TCNT   1
33e7786f27Saurel32 #define OFFSET_TCR    2
34e7786f27Saurel32 #define OFFSET_TCPR   3
35e7786f27Saurel32 
36cd1a3f68Sths typedef struct {
37cd1a3f68Sths     ptimer_state *timer;
38cd1a3f68Sths     uint32_t tcnt;
39cd1a3f68Sths     uint32_t tcor;
40cd1a3f68Sths     uint32_t tcr;
41cd1a3f68Sths     uint32_t tcpr;
42cd1a3f68Sths     int freq;
43cd1a3f68Sths     int int_level;
44703243a0Sbalrog     int old_level;
45cd1a3f68Sths     int feat;
46cd1a3f68Sths     int enabled;
4796e2fc41Saurel32     qemu_irq irq;
485d9b737eSBALATON Zoltan } SHTimerState;
49cd1a3f68Sths 
50cd1a3f68Sths /* Check all active timers, and schedule the next timer interrupt. */
51cd1a3f68Sths 
525d9b737eSBALATON Zoltan static void sh_timer_update(SHTimerState *s)
53cd1a3f68Sths {
54703243a0Sbalrog     int new_level = s->int_level && (s->tcr & TIMER_TCR_UNIE);
55703243a0Sbalrog 
56ac3c9e74SBALATON Zoltan     if (new_level != s->old_level) {
5796e2fc41Saurel32         qemu_set_irq(s->irq, new_level);
58ac3c9e74SBALATON Zoltan     }
59703243a0Sbalrog     s->old_level = s->int_level;
60703243a0Sbalrog     s->int_level = new_level;
61cd1a3f68Sths }
62cd1a3f68Sths 
63a8170e5eSAvi Kivity static uint32_t sh_timer_read(void *opaque, hwaddr offset)
64cd1a3f68Sths {
655d9b737eSBALATON Zoltan     SHTimerState *s = opaque;
66cd1a3f68Sths 
67cd1a3f68Sths     switch (offset >> 2) {
68e7786f27Saurel32     case OFFSET_TCOR:
69cd1a3f68Sths         return s->tcor;
70e7786f27Saurel32     case OFFSET_TCNT:
71cd1a3f68Sths         return ptimer_get_count(s->timer);
72e7786f27Saurel32     case OFFSET_TCR:
73cd1a3f68Sths         return s->tcr | (s->int_level ? TIMER_TCR_UNF : 0);
74e7786f27Saurel32     case OFFSET_TCPR:
75ac3c9e74SBALATON Zoltan         if (s->feat & TIMER_FEAT_CAPT) {
76cd1a3f68Sths             return s->tcpr;
77ac3c9e74SBALATON Zoltan         }
78edd7541bSPaolo Bonzini         /* fall through */
79cd1a3f68Sths     default:
802ac71179SPaul Brook         hw_error("sh_timer_read: Bad offset %x\n", (int)offset);
81cd1a3f68Sths         return 0;
82cd1a3f68Sths     }
83cd1a3f68Sths }
84cd1a3f68Sths 
85f64ccec4SBALATON Zoltan static void sh_timer_write(void *opaque, hwaddr offset, uint32_t value)
86cd1a3f68Sths {
875d9b737eSBALATON Zoltan     SHTimerState *s = opaque;
88cd1a3f68Sths     int freq;
89cd1a3f68Sths 
90cd1a3f68Sths     switch (offset >> 2) {
91e7786f27Saurel32     case OFFSET_TCOR:
92cd1a3f68Sths         s->tcor = value;
9328015830SPeter Maydell         ptimer_transaction_begin(s->timer);
94cd1a3f68Sths         ptimer_set_limit(s->timer, s->tcor, 0);
9528015830SPeter Maydell         ptimer_transaction_commit(s->timer);
96cd1a3f68Sths         break;
97e7786f27Saurel32     case OFFSET_TCNT:
98cd1a3f68Sths         s->tcnt = value;
9928015830SPeter Maydell         ptimer_transaction_begin(s->timer);
100cd1a3f68Sths         ptimer_set_count(s->timer, s->tcnt);
10128015830SPeter Maydell         ptimer_transaction_commit(s->timer);
102cd1a3f68Sths         break;
103e7786f27Saurel32     case OFFSET_TCR:
10428015830SPeter Maydell         ptimer_transaction_begin(s->timer);
105cd1a3f68Sths         if (s->enabled) {
10622138965SBALATON Zoltan             /*
10722138965SBALATON Zoltan              * Pause the timer if it is running. This may cause some inaccuracy
1083b885dabSBALATON Zoltan              * due to rounding, but avoids a whole lot of other messiness
10922138965SBALATON Zoltan              */
110cd1a3f68Sths             ptimer_stop(s->timer);
111cd1a3f68Sths         }
112cd1a3f68Sths         freq = s->freq;
113cd1a3f68Sths         /* ??? Need to recalculate expiry time after changing divisor.  */
114cd1a3f68Sths         switch (value & TIMER_TCR_TPSC) {
115f94bff13SBALATON Zoltan         case 0:
116f94bff13SBALATON Zoltan             freq >>= 2;
117f94bff13SBALATON Zoltan             break;
118f94bff13SBALATON Zoltan         case 1:
119f94bff13SBALATON Zoltan             freq >>= 4;
120f94bff13SBALATON Zoltan             break;
121f94bff13SBALATON Zoltan         case 2:
122f94bff13SBALATON Zoltan             freq >>= 6;
123f94bff13SBALATON Zoltan             break;
124f94bff13SBALATON Zoltan         case 3:
125f94bff13SBALATON Zoltan             freq >>= 8;
126f94bff13SBALATON Zoltan             break;
127f94bff13SBALATON Zoltan         case 4:
128f94bff13SBALATON Zoltan             freq >>= 10;
129f94bff13SBALATON Zoltan             break;
130cd1a3f68Sths         case 6:
1312f5af2dcSThomas Huth         case 7:
1322f5af2dcSThomas Huth             if (s->feat & TIMER_FEAT_EXTCLK) {
1332f5af2dcSThomas Huth                 break;
1342f5af2dcSThomas Huth             }
13597edd8baSThomas Huth             /* fallthrough */
1362f5af2dcSThomas Huth         default:
1372f5af2dcSThomas Huth             hw_error("sh_timer_write: Reserved TPSC value\n");
138cd1a3f68Sths         }
139cd1a3f68Sths         switch ((value & TIMER_TCR_CKEG) >> 3) {
1402f5af2dcSThomas Huth         case 0:
1412f5af2dcSThomas Huth             break;
142cd1a3f68Sths         case 1:
143cd1a3f68Sths         case 2:
1442f5af2dcSThomas Huth         case 3:
1452f5af2dcSThomas Huth             if (s->feat & TIMER_FEAT_EXTCLK) {
1462f5af2dcSThomas Huth                 break;
1472f5af2dcSThomas Huth             }
14897edd8baSThomas Huth             /* fallthrough */
1492f5af2dcSThomas Huth         default:
1502f5af2dcSThomas Huth             hw_error("sh_timer_write: Reserved CKEG value\n");
151cd1a3f68Sths         }
152cd1a3f68Sths         switch ((value & TIMER_TCR_ICPE) >> 6) {
1532f5af2dcSThomas Huth         case 0:
1542f5af2dcSThomas Huth             break;
155cd1a3f68Sths         case 2:
1562f5af2dcSThomas Huth         case 3:
1572f5af2dcSThomas Huth             if (s->feat & TIMER_FEAT_CAPT) {
1582f5af2dcSThomas Huth                 break;
159cd1a3f68Sths             }
16097edd8baSThomas Huth             /* fallthrough */
1612f5af2dcSThomas Huth         default:
1622f5af2dcSThomas Huth             hw_error("sh_timer_write: Reserved ICPE value\n");
1632f5af2dcSThomas Huth         }
1642f5af2dcSThomas Huth         if ((value & TIMER_TCR_UNF) == 0) {
165cd1a3f68Sths             s->int_level = 0;
1662f5af2dcSThomas Huth         }
167cd1a3f68Sths 
168cd1a3f68Sths         value &= ~TIMER_TCR_UNF;
169cd1a3f68Sths 
1702f5af2dcSThomas Huth         if ((value & TIMER_TCR_ICPF) && (!(s->feat & TIMER_FEAT_CAPT))) {
1712ac71179SPaul Brook             hw_error("sh_timer_write: Reserved ICPF value\n");
1722f5af2dcSThomas Huth         }
173cd1a3f68Sths 
174cd1a3f68Sths         value &= ~TIMER_TCR_ICPF; /* capture not supported */
175cd1a3f68Sths 
1762f5af2dcSThomas Huth         if (value & TIMER_TCR_RESERVED) {
1772ac71179SPaul Brook             hw_error("sh_timer_write: Reserved TCR bits set\n");
1782f5af2dcSThomas Huth         }
179cd1a3f68Sths         s->tcr = value;
180cd1a3f68Sths         ptimer_set_limit(s->timer, s->tcor, 0);
181cd1a3f68Sths         ptimer_set_freq(s->timer, freq);
182cd1a3f68Sths         if (s->enabled) {
183cd1a3f68Sths             /* Restart the timer if still enabled.  */
184cd1a3f68Sths             ptimer_run(s->timer, 0);
185cd1a3f68Sths         }
18628015830SPeter Maydell         ptimer_transaction_commit(s->timer);
187cd1a3f68Sths         break;
188e7786f27Saurel32     case OFFSET_TCPR:
189cd1a3f68Sths         if (s->feat & TIMER_FEAT_CAPT) {
190cd1a3f68Sths             s->tcpr = value;
191cd1a3f68Sths             break;
192cd1a3f68Sths         }
19397edd8baSThomas Huth         /* fallthrough */
194cd1a3f68Sths     default:
1952ac71179SPaul Brook         hw_error("sh_timer_write: Bad offset %x\n", (int)offset);
196cd1a3f68Sths     }
197cd1a3f68Sths     sh_timer_update(s);
198cd1a3f68Sths }
199cd1a3f68Sths 
200cd1a3f68Sths static void sh_timer_start_stop(void *opaque, int enable)
201cd1a3f68Sths {
2025d9b737eSBALATON Zoltan     SHTimerState *s = opaque;
203cd1a3f68Sths 
204ad52cfc1SBALATON Zoltan     trace_sh_timer_start_stop(enable, s->enabled);
20528015830SPeter Maydell     ptimer_transaction_begin(s->timer);
206cd1a3f68Sths     if (s->enabled && !enable) {
207cd1a3f68Sths         ptimer_stop(s->timer);
208cd1a3f68Sths     }
209cd1a3f68Sths     if (!s->enabled && enable) {
210cd1a3f68Sths         ptimer_run(s->timer, 0);
211cd1a3f68Sths     }
21228015830SPeter Maydell     ptimer_transaction_commit(s->timer);
213cd1a3f68Sths     s->enabled = !!enable;
214cd1a3f68Sths }
215cd1a3f68Sths 
216cd1a3f68Sths static void sh_timer_tick(void *opaque)
217cd1a3f68Sths {
2185d9b737eSBALATON Zoltan     SHTimerState *s = opaque;
219cd1a3f68Sths     s->int_level = s->enabled;
220cd1a3f68Sths     sh_timer_update(s);
221cd1a3f68Sths }
222cd1a3f68Sths 
22396e2fc41Saurel32 static void *sh_timer_init(uint32_t freq, int feat, qemu_irq irq)
224cd1a3f68Sths {
2255d9b737eSBALATON Zoltan     SHTimerState *s;
226cd1a3f68Sths 
227373b96b9SBALATON Zoltan     s = g_malloc0(sizeof(*s));
228cd1a3f68Sths     s->freq = freq;
229cd1a3f68Sths     s->feat = feat;
230cd1a3f68Sths     s->tcor = 0xffffffff;
231cd1a3f68Sths     s->tcnt = 0xffffffff;
232cd1a3f68Sths     s->tcpr = 0xdeadbeef;
233e7786f27Saurel32     s->tcr = 0;
234cd1a3f68Sths     s->enabled = 0;
235703243a0Sbalrog     s->irq = irq;
236cd1a3f68Sths 
23728015830SPeter Maydell     s->timer = ptimer_init(sh_timer_tick, s, PTIMER_POLICY_DEFAULT);
238e7786f27Saurel32 
239e7786f27Saurel32     sh_timer_write(s, OFFSET_TCOR >> 2, s->tcor);
240e7786f27Saurel32     sh_timer_write(s, OFFSET_TCNT >> 2, s->tcnt);
241e7786f27Saurel32     sh_timer_write(s, OFFSET_TCPR >> 2, s->tcpr);
242e7786f27Saurel32     sh_timer_write(s, OFFSET_TCR  >> 2, s->tcpr);
243cd1a3f68Sths     /* ??? Save/restore.  */
244cd1a3f68Sths     return s;
245cd1a3f68Sths }
246cd1a3f68Sths 
247cd1a3f68Sths typedef struct {
24889e29451SBenoît Canet     MemoryRegion iomem;
24989e29451SBenoît Canet     MemoryRegion iomem_p4;
25089e29451SBenoît Canet     MemoryRegion iomem_a7;
251cd1a3f68Sths     void *timer[3];
252cd1a3f68Sths     int level[3];
253cd1a3f68Sths     uint32_t tocr;
254cd1a3f68Sths     uint32_t tstr;
255cd1a3f68Sths     int feat;
256cd1a3f68Sths } tmu012_state;
257cd1a3f68Sths 
258f64ccec4SBALATON Zoltan static uint64_t tmu012_read(void *opaque, hwaddr offset, unsigned size)
259cd1a3f68Sths {
2605d9b737eSBALATON Zoltan     tmu012_state *s = opaque;
261cd1a3f68Sths 
262ad52cfc1SBALATON Zoltan     trace_sh_timer_read(offset);
263cd1a3f68Sths     if (offset >= 0x20) {
2642f5af2dcSThomas Huth         if (!(s->feat & TMU012_FEAT_3CHAN)) {
2652ac71179SPaul Brook             hw_error("tmu012_write: Bad channel offset %x\n", (int)offset);
2662f5af2dcSThomas Huth         }
267cd1a3f68Sths         return sh_timer_read(s->timer[2], offset - 0x20);
268cd1a3f68Sths     }
269cd1a3f68Sths 
270ac3c9e74SBALATON Zoltan     if (offset >= 0x14) {
271cd1a3f68Sths         return sh_timer_read(s->timer[1], offset - 0x14);
272ac3c9e74SBALATON Zoltan     }
273ac3c9e74SBALATON Zoltan     if (offset >= 0x08) {
274cd1a3f68Sths         return sh_timer_read(s->timer[0], offset - 0x08);
275ac3c9e74SBALATON Zoltan     }
276ac3c9e74SBALATON Zoltan     if (offset == 4) {
277cd1a3f68Sths         return s->tstr;
278ac3c9e74SBALATON Zoltan     }
279ac3c9e74SBALATON Zoltan     if ((s->feat & TMU012_FEAT_TOCR) && offset == 0) {
280cd1a3f68Sths         return s->tocr;
281ac3c9e74SBALATON Zoltan     }
282cd1a3f68Sths 
2832ac71179SPaul Brook     hw_error("tmu012_write: Bad offset %x\n", (int)offset);
284cd1a3f68Sths     return 0;
285cd1a3f68Sths }
286cd1a3f68Sths 
287a8170e5eSAvi Kivity static void tmu012_write(void *opaque, hwaddr offset,
28889e29451SBenoît Canet                         uint64_t value, unsigned size)
289cd1a3f68Sths {
2905d9b737eSBALATON Zoltan     tmu012_state *s = opaque;
291cd1a3f68Sths 
292ad52cfc1SBALATON Zoltan     trace_sh_timer_write(offset, value);
293cd1a3f68Sths     if (offset >= 0x20) {
2942f5af2dcSThomas Huth         if (!(s->feat & TMU012_FEAT_3CHAN)) {
2952ac71179SPaul Brook             hw_error("tmu012_write: Bad channel offset %x\n", (int)offset);
2962f5af2dcSThomas Huth         }
297cd1a3f68Sths         sh_timer_write(s->timer[2], offset - 0x20, value);
298cd1a3f68Sths         return;
299cd1a3f68Sths     }
300cd1a3f68Sths 
301cd1a3f68Sths     if (offset >= 0x14) {
302cd1a3f68Sths         sh_timer_write(s->timer[1], offset - 0x14, value);
303cd1a3f68Sths         return;
304cd1a3f68Sths     }
305cd1a3f68Sths 
306cd1a3f68Sths     if (offset >= 0x08) {
307cd1a3f68Sths         sh_timer_write(s->timer[0], offset - 0x08, value);
308cd1a3f68Sths         return;
309cd1a3f68Sths     }
310cd1a3f68Sths 
311cd1a3f68Sths     if (offset == 4) {
312cd1a3f68Sths         sh_timer_start_stop(s->timer[0], value & (1 << 0));
313cd1a3f68Sths         sh_timer_start_stop(s->timer[1], value & (1 << 1));
3142f5af2dcSThomas Huth         if (s->feat & TMU012_FEAT_3CHAN) {
315cd1a3f68Sths             sh_timer_start_stop(s->timer[2], value & (1 << 2));
3162f5af2dcSThomas Huth         } else {
3172f5af2dcSThomas Huth             if (value & (1 << 2)) {
3182ac71179SPaul Brook                 hw_error("tmu012_write: Bad channel\n");
3192f5af2dcSThomas Huth             }
3202f5af2dcSThomas Huth         }
321cd1a3f68Sths 
322cd1a3f68Sths         s->tstr = value;
323cd1a3f68Sths         return;
324cd1a3f68Sths     }
325cd1a3f68Sths 
326cd1a3f68Sths     if ((s->feat & TMU012_FEAT_TOCR) && offset == 0) {
327cd1a3f68Sths         s->tocr = value & (1 << 0);
328cd1a3f68Sths     }
329cd1a3f68Sths }
330cd1a3f68Sths 
33189e29451SBenoît Canet static const MemoryRegionOps tmu012_ops = {
33289e29451SBenoît Canet     .read = tmu012_read,
33389e29451SBenoît Canet     .write = tmu012_write,
33489e29451SBenoît Canet     .endianness = DEVICE_NATIVE_ENDIAN,
335cd1a3f68Sths };
336cd1a3f68Sths 
337f64ccec4SBALATON Zoltan void tmu012_init(MemoryRegion *sysmem, hwaddr base, int feat, uint32_t freq,
33896e2fc41Saurel32                  qemu_irq ch0_irq, qemu_irq ch1_irq,
33996e2fc41Saurel32                  qemu_irq ch2_irq0, qemu_irq ch2_irq1)
340cd1a3f68Sths {
341cd1a3f68Sths     tmu012_state *s;
342cd1a3f68Sths     int timer_feat = (feat & TMU012_FEAT_EXTCLK) ? TIMER_FEAT_EXTCLK : 0;
343cd1a3f68Sths 
344373b96b9SBALATON Zoltan     s = g_malloc0(sizeof(*s));
345cd1a3f68Sths     s->feat = feat;
346703243a0Sbalrog     s->timer[0] = sh_timer_init(freq, timer_feat, ch0_irq);
347703243a0Sbalrog     s->timer[1] = sh_timer_init(freq, timer_feat, ch1_irq);
3482f5af2dcSThomas Huth     if (feat & TMU012_FEAT_3CHAN) {
349703243a0Sbalrog         s->timer[2] = sh_timer_init(freq, timer_feat | TIMER_FEAT_CAPT,
350703243a0Sbalrog                                     ch2_irq0); /* ch2_irq1 not supported */
3512f5af2dcSThomas Huth     }
35289e29451SBenoît Canet 
353*65307c77SBALATON Zoltan     memory_region_init_io(&s->iomem, NULL, &tmu012_ops, s, "timer", 0x30);
35489e29451SBenoît Canet 
3552c9b15caSPaolo Bonzini     memory_region_init_alias(&s->iomem_p4, NULL, "timer-p4",
356*65307c77SBALATON Zoltan                              &s->iomem, 0, memory_region_size(&s->iomem));
35789e29451SBenoît Canet     memory_region_add_subregion(sysmem, P4ADDR(base), &s->iomem_p4);
35889e29451SBenoît Canet 
3592c9b15caSPaolo Bonzini     memory_region_init_alias(&s->iomem_a7, NULL, "timer-a7",
360*65307c77SBALATON Zoltan                              &s->iomem, 0, memory_region_size(&s->iomem));
36189e29451SBenoît Canet     memory_region_add_subregion(sysmem, A7ADDR(base), &s->iomem_a7);
362cd1a3f68Sths     /* ??? Save/restore.  */
363cd1a3f68Sths }
364