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 "primecell.h"
14 #include "qemu-timer.h"
20 #define DPRINTF(fmt, args...) \
21 do { printf("pl031: " fmt , ##args); } while (0)
23 #define DPRINTF(fmt, args...) 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 */
49 static const unsigned char pl031_id
[] = {
50 0x31, 0x10, 0x14, 0x00, /* Device ID */
51 0x0d, 0xf0, 0x05, 0xb1 /* Cell ID */
54 static void pl031_update(pl031_state
*s
)
56 qemu_set_irq(s
->irq
, s
->is
& s
->im
);
59 static void pl031_interrupt(void * opaque
)
61 pl031_state
*s
= (pl031_state
*)opaque
;
64 DPRINTF("Alarm raised\n");
68 static uint32_t pl031_get_count(pl031_state
*s
)
70 /* This assumes qemu_get_clock returns the time since the machine was
72 return s
->tick_offset
+ qemu_get_clock(vm_clock
) / ticks_per_sec
;
75 static void pl031_set_alarm(pl031_state
*s
)
80 now
= qemu_get_clock(vm_clock
);
81 ticks
= s
->tick_offset
+ now
/ ticks_per_sec
;
83 /* The timer wraps around. This subtraction also wraps in the same way,
84 and gives correct results when alarm < now_ticks. */
85 ticks
= s
->mr
- ticks
;
86 DPRINTF("Alarm set in %ud ticks\n", ticks
);
88 qemu_del_timer(s
->timer
);
91 qemu_mod_timer(s
->timer
, now
+ (int64_t)ticks
* ticks_per_sec
);
95 static uint32_t pl031_read(void *opaque
, target_phys_addr_t offset
)
97 pl031_state
*s
= (pl031_state
*)opaque
;
99 if (offset
>= 0xfe0 && offset
< 0x1000)
100 return pl031_id
[(offset
- 0xfe0) >> 2];
104 return pl031_get_count(s
);
114 /* RTC is permanently enabled. */
117 return s
->is
& s
->im
;
119 fprintf(stderr
, "qemu: pl031_read: Unexpected offset 0x%x\n",
123 cpu_abort(cpu_single_env
, "pl031_read: Bad offset 0x%x\n",
131 static void pl031_write(void * opaque
, target_phys_addr_t offset
,
134 pl031_state
*s
= (pl031_state
*)opaque
;
139 s
->tick_offset
+= value
- pl031_get_count(s
);
148 DPRINTF("Interrupt mask %d\n", s
->im
);
152 /* The PL031 documentation (DDI0224B) states that the interupt is
153 cleared when bit 0 of the written value is set. However the
154 arm926e documentation (DDI0287B) states that the interrupt is
155 cleared when any value is written. */
156 DPRINTF("Interrupt cleared");
161 /* Written value is ignored. */
167 fprintf(stderr
, "qemu: pl031_write: Unexpected offset 0x%x\n",
172 cpu_abort(cpu_single_env
, "pl031_write: Bad offset 0x%x\n",
178 static CPUWriteMemoryFunc
* pl031_writefn
[] = {
184 static CPUReadMemoryFunc
* pl031_readfn
[] = {
190 void pl031_init(uint32_t base
, qemu_irq irq
)
196 s
= qemu_mallocz(sizeof(pl031_state
));
198 cpu_abort(cpu_single_env
, "pl031_init: Out of memory\n");
200 iomemtype
= cpu_register_io_memory(0, pl031_readfn
, pl031_writefn
, s
);
202 cpu_abort(cpu_single_env
, "pl031_init: Can't register I/O memory\n");
204 cpu_register_physical_memory(base
, 0x00001000, iomemtype
);
207 /* ??? We assume vm_clock is zero at this point. */
208 qemu_get_timedate(&tm
, 0);
209 s
->tick_offset
= mktimegm(&tm
);
211 s
->timer
= qemu_new_timer(vm_clock
, pl031_interrupt
, s
);