forked from Rujuta/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmos.c
84 lines (71 loc) · 1.67 KB
/
cmos.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "types.h"
#include "defs.h"
#include "date.h"
#include "x86.h"
#include "cmos.h"
uint cmosread(uint reg)
{
if(reg >= CMOS_NMI_BIT)
panic("cmosread: invalid register");
outb(CMOS_PORT, reg);
microdelay(200);
return inb(CMOS_RETURN);
}
void cmoswrite(uint reg, uint data)
{
if(reg >= CMOS_NMI_BIT)
panic("cmoswrite: invalid register");
if(data > 0xff)
panic("cmoswrite: invalid data");
outb(CMOS_PORT, reg);
microdelay(200);
outb(CMOS_RETURN, data);
}
static void fill_rtcdate(struct rtcdate *r)
{
r->second = cmosread(CMOS_SECS);
r->minute = cmosread(CMOS_MINS);
r->hour = cmosread(CMOS_HOURS);
r->day = cmosread(CMOS_DAY);
r->month = cmosread(CMOS_MONTH);
r->year = cmosread(CMOS_YEAR);
}
// qemu seems to use 24-hour GWT and the values are BCD encoded
void cmostime(struct rtcdate *r)
{
struct rtcdate t1, t2;
int sb, bcd, tf;
sb = cmosread(CMOS_STATB);
bcd = (sb & CMOS_BINARY_BIT) == 0;
tf = (sb & CMOS_24H_BIT) != 0;
// make sure CMOS doesn't modify time while we read it
for(;;){
fill_rtcdate(&t1);
if(cmosread(CMOS_STATA) & CMOS_UIP_BIT)
continue;
fill_rtcdate(&t2);
if(memcmp(&t1, &t2, sizeof(t1)) == 0)
break;
}
// backup raw values since BCD conversion removes PM bit from hour
t2 = t1;
// convert t1 from BCD
if(bcd){
#define CONV(x) (t1.x = ((t1.x >> 4) * 10) + (t1.x & 0xf))
CONV(second);
CONV(minute);
CONV(hour );
CONV(day );
CONV(month );
CONV(year );
#undef CONV
}
// convert 12 hour format to 24 hour format
if(!tf){
if(t2.hour & CMOS_PM_BIT){
t1.hour = (t1.hour + 12) % 24;
}
}
*r = t1;
r->year += 2000;
}