xref: /qemu/hw/timer/sh_timer.c (revision 28015830d944169edd2db0cfd64c84e937ec4f25)
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"
1283c9f4caSPaolo Bonzini #include "hw/hw.h"
1364552b6bSMarkus Armbruster #include "hw/irq.h"
140d09e41aSPaolo Bonzini #include "hw/sh4/sh.h"
151de7afc9SPaolo Bonzini #include "qemu/timer.h"
1683c9f4caSPaolo Bonzini #include "hw/ptimer.h"
17cd1a3f68Sths 
18cd1a3f68Sths //#define DEBUG_TIMER
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;
48cd1a3f68Sths } sh_timer_state;
49cd1a3f68Sths 
50cd1a3f68Sths /* Check all active timers, and schedule the next timer interrupt. */
51cd1a3f68Sths 
52cd1a3f68Sths static void sh_timer_update(sh_timer_state *s)
53cd1a3f68Sths {
54703243a0Sbalrog     int new_level = s->int_level && (s->tcr & TIMER_TCR_UNIE);
55703243a0Sbalrog 
56703243a0Sbalrog     if (new_level != s->old_level)
5796e2fc41Saurel32       qemu_set_irq (s->irq, new_level);
58703243a0Sbalrog 
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 {
65cd1a3f68Sths     sh_timer_state *s = (sh_timer_state *)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:
75cd1a3f68Sths         if (s->feat & TIMER_FEAT_CAPT)
76cd1a3f68Sths             return s->tcpr;
77edd7541bSPaolo Bonzini         /* fall through */
78cd1a3f68Sths     default:
792ac71179SPaul Brook         hw_error("sh_timer_read: Bad offset %x\n", (int)offset);
80cd1a3f68Sths         return 0;
81cd1a3f68Sths     }
82cd1a3f68Sths }
83cd1a3f68Sths 
84a8170e5eSAvi Kivity static void sh_timer_write(void *opaque, hwaddr offset,
85cd1a3f68Sths                             uint32_t value)
86cd1a3f68Sths {
87cd1a3f68Sths     sh_timer_state *s = (sh_timer_state *)opaque;
88cd1a3f68Sths     int freq;
89cd1a3f68Sths 
90cd1a3f68Sths     switch (offset >> 2) {
91e7786f27Saurel32     case OFFSET_TCOR:
92cd1a3f68Sths         s->tcor = value;
93*28015830SPeter Maydell         ptimer_transaction_begin(s->timer);
94cd1a3f68Sths         ptimer_set_limit(s->timer, s->tcor, 0);
95*28015830SPeter Maydell         ptimer_transaction_commit(s->timer);
96cd1a3f68Sths         break;
97e7786f27Saurel32     case OFFSET_TCNT:
98cd1a3f68Sths         s->tcnt = value;
99*28015830SPeter Maydell         ptimer_transaction_begin(s->timer);
100cd1a3f68Sths         ptimer_set_count(s->timer, s->tcnt);
101*28015830SPeter Maydell         ptimer_transaction_commit(s->timer);
102cd1a3f68Sths         break;
103e7786f27Saurel32     case OFFSET_TCR:
104*28015830SPeter Maydell         ptimer_transaction_begin(s->timer);
105cd1a3f68Sths         if (s->enabled) {
106cd1a3f68Sths             /* Pause the timer if it is running.  This may cause some
107cd1a3f68Sths                inaccuracy dure to rounding, but avoids a whole lot of other
108cd1a3f68Sths                messyness.  */
109cd1a3f68Sths             ptimer_stop(s->timer);
110cd1a3f68Sths         }
111cd1a3f68Sths         freq = s->freq;
112cd1a3f68Sths         /* ??? Need to recalculate expiry time after changing divisor.  */
113cd1a3f68Sths         switch (value & TIMER_TCR_TPSC) {
114cd1a3f68Sths         case 0: freq >>= 2; break;
115cd1a3f68Sths         case 1: freq >>= 4; break;
116cd1a3f68Sths         case 2: freq >>= 6; break;
117cd1a3f68Sths         case 3: freq >>= 8; break;
118cd1a3f68Sths         case 4: freq >>= 10; break;
119cd1a3f68Sths 	case 6:
120cd1a3f68Sths 	case 7: if (s->feat & TIMER_FEAT_EXTCLK) break;
1212ac71179SPaul Brook 	default: hw_error("sh_timer_write: Reserved TPSC value\n"); break;
122cd1a3f68Sths         }
123cd1a3f68Sths         switch ((value & TIMER_TCR_CKEG) >> 3) {
124cd1a3f68Sths 	case 0: break;
125cd1a3f68Sths         case 1:
126cd1a3f68Sths         case 2:
127cd1a3f68Sths         case 3: if (s->feat & TIMER_FEAT_EXTCLK) break;
1282ac71179SPaul Brook 	default: hw_error("sh_timer_write: Reserved CKEG value\n"); break;
129cd1a3f68Sths         }
130cd1a3f68Sths         switch ((value & TIMER_TCR_ICPE) >> 6) {
131cd1a3f68Sths 	case 0: break;
132cd1a3f68Sths         case 2:
133cd1a3f68Sths         case 3: if (s->feat & TIMER_FEAT_CAPT) break;
1342ac71179SPaul Brook 	default: hw_error("sh_timer_write: Reserved ICPE value\n"); break;
135cd1a3f68Sths         }
136cd1a3f68Sths 	if ((value & TIMER_TCR_UNF) == 0)
137cd1a3f68Sths             s->int_level = 0;
138cd1a3f68Sths 
139cd1a3f68Sths 	value &= ~TIMER_TCR_UNF;
140cd1a3f68Sths 
141cd1a3f68Sths 	if ((value & TIMER_TCR_ICPF) && (!(s->feat & TIMER_FEAT_CAPT)))
1422ac71179SPaul Brook             hw_error("sh_timer_write: Reserved ICPF value\n");
143cd1a3f68Sths 
144cd1a3f68Sths 	value &= ~TIMER_TCR_ICPF; /* capture not supported */
145cd1a3f68Sths 
146cd1a3f68Sths 	if (value & TIMER_TCR_RESERVED)
1472ac71179SPaul Brook             hw_error("sh_timer_write: Reserved TCR bits set\n");
148cd1a3f68Sths         s->tcr = value;
149cd1a3f68Sths         ptimer_set_limit(s->timer, s->tcor, 0);
150cd1a3f68Sths         ptimer_set_freq(s->timer, freq);
151cd1a3f68Sths         if (s->enabled) {
152cd1a3f68Sths             /* Restart the timer if still enabled.  */
153cd1a3f68Sths             ptimer_run(s->timer, 0);
154cd1a3f68Sths         }
155*28015830SPeter Maydell         ptimer_transaction_commit(s->timer);
156cd1a3f68Sths         break;
157e7786f27Saurel32     case OFFSET_TCPR:
158cd1a3f68Sths         if (s->feat & TIMER_FEAT_CAPT) {
159cd1a3f68Sths             s->tcpr = value;
160cd1a3f68Sths 	    break;
161cd1a3f68Sths 	}
162cd1a3f68Sths     default:
1632ac71179SPaul Brook         hw_error("sh_timer_write: Bad offset %x\n", (int)offset);
164cd1a3f68Sths     }
165cd1a3f68Sths     sh_timer_update(s);
166cd1a3f68Sths }
167cd1a3f68Sths 
168cd1a3f68Sths static void sh_timer_start_stop(void *opaque, int enable)
169cd1a3f68Sths {
170cd1a3f68Sths     sh_timer_state *s = (sh_timer_state *)opaque;
171cd1a3f68Sths 
172cd1a3f68Sths #ifdef DEBUG_TIMER
173cd1a3f68Sths     printf("sh_timer_start_stop %d (%d)\n", enable, s->enabled);
174cd1a3f68Sths #endif
175cd1a3f68Sths 
176*28015830SPeter Maydell     ptimer_transaction_begin(s->timer);
177cd1a3f68Sths     if (s->enabled && !enable) {
178cd1a3f68Sths         ptimer_stop(s->timer);
179cd1a3f68Sths     }
180cd1a3f68Sths     if (!s->enabled && enable) {
181cd1a3f68Sths         ptimer_run(s->timer, 0);
182cd1a3f68Sths     }
183*28015830SPeter Maydell     ptimer_transaction_commit(s->timer);
184cd1a3f68Sths     s->enabled = !!enable;
185cd1a3f68Sths 
186cd1a3f68Sths #ifdef DEBUG_TIMER
187cd1a3f68Sths     printf("sh_timer_start_stop done %d\n", s->enabled);
188cd1a3f68Sths #endif
189cd1a3f68Sths }
190cd1a3f68Sths 
191cd1a3f68Sths static void sh_timer_tick(void *opaque)
192cd1a3f68Sths {
193cd1a3f68Sths     sh_timer_state *s = (sh_timer_state *)opaque;
194cd1a3f68Sths     s->int_level = s->enabled;
195cd1a3f68Sths     sh_timer_update(s);
196cd1a3f68Sths }
197cd1a3f68Sths 
19896e2fc41Saurel32 static void *sh_timer_init(uint32_t freq, int feat, qemu_irq irq)
199cd1a3f68Sths {
200cd1a3f68Sths     sh_timer_state *s;
201cd1a3f68Sths 
2027267c094SAnthony Liguori     s = (sh_timer_state *)g_malloc0(sizeof(sh_timer_state));
203cd1a3f68Sths     s->freq = freq;
204cd1a3f68Sths     s->feat = feat;
205cd1a3f68Sths     s->tcor = 0xffffffff;
206cd1a3f68Sths     s->tcnt = 0xffffffff;
207cd1a3f68Sths     s->tcpr = 0xdeadbeef;
208e7786f27Saurel32     s->tcr = 0;
209cd1a3f68Sths     s->enabled = 0;
210703243a0Sbalrog     s->irq = irq;
211cd1a3f68Sths 
212*28015830SPeter Maydell     s->timer = ptimer_init(sh_timer_tick, s, PTIMER_POLICY_DEFAULT);
213e7786f27Saurel32 
214e7786f27Saurel32     sh_timer_write(s, OFFSET_TCOR >> 2, s->tcor);
215e7786f27Saurel32     sh_timer_write(s, OFFSET_TCNT >> 2, s->tcnt);
216e7786f27Saurel32     sh_timer_write(s, OFFSET_TCPR >> 2, s->tcpr);
217e7786f27Saurel32     sh_timer_write(s, OFFSET_TCR  >> 2, s->tcpr);
218cd1a3f68Sths     /* ??? Save/restore.  */
219cd1a3f68Sths     return s;
220cd1a3f68Sths }
221cd1a3f68Sths 
222cd1a3f68Sths typedef struct {
22389e29451SBenoît Canet     MemoryRegion iomem;
22489e29451SBenoît Canet     MemoryRegion iomem_p4;
22589e29451SBenoît Canet     MemoryRegion iomem_a7;
226cd1a3f68Sths     void *timer[3];
227cd1a3f68Sths     int level[3];
228cd1a3f68Sths     uint32_t tocr;
229cd1a3f68Sths     uint32_t tstr;
230cd1a3f68Sths     int feat;
231cd1a3f68Sths } tmu012_state;
232cd1a3f68Sths 
233a8170e5eSAvi Kivity static uint64_t tmu012_read(void *opaque, hwaddr offset,
23489e29451SBenoît Canet                             unsigned size)
235cd1a3f68Sths {
236cd1a3f68Sths     tmu012_state *s = (tmu012_state *)opaque;
237cd1a3f68Sths 
238cd1a3f68Sths #ifdef DEBUG_TIMER
239cd1a3f68Sths     printf("tmu012_read 0x%lx\n", (unsigned long) offset);
240cd1a3f68Sths #endif
241cd1a3f68Sths 
242cd1a3f68Sths     if (offset >= 0x20) {
243cd1a3f68Sths         if (!(s->feat & TMU012_FEAT_3CHAN))
2442ac71179SPaul Brook 	    hw_error("tmu012_write: Bad channel offset %x\n", (int)offset);
245cd1a3f68Sths         return sh_timer_read(s->timer[2], offset - 0x20);
246cd1a3f68Sths     }
247cd1a3f68Sths 
248cd1a3f68Sths     if (offset >= 0x14)
249cd1a3f68Sths         return sh_timer_read(s->timer[1], offset - 0x14);
250cd1a3f68Sths 
251cd1a3f68Sths     if (offset >= 0x08)
252cd1a3f68Sths         return sh_timer_read(s->timer[0], offset - 0x08);
253cd1a3f68Sths 
254cd1a3f68Sths     if (offset == 4)
255cd1a3f68Sths         return s->tstr;
256cd1a3f68Sths 
257cd1a3f68Sths     if ((s->feat & TMU012_FEAT_TOCR) && offset == 0)
258cd1a3f68Sths         return s->tocr;
259cd1a3f68Sths 
2602ac71179SPaul Brook     hw_error("tmu012_write: Bad offset %x\n", (int)offset);
261cd1a3f68Sths     return 0;
262cd1a3f68Sths }
263cd1a3f68Sths 
264a8170e5eSAvi Kivity static void tmu012_write(void *opaque, hwaddr offset,
26589e29451SBenoît Canet                         uint64_t value, unsigned size)
266cd1a3f68Sths {
267cd1a3f68Sths     tmu012_state *s = (tmu012_state *)opaque;
268cd1a3f68Sths 
269cd1a3f68Sths #ifdef DEBUG_TIMER
270cd1a3f68Sths     printf("tmu012_write 0x%lx 0x%08x\n", (unsigned long) offset, value);
271cd1a3f68Sths #endif
272cd1a3f68Sths 
273cd1a3f68Sths     if (offset >= 0x20) {
274cd1a3f68Sths         if (!(s->feat & TMU012_FEAT_3CHAN))
2752ac71179SPaul Brook 	    hw_error("tmu012_write: Bad channel offset %x\n", (int)offset);
276cd1a3f68Sths         sh_timer_write(s->timer[2], offset - 0x20, value);
277cd1a3f68Sths 	return;
278cd1a3f68Sths     }
279cd1a3f68Sths 
280cd1a3f68Sths     if (offset >= 0x14) {
281cd1a3f68Sths         sh_timer_write(s->timer[1], offset - 0x14, value);
282cd1a3f68Sths 	return;
283cd1a3f68Sths     }
284cd1a3f68Sths 
285cd1a3f68Sths     if (offset >= 0x08) {
286cd1a3f68Sths         sh_timer_write(s->timer[0], offset - 0x08, value);
287cd1a3f68Sths 	return;
288cd1a3f68Sths     }
289cd1a3f68Sths 
290cd1a3f68Sths     if (offset == 4) {
291cd1a3f68Sths         sh_timer_start_stop(s->timer[0], value & (1 << 0));
292cd1a3f68Sths         sh_timer_start_stop(s->timer[1], value & (1 << 1));
293cd1a3f68Sths         if (s->feat & TMU012_FEAT_3CHAN)
294cd1a3f68Sths             sh_timer_start_stop(s->timer[2], value & (1 << 2));
295cd1a3f68Sths 	else
296cd1a3f68Sths             if (value & (1 << 2))
2972ac71179SPaul Brook                 hw_error("tmu012_write: Bad channel\n");
298cd1a3f68Sths 
299cd1a3f68Sths 	s->tstr = value;
300cd1a3f68Sths 	return;
301cd1a3f68Sths     }
302cd1a3f68Sths 
303cd1a3f68Sths     if ((s->feat & TMU012_FEAT_TOCR) && offset == 0) {
304cd1a3f68Sths         s->tocr = value & (1 << 0);
305cd1a3f68Sths     }
306cd1a3f68Sths }
307cd1a3f68Sths 
30889e29451SBenoît Canet static const MemoryRegionOps tmu012_ops = {
30989e29451SBenoît Canet     .read = tmu012_read,
31089e29451SBenoît Canet     .write = tmu012_write,
31189e29451SBenoît Canet     .endianness = DEVICE_NATIVE_ENDIAN,
312cd1a3f68Sths };
313cd1a3f68Sths 
314a8170e5eSAvi Kivity void tmu012_init(MemoryRegion *sysmem, hwaddr base,
31589e29451SBenoît Canet                  int feat, uint32_t freq,
31696e2fc41Saurel32 		 qemu_irq ch0_irq, qemu_irq ch1_irq,
31796e2fc41Saurel32 		 qemu_irq ch2_irq0, qemu_irq ch2_irq1)
318cd1a3f68Sths {
319cd1a3f68Sths     tmu012_state *s;
320cd1a3f68Sths     int timer_feat = (feat & TMU012_FEAT_EXTCLK) ? TIMER_FEAT_EXTCLK : 0;
321cd1a3f68Sths 
3227267c094SAnthony Liguori     s = (tmu012_state *)g_malloc0(sizeof(tmu012_state));
323cd1a3f68Sths     s->feat = feat;
324703243a0Sbalrog     s->timer[0] = sh_timer_init(freq, timer_feat, ch0_irq);
325703243a0Sbalrog     s->timer[1] = sh_timer_init(freq, timer_feat, ch1_irq);
326cd1a3f68Sths     if (feat & TMU012_FEAT_3CHAN)
327703243a0Sbalrog         s->timer[2] = sh_timer_init(freq, timer_feat | TIMER_FEAT_CAPT,
328703243a0Sbalrog 				    ch2_irq0); /* ch2_irq1 not supported */
32989e29451SBenoît Canet 
3302c9b15caSPaolo Bonzini     memory_region_init_io(&s->iomem, NULL, &tmu012_ops, s,
33189e29451SBenoît Canet                           "timer", 0x100000000ULL);
33289e29451SBenoît Canet 
3332c9b15caSPaolo Bonzini     memory_region_init_alias(&s->iomem_p4, NULL, "timer-p4",
33489e29451SBenoît Canet                              &s->iomem, 0, 0x1000);
33589e29451SBenoît Canet     memory_region_add_subregion(sysmem, P4ADDR(base), &s->iomem_p4);
33689e29451SBenoît Canet 
3372c9b15caSPaolo Bonzini     memory_region_init_alias(&s->iomem_a7, NULL, "timer-a7",
33889e29451SBenoît Canet                              &s->iomem, 0, 0x1000);
33989e29451SBenoît Canet     memory_region_add_subregion(sysmem, A7ADDR(base), &s->iomem_a7);
340cd1a3f68Sths     /* ??? Save/restore.  */
341cd1a3f68Sths }
342