1
0
mirror of https://gitlab.os-k.eu/os-k-team/os-k.git synced 2023-08-25 14:03:10 +02:00
os-k/kaleid/kernel/ke/rtc.c

229 lines
7.7 KiB
C
Raw Normal View History

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/>. //
//----------------------------------------------------------------------------//
2019-05-13 23:22:27 +02:00
#include <lib/buf.h>
#include <ke/time.h>
2019-05-14 11:48:07 +02:00
#include <ke/idt.h>
2019-04-24 18:43:05 +02:00
2019-05-14 11:48:07 +02:00
static ulong Ticks = 0;
static Time_t OriginTime;
// TODO asnprintf()
static uchar RTC_RATE = 0x05; //2048Hz
2019-04-25 16:31:06 +02:00
static char time24or12Mode;
2019-04-24 21:08:44 +02:00
2019-05-14 11:48:07 +02:00
static void GetTimeFromRTC(void)
2019-04-25 12:23:45 +02:00
{
Time_t lastTime;
char updateInProgress = 1;
2019-05-14 11:48:07 +02:00
// Wait while the RTC updates its value
while (updateInProgress) {
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x0A);
updateInProgress = (IoReadByteFromPort(0x71) & 0x80);
}
IoWriteByteOnPort(0x70, 0x0);
2019-05-14 11:48:07 +02:00
OriginTime.sec = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x02);
2019-05-14 11:48:07 +02:00
OriginTime.min = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x04);
2019-05-14 11:48:07 +02:00
OriginTime.hour = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x06);
2019-05-14 11:48:07 +02:00
OriginTime.weekday = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x07);
2019-05-14 11:48:07 +02:00
OriginTime.day = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x08);
2019-05-14 11:48:07 +02:00
OriginTime.month = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x09);
2019-05-14 11:48:07 +02:00
OriginTime.year = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x32);
2019-05-14 11:48:07 +02:00
OriginTime.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-05-14 11:48:07 +02:00
lastTime.sec = OriginTime.sec;
lastTime.min = OriginTime.min;
lastTime.hour = OriginTime.hour;
lastTime.weekday = OriginTime.weekday;
lastTime.day = OriginTime.day;
lastTime.month = OriginTime.month;
lastTime.year = OriginTime.year;
lastTime.century = OriginTime.century;
while (updateInProgress) {
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x0A);
updateInProgress = (IoReadByteFromPort(0x71) & 0x80);
}
IoWriteByteOnPort(0x70, 0x0);
2019-05-14 11:48:07 +02:00
OriginTime.sec = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x02);
2019-05-14 11:48:07 +02:00
OriginTime.min = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x04);
2019-05-14 11:48:07 +02:00
OriginTime.hour = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x06);
2019-05-14 11:48:07 +02:00
OriginTime.weekday = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x07);
2019-05-14 11:48:07 +02:00
OriginTime.day = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x08);
2019-05-14 11:48:07 +02:00
OriginTime.month = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x09);
2019-05-14 11:48:07 +02:00
OriginTime.year = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
2019-05-14 11:48:07 +02:00
IoWriteByteOnPort(0x70, 0x32);
OriginTime.century = IoReadByteFromPort(0x71);
} while ((lastTime.sec != OriginTime.sec)
|| (lastTime.min != OriginTime.min)
|| (lastTime.hour != OriginTime.hour)
|| (lastTime.weekday != OriginTime.weekday)
|| (lastTime.day != OriginTime.day)
|| (lastTime.month != OriginTime.month)
|| (lastTime.year != OriginTime.year)
|| (lastTime.century != OriginTime.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);
2019-05-14 11:48:07 +02:00
2019-04-25 12:23:45 +02:00
// Convert to binary if it is necessary
if (!(time24or12Mode & 0x04)) {
2019-05-14 11:48:07 +02:00
OriginTime.sec = (OriginTime.sec & 0x0F)
+ ((OriginTime.sec / 16) * 10);
OriginTime.min = (OriginTime.min & 0x0F)
+ ((OriginTime.min / 16) * 10);
OriginTime.hour = ( (OriginTime.hour & 0x0F)
+ (((OriginTime.hour & 0x70) / 16) * 10) )
| (OriginTime.hour & 0x80);
OriginTime.day = (OriginTime.day & 0x0F)
+ ((OriginTime.day / 16) * 10);
OriginTime.month = (OriginTime.month & 0x0F)
+ ((OriginTime.month / 16) * 10);
OriginTime.year = (OriginTime.year & 0x0F)
+ ((OriginTime.year / 16) * 10);
OriginTime.century = (OriginTime.century & 0x0F)
+ ((OriginTime.century / 16) * 10);
OriginTime.weekday = (OriginTime.weekday & 0x0F)
+ ((OriginTime.weekday / 16) * 10);
2019-04-25 12:23:45 +02:00
}
// Convert 12 to 24 hour if necessary
2019-05-14 11:48:07 +02:00
if (!(time24or12Mode & 0x02) && (OriginTime.hour & 0x80)) {
OriginTime.hour = ((OriginTime.hour & 0x7)+ 10) % 24;
2019-04-25 12:23:45 +02:00
}
2019-04-25 16:31:06 +02:00
2019-11-14 10:19:01 +01:00
KeSetCurTime(OriginTime);
2019-04-25 12:23:45 +02:00
}
2019-11-11 23:57:31 +01:00
//
// ISR handler for the real time clock
//
2019-05-14 11:48:07 +02:00
static void HandleRTC(ISRFrame_t *regs)
2019-04-24 21:08:44 +02:00
{
IoWriteByteOnPort(0x70, 0x0C); // Selects status reg C
IoReadByteFromPort(0x71); // Flush
2019-05-14 11:48:07 +02:00
Ticks++;
KeSendEOItoPIC(0x28);
2019-04-24 21:08:44 +02:00
}
2019-04-25 12:23:45 +02:00
2019-11-14 10:19:01 +01:00
ulong KeGetClockTicks(void)
2019-05-13 20:34:41 +02:00
{
2019-11-14 10:19:01 +01:00
return Ticks;
2019-05-13 20:34:41 +02:00
}
2019-11-14 10:19:01 +01:00
Time_t* KeGetOriginTime(void)
2019-04-25 13:08:54 +02:00
{
2019-11-14 10:19:01 +01:00
return &OriginTime;
2019-04-25 13:08:54 +02:00
}
2019-05-14 11:48:07 +02:00
void KeEnableRTC(void)
2019-04-27 00:04:27 +02:00
{
ulong flags = KePauseIRQs();
2019-05-05 18:55:00 +02:00
char readInterruptConfig;
char readRegister;
2019-04-27 00:04:27 +02:00
2019-05-14 11:48:07 +02:00
KeRegisterISR(HandleRTC, 0x28);
2019-04-27 00:04:27 +02:00
// Setting up the register control and interrupt rates
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-14 11:48:07 +02:00
IoWriteByteOnPort(0x71, (readInterruptConfig & 0xF0) | RTC_RATE);
2019-04-27 00:04:27 +02:00
IoWriteByteOnPort(0x70, 0x0C);
IoReadByteFromPort(0x71); // Flush
2019-11-18 00:15:39 +01:00
// Setting up the IRQ line
2019-11-14 00:54:34 +01:00
KeUnmaskIRQ(8);
2019-04-27 00:04:27 +02:00
// Clean-up
IoWriteByteOnPort(0x70, 0x0C); // Select status reg C
IoReadByteFromPort(0x71); // Flush
2019-05-14 11:48:07 +02:00
GetTimeFromRTC();
2019-04-27 00:04:27 +02:00
KeRestoreIRQs(flags);
2019-05-14 11:48:07 +02:00
KeEnableNMI();
2019-11-14 00:54:34 +01:00
srand(KeGetTimeStamp()); // Initializes the kernel number generator
2019-11-18 00:15:39 +01:00
DebugLog("\tRTC interrupt frequency set to %d Hz\n",
32768 >> (RTC_RATE - 1));
2019-04-27 00:04:27 +02:00
}
2019-05-14 11:48:07 +02:00
void KeDelayExecution(uint time)
2019-05-07 17:02:25 +02:00
{
2019-05-14 11:48:07 +02:00
ulong frequency = 32768 >> (RTC_RATE - 1);
ulong beginTick = KeGetClockTicks();
while (KeGetClockTicks() < beginTick + (frequency/1000) * time) {
KeRelaxCPU();
2019-05-07 17:02:25 +02:00
}
}