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