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 unsigned char pl031_id
[] = {
48 0x31, 0x10, 0x14, 0x00, /* Device ID */
49 0x0d, 0xf0, 0x05, 0xb1 /* Cell ID */
52 static void pl031_update(pl031_state
*s
)
54 qemu_set_irq(s
->irq
, s
->is
& s
->im
);
57 static void pl031_interrupt(void * opaque
)
59 pl031_state
*s
= (pl031_state
*)opaque
;
62 DPRINTF("Alarm raised\n");
66 static uint32_t pl031_get_count(pl031_state
*s
)
68 /* This assumes qemu_get_clock returns the time since the machine was
70 return s
->tick_offset
+ qemu_get_clock(vm_clock
) / get_ticks_per_sec();
73 static void pl031_set_alarm(pl031_state
*s
)
78 now
= qemu_get_clock(vm_clock
);
79 ticks
= s
->tick_offset
+ now
/ get_ticks_per_sec();
81 /* The timer wraps around. This subtraction also wraps in the same way,
82 and gives correct results when alarm < now_ticks. */
83 ticks
= s
->mr
- ticks
;
84 DPRINTF("Alarm set in %ud ticks\n", ticks
);
86 qemu_del_timer(s
->timer
);
89 qemu_mod_timer(s
->timer
, now
+ (int64_t)ticks
* get_ticks_per_sec());
93 static uint32_t pl031_read(void *opaque
, target_phys_addr_t offset
)
95 pl031_state
*s
= (pl031_state
*)opaque
;
97 if (offset
>= 0xfe0 && offset
< 0x1000)
98 return pl031_id
[(offset
- 0xfe0) >> 2];
102 return pl031_get_count(s
);
112 /* RTC is permanently enabled. */
115 return s
->is
& s
->im
;
117 fprintf(stderr
, "qemu: pl031_read: Unexpected offset 0x%x\n",
121 hw_error("pl031_read: Bad offset 0x%x\n", (int)offset
);
128 static void pl031_write(void * opaque
, target_phys_addr_t offset
,
131 pl031_state
*s
= (pl031_state
*)opaque
;
136 s
->tick_offset
+= value
- pl031_get_count(s
);
145 DPRINTF("Interrupt mask %d\n", s
->im
);
149 /* The PL031 documentation (DDI0224B) states that the interupt is
150 cleared when bit 0 of the written value is set. However the
151 arm926e documentation (DDI0287B) states that the interrupt is
152 cleared when any value is written. */
153 DPRINTF("Interrupt cleared");
158 /* Written value is ignored. */
164 fprintf(stderr
, "qemu: pl031_write: Unexpected offset 0x%x\n",
169 hw_error("pl031_write: Bad offset 0x%x\n", (int)offset
);
174 static CPUWriteMemoryFunc
* const pl031_writefn
[] = {
180 static CPUReadMemoryFunc
* const pl031_readfn
[] = {
186 static int pl031_init(SysBusDevice
*dev
)
189 pl031_state
*s
= FROM_SYSBUS(pl031_state
, dev
);
192 iomemtype
= cpu_register_io_memory(pl031_readfn
, pl031_writefn
, s
);
193 if (iomemtype
== -1) {
194 hw_error("pl031_init: Can't register I/O memory\n");
197 sysbus_init_mmio(dev
, 0x1000, iomemtype
);
199 sysbus_init_irq(dev
, &s
->irq
);
200 /* ??? We assume vm_clock is zero at this point. */
201 qemu_get_timedate(&tm
, 0);
202 s
->tick_offset
= mktimegm(&tm
);
204 s
->timer
= qemu_new_timer(vm_clock
, pl031_interrupt
, s
);
208 static void pl031_register_devices(void)
210 sysbus_register_dev("pl031", sizeof(pl031_state
), pl031_init
);
213 device_init(pl031_register_devices
)