2019-04-24 18:43:05 +02:00
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
// GNU GPL OS/K //
|
|
|
|
// //
|
2019-05-07 18:35:46 +02:00
|
|
|
// Desc: RTC Time related functions //
|
2019-04-24 18:43:05 +02:00
|
|
|
// //
|
|
|
|
// //
|
|
|
|
// Copyright © 2018-2019 The OS/K Team //
|
|
|
|
// //
|
|
|
|
// This file is part of OS/K. //
|
|
|
|
// //
|
|
|
|
// OS/K is free software: you can redistribute it and/or modify //
|
|
|
|
// it under the terms of the GNU General Public License as published by //
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or //
|
|
|
|
// any later version. //
|
|
|
|
// //
|
|
|
|
// OS/K is distributed in the hope that it will be useful, //
|
|
|
|
// but WITHOUT ANY WARRANTY//without even the implied warranty of //
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
|
|
|
// GNU General Public License for more details. //
|
|
|
|
// //
|
|
|
|
// You should have received a copy of the GNU General Public License //
|
|
|
|
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
|
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
|
|
|
|
#include <kernel/base.h>
|
|
|
|
#include <kernel/iomisc.h>
|
2019-04-24 21:08:44 +02:00
|
|
|
#include <extras/buf.h>
|
2019-04-25 13:08:54 +02:00
|
|
|
#include <kernel/time.h>
|
2019-04-24 18:43:05 +02:00
|
|
|
|
2019-04-25 16:31:06 +02:00
|
|
|
static ulong IoRtcTicks = 0;
|
2019-05-05 23:29:17 +02:00
|
|
|
static char IoTimeChar[22] = { 0 };
|
2019-04-25 16:31:06 +02:00
|
|
|
static uchar RtcRate = 0x05; //2048Hz
|
|
|
|
static Time_t IoRtcOriginTime;
|
|
|
|
static Time_t IoRtcTime;
|
|
|
|
static char time24or12Mode;
|
2019-04-24 21:08:44 +02:00
|
|
|
|
2019-04-25 16:31:06 +02:00
|
|
|
static void GetTimeFromRtc(void)
|
2019-04-25 12:23:45 +02:00
|
|
|
{
|
|
|
|
Time_t lastTime;
|
|
|
|
|
|
|
|
char updateInProgress = 1;
|
|
|
|
|
|
|
|
while(updateInProgress) { // wait while the RTC updates its value
|
|
|
|
IoWriteByteOnPort(0x70, 0x0A);
|
|
|
|
updateInProgress = (IoReadByteFromPort(0x71) & 0x80);
|
|
|
|
}
|
|
|
|
|
|
|
|
IoWriteByteOnPort(0x70, 0x0);
|
2019-04-25 16:31:06 +02:00
|
|
|
IoRtcOriginTime.sec = IoReadByteFromPort(0x71);
|
2019-04-25 12:23:45 +02:00
|
|
|
IoWriteByteOnPort(0x70, 0x02);
|
2019-04-25 16:31:06 +02:00
|
|
|
IoRtcOriginTime.min = IoReadByteFromPort(0x71);
|
2019-04-25 12:23:45 +02:00
|
|
|
IoWriteByteOnPort(0x70, 0x04);
|
2019-04-25 16:31:06 +02:00
|
|
|
IoRtcOriginTime.hour = IoReadByteFromPort(0x71);
|
2019-04-25 12:23:45 +02:00
|
|
|
IoWriteByteOnPort(0x70, 0x06);
|
2019-04-25 16:31:06 +02:00
|
|
|
IoRtcOriginTime.weekday = IoReadByteFromPort(0x71);
|
2019-04-25 12:23:45 +02:00
|
|
|
IoWriteByteOnPort(0x70, 0x07);
|
2019-04-25 16:31:06 +02:00
|
|
|
IoRtcOriginTime.day = IoReadByteFromPort(0x71);
|
2019-04-25 12:23:45 +02:00
|
|
|
IoWriteByteOnPort(0x70, 0x08);
|
2019-04-25 16:31:06 +02:00
|
|
|
IoRtcOriginTime.month = IoReadByteFromPort(0x71);
|
2019-04-25 12:23:45 +02:00
|
|
|
IoWriteByteOnPort(0x70, 0x09);
|
2019-04-25 16:31:06 +02:00
|
|
|
IoRtcOriginTime.year = IoReadByteFromPort(0x71);
|
2019-04-25 12:23:45 +02:00
|
|
|
IoWriteByteOnPort(0x70, 0x32);
|
2019-04-25 16:31:06 +02:00
|
|
|
IoRtcOriginTime.century = IoReadByteFromPort(0x71);
|
2019-04-25 12:23:45 +02:00
|
|
|
|
|
|
|
// Now while we don't get the same value, read the registers (ensure data are valid)
|
|
|
|
do {
|
2019-04-25 16:31:06 +02:00
|
|
|
lastTime.sec = IoRtcOriginTime.sec;
|
|
|
|
lastTime.min = IoRtcOriginTime.min;
|
|
|
|
lastTime.hour = IoRtcOriginTime.hour;
|
|
|
|
lastTime.weekday = IoRtcOriginTime.weekday;
|
|
|
|
lastTime.day = IoRtcOriginTime.day;
|
|
|
|
lastTime.month = IoRtcOriginTime.month;
|
|
|
|
lastTime.year = IoRtcOriginTime.year;
|
|
|
|
lastTime.century = IoRtcOriginTime.century;
|
2019-04-25 12:23:45 +02:00
|
|
|
|
|
|
|
while(updateInProgress) { // wait while the RTC updates its value
|
|
|
|
IoWriteByteOnPort(0x70, 0x0A);
|
|
|
|
updateInProgress = (IoReadByteFromPort(0x71) & 0x80);
|
|
|
|
}
|
|
|
|
|
|
|
|
IoWriteByteOnPort(0x70, 0x0);
|
2019-04-25 16:31:06 +02:00
|
|
|
IoRtcOriginTime.sec = IoReadByteFromPort(0x71);
|
2019-04-25 12:23:45 +02:00
|
|
|
IoWriteByteOnPort(0x70, 0x02);
|
2019-04-25 16:31:06 +02:00
|
|
|
IoRtcOriginTime.min = IoReadByteFromPort(0x71);
|
2019-04-25 12:23:45 +02:00
|
|
|
IoWriteByteOnPort(0x70, 0x04);
|
2019-04-25 16:31:06 +02:00
|
|
|
IoRtcOriginTime.hour = IoReadByteFromPort(0x71);
|
2019-04-25 12:23:45 +02:00
|
|
|
IoWriteByteOnPort(0x70, 0x06);
|
2019-04-25 16:31:06 +02:00
|
|
|
IoRtcOriginTime.weekday = IoReadByteFromPort(0x71);
|
2019-04-25 12:23:45 +02:00
|
|
|
IoWriteByteOnPort(0x70, 0x07);
|
2019-04-25 16:31:06 +02:00
|
|
|
IoRtcOriginTime.day = IoReadByteFromPort(0x71);
|
2019-04-25 12:23:45 +02:00
|
|
|
IoWriteByteOnPort(0x70, 0x08);
|
2019-04-25 16:31:06 +02:00
|
|
|
IoRtcOriginTime.month = IoReadByteFromPort(0x71);
|
2019-04-25 12:23:45 +02:00
|
|
|
IoWriteByteOnPort(0x70, 0x09);
|
2019-04-25 16:31:06 +02:00
|
|
|
IoRtcOriginTime.year = IoReadByteFromPort(0x71);
|
2019-04-25 12:23:45 +02:00
|
|
|
IoWriteByteOnPort(0x70, 0x32);
|
2019-04-25 16:31:06 +02:00
|
|
|
IoRtcOriginTime.century = IoReadByteFromPort(0x71);
|
2019-04-25 12:23:45 +02:00
|
|
|
|
2019-04-25 16:31:06 +02:00
|
|
|
} while ( (lastTime.sec != IoRtcOriginTime.sec) || (lastTime.min != IoRtcOriginTime.min) ||
|
|
|
|
(lastTime.hour != IoRtcOriginTime.hour) || (lastTime.weekday != IoRtcOriginTime.weekday) ||
|
|
|
|
(lastTime.day != IoRtcOriginTime.day) || (lastTime.month != IoRtcOriginTime.month) ||
|
|
|
|
(lastTime.year != IoRtcOriginTime.year) || (lastTime.century != IoRtcOriginTime.century)
|
2019-04-25 12:23:45 +02:00
|
|
|
);
|
|
|
|
|
2019-04-25 16:31:06 +02:00
|
|
|
IoWriteByteOnPort(0x70, 0x0B);
|
2019-04-25 12:23:45 +02:00
|
|
|
time24or12Mode = IoReadByteFromPort(0x71);
|
|
|
|
// Convert to binary if it is necessary
|
|
|
|
if (!(time24or12Mode & 0x04)) {
|
2019-04-25 16:31:06 +02:00
|
|
|
IoRtcOriginTime.sec = (IoRtcOriginTime.sec & 0x0F)
|
|
|
|
+ ((IoRtcOriginTime.sec / 16) * 10);
|
|
|
|
IoRtcOriginTime.min = (IoRtcOriginTime.min & 0x0F)
|
|
|
|
+ ((IoRtcOriginTime.min / 16) * 10);
|
|
|
|
IoRtcOriginTime.hour = ( (IoRtcOriginTime.hour & 0x0F)
|
|
|
|
+ (((IoRtcOriginTime.hour & 0x70) / 16) * 10) )
|
|
|
|
| (IoRtcOriginTime.hour & 0x80);
|
|
|
|
IoRtcOriginTime.day = (IoRtcOriginTime.day & 0x0F) + ((IoRtcOriginTime.day / 16) * 10);
|
|
|
|
IoRtcOriginTime.month = (IoRtcOriginTime.month & 0x0F)
|
|
|
|
+ ((IoRtcOriginTime.month / 16) * 10);
|
|
|
|
IoRtcOriginTime.year = (IoRtcOriginTime.year & 0x0F)
|
|
|
|
+ ((IoRtcOriginTime.year / 16) * 10);
|
|
|
|
IoRtcOriginTime.century = (IoRtcOriginTime.century & 0x0F)
|
|
|
|
+ ((IoRtcOriginTime.century / 16) * 10);
|
|
|
|
IoRtcOriginTime.weekday = (IoRtcOriginTime.weekday & 0x0F)
|
|
|
|
+ ((IoRtcOriginTime.weekday / 16) * 10);
|
2019-04-25 12:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert 12 to 24 hour if necessary
|
2019-04-25 16:31:06 +02:00
|
|
|
if (!(time24or12Mode & 0x02) && (IoRtcOriginTime.hour & 0x80)) {
|
|
|
|
IoRtcOriginTime.hour = ((IoRtcOriginTime.hour & 0x7)+ 10) % 24;
|
2019-04-25 12:23:45 +02:00
|
|
|
}
|
2019-04-25 16:31:06 +02:00
|
|
|
|
|
|
|
IoRtcTime.sec = IoRtcOriginTime.sec;
|
|
|
|
IoRtcTime.min = IoRtcOriginTime.min;
|
|
|
|
IoRtcTime.hour = IoRtcOriginTime.hour;
|
|
|
|
IoRtcTime.weekday = IoRtcOriginTime.weekday;
|
|
|
|
IoRtcTime.day = IoRtcOriginTime.day;
|
|
|
|
IoRtcTime.month = IoRtcOriginTime.month;
|
|
|
|
IoRtcTime.year = IoRtcOriginTime.year;
|
|
|
|
IoRtcTime.century = IoRtcOriginTime.century;
|
2019-04-25 12:23:45 +02:00
|
|
|
}
|
|
|
|
|
2019-04-27 00:04:27 +02:00
|
|
|
void RtcHandler(ISRFrame_t *regs)
|
2019-04-24 21:08:44 +02:00
|
|
|
{
|
|
|
|
//bprintf(BStdOut, " *RTC - ");
|
|
|
|
IoWriteByteOnPort(0x70, 0x0C); // Selects status reg C
|
|
|
|
IoReadByteFromPort(0x71); // Flush
|
|
|
|
IoRtcTicks++;
|
2019-04-24 23:00:12 +02:00
|
|
|
IoSendEOItoPIC(0x28);
|
2019-04-24 21:08:44 +02:00
|
|
|
//bprintf(BStdOut, " - EOI* ");
|
|
|
|
}
|
2019-04-25 12:23:45 +02:00
|
|
|
|
|
|
|
void IoPrintRtcTime(void)
|
|
|
|
{
|
2019-04-25 16:31:06 +02:00
|
|
|
Time_t* RtcTime = IoGetRtcTime();
|
2019-05-05 18:47:36 +02:00
|
|
|
KernLog("[RTC Time] %02d/%02d/%04d ; %02d:%02d:%02d \n\n",
|
2019-04-25 16:31:06 +02:00
|
|
|
RtcTime->day,
|
|
|
|
RtcTime->month,
|
|
|
|
RtcTime->year + RtcTime->century*100,
|
|
|
|
RtcTime->hour,
|
|
|
|
RtcTime->min,
|
|
|
|
RtcTime->sec
|
2019-04-25 12:23:45 +02:00
|
|
|
);
|
|
|
|
}
|
2019-04-25 13:08:54 +02:00
|
|
|
|
2019-05-05 23:29:17 +02:00
|
|
|
char* IoGetRtcTimeChar(void)
|
2019-04-25 16:31:06 +02:00
|
|
|
{
|
2019-05-05 18:47:36 +02:00
|
|
|
Time_t *RtcTime = IoGetRtcTime();
|
2019-05-07 22:32:17 +02:00
|
|
|
snprintf(IoTimeChar, sizeof(IoTimeChar),
|
|
|
|
"%02d/%02d/%02d ; %02d:%02d:%02d",
|
2019-04-25 16:31:06 +02:00
|
|
|
RtcTime->day,
|
|
|
|
RtcTime->month,
|
|
|
|
RtcTime->year + RtcTime->century*100,
|
|
|
|
RtcTime->hour,
|
|
|
|
RtcTime->min,
|
|
|
|
RtcTime->sec
|
|
|
|
);
|
2019-05-05 23:29:17 +02:00
|
|
|
return IoTimeChar;
|
2019-04-25 16:31:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void UpdateRtcTime(void)
|
|
|
|
{
|
|
|
|
ulong frequency = 32768 >> (RtcRate-1);
|
|
|
|
uchar minRemain, hourRemain, dayRemain;
|
|
|
|
|
|
|
|
IoRtcTime.sec =
|
2019-04-28 15:30:54 +02:00
|
|
|
(uchar)(((ulong)IoRtcOriginTime.sec + (IoRtcTicks / frequency)) % 60);
|
2019-04-25 16:31:06 +02:00
|
|
|
minRemain =
|
2019-04-28 15:30:54 +02:00
|
|
|
(uchar)(((ulong)IoRtcOriginTime.sec + (IoRtcTicks / frequency)) / 60);
|
2019-04-25 16:31:06 +02:00
|
|
|
|
|
|
|
IoRtcTime.min =
|
|
|
|
(uchar)(((ulong)IoRtcOriginTime.min + minRemain) % 60);
|
|
|
|
hourRemain =
|
|
|
|
(uchar)(((ulong)IoRtcOriginTime.min + minRemain) / 60);
|
|
|
|
|
|
|
|
IoRtcTime.hour =
|
|
|
|
(uchar)(((ulong)IoRtcOriginTime.hour + hourRemain) % 24);
|
|
|
|
dayRemain =
|
|
|
|
(uchar)(((ulong)IoRtcOriginTime.hour + hourRemain) / 24);
|
|
|
|
|
|
|
|
if (dayRemain) {
|
|
|
|
KeStartPanic("[RTC Time] We must shutdown this computer for your safety.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-25 13:08:54 +02:00
|
|
|
Time_t* IoGetRtcTime(void)
|
|
|
|
{
|
2019-04-25 16:31:06 +02:00
|
|
|
UpdateRtcTime();
|
2019-04-25 13:08:54 +02:00
|
|
|
return &IoRtcTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong IoGetRtcTicks(void)
|
|
|
|
{
|
|
|
|
return IoRtcTicks;
|
|
|
|
}
|
|
|
|
|
2019-04-27 00:04:27 +02:00
|
|
|
void IoEnableRtc(void)
|
|
|
|
{
|
|
|
|
ulong flags = KePauseIRQs();
|
2019-05-05 18:55:00 +02:00
|
|
|
char readInterruptConfig;
|
|
|
|
char readRegister;
|
|
|
|
char readIrqs;
|
2019-04-27 00:04:27 +02:00
|
|
|
|
2019-05-06 22:04:22 +02:00
|
|
|
IdtRegisterIsr(RtcHandler, 0x28);
|
2019-04-27 00:04:27 +02:00
|
|
|
|
|
|
|
// Setting up the register control and interrupt rates
|
|
|
|
DebugLog("[RTC Time] Interrupt frequency set to %d Hz\n",
|
|
|
|
32768 >> (RtcRate-1));
|
|
|
|
|
|
|
|
IoWriteByteOnPort(0x70, 0x8B);
|
2019-05-05 18:55:00 +02:00
|
|
|
readRegister = IoReadByteFromPort(0x71);
|
2019-04-27 00:04:27 +02:00
|
|
|
IoWriteByteOnPort(0x70, 0x8B); // Because reading flushes
|
2019-05-05 18:55:00 +02:00
|
|
|
IoWriteByteOnPort(0x71, readRegister | 0x40);
|
2019-04-27 00:04:27 +02:00
|
|
|
|
|
|
|
IoWriteByteOnPort(0x70, 0x8A);
|
2019-05-05 18:55:00 +02:00
|
|
|
readInterruptConfig = IoReadByteFromPort(0x71);
|
2019-04-27 00:04:27 +02:00
|
|
|
IoWriteByteOnPort(0x70, 0x8A); // Because reading flushes
|
2019-05-05 18:55:00 +02:00
|
|
|
IoWriteByteOnPort(0x71, (readInterruptConfig & 0xF0) | RtcRate);
|
2019-04-27 00:04:27 +02:00
|
|
|
IoWriteByteOnPort(0x70, 0x0C);
|
|
|
|
IoReadByteFromPort(0x71); // Flush
|
|
|
|
|
|
|
|
// Setting up the IRQs
|
2019-05-05 18:55:00 +02:00
|
|
|
readIrqs = IoReadByteFromPort(0xA1);
|
|
|
|
IoWriteByteOnPort(0xA1, 0xFE & readIrqs); // Enables IRQ on PIC 2
|
|
|
|
readIrqs = IoReadByteFromPort(0x21);
|
|
|
|
IoWriteByteOnPort(0x21, 0xFB & readIrqs); // Enables IRQ on PIC 1
|
2019-04-27 00:04:27 +02:00
|
|
|
|
|
|
|
// Clean-up
|
|
|
|
IoWriteByteOnPort(0x70, 0x0C); // Select status reg C
|
|
|
|
IoReadByteFromPort(0x71); // Flush
|
|
|
|
|
|
|
|
GetTimeFromRtc();
|
|
|
|
KeRestoreIRQs(flags);
|
|
|
|
IoEnableNMI();
|
|
|
|
}
|
|
|
|
|
2019-05-07 17:02:25 +02:00
|
|
|
void IoRtcWait(uint time) // time in ms
|
|
|
|
{
|
|
|
|
ulong frequency = 32768 >> (RtcRate-1);
|
|
|
|
ulong beginTick = IoGetRtcTicks();
|
|
|
|
while(IoGetRtcTicks() < beginTick + (frequency/1000) * time) {
|
|
|
|
KePauseCPU();
|
|
|
|
}
|
2019-04-25 16:31:06 +02:00
|
|
|
|
2019-05-07 17:02:25 +02:00
|
|
|
}
|