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.
15 #include "qemu-timer.h"
20 #define DPRINTF(fmt, ...) \
21 do { printf("pl031: " fmt , ## __VA_ARGS__); } while (0)
23 #define DPRINTF(fmt, ...) do {} while(0)
26 #define RTC_DR 0x00 /* Data read register */
27 #define RTC_MR 0x04 /* Match register */
28 #define RTC_LR 0x08 /* Data load register */
29 #define RTC_CR 0x0c /* Control register */
30 #define RTC_IMSC 0x10 /* Interrupt mask and set register */
31 #define RTC_RIS 0x14 /* Raw interrupt status register */
32 #define RTC_MIS 0x18 /* Masked interrupt status register */
33 #define RTC_ICR 0x1c /* Interrupt clear register */
50 static const VMStateDescription vmstate_pl031
= {
53 .minimum_version_id
= 1,
54 .fields
= (VMStateField
[]) {
55 VMSTATE_UINT32(tick_offset
, pl031_state
),
56 VMSTATE_UINT32(mr
, pl031_state
),
57 VMSTATE_UINT32(lr
, pl031_state
),
58 VMSTATE_UINT32(cr
, pl031_state
),
59 VMSTATE_UINT32(im
, pl031_state
),
60 VMSTATE_UINT32(is
, pl031_state
),
65 static const unsigned char pl031_id
[] = {
66 0x31, 0x10, 0x14, 0x00, /* Device ID */
67 0x0d, 0xf0, 0x05, 0xb1 /* Cell ID */
70 static void pl031_update(pl031_state
*s
)
72 qemu_set_irq(s
->irq
, s
->is
& s
->im
);
75 static void pl031_interrupt(void * opaque
)
77 pl031_state
*s
= (pl031_state
*)opaque
;
80 DPRINTF("Alarm raised\n");
84 static uint32_t pl031_get_count(pl031_state
*s
)
86 /* This assumes qemu_get_clock_ns returns the time since the machine was
88 return s
->tick_offset
+ qemu_get_clock_ns(vm_clock
) / get_ticks_per_sec();
91 static void pl031_set_alarm(pl031_state
*s
)
96 now
= qemu_get_clock_ns(vm_clock
);
97 ticks
= s
->tick_offset
+ now
/ get_ticks_per_sec();
99 /* The timer wraps around. This subtraction also wraps in the same way,
100 and gives correct results when alarm < now_ticks. */
101 ticks
= s
->mr
- ticks
;
102 DPRINTF("Alarm set in %ud ticks\n", ticks
);
104 qemu_del_timer(s
->timer
);
107 qemu_mod_timer(s
->timer
, now
+ (int64_t)ticks
* get_ticks_per_sec());
111 static uint64_t pl031_read(void *opaque
, target_phys_addr_t offset
,
114 pl031_state
*s
= (pl031_state
*)opaque
;
116 if (offset
>= 0xfe0 && offset
< 0x1000)
117 return pl031_id
[(offset
- 0xfe0) >> 2];
121 return pl031_get_count(s
);
131 /* RTC is permanently enabled. */
134 return s
->is
& s
->im
;
136 fprintf(stderr
, "qemu: pl031_read: Unexpected offset 0x%x\n",
140 hw_error("pl031_read: Bad offset 0x%x\n", (int)offset
);
147 static void pl031_write(void * opaque
, target_phys_addr_t offset
,
148 uint64_t value
, unsigned size
)
150 pl031_state
*s
= (pl031_state
*)opaque
;
155 s
->tick_offset
+= value
- pl031_get_count(s
);
164 DPRINTF("Interrupt mask %d\n", s
->im
);
168 /* The PL031 documentation (DDI0224B) states that the interrupt is
169 cleared when bit 0 of the written value is set. However the
170 arm926e documentation (DDI0287B) states that the interrupt is
171 cleared when any value is written. */
172 DPRINTF("Interrupt cleared");
177 /* Written value is ignored. */
183 fprintf(stderr
, "qemu: pl031_write: Unexpected offset 0x%x\n",
188 hw_error("pl031_write: Bad offset 0x%x\n", (int)offset
);
193 static const MemoryRegionOps pl031_ops
= {
195 .write
= pl031_write
,
196 .endianness
= DEVICE_NATIVE_ENDIAN
,
199 static int pl031_init(SysBusDevice
*dev
)
201 pl031_state
*s
= FROM_SYSBUS(pl031_state
, dev
);
204 memory_region_init_io(&s
->iomem
, &pl031_ops
, s
, "pl031", 0x1000);
205 sysbus_init_mmio(dev
, &s
->iomem
);
207 sysbus_init_irq(dev
, &s
->irq
);
208 /* ??? We assume vm_clock is zero at this point. */
209 qemu_get_timedate(&tm
, 0);
210 s
->tick_offset
= mktimegm(&tm
);
212 s
->timer
= qemu_new_timer_ns(vm_clock
, pl031_interrupt
, s
);
216 static void pl031_class_init(ObjectClass
*klass
, void *data
)
218 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
220 k
->init
= pl031_init
;
223 static DeviceInfo pl031_info
= {
225 .size
= sizeof(pl031_state
),
226 .vmsd
= &vmstate_pl031
,
228 .class_init
= pl031_class_init
,
231 static void pl031_register_devices(void)
233 sysbus_register_withprop(&pl031_info
);
236 device_init(pl031_register_devices
)