4 * @brief RTC & CMOS abstraction. Reference: MC146818A & Intel Series 500 PCH datasheet
8 * @copyright Copyright (c) 2022
12 #include <klibc/string.h>
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);
20 // Make sure the rtc timer is disabled by default
25 rtc_read_reg(uint8_t reg_selector)
27 io_outb(RTC_INDEX_PORT, reg_selector);
28 return io_inb(RTC_TARGET_PORT);
32 rtc_write_reg(uint8_t reg_selector, uint8_t val)
34 io_outb(RTC_INDEX_PORT, reg_selector);
35 io_outb(RTC_TARGET_PORT, val);
41 return ((bcd & 0xF0) >> 1) + ((bcd & 0xF0) >> 3) + (bcd & 0xf);
45 rtc_date_same(rtc_datetime* a, rtc_datetime* b) {
46 return a->year == b->year &&
47 a->month == b->month &&
49 a->weekday == b->weekday &&
50 a->minute == b->minute &&
51 a->second == b->second;
55 rtc_get_datetime(rtc_datetime* datetime)
61 while (rtc_read_reg(RTC_REG_A) & 0x80);
62 memcpy(¤t, datetime, sizeof(rtc_datetime));
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, ¤t));
73 uint8_t regbv = rtc_read_reg(RTC_REG_B);
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);
87 if (!RTC_24HRS_ENCODED(regbv) && (datetime->hour >> 7)) {
88 datetime->hour = (12 + datetime->hour & 0x80);
91 datetime->year += RTC_CURRENT_CENTRY * 100;
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);
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);