patch-2.4.22 linux-2.4.22/drivers/video/sis/init301.c

Next file: linux-2.4.22/drivers/video/sis/init301.h
Previous file: linux-2.4.22/drivers/video/sis/init.h
Back to the patch index
Back to the overall index

diff -urN linux-2.4.21/drivers/video/sis/init301.c linux-2.4.22/drivers/video/sis/init301.c
@@ -1,27 +1,16 @@
 /* $XFree86: xc/programs/Xserver/hw/xfree86/drivers/sis/init301.c,v 1.3 2002/22/04 01:16:16 dawes Exp $ */
 /*
- * Mode switching code (CRT2 section) for SiS 300/540/630/730/315/550/650/740/330
- * (Universal module for Linux kernel framebuffer, XFree86 4.x)
+ * Mode switching code (CRT2 section) for SiS 300/540/630/730/315/550/650/740/330/660
+ * (Universal module for Linux kernel framebuffer and XFree86 4.x)
  *
  * Assembler-To-C translation
  * Copyright 2002, 2003 by Thomas Winischhofer <thomas@winischhofer.net>
- * Minor parts Copyright SiS, Inc.
+ * Formerly based on non-functional code-fragements by SiS, Inc.
  *
- * Based on BIOS
- *     1.10.07, 1.10a for 650/CH7019
- *     1.11.21a for 740/CH7019
- *     1.11.05 for 650/LVDS (w/o Chrontel)
- *     1.07.1b, 1.10.6s, 1.11.6w, 1.11.7w, 1.11.8r for 650/301(B/LV), 650/302LV
- *     2.04.50 (I) and 2.04.5c (II) for 630/301(B)
- *     2.02.3b, 2.03.02, 2.04.2c, 2.04.5c, 2.07a and 2.08.b3 for 630/LVDS/LVDS+CH7005
- *     2.04.5c, 2.04.6c for 730+LVDS+CH7005
- *     1.09b for 315/301(B)
- *     1.16.51 for 300+301LV (ECS A907)
- *     1.01.03 for 330 (Xabre 400)
+ * If distributed as part of the linux kernel, the contents of this file
+ * is entirely covered by the GPL.
  *
- * Known bugs:
- *   1024x768 panel, expanding (CR37=1): Mode 640x480 does not work on SOME panels
- *       therefore, we always do the scaling ourselves for now.
+ * Otherwise, the following terms apply:
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
  * documentation for any purpose is hereby granted without fee, provided that
@@ -73,8 +62,8 @@
 #define SiS_I2CDELAYSHORT  150
 
 BOOLEAN
-SiS_SetCRT2Group301(SiS_Private *SiS_Pr, USHORT BaseAddr,UCHAR *ROMAddr,USHORT ModeNo,
-                    PSIS_HW_DEVICE_INFO HwDeviceExtension)
+SiS_SetCRT2Group(SiS_Private *SiS_Pr, USHORT BaseAddr,UCHAR *ROMAddr,USHORT ModeNo,
+                 PSIS_HW_DEVICE_INFO HwDeviceExtension)
 {
    USHORT ModeIdIndex;
    USHORT RefreshRateTableIndex;
@@ -85,9 +74,9 @@
       SiS_SearchModeID(SiS_Pr,ROMAddr,&ModeNo,&ModeIdIndex);
    } else {
       ModeIdIndex = 0;
-   }      
+   }
 
-   /* TW: Used for shifting CR33 */
+   /* Used for shifting CR33 */
    SiS_Pr->SiS_SelectCRT2Rate = 4;
 
    SiS_UnLockCRT2(SiS_Pr, HwDeviceExtension, BaseAddr);
@@ -110,8 +99,6 @@
       return(TRUE);
    }
 
-   if(SiS_Pr->UseCustomMode) return(FALSE);
-   
    SiS_GetCRT2Data(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
                    HwDeviceExtension);
 
@@ -153,9 +140,9 @@
       	   SiS_SetGroup5(SiS_Pr,HwDeviceExtension, BaseAddr,ROMAddr,
 	                 ModeNo,ModeIdIndex);
 
-	   /* TW: For 301BDH (Panel link initialization): */
+	   /* For 301BDH (Panel link initialization): */
 	   if((SiS_Pr->SiS_VBType & VB_NoLCD) && (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD)) {
-	      if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480) {	 
+	      if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480) {
 		 if(!((SiS_Pr->SiS_SetFlag & SetDOSMode) && ((ModeNo == 0x03) || (ModeNo = 0x10)))) {
 		    if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
 		       SiS_ModCRT1CRTC(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,
@@ -176,10 +163,10 @@
 	                      RefreshRateTableIndex,HwDeviceExtension);
 	   }
 	}
-        if(SiS_Pr->SiS_IF_DEF_FSTN == 0) {
-     	   SiS_SetCRT2ECLK(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,
-	 	           RefreshRateTableIndex,HwDeviceExtension);
-	}
+
+        SiS_SetCRT2ECLK(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,
+	                RefreshRateTableIndex,HwDeviceExtension);
+
 	if(SiS_LowModeStuff(SiS_Pr,ModeNo,HwDeviceExtension)) {
      	   if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
 	      if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
@@ -199,74 +186,64 @@
    }
 
 #ifdef SIS300
-   if ( (HwDeviceExtension->jChipType == SIS_540) ||
-        (HwDeviceExtension->jChipType == SIS_630) ||
-        (HwDeviceExtension->jChipType == SIS_730) ||
-        (HwDeviceExtension->jChipType == SIS_300) )
-    {
-	if(SiS_LowModeStuff(SiS_Pr,ModeNo,HwDeviceExtension)) {
-	   if(SiS_Pr->SiS_UseOEM) {
-	      if((SiS_Pr->SiS_UseROM) && ROMAddr && (SiS_Pr->SiS_UseOEM == -1)) {
-	         if((ROMAddr[0x233] == 0x12) && (ROMAddr[0x234] == 0x34)) {
-	            SiS_OEM300Setting(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo);
-	         }
-	      } else {
-       	         SiS_OEM300Setting(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo);
-	      }
-	   }
-	}
-    }
-#endif
-
-#ifdef SIS315H
-   if ( (HwDeviceExtension->jChipType == SIS_315H)  ||
-        (HwDeviceExtension->jChipType == SIS_315)   ||
-	(HwDeviceExtension->jChipType == SIS_315PRO)||
-        (HwDeviceExtension->jChipType == SIS_550)   ||
-        (HwDeviceExtension->jChipType == SIS_740)   ||
-        (HwDeviceExtension->jChipType == SIS_650)   ||
-	(HwDeviceExtension->jChipType == SIS_330) )
-   {
-        if(SiS_LowModeStuff(SiS_Pr,ModeNo,HwDeviceExtension)) {
-	   SiS_FinalizeLCD(SiS_Pr,BaseAddr,ROMAddr,ModeNo,ModeIdIndex, HwDeviceExtension);
-#if 0      /* Instead of FinalizeLCD(), older BIOSes (A92x) used OEMLCD() */
-	   SiS_OEMLCD(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,ModeIdIndex);
-#endif
-           if(SiS_Pr->SiS_UseOEM) {
-              SiS_OEM310Setting(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,ModeIdIndex);
-           }
-           SiS_CRT2AutoThreshold(SiS_Pr,BaseAddr);
-        }
-   }
-#endif
-
    if(HwDeviceExtension->jChipType < SIS_315H) {
+      if(SiS_LowModeStuff(SiS_Pr,ModeNo,HwDeviceExtension)) {
+	 if(SiS_Pr->SiS_UseOEM) {
+	    if((SiS_Pr->SiS_UseROM) && ROMAddr && (SiS_Pr->SiS_UseOEM == -1)) {
+	       if((ROMAddr[0x233] == 0x12) && (ROMAddr[0x234] == 0x34)) {
+	          SiS_OEM300Setting(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,ModeIdIndex,
+	       			    RefreshRateTableIndex);
+	       }
+	    } else {
+       	       SiS_OEM300Setting(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,ModeIdIndex,
+	       			 RefreshRateTableIndex);
+	    }
+	 }
+	 if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
+            if((SiS_Pr->SiS_CustomT == CUT_BARCO1366) ||
+	       (SiS_Pr->SiS_CustomT == CUT_BARCO1024)) {
+	       SetOEMLCDData2(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,
+	                      ModeIdIndex,RefreshRateTableIndex);
+	    }
+            if(HwDeviceExtension->jChipType == SIS_730) {
+               SiS_DisplayOn(SiS_Pr);
+	    }
+         }
+      }
       if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
           if(HwDeviceExtension->jChipType != SIS_730) {
              SiS_DisplayOn(SiS_Pr);
 	  }
       }
    }
+#endif
 
-   if(SiS_LowModeStuff(SiS_Pr,ModeNo,HwDeviceExtension)) {
-      if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
-         if(HwDeviceExtension->jChipType == SIS_730) {
-            SiS_DisplayOn(SiS_Pr);
-	 }
+#ifdef SIS315H
+   if(HwDeviceExtension->jChipType >= SIS_315H) {
+      if(SiS_LowModeStuff(SiS_Pr,ModeNo,HwDeviceExtension)) {
+	 SiS_FinalizeLCD(SiS_Pr,BaseAddr,ROMAddr,ModeNo,ModeIdIndex, HwDeviceExtension);
+         if(SiS_Pr->SiS_UseOEM) {
+            SiS_OEM310Setting(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,ModeIdIndex);
+         }
+         SiS_CRT2AutoThreshold(SiS_Pr,BaseAddr);
       }
+   }
+#endif
+
+   if(SiS_LowModeStuff(SiS_Pr,ModeNo,HwDeviceExtension)) {
       SiS_EnableBridge(SiS_Pr,HwDeviceExtension,BaseAddr);
    }
 
    SiS_DisplayOn(SiS_Pr);
 
    if(SiS_Pr->SiS_IF_DEF_CH70xx == 1) {
-	if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
-	     /* TW: Disable LCD panel when using TV */
-	     SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x11,0x0C);
-	} else {
-	     /* TW: Disable TV when using LCD */
-	     SiS_SetCH70xxANDOR(SiS_Pr,0x010E,0xF8);
-	}
+      if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+	 /* Disable LCD panel when using TV */
+	 SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x11,0x0C);
+      } else {
+	 /* Disable TV when using LCD */
+	 SiS_SetCH70xxANDOR(SiS_Pr,0x010E,0xF8);
+      }
    }
 
    if(SiS_LowModeStuff(SiS_Pr,ModeNo,HwDeviceExtension)) {
@@ -283,7 +260,7 @@
     USHORT temp,temp1,temp2;
 
     if((ModeNo != 0x03) && (ModeNo != 0x10) && (ModeNo != 0x12))
-         return(1);
+       return(1);
     temp = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x11);
     SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x11,0x80);
     temp1 = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x00);
@@ -304,7 +281,7 @@
     }
 }
 
-/* TW: Set Part1 registers */
+/* Set Part1 registers */
 void
 SiS_SetGroup1(SiS_Private *SiS_Pr,USHORT BaseAddr,UCHAR *ROMAddr,USHORT ModeNo,
               USHORT ModeIdIndex,PSIS_HW_DEVICE_INFO HwDeviceExtension,
@@ -313,25 +290,29 @@
   USHORT  temp=0, tempax=0, tempbx=0, tempcx=0;
   USHORT  pushbx=0, CRT1Index=0;
 #ifdef SIS315H
-  USHORT  pushcx=0, tempbl=0;
+  USHORT  tempbl=0;
 #endif
   USHORT  modeflag, resinfo=0;
 
-  if(ModeNo<=0x13) {
-	modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
+  if(ModeNo <= 0x13) {
+     modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
   } else {
+     if(SiS_Pr->UseCustomMode) {
+	modeflag = SiS_Pr->CModeFlag;
+     } else {
     	CRT1Index = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
     	resinfo = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO;
 	modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
+     }
   }
 
   if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
-
-	   SiS_SetCRT2Sync(SiS_Pr,BaseAddr,ROMAddr,ModeNo,
-                           RefreshRateTableIndex,HwDeviceExtension);
 #ifdef SIS315H
-	   SiS_SetGroup1_LCDA(SiS_Pr,BaseAddr,ROMAddr,ModeNo,ModeIdIndex,
-     	                      HwDeviceExtension,RefreshRateTableIndex);
+     SiS_SetCRT2Sync(SiS_Pr,BaseAddr,ROMAddr,ModeNo,
+                     RefreshRateTableIndex,HwDeviceExtension);
+
+     SiS_SetGroup1_LCDA(SiS_Pr,BaseAddr,ROMAddr,ModeNo,ModeIdIndex,
+                        HwDeviceExtension,RefreshRateTableIndex);
 #endif
   } else {
 
@@ -367,13 +348,13 @@
 #ifdef SIS300   /* ------------- 300 series --------------*/
 
     		temp = (SiS_Pr->SiS_VGAHT - 1) & 0x0FF;   			/* BTVGA2HT 0x08,0x09 */
-    		SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x08,temp);                   /* TW: CRT2 Horizontal Total */
+    		SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x08,temp);                   /* CRT2 Horizontal Total */
 
     		temp = (((SiS_Pr->SiS_VGAHT - 1) & 0xFF00) >> 8) << 4;
-    		SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x09,0x0f,temp);          /* TW: CRT2 Horizontal Total Overflow [7:4] */
+    		SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x09,0x0f,temp);          /* CRT2 Horizontal Total Overflow [7:4] */
 
     		temp = (SiS_Pr->SiS_VGAHDE + 12) & 0x0FF;                       /* BTVGA2HDEE 0x0A,0x0C */
-    		SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0A,temp);                   /* TW: CRT2 Horizontal Display Enable End */
+    		SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0A,temp);                   /* CRT2 Horizontal Display Enable End */
 
     		pushbx = SiS_Pr->SiS_VGAHDE + 12;                               /* bx  BTVGA@HRS 0x0B,0x0C */
     		tempcx = (SiS_Pr->SiS_VGAHT - SiS_Pr->SiS_VGAHDE) >> 2;
@@ -382,108 +363,129 @@
     		tempcx += tempbx;
 
     		if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
-      			if(SiS_Pr->SiS_VBInfo & SetCRT2ToRAMDAC){
-			        /* CRT1Index &= 0x3F; - Not any longer */
-        			tempbx = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[4];
-        			tempbx |= ((SiS_Pr->SiS_CRT1Table[CRT1Index].CR[14] & 0xC0) << 2);
-        			tempbx = (tempbx - 1) << 3;
-        			tempcx = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[5];
-        			tempcx &= 0x1F;
-        			temp = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[15];
-        			temp = (temp & 0x04) << (6-2);
-        			tempcx = (tempcx | temp);
-				tempcx--;
-				tempcx <<= 3;
-      			}
-
-    			if((SiS_Pr->SiS_VBInfo & SetCRT2ToTV) && (resinfo == 0x08)){
-        			if(!(SiS_Pr->SiS_VBInfo & SetPALTV)){
-      					tempbx = 1040;
-      					tempcx = 1042;
-      				}
-    			}
+
+		   if(SiS_Pr->UseCustomMode) {
+		      tempbx = SiS_Pr->CHSyncStart + 12;
+		      tempcx = SiS_Pr->CHSyncEnd + 12;
+		   }
+
+      		   if(SiS_Pr->SiS_VBInfo & SetCRT2ToRAMDAC) {
+		      unsigned char cr4, cr14, cr5, cr15;
+		      if(SiS_Pr->UseCustomMode) {
+		         cr4  = SiS_Pr->CCRT1CRTC[4];
+			 cr14 = SiS_Pr->CCRT1CRTC[14];
+			 cr5  = SiS_Pr->CCRT1CRTC[5];
+			 cr15 = SiS_Pr->CCRT1CRTC[15];
+		      } else {
+		         cr4  = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[4];
+			 cr14 = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[14];
+			 cr5  = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[5];
+			 cr15 = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[15];
+		      }
+        	      tempbx = ((cr4 | ((cr14 & 0xC0) << 2)) - 1) << 3;
+        	      tempcx = (((cr5 & 0x1F) | ((cr15 & 0x04) << (6-2))) - 1) << 3;
+      		   }
+
+    		   if((SiS_Pr->SiS_VBInfo & SetCRT2ToTV) && (resinfo == SIS_RI_1024x768)){
+        	      if(!(SiS_Pr->SiS_VBInfo & SetPALTV)){
+      			 tempbx = 1040;
+      			 tempcx = 1042;
+      		      }
+    		   }
 	        }
 
     		temp = tempbx & 0x00FF;
-    		SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0B,temp);                   /* TW: CRT2 Horizontal Retrace Start */
+    		SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0B,temp);                   /* CRT2 Horizontal Retrace Start */
 #endif /* SIS300 */
 
  	} else {
 
-#ifdef SIS315H  /* ----------------- 310/325/330 series ------------- */
+#ifdef SIS315H  /* ------------------- 315/330 series --------------- */
 
 	        tempcx = SiS_Pr->SiS_VGAHT;				       /* BTVGA2HT 0x08,0x09 */
-		pushcx = tempcx;
 		if(modeflag & HalfDCLK) {
-#ifndef NEWCH701x		
-		    if((SiS_Pr->SiS_IF_DEF_LVDS == 1) && (SiS_Pr->SiS_IF_DEF_CH70xx == 0)) {
-#endif		    
+		    if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
 		          tempax = SiS_Pr->SiS_VGAHDE >> 1;
 			  tempcx = SiS_Pr->SiS_HT - SiS_Pr->SiS_HDE + tempax;
 			  if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
 			      tempcx = SiS_Pr->SiS_HT - tempax;
 			  }
-#ifndef NEWCH701x					  
 		    } else {
 			  tempcx >>= 1;
 		    }
-#endif		    
 		}
 		tempcx--;
 
 		temp = tempcx & 0xff;
-		SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x08,temp);                  /* TW: CRT2 Horizontal Total */
+		SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x08,temp);                  /* CRT2 Horizontal Total */
 
 		temp = ((tempcx & 0xff00) >> 8) << 4;
-		SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x09,0x0F,temp);         /* TW: CRT2 Horizontal Total Overflow [7:4] */
+		SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x09,0x0F,temp);         /* CRT2 Horizontal Total Overflow [7:4] */
 
-		tempcx = pushcx;					       /* BTVGA2HDEE 0x0A,0x0C */
+		tempcx = SiS_Pr->SiS_VGAHT;				       /* BTVGA2HDEE 0x0A,0x0C */
 		tempbx = SiS_Pr->SiS_VGAHDE;
 		tempcx -= tempbx;
 		tempcx >>= 2;
 		if(modeflag & HalfDCLK) {
-		    tempbx >>= 1;
-		    tempcx >>= 1;
+		   tempbx >>= 1;
+		   tempcx >>= 1;
 		}
 		tempbx += 16;
 
 		temp = tempbx & 0xff;
-		SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0A,temp);                  /* TW: CRT2 Horizontal Display Enable End */
+		SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0A,temp);                  /* CRT2 Horizontal Display Enable End */
 
 		pushbx = tempbx;
 		tempcx >>= 1;
 		tempbx += tempcx;
 		tempcx += tempbx;
 
-		if(SiS_Pr->SiS_IF_DEF_LVDS==0) {
+		if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
+
+		   if(SiS_Pr->UseCustomMode) {
+		      tempbx = SiS_Pr->CHSyncStart + 16;
+		      tempcx = SiS_Pr->CHSyncEnd + 16;
+		      tempax = SiS_Pr->SiS_VGAHT;
+		      if(modeflag & HalfDCLK) tempax >>= 1;
+		      tempax--;
+		      if(tempcx > tempax)  tempcx = tempax;
+		   }
+
              	   if(SiS_Pr->SiS_VBInfo & SetCRT2ToRAMDAC) {
-                	tempbx = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[4];
-                	tempbx |= ((SiS_Pr->SiS_CRT1Table[CRT1Index].CR[14] & 0xC0) << 2);
-                	tempbx = (tempbx - 3) << 3;         		/*(VGAHRS-3)*8 */
-                	tempcx = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[5];
-               		tempcx &= 0x1F;
-                	temp = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[15];
-                	temp = (temp & 0x04) << (5-2);      		/* VGAHRE D[5] */
-                	tempcx = (tempcx | temp);	  	  	/* (VGAHRE-3)*8 */
-			tempcx -= 3;
-			tempcx <<= 3;
-			tempcx &= 0x00FF;
-			tempcx |= (tempbx & 0xFF00);
-                	tempbx += 16;
-                	tempcx += 16;
-			tempax = SiS_Pr->SiS_VGAHT;
-			if(modeflag & HalfDCLK)  tempax >>= 1;
-			tempax--;
-			if(tempcx > tempax)  tempcx = tempax;
+		      unsigned char cr4, cr14, cr5, cr15;
+		      if(SiS_Pr->UseCustomMode) {
+		         cr4  = SiS_Pr->CCRT1CRTC[4];
+			 cr14 = SiS_Pr->CCRT1CRTC[14];
+			 cr5  = SiS_Pr->CCRT1CRTC[5];
+			 cr15 = SiS_Pr->CCRT1CRTC[15];
+		      } else {
+		         cr4  = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[4];
+			 cr14 = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[14];
+			 cr5  = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[5];
+			 cr15 = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[15];
+		      }
+                      tempbx = ((cr4 | ((cr14 & 0xC0) << 2)) - 3) << 3; 		/* (VGAHRS-3)*8 */
+                      tempcx = (((cr5 & 0x1f) | ((cr15 & 0x04) << (5-2))) - 3) << 3; 	/* (VGAHRE-3)*8 */
+		      tempcx &= 0x00FF;
+		      tempcx |= (tempbx & 0xFF00);
+                      tempbx += 16;
+                      tempcx += 16;
+		      tempax = SiS_Pr->SiS_VGAHT;
+		      if(modeflag & HalfDCLK) tempax >>= 1;
+		      tempax--;
+		      if(tempcx > tempax)  tempcx = tempax;
              	   }
-         	   if((SiS_Pr->SiS_VBInfo & SetCRT2ToTV) && (resinfo == 0x08)){
-             	      if(!(SiS_Pr->SiS_VBInfo & SetPALTV)){
-      		 	 tempbx = 1040;
-      		 	 tempcx = 1042;
-      	     	      }
+         	   if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+		      if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) {
+		         if(resinfo == SIS_RI_1024x768) {
+      		 	    tempbx = 1040;
+      		 	    tempcx = 1042;
+      	     	         }
+		      }
          	   }
-		   /* TW: Makes no sense, but is in 650/302LV 1.10.6s */
-         	   if((SiS_Pr->SiS_VBInfo & SetCRT2ToTV) && (resinfo == 0x08)){
+#if 0
+		   /* Makes no sense, but is in 650/30xLV 1.10.6s */
+         	   if((SiS_Pr->SiS_VBInfo & SetCRT2ToTV) && (resinfo == SIS_RI_1024x768)){
 		      if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV)) {
              	         if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) {
       		 	    tempbx = 1040;
@@ -491,25 +493,26 @@
       	     	         }
 		      }
          	   }
+#endif
                 }
 
 		temp = tempbx & 0xff;
-	 	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0B,temp);                 /* TW: CRT2 Horizontal Retrace Start */
+	 	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0B,temp);                 /* CRT2 Horizontal Retrace Start */
 #endif  /* SIS315H */
 
-     	}  /* 310/325/330 series */
+     	}  /* 315/330 series */
 
-  	/* TW: The following is done for all bridge/chip types/series */
+  	/* The following is done for all bridge/chip types/series */
 
   	tempax = tempbx & 0xFF00;
   	tempbx = pushbx;
   	tempbx = (tempbx & 0x00FF) | ((tempbx & 0xFF00) << 4);
   	tempax |= (tempbx & 0xFF00);
   	temp = (tempax & 0xFF00) >> 8;
-  	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0C,temp);                        /* TW: Overflow */
+  	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0C,temp);                        /* Overflow */
 
   	temp = tempcx & 0x00FF;
-  	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0D,temp);                        /* TW: CRT2 Horizontal Retrace End */
+  	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0D,temp);                        /* CRT2 Horizontal Retrace End */
 
   	/* 2. Vertical setup */
 
@@ -517,30 +520,30 @@
   	temp = tempcx & 0x00FF;
 
         if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
-	     if(HwDeviceExtension->jChipType < SIS_315H) {
-	          if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
-		       if(SiS_Pr->SiS_VBInfo & (SetCRT2ToSVIDEO | SetCRT2ToAVIDEO)) {
-		           temp--;
-		       }
-                  }
-	     } else {
- 		      temp--;
-             }
+	   if(HwDeviceExtension->jChipType < SIS_315H) {
+	      if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
+	         if(SiS_Pr->SiS_VBInfo & (SetCRT2ToSVIDEO | SetCRT2ToAVIDEO)) {
+	            temp--;
+	         }
+              }
+	   } else {
+ 	      temp--;
+           }
         } else if(HwDeviceExtension->jChipType >= SIS_315H) {
-	    /* TW: 650/30xLV 1.10.6s */
-	    temp--;
+	   /* 650/30xLV 1.10.6s */
+	   temp--;
 	}
-  	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0E,temp);                        /* TW: CRT2 Vertical Total */
+  	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0E,temp);                        /* CRT2 Vertical Total */
 
   	tempbx = SiS_Pr->SiS_VGAVDE - 1;
   	temp = tempbx & 0x00FF;
-  	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0F,temp);                        /* TW: CRT2 Vertical Display Enable End */
+  	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0F,temp);                        /* CRT2 Vertical Display Enable End */
 
   	temp = ((tempbx & 0xFF00) << 3) >> 8;
   	temp |= ((tempcx & 0xFF00) >> 8);
-  	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x12,temp);                        /* TW: Overflow (and HWCursor Test Mode) */
+  	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x12,temp);                        /* Overflow (and HWCursor Test Mode) */
 
-	/* TW: 650/LVDS (1.10.07), 650/30xLV (1.10.6s) */
+	/* 650/LVDS (1.10.07), 650/30xLV (1.10.6s) */
 	if(HwDeviceExtension->jChipType >= SIS_315H) {
            tempbx++;
    	   tempax = tempbx;
@@ -553,29 +556,44 @@
 	   tempcx += tempbx;
 	   tempcx++;
 	} else {
-	   /* TW: 300 series, LVDS/301B: */
+	   /* 300 series, LVDS/301B: */
   	   tempbx = (SiS_Pr->SiS_VGAVT + SiS_Pr->SiS_VGAVDE) >> 1;                 /*  BTVGA2VRS     0x10,0x11   */
   	   tempcx = ((SiS_Pr->SiS_VGAVT - SiS_Pr->SiS_VGAVDE) >> 4) + tempbx + 1;  /*  BTVGA2VRE     0x11        */
 	}
 
   	if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
-    	   if(SiS_Pr->SiS_VBInfo & SetCRT2ToRAMDAC){
-      		tempbx = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[8];
-      		temp = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[7];
-      		if(temp & 0x04) tempbx |= 0x0100;
-      		if(temp & 0x80) tempbx |= 0x0200;
-      		temp = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[13];
-      		if(temp & 0x08) tempbx |= 0x0400;
-      		temp = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[9];
-      		tempcx = (tempcx & 0xFF00) | (temp & 0x00FF);
+
+	   if(SiS_Pr->UseCustomMode) {
+	      tempbx = SiS_Pr->CVSyncStart;
+	      tempcx = (tempcx & 0xFF00) | (SiS_Pr->CVSyncEnd & 0x00FF);
+	   }
+
+    	   if(SiS_Pr->SiS_VBInfo & SetCRT2ToRAMDAC) {
+	      unsigned char cr8, cr7, cr13, cr9;
+	      if(SiS_Pr->UseCustomMode) {
+	         cr8  = SiS_Pr->CCRT1CRTC[8];
+		 cr7  = SiS_Pr->CCRT1CRTC[7];
+		 cr13 = SiS_Pr->CCRT1CRTC[13];
+		 cr9  = SiS_Pr->CCRT1CRTC[9];
+	      } else {
+	         cr8  = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[8];
+		 cr7  = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[7];
+		 cr13 = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[13];
+		 cr9  = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[9];
+	      }
+      	      tempbx = cr8;
+      	      if(cr7 & 0x04)  tempbx |= 0x0100;
+      	      if(cr7 & 0x80)  tempbx |= 0x0200;
+      	      if(cr13 & 0x08) tempbx |= 0x0400;
+      	      tempcx = (tempcx & 0xFF00) | (cr9 & 0x00FF);
     	   }
   	}
   	temp = tempbx & 0x00FF;
-  	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x10,temp);           /* TW: CRT2 Vertical Retrace Start */
+  	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x10,temp);           /* CRT2 Vertical Retrace Start */
 
   	temp = ((tempbx & 0xFF00) >> 8) << 4;
   	temp |= (tempcx & 0x000F);
-  	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x11,temp);           /* TW: CRT2 Vert. Retrace End; Overflow; "Enable CRTC Check" */
+  	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x11,temp);           /* CRT2 Vert. Retrace End; Overflow; "Enable CRTC Check" */
 
   	/* 3. Panel compensation delay */
 
@@ -590,18 +608,18 @@
 		   temp = 0x10;
 		   if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768)  temp = 0x2c;
 		   if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) temp = 0x20;
-		   if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x960)  temp = 0x24;
 		}
 		if(SiS_Pr->SiS_VBType & VB_SIS301) {
 		   if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) temp = 0x20;
 		}
 		if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x960)     temp = 0x24;
+		if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_PanelCustom)       temp = 0x2c;
 		if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) 		temp = 0x08;
 		if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) {
       		   if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) 	temp = 0x2c;
       		   else 					temp = 0x20;
     	        }
-		if((ROMAddr) && (SiS_Pr->SiS_UseROM) && (SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV)) {
+		if((ROMAddr) && (SiS_Pr->SiS_UseROM)) {
 		    if(ROMAddr[0x220] & 0x80) {
 		        if(SiS_Pr->SiS_VBInfo & (SetCRT2ToTV-SetCRT2ToHiVisionTV))
 				temp = ROMAddr[0x221];
@@ -636,19 +654,20 @@
 		}
 	   }
 
-    	   SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x13,~0x03C,temp);         /* TW: Panel Link Delay Compensation; (Software Command Reset; Power Saving) */
+    	   SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x13,~0x03C,temp);         /* Panel Link Delay Compensation; (Software Command Reset; Power Saving) */
 
 #endif  /* SIS300 */
 
   	} else {
 
-#ifdef SIS315H   /* ----------- 310/325/330 series ---------------*/
+#ifdef SIS315H   /* --------------- 315/330 series ---------------*/
 
 	   if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
                 temp = 0x10;
                 if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768)  temp = 0x2c;
     	        if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) temp = 0x20;
     	        if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x960)  temp = 0x24;
+		if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_PanelCustom)    temp = 0x2c;
 		if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
 		   temp = 0x08;
 		   if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) {
@@ -664,7 +683,7 @@
 		      }
     	           }
 		}
-		if(SiS_Pr->SiS_VBType & VB_SIS301B302B) {
+		if((SiS_Pr->SiS_VBType & VB_SIS301B302B) && (!(SiS_Pr->SiS_VBType & VB_NoLCD))) {
 		   tempbl = 0x00;
 		   if((ROMAddr) && (SiS_Pr->SiS_UseROM)) {
 		      if(HwDeviceExtension->jChipType < SIS_330) {
@@ -689,8 +708,16 @@
 		      if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToTV)) tempbl = 0x0F;
 		   }
 		}
+		
+		if(SiS_Pr->SiS_IF_DEF_DSTN || SiS_Pr->SiS_IF_DEF_FSTN) {
+		   temp = 0x08;
+		   tempbl = 0;
+		   if((ROMAddr) && (SiS_Pr->SiS_UseROM)) {
+		      if(ROMAddr[0x13c] & 0x80) tempbl = 0xf0;
+		   }
+		}
 	   }
-	   SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2D,tempbl,temp);	    /* TW: Panel Link Delay Compensation */
+	   SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2D,tempbl,temp);	    /* Panel Link Delay Compensation */
 
     	   tempax = 0;
     	   if (modeflag & DoubleScanMode) tempax |= 0x80;
@@ -705,14 +732,13 @@
 
      if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
 
-        /* TW: For 301BDH, we set up the Panel Link */
-        if( (SiS_Pr->SiS_VBType & VB_NoLCD) &&
-	    (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) ) {
+        /* For 301BDH with LCD, we set up the Panel Link */
+        if( (SiS_Pr->SiS_VBType & VB_NoLCD) && (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) ) {
 
 	    SiS_SetGroup1_LVDS(SiS_Pr,BaseAddr,ROMAddr,ModeNo,ModeIdIndex,
 	                       HwDeviceExtension,RefreshRateTableIndex);
 
-        } else if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {                             
+        } else if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
 
     	    SiS_SetGroup1_301(SiS_Pr,BaseAddr,ROMAddr,ModeNo,ModeIdIndex,
 	                      HwDeviceExtension,RefreshRateTableIndex);
@@ -749,23 +775,27 @@
   USHORT  push1,push2;
   USHORT  tempax,tempbx,tempcx,temp;
   USHORT  resinfo,modeflag;
+  unsigned char p1_7, p1_8;
 
   if(ModeNo <= 0x13) {
-    	modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
-    	resinfo = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ResInfo;
+     modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
+     resinfo = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ResInfo;
   } else {
+     if(SiS_Pr->UseCustomMode) {
+        modeflag = SiS_Pr->CModeFlag;
+	resinfo = 0;
+     } else {
     	modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
     	resinfo = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO;
+     }
   }
 
-  /* TW: The following is only done if bridge is in slave mode: */
+  /* The following is only done if bridge is in slave mode: */
 
   tempax = 0xFFFF;
   if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToTV))  tempax = SiS_GetVGAHT2(SiS_Pr);
 
-  if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-  	modeflag |= Charx8Dot;
-  }
+  if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV)  modeflag |= Charx8Dot;
 
   if(modeflag & Charx8Dot) tempcx = 0x08;
   else                     tempcx = 0x09;
@@ -777,8 +807,7 @@
   tempax = (tempax / tempcx) - 5;
   tempbx = tempax & 0x00FF;
 
-  temp = 0xFF;                                                  /* set MAX HT */
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x03,temp);
+  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x03,0xff);                 /* set MAX HT */
 
   tempax = SiS_Pr->SiS_VGAHDE;                                 	/* 0x04 Horizontal Display End */
   if(modeflag & HalfDCLK) tempax >>= 1;
@@ -789,13 +818,13 @@
 
   temp = (tempbx & 0xFF00) >> 8;
   if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
-        if(!(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV)) {        
-    	    temp += 2;
-        }
-  }	
+     if(!(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV)) {
+        temp += 2;
+     }
+  }
   if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) {
      if(SiS_Pr->SiS_HiVision == 3) {
-              if(resinfo == 7) temp -= 2;
+        if(resinfo == SIS_RI_800x600) temp -= 2;
      }
   }
   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x05,temp);                 /* 0x05 Horizontal Display Start */
@@ -804,140 +833,133 @@
 
   if((SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) &&
      (SiS_Pr->SiS_HiVision == 3)) {
-    temp = (tempbx & 0x00FF) - 1;
-    if(!(modeflag & HalfDCLK)) {
-      temp -= 6;
-      if(SiS_Pr->SiS_SetFlag & TVSimuMode) {
-        temp -= 2;
-        if(ModeNo > 0x13) temp -= 10;
-      }
-    }
+     temp = (tempbx & 0x00FF) - 1;
+     if(!(modeflag & HalfDCLK)) {
+        temp -= 6;
+        if(SiS_Pr->SiS_SetFlag & TVSimuMode) {
+           temp -= 2;
+           if(ModeNo > 0x13) temp -= 10;
+        }
+     }
   } else {
-    tempcx = tempbx & 0x00FF;
-    tempbx = (tempbx & 0xFF00) >> 8;
-    tempcx = (tempcx + tempbx) >> 1;
-    temp = (tempcx & 0x00FF) + 2;
-    if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV){
-       temp--;
-       if(!(modeflag & HalfDCLK)){
-          if((modeflag & Charx8Dot)){
-             temp += 4;
-             if(SiS_Pr->SiS_VGAHDE >= 800) temp -= 6;
-             if(HwDeviceExtension->jChipType >= SIS_315H) {
-	        if(SiS_Pr->SiS_VGAHDE == 800) temp += 2;
-             }
-          }
-       }
-    } else {
-       if(!(modeflag & HalfDCLK)) {
-          temp -= 4;
-          if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1280x960) {
-             if(SiS_Pr->SiS_VGAHDE >= 800) {
-                temp -= 7;
-	        if(HwDeviceExtension->jChipType < SIS_315H) {
-	           /* 650/301LV(x) does not do this, 630/301B, 300/301LV do */
-                   if(SiS_Pr->SiS_ModeType == ModeEGA) {
-                      if(SiS_Pr->SiS_VGAVDE == 1024) {
-                         temp += 15;
-                         if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1280x1024) 
-			    temp += 7;
-                      }
-                   }
-	        }
-                if(SiS_Pr->SiS_VGAHDE >= 1280) {
-                   if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1280x960) {
-                      if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) temp += 28;
-                   }
-                }
-             }
-          }
-       }
-    }
+     tempcx = tempbx & 0x00FF;
+     tempbx = (tempbx & 0xFF00) >> 8;
+     tempcx = (tempcx + tempbx) >> 1;
+     temp = (tempcx & 0x00FF) + 2;
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+        temp--;
+        if(!(modeflag & HalfDCLK)) {
+           if((modeflag & Charx8Dot)) {
+              temp += 4;
+              if(SiS_Pr->SiS_VGAHDE >= 800) temp -= 6;
+              if(HwDeviceExtension->jChipType >= SIS_315H) {
+	         if(SiS_Pr->SiS_VGAHDE == 800) temp += 2;
+              }
+           }
+        }
+     } else {
+        if(!(modeflag & HalfDCLK)) {
+           temp -= 4;
+           if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1280x960) {
+              if(SiS_Pr->SiS_VGAHDE >= 800) {
+                 temp -= 7;
+	         if(HwDeviceExtension->jChipType < SIS_315H) {
+	            /* 650/301LV(x) does not do this, 630/301B, 300/301LV do */
+                    if(SiS_Pr->SiS_ModeType == ModeEGA) {
+                       if(SiS_Pr->SiS_VGAVDE == 1024) {
+                          temp += 15;
+                          if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1280x1024)
+		  	     temp += 7;
+                       }
+                    }
+	         }
+                 if(SiS_Pr->SiS_VGAHDE >= 1280) {
+                    if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) temp += 28;
+                 }
+              }
+           }
+        }
+     }
   }
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x07,temp);               	/* 0x07 Horizontal Retrace Start */
 
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x08,0x00);                 /* 0x08 Horizontal Retrace End   */
+  p1_7 = temp;
+  p1_8 = 0x00;
 
   if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
      if(SiS_Pr->SiS_SetFlag & TVSimuMode) {
-            if(ModeNo <= 0x01) {
-	        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x07,0x2a);
-		if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) {
-		    SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x08,0x61);
-		} else {
-		    SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x08,0x41);
-		}
-	    } else if(SiS_Pr->SiS_ModeType == ModeText) {
-	        if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) {
-		    SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x07,0x54);
-		} else {
-		    SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x07,0x55);
-		}
-		SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x08,0x00);
-	    } else if(ModeNo <= 0x13) {
-	        if(modeflag & HalfDCLK) {
-		    if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) {
-		        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x07,0x30);
-			SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x08,0x03);
-		    } else {
-		        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x07,0x2f);
-			SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x08,0x02);
-		    }
-		} else {
-		    SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x07,0x5b);
-		    SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x08,0x03);
-		}
-	    } else if( ((HwDeviceExtension->jChipType >= SIS_315H) && (ModeNo == 0x50)) ||
-	               ((HwDeviceExtension->jChipType < SIS_315H) && (resinfo == 0 || resinfo == 1)) ) {
-	        if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) {
-		    SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x07,0x30);
-		    SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x08,0x03);
-		} else {
-		    SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x07,0x2f);
-		    SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x08,0x03);
-		}
-	    }
-
+        if(ModeNo <= 0x01) {
+	   p1_7 = 0x2a;
+	   if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) p1_8 = 0x61;
+	   else 	      			p1_8 = 0x41;
+	} else if(SiS_Pr->SiS_ModeType == ModeText) {
+	   if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) p1_7 = 0x54;
+	   else 	    			p1_7 = 0x55;
+	   p1_8 = 0x00;
+	} else if(ModeNo <= 0x13) {
+	   if(modeflag & HalfDCLK) {
+	      if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) {
+		 p1_7 = 0x30;
+		 p1_8 = 0x03;
+	      } else {
+	 	 p1_7 = 0x2f;
+		 p1_8 = 0x02;
+	      }
+	   } else {
+	      p1_7 = 0x5b;
+	      p1_8 = 0x03;
+	   }
+	} else if( ((HwDeviceExtension->jChipType >= SIS_315H) &&
+	            ((ModeNo == 0x50) || (ModeNo = 0x56) || (ModeNo = 0x53))) ||
+	           ((HwDeviceExtension->jChipType < SIS_315H) &&
+		    (resinfo == SIS_RI_320x200 || resinfo == SIS_RI_320x240)) ) {
+	   if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) {
+	      p1_7 = 0x30,
+	      p1_8 = 0x03;
+	   } else {
+	      p1_7 = 0x2f;
+	      p1_8 = 0x03;
+	   }
+        }
      }
   }
+
   if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) {
      if(SiS_Pr->SiS_HiVision & 0x03) {
-        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x07,0xb2);
+	p1_7 = 0xb2;
 	if(SiS_Pr->SiS_HiVision & 0x02) {
-	   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x07,0xab);
+	   p1_7 = 0xab;
 	}
      }
   }
 
+  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x07,p1_7);			/* 0x07 Horizontal Retrace Start */
+  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x08,p1_8);			/* 0x08 Horizontal Retrace End   */
+
+
   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x18,0x03);                	/* 0x18 SR08 (FIFO Threshold?)   */
 
   SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x19,0xF0);
 
-  tempbx = SiS_Pr->SiS_VGAVT;
-  push1 = tempbx;
-
   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x09,0xFF);                	/* 0x09 Set Max VT    */
 
   tempcx = 0x121;
   tempbx = SiS_Pr->SiS_VGAVDE;                               	/* 0x0E Vertical Display End */
-  if(tempbx == 357) tempbx = 350;
-  if(tempbx == 360) tempbx = 350;
-  if(tempbx == 375) tempbx = 350;
-  if(tempbx == 405) tempbx = 400;
-  if(tempbx == 420) tempbx = 400;
-  if(tempbx == 525) tempbx = 480;
+  if     (tempbx == 357) tempbx = 350;
+  else if(tempbx == 360) tempbx = 350;
+  else if(tempbx == 375) tempbx = 350;
+  else if(tempbx == 405) tempbx = 400;
+  else if(tempbx == 420) tempbx = 400;
+  else if(tempbx == 525) tempbx = 480;
   push2 = tempbx;
   if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
-    	if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
-      		if(!(SiS_Pr->SiS_SetFlag & LCDVESATiming)) {
-        		if(tempbx == 350) tempbx += 5;
-        		if(tempbx == 480) tempbx += 5;
-      		}
-    	}
+     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
+      	if(!(SiS_Pr->SiS_SetFlag & LCDVESATiming)) {
+           if     (tempbx == 350) tempbx += 5;
+           else if(tempbx == 480) tempbx += 5;
+      	}
+     }
   }
-  tempbx--;
-  temp = tempbx & 0x00FF;
-  tempbx--;
+  tempbx -= 2;
   temp = tempbx & 0x00FF;
   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x10,temp);        		/* 0x10 vertical Blank Start */
 
@@ -945,126 +967,91 @@
   tempbx--;
   temp = tempbx & 0x00FF;
 #if 0
-  /* TW: Missing code from 630/301B 2.04.5a and 650/302LV 1.10.6s (calles int 2f) */
+  /* Missing code from 630/301B 2.04.5a and 650/302LV 1.10.6s (calles int 2f) */
   if(xxx()) {
       if(temp == 0xdf) temp = 0xda;
   }
 #endif
   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0E,temp);
 
-  if(tempbx & 0x0100) {
-  	tempcx |= 0x0002;
-	if(SiS_Pr->SiS_VBType & VB_SIS301) tempcx |= 0x000a;
-  }
+  if(tempbx & 0x0100)  tempcx |= 0x0002;
 
   tempax = 0x000B;
   if(modeflag & DoubleScanMode) tempax |= 0x8000;
 
-  if(tempbx & 0x0200) {
-  	tempcx |= 0x0040;
-	if(SiS_Pr->SiS_VBType & VB_SIS301) tempax |= 0x2000;
-  }
-
-  if(SiS_Pr->SiS_VBType & VB_SIS301) {
-        if(SiS_Pr->SiS_VBInfo & SetPALTV) {
-	      if(SiS_Pr->SiS_VGAVDE == 480) {
-	             tempax = (tempax & 0x00ff) | 0x2000;
-		     if(modeflag & DoubleScanMode)  tempax |= 0x8000;
-	      }
-	}
-  }
+  if(tempbx & 0x0200)  tempcx |= 0x0040;
 
   temp = (tempax & 0xFF00) >> 8;
   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0B,temp);
 
-  if(tempbx & 0x0400) tempcx |= 0x0600;
+  if(tempbx & 0x0400)  tempcx |= 0x0600;
 
   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x11,0x00);                	/* 0x11 Vertical Blank End */
 
-  tempax = push1;
-  tempax -= tempbx;
-  tempax >>= 2;
-  push1 = tempax;
+  tempax = (SiS_Pr->SiS_VGAVT - tempbx) >> 2;
 
-  if(HwDeviceExtension->jChipType >= SIS_315H) {
-        /* TW: 650/30xLV 1.10.6s */
-        if(ModeNo > 0x13) {
-	    if(resinfo != 0x09) {  /* 1280x1024 */
-	        tempax <<= 1;
-		tempbx += tempax;
-	    }
-	} else {
-	    if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1400x1050) {
-	        tempax <<= 1;
-		tempbx += tempax;
-	    }
-	}
-  } else if((resinfo != 0x09) || (SiS_Pr->SiS_VBType & VB_SIS301)) {
-    	tempax <<= 1;
-    	tempbx += tempax;
+  if((ModeNo > 0x13) || (HwDeviceExtension->jChipType < SIS_315H)) {
+     if(resinfo != SIS_RI_1280x1024) {
+	tempbx += (tempax << 1);
+     }
+  } else if(HwDeviceExtension->jChipType >= SIS_315H) {
+     if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1400x1050) {
+	tempbx += (tempax << 1);
+     }
   }
 
-  if( (SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) &&
-      (SiS_Pr->SiS_HiVision == 3) ) {
-    	tempbx -= 10;
+  if((SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) &&
+     (SiS_Pr->SiS_HiVision == 3)) {
+     tempbx -= 10;
   } else {
-    	if(SiS_Pr->SiS_SetFlag & TVSimuMode) {
-      	   if(SiS_Pr->SiS_VBInfo & SetPALTV) {
-	       if(!(SiS_Pr->SiS_HiVision & 0x03)) {
-                    tempbx += 40;
-		    if(HwDeviceExtension->jChipType >= SIS_315H) {
-		       if(SiS_Pr->SiS_VGAHDE == 800) tempbx += 10;
-		    }
-      	       }
-	   }
-    	}
+     if(SiS_Pr->SiS_SetFlag & TVSimuMode) {
+        if(SiS_Pr->SiS_VBInfo & SetPALTV) {
+	   if(!(SiS_Pr->SiS_HiVision & 0x03)) {
+              tempbx += 40;
+	      if(HwDeviceExtension->jChipType >= SIS_315H) {
+	         if(SiS_Pr->SiS_VGAHDE == 800) tempbx += 10;
+	      }
+      	   }
+	}
+     }
   }
-  tempax = push1;
   tempax >>= 2;
   tempax++;
   tempax += tempbx;
   push1 = tempax;
   if(SiS_Pr->SiS_VBInfo & SetPALTV) {
-    	if(tempbx <= 513)  {
-      		if(tempax >= 513) tempbx = 513;
-    	}
+     if(tempbx <= 513)  {
+     	if(tempax >= 513) tempbx = 513;
+     }
   }
   temp = tempbx & 0x00FF;
   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0C,temp);			/* 0x0C Vertical Retrace Start */
 
-  if(!(SiS_Pr->SiS_VBType & VB_SIS301)) {
-  	tempbx--;
-  	temp = tempbx & 0x00FF;
-  	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x10,temp);
-
-	if(tempbx & 0x0100) tempcx |= 0x0008;
+  tempbx--;
+  temp = tempbx & 0x00FF;
+  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x10,temp);
 
-  	if(tempbx & 0x0200) {
-    	   SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x0B,0x20);
-	}
+  if(tempbx & 0x0100) tempcx |= 0x0008;
 
-  	tempbx++;
+  if(tempbx & 0x0200) {
+     SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x0B,0x20);
   }
+  tempbx++;
+
   if(tempbx & 0x0100) tempcx |= 0x0004;
   if(tempbx & 0x0200) tempcx |= 0x0080;
   if(tempbx & 0x0400) {
-        if(SiS_Pr->SiS_VBType & VB_SIS301) tempcx |= 0x0800;
-  	else                               tempcx |= 0x0C00;
+     if(SiS_Pr->SiS_VBType & VB_SIS301) tempcx |= 0x0800;
+     else                               tempcx |= 0x0C00;
   }
 
   tempbx = push1;
-  temp = tempbx & 0x00FF;
-  temp &= 0x0F;
+  temp = tempbx & 0x000F;
   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0D,temp);        		/* 0x0D vertical Retrace End */
 
   if(tempbx & 0x0010) tempcx |= 0x2000;
 
   temp = tempcx & 0x00FF;
-  if(SiS_Pr->SiS_VBType & VB_SIS301) {
-	if(SiS_Pr->SiS_VBInfo & SetPALTV) {
-	      if(SiS_Pr->SiS_VGAVDE == 480)  temp = 0xa3;
-	}
-  }
   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0A,temp);              	/* 0x0A CR07 */
 
   temp = (tempcx & 0xFF00) >> 8;
@@ -1074,8 +1061,8 @@
   temp = (tempax & 0xFF00) >> 8;
   temp = (temp >> 1) & 0x09;
   if(!(SiS_Pr->SiS_VBType & VB_SIS301)) {
-       /* Only use 8 dot clock */
-       temp |= 0x01;
+     /* Only use 8 dot clock */
+     temp |= 0x01;
   }
   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x16,temp);              	/* 0x16 SR01 */
 
@@ -1084,16 +1071,15 @@
   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x12,0x00);              	/* 0x12 CR17 */
 
   if(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit) {
-       if(IS_SIS650) {
-           /* TW: 650/30xLV 1.10.6s */
-           if(SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x00) & 0x01) {
-	       temp = 0x80;
-	   }
-       } else temp = 0x80;
-  } else  temp = 0x00;
+     if(IS_SIS650) {
+        /* 650/30xLV 1.10.6s */
+        if(SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x00) & 0x01) {
+	   temp = 0x80;
+	}
+     } else temp = 0x80;
+  } else temp = 0x00;
   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1A,temp);                	/* 0x1A SR0E */
 
-  return;
 }
 
 void
@@ -1108,40 +1094,52 @@
 #endif
   ULONG  tempeax=0, tempebx, tempecx, tempvcfact=0;
 
+  /* This is not supported on LVDS */
+  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_PanelCustom) return;
+  if(SiS_Pr->UseCustomMode) return;
+
   if(ModeNo <= 0x13) {
-    	modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
-    	resinfo = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ResInfo;
+     modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
+     resinfo = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ResInfo;
   } else {
-    	modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
-    	resinfo = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO;
+     modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
+     resinfo = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO;
   }
 
-  /* TW: Set up Panel Link */
+  /* Set up Panel Link */
 
   /* 1. Horizontal setup */
 
   tempax = SiS_Pr->SiS_LCDHDES;
 
-  if( (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480) &&
-      (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) ) {
-  	tempax -= 8;
+  if((!SiS_Pr->SiS_IF_DEF_FSTN) && (!SiS_Pr->SiS_IF_DEF_DSTN)) {
+     if( (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480) &&
+         (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) ) {
+  	   tempax -= 8;
+     }
   }
 
   tempcx = SiS_Pr->SiS_HT;    				  /* Horiz. Total */
 
   tempbx = SiS_Pr->SiS_HDE;                               /* Horiz. Display End */
 
+  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_2 ||
+     SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_3) {
+     tempbx >>= 1;
+  }
+
   if(!(SiS_Pr->SiS_LCDInfo & LCDPass11)) {
      if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
-        if((!SiS_Pr->SiS_IF_DEF_DSTN) && (SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480)) {
- 	   if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600)        tempbx =  800;
-	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x600)  tempbx = 1024;  /* TW */
-    	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768)  tempbx = 1024;
-	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1152x768)  tempbx = 1152;  /* TW */
-	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768)  tempbx = 1280;
-	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) tempbx = 1280; 
-	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) tempbx = 1400; 
-	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) tempbx = 1600; 
+        if(SiS_Pr->SiS_IF_DEF_FSTN || SiS_Pr->SiS_IF_DEF_DSTN) {
+	   tempbx = SiS_Pr->PanelXRes;
+	} else if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480) {
+	   tempbx = SiS_Pr->PanelXRes;
+	   if(SiS_Pr->SiS_CustomT == CUT_BARCO1024) {
+	      tempbx = 800;
+	      if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel800x600) {
+	         tempbx = 1024;
+	      }
+	   }
         }
      }
   }
@@ -1154,13 +1152,17 @@
   if(tempax >= SiS_Pr->SiS_HT) tempax -= SiS_Pr->SiS_HT;
 
   push2 = tempax;
-  
-  if(!(SiS_Pr->SiS_LCDInfo & LCDPass11)) { 
-     if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
-        if((!SiS_Pr->SiS_IF_DEF_DSTN) && (SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480)) {
-     	   if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600)        tempcx = 0x0028;
-	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x600)  tempcx = 0x0018;
-     	   else if( (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) ||
+
+  if((!SiS_Pr->SiS_IF_DEF_FSTN) &&
+     (!SiS_Pr->SiS_IF_DEF_DSTN) &&
+     (SiS_Pr->SiS_CustomT != CUT_BARCO1366) &&
+     (SiS_Pr->SiS_CustomT != CUT_BARCO1024)) {
+     if(!(SiS_Pr->SiS_LCDInfo & LCDPass11)) {
+        if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
+           if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480) {
+     	      if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600)        tempcx = 0x0028;
+	      else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x600)  tempcx = 0x0018;
+     	      else if( (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) ||
 	            (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1152x768) ) {
 	  	   if(HwDeviceExtension->jChipType < SIS_315H) {
 		      if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
@@ -1174,11 +1176,12 @@
 		   } else {
 		      tempcx = 0x0018;
 		   }
+	      }
+	      else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768)  tempcx = 0x0028;
+	      else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) tempcx = 0x0030;
+	      else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) tempcx = 0x0030;
+	      else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) tempcx = 0x0040;
 	   }
-	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768)  tempcx = 0x0028;
-	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) tempcx = 0x0030;
-	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) tempcx = 0x0030;
-	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) tempcx = 0x0040;
         }
      }
   }
@@ -1188,14 +1191,17 @@
 
   tempax = tempcx >> 3;                          /* BPLHRS */
   temp = tempax & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x14,temp);		 /* Part1_14h; TW: Panel Link Horizontal Retrace Start  */
+  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x14,temp);		 /* Part1_14h; Panel Link Horizontal Retrace Start  */
 
   if(SiS_Pr->SiS_LCDInfo & LCDPass11) {
      temp = (tempax & 0x00FF) + 2;
   } else {
      temp = (tempax & 0x00FF) + 10;
      if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
-        if(!SiS_Pr->SiS_IF_DEF_DSTN) {
+        if((!SiS_Pr->SiS_IF_DEF_DSTN) &&
+	   (!SiS_Pr->SiS_IF_DEF_FSTN) &&
+	   (SiS_Pr->SiS_CustomT != CUT_BARCO1366) &&
+	   (SiS_Pr->SiS_CustomT != CUT_BARCO1024)) {
            if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480) {
 	      temp += 6;
               if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel800x600) {
@@ -1219,47 +1225,57 @@
 
   temp &= 0x1F;
   temp |= ((tempcx & 0x0007) << 5);
-  if(SiS_Pr->SiS_IF_DEF_FSTN) temp = 0x20;
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x15,temp);    	 /* Part1_15h; TW: Panel Link Horizontal Retrace End/Skew */
+#if 0
+  if(SiS_Pr->SiS_IF_DEF_FSTN) temp = 0x20;       /* WRONG? BIOS loads cl, not ah */
+#endif  
+  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x15,temp);    	 /* Part1_15h; Panel Link Horizontal Retrace End/Skew */
 
   tempbx = push2;
   tempcx = push1;                                /* lcdhdes  */
 
   temp = (tempcx & 0x0007);                      /* BPLHDESKEW  */
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1A,temp);   	 /* Part1_1Ah; TW: Panel Link Vertical Retrace Start (2:0) */
+  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1A,temp);   	 /* Part1_1Ah; Panel Link Vertical Retrace Start (2:0) */
 
   tempcx >>= 3;                                  /* BPLHDES */
   temp = (tempcx & 0x00FF);
-  if(ModeNo == 0x5b) temp--;                     
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x16,temp);    	 /* Part1_16h; TW: Panel Link Horizontal Display Enable Start  */
+#if 0 /* Not 550 FSTN */
+  if(HwDeviceExtension->jChipType >= SIS_315H) {
+     if(ModeNo == 0x5b) temp--; */
+  }
+#endif
+  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x16,temp);    	 /* Part1_16h; Panel Link Horizontal Display Enable Start  */
 
-  if(HwDeviceExtension->jChipType < SIS_315H) {  
+  if((HwDeviceExtension->jChipType < SIS_315H) ||
+     (SiS_Pr->SiS_IF_DEF_FSTN) ||
+     (SiS_Pr->SiS_IF_DEF_DSTN)) {
      if(tempbx & 0x07) tempbx += 8;              
   }
   tempbx >>= 3;                                  /* BPLHDEE  */
   temp = tempbx & 0x00FF;
-  if(ModeNo == 0x5b) temp--;			 
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x17,temp);   	 /* Part1_17h; TW: Panel Link Horizontal Display Enable End  */
+#if 0 /* Not 550 FSTN */
+  if(HwDeviceExtension->jChipType >= SIS_315H) {
+     if(ModeNo == 0x5b) temp--;
+  }
+#endif
+  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x17,temp);   	 /* Part1_17h; Panel Link Horizontal Display Enable End  */
 
   /* 2. Vertical setup */
 
   if(HwDeviceExtension->jChipType < SIS_315H) {
      tempcx = SiS_Pr->SiS_VGAVT;
      tempbx = SiS_Pr->SiS_VGAVDE;
-     if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
-        if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480) {
-	   if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600)       tempbx =  600;
-	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x600) tempbx =  600;
-	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) tempbx =  768;
-	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1152x768) tempbx =  768;
-	   else								tempbx = 1024;
-        }
+     if((SiS_Pr->SiS_CustomT != CUT_BARCO1366) && (SiS_Pr->SiS_CustomT != CUT_BARCO1024)) {
+        if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
+           if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480) {
+	      tempbx = SiS_Pr->PanelYRes;
+           }
+	}
      }
      tempcx -= tempbx;
 
   } else {
 
-     tempcx = SiS_Pr->SiS_VGAVT - SiS_Pr->SiS_VGAVDE;          /* VGAVT-VGAVDE  */
+     tempcx = SiS_Pr->SiS_VGAVT - SiS_Pr->SiS_VGAVDE;           /* VGAVT-VGAVDE  */
 
   }
 
@@ -1268,18 +1284,20 @@
 
   tempax = SiS_Pr->SiS_VGAVDE;
 
-  if((SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) && (!SiS_Pr->SiS_IF_DEF_DSTN)) {
-     if( (SiS_Pr->SiS_IF_DEF_TRUMPION == 0)   && 
-         (!(SiS_Pr->SiS_LCDInfo & LCDPass11)) &&
-         (SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480) ) {
-        if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600)        tempax =  600;
-        else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x600)  tempax =  600;  
-        else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768)  tempax =  768;
-        else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1152x768)  tempax =  768;  
-        else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768)  tempax =  768;
-        else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) tempax = 1024; 
-        else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) tempax = 1050; 
-        else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) tempax = 1200; 
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
+     if((SiS_Pr->SiS_CustomT == CUT_BARCO1366) || (SiS_Pr->SiS_CustomT == CUT_BARCO1024)) {
+        if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480) {
+           tempax = 600;
+	   if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel800x600) {
+	      tempax = 768;
+	   }
+	}
+     } else if( (SiS_Pr->SiS_IF_DEF_TRUMPION == 0)   &&
+                (!(SiS_Pr->SiS_LCDInfo & LCDPass11)) &&
+                ((SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480) ||
+	         (SiS_Pr->SiS_IF_DEF_FSTN) ||
+	         (SiS_Pr->SiS_IF_DEF_DSTN)) ) {
+	tempax = SiS_Pr->PanelYRes;
      }
   }
 
@@ -1290,11 +1308,24 @@
 
   tempcx >>= 1;
 
-  if((SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) && (SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480)) {
-     if(!(SiS_Pr->SiS_LCDInfo & LCDPass11)) {
-        if(!SiS_Pr->SiS_IF_DEF_DSTN) {
-     	   if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600)        tempcx = 0x0001;
-	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x600)  tempcx = 0x0001;
+  if((SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) &&
+     (SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480) &&
+     (SiS_Pr->SiS_CustomT != CUT_BARCO1366) &&
+     (SiS_Pr->SiS_CustomT != CUT_BARCO1024)) {
+     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_2 ||
+        SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_3) {
+	tempcx = 0x0017;
+     } else if(!(SiS_Pr->SiS_LCDInfo & LCDPass11)) {
+        if(SiS_Pr->SiS_IF_DEF_FSTN || SiS_Pr->SiS_IF_DEF_DSTN) {
+	   if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600)         tempcx = 0x0003;
+  	   else if((SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) ||
+	           (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768)) tempcx = 0x0003;
+           else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024)  tempcx = 0x0001;
+           else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050)  tempcx = 0x0001;
+	   else 							  tempcx = 0x0057;
+        } else  {
+     	   if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600)         tempcx = 0x0001;
+	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x600)   tempcx = 0x0001;
      	   else if((SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) ||
 	           (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1152x768)) {
 		   if(HwDeviceExtension->jChipType < SIS_315H) {
@@ -1304,7 +1335,7 @@
 			    tempcx = 0x0003;
 #endif
 		      } else {
-		            tempcx = 0x0002;   /* TW: A901; sometimes 0x0003; */
+		            tempcx = 0x0002;   /* A901; sometimes 0x0003; */
 		      }
 		   } else tempcx = 0x0003;
            }
@@ -1319,26 +1350,32 @@
 
   tempbx += tempcx;			 	/* BPLVRS  */
 
-  if(HwDeviceExtension->jChipType < SIS_315H) {
-      tempbx++;
+  if((HwDeviceExtension->jChipType < SIS_315H) ||
+     (SiS_Pr->SiS_IF_DEF_FSTN) ||
+     (SiS_Pr->SiS_IF_DEF_DSTN)) {
+     tempbx++;
   }
 
   if(tempbx >= SiS_Pr->SiS_VT) tempbx -= SiS_Pr->SiS_VT;
 
   temp = tempbx & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x18,temp);       	 /* Part1_18h; TW: Panel Link Vertical Retrace Start  */
+  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x18,temp);       	 /* Part1_18h; Panel Link Vertical Retrace Start  */
 
   tempcx >>= 3;
 
-  if(!(SiS_Pr->SiS_LCDInfo & LCDPass11)) {
+  if((!(SiS_Pr->SiS_LCDInfo & LCDPass11)) &&
+     (SiS_Pr->SiS_CustomT != CUT_BARCO1366) &&
+     (SiS_Pr->SiS_CustomT != CUT_BARCO1024)) {
      if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
         if( (HwDeviceExtension->jChipType < SIS_315H) &&
             (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480) )     tempcx = 0x0001;
+	else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_2)  tempcx = 0x0002;
+	else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_3)  tempcx = 0x0002;
         else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600)    tempcx = 0x0003;
         else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x600)   tempcx = 0x0005;
         else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1152x768)   tempcx = 0x0005;
 	else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768)   tempcx = 0x0011;
-        else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024)  tempcx = 0x0005;	 
+        else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024)  tempcx = 0x0005;
         else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050)  tempcx = 0x0002;
         else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200)  tempcx = 0x0011;
         else if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480)  {
@@ -1360,26 +1397,36 @@
 
   tempcx = tempcx + tempbx + 1;                  /* BPLVRE  */
   temp = tempcx & 0x000F;
-  SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x19,0xf0,temp); /* Part1_19h; TW: Panel Link Vertical Retrace End (3:0); Misc.  */
+  if(SiS_Pr->SiS_IF_DEF_FSTN ||
+     SiS_Pr->SiS_IF_DEF_DSTN ||
+     (SiS_Pr->SiS_CustomT == CUT_BARCO1366) ||
+     (SiS_Pr->SiS_CustomT == CUT_BARCO1024)) {
+     temp |= 0x30;
+  }
+  SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x19,0xf0,temp); /* Part1_19h; Panel Link Vertical Retrace End (3:0); Misc.  */
 
   temp = ((tempbx & 0x0700) >> 8) << 3;          /* BPLDESKEW =0 */
-  if(SiS_Pr->SiS_VGAVDE != SiS_Pr->SiS_VDE)  temp |= 0x40;
-  if(SiS_Pr->SiS_SetFlag & EnableLVDSDDA)    temp |= 0x40;
+  if(SiS_Pr->SiS_IF_DEF_FSTN || SiS_Pr->SiS_IF_DEF_DSTN) {
+     if(SiS_Pr->SiS_HDE != 640) {
+        if(SiS_Pr->SiS_VGAVDE != SiS_Pr->SiS_VDE)   temp |= 0x40;
+     }
+  } else if(SiS_Pr->SiS_VGAVDE != SiS_Pr->SiS_VDE)  temp |= 0x40;
+  if(SiS_Pr->SiS_SetFlag & EnableLVDSDDA)           temp |= 0x40;
   if(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit) {
-      if(HwDeviceExtension->jChipType >= SIS_315H) {
-         if(SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x00) & 0x01) {
-            temp |= 0x80;
-         }
-      } else {
-	 if( (HwDeviceExtension->jChipType == SIS_630) ||
-	     (HwDeviceExtension->jChipType == SIS_730) ) {
-	    if(HwDeviceExtension->jChipRevision >= 0x30) {
-	       temp |= 0x80;
-	    }
-	 }
-      }
+     if(HwDeviceExtension->jChipType >= SIS_315H) {
+        if(SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x00) & 0x01) {
+           temp |= 0x80;
+        }
+     } else {
+	if( (HwDeviceExtension->jChipType == SIS_630) ||
+	    (HwDeviceExtension->jChipType == SIS_730) ) {
+	   if(HwDeviceExtension->jChipRevision >= 0x30) {
+	      temp |= 0x80;
+	   }
+	}
+     }
   }
-  SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x1A,0x87,temp);  /* Part1_1Ah; TW: Panel Link Control Signal (7:3); Vertical Retrace Start (2:0) */
+  SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x1A,0x87,temp);  /* Part1_1Ah; Panel Link Control Signal (7:3); Vertical Retrace Start (2:0) */
 
   if (HwDeviceExtension->jChipType < SIS_315H) {
 
@@ -1396,19 +1443,19 @@
 	}
 
   	temp = (USHORT)(tempebx & 0x00FF);
-  	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1E,temp);      /* Part1_1Eh; TW: Panel Link Vertical Scaling Factor */
+  	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1E,temp);      /* Part1_1Eh; Panel Link Vertical Scaling Factor */
 
 #endif /* SIS300 */
 
   } else {
 
-#ifdef SIS315H  /* 310/325 series */
+#ifdef SIS315H  /* 315 series */
 
-#ifdef NEWCH701x
-        SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x1E,0x03);
-#else
-	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1E,0x23);
-#endif	
+        if(HwDeviceExtension->jChipType == SIS_740) {
+           SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x1E,0x03);
+        } else {
+	   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1E,0x23);
+	}
 
 	tempeax = SiS_Pr->SiS_VGAVDE << 18;
     	temp = (USHORT)(tempeax % (ULONG)SiS_Pr->SiS_VDE);
@@ -1417,12 +1464,13 @@
     	tempebx = tempeax;                         /* BPLVCFACT  */
         tempvcfact = tempeax;
     	temp = (USHORT)(tempebx & 0x00FF);
-    	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x37,temp);      /* Part1_37h; TW: Panel Link Vertical Scaling Factor */
+    	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x37,temp);      /* Part1_37h; Panel Link Vertical Scaling Factor */
     	temp = (USHORT)((tempebx & 0x00FF00) >> 8);
-    	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x36,temp);      /* Part1_36h; TW: Panel Link Vertical Scaling Factor */
+    	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x36,temp);      /* Part1_36h; Panel Link Vertical Scaling Factor */
     	temp = (USHORT)((tempebx & 0x00030000) >> 16);
+	temp &= 0x03;
     	if(SiS_Pr->SiS_VDE == SiS_Pr->SiS_VGAVDE) temp |= 0x04;
-    	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x35,temp);      /* Part1_35h; TW: Panel Link Vertical Scaling Factor */
+    	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x35,temp);      /* Part1_35h; Panel Link Vertical Scaling Factor */
 
 #endif /* SIS315H */
 
@@ -1434,47 +1482,50 @@
   push1 = temp;					   
 
   if(!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
-   	if(!SiS_Pr->SiS_IF_DEF_DSTN){
+   	if(!SiS_Pr->SiS_IF_DEF_FSTN && !SiS_Pr->SiS_IF_DEF_DSTN) {
 		if(HwDeviceExtension->jChipType < SIS_315H) {
 			if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x600) {
-      				if(resinfo == 15) tempcx++;
+      				if(resinfo == SIS_RI_1024x600) tempcx++;
 				if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
-					if(resinfo == 7) tempcx++;
+					if(resinfo == SIS_RI_800x600) tempcx++;
 		    		}
 			} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600) {
-      				if(resinfo == 7) tempcx++;
-				if(resinfo == 8) tempcx++; /* TW: Doesnt make sense anyway... */
-			} else  if(resinfo == 8) tempcx++;
+      				if(resinfo == SIS_RI_800x600)  tempcx++;
+				if(resinfo == SIS_RI_1024x768) tempcx++; /* Doesnt make sense anyway... */
+			} else  if(resinfo == SIS_RI_1024x768) tempcx++;
 		} else {
 			if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600) {
-      				if(resinfo == 7) tempcx++;
+      				if(resinfo == SIS_RI_800x600)  tempcx++;
 			}
 		}
 	}
   }
 
   if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480) {
-     tempcx = SiS_Pr->SiS_VGAVDE;
-     tempbx = SiS_Pr->SiS_VGAVDE - 1;
+     if((!SiS_Pr->SiS_IF_DEF_FSTN) && (!SiS_Pr->SiS_IF_DEF_DSTN)) {
+        tempcx = SiS_Pr->SiS_VGAVDE;
+        tempbx = SiS_Pr->SiS_VGAVDE - 1;
+     }
   }
 
   temp = ((tempbx & 0x0700) >> 8) << 3;
   temp |= ((tempcx & 0x0700) >> 8);
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1D,temp);     	/* Part1_1Dh; TW: Vertical Display Overflow; Control Signal */
+  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1D,temp);     	/* Part1_1Dh; Vertical Display Overflow; Control Signal */
 
   temp = tempbx & 0x00FF;
-  if(SiS_Pr->SiS_IF_DEF_FSTN) temp++;
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1C,temp);      	/* Part1_1Ch; TW: Panel Link Vertical Display Enable End  */
+  /* if(SiS_Pr->SiS_IF_DEF_FSTN) temp++;  */
+  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1C,temp);      	/* Part1_1Ch; Panel Link Vertical Display Enable End  */
 
   temp = tempcx & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1B,temp);      	/* Part1_1Bh; TW: Panel Link Vertical Display Enable Start  */
+  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1B,temp);      	/* Part1_1Bh; Panel Link Vertical Display Enable Start  */
 
   /* 3. Additional horizontal setup (scaling, etc) */
 
   tempecx = SiS_Pr->SiS_VGAHDE;
   if(HwDeviceExtension->jChipType >= SIS_315H) {
-     if(modeflag & HalfDCLK)
-        tempecx >>= 1;
+     if((!SiS_Pr->SiS_IF_DEF_FSTN) && (!SiS_Pr->SiS_IF_DEF_DSTN)) {
+        if(modeflag & HalfDCLK) tempecx >>= 1;
+     }
   }
   tempebx = SiS_Pr->SiS_HDE;
   if(tempecx == tempebx) tempeax = 0xFFFF;
@@ -1484,139 +1535,165 @@
      temp = (USHORT)(tempeax % tempebx);
      tempeax = tempeax / tempebx;
      if(HwDeviceExtension->jChipType >= SIS_315H) {
-         if(temp) tempeax++;
+        if(temp) tempeax++;
      }
   }
   tempecx = tempeax;
 
   if(HwDeviceExtension->jChipType >= SIS_315H) {
-      tempeax = SiS_Pr->SiS_VGAHDE;
-      if(modeflag & HalfDCLK) tempeax >>= 1;
-      tempeax <<= 16;
-      tempeax = (tempeax / tempecx) - 1;
+     tempeax = SiS_Pr->SiS_VGAHDE;
+     if((!SiS_Pr->SiS_IF_DEF_FSTN) && (!SiS_Pr->SiS_IF_DEF_DSTN)) {
+        if(modeflag & HalfDCLK) tempeax >>= 1;
+     }
+     tempeax <<= 16;
+     tempeax = (tempeax / tempecx) - 1;
   } else {
-      tempeax = ((SiS_Pr->SiS_VGAHT << 16) / tempecx) - 1;
+     tempeax = ((SiS_Pr->SiS_VGAHT << 16) / tempecx) - 1;
   }
   tempecx <<= 16;
   tempecx |= (tempeax & 0xFFFF);
   temp = (USHORT)(tempecx & 0x00FF);
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1F,temp);  	 /* Part1_1Fh; TW: Panel Link DDA Operational Number in each horiz. line */
+  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1F,temp);  	 /* Part1_1Fh; Panel Link DDA Operational Number in each horiz. line */
 
   tempbx = SiS_Pr->SiS_VDE;
   if(HwDeviceExtension->jChipType >= SIS_315H) {
-      tempeax = (SiS_Pr->SiS_VGAVDE << 18) / tempvcfact;
-      tempbx = (USHORT)(tempeax & 0x0FFFF);
+     tempeax = (SiS_Pr->SiS_VGAVDE << 18) / tempvcfact;
+     tempbx = (USHORT)(tempeax & 0x0FFFF);
   } else {
-      tempax = SiS_Pr->SiS_VGAVDE << 6;
-      tempbx = push1;
-      tempbx &= 0x3f;
-      if(tempbx == 0) tempbx = 64;
-      tempax = tempax / tempbx;
-      tempbx = tempax;
+     tempeax = SiS_Pr->SiS_VGAVDE << 6;
+     tempbx = push1 & 0x3f;
+     if(tempbx == 0) tempbx = 64;
+     tempeax /= tempbx;
+     tempbx = (USHORT)(tempeax & 0x0FFFF);
   }
   if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) tempbx--;
-  if(SiS_Pr->SiS_SetFlag & EnableLVDSDDA)                 tempbx = 1;
+  if(SiS_Pr->SiS_SetFlag & EnableLVDSDDA) {
+     if((!SiS_Pr->SiS_IF_DEF_FSTN) && (!SiS_Pr->SiS_IF_DEF_DSTN)) tempbx = 1;
+     else if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480)  tempbx = 1;
+  }
 
   temp = ((tempbx & 0xFF00) >> 8) << 3;
   temp |= (USHORT)((tempecx & 0x0700) >> 8);
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x20,temp);  	/* Part1_20h; TW: Overflow register */
+  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x20,temp);  	/* Part1_20h; Overflow register */
 
   temp = tempbx & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x21,temp);  	/* Part1_21h; TW: Panel Link Vertical Accumulator Register */
+  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x21,temp);  	/* Part1_21h; Panel Link Vertical Accumulator Register */
 
   tempecx >>= 16;                               /* BPLHCFACT  */
-  if(HwDeviceExtension->jChipType < SIS_315H) {
-      if(modeflag & HalfDCLK) tempecx >>= 1;
+  if((HwDeviceExtension->jChipType < SIS_315H) || (SiS_Pr->SiS_IF_DEF_FSTN) || (SiS_Pr->SiS_IF_DEF_DSTN)) {
+     if(modeflag & HalfDCLK) tempecx >>= 1;
   }
   temp = (USHORT)((tempecx & 0xFF00) >> 8);
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x22,temp);     	/* Part1_22h; TW: Panel Link Horizontal Scaling Factor High */
+  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x22,temp);     	/* Part1_22h; Panel Link Horizontal Scaling Factor High */
 
   temp = (USHORT)(tempecx & 0x00FF);
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x23,temp);         /* Part1_22h; TW: Panel Link Horizontal Scaling Factor Low */
+  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x23,temp);         /* Part1_22h; Panel Link Horizontal Scaling Factor Low */
 
   /* 630/301B and 630/LVDS do something for 640x480 panels here */
 
 #ifdef SIS315H
-  /* TW: DSTN/FSTN initialisation - hardcoded for 320x480 panel */
-  if(SiS_Pr->SiS_IF_DEF_DSTN) {
-     	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1E,0x01);
-     	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x25,0x00);
-     	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x26,0x00);
-     	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x27,0x00);
-     	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x28,0x87);
-     	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x29,0x5A);
-     	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x2A,0x4B);
-     	SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x44,~0x007,0x03);
-     	tempbx = SiS_Pr->SiS_HDE + 64;                       	/*Blps = lcdhdee(lcdhdes+HDE) + 64*/
-     	temp = tempbx & 0x00FF;
-     	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x38,temp);
-     	temp=((tempbx & 0xFF00) >> 8) << 3;
-     	SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x35,~0x078,temp);
-     	tempbx += 32;		                     		/*Blpe=lBlps+32*/
-     	temp = tempbx & 0x00FF;
-     	if(SiS_Pr->SiS_IF_DEF_FSTN)  temp=0;
-     	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x39,temp);
-     	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x3A,0x00);        	/*Bflml=0*/
-     	SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x3C,~0x007,0x00);
-     	tempbx = SiS_Pr->SiS_VDE / 2;
-     	temp = tempbx & 0x00FF;
-     	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x3B,temp);
-     	temp = ((tempbx & 0xFF00) >> 8) << 3;
-     	SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x3C,~0x038,temp);
-     	tempeax = SiS_Pr->SiS_HDE << 2;                       	/* BDxFIFOSTOP = (HDE*4)/128 */
-     	tempebx = 128;
-     	temp = (USHORT)(tempeax % tempebx);
-     	tempeax = tempeax / tempebx;
-     	if(temp != 0)  tempeax++;
-     	temp = (USHORT)(tempeax & 0x003F);
-     	SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x45,~0x0FF,temp);
-     	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x3F,0x00);         	/* BDxWadrst0 */
-     	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x3E,0x00);
-     	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x3D,0x10);
-     	SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x3C,~0x040,0x00);
-     	tempax = SiS_Pr->SiS_HDE >> 4;                        	/* BDxWadroff = HDE*4/8/8 */
-     	pushcx = tempax;
-     	temp = tempax & 0x00FF;
-     	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x43,temp);
-     	temp = ((tempax & 0xFF00) >> 8) << 3;
-     	SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x44,~0x0F8,temp);
-     	tempax = SiS_Pr->SiS_VDE;                             /*BDxWadrst1 = BDxWadrst0 + BDxWadroff * VDE */
-     	tempeax = (tempax * pushcx);
-     	tempebx = 0x00100000 + tempeax;
-     	temp = (USHORT)tempebx & 0x000000FF;
-     	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x42,temp);
-     	temp = (USHORT)((tempebx & 0x0000FF00)>>8);
-     	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x41,temp);
-     	temp = (USHORT)((tempebx & 0x00FF0000)>>16);
-     	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x40,temp);
-     	temp = (USHORT)(((tempebx & 0x01000000)>>24) << 7);
-     	SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x3C,~0x080,temp);
-     	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x2F,0x03);
-     	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x03,0x50);
-     	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x04,0x00);
-     	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x2F,0x01);
-     	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x13,0x00);
-     	SiS_SetReg1(SiS_Pr->SiS_P3c4,0x05,0x86);        /* Unlock */
-     	SiS_SetReg1(SiS_Pr->SiS_P3c4,0x1e,0x62);
-     	if(SiS_Pr->SiS_IF_DEF_FSTN){
-         	SiS_SetReg1(SiS_Pr->SiS_P3c4,0x2b,0x1b);
-         	SiS_SetReg1(SiS_Pr->SiS_P3c4,0x2c,0xe3);
-         	SiS_SetReg1(SiS_Pr->SiS_P3c4,0x1e,0x62);
-         	SiS_SetReg1(SiS_Pr->SiS_P3c4,0x2e,0x04);
-         	SiS_SetReg1(SiS_Pr->SiS_P3c4,0x2f,0x42);
-         	SiS_SetReg1(SiS_Pr->SiS_P3c4,0x32,0x01);
-         	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x2b,0x02);
-         	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x2c,0x00);
-         	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x2d,0x00);
-     	}
-     	SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x19,0x0f,0x30);
-     	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1e,0x7d);
-     	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x2e,0xe0);
+  if(SiS_Pr->SiS_IF_DEF_FSTN || SiS_Pr->SiS_IF_DEF_DSTN) {
+     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x25,0x00);
+     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x26,0x00);
+     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x27,0x00);
+     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x28,0x87);
+     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x29,0x5A);
+     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x2A,0x4B);
+     SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x44,~0x007,0x03);
+     tempax = SiS_Pr->SiS_HDE;                       		/* Blps = lcdhdee(lcdhdes+HDE) + 64 */
+     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_2 ||
+        SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_3) tempax >>= 1;
+     tempax += 64;
+     temp = tempax & 0x00FF;
+     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x38,temp);
+     temp = ((tempax & 0xFF00) >> 8) << 3;
+     SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x35,~0x078,temp);
+     tempax += 32;		                     		/* Blpe=lBlps+32 */
+     temp = tempax & 0x00FF;
+     if(SiS_Pr->SiS_IF_DEF_FSTN) temp = 0;
+     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x39,temp);
+     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x3A,0x00);        	/* Bflml=0 */
+     SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x3C,~0x007,0x00);
+
+     tempax = SiS_Pr->SiS_VDE;
+     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_2 ||
+        SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_3) tempax >>= 1;
+     tempax >>= 1;
+     temp = tempax & 0x00FF;
+     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x3B,temp);
+     temp = ((tempax & 0xFF00) >> 8) << 3;
+     SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x3C,~0x038,temp);
+
+     tempeax = SiS_Pr->SiS_HDE;
+     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_2 ||
+        SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_3) tempeax >>= 1;
+     tempeax <<= 2;                       			/* BDxFIFOSTOP = (HDE*4)/128 */
+     tempebx = 128;
+     temp = (USHORT)(tempeax % tempebx);
+     tempeax = tempeax / tempebx;
+     if(temp) tempeax++;
+     temp = (USHORT)(tempeax & 0x003F);
+     SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x45,~0x0FF,temp);
+     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x3F,0x00);         	/* BDxWadrst0 */
+     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x3E,0x00);
+     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x3D,0x10);
+     SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x3C,~0x040,0x00);
+
+     tempax = SiS_Pr->SiS_HDE;
+     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_2 ||
+        SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_3) tempax >>= 1;
+     tempax >>= 4;                        			/* BDxWadroff = HDE*4/8/8 */
+     pushcx = tempax;
+     temp = tempax & 0x00FF;
+     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x43,temp);
+     temp = ((tempax & 0xFF00) >> 8) << 3;
+     SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x44,~0x0F8,temp);
+
+     tempax = SiS_Pr->SiS_VDE;                             	/* BDxWadrst1 = BDxWadrst0 + BDxWadroff * VDE */
+     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_2 ||
+        SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_3) tempax >>= 1;
+     tempeax = (tempax * pushcx);
+     tempebx = 0x00100000 + tempeax;
+     temp = (USHORT)tempebx & 0x000000FF;
+     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x42,temp);
+     temp = (USHORT)((tempebx & 0x0000FF00) >> 8);
+     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x41,temp);
+     temp = (USHORT)((tempebx & 0x00FF0000) >> 16);
+     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x40,temp);
+     temp = (USHORT)(((tempebx & 0x01000000) >> 24) << 7);
+     SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x3C,~0x080,temp);
+
+     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x2F,0x03);
+     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x03,0x50);
+     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x04,0x00);
+     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x2F,0x01);
+     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x19,0x38);
+
+     if(SiS_Pr->SiS_IF_DEF_FSTN) {
+        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x2b,0x02);
+        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x2c,0x00);
+        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x2d,0x00);
+        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x35,0x0c);
+        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x36,0x00);
+        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x37,0x00);
+        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x38,0x80);
+        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x39,0xA0);
+        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x3a,0x00);
+        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x3b,0xf0);
+        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x3c,0x00);
+        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x3d,0x10);
+        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x3e,0x00);
+        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x3f,0x00);
+        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x40,0x10);
+        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x41,0x25);
+        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x42,0x80);
+        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x43,0x14);
+        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x44,0x03);
+        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x45,0x0a);
+     }
   }
 #endif  /* SIS315H */
 
-  return;
-
 }
 
 #ifdef SIS315H
@@ -1629,7 +1706,7 @@
 
 
 #ifdef SIS315H
-/* TW: For LVDS / 302B/30xLV - LCDA (this must only be called on 310/325 series!) */
+/* For LVDS / 302B/30xLV - LCDA (this must only be called on 315 series!) */
 void
 SiS_SetGroup1_LCDA(SiS_Private *SiS_Pr, USHORT  BaseAddr,UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex,
                    PSIS_HW_DEVICE_INFO HwDeviceExtension,USHORT RefreshRateTableIndex)
@@ -1638,6 +1715,10 @@
   USHORT push1,push2,tempax,tempbx,tempcx,temp;
   ULONG tempeax=0,tempebx,tempecx,tempvcfact;
 
+  /* This is not supported with LCDA */
+  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_PanelCustom) return;
+  if(SiS_Pr->UseCustomMode) return;
+
   if(IS_SIS330) {
      SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2D,0x10);			/* Xabre 1.01.03 */
   } else if(IS_SIS740) {
@@ -1645,7 +1726,7 @@
         SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x13,0xfb,0x04);      	/* 740/LVDS */
 	SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2D,0x03);
      } else {
-        SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2D,0x10);			/* 740/301LV 1.10.1i */
+        SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2D,0x10);			/* 740/301LV, 301BDH */
      }
   } else {
      if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {					/* 650/LVDS */
@@ -1669,10 +1750,7 @@
   tempcx = SiS_Pr->SiS_HT;
 
   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
-        if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768)       tempbx = 1024;
-	else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) tempbx = 1400;
-	else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) tempbx = 1600;
-	else 							      tempbx = 1280;
+        tempbx = SiS_Pr->PanelXRes;
   }
   tempcx -= tempbx;                        	            	/* HT-HDE  */
   push1 = tempax;
@@ -1684,7 +1762,7 @@
 
   tempcx >>= 2;
 
-  /* TW: 650/30xLV 1.10.6s, 740/LVDS */
+  /* 650/30xLV 1.10.6s, 740/LVDS */
   if( ((SiS_Pr->SiS_IF_DEF_LVDS == 0) && (SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)) ||
       ((SiS_Pr->SiS_IF_DEF_LVDS == 1) && (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD)) ) {
      if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480) {
@@ -1748,13 +1826,10 @@
   tempbx = SiS_Pr->SiS_LCDVDES;                                	/* VGAVDES  */
   push1 = tempbx;                                      		
   if(SiS_Pr->SiS_IF_DEF_TRUMPION == 0) {
-    if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768)        tempax = 768;
-    else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768)   tempax = 768;
-    else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024)  tempax = 1024;
-    else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050)  tempax = 1050;
-    else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200)  tempax = 1200;
-    else                                                           tempax = 960;
-  } else tempax = SiS_Pr->SiS_VGAVDE;  /* Trumpion */
+     tempax = SiS_Pr->PanelYRes;
+  } else {
+     tempax = SiS_Pr->SiS_VGAVDE;
+  }
 
   tempbx += tempax;
   tempax = SiS_Pr->SiS_VT;                                    	/* VT  */
@@ -1764,7 +1839,7 @@
  
   tempcx >>= 2;	
 
-  /* TW: 650/30xLV 1.10.6s, 740/LVDS */
+  /* 650/30xLV 1.10.6s, 740/LVDS */
   if( ((SiS_Pr->SiS_IF_DEF_LVDS == 0) && (SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)) ||
       ((SiS_Pr->SiS_IF_DEF_LVDS == 1) && (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD)) ) {
      if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480) {
@@ -1806,7 +1881,7 @@
   if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
      SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x19,0xF0,temp);
   } else {
-     /* TW: 650/30xLV 1.10.6s, Xabre */
+     /* 650/30xLV 1.10.6s, Xabre */
      temp |= 0xC0;
      SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x19,0xF0,temp);             /* Part1_19h  */
   }
@@ -1823,7 +1898,7 @@
      }
   } else {
      if(IS_SIS650) {
-        /* TW: 650/30xLV 1.10.6s */
+        /* 650/30xLV 1.10.6s */
         if(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit) {
            if(SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x00) & 0x01) temp |= 0x80;
         }
@@ -1839,7 +1914,7 @@
 
   if(!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600) {
-      if(resinfo == 7) tempcx++;
+      if(resinfo == SIS_RI_800x600) tempcx++;
     }
   }
   if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480) {
@@ -1925,18 +2000,17 @@
   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x23,temp);
 
 #if 0
-  /* TW: Missing code (calles int 2f) (650/302LV 1.10.6s; 1.10.7w doesn't do this) */
+  /* Missing code (calles int 2f) (650/302LV 1.10.6s; 1.10.7w doesn't do this) */
   if(xxx()) {
       SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0e,0xda);
   }
 #endif
 
-  /* TW: Only for LVDS and 301LV/302LV */
+  /* Only for LVDS and 301LV/302LV */
   if((SiS_Pr->SiS_IF_DEF_LVDS == 1) || (SiS_Pr->SiS_VBInfo & VB_SIS301LV302LV)){
-  	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1e,0x20);
+     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1e,0x20);
   }
 
-  return;
 }
 #endif  /* SIS 315 */
 
@@ -1951,10 +2025,10 @@
 
   offset = SiS_GetOffset(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
                          HwDeviceExtension);
-#if 0
-  if(SiS_Pr->LCDResInfo == 13) offset >>= 1;
-  if(SiS_Pr->LCDResInfo == 12) offset >>= 1;
-#endif			 
+
+  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_2 ||
+     SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_3) offset >>= 1;
+
   temp = (UCHAR)(offset & 0xFF);
   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x07,temp);
   temp = (UCHAR)((offset & 0xFF00) >> 8);
@@ -1976,13 +2050,7 @@
   } else {
      infoflag = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
      modeinfo = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeInfo;
-  
-     if(HwDeviceExtension->jChipType < SIS_315H ) {
-    	index = (modeinfo >> 4) & 0xFF;
-     } else {
-    	index = (modeinfo >> 8) & 0xFF;
-     }
-
+     index = (modeinfo >> 8) & 0xFF;
      temp = SiS_Pr->SiS_ScreenOffset[index];
   }
   
@@ -1992,9 +2060,8 @@
 
   temp *= colordepth;
 
-  /* TW: For 1400x1050 and 856x480 */
-  if( ( ((ModeNo >= 0x26) && (ModeNo <= 0x28)) || 
-        ModeNo == 0x3f || 
+  if( ( ((ModeNo >= 0x26) && (ModeNo <= 0x28)) ||
+        ModeNo == 0x3f ||
 	ModeNo == 0x42 || 
 	ModeNo == 0x45 ) ||
       (SiS_Pr->UseCustomMode && (SiS_Pr->CHDisplay % 16)) ) {
@@ -2012,7 +2079,8 @@
   SHORT  index;
   USHORT modeflag;
 
-  if(SiS_Pr->UseCustomMode) {
+  /* Do NOT check UseCustomMode, will skrew up FIFO */
+  if(ModeNo == 0xfe) {
      modeflag = SiS_Pr->CModeFlag;
   } else {
      if(ModeNo <= 0x13)
@@ -2035,7 +2103,11 @@
   flag = 0;
   tempbl = 0xC0;
 
-  infoflag = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
+  if(SiS_Pr->UseCustomMode) {
+     infoflag = SiS_Pr->CInfoFlag;
+  } else {
+     infoflag = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
+  }
 
   if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {					/* LVDS */
 
@@ -2044,12 +2116,19 @@
     } else if((SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) && (SiS_Pr->SiS_LCDInfo & LCDSync)) {
        tempah = SiS_Pr->SiS_LCDInfo;
     } else tempah = infoflag >> 8;
-    
+
     tempah &= 0xC0;
-    
+
     tempah |= 0x20;
     if(!(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit)) tempah |= 0x10;
 
+    if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
+       if((SiS_Pr->SiS_CustomT == CUT_BARCO1366) ||
+          (SiS_Pr->SiS_CustomT == CUT_BARCO1024)) {
+	  tempah |= 0xc0;
+       }
+    }
+
     if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
        if(HwDeviceExtension->jChipType >= SIS_315H) {
           tempah >>= 3;
@@ -2079,9 +2158,11 @@
             tempah |= 0x20;
             if(!(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit)) tempah |= 0x10;
 
+#if 0
             if (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480) {
-	       	/* TW: BIOS does something here @@@ */
+	       	/* BIOS does something here @@@ */
             }
+#endif
 
  	    tempah &= 0x3f;
   	    tempah |= tempbl;
@@ -2089,16 +2170,11 @@
 
          } else {							/* 630 - 301 */
 
-            if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
-               tempah = SiS_Pr->SiS_LCDInfo;
-	       if(SiS_Pr->SiS_LCDInfo & DontExpandLCDShift) { /* ! */
-	          flag = 1;
-	       }
-            }
-            if(flag != 1) tempah = infoflag >> 8;
+            tempah = infoflag >> 8;
             tempah &= 0xC0;
-            tempah |= 0x30;
-            SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x19,0x3F,tempah);
+            tempah |= 0x20;
+	    if(!(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit)) tempah |= 0x10;
+            SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x19,0x0F,tempah);
 
          }
 
@@ -2106,9 +2182,9 @@
 
       } else {
 
-#ifdef SIS315H  /* ----- 310/325 series ---- */
+#ifdef SIS315H  /* ------- 315 series ------ */
 
-         if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {	  		/* 310/325 - 30xLV */
+         if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {	  		/* 315 - 30xLV */
 
             tempah = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x37);
             tempah &= 0xC0;
@@ -2116,24 +2192,33 @@
             if(!(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit)) tempah |= 0x10;
             SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x19,0x0F,tempah);
 
-         } else {							/* 310/325 - 301, 301B */
+         } else {							/* 315 - 301, 301B */
 
             tempah = infoflag >> 8;
             tempah &= 0xC0;
-	    if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
-	       if(SiS_Pr->SiS_LCDInfo & LCDSync) {
-	          tempah = SiS_Pr->SiS_LCDInfo;
-	          tempah &= 0xC0;
+	    if(!SiS_Pr->UseCustomMode) {
+	       if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
+	          if(SiS_Pr->SiS_LCDInfo & LCDSync) {
+	             tempah = SiS_Pr->SiS_LCDInfo;
+	             tempah &= 0xC0;
+	          }
 	       }
 	    }
 	    
             tempah |= 0x20;
             if(!(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit)) tempah |= 0x10;
+	    
 #if 0
             if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480) {
-		/* TW: BIOS does something here @@@ */
+		/* BIOS does something here @@@ */
             }
-#endif	    
+#endif
+
+	    if(SiS_Pr->SiS_VBType & VB_NoLCD) {			/* TEST, imitate BIOS bug */
+	       if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+	          tempah |= 0xc0;
+	       }
+	    }
             SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x19,0x0F,tempah);
 
          } 
@@ -2143,7 +2228,7 @@
    }
 }
 
-/* TW: Set CRT2 FIFO on 300/630/730 */
+/* Set CRT2 FIFO on 300/630/730 */
 #ifdef SIS300
 void
 SiS_SetCRT2FIFO_300(SiS_Private *SiS_Pr, UCHAR *ROMAddr,USHORT ModeNo,
@@ -2197,7 +2282,7 @@
 
   if(!SiS_Pr->CRT1UsesCustomMode) {
   
-     CRT1ModeNo = SiS_Pr->SiS_CRT1Mode;                                 /* get CRT1 ModeNo */
+     CRT1ModeNo = SiS_Pr->SiS_CRT1Mode;                                 	/* get CRT1 ModeNo */
      SiS_SearchModeID(SiS_Pr,ROMAddr,&CRT1ModeNo,&modeidindex);
      SiS_Pr->SiS_SetFlag &= (~ProgrammingCRT2);
      SiS_Pr->SiS_SelectCRT2Rate = 0;
@@ -2205,20 +2290,32 @@
 						modeidindex,HwDeviceExtension);
 
      if(CRT1ModeNo >= 0x13) {
-       index = SiS_Pr->SiS_RefIndex[refreshratetableindex].Ext_CRTVCLK;
-       index &= 0x3F;
-       VCLK = SiS_Pr->SiS_VCLKData[index].CLOCK;			/* Get VCLK */
-       data2 = SiS_Pr->SiS_ModeType - 2;
+        index = SiS_Pr->SiS_RefIndex[refreshratetableindex].Ext_CRTVCLK;
+        index &= 0x3F;
+        VCLK = SiS_Pr->SiS_VCLKData[index].CLOCK;				/* Get VCLK */
+
+	colorth = SiS_GetColorDepth(SiS_Pr,ROMAddr,CRT1ModeNo,modeidindex); 	/* Get colordepth */
+        colorth >>= 1;
+        if(!colorth) colorth++;
      }
-     
+
   } else {
   
      CRT1ModeNo = 0xfe;
-     VCLK = SiS_Pr->CSRClock;						/* Get VCLK */
-     data2 = (SiS_Pr->CModeFlag & ModeInfoFlag) - 2;
-  
-  }			
-     
+     VCLK = SiS_Pr->CSRClock_CRT1;						/* Get VCLK */
+     data2 = (SiS_Pr->CModeFlag_CRT1 & ModeInfoFlag) - 2;
+     switch(data2) {								/* Get color depth */
+        case 0 : colorth = 1; break;
+        case 1 : colorth = 1; break;
+        case 2 : colorth = 2; break;
+        case 3 : colorth = 2; break;
+        case 4 : colorth = 3; break;
+        case 5 : colorth = 4; break;
+        default: colorth = 2;
+     }
+
+  }
+
   if(CRT1ModeNo >= 0x13) {
     if(HwDeviceExtension->jChipType == SIS_300) {
        index = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x3A);
@@ -2227,22 +2324,8 @@
     }
     index &= 0x07;
     MCLK = SiS_Pr->SiS_MCLKData_0[index].CLOCK;				/* Get MCLK */
-    
-#ifdef TWDEBUG
-    xf86DrvMsg(0, X_INFO, "FIFO2: CRT1Mode 0x%x VCLK %d MCLK %d modetype-2 = %d\n",
-    	CRT1ModeNo, VCLK, MCLK, data2);
-#endif  
-  
-    switch(data2) {							/* Get color depth */
-      case 0 : 	colorth = 1; break;
-      case 1 : 	colorth = 1; break;
-      case 2 : 	colorth = 2; break;
-      case 3 : 	colorth = 2; break;
-      case 4 : 	colorth = 3; break;
-      case 5 : 	colorth = 4; break;
-      default:  colorth = 2; break;
-    }
-    data2 = (colorth * VCLK) / MCLK;  
+
+    data2 = (colorth * VCLK) / MCLK;
 
     temp = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x14);
     temp = ((temp & 0x00FF) >> 6) << 1;
@@ -2252,22 +2335,12 @@
 
     data2 = temp - data2;
     
-#ifdef TWDEBUG
-    xf86DrvMsg(0, X_INFO, "FIFO2: data2 (step1) = %d\n",
-    	data2);
-#endif    
-
     if((28 * 16) % data2) {
       	data2 = (28 * 16) / data2;
       	data2++;
     } else {
       	data2 = (28 * 16) / data2;
     }
-    
-#ifdef TWDEBUG
-    xf86DrvMsg(0, X_INFO, "FIFO2: data2 (step2) = %d\n",
-    	data2);
-#endif
 
     if(HwDeviceExtension->jChipType == SIS_300) {
 
@@ -2313,10 +2386,6 @@
        temp &= 0x0F;   
        tempal |= temp;
 
-#ifdef TWDEBUG
-       xf86DrvMsg(0, X_INFO, "FIFO2: Latencyfactorindex = 0x%x\n", tempal);
-#endif
-      
        tempbx = tempal;   /* BIOS BUG (2.04.5d, 2.04.6a use ah here, which is unset!) */
        tempbx = 0;        /* -- do it like the BIOS anyway... */
        tempax = tempbx;
@@ -2341,7 +2410,7 @@
        SiS_SetReg4(0xcf8,0x800000A0);
        eax = SiS_GetReg3(0xcfc);
 #else
-       /* TW: We use pci functions X offers. We use tag 0, because
+       /* We use pci functions X offers. We use tag 0, because
         * we want to read/write to the host bridge (which is always
         * 00:00.0 on 630, 730 and 540), not the VGA device.
         */
@@ -2368,43 +2437,41 @@
        if(!(temp & 0x80)) data += 5;
     }
     
-#ifdef TWDEBUG    
-    xf86DrvMsg(0, X_INFO, "FIFO2: latencyfactor (CRT1) = %d\n", data);
-#endif
-
     data += data2;				/* CRT1 Request Period */
     
-#ifdef TWDEBUG    
-    xf86DrvMsg(0, X_INFO, "FIFO2: CRT1 request period = %d\n", data);
-#endif
-
-    CRT2ModeNo = ModeNo;
     SiS_Pr->SiS_SetFlag |= ProgrammingCRT2;
     SiS_Pr->SiS_SelectCRT2Rate = SelectRate_backup;
-    SiS_SearchModeID(SiS_Pr,ROMAddr,&CRT2ModeNo,&modeidindex);    
 
-    refreshratetableindex = SiS_GetRatePtrCRT2(SiS_Pr,ROMAddr,CRT2ModeNo,
-                                               modeidindex,HwDeviceExtension);
+    if(!SiS_Pr->UseCustomMode) {
+
+       CRT2ModeNo = ModeNo;
+       SiS_SearchModeID(SiS_Pr,ROMAddr,&CRT2ModeNo,&modeidindex);
+
+       refreshratetableindex = SiS_GetRatePtrCRT2(SiS_Pr,ROMAddr,CRT2ModeNo,
+                                                  modeidindex,HwDeviceExtension);
+
+       index = SiS_GetVCLK2Ptr(SiS_Pr,ROMAddr,CRT2ModeNo,modeidindex,
+                               refreshratetableindex,HwDeviceExtension);
+       VCLK = SiS_Pr->SiS_VCLKData[index].CLOCK;                         	/* Get VCLK  */
+
+       if((SiS_Pr->SiS_CustomT == CUT_BARCO1366) || (SiS_Pr->SiS_CustomT == CUT_BARCO1024)) {
+          if((ROMAddr) && SiS_Pr->SiS_UseROM) {
+	     if(ROMAddr[0x220] & 0x01) {
+                VCLK = ROMAddr[0x229] | (ROMAddr[0x22a] << 8);
+	     }
+          }
+       }
+
+    } else {
+
+       CRT2ModeNo = 0xfe;
+       VCLK = SiS_Pr->CSRClock;							/* Get VCLK */
 
-    index = SiS_GetVCLK2Ptr(SiS_Pr,ROMAddr,CRT2ModeNo,modeidindex,
-                            refreshratetableindex,HwDeviceExtension);
-    VCLK = SiS_Pr->SiS_VCLKData[index].CLOCK;                         	/* Get VCLK  */
-    
-    data2 = SiS_Pr->SiS_ModeType - 2;
-    switch(data2) {							/* Get color depth */
-      case 0 : 	colorth = 1; break;
-      case 1 : 	colorth = 1; break;
-      case 2 : 	colorth = 2; break;
-      case 3 : 	colorth = 2; break;
-      case 4 : 	colorth = 3; break;
-      case 5 : 	colorth = 4; break;
-      default:  colorth = 2; break;
     }
-    
-#ifdef TWDEBUG    
-    xf86DrvMsg(0, X_INFO, "FIFO2: CRT2Mode 0x%x VCLK %d MCLK %d modetype-2 = %d, colorth %d\n",
-    	CRT2ModeNo, VCLK, MCLK, data2, colorth);
-#endif
+
+    colorth = SiS_GetColorDepth(SiS_Pr,ROMAddr,CRT2ModeNo,modeidindex);   	/* Get colordepth */
+    colorth >>= 1;
+    if(!colorth) colorth++;
 
     data = data * VCLK * colorth;
     if(data % (MCLK << 4)) {
@@ -2414,10 +2481,6 @@
       	data = data / (MCLK << 4);
     }
     
-#ifdef TWDEBUG    
-    xf86DrvMsg(0, X_INFO, "FIFO2: data (unclipped) = 0x%x\n", data);
-#endif    
-    
     if(data <= 6) data = 6;
     if(data > 0x14) data = 0x14;
 
@@ -2456,13 +2519,13 @@
 }
 #endif
 
-/* TW: Set FIFO on 310/325/330 series */
+/* Set FIFO on 315/330 series */
 #ifdef SIS315H
 void
 SiS_SetCRT2FIFO_310(SiS_Private *SiS_Pr, UCHAR *ROMAddr,USHORT ModeNo,
                     PSIS_HW_DEVICE_INFO HwDeviceExtension)
 {
-
+#if 0   /* This code is obsolete */
   UCHAR CombCode[]  = { 1, 1, 1, 4, 3, 1, 3, 4,
                         4, 1, 4, 4, 5, 1, 5, 4};
   UCHAR CRT2ThLow[] = { 39, 63, 55, 79, 78,102, 90,114,
@@ -2474,11 +2537,13 @@
   USHORT ModeIdIndex;
   USHORT RefreshRateTableIndex;
   USHORT SelectRate_backup;
-  
+
   SelectRate_backup = SiS_Pr->SiS_SelectCRT2Rate;
-  
+#endif
+
   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x01,0x3B);
 
+#if 0
   if(!SiS_Pr->CRT1UsesCustomMode) {
   
      CRT1ModeNo = SiS_Pr->SiS_CRT1Mode;                                 /* get CRT1 ModeNo */
@@ -2489,28 +2554,27 @@
 
      /* Get REFIndex for crt1 refreshrate */
      RefreshRateTableIndex = SiS_GetRatePtrCRT2(SiS_Pr,ROMAddr,CRT1ModeNo,
-                                             ModeIdIndex,HwDeviceExtension);
+                                                ModeIdIndex,HwDeviceExtension);
+     index = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
+     tempax = SiS_Pr->SiS_VCLKData[index].CLOCK;			/* Get VCLK */
 
-     index = SiS_GetVCLK2Ptr(SiS_Pr,ROMAddr,CRT1ModeNo,ModeIdIndex,
-                          RefreshRateTableIndex,HwDeviceExtension);
-     tempax = SiS_Pr->SiS_VCLKData[index].CLOCK;                        /* Get VCLK */
-     
      tempbx = SiS_GetColorDepth(SiS_Pr,ROMAddr,CRT1ModeNo,ModeIdIndex); /* Get colordepth */
      tempbx >>= 1;
-     if(!tempbx) tempbx++; 
-     
+     if(!tempbx) tempbx++;
+
   } else {
-  
-     tempax = SiS_Pr->CSRClock;						/* Get VCLK */
-     tempbx = (SiS_Pr->CModeFlag & ModeInfoFlag) - 2;
+
+     CRT1ModeNo = 0xfe;
+     tempax = SiS_Pr->CSRClock_CRT1;					/* Get VCLK */
+     tempbx = (SiS_Pr->CModeFlag_CRT1 & ModeInfoFlag) - 2;
      switch(tempbx) {							/* Get color depth */
-       case 0 : 	tempbx = 1; break;
-       case 1 : 	tempbx = 1; break;
-       case 2 : 	tempbx = 2; break;
-       case 3 : 	tempbx = 2; break;
-       case 4 : 	tempbx = 3; break;
-       case 5 : 	tempbx = 4; break;
-       default:  	tempbx = 2; break;
+       case 0 : tempbx = 1; break;
+       case 1 : tempbx = 1; break;
+       case 2 : tempbx = 2; break;
+       case 3 : tempbx = 2; break;
+       case 4 : tempbx = 3; break;
+       case 5 : tempbx = 4; break;
+       default: tempbx = 2;
      }
   
   }
@@ -2523,13 +2587,6 @@
 
   tempbx = tempax;
 
-#if 0 /* TW: BIOS code is skrewed */
-  if(SiS_GetReg1(SiS_Pr->SiS_P3c4,0x14) & 0x02) {
-   	tempax = 16;
-  } else {
-    	tempax = 8;
-  }
-#endif
   tempax = 16;
 
   tempax -= tempbx;
@@ -2556,24 +2613,33 @@
 
   tempcx +=  temp3;                                      /* CRT1 Request Period */
 
-  CRT2ModeNo = ModeNo;                                                 /* get CRT2 ModeNo */
-  SiS_SearchModeID(SiS_Pr,ROMAddr,&CRT2ModeNo,&ModeIdIndex);           /* Get ModeID Table */
-
   SiS_Pr->SiS_SetFlag |= ProgrammingCRT2;
   SiS_Pr->SiS_SelectCRT2Rate = SelectRate_backup;
 
-  RefreshRateTableIndex = SiS_GetRatePtrCRT2(SiS_Pr,ROMAddr,CRT2ModeNo,
-                                           ModeIdIndex,HwDeviceExtension);
+  if(!SiS_Pr->UseCustomMode) {
+
+     CRT2ModeNo = ModeNo;                                                 /* get CRT2 ModeNo */
+     SiS_SearchModeID(SiS_Pr,ROMAddr,&CRT2ModeNo,&ModeIdIndex);
+
+     RefreshRateTableIndex = SiS_GetRatePtrCRT2(SiS_Pr,ROMAddr,CRT2ModeNo,
+                                                ModeIdIndex,HwDeviceExtension);
+
+     index = SiS_GetVCLK2Ptr(SiS_Pr,ROMAddr,CRT2ModeNo,ModeIdIndex,
+                             RefreshRateTableIndex,HwDeviceExtension);
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
+        tempax = SiS_Pr->SiS_VCLKData[index].CLOCK;                       /* Get VCLK  */
+     } else {
+        tempax = SiS_Pr->SiS_VBVCLKData[index].CLOCK;
+     }
 
-  index = SiS_GetVCLK2Ptr(SiS_Pr,ROMAddr,CRT2ModeNo,ModeIdIndex,
-                          RefreshRateTableIndex,HwDeviceExtension);
-  if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
-     tempax = SiS_Pr->SiS_VCLKData[index].CLOCK;                       /* Get VCLK  */
   } else {
-     tempax = SiS_Pr->SiS_VBVCLKData[index].CLOCK;                     /* Get VCLK  */
+
+     CRT2ModeNo = 0xfe;							  /* Get VCLK  */
+     tempax = SiS_Pr->CSRClock;
+
   }
 
-  tempbx = SiS_GetColorDepth(SiS_Pr,ROMAddr,CRT2ModeNo,ModeIdIndex);   /* Get colordepth */
+  tempbx = SiS_GetColorDepth(SiS_Pr,ROMAddr,CRT2ModeNo,ModeIdIndex);   	  /* Get colordepth */
   tempbx >>= 1;
   if(!tempbx) tempbx++;
 
@@ -2590,12 +2656,16 @@
 
   if (tempax > 0x37)  tempax = 0x37;
 
-  /* TW: 650/LVDS (1.10.07, 1.10.00), 650/301LV, 740, 330 overrule calculated value; 315 does not */
+  /* 650/LVDS, 650/301LV, 740, 330 overrule calculated value; 315 does not */
   if(HwDeviceExtension->jChipType >= SIS_650) {
   	tempax = 0x04;
   }
-  
   SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x02,~0x3F,tempax);
+#else
+
+  SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x02,~0x3F,0x04);
+
+#endif
 }
 
 USHORT
@@ -2611,9 +2681,10 @@
     return(SiS_Pr->SiS_MCLKData_0[index].CLOCK);
   }
 }
+
 #endif
 
-/* TW: Checked against 650/LVDS 1.10.07 BIOS */
+/* Checked against 650/LVDS 1.10.07 BIOS */
 void
 SiS_GetLVDSDesData(SiS_Private *SiS_Pr, UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex,
                    USHORT RefreshRateTableIndex,
@@ -2623,6 +2694,13 @@
   USHORT PanelIndex,ResIndex;
   const  SiS_LVDSDesStruct *PanelDesPtr = NULL;
 
+  if((SiS_Pr->UseCustomMode) ||
+     (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_PanelCustom)) {
+     SiS_Pr->SiS_LCDHDES = 0;
+     SiS_Pr->SiS_LCDVDES = 0;
+     return;
+  }
+
   if((SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) && (SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) ) {
 
 #ifdef SIS315H  
@@ -2689,11 +2767,11 @@
      	case 52: PanelDesPtr = SiS_Pr->SiS_CHTVUPALDesData;    break;
      	case 53: PanelDesPtr = SiS_Pr->SiS_CHTVOPALDesData;    break;
 	default:
-		if(HwDeviceExtension->jChipType < SIS_315H)
-		   PanelDesPtr = SiS_Pr->SiS_PanelType0e_1;
-		else
-		   PanelDesPtr = SiS_Pr->SiS_PanelType01_1;
-		break;
+		 if(HwDeviceExtension->jChipType < SIS_315H)
+		    PanelDesPtr = SiS_Pr->SiS_PanelType0e_1;
+		 else
+		    PanelDesPtr = SiS_Pr->SiS_PanelType01_1;
+		 break;
      }
   }
   SiS_Pr->SiS_LCDHDES = (PanelDesPtr+ResIndex)->LCDHDES;
@@ -2710,7 +2788,7 @@
      } else {
         if(!(SiS_Pr->SiS_SetFlag & SetDOSMode)) {
            if( (HwDeviceExtension->jChipType < SIS_315H) || 
-	       (SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1280x1024) ) {  
+	       (SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1280x1024) ) {
               if(SiS_Pr->SiS_LCDResInfo >= SiS_Pr->SiS_Panel1024x768){
                  if(ModeNo <= 0x13) {
 	            modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
@@ -2737,7 +2815,7 @@
         }
      }
   }
-  return;
+
 }
 
 void
@@ -2757,11 +2835,11 @@
 
   tempbx = 0;
   if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
-     if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD)) {
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
         tempbx = 50;
         if((SiS_Pr->SiS_VBInfo & SetPALTV) && (!SiS_Pr->SiS_CHPALM)) tempbx += 2;
         if(SiS_Pr->SiS_VBInfo & SetCHTVOverScan) tempbx += 1;
-        /* TW: Nothing special needed for SOverscan    */
+        /* Nothing special needed for SOverscan    */
         /*     PALM uses NTSC data, PALN uses PAL data */
      }
   }
@@ -2773,7 +2851,7 @@
         if(modeflag & HalfDCLK) tempbx++;
      }
   }
-  /* TW: 630/LVDS and 650/LVDS (1.10.07) BIOS */
+  /* 630/LVDS and 650/LVDS (1.10.07) BIOS */
   if(SiS_Pr->SiS_SetFlag & SetDOSMode) {
      if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480)  {
         tempal = 0x07;
@@ -2833,14 +2911,13 @@
      }
   }
   
-  /* TW: BIOS does not do this (neither 301 nor LVDS) */
+  /* BIOS does not do this (neither 301 nor LVDS) */
   /*     (But it's harmless; see SetCRT2Offset) */
   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x03,0x00);   /* fix write part1 index 0  BTDRAM bit Bug */
 
-  /* TW: Removed 301B302B301LV302LV check here to match 650/LVDS BIOS */
   if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
 
-	/* TW:   1. for LVDS/302B/302LV **LCDA** */
+	/*   1. for LVDS/302B/302LV **LCDA** */
 
       SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x00,0xAF,0x40); /* FUNCTION CONTROL */
       SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2E,0xF7);
@@ -2855,23 +2932,23 @@
 
 #ifdef SIS300    /* ---- 300 series ---- */
 
-      /* For 301BDH: */
+      /* For 301BDH: (with LCD via LVDS) */
       if(SiS_Pr->SiS_VBType & VB_NoLCD) {
-	  temp = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x32);
-	  temp &= 0xef;
-	  temp |= 0x02;
-	  if((SiS_Pr->SiS_VBInfo & SetCRT2ToTV) || (SiS_Pr->SiS_VBInfo & SetCRT2ToRAMDAC)) {
-	     temp |= 0x10;
-	     temp &= 0xfd;
-	  }
-	  SiS_SetReg1(SiS_Pr->SiS_P3c4,0x32,temp);
+	 temp = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x32);
+	 temp &= 0xef;
+	 temp |= 0x02;
+	 if((SiS_Pr->SiS_VBInfo & SetCRT2ToTV) || (SiS_Pr->SiS_VBInfo & SetCRT2ToRAMDAC)) {
+	    temp |= 0x10;
+	    temp &= 0xfd;
+	 }
+	 SiS_SetReg1(SiS_Pr->SiS_P3c4,0x32,temp);
       }
 
       if(ModeNo > 0x13) {
-        tempcl -= ModeVGA;
-        if((tempcl > 0) || (tempcl == 0)) {      /* TW: tempcl is USHORT -> always true! */
-           tempah = ((0x10 >> tempcl) | 0x80);
-        }
+         tempcl -= ModeVGA;
+         if((tempcl > 0) || (tempcl == 0)) {      /* tempcl is USHORT -> always true! */
+            tempah = ((0x10 >> tempcl) | 0x80);
+         }
       } else tempah = 0x80;
 
       if(SiS_Pr->SiS_VBInfo & SetInSlaveMode)  tempah ^= 0xA0;
@@ -2880,7 +2957,7 @@
 
     } else {
 
-#ifdef SIS315H    /* ---- 310/325/330 series ---- */
+#ifdef SIS315H    /* ------- 315/330 series ------ */
 
       if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
          if(SiS_Pr->SiS_VBInfo & CRT2DisplayFlag) {
@@ -2889,12 +2966,12 @@
       }
 
       if(ModeNo > 0x13) {
-        tempcl -= ModeVGA;
-        if((tempcl > 0) || (tempcl == 0)) {  /* TW: tempcl is USHORT -> always true! */
-           tempah = (0x08 >> tempcl);
-           if (tempah == 0) tempah = 1;
-           tempah |= 0x40;
-        }
+         tempcl -= ModeVGA;
+         if((tempcl > 0) || (tempcl == 0)) {  /* tempcl is USHORT -> always true! */
+            tempah = (0x08 >> tempcl);
+            if (tempah == 0) tempah = 1;
+            tempah |= 0x40;
+         }
       } else tempah = 0x40;
 
       if(SiS_Pr->SiS_VBInfo & SetInSlaveMode)  tempah ^= 0x50;
@@ -2921,7 +2998,7 @@
 
     if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
 
-	/* TW:   2. for 301 (301B, 302B 301LV, 302LV non-LCDA) */
+	/*   2. for 301 (301B, 302B 301LV, 302LV non-LCDA) */
 
     	tempah = 0x01;
     	if(!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
@@ -2938,7 +3015,7 @@
 
     	if(HwDeviceExtension->jChipType < SIS_315H) {
 
-		/* --- 300 series --- */
+		/* ---- 300 series ---- */
 
       		tempah = (tempah << 5) & 0xFF;
       		SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x01,tempah);
@@ -2946,7 +3023,7 @@
 
     	} else {
 
-		/* --- 310 series --- */
+		/* ---- 315 series ---- */
 
       		SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2E,0xF8,tempah);
 
@@ -2956,9 +3033,9 @@
       		tempah |= 0x10;
 	}
 
-	/* TW: 630/301 BIOS */
 	if((HwDeviceExtension->jChipType < SIS_315H) && (SiS_Pr->SiS_VBType & VB_SIS301)) {
-		if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
+		if((SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) ||
+		   (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x960)) {
 			tempah |= 0x80;
 		}
 	} else {
@@ -2996,28 +3073,31 @@
       	      }
 	   }
     	}
-	/* TW: For 302LV dual-channel */
+
+	/* For 302LV dual-channel */
 	if(HwDeviceExtension->jChipType >= SIS_315H) {
-	    if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-	        if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x39) & 0x04)
-		    tempah |= 0x40;
-	    }
+	   if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
+	      if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x39) & 0x04)
+	         tempah |= 0x40;
+	   }
 	}
 
 	if((SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) ||
-	   (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x960)) {
-		tempah |= 0x80;
+	   (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x960)  ||
+	   ((SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_PanelCustom) &&
+	    (SiS_Pr->CP_MaxX >= 1280) && (SiS_Pr->CP_MaxY >= 960))) {
+	   tempah |= 0x80;
 	}
 
     	SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x0C,tempah);
 
     } else {
 
-    	/* TW: 3. for LVDS */
+    	/* 3. for LVDS */
 
 	if(HwDeviceExtension->jChipType >= SIS_315H) {
 
-	   /* TW: Inserted this entire section (BIOS 650/LVDS); added ModeType check
+	   /* Inserted this entire section (BIOS 650/LVDS); added ModeType check
 	    *     (LVDS can only be slave in 8bpp modes)
 	    */
 	   tempah = 0x80;
@@ -3043,7 +3123,7 @@
 
 	} else {
 
-	   /* TW: (added ModeType check) */
+	   /* (added ModeType check) */
 	   tempah = 0;
 	   if( (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) && (SiS_Pr->SiS_ModeType > ModeVGA) ) {
                	  tempah |= 0x02;
@@ -3060,50 +3140,69 @@
 
   }
 
-  /* TW: Inserted the entire following section */
+  /* Inserted the entire following section */
 
   if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
 
       if(HwDeviceExtension->jChipType >= SIS_315H) {
 
 #ifdef SIS315H
+
+         unsigned char bridgerev = SiS_GetReg1(SiS_Pr->SiS_Part4Port,0x01);;
+
+	 /* The following is nearly unpreditable and varies from machine
+	  * to machine. Especially the 301DH seems to be a real trouble
+	  * maker. Some BIOSes simply set the registers (like in the
+	  * NoLCD-if-statements here), some set them according to the
+	  * LCDA stuff. It is very likely that some machines are not
+	  * treated correctly in the following, very case-orientated
+	  * code. What do I do then...?
+	  */
+
+	 /* 740 variants match for 30xB, 301B-DH, 30xLV */
+
          if(!(IS_SIS740)) {
             tempah = 0x04;						   /* For all bridges */
             tempbl = 0xfb;
             if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)) {
                tempah = 0x00;
-	       if(SiS_IsDualEdge(SiS_Pr, HwDeviceExtension, BaseAddr))
+	       if(SiS_IsDualEdge(SiS_Pr, HwDeviceExtension, BaseAddr)) {
 	          tempbl = 0xff;
+	       }
             }
             SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x13,tempbl,tempah);   
 	 }
-	 
-	 if(IS_SIS740) {						
+
+	 /* The following two are responsible for eventually wrong colors
+	  * in TV output. The DH (VB_NoLCD) conditions are unknown; the
+	  * b0 was found in some 651 machine (Pim); the b1 version in a
+	  * 650 box (Jake). What is the criteria?
+	  */
+
+	 if(IS_SIS740) {
 	    tempah = 0x30;
 	    tempbl = 0xcf;
 	    if(SiS_Pr->SiS_VBInfo & DisableCRT2Display) {
 	       tempah = 0x00;
 	    }
-	    SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2c,tempbl,tempah);    
+	    SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2c,tempbl,tempah);
+	 } else if(SiS_Pr->SiS_VBType & VB_SIS301) {
+	    /* Fixes "TV-blue-bug" on 315+301 */
+	    SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2c,0xCF);          /* For 301   */
+	 } else if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
+	    SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2c,0xCF,0x30);   /* For 30xLV */
+	 } else if((SiS_Pr->SiS_VBType & VB_NoLCD) && (bridgerev == 0xb0)) {
+	    SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2c,0xCF,0x30);   /* For 30xB-DH rev b0 (or "DH on 651"?) */
 	 } else {
-	    /* TW: This in order to fix "TV-blue-bug" on 315+301 */
-            if(SiS_Pr->SiS_VBType & VB_SIS301) {
-	       SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2c,0xCF);             /* For 301   */
-	    } else {
-	       if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
-	          SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2c,0xCF,0x30);   /* For 30xLV */
-	       } else {
-	          tempah = 0x30;					   /* For 301B  */
-	          tempbl = 0xcf;
-	          if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)) {
-	             tempah = 0x00;
-		     if(SiS_IsDualEdge(SiS_Pr, HwDeviceExtension, BaseAddr)) {
-		        tempbl = 0xff;
-		     }
-	          }
-	          SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2c,tempbl,tempah);    
+	    tempah = 0x30;					     /* For 30xB (and 301BDH rev b1) */
+	    tempbl = 0xcf;
+	    if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)) {
+	       tempah = 0x00;
+	       if(SiS_IsDualEdge(SiS_Pr, HwDeviceExtension, BaseAddr)) {
+		  tempbl = 0xff;
 	       }
 	    }
+	    SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2c,tempbl,tempah);
 	 }
 
 	 if(IS_SIS740) {
@@ -3111,25 +3210,25 @@
   	    tempbl = 0x3f;
 	    if(SiS_Pr->SiS_VBInfo & DisableCRT2Display) {
 	       tempah = 0x00;
-	    } 
-	    SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x21,tempbl,tempah);     
+	    }
+	    SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x21,tempbl,tempah);
+	 } else if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
+	    SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x21,0x3f,0xc0);	/* For 30xLV */
+	 } else if((SiS_Pr->SiS_VBType & VB_NoLCD) && (bridgerev == 0xb0)) {
+	    SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x21,0x3f,0xc0);	/* For 30xB-DH rev b0 (or "DH on 651"? */
 	 } else {
-	    if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {			/* For 30xLV */
-	       SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x21,0x3f,0xc0);
-	    } else {							/* For 301, 301B */ 
-	        tempah = 0xc0;
-	        tempbl = 0x3f;
-	        if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)) {
-	           tempah = 0x00;
-		   if(SiS_IsDualEdge(SiS_Pr, HwDeviceExtension, BaseAddr)) {
-		      tempbl = 0xff;
-		   }
-	        }
-	        SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x21,tempbl,tempah);     
+	    tempah = 0xc0;						/* For 301, 301B (and 301BDH rev b1) */
+	    tempbl = 0x3f;
+	    if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)) {
+	       tempah = 0x00;
+	       if(SiS_IsDualEdge(SiS_Pr, HwDeviceExtension, BaseAddr)) {
+		  tempbl = 0xff;
+	       }
 	    }
+	    SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x21,tempbl,tempah);
 	 }
 
-	 if(IS_SIS740) {						
+	 if(IS_SIS740) {
 	    tempah = 0x80;
 	    tempbl = 0x7f;
 	    if(SiS_Pr->SiS_VBInfo & DisableCRT2Display) {
@@ -3141,8 +3240,9 @@
             tempbl = 0x7f;
             if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)) {
                tempbl = 0xff;
-	       if(!(SiS_IsDualEdge(SiS_Pr, HwDeviceExtension, BaseAddr)))
+	       if(!(SiS_IsDualEdge(SiS_Pr, HwDeviceExtension, BaseAddr))) {
 	          tempah |= 0x80;
+	       }
             }
             SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x23,tempbl,tempah);
 	 }
@@ -3174,16 +3274,32 @@
 	    tempbl = 0xfb;
             if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)) {
                tempah = 0x00;
-	       if(SiS_IsDualEdge(SiS_Pr, HwDeviceExtension, BaseAddr))
+	       if(SiS_IsDualEdge(SiS_Pr, HwDeviceExtension, BaseAddr)) {
 	          tempbl = 0xff;
+	       }
             }
 	    SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x13,tempbl,tempah);
 
-	    if(SiS_Pr->SiS_VBInfo & DisableCRT2Display)
+	    if(SiS_Pr->SiS_VBInfo & DisableCRT2Display) {
 	       SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x13,0xfb,0x00);
+	    }
 
 	    SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2c,0xcf,0x30);
 
+	 } else if(HwDeviceExtension->jChipType == SIS_550) {
+
+#if 0
+	    tempah = 0x00;
+	    tempbl = 0xfb;
+	    if(SiS_Pr->SiS_VBInfo & DisableCRT2Display) {
+	       tempah = 0x00;
+	       tempbl = 0xfb;
+	    }
+	    SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x13,tempbl,tempah);
+#endif
+	    SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x13,0xfb);
+
+	    SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2c,0xcf,0x30);
 	 }
 
       }
@@ -3209,10 +3325,9 @@
 	                      HwDeviceExtension);
         } else {
 
-	   if( (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) &&
-	       (SiS_Pr->SiS_VBType & VB_NoLCD) ) {
-	       
-	      /* TW: Need LVDS Data for LCD on 301BDH */
+	   if( (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) && (SiS_Pr->SiS_VBType & VB_NoLCD) ) {
+
+	      /* Need LVDS Data for LCD on 301B-DH */
 	      SiS_GetCRT2DataLVDS(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
 	                          HwDeviceExtension);
 				  
@@ -3282,18 +3397,16 @@
 
    } else {
 
-      /* TW: 301BDH needs LVDS Data */
-      if( (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) &&
-          (SiS_Pr->SiS_VBType & VB_NoLCD) ) {
+      /* 301BDH needs LVDS Data */
+      if( (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) && (SiS_Pr->SiS_VBType & VB_NoLCD) ) {
 	      SiS_Pr->SiS_IF_DEF_LVDS = 1;
       }
 
       SiS_GetCRT2Ptr(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
                      &CRT2Index,&ResIndex,HwDeviceExtension);
 
-      /* TW: 301BDH needs LVDS Data */
-      if( (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) &&
-          (SiS_Pr->SiS_VBType & VB_NoLCD) ) {
+      /* 301BDH needs LVDS Data */
+      if( (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) && (SiS_Pr->SiS_VBType & VB_NoLCD) ) {
               SiS_Pr->SiS_IF_DEF_LVDS = 0;
       }
 
@@ -3321,11 +3434,16 @@
 	case 20:  LVDSData = SiS_Pr->SiS_LVDS1280x768Data_2;   break;
 	case 21:  LVDSData = SiS_Pr->SiS_LVDS1600x1200Data_1;  break;
 	case 22:  LVDSData = SiS_Pr->SiS_LVDS1600x1200Data_2;  break;
+	case 30:  LVDSData = SiS_Pr->SiS_LVDS640x480Data_2;    break;
+	case 80:  LVDSData = SiS_Pr->SiS_LVDSBARCO1366Data_1;  break;
+	case 81:  LVDSData = SiS_Pr->SiS_LVDSBARCO1366Data_2;  break;
+	case 82:  LVDSData = SiS_Pr->SiS_LVDSBARCO1024Data_1;  break;
+	case 83:  LVDSData = SiS_Pr->SiS_LVDSBARCO1024Data_2;  break;
 	case 90:  LVDSData = SiS_Pr->SiS_CHTVUPALMData;        break;
       	case 91:  LVDSData = SiS_Pr->SiS_CHTVOPALMData;        break;
       	case 92:  LVDSData = SiS_Pr->SiS_CHTVUPALNData;        break;
       	case 93:  LVDSData = SiS_Pr->SiS_CHTVOPALNData;        break;
-	case 99:  LVDSData = SiS_Pr->SiS_CHTVSOPALData;	       break;  /* TW: Super Overscan */
+	case 99:  LVDSData = SiS_Pr->SiS_CHTVSOPALData;	       break;  /* Super Overscan */
 	default:  LVDSData = SiS_Pr->SiS_LVDS1024x768Data_1;   break;
      }
    }
@@ -3337,67 +3455,36 @@
 
   if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
 
-    if(!(SiS_Pr->SiS_LCDInfo & DontExpandLCD)){
-         if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768){
-           SiS_Pr->SiS_HDE = 1024;
-           SiS_Pr->SiS_VDE =  768;
-         } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024){
-           SiS_Pr->SiS_HDE = 1280;
-           SiS_Pr->SiS_VDE = 1024;
-	 } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050){
-           SiS_Pr->SiS_HDE = 1400;
-           SiS_Pr->SiS_VDE = 1050;
-	 } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200){
-           SiS_Pr->SiS_HDE = 1600;
-           SiS_Pr->SiS_VDE = 1200;
-         } else {
-	   SiS_Pr->SiS_HDE = 1280;
-	   SiS_Pr->SiS_VDE =  960;
-	 }
-    }
+     if(!(SiS_Pr->SiS_LCDInfo & DontExpandLCD)) {
+        SiS_Pr->SiS_HDE = SiS_Pr->PanelXRes;
+        SiS_Pr->SiS_VDE = SiS_Pr->PanelYRes;
+     }
 
   } else {
 
-    if(SiS_Pr->SiS_IF_DEF_TRUMPION == 0) {
-      if((SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) && (!(SiS_Pr->SiS_LCDInfo & LCDPass11))) {
-        if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480) {
-          if((!(SiS_Pr->SiS_LCDInfo & DontExpandLCD)) || (SiS_Pr->SiS_SetFlag & SetDOSMode)) {
-            if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600) {
-              SiS_Pr->SiS_HDE =  800;
-              SiS_Pr->SiS_VDE =  600;
-	    } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x600) {
-	      SiS_Pr->SiS_HDE = 1024;
-              SiS_Pr->SiS_VDE =  600;  
-            } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
-              SiS_Pr->SiS_HDE = 1024;
-              SiS_Pr->SiS_VDE =  768;
- 	    } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1152x768) {
-	      SiS_Pr->SiS_HDE = 1152;
-	      SiS_Pr->SiS_VDE =  768;	
-	    } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1152x864) {
-	      SiS_Pr->SiS_HDE = 1152;
-	      SiS_Pr->SiS_VDE =  864;  
-	    } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768) {
-	      SiS_Pr->SiS_HDE = 1280;
-	      SiS_Pr->SiS_VDE =  768;        
-            } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
-              SiS_Pr->SiS_HDE = 1280;
-              SiS_Pr->SiS_VDE = 1024;
-	    } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) {
-	      SiS_Pr->SiS_HDE = 1400;
-              SiS_Pr->SiS_VDE = 1050;
-	    } else {
-	      SiS_Pr->SiS_HDE = 1600;
-	      SiS_Pr->SiS_VDE = 1200;
-	    }
-            if(SiS_Pr->SiS_IF_DEF_FSTN) {
-              SiS_Pr->SiS_HDE = 320;
-              SiS_Pr->SiS_VDE = 480;
-            }
-          }
+     if(SiS_Pr->SiS_IF_DEF_TRUMPION == 0) {
+        if((SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) && (!(SiS_Pr->SiS_LCDInfo & LCDPass11))) {
+           if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480) {
+              if((!(SiS_Pr->SiS_LCDInfo & DontExpandLCD)) || (SiS_Pr->SiS_SetFlag & SetDOSMode)) {
+	         SiS_Pr->SiS_HDE = SiS_Pr->PanelXRes;
+                 SiS_Pr->SiS_VDE = SiS_Pr->PanelYRes;
+
+		 if(SiS_Pr->SiS_CustomT == CUT_BARCO1366) {
+		    if(ResIndex < 0x08) {
+		       SiS_Pr->SiS_HDE = 1280;
+                       SiS_Pr->SiS_VDE = 1024;
+		    }
+		 }
+#if 0
+                 if(SiS_Pr->SiS_IF_DEF_FSTN) {
+                    SiS_Pr->SiS_HDE = 320;
+                    SiS_Pr->SiS_VDE = 480;
+                 }
+#endif		 
+              }
+           }
         }
-      }
-    }
+     }
   }
 }
 
@@ -3413,11 +3500,16 @@
   const SiS_TVDataStruct  *TVPtr  = NULL;
 
   if(ModeNo <= 0x13) {
-    	modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
-    	resinfo = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ResInfo;
+     modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
+     resinfo = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ResInfo;
   } else {
+     if(SiS_Pr->UseCustomMode) {
+        modeflag = SiS_Pr->CModeFlag;
+	resinfo = 0;
+     } else {
     	modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
     	resinfo = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO;
+     }
   }
   
   SiS_Pr->SiS_NewFlickerMode = 0;
@@ -3429,17 +3521,26 @@
 
   SiS_GetCRT2ResInfo(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,HwDeviceExtension);
 
-  /* TW: For VGA2 ("RAMDAC2") */
-
   if(SiS_Pr->SiS_VBInfo & SetCRT2ToRAMDAC){
-     SiS_GetRAMDAC2DATA(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
-                        HwDeviceExtension);
-     return;
-  }
 
-  /* TW: For TV */
+     if(SiS_Pr->UseCustomMode) {
+
+        SiS_Pr->SiS_RVBHCMAX  = 1;
+        SiS_Pr->SiS_RVBHCFACT = 1;
+        SiS_Pr->SiS_VGAHT     = SiS_Pr->CHTotal;
+        SiS_Pr->SiS_VGAVT     = SiS_Pr->CVTotal;
+        SiS_Pr->SiS_HT        = SiS_Pr->CHTotal;
+        SiS_Pr->SiS_VT        = SiS_Pr->CVTotal;
+	SiS_Pr->SiS_HDE       = SiS_Pr->SiS_VGAHDE;
+        SiS_Pr->SiS_VDE       = SiS_Pr->SiS_VGAVDE;
 
-  if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+     } else {
+
+        SiS_GetRAMDAC2DATA(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
+                           HwDeviceExtension);
+     }
+
+  } else if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
 
     SiS_GetCRT2Ptr(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
                    &CRT2Index,&ResIndex,HwDeviceExtension);
@@ -3452,7 +3553,7 @@
       case  4:  TVPtr = SiS_Pr->SiS_ExtNTSCData;   break;
       case  8:  TVPtr = SiS_Pr->SiS_StPALData;     break;
       case  9:  TVPtr = SiS_Pr->SiS_StNTSCData;    break;
-      default:  TVPtr = SiS_Pr->SiS_StPALData;     break;  /* TW: Just to avoid a crash */
+      default:  TVPtr = SiS_Pr->SiS_StPALData;     break;  /* Just to avoid a crash */
     }
 
     SiS_Pr->SiS_RVBHCMAX  = (TVPtr+ResIndex)->RVBHCMAX;
@@ -3464,18 +3565,16 @@
     SiS_Pr->SiS_RVBHRS    = (TVPtr+ResIndex)->RVBHRS;
     SiS_Pr->SiS_NewFlickerMode = (TVPtr+ResIndex)->FlickerMode;
     if(modeflag & HalfDCLK) {
-	SiS_Pr->SiS_RVBHRS    = (TVPtr+ResIndex)->HALFRVBHRS;
+	SiS_Pr->SiS_RVBHRS     = (TVPtr+ResIndex)->HALFRVBHRS;
     }
 
     if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) {  
     
        if(SiS_Pr->SiS_HiVision != 3) {
-       
-      	  if(resinfo == 0x08) SiS_Pr->SiS_NewFlickerMode = 0x40;
-      	  if(resinfo == 0x09) SiS_Pr->SiS_NewFlickerMode = 0x40;
-	  if(resinfo == 0x12) SiS_Pr->SiS_NewFlickerMode = 0x40;
-	  
-       } 
+      	  if(resinfo == SIS_RI_1024x768)  SiS_Pr->SiS_NewFlickerMode = 0x40;
+      	  if(resinfo == SIS_RI_1280x1024) SiS_Pr->SiS_NewFlickerMode = 0x40;
+	  if(resinfo == SIS_RI_1280x720)  SiS_Pr->SiS_NewFlickerMode = 0x40;
+       }
        
        switch(SiS_Pr->SiS_HiVision) {
        case 2:
@@ -3504,127 +3603,126 @@
 
     } else {
 
-      SiS_Pr->SiS_RY1COE = (TVPtr+ResIndex)->RY1COE;
-      SiS_Pr->SiS_RY2COE = (TVPtr+ResIndex)->RY2COE;
-      SiS_Pr->SiS_RY3COE = (TVPtr+ResIndex)->RY3COE;
-      SiS_Pr->SiS_RY4COE = (TVPtr+ResIndex)->RY4COE;
-
-      if(modeflag & HalfDCLK) {
-         SiS_Pr->SiS_RY1COE = 0x00;
-         SiS_Pr->SiS_RY2COE = 0xf4;
-         SiS_Pr->SiS_RY3COE = 0x10;
-         SiS_Pr->SiS_RY4COE = 0x38;
-      }
-
-      if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) {
-        SiS_Pr->SiS_HT = NTSCHT;
-	if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {  
-	   if((ModeNo == 0x4a) || (ModeNo == 0x38)) SiS_Pr->SiS_HT = NTSC2HT;
-	}  
-        SiS_Pr->SiS_VT = NTSCVT;
-      } else {
-        SiS_Pr->SiS_HT = PALHT;
-        SiS_Pr->SiS_VT = PALVT;
-      }
+       SiS_Pr->SiS_RY1COE = (TVPtr+ResIndex)->RY1COE;
+       SiS_Pr->SiS_RY2COE = (TVPtr+ResIndex)->RY2COE;
+       SiS_Pr->SiS_RY3COE = (TVPtr+ResIndex)->RY3COE;
+       SiS_Pr->SiS_RY4COE = (TVPtr+ResIndex)->RY4COE;
+
+       if(modeflag & HalfDCLK) {
+          SiS_Pr->SiS_RY1COE = 0x00;
+          SiS_Pr->SiS_RY2COE = 0xf4;
+          SiS_Pr->SiS_RY3COE = 0x10;
+          SiS_Pr->SiS_RY4COE = 0x38;
+       }
+
+       if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) {
+          SiS_Pr->SiS_HT = NTSCHT;
+	  if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
+	     if((ModeNo == 0x64) || (ModeNo == 0x4a) || (ModeNo == 0x38)) SiS_Pr->SiS_HT = NTSC2HT;
+	  }
+          SiS_Pr->SiS_VT = NTSCVT;
+       } else {
+          SiS_Pr->SiS_HT = PALHT;
+          SiS_Pr->SiS_VT = PALVT;
+       }
 
     }
 
-    return;
-  }
+  } else if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
+
+     if(SiS_Pr->UseCustomMode) {
 
-  /* TW: For LCD */
+        SiS_Pr->SiS_RVBHCMAX  = 1;
+        SiS_Pr->SiS_RVBHCFACT = 1;
+        SiS_Pr->SiS_VGAHT     = SiS_Pr->CHTotal;
+        SiS_Pr->SiS_VGAVT     = SiS_Pr->CVTotal;
+        SiS_Pr->SiS_HT        = SiS_Pr->CHTotal;
+        SiS_Pr->SiS_VT        = SiS_Pr->CVTotal;
+	SiS_Pr->SiS_HDE       = SiS_Pr->SiS_VGAHDE;
+        SiS_Pr->SiS_VDE       = SiS_Pr->SiS_VGAVDE;
 
-  if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
+     } else {
 
-    SiS_GetCRT2Ptr(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
-                   &CRT2Index,&ResIndex,HwDeviceExtension);
+        SiS_GetCRT2Ptr(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
+                      &CRT2Index,&ResIndex,HwDeviceExtension);
+
+        switch(CRT2Index) {
+         case  0: LCDPtr = SiS_Pr->SiS_ExtLCD1024x768Data;        break; /* VESA Timing */
+         case  1: LCDPtr = SiS_Pr->SiS_ExtLCD1280x1024Data;       break; /* VESA Timing */
+         case  5: LCDPtr = SiS_Pr->SiS_StLCD1024x768Data;         break; /* Obviously unused */
+         case  6: LCDPtr = SiS_Pr->SiS_StLCD1280x1024Data;        break; /* Obviously unused */
+         case 10: LCDPtr = SiS_Pr->SiS_St2LCD1024x768Data;        break; /* Non-VESA Timing */
+         case 11: LCDPtr = SiS_Pr->SiS_St2LCD1280x1024Data;       break; /* Non-VESA Timing */
+         case 13: LCDPtr = SiS_Pr->SiS_NoScaleData1024x768;       break; /* Non-expanding */
+         case 14: LCDPtr = SiS_Pr->SiS_NoScaleData1280x1024;      break; /* Non-expanding */
+         case 15: LCDPtr = SiS_Pr->SiS_LCD1280x960Data;           break; /* 1280x960 */
+         case 20: LCDPtr = SiS_Pr->SiS_ExtLCD1400x1050Data;       break; /* VESA Timing */
+         case 21: LCDPtr = SiS_Pr->SiS_NoScaleData1400x1050;      break; /* Non-expanding (let panel scale) */
+         case 22: LCDPtr = SiS_Pr->SiS_StLCD1400x1050Data;        break; /* Non-VESA Timing (let panel scale) */
+         case 23: LCDPtr = SiS_Pr->SiS_ExtLCD1600x1200Data;       break; /* VESA Timing */
+         case 24: LCDPtr = SiS_Pr->SiS_NoScaleData1600x1200;      break; /* Non-expanding */
+         case 25: LCDPtr = SiS_Pr->SiS_StLCD1600x1200Data;        break; /* Non-VESA Timing */
+         case 26: LCDPtr = SiS_Pr->SiS_ExtLCD1280x768Data;        break; /* VESA Timing */
+         case 27: LCDPtr = SiS_Pr->SiS_NoScaleData1280x768;       break; /* Non-expanding */
+         case 28: LCDPtr = SiS_Pr->SiS_StLCD1280x768Data;         break; /* Non-VESA Timing */
+         case 29: LCDPtr = SiS_Pr->SiS_NoScaleData;	          break; /* Generic no-scale data */
+         default: LCDPtr = SiS_Pr->SiS_ExtLCD1024x768Data;	  break; /* Just to avoid a crash */
+        }
+
+        SiS_Pr->SiS_RVBHCMAX  = (LCDPtr+ResIndex)->RVBHCMAX;
+        SiS_Pr->SiS_RVBHCFACT = (LCDPtr+ResIndex)->RVBHCFACT;
+        SiS_Pr->SiS_VGAHT     = (LCDPtr+ResIndex)->VGAHT;
+        SiS_Pr->SiS_VGAVT     = (LCDPtr+ResIndex)->VGAVT;
+        SiS_Pr->SiS_HT        = (LCDPtr+ResIndex)->LCDHT;
+        SiS_Pr->SiS_VT        = (LCDPtr+ResIndex)->LCDVT;
 
-    switch(CRT2Index) {
-      case  0: LCDPtr = SiS_Pr->SiS_ExtLCD1024x768Data;        break; /* VESA Timing */
-      case  1: LCDPtr = SiS_Pr->SiS_ExtLCD1280x1024Data;       break; /* VESA Timing */
-      case  5: LCDPtr = SiS_Pr->SiS_StLCD1024x768Data;         break; /* Obviously unused */
-      case  6: LCDPtr = SiS_Pr->SiS_StLCD1280x1024Data;        break; /* Obviously unused */
-      case 10: LCDPtr = SiS_Pr->SiS_St2LCD1024x768Data;        break; /* Non-VESA Timing */
-      case 11: LCDPtr = SiS_Pr->SiS_St2LCD1280x1024Data;       break; /* Non-VESA Timing */
-      case 13: LCDPtr = SiS_Pr->SiS_NoScaleData1024x768;       break; /* Non-expanding */
-      case 14: LCDPtr = SiS_Pr->SiS_NoScaleData1280x1024;      break; /* Non-expanding */
-      case 15: LCDPtr = SiS_Pr->SiS_LCD1280x960Data;           break; /* 1280x960 */
-      case 20: LCDPtr = SiS_Pr->SiS_ExtLCD1400x1050Data;       break; /* VESA Timing */
-      case 21: LCDPtr = SiS_Pr->SiS_NoScaleData1400x1050;      break; /* Non-expanding (let panel scale) */
-      case 22: LCDPtr = SiS_Pr->SiS_StLCD1400x1050Data;	       break; /* Non-VESA Timing (let panel scale) */
-      case 23: LCDPtr = SiS_Pr->SiS_ExtLCD1600x1200Data;       break; /* VESA Timing */
-      case 24: LCDPtr = SiS_Pr->SiS_NoScaleData1600x1200;      break; /* Non-expanding */
-      case 25: LCDPtr = SiS_Pr->SiS_StLCD1600x1200Data;	       break; /* Non-VESA Timing */
-      case 26: LCDPtr = SiS_Pr->SiS_ExtLCD1280x768Data;        break; /* VESA Timing */
-      case 27: LCDPtr = SiS_Pr->SiS_NoScaleData1280x768;       break; /* Non-expanding */
-      case 28: LCDPtr = SiS_Pr->SiS_StLCD1280x768Data;	       break; /* Non-VESA Timing */
-      case 29: LCDPtr = SiS_Pr->SiS_NoScaleData;	       break; /* Generic no-scale data */
-      default: LCDPtr = SiS_Pr->SiS_ExtLCD1024x768Data;	       break; /* Just to avoid a crash */
-    }
-
-    SiS_Pr->SiS_RVBHCMAX  = (LCDPtr+ResIndex)->RVBHCMAX;
-    SiS_Pr->SiS_RVBHCFACT = (LCDPtr+ResIndex)->RVBHCFACT;
-    SiS_Pr->SiS_VGAHT     = (LCDPtr+ResIndex)->VGAHT;
-    SiS_Pr->SiS_VGAVT     = (LCDPtr+ResIndex)->VGAVT;
-    SiS_Pr->SiS_HT        = (LCDPtr+ResIndex)->LCDHT;
-    SiS_Pr->SiS_VT        = (LCDPtr+ResIndex)->LCDVT;
-    
 #ifdef TWDEBUG
-    xf86DrvMsg(0, X_INFO,
-    	"GetCRT2Data: Index %d ResIndex %d\n", CRT2Index, ResIndex);
-#endif    
+        xf86DrvMsg(0, X_INFO,
+    	    "GetCRT2Data: Index %d ResIndex %d\n", CRT2Index, ResIndex);
+#endif
 
-    tempax = 1024;
-    if(SiS_Pr->SiS_SetFlag & LCDVESATiming) {
-      if(HwDeviceExtension->jChipType < SIS_315H) {
-         if     (SiS_Pr->SiS_VGAVDE == 350) tempbx = 560;
-         else if(SiS_Pr->SiS_VGAVDE == 400) tempbx = 640;
-         else                               tempbx = 768;
-      } else {      
-         tempbx = 768; 
-      }
-    } else {
-      if     (SiS_Pr->SiS_VGAVDE == 357) tempbx = 527;
-      else if(SiS_Pr->SiS_VGAVDE == 420) tempbx = 620;
-      else if(SiS_Pr->SiS_VGAVDE == 525) tempbx = 775;
-      else if(SiS_Pr->SiS_VGAVDE == 600) tempbx = 775;
-      else if(SiS_Pr->SiS_VGAVDE == 350) tempbx = 560;
-      else if(SiS_Pr->SiS_VGAVDE == 400) tempbx = 640;
-      else                               tempbx = 768;
-    }
-    if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
-      tempax = 1280;
-      if     (SiS_Pr->SiS_VGAVDE == 360) tempbx = 768;
-      else if(SiS_Pr->SiS_VGAVDE == 375) tempbx = 800;
-      else if(SiS_Pr->SiS_VGAVDE == 405) tempbx = 864;
-      else                               tempbx = 1024;
-    }
-    if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x960) {
-      tempax = 1280;
-      if     (SiS_Pr->SiS_VGAVDE == 350)  tempbx = 700;
-      else if(SiS_Pr->SiS_VGAVDE == 400)  tempbx = 800;
-      else if(SiS_Pr->SiS_VGAVDE == 1024) tempbx = 960;
-      else                                tempbx = 960;
-    }
-    if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) {
-      tempax = 1400;
-      tempbx = 1050;
-    }
-    if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) {
-      tempax = 1600;
-      tempbx = 1200;
-    }
-    if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768) {  /* guessed */
-      tempax = 1280;
-      tempbx = 768;
-    }
-    if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
-       tempax = SiS_Pr->SiS_VGAHDE;
-       tempbx = SiS_Pr->SiS_VGAVDE;
-    }
-    SiS_Pr->SiS_HDE = tempax;
-    SiS_Pr->SiS_VDE = tempbx;
-    return;
+	if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
+           tempax = 1024;
+           if(SiS_Pr->SiS_SetFlag & LCDVESATiming) {
+              if(HwDeviceExtension->jChipType < SIS_315H) {
+                 if     (SiS_Pr->SiS_VGAVDE == 350) tempbx = 560;
+                 else if(SiS_Pr->SiS_VGAVDE == 400) tempbx = 640;
+                 else                               tempbx = 768;
+              } else {
+                 tempbx = 768;
+              }
+           } else {
+              if     (SiS_Pr->SiS_VGAVDE == 357) tempbx = 527;
+              else if(SiS_Pr->SiS_VGAVDE == 420) tempbx = 620;
+              else if(SiS_Pr->SiS_VGAVDE == 525) tempbx = 775;
+              else if(SiS_Pr->SiS_VGAVDE == 600) tempbx = 775;
+              else if(SiS_Pr->SiS_VGAVDE == 350) tempbx = 560;
+              else if(SiS_Pr->SiS_VGAVDE == 400) tempbx = 640;
+              else                               tempbx = 768;
+           }
+	} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
+           tempax = 1280;
+           if     (SiS_Pr->SiS_VGAVDE == 360) tempbx = 768;
+           else if(SiS_Pr->SiS_VGAVDE == 375) tempbx = 800;
+           else if(SiS_Pr->SiS_VGAVDE == 405) tempbx = 864;
+           else                               tempbx = 1024;
+        } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x960) {
+           tempax = 1280;
+           if     (SiS_Pr->SiS_VGAVDE == 350)  tempbx = 700;
+           else if(SiS_Pr->SiS_VGAVDE == 400)  tempbx = 800;
+           else if(SiS_Pr->SiS_VGAVDE == 1024) tempbx = 960;
+           else                                tempbx = 960;
+        } else {
+	   tempax = SiS_Pr->PanelXRes;
+           tempbx = SiS_Pr->PanelYRes;
+	}
+        if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
+           tempax = SiS_Pr->SiS_VGAHDE;
+           tempbx = SiS_Pr->SiS_VGAVDE;
+        }
+        SiS_Pr->SiS_HDE = tempax;
+        SiS_Pr->SiS_VDE = tempbx;
+     }
   }
 }
 
@@ -3634,9 +3732,9 @@
   USHORT resindex;
 
   if(ModeNo <= 0x13)
-    	resindex=SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ResInfo;
+     resindex=SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ResInfo;
   else
-    	resindex=SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO;
+     resindex=SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO;
 
   return(resindex);
 }
@@ -3647,41 +3745,46 @@
 {
   USHORT xres,yres,modeflag=0,resindex;
 
+  if(SiS_Pr->UseCustomMode) {
+     SiS_Pr->SiS_VGAHDE = SiS_Pr->SiS_HDE = SiS_Pr->CHDisplay;
+     SiS_Pr->SiS_VGAVDE = SiS_Pr->SiS_VDE = SiS_Pr->CVDisplay;
+     return;
+  }
+
   resindex = SiS_GetResInfo(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex);
 
   if(ModeNo <= 0x13) {
-    	xres = SiS_Pr->SiS_StResInfo[resindex].HTotal;
-    	yres = SiS_Pr->SiS_StResInfo[resindex].VTotal;
+     xres = SiS_Pr->SiS_StResInfo[resindex].HTotal;
+     yres = SiS_Pr->SiS_StResInfo[resindex].VTotal;
   } else {
-	xres = SiS_Pr->SiS_ModeResInfo[resindex].HTotal;
-    	yres = SiS_Pr->SiS_ModeResInfo[resindex].VTotal;
-    	modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
+     xres = SiS_Pr->SiS_ModeResInfo[resindex].HTotal;
+     yres = SiS_Pr->SiS_ModeResInfo[resindex].VTotal;
+     modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
   }
 
-  if((HwDeviceExtension->jChipType >= SIS_315H) && (SiS_Pr->SiS_IF_DEF_LVDS == 1)) {
-      if((ModeNo != 0x03) && (SiS_Pr->SiS_SetFlag & SetDOSMode)) {
-          if(yres == 350) yres = 400;
-      }
-      if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x3a) & 0x01) {
- 	  if(ModeNo == 0x12) yres = 400;
-      }
-  }
+  if((!SiS_Pr->SiS_IF_DEF_DSTN) && (!SiS_Pr->SiS_IF_DEF_FSTN)) {
+
+     if((HwDeviceExtension->jChipType >= SIS_315H) && (SiS_Pr->SiS_IF_DEF_LVDS == 1)) {
+        if((ModeNo != 0x03) && (SiS_Pr->SiS_SetFlag & SetDOSMode)) {
+           if(yres == 350) yres = 400;
+        }
+        if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x3a) & 0x01) {
+ 	   if(ModeNo == 0x12) yres = 400;
+        }
+     }
+
+     if(ModeNo > 0x13) {
+  	if(modeflag & HalfDCLK)       xres *= 2;
+  	if(modeflag & DoubleScanMode) yres *= 2;
+     }
 
-  if(ModeNo > 0x13) {
-      if(SiS_Pr->SiS_IF_DEF_FSTN == 1){
-            xres *= 2;
-            yres *= 2;
-      } else {
-  	    if(modeflag & HalfDCLK)       xres *= 2;
-  	    if(modeflag & DoubleScanMode) yres *= 2;
-      }
   }
 
   if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
         if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
            if(xres == 720) xres = 640;
 	} else {
-	   if(SiS_Pr->SiS_VBType & VB_NoLCD) {           /* TW: 301BDH */
+	   if(SiS_Pr->SiS_VBType & VB_NoLCD) {           /* 301BDH */
 	        if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
                    if(xres == 720) xres = 640;
 		}
@@ -3702,17 +3805,18 @@
 	         if(xres == 720) xres = 640;
 	      }
 	      if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
-    	         if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
-		    if(ModeNo <= 0x13) {
-		       /* TW: This is wrong for 640x400 *graphics* mode */
+		 if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
+		    if((ModeNo <= 0x13) || (SiS_Pr->SiS_VBType & VB_SIS301LV302LV)) {
+		       /* Panel on 301B via DVI shows 5 extra (garbage-)lines if this is done,
+		        * panel on 30xLV blinks if it's not done...
+			*/
       		       if(yres == 400) yres = 405;
 		    }
       		    if(yres == 350) yres = 360;
       		    if(SiS_Pr->SiS_SetFlag & LCDVESATiming) {
         	       if(yres == 360) yres = 375;
       		    }
-   	         }
-    	         if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768){
+   	         } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
       		    if(!(SiS_Pr->SiS_SetFlag & LCDVESATiming)) {
         	       if(!(SiS_Pr->SiS_LCDInfo & DontExpandLCD)) {
           	          if(yres == 350) yres = 357;
@@ -3727,12 +3831,15 @@
   } else {
     	if(xres == 720) xres = 640;
 	if(SiS_Pr->SiS_SetFlag & SetDOSMode) {
-	      yres = 400;
-	      if(HwDeviceExtension->jChipType >= SIS_315H) {
-	          if(SiS_GetReg1(SiS_Pr->SiS_P3c4,0x17) & 0x80) yres = 480;
-	      } else {
-	          if(SiS_GetReg1(SiS_Pr->SiS_P3c4,0x13) & 0x80) yres = 480;
-	      }
+	   yres = 400;
+	   if(HwDeviceExtension->jChipType >= SIS_315H) {
+	      if(SiS_GetReg1(SiS_Pr->SiS_P3c4,0x17) & 0x80) yres = 480;
+	   } else {
+	      if(SiS_GetReg1(SiS_Pr->SiS_P3c4,0x13) & 0x80) yres = 480;
+	   }
+	   if(SiS_Pr->SiS_IF_DEF_DSTN || SiS_Pr->SiS_IF_DEF_FSTN) {
+	      yres = 480;
+	   }
 	}
   }
   SiS_Pr->SiS_VGAHDE = SiS_Pr->SiS_HDE = xres;
@@ -3782,12 +3889,10 @@
 			} else {
 			   tempbx = 29;
 			   if(ModeNo >= 0x13) {
-			      if(HwDeviceExtension->jChipType >= SIS_315H) {
-			         /* 1280x768 and 1280x960 have same CRT2CRTC,
-				  * so we change it here if 1280x960 is chosen
-				  */
-			         if(resinfo == 0x0f) tempal = 10;
-			      }
+			      /* 1280x768 and 1280x960 have same CRT2CRTC,
+			       * so we change it here if 1280x960 is chosen
+			       */
+			      if(resinfo == SIS_RI_1280x960) tempal = 10;
 			   }
 			}
 		} else {
@@ -3795,7 +3900,7 @@
       		   if(!(SiS_Pr->SiS_SetFlag & LCDVESATiming)) {
         		tempbx += 5;
                         /* GetRevisionID();  */
-			/* TW: BIOS only adds 5 once */
+			/* BIOS only adds 5 once */
         		tempbx += 5;
        		   }
 	        }
@@ -3819,24 +3924,16 @@
 
         tempal &= 0x3F;
 
-      	if((SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) &&
-           (SiS_Pr->SiS_VBInfo & (SetCRT2ToTV-SetCRT2ToHiVisionTV))) {
-      		if(tempal == 0x06) tempal = 0x07;
-        }
-
-	if((HwDeviceExtension->jChipType == SIS_300) &&
-	   (SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV)) {
-	    if(ModeNo > 0x13) {
-	        if((resinfo == 0x0c) || (resinfo == 0x0d))  /* 720 (index diff. on 310/325!) */
-		    tempal = 6;
-	    }
-	}
-
-	if(HwDeviceExtension->jChipType != SIS_300) {
-           if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
-              if((ModeNo == 0x31) || (ModeNo == 0x32)) tempal = 6;
+      	if(SiS_Pr->SiS_VBInfo & (SetCRT2ToTV - SetCRT2ToHiVisionTV)) {
+	   if(ModeNo > 0x13) {
+      	      if(tempal == 6) tempal = 7;
+              if((resinfo == SIS_RI_720x480) ||
+	         (resinfo == SIS_RI_720x576) ||
+	         (resinfo == SIS_RI_768x576)) {
+		 tempal = 6;
+	      }
 	   }
-	}
+        }
 
      	*CRT2Index = tempbx;
      	*ResIndex = tempal;
@@ -3846,25 +3943,22 @@
     	Flag = 1;
     	tempbx = 0;
     	if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
-	
-      		if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD)) {
-        		Flag = 0;
-        		tempbx = 10;
-			if(SiS_Pr->SiS_VBInfo & SetCHTVOverScan) tempbx += 1;
-        		if(SiS_Pr->SiS_VBInfo & SetPALTV) {
-				tempbx += 2;
-				if(SiS_Pr->SiS_CHSOverScan) tempbx = 99;
-				if(SiS_Pr->SiS_CHPALM) {
-					tempbx = 90;
-					if(SiS_Pr->SiS_VBInfo & SetCHTVOverScan) tempbx += 1;
-				} else if(SiS_Pr->SiS_CHPALN) {
-					tempbx = 92;
-					if(SiS_Pr->SiS_VBInfo & SetCHTVOverScan) tempbx += 1;
-				}
-				
-			}
-      		}
-		
+      	   if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD)) {
+              Flag = 0;
+              tempbx = 10;
+	      if(SiS_Pr->SiS_VBInfo & SetCHTVOverScan) tempbx += 1;
+              if(SiS_Pr->SiS_VBInfo & SetPALTV) {
+		 tempbx += 2;
+		 if(SiS_Pr->SiS_CHSOverScan) tempbx = 99;
+		 if(SiS_Pr->SiS_CHPALM) {
+		    tempbx = 90;
+		    if(SiS_Pr->SiS_VBInfo & SetCHTVOverScan) tempbx += 1;
+		 } else if(SiS_Pr->SiS_CHPALN) {
+		    tempbx = 92;
+		    if(SiS_Pr->SiS_VBInfo & SetCHTVOverScan) tempbx += 1;
+	 	 }
+              }
+           }
     	}
 
     	if(Flag) {
@@ -3872,11 +3966,19 @@
 		if(SiS_Pr->SiS_LCDResInfo <= SiS_Pr->SiS_Panel1280x1024) {
 		   tempbx = SiS_Pr->SiS_LCDResInfo - SiS_Pr->SiS_PanelMinLVDS;
    	      	   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)  tempbx += 3;
+		   if(SiS_Pr->SiS_CustomT == CUT_BARCO1024) {
+		      tempbx = 82;
+		      if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)  tempbx++;
+		   }
 		} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768) {
 		   tempbx = 18;
 		   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)  tempbx++; 
-	        } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480) { 
+	        } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480) {
 		   tempbx = 6;
+		} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_2) {
+		   tempbx = 30;
+		} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_3) {
+		   tempbx = 30;
 		} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x600) {
 		   tempbx = 15;
   		   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)  tempbx += 2;
@@ -3889,20 +3991,25 @@
 		} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) {
 		   tempbx = 21;
 		   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)  tempbx++;
+		} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_PanelBarco1366) {
+		   tempbx = 80;
+   	      	   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)  tempbx++;
 		}
-		
+
 		if(SiS_Pr->SiS_LCDInfo & LCDPass11) {
 		   tempbx = 7;
         	}
 		
 	}
 
+#if 0
 	if(SiS_Pr->SiS_IF_DEF_FSTN){
        	 	if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel320x480){
          		tempbx = 14;
          		tempal = 6;
         	}
     	}
+#endif	
 
 	if(SiS_Pr->SiS_SetFlag & SetDOSMode) {
 	        if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480) tempal = 7;
@@ -3914,13 +4021,10 @@
 
 	if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
 	    if(ModeNo > 0x13) {
-	        if(HwDeviceExtension->jChipType < SIS_315H) {
-	           if((resinfo == 0x0c) || (resinfo == 0x0d))  /* 720 */
-		       tempal = 6;
-	        } else {
-		   if((resinfo == 0x0d) || (resinfo == 0x0e))  /* 720 */
-		       tempal = 6;
-		}
+	       if((resinfo == SIS_RI_720x480) ||
+	          (resinfo == SIS_RI_720x576) ||
+		  (resinfo == SIS_RI_768x576))
+		  tempal = 6;
 	    }
 	}
 
@@ -3988,8 +4092,9 @@
   USHORT RefreshRateTableIndex,i,backup_i;
   USHORT modeflag,index,temp,backupindex;
 
-  if(SiS_Pr->UseCustomMode) return 0;
-  
+  /* Do NOT check for UseCustomMode here, will skrew up FIFO */
+  if(ModeNo == 0xfe) return 0;
+
   if(ModeNo <= 0x13)
     	modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
   else
@@ -4003,7 +4108,7 @@
 
   if(ModeNo < 0x14) return(0xFFFF);
 
- /* TW: CR33 holds refresh rate index for CRT1 [3:0] and CRT2 [7:4].
+ /* CR33 holds refresh rate index for CRT1 [3:0] and CRT2 [7:4].
   *     On LVDS machines, CRT2 index is always 0 and will be
   *     set to 0 by the following code; this causes the function
   *     to take the first non-interlaced mode in SiS_Ext2Struct
@@ -4017,40 +4122,38 @@
   if(index > 0) index--;
 
   if(SiS_Pr->SiS_SetFlag & ProgrammingCRT2) {
-      if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
+     if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
         if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA))  index = 0;
-      } else {
+     } else {
         if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
-	    if(SiS_Pr->SiS_VBType & VB_NoLCD)
-	       	    index = 0;
-	    else if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)
-	    	    index = backupindex = 0;
+	   if(SiS_Pr->SiS_VBType & VB_NoLCD)
+	      index = 0;
+	   else if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)
+	      index = backupindex = 0;
 	}
-      }
-  }
+     }
 
-  if(SiS_Pr->SiS_SetFlag & ProgrammingCRT2) {
-    	if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
-      		if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
-        		index = 0;
-      		}
-    	}
-    	if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
-      		if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
-			if(!(SiS_Pr->SiS_VBType & VB_NoLCD)) {
-           		   temp = LCDRefreshIndex[SiS_Pr->SiS_LCDResInfo];
-        		   if(index > temp) index = temp;
-			}
-      		} else {
-        		index = 0;
-      		}
-    	}
+     if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
+        if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+           index = 0;
+        }
+     }
+     if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
+        if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
+	   if(!(SiS_Pr->SiS_VBType & VB_NoLCD)) {
+              temp = LCDRefreshIndex[SiS_Pr->SiS_LCDResInfo];
+              if(index > temp) index = temp;
+	   }
+      	} else {
+           index = 0;
+      	}
+     }
   }
 
   RefreshRateTableIndex = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].REFindex;
   ModeNo = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].ModeID;
 
-  /* TW: 650/LVDS 1.10.07, 650/30xLV 1.10.6s */
+  /* 650/LVDS 1.10.07, 650/30xLV 1.10.6s */
   if(HwDeviceExtension->jChipType >= SIS_315H) {
      if(!(SiS_Pr->SiS_VBInfo & DriverMode)) {
         if( (SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_VESAID == 0x105) ||
@@ -4085,7 +4188,7 @@
     	backup_i = i;
     	if (!(SiS_AdjustCRT2Rate(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,
 	                             RefreshRateTableIndex,&i,HwDeviceExtension))) {
-		/* TW: This is for avoiding random data to be used; i is
+		/* This is for avoiding random data to be used; i is
 		 *     in an undefined state if no matching CRT2 mode is
 		 *     found.
 		 */
@@ -4104,42 +4207,45 @@
   USHORT tempax,tempbx,resinfo;
   USHORT modeflag,infoflag;
 
-  if (ModeNo <= 0x13)
+  if(ModeNo <= 0x13) {
     	modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
-  else
+	resinfo = 0;
+  } else {
     	modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
+        resinfo = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO;
+  }
 
-  resinfo = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO;
   tempbx = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex + (*i)].ModeID;
 
   tempax = 0;
+
   if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
+
     	if(SiS_Pr->SiS_VBInfo & SetCRT2ToRAMDAC) {
       		tempax |= SupportRAMDAC2;
 		if(HwDeviceExtension->jChipType >= SIS_315H) {
-		    tempax |= SupportTV;
-		    if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-		        if(!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
-			    if(resinfo == 0x0a) tempax |= SupportTV1024;
-			}
-		    }
+		   tempax |= SupportTV;
+		   if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
+		      if(!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
+			 if(resinfo == SIS_RI_1600x1200) tempax |= SupportTV1024;
+		      }
+		   }
 		}
-    	}
-    	if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
+    	} else if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
       		tempax |= SupportLCD;
 		if(HwDeviceExtension->jChipType >= SIS_315H) {
                    if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1600x1200) {
 		      if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1400x1050) {
-		         if((resinfo == 6) && (SiS_Pr->SiS_LCDInfo & DontExpandLCD)) {
+		         if((resinfo == SIS_RI_640x480) && (SiS_Pr->SiS_LCDInfo & DontExpandLCD)) {
 			    (*i) = 0;
                             return(1);
 		         } else {
       		            if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1280x1024) {
         		       if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1280x960) {
-           			  if((resinfo == 6) && (SiS_Pr->SiS_LCDInfo & DontExpandLCD)) {
+           			  if((resinfo == SIS_RI_640x480) && (SiS_Pr->SiS_LCDInfo & DontExpandLCD)) {
 				     return(0);
 				  } else {
-             			     if((resinfo >= 9) && (resinfo != 0x14)) {
+             			     if((resinfo >= SIS_RI_1280x1024) && (resinfo != SIS_RI_1280x768)) {
                				return(0);
              			     }
            			  }
@@ -4150,150 +4256,150 @@
       		   }
 		} else {
 		  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x600) {
-		     if((resinfo != 0x0f) && ((resinfo == 4) || (resinfo >= 8))) return(0);
+		     if( (resinfo != SIS_RI_1024x600) &&
+		         ((resinfo == SIS_RI_512x384) || (resinfo >= SIS_RI_1024x768))) return(0);
 		  } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1152x768) {
-		     if((resinfo != 0x10) && (resinfo > 8)) return(0);
+		     if((resinfo != SIS_RI_1152x768) && (resinfo > SIS_RI_1024x768)) return(0);
 		  } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x960) {
-		     if((resinfo != 0x0e) && (resinfo > 8)) return(0);
+		     if((resinfo != SIS_RI_1280x960) && (resinfo > SIS_RI_1024x768)) return(0);
 		  } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
-		     if(resinfo > 9) return(0);
+		     if(resinfo > SIS_RI_1280x1024) return(0);
 		  } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
-		     if(resinfo > 8) return(0);
+		     if(resinfo > SIS_RI_1024x768) return(0);
 		  } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600) {
-		     if((resinfo == 4) || (resinfo > 7)) return(0);
+		     if((resinfo == SIS_RI_512x384) || (resinfo > SIS_RI_800x600)) return(0);
 		  } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480) {
-		     if((resinfo == 4) || (resinfo == 3) || (resinfo > 6)) return(0);
+		     if((resinfo == SIS_RI_512x384) ||
+		        (resinfo == SIS_RI_400x300) ||
+			(resinfo > SIS_RI_640x480)) return(0);
 		  }
 		}
-    	}
-    	if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) { 
+    	} else if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) {
 	        if(SiS_Pr->SiS_HiVision == 3) {
 		      	tempax |= SupportHiVisionTV2;
       			if(SiS_Pr->SiS_VBInfo & SetInSlaveMode){
-        			if(resinfo == 4) return(0);
-        			if(resinfo == 3) return(0);
-				if(resinfo == 7) {
+        			if(resinfo == SIS_RI_512x384) return(0);
+        			if(resinfo == SIS_RI_400x300) return(0);
+				if(resinfo == SIS_RI_800x600) {
 	          			if(SiS_Pr->SiS_SetFlag & TVSimuMode) return(0);
         			}
-        			if(resinfo > 7) return(0);
+        			if(resinfo > SIS_RI_800x600) return(0);
 			}
 		} else {  
       			tempax |= SupportHiVisionTV;
       			if(SiS_Pr->SiS_VBInfo & SetInSlaveMode){
-        			if(resinfo == 4) return(0);
-        			if((resinfo == 3) || (resinfo == 7)) {
+        			if(resinfo == SIS_RI_512x384) return(0);
+        			if((resinfo == SIS_RI_400x300) || (resinfo == SIS_RI_800x600)) {
 	          			if(SiS_Pr->SiS_SetFlag & TVSimuMode) return(0);
         			}
-        			if(resinfo > 7) return(0);
+        			if(resinfo > SIS_RI_800x600) return(0);
 			}
 		}
-    	} else {
-      	   if(SiS_Pr->SiS_VBInfo & (SetCRT2ToAVIDEO|SetCRT2ToSVIDEO|SetCRT2ToSCART)) {
+    	} else if(SiS_Pr->SiS_VBInfo & (SetCRT2ToAVIDEO|SetCRT2ToSVIDEO|SetCRT2ToSCART)) {
         	tempax |= SupportTV;
 		tempax |= SupportTV1024;
 		if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-		    if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
-		        if((SiS_Pr->SiS_VBInfo & SetNotSimuMode) && (SiS_Pr->SiS_VBInfo & SetPALTV)) {
-			     if(resinfo != 8) {
-			         if( (!(SiS_Pr->SiS_VBInfo & SetPALTV)) ||
-				     ((SiS_Pr->SiS_VBInfo & SetPALTV) && (resinfo != 4)) ) {
-				     tempax &= ~(SupportTV1024);
-				     if(HwDeviceExtension->jChipType >= SIS_315H) {
-                                         if((modeflag & NoSupportSimuTV) && (SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
-				             if( (!(SiS_Pr->SiS_VBInfo & SetPALTV)) ||
-			                         ((SiS_Pr->SiS_VBInfo & SetPALTV) && (resinfo != 7)) ) {
-			                         if(!(SiS_Pr->SiS_VBInfo & SetNotSimuMode)) return(0);
-		                             }
-				         }
-		                     } else {
-				         if( (resinfo != 3) ||
-					     (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) ||
-					     (SiS_Pr->SiS_VBInfo & SetNotSimuMode) ) {
-					     if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) {
-						 if((modeflag & NoSupportSimuTV) && (SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
-						     if(resinfo == 3) return(0);
-						     if(!(SiS_Pr->SiS_VBInfo & SetNotSimuMode)) return (0);
-						 }
-		                             }
-                                         } else return(0);
-				     }
-				 }
-			     }
-			} else {
-			    tempax &= ~(SupportTV1024);
-			    if(HwDeviceExtension->jChipType >= SIS_315H) {
-			        if((modeflag & NoSupportSimuTV) && (SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
-			            if( (!(SiS_Pr->SiS_VBInfo & SetPALTV)) ||
-			                ((SiS_Pr->SiS_VBInfo & SetPALTV) && (resinfo != 7)) ) {
+		   if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
+		      if((SiS_Pr->SiS_VBInfo & SetNotSimuMode) && (SiS_Pr->SiS_VBInfo & SetPALTV)) {
+		         if(resinfo != SIS_RI_1024x768) {
+			    if( (!(SiS_Pr->SiS_VBInfo & SetPALTV)) ||
+			        ((SiS_Pr->SiS_VBInfo & SetPALTV) && (resinfo != SIS_RI_512x384)) ) {
+			       tempax &= ~(SupportTV1024);
+			       if(HwDeviceExtension->jChipType >= SIS_315H) {
+                                  if((modeflag & NoSupportSimuTV) && (SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
+			             if( (!(SiS_Pr->SiS_VBInfo & SetPALTV)) ||
+			                 ((SiS_Pr->SiS_VBInfo & SetPALTV) && (resinfo != SIS_RI_800x600)) ) {
 			                if(!(SiS_Pr->SiS_VBInfo & SetNotSimuMode)) return(0);
-		                    }
-		                }
-			    } else {
-			        if( (resinfo != 3) ||
-				    (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) ||
-				    (SiS_Pr->SiS_VBInfo & SetNotSimuMode) ) {
-				     if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) {
-					 if((modeflag & NoSupportSimuTV) && (SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
-					     if(resinfo == 3) return(0);
-					     if(!(SiS_Pr->SiS_VBInfo & SetNotSimuMode)) return (0);
-					 }
 		                     }
-                                } else return(0);
-                            }
-			}
-		    } else {  /* slavemode */
-			if(resinfo != 8) {
-			    if( (!(SiS_Pr->SiS_VBInfo & SetPALTV)) ||
-			        ((SiS_Pr->SiS_VBInfo & SetPALTV) && (resinfo != 4) ) ) {
-				 tempax &= ~(SupportTV1024);
-				 if(HwDeviceExtension->jChipType >= SIS_315H) {
-				     if((modeflag & NoSupportSimuTV) && (SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
-				         if( (!(SiS_Pr->SiS_VBInfo & SetPALTV)) ||
-			                     ((SiS_Pr->SiS_VBInfo & SetPALTV) && (resinfo != 7)) ) {
-			                     if(!(SiS_Pr->SiS_VBInfo & SetNotSimuMode))  return(0);
-		                         }
+				  }
+		               } else {
+				  if( (resinfo != SIS_RI_400x300) ||
+				      (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) ||
+				      (SiS_Pr->SiS_VBInfo & SetNotSimuMode) ) {
+				     if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) {
+					if((modeflag & NoSupportSimuTV) && (SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
+					   if(resinfo == SIS_RI_400x300) return(0);
+					   if(!(SiS_Pr->SiS_VBInfo & SetNotSimuMode)) return (0);
+					}
 		                     }
-			        } else {
-				    if( (resinfo != 3) ||
-				        (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) ||
-				        (SiS_Pr->SiS_VBInfo & SetNotSimuMode) ) {
-				         if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) {
-					     if((modeflag & NoSupportSimuTV) && (SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
-					         if(resinfo == 3) return(0);
-					         if(!(SiS_Pr->SiS_VBInfo & SetNotSimuMode)) return (0);
-					     }
-		                         }
-                                    } else return(0);
-				}
+                                  } else return(0);
+			       }
 			    }
-			}
-		    }
-		} else {   /* 301 */
-		    tempax &= ~(SupportTV1024);
-		    if(HwDeviceExtension->jChipType >= SIS_315H) {
-		        if((modeflag & NoSupportSimuTV) && (SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
-		            if( (!(SiS_Pr->SiS_VBInfo & SetPALTV)) ||
-			        ((SiS_Pr->SiS_VBInfo & SetPALTV) && (resinfo != 7)) ) {
-			        if(!(SiS_Pr->SiS_VBInfo & SetNotSimuMode)) return(0);
-		            }
-		        }
-		    } else {
-		        if( (resinfo != 3) ||
-			    (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) ||
-			    (SiS_Pr->SiS_VBInfo & SetNotSimuMode) ) {
-			    if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) {
-			        if((modeflag & NoSupportSimuTV) && (SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
-				    if(resinfo == 3) return(0);
-				    if(!(SiS_Pr->SiS_VBInfo & SetNotSimuMode)) return (0);
-				}
+			 }
+		      } else {
+			 tempax &= ~(SupportTV1024);
+			 if(HwDeviceExtension->jChipType >= SIS_315H) {
+			    if((modeflag & NoSupportSimuTV) && (SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
+			       if( (!(SiS_Pr->SiS_VBInfo & SetPALTV)) ||
+			           ((SiS_Pr->SiS_VBInfo & SetPALTV) && (resinfo != SIS_RI_800x600)) ) {
+			          if(!(SiS_Pr->SiS_VBInfo & SetNotSimuMode)) return(0);
+		               }
 		            }
-                        } else return(0);
-		    }
-		}
-           }
-    	}
-	
-  } else {	/* TW: for LVDS  */
+			 } else {
+			    if( (resinfo != SIS_RI_400x300) ||
+			        (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) ||
+				(SiS_Pr->SiS_VBInfo & SetNotSimuMode) ) {
+			       if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) {
+				  if((modeflag & NoSupportSimuTV) && (SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
+				     if(resinfo == SIS_RI_400x300) return(0);
+				     if(!(SiS_Pr->SiS_VBInfo & SetNotSimuMode)) return(0);
+				  }
+		               }
+                            } else return(0);
+                         }
+		      }
+		   } else {  /* slavemode */
+		      if(resinfo != SIS_RI_1024x768) {
+			 if( (!(SiS_Pr->SiS_VBInfo & SetPALTV)) ||
+			     ((SiS_Pr->SiS_VBInfo & SetPALTV) && (resinfo != SIS_RI_512x384) ) ) {
+			    tempax &= ~(SupportTV1024);
+			    if(HwDeviceExtension->jChipType >= SIS_315H) {
+			       if((modeflag & NoSupportSimuTV) && (SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
+			          if( (!(SiS_Pr->SiS_VBInfo & SetPALTV)) ||
+			              ((SiS_Pr->SiS_VBInfo & SetPALTV) && (resinfo != SIS_RI_800x600)) ) {
+			             if(!(SiS_Pr->SiS_VBInfo & SetNotSimuMode))  return(0);
+		                  }
+		               }
+			    } else {
+			       if( (resinfo != SIS_RI_400x300) ||
+			           (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) ||
+			           (SiS_Pr->SiS_VBInfo & SetNotSimuMode) ) {
+			          if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) {
+				     if((modeflag & NoSupportSimuTV) && (SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
+				        if(resinfo == SIS_RI_400x300) return(0);
+				        if(!(SiS_Pr->SiS_VBInfo & SetNotSimuMode)) return(0);
+				     }
+		                  }
+                               } else return(0);
+			    }
+		  	 }
+		      }
+		   }
+	        } else {   /* 301 */
+		   tempax &= ~(SupportTV1024);
+		   if(HwDeviceExtension->jChipType >= SIS_315H) {
+		      if((modeflag & NoSupportSimuTV) && (SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
+		         if( (!(SiS_Pr->SiS_VBInfo & SetPALTV)) ||
+		             ((SiS_Pr->SiS_VBInfo & SetPALTV) && (resinfo != SIS_RI_800x600)) ) {
+		            if(!(SiS_Pr->SiS_VBInfo & SetNotSimuMode)) return(0);
+		         }
+		      }
+		   } else {
+		      if( (resinfo != SIS_RI_400x300) ||
+			  (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) ||
+			  (SiS_Pr->SiS_VBInfo & SetNotSimuMode) ) {
+		         if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) {
+			    if((modeflag & NoSupportSimuTV) && (SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
+			       if(resinfo == SIS_RI_400x300) return(0);
+			       if(!(SiS_Pr->SiS_VBInfo & SetNotSimuMode)) return (0);
+			    }
+		         }
+                      } else return(0);
+		   }
+	        }
+        }
+
+  } else {	/* for LVDS  */
 
     	if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
       		if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
@@ -4303,24 +4409,29 @@
     	if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
       		tempax |= SupportLCD;
 		if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768) {
-		     if((resinfo != 0x14) && (resinfo > 0x09)) return(0);
+		     if((resinfo != SIS_RI_1280x768) && (resinfo >= SIS_RI_1280x1024)) return(0);
 		} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x600) {
-		     if((resinfo != 0x0f) && (resinfo > 0x08)) return(0);
+		     if((resinfo != SIS_RI_1024x600) && (resinfo >= SIS_RI_1024x768))  return(0);
 		} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1152x768) {
-		     if((resinfo != 0x10) && (resinfo > 0x08)) return(0);
+		     if((resinfo != SIS_RI_1152x768) && (resinfo > SIS_RI_1024x768))   return(0);
 		} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) {
-		     if((resinfo != 0x15) && (resinfo > 0x09)) return(0);
+		     if((resinfo != SIS_RI_1400x1050) && (resinfo > SIS_RI_1280x1024)) return(0);
+		} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) {
+                     if(resinfo > SIS_RI_1600x1200) return(0);
 		} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
-                     if(resinfo > 0x09) return(0);
+                     if(resinfo > SIS_RI_1280x1024) return(0);
                 } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
-		     if(resinfo > 0x08) return(0);
+		     if(resinfo > SIS_RI_1024x768)  return(0);
 		} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600){
-		     if(resinfo > 0x07) return(0);
-		     if(resinfo == 0x04) return(0);
+		     if(resinfo > SIS_RI_800x600)   return(0);
+		     if(resinfo == SIS_RI_512x384)  return(0);
+		} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_PanelBarco1366) {
+                     if((resinfo != SIS_RI_1360x1024) && (resinfo > SIS_RI_1280x1024)) return(0);
 		}
     	}
   }
-  /* TW: Look backwards in table for matching CRT2 mode */
+  
+  /* Look backwards in table for matching CRT2 mode */
   for(; SiS_Pr->SiS_RefIndex[RefreshRateTableIndex+(*i)].ModeID == tempbx; (*i)--) {
      	infoflag = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex + (*i)].Ext_InfoFlag;
      	if(infoflag & tempax) {
@@ -4328,7 +4439,7 @@
      	}
      	if ((*i) == 0) break;
   }
-  /* TW: Look through the whole mode-section of the table from the beginning
+  /* Look through the whole mode-section of the table from the beginning
    *     for a matching CRT2 mode if no mode was found yet.
    */
   for((*i) = 0; ; (*i)++) {
@@ -4348,7 +4459,7 @@
 {
   USHORT temp1,temp2;
 
-  /* TW: We store CRT1 ModeNo in CR34 */
+  /* We store CRT1 ModeNo in CR34 */
   SiS_SetReg1(SiS_Pr->SiS_P3d4,0x34,ModeNo);
   temp1 = (SiS_Pr->SiS_VBInfo & SetInSlaveMode) >> 8;
   temp2 = ~(SetInSlaveMode >> 8);
@@ -4364,16 +4475,16 @@
   USHORT modeflag, resinfo=0;
   UCHAR  OutputSelect = *SiS_Pr->pSiS_OutputSelect;
 
-  if(SiS_Pr->UseCustomMode) {
-        modeflag = SiS_Pr->CModeFlag;
+  if(ModeNo <= 0x13) {
+     modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
   } else {
-    if (ModeNo <= 0x13)
-    	modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
-    else {
+     if(SiS_Pr->UseCustomMode) {
+        modeflag = SiS_Pr->CModeFlag;
+     } else {
    	modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
 	resinfo = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO;
-    }
-  }    
+     }
+  }
 
   SiS_Pr->SiS_SetFlag = 0;
 
@@ -4383,22 +4494,24 @@
   if(SiS_BridgeIsOn(SiS_Pr,BaseAddr,HwDeviceExtension) == 0) {  
     	temp = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x30);
 #if 0	
-	/* SiS_HiVision is only used on 310/325/330+30xLV */
+	/* SiS_HiVision is only used on 315/330+30xLV */
 	if(SiS_Pr->SiS_VBType & (VB_SIS301LV302LV)) {
-	   if(SiS_Pr->SiS_HiVision & 0x03) {	/* TW: New from 650/30xLV 1.10.6s */
+	   if(SiS_Pr->SiS_HiVision & 0x03) {	/* New from 650/30xLV 1.10.6s */
 	      temp &= (SetCRT2ToHiVisionTV | SwitchToCRT2 | SetSimuScanMode); 	/* 0x83 */
 	      temp |= SetCRT2ToHiVisionTV;   					/* 0x80 */
 	   }
-	   if(SiS_Pr->SiS_HiVision & 0x04) {	/* TW: New from 650/30xLV 1.10.6s */
+	   if(SiS_Pr->SiS_HiVision & 0x04) {	/* New from 650/30xLV 1.10.6s */
 	      temp &= (SetCRT2ToHiVisionTV | SwitchToCRT2 | SetSimuScanMode); 	/* 0x83 */
 	      temp |= SetCRT2ToSVIDEO;  					/* 0x08 */
 	   }
 	}
-#endif	
+#endif
+#if 0
     	if(SiS_Pr->SiS_IF_DEF_FSTN) {   /* fstn must set CR30=0x21 */
        		temp = (SetCRT2ToLCD | SetSimuScanMode);
        		SiS_SetReg1(SiS_Pr->SiS_P3d4,0x30,temp);
     	}
+#endif	
     	tempbx |= temp;
     	tempax = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x31) << 8;
         tempax &= (LoadDACFlag | DriverMode | SetDispDevSwitch | SetNotSimuMode | SetPALTV);
@@ -4538,7 +4651,7 @@
       		tempbx |= SetSimuScanMode;
     	}
 
-	/* TW: LVDS (LCD/TV) and 301BDH (LCD) can only be slave in 8bpp modes */
+	/* LVDS (LCD/TV) and 301BDH (LCD) can only be slave in 8bpp modes */
 	if(SiS_Pr->SiS_ModeType <= ModeVGA) {
 	   if( (SiS_Pr->SiS_IF_DEF_LVDS == 1) ||
 	       ((tempbx & SetCRT2ToLCD) && (SiS_Pr->SiS_VBType & VB_NoLCD)) ) {
@@ -4546,12 +4659,12 @@
 	   }
 	}
 	
-    	if(!(tempbx & SetSimuScanMode)){
+    	if(!(tempbx & SetSimuScanMode)) {
       	    if(tempbx & SwitchToCRT2) {
         	if((!(modeflag & CRT2Mode)) && (checkcrt2mode)) {
 		     if( (HwDeviceExtension->jChipType >= SIS_315H) &&
 			 (SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) ) {
-			 if(resinfo != 0x0a)
+			 if(resinfo != SIS_RI_1600x1200)
                               tempbx |= SetSimuScanMode;
 		     } else {
             		      tempbx |= SetSimuScanMode;
@@ -4574,7 +4687,7 @@
           	    if((!(modeflag & CRT2Mode)) && (checkcrt2mode)) {
 	                if( (HwDeviceExtension->jChipType >= SIS_315H) &&
 			    (SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) ) {
-			     if(resinfo != 0x0a) {  /* TW: 650/301 BIOS */
+			     if(resinfo != SIS_RI_1600x1200) {  /* 650/301 BIOS */
 			          tempbx |= SetInSlaveMode;
             		          if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
               		 	     if(tempbx & SetCRT2ToTV) {
@@ -4582,7 +4695,7 @@
 					     SiS_Pr->SiS_SetFlag |= TVSimuMode;
               			     }
                                   }
-			     }                      /* TW: 650/301 BIOS */
+			     }                      /* 650/301 BIOS */
 		        } else {
             		    tempbx |= SetInSlaveMode;
             		    if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
@@ -4640,7 +4753,7 @@
      	if(HwDeviceExtension->jChipType >= SIS_315H) {
 	        if(ROMAddr && SiS_Pr->SiS_UseROM) {
 		    OutputSelect = ROMAddr[0xf3];
-		    if(HwDeviceExtension->jChipType == SIS_330) {
+		    if(HwDeviceExtension->jChipType >= SIS_330) {
 			OutputSelect = ROMAddr[0x11b];
 		    }
                 }
@@ -4671,7 +4784,7 @@
   SiS_Pr->SiS_VBInfo = tempbx;
 
   if(HwDeviceExtension->jChipType == SIS_630) {
-       SiS_WhatIsThis(SiS_Pr, SiS_Pr->SiS_VBInfo);
+       SiS_SetChrontelGPIO(SiS_Pr, SiS_Pr->SiS_VBInfo);
   }
 
 #ifdef TWDEBUG
@@ -4685,61 +4798,35 @@
 #endif
 #endif
 
-#if 0  /* TW: Incomplete! (But does not seem to be required) */
-  if(HwDeviceExtension->jChipType < SIS_315H) {
-     /* TW: From A901/630+301B BIOS */
-     if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
-         if(SiS_GetReg1(SiS_Pr->SiS_P3c4,0x13) & 0x80)
-     }
-     if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD)) {
-         if(SiS_GetReg1(SiS_Pr->SiS_P3c4,0x13) & 0x80)
-	     if( [si] == 3) ModeIdIndex = 0x3f2b;
-	 }
-     }
-     SiS_SetRegAND(SiS_Pr->SiS_P3d4, 0x31, 0xF7);
-     if(ModeNo == 0x13) bp+4 = 0x03;
-  } else {
-     /* From 650/30xLV BIOS: */
-     SiS_SetRegAND(SiS_Pr->SiS_P3d4, 0x31, 0xF7);
-     if(ModeNo == 0x13) bp+4 = 0x03;
-     else bp+4 = ModeNo;
-  }
-#endif
-
-  /* TW: 630/301B and 650/301 (not 301LV!) BIOSes do more here, but this seems for DOS mode */
-
 }
 
+/* Setup general purpose IO for Chrontel communication */
 void
-SiS_WhatIsThis(SiS_Private *SiS_Pr, USHORT myvbinfo)
+SiS_SetChrontelGPIO(SiS_Private *SiS_Pr, USHORT myvbinfo)
 {
-   unsigned long eax, temp;
-   unsigned short temp1;
+   unsigned long  acpibase;
+   unsigned short temp;
 
    if(!(SiS_Pr->SiS_ChSW)) return;
 
 #ifndef LINUX_XF86
-   SiS_SetReg4(0xcf8,0x80000874);
-   eax = SiS_GetReg3(0xcfc);
+   SiS_SetReg4(0xcf8,0x80000874);		   /* get ACPI base */
+   acpibase = SiS_GetReg3(0xcfc);
 #else
-   eax = pciReadLong(0x00000800, 0x74);
+   acpibase = pciReadLong(0x00000800, 0x74);
 #endif
-   eax &= 0xFFFF;
-   temp = eax;
-   eax += 0x3c;
-   temp1 = SiS_GetReg4((USHORT)eax);
-   temp1 &= 0xFEFF;
-   SiS_SetReg5((USHORT)eax, temp1);
-   temp1 = SiS_GetReg4((USHORT)eax);
-   eax = temp;
-   eax += 0x3a;
-   temp1 = SiS_GetReg4((USHORT)eax);
-   temp1 &= 0xFEFF;
+   acpibase &= 0xFFFF;
+   temp = SiS_GetReg4((USHORT)(acpibase + 0x3c));  /* ACPI register 0x3c: GP Event 1 I/O mode select */
+   temp &= 0xFEFF;
+   SiS_SetReg5((USHORT)(acpibase + 0x3c), temp);
+   temp = SiS_GetReg4((USHORT)(acpibase + 0x3c));
+   temp = SiS_GetReg4((USHORT)(acpibase + 0x3a));  /* ACPI register 0x3a: GP Pin Level (low/high) */
+   temp &= 0xFEFF;
    if(!(myvbinfo & SetCRT2ToTV)) {
-      temp1 |= 0x0100;
+      temp |= 0x0100;
    }
-   SiS_SetReg5((USHORT)eax, temp1);
-   temp1 = SiS_GetReg4((USHORT)eax);
+   SiS_SetReg5((USHORT)(acpibase + 0x3a), temp);
+   temp = SiS_GetReg4((USHORT)(acpibase + 0x3a));
 }
 
 void
@@ -4750,14 +4837,14 @@
   USHORT temp1=0,modeflag=0,tempcx=0;
   USHORT StandTableIndex,CRT1Index;
 #ifdef SIS315H   
-  USHORT ResInfo,DisplayType,temp=0;
+  USHORT ResIndex,DisplayType,temp=0;
   const  SiS_LVDSCRT1DataStruct *LVDSCRT1Ptr = NULL;
 #endif
 
   SiS_Pr->SiS_RVBHCMAX  = 1;
   SiS_Pr->SiS_RVBHCFACT = 1;
 
-  if(ModeNo <= 0x13){
+  if(ModeNo <= 0x13) {
 
     	modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
     	StandTableIndex = SiS_GetModePtr(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex);
@@ -4771,7 +4858,7 @@
 
 #ifdef SIS315H     
     	temp = SiS_GetLVDSCRT1Ptr(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,
-			RefreshRateTableIndex,&ResInfo,&DisplayType);
+			RefreshRateTableIndex,&ResIndex,&DisplayType);
 
     	if(temp == 0)  return;
 
@@ -4814,16 +4901,16 @@
 		case 99: LVDSCRT1Ptr = SiS_Pr->SiS_CHTVCRT1OPAL;                break;
 		default: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11024x768_1;          break;
     	}
-	tempax = (LVDSCRT1Ptr+ResInfo)->CR[0];
-	tempax |= (LVDSCRT1Ptr+ResInfo)->CR[14] << 8;
+	tempax = (LVDSCRT1Ptr+ResIndex)->CR[0];
+	tempax |= (LVDSCRT1Ptr+ResIndex)->CR[14] << 8;
 	tempax &= 0x03FF;
-    	tempbx = (LVDSCRT1Ptr+ResInfo)->CR[6];
-    	tempcx = (LVDSCRT1Ptr+ResInfo)->CR[13] << 8;
+    	tempbx = (LVDSCRT1Ptr+ResIndex)->CR[6];
+    	tempcx = (LVDSCRT1Ptr+ResIndex)->CR[13] << 8;
     	tempcx &= 0x0100;
     	tempcx <<= 2;
     	tempbx |= tempcx;
-	temp1  = (LVDSCRT1Ptr+ResInfo)->CR[7];
-#endif	
+	temp1  = (LVDSCRT1Ptr+ResIndex)->CR[7];
+#endif
 
     } else {
 
@@ -4859,11 +4946,12 @@
   if(modeflag & Charx8Dot) tempax *= 8;
   else                     tempax *= 9;
 
-  /* TW: From 650/30xLV 1.10.6s */
+  /* From 650/30xLV 1.10.6s */
   if(modeflag & HalfDCLK)  tempax <<= 1;
 
-  SiS_Pr->SiS_VGAHT = SiS_Pr->SiS_HT = tempax;
   tempbx++;
+
+  SiS_Pr->SiS_VGAHT = SiS_Pr->SiS_HT = tempax;
   SiS_Pr->SiS_VGAVT = SiS_Pr->SiS_VT = tempbx;
 }
 
@@ -4871,18 +4959,18 @@
 SiS_UnLockCRT2(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,USHORT BaseAddr)
 {
   if(HwDeviceExtension->jChipType >= SIS_315H)
-    	SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2f,0x01);
+     SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2f,0x01);
   else
-    	SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x24,0x01);
+     SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x24,0x01);
 }
 
 void
 SiS_LockCRT2(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,USHORT BaseAddr)
 {
   if(HwDeviceExtension->jChipType >= SIS_315H)
-    	SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2F,0xFE);
+     SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2F,0xFE);
   else
-     	SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x24,0xFE);
+     SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x24,0xFE);
 }
 
 void
@@ -4891,7 +4979,6 @@
   SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x1E,0x20);
 }
 
-/* Checked against all BIOSes */
 void
 SiS_DisableBridge(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,USHORT BaseAddr)
 {
@@ -4901,9 +4988,9 @@
   USHORT temp=0;
   UCHAR *ROMAddr = HwDeviceExtension->pjVirtualRomBase;
 
-  if (SiS_Pr->SiS_IF_DEF_LVDS == 0) {
+  if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
 
-      if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {   /* ===== TW: For 30xB/LV ===== */
+      if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {   /* ===== For 30xB/LV ===== */
 
         if(HwDeviceExtension->jChipType < SIS_315H) {
 
@@ -4959,9 +5046,9 @@
 
         } else {
 
-#ifdef SIS315H	   /* 310/325 series */
+#ifdef SIS315H	   /* 315 series */
 
-           if(IS_SIS650740) {		/* 650, 740 */
+           if(IS_SIS550650740660) {		/* 550, 650, 740, 660 */
 
 #if 0
 	      if(SiS_GetReg1(SiS_Pr->SiS_Part4Port,0x00) != 1) return;	/* From 1.10.7w */
@@ -4985,22 +5072,35 @@
 	         SiS_SetReg3(SiS_Pr->SiS_P3c6,0x00);
 
                  pushax = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x06);
-		 
+
 		 if(IS_SIS740) {
 		    SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x06,0xE3);
 		 }
 
 	         SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 3);
-		 
-		 if(!(SiS_IsNotM650or651(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-	            tempah = 0xef;
-	            if(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-	               tempah = 0xf7;
-                    }
-	            SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x4c,tempah);
+
+		 if(!(IS_SIS740)) {
+		    if(!(SiS_IsNotM650or651(SiS_Pr,HwDeviceExtension, BaseAddr))) {
+	               tempah = 0xef;
+	               if(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) {
+	                  tempah = 0xf7;
+                       }
+	               SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x4c,tempah);
+		    }
 	         }
 
-              }
+              } else if(SiS_Pr->SiS_VBType & VB_NoLCD) {
+	         if(HwDeviceExtension->jChipType == SIS_650) {
+	            if(!(SiS_IsNotM650or651(SiS_Pr,HwDeviceExtension, BaseAddr))) {
+	               SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x4c,0xef);
+	            }
+		    if((!(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr))) ||
+		       (SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) ) {
+	     	       SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x26,0xFE,0x00);
+	            }
+		    SiS_SetPanelDelay(SiS_Pr, ROMAddr, HwDeviceExtension, 3);
+		 }
+	      }
 
               if(SiS_Pr->SiS_VBType & VB_SIS301B302B) {
 	         tempah = 0x3f;
@@ -5065,8 +5165,8 @@
 
 	      if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
 
-		 SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x1f,~0x10);    /* 1.10.8r */
-		 
+		 SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x1f,~0x10);    		/* 1.10.8r, 8m */
+
 	         tempah = 0x3f;
 	         if(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr)) {
 	            tempah = 0x7f;
@@ -5076,9 +5176,9 @@
 	         }
 	         SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x1F,tempah);
 
-		 if(SiS_IsNotM650or651(SiS_Pr,HwDeviceExtension, BaseAddr)) {   /* 1.10.8r */
+		 if(SiS_IsNotM650or651(SiS_Pr,HwDeviceExtension, BaseAddr)) {   /* 1.10.8r, 8m */
 	            SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2e,0x7f);
-		 }								/* 1.10.8r */
+		 }								/* 1.10.8r, 8m */
 
 	         if(!(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr))) {
 	            SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x00,0xdf);
@@ -5094,80 +5194,32 @@
 
 	         SiS_SetReg1(SiS_Pr->SiS_P3c4,0x06,pushax);
 
-  	      }
-
-#if 0
-          } else if(IS_SIS740) {	/* 740 */
-	  
-	     if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {   /* 30xLV */
-	     
-	        if( (!(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr))) ||
-		    (SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) ) {
-	     	      SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x26,0xFE,0x00);
-	        }
-		
-		SiS_SetReg3(SiS_Pr->SiS_P3c6,0x00);
-
-                pushax = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x06);
-		
-		SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x06,0xE3);
-
-	        SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 3);
-		
-		if(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-		   SiS_DisplayOff(SiS_Pr);
-	           SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 2);
-	           SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x32,0xDF);
-	           SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x1E,0xDF);
-		} else {
-		   SiS_DisplayOff(SiS_Pr);
-	           SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x00,0x80);
-	           SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 2);
-	           SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x32,0xDF);
-	           temp = SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x00);
-                   SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x00,0x10);
-	           SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x1E,0xDF);
-	           SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x00,temp);
-		}
-		
-		SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x1F,0x3F);
-		SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x1F,0xEF);  /* (from 650) */
-		
-		SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2e,0x7f);
-		
-		if(!(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-	           SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x00,0xdf);
-	        }
-		
-		if(!(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-		   if(!(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-	              if(!(SiS_CRT2IsLCD(SiS_Pr,BaseAddr,HwDeviceExtension))) {
-	                 if(!(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-		            SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x26,0xFD,0x00);
-                         }
-                      }
-	           }
-		}
-	        SiS_SetReg1(SiS_Pr->SiS_P3c4,0x06,pushax);
-	     
-	     } else {	/* (301,) 301B */
-	  
-	        if(SiS_Is301B(SiS_Pr,BaseAddr)) {
-	           SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x1F,0x3F);
-	        }
-	     
-	        SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x00,0xDF);
-	        SiS_DisplayOff(SiS_Pr);
-	        SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x00,0x80);
-	        SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x32,0xDF);
+  	      } else if(SiS_Pr->SiS_VBType & VB_NoLCD) {
+	         if(HwDeviceExtension->jChipType == SIS_650) {
+		    if((SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) ||
+		       (!(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr)))) {
+		       if((!(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr))) ||
+		          (!(SiS_CRT2IsLCD(SiS_Pr,BaseAddr,HwDeviceExtension)))) {
+			  SiS_SetPanelDelay(SiS_Pr, ROMAddr, HwDeviceExtension, 2);
+	     	          SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x26,0xFD,0x00);
+			  SiS_SetPanelDelay(SiS_Pr, ROMAddr, HwDeviceExtension, 4);
+	               }
+		    }
+		 }
+	      } else if(SiS_Pr->SiS_VBType & VB_SIS301B302B) {
+	         if(HwDeviceExtension->jChipType == SIS_650) {
+		    if(!(SiS_IsNotM650or651(SiS_Pr,HwDeviceExtension, BaseAddr))) {
+	               tempah = 0xef;
+	               if(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr)) {
+		          if(modenum > 0x13) {
+	                     tempah = 0xf7;
+			  }
+                       }
+	               SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x4c,tempah);
+		    }
+		 }
+	      }
 
-	        temp = SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x00);
-                SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x00,0x10);
-	        SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x1E,0xDF);
-	        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x00,temp);
-		
-	     }
-#endif	  
 	  } else {			/* 315, 330 - all bridge types */
 
 	     if(SiS_Is301B(SiS_Pr,BaseAddr)) {
@@ -5212,13 +5264,13 @@
 
 	}
 
-      } else {     /* ============ TW: For 301 ================ */
+      } else {     /* ============ For 301 ================ */
 
         if(HwDeviceExtension->jChipType < SIS_315H) {
-            if(SiS_CRT2IsLCD(SiS_Pr,BaseAddr,HwDeviceExtension)) {
-                SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x11,0xF0,0x0B);
-	        SiS_SetPanelDelay(SiS_Pr, ROMAddr, HwDeviceExtension, 1);
-	    }
+            if(!(SiS_CR36BIOSWord23b(SiS_Pr,HwDeviceExtension))) {
+	      SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x11,0xF7,0x08);
+	      SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 3);
+	   }
 	}
 
         SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x00,0xDF);           /* disable VB */
@@ -5237,18 +5289,23 @@
 	    SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x00,temp);
 	} else {
             SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x1E,0xDF);            /* disable CRT2 */
+	    if( (!(SiS_CRT2IsLCD(SiS_Pr,BaseAddr,HwDeviceExtension))) ||
+	        (!(SiS_CR36BIOSWord23d(SiS_Pr,HwDeviceExtension))) ) {
+		SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 2);
+		SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x11,0xFB,0x04);
+	    }
 	}
 
       }
 
-  } else {     /* ============ TW: For LVDS =============*/
+  } else {     /* ============ For LVDS =============*/
 
     if(HwDeviceExtension->jChipType < SIS_315H) {
 
 #ifdef SIS300	/* 300 series */
 
 	if(SiS_Pr->SiS_IF_DEF_CH70xx == 1) {
-	    SiS_SetCH700x(SiS_Pr,0x090E);
+	   SiS_SetCH700x(SiS_Pr,0x090E);
 	}
 
 	if(HwDeviceExtension->jChipType == SIS_730) {
@@ -5264,17 +5321,17 @@
 
 	      if(!(SiS_GetReg1(SiS_Pr->SiS_P3c4,0x13) & 0x40)) {
   
-  	          if(!(SiS_CR36BIOSWord23b(SiS_Pr,HwDeviceExtension))) {
+  	         if(!(SiS_CR36BIOSWord23b(SiS_Pr,HwDeviceExtension))) {
 
-                     SiS_WaitVBRetrace(SiS_Pr,HwDeviceExtension);
+                    SiS_WaitVBRetrace(SiS_Pr,HwDeviceExtension);
 
-		     if(!(SiS_GetReg1(SiS_Pr->SiS_P3c4,0x06) & 0x1c)) {
-		         SiS_DisplayOff(SiS_Pr);
-	             }
+		    if(!(SiS_GetReg1(SiS_Pr->SiS_P3c4,0x06) & 0x1c)) {
+		        SiS_DisplayOff(SiS_Pr);
+	            }
 
-	             SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x11,0xF7,0x08);
-	             SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 3);
-                  }
+	            SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x11,0xF7,0x08);
+	            SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 3);
+                 }
               }
 	   }
 	}
@@ -5298,29 +5355,40 @@
 
     } else {
 
-#ifdef SIS315H	/* 310/325 series */
+#ifdef SIS315H	/* 315 series */
 
 	if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
-		temp = SiS_GetCH701x(SiS_Pr,0x61);
-		if(temp < 1) {
-		   SiS_SetCH701x(SiS_Pr,0xac76);
-		   SiS_SetCH701x(SiS_Pr,0x0066);
-		}
-		
-		if(!(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr))) {
+
+		if(HwDeviceExtension->jChipType == SIS_740) {
+		   temp = SiS_GetCH701x(SiS_Pr,0x61);
+		   if(temp < 1) {
+		      SiS_SetCH701x(SiS_Pr,0xac76);
+		      SiS_SetCH701x(SiS_Pr,0x0066);
+		   }
+
+		   if(!(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr))) {
 			SiS_SetCH701x(SiS_Pr,0x3e49);
-		} else if(SiS_IsTVOrYPbPrOrScart(SiS_Pr,HwDeviceExtension, BaseAddr))  {
+		   } else if(SiS_IsTVOrYPbPrOrScart(SiS_Pr,HwDeviceExtension, BaseAddr))  {
 			SiS_SetCH701x(SiS_Pr,0x3e49);
+		   }
 		}
-		
+
 		if(!(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr))) {
 			SiS_Chrontel701xBLOff(SiS_Pr);
-			SiS_Chrontel701xOff(SiS_Pr);
+			SiS_Chrontel701xOff(SiS_Pr,HwDeviceExtension);
 		} else if(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) {
 			SiS_Chrontel701xBLOff(SiS_Pr);
-			SiS_Chrontel701xOff(SiS_Pr);
+			SiS_Chrontel701xOff(SiS_Pr,HwDeviceExtension);
 		}
-		
+
+		if(HwDeviceExtension->jChipType != SIS_740) {
+		   if(!(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr))) {
+			SiS_SetCH701x(SiS_Pr,0x0149);
+		   } else if(SiS_IsTVOrYPbPrOrScart(SiS_Pr,HwDeviceExtension, BaseAddr))  {
+			SiS_SetCH701x(SiS_Pr,0x0149);
+		   }
+		}
+
 	}
 
 	if(SiS_Pr->SiS_IF_DEF_CH70xx == 0) {
@@ -5344,6 +5412,10 @@
 		SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x00,0x80);
 	}
 
+	if(HwDeviceExtension->jChipType == SIS_740) {
+	   SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2e,0x7f);
+	}
+
 	SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x32,0xDF);
 
 	if(SiS_Pr->SiS_IF_DEF_CH70xx == 0) {
@@ -5356,15 +5428,27 @@
 
 	if(SiS_Pr->SiS_IF_DEF_CH70xx == 0) {
 	        if(SiS_CRT2IsLCD(SiS_Pr, BaseAddr,HwDeviceExtension)) {
-			SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x1e,0xdf);
+		   SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x1e,0xdf);
+		   if(HwDeviceExtension->jChipType == SIS_550) {
+		      SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x1e,0xbf);
+		      SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x1e,0xef);
+		   }
 		}
-	} else if(SiS_IsLCDOrLCDA(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-		SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x1e,0xdf);
+	} else {
+	   if(HwDeviceExtension->jChipType == SIS_740) {
+	        if(SiS_IsLCDOrLCDA(SiS_Pr,HwDeviceExtension, BaseAddr)) {
+		   SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x1e,0xdf);
+		}
+	   } else {
+	        if(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) {
+		   SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x1e,0xdf);
+	        }
+	   }
 	}
 
 	if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
 	    	if(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-			SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x13,0xff);
+			/* SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x13,0xff); */
 		} else {
 			SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x13,0xfb);
 		}
@@ -5372,7 +5456,10 @@
 
 	SiS_UnLockCRT2(SiS_Pr,HwDeviceExtension, BaseAddr);
 
-	if(SiS_Pr->SiS_IF_DEF_CH70xx == 0) {
+	if(HwDeviceExtension->jChipType == SIS_550) {
+	        SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x01,0x80);
+		SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x02,0x40);
+	} else if(SiS_Pr->SiS_IF_DEF_CH70xx == 0) {
 		SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2e,0xf7);
 	} else if(!(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr))) {
 		SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2e,0xf7);
@@ -5380,7 +5467,7 @@
 		SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2e,0xf7);
 	}
 
-#if 0  /* TW: BIOS code makes no sense */
+#if 0  /* BIOS code makes no sense */
        if(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) {
            if(!(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr))) {
 	        if(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr)) {
@@ -5406,7 +5493,6 @@
 
 }
 
-/* TW: Checked against all BIOSes */
 void
 SiS_EnableBridge(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension, USHORT BaseAddr)
 {
@@ -5419,7 +5505,7 @@
 
   if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
 
-    if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {   /* TW: ====== For 301B et al  ====== */
+    if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {   /* ====== For 301B et al  ====== */
 
       if(HwDeviceExtension->jChipType < SIS_315H) {
 
@@ -5516,9 +5602,9 @@
 
       } else {
 
-#ifdef SIS315H    /* 310/325 series */
+#ifdef SIS315H    /* 315 series */
 
-	 if(IS_SIS650740) {		/* 650 */
+	 if(IS_SIS550650740660) {		/* 550, 650, 740, 660 */
 
 #if 0
 	    if(SiS_GetReg1(SiS_Pr->SiS_Part4Port,0x00) != 1) return;	/* From 1.10.7w */
@@ -5560,7 +5646,32 @@
 		  delaylong = TRUE;
 	       }
 
-	    }
+	    } else if(SiS_Pr->SiS_VBType & VB_NoLCD) {
+	       if(HwDeviceExtension->jChipType == SIS_650) {
+	          if(!(SiS_IsNotM650or651(SiS_Pr,HwDeviceExtension, BaseAddr))) {
+	             SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x4c,0x10);
+	          }
+		  if( (SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) ||
+		      (SiS_CRT2IsLCD(SiS_Pr,BaseAddr,HwDeviceExtension)) ) {
+		     SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x26,0x02);
+		     SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 0);
+		  }
+	       }
+  	    } else if(SiS_Pr->SiS_VBType & VB_SIS301B302B) {
+
+	         if(HwDeviceExtension->jChipType == SIS_650) {
+		    if(!(SiS_IsNotM650or651(SiS_Pr,HwDeviceExtension, BaseAddr))) {
+	               tempah = 0x10;
+		       if(SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x13) & 0x04) {
+		          tempah = 0x18;
+		          if((SiS_GetReg1(SiS_Pr->SiS_Part2Port,0x00) & 0x0f) == 0x0c) {
+			     tempah = 0x08;
+			  }
+		       }
+	               SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x4c,tempah);
+		    }
+		 }
+	      }
 
 	    if(!(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr))) {
                temp = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x32) & 0xDF;
@@ -5614,11 +5725,24 @@
 	       SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x30,0x00);	/* All this from 1.10.7u */
 	       SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x27,0x0c);
 	       SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x30,0x20);
-	       SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x31,0x05);  
-	       SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x32,0x60);  
-	       SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x33,0x00);  
-	       SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x34,0x10); 
-	       SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x30,0x40); 
+
+	       SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x31,0x12);
+	       SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x32,0xd0);
+	       SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x33,0x6b);
+	       if((SiS_GetReg1(SiS_Pr->SiS_P3d4,0x36) & 0x0f) == 0x02) {
+	          SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x31,0x0d);
+	          SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x32,0x70);
+	          SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x33,0x40);
+		  if(((SiS_GetReg1(SiS_Pr->SiS_P3d4,0x36) & 0xf0) != 0x03)) {
+		     SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x31,0x05);
+	             SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x32,0x60);
+	             SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x33,0x33);  /* 00 */
+		  }
+	       }
+	       SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x34,0x10);
+	       if((SiS_GetReg1(SiS_Pr->SiS_P3d4,0x36) & 0x0f) != 0x03) {
+	          SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x30,0x40);
+	       }
 	       
 	       SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 2);
 	    
@@ -5629,127 +5753,44 @@
 	       if(!(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr))) {
 	          if( (SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) ||
 	              ((SiS_CRT2IsLCD(SiS_Pr,BaseAddr,HwDeviceExtension))) ) {
-		    SiS_SetPanelDelayLoop(SiS_Pr,ROMAddr, HwDeviceExtension, 3, 10);
-		    if(delaylong) {
+		     SiS_SetPanelDelayLoop(SiS_Pr,ROMAddr, HwDeviceExtension, 3, 10);
+		     if(delaylong) {
 			SiS_SetPanelDelayLoop(SiS_Pr,ROMAddr, HwDeviceExtension, 3, 10);
-		    }
-                    SiS_WaitVBRetrace(SiS_Pr,HwDeviceExtension);
-		    SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x26,0xfe,0x01);
-	         }
-	      }
+		     }
+                     SiS_WaitVBRetrace(SiS_Pr,HwDeviceExtension);
+		     SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x26,0xfe,0x01);
+	          }
+	       }
 
-	      SiS_SetReg1(SiS_Pr->SiS_P3c4,0x06,pushax);
-	      SiS_DisplayOn(SiS_Pr);
-	      SiS_SetReg3(SiS_Pr->SiS_P3c6,0xff);
+	       SiS_SetReg1(SiS_Pr->SiS_P3c4,0x06,pushax);
+	       SiS_DisplayOn(SiS_Pr);
+	       SiS_SetReg3(SiS_Pr->SiS_P3c6,0xff);
 
-	      if(!(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr))) {
+	       if(!(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr))) {
 	          SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x00,0x7f);
-	      }
+	       }
 #if 0
-              SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x30,0x00);
-	      SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x27,0x0c);
-	      SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x30,0x20);
-	      SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x31,0x05);   /* 1.10.8r: 0x0d */
-	      SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x32,0x60);   /* 1.10.8r: 0x70 */
-	      SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x33,0x00);   /* 1.10.8r: 0x40 */
-	      SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x34,0x10); 
-	      SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x30,0x40); 
+               SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x30,0x00);
+	       SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x27,0x0c);
+	       SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x30,0x20);
+	       SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x31,0x05);   /* 1.10.8r: 0x0d */
+	       SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x32,0x60);   /* 1.10.8r: 0x70 */
+	       SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x33,0x00);   /* 1.10.8r: 0x40 */
+	       SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x34,0x10);
+	       SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x30,0x40);
 #endif	      
+	    } if(SiS_Pr->SiS_VBType & VB_NoLCD) {
+	       if(HwDeviceExtension->jChipType == SIS_650) {
+	          if(!(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr))) {
+		     if( (SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) ||
+		         (SiS_CRT2IsLCD(SiS_Pr,BaseAddr,HwDeviceExtension)) ) {
+			SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 1);
+		        SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x26,0x01);
+		     }
+		  }
+	       }
+  	    }
 
-	  }
-
-#if 0
-         } else if(IS_SIS740) {		/* 740 */
-	 
-	   if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {  /* 30xLV */
-	   
-	      SiS_SetReg3(SiS_Pr->SiS_P3c6,0x00);
-	      SiS_DisplayOff(SiS_Pr);
-	      pushax = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x06);
-	      SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x06,0xE3);
-	      
-	      if( (SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) ||
-	          (SiS_CRT2IsLCD(SiS_Pr,BaseAddr,HwDeviceExtension)) ) {
-                   if(!(SiS_GetReg1(SiS_Pr->SiS_Part4Port,0x26) & 0x02)) {
-		      SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x26,0x02);
-	              SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 3);
-		   }
-	      }
-	      
-	      if(!(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-	         temp = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x32) & 0xDF;
-	         if(SiS_BridgeInSlave(SiS_Pr)) {
-                    tempah = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x30);
-                    if(!(tempah & SetCRT2ToRAMDAC))  temp |= 0x20;
-                 }
-                 SiS_SetReg1(SiS_Pr->SiS_P3c4,0x32,temp);
-
-	         SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x1E,0x20);  	
-		 SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 2);
-	      }
-	      
-	      if(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-	         SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x1E,0x20);
-	      }
-	      
-	      SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x00,0x1f,0x20);
-	      SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x1F,0xC0);
-	      SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x1F,0x10);  /* (taken from 650 1.10.8r) */
-	      SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2E,0x80);
-	      
-	      if(!(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-	         if( (SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) ||
-	             (SiS_CRT2IsLCD(SiS_Pr,BaseAddr,HwDeviceExtension)) ) {
-		    SiS_SetPanelDelayLoop(SiS_Pr,ROMAddr, HwDeviceExtension, 3, 10);
-		    if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x31) & 0x40) {
-		       SiS_WaitVBRetrace(SiS_Pr,HwDeviceExtension);
-		    }
-		    SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x26,0xfe,0x01);
-		    SiS_SetPanelDelayLoop(SiS_Pr,ROMAddr, HwDeviceExtension, 3, 10);
-		    SiS_WaitVBRetrace(SiS_Pr,HwDeviceExtension);
-	         }
-              }
-	      
-	      SiS_SetReg1(SiS_Pr->SiS_P3c4,0x06,pushax);
-	      SiS_DisplayOn(SiS_Pr);
-	      SiS_SetReg3(SiS_Pr->SiS_P3c6,0xff);
-	      
-	      if(!(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-	         SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x00,0x7f);
-	      }
-	   
-	   } else {	/* (301), 301B */
-	 
-	      if(!(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-	         temp = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x32) & 0xDF;
-	         if(SiS_BridgeInSlave(SiS_Pr)) {
-                    tempah = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x30);
-                    if(!(tempah & SetCRT2ToRAMDAC))  temp |= 0x20;
-                 }
-                 SiS_SetReg1(SiS_Pr->SiS_P3c4,0x32,temp);
-
-	         SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x1E,0x20);                   /* enable CRT2 */
-
-	         temp = SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x2E);
-                 if(!(temp & 0x80))
-                    SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2E,0x80);
-              }
-
-	      SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x00,0x1f,0x20);
-
-	      if(SiS_Is301B(SiS_Pr,BaseAddr)) { 
-	         SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x1F,0xC0);
-	         SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x00,0x7f);
-	      } else {
-	         SiS_VBLongWait(SiS_Pr);
-                 SiS_DisplayOn(SiS_Pr);
-	         SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x00,0x7F);
-                 SiS_VBLongWait(SiS_Pr);
-	      }
-	      
-	   }
-#endif
-	  
 	 } else {			/* 315, 330 */
 
 	   if(!(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr))) {
@@ -5801,11 +5842,11 @@
 
       }
 
-    } else {	/* ============  TW: For 301 ================ */
+    } else {	/* ============  For 301 ================ */
 
        if(HwDeviceExtension->jChipType < SIS_315H) {
             if(SiS_CRT2IsLCD(SiS_Pr,BaseAddr,HwDeviceExtension)) {
-                SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x11,0xF0,0x0B);
+                SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x11,0xFB);
 	        SiS_SetPanelDelay(SiS_Pr, ROMAddr, HwDeviceExtension, 0);
 	    }
        }
@@ -5837,13 +5878,13 @@
        if(HwDeviceExtension->jChipType < SIS_315H) {
             if(SiS_CRT2IsLCD(SiS_Pr,BaseAddr,HwDeviceExtension)) {
 	        SiS_SetPanelDelay(SiS_Pr, ROMAddr, HwDeviceExtension, 1);
-                SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x11,0xF0,0x03);
+                SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x11,0xF7);
 	    }
        }
 
     }
 
-  } else {   /* =================== TW: For LVDS ================== */
+  } else {   /* =================== For LVDS ================== */
 
     if(HwDeviceExtension->jChipType < SIS_315H) {
 
@@ -5894,7 +5935,7 @@
 
     } else {
 
-#ifdef SIS315H    /* 310/325 series */
+#ifdef SIS315H    /* 315 series */
 
 #if 0  /* BIOS code makes no sense */
        if(SiS_IsVAMode()) {
@@ -5921,36 +5962,41 @@
 	  SiS_Chrontel701xBLOff(SiS_Pr);
        }
 
-       SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2e,0x7f);
-       
-#ifdef NEWCH701x
-       if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
-           if(SiS_IsLCDOrLCDA(SiS_Pr,HwDeviceExtension,BaseAddr)) {
+       if(HwDeviceExtension->jChipType != SIS_550) {
+          SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2e,0x7f);
+       }
+
+       if(HwDeviceExtension->jChipType == SIS_740) {
+          if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
+             if(SiS_IsLCDOrLCDA(SiS_Pr,HwDeviceExtension,BaseAddr)) {
 	   	SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x1E,0x20);
-	   }
+	     }
+	  }
        }
-#endif       
 
        temp1 = SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x2E);
-       if (!(temp1 & 0x80))
+       if(!(temp1 & 0x80))
            SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2E,0x80);
 
        if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
            if(temp) {
-	       SiS_Chrontel701xBLOn(SiS_Pr);
+	       SiS_Chrontel701xBLOn(SiS_Pr, HwDeviceExtension);
 	   }
        }
 
        if(SiS_Pr->SiS_IF_DEF_CH70xx == 0) {
            if(SiS_CRT2IsLCD(SiS_Pr,BaseAddr,HwDeviceExtension)) {
 	   	SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x1E,0x20);
+		if(HwDeviceExtension->jChipType == SIS_550) {
+		   SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x1E,0x40);
+		   SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x1E,0x10);
+		}
+	   }
+       } else if(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) {
+           if(HwDeviceExtension->jChipType != SIS_740) {
+              SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x1E,0x20);
 	   }
-       } 
-#ifndef NEWCH701x       
-         else if(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-           SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x1E,0x20);
        }
-#endif       
 
        if(!(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr))) {
            SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x00,0x7f);
@@ -5973,10 +6019,10 @@
        if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
        	 	if(!(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr))) {
  	   		if(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-	            		SiS_Chrontel701xBLOn(SiS_Pr);
+	            		SiS_Chrontel701xBLOn(SiS_Pr, HwDeviceExtension);
 	            		SiS_ChrontelDoSomething4(SiS_Pr,HwDeviceExtension, BaseAddr);
            		} else if(SiS_IsLCDOrLCDA(SiS_Pr,HwDeviceExtension, BaseAddr))  {
-       				SiS_Chrontel701xBLOn(SiS_Pr);
+       				SiS_Chrontel701xBLOn(SiS_Pr, HwDeviceExtension);
        				SiS_ChrontelDoSomething4(SiS_Pr,HwDeviceExtension, BaseAddr);
 	   		}
        		}
@@ -6002,7 +6048,7 @@
 {
   USHORT  BaseAddr = (USHORT)HwDeviceExtension->ulIOAddress;
 
-  /* TW: Switch on LCD backlight on SiS30xLV */
+  /* Switch on LCD backlight on SiS30xLV */
   if( (SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) ||
       (SiS_CRT2IsLCD(SiS_Pr,BaseAddr,HwDeviceExtension)) ) {
     if(!(SiS_GetReg1(SiS_Pr->SiS_Part4Port,0x26) & 0x02)) {
@@ -6020,7 +6066,7 @@
 {
   USHORT  BaseAddr = (USHORT)HwDeviceExtension->ulIOAddress;
 
-  /* TW: Switch off LCD backlight on SiS30xLV */
+  /* Switch off LCD backlight on SiS30xLV */
   if( (!(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr))) ||
       (SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) ) {
 	 SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x26,0xFE,0x00);
@@ -6042,12 +6088,14 @@
   UCHAR *ROMAddr;
 
   if((ROMAddr = (UCHAR *)HwDeviceExtension->pjVirtualRomBase) && SiS_Pr->SiS_UseROM) {
-     temp = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x36) & 0xff;
-     temp >>= 4;
-     temp = 1 << temp;
-     temp1 = (ROMAddr[0x23c] << 8) | ROMAddr[0x23b];
-     if(temp1 & temp) return(1);
-     else return(0);
+     if((ROMAddr[0x233] == 0x12) && (ROMAddr[0x234] == 0x34)) {
+        temp = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x36) & 0xff;
+        temp >>= 4;
+        temp = 1 << temp;
+        temp1 = (ROMAddr[0x23c] << 8) | ROMAddr[0x23b];
+        if(temp1 & temp) return(1);
+        else return(0);
+     } else return(0);
   } else {
      return(0);
   }
@@ -6060,12 +6108,14 @@
   UCHAR *ROMAddr;
 
   if((ROMAddr = (UCHAR *)HwDeviceExtension->pjVirtualRomBase) && SiS_Pr->SiS_UseROM) {
-     temp = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x36) & 0xff;
-     temp >>= 4;
-     temp = 1 << temp;
-     temp1 = (ROMAddr[0x23e] << 8) | ROMAddr[0x23d];
-     if(temp1 & temp) return(1);
-     else return(0);
+     if((ROMAddr[0x233] == 0x12) && (ROMAddr[0x234] == 0x34)) {
+        temp = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x36) & 0xff;
+        temp >>= 4;
+        temp = 1 << temp;
+        temp1 = (ROMAddr[0x23e] << 8) | ROMAddr[0x23d];
+        if(temp1 & temp) return(1);
+        else return(0);
+     } else return(0);
   } else {
      return(0);
   }
@@ -6162,7 +6212,7 @@
 
 #ifdef SIS315H
 
-      if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {			/* 310/325 series, LVDS */
+      if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {			/* 315 series, LVDS */
 
           if(SiS_Pr->SiS_IF_DEF_CH70xx == 0) {
               PanelID = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x36);
@@ -6190,7 +6240,7 @@
 	      SiS_ShortDelay(SiS_Pr,Delay);
 	  }
 
-      } else {							/* 310/325 series, 301(B) */
+      } else {							/* 315 series, 301(B) */
 
           PanelID = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x36);
 	  DelayIndex = PanelID >> 4;
@@ -6290,14 +6340,7 @@
   if(HwDeviceExtension->jChipType >= SIS_315H) {
      flag = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x38);
      if((flag & EnableDualEdge) && (flag & SetToLCDA))   return(1);
-#if 0 /* Not done in 650/30xLV 1.10.6s, but in 650/301LV */
-     else if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-       if(flag) return(1);
-       else     return(0);   			         
-     }
-#endif
-     else
-       return(0);
+     else return(0);
   } else
 #endif
      return(0);
@@ -6344,7 +6387,9 @@
   if(HwDeviceExtension->jChipType == SIS_650) {
      flag = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x5f);
      flag &= 0xF0;
-     if((flag == 0xb0) || (flag == 0x90)) return 0;
+     /* Check for revision != A0 only */
+     if((flag == 0xe0) || (flag == 0xc0) ||
+        (flag == 0xb0) || (flag == 0x90)) return 0;
      else return 1;
   } else
 #endif
@@ -6462,7 +6507,7 @@
       if((flag == 0x80) || (flag == 0x20)) return 0;
       else	                           return 1;
     } else {
-      /* 310/325 series (650/30xLV 1.10.6s) */
+      /* 315 series (650/30xLV 1.10.6s) */
       flag &= 0x50;
       if((flag == 0x40) || (flag == 0x10)) return 0;
       else                                 return 1;
@@ -6506,14 +6551,14 @@
   }
 }
 
-BOOLEAN
+void
 SiS_GetLCDResInfo(SiS_Private *SiS_Pr, UCHAR *ROMAddr,USHORT ModeNo,
                   USHORT ModeIdIndex, PSIS_HW_DEVICE_INFO HwDeviceExtension)
 {
   USHORT temp,modeflag,resinfo=0;
   const unsigned char SiS300SeriesLCDRes[] =
-         { 0, 1, 2, 3, 7, 4, 5, 8,
-	   0, 0, 0, 0, 0, 0, 0, 0 };
+         { 0,  1,  2,  3,  7,  4,  5,  8,
+	   0,  0, 10,  0,  0,  0,  0, 15 };
 
   SiS_Pr->SiS_LCDResInfo = 0;
   SiS_Pr->SiS_LCDTypeInfo = 0;
@@ -6530,17 +6575,19 @@
      }
   }
 
-  if(!(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)))   return 0;
+  if(!(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)))   return;
 
-  if(!(SiS_Pr->SiS_VBInfo & (SetSimuScanMode | SwitchToCRT2))) return 0;
+  if(!(SiS_Pr->SiS_VBInfo & (SetSimuScanMode | SwitchToCRT2))) return;
 
   temp = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x36);
 
+#if 0
   /* FSTN: Fake CR36 (TypeInfo 2, ResInfo SiS_Panel320x480) */
   if(SiS_Pr->SiS_IF_DEF_FSTN) {
    	temp = 0x20 | SiS_Pr->SiS_Panel320x480;
    	SiS_SetReg1(SiS_Pr->SiS_P3d4,0x36,temp);
   }
+#endif
 
   if(HwDeviceExtension->jChipType < SIS_315H) {
   	SiS_Pr->SiS_LCDTypeInfo = temp >> 4;
@@ -6549,14 +6596,16 @@
   }
   temp &= 0x0f;
   if(HwDeviceExtension->jChipType < SIS_315H) {
-      /* TW: Translate 300 series LCDRes to 310/325 series for unified usage */
+      /* Translate 300 series LCDRes to 315 series for unified usage */
       temp = SiS300SeriesLCDRes[temp];
   }
   SiS_Pr->SiS_LCDResInfo = temp;
 
+#if 0
   if(SiS_Pr->SiS_IF_DEF_FSTN){
        	SiS_Pr->SiS_LCDResInfo = SiS_Pr->SiS_Panel320x480;
   }
+#endif
 
   if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
     	if(SiS_Pr->SiS_LCDResInfo < SiS_Pr->SiS_PanelMin301)
@@ -6566,83 +6615,109 @@
 		SiS_Pr->SiS_LCDResInfo = SiS_Pr->SiS_PanelMinLVDS;
   }
 
-  if(SiS_Pr->SiS_LCDResInfo > SiS_Pr->SiS_PanelMax)
+  if((!SiS_Pr->CP_HaveCustomData) || (SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_PanelCustom)) {
+     if(SiS_Pr->SiS_LCDResInfo > SiS_Pr->SiS_PanelMax)
   	SiS_Pr->SiS_LCDResInfo = SiS_Pr->SiS_Panel1024x768;
+  }
+
+  if(SiS_Pr->SiS_CustomT == CUT_BARCO1366) {
+     SiS_Pr->SiS_LCDResInfo = Panel_Barco1366;
+  }
+
+  switch(SiS_Pr->SiS_LCDResInfo) {
+     case Panel_800x600:   SiS_Pr->PanelXRes =  800; SiS_Pr->PanelYRes =  600; break;
+     case Panel_1024x768:  SiS_Pr->PanelXRes = 1024; SiS_Pr->PanelYRes =  768; break;
+     case Panel_1280x1024: SiS_Pr->PanelXRes = 1280; SiS_Pr->PanelYRes = 1024; break;
+     case Panel_640x480_3:
+     case Panel_640x480_2:
+     case Panel_640x480:   SiS_Pr->PanelXRes =  640; SiS_Pr->PanelYRes =  480; break;
+     case Panel_1024x600:  SiS_Pr->PanelXRes = 1024; SiS_Pr->PanelYRes =  600; break;
+     case Panel_1152x864:  SiS_Pr->PanelXRes = 1152; SiS_Pr->PanelYRes =  864; break;
+     case Panel_1280x960:  SiS_Pr->PanelXRes = 1280; SiS_Pr->PanelYRes =  960; break;
+     case Panel_1152x768:  SiS_Pr->PanelXRes = 1152; SiS_Pr->PanelYRes =  768; break;
+     case Panel_1400x1050: SiS_Pr->PanelXRes = 1400; SiS_Pr->PanelYRes = 1050; break;
+     case Panel_1280x768:  SiS_Pr->PanelXRes = 1280; SiS_Pr->PanelYRes =  768; break;
+     case Panel_1600x1200: SiS_Pr->PanelXRes = 1600; SiS_Pr->PanelYRes = 1200; break;
+     case Panel_320x480:   SiS_Pr->PanelXRes =  320; SiS_Pr->PanelYRes =  480; break;
+     case Panel_Custom:    SiS_Pr->PanelXRes = SiS_Pr->CP_MaxX;
+    			   SiS_Pr->PanelYRes = SiS_Pr->CP_MaxY;
+			   break;
+     case Panel_Barco1366: SiS_Pr->PanelXRes = 1360; SiS_Pr->PanelYRes = 1024; break;
+     default:		   SiS_Pr->PanelXRes = 1024; SiS_Pr->PanelYRes =  768; break;
+  }
 
   temp = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x37);
+#if 0
   if(SiS_Pr->SiS_IF_DEF_FSTN){
-        /* TW: Fake LVDS bridge for FSTN */
+        /* Fake LVDS bridge for FSTN */
       	temp = 0x04;
       	SiS_SetReg1(SiS_Pr->SiS_P3d4,0x37,temp);
   }
+#endif
   SiS_Pr->SiS_LCDInfo = temp;
   
   if(!(SiS_Pr->UsePanelScaler))        SiS_Pr->SiS_LCDInfo &= ~DontExpandLCD;
   else if(SiS_Pr->UsePanelScaler == 1) SiS_Pr->SiS_LCDInfo |= DontExpandLCD;
 
-  /* TW: Inserted entire 315-block from 650/LVDS/30xLV BIOSes */
-  if(HwDeviceExtension->jChipType >= SIS_315H) {
-#ifdef SIS315H
-     if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
-         if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
-	     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) {
-		 if(ModeNo == 0x3a || ModeNo == 0x4d || ModeNo == 0x65) {
-		     /* Bridge does not scale to 1280x1024 */
-		     SiS_Pr->SiS_LCDInfo |= DontExpandLCD;
-		 }
-	     }
-	     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
-	         if(ModeNo == 0x7c || ModeNo == 0x7d || ModeNo == 0x7e) {
-		     /* TW: Bridge does not scale to 1280x960 */
-		     SiS_Pr->SiS_LCDInfo |= DontExpandLCD;
-		 }
-		 if(ModeNo == 0x2f || ModeNo == 0x5d || ModeNo == 0x5e) {
-		     /* TW: Bridge does not scale to 640x400 */
-		     SiS_Pr->SiS_LCDInfo |= DontExpandLCD;
-		 }
-	     }
-	     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
-	         if(ModeNo == 0x2f || ModeNo == 0x5d || ModeNo == 0x5e) {
-		     /* TW: Most panels can't scale to 640x400 */
-		     SiS_Pr->SiS_LCDInfo &= ~DontExpandLCD;
-		 }
-	     }
-	     /* TEMP - no idea about the timing and zoom factors */
-	     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768) {
-	         SiS_Pr->SiS_LCDInfo |= DontExpandLCD;
-	     }
-	     /* TEMP - no idea about the timing and zoom factors */
-	     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) {
+  if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
+        if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_PanelCustom) {
+	   /* For non-standard LCD resolution, we let the panel scale */
+           SiS_Pr->SiS_LCDInfo |= DontExpandLCD;
+        } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
+	   if(ModeNo == 0x7c || ModeNo == 0x7d || ModeNo == 0x7e) {
+	      /* Bridge does not scale to 1280x960 */
+              SiS_Pr->SiS_LCDInfo |= DontExpandLCD;
+	   } else if(ModeNo == 0x2f || ModeNo == 0x5d || ModeNo == 0x5e) {
+	      if(!(SiS_Pr->SiS_VBType & VB_SIS301LV302LV)) {
+	         /* Bridge does not scale to 640x400 */
 	         SiS_Pr->SiS_LCDInfo |= DontExpandLCD;
-	     }
-	 }
+	      }
+	   }
+        } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768) {
+           /* TEMP - no idea about the timing and zoom factors */
+           SiS_Pr->SiS_LCDInfo |= DontExpandLCD;
+        } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) {
+	   if(ModeNo == 0x3a || ModeNo == 0x4d || ModeNo == 0x65) {
+	      /* Bridge does not scale to 1280x1024 */
+	      SiS_Pr->SiS_LCDInfo |= DontExpandLCD;
+	   }
+	} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) {
+	   /* TEMP - no idea about the timing and zoom factors */
+	   SiS_Pr->SiS_LCDInfo |= DontExpandLCD;
+	}
      }
+  }
+
+
+  if(HwDeviceExtension->jChipType >= SIS_315H) {
+#ifdef SIS315H
      if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x39) & 0x01) {
-         SiS_Pr->SiS_LCDInfo &= 0xFFEF;    
-	 SiS_Pr->SiS_LCDInfo |= LCDPass11;
+        SiS_Pr->SiS_LCDInfo &= 0xFFEF;
+	SiS_Pr->SiS_LCDInfo |= LCDPass11;
      }
 #endif
   } else {
 #ifdef SIS300
      if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
         if((ROMAddr) && SiS_Pr->SiS_UseROM) {
-           if(!(ROMAddr[0x235] & 0x02)) {
-	      SiS_Pr->SiS_LCDInfo &= 0xEF;
+	   if((ROMAddr[0x233] == 0x12) && (ROMAddr[0x234] == 0x34)) {
+              if(!(ROMAddr[0x235] & 0x02)) {
+	         SiS_Pr->SiS_LCDInfo &= 0xEF;
+ 	      }
 	   }
         }
-     } else {
-        if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-	   if((SiS_Pr->SiS_SetFlag & SetDOSMode) && ((ModeNo == 0x03) || (ModeNo == 0x10))) {
-               SiS_Pr->SiS_LCDInfo &= 0xEF;
-	   }
+     } else if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
+	if((SiS_Pr->SiS_SetFlag & SetDOSMode) && ((ModeNo == 0x03) || (ModeNo == 0x10))) {
+           SiS_Pr->SiS_LCDInfo &= 0xEF;
 	}
      }
 #endif
   }
-  
-  /* TW: With Trumpion, always Expanding */
-  if(SiS_Pr->SiS_IF_DEF_TRUMPION != 0){
-       SiS_Pr->SiS_LCDInfo &= (~DontExpandLCD);
+
+  /* Trumpion: Assume non-expanding */
+  if(SiS_Pr->SiS_IF_DEF_TRUMPION != 0) {
+     SiS_Pr->SiS_LCDInfo &= (~DontExpandLCD);
   }
 
   if(!((HwDeviceExtension->jChipType < SIS_315H) && (SiS_Pr->SiS_SetFlag & SetDOSMode))) {
@@ -6651,7 +6726,7 @@
         if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x600) {
 	   if(ModeNo > 0x13) {
 	      if(!(SiS_Pr->SiS_LCDInfo & DontExpandLCD)) {
-                 if((resinfo == 7) || (resinfo == 3)) {
+                 if((resinfo == SIS_RI_800x600) || (resinfo == SIS_RI_400x300)) {
                     SiS_Pr->SiS_SetFlag |= EnableLVDSDDA;
 		 }
               }
@@ -6671,9 +6746,9 @@
 	                                      (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480))) {
                  if(ModeNo > 0x13) {
                     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
-                       if(resinfo == 4) SiS_Pr->SiS_SetFlag |= EnableLVDSDDA;     /* 512x384  */
+                       if(resinfo == SIS_RI_512x384) SiS_Pr->SiS_SetFlag |= EnableLVDSDDA;
                     } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600) {
-                       if(resinfo == 3) SiS_Pr->SiS_SetFlag |= EnableLVDSDDA;     /* 400x300  */
+                       if(resinfo == SIS_RI_400x300) SiS_Pr->SiS_SetFlag |= EnableLVDSDDA;
                     }
                  }
 	      } else SiS_Pr->SiS_SetFlag |= EnableLVDSDDA;
@@ -6692,19 +6767,21 @@
   }
 
 #ifdef SIS315H
-  /* TW: 650/30xLV 1.10.6s */
+  /* 650/30xLV 1.10.6s */
   if(HwDeviceExtension->jChipType >= SIS_315H) {
-    if(SiS_Pr->SiS_VBType & (VB_SIS302B | VB_SIS302LV)) {
-      /* Enable 302B/302LV dual link mode */
-      /* (302B is a theory - not in any BIOS */
-      temp = 0x00;
-      if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) temp = 0x04;
-      if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) temp = 0x04;
-      if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) temp = 0x04;
-      SiS_SetReg1(SiS_Pr->SiS_P3d4,0x39,temp);
-    } else if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
-      SiS_SetReg1(SiS_Pr->SiS_P3d4,0x39,0x00);
-    }
+     if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
+        SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x39,~0x04);
+        if(SiS_Pr->SiS_VBType & (VB_SIS302B | VB_SIS302LV)) {
+           /* Enable 302B/302LV dual link mode.
+            * (302B is a theory - not in any BIOS)
+	    */
+           if((SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) ||
+              (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) ||
+              (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200)) {
+	      SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x39,0x04);
+	   }
+  	}
+     }
   }
 #endif
 
@@ -6720,20 +6797,6 @@
 	SiS_Pr->SiS_LCDInfo, SiS_Pr->SiS_LCDResInfo, SiS_Pr->SiS_LCDTypeInfo, SiS_Pr->SiS_SetFlag);
 #endif
 
-  return 1;
-}
-
-void
-SiS_PresetScratchregister(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension)
-{
-  return;
-  /*SiS_SetReg1(SiS_Pr->SiS_P3d4,0x30,0x21);  */
-  /*SiS_SetReg1(SiS_Pr->SiS_P3d4,0x31,0x41);  */
-  /*SiS_SetReg1(SiS_Pr->SiS_P3d4,0x32,0x28);  */
-  /*SiS_SetReg1(SiS_Pr->SiS_P3d4,0x33,0x22);  */
-  /*SiS_SetReg1(SiS_Pr->SiS_P3d4,0x35,0x43);  */
-  /*SiS_SetReg1(SiS_Pr->SiS_P3d4,0x36,0x01);  */
-  /*SiS_SetReg1(SiS_Pr->SiS_P3d4,0x37,0x00);  */
 }
 
 void
@@ -6763,7 +6826,6 @@
   } else {
     SiS_LongWait(SiS_Pr);
   }
-  return;
 }
 
 void
@@ -6830,7 +6892,7 @@
 #endif
   } else {
 #ifdef SIS300
-#if 0  /* TW: Not done in A901 BIOS */
+#if 0  /* Not done in A901 BIOS */
      if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
         if(SiS_GetReg1(SiS_Pr->SiS_P3c4,0x1f) & 0xc0) return;
      }
@@ -6925,7 +6987,51 @@
 
 /* ========================================================= */
 
-/* TW: Set 301 TV Encoder (and some LCD relevant) registers */
+static void
+SiS_SetTVSpecial(SiS_Private *SiS_Pr, USHORT ModeNo)
+{
+  if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+        if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) {
+           if((ModeNo == 0x64) || (ModeNo == 0x4a) || (ModeNo == 0x38)) {
+              SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x1c,0xa7);
+              SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x1d,0x07);
+	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x1e,0xf2);
+	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x1f,0x6e);
+	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x20,0x17);
+	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x21,0x8b);
+	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x22,0x73);
+	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x23,0x53);
+	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x24,0x13);
+	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x25,0x40);
+	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x26,0x34);
+	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x27,0xf4);
+	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x28,0x63);
+	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x29,0xbb);
+	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2a,0xcc);
+	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2b,0x7a);
+	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2c,0x58);   /* 48 */
+	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2d,0xe4);
+	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2e,0x73);
+	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2f,0xda);   /* de */
+	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x30,0x13);
+	      if((SiS_GetReg1(SiS_Pr->SiS_P3d4,0x38)) & 0x40) {
+	         SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x01,0x14);
+	      } else {
+	         SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x01,0x15);
+	      }
+	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x02,0x1b);
+	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x43,0x72);
+           }
+        } else {
+	   SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x01,0x21);
+	   SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x02,0x5a);
+	}
+     }
+  }
+}
+
+/* Set 301 TV Encoder (and some LCD relevant) registers */
 void
 SiS_SetGroup2(SiS_Private *SiS_Pr, USHORT BaseAddr,UCHAR *ROMAddr, USHORT ModeNo,
               USHORT ModeIdIndex,USHORT RefreshRateTableIndex,
@@ -6938,67 +7044,46 @@
 #ifdef SIS315H   
   const       SiS_Part2PortTblStruct *CRT2Part2Ptr = NULL;
   USHORT      resindex, CRT2Index;
-#endif  
+#endif
   USHORT      modeflag, resinfo, crt2crtc;
-  ULONG       longtemp, tempeax, tempebx, temp2, tempecx;
+  ULONG       longtemp, tempeax;
+#ifdef SIS300
   const UCHAR atable[] = {
                  0xc3,0x9e,0xc3,0x9e,0x02,0x02,0x02,
 	         0xab,0x87,0xab,0x9e,0xe7,0x02,0x02
   };
+#endif  
 
 #ifdef SIS315H   
   if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
-     /* TW: 650/30xLV 1.10.6s: (Is at end of SetGroup2!) */
+     /* 650/30xLV 1.10.6s: (Is at end of SetGroup2!) */
      if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
         if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) {
 	   SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x1a,0xfc,0x03);
-	   temp = 1;
-	   if(ModeNo <= 0x13) temp = 3;
+	   temp = 0x01;
+	   if(ModeNo <= 0x13) temp = 0x03;
 	   SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x0b,temp);
 	}
      }
-     if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-       if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
-         if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) {
-           if((ModeNo == 0x4a) || (ModeNo == 0x38)) {
-               SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x1c,0xa7);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x1d,0x07);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x1e,0xf2);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x1f,0x6e);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x20,0x17);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x21,0x8b);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x22,0x73);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x23,0x53);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x24,0x13);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x25,0x40);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x26,0x34);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x27,0xf4);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x28,0x63);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x29,0xbb);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2a,0xcc);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2b,0x7a);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2c,0x58);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2d,0xe4);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2e,0x73);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2f,0xda);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x30,0x13);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x43,0x72);
-           }
-         }
-       }
-     }
+     SiS_SetTVSpecial(SiS_Pr, ModeNo);
      return;
   }
-#endif  
+#endif
 
-  if(ModeNo<=0x13) {
-    	modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;      /* si+St_ResInfo */
-    	resinfo = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ResInfo;
-    	crt2crtc = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_CRT2CRTC;
+  if(ModeNo <= 0x13) {
+     modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
+     resinfo = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ResInfo;
+     crt2crtc = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_CRT2CRTC;
   } else {
-    	modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;     /* si+Ext_ResInfo */
+     if(SiS_Pr->UseCustomMode) {
+        modeflag = SiS_Pr->CModeFlag;
+	resinfo = 0;
+	crt2crtc = 0;
+     } else {
+        modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
     	resinfo = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO;
     	crt2crtc = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
+     }
   }
 
   tempcx = SiS_Pr->SiS_VBInfo;
@@ -7011,9 +7096,9 @@
   temp |= ((tempbx & 0x00FF) >> 3);
   temp ^= 0x0C;
 
-  /* TW: From 1.10.7w (no vb check there; don't care - this only disables SVIDEO and CVBS signal) */
+  /* From 1.10.7w (no vb check there; don't care - this only disables SVIDEO and CVBS signal) */
   if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
-  	temp |= 0x0c;
+     temp |= 0x0c;
   }
 
   PhasePoint  = SiS_Pr->SiS_PALPhase;
@@ -7021,44 +7106,44 @@
   
   if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) {          
   
-    temp ^= 0x01;
-    if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
-      TimingPoint = SiS_Pr->SiS_HiTVSt2Timing;
-      if(SiS_Pr->SiS_SetFlag & TVSimuMode) {
-        if(modeflag & Charx8Dot) TimingPoint = SiS_Pr->SiS_HiTVSt1Timing;
-        else TimingPoint = SiS_Pr->SiS_HiTVTextTiming;
-      }
-    } else TimingPoint = SiS_Pr->SiS_HiTVExtTiming;
-    
-    if(SiS_Pr->SiS_HiVision & 0x03) temp &= 0xfe;
-    
+     temp ^= 0x01;
+     if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
+        TimingPoint = SiS_Pr->SiS_HiTVSt2Timing;
+        if(SiS_Pr->SiS_SetFlag & TVSimuMode) {
+           if(modeflag & Charx8Dot) TimingPoint = SiS_Pr->SiS_HiTVSt1Timing;
+           else TimingPoint = SiS_Pr->SiS_HiTVTextTiming;
+        }
+     } else TimingPoint = SiS_Pr->SiS_HiTVExtTiming;
+
+     if(SiS_Pr->SiS_HiVision & 0x03) temp &= 0xfe;
+
   } else {
-  
-    if(SiS_Pr->SiS_VBInfo & SetPALTV){
 
-      TimingPoint = SiS_Pr->SiS_PALTiming;
-      PhasePoint  = SiS_Pr->SiS_PALPhase;
+     if(SiS_Pr->SiS_VBInfo & SetPALTV){
 
-      if( (SiS_Pr->SiS_VBType & VB_SIS301B302B) &&
-          ( (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) ||
-	    (SiS_Pr->SiS_SetFlag & TVSimuMode) ) ) {
-         PhasePoint = SiS_Pr->SiS_PALPhase2;
-      }
+        TimingPoint = SiS_Pr->SiS_PALTiming;
+        PhasePoint  = SiS_Pr->SiS_PALPhase;
 
-    } else {
+        if( (SiS_Pr->SiS_VBType & VB_SIS301B302B) &&
+            ( (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) ||
+	      (SiS_Pr->SiS_SetFlag & TVSimuMode) ) ) {
+           PhasePoint = SiS_Pr->SiS_PALPhase2;
+        }
+
+     } else {
 
         temp |= 0x10;
-	TimingPoint = SiS_Pr->SiS_NTSCTiming;
-	PhasePoint  = SiS_Pr->SiS_NTSCPhase;
+        TimingPoint = SiS_Pr->SiS_NTSCTiming;
+        PhasePoint  = SiS_Pr->SiS_NTSCPhase;
 
         if( (SiS_Pr->SiS_VBType & VB_SIS301B302B) &&
 	    ( (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) ||
 	      (SiS_Pr->SiS_SetFlag & TVSimuMode) ) ) {
-        	PhasePoint = SiS_Pr->SiS_NTSCPhase2;
+           PhasePoint = SiS_Pr->SiS_NTSCPhase2;
         }
 
-    }
-    
+     }
+
   }
   SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x00,temp);
 
@@ -7071,36 +7156,35 @@
      temp = 0x38;
   }
   if(temp) {
-    if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
-      if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x31) & 0x01) {
-          temp1 = SiS_GetReg1(SiS_Pr->SiS_P3d4,temp);
-          if(temp1 & EnablePALM) {	/* 0x40 */
-              	PhasePoint = SiS_Pr->SiS_PALMPhase;
-		if( (SiS_Pr->SiS_VBType & VB_SIS301B302B) &&
-		    ( (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) ||
-		      (SiS_Pr->SiS_SetFlag & TVSimuMode) ) ) {
-	           PhasePoint = SiS_Pr->SiS_PALMPhase2;
-		}
-	  }
-          if(temp1 & EnablePALN) {	/* 0x80 */
-               	PhasePoint = SiS_Pr->SiS_PALNPhase;
-		if( (SiS_Pr->SiS_VBType & VB_SIS301B302B) &&
-		    ( (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) ||
-		      (SiS_Pr->SiS_SetFlag & TVSimuMode) ) ) {
-	           PhasePoint = SiS_Pr->SiS_PALNPhase2;
-		}
-	  }
-      }
-    }
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+        if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x31) & 0x01) {
+           temp1 = SiS_GetReg1(SiS_Pr->SiS_P3d4,temp);
+           if(temp1 & EnablePALM) {	/* 0x40 */
+              PhasePoint = SiS_Pr->SiS_PALMPhase;
+	      if( (SiS_Pr->SiS_VBType & VB_SIS301B302B) &&
+		  ( (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) ||
+		    (SiS_Pr->SiS_SetFlag & TVSimuMode) ) ) {
+	         PhasePoint = SiS_Pr->SiS_PALMPhase2;
+	      }
+	   }
+           if(temp1 & EnablePALN) {	/* 0x80 */
+              PhasePoint = SiS_Pr->SiS_PALNPhase;
+	      if( (SiS_Pr->SiS_VBType & VB_SIS301B302B) &&
+		  ( (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) ||
+		    (SiS_Pr->SiS_SetFlag & TVSimuMode) ) ) {
+	         PhasePoint = SiS_Pr->SiS_PALNPhase2;
+	      }
+	   }
+        }
+     }
   }
 
 #ifdef SIS315H
-  /* TW: 650/301LV BIOS */
   if(HwDeviceExtension->jChipType >= SIS_315H) {
      if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {  
         if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
            if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) {
-              if((ModeNo == 0x4a) || (ModeNo == 0x38)) {
+              if((ModeNo == 0x64) || (ModeNo == 0x4a) || (ModeNo == 0x38)) {
 	         PhasePoint = SiS_Pr->SiS_SpecialPhase;
 	      }
            }
@@ -7121,12 +7205,12 @@
   }
 
   if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
-    if(HwDeviceExtension->jChipType >= SIS_315H) {
-      if(!(SiS_Pr->SiS_ModeType & 0x07))
+     if(HwDeviceExtension->jChipType >= SIS_315H) {
+        if(!(SiS_Pr->SiS_ModeType & 0x07))
+           SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x3A,0x1F);
+     } else {
         SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x3A,0x1F);
-    } else {
-      SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x3A,0x1F);
-    }
+     }
   }
 
   SiS_SetRegOR(SiS_Pr->SiS_Part2Port,0x0A,SiS_Pr->SiS_NewFlickerMode);
@@ -7137,11 +7221,11 @@
   SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x38,SiS_Pr->SiS_RY4COE);
 
   if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) {
-      if(SiS_Pr->SiS_HiVision == 3) tempax = 950;
-      else tempax = 440;
+     if(SiS_Pr->SiS_HiVision == 3) tempax = 950;
+     else tempax = 440;
   } else {
-    if(SiS_Pr->SiS_VBInfo & SetPALTV) tempax = 520;
-    else tempax = 440;
+     if(SiS_Pr->SiS_VBInfo & SetPALTV) tempax = 520;
+     else tempax = 440;
   }
 
   if( ( ( (!(SiS_Pr->SiS_VBInfo & SetCRT2ToTV)) || (SiS_Pr->SiS_HiVision == 3) ) && (SiS_Pr->SiS_VDE <= tempax) ) ||
@@ -7161,18 +7245,18 @@
      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x02,temp);
 
      if( (SiS_Pr->SiS_VBInfo & (SetCRT2ToTV - SetCRT2ToHiVisionTV)) &&
-        (SiS_Pr->SiS_HiVision != 3) &&
-        (SiS_Pr->SiS_VGAHDE >= 1024) ) {
+         (SiS_Pr->SiS_HiVision != 3) &&
+         (SiS_Pr->SiS_VGAHDE >= 1024) ) {
         if(SiS_Pr->SiS_VBInfo & SetPALTV) {
            SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x01,0x19);
            SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x02,0x52);
         } else {
            if(HwDeviceExtension->jChipType >= SIS_315H) {
-             SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x01,0x17);
-             SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x02,0x1d);
+              SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x01,0x17);
+              SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x02,0x1d);
 	   } else {
-             SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x01,0x0b);
-             SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x02,0x11);
+              SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x01,0x0b);
+              SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x02,0x11);
 	   }
         }
      }
@@ -7181,7 +7265,7 @@
 
   tempcx = SiS_Pr->SiS_HT;
 
-  /* TW: 650/30xLV 1.10.6s */
+  /* 650/30xLV 1.10.6s */
   if(HwDeviceExtension->jChipType >= SIS_315H) {
       if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x39) & 0x04) {
       	   tempcx >>= 1;
@@ -7208,7 +7292,7 @@
   tempcx += 7;
   if((SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) &&
      (SiS_Pr->SiS_HiVision == 3)) {
-       tempcx -= 4;
+     tempcx -= 4;
   }
   temp = (tempcx & 0x00FF) << 4;
   SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x22,0x0F,temp);
@@ -7228,8 +7312,8 @@
   tempbx += 8;
   if((SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) &&
      (SiS_Pr->SiS_HiVision == 3)) {
-    tempbx -= 4;
-    tempcx = tempbx;
+     tempbx -= 4;
+     tempcx = tempbx;
   }
   temp = (tempbx & 0x00FF) << 4;
   SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x29,0x0F,temp);
@@ -7258,8 +7342,8 @@
 
   tempcx -= 11;
   if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToTV)) {
-    tempax = SiS_GetVGAHT2(SiS_Pr) - 1;
-    tempcx = tempax;
+     tempax = SiS_GetVGAHT2(SiS_Pr) - 1;
+     tempcx = tempax;
   }
   temp = tempcx & 0x00FF;
   SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2E,temp);
@@ -7269,38 +7353,38 @@
   if(SiS_Pr->SiS_VGAVDE == 375) tempbx = 746;
   if(SiS_Pr->SiS_VGAVDE == 405) tempbx = 853;
   if(HwDeviceExtension->jChipType < SIS_315H) {
-  	if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) tempbx >>= 1;
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) tempbx >>= 1;
   } else {
-	if((SiS_Pr->SiS_VBInfo & SetCRT2ToTV) && (!(SiS_Pr->SiS_HiVision & 0x03))) {
-	   tempbx >>= 1;
-	   if(SiS_Pr->SiS_SetFlag & TVSimuMode) {
-	      if(ModeNo <= 0x13) {
-	         if(crt2crtc == 1) {
-	            tempbx++;
-                 }
-	      }
-	   } else {
-              if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
-	         if(crt2crtc == 4)   /* TW: BIOS calls GetRatePtrCRT2 here - does not make sense */
-                    if(SiS_Pr->SiS_ModeType <= 3) tempbx++;
-	      }
+     if((SiS_Pr->SiS_VBInfo & SetCRT2ToTV) && (!(SiS_Pr->SiS_HiVision & 0x03))) {
+	tempbx >>= 1;
+	if(SiS_Pr->SiS_SetFlag & TVSimuMode) {
+	   if(ModeNo <= 0x13) {
+	      if(crt2crtc == 1) {
+	         tempbx++;
+              }
 	   }
-        }
+	} else {
+           if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
+	      if(crt2crtc == 4)   /* BIOS calls GetRatePtrCRT2 here - does not make sense */
+                 if(SiS_Pr->SiS_ModeType <= 3) tempbx++;
+	   }
+	}
+     }
   }
   tempbx -= 2;
   temp = tempbx & 0x00FF;
   if((SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) &&
      (SiS_Pr->SiS_HiVision == 3)) {
-    if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
-      if(ModeNo == 0x2f) temp++;
-    }
+     if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
+        if((ModeNo == 0x2f) || (ModeNo == 0x5d) || (ModeNo == 0x5e)) temp++;
+     }
   }
-  /* TW: From 1.10.7w - doesn't make sense */
+  /* From 1.10.7w - doesn't make sense */
   if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
      if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
         if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) {
 	   if(!(SiS_Pr->SiS_SetFlag & LCDVESATiming)) {   /* SetFlag?? */
-	       if(ModeNo == 0x03) temp++;
+	      if(ModeNo == 0x03) temp++;
 	   }
 	}
      }
@@ -7312,15 +7396,15 @@
   tempax |= (tempbx & 0xFF00);
   if(HwDeviceExtension->jChipType < SIS_315H) {
      if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV)) {
-        if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToSCART)) {		/* TW: New from 630/301B (II) BIOS */
+        if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToSCART)) {		/* New from 630/301B (II) BIOS */
            tempax |= 0x1000;
            if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToSVIDEO))  tempax |= 0x2000;
         }
      }
   } else {
      /* TODO Check this with other BIOSes */
-     if((!(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV)) && 
-        (SiS_Pr->SiS_HiVision == 3)) {
+     if((!(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV))
+         /* && (SiS_Pr->SiS_HiVision == 3) */ ) {
 	tempax |= 0x1000;
         if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToSVIDEO))  tempax |= 0x2000;
      }
@@ -7328,12 +7412,12 @@
   temp = (tempax & 0xFF00) >> 8;
   SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x30,temp);
 
-  /* TW: 650/30xLV 1.10.6s */
+  /* 650/30xLV 1.10.6s */
   if(HwDeviceExtension->jChipType > SIS_315H) {
      if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
         if( (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) ||
             (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) ) {
-            SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x10,0x60);
+           SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x10,0x60);
         }
      }
   }
@@ -7349,79 +7433,70 @@
      }
   }
 
-  if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {     
-    tempbx = SiS_Pr->SiS_VDE;
-    if((SiS_Pr->SiS_VBInfo & SetCRT2ToTV) && (!(SiS_Pr->SiS_HiVision & 0x03))) {
-         tempbx >>= 1;
-    }
-    tempbx -= 3;
-    tempbx &= 0x03ff;
-    temp = ((tempbx & 0xFF00) >> 8) << 5;
-    temp |= 0x18;
-    SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x46,temp);
-    temp = tempbx & 0x00FF;
-    SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x47,temp);	/* tv gatingno */
-    if(HwDeviceExtension->jChipType >= SIS_315H) {	/* TW: 650/30xLV 1.10.6s */
-       if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
-          tempax = 0;
-          if(SiS_Pr->SiS_HiVision & 0x03) {
-	     tempax = 0x3000;
-	     if(SiS_Pr->SiS_HiVision & 0x01) tempax = 0x5000;
-	  }
-	  temp = (tempax & 0xFF00) >> 8;
-          SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x4d,temp);
-       }
-    }
+  if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
+     tempbx = SiS_Pr->SiS_VDE;
+     if((SiS_Pr->SiS_VBInfo & SetCRT2ToTV) && (!(SiS_Pr->SiS_HiVision & 0x03))) {
+        tempbx >>= 1;
+     }
+     tempbx -= 3;
+     tempbx &= 0x03ff;
+     temp = ((tempbx & 0xFF00) >> 8) << 5;
+     temp |= 0x18;
+     SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x46,temp);
+     temp = tempbx & 0x00FF;
+     SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x47,temp);	/* tv gatingno */
+     if(HwDeviceExtension->jChipType >= SIS_315H) {
+        if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
+           tempax = 0;
+           if(SiS_Pr->SiS_HiVision & 0x03) {
+ 	      tempax = 0x3000;
+	      if(SiS_Pr->SiS_HiVision & 0x01) tempax = 0x5000;
+	   }
+	   temp = (tempax & 0xFF00) >> 8;
+           SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x4d,temp);
+        }
+     }
   }
 
   tempbx &= 0x00FF;
   if(!(modeflag & HalfDCLK)) {
-    tempcx = SiS_Pr->SiS_VGAHDE;
-    if(tempcx >= SiS_Pr->SiS_HDE) {
-      tempbx |= 0x2000;
-      tempax &= 0x00FF;
-    }
+     if(SiS_Pr->SiS_VGAHDE >= SiS_Pr->SiS_HDE) {
+        tempbx |= 0x2000;
+        tempax &= 0x00FF;
+     }
   }
 
   tempcx = 0x0101;
-/*if(SiS_Pr->SiS_VBInfo & (SetPALTV | SetCRT2ToTV)) {  */ /*301b- TW: BIOS BUG? */
+/*if(SiS_Pr->SiS_VBInfo & (SetPALTV | SetCRT2ToTV)) {  */ /* BIOS BUG? */
   if(SiS_Pr->SiS_VBInfo & (SetCRT2ToTV - SetCRT2ToHiVisionTV)) {
-    if(!(SiS_Pr->SiS_HiVision & 0x03)) {
-      if(SiS_Pr->SiS_VGAHDE >= 1024) {
-        if((!(modeflag & HalfDCLK)) || (HwDeviceExtension->jChipType < SIS_315H)) {   /* TW: This check not in 630/301B */
-          tempcx = 0x1920;
-          if(SiS_Pr->SiS_VGAHDE >= 1280) {
-            tempcx = 0x1420;
-            tempbx &= 0xDFFF;
-          }
+     if(!(SiS_Pr->SiS_HiVision & 0x03)) {
+        if(SiS_Pr->SiS_VGAHDE >= 1024) {
+           if((!(modeflag & HalfDCLK)) || (HwDeviceExtension->jChipType < SIS_315H)) {
+              tempcx = 0x1920;
+              if(SiS_Pr->SiS_VGAHDE >= 1280) {
+                 tempcx = 0x1420;
+                 tempbx &= 0xDFFF;
+              }
+           }
         }
-      }
-    }
+     }
   }
 
   if(!(tempbx & 0x2000)) {
-    if(modeflag & HalfDCLK) {
-         tempcx = (tempcx & 0xFF00) | (((tempcx & 0x00FF) << 1) & 0xff);
-    }
-    push1 = tempbx;
-    tempeax = SiS_Pr->SiS_VGAHDE;
-    tempebx = (tempcx & 0xFF00) >> 8;
-    longtemp = tempeax * tempebx;
-    tempecx = tempcx & 0x00FF;
-    longtemp /= tempecx;
-    longtemp <<= 0x0d;
-    if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
+     if(modeflag & HalfDCLK) {
+        tempcx = (tempcx & 0xFF00) | ((tempcx << 1) & 0x00FF);
+     }
+     longtemp = (SiS_Pr->SiS_VGAHDE * ((tempcx & 0xFF00) >> 8)) / (tempcx & 0x00FF);
+     longtemp <<= 13;
+     if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
      	longtemp <<= 3;
-    }
-    tempecx = SiS_Pr->SiS_HDE;
-    temp2 = longtemp % tempecx;
-    tempeax = longtemp / tempecx;
-    if(temp2 != 0) tempeax++;
-    tempax = (USHORT)tempeax;
-    tempbx = push1;
-    tempcx = (tempcx & 0xff00) | (((tempax & 0xFF00) >> 8) >> 5);
-    tempbx |= (tempax & 0x1F00);
-    tempax = ((tempax & 0x00FF) << 8) | (tempax & 0x00FF);
+     }
+     tempeax = longtemp / SiS_Pr->SiS_HDE;
+     if(longtemp % SiS_Pr->SiS_HDE) tempeax++;
+     tempax = (USHORT)tempeax;
+     tempcx = (tempcx & 0xFF00) | ((tempax & 0xFF00) >> (8 + 5));
+     tempbx |= (tempax & 0x1F00);
+     tempax = ((tempax & 0x00FF) << 8) | (tempax & 0x00FF);
   }
 
   temp = (tempax & 0xFF00) >> 8;
@@ -7430,33 +7505,32 @@
   SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x45,0xC0,temp);
 
   if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-       temp = tempcx & 0x00FF;
-       if(tempbx & 0x2000) temp = 0;
-       temp |= 0x18;
-       SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x46,0xE0,temp);
-       if(SiS_Pr->SiS_VBInfo & SetPALTV) {
-             tempbx = 0x0382;  
-             tempcx = 0x007e;  
-       } else {
-             tempbx = 0x0369;  
-             tempcx = 0x0061;  
-       }
-       temp = (tempbx & 0x00FF) ;
-       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x4B,temp);
-       temp = (tempcx & 0x00FF) ;
-       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x4C,temp);
-       tempbx &= 0x03FF;
-       temp = (tempcx & 0xFF00) >> 8;
-       temp = (temp & 0x0003) << 2;
-       temp |= (tempbx >> 8);
-       if(HwDeviceExtension->jChipType < SIS_315H) {
-          SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x4D,temp);
-       } else {
-          SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x4D,0xF0,temp);
-       }
+     temp = tempcx & 0x00FF;
+     if(tempbx & 0x2000) temp = 0;
+     temp |= 0x18;
+     SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x46,0xE0,temp);
 
-       temp = SiS_GetReg1(SiS_Pr->SiS_Part2Port,0x43);
-       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x43,(USHORT)(temp - 3));
+     if(SiS_Pr->SiS_VBInfo & SetPALTV) {
+        tempbx = 0x0382;
+        tempcx = 0x007e;
+     } else {
+        tempbx = 0x0369;
+        tempcx = 0x0061;
+     }
+     temp = (tempbx & 0x00FF) ;
+     SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x4B,temp);
+     temp = (tempcx & 0x00FF) ;
+     SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x4C,temp);
+     temp = (tempcx & 0x0300) >> (8 - 2);
+     temp |= ((tempbx >> 8) & 0x03);
+     if(HwDeviceExtension->jChipType < SIS_315H) {
+        SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x4D,temp);
+     } else {
+        SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x4D,0xF0,temp);
+     }
+
+     temp = SiS_GetReg1(SiS_Pr->SiS_Part2Port,0x43);
+     SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x43,(USHORT)(temp - 3));
   }
 
   temp = 0;
@@ -7467,23 +7541,33 @@
      temp = 0x38;
   }
   if(temp) {
-      if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
-          if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x31) & 0x01) {
-               if(SiS_GetReg1(SiS_Pr->SiS_P3d4,temp) & EnablePALM) {  /* 0x40 */
-                     SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x00,0xEF);
-                     temp = SiS_GetReg1(SiS_Pr->SiS_Part2Port,0x01);
-                     SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x01,temp - 1);
-               }
-          }
-      }
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+        if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x31) & 0x01) {
+           if(SiS_GetReg1(SiS_Pr->SiS_P3d4,temp) & EnablePALM) {  /* 0x40 */
+              SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x00,0xEF);
+              temp = SiS_GetReg1(SiS_Pr->SiS_Part2Port,0x01);
+              SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x01,temp - 1);
+           }
+        }
+     }
+  }
+
+  if(HwDeviceExtension->jChipType >= SIS_315H) {
+     if((SiS_Pr->SiS_VBType & VB_SIS301B302B) && (!(SiS_Pr->SiS_VBType & VB_NoLCD))) {
+        if(!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
+           SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x0B,0x00);
+        }
+     }
   }
 
+#if 0  /* Old: Why HiVision? */
   if( (SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) &&
       (!(SiS_Pr->SiS_VBType & VB_SIS301LV302LV)) ) {
-    if(!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
-      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x0B,0x00);
-    }
+     if(!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
+        SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x0B,0x00);
+     }
   }
+#endif
 
   if(HwDeviceExtension->jChipType < SIS_315H) {
      if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
@@ -7492,74 +7576,43 @@
 	return;
      }
   } else {
-     /* TW: !!! The following is a duplicate, done for LCDA as well (see above) */
+     /* !!! The following is a duplicate, done for LCDA as well (see above) */
      if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
-       if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {    
-         if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
-           if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) {
-             if((ModeNo == 0x4a) || (ModeNo == 0x38)) {
-               SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x1c,0xa7);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x1d,0x07);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x1e,0xf2);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x1f,0x6e);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x20,0x17);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x21,0x8b);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x22,0x73);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x23,0x53);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x24,0x13);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x25,0x40);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x26,0x34);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x27,0xf4);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x28,0x63);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x29,0xbb);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2a,0xcc);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2b,0x7a);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2c,0x58);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2d,0xe4);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2e,0x73);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2f,0xda);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x30,0x13);
-	       SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x43,0x72);
-	     }
-           }
-         }
-       }
-       return;
+        SiS_SetTVSpecial(SiS_Pr, ModeNo);
+        return;
      }
   }
 
-  /* TW: From here: Part2 LCD setup */
+  /* From here: Part2 LCD setup */
 
   tempbx = SiS_Pr->SiS_HDE;
   if(HwDeviceExtension->jChipType >= SIS_315H) {
-      /* TW: 650/30xLV 1.10.6s */
-      if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x39) & 0x04) tempbx >>= 1;
+     /* 650/30xLV 1.10.6s */
+     if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x39) & 0x04) tempbx >>= 1;
   }
   tempbx--;			         	/* RHACTE=HDE-1 */
   temp = tempbx & 0x00FF;
   SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2C,temp);
-  temp = (tempbx & 0xFF00) >> 8;
-  temp <<= 4;
+  temp = (tempbx & 0xFF00) >> 4;
   SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x2B,0x0F,temp);
 
   temp = 0x01;
   if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
-    if(SiS_Pr->SiS_ModeType == ModeEGA) {
-      if(SiS_Pr->SiS_VGAHDE >= 1024) {
-        temp = 0x02;
-	if(HwDeviceExtension->jChipType >= SIS_315H) {
-           if(SiS_Pr->SiS_SetFlag & LCDVESATiming) {
-             temp = 0x01;
+     if(SiS_Pr->SiS_ModeType == ModeEGA) {
+        if(SiS_Pr->SiS_VGAHDE >= 1024) {
+           temp = 0x02;
+	   if(HwDeviceExtension->jChipType >= SIS_315H) {
+              if(SiS_Pr->SiS_SetFlag & LCDVESATiming) {
+                 temp = 0x01;
+	      }
 	   }
-	}
-      }
-    }
+        }
+     }
   }
   SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x0B,temp);
 
-  tempbx = SiS_Pr->SiS_VDE;         		/* RTVACTEO=(VDE-1)&0xFF */
-  push1 = tempbx;
-
+  tempbx = SiS_Pr->SiS_VDE;         		/* RTVACTEO = VDE - 1 */
+  /* push1 = tempbx; */
   tempbx--;
   temp = tempbx & 0x00FF;
   SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x03,temp);
@@ -7567,10 +7620,9 @@
   SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x0C,0xF8,temp);
 
   tempcx = SiS_Pr->SiS_VT;
-  push2 = tempcx;
-
+  /* push2 = tempcx; */
   tempcx--;
-  temp = tempcx & 0x00FF;  			 /* RVTVT=VT-1 */
+  temp = tempcx & 0x00FF;  			 /* RVTVT = VT - 1 */
   SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x19,temp);
 
   temp = (tempcx & 0xFF00) >> 8;
@@ -7578,30 +7630,26 @@
   
   /* Enable dithering; newer versions only do this for 32bpp mode */
   if((HwDeviceExtension->jChipType == SIS_300) && (SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV)) {
-    if(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit) temp |= 0x10;
+     if(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit) temp |= 0x10;
   } else if(HwDeviceExtension->jChipType < SIS_315H) {
-    if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) temp |= 0x10;
-    else {
-      if(SiS_Pr->SiS_LCDInfo & LCDSync)       /* TW: 630/301 BIOS checks this */
-         temp |= 0x10;
-    }
+     temp |= 0x10;
   } else {
-      if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
-         /* TW: 650/30xLV 1.10.6s */
-         if(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit) {
-            if(SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x00) & 0x01) {  /* 32bpp mode? */
-      	       temp |= 0x10;
-	    }
-         }
-      } else {
-         temp |= 0x10;
-      }
+     if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
+        /* 650/30xLV 1.10.6s */
+        if(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit) {
+           if(SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x00) & 0x01) {  /* 32bpp mode? */
+              temp |= 0x10;
+	   }
+        }
+     } else {
+        temp |= 0x10;
+     }
   }
 
   /* 630/301 does not do all this */
   if((SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) && (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD)) {
      if((HwDeviceExtension->jChipType >= SIS_315H) && (SiS_Pr->SiS_VBType & VB_SIS301LV302LV)) {
-        /* TW: 650/30xLV 1.10.6s */
+        /* 650/30xLV 1.10.6s */
         temp |= (SiS_GetReg1(SiS_Pr->SiS_P3d4,0x37) >> 6);
 	temp |= 0x08;   					/* From 1.10.7w */
 	if(!(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit)) temp |= 0x04; 	/* From 1.10.7w */
@@ -7622,18 +7670,15 @@
   SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x17,0xFB);
   SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x18,0xDF);
 
-  /* 1280x960, 1280x1024 and 1600x1200 data invalid/missing in tables, use old calculation */
-  if((HwDeviceExtension->jChipType >= SIS_315H)             &&
-     (SiS_Pr->SiS_VBType & VB_SIS301LV302LV)                &&
-     (SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1280x1024) &&
-     (SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1280x768)  &&
-     (SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1600x1200) &&
-     (SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1280x960)) {
+  /* much data invalid/missing in tables, use old calculation in such case */
+  if((HwDeviceExtension->jChipType >= SIS_315H)         &&
+     (SiS_Pr->SiS_VBType & VB_SIS301LV302LV)            &&
+     ((SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768)  ||
+      (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) ||
+      (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050)) ) {
      
-#ifdef SIS315H 							/* ------------- 310/325/330 series ------------ */
+#ifdef SIS315H 							/* ------------- 315/330 series ------------ */
 
-      /* TW: Inserted this entire section from 650/301LV(x) BIOS */
-      
       /* Using this on the 301B with an auto-expanding 1024 panel (CR37=1) results
        * in a black bar in modes < 1024; if the panel is non-expanding, the bridge
        * scales all modes to 1024. All modes in both variants (exp/non-exp) work.
@@ -7681,8 +7726,7 @@
 	  }
 	  SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2f,temp);
 	  SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x30,0xb3);
-	}
-	if(SiS_Pr->SiS_VGAVDE == 420) {
+	} else if(SiS_Pr->SiS_VGAVDE == 420) {
 	  temp = 0x4d;
 	  if(SiS_Pr->SiS_ModeType <= ModeVGA) {
 	     temp++;
@@ -7692,7 +7736,6 @@
 	}
      }
 
-     /* TW: 650/30xLV 1.10.6s: */
      /* !!! This is a duplicate, done for LCDA as well - see above */
      if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
         if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) {
@@ -7711,60 +7754,46 @@
        * the bridge scales all modes to 1024.
        * !!! Malfunction at 640x480 and 640x400 when panel is auto-expanding - black screen !!!
        */
-  
+
+    /* cx = VT - 1 */
+
     tempcx++;
-    
-    if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768)       tempbx =  768;
-    else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) tempbx = 1024;
-    else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) tempbx = 1200;
-    else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768)  tempbx =  768;
-    else if(SiS_Pr->SiS_VDE != 1024) 				  tempbx =  960;
-    else            						  tempbx = 1024;
-    
-#if 0  /* old */
-    tempbx = 768;
-    if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1024x768) {
-      tempbx = 1024;
-      if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1280x1024) {
-         tempbx = 1200;
-         if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1600x1200) {
-            if(tempbx != SiS_Pr->SiS_VDE) {
-               tempbx = 960;
-            }
-         }
-      }
-    }
-#endif
-    
+
+    tempbx = SiS_Pr->PanelYRes;
+
     if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
-      tempbx = SiS_Pr->SiS_VDE - 1;
-      tempcx--;
+       tempbx = SiS_Pr->SiS_VDE - 1;
+       tempcx--;
     }
-    
+
     tempax = 1;
     if(!(SiS_Pr->SiS_LCDInfo & DontExpandLCD)) {
-      if(tempbx != SiS_Pr->SiS_VDE) {
-        tempax = tempbx;
-/*	if(SiS_Pr->SiS_VGAVDE == 525) tempax += 60;   in 650/301B BIOS */
-        if(tempax < SiS_Pr->SiS_VDE) {
-          tempax = 0;
-          tempcx = 0;
-        } else {
-          tempax -= SiS_Pr->SiS_VDE;
-        }
-        tempax >>= 1;
-      }
-      tempcx -= tempax; /* lcdvdes */
-      tempbx -= tempax; /* lcdvdee */
-    } else {
-      tempax >>= 1;
-      tempcx -= tempax; /* lcdvdes */
-      tempbx -= tempax; /* lcdvdee */
+       if(tempbx != SiS_Pr->SiS_VDE) {
+          tempax = tempbx;
+          if(tempax < SiS_Pr->SiS_VDE) {
+             tempax = 0;
+             tempcx = 0;
+          } else {
+             tempax -= SiS_Pr->SiS_VDE;
+          }
+          tempax >>= 1;
+       }
+       tempcx -= tempax; /* lcdvdes */
+       tempbx -= tempax; /* lcdvdee */
     }
-    
+#if 0  /* meaningless: 1 / 2 = 0... */
+    else {
+       tempax >>= 1;
+       tempcx -= tempax; /* lcdvdes */
+       tempbx -= tempax; /* lcdvdee */
+    }
+#endif
+
+    /* Non-expanding: lcdvdees = tempcx = VT-1; lcdvdee = tempbx = VDE-1 */
+
 #ifdef TWDEBUG
-    xf86DrvMsg(0, X_INFO, "lcdvds 0x%x lcdvde 0x%x\n", tempcx, tempbx);
-#endif    
+    xf86DrvMsg(0, X_INFO, "lcdvdes 0x%x lcdvdee 0x%x\n", tempcx, tempbx);
+#endif
 
     temp = tempcx & 0x00FF;   				/* RVEQ1EQ=lcdvdes */
     SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x05,temp);
@@ -7775,140 +7804,178 @@
     temp |= ((tempcx & 0xFF00) >> 8);
     SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x02,temp);
 
-    tempbx = push2;
-    tempax = push1;
-    tempcx = tempbx;
-    tempcx -= tempax;
-    tempcx >>= 4;
+    tempbx = SiS_Pr->SiS_VT;    /* push2; */
+    tempax = SiS_Pr->SiS_VDE;   /* push1; */
+    tempcx = (tempbx - tempax) >> 4;
     tempbx += tempax;
     tempbx >>= 1;
     if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)  tempbx -= 10;
-    
+
+    /* non-expanding: lcdvrs = tempbx = ((VT + VDE) / 2) - 10 */
+
+    if(SiS_Pr->UseCustomMode) {
+       tempbx = SiS_Pr->CVSyncStart;
+    }
+
 #ifdef TWDEBUG
     xf86DrvMsg(0, X_INFO, "lcdvrs 0x%x\n", tempbx);
 #endif
 
-    temp = tempbx & 0x00FF;   				/* RTVACTEE=lcdvrs */
+    temp = tempbx & 0x00FF;   				/* RTVACTEE = lcdvrs */
     SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x04,temp);
 
     temp = ((tempbx & 0xFF00) >> 8) << 4;
     tempbx += (tempcx + 1);
     temp |= (tempbx & 0x000F);
+
+    if(SiS_Pr->UseCustomMode) {
+       temp &= 0xf0;
+       temp |= (SiS_Pr->CVSyncEnd & 0x0f);
+    }
+
+#ifdef TWDEBUG
+    xf86DrvMsg(0, X_INFO, "lcdvre[3:0] 0x%x\n", (temp & 0x0f));
+#endif
+
     SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x01,temp);
 
-    /* TW: Code from 630/301B (I+II) BIOS */
+    /* Code from 630/301B (I+II) BIOS */
 
-    if( ( ( (HwDeviceExtension->jChipType == SIS_630) ||
-            (HwDeviceExtension->jChipType == SIS_730) ) &&
-          (HwDeviceExtension->jChipRevision > 2) )  &&
-        (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) &&
-        (!(SiS_Pr->SiS_SetFlag & LCDVESATiming))  &&
-        (!(SiS_Pr->SiS_LCDInfo & DontExpandLCD)) ) {
-            if(ModeNo == 0x13) {
-              SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x04,0xB9);
-              SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x05,0xCC);
-              SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x06,0xA6);
-            } else {
-              if((crt2crtc & 0x3F) == 4) {
+    if(!SiS_Pr->UseCustomMode) {
+       if( ( ( (HwDeviceExtension->jChipType == SIS_630) ||
+               (HwDeviceExtension->jChipType == SIS_730) ) &&
+             (HwDeviceExtension->jChipRevision > 2) )  &&
+           (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) &&
+           (!(SiS_Pr->SiS_SetFlag & LCDVESATiming))  &&
+           (!(SiS_Pr->SiS_LCDInfo & DontExpandLCD)) ) {
+          if(ModeNo == 0x13) {
+             SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x04,0xB9);
+             SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x05,0xCC);
+             SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x06,0xA6);
+          } else {
+             if((crt2crtc & 0x3F) == 4) {
                 SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x01,0x2B);
                 SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x02,0x13);
                 SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x04,0xE5);
                 SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x05,0x08);
                 SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x06,0xE2);
-              }
-            }
+             }
+          }
+       }
     }
 
-    /* TW: Inserted missing code from 630/301B BIOS;
-     *     Strangely, this is done in all 650 BIOSes as
-     *     well (although LCDTypeInfo is not used there
-     *     in the same way as on 300 series)
-     */
-
-    if(SiS_Pr->SiS_LCDTypeInfo == 0x0c) {
-         crt2crtc &= 0x1f;
-         tempcx = 0;
-         if(!(SiS_Pr->SiS_VBInfo & SetNotSimuMode)) {
-           if (SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
-              tempcx += 7;
-           }
-         }
-         tempcx += crt2crtc;
-         if (crt2crtc >= 4) {
-           SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x06,0xff);
-         }
+#ifdef SIS300
+    if(HwDeviceExtension->jChipType < SIS_315H) {
+       if(!SiS_Pr->UseCustomMode) {
+          if(SiS_Pr->SiS_LCDTypeInfo == 0x0c) {
+             crt2crtc &= 0x1f;
+             tempcx = 0;
+             if(!(SiS_Pr->SiS_VBInfo & SetNotSimuMode)) {
+                if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
+                   tempcx += 7;
+                }
+             }
+             tempcx += crt2crtc;
+             if(crt2crtc >= 4) {
+                SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x06,0xff);
+             }
 
-         if(!(SiS_Pr->SiS_VBInfo & SetNotSimuMode)) {
-           if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
-             if(crt2crtc == 4) {
-                SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x01,0x28);
+             if(!(SiS_Pr->SiS_VBInfo & SetNotSimuMode)) {
+                if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
+                   if(crt2crtc == 4) {
+                      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x01,0x28);
+                   }
+                }
              }
-           }
-         }
-         SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x02,0x18);
-         SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x04,atable[tempcx]);
+             SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x02,0x18);
+             SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x04,atable[tempcx]);
+          }
+       }
     }
+#endif
 
-    tempcx = (SiS_Pr->SiS_HT - SiS_Pr->SiS_HDE) >> 2;     /* (HT-HDE)>>2     */
+    tempcx = (SiS_Pr->SiS_HT - SiS_Pr->SiS_HDE) >> 2;     /* (HT - HDE) >> 2 */
     tempbx = SiS_Pr->SiS_HDE + 7;            		  /* lcdhdee         */
     if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-         tempbx += 2;
+       tempbx += 2;
     }
     push1 = tempbx;
+
 #ifdef TWDEBUG
-    xf86DrvMsg(0, X_INFO, "lcdhde 0x%x\n", tempbx);
-#endif    
-    temp = tempbx & 0x00FF;    			          /* RHEQPLE=lcdhdee */
+    xf86DrvMsg(0, X_INFO, "lcdhdee 0x%x\n", tempbx);
+#endif
+
+    temp = tempbx & 0x00FF;    			          /* RHEQPLE = lcdhdee */
     SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x23,temp);
     temp = (tempbx & 0xFF00) >> 8;
     SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x25,0xF0,temp);
 
     temp = 7;
     if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-         temp += 2;
+       temp += 2;
     }
-    SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x1F,temp);  	  /* RHBLKE=lcdhdes[7:0] */
+    SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x1F,temp);  	  /* RHBLKE = lcdhdes[7:0] */
     SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x20,0x0F);	  /* lcdhdes [11:8] */
 
     tempbx += tempcx;
     push2 = tempbx;
+
+    if(SiS_Pr->UseCustomMode) {
+       tempbx = SiS_Pr->CHSyncStart + 7;
+       if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
+          tempbx += 2;
+       }
+    }
+
 #ifdef TWDEBUG
     xf86DrvMsg(0, X_INFO, "lcdhrs 0x%x\n", tempbx);
 #endif
-    temp = tempbx & 0x00FF;            		          /* RHBURSTS=lcdhrs */
-    if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
-       if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
-          if(SiS_Pr->SiS_HDE == 1280) temp = 0x47;
+
+    temp = tempbx & 0x00FF;            		          /* RHBURSTS = lcdhrs */
+    if(!SiS_Pr->UseCustomMode) {
+       if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
+          if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
+             if(SiS_Pr->SiS_HDE == 1280) temp = 0x47;
+          }
        }
     }
     SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x1C,temp);
-    temp = ((tempbx & 0xFF00) >> 8) << 4;
+    temp = (tempbx & 0x0F00) >> 4;
     SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x1D,0x0F,temp);
 
     tempbx = push2;
     tempcx <<= 1;
     tempbx += tempcx;
+
+    if(SiS_Pr->UseCustomMode) {
+       tempbx = SiS_Pr->CHSyncEnd + 7;
+       if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
+          tempbx += 2;
+       }
+    }
+
 #ifdef TWDEBUG
     xf86DrvMsg(0, X_INFO, "lcdhre 0x%x\n", tempbx);
-#endif    
-    temp = tempbx & 0x00FF;            		          /* RHSYEXP2S=lcdhre */
+#endif
+
+    temp = tempbx & 0x00FF;            		          /* RHSYEXP2S = lcdhre */
     SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x21,temp);
 
     if(!(SiS_Pr->SiS_SetFlag & LCDVESATiming)) {
-      if(SiS_Pr->SiS_VGAVDE == 525) {
-        if(SiS_Pr->SiS_ModeType <= ModeVGA)
-    	   temp=0xC6;
-        else
-       	   temp=0xC3;
-        SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2f,temp);
-        SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x30,0xB3);
-      } else if(SiS_Pr->SiS_VGAVDE == 420) {
-        if(SiS_Pr->SiS_ModeType <= ModeVGA)
-	   temp=0x4F;
-        else
-       	   temp=0x4D;   
-        SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2f,temp);
-      }
+       if(SiS_Pr->SiS_VGAVDE == 525) {
+          if(SiS_Pr->SiS_ModeType <= ModeVGA)
+    	     temp=0xC6;
+          else
+       	     temp=0xC3;
+          SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2f,temp);
+          SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x30,0xB3);
+       } else if(SiS_Pr->SiS_VGAVDE == 420) {
+          if(SiS_Pr->SiS_ModeType <= ModeVGA)
+	     temp=0x4F;
+          else
+       	     temp=0x4D;
+          SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2f,temp);
+       }
     }
     SiS_Set300Part2Regs(SiS_Pr, HwDeviceExtension, ModeIdIndex,
                         RefreshRateTableIndex, BaseAddr, ModeNo);
@@ -7927,7 +7994,7 @@
   return((USHORT) tempax);
 }
 
-/* TW: New from 300/301LV BIOS 1.16.51 for ECS A907. Seems highly preliminary. */
+/* New from 300/301LV BIOS 1.16.51 for ECS A907. Seems highly preliminary. */
 void
 SiS_Set300Part2Regs(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,
     			USHORT ModeIdIndex, USHORT RefreshRateTableIndex,
@@ -7939,18 +8006,19 @@
 
   if(HwDeviceExtension->jChipType != SIS_300) return;
   if(!(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV)) return;
+  if(SiS_Pr->UseCustomMode) return;
 
-  if(ModeNo<=0x13) {
-    	crt2crtc = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_CRT2CRTC;
+  if(ModeNo <= 0x13) {
+     crt2crtc = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_CRT2CRTC;
   } else {
-    	crt2crtc = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
+     crt2crtc = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
   }
 
   resindex = crt2crtc & 0x3F;
   if(SiS_Pr->SiS_SetFlag & LCDVESATiming) CRT2Part2Ptr = SiS_Pr->SiS_CRT2Part2_1024x768_1;
   else                                    CRT2Part2Ptr = SiS_Pr->SiS_CRT2Part2_1024x768_2;
 
-  /* TW: The BIOS code (1.16.51) is obviously a fragment! */
+  /* The BIOS code (1.16.51) is obviously a fragment! */
   if(ModeNo > 0x13) {
      CRT2Part2Ptr = SiS_Pr->SiS_CRT2Part2_1024x768_1;
      resindex = 4;
@@ -7971,7 +8039,6 @@
   SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x25,0x0f,(CRT2Part2Ptr+resindex)->CR[11]);
 }
 
-/* TW: Set 301 Macrovision(tm) registers */
 void
 SiS_SetGroup3(SiS_Private *SiS_Pr, USHORT  BaseAddr,UCHAR *ROMAddr,USHORT ModeNo,
               USHORT ModeIdIndex,PSIS_HW_DEVICE_INFO HwDeviceExtension)
@@ -7983,24 +8050,35 @@
 
   if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) return;
 
-  if(ModeNo<=0x13)
-    	modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
-  else
+  if(ModeNo<=0x13) {
+     modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
+  } else {
+     if(SiS_Pr->UseCustomMode) {
+        modeflag = SiS_Pr->CModeFlag;
+     } else {
     	modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
+     }
+  }
 
+#ifndef SIS_CP
   SiS_SetReg1(SiS_Pr->SiS_Part3Port,0x00,0x00);
+#endif  
+
+#ifdef SIS_CP
+  SIS_CP_INIT301_CP
+#endif
 
   if(SiS_Pr->SiS_VBInfo & SetPALTV) {
-    SiS_SetReg1(SiS_Pr->SiS_Part3Port,0x13,0xFA);
-    SiS_SetReg1(SiS_Pr->SiS_Part3Port,0x14,0xC8);
+     SiS_SetReg1(SiS_Pr->SiS_Part3Port,0x13,0xFA);
+     SiS_SetReg1(SiS_Pr->SiS_Part3Port,0x14,0xC8);
   } else {
-    if(HwDeviceExtension->jChipType >= SIS_315H) {
-      SiS_SetReg1(SiS_Pr->SiS_Part3Port,0x13,0xF5);
-      SiS_SetReg1(SiS_Pr->SiS_Part3Port,0x14,0xB7);
-    } else {
-      SiS_SetReg1(SiS_Pr->SiS_Part3Port,0x13,0xF6);
-      SiS_SetReg1(SiS_Pr->SiS_Part3Port,0x14,0xBf);
-    }
+     if(HwDeviceExtension->jChipType >= SIS_315H) {
+        SiS_SetReg1(SiS_Pr->SiS_Part3Port,0x13,0xF5);
+        SiS_SetReg1(SiS_Pr->SiS_Part3Port,0x14,0xB7);
+     } else {
+        SiS_SetReg1(SiS_Pr->SiS_Part3Port,0x13,0xF6);
+        SiS_SetReg1(SiS_Pr->SiS_Part3Port,0x14,0xBf);
+     }
   }
 
   temp = 0;
@@ -8008,41 +8086,44 @@
      (HwDeviceExtension->jChipType == SIS_730)) {
      temp = 0x35;
   } else if(HwDeviceExtension->jChipType >= SIS_315H) {
-     temp = 0x38;
-  }
-  if(temp) {
-      if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
-          if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x31) & 0x01) {
-              if(SiS_GetReg1(SiS_Pr->SiS_P3d4,temp) & EnablePALM){  /* 0x40 */
-                  SiS_SetReg1(SiS_Pr->SiS_Part3Port,0x13,0xFA);
-                  SiS_SetReg1(SiS_Pr->SiS_Part3Port,0x14,0xC8);
-                  SiS_SetReg1(SiS_Pr->SiS_Part3Port,0x3D,0xA8);
-              }
-          }
-      }
+     temp = 0x38;
+  }
+  if(temp) {
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+        if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x31) & 0x01) {
+           if(SiS_GetReg1(SiS_Pr->SiS_P3d4,temp) & EnablePALM){  /* 0x40 */
+              SiS_SetReg1(SiS_Pr->SiS_Part3Port,0x13,0xFA);
+              SiS_SetReg1(SiS_Pr->SiS_Part3Port,0x14,0xC8);
+              SiS_SetReg1(SiS_Pr->SiS_Part3Port,0x3D,0xA8);
+           }
+        }
+     }
   }
 
   if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) {
-    tempdi = SiS_Pr->SiS_HiTVGroup3Data;
-    if(SiS_Pr->SiS_SetFlag & TVSimuMode) {
-      tempdi = SiS_Pr->SiS_HiTVGroup3Simu;
-      if(!(modeflag & Charx8Dot)) {
-        tempdi = SiS_Pr->SiS_HiTVGroup3Text;
-      }
-    }
-    if(SiS_Pr->SiS_HiVision & 0x03) {
-       tempdi = SiS_HiTVGroup3_1;
-       if(SiS_Pr->SiS_HiVision & 0x02) tempdi = SiS_HiTVGroup3_2;
-    }
-    for(i=0; i<=0x3E; i++){
-       SiS_SetReg1(SiS_Pr->SiS_Part3Port,i,tempdi[i]);
-    }
+     tempdi = SiS_Pr->SiS_HiTVGroup3Data;
+     if(SiS_Pr->SiS_SetFlag & TVSimuMode) {
+        tempdi = SiS_Pr->SiS_HiTVGroup3Simu;
+        if(!(modeflag & Charx8Dot)) {
+           tempdi = SiS_Pr->SiS_HiTVGroup3Text;
+        }
+     }
+     if(SiS_Pr->SiS_HiVision & 0x03) {
+        tempdi = SiS_HiTVGroup3_1;
+        if(SiS_Pr->SiS_HiVision & 0x02) tempdi = SiS_HiTVGroup3_2;
+     }
+     for(i=0; i<=0x3E; i++){
+        SiS_SetReg1(SiS_Pr->SiS_Part3Port,i,tempdi[i]);
+     }
   }
 
-  return;
+#ifdef SIS_CP
+  SIS_CP_INIT301_CP2
+#endif
+
 }
 
-/* TW: Set 301 VGA2 registers */
+/* Set 301 VGA2 registers */
 void
 SiS_SetGroup4(SiS_Private *SiS_Pr, USHORT  BaseAddr,UCHAR *ROMAddr,USHORT ModeNo,
               USHORT ModeIdIndex,USHORT RefreshRateTableIndex,
@@ -8051,17 +8132,20 @@
   USHORT tempax,tempcx,tempbx,modeflag,temp,temp2,resinfo;
   ULONG tempebx,tempeax,templong;
 
-
-  if(ModeNo<=0x13) {
-    	modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
-	resinfo = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ResInfo;
+  if(ModeNo <= 0x13) {
+     modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
+     resinfo = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ResInfo;
   } else {
+     if(SiS_Pr->UseCustomMode) {
+        modeflag = SiS_Pr->CModeFlag;
+	resinfo = 0;
+     } else {
     	modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
 	resinfo = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO;
+     }
   }
 
   if(HwDeviceExtension->jChipType >= SIS_315H) {
-      /* TW: From 650/302LV 1.10.6s (not for 300/301LV - no LCDA on this combination) */
      if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
         if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
            SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x24,0x0e);
@@ -8078,10 +8162,10 @@
   if(HwDeviceExtension->jChipType >= SIS_315H) {
      if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
         if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
-	   /* TW: From 650/301LV (any, incl. 1.10.6s, 1.10.7w) */
-  	   /* TW: This is a duplicate; done at the end, too */
+	   /* From 650/301LV (any, incl. 1.10.6s, 1.10.7w) */
+  	   /* This is a duplicate; done at the end, too */
 	   if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x39) & 0x04) {
-		SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x27,0x2c);
+	      SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x27,0x2c);
 	   }
 	   SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x2a,0x00);
 	   SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x30,0x00);
@@ -8119,39 +8203,33 @@
   tempbx = SiS_Pr->SiS_VGAHDE;
   if(modeflag & HalfDCLK)  tempbx >>= 1;
 
-  /* TW: New for 650/301LV and 630/301B */
   temp = 0xA0;
   if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) {
-       temp = 0;
-       if(tempbx > 800) {
-          temp = 0xA0;
-          if(tempbx != 1024) {
-             temp = 0xC0;
-             if(tempbx != 1280) temp = 0;
-	  }
-       }
-  } else
-    if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
-      if(tempbx <= 800) {
-         temp = 0x80;
-	 if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
-            temp = 0;
-            if(tempbx > 800) temp = 0x60;
-         }
-      }
+     temp = 0;
+     if(tempbx > 800) {
+        temp = 0xA0;
+        if(tempbx != 1024) {
+           temp = 0xC0;
+           if(tempbx != 1280) temp = 0;
+	}
+     }
+  } else if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+     if(tempbx <= 800) {
+        temp = 0x80;
+     }
   } else {
-      temp = 0x80;
-      if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
-            temp = 0;
-            if(tempbx > 800) temp = 0x60;
-      }
+     temp = 0x80;
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
+        temp = 0;
+        if(tempbx > 800) temp = 0x60;
+     }
   }
   if(SiS_Pr->SiS_HiVision & 0x03) {
         temp = 0;
 	if(SiS_Pr->SiS_VGAHDE == 1024) temp = 0x20;
   }
   if(HwDeviceExtension->jChipType >= SIS_315H) {
-  	if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x39) & 0x04) temp = 0;
+     if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x39) & 0x04) temp = 0;
   }
 
   if(SiS_Pr->SiS_VBType & VB_SIS301) {
@@ -8173,10 +8251,10 @@
 
   tempeax = SiS_Pr->SiS_VGAVDE;
   tempcx |= 0x4000;
-  if(tempeax <= tempebx){
-    tempcx ^= 0x4000;
+  if(tempeax <= tempebx) {
+     tempcx ^= 0x4000;
   } else {
-    tempeax -= tempebx;
+     tempeax -= tempebx;
   }
 
   templong = (tempeax * 256 * 1024) % tempebx;
@@ -8202,64 +8280,64 @@
          tempax = SiS_Pr->SiS_VGAHDE;
          if(modeflag & HalfDCLK) tempax >>= 1;
          if((SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) || (SiS_Pr->SiS_HiVision & 0x03)) {
-	     if(HwDeviceExtension->jChipType >= SIS_315H) {
-	         if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x39) & 0x04) tempax >>= 1;
-		 else if(tempax > 800) tempax -= 800;
-	     } else {
-                 if(tempax > 800) tempax -= 800;
-             }
+	    if(HwDeviceExtension->jChipType >= SIS_315H) {
+	       if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x39) & 0x04) tempax >>= 1;
+	       else if(tempax > 800) tempax -= 800;
+	    } else {
+               if(tempax > 800) tempax -= 800;
+            }
          }
 
 /*       if((SiS_Pr->SiS_VBInfo & (SetCRT2ToTV | SetPALTV)) && (!(SiS_Pr->SiS_HiVision & 0x03))) {  */
  	 if((SiS_Pr->SiS_VBInfo & (SetCRT2ToTV - SetCRT2ToHiVisionTV)) && (!(SiS_Pr->SiS_HiVision & 0x03))) {
-           if(tempax > 800) {
-	      tempbx = 8;
-              if(tempax == 1024)
-	        tempax *= 25;
-              else
-	        tempax *= 20;
-
-	      temp = tempax % 32;
-	      tempax /= 32;
-	      tempax--;
-	      if (temp!=0) tempax++;
-           }
+            if(tempax > 800) {
+	       tempbx = 8;
+               if(tempax == 1024)
+	          tempax *= 25;
+               else
+	          tempax *= 20;
+
+	       temp = tempax % 32;
+	       tempax /= 32;
+	       tempax--;
+	       if (temp!=0) tempax++;
+            }
          }
 	 tempax--;
          temp = (tempax & 0xFF00) >> 8;
          temp &= 0x03;
-	 if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {		/* From 1.10.7w */
-	 	if(ModeNo > 0x13) {			/* From 1.10.7w */
-			if(resinfo == 8) tempax = 0x1f;	/* From 1.10.7w */
-		}					/* From 1.10.7w */
-	 }						/* From 1.10.7w */
+	 if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {			/* From 1.10.7w */
+	    if(ModeNo > 0x13) {					/* From 1.10.7w */
+	       if(resinfo == SIS_RI_1024x768) tempax = 0x1f;	/* From 1.10.7w */
+	    }							/* From 1.10.7w */
+	 }							/* From 1.10.7w */
 	 SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x1D,tempax & 0x00FF);
 	 temp <<= 4;
 	 temp |= tempbx;
 	 SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x1E,temp);
 
 	 if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
-	    if(IS_SIS650740) {
-	        temp = 0x0026;  /* 1.10.7w; 1.10.8r; needs corresponding code in Dis/EnableBridge! */
+	    if(IS_SIS550650740660) {
+	       temp = 0x0026;  /* 1.10.7w; 1.10.8r; needs corresponding code in Dis/EnableBridge! */
 	    } else {
-	        temp = 0x0036;
+	       temp = 0x0036;
 	    }
 	 } else {
-	     temp = 0x0036;
+	    temp = 0x0036;
 	 }
          if((SiS_Pr->SiS_VBInfo & (SetCRT2ToTV - SetCRT2ToHiVisionTV)) &&
 	                               (!(SiS_Pr->SiS_HiVision & 0x03))) {
-		temp |= 0x01;
-	        if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
-	          if(!(SiS_Pr->SiS_SetFlag & TVSimuMode))
-  	                  temp &= 0xFE;
-		}
+	    temp |= 0x01;
+	    if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
+	       if(!(SiS_Pr->SiS_SetFlag & TVSimuMode))
+  	          temp &= 0xFE;
+	    }
          }
          SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x1F,0xC0,temp);
 
 	 tempbx = SiS_Pr->SiS_HT;
 	 if(HwDeviceExtension->jChipType >= SIS_315H) {
-	 	if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x39) & 0x04) tempbx >>= 1;
+	    if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x39) & 0x04) tempbx >>= 1;
 	 }
          tempbx >>= 1;
 	 tempbx -= 2;
@@ -8267,7 +8345,7 @@
          SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x21,0xC0,temp);
          temp = tempbx & 0x00FF;
          SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x22,temp);
-	 
+
          if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
 	    if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
                SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x24,0x0e);
@@ -8275,24 +8353,24 @@
 	 }
 
 	 if(HwDeviceExtension->jChipType >= SIS_315H) {
-	     /* TW: 650/LV BIOS does this for all bridge types - assumingly wrong */
-	     /* 315, 330, 650+301B BIOS don't do this at all */
-             /* TW: This is a duplicate; done for LCDA as well (see above) */
-	     if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
-	        if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x39) & 0x04) {
-		   SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x27,0x2c);
-	        }
-	        SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x2a,0x00);
-	        SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x30,0x00);
-	        SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x34,0x10);
-	     }
+	    /* 650/LV BIOS does this for all bridge types - assumingly wrong */
+	    /* 315, 330, 650+301B BIOS don't do this at all */
+            /* This is a duplicate; done for LCDA as well (see above) */
+	    if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
+	       if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x39) & 0x04) {
+		  SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x27,0x2c);
+	       }
+	       SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x2a,0x00);
+	       SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x30,0x00);
+	       SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x34,0x10);
+	    }
          } else if(HwDeviceExtension->jChipType == SIS_300) {
-	     /* TW: 300/301LV BIOS does this for all bridge types - assumingly wrong */
-	     if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
-	        SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x2a,0x00);
-	        SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x30,0x00);
-	        SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x34,0x10);
-	     }
+	    /* 300/301LV BIOS does this for all bridge types - assumingly wrong */
+	    if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
+	       SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x2a,0x00);
+	       SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x30,0x00);
+	       SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x34,0x10);
+	    }
 	 }
 
   }  /* 301B */
@@ -8307,21 +8385,26 @@
                  USHORT RefreshRateTableIndex,PSIS_HW_DEVICE_INFO HwDeviceExtension)
 {
   USHORT vclkindex;
-  USHORT tempah;
+  USHORT temp, reg1, reg2;
 
-  vclkindex = SiS_GetVCLK2Ptr(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
-                              HwDeviceExtension);
+  if(SiS_Pr->UseCustomMode) {
+     reg1 = SiS_Pr->CSR2B;
+     reg2 = SiS_Pr->CSR2C;
+  } else {
+     vclkindex = SiS_GetVCLK2Ptr(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
+                                 HwDeviceExtension);
+     reg1 = SiS_Pr->SiS_VBVCLKData[vclkindex].Part4_A;
+     reg2 = SiS_Pr->SiS_VBVCLKData[vclkindex].Part4_B;
+  }
 
   if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-     tempah = SiS_Pr->SiS_VBVCLKData[vclkindex].Part4_A;
-     SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x0A,tempah);
-     tempah = SiS_Pr->SiS_VBVCLKData[vclkindex].Part4_B;
-     SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x0B,tempah);
+     SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x0A,reg1);
+     SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x0B,reg2);
      if(HwDeviceExtension->jChipType >= SIS_315H) {
 	if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
            if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
 	      if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) {
-                 if((ModeNo == 0x4a) || (ModeNo == 0x38)) {
+                 if((ModeNo == 0x64) || (ModeNo == 0x4a) || (ModeNo == 0x38)) {
 		    SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x0a,0x57);
 		    SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x0b,0x46);
 		    SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x1f,0xf6);
@@ -8332,15 +8415,13 @@
      }
   } else {	
      SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x0A,0x01);
-     tempah = SiS_Pr->SiS_VBVCLKData[vclkindex].Part4_B;
-     SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x0B,tempah);
-     tempah = SiS_Pr->SiS_VBVCLKData[vclkindex].Part4_A;
-     SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x0A,tempah);
+     SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x0B,reg2);
+     SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x0A,reg1);
   }
   SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x12,0x00);
-  tempah = 0x08;
-  if(SiS_Pr->SiS_VBInfo & SetCRT2ToRAMDAC) tempah |= 0x20;
-  SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x12,tempah);
+  temp = 0x08;
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToRAMDAC) temp |= 0x20;
+  SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x12,temp);
 }
 
 USHORT
@@ -8348,23 +8429,24 @@
                 USHORT RefreshRateTableIndex,PSIS_HW_DEVICE_INFO HwDeviceExtension)
 {
   USHORT tempbx;
-  const USHORT LCDXlat0VCLK[4]    = {VCLK40, VCLK40, VCLK40, VCLK40};
-  const USHORT LVDSXlat1VCLK[4]   = {VCLK40, VCLK40, VCLK40, VCLK40};
+  const USHORT LCDXlat0VCLK[4]    = {VCLK40,       VCLK40,       VCLK40,       VCLK40};
+  const USHORT LVDSXlat1VCLK[4]   = {VCLK40,       VCLK40,       VCLK40,       VCLK40};
+  const USHORT LVDSXlat4VCLK[4]   = {VCLK28,       VCLK28,       VCLK28,       VCLK28};
 #ifdef SIS300
-  const USHORT LCDXlat1VCLK300[4] = {VCLK65,   VCLK65,   VCLK65,   VCLK65};
-  const USHORT LCDXlat2VCLK300[4] = {VCLK108_2,VCLK108_2,VCLK108_2,VCLK108_2};
-  const USHORT LVDSXlat2VCLK300[4]= {VCLK65,   VCLK65,   VCLK65,   VCLK65};
-  const USHORT LVDSXlat3VCLK300[4]= {VCLK65,   VCLK65,   VCLK65,   VCLK65};
+  const USHORT LCDXlat1VCLK300[4] = {VCLK65_300,   VCLK65_300,   VCLK65_300,   VCLK65_300};
+  const USHORT LCDXlat2VCLK300[4] = {VCLK108_2_300,VCLK108_2_300,VCLK108_2_300,VCLK108_2_300};
+  const USHORT LVDSXlat2VCLK300[4]= {VCLK65_300,   VCLK65_300,   VCLK65_300,   VCLK65_300};
+  const USHORT LVDSXlat3VCLK300[4]= {VCLK65_300,   VCLK65_300,   VCLK65_300,   VCLK65_300};
 #endif
 #ifdef SIS315H
-  const USHORT LCDXlat1VCLK310[4] = {VCLK65+2,   VCLK65+2,   VCLK65+2,   VCLK65+2};
-  const USHORT LCDXlat2VCLK310[4] = {VCLK108_2+5,VCLK108_2+5,VCLK108_2+5,VCLK108_2+5};
-  const USHORT LVDSXlat2VCLK310[4]= {VCLK65+2,   VCLK65+2,   VCLK65+2,   VCLK65+2};
-  const USHORT LVDSXlat3VCLK310[4]= {VCLK108_2+5,VCLK108_2+5,VCLK108_2+5,VCLK108_2+5};
+  const USHORT LCDXlat1VCLK310[4] = {VCLK65_315,   VCLK65_315,   VCLK65_315,   VCLK65_315};
+  const USHORT LCDXlat2VCLK310[4] = {VCLK108_2_315,VCLK108_2_315,VCLK108_2_315,VCLK108_2_315};
+  const USHORT LVDSXlat2VCLK310[4]= {VCLK65_315,   VCLK65_315,   VCLK65_315,   VCLK65_315};
+  const USHORT LVDSXlat3VCLK310[4]= {VCLK108_2_315,VCLK108_2_315,VCLK108_2_315,VCLK108_2_315};
 #endif
   USHORT CRT2Index,VCLKIndex=0;
   USHORT modeflag,resinfo;
-  const UCHAR *CHTVVCLKPtr=NULL;
+  const UCHAR  *CHTVVCLKPtr = NULL;
   const USHORT *LCDXlatVCLK1 = NULL;
   const USHORT *LCDXlatVCLK2 = NULL;
   const USHORT *LVDSXlatVCLK2 = NULL;
@@ -8401,32 +8483,38 @@
      if(SiS_Pr->SiS_SetFlag & ProgrammingCRT2) {
 
         CRT2Index >>= 6;
-        if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)){      /*  LCD */
+        if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {      /*  LCD */
             if(HwDeviceExtension->jChipType < SIS_315H) {
-	       if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600)
+	       if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600) {
 	    		VCLKIndex = LCDXlat0VCLK[CRT2Index];
-	       else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768)
+	       } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
 	    		VCLKIndex = LCDXlatVCLK1[CRT2Index];
-	       else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x600)
+	       } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x600) {
 	    		VCLKIndex = LCDXlatVCLK1[CRT2Index];
-	       else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1152x768)
+	       } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1152x768) {
 	    		VCLKIndex = LCDXlatVCLK1[CRT2Index];
-	       else
+	       } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768) {
+	                VCLKIndex = VCLK81_300;	/* guessed */
+	       } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x960) {
+		        VCLKIndex = VCLK108_3_300;
+		        if(resinfo == SIS_RI_1280x1024) VCLKIndex = VCLK100_300;
+	       } else {
 	    		VCLKIndex = LCDXlatVCLK2[CRT2Index];
+	       }
 	    } else {
 	       if( (SiS_Pr->SiS_VBType & VB_SIS301LV302LV) ||
 	           (!(SiS_Pr->SiS_LCDInfo & DontExpandLCD)) ) {
       	          if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
-		     VCLKIndex = 0x19;
+		     VCLKIndex = VCLK108_2_315;
 		  } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768) {
-		     VCLKIndex = 0x12;  /* TEMP - guessed */
+		     VCLKIndex = VCLK81_315;  	/* guessed */
 		  } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) {
-		     VCLKIndex = 0x19;
+		     VCLKIndex = VCLK108_2_315;
 		  } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) {
-		     VCLKIndex = 0x21;
+		     VCLKIndex = VCLK162_315;
 		  } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x960) {
-		     VCLKIndex = 0x45;  /* TW: in VBVCLK table */
-		     if(resinfo == 0x09) VCLKIndex++;
+		     VCLKIndex = VCLK108_3_315;
+		     if(resinfo == SIS_RI_1280x1024) VCLKIndex = VCLK100_315;
 		  } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
 		     VCLKIndex = LCDXlatVCLK1[CRT2Index];
 	          } else {
@@ -8465,9 +8553,11 @@
        			if(SiS_Pr->SiS_SetFlag & RPLLDIV2XO)  VCLKIndex = TVVCLKDIV2;
             		else         		              VCLKIndex = TVVCLK;
           	}
-		if(HwDeviceExtension->jChipType >= SIS_315H) {
-              		VCLKIndex += 25;
-  		}
+		if(HwDeviceExtension->jChipType < SIS_315H) {
+              		VCLKIndex += TVCLKBASE_300;
+  		} else {
+			VCLKIndex += TVCLKBASE_315;
+		}
         } else {         					/* RAMDAC2 */
         	VCLKIndex = (UCHAR)SiS_GetReg2((USHORT)(SiS_Pr->SiS_P3ca+0x02));
         	VCLKIndex = ((VCLKIndex >> 2) & 0x03);
@@ -8477,7 +8567,11 @@
           			VCLKIndex &= 0x3f;
 				if( (HwDeviceExtension->jChipType == SIS_630) &&
 				    (HwDeviceExtension->jChipRevision >= 0x30)) {
-				     if(VCLKIndex == 0x14) VCLKIndex = 0x2e;
+				     /* This is certainly wrong: It replaces clock
+				      * 108 by 47...
+				      */
+				     /* if(VCLKIndex == 0x14) VCLKIndex = 0x2e; */
+				     if(VCLKIndex == 0x14) VCLKIndex = 0x34;
 				}
 			}
         	}
@@ -8541,6 +8635,10 @@
      		if((SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600) ||
 		   (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel320x480))
      			VCLKIndex = LVDSXlat1VCLK[VCLKIndex];
+		else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480   ||
+		        SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_2 ||
+			SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_3)
+			VCLKIndex = LVDSXlat4VCLK[VCLKIndex];
      		else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768)
      			VCLKIndex = LVDSXlatVCLK2[VCLKIndex];
 		else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x600)
@@ -8549,6 +8647,11 @@
                         VCLKIndex = LVDSXlatVCLK2[VCLKIndex];			
      		else    VCLKIndex = LVDSXlatVCLK3[VCLKIndex];
 
+		if(SiS_Pr->SiS_CustomT == CUT_BARCO1366) {
+		   /* Special Timing: Barco iQ Pro R300/400 */
+		   VCLKIndex = 0x44;
+		}
+
 	   } else {
 
 	        VCLKIndex = (UCHAR)SiS_GetReg2((USHORT)(SiS_Pr->SiS_P3ca+0x02));
@@ -8592,11 +8695,11 @@
 #ifdef TWDEBUG
   xf86DrvMsg(0, X_INFO, "VCLKIndex %d (0x%x)\n", VCLKIndex, VCLKIndex);
 #endif
-  return (VCLKIndex);
+  return(VCLKIndex);
 }
 
-/* TW: Set 301 Palette address port registers */
-/* TW: Checked against 650/301LV BIOS */
+/* Set 301 Palette address port registers */
+/* Checked against 650/301LV BIOS */
 void
 SiS_SetGroup5(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension, USHORT BaseAddr,
               UCHAR *ROMAddr, USHORT ModeNo, USHORT ModeIdIndex)
@@ -8604,7 +8707,7 @@
 
   if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)  return;
 
-  if(SiS_Pr->SiS_ModeType == ModeVGA){
+  if(SiS_Pr->SiS_ModeType == ModeVGA) {
      if(!(SiS_Pr->SiS_VBInfo & (SetInSlaveMode | LoadDACFlag))){
         SiS_EnableCRT2(SiS_Pr);
         SiS_LoadDAC(SiS_Pr,HwDeviceExtension,ROMAddr,ModeNo,ModeIdIndex);
@@ -8617,17 +8720,21 @@
                 USHORT RefreshRateTableIndex,PSIS_HW_DEVICE_INFO HwDeviceExtension)
 {
   USHORT temp,tempah,i,modeflag,j;
-  USHORT ResInfo,DisplayType;
+  USHORT ResIndex,DisplayType;
   const SiS_LVDSCRT1DataStruct *LVDSCRT1Ptr=NULL;
 
   if(ModeNo <= 0x13) {
-    	modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
+     modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
   } else {
-    	modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
+     modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
   }
 
+  if((SiS_Pr->SiS_CustomT == CUT_BARCO1366) ||
+     (SiS_Pr->SiS_CustomT == CUT_BARCO1024))
+     return;
+
   temp = SiS_GetLVDSCRT1Ptr(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
-                            &ResInfo,&DisplayType);
+                            &ResIndex,&DisplayType);
 
   if(temp == 0) return;
 
@@ -8675,47 +8782,53 @@
     case 41: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11280x768_1_H;        break;
     case 42: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11280x768_2;          break;
     case 43: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11280x768_2_H;        break;
+    case 50: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT1640x480_1;           break;
+    case 51: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT1640x480_1_H;         break;
+    case 52: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT1640x480_2;           break;
+    case 53: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT1640x480_2_H;         break;
+    case 54: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT1640x480_3;           break;
+    case 55: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT1640x480_3_H;         break;
     case 99: LVDSCRT1Ptr = SiS_Pr->SiS_CHTVCRT1SOPAL;               break;
     default: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11024x768_1;          break;
   }
 
   SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x11,0x7f);                        /*unlock cr0-7  */
 
-  tempah = (LVDSCRT1Ptr+ResInfo)->CR[0];
+  tempah = (LVDSCRT1Ptr + ResIndex)->CR[0];
   SiS_SetReg1(SiS_Pr->SiS_P3d4,0x00,tempah);
 
   for(i=0x02,j=1;i<=0x05;i++,j++){
-    tempah = (LVDSCRT1Ptr+ResInfo)->CR[j];
+    tempah = (LVDSCRT1Ptr + ResIndex)->CR[j];
     SiS_SetReg1(SiS_Pr->SiS_P3d4,i,tempah);
   }
   for(i=0x06,j=5;i<=0x07;i++,j++){
-    tempah = (LVDSCRT1Ptr+ResInfo)->CR[j];
+    tempah = (LVDSCRT1Ptr + ResIndex)->CR[j];
     SiS_SetReg1(SiS_Pr->SiS_P3d4,i,tempah);
   }
   for(i=0x10,j=7;i<=0x11;i++,j++){
-    tempah = (LVDSCRT1Ptr+ResInfo)->CR[j];
+    tempah = (LVDSCRT1Ptr + ResIndex)->CR[j];
     SiS_SetReg1(SiS_Pr->SiS_P3d4,i,tempah);
   }
   for(i=0x15,j=9;i<=0x16;i++,j++){
-    tempah = (LVDSCRT1Ptr+ResInfo)->CR[j];
+    tempah = (LVDSCRT1Ptr + ResIndex)->CR[j];
     SiS_SetReg1(SiS_Pr->SiS_P3d4,i,tempah);
   }
   for(i=0x0A,j=11;i<=0x0C;i++,j++){
-    tempah = (LVDSCRT1Ptr+ResInfo)->CR[j];
+    tempah = (LVDSCRT1Ptr + ResIndex)->CR[j];
     SiS_SetReg1(SiS_Pr->SiS_P3c4,i,tempah);
   }
 
-  tempah = (LVDSCRT1Ptr+ResInfo)->CR[14];
+  tempah = (LVDSCRT1Ptr + ResIndex)->CR[14];
   tempah &= 0xE0;
-  SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x0E,0x1f,tempah);     
+  SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x0E,0x1f,tempah);
 
-  tempah = (LVDSCRT1Ptr+ResInfo)->CR[14];
+  tempah = (LVDSCRT1Ptr + ResIndex)->CR[14];
   tempah &= 0x01;
   tempah <<= 5;
   if(modeflag & DoubleScanMode)  tempah |= 0x080;
   SiS_SetRegANDOR(SiS_Pr->SiS_P3d4,0x09,~0x020,tempah);
 
-  /* TW: 650/LVDS BIOS - doesn't make sense */
+  /* 650/LVDS BIOS - doesn't make sense */
   if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
      if(modeflag & HalfDCLK)
         SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x11,0x7f);
@@ -8724,7 +8837,7 @@
 
 BOOLEAN
 SiS_GetLVDSCRT1Ptr(SiS_Private *SiS_Pr, UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex,
-		   USHORT RefreshRateTableIndex,USHORT *ResInfo,
+		   USHORT RefreshRateTableIndex,USHORT *ResIndex,
 		   USHORT *DisplayType)
  {
   USHORT tempbx,modeflag=0;
@@ -8793,19 +8906,33 @@
            tempbx = 40;
 	   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) tempbx += 2;
 	   if(modeflag & HalfDCLK) tempbx++;
+        } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_3) {
+           tempbx = 54;
+	   if(modeflag & HalfDCLK) tempbx++;
+	} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_2) {
+           tempbx = 52;
+	   if(modeflag & HalfDCLK) tempbx++;
+	} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480) {
+           tempbx = 50;
+	   if(modeflag & HalfDCLK) tempbx++;
         }
+
      }
      if(SiS_Pr->SiS_LCDInfo & LCDPass11) {
         tempbx = 12;
 	if(modeflag & HalfDCLK) tempbx++;
      }
   }
-  if(SiS_Pr->SiS_IF_DEF_FSTN){
+
+#if 0
+  if(SiS_Pr->SiS_IF_DEF_FSTN) {
      if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel320x480){
         tempbx = 22;
      }
   }
-  *ResInfo = CRT2CRTC & 0x3F;
+#endif
+
+  *ResIndex = CRT2CRTC & 0x3F;
   *DisplayType = tempbx;
   return 1;
 }
@@ -8814,54 +8941,54 @@
 SiS_SetCRT2ECLK(SiS_Private *SiS_Pr, UCHAR *ROMAddr, USHORT ModeNo,USHORT ModeIdIndex,
            USHORT RefreshRateTableIndex,PSIS_HW_DEVICE_INFO HwDeviceExtension)
 {
-  USHORT tempah,tempal,pushax;
-  USHORT vclkindex=0;
-    
+  USHORT clkbase, vclkindex=0;
+  UCHAR  sr2b, sr2c;
+
   if((SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480) || (SiS_Pr->SiS_IF_DEF_TRUMPION == 1)) {
 	SiS_Pr->SiS_SetFlag &= (~ProgrammingCRT2);
-        tempal = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
-    	tempal &= 0x3F;
-	if(tempal == 2) RefreshRateTableIndex--;
+        if((SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRTVCLK & 0x3f) == 2) {
+	   RefreshRateTableIndex--;
+	}
 	vclkindex = SiS_GetVCLK2Ptr(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,
-                               RefreshRateTableIndex,HwDeviceExtension);
+                                    RefreshRateTableIndex,HwDeviceExtension);
 	SiS_Pr->SiS_SetFlag |= ProgrammingCRT2;
   } else {
         vclkindex = SiS_GetVCLK2Ptr(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,
-                               RefreshRateTableIndex,HwDeviceExtension);
+                                    RefreshRateTableIndex,HwDeviceExtension);
   }
-  
-  tempal = 0x02B;
+
+  sr2b = SiS_Pr->SiS_VCLKData[vclkindex].SR2B;
+  sr2c = SiS_Pr->SiS_VCLKData[vclkindex].SR2C;
+
+  if((SiS_Pr->SiS_CustomT == CUT_BARCO1366) || (SiS_Pr->SiS_CustomT == CUT_BARCO1024)) {
+     if((ROMAddr) && SiS_Pr->SiS_UseROM) {
+	if(ROMAddr[0x220] & 0x01) {
+           sr2b = ROMAddr[0x227];
+	   sr2c = ROMAddr[0x228];
+	}
+     }
+  }
+
+  clkbase = 0x02B;
   if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)) {
      if(!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
-    	tempal += 3;
+    	clkbase += 3;
      }
   }
+
   SiS_SetReg1(SiS_Pr->SiS_P3c4,0x05,0x86);
-  pushax = tempal;
   SiS_SetReg1(SiS_Pr->SiS_P3c4,0x31,0x20);
-  tempah = SiS_Pr->SiS_VCLKData[vclkindex].SR2B;
-  SiS_SetReg1(SiS_Pr->SiS_P3c4,tempal,tempah);
-  tempal++;
-  tempah = SiS_Pr->SiS_VCLKData[vclkindex].SR2C;
-  SiS_SetReg1(SiS_Pr->SiS_P3c4,tempal,tempah);
+  SiS_SetReg1(SiS_Pr->SiS_P3c4,clkbase,sr2b);
+  SiS_SetReg1(SiS_Pr->SiS_P3c4,clkbase+1,sr2c);
   SiS_SetReg1(SiS_Pr->SiS_P3c4,0x31,0x10);
-  tempal = pushax;
-  tempah = SiS_Pr->SiS_VCLKData[vclkindex].SR2B;
-  SiS_SetReg1(SiS_Pr->SiS_P3c4,tempal,tempah);
-  tempal++;
-  tempah = SiS_Pr->SiS_VCLKData[vclkindex].SR2C;
-  SiS_SetReg1(SiS_Pr->SiS_P3c4,tempal,tempah);
+  SiS_SetReg1(SiS_Pr->SiS_P3c4,clkbase,sr2b);
+  SiS_SetReg1(SiS_Pr->SiS_P3c4,clkbase+1,sr2c);
   SiS_SetReg1(SiS_Pr->SiS_P3c4,0x31,0x00);
-  tempal = pushax;
-  tempah = SiS_Pr->SiS_VCLKData[vclkindex].SR2B;
-  SiS_SetReg1(SiS_Pr->SiS_P3c4,tempal,tempah);
-  tempal++;
-  tempah = SiS_Pr->SiS_VCLKData[vclkindex].SR2C;
-  SiS_SetReg1(SiS_Pr->SiS_P3c4,tempal,tempah);
-  return;
+  SiS_SetReg1(SiS_Pr->SiS_P3c4,clkbase,sr2b);
+  SiS_SetReg1(SiS_Pr->SiS_P3c4,clkbase+1,sr2c);
 }
 
-#if 0  /* TW: Not used */
+#if 0  /* Not used */
 void
 SiS_SetDefCRT2ExtRegs(SiS_Private *SiS_Pr, USHORT BaseAddr)
 {
@@ -8880,7 +9007,7 @@
 }
 #endif
 
-/* TW: Start of Chrontel 70xx functions ---------------------- */
+/* Start of Chrontel 70xx functions ---------------------- */
 
 /* Set-up the Chrontel Registers */
 void
@@ -8926,44 +9053,44 @@
   if(SiS_Pr->SiS_IF_DEF_CH70xx == 1) {
 
 #ifdef SIS300
-  
-     /* Chrontel 7005 - I assume that it does not come with a 310/325 series chip */
 
-     /* TW: We don't support modes >800x600 */
+     /* Chrontel 7005 - I assume that it does not come with a 315 series chip */
+
+     /* We don't support modes >800x600 */
      if (resindex > 5) return;
 
      if(SiS_Pr->SiS_VBInfo & SetPALTV) {
-    	SiS_SetCH700x(SiS_Pr,0x4304);   /* TW: 0x40=76uA (PAL); 0x03=15bit non-multi RGB*/
-    	SiS_SetCH700x(SiS_Pr,0x6909);	/* TW: Black level for PAL (105)*/
+    	SiS_SetCH700x(SiS_Pr,0x4304);   /* 0x40=76uA (PAL); 0x03=15bit non-multi RGB*/
+    	SiS_SetCH700x(SiS_Pr,0x6909);	/* Black level for PAL (105)*/
      } else {
-    	SiS_SetCH700x(SiS_Pr,0x0304);   /* TW: upper nibble=71uA (NTSC), 0x03=15bit non-multi RGB*/
-    	SiS_SetCH700x(SiS_Pr,0x7109);	/* TW: Black level for NTSC (113)*/
+    	SiS_SetCH700x(SiS_Pr,0x0304);   /* upper nibble=71uA (NTSC), 0x03=15bit non-multi RGB*/
+    	SiS_SetCH700x(SiS_Pr,0x7109);	/* Black level for NTSC (113)*/
      }
 
      temp = CHTVRegData[resindex].Reg[0];
-     tempbx=((temp&0x00FF)<<8)|0x00;	/* TW: Mode register */
+     tempbx=((temp&0x00FF)<<8)|0x00;	/* Mode register */
      SiS_SetCH700x(SiS_Pr,tempbx);
      temp = CHTVRegData[resindex].Reg[1];
-     tempbx=((temp&0x00FF)<<8)|0x07;	/* TW: Start active video register */
+     tempbx=((temp&0x00FF)<<8)|0x07;	/* Start active video register */
      SiS_SetCH700x(SiS_Pr,tempbx);
      temp = CHTVRegData[resindex].Reg[2];
-     tempbx=((temp&0x00FF)<<8)|0x08;	/* TW: Position overflow register */
+     tempbx=((temp&0x00FF)<<8)|0x08;	/* Position overflow register */
      SiS_SetCH700x(SiS_Pr,tempbx);
      temp = CHTVRegData[resindex].Reg[3];
-     tempbx=((temp&0x00FF)<<8)|0x0A;	/* TW: Horiz Position register */
+     tempbx=((temp&0x00FF)<<8)|0x0A;	/* Horiz Position register */
      SiS_SetCH700x(SiS_Pr,tempbx);
      temp = CHTVRegData[resindex].Reg[4];
-     tempbx=((temp&0x00FF)<<8)|0x0B;	/* TW: Vertical Position register */
+     tempbx=((temp&0x00FF)<<8)|0x0B;	/* Vertical Position register */
      SiS_SetCH700x(SiS_Pr,tempbx);
 
-     /* TW: Set minimum flicker filter for Luma channel (SR1-0=00),
+     /* Set minimum flicker filter for Luma channel (SR1-0=00),
                 minimum text enhancement (S3-2=10),
    	        maximum flicker filter for Chroma channel (S5-4=10)
 	        =00101000=0x28 (When reading, S1-0->S3-2, and S3-2->S1-0!)
       */
      SiS_SetCH700x(SiS_Pr,0x2801);
 
-     /* TW: Set video bandwidth
+     /* Set video bandwidth
             High bandwith Luma composite video filter(S0=1)
             low bandwith Luma S-video filter (S2-1=00)
 	    disable peak filter in S-video channel (S3=0)
@@ -8972,22 +9099,24 @@
      */
      SiS_SetCH700x(SiS_Pr,0xb103);       /* old: 3103 */
 
-     /* TW: Register 0x3D does not exist in non-macrovision register map
+     /* Register 0x3D does not exist in non-macrovision register map
             (Maybe this is a macrovision register?)
       */
-     /* SiS_SetCH70xx(SiS_Pr,0x003D); */
+#ifndef SIS_CP
+     SiS_SetCH70xx(SiS_Pr,0x003D);
+#endif     
 
-     /* TW: Register 0x10 only contains 1 writable bit (S0) for sensing,
+     /* Register 0x10 only contains 1 writable bit (S0) for sensing,
             all other bits a read-only. Macrovision?
       */
      SiS_SetCH70xxANDOR(SiS_Pr,0x0010,0x1F);
 
-     /* TW: Register 0x11 only contains 3 writable bits (S0-S2) for
+     /* Register 0x11 only contains 3 writable bits (S0-S2) for
             contrast enhancement (set to 010 -> gain 1 Yout = 17/16*(Yin-30) )
       */
      SiS_SetCH70xxANDOR(SiS_Pr,0x0211,0xF8);
 
-     /* TW: Clear DSEN
+     /* Clear DSEN
       */
      SiS_SetCH70xxANDOR(SiS_Pr,0x001C,0xEF);
 
@@ -9030,7 +9159,7 @@
          }
        }
      } else {				/* ---- PAL ---- */
-           /* TW: We don't play around with FSCI in PAL mode */
+           /* We don't play around with FSCI in PAL mode */
          if (resindex == 0x04) {
            SiS_SetCH70xxANDOR(SiS_Pr,0x0020,0xEF); 	/* loop filter off */
            SiS_SetCH70xxANDOR(SiS_Pr,0x0121,0xFE);      /* ACIV on */
@@ -9048,7 +9177,7 @@
 
 #ifdef SIS315H
 
-     /* TW: We don't support modes >1024x768 */
+     /* We don't support modes >1024x768 */
      if (resindex > 6) return;
 
      temp = CHTVRegData[resindex].Reg[0];
@@ -9118,26 +9247,29 @@
 #endif	/* 315 */
 
   }
+
+#ifdef SIS_CP
+  SIS_CP_INIT301_CP3
+#endif
+
 }
 
-/* TW: Chrontel 701x functions ================================= */
+/* Chrontel 701x functions ================================= */
 
 void
-SiS_Chrontel701xBLOn(SiS_Private *SiS_Pr)
+SiS_Chrontel701xBLOn(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension)
 {
-#ifndef NEWCH701x
   USHORT temp;
-#endif  
 
-  /* TW: Enable Chrontel 7019 LCD panel backlight */
+  /* Enable Chrontel 7019 LCD panel backlight */
   if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
-#ifdef NEWCH701x
+     if(HwDeviceExtension->jChipType == SIS_740) {
         SiS_SetCH701x(SiS_Pr,0x6566);
-#else  
+     } else {
         temp = SiS_GetCH701x(SiS_Pr,0x66);
         temp |= 0x20;
 	SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x66);
-#endif	
+     }
   }
 }
 
@@ -9146,7 +9278,7 @@
 {
   USHORT temp;
 
-  /* TW: Disable Chrontel 7019 LCD panel backlight */
+  /* Disable Chrontel 7019 LCD panel backlight */
   if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
         temp = SiS_GetCH701x(SiS_Pr,0x66);
         temp &= 0xDF;
@@ -9154,47 +9286,54 @@
   }
 }
 
-#ifdef SIS315H  /* -------- 310/325 series only --------- */
+#ifdef SIS315H  /* ----------- 315 series only ---------- */
 
 void
 SiS_SetCH701xForLCD(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,USHORT BaseAddr)
 {
-#ifdef NEWCH701x  
-  UCHAR regtable[]  = { 0x1c, 0x5f, 0x64, 0x6f, 0x70, 0x71,
-                        0x72, 0x73, 0x74, 0x76, 0x78, 0x7d, 0x66 };
-  UCHAR table1024[] = { 0x60, 0x02, 0x00, 0x07, 0x40, 0xed,
-                        0xa3, 0xc8, 0xc7, 0xac, 0xe0, 0x02, 0x44 }; 
-  UCHAR table1280[] = { 0x60, 0x03, 0x11, 0x00, 0x40, 0xe3,
-   			0xad, 0xdb, 0xf6, 0xac, 0xe0, 0x02, 0x44 }; 			
-  UCHAR table1400[] = { 0x60, 0x03, 0x11, 0x00, 0x40, 0xe3,         
-                        0xad, 0xdb, 0xf6, 0xac, 0xe0, 0x02, 0x44 }; 
-  UCHAR table1600[] = { 0x60, 0x04, 0x11, 0x00, 0x40, 0xe3,
-  			0xad, 0xde, 0xf6, 0xac, 0x60, 0x1a, 0x44 };
-#else
-  UCHAR regtable[]  = { 0x1c, 0x5f, 0x64, 0x6f, 0x70, 0x71,
-                        0x72, 0x73, 0x74, 0x76, 0x78, 0x7d };
-  UCHAR table1024[] = { 0x60, 0x02, 0x00, 0x07, 0x40, 0xed,
-                        0xa3, 0xc8, 0xc7, 0xac, 0x60, 0x02 }; 
-  UCHAR table1280[] = { 0x60, 0x03, 0x11, 0x00, 0x40, 0xe3,
-   			0xad, 0xdb, 0xf6, 0xac, 0xe0, 0x02 }; 			
-  UCHAR table1400[] = { 0x60, 0x03, 0x11, 0x00, 0x40, 0xef,   
-                        0xad, 0xdb, 0xf6, 0xac, 0x60, 0x02 }; 
-  UCHAR table1600[] = { 0x60, 0x04, 0x11, 0x00, 0x40, 0xe3,
-  			0xad, 0xde, 0xf6, 0xac, 0x60, 0x1a };
-#endif			
+  UCHAR regtable[]      = { 0x1c, 0x5f, 0x64, 0x6f, 0x70, 0x71,
+                            0x72, 0x73, 0x74, 0x76, 0x78, 0x7d, 0x66 };
+  UCHAR table1024_740[] = { 0x60, 0x02, 0x00, 0x07, 0x40, 0xed,
+                            0xa3, 0xc8, 0xc7, 0xac, 0xe0, 0x02, 0x44 };
+  UCHAR table1280_740[] = { 0x60, 0x03, 0x11, 0x00, 0x40, 0xe3,
+   			    0xad, 0xdb, 0xf6, 0xac, 0xe0, 0x02, 0x44 };
+  UCHAR table1400_740[] = { 0x60, 0x03, 0x11, 0x00, 0x40, 0xe3,
+                            0xad, 0xdb, 0xf6, 0xac, 0xe0, 0x02, 0x44 };
+  UCHAR table1600_740[] = { 0x60, 0x04, 0x11, 0x00, 0x40, 0xe3,
+  			    0xad, 0xde, 0xf6, 0xac, 0x60, 0x1a, 0x44 };
+  UCHAR table1024_650[] = { 0x60, 0x02, 0x00, 0x07, 0x40, 0xed,
+                            0xa3, 0xc8, 0xc7, 0xac, 0x60, 0x02 };
+  UCHAR table1280_650[] = { 0x60, 0x03, 0x11, 0x00, 0x40, 0xe3,
+   		   	    0xad, 0xdb, 0xf6, 0xac, 0xe0, 0x02 };
+  UCHAR table1400_650[] = { 0x60, 0x03, 0x11, 0x00, 0x40, 0xef,
+                            0xad, 0xdb, 0xf6, 0xac, 0x60, 0x02 };
+  UCHAR table1600_650[] = { 0x60, 0x04, 0x11, 0x00, 0x40, 0xe3,
+  			    0xad, 0xde, 0xf6, 0xac, 0x60, 0x1a };
   UCHAR *tableptr = NULL;
   USHORT tempbh;
   int i;
 
-  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
-     tableptr = table1024;
-  } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
-     tableptr = table1280;
-  } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) {
-     tableptr = table1400;
-  } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) {
-     tableptr = table1600;
-  } else return;
+  if(HwDeviceExtension->jChipType == SIS_740) {
+     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
+        tableptr = table1024_740;
+     } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
+        tableptr = table1280_740;
+     } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) {
+        tableptr = table1400_740;
+     } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) {
+        tableptr = table1600_740;
+     } else return;
+  } else {
+     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
+        tableptr = table1024_650;
+     } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
+        tableptr = table1280_650;
+     } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) {
+        tableptr = table1400_650;
+     } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) {
+        tableptr = table1600_650;
+     } else return;
+  }
 
   tempbh = SiS_GetCH701x(SiS_Pr,0x74);
   if((tempbh == 0xf6) || (tempbh == 0xc7)) {
@@ -9208,54 +9347,64 @@
         if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) return;
      }
   }
-#ifdef NEWCH701x     /* New from 740/LVDS: */    
-  for(i=0; i<0x0d; i++) {	
-#else
-  for(i=0; i<0x0c; i++) {
-#endif  
+
+  if(HwDeviceExtension->jChipType == SIS_740) {
+     tempbh = 0x0d;
+  } else {
+     tempbh = 0x0c;
+  }
+  for(i = 0; i < tempbh; i++) {
      SiS_SetCH701x(SiS_Pr,(tableptr[i] << 8) | regtable[i]);
   }
-  SiS_ChrontelPowerSequencing(SiS_Pr);
+  SiS_ChrontelPowerSequencing(SiS_Pr,HwDeviceExtension);
   tempbh = SiS_GetCH701x(SiS_Pr,0x1e);
   tempbh |= 0xc0;
   SiS_SetCH701x(SiS_Pr,(tempbh << 8) | 0x1e);
-  
-#ifdef NEWCH701x     /* 740/LVDS: */
-  tempbh = SiS_GetCH701x(SiS_Pr,0x1c);
-  tempbh &= 0xfb;
-  SiS_SetCH701x(SiS_Pr,(tempbh << 8) | 0x1c);
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port, 0x2d, 0x03);
-  tempbh = SiS_GetCH701x(SiS_Pr,0x64);
-  tempbh |= 0x40;
-  SiS_SetCH701x(SiS_Pr,(tempbh << 8) | 0x64);
-  tempbh = SiS_GetCH701x(SiS_Pr,0x03);
-  tempbh &= 0x3f;
-  SiS_SetCH701x(SiS_Pr,(tempbh << 8) | 0x03);
-#endif  /* End 740/LVDS */
+
+  if(HwDeviceExtension->jChipType == SIS_740) {
+     tempbh = SiS_GetCH701x(SiS_Pr,0x1c);
+     tempbh &= 0xfb;
+     SiS_SetCH701x(SiS_Pr,(tempbh << 8) | 0x1c);
+     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x2d,0x03);
+     tempbh = SiS_GetCH701x(SiS_Pr,0x64);
+     tempbh |= 0x40;
+     SiS_SetCH701x(SiS_Pr,(tempbh << 8) | 0x64);
+     tempbh = SiS_GetCH701x(SiS_Pr,0x03);
+     tempbh &= 0x3f;
+     SiS_SetCH701x(SiS_Pr,(tempbh << 8) | 0x03);
+  }
 }
 
 void
-SiS_ChrontelPowerSequencing(SiS_Private *SiS_Pr)
-{
-  UCHAR regtable[]  = { 0x67, 0x68, 0x69, 0x6a, 0x6b };
-#ifdef NEWCH701x  
-  UCHAR table1024[] = { 0x01, 0x02, 0x01, 0x01, 0x01 };
-  UCHAR table1400[] = { 0x01, 0x6e, 0x01, 0x01, 0x01 };
-#else
-  UCHAR table1024[] = { 0x01, 0x02, 0x01, 0x01, 0x02 };
-  UCHAR table1400[] = { 0x01, 0x02, 0x01, 0x01, 0x02 };
-#endif  
+SiS_ChrontelPowerSequencing(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension)
+{
+  UCHAR regtable[]      = { 0x67, 0x68, 0x69, 0x6a, 0x6b };
+  UCHAR table1024_740[] = { 0x01, 0x02, 0x01, 0x01, 0x01 };
+  UCHAR table1400_740[] = { 0x01, 0x6e, 0x01, 0x01, 0x01 };
+  UCHAR table1024_650[] = { 0x01, 0x02, 0x01, 0x01, 0x02 };
+  UCHAR table1400_650[] = { 0x01, 0x02, 0x01, 0x01, 0x02 };
   UCHAR *tableptr = NULL;
   int i;
 
   /* Set up Power up/down timing */
-  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
-     tableptr = table1024;
-  } else if((SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) ||
-            (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) ||
-	    (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200)) {
-     tableptr = table1400;
-  } else return;
+
+  if(HwDeviceExtension->jChipType == SIS_740) {
+     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
+        tableptr = table1024_740;
+     } else if((SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) ||
+               (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) ||
+	       (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200)) {
+        tableptr = table1400_740;
+     } else return;
+  } else {
+     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
+        tableptr = table1024_650;
+     } else if((SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) ||
+               (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) ||
+	       (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200)) {
+        tableptr = table1400_650;
+     } else return;
+  }
   
   for(i=0; i<5; i++) {
      SiS_SetCH701x(SiS_Pr,(tableptr[i] << 8) | regtable[i]);
@@ -9268,66 +9417,72 @@
   USHORT temp;
 
   if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
-#ifdef NEWCH701x
-     temp = SiS_GetCH701x(SiS_Pr,0x1c);
-     temp |= 0x04;
-     SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x1c);
-#endif 
+     if(HwDeviceExtension->jChipType == SIS_740) {
+        temp = SiS_GetCH701x(SiS_Pr,0x1c);
+        temp |= 0x04;
+        SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x1c);
+     }
      if(SiS_IsYPbPr(SiS_Pr,HwDeviceExtension, BaseAddr)) {
         temp = SiS_GetCH701x(SiS_Pr,0x01);
 	temp &= 0x3f;
-	temp |= 0x80;	/* TW: Enable YPrPb (HDTV) */
+	temp |= 0x80;	/* Enable YPrPb (HDTV) */
 	SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x01);
      }
      if(SiS_IsChScart(SiS_Pr,HwDeviceExtension, BaseAddr)) {
         temp = SiS_GetCH701x(SiS_Pr,0x01);
 	temp &= 0x3f;
-	temp |= 0xc0;	/* TW: Enable SCART + CVBS */
+	temp |= 0xc0;	/* Enable SCART + CVBS */
 	SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x01);
      }
-#ifdef NEWCH701x
-     SiS_ChrontelDoSomething5(SiS_Pr);
-     SiS_SetCH701x(SiS_Pr,0x2049);   			/* TW: Enable TV path */
-#else      
-     SiS_SetCH701x(SiS_Pr,0x2049);   			/* TW: Enable TV path */
-     temp = SiS_GetCH701x(SiS_Pr,0x49);
-     if(SiS_IsYPbPr(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-        temp = SiS_GetCH701x(SiS_Pr,0x73);
-	temp |= 0x60;
-	SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x73);
+     if(HwDeviceExtension->jChipType == SIS_740) {
+        SiS_ChrontelDoSomething5(SiS_Pr);
+        SiS_SetCH701x(SiS_Pr,0x2049);   			/* Enable TV path */
+     } else {
+        SiS_SetCH701x(SiS_Pr,0x2049);   			/* Enable TV path */
+        temp = SiS_GetCH701x(SiS_Pr,0x49);
+        if(SiS_IsYPbPr(SiS_Pr,HwDeviceExtension, BaseAddr)) {
+           temp = SiS_GetCH701x(SiS_Pr,0x73);
+	   temp |= 0x60;
+	   SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x73);
+        }
+        temp = SiS_GetCH701x(SiS_Pr,0x47);
+        temp &= 0x7f;
+        SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x47);
+        SiS_LongDelay(SiS_Pr,2);
+        temp = SiS_GetCH701x(SiS_Pr,0x47);
+        temp |= 0x80;
+        SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x47);
      }
-     temp = SiS_GetCH701x(SiS_Pr,0x47);
-     temp &= 0x7f;
-     SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x47);
-     SiS_LongDelay(SiS_Pr,2);
-     temp = SiS_GetCH701x(SiS_Pr,0x47);
-     temp |= 0x80;
-     SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x47);
-#endif     
   }
 }
 
 void
-SiS_Chrontel701xOff(SiS_Private *SiS_Pr)
+SiS_Chrontel701xOff(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension)
 {
   USHORT temp;
 
+  /* Complete power down of LVDS */
   if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
+     if(HwDeviceExtension->jChipType == SIS_740) {
+        SiS_LongDelay(SiS_Pr,1);
+	SiS_GenericDelay(SiS_Pr,0x16ff);
+	SiS_SetCH701x(SiS_Pr,0xac76);
+	SiS_SetCH701x(SiS_Pr,0x0066);
+     } else {
         SiS_LongDelay(SiS_Pr,2);
-	/* TW: Complete power down of LVDS */
 	temp = SiS_GetCH701x(SiS_Pr,0x76);
 	temp &= 0xfc;
 	SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x76);
 	SiS_SetCH701x(SiS_Pr,0x0066);
+     }
   }
 }
 
-#ifdef NEWCH701x
 void
 SiS_ChrontelDoSomething5(SiS_Private *SiS_Pr)
 {
      unsigned char temp, temp1;
-     
+
      temp1 = SiS_GetCH701x(SiS_Pr,0x49);
      SiS_SetCH701x(SiS_Pr,0x3e49);
      temp = SiS_GetCH701x(SiS_Pr,0x47);
@@ -9339,130 +9494,134 @@
      SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x47);
      SiS_SetCH701x(SiS_Pr,(temp1 << 8) | 0x49);
 }
-#endif
 
 void
 SiS_ChrontelResetDB(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension, USHORT BaseAddr)
 {
-#ifdef NEWCH701x
      USHORT temp;
-     
-     /* 740/LVDS: */
-     temp = SiS_GetCH701x(SiS_Pr,0x4a);
-     temp &= 0x01;
-     if(!(temp)) {
-     
-        if(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-	   temp = SiS_GetCH701x(SiS_Pr,0x49);
-	   SiS_SetCH701x(SiS_Pr,0x3e49);
-	}
-	/* TW: Reset Chrontel 7019 datapath */
+
+     if(HwDeviceExtension->jChipType == SIS_740) {
+        temp = SiS_GetCH701x(SiS_Pr,0x4a);
+        temp &= 0x01;
+        if(!(temp)) {
+
+           if(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr)) {
+	      temp = SiS_GetCH701x(SiS_Pr,0x49);
+	      SiS_SetCH701x(SiS_Pr,0x3e49);
+	   }
+	   /* Reset Chrontel 7019 datapath */
+           SiS_SetCH701x(SiS_Pr,0x1048);
+           SiS_LongDelay(SiS_Pr,1);
+           SiS_SetCH701x(SiS_Pr,0x1848);
+
+	   if(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr)) {
+	      SiS_ChrontelDoSomething5(SiS_Pr);
+	      SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x49);
+	   }
+
+        } else {
+
+           temp = SiS_GetCH701x(SiS_Pr,0x5c);
+	   temp &= 0xef;
+	   SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x5c);
+	   temp = SiS_GetCH701x(SiS_Pr,0x5c);
+	   temp |= 0x10;
+	   SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x5c);
+	   temp = SiS_GetCH701x(SiS_Pr,0x5c);
+	   temp &= 0xef;
+	   SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x5c);
+	   temp = SiS_GetCH701x(SiS_Pr,0x61);
+	   if(!temp) {
+	      SiS_SetCH701xForLCD(SiS_Pr,HwDeviceExtension,BaseAddr);
+	   }
+        }
+     } else { /* 650 */
+        /* Reset Chrontel 7019 datapath */
         SiS_SetCH701x(SiS_Pr,0x1048);
         SiS_LongDelay(SiS_Pr,1);
         SiS_SetCH701x(SiS_Pr,0x1848);
-	
-	if(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-	   SiS_ChrontelDoSomething5(SiS_Pr);
-	   SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x49);
-	}    
-     } else {
-     
-        temp = SiS_GetCH701x(SiS_Pr,0x5c);
-	temp &= 0xef;
-	SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x5c);
-	temp = SiS_GetCH701x(SiS_Pr,0x5c);
-	temp |= 0x10;
-	SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x5c);
-	temp = SiS_GetCH701x(SiS_Pr,0x5c);
-	temp &= 0xef;
-	SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x5c);
-	temp = SiS_GetCH701x(SiS_Pr,0x61);
-	if(!temp) {
-	   SiS_SetCH701xForLCD(SiS_Pr,HwDeviceExtension,BaseAddr);
-	}
-     }
-#else /* pre 740/LVDS code */     
-     /* TW: Reset Chrontel 7019 datapath */
-     SiS_SetCH701x(SiS_Pr,0x1048);
-     SiS_LongDelay(SiS_Pr,1);
-     SiS_SetCH701x(SiS_Pr,0x1848);
-#endif     
+     }
 }
 
 void
 SiS_ChrontelDoSomething4(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension, USHORT BaseAddr)
 {
-#ifdef NEWCH701x
-     if(!(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-        SiS_ChrontelDoSomething5(SiS_Pr);
-     }
-#else
      USHORT temp;
 
-     SiS_SetCH701x(SiS_Pr,0xaf76);  /* Power up LVDS block */
-     temp = SiS_GetCH701x(SiS_Pr,0x49);
-     temp &= 1;
-     if(temp != 1) {  /* TV block powered? (0 = yes, 1 = no) */
-	temp = SiS_GetCH701x(SiS_Pr,0x47);
-	temp &= 0x70;
-	SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x47);  /* enable VSYNC */
-	SiS_LongDelay(SiS_Pr,3);
-	temp = SiS_GetCH701x(SiS_Pr,0x47);
-	temp |= 0x80;
-	SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x47);  /* disable VSYNC */
+     if(HwDeviceExtension->jChipType == SIS_740) {
+
+        if(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr)) {
+           SiS_ChrontelDoSomething5(SiS_Pr);
+        }
+
+     } else {
+
+        SiS_SetCH701x(SiS_Pr,0xaf76);  /* Power up LVDS block */
+        temp = SiS_GetCH701x(SiS_Pr,0x49);
+        temp &= 1;
+        if(temp != 1) {  /* TV block powered? (0 = yes, 1 = no) */
+	   temp = SiS_GetCH701x(SiS_Pr,0x47);
+	   temp &= 0x70;
+	   SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x47);  /* enable VSYNC */
+	   SiS_LongDelay(SiS_Pr,3);
+	   temp = SiS_GetCH701x(SiS_Pr,0x47);
+	   temp |= 0x80;
+	   SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x47);  /* disable VSYNC */
+        }
+
      }
-#endif     
 }
 
 void
 SiS_ChrontelDoSomething3(SiS_Private *SiS_Pr, USHORT ModeNo, PSIS_HW_DEVICE_INFO HwDeviceExtension,
                          USHORT BaseAddr)
 {
-#ifdef NEWCH701x
-     USHORT temp;
-     
-     temp = SiS_GetCH701x(SiS_Pr,0x61);
-     if(temp < 1) {
-          temp++;
-	  SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x61);
-     }
-     SiS_SetCH701x(SiS_Pr,0x4566);
-     SiS_SetCH701x(SiS_Pr,0xaf76);
-     SiS_LongDelay(SiS_Pr,1);
-     SiS_GenericDelay(SiS_Pr,0x16ff);
-
-#else
      USHORT temp,temp1;
-     
-     temp1 = 0;
-     temp = SiS_GetCH701x(SiS_Pr,0x61);
-     if(temp < 2) {
-          temp++;
-	  SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x61);
-	  temp1 = 1;
-     }
-     SiS_SetCH701x(SiS_Pr,0xac76);
-     temp = SiS_GetCH701x(SiS_Pr,0x66);
-     temp |= 0x5f;
-     SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x66);
-     if(ModeNo > 0x13) {
-         if(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-	    SiS_GenericDelay(SiS_Pr,0x3ff);
-	 } else {
-	    SiS_GenericDelay(SiS_Pr,0x2ff);
-	 }
-     } else {
-         if(!temp1)
-	    SiS_GenericDelay(SiS_Pr,0x2ff);
+
+     if(HwDeviceExtension->jChipType == SIS_740) {
+
+        temp = SiS_GetCH701x(SiS_Pr,0x61);
+        if(temp < 1) {
+           temp++;
+	   SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x61);
+        }
+        SiS_SetCH701x(SiS_Pr,0x4566);
+        SiS_SetCH701x(SiS_Pr,0xaf76);
+        SiS_LongDelay(SiS_Pr,1);
+        SiS_GenericDelay(SiS_Pr,0x16ff);
+
+     } else {  /* 650 */
+
+        temp1 = 0;
+        temp = SiS_GetCH701x(SiS_Pr,0x61);
+        if(temp < 2) {
+           temp++;
+	   SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x61);
+	   temp1 = 1;
+        }
+        SiS_SetCH701x(SiS_Pr,0xac76);
+        temp = SiS_GetCH701x(SiS_Pr,0x66);
+        temp |= 0x5f;
+        SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x66);
+        if(ModeNo > 0x13) {
+           if(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr)) {
+	      SiS_GenericDelay(SiS_Pr,0x3ff);
+	   } else {
+	      SiS_GenericDelay(SiS_Pr,0x2ff);
+	   }
+        } else {
+           if(!temp1)
+	      SiS_GenericDelay(SiS_Pr,0x2ff);
+        }
+        temp = SiS_GetCH701x(SiS_Pr,0x76);
+        temp |= 0x03;
+        SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x76);
+        temp = SiS_GetCH701x(SiS_Pr,0x66);
+        temp &= 0x7f;
+        SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x66);
+        SiS_LongDelay(SiS_Pr,1);
+
      }
-     temp = SiS_GetCH701x(SiS_Pr,0x76);
-     temp |= 0x03;
-     SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x76);
-     temp = SiS_GetCH701x(SiS_Pr,0x66);
-     temp &= 0x7f;
-     SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x66);
-     SiS_LongDelay(SiS_Pr,1);
-#endif     
 }
 
 void
@@ -9479,9 +9638,9 @@
        temp &= 0x04;
        if(temp == 0x04) break;
        
-#ifdef NEWCH701x
-       SiS_SetCH701x(SiS_Pr,0xac76);    /* 740/LVDS */
-#endif       
+       if(HwDeviceExtension->jChipType == SIS_740) {
+          SiS_SetCH701x(SiS_Pr,0xac76);
+       }
 
        SiS_SetCH701xForLCD(SiS_Pr,HwDeviceExtension,BaseAddr);
 
@@ -9499,11 +9658,11 @@
        temp = SiS_GetCH701x(SiS_Pr,0x76);
        temp |= 0x04;
        SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x76);
-#ifdef NEWCH701x
-       SiS_SetCH701x(SiS_Pr,0xe078);
-#else       
-       SiS_SetCH701x(SiS_Pr,0x6078);
-#endif       
+       if(HwDeviceExtension->jChipType == SIS_740) {
+          SiS_SetCH701x(SiS_Pr,0xe078);
+       } else {
+          SiS_SetCH701x(SiS_Pr,0x6078);
+       }
        SiS_LongDelay(SiS_Pr,2);
     } while(0);
 
@@ -9521,51 +9680,52 @@
      temp &= 0xbf;	/* Set datapath 2 to LVDS */
      SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x03);
      
-#ifdef NEWCH701x   /* 740/LVDS: */
+     if(HwDeviceExtension->jChipType == SIS_740) {
+
+        temp = SiS_GetCH701x(SiS_Pr,0x1c);
+        temp &= 0xfb;
+        SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x1c);
+
+        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x2d,0x03);
+
+        temp = SiS_GetCH701x(SiS_Pr,0x64);
+        temp |= 0x40;
+        SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x64);
+
+        temp = SiS_GetCH701x(SiS_Pr,0x03);
+        temp &= 0x3f;
+        SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x03);
+
+        temp = SiS_GetCH701x(SiS_Pr,0x66);
+        if(temp != 0x45) {
+           SiS_ChrontelResetDB(SiS_Pr,HwDeviceExtension,BaseAddr);
+           SiS_ChrontelDoSomething2(SiS_Pr,HwDeviceExtension,BaseAddr);
+	   temp = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x34);
+           SiS_ChrontelDoSomething3(SiS_Pr,temp,HwDeviceExtension,BaseAddr);
+        }
+
+     } else { /* 650 */
 
-     temp = SiS_GetCH701x(SiS_Pr,0x1c);
-     temp &= 0xfb;
-     SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x1c);
-     
-     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x2d,0x03);
-     
-     temp = SiS_GetCH701x(SiS_Pr,0x64);
-     temp |= 0x40;
-     SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x64);
-     
-     temp = SiS_GetCH701x(SiS_Pr,0x03);
-     temp &= 0x3f;	
-     SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x03);
-     
-     temp = SiS_GetCH701x(SiS_Pr,0x66);
-     if(temp != 0x45) {
         SiS_ChrontelResetDB(SiS_Pr,HwDeviceExtension,BaseAddr);
-        SiS_ChrontelDoSomething2(SiS_Pr,HwDeviceExtension,BaseAddr);
-	temp = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x34);
-        SiS_ChrontelDoSomething3(SiS_Pr,temp,HwDeviceExtension,BaseAddr);
-     }     
 
-#else  /* pre-740/LVDS: */     
+        SiS_ChrontelDoSomething2(SiS_Pr,HwDeviceExtension,BaseAddr);
 
-     SiS_ChrontelResetDB(SiS_Pr);
+        temp = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x34);
+        SiS_ChrontelDoSomething3(SiS_Pr,temp,HwDeviceExtension,BaseAddr);
 
-     SiS_ChrontelDoSomething2(SiS_Pr,HwDeviceExtension,BaseAddr);
+        SiS_SetCH701x(SiS_Pr,0xaf76);
 
-     temp = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x34);
-     SiS_ChrontelDoSomething3(SiS_Pr,temp,HwDeviceExtension,BaseAddr);
+     }
 
-     SiS_SetCH701x(SiS_Pr,0xaf76);
-     
-#endif  /* End of pre-740/LVDS */
 }
 
-#endif  /* 310/325 series --------------------------------- */
+#endif  /* 315 series ------------------------------------ */
 
-/* TW: End of Chrontel 701x functions ==================================== */
+/* End of Chrontel 701x functions ==================================== */
 
-/* TW: Generic Read/write routines for Chrontel ========================== */
+/* Generic Read/write routines for Chrontel ========================== */
 
-/* TW: The Chrontel is connected to the 630/730 via
+/* The Chrontel is connected to the 630/730 via
  * the 630/730's DDC/I2C port.
  *
  * On 630(S)T chipset, the index changed from 0x11 to 0x0a,
@@ -9575,13 +9735,13 @@
 void
 SiS_SetCH70xx(SiS_Private *SiS_Pr, USHORT tempbx)
 {
-   if (SiS_Pr->SiS_IF_DEF_CH70xx == 1)
+   if(SiS_Pr->SiS_IF_DEF_CH70xx == 1)
       SiS_SetCH700x(SiS_Pr,tempbx);
    else
       SiS_SetCH701x(SiS_Pr,tempbx);
 }
 
-/* TW: Write to Chrontel 700x */
+/* Write to Chrontel 700x */
 /* Parameter is [Data (S15-S8) | Register no (S7-S0)] */
 void
 SiS_SetCH700x(SiS_Private *SiS_Pr, USHORT tempbx)
@@ -9589,100 +9749,100 @@
   USHORT tempah,temp,i;
 
   if(!(SiS_Pr->SiS_ChrontelInit)) {
-     SiS_Pr->SiS_DDC_Index = 0x11;		   /* TW: Bit 0 = SC;  Bit 1 = SD */
+     SiS_Pr->SiS_DDC_Index = 0x11;		   /* Bit 0 = SC;  Bit 1 = SD */
      SiS_Pr->SiS_DDC_Data  = 0x02;                 /* Bitmask in IndexReg for Data */
      SiS_Pr->SiS_DDC_Clk   = 0x01;                 /* Bitmask in IndexReg for Clk */
      SiS_Pr->SiS_DDC_DataShift = 0x00;
-     SiS_Pr->SiS_DDC_DeviceAddr = 0xEA;  	   /* TW: DAB (Device Address Byte) */
+     SiS_Pr->SiS_DDC_DeviceAddr = 0xEA;  	   /* DAB (Device Address Byte) */
   }
 
-  for(i=0;i<10;i++) {	/* TW: Do only 10 attempts to write */
+  for(i=0;i<10;i++) {	/* Do only 10 attempts to write */
     /* SiS_SetSwitchDDC2(SiS_Pr); */
-    if(SiS_SetStart(SiS_Pr)) continue;		/* TW: Set start condition */
+    if(SiS_SetStart(SiS_Pr)) continue;		/* Set start condition */
     tempah = SiS_Pr->SiS_DDC_DeviceAddr;
-    temp = SiS_WriteDDC2Data(SiS_Pr,tempah);	/* TW: Write DAB (S0=0=write) */
-    if(temp) continue;				/* TW:    (ERROR: no ack) */
-    tempah = tempbx & 0x00FF;			/* TW: Write RAB */
-    tempah |= 0x80;                             /* TW: (set bit 7, see datasheet) */
+    temp = SiS_WriteDDC2Data(SiS_Pr,tempah);	/* Write DAB (S0=0=write) */
+    if(temp) continue;				/*    (ERROR: no ack) */
+    tempah = tempbx & 0x00FF;			/* Write RAB */
+    tempah |= 0x80;                             /* (set bit 7, see datasheet) */
     temp = SiS_WriteDDC2Data(SiS_Pr,tempah);
-    if(temp) continue;				/* TW:    (ERROR: no ack) */
+    if(temp) continue;				/*    (ERROR: no ack) */
     tempah = (tempbx & 0xFF00) >> 8;
-    temp = SiS_WriteDDC2Data(SiS_Pr,tempah);	/* TW: Write data */
-    if(temp) continue;				/* TW:    (ERROR: no ack) */
-    if(SiS_SetStop(SiS_Pr)) continue;		/* TW: Set stop condition */
+    temp = SiS_WriteDDC2Data(SiS_Pr,tempah);	/* Write data */
+    if(temp) continue;				/*    (ERROR: no ack) */
+    if(SiS_SetStop(SiS_Pr)) continue;		/* Set stop condition */
     SiS_Pr->SiS_ChrontelInit = 1;
     return;
   }
 
-  /* TW: For 630ST */
+  /* For 630ST */
   if(!(SiS_Pr->SiS_ChrontelInit)) {
-     SiS_Pr->SiS_DDC_Index = 0x0a;		/* TW: Bit 7 = SC;  Bit 6 = SD */
+     SiS_Pr->SiS_DDC_Index = 0x0a;		/* Bit 7 = SC;  Bit 6 = SD */
      SiS_Pr->SiS_DDC_Data  = 0x80;              /* Bitmask in IndexReg for Data */
      SiS_Pr->SiS_DDC_Clk   = 0x40;              /* Bitmask in IndexReg for Clk */
      SiS_Pr->SiS_DDC_DataShift = 0x00;
-     SiS_Pr->SiS_DDC_DeviceAddr = 0xEA;  	/* TW: DAB (Device Address Byte) */
+     SiS_Pr->SiS_DDC_DeviceAddr = 0xEA;  	/* DAB (Device Address Byte) */
 
-     for(i=0;i<10;i++) {	/* TW: Do only 10 attempts to write */
+     for(i=0;i<10;i++) {	/* Do only 10 attempts to write */
        /* SiS_SetSwitchDDC2(SiS_Pr); */
-       if (SiS_SetStart(SiS_Pr)) continue;	/* TW: Set start condition */
+       if (SiS_SetStart(SiS_Pr)) continue;	/* Set start condition */
        tempah = SiS_Pr->SiS_DDC_DeviceAddr;
-       temp = SiS_WriteDDC2Data(SiS_Pr,tempah);	/* TW: Write DAB (S0=0=write) */
-       if(temp) continue;			/* TW:    (ERROR: no ack) */
-       tempah = tempbx & 0x00FF;		/* TW: Write RAB */
-       tempah |= 0x80;                          /* TW: (set bit 7, see datasheet) */
+       temp = SiS_WriteDDC2Data(SiS_Pr,tempah);	/* Write DAB (S0=0=write) */
+       if(temp) continue;			/*    (ERROR: no ack) */
+       tempah = tempbx & 0x00FF;		/* Write RAB */
+       tempah |= 0x80;                          /* (set bit 7, see datasheet) */
        temp = SiS_WriteDDC2Data(SiS_Pr,tempah);
-       if(temp) continue;			/* TW:    (ERROR: no ack) */
+       if(temp) continue;			/*    (ERROR: no ack) */
        tempah = (tempbx & 0xFF00) >> 8;
-       temp = SiS_WriteDDC2Data(SiS_Pr,tempah);	/* TW: Write data */
-       if(temp) continue;			/* TW:    (ERROR: no ack) */
-       if(SiS_SetStop(SiS_Pr)) continue;	/* TW: Set stop condition */
+       temp = SiS_WriteDDC2Data(SiS_Pr,tempah);	/* Write data */
+       if(temp) continue;			/*    (ERROR: no ack) */
+       if(SiS_SetStop(SiS_Pr)) continue;	/* Set stop condition */
        SiS_Pr->SiS_ChrontelInit = 1;
        return;
     }
   }
 }
 
-/* TW: Write to Chrontel 701x */
+/* Write to Chrontel 701x */
 /* Parameter is [Data (S15-S8) | Register no (S7-S0)] */
 void
 SiS_SetCH701x(SiS_Private *SiS_Pr, USHORT tempbx)
 {
   USHORT tempah,temp,i;
 
-  SiS_Pr->SiS_DDC_Index = 0x11;			/* TW: Bit 0 = SC;  Bit 1 = SD */
+  SiS_Pr->SiS_DDC_Index = 0x11;			/* Bit 0 = SC;  Bit 1 = SD */
   SiS_Pr->SiS_DDC_Data  = 0x08;                 /* Bitmask in IndexReg for Data */
   SiS_Pr->SiS_DDC_Clk   = 0x04;                 /* Bitmask in IndexReg for Clk */
   SiS_Pr->SiS_DDC_DataShift = 0x00;
-  SiS_Pr->SiS_DDC_DeviceAddr = 0xEA;  		/* TW: DAB (Device Address Byte) */
+  SiS_Pr->SiS_DDC_DeviceAddr = 0xEA;  		/* DAB (Device Address Byte) */
 
-  for(i=0;i<10;i++) {	/* TW: Do only 10 attempts to write */
-    if (SiS_SetStart(SiS_Pr)) continue;		/* TW: Set start condition */
+  for(i=0;i<10;i++) {	/* Do only 10 attempts to write */
+    if (SiS_SetStart(SiS_Pr)) continue;		/* Set start condition */
     tempah = SiS_Pr->SiS_DDC_DeviceAddr;
-    temp = SiS_WriteDDC2Data(SiS_Pr,tempah);	/* TW: Write DAB (S0=0=write) */
-    if(temp) continue;				/* TW:    (ERROR: no ack) */
+    temp = SiS_WriteDDC2Data(SiS_Pr,tempah);	/* Write DAB (S0=0=write) */
+    if(temp) continue;				/*    (ERROR: no ack) */
     tempah = tempbx & 0x00FF;
-    temp = SiS_WriteDDC2Data(SiS_Pr,tempah);	/* TW: Write RAB */
-    if(temp) continue;				/* TW:    (ERROR: no ack) */
+    temp = SiS_WriteDDC2Data(SiS_Pr,tempah);	/* Write RAB */
+    if(temp) continue;				/*    (ERROR: no ack) */
     tempah = (tempbx & 0xFF00) >> 8;
-    temp = SiS_WriteDDC2Data(SiS_Pr,tempah);	/* TW: Write data */
-    if(temp) continue;				/* TW:    (ERROR: no ack) */
-    if(SiS_SetStop(SiS_Pr)) continue;		/* TW: Set stop condition */
+    temp = SiS_WriteDDC2Data(SiS_Pr,tempah);	/* Write data */
+    if(temp) continue;				/*    (ERROR: no ack) */
+    if(SiS_SetStop(SiS_Pr)) continue;		/* Set stop condition */
     return;
   }
 }
 
-/* TW: Read from Chrontel 70xx */
+/* Read from Chrontel 70xx */
 /* Parameter is [Register no (S7-S0)] */
 USHORT
 SiS_GetCH70xx(SiS_Private *SiS_Pr, USHORT tempbx)
 {
-   if (SiS_Pr->SiS_IF_DEF_CH70xx == 1)
+   if(SiS_Pr->SiS_IF_DEF_CH70xx == 1)
       return(SiS_GetCH700x(SiS_Pr,tempbx));
    else
       return(SiS_GetCH701x(SiS_Pr,tempbx));
 }
 
-/* TW: Read from Chrontel 700x */
+/* Read from Chrontel 700x */
 /* Parameter is [Register no (S7-S0)] */
 USHORT
 SiS_GetCH700x(SiS_Private *SiS_Pr, USHORT tempbx)
@@ -9690,57 +9850,57 @@
   USHORT tempah,temp,i;
 
   if(!(SiS_Pr->SiS_ChrontelInit)) {
-     SiS_Pr->SiS_DDC_Index = 0x11;		/* TW: Bit 0 = SC;  Bit 1 = SD */
+     SiS_Pr->SiS_DDC_Index = 0x11;		/* Bit 0 = SC;  Bit 1 = SD */
      SiS_Pr->SiS_DDC_Data  = 0x02;              /* Bitmask in IndexReg for Data */
      SiS_Pr->SiS_DDC_Clk   = 0x01;              /* Bitmask in IndexReg for Clk */
      SiS_Pr->SiS_DDC_DataShift = 0x00;
-     SiS_Pr->SiS_DDC_DeviceAddr = 0xEA;		/* TW: DAB */
+     SiS_Pr->SiS_DDC_DeviceAddr = 0xEA;		/* DAB */
   }
 
   SiS_Pr->SiS_DDC_ReadAddr = tempbx;
 
-  for(i=0;i<20;i++) {	/* TW: Do only 20 attempts to read */
+  for(i=0;i<20;i++) {	/* Do only 20 attempts to read */
     /* SiS_SetSwitchDDC2(SiS_Pr); */
-    if(SiS_SetStart(SiS_Pr)) continue;		/* TW: Set start condition */
+    if(SiS_SetStart(SiS_Pr)) continue;		/* Set start condition */
     tempah = SiS_Pr->SiS_DDC_DeviceAddr;
-    temp = SiS_WriteDDC2Data(SiS_Pr,tempah);	/* TW: Write DAB (S0=0=write) */
-    if(temp) continue;				/* TW:        (ERROR: no ack) */
-    tempah = SiS_Pr->SiS_DDC_ReadAddr | 0x80;	/* TW: Write RAB | 0x80 */
+    temp = SiS_WriteDDC2Data(SiS_Pr,tempah);	/* Write DAB (S0=0=write) */
+    if(temp) continue;				/*        (ERROR: no ack) */
+    tempah = SiS_Pr->SiS_DDC_ReadAddr | 0x80;	/* Write RAB | 0x80 */
     temp = SiS_WriteDDC2Data(SiS_Pr,tempah);
-    if(temp) continue;				/* TW:        (ERROR: no ack) */
-    if (SiS_SetStart(SiS_Pr)) continue;		/* TW: Re-start */
+    if(temp) continue;				/*        (ERROR: no ack) */
+    if (SiS_SetStart(SiS_Pr)) continue;		/* Re-start */
     tempah = SiS_Pr->SiS_DDC_DeviceAddr | 0x01; /* DAB | 0x01 = Read */
-    temp = SiS_WriteDDC2Data(SiS_Pr,tempah);	/* TW: DAB (S0=1=read) */
-    if(temp) continue;				/* TW:        (ERROR: no ack) */
-    tempah = SiS_ReadDDC2Data(SiS_Pr,tempah);	/* TW: Read byte */
-    if (SiS_SetStop(SiS_Pr)) continue;		/* TW: Stop condition */
+    temp = SiS_WriteDDC2Data(SiS_Pr,tempah);	/* DAB (S0=1=read) */
+    if(temp) continue;				/*        (ERROR: no ack) */
+    tempah = SiS_ReadDDC2Data(SiS_Pr,tempah);	/* Read byte */
+    if (SiS_SetStop(SiS_Pr)) continue;		/* Stop condition */
     SiS_Pr->SiS_ChrontelInit = 1;
     return(tempah);
   }
 
-  /* TW: For 630ST */
+  /* For 630ST */
   if(!SiS_Pr->SiS_ChrontelInit) {
-     SiS_Pr->SiS_DDC_Index = 0x0a;		/* TW: Bit 0 = SC;  Bit 1 = SD */
+     SiS_Pr->SiS_DDC_Index = 0x0a;		/* Bit 0 = SC;  Bit 1 = SD */
      SiS_Pr->SiS_DDC_Data  = 0x80;              /* Bitmask in IndexReg for Data */
      SiS_Pr->SiS_DDC_Clk   = 0x40;              /* Bitmask in IndexReg for Clk */
      SiS_Pr->SiS_DDC_DataShift = 0x00;
-     SiS_Pr->SiS_DDC_DeviceAddr = 0xEA;  	/* TW: DAB (Device Address Byte) */
+     SiS_Pr->SiS_DDC_DeviceAddr = 0xEA;  	/* DAB (Device Address Byte) */
 
-     for(i=0;i<20;i++) {	/* TW: Do only 20 attempts to read */
+     for(i=0;i<20;i++) {	/* Do only 20 attempts to read */
        /* SiS_SetSwitchDDC2(SiS_Pr); */
-       if(SiS_SetStart(SiS_Pr)) continue;		/* TW: Set start condition */
+       if(SiS_SetStart(SiS_Pr)) continue;		/* Set start condition */
        tempah = SiS_Pr->SiS_DDC_DeviceAddr;
-       temp = SiS_WriteDDC2Data(SiS_Pr,tempah);		/* TW: Write DAB (S0=0=write) */
-       if(temp) continue;				/* TW:        (ERROR: no ack) */
-       tempah = SiS_Pr->SiS_DDC_ReadAddr | 0x80;	/* TW: Write RAB | 0x80 */
+       temp = SiS_WriteDDC2Data(SiS_Pr,tempah);		/* Write DAB (S0=0=write) */
+       if(temp) continue;				/*        (ERROR: no ack) */
+       tempah = SiS_Pr->SiS_DDC_ReadAddr | 0x80;	/* Write RAB | 0x80 */
        temp = SiS_WriteDDC2Data(SiS_Pr,tempah);
-       if(temp) continue;				/* TW:        (ERROR: no ack) */
-       if (SiS_SetStart(SiS_Pr)) continue;		/* TW: Re-start */
+       if(temp) continue;				/*        (ERROR: no ack) */
+       if (SiS_SetStart(SiS_Pr)) continue;		/* Re-start */
        tempah = SiS_Pr->SiS_DDC_DeviceAddr | 0x01; 	/* DAB | 0x01 = Read */
-       temp = SiS_WriteDDC2Data(SiS_Pr,tempah);		/* TW: DAB (S0=1=read) */
-       if(temp) continue;				/* TW:        (ERROR: no ack) */
-       tempah = SiS_ReadDDC2Data(SiS_Pr,tempah);	/* TW: Read byte */
-       if (SiS_SetStop(SiS_Pr)) continue;		/* TW: Stop condition */
+       temp = SiS_WriteDDC2Data(SiS_Pr,tempah);		/* DAB (S0=1=read) */
+       if(temp) continue;				/*        (ERROR: no ack) */
+       tempah = SiS_ReadDDC2Data(SiS_Pr,tempah);	/* Read byte */
+       if (SiS_SetStop(SiS_Pr)) continue;		/* Stop condition */
        SiS_Pr->SiS_ChrontelInit = 1;
        return(tempah);
      }
@@ -9748,52 +9908,51 @@
   return(0xFFFF);
 }
 
-/* TW: Read from Chrontel 701x */
+/* Read from Chrontel 701x */
 /* Parameter is [Register no (S7-S0)] */
 USHORT
 SiS_GetCH701x(SiS_Private *SiS_Pr, USHORT tempbx)
 {
   USHORT tempah,temp,i;
 
-  SiS_Pr->SiS_DDC_Index = 0x11;			/* TW: Bit 0 = SC;  Bit 1 = SD */
+  SiS_Pr->SiS_DDC_Index = 0x11;			/* Bit 0 = SC;  Bit 1 = SD */
   SiS_Pr->SiS_DDC_Data  = 0x08;                 /* Bitmask in IndexReg for Data */
   SiS_Pr->SiS_DDC_Clk   = 0x04;                 /* Bitmask in IndexReg for Clk */
   SiS_Pr->SiS_DDC_DataShift = 0x00;
-  SiS_Pr->SiS_DDC_DeviceAddr = 0xEA;		/* TW: DAB */
+  SiS_Pr->SiS_DDC_DeviceAddr = 0xEA;		/* DAB */
   SiS_Pr->SiS_DDC_ReadAddr = tempbx;
 
-   for(i=0;i<20;i++) {	/* TW: Do only 20 attempts to read */
-    if(SiS_SetStart(SiS_Pr)) continue;		/* TW: Set start condition */
+   for(i=0;i<20;i++) {	/* Do only 20 attempts to read */
+    if(SiS_SetStart(SiS_Pr)) continue;		/* Set start condition */
     tempah = SiS_Pr->SiS_DDC_DeviceAddr;
-    temp = SiS_WriteDDC2Data(SiS_Pr,tempah);	/* TW: Write DAB (S0=0=write) */
-    if(temp) continue;				/* TW:        (ERROR: no ack) */
-    tempah = SiS_Pr->SiS_DDC_ReadAddr;		/* TW: Write RAB */
+    temp = SiS_WriteDDC2Data(SiS_Pr,tempah);	/* Write DAB (S0=0=write) */
+    if(temp) continue;				/*        (ERROR: no ack) */
+    tempah = SiS_Pr->SiS_DDC_ReadAddr;		/* Write RAB */
     temp = SiS_WriteDDC2Data(SiS_Pr,tempah);
-    if(temp) continue;				/* TW:        (ERROR: no ack) */
-    if (SiS_SetStart(SiS_Pr)) continue;		/* TW: Re-start */
+    if(temp) continue;				/*        (ERROR: no ack) */
+    if (SiS_SetStart(SiS_Pr)) continue;		/* Re-start */
     tempah = SiS_Pr->SiS_DDC_DeviceAddr | 0x01; /* DAB | 0x01 = Read */
-    temp = SiS_WriteDDC2Data(SiS_Pr,tempah);	/* TW: DAB (S0=1=read) */
-    if(temp) continue;				/* TW:        (ERROR: no ack) */
-    tempah = SiS_ReadDDC2Data(SiS_Pr,tempah);	/* TW: Read byte */
-    SiS_SetStop(SiS_Pr);			/* TW: Stop condition */
+    temp = SiS_WriteDDC2Data(SiS_Pr,tempah);	/* DAB (S0=1=read) */
+    if(temp) continue;				/*        (ERROR: no ack) */
+    tempah = SiS_ReadDDC2Data(SiS_Pr,tempah);	/* Read byte */
+    SiS_SetStop(SiS_Pr);			/* Stop condition */
     return(tempah);
    }
   return 0xFFFF;
 }
 
-#ifdef LINUX_XF86
-/* TW: Our own DDC functions */
+/* Our own DDC functions */
 USHORT
-SiS_InitDDCRegs(SiS_Private *SiS_Pr, SISPtr pSiS, USHORT adaptnum, USHORT DDCdatatype,
-		BOOLEAN checkcr32)
+SiS_InitDDCRegs(SiS_Private *SiS_Pr, unsigned long VBFlags, int VGAEngine,
+                USHORT adaptnum, USHORT DDCdatatype, BOOLEAN checkcr32)
 {
      unsigned char ddcdtype[] = { 0xa0, 0xa0, 0xa0, 0xa2, 0xa6 };
      unsigned char flag, cr32;
      USHORT        temp = 0, myadaptnum = adaptnum;
 
      if(adaptnum != 0) {
-        if(!(pSiS->VBFlags & (VB_301|VB_301B|VB_302B))) return 0xFFFF;
-	if((pSiS->VBFlags & VB_30xBDH) && (adaptnum == 1)) return 0xFFFF;
+        if(!(VBFlags & (VB_301|VB_301B|VB_302B))) return 0xFFFF;
+	if((VBFlags & VB_30xBDH) && (adaptnum == 1)) return 0xFFFF;
      }	
      
      /* adapternum for SiS bridges: 0 = CRT1, 1 = LCD, 2 = VGA2 */
@@ -9807,8 +9966,9 @@
      flag = 0xff;
 
      cr32 = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x32);
-  
-     if(pSiS->VBFlags & VB_SISBRIDGE) {
+
+#if 0
+     if(VBFlags & VB_SISBRIDGE) {
 	if(myadaptnum == 0) {
 	   if(!(cr32 & 0x20)) {
 	      myadaptnum = 2;
@@ -9821,18 +9981,19 @@
 	   }
         }
      }
+#endif
 
-     if(pSiS->VGAEngine == SIS_300_VGA) {		/* 300 series */
+     if(VGAEngine == SIS_300_VGA) {		/* 300 series */
 	
         if(myadaptnum != 0) {
 	   flag = 0;
-	   if(pSiS->VBFlags & VB_SISBRIDGE) {
+	   if(VBFlags & VB_SISBRIDGE) {
 	      SiS_Pr->SiS_DDC_Port = SiS_Pr->SiS_Part4Port;
               SiS_Pr->SiS_DDC_Index = 0x0f;
 	   }
         }
 
-	if(!(pSiS->VBFlags & VB_301)) {
+	if(!(VBFlags & VB_301)) {
 	   if((cr32 & 0x80) && (checkcr32)) {
               if(myadaptnum >= 1) {
 	         if(!(cr32 & 0x08)) {
@@ -9846,11 +10007,11 @@
 	temp = 4 - (myadaptnum * 2);
 	if(flag) temp = 0;
 
-     } else {						/* 310/325/330 series */
+     } else {						/* 315/330 series */
 
      	/* here we simplify: 0 = CRT1, 1 = CRT2 (VGA, LCD) */
 	
-	if(pSiS->VBFlags & VB_SISBRIDGE) {
+	if(VBFlags & VB_SISBRIDGE) {
 	   if(myadaptnum == 2) {
 	      myadaptnum = 1;
            }
@@ -9858,7 +10019,7 @@
 
         if(myadaptnum == 1) {
      	   flag = 0;
-	   if(pSiS->VBFlags & VB_SISBRIDGE) {
+	   if(VBFlags & VB_SISBRIDGE) {
 	      SiS_Pr->SiS_DDC_Port = SiS_Pr->SiS_Part4Port;
               SiS_Pr->SiS_DDC_Index = 0x0f;
 	   }
@@ -9876,7 +10037,7 @@
         temp = myadaptnum;
         if(myadaptnum == 1) {
            temp = 0;
-	   if(pSiS->VBFlags & VB_LVDS) flag = 0xff;
+	   if(VBFlags & VB_LVDS) flag = 0xff;
         }
 
 	if(flag) temp = 0;
@@ -9888,7 +10049,7 @@
 #ifdef TWDEBUG
     xf86DrvMsg(0, X_INFO, "DDC Port %x Index %x Shift %d\n",
     		SiS_Pr->SiS_DDC_Port, SiS_Pr->SiS_DDC_Index, temp);
-#endif	 
+#endif
     
     return 0;
 }
@@ -9898,15 +10059,9 @@
 {
    if(SiS_SetStart(SiS_Pr)) return 0xFFFF;
    if(SiS_WriteDDC2Data(SiS_Pr, SiS_Pr->SiS_DDC_DeviceAddr)) {
-#ifdef TWDEBUG
-        xf86DrvMsg(0, X_INFO, "WriteDAB 1 failed\n");
-#endif	 
   	return 0xFFFF;
    }
    if(SiS_WriteDDC2Data(SiS_Pr, SiS_Pr->SiS_DDC_SecAddr)) {
-#ifdef TWDEBUG
-        xf86DrvMsg(0, X_INFO, "WriteDAB 2 failed\n");
-#endif	 
    	return 0xFFFF;
    }
    return(0);
@@ -9917,9 +10072,6 @@
 {
    if(SiS_SetStart(SiS_Pr)) return 0xFFFF;
    if(SiS_WriteDDC2Data(SiS_Pr, (SiS_Pr->SiS_DDC_DeviceAddr | 0x01))) {
-#ifdef TWDEBUG
-        xf86DrvMsg(0, X_INFO, "PrepareReadDDC 1 failed\n");
-#endif	 
    	return 0xFFFF;
    }
    return(0);
@@ -9957,9 +10109,6 @@
     SiS_SetSwitchDDC2(SiS_Pr);
     if(SiS_PrepareDDC(SiS_Pr)) {
          SiS_SetStop(SiS_Pr);
-#ifdef TWDEBUG
-	 xf86DrvMsg(0, X_INFO, "DoProbeDDC 1 failed at PrepareDDC\n");
-#endif	 
          return(0xFFFF);
     }
     mask = 0xf0;
@@ -10008,7 +10157,7 @@
 }
 
 USHORT
-SiS_ReadDDC(SiS_Private *SiS_Pr, SISPtr pSiS, USHORT DDCdatatype, unsigned char *buffer)
+SiS_ReadDDC(SiS_Private *SiS_Pr, USHORT DDCdatatype, unsigned char *buffer)
 {
    USHORT flag, length, i;
    unsigned char chksum,gotcha;
@@ -10040,28 +10189,7 @@
    return(flag);
 }
 
-USHORT
-SiS_ReadLCDDDC(SiS_Private *SiS_Pr, USHORT length, unsigned char *buffer)
-{
-   USHORT i=0, flag=0;
-
-   length--;
-   
-   SiS_SetSwitchDDC2(SiS_Pr);
-   if(!(SiS_PrepareDDC(SiS_Pr))) {
-      for(i=0; i<length; i++) {
-         buffer[i] = (unsigned char)SiS_ReadDDC2Data(SiS_Pr, 0);
-         SiS_SendACK(SiS_Pr, 0);
-      }
-      buffer[i] = (unsigned char)SiS_ReadDDC2Data(SiS_Pr, 0);
-      SiS_SendACK(SiS_Pr, 1);
-   } else flag = 0xFFFF;
-   
-   SiS_SetStop(SiS_Pr);
-   return(0);
-}
-
-/* TW: Our private DDC function
+/* Our private DDC functions
 
    It complies somewhat with the corresponding VESA function
    in arguments and return values.
@@ -10072,7 +10200,7 @@
 
    Arguments:
        adaptnum: 0=CRT1, 1=LCD, 2=VGA2
-                 CRT2 DDC is only supported on SiS301, 301B (non-DH version), 302B.
+                 CRT2 DDC is only supported on SiS301, 301B, 302B.
        DDCdatatype: 0=Probe, 1=EDID, 2=EDID+VDIF, 3=EDID V2 (P&D), 4=EDID V2 (FPDI-2)
        buffer: ptr to 256 data bytes which will be filled with read data.
 
@@ -10082,43 +10210,48 @@
 
  */
 USHORT
-SiS_HandleDDC(SiS_Private *SiS_Pr, SISPtr pSiS, USHORT adaptnum,
-              USHORT DDCdatatype, unsigned char *buffer)
+SiS_HandleDDC(SiS_Private *SiS_Pr, unsigned long VBFlags, int VGAEngine,
+              USHORT adaptnum, USHORT DDCdatatype, unsigned char *buffer)
 {
    if(adaptnum > 2) return 0xFFFF;
    if(DDCdatatype > 4) return 0xFFFF;
-   if((!(pSiS->VBFlags & VB_VIDEOBRIDGE)) && (adaptnum > 0)) return 0xFFFF;
-   if(SiS_InitDDCRegs(SiS_Pr, pSiS, adaptnum, DDCdatatype, TRUE) == 0xFFFF) return 0xFFFF;
+   if((!(VBFlags & VB_VIDEOBRIDGE)) && (adaptnum > 0)) return 0xFFFF;
+   if(SiS_InitDDCRegs(SiS_Pr, VBFlags, VGAEngine, adaptnum, DDCdatatype, TRUE) == 0xFFFF) return 0xFFFF;
    if(DDCdatatype == 0) {
        return(SiS_ProbeDDC(SiS_Pr));
    } else {
-       return(SiS_ReadDDC(SiS_Pr, pSiS, DDCdatatype, buffer));
+       return(SiS_ReadDDC(SiS_Pr, DDCdatatype, buffer));
    }
 }
 
+#ifdef LINUX_XF86
 /* Sense the LCD parameters (CR36, CR37) via DDC */
 /* SiS30x(B) only */
 USHORT
 SiS_SenseLCDDDC(SiS_Private *SiS_Pr, SISPtr pSiS)
 {
-   USHORT DDCdatatype, paneltype, flag, xres, yres;
+   USHORT DDCdatatype, paneltype, flag, xres=0, yres=0;
    USHORT index, myindex, lumsize, numcodes;
    unsigned char cr37=0, seekcode;
    BOOLEAN checkexpand = FALSE;
    int retry, i;
    unsigned char buffer[256];
-   
+
+   for(i=0; i<7; i++) SiS_Pr->CP_DataValid[i] = FALSE;
+   SiS_Pr->CP_HaveCustomData = FALSE;
+   SiS_Pr->CP_MaxX = SiS_Pr->CP_MaxY = SiS_Pr->CP_MaxClock = 0;
+
    if(!(pSiS->VBFlags & (VB_301|VB_301B|VB_302B))) return 0;
    if(pSiS->VBFlags & VB_30xBDH) return 0;
   
-   if(SiS_InitDDCRegs(SiS_Pr, pSiS, 1, 0, FALSE) == 0xFFFF) return 0;
+   if(SiS_InitDDCRegs(SiS_Pr, pSiS->VBFlags, pSiS->VGAEngine, 1, 0, FALSE) == 0xFFFF) return 0;
    
    SiS_Pr->SiS_DDC_SecAddr = 0x00;
    
    /* Probe supported DA's */
    flag = SiS_ProbeDDC(SiS_Pr);
 #ifdef TWDEBUG   
-   xf86DrvMsg(pSiS->pScrn->scrnIndex, X_INFO, 
+   xf86DrvMsg(pSiS->pScrn->scrnIndex, X_INFO,
    	"CRT2 DDC capabilities 0x%x\n", flag);
 #endif	
    if(flag & 0x10) {
@@ -10135,7 +10268,7 @@
    /* Read the entire EDID */
    retry = 2;
    do {
-      if(SiS_ReadDDC(SiS_Pr, pSiS, DDCdatatype, buffer)) {
+      if(SiS_ReadDDC(SiS_Pr, DDCdatatype, buffer)) {
          xf86DrvMsg(pSiS->pScrn->scrnIndex, X_INFO, 
 	 	"CRT2: DDC read failed (attempt %d), %s\n", 
 		(3-retry), (retry == 1) ? "giving up" : "retrying");
@@ -10168,7 +10301,7 @@
       }
       
       if((buffer[0x18] & 0x18) != 0x08) {
-         xf86DrvMsg(pSiS->pScrn->scrnIndex, X_PROBED, 
+         xf86DrvMsg(pSiS->pScrn->scrnIndex, X_PROBED,
 	 	"CRT2: Attached display is not of RGB but of %s type (0x%02x)\n", 
 		((buffer[0x18] & 0x18) == 0x00) ? "monochrome/greyscale" :
 		  ( ((buffer[0x18] & 0x18) == 0x10) ? "non-RGB multicolor" : 
@@ -10176,104 +10309,226 @@
 		buffer[0x18]);
 	 return 0;
       }
-      
-      /* Now analyze the first Detailed Timing Block and hope
-       * that the preferred timing mode is stored there.
-       */	
-      xres = buffer[0x38] | ((buffer[0x3a] & 0xf0) << 4);
-      yres = buffer[0x3b] | ((buffer[0x3d] & 0xf0) << 4);
+
+      /* Now analyze the first Detailed Timing Block and see
+       * if the preferred timing mode is stored there. If so,
+       * check if this is a standard panel for which we already
+       * know the timing.
+       */
+
+      paneltype = Panel_Custom;
       checkexpand = FALSE;
-      switch(xres) {
-         case 800:
-	     if(yres == 600) {
-	     	paneltype = Panel310_800x600;
-	     	checkexpand = TRUE;
-	     }
-	     break;
-         case 1024:
-	     if(yres == 768) {
-	     	paneltype = Panel310_1024x768;
-	     	checkexpand = FALSE;    /* expand causes error at 640x480, should otherwise be TRUE; */
-	     }
-	     break;
-	 case 1280:
-	     if(yres == 960) {
-	        if(pSiS->VGAEngine == SIS_300_VGA) {
-		   paneltype = Panel300_1280x960;
-		} else {
-		   paneltype = Panel310_1280x960; 
-		}
-	     } else if(yres == 1024) {
-	     	paneltype = Panel310_1280x1024;  
-		checkexpand = TRUE;
-	     } else if(pSiS->VGAEngine == SIS_315_VGA) {
+
+      if(buffer[0x18] & 0x02) {
+
+         xres = buffer[0x38] | ((buffer[0x3a] & 0xf0) << 4);
+         yres = buffer[0x3b] | ((buffer[0x3d] & 0xf0) << 4);
+
+	 SiS_Pr->CP_PreferredX = xres;
+	 SiS_Pr->CP_PreferredY = yres;
+
+         switch(xres) {
+            case 800:
+	        if(yres == 600) {
+	     	   paneltype = Panel_800x600;
+	     	   checkexpand = TRUE;
+	        }
+	        break;
+            case 1024:
 	        if(yres == 768) {
-	       	   paneltype = Panel310_1280x768;
+	     	   paneltype = Panel_1024x768;
+	     	   checkexpand = TRUE;
+	        }
+	        break;
+	    case 1280:
+	        if(yres == 1024) {
+	     	   paneltype = Panel_1280x1024;
+		   checkexpand = TRUE;
+	        } else if(yres == 960) {
+	           if(pSiS->VGAEngine == SIS_300_VGA) {
+		      paneltype = Panel300_1280x960;
+		   } else {
+		      paneltype = Panel310_1280x960;
+		   }
+	        } else if(yres == 768) {
+	       	   paneltype = Panel_1280x768;
 		   checkexpand = FALSE;
 		   cr37 |= 0x10;
 	        }
-	     }
-	     break;
-	 case 1400:
-	     if(pSiS->VGAEngine == SIS_315_VGA) {
-	        if(yres == 1050) {
-	           paneltype = Panel310_1400x1050;
-		   checkexpand = TRUE; 
-	        } 
-	     }
-      	     break;
-	 case 1600:
-	     if(pSiS->VGAEngine == SIS_315_VGA) {
-	        if(yres == 1200) {
-	           paneltype = Panel310_1600x1200;
-		   checkexpand = TRUE;
-	        } 
-	     }
-      	     break;
+	        break;
+	    case 1400:
+	        if(pSiS->VGAEngine == SIS_315_VGA) {
+	           if(yres == 1050) {
+	              paneltype = Panel310_1400x1050;
+		      checkexpand = TRUE;
+	           }
+	        }
+      	        break;
+#if 0	    /* Treat this as custom, as we have no valid timing data yet */
+	    case 1600:
+	        if(pSiS->VGAEngine == SIS_315_VGA) {
+	           if(yres == 1200) {
+	              paneltype = Panel310_1600x1200;
+		      checkexpand = TRUE;
+	           }
+	        }
+      	        break;
+#endif
+         }
+
+	 if(paneltype != Panel_Custom) {
+	    if((buffer[0x47] & 0x18) == 0x18) {
+	       cr37 |= ((((buffer[0x47] & 0x06) ^ 0x06) << 5) | 0x20);
+	    } else {
+	       /* What now? There is no digital separate output timing... */
+	       xf86DrvMsg(pSiS->pScrn->scrnIndex, X_WARNING,
+	       	   "CRT2: Unable to retrieve Sync polarity information\n");
+	    }
+	 }
+
       }
 
-      if(buffer[0x18] & 0x02) {
-         /* If the preferred timing mode is stored in the first
-	  * detailed timing block, we now can extract the sync
-	  * polarisation information as well. This only works
-	  * if the Flags indicate a digital separate output.
-	  */
-	  if((buffer[0x47] & 0x18) == 0x18) {
-	     cr37 |= ((((buffer[0x47] & 0x06) ^ 0x06) << 5) | 0x20);
-	  } else {
-	     /* What now? There is no digital separate output timing... */
-	     xf86DrvMsg(pSiS->pScrn->scrnIndex, X_WARNING, 
-	     	"CRT2: Unable to retrieve Sync polarity information\n");
-	  }
-	  
-      } else {
-         /* If the preferred timing mode is *not* stored in the first
-	  * detailed timing block, we need to guess the resolution
-	  * from the supported Established Timings and assume the
-	  * default sync polarity
-	  */
+      /* If we still don't know what panel this is, we take it
+       * as a custom panel and derive the timing data from the
+       * detailed timing blocks
+       */
+      if(paneltype == Panel_Custom) {
+
+         BOOLEAN havesync = FALSE;
+	 int i, temp, base = 0x36;
+	 unsigned long estpack;
+	 unsigned short estx[] = {
+	 	720, 720, 640, 640, 640, 640, 800, 800,
+		800, 800, 832,1024,1024,1024,1024,1280,
+		1152
+	 };
+	 unsigned short esty[] = {
+	 	400, 400, 480, 480, 480, 480, 600, 600,
+		600, 600, 624, 768, 768, 768, 768,1024,
+		870
+	 };
+
 	 paneltype = 0;
-	 if(buffer[0x24] & 0x01) { 	
-	 	paneltype = Panel310_1280x1024;
-		checkexpand = TRUE;
-		cr37 |= 0x20;
-	 } else if(buffer[0x24] & 0x0e) {
-	 	paneltype = Panel310_1024x768;
-		cr37 |= 0xe0;
-		checkexpand = FALSE;		/* Bug at 640x480 */
-	 } else if(buffer[0x23] & 0x01) {
-	 	paneltype = Panel310_800x600;
-		cr37 |= 0xe0;
-		checkexpand = TRUE;
-         }
+
+	 /* Find the maximum resolution */
+
+	 /* 1. From Established timings */
+	 estpack = (buffer[0x23] << 9) | (buffer[0x24] << 1) | ((buffer[0x25] >> 7) & 0x01);
+	 for(i=16; i>=0; i--) {
+	     if(estpack & (1 << i)) {
+	        if(estx[16 - i] > SiS_Pr->CP_MaxX) SiS_Pr->CP_MaxX = estx[16 - i];
+		if(esty[16 - i] > SiS_Pr->CP_MaxY) SiS_Pr->CP_MaxY = esty[16 - i];
+	     }
+	 }
+
+	 /* 2. From Standard Timings */
+	 for(i=0x26; i < 0x36; i+=2) {
+	    if((buffer[i] != 0x01) && (buffer[i+1] != 0x01)) {
+	       temp = (buffer[i] + 31) * 8;
+	       if(temp > SiS_Pr->CP_MaxX) SiS_Pr->CP_MaxX = temp;
+	       switch((buffer[i+1] & 0xc0) >> 6) {
+	       case 0x03: temp = temp * 9 / 16; break;
+	       case 0x02: temp = temp * 4 / 5;  break;
+	       case 0x01: temp = temp * 3 / 4;  break;
+	       }
+	       if(temp > SiS_Pr->CP_MaxY) SiS_Pr->CP_MaxY = temp;
+	    }
+	 }
+
+	 /* Now extract the Detailed Timings and convert them into modes */
+
+         for(i = 0; i < 4; i++, base += 18) {
+
+	    /* Is this a detailed timing block or a monitor descriptor? */
+	    if(buffer[base] || buffer[base+1] || buffer[base+2]) {
+
+      	       xres = buffer[base+2] | ((buffer[base+4] & 0xf0) << 4);
+               yres = buffer[base+5] | ((buffer[base+7] & 0xf0) << 4);
+
+	       SiS_Pr->CP_HDisplay[i] = xres;
+	       SiS_Pr->CP_HSyncStart[i] = xres + (buffer[base+8] | ((buffer[base+11] & 0xc0) << 2));
+               SiS_Pr->CP_HSyncEnd[i]   = SiS_Pr->CP_HSyncStart[i] + (buffer[base+9] | ((buffer[base+11] & 0x30) << 4));
+	       SiS_Pr->CP_HTotal[i] = xres + (buffer[base+3] | ((buffer[base+4] & 0x0f) << 8));
+	       SiS_Pr->CP_HBlankStart[i] = xres + 1;
+	       SiS_Pr->CP_HBlankEnd[i] = SiS_Pr->CP_HTotal[i];
+
+	       SiS_Pr->CP_VDisplay[i] = yres;
+               SiS_Pr->CP_VSyncStart[i] = yres + (((buffer[base+10] & 0xf0) >> 4) | ((buffer[base+11] & 0x0c) << 2));
+               SiS_Pr->CP_VSyncEnd[i] = SiS_Pr->CP_VSyncStart[i] + ((buffer[base+10] & 0x0f) | ((buffer[base+11] & 0x03) << 4));
+	       SiS_Pr->CP_VTotal[i] = yres + (buffer[base+6] | ((buffer[base+7] & 0x0f) << 8));
+	       SiS_Pr->CP_VBlankStart[i] = yres + 1;
+	       SiS_Pr->CP_VBlankEnd[i] = SiS_Pr->CP_VTotal[i];
+
+	       SiS_Pr->CP_Clock[i] = (buffer[base] | (buffer[base+1] << 8)) * 10;
+
+	       SiS_Pr->CP_DataValid[i] = TRUE;
+
+	       /* Sort out invalid timings, interlace and too high clocks */
+	       if((SiS_Pr->CP_HDisplay[i] > SiS_Pr->CP_HSyncStart[i])  ||
+	          (SiS_Pr->CP_HDisplay[i] >= SiS_Pr->CP_HSyncEnd[i])   ||
+	          (SiS_Pr->CP_HDisplay[i] >= SiS_Pr->CP_HTotal[i])     ||
+	          (SiS_Pr->CP_HSyncStart[i] >= SiS_Pr->CP_HSyncEnd[i]) ||
+	          (SiS_Pr->CP_HSyncStart[i] > SiS_Pr->CP_HTotal[i])    ||
+	          (SiS_Pr->CP_HSyncEnd[i] > SiS_Pr->CP_HTotal[i])      ||
+	          (SiS_Pr->CP_VDisplay[i] > SiS_Pr->CP_VSyncStart[i])  ||
+	          (SiS_Pr->CP_VDisplay[i] >= SiS_Pr->CP_VSyncEnd[i])   ||
+	          (SiS_Pr->CP_VDisplay[i] >= SiS_Pr->CP_VTotal[i])     ||
+	          (SiS_Pr->CP_VSyncStart[i] > SiS_Pr->CP_VSyncEnd[i])  ||
+	          (SiS_Pr->CP_VSyncStart[i] > SiS_Pr->CP_VTotal[i])    ||
+	          (SiS_Pr->CP_VSyncEnd[i] > SiS_Pr->CP_VTotal[i])      ||
+	          (SiS_Pr->CP_Clock[i] > 108000)                       ||
+		  (buffer[base+17] & 0x80)) {
+
+	          SiS_Pr->CP_DataValid[i] = FALSE;
+
+	       } else {
+
+	          paneltype = Panel_Custom;
+
+		  SiS_Pr->CP_HaveCustomData = TRUE;
+
+		  if(xres > SiS_Pr->CP_MaxX) SiS_Pr->CP_MaxX = xres;
+	          if(yres > SiS_Pr->CP_MaxY) SiS_Pr->CP_MaxY = yres;
+		  if(SiS_Pr->CP_Clock[i] > SiS_Pr->CP_MaxClock) SiS_Pr->CP_MaxClock = SiS_Pr->CP_Clock[i];
+
+		  SiS_Pr->CP_Vendor = buffer[9] | (buffer[8] << 8);
+		  SiS_Pr->CP_Product = buffer[10] | (buffer[11] << 8);
+
+	          /* We must assume the panel can scale, since we have
+	           * no scaling data
+		   */
+	          checkexpand = FALSE;
+	          cr37 |= 0x10;
+
+	          /* Extract the sync polarisation information. This only works
+	           * if the Flags indicate a digital separate output.
+	           */
+	          if((buffer[base+17] & 0x18) == 0x18) {
+		     SiS_Pr->CP_HSync_P[i] = (buffer[base+17] & 0x02) ? TRUE : FALSE;
+		     SiS_Pr->CP_VSync_P[i] = (buffer[base+17] & 0x04) ? TRUE : FALSE;
+		     SiS_Pr->CP_SyncValid[i] = TRUE;
+		     if(!havesync) {
+	                cr37 |= ((((buffer[base+17] & 0x06) ^ 0x06) << 5) | 0x20);
+			havesync = TRUE;
+	   	     }
+	          } else {
+		     SiS_Pr->CP_SyncValid[i] = FALSE;
+		  }
+	       }
+            }
+	 }
+	 if(!havesync) {
+	    xf86DrvMsg(pSiS->pScrn->scrnIndex, X_WARNING,
+	       	   "CRT2: Unable to retrieve Sync polarity information\n");
+   	 }
       }
-      
-      if(checkexpand) {
-         /* If any of the Established low-res modes is supported, the 
+
+      if(paneltype && checkexpand) {
+         /* If any of the Established low-res modes is supported, the
 	  * panel can scale automatically. For 800x600 panels, we only 
 	  * check the even lower ones.
 	  */
-	 if(paneltype == Panel310_800x600) {
+	 if(paneltype == Panel_800x600) {
 	    if(buffer[0x23] & 0xfc) cr37 |= 0x10;
 	 } else {
             if(buffer[0x23])	    cr37 |= 0x10;
@@ -10299,20 +10554,31 @@
 		buffer[0x41]);
 	 return 0;
       }
-   
-      xres = buffer[0x76] | (buffer[0x77] << 8);
-      yres = buffer[0x78] | (buffer[0x79] << 8);
+
+      paneltype = Panel_Custom;
+      SiS_Pr->CP_MaxX = xres = buffer[0x76] | (buffer[0x77] << 8);
+      SiS_Pr->CP_MaxY = yres = buffer[0x78] | (buffer[0x79] << 8);
       switch(xres) {
          case 800:
 	     if(yres == 600) {
-	     	paneltype = Panel310_800x600;
+	     	paneltype = Panel_800x600;
 	     	checkexpand = TRUE;
 	     }
 	     break;
          case 1024:
 	     if(yres == 768) {
-	     	paneltype = Panel310_1024x768;
-	     	checkexpand = FALSE;			/* Bug at 640x480; we do the scaling ourselves */
+	     	paneltype = Panel_1024x768;
+	     	checkexpand = TRUE;
+	     }
+	     break;
+	 case 1152:
+	     if(yres == 768) {
+	        if(pSiS->VGAEngine == SIS_300_VGA) {
+		   paneltype = Panel300_1152x768;
+		} else {
+		   paneltype = Panel310_1152x768;
+		}
+	     	checkexpand = TRUE;
 	     }
 	     break;
 	 case 1280:
@@ -10323,46 +10589,46 @@
 		   paneltype = Panel300_1280x960;
 		}
 	     } else if(yres == 1024) {
-	     	paneltype = Panel310_1280x1024;  
+	     	paneltype = Panel_1280x1024;
 		checkexpand = TRUE;
-	     } else if(pSiS->VGAEngine == SIS_315_VGA) {
-	        if(yres == 768) {
-	       	   paneltype = Panel310_1280x768;
-		   checkexpand = FALSE;
-		   cr37 |= 0x10;
-	        }
-	     } 
+	     } else if(yres == 768) {
+	        paneltype = Panel_1280x768;
+		checkexpand = FALSE;
+		cr37 |= 0x10;
+	     }
 	     break;
 	 case 1400:
 	     if(pSiS->VGAEngine == SIS_315_VGA) {
 	        if(yres == 1050) {
 	           paneltype = Panel310_1400x1050;
 		   checkexpand = TRUE;
-	        } 
+	        }
 	     }
       	     break;
+#if 0    /* Treat this one as custom since we have no timing data yet */
 	 case 1600:
 	     if(pSiS->VGAEngine == SIS_315_VGA) {
 	        if(yres == 1200) {
 	           paneltype = Panel310_1600x1200;
 		   checkexpand = TRUE;
-	        } 
+	        }
 	     }
       	     break;
+#endif
       }
-                 
+
       /* Determine if RGB18 or RGB24 */
       if(index) {
          if((buffer[index] == 0x20) || (buffer[index] == 0x34)) {
 	    cr37 |= 0x01;
 	 }
       }
-      
+
       if(checkexpand) {
          /* TODO - for now, we let the panel scale */
 	 cr37 |= 0x10;
       }
-     
+
       /* Now seek 4-Byte Timing codes and extract sync pol info */
       index = 0x80;
       if(buffer[0x7e] & 0x20) {			    /* skip Luminance Table (if provided) */
@@ -10384,28 +10650,117 @@
 	 if(buffer[myindex] == seekcode) {
 	    cr37 |= ((((buffer[myindex + 1] & 0x0c) ^ 0x0c) << 4) | 0x20);
 	 } else {
-	    xf86DrvMsg(pSiS->pScrn->scrnIndex, X_WARNING, 
-	    	"CRT2: Unable to retrieve Sync polarity information\n");    
+	    xf86DrvMsg(pSiS->pScrn->scrnIndex, X_WARNING,
+	        "CRT2: Unable to retrieve Sync polarity information\n");
 	 }
       } else {
-         xf86DrvMsg(pSiS->pScrn->scrnIndex, X_WARNING, 
-	    	"CRT2: Unable to retrieve Sync polarity information\n");
+         xf86DrvMsg(pSiS->pScrn->scrnIndex, X_WARNING,
+	     "CRT2: Unable to retrieve Sync polarity information\n");
+      }
+
+      /* Now seek the detailed timing descriptions for custom panels */
+      if(paneltype == Panel_Custom) {
+         index += (numcodes * 4);
+	 numcodes = buffer[0x7f] & 0x07;
+	 for(i=0; i<numcodes; i++) {
+	    xres = buffer[index+2] | ((buffer[index+4] & 0xf0) << 4);
+            yres = buffer[index+5] | ((buffer[index+7] & 0xf0) << 4);
+
+	    SiS_Pr->CP_HDisplay[i] = xres;
+	    SiS_Pr->CP_HSyncStart[i] = xres + (buffer[index+8] | ((buffer[index+11] & 0xc0) << 2));
+            SiS_Pr->CP_HSyncEnd[i] = SiS_Pr->CP_HSyncStart[i] + (buffer[index+9] | ((buffer[index+11] & 0x30) << 4));
+	    SiS_Pr->CP_HTotal[i] = xres + (buffer[index+3] | ((buffer[index+4] & 0x0f) << 8));
+	    SiS_Pr->CP_HBlankStart[i] = xres + 1;
+	    SiS_Pr->CP_HBlankEnd[i] = SiS_Pr->CP_HTotal[i];
+
+	    SiS_Pr->CP_VDisplay[i] = yres;
+            SiS_Pr->CP_VSyncStart[i] = yres + (((buffer[index+10] & 0xf0) >> 4) | ((buffer[index+11] & 0x0c) << 2));
+            SiS_Pr->CP_VSyncEnd[i] = SiS_Pr->CP_VSyncStart[i] + ((buffer[index+10] & 0x0f) | ((buffer[index+11] & 0x03) << 4));
+	    SiS_Pr->CP_VTotal[i] = yres + (buffer[index+6] | ((buffer[index+7] & 0x0f) << 8));
+	    SiS_Pr->CP_VBlankStart[i] = yres + 1;
+	    SiS_Pr->CP_VBlankEnd[i] = SiS_Pr->CP_VTotal[i];
+
+	    SiS_Pr->CP_Clock[i] = (buffer[index] | (buffer[index+1] << 8)) * 10;
+
+	    SiS_Pr->CP_DataValid[i] = TRUE;
+
+	    if((SiS_Pr->CP_HDisplay[i] > SiS_Pr->CP_HSyncStart[i])  ||
+	       (SiS_Pr->CP_HDisplay[i] >= SiS_Pr->CP_HSyncEnd[i])   ||
+	       (SiS_Pr->CP_HDisplay[i] >= SiS_Pr->CP_HTotal[i])     ||
+	       (SiS_Pr->CP_HSyncStart[i] >= SiS_Pr->CP_HSyncEnd[i]) ||
+	       (SiS_Pr->CP_HSyncStart[i] > SiS_Pr->CP_HTotal[i])    ||
+	       (SiS_Pr->CP_HSyncEnd[i] > SiS_Pr->CP_HTotal[i])      ||
+	       (SiS_Pr->CP_VDisplay[i] > SiS_Pr->CP_VSyncStart[i])  ||
+	       (SiS_Pr->CP_VDisplay[i] >= SiS_Pr->CP_VSyncEnd[i])   ||
+	       (SiS_Pr->CP_VDisplay[i] >= SiS_Pr->CP_VTotal[i])     ||
+	       (SiS_Pr->CP_VSyncStart[i] > SiS_Pr->CP_VSyncEnd[i])  ||
+	       (SiS_Pr->CP_VSyncStart[i] > SiS_Pr->CP_VTotal[i])    ||
+	       (SiS_Pr->CP_VSyncEnd[i] > SiS_Pr->CP_VTotal[i])      ||
+	       (SiS_Pr->CP_Clock[i] > 108000)                       ||
+	       (buffer[index + 17] & 0x80)) {
+
+	       SiS_Pr->CP_DataValid[i] = FALSE;
+
+	    } else {
+
+	       SiS_Pr->CP_HaveCustomData = TRUE;
+
+	       if(SiS_Pr->CP_Clock[i] > SiS_Pr->CP_MaxClock) SiS_Pr->CP_MaxClock = SiS_Pr->CP_Clock[i];
+
+	       SiS_Pr->CP_HSync_P[i] = (buffer[index + 17] & 0x02) ? TRUE : FALSE;
+	       SiS_Pr->CP_VSync_P[i] = (buffer[index + 17] & 0x04) ? TRUE : FALSE;
+	       SiS_Pr->CP_SyncValid[i] = TRUE;
+
+	       SiS_Pr->CP_Vendor = buffer[2] | (buffer[1] << 8);
+	       SiS_Pr->CP_Product = buffer[3] | (buffer[4] << 8);
+
+	       /* We must assume the panel can scale, since we have
+	        * no scaling data
+    	        */
+	       cr37 |= 0x10;
+
+	    }
+	 }
+
       }
 
       break;
-     
+
    }
-   
+
    /* 1280x960 panels are always RGB24, unable to scale and use
     * high active sync polarity
     */
    if(pSiS->VGAEngine == SIS_315_VGA) {
-      if(paneltype == Panel310_1280x960) cr37 &= 0x0e; 
+      if(paneltype == Panel310_1280x960) cr37 &= 0x0e;
    } else {
-      if(paneltype == Panel300_1280x960) cr37 &= 0x0e; 
+      if(paneltype == Panel300_1280x960) cr37 &= 0x0e;
    }
-   
+
+   for(i = 0; i < 7; i++) {
+      if(SiS_Pr->CP_DataValid[i]) {
+         xf86DrvMsg(pSiS->pScrn->scrnIndex, X_PROBED,
+            "Non-standard LCD timing data no. %d:\n", i);
+         xf86DrvMsg(pSiS->pScrn->scrnIndex, X_PROBED,
+	    "   HDisplay %d HSync %d HSyncEnd %d HTotal %d\n",
+	    SiS_Pr->CP_HDisplay[i], SiS_Pr->CP_HSyncStart[i],
+	    SiS_Pr->CP_HSyncEnd[i], SiS_Pr->CP_HTotal[i]);
+         xf86DrvMsg(pSiS->pScrn->scrnIndex, X_PROBED,
+            "   VDisplay %d VSync %d VSyncEnd %d VTotal %d\n",
+            SiS_Pr->CP_VDisplay[i], SiS_Pr->CP_VSyncStart[i],
+   	    SiS_Pr->CP_VSyncEnd[i], SiS_Pr->CP_VTotal[i]);
+         xf86DrvMsg(pSiS->pScrn->scrnIndex, X_PROBED,
+	    "   Pixel clock: %3.3fMhz\n", (float)SiS_Pr->CP_Clock[i] / 1000);
+	 xf86DrvMsg(pSiS->pScrn->scrnIndex, X_INFO,
+	    "   To use this, add \"%dx%d\" to the list of Modes in the Display section\n",
+	    SiS_Pr->CP_HDisplay[i],
+	    SiS_Pr->CP_VDisplay[i]);
+      }
+   }
+
    if(paneltype) {
+       if(!SiS_Pr->CP_PreferredX) SiS_Pr->CP_PreferredX = SiS_Pr->CP_MaxX;
+       if(!SiS_Pr->CP_PreferredY) SiS_Pr->CP_PreferredY = SiS_Pr->CP_MaxY;
        cr37 &= 0xf1;
        cr37 |= 0x02;    /* SiS301 */
        SiS_SetRegANDOR(SiS_Pr->SiS_P3d4,0x36,0xf0,paneltype);
@@ -10430,7 +10785,7 @@
    if(!(pSiS->VBFlags & (VB_301|VB_301B|VB_302B))) return 0;
 /* if(pSiS->VBFlags & VB_30xBDH) return 0;  */
    
-   if(SiS_InitDDCRegs(SiS_Pr, pSiS, 2, 0, FALSE) == 0xFFFF) return 0;
+   if(SiS_InitDDCRegs(SiS_Pr, pSiS->VBFlags, pSiS->VGAEngine, 2, 0, FALSE) == 0xFFFF) return 0;
    
    SiS_Pr->SiS_DDC_SecAddr = 0x00;
    
@@ -10454,7 +10809,7 @@
    /* Read the entire EDID */
    retry = 2;
    do {
-      if(SiS_ReadDDC(SiS_Pr, pSiS, DDCdatatype, buffer)) {
+      if(SiS_ReadDDC(SiS_Pr, DDCdatatype, buffer)) {
          xf86DrvMsg(pSiS->pScrn->scrnIndex, X_INFO, 
 	 	"CRT2: DDC read failed (attempt %d), %s\n", 
 		(3-retry), (retry == 1) ? "giving up" : "retrying");
@@ -10473,6 +10828,8 @@
 	  	"CRT2: Attached display expects digital input\n");
       	  return 0;  	
       }
+      SiS_Pr->CP_Vendor = buffer[9] | (buffer[8] << 8);
+      SiS_Pr->CP_Product = buffer[10] | (buffer[11] << 8);
       foundcrt = TRUE;
       break;
    case 3:
@@ -10486,198 +10843,19 @@
 		buffer[0x41]);
 	  return 0;
       }
+      SiS_Pr->CP_Vendor = buffer[2] | (buffer[1] << 8);
+      SiS_Pr->CP_Product = buffer[3] | (buffer[4] << 8);
       foundcrt = TRUE;
-      break;	  
+      break;
    }
-   
+
    if(foundcrt) {
        SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x32,0x10);
    }
    return(0);
 }
 
-#if 0
-   /* ----- */
-USHORT
-SiS_SenseLCDDDC(SiS_Private *SiS_Pr, SISPtr pSiS)
-{
-   USHORT DDCdatatype, paneltype, flag;
-   unsigned char cr36=0, cr37=0;
-   unsigned char tempal, tempah, tempbl, tempbh;
-   USHORT tempax, tempbx, tempcx, push1, push2, push3;
-   unsigned char addresstable[] = { 0x00, 0x22, 0x30, 0x40 };
-   int i;
-   unsigned char buffer[256];
-   
-   if(pSiS->VGAEngine != SIS_315_VGA) return 0xFFFF;
-   if(!(pSiS->VBFlags & (VB_301B|VB_302B))) return 0xFFFF;
-   
-   if(SiS_InitDDCRegs(SiS_Pr, pSiS, 1, 0, FALSE) == 0xFFFF) return 0xFFFF;   
-   
-   flag = SiS_ProbeDDC(SiS_Pr);
-   if(flag & 0x02) {
-      SiS_Pr->SiS_DDC_DeviceAddr = 0xa0;	/* EDID V1 */
-      DDCdatatype = 1;
-      SiS_Pr->SiS_DDC_SecAddr = 0x3a;
-   } else if(flag & 0x08) {
-      SiS_Pr->SiS_DDC_DeviceAddr = 0xa2;	/* EDID V2 (P&D-D Monitor) */
-      DDCdatatype = 3;
-      SiS_Pr->SiS_DDC_SecAddr = 0x76;
-   } else if(flag & 0x10) {
-      SiS_Pr->SiS_DDC_DeviceAddr = 0xa6;	/* EDID V2 (FP) */
-      DDCdatatype = 4;
-      SiS_Pr->SiS_DDC_SecAddr = 0x76;
-   } else return 0xFFFF;
-   
-   
-   SiS_ReadLCDDDC(SiS_Pr, 4, buffer);
-   tempbl = buffer[0];  /* 3a - 76 */
-   tempbh = buffer[1];  /* 3b - 77 */
-   
-   if(SiS_Pr->SiS_DDC_DeviceAddr == 0xa0) {
-   
-      /* Read and analyze EDID V1 (res) */
-   
-      tempah = 0x02;				/* 1024x768 by default */
-      tempbl &= 0xf0;
-      if(tempbl != 0x40) {			
-         tempah = 0x03;				/* 1280x1024 by default */
-	 if(tempbl == 0x50) {
-	    if(!tempbh) {
-	       tempbh = buffer[3] & 0xf0;
-	       if(tempbh == 0x30) {
-	           SiS_Pr->SiS_DDC_DeviceAddr = 0xa0;
-      		   SiS_Pr->SiS_DDC_SecAddr = 0x23;
-		   SiS_ReadLCDDDC(SiS_Pr, 2, buffer);
-		   tempbl = buffer[0];  /* 0x23 */
-		   tempbh = buffer[1];  /* 0x24 */
-		   if(tempbl) cr37 |= 0x10;
-		   tempah = 0x0a;		/* 1280x768 */
-	       }
-	       if(tempbh == 0x40) {
-	           SiS_Pr->SiS_DDC_DeviceAddr = 0xa0;
-      		   SiS_Pr->SiS_DDC_SecAddr = 0x23;
-		   SiS_ReadLCDDDC(SiS_Pr, 2, buffer);
-		   tempbl = buffer[0];  /* 0x23 */
-		   tempbh = buffer[1];  /* 0x24 */
-		   if(tempbl) cr37 |= 0x10;
-		   tempah = 0x03;		/* 1280x1024 */
-	       }
-	       tempbh = 0x00;
-	    }
-	 }
-	 if(tempbh == 0x00) goto cr36ready;
-	 tempah = 0x07;				/* 1280x960 */
-	 if(tempbh == 0xc0) goto cr36ready;
-      }
-      SiS_Pr->SiS_DDC_DeviceAddr = 0xa0;
-      SiS_Pr->SiS_DDC_SecAddr = 0x18;		/* feature support */
-      SiS_ReadLCDDDC(SiS_Pr, 2, buffer);
-      tempbl = buffer[0];
-      tempbh = buffer[1];
-      if(tempbl & 0x02) goto cr36ready;
-      SiS_Pr->SiS_DDC_DeviceAddr = 0xa0;
-      SiS_Pr->SiS_DDC_SecAddr = 0x23;		/* Established Timings */
-      SiS_ReadLCDDDC(SiS_Pr, 2, buffer);
-      tempbl = buffer[0];
-      tempbh = buffer[1];
-      tempah = 0x03;
-      if(!(tempbh & 0x01)) tempah = 0x02;
-      if(!tempbl) cr37 |= 0x10;
-      
-  } else {
-  
-      /* Read and analyze EDID V2 (res) */
-      
-      tempah = 0x02;
-      tempbx = tempbl | (tempbh << 8);
-      if(tempbx != 1024) tempah = 0x03;
-      
-  }     
-
-cr36ready:
-  cr36 = tempah;      
-  
-  if(SiS_Pr->SiS_DDC_DeviceAddr == 0xa0) {
-  
-     /* Read and analyze EDID V1 (pol) */
-  
-     SiS_Pr->SiS_DDC_SecAddr = 0x47;
-     SiS_ReadLCDDDC(SiS_Pr, 2, buffer);
-     tempah = buffer[0];
-     tempah &= 0x06;
-     tempah ^= 0x06;
-     tempah <<= 5;
-     tempah |= 0x20;
-     cr37 &= 0x1f;
-     cr37 |= tempah;
-     if((cr36 & 0x07) == 0x07) cr37 &= 0x0e;
-     
-  } else {
-  
-     /* Read and analyze EDID V2 (depth, pol) */
-  
-     push1 = tempah;
-     SiS_Pr->SiS_DDC_SecAddr = 0x45;
-     SiS_ReadLCDDDC(SiS_Pr, 2, buffer);
-     tempah = 0x01;
-     if((buffer[0] != 0x20) && (buffer[0] != 0x34)) {   /* RGB18 or 24? */
-        tempah = 0x00;
-     }
-     cr37 &= 0xfe;
-     cr37 |= tempah;
-     
-     SiS_Pr->SiS_DDC_SecAddr = 0x7e;
-     SiS_ReadLCDDDC(SiS_Pr, 2, buffer);
-     tempax = (USHORT)(buffer[0] | (buffer[1] << 8));
-     push2 = tempax;
-     tempax &= 0x0003;
-     tempax *= 0x1b;
-     push3 = tempax;
-     tempax = (USHORT)buffer[0];
-     tempax &= 0x001c;
-     tempax >>= 2;
-     tempax *= 8;
-     tempbx = push3;
-     tempbx += tempax;
-     if(buffer[0] & 0x20) {		/* Luminance table provided? */
-        SiS_Pr->SiS_DDC_SecAddr = 0x80;
-	SiS_ReadLCDDDC(SiS_Pr, 2, buffer);
-	tempax = buffer[0] | (buffer[1] << 8);
-	tempax &= 0x1f;
-	if(buffer[0] & 0x70) tempax <<= 1;
-	tempax++;	
-	tempbx += tempax;        	/* yes -> skip it */
-     }
-     tempcx = push2;
-     tempax = push1 << 8;
-     tempbx = (tempbx & 0xff00) | (((tempbx & 0x00ff) + 0x80) & 0x00ff);
-     if(tempcx & 0xf800) {
-        tempal = addresstable[((tempax & 0xff00) >> 8)];
-	tempcx &= 0xf8ff;
-	tempcx >>= 11;
-	for(i=0; i<tempcx; i++) {
-	   SiS_Pr->SiS_DDC_SecAddr = (tempbx & 0x00ff);
-	   SiS_ReadLCDDDC(SiS_Pr, 2, buffer);
-	   tempbx += 0x04;
-	   if(buffer[0] == tempal) break;
-	}
-	tempah = buffer[1];
-	tempah &= 0x0c;
-	tempah ^= 0x0c;
-	tempah <<= 4;
-	tempah |= 0x20;
-        cr37 &= 0x1f;
-        cr37 |= tempah;
-	if((cr36 & 0x07) == 0x07) cr37 &= 0x0e;
-     }
-  }
-  xf86DrvMsg(0, X_INFO, "DDC: cr36 = 0x%02x, cr37 = 0x%02x\n", cr36, cr37);
-  return 0;
-}
-#endif
-
-/* TW: Generic I2C functions (compliant to i2c library) */
+/* Generic I2C functions (compliant to i2c library) */
 
 #if 0
 USHORT
@@ -10722,7 +10900,7 @@
   SiS_SetCH70xx(SiS_Pr,tempbl);
 }
 
-/* TW: Generic I2C functions for Chrontel --------- */
+/* Generic I2C functions for Chrontel --------- */
 
 void
 SiS_SetSwitchDDC2(SiS_Private *SiS_Pr)
@@ -10736,37 +10914,37 @@
   SiS_WaitRetraceDDC(SiS_Pr);
 }
 
-/* TW: Set I2C start condition */
-/* TW: This is done by a SD high-to-low transition while SC is high */
+/* Set I2C start condition */
+/* This is done by a SD high-to-low transition while SC is high */
 USHORT
 SiS_SetStart(SiS_Private *SiS_Pr)
 {
-  if(SiS_SetSCLKLow(SiS_Pr)) return 0xFFFF;			           /* TW: (SC->low)  */
+  if(SiS_SetSCLKLow(SiS_Pr)) return 0xFFFF;			           /* (SC->low)  */
   SiS_SetRegANDOR(SiS_Pr->SiS_DDC_Port,SiS_Pr->SiS_DDC_Index,
-                  ~SiS_Pr->SiS_DDC_Data,SiS_Pr->SiS_DDC_Data);             /* TW: SD->high */
-  if(SiS_SetSCLKHigh(SiS_Pr)) return 0xFFFF;			           /* TW: SC->high */
+                  ~SiS_Pr->SiS_DDC_Data,SiS_Pr->SiS_DDC_Data);             /* SD->high */
+  if(SiS_SetSCLKHigh(SiS_Pr)) return 0xFFFF;			           /* SC->high */
   SiS_SetRegANDOR(SiS_Pr->SiS_DDC_Port,SiS_Pr->SiS_DDC_Index,
-                  ~SiS_Pr->SiS_DDC_Data,0x00);                             /* TW: SD->low = start condition */
-  if(SiS_SetSCLKHigh(SiS_Pr)) return 0xFFFF;			           /* TW: (SC->low) */
+                  ~SiS_Pr->SiS_DDC_Data,0x00);                             /* SD->low = start condition */
+  if(SiS_SetSCLKHigh(SiS_Pr)) return 0xFFFF;			           /* (SC->low) */
   return 0;
 }
 
-/* TW: Set I2C stop condition */
-/* TW: This is done by a SD low-to-high transition while SC is high */
+/* Set I2C stop condition */
+/* This is done by a SD low-to-high transition while SC is high */
 USHORT
 SiS_SetStop(SiS_Private *SiS_Pr)
 {
-  if(SiS_SetSCLKLow(SiS_Pr)) return 0xFFFF;			           /* TW: (SC->low) */
+  if(SiS_SetSCLKLow(SiS_Pr)) return 0xFFFF;			           /* (SC->low) */
   SiS_SetRegANDOR(SiS_Pr->SiS_DDC_Port,SiS_Pr->SiS_DDC_Index,
-                  ~SiS_Pr->SiS_DDC_Data,0x00);          		   /* TW: SD->low   */
-  if(SiS_SetSCLKHigh(SiS_Pr)) return 0xFFFF;			           /* TW: SC->high  */
+                  ~SiS_Pr->SiS_DDC_Data,0x00);          		   /* SD->low   */
+  if(SiS_SetSCLKHigh(SiS_Pr)) return 0xFFFF;			           /* SC->high  */
   SiS_SetRegANDOR(SiS_Pr->SiS_DDC_Port,SiS_Pr->SiS_DDC_Index,
-                  ~SiS_Pr->SiS_DDC_Data,SiS_Pr->SiS_DDC_Data);  	   /* TW: SD->high = stop condition */
-  if(SiS_SetSCLKHigh(SiS_Pr)) return 0xFFFF;			           /* TW: (SC->high) */
+                  ~SiS_Pr->SiS_DDC_Data,SiS_Pr->SiS_DDC_Data);  	   /* SD->high = stop condition */
+  if(SiS_SetSCLKHigh(SiS_Pr)) return 0xFFFF;			           /* (SC->high) */
   return 0;
 }
 
-/* TW: Write 8 bits of data */
+/* Write 8 bits of data */
 USHORT
 SiS_WriteDDC2Data(SiS_Private *SiS_Pr, USHORT tempax)
 {
@@ -10774,18 +10952,18 @@
 
   flag=0x80;
   for(i=0;i<8;i++) {
-    SiS_SetSCLKLow(SiS_Pr);				                      /* TW: SC->low */
+    SiS_SetSCLKLow(SiS_Pr);				                      /* SC->low */
     if(tempax & flag) {
       SiS_SetRegANDOR(SiS_Pr->SiS_DDC_Port,SiS_Pr->SiS_DDC_Index,
-                      ~SiS_Pr->SiS_DDC_Data,SiS_Pr->SiS_DDC_Data);            /* TW: Write bit (1) to SD */
+                      ~SiS_Pr->SiS_DDC_Data,SiS_Pr->SiS_DDC_Data);            /* Write bit (1) to SD */
     } else {
       SiS_SetRegANDOR(SiS_Pr->SiS_DDC_Port,SiS_Pr->SiS_DDC_Index,
-                      ~SiS_Pr->SiS_DDC_Data,0x00);                            /* TW: Write bit (0) to SD */
+                      ~SiS_Pr->SiS_DDC_Data,0x00);                            /* Write bit (0) to SD */
     }
-    SiS_SetSCLKHigh(SiS_Pr);				                      /* TW: SC->high */
+    SiS_SetSCLKHigh(SiS_Pr);				                      /* SC->high */
     flag >>= 1;
   }
-  temp = SiS_CheckACK(SiS_Pr);				                      /* TW: Check acknowledge */
+  temp = SiS_CheckACK(SiS_Pr);				                      /* Check acknowledge */
   return(temp);
 }
 
@@ -10846,27 +11024,27 @@
   }
 }
 
-/* TW: Check I2C acknowledge */
+/* Check I2C acknowledge */
 /* Returns 0 if ack ok, non-0 if ack not ok */
 USHORT
 SiS_CheckACK(SiS_Private *SiS_Pr)
 {
   USHORT tempah;
 
-  SiS_SetSCLKLow(SiS_Pr);				           /* TW: (SC->low) */
+  SiS_SetSCLKLow(SiS_Pr);				           /* (SC->low) */
   SiS_SetRegANDOR(SiS_Pr->SiS_DDC_Port,SiS_Pr->SiS_DDC_Index,
-                  ~SiS_Pr->SiS_DDC_Data,SiS_Pr->SiS_DDC_Data);     /* TW: (SD->high) */
-  SiS_SetSCLKHigh(SiS_Pr);				           /* TW: SC->high = clock impulse for ack */
-  tempah = SiS_GetReg1(SiS_Pr->SiS_DDC_Port,SiS_Pr->SiS_DDC_Index);/* TW: Read SD */
-  SiS_SetSCLKLow(SiS_Pr);				           /* TW: SC->low = end of clock impulse */
-  if(tempah & SiS_Pr->SiS_DDC_Data) return(1);			   /* TW: Ack OK if bit = 0 */
+                  ~SiS_Pr->SiS_DDC_Data,SiS_Pr->SiS_DDC_Data);     /* (SD->high) */
+  SiS_SetSCLKHigh(SiS_Pr);				           /* SC->high = clock impulse for ack */
+  tempah = SiS_GetReg1(SiS_Pr->SiS_DDC_Port,SiS_Pr->SiS_DDC_Index);/* Read SD */
+  SiS_SetSCLKLow(SiS_Pr);				           /* SC->low = end of clock impulse */
+  if(tempah & SiS_Pr->SiS_DDC_Data) return(1);			   /* Ack OK if bit = 0 */
   else return(0);
 }
 
-/* TW: End of I2C functions ----------------------- */
+/* End of I2C functions ----------------------- */
 
 
-/* =============== SiS 310/325/330 O.E.M. ================= */
+/* =============== SiS 315/330 O.E.M. ================= */
 
 #ifdef SIS315H
 
@@ -10876,11 +11054,11 @@
   USHORT romptr;
 
   if(HwDeviceExtension->jChipType < SIS_330) {
-     romptr = ROMAddr[0x128] | (ROMAddr[0x129] << 8);  
+     romptr = ROMAddr[0x128] | (ROMAddr[0x129] << 8);
      if(SiS_Pr->SiS_VBType & VB_SIS301B302B)
         romptr = ROMAddr[0x12a] | (ROMAddr[0x12b] << 8);
   } else {
-     romptr = ROMAddr[0x1a8] | (ROMAddr[0x1a9] << 8);  
+     romptr = ROMAddr[0x1a8] | (ROMAddr[0x1a9] << 8);
      if(SiS_Pr->SiS_VBType & VB_SIS301B302B)
         romptr = ROMAddr[0x1aa] | (ROMAddr[0x1ab] << 8);
   }
@@ -10893,11 +11071,11 @@
   USHORT romptr;
 
   if(HwDeviceExtension->jChipType < SIS_330) {
-     romptr = ROMAddr[0x120] | (ROMAddr[0x121] << 8);  
+     romptr = ROMAddr[0x120] | (ROMAddr[0x121] << 8);
      if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV)
         romptr = ROMAddr[0x122] | (ROMAddr[0x123] << 8);
   } else {
-     romptr = ROMAddr[0x1a0] | (ROMAddr[0x1a1] << 8);  
+     romptr = ROMAddr[0x1a0] | (ROMAddr[0x1a1] << 8);
      if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV)
         romptr = ROMAddr[0x1a2] | (ROMAddr[0x1a3] << 8);
   }
@@ -10922,10 +11100,22 @@
 }
 
 static USHORT
-GetLCDPtrIndexBIOS(SiS_Private *SiS_Pr)
+GetLCDPtrIndexBIOS(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension, USHORT BaseAddr)
 {
   USHORT index;
-  
+
+  if((IS_SIS650) && (SiS_Pr->SiS_VBType & VB_SIS301LV302LV)) {
+     if(!(SiS_IsNotM650or651(SiS_Pr,HwDeviceExtension, BaseAddr))) {
+        if((index = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x36) & 0xf0)) {
+	   index >>= 4;
+	   index *= 3;
+	   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) index += 2;
+           else if(!(SiS_Pr->SiS_SetFlag & LCDVESATiming)) index++;
+           return index;
+	}
+     }
+  }
+
   index = SiS_Pr->SiS_LCDResInfo & 0x0F;
   if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050)      index -= 5;
   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) index -= 6;
@@ -10933,7 +11123,6 @@
   index *= 3;
   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) index += 2;
   else if(!(SiS_Pr->SiS_SetFlag & LCDVESATiming)) index++;
-
   return index;
 }
 
@@ -10984,7 +11173,7 @@
              UCHAR *ROMAddr, USHORT ModeNo)
 {
   USHORT delay,index,myindex,temp,romptr=0;
-  
+
   if(SiS_Pr->SiS_VBInfo & SetCRT2ToRAMDAC) {			/* VGA */
      
      if((ROMAddr) && SiS_Pr->SiS_UseROM) {
@@ -11009,8 +11198,10 @@
      }
   
   } else if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {		/* LCD */
+
+     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_PanelCustom) return;
   
-     index = GetLCDPtrIndexBIOS(SiS_Pr);
+     index = GetLCDPtrIndexBIOS(SiS_Pr, HwDeviceExtension, BaseAddr);
      myindex = GetLCDPtrIndex(SiS_Pr);
      
      if(IS_SIS650 && (SiS_Pr->SiS_VBType & VB_SIS301LV302LV)) { 	/* 650+30xLV */
@@ -11018,7 +11209,7 @@
           if((ROMAddr) && SiS_Pr->SiS_UseROM) {
 #if 0	     /* Always use the second pointer on 650; some BIOSes */
              /* still carry old 301 data at the first location    */  
-	     romptr = ROMAddr[0x120] | (ROMAddr[0x121] << 8); 
+	     romptr = ROMAddr[0x120] | (ROMAddr[0x121] << 8);
 	     if(SiS_Pr->SiS_VBType & VB_SIS302LV) 
 #endif		
 	        romptr = ROMAddr[0x122] | (ROMAddr[0x123] << 8);
@@ -11032,13 +11223,13 @@
 #endif		
 	  }
        } else {
-          delay = SiS310_LCDDelayCompensation_651301LV[myindex];     
+          delay = SiS310_LCDDelayCompensation_651301LV[myindex];
 	  if(SiS_Pr->SiS_VBType & VB_SIS302LV)
-	     delay = SiS310_LCDDelayCompensation_651302LV[myindex];  
+	     delay = SiS310_LCDDelayCompensation_651302LV[myindex];
        }
      } else {
         if((ROMAddr) && SiS_Pr->SiS_UseROM && 				/* 315, 330, 740, 650+301B */
-	   (SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1280x1024)) { 
+	   (SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1280x1024)) {
            romptr = GetLCDromptr(SiS_Pr, HwDeviceExtension, ROMAddr);
 	   if(!romptr) return;
 	   delay = ROMAddr[(romptr + index)];
@@ -11046,7 +11237,7 @@
            delay = SiS310_LCDDelayCompensation_301[myindex];
            if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
 #if 0 	      /* This data is (like the one in the BIOS) wrong. */	   
-	      if(IS_SIS650740) {  /* V */
+	      if(IS_SIS550650740660) {
 	         delay = SiS310_LCDDelayCompensation_650301B[myindex];
 	      } else {
 #endif	      
@@ -11126,14 +11317,16 @@
   } else {
      if(IS_SIS650 && (SiS_Pr->SiS_VBType & VB_SIS301LV302LV)) {
         temp = (SiS_GetReg1(SiS_Pr->SiS_P3d4,0x36) & 0xf0) >> 4;
-        if(temp == 8) {
+        if(temp == 8) {		/* 1400x1050 BIOS */
 	   delay &= 0x0f;
 	   delay |= 0xb0;
         } else if(temp == 6) {
            delay &= 0x0f;
 	   delay |= 0xc0;
+        } else if(temp > 7) {	/* 1280x1024 BIOS */
+	   delay = 0x35;
         }
-        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x2D,delay);  /* index 2D D[3:0] */
+        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x2D,delay);
      } else {
         SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2D,0xF0,delay);
      }
@@ -11156,7 +11349,7 @@
 
   if(ROMAddr && SiS_Pr->SiS_UseROM) {
      romptr = ROMAddr[0x112] | (ROMAddr[0x113] << 8);
-     if(HwDeviceExtension->jChipType == SIS_330) {
+     if(HwDeviceExtension->jChipType >= SIS_330) {
         romptr = ROMAddr[0x192] | (ROMAddr[0x193] << 8);
      }
   }
@@ -11188,7 +11381,7 @@
 
   if(ROMAddr && SiS_Pr->SiS_UseROM) {
      romptr = ROMAddr[0x124] | (ROMAddr[0x125] << 8);
-     if(HwDeviceExtension->jChipType == SIS_330) {
+     if(HwDeviceExtension->jChipType >= SIS_330) {
         romptr = ROMAddr[0x1a4] | (ROMAddr[0x1a5] << 8);
      }
   }
@@ -11236,7 +11429,7 @@
 
   if(ROMAddr && SiS_Pr->SiS_UseROM) {
   	OutputSelect = ROMAddr[0xf3];
-	if(HwDeviceExtension->jChipType == SIS_330) {
+	if(HwDeviceExtension->jChipType >= SIS_330) {
 	    OutputSelect = ROMAddr[0x11b];
 	}
   }
@@ -11300,17 +11493,17 @@
    */
   if((ROMAddr) && SiS_Pr->SiS_UseROM) {
      romptr = ROMAddr[0x116] | (ROMAddr[0x117] << 8);
-     if(HwDeviceExtension->jChipType == SIS_330) {
+     if(HwDeviceExtension->jChipType >= SIS_330) {
         romptr = ROMAddr[0x196] | (ROMAddr[0x197] << 8);
      }
      if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
         romptr = ROMAddr[0x11c] | (ROMAddr[0x11d] << 8);
-	if(HwDeviceExtension->jChipType == SIS_330) {
+	if(HwDeviceExtension->jChipType >= SIS_330) {
 	   romptr = ROMAddr[0x19c] | (ROMAddr[0x19d] << 8);
 	}
 	if((SiS_Pr->SiS_VBInfo & SetInSlaveMode) && (!(SiS_Pr->SiS_SetFlag & TVSimuMode))) {
 	   romptr = ROMAddr[0x116] | (ROMAddr[0x117] << 8);
-	   if(HwDeviceExtension->jChipType == SIS_330) {
+	   if(HwDeviceExtension->jChipType >= SIS_330) {
               romptr = ROMAddr[0x196] | (ROMAddr[0x197] << 8);
            }
 	}
@@ -11334,19 +11527,19 @@
      }
   }
 
-  if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {    /* TW: 650/301LV: (VB_SIS301LV | VB_SIS302LV)) */
+  if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {    /* 650/301LV: (VB_SIS301LV | VB_SIS302LV)) */
      if((!(SiS_Pr->SiS_VBInfo & SetPALTV)) && (ModeNo > 0x13)) {
-        if(resinfo == 6) {
+        if(resinfo == SIS_RI_640x480) {
 	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x31,0x21);
 	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x32,0xf0);
 	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x33,0xf5);
 	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x34,0x7f);
-	} else if (resinfo == 7) {
+	} else if (resinfo == SIS_RI_800x600) {
 	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x31,0x21);
 	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x32,0xf0);
 	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x33,0xf5);
 	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x34,0x7f);
-	} else if (resinfo == 8) {
+	} else if (resinfo == SIS_RI_1024x768) {
 	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x31,0x1e);
 	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x32,0x8b);
 	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x33,0xfb);
@@ -11361,7 +11554,9 @@
                   UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex)
 {
    SetDelayComp(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo);
-   /* TW: The TV functions are not for LVDS */
+
+   if(SiS_Pr->UseCustomMode) return;
+
    if( (SiS_Pr->SiS_IF_DEF_LVDS == 0) && (SiS_Pr->SiS_VBInfo & SetCRT2ToTV) ) {
        SetAntiFlicker(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,ModeIdIndex);
        SetPhaseIncr(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,ModeIdIndex);
@@ -11373,7 +11568,7 @@
 }
 
 /* FinalizeLCD
- * This finalizes some Part1 registers for the very panel used.
+ * This finalizes some CRT2 registers for the very panel used.
  * If we have a backup if these registers, we use it; otherwise
  * we set the register according to most BIOSes. However, this
  * function looks quite different in every BIOS, so you better
@@ -11388,6 +11583,9 @@
 
   if(!(SiS_Pr->SiS_VBType & VB_SIS301LV302LV)) return;
 
+  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_PanelCustom) return;
+  if(SiS_Pr->UseCustomMode) return;
+
   if(ModeNo <= 0x13) {
 	resinfo = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ResInfo;
 	modeflag =  SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
@@ -11396,20 +11594,31 @@
 	modeflag =  SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
   }
 
+  if((SiS_GetReg1(SiS_Pr->SiS_P3d4, 0x5f) & 0xf0)) {
+     SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x1e,0x03);
+  }
+
   if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
      if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
         SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x2a,0x00);
 	SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x30,0x00);
 	SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x34,0x10);
+     } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {   /* For all panels? */
+        SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x24,0xfc,0x01);
      }
      tempch = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x36);
      tempch &= 0xf0;
      tempch >>= 4;
      if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
+        if(tempch == 0x03) {
+	   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x18,0x02);
+	   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1b,0x25);
+	   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1c,0x00);
+	   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1d,0x1b);
+	}
 	if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) {
 	   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1f,0x76);
-	}
-	if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {	
+	} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
 	   if((SiS_Pr->Backup == TRUE) && (SiS_Pr->Backup_Mode == ModeNo)) {
 	      SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x14,SiS_Pr->Backup_14);
 	      SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x15,SiS_Pr->Backup_15);
@@ -11425,14 +11634,14 @@
 	       SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x14,0x90);
 	       if(ModeNo <= 0x13) {
 	          SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x18,0x11);
-		  if((resinfo == 0) && (resinfo == 2)) return;
+		  if((resinfo == 0) || (resinfo == 2)) return;
 		  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x18,0x18);
-		  if((resinfo == 1) && (resinfo == 3)) return;
+		  if((resinfo == 1) || (resinfo == 3)) return;
 	       }
 	       SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x18,0x02);
-	       if((ModeNo > 0x13) && (resinfo == 8)) {
+	       if((ModeNo > 0x13) && (resinfo == SIS_RI_1024x768)) {
 	          SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x18,0x02);  /* 1.10.7u */
-#if 0	       
+#if 0
 	          tempbx = 806;  /* 0x326 */			 /* other older BIOSes */
 		  tempbx--;
 		  temp = tempbx & 0xff;
@@ -11487,7 +11696,7 @@
 	tempbl = SiS_GetReg1(SiS_Pr->SiS_Part2Port,0x04);
 	tempbx = (tempbh << 8) | tempbl;
 	if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
-	   if((resinfo == 8) || (!(SiS_Pr->SiS_LCDInfo & DontExpandLCD))) {
+	   if((resinfo == SIS_RI_1024x768) || (!(SiS_Pr->SiS_LCDInfo & DontExpandLCD))) {
 	      if(SiS_Pr->SiS_SetFlag & LCDVESATiming) {
 	      	tempbx = 770;
 	      } else {
@@ -11515,67 +11724,56 @@
   }
 }
 
-#if 0
-/* TW: New and checked from 650/301LV BIOS */
-/* This might clash with newer "FinalizeLCD()" function */
+#endif
+
+
+/*  =================  SiS 300 O.E.M. ================== */
+
+#ifdef SIS300
+
 void
-SiS_OEMLCD(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,USHORT BaseAddr,
-                  UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex)
+SetOEMLCDData2(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,USHORT BaseAddr,
+              UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex, USHORT RefTabIndex)
 {
-   USHORT tempbx,tempah,tempbl,tempbh,tempcl;
-
-   if(!(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV)) return;
+  USHORT crt2crtc=0, modeflag, myindex=0;
+  UCHAR  temp;
+  int i;
 
-   if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
-      SiS_UnLockCRT2(SiS_Pr,HwDeviceExtension,BaseAddr);
-      tempbh = SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x1a);
-      tempbh &= 0x38;
-      tempbh >>= 3;
-      tempbl = SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x18);
-      tempbx = (tempbh << 8) | tempbl;
-      if(SiS_Pr->SiS_LCDTypeInfo == 1)  tempbx -= 0x12;
-      SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x18,tempbx & 0x00ff);
-      tempah = (tempbx & 0xff00) >> 8;
-      tempah &= 0x07;
-      tempah <<= 3;
-      SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x1a,0xc7,tempah);
-      tempah = SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x19);
-      tempah &= 0x0f;
-      if(SiS_Pr->SiS_LCDTypeInfo == 1)  tempah -= 2;
-      tempah &= 0x0f;
-      SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x19,0xF0,tempah);
-      tempah = SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x14);
-      if(SiS_Pr->SiS_LCDTypeInfo == 1)  tempah++;
-      tempah -= 8;
-      SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x14,tempah);
-   } else if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
-      tempcl = tempbh = SiS_GetReg1(SiS_Pr->SiS_Part2Port,0x01);
-      tempbh &= 0x70;
-      tempbh >>= 4;
-      tempbl = SiS_GetReg1(SiS_Pr->SiS_Part2Port,0x04);
-      tempbx = (tempbh << 8) | tempbl;
-      if(SiS_Pr->SiS_LCDTypeInfo == 1)  {
-           tempbx -= 0x1e;
-	   tempcl &= 0x0f;
-	   tempcl -= 4;
-	   tempcl &= 0x0f;
-      }
-      tempbl = tempbx & 0x00ff;
-      tempbh = (tempbx >> 8) & 0x00ff;
-      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x04,tempbl);
-      tempbh <<= 4;
-      tempbh |= tempcl;
-      SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x01,0x80,tempbh);
-   }
-}
-#endif
+  if(ModeNo <= 0x13) {
+        modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
+	crt2crtc = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_CRT2CRTC;
+  } else {
+        modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
+	crt2crtc = SiS_Pr->SiS_RefIndex[RefTabIndex].Ext_CRT2CRTC;
+  }
 
-#endif
+  crt2crtc &= 0x3f;
 
+  if(SiS_Pr->SiS_CustomT == CUT_BARCO1024) {
+     SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x13,0xdf);
+  }
 
-/*  =================  SiS 300 O.E.M. ================== */
+  if(SiS_Pr->SiS_CustomT == CUT_BARCO1366) {
+     if(modeflag & HalfDCLK) myindex = 1;
 
-#ifdef SIS300
+     if(SiS_LowModeStuff(SiS_Pr,ModeNo,HwDeviceExtension)) {
+        for(i=0; i<7; i++) {
+           if(barco_p1[myindex][crt2crtc][i][0]) {
+	      SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,
+	                      barco_p1[myindex][crt2crtc][i][0],
+	   	   	      barco_p1[myindex][crt2crtc][i][2],
+			      barco_p1[myindex][crt2crtc][i][1]);
+	   }
+        }
+     }
+     temp = SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x00);
+     if(temp & 0x80) {
+        temp = SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x18);
+        temp++;
+        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x18,temp);
+     }
+  }
+}
 
 #if 0   /* Not used */
 static USHORT
@@ -11620,7 +11818,7 @@
 
   if(HwDeviceExtension->jChipType == SIS_300) {
 
-    tempbx = SiS_Pr->SiS_LCDResInfo - 2;
+    tempbx = (SiS_GetReg1(SiS_Pr->SiS_P3d4,0x36) & 0x0f) - 2;
     if(!(SiS_Pr->SiS_SetFlag & LCDVESATiming)) tempbx += 4;
     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
        if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) tempbx += 3;
@@ -11674,24 +11872,29 @@
 {
   USHORT index,temp,romptr=0;
 
+  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_PanelCustom) return;
+
   if((ROMAddr) && SiS_Pr->SiS_UseROM) {
      if(!(ROMAddr[0x237] & 0x01)) return;
      if(!(ROMAddr[0x237] & 0x02)) return;
      romptr = ROMAddr[0x24b] | (ROMAddr[0x24c] << 8);
   }
 
-  /* TW: The Panel Compensation Delay should be set according to tables
-   *     here. Unfortunately, various BIOS versions don't case about
-   *     a uniform way using eg. ROM byte 0x220, but use different
-   *     hard coded delays (0x04, 0x20, 0x18) in SetGroup1().
-   *     Thus we don't set this if the user select a custom pdc or if
-   *     we otherwise detected a valid pdc.
+  /* The Panel Compensation Delay should be set according to tables
+   * here. Unfortunately, various BIOS versions don't case about
+   * a uniform way using eg. ROM byte 0x220, but use different
+   * hard coded delays (0x04, 0x20, 0x18) in SetGroup1().
+   * Thus we don't set this if the user select a custom pdc or if
+   * we otherwise detected a valid pdc.
    */
   if(HwDeviceExtension->pdc) return;
 
   temp = GetOEMLCDPtr(SiS_Pr,HwDeviceExtension, ROMAddr, 0);
 
-  index = SiS_Pr->SiS_VBModeIDTable[ModeIdIndex].VB_LCDDelayIndex;
+  if(SiS_Pr->UseCustomMode)
+     index = 0;
+  else
+     index = SiS_Pr->SiS_VBModeIDTable[ModeIdIndex].VB_LCDDelayIndex;
 
   if(HwDeviceExtension->jChipType != SIS_300) {
 	if(romptr) {
@@ -11738,9 +11941,9 @@
 
 static void
 SetOEMLCDData(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,USHORT BaseAddr,
-               UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex)
+              UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex)
 {
-#if 0  /* TW: Unfinished; VData table missing */
+#if 0  /* Unfinished; Data table missing */
   USHORT index,temp;
 
   if((ROMAddr) && SiS_Pr->SiS_UseROM) {
@@ -11937,25 +12140,29 @@
 
 void
 SiS_OEM300Setting(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,
-		  USHORT BaseAddr,UCHAR *ROMAddr,USHORT ModeNo)
+		  USHORT BaseAddr,UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex,
+		  USHORT RefTableIndex)
 {
-  USHORT ModeIdIndex;
+  USHORT OEMModeIdIndex=0;
 
-  ModeIdIndex = SiS_SearchVBModeID(SiS_Pr,ROMAddr,&ModeNo);
-  if(!(ModeIdIndex)) return;
+  if(!SiS_Pr->UseCustomMode) {
+     OEMModeIdIndex = SiS_SearchVBModeID(SiS_Pr,ROMAddr,&ModeNo);
+     if(!(OEMModeIdIndex)) return;
+  }
 
   if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
-       SetOEMLCDDelay(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,ModeIdIndex);
+       SetOEMLCDDelay(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,OEMModeIdIndex);
        if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
-            SetOEMLCDData(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,ModeIdIndex);
+            SetOEMLCDData(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,OEMModeIdIndex);
        }
   }
+  if(SiS_Pr->UseCustomMode) return;
   if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
-       SetOEMTVDelay(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,ModeIdIndex);
+       SetOEMTVDelay(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,OEMModeIdIndex);
        if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
-       		SetOEMAntiFlicker(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,ModeIdIndex);
-    		SetOEMPhaseIncr(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,ModeIdIndex);
-       		SetOEMYFilter(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,ModeIdIndex);
+       		SetOEMAntiFlicker(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,OEMModeIdIndex);
+    		SetOEMPhaseIncr(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,OEMModeIdIndex);
+       		SetOEMYFilter(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,OEMModeIdIndex);
        }
   }
 }

FUNET's LINUX-ADM group, linux-adm@nic.funet.fi
TCL-scripts by Sam Shen (who was at: slshen@lbl.gov)