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