summaryrefslogtreecommitdiff
path: root/drivers
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2023-10-24 19:22:27 +0200
committerAnton Kling <anton@kling.gg>2023-10-30 21:49:48 +0100
commit730e065e0bba1394750b3172bb7e2f1c0fd42c73 (patch)
treea37d4745fccb530948eb795a3eca7200c24d63b9 /drivers
parentba1952bf4c08b7783854d501ba30815a2aa2574c (diff)
Kernel: Add basic PCI and very basic rtl8139 support
Diffstat (limited to 'drivers')
-rw-r--r--drivers/pci.c45
-rw-r--r--drivers/pci.h14
-rw-r--r--drivers/rtl8139.c50
-rw-r--r--drivers/rtl8139.h1
4 files changed, 110 insertions, 0 deletions
diff --git a/drivers/pci.c b/drivers/pci.c
new file mode 100644
index 0000000..a71cc9a
--- /dev/null
+++ b/drivers/pci.c
@@ -0,0 +1,45 @@
+#include <cpu/io.h>
+#include <drivers/pci.h>
+#include <stdio.h>
+
+#define CONFIG_ADDRESS 0xCF8
+#define CONFIG_DATA 0xCFC
+
+uint32_t pci_config_read32(const struct PCI_DEVICE *device, uint8_t func,
+ uint8_t offset) {
+ uint32_t address;
+ uint32_t lbus = (uint32_t)device->bus;
+ uint32_t lslot = (uint32_t)device->slot;
+ uint32_t lfunc = (uint32_t)func;
+
+ // Create configuration address as per Figure 1
+ address = (uint32_t)((lbus << 16) | (lslot << 11) | (lfunc << 8) |
+ (offset & 0xFC) | ((uint32_t)0x80000000));
+
+ // Write out the address
+ outl(CONFIG_ADDRESS, address);
+ return inl(CONFIG_DATA);
+}
+
+int pci_populate_device_struct(uint16_t vendor, uint16_t device,
+ struct PCI_DEVICE *pci_device) {
+ pci_device->vendor = vendor;
+ pci_device->device = device;
+
+ for (int bus = 0; bus < 256; bus++) {
+ for (int slot = 0; slot < 256; slot++) {
+ struct PCI_DEVICE tmp;
+ tmp.bus = bus;
+ tmp.slot = slot;
+ uint32_t device_vendor = pci_config_read32(&tmp, 0, 0);
+ if (vendor != (device_vendor & 0xFFFF))
+ continue;
+ if (device != (device_vendor >> 16))
+ continue;
+ pci_device->bus = bus;
+ pci_device->slot = slot;
+ return 1;
+ }
+ }
+ return 0;
+}
diff --git a/drivers/pci.h b/drivers/pci.h
new file mode 100644
index 0000000..6a8bc1c
--- /dev/null
+++ b/drivers/pci.h
@@ -0,0 +1,14 @@
+#include <stdint.h>
+
+struct PCI_DEVICE {
+ uint16_t vendor;
+ uint16_t device;
+ uint8_t bus;
+ uint8_t slot;
+};
+
+uint32_t pci_config_read32(const struct PCI_DEVICE *device, uint8_t func,
+ uint8_t offset);
+
+int pci_populate_device_struct(uint16_t vendor, uint16_t device,
+ struct PCI_DEVICE *pci_device);
diff --git a/drivers/rtl8139.c b/drivers/rtl8139.c
new file mode 100644
index 0000000..44212e0
--- /dev/null
+++ b/drivers/rtl8139.c
@@ -0,0 +1,50 @@
+#include <assert.h>
+#include <cpu/io.h>
+#include <drivers/pci.h>
+#include <drivers/rtl8139.h>
+#include <mmu.h>
+
+#define RBSTART 0x30
+#define CMD 0x37
+#define IMR 0x3C
+
+struct PCI_DEVICE rtl8139;
+uint8_t device_buffer[8192 + 16];
+
+void rtl8139_init(void) {
+ if (!pci_populate_device_struct(0x10EC, 0x8139, &rtl8139)) {
+ kprintf("RTL8139 not found :(\n");
+ return;
+ }
+ kprintf("RTL8139 found at bus: %x slot: %x\n", rtl8139.bus, rtl8139.slot);
+
+ uint8_t header_type = (pci_config_read32(&rtl8139, 0, 0xC) >> 16) & 0xFF;
+ assert(0 == header_type);
+
+ uint32_t base_address = pci_config_read32(&rtl8139, 0, 0x10);
+ uint8_t interrupt_line = pci_config_read32(&rtl8139, 0, 0x3C);
+ kprintf("interrupt_line: %x\n", interrupt_line);
+
+ // Turning on the device
+ outb(base_address + 0x52, 0x0);
+
+ // Reset the device and clear the RX and TX buffers
+ outb(base_address + CMD, 0x10);
+ for (; 0 != (inb(base_address + CMD) & 0x10);)
+ ;
+
+ // Setupt the recieve buffer
+ uint32_t rx_buffer = (uint32_t)virtual_to_physical(device_buffer, NULL);
+ outl(base_address + RBSTART, rx_buffer);
+
+ // Set IMR + ISR
+ outw(base_address + IMR, 0x0005); // Sets the TOK and ROK bits high
+
+ // Configure the recieve buffer
+ outl(base_address + 0x44,
+ 0xf | (1 << 7)); // (1 << 7) is the WRAP bit, 0xf is AB+AM+APM+AAP
+
+ // Enable recieve and transmitter
+ outb(base_address + 0x37, 0x0C); // Sets the RE and TE bits high
+ asm("sti");
+}
diff --git a/drivers/rtl8139.h b/drivers/rtl8139.h
new file mode 100644
index 0000000..1f3d84e
--- /dev/null
+++ b/drivers/rtl8139.h
@@ -0,0 +1 @@
+void rtl8139_init(void);