]> Git Repo - qemu.git/blame - hw/timer/arm_timer.c
usb: delete redundant brackets in usb_host_handle_control()
[qemu.git] / hw / timer / arm_timer.c
CommitLineData
5fafdf24 1/*
cdbdb648
PB
2 * ARM PrimeCell Timer modules.
3 *
4 * Copyright (c) 2005-2006 CodeSourcery.
5 * Written by Paul Brook
6 *
8e31bf38 7 * This code is licensed under the GPL.
cdbdb648
PB
8 */
9
83c9f4ca 10#include "hw/sysbus.h"
1de7afc9 11#include "qemu/timer.h"
104a26a2 12#include "qemu-common.h"
83c9f4ca
PB
13#include "hw/qdev.h"
14#include "hw/ptimer.h"
6a1751b7 15#include "qemu/main-loop.h"
cdbdb648
PB
16
17/* Common timer implementation. */
18
19#define TIMER_CTRL_ONESHOT (1 << 0)
20#define TIMER_CTRL_32BIT (1 << 1)
21#define TIMER_CTRL_DIV1 (0 << 2)
22#define TIMER_CTRL_DIV16 (1 << 2)
23#define TIMER_CTRL_DIV256 (2 << 2)
24#define TIMER_CTRL_IE (1 << 5)
25#define TIMER_CTRL_PERIODIC (1 << 6)
26#define TIMER_CTRL_ENABLE (1 << 7)
27
28typedef struct {
423f0742 29 ptimer_state *timer;
cdbdb648 30 uint32_t control;
cdbdb648 31 uint32_t limit;
cdbdb648
PB
32 int freq;
33 int int_level;
d537cf6c 34 qemu_irq irq;
cdbdb648
PB
35} arm_timer_state;
36
cdbdb648
PB
37/* Check all active timers, and schedule the next timer interrupt. */
38
423f0742 39static void arm_timer_update(arm_timer_state *s)
cdbdb648 40{
cdbdb648
PB
41 /* Update interrupts. */
42 if (s->int_level && (s->control & TIMER_CTRL_IE)) {
d537cf6c 43 qemu_irq_raise(s->irq);
cdbdb648 44 } else {
d537cf6c 45 qemu_irq_lower(s->irq);
cdbdb648 46 }
cdbdb648
PB
47}
48
a8170e5e 49static uint32_t arm_timer_read(void *opaque, hwaddr offset)
cdbdb648
PB
50{
51 arm_timer_state *s = (arm_timer_state *)opaque;
52
53 switch (offset >> 2) {
54 case 0: /* TimerLoad */
55 case 6: /* TimerBGLoad */
56 return s->limit;
57 case 1: /* TimerValue */
423f0742 58 return ptimer_get_count(s->timer);
cdbdb648
PB
59 case 2: /* TimerControl */
60 return s->control;
61 case 4: /* TimerRIS */
62 return s->int_level;
63 case 5: /* TimerMIS */
64 if ((s->control & TIMER_CTRL_IE) == 0)
65 return 0;
66 return s->int_level;
67 default:
edb94a41
PM
68 qemu_log_mask(LOG_GUEST_ERROR,
69 "%s: Bad offset %x\n", __func__, (int)offset);
cdbdb648
PB
70 return 0;
71 }
72}
73
423f0742
PB
74/* Reset the timer limit after settings have changed. */
75static void arm_timer_recalibrate(arm_timer_state *s, int reload)
76{
77 uint32_t limit;
78
a9cf98d9 79 if ((s->control & (TIMER_CTRL_PERIODIC | TIMER_CTRL_ONESHOT)) == 0) {
423f0742
PB
80 /* Free running. */
81 if (s->control & TIMER_CTRL_32BIT)
82 limit = 0xffffffff;
83 else
84 limit = 0xffff;
85 } else {
86 /* Periodic. */
87 limit = s->limit;
88 }
89 ptimer_set_limit(s->timer, limit, reload);
90}
91
a8170e5e 92static void arm_timer_write(void *opaque, hwaddr offset,
cdbdb648
PB
93 uint32_t value)
94{
95 arm_timer_state *s = (arm_timer_state *)opaque;
423f0742 96 int freq;
cdbdb648 97
cdbdb648
PB
98 switch (offset >> 2) {
99 case 0: /* TimerLoad */
100 s->limit = value;
423f0742 101 arm_timer_recalibrate(s, 1);
cdbdb648
PB
102 break;
103 case 1: /* TimerValue */
104 /* ??? Linux seems to want to write to this readonly register.
105 Ignore it. */
106 break;
107 case 2: /* TimerControl */
108 if (s->control & TIMER_CTRL_ENABLE) {
109 /* Pause the timer if it is running. This may cause some
110 inaccuracy dure to rounding, but avoids a whole lot of other
111 messyness. */
423f0742 112 ptimer_stop(s->timer);
cdbdb648
PB
113 }
114 s->control = value;
423f0742 115 freq = s->freq;
cdbdb648
PB
116 /* ??? Need to recalculate expiry time after changing divisor. */
117 switch ((value >> 2) & 3) {
423f0742
PB
118 case 1: freq >>= 4; break;
119 case 2: freq >>= 8; break;
cdbdb648 120 }
d6759902 121 arm_timer_recalibrate(s, s->control & TIMER_CTRL_ENABLE);
423f0742 122 ptimer_set_freq(s->timer, freq);
cdbdb648
PB
123 if (s->control & TIMER_CTRL_ENABLE) {
124 /* Restart the timer if still enabled. */
423f0742 125 ptimer_run(s->timer, (s->control & TIMER_CTRL_ONESHOT) != 0);
cdbdb648
PB
126 }
127 break;
128 case 3: /* TimerIntClr */
129 s->int_level = 0;
130 break;
131 case 6: /* TimerBGLoad */
132 s->limit = value;
423f0742 133 arm_timer_recalibrate(s, 0);
cdbdb648
PB
134 break;
135 default:
edb94a41
PM
136 qemu_log_mask(LOG_GUEST_ERROR,
137 "%s: Bad offset %x\n", __func__, (int)offset);
cdbdb648 138 }
423f0742 139 arm_timer_update(s);
cdbdb648
PB
140}
141
142static void arm_timer_tick(void *opaque)
143{
423f0742
PB
144 arm_timer_state *s = (arm_timer_state *)opaque;
145 s->int_level = 1;
146 arm_timer_update(s);
cdbdb648
PB
147}
148
eecd33a5
JQ
149static const VMStateDescription vmstate_arm_timer = {
150 .name = "arm_timer",
151 .version_id = 1,
152 .minimum_version_id = 1,
8f1e884b 153 .fields = (VMStateField[]) {
eecd33a5
JQ
154 VMSTATE_UINT32(control, arm_timer_state),
155 VMSTATE_UINT32(limit, arm_timer_state),
156 VMSTATE_INT32(int_level, arm_timer_state),
157 VMSTATE_PTIMER(timer, arm_timer_state),
158 VMSTATE_END_OF_LIST()
159 }
160};
23e39294 161
6a824ec3 162static arm_timer_state *arm_timer_init(uint32_t freq)
cdbdb648
PB
163{
164 arm_timer_state *s;
423f0742 165 QEMUBH *bh;
cdbdb648 166
7267c094 167 s = (arm_timer_state *)g_malloc0(sizeof(arm_timer_state));
423f0742 168 s->freq = freq;
cdbdb648 169 s->control = TIMER_CTRL_IE;
cdbdb648 170
423f0742
PB
171 bh = qemu_bh_new(arm_timer_tick, s);
172 s->timer = ptimer_init(bh);
eecd33a5 173 vmstate_register(NULL, -1, &vmstate_arm_timer, s);
cdbdb648
PB
174 return s;
175}
176
177/* ARM PrimeCell SP804 dual timer module.
7b4252e8
PC
178 * Docs at
179 * http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0271d/index.html
180*/
cdbdb648 181
0c88dea5
AF
182#define TYPE_SP804 "sp804"
183#define SP804(obj) OBJECT_CHECK(SP804State, (obj), TYPE_SP804)
184
1024d7f0 185typedef struct SP804State {
0c88dea5
AF
186 SysBusDevice parent_obj;
187
e219dea2 188 MemoryRegion iomem;
6a824ec3 189 arm_timer_state *timer[2];
104a26a2 190 uint32_t freq0, freq1;
cdbdb648 191 int level[2];
d537cf6c 192 qemu_irq irq;
1024d7f0 193} SP804State;
cdbdb648 194
7b4252e8
PC
195static const uint8_t sp804_ids[] = {
196 /* Timer ID */
197 0x04, 0x18, 0x14, 0,
198 /* PrimeCell ID */
199 0xd, 0xf0, 0x05, 0xb1
200};
201
d537cf6c 202/* Merge the IRQs from the two component devices. */
cdbdb648
PB
203static void sp804_set_irq(void *opaque, int irq, int level)
204{
1024d7f0 205 SP804State *s = (SP804State *)opaque;
cdbdb648
PB
206
207 s->level[irq] = level;
d537cf6c 208 qemu_set_irq(s->irq, s->level[0] || s->level[1]);
cdbdb648
PB
209}
210
a8170e5e 211static uint64_t sp804_read(void *opaque, hwaddr offset,
e219dea2 212 unsigned size)
cdbdb648 213{
1024d7f0 214 SP804State *s = (SP804State *)opaque;
cdbdb648 215
cdbdb648
PB
216 if (offset < 0x20) {
217 return arm_timer_read(s->timer[0], offset);
7b4252e8
PC
218 }
219 if (offset < 0x40) {
cdbdb648
PB
220 return arm_timer_read(s->timer[1], offset - 0x20);
221 }
7b4252e8
PC
222
223 /* TimerPeriphID */
224 if (offset >= 0xfe0 && offset <= 0xffc) {
225 return sp804_ids[(offset - 0xfe0) >> 2];
226 }
227
228 switch (offset) {
229 /* Integration Test control registers, which we won't support */
230 case 0xf00: /* TimerITCR */
231 case 0xf04: /* TimerITOP (strictly write only but..) */
edb94a41
PM
232 qemu_log_mask(LOG_UNIMP,
233 "%s: integration test registers unimplemented\n",
234 __func__);
7b4252e8
PC
235 return 0;
236 }
237
edb94a41
PM
238 qemu_log_mask(LOG_GUEST_ERROR,
239 "%s: Bad offset %x\n", __func__, (int)offset);
7b4252e8 240 return 0;
cdbdb648
PB
241}
242
a8170e5e 243static void sp804_write(void *opaque, hwaddr offset,
e219dea2 244 uint64_t value, unsigned size)
cdbdb648 245{
1024d7f0 246 SP804State *s = (SP804State *)opaque;
cdbdb648 247
cdbdb648
PB
248 if (offset < 0x20) {
249 arm_timer_write(s->timer[0], offset, value);
7b4252e8
PC
250 return;
251 }
252
253 if (offset < 0x40) {
cdbdb648 254 arm_timer_write(s->timer[1], offset - 0x20, value);
7b4252e8 255 return;
cdbdb648 256 }
7b4252e8
PC
257
258 /* Technically we could be writing to the Test Registers, but not likely */
edb94a41
PM
259 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %x\n",
260 __func__, (int)offset);
cdbdb648
PB
261}
262
e219dea2
AK
263static const MemoryRegionOps sp804_ops = {
264 .read = sp804_read,
265 .write = sp804_write,
266 .endianness = DEVICE_NATIVE_ENDIAN,
cdbdb648
PB
267};
268
81986ac4
JQ
269static const VMStateDescription vmstate_sp804 = {
270 .name = "sp804",
271 .version_id = 1,
272 .minimum_version_id = 1,
8f1e884b 273 .fields = (VMStateField[]) {
1024d7f0 274 VMSTATE_INT32_ARRAY(level, SP804State, 2),
81986ac4
JQ
275 VMSTATE_END_OF_LIST()
276 }
277};
23e39294 278
0c88dea5 279static int sp804_init(SysBusDevice *sbd)
cdbdb648 280{
0c88dea5
AF
281 DeviceState *dev = DEVICE(sbd);
282 SP804State *s = SP804(dev);
d537cf6c 283 qemu_irq *qi;
cdbdb648 284
d537cf6c 285 qi = qemu_allocate_irqs(sp804_set_irq, s, 2);
0c88dea5 286 sysbus_init_irq(sbd, &s->irq);
104a26a2
ML
287 s->timer[0] = arm_timer_init(s->freq0);
288 s->timer[1] = arm_timer_init(s->freq1);
6a824ec3
PB
289 s->timer[0]->irq = qi[0];
290 s->timer[1]->irq = qi[1];
853dca12
PB
291 memory_region_init_io(&s->iomem, OBJECT(s), &sp804_ops, s,
292 "sp804", 0x1000);
0c88dea5
AF
293 sysbus_init_mmio(sbd, &s->iomem);
294 vmstate_register(dev, -1, &vmstate_sp804, s);
81a322d4 295 return 0;
cdbdb648
PB
296}
297
cdbdb648
PB
298/* Integrator/CP timer module. */
299
e2051b42
AF
300#define TYPE_INTEGRATOR_PIT "integrator_pit"
301#define INTEGRATOR_PIT(obj) \
302 OBJECT_CHECK(icp_pit_state, (obj), TYPE_INTEGRATOR_PIT)
303
cdbdb648 304typedef struct {
e2051b42
AF
305 SysBusDevice parent_obj;
306
e219dea2 307 MemoryRegion iomem;
6a824ec3 308 arm_timer_state *timer[3];
cdbdb648
PB
309} icp_pit_state;
310
a8170e5e 311static uint64_t icp_pit_read(void *opaque, hwaddr offset,
e219dea2 312 unsigned size)
cdbdb648
PB
313{
314 icp_pit_state *s = (icp_pit_state *)opaque;
315 int n;
316
317 /* ??? Don't know the PrimeCell ID for this device. */
cdbdb648 318 n = offset >> 8;
ee71c984 319 if (n > 2) {
edb94a41 320 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad timer %d\n", __func__, n);
cba933b2 321 return 0;
2ac71179 322 }
cdbdb648
PB
323
324 return arm_timer_read(s->timer[n], offset & 0xff);
325}
326
a8170e5e 327static void icp_pit_write(void *opaque, hwaddr offset,
e219dea2 328 uint64_t value, unsigned size)
cdbdb648
PB
329{
330 icp_pit_state *s = (icp_pit_state *)opaque;
331 int n;
332
cdbdb648 333 n = offset >> 8;
ee71c984 334 if (n > 2) {
edb94a41 335 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad timer %d\n", __func__, n);
cba933b2 336 return;
2ac71179 337 }
cdbdb648
PB
338
339 arm_timer_write(s->timer[n], offset & 0xff, value);
340}
341
e219dea2
AK
342static const MemoryRegionOps icp_pit_ops = {
343 .read = icp_pit_read,
344 .write = icp_pit_write,
345 .endianness = DEVICE_NATIVE_ENDIAN,
cdbdb648
PB
346};
347
81a322d4 348static int icp_pit_init(SysBusDevice *dev)
cdbdb648 349{
e2051b42 350 icp_pit_state *s = INTEGRATOR_PIT(dev);
cdbdb648 351
cdbdb648 352 /* Timer 0 runs at the system clock speed (40MHz). */
6a824ec3 353 s->timer[0] = arm_timer_init(40000000);
cdbdb648 354 /* The other two timers run at 1MHz. */
6a824ec3
PB
355 s->timer[1] = arm_timer_init(1000000);
356 s->timer[2] = arm_timer_init(1000000);
357
358 sysbus_init_irq(dev, &s->timer[0]->irq);
359 sysbus_init_irq(dev, &s->timer[1]->irq);
360 sysbus_init_irq(dev, &s->timer[2]->irq);
cdbdb648 361
853dca12
PB
362 memory_region_init_io(&s->iomem, OBJECT(s), &icp_pit_ops, s,
363 "icp_pit", 0x1000);
750ecd44 364 sysbus_init_mmio(dev, &s->iomem);
23e39294
PB
365 /* This device has no state to save/restore. The component timers will
366 save themselves. */
81a322d4 367 return 0;
cdbdb648 368}
6a824ec3 369
999e12bb
AL
370static void icp_pit_class_init(ObjectClass *klass, void *data)
371{
372 SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);
373
374 sdc->init = icp_pit_init;
375}
376
8c43a6f0 377static const TypeInfo icp_pit_info = {
e2051b42 378 .name = TYPE_INTEGRATOR_PIT,
39bffca2
AL
379 .parent = TYPE_SYS_BUS_DEVICE,
380 .instance_size = sizeof(icp_pit_state),
381 .class_init = icp_pit_class_init,
382};
383
384static Property sp804_properties[] = {
1024d7f0
AF
385 DEFINE_PROP_UINT32("freq0", SP804State, freq0, 1000000),
386 DEFINE_PROP_UINT32("freq1", SP804State, freq1, 1000000),
39bffca2 387 DEFINE_PROP_END_OF_LIST(),
999e12bb
AL
388};
389
390static void sp804_class_init(ObjectClass *klass, void *data)
391{
392 SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);
39bffca2 393 DeviceClass *k = DEVICE_CLASS(klass);
999e12bb
AL
394
395 sdc->init = sp804_init;
39bffca2 396 k->props = sp804_properties;
999e12bb
AL
397}
398
8c43a6f0 399static const TypeInfo sp804_info = {
0c88dea5 400 .name = TYPE_SP804,
39bffca2 401 .parent = TYPE_SYS_BUS_DEVICE,
1024d7f0 402 .instance_size = sizeof(SP804State),
39bffca2 403 .class_init = sp804_class_init,
999e12bb
AL
404};
405
83f7d43a 406static void arm_timer_register_types(void)
6a824ec3 407{
39bffca2
AL
408 type_register_static(&icp_pit_info);
409 type_register_static(&sp804_info);
6a824ec3
PB
410}
411
83f7d43a 412type_init(arm_timer_register_types)
This page took 0.740357 seconds and 4 git commands to generate.