From 3814785a54ee445240498a9189f28cf327b8ad8d Mon Sep 17 00:00:00 2001
From: aaron <aaron@6dbc8c32-bb84-406f-8558-d1cf31a0ab0c>
Date: Fri, 27 Oct 2017 02:43:42 +0000
Subject: [PATCH] Finished re-writting PCI driver, it is now fully functional.

git-svn-id: https://spexeah.com:8443/svn/Asuro@268 6dbc8c32-bb84-406f-8558-d1cf31a0ab0c
---
 src/driver/ATA.pas |  8 ++++++--
 src/driver/PCI.pas | 40 ++++++++++++++++++++++++++++++----------
 src/kernel.pas     |  6 +++---
 3 files changed, 39 insertions(+), 15 deletions(-)

diff --git a/src/driver/ATA.pas b/src/driver/ATA.pas
index 13e84f69..256ea077 100644
--- a/src/driver/ATA.pas
+++ b/src/driver/ATA.pas
@@ -16,7 +16,8 @@ uses
     drivertypes,
     console,
     terminal,
-    isr76;
+    isr76,
+    vmemorymanager;
 
 type 
 
@@ -73,12 +74,15 @@ procedure callback(data : void);
 
 implementation
 
-procedure init(_controller : TPCI_device);
+procedure init(_controller : TPCI_device); //alloc, pmem; map_page vmem;
 begin
     console.writestringln('ATA: INIT BEGIN.');
     isr76.hook(uint32(@callback));
 
     controller := _controller;
+
+    new_page_at_address(controller.address4);
+
     devices[0].primary := true;
     devices[0].Command_Register := controller.address4;
     devices[0].Status_Register := controller.address4 + 2;
diff --git a/src/driver/PCI.pas b/src/driver/PCI.pas
index cebe48c8..9e43d58f 100644
--- a/src/driver/PCI.pas
+++ b/src/driver/PCI.pas
@@ -62,7 +62,7 @@ var
     busses : array[0..256] of TPCI_Device_Bridge;
 
     device_count : uint16;
-    bus_count : uint8;
+    bus_count : uint8 = 1;
 
 procedure init();
 procedure scanBus(bus : uint8);
@@ -72,11 +72,26 @@ function getDeviceInfo(class_code : uint8; subclass_code : uint8) : TPCI_Device;
 implementation 
 
 procedure init();
+var
+    current_bus : uint8;
 begin
     console.writestringln('PCI: INIT BEGIN.');
+    console.writestringln('PCI: Scanning Bus: 0');
     scanBus(0);
+    scanbus(1);
+    scanbus(2);
 
     //while unscanned busses scan busses
+    current_bus := 1;
+    while true do begin
+        if current_bus < bus_count then begin
+            console.writestringln('PCI: Scanning Bus: ');
+            console.writeint(bus_count);
+            scanBus(current_bus);
+            current_bus := current_bus + 1;
+        end else break;
+    end;
+
     console.writestringln('PCI: INIT END.');
 end;
 
@@ -92,6 +107,7 @@ begin
             if devices[device_count - 1].header_type and $40 = 40 then begin
                 for ii := 1 to 8 do begin
                     loadDeviceConfig(bus, i, ii);
+                    psleep(1000);
                 end;
             end;
         end;
@@ -183,6 +199,9 @@ begin
     busT.interrupt_pin := getbyte(data, 2);
     busT.interrupt_line := getbyte(data, 3);
 
+    busses[bus_count] := busT;
+    bus_count := bus_count + 1;
+
 end;
 
 function loadDeviceConfig(bus : uint8; slot : uint8; func : uint8) : boolean;
@@ -207,22 +226,23 @@ begin
 
     requestConfig(bus, slot, func, 2);
     data := inl($CFC);
-    device.class_code := getbyte(data, 0);
-    device.subclass_class := getbyte(data, 1);
-    device.prog_if := getbyte(data, 2);
-    device.revision_id := getbyte(data, 3);
+    device.class_code := getbyte(data, 3);
+    device.subclass_class := getbyte(data, 2);
+    device.prog_if := getbyte(data, 1);
+    device.revision_id := getbyte(data, 0);
 
     requestConfig(bus, slot, func, 3);
     data := inl($CFC);
-    device.BIST := getbyte(data, 0);
-    device.header_type := getbyte(data, 1);
-    device.latency_timer := getbyte(data, 2);
-    device.cache_size := getbyte(data, 3);
+    device.BIST := getbyte(data, 3);
+    device.header_type := getbyte(data, 2);
+    device.latency_timer := getbyte(data, 1);
+    device.cache_size := getbyte(data, 0);
 
     if device.header_type and $7 = 0 then begin 
         loadDeviceConfig := true;
     end else begin
         loadBusConfig(bus, slot, func, device);
+        exit(false);
     end;
      //TODO implement other types?
 
@@ -268,7 +288,7 @@ begin
 
 
     console.writestring('PCI: Found Device: ');
-    console.writehex(slot);
+    console.writehex(device.header_type);
     console.writestring('  ');
     console.writehex(device.device_id);        
     console.writestring('  ');        
diff --git a/src/kernel.pas b/src/kernel.pas
index 086db9b8..c1bb5817 100644
--- a/src/kernel.pas
+++ b/src/kernel.pas
@@ -118,10 +118,10 @@ begin
      mouse.init();
      console.writestringln('DRIVERS: INIT END.');
 
-     temp := 8294;
-     ata.write(uint32(@temp), 0, 4);
+     //temp := 8294;
+     //ata.write(uint32(@temp), 10, 4);
      //temp := 1234;
-     //ata.read(uint32(@temp), 0, 4);
+     //ata.read(uint32(@temp), 10, 4);
      //console.writeintln(temp);
 
      console.writestringln('');