Kernel/PCI - Updated PCI API to take the protocol field into account
authorJohn Hodge <[email protected]>
Thu, 1 Dec 2011 14:10:43 +0000 (22:10 +0800)
committerJohn Hodge <[email protected]>
Thu, 1 Dec 2011 14:10:43 +0000 (22:10 +0800)
Kernel/drv/pci.c
Kernel/heap.c
Kernel/include/drv_pci.h
Modules/Storage/ATA/io.c
Modules/USB/UHCI/uhci.c
Modules/USB/UHCI/uhci.h

index c176f47..7234296 100644 (file)
@@ -17,13 +17,8 @@ typedef struct sPCIDevice
 {\r
        Uint16  bus, slot, fcn;\r
        Uint16  vendor, device;\r
-       union {\r
-               struct {\r
-                       Uint8 class, subclass;\r
-               };\r
-               Uint16  oc;\r
-       };\r
-       Uint8   revision, progif;\r
+       Uint32  class;  // Class:Subclass:ProgIf\r
+       Uint8   revision;\r
        Uint32  ConfigCache[256/4];\r
        char    Name[8];\r
        tVFS_Node       Node;\r
@@ -143,7 +138,7 @@ int PCI_ScanBus(int BusID, int bFill)
                        if(!PCI_int_EnumDevice(BusID, dev, fcn, &devInfo))\r
                                continue;\r
                        \r
-                       if(devInfo.oc == PCI_OC_PCIBRIDGE)\r
+                       if(devInfo.class == PCI_OC_PCIBRIDGE)\r
                        {\r
                                #if LIST_DEVICES\r
                                if( !bFill )\r
@@ -158,8 +153,8 @@ int PCI_ScanBus(int BusID, int bFill)
                        {\r
                                #if LIST_DEVICES\r
                                if( !bFill )\r
-                                       Log_Log("PCI", "Device %i,%i:%i %04x => 0x%04x:0x%04x",\r
-                                               BusID, dev, fcn, devInfo.oc, devInfo.vendor, devInfo.device);\r
+                                       Log_Log("PCI", "Device %i,%i:%i %06x => 0x%04x:0x%04x",\r
+                                               BusID, dev, fcn, devInfo.class, devInfo.vendor, devInfo.device);\r
                                #endif\r
                        }\r
                        \r
@@ -288,7 +283,7 @@ tPCIDev PCI_GetDevice(Uint16 vendor, Uint16 device, int idx)
  * \param mask Mask for class comparison\r
  * \param prev ID of previous device (-1 for no previous)\r
  */\r
-tPCIDev PCI_GetDeviceByClass(Uint16 class, Uint16 mask, tPCIDev prev)\r
+tPCIDev PCI_GetDeviceByClass(Uint32 class, Uint32 mask, tPCIDev prev)\r
 {\r
         int    i;\r
        // Check if prev is negative (meaning get first)\r
@@ -297,30 +292,29 @@ tPCIDev PCI_GetDeviceByClass(Uint16 class, Uint16 mask, tPCIDev prev)
        \r
        for( ; i < giPCI_DeviceCount; i++ )\r
        {\r
-               if((gPCI_Devices[i].oc & mask) == class)\r
+               if((gPCI_Devices[i].class & mask) == class)\r
                        return i;\r
        }\r
        return -1;\r
 }\r
 \r
-int PCI_GetDeviceInfo(tPCIDev ID, Uint16 *Vendor, Uint16 *Device, Uint16 *Class)\r
+int PCI_GetDeviceInfo(tPCIDev ID, Uint16 *Vendor, Uint16 *Device, Uint32 *Class)\r
 {\r
        tPCIDevice      *dev = &gPCI_Devices[ID];\r
        if(ID < 0 || ID >= giPCI_DeviceCount)   return 1;\r
        \r
        if(Vendor)      *Vendor = dev->vendor;\r
        if(Device)      *Device = dev->device;\r
-       if(Class)       *Class = dev->oc;\r
+       if(Class)       *Class = dev->class;\r
        return 0;\r
 }\r
 \r
-int PCI_GetDeviceVersion(tPCIDev ID, Uint8 *Revision, Uint8 *ProgIF)\r
+int PCI_GetDeviceVersion(tPCIDev ID, Uint8 *Revision)\r
 {\r
        tPCIDevice      *dev = &gPCI_Devices[ID];\r
        if(ID < 0 || ID >= giPCI_DeviceCount)   return 1;\r
        \r
        if(Revision)    *Revision = dev->revision;\r
-       if(ProgIF)      *ProgIF = dev->progif;\r
        return 0;\r
 }\r
 \r
@@ -453,13 +447,13 @@ int PCI_int_EnumDevice(Uint16 bus, Uint16 slot, Uint16 fcn, tPCIDevice *info)
        info->vendor = vendor_dev & 0xFFFF;\r
        info->device = vendor_dev >> 16;\r
        tmp = info->ConfigCache[2];\r
-       info->revision = tmp & 0xFFFF;\r
-       info->oc = tmp >> 16;\r
+       info->revision = tmp & 0xFF;\r
+       info->class = tmp >> 8;\r
        \r
 //     #if LIST_DEVICES\r
 //     Log("BAR0 0x%08x BAR1 0x%08x BAR2 0x%08x", info->ConfigCache[4], info->ConfigCache[5], info->ConfigCache[6]);\r
 //     Log("BAR3 0x%08x BAR4 0x%08x BAR5 0x%08x", info->ConfigCache[7], info->ConfigCache[8], info->ConfigCache[9]);\r
-//     Log("Class: 0x%04x", info->oc);\r
+//     Log("Class: 0x%06x", info->class);\r
 //     #endif\r
        \r
        // Make node name\r
index 6d099b3..5593c68 100644 (file)
@@ -684,7 +684,10 @@ void Heap_Stats(void)
        else
                frag = 0;
        Log_Log("Heap", "%i.%02i%% Heap Fragmentation", frag/100, frag%100);
-       avgAlloc = (totalBytes-freeBytes)/(nBlocks-nFree);
+       if(nBlocks <= nFree)
+               avgAlloc = 0;
+       else
+               avgAlloc = (totalBytes-freeBytes)/(nBlocks-nFree);
        if(avgAlloc != 0)
                overhead = (sizeof(tHeapFoot)+sizeof(tHeapHead))*10000/avgAlloc;
        else
index 9697df0..33c4a3e 100644 (file)
@@ -29,8 +29,8 @@ enum ePCIClasses
 \r
 enum ePCIOverClasses\r
 {\r
-       PCI_OC_PCIBRIDGE = 0x0604,\r
-       PCI_OC_SCSI = 0x0100\r
+       PCI_OC_PCIBRIDGE = 0x060400,\r
+       PCI_OC_SCSI = 0x010000\r
 };\r
 \r
 typedef int    tPCIDev;\r
@@ -42,10 +42,13 @@ typedef int tPCIDev;
  */\r
 extern int     PCI_CountDevices(Uint16 VendorID, Uint16 DeviceID);\r
 extern tPCIDev PCI_GetDevice(Uint16 VendorID, Uint16 DeviceID, int index);\r
-extern tPCIDev PCI_GetDeviceByClass(Uint16 ClassCode, Uint16 Mask, tPCIDev prev);\r
+/**\r
+ * \param ClassCode (Class:SubClass:PI)\r
+ */\r
+extern tPCIDev PCI_GetDeviceByClass(Uint32 ClassCode, Uint32 Mask, tPCIDev prev);\r
 \r
-extern int     PCI_GetDeviceInfo(tPCIDev id, Uint16 *Vendor, Uint16 *Device, Uint16 *Class);\r
-extern int     PCI_GetDeviceVersion(tPCIDev id, Uint8 *Revision, Uint8 *ProgIF);\r
+extern int     PCI_GetDeviceInfo(tPCIDev id, Uint16 *Vendor, Uint16 *Device, Uint32 *Class);\r
+extern int     PCI_GetDeviceVersion(tPCIDev id, Uint8 *Revision);\r
 extern int     PCI_GetDeviceSubsys(tPCIDev id, Uint16 *SubsystemVendor, Uint16 *SubsystemID);\r
 \r
 extern Uint32  PCI_ConfigRead(tPCIDev id, int Offset, int Size);\r
index 6304cb4..b8a15d0 100644 (file)
@@ -117,7 +117,7 @@ int ATA_SetupIO(void)
        ENTER("");
 
        // Get IDE Controller's PCI Entry
-       ent = PCI_GetDeviceByClass(0x0101, 0xFFFF, -1);
+       ent = PCI_GetDeviceByClass(0x010100, 0xFFFF00, -1);
        LOG("ent = %i", ent);
        gATA_BusMasterBase = PCI_GetBAR(ent, 4);
        if( gATA_BusMasterBase == 0 ) {
index d6c0f7f..5ea3566 100644 (file)
@@ -30,6 +30,11 @@ void *UHCI_SendSetup(void *Ptr, int Fcn, int Endpt, int DataTgl, tUSBHostCb Cb,
  int   UHCI_Int_InitHost(tUHCI_Controller *Host);
 void   UHCI_CheckPortUpdate(void *Ptr);
 void   UHCI_InterruptHandler(int IRQ, void *Ptr);
+// 
+static void    _OutByte(tUHCI_Controller *Host, int Reg, Uint8 Value);
+static void    _OutWord(tUHCI_Controller *Host, int Reg, Uint16 Value);
+static void    _OutDWord(tUHCI_Controller *Host, int Reg, Uint32 Value);
+static Uint16  _InWord(tUHCI_Controller *Host, int Reg);
 
 // === GLOBALS ===
 MODULE_DEFINE(0, VERSION, USB_UHCI, UHCI_Initialise, NULL, "USB_Core", NULL);
@@ -56,22 +61,28 @@ int UHCI_Initialise(char **Arguments)
        ENTER("");
        
        // Enumerate PCI Bus, getting a maximum of `MAX_CONTROLLERS` devices
-       while( (id = PCI_GetDeviceByClass(0x0C03, 0xFFFF, id)) >= 0 && i < MAX_CONTROLLERS )
+       while( (id = PCI_GetDeviceByClass(0x0C0300, 0xFFFFFF, id)) >= 0 && i < MAX_CONTROLLERS )
        {
                tUHCI_Controller        *cinfo = &gUHCI_Controllers[i];
+               Uint32  base_addr;
                // NOTE: Check "protocol" from PCI?
                
                cinfo->PciId = id;
-               cinfo->IOBase = PCI_GetBAR(id, 4);
-               if( !(cinfo->IOBase & 1) ) {
-                       Log_Warning("UHCI", "MMIO is not supported");
-                       continue ;
+               base_addr = PCI_GetBAR(id, 4);
+               
+               if( base_addr & 1 )
+               {
+                       cinfo->IOBase = base_addr & ~1;
+                       cinfo->MemIOMap = NULL;
+               }
+               else
+               {
+                       cinfo->MemIOMap = (void*)MM_MapHWPages(base_addr, 1);
                }
-               cinfo->IOBase &= ~1;
                cinfo->IRQNum = PCI_GetIRQ(id);
                
                Log_Debug("UHCI", "Controller PCI #%i: IO Base = 0x%x, IRQ %i",
-                       id, cinfo->IOBase, cinfo->IRQNum);
+                       id, base_addr, cinfo->IRQNum);
                
                IRQ_AddHandler(cinfo->IRQNum, UHCI_InterruptHandler, cinfo);
        
@@ -90,6 +101,12 @@ int UHCI_Initialise(char **Arguments)
 
                i ++;
        }
+
+       if(i == 0) {
+               LEAVE('i', MODULE_ERR_NOTNEEDED);
+               return MODULE_ERR_NOTNEEDED;
+       }
+
        if(i == MAX_CONTROLLERS) {
                Log_Warning("UHCI", "Over "EXPAND_STR(MAX_CONTROLLERS)" UHCI controllers detected, ignoring rest");
        }
@@ -142,7 +159,7 @@ tUHCI_TD *UHCI_int_GetTDFromPhys(tPAddr PAddr)
 
 void UHCI_int_AppendTD(tUHCI_Controller *Cont, tUHCI_TD *TD)
 {
-        int    next_frame = (inw(Cont->IOBase + FRNUM) + 2) & (1024-1);
+        int    next_frame = (_InWord(Cont, FRNUM) + 2) & (1024-1);
        tUHCI_TD        *prev_td;
        Uint32  link;
 
@@ -269,10 +286,9 @@ int UHCI_Int_InitHost(tUHCI_Controller *Host)
 {
        ENTER("pHost", Host);
 
-       outw( Host->IOBase + USBCMD, 4 );       // GRESET
-       Time_Delay(10);
+       _OutWord( Host, USBCMD, 4 );    // GRESET
        // TODO: Wait for at least 10ms
-       outw( Host->IOBase + USBCMD, 0 );       // GRESET
+       _OutWord( Host, USBCMD, 0 );    // GRESET
        
        // Allocate Frame List
        // - 1 Page, 32-bit address
@@ -289,18 +305,18 @@ int UHCI_Int_InitHost(tUHCI_Controller *Host)
        //! \todo Properly fill frame list
        
        // Set frame length to 1 ms
-       outb( Host->IOBase + SOFMOD, 64 );
+       _OutByte( Host, SOFMOD, 64 );
        
        // Set Frame List
-       outd( Host->IOBase + FLBASEADD, Host->PhysFrameList );
-       outw( Host->IOBase + FRNUM, 0 );
+       _OutDWord( Host, FLBASEADD, Host->PhysFrameList );
+       _OutWord( Host, FRNUM, 0 );
        
        // Enable Interrupts
-       outw( Host->IOBase + USBINTR, 0x000F );
+       _OutWord( Host, USBINTR, 0x000F );
        PCI_ConfigWrite( Host->PciId, 0xC0, 2, 0x2000 );
 
        // Enable processing
-       outw( Host->IOBase + USBCMD, 0x0001 );
+       _OutWord( Host, USBCMD, 0x0001 );
 
        LEAVE('i', 0);
        return 0;
@@ -312,13 +328,16 @@ void UHCI_CheckPortUpdate(void *Ptr)
        // Enable ports
        for( int i = 0; i < 2; i ++ )
        {
-                int    port = Host->IOBase + PORTSC1 + i*2;
+                int    port = PORTSC1 + i*2;
+               Uint16  status;
+       
+               status = _InWord(Host, port);
                // Check for port change
-               if( !(inw(port) & 0x0002) )     continue;
-               outw(port, 0x0002);
+               if( !(status & 0x0002) )        continue;
+               _OutWord(Host, port, 0x0002);
                
                // Check if the port is connected
-               if( !(inw(port) & 1) )
+               if( !(status & 1) )
                {
                        // Tell the USB code it's gone.
                        USB_DeviceDisconnected(Host->RootHub, i);
@@ -329,13 +348,13 @@ void UHCI_CheckPortUpdate(void *Ptr)
                        LOG("Port %i has something", i);
                        // Reset port (set bit 9)
                        LOG("Reset");
-                       outw( port, 0x0200 );
+                       _OutWord(Host, port, 0x0200);
                        Time_Delay(50); // 50ms delay
-                       outw( port, inw(port) & ~0x0200 );
+                       _OutWord(Host, port, _InWord(Host, port) & ~0x0200);
                        // Enable port
                        LOG("Enable");
                        Time_Delay(50); // 50ms delay
-                       outw( port, inw(port) | 0x0004 );
+                       _OutWord(Host, port, _InWord(Host, port) | 0x0004);
                        // Tell USB there's a new device
                        USB_DeviceConnected(Host->RootHub, i);
                }
@@ -345,8 +364,8 @@ void UHCI_CheckPortUpdate(void *Ptr)
 void UHCI_InterruptHandler(int IRQ, void *Ptr)
 {
        tUHCI_Controller *Host = Ptr;
-        int    frame = ((int)inw(Host->IOBase + FRNUM) - 1) & 0x3FF;
-       Uint16  status = inw(Host->IOBase + USBSTS);
+        int    frame = (_InWord(Host, FRNUM) - 1) & 0x3FF;
+       Uint16  status = _InWord(Host, USBSTS);
 //     Log_Debug("UHCI", "UHIC Interrupt, status = 0x%x, frame = %i", status, frame);
        
        // Interrupt-on-completion
@@ -393,5 +412,38 @@ void UHCI_InterruptHandler(int IRQ, void *Ptr)
        }
 
        LOG("status = 0x%02x", status);
-       outw(Host->IOBase + USBSTS, status);
+       _OutWord(Host, USBSTS, status);
 }
+
+void _OutByte(tUHCI_Controller *Host, int Reg, Uint8 Value)
+{
+       if( Host->MemIOMap )
+               ((Uint8*)Host->MemIOMap)[Reg] = Value;
+       else
+               outb(Host->IOBase + Reg, Value);
+}
+
+void _OutWord(tUHCI_Controller *Host, int Reg, Uint16 Value)
+{
+       if( Host->MemIOMap )
+               Host->MemIOMap[Reg/2] = Value;
+       else
+               outw(Host->IOBase + Reg, Value);
+}
+
+void _OutDWord(tUHCI_Controller *Host, int Reg, Uint32 Value)
+{
+       if( Host->MemIOMap )
+               ((Uint32*)Host->MemIOMap)[Reg/4] = Value;
+       else
+               outd(Host->IOBase + Reg, Value);
+}
+
+Uint16 _InWord(tUHCI_Controller *Host, int Reg)
+{
+       if( Host->MemIOMap )
+               return Host->MemIOMap[Reg/2];
+       else
+               return inw(Host->IOBase + Reg);
+}
+
index ec74174..a93459a 100644 (file)
@@ -24,6 +24,11 @@ struct sUHCI_Controller
         */
        Uint16  IOBase;
        
+       /**
+        * \brief Memory Mapped-IO base address
+        */
+       Uint16  *MemIOMap;
+
        /**
         * \brief IRQ Number assigned to the device
         */

UCC git Repository :: git.ucc.asn.au