blob: 572804f9cc645f9e87c2153f2a277705d1c17a1d [file] [log] [blame]
ths5fafdf22007-09-16 21:08:06 +00001/*
pbrookcdbdb642006-04-09 01:32:52 +00002 * ARM PrimeCell Timer modules.
3 *
4 * Copyright (c) 2005-2006 CodeSourcery.
5 * Written by Paul Brook
6 *
7 * This code is licenced under the GPL.
8 */
9
Paul Brook6a824ec2009-05-14 22:35:07 +010010#include "sysbus.h"
pbrook87ecb682007-11-17 17:14:51 +000011#include "qemu-timer.h"
pbrookcdbdb642006-04-09 01:32:52 +000012
13/* Common timer implementation. */
14
15#define TIMER_CTRL_ONESHOT (1 << 0)
16#define TIMER_CTRL_32BIT (1 << 1)
17#define TIMER_CTRL_DIV1 (0 << 2)
18#define TIMER_CTRL_DIV16 (1 << 2)
19#define TIMER_CTRL_DIV256 (2 << 2)
20#define TIMER_CTRL_IE (1 << 5)
21#define TIMER_CTRL_PERIODIC (1 << 6)
22#define TIMER_CTRL_ENABLE (1 << 7)
23
24typedef struct {
pbrook423f0742007-05-23 00:06:54 +000025 ptimer_state *timer;
pbrookcdbdb642006-04-09 01:32:52 +000026 uint32_t control;
pbrookcdbdb642006-04-09 01:32:52 +000027 uint32_t limit;
pbrookcdbdb642006-04-09 01:32:52 +000028 int freq;
29 int int_level;
pbrookd537cf62007-04-07 18:14:41 +000030 qemu_irq irq;
pbrookcdbdb642006-04-09 01:32:52 +000031} arm_timer_state;
32
pbrookcdbdb642006-04-09 01:32:52 +000033/* Check all active timers, and schedule the next timer interrupt. */
34
pbrook423f0742007-05-23 00:06:54 +000035static void arm_timer_update(arm_timer_state *s)
pbrookcdbdb642006-04-09 01:32:52 +000036{
pbrookcdbdb642006-04-09 01:32:52 +000037 /* Update interrupts. */
38 if (s->int_level && (s->control & TIMER_CTRL_IE)) {
pbrookd537cf62007-04-07 18:14:41 +000039 qemu_irq_raise(s->irq);
pbrookcdbdb642006-04-09 01:32:52 +000040 } else {
pbrookd537cf62007-04-07 18:14:41 +000041 qemu_irq_lower(s->irq);
pbrookcdbdb642006-04-09 01:32:52 +000042 }
pbrookcdbdb642006-04-09 01:32:52 +000043}
44
pbrook9596ebb2007-11-18 01:44:38 +000045static uint32_t arm_timer_read(void *opaque, target_phys_addr_t offset)
pbrookcdbdb642006-04-09 01:32:52 +000046{
47 arm_timer_state *s = (arm_timer_state *)opaque;
48
49 switch (offset >> 2) {
50 case 0: /* TimerLoad */
51 case 6: /* TimerBGLoad */
52 return s->limit;
53 case 1: /* TimerValue */
pbrook423f0742007-05-23 00:06:54 +000054 return ptimer_get_count(s->timer);
pbrookcdbdb642006-04-09 01:32:52 +000055 case 2: /* TimerControl */
56 return s->control;
57 case 4: /* TimerRIS */
58 return s->int_level;
59 case 5: /* TimerMIS */
60 if ((s->control & TIMER_CTRL_IE) == 0)
61 return 0;
62 return s->int_level;
63 default:
Paul Brook2ac71172009-05-08 02:35:15 +010064 hw_error("arm_timer_read: Bad offset %x\n", (int)offset);
pbrookcdbdb642006-04-09 01:32:52 +000065 return 0;
66 }
67}
68
pbrook423f0742007-05-23 00:06:54 +000069/* Reset the timer limit after settings have changed. */
70static void arm_timer_recalibrate(arm_timer_state *s, int reload)
71{
72 uint32_t limit;
73
74 if ((s->control & TIMER_CTRL_PERIODIC) == 0) {
75 /* Free running. */
76 if (s->control & TIMER_CTRL_32BIT)
77 limit = 0xffffffff;
78 else
79 limit = 0xffff;
80 } else {
81 /* Periodic. */
82 limit = s->limit;
83 }
84 ptimer_set_limit(s->timer, limit, reload);
85}
86
pbrookcdbdb642006-04-09 01:32:52 +000087static void arm_timer_write(void *opaque, target_phys_addr_t offset,
88 uint32_t value)
89{
90 arm_timer_state *s = (arm_timer_state *)opaque;
pbrook423f0742007-05-23 00:06:54 +000091 int freq;
pbrookcdbdb642006-04-09 01:32:52 +000092
pbrookcdbdb642006-04-09 01:32:52 +000093 switch (offset >> 2) {
94 case 0: /* TimerLoad */
95 s->limit = value;
pbrook423f0742007-05-23 00:06:54 +000096 arm_timer_recalibrate(s, 1);
pbrookcdbdb642006-04-09 01:32:52 +000097 break;
98 case 1: /* TimerValue */
99 /* ??? Linux seems to want to write to this readonly register.
100 Ignore it. */
101 break;
102 case 2: /* TimerControl */
103 if (s->control & TIMER_CTRL_ENABLE) {
104 /* Pause the timer if it is running. This may cause some
105 inaccuracy dure to rounding, but avoids a whole lot of other
106 messyness. */
pbrook423f0742007-05-23 00:06:54 +0000107 ptimer_stop(s->timer);
pbrookcdbdb642006-04-09 01:32:52 +0000108 }
109 s->control = value;
pbrook423f0742007-05-23 00:06:54 +0000110 freq = s->freq;
pbrookcdbdb642006-04-09 01:32:52 +0000111 /* ??? Need to recalculate expiry time after changing divisor. */
112 switch ((value >> 2) & 3) {
pbrook423f0742007-05-23 00:06:54 +0000113 case 1: freq >>= 4; break;
114 case 2: freq >>= 8; break;
pbrookcdbdb642006-04-09 01:32:52 +0000115 }
pbrook423f0742007-05-23 00:06:54 +0000116 arm_timer_recalibrate(s, 0);
117 ptimer_set_freq(s->timer, freq);
pbrookcdbdb642006-04-09 01:32:52 +0000118 if (s->control & TIMER_CTRL_ENABLE) {
119 /* Restart the timer if still enabled. */
pbrook423f0742007-05-23 00:06:54 +0000120 ptimer_run(s->timer, (s->control & TIMER_CTRL_ONESHOT) != 0);
pbrookcdbdb642006-04-09 01:32:52 +0000121 }
122 break;
123 case 3: /* TimerIntClr */
124 s->int_level = 0;
125 break;
126 case 6: /* TimerBGLoad */
127 s->limit = value;
pbrook423f0742007-05-23 00:06:54 +0000128 arm_timer_recalibrate(s, 0);
pbrookcdbdb642006-04-09 01:32:52 +0000129 break;
130 default:
Paul Brook2ac71172009-05-08 02:35:15 +0100131 hw_error("arm_timer_write: Bad offset %x\n", (int)offset);
pbrookcdbdb642006-04-09 01:32:52 +0000132 }
pbrook423f0742007-05-23 00:06:54 +0000133 arm_timer_update(s);
pbrookcdbdb642006-04-09 01:32:52 +0000134}
135
136static void arm_timer_tick(void *opaque)
137{
pbrook423f0742007-05-23 00:06:54 +0000138 arm_timer_state *s = (arm_timer_state *)opaque;
139 s->int_level = 1;
140 arm_timer_update(s);
pbrookcdbdb642006-04-09 01:32:52 +0000141}
142
pbrook23e39292008-07-02 16:48:32 +0000143static void arm_timer_save(QEMUFile *f, void *opaque)
144{
145 arm_timer_state *s = (arm_timer_state *)opaque;
146 qemu_put_be32(f, s->control);
147 qemu_put_be32(f, s->limit);
148 qemu_put_be32(f, s->int_level);
149 qemu_put_ptimer(f, s->timer);
150}
151
152static int arm_timer_load(QEMUFile *f, void *opaque, int version_id)
153{
154 arm_timer_state *s = (arm_timer_state *)opaque;
155
156 if (version_id != 1)
157 return -EINVAL;
158
159 s->control = qemu_get_be32(f);
160 s->limit = qemu_get_be32(f);
161 s->int_level = qemu_get_be32(f);
162 qemu_get_ptimer(f, s->timer);
163 return 0;
164}
165
Paul Brook6a824ec2009-05-14 22:35:07 +0100166static arm_timer_state *arm_timer_init(uint32_t freq)
pbrookcdbdb642006-04-09 01:32:52 +0000167{
168 arm_timer_state *s;
pbrook423f0742007-05-23 00:06:54 +0000169 QEMUBH *bh;
pbrookcdbdb642006-04-09 01:32:52 +0000170
171 s = (arm_timer_state *)qemu_mallocz(sizeof(arm_timer_state));
pbrook423f0742007-05-23 00:06:54 +0000172 s->freq = freq;
pbrookcdbdb642006-04-09 01:32:52 +0000173 s->control = TIMER_CTRL_IE;
pbrookcdbdb642006-04-09 01:32:52 +0000174
pbrook423f0742007-05-23 00:06:54 +0000175 bh = qemu_bh_new(arm_timer_tick, s);
176 s->timer = ptimer_init(bh);
pbrook23e39292008-07-02 16:48:32 +0000177 register_savevm("arm_timer", -1, 1, arm_timer_save, arm_timer_load, s);
pbrookcdbdb642006-04-09 01:32:52 +0000178 return s;
179}
180
181/* ARM PrimeCell SP804 dual timer module.
182 Docs for this device don't seem to be publicly available. This
pbrookd85fb992007-04-06 20:58:25 +0000183 implementation is based on guesswork, the linux kernel sources and the
pbrookcdbdb642006-04-09 01:32:52 +0000184 Integrator/CP timer modules. */
185
186typedef struct {
Paul Brook6a824ec2009-05-14 22:35:07 +0100187 SysBusDevice busdev;
188 arm_timer_state *timer[2];
pbrookcdbdb642006-04-09 01:32:52 +0000189 int level[2];
pbrookd537cf62007-04-07 18:14:41 +0000190 qemu_irq irq;
pbrookcdbdb642006-04-09 01:32:52 +0000191} sp804_state;
192
pbrookd537cf62007-04-07 18:14:41 +0000193/* Merge the IRQs from the two component devices. */
pbrookcdbdb642006-04-09 01:32:52 +0000194static void sp804_set_irq(void *opaque, int irq, int level)
195{
196 sp804_state *s = (sp804_state *)opaque;
197
198 s->level[irq] = level;
pbrookd537cf62007-04-07 18:14:41 +0000199 qemu_set_irq(s->irq, s->level[0] || s->level[1]);
pbrookcdbdb642006-04-09 01:32:52 +0000200}
201
202static uint32_t sp804_read(void *opaque, target_phys_addr_t offset)
203{
204 sp804_state *s = (sp804_state *)opaque;
205
206 /* ??? Don't know the PrimeCell ID for this device. */
pbrookcdbdb642006-04-09 01:32:52 +0000207 if (offset < 0x20) {
208 return arm_timer_read(s->timer[0], offset);
209 } else {
210 return arm_timer_read(s->timer[1], offset - 0x20);
211 }
212}
213
214static void sp804_write(void *opaque, target_phys_addr_t offset,
215 uint32_t value)
216{
217 sp804_state *s = (sp804_state *)opaque;
218
pbrookcdbdb642006-04-09 01:32:52 +0000219 if (offset < 0x20) {
220 arm_timer_write(s->timer[0], offset, value);
221 } else {
222 arm_timer_write(s->timer[1], offset - 0x20, value);
223 }
224}
225
226static CPUReadMemoryFunc *sp804_readfn[] = {
227 sp804_read,
228 sp804_read,
229 sp804_read
230};
231
232static CPUWriteMemoryFunc *sp804_writefn[] = {
233 sp804_write,
234 sp804_write,
235 sp804_write
236};
237
pbrook23e39292008-07-02 16:48:32 +0000238static void sp804_save(QEMUFile *f, void *opaque)
239{
240 sp804_state *s = (sp804_state *)opaque;
241 qemu_put_be32(f, s->level[0]);
242 qemu_put_be32(f, s->level[1]);
243}
244
245static int sp804_load(QEMUFile *f, void *opaque, int version_id)
246{
247 sp804_state *s = (sp804_state *)opaque;
248
249 if (version_id != 1)
250 return -EINVAL;
251
252 s->level[0] = qemu_get_be32(f);
253 s->level[1] = qemu_get_be32(f);
254 return 0;
255}
256
Paul Brook6a824ec2009-05-14 22:35:07 +0100257static void sp804_init(SysBusDevice *dev)
pbrookcdbdb642006-04-09 01:32:52 +0000258{
259 int iomemtype;
Paul Brook6a824ec2009-05-14 22:35:07 +0100260 sp804_state *s = FROM_SYSBUS(sp804_state, dev);
pbrookd537cf62007-04-07 18:14:41 +0000261 qemu_irq *qi;
pbrookcdbdb642006-04-09 01:32:52 +0000262
pbrookd537cf62007-04-07 18:14:41 +0000263 qi = qemu_allocate_irqs(sp804_set_irq, s, 2);
Paul Brook6a824ec2009-05-14 22:35:07 +0100264 sysbus_init_irq(dev, &s->irq);
pbrookcdbdb642006-04-09 01:32:52 +0000265 /* ??? The timers are actually configurable between 32kHz and 1MHz, but
266 we don't implement that. */
Paul Brook6a824ec2009-05-14 22:35:07 +0100267 s->timer[0] = arm_timer_init(1000000);
268 s->timer[1] = arm_timer_init(1000000);
269 s->timer[0]->irq = qi[0];
270 s->timer[1]->irq = qi[1];
Avi Kivity1eed09c2009-06-14 11:38:51 +0300271 iomemtype = cpu_register_io_memory(sp804_readfn,
pbrookcdbdb642006-04-09 01:32:52 +0000272 sp804_writefn, s);
Paul Brook6a824ec2009-05-14 22:35:07 +0100273 sysbus_init_mmio(dev, 0x1000, iomemtype);
pbrook23e39292008-07-02 16:48:32 +0000274 register_savevm("sp804", -1, 1, sp804_save, sp804_load, s);
pbrookcdbdb642006-04-09 01:32:52 +0000275}
276
277
278/* Integrator/CP timer module. */
279
280typedef struct {
Paul Brook6a824ec2009-05-14 22:35:07 +0100281 SysBusDevice busdev;
282 arm_timer_state *timer[3];
pbrookcdbdb642006-04-09 01:32:52 +0000283} icp_pit_state;
284
285static uint32_t icp_pit_read(void *opaque, target_phys_addr_t offset)
286{
287 icp_pit_state *s = (icp_pit_state *)opaque;
288 int n;
289
290 /* ??? Don't know the PrimeCell ID for this device. */
pbrookcdbdb642006-04-09 01:32:52 +0000291 n = offset >> 8;
Paul Brook2ac71172009-05-08 02:35:15 +0100292 if (n > 3) {
293 hw_error("sp804_read: Bad timer %d\n", n);
294 }
pbrookcdbdb642006-04-09 01:32:52 +0000295
296 return arm_timer_read(s->timer[n], offset & 0xff);
297}
298
299static void icp_pit_write(void *opaque, target_phys_addr_t offset,
300 uint32_t value)
301{
302 icp_pit_state *s = (icp_pit_state *)opaque;
303 int n;
304
pbrookcdbdb642006-04-09 01:32:52 +0000305 n = offset >> 8;
Paul Brook2ac71172009-05-08 02:35:15 +0100306 if (n > 3) {
307 hw_error("sp804_write: Bad timer %d\n", n);
308 }
pbrookcdbdb642006-04-09 01:32:52 +0000309
310 arm_timer_write(s->timer[n], offset & 0xff, value);
311}
312
313
314static CPUReadMemoryFunc *icp_pit_readfn[] = {
315 icp_pit_read,
316 icp_pit_read,
317 icp_pit_read
318};
319
320static CPUWriteMemoryFunc *icp_pit_writefn[] = {
321 icp_pit_write,
322 icp_pit_write,
323 icp_pit_write
324};
325
Paul Brook6a824ec2009-05-14 22:35:07 +0100326static void icp_pit_init(SysBusDevice *dev)
pbrookcdbdb642006-04-09 01:32:52 +0000327{
328 int iomemtype;
Paul Brook6a824ec2009-05-14 22:35:07 +0100329 icp_pit_state *s = FROM_SYSBUS(icp_pit_state, dev);
pbrookcdbdb642006-04-09 01:32:52 +0000330
pbrookcdbdb642006-04-09 01:32:52 +0000331 /* Timer 0 runs at the system clock speed (40MHz). */
Paul Brook6a824ec2009-05-14 22:35:07 +0100332 s->timer[0] = arm_timer_init(40000000);
pbrookcdbdb642006-04-09 01:32:52 +0000333 /* The other two timers run at 1MHz. */
Paul Brook6a824ec2009-05-14 22:35:07 +0100334 s->timer[1] = arm_timer_init(1000000);
335 s->timer[2] = arm_timer_init(1000000);
336
337 sysbus_init_irq(dev, &s->timer[0]->irq);
338 sysbus_init_irq(dev, &s->timer[1]->irq);
339 sysbus_init_irq(dev, &s->timer[2]->irq);
pbrookcdbdb642006-04-09 01:32:52 +0000340
Avi Kivity1eed09c2009-06-14 11:38:51 +0300341 iomemtype = cpu_register_io_memory(icp_pit_readfn,
pbrookcdbdb642006-04-09 01:32:52 +0000342 icp_pit_writefn, s);
Paul Brook6a824ec2009-05-14 22:35:07 +0100343 sysbus_init_mmio(dev, 0x1000, iomemtype);
pbrook23e39292008-07-02 16:48:32 +0000344 /* This device has no state to save/restore. The component timers will
345 save themselves. */
pbrookcdbdb642006-04-09 01:32:52 +0000346}
Paul Brook6a824ec2009-05-14 22:35:07 +0100347
348static void arm_timer_register_devices(void)
349{
350 sysbus_register_dev("integrator_pit", sizeof(icp_pit_state), icp_pit_init);
351 sysbus_register_dev("sp804", sizeof(sp804_state), sp804_init);
352}
353
354device_init(arm_timer_register_devices)