VirtualBox

source: vbox/trunk/src/VBox/Devices/PC/DevRTC.cpp@ 20037

Last change on this file since 20037 was 19706, checked in by vboxsync, 16 years ago

DevRTC: UIP logging (level 2).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 31.5 KB
Line 
1/* $Id: DevRTC.cpp 19706 2009-05-14 17:14:35Z vboxsync $ */
2/** @file
3 * Motorola MC146818 RTC/CMOS Device.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 * --------------------------------------------------------------------
21 *
22 * This code is based on:
23 *
24 * QEMU MC146818 RTC emulation
25 *
26 * Copyright (c) 2003-2004 Fabrice Bellard
27 *
28 * Permission is hereby granted, free of charge, to any person obtaining a copy
29 * of this software and associated documentation files (the "Software"), to deal
30 * in the Software without restriction, including without limitation the rights
31 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
32 * copies of the Software, and to permit persons to whom the Software is
33 * furnished to do so, subject to the following conditions:
34 *
35 * The above copyright notice and this permission notice shall be included in
36 * all copies or substantial portions of the Software.
37 *
38 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
39 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
40 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
41 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
42 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
43 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
44 * THE SOFTWARE.
45 */
46
47/*******************************************************************************
48* Header Files *
49*******************************************************************************/
50#define LOG_GROUP LOG_GROUP_DEV_RTC
51#include <VBox/pdmdev.h>
52#include <VBox/log.h>
53#include <iprt/asm.h>
54#include <iprt/assert.h>
55#include <iprt/string.h>
56
57#include "../Builtins.h"
58
59struct RTCState;
60typedef struct RTCState RTCState;
61
62#define RTC_CRC_START 0x10
63#define RTC_CRC_LAST 0x2d
64#define RTC_CRC_HIGH 0x2e
65#define RTC_CRC_LOW 0x2f
66
67
68/*******************************************************************************
69* Internal Functions *
70*******************************************************************************/
71#ifndef VBOX_DEVICE_STRUCT_TESTCASE
72__BEGIN_DECLS
73PDMBOTHCBDECL(int) rtcIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb);
74PDMBOTHCBDECL(int) rtcIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb);
75PDMBOTHCBDECL(void) rtcTimerPeriodic(PPDMDEVINS pDevIns, PTMTIMER pTimer);
76PDMBOTHCBDECL(void) rtcTimerSecond(PPDMDEVINS pDevIns, PTMTIMER pTimer);
77PDMBOTHCBDECL(void) rtcTimerSecond2(PPDMDEVINS pDevIns, PTMTIMER pTimer);
78__END_DECLS
79#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
80
81
82/*******************************************************************************
83* Defined Constants And Macros *
84*******************************************************************************/
85/*#define DEBUG_CMOS*/
86
87#define RTC_SECONDS 0
88#define RTC_SECONDS_ALARM 1
89#define RTC_MINUTES 2
90#define RTC_MINUTES_ALARM 3
91#define RTC_HOURS 4
92#define RTC_HOURS_ALARM 5
93#define RTC_ALARM_DONT_CARE 0xC0
94
95#define RTC_DAY_OF_WEEK 6
96#define RTC_DAY_OF_MONTH 7
97#define RTC_MONTH 8
98#define RTC_YEAR 9
99
100#define RTC_REG_A 10
101#define RTC_REG_B 11
102#define RTC_REG_C 12
103#define RTC_REG_D 13
104
105#define REG_A_UIP 0x80
106
107#define REG_B_SET 0x80
108#define REG_B_PIE 0x40
109#define REG_B_AIE 0x20
110#define REG_B_UIE 0x10
111
112
113/*******************************************************************************
114* Structures and Typedefs *
115*******************************************************************************/
116/** @todo Replace struct my_tm with RTTIME. */
117struct my_tm
118{
119 int32_t tm_sec;
120 int32_t tm_min;
121 int32_t tm_hour;
122 int32_t tm_mday;
123 int32_t tm_mon;
124 int32_t tm_year;
125 int32_t tm_wday;
126 int32_t tm_yday;
127};
128
129
130struct RTCState {
131 uint8_t cmos_data[128];
132 uint8_t cmos_index;
133 uint8_t Alignment0[7];
134 struct my_tm current_tm;
135 int32_t irq;
136 /** Use UTC or local time initially. */
137 bool fUTC;
138 /* periodic timer */
139 int64_t next_periodic_time;
140 /* second update */
141 int64_t next_second_time;
142
143 /** Pointer to the device instance - R3 Ptr. */
144 PPDMDEVINSR3 pDevInsR3;
145 /** The periodic timer (rtcTimerPeriodic) - R3 Ptr. */
146 PTMTIMERR3 pPeriodicTimerR3;
147 /** The second timer (rtcTimerSecond) - R3 Ptr. */
148 PTMTIMERR3 pSecondTimerR3;
149 /** The second second timer (rtcTimerSecond2) - R3 Ptr. */
150 PTMTIMERR3 pSecondTimer2R3;
151
152 /** Pointer to the device instance - R0 Ptr. */
153 PPDMDEVINSR0 pDevInsR0;
154 /** The periodic timer (rtcTimerPeriodic) - R0 Ptr. */
155 PTMTIMERR0 pPeriodicTimerR0;
156 /** The second timer (rtcTimerSecond) - R0 Ptr. */
157 PTMTIMERR0 pSecondTimerR0;
158 /** The second second timer (rtcTimerSecond2) - R0 Ptr. */
159 PTMTIMERR0 pSecondTimer2R0;
160
161 /** Pointer to the device instance - RC Ptr. */
162 PPDMDEVINSRC pDevInsRC;
163 /** The periodic timer (rtcTimerPeriodic) - RC Ptr. */
164 PTMTIMERRC pPeriodicTimerRC;
165 /** The second timer (rtcTimerSecond) - RC Ptr. */
166 PTMTIMERRC pSecondTimerRC;
167 /** The second second timer (rtcTimerSecond2) - RC Ptr. */
168 PTMTIMERRC pSecondTimer2RC;
169
170 /** The RTC registration structure. */
171 PDMRTCREG RtcReg;
172 /** The RTC device helpers. */
173 R3PTRTYPE(PCPDMRTCHLP) pRtcHlpR3;
174 /** Number of release log entries. Used to prevent flooding. */
175 uint32_t cRelLogEntries;
176 /** The current/previous timer period. Used to prevent flooding changes. */
177 int32_t CurPeriod;
178};
179
180#ifndef VBOX_DEVICE_STRUCT_TESTCASE
181static void rtc_set_time(RTCState *s);
182static void rtc_copy_date(RTCState *s);
183
184static void rtc_timer_update(RTCState *s, int64_t current_time)
185{
186 int period_code, period;
187 uint64_t cur_clock, next_irq_clock;
188 uint32_t freq;
189
190 period_code = s->cmos_data[RTC_REG_A] & 0x0f;
191 if (period_code != 0 &&
192 (s->cmos_data[RTC_REG_B] & REG_B_PIE)) {
193 if (period_code <= 2)
194 period_code += 7;
195 /* period in 32 kHz cycles */
196 period = 1 << (period_code - 1);
197 /* compute 32 kHz clock */
198 freq = TMTimerGetFreq(s->CTX_SUFF(pPeriodicTimer));
199
200 cur_clock = ASMMultU64ByU32DivByU32(current_time, 32768, freq);
201 next_irq_clock = (cur_clock & ~(uint64_t)(period - 1)) + period;
202 s->next_periodic_time = ASMMultU64ByU32DivByU32(next_irq_clock, freq, 32768) + 1;
203 TMTimerSet(s->CTX_SUFF(pPeriodicTimer), s->next_periodic_time);
204
205 if (period != s->CurPeriod)
206 {
207 if (s->cRelLogEntries++ < 64)
208 LogRel(("RTC: period=%#x (%d) %u Hz\n", period, period, _32K / period));
209 s->CurPeriod = period;
210 }
211 } else {
212 if (TMTimerIsActive(s->CTX_SUFF(pPeriodicTimer)) && s->cRelLogEntries++ < 64)
213 LogRel(("RTC: stopped the periodic timer\n"));
214 TMTimerStop(s->CTX_SUFF(pPeriodicTimer));
215 }
216}
217
218static void rtc_periodic_timer(void *opaque)
219{
220 RTCState *s = (RTCState*)opaque;
221
222 rtc_timer_update(s, s->next_periodic_time);
223 s->cmos_data[RTC_REG_C] |= 0xc0;
224 PDMDevHlpISASetIrq(s->CTX_SUFF(pDevIns), s->irq, 1);
225}
226
227static void cmos_ioport_write(void *opaque, uint32_t addr, uint32_t data)
228{
229 RTCState *s = (RTCState*)opaque;
230
231 if ((addr & 1) == 0) {
232 s->cmos_index = data & 0x7f;
233 } else {
234 Log(("CMOS: Write idx %#04x: %#04x (old %#04x)\n", s->cmos_index, data, s->cmos_data[s->cmos_index]));
235 switch(s->cmos_index) {
236 case RTC_SECONDS_ALARM:
237 case RTC_MINUTES_ALARM:
238 case RTC_HOURS_ALARM:
239 s->cmos_data[s->cmos_index] = data;
240 break;
241 case RTC_SECONDS:
242 case RTC_MINUTES:
243 case RTC_HOURS:
244 case RTC_DAY_OF_WEEK:
245 case RTC_DAY_OF_MONTH:
246 case RTC_MONTH:
247 case RTC_YEAR:
248 s->cmos_data[s->cmos_index] = data;
249 /* if in set mode, do not update the time */
250 if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
251 rtc_set_time(s);
252 }
253 break;
254 case RTC_REG_A:
255 /* UIP bit is read only */
256 s->cmos_data[RTC_REG_A] = (data & ~REG_A_UIP) |
257 (s->cmos_data[RTC_REG_A] & REG_A_UIP);
258 rtc_timer_update(s, TMTimerGet(s->CTX_SUFF(pPeriodicTimer)));
259 break;
260 case RTC_REG_B:
261 if (data & REG_B_SET) {
262 /* set mode: reset UIP mode */
263 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
264#if 0 /* This is probably wrong as it breaks changing the time/date in OS/2. */
265 data &= ~REG_B_UIE;
266#endif
267 } else {
268 /* if disabling set mode, update the time */
269 if (s->cmos_data[RTC_REG_B] & REG_B_SET) {
270 rtc_set_time(s);
271 }
272 }
273 s->cmos_data[RTC_REG_B] = data;
274 rtc_timer_update(s, TMTimerGet(s->CTX_SUFF(pPeriodicTimer)));
275 break;
276 case RTC_REG_C:
277 case RTC_REG_D:
278 /* cannot write to them */
279 break;
280 default:
281 s->cmos_data[s->cmos_index] = data;
282 break;
283 }
284 }
285}
286
287static inline int to_bcd(RTCState *s, int a)
288{
289 if (s->cmos_data[RTC_REG_B] & 0x04) {
290 return a;
291 } else {
292 return ((a / 10) << 4) | (a % 10);
293 }
294}
295
296static inline int from_bcd(RTCState *s, int a)
297{
298 if (s->cmos_data[RTC_REG_B] & 0x04) {
299 return a;
300 } else {
301 return ((a >> 4) * 10) + (a & 0x0f);
302 }
303}
304
305static void rtc_set_time(RTCState *s)
306{
307 struct my_tm *tm = &s->current_tm;
308
309 tm->tm_sec = from_bcd(s, s->cmos_data[RTC_SECONDS]);
310 tm->tm_min = from_bcd(s, s->cmos_data[RTC_MINUTES]);
311 tm->tm_hour = from_bcd(s, s->cmos_data[RTC_HOURS] & 0x7f);
312 if (!(s->cmos_data[RTC_REG_B] & 0x02) &&
313 (s->cmos_data[RTC_HOURS] & 0x80)) {
314 tm->tm_hour += 12;
315 }
316 tm->tm_wday = from_bcd(s, s->cmos_data[RTC_DAY_OF_WEEK]);
317 tm->tm_mday = from_bcd(s, s->cmos_data[RTC_DAY_OF_MONTH]);
318 tm->tm_mon = from_bcd(s, s->cmos_data[RTC_MONTH]) - 1;
319 tm->tm_year = from_bcd(s, s->cmos_data[RTC_YEAR]) + 100;
320}
321
322static void rtc_copy_date(RTCState *s)
323{
324 const struct my_tm *tm = &s->current_tm;
325
326 s->cmos_data[RTC_SECONDS] = to_bcd(s, tm->tm_sec);
327 s->cmos_data[RTC_MINUTES] = to_bcd(s, tm->tm_min);
328 if (s->cmos_data[RTC_REG_B] & 0x02) {
329 /* 24 hour format */
330 s->cmos_data[RTC_HOURS] = to_bcd(s, tm->tm_hour);
331 } else {
332 /* 12 hour format */
333 s->cmos_data[RTC_HOURS] = to_bcd(s, tm->tm_hour % 12);
334 if (tm->tm_hour >= 12)
335 s->cmos_data[RTC_HOURS] |= 0x80;
336 }
337 s->cmos_data[RTC_DAY_OF_WEEK] = to_bcd(s, tm->tm_wday);
338 s->cmos_data[RTC_DAY_OF_MONTH] = to_bcd(s, tm->tm_mday);
339 s->cmos_data[RTC_MONTH] = to_bcd(s, tm->tm_mon + 1);
340 s->cmos_data[RTC_YEAR] = to_bcd(s, tm->tm_year % 100);
341}
342
343/* month is between 0 and 11. */
344static int get_days_in_month(int month, int year)
345{
346 static const int days_tab[12] = {
347 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
348 };
349 int d;
350 if ((unsigned )month >= 12)
351 return 31;
352 d = days_tab[month];
353 if (month == 1) {
354 if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0))
355 d++;
356 }
357 return d;
358}
359
360/* update 'tm' to the next second */
361static void rtc_next_second(struct my_tm *tm)
362{
363 int days_in_month;
364
365 tm->tm_sec++;
366 if ((unsigned)tm->tm_sec >= 60) {
367 tm->tm_sec = 0;
368 tm->tm_min++;
369 if ((unsigned)tm->tm_min >= 60) {
370 tm->tm_min = 0;
371 tm->tm_hour++;
372 if ((unsigned)tm->tm_hour >= 24) {
373 tm->tm_hour = 0;
374 /* next day */
375 tm->tm_wday++;
376 if ((unsigned)tm->tm_wday >= 7)
377 tm->tm_wday = 0;
378 days_in_month = get_days_in_month(tm->tm_mon,
379 tm->tm_year + 1900);
380 tm->tm_mday++;
381 if (tm->tm_mday < 1) {
382 tm->tm_mday = 1;
383 } else if (tm->tm_mday > days_in_month) {
384 tm->tm_mday = 1;
385 tm->tm_mon++;
386 if (tm->tm_mon >= 12) {
387 tm->tm_mon = 0;
388 tm->tm_year++;
389 }
390 }
391 }
392 }
393 }
394}
395
396
397static void rtc_update_second(void *opaque)
398{
399 RTCState *s = (RTCState*)opaque;
400
401 /* if the oscillator is not in normal operation, we do not update */
402 if ((s->cmos_data[RTC_REG_A] & 0x70) != 0x20) {
403 s->next_second_time += TMTimerGetFreq(s->CTX_SUFF(pSecondTimer));
404 TMTimerSet(s->CTX_SUFF(pSecondTimer), s->next_second_time);
405 } else {
406 rtc_next_second(&s->current_tm);
407
408 if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
409 /* update in progress bit */
410 Log2(("RTC: UIP %x -> 1\n", !!(s->cmos_data[RTC_REG_A] & REG_A_UIP)));
411 s->cmos_data[RTC_REG_A] |= REG_A_UIP;
412 }
413
414 /* 244140 ns = 8 / 32768 seconds */
415 uint64_t delay = TMTimerFromNano(s->CTX_SUFF(pSecondTimer2), 244140);
416 TMTimerSet(s->CTX_SUFF(pSecondTimer2), s->next_second_time + delay);
417 }
418}
419
420static void rtc_update_second2(void *opaque)
421{
422 RTCState *s = (RTCState*)opaque;
423
424 if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
425 rtc_copy_date(s);
426 }
427
428 /* check alarm */
429 if (s->cmos_data[RTC_REG_B] & REG_B_AIE) {
430 if (((s->cmos_data[RTC_SECONDS_ALARM] & 0xc0) == 0xc0 ||
431 from_bcd(s, s->cmos_data[RTC_SECONDS_ALARM]) == s->current_tm.tm_sec) &&
432 ((s->cmos_data[RTC_MINUTES_ALARM] & 0xc0) == 0xc0 ||
433 from_bcd(s, s->cmos_data[RTC_MINUTES_ALARM]) == s->current_tm.tm_min) &&
434 ((s->cmos_data[RTC_HOURS_ALARM] & 0xc0) == 0xc0 ||
435 from_bcd(s, s->cmos_data[RTC_HOURS_ALARM]) == s->current_tm.tm_hour)) {
436
437 s->cmos_data[RTC_REG_C] |= 0xa0;
438 PDMDevHlpISASetIrq(s->CTX_SUFF(pDevIns), s->irq, 1);
439 }
440 }
441
442 /* update ended interrupt */
443 if (s->cmos_data[RTC_REG_B] & REG_B_UIE) {
444 s->cmos_data[RTC_REG_C] |= 0x90;
445 PDMDevHlpISASetIrq(s->CTX_SUFF(pDevIns), s->irq, 1);
446 }
447
448 /* clear update in progress bit */
449 Log2(("RTC: UIP %x -> 0\n", !!(s->cmos_data[RTC_REG_A] & REG_A_UIP)));
450 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
451
452 s->next_second_time += TMTimerGetFreq(s->CTX_SUFF(pSecondTimer));
453 TMTimerSet(s->CTX_SUFF(pSecondTimer), s->next_second_time);
454}
455
456static uint32_t cmos_ioport_read(void *opaque, uint32_t addr)
457{
458 RTCState *s = (RTCState*)opaque;
459 int ret;
460 if ((addr & 1) == 0) {
461 return 0xff;
462 } else {
463 switch(s->cmos_index) {
464 case RTC_SECONDS:
465 case RTC_MINUTES:
466 case RTC_HOURS:
467 case RTC_DAY_OF_WEEK:
468 case RTC_DAY_OF_MONTH:
469 case RTC_MONTH:
470 case RTC_YEAR:
471 ret = s->cmos_data[s->cmos_index];
472 break;
473 case RTC_REG_A:
474 ret = s->cmos_data[s->cmos_index];
475 break;
476 case RTC_REG_C:
477 ret = s->cmos_data[s->cmos_index];
478 PDMDevHlpISASetIrq(s->CTX_SUFF(pDevIns), s->irq, 0);
479 s->cmos_data[RTC_REG_C] = 0x00;
480 break;
481 default:
482 ret = s->cmos_data[s->cmos_index];
483 break;
484 }
485 Log(("CMOS: Read idx %#04x: %#04x\n", s->cmos_index, ret));
486 return ret;
487 }
488}
489
490#ifdef IN_RING3
491static void rtc_set_memory(RTCState *s, int addr, int val)
492{
493 if (addr >= 0 && addr <= 127)
494 s->cmos_data[addr] = val;
495}
496
497static void rtc_set_date(RTCState *s, const struct my_tm *tm)
498{
499 s->current_tm = *tm;
500 rtc_copy_date(s);
501}
502
503#endif /* IN_RING3 */
504
505/* -=-=-=-=-=- wrappers / stuff -=-=-=-=-=- */
506
507/**
508 * Port I/O Handler for IN operations.
509 *
510 * @returns VBox status code.
511 *
512 * @param pDevIns The device instance.
513 * @param pvUser User argument - ignored.
514 * @param uPort Port number used for the IN operation.
515 * @param pu32 Where to store the result.
516 * @param cb Number of bytes read.
517 */
518PDMBOTHCBDECL(int) rtcIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
519{
520 NOREF(pvUser);
521 if (cb == 1)
522 {
523 *pu32 = cmos_ioport_read(PDMINS_2_DATA(pDevIns, RTCState *), Port);
524 return VINF_SUCCESS;
525 }
526 return VERR_IOM_IOPORT_UNUSED;
527}
528
529
530/**
531 * Port I/O Handler for OUT operations.
532 *
533 * @returns VBox status code.
534 *
535 * @param pDevIns The device instance.
536 * @param pvUser User argument - ignored.
537 * @param uPort Port number used for the IN operation.
538 * @param u32 The value to output.
539 * @param cb The value size in bytes.
540 */
541PDMBOTHCBDECL(int) rtcIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
542{
543 NOREF(pvUser);
544 if (cb == 1)
545 cmos_ioport_write(PDMINS_2_DATA(pDevIns, RTCState *), Port, u32);
546 return VINF_SUCCESS;
547}
548
549
550/**
551 * Device timer callback function, periodic.
552 *
553 * @param pDevIns Device instance of the device which registered the timer.
554 * @param pTimer The timer handle.
555 */
556PDMBOTHCBDECL(void) rtcTimerPeriodic(PPDMDEVINS pDevIns, PTMTIMER pTimer)
557{
558 rtc_periodic_timer(PDMINS_2_DATA(pDevIns, RTCState *));
559}
560
561
562/**
563 * Device timer callback function, second.
564 *
565 * @param pDevIns Device instance of the device which registered the timer.
566 * @param pTimer The timer handle.
567 */
568PDMBOTHCBDECL(void) rtcTimerSecond(PPDMDEVINS pDevIns, PTMTIMER pTimer)
569{
570 rtc_update_second(PDMINS_2_DATA(pDevIns, RTCState *));
571}
572
573
574/**
575 * Device timer callback function, second2.
576 *
577 * @param pDevIns Device instance of the device which registered the timer.
578 * @param pTimer The timer handle.
579 */
580PDMBOTHCBDECL(void) rtcTimerSecond2(PPDMDEVINS pDevIns, PTMTIMER pTimer)
581{
582 rtc_update_second2(PDMINS_2_DATA(pDevIns, RTCState *));
583}
584
585
586#ifdef IN_RING3
587/**
588 * Saves a state of the programmable interval timer device.
589 *
590 * @returns VBox status code.
591 * @param pDevIns The device instance.
592 * @param pSSMHandle The handle to save the state to.
593 */
594static DECLCALLBACK(int) rtcSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle)
595{
596 RTCState *pThis = PDMINS_2_DATA(pDevIns, RTCState *);
597
598 SSMR3PutMem(pSSMHandle, pThis->cmos_data, 128);
599 SSMR3PutU8(pSSMHandle, pThis->cmos_index);
600
601 SSMR3PutS32(pSSMHandle, pThis->current_tm.tm_sec);
602 SSMR3PutS32(pSSMHandle, pThis->current_tm.tm_min);
603 SSMR3PutS32(pSSMHandle, pThis->current_tm.tm_hour);
604 SSMR3PutS32(pSSMHandle, pThis->current_tm.tm_wday);
605 SSMR3PutS32(pSSMHandle, pThis->current_tm.tm_mday);
606 SSMR3PutS32(pSSMHandle, pThis->current_tm.tm_mon);
607 SSMR3PutS32(pSSMHandle, pThis->current_tm.tm_year);
608
609 TMR3TimerSave(pThis->CTX_SUFF(pPeriodicTimer), pSSMHandle);
610
611 SSMR3PutS64(pSSMHandle, pThis->next_periodic_time);
612
613 SSMR3PutS64(pSSMHandle, pThis->next_second_time);
614 TMR3TimerSave(pThis->CTX_SUFF(pSecondTimer), pSSMHandle);
615 TMR3TimerSave(pThis->CTX_SUFF(pSecondTimer2), pSSMHandle);
616
617 return VINF_SUCCESS;
618}
619
620
621/**
622 * Loads a saved programmable interval timer device state.
623 *
624 * @returns VBox status code.
625 * @param pDevIns The device instance.
626 * @param pSSMHandle The handle to the saved state.
627 * @param u32Version The data unit version number.
628 */
629static DECLCALLBACK(int) rtcLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle, uint32_t u32Version)
630{
631 RTCState *pThis = PDMINS_2_DATA(pDevIns, RTCState *);
632
633 if (u32Version != 1)
634 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
635
636 SSMR3GetMem(pSSMHandle, pThis->cmos_data, 128);
637 SSMR3GetU8(pSSMHandle, &pThis->cmos_index);
638
639 SSMR3GetS32(pSSMHandle, &pThis->current_tm.tm_sec);
640 SSMR3GetS32(pSSMHandle, &pThis->current_tm.tm_min);
641 SSMR3GetS32(pSSMHandle, &pThis->current_tm.tm_hour);
642 SSMR3GetS32(pSSMHandle, &pThis->current_tm.tm_wday);
643 SSMR3GetS32(pSSMHandle, &pThis->current_tm.tm_mday);
644 SSMR3GetS32(pSSMHandle, &pThis->current_tm.tm_mon);
645 SSMR3GetS32(pSSMHandle, &pThis->current_tm.tm_year);
646
647 TMR3TimerLoad(pThis->CTX_SUFF(pPeriodicTimer), pSSMHandle);
648
649 SSMR3GetS64(pSSMHandle, &pThis->next_periodic_time);
650
651 SSMR3GetS64(pSSMHandle, &pThis->next_second_time);
652 TMR3TimerLoad(pThis->CTX_SUFF(pSecondTimer), pSSMHandle);
653 TMR3TimerLoad(pThis->CTX_SUFF(pSecondTimer2), pSSMHandle);
654
655 int period_code = pThis->cmos_data[RTC_REG_A] & 0x0f;
656 if ( period_code != 0
657 && (pThis->cmos_data[RTC_REG_B] & REG_B_PIE)) {
658 if (period_code <= 2)
659 period_code += 7;
660 int period = 1 << (period_code - 1);
661 LogRel(("RTC: period=%#x (%d) %u Hz (restore)\n", period, period, _32K / period));
662 pThis->CurPeriod = period;
663 } else {
664 LogRel(("RTC: stopped the periodic timer (restore)\n"));
665 pThis->CurPeriod = 0;
666 }
667 pThis->cRelLogEntries = 0;
668 return VINF_SUCCESS;
669}
670
671
672/* -=-=-=-=-=- PDM Interface provided by the RTC device -=-=-=-=-=- */
673
674/**
675 * Calculate and update the standard CMOS checksum.
676 *
677 * @param pThis Pointer to the RTC state data.
678 */
679static void rtcCalcCRC(RTCState *pThis)
680{
681 uint16_t u16;
682 unsigned i;
683
684 for (i = RTC_CRC_START, u16 = 0; i <= RTC_CRC_LAST; i++)
685 u16 += pThis->cmos_data[i];
686 pThis->cmos_data[RTC_CRC_LOW] = u16 & 0xff;
687 pThis->cmos_data[RTC_CRC_HIGH] = (u16 >> 8) & 0xff;
688}
689
690
691/**
692 * Write to a CMOS register and update the checksum if necessary.
693 *
694 * @returns VBox status code.
695 * @param pDevIns Device instance of the RTC.
696 * @param iReg The CMOS register index.
697 * @param u8Value The CMOS register value.
698 */
699static DECLCALLBACK(int) rtcCMOSWrite(PPDMDEVINS pDevIns, unsigned iReg, uint8_t u8Value)
700{
701 RTCState *pThis = PDMINS_2_DATA(pDevIns, RTCState *);
702 if (iReg < RT_ELEMENTS(pThis->cmos_data))
703 {
704 pThis->cmos_data[iReg] = u8Value;
705
706 /* does it require checksum update? */
707 if ( iReg >= RTC_CRC_START
708 && iReg <= RTC_CRC_LAST)
709 rtcCalcCRC(pThis);
710
711 return VINF_SUCCESS;
712 }
713 AssertMsgFailed(("iReg=%d\n", iReg));
714 return VERR_INVALID_PARAMETER;
715}
716
717
718/**
719 * Read a CMOS register.
720 *
721 * @returns VBox status code.
722 * @param pDevIns Device instance of the RTC.
723 * @param iReg The CMOS register index.
724 * @param pu8Value Where to store the CMOS register value.
725 */
726static DECLCALLBACK(int) rtcCMOSRead(PPDMDEVINS pDevIns, unsigned iReg, uint8_t *pu8Value)
727{
728 RTCState *pThis = PDMINS_2_DATA(pDevIns, RTCState *);
729 if (iReg < RT_ELEMENTS(pThis->cmos_data))
730 {
731 *pu8Value = pThis->cmos_data[iReg];
732 return VINF_SUCCESS;
733 }
734 AssertMsgFailed(("iReg=%d\n", iReg));
735 return VERR_INVALID_PARAMETER;
736}
737
738
739/* -=-=-=-=-=- based on bits from pc.c -=-=-=-=-=- */
740
741/** @copydoc FNPDMDEVINITCOMPLETE */
742static DECLCALLBACK(int) rtcInitComplete(PPDMDEVINS pDevIns)
743{
744 /** @todo this should be (re)done at power on if we didn't load a state... */
745 RTCState *pThis = PDMINS_2_DATA(pDevIns, RTCState *);
746
747 /*
748 * Set the CMOS date/time.
749 */
750 RTTIMESPEC Now;
751 PDMDevHlpUTCNow(pDevIns, &Now);
752 RTTIME Time;
753 if (pThis->fUTC)
754 RTTimeExplode(&Time, &Now);
755 else
756 RTTimeLocalExplode(&Time, &Now);
757
758 struct my_tm Tm;
759 memset(&Tm, 0, sizeof(Tm));
760 Tm.tm_year = Time.i32Year - 1900;
761 Tm.tm_mon = Time.u8Month - 1;
762 Tm.tm_mday = Time.u8MonthDay;
763 Tm.tm_wday = (Time.u8WeekDay - 1 + 7) % 7; /* 0 = monday -> sunday */
764 Tm.tm_yday = Time.u16YearDay - 1;
765 Tm.tm_hour = Time.u8Hour;
766 Tm.tm_min = Time.u8Minute;
767 Tm.tm_sec = Time.u8Second;
768
769 rtc_set_date(pThis, &Tm);
770
771 int iYear = to_bcd(pThis, (Tm.tm_year / 100) + 19); /* tm_year is 1900 based */
772 rtc_set_memory(pThis, 0x32, iYear); /* 32h - Century Byte (BCD value for the century */
773 rtc_set_memory(pThis, 0x37, iYear); /* 37h - (IBM PS/2) Date Century Byte */
774
775 /*
776 * Recalculate the checksum just in case.
777 */
778 rtcCalcCRC(pThis);
779
780 Log(("CMOS: \n%16.128Rhxd\n", pThis->cmos_data));
781 return VINF_SUCCESS;
782}
783
784
785/* -=-=-=-=-=- real code -=-=-=-=-=- */
786
787/**
788 * @copydoc
789 */
790static DECLCALLBACK(void) rtcRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
791{
792 RTCState *pThis = PDMINS_2_DATA(pDevIns, RTCState *);
793
794 pThis->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
795 pThis->pPeriodicTimerRC = TMTimerRCPtr(pThis->pPeriodicTimerR3);
796 pThis->pSecondTimerRC = TMTimerRCPtr(pThis->pSecondTimerR3);
797 pThis->pSecondTimer2RC = TMTimerRCPtr(pThis->pSecondTimer2R3);
798}
799
800
801/**
802 * Construct a device instance for a VM.
803 *
804 * @returns VBox status.
805 * @param pDevIns The device instance data.
806 * If the registration structure is needed, pDevIns->pDevReg points to it.
807 * @param iInstance Instance number. Use this to figure out which registers and such to use.
808 * The device number is also found in pDevIns->iInstance, but since it's
809 * likely to be freqently used PDM passes it as parameter.
810 * @param pCfgHandle Configuration node handle for the device. Use this to obtain the configuration
811 * of the device instance. It's also found in pDevIns->pCfgHandle, but like
812 * iInstance it's expected to be used a bit in this function.
813 */
814static DECLCALLBACK(int) rtcConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfgHandle)
815{
816 RTCState *pThis = PDMINS_2_DATA(pDevIns, RTCState *);
817 int rc;
818 uint8_t u8Irq;
819 uint16_t u16Base;
820 bool fGCEnabled;
821 bool fR0Enabled;
822 Assert(iInstance == 0);
823
824 /*
825 * Validate configuration.
826 */
827 if (!CFGMR3AreValuesValid(pCfgHandle, "Irq\0" "Base\0" "GCEnabled\0" "R0Enabled\0"))
828 return VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES;
829
830 /*
831 * Init the data.
832 */
833 rc = CFGMR3QueryU8Def(pCfgHandle, "Irq", &u8Irq, 8);
834 if (RT_FAILURE(rc))
835 return PDMDEV_SET_ERROR(pDevIns, rc,
836 N_("Configuration error: Querying \"Irq\" as a uint8_t failed"));
837
838 rc = CFGMR3QueryU16Def(pCfgHandle, "Base", &u16Base, 0x70);
839 if (RT_FAILURE(rc))
840 return PDMDEV_SET_ERROR(pDevIns, rc,
841 N_("Configuration error: Querying \"Base\" as a uint16_t failed"));
842
843 rc = CFGMR3QueryBoolDef(pCfgHandle, "GCEnabled", &fGCEnabled, true);
844 if (RT_FAILURE(rc))
845 return PDMDEV_SET_ERROR(pDevIns, rc,
846 N_("Configuration error: failed to read GCEnabled as boolean"));
847
848 rc = CFGMR3QueryBoolDef(pCfgHandle, "R0Enabled", &fR0Enabled, true);
849 if (RT_FAILURE(rc))
850 return PDMDEV_SET_ERROR(pDevIns, rc,
851 N_("Configuration error: failed to read R0Enabled as boolean"));
852
853 Log(("RTC: Irq=%#x Base=%#x fGCEnabled=%RTbool fR0Enabled=%RTbool\n", u8Irq, u16Base, fGCEnabled, fR0Enabled));
854
855
856 pThis->pDevInsR3 = pDevIns;
857 pThis->pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
858 pThis->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
859 pThis->irq = u8Irq;
860 pThis->cmos_data[RTC_REG_A] = 0x26;
861 pThis->cmos_data[RTC_REG_B] = 0x02;
862 pThis->cmos_data[RTC_REG_C] = 0x00;
863 pThis->cmos_data[RTC_REG_D] = 0x80;
864 pThis->RtcReg.u32Version = PDM_RTCREG_VERSION;
865 pThis->RtcReg.pfnRead = rtcCMOSRead;
866 pThis->RtcReg.pfnWrite = rtcCMOSWrite;
867
868 /*
869 * Create timers, arm them, register I/O Ports and save state.
870 */
871 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL_SYNC, rtcTimerPeriodic, "MC146818 RTC/CMOS - Periodic", &pThis->pPeriodicTimerR3);
872 if (RT_FAILURE(rc))
873 return rc;
874 pThis->pPeriodicTimerR0 = TMTimerR0Ptr(pThis->pPeriodicTimerR3);
875 pThis->pPeriodicTimerRC = TMTimerRCPtr(pThis->pPeriodicTimerR3);
876
877 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL_SYNC, rtcTimerSecond, "MC146818 RTC/CMOS - Second", &pThis->pSecondTimerR3);
878 if (RT_FAILURE(rc))
879 return rc;
880 pThis->pSecondTimerR0 = TMTimerR0Ptr(pThis->pSecondTimerR3);
881 pThis->pSecondTimerRC = TMTimerRCPtr(pThis->pSecondTimerR3);
882
883 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL_SYNC, rtcTimerSecond2, "MC146818 RTC/CMOS - Second2", &pThis->pSecondTimer2R3);
884 if (RT_FAILURE(rc))
885 return rc;
886 pThis->pSecondTimer2R0 = TMTimerR0Ptr(pThis->pSecondTimer2R3);
887 pThis->pSecondTimer2RC = TMTimerRCPtr(pThis->pSecondTimer2R3);
888 pThis->next_second_time = TMTimerGet(pThis->CTX_SUFF(pSecondTimer2)) + (TMTimerGetFreq(pThis->CTX_SUFF(pSecondTimer2)) * 99) / 100;
889 rc = TMTimerSet(pThis->CTX_SUFF(pSecondTimer2), pThis->next_second_time);
890 if (RT_FAILURE(rc))
891 return rc;
892
893 rc = PDMDevHlpIOPortRegister(pDevIns, u16Base, 2, NULL, rtcIOPortWrite, rtcIOPortRead, NULL, NULL, "MC146818 RTC/CMOS");
894 if (RT_FAILURE(rc))
895 return rc;
896 if (fGCEnabled)
897 {
898 rc = PDMDevHlpIOPortRegisterGC(pDevIns, u16Base, 2, 0, "rtcIOPortWrite", "rtcIOPortRead", NULL, NULL, "MC146818 RTC/CMOS");
899 if (RT_FAILURE(rc))
900 return rc;
901 }
902 if (fR0Enabled)
903 {
904 rc = PDMDevHlpIOPortRegisterR0(pDevIns, u16Base, 2, 0, "rtcIOPortWrite", "rtcIOPortRead", NULL, NULL, "MC146818 RTC/CMOS");
905 if (RT_FAILURE(rc))
906 return rc;
907 }
908
909 rc = PDMDevHlpSSMRegister(pDevIns, pDevIns->pDevReg->szDeviceName, iInstance, 1 /* version */, sizeof(*pThis),
910 NULL, rtcSaveExec, NULL,
911 NULL, rtcLoadExec, NULL);
912 if (RT_FAILURE(rc))
913 return rc;
914
915 /*
916 * Register ourselves as the RTC/CMOS with PDM.
917 */
918 rc = pDevIns->pDevHlpR3->pfnRTCRegister(pDevIns, &pThis->RtcReg, &pThis->pRtcHlpR3);
919 if (RT_FAILURE(rc))
920 return rc;
921
922 return VINF_SUCCESS;
923}
924
925
926/**
927 * The device registration structure.
928 */
929const PDMDEVREG g_DeviceMC146818 =
930{
931 /* u32Version */
932 PDM_DEVREG_VERSION,
933 /* szDeviceName */
934 "mc146818",
935 /* szRCMod */
936 "VBoxDDGC.gc",
937 /* szR0Mod */
938 "VBoxDDR0.r0",
939 /* pszDescription */
940 "Motorola MC146818 RTC/CMOS Device.",
941 /* fFlags */
942 PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_32_64 | PDM_DEVREG_FLAGS_PAE36 | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
943 /* fClass */
944 PDM_DEVREG_CLASS_RTC,
945 /* cMaxInstances */
946 1,
947 /* cbInstance */
948 sizeof(RTCState),
949 /* pfnConstruct */
950 rtcConstruct,
951 /* pfnDestruct */
952 NULL,
953 /* pfnRelocate */
954 rtcRelocate,
955 /* pfnIOCtl */
956 NULL,
957 /* pfnPowerOn */
958 NULL,
959 /* pfnReset */
960 NULL,
961 /* pfnSuspend */
962 NULL,
963 /* pfnResume */
964 NULL,
965 /* pfnAttach */
966 NULL,
967 /* pfnDetach */
968 NULL,
969 /* pfnQueryInterface */
970 NULL,
971 /* pfnInitComplete */
972 rtcInitComplete,
973 /* pfnPowerOff */
974 NULL,
975 /* pfnSoftReset */
976 NULL,
977 /* u32VersionEnd */
978 PDM_DEVREG_VERSION
979};
980#endif /* IN_RING3 */
981#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
982
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette