60a1aed86654652bb225e655fdd46669aa439719
[lunaix-os.git] / lunaix-os / hal / rtc.c
1 /**
2  * @file rtc.c
3  * @author Lunaixsky
4  * @brief RTC & CMOS abstraction. Reference: MC146818A & Intel Series 500 PCH datasheet
5  * @version 0.1
6  * @date 2022-03-07
7  * 
8  * @copyright Copyright (c) 2022
9  * 
10  */
11 #include <hal/rtc.h>
12 #include <klibc/string.h>
13
14 void
15 rtc_init() {
16     uint8_t regA = rtc_read_reg(RTC_REG_A | WITH_NMI_DISABLED);
17     regA = (regA & ~0x7f) | RTC_FREQUENCY_1024HZ | RTC_DIVIDER_33KHZ;
18     rtc_write_reg(RTC_REG_A | WITH_NMI_DISABLED, regA);
19
20     // Make sure the rtc timer is disabled by default
21     rtc_disable_timer();
22 }
23
24 uint8_t
25 rtc_read_reg(uint8_t reg_selector)
26 {
27     io_outb(RTC_INDEX_PORT, reg_selector);
28     return io_inb(RTC_TARGET_PORT);
29 }
30
31 void
32 rtc_write_reg(uint8_t reg_selector, uint8_t val)
33 {
34     io_outb(RTC_INDEX_PORT, reg_selector);
35     io_outb(RTC_TARGET_PORT, val);
36 }
37
38 uint8_t
39 bcd2dec(uint8_t bcd)
40 {
41     return ((bcd & 0xF0) >> 1) + ((bcd & 0xF0) >> 3) + (bcd & 0xf);
42 }
43
44 int
45 rtc_date_same(rtc_datetime* a, rtc_datetime* b) {
46     return a->year == b->year &&
47            a->month == b->month &&
48            a->day == b->day &&
49            a->weekday == b->weekday &&
50            a->minute == b->minute &&
51            a->second == b->second;
52 }
53
54 void
55 rtc_get_datetime(rtc_datetime* datetime)
56 {
57     rtc_datetime current;
58     
59     do
60     {
61         while (rtc_read_reg(RTC_REG_A) & 0x80);
62         memcpy(&current, datetime, sizeof(rtc_datetime));
63
64         datetime->year = rtc_read_reg(RTC_REG_YRS);
65         datetime->month = rtc_read_reg(RTC_REG_MTH);
66         datetime->day = rtc_read_reg(RTC_REG_DAY);
67         datetime->weekday = rtc_read_reg(RTC_REG_WDY);
68         datetime->hour = rtc_read_reg(RTC_REG_HRS);
69         datetime->minute = rtc_read_reg(RTC_REG_MIN);
70         datetime->second = rtc_read_reg(RTC_REG_SEC);
71     } while (!rtc_date_same(datetime, &current));
72
73     uint8_t regbv = rtc_read_reg(RTC_REG_B);
74
75     // Convert from bcd to binary when needed
76     if (!RTC_BIN_ENCODED(regbv)) {
77         datetime->year = bcd2dec(datetime->year);
78         datetime->month = bcd2dec(datetime->month);
79         datetime->day = bcd2dec(datetime->day);
80         datetime->hour = bcd2dec(datetime->hour);
81         datetime->minute = bcd2dec(datetime->minute);
82         datetime->second = bcd2dec(datetime->second);
83     }
84
85
86     // To 24 hour format
87     if (!RTC_24HRS_ENCODED(regbv) && (datetime->hour >> 7)) {
88         datetime->hour = (12 + datetime->hour & 0x80);
89     }
90
91     datetime->year += RTC_CURRENT_CENTRY * 100;
92 }
93
94 void
95 rtc_enable_timer() {
96     uint8_t regB = rtc_read_reg(RTC_REG_B | WITH_NMI_DISABLED);
97     rtc_write_reg(RTC_REG_B | WITH_NMI_DISABLED, regB | RTC_TIMER_ON);
98 }
99
100 void
101 rtc_disable_timer() {
102     uint8_t regB = rtc_read_reg(RTC_REG_B | WITH_NMI_DISABLED);
103     rtc_write_reg(RTC_REG_B | WITH_NMI_DISABLED, regB & ~RTC_TIMER_ON);
104 }