blob: 82f05dec84fa523d39f1bb13be77878b5c6c3150 [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
Anthony Liguoric227f092009-10-01 16:12:16 -050045static 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
Rabin Vincenta9cf98d2010-05-02 15:20:52 +053074 if ((s->control & (TIMER_CTRL_PERIODIC | TIMER_CTRL_ONESHOT)) == 0) {
pbrook423f0742007-05-23 00:06:54 +000075 /* 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
Anthony Liguoric227f092009-10-01 16:12:16 -050087static void arm_timer_write(void *opaque, target_phys_addr_t offset,
pbrookcdbdb642006-04-09 01:32:52 +000088 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 }
Rabin Vincentd6759902010-05-02 15:20:51 +0530116 arm_timer_recalibrate(s, s->control & TIMER_CTRL_ENABLE);
pbrook423f0742007-05-23 00:06:54 +0000117 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);
Alex Williamson0be71e32010-06-25 11:09:07 -0600177 register_savevm(NULL, "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
Anthony Liguoric227f092009-10-01 16:12:16 -0500202static uint32_t sp804_read(void *opaque, target_phys_addr_t offset)
pbrookcdbdb642006-04-09 01:32:52 +0000203{
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
Anthony Liguoric227f092009-10-01 16:12:16 -0500214static void sp804_write(void *opaque, target_phys_addr_t offset,
pbrookcdbdb642006-04-09 01:32:52 +0000215 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
Blue Swirld60efc62009-08-25 18:29:31 +0000226static CPUReadMemoryFunc * const sp804_readfn[] = {
pbrookcdbdb642006-04-09 01:32:52 +0000227 sp804_read,
228 sp804_read,
229 sp804_read
230};
231
Blue Swirld60efc62009-08-25 18:29:31 +0000232static CPUWriteMemoryFunc * const sp804_writefn[] = {
pbrookcdbdb642006-04-09 01:32:52 +0000233 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
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200257static int 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,
Alexander Graf2507c122010-12-08 12:05:37 +0100272 sp804_writefn, s, DEVICE_NATIVE_ENDIAN);
Paul Brook6a824ec2009-05-14 22:35:07 +0100273 sysbus_init_mmio(dev, 0x1000, iomemtype);
Alex Williamson0be71e32010-06-25 11:09:07 -0600274 register_savevm(&dev->qdev, "sp804", -1, 1, sp804_save, sp804_load, s);
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200275 return 0;
pbrookcdbdb642006-04-09 01:32:52 +0000276}
277
278
279/* Integrator/CP timer module. */
280
281typedef struct {
Paul Brook6a824ec2009-05-14 22:35:07 +0100282 SysBusDevice busdev;
283 arm_timer_state *timer[3];
pbrookcdbdb642006-04-09 01:32:52 +0000284} icp_pit_state;
285
Anthony Liguoric227f092009-10-01 16:12:16 -0500286static uint32_t icp_pit_read(void *opaque, target_phys_addr_t offset)
pbrookcdbdb642006-04-09 01:32:52 +0000287{
288 icp_pit_state *s = (icp_pit_state *)opaque;
289 int n;
290
291 /* ??? Don't know the PrimeCell ID for this device. */
pbrookcdbdb642006-04-09 01:32:52 +0000292 n = offset >> 8;
Paul Brook2ac71172009-05-08 02:35:15 +0100293 if (n > 3) {
294 hw_error("sp804_read: Bad timer %d\n", n);
295 }
pbrookcdbdb642006-04-09 01:32:52 +0000296
297 return arm_timer_read(s->timer[n], offset & 0xff);
298}
299
Anthony Liguoric227f092009-10-01 16:12:16 -0500300static void icp_pit_write(void *opaque, target_phys_addr_t offset,
pbrookcdbdb642006-04-09 01:32:52 +0000301 uint32_t value)
302{
303 icp_pit_state *s = (icp_pit_state *)opaque;
304 int n;
305
pbrookcdbdb642006-04-09 01:32:52 +0000306 n = offset >> 8;
Paul Brook2ac71172009-05-08 02:35:15 +0100307 if (n > 3) {
308 hw_error("sp804_write: Bad timer %d\n", n);
309 }
pbrookcdbdb642006-04-09 01:32:52 +0000310
311 arm_timer_write(s->timer[n], offset & 0xff, value);
312}
313
314
Blue Swirld60efc62009-08-25 18:29:31 +0000315static CPUReadMemoryFunc * const icp_pit_readfn[] = {
pbrookcdbdb642006-04-09 01:32:52 +0000316 icp_pit_read,
317 icp_pit_read,
318 icp_pit_read
319};
320
Blue Swirld60efc62009-08-25 18:29:31 +0000321static CPUWriteMemoryFunc * const icp_pit_writefn[] = {
pbrookcdbdb642006-04-09 01:32:52 +0000322 icp_pit_write,
323 icp_pit_write,
324 icp_pit_write
325};
326
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200327static int icp_pit_init(SysBusDevice *dev)
pbrookcdbdb642006-04-09 01:32:52 +0000328{
329 int iomemtype;
Paul Brook6a824ec2009-05-14 22:35:07 +0100330 icp_pit_state *s = FROM_SYSBUS(icp_pit_state, dev);
pbrookcdbdb642006-04-09 01:32:52 +0000331
pbrookcdbdb642006-04-09 01:32:52 +0000332 /* Timer 0 runs at the system clock speed (40MHz). */
Paul Brook6a824ec2009-05-14 22:35:07 +0100333 s->timer[0] = arm_timer_init(40000000);
pbrookcdbdb642006-04-09 01:32:52 +0000334 /* The other two timers run at 1MHz. */
Paul Brook6a824ec2009-05-14 22:35:07 +0100335 s->timer[1] = arm_timer_init(1000000);
336 s->timer[2] = arm_timer_init(1000000);
337
338 sysbus_init_irq(dev, &s->timer[0]->irq);
339 sysbus_init_irq(dev, &s->timer[1]->irq);
340 sysbus_init_irq(dev, &s->timer[2]->irq);
pbrookcdbdb642006-04-09 01:32:52 +0000341
Avi Kivity1eed09c2009-06-14 11:38:51 +0300342 iomemtype = cpu_register_io_memory(icp_pit_readfn,
Alexander Graf2507c122010-12-08 12:05:37 +0100343 icp_pit_writefn, s,
344 DEVICE_NATIVE_ENDIAN);
Paul Brook6a824ec2009-05-14 22:35:07 +0100345 sysbus_init_mmio(dev, 0x1000, iomemtype);
pbrook23e39292008-07-02 16:48:32 +0000346 /* This device has no state to save/restore. The component timers will
347 save themselves. */
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200348 return 0;
pbrookcdbdb642006-04-09 01:32:52 +0000349}
Paul Brook6a824ec2009-05-14 22:35:07 +0100350
351static void arm_timer_register_devices(void)
352{
353 sysbus_register_dev("integrator_pit", sizeof(icp_pit_state), icp_pit_init);
354 sysbus_register_dev("sp804", sizeof(sp804_state), sp804_init);
355}
356
357device_init(arm_timer_register_devices)