From 8d77151a4a28553826a703835244d81e6d48d3c5 Mon Sep 17 00:00:00 2001
From: Michael Seydl <michael.seydl@hlw-software.com>
Date: Wed, 25 Apr 2012 11:55:48 +0200
Subject: [PATCH 2/2] - tidy up of nvidia scanning

---
 i386/libsaio/nvidia.c |   93 ++++++++++++++++---------------------------------
 1 file changed, 30 insertions(+), 63 deletions(-)

diff --git a/i386/libsaio/nvidia.c b/i386/libsaio/nvidia.c
index fe1f7a8..21cd894 100644
--- a/i386/libsaio/nvidia.c
+++ b/i386/libsaio/nvidia.c
@@ -919,44 +919,12 @@ static struct nv_chipsets_t NVKnownChipsets[] = {
 	// 12F0 - 12FF
 };
 
-static uint16_t swap16(uint16_t x)
-{
-	return (((x & 0x00FF) << 8) | ((x & 0xFF00) >> 8));
-}
-
-static uint16_t read16(uint8_t *ptr, uint16_t offset)
-{
-	uint8_t ret[2];
-	
-	ret[0] = ptr[offset+1];
-	ret[1] = ptr[offset];
-	
-	return *((uint16_t*)&ret);
-}
 
-#if 0
-static uint32_t swap32(uint32_t x)
-{
-	return ((x & 0x000000FF) << 24) | ((x & 0x0000FF00) << 8 ) | ((x & 0x00FF0000) >> 8 ) | ((x & 0xFF000000) >> 24);
-}
-
-static uint8_t	read8(uint8_t *ptr, uint16_t offset)
-{ 
-	return ptr[offset];
-}
-
-static uint32_t read32(uint8_t *ptr, uint16_t offset)
-{
-	uint8_t ret[4];
-	
-	ret[0] = ptr[offset+3];
-	ret[1] = ptr[offset+2];
-	ret[2] = ptr[offset+1];
-	ret[3] = ptr[offset];
-	
-	return *((uint32_t*)&ret);
-}
-#endif
+#define READ_BYTE(rom, offset) (*(u_char *)(rom + offset))
+#define READ_LE_SHORT(rom, offset) (READ_BYTE(rom, offset+1) << 8 | READ_BYTE(rom, offset))
+#define READ_LE_INT(rom, offset)   (READ_LE_SHORT(rom, offset+2) << 16 | READ_LE_SHORT(rom, offset))
+#define WRITE_LE_SHORT(data)       (((data) << 8 & 0xff00) | ((data) >> 8 & 0x00ff ))
+#define WRITE_LE_INT(data)         (WRITE_LE_SHORT(data) << 16 | WRITE_LE_SHORT(data >> 16))
 
 static int patch_nvidia_rom(uint8_t *rom)
 {
@@ -965,14 +933,14 @@ static int patch_nvidia_rom(uint8_t *rom)
 		return PATCH_ROM_FAILED;
 	}
 	
-	uint16_t dcbptr = swap16(read16(rom, 0x36));
+	uint16_t dcbptr = READ_LE_SHORT(rom, 0x36);
 	
 	if (!dcbptr) {
 		printf("no dcb table found\n");
 		return PATCH_ROM_FAILED;
 	}
-//	else
-//		printf("dcb table at offset 0x%04x\n", dcbptr);
+	else
+		printf("dcb table at offset 0x%04x\n", dcbptr);
 	 
 	uint8_t *dcbtable		 = &rom[dcbptr];
 	uint8_t dcbtable_version = dcbtable[0];
@@ -990,11 +958,11 @@ static int patch_nvidia_rom(uint8_t *rom)
 			numentries	 = dcbtable[2];
 			recordlength = dcbtable[3];
 			
-			sig = *(uint32_t *)&dcbtable[6];
+			sig = READ_LE_INT(dcbtable, 6);
 		}
 		else
 		{
-			sig = *(uint32_t *)&dcbtable[4];
+			sig = READ_LE_INT(dcbtable, 4);
 			headerlength = 8;
 		}
 		
@@ -1038,7 +1006,7 @@ static int patch_nvidia_rom(uint8_t *rom)
 	for (i = 0; i < numentries; i++)
 	{
 		uint32_t connection;
-		connection = *(uint32_t *)&dcbtable[headerlength + recordlength * i];
+		connection = READ_LE_INT(dcbtable,headerlength + recordlength * i);
 		
 		/* Should we allow discontinuous DCBs? Certainly DCB I2C tables can be discontinuous */
 		if ((connection & 0x0000000f) == 0x0000000f) /* end of records */ 
@@ -1139,7 +1107,7 @@ static int patch_nvidia_rom(uint8_t *rom)
 			}
 		}
 	}
-	
+  	
 	// if we have left ungrouped outputs merge them to the empty channel
 	uint8_t *togroup;// = (channel1 ? (channel2 ? NULL : &channel2) : &channel1);
 	togroup = &channel2;
@@ -1355,10 +1323,10 @@ bool setup_nvidia_devprop(pci_dt_t *nvda_dev)
 	videoRam = mem_detect(regs, nvCardType, nvda_dev);
 	model = get_nvidia_model((nvda_dev->vendor_id << 16) | nvda_dev->device_id);
 	
-	verbose("nVidia %s %dMB NV%02x [%04x:%04x] :: %s\n",
+	verbose("nVidia %s %dMB NV%02x [%04x:%04x] :: %s device number: %d\n",
 			model, (uint32_t)(videoRam / 1024 / 1024),
 			(REG32(0) >> 20) & 0x1ff, nvda_dev->vendor_id, nvda_dev->device_id,
-			devicepath);
+			devicepath, devices_number);
 	
 	rom = malloc(NVIDIA_ROM_SIZE);
 	sprintf(nvFilename, "/Extra/%04x_%04x.rom", (uint16_t)nvda_dev->vendor_id,
@@ -1382,28 +1350,27 @@ bool setup_nvidia_devprop(pci_dt_t *nvda_dev)
 	}
 	else
 	{
-		// Otherwise read bios from card
+    	// Otherwise read bios from card
 		nvBiosOveride = 0;
 		
 		// TODO: we should really check for the signature before copying the rom, i think.
-		
-		// PRAMIN first
-		nvRom = (uint8_t*)&regs[NV_PRAMIN_OFFSET];
-		bcopy((uint32_t *)nvRom, rom, NVIDIA_ROM_SIZE);
-		
+        // PROM next
+        // Enable PROM access
+        (REG32(NV_PBUS_PCI_NV_20)) = NV_PBUS_PCI_NV_20_ROM_SHADOW_DISABLED;
+        
+        nvRom = (uint8_t*)&regs[NV_PROM_OFFSET];
+        bcopy((uint8_t *)nvRom, rom, NVIDIA_ROM_SIZE);
+        
+        // disable PROM access
+        (REG32(NV_PBUS_PCI_NV_20)) = NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED;
+	
 		// Valid Signature ?
 		if (rom[0] != 0x55 && rom[1] != 0xaa)
 		{
-			// PROM next
-			// Enable PROM access
-			(REG32(NV_PBUS_PCI_NV_20)) = NV_PBUS_PCI_NV_20_ROM_SHADOW_DISABLED;
-			
-			nvRom = (uint8_t*)&regs[NV_PROM_OFFSET];
-			bcopy((uint8_t *)nvRom, rom, NVIDIA_ROM_SIZE);
-			
-			// disable PROM access
-			(REG32(NV_PBUS_PCI_NV_20)) = NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED;
-			
+            // PRAMIN first
+            nvRom = (uint8_t*)&regs[NV_PRAMIN_OFFSET];
+            bcopy((uint32_t *)nvRom, rom, NVIDIA_ROM_SIZE);
+
 			// Valid Signature ?
 			if (rom[0] != 0x55 && rom[1] != 0xaa)
 			{
@@ -1436,7 +1403,7 @@ bool setup_nvidia_devprop(pci_dt_t *nvda_dev)
 		printf("ERROR: nVidia ROM Patching Failed!\n");
 		//return false;
 	}
-	
+    
 	rom_pci_header = (option_rom_pci_header_t*)(rom + *(uint16_t *)&rom[24]);
 	
 	// check for 'PCIR' sig
-- 
1.7.9.4

