root / hw / mc146818rtc.c @ c701b35b
History | View | Annotate | Download (16.9 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 | 80cabfad | bellard | |
30 | 80cabfad | bellard | //#define DEBUG_CMOS
|
31 | 80cabfad | bellard | |
32 | 80cabfad | bellard | #define RTC_SECONDS 0 |
33 | 80cabfad | bellard | #define RTC_SECONDS_ALARM 1 |
34 | 80cabfad | bellard | #define RTC_MINUTES 2 |
35 | 80cabfad | bellard | #define RTC_MINUTES_ALARM 3 |
36 | 80cabfad | bellard | #define RTC_HOURS 4 |
37 | 80cabfad | bellard | #define RTC_HOURS_ALARM 5 |
38 | 80cabfad | bellard | #define RTC_ALARM_DONT_CARE 0xC0 |
39 | 80cabfad | bellard | |
40 | 80cabfad | bellard | #define RTC_DAY_OF_WEEK 6 |
41 | 80cabfad | bellard | #define RTC_DAY_OF_MONTH 7 |
42 | 80cabfad | bellard | #define RTC_MONTH 8 |
43 | 80cabfad | bellard | #define RTC_YEAR 9 |
44 | 80cabfad | bellard | |
45 | 80cabfad | bellard | #define RTC_REG_A 10 |
46 | 80cabfad | bellard | #define RTC_REG_B 11 |
47 | 80cabfad | bellard | #define RTC_REG_C 12 |
48 | 80cabfad | bellard | #define RTC_REG_D 13 |
49 | 80cabfad | bellard | |
50 | dff38e7b | bellard | #define REG_A_UIP 0x80 |
51 | 80cabfad | bellard | |
52 | dff38e7b | bellard | #define REG_B_SET 0x80 |
53 | dff38e7b | bellard | #define REG_B_PIE 0x40 |
54 | dff38e7b | bellard | #define REG_B_AIE 0x20 |
55 | dff38e7b | bellard | #define REG_B_UIE 0x10 |
56 | dff38e7b | bellard | |
57 | dff38e7b | bellard | struct RTCState {
|
58 | dff38e7b | bellard | uint8_t cmos_data[128];
|
59 | dff38e7b | bellard | uint8_t cmos_index; |
60 | 43f493af | bellard | struct tm current_tm;
|
61 | d537cf6c | pbrook | qemu_irq irq; |
62 | 2ca9d013 | ths | target_phys_addr_t base; |
63 | 18c6e2ff | ths | int it_shift;
|
64 | dff38e7b | bellard | /* periodic timer */
|
65 | dff38e7b | bellard | QEMUTimer *periodic_timer; |
66 | dff38e7b | bellard | int64_t next_periodic_time; |
67 | dff38e7b | bellard | /* second update */
|
68 | dff38e7b | bellard | int64_t next_second_time; |
69 | dff38e7b | bellard | QEMUTimer *second_timer; |
70 | dff38e7b | bellard | QEMUTimer *second_timer2; |
71 | dff38e7b | bellard | }; |
72 | dff38e7b | bellard | |
73 | dff38e7b | bellard | static void rtc_set_time(RTCState *s); |
74 | dff38e7b | bellard | static void rtc_copy_date(RTCState *s); |
75 | dff38e7b | bellard | |
76 | dff38e7b | bellard | static void rtc_timer_update(RTCState *s, int64_t current_time) |
77 | dff38e7b | bellard | { |
78 | dff38e7b | bellard | int period_code, period;
|
79 | dff38e7b | bellard | int64_t cur_clock, next_irq_clock; |
80 | dff38e7b | bellard | |
81 | dff38e7b | bellard | period_code = s->cmos_data[RTC_REG_A] & 0x0f;
|
82 | 5fafdf24 | ths | if (period_code != 0 && |
83 | dff38e7b | bellard | (s->cmos_data[RTC_REG_B] & REG_B_PIE)) { |
84 | dff38e7b | bellard | if (period_code <= 2) |
85 | dff38e7b | bellard | period_code += 7;
|
86 | dff38e7b | bellard | /* period in 32 Khz cycles */
|
87 | dff38e7b | bellard | period = 1 << (period_code - 1); |
88 | dff38e7b | bellard | /* compute 32 khz clock */
|
89 | dff38e7b | bellard | cur_clock = muldiv64(current_time, 32768, ticks_per_sec);
|
90 | dff38e7b | bellard | next_irq_clock = (cur_clock & ~(period - 1)) + period;
|
91 | dff38e7b | bellard | s->next_periodic_time = muldiv64(next_irq_clock, ticks_per_sec, 32768) + 1; |
92 | dff38e7b | bellard | qemu_mod_timer(s->periodic_timer, s->next_periodic_time); |
93 | dff38e7b | bellard | } else {
|
94 | dff38e7b | bellard | qemu_del_timer(s->periodic_timer); |
95 | dff38e7b | bellard | } |
96 | dff38e7b | bellard | } |
97 | dff38e7b | bellard | |
98 | dff38e7b | bellard | static void rtc_periodic_timer(void *opaque) |
99 | dff38e7b | bellard | { |
100 | dff38e7b | bellard | RTCState *s = opaque; |
101 | dff38e7b | bellard | |
102 | dff38e7b | bellard | rtc_timer_update(s, s->next_periodic_time); |
103 | dff38e7b | bellard | s->cmos_data[RTC_REG_C] |= 0xc0;
|
104 | d537cf6c | pbrook | qemu_irq_raise(s->irq); |
105 | dff38e7b | bellard | } |
106 | 80cabfad | bellard | |
107 | b41a2cd1 | bellard | static void cmos_ioport_write(void *opaque, uint32_t addr, uint32_t data) |
108 | 80cabfad | bellard | { |
109 | b41a2cd1 | bellard | RTCState *s = opaque; |
110 | 80cabfad | bellard | |
111 | 80cabfad | bellard | if ((addr & 1) == 0) { |
112 | 80cabfad | bellard | s->cmos_index = data & 0x7f;
|
113 | 80cabfad | bellard | } else {
|
114 | 80cabfad | bellard | #ifdef DEBUG_CMOS
|
115 | 80cabfad | bellard | printf("cmos: write index=0x%02x val=0x%02x\n",
|
116 | 80cabfad | bellard | s->cmos_index, data); |
117 | 3b46e624 | ths | #endif
|
118 | dff38e7b | bellard | switch(s->cmos_index) {
|
119 | 80cabfad | bellard | case RTC_SECONDS_ALARM:
|
120 | 80cabfad | bellard | case RTC_MINUTES_ALARM:
|
121 | 80cabfad | bellard | case RTC_HOURS_ALARM:
|
122 | 80cabfad | bellard | /* XXX: not supported */
|
123 | 80cabfad | bellard | s->cmos_data[s->cmos_index] = data; |
124 | 80cabfad | bellard | break;
|
125 | 80cabfad | bellard | case RTC_SECONDS:
|
126 | 80cabfad | bellard | case RTC_MINUTES:
|
127 | 80cabfad | bellard | case RTC_HOURS:
|
128 | 80cabfad | bellard | case RTC_DAY_OF_WEEK:
|
129 | 80cabfad | bellard | case RTC_DAY_OF_MONTH:
|
130 | 80cabfad | bellard | case RTC_MONTH:
|
131 | 80cabfad | bellard | case RTC_YEAR:
|
132 | 80cabfad | bellard | s->cmos_data[s->cmos_index] = data; |
133 | dff38e7b | bellard | /* if in set mode, do not update the time */
|
134 | dff38e7b | bellard | if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
|
135 | dff38e7b | bellard | rtc_set_time(s); |
136 | dff38e7b | bellard | } |
137 | 80cabfad | bellard | break;
|
138 | 80cabfad | bellard | case RTC_REG_A:
|
139 | dff38e7b | bellard | /* UIP bit is read only */
|
140 | dff38e7b | bellard | s->cmos_data[RTC_REG_A] = (data & ~REG_A_UIP) | |
141 | dff38e7b | bellard | (s->cmos_data[RTC_REG_A] & REG_A_UIP); |
142 | dff38e7b | bellard | rtc_timer_update(s, qemu_get_clock(vm_clock)); |
143 | dff38e7b | bellard | break;
|
144 | 80cabfad | bellard | case RTC_REG_B:
|
145 | dff38e7b | bellard | if (data & REG_B_SET) {
|
146 | dff38e7b | bellard | /* set mode: reset UIP mode */
|
147 | dff38e7b | bellard | s->cmos_data[RTC_REG_A] &= ~REG_A_UIP; |
148 | dff38e7b | bellard | data &= ~REG_B_UIE; |
149 | dff38e7b | bellard | } else {
|
150 | dff38e7b | bellard | /* if disabling set mode, update the time */
|
151 | dff38e7b | bellard | if (s->cmos_data[RTC_REG_B] & REG_B_SET) {
|
152 | dff38e7b | bellard | rtc_set_time(s); |
153 | dff38e7b | bellard | } |
154 | dff38e7b | bellard | } |
155 | dff38e7b | bellard | s->cmos_data[RTC_REG_B] = data; |
156 | dff38e7b | bellard | rtc_timer_update(s, qemu_get_clock(vm_clock)); |
157 | 80cabfad | bellard | break;
|
158 | 80cabfad | bellard | case RTC_REG_C:
|
159 | 80cabfad | bellard | case RTC_REG_D:
|
160 | 80cabfad | bellard | /* cannot write to them */
|
161 | 80cabfad | bellard | break;
|
162 | 80cabfad | bellard | default:
|
163 | 80cabfad | bellard | s->cmos_data[s->cmos_index] = data; |
164 | 80cabfad | bellard | break;
|
165 | 80cabfad | bellard | } |
166 | 80cabfad | bellard | } |
167 | 80cabfad | bellard | } |
168 | 80cabfad | bellard | |
169 | dff38e7b | bellard | static inline int to_bcd(RTCState *s, int a) |
170 | 80cabfad | bellard | { |
171 | dff38e7b | bellard | if (s->cmos_data[RTC_REG_B] & 0x04) { |
172 | dff38e7b | bellard | return a;
|
173 | dff38e7b | bellard | } else {
|
174 | dff38e7b | bellard | return ((a / 10) << 4) | (a % 10); |
175 | dff38e7b | bellard | } |
176 | 80cabfad | bellard | } |
177 | 80cabfad | bellard | |
178 | dff38e7b | bellard | static inline int from_bcd(RTCState *s, int a) |
179 | 80cabfad | bellard | { |
180 | dff38e7b | bellard | if (s->cmos_data[RTC_REG_B] & 0x04) { |
181 | dff38e7b | bellard | return a;
|
182 | dff38e7b | bellard | } else {
|
183 | dff38e7b | bellard | return ((a >> 4) * 10) + (a & 0x0f); |
184 | dff38e7b | bellard | } |
185 | dff38e7b | bellard | } |
186 | dff38e7b | bellard | |
187 | dff38e7b | bellard | static void rtc_set_time(RTCState *s) |
188 | dff38e7b | bellard | { |
189 | 43f493af | bellard | struct tm *tm = &s->current_tm;
|
190 | dff38e7b | bellard | |
191 | dff38e7b | bellard | tm->tm_sec = from_bcd(s, s->cmos_data[RTC_SECONDS]); |
192 | dff38e7b | bellard | tm->tm_min = from_bcd(s, s->cmos_data[RTC_MINUTES]); |
193 | 43f493af | bellard | tm->tm_hour = from_bcd(s, s->cmos_data[RTC_HOURS] & 0x7f);
|
194 | 43f493af | bellard | if (!(s->cmos_data[RTC_REG_B] & 0x02) && |
195 | 43f493af | bellard | (s->cmos_data[RTC_HOURS] & 0x80)) {
|
196 | 43f493af | bellard | tm->tm_hour += 12;
|
197 | 43f493af | bellard | } |
198 | dff38e7b | bellard | tm->tm_wday = from_bcd(s, s->cmos_data[RTC_DAY_OF_WEEK]); |
199 | dff38e7b | bellard | tm->tm_mday = from_bcd(s, s->cmos_data[RTC_DAY_OF_MONTH]); |
200 | dff38e7b | bellard | tm->tm_mon = from_bcd(s, s->cmos_data[RTC_MONTH]) - 1;
|
201 | dff38e7b | bellard | tm->tm_year = from_bcd(s, s->cmos_data[RTC_YEAR]) + 100;
|
202 | 43f493af | bellard | } |
203 | 43f493af | bellard | |
204 | 43f493af | bellard | static void rtc_copy_date(RTCState *s) |
205 | 43f493af | bellard | { |
206 | 43f493af | bellard | const struct tm *tm = &s->current_tm; |
207 | dff38e7b | bellard | |
208 | 43f493af | bellard | s->cmos_data[RTC_SECONDS] = to_bcd(s, tm->tm_sec); |
209 | 43f493af | bellard | s->cmos_data[RTC_MINUTES] = to_bcd(s, tm->tm_min); |
210 | 43f493af | bellard | if (s->cmos_data[RTC_REG_B] & 0x02) { |
211 | 43f493af | bellard | /* 24 hour format */
|
212 | 43f493af | bellard | s->cmos_data[RTC_HOURS] = to_bcd(s, tm->tm_hour); |
213 | 43f493af | bellard | } else {
|
214 | 43f493af | bellard | /* 12 hour format */
|
215 | 43f493af | bellard | s->cmos_data[RTC_HOURS] = to_bcd(s, tm->tm_hour % 12);
|
216 | 43f493af | bellard | if (tm->tm_hour >= 12) |
217 | 43f493af | bellard | s->cmos_data[RTC_HOURS] |= 0x80;
|
218 | 43f493af | bellard | } |
219 | 43f493af | bellard | s->cmos_data[RTC_DAY_OF_WEEK] = to_bcd(s, tm->tm_wday); |
220 | 43f493af | bellard | s->cmos_data[RTC_DAY_OF_MONTH] = to_bcd(s, tm->tm_mday); |
221 | 43f493af | bellard | s->cmos_data[RTC_MONTH] = to_bcd(s, tm->tm_mon + 1);
|
222 | 43f493af | bellard | s->cmos_data[RTC_YEAR] = to_bcd(s, tm->tm_year % 100);
|
223 | 43f493af | bellard | } |
224 | 43f493af | bellard | |
225 | 43f493af | bellard | /* month is between 0 and 11. */
|
226 | 43f493af | bellard | static int get_days_in_month(int month, int year) |
227 | 43f493af | bellard | { |
228 | 5fafdf24 | ths | static const int days_tab[12] = { |
229 | 5fafdf24 | ths | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
230 | 43f493af | bellard | }; |
231 | 43f493af | bellard | int d;
|
232 | 43f493af | bellard | if ((unsigned )month >= 12) |
233 | 43f493af | bellard | return 31; |
234 | 43f493af | bellard | d = days_tab[month]; |
235 | 43f493af | bellard | if (month == 1) { |
236 | 43f493af | bellard | if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0)) |
237 | 43f493af | bellard | d++; |
238 | 43f493af | bellard | } |
239 | 43f493af | bellard | return d;
|
240 | 43f493af | bellard | } |
241 | 43f493af | bellard | |
242 | 43f493af | bellard | /* update 'tm' to the next second */
|
243 | 43f493af | bellard | static void rtc_next_second(struct tm *tm) |
244 | 43f493af | bellard | { |
245 | 43f493af | bellard | int days_in_month;
|
246 | 43f493af | bellard | |
247 | 43f493af | bellard | tm->tm_sec++; |
248 | 43f493af | bellard | if ((unsigned)tm->tm_sec >= 60) { |
249 | 43f493af | bellard | tm->tm_sec = 0;
|
250 | 43f493af | bellard | tm->tm_min++; |
251 | 43f493af | bellard | if ((unsigned)tm->tm_min >= 60) { |
252 | 43f493af | bellard | tm->tm_min = 0;
|
253 | 43f493af | bellard | tm->tm_hour++; |
254 | 43f493af | bellard | if ((unsigned)tm->tm_hour >= 24) { |
255 | 43f493af | bellard | tm->tm_hour = 0;
|
256 | 43f493af | bellard | /* next day */
|
257 | 43f493af | bellard | tm->tm_wday++; |
258 | 43f493af | bellard | if ((unsigned)tm->tm_wday >= 7) |
259 | 43f493af | bellard | tm->tm_wday = 0;
|
260 | 5fafdf24 | ths | days_in_month = get_days_in_month(tm->tm_mon, |
261 | 43f493af | bellard | tm->tm_year + 1900);
|
262 | 43f493af | bellard | tm->tm_mday++; |
263 | 43f493af | bellard | if (tm->tm_mday < 1) { |
264 | 43f493af | bellard | tm->tm_mday = 1;
|
265 | 43f493af | bellard | } else if (tm->tm_mday > days_in_month) { |
266 | 43f493af | bellard | tm->tm_mday = 1;
|
267 | 43f493af | bellard | tm->tm_mon++; |
268 | 43f493af | bellard | if (tm->tm_mon >= 12) { |
269 | 43f493af | bellard | tm->tm_mon = 0;
|
270 | 43f493af | bellard | tm->tm_year++; |
271 | 43f493af | bellard | } |
272 | 43f493af | bellard | } |
273 | 43f493af | bellard | } |
274 | 43f493af | bellard | } |
275 | 43f493af | bellard | } |
276 | dff38e7b | bellard | } |
277 | dff38e7b | bellard | |
278 | 43f493af | bellard | |
279 | dff38e7b | bellard | static void rtc_update_second(void *opaque) |
280 | dff38e7b | bellard | { |
281 | dff38e7b | bellard | RTCState *s = opaque; |
282 | 4721c457 | bellard | int64_t delay; |
283 | dff38e7b | bellard | |
284 | dff38e7b | bellard | /* if the oscillator is not in normal operation, we do not update */
|
285 | dff38e7b | bellard | if ((s->cmos_data[RTC_REG_A] & 0x70) != 0x20) { |
286 | dff38e7b | bellard | s->next_second_time += ticks_per_sec; |
287 | dff38e7b | bellard | qemu_mod_timer(s->second_timer, s->next_second_time); |
288 | dff38e7b | bellard | } else {
|
289 | 43f493af | bellard | rtc_next_second(&s->current_tm); |
290 | 3b46e624 | ths | |
291 | dff38e7b | bellard | if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
|
292 | dff38e7b | bellard | /* update in progress bit */
|
293 | dff38e7b | bellard | s->cmos_data[RTC_REG_A] |= REG_A_UIP; |
294 | dff38e7b | bellard | } |
295 | 4721c457 | bellard | /* should be 244 us = 8 / 32768 seconds, but currently the
|
296 | 4721c457 | bellard | timers do not have the necessary resolution. */
|
297 | 4721c457 | bellard | delay = (ticks_per_sec * 1) / 100; |
298 | 4721c457 | bellard | if (delay < 1) |
299 | 4721c457 | bellard | delay = 1;
|
300 | 5fafdf24 | ths | qemu_mod_timer(s->second_timer2, |
301 | 4721c457 | bellard | s->next_second_time + delay); |
302 | dff38e7b | bellard | } |
303 | dff38e7b | bellard | } |
304 | dff38e7b | bellard | |
305 | dff38e7b | bellard | static void rtc_update_second2(void *opaque) |
306 | dff38e7b | bellard | { |
307 | dff38e7b | bellard | RTCState *s = opaque; |
308 | dff38e7b | bellard | |
309 | dff38e7b | bellard | if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
|
310 | dff38e7b | bellard | rtc_copy_date(s); |
311 | dff38e7b | bellard | } |
312 | dff38e7b | bellard | |
313 | dff38e7b | bellard | /* check alarm */
|
314 | dff38e7b | bellard | if (s->cmos_data[RTC_REG_B] & REG_B_AIE) {
|
315 | dff38e7b | bellard | if (((s->cmos_data[RTC_SECONDS_ALARM] & 0xc0) == 0xc0 || |
316 | 43f493af | bellard | s->cmos_data[RTC_SECONDS_ALARM] == s->current_tm.tm_sec) && |
317 | dff38e7b | bellard | ((s->cmos_data[RTC_MINUTES_ALARM] & 0xc0) == 0xc0 || |
318 | 43f493af | bellard | s->cmos_data[RTC_MINUTES_ALARM] == s->current_tm.tm_mon) && |
319 | dff38e7b | bellard | ((s->cmos_data[RTC_HOURS_ALARM] & 0xc0) == 0xc0 || |
320 | 43f493af | bellard | s->cmos_data[RTC_HOURS_ALARM] == s->current_tm.tm_hour)) { |
321 | dff38e7b | bellard | |
322 | 5fafdf24 | ths | s->cmos_data[RTC_REG_C] |= 0xa0;
|
323 | d537cf6c | pbrook | qemu_irq_raise(s->irq); |
324 | dff38e7b | bellard | } |
325 | dff38e7b | bellard | } |
326 | dff38e7b | bellard | |
327 | dff38e7b | bellard | /* update ended interrupt */
|
328 | dff38e7b | bellard | if (s->cmos_data[RTC_REG_B] & REG_B_UIE) {
|
329 | 5fafdf24 | ths | s->cmos_data[RTC_REG_C] |= 0x90;
|
330 | d537cf6c | pbrook | qemu_irq_raise(s->irq); |
331 | dff38e7b | bellard | } |
332 | dff38e7b | bellard | |
333 | dff38e7b | bellard | /* clear update in progress bit */
|
334 | dff38e7b | bellard | s->cmos_data[RTC_REG_A] &= ~REG_A_UIP; |
335 | dff38e7b | bellard | |
336 | dff38e7b | bellard | s->next_second_time += ticks_per_sec; |
337 | dff38e7b | bellard | qemu_mod_timer(s->second_timer, s->next_second_time); |
338 | 80cabfad | bellard | } |
339 | 80cabfad | bellard | |
340 | b41a2cd1 | bellard | static uint32_t cmos_ioport_read(void *opaque, uint32_t addr) |
341 | 80cabfad | bellard | { |
342 | b41a2cd1 | bellard | RTCState *s = opaque; |
343 | 80cabfad | bellard | int ret;
|
344 | 80cabfad | bellard | if ((addr & 1) == 0) { |
345 | 80cabfad | bellard | return 0xff; |
346 | 80cabfad | bellard | } else {
|
347 | 80cabfad | bellard | switch(s->cmos_index) {
|
348 | 80cabfad | bellard | case RTC_SECONDS:
|
349 | 80cabfad | bellard | case RTC_MINUTES:
|
350 | 80cabfad | bellard | case RTC_HOURS:
|
351 | 80cabfad | bellard | case RTC_DAY_OF_WEEK:
|
352 | 80cabfad | bellard | case RTC_DAY_OF_MONTH:
|
353 | 80cabfad | bellard | case RTC_MONTH:
|
354 | 80cabfad | bellard | case RTC_YEAR:
|
355 | 80cabfad | bellard | ret = s->cmos_data[s->cmos_index]; |
356 | 80cabfad | bellard | break;
|
357 | 80cabfad | bellard | case RTC_REG_A:
|
358 | 80cabfad | bellard | ret = s->cmos_data[s->cmos_index]; |
359 | 80cabfad | bellard | break;
|
360 | 80cabfad | bellard | case RTC_REG_C:
|
361 | 80cabfad | bellard | ret = s->cmos_data[s->cmos_index]; |
362 | d537cf6c | pbrook | qemu_irq_lower(s->irq); |
363 | 5fafdf24 | ths | s->cmos_data[RTC_REG_C] = 0x00;
|
364 | 80cabfad | bellard | break;
|
365 | 80cabfad | bellard | default:
|
366 | 80cabfad | bellard | ret = s->cmos_data[s->cmos_index]; |
367 | 80cabfad | bellard | break;
|
368 | 80cabfad | bellard | } |
369 | 80cabfad | bellard | #ifdef DEBUG_CMOS
|
370 | 80cabfad | bellard | printf("cmos: read index=0x%02x val=0x%02x\n",
|
371 | 80cabfad | bellard | s->cmos_index, ret); |
372 | 80cabfad | bellard | #endif
|
373 | 80cabfad | bellard | return ret;
|
374 | 80cabfad | bellard | } |
375 | 80cabfad | bellard | } |
376 | 80cabfad | bellard | |
377 | dff38e7b | bellard | void rtc_set_memory(RTCState *s, int addr, int val) |
378 | dff38e7b | bellard | { |
379 | dff38e7b | bellard | if (addr >= 0 && addr <= 127) |
380 | dff38e7b | bellard | s->cmos_data[addr] = val; |
381 | dff38e7b | bellard | } |
382 | dff38e7b | bellard | |
383 | dff38e7b | bellard | void rtc_set_date(RTCState *s, const struct tm *tm) |
384 | dff38e7b | bellard | { |
385 | 43f493af | bellard | s->current_tm = *tm; |
386 | dff38e7b | bellard | rtc_copy_date(s); |
387 | dff38e7b | bellard | } |
388 | dff38e7b | bellard | |
389 | ea55ffb3 | ths | /* PC cmos mappings */
|
390 | ea55ffb3 | ths | #define REG_IBM_CENTURY_BYTE 0x32 |
391 | ea55ffb3 | ths | #define REG_IBM_PS2_CENTURY_BYTE 0x37 |
392 | ea55ffb3 | ths | |
393 | 9596ebb7 | pbrook | static void rtc_set_date_from_host(RTCState *s) |
394 | ea55ffb3 | ths | { |
395 | f6503059 | balrog | struct tm tm;
|
396 | ea55ffb3 | ths | int val;
|
397 | ea55ffb3 | ths | |
398 | ea55ffb3 | ths | /* set the CMOS date */
|
399 | f6503059 | balrog | qemu_get_timedate(&tm, 0);
|
400 | f6503059 | balrog | rtc_set_date(s, &tm); |
401 | ea55ffb3 | ths | |
402 | f6503059 | balrog | val = to_bcd(s, (tm.tm_year / 100) + 19); |
403 | ea55ffb3 | ths | rtc_set_memory(s, REG_IBM_CENTURY_BYTE, val); |
404 | ea55ffb3 | ths | rtc_set_memory(s, REG_IBM_PS2_CENTURY_BYTE, val); |
405 | ea55ffb3 | ths | } |
406 | ea55ffb3 | ths | |
407 | dff38e7b | bellard | static void rtc_save(QEMUFile *f, void *opaque) |
408 | dff38e7b | bellard | { |
409 | dff38e7b | bellard | RTCState *s = opaque; |
410 | dff38e7b | bellard | |
411 | dff38e7b | bellard | qemu_put_buffer(f, s->cmos_data, 128);
|
412 | dff38e7b | bellard | qemu_put_8s(f, &s->cmos_index); |
413 | 3b46e624 | ths | |
414 | bee8d684 | ths | qemu_put_be32(f, s->current_tm.tm_sec); |
415 | bee8d684 | ths | qemu_put_be32(f, s->current_tm.tm_min); |
416 | bee8d684 | ths | qemu_put_be32(f, s->current_tm.tm_hour); |
417 | bee8d684 | ths | qemu_put_be32(f, s->current_tm.tm_wday); |
418 | bee8d684 | ths | qemu_put_be32(f, s->current_tm.tm_mday); |
419 | bee8d684 | ths | qemu_put_be32(f, s->current_tm.tm_mon); |
420 | bee8d684 | ths | qemu_put_be32(f, s->current_tm.tm_year); |
421 | dff38e7b | bellard | |
422 | dff38e7b | bellard | qemu_put_timer(f, s->periodic_timer); |
423 | bee8d684 | ths | qemu_put_be64(f, s->next_periodic_time); |
424 | dff38e7b | bellard | |
425 | bee8d684 | ths | qemu_put_be64(f, s->next_second_time); |
426 | dff38e7b | bellard | qemu_put_timer(f, s->second_timer); |
427 | dff38e7b | bellard | qemu_put_timer(f, s->second_timer2); |
428 | 80cabfad | bellard | } |
429 | 80cabfad | bellard | |
430 | dff38e7b | bellard | static int rtc_load(QEMUFile *f, void *opaque, int version_id) |
431 | 80cabfad | bellard | { |
432 | dff38e7b | bellard | RTCState *s = opaque; |
433 | dff38e7b | bellard | |
434 | dff38e7b | bellard | if (version_id != 1) |
435 | dff38e7b | bellard | return -EINVAL;
|
436 | 80cabfad | bellard | |
437 | dff38e7b | bellard | qemu_get_buffer(f, s->cmos_data, 128);
|
438 | dff38e7b | bellard | qemu_get_8s(f, &s->cmos_index); |
439 | 43f493af | bellard | |
440 | bee8d684 | ths | s->current_tm.tm_sec=qemu_get_be32(f); |
441 | bee8d684 | ths | s->current_tm.tm_min=qemu_get_be32(f); |
442 | bee8d684 | ths | s->current_tm.tm_hour=qemu_get_be32(f); |
443 | bee8d684 | ths | s->current_tm.tm_wday=qemu_get_be32(f); |
444 | bee8d684 | ths | s->current_tm.tm_mday=qemu_get_be32(f); |
445 | bee8d684 | ths | s->current_tm.tm_mon=qemu_get_be32(f); |
446 | bee8d684 | ths | s->current_tm.tm_year=qemu_get_be32(f); |
447 | dff38e7b | bellard | |
448 | dff38e7b | bellard | qemu_get_timer(f, s->periodic_timer); |
449 | bee8d684 | ths | s->next_periodic_time=qemu_get_be64(f); |
450 | dff38e7b | bellard | |
451 | bee8d684 | ths | s->next_second_time=qemu_get_be64(f); |
452 | dff38e7b | bellard | qemu_get_timer(f, s->second_timer); |
453 | dff38e7b | bellard | qemu_get_timer(f, s->second_timer2); |
454 | dff38e7b | bellard | return 0; |
455 | dff38e7b | bellard | } |
456 | dff38e7b | bellard | |
457 | d537cf6c | pbrook | RTCState *rtc_init(int base, qemu_irq irq)
|
458 | dff38e7b | bellard | { |
459 | dff38e7b | bellard | RTCState *s; |
460 | dff38e7b | bellard | |
461 | dff38e7b | bellard | s = qemu_mallocz(sizeof(RTCState));
|
462 | dff38e7b | bellard | if (!s)
|
463 | dff38e7b | bellard | return NULL; |
464 | 80cabfad | bellard | |
465 | 80cabfad | bellard | s->irq = irq; |
466 | 80cabfad | bellard | s->cmos_data[RTC_REG_A] = 0x26;
|
467 | 80cabfad | bellard | s->cmos_data[RTC_REG_B] = 0x02;
|
468 | 80cabfad | bellard | s->cmos_data[RTC_REG_C] = 0x00;
|
469 | 80cabfad | bellard | s->cmos_data[RTC_REG_D] = 0x80;
|
470 | 80cabfad | bellard | |
471 | ea55ffb3 | ths | rtc_set_date_from_host(s); |
472 | ea55ffb3 | ths | |
473 | 5fafdf24 | ths | s->periodic_timer = qemu_new_timer(vm_clock, |
474 | dff38e7b | bellard | rtc_periodic_timer, s); |
475 | 5fafdf24 | ths | s->second_timer = qemu_new_timer(vm_clock, |
476 | dff38e7b | bellard | rtc_update_second, s); |
477 | 5fafdf24 | ths | s->second_timer2 = qemu_new_timer(vm_clock, |
478 | dff38e7b | bellard | rtc_update_second2, s); |
479 | dff38e7b | bellard | |
480 | dff38e7b | bellard | s->next_second_time = qemu_get_clock(vm_clock) + (ticks_per_sec * 99) / 100; |
481 | dff38e7b | bellard | qemu_mod_timer(s->second_timer2, s->next_second_time); |
482 | dff38e7b | bellard | |
483 | b41a2cd1 | bellard | register_ioport_write(base, 2, 1, cmos_ioport_write, s); |
484 | b41a2cd1 | bellard | register_ioport_read(base, 2, 1, cmos_ioport_read, s); |
485 | dff38e7b | bellard | |
486 | dff38e7b | bellard | register_savevm("mc146818rtc", base, 1, rtc_save, rtc_load, s); |
487 | dff38e7b | bellard | return s;
|
488 | 80cabfad | bellard | } |
489 | 80cabfad | bellard | |
490 | 2ca9d013 | ths | /* Memory mapped interface */
|
491 | 9596ebb7 | pbrook | static uint32_t cmos_mm_readb (void *opaque, target_phys_addr_t addr) |
492 | 2ca9d013 | ths | { |
493 | 2ca9d013 | ths | RTCState *s = opaque; |
494 | 2ca9d013 | ths | |
495 | 18c6e2ff | ths | return cmos_ioport_read(s, (addr - s->base) >> s->it_shift) & 0xFF; |
496 | 2ca9d013 | ths | } |
497 | 2ca9d013 | ths | |
498 | 9596ebb7 | pbrook | static void cmos_mm_writeb (void *opaque, |
499 | 9596ebb7 | pbrook | target_phys_addr_t addr, uint32_t value) |
500 | 2ca9d013 | ths | { |
501 | 2ca9d013 | ths | RTCState *s = opaque; |
502 | 2ca9d013 | ths | |
503 | 18c6e2ff | ths | cmos_ioport_write(s, (addr - s->base) >> s->it_shift, value & 0xFF);
|
504 | 2ca9d013 | ths | } |
505 | 2ca9d013 | ths | |
506 | 9596ebb7 | pbrook | static uint32_t cmos_mm_readw (void *opaque, target_phys_addr_t addr) |
507 | 2ca9d013 | ths | { |
508 | 2ca9d013 | ths | RTCState *s = opaque; |
509 | 18c6e2ff | ths | uint32_t val; |
510 | 2ca9d013 | ths | |
511 | 18c6e2ff | ths | val = cmos_ioport_read(s, (addr - s->base) >> s->it_shift) & 0xFFFF;
|
512 | 18c6e2ff | ths | #ifdef TARGET_WORDS_BIGENDIAN
|
513 | 18c6e2ff | ths | val = bswap16(val); |
514 | 18c6e2ff | ths | #endif
|
515 | 18c6e2ff | ths | return val;
|
516 | 2ca9d013 | ths | } |
517 | 2ca9d013 | ths | |
518 | 9596ebb7 | pbrook | static void cmos_mm_writew (void *opaque, |
519 | 9596ebb7 | pbrook | target_phys_addr_t addr, uint32_t value) |
520 | 2ca9d013 | ths | { |
521 | 2ca9d013 | ths | RTCState *s = opaque; |
522 | 18c6e2ff | ths | #ifdef TARGET_WORDS_BIGENDIAN
|
523 | 18c6e2ff | ths | value = bswap16(value); |
524 | 18c6e2ff | ths | #endif
|
525 | 18c6e2ff | ths | cmos_ioport_write(s, (addr - s->base) >> s->it_shift, value & 0xFFFF);
|
526 | 2ca9d013 | ths | } |
527 | 2ca9d013 | ths | |
528 | 9596ebb7 | pbrook | static uint32_t cmos_mm_readl (void *opaque, target_phys_addr_t addr) |
529 | 2ca9d013 | ths | { |
530 | 2ca9d013 | ths | RTCState *s = opaque; |
531 | 18c6e2ff | ths | uint32_t val; |
532 | 2ca9d013 | ths | |
533 | 18c6e2ff | ths | val = cmos_ioport_read(s, (addr - s->base) >> s->it_shift); |
534 | 18c6e2ff | ths | #ifdef TARGET_WORDS_BIGENDIAN
|
535 | 18c6e2ff | ths | val = bswap32(val); |
536 | 18c6e2ff | ths | #endif
|
537 | 18c6e2ff | ths | return val;
|
538 | 2ca9d013 | ths | } |
539 | 2ca9d013 | ths | |
540 | 9596ebb7 | pbrook | static void cmos_mm_writel (void *opaque, |
541 | 9596ebb7 | pbrook | target_phys_addr_t addr, uint32_t value) |
542 | 2ca9d013 | ths | { |
543 | 2ca9d013 | ths | RTCState *s = opaque; |
544 | 18c6e2ff | ths | #ifdef TARGET_WORDS_BIGENDIAN
|
545 | 18c6e2ff | ths | value = bswap32(value); |
546 | 18c6e2ff | ths | #endif
|
547 | 18c6e2ff | ths | cmos_ioport_write(s, (addr - s->base) >> s->it_shift, value); |
548 | 2ca9d013 | ths | } |
549 | 2ca9d013 | ths | |
550 | 2ca9d013 | ths | static CPUReadMemoryFunc *rtc_mm_read[] = {
|
551 | 2ca9d013 | ths | &cmos_mm_readb, |
552 | 2ca9d013 | ths | &cmos_mm_readw, |
553 | 2ca9d013 | ths | &cmos_mm_readl, |
554 | 2ca9d013 | ths | }; |
555 | 2ca9d013 | ths | |
556 | 2ca9d013 | ths | static CPUWriteMemoryFunc *rtc_mm_write[] = {
|
557 | 2ca9d013 | ths | &cmos_mm_writeb, |
558 | 2ca9d013 | ths | &cmos_mm_writew, |
559 | 2ca9d013 | ths | &cmos_mm_writel, |
560 | 2ca9d013 | ths | }; |
561 | 2ca9d013 | ths | |
562 | 18c6e2ff | ths | RTCState *rtc_mm_init(target_phys_addr_t base, int it_shift, qemu_irq irq)
|
563 | 2ca9d013 | ths | { |
564 | 2ca9d013 | ths | RTCState *s; |
565 | 2ca9d013 | ths | int io_memory;
|
566 | 2ca9d013 | ths | |
567 | 2ca9d013 | ths | s = qemu_mallocz(sizeof(RTCState));
|
568 | 2ca9d013 | ths | if (!s)
|
569 | 2ca9d013 | ths | return NULL; |
570 | 2ca9d013 | ths | |
571 | 2ca9d013 | ths | s->irq = irq; |
572 | 2ca9d013 | ths | s->cmos_data[RTC_REG_A] = 0x26;
|
573 | 2ca9d013 | ths | s->cmos_data[RTC_REG_B] = 0x02;
|
574 | 2ca9d013 | ths | s->cmos_data[RTC_REG_C] = 0x00;
|
575 | 2ca9d013 | ths | s->cmos_data[RTC_REG_D] = 0x80;
|
576 | 2ca9d013 | ths | s->base = base; |
577 | 2ca9d013 | ths | |
578 | 2ca9d013 | ths | rtc_set_date_from_host(s); |
579 | 2ca9d013 | ths | |
580 | 2ca9d013 | ths | s->periodic_timer = qemu_new_timer(vm_clock, |
581 | 2ca9d013 | ths | rtc_periodic_timer, s); |
582 | 2ca9d013 | ths | s->second_timer = qemu_new_timer(vm_clock, |
583 | 2ca9d013 | ths | rtc_update_second, s); |
584 | 2ca9d013 | ths | s->second_timer2 = qemu_new_timer(vm_clock, |
585 | 2ca9d013 | ths | rtc_update_second2, s); |
586 | 2ca9d013 | ths | |
587 | 2ca9d013 | ths | s->next_second_time = qemu_get_clock(vm_clock) + (ticks_per_sec * 99) / 100; |
588 | 2ca9d013 | ths | qemu_mod_timer(s->second_timer2, s->next_second_time); |
589 | 2ca9d013 | ths | |
590 | 2ca9d013 | ths | io_memory = cpu_register_io_memory(0, rtc_mm_read, rtc_mm_write, s);
|
591 | 18c6e2ff | ths | cpu_register_physical_memory(base, 2 << it_shift, io_memory);
|
592 | 2ca9d013 | ths | |
593 | 2ca9d013 | ths | register_savevm("mc146818rtc", base, 1, rtc_save, rtc_load, s); |
594 | 2ca9d013 | ths | return s;
|
595 | 2ca9d013 | ths | } |