From 354cf87a1677b7c687ca58ca3191d57078d60d53 Mon Sep 17 00:00:00 2001 From: "Chloe M." Date: Sat, 27 Jun 2026 05:18:33 -0500 Subject: [PATCH] stos/amd64: isa: Add legacy i8254 driver Signed-off-by: Chloe M. --- paw/stos/arch/amd64/platform/i8254.c | 42 ++++++++++++++++++++++++++++ paw/stos/head/arch/amd64/i8254.h | 36 ++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 paw/stos/arch/amd64/platform/i8254.c create mode 100644 paw/stos/head/arch/amd64/i8254.h diff --git a/paw/stos/arch/amd64/platform/i8254.c b/paw/stos/arch/amd64/platform/i8254.c new file mode 100644 index 0000000..f8f9038 --- /dev/null +++ b/paw/stos/arch/amd64/platform/i8254.c @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2026, Chloe M. + * Provided under the BSD-3 clause. + * + * Description: i8254 timer driver + * Author: Chloe M. + */ + +#include +#include + +USHORT +MdPitGetCount(VOID) +{ + UCHAR Low, High; + + MdOutb(I8254_COMMAND, 0x00); + Low = MdInb(0x40); + High = MdInb(0x40); + return (High << 8) | Low; +} + +VOID +MdPitSetCount(USHORT Value) +{ + MdOutb(I8254_COMMAND, 0x34); + MdOutb(0x40, Value & 0xFF); + MdOutb(0x40, (Value >> 8) & 0xFF); +} + +VOID +MdPitSetFrequency(UQUAD Frequency) +{ + UQUAD Divisor; + + Divisor = I8254_DIVIDEND / Frequency; + if ((I8254_DIVIDEND % Frequency) > (Frequency / 2)) { + ++Divisor; + } + + MdPitSetCount(Divisor); +} diff --git a/paw/stos/head/arch/amd64/i8254.h b/paw/stos/head/arch/amd64/i8254.h new file mode 100644 index 0000000..2d05a23 --- /dev/null +++ b/paw/stos/head/arch/amd64/i8254.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2026, Chloe M. + * Provided under the BSD-3 clause. + * + * Description: i8254 timer driver + * Author: Chloe M. + */ + +#ifndef _MACHINE_I8254_H_ +#define _MACHINE_I8254_H_ 1 + +#include + +#define I8254_COMMAND 0x43 +#define I8254_CHANNEL_0 0x40 +#define I8254_CHANNEL_2 0x42 +#define I8254_DIVIDEND 1193182ULL + +/* + * Obtain the current counter value + */ +USHORT MdPitGetCount(VOID); + +/* + * Set a new counter value + */ +VOID MdPitSetCount(USHORT Value); + +/* + * Set the PIT counter to match a frequency in Hz + * + * @Frequency: Frequency in HZ to set + */ +VOID MdPitSetFrequency(UQUAD Frequency); + +#endif /* !_MACHINE_I8254_H_ */