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.
10 * Contributions after 2012-01-13 are licensed under the terms of the
11 * GNU GPL, version 2 or (at your option) any later version.
14 #include "qemu/osdep.h"
15 #include "hw/timer/pl031.h"
16 #include "hw/sysbus.h"
17 #include "qemu/timer.h"
18 #include "sysemu/sysemu.h"
19 #include "qemu/cutils.h"
23 #define RTC_DR 0x00 /* Data read register */
24 #define RTC_MR 0x04 /* Match register */
25 #define RTC_LR 0x08 /* Data load register */
26 #define RTC_CR 0x0c /* Control register */
27 #define RTC_IMSC 0x10 /* Interrupt mask and set register */
28 #define RTC_RIS 0x14 /* Raw interrupt status register */
29 #define RTC_MIS 0x18 /* Masked interrupt status register */
30 #define RTC_ICR 0x1c /* Interrupt clear register */
32 static const unsigned char pl031_id
[] = {
33 0x31, 0x10, 0x14, 0x00, /* Device ID */
34 0x0d, 0xf0, 0x05, 0xb1 /* Cell ID */
37 static void pl031_update(PL031State
*s
)
39 uint32_t flags
= s
->is
& s
->im
;
41 trace_pl031_irq_state(flags
);
42 qemu_set_irq(s
->irq
, flags
);
45 static void pl031_interrupt(void * opaque
)
47 PL031State
*s
= (PL031State
*)opaque
;
50 trace_pl031_alarm_raised();
54 static uint32_t pl031_get_count(PL031State
*s
)
56 int64_t now
= qemu_clock_get_ns(rtc_clock
);
57 return s
->tick_offset
+ now
/ NANOSECONDS_PER_SECOND
;
60 static void pl031_set_alarm(PL031State
*s
)
64 /* The timer wraps around. This subtraction also wraps in the same way,
65 and gives correct results when alarm < now_ticks. */
66 ticks
= s
->mr
- pl031_get_count(s
);
67 trace_pl031_set_alarm(ticks
);
72 int64_t now
= qemu_clock_get_ns(rtc_clock
);
73 timer_mod(s
->timer
, now
+ (int64_t)ticks
* NANOSECONDS_PER_SECOND
);
77 static uint64_t pl031_read(void *opaque
, hwaddr offset
,
80 PL031State
*s
= (PL031State
*)opaque
;
85 r
= pl031_get_count(s
);
100 /* RTC is permanently enabled. */
106 case 0xfe0 ... 0xfff:
107 r
= pl031_id
[(offset
- 0xfe0) >> 2];
110 qemu_log_mask(LOG_GUEST_ERROR
,
111 "pl031: read of write-only register at offset 0x%x\n",
116 qemu_log_mask(LOG_GUEST_ERROR
,
117 "pl031_read: Bad offset 0x%x\n", (int)offset
);
122 trace_pl031_read(offset
, r
);
126 static void pl031_write(void * opaque
, hwaddr offset
,
127 uint64_t value
, unsigned size
)
129 PL031State
*s
= (PL031State
*)opaque
;
131 trace_pl031_write(offset
, value
);
135 s
->tick_offset
+= value
- pl031_get_count(s
);
147 /* The PL031 documentation (DDI0224B) states that the interrupt is
148 cleared when bit 0 of the written value is set. However the
149 arm926e documentation (DDI0287B) states that the interrupt is
150 cleared when any value is written. */
155 /* Written value is ignored. */
161 qemu_log_mask(LOG_GUEST_ERROR
,
162 "pl031: write to read-only register at offset 0x%x\n",
167 qemu_log_mask(LOG_GUEST_ERROR
,
168 "pl031_write: Bad offset 0x%x\n", (int)offset
);
173 static const MemoryRegionOps pl031_ops
= {
175 .write
= pl031_write
,
176 .endianness
= DEVICE_NATIVE_ENDIAN
,
179 static void pl031_init(Object
*obj
)
181 PL031State
*s
= PL031(obj
);
182 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
185 memory_region_init_io(&s
->iomem
, obj
, &pl031_ops
, s
, "pl031", 0x1000);
186 sysbus_init_mmio(dev
, &s
->iomem
);
188 sysbus_init_irq(dev
, &s
->irq
);
189 qemu_get_timedate(&tm
, 0);
190 s
->tick_offset
= mktimegm(&tm
) -
191 qemu_clock_get_ns(rtc_clock
) / NANOSECONDS_PER_SECOND
;
193 s
->timer
= timer_new_ns(rtc_clock
, pl031_interrupt
, s
);
196 static int pl031_pre_save(void *opaque
)
198 PL031State
*s
= opaque
;
200 /* tick_offset is base_time - rtc_clock base time. Instead, we want to
201 * store the base time relative to the QEMU_CLOCK_VIRTUAL for backwards-compatibility. */
202 int64_t delta
= qemu_clock_get_ns(rtc_clock
) - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
203 s
->tick_offset_vmstate
= s
->tick_offset
+ delta
/ NANOSECONDS_PER_SECOND
;
208 static int pl031_post_load(void *opaque
, int version_id
)
210 PL031State
*s
= opaque
;
212 int64_t delta
= qemu_clock_get_ns(rtc_clock
) - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
213 s
->tick_offset
= s
->tick_offset_vmstate
- delta
/ NANOSECONDS_PER_SECOND
;
218 static const VMStateDescription vmstate_pl031
= {
221 .minimum_version_id
= 1,
222 .pre_save
= pl031_pre_save
,
223 .post_load
= pl031_post_load
,
224 .fields
= (VMStateField
[]) {
225 VMSTATE_UINT32(tick_offset_vmstate
, PL031State
),
226 VMSTATE_UINT32(mr
, PL031State
),
227 VMSTATE_UINT32(lr
, PL031State
),
228 VMSTATE_UINT32(cr
, PL031State
),
229 VMSTATE_UINT32(im
, PL031State
),
230 VMSTATE_UINT32(is
, PL031State
),
231 VMSTATE_END_OF_LIST()
235 static void pl031_class_init(ObjectClass
*klass
, void *data
)
237 DeviceClass
*dc
= DEVICE_CLASS(klass
);
239 dc
->vmsd
= &vmstate_pl031
;
242 static const TypeInfo pl031_info
= {
244 .parent
= TYPE_SYS_BUS_DEVICE
,
245 .instance_size
= sizeof(PL031State
),
246 .instance_init
= pl031_init
,
247 .class_init
= pl031_class_init
,
250 static void pl031_register_types(void)
252 type_register_static(&pl031_info
);
255 type_init(pl031_register_types
)