2 * ARM AMBA PrimeCell PL031 RTC
4 * Copyright (c) 2007 CodeSourcery
6 * This file is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
13 #include "qemu-timer.h"
18 #define DPRINTF(fmt, ...) \
19 do { printf("pl031: " fmt , ## __VA_ARGS__); } while (0)
21 #define DPRINTF(fmt, ...) do {} while(0)
24 #define RTC_DR 0x00 /* Data read register */
25 #define RTC_MR 0x04 /* Match register */
26 #define RTC_LR 0x08 /* Data load register */
27 #define RTC_CR 0x0c /* Control register */
28 #define RTC_IMSC 0x10 /* Interrupt mask and set register */
29 #define RTC_RIS 0x14 /* Raw interrupt status register */
30 #define RTC_MIS 0x18 /* Masked interrupt status register */
31 #define RTC_ICR 0x1c /* Interrupt clear register */
47 static const VMStateDescription vmstate_pl031 = {
50 .minimum_version_id = 1,
51 .fields = (VMStateField[]) {
52 VMSTATE_UINT32(tick_offset, pl031_state),
53 VMSTATE_UINT32(mr, pl031_state),
54 VMSTATE_UINT32(lr, pl031_state),
55 VMSTATE_UINT32(cr, pl031_state),
56 VMSTATE_UINT32(im, pl031_state),
57 VMSTATE_UINT32(is, pl031_state),
62 static const unsigned char pl031_id[] = {
63 0x31, 0x10, 0x14, 0x00, /* Device ID */
64 0x0d, 0xf0, 0x05, 0xb1 /* Cell ID */
67 static void pl031_update(pl031_state *s)
69 qemu_set_irq(s->irq, s->is & s->im);
72 static void pl031_interrupt(void * opaque)
74 pl031_state *s = (pl031_state *)opaque;
77 DPRINTF("Alarm raised\n");
81 static uint32_t pl031_get_count(pl031_state *s)
83 /* This assumes qemu_get_clock_ns returns the time since the machine was
85 return s->tick_offset + qemu_get_clock_ns(vm_clock) / get_ticks_per_sec();
88 static void pl031_set_alarm(pl031_state *s)
93 now = qemu_get_clock_ns(vm_clock);
94 ticks = s->tick_offset + now / get_ticks_per_sec();
96 /* The timer wraps around. This subtraction also wraps in the same way,
97 and gives correct results when alarm < now_ticks. */
98 ticks = s->mr - ticks;
99 DPRINTF("Alarm set in %ud ticks\n", ticks);
101 qemu_del_timer(s->timer);
104 qemu_mod_timer(s->timer, now + (int64_t)ticks * get_ticks_per_sec());
108 static uint32_t pl031_read(void *opaque, target_phys_addr_t offset)
110 pl031_state *s = (pl031_state *)opaque;
112 if (offset >= 0xfe0 && offset < 0x1000)
113 return pl031_id[(offset - 0xfe0) >> 2];
117 return pl031_get_count(s);
127 /* RTC is permanently enabled. */
130 return s->is & s->im;
132 fprintf(stderr, "qemu: pl031_read: Unexpected offset 0x%x\n",
136 hw_error("pl031_read: Bad offset 0x%x\n", (int)offset);
143 static void pl031_write(void * opaque, target_phys_addr_t offset,
146 pl031_state *s = (pl031_state *)opaque;
151 s->tick_offset += value - pl031_get_count(s);
160 DPRINTF("Interrupt mask %d\n", s->im);
164 /* The PL031 documentation (DDI0224B) states that the interrupt is
165 cleared when bit 0 of the written value is set. However the
166 arm926e documentation (DDI0287B) states that the interrupt is
167 cleared when any value is written. */
168 DPRINTF("Interrupt cleared");
173 /* Written value is ignored. */
179 fprintf(stderr, "qemu: pl031_write: Unexpected offset 0x%x\n",
184 hw_error("pl031_write: Bad offset 0x%x\n", (int)offset);
189 static CPUWriteMemoryFunc * const pl031_writefn[] = {
195 static CPUReadMemoryFunc * const pl031_readfn[] = {
201 static int pl031_init(SysBusDevice *dev)
204 pl031_state *s = FROM_SYSBUS(pl031_state, dev);
207 iomemtype = cpu_register_io_memory(pl031_readfn, pl031_writefn, s,
208 DEVICE_NATIVE_ENDIAN);
209 if (iomemtype == -1) {
210 hw_error("pl031_init: Can't register I/O memory\n");
213 sysbus_init_mmio(dev, 0x1000, iomemtype);
215 sysbus_init_irq(dev, &s->irq);
216 /* ??? We assume vm_clock is zero at this point. */
217 qemu_get_timedate(&tm, 0);
218 s->tick_offset = mktimegm(&tm);
220 s->timer = qemu_new_timer_ns(vm_clock, pl031_interrupt, s);
224 static SysBusDeviceInfo pl031_info = {
226 .qdev.name = "pl031",
227 .qdev.size = sizeof(pl031_state),
228 .qdev.vmsd = &vmstate_pl031,
232 static void pl031_register_devices(void)
234 sysbus_register_withprop(&pl031_info);
237 device_init(pl031_register_devices)