]>
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" | |
aa28b9bf | 28 | #include "apic.h" |
87ecb68b | 29 | #include "isa.h" |
1d914fa0 | 30 | #include "mc146818rtc.h" |
80cabfad FB |
31 | |
32 | //#define DEBUG_CMOS | |
aa6f63ff | 33 | //#define DEBUG_COALESCED |
80cabfad | 34 | |
ec51e364 IY |
35 | #ifdef DEBUG_CMOS |
36 | # define CMOS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__) | |
37 | #else | |
38 | # define CMOS_DPRINTF(format, ...) do { } while (0) | |
39 | #endif | |
40 | ||
aa6f63ff BS |
41 | #ifdef DEBUG_COALESCED |
42 | # define DPRINTF_C(format, ...) printf(format, ## __VA_ARGS__) | |
43 | #else | |
44 | # define DPRINTF_C(format, ...) do { } while (0) | |
45 | #endif | |
46 | ||
dd17765b | 47 | #define RTC_REINJECT_ON_ACK_COUNT 20 |
ba32edab | 48 | |
80cabfad FB |
49 | #define RTC_SECONDS 0 |
50 | #define RTC_SECONDS_ALARM 1 | |
51 | #define RTC_MINUTES 2 | |
52 | #define RTC_MINUTES_ALARM 3 | |
53 | #define RTC_HOURS 4 | |
54 | #define RTC_HOURS_ALARM 5 | |
55 | #define RTC_ALARM_DONT_CARE 0xC0 | |
56 | ||
57 | #define RTC_DAY_OF_WEEK 6 | |
58 | #define RTC_DAY_OF_MONTH 7 | |
59 | #define RTC_MONTH 8 | |
60 | #define RTC_YEAR 9 | |
61 | ||
62 | #define RTC_REG_A 10 | |
63 | #define RTC_REG_B 11 | |
64 | #define RTC_REG_C 12 | |
65 | #define RTC_REG_D 13 | |
66 | ||
dff38e7b | 67 | #define REG_A_UIP 0x80 |
80cabfad | 68 | |
100d9891 AJ |
69 | #define REG_B_SET 0x80 |
70 | #define REG_B_PIE 0x40 | |
71 | #define REG_B_AIE 0x20 | |
72 | #define REG_B_UIE 0x10 | |
73 | #define REG_B_SQWE 0x08 | |
74 | #define REG_B_DM 0x04 | |
c29cd656 | 75 | #define REG_B_24H 0x02 |
dff38e7b | 76 | |
72716184 AL |
77 | #define REG_C_UF 0x10 |
78 | #define REG_C_IRQF 0x80 | |
79 | #define REG_C_PF 0x40 | |
80 | #define REG_C_AF 0x20 | |
81 | ||
1d914fa0 | 82 | typedef struct RTCState { |
32e0c826 | 83 | ISADevice dev; |
dff38e7b FB |
84 | uint8_t cmos_data[128]; |
85 | uint8_t cmos_index; | |
43f493af | 86 | struct tm current_tm; |
32e0c826 | 87 | int32_t base_year; |
d537cf6c | 88 | qemu_irq irq; |
100d9891 | 89 | qemu_irq sqw_irq; |
18c6e2ff | 90 | int it_shift; |
dff38e7b FB |
91 | /* periodic timer */ |
92 | QEMUTimer *periodic_timer; | |
93 | int64_t next_periodic_time; | |
94 | /* second update */ | |
95 | int64_t next_second_time; | |
ba32edab | 96 | uint16_t irq_reinject_on_ack_count; |
73822ec8 AL |
97 | uint32_t irq_coalesced; |
98 | uint32_t period; | |
93b66569 | 99 | QEMUTimer *coalesced_timer; |
dff38e7b FB |
100 | QEMUTimer *second_timer; |
101 | QEMUTimer *second_timer2; | |
17604dac | 102 | Notifier clock_reset_notifier; |
1d914fa0 | 103 | } RTCState; |
dff38e7b FB |
104 | |
105 | static void rtc_set_time(RTCState *s); | |
dff38e7b FB |
106 | static void rtc_copy_date(RTCState *s); |
107 | ||
93b66569 AL |
108 | #ifdef TARGET_I386 |
109 | static void rtc_coalesced_timer_update(RTCState *s) | |
110 | { | |
111 | if (s->irq_coalesced == 0) { | |
112 | qemu_del_timer(s->coalesced_timer); | |
113 | } else { | |
114 | /* divide each RTC interval to 2 - 8 smaller intervals */ | |
115 | int c = MIN(s->irq_coalesced, 7) + 1; | |
74475455 | 116 | int64_t next_clock = qemu_get_clock_ns(rtc_clock) + |
6875204c | 117 | muldiv64(s->period / c, get_ticks_per_sec(), 32768); |
93b66569 AL |
118 | qemu_mod_timer(s->coalesced_timer, next_clock); |
119 | } | |
120 | } | |
121 | ||
122 | static void rtc_coalesced_timer(void *opaque) | |
123 | { | |
124 | RTCState *s = opaque; | |
125 | ||
126 | if (s->irq_coalesced != 0) { | |
127 | apic_reset_irq_delivered(); | |
128 | s->cmos_data[RTC_REG_C] |= 0xc0; | |
aa6f63ff | 129 | DPRINTF_C("cmos: injecting from timer\n"); |
7d932dfd | 130 | qemu_irq_raise(s->irq); |
93b66569 AL |
131 | if (apic_get_irq_delivered()) { |
132 | s->irq_coalesced--; | |
aa6f63ff BS |
133 | DPRINTF_C("cmos: coalesced irqs decreased to %d\n", |
134 | s->irq_coalesced); | |
93b66569 AL |
135 | } |
136 | } | |
137 | ||
138 | rtc_coalesced_timer_update(s); | |
139 | } | |
140 | #endif | |
141 | ||
dff38e7b FB |
142 | static void rtc_timer_update(RTCState *s, int64_t current_time) |
143 | { | |
144 | int period_code, period; | |
145 | int64_t cur_clock, next_irq_clock; | |
146 | ||
147 | period_code = s->cmos_data[RTC_REG_A] & 0x0f; | |
100d9891 | 148 | if (period_code != 0 |
7d932dfd | 149 | && ((s->cmos_data[RTC_REG_B] & REG_B_PIE) |
100d9891 | 150 | || ((s->cmos_data[RTC_REG_B] & REG_B_SQWE) && s->sqw_irq))) { |
dff38e7b FB |
151 | if (period_code <= 2) |
152 | period_code += 7; | |
153 | /* period in 32 Khz cycles */ | |
154 | period = 1 << (period_code - 1); | |
73822ec8 | 155 | #ifdef TARGET_I386 |
aa6f63ff | 156 | if (period != s->period) { |
73822ec8 | 157 | s->irq_coalesced = (s->irq_coalesced * s->period) / period; |
aa6f63ff BS |
158 | DPRINTF_C("cmos: coalesced irqs scaled to %d\n", s->irq_coalesced); |
159 | } | |
73822ec8 AL |
160 | s->period = period; |
161 | #endif | |
dff38e7b | 162 | /* compute 32 khz clock */ |
6ee093c9 | 163 | cur_clock = muldiv64(current_time, 32768, get_ticks_per_sec()); |
dff38e7b | 164 | next_irq_clock = (cur_clock & ~(period - 1)) + period; |
6875204c JK |
165 | s->next_periodic_time = |
166 | muldiv64(next_irq_clock, get_ticks_per_sec(), 32768) + 1; | |
dff38e7b FB |
167 | qemu_mod_timer(s->periodic_timer, s->next_periodic_time); |
168 | } else { | |
73822ec8 AL |
169 | #ifdef TARGET_I386 |
170 | s->irq_coalesced = 0; | |
171 | #endif | |
dff38e7b FB |
172 | qemu_del_timer(s->periodic_timer); |
173 | } | |
174 | } | |
175 | ||
176 | static void rtc_periodic_timer(void *opaque) | |
177 | { | |
178 | RTCState *s = opaque; | |
179 | ||
180 | rtc_timer_update(s, s->next_periodic_time); | |
100d9891 AJ |
181 | if (s->cmos_data[RTC_REG_B] & REG_B_PIE) { |
182 | s->cmos_data[RTC_REG_C] |= 0xc0; | |
93b66569 AL |
183 | #ifdef TARGET_I386 |
184 | if(rtc_td_hack) { | |
ba32edab GN |
185 | if (s->irq_reinject_on_ack_count >= RTC_REINJECT_ON_ACK_COUNT) |
186 | s->irq_reinject_on_ack_count = 0; | |
93b66569 | 187 | apic_reset_irq_delivered(); |
7d932dfd | 188 | qemu_irq_raise(s->irq); |
93b66569 AL |
189 | if (!apic_get_irq_delivered()) { |
190 | s->irq_coalesced++; | |
191 | rtc_coalesced_timer_update(s); | |
aa6f63ff BS |
192 | DPRINTF_C("cmos: coalesced irqs increased to %d\n", |
193 | s->irq_coalesced); | |
93b66569 AL |
194 | } |
195 | } else | |
196 | #endif | |
7d932dfd | 197 | qemu_irq_raise(s->irq); |
100d9891 AJ |
198 | } |
199 | if (s->cmos_data[RTC_REG_B] & REG_B_SQWE) { | |
200 | /* Not square wave at all but we don't want 2048Hz interrupts! | |
201 | Must be seen as a pulse. */ | |
202 | qemu_irq_raise(s->sqw_irq); | |
203 | } | |
dff38e7b | 204 | } |
80cabfad | 205 | |
b41a2cd1 | 206 | static void cmos_ioport_write(void *opaque, uint32_t addr, uint32_t data) |
80cabfad | 207 | { |
b41a2cd1 | 208 | RTCState *s = opaque; |
80cabfad FB |
209 | |
210 | if ((addr & 1) == 0) { | |
211 | s->cmos_index = data & 0x7f; | |
212 | } else { | |
ec51e364 IY |
213 | CMOS_DPRINTF("cmos: write index=0x%02x val=0x%02x\n", |
214 | s->cmos_index, data); | |
dff38e7b | 215 | switch(s->cmos_index) { |
80cabfad FB |
216 | case RTC_SECONDS_ALARM: |
217 | case RTC_MINUTES_ALARM: | |
218 | case RTC_HOURS_ALARM: | |
80cabfad FB |
219 | s->cmos_data[s->cmos_index] = data; |
220 | break; | |
221 | case RTC_SECONDS: | |
222 | case RTC_MINUTES: | |
223 | case RTC_HOURS: | |
224 | case RTC_DAY_OF_WEEK: | |
225 | case RTC_DAY_OF_MONTH: | |
226 | case RTC_MONTH: | |
227 | case RTC_YEAR: | |
228 | s->cmos_data[s->cmos_index] = data; | |
dff38e7b FB |
229 | /* if in set mode, do not update the time */ |
230 | if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) { | |
231 | rtc_set_time(s); | |
232 | } | |
80cabfad FB |
233 | break; |
234 | case RTC_REG_A: | |
dff38e7b FB |
235 | /* UIP bit is read only */ |
236 | s->cmos_data[RTC_REG_A] = (data & ~REG_A_UIP) | | |
237 | (s->cmos_data[RTC_REG_A] & REG_A_UIP); | |
74475455 | 238 | rtc_timer_update(s, qemu_get_clock_ns(rtc_clock)); |
dff38e7b | 239 | break; |
80cabfad | 240 | case RTC_REG_B: |
dff38e7b FB |
241 | if (data & REG_B_SET) { |
242 | /* set mode: reset UIP mode */ | |
243 | s->cmos_data[RTC_REG_A] &= ~REG_A_UIP; | |
244 | data &= ~REG_B_UIE; | |
245 | } else { | |
246 | /* if disabling set mode, update the time */ | |
247 | if (s->cmos_data[RTC_REG_B] & REG_B_SET) { | |
248 | rtc_set_time(s); | |
249 | } | |
250 | } | |
51e08f3e AJ |
251 | if (((s->cmos_data[RTC_REG_B] ^ data) & (REG_B_DM | REG_B_24H)) && |
252 | !(data & REG_B_SET)) { | |
253 | /* If the time format has changed and not in set mode, | |
254 | update the registers immediately. */ | |
255 | s->cmos_data[RTC_REG_B] = data; | |
256 | rtc_copy_date(s); | |
257 | } else { | |
258 | s->cmos_data[RTC_REG_B] = data; | |
259 | } | |
74475455 | 260 | rtc_timer_update(s, qemu_get_clock_ns(rtc_clock)); |
80cabfad FB |
261 | break; |
262 | case RTC_REG_C: | |
263 | case RTC_REG_D: | |
264 | /* cannot write to them */ | |
265 | break; | |
266 | default: | |
267 | s->cmos_data[s->cmos_index] = data; | |
268 | break; | |
269 | } | |
270 | } | |
271 | } | |
272 | ||
abd0c6bd | 273 | static inline int rtc_to_bcd(RTCState *s, int a) |
80cabfad | 274 | { |
6f1bf24d | 275 | if (s->cmos_data[RTC_REG_B] & REG_B_DM) { |
dff38e7b FB |
276 | return a; |
277 | } else { | |
278 | return ((a / 10) << 4) | (a % 10); | |
279 | } | |
80cabfad FB |
280 | } |
281 | ||
abd0c6bd | 282 | static inline int rtc_from_bcd(RTCState *s, int a) |
80cabfad | 283 | { |
6f1bf24d | 284 | if (s->cmos_data[RTC_REG_B] & REG_B_DM) { |
dff38e7b FB |
285 | return a; |
286 | } else { | |
287 | return ((a >> 4) * 10) + (a & 0x0f); | |
288 | } | |
289 | } | |
290 | ||
291 | static void rtc_set_time(RTCState *s) | |
292 | { | |
43f493af | 293 | struct tm *tm = &s->current_tm; |
dff38e7b | 294 | |
abd0c6bd PB |
295 | tm->tm_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS]); |
296 | tm->tm_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES]); | |
297 | tm->tm_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS] & 0x7f); | |
c29cd656 | 298 | if (!(s->cmos_data[RTC_REG_B] & REG_B_24H) && |
43f493af FB |
299 | (s->cmos_data[RTC_HOURS] & 0x80)) { |
300 | tm->tm_hour += 12; | |
301 | } | |
abd0c6bd PB |
302 | tm->tm_wday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_WEEK]) - 1; |
303 | tm->tm_mday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_MONTH]); | |
304 | tm->tm_mon = rtc_from_bcd(s, s->cmos_data[RTC_MONTH]) - 1; | |
305 | tm->tm_year = rtc_from_bcd(s, s->cmos_data[RTC_YEAR]) + s->base_year - 1900; | |
80cd3478 LC |
306 | |
307 | rtc_change_mon_event(tm); | |
43f493af FB |
308 | } |
309 | ||
310 | static void rtc_copy_date(RTCState *s) | |
311 | { | |
312 | const struct tm *tm = &s->current_tm; | |
42fc73a1 | 313 | int year; |
dff38e7b | 314 | |
abd0c6bd PB |
315 | s->cmos_data[RTC_SECONDS] = rtc_to_bcd(s, tm->tm_sec); |
316 | s->cmos_data[RTC_MINUTES] = rtc_to_bcd(s, tm->tm_min); | |
c29cd656 | 317 | if (s->cmos_data[RTC_REG_B] & REG_B_24H) { |
43f493af | 318 | /* 24 hour format */ |
abd0c6bd | 319 | s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, tm->tm_hour); |
43f493af FB |
320 | } else { |
321 | /* 12 hour format */ | |
abd0c6bd | 322 | s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, tm->tm_hour % 12); |
43f493af FB |
323 | if (tm->tm_hour >= 12) |
324 | s->cmos_data[RTC_HOURS] |= 0x80; | |
325 | } | |
abd0c6bd PB |
326 | s->cmos_data[RTC_DAY_OF_WEEK] = rtc_to_bcd(s, tm->tm_wday + 1); |
327 | s->cmos_data[RTC_DAY_OF_MONTH] = rtc_to_bcd(s, tm->tm_mday); | |
328 | s->cmos_data[RTC_MONTH] = rtc_to_bcd(s, tm->tm_mon + 1); | |
42fc73a1 AJ |
329 | year = (tm->tm_year - s->base_year) % 100; |
330 | if (year < 0) | |
331 | year += 100; | |
abd0c6bd | 332 | s->cmos_data[RTC_YEAR] = rtc_to_bcd(s, year); |
43f493af FB |
333 | } |
334 | ||
335 | /* month is between 0 and 11. */ | |
336 | static int get_days_in_month(int month, int year) | |
337 | { | |
5fafdf24 TS |
338 | static const int days_tab[12] = { |
339 | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 | |
43f493af FB |
340 | }; |
341 | int d; | |
342 | if ((unsigned )month >= 12) | |
343 | return 31; | |
344 | d = days_tab[month]; | |
345 | if (month == 1) { | |
346 | if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0)) | |
347 | d++; | |
348 | } | |
349 | return d; | |
350 | } | |
351 | ||
352 | /* update 'tm' to the next second */ | |
353 | static void rtc_next_second(struct tm *tm) | |
354 | { | |
355 | int days_in_month; | |
356 | ||
357 | tm->tm_sec++; | |
358 | if ((unsigned)tm->tm_sec >= 60) { | |
359 | tm->tm_sec = 0; | |
360 | tm->tm_min++; | |
361 | if ((unsigned)tm->tm_min >= 60) { | |
362 | tm->tm_min = 0; | |
363 | tm->tm_hour++; | |
364 | if ((unsigned)tm->tm_hour >= 24) { | |
365 | tm->tm_hour = 0; | |
366 | /* next day */ | |
367 | tm->tm_wday++; | |
368 | if ((unsigned)tm->tm_wday >= 7) | |
369 | tm->tm_wday = 0; | |
5fafdf24 | 370 | days_in_month = get_days_in_month(tm->tm_mon, |
43f493af FB |
371 | tm->tm_year + 1900); |
372 | tm->tm_mday++; | |
373 | if (tm->tm_mday < 1) { | |
374 | tm->tm_mday = 1; | |
375 | } else if (tm->tm_mday > days_in_month) { | |
376 | tm->tm_mday = 1; | |
377 | tm->tm_mon++; | |
378 | if (tm->tm_mon >= 12) { | |
379 | tm->tm_mon = 0; | |
380 | tm->tm_year++; | |
381 | } | |
382 | } | |
383 | } | |
384 | } | |
385 | } | |
dff38e7b FB |
386 | } |
387 | ||
43f493af | 388 | |
dff38e7b FB |
389 | static void rtc_update_second(void *opaque) |
390 | { | |
391 | RTCState *s = opaque; | |
4721c457 | 392 | int64_t delay; |
dff38e7b FB |
393 | |
394 | /* if the oscillator is not in normal operation, we do not update */ | |
395 | if ((s->cmos_data[RTC_REG_A] & 0x70) != 0x20) { | |
6ee093c9 | 396 | s->next_second_time += get_ticks_per_sec(); |
dff38e7b FB |
397 | qemu_mod_timer(s->second_timer, s->next_second_time); |
398 | } else { | |
43f493af | 399 | rtc_next_second(&s->current_tm); |
3b46e624 | 400 | |
dff38e7b FB |
401 | if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) { |
402 | /* update in progress bit */ | |
403 | s->cmos_data[RTC_REG_A] |= REG_A_UIP; | |
404 | } | |
4721c457 FB |
405 | /* should be 244 us = 8 / 32768 seconds, but currently the |
406 | timers do not have the necessary resolution. */ | |
6ee093c9 | 407 | delay = (get_ticks_per_sec() * 1) / 100; |
4721c457 FB |
408 | if (delay < 1) |
409 | delay = 1; | |
5fafdf24 | 410 | qemu_mod_timer(s->second_timer2, |
4721c457 | 411 | s->next_second_time + delay); |
dff38e7b FB |
412 | } |
413 | } | |
414 | ||
415 | static void rtc_update_second2(void *opaque) | |
416 | { | |
417 | RTCState *s = opaque; | |
dff38e7b FB |
418 | |
419 | if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) { | |
420 | rtc_copy_date(s); | |
421 | } | |
422 | ||
423 | /* check alarm */ | |
424 | if (s->cmos_data[RTC_REG_B] & REG_B_AIE) { | |
425 | if (((s->cmos_data[RTC_SECONDS_ALARM] & 0xc0) == 0xc0 || | |
f292787d | 426 | rtc_from_bcd(s, s->cmos_data[RTC_SECONDS_ALARM]) == s->current_tm.tm_sec) && |
dff38e7b | 427 | ((s->cmos_data[RTC_MINUTES_ALARM] & 0xc0) == 0xc0 || |
f292787d | 428 | rtc_from_bcd(s, s->cmos_data[RTC_MINUTES_ALARM]) == s->current_tm.tm_min) && |
dff38e7b | 429 | ((s->cmos_data[RTC_HOURS_ALARM] & 0xc0) == 0xc0 || |
f292787d | 430 | rtc_from_bcd(s, s->cmos_data[RTC_HOURS_ALARM]) == s->current_tm.tm_hour)) { |
dff38e7b | 431 | |
5fafdf24 | 432 | s->cmos_data[RTC_REG_C] |= 0xa0; |
7d932dfd | 433 | qemu_irq_raise(s->irq); |
dff38e7b FB |
434 | } |
435 | } | |
436 | ||
437 | /* update ended interrupt */ | |
98815437 | 438 | s->cmos_data[RTC_REG_C] |= REG_C_UF; |
dff38e7b | 439 | if (s->cmos_data[RTC_REG_B] & REG_B_UIE) { |
7d932dfd JK |
440 | s->cmos_data[RTC_REG_C] |= REG_C_IRQF; |
441 | qemu_irq_raise(s->irq); | |
dff38e7b FB |
442 | } |
443 | ||
444 | /* clear update in progress bit */ | |
445 | s->cmos_data[RTC_REG_A] &= ~REG_A_UIP; | |
446 | ||
6ee093c9 | 447 | s->next_second_time += get_ticks_per_sec(); |
dff38e7b | 448 | qemu_mod_timer(s->second_timer, s->next_second_time); |
80cabfad FB |
449 | } |
450 | ||
b41a2cd1 | 451 | static uint32_t cmos_ioport_read(void *opaque, uint32_t addr) |
80cabfad | 452 | { |
b41a2cd1 | 453 | RTCState *s = opaque; |
80cabfad FB |
454 | int ret; |
455 | if ((addr & 1) == 0) { | |
456 | return 0xff; | |
457 | } else { | |
458 | switch(s->cmos_index) { | |
459 | case RTC_SECONDS: | |
460 | case RTC_MINUTES: | |
461 | case RTC_HOURS: | |
462 | case RTC_DAY_OF_WEEK: | |
463 | case RTC_DAY_OF_MONTH: | |
464 | case RTC_MONTH: | |
465 | case RTC_YEAR: | |
80cabfad FB |
466 | ret = s->cmos_data[s->cmos_index]; |
467 | break; | |
468 | case RTC_REG_A: | |
469 | ret = s->cmos_data[s->cmos_index]; | |
80cabfad FB |
470 | break; |
471 | case RTC_REG_C: | |
472 | ret = s->cmos_data[s->cmos_index]; | |
d537cf6c | 473 | qemu_irq_lower(s->irq); |
ba32edab GN |
474 | #ifdef TARGET_I386 |
475 | if(s->irq_coalesced && | |
476 | s->irq_reinject_on_ack_count < RTC_REINJECT_ON_ACK_COUNT) { | |
477 | s->irq_reinject_on_ack_count++; | |
478 | apic_reset_irq_delivered(); | |
aa6f63ff | 479 | DPRINTF_C("cmos: injecting on ack\n"); |
ba32edab | 480 | qemu_irq_raise(s->irq); |
aa6f63ff | 481 | if (apic_get_irq_delivered()) { |
ba32edab | 482 | s->irq_coalesced--; |
aa6f63ff BS |
483 | DPRINTF_C("cmos: coalesced irqs decreased to %d\n", |
484 | s->irq_coalesced); | |
485 | } | |
ba32edab GN |
486 | break; |
487 | } | |
488 | #endif | |
489 | ||
5fafdf24 | 490 | s->cmos_data[RTC_REG_C] = 0x00; |
80cabfad FB |
491 | break; |
492 | default: | |
493 | ret = s->cmos_data[s->cmos_index]; | |
494 | break; | |
495 | } | |
ec51e364 IY |
496 | CMOS_DPRINTF("cmos: read index=0x%02x val=0x%02x\n", |
497 | s->cmos_index, ret); | |
80cabfad FB |
498 | return ret; |
499 | } | |
500 | } | |
501 | ||
1d914fa0 | 502 | void rtc_set_memory(ISADevice *dev, int addr, int val) |
dff38e7b | 503 | { |
1d914fa0 | 504 | RTCState *s = DO_UPCAST(RTCState, dev, dev); |
dff38e7b FB |
505 | if (addr >= 0 && addr <= 127) |
506 | s->cmos_data[addr] = val; | |
507 | } | |
508 | ||
1d914fa0 | 509 | void rtc_set_date(ISADevice *dev, const struct tm *tm) |
dff38e7b | 510 | { |
1d914fa0 | 511 | RTCState *s = DO_UPCAST(RTCState, dev, dev); |
43f493af | 512 | s->current_tm = *tm; |
dff38e7b FB |
513 | rtc_copy_date(s); |
514 | } | |
515 | ||
ea55ffb3 TS |
516 | /* PC cmos mappings */ |
517 | #define REG_IBM_CENTURY_BYTE 0x32 | |
518 | #define REG_IBM_PS2_CENTURY_BYTE 0x37 | |
519 | ||
1d914fa0 | 520 | static void rtc_set_date_from_host(ISADevice *dev) |
ea55ffb3 | 521 | { |
1d914fa0 | 522 | RTCState *s = DO_UPCAST(RTCState, dev, dev); |
f6503059 | 523 | struct tm tm; |
ea55ffb3 TS |
524 | int val; |
525 | ||
526 | /* set the CMOS date */ | |
f6503059 | 527 | qemu_get_timedate(&tm, 0); |
1d914fa0 | 528 | rtc_set_date(dev, &tm); |
ea55ffb3 | 529 | |
abd0c6bd | 530 | val = rtc_to_bcd(s, (tm.tm_year / 100) + 19); |
1d914fa0 IY |
531 | rtc_set_memory(dev, REG_IBM_CENTURY_BYTE, val); |
532 | rtc_set_memory(dev, REG_IBM_PS2_CENTURY_BYTE, val); | |
ea55ffb3 TS |
533 | } |
534 | ||
6b075b8a | 535 | static int rtc_post_load(void *opaque, int version_id) |
80cabfad | 536 | { |
6b075b8a | 537 | #ifdef TARGET_I386 |
dff38e7b FB |
538 | RTCState *s = opaque; |
539 | ||
048c74c4 | 540 | if (version_id >= 2) { |
048c74c4 JQ |
541 | if (rtc_td_hack) { |
542 | rtc_coalesced_timer_update(s); | |
543 | } | |
048c74c4 | 544 | } |
6b075b8a | 545 | #endif |
73822ec8 AL |
546 | return 0; |
547 | } | |
73822ec8 | 548 | |
6b075b8a JQ |
549 | static const VMStateDescription vmstate_rtc = { |
550 | .name = "mc146818rtc", | |
551 | .version_id = 2, | |
552 | .minimum_version_id = 1, | |
553 | .minimum_version_id_old = 1, | |
554 | .post_load = rtc_post_load, | |
555 | .fields = (VMStateField []) { | |
556 | VMSTATE_BUFFER(cmos_data, RTCState), | |
557 | VMSTATE_UINT8(cmos_index, RTCState), | |
558 | VMSTATE_INT32(current_tm.tm_sec, RTCState), | |
559 | VMSTATE_INT32(current_tm.tm_min, RTCState), | |
560 | VMSTATE_INT32(current_tm.tm_hour, RTCState), | |
561 | VMSTATE_INT32(current_tm.tm_wday, RTCState), | |
562 | VMSTATE_INT32(current_tm.tm_mday, RTCState), | |
563 | VMSTATE_INT32(current_tm.tm_mon, RTCState), | |
564 | VMSTATE_INT32(current_tm.tm_year, RTCState), | |
565 | VMSTATE_TIMER(periodic_timer, RTCState), | |
566 | VMSTATE_INT64(next_periodic_time, RTCState), | |
567 | VMSTATE_INT64(next_second_time, RTCState), | |
568 | VMSTATE_TIMER(second_timer, RTCState), | |
569 | VMSTATE_TIMER(second_timer2, RTCState), | |
570 | VMSTATE_UINT32_V(irq_coalesced, RTCState, 2), | |
571 | VMSTATE_UINT32_V(period, RTCState, 2), | |
572 | VMSTATE_END_OF_LIST() | |
573 | } | |
574 | }; | |
575 | ||
17604dac JK |
576 | static void rtc_notify_clock_reset(Notifier *notifier, void *data) |
577 | { | |
578 | RTCState *s = container_of(notifier, RTCState, clock_reset_notifier); | |
579 | int64_t now = *(int64_t *)data; | |
580 | ||
581 | rtc_set_date_from_host(&s->dev); | |
582 | s->next_second_time = now + (get_ticks_per_sec() * 99) / 100; | |
583 | qemu_mod_timer(s->second_timer2, s->next_second_time); | |
584 | rtc_timer_update(s, now); | |
585 | #ifdef TARGET_I386 | |
586 | if (rtc_td_hack) { | |
587 | rtc_coalesced_timer_update(s); | |
588 | } | |
589 | #endif | |
590 | } | |
591 | ||
eeb7c03c GN |
592 | static void rtc_reset(void *opaque) |
593 | { | |
594 | RTCState *s = opaque; | |
595 | ||
72716184 AL |
596 | s->cmos_data[RTC_REG_B] &= ~(REG_B_PIE | REG_B_AIE | REG_B_SQWE); |
597 | s->cmos_data[RTC_REG_C] &= ~(REG_C_UF | REG_C_IRQF | REG_C_PF | REG_C_AF); | |
eeb7c03c | 598 | |
72716184 | 599 | qemu_irq_lower(s->irq); |
eeb7c03c GN |
600 | |
601 | #ifdef TARGET_I386 | |
602 | if (rtc_td_hack) | |
603 | s->irq_coalesced = 0; | |
604 | #endif | |
605 | } | |
606 | ||
32e0c826 | 607 | static int rtc_initfn(ISADevice *dev) |
dff38e7b | 608 | { |
32e0c826 GH |
609 | RTCState *s = DO_UPCAST(RTCState, dev, dev); |
610 | int base = 0x70; | |
80cabfad | 611 | |
80cabfad FB |
612 | s->cmos_data[RTC_REG_A] = 0x26; |
613 | s->cmos_data[RTC_REG_B] = 0x02; | |
614 | s->cmos_data[RTC_REG_C] = 0x00; | |
615 | s->cmos_data[RTC_REG_D] = 0x80; | |
616 | ||
1d914fa0 | 617 | rtc_set_date_from_host(dev); |
ea55ffb3 | 618 | |
74475455 | 619 | s->periodic_timer = qemu_new_timer_ns(rtc_clock, rtc_periodic_timer, s); |
93b66569 AL |
620 | #ifdef TARGET_I386 |
621 | if (rtc_td_hack) | |
6875204c | 622 | s->coalesced_timer = |
74475455 | 623 | qemu_new_timer_ns(rtc_clock, rtc_coalesced_timer, s); |
93b66569 | 624 | #endif |
74475455 PB |
625 | s->second_timer = qemu_new_timer_ns(rtc_clock, rtc_update_second, s); |
626 | s->second_timer2 = qemu_new_timer_ns(rtc_clock, rtc_update_second2, s); | |
dff38e7b | 627 | |
17604dac JK |
628 | s->clock_reset_notifier.notify = rtc_notify_clock_reset; |
629 | qemu_register_clock_reset_notifier(rtc_clock, &s->clock_reset_notifier); | |
630 | ||
6875204c | 631 | s->next_second_time = |
74475455 | 632 | qemu_get_clock_ns(rtc_clock) + (get_ticks_per_sec() * 99) / 100; |
dff38e7b FB |
633 | qemu_mod_timer(s->second_timer2, s->next_second_time); |
634 | ||
b41a2cd1 FB |
635 | register_ioport_write(base, 2, 1, cmos_ioport_write, s); |
636 | register_ioport_read(base, 2, 1, cmos_ioport_read, s); | |
dee41d58 | 637 | isa_init_ioport_range(dev, base, 2); |
dff38e7b | 638 | |
dc683910 | 639 | qdev_set_legacy_instance_id(&dev->qdev, base, 2); |
a08d4367 | 640 | qemu_register_reset(rtc_reset, s); |
32e0c826 GH |
641 | return 0; |
642 | } | |
643 | ||
7d932dfd | 644 | ISADevice *rtc_init(int base_year, qemu_irq intercept_irq) |
32e0c826 GH |
645 | { |
646 | ISADevice *dev; | |
7d932dfd | 647 | RTCState *s; |
eeb7c03c | 648 | |
32e0c826 | 649 | dev = isa_create("mc146818rtc"); |
7d932dfd | 650 | s = DO_UPCAST(RTCState, dev, dev); |
32e0c826 | 651 | qdev_prop_set_int32(&dev->qdev, "base_year", base_year); |
e23a1b33 | 652 | qdev_init_nofail(&dev->qdev); |
7d932dfd JK |
653 | if (intercept_irq) { |
654 | s->irq = intercept_irq; | |
655 | } else { | |
656 | isa_init_irq(dev, &s->irq, RTC_ISA_IRQ); | |
657 | } | |
1d914fa0 | 658 | return dev; |
80cabfad FB |
659 | } |
660 | ||
32e0c826 GH |
661 | static ISADeviceInfo mc146818rtc_info = { |
662 | .qdev.name = "mc146818rtc", | |
663 | .qdev.size = sizeof(RTCState), | |
664 | .qdev.no_user = 1, | |
dc683910 | 665 | .qdev.vmsd = &vmstate_rtc, |
32e0c826 GH |
666 | .init = rtc_initfn, |
667 | .qdev.props = (Property[]) { | |
668 | DEFINE_PROP_INT32("base_year", RTCState, base_year, 1980), | |
669 | DEFINE_PROP_END_OF_LIST(), | |
670 | } | |
671 | }; | |
672 | ||
673 | static void mc146818rtc_register(void) | |
100d9891 | 674 | { |
32e0c826 | 675 | isa_qdev_register(&mc146818rtc_info); |
100d9891 | 676 | } |
32e0c826 | 677 | device_init(mc146818rtc_register) |