kvm tools, setup: Create private directory
[linux-2.6/next.git] / tools / kvm / hw / rtc.c
blobc6879cce9fe886e0e75b742e8f0cfca46f3ae358
1 #include "kvm/rtc.h"
3 #include "kvm/ioport.h"
4 #include "kvm/kvm.h"
6 #include <time.h>
8 static u8 cmos_index;
10 #define CMOS_RTC_SECONDS 0x00
11 #define CMOS_RTC_MINUTES 0x02
12 #define CMOS_RTC_HOURS 0x04
13 #define CMOS_RTC_DATE_OF_MONTH 0x07
14 #define CMOS_RTC_MONTH 0x08
15 #define CMOS_RTC_YEAR 0x09
17 static inline unsigned char bin2bcd(unsigned val)
19 return ((val / 10) << 4) + val % 10;
22 static bool cmos_ram_data_in(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size)
24 struct tm *tm;
25 time_t ti;
27 time(&ti);
29 tm = gmtime(&ti);
31 switch (cmos_index) {
32 case CMOS_RTC_SECONDS:
33 ioport__write8(data, bin2bcd(tm->tm_sec));
34 break;
35 case CMOS_RTC_MINUTES:
36 ioport__write8(data, bin2bcd(tm->tm_min));
37 break;
38 case CMOS_RTC_HOURS:
39 ioport__write8(data, bin2bcd(tm->tm_hour));
40 break;
41 case CMOS_RTC_DATE_OF_MONTH:
42 ioport__write8(data, bin2bcd(tm->tm_mday));
43 break;
44 case CMOS_RTC_MONTH:
45 ioport__write8(data, bin2bcd(tm->tm_mon + 1));
46 break;
47 case CMOS_RTC_YEAR:
48 ioport__write8(data, bin2bcd(tm->tm_year));
49 break;
52 return true;
55 static bool cmos_ram_data_out(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size)
57 return true;
60 static struct ioport_operations cmos_ram_data_ioport_ops = {
61 .io_out = cmos_ram_data_out,
62 .io_in = cmos_ram_data_in,
65 static bool cmos_ram_index_out(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size)
67 u8 value;
69 value = ioport__read8(data);
71 kvm->nmi_disabled = value & (1UL << 7);
73 cmos_index = value & ~(1UL << 7);
75 return true;
78 static struct ioport_operations cmos_ram_index_ioport_ops = {
79 .io_out = cmos_ram_index_out,
82 void rtc__init(void)
84 /* PORT 0070-007F - CMOS RAM/RTC (REAL TIME CLOCK) */
85 ioport__register(0x0070, &cmos_ram_index_ioport_ops, 1, NULL);
86 ioport__register(0x0071, &cmos_ram_data_ioport_ops, 1, NULL);