root / hw / mc146818rtc.c @ 78971d57
History | View | Annotate | Download (21.3 kB)
1 | 80cabfad | bellard | /*
|
---|---|---|---|
2 | 80cabfad | bellard | * QEMU MC146818 RTC emulation
|
3 | 5fafdf24 | ths | *
|
4 | 80cabfad | bellard | * Copyright (c) 2003-2004 Fabrice Bellard
|
5 | 5fafdf24 | ths | *
|
6 | 80cabfad | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 80cabfad | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 80cabfad | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 80cabfad | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 80cabfad | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 80cabfad | bellard | * furnished to do so, subject to the following conditions:
|
12 | 80cabfad | bellard | *
|
13 | 80cabfad | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 80cabfad | bellard | * all copies or substantial portions of the Software.
|
15 | 80cabfad | bellard | *
|
16 | 80cabfad | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 80cabfad | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 80cabfad | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 80cabfad | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 80cabfad | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 80cabfad | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 80cabfad | bellard | * THE SOFTWARE.
|
23 | 80cabfad | bellard | */
|
24 | 87ecb68b | pbrook | #include "hw.h" |
25 | 87ecb68b | pbrook | #include "qemu-timer.h" |
26 | 87ecb68b | pbrook | #include "sysemu.h" |
27 | 87ecb68b | pbrook | #include "pc.h" |
28 | 87ecb68b | pbrook | #include "isa.h" |
29 | 16b29ae1 | aliguori | #include "hpet_emul.h" |
30 | 80cabfad | bellard | |
31 | 80cabfad | bellard | //#define DEBUG_CMOS
|
32 | 80cabfad | bellard | |
33 | 80cabfad | bellard | #define RTC_SECONDS 0 |
34 | 80cabfad | bellard | #define RTC_SECONDS_ALARM 1 |
35 | 80cabfad | bellard | #define RTC_MINUTES 2 |
36 | 80cabfad | bellard | #define RTC_MINUTES_ALARM 3 |
37 | 80cabfad | bellard | #define RTC_HOURS 4 |
38 | 80cabfad | bellard | #define RTC_HOURS_ALARM 5 |
39 | 80cabfad | bellard | #define RTC_ALARM_DONT_CARE 0xC0 |
40 | 80cabfad | bellard | |
41 | 80cabfad | bellard | #define RTC_DAY_OF_WEEK 6 |
42 | 80cabfad | bellard | #define RTC_DAY_OF_MONTH 7 |
43 | 80cabfad | bellard | #define RTC_MONTH 8 |
44 | 80cabfad | bellard | #define RTC_YEAR 9 |
45 | 80cabfad | bellard | |
46 | 80cabfad | bellard | #define RTC_REG_A 10 |
47 | 80cabfad | bellard | #define RTC_REG_B 11 |
48 | 80cabfad | bellard | #define RTC_REG_C 12 |
49 | 80cabfad | bellard | #define RTC_REG_D 13 |
50 | 80cabfad | bellard | |
51 | dff38e7b | bellard | #define REG_A_UIP 0x80 |
52 | 80cabfad | bellard | |
53 | 100d9891 | aurel32 | #define REG_B_SET 0x80 |
54 | 100d9891 | aurel32 | #define REG_B_PIE 0x40 |
55 | 100d9891 | aurel32 | #define REG_B_AIE 0x20 |
56 | 100d9891 | aurel32 | #define REG_B_UIE 0x10 |
57 | 100d9891 | aurel32 | #define REG_B_SQWE 0x08 |
58 | 100d9891 | aurel32 | #define REG_B_DM 0x04 |
59 | dff38e7b | bellard | |
60 | 72716184 | Anthony Liguori | #define REG_C_UF 0x10 |
61 | 72716184 | Anthony Liguori | #define REG_C_IRQF 0x80 |
62 | 72716184 | Anthony Liguori | #define REG_C_PF 0x40 |
63 | 72716184 | Anthony Liguori | #define REG_C_AF 0x20 |
64 | 72716184 | Anthony Liguori | |
65 | dff38e7b | bellard | struct RTCState {
|
66 | 32e0c826 | Gerd Hoffmann | ISADevice dev; |
67 | dff38e7b | bellard | uint8_t cmos_data[128];
|
68 | dff38e7b | bellard | uint8_t cmos_index; |
69 | 43f493af | bellard | struct tm current_tm;
|
70 | 32e0c826 | Gerd Hoffmann | int32_t base_year; |
71 | d537cf6c | pbrook | qemu_irq irq; |
72 | 100d9891 | aurel32 | qemu_irq sqw_irq; |
73 | 18c6e2ff | ths | int it_shift;
|
74 | dff38e7b | bellard | /* periodic timer */
|
75 | dff38e7b | bellard | QEMUTimer *periodic_timer; |
76 | dff38e7b | bellard | int64_t next_periodic_time; |
77 | dff38e7b | bellard | /* second update */
|
78 | dff38e7b | bellard | int64_t next_second_time; |
79 | 73822ec8 | aliguori | #ifdef TARGET_I386
|
80 | 73822ec8 | aliguori | uint32_t irq_coalesced; |
81 | 73822ec8 | aliguori | uint32_t period; |
82 | 93b66569 | aliguori | QEMUTimer *coalesced_timer; |
83 | 73822ec8 | aliguori | #endif
|
84 | dff38e7b | bellard | QEMUTimer *second_timer; |
85 | dff38e7b | bellard | QEMUTimer *second_timer2; |
86 | dff38e7b | bellard | }; |
87 | dff38e7b | bellard | |
88 | 16b29ae1 | aliguori | static void rtc_irq_raise(qemu_irq irq) { |
89 | c50c2d68 | aurel32 | /* When HPET is operating in legacy mode, RTC interrupts are disabled
|
90 | 16b29ae1 | aliguori | * We block qemu_irq_raise, but not qemu_irq_lower, in case legacy
|
91 | c50c2d68 | aurel32 | * mode is established while interrupt is raised. We want it to
|
92 | 16b29ae1 | aliguori | * be lowered in any case
|
93 | c50c2d68 | aurel32 | */
|
94 | ce88f890 | Juan Quintela | #if defined TARGET_I386
|
95 | c50c2d68 | aurel32 | if (!hpet_in_legacy_mode())
|
96 | 16b29ae1 | aliguori | #endif
|
97 | 16b29ae1 | aliguori | qemu_irq_raise(irq); |
98 | 16b29ae1 | aliguori | } |
99 | 16b29ae1 | aliguori | |
100 | dff38e7b | bellard | static void rtc_set_time(RTCState *s); |
101 | dff38e7b | bellard | static void rtc_copy_date(RTCState *s); |
102 | dff38e7b | bellard | |
103 | 93b66569 | aliguori | #ifdef TARGET_I386
|
104 | 93b66569 | aliguori | static void rtc_coalesced_timer_update(RTCState *s) |
105 | 93b66569 | aliguori | { |
106 | 93b66569 | aliguori | if (s->irq_coalesced == 0) { |
107 | 93b66569 | aliguori | qemu_del_timer(s->coalesced_timer); |
108 | 93b66569 | aliguori | } else {
|
109 | 93b66569 | aliguori | /* divide each RTC interval to 2 - 8 smaller intervals */
|
110 | 93b66569 | aliguori | int c = MIN(s->irq_coalesced, 7) + 1; |
111 | 6875204c | Jan Kiszka | int64_t next_clock = qemu_get_clock(rtc_clock) + |
112 | 6875204c | Jan Kiszka | muldiv64(s->period / c, get_ticks_per_sec(), 32768);
|
113 | 93b66569 | aliguori | qemu_mod_timer(s->coalesced_timer, next_clock); |
114 | 93b66569 | aliguori | } |
115 | 93b66569 | aliguori | } |
116 | 93b66569 | aliguori | |
117 | 93b66569 | aliguori | static void rtc_coalesced_timer(void *opaque) |
118 | 93b66569 | aliguori | { |
119 | 93b66569 | aliguori | RTCState *s = opaque; |
120 | 93b66569 | aliguori | |
121 | 93b66569 | aliguori | if (s->irq_coalesced != 0) { |
122 | 93b66569 | aliguori | apic_reset_irq_delivered(); |
123 | 93b66569 | aliguori | s->cmos_data[RTC_REG_C] |= 0xc0;
|
124 | 93b66569 | aliguori | rtc_irq_raise(s->irq); |
125 | 93b66569 | aliguori | if (apic_get_irq_delivered()) {
|
126 | 93b66569 | aliguori | s->irq_coalesced--; |
127 | 93b66569 | aliguori | } |
128 | 93b66569 | aliguori | } |
129 | 93b66569 | aliguori | |
130 | 93b66569 | aliguori | rtc_coalesced_timer_update(s); |
131 | 93b66569 | aliguori | } |
132 | 93b66569 | aliguori | #endif
|
133 | 93b66569 | aliguori | |
134 | dff38e7b | bellard | static void rtc_timer_update(RTCState *s, int64_t current_time) |
135 | dff38e7b | bellard | { |
136 | dff38e7b | bellard | int period_code, period;
|
137 | dff38e7b | bellard | int64_t cur_clock, next_irq_clock; |
138 | 100d9891 | aurel32 | int enable_pie;
|
139 | dff38e7b | bellard | |
140 | dff38e7b | bellard | period_code = s->cmos_data[RTC_REG_A] & 0x0f;
|
141 | ce88f890 | Juan Quintela | #if defined TARGET_I386
|
142 | c50c2d68 | aurel32 | /* disable periodic timer if hpet is in legacy mode, since interrupts are
|
143 | 16b29ae1 | aliguori | * disabled anyway.
|
144 | 16b29ae1 | aliguori | */
|
145 | a8b01dd8 | pbrook | enable_pie = !hpet_in_legacy_mode(); |
146 | 16b29ae1 | aliguori | #else
|
147 | 100d9891 | aurel32 | enable_pie = 1;
|
148 | 16b29ae1 | aliguori | #endif
|
149 | 100d9891 | aurel32 | if (period_code != 0 |
150 | 100d9891 | aurel32 | && (((s->cmos_data[RTC_REG_B] & REG_B_PIE) && enable_pie) |
151 | 100d9891 | aurel32 | || ((s->cmos_data[RTC_REG_B] & REG_B_SQWE) && s->sqw_irq))) { |
152 | dff38e7b | bellard | if (period_code <= 2) |
153 | dff38e7b | bellard | period_code += 7;
|
154 | dff38e7b | bellard | /* period in 32 Khz cycles */
|
155 | dff38e7b | bellard | period = 1 << (period_code - 1); |
156 | 73822ec8 | aliguori | #ifdef TARGET_I386
|
157 | 73822ec8 | aliguori | if(period != s->period)
|
158 | 73822ec8 | aliguori | s->irq_coalesced = (s->irq_coalesced * s->period) / period; |
159 | 73822ec8 | aliguori | s->period = period; |
160 | 73822ec8 | aliguori | #endif
|
161 | dff38e7b | bellard | /* compute 32 khz clock */
|
162 | 6ee093c9 | Juan Quintela | cur_clock = muldiv64(current_time, 32768, get_ticks_per_sec());
|
163 | dff38e7b | bellard | next_irq_clock = (cur_clock & ~(period - 1)) + period;
|
164 | 6875204c | Jan Kiszka | s->next_periodic_time = |
165 | 6875204c | Jan Kiszka | muldiv64(next_irq_clock, get_ticks_per_sec(), 32768) + 1; |
166 | dff38e7b | bellard | qemu_mod_timer(s->periodic_timer, s->next_periodic_time); |
167 | dff38e7b | bellard | } else {
|
168 | 73822ec8 | aliguori | #ifdef TARGET_I386
|
169 | 73822ec8 | aliguori | s->irq_coalesced = 0;
|
170 | 73822ec8 | aliguori | #endif
|
171 | dff38e7b | bellard | qemu_del_timer(s->periodic_timer); |
172 | dff38e7b | bellard | } |
173 | dff38e7b | bellard | } |
174 | dff38e7b | bellard | |
175 | dff38e7b | bellard | static void rtc_periodic_timer(void *opaque) |
176 | dff38e7b | bellard | { |
177 | dff38e7b | bellard | RTCState *s = opaque; |
178 | dff38e7b | bellard | |
179 | dff38e7b | bellard | rtc_timer_update(s, s->next_periodic_time); |
180 | 100d9891 | aurel32 | if (s->cmos_data[RTC_REG_B] & REG_B_PIE) {
|
181 | 100d9891 | aurel32 | s->cmos_data[RTC_REG_C] |= 0xc0;
|
182 | 93b66569 | aliguori | #ifdef TARGET_I386
|
183 | 93b66569 | aliguori | if(rtc_td_hack) {
|
184 | 93b66569 | aliguori | apic_reset_irq_delivered(); |
185 | 93b66569 | aliguori | rtc_irq_raise(s->irq); |
186 | 93b66569 | aliguori | if (!apic_get_irq_delivered()) {
|
187 | 93b66569 | aliguori | s->irq_coalesced++; |
188 | 93b66569 | aliguori | rtc_coalesced_timer_update(s); |
189 | 93b66569 | aliguori | } |
190 | 93b66569 | aliguori | } else
|
191 | 93b66569 | aliguori | #endif
|
192 | 100d9891 | aurel32 | rtc_irq_raise(s->irq); |
193 | 100d9891 | aurel32 | } |
194 | 100d9891 | aurel32 | if (s->cmos_data[RTC_REG_B] & REG_B_SQWE) {
|
195 | 100d9891 | aurel32 | /* Not square wave at all but we don't want 2048Hz interrupts!
|
196 | 100d9891 | aurel32 | Must be seen as a pulse. */
|
197 | 100d9891 | aurel32 | qemu_irq_raise(s->sqw_irq); |
198 | 100d9891 | aurel32 | } |
199 | dff38e7b | bellard | } |
200 | 80cabfad | bellard | |
201 | b41a2cd1 | bellard | static void cmos_ioport_write(void *opaque, uint32_t addr, uint32_t data) |
202 | 80cabfad | bellard | { |
203 | b41a2cd1 | bellard | RTCState *s = opaque; |
204 | 80cabfad | bellard | |
205 | 80cabfad | bellard | if ((addr & 1) == 0) { |
206 | 80cabfad | bellard | s->cmos_index = data & 0x7f;
|
207 | 80cabfad | bellard | } else {
|
208 | 80cabfad | bellard | #ifdef DEBUG_CMOS
|
209 | 80cabfad | bellard | printf("cmos: write index=0x%02x val=0x%02x\n",
|
210 | 80cabfad | bellard | s->cmos_index, data); |
211 | 3b46e624 | ths | #endif
|
212 | dff38e7b | bellard | switch(s->cmos_index) {
|
213 | 80cabfad | bellard | case RTC_SECONDS_ALARM:
|
214 | 80cabfad | bellard | case RTC_MINUTES_ALARM:
|
215 | 80cabfad | bellard | case RTC_HOURS_ALARM:
|
216 | 80cabfad | bellard | /* XXX: not supported */
|
217 | 80cabfad | bellard | s->cmos_data[s->cmos_index] = data; |
218 | 80cabfad | bellard | break;
|
219 | 80cabfad | bellard | case RTC_SECONDS:
|
220 | 80cabfad | bellard | case RTC_MINUTES:
|
221 | 80cabfad | bellard | case RTC_HOURS:
|
222 | 80cabfad | bellard | case RTC_DAY_OF_WEEK:
|
223 | 80cabfad | bellard | case RTC_DAY_OF_MONTH:
|
224 | 80cabfad | bellard | case RTC_MONTH:
|
225 | 80cabfad | bellard | case RTC_YEAR:
|
226 | 80cabfad | bellard | s->cmos_data[s->cmos_index] = data; |
227 | dff38e7b | bellard | /* if in set mode, do not update the time */
|
228 | dff38e7b | bellard | if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
|
229 | dff38e7b | bellard | rtc_set_time(s); |
230 | dff38e7b | bellard | } |
231 | 80cabfad | bellard | break;
|
232 | 80cabfad | bellard | case RTC_REG_A:
|
233 | dff38e7b | bellard | /* UIP bit is read only */
|
234 | dff38e7b | bellard | s->cmos_data[RTC_REG_A] = (data & ~REG_A_UIP) | |
235 | dff38e7b | bellard | (s->cmos_data[RTC_REG_A] & REG_A_UIP); |
236 | 6875204c | Jan Kiszka | rtc_timer_update(s, qemu_get_clock(rtc_clock)); |
237 | dff38e7b | bellard | break;
|
238 | 80cabfad | bellard | case RTC_REG_B:
|
239 | dff38e7b | bellard | if (data & REG_B_SET) {
|
240 | dff38e7b | bellard | /* set mode: reset UIP mode */
|
241 | dff38e7b | bellard | s->cmos_data[RTC_REG_A] &= ~REG_A_UIP; |
242 | dff38e7b | bellard | data &= ~REG_B_UIE; |
243 | dff38e7b | bellard | } else {
|
244 | dff38e7b | bellard | /* if disabling set mode, update the time */
|
245 | dff38e7b | bellard | if (s->cmos_data[RTC_REG_B] & REG_B_SET) {
|
246 | dff38e7b | bellard | rtc_set_time(s); |
247 | dff38e7b | bellard | } |
248 | dff38e7b | bellard | } |
249 | dff38e7b | bellard | s->cmos_data[RTC_REG_B] = data; |
250 | 6875204c | Jan Kiszka | rtc_timer_update(s, qemu_get_clock(rtc_clock)); |
251 | 80cabfad | bellard | break;
|
252 | 80cabfad | bellard | case RTC_REG_C:
|
253 | 80cabfad | bellard | case RTC_REG_D:
|
254 | 80cabfad | bellard | /* cannot write to them */
|
255 | 80cabfad | bellard | break;
|
256 | 80cabfad | bellard | default:
|
257 | 80cabfad | bellard | s->cmos_data[s->cmos_index] = data; |
258 | 80cabfad | bellard | break;
|
259 | 80cabfad | bellard | } |
260 | 80cabfad | bellard | } |
261 | 80cabfad | bellard | } |
262 | 80cabfad | bellard | |
263 | dff38e7b | bellard | static inline int to_bcd(RTCState *s, int a) |
264 | 80cabfad | bellard | { |
265 | 6f1bf24d | aurel32 | if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
|
266 | dff38e7b | bellard | return a;
|
267 | dff38e7b | bellard | } else {
|
268 | dff38e7b | bellard | return ((a / 10) << 4) | (a % 10); |
269 | dff38e7b | bellard | } |
270 | 80cabfad | bellard | } |
271 | 80cabfad | bellard | |
272 | dff38e7b | bellard | static inline int from_bcd(RTCState *s, int a) |
273 | 80cabfad | bellard | { |
274 | 6f1bf24d | aurel32 | if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
|
275 | dff38e7b | bellard | return a;
|
276 | dff38e7b | bellard | } else {
|
277 | dff38e7b | bellard | return ((a >> 4) * 10) + (a & 0x0f); |
278 | dff38e7b | bellard | } |
279 | dff38e7b | bellard | } |
280 | dff38e7b | bellard | |
281 | dff38e7b | bellard | static void rtc_set_time(RTCState *s) |
282 | dff38e7b | bellard | { |
283 | 43f493af | bellard | struct tm *tm = &s->current_tm;
|
284 | dff38e7b | bellard | |
285 | dff38e7b | bellard | tm->tm_sec = from_bcd(s, s->cmos_data[RTC_SECONDS]); |
286 | dff38e7b | bellard | tm->tm_min = from_bcd(s, s->cmos_data[RTC_MINUTES]); |
287 | 43f493af | bellard | tm->tm_hour = from_bcd(s, s->cmos_data[RTC_HOURS] & 0x7f);
|
288 | 43f493af | bellard | if (!(s->cmos_data[RTC_REG_B] & 0x02) && |
289 | 43f493af | bellard | (s->cmos_data[RTC_HOURS] & 0x80)) {
|
290 | 43f493af | bellard | tm->tm_hour += 12;
|
291 | 43f493af | bellard | } |
292 | 6f1bf24d | aurel32 | tm->tm_wday = from_bcd(s, s->cmos_data[RTC_DAY_OF_WEEK]) - 1;
|
293 | dff38e7b | bellard | tm->tm_mday = from_bcd(s, s->cmos_data[RTC_DAY_OF_MONTH]); |
294 | dff38e7b | bellard | tm->tm_mon = from_bcd(s, s->cmos_data[RTC_MONTH]) - 1;
|
295 | 42fc73a1 | aurel32 | tm->tm_year = from_bcd(s, s->cmos_data[RTC_YEAR]) + s->base_year - 1900;
|
296 | 43f493af | bellard | } |
297 | 43f493af | bellard | |
298 | 43f493af | bellard | static void rtc_copy_date(RTCState *s) |
299 | 43f493af | bellard | { |
300 | 43f493af | bellard | const struct tm *tm = &s->current_tm; |
301 | 42fc73a1 | aurel32 | int year;
|
302 | dff38e7b | bellard | |
303 | 43f493af | bellard | s->cmos_data[RTC_SECONDS] = to_bcd(s, tm->tm_sec); |
304 | 43f493af | bellard | s->cmos_data[RTC_MINUTES] = to_bcd(s, tm->tm_min); |
305 | 43f493af | bellard | if (s->cmos_data[RTC_REG_B] & 0x02) { |
306 | 43f493af | bellard | /* 24 hour format */
|
307 | 43f493af | bellard | s->cmos_data[RTC_HOURS] = to_bcd(s, tm->tm_hour); |
308 | 43f493af | bellard | } else {
|
309 | 43f493af | bellard | /* 12 hour format */
|
310 | 43f493af | bellard | s->cmos_data[RTC_HOURS] = to_bcd(s, tm->tm_hour % 12);
|
311 | 43f493af | bellard | if (tm->tm_hour >= 12) |
312 | 43f493af | bellard | s->cmos_data[RTC_HOURS] |= 0x80;
|
313 | 43f493af | bellard | } |
314 | 6f1bf24d | aurel32 | s->cmos_data[RTC_DAY_OF_WEEK] = to_bcd(s, tm->tm_wday + 1);
|
315 | 43f493af | bellard | s->cmos_data[RTC_DAY_OF_MONTH] = to_bcd(s, tm->tm_mday); |
316 | 43f493af | bellard | s->cmos_data[RTC_MONTH] = to_bcd(s, tm->tm_mon + 1);
|
317 | 42fc73a1 | aurel32 | year = (tm->tm_year - s->base_year) % 100;
|
318 | 42fc73a1 | aurel32 | if (year < 0) |
319 | 42fc73a1 | aurel32 | year += 100;
|
320 | 42fc73a1 | aurel32 | s->cmos_data[RTC_YEAR] = to_bcd(s, year); |
321 | 43f493af | bellard | } |
322 | 43f493af | bellard | |
323 | 43f493af | bellard | /* month is between 0 and 11. */
|
324 | 43f493af | bellard | static int get_days_in_month(int month, int year) |
325 | 43f493af | bellard | { |
326 | 5fafdf24 | ths | static const int days_tab[12] = { |
327 | 5fafdf24 | ths | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
328 | 43f493af | bellard | }; |
329 | 43f493af | bellard | int d;
|
330 | 43f493af | bellard | if ((unsigned )month >= 12) |
331 | 43f493af | bellard | return 31; |
332 | 43f493af | bellard | d = days_tab[month]; |
333 | 43f493af | bellard | if (month == 1) { |
334 | 43f493af | bellard | if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0)) |
335 | 43f493af | bellard | d++; |
336 | 43f493af | bellard | } |
337 | 43f493af | bellard | return d;
|
338 | 43f493af | bellard | } |
339 | 43f493af | bellard | |
340 | 43f493af | bellard | /* update 'tm' to the next second */
|
341 | 43f493af | bellard | static void rtc_next_second(struct tm *tm) |
342 | 43f493af | bellard | { |
343 | 43f493af | bellard | int days_in_month;
|
344 | 43f493af | bellard | |
345 | 43f493af | bellard | tm->tm_sec++; |
346 | 43f493af | bellard | if ((unsigned)tm->tm_sec >= 60) { |
347 | 43f493af | bellard | tm->tm_sec = 0;
|
348 | 43f493af | bellard | tm->tm_min++; |
349 | 43f493af | bellard | if ((unsigned)tm->tm_min >= 60) { |
350 | 43f493af | bellard | tm->tm_min = 0;
|
351 | 43f493af | bellard | tm->tm_hour++; |
352 | 43f493af | bellard | if ((unsigned)tm->tm_hour >= 24) { |
353 | 43f493af | bellard | tm->tm_hour = 0;
|
354 | 43f493af | bellard | /* next day */
|
355 | 43f493af | bellard | tm->tm_wday++; |
356 | 43f493af | bellard | if ((unsigned)tm->tm_wday >= 7) |
357 | 43f493af | bellard | tm->tm_wday = 0;
|
358 | 5fafdf24 | ths | days_in_month = get_days_in_month(tm->tm_mon, |
359 | 43f493af | bellard | tm->tm_year + 1900);
|
360 | 43f493af | bellard | tm->tm_mday++; |
361 | 43f493af | bellard | if (tm->tm_mday < 1) { |
362 | 43f493af | bellard | tm->tm_mday = 1;
|
363 | 43f493af | bellard | } else if (tm->tm_mday > days_in_month) { |
364 | 43f493af | bellard | tm->tm_mday = 1;
|
365 | 43f493af | bellard | tm->tm_mon++; |
366 | 43f493af | bellard | if (tm->tm_mon >= 12) { |
367 | 43f493af | bellard | tm->tm_mon = 0;
|
368 | 43f493af | bellard | tm->tm_year++; |
369 | 43f493af | bellard | } |
370 | 43f493af | bellard | } |
371 | 43f493af | bellard | } |
372 | 43f493af | bellard | } |
373 | 43f493af | bellard | } |
374 | dff38e7b | bellard | } |
375 | dff38e7b | bellard | |
376 | 43f493af | bellard | |
377 | dff38e7b | bellard | static void rtc_update_second(void *opaque) |
378 | dff38e7b | bellard | { |
379 | dff38e7b | bellard | RTCState *s = opaque; |
380 | 4721c457 | bellard | int64_t delay; |
381 | dff38e7b | bellard | |
382 | dff38e7b | bellard | /* if the oscillator is not in normal operation, we do not update */
|
383 | dff38e7b | bellard | if ((s->cmos_data[RTC_REG_A] & 0x70) != 0x20) { |
384 | 6ee093c9 | Juan Quintela | s->next_second_time += get_ticks_per_sec(); |
385 | dff38e7b | bellard | qemu_mod_timer(s->second_timer, s->next_second_time); |
386 | dff38e7b | bellard | } else {
|
387 | 43f493af | bellard | rtc_next_second(&s->current_tm); |
388 | 3b46e624 | ths | |
389 | dff38e7b | bellard | if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
|
390 | dff38e7b | bellard | /* update in progress bit */
|
391 | dff38e7b | bellard | s->cmos_data[RTC_REG_A] |= REG_A_UIP; |
392 | dff38e7b | bellard | } |
393 | 4721c457 | bellard | /* should be 244 us = 8 / 32768 seconds, but currently the
|
394 | 4721c457 | bellard | timers do not have the necessary resolution. */
|
395 | 6ee093c9 | Juan Quintela | delay = (get_ticks_per_sec() * 1) / 100; |
396 | 4721c457 | bellard | if (delay < 1) |
397 | 4721c457 | bellard | delay = 1;
|
398 | 5fafdf24 | ths | qemu_mod_timer(s->second_timer2, |
399 | 4721c457 | bellard | s->next_second_time + delay); |
400 | dff38e7b | bellard | } |
401 | dff38e7b | bellard | } |
402 | dff38e7b | bellard | |
403 | dff38e7b | bellard | static void rtc_update_second2(void *opaque) |
404 | dff38e7b | bellard | { |
405 | dff38e7b | bellard | RTCState *s = opaque; |
406 | dff38e7b | bellard | |
407 | dff38e7b | bellard | if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
|
408 | dff38e7b | bellard | rtc_copy_date(s); |
409 | dff38e7b | bellard | } |
410 | dff38e7b | bellard | |
411 | dff38e7b | bellard | /* check alarm */
|
412 | dff38e7b | bellard | if (s->cmos_data[RTC_REG_B] & REG_B_AIE) {
|
413 | dff38e7b | bellard | if (((s->cmos_data[RTC_SECONDS_ALARM] & 0xc0) == 0xc0 || |
414 | 43f493af | bellard | s->cmos_data[RTC_SECONDS_ALARM] == s->current_tm.tm_sec) && |
415 | dff38e7b | bellard | ((s->cmos_data[RTC_MINUTES_ALARM] & 0xc0) == 0xc0 || |
416 | 43f493af | bellard | s->cmos_data[RTC_MINUTES_ALARM] == s->current_tm.tm_mon) && |
417 | dff38e7b | bellard | ((s->cmos_data[RTC_HOURS_ALARM] & 0xc0) == 0xc0 || |
418 | 43f493af | bellard | s->cmos_data[RTC_HOURS_ALARM] == s->current_tm.tm_hour)) { |
419 | dff38e7b | bellard | |
420 | 5fafdf24 | ths | s->cmos_data[RTC_REG_C] |= 0xa0;
|
421 | 16b29ae1 | aliguori | rtc_irq_raise(s->irq); |
422 | dff38e7b | bellard | } |
423 | dff38e7b | bellard | } |
424 | dff38e7b | bellard | |
425 | dff38e7b | bellard | /* update ended interrupt */
|
426 | 98815437 | Bernhard Kauer | s->cmos_data[RTC_REG_C] |= REG_C_UF; |
427 | dff38e7b | bellard | if (s->cmos_data[RTC_REG_B] & REG_B_UIE) {
|
428 | 98815437 | Bernhard Kauer | s->cmos_data[RTC_REG_C] |= REG_C_IRQF; |
429 | 98815437 | Bernhard Kauer | rtc_irq_raise(s->irq); |
430 | dff38e7b | bellard | } |
431 | dff38e7b | bellard | |
432 | dff38e7b | bellard | /* clear update in progress bit */
|
433 | dff38e7b | bellard | s->cmos_data[RTC_REG_A] &= ~REG_A_UIP; |
434 | dff38e7b | bellard | |
435 | 6ee093c9 | Juan Quintela | s->next_second_time += get_ticks_per_sec(); |
436 | dff38e7b | bellard | qemu_mod_timer(s->second_timer, s->next_second_time); |
437 | 80cabfad | bellard | } |
438 | 80cabfad | bellard | |
439 | b41a2cd1 | bellard | static uint32_t cmos_ioport_read(void *opaque, uint32_t addr) |
440 | 80cabfad | bellard | { |
441 | b41a2cd1 | bellard | RTCState *s = opaque; |
442 | 80cabfad | bellard | int ret;
|
443 | 80cabfad | bellard | if ((addr & 1) == 0) { |
444 | 80cabfad | bellard | return 0xff; |
445 | 80cabfad | bellard | } else {
|
446 | 80cabfad | bellard | switch(s->cmos_index) {
|
447 | 80cabfad | bellard | case RTC_SECONDS:
|
448 | 80cabfad | bellard | case RTC_MINUTES:
|
449 | 80cabfad | bellard | case RTC_HOURS:
|
450 | 80cabfad | bellard | case RTC_DAY_OF_WEEK:
|
451 | 80cabfad | bellard | case RTC_DAY_OF_MONTH:
|
452 | 80cabfad | bellard | case RTC_MONTH:
|
453 | 80cabfad | bellard | case RTC_YEAR:
|
454 | 80cabfad | bellard | ret = s->cmos_data[s->cmos_index]; |
455 | 80cabfad | bellard | break;
|
456 | 80cabfad | bellard | case RTC_REG_A:
|
457 | 80cabfad | bellard | ret = s->cmos_data[s->cmos_index]; |
458 | 80cabfad | bellard | break;
|
459 | 80cabfad | bellard | case RTC_REG_C:
|
460 | 80cabfad | bellard | ret = s->cmos_data[s->cmos_index]; |
461 | d537cf6c | pbrook | qemu_irq_lower(s->irq); |
462 | 5fafdf24 | ths | s->cmos_data[RTC_REG_C] = 0x00;
|
463 | 80cabfad | bellard | break;
|
464 | 80cabfad | bellard | default:
|
465 | 80cabfad | bellard | ret = s->cmos_data[s->cmos_index]; |
466 | 80cabfad | bellard | break;
|
467 | 80cabfad | bellard | } |
468 | 80cabfad | bellard | #ifdef DEBUG_CMOS
|
469 | 80cabfad | bellard | printf("cmos: read index=0x%02x val=0x%02x\n",
|
470 | 80cabfad | bellard | s->cmos_index, ret); |
471 | 80cabfad | bellard | #endif
|
472 | 80cabfad | bellard | return ret;
|
473 | 80cabfad | bellard | } |
474 | 80cabfad | bellard | } |
475 | 80cabfad | bellard | |
476 | dff38e7b | bellard | void rtc_set_memory(RTCState *s, int addr, int val) |
477 | dff38e7b | bellard | { |
478 | dff38e7b | bellard | if (addr >= 0 && addr <= 127) |
479 | dff38e7b | bellard | s->cmos_data[addr] = val; |
480 | dff38e7b | bellard | } |
481 | dff38e7b | bellard | |
482 | dff38e7b | bellard | void rtc_set_date(RTCState *s, const struct tm *tm) |
483 | dff38e7b | bellard | { |
484 | 43f493af | bellard | s->current_tm = *tm; |
485 | dff38e7b | bellard | rtc_copy_date(s); |
486 | dff38e7b | bellard | } |
487 | dff38e7b | bellard | |
488 | ea55ffb3 | ths | /* PC cmos mappings */
|
489 | ea55ffb3 | ths | #define REG_IBM_CENTURY_BYTE 0x32 |
490 | ea55ffb3 | ths | #define REG_IBM_PS2_CENTURY_BYTE 0x37 |
491 | ea55ffb3 | ths | |
492 | 9596ebb7 | pbrook | static void rtc_set_date_from_host(RTCState *s) |
493 | ea55ffb3 | ths | { |
494 | f6503059 | balrog | struct tm tm;
|
495 | ea55ffb3 | ths | int val;
|
496 | ea55ffb3 | ths | |
497 | ea55ffb3 | ths | /* set the CMOS date */
|
498 | f6503059 | balrog | qemu_get_timedate(&tm, 0);
|
499 | f6503059 | balrog | rtc_set_date(s, &tm); |
500 | ea55ffb3 | ths | |
501 | f6503059 | balrog | val = to_bcd(s, (tm.tm_year / 100) + 19); |
502 | ea55ffb3 | ths | rtc_set_memory(s, REG_IBM_CENTURY_BYTE, val); |
503 | ea55ffb3 | ths | rtc_set_memory(s, REG_IBM_PS2_CENTURY_BYTE, val); |
504 | ea55ffb3 | ths | } |
505 | ea55ffb3 | ths | |
506 | dff38e7b | bellard | static void rtc_save(QEMUFile *f, void *opaque) |
507 | dff38e7b | bellard | { |
508 | dff38e7b | bellard | RTCState *s = opaque; |
509 | dff38e7b | bellard | |
510 | dff38e7b | bellard | qemu_put_buffer(f, s->cmos_data, 128);
|
511 | dff38e7b | bellard | qemu_put_8s(f, &s->cmos_index); |
512 | 3b46e624 | ths | |
513 | bee8d684 | ths | qemu_put_be32(f, s->current_tm.tm_sec); |
514 | bee8d684 | ths | qemu_put_be32(f, s->current_tm.tm_min); |
515 | bee8d684 | ths | qemu_put_be32(f, s->current_tm.tm_hour); |
516 | bee8d684 | ths | qemu_put_be32(f, s->current_tm.tm_wday); |
517 | bee8d684 | ths | qemu_put_be32(f, s->current_tm.tm_mday); |
518 | bee8d684 | ths | qemu_put_be32(f, s->current_tm.tm_mon); |
519 | bee8d684 | ths | qemu_put_be32(f, s->current_tm.tm_year); |
520 | dff38e7b | bellard | |
521 | dff38e7b | bellard | qemu_put_timer(f, s->periodic_timer); |
522 | bee8d684 | ths | qemu_put_be64(f, s->next_periodic_time); |
523 | dff38e7b | bellard | |
524 | bee8d684 | ths | qemu_put_be64(f, s->next_second_time); |
525 | dff38e7b | bellard | qemu_put_timer(f, s->second_timer); |
526 | dff38e7b | bellard | qemu_put_timer(f, s->second_timer2); |
527 | 80cabfad | bellard | } |
528 | 80cabfad | bellard | |
529 | dff38e7b | bellard | static int rtc_load(QEMUFile *f, void *opaque, int version_id) |
530 | 80cabfad | bellard | { |
531 | dff38e7b | bellard | RTCState *s = opaque; |
532 | dff38e7b | bellard | |
533 | dff38e7b | bellard | if (version_id != 1) |
534 | dff38e7b | bellard | return -EINVAL;
|
535 | 80cabfad | bellard | |
536 | dff38e7b | bellard | qemu_get_buffer(f, s->cmos_data, 128);
|
537 | dff38e7b | bellard | qemu_get_8s(f, &s->cmos_index); |
538 | 43f493af | bellard | |
539 | bee8d684 | ths | s->current_tm.tm_sec=qemu_get_be32(f); |
540 | bee8d684 | ths | s->current_tm.tm_min=qemu_get_be32(f); |
541 | bee8d684 | ths | s->current_tm.tm_hour=qemu_get_be32(f); |
542 | bee8d684 | ths | s->current_tm.tm_wday=qemu_get_be32(f); |
543 | bee8d684 | ths | s->current_tm.tm_mday=qemu_get_be32(f); |
544 | bee8d684 | ths | s->current_tm.tm_mon=qemu_get_be32(f); |
545 | bee8d684 | ths | s->current_tm.tm_year=qemu_get_be32(f); |
546 | dff38e7b | bellard | |
547 | dff38e7b | bellard | qemu_get_timer(f, s->periodic_timer); |
548 | bee8d684 | ths | s->next_periodic_time=qemu_get_be64(f); |
549 | dff38e7b | bellard | |
550 | bee8d684 | ths | s->next_second_time=qemu_get_be64(f); |
551 | dff38e7b | bellard | qemu_get_timer(f, s->second_timer); |
552 | dff38e7b | bellard | qemu_get_timer(f, s->second_timer2); |
553 | dff38e7b | bellard | return 0; |
554 | dff38e7b | bellard | } |
555 | dff38e7b | bellard | |
556 | 73822ec8 | aliguori | #ifdef TARGET_I386
|
557 | 73822ec8 | aliguori | static void rtc_save_td(QEMUFile *f, void *opaque) |
558 | 73822ec8 | aliguori | { |
559 | 73822ec8 | aliguori | RTCState *s = opaque; |
560 | 73822ec8 | aliguori | |
561 | 73822ec8 | aliguori | qemu_put_be32(f, s->irq_coalesced); |
562 | 73822ec8 | aliguori | qemu_put_be32(f, s->period); |
563 | 73822ec8 | aliguori | } |
564 | 73822ec8 | aliguori | |
565 | 73822ec8 | aliguori | static int rtc_load_td(QEMUFile *f, void *opaque, int version_id) |
566 | 73822ec8 | aliguori | { |
567 | 73822ec8 | aliguori | RTCState *s = opaque; |
568 | 73822ec8 | aliguori | |
569 | 73822ec8 | aliguori | if (version_id != 1) |
570 | 73822ec8 | aliguori | return -EINVAL;
|
571 | 73822ec8 | aliguori | |
572 | 73822ec8 | aliguori | s->irq_coalesced = qemu_get_be32(f); |
573 | 73822ec8 | aliguori | s->period = qemu_get_be32(f); |
574 | 93b66569 | aliguori | rtc_coalesced_timer_update(s); |
575 | 73822ec8 | aliguori | return 0; |
576 | 73822ec8 | aliguori | } |
577 | 73822ec8 | aliguori | #endif
|
578 | 73822ec8 | aliguori | |
579 | eeb7c03c | Gleb Natapov | static void rtc_reset(void *opaque) |
580 | eeb7c03c | Gleb Natapov | { |
581 | eeb7c03c | Gleb Natapov | RTCState *s = opaque; |
582 | eeb7c03c | Gleb Natapov | |
583 | 72716184 | Anthony Liguori | s->cmos_data[RTC_REG_B] &= ~(REG_B_PIE | REG_B_AIE | REG_B_SQWE); |
584 | 72716184 | Anthony Liguori | s->cmos_data[RTC_REG_C] &= ~(REG_C_UF | REG_C_IRQF | REG_C_PF | REG_C_AF); |
585 | eeb7c03c | Gleb Natapov | |
586 | 72716184 | Anthony Liguori | qemu_irq_lower(s->irq); |
587 | eeb7c03c | Gleb Natapov | |
588 | eeb7c03c | Gleb Natapov | #ifdef TARGET_I386
|
589 | eeb7c03c | Gleb Natapov | if (rtc_td_hack)
|
590 | eeb7c03c | Gleb Natapov | s->irq_coalesced = 0;
|
591 | eeb7c03c | Gleb Natapov | #endif
|
592 | eeb7c03c | Gleb Natapov | } |
593 | eeb7c03c | Gleb Natapov | |
594 | 32e0c826 | Gerd Hoffmann | static int rtc_initfn(ISADevice *dev) |
595 | dff38e7b | bellard | { |
596 | 32e0c826 | Gerd Hoffmann | RTCState *s = DO_UPCAST(RTCState, dev, dev); |
597 | 32e0c826 | Gerd Hoffmann | int base = 0x70; |
598 | 32e0c826 | Gerd Hoffmann | int isairq = 8; |
599 | dff38e7b | bellard | |
600 | 32e0c826 | Gerd Hoffmann | isa_init_irq(dev, &s->irq, isairq); |
601 | 80cabfad | bellard | |
602 | 80cabfad | bellard | s->cmos_data[RTC_REG_A] = 0x26;
|
603 | 80cabfad | bellard | s->cmos_data[RTC_REG_B] = 0x02;
|
604 | 80cabfad | bellard | s->cmos_data[RTC_REG_C] = 0x00;
|
605 | 80cabfad | bellard | s->cmos_data[RTC_REG_D] = 0x80;
|
606 | 80cabfad | bellard | |
607 | ea55ffb3 | ths | rtc_set_date_from_host(s); |
608 | ea55ffb3 | ths | |
609 | 6875204c | Jan Kiszka | s->periodic_timer = qemu_new_timer(rtc_clock, rtc_periodic_timer, s); |
610 | 93b66569 | aliguori | #ifdef TARGET_I386
|
611 | 93b66569 | aliguori | if (rtc_td_hack)
|
612 | 6875204c | Jan Kiszka | s->coalesced_timer = |
613 | 6875204c | Jan Kiszka | qemu_new_timer(rtc_clock, rtc_coalesced_timer, s); |
614 | 93b66569 | aliguori | #endif
|
615 | 6875204c | Jan Kiszka | s->second_timer = qemu_new_timer(rtc_clock, rtc_update_second, s); |
616 | 6875204c | Jan Kiszka | s->second_timer2 = qemu_new_timer(rtc_clock, rtc_update_second2, s); |
617 | dff38e7b | bellard | |
618 | 6875204c | Jan Kiszka | s->next_second_time = |
619 | 6875204c | Jan Kiszka | qemu_get_clock(rtc_clock) + (get_ticks_per_sec() * 99) / 100; |
620 | dff38e7b | bellard | qemu_mod_timer(s->second_timer2, s->next_second_time); |
621 | dff38e7b | bellard | |
622 | b41a2cd1 | bellard | register_ioport_write(base, 2, 1, cmos_ioport_write, s); |
623 | b41a2cd1 | bellard | register_ioport_read(base, 2, 1, cmos_ioport_read, s); |
624 | dff38e7b | bellard | |
625 | dff38e7b | bellard | register_savevm("mc146818rtc", base, 1, rtc_save, rtc_load, s); |
626 | 73822ec8 | aliguori | #ifdef TARGET_I386
|
627 | 73822ec8 | aliguori | if (rtc_td_hack)
|
628 | 73822ec8 | aliguori | register_savevm("mc146818rtc-td", base, 1, rtc_save_td, rtc_load_td, s); |
629 | 73822ec8 | aliguori | #endif
|
630 | a08d4367 | Jan Kiszka | qemu_register_reset(rtc_reset, s); |
631 | 32e0c826 | Gerd Hoffmann | return 0; |
632 | 32e0c826 | Gerd Hoffmann | } |
633 | 32e0c826 | Gerd Hoffmann | |
634 | 32e0c826 | Gerd Hoffmann | RTCState *rtc_init(int base_year)
|
635 | 32e0c826 | Gerd Hoffmann | { |
636 | 32e0c826 | Gerd Hoffmann | ISADevice *dev; |
637 | eeb7c03c | Gleb Natapov | |
638 | 32e0c826 | Gerd Hoffmann | dev = isa_create("mc146818rtc");
|
639 | 32e0c826 | Gerd Hoffmann | qdev_prop_set_int32(&dev->qdev, "base_year", base_year);
|
640 | e23a1b33 | Markus Armbruster | qdev_init_nofail(&dev->qdev); |
641 | 32e0c826 | Gerd Hoffmann | return DO_UPCAST(RTCState, dev, dev);
|
642 | 80cabfad | bellard | } |
643 | 80cabfad | bellard | |
644 | 32e0c826 | Gerd Hoffmann | static ISADeviceInfo mc146818rtc_info = {
|
645 | 32e0c826 | Gerd Hoffmann | .qdev.name = "mc146818rtc",
|
646 | 32e0c826 | Gerd Hoffmann | .qdev.size = sizeof(RTCState),
|
647 | 32e0c826 | Gerd Hoffmann | .qdev.no_user = 1,
|
648 | 32e0c826 | Gerd Hoffmann | .init = rtc_initfn, |
649 | 32e0c826 | Gerd Hoffmann | .qdev.props = (Property[]) { |
650 | 32e0c826 | Gerd Hoffmann | DEFINE_PROP_INT32("base_year", RTCState, base_year, 1980), |
651 | 32e0c826 | Gerd Hoffmann | DEFINE_PROP_END_OF_LIST(), |
652 | 32e0c826 | Gerd Hoffmann | } |
653 | 32e0c826 | Gerd Hoffmann | }; |
654 | 32e0c826 | Gerd Hoffmann | |
655 | 32e0c826 | Gerd Hoffmann | static void mc146818rtc_register(void) |
656 | 100d9891 | aurel32 | { |
657 | 32e0c826 | Gerd Hoffmann | isa_qdev_register(&mc146818rtc_info); |
658 | 100d9891 | aurel32 | } |
659 | 32e0c826 | Gerd Hoffmann | device_init(mc146818rtc_register) |
660 | 100d9891 | aurel32 | |
661 | 2ca9d013 | ths | /* Memory mapped interface */
|
662 | c227f099 | Anthony Liguori | static uint32_t cmos_mm_readb (void *opaque, target_phys_addr_t addr) |
663 | 2ca9d013 | ths | { |
664 | 2ca9d013 | ths | RTCState *s = opaque; |
665 | 2ca9d013 | ths | |
666 | 8da3ff18 | pbrook | return cmos_ioport_read(s, addr >> s->it_shift) & 0xFF; |
667 | 2ca9d013 | ths | } |
668 | 2ca9d013 | ths | |
669 | 9596ebb7 | pbrook | static void cmos_mm_writeb (void *opaque, |
670 | c227f099 | Anthony Liguori | target_phys_addr_t addr, uint32_t value) |
671 | 2ca9d013 | ths | { |
672 | 2ca9d013 | ths | RTCState *s = opaque; |
673 | 2ca9d013 | ths | |
674 | 8da3ff18 | pbrook | cmos_ioport_write(s, addr >> s->it_shift, value & 0xFF);
|
675 | 2ca9d013 | ths | } |
676 | 2ca9d013 | ths | |
677 | c227f099 | Anthony Liguori | static uint32_t cmos_mm_readw (void *opaque, target_phys_addr_t addr) |
678 | 2ca9d013 | ths | { |
679 | 2ca9d013 | ths | RTCState *s = opaque; |
680 | 18c6e2ff | ths | uint32_t val; |
681 | 2ca9d013 | ths | |
682 | 8da3ff18 | pbrook | val = cmos_ioport_read(s, addr >> s->it_shift) & 0xFFFF;
|
683 | 18c6e2ff | ths | #ifdef TARGET_WORDS_BIGENDIAN
|
684 | 18c6e2ff | ths | val = bswap16(val); |
685 | 18c6e2ff | ths | #endif
|
686 | 18c6e2ff | ths | return val;
|
687 | 2ca9d013 | ths | } |
688 | 2ca9d013 | ths | |
689 | 9596ebb7 | pbrook | static void cmos_mm_writew (void *opaque, |
690 | c227f099 | Anthony Liguori | target_phys_addr_t addr, uint32_t value) |
691 | 2ca9d013 | ths | { |
692 | 2ca9d013 | ths | RTCState *s = opaque; |
693 | 18c6e2ff | ths | #ifdef TARGET_WORDS_BIGENDIAN
|
694 | 18c6e2ff | ths | value = bswap16(value); |
695 | 18c6e2ff | ths | #endif
|
696 | 8da3ff18 | pbrook | cmos_ioport_write(s, addr >> s->it_shift, value & 0xFFFF);
|
697 | 2ca9d013 | ths | } |
698 | 2ca9d013 | ths | |
699 | c227f099 | Anthony Liguori | static uint32_t cmos_mm_readl (void *opaque, target_phys_addr_t addr) |
700 | 2ca9d013 | ths | { |
701 | 2ca9d013 | ths | RTCState *s = opaque; |
702 | 18c6e2ff | ths | uint32_t val; |
703 | 2ca9d013 | ths | |
704 | 8da3ff18 | pbrook | val = cmos_ioport_read(s, addr >> s->it_shift); |
705 | 18c6e2ff | ths | #ifdef TARGET_WORDS_BIGENDIAN
|
706 | 18c6e2ff | ths | val = bswap32(val); |
707 | 18c6e2ff | ths | #endif
|
708 | 18c6e2ff | ths | return val;
|
709 | 2ca9d013 | ths | } |
710 | 2ca9d013 | ths | |
711 | 9596ebb7 | pbrook | static void cmos_mm_writel (void *opaque, |
712 | c227f099 | Anthony Liguori | target_phys_addr_t addr, uint32_t value) |
713 | 2ca9d013 | ths | { |
714 | 2ca9d013 | ths | RTCState *s = opaque; |
715 | 18c6e2ff | ths | #ifdef TARGET_WORDS_BIGENDIAN
|
716 | 18c6e2ff | ths | value = bswap32(value); |
717 | 18c6e2ff | ths | #endif
|
718 | 8da3ff18 | pbrook | cmos_ioport_write(s, addr >> s->it_shift, value); |
719 | 2ca9d013 | ths | } |
720 | 2ca9d013 | ths | |
721 | d60efc6b | Blue Swirl | static CPUReadMemoryFunc * const rtc_mm_read[] = { |
722 | 2ca9d013 | ths | &cmos_mm_readb, |
723 | 2ca9d013 | ths | &cmos_mm_readw, |
724 | 2ca9d013 | ths | &cmos_mm_readl, |
725 | 2ca9d013 | ths | }; |
726 | 2ca9d013 | ths | |
727 | d60efc6b | Blue Swirl | static CPUWriteMemoryFunc * const rtc_mm_write[] = { |
728 | 2ca9d013 | ths | &cmos_mm_writeb, |
729 | 2ca9d013 | ths | &cmos_mm_writew, |
730 | 2ca9d013 | ths | &cmos_mm_writel, |
731 | 2ca9d013 | ths | }; |
732 | 2ca9d013 | ths | |
733 | c227f099 | Anthony Liguori | RTCState *rtc_mm_init(target_phys_addr_t base, int it_shift, qemu_irq irq,
|
734 | 42fc73a1 | aurel32 | int base_year)
|
735 | 2ca9d013 | ths | { |
736 | 2ca9d013 | ths | RTCState *s; |
737 | 2ca9d013 | ths | int io_memory;
|
738 | 2ca9d013 | ths | |
739 | 2ca9d013 | ths | s = qemu_mallocz(sizeof(RTCState));
|
740 | 2ca9d013 | ths | |
741 | 2ca9d013 | ths | s->irq = irq; |
742 | 2ca9d013 | ths | s->cmos_data[RTC_REG_A] = 0x26;
|
743 | 2ca9d013 | ths | s->cmos_data[RTC_REG_B] = 0x02;
|
744 | 2ca9d013 | ths | s->cmos_data[RTC_REG_C] = 0x00;
|
745 | 2ca9d013 | ths | s->cmos_data[RTC_REG_D] = 0x80;
|
746 | 2ca9d013 | ths | |
747 | 42fc73a1 | aurel32 | s->base_year = base_year; |
748 | 2ca9d013 | ths | rtc_set_date_from_host(s); |
749 | 2ca9d013 | ths | |
750 | 6875204c | Jan Kiszka | s->periodic_timer = qemu_new_timer(rtc_clock, rtc_periodic_timer, s); |
751 | 6875204c | Jan Kiszka | s->second_timer = qemu_new_timer(rtc_clock, rtc_update_second, s); |
752 | 6875204c | Jan Kiszka | s->second_timer2 = qemu_new_timer(rtc_clock, rtc_update_second2, s); |
753 | 2ca9d013 | ths | |
754 | 6875204c | Jan Kiszka | s->next_second_time = |
755 | 6875204c | Jan Kiszka | qemu_get_clock(rtc_clock) + (get_ticks_per_sec() * 99) / 100; |
756 | 2ca9d013 | ths | qemu_mod_timer(s->second_timer2, s->next_second_time); |
757 | 2ca9d013 | ths | |
758 | 1eed09cb | Avi Kivity | io_memory = cpu_register_io_memory(rtc_mm_read, rtc_mm_write, s); |
759 | 18c6e2ff | ths | cpu_register_physical_memory(base, 2 << it_shift, io_memory);
|
760 | 2ca9d013 | ths | |
761 | 2ca9d013 | ths | register_savevm("mc146818rtc", base, 1, rtc_save, rtc_load, s); |
762 | 73822ec8 | aliguori | #ifdef TARGET_I386
|
763 | 73822ec8 | aliguori | if (rtc_td_hack)
|
764 | 73822ec8 | aliguori | register_savevm("mc146818rtc-td", base, 1, rtc_save_td, rtc_load_td, s); |
765 | 73822ec8 | aliguori | #endif
|
766 | a08d4367 | Jan Kiszka | qemu_register_reset(rtc_reset, s); |
767 | 2ca9d013 | ths | return s;
|
768 | 2ca9d013 | ths | } |