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/sysbus.h"
16 #include "qemu/timer.h"
17 #include "sysemu/sysemu.h"
22 #define DPRINTF(fmt, ...) \
23 do { printf("pl031: " fmt , ## __VA_ARGS__); } while (0)
25 #define DPRINTF(fmt, ...) do {} while(0)
28 #define RTC_DR 0x00 /* Data read register */
29 #define RTC_MR 0x04 /* Match register */
30 #define RTC_LR 0x08 /* Data load register */
31 #define RTC_CR 0x0c /* Control register */
32 #define RTC_IMSC 0x10 /* Interrupt mask and set register */
33 #define RTC_RIS 0x14 /* Raw interrupt status register */
34 #define RTC_MIS 0x18 /* Masked interrupt status register */
35 #define RTC_ICR 0x1c /* Interrupt clear register */
37 #define TYPE_PL031 "pl031"
38 #define PL031(obj) OBJECT_CHECK(PL031State, (obj), TYPE_PL031)
40 typedef struct PL031State
{
41 SysBusDevice parent_obj
;
47 /* Needed to preserve the tick_count across migration, even if the
48 * absolute value of the rtc_clock is different on the source and
51 uint32_t tick_offset_vmstate
;
61 static const unsigned char pl031_id
[] = {
62 0x31, 0x10, 0x14, 0x00, /* Device ID */
63 0x0d, 0xf0, 0x05, 0xb1 /* Cell ID */
66 static void pl031_update(PL031State
*s
)
68 qemu_set_irq(s
->irq
, s
->is
& s
->im
);
71 static void pl031_interrupt(void * opaque
)
73 PL031State
*s
= (PL031State
*)opaque
;
76 DPRINTF("Alarm raised\n");
80 static uint32_t pl031_get_count(PL031State
*s
)
82 int64_t now
= qemu_clock_get_ns(rtc_clock
);
83 return s
->tick_offset
+ now
/ get_ticks_per_sec();
86 static void pl031_set_alarm(PL031State
*s
)
90 /* The timer wraps around. This subtraction also wraps in the same way,
91 and gives correct results when alarm < now_ticks. */
92 ticks
= s
->mr
- pl031_get_count(s
);
93 DPRINTF("Alarm set in %ud ticks\n", ticks
);
98 int64_t now
= qemu_clock_get_ns(rtc_clock
);
99 timer_mod(s
->timer
, now
+ (int64_t)ticks
* get_ticks_per_sec());
103 static uint64_t pl031_read(void *opaque
, hwaddr offset
,
106 PL031State
*s
= (PL031State
*)opaque
;
108 if (offset
>= 0xfe0 && offset
< 0x1000)
109 return pl031_id
[(offset
- 0xfe0) >> 2];
113 return pl031_get_count(s
);
123 /* RTC is permanently enabled. */
126 return s
->is
& s
->im
;
128 qemu_log_mask(LOG_GUEST_ERROR
,
129 "pl031: read of write-only register at offset 0x%x\n",
133 qemu_log_mask(LOG_GUEST_ERROR
,
134 "pl031_read: Bad offset 0x%x\n", (int)offset
);
141 static void pl031_write(void * opaque
, hwaddr offset
,
142 uint64_t value
, unsigned size
)
144 PL031State
*s
= (PL031State
*)opaque
;
149 s
->tick_offset
+= value
- pl031_get_count(s
);
158 DPRINTF("Interrupt mask %d\n", s
->im
);
162 /* The PL031 documentation (DDI0224B) states that the interrupt is
163 cleared when bit 0 of the written value is set. However the
164 arm926e documentation (DDI0287B) states that the interrupt is
165 cleared when any value is written. */
166 DPRINTF("Interrupt cleared");
171 /* Written value is ignored. */
177 qemu_log_mask(LOG_GUEST_ERROR
,
178 "pl031: write to read-only register at offset 0x%x\n",
183 qemu_log_mask(LOG_GUEST_ERROR
,
184 "pl031_write: Bad offset 0x%x\n", (int)offset
);
189 static const MemoryRegionOps pl031_ops
= {
191 .write
= pl031_write
,
192 .endianness
= DEVICE_NATIVE_ENDIAN
,
195 static int pl031_init(SysBusDevice
*dev
)
197 PL031State
*s
= PL031(dev
);
200 memory_region_init_io(&s
->iomem
, OBJECT(s
), &pl031_ops
, s
, "pl031", 0x1000);
201 sysbus_init_mmio(dev
, &s
->iomem
);
203 sysbus_init_irq(dev
, &s
->irq
);
204 qemu_get_timedate(&tm
, 0);
205 s
->tick_offset
= mktimegm(&tm
) -
206 qemu_clock_get_ns(rtc_clock
) / get_ticks_per_sec();
208 s
->timer
= timer_new_ns(rtc_clock
, pl031_interrupt
, s
);
212 static void pl031_pre_save(void *opaque
)
214 PL031State
*s
= opaque
;
216 /* tick_offset is base_time - rtc_clock base time. Instead, we want to
217 * store the base time relative to the QEMU_CLOCK_VIRTUAL for backwards-compatibility. */
218 int64_t delta
= qemu_clock_get_ns(rtc_clock
) - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
219 s
->tick_offset_vmstate
= s
->tick_offset
+ delta
/ get_ticks_per_sec();
222 static int pl031_post_load(void *opaque
, int version_id
)
224 PL031State
*s
= opaque
;
226 int64_t delta
= qemu_clock_get_ns(rtc_clock
) - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
227 s
->tick_offset
= s
->tick_offset_vmstate
- delta
/ get_ticks_per_sec();
232 static const VMStateDescription vmstate_pl031
= {
235 .minimum_version_id
= 1,
236 .pre_save
= pl031_pre_save
,
237 .post_load
= pl031_post_load
,
238 .fields
= (VMStateField
[]) {
239 VMSTATE_UINT32(tick_offset_vmstate
, PL031State
),
240 VMSTATE_UINT32(mr
, PL031State
),
241 VMSTATE_UINT32(lr
, PL031State
),
242 VMSTATE_UINT32(cr
, PL031State
),
243 VMSTATE_UINT32(im
, PL031State
),
244 VMSTATE_UINT32(is
, PL031State
),
245 VMSTATE_END_OF_LIST()
249 static void pl031_class_init(ObjectClass
*klass
, void *data
)
251 DeviceClass
*dc
= DEVICE_CLASS(klass
);
252 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
254 k
->init
= pl031_init
;
255 dc
->vmsd
= &vmstate_pl031
;
258 static const TypeInfo pl031_info
= {
260 .parent
= TYPE_SYS_BUS_DEVICE
,
261 .instance_size
= sizeof(PL031State
),
262 .class_init
= pl031_class_init
,
265 static void pl031_register_types(void)
267 type_register_static(&pl031_info
);
270 type_init(pl031_register_types
)