root / hw / pl031.c @ 067d01de
History | View | Annotate | Download (5.2 kB)
1 | 7e1543c2 | pbrook | /*
|
---|---|---|---|
2 | 7e1543c2 | pbrook | * ARM AMBA PrimeCell PL031 RTC
|
3 | 7e1543c2 | pbrook | *
|
4 | 7e1543c2 | pbrook | * Copyright (c) 2007 CodeSourcery
|
5 | 7e1543c2 | pbrook | *
|
6 | 7e1543c2 | pbrook | * This file is free software; you can redistribute it and/or modify
|
7 | 7e1543c2 | pbrook | * it under the terms of the GNU General Public License version 2 as
|
8 | 7e1543c2 | pbrook | * published by the Free Software Foundation.
|
9 | 7e1543c2 | pbrook | *
|
10 | 7e1543c2 | pbrook | */
|
11 | 7e1543c2 | pbrook | |
12 | a63bdb31 | Paul Brook | #include "sysbus.h" |
13 | 87ecb68b | pbrook | #include "qemu-timer.h" |
14 | 7e1543c2 | pbrook | |
15 | 7e1543c2 | pbrook | //#define DEBUG_PL031
|
16 | 7e1543c2 | pbrook | |
17 | 7e1543c2 | pbrook | #ifdef DEBUG_PL031
|
18 | 001faf32 | Blue Swirl | #define DPRINTF(fmt, ...) \
|
19 | 001faf32 | Blue Swirl | do { printf("pl031: " fmt , ## __VA_ARGS__); } while (0) |
20 | 7e1543c2 | pbrook | #else
|
21 | 001faf32 | Blue Swirl | #define DPRINTF(fmt, ...) do {} while(0) |
22 | 7e1543c2 | pbrook | #endif
|
23 | 7e1543c2 | pbrook | |
24 | 7e1543c2 | pbrook | #define RTC_DR 0x00 /* Data read register */ |
25 | 7e1543c2 | pbrook | #define RTC_MR 0x04 /* Match register */ |
26 | 7e1543c2 | pbrook | #define RTC_LR 0x08 /* Data load register */ |
27 | 7e1543c2 | pbrook | #define RTC_CR 0x0c /* Control register */ |
28 | 7e1543c2 | pbrook | #define RTC_IMSC 0x10 /* Interrupt mask and set register */ |
29 | 7e1543c2 | pbrook | #define RTC_RIS 0x14 /* Raw interrupt status register */ |
30 | 7e1543c2 | pbrook | #define RTC_MIS 0x18 /* Masked interrupt status register */ |
31 | 7e1543c2 | pbrook | #define RTC_ICR 0x1c /* Interrupt clear register */ |
32 | 7e1543c2 | pbrook | |
33 | 7e1543c2 | pbrook | typedef struct { |
34 | a63bdb31 | Paul Brook | SysBusDevice busdev; |
35 | 7e1543c2 | pbrook | QEMUTimer *timer; |
36 | 7e1543c2 | pbrook | qemu_irq irq; |
37 | 7e1543c2 | pbrook | |
38 | 7e1543c2 | pbrook | uint32_t tick_offset; |
39 | 7e1543c2 | pbrook | |
40 | 7e1543c2 | pbrook | uint32_t mr; |
41 | 7e1543c2 | pbrook | uint32_t lr; |
42 | 7e1543c2 | pbrook | uint32_t cr; |
43 | 7e1543c2 | pbrook | uint32_t im; |
44 | 7e1543c2 | pbrook | uint32_t is; |
45 | 7e1543c2 | pbrook | } pl031_state; |
46 | 7e1543c2 | pbrook | |
47 | 7e1543c2 | pbrook | static const unsigned char pl031_id[] = { |
48 | 7e1543c2 | pbrook | 0x31, 0x10, 0x14, 0x00, /* Device ID */ |
49 | 7e1543c2 | pbrook | 0x0d, 0xf0, 0x05, 0xb1 /* Cell ID */ |
50 | 7e1543c2 | pbrook | }; |
51 | 7e1543c2 | pbrook | |
52 | 7e1543c2 | pbrook | static void pl031_update(pl031_state *s) |
53 | 7e1543c2 | pbrook | { |
54 | 7e1543c2 | pbrook | qemu_set_irq(s->irq, s->is & s->im); |
55 | 7e1543c2 | pbrook | } |
56 | 7e1543c2 | pbrook | |
57 | 7e1543c2 | pbrook | static void pl031_interrupt(void * opaque) |
58 | 7e1543c2 | pbrook | { |
59 | 7e1543c2 | pbrook | pl031_state *s = (pl031_state *)opaque; |
60 | 7e1543c2 | pbrook | |
61 | 7e1543c2 | pbrook | s->im = 1;
|
62 | 7e1543c2 | pbrook | DPRINTF("Alarm raised\n");
|
63 | 7e1543c2 | pbrook | pl031_update(s); |
64 | 7e1543c2 | pbrook | } |
65 | 7e1543c2 | pbrook | |
66 | 7e1543c2 | pbrook | static uint32_t pl031_get_count(pl031_state *s)
|
67 | 7e1543c2 | pbrook | { |
68 | 7e1543c2 | pbrook | /* This assumes qemu_get_clock returns the time since the machine was
|
69 | 7e1543c2 | pbrook | created. */
|
70 | 6ee093c9 | Juan Quintela | return s->tick_offset + qemu_get_clock(vm_clock) / get_ticks_per_sec();
|
71 | 7e1543c2 | pbrook | } |
72 | 7e1543c2 | pbrook | |
73 | 7e1543c2 | pbrook | static void pl031_set_alarm(pl031_state *s) |
74 | 7e1543c2 | pbrook | { |
75 | 7e1543c2 | pbrook | int64_t now; |
76 | 7e1543c2 | pbrook | uint32_t ticks; |
77 | 7e1543c2 | pbrook | |
78 | 7e1543c2 | pbrook | now = qemu_get_clock(vm_clock); |
79 | 6ee093c9 | Juan Quintela | ticks = s->tick_offset + now / get_ticks_per_sec(); |
80 | 7e1543c2 | pbrook | |
81 | 7e1543c2 | pbrook | /* The timer wraps around. This subtraction also wraps in the same way,
|
82 | 7e1543c2 | pbrook | and gives correct results when alarm < now_ticks. */
|
83 | 7e1543c2 | pbrook | ticks = s->mr - ticks; |
84 | 7e1543c2 | pbrook | DPRINTF("Alarm set in %ud ticks\n", ticks);
|
85 | 7e1543c2 | pbrook | if (ticks == 0) { |
86 | 7e1543c2 | pbrook | qemu_del_timer(s->timer); |
87 | 7e1543c2 | pbrook | pl031_interrupt(s); |
88 | 7e1543c2 | pbrook | } else {
|
89 | 6ee093c9 | Juan Quintela | qemu_mod_timer(s->timer, now + (int64_t)ticks * get_ticks_per_sec()); |
90 | 7e1543c2 | pbrook | } |
91 | 7e1543c2 | pbrook | } |
92 | 7e1543c2 | pbrook | |
93 | 7e1543c2 | pbrook | static uint32_t pl031_read(void *opaque, target_phys_addr_t offset) |
94 | 7e1543c2 | pbrook | { |
95 | 7e1543c2 | pbrook | pl031_state *s = (pl031_state *)opaque; |
96 | 7e1543c2 | pbrook | |
97 | 7e1543c2 | pbrook | if (offset >= 0xfe0 && offset < 0x1000) |
98 | 7e1543c2 | pbrook | return pl031_id[(offset - 0xfe0) >> 2]; |
99 | 7e1543c2 | pbrook | |
100 | 7e1543c2 | pbrook | switch (offset) {
|
101 | 7e1543c2 | pbrook | case RTC_DR:
|
102 | 7e1543c2 | pbrook | return pl031_get_count(s);
|
103 | 7e1543c2 | pbrook | case RTC_MR:
|
104 | 7e1543c2 | pbrook | return s->mr;
|
105 | 7e1543c2 | pbrook | case RTC_IMSC:
|
106 | 7e1543c2 | pbrook | return s->im;
|
107 | 7e1543c2 | pbrook | case RTC_RIS:
|
108 | 7e1543c2 | pbrook | return s->is;
|
109 | 7e1543c2 | pbrook | case RTC_LR:
|
110 | 7e1543c2 | pbrook | return s->lr;
|
111 | 7e1543c2 | pbrook | case RTC_CR:
|
112 | 7e1543c2 | pbrook | /* RTC is permanently enabled. */
|
113 | 7e1543c2 | pbrook | return 1; |
114 | 7e1543c2 | pbrook | case RTC_MIS:
|
115 | 7e1543c2 | pbrook | return s->is & s->im;
|
116 | 7e1543c2 | pbrook | case RTC_ICR:
|
117 | 7e1543c2 | pbrook | fprintf(stderr, "qemu: pl031_read: Unexpected offset 0x%x\n",
|
118 | 7e1543c2 | pbrook | (int)offset);
|
119 | 7e1543c2 | pbrook | break;
|
120 | 7e1543c2 | pbrook | default:
|
121 | 2ac71179 | Paul Brook | hw_error("pl031_read: Bad offset 0x%x\n", (int)offset); |
122 | 7e1543c2 | pbrook | break;
|
123 | 7e1543c2 | pbrook | } |
124 | 7e1543c2 | pbrook | |
125 | 7e1543c2 | pbrook | return 0; |
126 | 7e1543c2 | pbrook | } |
127 | 7e1543c2 | pbrook | |
128 | 7e1543c2 | pbrook | static void pl031_write(void * opaque, target_phys_addr_t offset, |
129 | 7e1543c2 | pbrook | uint32_t value) |
130 | 7e1543c2 | pbrook | { |
131 | 7e1543c2 | pbrook | pl031_state *s = (pl031_state *)opaque; |
132 | 7e1543c2 | pbrook | |
133 | 7e1543c2 | pbrook | |
134 | 7e1543c2 | pbrook | switch (offset) {
|
135 | 7e1543c2 | pbrook | case RTC_LR:
|
136 | 7e1543c2 | pbrook | s->tick_offset += value - pl031_get_count(s); |
137 | 7e1543c2 | pbrook | pl031_set_alarm(s); |
138 | 7e1543c2 | pbrook | break;
|
139 | 7e1543c2 | pbrook | case RTC_MR:
|
140 | 7e1543c2 | pbrook | s->mr = value; |
141 | 7e1543c2 | pbrook | pl031_set_alarm(s); |
142 | 7e1543c2 | pbrook | break;
|
143 | 7e1543c2 | pbrook | case RTC_IMSC:
|
144 | 7e1543c2 | pbrook | s->im = value & 1;
|
145 | 7e1543c2 | pbrook | DPRINTF("Interrupt mask %d\n", s->im);
|
146 | 7e1543c2 | pbrook | pl031_update(s); |
147 | 7e1543c2 | pbrook | break;
|
148 | 7e1543c2 | pbrook | case RTC_ICR:
|
149 | 7e1543c2 | pbrook | /* The PL031 documentation (DDI0224B) states that the interupt is
|
150 | 7e1543c2 | pbrook | cleared when bit 0 of the written value is set. However the
|
151 | 7e1543c2 | pbrook | arm926e documentation (DDI0287B) states that the interrupt is
|
152 | 7e1543c2 | pbrook | cleared when any value is written. */
|
153 | 7e1543c2 | pbrook | DPRINTF("Interrupt cleared");
|
154 | 7e1543c2 | pbrook | s->is = 0;
|
155 | 7e1543c2 | pbrook | pl031_update(s); |
156 | 7e1543c2 | pbrook | break;
|
157 | 7e1543c2 | pbrook | case RTC_CR:
|
158 | 7e1543c2 | pbrook | /* Written value is ignored. */
|
159 | 7e1543c2 | pbrook | break;
|
160 | 7e1543c2 | pbrook | |
161 | 7e1543c2 | pbrook | case RTC_DR:
|
162 | 7e1543c2 | pbrook | case RTC_MIS:
|
163 | 7e1543c2 | pbrook | case RTC_RIS:
|
164 | 7e1543c2 | pbrook | fprintf(stderr, "qemu: pl031_write: Unexpected offset 0x%x\n",
|
165 | 7e1543c2 | pbrook | (int)offset);
|
166 | 7e1543c2 | pbrook | break;
|
167 | 7e1543c2 | pbrook | |
168 | 7e1543c2 | pbrook | default:
|
169 | 2ac71179 | Paul Brook | hw_error("pl031_write: Bad offset 0x%x\n", (int)offset); |
170 | 7e1543c2 | pbrook | break;
|
171 | 7e1543c2 | pbrook | } |
172 | 7e1543c2 | pbrook | } |
173 | 7e1543c2 | pbrook | |
174 | d60efc6b | Blue Swirl | static CPUWriteMemoryFunc * const pl031_writefn[] = { |
175 | 7e1543c2 | pbrook | pl031_write, |
176 | 7e1543c2 | pbrook | pl031_write, |
177 | 7e1543c2 | pbrook | pl031_write |
178 | 7e1543c2 | pbrook | }; |
179 | 7e1543c2 | pbrook | |
180 | d60efc6b | Blue Swirl | static CPUReadMemoryFunc * const pl031_readfn[] = { |
181 | 7e1543c2 | pbrook | pl031_read, |
182 | 7e1543c2 | pbrook | pl031_read, |
183 | 7e1543c2 | pbrook | pl031_read |
184 | 7e1543c2 | pbrook | }; |
185 | 7e1543c2 | pbrook | |
186 | 81a322d4 | Gerd Hoffmann | static int pl031_init(SysBusDevice *dev) |
187 | 7e1543c2 | pbrook | { |
188 | 7e1543c2 | pbrook | int iomemtype;
|
189 | a63bdb31 | Paul Brook | pl031_state *s = FROM_SYSBUS(pl031_state, dev); |
190 | f6503059 | balrog | struct tm tm;
|
191 | 7e1543c2 | pbrook | |
192 | 1eed09cb | Avi Kivity | iomemtype = cpu_register_io_memory(pl031_readfn, pl031_writefn, s); |
193 | 2ac71179 | Paul Brook | if (iomemtype == -1) { |
194 | 2ac71179 | Paul Brook | hw_error("pl031_init: Can't register I/O memory\n");
|
195 | 2ac71179 | Paul Brook | } |
196 | 7e1543c2 | pbrook | |
197 | a63bdb31 | Paul Brook | sysbus_init_mmio(dev, 0x1000, iomemtype);
|
198 | 7e1543c2 | pbrook | |
199 | a63bdb31 | Paul Brook | sysbus_init_irq(dev, &s->irq); |
200 | 7e1543c2 | pbrook | /* ??? We assume vm_clock is zero at this point. */
|
201 | f6503059 | balrog | qemu_get_timedate(&tm, 0);
|
202 | 0cd2df75 | aurel32 | s->tick_offset = mktimegm(&tm); |
203 | 7e1543c2 | pbrook | |
204 | 7e1543c2 | pbrook | s->timer = qemu_new_timer(vm_clock, pl031_interrupt, s); |
205 | 81a322d4 | Gerd Hoffmann | return 0; |
206 | 7e1543c2 | pbrook | } |
207 | a63bdb31 | Paul Brook | |
208 | a63bdb31 | Paul Brook | static void pl031_register_devices(void) |
209 | a63bdb31 | Paul Brook | { |
210 | a63bdb31 | Paul Brook | sysbus_register_dev("pl031", sizeof(pl031_state), pl031_init); |
211 | a63bdb31 | Paul Brook | } |
212 | a63bdb31 | Paul Brook | |
213 | a63bdb31 | Paul Brook | device_init(pl031_register_devices) |