ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / video / sis / init.c
1 /* $XFree86$ */
2 /*
3  * Mode initializing code (CRT1 section) for
4  * for SiS 300/305/540/630/730 and
5  *     SiS 315/550/650/M650/651/661FX/M661FX/740/741/M741/330/660/M660/760/M760
6  * (Universal module for Linux kernel framebuffer and XFree86 4.x)
7  *
8  * Copyright (C) 2001-2004 by Thomas Winischhofer, Vienna, Austria
9  *
10  * If distributed as part of the Linux kernel, the following license terms
11  * apply:
12  *
13  * * This program is free software; you can redistribute it and/or modify
14  * * it under the terms of the GNU General Public License as published by
15  * * the Free Software Foundation; either version 2 of the named License,
16  * * or any later version.
17  * *
18  * * This program is distributed in the hope that it will be useful,
19  * * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * * GNU General Public License for more details.
22  * *
23  * * You should have received a copy of the GNU General Public License
24  * * along with this program; if not, write to the Free Software
25  * * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
26  *
27  * Otherwise, the following license terms apply:
28  *
29  * * Redistribution and use in source and binary forms, with or without
30  * * modification, are permitted provided that the following conditions
31  * * are met:
32  * * 1) Redistributions of source code must retain the above copyright
33  * *    notice, this list of conditions and the following disclaimer.
34  * * 2) Redistributions in binary form must reproduce the above copyright
35  * *    notice, this list of conditions and the following disclaimer in the
36  * *    documentation and/or other materials provided with the distribution.
37  * * 3) All advertising materials mentioning features or use of this software
38  * *    must display the following acknowledgement: "This product includes
39  * *    software developed by Thomas Winischhofer, Vienna, Austria."
40  * * 4) The name of the author may not be used to endorse or promote products
41  * *    derived from this software without specific prior written permission.
42  * *
43  * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
44  * * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
45  * * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
46  * * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
47  * * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
48  * * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49  * * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50  * * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51  * * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
52  * * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53  *
54  * Author:      Thomas Winischhofer <thomas@winischhofer.net>
55  *
56  * Formerly based on non-functional code-fragements for 300 series by SiS, Inc.
57  * Used by permission.
58  *
59  * TW says: This code looks awful, I know. But please don't do anything about
60  * this otherwise debugging will be hell.
61  * The code is extremely fragile as regards the different chipsets, different
62  * video bridges and combinations thereof. If anything is changed, extreme
63  * care has to be taken that that change doesn't break it for other chipsets,
64  * bridges or combinations thereof.
65  * All comments in this file are by me, regardless if they are marked TW or not.
66  *
67  */
68  
69 #include "init.h"
70
71 #ifdef SIS300
72 #include "300vtbl.h"
73 #endif
74
75 #ifdef SIS315H
76 #include "310vtbl.h"
77 #endif
78
79 #if defined(ALLOC_PRAGMA)
80 #pragma alloc_text(PAGE,SiSSetMode)
81 #pragma alloc_text(PAGE,SiSInit)
82 #endif
83
84 /*********************************************/
85 /*         POINTER INITIALIZATION            */
86 /*********************************************/
87
88 static void
89 InitCommonPointer(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
90 {
91    SiS_Pr->SiS_StResInfo     = SiS_StResInfo;
92    SiS_Pr->SiS_ModeResInfo   = SiS_ModeResInfo;
93    SiS_Pr->SiS_StandTable    = SiS_StandTable;
94
95    SiS_Pr->SiS_NTSCPhase     = SiS_NTSCPhase;
96    SiS_Pr->SiS_PALPhase      = SiS_PALPhase;
97    SiS_Pr->SiS_NTSCPhase2    = SiS_NTSCPhase2;
98    SiS_Pr->SiS_PALPhase2     = SiS_PALPhase2;
99    SiS_Pr->SiS_PALMPhase     = SiS_PALMPhase;
100    SiS_Pr->SiS_PALNPhase     = SiS_PALNPhase;
101    SiS_Pr->SiS_PALMPhase2    = SiS_PALMPhase2;
102    SiS_Pr->SiS_PALNPhase2    = SiS_PALNPhase2;
103    SiS_Pr->SiS_SpecialPhase  = SiS_SpecialPhase;
104    SiS_Pr->SiS_SpecialPhaseM = SiS_SpecialPhaseM;
105    SiS_Pr->SiS_SpecialPhaseJ = SiS_SpecialPhaseJ;
106
107    SiS_Pr->SiS_NTSCTiming     = SiS_NTSCTiming;
108    SiS_Pr->SiS_PALTiming      = SiS_PALTiming;
109    SiS_Pr->SiS_HiTVSt1Timing  = SiS_HiTVSt1Timing;
110    SiS_Pr->SiS_HiTVSt2Timing  = SiS_HiTVSt2Timing;
111
112    SiS_Pr->SiS_HiTVExtTiming  = SiS_HiTVExtTiming;
113    SiS_Pr->SiS_HiTVGroup3Data = SiS_HiTVGroup3Data;
114    SiS_Pr->SiS_HiTVGroup3Simu = SiS_HiTVGroup3Simu;
115 #if 0
116    SiS_Pr->SiS_HiTVTextTiming = SiS_HiTVTextTiming;
117    SiS_Pr->SiS_HiTVGroup3Text = SiS_HiTVGroup3Text;
118 #endif
119
120    SiS_Pr->SiS_StPALData   = SiS_StPALData;
121    SiS_Pr->SiS_ExtPALData  = SiS_ExtPALData;
122    SiS_Pr->SiS_StNTSCData  = SiS_StNTSCData;
123    SiS_Pr->SiS_ExtNTSCData = SiS_ExtNTSCData;
124    SiS_Pr->SiS_St1HiTVData = SiS_StHiTVData;
125    SiS_Pr->SiS_St2HiTVData = SiS_St2HiTVData;
126    SiS_Pr->SiS_ExtHiTVData = SiS_ExtHiTVData;
127    SiS_Pr->SiS_St525iData  = SiS_StNTSCData;
128    SiS_Pr->SiS_St525pData  = SiS_St525pData;
129    SiS_Pr->SiS_St750pData  = SiS_St750pData;
130    SiS_Pr->SiS_Ext525iData = SiS_ExtNTSCData;
131    SiS_Pr->SiS_Ext525pData = SiS_ExtNTSCData;
132    SiS_Pr->SiS_Ext750pData = SiS_Ext750pData;
133
134    SiS_Pr->pSiS_OutputSelect = &SiS_OutputSelect;
135    SiS_Pr->pSiS_SoftSetting  = &SiS_SoftSetting;
136
137    SiS_Pr->SiS_LCD1280x960Data      = SiS_LCD1280x960Data;
138    SiS_Pr->SiS_ExtLCD1400x1050Data  = SiS_ExtLCD1400x1050Data;
139    SiS_Pr->SiS_ExtLCD1600x1200Data  = SiS_ExtLCD1600x1200Data;
140    SiS_Pr->SiS_StLCD1400x1050Data   = SiS_StLCD1400x1050Data;
141    SiS_Pr->SiS_StLCD1600x1200Data   = SiS_StLCD1600x1200Data;
142    SiS_Pr->SiS_NoScaleData1400x1050 = SiS_NoScaleData1400x1050;
143    SiS_Pr->SiS_NoScaleData1600x1200 = SiS_NoScaleData1600x1200;
144    SiS_Pr->SiS_ExtLCD1280x768Data   = SiS_ExtLCD1280x768Data;
145    SiS_Pr->SiS_StLCD1280x768Data    = SiS_StLCD1280x768Data;
146    SiS_Pr->SiS_NoScaleData1280x768  = SiS_NoScaleData1280x768;
147    SiS_Pr->SiS_NoScaleData          = SiS_NoScaleData;
148
149    SiS_Pr->SiS_LVDS320x480Data_1   = SiS_LVDS320x480Data_1;
150    SiS_Pr->SiS_LVDS800x600Data_1   = SiS_LVDS800x600Data_1;
151    SiS_Pr->SiS_LVDS800x600Data_2   = SiS_LVDS800x600Data_2;
152    SiS_Pr->SiS_LVDS1024x768Data_1  = SiS_LVDS1024x768Data_1;
153    SiS_Pr->SiS_LVDS1024x768Data_2  = SiS_LVDS1024x768Data_2;
154    SiS_Pr->SiS_LVDS1280x1024Data_1 = SiS_LVDS1280x1024Data_1;
155    SiS_Pr->SiS_LVDS1280x1024Data_2 = SiS_LVDS1280x1024Data_2;
156    SiS_Pr->SiS_LVDS1400x1050Data_1 = SiS_LVDS1400x1050Data_1;
157    SiS_Pr->SiS_LVDS1400x1050Data_2 = SiS_LVDS1400x1050Data_2;
158    SiS_Pr->SiS_LVDS1600x1200Data_1 = SiS_LVDS1600x1200Data_1;
159    SiS_Pr->SiS_LVDS1600x1200Data_2 = SiS_LVDS1600x1200Data_2;
160    SiS_Pr->SiS_LVDS1280x768Data_1  = SiS_LVDS1280x768Data_1;
161    SiS_Pr->SiS_LVDS1280x768Data_2  = SiS_LVDS1280x768Data_2;
162    SiS_Pr->SiS_LVDS1024x600Data_1  = SiS_LVDS1024x600Data_1;
163    SiS_Pr->SiS_LVDS1024x600Data_2  = SiS_LVDS1024x600Data_2;
164    SiS_Pr->SiS_LVDS1152x768Data_1  = SiS_LVDS1152x768Data_1;
165    SiS_Pr->SiS_LVDS1152x768Data_2  = SiS_LVDS1152x768Data_2;
166    SiS_Pr->SiS_LVDSXXXxXXXData_1   = SiS_LVDSXXXxXXXData_1;
167    SiS_Pr->SiS_LVDS1280x960Data_1  = SiS_LVDS1280x960Data_1;
168    SiS_Pr->SiS_LVDS1280x960Data_2  = SiS_LVDS1280x960Data_2;
169    SiS_Pr->SiS_LVDS640x480Data_1   = SiS_LVDS640x480Data_1;
170    SiS_Pr->SiS_LVDS1280x960Data_1  = SiS_LVDS1280x1024Data_1;
171    SiS_Pr->SiS_LVDS1280x960Data_2  = SiS_LVDS1280x1024Data_2;
172    SiS_Pr->SiS_LVDS640x480Data_1   = SiS_LVDS640x480Data_1;
173    SiS_Pr->SiS_LVDS640x480Data_2   = SiS_LVDS640x480Data_2;
174
175    SiS_Pr->SiS_LVDSBARCO1366Data_1 = SiS_LVDSBARCO1366Data_1;
176    SiS_Pr->SiS_LVDSBARCO1366Data_2 = SiS_LVDSBARCO1366Data_2;
177    SiS_Pr->SiS_LVDSBARCO1024Data_1 = SiS_LVDSBARCO1024Data_1;
178    SiS_Pr->SiS_LVDSBARCO1024Data_2 = SiS_LVDSBARCO1024Data_2;
179    SiS_Pr->SiS_LVDS848x480Data_1   = SiS_LVDS848x480Data_1;
180    SiS_Pr->SiS_LVDS848x480Data_2   = SiS_LVDS848x480Data_2;
181
182    SiS_Pr->SiS_CHTVUNTSCData = SiS_CHTVUNTSCData;
183    SiS_Pr->SiS_CHTVONTSCData = SiS_CHTVONTSCData;
184
185    SiS_Pr->SiS_LCDA1024x768Data_1  = SiS_LCDA1024x768Data_1;
186    SiS_Pr->SiS_LCDA1024x768Data_2  = SiS_LCDA1024x768Data_2;
187    SiS_Pr->SiS_LCDA1280x1024Data_1 = SiS_LCDA1280x1024Data_1;
188    SiS_Pr->SiS_LCDA1280x1024Data_2 = SiS_LCDA1280x1024Data_2;
189    SiS_Pr->SiS_LCDA1400x1050Data_1 = SiS_LCDA1400x1050Data_1;
190    SiS_Pr->SiS_LCDA1400x1050Data_2 = SiS_LCDA1400x1050Data_2;
191    SiS_Pr->SiS_LCDA1600x1200Data_1 = SiS_LCDA1600x1200Data_1;
192    SiS_Pr->SiS_LCDA1600x1200Data_2 = SiS_LCDA1600x1200Data_2;
193
194    SiS_Pr->LVDS1024x768Des_1  = SiS_PanelType1076_1;
195    SiS_Pr->LVDS1280x1024Des_1 = SiS_PanelType1210_1;
196    SiS_Pr->LVDS1400x1050Des_1 = SiS_PanelType1296_1;
197    SiS_Pr->LVDS1600x1200Des_1 = SiS_PanelType1600_1;
198    SiS_Pr->LVDS1024x768Des_2  = SiS_PanelType1076_2;
199    SiS_Pr->LVDS1280x1024Des_2 = SiS_PanelType1210_2;
200    SiS_Pr->LVDS1400x1050Des_2 = SiS_PanelType1296_2;
201    SiS_Pr->LVDS1600x1200Des_2 = SiS_PanelType1600_2;
202
203    SiS_Pr->SiS_PanelTypeNS_1 = SiS_PanelTypeNS_1;
204    SiS_Pr->SiS_PanelTypeNS_2 = SiS_PanelTypeNS_2;
205
206    SiS_Pr->SiS_CHTVUNTSCDesData = SiS_CHTVUNTSCDesData;
207    SiS_Pr->SiS_CHTVONTSCDesData = SiS_CHTVONTSCDesData;
208    SiS_Pr->SiS_CHTVUPALDesData  = SiS_CHTVUPALDesData;
209    SiS_Pr->SiS_CHTVOPALDesData  = SiS_CHTVOPALDesData;
210
211    SiS_Pr->SiS_LVDSCRT11280x768_1    = SiS_LVDSCRT11280x768_1;
212    SiS_Pr->SiS_LVDSCRT11024x600_1    = SiS_LVDSCRT11024x600_1;
213    SiS_Pr->SiS_LVDSCRT11152x768_1    = SiS_LVDSCRT11152x768_1;
214    SiS_Pr->SiS_LVDSCRT11280x768_1_H  = SiS_LVDSCRT11280x768_1_H;
215    SiS_Pr->SiS_LVDSCRT11024x600_1_H  = SiS_LVDSCRT11024x600_1_H;
216    SiS_Pr->SiS_LVDSCRT11152x768_1_H  = SiS_LVDSCRT11152x768_1_H;
217    SiS_Pr->SiS_LVDSCRT11280x768_2    = SiS_LVDSCRT11280x768_2;
218    SiS_Pr->SiS_LVDSCRT11024x600_2    = SiS_LVDSCRT11024x600_2;
219    SiS_Pr->SiS_LVDSCRT11152x768_2    = SiS_LVDSCRT11152x768_2;
220    SiS_Pr->SiS_LVDSCRT11280x768_2_H  = SiS_LVDSCRT11280x768_2_H;
221    SiS_Pr->SiS_LVDSCRT11024x600_2_H  = SiS_LVDSCRT11024x600_2_H;
222    SiS_Pr->SiS_LVDSCRT11152x768_2_H  = SiS_LVDSCRT11152x768_2_H;
223    SiS_Pr->SiS_LVDSCRT1320x480_1     = SiS_LVDSCRT1320x480_1;
224    SiS_Pr->SiS_LVDSCRT1XXXxXXX_1     = SiS_LVDSCRT1XXXxXXX_1;
225    SiS_Pr->SiS_LVDSCRT1XXXxXXX_1_H   = SiS_LVDSCRT1XXXxXXX_1_H;
226    SiS_Pr->SiS_LVDSCRT1640x480_1     = SiS_LVDSCRT1640x480_1;
227    SiS_Pr->SiS_LVDSCRT1640x480_1_H   = SiS_LVDSCRT1640x480_1_H;
228    SiS_Pr->SiS_LVDSCRT1640x480_2     = SiS_LVDSCRT1640x480_2;
229    SiS_Pr->SiS_LVDSCRT1640x480_2_H   = SiS_LVDSCRT1640x480_2_H;
230    SiS_Pr->SiS_LVDSCRT1640x480_3     = SiS_LVDSCRT1640x480_3;
231    SiS_Pr->SiS_LVDSCRT1640x480_3_H   = SiS_LVDSCRT1640x480_3_H;
232 }
233
234 #ifdef SIS300
235 static void
236 InitTo300Pointer(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
237 {
238    InitCommonPointer(SiS_Pr, HwInfo);
239
240    SiS_StandTable[0x04].CRTC[4] = 0x2b;
241    SiS_StandTable[0x05].CRTC[4] = 0x2b;
242    SiS_StandTable[0x06].CRTC[4] = 0x54;
243    SiS_StandTable[0x06].CRTC[5] = 0x80;
244    SiS_StandTable[0x0d].CRTC[4] = 0x2b;
245    SiS_StandTable[0x0e].CRTC[4] = 0x54;
246    SiS_StandTable[0x0e].CRTC[5] = 0x80;
247    SiS_StandTable[0x11].CRTC[4] = 0x54;
248    SiS_StandTable[0x11].CRTC[5] = 0x80;
249    SiS_StandTable[0x11].CRTC[16] = 0x83;
250    SiS_StandTable[0x11].CRTC[17] = 0x85;
251    SiS_StandTable[0x12].CRTC[4] = 0x54;
252    SiS_StandTable[0x12].CRTC[5] = 0x80;
253    SiS_StandTable[0x12].CRTC[16] = 0x83;
254    SiS_StandTable[0x12].CRTC[17] = 0x85;
255    SiS_StandTable[0x13].CRTC[5] = 0xa0;
256    SiS_StandTable[0x17].CRTC[5] = 0xa0;
257    SiS_StandTable[0x1a].CRTC[4] = 0x54;
258    SiS_StandTable[0x1a].CRTC[5] = 0x80;
259    SiS_StandTable[0x1a].CRTC[16] = 0xea;
260    SiS_StandTable[0x1a].CRTC[17] = 0x8c;
261    SiS_StandTable[0x1b].CRTC[4] = 0x54;
262    SiS_StandTable[0x1b].CRTC[5] = 0x80;
263    SiS_StandTable[0x1b].CRTC[16] = 0xea;
264    SiS_StandTable[0x1b].CRTC[17] = 0x8c;
265    SiS_StandTable[0x1c].CRTC[4] = 0x54;
266    SiS_StandTable[0x1c].CRTC[5] = 0x80;
267
268    SiS_Pr->SiS_SModeIDTable  = (SiS_StStruct *)SiS300_SModeIDTable;
269    SiS_Pr->SiS_VBModeIDTable = (SiS_VBModeStruct *)SiS300_VBModeIDTable;
270    SiS_Pr->SiS_EModeIDTable  = (SiS_ExtStruct *)SiS300_EModeIDTable;
271    SiS_Pr->SiS_RefIndex      = (SiS_Ext2Struct *)SiS300_RefIndex;
272    SiS_Pr->SiS_CRT1Table     = (SiS_CRT1TableStruct *)SiS300_CRT1Table;
273    if(HwInfo->jChipType == SIS_300) {
274       SiS_Pr->SiS_MCLKData_0    = (SiS_MCLKDataStruct *)SiS300_MCLKData_300; /* 300 */
275    } else {
276       SiS_Pr->SiS_MCLKData_0    = (SiS_MCLKDataStruct *)SiS300_MCLKData_630; /* 630, 730 */
277    }
278    SiS_Pr->SiS_VCLKData      = (SiS_VCLKDataStruct *)SiS300_VCLKData;
279    SiS_Pr->SiS_VBVCLKData    = (SiS_VBVCLKDataStruct *)SiS300_VCLKData;
280    SiS_Pr->SiS_ScreenOffset  = SiS300_ScreenOffset;
281
282    SiS_Pr->SiS_SR15  = SiS300_SR15;
283
284 #ifndef LINUX_XF86
285    SiS_Pr->pSiS_SR07 = &SiS300_SR07;
286    SiS_Pr->SiS_CR40  = SiS300_CR40;
287    SiS_Pr->SiS_CR49  = SiS300_CR49;
288    SiS_Pr->pSiS_SR1F = &SiS300_SR1F;
289    SiS_Pr->pSiS_SR21 = &SiS300_SR21;
290    SiS_Pr->pSiS_SR22 = &SiS300_SR22;
291    SiS_Pr->pSiS_SR23 = &SiS300_SR23;
292    SiS_Pr->pSiS_SR24 = &SiS300_SR24;
293    SiS_Pr->SiS_SR25  = SiS300_SR25;
294    SiS_Pr->pSiS_SR31 = &SiS300_SR31;
295    SiS_Pr->pSiS_SR32 = &SiS300_SR32;
296    SiS_Pr->pSiS_SR33 = &SiS300_SR33;
297    SiS_Pr->pSiS_CRT2Data_1_2  = &SiS300_CRT2Data_1_2;
298    SiS_Pr->pSiS_CRT2Data_4_D  = &SiS300_CRT2Data_4_D;
299    SiS_Pr->pSiS_CRT2Data_4_E  = &SiS300_CRT2Data_4_E;
300    SiS_Pr->pSiS_CRT2Data_4_10 = &SiS300_CRT2Data_4_10;
301    SiS_Pr->pSiS_RGBSenseData    = &SiS300_RGBSenseData;
302    SiS_Pr->pSiS_VideoSenseData  = &SiS300_VideoSenseData;
303    SiS_Pr->pSiS_YCSenseData     = &SiS300_YCSenseData;
304    SiS_Pr->pSiS_RGBSenseData2   = &SiS300_RGBSenseData2;
305    SiS_Pr->pSiS_VideoSenseData2 = &SiS300_VideoSenseData2;
306    SiS_Pr->pSiS_YCSenseData2    = &SiS300_YCSenseData2;
307 #endif
308
309    SiS_Pr->SiS_StLCD1024x768Data    = (SiS_LCDDataStruct *)SiS300_StLCD1024x768Data;
310    SiS_Pr->SiS_ExtLCD1024x768Data   = (SiS_LCDDataStruct *)SiS300_ExtLCD1024x768Data;
311    SiS_Pr->SiS_St2LCD1024x768Data   = (SiS_LCDDataStruct *)SiS300_St2LCD1024x768Data;
312    SiS_Pr->SiS_StLCD1280x1024Data   = (SiS_LCDDataStruct *)SiS300_StLCD1280x1024Data;
313    SiS_Pr->SiS_ExtLCD1280x1024Data  = (SiS_LCDDataStruct *)SiS300_ExtLCD1280x1024Data;
314    SiS_Pr->SiS_St2LCD1280x1024Data  = (SiS_LCDDataStruct *)SiS300_St2LCD1280x1024Data;
315    SiS_Pr->SiS_NoScaleData1024x768  = (SiS_LCDDataStruct *)SiS300_NoScaleData1024x768;
316    SiS_Pr->SiS_NoScaleData1280x1024 = (SiS_LCDDataStruct *)SiS300_NoScaleData1280x1024;
317
318    SiS_Pr->SiS_PanelDelayTbl     = (SiS_PanelDelayTblStruct *)SiS300_PanelDelayTbl;
319    SiS_Pr->SiS_PanelDelayTblLVDS = (SiS_PanelDelayTblStruct *)SiS300_PanelDelayTbl;
320 #if 0
321    SiS_Pr->SiS_PanelDelayTblLVDS = (SiS_PanelDelayTblStruct *)SiS300_PanelDelayTblLVDS;
322 #endif
323
324    SiS_Pr->SiS_CHTVUPALData  = (SiS_LVDSDataStruct *)SiS300_CHTVUPALData;
325    SiS_Pr->SiS_CHTVOPALData  = (SiS_LVDSDataStruct *)SiS300_CHTVOPALData;
326    SiS_Pr->SiS_CHTVUPALMData = SiS_CHTVUNTSCData;                          /* not supported on 300 series */
327    SiS_Pr->SiS_CHTVOPALMData = SiS_CHTVONTSCData;                          /* not supported on 300 series */
328    SiS_Pr->SiS_CHTVUPALNData = (SiS_LVDSDataStruct *)SiS300_CHTVUPALData;  /* not supported on 300 series */
329    SiS_Pr->SiS_CHTVOPALNData = (SiS_LVDSDataStruct *)SiS300_CHTVOPALData;  /* not supported on 300 series */
330    SiS_Pr->SiS_CHTVSOPALData = (SiS_LVDSDataStruct *)SiS300_CHTVSOPALData;
331
332    SiS_Pr->SiS_PanelType00_1 = (SiS_LVDSDesStruct *)SiS300_PanelType00_1;
333    SiS_Pr->SiS_PanelType01_1 = (SiS_LVDSDesStruct *)SiS300_PanelType01_1;
334    SiS_Pr->SiS_PanelType02_1 = (SiS_LVDSDesStruct *)SiS300_PanelType02_1;
335    SiS_Pr->SiS_PanelType03_1 = (SiS_LVDSDesStruct *)SiS300_PanelType03_1;
336    SiS_Pr->SiS_PanelType04_1 = (SiS_LVDSDesStruct *)SiS300_PanelType04_1;
337    SiS_Pr->SiS_PanelType05_1 = (SiS_LVDSDesStruct *)SiS300_PanelType05_1;
338    SiS_Pr->SiS_PanelType06_1 = (SiS_LVDSDesStruct *)SiS300_PanelType06_1;
339    SiS_Pr->SiS_PanelType07_1 = (SiS_LVDSDesStruct *)SiS300_PanelType07_1;
340    SiS_Pr->SiS_PanelType08_1 = (SiS_LVDSDesStruct *)SiS300_PanelType08_1;
341    SiS_Pr->SiS_PanelType09_1 = (SiS_LVDSDesStruct *)SiS300_PanelType09_1;
342    SiS_Pr->SiS_PanelType0a_1 = (SiS_LVDSDesStruct *)SiS300_PanelType0a_1;
343    SiS_Pr->SiS_PanelType0b_1 = (SiS_LVDSDesStruct *)SiS300_PanelType0b_1;
344    SiS_Pr->SiS_PanelType0c_1 = (SiS_LVDSDesStruct *)SiS300_PanelType0c_1;
345    SiS_Pr->SiS_PanelType0d_1 = (SiS_LVDSDesStruct *)SiS300_PanelType0d_1;
346    SiS_Pr->SiS_PanelType0e_1 = (SiS_LVDSDesStruct *)SiS300_PanelType0e_1;
347    SiS_Pr->SiS_PanelType0f_1 = (SiS_LVDSDesStruct *)SiS300_PanelType0f_1;
348    SiS_Pr->SiS_PanelType00_2 = (SiS_LVDSDesStruct *)SiS300_PanelType00_2;
349    SiS_Pr->SiS_PanelType01_2 = (SiS_LVDSDesStruct *)SiS300_PanelType01_2;
350    SiS_Pr->SiS_PanelType02_2 = (SiS_LVDSDesStruct *)SiS300_PanelType02_2;
351    SiS_Pr->SiS_PanelType03_2 = (SiS_LVDSDesStruct *)SiS300_PanelType03_2;
352    SiS_Pr->SiS_PanelType04_2 = (SiS_LVDSDesStruct *)SiS300_PanelType04_2;
353    SiS_Pr->SiS_PanelType05_2 = (SiS_LVDSDesStruct *)SiS300_PanelType05_2;
354    SiS_Pr->SiS_PanelType06_2 = (SiS_LVDSDesStruct *)SiS300_PanelType06_2;
355    SiS_Pr->SiS_PanelType07_2 = (SiS_LVDSDesStruct *)SiS300_PanelType07_2;
356    SiS_Pr->SiS_PanelType08_2 = (SiS_LVDSDesStruct *)SiS300_PanelType08_2;
357    SiS_Pr->SiS_PanelType09_2 = (SiS_LVDSDesStruct *)SiS300_PanelType09_2;
358    SiS_Pr->SiS_PanelType0a_2 = (SiS_LVDSDesStruct *)SiS300_PanelType0a_2;
359    SiS_Pr->SiS_PanelType0b_2 = (SiS_LVDSDesStruct *)SiS300_PanelType0b_2;
360    SiS_Pr->SiS_PanelType0c_2 = (SiS_LVDSDesStruct *)SiS300_PanelType0c_2;
361    SiS_Pr->SiS_PanelType0d_2 = (SiS_LVDSDesStruct *)SiS300_PanelType0d_2;
362    SiS_Pr->SiS_PanelType0e_2 = (SiS_LVDSDesStruct *)SiS300_PanelType0e_2;
363    SiS_Pr->SiS_PanelType0f_2 = (SiS_LVDSDesStruct *)SiS300_PanelType0f_2;
364
365    if(SiS_Pr->SiS_CustomT == CUT_BARCO1366) {
366       SiS_Pr->SiS_PanelType04_1 = (SiS_LVDSDesStruct *)SiS300_PanelType04_1a;
367       SiS_Pr->SiS_PanelType04_2 = (SiS_LVDSDesStruct *)SiS300_PanelType04_2a;
368    }
369    if(SiS_Pr->SiS_CustomT == CUT_BARCO1024) {
370       SiS_Pr->SiS_PanelType04_1 = (SiS_LVDSDesStruct *)SiS300_PanelType04_1b;
371       SiS_Pr->SiS_PanelType04_2 = (SiS_LVDSDesStruct *)SiS300_PanelType04_2b;
372    }
373
374    SiS_Pr->SiS_LVDSCRT1800x600_1     = (SiS_LVDSCRT1DataStruct *)SiS300_LVDSCRT1800x600_1;
375    SiS_Pr->SiS_LVDSCRT11024x768_1    = (SiS_LVDSCRT1DataStruct *)SiS300_LVDSCRT11024x768_1;
376    SiS_Pr->SiS_LVDSCRT11280x1024_1   = (SiS_LVDSCRT1DataStruct *)SiS300_LVDSCRT11280x1024_1;
377    SiS_Pr->SiS_LVDSCRT1800x600_1_H   = (SiS_LVDSCRT1DataStruct *)SiS300_LVDSCRT1800x600_1_H;
378    SiS_Pr->SiS_LVDSCRT11024x768_1_H  = (SiS_LVDSCRT1DataStruct *)SiS300_LVDSCRT11024x768_1_H;
379    SiS_Pr->SiS_LVDSCRT11280x1024_1_H = (SiS_LVDSCRT1DataStruct *)SiS300_LVDSCRT11280x1024_1_H;
380    SiS_Pr->SiS_LVDSCRT1800x600_2     = (SiS_LVDSCRT1DataStruct *)SiS300_LVDSCRT1800x600_2;
381    SiS_Pr->SiS_LVDSCRT11024x768_2    = (SiS_LVDSCRT1DataStruct *)SiS300_LVDSCRT11024x768_2;
382    SiS_Pr->SiS_LVDSCRT11280x1024_2   = (SiS_LVDSCRT1DataStruct *)SiS300_LVDSCRT11280x1024_2;
383    SiS_Pr->SiS_LVDSCRT1800x600_2_H   = (SiS_LVDSCRT1DataStruct *)SiS300_LVDSCRT1800x600_2_H;
384    SiS_Pr->SiS_LVDSCRT11024x768_2_H  = (SiS_LVDSCRT1DataStruct *)SiS300_LVDSCRT11024x768_2_H;
385    SiS_Pr->SiS_LVDSCRT11280x1024_2_H = (SiS_LVDSCRT1DataStruct *)SiS300_LVDSCRT11280x1024_2_H;
386    SiS_Pr->SiS_CHTVCRT1UNTSC = (SiS_LVDSCRT1DataStruct *)SiS300_CHTVCRT1UNTSC;
387    SiS_Pr->SiS_CHTVCRT1ONTSC = (SiS_LVDSCRT1DataStruct *)SiS300_CHTVCRT1ONTSC;
388    SiS_Pr->SiS_CHTVCRT1UPAL  = (SiS_LVDSCRT1DataStruct *)SiS300_CHTVCRT1UPAL;
389    SiS_Pr->SiS_CHTVCRT1OPAL  = (SiS_LVDSCRT1DataStruct *)SiS300_CHTVCRT1OPAL;
390    SiS_Pr->SiS_CHTVCRT1SOPAL = (SiS_LVDSCRT1DataStruct *)SiS300_CHTVCRT1SOPAL;
391    SiS_Pr->SiS_CHTVReg_UNTSC = (SiS_CHTVRegDataStruct *)SiS300_CHTVReg_UNTSC;
392    SiS_Pr->SiS_CHTVReg_ONTSC = (SiS_CHTVRegDataStruct *)SiS300_CHTVReg_ONTSC;
393    SiS_Pr->SiS_CHTVReg_UPAL  = (SiS_CHTVRegDataStruct *)SiS300_CHTVReg_UPAL;
394    SiS_Pr->SiS_CHTVReg_OPAL  = (SiS_CHTVRegDataStruct *)SiS300_CHTVReg_OPAL;
395    SiS_Pr->SiS_CHTVReg_UPALM = (SiS_CHTVRegDataStruct *)SiS300_CHTVReg_UNTSC;  /* not supported on 300 series */
396    SiS_Pr->SiS_CHTVReg_OPALM = (SiS_CHTVRegDataStruct *)SiS300_CHTVReg_ONTSC;  /* not supported on 300 series */
397    SiS_Pr->SiS_CHTVReg_UPALN = (SiS_CHTVRegDataStruct *)SiS300_CHTVReg_UPAL;   /* not supported on 300 series */
398    SiS_Pr->SiS_CHTVReg_OPALN = (SiS_CHTVRegDataStruct *)SiS300_CHTVReg_OPAL;   /* not supported on 300 series */
399    SiS_Pr->SiS_CHTVReg_SOPAL = (SiS_CHTVRegDataStruct *)SiS300_CHTVReg_SOPAL;
400    SiS_Pr->SiS_CHTVVCLKUNTSC = SiS300_CHTVVCLKUNTSC;
401    SiS_Pr->SiS_CHTVVCLKONTSC = SiS300_CHTVVCLKONTSC;
402    SiS_Pr->SiS_CHTVVCLKUPAL  = SiS300_CHTVVCLKUPAL;
403    SiS_Pr->SiS_CHTVVCLKOPAL  = SiS300_CHTVVCLKOPAL;
404    SiS_Pr->SiS_CHTVVCLKUPALM = SiS300_CHTVVCLKUNTSC;  /* not supported on 300 series */
405    SiS_Pr->SiS_CHTVVCLKOPALM = SiS300_CHTVVCLKONTSC;  /* not supported on 300 series */
406    SiS_Pr->SiS_CHTVVCLKUPALN = SiS300_CHTVVCLKUPAL;   /* not supported on 300 series */
407    SiS_Pr->SiS_CHTVVCLKOPALN = SiS300_CHTVVCLKOPAL;   /* not supported on 300 series */
408    SiS_Pr->SiS_CHTVVCLKSOPAL = SiS300_CHTVVCLKSOPAL;
409
410    SiS_Pr->SiS_CRT2Part2_1024x768_1  = (SiS_Part2PortTblStruct *)SiS300_CRT2Part2_1024x768_1;
411    SiS_Pr->SiS_CRT2Part2_1280x1024_1 = (SiS_Part2PortTblStruct *)SiS300_CRT2Part2_1280x1024_1;
412    SiS_Pr->SiS_CRT2Part2_1400x1050_1 = (SiS_Part2PortTblStruct *)SiS300_CRT2Part2_1400x1050_1;
413    SiS_Pr->SiS_CRT2Part2_1600x1200_1 = (SiS_Part2PortTblStruct *)SiS300_CRT2Part2_1600x1200_1;
414    SiS_Pr->SiS_CRT2Part2_1024x768_2  = (SiS_Part2PortTblStruct *)SiS300_CRT2Part2_1024x768_2;
415    SiS_Pr->SiS_CRT2Part2_1280x1024_2 = (SiS_Part2PortTblStruct *)SiS300_CRT2Part2_1280x1024_2;
416    SiS_Pr->SiS_CRT2Part2_1400x1050_2 = (SiS_Part2PortTblStruct *)SiS300_CRT2Part2_1400x1050_2;
417    SiS_Pr->SiS_CRT2Part2_1600x1200_2 = (SiS_Part2PortTblStruct *)SiS300_CRT2Part2_1600x1200_2;
418    SiS_Pr->SiS_CRT2Part2_1024x768_3  = (SiS_Part2PortTblStruct *)SiS300_CRT2Part2_1024x768_3;
419    SiS_Pr->SiS_CRT2Part2_1280x1024_3 = (SiS_Part2PortTblStruct *)SiS300_CRT2Part2_1280x1024_3;
420    SiS_Pr->SiS_CRT2Part2_1400x1050_3 = (SiS_Part2PortTblStruct *)SiS300_CRT2Part2_1400x1050_3;
421    SiS_Pr->SiS_CRT2Part2_1600x1200_3 = (SiS_Part2PortTblStruct *)SiS300_CRT2Part2_1600x1200_3;
422
423    /* LCDResInfo will on 300 series be translated to 315 series definitions */
424    SiS_Pr->SiS_Panel320x480   = Panel_320x480;
425    SiS_Pr->SiS_Panel640x480   = Panel_640x480;
426    SiS_Pr->SiS_Panel800x600   = Panel_800x600;
427    SiS_Pr->SiS_Panel1024x768  = Panel_1024x768;
428    SiS_Pr->SiS_Panel1280x1024 = Panel_1280x1024;
429    SiS_Pr->SiS_Panel1280x960  = Panel_1280x960;
430    SiS_Pr->SiS_Panel1024x600  = Panel_1024x600;
431    SiS_Pr->SiS_Panel1152x768  = Panel_1152x768;
432    SiS_Pr->SiS_Panel1280x768  = Panel_1280x768;
433    SiS_Pr->SiS_Panel1600x1200 = 255;               /* Something illegal */
434    SiS_Pr->SiS_Panel1400x1050 = 255;
435    SiS_Pr->SiS_Panel640x480_2 = 255;
436    SiS_Pr->SiS_Panel640x480_3 = 255;
437    SiS_Pr->SiS_Panel1152x864  = 255;
438    SiS_Pr->SiS_PanelMax       = Panel_320x480;     /* highest value */
439    SiS_Pr->SiS_PanelMinLVDS   = Panel_800x600;     /* Lowest value LVDS */
440    SiS_Pr->SiS_PanelMin301    = Panel_1024x768;    /* lowest value 301 */
441    SiS_Pr->SiS_PanelCustom    = Panel_Custom;
442    SiS_Pr->SiS_PanelBarco1366 = Panel_Barco1366;
443 }
444 #endif
445
446 #ifdef SIS315H
447 static void
448 InitTo310Pointer(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
449 {
450    InitCommonPointer(SiS_Pr, HwInfo);
451
452    SiS_StandTable[0x04].CRTC[4] = 0x2c;
453    SiS_StandTable[0x05].CRTC[4] = 0x2c;
454    SiS_StandTable[0x06].CRTC[4] = 0x55;
455    SiS_StandTable[0x06].CRTC[5] = 0x81;
456    SiS_StandTable[0x0d].CRTC[4] = 0x2c;
457    SiS_StandTable[0x0e].CRTC[4] = 0x55;
458    SiS_StandTable[0x0e].CRTC[5] = 0x81;
459    SiS_StandTable[0x11].CRTC[4] = 0x55;
460    SiS_StandTable[0x11].CRTC[5] = 0x81;
461    SiS_StandTable[0x11].CRTC[16] = 0x82;
462    SiS_StandTable[0x11].CRTC[17] = 0x84;
463    SiS_StandTable[0x12].CRTC[4] = 0x55;
464    SiS_StandTable[0x12].CRTC[5] = 0x81;
465    SiS_StandTable[0x12].CRTC[16] = 0x82;
466    SiS_StandTable[0x12].CRTC[17] = 0x84;
467    SiS_StandTable[0x13].CRTC[5] = 0xb1;
468    SiS_StandTable[0x17].CRTC[5] = 0xb1;
469    SiS_StandTable[0x1a].CRTC[4] = 0x55;
470    SiS_StandTable[0x1a].CRTC[5] = 0x81;
471    SiS_StandTable[0x1a].CRTC[16] = 0xe9;
472    SiS_StandTable[0x1a].CRTC[17] = 0x8b;
473    SiS_StandTable[0x1b].CRTC[4] = 0x55;
474    SiS_StandTable[0x1b].CRTC[5] = 0x81;
475    SiS_StandTable[0x1b].CRTC[16] = 0xe9;
476    SiS_StandTable[0x1b].CRTC[17] = 0x8b;
477    SiS_StandTable[0x1c].CRTC[4] = 0x55;
478    SiS_StandTable[0x1c].CRTC[5] = 0x81;
479
480    SiS_Pr->SiS_SModeIDTable  = (SiS_StStruct *)SiS310_SModeIDTable;
481    SiS_Pr->SiS_EModeIDTable  = (SiS_ExtStruct *)SiS310_EModeIDTable;
482    SiS_Pr->SiS_RefIndex      = (SiS_Ext2Struct *)SiS310_RefIndex;
483    SiS_Pr->SiS_CRT1Table     = (SiS_CRT1TableStruct *)SiS310_CRT1Table;
484    if(HwInfo->jChipType >= SIS_661) {
485       SiS_Pr->SiS_MCLKData_0 = (SiS_MCLKDataStruct *)SiS310_MCLKData_0_660;  /* 661/741/760 */
486    } else if(HwInfo->jChipType == SIS_330) {
487       SiS_Pr->SiS_MCLKData_0 = (SiS_MCLKDataStruct *)SiS310_MCLKData_0_330;  /* 330 */
488    } else if(HwInfo->jChipType > SIS_315PRO) {
489       SiS_Pr->SiS_MCLKData_0 = (SiS_MCLKDataStruct *)SiS310_MCLKData_0_650;  /* 550, 650, 740 */
490    } else {
491       SiS_Pr->SiS_MCLKData_0 = (SiS_MCLKDataStruct *)SiS310_MCLKData_0_315;  /* 315 */
492    }
493    SiS_Pr->SiS_MCLKData_1    = (SiS_MCLKDataStruct *)SiS310_MCLKData_1;
494    SiS_Pr->SiS_VCLKData      = (SiS_VCLKDataStruct *)SiS310_VCLKData;
495    SiS_Pr->SiS_VBVCLKData    = (SiS_VBVCLKDataStruct *)SiS310_VBVCLKData;
496    SiS_Pr->SiS_ScreenOffset  = SiS310_ScreenOffset;
497
498    SiS_Pr->SiS_SR15  = SiS310_SR15;
499
500 #ifndef LINUX_XF86
501    SiS_Pr->pSiS_SR07 = &SiS310_SR07;
502    SiS_Pr->SiS_CR40  = SiS310_CR40;
503    SiS_Pr->SiS_CR49  = SiS310_CR49;
504    SiS_Pr->pSiS_SR1F = &SiS310_SR1F;
505    SiS_Pr->pSiS_SR21 = &SiS310_SR21;
506    SiS_Pr->pSiS_SR22 = &SiS310_SR22;
507    SiS_Pr->pSiS_SR23 = &SiS310_SR23;
508    SiS_Pr->pSiS_SR24 = &SiS310_SR24;
509    SiS_Pr->SiS_SR25  = SiS310_SR25;
510    SiS_Pr->pSiS_SR31 = &SiS310_SR31;
511    SiS_Pr->pSiS_SR32 = &SiS310_SR32;
512    SiS_Pr->pSiS_SR33 = &SiS310_SR33;
513    SiS_Pr->pSiS_CRT2Data_1_2  = &SiS310_CRT2Data_1_2;
514    SiS_Pr->pSiS_CRT2Data_4_D  = &SiS310_CRT2Data_4_D;
515    SiS_Pr->pSiS_CRT2Data_4_E  = &SiS310_CRT2Data_4_E;
516    SiS_Pr->pSiS_CRT2Data_4_10 = &SiS310_CRT2Data_4_10;
517    SiS_Pr->pSiS_RGBSenseData    = &SiS310_RGBSenseData;
518    SiS_Pr->pSiS_VideoSenseData  = &SiS310_VideoSenseData;
519    SiS_Pr->pSiS_YCSenseData     = &SiS310_YCSenseData;
520    SiS_Pr->pSiS_RGBSenseData2   = &SiS310_RGBSenseData2;
521    SiS_Pr->pSiS_VideoSenseData2 = &SiS310_VideoSenseData2;
522    SiS_Pr->pSiS_YCSenseData2    = &SiS310_YCSenseData2;
523 #endif
524
525    SiS_Pr->SiS_StLCD1024x768Data    = (SiS_LCDDataStruct *)SiS310_StLCD1024x768Data;
526    SiS_Pr->SiS_ExtLCD1024x768Data   = (SiS_LCDDataStruct *)SiS310_ExtLCD1024x768Data;
527    SiS_Pr->SiS_St2LCD1024x768Data   = (SiS_LCDDataStruct *)SiS310_St2LCD1024x768Data;
528    SiS_Pr->SiS_StLCD1280x1024Data   = (SiS_LCDDataStruct *)SiS310_StLCD1280x1024Data;
529    SiS_Pr->SiS_ExtLCD1280x1024Data  = (SiS_LCDDataStruct *)SiS310_ExtLCD1280x1024Data;
530    SiS_Pr->SiS_St2LCD1280x1024Data  = (SiS_LCDDataStruct *)SiS310_St2LCD1280x1024Data;
531    SiS_Pr->SiS_NoScaleData1024x768  = (SiS_LCDDataStruct *)SiS310_NoScaleData1024x768;
532    SiS_Pr->SiS_NoScaleData1280x1024 = (SiS_LCDDataStruct *)SiS310_NoScaleData1280x1024;
533
534    SiS_Pr->SiS_PanelDelayTbl     = (SiS_PanelDelayTblStruct *)SiS310_PanelDelayTbl;
535    SiS_Pr->SiS_PanelDelayTblLVDS = (SiS_PanelDelayTblStruct *)SiS310_PanelDelayTblLVDS;
536
537    SiS_Pr->SiS_CHTVUPALData  = (SiS_LVDSDataStruct *)SiS310_CHTVUPALData;
538    SiS_Pr->SiS_CHTVOPALData  = (SiS_LVDSDataStruct *)SiS310_CHTVOPALData;
539    SiS_Pr->SiS_CHTVUPALMData = (SiS_LVDSDataStruct *)SiS310_CHTVUPALMData;
540    SiS_Pr->SiS_CHTVOPALMData = (SiS_LVDSDataStruct *)SiS310_CHTVOPALMData;
541    SiS_Pr->SiS_CHTVUPALNData = (SiS_LVDSDataStruct *)SiS310_CHTVUPALNData;
542    SiS_Pr->SiS_CHTVOPALNData = (SiS_LVDSDataStruct *)SiS310_CHTVOPALNData;
543    SiS_Pr->SiS_CHTVSOPALData = (SiS_LVDSDataStruct *)SiS310_CHTVSOPALData;
544
545    SiS_Pr->SiS_PanelType00_1 = (SiS_LVDSDesStruct *)SiS310_PanelType00_1;
546    SiS_Pr->SiS_PanelType01_1 = (SiS_LVDSDesStruct *)SiS310_PanelType01_1;
547    SiS_Pr->SiS_PanelType02_1 = (SiS_LVDSDesStruct *)SiS310_PanelType02_1;
548    SiS_Pr->SiS_PanelType03_1 = (SiS_LVDSDesStruct *)SiS310_PanelType03_1;
549    SiS_Pr->SiS_PanelType04_1 = (SiS_LVDSDesStruct *)SiS310_PanelType04_1;
550    SiS_Pr->SiS_PanelType05_1 = (SiS_LVDSDesStruct *)SiS310_PanelType05_1;
551    SiS_Pr->SiS_PanelType06_1 = (SiS_LVDSDesStruct *)SiS310_PanelType06_1;
552    SiS_Pr->SiS_PanelType07_1 = (SiS_LVDSDesStruct *)SiS310_PanelType07_1;
553    SiS_Pr->SiS_PanelType08_1 = (SiS_LVDSDesStruct *)SiS310_PanelType08_1;
554    SiS_Pr->SiS_PanelType09_1 = (SiS_LVDSDesStruct *)SiS310_PanelType09_1;
555    SiS_Pr->SiS_PanelType0a_1 = (SiS_LVDSDesStruct *)SiS310_PanelType0a_1;
556    SiS_Pr->SiS_PanelType0b_1 = (SiS_LVDSDesStruct *)SiS310_PanelType0b_1;
557    SiS_Pr->SiS_PanelType0c_1 = (SiS_LVDSDesStruct *)SiS310_PanelType0c_1;
558    SiS_Pr->SiS_PanelType0d_1 = (SiS_LVDSDesStruct *)SiS310_PanelType0d_1;
559    SiS_Pr->SiS_PanelType0e_1 = (SiS_LVDSDesStruct *)SiS310_PanelType0e_1;
560    SiS_Pr->SiS_PanelType0f_1 = (SiS_LVDSDesStruct *)SiS310_PanelType0f_1;
561    SiS_Pr->SiS_PanelType00_2 = (SiS_LVDSDesStruct *)SiS310_PanelType00_2;
562    SiS_Pr->SiS_PanelType01_2 = (SiS_LVDSDesStruct *)SiS310_PanelType01_2;
563    SiS_Pr->SiS_PanelType02_2 = (SiS_LVDSDesStruct *)SiS310_PanelType02_2;
564    SiS_Pr->SiS_PanelType03_2 = (SiS_LVDSDesStruct *)SiS310_PanelType03_2;
565    SiS_Pr->SiS_PanelType04_2 = (SiS_LVDSDesStruct *)SiS310_PanelType04_2;
566    SiS_Pr->SiS_PanelType05_2 = (SiS_LVDSDesStruct *)SiS310_PanelType05_2;
567    SiS_Pr->SiS_PanelType06_2 = (SiS_LVDSDesStruct *)SiS310_PanelType06_2;
568    SiS_Pr->SiS_PanelType07_2 = (SiS_LVDSDesStruct *)SiS310_PanelType07_2;
569    SiS_Pr->SiS_PanelType08_2 = (SiS_LVDSDesStruct *)SiS310_PanelType08_2;
570    SiS_Pr->SiS_PanelType09_2 = (SiS_LVDSDesStruct *)SiS310_PanelType09_2;
571    SiS_Pr->SiS_PanelType0a_2 = (SiS_LVDSDesStruct *)SiS310_PanelType0a_2;
572    SiS_Pr->SiS_PanelType0b_2 = (SiS_LVDSDesStruct *)SiS310_PanelType0b_2;
573    SiS_Pr->SiS_PanelType0c_2 = (SiS_LVDSDesStruct *)SiS310_PanelType0c_2;
574    SiS_Pr->SiS_PanelType0d_2 = (SiS_LVDSDesStruct *)SiS310_PanelType0d_2;
575    SiS_Pr->SiS_PanelType0e_2 = (SiS_LVDSDesStruct *)SiS310_PanelType0e_2;
576    SiS_Pr->SiS_PanelType0f_2 = (SiS_LVDSDesStruct *)SiS310_PanelType0f_2;
577
578    SiS_Pr->SiS_CRT2Part2_1024x768_1  = (SiS_Part2PortTblStruct *)SiS310_CRT2Part2_1024x768_1;
579    SiS_Pr->SiS_CRT2Part2_1280x1024_1 = (SiS_Part2PortTblStruct *)SiS310_CRT2Part2_1280x1024_1;
580    SiS_Pr->SiS_CRT2Part2_1400x1050_1 = (SiS_Part2PortTblStruct *)SiS310_CRT2Part2_1400x1050_1;
581    SiS_Pr->SiS_CRT2Part2_1600x1200_1 = (SiS_Part2PortTblStruct *)SiS310_CRT2Part2_1600x1200_1;
582    SiS_Pr->SiS_CRT2Part2_1024x768_2  = (SiS_Part2PortTblStruct *)SiS310_CRT2Part2_1024x768_2;
583    SiS_Pr->SiS_CRT2Part2_1280x1024_2 = (SiS_Part2PortTblStruct *)SiS310_CRT2Part2_1280x1024_2;
584    SiS_Pr->SiS_CRT2Part2_1400x1050_2 = (SiS_Part2PortTblStruct *)SiS310_CRT2Part2_1400x1050_2;
585    SiS_Pr->SiS_CRT2Part2_1600x1200_2 = (SiS_Part2PortTblStruct *)SiS310_CRT2Part2_1600x1200_2;
586    SiS_Pr->SiS_CRT2Part2_1024x768_3  = (SiS_Part2PortTblStruct *)SiS310_CRT2Part2_1024x768_3;
587    SiS_Pr->SiS_CRT2Part2_1280x1024_3 = (SiS_Part2PortTblStruct *)SiS310_CRT2Part2_1280x1024_3;
588    SiS_Pr->SiS_CRT2Part2_1400x1050_3 = (SiS_Part2PortTblStruct *)SiS310_CRT2Part2_1400x1050_3;
589    SiS_Pr->SiS_CRT2Part2_1600x1200_3 = (SiS_Part2PortTblStruct *)SiS310_CRT2Part2_1600x1200_3;
590
591    SiS_Pr->SiS_LVDSCRT1800x600_1     = (SiS_LVDSCRT1DataStruct *)SiS310_LVDSCRT1800x600_1;
592    SiS_Pr->SiS_LVDSCRT11024x768_1    = (SiS_LVDSCRT1DataStruct *)SiS310_LVDSCRT11024x768_1;
593    SiS_Pr->SiS_LVDSCRT11280x1024_1   = (SiS_LVDSCRT1DataStruct *)SiS310_LVDSCRT11280x1024_1;
594    SiS_Pr->SiS_LVDSCRT11400x1050_1   = (SiS_LVDSCRT1DataStruct *)SiS310_LVDSCRT11400x1050_1;
595    SiS_Pr->SiS_LVDSCRT11600x1200_1   = (SiS_LVDSCRT1DataStruct *)SiS310_LVDSCRT11600x1200_1;
596    SiS_Pr->SiS_LVDSCRT1800x600_1_H   = (SiS_LVDSCRT1DataStruct *)SiS310_LVDSCRT1800x600_1_H;
597    SiS_Pr->SiS_LVDSCRT11024x768_1_H  = (SiS_LVDSCRT1DataStruct *)SiS310_LVDSCRT11024x768_1_H;
598    SiS_Pr->SiS_LVDSCRT11280x1024_1_H = (SiS_LVDSCRT1DataStruct *)SiS310_LVDSCRT11280x1024_1_H;
599    SiS_Pr->SiS_LVDSCRT11400x1050_1_H = (SiS_LVDSCRT1DataStruct *)SiS310_LVDSCRT11400x1050_1_H;
600    SiS_Pr->SiS_LVDSCRT11600x1200_1_H = (SiS_LVDSCRT1DataStruct *)SiS310_LVDSCRT11600x1200_1_H;
601    SiS_Pr->SiS_LVDSCRT1800x600_2     = (SiS_LVDSCRT1DataStruct *)SiS310_LVDSCRT1800x600_2;
602    SiS_Pr->SiS_LVDSCRT11024x768_2    = (SiS_LVDSCRT1DataStruct *)SiS310_LVDSCRT11024x768_2;
603    SiS_Pr->SiS_LVDSCRT11280x1024_2   = (SiS_LVDSCRT1DataStruct *)SiS310_LVDSCRT11280x1024_2;
604    SiS_Pr->SiS_LVDSCRT11400x1050_2   = (SiS_LVDSCRT1DataStruct *)SiS310_LVDSCRT11400x1050_2;
605    SiS_Pr->SiS_LVDSCRT11600x1200_2   = (SiS_LVDSCRT1DataStruct *)SiS310_LVDSCRT11600x1200_2;
606    SiS_Pr->SiS_LVDSCRT1800x600_2_H   = (SiS_LVDSCRT1DataStruct *)SiS310_LVDSCRT1800x600_2_H;
607    SiS_Pr->SiS_LVDSCRT11024x768_2_H  = (SiS_LVDSCRT1DataStruct *)SiS310_LVDSCRT11024x768_2_H;
608    SiS_Pr->SiS_LVDSCRT11280x1024_2_H = (SiS_LVDSCRT1DataStruct *)SiS310_LVDSCRT11280x1024_2_H;
609    SiS_Pr->SiS_LVDSCRT11400x1050_2_H = (SiS_LVDSCRT1DataStruct *)SiS310_LVDSCRT11400x1050_2_H;
610    SiS_Pr->SiS_LVDSCRT11600x1200_2_H = (SiS_LVDSCRT1DataStruct *)SiS310_LVDSCRT11600x1200_2_H;
611    SiS_Pr->SiS_CHTVCRT1UNTSC         = (SiS_LVDSCRT1DataStruct *)SiS310_CHTVCRT1UNTSC;
612    SiS_Pr->SiS_CHTVCRT1ONTSC         = (SiS_LVDSCRT1DataStruct *)SiS310_CHTVCRT1ONTSC;
613    SiS_Pr->SiS_CHTVCRT1UPAL          = (SiS_LVDSCRT1DataStruct *)SiS310_CHTVCRT1UPAL;
614    SiS_Pr->SiS_CHTVCRT1OPAL          = (SiS_LVDSCRT1DataStruct *)SiS310_CHTVCRT1OPAL;
615    SiS_Pr->SiS_CHTVCRT1SOPAL         = (SiS_LVDSCRT1DataStruct *)SiS310_CHTVCRT1OPAL;
616
617    SiS_Pr->SiS_CHTVReg_UNTSC = (SiS_CHTVRegDataStruct *)SiS310_CHTVReg_UNTSC;
618    SiS_Pr->SiS_CHTVReg_ONTSC = (SiS_CHTVRegDataStruct *)SiS310_CHTVReg_ONTSC;
619    SiS_Pr->SiS_CHTVReg_UPAL  = (SiS_CHTVRegDataStruct *)SiS310_CHTVReg_UPAL;
620    SiS_Pr->SiS_CHTVReg_OPAL  = (SiS_CHTVRegDataStruct *)SiS310_CHTVReg_OPAL;
621    SiS_Pr->SiS_CHTVReg_UPALM = (SiS_CHTVRegDataStruct *)SiS310_CHTVReg_UPALM;
622    SiS_Pr->SiS_CHTVReg_OPALM = (SiS_CHTVRegDataStruct *)SiS310_CHTVReg_OPALM;
623    SiS_Pr->SiS_CHTVReg_UPALN = (SiS_CHTVRegDataStruct *)SiS310_CHTVReg_UPALN;
624    SiS_Pr->SiS_CHTVReg_OPALN = (SiS_CHTVRegDataStruct *)SiS310_CHTVReg_OPALN;
625    SiS_Pr->SiS_CHTVReg_SOPAL = (SiS_CHTVRegDataStruct *)SiS310_CHTVReg_OPAL;
626
627    SiS_Pr->SiS_LCDACRT11024x768_1    = (SiS_LCDACRT1DataStruct *)SiS310_LCDACRT11024x768_1;
628    SiS_Pr->SiS_LCDACRT11280x1024_1   = (SiS_LCDACRT1DataStruct *)SiS310_LCDACRT11280x1024_1;
629    SiS_Pr->SiS_LCDACRT11400x1050_1   = (SiS_LCDACRT1DataStruct *)SiS310_LCDACRT11400x1050_1;
630    SiS_Pr->SiS_LCDACRT11600x1200_1   = (SiS_LCDACRT1DataStruct *)SiS310_LCDACRT11600x1200_1;
631    SiS_Pr->SiS_LCDACRT11024x768_1_H  = (SiS_LCDACRT1DataStruct *)SiS310_LCDACRT11024x768_1_H;
632    SiS_Pr->SiS_LCDACRT11280x1024_1_H = (SiS_LCDACRT1DataStruct *)SiS310_LCDACRT11280x1024_1_H;
633    SiS_Pr->SiS_LCDACRT11400x1050_1_H = (SiS_LCDACRT1DataStruct *)SiS310_LCDACRT11400x1050_1_H;
634    SiS_Pr->SiS_LCDACRT11600x1200_1_H = (SiS_LCDACRT1DataStruct *)SiS310_LCDACRT11600x1200_1_H;
635    SiS_Pr->SiS_LCDACRT11024x768_2    = (SiS_LCDACRT1DataStruct *)SiS310_LCDACRT11024x768_2;
636    SiS_Pr->SiS_LCDACRT11280x1024_2   = (SiS_LCDACRT1DataStruct *)SiS310_LCDACRT11280x1024_2;
637    SiS_Pr->SiS_LCDACRT11400x1050_2   = (SiS_LCDACRT1DataStruct *)SiS310_LCDACRT11400x1050_2;
638    SiS_Pr->SiS_LCDACRT11600x1200_2   = (SiS_LCDACRT1DataStruct *)SiS310_LCDACRT11600x1200_2;
639    SiS_Pr->SiS_LCDACRT11024x768_2_H  = (SiS_LCDACRT1DataStruct *)SiS310_LCDACRT11024x768_2_H;
640    SiS_Pr->SiS_LCDACRT11280x1024_2_H = (SiS_LCDACRT1DataStruct *)SiS310_LCDACRT11280x1024_2_H;
641    SiS_Pr->SiS_LCDACRT11400x1050_2_H = (SiS_LCDACRT1DataStruct *)SiS310_LCDACRT11400x1050_2_H;
642    SiS_Pr->SiS_LCDACRT11600x1200_2_H = (SiS_LCDACRT1DataStruct *)SiS310_LCDACRT11600x1200_2_H;
643
644    SiS_Pr->SiS_CHTVVCLKUNTSC = SiS310_CHTVVCLKUNTSC;
645    SiS_Pr->SiS_CHTVVCLKONTSC = SiS310_CHTVVCLKONTSC;
646    SiS_Pr->SiS_CHTVVCLKUPAL  = SiS310_CHTVVCLKUPAL;
647    SiS_Pr->SiS_CHTVVCLKOPAL  = SiS310_CHTVVCLKOPAL;
648    SiS_Pr->SiS_CHTVVCLKUPALM = SiS310_CHTVVCLKUPALM;
649    SiS_Pr->SiS_CHTVVCLKOPALM = SiS310_CHTVVCLKOPALM;
650    SiS_Pr->SiS_CHTVVCLKUPALN = SiS310_CHTVVCLKUPALN;
651    SiS_Pr->SiS_CHTVVCLKOPALN = SiS310_CHTVVCLKOPALN;   
652    SiS_Pr->SiS_CHTVVCLKSOPAL = SiS310_CHTVVCLKOPAL;
653
654    SiS_Pr->SiS_Panel320x480   = Panel_320x480;
655    SiS_Pr->SiS_Panel640x480   = Panel_640x480;
656    SiS_Pr->SiS_Panel800x600   = Panel_800x600;
657    SiS_Pr->SiS_Panel1024x768  = Panel_1024x768;
658    SiS_Pr->SiS_Panel1280x1024 = Panel_1280x1024;
659    SiS_Pr->SiS_Panel1280x960  = Panel_1280x960;
660    SiS_Pr->SiS_Panel1600x1200 = Panel_1600x1200;
661    SiS_Pr->SiS_Panel1400x1050 = Panel_1400x1050;
662    SiS_Pr->SiS_Panel1152x768  = Panel_1152x768;
663    SiS_Pr->SiS_Panel1152x864  = Panel_1152x864;
664    SiS_Pr->SiS_Panel1280x768  = Panel_1280x768;
665    SiS_Pr->SiS_Panel1024x600  = Panel_1024x600;
666    SiS_Pr->SiS_Panel640x480_2 = Panel_640x480_2;
667    SiS_Pr->SiS_Panel640x480_3 = Panel_640x480_3;
668    SiS_Pr->SiS_PanelMax       = Panel_320x480;    /* highest value */
669    SiS_Pr->SiS_PanelMinLVDS   = Panel_800x600;    /* lowest value LVDS/LCDA */
670    SiS_Pr->SiS_PanelMin301    = Panel_1024x768;   /* lowest value 301 */
671    SiS_Pr->SiS_PanelCustom    = Panel_Custom;
672    SiS_Pr->SiS_PanelBarco1366 = 255;
673 }
674 #endif
675
676 static void
677 SiSInitPtr(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
678 {
679    switch(HwInfo->jChipType) {
680 #ifdef SIS315H
681    case SIS_315H:
682    case SIS_315:
683    case SIS_315PRO:
684    case SIS_550:
685    case SIS_650:
686    case SIS_740:
687    case SIS_330:
688    case SIS_661:
689    case SIS_741:
690    case SIS_660:
691    case SIS_760:
692       InitTo310Pointer(SiS_Pr, HwInfo);
693       break;
694 #endif
695 #ifdef SIS300
696    case SIS_300:
697    case SIS_540:
698    case SIS_630:
699    case SIS_730:
700       InitTo300Pointer(SiS_Pr, HwInfo);
701       break;
702 #endif
703    default:
704       break;
705    }
706 }
707
708 /*********************************************/
709 /*            HELPER: Get ModeID             */
710 /*********************************************/
711
712 USHORT
713 SiS_GetModeID(int VGAEngine, ULONG VBFlags, int HDisplay, int VDisplay, int Depth, BOOLEAN FSTN)
714 {
715    USHORT ModeIndex = 0;
716
717    switch(HDisplay)
718    {
719      case 320:
720           if(VDisplay == 200)     ModeIndex = ModeIndex_320x200[Depth];
721           else if(VDisplay == 240) {
722              if(FSTN) ModeIndex = ModeIndex_320x240_FSTN[Depth];
723              else     ModeIndex = ModeIndex_320x240[Depth];
724           }
725           break;
726      case 400:
727           if(VDisplay == 300) ModeIndex = ModeIndex_400x300[Depth];
728           break;
729      case 512:
730           if(VDisplay == 384) ModeIndex = ModeIndex_512x384[Depth];
731           break;
732      case 640:
733           if(VDisplay == 480)      ModeIndex = ModeIndex_640x480[Depth];
734           else if(VDisplay == 400) ModeIndex = ModeIndex_640x400[Depth];
735           break;
736      case 720:
737           if(!(VBFlags & CRT1_LCDA)) {
738              if(VDisplay == 480)      ModeIndex = ModeIndex_720x480[Depth];
739              else if(VDisplay == 576) ModeIndex = ModeIndex_720x576[Depth];
740           }
741           break;
742      case 768:
743           if(!(VBFlags & CRT1_LCDA)) {
744              if(VDisplay == 576) ModeIndex = ModeIndex_768x576[Depth];
745           }
746           break;
747      case 800:
748           if(VDisplay == 600)    ModeIndex = ModeIndex_800x600[Depth];
749           else if(!(VBFlags & CRT1_LCDA)) {
750              if(VDisplay == 480) ModeIndex = ModeIndex_800x480[Depth];
751           }
752           break;
753      case 848:
754           if(!(VBFlags & CRT1_LCDA)) {
755              if(VDisplay == 480) ModeIndex = ModeIndex_848x480[Depth];
756           }
757           break;
758      case 856:
759           if(!(VBFlags & CRT1_LCDA)) {
760              if(VDisplay == 480) ModeIndex = ModeIndex_856x480[Depth];
761           }
762           break;
763      case 1024:
764           if(VDisplay == 768) ModeIndex = ModeIndex_1024x768[Depth];
765           else if(!(VBFlags & CRT1_LCDA)) {
766              if(VDisplay == 576)    ModeIndex = ModeIndex_1024x576[Depth];
767              else if(VGAEngine == SIS_300_VGA) {
768                 if(VDisplay == 600) ModeIndex = ModeIndex_1024x600[Depth];
769              }
770           }
771           break;
772      case 1152:
773           if(!(VBFlags & CRT1_LCDA)) {
774              if(VDisplay == 864)    ModeIndex = ModeIndex_1152x864[Depth];
775              else if(VGAEngine == SIS_300_VGA) {
776                 if(VDisplay == 768) ModeIndex = ModeIndex_1152x768[Depth];
777              }
778           }
779           break;
780      case 1280:
781           if(VDisplay == 1024)        ModeIndex = ModeIndex_1280x1024[Depth];
782           else if(!(VBFlags & CRT1_LCDA)) {
783              if(VDisplay == 960)      ModeIndex = ModeIndex_1280x960[Depth];
784              else if(VDisplay == 720) ModeIndex = ModeIndex_1280x720[Depth];
785              else if(VDisplay == 768) {
786                 if(VGAEngine == SIS_300_VGA) {
787                    ModeIndex = ModeIndex_300_1280x768[Depth];
788                 } else {
789                    ModeIndex = ModeIndex_310_1280x768[Depth];
790                 }
791              }
792           }
793           break;
794      case 1360:
795           if(!(VBFlags & CRT1_LCDA)) {
796              if(VDisplay == 768)     ModeIndex = ModeIndex_1360x768[Depth];
797              else if(VGAEngine == SIS_300_VGA) {
798                 if(VDisplay == 1024) ModeIndex = ModeIndex_300_1360x1024[Depth];
799              }
800           }
801           break;
802      case 1400:
803           if(VGAEngine == SIS_315_VGA) {
804              if(VDisplay == 1050) ModeIndex = ModeIndex_1400x1050[Depth];
805           }
806           break;
807      case 1600:
808           if(VDisplay == 1200) ModeIndex = ModeIndex_1600x1200[Depth];
809           break;
810      case 1920:
811           if(!(VBFlags & CRT1_LCDA)) {
812              if(VDisplay == 1440) ModeIndex = ModeIndex_1920x1440[Depth];
813           }
814           break;
815      case 2048:
816           if(!(VBFlags & CRT1_LCDA)) {
817              if(VDisplay == 1536) {
818                 if(VGAEngine == SIS_300_VGA) {
819                     ModeIndex = ModeIndex_300_2048x1536[Depth];
820                 } else {
821                     ModeIndex = ModeIndex_310_2048x1536[Depth];
822                 }
823              }
824           }
825           break;
826    }
827
828    return(ModeIndex);
829 }
830
831 USHORT
832 SiS_GetModeID_LCD(int VGAEngine, ULONG VBFlags, int HDisplay, int VDisplay,
833                   int Depth, BOOLEAN FSTN, USHORT CustomT, int LCDwidth, int LCDheight)
834 {
835    USHORT ModeIndex = 0;
836
837    if(VBFlags & (VB_LVDS | VB_30xBDH)) {
838
839       switch(HDisplay)
840       {
841         case 320:
842              if(CustomT != CUT_PANEL848) {
843                 if(VDisplay == 200) ModeIndex = ModeIndex_320x200[Depth];
844                 else if(VDisplay == 240) {
845                    if(!FSTN) ModeIndex = ModeIndex_320x240[Depth];
846                    else if(VGAEngine == SIS_315_VGA) {
847                       ModeIndex = ModeIndex_320x240_FSTN[Depth];
848                    }
849                 }
850              }
851              break;
852         case 400:
853              if(CustomT != CUT_PANEL848) {
854                 if(VDisplay == 300) ModeIndex = ModeIndex_400x300[Depth];
855              }
856              break;
857         case 512:
858              if(CustomT != CUT_PANEL848) {
859                 if(VDisplay == 384) {
860                    if(LCDwidth != 1024 || LCDheight != 600) {
861                       ModeIndex = ModeIndex_512x384[Depth];
862                    }
863                 }
864              }
865              break;
866         case 640:
867              if(VDisplay == 480)            ModeIndex = ModeIndex_640x480[Depth];
868              else if(VDisplay == 400) {
869                 if(CustomT != CUT_PANEL848) ModeIndex = ModeIndex_640x400[Depth];
870              }
871              break;
872         case 800:
873              if(VDisplay == 600) ModeIndex = ModeIndex_800x600[Depth];
874              break;
875         case 848:
876              if(CustomT == CUT_PANEL848) {
877                 if(VDisplay == 480) ModeIndex = ModeIndex_848x480[Depth];
878              }
879              break;
880         case 1024:
881              if(VDisplay == 768) ModeIndex = ModeIndex_1024x768[Depth];
882              else if(VGAEngine == SIS_300_VGA) {
883                 if((VDisplay == 600) && (LCDheight == 600)) {
884                    ModeIndex = ModeIndex_1024x600[Depth];
885                 }
886              }
887              break;
888         case 1152:
889              if(VGAEngine == SIS_300_VGA) {
890                 if((VDisplay == 768) && (LCDheight == 768)) {
891                    ModeIndex = ModeIndex_1152x768[Depth];
892                 }
893              }
894              break;
895         case 1280:
896              if(VDisplay == 1024) ModeIndex = ModeIndex_1280x1024[Depth];
897              else if(VGAEngine == SIS_315_VGA) {
898                 if((VDisplay == 768) && (LCDheight == 768)) {
899                    ModeIndex = ModeIndex_310_1280x768[Depth];
900                 }
901              }
902              break;
903         case 1360:
904              if(VGAEngine == SIS_300_VGA) {
905                 if(CustomT == CUT_BARCO1366) {
906                    if(VDisplay == 1024) ModeIndex = ModeIndex_300_1360x1024[Depth];
907                 }
908              }
909              if(CustomT == CUT_PANEL848) {
910                 if(VDisplay == 768) ModeIndex = ModeIndex_1360x768[Depth];
911              }
912              break;
913         case 1400:
914              if(VGAEngine == SIS_315_VGA) {
915                 if(VDisplay == 1050) ModeIndex = ModeIndex_1400x1050[Depth];
916              }
917              break;
918         case 1600:
919              if(VGAEngine == SIS_315_VGA) {
920                 if(VDisplay == 1200) ModeIndex = ModeIndex_1600x1200[Depth];
921              }
922              break;
923       }
924
925    } else if(VBFlags & VB_SISBRIDGE) {
926
927       switch(HDisplay)
928       {
929         case 320:
930              if(VDisplay == 200)      ModeIndex = ModeIndex_320x200[Depth];
931              else if(VDisplay == 240) ModeIndex = ModeIndex_320x240[Depth];
932              break;
933         case 400:
934              if(VDisplay == 300) ModeIndex = ModeIndex_400x300[Depth];
935              break;
936         case 512:
937              if(VDisplay == 384) ModeIndex = ModeIndex_512x384[Depth];
938              break;
939         case 640:
940              if(VDisplay == 480)      ModeIndex = ModeIndex_640x480[Depth];
941              else if(VDisplay == 400) ModeIndex = ModeIndex_640x400[Depth];
942              break;
943         case 800:
944              if(VDisplay == 600) ModeIndex = ModeIndex_800x600[Depth];
945              break;
946         case 1024:
947              if(VDisplay == 768) ModeIndex = ModeIndex_1024x768[Depth];
948              break;
949         case 1280:
950              if(VDisplay == 1024) ModeIndex = ModeIndex_1280x1024[Depth];
951              else if(VDisplay == 768) {
952                 if((LCDheight == 768) ||
953                    ((LCDheight == 1024) && (VBFlags & (VB_301|VB_301B|VB_301C|VB_302B)))) {
954                    if(VGAEngine == SIS_300_VGA) {
955                       ModeIndex = ModeIndex_300_1280x768[Depth];
956                    } else {
957                       ModeIndex = ModeIndex_310_1280x768[Depth];
958                    }
959                 }
960              } else if(VDisplay == 960) {
961                 if((LCDheight == 960) ||
962                    ((LCDheight == 1024) && (VBFlags & (VB_301|VB_301B|VB_301C|VB_302B)))) {
963                    ModeIndex = ModeIndex_1280x960[Depth];
964                 }
965              }
966              break;
967         case 1400:
968              if(VGAEngine == SIS_315_VGA) {
969                 if(VBFlags & (VB_301B | VB_301C | VB_302B | VB_302LV | VB_302ELV)) {
970                    if(LCDheight != 1200) {
971                       if(VDisplay == 1050) ModeIndex = ModeIndex_1400x1050[Depth];
972                    }
973                 }
974              }
975              break;
976         case 1600:
977              if(VBFlags & (VB_301C | VB_302B | VB_302LV | VB_302ELV)) {
978                 if(VDisplay == 1200) ModeIndex = ModeIndex_1600x1200[Depth];
979              }
980              break;
981       }
982    }
983
984    return ModeIndex;
985 }
986
987 USHORT
988 SiS_GetModeID_TV(int VGAEngine, ULONG VBFlags, int HDisplay, int VDisplay, int Depth)
989 {
990    USHORT ModeIndex = 0;
991
992    if(VBFlags & VB_CHRONTEL) {
993
994       switch(HDisplay)
995       {
996         case 512:
997              if(VGAEngine == SIS_315_VGA) {
998                 if(VDisplay == 384) ModeIndex = ModeIndex_512x384[Depth];
999              }
1000              break;
1001         case 640:
1002              if(VDisplay == 480)      ModeIndex = ModeIndex_640x480[Depth];
1003              else if(VDisplay == 400) ModeIndex = ModeIndex_640x400[Depth];
1004              break;
1005         case 800:
1006              if(VDisplay == 600) ModeIndex = ModeIndex_800x600[Depth];
1007              break;
1008         case 1024:
1009              if(VGAEngine == SIS_315_VGA) {
1010                 if(VDisplay == 768) ModeIndex = ModeIndex_1024x768[Depth];
1011              }
1012              break;
1013       }
1014
1015    } else if(VBFlags & VB_SISTVBRIDGE) {
1016
1017       switch(HDisplay)
1018       {
1019         case 320:
1020              if(VDisplay == 200)      ModeIndex = ModeIndex_320x200[Depth];
1021              else if(VDisplay == 240) ModeIndex = ModeIndex_320x240[Depth];
1022              break;
1023         case 400:
1024              if(VDisplay == 300) ModeIndex = ModeIndex_400x300[Depth];
1025              break;
1026         case 512:
1027              if( ((VBFlags & TV_YPBPR) && (VBFlags & (TV_YPBPR525P | TV_YPBPR750P | TV_YPBPR1080I))) ||
1028                  (VBFlags & TV_HIVISION)                                                             ||
1029                  ((!(VBFlags & (TV_YPBPR | TV_PALM))) && (VBFlags & TV_PAL)) ) {
1030                 if(VDisplay == 384) ModeIndex = ModeIndex_512x384[Depth];
1031              }
1032              break;
1033         case 640:
1034              if(VDisplay == 480)      ModeIndex = ModeIndex_640x480[Depth];
1035              else if(VDisplay == 400) ModeIndex = ModeIndex_640x400[Depth];
1036              break;
1037         case 720:
1038              if((!(VBFlags & TV_HIVISION)) && (!((VBFlags & TV_YPBPR) && (VBFlags & TV_YPBPR1080I)))) {
1039                 if(VDisplay == 480) {
1040                    if((VBFlags & TV_YPBPR) || (VBFlags & (TV_NTSC | TV_PALM)))
1041                       ModeIndex = ModeIndex_720x480[Depth];
1042                 } else if(VDisplay == 576) {
1043                    if((!(VBFlags & (TV_YPBPR | TV_PALM))) && (VBFlags & TV_PAL))
1044                       ModeIndex = ModeIndex_720x576[Depth];
1045                 }
1046              }
1047              break;
1048         case 768:
1049              if((!(VBFlags & TV_HIVISION)) && (!((VBFlags & TV_YPBPR) && (VBFlags & TV_YPBPR1080I)))) {
1050                 if((!(VBFlags & (TV_YPBPR | TV_PALM))) && (VBFlags & TV_PAL)) {
1051                    if(VDisplay == 576) ModeIndex = ModeIndex_768x576[Depth];
1052                 }
1053              }
1054              break;
1055         case 800:
1056              if(VDisplay == 600) ModeIndex = ModeIndex_800x600[Depth];
1057              else if(VDisplay == 480) {
1058                 if((VBFlags & TV_HIVISION) || ((VBFlags & TV_YPBPR) && (VBFlags & TV_YPBPR1080I))) {
1059                    ModeIndex = ModeIndex_800x480[Depth];
1060                 }
1061              }
1062              break;
1063         case 1024:
1064              if(VDisplay == 768) {
1065                 if(VBFlags & (VB_301B|VB_301C|VB_302B|VB_301LV|VB_302LV|VB_302ELV)) {
1066                    ModeIndex = ModeIndex_1024x768[Depth];
1067                 }
1068              } else if(VDisplay == 576) {
1069                 if((VBFlags & TV_HIVISION) || ((VBFlags & TV_YPBPR) && (VBFlags & TV_YPBPR1080I))) {
1070                    ModeIndex = ModeIndex_1024x576[Depth];
1071                 }
1072              }
1073              break;
1074         case 1280:
1075              if((VBFlags & TV_HIVISION) || ((VBFlags & TV_YPBPR) && (VBFlags & TV_YPBPR1080I))) {
1076                 if(VDisplay == 720)       ModeIndex = ModeIndex_1280x720[Depth];
1077                 else if(VDisplay == 1024) ModeIndex = ModeIndex_1280x1024[Depth];
1078              }
1079              break;
1080       }
1081    }
1082    return ModeIndex;
1083 }
1084
1085 USHORT
1086 SiS_GetModeID_VGA2(int VGAEngine, ULONG VBFlags, int HDisplay, int VDisplay, int Depth)
1087 {
1088    USHORT ModeIndex = 0;
1089
1090    if(!(VBFlags & (VB_301|VB_301B|VB_301C|VB_302B))) return 0;
1091
1092    switch(HDisplay)
1093    {
1094         case 320:
1095                 if(VDisplay == 200)      ModeIndex = ModeIndex_320x200[Depth];
1096                 else if(VDisplay == 240) ModeIndex = ModeIndex_320x240[Depth];
1097                 break;
1098         case 400:
1099                 if(VDisplay == 300) ModeIndex = ModeIndex_400x300[Depth];
1100                 break;
1101         case 512:
1102                 if(VDisplay == 384) ModeIndex = ModeIndex_512x384[Depth];
1103                 break;
1104         case 640:
1105                 if(VDisplay == 480)      ModeIndex = ModeIndex_640x480[Depth];
1106                 else if(VDisplay == 400) ModeIndex = ModeIndex_640x400[Depth];
1107                 break;
1108         case 720:
1109                 if(VDisplay == 480)      ModeIndex = ModeIndex_720x480[Depth];
1110                 else if(VDisplay == 576) ModeIndex = ModeIndex_720x576[Depth];
1111                 break;
1112         case 768:
1113                 if(VDisplay == 576) ModeIndex = ModeIndex_768x576[Depth];
1114                 break;
1115         case 800:
1116                 if(VDisplay == 600)      ModeIndex = ModeIndex_800x600[Depth];
1117                 else if(VDisplay == 480) ModeIndex = ModeIndex_800x480[Depth];
1118                 break;
1119         case 848:
1120                 if(VDisplay == 480) ModeIndex = ModeIndex_848x480[Depth];
1121                 break;
1122         case 856:
1123                 if(VDisplay == 480) ModeIndex = ModeIndex_856x480[Depth];
1124                 break;
1125         case 1024:
1126                 if(VDisplay == 768)      ModeIndex = ModeIndex_1024x768[Depth];
1127                 else if(VDisplay == 576) ModeIndex = ModeIndex_1024x576[Depth];
1128                 break;
1129         case 1152:
1130                 if(VDisplay == 864)    ModeIndex = ModeIndex_1152x864[Depth];
1131                 else if(VGAEngine == SIS_300_VGA) {
1132                    if(VDisplay == 768) ModeIndex = ModeIndex_1152x768[Depth];
1133                 }
1134                 break;
1135         case 1280:
1136                 if(VDisplay == 768) {
1137                    if(VGAEngine == SIS_300_VGA) {
1138                       ModeIndex = ModeIndex_300_1280x768[Depth];
1139                    } else {
1140                       ModeIndex = ModeIndex_310_1280x768[Depth];
1141                    }
1142                 } else if(VDisplay == 1024) ModeIndex = ModeIndex_1280x1024[Depth];
1143                 else if(VDisplay == 720)    ModeIndex = ModeIndex_1280x720[Depth];
1144                 else if(VDisplay == 960)    ModeIndex = ModeIndex_1280x960[Depth];
1145                 break;
1146         case 1360:
1147                 if(VDisplay == 768) ModeIndex = ModeIndex_1360x768[Depth];
1148                 break;
1149         case 1400:
1150                 if(VGAEngine == SIS_315_VGA) {
1151                    if(VDisplay == 1050) ModeIndex = ModeIndex_1400x1050[Depth];
1152                 }
1153                 break;
1154         case 1600:
1155                 if(VGAEngine == SIS_315_VGA) {
1156                    if(VBFlags & (VB_301B|VB_301C|VB_302B)) {
1157                       if(VDisplay == 1200) ModeIndex = ModeIndex_1600x1200[Depth];
1158                    }
1159                 }
1160                 break;
1161    }
1162
1163    return ModeIndex;
1164 }
1165
1166
1167 /*********************************************/
1168 /*          HELPER: SetReg, GetReg           */
1169 /*********************************************/
1170
1171 void
1172 SiS_SetReg(SISIOADDRESS port, USHORT index, USHORT data)
1173 {
1174    OutPortByte(port,index);
1175    OutPortByte(port + 1,data);
1176 }
1177
1178 void
1179 SiS_SetRegByte(SISIOADDRESS port, USHORT data)
1180 {
1181    OutPortByte(port,data);
1182 }
1183
1184 void
1185 SiS_SetRegShort(SISIOADDRESS port, USHORT data)
1186 {
1187    OutPortWord(port,data);
1188 }
1189
1190 void
1191 SiS_SetRegLong(SISIOADDRESS port, ULONG data)
1192 {
1193    OutPortLong(port,data);
1194 }
1195
1196 UCHAR
1197 SiS_GetReg(SISIOADDRESS port, USHORT index)
1198 {
1199    OutPortByte(port,index);
1200    return(InPortByte(port + 1));
1201 }
1202
1203 UCHAR
1204 SiS_GetRegByte(SISIOADDRESS port)
1205 {
1206    return(InPortByte(port));
1207 }
1208
1209 USHORT
1210 SiS_GetRegShort(SISIOADDRESS port)
1211 {
1212    return(InPortWord(port));
1213 }
1214
1215 ULONG
1216 SiS_GetRegLong(SISIOADDRESS port)
1217 {
1218    return(InPortLong(port));
1219 }
1220
1221 void
1222 SiS_SetRegANDOR(SISIOADDRESS Port,USHORT Index,USHORT DataAND,USHORT DataOR)
1223 {
1224   USHORT temp;
1225
1226   temp = SiS_GetReg(Port,Index);
1227   temp = (temp & (DataAND)) | DataOR;
1228   SiS_SetReg(Port,Index,temp);
1229 }
1230
1231 void
1232 SiS_SetRegAND(SISIOADDRESS Port,USHORT Index,USHORT DataAND)
1233 {
1234   USHORT temp;
1235
1236   temp = SiS_GetReg(Port,Index);
1237   temp &= DataAND;
1238   SiS_SetReg(Port,Index,temp);
1239 }
1240
1241 void
1242 SiS_SetRegOR(SISIOADDRESS Port,USHORT Index,USHORT DataOR)
1243 {
1244   USHORT temp;
1245
1246   temp = SiS_GetReg(Port,Index);
1247   temp |= DataOR;
1248   SiS_SetReg(Port,Index,temp);
1249 }
1250
1251 /*********************************************/
1252 /*      HELPER: DisplayOn, DisplayOff        */
1253 /*********************************************/
1254
1255 void
1256 SiS_DisplayOn(SiS_Private *SiS_Pr)
1257 {
1258    SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x01,0xDF,0x00);
1259 }
1260
1261 void
1262 SiS_DisplayOff(SiS_Private *SiS_Pr)
1263 {
1264    SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x01,0xDF,0x20);
1265 }
1266
1267
1268 /*********************************************/
1269 /*        HELPER: Init Port Addresses        */
1270 /*********************************************/
1271
1272 void
1273 SiSRegInit(SiS_Private *SiS_Pr, SISIOADDRESS BaseAddr)
1274 {
1275    SiS_Pr->SiS_P3c4 = BaseAddr + 0x14;
1276    SiS_Pr->SiS_P3d4 = BaseAddr + 0x24;
1277    SiS_Pr->SiS_P3c0 = BaseAddr + 0x10;
1278    SiS_Pr->SiS_P3ce = BaseAddr + 0x1e;
1279    SiS_Pr->SiS_P3c2 = BaseAddr + 0x12;
1280    SiS_Pr->SiS_P3ca = BaseAddr + 0x1a;
1281    SiS_Pr->SiS_P3c6 = BaseAddr + 0x16;
1282    SiS_Pr->SiS_P3c7 = BaseAddr + 0x17;
1283    SiS_Pr->SiS_P3c8 = BaseAddr + 0x18;
1284    SiS_Pr->SiS_P3c9 = BaseAddr + 0x19;
1285    SiS_Pr->SiS_P3cb = BaseAddr + 0x1b;
1286    SiS_Pr->SiS_P3cd = BaseAddr + 0x1d;
1287    SiS_Pr->SiS_P3da = BaseAddr + 0x2a;
1288    SiS_Pr->SiS_Part1Port = BaseAddr + SIS_CRT2_PORT_04;     /* Digital video interface registers (LCD) */
1289    SiS_Pr->SiS_Part2Port = BaseAddr + SIS_CRT2_PORT_10;     /* 301 TV Encoder registers */
1290    SiS_Pr->SiS_Part3Port = BaseAddr + SIS_CRT2_PORT_12;     /* 301 Macrovision registers */
1291    SiS_Pr->SiS_Part4Port = BaseAddr + SIS_CRT2_PORT_14;     /* 301 VGA2 (and LCD) registers */
1292    SiS_Pr->SiS_Part5Port = BaseAddr + SIS_CRT2_PORT_14 + 2; /* 301 palette address port registers */
1293    SiS_Pr->SiS_DDC_Port = BaseAddr + 0x14;                  /* DDC Port ( = P3C4, SR11/0A) */
1294    SiS_Pr->SiS_VidCapt = BaseAddr + SIS_VIDEO_CAPTURE;
1295    SiS_Pr->SiS_VidPlay = BaseAddr + SIS_VIDEO_PLAYBACK;
1296 }
1297
1298 /*********************************************/
1299 /*             HELPER: GetSysFlags           */
1300 /*********************************************/
1301
1302 static void
1303 SiS_GetSysFlags(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
1304 {
1305    unsigned char cr5f, temp1, temp2;
1306
1307    /* 661 and newer: NEVER write non-zero to SR11[7:4] */
1308    /* (SR11 is used for DDC and in enable/disablebridge) */
1309    SiS_Pr->SiS_SensibleSR11 = FALSE;
1310    SiS_Pr->SiS_MyCR63 = 0x63;
1311    if(HwInfo->jChipType >= SIS_661) {
1312       SiS_Pr->SiS_SensibleSR11 = TRUE;
1313       SiS_Pr->SiS_MyCR63 = 0x53;
1314    }
1315
1316    /* You should use the macros, not these flags directly */
1317
1318    SiS_Pr->SiS_SysFlags = 0;
1319    if(HwInfo->jChipType == SIS_650) {
1320       cr5f = SiS_GetReg(SiS_Pr->SiS_P3d4,0x5f) & 0xf0;
1321       SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x5c,0x07);
1322       temp1 = SiS_GetReg(SiS_Pr->SiS_P3d4,0x5c) & 0xf8;
1323       SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x5c,0xf8);
1324       temp2 = SiS_GetReg(SiS_Pr->SiS_P3d4,0x5c) & 0xf8;
1325       if((!temp1) || (temp2)) {
1326          switch(cr5f) {
1327             case 0x80:
1328             case 0x90:
1329             case 0xc0:
1330                SiS_Pr->SiS_SysFlags |= SF_IsM650;  break;
1331             case 0xa0:
1332             case 0xb0:
1333             case 0xe0:
1334                SiS_Pr->SiS_SysFlags |= SF_Is651;   break;
1335          }
1336       } else {
1337          switch(cr5f) {
1338             case 0x90:
1339                temp1 = SiS_GetReg(SiS_Pr->SiS_P3d4,0x5c) & 0xf8;
1340                switch(temp1) {
1341                   case 0x00: SiS_Pr->SiS_SysFlags |= SF_IsM652; break;
1342                   case 0x40: SiS_Pr->SiS_SysFlags |= SF_IsM653; break;
1343                   default:   SiS_Pr->SiS_SysFlags |= SF_IsM650; break;
1344                }
1345                break;
1346             case 0xb0:
1347                SiS_Pr->SiS_SysFlags |= SF_Is652;  break;
1348             default:
1349                SiS_Pr->SiS_SysFlags |= SF_IsM650; break;
1350          }
1351       }
1352    }
1353 }
1354
1355 /*********************************************/
1356 /*         HELPER: Init PCI & Engines        */
1357 /*********************************************/
1358
1359 static void
1360 SiSInitPCIetc(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
1361 {
1362    switch(HwInfo->jChipType) {
1363    case SIS_300:
1364    case SIS_540:
1365    case SIS_630:
1366    case SIS_730:
1367       /* Set - PCI LINEAR ADDRESSING ENABLE (0x80)
1368        *     - RELOCATED VGA IO  (0x20)
1369        *     - MMIO ENABLE (0x1)
1370        */
1371       SiS_SetReg(SiS_Pr->SiS_P3c4,0x20,0xa1);
1372       /*  - Enable 2D (0x40)
1373        *  - Enable 3D (0x02)
1374        *  - Enable 3D Vertex command fetch (0x10) ?
1375        *  - Enable 3D command parser (0x08) ?
1376        */
1377       SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x1E,0x5A);
1378       break;
1379    case SIS_315H:
1380    case SIS_315:
1381    case SIS_315PRO:
1382    case SIS_650:
1383    case SIS_740:
1384    case SIS_330:
1385    case SIS_661:
1386    case SIS_741:
1387    case SIS_660:
1388    case SIS_760:
1389       SiS_SetReg(SiS_Pr->SiS_P3c4,0x20,0xa1);
1390       /*  - Enable 2D (0x40)
1391        *  - Enable 3D (0x02)
1392        *  - Enable 3D vertex command fetch (0x10)
1393        *  - Enable 3D command parser (0x08)
1394        *  - Enable 3D G/L transformation engine (0x80)
1395        */
1396       SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x1E,0xDA);
1397       break;
1398    case SIS_550:
1399       SiS_SetReg(SiS_Pr->SiS_P3c4,0x20,0xa1);
1400       /* No 3D engine ! */
1401       /*  - Enable 2D (0x40)
1402        */
1403       SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x1E,0x40);
1404    }
1405 }
1406
1407 /*********************************************/
1408 /*             HELPER: SetLVDSetc            */
1409 /*********************************************/
1410
1411 void
1412 SiSSetLVDSetc(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
1413 {
1414    ULONG   temp;
1415
1416    SiS_Pr->SiS_IF_DEF_LVDS = 0;
1417    SiS_Pr->SiS_IF_DEF_TRUMPION = 0;
1418    SiS_Pr->SiS_IF_DEF_CH70xx = 0;
1419    SiS_Pr->SiS_IF_DEF_DSTN = 0;
1420    SiS_Pr->SiS_IF_DEF_FSTN = 0;
1421    SiS_Pr->SiS_IF_DEF_CONEX = 0;
1422
1423    SiS_Pr->SiS_ChrontelInit = 0;
1424
1425    switch(HwInfo->jChipType) {
1426 #ifdef SIS300
1427    case SIS_540:
1428    case SIS_630:
1429    case SIS_730:
1430         /* Check for SiS30x first */
1431         temp = SiS_GetReg(SiS_Pr->SiS_Part4Port,0x00);
1432         if((temp == 1) || (temp == 2)) return;
1433         temp = SiS_GetReg(SiS_Pr->SiS_P3d4,0x37);
1434         temp = (temp & 0x0E) >> 1;
1435         if((temp >= 2) && (temp <= 5)) SiS_Pr->SiS_IF_DEF_LVDS = 1;
1436         if(temp == 3)   SiS_Pr->SiS_IF_DEF_TRUMPION = 1;
1437         if((temp == 4) || (temp == 5)) {
1438                 /* Save power status (and error check) - UNUSED */
1439                 SiS_Pr->SiS_Backup70xx = SiS_GetCH700x(SiS_Pr, 0x0e);
1440                 SiS_Pr->SiS_IF_DEF_CH70xx = 1;
1441         }
1442         break;
1443 #endif
1444 #ifdef SIS315H
1445    case SIS_550:
1446    case SIS_650:
1447    case SIS_740:
1448    case SIS_330:
1449         temp = SiS_GetReg(SiS_Pr->SiS_P3d4,0x37);
1450         temp = (temp & 0x0E) >> 1;
1451         if((temp >= 2) && (temp <= 3)) SiS_Pr->SiS_IF_DEF_LVDS = 1;
1452         if(temp == 3)  SiS_Pr->SiS_IF_DEF_CH70xx = 2;
1453         break;
1454    case SIS_661:
1455    case SIS_741:
1456    case SIS_660:
1457    case SIS_760:
1458         temp = SiS_GetReg(SiS_Pr->SiS_P3d4,0x38);
1459         temp = (temp & 0xe0) >> 5;
1460         if((temp >= 2) && (temp <= 3)) SiS_Pr->SiS_IF_DEF_LVDS = 1;
1461         if(temp == 3)  SiS_Pr->SiS_IF_DEF_CH70xx = 2;
1462         if(temp == 4)  SiS_Pr->SiS_IF_DEF_CONEX = 1;  /* Not yet supported */
1463         break;
1464 #endif
1465    default:
1466         break;
1467    }
1468 }
1469
1470 /*********************************************/
1471 /*          HELPER: Enable DSTN/FSTN         */
1472 /*********************************************/
1473
1474 void
1475 SiS_SetEnableDstn(SiS_Private *SiS_Pr, int enable)
1476 {
1477    SiS_Pr->SiS_IF_DEF_DSTN = enable ? 1 : 0;
1478 }
1479
1480 void
1481 SiS_SetEnableFstn(SiS_Private *SiS_Pr, int enable)
1482 {
1483    SiS_Pr->SiS_IF_DEF_FSTN = enable ? 1 : 0;
1484 }
1485
1486 /*********************************************/
1487 /*        HELPER: Determine ROM usage        */
1488 /*********************************************/
1489
1490 static void
1491 SiSDetermineROMUsage(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
1492 {
1493    UCHAR  *ROMAddr  = HwInfo->pjVirtualRomBase;
1494
1495    if((ROMAddr) && (HwInfo->UseROM)) {
1496       if((ROMAddr[0x00] != 0x55) || (ROMAddr[0x01] != 0xAA)) {
1497          SiS_Pr->SiS_UseROM = FALSE;
1498       } else if(HwInfo->jChipType == SIS_300) {
1499         /* 300: We check if the code starts below 0x220 by
1500          * checking the jmp instruction at the beginning
1501          * of the BIOS image.
1502          */
1503          if((ROMAddr[3] == 0xe9) &&
1504             ((ROMAddr[5] << 8) | ROMAddr[4]) > 0x21a)
1505             SiS_Pr->SiS_UseROM = TRUE;
1506          else
1507             SiS_Pr->SiS_UseROM = FALSE;
1508       } else if(HwInfo->jChipType < SIS_315H) {
1509 #if 0
1510         /* Rest of 300 series: We don't use the ROM image if
1511          * the BIOS version < 2.0.0 as such old BIOSes don't
1512          * have the needed data at the expected locations.
1513          */
1514          if(ROMAddr[0x06] < '2')  SiS_Pr->SiS_UseROM = FALSE;
1515          else                     SiS_Pr->SiS_UseROM = TRUE;
1516 #else
1517         /* Sony's VAIO BIOS 1.09 follows the standard, so perhaps
1518          * the others do as well
1519          */
1520          SiS_Pr->SiS_UseROM = TRUE;
1521 #endif
1522       } else {
1523          /* 315/330 series stick to the standard */
1524          SiS_Pr->SiS_UseROM = TRUE;
1525       }
1526    } else SiS_Pr->SiS_UseROM = FALSE;
1527
1528 }
1529
1530 /*********************************************/
1531 /*        HELPER: SET SEGMENT REGISTERS      */
1532 /*********************************************/
1533
1534 static void
1535 SiS_SetSegRegLower(SiS_Private *SiS_Pr, USHORT value)
1536 {
1537    USHORT temp;
1538
1539    value &= 0x00ff;
1540    temp = SiS_GetRegByte(SiS_Pr->SiS_P3cb) & 0xf0;
1541    temp |= (value >> 4);
1542    SiS_SetRegByte(SiS_Pr->SiS_P3cb, temp);
1543    temp = SiS_GetRegByte(SiS_Pr->SiS_P3cd) & 0xf0;
1544    temp |= (value & 0x0f);
1545    SiS_SetRegByte(SiS_Pr->SiS_P3cd, temp);
1546 }
1547
1548 static void
1549 SiS_SetSegRegUpper(SiS_Private *SiS_Pr, USHORT value)
1550 {
1551    USHORT temp;
1552
1553    value &= 0x00ff;
1554    temp = SiS_GetRegByte(SiS_Pr->SiS_P3cb) & 0x0f;
1555    temp |= (value & 0xf0);
1556    SiS_SetRegByte(SiS_Pr->SiS_P3cb, temp);
1557    temp = SiS_GetRegByte(SiS_Pr->SiS_P3cd) & 0x0f;
1558    temp |= (value << 4);
1559    SiS_SetRegByte(SiS_Pr->SiS_P3cd, temp);
1560 }
1561
1562 static void
1563 SiS_SetSegmentReg(SiS_Private *SiS_Pr, USHORT value)
1564 {
1565    SiS_SetSegRegLower(SiS_Pr, value);
1566    SiS_SetSegRegUpper(SiS_Pr, value);
1567 }
1568
1569 static void
1570 SiS_ResetSegmentReg(SiS_Private *SiS_Pr)
1571 {
1572    SiS_SetSegmentReg(SiS_Pr, 0);
1573 }
1574
1575 static void
1576 SiS_SetSegmentRegOver(SiS_Private *SiS_Pr, USHORT value)
1577 {
1578    USHORT temp = value >> 8;
1579
1580    temp &= 0x07;
1581    temp |= (temp << 4);
1582    SiS_SetReg(SiS_Pr->SiS_P3c4,0x1d,temp);
1583    SiS_SetSegmentReg(SiS_Pr, value);
1584 }
1585
1586 static void
1587 SiS_ResetSegmentRegOver(SiS_Private *SiS_Pr)
1588 {
1589    SiS_SetSegmentRegOver(SiS_Pr, 0);
1590 }
1591
1592 static void
1593 SiS_ResetSegmentRegisters(SiS_Private *SiS_Pr,PSIS_HW_INFO HwInfo)
1594 {
1595    if((IS_SIS65x) || (HwInfo->jChipType >= SIS_661)) {
1596       SiS_ResetSegmentReg(SiS_Pr);
1597       SiS_ResetSegmentRegOver(SiS_Pr);
1598    }
1599 }
1600
1601 /*********************************************/
1602 /*             HELPER: GetVBType             */
1603 /*********************************************/
1604
1605 void
1606 SiS_GetVBType(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
1607 {
1608   USHORT flag=0, rev=0, nolcd=0;
1609
1610   SiS_Pr->SiS_VBType = 0;
1611
1612   if(SiS_Pr->SiS_IF_DEF_LVDS == 1) return;
1613
1614   flag = SiS_GetReg(SiS_Pr->SiS_Part4Port,0x00);
1615
1616   if(flag > 3) return;
1617
1618   rev = SiS_GetReg(SiS_Pr->SiS_Part4Port,0x01);
1619
1620   if(flag >= 2) {
1621      SiS_Pr->SiS_VBType = VB_SIS302B;
1622   } else if(flag == 1) {
1623      SiS_Pr->SiS_VBType = VB_SIS301;
1624      if(rev >= 0xC0) {
1625         SiS_Pr->SiS_VBType = VB_SIS301C;
1626      } else if(rev >= 0xB0) {
1627         SiS_Pr->SiS_VBType = VB_SIS301B;
1628         /* Check if 30xB DH version (no LCD support, use Panel Link instead) */
1629         nolcd = SiS_GetReg(SiS_Pr->SiS_Part4Port,0x23);
1630         if(!(nolcd & 0x02)) SiS_Pr->SiS_VBType |= VB_NoLCD;
1631      }
1632   }
1633   if(SiS_Pr->SiS_VBType & (VB_SIS301B | VB_SIS301C | VB_SIS302B)) {
1634      if(rev >= 0xD0) {
1635         SiS_Pr->SiS_VBType &= ~(VB_SIS301B | VB_SIS301C | VB_SIS302B | VB_NoLCD);
1636         if(rev >= 0xE0) {
1637            flag = SiS_GetReg(SiS_Pr->SiS_Part4Port,0x39);
1638            if(flag == 0xff)
1639               SiS_Pr->SiS_VBType |= VB_SIS302LV;
1640            else
1641               SiS_Pr->SiS_VBType |= VB_SIS302ELV;
1642         } else {
1643            SiS_Pr->SiS_VBType |= VB_SIS301LV;
1644         }
1645      }
1646   }
1647 }
1648
1649 /*********************************************/
1650 /*            HELPER: GetDRAMSize            */
1651 /*********************************************/
1652
1653 #ifndef LINUX_XF86
1654 static ULONG
1655 GetDRAMSize(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
1656 {
1657   ULONG   AdapterMemorySize = 0;
1658 #ifdef SIS315H
1659   USHORT  counter;
1660 #endif
1661
1662   switch(HwInfo->jChipType) {
1663 #ifdef SIS315H
1664   case SIS_315H:
1665   case SIS_315:
1666   case SIS_315PRO:
1667         counter = SiS_GetReg(SiS_Pr->SiS_P3c4,0x14);
1668         AdapterMemorySize = 1 << ((counter & 0xF0) >> 4);
1669         counter >>= 2;
1670         counter &= 0x03;
1671         if(counter == 0x02) {
1672                 AdapterMemorySize += (AdapterMemorySize / 2);      /* DDR asymetric */
1673         } else if(counter != 0) {
1674                 AdapterMemorySize <<= 1;                           /* SINGLE_CHANNEL_2_RANK or DUAL_CHANNEL_1_RANK */
1675         }
1676         AdapterMemorySize *= (1024*1024);
1677         break;
1678
1679   case SIS_330:
1680         counter = SiS_GetReg(SiS_Pr->SiS_P3c4,0x14);
1681         AdapterMemorySize = 1 << ((counter & 0xF0) >> 4);
1682         counter &= 0x0c;
1683         if(counter != 0) {
1684                 AdapterMemorySize <<= 1;
1685         }
1686         AdapterMemorySize *= (1024*1024);
1687         break;
1688
1689   case SIS_550:
1690   case SIS_650:
1691   case SIS_740:
1692         counter = SiS_GetReg(SiS_Pr->SiS_P3c4,0x14) & 0x3F;
1693         counter++;
1694         AdapterMemorySize = counter * 4;
1695         AdapterMemorySize *= (1024*1024);
1696         break;
1697
1698   case SIS_661:
1699   case SIS_741:
1700   case SIS_660:
1701   case SIS_760:
1702         counter = (SiS_GetReg(SiS_Pr->SiS_P3c4,0x79) & 0xf0) >> 4;
1703         AdapterMemorySize = 1 << counter;
1704         AdapterMemorySize *= (1024*1024);
1705         break;
1706 #endif
1707
1708 #ifdef SIS300
1709   case SIS_300:
1710   case SIS_540:
1711   case SIS_630:
1712   case SIS_730:
1713         AdapterMemorySize = SiS_GetReg(SiS_Pr->SiS_P3c4,0x14) & 0x3F;
1714         AdapterMemorySize++;
1715         AdapterMemorySize *= (1024*1024);
1716         break;
1717 #endif
1718   default:
1719         break;
1720   }
1721
1722   return AdapterMemorySize;
1723 }
1724 #endif
1725
1726 /*********************************************/
1727 /*           HELPER: Check RAM size          */
1728 /*********************************************/
1729
1730 #ifndef LINUX_XF86
1731 static BOOLEAN
1732 SiS_CheckMemorySize(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,
1733                     USHORT ModeNo, USHORT ModeIdIndex)
1734 {
1735   USHORT memorysize,modeflag;
1736   ULONG  temp;
1737
1738   if(SiS_Pr->UseCustomMode) {
1739      modeflag = SiS_Pr->CModeFlag;
1740   } else {
1741      if(ModeNo <= 0x13) {
1742         modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
1743      } else {
1744         modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
1745      }
1746   }
1747
1748   memorysize = modeflag & MemoryInfoFlag;
1749   memorysize >>= MemorySizeShift;               /* Get required memory size */
1750   memorysize++;
1751
1752   temp = GetDRAMSize(SiS_Pr, HwInfo);           /* Get adapter memory size (in MB) */
1753   temp /= (1024*1024);
1754
1755   if(temp < memorysize) return(FALSE);
1756   else return(TRUE);
1757 }
1758 #endif
1759
1760 /*********************************************/
1761 /*           HELPER: Get DRAM type           */
1762 /*********************************************/
1763
1764 #ifdef SIS315H
1765 static UCHAR
1766 SiS_Get310DRAMType(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
1767 {
1768    UCHAR data, temp;
1769
1770    if(*SiS_Pr->pSiS_SoftSetting & SoftDRAMType) {
1771      data = *SiS_Pr->pSiS_SoftSetting & 0x03;
1772    } else {
1773      if(HwInfo->jChipType >= SIS_661) {
1774         data = SiS_GetReg(SiS_Pr->SiS_P3d4,0x78) & 0x07;
1775      } else if(IS_SIS550650740) {
1776         data = SiS_GetReg(SiS_Pr->SiS_P3c4,0x13) & 0x07;
1777      } else {   /* 315, 330 */
1778         data = SiS_GetReg(SiS_Pr->SiS_P3c4,0x3a) & 0x03;
1779         if(HwInfo->jChipType == SIS_330) {
1780            if(data > 1) {
1781               temp = SiS_GetReg(SiS_Pr->SiS_P3d4,0x5f) & 0x30;
1782               switch(temp) {
1783               case 0x00: data = 1; break;
1784               case 0x10: data = 3; break;
1785               case 0x20: data = 3; break;
1786               case 0x30: data = 2; break;
1787               }
1788            } else {
1789               data = 0;
1790            }
1791         }
1792      }
1793    }
1794
1795    return data;
1796 }
1797
1798 USHORT
1799 SiS_GetMCLK(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
1800 {
1801   USHORT index;
1802
1803   index = SiS_Get310DRAMType(SiS_Pr, HwInfo);
1804   if(HwInfo->jChipType >= SIS_661) {
1805      return(SiS_Pr->SiS_MCLKData_0[index].CLOCK);
1806   } else if(index >= 4) {
1807      index -= 4;
1808      return(SiS_Pr->SiS_MCLKData_1[index].CLOCK);
1809   } else {
1810      return(SiS_Pr->SiS_MCLKData_0[index].CLOCK);
1811   }
1812 }
1813 #endif
1814
1815 /*********************************************/
1816 /*           HELPER: ClearBuffer             */
1817 /*********************************************/
1818
1819 #ifndef LINUX_XF86
1820 static void
1821 SiS_ClearBuffer(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo, USHORT ModeNo)
1822 {
1823   UCHAR   *VideoMemoryAddress = HwInfo->pjVideoMemoryAddress;
1824   ULONG   AdapterMemorySize  = (ULONG)HwInfo->ulVideoMemorySize;
1825   USHORT  *pBuffer;
1826   int i;
1827
1828   if(SiS_Pr->SiS_ModeType >= ModeEGA) {
1829      if(ModeNo > 0x13) {
1830         AdapterMemorySize = GetDRAMSize(SiS_Pr, HwInfo);
1831         SiS_SetMemory(VideoMemoryAddress,AdapterMemorySize,0);
1832      } else {
1833         pBuffer = (USHORT *)VideoMemoryAddress;
1834         for(i=0; i<0x4000; i++)
1835            pBuffer[i] = 0x0000;
1836      }
1837   } else {
1838      pBuffer = (USHORT *)VideoMemoryAddress;
1839      if(SiS_Pr->SiS_ModeType < ModeCGA) {
1840         for(i=0; i<0x4000; i++)
1841            pBuffer[i] = 0x0720;
1842      } else {
1843         SiS_SetMemory(VideoMemoryAddress,0x8000,0);
1844      }
1845   }
1846 }
1847 #endif
1848
1849 /*********************************************/
1850 /*           HELPER: SearchModeID            */
1851 /*********************************************/
1852
1853 BOOLEAN
1854 SiS_SearchModeID(SiS_Private *SiS_Pr, USHORT *ModeNo, USHORT *ModeIdIndex)
1855 {
1856    UCHAR VGAINFO = SiS_Pr->SiS_VGAINFO;
1857
1858    if(*ModeNo <= 0x13) {
1859
1860       if((*ModeNo) <= 0x05) (*ModeNo) |= 0x01;
1861
1862       for(*ModeIdIndex = 0; ;(*ModeIdIndex)++) {
1863          if(SiS_Pr->SiS_SModeIDTable[*ModeIdIndex].St_ModeID == (*ModeNo)) break;
1864          if(SiS_Pr->SiS_SModeIDTable[*ModeIdIndex].St_ModeID == 0xFF)   return FALSE;
1865       }
1866
1867       if(*ModeNo == 0x07) {
1868           if(VGAINFO & 0x10) (*ModeIdIndex)++;   /* 400 lines */
1869           /* else 350 lines */
1870       }
1871       if(*ModeNo <= 0x03) {
1872          if(!(VGAINFO & 0x80)) (*ModeIdIndex)++;
1873          if(VGAINFO & 0x10)    (*ModeIdIndex)++; /* 400 lines  */
1874          /* else 350 lines  */
1875       }
1876       /* else 200 lines  */
1877
1878    } else {
1879
1880       for(*ModeIdIndex = 0; ;(*ModeIdIndex)++) {
1881          if(SiS_Pr->SiS_EModeIDTable[*ModeIdIndex].Ext_ModeID == (*ModeNo)) break;
1882          if(SiS_Pr->SiS_EModeIDTable[*ModeIdIndex].Ext_ModeID == 0xFF)      return FALSE;
1883       }
1884
1885    }
1886    return TRUE;
1887 }
1888
1889 /*********************************************/
1890 /*            HELPER: GetModePtr             */
1891 /*********************************************/
1892
1893 UCHAR
1894 SiS_GetModePtr(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex)
1895 {
1896    UCHAR index;
1897
1898    if(ModeNo <= 0x13) {
1899         index = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_StTableIndex;
1900    } else {
1901         if(SiS_Pr->SiS_ModeType <= 0x02) index = 0x1B;    /* 02 -> ModeEGA  */
1902         else index = 0x0F;
1903    }
1904    return index;
1905 }
1906
1907 /*********************************************/
1908 /*           HELPER: LowModeTests            */
1909 /*********************************************/
1910
1911 static BOOLEAN
1912 SiS_DoLowModeTest(SiS_Private *SiS_Pr, USHORT ModeNo, PSIS_HW_INFO HwInfo)
1913 {
1914     USHORT temp,temp1,temp2;
1915
1916     if((ModeNo != 0x03) && (ModeNo != 0x10) && (ModeNo != 0x12))
1917        return(1);
1918     temp = SiS_GetReg(SiS_Pr->SiS_P3d4,0x11);
1919     SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x11,0x80);
1920     temp1 = SiS_GetReg(SiS_Pr->SiS_P3d4,0x00);
1921     SiS_SetReg(SiS_Pr->SiS_P3d4,0x00,0x55);
1922     temp2 = SiS_GetReg(SiS_Pr->SiS_P3d4,0x00);
1923     SiS_SetReg(SiS_Pr->SiS_P3d4,0x00,temp1);
1924     SiS_SetReg(SiS_Pr->SiS_P3d4,0x11,temp);
1925     if((HwInfo->jChipType >= SIS_315H) ||
1926        (HwInfo->jChipType == SIS_300)) {
1927        if(temp2 == 0x55) return(0);
1928        else return(1);
1929     } else {
1930        if(temp2 != 0x55) return(1);
1931        else {
1932           SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x35,0x01);
1933           return(0);
1934        }
1935     }
1936 }
1937
1938 static void
1939 SiS_SetLowModeTest(SiS_Private *SiS_Pr, USHORT ModeNo, PSIS_HW_INFO HwInfo)
1940 {
1941     if(SiS_DoLowModeTest(SiS_Pr, ModeNo, HwInfo)) {
1942        SiS_Pr->SiS_SetFlag |= LowModeTests;
1943     }
1944 }
1945
1946 /*********************************************/
1947 /*           HELPER: GetColorDepth           */
1948 /*********************************************/
1949
1950 USHORT
1951 SiS_GetColorDepth(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex)
1952 {
1953   USHORT ColorDepth[6] = { 1, 2, 4, 4, 6, 8};
1954   SHORT  index;
1955   USHORT modeflag;
1956
1957   /* Do NOT check UseCustomMode, will skrew up FIFO */
1958   if(ModeNo == 0xfe) {
1959      modeflag = SiS_Pr->CModeFlag;
1960   } else {
1961      if(ModeNo <= 0x13)
1962         modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
1963      else
1964         modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
1965   }
1966
1967   index = (modeflag & ModeInfoFlag) - ModeEGA;
1968   if(index < 0) index = 0;
1969   return(ColorDepth[index]);
1970 }
1971
1972 /*********************************************/
1973 /*             HELPER: GetOffset             */
1974 /*********************************************/
1975
1976 USHORT
1977 SiS_GetOffset(SiS_Private *SiS_Pr,USHORT ModeNo,USHORT ModeIdIndex,
1978               USHORT RefreshRateTableIndex,PSIS_HW_INFO HwInfo)
1979 {
1980   USHORT temp,colordepth,infoflag;
1981
1982   if(SiS_Pr->UseCustomMode) {
1983      infoflag = SiS_Pr->CInfoFlag;
1984      temp = SiS_Pr->CHDisplay / 16;
1985   } else {
1986      infoflag = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
1987      temp = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeOffset;
1988      temp = SiS_Pr->SiS_ScreenOffset[temp];
1989   }
1990
1991   colordepth = SiS_GetColorDepth(SiS_Pr,ModeNo,ModeIdIndex);
1992
1993   if(infoflag & InterlaceMode) temp <<= 1;
1994
1995   temp *= colordepth;
1996
1997   if( ( ((ModeNo >= 0x26) && (ModeNo <= 0x28)) ||
1998         ModeNo == 0x3f ||
1999         ModeNo == 0x42 ||
2000         ModeNo == 0x45 ) ||
2001       (SiS_Pr->UseCustomMode && (SiS_Pr->CHDisplay % 16)) ) {
2002      colordepth >>= 1;
2003      temp += colordepth;
2004   }
2005
2006   return(temp);
2007 }
2008
2009 /*********************************************/
2010 /*                   SEQ                     */
2011 /*********************************************/
2012
2013 static void
2014 SiS_SetSeqRegs(SiS_Private *SiS_Pr, USHORT StandTableIndex, PSIS_HW_INFO HwInfo)
2015 {
2016    UCHAR SRdata;
2017    USHORT i;
2018
2019    SiS_SetReg(SiS_Pr->SiS_P3c4,0x00,0x03);              /* Set SR0  */
2020
2021    SRdata = SiS_Pr->SiS_StandTable[StandTableIndex].SR[0];
2022
2023    if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
2024       if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
2025          SRdata |= 0x01;
2026       }
2027       if(HwInfo->jChipType >= SIS_661) {
2028          if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToTV)) {
2029             if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
2030                SRdata |= 0x01;                          /* 8 dot clock  */
2031             }
2032          }
2033       } else if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
2034          if(SiS_Pr->SiS_VBType & VB_NoLCD) {
2035             if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
2036                SRdata |= 0x01;                          /* 8 dot clock  */
2037             }
2038          }
2039       }
2040    }
2041
2042    if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
2043       if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
2044          if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
2045             if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
2046                SRdata |= 0x01;                          /* 8 dot clock  */
2047             }
2048          }
2049       }
2050       if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
2051          if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
2052             SRdata |= 0x01;                             /* 8 dot clock  */
2053          }
2054       }
2055    }
2056
2057    SRdata |= 0x20;                                      /* screen off  */
2058
2059    SiS_SetReg(SiS_Pr->SiS_P3c4,0x01,SRdata);
2060
2061    for(i = 2; i <= 4; i++) {
2062       SRdata = SiS_Pr->SiS_StandTable[StandTableIndex].SR[i-1];
2063       SiS_SetReg(SiS_Pr->SiS_P3c4,i,SRdata);
2064    }
2065 }
2066
2067 /*********************************************/
2068 /*                  MISC                     */
2069 /*********************************************/
2070
2071 static void
2072 SiS_SetMiscRegs(SiS_Private *SiS_Pr, USHORT StandTableIndex, PSIS_HW_INFO HwInfo)
2073 {
2074    UCHAR Miscdata;
2075
2076    Miscdata = SiS_Pr->SiS_StandTable[StandTableIndex].MISC;
2077
2078    if(HwInfo->jChipType < SIS_661) {
2079       if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
2080          if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
2081             Miscdata |= 0x0C;
2082          }
2083       }
2084    }
2085
2086    SiS_SetRegByte(SiS_Pr->SiS_P3c2,Miscdata);
2087 }
2088
2089 /*********************************************/
2090 /*                  CRTC                     */
2091 /*********************************************/
2092
2093 static void
2094 SiS_SetCRTCRegs(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,
2095                 USHORT StandTableIndex)
2096 {
2097   UCHAR CRTCdata;
2098   USHORT i;
2099
2100   SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x11,0x7f);                       /* Unlock CRTC */
2101
2102   for(i = 0; i <= 0x18; i++) {
2103      CRTCdata = SiS_Pr->SiS_StandTable[StandTableIndex].CRTC[i];
2104      SiS_SetReg(SiS_Pr->SiS_P3d4,i,CRTCdata);                     /* Set CRTC(3d4) */
2105   }
2106   if( ( (HwInfo->jChipType == SIS_630) ||
2107         (HwInfo->jChipType == SIS_730) )  &&
2108       (HwInfo->jChipRevision >= 0x30) ) {                  /* for 630S0 */
2109      if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
2110         if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToTV)) {
2111            SiS_SetReg(SiS_Pr->SiS_P3d4,0x18,0xFE);
2112         }
2113      }
2114   }
2115 }
2116
2117 /*********************************************/
2118 /*                   ATT                     */
2119 /*********************************************/
2120
2121 static void
2122 SiS_SetATTRegs(SiS_Private *SiS_Pr, USHORT StandTableIndex,
2123                PSIS_HW_INFO HwInfo)
2124 {
2125    UCHAR ARdata;
2126    USHORT i;
2127
2128    for(i = 0; i <= 0x13; i++) {
2129       ARdata = SiS_Pr->SiS_StandTable[StandTableIndex].ATTR[i];
2130 #if 0
2131       if((i <= 0x0f) || (i == 0x11)) {
2132          if(ds:489 & 0x08) {
2133             continue;
2134          }
2135       }
2136 #endif
2137       if(i == 0x13) {
2138          /* Pixel shift. If screen on LCD or TV is shifted left or right,
2139           * this might be the cause.
2140           */
2141          if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
2142             if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)  ARdata=0;
2143          }
2144          if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
2145             if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
2146                if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
2147                   if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) ARdata=0;
2148                }
2149             }
2150          }
2151          if(HwInfo->jChipType >= SIS_661) {
2152             if(SiS_Pr->SiS_VBInfo & (SetCRT2ToTV | SetCRT2ToLCD)) {
2153                if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) ARdata=0;
2154             }
2155          } else if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
2156             if(HwInfo->jChipType >= SIS_315H) {
2157                if(IS_SIS550650740660) {
2158                   /* 315, 330 don't do this */
2159                   if(SiS_Pr->SiS_VBType & VB_SIS301B302B) {
2160                      if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) ARdata=0;
2161                   } else {
2162                      ARdata = 0;
2163                   }
2164                }
2165             } else {
2166                if(SiS_Pr->SiS_VBInfo & SetInSlaveMode)  ARdata=0;
2167             }
2168          }
2169       }
2170       SiS_GetRegByte(SiS_Pr->SiS_P3da);                         /* reset 3da  */
2171       SiS_SetRegByte(SiS_Pr->SiS_P3c0,i);                       /* set index  */
2172       SiS_SetRegByte(SiS_Pr->SiS_P3c0,ARdata);                  /* set data   */
2173    }
2174    SiS_GetRegByte(SiS_Pr->SiS_P3da);                            /* reset 3da  */
2175    SiS_SetRegByte(SiS_Pr->SiS_P3c0,0x14);                       /* set index  */
2176    SiS_SetRegByte(SiS_Pr->SiS_P3c0,0x00);                       /* set data   */
2177
2178    SiS_GetRegByte(SiS_Pr->SiS_P3da);
2179    SiS_SetRegByte(SiS_Pr->SiS_P3c0,0x20);                       /* Enable Attribute  */
2180    SiS_GetRegByte(SiS_Pr->SiS_P3da);
2181 }
2182
2183 /*********************************************/
2184 /*                   GRC                     */
2185 /*********************************************/
2186
2187 static void
2188 SiS_SetGRCRegs(SiS_Private *SiS_Pr, USHORT StandTableIndex)
2189 {
2190    UCHAR GRdata;
2191    USHORT i;
2192
2193    for(i = 0; i <= 0x08; i++) {
2194       GRdata = SiS_Pr->SiS_StandTable[StandTableIndex].GRC[i];
2195       SiS_SetReg(SiS_Pr->SiS_P3ce,i,GRdata);
2196    }
2197
2198    if(SiS_Pr->SiS_ModeType > ModeVGA) {
2199       /* 256 color disable */
2200       SiS_SetRegAND(SiS_Pr->SiS_P3ce,0x05,0xBF);
2201    }
2202 }
2203
2204 /*********************************************/
2205 /*          CLEAR EXTENDED REGISTERS         */
2206 /*********************************************/
2207
2208 static void
2209 SiS_ClearExt1Regs(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
2210 {
2211   USHORT i;
2212
2213   for(i = 0x0A; i <= 0x0E; i++) {
2214      SiS_SetReg(SiS_Pr->SiS_P3c4,i,0x00);
2215   }
2216
2217   if(HwInfo->jChipType >= SIS_315H) {
2218      SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x37,0xFE);
2219   }
2220 }
2221
2222 /*********************************************/
2223 /*                 RESET VCLK                */
2224 /*********************************************/
2225
2226 static void
2227 SiS_ResetCRT1VCLK(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
2228 {
2229    if(HwInfo->jChipType >= SIS_315H) {
2230       if(HwInfo->jChipType < SIS_661) {
2231          if(SiS_Pr->SiS_IF_DEF_LVDS == 0) return;
2232       }
2233    } else {
2234       if((SiS_Pr->SiS_IF_DEF_LVDS == 0) &&
2235          (!(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV)) ) {
2236          return;
2237       }
2238    }
2239
2240    if(HwInfo->jChipType >= SIS_315H) {
2241       SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x31,0xCF,0x20);
2242    } else {
2243       SiS_SetReg(SiS_Pr->SiS_P3c4,0x31,0x20);
2244    }
2245    SiS_SetReg(SiS_Pr->SiS_P3c4,0x2B,SiS_Pr->SiS_VCLKData[1].SR2B);
2246    SiS_SetReg(SiS_Pr->SiS_P3c4,0x2C,SiS_Pr->SiS_VCLKData[1].SR2C);
2247    SiS_SetReg(SiS_Pr->SiS_P3c4,0x2D,0x80);
2248    if(HwInfo->jChipType >= SIS_315H) {
2249       SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x31,0xcf,0x10);
2250    } else {
2251       SiS_SetReg(SiS_Pr->SiS_P3c4,0x31,0x10);
2252    }
2253    SiS_SetReg(SiS_Pr->SiS_P3c4,0x2B,SiS_Pr->SiS_VCLKData[0].SR2B);
2254    SiS_SetReg(SiS_Pr->SiS_P3c4,0x2C,SiS_Pr->SiS_VCLKData[0].SR2C);
2255    SiS_SetReg(SiS_Pr->SiS_P3c4,0x2D,0x80);
2256 }
2257
2258 /*********************************************/
2259 /*                  SYNC                     */
2260 /*********************************************/
2261
2262 static void
2263 SiS_SetCRT1Sync(SiS_Private *SiS_Pr, USHORT RefreshRateTableIndex)
2264 {
2265   USHORT sync;
2266
2267   if(SiS_Pr->UseCustomMode) {
2268      sync = SiS_Pr->CInfoFlag >> 8;
2269   } else {
2270      sync = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_InfoFlag >> 8;
2271   }
2272
2273   sync &= 0xC0;
2274   sync |= 0x2f;
2275   SiS_SetRegByte(SiS_Pr->SiS_P3c2,sync);
2276 }
2277
2278 /*********************************************/
2279 /*                  CRTC/2                   */
2280 /*********************************************/
2281
2282 #ifdef SIS315H
2283 static void
2284 SiS_GetLCDACRT1Ptr(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,
2285                    USHORT RefreshRateTableIndex, USHORT *ResIndex,
2286                    USHORT *DisplayType)
2287  {
2288   USHORT modeflag = 0;
2289
2290   if(ModeNo <= 0x13) {
2291      modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
2292      *ResIndex = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_CRT2CRTC;
2293   } else {
2294      modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
2295      *ResIndex = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
2296   }
2297
2298   *ResIndex &= 0x3F;
2299
2300   *DisplayType = SiS_Pr->SiS_LCDResInfo;
2301   if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) *DisplayType += 32;
2302   if(modeflag & HalfDCLK)                 *DisplayType += 16;
2303
2304   if(SiS_Pr->SiS_CustomT == CUT_COMPAQ1280) {
2305      if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1280x1024) {
2306         *DisplayType = 100;
2307         if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) *DisplayType += 2;
2308         if(modeflag & HalfDCLK)                 *DisplayType += 1;
2309      }
2310   } else if(SiS_Pr->SiS_CustomT == CUT_CLEVO1024) {
2311      if(SiS_Pr->SiS_LCDResInfo == SiS_Pr->SiS_Panel1024x768) {
2312         *DisplayType = 104;
2313         if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) *DisplayType += 2;
2314         if(modeflag & HalfDCLK)                 *DisplayType += 1;
2315      }
2316   }
2317
2318 }
2319 #endif
2320
2321 static void
2322 SiS_SetCRT1CRTC(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,
2323                 USHORT RefreshRateTableIndex,
2324                 PSIS_HW_INFO HwInfo)
2325 {
2326   UCHAR  index;
2327   USHORT temp,i,j,modeflag;
2328 #ifdef SIS315H
2329   USHORT ResIndex,DisplayType;
2330   const SiS_LCDACRT1DataStruct *LCDACRT1Ptr = NULL;
2331 #endif
2332
2333   SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x11,0x7f);            /* unlock cr0-7 */
2334
2335   if(SiS_Pr->UseCustomMode) {
2336
2337      modeflag = SiS_Pr->CModeFlag;
2338
2339      for(i=0,j=0;i<=07;i++,j++) {
2340         SiS_SetReg(SiS_Pr->SiS_P3d4,j,SiS_Pr->CCRT1CRTC[i]);
2341      }
2342      for(j=0x10;i<=10;i++,j++) {
2343         SiS_SetReg(SiS_Pr->SiS_P3d4,j,SiS_Pr->CCRT1CRTC[i]);
2344      }
2345      for(j=0x15;i<=12;i++,j++) {
2346         SiS_SetReg(SiS_Pr->SiS_P3d4,j,SiS_Pr->CCRT1CRTC[i]);
2347      }
2348      for(j=0x0A;i<=15;i++,j++) {
2349         SiS_SetReg(SiS_Pr->SiS_P3c4,j,SiS_Pr->CCRT1CRTC[i]);
2350      }
2351
2352      temp = SiS_Pr->CCRT1CRTC[16] & 0xE0;
2353      SiS_SetReg(SiS_Pr->SiS_P3c4,0x0E,temp);
2354
2355      temp = (SiS_Pr->CCRT1CRTC[16] & 0x01) << 5;
2356      if(modeflag & DoubleScanMode) temp |= 0x80;
2357      SiS_SetRegANDOR(SiS_Pr->SiS_P3d4,0x09,0xDF,temp);
2358
2359   } else {
2360
2361      if(ModeNo <= 0x13) {
2362         modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
2363      } else {
2364         modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
2365      }
2366
2367      if((SiS_Pr->SiS_VBType & VB_SISVB) && (SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)) {
2368
2369 #ifdef SIS315H
2370
2371         SiS_GetLCDACRT1Ptr(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex, &ResIndex, &DisplayType);
2372
2373         switch(DisplayType) {
2374         case Panel_1024x768      : LCDACRT1Ptr = SiS_Pr->SiS_LCDACRT11024x768_1;     break;
2375         case Panel_1280x1024     : LCDACRT1Ptr = SiS_Pr->SiS_LCDACRT11280x1024_1;    break;
2376         case Panel_1400x1050     : LCDACRT1Ptr = SiS_Pr->SiS_LCDACRT11400x1050_1;    break;
2377         case Panel_1600x1200     : LCDACRT1Ptr = SiS_Pr->SiS_LCDACRT11600x1200_1;    break;
2378         case Panel_1024x768  + 16: LCDACRT1Ptr = SiS_Pr->SiS_LCDACRT11024x768_1_H;   break;
2379         case Panel_1280x1024 + 16: LCDACRT1Ptr = SiS_Pr->SiS_LCDACRT11280x1024_1_H;  break;
2380         case Panel_1400x1050 + 16: LCDACRT1Ptr = SiS_Pr->SiS_LCDACRT11400x1050_1_H;  break;
2381         case Panel_1600x1200 + 16: LCDACRT1Ptr = SiS_Pr->SiS_LCDACRT11600x1200_1_H;  break;
2382         case Panel_1024x768  + 32: LCDACRT1Ptr = SiS_Pr->SiS_LCDACRT11024x768_2;     break;
2383         case Panel_1280x1024 + 32: LCDACRT1Ptr = SiS_Pr->SiS_LCDACRT11280x1024_2;    break;
2384         case Panel_1400x1050 + 32: LCDACRT1Ptr = SiS_Pr->SiS_LCDACRT11400x1050_2;    break;
2385         case Panel_1600x1200 + 32: LCDACRT1Ptr = SiS_Pr->SiS_LCDACRT11600x1200_2;    break;
2386         case Panel_1024x768  + 48: LCDACRT1Ptr = SiS_Pr->SiS_LCDACRT11024x768_2_H;   break;
2387         case Panel_1280x1024 + 48: LCDACRT1Ptr = SiS_Pr->SiS_LCDACRT11280x1024_2_H;  break;
2388         case Panel_1400x1050 + 48: LCDACRT1Ptr = SiS_Pr->SiS_LCDACRT11400x1050_2_H;  break;
2389         case Panel_1600x1200 + 48: LCDACRT1Ptr = SiS_Pr->SiS_LCDACRT11600x1200_2_H;  break;
2390         case 100:                  LCDACRT1Ptr = Compaq1280x1024_LCDACRT1_1;         break;
2391         case 101:                  LCDACRT1Ptr = Compaq1280x1024_LCDACRT1_1_H;       break;
2392         case 102:                  LCDACRT1Ptr = SiS_Pr->SiS_LCDACRT11280x1024_2;    break;
2393         case 103:                  LCDACRT1Ptr = SiS_Pr->SiS_LCDACRT11280x1024_2_H;  break;
2394         case 104:                  LCDACRT1Ptr = Clevo1024x768_LCDACRT1_1;           break;
2395         case 105:                  LCDACRT1Ptr = Clevo1024x768_LCDACRT1_1_H;         break;
2396         case 106:                  LCDACRT1Ptr = Clevo1024x768_LCDACRT1_2;           break;
2397         case 107:                  LCDACRT1Ptr = Clevo1024x768_LCDACRT1_2_H;         break;
2398         default:                   LCDACRT1Ptr = SiS_Pr->SiS_LCDACRT11024x768_1;     break;
2399         }
2400
2401         for(i=0, j=0; i<=0x07; i++, j++) {
2402            SiS_SetReg(SiS_Pr->SiS_P3d4,i,(LCDACRT1Ptr+ResIndex)->CR[j]);
2403         }
2404         for(i=0x10, j=8; i<=0x12; i++, j++) {
2405            SiS_SetReg(SiS_Pr->SiS_P3d4,i,(LCDACRT1Ptr+ResIndex)->CR[j]);
2406         }
2407         for(i=0x15, j=11; i<=0x16; i++, j++) {
2408            SiS_SetReg(SiS_Pr->SiS_P3d4,i,(LCDACRT1Ptr+ResIndex)->CR[j]);
2409         }
2410         for(i=0x0A, j=13; i<=0x0C; i++, j++) {
2411            SiS_SetReg(SiS_Pr->SiS_P3c4,i,(LCDACRT1Ptr+ResIndex)->CR[j]);
2412         }
2413
2414         temp = (LCDACRT1Ptr+ResIndex)->CR[16] & 0xE0;
2415         SiS_SetReg(SiS_Pr->SiS_P3c4,0x0E,temp);
2416
2417         temp = ((LCDACRT1Ptr+ResIndex)->CR[16] & 0x01) << 5;
2418         if(modeflag & DoubleScanMode) temp |= 0x80;
2419         SiS_SetRegANDOR(SiS_Pr->SiS_P3d4,0x09,0xDF,temp);
2420
2421 #endif
2422
2423      } else {
2424
2425         index = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
2426
2427         for(i=0,j=0;i<=07;i++,j++) {
2428           SiS_SetReg(SiS_Pr->SiS_P3d4,j,SiS_Pr->SiS_CRT1Table[index].CR[i]);
2429         }
2430         for(j=0x10;i<=10;i++,j++) {
2431           SiS_SetReg(SiS_Pr->SiS_P3d4,j,SiS_Pr->SiS_CRT1Table[index].CR[i]);
2432         }
2433         for(j=0x15;i<=12;i++,j++) {
2434           SiS_SetReg(SiS_Pr->SiS_P3d4,j,SiS_Pr->SiS_CRT1Table[index].CR[i]);
2435         }
2436         for(j=0x0A;i<=15;i++,j++) {
2437           SiS_SetReg(SiS_Pr->SiS_P3c4,j,SiS_Pr->SiS_CRT1Table[index].CR[i]);
2438         }
2439
2440         temp = SiS_Pr->SiS_CRT1Table[index].CR[16] & 0xE0;
2441         SiS_SetReg(SiS_Pr->SiS_P3c4,0x0E,temp);
2442
2443         temp = ((SiS_Pr->SiS_CRT1Table[index].CR[16]) & 0x01) << 5;
2444         if(modeflag & DoubleScanMode)  temp |= 0x80;
2445         SiS_SetRegANDOR(SiS_Pr->SiS_P3d4,0x09,0xDF,temp);
2446
2447      }
2448   }
2449
2450   if(SiS_Pr->SiS_ModeType > ModeVGA) SiS_SetReg(SiS_Pr->SiS_P3d4,0x14,0x4F);
2451 }
2452
2453 /*********************************************/
2454 /*               OFFSET & PITCH              */
2455 /*********************************************/
2456 /*  (partly overruled by SetPitch() in XF86) */
2457 /*********************************************/
2458
2459 static void
2460 SiS_SetCRT1Offset(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,
2461                   USHORT RefreshRateTableIndex,
2462                   PSIS_HW_INFO HwInfo)
2463 {
2464    USHORT temp, DisplayUnit, infoflag;
2465
2466    if(SiS_Pr->UseCustomMode) {
2467       infoflag = SiS_Pr->CInfoFlag;
2468    } else {
2469       infoflag = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
2470    }
2471
2472    DisplayUnit = SiS_GetOffset(SiS_Pr,ModeNo,ModeIdIndex,
2473                                RefreshRateTableIndex,HwInfo);
2474
2475    temp = (DisplayUnit >> 8) & 0x0f;
2476    SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x0E,0xF0,temp);
2477
2478    temp = DisplayUnit & 0xFF;
2479    SiS_SetReg(SiS_Pr->SiS_P3d4,0x13,temp);
2480
2481    if(infoflag & InterlaceMode) DisplayUnit >>= 1;
2482
2483    DisplayUnit <<= 5;
2484    temp = (DisplayUnit & 0xff00) >> 8;
2485    if (DisplayUnit & 0xff) temp++;
2486    temp++;
2487    SiS_SetReg(SiS_Pr->SiS_P3c4,0x10,temp);
2488 }
2489
2490 /*********************************************/
2491 /*                  VCLK                     */
2492 /*********************************************/
2493
2494 static void
2495 SiS_SetCRT1VCLK(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,
2496                 PSIS_HW_INFO HwInfo, USHORT RefreshRateTableIndex)
2497 {
2498   USHORT  index=0, clka, clkb;
2499
2500   if(SiS_Pr->UseCustomMode) {
2501      clka = SiS_Pr->CSR2B;
2502      clkb = SiS_Pr->CSR2C;
2503   } else {
2504      index = SiS_GetVCLK2Ptr(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex, HwInfo);
2505      if((SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) && (SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)) {
2506         clka = SiS_Pr->SiS_VBVCLKData[index].Part4_A;
2507         clkb = SiS_Pr->SiS_VBVCLKData[index].Part4_B;
2508      } else {
2509         clka = SiS_Pr->SiS_VCLKData[index].SR2B;
2510         clkb = SiS_Pr->SiS_VCLKData[index].SR2C;
2511      }
2512   }
2513
2514   if(HwInfo->jChipType >= SIS_315H) {
2515      SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x31,0xCF);
2516   } else {
2517      SiS_SetReg(SiS_Pr->SiS_P3c4,0x31,0x00);
2518   }
2519
2520   SiS_SetReg(SiS_Pr->SiS_P3c4,0x2B,clka);
2521   SiS_SetReg(SiS_Pr->SiS_P3c4,0x2C,clkb);
2522
2523   if(HwInfo->jChipType >= SIS_315H) {
2524      SiS_SetReg(SiS_Pr->SiS_P3c4,0x2D,0x01);
2525   } else {
2526      SiS_SetReg(SiS_Pr->SiS_P3c4,0x2D,0x80);
2527   }
2528 }
2529
2530 /*********************************************/
2531 /*                  FIFO                     */
2532 /*********************************************/
2533
2534 #ifdef SIS300
2535 static USHORT
2536 SiS_DoCalcDelay(SiS_Private *SiS_Pr, USHORT MCLK, USHORT VCLK, USHORT colordepth, USHORT key)
2537 {
2538   const UCHAR ThLowA[]   = { 61, 3,52, 5,68, 7,100,11,
2539                              43, 3,42, 5,54, 7, 78,11,
2540                              34, 3,37, 5,47, 7, 67,11 };
2541
2542   const UCHAR ThLowB[]   = { 81, 4,72, 6,88, 8,120,12,
2543                              55, 4,54, 6,66, 8, 90,12,
2544                              42, 4,45, 6,55, 8, 75,12 };
2545
2546   const UCHAR ThTiming[] = {  1, 2, 2, 3, 0, 1,  1, 2 };
2547
2548   USHORT tempah, tempal, tempcl, tempbx, temp;
2549   ULONG  longtemp;
2550
2551   tempah = SiS_GetReg(SiS_Pr->SiS_P3c4,0x18);
2552   tempah &= 0x62;
2553   tempah >>= 1;
2554   tempal = tempah;
2555   tempah >>= 3;
2556   tempal |= tempah;
2557   tempal &= 0x07;
2558   tempcl = ThTiming[tempal];
2559   tempbx = SiS_GetReg(SiS_Pr->SiS_P3c4,0x16);
2560   tempbx >>= 6;
2561   tempah = SiS_GetReg(SiS_Pr->SiS_P3c4,0x14);
2562   tempah >>= 4;
2563   tempah &= 0x0c;
2564   tempbx |= tempah;
2565   tempbx <<= 1;
2566   if(key == 0) {
2567      tempal = ThLowA[tempbx + 1];
2568      tempal *= tempcl;
2569      tempal += ThLowA[tempbx];
2570   } else {
2571      tempal = ThLowB[tempbx + 1];
2572      tempal *= tempcl;
2573      tempal += ThLowB[tempbx];
2574   }
2575   longtemp = tempal * VCLK * colordepth;
2576   temp = longtemp % (MCLK * 16);
2577   longtemp /= (MCLK * 16);
2578   if(temp) longtemp++;
2579   return((USHORT)longtemp);
2580 }
2581
2582 static USHORT
2583 SiS_CalcDelay(SiS_Private *SiS_Pr, USHORT VCLK, USHORT colordepth, USHORT MCLK)
2584 {
2585   USHORT tempax, tempbx;
2586
2587   tempbx = SiS_DoCalcDelay(SiS_Pr, MCLK, VCLK, colordepth, 0);
2588   tempax = SiS_DoCalcDelay(SiS_Pr, MCLK, VCLK, colordepth, 1);
2589   if(tempax < 4) tempax = 4;
2590   tempax -= 4;
2591   if(tempbx < tempax) tempbx = tempax;
2592   return(tempbx);
2593 }
2594
2595 static void
2596 SiS_SetCRT1FIFO_300(SiS_Private *SiS_Pr, USHORT ModeNo, PSIS_HW_INFO HwInfo,
2597                     USHORT RefreshRateTableIndex)
2598 {
2599   USHORT  ThresholdLow = 0;
2600   USHORT  index, VCLK, MCLK, colorth=0;
2601   USHORT  tempah, temp;
2602
2603   if(ModeNo > 0x13) {
2604
2605      if(SiS_Pr->UseCustomMode) {
2606         VCLK = SiS_Pr->CSRClock;
2607      } else {
2608         index = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
2609         index &= 0x3F;
2610         VCLK = SiS_Pr->SiS_VCLKData[index].CLOCK;             /* Get VCLK  */
2611      }
2612
2613      switch (SiS_Pr->SiS_ModeType - ModeEGA) {     /* Get half colordepth */
2614         case 0 : colorth = 1; break;
2615         case 1 : colorth = 1; break;
2616         case 2 : colorth = 2; break;
2617         case 3 : colorth = 2; break;
2618         case 4 : colorth = 3; break;
2619         case 5 : colorth = 4; break;
2620      }
2621
2622      index = SiS_GetReg(SiS_Pr->SiS_P3c4,0x3A);
2623      index &= 0x07;
2624      MCLK = SiS_Pr->SiS_MCLKData_0[index].CLOCK;           /* Get MCLK  */
2625
2626      tempah = SiS_GetReg(SiS_Pr->SiS_P3d4,0x35);
2627      tempah &= 0xc3;
2628      SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x16,0x3c,tempah);
2629
2630      do {
2631         ThresholdLow = SiS_CalcDelay(SiS_Pr, VCLK, colorth, MCLK);
2632         ThresholdLow++;
2633         if(ThresholdLow < 0x13) break;
2634         SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x16,0xfc);
2635         ThresholdLow = 0x13;
2636         tempah = SiS_GetReg(SiS_Pr->SiS_P3c4,0x16);
2637         tempah >>= 6;
2638         if(!(tempah)) break;
2639         tempah--;
2640         tempah <<= 6;
2641         SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x16,0x3f,tempah);
2642      } while(0);
2643
2644   } else ThresholdLow = 2;
2645
2646   /* Write CRT/CPU threshold low, CRT/Engine threshold high */
2647   temp = (ThresholdLow << 4) | 0x0f;
2648   SiS_SetReg(SiS_Pr->SiS_P3c4,0x08,temp);
2649
2650   temp = (ThresholdLow & 0x10) << 1;
2651   if(ModeNo > 0x13) temp |= 0x40;
2652   SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x0f,0x9f,temp);
2653
2654   /* What is this? */
2655   SiS_SetReg(SiS_Pr->SiS_P3c4,0x3B,0x09);
2656
2657   /* Write CRT/CPU threshold high */
2658   temp = ThresholdLow + 3;
2659   if(temp > 0x0f) temp = 0x0f;
2660   SiS_SetReg(SiS_Pr->SiS_P3c4,0x09,temp);
2661 }
2662
2663 static USHORT
2664 SiS_CalcDelay2(SiS_Private *SiS_Pr, UCHAR key, PSIS_HW_INFO HwInfo)
2665 {
2666   USHORT data,index;
2667   const UCHAR  LatencyFactor[] = {
2668         97, 88, 86, 79, 77, 00,       /*; 64  bit    BQ=2   */
2669         00, 87, 85, 78, 76, 54,       /*; 64  bit    BQ=1   */
2670         97, 88, 86, 79, 77, 00,       /*; 128 bit    BQ=2   */
2671         00, 79, 77, 70, 68, 48,       /*; 128 bit    BQ=1   */
2672         80, 72, 69, 63, 61, 00,       /*; 64  bit    BQ=2   */
2673         00, 70, 68, 61, 59, 37,       /*; 64  bit    BQ=1   */
2674         86, 77, 75, 68, 66, 00,       /*; 128 bit    BQ=2   */
2675         00, 68, 66, 59, 57, 37        /*; 128 bit    BQ=1   */
2676   };
2677   const UCHAR  LatencyFactor730[] = {
2678          69, 63, 61,
2679          86, 79, 77,
2680         103, 96, 94,
2681         120,113,111,
2682         137,130,128,    /* --- Table ends with this entry, data below */
2683         137,130,128,    /* to avoid using illegal values              */
2684         137,130,128,
2685         137,130,128,
2686         137,130,128,
2687         137,130,128,
2688         137,130,128,
2689         137,130,128,
2690         137,130,128,
2691         137,130,128,
2692         137,130,128,
2693         137,130,128,
2694   };
2695
2696   if(HwInfo->jChipType == SIS_730) {
2697      index = ((key & 0x0f) * 3) + ((key & 0xC0) >> 6);
2698      data = LatencyFactor730[index];
2699   } else {
2700      index = (key & 0xE0) >> 5;
2701      if(key & 0x10) index +=6;
2702      if(!(key & 0x01)) index += 24;
2703      data = SiS_GetReg(SiS_Pr->SiS_P3c4,0x14);
2704      if(data & 0x0080) index += 12;
2705      data = LatencyFactor[index];
2706   }
2707   return(data);
2708 }
2709
2710 static void
2711 SiS_SetCRT1FIFO_630(SiS_Private *SiS_Pr, USHORT ModeNo,
2712                     PSIS_HW_INFO HwInfo,
2713                     USHORT RefreshRateTableIndex)
2714 {
2715   USHORT  i,index,data,VCLK,MCLK,colorth=0;
2716   ULONG   B,eax,bl,data2;
2717   USHORT  ThresholdLow=0;
2718   UCHAR   FQBQData[]= {
2719         0x01,0x21,0x41,0x61,0x81,
2720         0x31,0x51,0x71,0x91,0xb1,
2721         0x00,0x20,0x40,0x60,0x80,
2722         0x30,0x50,0x70,0x90,0xb0,
2723         0xFF
2724   };
2725   UCHAR   FQBQData730[]= {
2726         0x34,0x74,0xb4,
2727         0x23,0x63,0xa3,
2728         0x12,0x52,0x92,
2729         0x01,0x41,0x81,
2730         0x00,0x40,0x80,
2731         0xff
2732   };
2733
2734   i=0;
2735   if(ModeNo > 0x13) {
2736     if(SiS_Pr->UseCustomMode) {
2737        VCLK = SiS_Pr->CSRClock;
2738     } else {
2739        index = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
2740        index &= 0x3F;
2741        VCLK = SiS_Pr->SiS_VCLKData[index].CLOCK;          /* Get VCLK  */
2742     }
2743
2744     index = SiS_GetReg(SiS_Pr->SiS_P3c4,0x1A);
2745     index &= 0x07;
2746     MCLK = SiS_Pr->SiS_MCLKData_0[index].CLOCK;           /* Get MCLK  */
2747
2748     data2 = SiS_Pr->SiS_ModeType - ModeEGA;       /* Get half colordepth */
2749     switch (data2) {
2750         case 0 : colorth = 1; break;
2751         case 1 : colorth = 1; break;
2752         case 2 : colorth = 2; break;
2753         case 3 : colorth = 2; break;
2754         case 4 : colorth = 3; break;
2755         case 5 : colorth = 4; break;
2756     }
2757
2758     if(HwInfo->jChipType == SIS_730) {
2759
2760        do {
2761           B = SiS_CalcDelay2(SiS_Pr, FQBQData730[i], HwInfo) * VCLK * colorth;
2762           bl = B / (MCLK * 16);
2763
2764           if(B == bl * 16 * MCLK) {
2765              bl = bl + 1;
2766           } else {
2767              bl = bl + 2;
2768           }
2769
2770           if(bl > 0x13) {
2771              if(FQBQData730[i+1] == 0xFF) {
2772                 ThresholdLow = 0x13;
2773                 break;
2774              }
2775              i++;
2776           } else {
2777              ThresholdLow = bl;
2778              break;
2779           }
2780        } while(FQBQData730[i] != 0xFF);
2781
2782     } else {
2783
2784        do {
2785           B = SiS_CalcDelay2(SiS_Pr, FQBQData[i], HwInfo) * VCLK * colorth;
2786           bl = B / (MCLK * 16);
2787
2788           if(B == bl * 16 * MCLK) {
2789              bl = bl + 1;
2790           } else {
2791              bl = bl + 2;
2792           }
2793
2794           if(bl > 0x13) {
2795              if(FQBQData[i+1] == 0xFF) {
2796                 ThresholdLow = 0x13;
2797                 break;
2798              }
2799              i++;
2800           } else {
2801              ThresholdLow = bl;
2802              break;
2803           }
2804        } while(FQBQData[i] != 0xFF);
2805     }
2806   }
2807   else {
2808     if(HwInfo->jChipType == SIS_730) {
2809     } else {
2810       i = 9;
2811     }
2812     ThresholdLow = 0x02;
2813   }
2814
2815   /* Write foreground and background queue */
2816   if(HwInfo->jChipType == SIS_730) {
2817
2818      data2 = FQBQData730[i];
2819      data2 = (data2 & 0xC0) >> 5;
2820      data2 <<= 8;
2821
2822 #ifndef LINUX_XF86
2823      SiS_SetRegLong(0xcf8,0x80000050);
2824      eax = SiS_GetRegLong(0xcfc);
2825      eax &= 0xfffff9ff;
2826      eax |= data2;
2827      SiS_SetRegLong(0xcfc,eax);
2828 #else
2829      /* We use pci functions X offers. We use pcitag 0, because
2830       * we want to read/write to the host bridge (which is always
2831       * 00:00.0 on 630, 730 and 540), not the VGA device.
2832       */
2833      eax = pciReadLong(0x00000000, 0x50);
2834      eax &= 0xfffff9ff;
2835      eax |= data2;
2836      pciWriteLong(0x00000000, 0x50, eax);
2837 #endif
2838
2839      /* Write GUI grant timer (PCI config 0xA3) */
2840      data2 = FQBQData730[i] << 8;
2841      data2 = (data2 & 0x0f00) | ((data2 & 0x3000) >> 8);
2842      data2 <<= 20;
2843
2844 #ifndef LINUX_XF86
2845      SiS_SetRegLong(0xcf8,0x800000A0);
2846      eax = SiS_GetRegLong(0xcfc);
2847      eax &= 0x00ffffff;
2848      eax |= data2;
2849      SiS_SetRegLong(0xcfc,eax);
2850 #else
2851      eax = pciReadLong(0x00000000, 0xA0);
2852      eax &= 0x00ffffff;
2853      eax |= data2;
2854      pciWriteLong(0x00000000, 0xA0, eax);
2855 #endif
2856
2857   } else {
2858
2859      data2 = FQBQData[i];
2860      data2 = (data2 & 0xf0) >> 4;
2861      data2 <<= 24;
2862
2863 #ifndef LINUX_XF86
2864      SiS_SetRegLong(0xcf8,0x80000050);
2865      eax = SiS_GetRegLong(0xcfc);
2866      eax &= 0xf0ffffff;
2867      eax |= data2;
2868      SiS_SetRegLong(0xcfc,eax);
2869 #else
2870      eax = pciReadLong(0x00000000, 0x50);
2871      eax &= 0xf0ffffff;
2872      eax |= data2;
2873      pciWriteLong(0x00000000, 0x50, eax);
2874 #endif
2875
2876      /* Write GUI grant timer (PCI config 0xA3) */
2877      data2 = FQBQData[i];
2878      data2 &= 0x0f;
2879      data2 <<= 24;
2880
2881 #ifndef LINUX_XF86
2882      SiS_SetRegLong(0xcf8,0x800000A0);
2883      eax = SiS_GetRegLong(0xcfc);
2884      eax &= 0xf0ffffff;
2885      eax |= data2;
2886      SiS_SetRegLong(0xcfc,eax);
2887 #else
2888      eax = pciReadLong(0x00000000, 0xA0);
2889      eax &= 0xf0ffffff;
2890      eax |= data2;
2891      pciWriteLong(0x00000000, 0xA0, eax);
2892 #endif
2893
2894   }
2895
2896   /* Write CRT/CPU threshold low, CRT/Engine threshold high */
2897   data = ((ThresholdLow & 0x0f) << 4) | 0x0f;
2898   SiS_SetReg(SiS_Pr->SiS_P3c4,0x08,data);
2899
2900   data = (ThresholdLow & 0x10) << 1;
2901   SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x0F,0xDF,data);
2902
2903   /* What is this? */
2904   SiS_SetReg(SiS_Pr->SiS_P3c4,0x3B,0x09);
2905
2906   /* Write CRT/CPU threshold high (gap = 3) */
2907   data = ThresholdLow + 3;
2908   if(data > 0x0f) data = 0x0f;
2909   SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x09,0x80,data);
2910 }
2911 #endif
2912
2913 #ifdef SIS315H
2914 static void
2915 SiS_SetCRT1FIFO_310(SiS_Private *SiS_Pr, USHORT ModeNo, USHORT ModeIdIndex,
2916                     PSIS_HW_INFO HwInfo)
2917 {
2918   USHORT modeflag;
2919
2920   SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x3D,0xFE);  /* disable auto-threshold */
2921
2922   if(SiS_Pr->UseCustomMode) {
2923      modeflag = SiS_Pr->CModeFlag;
2924   } else {
2925      modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
2926   }
2927
2928   if(HwInfo->jChipType >= SIS_661) {
2929      SiS_SetReg(SiS_Pr->SiS_P3c4,0x08,0xAE);
2930      SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x09,0xF0);
2931      if(ModeNo > 0x13) {
2932         if(!(modeflag & HalfDCLK)) {
2933            SiS_SetReg(SiS_Pr->SiS_P3c4,0x08,0x34);
2934            if(ModeNo != 0x38) {
2935               SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x3D,0x01);
2936            }
2937         }
2938      }
2939   } else {
2940      if(ModeNo > 0x13) {
2941         if( (!(modeflag & DoubleScanMode)) || (!(modeflag & HalfDCLK))) {
2942            SiS_SetReg(SiS_Pr->SiS_P3c4,0x08,0x34);
2943            SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x09,0xF0);
2944            SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x3D,0x01);
2945         } else {
2946            SiS_SetReg(SiS_Pr->SiS_P3c4,0x08,0xAE);
2947            SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x09,0xF0);
2948         }
2949      } else {
2950         SiS_SetReg(SiS_Pr->SiS_P3c4,0x08,0xAE);
2951         SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x09,0xF0);
2952      }
2953   }
2954 }
2955 #endif
2956
2957 /*********************************************/
2958 /*              MODE REGISTERS               */
2959 /*********************************************/
2960
2961 static void
2962 SiS_SetVCLKState(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,
2963                  USHORT ModeNo, USHORT RefreshRateTableIndex,
2964                  USHORT ModeIdIndex)
2965 {
2966   USHORT data, data2=0;
2967   USHORT VCLK, index=0;
2968
2969   if(ModeNo <= 0x13) VCLK = 0;
2970   else {
2971      if(SiS_Pr->UseCustomMode) {
2972         VCLK = SiS_Pr->CSRClock;
2973      } else {
2974         index = SiS_GetVCLK2Ptr(SiS_Pr,ModeNo,ModeIdIndex,
2975                        RefreshRateTableIndex,HwInfo);
2976         VCLK = SiS_Pr->SiS_VCLKData[index].CLOCK;
2977      }
2978   }
2979
2980   if(HwInfo->jChipType < SIS_315H) {            /* 300 series */
2981
2982      data2 = 0x00;
2983      if(VCLK > 150) data2 |= 0x80;
2984      SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x07,0x7B,data2);
2985
2986      data2 = 0x00;
2987      if(VCLK >= 150) data2 |= 0x08;
2988      SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x32,0xF7,data2);
2989
2990   } else {                                      /* 315 series */
2991
2992      data = 0;
2993      if(VCLK >= 166) data |= 0x0c;
2994      SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x32,0xf3,data);
2995
2996      if(VCLK >= 166) {
2997         SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x1f,0xe7);
2998      }
2999   }
3000
3001   data2 = 0x03;
3002   if((VCLK >= 135) && (VCLK < 160))      data2 = 0x02;
3003   else if((VCLK >= 160) && (VCLK < 260)) data2 = 0x01;
3004   else if(VCLK >= 260)                   data2 = 0x00;
3005
3006   if(HwInfo->jChipType == SIS_540) {
3007      if((VCLK == 203) || (VCLK < 234)) data2 = 0x02;
3008   }
3009
3010   if(HwInfo->jChipType < SIS_315H) {
3011      SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x07,0xFC,data2);         /* DAC speed */
3012   } else {
3013      if(HwInfo->jChipType > SIS_315PRO) {
3014         /* This "if" is done in 330 and 650/LVDS/301LV BIOSes; Not in 315 BIOS */
3015         if(ModeNo > 0x13) data2 &= 0xfc;
3016      }
3017      SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x07,0xF8,data2);         /* DAC speed */
3018   }
3019 }
3020
3021 static void
3022 SiS_SetCRT1ModeRegs(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,
3023                     USHORT ModeNo,USHORT ModeIdIndex,USHORT RefreshRateTableIndex)
3024 {
3025   USHORT data,data2;
3026   USHORT infoflag=0,modeflag;
3027   USHORT resindex,xres;
3028 #ifdef SIS315H
3029   USHORT data3;
3030   ULONG  longdata;
3031 #if 0
3032   resinfo = 0;
3033 #endif
3034 #endif
3035
3036   if(SiS_Pr->UseCustomMode) {
3037      modeflag = SiS_Pr->CModeFlag;
3038      infoflag = SiS_Pr->CInfoFlag;
3039   } else {
3040      if(ModeNo > 0x13) {
3041         modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
3042         infoflag = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
3043 #ifdef SIS315H
3044 #if 0
3045         resinfo = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_RESINFO;
3046 #endif
3047 #endif
3048      } else {
3049         modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
3050      }
3051   }
3052
3053   /* Disable DPMS */
3054   SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x1F,0x3F);
3055
3056   if(ModeNo > 0x13) data = infoflag;
3057   else data = 0;
3058
3059   data2 = 0;
3060   if(ModeNo > 0x13) {
3061      if(SiS_Pr->SiS_ModeType > 0x02) {
3062         data2 |= 0x02;
3063         data2 |= ((SiS_Pr->SiS_ModeType - ModeVGA) << 2);
3064      }
3065   }
3066
3067 #ifdef TWDEBUG
3068   xf86DrvMsg(0, X_INFO, "Debug: Mode infoflag = %x, Chiptype %d\n",
3069         data, HwInfo->jChipType);
3070 #endif
3071
3072   if(data & InterlaceMode) data2 |= 0x20;
3073   SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x06,0xC0,data2);
3074
3075   if(SiS_Pr->UseCustomMode) {
3076      xres = SiS_Pr->CHDisplay;
3077   } else {
3078      resindex = SiS_GetResInfo(SiS_Pr,ModeNo,ModeIdIndex);
3079      if(ModeNo <= 0x13) {
3080         xres = SiS_Pr->SiS_StResInfo[resindex].HTotal;
3081      } else {
3082         xres = SiS_Pr->SiS_ModeResInfo[resindex].HTotal;
3083      }
3084   }
3085
3086   if(HwInfo->jChipType != SIS_300) {
3087      data = 0x0000;
3088      if(infoflag & InterlaceMode) {
3089         if(xres <= 800) data = 0x0020;
3090         else if(xres <= 1024) data = 0x0035;
3091         else data = 0x0048;
3092      }
3093      SiS_SetReg(SiS_Pr->SiS_P3d4,0x19,(data & 0x00FF));
3094      SiS_SetRegANDOR(SiS_Pr->SiS_P3d4,0x1a,0xFC,(data >> 8));
3095   }
3096
3097   if(modeflag & HalfDCLK) {
3098      SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x01,0x08);
3099   }
3100
3101   if(HwInfo->jChipType == SIS_300) {
3102      if(modeflag & LineCompareOff) {
3103         SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x0F,0x08);
3104      } else {
3105         SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x0F,0xF7);
3106      }
3107   } else {
3108      if(modeflag & LineCompareOff) {
3109         SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x0F,0xB7,0x08);
3110      } else {
3111         SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x0F,0xB7);
3112      }
3113      if(SiS_Pr->SiS_ModeType == ModeEGA) {
3114         if(ModeNo > 0x13) {
3115            SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x0F,0x40);
3116         }
3117      }
3118   }
3119
3120 #ifdef SIS315H
3121   /* 315 BIOS sets SR17 at this point */
3122   if(HwInfo->jChipType == SIS_315PRO) {
3123      data = SiS_Get310DRAMType(SiS_Pr, HwInfo);
3124      data = SiS_Pr->SiS_SR15[2][data];
3125      if(SiS_Pr->SiS_ModeType == ModeText) {
3126         data &= 0xc7;
3127      } else {
3128         data2 = SiS_GetOffset(SiS_Pr,ModeNo,ModeIdIndex,
3129                               RefreshRateTableIndex,HwInfo);
3130         data2 >>= 1;
3131         if(infoflag & InterlaceMode) data2 >>= 1;
3132         data3 = SiS_GetColorDepth(SiS_Pr,ModeNo,ModeIdIndex) >> 1;
3133         if(!data3) data3++;
3134         data2 /= data3;
3135         if(data2 >= 0x50) {
3136            data &= 0x0f;
3137            data |= 0x50;
3138         }
3139      }
3140      SiS_SetReg(SiS_Pr->SiS_P3c4,0x17,data);
3141   }
3142
3143   /* 330 BIOS sets SR17 at this point */
3144   if(HwInfo->jChipType == SIS_330) {
3145      data = SiS_Get310DRAMType(SiS_Pr, HwInfo);
3146      data = SiS_Pr->SiS_SR15[2][data];
3147      if(SiS_Pr->SiS_ModeType <= ModeEGA) {
3148         data &= 0xc7;
3149      } else {
3150         if(SiS_Pr->UseCustomMode) {
3151            data2 = SiS_Pr->CSRClock;
3152         } else {
3153            data2 = SiS_GetVCLK2Ptr(SiS_Pr,ModeNo,ModeIdIndex,
3154                                    RefreshRateTableIndex,HwInfo);
3155            data2 = SiS_Pr->SiS_VCLKData[data2].CLOCK;
3156         }
3157
3158         data3 = SiS_GetColorDepth(SiS_Pr,ModeNo,ModeIdIndex) >> 1;
3159         if(!data3) data3++;
3160
3161         data2 *= data3;
3162
3163         longdata = SiS_GetMCLK(SiS_Pr, HwInfo) * 1024;
3164
3165         data2 = longdata / data2;
3166
3167         if(SiS_Pr->SiS_ModeType != Mode16Bpp) {
3168            if(data2 >= 0x19c)      data = 0xba;
3169            else if(data2 >= 0x140) data = 0x7a;
3170            else if(data2 >= 0x101) data = 0x3a;
3171            else if(data2 >= 0xf5)  data = 0x32;
3172            else if(data2 >= 0xe2)  data = 0x2a;
3173            else if(data2 >= 0xc4)  data = 0x22;
3174            else if(data2 >= 0xac)  data = 0x1a;
3175            else if(data2 >= 0x9e)  data = 0x12;
3176            else if(data2 >= 0x8e)  data = 0x0a;
3177            else                    data = 0x02;
3178          } else {
3179            if(data2 >= 0x127)      data = 0xba;
3180            else                    data = 0x7a;
3181          }
3182       }
3183       SiS_SetReg(SiS_Pr->SiS_P3c4,0x17,data);
3184   }
3185 #endif
3186
3187   data = 0x60;
3188   if(SiS_Pr->SiS_ModeType != ModeText) {
3189      data ^= 0x60;
3190      if(SiS_Pr->SiS_ModeType != ModeEGA) {
3191         data ^= 0xA0;
3192      }
3193   }
3194   SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x21,0x1F,data);
3195
3196   SiS_SetVCLKState(SiS_Pr, HwInfo, ModeNo, RefreshRateTableIndex, ModeIdIndex);
3197
3198 #ifdef SIS315H
3199   if(HwInfo->jChipType >= SIS_315H) {
3200      if(SiS_GetReg(SiS_Pr->SiS_P3d4,0x31) & 0x40) {
3201         SiS_SetReg(SiS_Pr->SiS_P3d4,0x52,0x2c);
3202      } else {
3203         SiS_SetReg(SiS_Pr->SiS_P3d4,0x52,0x6c);
3204      }
3205 #if 0   /* What is SR0E[D5:6]? */
3206      if(HwInfo->jChipType >= SIS_661) {
3207         data = 0;
3208         if((ModeNo == 6) || ((ModeNo >= 0x0e) && (ModeNo <= 0x13))) {
3209            data |= 0x20;
3210         }
3211         if(SiS_Pr->SiS_ModeType != ModeVGA) {
3212            if(SiS_Pr->UseCustomMode) {
3213               if((xres >= 640) && (SiS_Pr->CVDisplay >= 480)) {
3214                  data |= 0x40;
3215               }
3216               if((xres > 1280) && (SiS_Pr->CVDisplay > 1024)) {
3217                  data |= 0x60;
3218               }
3219            }
3220         } else if(ModeNo > 0x13) {   /* These are in the CRT1 table, and set by CRT1CRTC */
3221            if(resinfo >= SIS_RI_640x480) {
3222               if(resinfo <= SIS_RI_2048x1536) {
3223                  data |= 0x40;
3224                  if(resinfo > SIS_RI_1280x1024) {
3225                     data |= 0x60;
3226                     if(resinfo != SIS_RI_1600x1200) {
3227                        data = SiS_GetReg(SiS_Pr->SiS_P3c4,0x0e);
3228                        data += 0x60;
3229                        SiS_SetReg(SiS_Pr->SiS_P3c4,0x0e);
3230                        data = 0;
3231                     }
3232                  }
3233               }
3234               if(resinfo == SIS_RI_1152x864) {
3235                  data = 0x40;
3236               }
3237               if(resinfo == SIS_RI_1400x1050) { /* TW */
3238                  data = 0x60;
3239               }
3240            }
3241         }
3242         SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x0e,data);
3243      }
3244 #endif
3245   }
3246 #endif
3247 }
3248
3249 /*********************************************/
3250 /*                 LOAD DAC                  */
3251 /*********************************************/
3252
3253 #if 0
3254 static void
3255 SiS_ClearDAC(SiS_Private *SiS_Pr, ULONG port)
3256 {
3257    int i;
3258
3259    OutPortByte(port, 0);
3260    port++;
3261    for (i=0; i < (256 * 3); i++) {
3262       OutPortByte(port, 0);
3263    }
3264 }
3265 #endif
3266
3267 static void
3268 SiS_WriteDAC(SiS_Private *SiS_Pr, SISIOADDRESS DACData, USHORT shiftflag,
3269              USHORT dl, USHORT ah, USHORT al, USHORT dh)
3270 {
3271   USHORT temp,bh,bl;
3272
3273   bh = ah;
3274   bl = al;
3275   if(dl != 0) {
3276      temp = bh;
3277      bh = dh;
3278      dh = temp;
3279      if(dl == 1) {
3280         temp = bl;
3281         bl = dh;
3282         dh = temp;
3283      } else {
3284         temp = bl;
3285         bl = bh;
3286         bh = temp;
3287      }
3288   }
3289   if(shiftflag) {
3290      dh <<= 2;
3291      bh <<= 2;
3292      bl <<= 2;
3293   }
3294   SiS_SetRegByte(DACData,(USHORT)dh);
3295   SiS_SetRegByte(DACData,(USHORT)bh);
3296   SiS_SetRegByte(DACData,(USHORT)bl);
3297 }
3298
3299 void
3300 SiS_LoadDAC(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,
3301             USHORT ModeNo, USHORT ModeIdIndex)
3302 {
3303    USHORT data,data2;
3304    USHORT time,i,j,k,m,n,o;
3305    USHORT si,di,bx,dl,al,ah,dh;
3306    USHORT shiftflag;
3307    SISIOADDRESS DACAddr, DACData;
3308    const USHORT *table = NULL;
3309
3310    if(ModeNo <= 0x13) {
3311       data = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
3312    } else {
3313       if(SiS_Pr->UseCustomMode) {
3314          data = SiS_Pr->CModeFlag;
3315       } else {
3316          data = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
3317       }
3318    }
3319
3320    data &= DACInfoFlag;
3321    time = 64;
3322    if(data == 0x00) table = SiS_MDA_DAC;
3323    if(data == 0x08) table = SiS_CGA_DAC;
3324    if(data == 0x10) table = SiS_EGA_DAC;
3325    if(data == 0x18) {
3326       time = 256;
3327       table = SiS_VGA_DAC;
3328    }
3329    if(time == 256) j = 16;
3330    else            j = time;
3331
3332    if( ( (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) &&        /* 301B-DH LCD */
3333          (SiS_Pr->SiS_VBType & VB_NoLCD) )        ||
3334        (SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)       ||   /* LCDA */
3335        (!(SiS_Pr->SiS_SetFlag & ProgrammingCRT2)) ) {  /* Programming CRT1 */
3336       DACAddr = SiS_Pr->SiS_P3c8;
3337       DACData = SiS_Pr->SiS_P3c9;
3338       shiftflag = 0;
3339       SiS_SetRegByte(SiS_Pr->SiS_P3c6,0xFF);
3340    } else {
3341       shiftflag = 1;
3342       DACAddr = SiS_Pr->SiS_Part5Port;
3343       DACData = SiS_Pr->SiS_Part5Port + 1;
3344    }
3345
3346    SiS_SetRegByte(DACAddr,0x00);
3347
3348    for(i=0; i<j; i++) {
3349       data = table[i];
3350       for(k=0; k<3; k++) {
3351         data2 = 0;
3352         if(data & 0x01) data2 = 0x2A;
3353         if(data & 0x02) data2 += 0x15;
3354         if(shiftflag) data2 <<= 2;
3355         SiS_SetRegByte(DACData, data2);
3356         data >>= 2;
3357       }
3358    }
3359
3360    if(time == 256) {
3361       for(i = 16; i < 32; i++) {
3362          data = table[i];
3363          if(shiftflag) data <<= 2;
3364          for(k = 0; k < 3; k++) SiS_SetRegByte(DACData, data);
3365       }
3366       si = 32;
3367       for(m = 0; m < 9; m++) {
3368          di = si;
3369          bx = si + 4;
3370          dl = 0;
3371          for(n = 0; n < 3; n++) {
3372             for(o = 0; o < 5; o++) {
3373                dh = table[si];
3374                ah = table[di];
3375                al = table[bx];
3376                si++;
3377                SiS_WriteDAC(SiS_Pr, DACData, shiftflag, dl, ah, al, dh);
3378             }
3379             si -= 2;
3380             for(o = 0; o < 3; o++) {
3381                dh = table[bx];
3382                ah = table[di];
3383                al = table[si];
3384                si--;
3385                SiS_WriteDAC(SiS_Pr, DACData, shiftflag, dl, ah, al, dh);
3386             }
3387             dl++;
3388          }            /* for n < 3 */
3389          si += 5;
3390       }               /* for m < 9 */
3391    }
3392 }
3393
3394 /*********************************************/
3395 /*         SET CRT1 REGISTER GROUP           */
3396 /*********************************************/
3397
3398 static void
3399 SiS_SetCRT1Group(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,
3400                  USHORT ModeNo, USHORT ModeIdIndex)
3401 {
3402   USHORT  StandTableIndex,RefreshRateTableIndex;
3403
3404   SiS_Pr->SiS_CRT1Mode = ModeNo;
3405   StandTableIndex = SiS_GetModePtr(SiS_Pr, ModeNo, ModeIdIndex);
3406   if(SiS_Pr->SiS_SetFlag & LowModeTests) {
3407      if(SiS_Pr->SiS_VBInfo & (SetSimuScanMode | SwitchCRT2)) {
3408         SiS_DisableBridge(SiS_Pr, HwInfo);
3409      }
3410   }
3411
3412   SiS_ResetSegmentRegisters(SiS_Pr, HwInfo);
3413
3414   SiS_SetSeqRegs(SiS_Pr, StandTableIndex, HwInfo);
3415   SiS_SetMiscRegs(SiS_Pr, StandTableIndex, HwInfo);
3416   SiS_SetCRTCRegs(SiS_Pr, HwInfo, StandTableIndex);
3417   SiS_SetATTRegs(SiS_Pr, StandTableIndex, HwInfo);
3418   SiS_SetGRCRegs(SiS_Pr, StandTableIndex);
3419   SiS_ClearExt1Regs(SiS_Pr,HwInfo);
3420   SiS_ResetCRT1VCLK(SiS_Pr, HwInfo);
3421
3422   SiS_Pr->SiS_SelectCRT2Rate = 0;
3423   SiS_Pr->SiS_SetFlag &= (~ProgrammingCRT2);
3424
3425 #ifdef LINUX_XF86
3426   xf86DrvMsgVerb(0, X_PROBED, 4, "(init: VBType=0x%04x, VBInfo=0x%04x)\n",
3427                     SiS_Pr->SiS_VBType, SiS_Pr->SiS_VBInfo);
3428 #endif
3429
3430   if(SiS_Pr->SiS_VBInfo & SetSimuScanMode) {
3431      if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
3432         SiS_Pr->SiS_SetFlag |= ProgrammingCRT2;
3433      }
3434   }
3435
3436   if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
3437      SiS_Pr->SiS_SetFlag |= ProgrammingCRT2;
3438   }
3439
3440   RefreshRateTableIndex = SiS_GetRatePtr(SiS_Pr, ModeNo, ModeIdIndex, HwInfo);
3441
3442   if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)) {
3443      SiS_Pr->SiS_SetFlag &= ~ProgrammingCRT2;
3444   }
3445
3446   if(RefreshRateTableIndex != 0xFFFF) {
3447      SiS_SetCRT1Sync(SiS_Pr, RefreshRateTableIndex);
3448      SiS_SetCRT1CRTC(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex, HwInfo);
3449      SiS_SetCRT1Offset(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex, HwInfo);
3450      SiS_SetCRT1VCLK(SiS_Pr, ModeNo, ModeIdIndex, HwInfo, RefreshRateTableIndex);
3451   }
3452
3453 #ifdef SIS300
3454   if(HwInfo->jChipType == SIS_300) {
3455      SiS_SetCRT1FIFO_300(SiS_Pr, ModeNo,HwInfo,RefreshRateTableIndex);
3456   } else if((HwInfo->jChipType == SIS_630) ||
3457             (HwInfo->jChipType == SIS_730) ||
3458             (HwInfo->jChipType == SIS_540)) {
3459      SiS_SetCRT1FIFO_630(SiS_Pr, ModeNo, HwInfo, RefreshRateTableIndex);
3460   }
3461 #endif
3462 #ifdef SIS315H
3463   if(HwInfo->jChipType >= SIS_315H) {
3464      SiS_SetCRT1FIFO_310(SiS_Pr, ModeNo, ModeIdIndex, HwInfo);
3465   }
3466 #endif
3467
3468   SiS_SetCRT1ModeRegs(SiS_Pr, HwInfo, ModeNo, ModeIdIndex, RefreshRateTableIndex);
3469
3470   SiS_LoadDAC(SiS_Pr, HwInfo, ModeNo, ModeIdIndex);
3471
3472 #ifndef LINUX_XF86
3473   if(SiS_Pr->SiS_flag_clearbuffer) {
3474      SiS_ClearBuffer(SiS_Pr,HwInfo,ModeNo);
3475   }
3476 #endif
3477
3478   if(!(SiS_Pr->SiS_VBInfo & (SetSimuScanMode | SwitchCRT2 | SetCRT2ToLCDA))) {
3479      SiS_WaitRetrace1(SiS_Pr);
3480      SiS_DisplayOn(SiS_Pr);
3481   }
3482 }
3483
3484 /*********************************************/
3485 /*            HELPER: ENABLE CRT1            */
3486 /*********************************************/
3487
3488 static void
3489 SiS_HandleCRT1(SiS_Private *SiS_Pr)
3490 {
3491   SiS_SetRegAND(SiS_Pr->SiS_P3d4,SiS_Pr->SiS_MyCR63,0xbf);
3492 #if 0
3493   if(!(SiS_GetReg(SiS_Pr->SiS_P3c4,0x15) & 0x01)) {
3494      if((SiS_GetReg(SiS_Pr->SiS_P3c4,0x15) & 0x0a) ||
3495         (SiS_GetReg(SiS_Pr->SiS_P3c4,0x16) & 0x01)) {
3496         SiS_SetRegOR(SiS_Pr->SiS_P3d4,SiS_Pr->SiS_MyCR63,0x40);
3497      }
3498   }
3499 #endif
3500 }
3501
3502 /*********************************************/
3503 /*         HELPER: SET VIDEO REGISTERS       */
3504 /*********************************************/
3505
3506 static void
3507 SiS_StrangeStuff(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
3508 {
3509    if((IS_SIS651) || (IS_SISM650)) {
3510       SiS_SetReg(SiS_Pr->SiS_VidCapt, 0x3f, 0x00);   /* Fiddle with capture regs */
3511       SiS_SetReg(SiS_Pr->SiS_VidCapt, 0x00, 0x00);
3512       SiS_SetReg(SiS_Pr->SiS_VidPlay, 0x00, 0x86);   /* (BIOS does NOT unlock) */
3513       SiS_SetRegAND(SiS_Pr->SiS_VidPlay, 0x30, 0xfe); /* Fiddle with video regs */
3514       SiS_SetRegAND(SiS_Pr->SiS_VidPlay, 0x3f, 0xef);
3515    }
3516    /* !!! This does not support modes < 0x13 !!! */
3517 }
3518
3519 /*********************************************/
3520 /*         XFree86: SET SCREEN PITCH         */
3521 /*********************************************/
3522
3523 #ifdef LINUX_XF86
3524 static void
3525 SiS_SetPitchCRT1(SiS_Private *SiS_Pr, ScrnInfoPtr pScrn)
3526 {
3527    SISPtr pSiS = SISPTR(pScrn);
3528    UShort HDisplay = pSiS->scrnPitch >> 3;
3529
3530    SiS_SetReg(SiS_Pr->SiS_P3d4,0x13,(HDisplay & 0xFF));
3531    SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x0E,0xF0,(HDisplay>>8));
3532 }
3533
3534 static void
3535 SiS_SetPitchCRT2(SiS_Private *SiS_Pr, ScrnInfoPtr pScrn)
3536 {
3537    SISPtr pSiS = SISPTR(pScrn);
3538    UShort HDisplay = pSiS->scrnPitch2 >> 3;
3539
3540     /* Unlock CRT2 */
3541    if(pSiS->VGAEngine == SIS_315_VGA)
3542      SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x2F, 0x01);
3543    else
3544      SiS_SetRegOR(SiS_Pr->SiS_Part1Port,0x24, 0x01);
3545
3546    SiS_SetReg(SiS_Pr->SiS_Part1Port,0x07,(HDisplay & 0xFF));
3547    SiS_SetRegANDOR(SiS_Pr->SiS_Part1Port,0x09,0xF0,(HDisplay >> 8));
3548 }
3549
3550 static void
3551 SiS_SetPitch(SiS_Private *SiS_Pr, ScrnInfoPtr pScrn)
3552 {
3553    SISPtr pSiS = SISPTR(pScrn);
3554    BOOLEAN isslavemode = FALSE;
3555
3556    if( (pSiS->VBFlags & VB_VIDEOBRIDGE) &&
3557        ( ((pSiS->VGAEngine == SIS_300_VGA) &&
3558           (SiS_GetReg(SiS_Pr->SiS_Part1Port,0x00) & 0xa0) == 0x20) ||
3559          ((pSiS->VGAEngine == SIS_315_VGA) &&
3560           (SiS_GetReg(SiS_Pr->SiS_Part1Port,0x00) & 0x50) == 0x10) ) ) {
3561       isslavemode = TRUE;
3562    }
3563
3564    /* We need to set pitch for CRT1 if bridge is in slave mode, too */
3565    if((pSiS->VBFlags & DISPTYPE_DISP1) || (isslavemode)) {
3566       SiS_SetPitchCRT1(SiS_Pr, pScrn);
3567    }
3568    /* We must not set the pitch for CRT2 if bridge is in slave mode */
3569    if((pSiS->VBFlags & DISPTYPE_DISP2) && (!isslavemode)) {
3570       SiS_SetPitchCRT2(SiS_Pr, pScrn);
3571    }
3572 }
3573 #endif
3574
3575 /*********************************************/
3576 /*                 SiSSetMode()              */
3577 /*********************************************/
3578
3579 #ifdef LINUX_XF86
3580 /* We need pScrn for setting the pitch correctly */
3581 BOOLEAN
3582 SiSSetMode(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,ScrnInfoPtr pScrn,USHORT ModeNo, BOOLEAN dosetpitch)
3583 #else
3584 BOOLEAN
3585 SiSSetMode(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,USHORT ModeNo)
3586 #endif
3587 {
3588    ULONG   temp;
3589    USHORT  ModeIdIndex;
3590    UCHAR  *ROMAddr  = HwInfo->pjVirtualRomBase;
3591    SISIOADDRESS BaseAddr = HwInfo->ulIOAddress;
3592    unsigned char backupreg=0;
3593 #ifndef LINUX_XF86
3594    USHORT  KeepLockReg;
3595
3596    SiS_Pr->UseCustomMode = FALSE;
3597    SiS_Pr->CRT1UsesCustomMode = FALSE;
3598 #endif
3599
3600    if(SiS_Pr->UseCustomMode) {
3601       ModeNo = 0xfe;
3602    }
3603
3604    SiSInitPtr(SiS_Pr, HwInfo);
3605    SiSRegInit(SiS_Pr, BaseAddr);
3606    SiS_GetSysFlags(SiS_Pr, HwInfo);
3607
3608 #ifdef LINUX_XF86
3609    if(pScrn) SiS_Pr->SiS_VGAINFO = SiS_GetSetBIOSScratch(pScrn, 0x489, 0xff);
3610    else
3611 #endif
3612          SiS_Pr->SiS_VGAINFO = 0x11;
3613
3614    SiSInitPCIetc(SiS_Pr, HwInfo);
3615    SiSSetLVDSetc(SiS_Pr, HwInfo);
3616    SiSDetermineROMUsage(SiS_Pr, HwInfo);
3617
3618    if(!SiS_Pr->UseCustomMode) {
3619       ModeNo = ((ModeNo & 0x80) << 8) | (ModeNo & 0x7f);
3620    }
3621
3622 #ifdef LINUX_XF86
3623    /* We never clear the buffer in X */
3624    ModeNo |= 0x8000;
3625 #endif
3626
3627    if(ModeNo & 0x8000) {
3628         ModeNo &= 0x7fff;
3629         SiS_Pr->SiS_flag_clearbuffer = 0;
3630    } else {
3631         SiS_Pr->SiS_flag_clearbuffer = 1;
3632    }
3633
3634 #ifndef LINUX_XF86
3635    KeepLockReg = SiS_GetReg(SiS_Pr->SiS_P3c4,0x05);
3636 #endif
3637    SiS_SetReg(SiS_Pr->SiS_P3c4,0x05,0x86);
3638
3639    SiS_UnLockCRT2(SiS_Pr, HwInfo);
3640
3641    if(!SiS_Pr->UseCustomMode) {
3642       if(!(SiS_SearchModeID(SiS_Pr, &ModeNo, &ModeIdIndex))) return FALSE;
3643    } else {
3644       ModeIdIndex = 0;
3645    }
3646
3647    SiS_GetVBType(SiS_Pr, HwInfo);
3648
3649    /* Init/restore some VB registers */
3650
3651    if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
3652       if(HwInfo->jChipType >= SIS_315H) {
3653          SiS_UnLockCRT2(SiS_Pr,HwInfo);
3654          if(ROMAddr && SiS_Pr->SiS_UseROM) {
3655             if(HwInfo->jChipType < SIS_330) {
3656                temp = ROMAddr[VB310Data_1_2_Offset];
3657                temp |= 0x40;
3658                SiS_SetReg(SiS_Pr->SiS_Part1Port,0x02,temp);
3659             }
3660             if(HwInfo->jChipType > SIS_330) {
3661                temp = ROMAddr[0x7e];
3662                if(SiS_GetReg(SiS_Pr->SiS_P3d4,0x7b) >= 100) temp |= 0x40;
3663                SiS_SetReg(SiS_Pr->SiS_Part1Port,0x02,temp);
3664             }
3665          }
3666          SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x32,0x10);
3667
3668          SiS_SetRegOR(SiS_Pr->SiS_Part2Port,0x00,0x0c);
3669
3670          backupreg = SiS_GetReg(SiS_Pr->SiS_P3d4,0x38);
3671       } else {
3672          backupreg = SiS_GetReg(SiS_Pr->SiS_P3d4,0x35);
3673       }
3674    }
3675
3676    /* Get VB information (connectors, connected devices) */
3677    SiS_GetVBInfo(SiS_Pr, ModeNo, ModeIdIndex, HwInfo, (SiS_Pr->UseCustomMode) ? 0 : 1);
3678    SiS_SetYPbPr(SiS_Pr, HwInfo);
3679    SiS_SetTVMode(SiS_Pr, ModeNo, ModeIdIndex, HwInfo);
3680    SiS_GetLCDResInfo(SiS_Pr, ModeNo, ModeIdIndex, HwInfo);
3681    SiS_SetLowModeTest(SiS_Pr, ModeNo, HwInfo);
3682
3683 #ifndef LINUX_XF86
3684    /* 3. Check memory size (Kernel framebuffer driver only) */
3685    temp = SiS_CheckMemorySize(SiS_Pr, HwInfo, ModeNo, ModeIdIndex);
3686    if(!temp) return(0);
3687 #endif
3688
3689    if(HwInfo->jChipType >= SIS_315H) {
3690 #if 0
3691       if(SiS_GetReg(SiS_Pr->SiS_P3c4,0x17) & 0x08)  {
3692          if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
3693             if(ModeNo != 0x10)  SiS_Pr->SiS_SetFlag |= SetDOSMode;
3694          } else if((IS_SIS651) && (SiS_Pr->SiS_VBType & VB_NoLCD)) {
3695             SiS_Pr->SiS_SetFlag |= SetDOSMode;
3696          }
3697       }
3698 #endif
3699
3700       if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
3701          if(IS_SIS650) {
3702             SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x51,0x1f);
3703             if(IS_SIS651) SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x51,0x20);
3704             SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x56,0xe7);
3705          } else if(IS_SIS661741660760) {
3706             SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x51,0x1f);
3707             SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x56,0xe7);
3708             SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x3a,0xef);
3709          }
3710       }
3711    }
3712
3713    if(SiS_Pr->UseCustomMode) {
3714       SiS_Pr->CRT1UsesCustomMode = TRUE;
3715       SiS_Pr->CSRClock_CRT1 = SiS_Pr->CSRClock;
3716       SiS_Pr->CModeFlag_CRT1 = SiS_Pr->CModeFlag;
3717    } else {
3718       SiS_Pr->CRT1UsesCustomMode = FALSE;
3719    }
3720
3721    /* Set mode on CRT1 */
3722    if( (SiS_Pr->SiS_VBInfo & (SetSimuScanMode | SetCRT2ToLCDA)) ||
3723        (!(SiS_Pr->SiS_VBInfo & SwitchCRT2)) ) {
3724       SiS_SetCRT1Group(SiS_Pr, HwInfo, ModeNo, ModeIdIndex);
3725    }
3726
3727    /* Set mode on CRT2 */
3728    if(SiS_Pr->SiS_VBInfo & (SetSimuScanMode | SwitchCRT2 | SetCRT2ToLCDA)) {
3729       if( (SiS_Pr->SiS_VBType & VB_SISVB)    ||
3730           (SiS_Pr->SiS_IF_DEF_LVDS     == 1) ||
3731           (SiS_Pr->SiS_IF_DEF_CH70xx   != 0) ||
3732           (SiS_Pr->SiS_IF_DEF_TRUMPION != 0) ) {
3733          SiS_SetCRT2Group(SiS_Pr, HwInfo, ModeNo);
3734       }
3735    }
3736
3737    SiS_HandleCRT1(SiS_Pr);
3738
3739    SiS_StrangeStuff(SiS_Pr, HwInfo);
3740
3741    SiS_DisplayOn(SiS_Pr);
3742    SiS_SetRegByte(SiS_Pr->SiS_P3c6,0xFF);
3743
3744    if(HwInfo->jChipType >= SIS_315H) {
3745       if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
3746          if(!(SiS_IsDualEdge(SiS_Pr, HwInfo))) {
3747             SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x13,0xfb);
3748          }
3749       }
3750    }
3751
3752    if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
3753       if(HwInfo->jChipType >= SIS_315H) {
3754          if(HwInfo->jChipType < SIS_661) {
3755             if(SiS_IsVAMode(SiS_Pr,HwInfo)) {
3756                SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x35,0x01);
3757             } else {
3758                SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x35,0xFE);
3759             }
3760          }
3761
3762          SiS_SetReg(SiS_Pr->SiS_P3d4,0x38,backupreg);
3763
3764          if((IS_SIS650) && (SiS_GetReg(SiS_Pr->SiS_P3d4,0x30) & 0xfc)) {
3765             if((ModeNo == 0x03) || (ModeNo == 0x10)) {
3766                SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x51,0x80);
3767                SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x56,0x08);
3768             }
3769          }
3770
3771          if(SiS_GetReg(SiS_Pr->SiS_P3d4,0x30) & SetCRT2ToLCD) {
3772             SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x38,0xfc);
3773          }
3774       } else if((HwInfo->jChipType == SIS_630) ||
3775                 (HwInfo->jChipType == SIS_730)) {
3776          SiS_SetReg(SiS_Pr->SiS_P3d4,0x35,backupreg);
3777       }
3778    }
3779
3780 #ifdef LINUX_XF86
3781    if(pScrn) {
3782       /* SetPitch: Adapt to virtual size & position */
3783       if((ModeNo > 0x13) && (dosetpitch)) {
3784          SiS_SetPitch(SiS_Pr, pScrn);
3785       }
3786
3787       /* Backup/Set ModeNo in BIOS scratch area */
3788       SiS_GetSetModeID(pScrn, ModeNo);
3789    }
3790 #endif
3791
3792 #ifndef LINUX_XF86  /* We never lock registers in XF86 */
3793    if(KeepLockReg == 0xA1) SiS_SetReg(SiS_Pr->SiS_P3c4,0x05,0x86);
3794    else SiS_SetReg(SiS_Pr->SiS_P3c4,0x05,0x00);
3795 #endif
3796
3797    return TRUE;
3798 }
3799
3800 /*********************************************/
3801 /*          XFree86: SiSBIOSSetMode()        */
3802 /*           for non-Dual-Head mode          */
3803 /*********************************************/
3804
3805 #ifdef LINUX_XF86
3806 BOOLEAN
3807 SiSBIOSSetMode(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo, ScrnInfoPtr pScrn,
3808                DisplayModePtr mode, BOOLEAN IsCustom)
3809 {
3810    SISPtr  pSiS = SISPTR(pScrn);
3811    UShort  ModeNo=0;
3812
3813    SiS_Pr->UseCustomMode = FALSE;
3814
3815    if((IsCustom) && (SiS_CheckBuildCustomMode(pScrn, mode, pSiS->VBFlags))) {
3816
3817          xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, 3, "Setting custom mode %dx%d\n",
3818                 SiS_Pr->CHDisplay,
3819                 (mode->Flags & V_INTERLACE ? SiS_Pr->CVDisplay * 2 :
3820                    (mode->Flags & V_DBLSCAN ? SiS_Pr->CVDisplay / 2 :
3821                       SiS_Pr->CVDisplay)));
3822
3823          return(SiSSetMode(SiS_Pr, HwInfo, pScrn, ModeNo, TRUE));
3824
3825    }
3826
3827    ModeNo = SiS_CalcModeIndex(pScrn, mode, pSiS->VBFlags, pSiS->HaveCustomModes);
3828    if(!ModeNo) return FALSE;
3829
3830    xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, 3, "Setting standard mode 0x%x\n", ModeNo);
3831
3832    return(SiSSetMode(SiS_Pr, HwInfo, pScrn, ModeNo, TRUE));
3833 }
3834
3835 /*********************************************/
3836 /*       XFree86: SiSBIOSSetModeCRT2()       */
3837 /*           for Dual-Head modes             */
3838 /*********************************************/
3839 BOOLEAN
3840 SiSBIOSSetModeCRT2(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo, ScrnInfoPtr pScrn,
3841                DisplayModePtr mode, BOOLEAN IsCustom)
3842 {
3843    ULONG   temp;
3844    USHORT  ModeIdIndex;
3845    UCHAR  *ROMAddr  = HwInfo->pjVirtualRomBase;
3846    SISIOADDRESS BaseAddr = HwInfo->ulIOAddress;
3847    UShort  ModeNo   = 0;
3848    unsigned char backupreg=0;
3849    SISPtr  pSiS     = SISPTR(pScrn);
3850 #ifdef SISDUALHEAD
3851    SISEntPtr pSiSEnt = pSiS->entityPrivate;
3852 #endif
3853
3854    SiS_Pr->UseCustomMode = FALSE;
3855
3856    /* Remember: Custom modes for CRT2 are ONLY supported
3857     *           -) on 315/330 series,
3858     *           -) on the 30x/B/C, and
3859     *           -) if CRT2 is LCD or VGA
3860     */
3861
3862    if((IsCustom) && (SiS_CheckBuildCustomMode(pScrn, mode, pSiS->VBFlags))) {
3863
3864          ModeNo = 0xfe;
3865
3866    } else {
3867
3868          BOOLEAN havecustommodes = pSiS->HaveCustomModes;
3869
3870 #ifdef SISMERGED
3871          if(pSiS->MergedFB) havecustommodes = pSiS->HaveCustomModes2;
3872 #endif
3873
3874          ModeNo = SiS_CalcModeIndex(pScrn, mode, pSiS->VBFlags, havecustommodes);
3875          if(!ModeNo) return FALSE;
3876
3877    }
3878
3879    SiSRegInit(SiS_Pr, BaseAddr);
3880    SiSInitPtr(SiS_Pr, HwInfo);
3881    SiS_GetSysFlags(SiS_Pr, HwInfo);
3882    SiS_Pr->SiS_VGAINFO = SiS_GetSetBIOSScratch(pScrn, 0x489, 0xff);
3883    SiSInitPCIetc(SiS_Pr, HwInfo);
3884    SiSSetLVDSetc(SiS_Pr, HwInfo);
3885    SiSDetermineROMUsage(SiS_Pr, HwInfo);
3886
3887    /* Save mode info so we can set it from within SetMode for CRT1 */
3888 #ifdef SISDUALHEAD
3889    if(pSiS->DualHeadMode) {
3890       pSiSEnt->CRT2ModeNo = ModeNo;
3891       pSiSEnt->CRT2DMode = mode;
3892       pSiSEnt->CRT2IsCustom = IsCustom;
3893       pSiSEnt->CRT2CR30 = SiS_GetReg(SiS_Pr->SiS_P3d4,0x30);
3894       pSiSEnt->CRT2CR31 = SiS_GetReg(SiS_Pr->SiS_P3d4,0x31);
3895       pSiSEnt->CRT2CR35 = SiS_GetReg(SiS_Pr->SiS_P3d4,0x35);
3896       pSiSEnt->CRT2CR38 = SiS_GetReg(SiS_Pr->SiS_P3d4,0x38);
3897 #if 0
3898       /* We can't set CRT2 mode before CRT1 mode is set */
3899       if(pSiSEnt->CRT1ModeNo == -1) {
3900          xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, 3,
3901                 "Setting CRT2 mode delayed until after setting CRT1 mode\n");
3902          return TRUE;
3903       }
3904 #endif
3905       pSiSEnt->CRT2ModeSet = TRUE;
3906    }
3907 #endif
3908
3909    /* We don't clear the buffer under X */
3910    SiS_Pr->SiS_flag_clearbuffer=0;
3911
3912    if(SiS_Pr->UseCustomMode) {
3913
3914       USHORT temptemp = SiS_Pr->CVDisplay;
3915
3916       if(SiS_Pr->CModeFlag & DoubleScanMode)     temptemp >>= 1;
3917       else if(SiS_Pr->CInfoFlag & InterlaceMode) temptemp <<= 1;
3918
3919       xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, 3,
3920           "Setting custom mode %dx%d on CRT2\n",
3921           SiS_Pr->CHDisplay, temptemp);
3922
3923    } else {
3924
3925       xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, 3,
3926           "Setting standard mode 0x%x on CRT2\n", ModeNo);
3927
3928    }
3929
3930    SiS_SetReg(SiS_Pr->SiS_P3c4,0x05,0x86);
3931
3932    SiS_UnLockCRT2(SiS_Pr, HwInfo);
3933
3934    if(!SiS_Pr->UseCustomMode) {
3935       if(!(SiS_SearchModeID(SiS_Pr, &ModeNo, &ModeIdIndex))) return FALSE;
3936    } else {
3937       ModeIdIndex = 0;
3938    }
3939
3940    SiS_GetVBType(SiS_Pr, HwInfo);
3941
3942    if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
3943       if(HwInfo->jChipType >= SIS_315H) {
3944          SiS_UnLockCRT2(SiS_Pr,HwInfo);
3945          if(ROMAddr && SiS_Pr->SiS_UseROM) {
3946             if(HwInfo->jChipType < SIS_330) {
3947                temp = ROMAddr[VB310Data_1_2_Offset];
3948                temp |= 0x40;
3949                SiS_SetReg(SiS_Pr->SiS_Part1Port,0x02,temp);
3950             }
3951             if(HwInfo->jChipType > SIS_330) {
3952                temp = ROMAddr[0x7e];
3953                if(SiS_GetReg(SiS_Pr->SiS_P3d4,0x7b) >= 100) temp |= 0x40;
3954                SiS_SetReg(SiS_Pr->SiS_Part1Port,0x02,temp);
3955             }
3956          }
3957          SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x32,0x10);
3958
3959          SiS_SetRegOR(SiS_Pr->SiS_Part2Port,0x00,0x0c);
3960
3961          backupreg = SiS_GetReg(SiS_Pr->SiS_P3d4,0x38);
3962       } else {
3963          backupreg = SiS_GetReg(SiS_Pr->SiS_P3d4,0x35);
3964       }
3965    }
3966
3967    /* Get VB information (connectors, connected devices) */
3968    if(!SiS_Pr->UseCustomMode) {
3969       SiS_GetVBInfo(SiS_Pr, ModeNo, ModeIdIndex, HwInfo, 1);
3970    } else {
3971       /* If this is a custom mode, we don't check the modeflag for CRT2Mode */
3972       SiS_GetVBInfo(SiS_Pr, ModeNo, ModeIdIndex, HwInfo, 0);
3973    }
3974    SiS_SetYPbPr(SiS_Pr, HwInfo);
3975    SiS_SetTVMode(SiS_Pr, ModeNo, ModeIdIndex, HwInfo);
3976    SiS_GetLCDResInfo(SiS_Pr, ModeNo, ModeIdIndex, HwInfo);
3977    SiS_SetLowModeTest(SiS_Pr, ModeNo, HwInfo);
3978
3979    /* Set mode on CRT2 */
3980    if( (SiS_Pr->SiS_VBType & VB_SISVB)    ||
3981        (SiS_Pr->SiS_IF_DEF_LVDS     == 1) ||
3982        (SiS_Pr->SiS_IF_DEF_CH70xx   != 0) ||
3983        (SiS_Pr->SiS_IF_DEF_TRUMPION != 0) ) {
3984       SiS_SetCRT2Group(SiS_Pr, HwInfo, ModeNo);
3985    }
3986
3987    SiS_StrangeStuff(SiS_Pr, HwInfo);
3988
3989    SiS_DisplayOn(SiS_Pr);
3990    SiS_SetRegByte(SiS_Pr->SiS_P3c6,0xFF);
3991
3992    if(HwInfo->jChipType >= SIS_315H) {
3993       if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
3994          if(!(SiS_IsDualEdge(SiS_Pr, HwInfo))) {
3995             SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x13,0xfb);
3996          }
3997       }
3998    }
3999
4000    if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
4001       if(HwInfo->jChipType >= SIS_315H) {
4002          if(HwInfo->jChipType < SIS_661) {
4003             if(SiS_IsVAMode(SiS_Pr,HwInfo)) {
4004                SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x35,0x01);
4005             } else {
4006                SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x35,0xFE);
4007             }
4008          }
4009
4010          SiS_SetReg(SiS_Pr->SiS_P3d4,0x38,backupreg);
4011
4012          if(SiS_GetReg(SiS_Pr->SiS_P3d4,0x30) & SetCRT2ToLCD) {
4013             SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x38,0xfc);
4014          }
4015       } else if((HwInfo->jChipType == SIS_630) ||
4016                 (HwInfo->jChipType == SIS_730)) {
4017          SiS_SetReg(SiS_Pr->SiS_P3d4,0x35,backupreg);
4018       }
4019    }
4020
4021    /* SetPitch: Adapt to virtual size & position */
4022    SiS_SetPitchCRT2(SiS_Pr, pScrn);
4023
4024    return TRUE;
4025 }
4026
4027 /*********************************************/
4028 /*       XFree86: SiSBIOSSetModeCRT1()       */
4029 /*           for Dual-Head modes             */
4030 /*********************************************/
4031
4032 BOOLEAN
4033 SiSBIOSSetModeCRT1(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo, ScrnInfoPtr pScrn,
4034                    DisplayModePtr mode, BOOLEAN IsCustom)
4035 {
4036    SISPtr  pSiS = SISPTR(pScrn);
4037    SISIOADDRESS BaseAddr = HwInfo->ulIOAddress;
4038    USHORT  ModeIdIndex, ModeNo=0;
4039    UCHAR backupreg=0;
4040 #ifdef SISDUALHEAD
4041    SISEntPtr pSiSEnt = pSiS->entityPrivate;
4042    UCHAR backupcr30, backupcr31, backupcr38, backupcr35, backupp40d=0;
4043    BOOLEAN backupcustom;
4044 #endif
4045
4046    SiS_Pr->UseCustomMode = FALSE;
4047
4048    if((IsCustom) && (SiS_CheckBuildCustomMode(pScrn, mode, pSiS->VBFlags))) {
4049
4050          USHORT temptemp = SiS_Pr->CVDisplay;
4051
4052          if(SiS_Pr->CModeFlag & DoubleScanMode)     temptemp >>= 1;
4053          else if(SiS_Pr->CInfoFlag & InterlaceMode) temptemp <<= 1;
4054
4055          xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, 3,
4056                 "Setting custom mode %dx%d on CRT1\n",
4057                 SiS_Pr->CHDisplay, temptemp);
4058          ModeNo = 0xfe;
4059
4060    } else {
4061
4062          ModeNo = SiS_CalcModeIndex(pScrn, mode, pSiS->VBFlags, pSiS->HaveCustomModes);
4063          if(!ModeNo) return FALSE;
4064
4065          xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, 3,
4066                 "Setting standard mode 0x%x on CRT1\n", ModeNo);
4067    }
4068
4069    SiSInitPtr(SiS_Pr, HwInfo);
4070    SiSRegInit(SiS_Pr, BaseAddr);
4071    SiS_GetSysFlags(SiS_Pr, HwInfo);
4072    SiS_Pr->SiS_VGAINFO = SiS_GetSetBIOSScratch(pScrn, 0x489, 0xff);
4073    SiSInitPCIetc(SiS_Pr, HwInfo);
4074    SiSSetLVDSetc(SiS_Pr, HwInfo);
4075    SiSDetermineROMUsage(SiS_Pr, HwInfo);
4076
4077    /* We don't clear the buffer under X */
4078    SiS_Pr->SiS_flag_clearbuffer = 0;
4079
4080    SiS_SetReg(SiS_Pr->SiS_P3c4,0x05,0x86);
4081
4082    SiS_UnLockCRT2(SiS_Pr, HwInfo);
4083
4084    if(!SiS_Pr->UseCustomMode) {
4085       if(!(SiS_SearchModeID(SiS_Pr, &ModeNo, &ModeIdIndex))) return FALSE;
4086    } else {
4087       ModeIdIndex = 0;
4088    }
4089
4090    /* Determine VBType */
4091    SiS_GetVBType(SiS_Pr, HwInfo);
4092
4093    if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
4094       if(HwInfo->jChipType >= SIS_315H) {
4095          backupreg = SiS_GetReg(SiS_Pr->SiS_P3d4,0x38);
4096       } else {
4097          backupreg = SiS_GetReg(SiS_Pr->SiS_P3d4,0x35);
4098       }
4099    }
4100
4101    /* Get VB information (connectors, connected devices) */
4102    /* (We don't care if the current mode is a CRT2 mode) */
4103    SiS_GetVBInfo(SiS_Pr, ModeNo, ModeIdIndex, HwInfo, 0);
4104    SiS_SetYPbPr(SiS_Pr, HwInfo);
4105    SiS_SetTVMode(SiS_Pr, ModeNo, ModeIdIndex, HwInfo);
4106    SiS_GetLCDResInfo(SiS_Pr, ModeNo, ModeIdIndex, HwInfo);
4107    SiS_SetLowModeTest(SiS_Pr, ModeNo, HwInfo);
4108
4109    if(HwInfo->jChipType >= SIS_315H) {
4110 #if 0
4111       if(SiS_GetReg(SiS_Pr->SiS_P3c4,0x17) & 0x08)  {
4112          if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
4113             if(ModeNo != 0x10)  SiS_Pr->SiS_SetFlag |= SetDOSMode;
4114          } else if((IS_SIS651) && (SiS_Pr->SiS_VBType & VB_NoLCD)) {
4115             SiS_Pr->SiS_SetFlag |= SetDOSMode;
4116          }
4117       }
4118 #endif
4119
4120       if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
4121          if(IS_SIS650) {
4122             SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x51,0x1f);
4123             if(IS_SIS651) SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x51,0x20);
4124             SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x56,0xe7);
4125          } else if(IS_SIS661741660760) {
4126             SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x51,0x1f);
4127             SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x56,0xe7);
4128             SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x3a,0xef);
4129          }
4130       }
4131    }
4132
4133    /* Set mode on CRT1 */
4134    SiS_SetCRT1Group(SiS_Pr, HwInfo, ModeNo, ModeIdIndex);
4135    if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
4136       SiS_SetCRT2Group(SiS_Pr, HwInfo, ModeNo);
4137    }
4138
4139    /* SetPitch: Adapt to virtual size & position */
4140    SiS_SetPitchCRT1(SiS_Pr, pScrn);
4141
4142 #ifdef SISDUALHEAD
4143    if(pSiS->DualHeadMode) {
4144       pSiSEnt->CRT1ModeNo = ModeNo;
4145       pSiSEnt->CRT1DMode = mode;
4146    }
4147 #endif
4148
4149    if(SiS_Pr->UseCustomMode) {
4150       SiS_Pr->CRT1UsesCustomMode = TRUE;
4151       SiS_Pr->CSRClock_CRT1 = SiS_Pr->CSRClock;
4152       SiS_Pr->CModeFlag_CRT1 = SiS_Pr->CModeFlag;
4153    } else {
4154       SiS_Pr->CRT1UsesCustomMode = FALSE;
4155    }
4156
4157    /* Reset CRT2 if changing mode on CRT1 */
4158 #ifdef SISDUALHEAD
4159    if(pSiS->DualHeadMode) {
4160       if(pSiSEnt->CRT2ModeNo != -1) {
4161          xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, 3,
4162                                 "(Re-)Setting mode for CRT2\n");
4163          backupcustom = SiS_Pr->UseCustomMode;
4164          backupcr30 = SiS_GetReg(SiS_Pr->SiS_P3d4,0x30);
4165          backupcr31 = SiS_GetReg(SiS_Pr->SiS_P3d4,0x31);
4166          backupcr35 = SiS_GetReg(SiS_Pr->SiS_P3d4,0x35);
4167          backupcr38 = SiS_GetReg(SiS_Pr->SiS_P3d4,0x38);
4168          if(SiS_Pr->SiS_VBType & VB_SISVB) {
4169             /* Backup LUT-enable */
4170             if(pSiSEnt->CRT2ModeSet) {
4171                backupp40d = SiS_GetReg(SiS_Pr->SiS_Part4Port,0x0d) & 0x08;
4172             }
4173          }
4174          if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
4175             SiS_SetReg(SiS_Pr->SiS_P3d4,0x30,pSiSEnt->CRT2CR30);
4176             SiS_SetReg(SiS_Pr->SiS_P3d4,0x31,pSiSEnt->CRT2CR31);
4177             SiS_SetReg(SiS_Pr->SiS_P3d4,0x35,pSiSEnt->CRT2CR35);
4178             SiS_SetReg(SiS_Pr->SiS_P3d4,0x38,pSiSEnt->CRT2CR38);
4179          }
4180          SiSBIOSSetModeCRT2(SiS_Pr, HwInfo, pSiSEnt->pScrn_1,
4181                             pSiSEnt->CRT2DMode, pSiSEnt->CRT2IsCustom);
4182          SiS_SetReg(SiS_Pr->SiS_P3d4,0x30,backupcr30);
4183          SiS_SetReg(SiS_Pr->SiS_P3d4,0x31,backupcr31);
4184          SiS_SetReg(SiS_Pr->SiS_P3d4,0x35,backupcr35);
4185          SiS_SetReg(SiS_Pr->SiS_P3d4,0x38,backupcr38);
4186          if(SiS_Pr->SiS_VBType & VB_SISVB) {
4187             SiS_SetRegANDOR(SiS_Pr->SiS_Part4Port,0x0d, ~0x08, backupp40d);
4188          }
4189          SiS_Pr->UseCustomMode = backupcustom;
4190       }
4191    }
4192 #endif
4193
4194    /* Warning: From here, the custom mode entries in SiS_Pr are
4195     * possibly overwritten
4196     */
4197
4198    SiS_HandleCRT1(SiS_Pr);
4199
4200    SiS_StrangeStuff(SiS_Pr, HwInfo);
4201
4202    SiS_DisplayOn(SiS_Pr);
4203    SiS_SetRegByte(SiS_Pr->SiS_P3c6,0xFF);
4204
4205    if(SiS_Pr->SiS_VBType & VB_SIS301BLV302BLV) {
4206       if(HwInfo->jChipType >= SIS_315H) {
4207          SiS_SetReg(SiS_Pr->SiS_P3d4,0x38,backupreg);
4208       } else if((HwInfo->jChipType == SIS_630) ||
4209                 (HwInfo->jChipType == SIS_730)) {
4210          SiS_SetReg(SiS_Pr->SiS_P3d4,0x35,backupreg);
4211       }
4212    }
4213
4214    /* Backup/Set ModeNo in BIOS scratch area */
4215    SiS_GetSetModeID(pScrn,ModeNo);
4216
4217    return TRUE;
4218 }
4219 #endif /* Linux_XF86 */
4220
4221
4222 #ifdef LINUX_XF86
4223 BOOLEAN
4224 SiS_GetPanelID(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo)
4225 {
4226   const USHORT PanelTypeTable300[16] = {
4227       0xc101, 0xc117, 0x0121, 0xc135, 0xc142, 0xc152, 0xc162, 0xc072,
4228       0xc181, 0xc192, 0xc1a1, 0xc1b6, 0xc1c2, 0xc0d2, 0xc1e2, 0xc1f2
4229   };
4230   const USHORT PanelTypeTable31030x[16] = {
4231       0xc102, 0xc112, 0x0122, 0xc132, 0xc142, 0xc152, 0xc169, 0xc179,
4232       0x0189, 0xc192, 0xc1a2, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
4233   };
4234   const USHORT PanelTypeTable310LVDS[16] = {
4235       0xc111, 0xc122, 0xc133, 0xc144, 0xc155, 0xc166, 0xc177, 0xc188,
4236       0xc199, 0xc0aa, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
4237   };
4238   USHORT tempax,tempbx,temp;
4239
4240   if(HwInfo->jChipType < SIS_315H) {
4241
4242      tempax = SiS_GetReg(SiS_Pr->SiS_P3c4,0x18);
4243      tempbx = tempax & 0x0F;
4244      if(!(tempax & 0x10)){
4245         if(SiS_Pr->SiS_IF_DEF_LVDS == 1){
4246            tempbx = 0;
4247            temp = SiS_GetReg(SiS_Pr->SiS_P3c4,0x38);
4248            if(temp & 0x40) tempbx |= 0x08;
4249            if(temp & 0x20) tempbx |= 0x02;
4250            if(temp & 0x01) tempbx |= 0x01;
4251            temp = SiS_GetReg(SiS_Pr->SiS_P3c4,0x39);
4252            if(temp & 0x80) tempbx |= 0x04;
4253         } else {
4254            return 0;
4255         }
4256      }
4257      tempbx = PanelTypeTable300[tempbx];
4258      tempbx |= LCDSync;
4259      temp = tempbx & 0x00FF;
4260      SiS_SetReg(SiS_Pr->SiS_P3d4,0x36,temp);
4261      temp = (tempbx & 0xFF00) >> 8;
4262      SiS_SetRegANDOR(SiS_Pr->SiS_P3d4,0x37,~(LCDSyncBit|LCDRGB18Bit),temp);
4263
4264   } else {
4265
4266      if(HwInfo->jChipType >= SIS_661) return 0;
4267
4268      tempax = SiS_GetReg(SiS_Pr->SiS_P3c4,0x1a);
4269      tempax &= 0x1e;
4270      tempax >>= 1;
4271      if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
4272         if(tempax == 0) {
4273            /* TODO: Include HUGE detection routine
4274                     (Probably not worth bothering)
4275             */
4276            return 0;
4277         }
4278         temp = tempax & 0xff;
4279         tempax--;
4280         tempbx = PanelTypeTable310LVDS[tempax];
4281      } else {
4282         tempbx = PanelTypeTable31030x[tempax];
4283         temp = tempbx & 0xff;
4284      }
4285      SiS_SetReg(SiS_Pr->SiS_P3d4,0x36,temp);
4286      tempbx = (tempbx & 0xff00) >> 8;
4287      temp = tempbx & 0xc1;
4288      SiS_SetRegANDOR(SiS_Pr->SiS_P3d4,0x37,~(LCDSyncBit|LCDRGB18Bit),temp);
4289      if(SiS_Pr->SiS_VBType & VB_SISVB) {
4290         temp = tempbx & 0x04;
4291         SiS_SetRegANDOR(SiS_Pr->SiS_P3d4,0x39,0xfb,temp);
4292      }
4293
4294   }
4295   return 1;
4296 }
4297 #endif
4298
4299
4300 /* ================ XFREE86 ================= */
4301
4302 /* Helper functions */
4303
4304 #ifdef LINUX_XF86
4305 USHORT
4306 SiS_CheckBuildCustomMode(ScrnInfoPtr pScrn, DisplayModePtr mode, int VBFlags)
4307 {
4308    SISPtr pSiS = SISPTR(pScrn);
4309    int    out_n, out_dn, out_div, out_sbit, out_scale;
4310    int    depth = pSiS->CurrentLayout.bitsPerPixel;
4311    unsigned int vclk[5];
4312
4313 #define Midx         0
4314 #define Nidx         1
4315 #define VLDidx       2
4316 #define Pidx         3
4317 #define PSNidx       4
4318
4319    pSiS->SiS_Pr->CModeFlag = 0;
4320    
4321    pSiS->SiS_Pr->CDClock = mode->Clock;
4322
4323    pSiS->SiS_Pr->CHDisplay = mode->HDisplay;
4324    pSiS->SiS_Pr->CHSyncStart = mode->HSyncStart;
4325    pSiS->SiS_Pr->CHSyncEnd = mode->HSyncEnd;
4326    pSiS->SiS_Pr->CHTotal = mode->HTotal;
4327
4328    pSiS->SiS_Pr->CVDisplay = mode->VDisplay;
4329    pSiS->SiS_Pr->CVSyncStart = mode->VSyncStart;
4330    pSiS->SiS_Pr->CVSyncEnd = mode->VSyncEnd;
4331    pSiS->SiS_Pr->CVTotal = mode->VTotal;
4332
4333    pSiS->SiS_Pr->CFlags = mode->Flags;
4334
4335    if(pSiS->SiS_Pr->CFlags & V_INTERLACE) {
4336       pSiS->SiS_Pr->CVDisplay >>= 1;
4337       pSiS->SiS_Pr->CVSyncStart >>= 1;
4338       pSiS->SiS_Pr->CVSyncEnd >>= 1;
4339       pSiS->SiS_Pr->CVTotal >>= 1;
4340    }
4341    if(pSiS->SiS_Pr->CFlags & V_DBLSCAN) {
4342       /* pSiS->SiS_Pr->CDClock <<= 1; */
4343       pSiS->SiS_Pr->CVDisplay <<= 1;
4344       pSiS->SiS_Pr->CVSyncStart <<= 1;
4345       pSiS->SiS_Pr->CVSyncEnd <<= 1;
4346       pSiS->SiS_Pr->CVTotal <<= 1;
4347    }
4348
4349    pSiS->SiS_Pr->CHBlankStart = pSiS->SiS_Pr->CHDisplay;
4350    pSiS->SiS_Pr->CHBlankEnd = pSiS->SiS_Pr->CHTotal;
4351    pSiS->SiS_Pr->CVBlankStart = pSiS->SiS_Pr->CVSyncStart - 1;
4352    pSiS->SiS_Pr->CVBlankEnd = pSiS->SiS_Pr->CVTotal;
4353
4354    if(SiS_compute_vclk(pSiS->SiS_Pr->CDClock, &out_n, &out_dn, &out_div, &out_sbit, &out_scale)) {
4355       pSiS->SiS_Pr->CSR2B = (out_div == 2) ? 0x80 : 0x00;
4356       pSiS->SiS_Pr->CSR2B |= ((out_n - 1) & 0x7f);
4357       pSiS->SiS_Pr->CSR2C = (out_dn - 1) & 0x1f;
4358       pSiS->SiS_Pr->CSR2C |= (((out_scale - 1) & 3) << 5);
4359       pSiS->SiS_Pr->CSR2C |= ((out_sbit & 0x01) << 7);
4360 #ifdef TWDEBUG
4361       xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Clock %d: n %d dn %d div %d sb %d sc %d\n",
4362                 pSiS->SiS_Pr->CDClock, out_n, out_dn, out_div, out_sbit, out_scale);
4363 #endif
4364    } else {
4365       SiSCalcClock(pScrn, pSiS->SiS_Pr->CDClock, 2, vclk);
4366       pSiS->SiS_Pr->CSR2B = (vclk[VLDidx] == 2) ? 0x80 : 0x00;
4367       pSiS->SiS_Pr->CSR2B |= (vclk[Midx] - 1) & 0x7f;
4368       pSiS->SiS_Pr->CSR2C = (vclk[Nidx] - 1) & 0x1f;
4369       if(vclk[Pidx] <= 4) {
4370          /* postscale 1,2,3,4 */
4371          pSiS->SiS_Pr->CSR2C |= ((vclk[Pidx] - 1) & 3) << 5;
4372       } else {
4373          /* postscale 6,8 */
4374          pSiS->SiS_Pr->CSR2C |= (((vclk[Pidx] / 2) - 1) & 3) << 5;
4375          pSiS->SiS_Pr->CSR2C |= 0x80;
4376       }
4377 #ifdef TWDEBUG
4378       xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Clock %d: n %d dn %d div %d sc %d\n",
4379                 pSiS->SiS_Pr->CDClock, vclk[Midx], vclk[Nidx], vclk[VLDidx], vclk[Pidx]);
4380 #endif
4381    }
4382
4383    pSiS->SiS_Pr->CSRClock = (pSiS->SiS_Pr->CDClock / 1000) + 1;
4384
4385    pSiS->SiS_Pr->CCRT1CRTC[0]  =  ((pSiS->SiS_Pr->CHTotal >> 3) - 5) & 0xff;
4386    pSiS->SiS_Pr->CCRT1CRTC[1]  =  (pSiS->SiS_Pr->CHDisplay >> 3) - 1;
4387    pSiS->SiS_Pr->CCRT1CRTC[2]  =  (pSiS->SiS_Pr->CHBlankStart >> 3) - 1;
4388    pSiS->SiS_Pr->CCRT1CRTC[3]  =  (((pSiS->SiS_Pr->CHBlankEnd >> 3) - 1) & 0x1F) | 0x80;
4389    pSiS->SiS_Pr->CCRT1CRTC[4]  =  (pSiS->SiS_Pr->CHSyncStart >> 3) + 3;
4390    pSiS->SiS_Pr->CCRT1CRTC[5]  =  ((((pSiS->SiS_Pr->CHBlankEnd >> 3) - 1) & 0x20) << 2) |
4391                                   (((pSiS->SiS_Pr->CHSyncEnd >> 3) + 3) & 0x1F);
4392
4393    pSiS->SiS_Pr->CCRT1CRTC[6]  =  (pSiS->SiS_Pr->CVTotal - 2) & 0xFF;
4394    pSiS->SiS_Pr->CCRT1CRTC[7]  =  (((pSiS->SiS_Pr->CVTotal - 2) & 0x100) >> 8)
4395                                 | (((pSiS->SiS_Pr->CVDisplay - 1) & 0x100) >> 7)
4396                                 | ((pSiS->SiS_Pr->CVSyncStart & 0x100) >> 6)
4397                                 | (((pSiS->SiS_Pr->CVBlankStart - 1) & 0x100) >> 5)
4398                                 | 0x10
4399                                 | (((pSiS->SiS_Pr->CVTotal - 2) & 0x200)   >> 4)
4400                                 | (((pSiS->SiS_Pr->CVDisplay - 1) & 0x200) >> 3)
4401                                 | ((pSiS->SiS_Pr->CVSyncStart & 0x200) >> 2);
4402
4403    pSiS->SiS_Pr->CCRT1CRTC[16] = ((((pSiS->SiS_Pr->CVBlankStart - 1) & 0x200) >> 4) >> 5);      /* cr9 */
4404
4405 #if 0
4406    if (mode->VScan >= 32)
4407         regp->CRTC[9] |= 0x1F;
4408    else if (mode->VScan > 1)
4409         regp->CRTC[9] |= mode->VScan - 1;
4410 #endif
4411
4412    pSiS->SiS_Pr->CCRT1CRTC[8] =  (pSiS->SiS_Pr->CVSyncStart     ) & 0xFF;               /* cr10 */
4413    pSiS->SiS_Pr->CCRT1CRTC[9] =  ((pSiS->SiS_Pr->CVSyncEnd      ) & 0x0F) | 0x80;       /* cr11 */
4414    pSiS->SiS_Pr->CCRT1CRTC[10] = (pSiS->SiS_Pr->CVDisplay    - 1) & 0xFF;               /* cr12 */
4415    pSiS->SiS_Pr->CCRT1CRTC[11] = (pSiS->SiS_Pr->CVBlankStart - 1) & 0xFF;               /* cr15 */
4416    pSiS->SiS_Pr->CCRT1CRTC[12] = (pSiS->SiS_Pr->CVBlankEnd   - 1) & 0xFF;               /* cr16 */
4417
4418    pSiS->SiS_Pr->CCRT1CRTC[13] =
4419                         GETBITSTR((pSiS->SiS_Pr->CVTotal     -2), 10:10, 0:0) |
4420                         GETBITSTR((pSiS->SiS_Pr->CVDisplay   -1), 10:10, 1:1) |
4421                         GETBITSTR((pSiS->SiS_Pr->CVBlankStart-1), 10:10, 2:2) |
4422                         GETBITSTR((pSiS->SiS_Pr->CVSyncStart   ), 10:10, 3:3) |
4423                         GETBITSTR((pSiS->SiS_Pr->CVBlankEnd  -1),   8:8, 4:4) |
4424                         GETBITSTR((pSiS->SiS_Pr->CVSyncEnd     ),   4:4, 5:5) ;
4425
4426    pSiS->SiS_Pr->CCRT1CRTC[14] =
4427                         GETBITSTR((pSiS->SiS_Pr->CHTotal      >> 3) - 5, 9:8, 1:0) |
4428                         GETBITSTR((pSiS->SiS_Pr->CHDisplay    >> 3) - 1, 9:8, 3:2) |
4429                         GETBITSTR((pSiS->SiS_Pr->CHBlankStart >> 3) - 1, 9:8, 5:4) |
4430                         GETBITSTR((pSiS->SiS_Pr->CHSyncStart  >> 3) + 3, 9:8, 7:6) ;
4431
4432
4433    pSiS->SiS_Pr->CCRT1CRTC[15] =
4434                         GETBITSTR((pSiS->SiS_Pr->CHBlankEnd >> 3) - 1, 7:6, 1:0) |
4435                         GETBITSTR((pSiS->SiS_Pr->CHSyncEnd  >> 3) + 3, 5:5, 2:2) ;
4436
4437    switch(depth) {
4438    case 8:
4439         pSiS->SiS_Pr->CModeFlag |= 0x223b;
4440         break;
4441    case 16:
4442         pSiS->SiS_Pr->CModeFlag |= 0x227d;
4443         break;
4444    case 32:
4445         pSiS->SiS_Pr->CModeFlag |= 0x22ff;
4446         break;          
4447    default: 
4448         return 0;       
4449    }    
4450    
4451    if(pSiS->SiS_Pr->CFlags & V_DBLSCAN) 
4452       pSiS->SiS_Pr->CModeFlag |= DoubleScanMode;
4453
4454    if((pSiS->SiS_Pr->CVDisplay >= 1024) ||
4455       (pSiS->SiS_Pr->CVTotal >= 1024)   ||
4456       (pSiS->SiS_Pr->CHDisplay >= 1024))
4457       pSiS->SiS_Pr->CModeFlag |= LineCompareOff;
4458
4459    if(pSiS->SiS_Pr->CFlags & V_CLKDIV2)
4460       pSiS->SiS_Pr->CModeFlag |= HalfDCLK;
4461
4462    pSiS->SiS_Pr->CInfoFlag = 0x0007;
4463
4464    if(pSiS->SiS_Pr->CFlags & V_NHSYNC)
4465       pSiS->SiS_Pr->CInfoFlag |= 0x4000;
4466
4467    if(pSiS->SiS_Pr->CFlags & V_NVSYNC)
4468       pSiS->SiS_Pr->CInfoFlag |= 0x8000;
4469
4470    if(pSiS->SiS_Pr->CFlags & V_INTERLACE)
4471       pSiS->SiS_Pr->CInfoFlag |= InterlaceMode;
4472
4473    pSiS->SiS_Pr->UseCustomMode = TRUE;
4474 #ifdef TWDEBUG
4475    xf86DrvMsg(0, X_INFO, "Custom mode %dx%d:\n", 
4476         pSiS->SiS_Pr->CHDisplay,pSiS->SiS_Pr->CVDisplay);
4477    xf86DrvMsg(0, X_INFO, "Modeflag %04x, Infoflag %04x\n",
4478         pSiS->SiS_Pr->CModeFlag, pSiS->SiS_Pr->CInfoFlag);
4479    xf86DrvMsg(0, X_INFO, " {{0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,\n",
4480         pSiS->SiS_Pr->CCRT1CRTC[0],
4481         pSiS->SiS_Pr->CCRT1CRTC[1],
4482         pSiS->SiS_Pr->CCRT1CRTC[2],
4483         pSiS->SiS_Pr->CCRT1CRTC[3],
4484         pSiS->SiS_Pr->CCRT1CRTC[4],
4485         pSiS->SiS_Pr->CCRT1CRTC[5],
4486         pSiS->SiS_Pr->CCRT1CRTC[6],
4487         pSiS->SiS_Pr->CCRT1CRTC[7]);
4488    xf86DrvMsg(0, X_INFO, "  0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,\n",
4489         pSiS->SiS_Pr->CCRT1CRTC[8],
4490         pSiS->SiS_Pr->CCRT1CRTC[9],
4491         pSiS->SiS_Pr->CCRT1CRTC[10],
4492         pSiS->SiS_Pr->CCRT1CRTC[11],
4493         pSiS->SiS_Pr->CCRT1CRTC[12],
4494         pSiS->SiS_Pr->CCRT1CRTC[13],
4495         pSiS->SiS_Pr->CCRT1CRTC[14],
4496         pSiS->SiS_Pr->CCRT1CRTC[15]);
4497    xf86DrvMsg(0, X_INFO, "  0x%02x}},\n", pSiS->SiS_Pr->CCRT1CRTC[16]);
4498    xf86DrvMsg(0, X_INFO, "Clock: 0x%02x, 0x%02x, %d\n",
4499         pSiS->SiS_Pr->CSR2B,
4500         pSiS->SiS_Pr->CSR2C,
4501         pSiS->SiS_Pr->CSRClock);
4502 #endif
4503    return 1;
4504 }
4505
4506 /* Build a list of supported modes */
4507 DisplayModePtr
4508 SiSBuildBuiltInModeList(ScrnInfoPtr pScrn, BOOLEAN includelcdmodes, BOOLEAN isfordvi)
4509 {
4510    SISPtr         pSiS = SISPTR(pScrn);
4511    unsigned short VRE, VBE, VRS, VBS, VDE, VT;
4512    unsigned short HRE, HBE, HRS, HBS, HDE, HT;
4513    unsigned char  sr_data, cr_data, cr_data2, cr_data3;
4514    unsigned char  sr2b, sr2c;
4515    float          num, denum, postscalar, divider;
4516    int            A, B, C, D, E, F, temp, i, j, k, l, index, vclkindex;
4517    DisplayModePtr new = NULL, current = NULL, first = NULL;
4518    BOOLEAN        done = FALSE;
4519 #if 0
4520    DisplayModePtr backup = NULL;
4521 #endif
4522
4523    pSiS->backupmodelist = NULL;
4524    pSiS->AddedPlasmaModes = FALSE;
4525
4526    /* Initialize our pointers */
4527    if(pSiS->VGAEngine == SIS_300_VGA) {
4528 #ifdef SIS300
4529       InitTo300Pointer(pSiS->SiS_Pr, &pSiS->sishw_ext);
4530 #else
4531       return NULL;
4532 #endif
4533    } else if(pSiS->VGAEngine == SIS_315_VGA) {
4534 #ifdef SIS315H
4535       InitTo310Pointer(pSiS->SiS_Pr, &pSiS->sishw_ext);
4536 #else
4537       return NULL;
4538 #endif
4539    } else return NULL;
4540
4541    i = 0;
4542    while(pSiS->SiS_Pr->SiS_RefIndex[i].Ext_InfoFlag != 0xFFFF) {
4543
4544       index = pSiS->SiS_Pr->SiS_RefIndex[i].Ext_CRT1CRTC;
4545
4546       /* 0x5a (320x240) is a pure FTSN mode, not DSTN! */
4547       if((!pSiS->FSTN) &&
4548          (pSiS->SiS_Pr->SiS_RefIndex[i].ModeID == 0x5a))  {
4549            i++;
4550            continue;
4551       }
4552       if((pSiS->FSTN) &&
4553          (pSiS->SiS_Pr->SiS_RefIndex[i].XRes == 320) &&
4554          (pSiS->SiS_Pr->SiS_RefIndex[i].YRes == 240) &&
4555          (pSiS->SiS_Pr->SiS_RefIndex[i].ModeID != 0x5a)) {
4556            i++;
4557            continue;
4558       }
4559
4560       if(!(new = xalloc(sizeof(DisplayModeRec)))) return first;
4561       memset(new, 0, sizeof(DisplayModeRec));
4562       if(!(new->name = xalloc(10))) {
4563                 xfree(new);
4564                 return first;
4565       }
4566       if(!first) first = new;
4567       if(current) {
4568          current->next = new;
4569          new->prev = current;
4570       }
4571
4572       current = new;
4573
4574       sprintf(current->name, "%dx%d", pSiS->SiS_Pr->SiS_RefIndex[i].XRes,
4575                                       pSiS->SiS_Pr->SiS_RefIndex[i].YRes);
4576
4577       current->status = MODE_OK;
4578
4579       current->type = M_T_DEFAULT;
4580
4581       vclkindex = pSiS->SiS_Pr->SiS_RefIndex[i].Ext_CRTVCLK;
4582       if(pSiS->VGAEngine == SIS_300_VGA) vclkindex &= 0x3F;
4583
4584       sr2b = pSiS->SiS_Pr->SiS_VCLKData[vclkindex].SR2B;
4585       sr2c = pSiS->SiS_Pr->SiS_VCLKData[vclkindex].SR2C;
4586
4587       divider = (sr2b & 0x80) ? 2.0 : 1.0;
4588       postscalar = (sr2c & 0x80) ?
4589               ( (((sr2c >> 5) & 0x03) == 0x02) ? 6.0 : 8.0) : (((sr2c >> 5) & 0x03) + 1.0);
4590       num = (sr2b & 0x7f) + 1.0;
4591       denum = (sr2c & 0x1f) + 1.0;
4592
4593 #ifdef TWDEBUG
4594       xf86DrvMsg(0, X_INFO, "------------\n");
4595       xf86DrvMsg(0, X_INFO, "sr2b: %x sr2c %x div %f ps %f num %f denum %f\n",
4596          sr2b, sr2c, divider, postscalar, num, denum);
4597 #endif
4598
4599       current->Clock = (int)(14318 * (divider / postscalar) * (num / denum));
4600
4601       sr_data = pSiS->SiS_Pr->SiS_CRT1Table[index].CR[14];
4602         /* inSISIDXREG(SISSR, 0x0b, sr_data); */
4603
4604       cr_data = pSiS->SiS_Pr->SiS_CRT1Table[index].CR[0];
4605         /* inSISIDXREG(SISCR, 0x00, cr_data); */
4606
4607       /* Horizontal total */
4608       HT = (cr_data & 0xff) |
4609            ((unsigned short) (sr_data & 0x03) << 8);
4610       A = HT + 5;
4611
4612       cr_data = pSiS->SiS_Pr->SiS_CRT1Table[index].CR[1];
4613         /* inSISIDXREG(SISCR, 0x01, cr_data); */
4614
4615       /* Horizontal display enable end */
4616       HDE = (cr_data & 0xff) |
4617             ((unsigned short) (sr_data & 0x0C) << 6);
4618       E = HDE + 1;
4619
4620       cr_data = pSiS->SiS_Pr->SiS_CRT1Table[index].CR[4];
4621         /* inSISIDXREG(SISCR, 0x04, cr_data); */
4622
4623       /* Horizontal retrace (=sync) start */
4624       HRS = (cr_data & 0xff) |
4625             ((unsigned short) (sr_data & 0xC0) << 2);
4626       F = HRS - E - 3;
4627
4628       cr_data = pSiS->SiS_Pr->SiS_CRT1Table[index].CR[2];
4629         /* inSISIDXREG(SISCR, 0x02, cr_data); */
4630
4631       /* Horizontal blank start */
4632       HBS = (cr_data & 0xff) |
4633             ((unsigned short) (sr_data & 0x30) << 4);
4634
4635       sr_data = pSiS->SiS_Pr->SiS_CRT1Table[index].CR[15];
4636         /* inSISIDXREG(SISSR, 0x0c, sr_data); */
4637
4638       cr_data = pSiS->SiS_Pr->SiS_CRT1Table[index].CR[3];
4639         /* inSISIDXREG(SISCR, 0x03, cr_data);  */
4640
4641       cr_data2 = pSiS->SiS_Pr->SiS_CRT1Table[index].CR[5];
4642         /* inSISIDXREG(SISCR, 0x05, cr_data2); */
4643
4644       /* Horizontal blank end */
4645       HBE = (cr_data & 0x1f) |
4646             ((unsigned short) (cr_data2 & 0x80) >> 2) |
4647             ((unsigned short) (sr_data & 0x03) << 6);
4648
4649       /* Horizontal retrace (=sync) end */
4650       HRE = (cr_data2 & 0x1f) | ((sr_data & 0x04) << 3);
4651
4652       temp = HBE - ((E - 1) & 255);
4653       B = (temp > 0) ? temp : (temp + 256);
4654
4655       temp = HRE - ((E + F + 3) & 63);
4656       C = (temp > 0) ? temp : (temp + 64);
4657
4658       D = B - F - C;
4659
4660       if((pSiS->SiS_Pr->SiS_RefIndex[i].XRes == 320) &&
4661          ((pSiS->SiS_Pr->SiS_RefIndex[i].YRes == 200) ||
4662           (pSiS->SiS_Pr->SiS_RefIndex[i].YRes == 240))) {
4663
4664          /* Terrible hack, but correct CRTC data for
4665           * these modes only produces a black screen...
4666           * (HRE is 0, leading into a too large C and
4667           * a negative D. The CRT controller does not
4668           * seem to like correcting HRE to 50
4669           */
4670          current->HDisplay   = 320;
4671          current->HSyncStart = 328;
4672          current->HSyncEnd   = 376;
4673          current->HTotal     = 400;
4674
4675       } else {
4676
4677          current->HDisplay   = (E * 8);
4678          current->HSyncStart = (E * 8) + (F * 8);
4679          current->HSyncEnd   = (E * 8) + (F * 8) + (C * 8);
4680          current->HTotal     = (E * 8) + (F * 8) + (C * 8) + (D * 8);
4681
4682       }
4683
4684 #ifdef TWDEBUG
4685       xf86DrvMsg(0, X_INFO,
4686         "H: A %d B %d C %d D %d E %d F %d  HT %d HDE %d HRS %d HBS %d HBE %d HRE %d\n",
4687         A, B, C, D, E, F, HT, HDE, HRS, HBS, HBE, HRE);
4688 #endif
4689
4690       sr_data = pSiS->SiS_Pr->SiS_CRT1Table[index].CR[13];
4691         /* inSISIDXREG(SISSR, 0x0A, sr_data); */
4692
4693       cr_data = pSiS->SiS_Pr->SiS_CRT1Table[index].CR[6];
4694         /* inSISIDXREG(SISCR, 0x06, cr_data); */
4695
4696       cr_data2 = pSiS->SiS_Pr->SiS_CRT1Table[index].CR[7];
4697         /* inSISIDXREG(SISCR, 0x07, cr_data2);  */
4698
4699       /* Vertical total */
4700       VT = (cr_data & 0xFF) |
4701            ((unsigned short) (cr_data2 & 0x01) << 8) |
4702            ((unsigned short)(cr_data2 & 0x20) << 4) |
4703            ((unsigned short) (sr_data & 0x01) << 10);
4704       A = VT + 2;
4705
4706       cr_data = pSiS->SiS_Pr->SiS_CRT1Table[index].CR[10];
4707         /* inSISIDXREG(SISCR, 0x12, cr_data);  */
4708
4709       /* Vertical display enable end */
4710       VDE = (cr_data & 0xff) |
4711             ((unsigned short) (cr_data2 & 0x02) << 7) |
4712             ((unsigned short) (cr_data2 & 0x40) << 3) |
4713             ((unsigned short) (sr_data & 0x02) << 9);
4714       E = VDE + 1;
4715
4716       cr_data = pSiS->SiS_Pr->SiS_CRT1Table[index].CR[8];
4717         /* inSISIDXREG(SISCR, 0x10, cr_data); */
4718
4719       /* Vertical retrace (=sync) start */
4720       VRS = (cr_data & 0xff) |
4721             ((unsigned short) (cr_data2 & 0x04) << 6) |
4722             ((unsigned short) (cr_data2 & 0x80) << 2) |
4723             ((unsigned short) (sr_data & 0x08) << 7);
4724       F = VRS + 1 - E;
4725
4726       cr_data =  pSiS->SiS_Pr->SiS_CRT1Table[index].CR[11];
4727         /* inSISIDXREG(SISCR, 0x15, cr_data);  */
4728
4729       cr_data3 = (pSiS->SiS_Pr->SiS_CRT1Table[index].CR[16] & 0x01) << 5;
4730         /* inSISIDXREG(SISCR, 0x09, cr_data3);  */
4731
4732       /* Vertical blank start */
4733       VBS = (cr_data & 0xff) |
4734             ((unsigned short) (cr_data2 & 0x08) << 5) |
4735             ((unsigned short) (cr_data3 & 0x20) << 4) |
4736             ((unsigned short) (sr_data & 0x04) << 8);
4737
4738       cr_data =  pSiS->SiS_Pr->SiS_CRT1Table[index].CR[12];
4739         /* inSISIDXREG(SISCR, 0x16, cr_data); */
4740
4741       /* Vertical blank end */
4742       VBE = (cr_data & 0xff) |
4743             ((unsigned short) (sr_data & 0x10) << 4);
4744       temp = VBE - ((E - 1) & 511);
4745       B = (temp > 0) ? temp : (temp + 512);
4746
4747       cr_data = pSiS->SiS_Pr->SiS_CRT1Table[index].CR[9];
4748         /* inSISIDXREG(SISCR, 0x11, cr_data); */
4749
4750       /* Vertical retrace (=sync) end */
4751       VRE = (cr_data & 0x0f) | ((sr_data & 0x20) >> 1);
4752       temp = VRE - ((E + F - 1) & 31);
4753       C = (temp > 0) ? temp : (temp + 32);
4754
4755       D = B - F - C;
4756
4757       current->VDisplay   = VDE + 1;
4758       current->VSyncStart = VRS + 1;
4759       current->VSyncEnd   = ((VRS & ~0x1f) | VRE) + 1;
4760       if(VRE <= (VRS & 0x1f)) current->VSyncEnd += 32;
4761       current->VTotal     = E + D + C + F;
4762
4763 #if 0
4764       current->VDisplay   = E;
4765       current->VSyncStart = E + D;
4766       current->VSyncEnd   = E + D + C;
4767       current->VTotal     = E + D + C + F;
4768 #endif
4769
4770 #ifdef TWDEBUG
4771       xf86DrvMsg(0, X_INFO,
4772         "V: A %d B %d C %d D %d E %d F %d  VT %d VDE %d VRS %d VBS %d VBE %d VRE %d\n",
4773         A, B, C, D, E, F, VT, VDE, VRS, VBS, VBE, VRE);
4774 #endif
4775
4776       if(pSiS->SiS_Pr->SiS_RefIndex[i].Ext_InfoFlag & 0x4000)
4777           current->Flags |= V_NHSYNC;
4778       else
4779           current->Flags |= V_PHSYNC;
4780
4781       if(pSiS->SiS_Pr->SiS_RefIndex[i].Ext_InfoFlag & 0x8000)
4782           current->Flags |= V_NVSYNC;
4783       else
4784           current->Flags |= V_PVSYNC;
4785
4786       if(pSiS->SiS_Pr->SiS_RefIndex[i].Ext_InfoFlag & 0x0080)
4787           current->Flags |= V_INTERLACE;
4788
4789       j = 0;
4790       while(pSiS->SiS_Pr->SiS_EModeIDTable[j].Ext_ModeID != 0xff) {
4791           if(pSiS->SiS_Pr->SiS_EModeIDTable[j].Ext_ModeID ==
4792                           pSiS->SiS_Pr->SiS_RefIndex[i].ModeID) {
4793               if(pSiS->SiS_Pr->SiS_EModeIDTable[j].Ext_ModeFlag & DoubleScanMode) {
4794                   current->Flags |= V_DBLSCAN;
4795               }
4796               break;
4797           }
4798           j++;
4799       }
4800
4801       if(current->Flags & V_INTERLACE) {
4802          current->VDisplay <<= 1;
4803          current->VSyncStart <<= 1;
4804          current->VSyncEnd <<= 1;
4805          current->VTotal <<= 1;
4806          current->VTotal |= 1;
4807       }
4808       if(current->Flags & V_DBLSCAN) {
4809          current->Clock >>= 1;
4810          current->VDisplay >>= 1;
4811          current->VSyncStart >>= 1;
4812          current->VSyncEnd >>= 1;
4813          current->VTotal >>= 1;
4814       }
4815
4816 #if 0
4817       if((backup = xalloc(sizeof(DisplayModeRec)))) {
4818          if(!pSiS->backupmodelist) pSiS->backupmodelist = backup;
4819          else {
4820             pSiS->backupmodelist->next = backup;
4821             backup->prev = pSiS->backupmodelist;
4822          }
4823          backup->next = NULL;
4824          backup->HDisplay = current->HDisplay;
4825          backup->HSyncStart = current->HSyncStart;
4826          backup->HSyncEnd = current->HSyncEnd;
4827          backup->HTotal = current->HTotal;
4828          backup->VDisplay = current->VDisplay;
4829          backup->VSyncStart = current->VSyncStart;
4830          backup->VSyncEnd = current->VSyncEnd;
4831          backup->VTotal = current->VTotal;
4832          backup->Flags = current->Flags;
4833          backup->Clock = current->Clock;
4834       }
4835 #endif
4836
4837 #ifdef TWDEBUG
4838       xf86DrvMsg(pScrn->scrnIndex, X_INFO,
4839         "Built-in: %s %.2f %d %d %d %d %d %d %d %d\n",
4840         current->name, (float)current->Clock / 1000,
4841         current->HDisplay, current->HSyncStart, current->HSyncEnd, current->HTotal,
4842         current->VDisplay, current->VSyncStart, current->VSyncEnd, current->VTotal);
4843 #else
4844         (void)VBS;  (void)HBS;  (void)A;
4845 #endif
4846
4847       i++;
4848    }
4849
4850    /* Add non-standard LCD modes for panel's detailed timings */
4851
4852    if(!includelcdmodes) return first;
4853
4854    if(pSiS->SiS_Pr->CP_Vendor) {
4855       xf86DrvMsg(0, X_INFO, "Checking database for vendor %x, product %x\n",
4856          pSiS->SiS_Pr->CP_Vendor, pSiS->SiS_Pr->CP_Product);
4857    }
4858
4859    i = 0;
4860    while((!done) && (SiS_PlasmaTable[i].vendor) && (pSiS->SiS_Pr->CP_Vendor)) {
4861
4862      if(SiS_PlasmaTable[i].vendor == pSiS->SiS_Pr->CP_Vendor) {
4863
4864         for(j=0; j<SiS_PlasmaTable[i].productnum; j++) {
4865
4866             if(SiS_PlasmaTable[i].product[j] == pSiS->SiS_Pr->CP_Product) {
4867
4868                xf86DrvMsg(pScrn->scrnIndex, X_PROBED,
4869                   "Identified %s panel, adding specific modes\n",
4870                   SiS_PlasmaTable[i].plasmaname);
4871
4872                for(k=0; k<SiS_PlasmaTable[i].modenum; k++) {
4873
4874                   if(isfordvi) {
4875                      if(!(SiS_PlasmaTable[i].plasmamodes[k] & 0x80)) continue;
4876                   } else {
4877                      if(!(SiS_PlasmaTable[i].plasmamodes[k] & 0x40)) continue;
4878                   }
4879
4880                   if(!(new = xalloc(sizeof(DisplayModeRec)))) return first;
4881
4882                   memset(new, 0, sizeof(DisplayModeRec));
4883                   if(!(new->name = xalloc(10))) {
4884                      xfree(new);
4885                      return first;
4886                   }
4887                   if(!first) first = new;
4888                   if(current) {
4889                      current->next = new;
4890                      new->prev = current;
4891                   }
4892
4893                   current = new;
4894
4895                   pSiS->AddedPlasmaModes = TRUE;
4896
4897                   l = SiS_PlasmaTable[i].plasmamodes[k] & 0x3f;
4898
4899                   sprintf(current->name, "%dx%d", SiS_PlasmaMode[l].HDisplay,
4900                                                   SiS_PlasmaMode[l].VDisplay);
4901
4902                   current->status = MODE_OK;
4903
4904                   current->type = M_T_BUILTIN;
4905
4906                   current->Clock = SiS_PlasmaMode[l].clock;
4907                   current->SynthClock = current->Clock;
4908
4909                   current->HDisplay   = SiS_PlasmaMode[l].HDisplay;
4910                   current->HSyncStart = current->HDisplay + SiS_PlasmaMode[l].HFrontPorch;
4911                   current->HSyncEnd   = current->HSyncStart + SiS_PlasmaMode[l].HSyncWidth;
4912                   current->HTotal     = SiS_PlasmaMode[l].HTotal;
4913
4914                   current->VDisplay   = SiS_PlasmaMode[l].VDisplay;
4915                   current->VSyncStart = current->VDisplay + SiS_PlasmaMode[l].VFrontPorch;
4916                   current->VSyncEnd   = current->VSyncStart + SiS_PlasmaMode[l].VSyncWidth;
4917                   current->VTotal     = SiS_PlasmaMode[l].VTotal;
4918
4919                   current->CrtcHDisplay = current->HDisplay;
4920                   current->CrtcHBlankStart = current->HSyncStart;
4921                   current->CrtcHSyncStart = current->HSyncStart;
4922                   current->CrtcHSyncEnd = current->HSyncEnd;
4923                   current->CrtcHBlankEnd = current->HSyncEnd;
4924                   current->CrtcHTotal = current->HTotal;
4925
4926                   current->CrtcVDisplay = current->VDisplay;
4927                   current->CrtcVBlankStart = current->VSyncStart;
4928                   current->CrtcVSyncStart = current->VSyncStart;
4929                   current->CrtcVSyncEnd = current->VSyncEnd;
4930                   current->CrtcVBlankEnd = current->VSyncEnd;
4931                   current->CrtcVTotal = current->VTotal;
4932
4933                   if(SiS_PlasmaMode[l].SyncFlags & SIS_PL_HSYNCP)
4934                      current->Flags |= V_PHSYNC;
4935                   else
4936                      current->Flags |= V_NHSYNC;
4937
4938                   if(SiS_PlasmaMode[l].SyncFlags & SIS_PL_VSYNCP)
4939                      current->Flags |= V_PVSYNC;
4940                   else
4941                      current->Flags |= V_NVSYNC;
4942
4943                   if(current->HDisplay > pSiS->LCDwidth)
4944                      pSiS->LCDwidth = pSiS->SiS_Pr->CP_MaxX = current->HDisplay;
4945                   if(current->VDisplay > pSiS->LCDheight)
4946                      pSiS->LCDheight = pSiS->SiS_Pr->CP_MaxY = current->VDisplay;
4947
4948                }
4949                done = TRUE;
4950                break;
4951             }
4952         }
4953      }
4954
4955      i++;
4956
4957    }
4958
4959    if(pSiS->SiS_Pr->CP_HaveCustomData) {
4960
4961       for(i=0; i<7; i++) {
4962
4963          if(pSiS->SiS_Pr->CP_DataValid[i]) {
4964
4965             if(!(new = xalloc(sizeof(DisplayModeRec)))) return first;
4966
4967             memset(new, 0, sizeof(DisplayModeRec));
4968             if(!(new->name = xalloc(10))) {
4969                 xfree(new);
4970                 return first;
4971             }
4972             if(!first) first = new;
4973             if(current) {
4974                current->next = new;
4975                new->prev = current;
4976             }
4977
4978             current = new;
4979
4980             sprintf(current->name, "%dx%d", pSiS->SiS_Pr->CP_HDisplay[i],
4981                                             pSiS->SiS_Pr->CP_VDisplay[i]);
4982
4983             current->status = MODE_OK;
4984
4985             current->type = M_T_BUILTIN;
4986
4987             current->Clock = pSiS->SiS_Pr->CP_Clock[i];
4988             current->SynthClock = current->Clock;
4989
4990             current->HDisplay   = pSiS->SiS_Pr->CP_HDisplay[i];
4991             current->HSyncStart = pSiS->SiS_Pr->CP_HSyncStart[i];
4992             current->HSyncEnd   = pSiS->SiS_Pr->CP_HSyncEnd[i];
4993             current->HTotal     = pSiS->SiS_Pr->CP_HTotal[i];
4994
4995             current->VDisplay   = pSiS->SiS_Pr->CP_VDisplay[i];
4996             current->VSyncStart = pSiS->SiS_Pr->CP_VSyncStart[i];
4997             current->VSyncEnd   = pSiS->SiS_Pr->CP_VSyncEnd[i];
4998             current->VTotal     = pSiS->SiS_Pr->CP_VTotal[i];
4999
5000             current->CrtcHDisplay = current->HDisplay;
5001             current->CrtcHBlankStart = pSiS->SiS_Pr->CP_HBlankStart[i];
5002             current->CrtcHSyncStart = current->HSyncStart;
5003             current->CrtcHSyncEnd = current->HSyncEnd;
5004             current->CrtcHBlankEnd = pSiS->SiS_Pr->CP_HBlankEnd[i];
5005             current->CrtcHTotal = current->HTotal;
5006
5007             current->CrtcVDisplay = current->VDisplay;
5008             current->CrtcVBlankStart = pSiS->SiS_Pr->CP_VBlankStart[i];
5009             current->CrtcVSyncStart = current->VSyncStart;
5010             current->CrtcVSyncEnd = current->VSyncEnd;
5011             current->CrtcVBlankEnd = pSiS->SiS_Pr->CP_VBlankEnd[i];
5012             current->CrtcVTotal = current->VTotal;
5013
5014             if(pSiS->SiS_Pr->CP_SyncValid[i]) {
5015                if(pSiS->SiS_Pr->CP_HSync_P[i])
5016                   current->Flags |= V_PHSYNC;
5017                else
5018                   current->Flags |= V_NHSYNC;
5019
5020                if(pSiS->SiS_Pr->CP_VSync_P[i])
5021                   current->Flags |= V_PVSYNC;
5022                else
5023                   current->Flags |= V_NVSYNC;
5024             } else {
5025                /* No sync data? Use positive sync... */
5026                current->Flags |= V_PHSYNC;
5027                current->Flags |= V_PVSYNC;
5028             }
5029          }
5030       }
5031    }
5032
5033    return first;
5034
5035 }
5036
5037 /* Build a list of supported modes */
5038 int
5039 SiSTranslateToVESA(ScrnInfoPtr pScrn, int modenumber)
5040 {
5041    SISPtr         pSiS = SISPTR(pScrn);
5042    int i;
5043
5044    /* Initialize our pointers */
5045    if(pSiS->VGAEngine == SIS_300_VGA) {
5046 #ifdef SIS300
5047         InitTo300Pointer(pSiS->SiS_Pr, &pSiS->sishw_ext);
5048 #else
5049         return -1;
5050 #endif
5051    } else if(pSiS->VGAEngine == SIS_315_VGA) {
5052 #ifdef SIS315H
5053         InitTo310Pointer(pSiS->SiS_Pr, &pSiS->sishw_ext);
5054 #else
5055         return -1;
5056 #endif
5057    } else return -1;
5058
5059    if(modenumber <= 0x13) return modenumber;
5060
5061    i = 0;
5062    while(pSiS->SiS_Pr->SiS_EModeIDTable[i].Ext_ModeID != 0xff) {
5063       if(pSiS->SiS_Pr->SiS_EModeIDTable[i].Ext_ModeID == modenumber) {
5064          return (int)pSiS->SiS_Pr->SiS_EModeIDTable[i].Ext_VESAID;
5065       }
5066       i++;
5067    }
5068    return -1;
5069 }
5070 #endif  /* Xfree86 */
5071
5072 #ifdef LINUX_KERNEL
5073 int
5074 sisfb_mode_rate_to_dclock(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,
5075                           unsigned char modeno, unsigned char rateindex)
5076 {
5077     USHORT ModeNo = modeno;
5078     USHORT ModeIdIndex = 0, ClockIndex = 0;
5079     USHORT RefreshRateTableIndex = 0;
5080     int    Clock;
5081
5082     if(HwInfo->jChipType < SIS_315H) {
5083 #ifdef SIS300
5084        InitTo300Pointer(SiS_Pr, HwInfo);
5085 #else
5086        return 65 * 1000;
5087 #endif
5088     } else {
5089 #ifdef SIS315H
5090        InitTo310Pointer(SiS_Pr, HwInfo);
5091 #else
5092        return 65 * 1000;
5093 #endif
5094     }
5095
5096     if(!(SiS_SearchModeID(SiS_Pr, &ModeNo, &ModeIdIndex))) {;
5097         printk(KERN_ERR "Could not find mode %x\n", ModeNo);
5098         return 65 * 1000;
5099     }
5100
5101     RefreshRateTableIndex = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].REFindex;
5102     RefreshRateTableIndex += (rateindex - 1);
5103     ClockIndex = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
5104     if(HwInfo->jChipType < SIS_315H) {
5105        ClockIndex &= 0x3F;
5106     }
5107     Clock = SiS_Pr->SiS_VCLKData[ClockIndex].CLOCK * 1000;
5108     
5109     return(Clock);
5110 }
5111
5112 BOOLEAN
5113 sisfb_gettotalfrommode(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,
5114                        unsigned char modeno, int *htotal, int *vtotal, unsigned char rateindex)
5115 {
5116     USHORT ModeNo = modeno;
5117     USHORT ModeIdIndex = 0, CRT1Index = 0;
5118     USHORT RefreshRateTableIndex = 0;
5119     unsigned char  sr_data, cr_data, cr_data2;
5120
5121     if(HwInfo->jChipType < SIS_315H) {
5122 #ifdef SIS300
5123        InitTo300Pointer(SiS_Pr, HwInfo);
5124 #else
5125        return FALSE;
5126 #endif
5127     } else {
5128 #ifdef SIS315H
5129        InitTo310Pointer(SiS_Pr, HwInfo);
5130 #else
5131        return FALSE;
5132 #endif
5133     }
5134
5135     if(!(SiS_SearchModeID(SiS_Pr, &ModeNo, &ModeIdIndex))) return FALSE;
5136
5137     RefreshRateTableIndex = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].REFindex;
5138     RefreshRateTableIndex += (rateindex - 1);
5139     CRT1Index = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
5140
5141     sr_data = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[14];
5142     cr_data = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[0];
5143     *htotal = (((cr_data & 0xff) | ((unsigned short) (sr_data & 0x03) << 8)) + 5) * 8;
5144
5145     sr_data = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[13];
5146     cr_data = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[6];
5147     cr_data2 = SiS_Pr->SiS_CRT1Table[CRT1Index].CR[7];
5148     *vtotal = ((cr_data & 0xFF) |
5149                ((unsigned short)(cr_data2 & 0x01) <<  8) |
5150                ((unsigned short)(cr_data2 & 0x20) <<  4) |
5151                ((unsigned short)(sr_data  & 0x01) << 10)) + 2;
5152
5153     if(SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_InfoFlag & InterlaceMode)
5154        *vtotal *= 2;
5155
5156     return TRUE;
5157 }
5158
5159 int
5160 sisfb_mode_rate_to_ddata(SiS_Private *SiS_Pr, PSIS_HW_INFO HwInfo,
5161                          unsigned char modeno, unsigned char rateindex,
5162                          ULONG *left_margin, ULONG *right_margin, 
5163                          ULONG *upper_margin, ULONG *lower_margin,
5164                          ULONG *hsync_len, ULONG *vsync_len,
5165                          ULONG *sync, ULONG *vmode)
5166 {
5167     USHORT ModeNo = modeno;
5168     USHORT ModeIdIndex = 0, index = 0;
5169     USHORT RefreshRateTableIndex = 0;
5170     unsigned short VRE, VBE, VRS, VBS, VDE, VT;
5171     unsigned short HRE, HBE, HRS, HBS, HDE, HT;
5172     unsigned char  sr_data, cr_data, cr_data2, cr_data3;
5173     int            A, B, C, D, E, F, temp, j;
5174    
5175     if(HwInfo->jChipType < SIS_315H) {
5176 #ifdef SIS300
5177        InitTo300Pointer(SiS_Pr, HwInfo);
5178 #else
5179        return 0;
5180 #endif
5181     } else {
5182 #ifdef SIS315H
5183        InitTo310Pointer(SiS_Pr, HwInfo);
5184 #else
5185        return 0;
5186 #endif
5187     }
5188     
5189     if(!(SiS_SearchModeID(SiS_Pr, &ModeNo, &ModeIdIndex))) return 0;
5190
5191     RefreshRateTableIndex = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].REFindex;
5192     RefreshRateTableIndex += (rateindex - 1);
5193     index = SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
5194
5195     sr_data = SiS_Pr->SiS_CRT1Table[index].CR[14];
5196
5197     cr_data = SiS_Pr->SiS_CRT1Table[index].CR[0];
5198
5199     /* Horizontal total */
5200     HT = (cr_data & 0xff) |
5201          ((unsigned short) (sr_data & 0x03) << 8);
5202     A = HT + 5;
5203
5204     cr_data = SiS_Pr->SiS_CRT1Table[index].CR[1];
5205         
5206     /* Horizontal display enable end */
5207     HDE = (cr_data & 0xff) |
5208           ((unsigned short) (sr_data & 0x0C) << 6);
5209     E = HDE + 1;
5210
5211     cr_data = SiS_Pr->SiS_CRT1Table[index].CR[4];
5212         
5213     /* Horizontal retrace (=sync) start */
5214     HRS = (cr_data & 0xff) |
5215           ((unsigned short) (sr_data & 0xC0) << 2);
5216     F = HRS - E - 3;
5217
5218     cr_data = SiS_Pr->SiS_CRT1Table[index].CR[2];
5219         
5220     /* Horizontal blank start */
5221     HBS = (cr_data & 0xff) |
5222           ((unsigned short) (sr_data & 0x30) << 4);
5223
5224     sr_data = SiS_Pr->SiS_CRT1Table[index].CR[15];
5225         
5226     cr_data = SiS_Pr->SiS_CRT1Table[index].CR[3];
5227
5228     cr_data2 = SiS_Pr->SiS_CRT1Table[index].CR[5];
5229         
5230     /* Horizontal blank end */
5231     HBE = (cr_data & 0x1f) |
5232           ((unsigned short) (cr_data2 & 0x80) >> 2) |
5233           ((unsigned short) (sr_data & 0x03) << 6);
5234
5235     /* Horizontal retrace (=sync) end */
5236     HRE = (cr_data2 & 0x1f) | ((sr_data & 0x04) << 3);
5237
5238     temp = HBE - ((E - 1) & 255);
5239     B = (temp > 0) ? temp : (temp + 256);
5240
5241     temp = HRE - ((E + F + 3) & 63);
5242     C = (temp > 0) ? temp : (temp + 64);
5243
5244     D = B - F - C;
5245
5246     if((SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].XRes == 320) &&
5247        ((SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].YRes == 200) ||
5248         (SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].YRes == 240))) {
5249
5250          /* Terrible hack, but the correct CRTC data for
5251           * these modes only produces a black screen...
5252           */
5253        *left_margin = (400 - 376);
5254        *right_margin = (328 - 320);
5255        *hsync_len = (376 - 328);
5256
5257     } else {
5258
5259        *left_margin = D * 8;
5260        *right_margin = F * 8;
5261        *hsync_len = C * 8;
5262
5263     }
5264
5265     sr_data = SiS_Pr->SiS_CRT1Table[index].CR[13];
5266
5267     cr_data = SiS_Pr->SiS_CRT1Table[index].CR[6];
5268
5269     cr_data2 = SiS_Pr->SiS_CRT1Table[index].CR[7];
5270
5271     /* Vertical total */
5272     VT = (cr_data & 0xFF) |
5273          ((unsigned short) (cr_data2 & 0x01) << 8) |
5274          ((unsigned short)(cr_data2 & 0x20) << 4) |
5275          ((unsigned short) (sr_data & 0x01) << 10);
5276     A = VT + 2;
5277
5278     cr_data = SiS_Pr->SiS_CRT1Table[index].CR[10];
5279         
5280     /* Vertical display enable end */
5281     VDE = (cr_data & 0xff) |
5282           ((unsigned short) (cr_data2 & 0x02) << 7) |
5283           ((unsigned short) (cr_data2 & 0x40) << 3) |
5284           ((unsigned short) (sr_data & 0x02) << 9);
5285     E = VDE + 1;
5286
5287     cr_data = SiS_Pr->SiS_CRT1Table[index].CR[8];
5288
5289     /* Vertical retrace (=sync) start */
5290     VRS = (cr_data & 0xff) |
5291           ((unsigned short) (cr_data2 & 0x04) << 6) |
5292           ((unsigned short) (cr_data2 & 0x80) << 2) |
5293           ((unsigned short) (sr_data & 0x08) << 7);
5294     F = VRS + 1 - E;
5295
5296     cr_data =  SiS_Pr->SiS_CRT1Table[index].CR[11];
5297
5298     cr_data3 = (SiS_Pr->SiS_CRT1Table[index].CR[16] & 0x01) << 5;
5299
5300     /* Vertical blank start */
5301     VBS = (cr_data & 0xff) |
5302           ((unsigned short) (cr_data2 & 0x08) << 5) |
5303           ((unsigned short) (cr_data3 & 0x20) << 4) |
5304           ((unsigned short) (sr_data & 0x04) << 8);
5305
5306     cr_data =  SiS_Pr->SiS_CRT1Table[index].CR[12];
5307
5308     /* Vertical blank end */
5309     VBE = (cr_data & 0xff) |
5310           ((unsigned short) (sr_data & 0x10) << 4);
5311     temp = VBE - ((E - 1) & 511);
5312     B = (temp > 0) ? temp : (temp + 512);
5313
5314     cr_data = SiS_Pr->SiS_CRT1Table[index].CR[9];
5315
5316     /* Vertical retrace (=sync) end */
5317     VRE = (cr_data & 0x0f) | ((sr_data & 0x20) >> 1);
5318     temp = VRE - ((E + F - 1) & 31);
5319     C = (temp > 0) ? temp : (temp + 32);
5320
5321     D = B - F - C;
5322       
5323     *upper_margin = D;
5324     *lower_margin = F;
5325     *vsync_len = C;
5326
5327     if(SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_InfoFlag & 0x8000)
5328        *sync &= ~FB_SYNC_VERT_HIGH_ACT;
5329     else
5330        *sync |= FB_SYNC_VERT_HIGH_ACT;
5331
5332     if(SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_InfoFlag & 0x4000)       
5333        *sync &= ~FB_SYNC_HOR_HIGH_ACT;
5334     else
5335        *sync |= FB_SYNC_HOR_HIGH_ACT;
5336                 
5337     *vmode = FB_VMODE_NONINTERLACED;
5338     if(SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].Ext_InfoFlag & 0x0080)
5339        *vmode = FB_VMODE_INTERLACED;
5340     else {
5341       j = 0;
5342       while(SiS_Pr->SiS_EModeIDTable[j].Ext_ModeID != 0xff) {
5343           if(SiS_Pr->SiS_EModeIDTable[j].Ext_ModeID ==
5344                           SiS_Pr->SiS_RefIndex[RefreshRateTableIndex].ModeID) {
5345               if(SiS_Pr->SiS_EModeIDTable[j].Ext_ModeFlag & DoubleScanMode) {
5346                   *vmode = FB_VMODE_DOUBLE;
5347               }
5348               break;
5349           }
5350           j++;
5351       }
5352     }       
5353
5354     if((*vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) {
5355 #if 0  /* Do this? */
5356        *upper_margin <<= 1;
5357        *lower_margin <<= 1;
5358        *vsync_len <<= 1;
5359 #endif
5360     } else if((*vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE) {
5361        *upper_margin >>= 1;
5362        *lower_margin >>= 1;
5363        *vsync_len >>= 1;
5364     }
5365
5366     return 1;       
5367 }                         
5368
5369 #endif
5370