summaryrefslogtreecommitdiffhomepage
path: root/src/kernel/HALKit/AMD64/Network/Generic+Basic+RTL8139.cpp
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2026-02-19 08:14:48 +0100
committerAmlal El Mahrouss <amlal@nekernel.org>2026-02-19 08:14:48 +0100
commitf0acad6f3206079d804b2f59aace0dc32dbeb6dc (patch)
tree44116f2771ebf146ec016337ba07d0320575dae3 /src/kernel/HALKit/AMD64/Network/Generic+Basic+RTL8139.cpp
parent41117a33aa0dde66b8964b4bc0de0082fcd40667 (diff)
kernel: lots of tweaks and improvements, WIP: ASN, FileMgr support for OpenHeFS.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'src/kernel/HALKit/AMD64/Network/Generic+Basic+RTL8139.cpp')
-rw-r--r--src/kernel/HALKit/AMD64/Network/Generic+Basic+RTL8139.cpp127
1 files changed, 127 insertions, 0 deletions
diff --git a/src/kernel/HALKit/AMD64/Network/Generic+Basic+RTL8139.cpp b/src/kernel/HALKit/AMD64/Network/Generic+Basic+RTL8139.cpp
new file mode 100644
index 00000000..2f6cc6da
--- /dev/null
+++ b/src/kernel/HALKit/AMD64/Network/Generic+Basic+RTL8139.cpp
@@ -0,0 +1,127 @@
+// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org)
+// Licensed under the Apache License, Version 2.0 (see LICENSE file)
+// Official repository: https://github.com/nekernel-org/nekernel
+
+#include <DmaKit/DmaPool.h>
+#include <HALKit/AMD64/Processor.h>
+#include <modules/ACPI/ACPIFactoryInterface.h>
+
+using namespace Kernel;
+using namespace Kernel::HAL;
+
+STATIC UInt16 kRTLIOBase = 0xFFFF;
+
+STATIC BOOL kTXRXEnabled = NO;
+
+STATIC UInt32 kRXOffset = 0UL;
+STATIC constexpr CONST UInt32 kRXBufferSize = 8192 + 16 + 1500;
+
+STATIC UInt8* kRXUpperLayer = nullptr;
+STATIC UInt8* kRXBuffer = nullptr;
+
+/***********************************************************************************/
+///@brief RTL8139 Init routine.
+/***********************************************************************************/
+
+EXTERN_C BOOL rtl_init_nic_rtl8139(UInt16 io_base) {
+ if (kTXRXEnabled) return NO;
+
+ kRTLIOBase = io_base;
+
+ MUST_PASS(io_base != 0xFFFF);
+
+ kRXBuffer = reinterpret_cast<UInt8*>(rtl_dma_alloc(sizeof(UInt8) * kRXBufferSize, 0));
+
+ MUST_PASS(kRXBuffer);
+
+ /// Reset first.
+
+ rt_out8(io_base + 0x37, 0x10);
+
+ UInt16 timeout = 0U;
+
+ while (rt_in8(io_base + 0x37) & 0x10) {
+ ++timeout;
+ if (timeout > 0x1000) break;
+ }
+
+ if (timeout <= 0x1000) {
+ return NO;
+ }
+
+ rt_out32(io_base + 0x30, (UInt32) (UIntPtr) kRXBuffer);
+
+ rt_out8(io_base + 0x37, 0x0C);
+
+ rt_out32(io_base + 0x44, 0xF | (1 << 7));
+
+ rt_out16(io_base + 0x3C, 0x0005);
+
+ kTXRXEnabled = YES;
+
+ return YES;
+}
+
+/***********************************************************************************/
+/// @brief RTL8139 I/O interrupt handler.
+/// @param rsp stack pointer.
+/// @note This function is called when the device interrupts to retrieve network data.
+/***********************************************************************************/
+
+EXTERN_C Void rtl_rtl8139_interrupt_handler(UIntPtr rsp) {
+ if (kRTLIOBase == 0xFFFF || kRTLIOBase == 0) return;
+
+ NE_UNUSED(rsp);
+
+ UInt16 status = rt_in16(kRTLIOBase + 0x3E);
+ rt_out16(kRTLIOBase + 0x3E, status);
+
+ if (status & 0x01) {
+ // While we receive data.
+ while ((rt_in8(kRTLIOBase + 0x37) & 0x01) == 0) {
+ // We grab an offset from the RX buffer.
+ UInt32 offset = kRXOffset % kRXBufferSize;
+
+ // If the offset is too high, we reset it.
+ if (offset >= (kRXBufferSize - 16)) {
+ kRXOffset = 0UL;
+ offset = 0UL;
+ }
+
+ volatile UInt8* packet = kRXBuffer + offset + 4;
+ UInt16 len = *(UInt16*) (kRXBuffer + offset + 2);
+
+ kRXUpperLayer[(offset + 4)] = *packet;
+ kRXOffset += (len + 4);
+
+ rt_out16(kRTLIOBase + 0x38, (UInt16) (kRXOffset - 16));
+ }
+ }
+
+ if (!(status & 0x04)) {
+ err_global_get() = kErrorNoNetwork;
+ }
+}
+
+/***********************************************************************************/
+/// @brief RTL8139 get upper layer function
+/// @return the upper layer.
+/// @retval nullptr if no upper layer is set.
+/// @retval pointer to the upper layer if set.
+/***********************************************************************************/
+
+EXTERN_C UInt8* rtl_rtl8139_get_upper_layer() {
+ return kRXUpperLayer;
+}
+
+/***********************************************************************************/
+/// @brief RTL8139 set upper layer function
+/// @param layer the upper layer.
+/***********************************************************************************/
+
+EXTERN_C BOOL rtl_rtl8139_set_upper_layer(UInt8* layer) {
+ if (!layer) return NO;
+ kRXUpperLayer = layer;
+
+ return YES;
+}