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

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

diff -urN linux-2.4.24/drivers/video/sis/init301.c linux-2.4.25/drivers/video/sis/init301.c
@@ -1,34 +1,60 @@
-/* $XFree86: xc/programs/Xserver/hw/xfree86/drivers/sis/init301.c,v 1.3 2002/22/04 01:16:16 dawes Exp $ */
+/* $XFree86$ */
 /*
- * Mode switching code (CRT2 section)
- * for SiS 300/305/540/630/730/315/550/650/M650/651/740/330/660/M660/760/M760
+ * Mode initializing code (CRT2 section)
+ * for SiS 300/305/540/630/730 and
+ *     SiS 315/550/650/M650/651/661FX/M661xX/740/741/M741/330/660/M660/760/M760
  * (Universal module for Linux kernel framebuffer and XFree86 4.x)
  *
- * Copyright 2002, 2003 by Thomas Winischhofer <thomas@winischhofer.net>
- * Formerly based on non-functional code-fragements for 300 series by SiS, Inc.
+ * Copyright (C) 2001-2004 by Thomas Winischhofer, Vienna, Austria
+ *
+ * If distributed as part of the Linux kernel, the following license terms
+ * apply:
  *
- * If distributed as part of the linux kernel, the contents of this file
- * is entirely covered by the GPL.
+ * * This program is free software; you can redistribute it and/or modify
+ * * it under the terms of the GNU General Public License as published by
+ * * the Free Software Foundation; either version 2 of the named License,
+ * * or any later version.
+ * *
+ * * This program is distributed in the hope that it will be useful,
+ * * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * * GNU General Public License for more details.
+ * *
+ * * You should have received a copy of the GNU General Public License
+ * * along with this program; if not, write to the Free Software
+ * * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
  *
- * Otherwise, the following terms apply:
+ * Otherwise, the following license 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
- * the above copyright notice appear in all copies and that both that
- * copyright notice and this permission notice appear in supporting
- * documentation, and that the name of the copyright holder not be used in
- * advertising or publicity pertaining to distribution of the software without
- * specific, written prior permission.  The copyright holder makes no representations
- * about the suitability of this software for any purpose.  It is provided
- * "as is" without express or implied warranty.
+ * * Redistribution and use in source and binary forms, with or without
+ * * modification, are permitted provided that the following conditions
+ * * are met:
+ * * 1) Redistributions of source code must retain the above copyright
+ * *    notice, this list of conditions and the following disclaimer.
+ * * 2) Redistributions in binary form must reproduce the above copyright
+ * *    notice, this list of conditions and the following disclaimer in the
+ * *    documentation and/or other materials provided with the distribution.
+ * * 3) All advertising materials mentioning features or use of this software
+ * *    must display the following acknowledgement: "This product includes
+ * *    software developed by Thomas Winischhofer, Vienna, Austria."
+ * * 4) The name of the author may not be used to endorse or promote products
+ * *    derived from this software without specific prior written permission.
+ * *
+ * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
- * THE COPYRIGHT HOLDER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
- * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
- * EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY SPECIAL, INDIRECT OR
- * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
- * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
- * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
- * PERFORMANCE OF THIS SOFTWARE.
+ * Author: 	Thomas Winischhofer <thomas@winischhofer.net>
+ *
+ * Formerly based on non-functional code-fragements for 300 series by SiS, Inc.
+ * Used by permission.
  *
  * TW says: This code looks awful, I know. But please don't do anything about
  * this otherwise debugging will be hell.
@@ -36,14 +62,18 @@
  * video bridges and combinations thereof. If anything is changed, extreme
  * care has to be taken that that change doesn't break it for other chipsets,
  * bridges or combinations thereof.
- * All comments in this file are by me, regardless if they are marked TW or not.
+ * All comments in this file are by me, regardless if marked TW or not.
  *
  */
- 
-#if 1 
-#define NEWCH701x
+
+#if 1
+#define SET_EMI		/* 302LV/ELV: Set EMI values */
 #endif
 
+#define COMPAL_HACK	/* Needed for Compal 1400x1050 (EMI) */
+#define COMPAQ_HACK	/* Needed for Inventec/Compaq 1280x1024 (EMI) */
+#define ASUS_HACK	/* Needed for Asus A2H 1024x768 (EMI) */
+
 #include "init301.h"
 
 #if 0
@@ -61,2693 +91,3413 @@
 #define SiS_I2CDELAY      1000
 #define SiS_I2CDELAYSHORT  150
 
-BOOLEAN
-SiS_SetCRT2Group(SiS_Private *SiS_Pr, USHORT BaseAddr,UCHAR *ROMAddr,USHORT ModeNo,
-                 PSIS_HW_DEVICE_INFO HwDeviceExtension)
+/*********************************************/
+/*         HELPER: Lock/Unlock CRT2          */
+/*********************************************/
+
+void
+SiS_UnLockCRT2(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
 {
-   USHORT ModeIdIndex;
-   USHORT RefreshRateTableIndex;
+  if(HwInfo->jChipType >= SIS_315H)
+     SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2f,0x01);
+  else
+     SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x24,0x01);
+}
 
-   SiS_Pr->SiS_SetFlag |= ProgrammingCRT2;
+void
+SiS_LockCRT2(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
+{
+  if(HwInfo->jChipType >= SIS_315H)
+     SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2F,0xFE);
+  else
+     SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x24,0xFE);
+}
 
-   if(!SiS_Pr->UseCustomMode) {
-      SiS_SearchModeID(SiS_Pr,ROMAddr,&ModeNo,&ModeIdIndex);
-   } else {
-      ModeIdIndex = 0;
-   }
+/*********************************************/
+/*            HELPER: Enable CRT2            */
+/*********************************************/
 
-   /* Used for shifting CR33 */
-   SiS_Pr->SiS_SelectCRT2Rate = 4;
+void
+SiS_EnableCRT2(SiS_Private *SiS_Pr)
+{
+  SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x1E,0x20);
+}
 
-   SiS_UnLockCRT2(SiS_Pr, HwDeviceExtension, BaseAddr);
+/*********************************************/
+/*    HELPER: Get Pointer to LCD structure   */
+/*********************************************/
+
+/* For 661 series only */
+#ifdef SIS315H
+#if 0   /* Need to wait until hardware using this really exists */
+static UCHAR *
+GetLCDPtr661(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo, int tabletype,
+             USHORT ModeNo, USHORT ModeIdIndex, USHORT RRTI)
+{
+  UCHAR *ROMAddr =  HwInfo->pjVirtualRomBase;
+  UCHAR *tableptr = NULL;
+  UCHAR tablelengths[] = { 8, 7, 6, 6, 8, 6, 0, 0, 0 };
+  USHORT modeflag, CRT2Index, tablelength, lcdid, myid, tableptri;
 
-   RefreshRateTableIndex = SiS_GetRatePtrCRT2(SiS_Pr, ROMAddr,ModeNo,ModeIdIndex,HwDeviceExtension);
+  if(ModeNo <= 0x13) {
+     modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
+     CRT2Index = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_CRT2CRTC;
+  } else {
+     modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
+     CRT2Index = SiS_Pr->SiS_RefIndex[RRTI].Ext_CRT2CRTC;
+     /* This is total bullshit: */
+     if(SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO == SIS_RI_720x576) CRT2Index = 10;
+  }
 
-   SiS_SaveCRT2Info(SiS_Pr,ModeNo);
+  if(tabletype <= 1) {
+#if 0	/* Not yet implemented */
+     if(ModeNo <= 0x13) {
+        CRT2Index = SiS_Pr->SiS_SModeIDTable[ModeIdIndex]. + 5;
+     } else {
+        CRT2Index = SiS_Pr->SiS_RefIndex[RRTI]. + 5;
+     }
+     if(tabletype & 1) CRT2Index >>= 4;
+#endif
+  }
 
-   if(SiS_LowModeStuff(SiS_Pr,ModeNo,HwDeviceExtension)) {
-      SiS_DisableBridge(SiS_Pr,HwDeviceExtension,BaseAddr);
-      if((SiS_Pr->SiS_IF_DEF_LVDS == 1) && (HwDeviceExtension->jChipType == SIS_730)) {
-         SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x00,0x80);
-      }
-      SiS_SetCRT2ModeRegs(SiS_Pr,BaseAddr,ModeNo,ModeIdIndex,HwDeviceExtension);
-   }
+  CRT2Index &= 0x0f;
 
-   if(SiS_Pr->SiS_VBInfo & DisableCRT2Display) {
-      SiS_LockCRT2(SiS_Pr,HwDeviceExtension, BaseAddr);
-      SiS_DisplayOn(SiS_Pr);
-      return(TRUE);
-   }
+  tablelength = tablelengths[tabletype];
+  if(SiS_Pr->SiS_LCDInfo & LCDPass11) {
+     if((tabletype == 5) || (tabletype == 7)) tablelength = 8;
+     if((tabletype == 3) || (tabletype == 8)) tablelength = 8;
+  }
 
-   SiS_GetCRT2Data(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
-                   HwDeviceExtension);
+  if(!tablelength) return NULL;
 
-   /* Set up Panel Link for LVDS, 301BDH and 650/30xLV(for LCDA) */
-   if( (SiS_Pr->SiS_IF_DEF_LVDS == 1) ||
-       ((SiS_Pr->SiS_VBType & VB_NoLCD) && (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD)) ||
-       ((HwDeviceExtension->jChipType >= SIS_315H) && (SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV)) ) {
-   	SiS_GetLVDSDesData(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
-	                   HwDeviceExtension, BaseAddr);
+  tableptri = ROMAddr[0x222] | (ROMAddr[0x223] << 8);
+  tableptri += (tabletype << 1);
+  if(!tableptri) return NULL;
+  tableptr = &ROMAddr[tableptri];
+
+  do {
+     lcdid = tableptr[0];
+     if(lcdid == 0xff) break;
+     myid = SiS_Pr->SiS_LCDResInfo;
+     if((lcdid & 0x80) && (lcdid != 0x80)) {
+        lcdid &= 0x7f;
+	myid = SiS_Pr->SiS_LCDTypeInfo;
+     }
+     if(SiS_Pr->SiS_LCDInfo & LCDPass11) myid &= ~0x1f;
+
+     if(myid == lcdid) {
+	lcdid = tableptr[1] | (tableptr[2] << 8);
+	myid = SiS_Pr->SiS_LCDInfo661;
+	if(modeflag & HalfDCLK) myid |= 0x200;
+	if(ModeNo <= 0x13)      myid |= 0x400;
+	lcdid &= myid;
+	myid = tableptr[3] | (tableptr[4] << 8);
+	if(lcdid == myid) break;
+     }
+     tableptr += 7;
+  } while (1);
+
+  if(lcdid == myid) {
+     lcdid = tableptr[5] | (tableptr[6] << 8);
+     lcdid += (tablelength * CRT2Index);
+     return((UCHAR *)&ROMAddr[lcdid]);
+  }
+
+  return NULL;
+}
+#endif
+
+static UCHAR *
+GetLCDStructPtr661(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
+{
+   UCHAR  *ROMAddr = HwInfo->pjVirtualRomBase;
+   USHORT lcdres = SiS_Pr->SiS_LCDResInfo;
+   USHORT lcdtype = SiS_Pr->SiS_LCDTypeInfo;
+   USHORT romindex=0;
+   UCHAR  *myptr = NULL;
+   UCHAR  lcdid;
+
+   if((ROMAddr) && SiS_Pr->SiS_UseROM) {
+      romindex = ROMAddr[0x256] | (ROMAddr[0x257] << 8);
+   }
+   if(romindex) {
+      myptr = &ROMAddr[romindex];
    } else {
-        SiS_Pr->SiS_LCDHDES = SiS_Pr->SiS_LCDVDES = 0;
+      myptr = (UCHAR *)SiS_LCDStruct661;
    }
 
-#ifdef LINUX_XF86
-#ifdef TWDEBUG
-  xf86DrvMsg(0, X_INFO, "(init301: LCDHDES 0x%03x LCDVDES 0x%03x)\n", SiS_Pr->SiS_LCDHDES, SiS_Pr->SiS_LCDVDES);
-  xf86DrvMsg(0, X_INFO, "(init301: HDE     0x%03x VDE     0x%03x)\n", SiS_Pr->SiS_HDE, SiS_Pr->SiS_VDE);
-  xf86DrvMsg(0, X_INFO, "(init301: VGAHDE  0x%03x VGAVDE  0x%03x)\n", SiS_Pr->SiS_VGAHDE, SiS_Pr->SiS_VGAVDE);
-  xf86DrvMsg(0, X_INFO, "(init301: HT      0x%03x VT      0x%03x)\n", SiS_Pr->SiS_HT, SiS_Pr->SiS_VT);
-  xf86DrvMsg(0, X_INFO, "(init301: VGAHT   0x%03x VGAVT   0x%03x)\n", SiS_Pr->SiS_VGAHT, SiS_Pr->SiS_VGAVT);
-#endif
+   while(myptr[0] != 0xff) {
+      lcdid = myptr[0];
+      if((lcdid & 0x80) && (lcdid != 0x80)) {
+         lcdres = lcdtype;
+	 lcdid &= 0x7f;
+      }
+      if(lcdid == lcdres) break;
+      myptr += 26;
+   }
+   if(myptr[0] == 0xff) return NULL;
+
+   return myptr;
+}
 #endif
 
-   if(SiS_LowModeStuff(SiS_Pr,ModeNo,HwDeviceExtension)) {
-      SiS_SetGroup1(SiS_Pr,BaseAddr,ROMAddr,ModeNo,ModeIdIndex,
-                    HwDeviceExtension,RefreshRateTableIndex);
-   }
-
-   if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
-
-        if(SiS_LowModeStuff(SiS_Pr,ModeNo,HwDeviceExtension)) {
-
-	   SiS_SetGroup2(SiS_Pr,BaseAddr,ROMAddr,ModeNo,ModeIdIndex,
-	                 RefreshRateTableIndex,HwDeviceExtension);
-      	   SiS_SetGroup3(SiS_Pr,BaseAddr,ROMAddr,ModeNo,ModeIdIndex,
-	                 HwDeviceExtension);
-      	   SiS_SetGroup4(SiS_Pr,BaseAddr,ROMAddr,ModeNo,ModeIdIndex,
-	                 RefreshRateTableIndex,HwDeviceExtension);
-      	   SiS_SetGroup5(SiS_Pr,HwDeviceExtension, BaseAddr,ROMAddr,
-	                 ModeNo,ModeIdIndex);
+/*********************************************/
+/*           Adjust Rate for CRT2            */
+/*********************************************/
 
-	   /* 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_SetFlag & SetDOSMode) && ((ModeNo == 0x03) || (ModeNo = 0x10)))) {
-		    if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
-		       SiS_ModCRT1CRTC(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,
-		                       RefreshRateTableIndex,HwDeviceExtension);
-		    }
-                 }
-	      }
-	      SiS_SetCRT2ECLK(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,
-		              RefreshRateTableIndex,HwDeviceExtension);
-	   }
-        }
+static BOOLEAN
+SiS_AdjustCRT2Rate(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,
+                   USHORT RefreshRateTableIndex, USHORT *i,
+		   PSIS_HW_INFO HwInfo)
+{
+  USHORT tempax,tempbx,infoflag;
 
-   } else {
+  tempbx = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex + (*i)].ModeID;
 
-        if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480) {
-	   if(SiS_Pr->SiS_IF_DEF_TRUMPION == 0) {
-    	      SiS_ModCRT1CRTC(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,
-	                      RefreshRateTableIndex,HwDeviceExtension);
+  tempax = 0;
+
+  if(SiS_Pr->SiS_VBType & VB_SISVB) {
+
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToRAMDAC) {
+
+      	tempax |= SupportRAMDAC2;
+	if(HwInfo->jChipType >= SIS_315H) {
+	   tempax |= SupportRAMDAC2_135;
+	   if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
+	      tempax |= SupportRAMDAC2_162;
+	      if(SiS_Pr->SiS_VBType & VB_SIS301C) {
+		 tempax |= SupportRAMDAC2_202;
+	      }
 	   }
 	}
 
-        SiS_SetCRT2ECLK(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,
-	                RefreshRateTableIndex,HwDeviceExtension);
+     } else if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
 
-	if(SiS_LowModeStuff(SiS_Pr,ModeNo,HwDeviceExtension)) {
-     	   if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
-	      if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
-	         if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
-#ifdef SIS315H		 
-		    SiS_SetCH701xForLCD(SiS_Pr,HwDeviceExtension,BaseAddr);
-#endif		    
+     	tempax |= SupportLCD;
+	if(HwInfo->jChipType >= SIS_315H) {
+	   if(SiS_Pr->SiS_VBType & VB_SIS301B302B) {
+	      if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
+	         if(tempbx == 0x2e) {  /* 640x480 */
+		    tempax |= Support64048060Hz;
 		 }
 	      }
-	      if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
-       		 SiS_SetCHTVReg(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,
-		               RefreshRateTableIndex);
-	      }
-     	   }
+	   }
 	}
 
-   }
+     } else if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) {
 
-#ifdef SIS300
-   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
+      	tempax |= SupportHiVision;
 
-#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
+     } else if(SiS_Pr->SiS_VBInfo & (SetCRT2ToYPbPr525750|SetCRT2ToAVIDEO|SetCRT2ToSVIDEO|SetCRT2ToSCART)) {
 
-   if(SiS_LowModeStuff(SiS_Pr,ModeNo,HwDeviceExtension)) {
-      SiS_EnableBridge(SiS_Pr,HwDeviceExtension,BaseAddr);
-   }
+        tempax |= SupportTV;
+	if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
+	   tempax |= SupportTV1024;
+	}
 
-   SiS_DisplayOn(SiS_Pr);
+     }
 
-   if(SiS_Pr->SiS_IF_DEF_CH70xx == 1) {
-      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);
-      }
-   }
+  } else {	/* for LVDS  */
 
-   if(SiS_LowModeStuff(SiS_Pr,ModeNo,HwDeviceExtension)) {
-      SiS_LockCRT2(SiS_Pr,HwDeviceExtension, BaseAddr);
-   }
+     if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
+     	if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+           tempax |= SupportCHTV;
+      	}
+     }
 
-   return 1;
-}
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
+     	tempax |= SupportLCD;
+     }
 
-BOOLEAN
-SiS_LowModeStuff(SiS_Private *SiS_Pr, USHORT ModeNo,
-                 PSIS_HW_DEVICE_INFO HwDeviceExtension)
-{
-    USHORT temp,temp1,temp2;
+  }
 
-    if((ModeNo != 0x03) && (ModeNo != 0x10) && (ModeNo != 0x12))
-       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);
-    SiS_SetReg1(SiS_Pr->SiS_P3d4,0x00,0x55);
-    temp2 = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x00);
-    SiS_SetReg1(SiS_Pr->SiS_P3d4,0x00,temp1);
-    SiS_SetReg1(SiS_Pr->SiS_P3d4,0x11,temp);
-    if((HwDeviceExtension->jChipType >= SIS_315H) ||
-       (HwDeviceExtension->jChipType == SIS_300)) {
-       if(temp2 == 0x55) return(0);
-       else return(1);
-    } else {
-       if(temp2 != 0x55) return(1);
-       else {
-          SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x35,0x01);
-          return(0);
-       }
-    }
+  /* 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) return(1);
+     if((*i) == 0) break;
+  }
+
+  /* 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)++) {
+     if(SiS_Pr->SiS_RefIndex[RefreshRateTableIndex + (*i)].ModeID != tempbx) {
+     	return(0);
+     }
+     infoflag = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex + (*i)].Ext_InfoFlag;
+     if(infoflag & tempax) return(1);
+  }
+  return(1);
 }
 
-/* Set Part1 registers */
-void
-SiS_SetGroup1(SiS_Private *SiS_Pr,USHORT BaseAddr,UCHAR *ROMAddr,USHORT ModeNo,
-              USHORT ModeIdIndex,PSIS_HW_DEVICE_INFO HwDeviceExtension,
-	      USHORT RefreshRateTableIndex)
+/*********************************************/
+/*              Get rate pointer             */
+/*********************************************/
+
+USHORT
+SiS_GetRatePtr(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,
+               PSIS_HW_INFO HwInfo)
 {
-  USHORT  temp=0, tempax=0, tempbx=0, tempcx=0;
-  USHORT  pushbx=0, CRT1Index=0;
-#ifdef SIS315H
-  USHORT  tempbl=0;
-#endif
-  USHORT  modeflag, resinfo=0;
+  SHORT  LCDRefreshIndex[] = { 0x00, 0x00, 0x01, 0x01,
+                               0x01, 0x01, 0x01, 0x01,
+			       0x01, 0x01, 0x01, 0x01,
+			       0x01, 0x01, 0x01, 0x01,
+			       0x00, 0x00, 0x00, 0x00 };
+  USHORT RefreshRateTableIndex,i,backup_i;
+  USHORT modeflag,index,temp,backupindex;
 
-  if(ModeNo <= 0x13) {
+  /* 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 {
-     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;
+  else
+     modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
+
+  if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+     	if(modeflag & HalfDCLK) return(0);
      }
   }
 
-  if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
-#ifdef SIS315H
-     SiS_SetCRT2Sync(SiS_Pr,BaseAddr,ROMAddr,ModeNo,
-                     RefreshRateTableIndex,HwDeviceExtension);
+  if(ModeNo < 0x14) return(0xFFFF);
 
-     SiS_SetGroup1_LCDA(SiS_Pr,BaseAddr,ROMAddr,ModeNo,ModeIdIndex,
-                        HwDeviceExtension,RefreshRateTableIndex);
-#endif
-  } else {
+  /* CR33 holds refresh rate index for CRT1 [3:0] and CRT2 [7:4]. */
 
-     if( (HwDeviceExtension->jChipType >= SIS_315H) &&
-         (SiS_Pr->SiS_IF_DEF_LVDS == 1) &&
-	 (SiS_Pr->SiS_VBInfo & SetInSlaveMode) ) {
+  index = (SiS_GetReg(SiS_Pr->SiS_P3d4,0x33) >> SiS_Pr->SiS_SelectCRT2Rate) & 0x0F;
+  backupindex = index;
 
-        SiS_SetCRT2Sync(SiS_Pr,BaseAddr,ROMAddr,ModeNo,
-                        RefreshRateTableIndex,HwDeviceExtension);
+  if(index > 0) index--;
 
+  if(SiS_Pr->SiS_SetFlag & ProgrammingCRT2) {
+     if(SiS_Pr->SiS_VBType & VB_SISVB) {
+        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_VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
+	   if(!(SiS_Pr->SiS_VBType & VB_NoLCD)) {
+              temp = LCDRefreshIndex[SiS_Pr->SiS_LCDResInfo];
+              if(index > temp) index = temp;
+	   }
+	}
      } else {
+        if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) index = 0;
+	if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
+           if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) index = 0;
+        }
+     }
+  }
 
-        SiS_SetCRT2Offset(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,
-      		          RefreshRateTableIndex,HwDeviceExtension);
+  RefreshRateTableIndex = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].REFindex;
+  ModeNo = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].ModeID;
 
-        if (HwDeviceExtension->jChipType < SIS_315H ) {
-#ifdef SIS300
-    	      SiS_SetCRT2FIFO_300(SiS_Pr,ROMAddr,ModeNo,HwDeviceExtension);
-#endif
-        } else {
-#ifdef SIS315H
-              SiS_SetCRT2FIFO_310(SiS_Pr,ROMAddr,ModeNo,HwDeviceExtension);
-#endif
-	}
+  if(HwInfo->jChipType >= SIS_315H) {
+     if(!(SiS_Pr->SiS_VBInfo & DriverMode)) {
+        if( (SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_VESAID == 0x105) ||
+            (SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_VESAID == 0x107) ) {
+           if(backupindex <= 1) RefreshRateTableIndex++;
+        }
+     }
+  }
 
-        SiS_SetCRT2Sync(SiS_Pr,BaseAddr,ROMAddr,ModeNo,
-                        RefreshRateTableIndex,HwDeviceExtension);
+  i = 0;
+  do {
+     if(SiS_Pr->SiS_RefIndex[RefreshRateTableIndex + i].ModeID != ModeNo) break;
+     temp = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex + i].Ext_InfoFlag;
+     temp &= ModeInfoFlag;
+     if(temp < SiS_Pr->SiS_ModeType) break;
+     i++;
+     index--;
+  } while(index != 0xFFFF);
 
-	/* 1. Horizontal setup */
+  if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToRAMDAC)) {
+     if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
+      	temp = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex + i - 1].Ext_InfoFlag;
+      	if(temp & InterlaceMode) i++;
+     }
+  }
 
-        if (HwDeviceExtension->jChipType < SIS_315H ) {
+  i--;
 
-#ifdef SIS300   /* ------------- 300 series --------------*/
+  if((SiS_Pr->SiS_SetFlag & ProgrammingCRT2) && (!(SiS_Pr->SiS_VBInfo & DisableCRT2Display))) {
+     backup_i = i;
+     if(!(SiS_AdjustCRT2Rate(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex, &i, HwInfo))) {
+	i = backup_i;
+     }
+  }
 
-    		temp = (SiS_Pr->SiS_VGAHT - 1) & 0x0FF;   			/* BTVGA2HT 0x08,0x09 */
-    		SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x08,temp);                   /* CRT2 Horizontal Total */
+  return(RefreshRateTableIndex + i);
+}
 
-    		temp = (((SiS_Pr->SiS_VGAHT - 1) & 0xFF00) >> 8) << 4;
-    		SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x09,0x0f,temp);          /* CRT2 Horizontal Total Overflow [7:4] */
+/*********************************************/
+/*            STORE CRT2 INFO in CR34        */
+/*********************************************/
 
-    		temp = (SiS_Pr->SiS_VGAHDE + 12) & 0x0FF;                       /* BTVGA2HDEE 0x0A,0x0C */
-    		SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0A,temp);                   /* CRT2 Horizontal Display Enable End */
+static void
+SiS_SaveCRT2Info(SiS_Private *SiS_Pr, USHORT ModeNo)
+{
+  USHORT temp1,temp2;
 
-    		pushbx = SiS_Pr->SiS_VGAHDE + 12;                               /* bx  BTVGA@HRS 0x0B,0x0C */
-    		tempcx = (SiS_Pr->SiS_VGAHT - SiS_Pr->SiS_VGAHDE) >> 2;
-    		tempbx = pushbx + tempcx;
-    		tempcx <<= 1;
-    		tempcx += tempbx;
+  /* Store CRT1 ModeNo in CR34 */
+  SiS_SetReg(SiS_Pr->SiS_P3d4,0x34,ModeNo);
+  temp1 = (SiS_Pr->SiS_VBInfo & SetInSlaveMode) >> 8;
+  temp2 = ~(SetInSlaveMode >> 8);
+  SiS_SetRegANDOR(SiS_Pr->SiS_P3d4,0x31,temp2,temp1);
+}
 
-    		if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
+/*********************************************/
+/*    HELPER: GET SOME DATA FROM BIOS ROM    */
+/*********************************************/
 
-		   if(SiS_Pr->UseCustomMode) {
-		      tempbx = SiS_Pr->CHSyncStart + 12;
-		      tempcx = SiS_Pr->CHSyncEnd + 12;
-		   }
+static BOOLEAN
+SiS_CR36BIOSWord23b(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
+{
+  USHORT temp,temp1;
+  UCHAR *ROMAddr;
 
-      		   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((ROMAddr = (UCHAR *)HwInfo->pjVirtualRomBase) && SiS_Pr->SiS_UseROM) {
+     if((ROMAddr[0x233] == 0x12) && (ROMAddr[0x234] == 0x34)) {
+        temp = 1 << ((SiS_GetReg(SiS_Pr->SiS_P3d4,0x36) & 0xff) >> 4);
+        temp1 = (ROMAddr[0x23c] << 8) | ROMAddr[0x23b];
+        if(temp1 & temp) return(1);
+     }
+  }
+  return(0);
+}
 
-    		   if((SiS_Pr->SiS_VBInfo & SetCRT2ToTV) && (resinfo == SIS_RI_1024x768)){
-        	      if(!(SiS_Pr->SiS_VBInfo & SetPALTV)){
-      			 tempbx = 1040;
-      			 tempcx = 1042;
-      		      }
-    		   }
-	        }
+static BOOLEAN
+SiS_CR36BIOSWord23d(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
+{
+  USHORT temp,temp1;
+  UCHAR *ROMAddr;
 
-    		temp = tempbx & 0x00FF;
-    		SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0B,temp);                   /* CRT2 Horizontal Retrace Start */
-#endif /* SIS300 */
+  if((ROMAddr = (UCHAR *)HwInfo->pjVirtualRomBase) && SiS_Pr->SiS_UseROM) {
+     if((ROMAddr[0x233] == 0x12) && (ROMAddr[0x234] == 0x34)) {
+        temp = 1 << ((SiS_GetReg(SiS_Pr->SiS_P3d4,0x36) & 0xff) >> 4);
+        temp1 = (ROMAddr[0x23e] << 8) | ROMAddr[0x23d];
+        if(temp1 & temp) return(1);
+     }
+  }
+  return(0);
+}
 
- 	} else {
+/*********************************************/
+/*          HELPER: DELAY FUNCTIONS          */
+/*********************************************/
 
-#ifdef SIS315H  /* ------------------- 315/330 series --------------- */
+void
+SiS_DDC2Delay(SiS_Private *SiS_Pr, USHORT delaytime)
+{
+  USHORT i, j;
 
-	        tempcx = SiS_Pr->SiS_VGAHT;				       /* BTVGA2HT 0x08,0x09 */
-		if(modeflag & HalfDCLK) {
-		    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;
-			  }
-		    } else {
-			  tempcx >>= 1;
-		    }
-		}
-		tempcx--;
+  for(i=0; i<delaytime; i++) {
+     j += SiS_GetReg(SiS_Pr->SiS_P3c4,0x05);
+  }
+}
 
-		temp = tempcx & 0xff;
-		SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x08,temp);                  /* CRT2 Horizontal Total */
+static void
+SiS_GenericDelay(SiS_Private *SiS_Pr, USHORT delay)
+{
+  USHORT temp,flag;
 
-		temp = ((tempcx & 0xff00) >> 8) << 4;
-		SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x09,0x0F,temp);         /* CRT2 Horizontal Total Overflow [7:4] */
+  flag = SiS_GetRegByte(0x61) & 0x10;
 
-		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 += 16;
+  while(delay) {
+     temp = SiS_GetRegByte(0x61) & 0x10;
+     if(temp == flag) continue;
+     flag = temp;
+     delay--;
+  }
+}
 
-		temp = tempbx & 0xff;
-		SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0A,temp);                  /* CRT2 Horizontal Display Enable End */
+#ifdef SIS315H
+static void
+SiS_LongDelay(SiS_Private *SiS_Pr, USHORT delay)
+{
+  while(delay--) {
+     SiS_GenericDelay(SiS_Pr,0x19df);
+  }
+}
+#endif
 
-		pushbx = tempbx;
-		tempcx >>= 1;
-		tempbx += tempcx;
-		tempcx += tempbx;
+static void
+SiS_ShortDelay(SiS_Private *SiS_Pr, USHORT delay)
+{
+  while(delay--) {
+     SiS_GenericDelay(SiS_Pr,0x42);
+  }
+}
 
-		if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
+static void
+SiS_PanelDelay(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo, USHORT DelayTime)
+{
+  UCHAR  *ROMAddr = HwInfo->pjVirtualRomBase;
+  USHORT PanelID, DelayIndex, Delay=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(HwInfo->jChipType < SIS_315H) {
 
-             	   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)) - 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) {
-		      if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) {
-		         if(resinfo == SIS_RI_1024x768) {
-      		 	    tempbx = 1040;
-      		 	    tempcx = 1042;
-      	     	         }
-		      }
-         	   }
-#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;
-      		 	    tempcx = 1042;
-      	     	         }
-		      }
-         	   }
-#endif
-                }
+#ifdef SIS300
 
-		temp = tempbx & 0xff;
-	 	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0B,temp);                 /* CRT2 Horizontal Retrace Start */
-#endif  /* SIS315H */
+      PanelID = SiS_GetReg(SiS_Pr->SiS_P3d4,0x36);
+      if(SiS_Pr->SiS_VBType & VB_SISVB) {
+         if(!(SiS_GetReg(SiS_Pr->SiS_P3c4,0x18) & 0x10)) PanelID = 0x12;
+      }
+      DelayIndex = PanelID >> 4;
+      if((DelayTime >= 2) && ((PanelID & 0x0f) == 1))  {
+         Delay = 3;
+      } else {
+         if(DelayTime >= 2) DelayTime -= 2;
 
-     	}  /* 315/330 series */
+         if(!(DelayTime & 0x01)) {
+       	    Delay = SiS_Pr->SiS_PanelDelayTbl[DelayIndex].timer[0];
+         } else {
+       	    Delay = SiS_Pr->SiS_PanelDelayTbl[DelayIndex].timer[1];
+         }
+	 if((ROMAddr) && (SiS_Pr->SiS_UseROM)) {
+            if(ROMAddr[0x220] & 0x40) {
+               if(!(DelayTime & 0x01)) {
+	          Delay = (USHORT)ROMAddr[0x225];
+               } else {
+	    	  Delay = (USHORT)ROMAddr[0x226];
+               }
+            }
+         }
+      }
+      SiS_ShortDelay(SiS_Pr,Delay);
 
-  	/* The following is done for all bridge/chip types/series */
+#endif  /* SIS300 */
 
-  	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);                        /* Overflow */
+   } else {
 
-  	temp = tempcx & 0x00FF;
-  	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0D,temp);                        /* CRT2 Horizontal Retrace End */
+#ifdef SIS315H
 
-  	/* 2. Vertical setup */
+      if(HwInfo->jChipType >= SIS_330) return;
 
-  	tempcx = SiS_Pr->SiS_VGAVT - 1;
-  	temp = tempcx & 0x00FF;
+      if((SiS_Pr->SiS_IF_DEF_LVDS == 1) ||
+         (SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) ||
+	 (SiS_Pr->SiS_CustomT == CUT_CLEVO1400)) {			/* 315 series, LVDS; Special */
+
+         if(SiS_Pr->SiS_IF_DEF_CH70xx == 0) {
+            PanelID = SiS_GetReg(SiS_Pr->SiS_P3d4,0x36);
+	    if(SiS_Pr->SiS_CustomT == CUT_CLEVO1400) {
+	       if(!(SiS_GetReg(SiS_Pr->SiS_P3c4,0x1b) & 0x10)) PanelID = 0x12;
+	    }
+	    if(SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) {
+	       DelayIndex = PanelID & 0x0f;
+	    } else {
+	       DelayIndex = PanelID >> 4;
+	    }
+	    if((DelayTime >= 2) && ((PanelID & 0x0f) == 1))  {
+               Delay = 3;
+            } else {
+               if(DelayTime >= 2) DelayTime -= 2;
+               if(!(DelayTime & 0x01)) {
+       		  Delay = SiS_Pr->SiS_PanelDelayTblLVDS[DelayIndex].timer[0];
+               } else {
+       		  Delay = SiS_Pr->SiS_PanelDelayTblLVDS[DelayIndex].timer[1];
+               }
+	       if((ROMAddr) && (SiS_Pr->SiS_UseROM)) {
+                  if(ROMAddr[0x13c] & 0x40) {
+                     if(!(DelayTime & 0x01)) {
+	    	        Delay = (USHORT)ROMAddr[0x17e];
+                     } else {
+	    	        Delay = (USHORT)ROMAddr[0x17f];
+                     }
+                  }
+               }
+            }
+	    SiS_ShortDelay(SiS_Pr,Delay);
+	 }
 
-        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--;
-           }
-        } else if(HwDeviceExtension->jChipType >= SIS_315H) {
-	   /* 650/30xLV 1.10.6s */
-	   temp--;
-	}
-  	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0E,temp);                        /* CRT2 Vertical Total */
+      } else if(SiS_Pr->SiS_VBType & VB_SISVB) {			/* 315 series, all bridges */
 
-  	tempbx = SiS_Pr->SiS_VGAVDE - 1;
-  	temp = tempbx & 0x00FF;
-  	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0F,temp);                        /* CRT2 Vertical Display Enable End */
+	 DelayIndex = SiS_GetReg(SiS_Pr->SiS_P3d4,0x36) >> 4;
+         if(!(DelayTime & 0x01)) {
+       	    Delay = SiS_Pr->SiS_PanelDelayTbl[DelayIndex].timer[0];
+         } else {
+       	    Delay = SiS_Pr->SiS_PanelDelayTbl[DelayIndex].timer[1];
+         }
+	 Delay <<= 2;
+	 if(SiS_Pr->SiS_VBType & (VB_SIS302LV | VB_SIS302ELV)) {
+	    Delay <<= 5;
+	 }
+	 SiS_DDC2Delay(SiS_Pr, Delay);
 
-  	temp = ((tempbx & 0xFF00) << 3) >> 8;
-  	temp |= ((tempcx & 0xFF00) >> 8);
-  	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x12,temp);                        /* Overflow (and HWCursor Test Mode) */
+      }
 
-	/* 650/LVDS (1.10.07), 650/30xLV (1.10.6s) */
-	if(HwDeviceExtension->jChipType >= SIS_315H) {
-           tempbx++;
-   	   tempax = tempbx;
-	   tempcx++;
-	   tempcx -= tempax;
-	   tempcx >>= 2;
-	   tempbx += tempcx;
-	   if(tempcx < 4) tempcx = 4;
-	   tempcx >>= 2;
-	   tempcx += tempbx;
-	   tempcx++;
-	} else {
-	   /* 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        */
-	}
+#endif /* SIS315H */
 
-  	if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
+   }
+}
 
-	   if(SiS_Pr->UseCustomMode) {
-	      tempbx = SiS_Pr->CVSyncStart;
-	      tempcx = (tempcx & 0xFF00) | (SiS_Pr->CVSyncEnd & 0x00FF);
-	   }
+#ifdef SIS315H
+static void
+SiS_PanelDelayLoop(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,
+                      USHORT DelayTime, USHORT DelayLoop)
+{
+   int i;
+   for(i=0; i<DelayLoop; i++) {
+      SiS_PanelDelay(SiS_Pr, HwInfo, DelayTime);
+   }
+}
+#endif
 
-    	   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);           /* CRT2 Vertical Retrace Start */
+/*********************************************/
+/*    HELPER: WAIT-FOR-RETRACE FUNCTIONS     */
+/*********************************************/
 
-  	temp = ((tempbx & 0xFF00) >> 8) << 4;
-  	temp |= (tempcx & 0x000F);
-  	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x11,temp);           /* CRT2 Vert. Retrace End; Overflow; "Enable CRTC Check" */
+void
+SiS_WaitRetrace1(SiS_Private *SiS_Pr)
+{
+  USHORT watchdog;
 
-  	/* 3. Panel compensation delay */
+  if(SiS_GetReg(SiS_Pr->SiS_P3c4,0x1f) & 0xc0) return;
 
-  	if(HwDeviceExtension->jChipType < SIS_315H) {
+  if(!(SiS_GetReg(SiS_Pr->SiS_P3d4,0x17) & 0x80)) return;
 
-#ifdef SIS300  /* ---------- 300 series -------------- */
+  watchdog = 65535;
+  while((SiS_GetRegByte(SiS_Pr->SiS_P3da) & 0x08) && --watchdog);
+  watchdog = 65535;
+  while((!(SiS_GetRegByte(SiS_Pr->SiS_P3da) & 0x08)) && --watchdog);
+}
 
-	   if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
-	        temp = 0x20;
+static void
+SiS_WaitRetrace2(SiS_Private *SiS_Pr, USHORT reg)
+{
+  USHORT watchdog;
 
-		if(HwDeviceExtension->jChipType == SIS_300) {
-		   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_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)) {
-		    if(ROMAddr[0x220] & 0x80) {
-		        if(SiS_Pr->SiS_VBInfo & (SetCRT2ToTV-SetCRT2ToHiVisionTV))
-				temp = ROMAddr[0x221];
-			else if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV)
-				temp = ROMAddr[0x222];
-		        else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024)
-				temp = ROMAddr[0x223];
-			else
-				temp = ROMAddr[0x224];
-			temp &= 0x3c;
-		    }
-		}
-		if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
-		   if(HwDeviceExtension->pdc) {
-			temp = HwDeviceExtension->pdc & 0x3c;
-		   }
-		}
-	   } else {
-	        temp = 0x20;
-		if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToTV)) {
-		   if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480) temp = 0x04;
-		}
-		if((ROMAddr) && SiS_Pr->SiS_UseROM) {
-		    if(ROMAddr[0x220] & 0x80) {
-		        temp = ROMAddr[0x220] & 0x3c;
-		    }
-		}
-		if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
-		   if(HwDeviceExtension->pdc) {
-		      temp = HwDeviceExtension->pdc & 0x3c;
-		   }
-		}
-	   }
+  watchdog = 65535;
+  while((SiS_GetReg(SiS_Pr->SiS_Part1Port,reg) & 0x02) && --watchdog);
+  watchdog = 65535;
+  while((!(SiS_GetReg(SiS_Pr->SiS_Part1Port,reg) & 0x02)) && --watchdog);
+}
 
-    	   SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x13,~0x03C,temp);         /* Panel Link Delay Compensation; (Software Command Reset; Power Saving) */
+static void
+SiS_WaitVBRetrace(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
+{
+  if(HwInfo->jChipType < SIS_315H) {
+#ifdef SIS300
+     if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
+        if(!(SiS_GetReg(SiS_Pr->SiS_Part1Port,0x00) & 0x20)) return;
+     }
+     if(!(SiS_GetReg(SiS_Pr->SiS_Part1Port,0x00) & 0x80)) {
+        SiS_WaitRetrace1(SiS_Pr);
+     } else {
+        SiS_WaitRetrace2(SiS_Pr, 0x25);
+     }
+#endif
+  } else {
+#ifdef SIS315H
+     if(!(SiS_GetReg(SiS_Pr->SiS_Part1Port,0x00) & 0x40)) {
+        SiS_WaitRetrace1(SiS_Pr);
+     } else {
+        SiS_WaitRetrace2(SiS_Pr, 0x30);
+     }
+#endif
+  }
+}
 
-#endif  /* SIS300 */
+static void
+SiS_VBWait(SiS_Private *SiS_Pr)
+{
+  USHORT tempal,temp,i,j;
 
-  	} else {
+  temp = 0;
+  for(i=0; i<3; i++) {
+    for(j=0; j<100; j++) {
+       tempal = SiS_GetRegByte(SiS_Pr->SiS_P3da);
+       if(temp & 0x01) {
+          if((tempal & 0x08))  continue;
+          if(!(tempal & 0x08)) break;
+       } else {
+          if(!(tempal & 0x08)) continue;
+          if((tempal & 0x08))  break;
+       }
+    }
+    temp ^= 0x01;
+  }
+}
 
-#ifdef SIS315H   /* --------------- 315/330 series ---------------*/
+static void
+SiS_VBLongWait(SiS_Private *SiS_Pr)
+{
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+     SiS_VBWait(SiS_Pr);
+  } else {
+     SiS_WaitRetrace1(SiS_Pr);
+  }
+}
 
-	   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) {
-		      switch(SiS_Pr->SiS_HiVision) {
-		      case 2:
-		      case 1:
-		      case 0:
-		         temp = 0x08;
-			 break;
-		      default:
-      		         if(SiS_Pr->SiS_VBInfo & SetInSlaveMode)  temp = 0x2c;
-      		         else 					  temp = 0x20;
-		      }
-    	           }
-		}
-		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) {
-		         if(ROMAddr[0x13c] & 0x80) tempbl = 0xf0;
-		      } else {
-		         if(ROMAddr[0x1bc] & 0x80) tempbl = 0xf0;
-		      }
-		   }
-		} else {  /* LV (550/301LV checks ROM byte, other LV BIOSes do not) */
-		   tempbl = 0xF0;
-		}
-		if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD|SetCRT2ToLCDA)) {
-		   if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
-		      if(HwDeviceExtension->pdc) {
-		         temp = HwDeviceExtension->pdc;
-		         tempbl = 0;
-		      }
-		   }
-		}
-	   } else {
-	        if(HwDeviceExtension->jChipType == SIS_740) {
-		   temp = 0x03;
-	        } else {
-		   temp = 0x00;
-		}
-		if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) temp = 0x0a;
-		tempbl = 0xF0;
-		if(HwDeviceExtension->jChipType == SIS_650) {
-		   if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
-		      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);	    /* Panel Link Delay Compensation */
+/*********************************************/
+/*               HELPER: MISC                */
+/*********************************************/
 
-    	   tempax = 0;
-    	   if (modeflag & DoubleScanMode) tempax |= 0x80;
-    	   if (modeflag & HalfDCLK)       tempax |= 0x40;
-    	   SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2C,0x3f,tempax);
+static BOOLEAN
+SiS_Is301B(SiS_Private *SiS_Pr)
+{
+  USHORT flag;
 
-#endif  /* SIS315H */
+  flag = SiS_GetReg(SiS_Pr->SiS_Part4Port,0x01);
+  if(flag >= 0xb0) return TRUE;
+  return FALSE;
+}
 
-  	}
+static BOOLEAN
+SiS_CRT2IsLCD(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
+{
+  USHORT flag;
 
-     }  /* Slavemode */
+  if(HwInfo->jChipType == SIS_730) {
+     flag = SiS_GetReg(SiS_Pr->SiS_P3c4,0x13);
+     if(flag & 0x20) return TRUE;
+  }
+  flag = SiS_GetReg(SiS_Pr->SiS_P3d4,0x30);
+  if(flag & 0x20) return TRUE;
+  return FALSE;
+}
 
-     if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
+BOOLEAN
+SiS_IsDualEdge(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
+{
+#ifdef SIS315H
+  USHORT flag;
 
-        /* For 301BDH with LCD, we set up the Panel Link */
-        if( (SiS_Pr->SiS_VBType & VB_NoLCD) && (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) ) {
+  if(HwInfo->jChipType >= SIS_315H) {
+     if((HwInfo->jChipType != SIS_650) || (SiS_GetReg(SiS_Pr->SiS_P3d4,0x5f) & 0xf0)) {
+        flag = SiS_GetReg(SiS_Pr->SiS_P3d4,0x38);
+        if(flag & EnableDualEdge) return TRUE;
+     }
+  }
+#endif
+  return FALSE;
+}
 
-	    SiS_SetGroup1_LVDS(SiS_Pr,BaseAddr,ROMAddr,ModeNo,ModeIdIndex,
-	                       HwDeviceExtension,RefreshRateTableIndex);
+BOOLEAN
+SiS_IsVAMode(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
+{
+#ifdef SIS315H
+  USHORT flag;
 
-        } else if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
+  if(HwInfo->jChipType >= SIS_315H) {
+     flag = SiS_GetReg(SiS_Pr->SiS_P3d4,0x38);
+     if((flag & EnableDualEdge) && (flag & SetToLCDA)) return TRUE;
+  }
+#endif
+  return FALSE;
+}
 
-    	    SiS_SetGroup1_301(SiS_Pr,BaseAddr,ROMAddr,ModeNo,ModeIdIndex,
-	                      HwDeviceExtension,RefreshRateTableIndex);
-        }
+static BOOLEAN
+SiS_IsDualLink(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
+{
+#ifdef SIS315H
+  if(HwInfo->jChipType >= SIS_315H) {
+     if((SiS_CRT2IsLCD(SiS_Pr, HwInfo)) ||
+        (SiS_IsVAMode(SiS_Pr, HwInfo))) {
+        if(SiS_Pr->SiS_LCDInfo & LCDDualLink) return TRUE;
+     }
+  }
+#endif
+  return FALSE;
+}
 
-     } else {
+#ifdef SIS315H
+static BOOLEAN
+SiS_WeHaveBacklightCtrl(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
+{
+  USHORT flag;
 
-        if(HwDeviceExtension->jChipType < SIS_315H) {
-	
-	   SiS_SetGroup1_LVDS(SiS_Pr,BaseAddr,ROMAddr,ModeNo,ModeIdIndex,
-	                        HwDeviceExtension,RefreshRateTableIndex);
-	} else {
-	
-	   if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
-              if((!(SiS_Pr->SiS_VBInfo & SetCRT2ToTV)) || (SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
-    	          SiS_SetGroup1_LVDS(SiS_Pr,BaseAddr,ROMAddr,ModeNo,ModeIdIndex,
-	                              HwDeviceExtension,RefreshRateTableIndex);
-              }
-	   } else {
-	      SiS_SetGroup1_LVDS(SiS_Pr,BaseAddr,ROMAddr,ModeNo,ModeIdIndex,
-	                         HwDeviceExtension,RefreshRateTableIndex);
-	   }
-	
-	}
+  if((HwInfo->jChipType >= SIS_315H) && (HwInfo->jChipType < SIS_661)) {
+     flag = SiS_GetReg(SiS_Pr->SiS_P3d4,0x79);
+     if(flag & 0x10) return TRUE;
+  }
+  return FALSE;
+}
+#endif
 
-     }
-   } /* LCDA */
+#ifdef SIS315H
+static BOOLEAN
+SiS_IsNotM650orLater(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
+{
+  USHORT flag;
+
+  if(HwInfo->jChipType == SIS_650) {
+     flag = SiS_GetReg(SiS_Pr->SiS_P3d4,0x5f);
+     flag &= 0xF0;
+     /* Check for revision != A0 only */
+     if((flag == 0xe0) || (flag == 0xc0) ||
+        (flag == 0xb0) || (flag == 0x90)) return FALSE;
+  } else if(HwInfo->jChipType >= SIS_661) return FALSE;
+  return TRUE;
 }
+#endif
 
-void
-SiS_SetGroup1_301(SiS_Private *SiS_Pr, USHORT  BaseAddr,UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex,
-                  PSIS_HW_DEVICE_INFO HwDeviceExtension,USHORT RefreshRateTableIndex)
+#ifdef SIS315H
+static BOOLEAN
+SiS_IsYPbPr(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
 {
-  USHORT  push1,push2;
-  USHORT  tempax,tempbx,tempcx,temp;
-  USHORT  resinfo,modeflag;
-  unsigned char p1_7, p1_8;
+  USHORT flag;
 
-  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(HwInfo->jChipType >= SIS_315H) {
+     flag = SiS_GetReg(SiS_Pr->SiS_P3d4,0x38);
+     if(flag & EnableCHYPbPr) return TRUE;  /* = YPrPb = 0x08 */
   }
+  return FALSE;
+}
+#endif
 
-  /* The following is only done if bridge is in slave mode: */
+#ifdef SIS315H
+static BOOLEAN
+SiS_IsChScart(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
+{
+  USHORT flag;
 
-  tempax = 0xFFFF;
-  if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToTV))  tempax = SiS_GetVGAHT2(SiS_Pr);
+  if(HwInfo->jChipType >= SIS_315H) {
+     flag = SiS_GetReg(SiS_Pr->SiS_P3d4,0x38);
+     if(flag & EnableCHScart) return TRUE;  /* = Scart = 0x04 */
+  }
+  return FALSE;
+}
+#endif
 
-  if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV)  modeflag |= Charx8Dot;
+#ifdef SIS315H
+static BOOLEAN
+SiS_IsTVOrYPbPrOrScart(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
+{
+  USHORT flag;
 
-  if(modeflag & Charx8Dot) tempcx = 0x08;
-  else                     tempcx = 0x09;
+  if(HwInfo->jChipType >= SIS_315H) {
+     flag = SiS_GetReg(SiS_Pr->SiS_P3d4,0x30);
+     if(flag & SetCRT2ToTV)        return TRUE;
+     flag = SiS_GetReg(SiS_Pr->SiS_P3d4,0x38);
+     if(flag & EnableCHYPbPr)      return TRUE;  /* = YPrPb = 0x08 */
+     if(flag & EnableCHScart)      return TRUE;  /* = Scart = 0x04 - TW */
+  } else {
+     flag = SiS_GetReg(SiS_Pr->SiS_P3d4,0x30);
+     if(flag & SetCRT2ToTV)        return TRUE;
+  }
+  return FALSE;
+}
+#endif
 
-  if(tempax >= SiS_Pr->SiS_VGAHT) tempax = SiS_Pr->SiS_VGAHT;
+#ifdef SIS315H
+static BOOLEAN
+SiS_IsLCDOrLCDA(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
+{
+  USHORT flag;
 
-  if(modeflag & HalfDCLK) tempax >>= 1;
+  if(HwInfo->jChipType >= SIS_315H) {
+     flag = SiS_GetReg(SiS_Pr->SiS_P3d4,0x30);
+     if(flag & SetCRT2ToLCD) return TRUE;
+     flag = SiS_GetReg(SiS_Pr->SiS_P3d4,0x38);
+     if(flag & SetToLCDA)    return TRUE;
+  } else {
+     flag = SiS_GetReg(SiS_Pr->SiS_P3d4,0x30);
+     if(flag & SetCRT2ToLCD) return TRUE;
+  }
+  return FALSE;
+}
+#endif
 
-  tempax = (tempax / tempcx) - 5;
-  tempbx = tempax & 0x00FF;
+static BOOLEAN
+SiS_BridgeIsOn(SiS_Private *SiS_Pr)
+{
+  USHORT flag;
 
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x03,0xff);                 /* set MAX HT */
+  if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
+     return FALSE;
+  } else if(SiS_Pr->SiS_VBType & VB_SISVB) {
+     flag = SiS_GetReg(SiS_Pr->SiS_Part4Port,0x00);
+     if((flag == 1) || (flag == 2)) return FALSE;
+  }
+  return TRUE;
+}
 
-  tempax = SiS_Pr->SiS_VGAHDE;                                 	/* 0x04 Horizontal Display End */
-  if(modeflag & HalfDCLK) tempax >>= 1;
-  tempax = (tempax / tempcx) - 1;
-  tempbx |= ((tempax & 0x00FF) << 8);
-  temp = tempax & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x04,temp);
+static BOOLEAN
+SiS_BridgeIsEnabled(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
+{
+  USHORT flag;
 
-  temp = (tempbx & 0xFF00) >> 8;
-  if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
-     if(!(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV)) {
-        temp += 2;
-     }
-  }
-  if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) {
-     if(SiS_Pr->SiS_HiVision == 3) {
-        if(resinfo == SIS_RI_800x600) temp -= 2;
+  if(!(SiS_BridgeIsOn(SiS_Pr))) {
+     flag = SiS_GetReg(SiS_Pr->SiS_Part1Port,0x00);
+     if(HwInfo->jChipType < SIS_315H) {
+       flag &= 0xa0;
+       if((flag == 0x80) || (flag == 0x20)) return FALSE;
+     } else {
+       flag &= 0x50;
+       if((flag == 0x40) || (flag == 0x10)) return FALSE;
      }
   }
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x05,temp);                 /* 0x05 Horizontal Display Start */
+  return TRUE;
+}
 
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x06,0x03);                 /* 0x06 Horizontal Blank end     */
+static BOOLEAN
+SiS_BridgeInSlave(SiS_Private *SiS_Pr)
+{
+  USHORT flag1;
 
-  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;
-        }
-     }
+  flag1 = SiS_GetReg(SiS_Pr->SiS_P3d4,0x31);
+  if(flag1 & (SetInSlaveMode >> 8)) return TRUE;
+  return FALSE;
+}
+
+/*********************************************/
+/*       GET VIDEO BRIDGE CONFIG INFO        */
+/*********************************************/
+
+/* Setup general purpose IO for Chrontel communication */
+void
+SiS_SetChrontelGPIO(SiS_Private *SiS_Pr, USHORT myvbinfo)
+{
+   unsigned long  acpibase;
+   unsigned short temp;
+
+   if(!(SiS_Pr->SiS_ChSW)) return;
+
+#ifndef LINUX_XF86
+   SiS_SetRegLong(0xcf8,0x80000874);		   /* get ACPI base */
+   acpibase = SiS_GetRegLong(0xcfc);
+#else
+   acpibase = pciReadLong(0x00000800, 0x74);
+#endif
+   acpibase &= 0xFFFF;
+   temp = SiS_GetRegShort((USHORT)(acpibase + 0x3c));  /* ACPI register 0x3c: GP Event 1 I/O mode select */
+   temp &= 0xFEFF;
+   SiS_SetRegShort((USHORT)(acpibase + 0x3c), temp);
+   temp = SiS_GetRegShort((USHORT)(acpibase + 0x3c));
+   temp = SiS_GetRegShort((USHORT)(acpibase + 0x3a));  /* ACPI register 0x3a: GP Pin Level (low/high) */
+   temp &= 0xFEFF;
+   if(!(myvbinfo & SetCRT2ToTV)) temp |= 0x0100;
+   SiS_SetRegShort((USHORT)(acpibase + 0x3a), temp);
+   temp = SiS_GetRegShort((USHORT)(acpibase + 0x3a));
+}
+
+void
+SiS_GetVBInfo(SiS_Private *SiS_Pr, USHORT ModeNo,
+              USHORT ModeIdIndex,PSIS_HW_INFO HwInfo,
+	      int checkcrt2mode)
+{
+  USHORT tempax,tempbx,temp;
+  USHORT modeflag, resinfo=0;
+
+  if(ModeNo <= 0x13) {
+     modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
   } 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;
-              }
-           }
-        }
+     if(SiS_Pr->UseCustomMode) {
+        modeflag = SiS_Pr->CModeFlag;
      } 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;
-                 }
-              }
-           }
-        }
+   	modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
+	resinfo = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO;
      }
   }
 
-  p1_7 = temp;
-  p1_8 = 0x00;
+  SiS_Pr->SiS_SetFlag = 0;
 
-  if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
-     if(SiS_Pr->SiS_SetFlag & TVSimuMode) {
-        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;
+  SiS_Pr->SiS_ModeType = modeflag & ModeInfoFlag;
+
+  tempbx = 0;
+  if(SiS_BridgeIsOn(SiS_Pr) == 0) {
+    	temp = SiS_GetReg(SiS_Pr->SiS_P3d4,0x30);
+#if 0
+   	if(HwInfo->jChipType < SIS_661) {
+	   if(SiS_Pr->SiS_YPbPr & YPbPrModeMask) {
+	      temp &= (SetCRT2ToHiVision | SwitchCRT2 | SetSimuScanMode); 	/* 0x83 */
+	      temp |= SetCRT2ToHiVision;   					/* 0x80 */
+   	   }
+	   if(SiS_Pr->SiS_YPbPr & YPbPrSetSVideo) {
+	      temp &= (SetCRT2ToHiVision | SwitchCRT2 | SetSimuScanMode); 	/* 0x83 */
+	      temp |= SetCRT2ToSVIDEO;  					/* 0x08 */
+	   }
+	}
+#endif
+    	tempbx |= temp;
+    	tempax = SiS_GetReg(SiS_Pr->SiS_P3d4,0x31) << 8;
+        tempax &= (DriverMode | LoadDACFlag | SetNotSimuMode | SetPALTV);
+    	tempbx |= tempax;
+
+#ifdef SIS315H
+	if(HwInfo->jChipType >= SIS_315H) {
+    	   if(SiS_Pr->SiS_VBType & (VB_SIS301C|VB_SIS302B|VB_SIS301LV|VB_SIS302LV|VB_SIS302ELV)) {
+	      if(ModeNo == 0x03) {
+	         /* Mode 0x03 is never in driver mode */
+		 SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x31,0xbf);
+	      }
+	      if(!(SiS_GetReg(SiS_Pr->SiS_P3d4,0x31) & (DriverMode >> 8))) {
+	         /* Reset LCDA setting */
+		 SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x38,0xfc);
+	      }
+	      if(IS_SIS650) {
+	         if(SiS_Pr->SiS_UseLCDA) {
+		    if(SiS_GetReg(SiS_Pr->SiS_P3d4,0x5f) & 0xF0) {
+		       if((ModeNo <= 0x13) || (!(SiS_GetReg(SiS_Pr->SiS_P3d4,0x31) & (DriverMode >> 8)))) {
+		          SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x38,(EnableDualEdge | SetToLCDA));
+		       }
+		    }
+		 }
+	      }
+	      temp = SiS_GetReg(SiS_Pr->SiS_P3d4,0x38);
+       	      if((temp & (EnableDualEdge | SetToLCDA)) == (EnableDualEdge | SetToLCDA)) {
+          	 tempbx |= SetCRT2ToLCDA;
+	      }
+	      if(HwInfo->jChipType >= SIS_661) {
+	         if(SiS_Pr->SiS_VBType & (VB_SIS301C|VB_SIS301LV|VB_SIS302LV|VB_SIS302ELV)) {
+	            if(temp & 0x04) {
+		       temp = SiS_GetReg(SiS_Pr->SiS_P3d4,0x35) & 0xe0;
+		       if(temp == 0x60) tempbx |= SetCRT2ToHiVision;
+		       else             tempbx |= SetCRT2ToYPbPr525750;
+		    }
+	         }
+  	      }
+    	   }
+
+	   if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
+	      temp = SiS_GetReg(SiS_Pr->SiS_P3d4,0x38);
+	      if(temp & SetToLCDA) {
+		 tempbx |= SetCRT2ToLCDA;
+	      }
+	      if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
+	         if(temp & EnableCHYPbPr) {
+		    tempbx |= SetCRT2ToCHYPbPr;
+		 }
 	      }
-	   } 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;
+	}
+
+#endif  /* SIS315H */
+
+    	if(SiS_Pr->SiS_VBType & VB_SISVB) {
+	   temp = SetCRT2ToSVIDEO   |
+	          SetCRT2ToAVIDEO   |
+	          SetCRT2ToSCART    |
+	          SetCRT2ToLCDA     |
+	          SetCRT2ToLCD      |
+	          SetCRT2ToRAMDAC   |
+                  SetCRT2ToHiVision |
+		  SetCRT2ToYPbPr525750;
+    	} else {
+           if(HwInfo->jChipType >= SIS_315H) {
+              if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
+        	 temp = SetCRT2ToAVIDEO |
+		        SetCRT2ToSVIDEO |
+		        SetCRT2ToSCART  |
+		        SetCRT2ToLCDA   |
+		        SetCRT2ToLCD    |
+		        SetCRT2ToCHYPbPr;
+      	      } else {
+        	 temp = SetCRT2ToLCDA   |
+		        SetCRT2ToLCD;
+	      }
 	   } else {
-	      p1_7 = 0x2f;
-	      p1_8 = 0x03;
+      	      if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
+          	 temp = SetCRT2ToTV | SetCRT2ToLCD;
+              } else {
+        	 temp = SetCRT2ToLCD;
+	      }
 	   }
-        }
-     }
-  }
+    	}
+
+    	if(!(tempbx & temp)) {
+      	   tempax = DisableCRT2Display;
+      	   tempbx = 0;
+    	}
 
-  if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) {
-     if(SiS_Pr->SiS_HiVision & 0x03) {
-	p1_7 = 0xb2;
-	if(SiS_Pr->SiS_HiVision & 0x02) {
-	   p1_7 = 0xab;
+   	if(SiS_Pr->SiS_VBType & VB_SISVB) {
+	   USHORT clearmask = ( DriverMode 	   |
+				DisableCRT2Display |
+				LoadDACFlag 	   |
+				SetNotSimuMode 	   |
+				SetInSlaveMode 	   |
+				SetPALTV 	   |
+				SwitchCRT2	   |
+				SetSimuScanMode );
+      	   if(tempbx & SetCRT2ToLCDA) {
+              tempbx &= (clearmask | SetCRT2ToLCDA);
+      	   }
+	   if(tempbx & SetCRT2ToRAMDAC) {
+              tempbx &= (clearmask | SetCRT2ToRAMDAC);
+      	   }
+	   if(tempbx & SetCRT2ToLCD) {
+              tempbx &= (clearmask | SetCRT2ToLCD);
+      	   }
+	   if(tempbx & SetCRT2ToSCART) {
+              tempbx &= (clearmask | SetCRT2ToSCART);
+      	   }
+	   if(tempbx & SetCRT2ToHiVision) {
+              tempbx &= (clearmask | SetCRT2ToHiVision);
+      	   }
+	   if(tempbx & SetCRT2ToYPbPr525750) {
+	      tempbx &= (clearmask | SetCRT2ToYPbPr525750);
+	   }
+   	} else {
+	   if(HwInfo->jChipType >= SIS_315H) {
+	      if(tempbx & SetCRT2ToLCDA) {
+	         tempbx &= (0xFF00|SwitchCRT2|SetSimuScanMode);
+	      }
+	   }
+      	   if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
+              if(tempbx & SetCRT2ToTV) {
+          	 tempbx &= (0xFF00|SetCRT2ToTV|SwitchCRT2|SetSimuScanMode);
+	      }
+      	   }
+      	   if(tempbx & SetCRT2ToLCD) {
+              tempbx &= (0xFF00|SetCRT2ToLCD|SwitchCRT2|SetSimuScanMode);
+	   }
+	   if(HwInfo->jChipType >= SIS_315H) {
+	      if(tempbx & SetCRT2ToLCDA) {
+	         tempbx |= SetCRT2ToLCD;
+	      }
+	   }
 	}
-     }
-  }
 
-  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   */
+    	if(tempax & DisableCRT2Display) {
+      	   if(!(tempbx & (SwitchCRT2 | SetSimuScanMode))) {
+              tempbx = SetSimuScanMode | DisableCRT2Display;
+      	   }
+    	}
 
+    	if(!(tempbx & DriverMode)){
+      	   tempbx |= SetSimuScanMode;
+    	}
 
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x18,0x03);                	/* 0x18 SR08 (FIFO Threshold?)   */
+	/* LVDS/CHRONTEL (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)) ) {
+	       modeflag &= (~CRT2Mode);
+	   }
+	}
 
-  SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x19,0xF0);
+    	if(!(tempbx & SetSimuScanMode)) {
+      	   if(tempbx & SwitchCRT2) {
+              if((!(modeflag & CRT2Mode)) && (checkcrt2mode)) {
+		 if( (HwInfo->jChipType >= SIS_315H) &&
+		     (SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) ) {
+		    if(resinfo != SIS_RI_1600x1200) {
+                       tempbx |= SetSimuScanMode;
+		    }
+		 } else {
+            	    tempbx |= SetSimuScanMode;
+	         }
+              }
+      	   } else {
+              if(!(SiS_BridgeIsEnabled(SiS_Pr,HwInfo))) {
+          	 if(!(tempbx & DriverMode)) {
+            	    if(SiS_BridgeInSlave(SiS_Pr)) {
+		       tempbx |= SetSimuScanMode;
+            	    }
+                 }
+              }
+      	   }
+    	}
 
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x09,0xFF);                	/* 0x09 Set Max VT    */
+    	if(!(tempbx & DisableCRT2Display)) {
+           if(tempbx & DriverMode) {
+              if(tempbx & SetSimuScanMode) {
+          	 if((!(modeflag & CRT2Mode)) && (checkcrt2mode)) {
+	            if( (HwInfo->jChipType >= SIS_315H) &&
+		        (SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) ) {
+		       if(resinfo != SIS_RI_1600x1200) {
+		          tempbx |= SetInSlaveMode;
+		       }
+		    } else {
+            	       tempbx |= SetInSlaveMode;
+                    }
+	         }
+              }
+           } else {
+              tempbx |= SetInSlaveMode;
+      	   }
+    	}
 
-  tempcx = 0x121;
-  tempbx = SiS_Pr->SiS_VGAVDE;                               	/* 0x0E Vertical Display End */
-  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;
-           else if(tempbx == 480) tempbx += 5;
-      	}
-     }
   }
-  tempbx -= 2;
-  temp = tempbx & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x10,temp);        		/* 0x10 vertical Blank Start */
 
-  tempbx = push2;
-  tempbx--;
-  temp = tempbx & 0x00FF;
-#if 0
-  /* Missing code from 630/301B 2.04.5a and 650/302LV 1.10.6s (calles int 2f) */
-  if(xxx()) {
-      if(temp == 0xdf) temp = 0xda;
+  SiS_Pr->SiS_VBInfo = tempbx;
+
+  if(HwInfo->jChipType == SIS_630) {
+     SiS_SetChrontelGPIO(SiS_Pr, SiS_Pr->SiS_VBInfo);
   }
+
+#ifdef TWDEBUG
+#ifdef LINUX_KERNEL
+  printk(KERN_DEBUG "sisfb: (VBInfo= 0x%04x, SetFlag=0x%04x)\n",
+      SiS_Pr->SiS_VBInfo, SiS_Pr->SiS_SetFlag);
+#endif
+#ifdef LINUX_XF86
+  xf86DrvMsgVerb(0, X_PROBED, 3, "(init301: VBInfo=0x%04x, SetFlag=0x%04x)\n",
+      SiS_Pr->SiS_VBInfo, SiS_Pr->SiS_SetFlag);
 #endif
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0E,temp);
+#endif
+}
 
-  if(tempbx & 0x0100)  tempcx |= 0x0002;
+/*********************************************/
+/*           DETERMINE YPbPr MODE            */
+/*********************************************/
 
-  tempax = 0x000B;
-  if(modeflag & DoubleScanMode) tempax |= 0x8000;
+void
+SiS_SetYPbPr(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
+{
+  /* Note: This variable is only used on 30xLV systems.
+   * CR38 has a different meaning on LVDS/CH7019 systems.
+   * On 661 and later, these bits moved to CR35.
+   */
 
-  if(tempbx & 0x0200)  tempcx |= 0x0040;
+  SiS_Pr->SiS_YPbPr = 0;
+#ifdef SIS315H
+  if((HwInfo->jChipType >= SIS_315H) && (HwInfo->jChipType < SIS_661)) {
+     if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
+        if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) {
+	   SiS_Pr->SiS_YPbPr = ((SiS_GetReg(SiS_Pr->SiS_P3d4,0x38) & 0x38) >> 3);
+	}
+     }
+  }
+#endif /* SIS315H */
+}
 
-  temp = (tempax & 0xFF00) >> 8;
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0B,temp);
+/*********************************************/
+/*           DETERMINE TVMode flag           */
+/*********************************************/
 
-  if(tempbx & 0x0400)  tempcx |= 0x0600;
+void
+SiS_SetTVMode(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex, PSIS_HW_INFO HwInfo)
+{
+  UCHAR  *ROMAddr = HwInfo->pjVirtualRomBase;
+  USHORT temp, temp1, resinfo = 0, romindex = 0;
+  UCHAR  OutputSelect = *SiS_Pr->pSiS_OutputSelect;
 
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x11,0x00);                	/* 0x11 Vertical Blank End */
+  SiS_Pr->SiS_TVMode = 0;
 
-  tempax = (SiS_Pr->SiS_VGAVT - tempbx) >> 2;
+  if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToTV)) return;
+  if(SiS_Pr->UseCustomMode) return;
 
-  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(ModeNo > 0x13) {
+     resinfo = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO;
   }
 
-  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(HwInfo->jChipType < SIS_661) {
+
+     if(SiS_Pr->SiS_VBInfo & SetPALTV) SiS_Pr->SiS_TVMode |= TVSetPAL;
+
+     if(SiS_Pr->SiS_VBType & VB_SISVB) {
+        temp = 0;
+        if((HwInfo->jChipType == SIS_630) ||
+           (HwInfo->jChipType == SIS_730)) {
+           temp = 0x35;
+	   romindex = 0xfe;
+        } else if(HwInfo->jChipType >= SIS_315H) {
+           temp = 0x38;
+	   romindex = 0xf3;
+	   if(HwInfo->jChipType >= SIS_330) romindex = 0x11b;
+        }
+        if(temp) {
+           if(romindex && ROMAddr && SiS_Pr->SiS_UseROM) {
+	      OutputSelect = ROMAddr[romindex];
+	      if(!(OutputSelect & EnablePALMN)) {
+                 SiS_SetRegAND(SiS_Pr->SiS_P3d4,temp,0x3F);
+	      }
+	   }
+	   temp1 = SiS_GetReg(SiS_Pr->SiS_P3d4,temp);
+	   if(SiS_Pr->SiS_TVMode & TVSetPAL) {
+              if(temp1 & EnablePALM) {		/* 0x40 */
+                 SiS_Pr->SiS_TVMode |= TVSetPALM;
+	         SiS_Pr->SiS_TVMode &= ~TVSetPAL;
+	      } else if(temp1 & EnablePALN) {	/* 0x80 */
+	         SiS_Pr->SiS_TVMode |= TVSetPALN;
+              }
+	   } else {
+              if(temp1 & EnableNTSCJ) {		/* 0x40 */
+	         SiS_Pr->SiS_TVMode |= TVSetNTSCJ;
+  	      }
+	   }
+        }
+	/* Translate HiVision/YPbPr to our new flags */
+	if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) {
+	   if(SiS_Pr->SiS_YPbPr == YPbPr750)           SiS_Pr->SiS_TVMode |= TVSetYPbPr750p;
+	   else if(SiS_Pr->SiS_YPbPr == YPbPr525)      SiS_Pr->SiS_TVMode |= TVSetYPbPr525p;
+	   else	if(SiS_Pr->SiS_YPbPr == YPbPrHiVision) SiS_Pr->SiS_TVMode |= TVSetHiVision;
+	   else					       SiS_Pr->SiS_TVMode |= TVSetYPbPr525i;
+	   if(SiS_Pr->SiS_TVMode & (TVSetYPbPr750p | TVSetYPbPr525p | TVSetYPbPr525i)) {
+	      SiS_Pr->SiS_VBInfo &= ~SetCRT2ToHiVision;
+	      SiS_Pr->SiS_VBInfo |= SetCRT2ToYPbPr525750;
+	   } else if(SiS_Pr->SiS_TVMode & TVSetHiVision) {
+	      SiS_Pr->SiS_TVMode |= TVSetPAL;
+	   }
+	}
+     } else if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
+        if(SiS_Pr->SiS_CHOverScan) {
+           if(SiS_Pr->SiS_IF_DEF_CH70xx == 1) {
+              temp = SiS_GetReg(SiS_Pr->SiS_P3d4,0x35);
+              if((temp & TVOverScan) || (SiS_Pr->SiS_CHOverScan == 1)) {
+	         SiS_Pr->SiS_TVMode |= TVSetCHOverScan;
+              }
+           } else if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
+      	      temp = SiS_GetReg(SiS_Pr->SiS_P3d4,0x79);
+      	      if((temp & 0x80) || (SiS_Pr->SiS_CHOverScan == 1)) {
+	         SiS_Pr->SiS_TVMode |= TVSetCHOverScan;
+ 	      }
+	   }
+           if(SiS_Pr->SiS_CHSOverScan) {
+              SiS_Pr->SiS_TVMode |= TVSetCHOverScan;
+           }
+        }
+        if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
+	   temp = SiS_GetReg(SiS_Pr->SiS_P3d4,0x38);
+     	   if(SiS_Pr->SiS_TVMode & TVSetPAL) {
+              if(temp & EnablePALM)      SiS_Pr->SiS_TVMode |= TVSetPALM;
+	      else if(temp & EnablePALN) SiS_Pr->SiS_TVMode |= TVSetPALN;
+           } else {
+	      if(temp & EnableNTSCJ) {
+	         SiS_Pr->SiS_TVMode |= TVSetNTSCJ;
+  	      }
+	   }
 	}
      }
-  }
-  tempax >>= 2;
-  tempax++;
-  tempax += tempbx;
-  push1 = tempax;
-  if(SiS_Pr->SiS_VBInfo & SetPALTV) {
-     if(tempbx <= 513)  {
-     	if(tempax >= 513) tempbx = 513;
-     }
-  }
-  temp = tempbx & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0C,temp);			/* 0x0C Vertical Retrace Start */
-
-  tempbx--;
-  temp = tempbx & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x10,temp);
 
-  if(tempbx & 0x0100) tempcx |= 0x0008;
+  } else {  /* 661 and later */
 
-  if(tempbx & 0x0200) {
-     SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x0B,0x20);
+     temp1 = SiS_GetReg(SiS_Pr->SiS_P3d4,0x35);
+     if(temp1 & 0x01) {
+        SiS_Pr->SiS_TVMode |= TVSetPAL;
+	if(temp1 & 0x08) {
+	   SiS_Pr->SiS_TVMode |= TVSetPALN;
+	} else if(temp1 & 0x04) {
+	   if(SiS_Pr->SiS_VBType & VB_SISVB) {
+	      SiS_Pr->SiS_TVMode &= ~TVSetPAL;
+	   }
+	   SiS_Pr->SiS_TVMode |= TVSetPALM;
+	}
+     } else {
+        if(temp1 & 0x02) {
+	   SiS_Pr->SiS_TVMode |= TVSetNTSCJ;
+	}
+     }
+     if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
+        if(SiS_Pr->SiS_CHOverScan) {
+           if((temp1 & 0x10) || (SiS_Pr->SiS_CHOverScan == 1)) {
+	      SiS_Pr->SiS_TVMode |= TVSetCHOverScan;
+	   }
+	}
+     }
+     if(SiS_Pr->SiS_VBType & VB_SISVB) {
+        if(SiS_Pr->SiS_VBInfo & SetCRT2ToYPbPr525750) {
+	   temp1 &= 0xe0;
+	   if(temp1 == 0x00)      SiS_Pr->SiS_TVMode |= TVSetYPbPr525i;
+	   else if(temp1 == 0x20) SiS_Pr->SiS_TVMode |= TVSetYPbPr525p;
+	   else if(temp1 == 0x40) SiS_Pr->SiS_TVMode |= TVSetYPbPr750p;
+	} else if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) {
+	   SiS_Pr->SiS_TVMode |= (TVSetHiVision | TVSetPAL);
+	}
+     }
   }
-  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_VBInfo & SetCRT2ToSCART) SiS_Pr->SiS_TVMode |= TVSetPAL;
 
-  tempbx = push1;
-  temp = tempbx & 0x000F;
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0D,temp);        		/* 0x0D vertical Retrace End */
+  if(SiS_Pr->SiS_VBType & VB_SISVB) {
 
-  if(tempbx & 0x0010) tempcx |= 0x2000;
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) {
+        SiS_Pr->SiS_TVMode |= TVSetPAL;
+     } else if(SiS_Pr->SiS_VBInfo & SetCRT2ToYPbPr525750) {
+        if(SiS_Pr->SiS_TVMode & (TVSetYPbPr525i | TVSetYPbPr525p | TVSetYPbPr750p)) {
+	   SiS_Pr->SiS_TVMode &= ~(TVSetPAL | TVSetNTSCJ | TVSetPALM | TVSetPALN);
+	}
+     }
 
-  temp = tempcx & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0A,temp);              	/* 0x0A CR07 */
+     if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
+        if(!(SiS_Pr->SiS_VBInfo & SetNotSimuMode)) {
+           SiS_Pr->SiS_TVMode |= TVSetTVSimuMode;
+        }
+     }
 
-  temp = (tempcx & 0xFF00) >> 8;
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x17,temp);              	/* 0x17 SR0A */
+     if(!(SiS_Pr->SiS_TVMode & TVSetPAL)) {
+        if(!(SiS_Pr->SiS_TVMode & (TVSetHiVision | TVSetYPbPr525p | TVSetYPbPr750p))) {
+           if(resinfo == SIS_RI_1024x768) {
+              SiS_Pr->SiS_TVMode |= TVSetNTSC1024;
+	   }
+        }
+     }
 
-  tempax = modeflag;
-  temp = (tempax & 0xFF00) >> 8;
-  temp = (temp >> 1) & 0x09;
-  if(!(SiS_Pr->SiS_VBType & VB_SIS301)) {
-     /* Only use 8 dot clock */
-     temp |= 0x01;
-  }
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x16,temp);              	/* 0x16 SR01 */
+     SiS_Pr->SiS_TVMode |= TVRPLLDIV2XO;
+     if((SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) &&
+        (SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
+	SiS_Pr->SiS_TVMode &= ~TVRPLLDIV2XO;
+     } else if(SiS_Pr->SiS_TVMode & (TVSetYPbPr525p | TVSetYPbPr750p)) {
+        SiS_Pr->SiS_TVMode &= ~TVRPLLDIV2XO;
+     } else if(!(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV)) {
+        if(SiS_Pr->SiS_TVMode & TVSetTVSimuMode) {
+           SiS_Pr->SiS_TVMode &= ~TVRPLLDIV2XO;
+        }
+     }
 
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x0F,0x00);              	/* 0x0F CR14 */
+  }
 
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x12,0x00);              	/* 0x12 CR17 */
+  SiS_Pr->SiS_VBInfo &= ~SetPALTV;
 
-  if(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit) {
-     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 */
+#ifdef TWDEBUG
+  xf86DrvMsg(0, X_INFO, "(init301.c: TVMode %x)\n", SiS_Pr->SiS_TVMode);
+#endif 
 
 }
 
+/*********************************************/
+/*               GET LCD INFO                */
+/*********************************************/
+
 void
-SiS_SetGroup1_LVDS(SiS_Private *SiS_Pr,USHORT  BaseAddr,UCHAR *ROMAddr,USHORT ModeNo,
-		   USHORT ModeIdIndex,PSIS_HW_DEVICE_INFO HwDeviceExtension,
-		   USHORT RefreshRateTableIndex)
+SiS_GetLCDResInfo(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,
+		  PSIS_HW_INFO HwInfo)
 {
-  USHORT modeflag, resinfo;
-  USHORT push1, push2, tempax, tempbx, tempcx, temp;
-#ifdef SIS315H
-  USHORT pushcx;
+#ifdef SIS300
+  UCHAR  *ROMAddr = HwInfo->pjVirtualRomBase;
 #endif
-  ULONG  tempeax=0, tempebx, tempecx, tempvcfact=0;
+#ifdef SIS315H
+  UCHAR  *myptr = NULL;
+#endif  
+  USHORT temp,modeflag,resinfo=0;
+  const unsigned char SiS300SeriesLCDRes[] =
+         { 0,  1,  2,  3,  7,  4,  5,  8,
+	   0,  0, 10,  0,  0,  0,  0, 15 };
 
-  /* This is not supported on LVDS */
-  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_PanelCustom) return;
-  if(SiS_Pr->UseCustomMode) return;
+  SiS_Pr->SiS_LCDResInfo = 0;
+  SiS_Pr->SiS_LCDTypeInfo = 0;
+  SiS_Pr->SiS_LCDInfo = 0;
 
-  if(ModeNo <= 0x13) {
-     modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
-     resinfo = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ResInfo;
+  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;
+     if(ModeNo <= 0x13) {
+    	modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
+     } else {
+    	modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
+    	resinfo = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO;
+     }
   }
 
-  /* Set up Panel Link */
+  if(!(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA))) return;
 
-  /* 1. Horizontal setup */
+  temp = SiS_GetReg(SiS_Pr->SiS_P3d4,0x36);
 
-  tempax = SiS_Pr->SiS_LCDHDES;
+  if((HwInfo->jChipType < SIS_315H) || (HwInfo->jChipType >= SIS_661)) {
+     SiS_Pr->SiS_LCDTypeInfo = temp >> 4;
+  } else {
+     SiS_Pr->SiS_LCDTypeInfo = (temp & 0x0F) - 1;
+  }
+  temp &= 0x0f;
+  if(HwInfo->jChipType < SIS_315H) {
+      /* Translate 300 series LCDRes to 315 series for unified usage */
+      temp = SiS300SeriesLCDRes[temp];
+  }
+  SiS_Pr->SiS_LCDResInfo = temp;
 
-  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;
-     }
+  if(SiS_Pr->SiS_VBType & VB_SISVB) {
+     if(SiS_Pr->SiS_LCDResInfo < SiS_Pr->SiS_PanelMin301)
+	SiS_Pr->SiS_LCDResInfo = SiS_Pr->SiS_PanelMin301;
+  } else {
+     if(SiS_Pr->SiS_LCDResInfo < SiS_Pr->SiS_PanelMinLVDS)
+	SiS_Pr->SiS_LCDResInfo = SiS_Pr->SiS_PanelMinLVDS;
   }
 
-  tempcx = SiS_Pr->SiS_HT;    				  /* Horiz. Total */
+  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;
+  }
 
-  tempbx = SiS_Pr->SiS_HDE;                               /* Horiz. Display End */
+  if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
+     if(SiS_Pr->SiS_CustomT == CUT_BARCO1366) {
+        SiS_Pr->SiS_LCDResInfo = Panel_Barco1366;
+     } else if(SiS_Pr->SiS_CustomT == CUT_PANEL848) {
+        SiS_Pr->SiS_LCDResInfo = Panel_848x480;
+     }
+  }
 
-  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_2 ||
-     SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_3) {
-     tempbx >>= 1;
+  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;
+     case Panel_848x480:   SiS_Pr->PanelXRes =  848; SiS_Pr->PanelYRes =  480; break;
+     default:		   SiS_Pr->PanelXRes = 1024; SiS_Pr->PanelYRes =  768; break;
   }
 
-  if(!(SiS_Pr->SiS_LCDInfo & LCDPass11)) {
-     if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
-        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;
-	      }
-	   }
+  temp = SiS_GetReg(SiS_Pr->SiS_P3d4,0x37);
+  if(HwInfo->jChipType < SIS_661) {
+     temp &= ~0xe;
+  } else {
+#ifdef SIS315H
+     if(!(temp & 0x10)) {
+        if(temp & 0x08) temp |= LCDPass11;
+     }
+     temp &= ~0xe;
+     if(SiS_Pr->SiS_VBType & (VB_SIS302LV | VB_SIS302ELV)) {
+        myptr = GetLCDStructPtr661(SiS_Pr, HwInfo);
+        if(myptr) {
+           if(myptr[2] & 0x01) temp |= LCDDualLink;
         }
      }
+#endif
   }
-  tempcx = (tempcx - tempbx) >> 2;		 /* HT-HDE / 4 */
-
-  push1 = tempax;
+  SiS_Pr->SiS_LCDInfo = temp;
 
-  tempax += tempbx;
+  if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
+     if(SiS_Pr->SiS_CustomT == CUT_PANEL848) {
+        SiS_Pr->SiS_LCDInfo = 0x80 | 0x40 | 0x20;   /* neg h/v sync, RGB24 */
+     }
+  }
 
-  if(tempax >= SiS_Pr->SiS_HT) tempax -= SiS_Pr->SiS_HT;
+  if(!(SiS_Pr->UsePanelScaler))        SiS_Pr->SiS_LCDInfo &= ~DontExpandLCD;
+  else if(SiS_Pr->UsePanelScaler == 1) SiS_Pr->SiS_LCDInfo |= DontExpandLCD;
 
-  push2 = tempax;
+  if(SiS_Pr->SiS_VBType & VB_SISVB) {
+     if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
+        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) {
+	      /* We do not scale to 1280x960 (B/C bridges only) */
+              SiS_Pr->SiS_LCDInfo |= DontExpandLCD;
+	   }
+	   if(((HwInfo->jChipType >= SIS_315H) && (ModeNo == 0x23 || ModeNo == 0x24 || ModeNo == 0x25)) ||
+	      ((HwInfo->jChipType < SIS_315H)  && (ModeNo == 0x55 || ModeNo == 0x5a || ModeNo == 0x5b))) {
+	      /* We do not scale to 1280x768 (B/C bridges only) */
+              SiS_Pr->SiS_LCDInfo |= DontExpandLCD;
+	   }
+	   if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
+	      /* No non-scaling data available for LV bridges */
+	      SiS_Pr->SiS_LCDInfo &= ~DontExpandLCD;
+	   }
+        } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768) {
+           /* 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) {
+	      /* We do not scale to 1280x1024 (all bridges) */
+	      SiS_Pr->SiS_LCDInfo |= DontExpandLCD;
+	   }
+	} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) {
+	   if(SiS_Pr->SiS_VBType & VB_SIS301B302B) {
+	      /* No idea about the timing and zoom factors (C bridge only) */
+	      SiS_Pr->SiS_LCDInfo |= DontExpandLCD;
+	   }
+	}
+	if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
+	   if(SiS_Pr->SiS_CustomT == CUT_CLEVO1024) {
+              if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
+	         SiS_Pr->SiS_LCDInfo &= ~DontExpandLCD;
+	      }
+	   }
+	}
+     }
+  }
 
-  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) &&
-     (SiS_Pr->SiS_CustomT != CUT_PANEL848)) {
-     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) {
-		         tempcx = 0x0017;
-#ifdef TWNEWPANEL
-			 tempcx = 0x0018;
+  if(HwInfo->jChipType >= SIS_315H) {
+#ifdef SIS315H
+     if(HwInfo->jChipType < SIS_661) {
+        if(SiS_GetReg(SiS_Pr->SiS_P3d4,0x39) & 0x01) {
+           SiS_Pr->SiS_LCDInfo &= (~DontExpandLCD);
+	   SiS_Pr->SiS_LCDInfo |= LCDPass11;
+	}
+     }
 #endif
-		      } else {
-		         tempcx = 0x0017;  /* A901; sometimes 0x0018; */
-		      }
-		   } 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 {
+#ifdef SIS300
+     if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
+        if((ROMAddr) && SiS_Pr->SiS_UseROM) {
+	   if((ROMAddr[0x233] == 0x12) && (ROMAddr[0x234] == 0x34)) {
+              if(!(ROMAddr[0x235] & 0x02)) {
+	         SiS_Pr->SiS_LCDInfo &= (~DontExpandLCD);
+ 	      }
 	   }
         }
+     } else if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
+	if((SiS_Pr->SiS_SetFlag & SetDOSMode) && ((ModeNo == 0x03) || (ModeNo == 0x10))) {
+           SiS_Pr->SiS_LCDInfo &= (~DontExpandLCD);
+	}
      }
+#endif
   }
 
-  tempcx += tempax;                              /* lcdhrs  */
-  if(tempcx >= SiS_Pr->SiS_HT) tempcx -= SiS_Pr->SiS_HT;
+  /* Trumpion: Assume non-expanding */
+  if(SiS_Pr->SiS_IF_DEF_TRUMPION != 0) {
+     SiS_Pr->SiS_LCDInfo &= (~DontExpandLCD);
+  }
 
-  tempax = tempcx >> 3;                          /* BPLHRS */
-  temp = tempax & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x14,temp);		 /* Part1_14h; Panel Link Horizontal Retrace Start  */
+  if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
+     SiS_Pr->SiS_LCDInfo &= (~LCDPass11);
+  }
 
-  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) &&
-	   (!SiS_Pr->SiS_IF_DEF_FSTN) &&
-	   (SiS_Pr->SiS_CustomT != CUT_BARCO1366) &&
-	   (SiS_Pr->SiS_CustomT != CUT_BARCO1024) &&
-	   (SiS_Pr->SiS_CustomT != CUT_PANEL848)) {
-           if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480) {
-	      temp += 6;
-              if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel800x600) {
-	         temp++;
-	         if(HwDeviceExtension->jChipType >= SIS_315H) {
-	            if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1024x768) {
-	               temp += 7;
-		       if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1600x1200) {
-		          temp -= 0x14;
-			  if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1280x768) {
-			     temp -= 10;
-			  }
-		       }
-	            }
-	         }
-	      }
-           }
-        }
+#ifdef SIS315H
+  if((HwInfo->jChipType >= SIS_315H) && (HwInfo->jChipType < SIS_661)) {
+     if(SiS_Pr->SiS_VBType & (VB_SIS302LV | VB_SIS302ELV)) {
+	/* Enable 302LV/302ELV dual link mode.
+	 * For 661, this is done above.
+	 */
+        if((SiS_Pr->SiS_CustomT == CUT_CLEVO1024) &&
+	   (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768)) {
+	   /* (Sets this in SenseLCD; new paneltypes) */
+	   SiS_Pr->SiS_LCDInfo |= LCDDualLink;
+	}
+        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)) {
+	   SiS_Pr->SiS_LCDInfo |= LCDDualLink;
+	}
      }
   }
+#endif
 
-  temp &= 0x1F;
-  temp |= ((tempcx & 0x0007) << 5);
-#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 */
+  if(!((HwInfo->jChipType < SIS_315H) && (SiS_Pr->SiS_SetFlag & SetDOSMode))) {
 
-  tempbx = push2;
-  tempcx = push1;                                /* lcdhdes  */
+     if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
+        if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x600) {
+	   if(ModeNo > 0x13) {
+	      if(!(SiS_Pr->SiS_LCDInfo & DontExpandLCD)) {
+                 if((resinfo == SIS_RI_800x600) || (resinfo == SIS_RI_400x300)) {
+                    SiS_Pr->SiS_SetFlag |= EnableLVDSDDA;
+		 }
+              }
+           }
+        }
+	if(ModeNo == 0x12) {
+	   if(SiS_Pr->SiS_LCDInfo & LCDPass11) {
+	      SiS_Pr->SiS_SetFlag |= EnableLVDSDDA;
+	   }
+	}
+     }
 
-  temp = (tempcx & 0x0007);                      /* BPLHDESKEW  */
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1A,temp);   	 /* Part1_1Ah; Panel Link Vertical Retrace Start (2:0) */
+     if(modeflag & HalfDCLK) {
+        if(SiS_Pr->SiS_IF_DEF_TRUMPION == 0) {
+           if(!(SiS_Pr->SiS_LCDInfo & DontExpandLCD)) {
+	      if(!(((SiS_Pr->SiS_IF_DEF_LVDS == 1) || (HwInfo->jChipType < SIS_315H)) &&
+	                                      (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480))) {
+                 if(ModeNo > 0x13) {
+                    if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
+                       if(resinfo == SIS_RI_512x384) SiS_Pr->SiS_SetFlag |= EnableLVDSDDA;
+                    } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600) {
+                       if(resinfo == SIS_RI_400x300) SiS_Pr->SiS_SetFlag |= EnableLVDSDDA;
+                    }
+                 }
+	      } else SiS_Pr->SiS_SetFlag |= EnableLVDSDDA;
+           } else SiS_Pr->SiS_SetFlag |= EnableLVDSDDA;
+        } else SiS_Pr->SiS_SetFlag |= EnableLVDSDDA;
+     }
 
-  tempcx >>= 3;                                  /* BPLHDES */
-  temp = (tempcx & 0x00FF);
-#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) ||
-     (SiS_Pr->SiS_IF_DEF_FSTN) ||
-     (SiS_Pr->SiS_IF_DEF_DSTN)) {
-     if(tempbx & 0x07) tempbx += 8;              
-  }
-  tempbx >>= 3;                                  /* BPLHDEE  */
-  temp = tempbx & 0x00FF;
-#if 0 /* Not 550 FSTN */
-  if(HwDeviceExtension->jChipType >= SIS_315H) {
-     if(ModeNo == 0x5b) temp--;
+  if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
+     if(SiS_Pr->SiS_VBInfo & SetNotSimuMode) {
+     	SiS_Pr->SiS_SetFlag |= LCDVESATiming;
+     }
+  } else {
+     SiS_Pr->SiS_SetFlag |= LCDVESATiming;
   }
+
+  SiS_Pr->SiS_LCDInfo661 = 0;
+  if(SiS_Pr->SiS_SetFlag & LCDVESATiming) SiS_Pr->SiS_LCDInfo661 |= 0x0001;
+  if(SiS_Pr->SiS_SetFlag & EnableLVDSDDA) SiS_Pr->SiS_LCDInfo661 |= 0x0002;
+  if(SiS_Pr->SiS_LCDInfo & LCDPass11)     SiS_Pr->SiS_LCDInfo661 |= 0x0008;
+  if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) SiS_Pr->SiS_LCDInfo661 |= 0x0010;
+  SiS_Pr->SiS_LCDInfo661 |= (SiS_Pr->SiS_LCDInfo & 0xe0);
+  if(SiS_Pr->SiS_LCDInfo & LCDDualLink)   SiS_Pr->SiS_LCDInfo661 |= 0x0100;
+
+#ifdef LINUX_KERNEL
+#ifdef TWDEBUG
+  printk(KERN_DEBUG "sisfb: (LCDInfo=0x%04x LCDResInfo=0x%02x LCDTypeInfo=0x%02x)\n",
+	SiS_Pr->SiS_LCDInfo, SiS_Pr->SiS_LCDResInfo, SiS_Pr->SiS_LCDTypeInfo);
+#endif
 #endif
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x17,temp);   	 /* Part1_17h; Panel Link Horizontal Display Enable End  */
+#ifdef LINUX_XF86
+  xf86DrvMsgVerb(0, X_PROBED, 4,
+  	"(init301: LCDInfo=0x%04x LCDResInfo=0x%02x LCDTypeInfo=0x%02x SetFlag=0x%04x)\n",
+	SiS_Pr->SiS_LCDInfo, SiS_Pr->SiS_LCDResInfo, SiS_Pr->SiS_LCDTypeInfo, SiS_Pr->SiS_SetFlag);
+#endif
+}
 
-  /* 2. Vertical setup */
+/*********************************************/
+/*                 GET VCLK                  */
+/*********************************************/
 
-  if(HwDeviceExtension->jChipType < SIS_315H) {
-     tempcx = SiS_Pr->SiS_VGAVT;
-     tempbx = SiS_Pr->SiS_VGAVDE;
-     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;
+USHORT
+SiS_GetVCLK2Ptr(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,
+                USHORT RefreshRateTableIndex, PSIS_HW_INFO HwInfo)
+{
+  USHORT tempbx;
+  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_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_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 USHORT *LCDXlatVCLK1 = NULL;
+  const USHORT *LCDXlatVCLK2 = NULL;
+  const USHORT *LVDSXlatVCLK2 = NULL;
+  const USHORT *LVDSXlatVCLK3 = NULL;
 
+  if(HwInfo->jChipType >= SIS_315H) {
+#ifdef SIS315H
+     LCDXlatVCLK1 = LCDXlat1VCLK310;
+     LCDXlatVCLK2 = LCDXlat2VCLK310;
+     LVDSXlatVCLK2 = LVDSXlat2VCLK310;
+     LVDSXlatVCLK3 = LVDSXlat3VCLK310;
+#endif
   } else {
+#ifdef SIS300
+     LCDXlatVCLK1 = LCDXlat1VCLK300;
+     LCDXlatVCLK2 = LCDXlat2VCLK300;
+     LVDSXlatVCLK2 = LVDSXlat2VCLK300;
+     LVDSXlatVCLK3 = LVDSXlat3VCLK300;
+#endif
+  }
 
-     tempcx = SiS_Pr->SiS_VGAVT - SiS_Pr->SiS_VGAVDE;           /* VGAVT-VGAVDE  */
-
+  if(ModeNo <= 0x13) {
+     modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
+     resinfo = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ResInfo;
+     CRT2Index = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_CRT2CRTC;
+  } else {
+     modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
+     resinfo = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO;
+     CRT2Index = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
   }
 
-  tempbx = SiS_Pr->SiS_LCDVDES;	   		 	 	/* VGAVDES  */
-  push1 = tempbx;
+  if(SiS_Pr->SiS_VBType & VB_SISVB) {    /* 30x/B/LV */
 
-  tempax = SiS_Pr->SiS_VGAVDE;
+     if(SiS_Pr->SiS_SetFlag & ProgrammingCRT2) {
 
-  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;
+        CRT2Index >>= 6;
+        if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {      /*  LCD */
+
+           if(HwInfo->jChipType < SIS_315H) {
+	      if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600) {
+	         VCLKIndex = LCDXlat0VCLK[CRT2Index];
+	      } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
+	    	 VCLKIndex = LCDXlatVCLK1[CRT2Index];
+	      } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x600) {
+	    	 VCLKIndex = LCDXlatVCLK1[CRT2Index];
+	      } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1152x768) {
+	    	 VCLKIndex = LCDXlatVCLK1[CRT2Index];
+	      } 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 = VCLK108_2_315;
+		 } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768) {
+		    VCLKIndex = VCLK81_315;  	/* guessed */
+		 } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) {
+		    VCLKIndex = VCLK108_2_315;
+		 } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) {
+		    VCLKIndex = VCLK162_315;
+		 } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x960) {
+		    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 {
+		    VCLKIndex = LCDXlatVCLK2[CRT2Index];
+      	         }
+	      } else {
+                 VCLKIndex = (UCHAR)SiS_GetRegByte((USHORT)(SiS_Pr->SiS_P3ca+0x02));  /*  Port 3cch */
+         	 VCLKIndex = ((VCLKIndex >> 2) & 0x03);
+        	 if(ModeNo > 0x13) {
+          	    VCLKIndex = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
+        	 }
+		 if(ModeNo <= 0x13) {
+		    if(HwInfo->jChipType <= SIS_315PRO) {
+		       if(SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_CRT2CRTC == 1) VCLKIndex = 0x42;
+	            } else {
+		       if(SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_CRT2CRTC == 1) VCLKIndex = 0x00;
+		    }
+		 }
+		 if(HwInfo->jChipType <= SIS_315PRO) {
+		    if(VCLKIndex == 0) VCLKIndex = 0x41;
+		    if(VCLKIndex == 1) VCLKIndex = 0x43;
+		    if(VCLKIndex == 4) VCLKIndex = 0x44;
+		 }
+	      }
 	   }
-	}
-     } 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;
-     }
-  }
 
-  tempbx += tempax;
-  if(tempbx >= SiS_Pr->SiS_VT) tempbx -= SiS_Pr->SiS_VT;
+        } else if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {                 /*  TV */
 
-  push2 = tempbx;
+	   if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) {
+              if(SiS_Pr->SiS_TVMode & TVRPLLDIV2XO) VCLKIndex = HiTVVCLKDIV2;
+     	      else                                  VCLKIndex = HiTVVCLK;
+              if(SiS_Pr->SiS_TVMode & TVSetTVSimuMode) {
+            	 if(modeflag & Charx8Dot) 	    VCLKIndex = HiTVSimuVCLK;
+            	 else 			  	    VCLKIndex = HiTVTextVCLK;
+              }
+           } else if(SiS_Pr->SiS_TVMode & TVSetYPbPr750p) VCLKIndex = YPbPr750pVCLK - TVCLKBASE_315;
+	   else if(SiS_Pr->SiS_TVMode & TVSetYPbPr525p)   VCLKIndex = TVVCLKDIV2;
+	   else if(SiS_Pr->SiS_TVMode & TVRPLLDIV2XO)     VCLKIndex = TVVCLKDIV2;
+           else         		            	  VCLKIndex = TVVCLK;
 
-  tempcx >>= 1;
+	   if(HwInfo->jChipType < SIS_315H) {
+              VCLKIndex += TVCLKBASE_300;
+  	   } else {
+	      VCLKIndex += TVCLKBASE_315;
+	   }
 
-  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) &&
-     (SiS_Pr->SiS_CustomT != CUT_PANEL848)) {
-     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) {
-		      if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
-			    tempcx = 0x0002;
-#ifdef TWNEWPANEL
-			    tempcx = 0x0003;
-#endif
-		      } else {
-		            tempcx = 0x0002;   /* A901; sometimes 0x0003; */
-		      }
-		   } else tempcx = 0x0003;
+        } else {         					/* VGA2 */
+
+           VCLKIndex = (UCHAR)SiS_GetRegByte((USHORT)(SiS_Pr->SiS_P3ca+0x02));
+           VCLKIndex = ((VCLKIndex >> 2) & 0x03);
+           if(ModeNo > 0x13) {
+              VCLKIndex = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
+	      if(HwInfo->jChipType < SIS_315H) {
+          	 VCLKIndex &= 0x3f;
+		 if( (HwInfo->jChipType == SIS_630) &&
+		     (HwInfo->jChipRevision >= 0x30)) {
+		    if(VCLKIndex == 0x14) VCLKIndex = 0x34;
+		 }
+		 /* Better VGA2 clock for 1280x1024@75 */
+		 if(VCLKIndex == 0x17) VCLKIndex = 0x45;
+	      }
            }
-     	   else if(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 if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) tempcx = 0x0001;
-     	   else 							 tempcx = 0x0057;
-	}
+        }
+
+     } else {   /* If not programming CRT2 */
+
+        VCLKIndex = (UCHAR)SiS_GetRegByte((USHORT)(SiS_Pr->SiS_P3ca+0x02));
+        VCLKIndex = ((VCLKIndex >> 2) & 0x03);
+        if(ModeNo > 0x13) {
+           VCLKIndex = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
+	   if(HwInfo->jChipType < SIS_315H) {
+              VCLKIndex &= 0x3f;
+	      if( (HwInfo->jChipType != SIS_630) &&
+		  (HwInfo->jChipType != SIS_300) ) {
+		 if(VCLKIndex == 0x1b) VCLKIndex = 0x35;
+	      }
+	   }
+        }
      }
-  }
 
-  tempbx += tempcx;			 	/* BPLVRS  */
+  } else {       /*   LVDS  */
 
-  if((HwDeviceExtension->jChipType < SIS_315H) ||
-     (SiS_Pr->SiS_IF_DEF_FSTN) ||
-     (SiS_Pr->SiS_IF_DEF_DSTN)) {
-     tempbx++;
-  }
+     VCLKIndex = CRT2Index;
 
-  if(tempbx >= SiS_Pr->SiS_VT) tempbx -= SiS_Pr->SiS_VT;
+     if(SiS_Pr->SiS_SetFlag & ProgrammingCRT2) {  /* programming CRT2 */
 
-  temp = tempbx & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x18,temp);       	 /* Part1_18h; Panel Link Vertical Retrace Start  */
+        if( (SiS_Pr->SiS_IF_DEF_CH70xx != 0) && (SiS_Pr->SiS_VBInfo & SetCRT2ToTV) ) {
 
-  tempcx >>= 3;
+	   VCLKIndex &= 0x1f;
+           tempbx = 0;
+	   if(SiS_Pr->SiS_TVMode & TVSetCHOverScan) tempbx += 1;
+           if(SiS_Pr->SiS_TVMode & TVSetPAL) {
+	      tempbx += 2;
+	      if(SiS_Pr->SiS_ModeType > ModeVGA) {
+		 if(SiS_Pr->SiS_CHSOverScan) tempbx = 8;
+	      }
+	      if(SiS_Pr->SiS_TVMode & TVSetPALM) {
+		 tempbx = 4;
+		 if(SiS_Pr->SiS_TVMode & TVSetCHOverScan) tempbx += 1;
+	      } else if(SiS_Pr->SiS_TVMode & TVSetPALN) {
+		 tempbx = 6;
+		 if(SiS_Pr->SiS_TVMode & TVSetCHOverScan) tempbx += 1;
+	      }
+	   }
+       	   switch(tempbx) {
+             case  0: CHTVVCLKPtr = SiS_Pr->SiS_CHTVVCLKUNTSC;  break;
+             case  1: CHTVVCLKPtr = SiS_Pr->SiS_CHTVVCLKONTSC;  break;
+             case  2: CHTVVCLKPtr = SiS_Pr->SiS_CHTVVCLKUPAL;   break;
+             case  3: CHTVVCLKPtr = SiS_Pr->SiS_CHTVVCLKOPAL;   break;
+	     case  4: CHTVVCLKPtr = SiS_Pr->SiS_CHTVVCLKUPALM;  break;
+             case  5: CHTVVCLKPtr = SiS_Pr->SiS_CHTVVCLKOPALM;  break;
+             case  6: CHTVVCLKPtr = SiS_Pr->SiS_CHTVVCLKUPALN;  break;
+             case  7: CHTVVCLKPtr = SiS_Pr->SiS_CHTVVCLKOPALN;  break;
+	     case  8: CHTVVCLKPtr = SiS_Pr->SiS_CHTVVCLKSOPAL;  break;
+	     default: CHTVVCLKPtr = SiS_Pr->SiS_CHTVVCLKOPAL;   break;
+           }
+           VCLKIndex = CHTVVCLKPtr[VCLKIndex];
 
-  if((!(SiS_Pr->SiS_LCDInfo & LCDPass11)) &&
-     (SiS_Pr->SiS_CustomT != CUT_BARCO1366) &&
-     (SiS_Pr->SiS_CustomT != CUT_BARCO1024) &&
-     (SiS_Pr->SiS_CustomT != CUT_PANEL848)) {
-     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_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)  {
-     		if(HwDeviceExtension->jChipType < SIS_315H) {
-		        if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
-				tempcx = 0x0004;
-#ifdef TWNEWPANEL
-				tempcx = 0x0005;
-#endif
-		        } else {
-				tempcx = 0x0004;   /* A901; Other BIOS sets 0x0005; */
-			}
-		} else {
-			tempcx = 0x0005;
-		}
-        }
-     }
-  }
+        } else if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
 
-  tempcx = tempcx + tempbx + 1;                  /* BPLVRE  */
-  temp = tempcx & 0x000F;
-  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) ||
-     (SiS_Pr->SiS_CustomT == CUT_PANEL848)) {
-     temp |= 0x30;
-  }
-  SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x19,0xf0,temp); /* Part1_19h; Panel Link Vertical Retrace End (3:0); Misc.  */
+	   VCLKIndex >>= 6;
+     	   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)
+              VCLKIndex = LVDSXlatVCLK2[VCLKIndex];
+	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1152x768)
+              VCLKIndex = LVDSXlatVCLK2[VCLKIndex];
+	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768)
+	      VCLKIndex = VCLK68_315;
+	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200)
+	      VCLKIndex = VCLK162_315;
+     	   else
+	      VCLKIndex = LVDSXlatVCLK3[VCLKIndex];
+
+	   if(SiS_Pr->SiS_CustomT == CUT_BARCO1366) {
+	      /* Special Timing: Barco iQ Pro R series */
+	      VCLKIndex = 0x44;
+	   }
+
+	   if(SiS_Pr->SiS_CustomT == CUT_PANEL848) {
+	      if(HwInfo->jChipType < SIS_315H) {
+		 VCLKIndex = VCLK34_300;
+	         /* if(resinfo == SIS_RI_1360x768) VCLKIndex = ?; */
+	      } else {
+		 VCLKIndex = VCLK34_315;
+		 /* if(resinfo == SIS_RI_1360x768) VCLKIndex = ?; */
+	      }
+	   }
 
-  temp = ((tempbx & 0x0700) >> 8) << 3;          /* BPLDESKEW =0 */
-  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 {
+
+	   VCLKIndex = (UCHAR)SiS_GetRegByte((USHORT)(SiS_Pr->SiS_P3ca+0x02));
+           VCLKIndex = ((VCLKIndex >> 2) & 0x03);
+           if(ModeNo > 0x13) {
+              VCLKIndex = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
+	      if(HwInfo->jChipType < SIS_315H) {
+    	 	 VCLKIndex &= 0x3F;
+              }
+	      if( (HwInfo->jChipType == SIS_630) &&
+                  (HwInfo->jChipRevision >= 0x30) ) {
+		 if(VCLKIndex == 0x14) VCLKIndex = 0x2e;
+	      }
+	   }
         }
-     } else {
-	if( (HwDeviceExtension->jChipType == SIS_630) ||
-	    (HwDeviceExtension->jChipType == SIS_730) ) {
-	   if(HwDeviceExtension->jChipRevision >= 0x30) {
-	      temp |= 0x80;
+
+     } else {  /* if not programming CRT2 */
+
+        VCLKIndex = (UCHAR)SiS_GetRegByte((USHORT)(SiS_Pr->SiS_P3ca+0x02));
+        VCLKIndex = ((VCLKIndex >> 2) & 0x03);
+        if(ModeNo > 0x13) {
+           VCLKIndex = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
+           if(HwInfo->jChipType < SIS_315H) {
+	      VCLKIndex &= 0x3F;
+	      if( (HwInfo->jChipType != SIS_630) &&
+	          (HwInfo->jChipType != SIS_300) ) {
+		 if(VCLKIndex == 0x1b) VCLKIndex = 0x35;
+	      }
+#if 0
+	      if(HwInfo->jChipType == SIS_730) {
+		 if(VCLKIndex == 0x0b) VCLKIndex = 0x40;   /* 1024x768-70 */
+		 if(VCLKIndex == 0x0d) VCLKIndex = 0x41;   /* 1024x768-75 */
+	      }
+#endif
 	   }
-	}
+        }
+
      }
+
   }
-  SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x1A,0x87,temp);  /* Part1_1Ah; Panel Link Control Signal (7:3); Vertical Retrace Start (2:0) */
+  
+#ifdef TWDEBUG
+  xf86DrvMsg(0, X_INFO, "VCLKIndex %d (0x%x)\n", VCLKIndex, VCLKIndex);
+#endif
 
-  if (HwDeviceExtension->jChipType < SIS_315H) {
+  return(VCLKIndex);
+}
 
-#ifdef SIS300      /* 300 series */
+/*********************************************/
+/*        SET CRT2 MODE TYPE REGISTERS       */
+/*********************************************/
 
-        tempeax = SiS_Pr->SiS_VGAVDE << 6;
-        temp = (USHORT)(tempeax % (ULONG)SiS_Pr->SiS_VDE);
-        tempeax = tempeax / (ULONG)SiS_Pr->SiS_VDE;
-        if(temp != 0) tempeax++;
-        tempebx = tempeax;                         /* BPLVCFACT  */
+static void
+SiS_SetCRT2ModeRegs(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,
+                    PSIS_HW_INFO HwInfo)
+{
+  USHORT i,j,modeflag;
+  USHORT tempcl,tempah=0;
+#ifdef SIS300
+  USHORT temp;
+#endif
+#ifdef SIS315H
+  USHORT tempbl, tempah2, tempbl2;
+#endif
 
-  	if(SiS_Pr->SiS_SetFlag & EnableLVDSDDA) {
-	   tempebx = 0x003F;
-	}
+  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;
+     }
+  }
+  
+  /* BIOS does not do this (neither 301 nor LVDS) */
+  /* (But it's harmless; see SetCRT2Offset) */
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x03,0x00);   /* fix write part1 index 0  BTDRAM bit Bug */
 
-  	temp = (USHORT)(tempebx & 0x00FF);
-  	SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1E,temp);      /* Part1_1Eh; Panel Link Vertical Scaling Factor */
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
 
-#endif /* SIS300 */
+     SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x00,0xAF,0x40);
+     SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2E,0xF7);
 
   } else {
 
-#ifdef SIS315H  /* 315 series */
+     for(i=0,j=4; i<3; i++,j++) SiS_SetReg(SiS_Pr->SiS_Part1Port,j,0);
 
-        if(HwDeviceExtension->jChipType == SIS_740) {
-           SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x1E,0x03);
-        } else {
-	   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1E,0x23);
-	}
+     tempcl = SiS_Pr->SiS_ModeType;
 
-	tempeax = SiS_Pr->SiS_VGAVDE << 18;
-    	temp = (USHORT)(tempeax % (ULONG)SiS_Pr->SiS_VDE);
-    	tempeax = tempeax / SiS_Pr->SiS_VDE;
-    	if(temp != 0) tempeax++;
-    	tempebx = tempeax;                         /* BPLVCFACT  */
-        tempvcfact = tempeax;
-    	temp = (USHORT)(tempebx & 0x00FF);
-    	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; 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; Panel Link Vertical Scaling Factor */
+     if(HwInfo->jChipType < SIS_315H) {
 
-#endif /* SIS315H */
+#ifdef SIS300    /* ---- 300 series ---- */
 
-  }
+        /* For 301BDH: (with LCD via LVDS) */
+        if(SiS_Pr->SiS_VBType & VB_NoLCD) {
+	   temp = SiS_GetReg(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_SetReg(SiS_Pr->SiS_P3c4,0x32,temp);
+        }
 
-  tempbx = push2;                                  /* BPLVDEE  */
-  tempcx = push1;
+        if(ModeNo > 0x13) {
+           tempcl -= ModeVGA;
+           if((tempcl > 0) || (tempcl == 0)) {      /* tempcl is USHORT -> always true! */
+              tempah = ((0x10 >> tempcl) | 0x80);
+           }
+        } else tempah = 0x80;
 
-  push1 = temp;					   
+        if(SiS_Pr->SiS_VBInfo & SetInSlaveMode)  tempah ^= 0xA0;
 
-  if(!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
-   	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 == SIS_RI_1024x600) tempcx++;
-				if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
-					if(resinfo == SIS_RI_800x600) tempcx++;
-		    		}
-			} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600) {
-      				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 == SIS_RI_800x600)  tempcx++;
-			}
-		}
-	}
-  }
+#endif  /* SIS300 */
 
-  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480) {
-     if((!SiS_Pr->SiS_IF_DEF_FSTN) && (!SiS_Pr->SiS_IF_DEF_DSTN)) {
-        tempcx = SiS_Pr->SiS_VGAVDE;
-        tempbx = SiS_Pr->SiS_VGAVDE - 1;
-     }
-  }
+     } else {
 
-  temp = ((tempbx & 0x0700) >> 8) << 3;
-  temp |= ((tempcx & 0x0700) >> 8);
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1D,temp);     	/* Part1_1Dh; Vertical Display Overflow; Control Signal */
+#ifdef SIS315H    /* ------- 315/330 series ------ */
 
-  temp = tempbx & 0x00FF;
-  /* if(SiS_Pr->SiS_IF_DEF_FSTN) temp++;  */
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1C,temp);      	/* Part1_1Ch; Panel Link Vertical Display Enable End  */
+        if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
+           if(SiS_Pr->SiS_VBInfo & DisableCRT2Display) {
+	      SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2e,0x08);
+           }
+        }
 
-  temp = tempcx & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1B,temp);      	/* Part1_1Bh; Panel Link Vertical Display Enable Start  */
+        if(ModeNo > 0x13) {
+           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;
 
-  /* 3. Additional horizontal setup (scaling, etc) */
+        if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) tempah ^= 0x50;
 
-  tempecx = SiS_Pr->SiS_VGAHDE;
-  if(HwDeviceExtension->jChipType >= SIS_315H) {
-     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;
-  else {
-     tempeax = tempecx;
-     tempeax <<= 16;
-     temp = (USHORT)(tempeax % tempebx);
-     tempeax = tempeax / tempebx;
-     if(HwDeviceExtension->jChipType >= SIS_315H) {
-        if(temp) tempeax++;
-     }
-  }
-  tempecx = tempeax;
+#endif  /* SIS315H */
 
-  if(HwDeviceExtension->jChipType >= SIS_315H) {
-     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;
-  }
-  tempecx <<= 16;
-  tempecx |= (tempeax & 0xFFFF);
-  temp = (USHORT)(tempecx & 0x00FF);
-  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);
-  } else {
-     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) {
-     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;
-  }
+     if(SiS_Pr->SiS_VBInfo & DisableCRT2Display) tempah = 0;
 
-  temp = ((tempbx & 0xFF00) >> 8) << 3;
-  temp |= (USHORT)((tempecx & 0x0700) >> 8);
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x20,temp);  	/* Part1_20h; Overflow register */
+     if(HwInfo->jChipType < SIS_315H) {
+        SiS_SetReg(SiS_Pr->SiS_Part1Port,0x00,tempah);
+     } else {
+        if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
+           SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x00,0xa0,tempah);
+        } else if(SiS_Pr->SiS_VBType & VB_SISVB) {
+           if(IS_SIS740) {
+	      SiS_SetReg(SiS_Pr->SiS_Part1Port,0x00,tempah);
+	   } else {
+              SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x00,0xa0,tempah);
+	   }
+        }
+     }
 
-  temp = tempbx & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x21,temp);  	/* Part1_21h; Panel Link Vertical Accumulator Register */
+     if(SiS_Pr->SiS_VBType & VB_SISVB) {
 
-  tempecx >>= 16;                               /* BPLHCFACT  */
-  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; Panel Link Horizontal Scaling Factor High */
+        tempah = 0x01;
+        if(!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
+      	   tempah |= 0x02;
+        }
+        if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToRAMDAC)) {
+      	   tempah ^= 0x05;
+      	   if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD)) {
+              tempah ^= 0x01;
+      	   }
+        }
 
-  temp = (USHORT)(tempecx & 0x00FF);
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x23,temp);         /* Part1_22h; Panel Link Horizontal Scaling Factor Low */
+        if(SiS_Pr->SiS_VBInfo & DisableCRT2Display)  tempah = 0;
 
-  /* 630/301B and 630/LVDS do something for 640x480 panels here */
+        if(HwInfo->jChipType < SIS_315H) {
 
-#ifdef SIS315H
-  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);
+      	   tempah = (tempah << 5) & 0xFF;
+      	   SiS_SetReg(SiS_Pr->SiS_Part1Port,0x01,tempah);
+      	   tempah = (tempah >> 5) & 0xFF;
 
-     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);
+        } else {
 
-     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);
+      	   SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2E,0xF8,tempah);
 
-     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 */
+        if((SiS_Pr->SiS_ModeType == ModeVGA) && (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode))) {
+      	   tempah |= 0x10;
+        }
 
-}
+        if((HwInfo->jChipType < SIS_315H) && (SiS_Pr->SiS_VBType & VB_SIS301)) {
+	   if((SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) ||
+	      (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x960)) {
+	      tempah |= 0x80;
+	   }
+        } else {
+	   tempah |= 0x80;
+        }
 
-#ifdef SIS315H
-void
-SiS_CRT2AutoThreshold(SiS_Private *SiS_Pr, USHORT BaseAddr)
-{
-  SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x01,0x40);
-}
-#endif
+        if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+	   if(!(SiS_Pr->SiS_TVMode & (TVSetYPbPr750p | TVSetYPbPr525p))) {
+      	      if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
+                 tempah |= 0x20;
+	      }
+      	   }
+        }
 
+        SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x0D,0x40,tempah);
 
-#ifdef SIS315H
-/* 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)
-{
-  USHORT modeflag,resinfo;
-  USHORT push1,push2,tempax,tempbx,tempcx,temp;
-  ULONG tempeax=0,tempebx,tempecx,tempvcfact;
+        tempah = 0;
 
-  /* This is not supported with LCDA */
-  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_PanelCustom) return;
-  if(SiS_Pr->UseCustomMode) return;
+	if(SiS_IsDualLink(SiS_Pr, HwInfo)) tempah |= 0x40;
 
-  if(IS_SIS330) {
-     SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2D,0x10);			/* Xabre 1.01.03 */
-  } else if(IS_SIS740) {
-     if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {					/* 740/LVDS */
-        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, 301BDH */
-     }
-  } else {
-     if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {					/* 650/LVDS */
-        SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x13,0xfb,0x04);      	/* 650/LVDS */
-	SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2D,0x00);			/* 650/LVDS 1.10.07 */
-     } else {
-        SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2D,0x0f);			/* 650/30xLv 1.10.6s */
-     }
-  }
+        if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+	   if(SiS_Pr->SiS_TVMode & TVRPLLDIV2XO) {
+              tempah |= 0x40;
+       	   }
+        }
 
-  if(ModeNo <= 0x13) {
-    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;
-  }
+	if((SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) ||
+	   (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;
+        }
 
-  tempax = SiS_Pr->SiS_LCDHDES;
-  tempbx = SiS_Pr->SiS_HDE;
-  tempcx = SiS_Pr->SiS_HT;
+        SiS_SetReg(SiS_Pr->SiS_Part4Port,0x0C,tempah);
 
-  if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
-        tempbx = SiS_Pr->PanelXRes;
-  }
-  tempcx -= tempbx;                        	            	/* HT-HDE  */
-  push1 = tempax;
-  tempax += tempbx;	                                    	/* lcdhdee  */
-  tempbx = SiS_Pr->SiS_HT;
-  if(tempax >= tempbx)	tempax -= tempbx;
+     } else {  /* LVDS */
 
-  push2 = tempax;						/* push ax   lcdhdee  */
+        if(HwInfo->jChipType >= SIS_315H) {
 
-  tempcx >>= 2;
+	   /* LVDS can only be slave in 8bpp modes */
+	   tempah = 0x80;
+	   if((modeflag & CRT2Mode) && (SiS_Pr->SiS_ModeType > ModeVGA)) {
+	      if(SiS_Pr->SiS_VBInfo & DriverMode) {
+	         tempah |= 0x02;
+	      }
+	   }
 
-  /* 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) {
-        if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600)        tempcx = 0x28;
- 	else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768)  tempcx = 0x18;
-	else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) tempcx = 0x30;
-	else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) tempcx = 0x40;
-	else                                                          tempcx = 0x30;
-     }
-  }
+	   if(!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
+              tempah |= 0x02;
+    	   }
 
-  tempcx += tempax;  	                                  	/* lcdhrs  */
-  if(tempcx >= tempbx) tempcx -= tempbx;
-                                                           	/* v ah,cl  */
-  tempax = tempcx;
-  tempax >>= 3;   	                                     	/* BPLHRS */
-  temp = tempax & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x14,temp);                 	/* Part1_14h  */
+	   if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+	      tempah ^= 0x01;
+	   }
 
-  temp += 10;
-  if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
-     if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
-        if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480) {
-	   temp += 6;
-	   if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel800x600) {
-	      temp++;
-	      if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1024x768) {
-	         temp += 7;
-		 if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1600x1200) {
-		    temp -= 10;
-		 }
-	      }
+	   if(SiS_Pr->SiS_VBInfo & DisableCRT2Display) {
+	      tempah = 1;
 	   }
-	}
-     }
-  }
-  temp &= 0x1F;
-  temp |= ((tempcx & 0x07) << 5);
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x15,temp);                         /* Part1_15h  */
 
-  tempbx = push2;                                          	/* lcdhdee  */
-  tempcx = push1;                                          	/* lcdhdes  */
-  temp = (tempcx & 0x00FF);
-  temp &= 0x07;                                  		/* BPLHDESKEW  */
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1A,temp);                         /* Part1_1Ah  */
+    	   SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2e,0xF0,tempah);
 
-  tempcx >>= 3;   	                                     	/* BPLHDES */
-  temp = tempcx & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x16,temp);                         /* Part1_16h  */
+        } else {
 
-  if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
-     if(tempbx & 0x07) tempbx += 8;
-  }
-  tempbx >>= 3;                                        		/* BPLHDEE  */
-  temp = tempbx & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x17,temp);                        	/* Part1_17h  */
+	   tempah = 0;
+	   if( (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) && (SiS_Pr->SiS_ModeType > ModeVGA) ) {
+              tempah |= 0x02;
+    	   }
+	   tempah <<= 5;
 
-  tempcx = SiS_Pr->SiS_VGAVT;
-  tempbx = SiS_Pr->SiS_VGAVDE;
-  tempcx -= tempbx; 	                                   	/* GAVT-VGAVDE  */
-  tempbx = SiS_Pr->SiS_LCDVDES;                                	/* VGAVDES  */
-  push1 = tempbx;                                      		
-  if(SiS_Pr->SiS_IF_DEF_TRUMPION == 0) {
-     tempax = SiS_Pr->PanelYRes;
-  } else {
-     tempax = SiS_Pr->SiS_VGAVDE;
-  }
+	   if(SiS_Pr->SiS_VBInfo & DisableCRT2Display)  tempah = 0;
 
-  tempbx += tempax;
-  tempax = SiS_Pr->SiS_VT;                                    	/* VT  */
-  if(tempbx >= tempax)  tempbx -= tempax;
+	   SiS_SetReg(SiS_Pr->SiS_Part1Port,0x01,tempah);
 
-  push2 = tempbx;                                      		
- 
-  tempcx >>= 2;	
+        }
 
-  /* 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) {
-        if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600)         tempcx = 1;
-   	else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768)   tempcx = 3;
-	else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768)   tempcx = 3;
-	else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024)  tempcx = 1;
-	else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050)  tempcx = 1;
-	else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200)  tempcx = 1;
-	else                                                           tempcx = 0x0057;
      }
-  }
 
-  tempbx += tempcx;
-  if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
-     tempbx++;                                                	/* BPLVRS  */
-  }
-  if(tempbx >= tempax)   tempbx -= tempax;
-  temp = tempbx & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x18,temp);                             /* Part1_18h  */
+  }  /* LCDA */
 
-  tempcx >>= 3;
-  if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
-     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 = 3;
-   	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768)   tempcx = 5;
-	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768)   tempcx = 5;
-	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024)  tempcx = 5;
-	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050)  tempcx = 2;
-	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200)  tempcx = 2;
+  if(SiS_Pr->SiS_VBType & VB_SISVB) {
+
+     if(HwInfo->jChipType >= SIS_315H) {
+
+#ifdef SIS315H
+
+        unsigned char bridgerev = SiS_GetReg(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, HwInfo)) {
+	         tempbl = 0xff;
+	      }
+           }
+           SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x13,tempbl,tempah);
 	}
-     }
-  }
-  tempcx += tempbx;
-  tempcx++;                                                	/* BPLVRE  */
-  temp = tempcx & 0x00FF;
-  temp &= 0x0F;
-  if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
-     SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x19,0xF0,temp);
-  } else {
-     /* 650/30xLV 1.10.6s, Xabre */
-     temp |= 0xC0;
-     SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x19,0xF0,temp);             /* Part1_19h  */
-  }
 
-  temp = (tempbx & 0xFF00) >> 8;
-  temp &= 0x07;
-  temp <<= 3;  		                               		/* BPLDESKEW =0 */
-  tempbx = SiS_Pr->SiS_VGAVDE;
-  if(tempbx != SiS_Pr->SiS_VDE)              temp |= 0x40;
-  if(SiS_Pr->SiS_SetFlag & EnableLVDSDDA)    temp |= 0x40;
-  if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
-     if(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit) {
-        if(SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x00) & 0x01) temp |= 0x80;
-     }
-  } else {
-     if(IS_SIS650) {
-        /* 650/30xLV 1.10.6s */
-        if(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit) {
-           if(SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x00) & 0x01) temp |= 0x80;
-        }
-     } else {
-	if(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit)  temp |= 0x80;
-     }
-  }
-  SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x1A,0x87,temp);            /* Part1_1Ah */
+	/* 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; P4_23=0xe5); the b1 version
+	 * in a 650 box (Jake). What is the criteria?
+	 */
 
-  tempbx = push2;                                      		/* BPLVDEE  */
-  tempcx = push1;                                      		/* NPLVDES */
-  push1 = (USHORT)(tempeax & 0xFFFF);
+	if((IS_SIS740) || (HwInfo->jChipType >= SIS_661)) {
+	   tempah = 0x30;
+	   tempbl = 0xc0;
+	   if(SiS_Pr->SiS_VBInfo & DisableCRT2Display) {
+	      tempah = 0x00;
+	      tempbl = 0x00;
+	   }
+	   SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2c,0xcf,tempah);
+	   SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x21,0x3f,tempbl);
+	} 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   */
+	   SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x21,0x3f);
+	} else if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
+	   SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2c,0x30);      /* For 30xLV */
+	   SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x21,0xc0);
+	} else if((SiS_Pr->SiS_VBType & VB_NoLCD) && (bridgerev == 0xb0)) {
+	   SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2c,0x30);      /* For 30xB-DH rev b0 (or "DH on 651"?) */
+	   SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x21,0xc0);
+	} else {
+	   tempah = 0x30; tempah2 = 0xc0;		       /* For 30xB (and 301BDH rev b1) */
+	   tempbl = 0xcf; tempbl2 = 0x3f;
+	   if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)) {
+	      tempah = tempah2 = 0x00;
+	      if(SiS_IsDualEdge(SiS_Pr, HwInfo)) {
+		 tempbl = tempbl2 = 0xff;
+	      }
+	   }
+	   SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2c,tempbl,tempah);
+	   SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x21,tempbl2,tempah2);
+	}
 
-  if(!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
-    if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600) {
-      if(resinfo == SIS_RI_800x600) tempcx++;
-    }
-  }
-  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480) {
-    tempbx = SiS_Pr->SiS_VGAVDE;
-    tempcx = tempbx;
-    tempbx--;
-  }
+	if(IS_SIS740) {
+	   tempah = 0x80;
+	   if(SiS_Pr->SiS_VBInfo & DisableCRT2Display) {
+	      tempah = 0x00;
+	   }
+	   SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x23,0x7f,tempah);
+	} else {
+	   tempah = 0x00;
+           tempbl = 0x7f;
+           if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)) {
+              tempbl = 0xff;
+	      if(!(SiS_IsDualEdge(SiS_Pr, HwInfo))) {
+	         tempah = 0x80;
+	      }
+           }
+           SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x23,tempbl,tempah);
+	}
 
-  temp = (tempbx & 0xFF00) >> 8;
-  temp &= 0x07;
-  temp <<= 3;
-  temp = temp | (((tempcx & 0xFF00) >> 8) & 0x07);
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1D,temp);                          /* Part1_1Dh */
+	/* 661: Sets p4 27 and 34 here, done in SetGroup4 here */
 
-  temp = tempbx & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1C,temp);                          /* Part1_1Ch  */
+#endif /* SIS315H */
 
-  temp = tempcx & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1B,temp);                          /* Part1_1Bh  */
+     } else if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
 
-  tempecx = SiS_Pr->SiS_VGAVT;
-  tempebx = SiS_Pr->SiS_VDE;
-  tempeax = SiS_Pr->SiS_VGAVDE;
-  tempecx -= tempeax;    	                             	/* VGAVT-VGAVDE  */
-  tempeax <<= 18;
-  temp = (USHORT)(tempeax % tempebx);
-  tempeax = tempeax / tempebx;
-  if(temp)  tempeax++;
-  tempebx = tempeax;                                        	/* BPLVCFACT  */
-  tempvcfact = tempeax;
-  temp = (USHORT)(tempebx & 0x00FF);
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x37,temp);
+        SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x21,0x3f);
 
-  temp = (USHORT)((tempebx & 0x00FF00) >> 8);
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x36,temp);
+        if((SiS_Pr->SiS_VBInfo & DisableCRT2Display) ||
+           (   (SiS_Pr->SiS_VBType & VB_NoLCD) &&
+	       (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) ) ) {
+	   SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x23,0x7F);
+	} else {
+	   SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x23,0x80);
+	}
 
-  temp = (USHORT)((tempebx & 0x00030000) >> 16);
-  if(SiS_Pr->SiS_VDE == SiS_Pr->SiS_VGAVDE) temp |= 0x04;
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x35,temp);
+     }
 
-  tempecx = SiS_Pr->SiS_VGAHDE;
-  if(modeflag & HalfDCLK) tempecx >>= 1;
-  tempebx = SiS_Pr->SiS_HDE;
-  tempeax = tempecx;
-  tempeax <<= 16;
-  temp = tempeax % tempebx;
-  tempeax = tempeax / tempebx;
-  if(temp) tempeax++;
-  if(tempebx == tempecx)  tempeax = 0xFFFF;
-  tempecx = tempeax;
-  tempeax = SiS_Pr->SiS_VGAHDE;
-  if(modeflag & HalfDCLK) tempeax >>= 1;
-  tempeax <<= 16;
-  tempeax = tempeax / tempecx;
-  tempecx <<= 16;
-  tempeax--;
-  tempecx = tempecx | (tempeax & 0xFFFF);
-  temp = (USHORT)(tempecx & 0x00FF);
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1F,temp);                          /* Part1_1Fh  */
+  } else {  /* LVDS */
 
-  tempeax = SiS_Pr->SiS_VGAVDE;
-  tempeax <<= 18;
-  tempeax = tempeax / tempvcfact;
-  tempbx = (USHORT)(tempeax & 0x0FFFF);
+#ifdef SIS315H
+     if(HwInfo->jChipType >= SIS_315H) {
 
-  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) tempbx--;
+        if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
 
-  if(SiS_Pr->SiS_SetFlag & EnableLVDSDDA)  tempbx = 1;
+           tempah = 0x04;
+	   tempbl = 0xfb;
+           if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)) {
+              tempah = 0x00;
+	      if(SiS_IsDualEdge(SiS_Pr, HwInfo)) {
+	         tempbl = 0xff;
+	      }
+           }
+	   SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x13,tempbl,tempah);
 
-  temp = ((tempbx & 0xFF00) >> 8) << 3;
-  temp = temp | (USHORT)(((tempecx & 0x0000FF00) >> 8) & 0x07);
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x20,temp);                         /* Part1_20h */
+	   if(SiS_Pr->SiS_VBInfo & DisableCRT2Display) {
+	      SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x13,0xfb);
+	   }
 
-  temp = tempbx & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x21,temp);                         /* Part1_21h */
+	   SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2c,0x30);
 
-  tempecx >>= 16;   	                                  	/* BPLHCFACT  */
-  if(modeflag & HalfDCLK) tempecx >>= 1;
-  temp = (USHORT)((tempecx & 0x0000FF00) >> 8);
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x22,temp);                         /* Part1_22h */
+	} else if(HwInfo->jChipType == SIS_550) {
 
-  temp=(USHORT)(tempecx & 0x000000FF);
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x23,temp);
+	   SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x13,0xfb);
+	   SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2c,0x30);
 
-#if 0
-  /* 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
 
-  /* 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);
   }
 
 }
-#endif  /* SIS 315 */
 
-void SiS_SetCRT2Offset(SiS_Private *SiS_Pr, UCHAR *ROMAddr,USHORT ModeNo,
-                       USHORT ModeIdIndex ,USHORT RefreshRateTableIndex,
-		       PSIS_HW_DEVICE_INFO HwDeviceExtension)
-{
-  USHORT offset;
-  UCHAR temp;
-
-  if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) return;
+/*********************************************/
+/*            GET RESOLUTION DATA            */
+/*********************************************/
 
-  offset = SiS_GetOffset(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
-                         HwDeviceExtension);
+USHORT
+SiS_GetResInfo(SiS_Private *SiS_Pr,USHORT ModeNo,USHORT ModeIdIndex)
+{
+  USHORT resindex;
 
-  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_2 ||
-     SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_3) offset >>= 1;
+  if(ModeNo <= 0x13)
+     resindex = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ResInfo;
+  else
+     resindex = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO;
 
-  temp = (UCHAR)(offset & 0xFF);
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x07,temp);
-  temp = (UCHAR)((offset & 0xFF00) >> 8);
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x09,temp);
-  temp = (UCHAR)(((offset >> 3) & 0xFF) + 1);
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x03,temp);
+  return(resindex);
 }
 
-USHORT
-SiS_GetOffset(SiS_Private *SiS_Pr, UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex,
-              USHORT RefreshRateTableIndex,PSIS_HW_DEVICE_INFO HwDeviceExtension)
+static void
+SiS_GetCRT2ResInfo(SiS_Private *SiS_Pr,USHORT ModeNo,USHORT ModeIdIndex,
+                   PSIS_HW_INFO HwInfo)
 {
-  USHORT temp,colordepth;
-  USHORT modeinfo,index,infoflag;
+  USHORT xres,yres,modeflag=0,resindex;
 
   if(SiS_Pr->UseCustomMode) {
-     infoflag = SiS_Pr->CInfoFlag;
-     temp = SiS_Pr->CHDisplay / 16;
-  } else {
-     infoflag = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
-     modeinfo = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeInfo;
-     index = (modeinfo >> 8) & 0xFF;
-     temp = SiS_Pr->SiS_ScreenOffset[index];
+     SiS_Pr->SiS_VGAHDE = SiS_Pr->SiS_HDE = SiS_Pr->CHDisplay;
+     SiS_Pr->SiS_VGAVDE = SiS_Pr->SiS_VDE = SiS_Pr->CVDisplay;
+     return;
   }
-  
-  colordepth = SiS_GetColorDepth(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex);
-
-  if(infoflag & InterlaceMode) temp <<= 1;
 
-  temp *= colordepth;
+  resindex = SiS_GetResInfo(SiS_Pr,ModeNo,ModeIdIndex);
 
-  if( ( ((ModeNo >= 0x26) && (ModeNo <= 0x28)) ||
-        ModeNo == 0x3f ||
-	ModeNo == 0x42 || 
-	ModeNo == 0x45 ) ||
-      (SiS_Pr->UseCustomMode && (SiS_Pr->CHDisplay % 16)) ) {
-        colordepth >>= 1;
-	temp += colordepth;
+  if(ModeNo <= 0x13) {
+     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;
   }
 
-  return(temp);
-}
+  if((!SiS_Pr->SiS_IF_DEF_DSTN) && (!SiS_Pr->SiS_IF_DEF_FSTN)) {
 
-USHORT
-SiS_GetColorDepth(SiS_Private *SiS_Pr, UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex)
-{
-  USHORT ColorDepth[6] = { 1, 2, 4, 4, 6, 8};
-  SHORT  index;
-  USHORT modeflag;
+     if((HwInfo->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_GetReg(SiS_Pr->SiS_P3d4,0x3a) & 0x01) {
+ 	   if(ModeNo == 0x12) yres = 400;
+        }
+     }
 
-  /* Do NOT check UseCustomMode, will skrew up FIFO */
-  if(ModeNo == 0xfe) {
-     modeflag = SiS_Pr->CModeFlag;
-  } else {
-     if(ModeNo <= 0x13)
-    	modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
-     else
-    	modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
-  }	
+     if(ModeNo > 0x13) {
+  	if(modeflag & HalfDCLK)       xres *= 2;
+  	if(modeflag & DoubleScanMode) yres *= 2;
+     }
+
+  }
 
-  index = (modeflag & ModeInfoFlag) - ModeEGA;
-  if(index < 0) index = 0;
-  return(ColorDepth[index]);
+  if(SiS_Pr->SiS_VBType & VB_SISVB) {
+        if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
+           if(xres == 720) xres = 640;
+	} else {
+	   if(SiS_Pr->SiS_VBType & VB_NoLCD) {           /* 301BDH */
+	        if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToHiVision)) {
+                   if(xres == 720) xres = 640;
+		}
+		if(SiS_Pr->SiS_SetFlag & SetDOSMode) {
+	           yres = 400;
+	           if(HwInfo->jChipType >= SIS_315H) {
+	              if(SiS_GetReg(SiS_Pr->SiS_P3c4,0x17) & 0x80) yres = 480;
+	           } else {
+	              if(SiS_GetReg(SiS_Pr->SiS_P3c4,0x13) & 0x80) yres = 480;
+	           }
+	        }
+	   } else {
+	      if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToHiVision)) {
+	         if(xres == 720) xres = 640;
+	      }
+	      if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
+		 if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) {
+		    if(!(SiS_Pr->SiS_SetFlag & LCDVESATiming)) {
+        	       if(yres == 1024) yres = 1056;
+      		    }
+		 } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
+		    if(!(SiS_Pr->SiS_LCDInfo & DontExpandLCD)) {
+		       /* BIOS bug - does this regardless of scaling */
+      		       if(yres == 400) yres = 405;
+		    }
+      		    if(yres == 350) yres = 360;
+      		    if(SiS_Pr->SiS_SetFlag & LCDVESATiming) {
+        	       if(yres == 360) yres = 375;
+      		    }
+   	         } 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;
+          	          if(yres == 400) yres = 420;
+            	          if(yres == 480) yres = 525;
+        	       }
+      		    }
+    	         }
+	      }
+	   }
+	}
+  } else {
+    	if(xres == 720) xres = 640;
+	if(SiS_Pr->SiS_SetFlag & SetDOSMode) {
+	   yres = 400;
+	   if(HwInfo->jChipType >= SIS_315H) {
+	      if(SiS_GetReg(SiS_Pr->SiS_P3c4,0x17) & 0x80) yres = 480;
+	   } else {
+	      if(SiS_GetReg(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;
+  SiS_Pr->SiS_VGAVDE = SiS_Pr->SiS_VDE = yres;
 }
 
-void
-SiS_SetCRT2Sync(SiS_Private *SiS_Pr, USHORT BaseAddr,UCHAR *ROMAddr,USHORT ModeNo,
-                USHORT RefreshRateTableIndex,PSIS_HW_DEVICE_INFO HwDeviceExtension)
-{
-  USHORT tempah=0,tempbl,infoflag,flag;
+/*********************************************/
+/*           GET CRT2 TIMING DATA            */
+/*********************************************/
+
+static BOOLEAN
+SiS_GetLVDSCRT1Ptr(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,
+		   USHORT RefreshRateTableIndex, USHORT *ResIndex,
+		   USHORT *DisplayType)
+ {
+  USHORT tempbx,modeflag=0;
+  USHORT Flag,CRT2CRTC;
 
-  flag = 0;
-  tempbl = 0xC0;
+  if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
+     if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)) {
+        if(!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) return FALSE;
+     }
+  } else if(SiS_Pr->SiS_VBType & VB_SISVB) {
+     if(!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) return FALSE;
+  } else
+     return FALSE;
 
-  if(SiS_Pr->UseCustomMode) {
-     infoflag = SiS_Pr->CInfoFlag;
+  if(ModeNo <= 0x13) {
+     modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
+     CRT2CRTC = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_CRT2CRTC;
   } else {
-     infoflag = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
+     modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
+     CRT2CRTC = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
   }
 
-  if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {					/* LVDS */
+  Flag = 1;
+  tempbx = 0;
+  if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
+     if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD)) {
+        Flag = 0;
+        tempbx = 18;
+        if(SiS_Pr->SiS_TVMode & TVSetCHOverScan) tempbx++;
+        if(SiS_Pr->SiS_TVMode & TVSetPAL) {
+      	   tempbx += 2;
+	   if(SiS_Pr->SiS_ModeType > ModeVGA) {
+	      if(SiS_Pr->SiS_CHSOverScan) tempbx = 99;
+	   }
+	   if(SiS_Pr->SiS_TVMode & TVSetPALM) {
+	      tempbx = 18;  /* PALM uses NTSC data */
+	      if(SiS_Pr->SiS_TVMode & TVSetCHOverScan) tempbx++;
+	   } else if(SiS_Pr->SiS_TVMode & TVSetPALN) {
+	      tempbx = 20;  /* PALN uses PAL data  */
+	      if(SiS_Pr->SiS_TVMode & TVSetCHOverScan) tempbx++;
+	   }
+        }
+     }
+  }
+  if(Flag) {
+     tempbx = SiS_Pr->SiS_LCDResInfo;
+     tempbx -= SiS_Pr->SiS_PanelMinLVDS;
+     if(SiS_Pr->SiS_LCDResInfo <= SiS_Pr->SiS_Panel1280x1024) {
+        if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) tempbx += 6;
+        if(modeflag & HalfDCLK) tempbx += 3;
+     } else {
+        if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) {
+           tempbx = 14;
+	   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) tempbx += 2;
+	   if(modeflag & HalfDCLK) tempbx++;
+        } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x600) {
+           tempbx = 23;
+	   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) tempbx += 2;
+	   if(modeflag & HalfDCLK) tempbx++;
+        } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1152x768) {
+           tempbx = 27;
+	   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) tempbx += 2;
+	   if(modeflag & HalfDCLK) tempbx++;
+        } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) {
+           tempbx = 36;
+	   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) tempbx += 2;
+	   if(modeflag & HalfDCLK) tempbx++;
+        } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768) {
+           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_VBInfo & SetCRT2ToTV) {
-       tempah = 0;
-    } 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_LCDInfo & LCDPass11) {
+        tempbx = 12;
+	if(modeflag & HalfDCLK) tempbx++;
+     }
+  }
 
-    if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
-       if(HwDeviceExtension->jChipType >= SIS_315H) {
-          tempah >>= 3;
-          SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x13,0xE7,tempah);
-       }
-    } else {
-       SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x19,0x0F,tempah);
-    }
+#if 0
+  if(SiS_Pr->SiS_IF_DEF_FSTN) {
+     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel320x480){
+        tempbx = 22;
+     }
+  }
+#endif
 
-  } else {
+  *ResIndex = CRT2CRTC & 0x3F;
+  *DisplayType = tempbx;
+  return TRUE;
+}
 
-     if(HwDeviceExtension->jChipType < SIS_315H) {
+static void
+SiS_GetCRT2Ptr(SiS_Private *SiS_Pr,USHORT ModeNo,USHORT ModeIdIndex,
+	       USHORT RefreshRateTableIndex,USHORT *CRT2Index,USHORT *ResIndex,
+	       PSIS_HW_INFO HwInfo)
+{
+  USHORT tempbx=0,tempal=0;
+  USHORT Flag,resinfo=0;
 
-#ifdef SIS300  /* ---- 300 series --- */
+  if(ModeNo <= 0x13) {
+     tempal = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_CRT2CRTC;
+  } else {
+     tempal = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
+     resinfo = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO;
+  }
 
-        if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {			/* 630 - 301B(-DH) */
+  if((SiS_Pr->SiS_VBType & VB_SISVB) && (SiS_Pr->SiS_IF_DEF_LVDS == 0)) {
 
-            if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
-               tempah = SiS_Pr->SiS_LCDInfo;
-	       if(SiS_Pr->SiS_LCDInfo & LCDSync) {
-                  flag = 1;
-               }
-            }
-            if(flag != 1) tempah = infoflag >> 8;
-            tempah &= 0xC0;
-	    
-            tempah |= 0x20;
-            if(!(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit)) tempah |= 0x10;
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {                            /* LCD */
 
+	if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x960) {
+	   tempbx = 15;
+  	} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) {
+	   tempbx = 20;
+	   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)         tempbx = 21;
+	   else if(!(SiS_Pr->SiS_SetFlag & LCDVESATiming)) tempbx = 22;
+ 	} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) {
+	   tempbx = 23;
+	   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)         tempbx = 24;
+	   else if(!(SiS_Pr->SiS_SetFlag & LCDVESATiming)) tempbx = 25;
 #if 0
-            if (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480) {
-	       	/* BIOS does something here @@@ */
-            }
+	} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768) {
+	   tempbx = 26;
+	   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)         tempbx = 27;
+	   else if(!(SiS_Pr->SiS_SetFlag & LCDVESATiming)) tempbx = 28;
+#endif
+ 	} else if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
+	   if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
+	      if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768)       tempbx = 13;
+	      else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) tempbx = 14;
+	      else {
+	         tempbx = 29;
+		 if(ModeNo >= 0x13) {
+	            /* see below */
+	            if(resinfo == SIS_RI_1280x960) tempal = 10;
+	         }
+              }
+	   } else {
+	      tempbx = 29;
+	      if(ModeNo >= 0x13) {
+	         /* 1280x768 and 1280x960 have same CRT2CRTC,
+	          * so we change it here if 1280x960 is chosen
+	          */
+	         if(resinfo == SIS_RI_1280x960) tempal = 10;
+	      }
+   	   }
+	} else {
+      	   tempbx = SiS_Pr->SiS_LCDResInfo - SiS_Pr->SiS_Panel1024x768;
+      	   if(!(SiS_Pr->SiS_SetFlag & LCDVESATiming)) {
+              tempbx += 10;
+       	   }
+	}
+
+#ifdef SIS315H
+	if(SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) {
+	   if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
+	      tempbx = 50;
+	      if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)         tempbx = 51;
+	      else if(!(SiS_Pr->SiS_SetFlag & LCDVESATiming)) tempbx = 52;
+	   }
+	}
 #endif
 
- 	    tempah &= 0x3f;
-  	    tempah |= tempbl;
-            SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x19,0x0F,tempah);
-
-         } else {							/* 630 - 301 */
-
-            tempah = infoflag >> 8;
-            tempah &= 0xC0;
-            tempah |= 0x20;
-	    if(!(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit)) tempah |= 0x10;
-            SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x19,0x0F,tempah);
+     } else {						  	/* TV */
 
-         }
+     	if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) {
+           if(SiS_Pr->SiS_VGAVDE > 480) SiS_Pr->SiS_TVMode &= (~TVSetTVSimuMode);
+           tempbx = 2;
+           if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
+              if(!(SiS_Pr->SiS_TVMode & TVSetTVSimuMode)) tempbx = 13;
+           }
+	} else if(SiS_Pr->SiS_VBInfo & SetCRT2ToYPbPr525750) {
+	   if(SiS_Pr->SiS_TVMode & TVSetYPbPr750p)      tempbx = 7;
+	   else if(SiS_Pr->SiS_TVMode & TVSetYPbPr525p) tempbx = 6;
+	   else 					tempbx = 5;
+	   if(SiS_Pr->SiS_TVMode & TVSetTVSimuMode)     tempbx += 5;
+       	} else {
+           if(SiS_Pr->SiS_TVMode & TVSetPAL) 		tempbx = 3;
+           else 					tempbx = 4;
+           if(SiS_Pr->SiS_TVMode & TVSetTVSimuMode) 	tempbx += 5;
+       	}
 
-#endif /* SIS300 */
+     }
 
-      } else {
+     tempal &= 0x3F;
 
-#ifdef SIS315H  /* ------- 315 series ------ */
+     if(ModeNo > 0x13) {
+        if(SiS_Pr->SiS_VBInfo & SetCRT2ToTVNoHiVision) {
+      	   if(tempal == 6) tempal = 7;
+           if((resinfo == SIS_RI_720x480) ||
+	      (resinfo == SIS_RI_720x576) ||
+	      (resinfo == SIS_RI_768x576)) {
+	      tempal = 6;
+	   }
+	   if(SiS_Pr->SiS_VBInfo & SetCRT2ToYPbPr525750) {
+              if(SiS_Pr->SiS_TVMode & TVSetYPbPr525p) {
+	         if(resinfo == SIS_RI_1024x768) {
+	            tempal = 8;
+	         }
+	      }
+	   }
+	}
+     }
 
-         if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {	  		/* 315 - 30xLV */
+     *CRT2Index = tempbx;
+     *ResIndex = tempal;
 
-	    if(SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) {
-	       tempah = infoflag >> 8;
-	       if(SiS_Pr->SiS_LCDInfo & LCDSync) {
-	          tempah = SiS_Pr->SiS_LCDInfo;
-	       }
-	    } else {
-               tempah = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x37);
-	    }
-	    tempah &= 0xC0;
+  } else {   /* LVDS, 301B-DH (if running on LCD) */
 
-            tempah |= 0x20;
-            if(!(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit)) tempah |= 0x10;
-            SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x19,0x0F,tempah);
-
-         } else {							/* 315 - 301, 301B */
-
-            tempah = infoflag >> 8;
-	    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) {
-		/* BIOS does something here @@@ */
-            }
-#endif
+     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_TVMode & TVSetCHOverScan) tempbx += 1;
+           if(SiS_Pr->SiS_TVMode & TVSetPAL) {
+	      tempbx += 2;
+	      if(SiS_Pr->SiS_ModeType > ModeVGA) {
+		 if(SiS_Pr->SiS_CHSOverScan) tempbx = 99;
+	      }
+	      if(SiS_Pr->SiS_TVMode & TVSetPALM) {
+		 tempbx = 90;
+		 if(SiS_Pr->SiS_TVMode & TVSetCHOverScan) tempbx += 1;
+	      } else if(SiS_Pr->SiS_TVMode & TVSetPALN) {
+		 tempbx = 92;
+		 if(SiS_Pr->SiS_TVMode & TVSetCHOverScan) tempbx += 1;
+	      }
+           }
+        }
+     }
 
-	    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);
+     if(Flag) {
 
-         } 
-	 
-#endif  /* SIS315H */
-      }
-   }
-}
+	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;
 
-/* Set CRT2 FIFO on 300/630/730 */
-#ifdef SIS300
-void
-SiS_SetCRT2FIFO_300(SiS_Private *SiS_Pr, UCHAR *ROMAddr,USHORT ModeNo,
-                    PSIS_HW_DEVICE_INFO HwDeviceExtension)
-{
-  USHORT temp,index;
-  USHORT modeidindex,refreshratetableindex;
-  USHORT VCLK=0,MCLK,colorth=0,data2=0;
-  USHORT tempal, tempah, tempbx, tempcl, tempax;
-  USHORT CRT1ModeNo,CRT2ModeNo;
-  USHORT SelectRate_backup;
-  ULONG  data,eax;
-  const UCHAR  LatencyFactor[] = {
-  	97, 88, 86, 79, 77, 00,       /*; 64  bit    BQ=2   */
-        00, 87, 85, 78, 76, 54,       /*; 64  bit    BQ=1   */
-        97, 88, 86, 79, 77, 00,       /*; 128 bit    BQ=2   */
-        00, 79, 77, 70, 68, 48,       /*; 128 bit    BQ=1   */
-        80, 72, 69, 63, 61, 00,       /*; 64  bit    BQ=2   */
-        00, 70, 68, 61, 59, 37,       /*; 64  bit    BQ=1   */
-        86, 77, 75, 68, 66, 00,       /*; 128 bit    BQ=2   */
-        00, 68, 66, 59, 57, 37        /*; 128 bit    BQ=1   */
-  };
-  const UCHAR  LatencyFactor730[] = {
-         69, 63, 61, 
-	 86, 79, 77,
-	103, 96, 94,
-	120,113,111,
-	137,130,128,    /* <-- last entry, data below */
-	137,130,128,	/* to avoid using illegal values */
-	137,130,128,
-	137,130,128,
-	137,130,128,
-	137,130,128,
-	137,130,128,
-	137,130,128,
-	137,130,128,
-	137,130,128,
-	137,130,128,
-	137,130,128,
-  };
-  const UCHAR ThLowB[]   = {
-  	81, 4, 72, 6, 88, 8,120,12,
-        55, 4, 54, 6, 66, 8, 90,12,
-        42, 4, 45, 6, 55, 8, 75,12
-  };
-  const UCHAR ThTiming[] = {
-  	1, 2, 2, 3, 0, 1, 1, 2
-  };
-  
-  SelectRate_backup = SiS_Pr->SiS_SelectCRT2Rate;
+	   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) {
+	   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;
+	} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1152x768) {
+	   tempbx = 16;
+	   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)  tempbx += 2;
+	} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) {
+	   tempbx = 8;
+	   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)  tempbx++;
+	} 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->CRT1UsesCustomMode) {
-  
-     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;
-     refreshratetableindex = SiS_GetRatePtrCRT2(SiS_Pr,ROMAddr,CRT1ModeNo,
-						modeidindex,HwDeviceExtension);
+	if(SiS_Pr->SiS_LCDInfo & LCDPass11) {
+	   tempbx = 7;
+        }
 
-     if(CRT1ModeNo >= 0x13) {
-        index = SiS_Pr->SiS_RefIndex[refreshratetableindex].Ext_CRTVCLK;
-        index &= 0x3F;
-        VCLK = SiS_Pr->SiS_VCLKData[index].CLOCK;				/* Get VCLK */
+	if(SiS_Pr->SiS_CustomT == CUT_PANEL848) {
+	   tempbx = 84;
+	   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)  tempbx++;
+	}
 
-	colorth = SiS_GetColorDepth(SiS_Pr,ROMAddr,CRT1ModeNo,modeidindex); 	/* Get colordepth */
-        colorth >>= 1;
-        if(!colorth) colorth++;
      }
 
-  } else {
-  
-     CRT1ModeNo = 0xfe;
-     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(SiS_Pr->SiS_SetFlag & SetDOSMode) {
+        if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480) tempal = 7;
+  	if(HwInfo->jChipType < SIS_315H) {
+	   if(SiS_GetReg(SiS_Pr->SiS_P3c4,0x13) & 0x80) tempal++;
+	}
      }
 
+     *CRT2Index = tempbx;
+     *ResIndex = tempal & 0x1F;
   }
+}
 
-  if(CRT1ModeNo >= 0x13) {
-    if(HwDeviceExtension->jChipType == SIS_300) {
-       index = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x3A);
-    } else {
-       index = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x1A);
-    }
-    index &= 0x07;
-    MCLK = SiS_Pr->SiS_MCLKData_0[index].CLOCK;				/* Get MCLK */
+#ifdef SIS315H
+static void
+SiS_GetCRT2PtrA(SiS_Private *SiS_Pr,USHORT ModeNo,USHORT ModeIdIndex,
+		USHORT RefreshRateTableIndex,USHORT *CRT2Index,
+		USHORT *ResIndex)
+{
+  USHORT tempbx,tempal;
 
-    data2 = (colorth * VCLK) / MCLK;
+  tempbx = SiS_Pr->SiS_LCDResInfo;
 
-    temp = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x14);
-    temp = ((temp & 0x00FF) >> 6) << 1;
-    if(temp == 0) temp = 1;
-    temp <<= 2;
-    temp &= 0xff;
+  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200)      tempbx = 4;
+  else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) tempbx = 3;
+  else tempbx -= SiS_Pr->SiS_Panel1024x768;
 
-    data2 = temp - data2;
-    
-    if((28 * 16) % data2) {
-      	data2 = (28 * 16) / data2;
-      	data2++;
-    } else {
-      	data2 = (28 * 16) / data2;
-    }
+  if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)  tempbx += 5;
 
-    if(HwDeviceExtension->jChipType == SIS_300) {
+  if(ModeNo <= 0x13)
+     tempal = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_CRT2CRTC;
+  else
+     tempal = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
 
-	tempah = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x18);
-	tempah &= 0x62;
-	tempah >>= 1;
-	tempal = tempah;
-	tempah >>= 3;
-	tempal |= tempah;
-	tempal &= 0x07;
-	tempcl = ThTiming[tempal];
-	tempbx = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x16);
-	tempbx >>= 6;
-	tempah = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x14);
-	tempah >>= 4;
-	tempah &= 0x0c;
-	tempbx |= tempah;
-	tempbx <<= 1;
-	tempal = ThLowB[tempbx + 1];
-	tempal *= tempcl;
-	tempal += ThLowB[tempbx];
-	data = tempal;
+  /* No customs required yet (Clevo, Compaq, etc) */
 
-    } else if(HwDeviceExtension->jChipType == SIS_730) {
-       
-#ifndef LINUX_XF86
-       SiS_SetReg4(0xcf8,0x80000050);
-       eax = SiS_GetReg3(0xcfc);
-#else
-       eax = pciReadLong(0x00000000, 0x50);
+  *CRT2Index = tempbx;
+  *ResIndex = tempal & 0x1F;
+}
 #endif
-       tempal = (USHORT)(eax >> 8);
-       tempal &= 0x06;
-       tempal <<= 5;
 
-#ifndef LINUX_XF86
-       SiS_SetReg4(0xcf8,0x800000A0);
-       eax = SiS_GetReg3(0xcfc);
-#else
-       eax = pciReadLong(0x00000000, 0xA0);
-#endif
-       temp = (USHORT)(eax >> 28);
-       temp &= 0x0F;   
-       tempal |= temp;
+static void
+SiS_GetRAMDAC2DATA(SiS_Private *SiS_Pr,USHORT ModeNo,USHORT ModeIdIndex,
+                   USHORT RefreshRateTableIndex,PSIS_HW_INFO HwInfo)
+{
+  USHORT tempax=0,tempbx=0;
+  USHORT temp1=0,modeflag=0,tempcx=0;
+  USHORT index;
 
-       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;
-       tempbx &= 0xc0;
-       tempbx >>= 6;
-       tempax &= 0x0f;
-       tempax *= 3;
-       tempbx += tempax;
-       
-       data = LatencyFactor730[tempbx];
-       data += 15;
-       temp = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x14);
-       if(!(temp & 0x80)) data += 5;
-       
-    } else {
+  SiS_Pr->SiS_RVBHCMAX  = 1;
+  SiS_Pr->SiS_RVBHCFACT = 1;
 
-       index = 0;
-       temp = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x14);
-       if(temp & 0x0080) index += 12;
+  if(ModeNo <= 0x13) {
 
-#ifndef LINUX_XF86
-       SiS_SetReg4(0xcf8,0x800000A0);
-       eax = SiS_GetReg3(0xcfc);
-#else
-       /* 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.
-        */
-       eax = pciReadLong(0x00000000, 0xA0);
-#endif
-       temp = (USHORT)(eax >> 24);
-       if(!(temp&0x01)) index += 24;
+     modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
+     index = SiS_GetModePtr(SiS_Pr,ModeNo,ModeIdIndex);
 
-#ifndef LINUX_XF86
-       SiS_SetReg4(0xcf8,0x80000050);
-       eax = SiS_GetReg3(0xcfc);
-#else
-       eax = pciReadLong(0x00000000, 0x50);
-#endif
-       temp=(USHORT)(eax >> 24);
-       if(temp & 0x01) index += 6;
+     tempax = SiS_Pr->SiS_StandTable[index].CRTC[0];
+     tempbx = SiS_Pr->SiS_StandTable[index].CRTC[6];
+     temp1 = SiS_Pr->SiS_StandTable[index].CRTC[7];
 
-       temp = (temp & 0x0F) >> 1;
-       index += temp;
-       
-       data = LatencyFactor[index];
-       data += 15;
-       temp = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x14);
-       if(!(temp & 0x80)) data += 5;
-    }
-    
-    data += data2;				/* CRT1 Request Period */
-    
-    SiS_Pr->SiS_SetFlag |= ProgrammingCRT2;
-    SiS_Pr->SiS_SelectCRT2Rate = SelectRate_backup;
+  } else {
 
-    if(!SiS_Pr->UseCustomMode) {
+     modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
+     index = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
 
-       CRT2ModeNo = ModeNo;
-       SiS_SearchModeID(SiS_Pr,ROMAddr,&CRT2ModeNo,&modeidindex);
+     tempax = SiS_Pr->SiS_CRT1Table[index].CR[0];
+     tempax |= (SiS_Pr->SiS_CRT1Table[index].CR[14] << 8);
+     tempax &= 0x03FF;
+     tempbx = SiS_Pr->SiS_CRT1Table[index].CR[6];
+     tempcx = SiS_Pr->SiS_CRT1Table[index].CR[13] << 8;
+     tempcx &= 0x0100;
+     tempcx <<= 2;
+     tempbx |= tempcx;
+     temp1  = SiS_Pr->SiS_CRT1Table[index].CR[7];
 
-       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(temp1 & 0x01) tempbx |= 0x0100;
+  if(temp1 & 0x20) tempbx |= 0x0200;
+  
+  tempax += 5;
 
-       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);
-	     }
-          }
-       }
+  /* Charx8Dot is no more used (and assumed), so we set it */
+  if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
+     modeflag |= Charx8Dot;
+  }
 
-    } else {
+  if(modeflag & Charx8Dot) tempax *= 8;
+  else                     tempax *= 9;
 
-       CRT2ModeNo = 0xfe;
-       VCLK = SiS_Pr->CSRClock;							/* Get VCLK */
+  if(modeflag & HalfDCLK)  tempax <<= 1;
 
-    }
+  tempbx++;
 
-    colorth = SiS_GetColorDepth(SiS_Pr,ROMAddr,CRT2ModeNo,modeidindex);   	/* Get colordepth */
-    colorth >>= 1;
-    if(!colorth) colorth++;
+  SiS_Pr->SiS_VGAHT = SiS_Pr->SiS_HT = tempax;
+  SiS_Pr->SiS_VGAVT = SiS_Pr->SiS_VT = tempbx;
+}
 
-    data = data * VCLK * colorth;
-    if(data % (MCLK << 4)) {
-      	data = data / (MCLK << 4);
-      	data++;
-    } else {
-      	data = data / (MCLK << 4);
-    }
-    
-    if(data <= 6) data = 6;
-    if(data > 0x14) data = 0x14;
+static void
+SiS_GetCRT2DataLVDS(SiS_Private *SiS_Pr,USHORT ModeNo,USHORT ModeIdIndex,
+                    USHORT RefreshRateTableIndex,
+		    PSIS_HW_INFO HwInfo)
+{
+   USHORT CRT2Index, ResIndex;
+   const SiS_LVDSDataStruct *LVDSData = NULL;
 
-    temp = SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x01);
-    if(HwDeviceExtension->jChipType == SIS_300) {
-       if(data <= 0x0f) temp = (temp & (~0x1F)) | 0x13;
-       else             temp = (temp & (~0x1F)) | 0x16;
-       if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
-       		temp = (temp & (~0x1F)) | 0x13;
-       }
-    } else {
-       if( ( (HwDeviceExtension->jChipType == SIS_630) ||
-             (HwDeviceExtension->jChipType == SIS_730) )  &&
-           (HwDeviceExtension->jChipRevision >= 0x30) ) /* 630s or 730(s?) */
-      {
-	  temp = (temp & (~0x1F)) | 0x1b;
-      } else {
-	  temp = (temp & (~0x1F)) | 0x16;
-      }
-    }
-    SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x01,0xe0,temp);
+   SiS_GetCRT2ResInfo(SiS_Pr, ModeNo, ModeIdIndex, HwInfo);
 
-    if( (HwDeviceExtension->jChipType == SIS_630) &&
-        (HwDeviceExtension->jChipRevision >= 0x30) ) /* 630s, NOT 730 */
-    {
-   	if(data > 0x13) data = 0x13;
-    }
-    SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x02,0xe0,data);
-    
-  } else {  /* If mode <= 0x13, we just restore everything */
-  
-    SiS_Pr->SiS_SetFlag |= ProgrammingCRT2;
-    SiS_Pr->SiS_SelectCRT2Rate = SelectRate_backup;
-    
-  }
-}
-#endif
+   if(SiS_Pr->SiS_VBType & VB_SISVB) {
+      SiS_Pr->SiS_RVBHCMAX  = 1;
+      SiS_Pr->SiS_RVBHCFACT = 1;
+      SiS_Pr->SiS_NewFlickerMode = 0;
+      SiS_Pr->SiS_RVBHRS = 50;
+      SiS_Pr->SiS_RY1COE = 0;
+      SiS_Pr->SiS_RY2COE = 0;
+      SiS_Pr->SiS_RY3COE = 0;
+      SiS_Pr->SiS_RY4COE = 0;
+   }
+
+   if((SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) && (SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)) {
 
-/* 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,
-                        55, 87, 84,116,103,135,119,151};
-  USHORT temp3,tempax,tempbx,tempcx;
-  USHORT tempcl, tempch;
-  USHORT index;
-  USHORT CRT1ModeNo,CRT2ModeNo;
-  USHORT ModeIdIndex;
-  USHORT RefreshRateTableIndex;
-  USHORT SelectRate_backup;
+      SiS_GetCRT2PtrA(SiS_Pr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
+                      &CRT2Index,&ResIndex);
 
-  SelectRate_backup = SiS_Pr->SiS_SelectCRT2Rate;
+      switch (CRT2Index) {
+      	case  0:  LVDSData = SiS_Pr->SiS_LCDA1024x768Data_1;    break;
+      	case  1:  LVDSData = SiS_Pr->SiS_LCDA1280x1024Data_1;   break;
+	case  3:  LVDSData = SiS_Pr->SiS_LCDA1400x1050Data_1;   break;
+	case  4:  LVDSData = SiS_Pr->SiS_LCDA1600x1200Data_1;   break;
+      	case  5:  LVDSData = SiS_Pr->SiS_LCDA1024x768Data_2;    break;
+      	case  6:  LVDSData = SiS_Pr->SiS_LCDA1280x1024Data_2;   break;
+	case  8:  LVDSData = SiS_Pr->SiS_LCDA1400x1050Data_2;   break;
+	case  9:  LVDSData = SiS_Pr->SiS_LCDA1600x1200Data_2;   break;
+	default:  LVDSData = SiS_Pr->SiS_LCDA1024x768Data_1;    break;
+      }
 #endif
 
-  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x01,0x3B);
-
-#if 0
-  if(!SiS_Pr->CRT1UsesCustomMode) {
-  
-     CRT1ModeNo = SiS_Pr->SiS_CRT1Mode;                                 /* get CRT1 ModeNo */
-     SiS_SearchModeID(SiS_Pr,ROMAddr,&CRT1ModeNo,&ModeIdIndex);
+   } else {
 
-     SiS_Pr->SiS_SetFlag &= (~ProgrammingCRT2);   
-     SiS_Pr->SiS_SelectCRT2Rate = 0;
+      /* 301BDH needs LVDS Data */
+      if((SiS_Pr->SiS_VBType & VB_NoLCD) && (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD)) {
+	 SiS_Pr->SiS_IF_DEF_LVDS = 1;
+      }
 
-     /* Get REFIndex for crt1 refreshrate */
-     RefreshRateTableIndex = SiS_GetRatePtrCRT2(SiS_Pr,ROMAddr,CRT1ModeNo,
-                                                ModeIdIndex,HwDeviceExtension);
-     index = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
-     tempax = SiS_Pr->SiS_VCLKData[index].CLOCK;			/* Get VCLK */
+      SiS_GetCRT2Ptr(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex,
+                     &CRT2Index, &ResIndex, HwInfo);
 
-     tempbx = SiS_GetColorDepth(SiS_Pr,ROMAddr,CRT1ModeNo,ModeIdIndex); /* Get colordepth */
-     tempbx >>= 1;
-     if(!tempbx) tempbx++;
+      /* 301BDH needs LVDS Data */
+      if((SiS_Pr->SiS_VBType & VB_NoLCD) && (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD)) {
+         SiS_Pr->SiS_IF_DEF_LVDS = 0;
+      }
 
-  } else {
+      switch (CRT2Index) {
+      	case  0:  LVDSData = SiS_Pr->SiS_LVDS800x600Data_1;    break;
+      	case  1:  LVDSData = SiS_Pr->SiS_LVDS1024x768Data_1;   break;
+      	case  2:  LVDSData = SiS_Pr->SiS_LVDS1280x1024Data_1;  break;
+      	case  3:  LVDSData = SiS_Pr->SiS_LVDS800x600Data_2;    break;
+      	case  4:  LVDSData = SiS_Pr->SiS_LVDS1024x768Data_2;   break;
+      	case  5:  LVDSData = SiS_Pr->SiS_LVDS1280x1024Data_2;  break;
+	case  6:  LVDSData = SiS_Pr->SiS_LVDS640x480Data_1;    break;
+        case  7:  LVDSData = SiS_Pr->SiS_LVDSXXXxXXXData_1;    break;
+	case  8:  LVDSData = SiS_Pr->SiS_LVDS1400x1050Data_1;  break;
+	case  9:  LVDSData = SiS_Pr->SiS_LVDS1400x1050Data_2;  break;
+      	case 10:  LVDSData = SiS_Pr->SiS_CHTVUNTSCData;        break;
+      	case 11:  LVDSData = SiS_Pr->SiS_CHTVONTSCData;        break;
+      	case 12:  LVDSData = SiS_Pr->SiS_CHTVUPALData;         break;
+      	case 13:  LVDSData = SiS_Pr->SiS_CHTVOPALData;         break;
+      	case 14:  LVDSData = SiS_Pr->SiS_LVDS320x480Data_1;    break;
+	case 15:  LVDSData = SiS_Pr->SiS_LVDS1024x600Data_1;   break;
+	case 16:  LVDSData = SiS_Pr->SiS_LVDS1152x768Data_1;   break;
+	case 17:  LVDSData = SiS_Pr->SiS_LVDS1024x600Data_2;   break;
+	case 18:  LVDSData = SiS_Pr->SiS_LVDS1152x768Data_2;   break;
+	case 19:  LVDSData = SiS_Pr->SiS_LVDS1280x768Data_1;   break;
+	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 84:  LVDSData = SiS_Pr->SiS_LVDS848x480Data_1;    break;
+	case 85:  LVDSData = SiS_Pr->SiS_LVDS848x480Data_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;  /* Super Overscan */
+	default:  LVDSData = SiS_Pr->SiS_LVDS1024x768Data_1;   break;
+      }
+   }
 
-     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;
-     }
-  
-  }
-    
-  tempax *= tempbx;
+   SiS_Pr->SiS_VGAHT = (LVDSData+ResIndex)->VGAHT;
+   SiS_Pr->SiS_VGAVT = (LVDSData+ResIndex)->VGAVT;
+   SiS_Pr->SiS_HT    = (LVDSData+ResIndex)->LCDHT;
+   SiS_Pr->SiS_VT    = (LVDSData+ResIndex)->LCDVT;
 
-  tempbx = SiS_GetMCLK(SiS_Pr,ROMAddr, HwDeviceExtension);     		/* Get MCLK */
+   if(SiS_Pr->SiS_VBType & VB_SISVB) {
 
-  tempax /= tempbx;
+      if(!(SiS_Pr->SiS_LCDInfo & DontExpandLCD)) {
+         SiS_Pr->SiS_HDE = SiS_Pr->PanelXRes;
+         SiS_Pr->SiS_VDE = SiS_Pr->PanelYRes;
+      }
 
-  tempbx = tempax;
+   } else {
 
-  tempax = 16;
+      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;
+		     }
+		  }
+               }
+            }
+         }
+      }
+   }
+}
 
-  tempax -= tempbx;
+static void
+SiS_GetCRT2Data301(SiS_Private *SiS_Pr,USHORT ModeNo,USHORT ModeIdIndex,
+                   USHORT RefreshRateTableIndex,
+		   PSIS_HW_INFO HwInfo)
+{
+  USHORT tempax,tempbx,modeflag;
+  USHORT resinfo;
+  USHORT CRT2Index,ResIndex;
+  const SiS_LCDDataStruct *LCDPtr = NULL;
+  const SiS_TVDataStruct  *TVPtr  = NULL;
 
-  tempbx = tempax;    /* tempbx = 16-DRamBus - DCLK*BytePerPixel/MCLK */
+  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;
+     }
+  }
+  
+  SiS_Pr->SiS_NewFlickerMode = 0;
+  SiS_Pr->SiS_RVBHRS = 50;
+  SiS_Pr->SiS_RY1COE = 0;
+  SiS_Pr->SiS_RY2COE = 0;
+  SiS_Pr->SiS_RY3COE = 0;
+  SiS_Pr->SiS_RY4COE = 0;
 
-  tempax = ((52 * 16) / tempbx);
+  SiS_GetCRT2ResInfo(SiS_Pr,ModeNo,ModeIdIndex,HwInfo);
 
-  if ((52*16 % tempbx) != 0) {
-    	tempax++;
-  }
-  tempcx = tempax;
-  tempcx += 40;
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToRAMDAC){
 
-  /* get DRAM latency */
-  tempcl = (SiS_GetReg1(SiS_Pr->SiS_P3c4,0x17) >> 3) & 0x7;     /* SR17[5:3] DRAM Queue depth */
-  tempch = (SiS_GetReg1(SiS_Pr->SiS_P3c4,0x17) >> 6) & 0x3;     /* SR17[7:6] DRAM Grant length */
-
-  for (temp3 = 0; temp3 < 16; temp3 += 2) {
-    if ((CombCode[temp3] == tempcl) && (CombCode[temp3+1] == tempch)) {
-      temp3 = CRT2ThLow[temp3 >> 1];
-    }
-  }
+     if(SiS_Pr->UseCustomMode) {
 
-  tempcx +=  temp3;                                      /* CRT1 Request Period */
+        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;
 
-  SiS_Pr->SiS_SetFlag |= ProgrammingCRT2;
-  SiS_Pr->SiS_SelectCRT2Rate = SelectRate_backup;
+     } else {
 
-  if(!SiS_Pr->UseCustomMode) {
+        SiS_GetRAMDAC2DATA(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex, HwInfo);
+     }
 
-     CRT2ModeNo = ModeNo;                                                 /* get CRT2 ModeNo */
-     SiS_SearchModeID(SiS_Pr,ROMAddr,&CRT2ModeNo,&ModeIdIndex);
+  } else if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
 
-     RefreshRateTableIndex = SiS_GetRatePtrCRT2(SiS_Pr,ROMAddr,CRT2ModeNo,
-                                                ModeIdIndex,HwDeviceExtension);
+     SiS_GetCRT2Ptr(SiS_Pr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
+                    &CRT2Index,&ResIndex,HwInfo);
 
-     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;
+     switch(CRT2Index) {
+       case  2:  TVPtr = SiS_Pr->SiS_ExtHiTVData;   break;
+       case  3:  TVPtr = SiS_Pr->SiS_ExtPALData;    break;
+       case  4:  TVPtr = SiS_Pr->SiS_ExtNTSCData;   break;
+       case  5:  TVPtr = SiS_Pr->SiS_Ext525iData;   break;
+       case  6:  TVPtr = SiS_Pr->SiS_Ext525pData;   break;
+       case  7:  TVPtr = SiS_Pr->SiS_Ext750pData;   break;
+       case  8:  TVPtr = SiS_Pr->SiS_StPALData;     break;
+       case  9:  TVPtr = SiS_Pr->SiS_StNTSCData;    break;
+       case 10:  TVPtr = SiS_Pr->SiS_St525iData;    break;
+       case 11:  TVPtr = SiS_Pr->SiS_St525pData;    break;
+       case 12:  TVPtr = SiS_Pr->SiS_St750pData;    break;
+       case 14:  TVPtr = SiS_Pr->SiS_St2HiTVData;   break;
+       default:  TVPtr = SiS_Pr->SiS_StPALData;     break;
+     }
+
+     SiS_Pr->SiS_RVBHCMAX  = (TVPtr+ResIndex)->RVBHCMAX;
+     SiS_Pr->SiS_RVBHCFACT = (TVPtr+ResIndex)->RVBHCFACT;
+     SiS_Pr->SiS_VGAHT     = (TVPtr+ResIndex)->VGAHT;
+     SiS_Pr->SiS_VGAVT     = (TVPtr+ResIndex)->VGAVT;
+     SiS_Pr->SiS_HDE       = (TVPtr+ResIndex)->TVHDE;
+     SiS_Pr->SiS_VDE       = (TVPtr+ResIndex)->TVVDE;
+     SiS_Pr->SiS_RVBHRS    = (TVPtr+ResIndex)->RVBHRS;
+     SiS_Pr->SiS_NewFlickerMode = (TVPtr+ResIndex)->FlickerMode;
+     if(modeflag & HalfDCLK) {
+        SiS_Pr->SiS_RVBHRS = (TVPtr+ResIndex)->HALFRVBHRS;
      }
 
-  } else {
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) {
 
-     CRT2ModeNo = 0xfe;							  /* Get VCLK  */
-     tempax = SiS_Pr->CSRClock;
+        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;
+
+        if(SiS_Pr->SiS_VGAVDE == 350) SiS_Pr->SiS_TVMode |= TVSetTVSimuMode;
+
+        SiS_Pr->SiS_HT = ExtHiTVHT;
+        SiS_Pr->SiS_VT = ExtHiTVVT;
+        if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
+           if(SiS_Pr->SiS_TVMode & TVSetTVSimuMode) {
+              SiS_Pr->SiS_HT = StHiTVHT;
+              SiS_Pr->SiS_VT = StHiTVVT;
+              if(!(modeflag & Charx8Dot)) {
+                 SiS_Pr->SiS_HT = StHiTextTVHT;
+                 SiS_Pr->SiS_VT = StHiTextTVVT;
+              }
+           }
+        }
 
-  }
+     } else if(SiS_Pr->SiS_VBInfo & SetCRT2ToYPbPr525750) {
 
-  tempbx = SiS_GetColorDepth(SiS_Pr,ROMAddr,CRT2ModeNo,ModeIdIndex);   	  /* Get colordepth */
-  tempbx >>= 1;
-  if(!tempbx) tempbx++;
+        if(SiS_Pr->SiS_TVMode & TVSetYPbPr750p) {
+           SiS_Pr->SiS_HT = 1650;
+           SiS_Pr->SiS_VT = 750;
+	} else if(SiS_Pr->SiS_TVMode & TVSetYPbPr525p) {
+	   SiS_Pr->SiS_HT = NTSCHT;
+	   SiS_Pr->SiS_VT = NTSCVT;
+        } else {
+           SiS_Pr->SiS_HT = NTSCHT;
+	   if(SiS_Pr->SiS_TVMode & TVSetNTSC1024) SiS_Pr->SiS_HT = NTSC2HT;
+           SiS_Pr->SiS_VT = NTSCVT;
+        }
 
-  tempax *= tempbx;
+     } else {
 
-  tempax *= tempcx;
+        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;
 
-  tempbx = SiS_GetMCLK(SiS_Pr,ROMAddr, HwDeviceExtension);	       /* Get MCLK */
-  tempbx <<= 4;
+        if(modeflag & HalfDCLK) {
+           SiS_Pr->SiS_RY1COE = 0x00;
+           SiS_Pr->SiS_RY2COE = 0xf4;
+           SiS_Pr->SiS_RY3COE = 0x10;
+           SiS_Pr->SiS_RY4COE = 0x38;
+        }
 
-  tempcx = tempax;
-  tempax /= tempbx;
-  if(tempcx % tempbx) tempax++;		/* CRT1 Request period * TCLK * BytePerPixel / (MCLK*16) */
+        if(!(SiS_Pr->SiS_TVMode & TVSetPAL)) {
+           SiS_Pr->SiS_HT = NTSCHT;
+	   if(SiS_Pr->SiS_TVMode & TVSetNTSC1024) SiS_Pr->SiS_HT = NTSC2HT;
+           SiS_Pr->SiS_VT = NTSCVT;
+        } else {
+           SiS_Pr->SiS_HT = PALHT;
+           SiS_Pr->SiS_VT = PALVT;
+        }
 
-  if (tempax > 0x37)  tempax = 0x37;
+     }
 
-  /* 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
+  } else if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
 
-  SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x02,~0x3F,0x04);
+     if(SiS_Pr->UseCustomMode) {
 
-#endif
-}
+        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;
 
-USHORT
-SiS_GetMCLK(SiS_Private *SiS_Pr, UCHAR *ROMAddr, PSIS_HW_DEVICE_INFO HwDeviceExtension)
-{
-  USHORT index;
+     } else {
 
-  index = SiS_Get310DRAMType(SiS_Pr,ROMAddr,HwDeviceExtension);
-  if(index >= 4) {
-    index -= 4;
-    return(SiS_Pr->SiS_MCLKData_1[index].CLOCK);
-  } else {
-    return(SiS_Pr->SiS_MCLKData_0[index].CLOCK);
-  }
-}
+        SiS_GetCRT2Ptr(SiS_Pr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
+                      &CRT2Index,&ResIndex,HwInfo);
 
+        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 */
+#ifdef SIS315H
+	 case 50: LCDPtr = (SiS_LCDDataStruct *)SiS310_ExtCompaq1280x1024Data;	break;
+	 case 51: LCDPtr = SiS_Pr->SiS_NoScaleData1280x1024;			break;
+	 case 52: LCDPtr = SiS_Pr->SiS_St2LCD1280x1024Data;	  		break;
 #endif
+         default: LCDPtr = SiS_Pr->SiS_ExtLCD1024x768Data;	  break;
+        }
 
-/* Checked against 650/LVDS 1.10.07 BIOS */
-void
-SiS_GetLVDSDesData(SiS_Private *SiS_Pr, UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex,
-                   USHORT RefreshRateTableIndex, PSIS_HW_DEVICE_INFO HwDeviceExtension,
-		   USHORT BaseAddr)
-{
-  USHORT modeflag;
-  USHORT PanelIndex,ResIndex;
-  const  SiS_LVDSDesStruct *PanelDesPtr = NULL;
-
-  if((SiS_Pr->UseCustomMode) ||
-     (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_PanelCustom) ||
-     (SiS_Pr->SiS_CustomT == CUT_PANEL848)) {
-     SiS_Pr->SiS_LCDHDES = 0;
-     SiS_Pr->SiS_LCDVDES = 0;
-     return;
-  }
+        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;
 
-  if((SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) && (SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)) {
+#ifdef TWDEBUG
+        xf86DrvMsg(0, X_INFO,
+    	    "GetCRT2Data: Index %d ResIndex %d\n", CRT2Index, ResIndex);
+#endif
 
-#ifdef SIS315H  
-     SiS_GetLVDSDesPtrA(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
-                        &PanelIndex,&ResIndex, HwDeviceExtension, BaseAddr);
-			
-     switch (PanelIndex)
-     {
-     	case  0: PanelDesPtr = SiS_Pr->LVDS1024x768Des_1;   break;  /* --- expanding --- */
-     	case  1: PanelDesPtr = SiS_Pr->LVDS1280x1024Des_1;  break;
-	case  2: PanelDesPtr = SiS_Pr->LVDS1400x1050Des_1;  break;
-	case  3: PanelDesPtr = SiS_Pr->LVDS1600x1200Des_1;  break;
-     	case  4: PanelDesPtr = SiS_Pr->LVDS1024x768Des_2;   break;  /* --- non expanding --- */
-     	case  5: PanelDesPtr = SiS_Pr->LVDS1280x1024Des_2;  break;
-	case  6: PanelDesPtr = SiS_Pr->LVDS1400x1050Des_2;  break;
+	if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
+           tempax = 1024;
+           if(SiS_Pr->SiS_SetFlag & LCDVESATiming) {
+              if(HwInfo->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 if((SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) &&
+	          (HwInfo->jChipType >= SIS_661)) {
+	   tempax = 1400;
+	   tempbx = 1050;
+	   if(SiS_Pr->SiS_VGAVDE == 1024) {
+	      tempax = 1280;
+	      tempbx = 1024;
+	   }
+        } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) {
+           tempax = 1600;
+	   tempbx = 1200;
+	   if((HwInfo->jChipType < SIS_661) || (!(SiS_Pr->SiS_SetFlag & LCDVESATiming))) {
+              if     (SiS_Pr->SiS_VGAVDE == 350)  tempbx = 875;
+              else if(SiS_Pr->SiS_VGAVDE == 400)  tempbx = 1000;
+           }
+        } 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;
+     }
+  }
+}
+
+static void
+SiS_GetCRT2Data(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,
+                USHORT RefreshRateTableIndex, PSIS_HW_INFO HwInfo)
+{
+
+  if(SiS_Pr->SiS_VBType & VB_SISVB) {
+
+     if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
+
+        if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
+
+           SiS_GetCRT2DataLVDS(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex, HwInfo);
+
+        } else {
+
+	   if((SiS_Pr->SiS_VBType & VB_NoLCD) && (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD)) {
+
+	      /* Need LVDS Data for LCD on 301B-DH */
+	      SiS_GetCRT2DataLVDS(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex, HwInfo);
+
+	   } else {
+
+	      SiS_GetCRT2Data301(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex, HwInfo);
+
+           }
+
+        }
+
+     } else {
+
+     	SiS_GetCRT2Data301(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex, HwInfo);
+
+     }
+
+  } else {
+
+     SiS_GetCRT2DataLVDS(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex, HwInfo);
+
+  }
+}
+
+/*********************************************/
+/*            GET LVDS DES DATA              */
+/*********************************************/
+
+static void
+SiS_GetLVDSDesPtr(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,
+                  USHORT RefreshRateTableIndex, USHORT *PanelIndex,
+		  USHORT *ResIndex, PSIS_HW_INFO HwInfo)
+{
+  USHORT tempbx,tempal,modeflag;
+
+  if(ModeNo <= 0x13) {
+     modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
+     tempal = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_CRT2CRTC;
+  } else {
+     modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
+     tempal = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
+  }
+
+  tempbx = 0;
+  if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+        tempbx = 50;
+        if((SiS_Pr->SiS_TVMode & TVSetPAL) && (!(SiS_Pr->SiS_TVMode & TVSetPALM))) tempbx += 2;
+        if(SiS_Pr->SiS_TVMode & TVSetCHOverScan) tempbx += 1;
+        /* Nothing special needed for SOverscan    */
+        /*     PALM uses NTSC data, PALN uses PAL data */
+     }
+  }
+
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
+     tempbx = SiS_Pr->SiS_LCDTypeInfo;
+     if(HwInfo->jChipType >= SIS_661) {
+        /* As long as we don's use the BIOS tables, we
+	 * need to convert the TypeInfo as for 315 series
+	 */
+        tempbx = SiS_Pr->SiS_LCDResInfo - 1;
+     }
+     if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) tempbx += 16;
+     if(SiS_Pr->SiS_LCDInfo & LCDPass11) {
+        tempbx = 32;
+        if(modeflag & HalfDCLK) tempbx++;
+     }
+  }
+
+  if(SiS_Pr->SiS_SetFlag & SetDOSMode) {
+     if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480)  {
+        tempal = 0x07;
+        if(HwInfo->jChipType < SIS_315H) {
+           if(SiS_GetReg(SiS_Pr->SiS_P3c4,0x13) & 0x80) tempal++;
+        }
+     }
+  }
+
+  *PanelIndex = tempbx;
+  *ResIndex = tempal & 0x1F;
+}
+
+#ifdef SIS315H
+static void
+SiS_GetLVDSDesPtrA(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,
+                   USHORT RefreshRateTableIndex, USHORT *PanelIndex, USHORT *ResIndex,
+		   PSIS_HW_INFO HwInfo)
+{
+  USHORT tempbx=0,tempal;
+
+  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050)      tempbx = 2;
+  else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) tempbx = 3;
+  else tempbx = SiS_Pr->SiS_LCDResInfo - 2;
+
+  if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)  tempbx += 4;
+
+  if(SiS_Pr->SiS_CustomT == CUT_CLEVO1024) {
+     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
+        if(SiS_IsDualLink(SiS_Pr, HwInfo)) {
+	   tempbx = 80;
+	   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) tempbx++;
+	}
+     }
+  }
+  if((SiS_Pr->SiS_CustomT == CUT_UNIWILL1024) ||
+     (SiS_Pr->SiS_CustomT == CUT_UNIWILL10242)) {
+     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
+	tempbx = 82;
+	if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) tempbx++;
+     }
+  }
+  if(SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) {
+     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
+	tempbx = 84;
+	if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) tempbx++;
+     }
+  }
+  if(SiS_Pr->SiS_CustomT == CUT_ASUSA2H_2) {
+     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
+	tempbx = 86;
+	if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) tempbx++;
+     }
+  }
+
+  if(ModeNo <= 0x13)
+     tempal = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_CRT2CRTC;
+  else
+     tempal = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
+
+  *PanelIndex = tempbx;
+  *ResIndex = tempal & 0x1F;
+}
+#endif
+
+static void
+SiS_GetLVDSDesData(SiS_Private *SiS_Pr, USHORT ModeNo,USHORT ModeIdIndex,
+                   USHORT RefreshRateTableIndex, PSIS_HW_INFO HwInfo)
+{
+  USHORT modeflag;
+  USHORT PanelIndex,ResIndex;
+  const  SiS_LVDSDesStruct *PanelDesPtr = NULL;
+
+  if((SiS_Pr->UseCustomMode) ||
+     (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_PanelCustom) ||
+     (SiS_Pr->SiS_CustomT == CUT_PANEL848)) {
+     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
+     SiS_GetLVDSDesPtrA(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex,
+                        &PanelIndex, &ResIndex, HwInfo);
+
+     switch (PanelIndex)
+     {
+     	case  0: PanelDesPtr = SiS_Pr->LVDS1024x768Des_1;   break;  /* --- expanding --- */
+     	case  1: PanelDesPtr = SiS_Pr->LVDS1280x1024Des_1;  break;
+	case  2: PanelDesPtr = SiS_Pr->LVDS1400x1050Des_1;  break;
+	case  3: PanelDesPtr = SiS_Pr->LVDS1600x1200Des_1;  break;
+     	case  4: PanelDesPtr = SiS_Pr->LVDS1024x768Des_2;   break;  /* --- non expanding --- */
+     	case  5: PanelDesPtr = SiS_Pr->LVDS1280x1024Des_2;  break;
+	case  6: PanelDesPtr = SiS_Pr->LVDS1400x1050Des_2;  break;
 	case  7: PanelDesPtr = SiS_Pr->LVDS1600x1200Des_2;  break;
-	case 80: PanelDesPtr = (SiS_LVDSDesStruct *)Clevo1024x768Des_1;  break;  /*  custom  */
-	case 81: PanelDesPtr = (SiS_LVDSDesStruct *)Clevo1024x768Des_2;  break;
+	case 80: PanelDesPtr = (SiS_LVDSDesStruct *)Clevo1024x768Des_1;   break;  /*  custom  */
+	case 81: PanelDesPtr = (SiS_LVDSDesStruct *)Clevo1024x768Des_2;   break;
+	case 82: PanelDesPtr = (SiS_LVDSDesStruct *)Uniwill1024x768Des_1; break;
+	case 83: PanelDesPtr = (SiS_LVDSDesStruct *)Uniwill1024x768Des_2; break;
+	case 84: PanelDesPtr = (SiS_LVDSDesStruct *)Compaq1280x1024Des_1; break;
+	case 85: PanelDesPtr = (SiS_LVDSDesStruct *)Compaq1280x1024Des_2; break;
+	case 86: PanelDesPtr = (SiS_LVDSDesStruct *)Asus1024x768Des_1;    break;  /*  custom  */
+	case 87: PanelDesPtr = (SiS_LVDSDesStruct *)Asus1024x768Des_2;    break;
 	default: PanelDesPtr = SiS_Pr->LVDS1024x768Des_1;   break;
      }
 #endif
 
   } else {
 
-     SiS_GetLVDSDesPtr(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
-                       &PanelIndex,&ResIndex,HwDeviceExtension);
+     SiS_GetLVDSDesPtr(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex,
+                       &PanelIndex, &ResIndex, HwInfo);
 
      switch (PanelIndex)
      {
@@ -2790,7 +3540,7 @@
      	case 52: PanelDesPtr = SiS_Pr->SiS_CHTVUPALDesData;    break;
      	case 53: PanelDesPtr = SiS_Pr->SiS_CHTVOPALDesData;    break;
 	default:
-		 if(HwDeviceExtension->jChipType < SIS_315H)
+		 if(HwInfo->jChipType < SIS_315H)
 		    PanelDesPtr = SiS_Pr->SiS_PanelType0e_1;
 		 else
 		    PanelDesPtr = SiS_Pr->SiS_PanelType01_1;
@@ -2810,12 +3560,11 @@
         }
      } else {
         if(!(SiS_Pr->SiS_SetFlag & SetDOSMode)) {
-           if( (HwDeviceExtension->jChipType < SIS_315H) || 
-	       (SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1280x1024) ) {
-              if(SiS_Pr->SiS_LCDResInfo >= SiS_Pr->SiS_Panel1024x768){
+           if(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;
-	            if(HwDeviceExtension->jChipType < SIS_315H) {
+	            if(HwInfo->jChipType < SIS_315H) {
 	               if(!(modeflag & HalfDCLK)) SiS_Pr->SiS_LCDHDES = 320;
 	            } else {
 	               if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768)
@@ -2838,4487 +3587,3696 @@
         }
      }
   }
-
 }
 
-void
-SiS_GetLVDSDesPtr(SiS_Private *SiS_Pr, UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex,
-                  USHORT RefreshRateTableIndex,USHORT *PanelIndex,
-		  USHORT *ResIndex,PSIS_HW_DEVICE_INFO HwDeviceExtension)
-{
-  USHORT tempbx,tempal,modeflag;
-
-  if(ModeNo <= 0x13) {
-    	modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
-	tempal = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_CRT2CRTC;
-  } else {
-    	modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
-	tempal = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
-  }
-
-  tempbx = 0;
-  if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
-     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;
-        /* Nothing special needed for SOverscan    */
-        /*     PALM uses NTSC data, PALN uses PAL data */
-     }
-  }
-  if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
-     tempbx = SiS_Pr->SiS_LCDTypeInfo;
-     if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) tempbx += 16;
-     if(SiS_Pr->SiS_LCDInfo & LCDPass11) {
-        tempbx = 32;
-        if(modeflag & HalfDCLK) tempbx++;
-     }
-  }
-  /* 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;
-        if(HwDeviceExtension->jChipType < SIS_315H) {
-           if(SiS_GetReg1(SiS_Pr->SiS_P3c4,0x13) & 0x80) tempal++;
-        }
-     }
-  }
+/*********************************************/
+/*          SET CRT2 AUTO-THRESHOLD          */
+/*********************************************/
 
-  *PanelIndex = tempbx;
-  *ResIndex = tempal & 0x1F;
+#ifdef SIS315H
+static void
+SiS_CRT2AutoThreshold(SiS_Private *SiS_Pr)
+{
+  SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x01,0x40);
 }
+#endif
 
-#ifdef SIS315H
+/*********************************************/
+/*           DISABLE VIDEO BRIDGE            */
+/*********************************************/
+
+/* NEVER use any variables (VBInfo), this will be called
+ * from outside the context of modeswitch!
+ * MUST call getVBType before calling this
+ */
 void
-SiS_GetLVDSDesPtrA(SiS_Private *SiS_Pr, UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex,
-                   USHORT RefreshRateTableIndex,USHORT *PanelIndex,USHORT *ResIndex,
-		   PSIS_HW_DEVICE_INFO HwDeviceExtension, USHORT BaseAddr)
+SiS_DisableBridge(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
 {
-  USHORT tempbx=0,tempal;
+#ifdef SIS315H
+  USHORT tempah,pushax=0,modenum;
+#endif
+  USHORT temp=0;
 
-  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050)      tempbx = 2;
-  else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) tempbx = 3;
-  else tempbx = SiS_Pr->SiS_LCDResInfo - SiS_Pr->SiS_PanelMinLVDS;
 
-  if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)  tempbx += 4;
+  if(SiS_Pr->SiS_VBType & VB_SISVB) {
 
-  if(SiS_Pr->SiS_CustomT == CUT_CLEVO1024) {
-     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
-        if(SiS_IsDualLink(SiS_Pr, HwDeviceExtension, BaseAddr)) {
-	   tempbx = 80;
-	   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) tempbx++;
-	}
-     }
-  }
+      if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {   /* ===== For 30xB/LV ===== */
 
-  if(ModeNo <= 0x13)
-     tempal = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_CRT2CRTC;
-  else
-     tempal = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
+        if(HwInfo->jChipType < SIS_315H) {
 
-  *PanelIndex = tempbx;
-  *ResIndex = tempal & 0x1F;
-}
-#endif
-
-void
-SiS_SetCRT2ModeRegs(SiS_Private *SiS_Pr, USHORT BaseAddr, USHORT ModeNo, USHORT ModeIdIndex,
-                    PSIS_HW_DEVICE_INFO HwDeviceExtension)
-{
-  USHORT i,j,modeflag;
-  USHORT tempcl,tempah=0;
-#ifdef SIS300
-  USHORT temp;
-#endif
-#ifdef SIS315H
-  USHORT tempbl;
-#endif
-
-  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;
-     }
-  }
-  
-  /* 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 */
+#ifdef SIS300	   /* 300 series */
 
-  if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
+           if(HwInfo->jChipType == SIS_300) {  /* For 300+301LV (A907) */
 
-	/*   1. for LVDS/302B/302LV **LCDA** */
+	      if(!(SiS_CR36BIOSWord23b(SiS_Pr,HwInfo))) {
+	         if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
+	            SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x26,0xFE);
+		    SiS_PanelDelay(SiS_Pr, HwInfo, 3);
+		 }
+	      }
+	      SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x1f,0x3f);
+	      SiS_ShortDelay(SiS_Pr,1);
+	      SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x00,0xDF);
+	      SiS_DisplayOff(SiS_Pr);
+	      SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x32,0xDF);
+	      SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x1E,0xDF);
+	      if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
+	         if( (!(SiS_CRT2IsLCD(SiS_Pr, HwInfo))) ||
+	             (!(SiS_CR36BIOSWord23d(SiS_Pr, HwInfo))) ) {
+	            SiS_PanelDelay(SiS_Pr, HwInfo, 2);
+                    SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x26,0xFD);
+		 }
+	      }
 
-      SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x00,0xAF,0x40); /* FUNCTION CONTROL */
-      SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2E,0xF7);
+	   } else {
 
-  } else {
+	      if(!(SiS_CR36BIOSWord23b(SiS_Pr,HwInfo))) {
+	         SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x11,0xF7,0x08);
+	         SiS_PanelDelay(SiS_Pr, HwInfo, 3);
+	      }
+	      if(SiS_Is301B(SiS_Pr)) {
+	         SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x1f,0x3f);
+	         SiS_ShortDelay(SiS_Pr,1);
+	      }
+	      SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x00,0xDF);
+	      SiS_DisplayOff(SiS_Pr);
+	      SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x32,0xDF);
+	      SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x1E,0xDF);
+	      SiS_UnLockCRT2(SiS_Pr,HwInfo);
+	      SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x01,0x80);
+	      SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x02,0x40);
+	      if( (!(SiS_CRT2IsLCD(SiS_Pr, HwInfo))) ||
+	          (!(SiS_CR36BIOSWord23d(SiS_Pr, HwInfo))) ) {
+	         SiS_PanelDelay(SiS_Pr, HwInfo, 2);
+                 SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x11,0xFB,0x04);
+	      }
+	   }
 
-    for(i=0,j=4; i<3; i++,j++) SiS_SetReg1(SiS_Pr->SiS_Part1Port,j,0);
+#endif  /* SIS300 */
 
-    tempcl = SiS_Pr->SiS_ModeType;
+        } else {
 
-    if(HwDeviceExtension->jChipType < SIS_315H) {
+#ifdef SIS315H	   /* 315 series */
 
-#ifdef SIS300    /* ---- 300 series ---- */
+           if(IS_SIS550650740660) {		/* 550, 650, 740, 660 */
 
-      /* 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);
-      }
+	      modenum = SiS_GetReg(SiS_Pr->SiS_P3d4,0x34) & 0x7f;
 
-      if(ModeNo > 0x13) {
-         tempcl -= ModeVGA;
-         if((tempcl > 0) || (tempcl == 0)) {      /* tempcl is USHORT -> always true! */
-            tempah = ((0x10 >> tempcl) | 0x80);
-         }
-      } else tempah = 0x80;
+              if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {			/* LV */
+#ifdef SET_EMI
+	         if(SiS_Pr->SiS_VBType & (VB_SIS302LV | VB_SIS302ELV)) {
+		    if(SiS_Pr->SiS_CustomT != CUT_CLEVO1400) {
+	               SiS_SetReg(SiS_Pr->SiS_Part4Port,0x30,0x00);
+		    }
+		 }
+#endif
+		 if( (modenum <= 0x13) ||
+		     (!(SiS_IsDualEdge(SiS_Pr,HwInfo))) ||
+		     (SiS_IsVAMode(SiS_Pr,HwInfo)) ) {
+	     	    SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x26,0xFE);
+		    if((SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) ||
+		       (SiS_Pr->SiS_CustomT == CUT_CLEVO1400)) {
+		       SiS_PanelDelay(SiS_Pr, HwInfo, 3);
+		    }
+	         }
 
-      if(SiS_Pr->SiS_VBInfo & SetInSlaveMode)  tempah ^= 0xA0;
+		 if((SiS_Pr->SiS_CustomT != CUT_COMPAQ1280) &&
+		    (SiS_Pr->SiS_CustomT != CUT_CLEVO1400)) {
+		    SiS_DDC2Delay(SiS_Pr,0xff00);
+		    SiS_DDC2Delay(SiS_Pr,0xe000);
 
-#endif  /* SIS300 */
+	            SiS_SetRegByte(SiS_Pr->SiS_P3c6,0x00);
 
-    } else {
+                    pushax = SiS_GetReg(SiS_Pr->SiS_P3c4,0x06);
 
-#ifdef SIS315H    /* ------- 315/330 series ------ */
+		    if(IS_SIS740) {
+		       SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x06,0xE3);
+		    }
 
-      if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-         if(SiS_Pr->SiS_VBInfo & CRT2DisplayFlag) {
-	    SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2e,0x08);
-         }
-      }
+	            SiS_PanelDelay(SiS_Pr, HwInfo, 3);
 
-      if(ModeNo > 0x13) {
-         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_IsNotM650orLater(SiS_Pr, HwInfo))) {
+	               tempah = 0xef;
+	               if(SiS_IsVAMode(SiS_Pr,HwInfo)) {
+	                  tempah = 0xf7;
+                       }
+	               SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x4c,tempah);
+	            }
+		 }
 
-      if(SiS_Pr->SiS_VBInfo & SetInSlaveMode)  tempah ^= 0x50;
+              } else if(SiS_Pr->SiS_VBType & VB_NoLCD) {			/* B-DH */
 
-#endif  /* SIS315H */
+	         if(!(SiS_IsNotM650orLater(SiS_Pr,HwInfo))) {
+	            SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x4c,0xef);
+	         }
 
-    }
+	      }
 
-    if(SiS_Pr->SiS_VBInfo & CRT2DisplayFlag)  tempah = 0;
+	      if((SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) ||
+	         (SiS_Pr->SiS_CustomT == CUT_CLEVO1400)) {
+	         SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x1F,0xef);
+	      }
 
-    if(HwDeviceExtension->jChipType < SIS_315H) {
-       SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x00,tempah);  		/* FUNCTION CONTROL */
-    } else {
-       if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
-          SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x00,0xa0,tempah);  	/* FUNCTION CONTROL */
-       } else {
-          if(IS_SIS740) {
-	     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x00,tempah);  		/* FUNCTION CONTROL */
-	  } else {
-             SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x00,0xa0,tempah);  	/* FUNCTION CONTROL */
-	  }
-       }
-    }
+              if((SiS_Pr->SiS_VBType & VB_SIS301B302B) ||
+	         (SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) ||
+		 (SiS_Pr->SiS_CustomT == CUT_CLEVO1400)) {
+	         tempah = 0x3f;
+	         if(SiS_IsDualEdge(SiS_Pr,HwInfo)) {
+	            tempah = 0x7f;
+	            if(!(SiS_IsVAMode(SiS_Pr,HwInfo))) {
+		       tempah = 0xbf;
+                    }
+	         }
+	         SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x1F,tempah);
+	      }
 
-    if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
+              if((SiS_IsVAMode(SiS_Pr,HwInfo)) ||
+	         ((SiS_Pr->SiS_VBType & VB_SIS301LV302LV) && (modenum <= 0x13))) {
 
-	/*   2. for 301 (301B, 302B 301LV, 302LV non-LCDA) */
+	         if((SiS_Pr->SiS_VBType & VB_SIS301B302B) ||
+		    (SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) ||
+		    (SiS_Pr->SiS_CustomT == CUT_CLEVO1400)) {
+		    SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x1E,0xDF);
+		    SiS_DisplayOff(SiS_Pr);
+		    SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x32,0xDF);
+		 } else {
+	            SiS_DisplayOff(SiS_Pr);
+	            SiS_PanelDelay(SiS_Pr, HwInfo, 2);
+	            SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x32,0xDF);
+	            SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x1E,0xDF);
+		    if((SiS_Pr->SiS_VBType & VB_SIS301LV302LV) && (modenum <= 0x13)) {
+		       SiS_DisplayOff(SiS_Pr);
+	               SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x00,0x80);
+	               SiS_PanelDelay(SiS_Pr, HwInfo, 2);
+	               SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x32,0xDF);
+	               temp = SiS_GetReg(SiS_Pr->SiS_Part1Port,0x00);
+                       SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x00,0x10);
+	               SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x1E,0xDF);
+	               SiS_SetReg(SiS_Pr->SiS_Part1Port,0x00,temp);
+		    }
+		 }
 
-    	tempah = 0x01;
-    	if(!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
-      		tempah |= 0x02;
-    	}
-    	if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToRAMDAC)) {
-      		tempah ^= 0x05;
-      		if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD)) {
-        		tempah ^= 0x01;
-      		}
-    	}
+	      } else {
 
-	if(SiS_Pr->SiS_VBInfo & CRT2DisplayFlag)  tempah = 0;
+	         if((SiS_Pr->SiS_VBType & VB_SIS301B302B) ||
+		    (SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) ||
+		    (SiS_Pr->SiS_CustomT == CUT_CLEVO1400)) {
+		    if(!(SiS_IsDualEdge(SiS_Pr,HwInfo))) {
+		       SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x00,0xdf);
+		       SiS_DisplayOff(SiS_Pr);
+		    }
+		    SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x00,0x80);
+		 } else {
+                    SiS_DisplayOff(SiS_Pr);
+	            SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x00,0x80);
+	            SiS_PanelDelay(SiS_Pr, HwInfo, 2);
+		 }
 
-    	if(HwDeviceExtension->jChipType < SIS_315H) {
+		 SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x32,0xDF);
+	         temp = SiS_GetReg(SiS_Pr->SiS_Part1Port,0x00);
+                 SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x00,0x10);
+	         SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x1E,0xDF);
+	         SiS_SetReg(SiS_Pr->SiS_Part1Port,0x00,temp);
 
-		/* ---- 300 series ---- */
+	      }
 
-      		tempah = (tempah << 5) & 0xFF;
-      		SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x01,tempah);
-      		tempah = (tempah >> 5) & 0xFF;
+	      if((SiS_Pr->SiS_VBType & VB_SIS301LV302LV) &&
+	         (SiS_Pr->SiS_CustomT != CUT_COMPAQ1280) &&
+		 (SiS_Pr->SiS_CustomT != CUT_CLEVO1400)) {
 
-    	} else {
+		 SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x1f,~0x10);
 
-		/* ---- 315 series ---- */
+	         tempah = 0x3f;
+	         if(SiS_IsDualEdge(SiS_Pr,HwInfo)) {
+	            tempah = 0x7f;
+	            if(!(SiS_IsVAMode(SiS_Pr,HwInfo))) {
+		       tempah = 0xbf;
+                    }
+	         }
+	         SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x1F,tempah);
 
-      		SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2E,0xF8,tempah);
+		 if(SiS_IsNotM650orLater(SiS_Pr,HwInfo)) {
+	            SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2e,0x7f);
+		 }
 
-    	}
+	         if(!(SiS_IsVAMode(SiS_Pr,HwInfo))) {
+	            SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x00,0xdf);
+	            if(!(SiS_CRT2IsLCD(SiS_Pr,HwInfo))) {
+	               if(!(SiS_IsDualEdge(SiS_Pr,HwInfo))) {
+		          SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x26,0xFD);
+                       }
+                    }
+	         }
 
-    	if((SiS_Pr->SiS_ModeType == ModeVGA) && (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode))) {
-      		tempah |= 0x10;
-	}
+	         SiS_SetReg(SiS_Pr->SiS_P3c4,0x06,pushax);
 
-	if((HwDeviceExtension->jChipType < SIS_315H) && (SiS_Pr->SiS_VBType & VB_SIS301)) {
-		if((SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) ||
-		   (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x960)) {
-			tempah |= 0x80;
-		}
-	} else {
-		tempah |= 0x80;
-	}
+		 if(SiS_Pr->SiS_VBType & (VB_SIS302LV | VB_SIS302ELV)) {
+	            if( (SiS_IsVAMode(SiS_Pr, HwInfo)) ||
+	                (SiS_CRT2IsLCD(SiS_Pr, HwInfo)) ) {
+		       SiS_PanelDelayLoop(SiS_Pr, HwInfo, 3, 20);
+		    }
+	         }
 
-    	if(SiS_Pr->SiS_VBInfo & (SetCRT2ToTV - SetCRT2ToHiVisionTV)) {
-      		if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
-			if(!(SiS_Pr->SiS_HiVision & 0x03)) {
-          			tempah |= 0x20;
-			}
-      		}
-    	}
+  	      } else if(SiS_Pr->SiS_VBType & VB_NoLCD) {
 
-    	SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x0D,0x40,tempah);
+	         /* NIL */
 
-    	tempah = 0;
-    	if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
-	   if(!(SiS_Pr->SiS_HiVision & 0x03)) {
-      	      if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
-	         if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV)) {
-       		    if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-            	       SiS_Pr->SiS_SetFlag |= RPLLDIV2XO;
-            	       tempah |= 0x40;
-       		    } else {
-        	       if(!(SiS_Pr->SiS_SetFlag & TVSimuMode)) {
-          		  SiS_Pr->SiS_SetFlag |= RPLLDIV2XO;
-            		  tempah |= 0x40;
-        	       }
-      		    }
+	      } else if((SiS_Pr->SiS_VBType & VB_SIS301B302B) ||
+	                (SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) ||
+			(SiS_Pr->SiS_CustomT == CUT_CLEVO1400)) {
+
+		 if(!(SiS_IsNotM650orLater(SiS_Pr,HwInfo))) {
+	            tempah = 0xef;
+	            if(SiS_IsDualEdge(SiS_Pr,HwInfo)) {
+		       if(modenum > 0x13) {
+	                  tempah = 0xf7;
+		       }
+                    }
+	            SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x4c,tempah);
+		 }
+		 if((SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) ||
+		    (SiS_Pr->SiS_CustomT == CUT_CLEVO1400)) {
+		    if((SiS_IsVAMode(SiS_Pr,HwInfo)) ||
+		       (!(SiS_IsDualEdge(SiS_Pr,HwInfo)))) {
+		       if((!(SiS_WeHaveBacklightCtrl(SiS_Pr, HwInfo))) ||
+		          (!(SiS_CRT2IsLCD(SiS_Pr, HwInfo)))) {
+			  SiS_PanelDelay(SiS_Pr, HwInfo, 2);
+	     	          SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x26,0xFD);
+			  SiS_PanelDelay(SiS_Pr, HwInfo, 4);
+	               }
+		    }
 		 }
-     	      } else {
-        	SiS_Pr->SiS_SetFlag |= RPLLDIV2XO;
-        	tempah |= 0x40;
-      	      }
-	   }
-    	}
 
-	/* For 302LV dual-channel */
-	if(HwDeviceExtension->jChipType >= SIS_315H) {
-	   if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-	      if(SiS_IsDualLink(SiS_Pr, HwDeviceExtension, BaseAddr))
-	         tempah |= 0x40;
-	   }
-	}
+	      }
 
-	if((SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) ||
-	   (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;
-	}
+	  } else {			/* 315, 330 - all bridge types */
 
-    	SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x0C,tempah);
+	     if(SiS_Is301B(SiS_Pr)) {
+	        tempah = 0x3f;
+	        if(SiS_IsDualEdge(SiS_Pr,HwInfo)) {
+	           tempah = 0x7f;
+	           if(!(SiS_IsVAMode(SiS_Pr,HwInfo))) {
+		      tempah = 0xbf;
+                   }
+	        }
+	        SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x1F,tempah);
+	        if(SiS_IsVAMode(SiS_Pr,HwInfo)) {
+	           SiS_DisplayOff(SiS_Pr);
+		   SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x32,0xDF);
+	        }
+	     }
+	     if( (!(SiS_Is301B(SiS_Pr))) ||
+	         (!(SiS_IsVAMode(SiS_Pr,HwInfo))) ) {
 
-    } else {
+ 	 	if( (!(SiS_Is301B(SiS_Pr))) ||
+		    (!(SiS_IsDualEdge(SiS_Pr,HwInfo))) ) {
 
-    	/* 3. for LVDS */
+	           SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x00,0xDF);
+	           SiS_DisplayOff(SiS_Pr);
 
-	if(HwDeviceExtension->jChipType >= SIS_315H) {
+		}
 
-	   /* Inserted this entire section (BIOS 650/LVDS); added ModeType check
-	    *     (LVDS can only be slave in 8bpp modes)
-	    */
-	   tempah = 0x80;
-	   if( (modeflag & CRT2Mode) && (SiS_Pr->SiS_ModeType > ModeVGA) ) {
-	       if (SiS_Pr->SiS_VBInfo & DriverMode) {
-	           tempah |= 0x02;
-	       }
-	   }
+                SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x00,0x80);
 
-	   if(!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
-               tempah |= 0x02;
-    	   }
+                SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x32,0xDF);
 
-	   if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
-	       tempah ^= 0x01;
-	   }
+	        temp = SiS_GetReg(SiS_Pr->SiS_Part1Port,0x00);
+                SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x00,0x10);
+	        SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x1E,0xDF);
+	        SiS_SetReg(SiS_Pr->SiS_Part1Port,0x00,temp);
 
-	   if(SiS_Pr->SiS_VBInfo & DisableCRT2Display) {
-	       tempah = 1;
-	   }
+	     }
 
-    	   SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2e,0xF0,tempah);
+	  }    /* 315/330 */
 
-	} else {
+#endif /* SIS315H */
 
-	   /* (added ModeType check) */
-	   tempah = 0;
-	   if( (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) && (SiS_Pr->SiS_ModeType > ModeVGA) ) {
-               	  tempah |= 0x02;
-    	   }
-	   tempah <<= 5;
+	}
 
-	   if(SiS_Pr->SiS_VBInfo & DisableCRT2Display)  tempah = 0;
+      } else {     /* ============ For 301 ================ */
+
+        if(HwInfo->jChipType < SIS_315H) {
+           if(!(SiS_CR36BIOSWord23b(SiS_Pr,HwInfo))) {
+	      SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x11,0xF7,0x08);
+	      SiS_PanelDelay(SiS_Pr, HwInfo, 3);
+	   }
+	}
 
-	   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x01,tempah);
+        SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x00,0xDF);           /* disable VB */
+        SiS_DisplayOff(SiS_Pr);
 
+        if(HwInfo->jChipType >= SIS_315H) {
+           SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x00,0x80);
 	}
 
-    }
+        SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x32,0xDF);                /* disable lock mode */
 
-  }
+	if(HwInfo->jChipType >= SIS_315H) {
+            temp = SiS_GetReg(SiS_Pr->SiS_Part1Port,0x00);
+            SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x00,0x10);
+	    SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x1E,0x20);
+	    SiS_SetReg(SiS_Pr->SiS_Part1Port,0x00,temp);
+	} else {
+            SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x1E,0xDF);            /* disable CRT2 */
+	    if( (!(SiS_CRT2IsLCD(SiS_Pr, HwInfo))) ||
+	        (!(SiS_CR36BIOSWord23d(SiS_Pr,HwInfo))) ) {
+		SiS_PanelDelay(SiS_Pr, HwInfo, 2);
+		SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x11,0xFB,0x04);
+	    }
+	}
 
-  /* Inserted the entire following section */
+      }
 
-  if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
+  } else {     /* ============ For LVDS =============*/
 
-      if(HwDeviceExtension->jChipType >= SIS_315H) {
+    if(HwInfo->jChipType < SIS_315H) {
 
-#ifdef SIS315H
+#ifdef SIS300	/* 300 series */
 
-         unsigned char bridgerev = SiS_GetReg1(SiS_Pr->SiS_Part4Port,0x01);;
+	if(SiS_Pr->SiS_IF_DEF_CH70xx == 1) {
+	   SiS_SetCH700x(SiS_Pr,0x090E);
+	}
 
-	 /* 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...?
-	  */
+	if(HwInfo->jChipType == SIS_730) {
+	   if(!(SiS_GetReg(SiS_Pr->SiS_P3c4,0x11) & 0x08)) {
+	      SiS_WaitVBRetrace(SiS_Pr,HwInfo);
+	   }
+	   if(!(SiS_CR36BIOSWord23b(SiS_Pr,HwInfo))) {
+	      SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x11,0xF7,0x08);
+	      SiS_PanelDelay(SiS_Pr, HwInfo, 3);
+	   }
+	} else {
+	   if(!(SiS_GetReg(SiS_Pr->SiS_P3c4,0x11) & 0x08)) {
+	      if(!(SiS_GetReg(SiS_Pr->SiS_P3c4,0x13) & 0x40)) {
+  	         if(!(SiS_CR36BIOSWord23b(SiS_Pr,HwInfo))) {
+                    SiS_WaitVBRetrace(SiS_Pr,HwInfo);
+		    if(!(SiS_GetReg(SiS_Pr->SiS_P3c4,0x06) & 0x1c)) {
+		       SiS_DisplayOff(SiS_Pr);
+	            }
+	            SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x11,0xF7,0x08);
+	            SiS_PanelDelay(SiS_Pr, HwInfo, 3);
+                 }
+              }
+	   }
+	}
 
-	 /* 740 variants match for 30xB, 301B-DH, 30xLV */
+	SiS_DisplayOff(SiS_Pr);
 
-         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)) {
-	          tempbl = 0xff;
-	       }
-            }
-            SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x13,tempbl,tempah);   
-	 }
+	SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x32,0xDF);
 
-	 /* 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?
-	  */
+	SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x1E,0xDF);
+	SiS_UnLockCRT2(SiS_Pr,HwInfo);
+	SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x01,0x80);
+	SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x02,0x40);
 
-	 if(IS_SIS740) {
-	    tempah = 0x30;
-	    tempbl = 0xcf;
-	    if(SiS_Pr->SiS_VBInfo & DisableCRT2Display) {
-	       tempah = 0x00;
-	    }
-	    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 {
-	    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( (!(SiS_CRT2IsLCD(SiS_Pr, HwInfo))) ||
+	    (!(SiS_CR36BIOSWord23d(SiS_Pr,HwInfo))) ) {
+	   SiS_PanelDelay(SiS_Pr, HwInfo, 2);
+	   SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x11,0xFB,0x04);
+	}
 
-	 if(IS_SIS740) {
-	    tempah = 0xc0;
-  	    tempbl = 0x3f;
-	    if(SiS_Pr->SiS_VBInfo & DisableCRT2Display) {
-	       tempah = 0x00;
-	    }
-	    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 {
-	    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);
-	 }
+#endif  /* SIS300 */
 
-	 if(IS_SIS740) {
-	    tempah = 0x80;
-	    tempbl = 0x7f;
-	    if(SiS_Pr->SiS_VBInfo & DisableCRT2Display) {
-	       tempah = 0x00;
-	    } 
-	    SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x23,tempbl,tempah);
-	 } else {
-	    tempah = 0x00;						/* For all bridges */	
-            tempbl = 0x7f;
-            if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)) {
-               tempbl = 0xff;
-	       if(!(SiS_IsDualEdge(SiS_Pr, HwDeviceExtension, BaseAddr))) {
-	          tempah |= 0x80;
-	       }
-            }
-            SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x23,tempbl,tempah);
-	 }
+    } else {
 
-#endif /* SIS315H */
+#ifdef SIS315H	/* 315 series */
 
-      } else if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
+	if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
 
-         SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x21,0x3f);
+	   if(HwInfo->jChipType == SIS_740) {
+	      temp = SiS_GetCH701x(SiS_Pr,0x61);
+	      if(temp < 1) {
+	         SiS_SetCH701x(SiS_Pr,0xac76);
+	         SiS_SetCH701x(SiS_Pr,0x0066);
+	      }
 
-         if((SiS_Pr->SiS_VBInfo & DisableCRT2Display) ||
-            (   (SiS_Pr->SiS_VBType & VB_NoLCD) &&
-	        (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) ) ) {
-	    SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x23,0x7F);
-	 } else {
-	    SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x23,0x80);
-	 }
+	      if( (!(SiS_IsDualEdge(SiS_Pr,HwInfo))) ||
+	          (SiS_IsTVOrYPbPrOrScart(SiS_Pr,HwInfo)) ) {
+	  	 SiS_SetCH701x(SiS_Pr,0x3e49);
+	      }
+	   }
 
-      }
+	   if( (!(SiS_IsDualEdge(SiS_Pr,HwInfo))) ||
+	       (SiS_IsVAMode(SiS_Pr,HwInfo)) ) {
+	      SiS_Chrontel701xBLOff(SiS_Pr);
+	      SiS_Chrontel701xOff(SiS_Pr,HwInfo);
+	   }
 
-  } else {  /* LVDS */
+	   if(HwInfo->jChipType != SIS_740) {
+	      if( (!(SiS_IsDualEdge(SiS_Pr,HwInfo))) ||
+	          (SiS_IsTVOrYPbPrOrScart(SiS_Pr,HwInfo)) ) {
+	   	 SiS_SetCH701x(SiS_Pr,0x0149);
+  	      } 
+	   }
 
-#ifdef SIS315H
-      if(HwDeviceExtension->jChipType >= SIS_315H) {
+	}
 
-         if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
+	if(SiS_Pr->SiS_IF_DEF_CH70xx == 0) {
+	   SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x11,0xF7,0x08);
+	   SiS_PanelDelay(SiS_Pr, HwInfo, 3);
+	}
 
-            tempah = 0x04;
-	    tempbl = 0xfb;
-            if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)) {
-               tempah = 0x00;
-	       if(SiS_IsDualEdge(SiS_Pr, HwDeviceExtension, BaseAddr)) {
-	          tempbl = 0xff;
-	       }
-            }
-	    SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x13,tempbl,tempah);
+	if( (SiS_Pr->SiS_IF_DEF_CH70xx == 0)   ||
+	    (!(SiS_IsDualEdge(SiS_Pr,HwInfo))) ||
+	    (!(SiS_IsTVOrYPbPrOrScart(SiS_Pr,HwInfo))) ) {
+	   SiS_DisplayOff(SiS_Pr);
+	}
 
-	    if(SiS_Pr->SiS_VBInfo & DisableCRT2Display) {
-	       SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x13,0xfb,0x00);
-	    }
+	if( (SiS_Pr->SiS_IF_DEF_CH70xx == 0)   ||
+	    (!(SiS_IsDualEdge(SiS_Pr,HwInfo))) ||
+	    (!(SiS_IsVAMode(SiS_Pr,HwInfo))) ) {
+	   SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x00,0x80);
+	}
 
-	    SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2c,0xcf,0x30);
+	if(HwInfo->jChipType == SIS_740) {
+	   SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2e,0x7f);
+	}
 
-	 } else if(HwDeviceExtension->jChipType == SIS_550) {
+	SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x32,0xDF);
 
-#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);
+	if( (SiS_Pr->SiS_IF_DEF_CH70xx == 0)   ||
+	    (!(SiS_IsDualEdge(SiS_Pr,HwInfo))) ||
+	    (!(SiS_IsVAMode(SiS_Pr,HwInfo))) ) {
+	   SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x1E,0xDF);
+	}
 
-	    SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2c,0xcf,0x30);
-	 }
+	if(SiS_Pr->SiS_IF_DEF_CH70xx == 0) {
+	   if(SiS_CRT2IsLCD(SiS_Pr, HwInfo)) {
+	      SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x1e,0xdf);
+	      if(HwInfo->jChipType == SIS_550) {
+	         SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x1e,0xbf);
+	         SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x1e,0xef);
+	      }
+	   }
+	} else {
+	   if(HwInfo->jChipType == SIS_740) {
+	      if(SiS_IsLCDOrLCDA(SiS_Pr,HwInfo)) {
+	         SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x1e,0xdf);
+	      }
+	   } else if(SiS_IsVAMode(SiS_Pr,HwInfo)) {
+	      SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x1e,0xdf);
+	   }
+	}
 
-      }
-#endif
+	if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
+	   if(SiS_IsDualEdge(SiS_Pr,HwInfo)) {
+	      /* SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x13,0xff); */
+	   } else {
+	      SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x13,0xfb);
+	   }
+	}
 
-  }
+	SiS_UnLockCRT2(SiS_Pr,HwInfo);
+
+	if(HwInfo->jChipType == SIS_550) {
+	   SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x01,0x80); /* DirectDVD PAL?*/
+	   SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x02,0x40); /* VB clock / 4 ? */
+	} else if( (SiS_Pr->SiS_IF_DEF_CH70xx == 0)   ||
+	           (!(SiS_IsDualEdge(SiS_Pr,HwInfo))) ||
+		   (!(SiS_IsVAMode(SiS_Pr,HwInfo))) ) {
+	   SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2e,0xf7);
+	}
+
+        if(SiS_Pr->SiS_IF_DEF_CH70xx == 0) {
+	   if(SiS_CRT2IsLCD(SiS_Pr, HwInfo)) {
+	      if(!(SiS_WeHaveBacklightCtrl(SiS_Pr,HwInfo))) {
+	 	 SiS_PanelDelay(SiS_Pr, HwInfo, 2);
+		 SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x11,0xFB,0x04);
+	      }
+	   }
+        }
+
+#endif  /* SIS315H */
+
+    }  /* 310 series */
+
+  }  /* LVDS */
 
 }
 
+/*********************************************/
+/*            ENABLE VIDEO BRIDGE            */
+/*********************************************/
+
+/* NEVER use any variables (VBInfo), this will be called
+ * from outside the context of a mode switch!
+ * MUST call getVBType before calling this
+ */
 void
-SiS_GetCRT2Data(SiS_Private *SiS_Pr, UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex,
-                USHORT RefreshRateTableIndex,
-		PSIS_HW_DEVICE_INFO HwDeviceExtension)
+SiS_EnableBridge(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
 {
+  USHORT temp=0,tempah;
+#ifdef SIS315H
+  USHORT temp1,pushax=0;
+  BOOLEAN delaylong = FALSE;
+#endif
 
-  if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
-
-     if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
 
-        if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
+  if(SiS_Pr->SiS_VBType & VB_SISVB) {
 
-           SiS_GetCRT2DataLVDS(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
-	                      HwDeviceExtension);
-        } else {
+    if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {   /* ====== For 301B et al  ====== */
 
-	   if( (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) && (SiS_Pr->SiS_VBType & VB_NoLCD) ) {
+      if(HwInfo->jChipType < SIS_315H) {
 
-	      /* Need LVDS Data for LCD on 301B-DH */
-	      SiS_GetCRT2DataLVDS(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
-	                          HwDeviceExtension);
-				  
-	   } else {
-	
-	      SiS_GetCRT2Data301(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
-	                         HwDeviceExtension);
-           }
+#ifdef SIS300     /* 300 series */
 
-        }
+         if(HwInfo->jChipType == SIS_300) {
 
-     } else {
+	    if(SiS_CRT2IsLCD(SiS_Pr, HwInfo)) {
+	       if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
+	          SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x26,0x02);
+	          if(!(SiS_CR36BIOSWord23d(SiS_Pr, HwInfo))) {
+	             SiS_PanelDelay(SiS_Pr, HwInfo, 0);
+	          }
+	       }
+	    }
+	    temp = SiS_GetReg(SiS_Pr->SiS_P3c4,0x32) & 0xDF;             /* lock mode */
+            if(SiS_BridgeInSlave(SiS_Pr)) {
+               tempah = SiS_GetReg(SiS_Pr->SiS_P3d4,0x30);
+               if(!(tempah & SetCRT2ToRAMDAC))  temp |= 0x20;
+            }
+            SiS_SetReg(SiS_Pr->SiS_P3c4,0x32,temp);
+	    SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x1E,0x20);
+	    SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x00,0x1F,0x20);        /* enable VB processor */
+	    SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x1F,0xC0);
+	    SiS_DisplayOn(SiS_Pr);
+	    if(SiS_CRT2IsLCD(SiS_Pr, HwInfo)) {
+	       if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
+	          if(!(SiS_GetReg(SiS_Pr->SiS_P3c4,0x16) & 0x10)) {
+		     if(!(SiS_CR36BIOSWord23b(SiS_Pr,HwInfo))) {
+		        SiS_PanelDelay(SiS_Pr, HwInfo, 1);
+                     }
+		     SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x26,0x01);
+	 	  }
+	       }
+	    }
 
-     	SiS_GetCRT2Data301(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
-	                   HwDeviceExtension);
-     }
+	 } else {
 
-  } else {
-  
-     SiS_GetCRT2DataLVDS(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
-                         HwDeviceExtension);
-  }
-}
+	    if((SiS_Pr->SiS_VBType & VB_NoLCD) &&
+	       (SiS_CRT2IsLCD(SiS_Pr, HwInfo))) {
+	       /* This is only for LCD output on 301B-DH via LVDS */
+	       SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x11,0xFB);
+	       if(!(SiS_CR36BIOSWord23d(SiS_Pr,HwInfo))) {
+	          SiS_PanelDelay(SiS_Pr, HwInfo, 0);
+	       }
+	       SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x1E,0x20);   /* Enable CRT2 */
+               SiS_DisplayOn(SiS_Pr);
+	       SiS_UnLockCRT2(SiS_Pr,HwInfo);
+	       SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x02,0xBF);
+	       if(SiS_BridgeInSlave(SiS_Pr)) {
+      		  SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x01,0x1F);
+      	       } else {
+      		  SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x01,0x1F,0x40);
+               }
+	       if(!(SiS_GetReg(SiS_Pr->SiS_P3c4,0x13) & 0x40)) {
+	           if(!(SiS_GetReg(SiS_Pr->SiS_P3c4,0x16) & 0x10)) {
+		       if(!(SiS_CR36BIOSWord23b(SiS_Pr,HwInfo))) {
+		           SiS_PanelDelay(SiS_Pr, HwInfo, 1);
+                       }
+		       SiS_WaitVBRetrace(SiS_Pr,HwInfo);
+                       SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x11,0xF7,0x00);
+                   }
+	       }
+            } else {
+	       temp = SiS_GetReg(SiS_Pr->SiS_P3c4,0x32) & 0xDF;             /* lock mode */
+               if(SiS_BridgeInSlave(SiS_Pr)) {
+                  tempah = SiS_GetReg(SiS_Pr->SiS_P3d4,0x30);
+                  if(!(tempah & SetCRT2ToRAMDAC))  temp |= 0x20;
+               }
+               SiS_SetReg(SiS_Pr->SiS_P3c4,0x32,temp);
+	       SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x1E,0x20);
+	       SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x00,0x1F,0x20);        /* enable VB processor */
+	       SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x1F,0xC0);
+	       SiS_DisplayOn(SiS_Pr);
+	    }
 
-/* Checked with 650/LVDS 1.10.07 BIOS */
-void
-SiS_GetCRT2DataLVDS(SiS_Private *SiS_Pr, UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex,
-                    USHORT RefreshRateTableIndex,
-		    PSIS_HW_DEVICE_INFO HwDeviceExtension)
-{
-   USHORT CRT2Index, ResIndex;
-   const SiS_LVDSDataStruct *LVDSData = NULL;
+         }
+#endif /* SIS300 */
 
-   SiS_GetCRT2ResInfo(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,HwDeviceExtension);
-   
-   if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-      SiS_Pr->SiS_RVBHCMAX  = 1;
-      SiS_Pr->SiS_RVBHCFACT = 1;
-      SiS_Pr->SiS_NewFlickerMode = 0;
-      SiS_Pr->SiS_RVBHRS = 50;
-      SiS_Pr->SiS_RY1COE = 0;
-      SiS_Pr->SiS_RY2COE = 0;
-      SiS_Pr->SiS_RY3COE = 0;
-      SiS_Pr->SiS_RY4COE = 0;
-   }
+      } else {
 
-   if((SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) && (SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)) {
+#ifdef SIS315H    /* 315 series */
 
-#ifdef SIS315H   
-      SiS_GetCRT2PtrA(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
-                      &CRT2Index,&ResIndex);
+	 if(IS_SIS550650740660) {		/* 550, 650, 740, 660 */
 
-      switch (CRT2Index) {
-      	case  0:  LVDSData = SiS_Pr->SiS_LVDS1024x768Data_1;    break;
-      	case  1:  LVDSData = SiS_Pr->SiS_LVDS1280x1024Data_1;   break;
-        case  2:  LVDSData = SiS_Pr->SiS_LVDS1280x960Data_1;    break;
-	case  3:  LVDSData = SiS_Pr->SiS_LCDA1400x1050Data_1;   break;
-	case  4:  LVDSData = SiS_Pr->SiS_LCDA1600x1200Data_1;   break;
-      	case  5:  LVDSData = SiS_Pr->SiS_LVDS1024x768Data_2;    break;
-      	case  6:  LVDSData = SiS_Pr->SiS_LVDS1280x1024Data_2;   break;
-      	case  7:  LVDSData = SiS_Pr->SiS_LVDS1280x960Data_2;    break;
-	case  8:  LVDSData = SiS_Pr->SiS_LCDA1400x1050Data_2;   break;
-	case  9:  LVDSData = SiS_Pr->SiS_LCDA1600x1200Data_2;   break;
-	default:  LVDSData = SiS_Pr->SiS_LVDS1024x768Data_1;    break;
-      }
-#endif      
+	    UCHAR r30=0, r31=0, r32=0, r33=0, cr36=0;
 
-   } else {
+	    if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
 
-      /* 301BDH needs LVDS Data */
-      if( (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) && (SiS_Pr->SiS_VBType & VB_NoLCD) ) {
-	      SiS_Pr->SiS_IF_DEF_LVDS = 1;
-      }
+	       if((SiS_Pr->SiS_CustomT != CUT_COMPAQ1280) &&
+	          (SiS_Pr->SiS_CustomT != CUT_CLEVO1400)) {
+	          SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x1f,0xef);
+#ifdef SET_EMI
+		  if(SiS_Pr->SiS_VBType & (VB_SIS302LV | VB_SIS302ELV)) {
+	             SiS_SetReg(SiS_Pr->SiS_Part4Port,0x30,0x00);
+		  }
+#endif
+	       }
 
-      SiS_GetCRT2Ptr(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
-                     &CRT2Index,&ResIndex,HwDeviceExtension);
+               if(!(SiS_IsNotM650orLater(SiS_Pr,HwInfo))) {
+	          tempah = 0x10;
+		  if((SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) ||
+		     (SiS_Pr->SiS_CustomT == CUT_CLEVO1400)) {
+		     if(SiS_GetReg(SiS_Pr->SiS_Part1Port,0x13) & 0x04) {
+		        if((SiS_GetReg(SiS_Pr->SiS_Part2Port,0x00) & 0x0f) == 0x0c) {
+			   tempah = 0x08;
+			} else {
+			   tempah = 0x18;
+			}
+		     }
+		     SiS_SetReg(SiS_Pr->SiS_Part1Port,0x4c,tempah);
+		  } else {
+	             if(SiS_IsVAMode(SiS_Pr,HwInfo)) {
+	                tempah = 0x08;
+                     }
+		     SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x4c,tempah);
+		  }
+	       }
 
-      /* 301BDH needs LVDS Data */
-      if( (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) && (SiS_Pr->SiS_VBType & VB_NoLCD) ) {
-              SiS_Pr->SiS_IF_DEF_LVDS = 0;
-      }
+	       if((SiS_Pr->SiS_CustomT != CUT_COMPAQ1280) &&
+	          (SiS_Pr->SiS_CustomT != CUT_CLEVO1400)) {
+	          SiS_SetRegByte(SiS_Pr->SiS_P3c6,0x00);
+	          SiS_DisplayOff(SiS_Pr);
+	          pushax = SiS_GetReg(SiS_Pr->SiS_P3c4,0x06);
+	          if(IS_SIS740) {
+	             SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x06,0xE3);
+	          }
+	       }
 
-      switch (CRT2Index) {
-      	case  0:  LVDSData = SiS_Pr->SiS_LVDS800x600Data_1;    break;
-      	case  1:  LVDSData = SiS_Pr->SiS_LVDS1024x768Data_1;   break;
-      	case  2:  LVDSData = SiS_Pr->SiS_LVDS1280x1024Data_1;  break;
-      	case  3:  LVDSData = SiS_Pr->SiS_LVDS800x600Data_2;    break;
-      	case  4:  LVDSData = SiS_Pr->SiS_LVDS1024x768Data_2;   break;
-      	case  5:  LVDSData = SiS_Pr->SiS_LVDS1280x1024Data_2;  break;
-	case  6:  LVDSData = SiS_Pr->SiS_LVDS640x480Data_1;    break;
-        case  7:  LVDSData = SiS_Pr->SiS_LVDSXXXxXXXData_1;    break;
-	case  8:  LVDSData = SiS_Pr->SiS_LVDS1400x1050Data_1;  break;
-	case  9:  LVDSData = SiS_Pr->SiS_LVDS1400x1050Data_2;  break;
-      	case 10:  LVDSData = SiS_Pr->SiS_CHTVUNTSCData;        break;
-      	case 11:  LVDSData = SiS_Pr->SiS_CHTVONTSCData;        break;
-      	case 12:  LVDSData = SiS_Pr->SiS_CHTVUPALData;         break;
-      	case 13:  LVDSData = SiS_Pr->SiS_CHTVOPALData;         break;
-      	case 14:  LVDSData = SiS_Pr->SiS_LVDS320x480Data_1;    break;
-	case 15:  LVDSData = SiS_Pr->SiS_LVDS1024x600Data_1;   break;
-	case 16:  LVDSData = SiS_Pr->SiS_LVDS1152x768Data_1;   break;
-	case 17:  LVDSData = SiS_Pr->SiS_LVDS1024x600Data_2;   break;
-	case 18:  LVDSData = SiS_Pr->SiS_LVDS1152x768Data_2;   break;
-	case 19:  LVDSData = SiS_Pr->SiS_LVDS1280x768Data_1;   break;
-	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 84:  LVDSData = SiS_Pr->SiS_LVDS848x480Data_1;    break;
-	case 85:  LVDSData = SiS_Pr->SiS_LVDS848x480Data_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;  /* Super Overscan */
-	default:  LVDSData = SiS_Pr->SiS_LVDS1024x768Data_1;   break;
-     }
-   }
+	       if( (SiS_IsVAMode(SiS_Pr,HwInfo)) ||
+	           (SiS_CRT2IsLCD(SiS_Pr, HwInfo)) ) {
+                  if(!(SiS_GetReg(SiS_Pr->SiS_Part4Port,0x26) & 0x02)) {
+		     if((SiS_Pr->SiS_CustomT != CUT_COMPAQ1280) &&
+		        (SiS_Pr->SiS_CustomT != CUT_CLEVO1400)) {
+		        SiS_PanelDelayLoop(SiS_Pr, HwInfo, 3, 2);
+			SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x26,0x02);
+			if(SiS_Pr->SiS_VBType & (VB_SIS302LV | VB_SIS302ELV)) {
+			   SiS_GenericDelay(SiS_Pr, 0x4500);
+			}
+	                SiS_PanelDelayLoop(SiS_Pr, HwInfo, 3, 2);
+		     } else {
+		        SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x26,0x02);
+			SiS_PanelDelay(SiS_Pr, HwInfo, 0);
+		     }
+	          }
+	       }
 
-   SiS_Pr->SiS_VGAHT = (LVDSData+ResIndex)->VGAHT;
-   SiS_Pr->SiS_VGAVT = (LVDSData+ResIndex)->VGAVT;
-   SiS_Pr->SiS_HT    = (LVDSData+ResIndex)->LCDHT;
-   SiS_Pr->SiS_VT    = (LVDSData+ResIndex)->LCDVT;
+               if((SiS_Pr->SiS_CustomT != CUT_COMPAQ1280) &&
+	          (SiS_Pr->SiS_CustomT != CUT_CLEVO1400)) {
+	          if(!(SiS_GetReg(SiS_Pr->SiS_P3d4,0x31) & 0x40)) {
+                     SiS_PanelDelayLoop(SiS_Pr, HwInfo, 3, 10);
+		     delaylong = TRUE;
+		  }
+	       }
 
-  if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
+	    } else if(SiS_Pr->SiS_VBType & VB_NoLCD) {
 
-     if(!(SiS_Pr->SiS_LCDInfo & DontExpandLCD)) {
-        SiS_Pr->SiS_HDE = SiS_Pr->PanelXRes;
-        SiS_Pr->SiS_VDE = SiS_Pr->PanelYRes;
-     }
+	       if(!(SiS_IsNotM650orLater(SiS_Pr,HwInfo))) {
+	          SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x4c,0x10);
+	       }
 
-  } else {
+  	    } else if(SiS_Pr->SiS_VBType & VB_SIS301B302B) {
 
-     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		 
-              }
-           }
-        }
-     }
-  }
-}
+	       if(!(SiS_IsNotM650orLater(SiS_Pr,HwInfo))) {
+	          tempah = 0x10;
+		  if(SiS_GetReg(SiS_Pr->SiS_Part1Port,0x13) & 0x04) {
+		     tempah = 0x18;
+		     if((SiS_GetReg(SiS_Pr->SiS_Part2Port,0x00) & 0x0f) == 0x0c) {
+		        tempah = 0x08;
+		     }
+		  }
+	          SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x4c,tempah);
+	       }
+	       
+	    }
 
-void
-SiS_GetCRT2Data301(SiS_Private *SiS_Pr, UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex,
-                   USHORT RefreshRateTableIndex,
-		   PSIS_HW_DEVICE_INFO HwDeviceExtension)
-{
-  USHORT tempax,tempbx,modeflag;
-  USHORT resinfo;
-  USHORT CRT2Index,ResIndex;
-  const SiS_LCDDataStruct *LCDPtr = NULL;
-  const SiS_TVDataStruct  *TVPtr  = NULL;
-
-  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;
-     }
-  }
-  
-  SiS_Pr->SiS_NewFlickerMode = 0;
-  SiS_Pr->SiS_RVBHRS = 50;
-  SiS_Pr->SiS_RY1COE = 0;
-  SiS_Pr->SiS_RY2COE = 0;
-  SiS_Pr->SiS_RY3COE = 0;
-  SiS_Pr->SiS_RY4COE = 0;
-
-  SiS_GetCRT2ResInfo(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,HwDeviceExtension);
-
-  if(SiS_Pr->SiS_VBInfo & SetCRT2ToRAMDAC){
-
-     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;
-
-     } 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);
-
-    switch (CRT2Index) {
-      case  2:  TVPtr = SiS_Pr->SiS_ExtHiTVData;   break;
-/*    case  7:  TVPtr = SiS_Pr->SiS_St1HiTVData;   break;  */
-      case 12:  TVPtr = SiS_Pr->SiS_St2HiTVData;   break;
-      case  3:  TVPtr = SiS_Pr->SiS_ExtPALData;    break;
-      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;  /* Just to avoid a crash */
-    }
-
-    SiS_Pr->SiS_RVBHCMAX  = (TVPtr+ResIndex)->RVBHCMAX;
-    SiS_Pr->SiS_RVBHCFACT = (TVPtr+ResIndex)->RVBHCFACT;
-    SiS_Pr->SiS_VGAHT     = (TVPtr+ResIndex)->VGAHT;
-    SiS_Pr->SiS_VGAVT     = (TVPtr+ResIndex)->VGAVT;
-    SiS_Pr->SiS_HDE       = (TVPtr+ResIndex)->TVHDE;
-    SiS_Pr->SiS_VDE       = (TVPtr+ResIndex)->TVVDE;
-    SiS_Pr->SiS_RVBHRS    = (TVPtr+ResIndex)->RVBHRS;
-    SiS_Pr->SiS_NewFlickerMode = (TVPtr+ResIndex)->FlickerMode;
-    if(modeflag & HalfDCLK) {
-	SiS_Pr->SiS_RVBHRS     = (TVPtr+ResIndex)->HALFRVBHRS;
-    }
-
-    if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) {  
-    
-       if(SiS_Pr->SiS_HiVision != 3) {
-      	  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:
-       case 1:
-       case 0:
-          SiS_Pr->SiS_HT = 0x6b4;
-          SiS_Pr->SiS_VT = 0x20d;
-	  /* Don't care about TVSimuMode */
-          break;
-       default:
-          if(SiS_Pr->SiS_VGAVDE == 350) SiS_Pr->SiS_SetFlag |= TVSimuMode;
-
-          SiS_Pr->SiS_HT = ExtHiTVHT;
-          SiS_Pr->SiS_VT = ExtHiTVVT;
-          if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
-             if(SiS_Pr->SiS_SetFlag & TVSimuMode) {
-                SiS_Pr->SiS_HT = StHiTVHT;
-                SiS_Pr->SiS_VT = StHiTVVT;
-                if(!(modeflag & Charx8Dot)){
-                   SiS_Pr->SiS_HT = StHiTextTVHT;
-                   SiS_Pr->SiS_VT = StHiTextTVVT;
-                }
-             }
-          }
-       }
-
-    } 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 == 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;
-       }
-
-    }
-
-  } else if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
-
-     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;
-
-     } else {
-
-        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 */
-#ifdef SIS315H
-	 case 50: LCDPtr = (SiS_LCDDataStruct *)SiS310_ExtCompaq1280x1024Data;	break;
-	 case 51: LCDPtr = SiS_Pr->SiS_NoScaleData1280x1024;			break;
-	 case 52: LCDPtr = SiS_Pr->SiS_St2LCD1280x1024Data;	  		break;
-#endif
-         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
-
-	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 if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) {
-           tempax = 1600;
-           if     (SiS_Pr->SiS_VGAVDE == 350)  tempbx = 875;
-           else if(SiS_Pr->SiS_VGAVDE == 400)  tempbx = 1000;
-           else                                tempbx = 1200;
-        } 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;
-     }
-  }
-}
-
-USHORT
-SiS_GetResInfo(SiS_Private *SiS_Pr, UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex)
-{
-  USHORT resindex;
-
-  if(ModeNo <= 0x13)
-     resindex=SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ResInfo;
-  else
-     resindex=SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO;
-
-  return(resindex);
-}
-
-void
-SiS_GetCRT2ResInfo(SiS_Private *SiS_Pr, UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex,
-                   PSIS_HW_DEVICE_INFO HwDeviceExtension)
-{
-  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;
-  } else {
-     xres = SiS_Pr->SiS_ModeResInfo[resindex].HTotal;
-     yres = SiS_Pr->SiS_ModeResInfo[resindex].VTotal;
-     modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
-  }
-
-  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(!(SiS_IsVAMode(SiS_Pr,HwInfo))) {
+               temp = SiS_GetReg(SiS_Pr->SiS_P3c4,0x32) & 0xDF;
+	       if(SiS_BridgeInSlave(SiS_Pr)) {
+                  tempah = SiS_GetReg(SiS_Pr->SiS_P3d4,0x30);
+                  if(!(tempah & SetCRT2ToRAMDAC)) {
+		     if((SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) ||
+		        (SiS_Pr->SiS_CustomT == CUT_CLEVO1400)) {
+		        if(!(SiS_GetReg(SiS_Pr->SiS_Part1Port,0x13) & 0x04)) temp |= 0x20;
+		     } else temp |= 0x20;
+		  }
+               }
+               SiS_SetReg(SiS_Pr->SiS_P3c4,0x32,temp);
 
-  }
+	       SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x1E,0x20);                   /* enable CRT2 */
+	       
+	       if((SiS_Pr->SiS_VBType & VB_SIS301B302B) ||
+	          (SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) ||
+		  (SiS_Pr->SiS_CustomT == CUT_CLEVO1400)) {
+	          SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2e,0x7f);
+		  temp = SiS_GetReg(SiS_Pr->SiS_Part1Port,0x2e);
+		  if(!(temp & 0x80)) {
+		     SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2e,0x80);
+		  }
+	       } else {
+	          SiS_PanelDelay(SiS_Pr, HwInfo, 2);
+	       }
+	    } else {
+	       SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x1e,0x20);
+	    }
 
-  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) {           /* 301BDH */
-	        if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToHiVisionTV)) {
-                   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;
-	           }
-	        }
-	   } else {
-	      if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToHiVisionTV)) {
-	         if(xres == 720) xres = 640;
-	      }
-	      if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
-		 if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
-		    if(!(SiS_Pr->SiS_LCDInfo & DontExpandLCD)) {
-		       /* BIOS bug - does this regardless of scaling */
-      		       if(yres == 400) yres = 405;
-		    }
-      		    if(yres == 350) yres = 360;
-      		    if(SiS_Pr->SiS_SetFlag & LCDVESATiming) {
-        	       if(yres == 360) yres = 375;
-      		    }
-   	         } 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;
-          	          if(yres == 400) yres = 420;
-            	          if(yres == 480) yres = 525;
-        	       }
-      		    }
-    	         }
-	      }
-	   }
-	}
-  } 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;
-	   }
-	   if(SiS_Pr->SiS_IF_DEF_DSTN || SiS_Pr->SiS_IF_DEF_FSTN) {
-	      yres = 480;
-	   }
-	}
-  }
-  SiS_Pr->SiS_VGAHDE = SiS_Pr->SiS_HDE = xres;
-  SiS_Pr->SiS_VGAVDE = SiS_Pr->SiS_VDE = yres;
-}
+	    SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x00,0x1f,0x20);
 
-void
-SiS_GetCRT2Ptr(SiS_Private *SiS_Pr, UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex,
-	       USHORT RefreshRateTableIndex,USHORT *CRT2Index,USHORT *ResIndex,
-	       PSIS_HW_DEVICE_INFO HwDeviceExtension)
-{
-  USHORT tempbx=0,tempal=0;
-  USHORT Flag,resinfo=0;
+	    if((SiS_Pr->SiS_VBType & VB_SIS301B302B) ||
+	       (SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) ||
+	       (SiS_Pr->SiS_CustomT == CUT_CLEVO1400)) {
+	       temp = SiS_GetReg(SiS_Pr->SiS_Part1Port,0x2e);
+	       if(!(temp & 0x80)) {
+		  SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2e,0x80);
+	       }
+	    }
 
-  if(ModeNo <= 0x13) {
-     tempal = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_CRT2CRTC;
-  } else {
-     tempal = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
-     resinfo = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO;
-  }
+	    tempah = 0xc0;
+	    if(SiS_IsDualEdge(SiS_Pr, HwInfo)) {
+	       tempah = 0x80;
+	       if(!(SiS_IsVAMode(SiS_Pr, HwInfo))) {
+	          tempah = 0x40;
+               }
+	    }
+            SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x1F,tempah);
 
-  if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
+	    if((SiS_Pr->SiS_VBType & VB_SIS301B302B) ||
+	       (((SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) ||
+	         (SiS_Pr->SiS_CustomT == CUT_CLEVO1400))     &&
+	        (!(SiS_WeHaveBacklightCtrl(SiS_Pr,HwInfo))))) {
+               SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x00,0x7f);
+	    }
 
-    	if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {                            /* LCD */
+	    if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
 
-	        if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x960) {
-			tempbx = 15;
-		} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) {
-		        tempbx = 20;
-			if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)         tempbx = 21;
-			else if(!(SiS_Pr->SiS_SetFlag & LCDVESATiming)) tempbx = 22;
-		} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) {
-		        tempbx = 23;
-			if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)         tempbx = 24;
-			else if(!(SiS_Pr->SiS_SetFlag & LCDVESATiming)) tempbx = 25;
-#if 0
-	        } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768) {
-		        tempbx = 26;
-			if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)         tempbx = 27;
-			else if(!(SiS_Pr->SiS_SetFlag & LCDVESATiming)) tempbx = 28;
-#endif
-		} else if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
-			if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
-			   if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768)       tempbx = 13;
-			   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) tempbx = 14;
-			   else 							 tempbx = 29;
-			} else {
-			   tempbx = 29;
-			   if(ModeNo >= 0x13) {
-			      /* 1280x768 and 1280x960 have same CRT2CRTC,
-			       * so we change it here if 1280x960 is chosen
-			       */
-			      if(resinfo == SIS_RI_1280x960) tempal = 10;
-			   }
-			}
-		} else {
-      		   tempbx = SiS_Pr->SiS_LCDResInfo - SiS_Pr->SiS_Panel1024x768;
-      		   if(!(SiS_Pr->SiS_SetFlag & LCDVESATiming)) {
-        		tempbx += 5;
-                        /* GetRevisionID();  */
-			/* BIOS only adds 5 once */
-        		tempbx += 5;
-       		   }
-	        }
+	       if((SiS_Pr->SiS_CustomT != CUT_COMPAQ1280) &&
+	          (SiS_Pr->SiS_CustomT != CUT_CLEVO1400)) {
+	          SiS_PanelDelay(SiS_Pr, HwInfo, 2);
+	       }
+#ifdef COMPAQ_HACK
+	       if(SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) {
+	          SiS_PanelDelay(SiS_Pr, HwInfo, 2);
+	       }
+#endif
 
-#ifdef SIS315H
-		if(SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) {
-		   tempbx = 50;
-		   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)         tempbx = 51;
-		   else if(!(SiS_Pr->SiS_SetFlag & LCDVESATiming)) tempbx = 52;
-		}
+	       SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x1f,0x10);
+
+	       if(SiS_Pr->SiS_CustomT != CUT_CLEVO1400) {
+#ifdef SET_EMI
+	          if(SiS_Pr->SiS_VBType & (VB_SIS302LV | VB_SIS302ELV)) {
+	             SiS_SetReg(SiS_Pr->SiS_Part4Port,0x30,0x00);
+		  }
 #endif
+	          SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x27,0x0c);
+#ifdef SET_EMI
+	          if(SiS_Pr->SiS_VBType & (VB_SIS302LV | VB_SIS302ELV)) {
+
+		     cr36 = SiS_GetReg(SiS_Pr->SiS_P3d4,0x36);
+
+		     /*                                              (P4_30|0x40)  */
+		     /* Compal 1400x1050: 0x05, 0x60, 0x00                YES  (1.10.7w;  CR36=69)      */
+		     /* Compal 1400x1050: 0x0d, 0x70, 0x40                YES  (1.10.7x;  CR36=69)      */
+		     /* Acer   1280x1024: 0x12, 0xd0, 0x6b                NO   (1.10.9k;  CR36=73)      */
+		     /* Compaq 1280x1024: 0x0d, 0x70, 0x6b                YES  (1.12.04b; CR36=03)      */
+		     /* Clevo   1024x768: 0x05, 0x60, 0x33                NO   (1.10.8e;  CR36=12, DL!) */
+		     /* Clevo   1024x768: 0x0d, 0x70, 0x40 (if type == 3) YES  (1.10.8y;  CR36=?2)      */
+		     /* Clevo   1024x768: 0x05, 0x60, 0x33 (if type != 3) YES  (1.10.8y;  CR36=?2)      */
+		     /* Asus    1024x768: ?                                ?   (1.10.8o;  CR36=?2)      */
+		     /* Asus    1024x768: 0x08, 0x10, 0x3c (problematic)  YES  (1.10.8q;  CR36=22)      */
+
+		     if(SiS_Pr->HaveEMI) {
+		        r30 = SiS_Pr->EMI_30;
+			r31 = SiS_Pr->EMI_31;
+			r32 = SiS_Pr->EMI_32;
+			r33 = SiS_Pr->EMI_33;
+		     } else {
+		        r30 = 0;
+		     }
 
-     	} else {						  	/* TV */
-	
-       		if((SiS_Pr->SiS_VBType & VB_SIS301B302B) &&
-		   (SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV)) {
-         		if(SiS_Pr->SiS_VGAVDE > 480) SiS_Pr->SiS_SetFlag &= (~TVSimuMode);
-         		tempbx = 2;
-         		if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
-            			if(!(SiS_Pr->SiS_SetFlag & TVSimuMode)) tempbx = 12;
-         		}
-       		} else {
-         		if(SiS_Pr->SiS_VBInfo & SetPALTV) tempbx = 3;
-         		else tempbx = 4;
-         		if(SiS_Pr->SiS_SetFlag & TVSimuMode) tempbx += 5;
-       		}
-		
-     	}
+		     /* EMI_30 is read at driver start; however, the BIOS sets this
+		      * (if it is used) only if the LCD is in use. In case we caught
+		      * the machine while on TV output, this bit is not set and we
+		      * don't know if it should be set - hence our detection is wrong.
+		      * Work-around this here:
+		      */
+
+		     if((!SiS_Pr->HaveEMI) || (!SiS_Pr->HaveEMILCD)) {
+		        if((cr36 & 0x0f) == 0x02) {			/* 1024x768 */
+		           r30 |= 0x40;
+			   if(SiS_Pr->SiS_CustomT == CUT_CLEVO1024) {
+			      r30 &= ~0x40;
+			   }
+		        } else if((cr36 & 0x0f) == 0x03) {		/* 1280x1024 */
+		           r30 |= 0x40;
+			   if(SiS_Pr->SiS_CustomT != CUT_COMPAQ1280) {
+			      r30 &= ~0x40;
+			   }
+		        } else if((cr36 & 0x0f) == 0x09) {		/* 1400x1050 */
+		           r30 |= 0x40;
+		        } else if((cr36 & 0x0f) == 0x0b) {		/* 1600x1200 - unknown */
+		           r30 |= 0x40;
+		        }
+                     }
 
-        tempal &= 0x3F;
+		     if(!SiS_Pr->HaveEMI) {
+		        if((cr36 & 0x0f) == 0x02) {
+			   if((cr36 & 0xf0) == 0x30) {
+			      r31 = 0x0d; r32 = 0x70; r33 = 0x40;
+			   } else {
+			      r31 = 0x05; r32 = 0x60; r33 = 0x33;
+			   }
+		        } else if((cr36 & 0x0f) == 0x03) {
+			   if(SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) {
+			      r31 = 0x0d; r32 = 0x70; r33 = 0x6b;
+			   } else {
+			      r31 = 0x12; r32 = 0xd0; r33 = 0x6b;
+			   }
+			} else if((cr36 & 0x0f) == 0x09) {
+			   if(SiS_Pr->SiS_CustomT == CUT_COMPAL1400_2) {
+			      r31 = 0x0d; r32 = 0x70; r33 = 0x40;  /* BIOS values */
+			   } else {
+			      r31 = 0x05; r32 = 0x60; r33 = 0x00;
+			   }
+			} else {
+			   r31 = 0x05; r32 = 0x60; r33 = 0x00;
+			}
+		     }
 
-      	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;
-	      }
-	   }
-        }
+		     /* BIOS values don't work so well sometimes */
+		     if(!SiS_Pr->OverruleEMI) {
+#ifdef COMPAL_HACK
+		        if(SiS_Pr->SiS_CustomT == CUT_COMPAL1400_2) {
+		           if((cr36 & 0x0f) == 0x09) {
+			      r30 = 0x60; r31 = 0x05; r32 = 0x60; r33 = 0x00;
+			   }
+ 		        }
+#endif
+#ifdef COMPAQ_HACK
+		        if(SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) {
+		           if((cr36 & 0x0f) == 0x03) {
+			      r30 = 0x20; r31 = 0x12; r32 = 0xd0; r33 = 0x6b;     /* rev 1 */
+			   }
+			}
+#endif
+#ifdef ASUS_HACK
+		        if(SiS_Pr->SiS_CustomT == CUT_ASUSA2H_2) {
+		           if((cr36 & 0x0f) == 0x02) {
+			      /* r30 = 0x60; r31 = 0x05; r32 = 0x60; r33 = 0x33;  */   /* rev 2 */
+			      /* r30 = 0x20; r31 = 0x05; r32 = 0x60; r33 = 0x33;  */   /* rev 3 */
+			      /* r30 = 0x60; r31 = 0x0d; r32 = 0x70; r33 = 0x40;  */   /* rev 4 */
+			      /* r30 = 0x20; r31 = 0x0d; r32 = 0x70; r33 = 0x40;  */   /* rev 5 */
+			   }
+			}
+#endif
+ 		     }
+		     if(!(SiS_Pr->OverruleEMI && (!r30) && (!r31) && (!r32) && (!r33))) {
+		        SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x30,0x20);
+		     }
+		     SiS_SetReg(SiS_Pr->SiS_Part4Port,0x31,r31);
+		     SiS_SetReg(SiS_Pr->SiS_Part4Port,0x32,r32);
+		     SiS_SetReg(SiS_Pr->SiS_Part4Port,0x33,r33);
+		     if(!(SiS_Pr->OverruleEMI && (!r30) && (!r31) && (!r32) && (!r33))) {
+		        SiS_SetReg(SiS_Pr->SiS_Part4Port,0x34,0x10);
+		     } else {
+		        SiS_SetReg(SiS_Pr->SiS_Part4Port,0x34,0x00);
+		     }
+		     if( (SiS_IsVAMode(SiS_Pr,HwInfo)) ||
+	                 (SiS_CRT2IsLCD(SiS_Pr, HwInfo)) ) {
+	                if(r30 & 0x40) {
+		           SiS_PanelDelayLoop(SiS_Pr, HwInfo, 3, 5);
+			   if(delaylong) {
+			      SiS_PanelDelayLoop(SiS_Pr, HwInfo, 3, 5);
+			      delaylong = FALSE;
+			   }
+			   SiS_WaitVBRetrace(SiS_Pr,HwInfo);
+			   if(SiS_Pr->SiS_CustomT == CUT_ASUSA2H_2) {
+			      SiS_GenericDelay(SiS_Pr, 0x500);
+			   }
+	                   SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x30,0x40);
+	                }
+		     }
+		  }
+#endif
+	       }
 
-     	*CRT2Index = tempbx;
-     	*ResIndex = tempal;
+	       if(SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) {
 
-  } else {   /* LVDS, 301B-DH (if running on LCD) */
+	          if( (SiS_IsVAMode(SiS_Pr,HwInfo)) ||
+	              (SiS_CRT2IsLCD(SiS_Pr, HwInfo)) ) {
+		     SiS_DisplayOn(SiS_Pr);
+		     SiS_PanelDelay(SiS_Pr, HwInfo, 1);
+		     SiS_WaitVBRetrace(SiS_Pr, HwInfo);
+		     SiS_PanelDelay(SiS_Pr, HwInfo, 3);
+		     if(!(SiS_WeHaveBacklightCtrl(SiS_Pr,HwInfo))) {
+		        SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x26,0x01);
+	  	     }
+		  }
 
-    	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_ModeType > ModeVGA) {
-		    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;
-	 	 }
-              }
-           }
-    	}
+	       } else if(SiS_Pr->SiS_CustomT == CUT_CLEVO1400) {
 
-    	if(Flag) {
-      		
-		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) {
-		   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;
-		} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1152x768) {
-		   tempbx = 16;
-		   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)  tempbx += 2;
-		} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) {
-		   tempbx = 8;
-		   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)  tempbx++;
-		} 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_WeHaveBacklightCtrl(SiS_Pr,HwInfo))) {
+	             if( (SiS_IsVAMode(SiS_Pr, HwInfo)) ||
+	                 (SiS_CRT2IsLCD(SiS_Pr, HwInfo)) ) {
+		        SiS_DisplayOn(SiS_Pr);
+		        SiS_PanelDelay(SiS_Pr, HwInfo, 1);
+		        SiS_WaitVBRetrace(SiS_Pr,HwInfo);
+		        SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x26,0x01);
+		     }
+		  }
 
-		if(SiS_Pr->SiS_LCDInfo & LCDPass11) {
-		   tempbx = 7;
-        	}
-
-		if(SiS_Pr->SiS_CustomT == CUT_PANEL848) {
-		   tempbx = 84;
-		   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)  tempbx++;
-		}
+	       } else {
 
-	}
+	          SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2e,0x80);
+	          if(!(SiS_WeHaveBacklightCtrl(SiS_Pr,HwInfo))) {
+	             if( (SiS_IsVAMode(SiS_Pr,HwInfo)) ||
+	                 ((SiS_CRT2IsLCD(SiS_Pr, HwInfo))) ) {
+		        SiS_PanelDelayLoop(SiS_Pr, HwInfo, 3, 10);
+		        if(delaylong) {
+			   SiS_PanelDelayLoop(SiS_Pr, HwInfo, 3, 10);
+		        }
+                        SiS_WaitVBRetrace(SiS_Pr,HwInfo);
+			if(SiS_Pr->SiS_VBType & (VB_SIS302LV | VB_SIS302ELV)) {
+			   SiS_GenericDelay(SiS_Pr, 0x500);
+			}
+		        SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x26,0x01);
+	             }
+	          }
 
-#if 0
-	if(SiS_Pr->SiS_IF_DEF_FSTN){
-       	 	if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel320x480){
-         		tempbx = 14;
-         		tempal = 6;
-        	}
-    	}
-#endif	
+	          SiS_SetReg(SiS_Pr->SiS_P3c4,0x06,pushax);
+	          SiS_DisplayOn(SiS_Pr);
+	          SiS_SetRegByte(SiS_Pr->SiS_P3c6,0xff);
 
-	if(SiS_Pr->SiS_SetFlag & SetDOSMode) {
-	        if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480) tempal = 7;
-		if(HwDeviceExtension->jChipType < SIS_315H) {
-		    if(SiS_GetReg1(SiS_Pr->SiS_P3c4,0x13) & 0x80) tempal++;
-		}
+	          if(!(SiS_WeHaveBacklightCtrl(SiS_Pr,HwInfo))) {
+	             SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x00,0x7f);
+	          }
 
-	}
+	       }
 
-	if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-	    if(ModeNo > 0x13) {
-	       if((resinfo == SIS_RI_720x480) ||
-	          (resinfo == SIS_RI_720x576) ||
-		  (resinfo == SIS_RI_768x576))
-		  tempal = 6;
 	    }
-	}
 
-    	*CRT2Index = tempbx;
-    	*ResIndex = tempal & 0x1F;
-  }
-}
+	 } else {			/* 315, 330 */
 
-#ifdef SIS315H
-void
-SiS_GetCRT2PtrA(SiS_Private *SiS_Pr, UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex,
-		USHORT RefreshRateTableIndex,USHORT *CRT2Index,
-		USHORT *ResIndex)
-{
-  USHORT tempbx,tempal;
+	    if(!(SiS_IsVAMode(SiS_Pr,HwInfo))) {
+	       temp = SiS_GetReg(SiS_Pr->SiS_P3c4,0x32) & 0xDF;
+	       if(SiS_BridgeInSlave(SiS_Pr)) {
+                  tempah = SiS_GetReg(SiS_Pr->SiS_P3d4,0x30);
+                  if(!(tempah & SetCRT2ToRAMDAC))  temp |= 0x20;
+               }
+               SiS_SetReg(SiS_Pr->SiS_P3c4,0x32,temp);
 
-  tempbx = SiS_Pr->SiS_LCDResInfo;
+	       SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x1E,0x20);                   /* enable CRT2 */
 
-  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200)      tempbx = 4;
-  else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) tempbx = 3;
-  else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x960)  tempbx = 2;
-  else tempbx -= SiS_Pr->SiS_Panel1024x768;
+	       temp = SiS_GetReg(SiS_Pr->SiS_Part1Port,0x2E);
+               if(!(temp & 0x80))
+                  SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2E,0x80);
+            }
 
-  if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)  tempbx += 5;
+	    SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x00,0x1f,0x20);
 
-  if(ModeNo <= 0x13)
-      	tempal = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_CRT2CRTC;
-  else
-      	tempal = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
+	    if(SiS_Is301B(SiS_Pr)) {
 
-  *CRT2Index = tempbx;
-  *ResIndex = tempal & 0x1F;
-}
-#endif
+	       temp=SiS_GetReg(SiS_Pr->SiS_Part1Port,0x2E);
+               if(!(temp & 0x80))
+                  SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2E,0x80);
 
-void
-SiS_GetCRT2Part2Ptr(SiS_Private *SiS_Pr, UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex,
-		    USHORT RefreshRateTableIndex,USHORT *CRT2Index,
-		    USHORT *ResIndex,PSIS_HW_DEVICE_INFO HwDeviceExtension,
-		    USHORT BaseAddr)
-{
-  USHORT tempbx,tempal;
+	       tempah = 0xc0;
+	       if(SiS_IsDualEdge(SiS_Pr,HwInfo)) {
+	          tempah = 0x80;
+	          if(!(SiS_IsVAMode(SiS_Pr,HwInfo))) {
+	             tempah = 0x40;
+                  }
+	       }
+               SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x1F,tempah);
 
-  if(ModeNo <= 0x13)
-      	tempal = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_CRT2CRTC;
-  else
-      	tempal = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
+	       SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x00,0x7f);
 
-  tempbx = SiS_Pr->SiS_LCDResInfo;
+	    } else {
 
-  if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)      tempbx += 16;
-  else if(SiS_Pr->SiS_SetFlag & LCDVESATiming) tempbx += 32;
+	       SiS_VBLongWait(SiS_Pr);
+               SiS_DisplayOn(SiS_Pr);
+	       SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x00,0x7F);
+               SiS_VBLongWait(SiS_Pr);
 
-#ifdef SIS315H
-  if(SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) {
-     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
-        tempbx = 100;
-        if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)      tempbx = 101;
-  	else if(SiS_Pr->SiS_SetFlag & LCDVESATiming) tempbx = 102;
-     }
-  } else if(SiS_Pr->SiS_CustomT == CUT_CLEVO1024) {
-     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
-        if(SiS_IsDualLink(SiS_Pr, HwDeviceExtension, BaseAddr)) {
-           tempbx = 103;
-           if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)      tempbx = 104;
-  	   else if(SiS_Pr->SiS_SetFlag & LCDVESATiming) tempbx = 105;
-	}
-     }
-  }
-#endif
+	    }
 
-  *CRT2Index = tempbx;
-  *ResIndex = tempal & 0x3F;
-}
+	 }   /* 315, 330 */
 
-USHORT
-SiS_GetRatePtrCRT2(SiS_Private *SiS_Pr, UCHAR *ROMAddr, USHORT ModeNo, USHORT ModeIdIndex,
-                   PSIS_HW_DEVICE_INFO HwDeviceExtension)
-{
-  SHORT  LCDRefreshIndex[] = { 0x00, 0x00, 0x01, 0x01,
-                               0x01, 0x01, 0x01, 0x01,
-			       0x01, 0x01, 0x01, 0x01,
-			       0x01, 0x01, 0x01, 0x01,
-			       0x00, 0x00, 0x00, 0x00 };
-  USHORT RefreshRateTableIndex,i,backup_i;
-  USHORT modeflag,index,temp,backupindex;
+#endif /* SIS315H */
 
-  /* 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
-    	modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
+    } else {	/* ============  For 301 ================ */
 
-  if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
-    	if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
-      		if(modeflag & HalfDCLK) return(0);
-    	}
-  }
+       if(HwInfo->jChipType < SIS_315H) {
+          if(SiS_CRT2IsLCD(SiS_Pr, HwInfo)) {
+             SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x11,0xFB);
+	     SiS_PanelDelay(SiS_Pr, HwInfo, 0);
+	  }
+       }
 
-  if(ModeNo < 0x14) return(0xFFFF);
+       temp = SiS_GetReg(SiS_Pr->SiS_P3c4,0x32) & 0xDF;          /* lock mode */
+       if(SiS_BridgeInSlave(SiS_Pr)) {
+          tempah = SiS_GetReg(SiS_Pr->SiS_P3d4,0x30);
+          if(!(tempah & SetCRT2ToRAMDAC))  temp |= 0x20;
+       }
+       SiS_SetReg(SiS_Pr->SiS_P3c4,0x32,temp);
 
- /* 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
-  */
-
-  index = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x33);
-  index >>= SiS_Pr->SiS_SelectCRT2Rate;
-  index &= 0x0F;
-  backupindex = index;
+       SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x1E,0x20);                  /* enable CRT2 */
 
-  if(index > 0) index--;
+       if(HwInfo->jChipType >= SIS_315H) {
+          temp = SiS_GetReg(SiS_Pr->SiS_Part1Port,0x2E);
+          if(!(temp & 0x80)) {
+             SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2E,0x80);         /* BVBDOENABLE=1 */
+	  }
+       }
 
-  if(SiS_Pr->SiS_SetFlag & ProgrammingCRT2) {
-     if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
-        if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA))  index = 0;
-     } 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;
-	}
-     }
+       SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x00,0x1F,0x20);     /* enable VB processor */
 
-     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;
-      	}
-     }
-  }
+       SiS_VBLongWait(SiS_Pr);
+       SiS_DisplayOn(SiS_Pr);
+       if(HwInfo->jChipType >= SIS_315H) {
+          SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x00,0x7f);
+       }
+       SiS_VBLongWait(SiS_Pr);
 
-  RefreshRateTableIndex = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].REFindex;
-  ModeNo = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].ModeID;
+       if(HwInfo->jChipType < SIS_315H) {
+          if(SiS_CRT2IsLCD(SiS_Pr, HwInfo)) {
+	     SiS_PanelDelay(SiS_Pr, HwInfo, 1);
+             SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x11,0xF7);
+	  }
+       }
 
-  /* 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) ||
-            (SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_VESAID == 0x107) ) {
-           if(backupindex <= 1) RefreshRateTableIndex++;
-        }
-     }
-  }
+    }
 
-  i = 0;
-  do {
-    	if(SiS_Pr->SiS_RefIndex[RefreshRateTableIndex + i].ModeID != ModeNo) break;
-    	temp = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex + i].Ext_InfoFlag;
-    	temp &= ModeInfoFlag;
-    	if(temp < SiS_Pr->SiS_ModeType) break;
-    	i++;
-    	index--;
-  } while(index != 0xFFFF);
+  } else {   /* =================== For LVDS ================== */
 
-  if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToRAMDAC)) {
-    	if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
-      		temp = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex + i - 1].Ext_InfoFlag;
-      		if(temp & InterlaceMode) {
-        		i++;
-      		}
-    	}
-  }
+    if(HwInfo->jChipType < SIS_315H) {
 
-  i--;
+#ifdef SIS300    /* 300 series */
 
-  if((SiS_Pr->SiS_SetFlag & ProgrammingCRT2) && (!(SiS_Pr->SiS_VBInfo & DisableCRT2Display))) {
-    	backup_i = i;
-    	if (!(SiS_AdjustCRT2Rate(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,
-	                             RefreshRateTableIndex,&i,HwDeviceExtension))) {
-		/* This is for avoiding random data to be used; i is
-		 *     in an undefined state if no matching CRT2 mode is
-		 *     found.
-		 */
-		i = backup_i;
-	}
-  }
+       if(SiS_CRT2IsLCD(SiS_Pr, HwInfo)) {
+          if(HwInfo->jChipType == SIS_730) {
+	     SiS_PanelDelay(SiS_Pr, HwInfo, 1);
+	     SiS_PanelDelay(SiS_Pr, HwInfo, 1);
+	     SiS_PanelDelay(SiS_Pr, HwInfo, 1);
+	  }
+          SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x11,0xFB);
+	  if(!(SiS_CR36BIOSWord23d(SiS_Pr,HwInfo))) {
+	     SiS_PanelDelay(SiS_Pr, HwInfo, 0);
+	  }
+       }
+
+       SiS_EnableCRT2(SiS_Pr);
+       SiS_DisplayOn(SiS_Pr);
+       SiS_UnLockCRT2(SiS_Pr,HwInfo);
+       SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x02,0xBF);
+       if(SiS_BridgeInSlave(SiS_Pr)) {
+      	  SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x01,0x1F);
+       } else {
+      	  SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x01,0x1F,0x40);
+       }
 
-  return(RefreshRateTableIndex + i);
-}
+       if(SiS_Pr->SiS_IF_DEF_CH70xx == 1) {
+          if(!(SiS_CRT2IsLCD(SiS_Pr, HwInfo))) {
+	     SiS_WaitVBRetrace(SiS_Pr, HwInfo);
+	     SiS_SetCH700x(SiS_Pr,0x0B0E);
+          }
+       }
 
-/* Checked against all (incl 650/LVDS (1.10.07), 630/301) BIOSes */
-BOOLEAN
-SiS_AdjustCRT2Rate(SiS_Private *SiS_Pr, UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex,
-                   USHORT RefreshRateTableIndex,USHORT *i,PSIS_HW_DEVICE_INFO HwDeviceExtension)
-{
-  USHORT tempax,tempbx,resinfo;
-  USHORT modeflag,infoflag;
+       if(SiS_CRT2IsLCD(SiS_Pr, HwInfo)) {
+          if(!(SiS_GetReg(SiS_Pr->SiS_P3c4,0x13) & 0x40)) {
+             if(!(SiS_GetReg(SiS_Pr->SiS_P3c4,0x16) & 0x10)) {
+	        if(!(SiS_CR36BIOSWord23b(SiS_Pr,HwInfo))) {
+	 	   SiS_PanelDelay(SiS_Pr, HwInfo, 1);
+        	   SiS_PanelDelay(SiS_Pr, HwInfo, 1);
+	        }
+	        SiS_WaitVBRetrace(SiS_Pr, HwInfo);
+                SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x11,0xF7);
+             }
+	  }
+       }
 
-  if(ModeNo <= 0x13) {
-    	modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
-	resinfo = 0;
-  } else {
-    	modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
-        resinfo = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO;
-  }
+#endif  /* SIS300 */
 
-  tempbx = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex + (*i)].ModeID;
+    } else {
 
-  tempax = 0;
+#ifdef SIS315H    /* 315 series */
 
-  if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
+       if(SiS_Pr->SiS_IF_DEF_CH70xx == 0) {
+	  if(SiS_CRT2IsLCD(SiS_Pr, HwInfo)) {
+	     SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x11,0xFB);
+	     SiS_PanelDelay(SiS_Pr, HwInfo, 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 == SIS_RI_1600x1200) tempax |= SupportTV1024;
-		      }
-		   }
-		}
-    	} 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 == 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 == SIS_RI_640x480) && (SiS_Pr->SiS_LCDInfo & DontExpandLCD)) {
-				     return(0);
-				  } else {
-             			     if((resinfo >= SIS_RI_1280x1024) && (resinfo != SIS_RI_1280x768)) {
-               				return(0);
-             			     }
-           			  }
-        		       }
-		            }
-		         }
-		      }
-      		   }
-		} else {
-		  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x600) {
-		     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 != SIS_RI_1152x768) && (resinfo > SIS_RI_1024x768)) return(0);
-		  } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x960) {
-		     if((resinfo != SIS_RI_1280x960) && (resinfo > SIS_RI_1024x768)) return(0);
-		  } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
-		     if(resinfo > SIS_RI_1280x1024) return(0);
-		  } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
-		     if(resinfo > SIS_RI_1024x768) return(0);
-		  } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600) {
-		     if((resinfo == SIS_RI_512x384) || (resinfo > SIS_RI_800x600)) return(0);
-		  } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480) {
-		     if((resinfo == SIS_RI_512x384) ||
-		        (resinfo == SIS_RI_400x300) ||
-			(resinfo > SIS_RI_640x480)) return(0);
-		  }
-		}
-    	} else if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) {
-	        if(SiS_Pr->SiS_HiVision == 3) {
-		      	tempax |= SupportHiVisionTV2;
-      			if(SiS_Pr->SiS_VBInfo & SetInSlaveMode){
-        			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 > SIS_RI_800x600) return(0);
-			}
-		} else {  
-      			tempax |= SupportHiVisionTV;
-      			if(SiS_Pr->SiS_VBInfo & SetInSlaveMode){
-        			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 > SIS_RI_800x600) return(0);
-			}
-		}
-    	} 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 != 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 {
-			 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 {  /* 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);
-		   }
-	        }
-        }
+       SiS_EnableCRT2(SiS_Pr);
+       SiS_UnLockCRT2(SiS_Pr,HwInfo);
 
-  } else {	/* for LVDS  */
+       SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2e,0xf7);
 
-    	if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
-      		if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
-        		tempax |= SupportCHTV;
-      		}
-    	}
-    	if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
-      		tempax |= SupportLCD;
-		if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768) {
-		     if((resinfo != SIS_RI_1280x768) && (resinfo >= SIS_RI_1280x1024)) return(0);
-		} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x600) {
-		     if((resinfo != SIS_RI_1024x600) && (resinfo >= SIS_RI_1024x768))  return(0);
-		} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1152x768) {
-		     if((resinfo != SIS_RI_1152x768) && (resinfo > SIS_RI_1024x768))   return(0);
-		} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) {
-		     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 > SIS_RI_1280x1024) return(0);
-                } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
-		     if(resinfo > SIS_RI_1024x768)  return(0);
-		} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600){
-		     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);
-		}  else if(SiS_Pr->SiS_LCDResInfo == Panel_848x480) {
-                     if((resinfo != SIS_RI_1360x768) &&
-		        (resinfo != SIS_RI_848x480)  &&
-		        (resinfo > SIS_RI_1024x768)) return(0);
-		}
-    	}
-  }
-  
-  /* 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) {
-       		return(1);
-     	}
-     	if ((*i) == 0) break;
-  }
-  /* 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)++) {
-     	infoflag = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex + (*i)].Ext_InfoFlag;
-     	if(SiS_Pr->SiS_RefIndex[RefreshRateTableIndex + (*i)].ModeID != tempbx) {
-       		return(0);
-     	}
-     	if(infoflag & tempax) {
-       		return(1);
-     	}
-  }
-  return(1);
-}
+       if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
+          temp = SiS_GetCH701x(SiS_Pr,0x66);
+	  temp &= 0x20;
+	  SiS_Chrontel701xBLOff(SiS_Pr);
+       }
 
-void
-SiS_SaveCRT2Info(SiS_Private *SiS_Pr, USHORT ModeNo)
-{
-  USHORT temp1,temp2;
+       if(HwInfo->jChipType != SIS_550) {
+          SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2e,0x7f);
+       }
 
-  /* We store CRT1 ModeNo in CR34 */
-  SiS_SetReg1(SiS_Pr->SiS_P3d4,0x34,ModeNo);
-  temp1 = (SiS_Pr->SiS_VBInfo & SetInSlaveMode) >> 8;
-  temp2 = ~(SetInSlaveMode >> 8);
-  SiS_SetRegANDOR(SiS_Pr->SiS_P3d4,0x31,temp2,temp1);
-}
+       if(HwInfo->jChipType == SIS_740) {
+          if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
+             if(SiS_IsLCDOrLCDA(SiS_Pr, HwInfo)) {
+	   	SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x1E,0x20);
+	     }
+	  }
+       }
 
-void
-SiS_GetVBInfo(SiS_Private *SiS_Pr, USHORT BaseAddr,UCHAR *ROMAddr,USHORT ModeNo,
-              USHORT ModeIdIndex,PSIS_HW_DEVICE_INFO HwDeviceExtension,
-	      int checkcrt2mode)
-{
-  USHORT tempax,tempbx,temp;
-  USHORT modeflag, resinfo=0;
-  UCHAR  OutputSelect = *SiS_Pr->pSiS_OutputSelect;
+       temp1 = SiS_GetReg(SiS_Pr->SiS_Part1Port,0x2E);
+       if(!(temp1 & 0x80)) {
+          SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2E,0x80);
+       }
 
-  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;
-     }
-  }
+       if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
+          if(temp) {
+	     SiS_Chrontel701xBLOn(SiS_Pr, HwInfo);
+	  }
+       }
 
-  SiS_Pr->SiS_SetFlag = 0;
+       if(SiS_Pr->SiS_IF_DEF_CH70xx == 0) {
+          if(SiS_CRT2IsLCD(SiS_Pr, HwInfo)) {
+	     SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x1E,0x20);
+	     if(HwInfo->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,HwInfo)) {
+          if(HwInfo->jChipType != SIS_740) {
+             SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x1E,0x20);
+	  }
+       }
 
-  SiS_Pr->SiS_ModeType = modeflag & ModeInfoFlag;
+       if(!(SiS_WeHaveBacklightCtrl(SiS_Pr,HwInfo))) {
+          SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x00,0x7f);
+       }
 
-  tempbx = 0;
-  if(SiS_BridgeIsOn(SiS_Pr,BaseAddr,HwDeviceExtension) == 0) {  
-    	temp = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x30);
-#if 0	
-	/* SiS_HiVision is only used on 315/330+30xLV */
-	if(SiS_Pr->SiS_VBType & (VB_SIS301LV302LV)) {
-	   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) {	/* New from 650/30xLV 1.10.6s */
-	      temp &= (SetCRT2ToHiVisionTV | SwitchToCRT2 | SetSimuScanMode); 	/* 0x83 */
-	      temp |= SetCRT2ToSVIDEO;  					/* 0x08 */
-	   }
-	}
-#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);
-    	tempbx |= tempax;
-    	tempbx &= ~(SetCHTVOverScan | SetInSlaveMode | DisableCRT2Display);;
+       if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
+       	  if(SiS_IsTVOrYPbPrOrScart(SiS_Pr,HwInfo)) {
+             SiS_Chrontel701xOn(SiS_Pr,HwInfo);
+          }
+          if( (SiS_IsVAMode(SiS_Pr,HwInfo)) ||
+	      (SiS_IsLCDOrLCDA(SiS_Pr,HwInfo)) ) {
+             SiS_ChrontelDoSomething1(SiS_Pr,HwInfo);
+          }
+       }
 
-#ifdef SIS315H
+       if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
+       	  if(!(SiS_WeHaveBacklightCtrl(SiS_Pr,HwInfo))) {
+ 	     if( (SiS_IsVAMode(SiS_Pr,HwInfo)) ||
+	         (SiS_IsLCDOrLCDA(SiS_Pr,HwInfo)) ) {
+	     	SiS_Chrontel701xBLOn(SiS_Pr, HwInfo);
+	     	SiS_ChrontelInitTVVSync(SiS_Pr,HwInfo);
+             }
+       	  }
+       } else if(SiS_Pr->SiS_IF_DEF_CH70xx == 0) {
+       	  if(!(SiS_WeHaveBacklightCtrl(SiS_Pr,HwInfo))) {
+	     if(SiS_CRT2IsLCD(SiS_Pr, HwInfo)) {
+		SiS_PanelDelay(SiS_Pr, HwInfo, 1);
+		SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x11,0xF7);
+	     }
+	  }
+       }
 
-	if(HwDeviceExtension->jChipType >= SIS_315H) {
-    	   if(SiS_Pr->SiS_VBType & (VB_SIS302B | VB_SIS301LV | VB_SIS302LV)) {
-	      /* From 1.10.7w, not in 1.10.8r */
-	      if(ModeNo == 0x03) {   
-	         /* Mode 0x03 is never in driver mode */
-		 SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x31,0xbf);
-	      }
-	      /* From 1.10.7w, not in 1.10.8r */
-	      if(!(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x31) & (DriverMode >> 8))) {
-	         /* Reset LCDA setting */
-		 SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x38,0xfc);
-	      }
-	      if(IS_SIS650) {
-	         if(SiS_Pr->SiS_UseLCDA) {
-		    if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x5f) & 0xF0) {
-		       if((ModeNo <= 0x13) || (!(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x31) & (DriverMode >> 8)))) {
-		          SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x38,(EnableDualEdge | SetToLCDA));  /* 3 */
-		       }
-		    }
-		 }
-#if 0		 /* We can't detect it this way; there are machines which do not use LCDA despite
-                  * the chip revision
-		  */      	      
-		 if((tempbx & SetCRT2ToLCD) && (SiS_GetReg1(SiS_Pr->SiS_P3d4,0x30) & SetCRT2ToLCD)) {
-                    if((SiS_GetReg1(SiS_Pr->SiS_P3d4, 0x36) & 0x0f) == SiS_Pr->SiS_Panel1400x1050) {
-		       if((ModeNo <= 0x13) || (!(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x31) & (DriverMode >> 8)))) {
-		          if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x5f) & 0xF0) {
-			     SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x38,(EnableDualEdge | SetToLCDA));  /* 3 */
-			  }
-		       }
-		    } else {
-		       if((ModeNo <= 0x13) || (!(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x31) & (DriverMode >> 8)))) {
-			  if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x5f) & 0xF0) {
-			     SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x38,(EnableDualEdge | SetToLCDA));  /* 3 */
-		          }
-		       }
-		    }
-		 }
-#endif		 
-	      }
-	      temp = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x38);
-       	      if((temp & (EnableDualEdge | SetToLCDA)) == (EnableDualEdge | SetToLCDA)) {
-          		tempbx |= SetCRT2ToLCDA;
-	      }
-    	   }
+#endif  /* SIS315H */
 
-	   if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
-	        temp = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x38);
-	        if(temp & SetToLCDA)
-		   tempbx |= SetCRT2ToLCDA;
-		if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
-	           if(temp & EnableLVDSHiVision)
-		      tempbx |= SetCRT2ToHiVisionTV;
-		}
-	   }
-	}
+    } /* 310 series */
 
-#endif  /* SIS315H */
+  }  /* LVDS */
 
-    	if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
-	        temp = SetCRT2ToLCDA   | SetCRT2ToSCART      | SetCRT2ToLCD    |
-		       SetCRT2ToRAMDAC | SetCRT2ToSVIDEO     | SetCRT2ToAVIDEO |  /* = 0x807C; */
-                       SetCRT2ToHiVisionTV; 					  /* = 0x80FC; */
-    	} else {
-                if(HwDeviceExtension->jChipType >= SIS_315H) {
-                    if(SiS_Pr->SiS_IF_DEF_CH70xx != 0)
-        		temp = SetCRT2ToLCDA   | SetCRT2ToSCART |
-			       SetCRT2ToLCD    | SetCRT2ToHiVisionTV |
-			       SetCRT2ToAVIDEO | SetCRT2ToSVIDEO;  /* = 0x80bc */
-      		    else
-        		temp = SetCRT2ToLCDA | SetCRT2ToLCD;
-		} else {
-      		    if(SiS_Pr->SiS_IF_DEF_CH70xx != 0)
-        		temp = SetCRT2ToTV | SetCRT2ToLCD;
-      		    else
-        		temp = SetCRT2ToLCD;
-		}
-    	}
+}
 
-    	if(!(tempbx & temp)) {
-      		tempax = DisableCRT2Display;
-      		tempbx = 0;
-    	}
+/*********************************************/
+/*         SET PART 1 REGISTER GROUP         */
+/*********************************************/
 
-   	if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
-      		if(tempbx & SetCRT2ToLCDA) {
-        		tempbx &= (0xFF00|SwitchToCRT2|SetSimuScanMode);
-      		}
-		if(tempbx & SetCRT2ToRAMDAC) {
-        		tempbx &= (0xFF00|SetCRT2ToRAMDAC|SwitchToCRT2|SetSimuScanMode);
-      		}
-		if((tempbx & SetCRT2ToLCD) /* && (!(SiS_Pr->SiS_VBType & VB_NoLCD)) */ ) {
-		        /* We initialize the Panel Link of the type of bridge is DH */
-        		tempbx &= (0xFF00|SetCRT2ToLCD|SwitchToCRT2|SetSimuScanMode);
-      		}
-		if(tempbx & SetCRT2ToSCART) {
-        		tempbx &= (0xFF00|SetCRT2ToSCART|SwitchToCRT2|SetSimuScanMode);
-        		tempbx |= SetPALTV;
-      		}
-		if(tempbx & SetCRT2ToHiVisionTV) {
-        		tempbx &= (0xFF00|SetCRT2ToHiVisionTV|SwitchToCRT2|SetSimuScanMode);
-        		tempbx |= SetPALTV;
-      		}
-   	} else { /* LVDS */
-	        if(HwDeviceExtension->jChipType >= SIS_315H) {
-		    if(tempbx & SetCRT2ToLCDA)
-		        tempbx &= (0xFF00|SwitchToCRT2|SetSimuScanMode);
-		}
-      		if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
-        	    if(tempbx & SetCRT2ToTV)
-          		 tempbx &= (0xFF00|SetCRT2ToTV|SwitchToCRT2|SetSimuScanMode);
-      		}
-      		if(tempbx & SetCRT2ToLCD) {
-        		tempbx &= (0xFF00|SetCRT2ToLCD|SwitchToCRT2|SetSimuScanMode);
-		}
-	        if(HwDeviceExtension->jChipType >= SIS_315H) {
-		    if(tempbx & SetCRT2ToLCDA)
-		        tempbx |= SetCRT2ToLCD;
-		}
-	}
+/********** Set CRT2 OFFSET / PITCH **********/
+static void
+SiS_SetCRT2Offset(SiS_Private *SiS_Pr,USHORT ModeNo,
+                  USHORT ModeIdIndex ,USHORT RefreshRateTableIndex,
+	          PSIS_HW_INFO HwInfo)
+{
+  USHORT offset;
+  UCHAR temp;
 
-    	if(tempax & DisableCRT2Display) {
-      		if(!(tempbx & (SwitchToCRT2 | SetSimuScanMode))) {
-        		tempbx = SetSimuScanMode | DisableCRT2Display;
-      		}
-    	}
+  if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) return;
 
-    	if(!(tempbx & DriverMode)){
-      		tempbx |= SetSimuScanMode;
-    	}
+  offset = SiS_GetOffset(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex,
+                         HwInfo);
 
-	/* 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)) ) {
-	       modeflag &= (~CRT2Mode);
-	   }
-	}
-	
-    	if(!(tempbx & SetSimuScanMode)) {
-      	    if(tempbx & SwitchToCRT2) {
-        	if((!(modeflag & CRT2Mode)) && (checkcrt2mode)) {
-		     if( (HwDeviceExtension->jChipType >= SIS_315H) &&
-			 (SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) ) {
-			 if(resinfo != SIS_RI_1600x1200)
-                              tempbx |= SetSimuScanMode;
-		     } else {
-            		      tempbx |= SetSimuScanMode;
-	             }
-        	}
-      	    } else {
-        	if(!(SiS_BridgeIsEnable(SiS_Pr,BaseAddr,HwDeviceExtension))) {
-          	     if(!(tempbx & DriverMode)) {
-            		  if(SiS_BridgeInSlave(SiS_Pr)) {
-			      tempbx |= SetSimuScanMode;
-            		  }
-                     }
-                }
-      	    }
-    	}
+  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_2 ||
+     SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480_3) offset >>= 1;
 
-    	if(!(tempbx & DisableCRT2Display)) {
-            if(tempbx & DriverMode) {
-                if(tempbx & SetSimuScanMode) {
-          	    if((!(modeflag & CRT2Mode)) && (checkcrt2mode)) {
-	                if( (HwDeviceExtension->jChipType >= SIS_315H) &&
-			    (SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) ) {
-			     if(resinfo != SIS_RI_1600x1200) {  /* 650/301 BIOS */
-			          tempbx |= SetInSlaveMode;
-            		          if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
-              		 	     if(tempbx & SetCRT2ToTV) {
-                		         if(!(tempbx & SetNotSimuMode))
-					     SiS_Pr->SiS_SetFlag |= TVSimuMode;
-              			     }
-                                  }
-			     }                      /* 650/301 BIOS */
-		        } else {
-            		    tempbx |= SetInSlaveMode;
-            		    if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
-              		        if(tempbx & SetCRT2ToTV) {
-                		    if(!(tempbx & SetNotSimuMode))
-					SiS_Pr->SiS_SetFlag |= TVSimuMode;
-              			}
-            		    }
-                        }
-	            }
-                }
-            } else {
-                tempbx |= SetInSlaveMode;
-        	if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
-          	    if(tempbx & SetCRT2ToTV) {
-            		if(!(tempbx & SetNotSimuMode))
-			    SiS_Pr->SiS_SetFlag |= TVSimuMode;
-          	    }
-        	}
-      	    }
-    	}
-	
-	if(SiS_Pr->SiS_CHOverScan) {
-    	   if(SiS_Pr->SiS_IF_DEF_CH70xx == 1) {
-      		temp = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x35);
-      		if((temp & TVOverScan) || (SiS_Pr->SiS_CHOverScan == 1) )
-		      tempbx |= SetCHTVOverScan;
-    	   }
-	   if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
-      		temp = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x79);
-      		if( (temp & 0x80) || (SiS_Pr->SiS_CHOverScan == 1) )
-		      tempbx |= SetCHTVOverScan;
-    	   }
-	   if(SiS_Pr->SiS_CHSOverScan) {
-	      tempbx |= SetCHTVOverScan;
-	   }
-	}
-  }
+  temp = (UCHAR)(offset & 0xFF);
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x07,temp);
+  temp = (UCHAR)(offset >> 8);
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x09,temp);
+  temp = (UCHAR)(((offset >> 3) & 0xFF) + 1);
+  if(offset % 8) temp++;
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x03,temp);
+}
 
-  if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
-#ifdef SIS300
-     	if((HwDeviceExtension->jChipType==SIS_630) ||
-           (HwDeviceExtension->jChipType==SIS_730)) {
-	   	if(ROMAddr && SiS_Pr->SiS_UseROM) {
-			OutputSelect = ROMAddr[0xfe];
-                }
-           	if(!(OutputSelect & EnablePALMN))
-             		SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x35,0x3F);
-           	if(tempbx & SetCRT2ToTV) {
-              		if(tempbx & SetPALTV) {
-                  		temp = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x35);
-                  		if(temp & EnablePALM) tempbx &= (~SetPALTV);
-             		}
-          	}
-      	}
-#endif
-#ifdef SIS315H
-     	if(HwDeviceExtension->jChipType >= SIS_315H) {
-	        if(ROMAddr && SiS_Pr->SiS_UseROM) {
-		    OutputSelect = ROMAddr[0xf3];
-		    if(HwDeviceExtension->jChipType >= SIS_330) {
-			OutputSelect = ROMAddr[0x11b];
-		    }
-                }
-		if(!(OutputSelect & EnablePALMN))
-        		SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x38,0x3F);
-   		if(tempbx & SetCRT2ToTV) {
-    			if(tempbx & SetPALTV) {
-               			temp = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x38);
-               			if(temp & EnablePALM) tempbx &= (~SetPALTV);
-              		}
-        	}
-  	}
-#endif
-  }
-  
-  /* PALM/PALN on Chrontel 7019 */
-  SiS_Pr->SiS_CHPALM = SiS_Pr->SiS_CHPALN = FALSE;
-  if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
-  	if(tempbx & SetCRT2ToTV) {
-    		if(tempbx & SetPALTV) {
-        		temp = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x38);
-        		if(temp & EnablePALM)      SiS_Pr->SiS_CHPALM = TRUE;
-			else if(temp & EnablePALN) SiS_Pr->SiS_CHPALN = TRUE;
-        	}
-        }
-  }
+/************* Set CRT2 Sync *************/
+static void
+SiS_SetCRT2Sync(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT RefreshRateTableIndex,
+                PSIS_HW_INFO HwInfo)
+{
+  USHORT tempah=0,tempbl,infoflag;
 
-  SiS_Pr->SiS_VBInfo = tempbx;
+  tempbl = 0xC0;
 
-  if(HwDeviceExtension->jChipType == SIS_630) {
-       SiS_SetChrontelGPIO(SiS_Pr, SiS_Pr->SiS_VBInfo);
+  if(SiS_Pr->UseCustomMode) {
+     infoflag = SiS_Pr->CInfoFlag;
+  } else {
+     infoflag = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
   }
 
-#ifdef TWDEBUG
-#ifdef LINUX_KERNEL
-  printk(KERN_DEBUG "sisfb: (VBInfo= 0x%04x, SetFlag=0x%04x)\n", 
-      SiS_Pr->SiS_VBInfo, SiS_Pr->SiS_SetFlag);
-#endif
-#ifdef LINUX_XF86
-  xf86DrvMsgVerb(0, X_PROBED, 3, "(init301: VBInfo=0x%04x, SetFlag=0x%04x)\n", 
-      SiS_Pr->SiS_VBInfo, SiS_Pr->SiS_SetFlag);
-#endif
-#endif
+  if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {					/* LVDS */
 
-}
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+        tempah = 0;
+     } else if((SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) && (SiS_Pr->SiS_LCDInfo & LCDSync)) {
+        tempah = SiS_Pr->SiS_LCDInfo;
+     } else tempah = infoflag >> 8;
 
-/* Setup general purpose IO for Chrontel communication */
-void
-SiS_SetChrontelGPIO(SiS_Private *SiS_Pr, USHORT myvbinfo)
-{
-   unsigned long  acpibase;
-   unsigned short temp;
+     tempah &= 0xC0;
 
-   if(!(SiS_Pr->SiS_ChSW)) return;
+     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;
+        }
+     }
 
-#ifndef LINUX_XF86
-   SiS_SetReg4(0xcf8,0x80000874);		   /* get ACPI base */
-   acpibase = SiS_GetReg3(0xcfc);
-#else
-   acpibase = pciReadLong(0x00000800, 0x74);
-#endif
-   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)) {
-      temp |= 0x0100;
-   }
-   SiS_SetReg5((USHORT)(acpibase + 0x3a), temp);
-   temp = SiS_GetReg4((USHORT)(acpibase + 0x3a));
-}
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+        if(HwInfo->jChipType >= SIS_315H) {
+           tempah >>= 3;
+           SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x13,0xE7,tempah);
+        } else {
+           SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x19,0x0F,0xe0);
+        }
+     } else {
+        SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x19,0x0F,tempah);
+     }
 
-void
-SiS_GetRAMDAC2DATA(SiS_Private *SiS_Pr, UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex,
-                   USHORT RefreshRateTableIndex,PSIS_HW_DEVICE_INFO HwDeviceExtension)
-{
-  USHORT tempax=0,tempbx=0;
-  USHORT temp1=0,modeflag=0,tempcx=0;
-  USHORT StandTableIndex,CRT1Index;
-#ifdef SIS315H   
-  USHORT ResIndex,DisplayType,temp=0;
-  const  SiS_LVDSCRT1DataStruct *LVDSCRT1Ptr = NULL;
-#endif
+  } else if(SiS_Pr->SiS_VBType & VB_SISVB) {
 
-  SiS_Pr->SiS_RVBHCMAX  = 1;
-  SiS_Pr->SiS_RVBHCFACT = 1;
+     if(HwInfo->jChipType < SIS_315H) {
 
-  if(ModeNo <= 0x13) {
+#ifdef SIS300  /* ---- 300 series --- */
 
-    	modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
-    	StandTableIndex = SiS_GetModePtr(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex);
-    	tempax = SiS_Pr->SiS_StandTable[StandTableIndex].CRTC[0];
-    	tempbx = SiS_Pr->SiS_StandTable[StandTableIndex].CRTC[6];
-	temp1 = SiS_Pr->SiS_StandTable[StandTableIndex].CRTC[7];
+        if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {			/* 630 - 301B(-DH) */
 
-  } else {
+	   tempah = infoflag >> 8;
+           if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
+	      if(SiS_Pr->SiS_LCDInfo & LCDSync) {
+	         tempah = SiS_Pr->SiS_LCDInfo;
+              }
+           }
+           tempah &= 0xC0;
 
-     if( (SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) && (SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) ) {
-
-#ifdef SIS315H     
-    	temp = SiS_GetLVDSCRT1Ptr(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,
-			RefreshRateTableIndex,&ResIndex,&DisplayType);
-
-    	if(temp == 0)  return;
-
-    	switch(DisplayType) {
-    		case 0 : LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT1800x600_1;		break;
-    		case 1 : LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11024x768_1;          break;
-    		case 2 : LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11280x1024_1;         break;
-    		case 3 : LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT1800x600_1_H;         break;
-    		case 4 : LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11024x768_1_H;        break;
-    		case 5 : LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11280x1024_1_H;       break;
-    		case 6 : LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT1800x600_2;           break;
-    		case 7 : LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11024x768_2;          break;
-    		case 8 : LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11280x1024_2;         break;
-    		case 9 : LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT1800x600_2_H;         break;
-    		case 10: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11024x768_2_H;        break;
-    		case 11: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11280x1024_2_H;       break;
-		case 12: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT1XXXxXXX_1;           break;
-		case 13: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT1XXXxXXX_1_H;         break;
-		case 14: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11400x1050_1;         break;
-		case 15: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11400x1050_1_H;       break;
-		case 16: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11400x1050_2;         break;
-		case 17: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11400x1050_2_H;       break;
-    		case 18: LVDSCRT1Ptr = SiS_Pr->SiS_CHTVCRT1UNTSC;               break;
-    		case 19: LVDSCRT1Ptr = SiS_Pr->SiS_CHTVCRT1ONTSC;               break;
-    		case 20: LVDSCRT1Ptr = SiS_Pr->SiS_CHTVCRT1UPAL;                break;
-    		case 21: LVDSCRT1Ptr = SiS_Pr->SiS_CHTVCRT1OPAL;                break;
-    		case 22: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT1320x480_1;           break;
-		case 23: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11024x600_1;          break;
-    		case 24: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11024x600_1_H;        break;
-    		case 25: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11024x600_2;          break;
-    		case 26: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11024x600_2_H;        break;
-    		case 27: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11152x768_1;          break;
-    		case 28: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11152x768_1_H;        break;
-    		case 29: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11152x768_2;          break;
-    		case 30: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11152x768_2_H;        break;
-		case 36: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11600x1200_1;         break;
-		case 37: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11600x1200_1_H;       break;
-		case 38: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11600x1200_2;         break;
-		case 39: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11600x1200_2_H;       break;
-		case 99: LVDSCRT1Ptr = SiS_Pr->SiS_CHTVCRT1OPAL;                break;
-		default: LVDSCRT1Ptr = SiS_Pr->SiS_LVDSCRT11024x768_1;          break;
-    	}
-	tempax = (LVDSCRT1Ptr+ResIndex)->CR[0];
-	tempax |= (LVDSCRT1Ptr+ResIndex)->CR[14] << 8;
-	tempax &= 0x03FF;
-    	tempbx = (LVDSCRT1Ptr+ResIndex)->CR[6];
-    	tempcx = (LVDSCRT1Ptr+ResIndex)->CR[13] << 8;
-    	tempcx &= 0x0100;
-    	tempcx <<= 2;
-    	tempbx |= tempcx;
-	temp1  = (LVDSCRT1Ptr+ResIndex)->CR[7];
-#endif
+           tempah |= 0x20;
+           if(!(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit)) tempah |= 0x10;
 
-    } else {
+ 	   tempah &= 0x3f;
+  	   tempah |= tempbl;
+           SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x19,0x0F,tempah);
+
+        } else {							/* 630 - 301 */
+
+           tempah = infoflag >> 8;
+           tempah &= 0xC0;
+           tempah |= 0x20;
+	   if(!(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit)) tempah |= 0x10;
+           SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x19,0x0F,tempah);
 
-    	modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
-    	CRT1Index = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
-#if 0   /* Not any longer */	
-	if(HwDeviceExtension->jChipType < SIS_315H)  CRT1Index &= 0x3F;
-#endif	
-	tempax = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[0];
-	tempax |= SiS_Pr->SiS_CRT1Table[CRT1Index].CR[14] << 8;
-        tempax &= 0x03FF;
-    	tempbx = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[6];
-    	tempcx = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[13] << 8;
-    	tempcx &= 0x0100;
-    	tempcx <<= 2;
-    	tempbx |= tempcx;
-	temp1  = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[7];
+        }
 
-    }
+#endif /* SIS300 */
 
-  }
+     } else {
 
-  if(temp1 & 0x01) tempbx |= 0x0100;
-  if(temp1 & 0x20) tempbx |= 0x0200;
-  
-  tempax += 5;
+#ifdef SIS315H  /* ------- 315 series ------ */
 
-  /* Charx8Dot is no more used (and assumed), so we set it */
-  if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-      modeflag |= Charx8Dot;
-  }
+        if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {	  		/* 315 - 30xLV */
 
-  if(modeflag & Charx8Dot) tempax *= 8;
-  else                     tempax *= 9;
+	   if(((SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) &&
+	       (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024)) ||
+	      ((SiS_Pr->SiS_CustomT == CUT_CLEVO1400)  &&
+	       (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050))) {
+	      tempah = infoflag >> 8;
+	   } else {
+              tempah = SiS_GetReg(SiS_Pr->SiS_P3d4,0x37);
+	   }
+	   tempah &= 0xC0;
 
-  /* From 650/30xLV 1.10.6s */
-  if(modeflag & HalfDCLK)  tempax <<= 1;
+           tempah |= 0x20;
+           if(!(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit)) tempah |= 0x10;
+           SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x19,0x0F,tempah);
 
-  tempbx++;
+        } else {							/* 315 - 301, 301B */
 
-  SiS_Pr->SiS_VGAHT = SiS_Pr->SiS_HT = tempax;
-  SiS_Pr->SiS_VGAVT = SiS_Pr->SiS_VT = tempbx;
-}
+           tempah = infoflag >> 8;
+	   if(!SiS_Pr->UseCustomMode) {
+	      if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
+	         if(SiS_Pr->SiS_LCDInfo & LCDSync) {
+	            tempah = SiS_Pr->SiS_LCDInfo;
+	         }
+	      }
+	   }
+	   tempah &= 0xC0;
 
-void
-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);
-  else
-     SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x24,0x01);
-}
+           tempah |= 0x20;
+           if(!(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit)) tempah |= 0x10;
 
-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);
-  else
-     SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x24,0xFE);
-}
+	   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);
 
-void
-SiS_EnableCRT2(SiS_Private *SiS_Pr)
-{
-  SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x1E,0x20);
+        }
+#endif  /* SIS315H */
+      }
+   }
 }
 
-void
-SiS_DisableBridge(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,USHORT BaseAddr)
+/******** Set CRT2 FIFO on 300/630/730 *******/
+#ifdef SIS300
+static void
+SiS_SetCRT2FIFO_300(SiS_Private *SiS_Pr,USHORT ModeNo,
+                    PSIS_HW_INFO HwInfo)
 {
-#ifdef SIS315H
-  USHORT tempah,pushax=0,modenum;
-#endif
-  USHORT temp=0;
-  UCHAR *ROMAddr = HwDeviceExtension->pjVirtualRomBase;
+  UCHAR  *ROMAddr  = HwInfo->pjVirtualRomBase;
+  USHORT temp,index;
+  USHORT modeidindex,refreshratetableindex;
+  USHORT VCLK=0,MCLK,colorth=0,data2=0;
+  USHORT tempal, tempah, tempbx, tempcl, tempax;
+  USHORT CRT1ModeNo,CRT2ModeNo;
+  USHORT SelectRate_backup;
+  ULONG  data,eax;
+  const UCHAR  LatencyFactor[] = {
+  	97, 88, 86, 79, 77, 00,       /*; 64  bit    BQ=2   */
+        00, 87, 85, 78, 76, 54,       /*; 64  bit    BQ=1   */
+        97, 88, 86, 79, 77, 00,       /*; 128 bit    BQ=2   */
+        00, 79, 77, 70, 68, 48,       /*; 128 bit    BQ=1   */
+        80, 72, 69, 63, 61, 00,       /*; 64  bit    BQ=2   */
+        00, 70, 68, 61, 59, 37,       /*; 64  bit    BQ=1   */
+        86, 77, 75, 68, 66, 00,       /*; 128 bit    BQ=2   */
+        00, 68, 66, 59, 57, 37        /*; 128 bit    BQ=1   */
+  };
+  const UCHAR  LatencyFactor730[] = {
+         69, 63, 61,
+	 86, 79, 77,
+	103, 96, 94,
+	120,113,111,
+	137,130,128,    /* <-- last entry, data below */
+	137,130,128,	/* to avoid using illegal values */
+	137,130,128,
+	137,130,128,
+	137,130,128,
+	137,130,128,
+	137,130,128,
+	137,130,128,
+	137,130,128,
+	137,130,128,
+	137,130,128,
+	137,130,128,
+  };
+  const UCHAR ThLowB[]   = {
+  	81, 4, 72, 6, 88, 8,120,12,
+        55, 4, 54, 6, 66, 8, 90,12,
+        42, 4, 45, 6, 55, 8, 75,12
+  };
+  const UCHAR ThTiming[] = {
+  	1, 2, 2, 3, 0, 1, 1, 2
+  };
 
-  if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
+  SelectRate_backup = SiS_Pr->SiS_SelectCRT2Rate;
 
-      if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {   /* ===== For 30xB/LV ===== */
+  if(!SiS_Pr->CRT1UsesCustomMode) {
 
-        if(HwDeviceExtension->jChipType < SIS_315H) {
+     CRT1ModeNo = SiS_Pr->SiS_CRT1Mode;                                 	/* get CRT1 ModeNo */
+     SiS_SearchModeID(SiS_Pr, &CRT1ModeNo, &modeidindex);
+     SiS_Pr->SiS_SetFlag &= (~ProgrammingCRT2);
+     SiS_Pr->SiS_SelectCRT2Rate = 0;
+     refreshratetableindex = SiS_GetRatePtr(SiS_Pr, CRT1ModeNo, modeidindex, HwInfo);
 
-#ifdef SIS300	   /* 300 series */
+     if(CRT1ModeNo >= 0x13) {
+        index = SiS_Pr->SiS_RefIndex[refreshratetableindex].Ext_CRTVCLK;
+        index &= 0x3F;
+        VCLK = SiS_Pr->SiS_VCLKData[index].CLOCK;				/* Get VCLK */
 
-           if(HwDeviceExtension->jChipType == SIS_300) {  /* New for 300+301LV */
+	colorth = SiS_GetColorDepth(SiS_Pr,CRT1ModeNo,modeidindex); 	/* Get colordepth */
+        colorth >>= 1;
+        if(!colorth) colorth++;
+     }
 
-	      if(!(SiS_CR36BIOSWord23b(SiS_Pr,HwDeviceExtension))) {
-	         if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
-	            SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x26,0xFE,0x00);
-		    SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 3);
-		 }
-	      }
-	      if(SiS_Is301B(SiS_Pr,BaseAddr)) {
-	         SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x1f,0x3f);
-	         SiS_ShortDelay(SiS_Pr,1);
-	      }
-	      SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x00,0xDF);
-	      SiS_DisplayOff(SiS_Pr);
-	      SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x32,0xDF);
-	      SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x1E,0xDF);
-	      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 {
 
-	   } else {
+     CRT1ModeNo = 0xfe;
+     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(!(SiS_CR36BIOSWord23b(SiS_Pr,HwDeviceExtension))) {
-	         SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x11,0xF7,0x08);
-	         SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 3);
-	      }
-	      if(SiS_Is301B(SiS_Pr,BaseAddr)) {
-	         SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x1f,0x3f);
-	         SiS_ShortDelay(SiS_Pr,1);
-	      }
-	      SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x00,0xDF);
-	      SiS_DisplayOff(SiS_Pr);
-	      SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x32,0xDF);
-	      SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x1E,0xDF);
-	      SiS_UnLockCRT2(SiS_Pr,HwDeviceExtension,BaseAddr);
-	      SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x01,0x80);
-	      SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x02,0x40);
-	      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);
-	      }
-	   }
+  }
 
-#endif  /* SIS300 */
+  if(CRT1ModeNo >= 0x13) {
+    if(HwInfo->jChipType == SIS_300) {
+       index = SiS_GetReg(SiS_Pr->SiS_P3c4,0x3A);
+    } else {
+       index = SiS_GetReg(SiS_Pr->SiS_P3c4,0x1A);
+    }
+    index &= 0x07;
+    MCLK = SiS_Pr->SiS_MCLKData_0[index].CLOCK;				/* Get MCLK */
 
-        } else {
+    data2 = (colorth * VCLK) / MCLK;
 
-#ifdef SIS315H	   /* 315 series */
+    temp = SiS_GetReg(SiS_Pr->SiS_P3c4,0x14);
+    temp = ((temp & 0x00FF) >> 6) << 1;
+    if(temp == 0) temp = 1;
+    temp <<= 2;
+    temp &= 0xff;
 
-           if(IS_SIS550650740660) {		/* 550, 650, 740, 660 */
+    data2 = temp - data2;
 
-#if 0
-	      if(SiS_GetReg1(SiS_Pr->SiS_Part4Port,0x00) != 1) return;	/* From 1.10.7w */
-#endif
+    if((28 * 16) % data2) {
+      	data2 = (28 * 16) / data2;
+      	data2++;
+    } else {
+      	data2 = (28 * 16) / data2;
+    }
 
-	      modenum = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x34);
+    if(HwInfo->jChipType == SIS_300) {
 
-              if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {			/* LV */
-	      
-	         SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x30,0x00);
-		 
-		 if( (modenum <= 0x13) ||
-		     (!(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr))) ||
-		     (SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) ) {
-	     	      SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x26,0xFE);
-		      if(SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) {
-		         SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 3);
-		      }
-	         }
+	tempah = SiS_GetReg(SiS_Pr->SiS_P3c4,0x18);
+	tempah &= 0x62;
+	tempah >>= 1;
+	tempal = tempah;
+	tempah >>= 3;
+	tempal |= tempah;
+	tempal &= 0x07;
+	tempcl = ThTiming[tempal];
+	tempbx = SiS_GetReg(SiS_Pr->SiS_P3c4,0x16);
+	tempbx >>= 6;
+	tempah = SiS_GetReg(SiS_Pr->SiS_P3c4,0x14);
+	tempah >>= 4;
+	tempah &= 0x0c;
+	tempbx |= tempah;
+	tempbx <<= 1;
+	tempal = ThLowB[tempbx + 1];
+	tempal *= tempcl;
+	tempal += ThLowB[tempbx];
+	data = tempal;
 
-		 if(SiS_Pr->SiS_CustomT != CUT_COMPAQ1280) {
-		    SiS_DDC2Delay(SiS_Pr,0xff00);
-		    SiS_DDC2Delay(SiS_Pr,0x6000);
-		    SiS_DDC2Delay(SiS_Pr,0x8000);
+    } else if(HwInfo->jChipType == SIS_730) {
 
-	            SiS_SetReg3(SiS_Pr->SiS_P3c6,0x00);
+#ifndef LINUX_XF86
+       SiS_SetRegLong(0xcf8,0x80000050);
+       eax = SiS_GetRegLong(0xcfc);
+#else
+       eax = pciReadLong(0x00000000, 0x50);
+#endif
+       tempal = (USHORT)(eax >> 8);
+       tempal &= 0x06;
+       tempal <<= 5;
 
-                    pushax = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x06);
+#ifndef LINUX_XF86
+       SiS_SetRegLong(0xcf8,0x800000A0);
+       eax = SiS_GetRegLong(0xcfc);
+#else
+       eax = pciReadLong(0x00000000, 0xA0);
+#endif
+       temp = (USHORT)(eax >> 28);
+       temp &= 0x0F;
+       tempal |= temp;
 
-		    if(IS_SIS740) {
-		       SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x06,0xE3);
-		    }
+       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;
+       tempbx &= 0xc0;
+       tempbx >>= 6;
+       tempax &= 0x0f;
+       tempax *= 3;
+       tempbx += tempax;
 
-	            SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 3);
+       data = LatencyFactor730[tempbx];
+       data += 15;
+       temp = SiS_GetReg(SiS_Pr->SiS_P3c4,0x14);
+       if(!(temp & 0x80)) data += 5;
 
-		    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 {
 
-              } else if(SiS_Pr->SiS_VBType & VB_NoLCD) {			/* B-DH */
-	         /* This is actually bullshit. The B-DH bridge has cetainly no
-		  * Part4 Index 26, since it has no ability to drive LCD panels
-		  * at all. But as the BIOS does it, we do it, too...
-		  */
-	         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);
-		 }
-	      }
+       index = 0;
+       temp = SiS_GetReg(SiS_Pr->SiS_P3c4,0x14);
+       if(temp & 0x0080) index += 12;
 
-	      if(SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) {
-	         SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x1F,0xef);
-	      }
+#ifndef LINUX_XF86
+       SiS_SetRegLong(0xcf8,0x800000A0);
+       eax = SiS_GetRegLong(0xcfc);
+#else
+       /* 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.
+        */
+       eax = pciReadLong(0x00000000, 0xA0);
+#endif
+       temp = (USHORT)(eax >> 24);
+       if(!(temp&0x01)) index += 24;
 
-              if((SiS_Pr->SiS_VBType & VB_SIS301B302B) || (SiS_Pr->SiS_CustomT == CUT_COMPAQ1280)) {
-	         tempah = 0x3f;
-	         if(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-	            tempah = 0x7f;
-	            if(!(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-		       tempah = 0xbf;
-                    }
-	         }
-	         SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x1F,tempah);
-	      }
+#ifndef LINUX_XF86
+       SiS_SetRegLong(0xcf8,0x80000050);
+       eax = SiS_GetRegLong(0xcfc);
+#else
+       eax = pciReadLong(0x00000000, 0x50);
+#endif
+       temp=(USHORT)(eax >> 24);
+       if(temp & 0x01) index += 6;
 
-              if((SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) ||
-	         ((SiS_Pr->SiS_VBType & VB_SIS301LV302LV) && (modenum <= 0x13))) {
+       temp = (temp & 0x0F) >> 1;
+       index += temp;
 
-	         if((SiS_Pr->SiS_VBType & VB_SIS301B302B) || (SiS_Pr->SiS_CustomT == CUT_COMPAQ1280)) {
-		    SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x1E,0xDF);
-		    SiS_DisplayOff(SiS_Pr);
-		    SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x32,0xDF);
-		 } else {
-	            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);
-		    if((SiS_Pr->SiS_VBType & VB_SIS301LV302LV) && (modenum <= 0x13)) {
-		       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);
-		    }
-		 }
+       data = LatencyFactor[index];
+       data += 15;
+       temp = SiS_GetReg(SiS_Pr->SiS_P3c4,0x14);
+       if(!(temp & 0x80)) data += 5;
+    }
 
-	      } else {
+    data += data2;				/* CRT1 Request Period */
 
-	         if((SiS_Pr->SiS_VBType & VB_SIS301B302B) || (SiS_Pr->SiS_CustomT == CUT_COMPAQ1280)) {
-		    if(!(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-		       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);
-		    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);
-		 } 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_Pr->SiS_SetFlag |= ProgrammingCRT2;
+    SiS_Pr->SiS_SelectCRT2Rate = SelectRate_backup;
 
-	      }
+    if(!SiS_Pr->UseCustomMode) {
 
-	      if((SiS_Pr->SiS_VBType & VB_SIS301LV302LV) && (SiS_Pr->SiS_CustomT != CUT_COMPAQ1280)) {
+       CRT2ModeNo = ModeNo;
+       SiS_SearchModeID(SiS_Pr, &CRT2ModeNo, &modeidindex);
 
-		 SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x1f,~0x10);    		/* 1.10.8r, 8m */
+       refreshratetableindex = SiS_GetRatePtr(SiS_Pr, CRT2ModeNo, modeidindex, HwInfo);
 
-	         tempah = 0x3f;
-	         if(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-	            tempah = 0x7f;
-	            if(!(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-		       tempah = 0xbf;
-                    }
-	         }
-	         SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x1F,tempah);
+       index = SiS_GetVCLK2Ptr(SiS_Pr,CRT2ModeNo,modeidindex,
+                               refreshratetableindex,HwInfo);
+       VCLK = SiS_Pr->SiS_VCLKData[index].CLOCK;                         	/* Get VCLK  */
 
-		 if(SiS_IsNotM650or651(SiS_Pr,HwDeviceExtension, BaseAddr)) {   /* 1.10.8r, 8m */
-	            SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2e,0x7f);
-		 }								/* 1.10.8r, 8m */
+       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);
+	     }
+          }
+       }
 
-	         if(!(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-	            SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x00,0xdf);
-	         }
+    } else {
 
-	         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);
-                       }
-                    }
-	         }
+       CRT2ModeNo = 0xfe;
+       VCLK = SiS_Pr->CSRClock;							/* Get VCLK */
 
-	         SiS_SetReg1(SiS_Pr->SiS_P3c4,0x06,pushax);
+    }
 
-  	      } else if(SiS_Pr->SiS_VBType & VB_NoLCD) {
+    colorth = SiS_GetColorDepth(SiS_Pr,CRT2ModeNo,modeidindex);   	/* Get colordepth */
+    colorth >>= 1;
+    if(!colorth) colorth++;
 
-	         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_SetRegAND(SiS_Pr->SiS_Part4Port,0x26,0xFD);
-			  SiS_SetPanelDelay(SiS_Pr, ROMAddr, HwDeviceExtension, 4);
-	               }
-		    }
-		 }
+    data = data * VCLK * colorth;
+    if(data % (MCLK << 4)) {
+      	data = data / (MCLK << 4);
+      	data++;
+    } else {
+      	data = data / (MCLK << 4);
+    }
 
-	      } else if((SiS_Pr->SiS_VBType & VB_SIS301B302B) || (SiS_Pr->SiS_CustomT == CUT_COMPAQ1280)) {
+    if(data <= 6) data = 6;
+    if(data > 0x14) data = 0x14;
 
-	         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);
-		    }
-		    if(SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) {
-		       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_SetRegAND(SiS_Pr->SiS_Part4Port,0x26,0xFD);
-			     SiS_SetPanelDelay(SiS_Pr, ROMAddr, HwDeviceExtension, 4);
-	                  }
-		       }
-		    }
-		 }
+    temp = SiS_GetReg(SiS_Pr->SiS_Part1Port,0x01);
+    if(HwInfo->jChipType == SIS_300) {
+       if(data <= 0x0f) temp = (temp & (~0x1F)) | 0x13;
+       else             temp = (temp & (~0x1F)) | 0x16;
+       if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
+       		temp = (temp & (~0x1F)) | 0x13;
+       }
+    } else {
+       if( ( (HwInfo->jChipType == SIS_630) ||
+             (HwInfo->jChipType == SIS_730) )  &&
+           (HwInfo->jChipRevision >= 0x30) ) /* 630s or 730(s?) */
+      {
+	  temp = (temp & (~0x1F)) | 0x1b;
+      } else {
+	  temp = (temp & (~0x1F)) | 0x16;
+      }
+    }
+    SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x01,0xe0,temp);
 
-	      }
+    if( (HwInfo->jChipType == SIS_630) &&
+        (HwInfo->jChipRevision >= 0x30) ) /* 630s, NOT 730 */
+    {
+   	if(data > 0x13) data = 0x13;
+    }
+    SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x02,0xe0,data);
 
-	  } else {			/* 315, 330 - all bridge types */
+  } else {  /* If mode <= 0x13, we just restore everything */
 
-	     if(SiS_Is301B(SiS_Pr,BaseAddr)) {
-	        tempah = 0x3f;
-	        if(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-	           tempah = 0x7f;
-	           if(!(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-		      tempah = 0xbf;
-                   }
-	        }
-	        SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x1F,tempah);
-	        if(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-	           SiS_DisplayOff(SiS_Pr);
-		   SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x32,0xDF);
-	        }
-	     }
-	     if( (!(SiS_Is301B(SiS_Pr,BaseAddr))) ||
-	         (!(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr))) ) {
+    SiS_Pr->SiS_SetFlag |= ProgrammingCRT2;
+    SiS_Pr->SiS_SelectCRT2Rate = SelectRate_backup;
 
- 	 	if( (!(SiS_Is301B(SiS_Pr,BaseAddr))) ||
-		    (!(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr))) ) {
+  }
+}
+#endif
 
-	           SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x00,0xDF);
-	           SiS_DisplayOff(SiS_Pr);
+/**** Set CRT2 FIFO on 315/330 series ****/
+#ifdef SIS315H
+static void
+SiS_SetCRT2FIFO_310(SiS_Private *SiS_Pr)
+{
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x01,0x3B);
+  SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x02,~0x3F,0x04);
+}
+#endif
 
-		}
+/*************** Set LCD-A ***************/
+#ifdef SIS315H
+static void
+SiS_SetGroup1_LCDA(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,
+                   PSIS_HW_INFO HwInfo, USHORT RefreshRateTableIndex)
+{
+  USHORT modeflag,resinfo;
+  USHORT push2,tempax,tempbx,tempcx,temp;
+  ULONG tempeax=0,tempebx,tempecx,tempvcfact;
 
-                SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x00,0x80);
+  /* This is not supported with LCDA */
+  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_PanelCustom) return;
+  if(SiS_Pr->UseCustomMode) return;
 
-                SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x32,0xDF);
+  if(IS_SIS330) {
+     SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2D,0x10);			/* Xabre 1.01.03 */
+  } else if(IS_SIS740) {
+     if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {					/* 740/LVDS */
+        SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x13,0xfb,0x04);      	/* 740/LVDS */
+	SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2D,0x03);
+     } else if(SiS_Pr->SiS_VBType & VB_SISVB) {
+        SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2D,0x10);			/* 740/301LV, 301BDH */
+     }
+  } else {
+     if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {					/* 650/LVDS */
+        SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x13,0xfb,0x04);      	/* 650/LVDS */
+	SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2D,0x00);			/* 650/LVDS 1.10.07 */
+     } else if(SiS_Pr->SiS_VBType & VB_SISVB) {
+        SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2D,0x0f);			/* 650/30xLv 1.10.6s */
+     }
+  }
 
-	        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);
+  if(ModeNo <= 0x13) {
+     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;
+  }
 
-	     }
+  tempax = SiS_Pr->SiS_LCDHDES;
 
-	  }    /* 315/330 */
+  temp = (tempax & 0x0007);                        		/* BPLHDESKEW[2:0]   */
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1A,temp);                         /* Part1_1Ah  */
+  temp = (tempax >> 3) & 0x00FF;                               	/* BPLHDESKEW[10:3]  */
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x16,temp);                         /* Part1_16h  */
 
-#endif /* SIS315H */
+  tempbx = SiS_Pr->SiS_HDE;
+  if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
+     tempbx = SiS_Pr->PanelXRes;
+  }
 
-	}
+  tempax += tempbx;	                                    	/* HDE + HSKEW = lcdhdee  */
+  if(tempax >= SiS_Pr->SiS_HT) tempax -= SiS_Pr->SiS_HT;
 
-      } else {     /* ============ For 301 ================ */
+  temp = tempax;
+  if(SiS_Pr->SiS_VBType & VB_SISVB) {
+     if(temp & 0x07) temp += 8;
+  }
+  temp >>= 3;                                        		/* BPLHDEE  */
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x17,temp);                        	/* Part1_17h  */
 
-        if(HwDeviceExtension->jChipType < SIS_315H) {
-            if(!(SiS_CR36BIOSWord23b(SiS_Pr,HwDeviceExtension))) {
-	      SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x11,0xF7,0x08);
-	      SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 3);
-	   }
-	}
+  tempcx = (SiS_Pr->SiS_HT - tempbx) >> 2;     	            	/* (HT-HDE) / 4  */
 
-        SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x00,0xDF);           /* disable VB */
-        SiS_DisplayOff(SiS_Pr);
+  /* 650/30xLV 1.10.6s, 740/LVDS */
+  if( ((SiS_Pr->SiS_VBType & VB_SISVB) && (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) {
+        if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600)        tempcx = 0x28;
+ 	else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768)  tempcx = 0x18;
+	else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) tempcx = 0x30;
+	else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) tempcx = 0x40;
+	else                                                          tempcx = 0x30;
+     }
+  }
 
-        if(HwDeviceExtension->jChipType >= SIS_315H) {
-            SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x00,0x80);
-	}
+  tempcx += tempax;  	                                  	/* lcdhrs  */
+  if(tempcx >= SiS_Pr->SiS_HT) tempcx -= SiS_Pr->SiS_HT;
 
-        SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x32,0xDF);                /* disable lock mode */
+  temp = (tempcx >> 3) & 0x00FF;				/* BPLHRS */
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x14,temp);                 		/* Part1_14h  */
 
-	if(HwDeviceExtension->jChipType >= SIS_315H) {
-            temp = SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x00);
-            SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x00,0x10);
-	    SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x1E,0x20);
-	    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);
-	    }
+  temp += 10;
+  if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
+        if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480) {
+	   temp += 6;
+	   if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel800x600) {
+	      temp++;
+	      if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1024x768) {
+	         temp += 7;
+		 if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1600x1200) {
+		    temp -= 10;
+		 }
+	      }
+	   }
 	}
+     }
+  }
+  temp &= 0x1F;
+  temp |= ((tempcx & 0x07) << 5);
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x15,temp);                         /* Part1_15h  */
 
-      }
+  if(SiS_Pr->SiS_IF_DEF_TRUMPION == 0) {
+     tempax = SiS_Pr->PanelYRes;
+  } else {
+     tempax = SiS_Pr->SiS_VGAVDE;
+  }
 
-  } else {     /* ============ For LVDS =============*/
+  tempbx = SiS_Pr->SiS_LCDVDES + tempax;
+  if(tempbx >= SiS_Pr->SiS_VT) tempbx -= SiS_Pr->SiS_VT;
+  push2 = tempbx;
 
-    if(HwDeviceExtension->jChipType < SIS_315H) {
+  tempcx = (SiS_Pr->SiS_VGAVT - SiS_Pr->SiS_VGAVDE) >> 2;
 
-#ifdef SIS300	/* 300 series */
+  if( ((SiS_Pr->SiS_VBType & VB_SISVB) && (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) {
+        if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600)         tempcx = 1;
+   	else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768)   tempcx = 3;
+	else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768)   tempcx = 3;
+	else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024)  tempcx = 1;
+	else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050)  tempcx = 1;
+	else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200)  tempcx = 1;
+	else                                                           tempcx = 0x0057;
+     }
+  }
 
-	if(SiS_Pr->SiS_IF_DEF_CH70xx == 1) {
-	   SiS_SetCH700x(SiS_Pr,0x090E);
-	}
+  tempbx += tempcx;
+  if(SiS_Pr->SiS_VBType & VB_SISVB) {
+     tempbx++;                                                	/* BPLVRS  */
+  }
+  if(tempbx >= SiS_Pr->SiS_VT) tempbx -= SiS_Pr->SiS_VT;
+  temp = tempbx & 0x00FF;
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x18,temp);                             /* Part1_18h  */
 
-	if(HwDeviceExtension->jChipType == SIS_730) {
-	   if(!(SiS_GetReg1(SiS_Pr->SiS_P3c4,0x11) & 0x08)) {
-	      SiS_WaitVBRetrace(SiS_Pr,HwDeviceExtension);
-	   }
-	   if(!(SiS_CR36BIOSWord23b(SiS_Pr,HwDeviceExtension))) {
-	      SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x11,0xF7,0x08);
-	      SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 3);
-	   }
-	} else {
-	   if(!(SiS_GetReg1(SiS_Pr->SiS_P3c4,0x11) & 0x08)) {
+  tempcx >>= 3;
+  if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
+     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 = 3;
+   	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768)   tempcx = 5;
+	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768)   tempcx = 5;
+	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024)  tempcx = 5;
+	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050)  tempcx = 2;
+	   else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200)  tempcx = 2;
+	}
+     }
+  }
+  tempcx += tempbx;
+  tempcx++;                                                	/* BPLVRE  */
+  temp = tempcx & 0x000F;
+  if(SiS_Pr->SiS_VBType & VB_SISVB) {
+     temp |= 0xC0;
+     SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x19,0xF0,temp); 		/* Part1_19h  */
+  } else {
+     SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x19,0xF0,temp);
+  }
 
-	      if(!(SiS_GetReg1(SiS_Pr->SiS_P3c4,0x13) & 0x40)) {
-  
-  	         if(!(SiS_CR36BIOSWord23b(SiS_Pr,HwDeviceExtension))) {
+  temp = ((tempbx >> 8) & 0x07) << 3;
+  if(SiS_Pr->SiS_VGAVDE != SiS_Pr->SiS_VDE) temp |= 0x40;
+  if(SiS_Pr->SiS_SetFlag & EnableLVDSDDA)   temp |= 0x40;
+  if(SiS_Pr->SiS_VBType & VB_SISVB) {
+     /* Don't check Part1Port,0x00 -> is not being set if LCDA! */
+     /* We check SR06 instead here: */
+     if(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit) {
+        if(SiS_GetReg(SiS_Pr->SiS_P3c4,0x06) & 0x10) temp |= 0x80;
+     }
+  } else {
+     if(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit) {
+        if(SiS_GetReg(SiS_Pr->SiS_Part1Port,0x00) & 0x01) temp |= 0x80;
+     }
+  }
+  SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x1A,0x07,temp);            /* Part1_1Ah */
 
-                    SiS_WaitVBRetrace(SiS_Pr,HwDeviceExtension);
+  tempbx = push2;                                      		/* BPLVDEE */
 
-		    if(!(SiS_GetReg1(SiS_Pr->SiS_P3c4,0x06) & 0x1c)) {
-		        SiS_DisplayOff(SiS_Pr);
-	            }
+  tempcx = SiS_Pr->SiS_LCDVDES;                        		/* NPLVDES */
+  if(!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
+     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600) {
+        if(resinfo == SIS_RI_800x600) tempcx++;
+     }
+  }
+  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480) {
+     tempbx = tempcx = SiS_Pr->SiS_VGAVDE;
+     tempbx--;
+  }
 
-	            SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x11,0xF7,0x08);
-	            SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 3);
-                 }
-              }
-	   }
-	}
+  temp = ((tempbx >> 8) & 0x07) << 3;
+  temp = temp | ((tempcx >> 8) & 0x07);
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1D,temp);                          /* Part1_1Dh */
+  temp = tempbx & 0x00FF;
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1C,temp);                          /* Part1_1Ch  */
+  temp = tempcx & 0x00FF;
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1B,temp);                          /* Part1_1Bh  */
 
-	SiS_DisplayOff(SiS_Pr);
+  tempeax = SiS_Pr->SiS_VGAVDE << 18;
+  tempebx = SiS_Pr->SiS_VDE;
+  temp = (USHORT)(tempeax % tempebx);
+  tempeax = tempeax / tempebx;
+  if(temp) tempeax++;
+  tempvcfact = tempeax;
 
-	SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x32,0xDF);
+  temp = (USHORT)(tempeax & 0x00FF);
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x37,temp);
 
-	SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x1E,0xDF);
-	SiS_UnLockCRT2(SiS_Pr,HwDeviceExtension,BaseAddr);
-	SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x01,0x80);
-	SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x02,0x40);
+  temp = (USHORT)((tempeax & 0x00FF00) >> 8);
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x36,temp);
 
-	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);
-	}
+  temp = (USHORT)((tempeax & 0x00030000) >> 16);
+  if(SiS_Pr->SiS_VDE == SiS_Pr->SiS_VGAVDE) temp |= 0x04;
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x35,temp);
 
-#endif  /* SIS300 */
+  if(SiS_Pr->SiS_VBType & (VB_SIS301C | VB_SIS302ELV)) {
+     temp = (USHORT)(tempeax & 0x00FF);
+     SiS_SetReg(SiS_Pr->SiS_Part4Port,0x3c,temp);
+     temp = (USHORT)((tempeax & 0x00FF00) >> 8);
+     SiS_SetReg(SiS_Pr->SiS_Part4Port,0x3b,temp);
+     temp = (USHORT)(((tempeax & 0x00030000) >> 16) << 6);
+     SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x3a,0x3f,temp);
+     temp = 0;
+     if(SiS_Pr->SiS_VDE != SiS_Pr->SiS_VGAVDE) temp |= 0x08;
+     SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x30,0xf3,temp);
+  }
 
-    } else {
+  tempeax = SiS_Pr->SiS_VGAHDE << 16;
+  tempebx = SiS_Pr->SiS_HDE;
+  temp = tempeax % tempebx;
+  tempeax /= tempebx;
+  if(temp) tempeax++;
+  if(tempebx == SiS_Pr->SiS_VGAHDE) tempeax = 0xFFFF;
+  tempecx = tempeax;
+  tempeax = ((SiS_Pr->SiS_VGAHDE << 16) / tempecx) - 1;
+  tempecx = (tempecx << 16) | (tempeax & 0xFFFF);
+  temp = (USHORT)(tempecx & 0x00FF);
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1F,temp);                          /* Part1_1Fh  */
 
-#ifdef SIS315H	/* 315 series */
+  tempeax = (SiS_Pr->SiS_VGAVDE << 18) / tempvcfact;
+  tempbx = (USHORT)(tempeax & 0x0FFFF);
 
-	if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
+  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) tempbx--;
 
-		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_Pr->SiS_SetFlag & EnableLVDSDDA)  tempbx = 1;
 
-		   if(!(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-			SiS_SetCH701x(SiS_Pr,0x3e49);
-		   } else if(SiS_IsTVOrYPbPrOrScart(SiS_Pr,HwDeviceExtension, BaseAddr))  {
-			SiS_SetCH701x(SiS_Pr,0x3e49);
-		   }
-		}
+  temp = ((tempbx >> 8) & 0x07) << 3;
+  temp = temp | ((tempecx >> 8) & 0x07);
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x20,temp);                         /* Part1_20h */
 
-		if(!(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-			SiS_Chrontel701xBLOff(SiS_Pr);
-			SiS_Chrontel701xOff(SiS_Pr,HwDeviceExtension);
-		} else if(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-			SiS_Chrontel701xBLOff(SiS_Pr);
-			SiS_Chrontel701xOff(SiS_Pr,HwDeviceExtension);
-		}
+  temp = tempbx & 0x00FF;
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x21,temp);                         /* Part1_21h */
 
-		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);
-		   }
-		}
+  tempecx >>= 16;   	                                  	/* BPLHCFACT  */
+  if(modeflag & HalfDCLK) tempecx >>= 1;
+  temp = (USHORT)((tempecx & 0x0000FF00) >> 8);
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x22,temp);                         /* Part1_22h */
 
-	}
+  temp = (USHORT)(tempecx & 0x000000FF);
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x23,temp);
 
-	if(SiS_Pr->SiS_IF_DEF_CH70xx == 0) {
-		SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x11,0xF7,0x08);
-		SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 3);
-	}
+  if((SiS_Pr->SiS_IF_DEF_LVDS == 1) || (SiS_Pr->SiS_VBInfo & VB_SIS301LV302LV)) {
+     SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1e,0x20);
+  }
+}
+#endif  /* SIS 315 */
 
-	if(SiS_Pr->SiS_IF_DEF_CH70xx == 0) {
-		SiS_DisplayOff(SiS_Pr);
-	} else if(!(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-		SiS_DisplayOff(SiS_Pr);
-	} else if(!(SiS_IsTVOrYPbPrOrScart(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-		SiS_DisplayOff(SiS_Pr);
-	}
+static USHORT
+SiS_GetVGAHT2(SiS_Private *SiS_Pr)
+{
+  ULONG tempax,tempbx;
 
-	if(SiS_Pr->SiS_IF_DEF_CH70xx == 0) {
-		SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x00,0x80);
-	} else if(!(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-		SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x00,0x80);
-	} else if(!(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-		SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x00,0x80);
-	}
+  tempbx = ((SiS_Pr->SiS_VGAVT - SiS_Pr->SiS_VGAVDE) * SiS_Pr->SiS_RVBHCMAX) & 0xFFFF;
+  tempax = (SiS_Pr->SiS_VT - SiS_Pr->SiS_VDE) * SiS_Pr->SiS_RVBHCFACT;
+  tempax = (tempax * SiS_Pr->SiS_HT) / tempbx;
+  return((USHORT) tempax);
+}
 
-	if(HwDeviceExtension->jChipType == SIS_740) {
-	   SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2e,0x7f);
-	}
+/******* Set Part 1 / SiS bridge *********/
+static void
+SiS_SetGroup1_301(SiS_Private *SiS_Pr, USHORT ModeNo,USHORT ModeIdIndex,
+                  PSIS_HW_INFO HwInfo,USHORT RefreshRateTableIndex)
+{
+  USHORT  push1,push2;
+  USHORT  tempax,tempbx,tempcx,temp;
+  USHORT  resinfo,modeflag;
+  unsigned char p1_7, p1_8;
 
-	SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x32,0xDF);
+  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(SiS_Pr->SiS_IF_DEF_CH70xx == 0) {
-		SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x1E,0xDF);
-	} else if(!(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-		SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x1E,0xDF);
-	} else if(!(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-		SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x1E,0xDF);
-	}
+  /* The following is only done if bridge is in slave mode: */
 
-	if(SiS_Pr->SiS_IF_DEF_CH70xx == 0) {
-	        if(SiS_CRT2IsLCD(SiS_Pr, BaseAddr,HwDeviceExtension)) {
-		   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(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);
-	        }
-	   }
-	}
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x03,0xff);                  /* set MAX HT */
 
-	if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
-	    	if(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-			/* SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x13,0xff); */
-		} else {
-			SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x13,0xfb);
-		}
-	}
+  if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV)  modeflag |= Charx8Dot;
 
-	SiS_UnLockCRT2(SiS_Pr,HwDeviceExtension, BaseAddr);
+  if(modeflag & Charx8Dot) tempcx = 0x08;
+  else                     tempcx = 0x09;
 
-	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);
-	} else if(!(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-		SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2e,0xf7);
-	}
-
-#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)) {
-		  /* Nothing there! */
-		}
-           }
-       }
-#endif
-       if(SiS_Pr->SiS_IF_DEF_CH70xx == 0) {
-		if(SiS_CRT2IsLCD(SiS_Pr, BaseAddr,HwDeviceExtension)) {
-			if(!(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-				SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 2);
-				SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x11,0xFB,0x04);
-			}
-		}
-       }
+  tempax = SiS_Pr->SiS_VGAHDE;                                 	/* 0x04 Horizontal Display End */
+  if(modeflag & HalfDCLK) tempax >>= 1;
+  tempax = ((tempax / tempcx) - 1) & 0xff;
+  tempbx = tempax;
 
-#endif  /* SIS315H */
+  temp = tempax;
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x04,temp);
 
-    }  /* 310 series */
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+     if(!(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV)) {
+        temp += 2;
+     }
+  }
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) {
+     if(resinfo == SIS_RI_800x600) temp -= 2;
+  }
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x05,temp);                 /* 0x05 Horizontal Display Start */
 
-  }  /* LVDS */
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x06,0x03);                 /* 0x06 Horizontal Blank end     */
 
-}
+  tempax = 0xFFFF;
+  if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToTV)) tempax = SiS_GetVGAHT2(SiS_Pr);
+  if(tempax >= SiS_Pr->SiS_VGAHT) tempax = SiS_Pr->SiS_VGAHT;
+  if(modeflag & HalfDCLK)         tempax >>= 1;
+  tempax = (tempax / tempcx) - 5;
+  tempcx = tempax;
 
-void
-SiS_EnableBridge(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension, USHORT BaseAddr)
-{
-  USHORT temp=0,tempah;
-#ifdef SIS315H
-  USHORT temp1,pushax=0;
-  BOOLEAN delaylong = FALSE;
-#endif
-  UCHAR *ROMAddr = HwDeviceExtension->pjVirtualRomBase;
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) {
+     temp = tempcx - 1;
+     if(!(modeflag & HalfDCLK)) {
+        temp -= 6;
+        if(SiS_Pr->SiS_TVMode & TVSetTVSimuMode) {
+           temp -= 2;
+           if(ModeNo > 0x13) temp -= 10;
+        }
+     }
+  } else {
+     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(HwInfo->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) &&
+	      (SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1600x1200)) {
+              if(SiS_Pr->SiS_VGAHDE >= 800) {
+                 temp -= 7;
+	         if(HwInfo->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_LCDResInfo != SiS_Pr->SiS_Panel1400x1050) {
+                    if(SiS_Pr->SiS_VGAHDE >= 1280) {
+                       if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) temp += 28;
+		    }
+                 }
+              }
+           }
+        }
+     }
+  }
 
-  if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
+  p1_7 = temp;
+  p1_8 = 0x00;
 
-    if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {   /* ====== For 301B et al  ====== */
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+     if(SiS_Pr->SiS_TVMode & TVSetTVSimuMode) {
+        if(ModeNo <= 0x01) {
+	   p1_7 = 0x2a;
+	   if(!(SiS_Pr->SiS_TVMode & TVSetPAL)) p1_8 = 0x61;
+	   else 	      			p1_8 = 0x41;
+	} else if(SiS_Pr->SiS_ModeType == ModeText) {
+	   if(!(SiS_Pr->SiS_TVMode & TVSetPAL)) p1_7 = 0x54;
+	   else 	    			p1_7 = 0x55;
+	   p1_8 = 0x00;
+	} else if(ModeNo <= 0x13) {
+	   if(modeflag & HalfDCLK) {
+	      if(!(SiS_Pr->SiS_TVMode & TVSetPAL)) {
+		 p1_7 = 0x30;
+		 p1_8 = 0x03;
+	      } else {
+	 	 p1_7 = 0x2f;
+		 p1_8 = 0x02;
+	      }
+	   } else {
+	      p1_7 = 0x5b;
+	      p1_8 = 0x03;
+	   }
+	} else if( ((HwInfo->jChipType >= SIS_315H) &&
+	            ((ModeNo == 0x50) || (ModeNo == 0x56) || (ModeNo == 0x53))) ||
+	           ((HwInfo->jChipType < SIS_315H) &&
+		    (resinfo == SIS_RI_320x200 || resinfo == SIS_RI_320x240)) ) {
+	   if(!(SiS_Pr->SiS_TVMode & TVSetPAL)) {
+	      p1_7 = 0x30,
+	      p1_8 = 0x03;
+	   } else {
+	      p1_7 = 0x2f;
+	      p1_8 = 0x03;
+	   }
+        }
+     }
+  }
 
-      if(HwDeviceExtension->jChipType < SIS_315H) {
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToYPbPr525750) {
+     if(SiS_Pr->SiS_TVMode & (TVSetYPbPr525p|TVSetYPbPr750p)) {
+        p1_7 = 0x63;
+	if(SiS_Pr->SiS_TVMode & TVSetYPbPr750p) p1_7 = 0x55;
+     }
+     if(SiS_Pr->SiS_TVMode & TVSetTVSimuMode) {
+        if(!(modeflag & HalfDCLK)) {
+	   p1_7 = 0xb2;
+	   if(SiS_Pr->SiS_TVMode & TVSetYPbPr750p) {
+	      p1_7 = 0xab;
+	   }
+	}
+     }
+  }
 
-#ifdef SIS300     /* 300 series */
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x07,p1_7);			/* 0x07 Horizontal Retrace Start */
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x08,p1_8);			/* 0x08 Horizontal Retrace End   */
 
-         if(HwDeviceExtension->jChipType == SIS_300) {
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x18,0x03);                	/* 0x18 SR08 (FIFO Threshold?)   */
 
-	    if(SiS_CRT2IsLCD(SiS_Pr,BaseAddr,HwDeviceExtension)) {
-	       if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) { 
-	          SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x26,0x02);
-	          if(!(SiS_CR36BIOSWord23d(SiS_Pr,HwDeviceExtension))) {
-	             SiS_SetPanelDelay(SiS_Pr, ROMAddr, HwDeviceExtension, 0);
-	          }
-	       }
-	    }
-	    temp = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x32) & 0xDF;             /* lock mode */
-            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_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x00,0x1F,0x20);        /* enable VB processor */
-	    if(SiS_Is301B(SiS_Pr,BaseAddr)) {
-               SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x1F,0xC0);
-	       SiS_DisplayOn(SiS_Pr);
-	    } else {
-	       SiS_VBLongWait(SiS_Pr);
-	       SiS_DisplayOn(SiS_Pr);
-	       SiS_VBLongWait(SiS_Pr);
-	    }
-	    if(SiS_CRT2IsLCD(SiS_Pr,BaseAddr,HwDeviceExtension)) {
-	       if(!(SiS_GetReg1(SiS_Pr->SiS_P3c4,0x13) & 0x40)) {
-		  if(!(SiS_GetReg1(SiS_Pr->SiS_P3c4,0x16) & 0x10)) {
-		     if(!(SiS_CR36BIOSWord23b(SiS_Pr,HwDeviceExtension))) {
-		           SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 1);
-                     }
-		     if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) { 
-		         SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x26,0xfe,0x01);
-	 	     }
-		  }
-               }
-	    }
+  SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x19,0xF0);
 
-	 } else {
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x09,0xFF);                	/* 0x09 Set Max VT    */
 
-	    if((SiS_Pr->SiS_VBType & VB_NoLCD) &&
-	       (SiS_CRT2IsLCD(SiS_Pr,BaseAddr,HwDeviceExtension))) {
-	       /* This is only for LCD output on 301B-DH via LVDS */
-	       SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x11,0xFB);
-	       if(!(SiS_CR36BIOSWord23d(SiS_Pr,HwDeviceExtension))) {
-	          SiS_SetPanelDelay(SiS_Pr, ROMAddr, HwDeviceExtension, 0);
-	       }
-	       SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x1E,0x20);   /* Enable CRT2 */
-/*	       DoSomeThingPCI_On(SiS_Pr) */
-               SiS_DisplayOn(SiS_Pr);
-	       SiS_UnLockCRT2(SiS_Pr,HwDeviceExtension, BaseAddr);
-	       SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x02,0xBF);
-	       if(SiS_BridgeInSlave(SiS_Pr)) {
-      		  SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x01,0x1F);
-      	       } else {
-      		  SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x01,0x1F,0x40);
-               }
-	       if(!(SiS_GetReg1(SiS_Pr->SiS_P3c4,0x13) & 0x40)) {
-	           if(!(SiS_GetReg1(SiS_Pr->SiS_P3c4,0x16) & 0x10)) {
-		       if(!(SiS_CR36BIOSWord23b(SiS_Pr,HwDeviceExtension))) {
-		           SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 1);
-                       }
-		       SiS_WaitVBRetrace(SiS_Pr,HwDeviceExtension);
-                       SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x11,0xF7,0x00);
-                   }
-	       }
-            } else {
-	       temp = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x32) & 0xDF;             /* lock mode */
-               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_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x00,0x1F,0x20);        /* enable VB processor */
-	       if(SiS_Is301B(SiS_Pr,BaseAddr)) {
-                  SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x1F,0xC0);
-	          SiS_DisplayOn(SiS_Pr);
-	       } else {
-	          SiS_VBLongWait(SiS_Pr);
-	          SiS_DisplayOn(SiS_Pr);
-	          SiS_VBLongWait(SiS_Pr);
-	       }
-	    }
+  tempcx = 0x121;
+  tempbx = SiS_Pr->SiS_VGAVDE;                               	/* 0x0E Vertical Display End */
+  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;
+           else if(tempbx == 480) tempbx += 5;
+      	}
+     }
+  }
+  tempbx -= 2;
+  temp = tempbx & 0x00FF;
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x10,temp);        		/* 0x10 vertical Blank Start */
 
-         }
-#endif /* SIS300 */
+  tempbx = push2;
+  tempbx--;
+  temp = tempbx & 0x00FF;
+#if 0
+  /* 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_SetReg(SiS_Pr->SiS_Part1Port,0x0E,temp);
 
-      } else {
+  temp = 0;
+  if(modeflag & DoubleScanMode) temp |= 0x80;
+  if(HwInfo->jChipType >= SIS_661) {
+     if(tempbx & 0x0200)        temp |= 0x20;
+     SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x0B,0x5F,temp);
+     if(tempbx & 0x0100)  tempcx |= 0x000a;
+     if(tempbx & 0x0400)  tempcx |= 0x1200;
+  } else {
+     SiS_SetReg(SiS_Pr->SiS_Part1Port,0x0B,temp);
+     if(tempbx & 0x0100)  tempcx |= 0x0002;
+     if(tempbx & 0x0400)  tempcx |= 0x0600;
+  }
 
-#ifdef SIS315H    /* 315 series */
+  if(tempbx & 0x0200)  tempcx |= 0x0040;
 
-	 if(IS_SIS550650740660) {		/* 550, 650, 740, 660 */
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x11,0x00);                	/* 0x11 Vertical Blank End */
 
-#if 0
-	    if(SiS_GetReg1(SiS_Pr->SiS_Part4Port,0x00) != 1) return;	/* From 1.10.7w */
-#endif
+  tempax = (SiS_Pr->SiS_VGAVT - tempbx) >> 2;
 
-	    if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
+  if((ModeNo > 0x13) || (HwInfo->jChipType < SIS_315H)) {
+     if(resinfo != SIS_RI_1280x1024) {
+	tempbx += (tempax << 1);
+     }
+  } else if(HwInfo->jChipType >= SIS_315H) {
+     if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1400x1050) {
+	tempbx += (tempax << 1);
+     }
+  }
 
-	       if(SiS_Pr->SiS_CustomT != CUT_COMPAQ1280) {
-	          SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x1f,0xef);  /* 1.10.7u */
-	          SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x30,0x00);    /* 1.10.7u */
-	       }
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) {
+     tempbx -= 10;
+  } else {
+     if(SiS_Pr->SiS_TVMode & TVSetTVSimuMode) {
+        if(SiS_Pr->SiS_TVMode & TVSetPAL) {
+           tempbx += 40;
+	   if(HwInfo->jChipType >= SIS_315H) {
+	      if(SiS_Pr->SiS_VGAHDE == 800) tempbx += 10;
+	   }
+	}
+     }
+  }
+  tempax >>= 2;
+  tempax++;
+  tempax += tempbx;
+  push1 = tempax;
+  if(SiS_Pr->SiS_TVMode & TVSetPAL) {
+     if(tempbx <= 513)  {
+     	if(tempax >= 513) tempbx = 513;
+     }
+  }
+  temp = tempbx & 0x00FF;
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x0C,temp);			/* 0x0C Vertical Retrace Start */
 
-	       if(!(IS_SIS740)) {
-                  if(!(SiS_IsNotM650or651(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-	             tempah = 0x10;
-		     if(SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) {
-		        if(SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x13) & 0x04) {
-			   if((SiS_GetReg1(SiS_Pr->SiS_Part2Port,0x00) & 0x0f) == 0x0c) {
-			      tempah = 0x08;
-			   } else {
-			      tempah = 0x18;
-			   }
-			}
-			SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x4c,tempah);
-		     } else {
-	                if(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-	                   tempah = 0x08;
-                        }
-			SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x4c,tempah);
-		     }
-	          }
-	       }
+  tempbx--;
+  temp = tempbx & 0x00FF;
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x10,temp);
 
-	       if(SiS_Pr->SiS_CustomT != CUT_COMPAQ1280) {
-	          SiS_SetReg3(SiS_Pr->SiS_P3c6,0x00);
-	          SiS_DisplayOff(SiS_Pr);
-	          pushax = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x06);
-	          if(IS_SIS740) {
-	             SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x06,0xE3);
-	          }
-	       }
+  if(tempbx & 0x0100) tempcx |= 0x0008;
 
-	       if( (SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) ||
-	           (SiS_CRT2IsLCD(SiS_Pr,BaseAddr,HwDeviceExtension)) ) {
-                  if(!(SiS_GetReg1(SiS_Pr->SiS_Part4Port,0x26) & 0x02)) {
-		     if(SiS_Pr->SiS_CustomT != CUT_COMPAQ1280) {
-		        SiS_SetPanelDelayLoop(SiS_Pr,ROMAddr, HwDeviceExtension, 3, 2);
-		     }
-		     SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x26,0x02);
-	             SiS_SetPanelDelayLoop(SiS_Pr,ROMAddr, HwDeviceExtension, 3, 2);
-	          }
-	       }
+  if(tempbx & 0x0200) {
+     SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x0B,0x20);
+  }
+  tempbx++;
 
-               if(SiS_Pr->SiS_CustomT != CUT_COMPAQ1280) {
-	          if(!(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x31) & 0x40)) {
-                     SiS_SetPanelDelayLoop(SiS_Pr,ROMAddr, HwDeviceExtension, 3, 10);
-		     delaylong = TRUE;
-		  }
-	       }
+  if(tempbx & 0x0100) tempcx |= 0x0004;
+  if(tempbx & 0x0200) tempcx |= 0x0080;
+  if(tempbx & 0x0400) {
+     if(HwInfo->jChipType >= SIS_661)        tempcx |= 0x0800;
+     else if(SiS_Pr->SiS_VBType & VB_SIS301) tempcx |= 0x0800;
+     else                                    tempcx |= 0x0C00;
+  }
 
-	    } else if(SiS_Pr->SiS_VBType & VB_NoLCD) {
+  tempbx = push1;
+  temp = tempbx & 0x000F;
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x0D,temp);        		/* 0x0D vertical Retrace End */
 
-	       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);
-		  }
-	       }
+  if(tempbx & 0x0010) tempcx |= 0x2000;
 
-  	    } else if(SiS_Pr->SiS_VBType & VB_SIS301B302B) {
+  temp = tempcx & 0x00FF;
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x0A,temp);              	/* 0x0A CR07 */
 
-	       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);
-		  }
-	       }
-	       
-	    }
+  temp = (tempcx & 0xFF00) >> 8;
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x17,temp);              	/* 0x17 SR0A */
 
-	    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)) {
-		     if(SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) {
-		        if(!(SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x13) & 0x04)) temp |= 0x20;
-		     } else temp |= 0x20;
-		  }
-               }
-               SiS_SetReg1(SiS_Pr->SiS_P3c4,0x32,temp);
+  tempax = modeflag;
+  temp = (tempax & 0xFF00) >> 8;
+  temp = (temp >> 1) & 0x09;
+  if(!(SiS_Pr->SiS_VBType & VB_SIS301)) temp |= 0x01;		/* Always 8 dotclock */
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x16,temp);              	/* 0x16 SR01 */
 
-	       SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x1E,0x20);                   /* enable CRT2 */
-	       
-	       if((SiS_Pr->SiS_VBType & VB_SIS301B302B) || (SiS_Pr->SiS_CustomT == CUT_COMPAQ1280)) {
-	          SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2e,0x7f);
-		  temp = SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x2e);
-		  if(!(temp & 0x80)) {
-		     SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2e,0x80);
-		  }
-	       } else {
-	          SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 2);
-	       }
-	    }
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x0F,0x00);              	/* 0x0F CR14 */
 
-	    if(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-	       SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x1e,0x20);
-	    }
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x12,0x00);              	/* 0x12 CR17 */
 
-	    SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x00,0x1f,0x20);
+  temp = 0x00;
+  if(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit) {
+     if(SiS_GetReg(SiS_Pr->SiS_Part1Port,0x00) & 0x01) {
+	temp = 0x80;
+     }
+  }
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1A,temp);                	/* 0x1A SR0E */
+}
 
-	    if((SiS_Pr->SiS_VBType & VB_SIS301B302B) || (SiS_Pr->SiS_CustomT == CUT_COMPAQ1280)) {
-	       temp = SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x2e);
-	       if(!(temp & 0x80)) {
-		  SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2e,0x80);
-	       }
-	    }
+/*********** Set Part 1 / LVDS ***********/
+static void
+SiS_SetGroup1_LVDS(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,
+ 		   PSIS_HW_INFO HwInfo, USHORT RefreshRateTableIndex)
+{
+  USHORT modeflag, resinfo;
+  USHORT push1, push2, tempax, tempbx, tempcx, temp;
+#ifdef SIS315H
+  USHORT pushcx;
+#endif
+  ULONG  tempeax=0, tempebx, tempecx, tempvcfact=0;
 
-	    tempah = 0xc0;
-	    if(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-	       tempah = 0x80;
-	       if(!(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-	          tempah = 0x40;
-               }
-	    }
-            SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x1F,tempah);
+  /* This is not supported on LVDS */
+  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_PanelCustom) return;
+  if(SiS_Pr->UseCustomMode) return;
 
-	    if((SiS_Pr->SiS_VBType & VB_SIS301B302B) ||
-	       ((SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) &&
-	        (!(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr))))) {
-               SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x00,0x7f);
-	    }
+  if(ModeNo <= 0x13) {
+     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;
+  }
 
-	    if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
-	    
-	       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);
+  /* Set up Panel Link */
 
-	       if(SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) {
+  /* 1. Horizontal setup */
 
-		  SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x31,0x08);
-	          SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x32,0x10);
-	          SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x33,0x4d);
-		  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,0x6b);
-		  }
-		  SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x34,0x10);
+  tempax = SiS_Pr->SiS_LCDHDES;
 
-	       } else {
+  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;
+     }
+  }
 
-	          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) {   /* @@@@ really == ? */
-	             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);
-	          }
-	       }
+  tempcx = SiS_Pr->SiS_HT;    				  /* Horiz. Total */
 
-	       if(SiS_Pr->SiS_CustomT != CUT_COMPAQ1280) {
-	          SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 2);
-	       }
+  tempbx = SiS_Pr->SiS_HDE;                               /* Horiz. Display End */
 
-	       SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x1f,0x10);  /* 1.10.8r */
+  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_CustomT == CUT_COMPAQ1280) {
+  if(!(SiS_Pr->SiS_LCDInfo & LCDPass11)) {
+     if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
+        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;
+	      }
+	   }
+        }
+     }
+  }
+  tempcx = (tempcx - tempbx) >> 2;		 /* HT-HDE / 4 */
 
-	          if( (SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) ||
-	              ((SiS_CRT2IsLCD(SiS_Pr,BaseAddr,HwDeviceExtension))) ) {
-		     SiS_DisplayOn(SiS_Pr);
-		     SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 1);
-		     SiS_WaitVBRetrace(SiS_Pr,HwDeviceExtension);
-		     SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 3);
-	             SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x30,0x40);
-		     if(!(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-		        SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x26,0xfe,0x01);
-	  	     }
-		  }
+  push1 = tempax;
 
-	       } else {
+  tempax += tempbx;
 
-	          SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2e,0x80);
+  if(tempax >= SiS_Pr->SiS_HT) tempax -= SiS_Pr->SiS_HT;
 
-	          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);
-		        }
-                        SiS_WaitVBRetrace(SiS_Pr,HwDeviceExtension);
-		        SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x26,0xfe,0x01);
-	             }
-	          }
+  push2 = tempax;
 
-	          SiS_SetReg1(SiS_Pr->SiS_P3c4,0x06,pushax);
-	          SiS_DisplayOn(SiS_Pr);
-	          SiS_SetReg3(SiS_Pr->SiS_P3c6,0xff);
+  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) &&
+     (SiS_Pr->SiS_CustomT != CUT_PANEL848)) {
+     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(HwInfo->jChipType < SIS_315H) {
+		      if(SiS_Pr->SiS_VBType & VB_SISVB) {
+		         tempcx = 0x0017;  /* A901; sometimes 0x0018; */
+		      } else {
+		         tempcx = 0x0017;
+#ifdef TWNEWPANEL
+			 tempcx = 0x0018;
+#endif
+		      }
+		   } 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;
+	   }
+        }
+     }
+  }
 
-	          if(!(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-	             SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x00,0x7f);
-	          }
+  tempcx += tempax;                              /* lcdhrs  */
+  if(tempcx >= SiS_Pr->SiS_HT) tempcx -= SiS_Pr->SiS_HT;
 
-	       }
+  tempax = tempcx >> 3;                          /* BPLHRS */
+  temp = tempax & 0x00FF;
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x14,temp);		 /* Part1_14h; Panel Link Horizontal Retrace Start  */
 
-	    } if(SiS_Pr->SiS_VBType & VB_NoLCD) {
+  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) &&
+	   (!SiS_Pr->SiS_IF_DEF_FSTN) &&
+	   (SiS_Pr->SiS_CustomT != CUT_BARCO1366) &&
+	   (SiS_Pr->SiS_CustomT != CUT_BARCO1024) &&
+	   (SiS_Pr->SiS_CustomT != CUT_PANEL848)) {
+           if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480) {
+	      temp += 6;
+              if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel800x600) {
+	         temp++;
+	         if(HwInfo->jChipType >= SIS_315H) {
+	            if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1024x768) {
+	               temp += 7;
+		       if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1600x1200) {
+		          temp -= 0x14;
+			  if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1280x768) {
+			     temp -= 10;
+			  }
+		       }
+	            }
+	         }
+	      }
+           }
+        }
+     }
+  }
 
-	       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);
-		     }
-		  }
-	       }
+  temp &= 0x1F;
+  temp |= ((tempcx & 0x0007) << 5);
+#if 0
+  if(SiS_Pr->SiS_IF_DEF_FSTN) temp = 0x20;       /* WRONG? BIOS loads cl, not ah */
+#endif
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x15,temp);    	 /* Part1_15h; Panel Link Horizontal Retrace End/Skew */
 
-  	    }
+  tempbx = push2;
+  tempcx = push1;                                /* lcdhdes  */
 
-	 } else {			/* 315, 330 */
+  temp = (tempcx & 0x0007);                      /* BPLHDESKEW  */
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1A,temp);   	 /* Part1_1Ah; Panel Link Vertical Retrace Start (2:0) */
 
-	    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);
+  tempcx >>= 3;                                  /* BPLHDES */
+  temp = (tempcx & 0x00FF);
+#if 0 /* Not 550 FSTN */
+  if(HwInfo->jChipType >= SIS_315H) {
+     if(ModeNo == 0x5b) temp--; */
+  }
+#endif
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x16,temp);    	 /* Part1_16h; Panel Link Horizontal Display Enable Start  */
 
-	       SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x1E,0x20);                   /* enable CRT2 */
+  if((HwInfo->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 0 /* Not 550 FSTN */
+  if(HwInfo->jChipType >= SIS_315H) {
+     if(ModeNo == 0x5b) temp--;
+  }
+#endif
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x17,temp);   	 /* Part1_17h; Panel Link Horizontal Display Enable End  */
 
-	       temp = SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x2E);
-               if(!(temp & 0x80))
-                  SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2E,0x80);
-            }
+  /* 2. Vertical setup */
 
-	    SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x00,0x1f,0x20);
+  if(HwInfo->jChipType < SIS_315H) {
+     tempcx = SiS_Pr->SiS_VGAVT;
+     tempbx = SiS_Pr->SiS_VGAVDE;
+     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;
 
-	    if(SiS_Is301B(SiS_Pr,BaseAddr)) {
+  } else {
 
-	       temp=SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x2E);
-               if(!(temp & 0x80))
-                  SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2E,0x80);
+     tempcx = SiS_Pr->SiS_VGAVT - SiS_Pr->SiS_VGAVDE;           /* VGAVT-VGAVDE  */
 
-	       tempah = 0xc0;
-	       if(SiS_IsDualEdge(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-	          tempah = 0x80;
-	          if(!(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-	             tempah = 0x40;
-                  }
-	       }
-               SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x1F,tempah);
+  }
 
-	       SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x00,0x7f);
+  tempbx = SiS_Pr->SiS_LCDVDES;	   		 	 	/* VGAVDES  */
+  push1 = tempbx;
 
-	    } else {
+  tempax = SiS_Pr->SiS_VGAVDE;
 
-	       SiS_VBLongWait(SiS_Pr);
-               SiS_DisplayOn(SiS_Pr);
-	       SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x00,0x7F);
-               SiS_VBLongWait(SiS_Pr);
+  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;
+     }
+  }
 
-	    }
+  tempbx += tempax;
+  if(tempbx >= SiS_Pr->SiS_VT) tempbx -= SiS_Pr->SiS_VT;
 
-	 }   /* 315, 330 */
+  push2 = tempbx;
 
-#endif /* SIS315H */
+  tempcx >>= 1;
 
-      }
+  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) &&
+     (SiS_Pr->SiS_CustomT != CUT_PANEL848)) {
+     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(HwInfo->jChipType < SIS_315H) {
+		      if(SiS_Pr->SiS_VBType & VB_SISVB) {
+		         tempcx = 0x0002;   /* A901; sometimes 0x0003; */
+		      } else {
+			 tempcx = 0x0002;
+#ifdef TWNEWPANEL
+			 tempcx = 0x0003;
+#endif
+		      }
+		   } else tempcx = 0x0003;
+           }
+     	   else if(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 if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) tempcx = 0x0001;
+     	   else 							 tempcx = 0x0057;
+	}
+     }
+  }
 
-    } else {	/* ============  For 301 ================ */
+  tempbx += tempcx;			 	/* BPLVRS  */
 
-       if(HwDeviceExtension->jChipType < SIS_315H) {
-            if(SiS_CRT2IsLCD(SiS_Pr,BaseAddr,HwDeviceExtension)) {
-                SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x11,0xFB);
-	        SiS_SetPanelDelay(SiS_Pr, ROMAddr, HwDeviceExtension, 0);
-	    }
-       }
+  if((HwInfo->jChipType < SIS_315H) ||
+     (SiS_Pr->SiS_IF_DEF_FSTN) ||
+     (SiS_Pr->SiS_IF_DEF_DSTN)) {
+     tempbx++;
+  }
 
-       temp = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x32) & 0xDF;          /* lock mode */
-       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);
+  if(tempbx >= SiS_Pr->SiS_VT) tempbx -= SiS_Pr->SiS_VT;
 
-       SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x1E,0x20);                  /* enable CRT2 */
+  temp = tempbx & 0x00FF;
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x18,temp);       	 /* Part1_18h; Panel Link Vertical Retrace Start  */
 
-       if(HwDeviceExtension->jChipType >= SIS_315H) {
-         temp = SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x2E);
-         if(!(temp & 0x80))
-           SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2E,0x80);         /* BVBDOENABLE=1 */
-       }
+  tempcx >>= 3;
 
-       SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x00,0x1F,0x20);     /* enable VB processor */
+  if((!(SiS_Pr->SiS_LCDInfo & LCDPass11)) &&
+     (SiS_Pr->SiS_CustomT != CUT_BARCO1366) &&
+     (SiS_Pr->SiS_CustomT != CUT_BARCO1024) &&
+     (SiS_Pr->SiS_CustomT != CUT_PANEL848)) {
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
+        if( (HwInfo->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_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)  {
+     		if(HwInfo->jChipType < SIS_315H) {
+		   if(SiS_Pr->SiS_VBType & VB_SISVB) {
+		      tempcx = 0x0004;   /* A901; Other BIOS sets 0x0005; */
+		   } else {
+		      tempcx = 0x0004;
+#ifdef TWNEWPANEL
+		      tempcx = 0x0005;
+#endif
+		   }
+		} else {
+		   tempcx = 0x0005;
+		}
+        }
+     }
+  }
 
-       SiS_VBLongWait(SiS_Pr);
-       SiS_DisplayOn(SiS_Pr);
-       if(HwDeviceExtension->jChipType >= SIS_315H) {
-           SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x00,0x7f);
-       }
-       SiS_VBLongWait(SiS_Pr);
+  tempcx = tempcx + tempbx + 1;                  /* BPLVRE  */
+  temp = tempcx & 0x000F;
+  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) ||
+     (SiS_Pr->SiS_CustomT == CUT_PANEL848)) {
+     temp |= 0x30;
+  }
+  SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x19,0xf0,temp); /* Part1_19h; Panel Link Vertical Retrace End (3:0); Misc.  */
 
-       if(HwDeviceExtension->jChipType < SIS_315H) {
-            if(SiS_CRT2IsLCD(SiS_Pr,BaseAddr,HwDeviceExtension)) {
-	        SiS_SetPanelDelay(SiS_Pr, ROMAddr, HwDeviceExtension, 1);
-                SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x11,0xF7);
-	    }
-       }
+  temp = ((tempbx & 0x0700) >> 8) << 3;          /* BPLDESKEW =0 */
+  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(HwInfo->jChipType >= SIS_315H) {
+        if(SiS_GetReg(SiS_Pr->SiS_Part1Port,0x00) & 0x01) {
+           temp |= 0x80;
+        }
+     } else {
+	if( (HwInfo->jChipType == SIS_630) ||
+	    (HwInfo->jChipType == SIS_730) ) {
+	   if(HwInfo->jChipRevision >= 0x30) {
+	      temp |= 0x80;
+	   }
+	}
+     }
+  }
+  SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x1A,0x87,temp);  /* Part1_1Ah; Panel Link Control Signal (7:3); Vertical Retrace Start (2:0) */
 
-    }
+  if (HwInfo->jChipType < SIS_315H) {
 
-  } else {   /* =================== For LVDS ================== */
+#ifdef SIS300      /* 300 series */
 
-    if(HwDeviceExtension->jChipType < SIS_315H) {
+        tempeax = SiS_Pr->SiS_VGAVDE << 6;
+        temp = (USHORT)(tempeax % (ULONG)SiS_Pr->SiS_VDE);
+        tempeax = tempeax / (ULONG)SiS_Pr->SiS_VDE;
+        if(temp != 0) tempeax++;
+        tempebx = tempeax;                         /* BPLVCFACT  */
 
-#ifdef SIS300    /* 300 series */
+  	if(SiS_Pr->SiS_SetFlag & EnableLVDSDDA) {
+	   tempebx = 0x003F;
+	}
 
-      if(SiS_CRT2IsLCD(SiS_Pr,BaseAddr,HwDeviceExtension)) {
-         if(HwDeviceExtension->jChipType == SIS_730) {
-	    SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 1);
-	    SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 1);
-	    SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 1);
-	 }
-         SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x11,0xFB);
-	 if(!(SiS_CR36BIOSWord23d(SiS_Pr,HwDeviceExtension))) {
-	    SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 0);
-	 }
-      }
+  	temp = (USHORT)(tempebx & 0x00FF);
+  	SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1E,temp);      /* Part1_1Eh; Panel Link Vertical Scaling Factor */
 
-      SiS_EnableCRT2(SiS_Pr);
-      SiS_DisplayOn(SiS_Pr);
-      SiS_UnLockCRT2(SiS_Pr,HwDeviceExtension, BaseAddr);
-      SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x02,0xBF);
-      if(SiS_BridgeInSlave(SiS_Pr)) {
-      	 SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x01,0x1F);
-      } else {
-      	 SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x01,0x1F,0x40);
-      }
+#endif /* SIS300 */
 
-      if(SiS_Pr->SiS_IF_DEF_CH70xx == 1) {
-         if(!(SiS_CRT2IsLCD(SiS_Pr,BaseAddr,HwDeviceExtension))) {
-	    SiS_WaitVBRetrace(SiS_Pr,HwDeviceExtension);
-	    SiS_SetCH700x(SiS_Pr,0x0B0E);
-         }
-      }
+  } else {
 
-      if(SiS_CRT2IsLCD(SiS_Pr,BaseAddr,HwDeviceExtension)) {
-         if(!(SiS_GetReg1(SiS_Pr->SiS_P3c4,0x13) & 0x40)) {
-            if(!(SiS_GetReg1(SiS_Pr->SiS_P3c4,0x16) & 0x10)) {
-	       if(!(SiS_CR36BIOSWord23b(SiS_Pr,HwDeviceExtension))) {
-		  SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 1);
-        	  SiS_SetPanelDelay(SiS_Pr,ROMAddr, HwDeviceExtension, 1);
-	       }
-	       SiS_WaitVBRetrace(SiS_Pr,HwDeviceExtension);
-               SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x11,0xF7);
-            }
-	 }
-      }
+#ifdef SIS315H  /* 315 series */
 
-#endif  /* SIS300 */
+        if(HwInfo->jChipType == SIS_740) {
+           SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x1E,0x03);
+        } else {
+	   SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1E,0x23);
+	}
 
-    } else {
+	tempeax = SiS_Pr->SiS_VGAVDE << 18;
+    	temp = (USHORT)(tempeax % (ULONG)SiS_Pr->SiS_VDE);
+    	tempeax = tempeax / SiS_Pr->SiS_VDE;
+    	if(temp != 0) tempeax++;
+    	tempebx = tempeax;                         /* BPLVCFACT  */
+        tempvcfact = tempeax;
+    	temp = (USHORT)(tempebx & 0x00FF);
+    	SiS_SetReg(SiS_Pr->SiS_Part1Port,0x37,temp);      /* Part1_37h; Panel Link Vertical Scaling Factor */
+    	temp = (USHORT)((tempebx & 0x00FF00) >> 8);
+    	SiS_SetReg(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_SetReg(SiS_Pr->SiS_Part1Port,0x35,temp);      /* Part1_35h; Panel Link Vertical Scaling Factor */
 
-#ifdef SIS315H    /* 315 series */
+#endif /* SIS315H */
 
-#if 0  /* BIOS code makes no sense */
-       if(SiS_IsVAMode()) {
-          if(SiS_IsLCDOrLCDA()) {
-	  }
-       }
-#endif
+  }
 
-       if(SiS_Pr->SiS_IF_DEF_CH70xx == 0) {
-	    if(SiS_CRT2IsLCD(SiS_Pr,BaseAddr,HwDeviceExtension)) {
-	     	SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x11,0xFB);
-	        SiS_SetPanelDelay(SiS_Pr, ROMAddr, HwDeviceExtension, 0);
-            }
-       }
+  tempbx = push2;                                  /* BPLVDEE  */
+  tempcx = push1;
 
-       SiS_EnableCRT2(SiS_Pr);
-       SiS_UnLockCRT2(SiS_Pr,HwDeviceExtension, BaseAddr);
+  push1 = temp;
 
-       SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2e,0xf7);
+  if(!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
+   	if(!SiS_Pr->SiS_IF_DEF_FSTN && !SiS_Pr->SiS_IF_DEF_DSTN) {
+		if(HwInfo->jChipType < SIS_315H) {
+			if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x600) {
+      				if(resinfo == SIS_RI_1024x600) tempcx++;
+				if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
+					if(resinfo == SIS_RI_800x600) tempcx++;
+		    		}
+			} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600) {
+      				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 == SIS_RI_800x600)  tempcx++;
+			}
+		}
+	}
+  }
 
-       if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
-          temp = SiS_GetCH701x(SiS_Pr,0x66);
-	  temp &= 0x20;
-	  SiS_Chrontel701xBLOff(SiS_Pr);
-       }
+  if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480) {
+     if((!SiS_Pr->SiS_IF_DEF_FSTN) && (!SiS_Pr->SiS_IF_DEF_DSTN)) {
+        tempcx = SiS_Pr->SiS_VGAVDE;
+        tempbx = SiS_Pr->SiS_VGAVDE - 1;
+     }
+  }
 
-       if(HwDeviceExtension->jChipType != SIS_550) {
-          SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x2e,0x7f);
-       }
+  temp = ((tempbx & 0x0700) >> 8) << 3;
+  temp |= ((tempcx & 0x0700) >> 8);
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1D,temp);     	/* Part1_1Dh; Vertical Display Overflow; Control Signal */
 
-       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);
-	     }
-	  }
-       }
+  temp = tempbx & 0x00FF;
+  /* if(SiS_Pr->SiS_IF_DEF_FSTN) temp++;  */
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1C,temp);      	/* Part1_1Ch; Panel Link Vertical Display Enable End  */
 
-       temp1 = SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x2E);
-       if(!(temp1 & 0x80))
-           SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2E,0x80);
+  temp = tempcx & 0x00FF;
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1B,temp);      	/* Part1_1Bh; Panel Link Vertical Display Enable Start  */
 
-       if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
-           if(temp) {
-	       SiS_Chrontel701xBLOn(SiS_Pr, HwDeviceExtension);
-	   }
-       }
+  /* 3. Additional horizontal setup (scaling, etc) */
 
-       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);
-	   }
-       }
+  tempecx = SiS_Pr->SiS_VGAHDE;
+  if(HwInfo->jChipType >= SIS_315H) {
+     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;
+  else {
+     tempeax = tempecx;
+     tempeax <<= 16;
+     temp = (USHORT)(tempeax % tempebx);
+     tempeax = tempeax / tempebx;
+     if(HwInfo->jChipType >= SIS_315H) {
+        if(temp) tempeax++;
+     }
+  }
+  tempecx = tempeax;
 
-       if(!(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-           SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x00,0x7f);
-       }
+  if(HwInfo->jChipType >= SIS_315H) {
+     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;
+  }
+  tempecx <<= 16;
+  tempecx |= (tempeax & 0xFFFF);
+  temp = (USHORT)(tempecx & 0x00FF);
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1F,temp);  	 /* Part1_1Fh; Panel Link DDA Operational Number in each horiz. line */
 
-       if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
+  tempbx = SiS_Pr->SiS_VDE;
+  if(HwInfo->jChipType >= SIS_315H) {
+     tempeax = (SiS_Pr->SiS_VGAVDE << 18) / tempvcfact;
+     tempbx = (USHORT)(tempeax & 0x0FFFF);
+  } else {
+     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) {
+     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;
+  }
 
-       		if(SiS_IsTVOrYPbPrOrScart(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-           		SiS_Chrontel701xOn(SiS_Pr,HwDeviceExtension, BaseAddr);
-         	}
-
-         	if(SiS_IsVAMode(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-           		SiS_ChrontelDoSomething1(SiS_Pr,HwDeviceExtension, BaseAddr);
-         	} else if(SiS_IsLCDOrLCDA(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-           		SiS_ChrontelDoSomething1(SiS_Pr,HwDeviceExtension, BaseAddr);
-        	}
+  temp = ((tempbx & 0xFF00) >> 8) << 3;
+  temp |= (USHORT)((tempecx & 0x0700) >> 8);
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x20,temp);  	/* Part1_20h; Overflow register */
 
-       }
+  temp = tempbx & 0x00FF;
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x21,temp);  	/* Part1_21h; Panel Link Vertical Accumulator Register */
 
-       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, HwDeviceExtension);
-	            		SiS_ChrontelDoSomething4(SiS_Pr,HwDeviceExtension, BaseAddr);
-           		} else if(SiS_IsLCDOrLCDA(SiS_Pr,HwDeviceExtension, BaseAddr))  {
-       				SiS_Chrontel701xBLOn(SiS_Pr, HwDeviceExtension);
-       				SiS_ChrontelDoSomething4(SiS_Pr,HwDeviceExtension, BaseAddr);
-	   		}
-       		}
-       } else if(SiS_Pr->SiS_IF_DEF_CH70xx == 0) {
-       		if(!(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr))) {
-			if(SiS_CRT2IsLCD(SiS_Pr,BaseAddr,HwDeviceExtension)) {
-				SiS_SetPanelDelay(SiS_Pr, ROMAddr, HwDeviceExtension, 1);
-				SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x11,0xF7);
-			}
-		}
-       }
+  tempecx >>= 16;                               /* BPLHCFACT  */
+  if((HwInfo->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_SetReg(SiS_Pr->SiS_Part1Port,0x22,temp);     	/* Part1_22h; Panel Link Horizontal Scaling Factor High */
 
-#endif  /* SIS315H */
+  temp = (USHORT)(tempecx & 0x00FF);
+  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x23,temp);         /* Part1_22h; Panel Link Horizontal Scaling Factor Low */
 
-    } /* 310 series */
+  /* 630/301B and 630/LVDS do something for 640x480 panels here */
 
-  }  /* LVDS */
+#ifdef SIS315H
+  if(SiS_Pr->SiS_IF_DEF_FSTN || SiS_Pr->SiS_IF_DEF_DSTN) {
+     SiS_SetReg(SiS_Pr->SiS_Part1Port,0x25,0x00);
+     SiS_SetReg(SiS_Pr->SiS_Part1Port,0x26,0x00);
+     SiS_SetReg(SiS_Pr->SiS_Part1Port,0x27,0x00);
+     SiS_SetReg(SiS_Pr->SiS_Part1Port,0x28,0x87);
+     SiS_SetReg(SiS_Pr->SiS_Part1Port,0x29,0x5A);
+     SiS_SetReg(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_SetReg(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_SetReg(SiS_Pr->SiS_Part1Port,0x39,temp);
+     SiS_SetReg(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_SetReg(SiS_Pr->SiS_Part1Port,0x3B,temp);
+     temp = ((tempax & 0xFF00) >> 8) << 3;
+     SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x3C,~0x038,temp);
 
-void
-SiS_SiS30xBLOn(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension)
-{
-  USHORT  BaseAddr = (USHORT)HwDeviceExtension->ulIOAddress;
+     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_SetReg(SiS_Pr->SiS_Part1Port,0x3F,0x00);         	/* BDxWadrst0 */
+     SiS_SetReg(SiS_Pr->SiS_Part1Port,0x3E,0x00);
+     SiS_SetReg(SiS_Pr->SiS_Part1Port,0x3D,0x10);
+     SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x3C,~0x040,0x00);
 
-  /* 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)) {
-	SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x26,0x02);
-	SiS_WaitVBRetrace(SiS_Pr,HwDeviceExtension);
-    }
-    if(!(SiS_GetReg1(SiS_Pr->SiS_Part4Port,0x26) & 0x01)) {
-        SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x26,0x01);
-    }
-  }
-}
+     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_SetReg(SiS_Pr->SiS_Part1Port,0x43,temp);
+     temp = ((tempax & 0xFF00) >> 8) << 3;
+     SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x44,~0x0F8,temp);
 
-void
-SiS_SiS30xBLOff(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension)
-{
-  USHORT  BaseAddr = (USHORT)HwDeviceExtension->ulIOAddress;
+     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_SetReg(SiS_Pr->SiS_Part1Port,0x42,temp);
+     temp = (USHORT)((tempebx & 0x0000FF00) >> 8);
+     SiS_SetReg(SiS_Pr->SiS_Part1Port,0x41,temp);
+     temp = (USHORT)((tempebx & 0x00FF0000) >> 16);
+     SiS_SetReg(SiS_Pr->SiS_Part1Port,0x40,temp);
+     temp = (USHORT)(((tempebx & 0x01000000) >> 24) << 7);
+     SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x3C,~0x080,temp);
 
-  /* 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);
-  }
+     SiS_SetReg(SiS_Pr->SiS_Part1Port,0x2F,0x03);
+     SiS_SetReg(SiS_Pr->SiS_Part1Port,0x03,0x50);
+     SiS_SetReg(SiS_Pr->SiS_Part1Port,0x04,0x00);
+     SiS_SetReg(SiS_Pr->SiS_Part1Port,0x2F,0x01);
+     SiS_SetReg(SiS_Pr->SiS_Part1Port,0x19,0x38);
 
-  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);
-          }
-      }
+     if(SiS_Pr->SiS_IF_DEF_FSTN) {
+        SiS_SetReg(SiS_Pr->SiS_Part1Port,0x2b,0x02);
+        SiS_SetReg(SiS_Pr->SiS_Part1Port,0x2c,0x00);
+        SiS_SetReg(SiS_Pr->SiS_Part1Port,0x2d,0x00);
+        SiS_SetReg(SiS_Pr->SiS_Part1Port,0x35,0x0c);
+        SiS_SetReg(SiS_Pr->SiS_Part1Port,0x36,0x00);
+        SiS_SetReg(SiS_Pr->SiS_Part1Port,0x37,0x00);
+        SiS_SetReg(SiS_Pr->SiS_Part1Port,0x38,0x80);
+        SiS_SetReg(SiS_Pr->SiS_Part1Port,0x39,0xA0);
+        SiS_SetReg(SiS_Pr->SiS_Part1Port,0x3a,0x00);
+        SiS_SetReg(SiS_Pr->SiS_Part1Port,0x3b,0xf0);
+        SiS_SetReg(SiS_Pr->SiS_Part1Port,0x3c,0x00);
+        SiS_SetReg(SiS_Pr->SiS_Part1Port,0x3d,0x10);
+        SiS_SetReg(SiS_Pr->SiS_Part1Port,0x3e,0x00);
+        SiS_SetReg(SiS_Pr->SiS_Part1Port,0x3f,0x00);
+        SiS_SetReg(SiS_Pr->SiS_Part1Port,0x40,0x10);
+        SiS_SetReg(SiS_Pr->SiS_Part1Port,0x41,0x25);
+        SiS_SetReg(SiS_Pr->SiS_Part1Port,0x42,0x80);
+        SiS_SetReg(SiS_Pr->SiS_Part1Port,0x43,0x14);
+        SiS_SetReg(SiS_Pr->SiS_Part1Port,0x44,0x03);
+        SiS_SetReg(SiS_Pr->SiS_Part1Port,0x45,0x0a);
+     }
   }
-}
-
-BOOLEAN
-SiS_CR36BIOSWord23b(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension)
-{
-  USHORT temp,temp1;
-  UCHAR *ROMAddr;
+#endif  /* SIS315H */
 
-  if((ROMAddr = (UCHAR *)HwDeviceExtension->pjVirtualRomBase) && SiS_Pr->SiS_UseROM) {
-     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);
-  }
 }
 
-BOOLEAN
-SiS_CR36BIOSWord23d(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension)
+/************** Set Part 1 ***************/
+static void
+SiS_SetGroup1(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,
+              PSIS_HW_INFO HwInfo, USHORT RefreshRateTableIndex)
 {
-  USHORT temp,temp1;
-  UCHAR *ROMAddr;
+  UCHAR  *ROMAddr  = HwInfo->pjVirtualRomBase;
+  USHORT  temp=0, tempax=0, tempbx=0, tempcx=0;
+  USHORT  pushbx=0, CRT1Index=0;
+#ifdef SIS315H
+  USHORT  tempbl=0;
+#endif
+  USHORT  modeflag, resinfo=0;
 
-  if((ROMAddr = (UCHAR *)HwDeviceExtension->pjVirtualRomBase) && SiS_Pr->SiS_UseROM) {
-     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);
+  if(ModeNo <= 0x13) {
+     modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
   } else {
-     return(0);
+     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;
+     }
   }
-}
 
-void
-SiS_SetPanelDelayLoop(SiS_Private *SiS_Pr, UCHAR *ROMAddr, PSIS_HW_DEVICE_INFO HwDeviceExtension,
-                      USHORT DelayTime, USHORT DelayLoop)
-{
-   int i;
-   for(i=0; i<DelayLoop; i++) {
-      SiS_SetPanelDelay(SiS_Pr, ROMAddr, HwDeviceExtension, DelayTime);
-   }
-}		     
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
 
-void
-SiS_SetPanelDelay(SiS_Private *SiS_Pr, UCHAR *ROMAddr, PSIS_HW_DEVICE_INFO HwDeviceExtension,
-                  USHORT DelayTime)
-{
-  USHORT PanelID, DelayIndex, Delay;
-#ifdef SIS300
-  USHORT temp;
+#ifdef SIS315H
+     SiS_SetCRT2Sync(SiS_Pr, ModeNo, RefreshRateTableIndex, HwInfo);
+     SiS_SetGroup1_LCDA(SiS_Pr, ModeNo, ModeIdIndex, HwInfo, RefreshRateTableIndex);
 #endif
 
-  if(HwDeviceExtension->jChipType < SIS_315H) {
-
-#ifdef SIS300
-
-      if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {			/* 300 series, LVDS */
-
-	  PanelID = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x36);
-
-	  DelayIndex = PanelID >> 4;
-
-	  if((DelayTime >= 2) && ((PanelID & 0x0f) == 1))  {
-              Delay = 3;
-          } else {
-              if(DelayTime >= 2) DelayTime -= 2;
-
-              if(!(DelayTime & 0x01)) {
-       		  Delay = SiS_Pr->SiS_PanelDelayTbl[DelayIndex].timer[0];
-              } else {
-       		  Delay = SiS_Pr->SiS_PanelDelayTbl[DelayIndex].timer[1];
-              }
-	      if((ROMAddr) && (SiS_Pr->SiS_UseROM)) {
-                  if(ROMAddr[0x220] & 0x40) {
-                      if(!(DelayTime & 0x01)) {
-	    	          Delay = (USHORT)ROMAddr[0x225];
-                      } else {
-	    	          Delay = (USHORT)ROMAddr[0x226];
-                      }
-                  }
-              }
-          }
-	  SiS_ShortDelay(SiS_Pr,Delay);
-
-      } else {							/* 300 series, 301(B) */
-
-	  PanelID = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x36);
-	  temp = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x18);
-          if(!(temp & 0x10))  PanelID = 0x12;
-
-          DelayIndex = PanelID >> 4;
-
-	  if((DelayTime >= 2) && ((PanelID & 0x0f) == 1))  {
-              Delay = 3;
-          } else {
-              if(DelayTime >= 2) DelayTime -= 2;
-
-              if(!(DelayTime & 0x01)) {
-       		  Delay = SiS_Pr->SiS_PanelDelayTbl[DelayIndex].timer[0];
-              } else {
-       		  Delay = SiS_Pr->SiS_PanelDelayTbl[DelayIndex].timer[1];
-              }
-	      if((ROMAddr) && (SiS_Pr->SiS_UseROM)) {
-                  if(ROMAddr[0x220] & 0x40) {
-                      if(!(DelayTime & 0x01)) {
-	    	          Delay = (USHORT)ROMAddr[0x225];
-                      } else {
-	    	          Delay = (USHORT)ROMAddr[0x226];
-                      }
-                  }
-              }
-          }
-	  SiS_ShortDelay(SiS_Pr,Delay);
+  } else {
 
-      }
+     if( (HwInfo->jChipType >= SIS_315H) &&
+         (SiS_Pr->SiS_IF_DEF_LVDS == 1) &&
+	 (SiS_Pr->SiS_VBInfo & SetInSlaveMode) ) {
 
-#endif  /* SIS300 */
+        SiS_SetCRT2Sync(SiS_Pr, ModeNo, RefreshRateTableIndex, HwInfo);
 
-   } else {
+     } else {
 
-      if(HwDeviceExtension->jChipType == SIS_330) return;
+        SiS_SetCRT2Offset(SiS_Pr, ModeNo, ModeIdIndex,
+      		          RefreshRateTableIndex, HwInfo);
 
+        if (HwInfo->jChipType < SIS_315H ) {
+#ifdef SIS300
+    	      SiS_SetCRT2FIFO_300(SiS_Pr, ModeNo, HwInfo);
+#endif
+        } else {
 #ifdef SIS315H
+              SiS_SetCRT2FIFO_310(SiS_Pr);
+#endif
+	}
 
-      if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {			/* 315 series, LVDS */
+        SiS_SetCRT2Sync(SiS_Pr, ModeNo, RefreshRateTableIndex, HwInfo);
 
-          if(SiS_Pr->SiS_IF_DEF_CH70xx == 0) {
-              PanelID = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x36);
-	      DelayIndex = PanelID >> 4;
-	      if((DelayTime >= 2) && ((PanelID & 0x0f) == 1))  {
-                 Delay = 3;
-              } else {
-                 if(DelayTime >= 2) DelayTime -= 2;
+	/* 1. Horizontal setup */
 
-                 if(!(DelayTime & 0x01)) {
-       		     Delay = SiS_Pr->SiS_PanelDelayTblLVDS[DelayIndex].timer[0];
-                 } else {
-       		     Delay = SiS_Pr->SiS_PanelDelayTblLVDS[DelayIndex].timer[1];
-                 }
-	         if((ROMAddr) && (SiS_Pr->SiS_UseROM)) {
-                    if(ROMAddr[0x13c] & 0x40) {
-                        if(!(DelayTime & 0x01)) {
-	    	           Delay = (USHORT)ROMAddr[0x17e];
-                        } else {
-	    	           Delay = (USHORT)ROMAddr[0x17f];
-                        }
-                    }
-                 }
-              }
-	      SiS_ShortDelay(SiS_Pr,Delay);
-	  }
+        if(HwInfo->jChipType < SIS_315H ) {
 
-      } else {							/* 315 series, 301(B) */
+#ifdef SIS300   /* ------------- 300 series --------------*/
 
-          PanelID = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x36);
-	  DelayIndex = PanelID >> 4;
-          if(!(DelayTime & 0x01)) {
-       		Delay = SiS_Pr->SiS_PanelDelayTbl[DelayIndex].timer[0];
-          } else {
-       		Delay = SiS_Pr->SiS_PanelDelayTbl[DelayIndex].timer[1];
-          }
-	  SiS_DDC2Delay(SiS_Pr, Delay * 4);
+    		temp = (SiS_Pr->SiS_VGAHT - 1) & 0x0FF;   			/* BTVGA2HT 0x08,0x09 */
+    		SiS_SetReg(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);          /* CRT2 Horizontal Total Overflow [7:4] */
 
-#endif /* SIS315H */
+    		temp = (SiS_Pr->SiS_VGAHDE + 12) & 0x0FF;                       /* BTVGA2HDEE 0x0A,0x0C */
+    		SiS_SetReg(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;
+    		tempbx = pushbx + tempcx;
+    		tempcx <<= 1;
+    		tempcx += tempbx;
 
-}
+    		if(SiS_Pr->SiS_VBType & VB_SISVB) {
 
-void
-SiS_LongDelay(SiS_Private *SiS_Pr, USHORT delay)
-{
-  while(delay--) {
-    SiS_GenericDelay(SiS_Pr,0x19df);
-  }
-}
+		   if(SiS_Pr->UseCustomMode) {
+		      tempbx = SiS_Pr->CHSyncStart + 12;
+		      tempcx = SiS_Pr->CHSyncEnd + 12;
+		   }
 
-void
-SiS_ShortDelay(SiS_Private *SiS_Pr, USHORT delay)
-{
-  while(delay--) {
-      SiS_GenericDelay(SiS_Pr,0x42);
-  }
-}
+      		   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;
+      		   }
 
-void
-SiS_GenericDelay(SiS_Private *SiS_Pr, USHORT delay)
-{
-  USHORT temp,flag;
+    		   if((SiS_Pr->SiS_VBInfo & SetCRT2ToTV) && (resinfo == SIS_RI_1024x768)){
+        	      if(!(SiS_Pr->SiS_TVMode & TVSetPAL)){
+      			 tempbx = 1040;
+      			 tempcx = 1042;
+      		      }
+    		   }
+	        }
 
-  flag = SiS_GetReg3(0x61) & 0x10;
+    		temp = tempbx & 0x00FF;
+    		SiS_SetReg(SiS_Pr->SiS_Part1Port,0x0B,temp);                   /* CRT2 Horizontal Retrace Start */
+#endif /* SIS300 */
 
-  while(delay) {
-      temp = SiS_GetReg3(0x61) & 0x10;
-      if(temp == flag) continue;
-      flag = temp;
-      delay--;
-  }
-}
+ 	} else {
 
-BOOLEAN
-SiS_Is301B(SiS_Private *SiS_Pr, USHORT BaseAddr)
-{
-  USHORT flag;
+#ifdef SIS315H  /* ------------------- 315/330 series --------------- */
 
-  flag = SiS_GetReg1(SiS_Pr->SiS_Part4Port,0x01);
-  if(flag >= 0x0B0) return(1);
-  else return(0);
-}
+	        tempcx = SiS_Pr->SiS_VGAHT;				       /* BTVGA2HT 0x08,0x09 */
+		if(modeflag & HalfDCLK) {
+		    if(SiS_Pr->SiS_VBType & VB_SISVB) {
+		       tempcx >>= 1;
+		    } else {
+		       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;
+		       }
+		    }
+		}
+		tempcx--;
 
-BOOLEAN
-SiS_CRT2IsLCD(SiS_Private *SiS_Pr, USHORT BaseAddr, PSIS_HW_DEVICE_INFO HwDeviceExtension)
-{
-  USHORT flag;
+		temp = tempcx & 0xff;
+		SiS_SetReg(SiS_Pr->SiS_Part1Port,0x08,temp);                  /* CRT2 Horizontal Total */
 
-  if(HwDeviceExtension->jChipType == SIS_730) {
-     flag = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x13);
-     if(flag & 0x20) return(1);
-  }
-  flag = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x30);
-  if(flag & 0x20) return(1);
-  else return(0);
-}
+		temp = ((tempcx & 0xff00) >> 8) << 4;
+		SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x09,0x0F,temp);         /* CRT2 Horizontal Total Overflow [7:4] */
 
-BOOLEAN
-SiS_IsDualEdge(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension, USHORT BaseAddr)
-{
-#ifdef SIS315H
-  USHORT flag;
+		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 += 16;
 
-  if(HwDeviceExtension->jChipType >= SIS_315H) {
-     if((HwDeviceExtension->jChipType != SIS_650) || (SiS_GetReg1(SiS_Pr->SiS_P3d4,0x5f) & 0xf0)) {
-        flag = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x38);
-        if(flag & EnableDualEdge) return(1);
-        else return(0);
-     } else return(0);
-  } else
-#endif
-     return(0);
-}
+		temp = tempbx & 0xff;
+		SiS_SetReg(SiS_Pr->SiS_Part1Port,0x0A,temp);                  /* CRT2 Horizontal Display Enable End */
 
-BOOLEAN
-SiS_IsVAMode(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension, USHORT BaseAddr)
-{
-#ifdef SIS315H
-  USHORT flag;
+		pushbx = tempbx;
+		tempcx >>= 1;
+		tempbx += tempcx;
+		tempcx += tempbx;
 
-  if(HwDeviceExtension->jChipType >= SIS_315H) {
-     flag = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x38);
-     if((flag & EnableDualEdge) && (flag & SetToLCDA))   return(1);
-     else return(0);
-  } else
-#endif
-     return(0);
-}
+		if(SiS_Pr->SiS_VBType & VB_SISVB) {
 
-BOOLEAN
-SiS_IsDualLink(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension, USHORT BaseAddr)
-{
-#ifdef SIS315H
-  if(HwDeviceExtension->jChipType >= SIS_315H) {
-     if((SiS_CRT2IsLCD(SiS_Pr, BaseAddr, HwDeviceExtension)) ||
-        (SiS_IsVAMode(SiS_Pr, HwDeviceExtension, BaseAddr))) {
-        if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x39) & 0x04) return(1);
-     }
-  }
-#endif
-     return(0);
- }
+		   if(HwInfo->jChipType >= SIS_661) {
+		      if((SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) ||
+		         (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024)) {
+			 if(resinfo == SIS_RI_1280x1024) {
+		            tempcx = 0x30;
+			 } else if(resinfo == SIS_RI_1600x1200) {
+			    tempcx = 0xff;
+			 }
+		      }
+		   }
 
-BOOLEAN
-SiS_WeHaveBacklightCtrl(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension, USHORT BaseAddr)
-{
-#ifdef SIS315H
-  USHORT flag;
+		   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(HwDeviceExtension->jChipType >= SIS_315H) {
-     flag = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x79);
-     if(flag & 0x10)  return(1);
-     else             return(0);
-  } else
-#endif
-     return(0);
- }
+             	   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)) - 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 0
-BOOLEAN
-SiS_Is315E(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension, USHORT BaseAddr)
-{
-#ifdef SIS315H
-  USHORT flag;
+		   if(SiS_Pr->SiS_TVMode & TVSetNTSC1024) {
+      		      tempbx = 1040;
+      		      tempcx = 1042;
+      	     	   }
 
-  if(HwDeviceExtension->jChipType >= SIS_315H) {
-     flag = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x5f);
-     if(flag & 0x10)  return(1);
-     else      	      return(0);
-  } else
-#endif
-     return(0);
-}
-#endif
+                }
 
-BOOLEAN
-SiS_IsNotM650or651(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension, USHORT BaseAddr)
-{
-#ifdef SIS315H
-  USHORT flag;
+		temp = tempbx & 0xff;
+	 	SiS_SetReg(SiS_Pr->SiS_Part1Port,0x0B,temp);                 /* CRT2 Horizontal Retrace Start */
+#endif  /* SIS315H */
 
-  if(HwDeviceExtension->jChipType == SIS_650) {
-     flag = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x5f);
-     flag &= 0xF0;
-     /* Check for revision != A0 only */
-     if((flag == 0xe0) || (flag == 0xc0) ||
-        (flag == 0xb0) || (flag == 0x90)) return 0;
-     else return 1;
-  } else
-#endif
-    return 1;
-}
+     	}  /* 315/330 series */
 
-BOOLEAN
-SiS_IsYPbPr(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension, USHORT BaseAddr)
-{
-#ifdef SIS315H
-  USHORT flag;
+  	/* The following is done for all bridge/chip types/series */
 
-  if(HwDeviceExtension->jChipType >= SIS_315H) {
-     flag = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x38);
-     if(flag & EnableLVDSHiVision)  return(1);  /* = YPrPb = 0x08 */
-     else      	                    return(0);
-  } else
-#endif
-     return(0);
-}
+  	tempax = tempbx & 0xFF00;
+  	tempbx = pushbx;
+  	tempbx = (tempbx & 0x00FF) | ((tempbx & 0xFF00) << 4);
+  	tempax |= (tempbx & 0xFF00);
+  	temp = (tempax & 0xFF00) >> 8;
+  	SiS_SetReg(SiS_Pr->SiS_Part1Port,0x0C,temp);                        /* Overflow */
 
-BOOLEAN
-SiS_IsChScart(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension, USHORT BaseAddr)
-{
-#ifdef SIS315H
-  USHORT flag;
+  	temp = tempcx & 0x00FF;
+  	SiS_SetReg(SiS_Pr->SiS_Part1Port,0x0D,temp);                        /* CRT2 Horizontal Retrace End */
 
-  if(HwDeviceExtension->jChipType >= SIS_315H) {
-     flag = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x38);
-     if(flag & EnableLVDSScart)     return(1);  /* = Scart = 0x04 */
-     else      	                    return(0);
-  } else
-#endif
-     return(0);
-}
+  	/* 2. Vertical setup */
 
-BOOLEAN
-SiS_IsTVOrYPbPrOrScart(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension, USHORT BaseAddr)
-{
-  USHORT flag;
+  	tempcx = SiS_Pr->SiS_VGAVT - 1;
+  	temp = tempcx & 0x00FF;
 
-#ifdef SIS315H
-  if(HwDeviceExtension->jChipType >= SIS_315H) {
-     flag = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x30);
-     if(flag & SetCRT2ToTV)        return(1);
-     flag = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x38);
-     if(flag & EnableLVDSHiVision) return(1);  /* = YPrPb = 0x08 */
-     if(flag & EnableLVDSScart)    return(1);  /* = Scart = 0x04- TW inserted */
-     else                          return(0);
-  } else
-#endif
-  {
-     flag = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x30);
-     if(flag & SetCRT2ToTV) return(1);
-  }
-  return(0);
-}
+	if(HwInfo->jChipType < SIS_661) {
+           if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
+	      if(HwInfo->jChipType < SIS_315H) {
+	         if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
+	            if(SiS_Pr->SiS_VBInfo & (SetCRT2ToSVIDEO | SetCRT2ToAVIDEO)) {
+	               temp--;
+	            }
+                 }
+	      } else {
+ 	         temp--;
+              }
+           } else if(HwInfo->jChipType >= SIS_315H) {
+	      temp--;
+	   }
+	}
+  	SiS_SetReg(SiS_Pr->SiS_Part1Port,0x0E,temp);                        /* CRT2 Vertical Total */
 
-BOOLEAN
-SiS_IsLCDOrLCDA(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension, USHORT BaseAddr)
-{
-  USHORT flag;
+  	tempbx = SiS_Pr->SiS_VGAVDE - 1;
+  	temp = tempbx & 0x00FF;
+  	SiS_SetReg(SiS_Pr->SiS_Part1Port,0x0F,temp);                        /* CRT2 Vertical Display Enable End */
 
-#ifdef SIS315H
-  if(HwDeviceExtension->jChipType >= SIS_315H) {
-     flag = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x30);
-     if(flag & SetCRT2ToLCD) return(1);
-     flag = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x38);
-     if(flag & SetToLCDA)    return(1);
-     else                    return(0);
-  } else
-#endif
-  {
-   flag = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x30);
-   if(flag & SetCRT2ToLCD)   return(1);
-  }
-  return(0);
+  	temp = ((tempbx & 0xFF00) << 3) >> 8;
+  	temp |= ((tempcx & 0xFF00) >> 8);
+  	SiS_SetReg(SiS_Pr->SiS_Part1Port,0x12,temp);                        /* Overflow (and HWCursor Test Mode) */
 
-}
+	if((HwInfo->jChipType >= SIS_315H) && (HwInfo->jChipType < SIS_661)) {
+           tempbx++;
+   	   tempax = tempbx;
+	   tempcx++;
+	   tempcx -= tempax;
+	   tempcx >>= 2;
+	   tempbx += tempcx;
+	   if(tempcx < 4) tempcx = 4;
+	   tempcx >>= 2;
+	   tempcx += tempbx;
+	   tempcx++;
+	} else {
+  	   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        */
+	}
 
-BOOLEAN
-SiS_BridgeIsOn(SiS_Private *SiS_Pr, USHORT BaseAddr,PSIS_HW_DEVICE_INFO HwDeviceExtension)
-{
-  USHORT flag;
+  	if(SiS_Pr->SiS_VBType & VB_SISVB) {
 
-  if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
-     return(0);
-  } else {
-     flag = SiS_GetReg1(SiS_Pr->SiS_Part4Port,0x00);
-     if((flag == 1) || (flag == 2)) return(0);
-     else return(1);
-  }
-}
+	   if(SiS_Pr->UseCustomMode) {
+	      tempbx = SiS_Pr->CVSyncStart;
+	      tempcx = (tempcx & 0xFF00) | (SiS_Pr->CVSyncEnd & 0x00FF);
+	   }
 
-BOOLEAN
-SiS_BridgeIsEnable(SiS_Private *SiS_Pr, USHORT BaseAddr,PSIS_HW_DEVICE_INFO HwDeviceExtension)
-{
-  USHORT flag;
+    	   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_SetReg(SiS_Pr->SiS_Part1Port,0x10,temp);           /* CRT2 Vertical Retrace Start */
 
-  if(!(SiS_BridgeIsOn(SiS_Pr,BaseAddr,HwDeviceExtension))) {
-    flag = SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x00);
-    if(HwDeviceExtension->jChipType < SIS_315H) {
-      /* 300 series (630/301B 2.04.5a) */
-      flag &= 0xa0;
-      if((flag == 0x80) || (flag == 0x20)) return 0;
-      else	                           return 1;
-    } else {
-      /* 315 series (650/30xLV 1.10.6s) */
-      flag &= 0x50;
-      if((flag == 0x40) || (flag == 0x10)) return 0;
-      else                                 return 1;
-    }
-  }
-  return 1;
-}
+  	temp = ((tempbx & 0xFF00) >> 8) << 4;
+  	temp |= (tempcx & 0x000F);
+  	SiS_SetReg(SiS_Pr->SiS_Part1Port,0x11,temp);           /* CRT2 Vert. Retrace End; Overflow; "Enable CRTC Check" */
 
-BOOLEAN
-SiS_BridgeInSlave(SiS_Private *SiS_Pr)
-{
-  USHORT flag1;
+  	/* 3. Panel compensation delay */
 
-  flag1 = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x31);
-  if(flag1 & (SetInSlaveMode >> 8)) return 1;
-  else return 0;
-}
+  	if(HwInfo->jChipType < SIS_315H) {
 
-void
-SiS_SetHiVision(SiS_Private *SiS_Pr, USHORT BaseAddr,PSIS_HW_DEVICE_INFO HwDeviceExtension)
-{
-#ifdef SIS315H
-  USHORT temp;
-#endif
+#ifdef SIS300  /* ---------- 300 series -------------- */
 
-  /* Note: This variable is only used on 30xLV systems.
-     CR38 has a different meaning on LVDS/CH7019 systems.
-   */
+	   if(SiS_Pr->SiS_VBType & VB_SISVB) {
+	        temp = 0x20;
 
-  SiS_Pr->SiS_HiVision = 0;
-  if(HwDeviceExtension->jChipType >= SIS_315H) {
-#ifdef SIS315H
-     if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
-        if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) {
-           temp = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x38);
-	   temp &= 0x38;
-	   SiS_Pr->SiS_HiVision = (temp >> 3);
-	}
-     }
-#endif /* SIS315H */
-  }
-}
+		if(HwInfo->jChipType == SIS_300) {
+		   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_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 & SetCRT2ToHiVision) {
+      		   if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) 	temp = 0x2c;
+      		   else 					temp = 0x20;
+    	        }
+		if((ROMAddr) && (SiS_Pr->SiS_UseROM)) {
+		    if(ROMAddr[0x220] & 0x80) {
+		        if(SiS_Pr->SiS_VBInfo & SetCRT2ToTVNoYPbPrHiVision)
+				temp = ROMAddr[0x221];
+			else if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision)
+				temp = ROMAddr[0x222];
+		        else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024)
+				temp = ROMAddr[0x223];
+			else
+				temp = ROMAddr[0x224];
+			temp &= 0x3c;
+		    }
+		}
+		if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
+		   if(SiS_Pr->PDC) {
+			temp = SiS_Pr->PDC & 0x3c;
+		   }
+		}
+	   } else {
+	        temp = 0x20;
+		if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToTV)) {
+		   if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480) temp = 0x04;
+		}
+		if((ROMAddr) && SiS_Pr->SiS_UseROM) {
+		    if(ROMAddr[0x220] & 0x80) {
+		        temp = ROMAddr[0x220] & 0x3c;
+		    }
+		}
+		if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
+		   if(SiS_Pr->PDC) {
+		      temp = SiS_Pr->PDC & 0x3c;
+		   }
+		}
+	   }
 
-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, 10,  0,  0,  0,  0, 15 };
+    	   SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x13,~0x03C,temp);         /* Panel Link Delay Compensation; (Software Command Reset; Power Saving) */
 
-  SiS_Pr->SiS_LCDResInfo = 0;
-  SiS_Pr->SiS_LCDTypeInfo = 0;
-  SiS_Pr->SiS_LCDInfo = 0;
+#endif  /* SIS300 */
 
-  if(SiS_Pr->UseCustomMode) {
-     modeflag = SiS_Pr->CModeFlag;
-  } else {
-     if(ModeNo <= 0x13) {
-    	modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
-     } else {
-    	modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
-    	resinfo = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO;
-     }
-  }
+  	} else {
 
-#ifdef SIS315H
-  if(HwDeviceExtension->jChipType >= SIS_315H) {
-     if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
-        SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x39,~0x04);
-     }
-  }
-#endif
+#ifdef SIS315H   /* --------------- 315/330 series ---------------*/
+
+   	   if(HwInfo->jChipType < SIS_661) {
 
-  if(!(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)))   return;
+	      if(SiS_Pr->SiS_VBType & VB_SISVB) {
 
-  if(!(SiS_Pr->SiS_VBInfo & (SetSimuScanMode | SwitchToCRT2))) return;
+                 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 & SetCRT2ToHiVision) {
+		       if(SiS_Pr->SiS_VBInfo & SetInSlaveMode)  temp = 0x2c;
+      		       else 					temp = 0x20;
+		    }
+		 }
+		 if((SiS_Pr->SiS_VBType & VB_SIS301B302B) && (!(SiS_Pr->SiS_VBType & VB_NoLCD))) {
+		    tempbl = 0x00;
+		    if((ROMAddr) && (SiS_Pr->SiS_UseROM)) {
+		       if(HwInfo->jChipType < SIS_330) {
+		          if(ROMAddr[0x13c] & 0x80) tempbl = 0xf0;
+		       } else {
+		          if(ROMAddr[0x1bc] & 0x80) tempbl = 0xf0;
+		       }
+		    }
+		 } else {  /* LV (550/301LV checks ROM byte, other LV BIOSes do not) */
+		    tempbl = 0xF0;
+		 }
+		 if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD|SetCRT2ToLCDA)) {
+		    if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
+		       if(SiS_Pr->PDC) {
+		          temp = SiS_Pr->PDC;
+		          tempbl = 0;
+		       }
+		    }
+		 }
 
-  temp = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x36);
+	      } else {  /* LVDS */
 
-#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(HwInfo->jChipType == SIS_740) {
+		    temp = 0x03;
+	         } else {
+		    temp = 0x00;
+		 }
+	 	 if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) temp = 0x0a;
+		 tempbl = 0xF0;
+		 if(HwInfo->jChipType == SIS_650) {
+		    if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
+		       if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToTV)) tempbl = 0x0F;
+		    }
+		 }
 
-  if(HwDeviceExtension->jChipType < SIS_315H) {
-  	SiS_Pr->SiS_LCDTypeInfo = temp >> 4;
-  } else {
-        SiS_Pr->SiS_LCDTypeInfo = (temp & 0x0F) - 1;
-  }
-  temp &= 0x0f;
-  if(HwDeviceExtension->jChipType < SIS_315H) {
-      /* Translate 300 series LCDRes to 315 series for unified usage */
-      temp = SiS300SeriesLCDRes[temp];
-  }
-  SiS_Pr->SiS_LCDResInfo = temp;
+		 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;
+		    }
+		 }
+	      }
 
-#if 0
-  if(SiS_Pr->SiS_IF_DEF_FSTN){
-       	SiS_Pr->SiS_LCDResInfo = SiS_Pr->SiS_Panel320x480;
-  }
-#endif
+	      SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2D,tempbl,temp);	    /* Panel Link Delay Compensation */
 
-  if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
-    	if(SiS_Pr->SiS_LCDResInfo < SiS_Pr->SiS_PanelMin301)
-		SiS_Pr->SiS_LCDResInfo = SiS_Pr->SiS_PanelMin301;
-  } else {
-    	if(SiS_Pr->SiS_LCDResInfo < SiS_Pr->SiS_PanelMinLVDS)
-		SiS_Pr->SiS_LCDResInfo = SiS_Pr->SiS_PanelMinLVDS;
-  }
+	   } /* < 661 */
 
-  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;
-  }
+    	   tempax = 0;
+    	   if (modeflag & DoubleScanMode) tempax |= 0x80;
+    	   if (modeflag & HalfDCLK)       tempax |= 0x40;
+    	   SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2C,0x3f,tempax);
 
-  if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
-     if(SiS_Pr->SiS_CustomT == CUT_BARCO1366) {
-        SiS_Pr->SiS_LCDResInfo = Panel_Barco1366;
-     } else if(SiS_Pr->SiS_CustomT == CUT_PANEL848) {
-        SiS_Pr->SiS_LCDResInfo = Panel_848x480;
-     }
-  }
+#endif  /* SIS315H */
 
-  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;
-     case Panel_848x480:   SiS_Pr->PanelXRes =  848; SiS_Pr->PanelYRes =  480; 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){
-        /* Fake LVDS bridge for FSTN */
-      	temp = 0x04;
-      	SiS_SetReg1(SiS_Pr->SiS_P3d4,0x37,temp);
-  }
-#endif
-  SiS_Pr->SiS_LCDInfo = temp;
+     }  /* Slavemode */
 
-  if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
-     if(SiS_Pr->SiS_CustomT == CUT_PANEL848) {
-        SiS_Pr->SiS_LCDInfo = 0x80 | 0x40 | 0x20;   /* neg sync, RGB24 */
-     }
-  }
+     if(SiS_Pr->SiS_VBType & VB_SISVB) {
 
-  if(!(SiS_Pr->UsePanelScaler))        SiS_Pr->SiS_LCDInfo &= ~DontExpandLCD;
-  else if(SiS_Pr->UsePanelScaler == 1) SiS_Pr->SiS_LCDInfo |= DontExpandLCD;
+        /* For 301BDH with LCD, we set up the Panel Link */
+        if( (SiS_Pr->SiS_VBType & VB_NoLCD) && (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) ) {
 
-  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(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(SiS_Pr->SiS_CustomT == CUT_CLEVO1024) {
-           if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
-	      SiS_Pr->SiS_LCDInfo &= ~DontExpandLCD;
-	   }
-	}
-     }
-  }
+	    SiS_SetGroup1_LVDS(SiS_Pr, ModeNo, ModeIdIndex,
+	                       HwInfo, RefreshRateTableIndex);
 
+        } else if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
 
-  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;
-     }
-#endif
-  } else {
-#ifdef SIS300
-     if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
-        if((ROMAddr) && SiS_Pr->SiS_UseROM) {
-	   if((ROMAddr[0x233] == 0x12) && (ROMAddr[0x234] == 0x34)) {
-              if(!(ROMAddr[0x235] & 0x02)) {
-	         SiS_Pr->SiS_LCDInfo &= 0xEF;
- 	      }
-	   }
+    	    SiS_SetGroup1_301(SiS_Pr, ModeNo, ModeIdIndex,
+	                      HwInfo, RefreshRateTableIndex);
         }
-     } else if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-	if((SiS_Pr->SiS_SetFlag & SetDOSMode) && ((ModeNo == 0x03) || (ModeNo == 0x10))) {
-           SiS_Pr->SiS_LCDInfo &= 0xEF;
-	}
-     }
-#endif
-  }
 
-  /* Trumpion: Assume non-expanding */
-  if(SiS_Pr->SiS_IF_DEF_TRUMPION != 0) {
-     SiS_Pr->SiS_LCDInfo &= (~DontExpandLCD);
-  }
+     } else {
 
-  if(!((HwDeviceExtension->jChipType < SIS_315H) && (SiS_Pr->SiS_SetFlag & SetDOSMode))) {
+        if(HwInfo->jChipType < SIS_315H) {
 
-     if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
-        if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x600) {
-	   if(ModeNo > 0x13) {
-	      if(!(SiS_Pr->SiS_LCDInfo & DontExpandLCD)) {
-                 if((resinfo == SIS_RI_800x600) || (resinfo == SIS_RI_400x300)) {
-                    SiS_Pr->SiS_SetFlag |= EnableLVDSDDA;
-		 }
+	   SiS_SetGroup1_LVDS(SiS_Pr, ModeNo, ModeIdIndex,
+	                        HwInfo, RefreshRateTableIndex);
+	} else {
+
+	   if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
+              if((!(SiS_Pr->SiS_VBInfo & SetCRT2ToTV)) || (SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
+    	          SiS_SetGroup1_LVDS(SiS_Pr, ModeNo,ModeIdIndex,
+	                              HwInfo,RefreshRateTableIndex);
               }
-           }
-        }
-	if(ModeNo == 0x12) {
-	   if(SiS_Pr->SiS_LCDInfo & LCDPass11) {
-	      SiS_Pr->SiS_SetFlag |= EnableLVDSDDA;
+	   } else {
+	      SiS_SetGroup1_LVDS(SiS_Pr, ModeNo,ModeIdIndex,
+	                         HwInfo,RefreshRateTableIndex);
 	   }
-	}
-     }
-
-     if(modeflag & HalfDCLK) {
-        if(SiS_Pr->SiS_IF_DEF_TRUMPION == 0) {
-           if(!(SiS_Pr->SiS_LCDInfo & DontExpandLCD)) {
-	      if(!(((SiS_Pr->SiS_IF_DEF_LVDS == 1) || (HwDeviceExtension->jChipType < SIS_315H)) &&
-	                                      (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel640x480))) {
-                 if(ModeNo > 0x13) {
-                    if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
-                       if(resinfo == SIS_RI_512x384) SiS_Pr->SiS_SetFlag |= EnableLVDSDDA;
-                    } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600) {
-                       if(resinfo == SIS_RI_400x300) SiS_Pr->SiS_SetFlag |= EnableLVDSDDA;
-                    }
-                 }
-	      } else SiS_Pr->SiS_SetFlag |= EnableLVDSDDA;
-           } else SiS_Pr->SiS_SetFlag |= EnableLVDSDDA;
-        } else SiS_Pr->SiS_SetFlag |= EnableLVDSDDA;
-     }
-
-  }
-
-  if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
-    	if(SiS_Pr->SiS_VBInfo & SetNotSimuMode) {
-      		SiS_Pr->SiS_SetFlag |= LCDVESATiming;
-    	}
-  } else {
-    	SiS_Pr->SiS_SetFlag |= LCDVESATiming;
-  }
 
-#ifdef SIS315H
-  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)
-	 */
-        if(SiS_Pr->SiS_CustomT == CUT_CLEVO1024) {
-	   if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
-	      /* (Sets this in SenseLCD; new paneltypes) */
-	      SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x39,0x04);
-	   }
-	}
-        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)) {
-	   SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x39,0x04);
 	}
-     }
-  }
-#endif
-
-#ifdef LINUX_KERNEL
-#ifdef TWDEBUG
-  printk(KERN_DEBUG "sisfb: (LCDInfo=0x%04x LCDResInfo=0x%02x LCDTypeInfo=0x%02x)\n",
-	SiS_Pr->SiS_LCDInfo, SiS_Pr->SiS_LCDResInfo, SiS_Pr->SiS_LCDTypeInfo);
-#endif
-#endif
-#ifdef LINUX_XF86
-  xf86DrvMsgVerb(0, X_PROBED, 3, 
-  	"(init301: LCDInfo=0x%04x LCDResInfo=0x%02x LCDTypeInfo=0x%02x SetFlag=0x%04x)\n",
-	SiS_Pr->SiS_LCDInfo, SiS_Pr->SiS_LCDResInfo, SiS_Pr->SiS_LCDTypeInfo, SiS_Pr->SiS_SetFlag);
-#endif
 
+     }
+  } /* LCDA */
 }
 
-void
-SiS_LongWait(SiS_Private *SiS_Pr)
-{
-  USHORT i;
-
-  i = SiS_GetReg1(SiS_Pr->SiS_P3c4,0x1F);
-
-  if(!(i & 0xC0)) {
-    for(i=0; i<0xFFFF; i++) {
-       if(!(SiS_GetReg2(SiS_Pr->SiS_P3da) & 0x08))
-         break;
-    }
-    for(i=0; i<0xFFFF; i++) {
-       if((SiS_GetReg2(SiS_Pr->SiS_P3da) & 0x08))
-         break;
-    }
-  }
-}
+/*********************************************/
+/*         SET PART 2 REGISTER GROUP         */
+/*********************************************/
 
-void
-SiS_VBLongWait(SiS_Private *SiS_Pr)
+#ifdef SIS315H
+static UCHAR *
+SiS_GetGroup2CLVXPtr(SiS_Private *SiS_Pr, int tabletype, PSIS_HW_INFO HwInfo)
 {
-  if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToTV)) {
-    SiS_VBWait(SiS_Pr);
-  } else {
-    SiS_LongWait(SiS_Pr);
-  }
-}
+   UCHAR  *ROMAddr = HwInfo->pjVirtualRomBase;
+   const UCHAR  *tableptr = NULL;
+   USHORT a, b, p = 0;
 
-void
-SiS_VBWait(SiS_Private *SiS_Pr)
-{
-  USHORT tempal,temp,i,j;
+   a = SiS_Pr->SiS_VGAHDE;
+   b = SiS_Pr->SiS_HDE;
+   if(tabletype) {
+      a = SiS_Pr->SiS_VGAVDE;
+      b = SiS_Pr->SiS_VDE;
+   }
 
-  temp = 0;
-  for(i=0; i<3; i++) {
-    for(j=0; j<100; j++) {
-       tempal = SiS_GetReg2(SiS_Pr->SiS_P3da);
-       if(temp & 0x01) {
-          if((tempal & 0x08))  continue;
-          if(!(tempal & 0x08)) break;
-       } else {
-          if(!(tempal & 0x08)) continue;
-          if((tempal & 0x08))  break;
-       }
-    }
-    temp ^= 0x01;
-  }
-}
+   if((ROMAddr = (UCHAR *)HwInfo->pjVirtualRomBase) && SiS_Pr->SiS_UseROM) {
 
-void
-SiS_WaitVBRetrace(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension)
-{
-  if(HwDeviceExtension->jChipType < SIS_315H) {
-#ifdef SIS300
-     if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-        if(!(SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x00) & 0x20)) return;
-     }
-     if(!(SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x00) & 0x80)) {
-        SiS_WaitRetrace1(SiS_Pr,HwDeviceExtension);
-     } else {
-        SiS_WaitRetrace2(SiS_Pr,HwDeviceExtension);
-     }
-#endif
-  } else {
-#ifdef SIS315H
-     if(!(SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x00) & 0x40)) {
-        SiS_WaitRetrace1(SiS_Pr,HwDeviceExtension);
-     } else {
-        SiS_WaitRetrace2(SiS_Pr,HwDeviceExtension);
-     }
-#endif
-  }
-}
+      if(a < b) {
+         p = ROMAddr[0x278] | (ROMAddr[0x279] << 8);
+      } else if(a == b) {
+         p = ROMAddr[0x27a] | (ROMAddr[0x27b] << 8);
+      } else {
+         if(SiS_Pr->SiS_TVMode & TVSetPAL) {
+	    p = ROMAddr[0x27e] | (ROMAddr[0x27f] << 8);
+	 } else {
+	    p = ROMAddr[0x27c] | (ROMAddr[0x27d] << 8);
+	 }
+	 if(SiS_Pr->SiS_VBInfo & SetCRT2ToYPbPr525750) {
+	    if(SiS_Pr->SiS_TVMode & TVSetYPbPr525i) 	 p = ROMAddr[0x280] | (ROMAddr[0x281] << 8);
+	    else if(SiS_Pr->SiS_TVMode & TVSetYPbPr525p) p = ROMAddr[0x282] | (ROMAddr[0x283] << 8);
+	    else 				 	 p = ROMAddr[0x284] | (ROMAddr[0x285] << 8);
+	 } else if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) {
+	    p = ROMAddr[0x286] | (ROMAddr[0x287] << 8);
+	 }
+	 do {
+	    if((ROMAddr[p] | ROMAddr[p+1] << 8) == a) break;
+	    p += 0x42;
+	 } while((ROMAddr[p] | ROMAddr[p+1] << 8) != 0xffff);
+	 if((ROMAddr[p] | ROMAddr[p+1] << 8) == 0xffff) p -= 0x42;
+      }
+      p += 2;
+      return(&ROMAddr[p]);
 
-void
-SiS_WaitRetrace1(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension)
-{
-  USHORT watchdog;
-#ifdef SIS300
-  USHORT i;
-#endif
+   } else {
 
-  if(HwDeviceExtension->jChipType >= SIS_315H) {
-#ifdef SIS315H
-     if(SiS_GetReg1(SiS_Pr->SiS_P3c4,0x1f) & 0xc0) return;
-     watchdog = 65535;
-     while( (SiS_GetReg2(SiS_Pr->SiS_P3da) & 0x08) && --watchdog);
-     watchdog = 65535;
-     while( (!(SiS_GetReg2(SiS_Pr->SiS_P3da) & 0x08)) && --watchdog);
-#endif
-  } else {
-#ifdef SIS300
-#if 0  /* Not done in A901 BIOS */
-     if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-        if(SiS_GetReg1(SiS_Pr->SiS_P3c4,0x1f) & 0xc0) return;
-     }
-#endif
-     for(i=0; i<10; i++) {
-        watchdog = 65535;
-        while( (SiS_GetReg2(SiS_Pr->SiS_P3da) & 0x08) && --watchdog);
-	if(watchdog) break;
-     }
-     for(i=0; i<10; i++) {
-        watchdog = 65535;
-        while( (!(SiS_GetReg2(SiS_Pr->SiS_P3da) & 0x08)) && --watchdog);
-	if(watchdog) break;
-     }
-#endif
-  }
+      if(a < b) {
+         tableptr = SiS_Part2CLVX_1;
+      } else if(a == b) {
+         tableptr = SiS_Part2CLVX_2;
+      } else {
+         if(SiS_Pr->SiS_TVMode & TVSetPAL) {
+	    tableptr = SiS_Part2CLVX_4;
+	 } else {
+	    tableptr = SiS_Part2CLVX_3;
+	 }
+	 if(SiS_Pr->SiS_VBInfo & SetCRT2ToYPbPr525750) {
+	    if(SiS_Pr->SiS_TVMode & TVSetYPbPr525i) 	 tableptr = SiS_Part2CLVX_3;
+	    else if(SiS_Pr->SiS_TVMode & TVSetYPbPr525p) tableptr = SiS_Part2CLVX_3;
+	    else 				         tableptr = SiS_Part2CLVX_5;
+	 } else if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) {
+	    tableptr = SiS_Part2CLVX_6;
+	 }
+	 do {
+	    if((tableptr[p] | tableptr[p+1] << 8) == a) break;
+	    p += 0x42;
+	 } while((tableptr[p] | tableptr[p+1] << 8) != 0xffff);
+	 if((tableptr[p] | tableptr[p+1] << 8) == 0xffff) p -= 0x42;
+      }
+      p += 2;
+      return((UCHAR *)&tableptr[p]);
+   }
 }
 
-void
-SiS_WaitRetraceDDC(SiS_Private *SiS_Pr)
+static void
+SiS_SetGroup2_C_ELV(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,
+	      	    USHORT RefreshRateTableIndex, PSIS_HW_INFO HwInfo)
 {
-  USHORT watchdog;
-
-  if(SiS_GetReg1(SiS_Pr->SiS_P3c4,0x1f) & 0xc0) return;
-  watchdog = 65535;
-  while( (SiS_GetReg2(SiS_Pr->SiS_P3da) & 0x08) && --watchdog);
-  watchdog = 65535;
-  while( (!(SiS_GetReg2(SiS_Pr->SiS_P3da) & 0x08)) && --watchdog);
+   UCHAR *tableptr;
+   int i, j;
+   UCHAR temp;
+
+   if(!(SiS_Pr->SiS_VBType & (VB_SIS301C | VB_SIS302ELV))) return;
+
+   tableptr = SiS_GetGroup2CLVXPtr(SiS_Pr, 0, HwInfo);
+   for(i = 0x80, j = 0; i <= 0xbf; i++, j++) {
+      SiS_SetReg(SiS_Pr->SiS_Part2Port, i, tableptr[j]);
+   }
+   if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+      tableptr = SiS_GetGroup2CLVXPtr(SiS_Pr, 1, HwInfo);
+      for(i = 0xc0, j = 0; i <= 0xff; i++, j++) {
+         SiS_SetReg(SiS_Pr->SiS_Part2Port, i, tableptr[j]);
+      }
+   }
+   temp = 0x10;
+   if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) temp |= 0x04;
+   SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x4e,0xeb,temp);
 }
 
-void
-SiS_WaitRetrace2(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension)
+static void
+SiS_GetCRT2Part2Ptr(SiS_Private *SiS_Pr,USHORT ModeNo,USHORT ModeIdIndex,
+		    USHORT RefreshRateTableIndex,USHORT *CRT2Index,
+		    USHORT *ResIndex,PSIS_HW_INFO HwInfo)
 {
-  USHORT watchdog;
-#ifdef SIS300
-  USHORT i;
-#endif
+  USHORT tempbx,tempal;
 
-  if(HwDeviceExtension->jChipType >= SIS_315H) {
-#ifdef SIS315H
-     watchdog = 65535;
-     while( (SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x30) & 0x02) && --watchdog);
-     watchdog = 65535;
-     while( (!(SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x30) & 0x02)) && --watchdog);
-#endif
-  } else {
-#ifdef SIS300
-     for(i=0; i<10; i++) {
-        watchdog = 65535;
-	while( (SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x25) & 0x02) && --watchdog);
-	if(watchdog) break;
+  if(ModeNo <= 0x13)
+      	tempal = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_CRT2CRTC;
+  else
+      	tempal = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
+
+  tempbx = SiS_Pr->SiS_LCDResInfo;
+
+  if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)      tempbx += 16;
+  else if(SiS_Pr->SiS_SetFlag & LCDVESATiming) tempbx += 32;
+
+  if(SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) {
+     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
+        tempbx = 100;
+        if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)      tempbx = 101;
+  	else if(SiS_Pr->SiS_SetFlag & LCDVESATiming) tempbx = 102;
+     }
+  } else if(SiS_Pr->SiS_CustomT == CUT_CLEVO1024) {
+     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
+        if(SiS_IsDualLink(SiS_Pr, HwInfo)) {
+           tempbx = 103;
+           if(SiS_Pr->SiS_LCDInfo & DontExpandLCD)      tempbx = 104;
+  	   else if(SiS_Pr->SiS_SetFlag & LCDVESATiming) tempbx = 105;
+	}
      }
-     for(i=0; i<10; i++) {
-        watchdog = 65535;
-	while( (!(SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x25) & 0x02)) && --watchdog);
-	if(watchdog) break;
+  }  else if(SiS_Pr->SiS_CustomT == CUT_ASUSA2H_2) {
+     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
+        if(SiS_Pr->SiS_SetFlag & LCDVESATiming) tempbx = 106;
      }
-#endif
   }
-}
 
-/* =========== Set and Get register routines ========== */
+  *CRT2Index = tempbx;
+  *ResIndex = tempal & 0x3F;
+}
+#endif
 
-void
-SiS_SetRegANDOR(USHORT Port,USHORT Index,USHORT DataAND,USHORT DataOR)
+#ifdef SIS300
+/* For ECS A907. Highly preliminary. */
+static void
+SiS_Set300Part2Regs(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,
+    		    USHORT ModeIdIndex, USHORT RefreshRateTableIndex,
+		    USHORT ModeNo)
 {
-  USHORT temp;
+  USHORT crt2crtc, resindex;
+  int    i,j;
+  const  SiS_Part2PortTblStruct *CRT2Part2Ptr = NULL;
 
-  temp = SiS_GetReg1(Port,Index);    
-  temp = (temp & (DataAND)) | DataOR;
-  SiS_SetReg1(Port,Index,temp);
-}
+  if(HwInfo->jChipType != SIS_300) return;
+  if(!(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV)) return;
+  if(SiS_Pr->UseCustomMode) return;
 
-void
-SiS_SetRegAND(USHORT Port,USHORT Index,USHORT DataAND)
-{
-  USHORT temp;
+  if(ModeNo <= 0x13) {
+     crt2crtc = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_CRT2CRTC;
+  } else {
+     crt2crtc = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
+  }
 
-  temp = SiS_GetReg1(Port,Index);    
-  temp &= DataAND;
-  SiS_SetReg1(Port,Index,temp);
-}
+  resindex = crt2crtc & 0x3F;
+  if(SiS_Pr->SiS_SetFlag & LCDVESATiming) CRT2Part2Ptr = SiS_Pr->SiS_CRT2Part2_1024x768_1;
+  else                                    CRT2Part2Ptr = SiS_Pr->SiS_CRT2Part2_1024x768_2;
 
-void SiS_SetRegOR(USHORT Port,USHORT Index,USHORT DataOR)
-{
-  USHORT temp;
+  /* The BIOS code (1.16.51,56) is obviously a fragment! */
+  if(ModeNo > 0x13) {
+     CRT2Part2Ptr = SiS_Pr->SiS_CRT2Part2_1024x768_1;
+     resindex = 4;
+  }
 
-  temp = SiS_GetReg1(Port,Index);    
-  temp |= DataOR;
-  SiS_SetReg1(Port,Index,temp);
+  SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x01,0x80,(CRT2Part2Ptr+resindex)->CR[0]);
+  SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x02,0x80,(CRT2Part2Ptr+resindex)->CR[1]);
+  for(i = 2, j = 0x04; j <= 0x06; i++, j++ ) {
+        SiS_SetReg(SiS_Pr->SiS_Part2Port,j,(CRT2Part2Ptr+resindex)->CR[i]);
+  }
+  for(j = 0x1c; j <= 0x1d; i++, j++ ) {
+        SiS_SetReg(SiS_Pr->SiS_Part2Port,j,(CRT2Part2Ptr+resindex)->CR[i]);
+  }
+  for(j = 0x1f; j <= 0x21; i++, j++ ) {
+        SiS_SetReg(SiS_Pr->SiS_Part2Port,j,(CRT2Part2Ptr+resindex)->CR[i]);
+  }
+  SiS_SetReg(SiS_Pr->SiS_Part2Port,0x23,(CRT2Part2Ptr+resindex)->CR[10]);
+  SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x25,0x0f,(CRT2Part2Ptr+resindex)->CR[11]);
 }
-
-/* ========================================================= */
+#endif
 
 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);
+  if(!(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV)) return;
+  if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToTVNoHiVision)) return;
+  if(SiS_Pr->SiS_TVMode & (TVSetYPbPr525p | TVSetYPbPr750p)) return;
+
+  if(!(SiS_Pr->SiS_TVMode & TVSetPAL)) {
+     if(SiS_Pr->SiS_TVMode & TVSetNTSC1024) {
+        const UCHAR specialtv[] = {
+		0xa7,0x07,0xf2,0x6e,0x17,0x8b,0x73,0x53,
+		0x13,0x40,0x34,0xf4,0x63,0xbb,0xcc,0x7a,
+		0x58,0xe4,0x73,0xda,0x13
+	};
+	int i, j;
+	for(i = 0x1c, j = 0; i <= 0x30; i++, j++) {
+	   SiS_SetReg(SiS_Pr->SiS_Part2Port,i,specialtv[j]);
+	}
+	SiS_SetReg(SiS_Pr->SiS_Part2Port,0x43,0x72);
+	if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToYPbPr525750)) {
+	   if(SiS_Pr->SiS_TVMode & TVSetPALM) {
+	      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x01,0x14);
+	   } else {
+	      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x01,0x15);
+	   }
+	   SiS_SetReg(SiS_Pr->SiS_Part2Port,0x02,0x1b);
 	}
      }
+  } else {
+     SiS_SetReg(SiS_Pr->SiS_Part2Port,0x01,0x21);
+     SiS_SetReg(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,
-	      PSIS_HW_DEVICE_INFO HwDeviceExtension)
+static void
+SiS_SetGroup2(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,USHORT RefreshRateTableIndex,
+	      PSIS_HW_INFO HwInfo)
 {
-  USHORT      i, j, tempax, tempbx, tempcx, temp, temp1;
-  USHORT      push1, push2;
+  USHORT      i, j, tempax, tempbx, tempcx, temp;
+  USHORT      push1, push2, modeflag, crt2crtc;
+  ULONG       longtemp, tempeax;
   const       UCHAR *PhasePoint;
   const       UCHAR *TimingPoint;
-#ifdef SIS315H   
-  const       SiS_Part2PortTblStruct *CRT2Part2Ptr = NULL;
+#ifdef SIS315H
   USHORT      resindex, CRT2Index;
+  const       SiS_Part2PortTblStruct *CRT2Part2Ptr = NULL;
 #endif
-  USHORT      modeflag, resinfo, crt2crtc;
-  ULONG       longtemp, tempeax;
 #ifdef SIS300
   const UCHAR atable[] = {
-                 0xc3,0x9e,0xc3,0x9e,0x02,0x02,0x02,
-	         0xab,0x87,0xab,0x9e,0xe7,0x02,0x02
+       0xc3,0x9e,0xc3,0x9e,0x02,0x02,0x02,
+       0xab,0x87,0xab,0x9e,0xe7,0x02,0x02
   };
 #endif  
 
 #ifdef SIS315H   
-  if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
-     /* 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 = 0x01;
-	   if(ModeNo <= 0x13) temp = 0x03;
-	   SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x0b,temp);
-	}
-     }
-     SiS_SetTVSpecial(SiS_Pr, ModeNo);
-     return;
-  }
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) return;
 #endif
 
   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 {
      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;
-  tempax = (tempcx & 0x00FF) << 8;
-  tempbx = (tempcx & 0x00FF) | ((tempcx & 0x00FF) << 8);
-  tempbx &= 0x0410;
-  temp = (tempax & 0x0800) >> 8;
-  temp >>= 1;
-  temp |= (((tempbx & 0xFF00) >> 8) << 1);
-  temp |= ((tempbx & 0x00FF) >> 3);
-  temp ^= 0x0C;
-
-  /* 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 = 0;
+  if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToAVIDEO)) temp |= 0x08;
+  if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToSVIDEO)) temp |= 0x04;
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToSCART)     temp |= 0x02;
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision)  temp |= 0x01;
+
+  /* Disable SVIDEO and CVBS signal */
+  if((SiS_Pr->SiS_CustomT != CUT_COMPAQ1280) &&
+     (SiS_Pr->SiS_CustomT != CUT_CLEVO1400)) {
+     if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
+        temp |= 0x0c;
+     }
   }
 
   PhasePoint  = SiS_Pr->SiS_PALPhase;
   TimingPoint = SiS_Pr->SiS_PALTiming;
-  
-  if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) {          
-  
-     temp ^= 0x01;
+
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) {
+
      if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
         TimingPoint = SiS_Pr->SiS_HiTVSt2Timing;
-        if(SiS_Pr->SiS_SetFlag & TVSimuMode) {
+        if(SiS_Pr->SiS_TVMode & TVSetTVSimuMode) {
            if(modeflag & Charx8Dot) TimingPoint = SiS_Pr->SiS_HiTVSt1Timing;
-           else TimingPoint = SiS_Pr->SiS_HiTVTextTiming;
+           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 & SetCRT2ToYPbPr525750) {
+
+     if(SiS_Pr->SiS_TVMode & TVSetYPbPr750p)      TimingPoint = &SiS_YPbPrTable[2][0];
+     else if(SiS_Pr->SiS_TVMode & TVSetYPbPr525p) TimingPoint = &SiS_YPbPrTable[1][0];
+     else					  TimingPoint = &SiS_YPbPrTable[0][0];
+
+     PhasePoint = SiS_Pr->SiS_NTSCPhase;
 
   } else {
 
-     if(SiS_Pr->SiS_VBInfo & SetPALTV){
+     if(SiS_Pr->SiS_TVMode & TVSetPAL) {
 
         TimingPoint = SiS_Pr->SiS_PALTiming;
         PhasePoint  = SiS_Pr->SiS_PALPhase;
 
         if( (SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) &&
             ( (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) ||
-	      (SiS_Pr->SiS_SetFlag & TVSimuMode) ) ) {
+	      (SiS_Pr->SiS_TVMode & TVSetTVSimuMode) ) ) {
            PhasePoint = SiS_Pr->SiS_PALPhase2;
         }
 
@@ -7327,161 +7285,122 @@
         temp |= 0x10;
         TimingPoint = SiS_Pr->SiS_NTSCTiming;
         PhasePoint  = SiS_Pr->SiS_NTSCPhase;
+	if(SiS_Pr->SiS_TVMode & TVSetNTSCJ) {
+	   PhasePoint = SiS_Pr->SiS_PALPhase;
+	}
 
         if( (SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) &&
 	    ( (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) ||
-	      (SiS_Pr->SiS_SetFlag & TVSimuMode) ) ) {
+	      (SiS_Pr->SiS_TVMode & TVSetTVSimuMode) ) ) {
            PhasePoint = SiS_Pr->SiS_NTSCPhase2;
+	   if(SiS_Pr->SiS_TVMode & TVSetNTSCJ) {
+	      PhasePoint = SiS_Pr->SiS_PALPhase2;
+	   }
         }
 
      }
 
   }
-  SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x00,temp);
+  SiS_SetReg(SiS_Pr->SiS_Part2Port,0x00,temp);
 
-  temp = 0;
-  if((HwDeviceExtension->jChipType == SIS_630)||
-     (HwDeviceExtension->jChipType == SIS_730)) {
-     temp = 0x35;
-  }
-  if(HwDeviceExtension->jChipType >= SIS_315H) {
-     temp = 0x38;
+  if(SiS_Pr->SiS_TVMode & TVSetPALM) {
+     PhasePoint = SiS_Pr->SiS_PALMPhase;
+     if( (SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) &&
+	 ( (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) ||
+	   (SiS_Pr->SiS_TVMode & TVSetTVSimuMode) ) ) {
+        PhasePoint = SiS_Pr->SiS_PALMPhase2;
+     }
   }
-  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_SIS301BLV302BLV) &&
-		  ( (!(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_SIS301BLV302BLV) &&
-		  ( (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) ||
-		    (SiS_Pr->SiS_SetFlag & TVSimuMode) ) ) {
-	         PhasePoint = SiS_Pr->SiS_PALNPhase2;
-	      }
-	   }
-        }
+
+  if(SiS_Pr->SiS_TVMode & TVSetPALN) {
+     PhasePoint = SiS_Pr->SiS_PALNPhase;
+     if( (SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) &&
+	 ( (!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) ||
+	   (SiS_Pr->SiS_TVMode & TVSetTVSimuMode) ) ) {
+	PhasePoint = SiS_Pr->SiS_PALNPhase2;
      }
   }
 
-#ifdef SIS315H
-  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 == 0x64) || (ModeNo == 0x4a) || (ModeNo == 0x38)) {
-	         PhasePoint = SiS_Pr->SiS_SpecialPhase;
-	      }
-           }
-        }
+  if(SiS_Pr->SiS_TVMode & TVSetNTSC1024) {
+     PhasePoint = SiS_Pr->SiS_SpecialPhase;
+     if(SiS_Pr->SiS_TVMode & TVSetPALM) {
+        PhasePoint = SiS_Pr->SiS_SpecialPhaseM;
+     } else if(SiS_Pr->SiS_TVMode & TVSetNTSCJ) {
+        PhasePoint = SiS_Pr->SiS_SpecialPhaseJ;
      }
   }
-#endif
 
   for(i=0x31, j=0; i<=0x34; i++, j++) {
-     SiS_SetReg1(SiS_Pr->SiS_Part2Port,i,PhasePoint[j]);
+     SiS_SetReg(SiS_Pr->SiS_Part2Port,i,PhasePoint[j]);
   }
 
   for(i=0x01, j=0; i<=0x2D; i++, j++) {
-     SiS_SetReg1(SiS_Pr->SiS_Part2Port,i,TimingPoint[j]);
+     SiS_SetReg(SiS_Pr->SiS_Part2Port,i,TimingPoint[j]);
   }
   for(i=0x39; i<=0x45; i++, j++) {
-     SiS_SetReg1(SiS_Pr->SiS_Part2Port,i,TimingPoint[j]);
+     SiS_SetReg(SiS_Pr->SiS_Part2Port,i,TimingPoint[j]);
   }
 
   if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
-     if(HwDeviceExtension->jChipType >= SIS_315H) {
-        if(!(SiS_Pr->SiS_ModeType & 0x07))
-           SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x3A,0x1F);
-     } else {
+     if(SiS_Pr->SiS_ModeType != ModeText) {
         SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x3A,0x1F);
      }
   }
 
   SiS_SetRegOR(SiS_Pr->SiS_Part2Port,0x0A,SiS_Pr->SiS_NewFlickerMode);
 
-  SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x35,SiS_Pr->SiS_RY1COE);
-  SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x36,SiS_Pr->SiS_RY2COE);
-  SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x37,SiS_Pr->SiS_RY3COE);
-  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;
-  } else {
-     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) ) ||
-      ( (SiS_Pr->SiS_VBInfo & SetCRT2ToTV) && (SiS_Pr->SiS_HiVision != 3) &&
-        ( (SiS_Pr->SiS_VGAHDE == 1024) || ((SiS_Pr->SiS_VGAHDE != 1024) && (SiS_Pr->SiS_VDE <= tempax)) ) ) ) {
+  SiS_SetReg(SiS_Pr->SiS_Part2Port,0x35,SiS_Pr->SiS_RY1COE);
+  SiS_SetReg(SiS_Pr->SiS_Part2Port,0x36,SiS_Pr->SiS_RY2COE);
+  SiS_SetReg(SiS_Pr->SiS_Part2Port,0x37,SiS_Pr->SiS_RY3COE);
+  SiS_SetReg(SiS_Pr->SiS_Part2Port,0x38,SiS_Pr->SiS_RY4COE);
+
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) 	tempax = 950;
+  else if(SiS_Pr->SiS_TVMode & TVSetPAL)      	tempax = 520;
+  else 			            		tempax = 440;
+
+  if( ( (!(SiS_Pr->SiS_VBInfo & SetCRT2ToTVNoHiVision)) && (SiS_Pr->SiS_VDE <= tempax) ) ||
+      ( (SiS_Pr->SiS_VBInfo & SetCRT2ToTVNoHiVision) &&
+        ((SiS_Pr->SiS_VGAHDE == 1024) || (SiS_Pr->SiS_VDE <= tempax)) ) ) {
 
      tempax -= SiS_Pr->SiS_VDE;
      tempax >>= 2;
-     tempax = (tempax & 0x00FF) | ((tempax & 0x00FF) << 8);
+     tempax &= 0x00ff;
+
+     temp = tempax + (USHORT)TimingPoint[0];
+     SiS_SetReg(SiS_Pr->SiS_Part2Port,0x01,temp);
 
-     temp = (tempax & 0xFF00) >> 8;
-     temp += (USHORT)TimingPoint[0];
-     SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x01,temp);
-
-     temp = (tempax & 0xFF00) >> 8;
-     temp += (USHORT)TimingPoint[1];
-     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) ) {
-        if(SiS_Pr->SiS_VBInfo & SetPALTV) {
-           SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x01,0x19);
-           SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x02,0x52);
+     temp = tempax + (USHORT)TimingPoint[1];
+     SiS_SetReg(SiS_Pr->SiS_Part2Port,0x02,temp);
+
+     if((SiS_Pr->SiS_VBInfo & SetCRT2ToTVNoHiVision) && (SiS_Pr->SiS_VGAHDE >= 1024)) {
+        if(SiS_Pr->SiS_TVMode & TVSetPAL) {
+           SiS_SetReg(SiS_Pr->SiS_Part2Port,0x01,0x19);
+           SiS_SetReg(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);
-	   } else {
-              SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x01,0x0b);
-              SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x02,0x11);
-	   }
+           SiS_SetReg(SiS_Pr->SiS_Part2Port,0x01,0x17);
+           SiS_SetReg(SiS_Pr->SiS_Part2Port,0x02,0x1d);
         }
      }
 
   }
 
   tempcx = SiS_Pr->SiS_HT;
-
-  /* 650/30xLV 1.10.6s */
-  if(SiS_IsDualLink(SiS_Pr, HwDeviceExtension, BaseAddr)) {
-     tempcx >>= 1;
-  }
-
+  if(SiS_IsDualLink(SiS_Pr, HwInfo)) tempcx >>= 1;
   tempcx--;
-  if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-        tempcx--;
-  }
+  if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) tempcx--;
   temp = tempcx & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x1B,temp);
+  SiS_SetReg(SiS_Pr->SiS_Part2Port,0x1B,temp);
   temp = (tempcx & 0xFF00) >> 8;
   SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x1D,0xF0,temp);
 
   tempcx++;
-  if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-        tempcx++;
-  }
+  if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) tempcx++;
   tempcx >>= 1;
 
   push1 = tempcx;
 
   tempcx += 7;
-  if((SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) &&
-     (SiS_Pr->SiS_HiVision == 3)) {
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) {
      tempcx -= 4;
   }
   temp = (tempcx & 0x00FF) << 4;
@@ -7493,15 +7412,14 @@
   push2 = tempbx;
 
   temp = tempbx & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x24,temp);
+  SiS_SetReg(SiS_Pr->SiS_Part2Port,0x24,temp);
   temp = ((tempbx & 0xFF00) >> 8) << 4;
   SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x25,0x0F,temp);
 
   tempbx = push2;
 
   tempbx += 8;
-  if((SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) &&
-     (SiS_Pr->SiS_HiVision == 3)) {
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) {
      tempbx -= 4;
      tempcx = tempbx;
   }
@@ -7511,14 +7429,13 @@
   j += 2;
   tempcx += ((TimingPoint[j] | ((TimingPoint[j+1]) << 8)));
   temp = tempcx & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x27,temp);
+  SiS_SetReg(SiS_Pr->SiS_Part2Port,0x27,temp);
   temp = ((tempcx & 0xFF00) >> 8) << 4;
   SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x28,0x0F,temp);
 
   tempcx += 8;
-  if((SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) &&
-     (SiS_Pr->SiS_HiVision == 3)) {
-     tempcx -= 4; 
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) {
+     tempcx -= 4;
   }
   temp = (tempcx & 0x00FF) << 4;
   SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x2A,0x0F,temp);
@@ -7532,141 +7449,114 @@
 
   tempcx -= 11;
   if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToTV)) {
-     tempax = SiS_GetVGAHT2(SiS_Pr) - 1;
-     tempcx = tempax;
+     tempcx = SiS_GetVGAHT2(SiS_Pr) - 1;
   }
   temp = tempcx & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2E,temp);
+  SiS_SetReg(SiS_Pr->SiS_Part2Port,0x2E,temp);
 
   tempbx = SiS_Pr->SiS_VDE;
   if(SiS_Pr->SiS_VGAVDE == 360) tempbx = 746;
   if(SiS_Pr->SiS_VGAVDE == 375) tempbx = 746;
   if(SiS_Pr->SiS_VGAVDE == 405) tempbx = 853;
-  if(HwDeviceExtension->jChipType < SIS_315H) {
+  if(HwInfo->jChipType < SIS_315H) {
      if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) tempbx >>= 1;
   } else {
-     if((SiS_Pr->SiS_VBInfo & SetCRT2ToTV) && (!(SiS_Pr->SiS_HiVision & 0x03))) {
+     if( (SiS_Pr->SiS_VBInfo & SetCRT2ToTV) &&
+         (!(SiS_Pr->SiS_TVMode & (TVSetYPbPr525p|TVSetYPbPr750p))) ) {
 	tempbx >>= 1;
-	if(SiS_Pr->SiS_SetFlag & TVSimuMode) {
+	if(SiS_Pr->SiS_TVMode & TVSetTVSimuMode) {
 	   if(ModeNo <= 0x13) {
-	      if(crt2crtc == 1) {
-	         tempbx++;
-              }
+	      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++;
+	} else if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
+	   if(crt2crtc == 4) {
+              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 & SetCRT2ToHiVision) {
      if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
         if((ModeNo == 0x2f) || (ModeNo == 0x5d) || (ModeNo == 0x5e)) temp++;
      }
   }
-  /* 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(HwInfo->jChipType < SIS_661) {
+     /* From 1.10.7w - doesn't make sense */
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+        if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
+           if(!(SiS_Pr->SiS_TVMode & TVSetPAL)) {
+	      if(!(SiS_Pr->SiS_SetFlag & LCDVESATiming)) {   /* SetFlag?? */
+	         if(ModeNo == 0x03) temp++;
+	      }
 	   }
-	}
+        }
      }
   }
-  SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2F,temp);
+  SiS_SetReg(SiS_Pr->SiS_Part2Port,0x2F,temp);
 
-  tempax = (tempcx & 0xFF00) | (tempax & 0x00FF);
-  tempbx = ((tempbx & 0xFF00) << 6) | (tempbx & 0x00FF);
-  tempax |= (tempbx & 0xFF00);
-  if(HwDeviceExtension->jChipType < SIS_315H) {
-     if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV)) {
-        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) */ ) {
-	tempax |= 0x1000;
-        if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToSVIDEO))  tempax |= 0x2000;
-     }
+  temp = (tempcx >> 8) & 0x0F;
+  temp |= (((tempbx >> 8) << 6) & 0xC0);
+  if(!(SiS_Pr->SiS_VBInfo & (SetCRT2ToHiVision | SetCRT2ToYPbPr525750 | SetCRT2ToSCART))) {
+     temp |= 0x10;
+     if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToSVIDEO)) temp |= 0x20;
   }
-  temp = (tempax & 0xFF00) >> 8;
-  SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x30,temp);
+  SiS_SetReg(SiS_Pr->SiS_Part2Port,0x30,temp);
 
-  /* 650/30xLV 1.10.6s */
-  if(HwDeviceExtension->jChipType > SIS_315H) {
+  if((HwInfo->jChipType > SIS_315H) && (HwInfo->jChipType < SIS_661)) {
      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);
+        if(SiS_Pr->SiS_VBType & (VB_SIS301C | VB_SIS302LV | VB_SIS302ELV)) {
+           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);
+	   }
         }
      }
   }
-  
-  if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) {
-     if(SiS_Pr->SiS_HiVision != 3) {
-	for(i=0, j=0; i<=0x2d; i++, j++) {
-	    SiS_SetReg1(SiS_Pr->SiS_Part2Port,i,SiS_HiVisionTable[SiS_Pr->SiS_HiVision][j]);
-	}
-	for(i=0x39; i<=0x45; i++, j++) {
-	    SiS_SetReg1(SiS_Pr->SiS_Part2Port,i,SiS_HiVisionTable[SiS_Pr->SiS_HiVision][j]);
-	}
-     }
-  }
 
   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;
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+        if(!(SiS_Pr->SiS_TVMode & (TVSetYPbPr525p | TVSetYPbPr750p))) {
+           tempbx >>= 1;
+	}
      }
      tempbx -= 3;
+     if(HwInfo->jChipType >= SIS_661) {
+        if(SiS_Pr->SiS_VBType & (VB_SIS301C | VB_SIS302LV | VB_SIS302ELV)) {  /* Why not 301B/LV? */
+           temp = 0;
+	   if(tempcx & 0x0400) temp |= 0x20;
+	   if(tempbx & 0x0400) temp |= 0x40;
+	   SiS_SetReg(SiS_Pr->SiS_Part4Port,0x10,temp);
+	}
+     }
      tempbx &= 0x03ff;
      temp = ((tempbx & 0xFF00) >> 8) << 5;
      temp |= 0x18;
-     SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x46,temp);
+     SiS_SetReg(SiS_Pr->SiS_Part2Port,0x46,temp);
      temp = tempbx & 0x00FF;
-     SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x47,temp);
-     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);
-        }
-     }
+     SiS_SetReg(SiS_Pr->SiS_Part2Port,0x47,temp);
+
   }
 
-  tempbx &= 0x00FF;
+  tempbx = 0;
   if(!(modeflag & HalfDCLK)) {
      if(SiS_Pr->SiS_VGAHDE >= SiS_Pr->SiS_HDE) {
+        tempax = 0;
         tempbx |= 0x2000;
-        tempax &= 0x00FF;
      }
   }
 
   tempcx = 0x0101;
-/*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)) {
-              tempcx = 0x1920;
-              if(SiS_Pr->SiS_VGAHDE >= 1280) {
-                 tempcx = 0x1420;
-                 tempbx &= 0xDFFF;
-              }
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+     if(SiS_Pr->SiS_VGAHDE >= 1024) {
+        if((!(modeflag & HalfDCLK)) || (HwInfo->jChipType < SIS_315H)) {
+           tempcx = 0x1920;
+           if(SiS_Pr->SiS_VGAHDE >= 1280) {
+              tempcx = 0x1420;
+              tempbx &= ~0x2000;
            }
         }
      }
@@ -7684,23 +7574,23 @@
      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);
+     tempcx = (tempax & 0xFF00) >> (8 + 5);
   }
 
-  temp = (tempax & 0xFF00) >> 8;
-  SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x44,temp);
-  temp = (tempbx & 0xFF00) >> 8;
-  SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x45,0xC0,temp);
+  SiS_SetReg(SiS_Pr->SiS_Part2Port,0x44,tempax);
+  SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x45,0xC0,(tempbx >> 8));
 
   if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-     temp = tempcx & 0x00FF;
+
+     temp = tempcx & 0x0007;
      if(tempbx & 0x2000) temp = 0;
-     temp |= 0x18;
+     if((HwInfo->jChipType < SIS_661) || (!(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD))) {
+        temp |= 0x18;
+     }
      SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x46,0xE0,temp);
 
-     if(SiS_Pr->SiS_VBInfo & SetPALTV) {
+     if(SiS_Pr->SiS_TVMode & TVSetPAL) {
         tempbx = 0x0382;
         tempcx = 0x007e;
      } else {
@@ -7708,78 +7598,54 @@
         tempcx = 0x0061;
      }
      temp = (tempbx & 0x00FF) ;
-     SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x4B,temp);
+     SiS_SetReg(SiS_Pr->SiS_Part2Port,0x4B,temp);
      temp = (tempcx & 0x00FF) ;
-     SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x4C,temp);
+     SiS_SetReg(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);
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToYPbPr525750) {
+        temp |= 0x10;
+	if(SiS_Pr->SiS_TVMode & TVSetYPbPr525p)      temp |= 0x20;
+	else if(SiS_Pr->SiS_TVMode & TVSetYPbPr750p) temp |= 0x40;
      }
+     SiS_SetReg(SiS_Pr->SiS_Part2Port,0x4D,temp);
 
-     temp = SiS_GetReg1(SiS_Pr->SiS_Part2Port,0x43);
-     SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x43,(USHORT)(temp - 3));
-  }
+     temp = SiS_GetReg(SiS_Pr->SiS_Part2Port,0x43);
+     SiS_SetReg(SiS_Pr->SiS_Part2Port,0x43,(USHORT)(temp - 3));
 
-  temp = 0;
-  if((HwDeviceExtension->jChipType == SIS_630) ||
-     (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_SetRegAND(SiS_Pr->SiS_Part2Port,0x00,0xEF);
-              temp = SiS_GetReg1(SiS_Pr->SiS_Part2Port,0x01);
-              SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x01,temp - 1);
-           }
-        }
+     SiS_SetTVSpecial(SiS_Pr, ModeNo);
+
+     if(SiS_Pr->SiS_VBType & VB_SIS301C) {
+        temp = 0;
+        if(SiS_Pr->SiS_TVMode & TVSetPALM) temp = 8;
+        SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x4e,0xf7,temp);
      }
+
   }
 
-  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(SiS_Pr->SiS_TVMode & TVSetPALM) {
+     if(!(SiS_Pr->SiS_TVMode & TVSetNTSC1024)) {
+        temp = SiS_GetReg(SiS_Pr->SiS_Part2Port,0x01);
+        SiS_SetReg(SiS_Pr->SiS_Part2Port,0x01,temp - 1);
      }
+     SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x00,0xEF);
   }
 
-#if 0  /* Old: Why HiVision? */
-  if( (SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) &&
-      (!(SiS_Pr->SiS_VBType & VB_SIS301LV302LV)) ) {
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) {
      if(!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) {
-        SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x0B,0x00);
+        SiS_SetReg(SiS_Pr->SiS_Part2Port,0x0B,0x00);
      }
   }
-#endif
 
-  if(HwDeviceExtension->jChipType < SIS_315H) {
-     if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
-     	SiS_Set300Part2Regs(SiS_Pr, HwDeviceExtension, ModeIdIndex,
-			    RefreshRateTableIndex, BaseAddr, ModeNo);
-	return;
-     }
-  } else {
-     /* !!! The following is a duplicate, done for LCDA as well (see above) */
-     if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
-        SiS_SetTVSpecial(SiS_Pr, ModeNo);
-        return;
-     }
-  }
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) return;
 
   /* From here: Part2 LCD setup */
 
   tempbx = SiS_Pr->SiS_HDE;
-  if(SiS_IsDualLink(SiS_Pr, HwDeviceExtension, BaseAddr)) tempbx >>= 1;
-  tempbx--;			         	/* RHACTE=HDE-1 */
+  if(SiS_IsDualLink(SiS_Pr, HwInfo)) tempbx >>= 1;
+  tempbx--;			         	/* RHACTE = HDE - 1 */
   temp = tempbx & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2C,temp);
+  SiS_SetReg(SiS_Pr->SiS_Part2Port,0x2C,temp);
   temp = (tempbx & 0xFF00) >> 4;
   SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x2B,0x0F,temp);
 
@@ -7788,7 +7654,7 @@
      if(SiS_Pr->SiS_ModeType == ModeEGA) {
         if(SiS_Pr->SiS_VGAHDE >= 1024) {
            temp = 0x02;
-	   if(HwDeviceExtension->jChipType >= SIS_315H) {
+	   if(HwInfo->jChipType >= SIS_315H) {
               if(SiS_Pr->SiS_SetFlag & LCDVESATiming) {
                  temp = 0x01;
 	      }
@@ -7796,89 +7662,185 @@
         }
      }
   }
-  SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x0B,temp);
+  SiS_SetReg(SiS_Pr->SiS_Part2Port,0x0B,temp);
 
-  tempbx = SiS_Pr->SiS_VDE;         		/* RTVACTEO = VDE - 1 */
-  /* push1 = tempbx; */
-  tempbx--;
+  tempbx = SiS_Pr->SiS_VDE - 1;
   temp = tempbx & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x03,temp);
+  SiS_SetReg(SiS_Pr->SiS_Part2Port,0x03,temp);
   temp = ((tempbx & 0xFF00) >> 8) & 0x07;
   SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x0C,0xF8,temp);
 
-  tempcx = SiS_Pr->SiS_VT;
-  /* push2 = tempcx; */
-  tempcx--;
-  temp = tempcx & 0x00FF;  			 /* RVTVT = VT - 1 */
-  SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x19,temp);
+  tempcx = SiS_Pr->SiS_VT - 1;
+  temp = tempcx & 0x00FF;
+  SiS_SetReg(SiS_Pr->SiS_Part2Port,0x19,temp);
 
-  temp = (tempcx & 0xFF00) >> 8;
-  temp <<= 5;
-  
-  /* 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;
-  } else if(HwDeviceExtension->jChipType < SIS_315H) {
-     temp |= 0x10;
-  } else {
-     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 = ((tempcx & 0xFF00) >> 8) << 5;
+
+  /* Enable dithering; only do this for 32bpp mode */
+  if(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit) {
+     if(SiS_GetReg(SiS_Pr->SiS_Part1Port,0x00) & 0x01) {
         temp |= 0x10;
      }
   }
 
-  /* 630/301 does not do all this */
+  /* Must do special for Compaq1280; Acer1280 OK, Clevo1400 OK, COMPAL1400 OK */
+  /* Compaq1280 panel loses sync if using CR37 sync info. */
   if((SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) && (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD)) {
-     if((HwDeviceExtension->jChipType >= SIS_315H) && (SiS_Pr->SiS_VBType & VB_SIS301LV302LV)) {
+     if((HwInfo->jChipType >= SIS_315H) && (SiS_Pr->SiS_VBType & VB_SIS301LV302LV)) {
 	if((SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) &&
 	   (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024)) {
-#ifdef SIS315H
 	   if(SiS_Pr->SiS_LCDInfo & LCDSync) {
-	      temp |= (SiS_Pr->SiS_LCDInfo >> 6);
+	      temp |= ((SiS_Pr->SiS_LCDInfo & 0xc0) >> 6);
 	   }
-#endif
+	} else if((SiS_Pr->SiS_CustomT == CUT_CLEVO1400) &&
+	          (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050)) {
+	   temp |= 0x03;
 	} else {
-	   /* 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 */
+           temp |= (SiS_GetReg(SiS_Pr->SiS_P3d4,0x37) >> 6);
+	   temp |= 0x08;
+	   if(!(SiS_Pr->SiS_LCDInfo & LCDRGB18Bit)) temp |= 0x04;
 	}
      } else {
         if(SiS_Pr->SiS_LCDInfo & LCDSync) {
-	   temp |= (SiS_Pr->SiS_LCDInfo >> 6);
+	   temp |= ((SiS_Pr->SiS_LCDInfo & 0xc0) >> 6);
 	}
      }
   }
-  SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x1A,temp);
 
-  SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x09,0xF0);
-  SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x0A,0xF0);
+  SiS_SetReg(SiS_Pr->SiS_Part2Port,0x1A,temp);
+
+  SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x09,0xF0);
+  SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x0A,0xF0);
+
+  SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x17,0xFB);
+  SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x18,0xDF);
+
+#if 0  /* Use the 315/330 series code for now */
+  if((HwInfo->jChipType >= SIS_661)          &&
+     (SiS_Pr->SiS_VBType & VB_SIS301LV302LV) &&
+     (ROMAddr && SiS_Pr->SiS_UseROM)) {
+
+      /* This is done for the LVDS bridges only, since
+       * the TMDS panels already work correctly with
+       * the old code. Besides, we only do that if
+       * we can get the data from the ROM, I am tired
+       * of carrying a lot of tables around.
+       */
+
+#ifdef SIS315H 							/* ------------ 661/741/760 series --------- */
+      UCHAR *myptr = NULL, myptr1 = NULL;
+
+      myptr = (UCHAR *)GetLCDPtr661(SiS_Pr, HwInfo, 6, ModeNo, ModeIdIndex, RefreshRateTableIndex);
+      myptr1 = (UCHAR *)GetLCDStructPtr661(SiS_Pr, HwInfo);
+
+      tempbx = (myptr[3] | (myptr[4] << 8)) & 0x0fff;
+      tempcx = SiS_Pr->PanelYRes;
+      if(SiS_Pr->SiS_LCDInfo & LCDPass11) {
+         tempcx = SiS_Pr->SiS_VDE;
+      }
+
+      tempcx += tempbx;
+      if(tempcx >= SiS_Pr->SiS_VT) tempcx -= SiS_Pr->SiS_VT;
+
+      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x05,tempbx);
+      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x06,tempcx);
+
+      tempcx &= 0x07ff;
+      tempbx &= 0x07ff;
+      temp = (tempcx >> 8) << 3;
+      temp |= (tempbx >> 8);
+      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x02,temp);
+
+      tempbx = (myptr[4] | (myptr[5] << 8)) >> 4;
+      tempcx = myptr1[6];
+      if(SiS_Pr->SiS_LCDInfo & LCDPass11) tempcx = myptr[7];
+
+      tempcx += tempbx;
+      if(tempcx >= SiS_Pr->SiS_VT) tempcx -= SiS_Pr->SiS_VT;
+
+      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x04,tempbx);
+      temp = tempcx & 0x000f;
+      temp |= ((tempbx & 0x0f00) >> 4);
+      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x01,temp);
+
+      tempax = SiS_Pr->SiS_HT;
+      tempbx = (myptr[0] | (myptr[1] << 8)) & 0x0fff;
+      tempcx = SiS_Pr->PanelXRes;
+      if(SiS_Pr->SiS_LCDInfo & LCDPass11) tempcx = SiS_Pr->SiS_HDE;
+
+      if(SiS_IsDualLink(SiS_Pr, HwInfo)) {
+         tempax >>= 1;
+	 tempbx >>= 1;
+	 tempcx >>= 1;
+      }
+      if(SiS_Pr->SiS_VBType & VB_SIS302LV)                 tempbx++;
+      if(SiS_Pr->SiS_VBType & (VB_SIS301C | VB_SIS302ELV)) tempbx++;
+
+      tempcx += tempbx;
+      if(tempcx >= tempax) tempcx -= tempax;
+
+      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x1f,tempbx);
+      temp = ((tempbx & 0xff00) >> 8) << 4;
+      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x20,temp);
+      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x23,tempcx);
+      temp = tempcx >> 8;
+      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x25,temp);
+
+      tempax = SiS_Pr->SiS_HT;
+      tempbx = (myptr[1] | (myptr[2] << 8)) >> 4;
+      tempcx = myptr1[5];
+      if(SiS_Pr->SiS_LCDInfo & LCDPass11) {
+         tempcx = myptr[6];
+      }
+      if(SiS_IsDualLink(SiS_Pr, HwInfo)) {
+         tempax >>= 1;
+	 tempbx >>= 1;
+	 tempcx >>= 1;
+      }
+      if(SiS_Pr->SiS_VBType & VB_SIS302LV) tempbx++;
+
+      tempcx += tempbx;
+      if(tempcx >= tempax) tempcx -= tempax;
+
+      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x1c,tempbx);
+      temp = (tempbx & 0x0f00) >> 4;
+      SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x1d,0x0f,temp);
+      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x21,tempcx);
+
+      if(!(SiS_Pr->SiS_SetFlag & LCDVESATiming)) {
+         if(SiS_Pr->SiS_VGAVDE == 525) {
+	    temp = 0xc3;
+	    if(SiS_Pr->SiS_ModeType <= ModeVGA) {
+	       temp++;
+	       if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) temp += 2;
+	    }
+	    SiS_SetReg(SiS_Pr->SiS_Part2Port,0x2f,temp);
+	    SiS_SetReg(SiS_Pr->SiS_Part2Port,0x30,0xb3);
+	 } else if(SiS_Pr->SiS_VGAVDE == 420) {
+	    temp = 0x4d;
+	    if(SiS_Pr->SiS_ModeType <= ModeVGA) {
+	       temp++;
+	       if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) temp++;
+	    }
+	    SiS_SetReg(SiS_Pr->SiS_Part2Port,0x2f,temp);
+	 }
+      }
+
+#endif
 
-  SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x17,0xFB);
-  SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x18,0xDF);
+  } else
+#endif
+         if((HwInfo->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) ||
+             (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200)) ) {
 
-  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) ||
-      (SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200)) ) {
-     
 #ifdef SIS315H 							/* ------------- 315/330 series ------------ */
 
-      /* 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.
-       */
-
-      SiS_GetCRT2Part2Ptr(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
-                         &CRT2Index,&resindex,HwDeviceExtension,BaseAddr);
+      SiS_GetCRT2Part2Ptr(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex,
+                          &CRT2Index, &resindex, HwInfo);
 
       switch(CRT2Index) {
         case Panel_1024x768      : CRT2Part2Ptr = SiS_Pr->SiS_CRT2Part2_1024x768_1;  break;  /* "Normal" */
@@ -7899,21 +7861,22 @@
 	case 103:		   CRT2Part2Ptr = (SiS_Part2PortTblStruct *)SiS310_CRT2Part2_Clevo1024x768_1; break;    /* Custom */
 	case 104:		   CRT2Part2Ptr = (SiS_Part2PortTblStruct *)SiS310_CRT2Part2_Clevo1024x768_2; break;
 	case 105:		   CRT2Part2Ptr = (SiS_Part2PortTblStruct *)SiS310_CRT2Part2_Clevo1024x768_3; break;
+	case 106:		   CRT2Part2Ptr = (SiS_Part2PortTblStruct *)SiS310_CRT2Part2_Asus1024x768_3; break;
 	default:                   CRT2Part2Ptr = SiS_Pr->SiS_CRT2Part2_1024x768_3;  break;
       }
 
       SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x01,0x80,(CRT2Part2Ptr+resindex)->CR[0]);
       SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x02,0x80,(CRT2Part2Ptr+resindex)->CR[1]);
       for(i = 2, j = 0x04; j <= 0x06; i++, j++ ) {
-        SiS_SetReg1(SiS_Pr->SiS_Part2Port,j,(CRT2Part2Ptr+resindex)->CR[i]);
+        SiS_SetReg(SiS_Pr->SiS_Part2Port,j,(CRT2Part2Ptr+resindex)->CR[i]);
       }
       for(j = 0x1c; j <= 0x1d; i++, j++ ) {
-        SiS_SetReg1(SiS_Pr->SiS_Part2Port,j,(CRT2Part2Ptr+resindex)->CR[i]);
+        SiS_SetReg(SiS_Pr->SiS_Part2Port,j,(CRT2Part2Ptr+resindex)->CR[i]);
       }
       for(j = 0x1f; j <= 0x21; i++, j++ ) {
-        SiS_SetReg1(SiS_Pr->SiS_Part2Port,j,(CRT2Part2Ptr+resindex)->CR[i]);
+        SiS_SetReg(SiS_Pr->SiS_Part2Port,j,(CRT2Part2Ptr+resindex)->CR[i]);
       }
-      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x23,(CRT2Part2Ptr+resindex)->CR[10]);
+      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x23,(CRT2Part2Ptr+resindex)->CR[10]);
       SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x25,0x0f,(CRT2Part2Ptr+resindex)->CR[11]);
 
       if(!(SiS_Pr->SiS_SetFlag & LCDVESATiming)) {
@@ -7923,25 +7886,24 @@
 	     temp++;
 	     if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) temp += 2;
 	  }
-	  SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2f,temp);
-	  SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x30,0xb3);
+	  SiS_SetReg(SiS_Pr->SiS_Part2Port,0x2f,temp);
+	  SiS_SetReg(SiS_Pr->SiS_Part2Port,0x30,0xb3);
 	} else if(SiS_Pr->SiS_VGAVDE == 420) {
 	  temp = 0x4d;
 	  if(SiS_Pr->SiS_ModeType <= ModeVGA) {
 	     temp++;
 	     if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) temp++;
 	  }
-	  SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2f,temp);
+	  SiS_SetReg(SiS_Pr->SiS_Part2Port,0x2f,temp);
 	}
      }
 
-     /* !!! 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) {
-	   SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x1a,0xfc,0x03);   /* Not done in 1.10.7w */
+	   /* See Sync above, 0x1a */
 	   temp = 1;
 	   if(ModeNo <= 0x13) temp = 3;
-	   SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x0b,temp);
+	   SiS_SetReg(SiS_Pr->SiS_Part2Port,0x0b,temp);
 	}
      }
 #endif
@@ -7949,15 +7911,12 @@
   } else {   /* ------ 300 series and other bridges, other LCD resolutions ------ */
 
       /* Using this on the 301B with an auto-expanding 1024 panel (CR37=1) makes
-       * the panel scale at modes < 1024 (no black bars); if the panel is non-expanding, 
+       * the panel scale at modes < 1024 (no black bars); if the panel is non-expanding,
        * the bridge scales all modes to 1024.
        * !!! Malfunction at 640x480 and 640x400 when panel is auto-expanding - black screen !!!
        */
 
-    /* cx = VT - 1 */
-
-    tempcx++;
-
+    tempcx = SiS_Pr->SiS_VT;
     tempbx = SiS_Pr->PanelYRes;
 
     if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
@@ -7980,13 +7939,6 @@
        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 */
 
@@ -7995,20 +7947,20 @@
 #endif
 
     temp = tempcx & 0x00FF;   				/* RVEQ1EQ=lcdvdes */
-    SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x05,temp);
+    SiS_SetReg(SiS_Pr->SiS_Part2Port,0x05,temp);
     temp = tempbx & 0x00FF;   				/* RVEQ2EQ=lcdvdee */
-    SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x06,temp);
+    SiS_SetReg(SiS_Pr->SiS_Part2Port,0x06,temp);
 
     temp = ((tempbx & 0xFF00) >> 8) << 3;
     temp |= ((tempcx & 0xFF00) >> 8);
-    SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x02,temp);
+    SiS_SetReg(SiS_Pr->SiS_Part2Port,0x02,temp);
 
     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;
+    if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) tempbx -= 10;
 
     /* non-expanding: lcdvrs = tempbx = ((VT + VDE) / 2) - 10 */
 
@@ -8021,7 +7973,7 @@
 #endif
 
     temp = tempbx & 0x00FF;   				/* RTVACTEE = lcdvrs */
-    SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x04,temp);
+    SiS_SetReg(SiS_Pr->SiS_Part2Port,0x04,temp);
 
     temp = ((tempbx & 0xFF00) >> 8) << 4;
     tempbx += (tempcx + 1);
@@ -8036,36 +7988,34 @@
     xf86DrvMsg(0, X_INFO, "lcdvre[3:0] 0x%x\n", (temp & 0x0f));
 #endif
 
-    SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x01,temp);
+    SiS_SetReg(SiS_Pr->SiS_Part2Port,0x01,temp);
 
     /* Code from 630/301B (I+II) BIOS */
 
+#ifdef SIS300
     if(!SiS_Pr->UseCustomMode) {
-       if( ( ( (HwDeviceExtension->jChipType == SIS_630) ||
-               (HwDeviceExtension->jChipType == SIS_730) ) &&
-             (HwDeviceExtension->jChipRevision > 2) )  &&
+       if( ( ( (HwInfo->jChipType == SIS_630) ||
+               (HwInfo->jChipType == SIS_730) ) &&
+             (HwInfo->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);
+             SiS_SetReg(SiS_Pr->SiS_Part2Port,0x04,0xB9);
+             SiS_SetReg(SiS_Pr->SiS_Part2Port,0x05,0xCC);
+             SiS_SetReg(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);
+                SiS_SetReg(SiS_Pr->SiS_Part2Port,0x01,0x2B);
+                SiS_SetReg(SiS_Pr->SiS_Part2Port,0x02,0x13);
+                SiS_SetReg(SiS_Pr->SiS_Part2Port,0x04,0xE5);
+                SiS_SetReg(SiS_Pr->SiS_Part2Port,0x05,0x08);
+                SiS_SetReg(SiS_Pr->SiS_Part2Port,0x06,0xE2);
              }
           }
        }
-    }
 
-#ifdef SIS300
-    if(HwDeviceExtension->jChipType < SIS_315H) {
-       if(!SiS_Pr->UseCustomMode) {
+       if(HwInfo->jChipType < SIS_315H) {
           if(SiS_Pr->SiS_LCDTypeInfo == 0x0c) {
              crt2crtc &= 0x1f;
              tempcx = 0;
@@ -8076,18 +8026,18 @@
              }
              tempcx += crt2crtc;
              if(crt2crtc >= 4) {
-                SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x06,0xff);
+                SiS_SetReg(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);
+                      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x01,0x28);
                    }
                 }
              }
-             SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x02,0x18);
-             SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x04,atable[tempcx]);
+             SiS_SetReg(SiS_Pr->SiS_Part2Port,0x02,0x18);
+             SiS_SetReg(SiS_Pr->SiS_Part2Port,0x04,atable[tempcx]);
           }
        }
     }
@@ -8105,7 +8055,7 @@
 #endif
 
     temp = tempbx & 0x00FF;    			          /* RHEQPLE = lcdhdee */
-    SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x23,temp);
+    SiS_SetReg(SiS_Pr->SiS_Part2Port,0x23,temp);
     temp = (tempbx & 0xFF00) >> 8;
     SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x25,0xF0,temp);
 
@@ -8113,7 +8063,7 @@
     if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
        temp += 2;
     }
-    SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x1F,temp);  	  /* RHBLKE = lcdhdes[7:0] */
+    SiS_SetReg(SiS_Pr->SiS_Part2Port,0x1F,temp);  	  /* RHBLKE = lcdhdes[7:0] */
     SiS_SetRegAND(SiS_Pr->SiS_Part2Port,0x20,0x0F);	  /* lcdhdes [11:8] */
 
     tempbx += tempcx;
@@ -8138,7 +8088,7 @@
           }
        }
     }
-    SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x1C,temp);
+    SiS_SetReg(SiS_Pr->SiS_Part2Port,0x1C,temp);
     temp = (tempbx & 0x0F00) >> 4;
     SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x1D,0x0F,temp);
 
@@ -8158,7 +8108,7 @@
 #endif
 
     temp = tempbx & 0x00FF;            		          /* RHSYEXP2S = lcdhre */
-    SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x21,temp);
+    SiS_SetReg(SiS_Pr->SiS_Part2Port,0x21,temp);
 
     if(!(SiS_Pr->SiS_SetFlag & LCDVESATiming)) {
        if(SiS_Pr->SiS_VGAVDE == 525) {
@@ -8166,86 +8116,35 @@
     	     temp=0xC6;
           else
        	     temp=0xC3;
-          SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x2f,temp);
-          SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x30,0xB3);
+          SiS_SetReg(SiS_Pr->SiS_Part2Port,0x2f,temp);
+          SiS_SetReg(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_SetReg(SiS_Pr->SiS_Part2Port,0x2f,temp);
        }
     }
-    SiS_Set300Part2Regs(SiS_Pr, HwDeviceExtension, ModeIdIndex,
-                        RefreshRateTableIndex, BaseAddr, ModeNo);
-
-  } /* HwDeviceExtension */
-}
 
-USHORT
-SiS_GetVGAHT2(SiS_Private *SiS_Pr)
-{
-  ULONG tempax,tempbx;
+#ifdef SIS300
+    SiS_Set300Part2Regs(SiS_Pr, HwInfo, ModeIdIndex,
+                        RefreshRateTableIndex, ModeNo);
+#endif
 
-  tempbx = ((SiS_Pr->SiS_VGAVT - SiS_Pr->SiS_VGAVDE) * SiS_Pr->SiS_RVBHCMAX) & 0xFFFF;
-  tempax = (SiS_Pr->SiS_VT - SiS_Pr->SiS_VDE) * SiS_Pr->SiS_RVBHCFACT;
-  tempax = (tempax * SiS_Pr->SiS_HT) / tempbx;
-  return((USHORT) tempax);
+  } /* HwInfo */
 }
 
-/* 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,
-			USHORT BaseAddr, USHORT ModeNo)
-{
-  USHORT crt2crtc, resindex;
-  int    i,j;
-  const  SiS_Part2PortTblStruct *CRT2Part2Ptr = NULL;
-
-  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;
-  } else {
-     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;
-
-  /* The BIOS code (1.16.51) is obviously a fragment! */
-  if(ModeNo > 0x13) {
-     CRT2Part2Ptr = SiS_Pr->SiS_CRT2Part2_1024x768_1;
-     resindex = 4;
-  }
-
-  SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x01,0x80,(CRT2Part2Ptr+resindex)->CR[0]);
-  SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x02,0x80,(CRT2Part2Ptr+resindex)->CR[1]);
-  for(i = 2, j = 0x04; j <= 0x06; i++, j++ ) {
-        SiS_SetReg1(SiS_Pr->SiS_Part2Port,j,(CRT2Part2Ptr+resindex)->CR[i]);
-  }
-  for(j = 0x1c; j <= 0x1d; i++, j++ ) {
-        SiS_SetReg1(SiS_Pr->SiS_Part2Port,j,(CRT2Part2Ptr+resindex)->CR[i]);
-  }
-  for(j = 0x1f; j <= 0x21; i++, j++ ) {
-        SiS_SetReg1(SiS_Pr->SiS_Part2Port,j,(CRT2Part2Ptr+resindex)->CR[i]);
-  }
-  SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x23,(CRT2Part2Ptr+resindex)->CR[10]);
-  SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x25,0x0f,(CRT2Part2Ptr+resindex)->CR[11]);
-}
+/*********************************************/
+/*         SET PART 3 REGISTER GROUP         */
+/*********************************************/
 
-void
-SiS_SetGroup3(SiS_Private *SiS_Pr, USHORT  BaseAddr,UCHAR *ROMAddr,USHORT ModeNo,
-              USHORT ModeIdIndex,PSIS_HW_DEVICE_INFO HwDeviceExtension)
+static void
+SiS_SetGroup3(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,
+              PSIS_HW_INFO HwInfo)
 {
-  USHORT temp;
-  USHORT i;
+  USHORT modeflag, i;
   const UCHAR  *tempdi;
-  USHORT modeflag;
 
   if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) return;
 
@@ -8260,73 +8159,105 @@
   }
 
 #ifndef SIS_CP
-  SiS_SetReg1(SiS_Pr->SiS_Part3Port,0x00,0x00);
-#endif  
+  SiS_SetReg(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);
-  } 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(SiS_Pr->SiS_TVMode & TVSetPAL) {
+     SiS_SetReg(SiS_Pr->SiS_Part3Port,0x13,0xFA);
+     SiS_SetReg(SiS_Pr->SiS_Part3Port,0x14,0xC8);
+  } else {
+     SiS_SetReg(SiS_Pr->SiS_Part3Port,0x13,0xF5);
+     SiS_SetReg(SiS_Pr->SiS_Part3Port,0x14,0xB7);
   }
 
-  temp = 0;
-  if((HwDeviceExtension->jChipType == SIS_630)||
-     (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);
-           }
-        }
-     }
+  if(SiS_Pr->SiS_TVMode & TVSetPALM) {
+     SiS_SetReg(SiS_Pr->SiS_Part3Port,0x13,0xFA);
+     SiS_SetReg(SiS_Pr->SiS_Part3Port,0x14,0xC8);
+     SiS_SetReg(SiS_Pr->SiS_Part3Port,0x3D,0xA8);
   }
 
-  if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) {
+  tempdi = NULL;
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) {
      tempdi = SiS_Pr->SiS_HiTVGroup3Data;
-     if(SiS_Pr->SiS_SetFlag & TVSimuMode) {
+     if(SiS_Pr->SiS_TVMode & TVSetTVSimuMode) {
         tempdi = SiS_Pr->SiS_HiTVGroup3Simu;
         if(!(modeflag & Charx8Dot)) {
            tempdi = SiS_Pr->SiS_HiTVGroup3Text;
         }
      }
-     if(SiS_Pr->SiS_HiVision & 0x03) {
+  } else if(SiS_Pr->SiS_VBInfo & SetCRT2ToYPbPr525750) {
+     if(!(SiS_Pr->SiS_TVMode & TVSetYPbPr525i)) {
         tempdi = SiS_HiTVGroup3_1;
-        if(SiS_Pr->SiS_HiVision & 0x02) tempdi = SiS_HiTVGroup3_2;
+        if(SiS_Pr->SiS_TVMode & TVSetYPbPr750p) tempdi = SiS_HiTVGroup3_2;
      }
+  }
+  if(tempdi) {
      for(i=0; i<=0x3E; i++){
-        SiS_SetReg1(SiS_Pr->SiS_Part3Port,i,tempdi[i]);
+        SiS_SetReg(SiS_Pr->SiS_Part3Port,i,tempdi[i]);
+     }
+     if(SiS_Pr->SiS_VBType & (VB_SIS301C | VB_SIS302ELV)) {
+	if(SiS_Pr->SiS_TVMode & TVSetYPbPr525p) {
+	   SiS_SetReg(SiS_Pr->SiS_Part3Port,0x28,0x3f);
+	}
      }
   }
 
+  if(!(SiS_Pr->SiS_VBInfo & (SetCRT2ToHiVision | SetCRT2ToYPbPr525750))) {
 #ifdef SIS_CP
-  SIS_CP_INIT301_CP2
+     SIS_CP_INIT301_CP2
 #endif
+  }
+}
+
+/*********************************************/
+/*         SET PART 4 REGISTER GROUP         */
+/*********************************************/
+
+static void
+SiS_SetCRT2VCLK(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,
+                 USHORT RefreshRateTableIndex, PSIS_HW_INFO HwInfo)
+{
+  USHORT vclkindex;
+  USHORT temp, reg1, reg2;
+
+  if(SiS_Pr->UseCustomMode) {
+     reg1 = SiS_Pr->CSR2B;
+     reg2 = SiS_Pr->CSR2C;
+  } else {
+     vclkindex = SiS_GetVCLK2Ptr(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex,
+                                 HwInfo);
+     reg1 = SiS_Pr->SiS_VBVCLKData[vclkindex].Part4_A;
+     reg2 = SiS_Pr->SiS_VBVCLKData[vclkindex].Part4_B;
+  }
 
+  if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
+     if(SiS_Pr->SiS_TVMode & TVSetNTSC1024) {
+        SiS_SetReg(SiS_Pr->SiS_Part4Port,0x0a,0x57);
+ 	SiS_SetReg(SiS_Pr->SiS_Part4Port,0x0b,0x46);
+	SiS_SetReg(SiS_Pr->SiS_Part4Port,0x1f,0xf6);
+     } else {
+        SiS_SetReg(SiS_Pr->SiS_Part4Port,0x0a,reg1);
+        SiS_SetReg(SiS_Pr->SiS_Part4Port,0x0b,reg2);
+     }
+  } else {
+     SiS_SetReg(SiS_Pr->SiS_Part4Port,0x0a,0x01);
+     SiS_SetReg(SiS_Pr->SiS_Part4Port,0x0b,reg2);
+     SiS_SetReg(SiS_Pr->SiS_Part4Port,0x0a,reg1);
+  }
+  SiS_SetReg(SiS_Pr->SiS_Part4Port,0x12,0x00);
+  temp = 0x08;
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToRAMDAC) temp |= 0x20;
+  SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x12,temp);
 }
 
 /* Set 301 VGA2 registers */
-void
-SiS_SetGroup4(SiS_Private *SiS_Pr, USHORT  BaseAddr,UCHAR *ROMAddr,USHORT ModeNo,
-              USHORT ModeIdIndex,USHORT RefreshRateTableIndex,
-	      PSIS_HW_DEVICE_INFO HwDeviceExtension)
+static void
+SiS_SetGroup4(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,
+  	      USHORT RefreshRateTableIndex, PSIS_HW_INFO HwInfo)
 {
   USHORT tempax,tempcx,tempbx,modeflag,temp,temp2,resinfo;
   ULONG tempebx,tempeax,templong;
@@ -8344,48 +8275,50 @@
      }
   }
 
-  if(HwDeviceExtension->jChipType >= SIS_315H) {
+  if(HwInfo->jChipType >= SIS_315H) {
      if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
         if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
-           SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x24,0x0e);
+           SiS_SetReg(SiS_Pr->SiS_Part4Port,0x24,0x0e);
         }
      }
   }
 
-  if(SiS_Pr->SiS_VBType & VB_SIS302LV) {
-      if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
-          SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x10,0x9f);
-      }
+  if(SiS_Pr->SiS_VBType & (VB_SIS301C | VB_SIS302LV)) {
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+        SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x10,0x9f);
+     }
   }
 
-  if(HwDeviceExtension->jChipType >= SIS_315H) {
+  if(HwInfo->jChipType >= SIS_315H) {
      if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
         if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
-	   /* From 650/301LV (any, incl. 1.10.6s, 1.10.7w) */
-  	   /* This is a duplicate; done at the end, too */
-	   if(SiS_IsDualLink(SiS_Pr, HwDeviceExtension, BaseAddr)) {
+	   if(SiS_IsDualLink(SiS_Pr, HwInfo)) {
 	      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);
+#ifdef SET_EMI
+	   if(SiS_Pr->SiS_VBType & (VB_SIS302LV | VB_SIS302ELV)) {
+	      SiS_SetReg(SiS_Pr->SiS_Part4Port,0x2a,0x00);
+	      SiS_SetReg(SiS_Pr->SiS_Part4Port,0x30,0x00);
+	      SiS_SetReg(SiS_Pr->SiS_Part4Port,0x34,0x10);
+	   }
+#endif
 	}
    	return;
      }
   }
 
   temp = SiS_Pr->SiS_RVBHCFACT;
-  SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x13,temp);
+  SiS_SetReg(SiS_Pr->SiS_Part4Port,0x13,temp);
 
   tempbx = SiS_Pr->SiS_RVBHCMAX;
   temp = tempbx & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x14,temp);
+  SiS_SetReg(SiS_Pr->SiS_Part4Port,0x14,temp);
 
   temp2 = (((tempbx & 0xFF00) >> 8) << 7) & 0x00ff;
 
   tempcx = SiS_Pr->SiS_VGAHT - 1;
   temp = tempcx & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x16,temp);
+  SiS_SetReg(SiS_Pr->SiS_Part4Port,0x16,temp);
 
   temp = (((tempcx & 0xFF00) >> 8) << 3) & 0x00ff;
   temp2 |= temp;
@@ -8394,16 +8327,19 @@
   if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToTV))  tempcx -= 5;
 
   temp = tempcx & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x17,temp);
+  SiS_SetReg(SiS_Pr->SiS_Part4Port,0x17,temp);
 
   temp = temp2 | ((tempcx & 0xFF00) >> 8);
-  SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x15,temp);
+  SiS_SetReg(SiS_Pr->SiS_Part4Port,0x15,temp);
 
   tempbx = SiS_Pr->SiS_VGAHDE;
   if(modeflag & HalfDCLK)  tempbx >>= 1;
+  if(HwInfo->jChipType >= SIS_661) {
+     if(SiS_IsDualLink(SiS_Pr, HwInfo)) tempbx >>= 1;
+  }
 
   temp = 0xA0;
-  if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) {
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) {
      temp = 0;
      if(tempbx > 800) {
         temp = 0xA0;
@@ -8411,524 +8347,188 @@
            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;
-     }
-  }
-  if(SiS_Pr->SiS_HiVision & 0x03) {
-        temp = 0;
-	if(SiS_Pr->SiS_VGAHDE == 1024) temp = 0x20;
-  }
-
-  if(SiS_IsDualLink(SiS_Pr, HwDeviceExtension, BaseAddr)) temp = 0;
-
-  if(SiS_Pr->SiS_VBType & VB_SIS301) {
-     if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1280x1024)
-        temp |= 0x0A;
-  }
-
-  SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x0E,0x10,temp);
-
-  tempebx = SiS_Pr->SiS_VDE;
-
-  if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) {
-     if(!(temp & 0xE0)) tempebx >>=1;
-  }
-
-  tempcx = SiS_Pr->SiS_RVBHRS;
-  temp = tempcx & 0x00FF;
-  SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x18,temp);
-
-  tempeax = SiS_Pr->SiS_VGAVDE;
-  tempcx |= 0x4000;
-  if(tempeax <= tempebx) {
-     tempcx ^= 0x4000;
-  } else {
-     tempeax -= tempebx;
-  }
-
-  templong = (tempeax * 256 * 1024) % tempebx;
-  tempeax = (tempeax * 256 * 1024) / tempebx;
-  tempebx = tempeax;
-  if(templong != 0) tempebx++;
-
-  temp = (USHORT)(tempebx & 0x000000FF);
-  SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x1B,temp);
-  temp = (USHORT)((tempebx & 0x0000FF00) >> 8);
-  SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x1A,temp);
-
-  tempbx = (USHORT)(tempebx >> 16);
-  temp = tempbx & 0x00FF;
-  temp <<= 4;
-  temp |= ((tempcx & 0xFF00) >> 8);
-  SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x19,temp);
-
-  if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-
-         SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x1C,0x28);
-	 tempbx = 0;
-         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_IsDualLink(SiS_Pr, HwDeviceExtension, BaseAddr)) 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++;
-            }
-         }
-	 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 == 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_SIS550650740660) {
-	       temp = 0x0026;  /* 1.10.7w; 1.10.8r; needs corresponding code in Dis/EnableBridge! */
-	    } else {
-	       temp = 0x0036;
-	    }
-	 } else {
-	    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;
-	    }
-         }
-         SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x1F,0xC0,temp);
-
-	 tempbx = SiS_Pr->SiS_HT;
-	 if(SiS_IsDualLink(SiS_Pr, HwDeviceExtension, BaseAddr)) tempbx >>= 1;
-         tempbx >>= 1;
-	 tempbx -= 2;
-         temp = ((tempbx & 0x0700) >> 8) << 3;
-         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);
-	       /* LCD-too-dark-error-source, see FinalizeLCD() */
-	    }
-	 }
-
-	 if(HwDeviceExtension->jChipType >= SIS_315H) {
-	    /* 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_IsDualLink(SiS_Pr, HwDeviceExtension, BaseAddr)) {
-		  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) {
-	    /* 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 */
-
-  SiS_SetCRT2VCLK(SiS_Pr,BaseAddr,ROMAddr,ModeNo,ModeIdIndex,
-                  RefreshRateTableIndex,HwDeviceExtension);
-}
-
-
-void
-SiS_SetCRT2VCLK(SiS_Private *SiS_Pr, USHORT BaseAddr,UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex,
-                 USHORT RefreshRateTableIndex,PSIS_HW_DEVICE_INFO HwDeviceExtension)
-{
-  USHORT vclkindex;
-  USHORT temp, reg1, reg2;
-
-  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) {
-     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 == 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);
-                 }
-              }
-           }
-	}
-     }
-  } else {	
-     SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x0A,0x01);
-     SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x0B,reg2);
-     SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x0A,reg1);
-  }
-  SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x12,0x00);
-  temp = 0x08;
-  if(SiS_Pr->SiS_VBInfo & SetCRT2ToRAMDAC) temp |= 0x20;
-  SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x12,temp);
-}
-
-USHORT
-SiS_GetVCLK2Ptr(SiS_Private *SiS_Pr, UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex,
-                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 LVDSXlat4VCLK[4]   = {VCLK28,       VCLK28,       VCLK28,       VCLK28};
-#ifdef SIS300
-  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_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 USHORT *LCDXlatVCLK1 = NULL;
-  const USHORT *LCDXlatVCLK2 = NULL;
-  const USHORT *LVDSXlatVCLK2 = NULL;
-  const USHORT *LVDSXlatVCLK3 = NULL;
-
-  if(HwDeviceExtension->jChipType >= SIS_315H) {
-#ifdef SIS315H
-		LCDXlatVCLK1 = LCDXlat1VCLK310;
-		LCDXlatVCLK2 = LCDXlat2VCLK310;
-		LVDSXlatVCLK2 = LVDSXlat2VCLK310;
-		LVDSXlatVCLK3 = LVDSXlat3VCLK310;
-#endif
+     }
+  } else if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+     if(tempbx <= 800) {
+        temp = 0x80;
+     }
   } else {
-#ifdef SIS300
-		LCDXlatVCLK1 = LCDXlat1VCLK300;
-		LCDXlatVCLK2 = LCDXlat2VCLK300;
-		LVDSXlatVCLK2 = LVDSXlat2VCLK300;
-		LVDSXlatVCLK3 = LVDSXlat3VCLK300;
-#endif
+     temp = 0x80;
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
+        temp = 0;
+        if(tempbx > 800) temp = 0x60;
+     }
   }
 
-  if(ModeNo <= 0x13) {
-    	modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
-    	resinfo = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ResInfo;
-    	CRT2Index = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_CRT2CRTC;
-  } else {
-    	modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
-    	resinfo = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO;
-    	CRT2Index = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
+  if(SiS_Pr->SiS_TVMode & (TVSetYPbPr525p | TVSetYPbPr750p)) {
+     temp = 0;
+     if(SiS_Pr->SiS_VGAHDE == 1024) temp = 0x20;
   }
 
-  if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {    /* 30x/B/LV */
-
-     if(SiS_Pr->SiS_SetFlag & ProgrammingCRT2) {
+  if(HwInfo->jChipType < SIS_661) {
+     if(SiS_IsDualLink(SiS_Pr, HwInfo)) temp = 0;
+  }
 
-        CRT2Index >>= 6;
-        if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {      /*  LCD */
-            if(HwDeviceExtension->jChipType < SIS_315H) {
-	       if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel800x600) {
-	    		VCLKIndex = LCDXlat0VCLK[CRT2Index];
-	       } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
-	    		VCLKIndex = LCDXlatVCLK1[CRT2Index];
-	       } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x600) {
-	    		VCLKIndex = LCDXlatVCLK1[CRT2Index];
-	       } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1152x768) {
-	    		VCLKIndex = LCDXlatVCLK1[CRT2Index];
-	       } 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 = VCLK108_2_315;
-		  } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768) {
-		     VCLKIndex = VCLK81_315;  	/* guessed */
-		  } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) {
-		     VCLKIndex = VCLK108_2_315;
-		  } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) {
-		     VCLKIndex = VCLK162_315;
-		  } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x960) {
-		     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 {
-		     VCLKIndex = LCDXlatVCLK2[CRT2Index];
-      	          }
-	       } else {
-                   VCLKIndex = (UCHAR)SiS_GetReg2((USHORT)(SiS_Pr->SiS_P3ca+0x02));  /*  Port 3cch */
-         	   VCLKIndex = ((VCLKIndex >> 2) & 0x03);
-        	   if(ModeNo > 0x13) {
-          		VCLKIndex = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
-        	   }
-		   if(ModeNo <= 0x13) {
-		      if(HwDeviceExtension->jChipType <= SIS_315PRO) {
-		         if(SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_CRT2CRTC == 1) VCLKIndex = 0x42;
-	              } else {
-		         if(SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_CRT2CRTC == 1) VCLKIndex = 0x00;
-		      }
-		   }
-		   if(HwDeviceExtension->jChipType <= SIS_315PRO) {
-		      if(VCLKIndex == 0) VCLKIndex = 0x41;
-		      if(VCLKIndex == 1) VCLKIndex = 0x43;
-		      if(VCLKIndex == 4) VCLKIndex = 0x44;
-		   }
-	       }
-	    }
-        } else if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {                 /*  TV */
-        	if( (SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) && 
-		    (!(SiS_Pr->SiS_VBType & VB_SIS301LV302LV)) ) {
-          		if(SiS_Pr->SiS_SetFlag & RPLLDIV2XO)  VCLKIndex = HiTVVCLKDIV2;
-     			else                                  VCLKIndex = HiTVVCLK;
-          		if(SiS_Pr->SiS_SetFlag & TVSimuMode) {
-            			if(modeflag & Charx8Dot)      VCLKIndex = HiTVSimuVCLK;
-            			else 			      VCLKIndex = HiTVTextVCLK;
-          		}
-        	} else {
-       			if(SiS_Pr->SiS_SetFlag & RPLLDIV2XO)  VCLKIndex = TVVCLKDIV2;
-            		else         		              VCLKIndex = TVVCLK;
-          	}
-		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);
-        	if(ModeNo > 0x13) {
-          		VCLKIndex = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
-			if(HwDeviceExtension->jChipType < SIS_315H) {
-          			VCLKIndex &= 0x3f;
-				if( (HwDeviceExtension->jChipType == SIS_630) &&
-				    (HwDeviceExtension->jChipRevision >= 0x30)) {
-				     /* This is certainly wrong: It replaces clock
-				      * 108 by 47...
-				      */
-				     /* if(VCLKIndex == 0x14) VCLKIndex = 0x2e; */
-				     if(VCLKIndex == 0x14) VCLKIndex = 0x34;
-				}
-			}
-        	}
-        }
+  if(SiS_Pr->SiS_VBType & VB_SIS301) {
+     if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1280x1024)
+        temp |= 0x0A;
+  }
 
-    } else {   /* If not programming CRT2 */
+  SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x0E,0x10,temp);
 
-        VCLKIndex = (UCHAR)SiS_GetReg2((USHORT)(SiS_Pr->SiS_P3ca+0x02));
-        VCLKIndex = ((VCLKIndex >> 2) & 0x03);
-        if(ModeNo > 0x13) {
-             VCLKIndex = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
-	     if(HwDeviceExtension->jChipType < SIS_315H) {
-                VCLKIndex &= 0x3f;
-		if( (HwDeviceExtension->jChipType != SIS_630) &&
-		    (HwDeviceExtension->jChipType != SIS_300) ) {
-		   if(VCLKIndex == 0x1b) VCLKIndex = 0x35;
-		}
-	     }
-        }
-    }
+  tempebx = SiS_Pr->SiS_VDE;
 
-  } else {       /*   LVDS  */
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) {
+     if(!(temp & 0xE0)) tempebx >>=1;
+  }
 
-    	VCLKIndex = CRT2Index;
+  tempcx = SiS_Pr->SiS_RVBHRS;
+  temp = tempcx & 0x00FF;
+  SiS_SetReg(SiS_Pr->SiS_Part4Port,0x18,temp);
 
-	if(SiS_Pr->SiS_SetFlag & ProgrammingCRT2) {  /* programming CRT2 */
+  tempeax = SiS_Pr->SiS_VGAVDE;
+  tempcx |= 0x4000;
+  if(tempeax <= tempebx) {
+     tempcx ^= 0x4000;
+  } else {
+     tempeax -= tempebx;
+  }
 
-	   if( (SiS_Pr->SiS_IF_DEF_CH70xx != 0) && (SiS_Pr->SiS_VBInfo & SetCRT2ToTV) ) {
+  templong = (tempeax * 256 * 1024) % tempebx;
+  tempeax = (tempeax * 256 * 1024) / tempebx;
+  tempebx = tempeax;
+  if(templong != 0) tempebx++;
 
-		VCLKIndex &= 0x1f;
-        	tempbx = 0;
-		if(SiS_Pr->SiS_VBInfo & SetCHTVOverScan) tempbx += 1;
-        	if(SiS_Pr->SiS_VBInfo & SetPALTV) {
-			tempbx += 2;
-			if(SiS_Pr->SiS_ModeType > ModeVGA) {
-			   if(SiS_Pr->SiS_CHSOverScan) tempbx = 8;
-			}
-			if(SiS_Pr->SiS_CHPALM) {
-				tempbx = 4;
-				if(SiS_Pr->SiS_VBInfo & SetCHTVOverScan) tempbx += 1;
-			} else if(SiS_Pr->SiS_CHPALN) {
-				tempbx = 6;
-				if(SiS_Pr->SiS_VBInfo & SetCHTVOverScan) tempbx += 1;
-			}
-		}
-       		switch(tempbx) {
-          	   case  0: CHTVVCLKPtr = SiS_Pr->SiS_CHTVVCLKUNTSC;  break;
-         	   case  1: CHTVVCLKPtr = SiS_Pr->SiS_CHTVVCLKONTSC;  break;
-                   case  2: CHTVVCLKPtr = SiS_Pr->SiS_CHTVVCLKUPAL;   break;
-                   case  3: CHTVVCLKPtr = SiS_Pr->SiS_CHTVVCLKOPAL;   break;
-		   case  4: CHTVVCLKPtr = SiS_Pr->SiS_CHTVVCLKUPALM;  break;
-         	   case  5: CHTVVCLKPtr = SiS_Pr->SiS_CHTVVCLKOPALM;  break;
-                   case  6: CHTVVCLKPtr = SiS_Pr->SiS_CHTVVCLKUPALN;  break;
-                   case  7: CHTVVCLKPtr = SiS_Pr->SiS_CHTVVCLKOPALN;  break;
-		   case  8: CHTVVCLKPtr = SiS_Pr->SiS_CHTVVCLKSOPAL;  break;
-		   default: CHTVVCLKPtr = SiS_Pr->SiS_CHTVVCLKOPAL;   break;
-        	}
-        	VCLKIndex = CHTVVCLKPtr[VCLKIndex];
-
-	   } else if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
-
-	        VCLKIndex >>= 6;
-     		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)
-                        VCLKIndex = LVDSXlatVCLK2[VCLKIndex];
-		else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1152x768)
-                        VCLKIndex = LVDSXlatVCLK2[VCLKIndex];			
-     		else    VCLKIndex = LVDSXlatVCLK3[VCLKIndex];
-
-		if(SiS_Pr->SiS_CustomT == CUT_BARCO1366) {
-		   /* Special Timing: Barco iQ Pro R300/400/... */
-		   VCLKIndex = 0x44;
-		}
+  temp = (USHORT)(tempebx & 0x000000FF);
+  SiS_SetReg(SiS_Pr->SiS_Part4Port,0x1B,temp);
+  temp = (USHORT)((tempebx & 0x0000FF00) >> 8);
+  SiS_SetReg(SiS_Pr->SiS_Part4Port,0x1A,temp);
 
-		if(SiS_Pr->SiS_CustomT == CUT_PANEL848) {
-		   if(HwDeviceExtension->jChipType < SIS_315H) {
-		      VCLKIndex = VCLK34_300;
-		      /* if(resinfo == SIS_RI_1360x768) VCLKIndex = ?; */
-		   } else {
-		      VCLKIndex = VCLK34_315;
-		      /* if(resinfo == SIS_RI_1360x768) VCLKIndex = ?; */
-		   }
-		}
+  tempbx = (USHORT)(tempebx >> 16);
+  temp = tempbx & 0x00FF;
+  temp <<= 4;
+  temp |= ((tempcx & 0xFF00) >> 8);
+  SiS_SetReg(SiS_Pr->SiS_Part4Port,0x19,temp);
 
-	   } else {
+  if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
 
-	        VCLKIndex = (UCHAR)SiS_GetReg2((USHORT)(SiS_Pr->SiS_P3ca+0x02));
-                VCLKIndex = ((VCLKIndex >> 2) & 0x03);
-                if(ModeNo > 0x13) {
-                     VCLKIndex = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
-		     if(HwDeviceExtension->jChipType < SIS_315H) {
-    		        VCLKIndex &= 0x3F;
-                     }
-		     if( (HwDeviceExtension->jChipType == SIS_630) &&
-                         (HwDeviceExtension->jChipRevision >= 0x30) ) {
-		         	if(VCLKIndex == 0x14) VCLKIndex = 0x2e;
-		     }
-	        }
-	   }
+     SiS_SetReg(SiS_Pr->SiS_Part4Port,0x1C,0x28);
 
-	} else {  /* if not programming CRT2 */
+     tempbx = 0;
+     tempax = SiS_Pr->SiS_VGAHDE;
+     if(modeflag & HalfDCLK) 		tempax >>= 1;
+     if(SiS_IsDualLink(SiS_Pr, HwInfo)) tempax >>= 1;
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
+	if(tempax > 800) tempax -= 800;
+     } else {
+        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) & 0x03;
+     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_SetReg(SiS_Pr->SiS_Part4Port,0x1D,tempax & 0x00FF);
+     temp <<= 4;
+     temp |= tempbx;
+     SiS_SetReg(SiS_Pr->SiS_Part4Port,0x1E,temp);
 
-	   VCLKIndex = (UCHAR)SiS_GetReg2((USHORT)(SiS_Pr->SiS_P3ca+0x02));
-           VCLKIndex = ((VCLKIndex >> 2) & 0x03);
-           if(ModeNo > 0x13) {
-              VCLKIndex = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
-              if(HwDeviceExtension->jChipType < SIS_315H) {
-	         VCLKIndex &= 0x3F;
-	         if( (HwDeviceExtension->jChipType != SIS_630) &&
-		     (HwDeviceExtension->jChipType != SIS_300) ) {
-		        if(VCLKIndex == 0x1b) VCLKIndex = 0x35;
-	         }
-#if 0		 
-		 if(HwDeviceExtension->jChipType == SIS_730) {
-		    if(VCLKIndex == 0x0b) VCLKIndex = 0x40;   /* 1024x768-70 */
-		    if(VCLKIndex == 0x0d) VCLKIndex = 0x41;   /* 1024x768-75 */ 
-		 }
-#endif		 
+     if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
+	if(IS_SIS550650740660) {
+	   temp = 0x0026;  /* 1.10.7w; 1.10.8r; needs corresponding code in Dis/EnableBridge! */
+	} else {
+	   temp = 0x0036;
+	}
+     } else {
+	temp = 0x0036;
+     }
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+        if(!(SiS_Pr->SiS_TVMode & (TVSetNTSC1024 | TVSetHiVision | TVSetYPbPr750p | TVSetYPbPr525p))) {
+	   temp |= 0x01;
+	   if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
+	      if(!(SiS_Pr->SiS_TVMode & TVSetTVSimuMode)) {
+  	         temp &= 0xFE;
 	      }
 	   }
-
 	}
+     }
+     SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x1F,0xC0,temp);
 
-  }
-#ifdef TWDEBUG
-  xf86DrvMsg(0, X_INFO, "VCLKIndex %d (0x%x)\n", VCLKIndex, VCLKIndex);
+     tempbx = SiS_Pr->SiS_HT;
+     if(SiS_IsDualLink(SiS_Pr, HwInfo)) tempbx >>= 1;
+     tempbx >>= 1;
+     tempbx -= 2;
+     temp = ((tempbx & 0x0700) >> 8) << 3;
+     SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x21,0xC0,temp);
+     temp = tempbx & 0x00FF;
+     SiS_SetReg(SiS_Pr->SiS_Part4Port,0x22,temp);
+
+     if(SiS_Pr->SiS_VBType & VB_SIS301LV302LV) {
+	if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
+           SiS_SetReg(SiS_Pr->SiS_Part4Port,0x24,0x0e);
+	   /* LCD-too-dark-error-source, see FinalizeLCD() */
+	}
+	if(HwInfo->jChipType >= SIS_315H) {
+	   if(SiS_IsDualLink(SiS_Pr, HwInfo)) {
+	      SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x27,0x2c);
+	   }
+	}
+#ifdef SET_EMI
+	if(SiS_Pr->SiS_VBType & (VB_SIS302LV | VB_SIS302ELV)) {
+	   SiS_SetReg(SiS_Pr->SiS_Part4Port,0x2a,0x00);
+	   SiS_SetReg(SiS_Pr->SiS_Part4Port,0x30,0x00);
+	   SiS_SetReg(SiS_Pr->SiS_Part4Port,0x34,0x10);
+	}
 #endif
-  return(VCLKIndex);
+     }
+
+  }  /* 301B */
+
+  SiS_SetCRT2VCLK(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex, HwInfo);
 }
 
+/*********************************************/
+/*         SET PART 5 REGISTER GROUP         */
+/*********************************************/
+
 /* 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)
+static void
+SiS_SetGroup5(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,
+              PSIS_HW_INFO HwInfo)
 {
 
   if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)  return;
 
   if(SiS_Pr->SiS_ModeType == ModeVGA) {
-     if(!(SiS_Pr->SiS_VBInfo & (SetInSlaveMode | LoadDACFlag))){
+     if(!(SiS_Pr->SiS_VBInfo & (SetInSlaveMode | LoadDACFlag))) {
         SiS_EnableCRT2(SiS_Pr);
-        SiS_LoadDAC(SiS_Pr,HwDeviceExtension,ROMAddr,ModeNo,ModeIdIndex);
+        SiS_LoadDAC(SiS_Pr, HwInfo, ModeNo, ModeIdIndex);
      }
   }
 }
 
-void
-SiS_ModCRT1CRTC(SiS_Private *SiS_Pr, UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex,
-                USHORT RefreshRateTableIndex,PSIS_HW_DEVICE_INFO HwDeviceExtension)
+/*********************************************/
+/*     MODIFY CRT1 GROUP FOR SLAVE MODE      */
+/*********************************************/
+
+static void
+SiS_ModCRT1CRTC(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,
+                USHORT RefreshRateTableIndex, PSIS_HW_INFO HwInfo)
 {
-  USHORT temp,tempah,i,modeflag,j;
+  USHORT tempah,i,modeflag,j;
   USHORT ResIndex,DisplayType;
   const SiS_LVDSCRT1DataStruct *LVDSCRT1Ptr=NULL;
 
@@ -8943,12 +8543,10 @@
      (SiS_Pr->SiS_CustomT == CUT_PANEL848))
      return;
 
-  temp = SiS_GetLVDSCRT1Ptr(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,RefreshRateTableIndex,
-                            &ResIndex,&DisplayType);
+  if(!(SiS_GetLVDSCRT1Ptr(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex,
+                          &ResIndex, &DisplayType))) return;
 
-  if(temp == 0) return;
-
-  if(HwDeviceExtension->jChipType < SIS_315H) {
+  if(HwInfo->jChipType < SIS_315H) {
      if(SiS_Pr->SiS_SetFlag & SetDOSMode) return;
   }
 
@@ -9005,27 +8603,27 @@
   SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x11,0x7f);                        /*unlock cr0-7  */
 
   tempah = (LVDSCRT1Ptr + ResIndex)->CR[0];
-  SiS_SetReg1(SiS_Pr->SiS_P3d4,0x00,tempah);
+  SiS_SetReg(SiS_Pr->SiS_P3d4,0x00,tempah);
 
   for(i=0x02,j=1;i<=0x05;i++,j++){
     tempah = (LVDSCRT1Ptr + ResIndex)->CR[j];
-    SiS_SetReg1(SiS_Pr->SiS_P3d4,i,tempah);
+    SiS_SetReg(SiS_Pr->SiS_P3d4,i,tempah);
   }
   for(i=0x06,j=5;i<=0x07;i++,j++){
     tempah = (LVDSCRT1Ptr + ResIndex)->CR[j];
-    SiS_SetReg1(SiS_Pr->SiS_P3d4,i,tempah);
+    SiS_SetReg(SiS_Pr->SiS_P3d4,i,tempah);
   }
   for(i=0x10,j=7;i<=0x11;i++,j++){
     tempah = (LVDSCRT1Ptr + ResIndex)->CR[j];
-    SiS_SetReg1(SiS_Pr->SiS_P3d4,i,tempah);
+    SiS_SetReg(SiS_Pr->SiS_P3d4,i,tempah);
   }
   for(i=0x15,j=9;i<=0x16;i++,j++){
     tempah = (LVDSCRT1Ptr + ResIndex)->CR[j];
-    SiS_SetReg1(SiS_Pr->SiS_P3d4,i,tempah);
+    SiS_SetReg(SiS_Pr->SiS_P3d4,i,tempah);
   }
   for(i=0x0A,j=11;i<=0x0C;i++,j++){
     tempah = (LVDSCRT1Ptr + ResIndex)->CR[j];
-    SiS_SetReg1(SiS_Pr->SiS_P3c4,i,tempah);
+    SiS_SetReg(SiS_Pr->SiS_P3c4,i,tempah);
   }
 
   tempah = (LVDSCRT1Ptr + ResIndex)->CR[14];
@@ -9045,114 +8643,15 @@
   }
 }
 
-BOOLEAN
-SiS_GetLVDSCRT1Ptr(SiS_Private *SiS_Pr, UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex,
-		   USHORT RefreshRateTableIndex,USHORT *ResIndex,
-		   USHORT *DisplayType)
- {
-  USHORT tempbx,modeflag=0;
-  USHORT Flag,CRT2CRTC;
-
-  if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
-     if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)) {
-        if(!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) return 0;
-     }
-  } else {
-     if(!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) return 0;
-  }
-
-  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[RefreshRateTableIndex].Ext_CRT2CRTC;
-  }
-
-  Flag = 1;
-  tempbx = 0;
-  if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
-     if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD)) {
-        Flag = 0;
-        tempbx = 18;
-        if(SiS_Pr->SiS_VBInfo & SetCHTVOverScan) tempbx++;
-        if(SiS_Pr->SiS_VBInfo & SetPALTV) {
-      	   tempbx += 2;
-	   if(SiS_Pr->SiS_ModeType > ModeVGA) {
-	      if(SiS_Pr->SiS_CHSOverScan) tempbx = 99;
-	   }
-	   if(SiS_Pr->SiS_CHPALM) {
-	      tempbx = 18;  /* PALM uses NTSC data */
-	      if(SiS_Pr->SiS_VBInfo & SetCHTVOverScan) tempbx++;
-	   } else if(SiS_Pr->SiS_CHPALN) {
-	      tempbx = 20;  /* PALN uses PAL data  */
-	      if(SiS_Pr->SiS_VBInfo & SetCHTVOverScan) tempbx++;
-	   }
-        }
-     }
-  }
-  if(Flag) {
-     tempbx = SiS_Pr->SiS_LCDResInfo;
-     tempbx -= SiS_Pr->SiS_PanelMinLVDS;
-     if(SiS_Pr->SiS_LCDResInfo <= SiS_Pr->SiS_Panel1280x1024) {
-        if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) tempbx += 6;
-        if(modeflag & HalfDCLK) tempbx += 3;
-     } else {
-        if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) {
-           tempbx = 14;
-	   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) tempbx += 2;
-	   if(modeflag & HalfDCLK) tempbx++;
-        } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x600) {
-           tempbx = 23;
-	   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) tempbx += 2;
-	   if(modeflag & HalfDCLK) tempbx++;
-        } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1152x768) {
-           tempbx = 27;
-	   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) tempbx += 2;
-	   if(modeflag & HalfDCLK) tempbx++;
-        } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) {
-           tempbx = 36;
-	   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) tempbx += 2;
-	   if(modeflag & HalfDCLK) tempbx++;
-        } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x768) {
-           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 0
-  if(SiS_Pr->SiS_IF_DEF_FSTN) {
-     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel320x480){
-        tempbx = 22;
-     }
-  }
-#endif
-
-  *ResIndex = CRT2CRTC & 0x3F;
-  *DisplayType = tempbx;
-  return 1;
-}
+/*********************************************/
+/*              SET CRT2 ECLK                */
+/*********************************************/
 
-void
-SiS_SetCRT2ECLK(SiS_Private *SiS_Pr, UCHAR *ROMAddr, USHORT ModeNo,USHORT ModeIdIndex,
-           USHORT RefreshRateTableIndex,PSIS_HW_DEVICE_INFO HwDeviceExtension)
+static void
+SiS_SetCRT2ECLK(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,
+           USHORT RefreshRateTableIndex, PSIS_HW_INFO HwInfo)
 {
+  UCHAR  *ROMAddr = HwInfo->pjVirtualRomBase;
   USHORT clkbase, vclkindex=0;
   UCHAR  sr2b, sr2c;
 
@@ -9161,12 +8660,12 @@
         if((SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRTVCLK & 0x3f) == 2) {
 	   RefreshRateTableIndex--;
 	}
-	vclkindex = SiS_GetVCLK2Ptr(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,
-                                    RefreshRateTableIndex,HwDeviceExtension);
+	vclkindex = SiS_GetVCLK2Ptr(SiS_Pr, ModeNo, ModeIdIndex,
+                                    RefreshRateTableIndex, HwInfo);
 	SiS_Pr->SiS_SetFlag |= ProgrammingCRT2;
   } else {
-        vclkindex = SiS_GetVCLK2Ptr(SiS_Pr,ROMAddr,ModeNo,ModeIdIndex,
-                                    RefreshRateTableIndex,HwDeviceExtension);
+        vclkindex = SiS_GetVCLK2Ptr(SiS_Pr, ModeNo, ModeIdIndex,
+                                    RefreshRateTableIndex, HwInfo);
   }
 
   sr2b = SiS_Pr->SiS_VCLKData[vclkindex].SR2B;
@@ -9188,42 +8687,24 @@
      }
   }
 
-  SiS_SetReg1(SiS_Pr->SiS_P3c4,0x05,0x86);
-  SiS_SetReg1(SiS_Pr->SiS_P3c4,0x31,0x20);
-  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);
-  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);
-  SiS_SetReg1(SiS_Pr->SiS_P3c4,clkbase,sr2b);
-  SiS_SetReg1(SiS_Pr->SiS_P3c4,clkbase+1,sr2c);
-}
-
-#if 0  /* Not used */
-void
-SiS_SetDefCRT2ExtRegs(SiS_Private *SiS_Pr, USHORT BaseAddr)
-{
-  USHORT  temp;
-
-  if(SiS_Pr->SiS_IF_DEF_LVDS==0) {
-    SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x02,0x40);
-    SiS_SetReg1(SiS_Pr->SiS_Part4Port,0x10,0x80);
-    temp=(UCHAR)SiS_GetReg1(SiS_Pr->SiS_P3c4,0x16);
-    temp &= 0xC3;
-    SiS_SetReg1(SiS_Pr->SiS_P3d4,0x35,temp);
-  } else {
-    SiS_SetReg1(SiS_Pr->SiS_P3d4,0x32,0x02);
-    SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x02,0x00);
-  }
+  SiS_SetReg(SiS_Pr->SiS_P3c4,0x05,0x86);
+  SiS_SetReg(SiS_Pr->SiS_P3c4,0x31,0x20);
+  SiS_SetReg(SiS_Pr->SiS_P3c4,clkbase,sr2b);
+  SiS_SetReg(SiS_Pr->SiS_P3c4,clkbase+1,sr2c);
+  SiS_SetReg(SiS_Pr->SiS_P3c4,0x31,0x10);
+  SiS_SetReg(SiS_Pr->SiS_P3c4,clkbase,sr2b);
+  SiS_SetReg(SiS_Pr->SiS_P3c4,clkbase+1,sr2c);
+  SiS_SetReg(SiS_Pr->SiS_P3c4,0x31,0x00);
+  SiS_SetReg(SiS_Pr->SiS_P3c4,clkbase,sr2b);
+  SiS_SetReg(SiS_Pr->SiS_P3c4,clkbase+1,sr2c);
 }
-#endif
 
-/* Start of Chrontel 70xx functions ---------------------- */
+/*********************************************/
+/*           SET UP CHRONTEL CHIPS           */
+/*********************************************/
 
-/* Set-up the Chrontel Registers */
-void
-SiS_SetCHTVReg(SiS_Private *SiS_Pr, UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex,
+static void
+SiS_SetCHTVReg(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,
                USHORT RefreshRateTableIndex)
 {
   USHORT temp, tempbx, tempcl;
@@ -9236,18 +8717,18 @@
     	tempcl = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
 
   TVType = 0;
-  if(SiS_Pr->SiS_VBInfo & SetCHTVOverScan) TVType += 1;
-  if(SiS_Pr->SiS_VBInfo & SetPALTV) {
+  if(SiS_Pr->SiS_TVMode & TVSetCHOverScan) TVType += 1;
+  if(SiS_Pr->SiS_TVMode & TVSetPAL) {
   	TVType += 2;
 	if(SiS_Pr->SiS_ModeType > ModeVGA) {
 	   if(SiS_Pr->SiS_CHSOverScan) TVType = 8;
 	}
-	if(SiS_Pr->SiS_CHPALM) {
+	if(SiS_Pr->SiS_TVMode & TVSetPALM) {
 		TVType = 4;
-		if(SiS_Pr->SiS_VBInfo & SetCHTVOverScan) TVType += 1;
-	} else if(SiS_Pr->SiS_CHPALN) {
+		if(SiS_Pr->SiS_TVMode & TVSetCHOverScan) TVType += 1;
+	} else if(SiS_Pr->SiS_TVMode & TVSetPALN) {
 		TVType = 6;
-		if(SiS_Pr->SiS_VBInfo & SetCHTVOverScan) TVType += 1;
+		if(SiS_Pr->SiS_TVMode & TVSetCHOverScan) TVType += 1;
 	}
   }
   switch(TVType) {
@@ -9273,7 +8754,7 @@
      /* We don't support modes >800x600 */
      if (resindex > 5) return;
 
-     if(SiS_Pr->SiS_VBInfo & SetPALTV) {
+     if(SiS_Pr->SiS_TVMode & TVSetPAL) {
     	SiS_SetCH700x(SiS_Pr,0x4304);   /* 0x40=76uA (PAL); 0x03=15bit non-multi RGB*/
     	SiS_SetCH700x(SiS_Pr,0x6909);	/* Black level for PAL (105)*/
      } else {
@@ -9318,7 +8799,7 @@
       */
 #ifndef SIS_CP
      SiS_SetCH70xx(SiS_Pr,0x003D);
-#endif     
+#endif
 
      /* Register 0x10 only contains 1 writable bit (S0) for sensing,
             all other bits a read-only. Macrovision?
@@ -9334,12 +8815,12 @@
       */
      SiS_SetCH70xxANDOR(SiS_Pr,0x001C,0xEF);
 
-     if(!(SiS_Pr->SiS_VBInfo & SetPALTV)) {		/* ---- NTSC ---- */
-       if(SiS_Pr->SiS_VBInfo & SetCHTVOverScan) {
+     if(!(SiS_Pr->SiS_TVMode & TVSetPAL)) {		/* ---- NTSC ---- */
+       if(SiS_Pr->SiS_TVMode & TVSetCHOverScan) {
          if(resindex == 0x04) {   			/* 640x480 overscan: Mode 16 */
       	   SiS_SetCH70xxANDOR(SiS_Pr,0x0020,0xEF);   	/* loop filter off */
            SiS_SetCH70xxANDOR(SiS_Pr,0x0121,0xFE);      /* ACIV on, no need to set FSCI */
-         } else  if(resindex == 0x05) {    		/* 800x600 overscan: Mode 23 */
+         } else if(resindex == 0x05) {    		/* 800x600 overscan: Mode 23 */
            SiS_SetCH70xxANDOR(SiS_Pr,0x0118,0xF0);	/* 0x18-0x1f: FSCI 469,762,048 */
            SiS_SetCH70xxANDOR(SiS_Pr,0x0C19,0xF0);
            SiS_SetCH70xxANDOR(SiS_Pr,0x001A,0xF0);
@@ -9372,9 +8853,9 @@
            SiS_SetCH70xxANDOR(SiS_Pr,0x0121,0xFE);
          }
        }
-     } else {				/* ---- PAL ---- */
+     } else {						/* ---- PAL ---- */
            /* We don't play around with FSCI in PAL mode */
-         if (resindex == 0x04) {
+         if(resindex == 0x04) {
            SiS_SetCH70xxANDOR(SiS_Pr,0x0020,0xEF); 	/* loop filter off */
            SiS_SetCH70xxANDOR(SiS_Pr,0x0121,0xFE);      /* ACIV on */
          } else {
@@ -9395,69 +8876,85 @@
      if (resindex > 6) return;
 
      temp = CHTVRegData[resindex].Reg[0];
-     tempbx=((temp & 0x00FF) <<8 ) | 0x00;
+     if(SiS_Pr->SiS_TVMode & TVSetNTSCJ) {
+        temp |= 0x10;
+     }
+     tempbx=((temp & 0x00FF) << 8) | 0x00;
      SiS_SetCH701x(SiS_Pr,tempbx);
 
      temp = CHTVRegData[resindex].Reg[1];
-     tempbx=((temp & 0x00FF) <<8 ) | 0x01;
+     tempbx=((temp & 0x00FF) << 8) | 0x01;
      SiS_SetCH701x(SiS_Pr,tempbx);
 
      temp = CHTVRegData[resindex].Reg[2];
-     tempbx=((temp & 0x00FF) <<8 ) | 0x02;
+     tempbx=((temp & 0x00FF) << 8) | 0x02;
      SiS_SetCH701x(SiS_Pr,tempbx);
 
      temp = CHTVRegData[resindex].Reg[3];
-     tempbx=((temp & 0x00FF) <<8 ) | 0x04;
+     tempbx=((temp & 0x00FF) << 8) | 0x04;
      SiS_SetCH701x(SiS_Pr,tempbx);
 
      temp = CHTVRegData[resindex].Reg[4];
-     tempbx=((temp & 0x00FF) <<8 ) | 0x03;
+     tempbx=((temp & 0x00FF) << 8) | 0x03;
      SiS_SetCH701x(SiS_Pr,tempbx);
 
      temp = CHTVRegData[resindex].Reg[5];
-     tempbx=((temp & 0x00FF) <<8 ) | 0x05;
+     tempbx=((temp & 0x00FF) << 8) | 0x05;
      SiS_SetCH701x(SiS_Pr,tempbx);
 
      temp = CHTVRegData[resindex].Reg[6];
-     tempbx=((temp & 0x00FF) <<8 ) | 0x06;
+     tempbx=((temp & 0x00FF) << 8) | 0x06;
      SiS_SetCH701x(SiS_Pr,tempbx);
 
      temp = CHTVRegData[resindex].Reg[7];
-     tempbx=((temp & 0x00FF) <<8 ) | 0x07;
+     if(SiS_Pr->SiS_TVMode & TVSetNTSCJ) {
+	temp = 0x66;
+     }
+     tempbx=((temp & 0x00FF) << 8) | 0x07;
      SiS_SetCH701x(SiS_Pr,tempbx);
 
      temp = CHTVRegData[resindex].Reg[8];
-     tempbx=((temp & 0x00FF) <<8 ) | 0x08;
+     tempbx=((temp & 0x00FF) << 8) | 0x08;
      SiS_SetCH701x(SiS_Pr,tempbx);
 
      temp = CHTVRegData[resindex].Reg[9];
-     tempbx=((temp & 0x00FF) <<8 ) | 0x15;
+     tempbx=((temp & 0x00FF) << 8) | 0x15;
      SiS_SetCH701x(SiS_Pr,tempbx);
 
      temp = CHTVRegData[resindex].Reg[10];
-     tempbx=((temp & 0x00FF) <<8 ) | 0x1f;
+     tempbx=((temp & 0x00FF) << 8) | 0x1f;
      SiS_SetCH701x(SiS_Pr,tempbx);
 
      temp = CHTVRegData[resindex].Reg[11];
-     tempbx=((temp & 0x00FF) <<8 ) | 0x0c;
+     tempbx=((temp & 0x00FF) << 8) | 0x0c;
      SiS_SetCH701x(SiS_Pr,tempbx);
 
      temp = CHTVRegData[resindex].Reg[12];
-     tempbx=((temp & 0x00FF) <<8 ) | 0x0d;
+     tempbx=((temp & 0x00FF) << 8) | 0x0d;
      SiS_SetCH701x(SiS_Pr,tempbx);
 
      temp = CHTVRegData[resindex].Reg[13];
-     tempbx=((temp & 0x00FF) <<8 ) | 0x0e;
+     tempbx=((temp & 0x00FF) << 8) | 0x0e;
      SiS_SetCH701x(SiS_Pr,tempbx);
 
      temp = CHTVRegData[resindex].Reg[14];
-     tempbx=((temp & 0x00FF) <<8 ) | 0x0f;
+     tempbx=((temp & 0x00FF) << 8) | 0x0f;
      SiS_SetCH701x(SiS_Pr,tempbx);
 
      temp = CHTVRegData[resindex].Reg[15];
-     tempbx=((temp & 0x00FF) <<8 ) | 0x10;
+     tempbx=((temp & 0x00FF) << 8) | 0x10;
      SiS_SetCH701x(SiS_Pr,tempbx);
-     
+
+     temp = SiS_GetCH701x(SiS_Pr,0x21) & ~0x02;
+     /* D1 should be set for PAL, PAL-N and NTSC-J,
+        but I won't do that for PAL unless somebody
+	tells me to do so. Since the BIOS uses
+	non-default CIV values and blacklevels,
+	this might be compensated anyway.
+      */
+     if(SiS_Pr->SiS_TVMode & (TVSetPALN | TVSetNTSCJ)) temp |= 0x02;
+     SiS_SetCH701x(SiS_Pr,((temp << 8) | 0x21));
+
 #endif	/* 315 */
 
   }
@@ -9468,16 +8965,14 @@
 
 }
 
-/* Chrontel 701x functions ================================= */
-
 void
-SiS_Chrontel701xBLOn(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension)
+SiS_Chrontel701xBLOn(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
 {
   USHORT temp;
 
   /* Enable Chrontel 7019 LCD panel backlight */
   if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
-     if(HwDeviceExtension->jChipType == SIS_740) {
+     if(HwInfo->jChipType == SIS_740) {
         SiS_SetCH701x(SiS_Pr,0x6566);
      } else {
         temp = SiS_GetCH701x(SiS_Pr,0x66);
@@ -9487,23 +8982,63 @@
   }
 }
 
-void
-SiS_Chrontel701xBLOff(SiS_Private *SiS_Pr)
-{
-  USHORT temp;
+void
+SiS_Chrontel701xBLOff(SiS_Private *SiS_Pr)
+{
+  USHORT temp;
+
+  /* Disable Chrontel 7019 LCD panel backlight */
+  if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
+     temp = SiS_GetCH701x(SiS_Pr,0x66);
+     temp &= 0xDF;
+     SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x66);
+  }
+}
+
+#ifdef SIS315H  /* ----------- 315 series only ---------- */
+
+static void
+SiS_ChrontelPowerSequencing(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
+{
+  UCHAR regtable[]      = { 0x67, 0x68, 0x69, 0x6a, 0x6b };
+  UCHAR table1024_740[] = { 0x01, 0x02, 0x01, 0x01, 0x01 };
+  UCHAR table1400_740[] = { 0x01, 0x6e, 0x01, 0x01, 0x01 };
+  UCHAR asus1024_740[]  = { 0x19, 0x6e, 0x01, 0x19, 0x09 };
+  UCHAR asus1400_740[]  = { 0x19, 0x6e, 0x01, 0x19, 0x09 };
+  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 */
 
-  /* Disable Chrontel 7019 LCD panel backlight */
-  if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
-        temp = SiS_GetCH701x(SiS_Pr,0x66);
-        temp &= 0xDF;
-	SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x66);
+  if(HwInfo->jChipType == SIS_740) {
+     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
+        if(SiS_Pr->SiS_CustomT == CUT_ASUSL3000D) tableptr = asus1024_740;
+        else    			          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)) {
+	if(SiS_Pr->SiS_CustomT == CUT_ASUSL3000D) tableptr = asus1400_740;
+        else					  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;
   }
-}
 
-#ifdef SIS315H  /* ----------- 315 series only ---------- */
+  for(i=0; i<5; i++) {
+     SiS_SetCH701x(SiS_Pr,(tableptr[i] << 8) | regtable[i]);
+  }
+}
 
-void
-SiS_SetCH701xForLCD(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,USHORT BaseAddr)
+static void
+SiS_SetCH701xForLCD(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
 {
   UCHAR regtable[]      = { 0x1c, 0x5f, 0x64, 0x6f, 0x70, 0x71,
                             0x72, 0x73, 0x74, 0x76, 0x78, 0x7d, 0x66 };
@@ -9527,7 +9062,7 @@
   USHORT tempbh;
   int i;
 
-  if(HwDeviceExtension->jChipType == SIS_740) {
+  if(HwInfo->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) {
@@ -9558,11 +9093,11 @@
         if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) return;
 	if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) return;
      } else if(tempbh == 0xde) {
-        if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) return;
+        if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1600x1200) return;
      }
   }
 
-  if(HwDeviceExtension->jChipType == SIS_740) {
+  if(HwInfo->jChipType == SIS_740) {
      tempbh = 0x0d;
   } else {
      tempbh = 0x0c;
@@ -9570,16 +9105,16 @@
   for(i = 0; i < tempbh; i++) {
      SiS_SetCH701x(SiS_Pr,(tableptr[i] << 8) | regtable[i]);
   }
-  SiS_ChrontelPowerSequencing(SiS_Pr,HwDeviceExtension);
+  SiS_ChrontelPowerSequencing(SiS_Pr,HwInfo);
   tempbh = SiS_GetCH701x(SiS_Pr,0x1e);
   tempbh |= 0xc0;
   SiS_SetCH701x(SiS_Pr,(tempbh << 8) | 0x1e);
 
-  if(HwDeviceExtension->jChipType == SIS_740) {
+  if(HwInfo->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);
+     SiS_SetReg(SiS_Pr->SiS_Part1Port,0x2d,0x03);
      tempbh = SiS_GetCH701x(SiS_Pr,0x64);
      tempbh |= 0x40;
      SiS_SetCH701x(SiS_Pr,(tempbh << 8) | 0x64);
@@ -9589,72 +9124,53 @@
   }
 }
 
-void
-SiS_ChrontelPowerSequencing(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension)
+static void
+SiS_ChrontelResetVSync(SiS_Private *SiS_Pr)
 {
-  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 */
+  unsigned char temp, temp1;
 
-  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]);
-  }
+  temp1 = SiS_GetCH701x(SiS_Pr,0x49);
+  SiS_SetCH701x(SiS_Pr,0x3e49);
+  temp = SiS_GetCH701x(SiS_Pr,0x47);
+  temp &= 0x7f;	/* Use external VSYNC */
+  SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x47);
+  SiS_LongDelay(SiS_Pr,3);
+  temp = SiS_GetCH701x(SiS_Pr,0x47);
+  temp |= 0x80;	/* Use internal VSYNC */
+  SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x47);
+  SiS_SetCH701x(SiS_Pr,(temp1 << 8) | 0x49);
 }
 
 void
-SiS_Chrontel701xOn(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,USHORT BaseAddr)
+SiS_Chrontel701xOn(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
 {
   USHORT temp;
 
   if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
-     if(HwDeviceExtension->jChipType == SIS_740) {
+     if(HwInfo->jChipType == SIS_740) {
         temp = SiS_GetCH701x(SiS_Pr,0x1c);
-        temp |= 0x04;
+        temp |= 0x04;	/* Invert XCLK phase */
         SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x1c);
      }
-     if(SiS_IsYPbPr(SiS_Pr,HwDeviceExtension, BaseAddr)) {
+     if(SiS_IsYPbPr(SiS_Pr, HwInfo)) {
         temp = SiS_GetCH701x(SiS_Pr,0x01);
 	temp &= 0x3f;
 	temp |= 0x80;	/* Enable YPrPb (HDTV) */
 	SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x01);
      }
-     if(SiS_IsChScart(SiS_Pr,HwDeviceExtension, BaseAddr)) {
+     if(SiS_IsChScart(SiS_Pr, HwInfo)) {
         temp = SiS_GetCH701x(SiS_Pr,0x01);
 	temp &= 0x3f;
 	temp |= 0xc0;	/* Enable SCART + CVBS */
 	SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x01);
      }
-     if(HwDeviceExtension->jChipType == SIS_740) {
-        SiS_ChrontelDoSomething5(SiS_Pr);
-        SiS_SetCH701x(SiS_Pr,0x2049);   			/* Enable TV path */
+     if(HwInfo->jChipType == SIS_740) {
+        SiS_ChrontelResetVSync(SiS_Pr);
+        SiS_SetCH701x(SiS_Pr,0x2049);   /* Enable TV path */
      } else {
-        SiS_SetCH701x(SiS_Pr,0x2049);   			/* Enable TV path */
+        SiS_SetCH701x(SiS_Pr,0x2049);   /* Enable TV path */
         temp = SiS_GetCH701x(SiS_Pr,0x49);
-        if(SiS_IsYPbPr(SiS_Pr,HwDeviceExtension, BaseAddr)) {
+        if(SiS_IsYPbPr(SiS_Pr,HwInfo)) {
            temp = SiS_GetCH701x(SiS_Pr,0x73);
 	   temp |= 0x60;
 	   SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x73);
@@ -9671,13 +9187,13 @@
 }
 
 void
-SiS_Chrontel701xOff(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension)
+SiS_Chrontel701xOff(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
 {
   USHORT temp;
 
   /* Complete power down of LVDS */
   if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
-     if(HwDeviceExtension->jChipType == SIS_740) {
+     if(HwInfo->jChipType == SIS_740) {
         SiS_LongDelay(SiS_Pr,1);
 	SiS_GenericDelay(SiS_Pr,0x16ff);
 	SiS_SetCH701x(SiS_Pr,0xac76);
@@ -9692,34 +9208,18 @@
   }
 }
 
-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);
-     temp &= 0x7f;
-     SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x47);
-     SiS_LongDelay(SiS_Pr,3);
-     temp = SiS_GetCH701x(SiS_Pr,0x47);
-     temp |= 0x80;
-     SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x47);
-     SiS_SetCH701x(SiS_Pr,(temp1 << 8) | 0x49);
-}
-
-void
-SiS_ChrontelResetDB(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension, USHORT BaseAddr)
+static void
+SiS_ChrontelResetDB(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
 {
      USHORT temp;
 
-     if(HwDeviceExtension->jChipType == SIS_740) {
-        temp = SiS_GetCH701x(SiS_Pr,0x4a);
+     if(HwInfo->jChipType == SIS_740) {
+
+        temp = SiS_GetCH701x(SiS_Pr,0x4a);  /* Version ID */
         temp &= 0x01;
-        if(!(temp)) {
+        if(!temp) {
 
-           if(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr)) {
+           if(SiS_WeHaveBacklightCtrl(SiS_Pr,HwInfo)) {
 	      temp = SiS_GetCH701x(SiS_Pr,0x49);
 	      SiS_SetCH701x(SiS_Pr,0x3e49);
 	   }
@@ -9728,13 +9228,14 @@
            SiS_LongDelay(SiS_Pr,1);
            SiS_SetCH701x(SiS_Pr,0x1848);
 
-	   if(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-	      SiS_ChrontelDoSomething5(SiS_Pr);
+	   if(SiS_WeHaveBacklightCtrl(SiS_Pr, HwInfo)) {
+	      SiS_ChrontelResetVSync(SiS_Pr);
 	      SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x49);
 	   }
 
         } else {
 
+	   /* Clear/set/clear GPIO */
            temp = SiS_GetCH701x(SiS_Pr,0x5c);
 	   temp &= 0xef;
 	   SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x5c);
@@ -9746,9 +9247,10 @@
 	   SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x5c);
 	   temp = SiS_GetCH701x(SiS_Pr,0x61);
 	   if(!temp) {
-	      SiS_SetCH701xForLCD(SiS_Pr,HwDeviceExtension,BaseAddr);
+	      SiS_SetCH701xForLCD(SiS_Pr, HwInfo);
 	   }
         }
+
      } else { /* 650 */
         /* Reset Chrontel 7019 datapath */
         SiS_SetCH701x(SiS_Pr,0x1048);
@@ -9758,14 +9260,14 @@
 }
 
 void
-SiS_ChrontelDoSomething4(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension, USHORT BaseAddr)
+SiS_ChrontelInitTVVSync(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
 {
      USHORT temp;
 
-     if(HwDeviceExtension->jChipType == SIS_740) {
+     if(HwInfo->jChipType == SIS_740) {
 
-        if(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-           SiS_ChrontelDoSomething5(SiS_Pr);
+        if(SiS_WeHaveBacklightCtrl(SiS_Pr,HwInfo)) {
+           SiS_ChrontelResetVSync(SiS_Pr);
         }
 
      } else {
@@ -9786,21 +9288,20 @@
      }
 }
 
-void
-SiS_ChrontelDoSomething3(SiS_Private *SiS_Pr, USHORT ModeNo, PSIS_HW_DEVICE_INFO HwDeviceExtension,
-                         USHORT BaseAddr)
+static void
+SiS_ChrontelDoSomething3(SiS_Private *SiS_Pr, USHORT ModeNo, PSIS_HW_INFO HwInfo)
 {
      USHORT temp,temp1;
 
-     if(HwDeviceExtension->jChipType == SIS_740) {
+     if(HwInfo->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_SetCH701x(SiS_Pr,0x4566);  /* Panel power on */
+        SiS_SetCH701x(SiS_Pr,0xaf76);  /* All power on */
         SiS_LongDelay(SiS_Pr,1);
         SiS_GenericDelay(SiS_Pr,0x16ff);
 
@@ -9818,7 +9319,7 @@
         temp |= 0x5f;
         SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x66);
         if(ModeNo > 0x13) {
-           if(SiS_WeHaveBacklightCtrl(SiS_Pr,HwDeviceExtension, BaseAddr)) {
+           if(SiS_WeHaveBacklightCtrl(SiS_Pr, HwInfo)) {
 	      SiS_GenericDelay(SiS_Pr,0x3ff);
 	   } else {
 	      SiS_GenericDelay(SiS_Pr,0x2ff);
@@ -9838,8 +9339,8 @@
      }
 }
 
-void
-SiS_ChrontelDoSomething2(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension, USHORT BaseAddr)
+static void
+SiS_ChrontelDoSomething2(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
 {
      USHORT temp,tempcl,tempch;
 
@@ -9849,43 +9350,43 @@
 
      do {
        temp = SiS_GetCH701x(SiS_Pr,0x66);
-       temp &= 0x04;
+       temp &= 0x04;  /* PLL stable? -> bail out */
        if(temp == 0x04) break;
-       
-       if(HwDeviceExtension->jChipType == SIS_740) {
+
+       if(HwInfo->jChipType == SIS_740) {
+          /* Power down LVDS output, PLL normal operation */
           SiS_SetCH701x(SiS_Pr,0xac76);
        }
 
-       SiS_SetCH701xForLCD(SiS_Pr,HwDeviceExtension,BaseAddr);
+       SiS_SetCH701xForLCD(SiS_Pr,HwInfo);
 
        if(tempcl == 0) {
            if(tempch == 3) break;
-	   SiS_ChrontelResetDB(SiS_Pr,HwDeviceExtension,BaseAddr);
+	   SiS_ChrontelResetDB(SiS_Pr,HwInfo);
 	   tempcl = 3;
 	   tempch++;
        }
        tempcl--;
        temp = SiS_GetCH701x(SiS_Pr,0x76);
-       temp &= 0xfb;
+       temp &= 0xfb;  /* Reset PLL */
        SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x76);
        SiS_LongDelay(SiS_Pr,2);
        temp = SiS_GetCH701x(SiS_Pr,0x76);
-       temp |= 0x04;
+       temp |= 0x04;  /* PLL normal operation */
        SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x76);
-       if(HwDeviceExtension->jChipType == SIS_740) {
-          SiS_SetCH701x(SiS_Pr,0xe078);
+       if(HwInfo->jChipType == SIS_740) {
+          SiS_SetCH701x(SiS_Pr,0xe078);	/* PLL loop filter */
        } else {
           SiS_SetCH701x(SiS_Pr,0x6078);
        }
        SiS_LongDelay(SiS_Pr,2);
     } while(0);
 
-    SiS_SetCH701x(SiS_Pr,0x0077);
+    SiS_SetCH701x(SiS_Pr,0x0077);  /* MV? */
 }
 
 void
-SiS_ChrontelDoSomething1(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,
-                         USHORT BaseAddr)
+SiS_ChrontelDoSomething1(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
 {
      USHORT temp;
 
@@ -9893,66 +9394,314 @@
      temp |= 0x80;	/* Set datapath 1 to TV   */
      temp &= 0xbf;	/* Set datapath 2 to LVDS */
      SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x03);
-     
-     if(HwDeviceExtension->jChipType == SIS_740) {
+
+     if(HwInfo->jChipType == SIS_740) {
 
         temp = SiS_GetCH701x(SiS_Pr,0x1c);
-        temp &= 0xfb;
+        temp &= 0xfb;	/* Normal XCLK phase */
         SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x1c);
 
-        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x2d,0x03);
+        SiS_SetReg(SiS_Pr->SiS_Part1Port,0x2d,0x03);
 
         temp = SiS_GetCH701x(SiS_Pr,0x64);
-        temp |= 0x40;
+        temp |= 0x40;	/* ? Bit not defined */
         SiS_SetCH701x(SiS_Pr,(temp << 8) | 0x64);
 
         temp = SiS_GetCH701x(SiS_Pr,0x03);
-        temp &= 0x3f;
+        temp &= 0x3f;	/* D1 input to both LVDS and TV */
         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);
-        }
+	if(SiS_Pr->SiS_CustomT == CUT_ASUSL3000D) {
+	   SiS_SetCH701x(SiS_Pr,0x4063); /* LVDS off */
+	   SiS_LongDelay(SiS_Pr, 1);
+	   SiS_SetCH701x(SiS_Pr,0x0063); /* LVDS on */
+	   SiS_ChrontelResetDB(SiS_Pr, HwInfo);
+	   SiS_ChrontelDoSomething2(SiS_Pr, HwInfo);
+	   SiS_ChrontelDoSomething3(SiS_Pr, 0, HwInfo);
+	} else {
+           temp = SiS_GetCH701x(SiS_Pr,0x66);
+           if(temp != 0x45) {
+              SiS_ChrontelResetDB(SiS_Pr, HwInfo);
+              SiS_ChrontelDoSomething2(SiS_Pr, HwInfo);
+              SiS_ChrontelDoSomething3(SiS_Pr, 0, HwInfo);
+           }
+	}
 
      } else { /* 650 */
 
-        SiS_ChrontelResetDB(SiS_Pr,HwDeviceExtension,BaseAddr);
+        SiS_ChrontelResetDB(SiS_Pr,HwInfo);
+        SiS_ChrontelDoSomething2(SiS_Pr,HwInfo);
+        temp = SiS_GetReg(SiS_Pr->SiS_P3d4,0x34);
+        SiS_ChrontelDoSomething3(SiS_Pr,temp,HwInfo);
+        SiS_SetCH701x(SiS_Pr,0xaf76);  /* All power on, LVDS normal operation */
 
-        SiS_ChrontelDoSomething2(SiS_Pr,HwDeviceExtension,BaseAddr);
+     }
 
-        temp = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x34);
-        SiS_ChrontelDoSomething3(SiS_Pr,temp,HwDeviceExtension,BaseAddr);
+}
+#endif  /* 315 series  */
 
-        SiS_SetCH701x(SiS_Pr,0xaf76);
+/*********************************************/
+/*      MAIN: SET CRT2 REGISTER GROUP        */
+/*********************************************/
 
-     }
+BOOLEAN
+SiS_SetCRT2Group(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo, USHORT ModeNo)
+{
+#ifdef SIS300
+   UCHAR  *ROMAddr  = HwInfo->pjVirtualRomBase;
+#endif
+   USHORT ModeIdIndex, RefreshRateTableIndex;
+#if 0
+   USHORT temp;
+#endif   
+
+   SiS_Pr->SiS_SetFlag |= ProgrammingCRT2;
+
+   if(!SiS_Pr->UseCustomMode) {
+      SiS_SearchModeID(SiS_Pr, &ModeNo, &ModeIdIndex);
+   } else {
+      ModeIdIndex = 0;
+   }
+
+   /* Used for shifting CR33 */
+   SiS_Pr->SiS_SelectCRT2Rate = 4;
+
+   SiS_UnLockCRT2(SiS_Pr, HwInfo);
+
+   RefreshRateTableIndex = SiS_GetRatePtr(SiS_Pr, ModeNo, ModeIdIndex, HwInfo);
+
+   SiS_SaveCRT2Info(SiS_Pr,ModeNo);
+
+   if(SiS_Pr->SiS_SetFlag & LowModeTests) {
+      SiS_DisableBridge(SiS_Pr,HwInfo);
+      if((SiS_Pr->SiS_IF_DEF_LVDS == 1) && (HwInfo->jChipType == SIS_730)) {
+         SiS_SetReg(SiS_Pr->SiS_Part1Port,0x00,0x80);
+      }
+      SiS_SetCRT2ModeRegs(SiS_Pr, ModeNo, ModeIdIndex, HwInfo);
+   }
+
+   if(SiS_Pr->SiS_VBInfo & DisableCRT2Display) {
+      SiS_LockCRT2(SiS_Pr, HwInfo);
+      SiS_DisplayOn(SiS_Pr);
+      return TRUE;
+   }
+
+   SiS_GetCRT2Data(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex, HwInfo);
+
+   /* Set up Panel Link for LVDS, 301BDH and 30xLV(for LCDA) */
+   if( (SiS_Pr->SiS_IF_DEF_LVDS == 1) ||
+       ((SiS_Pr->SiS_VBType & VB_NoLCD) && (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD)) ||
+       ((HwInfo->jChipType >= SIS_315H) && (SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV)) ) {
+      SiS_GetLVDSDesData(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex, HwInfo);
+   } else {
+      SiS_Pr->SiS_LCDHDES = SiS_Pr->SiS_LCDVDES = 0;
+   }
+
+#ifdef LINUX_XF86
+#ifdef TWDEBUG
+  xf86DrvMsg(0, X_INFO, "(init301: LCDHDES 0x%03x LCDVDES 0x%03x)\n", SiS_Pr->SiS_LCDHDES, SiS_Pr->SiS_LCDVDES);
+  xf86DrvMsg(0, X_INFO, "(init301: HDE     0x%03x VDE     0x%03x)\n", SiS_Pr->SiS_HDE, SiS_Pr->SiS_VDE);
+  xf86DrvMsg(0, X_INFO, "(init301: VGAHDE  0x%03x VGAVDE  0x%03x)\n", SiS_Pr->SiS_VGAHDE, SiS_Pr->SiS_VGAVDE);
+  xf86DrvMsg(0, X_INFO, "(init301: HT      0x%03x VT      0x%03x)\n", SiS_Pr->SiS_HT, SiS_Pr->SiS_VT);
+  xf86DrvMsg(0, X_INFO, "(init301: VGAHT   0x%03x VGAVT   0x%03x)\n", SiS_Pr->SiS_VGAHT, SiS_Pr->SiS_VGAVT);
+#endif
+#endif
+
+   if(SiS_Pr->SiS_SetFlag & LowModeTests) {
+      SiS_SetGroup1(SiS_Pr, ModeNo, ModeIdIndex, HwInfo, RefreshRateTableIndex);
+   }
+
+   if(SiS_Pr->SiS_VBType & VB_SISVB) {
+
+        if(SiS_Pr->SiS_SetFlag & LowModeTests) {
+
+	   SiS_SetGroup2(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex, HwInfo);
+#ifdef SIS315H
+	   SiS_SetGroup2_C_ELV(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex, HwInfo);
+#endif
+      	   SiS_SetGroup3(SiS_Pr, ModeNo, ModeIdIndex, HwInfo);
+      	   SiS_SetGroup4(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex, HwInfo);
+      	   SiS_SetGroup5(SiS_Pr, ModeNo, ModeIdIndex, HwInfo);
+
+	   /* 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_SetFlag & SetDOSMode) && ((ModeNo == 0x03) || (ModeNo == 0x10)))) {
+		    if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
+		       SiS_ModCRT1CRTC(SiS_Pr,ModeNo,ModeIdIndex,
+		                       RefreshRateTableIndex,HwInfo);
+		    }
+                 }
+	      }
+	      SiS_SetCRT2ECLK(SiS_Pr,ModeNo,ModeIdIndex,
+		              RefreshRateTableIndex,HwInfo);
+	   }
+        }
+
+   } else {
+
+        if(SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel640x480) {
+	   if(SiS_Pr->SiS_IF_DEF_TRUMPION == 0) {
+    	      SiS_ModCRT1CRTC(SiS_Pr,ModeNo,ModeIdIndex,
+	                      RefreshRateTableIndex,HwInfo);
+	   }
+	}
+
+        SiS_SetCRT2ECLK(SiS_Pr,ModeNo,ModeIdIndex,
+	                RefreshRateTableIndex,HwInfo);
+
+	if(SiS_Pr->SiS_SetFlag & LowModeTests) {
+     	   if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
+	      if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
+	         if(SiS_Pr->SiS_IF_DEF_CH70xx == 2) {
+#ifdef SIS315H		 
+		    SiS_SetCH701xForLCD(SiS_Pr,HwInfo);
+#endif		    
+		 }
+	      }
+	      if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+       		 SiS_SetCHTVReg(SiS_Pr,ModeNo,ModeIdIndex,
+		               RefreshRateTableIndex);
+	      }
+     	   }
+	}
+
+   }
+
+#ifdef SIS300
+   if(HwInfo->jChipType < SIS_315H) {
+      if(SiS_Pr->SiS_SetFlag & LowModeTests) {
+	 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,HwInfo,ModeNo,ModeIdIndex,
+	       			    RefreshRateTableIndex);
+	       }
+	    } else {
+       	       SiS_OEM300Setting(SiS_Pr,HwInfo,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, HwInfo, ModeNo, ModeIdIndex,RefreshRateTableIndex);
+	    }
+            if(HwInfo->jChipType == SIS_730) {
+               SiS_DisplayOn(SiS_Pr);
+	    }
+         }
+      }
+      if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
+          if(HwInfo->jChipType != SIS_730) {
+             SiS_DisplayOn(SiS_Pr);
+	  }
+      }
+   }
+#endif
+
+#ifdef SIS315H
+   if(HwInfo->jChipType >= SIS_315H) {
+      if(SiS_Pr->SiS_SetFlag & LowModeTests) {
+	 if(HwInfo->jChipType < SIS_661) {
+	    SiS_FinalizeLCD(SiS_Pr, ModeNo, ModeIdIndex, HwInfo);
+            if(SiS_Pr->SiS_UseOEM) {
+               SiS_OEM310Setting(SiS_Pr, HwInfo, ModeNo, ModeIdIndex);
+            }
+	 } else {
+	    SiS_OEM661Setting(SiS_Pr, HwInfo, ModeNo, ModeIdIndex, RefreshRateTableIndex);
+	 }
+         SiS_CRT2AutoThreshold(SiS_Pr);
+      }
+   }
+#endif
+
+   if(SiS_Pr->SiS_SetFlag & LowModeTests) {
+      SiS_EnableBridge(SiS_Pr, HwInfo);
+   }
+
+   SiS_DisplayOn(SiS_Pr);
+
+   if(SiS_Pr->SiS_IF_DEF_CH70xx == 1) {
+      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_Pr->SiS_SetFlag & LowModeTests) {
+      SiS_LockCRT2(SiS_Pr,HwInfo);
+   }
+
+   return TRUE;
 }
 
-#endif  /* 315 series ------------------------------------ */
 
-/* End of Chrontel 701x functions ==================================== */
+/*********************************************/
+/*     ENABLE/DISABLE LCD BACKLIGHT (SIS)    */
+/*********************************************/
+
+void
+SiS_SiS30xBLOn(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
+{
+  /* Switch on LCD backlight on SiS30xLV */
+  if(!(SiS_GetReg(SiS_Pr->SiS_Part4Port,0x26) & 0x02)) {
+     SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x26,0x02);
+     SiS_WaitVBRetrace(SiS_Pr,HwInfo);
+  }
+  if(!(SiS_GetReg(SiS_Pr->SiS_Part4Port,0x26) & 0x01)) {
+     SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x26,0x01);
+  }
+}
+
+void
+SiS_SiS30xBLOff(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
+{
+  /* Switch off LCD backlight on SiS30xLV */
+  SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x26,0xFE);
+  SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x26,0xFD);
+}
 
-/* Generic Read/write routines for Chrontel ========================== */
+/*********************************************/
+/*          DDC RELATED FUNCTIONS            */
+/*********************************************/
 
-/* The Chrontel is connected to the 630/730 via
+/* The Chrontel 700x 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,
- * possibly for working around the DDC problems
+ * On 630(S)T chipset, the index changed from 0x11 to
+ * 0x0a, possibly for working around the DDC problems
  */
 
-void
-SiS_SetCH70xx(SiS_Private *SiS_Pr, USHORT tempbx)
+static BOOLEAN
+SiS_SetChReg(SiS_Private *SiS_Pr, USHORT tempbx, USHORT myor)
 {
-   if(SiS_Pr->SiS_IF_DEF_CH70xx == 1)
-      SiS_SetCH700x(SiS_Pr,tempbx);
-   else
-      SiS_SetCH701x(SiS_Pr,tempbx);
+  USHORT tempah,temp,i;
+
+  for(i=0; i<20; i++) {				/* Do 20 attempts to write */
+     if(i) {
+        SiS_SetStop(SiS_Pr);
+	SiS_DDC2Delay(SiS_Pr,SiS_I2CDELAYSHORT);
+     }
+     if(SiS_SetStart(SiS_Pr)) continue;		/* Set start condition */
+     tempah = SiS_Pr->SiS_DDC_DeviceAddr;
+     temp = SiS_WriteDDC2Data(SiS_Pr,tempah);	/* Write DAB (S0=0=write) */
+     if(temp) continue;				/*    (ERROR: no ack) */
+     tempah = tempbx & 0x00FF;			/* Write RAB */
+     tempah |= myor;                            /* (700x: set bit 7, see datasheet) */
+     temp = SiS_WriteDDC2Data(SiS_Pr,tempah);
+     if(temp) continue;				/*    (ERROR: no ack) */
+     tempah = (tempbx & 0xFF00) >> 8;
+     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 TRUE;
+  }
+  return FALSE;
 }
 
 /* Write to Chrontel 700x */
@@ -9960,59 +9709,22 @@
 void
 SiS_SetCH700x(SiS_Private *SiS_Pr, USHORT tempbx)
 {
-  USHORT tempah,temp,i;
+  SiS_Pr->SiS_DDC_DataShift = 0x00;
+  SiS_Pr->SiS_DDC_DeviceAddr = 0xEA;  		/* DAB (Device Address Byte) */
 
   if(!(SiS_Pr->SiS_ChrontelInit)) {
-     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;  	   /* DAB (Device Address Byte) */
-  }
-
-  for(i=0;i<10;i++) {	/* Do only 10 attempts to write */
-    /* SiS_SetSwitchDDC2(SiS_Pr); */
-    if(SiS_SetStart(SiS_Pr)) continue;		/* Set start condition */
-    tempah = SiS_Pr->SiS_DDC_DeviceAddr;
-    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;				/*    (ERROR: no ack) */
-    tempah = (tempbx & 0xFF00) >> 8;
-    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;
+     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 */
   }
 
-  /* For 630ST */
-  if(!(SiS_Pr->SiS_ChrontelInit)) {
+  if( (!(SiS_SetChReg(SiS_Pr, tempbx, 0x80))) &&
+      (!(SiS_Pr->SiS_ChrontelInit)) ) {
      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;  	/* DAB (Device Address Byte) */
 
-     for(i=0;i<10;i++) {	/* Do only 10 attempts to write */
-       /* SiS_SetSwitchDDC2(SiS_Pr); */
-       if (SiS_SetStart(SiS_Pr)) continue;	/* Set start condition */
-       tempah = SiS_Pr->SiS_DDC_DeviceAddr;
-       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;			/*    (ERROR: no ack) */
-       tempah = (tempbx & 0xFF00) >> 8;
-       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;
-    }
+     SiS_SetChReg(SiS_Pr, tempbx, 0x80);
   }
 }
 
@@ -10021,39 +9733,51 @@
 void
 SiS_SetCH701x(SiS_Private *SiS_Pr, USHORT tempbx)
 {
-  USHORT tempah,temp,i;
-
   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;  		/* DAB (Device Address Byte) */
 
-  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);	/* Write DAB (S0=0=write) */
-    if(temp) continue;				/*    (ERROR: no ack) */
-    tempah = tempbx & 0x00FF;
-    temp = SiS_WriteDDC2Data(SiS_Pr,tempah);	/* Write RAB */
-    if(temp) continue;				/*    (ERROR: no ack) */
-    tempah = (tempbx & 0xFF00) >> 8;
-    temp = SiS_WriteDDC2Data(SiS_Pr,tempah);	/* Write data */
-    if(temp) continue;				/*    (ERROR: no ack) */
-    if(SiS_SetStop(SiS_Pr)) continue;		/* Set stop condition */
-    return;
-  }
+  SiS_SetChReg(SiS_Pr, tempbx, 0);
 }
-
-/* Read from Chrontel 70xx */
-/* Parameter is [Register no (S7-S0)] */
-USHORT
-SiS_GetCH70xx(SiS_Private *SiS_Pr, USHORT tempbx)
+
+void
+SiS_SetCH70xx(SiS_Private *SiS_Pr, USHORT tempbx)
 {
    if(SiS_Pr->SiS_IF_DEF_CH70xx == 1)
-      return(SiS_GetCH700x(SiS_Pr,tempbx));
+      SiS_SetCH700x(SiS_Pr,tempbx);
    else
-      return(SiS_GetCH701x(SiS_Pr,tempbx));
+      SiS_SetCH701x(SiS_Pr,tempbx);
+}
+
+static USHORT
+SiS_GetChReg(SiS_Private *SiS_Pr, USHORT myor)
+{
+  USHORT tempah,temp,i;
+
+  for(i=0; i<20; i++) {				/* Do 20 attempts to read */
+     if(i) {
+        SiS_SetStop(SiS_Pr);
+	SiS_DDC2Delay(SiS_Pr,SiS_I2CDELAYSHORT);
+     }
+     if(SiS_SetStart(SiS_Pr)) continue;		/* Set start condition */
+     tempah = SiS_Pr->SiS_DDC_DeviceAddr;
+     temp = SiS_WriteDDC2Data(SiS_Pr,tempah);	/* Write DAB (S0=0=write) */
+     if(temp) continue;				/*        (ERROR: no ack) */
+     tempah = SiS_Pr->SiS_DDC_ReadAddr | myor;	/* Write RAB (700x: | 0x80) */
+     temp = SiS_WriteDDC2Data(SiS_Pr,tempah);
+     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);	/* 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);
+  }
+  return 0xFFFF;
 }
 
 /* Read from Chrontel 700x */
@@ -10061,65 +9785,29 @@
 USHORT
 SiS_GetCH700x(SiS_Private *SiS_Pr, USHORT tempbx)
 {
-  USHORT tempah,temp,i;
+  USHORT result;
+
+  SiS_Pr->SiS_DDC_DataShift = 0x00;
+  SiS_Pr->SiS_DDC_DeviceAddr = 0xEA;		/* DAB */
 
   if(!(SiS_Pr->SiS_ChrontelInit)) {
      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;		/* DAB */
   }
 
   SiS_Pr->SiS_DDC_ReadAddr = tempbx;
 
-  for(i=0;i<20;i++) {	/* Do only 20 attempts to read */
-    /* SiS_SetSwitchDDC2(SiS_Pr); */
-    if(SiS_SetStart(SiS_Pr)) continue;		/* Set start condition */
-    tempah = SiS_Pr->SiS_DDC_DeviceAddr;
-    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;				/*        (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);	/* 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);
-  }
-
-  /* For 630ST */
-  if(!SiS_Pr->SiS_ChrontelInit) {
-     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;  	/* DAB (Device Address Byte) */
+  if( ((result = SiS_GetChReg(SiS_Pr,0x80)) == 0xFFFF) &&
+      (!SiS_Pr->SiS_ChrontelInit) ) {
 
-     for(i=0;i<20;i++) {	/* Do only 20 attempts to read */
-       /* SiS_SetSwitchDDC2(SiS_Pr); */
-       if(SiS_SetStart(SiS_Pr)) continue;		/* Set start condition */
-       tempah = SiS_Pr->SiS_DDC_DeviceAddr;
-       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;				/*        (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);		/* 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);
-     }
+     SiS_Pr->SiS_DDC_Index = 0x0a;
+     SiS_Pr->SiS_DDC_Data  = 0x80;
+     SiS_Pr->SiS_DDC_Clk   = 0x40;
+
+     result = SiS_GetChReg(SiS_Pr,0x80);
   }
-  return(0xFFFF);
+  return(result);
 }
 
 /* Read from Chrontel 701x */
@@ -10127,32 +9815,26 @@
 USHORT
 SiS_GetCH701x(SiS_Private *SiS_Pr, USHORT tempbx)
 {
-  USHORT tempah,temp,i;
-
   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;		/* DAB */
+
   SiS_Pr->SiS_DDC_ReadAddr = tempbx;
 
-   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);	/* 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;				/*        (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);	/* 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;
+  return(SiS_GetChReg(SiS_Pr,0));
+}
+
+/* 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)
+      return(SiS_GetCH700x(SiS_Pr, tempbx));
+   else
+      return(SiS_GetCH701x(SiS_Pr, tempbx));
 }
 
 /* Our own DDC functions */
@@ -10179,7 +9861,7 @@
      SiS_Pr->SiS_DDC_Index = 0x11;
      flag = 0xff;
 
-     cr32 = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x32);
+     cr32 = SiS_GetReg(SiS_Pr->SiS_P3d4,0x32);
 
 #if 0
      if(VBFlags & VB_SISBRIDGE) {
@@ -10224,7 +9906,7 @@
      } else {						/* 315/330 series */
 
      	/* here we simplify: 0 = CRT1, 1 = CRT2 (VGA, LCD) */
-	
+
 	if(VBFlags & VB_SISBRIDGE) {
 	   if(myadaptnum == 2) {
 	      myadaptnum = 1;
@@ -10323,6 +10005,9 @@
     SiS_SetSwitchDDC2(SiS_Pr);
     if(SiS_PrepareDDC(SiS_Pr)) {
          SiS_SetStop(SiS_Pr);
+#ifdef TWDEBUG
+         xf86DrvMsg(0, X_INFO, "Probe: Prepare failed\n");
+#endif
          return(0xFFFF);
     }
     mask = 0xf0;
@@ -10336,6 +10021,9 @@
        } else {
            failed = TRUE;
 	   ret = 0xFFFF;
+#ifdef TWDEBUG
+           xf86DrvMsg(0, X_INFO, "Probe: Read 1 failed\n");
+#endif
        }
     }
     if(failed == FALSE) {
@@ -10345,6 +10033,9 @@
        if(temp == value) ret = 0;
        else {
           ret = 0xFFFF;
+#ifdef TWDEBUG
+          xf86DrvMsg(0, X_INFO, "Probe: Read 2 failed\n");
+#endif
           if(SiS_Pr->SiS_DDC_DeviceAddr == 0xa0) {
              if(temp == 0x30) ret = 0;
           }
@@ -10427,18 +10118,88 @@
 SiS_HandleDDC(SiS_Private *SiS_Pr, unsigned long VBFlags, int VGAEngine,
               USHORT adaptnum, USHORT DDCdatatype, unsigned char *buffer)
 {
+   unsigned char sr1f,cr17=1;
+   USHORT result;
+
    if(adaptnum > 2) return 0xFFFF;
    if(DDCdatatype > 4) return 0xFFFF;
    if((!(VBFlags & VB_VIDEOBRIDGE)) && (adaptnum > 0)) return 0xFFFF;
-   if(SiS_InitDDCRegs(SiS_Pr, VBFlags, VGAEngine, adaptnum, DDCdatatype, TRUE) == 0xFFFF) return 0xFFFF;
+   if(SiS_InitDDCRegs(SiS_Pr, VBFlags, VGAEngine, adaptnum, DDCdatatype, FALSE) == 0xFFFF) return 0xFFFF;
+
+   sr1f = SiS_GetReg(SiS_Pr->SiS_P3c4,0x1f);
+   SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x1f,0x3f,0x04);
+   if(VGAEngine == SIS_300_VGA) {
+      cr17 = SiS_GetReg(SiS_Pr->SiS_P3d4,0x17) & 0x80;
+      if(!cr17) {
+         SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x17,0x80);
+         SiS_SetReg(SiS_Pr->SiS_P3c4,0x00,0x01);
+         SiS_SetReg(SiS_Pr->SiS_P3c4,0x00,0x03);
+      }
+   }
+   if((sr1f) || (!cr17)) {
+      SiS_WaitRetrace1(SiS_Pr);
+      SiS_WaitRetrace1(SiS_Pr);
+      SiS_WaitRetrace1(SiS_Pr);
+      SiS_WaitRetrace1(SiS_Pr);
+   }
+
    if(DDCdatatype == 0) {
-       return(SiS_ProbeDDC(SiS_Pr));
+      result = SiS_ProbeDDC(SiS_Pr);
    } else {
-       return(SiS_ReadDDC(SiS_Pr, DDCdatatype, buffer));
+      result = SiS_ReadDDC(SiS_Pr, DDCdatatype, buffer);
+   }
+   SiS_SetReg(SiS_Pr->SiS_P3c4,0x1f,sr1f);
+   if(VGAEngine == SIS_300_VGA) {
+      SiS_SetRegANDOR(SiS_Pr->SiS_P3d4,0x17,0x7f,cr17);
    }
+   return result;
 }
 
 #ifdef LINUX_XF86
+
+static BOOLEAN
+checkedid1(unsigned char *buffer)
+{
+   /* Check header */
+   if((buffer[0] != 0x00) ||
+      (buffer[1] != 0xff) ||
+      (buffer[2] != 0xff) ||
+      (buffer[3] != 0xff) ||
+      (buffer[4] != 0xff) ||
+      (buffer[5] != 0xff) ||
+      (buffer[6] != 0xff) ||
+      (buffer[7] != 0x00))
+      return FALSE;
+
+   /* Check EDID version and revision */
+   if((buffer[0x12] != 1) || (buffer[0x13] > 4)) return FALSE;
+
+   /* Check week of manufacture for sanity */
+   if(buffer[0x10] > 53) return FALSE;
+
+   /* Check year of manufacture for sanity */
+   if(buffer[0x11] > 40) return FALSE;
+
+   return TRUE;
+}
+
+static BOOLEAN
+checkedid2(unsigned char *buffer)
+{
+   USHORT year = buffer[6] | (buffer[7] << 8);
+
+   /* Check EDID version */
+   if((buffer[0] & 0xf0) != 0x20) return FALSE;
+
+   /* Check week of manufacture for sanity */
+   if(buffer[5] > 53) return FALSE;
+
+   /* Check year of manufacture for sanity */
+   if((year != 0) && ((year < 1990) || (year > 2030))) return FALSE;
+
+   return TRUE;
+}
+
 /* Sense the LCD parameters (CR36, CR37) via DDC */
 /* SiS30x(B) only */
 USHORT
@@ -10464,7 +10225,7 @@
    
    /* Probe supported DA's */
    flag = SiS_ProbeDDC(SiS_Pr);
-#ifdef TWDEBUG   
+#ifdef TWDEBUG
    xf86DrvMsg(pSiS->pScrn->scrnIndex, X_INFO,
    	"CRT2 DDC capabilities 0x%x\n", flag);
 #endif	
@@ -10483,17 +10244,17 @@
    retry = 2;
    do {
       if(SiS_ReadDDC(SiS_Pr, DDCdatatype, buffer)) {
-         xf86DrvMsg(pSiS->pScrn->scrnIndex, X_INFO, 
-	 	"CRT2: DDC read failed (attempt %d), %s\n", 
+         xf86DrvMsg(pSiS->pScrn->scrnIndex, X_PROBED,
+	 	"CRT2: DDC read failed (attempt %d), %s\n",
 		(3-retry), (retry == 1) ? "giving up" : "retrying");
 	 retry--;
 	 if(retry == 0) return 0xFFFF;
       } else break;
    } while(1);
-   
-#ifdef TWDEBUG   
+
+#ifdef TWDEBUG
    for(i=0; i<256; i+=16) {
-       xf86DrvMsg(pSiS->pScrn->scrnIndex, X_INFO,
+       xf86DrvMsg(pSiS->pScrn->scrnIndex, X_PROBED,
        	"%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
 	buffer[i],    buffer[i+1], buffer[i+2], buffer[i+3],
 	buffer[i+4],  buffer[i+5], buffer[i+6], buffer[i+7],
@@ -10507,8 +10268,14 @@
    switch(DDCdatatype) {
    case 1:							/* Analyze EDID V1 */
       /* Catch a few clear cases: */
+      if(!(checkedid1(buffer))) {
+         xf86DrvMsg(pSiS->pScrn->scrnIndex, X_PROBED,
+	 	"CRT2: EDID corrupt\n");
+	 return 0;
+      }
+
       if(!(buffer[0x14] & 0x80)) {
-         xf86DrvMsg(pSiS->pScrn->scrnIndex, X_PROBED, 
+         xf86DrvMsg(pSiS->pScrn->scrnIndex, X_PROBED,
 	        "CRT2: Attached display expects analog input (0x%02x)\n",
 		buffer[0x14]);
       	 return 0;
@@ -10565,9 +10332,13 @@
 		      paneltype = Panel310_1280x960;
 		   }
 	        } else if(yres == 768) {
-	       	   paneltype = Panel_1280x768;
-		   checkexpand = FALSE;
-		   cr37 |= 0x10;
+		   if( ((buffer[0x36] | (buffer[0x37] << 8)) == 8100) &&
+		       ((buffer[0x39] | ((buffer[0x3a] & 0x0f) << 8)) == (1688 - 1280)) &&
+		       ((buffer[0x3c] | ((buffer[0x3d] & 0x0f) << 8)) == (802 - 768)) ) {
+	       	      paneltype = Panel_1280x768;
+		      checkexpand = FALSE;
+		      cr37 |= 0x10;
+		   }
 	        }
 	        break;
 	    case 1400:
@@ -10597,6 +10368,7 @@
 	       /* What now? There is no digital separate output timing... */
 	       xf86DrvMsg(pSiS->pScrn->scrnIndex, X_WARNING,
 	       	   "CRT2: Unable to retrieve Sync polarity information\n");
+	       cr37 |= 0xc0;  /* Default */
 	    }
 	 }
 
@@ -10623,6 +10395,7 @@
 	 };
 
 	 paneltype = 0;
+	 SiS_Pr->CP_Supports64048075 = TRUE;
 
 	 /* Find the maximum resolution */
 
@@ -10678,20 +10451,21 @@
 	       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])      ||
-	          ( ((pSiS->VBFlags & VB_301C) && (SiS_Pr->CP_Clock[i] > 162000)) ||
-		    ((!(pSiS->VBFlags & VB_301C)) && (SiS_Pr->CP_Clock[i] > 108000)) ) ||
+	       if((SiS_Pr->CP_HDisplay[i] & 7)						||
+	          (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])      			||
+		  (((pSiS->VBFlags & VB_301C) && (SiS_Pr->CP_Clock[i] > 162500)) ||
+	           ((!(pSiS->VBFlags & VB_301C)) && (SiS_Pr->CP_Clock[i] > 108200)))	||
 		  (buffer[base+17] & 0x80)) {
 
 	          SiS_Pr->CP_DataValid[i] = FALSE;
@@ -10709,6 +10483,11 @@
 		  SiS_Pr->CP_Vendor = buffer[9] | (buffer[8] << 8);
 		  SiS_Pr->CP_Product = buffer[10] | (buffer[11] << 8);
 
+		  /* By default we drive the LCD at 75Hz in 640x480 mode; if
+		   * the panel does not provide this mode, use 60hz
+		   */
+		  if(!(buffer[0x23] & 0x04)) SiS_Pr->CP_Supports64048075 = FALSE;
+
 	          /* We must assume the panel can scale, since we have
 	           * no scaling data
 		   */
@@ -10755,6 +10534,13 @@
    case 3:							/* Analyze EDID V2 */
    case 4:
       index = 0;
+
+      if(!(checkedid2(buffer))) {
+         xf86DrvMsg(pSiS->pScrn->scrnIndex, X_PROBED,
+	 	"CRT2: EDID corrupt\n");
+	 return 0;
+      }
+
       if((buffer[0x41] & 0x0f) == 0x03) {
          index = 0x42 + 3;
          xf86DrvMsg(pSiS->pScrn->scrnIndex, X_PROBED,
@@ -10806,11 +10592,8 @@
 	     } else if(yres == 1024) {
 	     	paneltype = Panel_1280x1024;
 		checkexpand = TRUE;
-	     } else if(yres == 768) {
-	        paneltype = Panel_1280x768;
-		checkexpand = FALSE;
-		cr37 |= 0x10;
 	     }
+	     /* 1280x768 treated as custom here */
 	     break;
 	 case 1400:
 	     if(pSiS->VGAEngine == SIS_315_VGA) {
@@ -10849,7 +10632,7 @@
       if(buffer[0x7e] & 0x20) {			    /* skip Luminance Table (if provided) */
          lumsize = buffer[0x80] & 0x1f;
 	 if(buffer[0x80] & 0x80) lumsize *= 3;
-	 lumsize++;
+	 lumsize++;  /* luminance header byte */
 	 index += lumsize;
       }
       index += (((buffer[0x7e] & 0x1c) >> 2) * 8);   /* skip Frequency Ranges */
@@ -10875,9 +10658,12 @@
 
       /* Now seek the detailed timing descriptions for custom panels */
       if(paneltype == Panel_Custom) {
+
+         SiS_Pr->CP_Supports64048075 = TRUE;
+
          index += (numcodes * 4);
 	 numcodes = buffer[0x7f] & 0x07;
-	 for(i=0; i<numcodes; i++) {
+	 for(i=0; i<numcodes; i++, index += 18) {
 	    xres = buffer[index+2] | ((buffer[index+4] & 0xf0) << 4);
             yres = buffer[index+5] | ((buffer[index+7] & 0xf0) << 4);
 
@@ -10899,20 +10685,21 @@
 
 	    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])      ||
-	       ( ((pSiS->VBFlags & VB_301C) && (SiS_Pr->CP_Clock[i] > 162000)) ||
-		 ((!(pSiS->VBFlags & VB_301C)) && (SiS_Pr->CP_Clock[i] > 108000)) ) ||
+	    if((SiS_Pr->CP_HDisplay[i] & 7)						||
+	       (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])      			||
+	       (((pSiS->VBFlags & VB_301C) && (SiS_Pr->CP_Clock[i] > 162500)) ||
+	        ((!(pSiS->VBFlags & VB_301C)) && (SiS_Pr->CP_Clock[i] > 108200)))	||
 	       (buffer[index + 17] & 0x80)) {
 
 	       SiS_Pr->CP_DataValid[i] = FALSE;
@@ -10968,7 +10755,7 @@
          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",
+	    "   To use this, add \"%dx%d\" to the list of Modes in the Screen section\n",
 	    SiS_Pr->CP_HDisplay[i],
 	    SiS_Pr->CP_VDisplay[i]);
       }
@@ -10978,11 +10765,10 @@
        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);
-       SiS_SetReg1(SiS_Pr->SiS_P3d4,0x37,cr37);
+       SiS_SetRegANDOR(SiS_Pr->SiS_P3d4,0x37,0xf1,cr37);
        SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x32,0x08);
-#ifdef TWDEBUG       
+#ifdef TWDEBUG
        xf86DrvMsgVerb(pSiS->pScrn->scrnIndex, X_PROBED, 3, 
        	"CRT2: [DDC LCD results: 0x%02x, 0x%02x]\n", paneltype, cr37);
 #endif	
@@ -10999,8 +10785,7 @@
    unsigned char buffer[256];
 
    if(!(pSiS->VBFlags & (VB_301|VB_301B|VB_301C|VB_302B))) return 0;
-/* if(pSiS->VBFlags & VB_30xBDH) return 0;  */
-   
+
    if(SiS_InitDDCRegs(SiS_Pr, pSiS->VBFlags, pSiS->VGAEngine, 2, 0, FALSE) == 0xFFFF) return 0;
    
    SiS_Pr->SiS_DDC_SecAddr = 0x00;
@@ -11017,16 +10802,16 @@
       SiS_Pr->SiS_DDC_DeviceAddr = 0xa0;	/* EDID V1 */
       DDCdatatype = 1;
    } else {
-   	xf86DrvMsg(pSiS->pScrn->scrnIndex, X_INFO, 
+   	xf86DrvMsg(pSiS->pScrn->scrnIndex, X_PROBED,
 		"Do DDC answer\n");
    	return 0;				/* no DDC support (or no device attached) */
    }
-   
+
    /* Read the entire EDID */
    retry = 2;
    do {
       if(SiS_ReadDDC(SiS_Pr, DDCdatatype, buffer)) {
-         xf86DrvMsg(pSiS->pScrn->scrnIndex, X_INFO, 
+         xf86DrvMsg(pSiS->pScrn->scrnIndex, X_PROBED,
 	 	"CRT2: DDC read failed (attempt %d), %s\n", 
 		(3-retry), (retry == 1) ? "giving up" : "retrying");
 	 retry--;
@@ -11039,10 +10824,15 @@
     */
    switch(DDCdatatype) {
    case 1:
+      if(!(checkedid1(buffer))) {
+          xf86DrvMsg(pSiS->pScrn->scrnIndex, X_PROBED,
+	  	"CRT2: EDID corrupt\n");
+      	  return 0;
+      }
       if(buffer[0x14] & 0x80) {			/* Display uses digital input */
           xf86DrvMsg(pSiS->pScrn->scrnIndex, X_PROBED,
 	  	"CRT2: Attached display expects digital input\n");
-      	  return 0;  	
+      	  return 0;
       }
       SiS_Pr->CP_Vendor = buffer[9] | (buffer[8] << 8);
       SiS_Pr->CP_Product = buffer[10] | (buffer[11] << 8);
@@ -11050,6 +10840,11 @@
       break;
    case 3:
    case 4:
+      if(!(checkedid2(buffer))) {
+          xf86DrvMsg(pSiS->pScrn->scrnIndex, X_PROBED,
+	  	"CRT2: EDID corrupt\n");
+      	  return 0;
+      }
       if( ((buffer[0x41] & 0x0f) != 0x01) &&  	/* Display does not support analog input */
           ((buffer[0x41] & 0x0f) != 0x02) &&
 	  ((buffer[0x41] & 0xf0) != 0x10) &&
@@ -11071,37 +10866,6 @@
    return(0);
 }
 
-/* Generic I2C functions (compliant to i2c library) */
-
-#if 0
-USHORT
-SiS_I2C_GetByte(SiS_Private *SiS_Pr)
-{
-   return(SiS_ReadDDC2Data(SiS_Pr,0));
-}
-
-Bool
-SiS_I2C_PutByte(SiS_Private *SiS_Pr, USHORT data)
-{
-   if(SiS_WriteDDC2Data(SiS_Pr,data)) return FALSE;
-   return TRUE;
-}
-
-Bool
-SiS_I2C_Address(SiS_Private *SiS_Pr, USHORT addr)
-{
-   if(SiS_SetStart(SiS_Pr)) return FALSE;
-   if(SiS_WriteDDC2Data(SiS_Pr,addr)) return FALSE;
-   return TRUE;
-}
-
-void
-SiS_I2C_Stop(SiS_Private *SiS_Pr)
-{
-   SiS_SetStop(SiS_Pr);
-}
-#endif
-
 #endif
 
 void
@@ -11114,18 +10878,23 @@
   SiS_SetCH70xx(SiS_Pr,tempbl);
 }
 
-/* Generic I2C functions for Chrontel --------- */
+/* Generic I2C functions for Chrontel & DDC --------- */
 
 void
 SiS_SetSwitchDDC2(SiS_Private *SiS_Pr)
 {
   SiS_SetSCLKHigh(SiS_Pr);
-  /* SiS_DDC2Delay(SiS_Pr,SiS_I2CDELAY); */
-  SiS_WaitRetraceDDC(SiS_Pr);
+  SiS_WaitRetrace1(SiS_Pr);
 
   SiS_SetSCLKLow(SiS_Pr);
-  /* SiS_DDC2Delay(SiS_Pr,SiS_I2CDELAY); */
-  SiS_WaitRetraceDDC(SiS_Pr);
+  SiS_WaitRetrace1(SiS_Pr);
+}
+
+USHORT
+SiS_ReadDDC1Bit(SiS_Private *SiS_Pr)
+{
+   SiS_WaitRetrace1(SiS_Pr);
+   return((SiS_GetReg(SiS_Pr->SiS_P3c4,0x11) & 0x02) >> 1);
 }
 
 /* Set I2C start condition */
@@ -11164,8 +10933,8 @@
 {
   USHORT i,flag,temp;
 
-  flag=0x80;
-  for(i=0;i<8;i++) {
+  flag = 0x80;
+  for(i=0; i<8; i++) {
     SiS_SetSCLKLow(SiS_Pr);				                      /* SC->low */
     if(tempax & flag) {
       SiS_SetRegANDOR(SiS_Pr->SiS_DDC_Port,SiS_Pr->SiS_DDC_Index,
@@ -11193,7 +10962,7 @@
     SiS_SetRegANDOR(SiS_Pr->SiS_DDC_Port,SiS_Pr->SiS_DDC_Index,
                     ~SiS_Pr->SiS_DDC_Data,SiS_Pr->SiS_DDC_Data);
     SiS_SetSCLKHigh(SiS_Pr);
-    temp = SiS_GetReg1(SiS_Pr->SiS_DDC_Port,SiS_Pr->SiS_DDC_Index);
+    temp = SiS_GetReg(SiS_Pr->SiS_DDC_Port,SiS_Pr->SiS_DDC_Index);
     if(temp & SiS_Pr->SiS_DDC_Data) getdata |= 0x01;
   }
   return(getdata);
@@ -11211,33 +10980,23 @@
 USHORT
 SiS_SetSCLKHigh(SiS_Private *SiS_Pr)
 {
-  USHORT temp,watchdog=1000;
+  USHORT temp, watchdog=1000;
 
   SiS_SetRegANDOR(SiS_Pr->SiS_DDC_Port,SiS_Pr->SiS_DDC_Index,
                   ~SiS_Pr->SiS_DDC_Clk,SiS_Pr->SiS_DDC_Clk);  	/* SetSCLKHigh()  */
   do {
-    temp = SiS_GetReg1(SiS_Pr->SiS_DDC_Port,SiS_Pr->SiS_DDC_Index);
+    temp = SiS_GetReg(SiS_Pr->SiS_DDC_Port,SiS_Pr->SiS_DDC_Index);
   } while((!(temp & SiS_Pr->SiS_DDC_Clk)) && --watchdog);
   if (!watchdog) {
 #ifdef TWDEBUG
         xf86DrvMsg(0, X_INFO, "SetClkHigh failed\n");
-#endif	   
+#endif
   	return 0xFFFF;
   }
   SiS_DDC2Delay(SiS_Pr,SiS_I2CDELAYSHORT);
   return 0;
 }
 
-void
-SiS_DDC2Delay(SiS_Private *SiS_Pr, USHORT delaytime)
-{
-  USHORT i;
-
-  for(i=0; i<delaytime; i++) {
-    SiS_GetReg1(SiS_Pr->SiS_P3c4,0x05);
-  }
-}
-
 /* Check I2C acknowledge */
 /* Returns 0 if ack ok, non-0 if ack not ok */
 USHORT
@@ -11249,7 +11008,7 @@
   SiS_SetRegANDOR(SiS_Pr->SiS_DDC_Port,SiS_Pr->SiS_DDC_Index,
                   ~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 */
+  tempah = SiS_GetReg(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);
@@ -11263,11 +11022,12 @@
 #ifdef SIS315H
 
 static USHORT
-GetRAMDACromptr(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension, UCHAR *ROMAddr)
+GetRAMDACromptr(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
 {
+  UCHAR  *ROMAddr = HwInfo->pjVirtualRomBase;
   USHORT romptr;
 
-  if(HwDeviceExtension->jChipType < SIS_330) {
+  if(HwInfo->jChipType < SIS_330) {
      romptr = ROMAddr[0x128] | (ROMAddr[0x129] << 8);
      if(SiS_Pr->SiS_VBType & VB_SIS301B302B)
         romptr = ROMAddr[0x12a] | (ROMAddr[0x12b] << 8);
@@ -11280,11 +11040,12 @@
 }
 
 static USHORT
-GetLCDromptr(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension, UCHAR *ROMAddr)
+GetLCDromptr(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
 {
+  UCHAR  *ROMAddr = HwInfo->pjVirtualRomBase;
   USHORT romptr;
 
-  if(HwDeviceExtension->jChipType < SIS_330) {
+  if(HwInfo->jChipType < SIS_330) {
      romptr = ROMAddr[0x120] | (ROMAddr[0x121] << 8);
      if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV)
         romptr = ROMAddr[0x122] | (ROMAddr[0x123] << 8);
@@ -11297,11 +11058,12 @@
 }
 
 static USHORT
-GetTVromptr(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension, UCHAR *ROMAddr)
+GetTVromptr(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
 {
+  UCHAR  *ROMAddr = HwInfo->pjVirtualRomBase;
   USHORT romptr;
 
-  if(HwDeviceExtension->jChipType < SIS_330) {
+  if(HwInfo->jChipType < SIS_330) {
      romptr = ROMAddr[0x114] | (ROMAddr[0x115] << 8);
      if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV)
         romptr = ROMAddr[0x11a] | (ROMAddr[0x11b] << 8);
@@ -11314,13 +11076,13 @@
 }
 
 static USHORT
-GetLCDPtrIndexBIOS(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension, USHORT BaseAddr)
+GetLCDPtrIndexBIOS(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
 {
   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)) {
+     if(!(SiS_IsNotM650orLater(SiS_Pr, HwInfo))) {
+        if((index = SiS_GetReg(SiS_Pr->SiS_P3d4,0x36) & 0xf0)) {
 	   index >>= 4;
 	   index *= 3;
 	   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) index += 2;
@@ -11354,45 +11116,80 @@
   return index;
 }
 
-/*
----------------------------------------------------------
-       GetTVPtrIndex()
-          return       0 : NTSC Enhanced/Standard
-                       1 : NTSC Standard TVSimuMode
-                       2 : PAL Enhanced/Standard
-                       3 : PAL Standard TVSimuMode
-                       4 : HiVision Enhanced/Standard
-                       5 : HiVision Standard TVSimuMode
----------------------------------------------------------
-*/
 static USHORT
 GetTVPtrIndex(SiS_Private *SiS_Pr)
 {
   USHORT index;
 
   index = 0;
-  if(SiS_Pr->SiS_VBInfo & SetPALTV) index++;
-  if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) index++;  /* Hivision TV use PAL */
+  if(SiS_Pr->SiS_TVMode & TVSetPAL) index++;
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) index++;  /* HiVision uses PAL */
 
   index <<= 1;
 
-  if((SiS_Pr->SiS_VBInfo & SetInSlaveMode) && (SiS_Pr->SiS_SetFlag & TVSimuMode))
-    index++;
+  if((SiS_Pr->SiS_VBInfo & SetInSlaveMode) &&
+     (SiS_Pr->SiS_TVMode & TVSetTVSimuMode)) {
+     index++;
+  }
 
   return index;
 }
 
+static ULONG
+GetOEMTVPtr661_2(SiS_Private *SiS_Pr)
+{
+   USHORT index = 0, temp = 0;
+
+   if(SiS_Pr->SiS_TVMode & TVSetPAL)   index = 1;
+   if(SiS_Pr->SiS_TVMode & TVSetPALM)  index = 2;
+   if(SiS_Pr->SiS_TVMode & TVSetPALN)  index = 3;
+   if(SiS_Pr->SiS_TVMode & TVSetNTSCJ) index = 6;
+   if(SiS_Pr->SiS_TVMode & TVSetNTSC1024) {
+      index = 4;
+      if(SiS_Pr->SiS_TVMode & TVSetPALM)  index++;
+      if(SiS_Pr->SiS_TVMode & TVSetNTSCJ) index = 7;
+   }
+
+   if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
+      if((!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) ||
+         (SiS_Pr->SiS_TVMode & TVSetTVSimuMode)) {
+	 index += 8;
+	 temp++;
+      }
+      temp += 0x0100;
+   }
+   return(ULONG)(index | (temp << 16));
+}
+
+static int
+GetOEMTVPtr661(SiS_Private *SiS_Pr)
+{
+   int index = 0;
+
+   if(SiS_Pr->SiS_TVMode & TVSetPAL)       index = 2;
+   if(SiS_Pr->SiS_TVMode & TVSetHiVision)  index = 4;
+   if(SiS_Pr->SiS_TVMode & TVSetYPbPr525i) index = 6;
+   if(SiS_Pr->SiS_TVMode & TVSetYPbPr525p) index = 8;
+   if(SiS_Pr->SiS_TVMode & TVSetYPbPr750p) index = 10;
+
+   if(SiS_Pr->SiS_TVMode & TVSetTVSimuMode) index++;
+
+   return index;
+}
+
 static void
-SetDelayComp(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,USHORT BaseAddr,
-             UCHAR *ROMAddr, USHORT ModeNo)
+SetDelayComp(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo, USHORT ModeNo)
 {
+  UCHAR  *ROMAddr = HwInfo->pjVirtualRomBase;
   USHORT delay=0,index,myindex,temp,romptr=0;
   BOOLEAN dochiptest = TRUE;
 
-  if(SiS_Pr->SiS_VBInfo & SetCRT2ToRAMDAC) {			/* VGA */
+  /* Find delay (from ROM, internal tables, PCI subsystem) */
+
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToRAMDAC) {			/* ------------ VGA */
      
      if((ROMAddr) && SiS_Pr->SiS_UseROM) {
-        romptr = GetRAMDACromptr(SiS_Pr, HwDeviceExtension, ROMAddr);
+        romptr = GetRAMDACromptr(SiS_Pr, HwInfo);
 	if(!romptr) return;
 	delay = ROMAddr[romptr];
      } else {
@@ -11402,166 +11199,247 @@
 	      delay = 0x0a;
 	   } else if(IS_SIS740) {
 	      delay = 0x00;
-	   } else if(HwDeviceExtension->jChipType < SIS_330) {
+	   } else if(HwInfo->jChipType < SIS_330) {
 	      delay = 0x0c;
 	   } else {
 	      delay = 0x0c;
 	   }
 	}
-        if(SiS_Pr->SiS_IF_DEF_LVDS == 1)
+        if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
            delay = 0x00;
+	}
      }
 
-  } else if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {		/* LCD */
+  } else if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD|SetCRT2ToLCDA)) {  /* ----------	LCD/LCDA */
 
      BOOLEAN gotitfrompci = FALSE;
 
      if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_PanelCustom) return;
 
-     if(HwDeviceExtension->pdc) return;
+     /* Could we detect a PDC for LCD? If yes, use it */
+
+     if(SiS_Pr->PDC) {
+        if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
+	   SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2d,0x0f,((SiS_Pr->PDC & 0x0f) << 4));
+	}
+        return;
+     }
 
      /* This is a piece of typical SiS crap: They code the OEM LCD
-      * delay into the code, at none defined place in the BIOS.
+      * delay into the code, at no defined place in the BIOS.
       * We now have to start doing a PCI subsystem check here.
       */
 
-     if(SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) {
+     switch(SiS_Pr->SiS_CustomT) {
+     case CUT_COMPAQ1280:
+     case CUT_COMPAQ12802:
 	if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
 	   gotitfrompci = TRUE;
 	   dochiptest = FALSE;
 	   delay = 0x03;
 	}
-     }
-
-     if((SiS_Pr->SiS_CustomT == CUT_CLEVO1024) ||
-        (SiS_Pr->SiS_CustomT == CUT_CLEVO10242)) {
+	break;
+     case CUT_CLEVO1400:
+     case CUT_CLEVO14002:
+	/* if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) { */
+	   gotitfrompci = TRUE;
+	   dochiptest = FALSE;
+	   delay = 0x02;
+	/* } */
+	break;
+     case CUT_CLEVO1024:
+     case CUT_CLEVO10242:
         if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
 	   gotitfrompci = TRUE;
 	   dochiptest = FALSE;
 	   delay = 0x33;
-	   SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x2D,delay);
+	   SiS_SetReg(SiS_Pr->SiS_Part1Port,0x2D,delay);
 	   delay &= 0x0f;
 	}
+	break;
      }
 
+     /* Could we find it through the PCI ID? If no, use ROM or table */
+
      if(!gotitfrompci) {
 
-        index = GetLCDPtrIndexBIOS(SiS_Pr, HwDeviceExtension, BaseAddr);
+        index = GetLCDPtrIndexBIOS(SiS_Pr, HwInfo);
         myindex = GetLCDPtrIndex(SiS_Pr);
 
-        if(IS_SIS650 && (SiS_Pr->SiS_VBType & VB_SIS301LV302LV)) { 	/* 650+30xLV */
-           if(SiS_IsNotM650or651(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-             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);
-	        if(SiS_Pr->SiS_VBType & VB_SIS302LV)
-#endif
-	           romptr = ROMAddr[0x122] | (ROMAddr[0x123] << 8);
-	        if(!romptr) return;
-	        delay = ROMAddr[(romptr + index)];
-	     } else {
-                delay = SiS310_LCDDelayCompensation_650301B[myindex];
+        if(IS_SIS650 && (SiS_Pr->SiS_VBType & VB_SIS301LV302LV)) {
+
+           if(SiS_IsNotM650orLater(SiS_Pr, HwInfo)) {
+
+              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);
+	         if(SiS_Pr->SiS_VBType & VB_SIS302LV)
+#endif
+	            romptr = ROMAddr[0x122] | (ROMAddr[0x123] << 8);
+	         if(!romptr) return;
+	         delay = ROMAddr[(romptr + index)];
+	      } else {
+                 delay = SiS310_LCDDelayCompensation_650301B[myindex];
 #if 0
-	        if(SiS_Pr->SiS_VBType & VB_SIS302LV)
-	           delay = SiS310_LCDDelayCompensation_650301B[myindex];
+	         if(SiS_Pr->SiS_VBType & VB_SIS302LV)
+	            delay = SiS310_LCDDelayCompensation_650301B[myindex];
 #endif
-	     }
+	      }
+
           } else {
+
              delay = SiS310_LCDDelayCompensation_651301LV[myindex];
-	     if(SiS_Pr->SiS_VBType & VB_SIS302LV)
+	     if(SiS_Pr->SiS_VBType & (VB_SIS302LV | VB_SIS302ELV))
 	        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)) {
-              romptr = GetLCDromptr(SiS_Pr, HwDeviceExtension, ROMAddr);
-	      if(!romptr) return;
-	      delay = ROMAddr[(romptr + index)];
-           } else {
-              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_SIS550650740660) {
-	            delay = SiS310_LCDDelayCompensation_650301B[myindex];
-	         } else {
+
+        } else if((ROMAddr) && SiS_Pr->SiS_UseROM &&
+	          (SiS_Pr->SiS_LCDResInfo != SiS_Pr->SiS_Panel1280x1024)) {
+
+           romptr = GetLCDromptr(SiS_Pr, HwInfo);
+	   if(!romptr) return;
+	   delay = ROMAddr[(romptr + index)];
+
+        } else if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
+
+	   if(IS_SIS650) {
+              delay = SiS310_LCDDelayCompensation_LVDS650[myindex];
+	   } else {
+	      delay = SiS310_LCDDelayCompensation_LVDS740[myindex];
+	   }
+
+	} else {
+
+           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_SIS550650740660) {
+	         delay = SiS310_LCDDelayCompensation_650301B[myindex];
+	      } else {
 #endif
-                    delay = SiS310_LCDDelayCompensation_3xx301B[myindex];
+                 delay = SiS310_LCDDelayCompensation_3xx301B[myindex];
 #if 0
-	         }
-#endif
-	      }
-              if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
-	         if(IS_SIS650) {
-                    delay = SiS310_LCDDelayCompensation_LVDS650[myindex];
-	         } else {
-	            delay = SiS310_LCDDelayCompensation_LVDS740[myindex];
-	         }
 	      }
-           }
+#endif
+	   }
+
         }
+
+     }  /* got it from PCI */
+
+     if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
+	SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2D,0x0F,((delay << 4) & 0xf0));
+	dochiptest = FALSE;
      }
      
-  } else {							/* TV */
+  } else if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {			/* ------------ TV */
 
      index = GetTVPtrIndex(SiS_Pr);
      
      if(IS_SIS650 && (SiS_Pr->SiS_VBType & VB_SIS301LV302LV)) {
-       if(SiS_IsNotM650or651(SiS_Pr,HwDeviceExtension, BaseAddr)) {
-          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[0x114] | (ROMAddr[0x115] << 8);
-	     if(SiS_Pr->SiS_VBType & VB_SIS302LV)
-#endif	     
-	        romptr = ROMAddr[0x11a] | (ROMAddr[0x11b] << 8);
-	     if(!romptr) return;
-	     delay = ROMAddr[romptr + index];
-	  } else {
-	     delay = SiS310_TVDelayCompensation_301B[index];
+
+        if(SiS_IsNotM650orLater(SiS_Pr,HwInfo)) {
+
+           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[0x114] | (ROMAddr[0x115] << 8);
+	      if(SiS_Pr->SiS_VBType & VB_SIS302LV)
+#endif
+	         romptr = ROMAddr[0x11a] | (ROMAddr[0x11b] << 8);
+	      if(!romptr) return;
+	      delay = ROMAddr[romptr + index];
+
+	   } else {
+
+	      delay = SiS310_TVDelayCompensation_301B[index];
 #if 0
-	     if(SiS_Pr->SiS_VBType & VB_SIS302LV)
-	        delay = SiS310_TVDelayCompensation_301B[index];
+	      if(SiS_Pr->SiS_VBType & VB_SIS302LV)
+	         delay = SiS310_TVDelayCompensation_301B[index];
 #endif		
-	  }
-       } else {
-          switch(SiS_Pr->SiS_CustomT) {
-	  case CUT_COMPAQ1280:
-	     delay = 0x02;
-	     dochiptest = FALSE;
-	     break;
-	  case CUT_CLEVO1024:
-	  case CUT_CLEVO10242:
-	     delay = 0x03;
-	     dochiptest = FALSE;
-   	     break;
-	  default:
-             delay = SiS310_TVDelayCompensation_651301LV[index];
-	     if(SiS_Pr->SiS_VBType & VB_SIS302LV)
-	        delay = SiS310_TVDelayCompensation_651302LV[index];
-	  }
-       }
+	   }
+
+        } else {
+
+           switch(SiS_Pr->SiS_CustomT) {
+	   case CUT_COMPAQ1280:
+	   case CUT_COMPAQ12802:
+	   case CUT_CLEVO1400:
+	   case CUT_CLEVO14002:
+	      delay = 0x02;
+	      dochiptest = FALSE;
+	      break;
+	   case CUT_CLEVO1024:
+	   case CUT_CLEVO10242:
+	      delay = 0x03;
+	      dochiptest = FALSE;
+   	      break;
+	   default:
+              delay = SiS310_TVDelayCompensation_651301LV[index];
+	      if(SiS_Pr->SiS_VBType & VB_SIS302LV) {
+	         delay = SiS310_TVDelayCompensation_651302LV[index];
+	      }
+	   }
+        }
+
+     } else if((ROMAddr) && SiS_Pr->SiS_UseROM) {
+
+        romptr = GetTVromptr(SiS_Pr, HwInfo);
+	if(!romptr) return;
+	delay = ROMAddr[romptr + index];
+
+     } else if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
+
+        delay = SiS310_TVDelayCompensation_LVDS[index];
+
      } else {
-       if((ROMAddr) && SiS_Pr->SiS_UseROM) {
-          romptr = GetTVromptr(SiS_Pr, HwDeviceExtension, ROMAddr);
-	  if(!romptr) return;
-	  delay = ROMAddr[romptr + index];
-       } else {
-	    delay = SiS310_TVDelayCompensation_301[index];
-            if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-	       if(IS_SIS740)
-	          delay = SiS310_TVDelayCompensation_740301B[index];
-	       else 
-                  delay = SiS310_TVDelayCompensation_301B[index];
-	    }
-            if(SiS_Pr->SiS_IF_DEF_LVDS == 1)
-               delay = SiS310_TVDelayCompensation_LVDS[index];
-       }
+
+	delay = SiS310_TVDelayCompensation_301[index];
+        if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
+	   if(IS_SIS740) {
+	      delay = SiS310_TVDelayCompensation_740301B[index];
+	   } else {
+              delay = SiS310_TVDelayCompensation_301B[index];
+	   }
+	}
+
+     }
+
+     if(SiS_GetReg(SiS_Pr->SiS_Part1Port,0x13) & 0x04) {  /* LCDA */
+	delay &= 0x0f;
+	dochiptest = FALSE;
+     }
+    
+  } else return;
+
+  /* Write delay */
+
+  if(SiS_Pr->SiS_VBType & VB_SISVB) {
+
+     if(IS_SIS650 && (SiS_Pr->SiS_VBType & VB_SIS301LV302LV) && dochiptest) {
+
+        temp = (SiS_GetReg(SiS_Pr->SiS_P3d4,0x36) & 0xf0) >> 4;
+        if(temp == 8) {		/* 1400x1050 BIOS (COMPAL) */
+	   delay &= 0x0f;
+	   delay |= 0xb0;
+        } else if(temp == 6) {
+           delay &= 0x0f;
+	   delay |= 0xc0;
+        } else if(temp > 7) {	/* 1280x1024 BIOS (which one?) */
+	   delay = 0x35;
+        }
+        SiS_SetReg(SiS_Pr->SiS_Part1Port,0x2D,delay);
+
+     } else {
+
+        SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2D,0xF0,delay);
+
      }
-    
-  }
 
-  if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
+  } else {  /* LVDS */
+
      if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
         SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2D,0xF0,delay);
      } else {
@@ -11572,49 +11450,44 @@
            SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2D,0xF0,delay);
         }
      }
-  } else {
-     if(dochiptest && IS_SIS650 && (SiS_Pr->SiS_VBType & VB_SIS301LV302LV)) {
-        temp = (SiS_GetReg1(SiS_Pr->SiS_P3d4,0x36) & 0xf0) >> 4;
-        if(temp == 8) {		/* 1400x1050 BIOS (ECS) */
-	   delay &= 0x0f;
-	   delay |= 0xb0;
-        } else if(temp == 6) {
-           delay &= 0x0f;
-	   delay |= 0xc0;
-        } else if(temp > 7) {	/* 1280x1024 BIOS (which one?) */
-	   delay = 0x35;
-        }
-        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x2D,delay);
-     } else {
-        SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x2D,0xF0,delay);
-     }
+
   }
+
 }
 
 static void
-SetAntiFlicker(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,USHORT BaseAddr,
-               UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex)
+SetAntiFlicker(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,
+               USHORT ModeNo,USHORT ModeIdIndex)
 {
-  USHORT index,temp,romptr=0;
+  UCHAR  *ROMAddr = HwInfo->pjVirtualRomBase;
+  USHORT index,temp,temp1,romptr=0;
 
-  temp = GetTVPtrIndex(SiS_Pr);
-  temp >>= 1;  	  /* 0: NTSC, 1: PAL, 2: HiTV */
+  if(SiS_Pr->SiS_TVMode & (TVSetYPbPr750p|TVSetYPbPr525p)) return;
 
   if(ModeNo<=0x13)
      index = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].VB_StTVFlickerIndex;
   else
      index = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].VB_ExtTVFlickerIndex;
 
+  temp = GetTVPtrIndex(SiS_Pr);
+  temp >>= 1;  	  /* 0: NTSC, 1: PAL, 2: HiTV */
+  temp1 = temp;
+
   if(ROMAddr && SiS_Pr->SiS_UseROM) {
-     romptr = ROMAddr[0x112] | (ROMAddr[0x113] << 8);
-     if(HwDeviceExtension->jChipType >= SIS_330) {
+     if(HwInfo->jChipType >= SIS_661) {
+	romptr = ROMAddr[0x260] | (ROMAddr[0x261] << 8);
+	temp1 = GetOEMTVPtr661(SiS_Pr);
+        temp1 >>= 1;
+     } else if(HwInfo->jChipType >= SIS_330) {
         romptr = ROMAddr[0x192] | (ROMAddr[0x193] << 8);
+     } else {
+        romptr = ROMAddr[0x112] | (ROMAddr[0x113] << 8);
      }
   }
 
   if(romptr) {
-     temp <<= 1;
-     temp = ROMAddr[romptr + temp + index];
+     temp1 <<= 1;
+     temp = ROMAddr[romptr + temp1 + index];
   } else {
      temp = SiS310_TVAntiFlick1[temp][index];
   }
@@ -11624,13 +11497,15 @@
 }
 
 static void
-SetEdgeEnhance(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,USHORT BaseAddr,
-               UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex)
+SetEdgeEnhance(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,
+               USHORT ModeNo,USHORT ModeIdIndex)
 {
-  USHORT index,temp,romptr=0;
+  UCHAR  *ROMAddr = HwInfo->pjVirtualRomBase;
+  USHORT index,temp,temp1,romptr=0;
 
   temp = GetTVPtrIndex(SiS_Pr);
   temp >>= 1;              	/* 0: NTSC, 1: PAL, 2: HiTV */
+  temp1 = temp;
 
   if(ModeNo<=0x13)
      index = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].VB_StTVEdgeIndex;
@@ -11638,15 +11513,20 @@
      index = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].VB_ExtTVEdgeIndex;
 
   if(ROMAddr && SiS_Pr->SiS_UseROM) {
-     romptr = ROMAddr[0x124] | (ROMAddr[0x125] << 8);
-     if(HwDeviceExtension->jChipType >= SIS_330) {
+     if(HwInfo->jChipType >= SIS_661) {
+	romptr = ROMAddr[0x26c] | (ROMAddr[0x26d] << 8);
+	temp1 = GetOEMTVPtr661(SiS_Pr);
+        temp1 >>= 1;
+     } else if(HwInfo->jChipType >= SIS_330) {
         romptr = ROMAddr[0x1a4] | (ROMAddr[0x1a5] << 8);
+     } else {
+        romptr = ROMAddr[0x124] | (ROMAddr[0x125] << 8);
      }
   }
 
   if(romptr) {
-     temp <<= 1;
-     temp = ROMAddr[romptr + temp + index];
+     temp1 <<= 1;
+     temp = ROMAddr[romptr + temp1 + index];
   } else {
      temp = SiS310_TVEdge1[temp][index];
   }
@@ -11655,90 +11535,119 @@
 }
 
 static void
-SetYFilter(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,USHORT BaseAddr,
-           UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex)
+SetYFilter(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,
+           USHORT ModeNo,USHORT ModeIdIndex)
 {
-  USHORT index, temp, i, j;
-  UCHAR  OutputSelect = *SiS_Pr->pSiS_OutputSelect;
-
-  temp = GetTVPtrIndex(SiS_Pr);
-  temp >>= 1;  			/* 0: NTSC, 1: PAL, 2: HiTV */
+  UCHAR  *ROMAddr = HwInfo->pjVirtualRomBase;
+  USHORT index, myindex, oldindex,temp, i, j, flag1 = 0, flag2 = 0, romptr = 0;
+  ULONG  lindex;
 
-  if (ModeNo<=0x13) {
+  if(ModeNo<=0x13) {
     index =  SiS_Pr->SiS_SModeIDTable[ModeIdIndex].VB_StTVYFilterIndex;
   } else {
     index =  SiS_Pr->SiS_EModeIDTable[ModeIdIndex].VB_ExtTVYFilterIndex;
   }
 
-  if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV)  temp = 1;  /* Hivision TV uses PAL */
-
-  if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-    for(i=0x35, j=0; i<=0x38; i++, j++) {
-       SiS_SetReg1(SiS_Pr->SiS_Part2Port,i,SiS310_TVYFilter2[temp][index][j]);
-    }
-    for(i=0x48; i<=0x4A; i++, j++) {
-       SiS_SetReg1(SiS_Pr->SiS_Part2Port,i,SiS310_TVYFilter2[temp][index][j]);
-    }
-  } else {
-    for(i=0x35, j=0; i<=0x38; i++, j++){
-       SiS_SetReg1(SiS_Pr->SiS_Part2Port,i,SiS310_TVYFilter1[temp][index][j]);
-    }
-  }
+  oldindex = index;
 
-  if(ROMAddr && SiS_Pr->SiS_UseROM) {
-  	OutputSelect = ROMAddr[0xf3];
-	if(HwDeviceExtension->jChipType >= SIS_330) {
-	    OutputSelect = ROMAddr[0x11b];
+  if((HwInfo->jChipType >= SIS_661) && ROMAddr && SiS_Pr->SiS_UseROM) {
+     if(ModeNo > 0x13) {
+        index =  SiS_Pr->SiS_EModeIDTable[ModeIdIndex].VB_ExtTVYFilterIndexROM661;
+     }
+     lindex = GetOEMTVPtr661_2(SiS_Pr);
+     if(lindex & 0x00ff0000) flag1 = 1;
+     if(lindex & 0xff000000) flag2 = 1;
+     lindex &= 0xffff;
+
+     /* NTSC-J: Use PAL filters */
+     if(SiS_Pr->SiS_TVMode & TVSetNTSCJ) lindex = 1;
+
+     romptr = ROMAddr[0x268] | (ROMAddr[0x269] << 8);
+     if(flag1) myindex = index * 7;
+     else      myindex = index << 2;
+
+     if(romptr) {
+        romptr += (lindex << 1);
+        romptr = (ROMAddr[romptr] | (ROMAddr[romptr+1] << 8)) + myindex;
+	if(romptr) {
+           if((!flag1) && (flag2)) {
+	      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x35,0x00);
+	      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x36,0x00);
+	      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x37,0x00);
+	      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x38,ROMAddr[romptr++]);
+           } else {
+	      for(i=0x35; i<=0x38; i++) {
+                 SiS_SetReg(SiS_Pr->SiS_Part2Port,i,ROMAddr[romptr++]);
+              }
+           }
+           if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
+	      for(j=0, i=0x48; i<=0x4a; i++, j++) {
+                 SiS_SetReg(SiS_Pr->SiS_Part2Port,i,ROMAddr[romptr++]);
+              }
+           }
+           return;
 	}
+     }
   }
-  if(OutputSelect & EnablePALMN) {
-      if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x31) & 0x01) {
-         temp = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x38);
-         temp &= (EnablePALM | EnablePALN);
-         if(temp == EnablePALM) {
-              if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-                 for(i=0x35, j=0; i<=0x38; i++, j++) {
-                      SiS_SetReg1(SiS_Pr->SiS_Part2Port,i,SiS310_PALMFilter2[index][j]);
-                 }
-                 for(i=0x48; i<=0x4A; i++, j++) {
-                      SiS_SetReg1(SiS_Pr->SiS_Part2Port,i,SiS310_PALMFilter2[index][j]);
-                 }
-              } else {
-                 for(i=0x35, j=0; i<=0x38; i++, j++) {
-                      SiS_SetReg1(SiS_Pr->SiS_Part2Port,i,SiS310_PALMFilter[index][j]);
-                 }
-              }
-         }
-	 /* PALN : Is this data correct? */
-         if(temp == EnablePALN) {
-              if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-                 for(i=0x35, j=0; i<=0x38; i++, j++) {
-                      SiS_SetReg1(SiS_Pr->SiS_Part2Port,i,SiS310_PALNFilter2[index][j]);
-                 }
-                 for(i=0x48, j=0; i<=0x4A; i++, j++) {
-                      SiS_SetReg1(SiS_Pr->SiS_Part2Port,i,SiS310_PALNFilter2[index][j]);
-                 }
-             } else {
-                 for(i=0x35, j=0; i<=0x38; i++, j++) {
-                      SiS_SetReg1(SiS_Pr->SiS_Part2Port,i,SiS310_PALNFilter[index][j]);
-		 }
-             }
-         }
-      }
+
+  index = oldindex;
+
+  temp = GetTVPtrIndex(SiS_Pr);
+  temp >>= 1;  			/* 0: NTSC, 1: PAL, 2: HiTV */
+
+  if(SiS_Pr->SiS_TVMode & TVSetNTSCJ)	     temp = 1;  /* NTSC-J uses PAL */
+  else if(SiS_Pr->SiS_TVMode & TVSetPALM)    temp = 3;  /* PAL-M */
+  else if(SiS_Pr->SiS_TVMode & TVSetPALN)    temp = 4;  /* PAL-N */
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) temp = 1;  /* HiVision uses PAL */
+
+  if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
+     for(i=0x35, j=0; i<=0x38; i++, j++) {
+        SiS_SetReg(SiS_Pr->SiS_Part2Port,i,SiS310_TVYFilter2[temp][index][j]);
+     }
+     for(i=0x48; i<=0x4A; i++, j++) {
+        SiS_SetReg(SiS_Pr->SiS_Part2Port,i,SiS310_TVYFilter2[temp][index][j]);
+     }
+  } else {
+     for(i=0x35, j=0; i<=0x38; i++, j++) {
+        SiS_SetReg(SiS_Pr->SiS_Part2Port,i,SiS310_TVYFilter1[temp][index][j]);
+     }
   }
 }
 
 static void
-SetPhaseIncr(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,USHORT BaseAddr,
-             UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex)
+SetPhaseIncr(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,
+             USHORT ModeNo,USHORT ModeIdIndex)
 {
-  USHORT index,temp,temp1,i,j,resinfo,romptr=0;
+  UCHAR  *ROMAddr = HwInfo->pjVirtualRomBase;
+  USHORT index,temp,i,j,resinfo,romptr=0;
+  ULONG  lindex;
 
   if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToTV)) return;
 
-  temp1 = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x38);        /* if PALM/N not set */
-  temp1 &= (EnablePALM | EnablePALN);
-  if(temp1) return;
+  /* NTSC-J data not in BIOS, and already set in SetGroup2 */
+  if(SiS_Pr->SiS_TVMode & TVSetNTSCJ) return;
+
+  if(HwInfo->jChipType >= SIS_661) {
+     lindex = GetOEMTVPtr661_2(SiS_Pr) & 0xffff;
+     lindex <<= 2;
+     if((ROMAddr) && SiS_Pr->SiS_UseROM) {
+        romptr = ROMAddr[0x264] | (ROMAddr[0x265] << 8);
+     }
+     if(romptr) {
+	romptr += lindex;
+	for(j=0, i=0x31; i<=0x34; i++, j++) {
+           SiS_SetReg(SiS_Pr->SiS_Part2Port,i,ROMAddr[romptr + j]);
+        }
+     } else {
+        for(j=0, i=0x31; i<=0x34; i++, j++) {
+           SiS_SetReg(SiS_Pr->SiS_Part2Port,i,SiS661_TVPhase[lindex + j]);
+        }
+     }
+     return;
+  }
+
+  /* PAL-M, PAL-N not in BIOS, and already set in SetGroup2 */
+  if(SiS_Pr->SiS_TVMode & (TVSetPALM | TVSetPALN)) return;
 
   if(ModeNo<=0x13) {
      resinfo = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ResInfo;
@@ -11752,17 +11661,17 @@
    */
   if((ROMAddr) && SiS_Pr->SiS_UseROM) {
      romptr = ROMAddr[0x116] | (ROMAddr[0x117] << 8);
-     if(HwDeviceExtension->jChipType >= SIS_330) {
+     if(HwInfo->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(HwInfo->jChipType >= SIS_330) {
 	   romptr = ROMAddr[0x19c] | (ROMAddr[0x19d] << 8);
 	}
-	if((SiS_Pr->SiS_VBInfo & SetInSlaveMode) && (!(SiS_Pr->SiS_SetFlag & TVSimuMode))) {
+	if((SiS_Pr->SiS_VBInfo & SetInSlaveMode) && (!(SiS_Pr->SiS_TVMode & TVSetTVSimuMode))) {
 	   romptr = ROMAddr[0x116] | (ROMAddr[0x117] << 8);
-	   if(HwDeviceExtension->jChipType >= SIS_330) {
+	   if(HwInfo->jChipType >= SIS_330) {
               romptr = ROMAddr[0x196] | (ROMAddr[0x197] << 8);
            }
 	}
@@ -11771,61 +11680,204 @@
   if(romptr) {
      romptr += (temp << 2);
      for(j=0, i=0x31; i<=0x34; i++, j++) {
-        SiS_SetReg1(SiS_Pr->SiS_Part2Port,i,ROMAddr[romptr + j]);
+        SiS_SetReg(SiS_Pr->SiS_Part2Port,i,ROMAddr[romptr + j]);
      }
   } else {
      index = temp % 2;
      temp >>= 1;          /* 0:NTSC, 1:PAL, 2:HiTV */
      for(j=0, i=0x31; i<=0x34; i++, j++) {
         if(!(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV))
-	   SiS_SetReg1(SiS_Pr->SiS_Part2Port,i,SiS310_TVPhaseIncr1[temp][index][j]);
-        else if((!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) || (SiS_Pr->SiS_SetFlag & TVSimuMode))
-           SiS_SetReg1(SiS_Pr->SiS_Part2Port,i,SiS310_TVPhaseIncr2[temp][index][j]);
+	   SiS_SetReg(SiS_Pr->SiS_Part2Port,i,SiS310_TVPhaseIncr1[temp][index][j]);
+        else if((!(SiS_Pr->SiS_VBInfo & SetInSlaveMode)) || (SiS_Pr->SiS_TVMode & TVSetTVSimuMode))
+           SiS_SetReg(SiS_Pr->SiS_Part2Port,i,SiS310_TVPhaseIncr2[temp][index][j]);
         else
-           SiS_SetReg1(SiS_Pr->SiS_Part2Port,i,SiS310_TVPhaseIncr1[temp][index][j]);
+           SiS_SetReg(SiS_Pr->SiS_Part2Port,i,SiS310_TVPhaseIncr1[temp][index][j]);
      }
   }
 
-  if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {    /* 650/301LV: (VB_SIS301LV | VB_SIS302LV)) */
-     if((!(SiS_Pr->SiS_VBInfo & SetPALTV)) && (ModeNo > 0x13)) {
-        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 == 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);
+  if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
+     if((!(SiS_Pr->SiS_TVMode & TVSetPAL)) && (ModeNo > 0x13)) {
+        if((resinfo == SIS_RI_640x480) ||
+	   (resinfo == SIS_RI_800x600)) {
+	      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x31,0x21);
+	      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x32,0xf0);
+	      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x33,0xf5);
+	      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x34,0x7f);
 	} 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);
-	      SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x34,0x7b);
+	      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x31,0x1e);
+	      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x32,0x8b);
+	      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x33,0xfb);
+	      SiS_SetReg(SiS_Pr->SiS_Part2Port,0x34,0x7b);
 	}
      }
   }
 }
 
 void
-SiS_OEM310Setting(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,USHORT BaseAddr,
-                  UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex)
+SiS_OEM310Setting(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,
+                  USHORT ModeNo,USHORT ModeIdIndex)
 {
-   SetDelayComp(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo);
+   SetDelayComp(SiS_Pr,HwInfo,ModeNo);
 
    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);
-       SetYFilter(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,ModeIdIndex);
+   if((SiS_Pr->SiS_VBType & VB_SISVB) && (SiS_Pr->SiS_VBInfo & SetCRT2ToTV)) {
+       SetAntiFlicker(SiS_Pr,HwInfo,ModeNo,ModeIdIndex);
+       SetPhaseIncr(SiS_Pr,HwInfo,ModeNo,ModeIdIndex);
+       SetYFilter(SiS_Pr,HwInfo,ModeNo,ModeIdIndex);
        if(!(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV)) {
-          SetEdgeEnhance(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,ModeIdIndex);
+          SetEdgeEnhance(SiS_Pr,HwInfo,ModeNo,ModeIdIndex);
        }
    }
 }
 
+static void
+SetDelayComp661(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo, USHORT ModeNo,
+                USHORT ModeIdIndex, USHORT RTI)
+{
+   UCHAR  *ROMAddr = HwInfo->pjVirtualRomBase;
+   USHORT delay = 0, romptr = 0, index;
+   UCHAR  *myptr = NULL;
+   UCHAR  temp;
+
+   if(!(SiS_Pr->SiS_VBInfo & (SetCRT2ToTV | SetCRT2ToLCD | SetCRT2ToLCDA | SetCRT2ToRAMDAC)))
+      return;
+
+   delay = SiS_Pr->SiS_RefIndex[RTI].Ext_PDC;
+
+   delay &= 0xf0;
+   delay >>= 4;
+   if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) delay <<= 12;  /* BIOS: 8, wrong */
+
+   if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+      index = GetOEMTVPtr661(SiS_Pr);
+      if((ROMAddr) && SiS_Pr->SiS_UseROM) {
+         romptr = ROMAddr[0x25c] | (ROMAddr[0x25d] << 8);
+         if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
+            romptr = ROMAddr[0x25e] | (ROMAddr[0x25f] << 8);
+         }
+      }
+      if(romptr) myptr = &ROMAddr[romptr];
+      if(!myptr) {
+         myptr = (UCHAR *)SiS_TVDelay661_301;
+	 if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
+	    myptr = (UCHAR *)SiS_TVDelay661_301B;
+	 }
+      }
+      delay = myptr[index];
+      if(SiS_GetReg(SiS_Pr->SiS_Part1Port,0x13) & 0x04) delay >>= 4;  /* Should test dual edge */
+   } else if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
+      if(SiS_Pr->PDC) {
+         delay = SiS_Pr->PDC & 0x0f;
+	 if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
+            delay |= (delay << 12);
+         }
+      } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_PanelCustom) {
+         delay = 0x4444;  /* TEST THIS */
+      } else if(!(SiS_Pr->SiS_LCDInfo & LCDPass11)) {
+         myptr = GetLCDStructPtr661(SiS_Pr, HwInfo);
+         if(myptr) delay = myptr[4];
+         else delay = 0x44;
+         if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
+            delay |= (delay << 8);
+         }
+      }
+   }
+
+   temp = SiS_GetReg(SiS_Pr->SiS_Part1Port,0x2d);
+   if(SiS_Pr->SiS_VBInfo & (SetCRT2ToTV | SetCRT2ToLCD | SetCRT2ToRAMDAC)) {
+      temp &= 0xf0;
+      temp |= (delay & 0x000f);
+   }
+   if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
+      temp &= 0x0f;
+      temp |= ((delay & 0xf000) >> 8);
+   }
+   SiS_SetReg(SiS_Pr->SiS_Part1Port,0x2d,temp);
+}
+
+static void
+SetCRT2SyncDither661(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo, USHORT ModeNo, USHORT RTI)
+{
+   USHORT infoflag;
+   UCHAR temp;
+
+   infoflag = SiS_Pr->SiS_RefIndex[RTI].Ext_InfoFlag;
+   if(ModeNo <= 0x13) {
+      infoflag = SiS_GetRegByte(SiS_Pr->SiS_P3ca+2);
+   }
+   infoflag &= 0xc0;
+   if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
+      temp = SiS_GetReg(SiS_Pr->SiS_P3d4,0x37);
+      if(temp & 0x20) infoflag = temp;
+      if(temp & 0x01) infoflag |= 0x01;
+   }
+   if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
+      temp = 0x0c;
+      if(infoflag & 0x01) temp ^= 0x14;  /* BIOS: 18, wrong */
+      temp |= (infoflag >> 6);
+      SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x1a,0xe0,temp);
+   }
+   if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
+      temp = 0;
+      if(infoflag & 0x01) temp |= 0x80;
+      SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x1a,0x7f,temp);
+      temp = 0x30;
+      if(infoflag & 0x01) temp = 0x20;
+      infoflag &= 0xc0;
+      temp |= infoflag;
+      SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x19,0x0f,temp);
+   }
+}
+
+static void
+SetPanelParms661(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
+{
+   UCHAR *myptr;
+
+   if(SiS_Pr->SiS_VBType & (VB_SIS301LV | VB_SIS302LV | VB_SIS302ELV)) {
+      if(SiS_Pr->LVDSHL != -1) {
+         SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x24,0xfc,SiS_Pr->LVDSHL);
+      }
+   }
+
+   myptr = GetLCDStructPtr661(SiS_Pr, HwInfo);
+   if(myptr) {
+      if(SiS_Pr->SiS_VBType & (VB_SIS301LV | VB_SIS302LV | VB_SIS302ELV)) {
+         if(SiS_Pr->LVDSHL == -1) {
+            SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x24,0xE0,myptr[1] & 0x1f);
+	 } else {
+	    SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x24,0xE3,myptr[1] & 0x1c);
+	 }
+      }
+      SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x0d,0x3f,myptr[2] & 0xc0);
+   }
+}
+
+void
+SiS_OEM661Setting(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,
+                  USHORT ModeNo,USHORT ModeIdIndex, USHORT RRTI)
+{
+   if(SiS_Pr->SiS_VBType & VB_SISVB) {
+
+      SetDelayComp661(SiS_Pr,HwInfo,ModeNo,ModeIdIndex,RRTI);
+
+      if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
+         SetCRT2SyncDither661(SiS_Pr,HwInfo,ModeNo,RRTI);
+         SetPanelParms661(SiS_Pr,HwInfo);
+      }
+
+      if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
+         SetPhaseIncr(SiS_Pr,HwInfo,ModeNo,ModeIdIndex);
+         SetYFilter(SiS_Pr,HwInfo,ModeNo,ModeIdIndex);
+         SetAntiFlicker(SiS_Pr,HwInfo,ModeNo,ModeIdIndex);
+         if(!(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV)) {
+            SetEdgeEnhance(SiS_Pr,HwInfo,ModeNo,ModeIdIndex);
+         }
+      }
+   }
+}
+
 /* FinalizeLCD
  * This finalizes some CRT2 registers for the very panel used.
  * If we have a backup if these registers, we use it; otherwise
@@ -11834,8 +11886,8 @@
  * pray that we have a backup...
  */
 void
-SiS_FinalizeLCD(SiS_Private *SiS_Pr, USHORT BaseAddr,UCHAR *ROMAddr,USHORT ModeNo,
-                USHORT ModeIdIndex, PSIS_HW_DEVICE_INFO HwDeviceExtension)
+SiS_FinalizeLCD(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,
+                PSIS_HW_INFO HwInfo)
 {
   USHORT tempcl,tempch,tempbl,tempbh,tempbx,tempax,temp;
   USHORT resinfo,modeflag;
@@ -11851,18 +11903,24 @@
   if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_PanelCustom) return;
   if(SiS_Pr->UseCustomMode) return;
 
-  if(SiS_Pr->SiS_CustomT == CUT_COMPAQ12802) return;
+  switch(SiS_Pr->SiS_CustomT) {
+  case CUT_COMPAQ1280:
+  case CUT_COMPAQ12802:
+  case CUT_CLEVO1400:
+  case CUT_CLEVO14002:
+     return;
+  }
 
   if(ModeNo <= 0x13) {
-	resinfo = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ResInfo;
-	modeflag =  SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
+     resinfo = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ResInfo;
+     modeflag =  SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
   } else {
-    	resinfo = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO;
-	modeflag =  SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
+     resinfo = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO;
+     modeflag =  SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
   }
 
   if(IS_SIS650) {
-     if(!(SiS_GetReg1(SiS_Pr->SiS_P3d4, 0x5f) & 0xf0)) {
+     if(!(SiS_GetReg(SiS_Pr->SiS_P3d4, 0x5f) & 0xf0)) {
         if(SiS_Pr->SiS_CustomT == CUT_CLEVO1024) {
 	   SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x1e,0x02);
 	} else {
@@ -11872,7 +11930,8 @@
   }
 
   if(SiS_Pr->SiS_CustomT == CUT_CLEVO1024) {
-     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {  /* Maybe all panels? */
+     if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
+        /* Maybe all panels? */
         if(SiS_Pr->LVDSHL == -1) {
            SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x24,0xfc,0x01);
 	}
@@ -11882,17 +11941,18 @@
 
   if(SiS_Pr->SiS_CustomT == CUT_CLEVO10242) {
      if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
-        if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {  /* Maybe all panels? */
+        if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
 	   if(SiS_Pr->LVDSHL == -1) {
+	      /* Maybe all panels? */
               SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x24,0xfc,0x01);
 	   }
 	   if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
-	      tempch = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x36) >> 4;
+	      tempch = SiS_GetReg(SiS_Pr->SiS_P3d4,0x36) >> 4;
 	      if(tempch == 3) {
-	         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);
+	         SiS_SetReg(SiS_Pr->SiS_Part1Port,0x18,0x02);
+	         SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1b,0x25);
+	         SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1c,0x00);
+	         SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1d,0x1b);
 	      }
 	   }
 	   return;
@@ -11902,128 +11962,122 @@
 
   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) {   /* Maybe all panels? */
+#ifdef SET_EMI
+	if(SiS_Pr->SiS_VBType & (VB_SIS302LV | VB_SIS302ELV)) {
+	   SiS_SetReg(SiS_Pr->SiS_Part4Port,0x2a,0x00);
+	   SiS_SetReg(SiS_Pr->SiS_Part4Port,0x30,0x00);
+	   SiS_SetReg(SiS_Pr->SiS_Part4Port,0x34,0x10);
+	}
+#endif
+     } else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
         if(SiS_Pr->LVDSHL == -1) {
            /* Maybe ACER only? */
            SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x24,0xfc,0x01);
 	}
      }
-     tempch = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x36);
-     tempch &= 0xf0;
-     tempch >>= 4;
+     tempch = SiS_GetReg(SiS_Pr->SiS_P3d4,0x36) >> 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);
+	   SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1f,0x76);
 	} else if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
+	   if(tempch == 0x03) {
+	      SiS_SetReg(SiS_Pr->SiS_Part1Port,0x18,0x02);
+	      SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1b,0x25);
+	      SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1c,0x00);
+	      SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1d,0x1b);
+	   }
 	   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);
-	      SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x16,SiS_Pr->Backup_16);
-	      SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x17,SiS_Pr->Backup_17);
-	      SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x18,SiS_Pr->Backup_18);
-	      SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x19,SiS_Pr->Backup_19);
-	      SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1a,SiS_Pr->Backup_1a);
-	      SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1b,SiS_Pr->Backup_1b);
-	      SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1c,SiS_Pr->Backup_1c);
-	      SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1d,SiS_Pr->Backup_1d);
-	   } else if(!(SiS_Pr->SiS_LCDInfo & DontExpandLCD)) {	/* From 1.10.8w */
-	       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;
-		  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x18,0x18);
-		  if((resinfo == 1) || (resinfo == 3)) return;
-	       }
-	       SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x18,0x02);
-	       if((ModeNo > 0x13) && (resinfo == SIS_RI_1024x768)) {
-	          SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x18,0x02);  /* 1.10.7u */
+	      SiS_SetReg(SiS_Pr->SiS_Part1Port,0x14,SiS_Pr->Backup_14);
+	      SiS_SetReg(SiS_Pr->SiS_Part1Port,0x15,SiS_Pr->Backup_15);
+	      SiS_SetReg(SiS_Pr->SiS_Part1Port,0x16,SiS_Pr->Backup_16);
+	      SiS_SetReg(SiS_Pr->SiS_Part1Port,0x17,SiS_Pr->Backup_17);
+	      SiS_SetReg(SiS_Pr->SiS_Part1Port,0x18,SiS_Pr->Backup_18);
+	      SiS_SetReg(SiS_Pr->SiS_Part1Port,0x19,SiS_Pr->Backup_19);
+	      SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1a,SiS_Pr->Backup_1a);
+	      SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1b,SiS_Pr->Backup_1b);
+	      SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1c,SiS_Pr->Backup_1c);
+	      SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1d,SiS_Pr->Backup_1d);
+	   } else if(!(SiS_Pr->SiS_LCDInfo & DontExpandLCD)) {	/* 1.10.8w */
+	      SiS_SetReg(SiS_Pr->SiS_Part1Port,0x14,0x90);
+	      if(ModeNo <= 0x13) {
+	         SiS_SetReg(SiS_Pr->SiS_Part1Port,0x18,0x11);
+		 if((resinfo == 0) || (resinfo == 2)) return;
+		 SiS_SetReg(SiS_Pr->SiS_Part1Port,0x18,0x18);
+		 if((resinfo == 1) || (resinfo == 3)) return;
+	      }
+	      SiS_SetReg(SiS_Pr->SiS_Part1Port,0x18,0x02);
+	      if((ModeNo > 0x13) && (resinfo == SIS_RI_1024x768)) {
+	         SiS_SetReg(SiS_Pr->SiS_Part1Port,0x18,0x02);  /* 1.10.7u */
 #if 0
-	          tempbx = 806;  /* 0x326 */			 /* other older BIOSes */
-		  tempbx--;
-		  temp = tempbx & 0xff;
-		  SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1b,temp);
-		  temp = (tempbx >> 8) & 0x03;
-		  SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x1d,0xf8,temp);
-#endif		  
-	       }
-	   } else {
-	       if(ModeNo <= 0x13) {
-	          if(ModeNo <= 1) {
-		     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x18,0x70);
-		     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x19,0xff);
-		     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1b,0x48);
-		     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1d,0x12);
-		  }
-		  if(!(modeflag & HalfDCLK)) {
-		     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x14,0x20);
-		     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x15,0x1a);
-		     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x16,0x28);
-		     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x17,0x00);
-		     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x18,0x4c);
-		     SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x19,0xdc);
-		     if(ModeNo == 0x12) {
-			 switch(tempch) {
-			 case 0:
-			    SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x18,0x95);
-			    SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x19,0xdc);
-			    SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1a,0x10);
-			    SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1b,0x95);
-			    SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1c,0x48);
-			    SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1d,0x12);
-			    break;
-			 case 2:
-			    SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x18,0x95);
-			    SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1b,0x48);
-			    break;
-			 case 3:
-			    SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x1b,0x95);
-			    break;
-			 }
-		     }
-		  }
-	       }
+	         tempbx = 806;  /* 0x326 */			 /* other older BIOSes */
+		 tempbx--;
+		 temp = tempbx & 0xff;
+		 SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1b,temp);
+		 temp = (tempbx >> 8) & 0x03;
+		 SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x1d,0xf8,temp);
+#endif
+	      }
+	   } else if(ModeNo <= 0x13) {
+	      if(ModeNo <= 1) {
+		 SiS_SetReg(SiS_Pr->SiS_Part1Port,0x18,0x70);
+		 SiS_SetReg(SiS_Pr->SiS_Part1Port,0x19,0xff);
+		 SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1b,0x48);
+		 SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1d,0x12);
+	      }
+	      if(!(modeflag & HalfDCLK)) {
+		 SiS_SetReg(SiS_Pr->SiS_Part1Port,0x14,0x20);
+		 SiS_SetReg(SiS_Pr->SiS_Part1Port,0x15,0x1a);
+		 SiS_SetReg(SiS_Pr->SiS_Part1Port,0x16,0x28);
+		 SiS_SetReg(SiS_Pr->SiS_Part1Port,0x17,0x00);
+		 SiS_SetReg(SiS_Pr->SiS_Part1Port,0x18,0x4c);
+		 SiS_SetReg(SiS_Pr->SiS_Part1Port,0x19,0xdc);
+		 if(ModeNo == 0x12) {
+		    switch(tempch) {
+		       case 0:
+			  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x18,0x95);
+			  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x19,0xdc);
+			  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1a,0x10);
+			  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1b,0x95);
+			  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1c,0x48);
+			  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1d,0x12);
+			  break;
+		       case 2:
+			  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x18,0x95);
+			  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1b,0x48);
+			  break;
+		       case 3:
+			  SiS_SetReg(SiS_Pr->SiS_Part1Port,0x1b,0x95);
+			  break;
+		    }
+		 }
+	      }
 	   }
 	}
      } else {
-        tempcl = tempbh = SiS_GetReg1(SiS_Pr->SiS_Part2Port,0x01);
+        tempcl = tempbh = SiS_GetReg(SiS_Pr->SiS_Part2Port,0x01);
 	tempcl &= 0x0f;
 	tempbh &= 0x70;
 	tempbh >>= 4;
-	tempbl = SiS_GetReg1(SiS_Pr->SiS_Part2Port,0x04);
+	tempbl = SiS_GetReg(SiS_Pr->SiS_Part2Port,0x04);
 	tempbx = (tempbh << 8) | tempbl;
 	if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
 	   if((resinfo == SIS_RI_1024x768) || (!(SiS_Pr->SiS_LCDInfo & DontExpandLCD))) {
 	      if(SiS_Pr->SiS_SetFlag & LCDVESATiming) {
-	      	tempbx = 770;
+	      	 tempbx = 770;
 	      } else {
-	        if(tempbx > 770) tempbx = 770;
-		if(SiS_Pr->SiS_VGAVDE < 600) {
-		   tempax = 768 - SiS_Pr->SiS_VGAVDE;
-		   tempax >>= 4;  				/* From 1.10.7w; 1.10.6s: 3;  */
-		   if(SiS_Pr->SiS_VGAVDE <= 480)  tempax >>= 4; /* From 1.10.7w; 1.10.6s: < 480; >>=1; */
-		   tempbx -= tempax;
-		}
+	         if(tempbx > 770) tempbx = 770;
+		 if(SiS_Pr->SiS_VGAVDE < 600) {
+		    tempax = 768 - SiS_Pr->SiS_VGAVDE;
+		    tempax >>= 4;  				 /* 1.10.7w; 1.10.6s: 3;  */
+		    if(SiS_Pr->SiS_VGAVDE <= 480)  tempax >>= 4; /* 1.10.7w; 1.10.6s: < 480; >>=1; */
+		    tempbx -= tempax;
+		 }
 	      }
 	   } else return;
 	}
-#if 0
-	if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1400x1050) {
-	}
-#endif
 	temp = tempbx & 0xff;
-	SiS_SetReg1(SiS_Pr->SiS_Part2Port,0x04,temp);
-	temp = (tempbx & 0xff00) >> 8;
-	temp <<= 4;
-	temp |= tempcl;
+	SiS_SetReg(SiS_Pr->SiS_Part2Port,0x04,temp);
+	temp = ((tempbx & 0xff00) >> 4) | tempcl;
 	SiS_SetRegANDOR(SiS_Pr->SiS_Part2Port,0x01,0x80,temp);
      }
   }
@@ -12031,25 +12085,24 @@
 
 #endif
 
-
 /*  =================  SiS 300 O.E.M. ================== */
 
 #ifdef SIS300
 
 void
-SetOEMLCDData2(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,USHORT BaseAddr,
-              UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex, USHORT RefTabIndex)
+SetOEMLCDData2(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,
+               USHORT ModeNo,USHORT ModeIdIndex, USHORT RefTabIndex)
 {
   USHORT crt2crtc=0, modeflag, myindex=0;
   UCHAR  temp;
   int i;
 
   if(ModeNo <= 0x13) {
-        modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
-	crt2crtc = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_CRT2CRTC;
+     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;
+     modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
+     crt2crtc = SiS_Pr->SiS_RefIndex[RefTabIndex].Ext_CRT2CRTC;
   }
 
   crt2crtc &= 0x3f;
@@ -12061,7 +12114,7 @@
   if(SiS_Pr->SiS_CustomT == CUT_BARCO1366) {
      if(modeflag & HalfDCLK) myindex = 1;
 
-     if(SiS_LowModeStuff(SiS_Pr,ModeNo,HwDeviceExtension)) {
+     if(SiS_Pr->SiS_SetFlag & LowModeTests) {
         for(i=0; i<7; i++) {
            if(barco_p1[myindex][crt2crtc][i][0]) {
 	      SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,
@@ -12071,46 +12124,19 @@
 	   }
         }
      }
-     temp = SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x00);
+     temp = SiS_GetReg(SiS_Pr->SiS_Part1Port,0x00);
      if(temp & 0x80) {
-        temp = SiS_GetReg1(SiS_Pr->SiS_Part1Port,0x18);
+        temp = SiS_GetReg(SiS_Pr->SiS_Part1Port,0x18);
         temp++;
-        SiS_SetReg1(SiS_Pr->SiS_Part1Port,0x18,temp);
+        SiS_SetReg(SiS_Pr->SiS_Part1Port,0x18,temp);
      }
   }
 }
 
-#if 0   /* Not used */
-static USHORT
-GetRevisionID(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension)
-{
-   ULONG temp1;
-#ifndef LINUX_XF86
-   ULONG base;
-#endif
-   USHORT temp2 = 0;
-
-   if((HwDeviceExtension->jChipType==SIS_540)||
-      (HwDeviceExtension->jChipType==SIS_630)||
-      (HwDeviceExtension->jChipType==SIS_730)) {
-#ifndef LINUX_XF86
-     	base = 0x80000008;
-     	OutPortLong(base,0xcf8);
-     	temp1 = InPortLong(0xcfc);
-#else
-	temp1=pciReadLong(0x00000000, 0x08);
-#endif
-     	temp1 &= 0x000000FF;
-     	temp2 = (USHORT)(temp1);
-    	return temp2;
-   }
-   return 0;
-}
-#endif
-
 static USHORT
-GetOEMLCDPtr(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension, UCHAR *ROMAddr, int Flag)
+GetOEMLCDPtr(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo, int Flag)
 {
+  UCHAR  *ROMAddr = HwInfo->pjVirtualRomBase;
   USHORT tempbx=0,romptr=0;
   UCHAR customtable300[] = {
   	0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
@@ -12121,9 +12147,9 @@
 	0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
   };
 
-  if(HwDeviceExtension->jChipType == SIS_300) {
+  if(HwInfo->jChipType == SIS_300) {
 
-    tempbx = (SiS_GetReg1(SiS_Pr->SiS_P3d4,0x36) & 0x0f) - 2;
+    tempbx = (SiS_GetReg(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;
@@ -12132,13 +12158,13 @@
        if(ROMAddr[0x235] & 0x80) {
           tempbx = SiS_Pr->SiS_LCDTypeInfo;
           if(Flag) {
-	    romptr = ROMAddr[0x255] | (ROMAddr[0x256] << 8);
-	    if(romptr) {
+	     romptr = ROMAddr[0x255] | (ROMAddr[0x256] << 8);
+	     if(romptr) {
 	        tempbx = ROMAddr[romptr + SiS_Pr->SiS_LCDTypeInfo];
-	    } else {
+	     } else {
 	        tempbx = customtable300[SiS_Pr->SiS_LCDTypeInfo];
-	    }
-            if(tempbx == 0xFF) return 0xFFFF;
+	     }
+             if(tempbx == 0xFF) return 0xFFFF;
           }
 	  tempbx <<= 1;
 	  if(!(SiS_Pr->SiS_SetFlag & LCDVESATiming)) tempbx++;
@@ -12148,21 +12174,21 @@
   } else {
 
     if(Flag) {
-      if((ROMAddr) && SiS_Pr->SiS_UseROM) {
-         romptr = ROMAddr[0x255] | (ROMAddr[0x256] << 8);
-	 if(romptr) {
-	    tempbx = ROMAddr[romptr + SiS_Pr->SiS_LCDTypeInfo];
-	 } else {
-	    tempbx = 0xff;
-	 }
-      } else {
-         tempbx = customtable630[SiS_Pr->SiS_LCDTypeInfo];
-      }
-      if(tempbx == 0xFF) return 0xFFFF;
-      tempbx <<= 2;
-      if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) tempbx += 2;
-      if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) tempbx++;
-      return tempbx;
+       if((ROMAddr) && SiS_Pr->SiS_UseROM) {
+          romptr = ROMAddr[0x255] | (ROMAddr[0x256] << 8);
+	  if(romptr) {
+	     tempbx = ROMAddr[romptr + SiS_Pr->SiS_LCDTypeInfo];
+	  } else {
+	     tempbx = 0xff;
+	  }
+       } else {
+          tempbx = customtable630[SiS_Pr->SiS_LCDTypeInfo];
+       }
+       if(tempbx == 0xFF) return 0xFFFF;
+       tempbx <<= 2;
+       if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) tempbx += 2;
+       if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) tempbx++;
+       return tempbx;
     }
     tempbx = SiS_Pr->SiS_LCDTypeInfo << 2;
     if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) tempbx += 2;
@@ -12172,9 +12198,10 @@
 }
 
 static void
-SetOEMLCDDelay(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,USHORT BaseAddr,
-               UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex)
+SetOEMLCDDelay(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,
+               USHORT ModeNo,USHORT ModeIdIndex)
 {
+  UCHAR  *ROMAddr = HwInfo->pjVirtualRomBase;
   USHORT index,temp,romptr=0;
 
   if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_PanelCustom) return;
@@ -12192,63 +12219,64 @@
    * 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;
+  if(SiS_Pr->PDC) return;
 
-  temp = GetOEMLCDPtr(SiS_Pr,HwDeviceExtension, ROMAddr, 0);
+  temp = GetOEMLCDPtr(SiS_Pr,HwInfo, 0);
 
   if(SiS_Pr->UseCustomMode)
      index = 0;
   else
      index = SiS_Pr->SiS_VBModeIDTable[ModeIdIndex].VB_LCDDelayIndex;
 
-  if(HwDeviceExtension->jChipType != SIS_300) {
+  if(HwInfo->jChipType != SIS_300) {
+     if(romptr) {
+	romptr += (temp * 2);
+	romptr = ROMAddr[romptr] | (ROMAddr[romptr + 1] << 8);
+	romptr += index;
+	temp = ROMAddr[romptr];
+     } else {
+	if(SiS_Pr->SiS_VBType & VB_SISVB) {
+    	   temp = SiS300_OEMLCDDelay2[temp][index];
+	} else {
+           temp = SiS300_OEMLCDDelay3[temp][index];
+        }
+     }
+  } else {
+     if((ROMAddr) && SiS_Pr->SiS_UseROM && (ROMAddr[0x235] & 0x80)) {
 	if(romptr) {
 	   romptr += (temp * 2);
 	   romptr = ROMAddr[romptr] | (ROMAddr[romptr + 1] << 8);
 	   romptr += index;
 	   temp = ROMAddr[romptr];
 	} else {
-	   if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
-    	      temp = SiS300_OEMLCDDelay2[temp][index];
+	   temp = SiS300_OEMLCDDelay5[temp][index];
+	}
+     } else {
+        if((ROMAddr) && SiS_Pr->SiS_UseROM) {
+	   romptr = ROMAddr[0x249] | (ROMAddr[0x24a] << 8);
+	   if(romptr) {
+	      romptr += (temp * 2);
+	      romptr = ROMAddr[romptr] | (ROMAddr[romptr + 1] << 8);
+	      romptr += index;
+	      temp = ROMAddr[romptr];
 	   } else {
-              temp = SiS300_OEMLCDDelay3[temp][index];
-           }
+	      temp = SiS300_OEMLCDDelay4[temp][index];
+	   }
+	} else {
+	   temp = SiS300_OEMLCDDelay4[temp][index];
 	}
-  } else {
-      if((ROMAddr) && SiS_Pr->SiS_UseROM && (ROMAddr[0x235] & 0x80)) {
-	  if(romptr) {
-	     romptr += (temp * 2);
-	     romptr = ROMAddr[romptr] | (ROMAddr[romptr + 1] << 8);
-	     romptr += index;
-	     temp = ROMAddr[romptr];
-	  } else {
-	     temp = SiS300_OEMLCDDelay5[temp][index];
-	  }
-      } else {
-          if((ROMAddr) && SiS_Pr->SiS_UseROM) {
-	     romptr = ROMAddr[0x249] | (ROMAddr[0x24a] << 8);
-	     if(romptr) {
-	        romptr += (temp * 2);
-	        romptr = ROMAddr[romptr] | (ROMAddr[romptr + 1] << 8);
-	        romptr += index;
-	        temp = ROMAddr[romptr];
-	     } else {
-	        temp = SiS300_OEMLCDDelay4[temp][index];
-	     }
-	  } else {
-	     temp = SiS300_OEMLCDDelay4[temp][index];
-	  }
-      }
+     }
   }
   temp &= 0x3c;
   SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x13,~0x3C,temp);  /* index 0A D[6:4] */
 }
 
 static void
-SetOEMLCDData(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,USHORT BaseAddr,
-              UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex)
+SetOEMLCDData(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,
+              USHORT ModeNo,USHORT ModeIdIndex)
 {
 #if 0  /* Unfinished; Data table missing */
+  UCHAR  *ROMAddr = HwInfo->pjVirtualRomBase;
   USHORT index,temp;
 
   if((ROMAddr) && SiS_Pr->SiS_UseROM) {
@@ -12257,21 +12285,21 @@
      /* No rom pointer in BIOS header! */
   }
 
-  temp = GetOEMLCDPtr(SiS_Pr,HwDeviceExtension, ROMAddr, 1);
+  temp = GetOEMLCDPtr(SiS_Pr,HwInfo, 1);
   if(temp = 0xFFFF) return;
 
   index = SiS_Pr->SiS_VBModeIDTable[ModeIdIndex]._VB_LCDHIndex;
   for(i=0x14, j=0; i<=0x17; i++, j++) {
-      SiS_SetReg1(SiS_Pr->SiS_Part1Port,i,SiS300_LCDHData[temp][index][j]);
+      SiS_SetReg(SiS_Pr->SiS_Part1Port,i,SiS300_LCDHData[temp][index][j]);
   }
   SiS_SetRegANDOR(SiS_SiS_Part1Port,0x1a, 0xf8, (SiS300_LCDHData[temp][index][j] & 0x07));
 
   index = SiS_Pr->SiS_VBModeIDTable[ModeIdIndex]._VB_LCDVIndex;
-  SiS_SetReg1(SiS_SiS_Part1Port,0x18, SiS300_LCDVData[temp][index][0]);
+  SiS_SetReg(SiS_SiS_Part1Port,0x18, SiS300_LCDVData[temp][index][0]);
   SiS_SetRegANDOR(SiS_SiS_Part1Port,0x19, 0xF0, SiS300_LCDVData[temp][index][1]);
   SiS_SetRegANDOR(SiS_SiS_Part1Port,0x1A, 0xC7, (SiS300_LCDVData[temp][index][2] & 0x38));
   for(i=0x1b, j=3; i<=0x1d; i++, j++) {
-      SiS_SetReg1(SiS_Pr->SiS_Part1Port,i,SiS300_LCDVData[temp][index][j]);
+      SiS_SetReg(SiS_Pr->SiS_Part1Port,i,SiS300_LCDVData[temp][index][j]);
   }
 #endif
 }
@@ -12283,21 +12311,22 @@
 
   index = 0;
   if(!(SiS_Pr->SiS_VBInfo & SetInSlaveMode))  index += 4;
-  if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
+  if(SiS_Pr->SiS_VBType & VB_SISVB) {
      if(SiS_Pr->SiS_VBInfo & SetCRT2ToSCART)  index += 2;
-     else if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) index += 3;
-     else if(SiS_Pr->SiS_VBInfo & SetPALTV)   index += 1;
+     else if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) index += 3;
+     else if(SiS_Pr->SiS_TVMode & TVSetPAL)   index += 1;
   } else {
-     if(SiS_Pr->SiS_VBInfo & SetCHTVOverScan) index += 2;
-     if(SiS_Pr->SiS_VBInfo & SetPALTV)        index += 1;
+     if(SiS_Pr->SiS_TVMode & TVSetCHOverScan) index += 2;
+     if(SiS_Pr->SiS_TVMode & TVSetPAL)        index += 1;
   }
   return index;
 }
 
 static void
-SetOEMTVDelay(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,USHORT BaseAddr,
-              UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex)
+SetOEMTVDelay(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,
+              USHORT ModeNo,USHORT ModeIdIndex)
 {
+  UCHAR  *ROMAddr = HwInfo->pjVirtualRomBase;
   USHORT index,temp,romptr=0;
 
   if((ROMAddr) && SiS_Pr->SiS_UseROM) {
@@ -12316,7 +12345,7 @@
      romptr += index;
      temp = ROMAddr[romptr];
   } else {
-     if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
+     if(SiS_Pr->SiS_VBType & VB_SISVB) {
         temp = SiS300_OEMTVDelay301[temp][index];
      } else {
         temp = SiS300_OEMTVDelayLVDS[temp][index];
@@ -12327,10 +12356,10 @@
 }
 
 static void
-SetOEMAntiFlicker(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,
-                  USHORT BaseAddr,UCHAR *ROMAddr,USHORT ModeNo,
-		  USHORT ModeIdIndex)
+SetOEMAntiFlicker(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,
+                  USHORT ModeNo, USHORT ModeIdIndex)
 {
+  UCHAR  *ROMAddr = HwInfo->pjVirtualRomBase;
   USHORT index,temp,romptr=0;
 
   if((ROMAddr) && SiS_Pr->SiS_UseROM) {
@@ -12356,12 +12385,15 @@
 }
 
 static void
-SetOEMPhaseIncr(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,USHORT BaseAddr,
-                UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex)
+SetOEMPhaseIncr(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,
+                USHORT ModeNo,USHORT ModeIdIndex)
 {
+  UCHAR  *ROMAddr = HwInfo->pjVirtualRomBase;
   USHORT index,i,j,temp,romptr=0;
 
-  if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVisionTV) return;
+  if(SiS_Pr->SiS_VBInfo & SetCRT2ToHiVision) return;
+
+  if(SiS_Pr->SiS_TVMode & (TVSetNTSC1024 | TVSetNTSCJ | TVSetPALM | TVSetPALN)) return;
 
   if((ROMAddr) && SiS_Pr->SiS_UseROM) {
      if(!(ROMAddr[0x238] & 0x01)) return;
@@ -12374,32 +12406,33 @@
   index = SiS_Pr->SiS_VBModeIDTable[ModeIdIndex].VB_TVPhaseIndex;
 
   if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
-       for(i=0x31, j=0; i<=0x34; i++, j++) {
-          SiS_SetReg1(SiS_Pr->SiS_Part2Port,i,SiS300_Phase2[temp][index][j]);
-       }
+     for(i=0x31, j=0; i<=0x34; i++, j++) {
+        SiS_SetReg(SiS_Pr->SiS_Part2Port,i,SiS300_Phase2[temp][index][j]);
+     }
   } else {
-       if(romptr) {
-          romptr += (temp * 2);
-	  romptr = ROMAddr[romptr] | (ROMAddr[romptr + 1] << 8);
-	  romptr += (index * 4);
-          for(i=0x31, j=0; i<=0x34; i++, j++) {
-	     SiS_SetReg1(SiS_Pr->SiS_Part2Port,i,ROMAddr[romptr + j]);
-	  }
-       } else {
-          for(i=0x31, j=0; i<=0x34; i++, j++) {
-             SiS_SetReg1(SiS_Pr->SiS_Part2Port,i,SiS300_Phase1[temp][index][j]);
-	  }
-       }
+     if(romptr) {
+        romptr += (temp * 2);
+	romptr = ROMAddr[romptr] | (ROMAddr[romptr + 1] << 8);
+	romptr += (index * 4);
+        for(i=0x31, j=0; i<=0x34; i++, j++) {
+	   SiS_SetReg(SiS_Pr->SiS_Part2Port,i,ROMAddr[romptr + j]);
+	}
+     } else {
+        for(i=0x31, j=0; i<=0x34; i++, j++) {
+           SiS_SetReg(SiS_Pr->SiS_Part2Port,i,SiS300_Phase1[temp][index][j]);
+	}
+     }
   }
 }
 
 static void
-SetOEMYFilter(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,USHORT BaseAddr,
-              UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex)
+SetOEMYFilter(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,
+              USHORT ModeNo,USHORT ModeIdIndex)
 {
-  USHORT index,temp,temp1,i,j,romptr=0;
+  UCHAR  *ROMAddr = HwInfo->pjVirtualRomBase;
+  USHORT index,temp,i,j,romptr=0;
 
-  if(SiS_Pr->SiS_VBInfo & (SetCRT2ToSCART | SetCRT2ToHiVisionTV)) return;
+  if(SiS_Pr->SiS_VBInfo & (SetCRT2ToSCART | SetCRT2ToHiVision | SetCRT2ToYPbPr525750)) return;
 
   if((ROMAddr) && SiS_Pr->SiS_UseROM) {
      if(!(ROMAddr[0x238] & 0x01)) return;
@@ -12409,66 +12442,84 @@
 
   temp = GetOEMTVPtr(SiS_Pr);
 
+  if(SiS_Pr->SiS_TVMode & TVSetPALM)      temp = 8;
+  else if(SiS_Pr->SiS_TVMode & TVSetPALN) temp = 9;
+  /* NTSCJ uses NTSC filters */
+
   index = SiS_Pr->SiS_VBModeIDTable[ModeIdIndex].VB_TVYFilterIndex;
 
-  if(HwDeviceExtension->jChipType > SIS_300) {
-     if(SiS_GetReg1(SiS_Pr->SiS_P3d4,0x31) & 0x01) {
-       temp1 = SiS_GetReg1(SiS_Pr->SiS_P3d4,0x35);
-       if(temp1 & (EnablePALM | EnablePALN)) {
-          temp = 8;
-	  if(!(temp1 & EnablePALM)) temp = 9;
-       }
-     }
-  }
   if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
       for(i=0x35, j=0; i<=0x38; i++, j++) {
-       	SiS_SetReg1(SiS_Pr->SiS_Part2Port,i,SiS300_Filter2[temp][index][j]);
+       	SiS_SetReg(SiS_Pr->SiS_Part2Port,i,SiS300_Filter2[temp][index][j]);
       }
       for(i=0x48; i<=0x4A; i++, j++) {
-     	SiS_SetReg1(SiS_Pr->SiS_Part2Port,i,SiS300_Filter2[temp][index][j]);
+     	SiS_SetReg(SiS_Pr->SiS_Part2Port,i,SiS300_Filter2[temp][index][j]);
       }
   } else {
-      if(romptr) {
+      if((romptr) && (!(SiS_Pr->SiS_TVMode & (TVSetPALM|TVSetPALN)))) {
          romptr += (temp * 2);
 	 romptr = ROMAddr[romptr] | (ROMAddr[romptr + 1] << 8);
 	 romptr += (index * 4);
 	 for(i=0x35, j=0; i<=0x38; i++, j++) {
-       	    SiS_SetReg1(SiS_Pr->SiS_Part2Port,i,ROMAddr[romptr + j]);
+       	    SiS_SetReg(SiS_Pr->SiS_Part2Port,i,ROMAddr[romptr + j]);
          }
       } else {
          for(i=0x35, j=0; i<=0x38; i++, j++) {
-       	    SiS_SetReg1(SiS_Pr->SiS_Part2Port,i,SiS300_Filter1[temp][index][j]);
+       	    SiS_SetReg(SiS_Pr->SiS_Part2Port,i,SiS300_Filter1[temp][index][j]);
          }
       }
   }
 }
 
+static USHORT
+SiS_SearchVBModeID(SiS_Private *SiS_Pr, USHORT *ModeNo)
+{
+   USHORT ModeIdIndex;
+   UCHAR VGAINFO = SiS_Pr->SiS_VGAINFO;
+
+   if(*ModeNo <= 5) *ModeNo |= 1;
+
+   for(ModeIdIndex=0; ; ModeIdIndex++) {
+      if(SiS_Pr->SiS_VBModeIDTable[ModeIdIndex].ModeID == *ModeNo) break;
+      if(SiS_Pr->SiS_VBModeIDTable[ModeIdIndex].ModeID == 0xFF)    return 0;
+   }
+
+   if(*ModeNo != 0x07) {
+      if(*ModeNo > 0x03) return ModeIdIndex;
+      if(VGAINFO & 0x80) return ModeIdIndex;
+      ModeIdIndex++;
+   }
+
+   if(VGAINFO & 0x10) ModeIdIndex++;   /* 400 lines */
+	                               /* else 350 lines */
+   return ModeIdIndex;
+}
+
 void
-SiS_OEM300Setting(SiS_Private *SiS_Pr, PSIS_HW_DEVICE_INFO HwDeviceExtension,
-		  USHORT BaseAddr,UCHAR *ROMAddr,USHORT ModeNo,USHORT ModeIdIndex,
-		  USHORT RefTableIndex)
+SiS_OEM300Setting(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,
+		  USHORT ModeNo, USHORT ModeIdIndex, USHORT RefTableIndex)
 {
   USHORT OEMModeIdIndex=0;
 
   if(!SiS_Pr->UseCustomMode) {
-     OEMModeIdIndex = SiS_SearchVBModeID(SiS_Pr,ROMAddr,&ModeNo);
+     OEMModeIdIndex = SiS_SearchVBModeID(SiS_Pr,&ModeNo);
      if(!(OEMModeIdIndex)) return;
   }
 
   if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
-       SetOEMLCDDelay(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,OEMModeIdIndex);
-       if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
-            SetOEMLCDData(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,OEMModeIdIndex);
-       }
+     SetOEMLCDDelay(SiS_Pr, HwInfo, ModeNo, OEMModeIdIndex);
+     if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
+        SetOEMLCDData(SiS_Pr, HwInfo, ModeNo, OEMModeIdIndex);
+     }
   }
   if(SiS_Pr->UseCustomMode) return;
   if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
-       SetOEMTVDelay(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,OEMModeIdIndex);
-       if(SiS_Pr->SiS_IF_DEF_LVDS == 0) {
-       		SetOEMAntiFlicker(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,OEMModeIdIndex);
-    		SetOEMPhaseIncr(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,OEMModeIdIndex);
-       		SetOEMYFilter(SiS_Pr,HwDeviceExtension,BaseAddr,ROMAddr,ModeNo,OEMModeIdIndex);
-       }
+     SetOEMTVDelay(SiS_Pr, HwInfo, ModeNo,OEMModeIdIndex);
+     if(SiS_Pr->SiS_VBType & VB_SISVB) {
+        SetOEMAntiFlicker(SiS_Pr, HwInfo, ModeNo, OEMModeIdIndex);
+    	SetOEMPhaseIncr(SiS_Pr, HwInfo, ModeNo, OEMModeIdIndex);
+       	SetOEMYFilter(SiS_Pr, HwInfo, ModeNo, OEMModeIdIndex);
+     }
   }
 }
 #endif

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