blob: 1902f1a7b9a916c46a1682a3be18d9c1e73329ff [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 *
Matthew Fernandez8e31bf32011-06-26 12:21:35 +10007 * This code is licensed under the GPL.
pbrookcdbdb642006-04-09 01:32:52 +00008 */
9
Paul Brook6a824ec2009-05-14 22:35:07 +010010#include "sysbus.h"
pbrook87ecb682007-11-17 17:14:51 +000011#include "qemu-timer.h"
Mark Langsdorf104a26a2011-12-29 06:19:51 +000012#include "qemu-common.h"
13#include "qdev.h"
Paolo Bonzini49d4d9b62012-01-13 17:07:19 +010014#include "ptimer.h"
pbrookcdbdb642006-04-09 01:32:52 +000015
16/* Common timer implementation. */
17
18#define TIMER_CTRL_ONESHOT (1 << 0)
19#define TIMER_CTRL_32BIT (1 << 1)
20#define TIMER_CTRL_DIV1 (0 << 2)
21#define TIMER_CTRL_DIV16 (1 << 2)
22#define TIMER_CTRL_DIV256 (2 << 2)
23#define TIMER_CTRL_IE (1 << 5)
24#define TIMER_CTRL_PERIODIC (1 << 6)
25#define TIMER_CTRL_ENABLE (1 << 7)
26
27typedef struct {
pbrook423f0742007-05-23 00:06:54 +000028 ptimer_state *timer;
pbrookcdbdb642006-04-09 01:32:52 +000029 uint32_t control;
pbrookcdbdb642006-04-09 01:32:52 +000030 uint32_t limit;
pbrookcdbdb642006-04-09 01:32:52 +000031 int freq;
32 int int_level;
pbrookd537cf62007-04-07 18:14:41 +000033 qemu_irq irq;
pbrookcdbdb642006-04-09 01:32:52 +000034} arm_timer_state;
35
pbrookcdbdb642006-04-09 01:32:52 +000036/* Check all active timers, and schedule the next timer interrupt. */
37
pbrook423f0742007-05-23 00:06:54 +000038static void arm_timer_update(arm_timer_state *s)
pbrookcdbdb642006-04-09 01:32:52 +000039{
pbrookcdbdb642006-04-09 01:32:52 +000040 /* Update interrupts. */
41 if (s->int_level && (s->control & TIMER_CTRL_IE)) {
pbrookd537cf62007-04-07 18:14:41 +000042 qemu_irq_raise(s->irq);
pbrookcdbdb642006-04-09 01:32:52 +000043 } else {
pbrookd537cf62007-04-07 18:14:41 +000044 qemu_irq_lower(s->irq);
pbrookcdbdb642006-04-09 01:32:52 +000045 }
pbrookcdbdb642006-04-09 01:32:52 +000046}
47
Anthony Liguoric227f092009-10-01 16:12:16 -050048static uint32_t arm_timer_read(void *opaque, target_phys_addr_t offset)
pbrookcdbdb642006-04-09 01:32:52 +000049{
50 arm_timer_state *s = (arm_timer_state *)opaque;
51
52 switch (offset >> 2) {
53 case 0: /* TimerLoad */
54 case 6: /* TimerBGLoad */
55 return s->limit;
56 case 1: /* TimerValue */
pbrook423f0742007-05-23 00:06:54 +000057 return ptimer_get_count(s->timer);
pbrookcdbdb642006-04-09 01:32:52 +000058 case 2: /* TimerControl */
59 return s->control;
60 case 4: /* TimerRIS */
61 return s->int_level;
62 case 5: /* TimerMIS */
63 if ((s->control & TIMER_CTRL_IE) == 0)
64 return 0;
65 return s->int_level;
66 default:
Peter Chubb4abc7eb2011-11-22 04:20:23 +010067 hw_error("%s: Bad offset %x\n", __func__, (int)offset);
pbrookcdbdb642006-04-09 01:32:52 +000068 return 0;
69 }
70}
71
pbrook423f0742007-05-23 00:06:54 +000072/* Reset the timer limit after settings have changed. */
73static void arm_timer_recalibrate(arm_timer_state *s, int reload)
74{
75 uint32_t limit;
76
Rabin Vincenta9cf98d2010-05-02 15:20:52 +053077 if ((s->control & (TIMER_CTRL_PERIODIC | TIMER_CTRL_ONESHOT)) == 0) {
pbrook423f0742007-05-23 00:06:54 +000078 /* Free running. */
79 if (s->control & TIMER_CTRL_32BIT)
80 limit = 0xffffffff;
81 else
82 limit = 0xffff;
83 } else {
84 /* Periodic. */
85 limit = s->limit;
86 }
87 ptimer_set_limit(s->timer, limit, reload);
88}
89
Anthony Liguoric227f092009-10-01 16:12:16 -050090static void arm_timer_write(void *opaque, target_phys_addr_t offset,
pbrookcdbdb642006-04-09 01:32:52 +000091 uint32_t value)
92{
93 arm_timer_state *s = (arm_timer_state *)opaque;
pbrook423f0742007-05-23 00:06:54 +000094 int freq;
pbrookcdbdb642006-04-09 01:32:52 +000095
pbrookcdbdb642006-04-09 01:32:52 +000096 switch (offset >> 2) {
97 case 0: /* TimerLoad */
98 s->limit = value;
pbrook423f0742007-05-23 00:06:54 +000099 arm_timer_recalibrate(s, 1);
pbrookcdbdb642006-04-09 01:32:52 +0000100 break;
101 case 1: /* TimerValue */
102 /* ??? Linux seems to want to write to this readonly register.
103 Ignore it. */
104 break;
105 case 2: /* TimerControl */
106 if (s->control & TIMER_CTRL_ENABLE) {
107 /* Pause the timer if it is running. This may cause some
108 inaccuracy dure to rounding, but avoids a whole lot of other
109 messyness. */
pbrook423f0742007-05-23 00:06:54 +0000110 ptimer_stop(s->timer);
pbrookcdbdb642006-04-09 01:32:52 +0000111 }
112 s->control = value;
pbrook423f0742007-05-23 00:06:54 +0000113 freq = s->freq;
pbrookcdbdb642006-04-09 01:32:52 +0000114 /* ??? Need to recalculate expiry time after changing divisor. */
115 switch ((value >> 2) & 3) {
pbrook423f0742007-05-23 00:06:54 +0000116 case 1: freq >>= 4; break;
117 case 2: freq >>= 8; break;
pbrookcdbdb642006-04-09 01:32:52 +0000118 }
Rabin Vincentd6759902010-05-02 15:20:51 +0530119 arm_timer_recalibrate(s, s->control & TIMER_CTRL_ENABLE);
pbrook423f0742007-05-23 00:06:54 +0000120 ptimer_set_freq(s->timer, freq);
pbrookcdbdb642006-04-09 01:32:52 +0000121 if (s->control & TIMER_CTRL_ENABLE) {
122 /* Restart the timer if still enabled. */
pbrook423f0742007-05-23 00:06:54 +0000123 ptimer_run(s->timer, (s->control & TIMER_CTRL_ONESHOT) != 0);
pbrookcdbdb642006-04-09 01:32:52 +0000124 }
125 break;
126 case 3: /* TimerIntClr */
127 s->int_level = 0;
128 break;
129 case 6: /* TimerBGLoad */
130 s->limit = value;
pbrook423f0742007-05-23 00:06:54 +0000131 arm_timer_recalibrate(s, 0);
pbrookcdbdb642006-04-09 01:32:52 +0000132 break;
133 default:
Peter Chubb4abc7eb2011-11-22 04:20:23 +0100134 hw_error("%s: Bad offset %x\n", __func__, (int)offset);
pbrookcdbdb642006-04-09 01:32:52 +0000135 }
pbrook423f0742007-05-23 00:06:54 +0000136 arm_timer_update(s);
pbrookcdbdb642006-04-09 01:32:52 +0000137}
138
139static void arm_timer_tick(void *opaque)
140{
pbrook423f0742007-05-23 00:06:54 +0000141 arm_timer_state *s = (arm_timer_state *)opaque;
142 s->int_level = 1;
143 arm_timer_update(s);
pbrookcdbdb642006-04-09 01:32:52 +0000144}
145
Juan Quintelaeecd33a2010-12-01 23:15:41 +0100146static const VMStateDescription vmstate_arm_timer = {
147 .name = "arm_timer",
148 .version_id = 1,
149 .minimum_version_id = 1,
150 .minimum_version_id_old = 1,
151 .fields = (VMStateField[]) {
152 VMSTATE_UINT32(control, arm_timer_state),
153 VMSTATE_UINT32(limit, arm_timer_state),
154 VMSTATE_INT32(int_level, arm_timer_state),
155 VMSTATE_PTIMER(timer, arm_timer_state),
156 VMSTATE_END_OF_LIST()
157 }
158};
pbrook23e39292008-07-02 16:48:32 +0000159
Paul Brook6a824ec2009-05-14 22:35:07 +0100160static arm_timer_state *arm_timer_init(uint32_t freq)
pbrookcdbdb642006-04-09 01:32:52 +0000161{
162 arm_timer_state *s;
pbrook423f0742007-05-23 00:06:54 +0000163 QEMUBH *bh;
pbrookcdbdb642006-04-09 01:32:52 +0000164
Anthony Liguori7267c092011-08-20 22:09:37 -0500165 s = (arm_timer_state *)g_malloc0(sizeof(arm_timer_state));
pbrook423f0742007-05-23 00:06:54 +0000166 s->freq = freq;
pbrookcdbdb642006-04-09 01:32:52 +0000167 s->control = TIMER_CTRL_IE;
pbrookcdbdb642006-04-09 01:32:52 +0000168
pbrook423f0742007-05-23 00:06:54 +0000169 bh = qemu_bh_new(arm_timer_tick, s);
170 s->timer = ptimer_init(bh);
Juan Quintelaeecd33a2010-12-01 23:15:41 +0100171 vmstate_register(NULL, -1, &vmstate_arm_timer, s);
pbrookcdbdb642006-04-09 01:32:52 +0000172 return s;
173}
174
175/* ARM PrimeCell SP804 dual timer module.
Peter Chubb7b4252e2011-12-12 10:25:42 +0000176 * Docs at
177 * http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0271d/index.html
178*/
pbrookcdbdb642006-04-09 01:32:52 +0000179
180typedef struct {
Paul Brook6a824ec2009-05-14 22:35:07 +0100181 SysBusDevice busdev;
Avi Kivitye219dea2011-08-15 17:17:19 +0300182 MemoryRegion iomem;
Paul Brook6a824ec2009-05-14 22:35:07 +0100183 arm_timer_state *timer[2];
Mark Langsdorf104a26a2011-12-29 06:19:51 +0000184 uint32_t freq0, freq1;
pbrookcdbdb642006-04-09 01:32:52 +0000185 int level[2];
pbrookd537cf62007-04-07 18:14:41 +0000186 qemu_irq irq;
pbrookcdbdb642006-04-09 01:32:52 +0000187} sp804_state;
188
Peter Chubb7b4252e2011-12-12 10:25:42 +0000189static const uint8_t sp804_ids[] = {
190 /* Timer ID */
191 0x04, 0x18, 0x14, 0,
192 /* PrimeCell ID */
193 0xd, 0xf0, 0x05, 0xb1
194};
195
pbrookd537cf62007-04-07 18:14:41 +0000196/* Merge the IRQs from the two component devices. */
pbrookcdbdb642006-04-09 01:32:52 +0000197static void sp804_set_irq(void *opaque, int irq, int level)
198{
199 sp804_state *s = (sp804_state *)opaque;
200
201 s->level[irq] = level;
pbrookd537cf62007-04-07 18:14:41 +0000202 qemu_set_irq(s->irq, s->level[0] || s->level[1]);
pbrookcdbdb642006-04-09 01:32:52 +0000203}
204
Avi Kivitye219dea2011-08-15 17:17:19 +0300205static uint64_t sp804_read(void *opaque, target_phys_addr_t offset,
206 unsigned size)
pbrookcdbdb642006-04-09 01:32:52 +0000207{
208 sp804_state *s = (sp804_state *)opaque;
209
pbrookcdbdb642006-04-09 01:32:52 +0000210 if (offset < 0x20) {
211 return arm_timer_read(s->timer[0], offset);
Peter Chubb7b4252e2011-12-12 10:25:42 +0000212 }
213 if (offset < 0x40) {
pbrookcdbdb642006-04-09 01:32:52 +0000214 return arm_timer_read(s->timer[1], offset - 0x20);
215 }
Peter Chubb7b4252e2011-12-12 10:25:42 +0000216
217 /* TimerPeriphID */
218 if (offset >= 0xfe0 && offset <= 0xffc) {
219 return sp804_ids[(offset - 0xfe0) >> 2];
220 }
221
222 switch (offset) {
223 /* Integration Test control registers, which we won't support */
224 case 0xf00: /* TimerITCR */
225 case 0xf04: /* TimerITOP (strictly write only but..) */
226 return 0;
227 }
228
229 hw_error("%s: Bad offset %x\n", __func__, (int)offset);
230 return 0;
pbrookcdbdb642006-04-09 01:32:52 +0000231}
232
Anthony Liguoric227f092009-10-01 16:12:16 -0500233static void sp804_write(void *opaque, target_phys_addr_t offset,
Avi Kivitye219dea2011-08-15 17:17:19 +0300234 uint64_t value, unsigned size)
pbrookcdbdb642006-04-09 01:32:52 +0000235{
236 sp804_state *s = (sp804_state *)opaque;
237
pbrookcdbdb642006-04-09 01:32:52 +0000238 if (offset < 0x20) {
239 arm_timer_write(s->timer[0], offset, value);
Peter Chubb7b4252e2011-12-12 10:25:42 +0000240 return;
pbrookcdbdb642006-04-09 01:32:52 +0000241 }
Peter Chubb7b4252e2011-12-12 10:25:42 +0000242
243 if (offset < 0x40) {
244 arm_timer_write(s->timer[1], offset - 0x20, value);
245 return;
246 }
247
248 /* Technically we could be writing to the Test Registers, but not likely */
249 hw_error("%s: Bad offset %x\n", __func__, (int)offset);
pbrookcdbdb642006-04-09 01:32:52 +0000250}
251
Avi Kivitye219dea2011-08-15 17:17:19 +0300252static const MemoryRegionOps sp804_ops = {
253 .read = sp804_read,
254 .write = sp804_write,
255 .endianness = DEVICE_NATIVE_ENDIAN,
pbrookcdbdb642006-04-09 01:32:52 +0000256};
257
Juan Quintela81986ac2010-12-01 23:12:32 +0100258static const VMStateDescription vmstate_sp804 = {
259 .name = "sp804",
260 .version_id = 1,
261 .minimum_version_id = 1,
262 .minimum_version_id_old = 1,
263 .fields = (VMStateField[]) {
264 VMSTATE_INT32_ARRAY(level, sp804_state, 2),
265 VMSTATE_END_OF_LIST()
266 }
267};
pbrook23e39292008-07-02 16:48:32 +0000268
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200269static int sp804_init(SysBusDevice *dev)
pbrookcdbdb642006-04-09 01:32:52 +0000270{
Paul Brook6a824ec2009-05-14 22:35:07 +0100271 sp804_state *s = FROM_SYSBUS(sp804_state, dev);
pbrookd537cf62007-04-07 18:14:41 +0000272 qemu_irq *qi;
pbrookcdbdb642006-04-09 01:32:52 +0000273
pbrookd537cf62007-04-07 18:14:41 +0000274 qi = qemu_allocate_irqs(sp804_set_irq, s, 2);
Paul Brook6a824ec2009-05-14 22:35:07 +0100275 sysbus_init_irq(dev, &s->irq);
Mark Langsdorf104a26a2011-12-29 06:19:51 +0000276 /* The timers are configurable between 32kHz and 1MHz
277 * defaulting to 1MHz but overrideable as individual properties */
278 s->timer[0] = arm_timer_init(s->freq0);
279 s->timer[1] = arm_timer_init(s->freq1);
280
Paul Brook6a824ec2009-05-14 22:35:07 +0100281 s->timer[0]->irq = qi[0];
282 s->timer[1]->irq = qi[1];
Avi Kivitye219dea2011-08-15 17:17:19 +0300283 memory_region_init_io(&s->iomem, &sp804_ops, s, "sp804", 0x1000);
Avi Kivity750ecd42011-11-27 11:38:10 +0200284 sysbus_init_mmio(dev, &s->iomem);
Juan Quintela81986ac2010-12-01 23:12:32 +0100285 vmstate_register(&dev->qdev, -1, &vmstate_sp804, s);
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200286 return 0;
pbrookcdbdb642006-04-09 01:32:52 +0000287}
288
Mark Langsdorf104a26a2011-12-29 06:19:51 +0000289static SysBusDeviceInfo sp804_info = {
290 .init = sp804_init,
291 .qdev.name = "sp804",
292 .qdev.size = sizeof(sp804_state),
293 .qdev.props = (Property[]) {
294 DEFINE_PROP_UINT32("freq0", sp804_state, freq0, 1000000),
295 DEFINE_PROP_UINT32("freq1", sp804_state, freq1, 1000000),
296 DEFINE_PROP_END_OF_LIST(),
297 }
298};
pbrookcdbdb642006-04-09 01:32:52 +0000299
300/* Integrator/CP timer module. */
301
302typedef struct {
Paul Brook6a824ec2009-05-14 22:35:07 +0100303 SysBusDevice busdev;
Avi Kivitye219dea2011-08-15 17:17:19 +0300304 MemoryRegion iomem;
Paul Brook6a824ec2009-05-14 22:35:07 +0100305 arm_timer_state *timer[3];
pbrookcdbdb642006-04-09 01:32:52 +0000306} icp_pit_state;
307
Avi Kivitye219dea2011-08-15 17:17:19 +0300308static uint64_t icp_pit_read(void *opaque, target_phys_addr_t offset,
309 unsigned size)
pbrookcdbdb642006-04-09 01:32:52 +0000310{
311 icp_pit_state *s = (icp_pit_state *)opaque;
312 int n;
313
314 /* ??? Don't know the PrimeCell ID for this device. */
pbrookcdbdb642006-04-09 01:32:52 +0000315 n = offset >> 8;
Peter Maydellee71c982011-11-11 13:30:15 +0000316 if (n > 2) {
Peter Chubb4abc7eb2011-11-22 04:20:23 +0100317 hw_error("%s: Bad timer %d\n", __func__, n);
Paul Brook2ac71172009-05-08 02:35:15 +0100318 }
pbrookcdbdb642006-04-09 01:32:52 +0000319
320 return arm_timer_read(s->timer[n], offset & 0xff);
321}
322
Anthony Liguoric227f092009-10-01 16:12:16 -0500323static void icp_pit_write(void *opaque, target_phys_addr_t offset,
Avi Kivitye219dea2011-08-15 17:17:19 +0300324 uint64_t value, unsigned size)
pbrookcdbdb642006-04-09 01:32:52 +0000325{
326 icp_pit_state *s = (icp_pit_state *)opaque;
327 int n;
328
pbrookcdbdb642006-04-09 01:32:52 +0000329 n = offset >> 8;
Peter Maydellee71c982011-11-11 13:30:15 +0000330 if (n > 2) {
Peter Chubb4abc7eb2011-11-22 04:20:23 +0100331 hw_error("%s: Bad timer %d\n", __func__, n);
Paul Brook2ac71172009-05-08 02:35:15 +0100332 }
pbrookcdbdb642006-04-09 01:32:52 +0000333
334 arm_timer_write(s->timer[n], offset & 0xff, value);
335}
336
Avi Kivitye219dea2011-08-15 17:17:19 +0300337static const MemoryRegionOps icp_pit_ops = {
338 .read = icp_pit_read,
339 .write = icp_pit_write,
340 .endianness = DEVICE_NATIVE_ENDIAN,
pbrookcdbdb642006-04-09 01:32:52 +0000341};
342
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200343static int icp_pit_init(SysBusDevice *dev)
pbrookcdbdb642006-04-09 01:32:52 +0000344{
Paul Brook6a824ec2009-05-14 22:35:07 +0100345 icp_pit_state *s = FROM_SYSBUS(icp_pit_state, dev);
pbrookcdbdb642006-04-09 01:32:52 +0000346
pbrookcdbdb642006-04-09 01:32:52 +0000347 /* Timer 0 runs at the system clock speed (40MHz). */
Paul Brook6a824ec2009-05-14 22:35:07 +0100348 s->timer[0] = arm_timer_init(40000000);
pbrookcdbdb642006-04-09 01:32:52 +0000349 /* The other two timers run at 1MHz. */
Paul Brook6a824ec2009-05-14 22:35:07 +0100350 s->timer[1] = arm_timer_init(1000000);
351 s->timer[2] = arm_timer_init(1000000);
352
353 sysbus_init_irq(dev, &s->timer[0]->irq);
354 sysbus_init_irq(dev, &s->timer[1]->irq);
355 sysbus_init_irq(dev, &s->timer[2]->irq);
pbrookcdbdb642006-04-09 01:32:52 +0000356
Avi Kivitye219dea2011-08-15 17:17:19 +0300357 memory_region_init_io(&s->iomem, &icp_pit_ops, s, "icp_pit", 0x1000);
Avi Kivity750ecd42011-11-27 11:38:10 +0200358 sysbus_init_mmio(dev, &s->iomem);
pbrook23e39292008-07-02 16:48:32 +0000359 /* This device has no state to save/restore. The component timers will
360 save themselves. */
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200361 return 0;
pbrookcdbdb642006-04-09 01:32:52 +0000362}
Paul Brook6a824ec2009-05-14 22:35:07 +0100363
364static void arm_timer_register_devices(void)
365{
366 sysbus_register_dev("integrator_pit", sizeof(icp_pit_state), icp_pit_init);
Mark Langsdorf104a26a2011-12-29 06:19:51 +0000367 sysbus_register_withprop(&sp804_info);
Paul Brook6a824ec2009-05-14 22:35:07 +0100368}
369
370device_init(arm_timer_register_devices)