root / hw / mc146818rtc.c @ 4677d8ed
History | View | Annotate | Download (17.8 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 | uint32_t irq_coalesced; |
80 | 73822ec8 | aliguori | uint32_t period; |
81 | 93b66569 | aliguori | QEMUTimer *coalesced_timer; |
82 | dff38e7b | bellard | QEMUTimer *second_timer; |
83 | dff38e7b | bellard | QEMUTimer *second_timer2; |
84 | dff38e7b | bellard | }; |
85 | dff38e7b | bellard | |
86 | e0ca7b94 | Juan Quintela | static void rtc_irq_raise(qemu_irq irq) |
87 | e0ca7b94 | Juan Quintela | { |
88 | c50c2d68 | aurel32 | /* When HPET is operating in legacy mode, RTC interrupts are disabled
|
89 | 16b29ae1 | aliguori | * We block qemu_irq_raise, but not qemu_irq_lower, in case legacy
|
90 | c50c2d68 | aurel32 | * mode is established while interrupt is raised. We want it to
|
91 | 16b29ae1 | aliguori | * be lowered in any case
|
92 | c50c2d68 | aurel32 | */
|
93 | ce88f890 | Juan Quintela | #if defined TARGET_I386
|
94 | c50c2d68 | aurel32 | if (!hpet_in_legacy_mode())
|
95 | 16b29ae1 | aliguori | #endif
|
96 | 16b29ae1 | aliguori | qemu_irq_raise(irq); |
97 | 16b29ae1 | aliguori | } |
98 | 16b29ae1 | aliguori | |
99 | dff38e7b | bellard | static void rtc_set_time(RTCState *s); |
100 | dff38e7b | bellard | static void rtc_copy_date(RTCState *s); |
101 | dff38e7b | bellard | |
102 | 93b66569 | aliguori | #ifdef TARGET_I386
|
103 | 93b66569 | aliguori | static void rtc_coalesced_timer_update(RTCState *s) |
104 | 93b66569 | aliguori | { |
105 | 93b66569 | aliguori | if (s->irq_coalesced == 0) { |
106 | 93b66569 | aliguori | qemu_del_timer(s->coalesced_timer); |
107 | 93b66569 | aliguori | } else {
|
108 | 93b66569 | aliguori | /* divide each RTC interval to 2 - 8 smaller intervals */
|
109 | 93b66569 | aliguori | int c = MIN(s->irq_coalesced, 7) + 1; |
110 | 6875204c | Jan Kiszka | int64_t next_clock = qemu_get_clock(rtc_clock) + |
111 | 6875204c | Jan Kiszka | muldiv64(s->period / c, get_ticks_per_sec(), 32768);
|
112 | 93b66569 | aliguori | qemu_mod_timer(s->coalesced_timer, next_clock); |
113 | 93b66569 | aliguori | } |
114 | 93b66569 | aliguori | } |
115 | 93b66569 | aliguori | |
116 | 93b66569 | aliguori | static void rtc_coalesced_timer(void *opaque) |
117 | 93b66569 | aliguori | { |
118 | 93b66569 | aliguori | RTCState *s = opaque; |
119 | 93b66569 | aliguori | |
120 | 93b66569 | aliguori | if (s->irq_coalesced != 0) { |
121 | 93b66569 | aliguori | apic_reset_irq_delivered(); |
122 | 93b66569 | aliguori | s->cmos_data[RTC_REG_C] |= 0xc0;
|
123 | 93b66569 | aliguori | rtc_irq_raise(s->irq); |
124 | 93b66569 | aliguori | if (apic_get_irq_delivered()) {
|
125 | 93b66569 | aliguori | s->irq_coalesced--; |
126 | 93b66569 | aliguori | } |
127 | 93b66569 | aliguori | } |
128 | 93b66569 | aliguori | |
129 | 93b66569 | aliguori | rtc_coalesced_timer_update(s); |
130 | 93b66569 | aliguori | } |
131 | 93b66569 | aliguori | #endif
|
132 | 93b66569 | aliguori | |
133 | dff38e7b | bellard | static void rtc_timer_update(RTCState *s, int64_t current_time) |
134 | dff38e7b | bellard | { |
135 | dff38e7b | bellard | int period_code, period;
|
136 | dff38e7b | bellard | int64_t cur_clock, next_irq_clock; |
137 | 100d9891 | aurel32 | int enable_pie;
|
138 | dff38e7b | bellard | |
139 | dff38e7b | bellard | period_code = s->cmos_data[RTC_REG_A] & 0x0f;
|
140 | ce88f890 | Juan Quintela | #if defined TARGET_I386
|
141 | c50c2d68 | aurel32 | /* disable periodic timer if hpet is in legacy mode, since interrupts are
|
142 | 16b29ae1 | aliguori | * disabled anyway.
|
143 | 16b29ae1 | aliguori | */
|
144 | a8b01dd8 | pbrook | enable_pie = !hpet_in_legacy_mode(); |
145 | 16b29ae1 | aliguori | #else
|
146 | 100d9891 | aurel32 | enable_pie = 1;
|
147 | 16b29ae1 | aliguori | #endif
|
148 | 100d9891 | aurel32 | if (period_code != 0 |
149 | 100d9891 | aurel32 | && (((s->cmos_data[RTC_REG_B] & REG_B_PIE) && enable_pie) |
150 | 100d9891 | aurel32 | || ((s->cmos_data[RTC_REG_B] & REG_B_SQWE) && s->sqw_irq))) { |
151 | dff38e7b | bellard | if (period_code <= 2) |
152 | dff38e7b | bellard | period_code += 7;
|
153 | dff38e7b | bellard | /* period in 32 Khz cycles */
|
154 | dff38e7b | bellard | period = 1 << (period_code - 1); |
155 | 73822ec8 | aliguori | #ifdef TARGET_I386
|
156 | 73822ec8 | aliguori | if(period != s->period)
|
157 | 73822ec8 | aliguori | s->irq_coalesced = (s->irq_coalesced * s->period) / period; |
158 | 73822ec8 | aliguori | s->period = period; |
159 | 73822ec8 | aliguori | #endif
|
160 | dff38e7b | bellard | /* compute 32 khz clock */
|
161 | 6ee093c9 | Juan Quintela | cur_clock = muldiv64(current_time, 32768, get_ticks_per_sec());
|
162 | dff38e7b | bellard | next_irq_clock = (cur_clock & ~(period - 1)) + period;
|
163 | 6875204c | Jan Kiszka | s->next_periodic_time = |
164 | 6875204c | Jan Kiszka | muldiv64(next_irq_clock, get_ticks_per_sec(), 32768) + 1; |
165 | dff38e7b | bellard | qemu_mod_timer(s->periodic_timer, s->next_periodic_time); |
166 | dff38e7b | bellard | } else {
|
167 | 73822ec8 | aliguori | #ifdef TARGET_I386
|
168 | 73822ec8 | aliguori | s->irq_coalesced = 0;
|
169 | 73822ec8 | aliguori | #endif
|
170 | dff38e7b | bellard | qemu_del_timer(s->periodic_timer); |
171 | dff38e7b | bellard | } |
172 | dff38e7b | bellard | } |
173 | dff38e7b | bellard | |
174 | dff38e7b | bellard | static void rtc_periodic_timer(void *opaque) |
175 | dff38e7b | bellard | { |
176 | dff38e7b | bellard | RTCState *s = opaque; |
177 | dff38e7b | bellard | |
178 | dff38e7b | bellard | rtc_timer_update(s, s->next_periodic_time); |
179 | 100d9891 | aurel32 | if (s->cmos_data[RTC_REG_B] & REG_B_PIE) {
|
180 | 100d9891 | aurel32 | s->cmos_data[RTC_REG_C] |= 0xc0;
|
181 | 93b66569 | aliguori | #ifdef TARGET_I386
|
182 | 93b66569 | aliguori | if(rtc_td_hack) {
|
183 | 93b66569 | aliguori | apic_reset_irq_delivered(); |
184 | 93b66569 | aliguori | rtc_irq_raise(s->irq); |
185 | 93b66569 | aliguori | if (!apic_get_irq_delivered()) {
|
186 | 93b66569 | aliguori | s->irq_coalesced++; |
187 | 93b66569 | aliguori | rtc_coalesced_timer_update(s); |
188 | 93b66569 | aliguori | } |
189 | 93b66569 | aliguori | } else
|
190 | 93b66569 | aliguori | #endif
|
191 | 100d9891 | aurel32 | rtc_irq_raise(s->irq); |
192 | 100d9891 | aurel32 | } |
193 | 100d9891 | aurel32 | if (s->cmos_data[RTC_REG_B] & REG_B_SQWE) {
|
194 | 100d9891 | aurel32 | /* Not square wave at all but we don't want 2048Hz interrupts!
|
195 | 100d9891 | aurel32 | Must be seen as a pulse. */
|
196 | 100d9891 | aurel32 | qemu_irq_raise(s->sqw_irq); |
197 | 100d9891 | aurel32 | } |
198 | dff38e7b | bellard | } |
199 | 80cabfad | bellard | |
200 | b41a2cd1 | bellard | static void cmos_ioport_write(void *opaque, uint32_t addr, uint32_t data) |
201 | 80cabfad | bellard | { |
202 | b41a2cd1 | bellard | RTCState *s = opaque; |
203 | 80cabfad | bellard | |
204 | 80cabfad | bellard | if ((addr & 1) == 0) { |
205 | 80cabfad | bellard | s->cmos_index = data & 0x7f;
|
206 | 80cabfad | bellard | } else {
|
207 | 80cabfad | bellard | #ifdef DEBUG_CMOS
|
208 | 80cabfad | bellard | printf("cmos: write index=0x%02x val=0x%02x\n",
|
209 | 80cabfad | bellard | s->cmos_index, data); |
210 | 3b46e624 | ths | #endif
|
211 | dff38e7b | bellard | switch(s->cmos_index) {
|
212 | 80cabfad | bellard | case RTC_SECONDS_ALARM:
|
213 | 80cabfad | bellard | case RTC_MINUTES_ALARM:
|
214 | 80cabfad | bellard | case RTC_HOURS_ALARM:
|
215 | 80cabfad | bellard | /* XXX: not supported */
|
216 | 80cabfad | bellard | s->cmos_data[s->cmos_index] = data; |
217 | 80cabfad | bellard | break;
|
218 | 80cabfad | bellard | case RTC_SECONDS:
|
219 | 80cabfad | bellard | case RTC_MINUTES:
|
220 | 80cabfad | bellard | case RTC_HOURS:
|
221 | 80cabfad | bellard | case RTC_DAY_OF_WEEK:
|
222 | 80cabfad | bellard | case RTC_DAY_OF_MONTH:
|
223 | 80cabfad | bellard | case RTC_MONTH:
|
224 | 80cabfad | bellard | case RTC_YEAR:
|
225 | 80cabfad | bellard | s->cmos_data[s->cmos_index] = data; |
226 | dff38e7b | bellard | /* if in set mode, do not update the time */
|
227 | dff38e7b | bellard | if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
|
228 | dff38e7b | bellard | rtc_set_time(s); |
229 | dff38e7b | bellard | } |
230 | 80cabfad | bellard | break;
|
231 | 80cabfad | bellard | case RTC_REG_A:
|
232 | dff38e7b | bellard | /* UIP bit is read only */
|
233 | dff38e7b | bellard | s->cmos_data[RTC_REG_A] = (data & ~REG_A_UIP) | |
234 | dff38e7b | bellard | (s->cmos_data[RTC_REG_A] & REG_A_UIP); |
235 | 6875204c | Jan Kiszka | rtc_timer_update(s, qemu_get_clock(rtc_clock)); |
236 | dff38e7b | bellard | break;
|
237 | 80cabfad | bellard | case RTC_REG_B:
|
238 | dff38e7b | bellard | if (data & REG_B_SET) {
|
239 | dff38e7b | bellard | /* set mode: reset UIP mode */
|
240 | dff38e7b | bellard | s->cmos_data[RTC_REG_A] &= ~REG_A_UIP; |
241 | dff38e7b | bellard | data &= ~REG_B_UIE; |
242 | dff38e7b | bellard | } else {
|
243 | dff38e7b | bellard | /* if disabling set mode, update the time */
|
244 | dff38e7b | bellard | if (s->cmos_data[RTC_REG_B] & REG_B_SET) {
|
245 | dff38e7b | bellard | rtc_set_time(s); |
246 | dff38e7b | bellard | } |
247 | dff38e7b | bellard | } |
248 | dff38e7b | bellard | s->cmos_data[RTC_REG_B] = data; |
249 | 6875204c | Jan Kiszka | rtc_timer_update(s, qemu_get_clock(rtc_clock)); |
250 | 80cabfad | bellard | break;
|
251 | 80cabfad | bellard | case RTC_REG_C:
|
252 | 80cabfad | bellard | case RTC_REG_D:
|
253 | 80cabfad | bellard | /* cannot write to them */
|
254 | 80cabfad | bellard | break;
|
255 | 80cabfad | bellard | default:
|
256 | 80cabfad | bellard | s->cmos_data[s->cmos_index] = data; |
257 | 80cabfad | bellard | break;
|
258 | 80cabfad | bellard | } |
259 | 80cabfad | bellard | } |
260 | 80cabfad | bellard | } |
261 | 80cabfad | bellard | |
262 | abd0c6bd | Paul Brook | static inline int rtc_to_bcd(RTCState *s, int a) |
263 | 80cabfad | bellard | { |
264 | 6f1bf24d | aurel32 | if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
|
265 | dff38e7b | bellard | return a;
|
266 | dff38e7b | bellard | } else {
|
267 | dff38e7b | bellard | return ((a / 10) << 4) | (a % 10); |
268 | dff38e7b | bellard | } |
269 | 80cabfad | bellard | } |
270 | 80cabfad | bellard | |
271 | abd0c6bd | Paul Brook | static inline int rtc_from_bcd(RTCState *s, int a) |
272 | 80cabfad | bellard | { |
273 | 6f1bf24d | aurel32 | if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
|
274 | dff38e7b | bellard | return a;
|
275 | dff38e7b | bellard | } else {
|
276 | dff38e7b | bellard | return ((a >> 4) * 10) + (a & 0x0f); |
277 | dff38e7b | bellard | } |
278 | dff38e7b | bellard | } |
279 | dff38e7b | bellard | |
280 | dff38e7b | bellard | static void rtc_set_time(RTCState *s) |
281 | dff38e7b | bellard | { |
282 | 43f493af | bellard | struct tm *tm = &s->current_tm;
|
283 | dff38e7b | bellard | |
284 | abd0c6bd | Paul Brook | tm->tm_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS]); |
285 | abd0c6bd | Paul Brook | tm->tm_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES]); |
286 | abd0c6bd | Paul Brook | tm->tm_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS] & 0x7f);
|
287 | 43f493af | bellard | if (!(s->cmos_data[RTC_REG_B] & 0x02) && |
288 | 43f493af | bellard | (s->cmos_data[RTC_HOURS] & 0x80)) {
|
289 | 43f493af | bellard | tm->tm_hour += 12;
|
290 | 43f493af | bellard | } |
291 | abd0c6bd | Paul Brook | tm->tm_wday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_WEEK]) - 1;
|
292 | abd0c6bd | Paul Brook | tm->tm_mday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_MONTH]); |
293 | abd0c6bd | Paul Brook | tm->tm_mon = rtc_from_bcd(s, s->cmos_data[RTC_MONTH]) - 1;
|
294 | abd0c6bd | Paul Brook | tm->tm_year = rtc_from_bcd(s, s->cmos_data[RTC_YEAR]) + s->base_year - 1900;
|
295 | 43f493af | bellard | } |
296 | 43f493af | bellard | |
297 | 43f493af | bellard | static void rtc_copy_date(RTCState *s) |
298 | 43f493af | bellard | { |
299 | 43f493af | bellard | const struct tm *tm = &s->current_tm; |
300 | 42fc73a1 | aurel32 | int year;
|
301 | dff38e7b | bellard | |
302 | abd0c6bd | Paul Brook | s->cmos_data[RTC_SECONDS] = rtc_to_bcd(s, tm->tm_sec); |
303 | abd0c6bd | Paul Brook | s->cmos_data[RTC_MINUTES] = rtc_to_bcd(s, tm->tm_min); |
304 | 43f493af | bellard | if (s->cmos_data[RTC_REG_B] & 0x02) { |
305 | 43f493af | bellard | /* 24 hour format */
|
306 | abd0c6bd | Paul Brook | s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, tm->tm_hour); |
307 | 43f493af | bellard | } else {
|
308 | 43f493af | bellard | /* 12 hour format */
|
309 | abd0c6bd | Paul Brook | s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, tm->tm_hour % 12);
|
310 | 43f493af | bellard | if (tm->tm_hour >= 12) |
311 | 43f493af | bellard | s->cmos_data[RTC_HOURS] |= 0x80;
|
312 | 43f493af | bellard | } |
313 | abd0c6bd | Paul Brook | s->cmos_data[RTC_DAY_OF_WEEK] = rtc_to_bcd(s, tm->tm_wday + 1);
|
314 | abd0c6bd | Paul Brook | s->cmos_data[RTC_DAY_OF_MONTH] = rtc_to_bcd(s, tm->tm_mday); |
315 | abd0c6bd | Paul Brook | s->cmos_data[RTC_MONTH] = rtc_to_bcd(s, tm->tm_mon + 1);
|
316 | 42fc73a1 | aurel32 | year = (tm->tm_year - s->base_year) % 100;
|
317 | 42fc73a1 | aurel32 | if (year < 0) |
318 | 42fc73a1 | aurel32 | year += 100;
|
319 | abd0c6bd | Paul Brook | s->cmos_data[RTC_YEAR] = rtc_to_bcd(s, year); |
320 | 43f493af | bellard | } |
321 | 43f493af | bellard | |
322 | 43f493af | bellard | /* month is between 0 and 11. */
|
323 | 43f493af | bellard | static int get_days_in_month(int month, int year) |
324 | 43f493af | bellard | { |
325 | 5fafdf24 | ths | static const int days_tab[12] = { |
326 | 5fafdf24 | ths | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
327 | 43f493af | bellard | }; |
328 | 43f493af | bellard | int d;
|
329 | 43f493af | bellard | if ((unsigned )month >= 12) |
330 | 43f493af | bellard | return 31; |
331 | 43f493af | bellard | d = days_tab[month]; |
332 | 43f493af | bellard | if (month == 1) { |
333 | 43f493af | bellard | if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0)) |
334 | 43f493af | bellard | d++; |
335 | 43f493af | bellard | } |
336 | 43f493af | bellard | return d;
|
337 | 43f493af | bellard | } |
338 | 43f493af | bellard | |
339 | 43f493af | bellard | /* update 'tm' to the next second */
|
340 | 43f493af | bellard | static void rtc_next_second(struct tm *tm) |
341 | 43f493af | bellard | { |
342 | 43f493af | bellard | int days_in_month;
|
343 | 43f493af | bellard | |
344 | 43f493af | bellard | tm->tm_sec++; |
345 | 43f493af | bellard | if ((unsigned)tm->tm_sec >= 60) { |
346 | 43f493af | bellard | tm->tm_sec = 0;
|
347 | 43f493af | bellard | tm->tm_min++; |
348 | 43f493af | bellard | if ((unsigned)tm->tm_min >= 60) { |
349 | 43f493af | bellard | tm->tm_min = 0;
|
350 | 43f493af | bellard | tm->tm_hour++; |
351 | 43f493af | bellard | if ((unsigned)tm->tm_hour >= 24) { |
352 | 43f493af | bellard | tm->tm_hour = 0;
|
353 | 43f493af | bellard | /* next day */
|
354 | 43f493af | bellard | tm->tm_wday++; |
355 | 43f493af | bellard | if ((unsigned)tm->tm_wday >= 7) |
356 | 43f493af | bellard | tm->tm_wday = 0;
|
357 | 5fafdf24 | ths | days_in_month = get_days_in_month(tm->tm_mon, |
358 | 43f493af | bellard | tm->tm_year + 1900);
|
359 | 43f493af | bellard | tm->tm_mday++; |
360 | 43f493af | bellard | if (tm->tm_mday < 1) { |
361 | 43f493af | bellard | tm->tm_mday = 1;
|
362 | 43f493af | bellard | } else if (tm->tm_mday > days_in_month) { |
363 | 43f493af | bellard | tm->tm_mday = 1;
|
364 | 43f493af | bellard | tm->tm_mon++; |
365 | 43f493af | bellard | if (tm->tm_mon >= 12) { |
366 | 43f493af | bellard | tm->tm_mon = 0;
|
367 | 43f493af | bellard | tm->tm_year++; |
368 | 43f493af | bellard | } |
369 | 43f493af | bellard | } |
370 | 43f493af | bellard | } |
371 | 43f493af | bellard | } |
372 | 43f493af | bellard | } |
373 | dff38e7b | bellard | } |
374 | dff38e7b | bellard | |
375 | 43f493af | bellard | |
376 | dff38e7b | bellard | static void rtc_update_second(void *opaque) |
377 | dff38e7b | bellard | { |
378 | dff38e7b | bellard | RTCState *s = opaque; |
379 | 4721c457 | bellard | int64_t delay; |
380 | dff38e7b | bellard | |
381 | dff38e7b | bellard | /* if the oscillator is not in normal operation, we do not update */
|
382 | dff38e7b | bellard | if ((s->cmos_data[RTC_REG_A] & 0x70) != 0x20) { |
383 | 6ee093c9 | Juan Quintela | s->next_second_time += get_ticks_per_sec(); |
384 | dff38e7b | bellard | qemu_mod_timer(s->second_timer, s->next_second_time); |
385 | dff38e7b | bellard | } else {
|
386 | 43f493af | bellard | rtc_next_second(&s->current_tm); |
387 | 3b46e624 | ths | |
388 | dff38e7b | bellard | if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
|
389 | dff38e7b | bellard | /* update in progress bit */
|
390 | dff38e7b | bellard | s->cmos_data[RTC_REG_A] |= REG_A_UIP; |
391 | dff38e7b | bellard | } |
392 | 4721c457 | bellard | /* should be 244 us = 8 / 32768 seconds, but currently the
|
393 | 4721c457 | bellard | timers do not have the necessary resolution. */
|
394 | 6ee093c9 | Juan Quintela | delay = (get_ticks_per_sec() * 1) / 100; |
395 | 4721c457 | bellard | if (delay < 1) |
396 | 4721c457 | bellard | delay = 1;
|
397 | 5fafdf24 | ths | qemu_mod_timer(s->second_timer2, |
398 | 4721c457 | bellard | s->next_second_time + delay); |
399 | dff38e7b | bellard | } |
400 | dff38e7b | bellard | } |
401 | dff38e7b | bellard | |
402 | dff38e7b | bellard | static void rtc_update_second2(void *opaque) |
403 | dff38e7b | bellard | { |
404 | dff38e7b | bellard | RTCState *s = opaque; |
405 | dff38e7b | bellard | |
406 | dff38e7b | bellard | if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
|
407 | dff38e7b | bellard | rtc_copy_date(s); |
408 | dff38e7b | bellard | } |
409 | dff38e7b | bellard | |
410 | dff38e7b | bellard | /* check alarm */
|
411 | dff38e7b | bellard | if (s->cmos_data[RTC_REG_B] & REG_B_AIE) {
|
412 | dff38e7b | bellard | if (((s->cmos_data[RTC_SECONDS_ALARM] & 0xc0) == 0xc0 || |
413 | 43f493af | bellard | s->cmos_data[RTC_SECONDS_ALARM] == s->current_tm.tm_sec) && |
414 | dff38e7b | bellard | ((s->cmos_data[RTC_MINUTES_ALARM] & 0xc0) == 0xc0 || |
415 | 43f493af | bellard | s->cmos_data[RTC_MINUTES_ALARM] == s->current_tm.tm_mon) && |
416 | dff38e7b | bellard | ((s->cmos_data[RTC_HOURS_ALARM] & 0xc0) == 0xc0 || |
417 | 43f493af | bellard | s->cmos_data[RTC_HOURS_ALARM] == s->current_tm.tm_hour)) { |
418 | dff38e7b | bellard | |
419 | 5fafdf24 | ths | s->cmos_data[RTC_REG_C] |= 0xa0;
|
420 | 16b29ae1 | aliguori | rtc_irq_raise(s->irq); |
421 | dff38e7b | bellard | } |
422 | dff38e7b | bellard | } |
423 | dff38e7b | bellard | |
424 | dff38e7b | bellard | /* update ended interrupt */
|
425 | 98815437 | Bernhard Kauer | s->cmos_data[RTC_REG_C] |= REG_C_UF; |
426 | dff38e7b | bellard | if (s->cmos_data[RTC_REG_B] & REG_B_UIE) {
|
427 | 98815437 | Bernhard Kauer | s->cmos_data[RTC_REG_C] |= REG_C_IRQF; |
428 | 98815437 | Bernhard Kauer | rtc_irq_raise(s->irq); |
429 | dff38e7b | bellard | } |
430 | dff38e7b | bellard | |
431 | dff38e7b | bellard | /* clear update in progress bit */
|
432 | dff38e7b | bellard | s->cmos_data[RTC_REG_A] &= ~REG_A_UIP; |
433 | dff38e7b | bellard | |
434 | 6ee093c9 | Juan Quintela | s->next_second_time += get_ticks_per_sec(); |
435 | dff38e7b | bellard | qemu_mod_timer(s->second_timer, s->next_second_time); |
436 | 80cabfad | bellard | } |
437 | 80cabfad | bellard | |
438 | b41a2cd1 | bellard | static uint32_t cmos_ioport_read(void *opaque, uint32_t addr) |
439 | 80cabfad | bellard | { |
440 | b41a2cd1 | bellard | RTCState *s = opaque; |
441 | 80cabfad | bellard | int ret;
|
442 | 80cabfad | bellard | if ((addr & 1) == 0) { |
443 | 80cabfad | bellard | return 0xff; |
444 | 80cabfad | bellard | } else {
|
445 | 80cabfad | bellard | switch(s->cmos_index) {
|
446 | 80cabfad | bellard | case RTC_SECONDS:
|
447 | 80cabfad | bellard | case RTC_MINUTES:
|
448 | 80cabfad | bellard | case RTC_HOURS:
|
449 | 80cabfad | bellard | case RTC_DAY_OF_WEEK:
|
450 | 80cabfad | bellard | case RTC_DAY_OF_MONTH:
|
451 | 80cabfad | bellard | case RTC_MONTH:
|
452 | 80cabfad | bellard | case RTC_YEAR:
|
453 | 80cabfad | bellard | ret = s->cmos_data[s->cmos_index]; |
454 | 80cabfad | bellard | break;
|
455 | 80cabfad | bellard | case RTC_REG_A:
|
456 | 80cabfad | bellard | ret = s->cmos_data[s->cmos_index]; |
457 | 80cabfad | bellard | break;
|
458 | 80cabfad | bellard | case RTC_REG_C:
|
459 | 80cabfad | bellard | ret = s->cmos_data[s->cmos_index]; |
460 | d537cf6c | pbrook | qemu_irq_lower(s->irq); |
461 | 5fafdf24 | ths | s->cmos_data[RTC_REG_C] = 0x00;
|
462 | 80cabfad | bellard | break;
|
463 | 80cabfad | bellard | default:
|
464 | 80cabfad | bellard | ret = s->cmos_data[s->cmos_index]; |
465 | 80cabfad | bellard | break;
|
466 | 80cabfad | bellard | } |
467 | 80cabfad | bellard | #ifdef DEBUG_CMOS
|
468 | 80cabfad | bellard | printf("cmos: read index=0x%02x val=0x%02x\n",
|
469 | 80cabfad | bellard | s->cmos_index, ret); |
470 | 80cabfad | bellard | #endif
|
471 | 80cabfad | bellard | return ret;
|
472 | 80cabfad | bellard | } |
473 | 80cabfad | bellard | } |
474 | 80cabfad | bellard | |
475 | dff38e7b | bellard | void rtc_set_memory(RTCState *s, int addr, int val) |
476 | dff38e7b | bellard | { |
477 | dff38e7b | bellard | if (addr >= 0 && addr <= 127) |
478 | dff38e7b | bellard | s->cmos_data[addr] = val; |
479 | dff38e7b | bellard | } |
480 | dff38e7b | bellard | |
481 | dff38e7b | bellard | void rtc_set_date(RTCState *s, const struct tm *tm) |
482 | dff38e7b | bellard | { |
483 | 43f493af | bellard | s->current_tm = *tm; |
484 | dff38e7b | bellard | rtc_copy_date(s); |
485 | dff38e7b | bellard | } |
486 | dff38e7b | bellard | |
487 | ea55ffb3 | ths | /* PC cmos mappings */
|
488 | ea55ffb3 | ths | #define REG_IBM_CENTURY_BYTE 0x32 |
489 | ea55ffb3 | ths | #define REG_IBM_PS2_CENTURY_BYTE 0x37 |
490 | ea55ffb3 | ths | |
491 | 9596ebb7 | pbrook | static void rtc_set_date_from_host(RTCState *s) |
492 | ea55ffb3 | ths | { |
493 | f6503059 | balrog | struct tm tm;
|
494 | ea55ffb3 | ths | int val;
|
495 | ea55ffb3 | ths | |
496 | ea55ffb3 | ths | /* set the CMOS date */
|
497 | f6503059 | balrog | qemu_get_timedate(&tm, 0);
|
498 | f6503059 | balrog | rtc_set_date(s, &tm); |
499 | ea55ffb3 | ths | |
500 | abd0c6bd | Paul Brook | val = rtc_to_bcd(s, (tm.tm_year / 100) + 19); |
501 | ea55ffb3 | ths | rtc_set_memory(s, REG_IBM_CENTURY_BYTE, val); |
502 | ea55ffb3 | ths | rtc_set_memory(s, REG_IBM_PS2_CENTURY_BYTE, val); |
503 | ea55ffb3 | ths | } |
504 | ea55ffb3 | ths | |
505 | 6b075b8a | Juan Quintela | static int rtc_post_load(void *opaque, int version_id) |
506 | 80cabfad | bellard | { |
507 | 6b075b8a | Juan Quintela | #ifdef TARGET_I386
|
508 | dff38e7b | bellard | RTCState *s = opaque; |
509 | dff38e7b | bellard | |
510 | 048c74c4 | Juan Quintela | if (version_id >= 2) { |
511 | 048c74c4 | Juan Quintela | if (rtc_td_hack) {
|
512 | 048c74c4 | Juan Quintela | rtc_coalesced_timer_update(s); |
513 | 048c74c4 | Juan Quintela | } |
514 | 048c74c4 | Juan Quintela | } |
515 | 6b075b8a | Juan Quintela | #endif
|
516 | 73822ec8 | aliguori | return 0; |
517 | 73822ec8 | aliguori | } |
518 | 73822ec8 | aliguori | |
519 | 6b075b8a | Juan Quintela | static const VMStateDescription vmstate_rtc = { |
520 | 6b075b8a | Juan Quintela | .name = "mc146818rtc",
|
521 | 6b075b8a | Juan Quintela | .version_id = 2,
|
522 | 6b075b8a | Juan Quintela | .minimum_version_id = 1,
|
523 | 6b075b8a | Juan Quintela | .minimum_version_id_old = 1,
|
524 | 6b075b8a | Juan Quintela | .post_load = rtc_post_load, |
525 | 6b075b8a | Juan Quintela | .fields = (VMStateField []) { |
526 | 6b075b8a | Juan Quintela | VMSTATE_BUFFER(cmos_data, RTCState), |
527 | 6b075b8a | Juan Quintela | VMSTATE_UINT8(cmos_index, RTCState), |
528 | 6b075b8a | Juan Quintela | VMSTATE_INT32(current_tm.tm_sec, RTCState), |
529 | 6b075b8a | Juan Quintela | VMSTATE_INT32(current_tm.tm_min, RTCState), |
530 | 6b075b8a | Juan Quintela | VMSTATE_INT32(current_tm.tm_hour, RTCState), |
531 | 6b075b8a | Juan Quintela | VMSTATE_INT32(current_tm.tm_wday, RTCState), |
532 | 6b075b8a | Juan Quintela | VMSTATE_INT32(current_tm.tm_mday, RTCState), |
533 | 6b075b8a | Juan Quintela | VMSTATE_INT32(current_tm.tm_mon, RTCState), |
534 | 6b075b8a | Juan Quintela | VMSTATE_INT32(current_tm.tm_year, RTCState), |
535 | 6b075b8a | Juan Quintela | VMSTATE_TIMER(periodic_timer, RTCState), |
536 | 6b075b8a | Juan Quintela | VMSTATE_INT64(next_periodic_time, RTCState), |
537 | 6b075b8a | Juan Quintela | VMSTATE_INT64(next_second_time, RTCState), |
538 | 6b075b8a | Juan Quintela | VMSTATE_TIMER(second_timer, RTCState), |
539 | 6b075b8a | Juan Quintela | VMSTATE_TIMER(second_timer2, RTCState), |
540 | 6b075b8a | Juan Quintela | VMSTATE_UINT32_V(irq_coalesced, RTCState, 2),
|
541 | 6b075b8a | Juan Quintela | VMSTATE_UINT32_V(period, RTCState, 2),
|
542 | 6b075b8a | Juan Quintela | VMSTATE_END_OF_LIST() |
543 | 6b075b8a | Juan Quintela | } |
544 | 6b075b8a | Juan Quintela | }; |
545 | 6b075b8a | Juan Quintela | |
546 | eeb7c03c | Gleb Natapov | static void rtc_reset(void *opaque) |
547 | eeb7c03c | Gleb Natapov | { |
548 | eeb7c03c | Gleb Natapov | RTCState *s = opaque; |
549 | eeb7c03c | Gleb Natapov | |
550 | 72716184 | Anthony Liguori | s->cmos_data[RTC_REG_B] &= ~(REG_B_PIE | REG_B_AIE | REG_B_SQWE); |
551 | 72716184 | Anthony Liguori | s->cmos_data[RTC_REG_C] &= ~(REG_C_UF | REG_C_IRQF | REG_C_PF | REG_C_AF); |
552 | eeb7c03c | Gleb Natapov | |
553 | 72716184 | Anthony Liguori | qemu_irq_lower(s->irq); |
554 | eeb7c03c | Gleb Natapov | |
555 | eeb7c03c | Gleb Natapov | #ifdef TARGET_I386
|
556 | eeb7c03c | Gleb Natapov | if (rtc_td_hack)
|
557 | eeb7c03c | Gleb Natapov | s->irq_coalesced = 0;
|
558 | eeb7c03c | Gleb Natapov | #endif
|
559 | eeb7c03c | Gleb Natapov | } |
560 | eeb7c03c | Gleb Natapov | |
561 | 32e0c826 | Gerd Hoffmann | static int rtc_initfn(ISADevice *dev) |
562 | dff38e7b | bellard | { |
563 | 32e0c826 | Gerd Hoffmann | RTCState *s = DO_UPCAST(RTCState, dev, dev); |
564 | 32e0c826 | Gerd Hoffmann | int base = 0x70; |
565 | 32e0c826 | Gerd Hoffmann | int isairq = 8; |
566 | dff38e7b | bellard | |
567 | 32e0c826 | Gerd Hoffmann | isa_init_irq(dev, &s->irq, isairq); |
568 | 80cabfad | bellard | |
569 | 80cabfad | bellard | s->cmos_data[RTC_REG_A] = 0x26;
|
570 | 80cabfad | bellard | s->cmos_data[RTC_REG_B] = 0x02;
|
571 | 80cabfad | bellard | s->cmos_data[RTC_REG_C] = 0x00;
|
572 | 80cabfad | bellard | s->cmos_data[RTC_REG_D] = 0x80;
|
573 | 80cabfad | bellard | |
574 | ea55ffb3 | ths | rtc_set_date_from_host(s); |
575 | ea55ffb3 | ths | |
576 | 6875204c | Jan Kiszka | s->periodic_timer = qemu_new_timer(rtc_clock, rtc_periodic_timer, s); |
577 | 93b66569 | aliguori | #ifdef TARGET_I386
|
578 | 93b66569 | aliguori | if (rtc_td_hack)
|
579 | 6875204c | Jan Kiszka | s->coalesced_timer = |
580 | 6875204c | Jan Kiszka | qemu_new_timer(rtc_clock, rtc_coalesced_timer, s); |
581 | 93b66569 | aliguori | #endif
|
582 | 6875204c | Jan Kiszka | s->second_timer = qemu_new_timer(rtc_clock, rtc_update_second, s); |
583 | 6875204c | Jan Kiszka | s->second_timer2 = qemu_new_timer(rtc_clock, rtc_update_second2, s); |
584 | dff38e7b | bellard | |
585 | 6875204c | Jan Kiszka | s->next_second_time = |
586 | 6875204c | Jan Kiszka | qemu_get_clock(rtc_clock) + (get_ticks_per_sec() * 99) / 100; |
587 | dff38e7b | bellard | qemu_mod_timer(s->second_timer2, s->next_second_time); |
588 | dff38e7b | bellard | |
589 | b41a2cd1 | bellard | register_ioport_write(base, 2, 1, cmos_ioport_write, s); |
590 | b41a2cd1 | bellard | register_ioport_read(base, 2, 1, cmos_ioport_read, s); |
591 | dff38e7b | bellard | |
592 | 6b075b8a | Juan Quintela | vmstate_register(base, &vmstate_rtc, s); |
593 | a08d4367 | Jan Kiszka | qemu_register_reset(rtc_reset, s); |
594 | 32e0c826 | Gerd Hoffmann | return 0; |
595 | 32e0c826 | Gerd Hoffmann | } |
596 | 32e0c826 | Gerd Hoffmann | |
597 | 32e0c826 | Gerd Hoffmann | RTCState *rtc_init(int base_year)
|
598 | 32e0c826 | Gerd Hoffmann | { |
599 | 32e0c826 | Gerd Hoffmann | ISADevice *dev; |
600 | eeb7c03c | Gleb Natapov | |
601 | 32e0c826 | Gerd Hoffmann | dev = isa_create("mc146818rtc");
|
602 | 32e0c826 | Gerd Hoffmann | qdev_prop_set_int32(&dev->qdev, "base_year", base_year);
|
603 | e23a1b33 | Markus Armbruster | qdev_init_nofail(&dev->qdev); |
604 | 32e0c826 | Gerd Hoffmann | return DO_UPCAST(RTCState, dev, dev);
|
605 | 80cabfad | bellard | } |
606 | 80cabfad | bellard | |
607 | 32e0c826 | Gerd Hoffmann | static ISADeviceInfo mc146818rtc_info = {
|
608 | 32e0c826 | Gerd Hoffmann | .qdev.name = "mc146818rtc",
|
609 | 32e0c826 | Gerd Hoffmann | .qdev.size = sizeof(RTCState),
|
610 | 32e0c826 | Gerd Hoffmann | .qdev.no_user = 1,
|
611 | 32e0c826 | Gerd Hoffmann | .init = rtc_initfn, |
612 | 32e0c826 | Gerd Hoffmann | .qdev.props = (Property[]) { |
613 | 32e0c826 | Gerd Hoffmann | DEFINE_PROP_INT32("base_year", RTCState, base_year, 1980), |
614 | 32e0c826 | Gerd Hoffmann | DEFINE_PROP_END_OF_LIST(), |
615 | 32e0c826 | Gerd Hoffmann | } |
616 | 32e0c826 | Gerd Hoffmann | }; |
617 | 32e0c826 | Gerd Hoffmann | |
618 | 32e0c826 | Gerd Hoffmann | static void mc146818rtc_register(void) |
619 | 100d9891 | aurel32 | { |
620 | 32e0c826 | Gerd Hoffmann | isa_qdev_register(&mc146818rtc_info); |
621 | 100d9891 | aurel32 | } |
622 | 32e0c826 | Gerd Hoffmann | device_init(mc146818rtc_register) |