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