Merge branch 'hwmon-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/groec...
[cascardo/linux.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,
6  *     SiS 315/550/[M]650/651/[M]661[FGM]X/[M]74x[GX]/330/[M]76x[GX],
7  *     XGI Volari V3XT/V5/V8, Z7
8  * (Universal module for Linux kernel framebuffer and X.org/XFree86 4.x)
9  *
10  * Copyright (C) 2001-2005 by Thomas Winischhofer, Vienna, Austria
11  *
12  * If distributed as part of the Linux kernel, the following license terms
13  * apply:
14  *
15  * * This program is free software; you can redistribute it and/or modify
16  * * it under the terms of the GNU General Public License as published by
17  * * the Free Software Foundation; either version 2 of the named License,
18  * * or any later version.
19  * *
20  * * This program is distributed in the hope that it will be useful,
21  * * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * * GNU General Public License for more details.
24  * *
25  * * You should have received a copy of the GNU General Public License
26  * * along with this program; if not, write to the Free Software
27  * * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
28  *
29  * Otherwise, the following license terms apply:
30  *
31  * * Redistribution and use in source and binary forms, with or without
32  * * modification, are permitted provided that the following conditions
33  * * are met:
34  * * 1) Redistributions of source code must retain the above copyright
35  * *    notice, this list of conditions and the following disclaimer.
36  * * 2) Redistributions in binary form must reproduce the above copyright
37  * *    notice, this list of conditions and the following disclaimer in the
38  * *    documentation and/or other materials provided with the distribution.
39  * * 3) The name of the author may not be used to endorse or promote products
40  * *    derived from this software without specific prior written permission.
41  * *
42  * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
43  * * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
44  * * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
45  * * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
46  * * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47  * * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
48  * * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
49  * * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
50  * * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
51  * * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52  *
53  * Author:      Thomas Winischhofer <thomas@winischhofer.net>
54  *
55  * Formerly based on non-functional code-fragements for 300 series by SiS, Inc.
56  * Used by permission.
57  */
58
59 #ifdef HAVE_CONFIG_H
60 #include "config.h"
61 #endif
62
63 #include "init.h"
64
65 #ifdef CONFIG_FB_SIS_300
66 #include "300vtbl.h"
67 #endif
68
69 #ifdef CONFIG_FB_SIS_315
70 #include "310vtbl.h"
71 #endif
72
73 #if defined(ALLOC_PRAGMA)
74 #pragma alloc_text(PAGE,SiSSetMode)
75 #endif
76
77 /*********************************************/
78 /*         POINTER INITIALIZATION            */
79 /*********************************************/
80
81 #if defined(CONFIG_FB_SIS_300) || defined(CONFIG_FB_SIS_315)
82 static void
83 InitCommonPointer(struct SiS_Private *SiS_Pr)
84 {
85    SiS_Pr->SiS_SModeIDTable  = SiS_SModeIDTable;
86    SiS_Pr->SiS_StResInfo     = SiS_StResInfo;
87    SiS_Pr->SiS_ModeResInfo   = SiS_ModeResInfo;
88    SiS_Pr->SiS_StandTable    = SiS_StandTable;
89
90    SiS_Pr->SiS_NTSCTiming     = SiS_NTSCTiming;
91    SiS_Pr->SiS_PALTiming      = SiS_PALTiming;
92    SiS_Pr->SiS_HiTVSt1Timing  = SiS_HiTVSt1Timing;
93    SiS_Pr->SiS_HiTVSt2Timing  = SiS_HiTVSt2Timing;
94
95    SiS_Pr->SiS_HiTVExtTiming  = SiS_HiTVExtTiming;
96    SiS_Pr->SiS_HiTVGroup3Data = SiS_HiTVGroup3Data;
97    SiS_Pr->SiS_HiTVGroup3Simu = SiS_HiTVGroup3Simu;
98 #if 0
99    SiS_Pr->SiS_HiTVTextTiming = SiS_HiTVTextTiming;
100    SiS_Pr->SiS_HiTVGroup3Text = SiS_HiTVGroup3Text;
101 #endif
102
103    SiS_Pr->SiS_StPALData   = SiS_StPALData;
104    SiS_Pr->SiS_ExtPALData  = SiS_ExtPALData;
105    SiS_Pr->SiS_StNTSCData  = SiS_StNTSCData;
106    SiS_Pr->SiS_ExtNTSCData = SiS_ExtNTSCData;
107    SiS_Pr->SiS_St1HiTVData = SiS_StHiTVData;
108    SiS_Pr->SiS_St2HiTVData = SiS_St2HiTVData;
109    SiS_Pr->SiS_ExtHiTVData = SiS_ExtHiTVData;
110    SiS_Pr->SiS_St525iData  = SiS_StNTSCData;
111    SiS_Pr->SiS_St525pData  = SiS_St525pData;
112    SiS_Pr->SiS_St750pData  = SiS_St750pData;
113    SiS_Pr->SiS_Ext525iData = SiS_ExtNTSCData;
114    SiS_Pr->SiS_Ext525pData = SiS_ExtNTSCData;
115    SiS_Pr->SiS_Ext750pData = SiS_Ext750pData;
116
117    SiS_Pr->pSiS_OutputSelect = &SiS_OutputSelect;
118    SiS_Pr->pSiS_SoftSetting  = &SiS_SoftSetting;
119
120    SiS_Pr->SiS_LCD1280x720Data      = SiS_LCD1280x720Data;
121    SiS_Pr->SiS_StLCD1280x768_2Data  = SiS_StLCD1280x768_2Data;
122    SiS_Pr->SiS_ExtLCD1280x768_2Data = SiS_ExtLCD1280x768_2Data;
123    SiS_Pr->SiS_LCD1280x800Data      = SiS_LCD1280x800Data;
124    SiS_Pr->SiS_LCD1280x800_2Data    = SiS_LCD1280x800_2Data;
125    SiS_Pr->SiS_LCD1280x854Data      = SiS_LCD1280x854Data;
126    SiS_Pr->SiS_LCD1280x960Data      = SiS_LCD1280x960Data;
127    SiS_Pr->SiS_StLCD1400x1050Data   = SiS_StLCD1400x1050Data;
128    SiS_Pr->SiS_ExtLCD1400x1050Data  = SiS_ExtLCD1400x1050Data;
129    SiS_Pr->SiS_LCD1680x1050Data     = SiS_LCD1680x1050Data;
130    SiS_Pr->SiS_StLCD1600x1200Data   = SiS_StLCD1600x1200Data;
131    SiS_Pr->SiS_ExtLCD1600x1200Data  = SiS_ExtLCD1600x1200Data;
132    SiS_Pr->SiS_NoScaleData          = SiS_NoScaleData;
133
134    SiS_Pr->SiS_LVDS320x240Data_1   = SiS_LVDS320x240Data_1;
135    SiS_Pr->SiS_LVDS320x240Data_2   = SiS_LVDS320x240Data_2;
136    SiS_Pr->SiS_LVDS640x480Data_1   = SiS_LVDS640x480Data_1;
137    SiS_Pr->SiS_LVDS800x600Data_1   = SiS_LVDS800x600Data_1;
138    SiS_Pr->SiS_LVDS1024x600Data_1  = SiS_LVDS1024x600Data_1;
139    SiS_Pr->SiS_LVDS1024x768Data_1  = SiS_LVDS1024x768Data_1;
140
141    SiS_Pr->SiS_LVDSCRT1320x240_1     = SiS_LVDSCRT1320x240_1;
142    SiS_Pr->SiS_LVDSCRT1320x240_2     = SiS_LVDSCRT1320x240_2;
143    SiS_Pr->SiS_LVDSCRT1320x240_2_H   = SiS_LVDSCRT1320x240_2_H;
144    SiS_Pr->SiS_LVDSCRT1320x240_3     = SiS_LVDSCRT1320x240_3;
145    SiS_Pr->SiS_LVDSCRT1320x240_3_H   = SiS_LVDSCRT1320x240_3_H;
146    SiS_Pr->SiS_LVDSCRT1640x480_1     = SiS_LVDSCRT1640x480_1;
147    SiS_Pr->SiS_LVDSCRT1640x480_1_H   = SiS_LVDSCRT1640x480_1_H;
148 #if 0
149    SiS_Pr->SiS_LVDSCRT11024x600_1    = SiS_LVDSCRT11024x600_1;
150    SiS_Pr->SiS_LVDSCRT11024x600_1_H  = SiS_LVDSCRT11024x600_1_H;
151    SiS_Pr->SiS_LVDSCRT11024x600_2    = SiS_LVDSCRT11024x600_2;
152    SiS_Pr->SiS_LVDSCRT11024x600_2_H  = SiS_LVDSCRT11024x600_2_H;
153 #endif
154
155    SiS_Pr->SiS_CHTVUNTSCData = SiS_CHTVUNTSCData;
156    SiS_Pr->SiS_CHTVONTSCData = SiS_CHTVONTSCData;
157
158    SiS_Pr->SiS_PanelMinLVDS   = Panel_800x600;    /* lowest value LVDS/LCDA */
159    SiS_Pr->SiS_PanelMin301    = Panel_1024x768;   /* lowest value 301 */
160 }
161 #endif
162
163 #ifdef CONFIG_FB_SIS_300
164 static void
165 InitTo300Pointer(struct SiS_Private *SiS_Pr)
166 {
167    InitCommonPointer(SiS_Pr);
168
169    SiS_Pr->SiS_VBModeIDTable = SiS300_VBModeIDTable;
170    SiS_Pr->SiS_EModeIDTable  = SiS300_EModeIDTable;
171    SiS_Pr->SiS_RefIndex      = SiS300_RefIndex;
172    SiS_Pr->SiS_CRT1Table     = SiS300_CRT1Table;
173    if(SiS_Pr->ChipType == SIS_300) {
174       SiS_Pr->SiS_MCLKData_0 = SiS300_MCLKData_300; /* 300 */
175    } else {
176       SiS_Pr->SiS_MCLKData_0 = SiS300_MCLKData_630; /* 630, 730 */
177    }
178    SiS_Pr->SiS_VCLKData      = SiS300_VCLKData;
179    SiS_Pr->SiS_VBVCLKData    = (struct SiS_VBVCLKData *)SiS300_VCLKData;
180
181    SiS_Pr->SiS_SR15  = SiS300_SR15;
182
183    SiS_Pr->SiS_PanelDelayTbl     = SiS300_PanelDelayTbl;
184    SiS_Pr->SiS_PanelDelayTblLVDS = SiS300_PanelDelayTbl;
185
186    SiS_Pr->SiS_ExtLCD1024x768Data   = SiS300_ExtLCD1024x768Data;
187    SiS_Pr->SiS_St2LCD1024x768Data   = SiS300_St2LCD1024x768Data;
188    SiS_Pr->SiS_ExtLCD1280x1024Data  = SiS300_ExtLCD1280x1024Data;
189    SiS_Pr->SiS_St2LCD1280x1024Data  = SiS300_St2LCD1280x1024Data;
190
191    SiS_Pr->SiS_CRT2Part2_1024x768_1  = SiS300_CRT2Part2_1024x768_1;
192    SiS_Pr->SiS_CRT2Part2_1024x768_2  = SiS300_CRT2Part2_1024x768_2;
193    SiS_Pr->SiS_CRT2Part2_1024x768_3  = SiS300_CRT2Part2_1024x768_3;
194
195    SiS_Pr->SiS_CHTVUPALData  = SiS300_CHTVUPALData;
196    SiS_Pr->SiS_CHTVOPALData  = SiS300_CHTVOPALData;
197    SiS_Pr->SiS_CHTVUPALMData = SiS_CHTVUNTSCData;    /* not supported on 300 series */
198    SiS_Pr->SiS_CHTVOPALMData = SiS_CHTVONTSCData;    /* not supported on 300 series */
199    SiS_Pr->SiS_CHTVUPALNData = SiS300_CHTVUPALData;  /* not supported on 300 series */
200    SiS_Pr->SiS_CHTVOPALNData = SiS300_CHTVOPALData;  /* not supported on 300 series */
201    SiS_Pr->SiS_CHTVSOPALData = SiS300_CHTVSOPALData;
202
203    SiS_Pr->SiS_LVDS848x480Data_1   = SiS300_LVDS848x480Data_1;
204    SiS_Pr->SiS_LVDS848x480Data_2   = SiS300_LVDS848x480Data_2;
205    SiS_Pr->SiS_LVDSBARCO1024Data_1 = SiS300_LVDSBARCO1024Data_1;
206    SiS_Pr->SiS_LVDSBARCO1366Data_1 = SiS300_LVDSBARCO1366Data_1;
207    SiS_Pr->SiS_LVDSBARCO1366Data_2 = SiS300_LVDSBARCO1366Data_2;
208
209    SiS_Pr->SiS_PanelType04_1a = SiS300_PanelType04_1a;
210    SiS_Pr->SiS_PanelType04_2a = SiS300_PanelType04_2a;
211    SiS_Pr->SiS_PanelType04_1b = SiS300_PanelType04_1b;
212    SiS_Pr->SiS_PanelType04_2b = SiS300_PanelType04_2b;
213
214    SiS_Pr->SiS_CHTVCRT1UNTSC = SiS300_CHTVCRT1UNTSC;
215    SiS_Pr->SiS_CHTVCRT1ONTSC = SiS300_CHTVCRT1ONTSC;
216    SiS_Pr->SiS_CHTVCRT1UPAL  = SiS300_CHTVCRT1UPAL;
217    SiS_Pr->SiS_CHTVCRT1OPAL  = SiS300_CHTVCRT1OPAL;
218    SiS_Pr->SiS_CHTVCRT1SOPAL = SiS300_CHTVCRT1SOPAL;
219    SiS_Pr->SiS_CHTVReg_UNTSC = SiS300_CHTVReg_UNTSC;
220    SiS_Pr->SiS_CHTVReg_ONTSC = SiS300_CHTVReg_ONTSC;
221    SiS_Pr->SiS_CHTVReg_UPAL  = SiS300_CHTVReg_UPAL;
222    SiS_Pr->SiS_CHTVReg_OPAL  = SiS300_CHTVReg_OPAL;
223    SiS_Pr->SiS_CHTVReg_UPALM = SiS300_CHTVReg_UNTSC;  /* not supported on 300 series */
224    SiS_Pr->SiS_CHTVReg_OPALM = SiS300_CHTVReg_ONTSC;  /* not supported on 300 series */
225    SiS_Pr->SiS_CHTVReg_UPALN = SiS300_CHTVReg_UPAL;   /* not supported on 300 series */
226    SiS_Pr->SiS_CHTVReg_OPALN = SiS300_CHTVReg_OPAL;   /* not supported on 300 series */
227    SiS_Pr->SiS_CHTVReg_SOPAL = SiS300_CHTVReg_SOPAL;
228    SiS_Pr->SiS_CHTVVCLKUNTSC = SiS300_CHTVVCLKUNTSC;
229    SiS_Pr->SiS_CHTVVCLKONTSC = SiS300_CHTVVCLKONTSC;
230    SiS_Pr->SiS_CHTVVCLKUPAL  = SiS300_CHTVVCLKUPAL;
231    SiS_Pr->SiS_CHTVVCLKOPAL  = SiS300_CHTVVCLKOPAL;
232    SiS_Pr->SiS_CHTVVCLKUPALM = SiS300_CHTVVCLKUNTSC;  /* not supported on 300 series */
233    SiS_Pr->SiS_CHTVVCLKOPALM = SiS300_CHTVVCLKONTSC;  /* not supported on 300 series */
234    SiS_Pr->SiS_CHTVVCLKUPALN = SiS300_CHTVVCLKUPAL;   /* not supported on 300 series */
235    SiS_Pr->SiS_CHTVVCLKOPALN = SiS300_CHTVVCLKOPAL;   /* not supported on 300 series */
236    SiS_Pr->SiS_CHTVVCLKSOPAL = SiS300_CHTVVCLKSOPAL;
237 }
238 #endif
239
240 #ifdef CONFIG_FB_SIS_315
241 static void
242 InitTo310Pointer(struct SiS_Private *SiS_Pr)
243 {
244    InitCommonPointer(SiS_Pr);
245
246    SiS_Pr->SiS_EModeIDTable  = SiS310_EModeIDTable;
247    SiS_Pr->SiS_RefIndex      = SiS310_RefIndex;
248    SiS_Pr->SiS_CRT1Table     = SiS310_CRT1Table;
249    if(SiS_Pr->ChipType >= SIS_340) {
250       SiS_Pr->SiS_MCLKData_0 = SiS310_MCLKData_0_340;  /* 340 + XGI */
251    } else if(SiS_Pr->ChipType >= SIS_761) {
252       SiS_Pr->SiS_MCLKData_0 = SiS310_MCLKData_0_761;  /* 761 - preliminary */
253    } else if(SiS_Pr->ChipType >= SIS_760) {
254       SiS_Pr->SiS_MCLKData_0 = SiS310_MCLKData_0_760;  /* 760 */
255    } else if(SiS_Pr->ChipType >= SIS_661) {
256       SiS_Pr->SiS_MCLKData_0 = SiS310_MCLKData_0_660;  /* 661/741 */
257    } else if(SiS_Pr->ChipType == SIS_330) {
258       SiS_Pr->SiS_MCLKData_0 = SiS310_MCLKData_0_330;  /* 330 */
259    } else if(SiS_Pr->ChipType > SIS_315PRO) {
260       SiS_Pr->SiS_MCLKData_0 = SiS310_MCLKData_0_650;  /* 550, 650, 740 */
261    } else {
262       SiS_Pr->SiS_MCLKData_0 = SiS310_MCLKData_0_315;  /* 315 */
263    }
264    if(SiS_Pr->ChipType >= SIS_340) {
265       SiS_Pr->SiS_MCLKData_1 = SiS310_MCLKData_1_340;
266    } else {
267       SiS_Pr->SiS_MCLKData_1 = SiS310_MCLKData_1;
268    }
269    SiS_Pr->SiS_VCLKData      = SiS310_VCLKData;
270    SiS_Pr->SiS_VBVCLKData    = SiS310_VBVCLKData;
271
272    SiS_Pr->SiS_SR15  = SiS310_SR15;
273
274    SiS_Pr->SiS_PanelDelayTbl     = SiS310_PanelDelayTbl;
275    SiS_Pr->SiS_PanelDelayTblLVDS = SiS310_PanelDelayTblLVDS;
276
277    SiS_Pr->SiS_St2LCD1024x768Data   = SiS310_St2LCD1024x768Data;
278    SiS_Pr->SiS_ExtLCD1024x768Data   = SiS310_ExtLCD1024x768Data;
279    SiS_Pr->SiS_St2LCD1280x1024Data  = SiS310_St2LCD1280x1024Data;
280    SiS_Pr->SiS_ExtLCD1280x1024Data  = SiS310_ExtLCD1280x1024Data;
281
282    SiS_Pr->SiS_CRT2Part2_1024x768_1  = SiS310_CRT2Part2_1024x768_1;
283
284    SiS_Pr->SiS_CHTVUPALData  = SiS310_CHTVUPALData;
285    SiS_Pr->SiS_CHTVOPALData  = SiS310_CHTVOPALData;
286    SiS_Pr->SiS_CHTVUPALMData = SiS310_CHTVUPALMData;
287    SiS_Pr->SiS_CHTVOPALMData = SiS310_CHTVOPALMData;
288    SiS_Pr->SiS_CHTVUPALNData = SiS310_CHTVUPALNData;
289    SiS_Pr->SiS_CHTVOPALNData = SiS310_CHTVOPALNData;
290    SiS_Pr->SiS_CHTVSOPALData = SiS310_CHTVSOPALData;
291
292    SiS_Pr->SiS_CHTVCRT1UNTSC = SiS310_CHTVCRT1UNTSC;
293    SiS_Pr->SiS_CHTVCRT1ONTSC = SiS310_CHTVCRT1ONTSC;
294    SiS_Pr->SiS_CHTVCRT1UPAL  = SiS310_CHTVCRT1UPAL;
295    SiS_Pr->SiS_CHTVCRT1OPAL  = SiS310_CHTVCRT1OPAL;
296    SiS_Pr->SiS_CHTVCRT1SOPAL = SiS310_CHTVCRT1OPAL;
297
298    SiS_Pr->SiS_CHTVReg_UNTSC = SiS310_CHTVReg_UNTSC;
299    SiS_Pr->SiS_CHTVReg_ONTSC = SiS310_CHTVReg_ONTSC;
300    SiS_Pr->SiS_CHTVReg_UPAL  = SiS310_CHTVReg_UPAL;
301    SiS_Pr->SiS_CHTVReg_OPAL  = SiS310_CHTVReg_OPAL;
302    SiS_Pr->SiS_CHTVReg_UPALM = SiS310_CHTVReg_UPALM;
303    SiS_Pr->SiS_CHTVReg_OPALM = SiS310_CHTVReg_OPALM;
304    SiS_Pr->SiS_CHTVReg_UPALN = SiS310_CHTVReg_UPALN;
305    SiS_Pr->SiS_CHTVReg_OPALN = SiS310_CHTVReg_OPALN;
306    SiS_Pr->SiS_CHTVReg_SOPAL = SiS310_CHTVReg_OPAL;
307
308    SiS_Pr->SiS_CHTVVCLKUNTSC = SiS310_CHTVVCLKUNTSC;
309    SiS_Pr->SiS_CHTVVCLKONTSC = SiS310_CHTVVCLKONTSC;
310    SiS_Pr->SiS_CHTVVCLKUPAL  = SiS310_CHTVVCLKUPAL;
311    SiS_Pr->SiS_CHTVVCLKOPAL  = SiS310_CHTVVCLKOPAL;
312    SiS_Pr->SiS_CHTVVCLKUPALM = SiS310_CHTVVCLKUPALM;
313    SiS_Pr->SiS_CHTVVCLKOPALM = SiS310_CHTVVCLKOPALM;
314    SiS_Pr->SiS_CHTVVCLKUPALN = SiS310_CHTVVCLKUPALN;
315    SiS_Pr->SiS_CHTVVCLKOPALN = SiS310_CHTVVCLKOPALN;
316    SiS_Pr->SiS_CHTVVCLKSOPAL = SiS310_CHTVVCLKOPAL;
317 }
318 #endif
319
320 bool
321 SiSInitPtr(struct SiS_Private *SiS_Pr)
322 {
323    if(SiS_Pr->ChipType < SIS_315H) {
324 #ifdef CONFIG_FB_SIS_300
325       InitTo300Pointer(SiS_Pr);
326 #else
327       return false;
328 #endif
329    } else {
330 #ifdef CONFIG_FB_SIS_315
331       InitTo310Pointer(SiS_Pr);
332 #else
333       return false;
334 #endif
335    }
336    return true;
337 }
338
339 /*********************************************/
340 /*            HELPER: Get ModeID             */
341 /*********************************************/
342
343 static
344 unsigned short
345 SiS_GetModeID(int VGAEngine, unsigned int VBFlags, int HDisplay, int VDisplay,
346                 int Depth, bool FSTN, int LCDwidth, int LCDheight)
347 {
348    unsigned short ModeIndex = 0;
349
350    switch(HDisplay)
351    {
352         case 320:
353                 if(VDisplay == 200) ModeIndex = ModeIndex_320x200[Depth];
354                 else if(VDisplay == 240) {
355                         if((VBFlags & CRT2_LCD) && (FSTN))
356                                 ModeIndex = ModeIndex_320x240_FSTN[Depth];
357                         else
358                                 ModeIndex = ModeIndex_320x240[Depth];
359                 }
360                 break;
361         case 400:
362                 if((!(VBFlags & CRT1_LCDA)) || ((LCDwidth >= 800) && (LCDwidth >= 600))) {
363                         if(VDisplay == 300) ModeIndex = ModeIndex_400x300[Depth];
364                 }
365                 break;
366         case 512:
367                 if((!(VBFlags & CRT1_LCDA)) || ((LCDwidth >= 1024) && (LCDwidth >= 768))) {
368                         if(VDisplay == 384) ModeIndex = ModeIndex_512x384[Depth];
369                 }
370                 break;
371         case 640:
372                 if(VDisplay == 480)      ModeIndex = ModeIndex_640x480[Depth];
373                 else if(VDisplay == 400) ModeIndex = ModeIndex_640x400[Depth];
374                 break;
375         case 720:
376                 if(VDisplay == 480)      ModeIndex = ModeIndex_720x480[Depth];
377                 else if(VDisplay == 576) ModeIndex = ModeIndex_720x576[Depth];
378                 break;
379         case 768:
380                 if(VDisplay == 576) ModeIndex = ModeIndex_768x576[Depth];
381                 break;
382         case 800:
383                 if(VDisplay == 600)      ModeIndex = ModeIndex_800x600[Depth];
384                 else if(VDisplay == 480) ModeIndex = ModeIndex_800x480[Depth];
385                 break;
386         case 848:
387                 if(VDisplay == 480) ModeIndex = ModeIndex_848x480[Depth];
388                 break;
389         case 856:
390                 if(VDisplay == 480) ModeIndex = ModeIndex_856x480[Depth];
391                 break;
392         case 960:
393                 if(VGAEngine == SIS_315_VGA) {
394                         if(VDisplay == 540)      ModeIndex = ModeIndex_960x540[Depth];
395                         else if(VDisplay == 600) ModeIndex = ModeIndex_960x600[Depth];
396                 }
397                 break;
398         case 1024:
399                 if(VDisplay == 576)      ModeIndex = ModeIndex_1024x576[Depth];
400                 else if(VDisplay == 768) ModeIndex = ModeIndex_1024x768[Depth];
401                 else if(VGAEngine == SIS_300_VGA) {
402                         if(VDisplay == 600) ModeIndex = ModeIndex_1024x600[Depth];
403                 }
404                 break;
405         case 1152:
406                 if(VDisplay == 864) ModeIndex = ModeIndex_1152x864[Depth];
407                 if(VGAEngine == SIS_300_VGA) {
408                         if(VDisplay == 768) ModeIndex = ModeIndex_1152x768[Depth];
409                 }
410                 break;
411         case 1280:
412                 switch(VDisplay) {
413                         case 720:
414                                 ModeIndex = ModeIndex_1280x720[Depth];
415                                 break;
416                         case 768:
417                                 if(VGAEngine == SIS_300_VGA) {
418                                         ModeIndex = ModeIndex_300_1280x768[Depth];
419                                 } else {
420                                         ModeIndex = ModeIndex_310_1280x768[Depth];
421                                 }
422                                 break;
423                         case 800:
424                                 if(VGAEngine == SIS_315_VGA) {
425                                         ModeIndex = ModeIndex_1280x800[Depth];
426                                 }
427                                 break;
428                         case 854:
429                                 if(VGAEngine == SIS_315_VGA) {
430                                         ModeIndex = ModeIndex_1280x854[Depth];
431                                 }
432                                 break;
433                         case 960:
434                                 ModeIndex = ModeIndex_1280x960[Depth];
435                                 break;
436                         case 1024:
437                                 ModeIndex = ModeIndex_1280x1024[Depth];
438                                 break;
439                 }
440                 break;
441         case 1360:
442                 if(VDisplay == 768) ModeIndex = ModeIndex_1360x768[Depth];
443                 if(VGAEngine == SIS_300_VGA) {
444                         if(VDisplay == 1024) ModeIndex = ModeIndex_300_1360x1024[Depth];
445                 }
446                 break;
447         case 1400:
448                 if(VGAEngine == SIS_315_VGA) {
449                         if(VDisplay == 1050) {
450                                 ModeIndex = ModeIndex_1400x1050[Depth];
451                         }
452                 }
453                 break;
454         case 1600:
455                 if(VDisplay == 1200) ModeIndex = ModeIndex_1600x1200[Depth];
456                 break;
457         case 1680:
458                 if(VGAEngine == SIS_315_VGA) {
459                         if(VDisplay == 1050) ModeIndex = ModeIndex_1680x1050[Depth];
460                 }
461                 break;
462         case 1920:
463                 if(VDisplay == 1440) ModeIndex = ModeIndex_1920x1440[Depth];
464                 else if(VGAEngine == SIS_315_VGA) {
465                         if(VDisplay == 1080) ModeIndex = ModeIndex_1920x1080[Depth];
466                 }
467                 break;
468         case 2048:
469                 if(VDisplay == 1536) {
470                         if(VGAEngine == SIS_300_VGA) {
471                                 ModeIndex = ModeIndex_300_2048x1536[Depth];
472                         } else {
473                                 ModeIndex = ModeIndex_310_2048x1536[Depth];
474                         }
475                 }
476                 break;
477    }
478
479    return ModeIndex;
480 }
481
482 unsigned short
483 SiS_GetModeID_LCD(int VGAEngine, unsigned int VBFlags, int HDisplay, int VDisplay,
484                 int Depth, bool FSTN, unsigned short CustomT, int LCDwidth, int LCDheight,
485                 unsigned int VBFlags2)
486 {
487    unsigned short ModeIndex = 0;
488
489    if(VBFlags2 & (VB2_LVDS | VB2_30xBDH)) {
490
491       switch(HDisplay)
492       {
493         case 320:
494              if((CustomT != CUT_PANEL848) && (CustomT != CUT_PANEL856)) {
495                 if(VDisplay == 200) {
496                    if(!FSTN) ModeIndex = ModeIndex_320x200[Depth];
497                 } else if(VDisplay == 240) {
498                    if(!FSTN) ModeIndex = ModeIndex_320x240[Depth];
499                    else if(VGAEngine == SIS_315_VGA) {
500                       ModeIndex = ModeIndex_320x240_FSTN[Depth];
501                    }
502                 }
503              }
504              break;
505         case 400:
506              if((CustomT != CUT_PANEL848) && (CustomT != CUT_PANEL856)) {
507                 if(!((VGAEngine == SIS_300_VGA) && (VBFlags2 & VB2_TRUMPION))) {
508                    if(VDisplay == 300) ModeIndex = ModeIndex_400x300[Depth];
509                 }
510              }
511              break;
512         case 512:
513              if((CustomT != CUT_PANEL848) && (CustomT != CUT_PANEL856)) {
514                 if(!((VGAEngine == SIS_300_VGA) && (VBFlags2 & VB2_TRUMPION))) {
515                    if(LCDwidth >= 1024 && LCDwidth != 1152 && LCDheight >= 768) {
516                       if(VDisplay == 384) {
517                          ModeIndex = ModeIndex_512x384[Depth];
518                       }
519                    }
520                 }
521              }
522              break;
523         case 640:
524              if(VDisplay == 480) ModeIndex = ModeIndex_640x480[Depth];
525              else if(VDisplay == 400) {
526                 if((CustomT != CUT_PANEL848) && (CustomT != CUT_PANEL856))
527                    ModeIndex = ModeIndex_640x400[Depth];
528              }
529              break;
530         case 800:
531              if(VDisplay == 600) ModeIndex = ModeIndex_800x600[Depth];
532              break;
533         case 848:
534              if(CustomT == CUT_PANEL848) {
535                 if(VDisplay == 480) ModeIndex = ModeIndex_848x480[Depth];
536              }
537              break;
538         case 856:
539              if(CustomT == CUT_PANEL856) {
540                 if(VDisplay == 480) ModeIndex = ModeIndex_856x480[Depth];
541              }
542              break;
543         case 1024:
544              if(VDisplay == 768) ModeIndex = ModeIndex_1024x768[Depth];
545              else if(VGAEngine == SIS_300_VGA) {
546                 if((VDisplay == 600) && (LCDheight == 600)) {
547                    ModeIndex = ModeIndex_1024x600[Depth];
548                 }
549              }
550              break;
551         case 1152:
552              if(VGAEngine == SIS_300_VGA) {
553                 if((VDisplay == 768) && (LCDheight == 768)) {
554                    ModeIndex = ModeIndex_1152x768[Depth];
555                 }
556              }
557              break;
558         case 1280:
559              if(VDisplay == 1024) ModeIndex = ModeIndex_1280x1024[Depth];
560              else if(VGAEngine == SIS_315_VGA) {
561                 if((VDisplay == 768) && (LCDheight == 768)) {
562                    ModeIndex = ModeIndex_310_1280x768[Depth];
563                 }
564              }
565              break;
566         case 1360:
567              if(VGAEngine == SIS_300_VGA) {
568                 if(CustomT == CUT_BARCO1366) {
569                    if(VDisplay == 1024) ModeIndex = ModeIndex_300_1360x1024[Depth];
570                 }
571              }
572              if(CustomT == CUT_PANEL848) {
573                 if(VDisplay == 768) ModeIndex = ModeIndex_1360x768[Depth];
574              }
575              break;
576         case 1400:
577              if(VGAEngine == SIS_315_VGA) {
578                 if(VDisplay == 1050) ModeIndex = ModeIndex_1400x1050[Depth];
579              }
580              break;
581         case 1600:
582              if(VGAEngine == SIS_315_VGA) {
583                 if(VDisplay == 1200) ModeIndex = ModeIndex_1600x1200[Depth];
584              }
585              break;
586       }
587
588    } else if(VBFlags2 & VB2_SISBRIDGE) {
589
590       switch(HDisplay)
591       {
592         case 320:
593              if(VDisplay == 200)      ModeIndex = ModeIndex_320x200[Depth];
594              else if(VDisplay == 240) ModeIndex = ModeIndex_320x240[Depth];
595              break;
596         case 400:
597              if(LCDwidth >= 800 && LCDheight >= 600) {
598                 if(VDisplay == 300) ModeIndex = ModeIndex_400x300[Depth];
599              }
600              break;
601         case 512:
602              if(LCDwidth >= 1024 && LCDheight >= 768 && LCDwidth != 1152) {
603                 if(VDisplay == 384) ModeIndex = ModeIndex_512x384[Depth];
604              }
605              break;
606         case 640:
607              if(VDisplay == 480)      ModeIndex = ModeIndex_640x480[Depth];
608              else if(VDisplay == 400) ModeIndex = ModeIndex_640x400[Depth];
609              break;
610         case 720:
611              if(VGAEngine == SIS_315_VGA) {
612                 if(VDisplay == 480)      ModeIndex = ModeIndex_720x480[Depth];
613                 else if(VDisplay == 576) ModeIndex = ModeIndex_720x576[Depth];
614              }
615              break;
616         case 768:
617              if(VGAEngine == SIS_315_VGA) {
618                 if(VDisplay == 576) ModeIndex = ModeIndex_768x576[Depth];
619              }
620              break;
621         case 800:
622              if(VDisplay == 600) ModeIndex = ModeIndex_800x600[Depth];
623              if(VGAEngine == SIS_315_VGA) {
624                 if(VDisplay == 480) ModeIndex = ModeIndex_800x480[Depth];
625              }
626              break;
627         case 848:
628              if(VGAEngine == SIS_315_VGA) {
629                 if(VDisplay == 480) ModeIndex = ModeIndex_848x480[Depth];
630              }
631              break;
632         case 856:
633              if(VGAEngine == SIS_315_VGA) {
634                 if(VDisplay == 480) ModeIndex = ModeIndex_856x480[Depth];
635              }
636              break;
637         case 960:
638              if(VGAEngine == SIS_315_VGA) {
639                 if(VDisplay == 540)      ModeIndex = ModeIndex_960x540[Depth];
640                 else if(VDisplay == 600) ModeIndex = ModeIndex_960x600[Depth];
641              }
642              break;
643         case 1024:
644              if(VDisplay == 768) ModeIndex = ModeIndex_1024x768[Depth];
645              if(VGAEngine == SIS_315_VGA) {
646                 if(VDisplay == 576) ModeIndex = ModeIndex_1024x576[Depth];
647              }
648              break;
649         case 1152:
650              if(VGAEngine == SIS_315_VGA) {
651                 if(VDisplay == 864) ModeIndex = ModeIndex_1152x864[Depth];
652              }
653              break;
654         case 1280:
655              switch(VDisplay) {
656              case 720:
657                 ModeIndex = ModeIndex_1280x720[Depth];
658              case 768:
659                 if(VGAEngine == SIS_300_VGA) {
660                    ModeIndex = ModeIndex_300_1280x768[Depth];
661                 } else {
662                    ModeIndex = ModeIndex_310_1280x768[Depth];
663                 }
664                 break;
665              case 800:
666                 if(VGAEngine == SIS_315_VGA) {
667                    ModeIndex = ModeIndex_1280x800[Depth];
668                 }
669                 break;
670              case 854:
671                 if(VGAEngine == SIS_315_VGA) {
672                    ModeIndex = ModeIndex_1280x854[Depth];
673                 }
674                 break;
675              case 960:
676                 ModeIndex = ModeIndex_1280x960[Depth];
677                 break;
678              case 1024:
679                 ModeIndex = ModeIndex_1280x1024[Depth];
680                 break;
681              }
682              break;
683         case 1360:
684              if(VGAEngine == SIS_315_VGA) {  /* OVER1280 only? */
685                 if(VDisplay == 768) ModeIndex = ModeIndex_1360x768[Depth];
686              }
687              break;
688         case 1400:
689              if(VGAEngine == SIS_315_VGA) {
690                 if(VBFlags2 & VB2_LCDOVER1280BRIDGE) {
691                    if(VDisplay == 1050) ModeIndex = ModeIndex_1400x1050[Depth];
692                 }
693              }
694              break;
695         case 1600:
696              if(VGAEngine == SIS_315_VGA) {
697                 if(VBFlags2 & VB2_LCDOVER1280BRIDGE) {
698                    if(VDisplay == 1200) ModeIndex = ModeIndex_1600x1200[Depth];
699                 }
700              }
701              break;
702 #ifndef VB_FORBID_CRT2LCD_OVER_1600
703         case 1680:
704              if(VGAEngine == SIS_315_VGA) {
705                 if(VBFlags2 & VB2_LCDOVER1280BRIDGE) {
706                    if(VDisplay == 1050) ModeIndex = ModeIndex_1680x1050[Depth];
707                 }
708              }
709              break;
710         case 1920:
711              if(VGAEngine == SIS_315_VGA) {
712                 if(VBFlags2 & VB2_LCDOVER1600BRIDGE) {
713                    if(VDisplay == 1440) ModeIndex = ModeIndex_1920x1440[Depth];
714                 }
715              }
716              break;
717         case 2048:
718              if(VGAEngine == SIS_315_VGA) {
719                 if(VBFlags2 & VB2_LCDOVER1600BRIDGE) {
720                    if(VDisplay == 1536) ModeIndex = ModeIndex_310_2048x1536[Depth];
721                 }
722              }
723              break;
724 #endif
725       }
726    }
727
728    return ModeIndex;
729 }
730
731 unsigned short
732 SiS_GetModeID_TV(int VGAEngine, unsigned int VBFlags, int HDisplay, int VDisplay, int Depth,
733                         unsigned int VBFlags2)
734 {
735    unsigned short ModeIndex = 0;
736
737    if(VBFlags2 & VB2_CHRONTEL) {
738
739       switch(HDisplay)
740       {
741         case 512:
742              if(VGAEngine == SIS_315_VGA) {
743                 if(VDisplay == 384) ModeIndex = ModeIndex_512x384[Depth];
744              }
745              break;
746         case 640:
747              if(VDisplay == 480)      ModeIndex = ModeIndex_640x480[Depth];
748              else if(VDisplay == 400) ModeIndex = ModeIndex_640x400[Depth];
749              break;
750         case 800:
751              if(VDisplay == 600) ModeIndex = ModeIndex_800x600[Depth];
752              break;
753         case 1024:
754              if(VGAEngine == SIS_315_VGA) {
755                 if(VDisplay == 768) ModeIndex = ModeIndex_1024x768[Depth];
756              }
757              break;
758       }
759
760    } else if(VBFlags2 & VB2_SISTVBRIDGE) {
761
762       switch(HDisplay)
763       {
764         case 320:
765              if(VDisplay == 200)      ModeIndex = ModeIndex_320x200[Depth];
766              else if(VDisplay == 240) ModeIndex = ModeIndex_320x240[Depth];
767              break;
768         case 400:
769              if(VDisplay == 300) ModeIndex = ModeIndex_400x300[Depth];
770              break;
771         case 512:
772              if( ((VBFlags & TV_YPBPR) && (VBFlags & (TV_YPBPR750P | TV_YPBPR1080I))) ||
773                  (VBFlags & TV_HIVISION)                                              ||
774                  ((!(VBFlags & (TV_YPBPR | TV_PALM))) && (VBFlags & TV_PAL)) ) {
775                 if(VDisplay == 384) ModeIndex = ModeIndex_512x384[Depth];
776              }
777              break;
778         case 640:
779              if(VDisplay == 480)      ModeIndex = ModeIndex_640x480[Depth];
780              else if(VDisplay == 400) ModeIndex = ModeIndex_640x400[Depth];
781              break;
782         case 720:
783              if((!(VBFlags & TV_HIVISION)) && (!((VBFlags & TV_YPBPR) && (VBFlags & TV_YPBPR1080I)))) {
784                 if(VDisplay == 480) {
785                    ModeIndex = ModeIndex_720x480[Depth];
786                 } else if(VDisplay == 576) {
787                    if( ((VBFlags & TV_YPBPR) && (VBFlags & TV_YPBPR750P)) ||
788                        ((!(VBFlags & (TV_YPBPR | TV_PALM))) && (VBFlags & TV_PAL)) )
789                       ModeIndex = ModeIndex_720x576[Depth];
790                 }
791              }
792              break;
793         case 768:
794              if((!(VBFlags & TV_HIVISION)) && (!((VBFlags & TV_YPBPR) && (VBFlags & TV_YPBPR1080I)))) {
795                 if( ((VBFlags & TV_YPBPR) && (VBFlags & TV_YPBPR750P)) ||
796                     ((!(VBFlags & (TV_YPBPR | TV_PALM))) && (VBFlags & TV_PAL)) ) {
797                    if(VDisplay == 576) ModeIndex = ModeIndex_768x576[Depth];
798                 }
799              }
800              break;
801         case 800:
802              if(VDisplay == 600) ModeIndex = ModeIndex_800x600[Depth];
803              else if(VDisplay == 480) {
804                 if(!((VBFlags & TV_YPBPR) && (VBFlags & TV_YPBPR750P))) {
805                    ModeIndex = ModeIndex_800x480[Depth];
806                 }
807              }
808              break;
809         case 960:
810              if(VGAEngine == SIS_315_VGA) {
811                 if(VDisplay == 600) {
812                    if((VBFlags & TV_HIVISION) || ((VBFlags & TV_YPBPR) && (VBFlags & TV_YPBPR1080I))) {
813                       ModeIndex = ModeIndex_960x600[Depth];
814                    }
815                 }
816              }
817              break;
818         case 1024:
819              if(VDisplay == 768) {
820                 if(VBFlags2 & VB2_30xBLV) {
821                    ModeIndex = ModeIndex_1024x768[Depth];
822                 }
823              } else if(VDisplay == 576) {
824                 if( (VBFlags & TV_HIVISION) ||
825                     ((VBFlags & TV_YPBPR) && (VBFlags & TV_YPBPR1080I)) ||
826                     ((VBFlags2 & VB2_30xBLV) &&
827                      ((!(VBFlags & (TV_YPBPR | TV_PALM))) && (VBFlags & TV_PAL))) ) {
828                    ModeIndex = ModeIndex_1024x576[Depth];
829                 }
830              }
831              break;
832         case 1280:
833              if(VDisplay == 720) {
834                 if((VBFlags & TV_HIVISION) ||
835                    ((VBFlags & TV_YPBPR) && (VBFlags & (TV_YPBPR1080I | TV_YPBPR750P)))) {
836                    ModeIndex = ModeIndex_1280x720[Depth];
837                 }
838              } else if(VDisplay == 1024) {
839                 if((VBFlags & TV_HIVISION) ||
840                    ((VBFlags & TV_YPBPR) && (VBFlags & TV_YPBPR1080I))) {
841                    ModeIndex = ModeIndex_1280x1024[Depth];
842                 }
843              }
844              break;
845       }
846    }
847    return ModeIndex;
848 }
849
850 unsigned short
851 SiS_GetModeID_VGA2(int VGAEngine, unsigned int VBFlags, int HDisplay, int VDisplay, int Depth,
852                         unsigned int VBFlags2)
853 {
854    if(!(VBFlags2 & VB2_SISVGA2BRIDGE)) return 0;
855
856    if(HDisplay >= 1920) return 0;
857
858    switch(HDisplay)
859    {
860         case 1600:
861                 if(VDisplay == 1200) {
862                         if(VGAEngine != SIS_315_VGA) return 0;
863                         if(!(VBFlags2 & VB2_30xB)) return 0;
864                 }
865                 break;
866         case 1680:
867                 if(VDisplay == 1050) {
868                         if(VGAEngine != SIS_315_VGA) return 0;
869                         if(!(VBFlags2 & VB2_30xB)) return 0;
870                 }
871                 break;
872    }
873
874    return SiS_GetModeID(VGAEngine, 0, HDisplay, VDisplay, Depth, false, 0, 0);
875 }
876
877
878 /*********************************************/
879 /*          HELPER: SetReg, GetReg           */
880 /*********************************************/
881
882 void
883 SiS_SetReg(SISIOADDRESS port, unsigned short index, unsigned short data)
884 {
885         outb((u8)index, port);
886         outb((u8)data, port + 1);
887 }
888
889 void
890 SiS_SetRegByte(SISIOADDRESS port, unsigned short data)
891 {
892         outb((u8)data, port);
893 }
894
895 void
896 SiS_SetRegShort(SISIOADDRESS port, unsigned short data)
897 {
898         outw((u16)data, port);
899 }
900
901 void
902 SiS_SetRegLong(SISIOADDRESS port, unsigned int data)
903 {
904         outl((u32)data, port);
905 }
906
907 unsigned char
908 SiS_GetReg(SISIOADDRESS port, unsigned short index)
909 {
910         outb((u8)index, port);
911         return inb(port + 1);
912 }
913
914 unsigned char
915 SiS_GetRegByte(SISIOADDRESS port)
916 {
917         return inb(port);
918 }
919
920 unsigned short
921 SiS_GetRegShort(SISIOADDRESS port)
922 {
923         return inw(port);
924 }
925
926 unsigned int
927 SiS_GetRegLong(SISIOADDRESS port)
928 {
929         return inl(port);
930 }
931
932 void
933 SiS_SetRegANDOR(SISIOADDRESS Port, unsigned short Index, unsigned short DataAND, unsigned short DataOR)
934 {
935    unsigned short temp;
936
937    temp = SiS_GetReg(Port, Index);
938    temp = (temp & (DataAND)) | DataOR;
939    SiS_SetReg(Port, Index, temp);
940 }
941
942 void
943 SiS_SetRegAND(SISIOADDRESS Port, unsigned short Index, unsigned short DataAND)
944 {
945    unsigned short temp;
946
947    temp = SiS_GetReg(Port, Index);
948    temp &= DataAND;
949    SiS_SetReg(Port, Index, temp);
950 }
951
952 void
953 SiS_SetRegOR(SISIOADDRESS Port, unsigned short Index, unsigned short DataOR)
954 {
955    unsigned short temp;
956
957    temp = SiS_GetReg(Port, Index);
958    temp |= DataOR;
959    SiS_SetReg(Port, Index, temp);
960 }
961
962 /*********************************************/
963 /*      HELPER: DisplayOn, DisplayOff        */
964 /*********************************************/
965
966 void
967 SiS_DisplayOn(struct SiS_Private *SiS_Pr)
968 {
969    SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x01,0xDF);
970 }
971
972 void
973 SiS_DisplayOff(struct SiS_Private *SiS_Pr)
974 {
975    SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x01,0x20);
976 }
977
978
979 /*********************************************/
980 /*        HELPER: Init Port Addresses        */
981 /*********************************************/
982
983 void
984 SiSRegInit(struct SiS_Private *SiS_Pr, SISIOADDRESS BaseAddr)
985 {
986    SiS_Pr->SiS_P3c4 = BaseAddr + 0x14;
987    SiS_Pr->SiS_P3d4 = BaseAddr + 0x24;
988    SiS_Pr->SiS_P3c0 = BaseAddr + 0x10;
989    SiS_Pr->SiS_P3ce = BaseAddr + 0x1e;
990    SiS_Pr->SiS_P3c2 = BaseAddr + 0x12;
991    SiS_Pr->SiS_P3ca = BaseAddr + 0x1a;
992    SiS_Pr->SiS_P3c6 = BaseAddr + 0x16;
993    SiS_Pr->SiS_P3c7 = BaseAddr + 0x17;
994    SiS_Pr->SiS_P3c8 = BaseAddr + 0x18;
995    SiS_Pr->SiS_P3c9 = BaseAddr + 0x19;
996    SiS_Pr->SiS_P3cb = BaseAddr + 0x1b;
997    SiS_Pr->SiS_P3cc = BaseAddr + 0x1c;
998    SiS_Pr->SiS_P3cd = BaseAddr + 0x1d;
999    SiS_Pr->SiS_P3da = BaseAddr + 0x2a;
1000    SiS_Pr->SiS_Part1Port = BaseAddr + SIS_CRT2_PORT_04;
1001    SiS_Pr->SiS_Part2Port = BaseAddr + SIS_CRT2_PORT_10;
1002    SiS_Pr->SiS_Part3Port = BaseAddr + SIS_CRT2_PORT_12;
1003    SiS_Pr->SiS_Part4Port = BaseAddr + SIS_CRT2_PORT_14;
1004    SiS_Pr->SiS_Part5Port = BaseAddr + SIS_CRT2_PORT_14 + 2;
1005    SiS_Pr->SiS_DDC_Port  = BaseAddr + 0x14;
1006    SiS_Pr->SiS_VidCapt   = BaseAddr + SIS_VIDEO_CAPTURE;
1007    SiS_Pr->SiS_VidPlay   = BaseAddr + SIS_VIDEO_PLAYBACK;
1008 }
1009
1010 /*********************************************/
1011 /*             HELPER: GetSysFlags           */
1012 /*********************************************/
1013
1014 static void
1015 SiS_GetSysFlags(struct SiS_Private *SiS_Pr)
1016 {
1017    unsigned char cr5f, temp1, temp2;
1018
1019    /* 661 and newer: NEVER write non-zero to SR11[7:4] */
1020    /* (SR11 is used for DDC and in enable/disablebridge) */
1021    SiS_Pr->SiS_SensibleSR11 = false;
1022    SiS_Pr->SiS_MyCR63 = 0x63;
1023    if(SiS_Pr->ChipType >= SIS_330) {
1024       SiS_Pr->SiS_MyCR63 = 0x53;
1025       if(SiS_Pr->ChipType >= SIS_661) {
1026          SiS_Pr->SiS_SensibleSR11 = true;
1027       }
1028    }
1029
1030    /* You should use the macros, not these flags directly */
1031
1032    SiS_Pr->SiS_SysFlags = 0;
1033    if(SiS_Pr->ChipType == SIS_650) {
1034       cr5f = SiS_GetReg(SiS_Pr->SiS_P3d4,0x5f) & 0xf0;
1035       SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x5c,0x07);
1036       temp1 = SiS_GetReg(SiS_Pr->SiS_P3d4,0x5c) & 0xf8;
1037       SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x5c,0xf8);
1038       temp2 = SiS_GetReg(SiS_Pr->SiS_P3d4,0x5c) & 0xf8;
1039       if((!temp1) || (temp2)) {
1040          switch(cr5f) {
1041             case 0x80:
1042             case 0x90:
1043             case 0xc0:
1044                SiS_Pr->SiS_SysFlags |= SF_IsM650;
1045                break;
1046             case 0xa0:
1047             case 0xb0:
1048             case 0xe0:
1049                SiS_Pr->SiS_SysFlags |= SF_Is651;
1050                break;
1051          }
1052       } else {
1053          switch(cr5f) {
1054             case 0x90:
1055                temp1 = SiS_GetReg(SiS_Pr->SiS_P3d4,0x5c) & 0xf8;
1056                switch(temp1) {
1057                   case 0x00: SiS_Pr->SiS_SysFlags |= SF_IsM652; break;
1058                   case 0x40: SiS_Pr->SiS_SysFlags |= SF_IsM653; break;
1059                   default:   SiS_Pr->SiS_SysFlags |= SF_IsM650; break;
1060                }
1061                break;
1062             case 0xb0:
1063                SiS_Pr->SiS_SysFlags |= SF_Is652;
1064                break;
1065             default:
1066                SiS_Pr->SiS_SysFlags |= SF_IsM650;
1067                break;
1068          }
1069       }
1070    }
1071
1072    if(SiS_Pr->ChipType >= SIS_760 && SiS_Pr->ChipType <= SIS_761) {
1073       if(SiS_GetReg(SiS_Pr->SiS_P3d4,0x78) & 0x30) {
1074          SiS_Pr->SiS_SysFlags |= SF_760LFB;
1075       }
1076       if(SiS_GetReg(SiS_Pr->SiS_P3d4,0x79) & 0xf0) {
1077          SiS_Pr->SiS_SysFlags |= SF_760UMA;
1078       }
1079    }
1080 }
1081
1082 /*********************************************/
1083 /*         HELPER: Init PCI & Engines        */
1084 /*********************************************/
1085
1086 static void
1087 SiSInitPCIetc(struct SiS_Private *SiS_Pr)
1088 {
1089    switch(SiS_Pr->ChipType) {
1090 #ifdef CONFIG_FB_SIS_300
1091    case SIS_300:
1092    case SIS_540:
1093    case SIS_630:
1094    case SIS_730:
1095       /* Set - PCI LINEAR ADDRESSING ENABLE (0x80)
1096        *     - RELOCATED VGA IO ENABLED (0x20)
1097        *     - MMIO ENABLED (0x01)
1098        * Leave other bits untouched.
1099        */
1100       SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x20,0xa1);
1101       /*  - Enable 2D (0x40)
1102        *  - Enable 3D (0x02)
1103        *  - Enable 3D Vertex command fetch (0x10) ?
1104        *  - Enable 3D command parser (0x08) ?
1105        */
1106       SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x1E,0x5A);
1107       break;
1108 #endif
1109 #ifdef CONFIG_FB_SIS_315
1110    case SIS_315H:
1111    case SIS_315:
1112    case SIS_315PRO:
1113    case SIS_650:
1114    case SIS_740:
1115    case SIS_330:
1116    case SIS_661:
1117    case SIS_741:
1118    case SIS_660:
1119    case SIS_760:
1120    case SIS_761:
1121    case SIS_340:
1122    case XGI_40:
1123       /* See above */
1124       SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x20,0xa1);
1125       /*  - Enable 3D G/L transformation engine (0x80)
1126        *  - Enable 2D (0x40)
1127        *  - Enable 3D vertex command fetch (0x10)
1128        *  - Enable 3D command parser (0x08)
1129        *  - Enable 3D (0x02)
1130        */
1131       SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x1E,0xDA);
1132       break;
1133    case XGI_20:
1134    case SIS_550:
1135       /* See above */
1136       SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x20,0xa1);
1137       /* No 3D engine ! */
1138       /*  - Enable 2D (0x40)
1139        *  - disable 3D
1140        */
1141       SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x1E,0x60,0x40);
1142       break;
1143 #endif
1144    default:
1145       break;
1146    }
1147 }
1148
1149 /*********************************************/
1150 /*             HELPER: SetLVDSetc            */
1151 /*********************************************/
1152
1153 static
1154 void
1155 SiSSetLVDSetc(struct SiS_Private *SiS_Pr)
1156 {
1157    unsigned short temp;
1158
1159    SiS_Pr->SiS_IF_DEF_LVDS = 0;
1160    SiS_Pr->SiS_IF_DEF_TRUMPION = 0;
1161    SiS_Pr->SiS_IF_DEF_CH70xx = 0;
1162    SiS_Pr->SiS_IF_DEF_CONEX = 0;
1163
1164    SiS_Pr->SiS_ChrontelInit = 0;
1165
1166    if(SiS_Pr->ChipType == XGI_20) return;
1167
1168    /* Check for SiS30x first */
1169    temp = SiS_GetReg(SiS_Pr->SiS_Part4Port,0x00);
1170    if((temp == 1) || (temp == 2)) return;
1171
1172    switch(SiS_Pr->ChipType) {
1173 #ifdef CONFIG_FB_SIS_300
1174    case SIS_540:
1175    case SIS_630:
1176    case SIS_730:
1177         temp = (SiS_GetReg(SiS_Pr->SiS_P3d4,0x37) & 0x0e) >> 1;
1178         if((temp >= 2) && (temp <= 5))  SiS_Pr->SiS_IF_DEF_LVDS = 1;
1179         if(temp == 3)                   SiS_Pr->SiS_IF_DEF_TRUMPION = 1;
1180         if((temp == 4) || (temp == 5)) {
1181                 /* Save power status (and error check) - UNUSED */
1182                 SiS_Pr->SiS_Backup70xx = SiS_GetCH700x(SiS_Pr, 0x0e);
1183                 SiS_Pr->SiS_IF_DEF_CH70xx = 1;
1184         }
1185         break;
1186 #endif
1187 #ifdef CONFIG_FB_SIS_315
1188    case SIS_550:
1189    case SIS_650:
1190    case SIS_740:
1191    case SIS_330:
1192         temp = (SiS_GetReg(SiS_Pr->SiS_P3d4,0x37) & 0x0e) >> 1;
1193         if((temp >= 2) && (temp <= 3))  SiS_Pr->SiS_IF_DEF_LVDS = 1;
1194         if(temp == 3)                   SiS_Pr->SiS_IF_DEF_CH70xx = 2;
1195         break;
1196    case SIS_661:
1197    case SIS_741:
1198    case SIS_660:
1199    case SIS_760:
1200    case SIS_761:
1201    case SIS_340:
1202    case XGI_20:
1203    case XGI_40:
1204         temp = (SiS_GetReg(SiS_Pr->SiS_P3d4,0x38) & 0xe0) >> 5;
1205         if((temp >= 2) && (temp <= 3))  SiS_Pr->SiS_IF_DEF_LVDS = 1;
1206         if(temp == 3)                   SiS_Pr->SiS_IF_DEF_CH70xx = 2;
1207         if(temp == 4)                   SiS_Pr->SiS_IF_DEF_CONEX = 1;  /* Not yet supported */
1208         break;
1209 #endif
1210    default:
1211         break;
1212    }
1213 }
1214
1215 /*********************************************/
1216 /*          HELPER: Enable DSTN/FSTN         */
1217 /*********************************************/
1218
1219 void
1220 SiS_SetEnableDstn(struct SiS_Private *SiS_Pr, int enable)
1221 {
1222    SiS_Pr->SiS_IF_DEF_DSTN = enable ? 1 : 0;
1223 }
1224
1225 void
1226 SiS_SetEnableFstn(struct SiS_Private *SiS_Pr, int enable)
1227 {
1228    SiS_Pr->SiS_IF_DEF_FSTN = enable ? 1 : 0;
1229 }
1230
1231 /*********************************************/
1232 /*            HELPER: Get modeflag           */
1233 /*********************************************/
1234
1235 unsigned short
1236 SiS_GetModeFlag(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
1237                 unsigned short ModeIdIndex)
1238 {
1239    if(SiS_Pr->UseCustomMode) {
1240       return SiS_Pr->CModeFlag;
1241    } else if(ModeNo <= 0x13) {
1242       return SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
1243    } else {
1244       return SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
1245    }
1246 }
1247
1248 /*********************************************/
1249 /*        HELPER: Determine ROM usage        */
1250 /*********************************************/
1251
1252 bool
1253 SiSDetermineROMLayout661(struct SiS_Private *SiS_Pr)
1254 {
1255    unsigned char  *ROMAddr  = SiS_Pr->VirtualRomBase;
1256    unsigned short romversoffs, romvmaj = 1, romvmin = 0;
1257
1258    if(SiS_Pr->ChipType >= XGI_20) {
1259       /* XGI ROMs don't qualify */
1260       return false;
1261    } else if(SiS_Pr->ChipType >= SIS_761) {
1262       /* I very much assume 761, 340 and newer will use new layout */
1263       return true;
1264    } else if(SiS_Pr->ChipType >= SIS_661) {
1265       if((ROMAddr[0x1a] == 'N') &&
1266          (ROMAddr[0x1b] == 'e') &&
1267          (ROMAddr[0x1c] == 'w') &&
1268          (ROMAddr[0x1d] == 'V')) {
1269          return true;
1270       }
1271       romversoffs = ROMAddr[0x16] | (ROMAddr[0x17] << 8);
1272       if(romversoffs) {
1273          if((ROMAddr[romversoffs+1] == '.') || (ROMAddr[romversoffs+4] == '.')) {
1274             romvmaj = ROMAddr[romversoffs] - '0';
1275             romvmin = ((ROMAddr[romversoffs+2] -'0') * 10) + (ROMAddr[romversoffs+3] - '0');
1276          }
1277       }
1278       if((romvmaj != 0) || (romvmin >= 92)) {
1279          return true;
1280       }
1281    } else if(IS_SIS650740) {
1282       if((ROMAddr[0x1a] == 'N') &&
1283          (ROMAddr[0x1b] == 'e') &&
1284          (ROMAddr[0x1c] == 'w') &&
1285          (ROMAddr[0x1d] == 'V')) {
1286          return true;
1287       }
1288    }
1289    return false;
1290 }
1291
1292 static void
1293 SiSDetermineROMUsage(struct SiS_Private *SiS_Pr)
1294 {
1295    unsigned char  *ROMAddr  = SiS_Pr->VirtualRomBase;
1296    unsigned short romptr = 0;
1297
1298    SiS_Pr->SiS_UseROM = false;
1299    SiS_Pr->SiS_ROMNew = false;
1300    SiS_Pr->SiS_PWDOffset = 0;
1301
1302    if(SiS_Pr->ChipType >= XGI_20) return;
1303
1304    if((ROMAddr) && (SiS_Pr->UseROM)) {
1305       if(SiS_Pr->ChipType == SIS_300) {
1306          /* 300: We check if the code starts below 0x220 by
1307           * checking the jmp instruction at the beginning
1308           * of the BIOS image.
1309           */
1310          if((ROMAddr[3] == 0xe9) && ((ROMAddr[5] << 8) | ROMAddr[4]) > 0x21a)
1311             SiS_Pr->SiS_UseROM = true;
1312       } else if(SiS_Pr->ChipType < SIS_315H) {
1313          /* Sony's VAIO BIOS 1.09 follows the standard, so perhaps
1314           * the others do as well
1315           */
1316          SiS_Pr->SiS_UseROM = true;
1317       } else {
1318          /* 315/330 series stick to the standard(s) */
1319          SiS_Pr->SiS_UseROM = true;
1320          if((SiS_Pr->SiS_ROMNew = SiSDetermineROMLayout661(SiS_Pr))) {
1321             SiS_Pr->SiS_EMIOffset = 14;
1322             SiS_Pr->SiS_PWDOffset = 17;
1323             SiS_Pr->SiS661LCD2TableSize = 36;
1324             /* Find out about LCD data table entry size */
1325             if((romptr = SISGETROMW(0x0102))) {
1326                if(ROMAddr[romptr + (32 * 16)] == 0xff)
1327                   SiS_Pr->SiS661LCD2TableSize = 32;
1328                else if(ROMAddr[romptr + (34 * 16)] == 0xff)
1329                   SiS_Pr->SiS661LCD2TableSize = 34;
1330                else if(ROMAddr[romptr + (36 * 16)] == 0xff)        /* 0.94, 2.05.00+ */
1331                   SiS_Pr->SiS661LCD2TableSize = 36;
1332                else if( (ROMAddr[romptr + (38 * 16)] == 0xff) ||   /* 2.00.00 - 2.02.00 */
1333                         (ROMAddr[0x6F] & 0x01) ) {                 /* 2.03.00 - <2.05.00 */
1334                   SiS_Pr->SiS661LCD2TableSize = 38;                /* UMC data layout abandoned at 2.05.00 */
1335                   SiS_Pr->SiS_EMIOffset = 16;
1336                   SiS_Pr->SiS_PWDOffset = 19;
1337                }
1338             }
1339          }
1340       }
1341    }
1342 }
1343
1344 /*********************************************/
1345 /*        HELPER: SET SEGMENT REGISTERS      */
1346 /*********************************************/
1347
1348 static void
1349 SiS_SetSegRegLower(struct SiS_Private *SiS_Pr, unsigned short value)
1350 {
1351    unsigned short temp;
1352
1353    value &= 0x00ff;
1354    temp = SiS_GetRegByte(SiS_Pr->SiS_P3cb) & 0xf0;
1355    temp |= (value >> 4);
1356    SiS_SetRegByte(SiS_Pr->SiS_P3cb, temp);
1357    temp = SiS_GetRegByte(SiS_Pr->SiS_P3cd) & 0xf0;
1358    temp |= (value & 0x0f);
1359    SiS_SetRegByte(SiS_Pr->SiS_P3cd, temp);
1360 }
1361
1362 static void
1363 SiS_SetSegRegUpper(struct SiS_Private *SiS_Pr, unsigned short value)
1364 {
1365    unsigned short temp;
1366
1367    value &= 0x00ff;
1368    temp = SiS_GetRegByte(SiS_Pr->SiS_P3cb) & 0x0f;
1369    temp |= (value & 0xf0);
1370    SiS_SetRegByte(SiS_Pr->SiS_P3cb, temp);
1371    temp = SiS_GetRegByte(SiS_Pr->SiS_P3cd) & 0x0f;
1372    temp |= (value << 4);
1373    SiS_SetRegByte(SiS_Pr->SiS_P3cd, temp);
1374 }
1375
1376 static void
1377 SiS_SetSegmentReg(struct SiS_Private *SiS_Pr, unsigned short value)
1378 {
1379    SiS_SetSegRegLower(SiS_Pr, value);
1380    SiS_SetSegRegUpper(SiS_Pr, value);
1381 }
1382
1383 static void
1384 SiS_ResetSegmentReg(struct SiS_Private *SiS_Pr)
1385 {
1386    SiS_SetSegmentReg(SiS_Pr, 0);
1387 }
1388
1389 static void
1390 SiS_SetSegmentRegOver(struct SiS_Private *SiS_Pr, unsigned short value)
1391 {
1392    unsigned short temp = value >> 8;
1393
1394    temp &= 0x07;
1395    temp |= (temp << 4);
1396    SiS_SetReg(SiS_Pr->SiS_P3c4,0x1d,temp);
1397    SiS_SetSegmentReg(SiS_Pr, value);
1398 }
1399
1400 static void
1401 SiS_ResetSegmentRegOver(struct SiS_Private *SiS_Pr)
1402 {
1403    SiS_SetSegmentRegOver(SiS_Pr, 0);
1404 }
1405
1406 static void
1407 SiS_ResetSegmentRegisters(struct SiS_Private *SiS_Pr)
1408 {
1409    if((IS_SIS65x) || (SiS_Pr->ChipType >= SIS_661)) {
1410       SiS_ResetSegmentReg(SiS_Pr);
1411       SiS_ResetSegmentRegOver(SiS_Pr);
1412    }
1413 }
1414
1415 /*********************************************/
1416 /*             HELPER: GetVBType             */
1417 /*********************************************/
1418
1419 static
1420 void
1421 SiS_GetVBType(struct SiS_Private *SiS_Pr)
1422 {
1423    unsigned short flag = 0, rev = 0, nolcd = 0;
1424    unsigned short p4_0f, p4_25, p4_27;
1425
1426    SiS_Pr->SiS_VBType = 0;
1427
1428    if((SiS_Pr->SiS_IF_DEF_LVDS) || (SiS_Pr->SiS_IF_DEF_CONEX))
1429       return;
1430
1431    if(SiS_Pr->ChipType == XGI_20)
1432       return;
1433
1434    flag = SiS_GetReg(SiS_Pr->SiS_Part4Port,0x00);
1435
1436    if(flag > 3)
1437       return;
1438
1439    rev = SiS_GetReg(SiS_Pr->SiS_Part4Port,0x01);
1440
1441    if(flag >= 2) {
1442       SiS_Pr->SiS_VBType = VB_SIS302B;
1443    } else if(flag == 1) {
1444       if(rev >= 0xC0) {
1445          SiS_Pr->SiS_VBType = VB_SIS301C;
1446       } else if(rev >= 0xB0) {
1447          SiS_Pr->SiS_VBType = VB_SIS301B;
1448          /* Check if 30xB DH version (no LCD support, use Panel Link instead) */
1449          nolcd = SiS_GetReg(SiS_Pr->SiS_Part4Port,0x23);
1450          if(!(nolcd & 0x02)) SiS_Pr->SiS_VBType |= VB_NoLCD;
1451       } else {
1452          SiS_Pr->SiS_VBType = VB_SIS301;
1453       }
1454    }
1455    if(SiS_Pr->SiS_VBType & (VB_SIS301B | VB_SIS301C | VB_SIS302B)) {
1456       if(rev >= 0xE0) {
1457          flag = SiS_GetReg(SiS_Pr->SiS_Part4Port,0x39);
1458          if(flag == 0xff) SiS_Pr->SiS_VBType = VB_SIS302LV;
1459          else             SiS_Pr->SiS_VBType = VB_SIS301C;  /* VB_SIS302ELV; */
1460       } else if(rev >= 0xD0) {
1461          SiS_Pr->SiS_VBType = VB_SIS301LV;
1462       }
1463    }
1464    if(SiS_Pr->SiS_VBType & (VB_SIS301C | VB_SIS301LV | VB_SIS302LV | VB_SIS302ELV)) {
1465       p4_0f = SiS_GetReg(SiS_Pr->SiS_Part4Port,0x0f);
1466       p4_25 = SiS_GetReg(SiS_Pr->SiS_Part4Port,0x25);
1467       p4_27 = SiS_GetReg(SiS_Pr->SiS_Part4Port,0x27);
1468       SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x0f,0x7f);
1469       SiS_SetRegOR(SiS_Pr->SiS_Part4Port,0x25,0x08);
1470       SiS_SetRegAND(SiS_Pr->SiS_Part4Port,0x27,0xfd);
1471       if(SiS_GetReg(SiS_Pr->SiS_Part4Port,0x26) & 0x08) {
1472          SiS_Pr->SiS_VBType |= VB_UMC;
1473       }
1474       SiS_SetReg(SiS_Pr->SiS_Part4Port,0x27,p4_27);
1475       SiS_SetReg(SiS_Pr->SiS_Part4Port,0x25,p4_25);
1476       SiS_SetReg(SiS_Pr->SiS_Part4Port,0x0f,p4_0f);
1477    }
1478 }
1479
1480 /*********************************************/
1481 /*           HELPER: Check RAM size          */
1482 /*********************************************/
1483
1484 static bool
1485 SiS_CheckMemorySize(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
1486                 unsigned short ModeIdIndex)
1487 {
1488    unsigned short AdapterMemSize = SiS_Pr->VideoMemorySize / (1024*1024);
1489    unsigned short modeflag = SiS_GetModeFlag(SiS_Pr, ModeNo, ModeIdIndex);
1490    unsigned short memorysize = ((modeflag & MemoryInfoFlag) >> MemorySizeShift) + 1;
1491
1492    if(!AdapterMemSize) return true;
1493
1494    if(AdapterMemSize < memorysize) return false;
1495    return true;
1496 }
1497
1498 /*********************************************/
1499 /*           HELPER: Get DRAM type           */
1500 /*********************************************/
1501
1502 #ifdef CONFIG_FB_SIS_315
1503 static unsigned char
1504 SiS_Get310DRAMType(struct SiS_Private *SiS_Pr)
1505 {
1506    unsigned char data;
1507
1508    if((*SiS_Pr->pSiS_SoftSetting) & SoftDRAMType) {
1509       data = (*SiS_Pr->pSiS_SoftSetting) & 0x03;
1510    } else {
1511       if(SiS_Pr->ChipType >= XGI_20) {
1512          /* Do I need this? SR17 seems to be zero anyway... */
1513          data = 0;
1514       } else if(SiS_Pr->ChipType >= SIS_340) {
1515          /* TODO */
1516          data = 0;
1517       } if(SiS_Pr->ChipType >= SIS_661) {
1518          if(SiS_Pr->SiS_ROMNew) {
1519             data = ((SiS_GetReg(SiS_Pr->SiS_P3d4,0x78) & 0xc0) >> 6);
1520          } else {
1521             data = SiS_GetReg(SiS_Pr->SiS_P3d4,0x78) & 0x07;
1522          }
1523       } else if(IS_SIS550650740) {
1524          data = SiS_GetReg(SiS_Pr->SiS_P3c4,0x13) & 0x07;
1525       } else {  /* 315, 330 */
1526          data = SiS_GetReg(SiS_Pr->SiS_P3c4,0x3a) & 0x03;
1527          if(SiS_Pr->ChipType == SIS_330) {
1528             if(data > 1) {
1529                switch(SiS_GetReg(SiS_Pr->SiS_P3d4,0x5f) & 0x30) {
1530                case 0x00: data = 1; break;
1531                case 0x10: data = 3; break;
1532                case 0x20: data = 3; break;
1533                case 0x30: data = 2; break;
1534                }
1535             } else {
1536                data = 0;
1537             }
1538          }
1539       }
1540    }
1541
1542    return data;
1543 }
1544
1545 static unsigned short
1546 SiS_GetMCLK(struct SiS_Private *SiS_Pr)
1547 {
1548    unsigned char  *ROMAddr = SiS_Pr->VirtualRomBase;
1549    unsigned short index;
1550
1551    index = SiS_Get310DRAMType(SiS_Pr);
1552    if(SiS_Pr->ChipType >= SIS_661) {
1553       if(SiS_Pr->SiS_ROMNew) {
1554          return((unsigned short)(SISGETROMW((0x90 + (index * 5) + 3))));
1555       }
1556       return(SiS_Pr->SiS_MCLKData_0[index].CLOCK);
1557    } else if(index >= 4) {
1558       return(SiS_Pr->SiS_MCLKData_1[index - 4].CLOCK);
1559    } else {
1560       return(SiS_Pr->SiS_MCLKData_0[index].CLOCK);
1561    }
1562 }
1563 #endif
1564
1565 /*********************************************/
1566 /*           HELPER: ClearBuffer             */
1567 /*********************************************/
1568
1569 static void
1570 SiS_ClearBuffer(struct SiS_Private *SiS_Pr, unsigned short ModeNo)
1571 {
1572    unsigned char  SISIOMEMTYPE *memaddr = SiS_Pr->VideoMemoryAddress;
1573    unsigned int   memsize = SiS_Pr->VideoMemorySize;
1574    unsigned short SISIOMEMTYPE *pBuffer;
1575    int i;
1576
1577    if(!memaddr || !memsize) return;
1578
1579    if(SiS_Pr->SiS_ModeType >= ModeEGA) {
1580       if(ModeNo > 0x13) {
1581          memset_io(memaddr, 0, memsize);
1582       } else {
1583          pBuffer = (unsigned short SISIOMEMTYPE *)memaddr;
1584          for(i = 0; i < 0x4000; i++) writew(0x0000, &pBuffer[i]);
1585       }
1586    } else if(SiS_Pr->SiS_ModeType < ModeCGA) {
1587       pBuffer = (unsigned short SISIOMEMTYPE *)memaddr;
1588       for(i = 0; i < 0x4000; i++) writew(0x0720, &pBuffer[i]);
1589    } else {
1590       memset_io(memaddr, 0, 0x8000);
1591    }
1592 }
1593
1594 /*********************************************/
1595 /*           HELPER: SearchModeID            */
1596 /*********************************************/
1597
1598 bool
1599 SiS_SearchModeID(struct SiS_Private *SiS_Pr, unsigned short *ModeNo,
1600                 unsigned short *ModeIdIndex)
1601 {
1602    unsigned char VGAINFO = SiS_Pr->SiS_VGAINFO;
1603
1604    if((*ModeNo) <= 0x13) {
1605
1606       if((*ModeNo) <= 0x05) (*ModeNo) |= 0x01;
1607
1608       for((*ModeIdIndex) = 0; ;(*ModeIdIndex)++) {
1609          if(SiS_Pr->SiS_SModeIDTable[(*ModeIdIndex)].St_ModeID == (*ModeNo)) break;
1610          if(SiS_Pr->SiS_SModeIDTable[(*ModeIdIndex)].St_ModeID == 0xFF) return false;
1611       }
1612
1613       if((*ModeNo) == 0x07) {
1614           if(VGAINFO & 0x10) (*ModeIdIndex)++;   /* 400 lines */
1615           /* else 350 lines */
1616       }
1617       if((*ModeNo) <= 0x03) {
1618          if(!(VGAINFO & 0x80)) (*ModeIdIndex)++;
1619          if(VGAINFO & 0x10)    (*ModeIdIndex)++; /* 400 lines  */
1620          /* else 350 lines  */
1621       }
1622       /* else 200 lines  */
1623
1624    } else {
1625
1626       for((*ModeIdIndex) = 0; ;(*ModeIdIndex)++) {
1627          if(SiS_Pr->SiS_EModeIDTable[(*ModeIdIndex)].Ext_ModeID == (*ModeNo)) break;
1628          if(SiS_Pr->SiS_EModeIDTable[(*ModeIdIndex)].Ext_ModeID == 0xFF) return false;
1629       }
1630
1631    }
1632    return true;
1633 }
1634
1635 /*********************************************/
1636 /*            HELPER: GetModePtr             */
1637 /*********************************************/
1638
1639 unsigned short
1640 SiS_GetModePtr(struct SiS_Private *SiS_Pr, unsigned short ModeNo, unsigned short ModeIdIndex)
1641 {
1642    unsigned short index;
1643
1644    if(ModeNo <= 0x13) {
1645       index = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_StTableIndex;
1646    } else {
1647       if(SiS_Pr->SiS_ModeType <= ModeEGA) index = 0x1B;
1648       else index = 0x0F;
1649    }
1650    return index;
1651 }
1652
1653 /*********************************************/
1654 /*         HELPERS: Get some indices         */
1655 /*********************************************/
1656
1657 unsigned short
1658 SiS_GetRefCRTVCLK(struct SiS_Private *SiS_Pr, unsigned short Index, int UseWide)
1659 {
1660    if(SiS_Pr->SiS_RefIndex[Index].Ext_InfoFlag & HaveWideTiming) {
1661       if(UseWide == 1) {
1662          return SiS_Pr->SiS_RefIndex[Index].Ext_CRTVCLK_WIDE;
1663       } else {
1664          return SiS_Pr->SiS_RefIndex[Index].Ext_CRTVCLK_NORM;
1665       }
1666    } else {
1667       return SiS_Pr->SiS_RefIndex[Index].Ext_CRTVCLK;
1668    }
1669 }
1670
1671 unsigned short
1672 SiS_GetRefCRT1CRTC(struct SiS_Private *SiS_Pr, unsigned short Index, int UseWide)
1673 {
1674    if(SiS_Pr->SiS_RefIndex[Index].Ext_InfoFlag & HaveWideTiming) {
1675       if(UseWide == 1) {
1676          return SiS_Pr->SiS_RefIndex[Index].Ext_CRT1CRTC_WIDE;
1677       } else {
1678          return SiS_Pr->SiS_RefIndex[Index].Ext_CRT1CRTC_NORM;
1679       }
1680    } else {
1681       return SiS_Pr->SiS_RefIndex[Index].Ext_CRT1CRTC;
1682    }
1683 }
1684
1685 /*********************************************/
1686 /*           HELPER: LowModeTests            */
1687 /*********************************************/
1688
1689 static bool
1690 SiS_DoLowModeTest(struct SiS_Private *SiS_Pr, unsigned short ModeNo)
1691 {
1692    unsigned short temp, temp1, temp2;
1693
1694    if((ModeNo != 0x03) && (ModeNo != 0x10) && (ModeNo != 0x12))
1695       return true;
1696    temp = SiS_GetReg(SiS_Pr->SiS_P3d4,0x11);
1697    SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x11,0x80);
1698    temp1 = SiS_GetReg(SiS_Pr->SiS_P3d4,0x00);
1699    SiS_SetReg(SiS_Pr->SiS_P3d4,0x00,0x55);
1700    temp2 = SiS_GetReg(SiS_Pr->SiS_P3d4,0x00);
1701    SiS_SetReg(SiS_Pr->SiS_P3d4,0x00,temp1);
1702    SiS_SetReg(SiS_Pr->SiS_P3d4,0x11,temp);
1703    if((SiS_Pr->ChipType >= SIS_315H) ||
1704       (SiS_Pr->ChipType == SIS_300)) {
1705       if(temp2 == 0x55) return false;
1706       else return true;
1707    } else {
1708       if(temp2 != 0x55) return true;
1709       else {
1710          SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x35,0x01);
1711          return false;
1712       }
1713    }
1714 }
1715
1716 static void
1717 SiS_SetLowModeTest(struct SiS_Private *SiS_Pr, unsigned short ModeNo)
1718 {
1719    if(SiS_DoLowModeTest(SiS_Pr, ModeNo)) {
1720       SiS_Pr->SiS_SetFlag |= LowModeTests;
1721    }
1722 }
1723
1724 /*********************************************/
1725 /*        HELPER: OPEN/CLOSE CRT1 CRTC       */
1726 /*********************************************/
1727
1728 static void
1729 SiS_OpenCRTC(struct SiS_Private *SiS_Pr)
1730 {
1731    if(IS_SIS650) {
1732       SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x51,0x1f);
1733       if(IS_SIS651) SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x51,0x20);
1734       SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x56,0xe7);
1735    } else if(IS_SIS661741660760) {
1736       SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x61,0xf7);
1737       SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x51,0x1f);
1738       SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x56,0xe7);
1739       if(!SiS_Pr->SiS_ROMNew) {
1740          SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x3a,0xef);
1741       }
1742    }
1743 }
1744
1745 static void
1746 SiS_CloseCRTC(struct SiS_Private *SiS_Pr)
1747 {
1748 #if 0 /* This locks some CRTC registers. We don't want that. */
1749    unsigned short temp1 = 0, temp2 = 0;
1750
1751    if(IS_SIS661741660760) {
1752       if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
1753          temp1 = 0xa0; temp2 = 0x08;
1754       }
1755       SiS_SetRegANDOR(SiS_Pr->SiS_P3d4,0x51,0x1f,temp1);
1756       SiS_SetRegANDOR(SiS_Pr->SiS_P3d4,0x56,0xe7,temp2);
1757    }
1758 #endif
1759 }
1760
1761 static void
1762 SiS_HandleCRT1(struct SiS_Private *SiS_Pr)
1763 {
1764    /* Enable CRT1 gating */
1765    SiS_SetRegAND(SiS_Pr->SiS_P3d4,SiS_Pr->SiS_MyCR63,0xbf);
1766 #if 0
1767    if(!(SiS_GetReg(SiS_Pr->SiS_P3c4,0x15) & 0x01)) {
1768       if((SiS_GetReg(SiS_Pr->SiS_P3c4,0x15) & 0x0a) ||
1769          (SiS_GetReg(SiS_Pr->SiS_P3c4,0x16) & 0x01)) {
1770          SiS_SetRegOR(SiS_Pr->SiS_P3d4,SiS_Pr->SiS_MyCR63,0x40);
1771       }
1772    }
1773 #endif
1774 }
1775
1776 /*********************************************/
1777 /*           HELPER: GetColorDepth           */
1778 /*********************************************/
1779
1780 unsigned short
1781 SiS_GetColorDepth(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
1782                 unsigned short ModeIdIndex)
1783 {
1784    static const unsigned short ColorDepth[6] = { 1, 2, 4, 4, 6, 8 };
1785    unsigned short modeflag;
1786    short index;
1787
1788    /* Do NOT check UseCustomMode, will skrew up FIFO */
1789    if(ModeNo == 0xfe) {
1790       modeflag = SiS_Pr->CModeFlag;
1791    } else if(ModeNo <= 0x13) {
1792       modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
1793    } else {
1794       modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
1795    }
1796
1797    index = (modeflag & ModeTypeMask) - ModeEGA;
1798    if(index < 0) index = 0;
1799    return ColorDepth[index];
1800 }
1801
1802 /*********************************************/
1803 /*             HELPER: GetOffset             */
1804 /*********************************************/
1805
1806 unsigned short
1807 SiS_GetOffset(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
1808                 unsigned short ModeIdIndex, unsigned short RRTI)
1809 {
1810    unsigned short xres, temp, colordepth, infoflag;
1811
1812    if(SiS_Pr->UseCustomMode) {
1813       infoflag = SiS_Pr->CInfoFlag;
1814       xres = SiS_Pr->CHDisplay;
1815    } else {
1816       infoflag = SiS_Pr->SiS_RefIndex[RRTI].Ext_InfoFlag;
1817       xres = SiS_Pr->SiS_RefIndex[RRTI].XRes;
1818    }
1819
1820    colordepth = SiS_GetColorDepth(SiS_Pr, ModeNo, ModeIdIndex);
1821
1822    temp = xres / 16;
1823    if(infoflag & InterlaceMode) temp <<= 1;
1824    temp *= colordepth;
1825    if(xres % 16) temp += (colordepth >> 1);
1826
1827    return temp;
1828 }
1829
1830 /*********************************************/
1831 /*                   SEQ                     */
1832 /*********************************************/
1833
1834 static void
1835 SiS_SetSeqRegs(struct SiS_Private *SiS_Pr, unsigned short StandTableIndex)
1836 {
1837    unsigned char SRdata;
1838    int i;
1839
1840    SiS_SetReg(SiS_Pr->SiS_P3c4,0x00,0x03);
1841
1842    /* or "display off"  */
1843    SRdata = SiS_Pr->SiS_StandTable[StandTableIndex].SR[0] | 0x20;
1844
1845    /* determine whether to force x8 dotclock */
1846    if((SiS_Pr->SiS_VBType & VB_SISVB) || (SiS_Pr->SiS_IF_DEF_LVDS)) {
1847
1848       if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToTV)) {
1849          if(SiS_Pr->SiS_VBInfo & SetInSlaveMode)    SRdata |= 0x01;
1850       } else if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) SRdata |= 0x01;
1851
1852    }
1853
1854    SiS_SetReg(SiS_Pr->SiS_P3c4,0x01,SRdata);
1855
1856    for(i = 2; i <= 4; i++) {
1857       SRdata = SiS_Pr->SiS_StandTable[StandTableIndex].SR[i - 1];
1858       SiS_SetReg(SiS_Pr->SiS_P3c4,i,SRdata);
1859    }
1860 }
1861
1862 /*********************************************/
1863 /*                  MISC                     */
1864 /*********************************************/
1865
1866 static void
1867 SiS_SetMiscRegs(struct SiS_Private *SiS_Pr, unsigned short StandTableIndex)
1868 {
1869    unsigned char Miscdata;
1870
1871    Miscdata = SiS_Pr->SiS_StandTable[StandTableIndex].MISC;
1872
1873    if(SiS_Pr->ChipType < SIS_661) {
1874       if(SiS_Pr->SiS_VBType & VB_SIS30xBLV) {
1875          if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
1876            Miscdata |= 0x0C;
1877          }
1878       }
1879    }
1880
1881    SiS_SetRegByte(SiS_Pr->SiS_P3c2,Miscdata);
1882 }
1883
1884 /*********************************************/
1885 /*                  CRTC                     */
1886 /*********************************************/
1887
1888 static void
1889 SiS_SetCRTCRegs(struct SiS_Private *SiS_Pr, unsigned short StandTableIndex)
1890 {
1891    unsigned char  CRTCdata;
1892    unsigned short i;
1893
1894    /* Unlock CRTC */
1895    SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x11,0x7f);
1896
1897    for(i = 0; i <= 0x18; i++) {
1898       CRTCdata = SiS_Pr->SiS_StandTable[StandTableIndex].CRTC[i];
1899       SiS_SetReg(SiS_Pr->SiS_P3d4,i,CRTCdata);
1900    }
1901
1902    if(SiS_Pr->ChipType >= SIS_661) {
1903       SiS_OpenCRTC(SiS_Pr);
1904       for(i = 0x13; i <= 0x14; i++) {
1905          CRTCdata = SiS_Pr->SiS_StandTable[StandTableIndex].CRTC[i];
1906          SiS_SetReg(SiS_Pr->SiS_P3d4,i,CRTCdata);
1907       }
1908    } else if( ( (SiS_Pr->ChipType == SIS_630) ||
1909                 (SiS_Pr->ChipType == SIS_730) )  &&
1910               (SiS_Pr->ChipRevision >= 0x30) ) {
1911       if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
1912          if(SiS_Pr->SiS_VBInfo & (SetCRT2ToLCD | SetCRT2ToTV)) {
1913             SiS_SetReg(SiS_Pr->SiS_P3d4,0x18,0xFE);
1914          }
1915       }
1916    }
1917 }
1918
1919 /*********************************************/
1920 /*                   ATT                     */
1921 /*********************************************/
1922
1923 static void
1924 SiS_SetATTRegs(struct SiS_Private *SiS_Pr, unsigned short StandTableIndex)
1925 {
1926    unsigned char  ARdata;
1927    unsigned short i;
1928
1929    for(i = 0; i <= 0x13; i++) {
1930       ARdata = SiS_Pr->SiS_StandTable[StandTableIndex].ATTR[i];
1931
1932       if(i == 0x13) {
1933          /* Pixel shift. If screen on LCD or TV is shifted left or right,
1934           * this might be the cause.
1935           */
1936          if(SiS_Pr->SiS_VBType & VB_SIS30xBLV) {
1937             if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) ARdata = 0;
1938          }
1939          if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
1940             if(SiS_Pr->SiS_IF_DEF_CH70xx != 0) {
1941                if(SiS_Pr->SiS_VBInfo & SetCRT2ToTV) {
1942                   if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) ARdata = 0;
1943                }
1944             }
1945          }
1946          if(SiS_Pr->ChipType >= SIS_661) {
1947             if(SiS_Pr->SiS_VBInfo & (SetCRT2ToTV | SetCRT2ToLCD)) {
1948                if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) ARdata = 0;
1949             }
1950          } else if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) {
1951             if(SiS_Pr->ChipType >= SIS_315H) {
1952                if(IS_SIS550650740660) {
1953                   /* 315, 330 don't do this */
1954                   if(SiS_Pr->SiS_VBType & VB_SIS30xB) {
1955                      if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) ARdata = 0;
1956                   } else {
1957                      ARdata = 0;
1958                   }
1959                }
1960             } else {
1961                if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) ARdata = 0;
1962             }
1963          }
1964       }
1965       SiS_GetRegByte(SiS_Pr->SiS_P3da);         /* reset 3da  */
1966       SiS_SetRegByte(SiS_Pr->SiS_P3c0,i);       /* set index  */
1967       SiS_SetRegByte(SiS_Pr->SiS_P3c0,ARdata);  /* set data   */
1968    }
1969
1970    SiS_GetRegByte(SiS_Pr->SiS_P3da);            /* reset 3da  */
1971    SiS_SetRegByte(SiS_Pr->SiS_P3c0,0x14);       /* set index  */
1972    SiS_SetRegByte(SiS_Pr->SiS_P3c0,0x00);       /* set data   */
1973
1974    SiS_GetRegByte(SiS_Pr->SiS_P3da);
1975    SiS_SetRegByte(SiS_Pr->SiS_P3c0,0x20);       /* Enable Attribute  */
1976    SiS_GetRegByte(SiS_Pr->SiS_P3da);
1977 }
1978
1979 /*********************************************/
1980 /*                   GRC                     */
1981 /*********************************************/
1982
1983 static void
1984 SiS_SetGRCRegs(struct SiS_Private *SiS_Pr, unsigned short StandTableIndex)
1985 {
1986    unsigned char  GRdata;
1987    unsigned short i;
1988
1989    for(i = 0; i <= 0x08; i++) {
1990       GRdata = SiS_Pr->SiS_StandTable[StandTableIndex].GRC[i];
1991       SiS_SetReg(SiS_Pr->SiS_P3ce,i,GRdata);
1992    }
1993
1994    if(SiS_Pr->SiS_ModeType > ModeVGA) {
1995       /* 256 color disable */
1996       SiS_SetRegAND(SiS_Pr->SiS_P3ce,0x05,0xBF);
1997    }
1998 }
1999
2000 /*********************************************/
2001 /*          CLEAR EXTENDED REGISTERS         */
2002 /*********************************************/
2003
2004 static void
2005 SiS_ClearExt1Regs(struct SiS_Private *SiS_Pr, unsigned short ModeNo)
2006 {
2007    unsigned short i;
2008
2009    for(i = 0x0A; i <= 0x0E; i++) {
2010       SiS_SetReg(SiS_Pr->SiS_P3c4,i,0x00);
2011    }
2012
2013    if(SiS_Pr->ChipType >= SIS_315H) {
2014       SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x37,0xFE);
2015       if(ModeNo <= 0x13) {
2016          if(ModeNo == 0x06 || ModeNo >= 0x0e) {
2017             SiS_SetReg(SiS_Pr->SiS_P3c4,0x0e,0x20);
2018          }
2019       }
2020    }
2021 }
2022
2023 /*********************************************/
2024 /*                 RESET VCLK                */
2025 /*********************************************/
2026
2027 static void
2028 SiS_ResetCRT1VCLK(struct SiS_Private *SiS_Pr)
2029 {
2030    if(SiS_Pr->ChipType >= SIS_315H) {
2031       if(SiS_Pr->ChipType < SIS_661) {
2032          if(SiS_Pr->SiS_IF_DEF_LVDS == 0) return;
2033       }
2034    } else {
2035       if((SiS_Pr->SiS_IF_DEF_LVDS == 0) &&
2036          (!(SiS_Pr->SiS_VBType & VB_SIS30xBLV)) ) {
2037          return;
2038       }
2039    }
2040
2041    SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x31,0xcf,0x20);
2042    SiS_SetReg(SiS_Pr->SiS_P3c4,0x2B,SiS_Pr->SiS_VCLKData[1].SR2B);
2043    SiS_SetReg(SiS_Pr->SiS_P3c4,0x2C,SiS_Pr->SiS_VCLKData[1].SR2C);
2044    SiS_SetReg(SiS_Pr->SiS_P3c4,0x2D,0x80);
2045    SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x31,0xcf,0x10);
2046    SiS_SetReg(SiS_Pr->SiS_P3c4,0x2B,SiS_Pr->SiS_VCLKData[0].SR2B);
2047    SiS_SetReg(SiS_Pr->SiS_P3c4,0x2C,SiS_Pr->SiS_VCLKData[0].SR2C);
2048    SiS_SetReg(SiS_Pr->SiS_P3c4,0x2D,0x80);
2049 }
2050
2051 /*********************************************/
2052 /*                  SYNC                     */
2053 /*********************************************/
2054
2055 static void
2056 SiS_SetCRT1Sync(struct SiS_Private *SiS_Pr, unsigned short RRTI)
2057 {
2058    unsigned short sync;
2059
2060    if(SiS_Pr->UseCustomMode) {
2061       sync = SiS_Pr->CInfoFlag >> 8;
2062    } else {
2063       sync = SiS_Pr->SiS_RefIndex[RRTI].Ext_InfoFlag >> 8;
2064    }
2065
2066    sync &= 0xC0;
2067    sync |= 0x2f;
2068    SiS_SetRegByte(SiS_Pr->SiS_P3c2,sync);
2069 }
2070
2071 /*********************************************/
2072 /*                  CRTC/2                   */
2073 /*********************************************/
2074
2075 static void
2076 SiS_SetCRT1CRTC(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
2077                 unsigned short ModeIdIndex, unsigned short RRTI)
2078 {
2079    unsigned short temp, i, j, modeflag;
2080    unsigned char  *crt1data = NULL;
2081
2082    modeflag = SiS_GetModeFlag(SiS_Pr, ModeNo, ModeIdIndex);
2083
2084    if(SiS_Pr->UseCustomMode) {
2085
2086       crt1data = &SiS_Pr->CCRT1CRTC[0];
2087
2088    } else {
2089
2090       temp = SiS_GetRefCRT1CRTC(SiS_Pr, RRTI, SiS_Pr->SiS_UseWide);
2091
2092       /* Alternate for 1600x1200 LCDA */
2093       if((temp == 0x20) && (SiS_Pr->Alternate1600x1200)) temp = 0x57;
2094
2095       crt1data = (unsigned char *)&SiS_Pr->SiS_CRT1Table[temp].CR[0];
2096
2097    }
2098
2099    /* unlock cr0-7 */
2100    SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x11,0x7f);
2101
2102    for(i = 0, j = 0; i <= 7; i++, j++) {
2103       SiS_SetReg(SiS_Pr->SiS_P3d4,j,crt1data[i]);
2104    }
2105    for(j = 0x10; i <= 10; i++, j++) {
2106       SiS_SetReg(SiS_Pr->SiS_P3d4,j,crt1data[i]);
2107    }
2108    for(j = 0x15; i <= 12; i++, j++) {
2109       SiS_SetReg(SiS_Pr->SiS_P3d4,j,crt1data[i]);
2110    }
2111    for(j = 0x0A; i <= 15; i++, j++) {
2112       SiS_SetReg(SiS_Pr->SiS_P3c4,j,crt1data[i]);
2113    }
2114
2115    SiS_SetReg(SiS_Pr->SiS_P3c4,0x0E,crt1data[16] & 0xE0);
2116
2117    temp = (crt1data[16] & 0x01) << 5;
2118    if(modeflag & DoubleScanMode) temp |= 0x80;
2119    SiS_SetRegANDOR(SiS_Pr->SiS_P3d4,0x09,0x5F,temp);
2120
2121    if(SiS_Pr->SiS_ModeType > ModeVGA) {
2122       SiS_SetReg(SiS_Pr->SiS_P3d4,0x14,0x4F);
2123    }
2124
2125 #ifdef CONFIG_FB_SIS_315
2126    if(SiS_Pr->ChipType == XGI_20) {
2127       SiS_SetReg(SiS_Pr->SiS_P3d4,0x04,crt1data[4] - 1);
2128       if(!(temp = crt1data[5] & 0x1f)) {
2129          SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x0c,0xfb);
2130       }
2131       SiS_SetRegANDOR(SiS_Pr->SiS_P3d4,0x05,0xe0,((temp - 1) & 0x1f));
2132       temp = (crt1data[16] >> 5) + 3;
2133       if(temp > 7) temp -= 7;
2134       SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x0e,0x1f,(temp << 5));
2135    }
2136 #endif
2137 }
2138
2139 /*********************************************/
2140 /*               OFFSET & PITCH              */
2141 /*********************************************/
2142 /*  (partly overruled by SetPitch() in XF86) */
2143 /*********************************************/
2144
2145 static void
2146 SiS_SetCRT1Offset(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
2147                 unsigned short ModeIdIndex, unsigned short RRTI)
2148 {
2149    unsigned short temp, DisplayUnit, infoflag;
2150
2151    if(SiS_Pr->UseCustomMode) {
2152       infoflag = SiS_Pr->CInfoFlag;
2153    } else {
2154       infoflag = SiS_Pr->SiS_RefIndex[RRTI].Ext_InfoFlag;
2155    }
2156
2157    DisplayUnit = SiS_GetOffset(SiS_Pr, ModeNo, ModeIdIndex, RRTI);
2158
2159    temp = (DisplayUnit >> 8) & 0x0f;
2160    SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x0E,0xF0,temp);
2161
2162    SiS_SetReg(SiS_Pr->SiS_P3d4,0x13,DisplayUnit & 0xFF);
2163
2164    if(infoflag & InterlaceMode) DisplayUnit >>= 1;
2165
2166    DisplayUnit <<= 5;
2167    temp = (DisplayUnit >> 8) + 1;
2168    if(DisplayUnit & 0xff) temp++;
2169    if(SiS_Pr->ChipType == XGI_20) {
2170       if(ModeNo == 0x4a || ModeNo == 0x49) temp--;
2171    }
2172    SiS_SetReg(SiS_Pr->SiS_P3c4,0x10,temp);
2173 }
2174
2175 /*********************************************/
2176 /*                  VCLK                     */
2177 /*********************************************/
2178
2179 static void
2180 SiS_SetCRT1VCLK(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
2181                 unsigned short ModeIdIndex, unsigned short RRTI)
2182 {
2183    unsigned short index = 0, clka, clkb;
2184
2185    if(SiS_Pr->UseCustomMode) {
2186       clka = SiS_Pr->CSR2B;
2187       clkb = SiS_Pr->CSR2C;
2188    } else {
2189       index = SiS_GetVCLK2Ptr(SiS_Pr, ModeNo, ModeIdIndex, RRTI);
2190       if((SiS_Pr->SiS_VBType & VB_SIS30xBLV) &&
2191          (SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)) {
2192          /* Alternate for 1600x1200 LCDA */
2193          if((index == 0x21) && (SiS_Pr->Alternate1600x1200)) index = 0x72;
2194          clka = SiS_Pr->SiS_VBVCLKData[index].Part4_A;
2195          clkb = SiS_Pr->SiS_VBVCLKData[index].Part4_B;
2196       } else {
2197          clka = SiS_Pr->SiS_VCLKData[index].SR2B;
2198          clkb = SiS_Pr->SiS_VCLKData[index].SR2C;
2199       }
2200    }
2201
2202    SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x31,0xCF);
2203
2204    SiS_SetReg(SiS_Pr->SiS_P3c4,0x2b,clka);
2205    SiS_SetReg(SiS_Pr->SiS_P3c4,0x2c,clkb);
2206
2207    if(SiS_Pr->ChipType >= SIS_315H) {
2208 #ifdef CONFIG_FB_SIS_315
2209       SiS_SetReg(SiS_Pr->SiS_P3c4,0x2D,0x01);
2210       if(SiS_Pr->ChipType == XGI_20) {
2211          unsigned short mf = SiS_GetModeFlag(SiS_Pr, ModeNo, ModeIdIndex);
2212          if(mf & HalfDCLK) {
2213             SiS_SetReg(SiS_Pr->SiS_P3c4,0x2b,SiS_GetReg(SiS_Pr->SiS_P3c4,0x2b));
2214             clkb = SiS_GetReg(SiS_Pr->SiS_P3c4,0x2c);
2215             clkb = (((clkb & 0x1f) << 1) + 1) | (clkb & 0xe0);
2216             SiS_SetReg(SiS_Pr->SiS_P3c4,0x2c,clkb);
2217          }
2218       }
2219 #endif
2220    } else {
2221       SiS_SetReg(SiS_Pr->SiS_P3c4,0x2D,0x80);
2222    }
2223 }
2224
2225 /*********************************************/
2226 /*                  FIFO                     */
2227 /*********************************************/
2228
2229 #ifdef CONFIG_FB_SIS_300
2230 void
2231 SiS_GetFIFOThresholdIndex300(struct SiS_Private *SiS_Pr, unsigned short *idx1,
2232                 unsigned short *idx2)
2233 {
2234    unsigned short temp1, temp2;
2235    static const unsigned char ThTiming[8] = {
2236                 1, 2, 2, 3, 0, 1, 1, 2
2237    };
2238
2239    temp1 = temp2 = (SiS_GetReg(SiS_Pr->SiS_P3c4,0x18) & 0x62) >> 1;
2240    (*idx2) = (unsigned short)(ThTiming[((temp2 >> 3) | temp1) & 0x07]);
2241    (*idx1) = (unsigned short)(SiS_GetReg(SiS_Pr->SiS_P3c4,0x16) >> 6) & 0x03;
2242    (*idx1) |= (unsigned short)(((SiS_GetReg(SiS_Pr->SiS_P3c4,0x14) >> 4) & 0x0c));
2243    (*idx1) <<= 1;
2244 }
2245
2246 static unsigned short
2247 SiS_GetFIFOThresholdA300(unsigned short idx1, unsigned short idx2)
2248 {
2249    static const unsigned char ThLowA[8 * 3] = {
2250                 61, 3,52, 5,68, 7,100,11,
2251                 43, 3,42, 5,54, 7, 78,11,
2252                 34, 3,37, 5,47, 7, 67,11
2253    };
2254
2255    return (unsigned short)((ThLowA[idx1 + 1] * idx2) + ThLowA[idx1]);
2256 }
2257
2258 unsigned short
2259 SiS_GetFIFOThresholdB300(unsigned short idx1, unsigned short idx2)
2260 {
2261    static const unsigned char ThLowB[8 * 3] = {
2262                 81, 4,72, 6,88, 8,120,12,
2263                 55, 4,54, 6,66, 8, 90,12,
2264                 42, 4,45, 6,55, 8, 75,12
2265    };
2266
2267    return (unsigned short)((ThLowB[idx1 + 1] * idx2) + ThLowB[idx1]);
2268 }
2269
2270 static unsigned short
2271 SiS_DoCalcDelay(struct SiS_Private *SiS_Pr, unsigned short MCLK, unsigned short VCLK,
2272                 unsigned short colordepth, unsigned short key)
2273 {
2274    unsigned short idx1, idx2;
2275    unsigned int   longtemp = VCLK * colordepth;
2276
2277    SiS_GetFIFOThresholdIndex300(SiS_Pr, &idx1, &idx2);
2278
2279    if(key == 0) {
2280       longtemp *= SiS_GetFIFOThresholdA300(idx1, idx2);
2281    } else {
2282       longtemp *= SiS_GetFIFOThresholdB300(idx1, idx2);
2283    }
2284    idx1 = longtemp % (MCLK * 16);
2285    longtemp /= (MCLK * 16);
2286    if(idx1) longtemp++;
2287    return (unsigned short)longtemp;
2288 }
2289
2290 static unsigned short
2291 SiS_CalcDelay(struct SiS_Private *SiS_Pr, unsigned short VCLK,
2292                 unsigned short colordepth, unsigned short MCLK)
2293 {
2294    unsigned short temp1, temp2;
2295
2296    temp2 = SiS_DoCalcDelay(SiS_Pr, MCLK, VCLK, colordepth, 0);
2297    temp1 = SiS_DoCalcDelay(SiS_Pr, MCLK, VCLK, colordepth, 1);
2298    if(temp1 < 4) temp1 = 4;
2299    temp1 -= 4;
2300    if(temp2 < temp1) temp2 = temp1;
2301    return temp2;
2302 }
2303
2304 static void
2305 SiS_SetCRT1FIFO_300(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
2306                 unsigned short RefreshRateTableIndex)
2307 {
2308    unsigned short ThresholdLow = 0;
2309    unsigned short temp, index, VCLK, MCLK, colorth;
2310    static const unsigned short colortharray[6] = { 1, 1, 2, 2, 3, 4 };
2311
2312    if(ModeNo > 0x13) {
2313
2314       /* Get VCLK  */
2315       if(SiS_Pr->UseCustomMode) {
2316          VCLK = SiS_Pr->CSRClock;
2317       } else {
2318          index = SiS_GetRefCRTVCLK(SiS_Pr, RefreshRateTableIndex, SiS_Pr->SiS_UseWide);
2319          VCLK = SiS_Pr->SiS_VCLKData[index].CLOCK;
2320       }
2321
2322       /* Get half colordepth */
2323       colorth = colortharray[(SiS_Pr->SiS_ModeType - ModeEGA)];
2324
2325       /* Get MCLK  */
2326       index = SiS_GetReg(SiS_Pr->SiS_P3c4,0x3A) & 0x07;
2327       MCLK = SiS_Pr->SiS_MCLKData_0[index].CLOCK;
2328
2329       temp = SiS_GetReg(SiS_Pr->SiS_P3d4,0x35) & 0xc3;
2330       SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x16,0x3c,temp);
2331
2332       do {
2333          ThresholdLow = SiS_CalcDelay(SiS_Pr, VCLK, colorth, MCLK) + 1;
2334          if(ThresholdLow < 0x13) break;
2335          SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x16,0xfc);
2336          ThresholdLow = 0x13;
2337          temp = SiS_GetReg(SiS_Pr->SiS_P3c4,0x16) >> 6;
2338          if(!temp) break;
2339          SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x16,0x3f,((temp - 1) << 6));
2340       } while(0);
2341
2342    } else ThresholdLow = 2;
2343
2344    /* Write CRT/CPU threshold low, CRT/Engine threshold high */
2345    temp = (ThresholdLow << 4) | 0x0f;
2346    SiS_SetReg(SiS_Pr->SiS_P3c4,0x08,temp);
2347
2348    temp = (ThresholdLow & 0x10) << 1;
2349    if(ModeNo > 0x13) temp |= 0x40;
2350    SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x0f,0x9f,temp);
2351
2352    /* What is this? */
2353    SiS_SetReg(SiS_Pr->SiS_P3c4,0x3B,0x09);
2354
2355    /* Write CRT/CPU threshold high */
2356    temp = ThresholdLow + 3;
2357    if(temp > 0x0f) temp = 0x0f;
2358    SiS_SetReg(SiS_Pr->SiS_P3c4,0x09,temp);
2359 }
2360
2361 unsigned short
2362 SiS_GetLatencyFactor630(struct SiS_Private *SiS_Pr, unsigned short index)
2363 {
2364    static const unsigned char LatencyFactor[] = {
2365                 97, 88, 86, 79, 77,  0,       /* 64  bit    BQ=2   */
2366                  0, 87, 85, 78, 76, 54,       /* 64  bit    BQ=1   */
2367                 97, 88, 86, 79, 77,  0,       /* 128 bit    BQ=2   */
2368                  0, 79, 77, 70, 68, 48,       /* 128 bit    BQ=1   */
2369                 80, 72, 69, 63, 61,  0,       /* 64  bit    BQ=2   */
2370                  0, 70, 68, 61, 59, 37,       /* 64  bit    BQ=1   */
2371                 86, 77, 75, 68, 66,  0,       /* 128 bit    BQ=2   */
2372                  0, 68, 66, 59, 57, 37        /* 128 bit    BQ=1   */
2373    };
2374    static const unsigned char LatencyFactor730[] = {
2375                  69, 63, 61,
2376                  86, 79, 77,
2377                 103, 96, 94,
2378                 120,113,111,
2379                 137,130,128
2380    };
2381
2382    if(SiS_Pr->ChipType == SIS_730) {
2383       return (unsigned short)LatencyFactor730[index];
2384    } else {
2385       return (unsigned short)LatencyFactor[index];
2386    }
2387 }
2388
2389 static unsigned short
2390 SiS_CalcDelay2(struct SiS_Private *SiS_Pr, unsigned char key)
2391 {
2392    unsigned short index;
2393
2394    if(SiS_Pr->ChipType == SIS_730) {
2395       index = ((key & 0x0f) * 3) + ((key & 0xc0) >> 6);
2396    } else {
2397       index = (key & 0xe0) >> 5;
2398       if(key & 0x10)    index +=  6;
2399       if(!(key & 0x01)) index += 24;
2400       if(SiS_GetReg(SiS_Pr->SiS_P3c4,0x14) & 0x80) index += 12;
2401    }
2402    return SiS_GetLatencyFactor630(SiS_Pr, index);
2403 }
2404
2405 static void
2406 SiS_SetCRT1FIFO_630(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
2407                     unsigned short RefreshRateTableIndex)
2408 {
2409    unsigned short  ThresholdLow = 0;
2410    unsigned short  i, data, VCLK, MCLK16, colorth = 0;
2411    unsigned int    templ, datal;
2412    const unsigned char *queuedata = NULL;
2413    static const unsigned char FQBQData[21] = {
2414                 0x01,0x21,0x41,0x61,0x81,
2415                 0x31,0x51,0x71,0x91,0xb1,
2416                 0x00,0x20,0x40,0x60,0x80,
2417                 0x30,0x50,0x70,0x90,0xb0,
2418                 0xff
2419    };
2420    static const unsigned char FQBQData730[16] = {
2421                 0x34,0x74,0xb4,
2422                 0x23,0x63,0xa3,
2423                 0x12,0x52,0x92,
2424                 0x01,0x41,0x81,
2425                 0x00,0x40,0x80,
2426                 0xff
2427    };
2428    static const unsigned short colortharray[6] = {
2429                 1, 1, 2, 2, 3, 4
2430    };
2431
2432    i = 0;
2433
2434    if(ModeNo > 0x13) {
2435
2436       /* Get VCLK  */
2437       if(SiS_Pr->UseCustomMode) {
2438          VCLK = SiS_Pr->CSRClock;
2439       } else {
2440          data = SiS_GetRefCRTVCLK(SiS_Pr, RefreshRateTableIndex, SiS_Pr->SiS_UseWide);
2441          VCLK = SiS_Pr->SiS_VCLKData[data].CLOCK;
2442       }
2443
2444       /* Get MCLK * 16 */
2445       data = SiS_GetReg(SiS_Pr->SiS_P3c4,0x1A) & 0x07;
2446       MCLK16 = SiS_Pr->SiS_MCLKData_0[data].CLOCK * 16;
2447
2448       /* Get half colordepth */
2449       colorth = colortharray[(SiS_Pr->SiS_ModeType - ModeEGA)];
2450
2451       if(SiS_Pr->ChipType == SIS_730) {
2452          queuedata = &FQBQData730[0];
2453       } else {
2454          queuedata = &FQBQData[0];
2455       }
2456
2457       do {
2458          templ = SiS_CalcDelay2(SiS_Pr, queuedata[i]) * VCLK * colorth;
2459
2460          datal = templ % MCLK16;
2461          templ = (templ / MCLK16) + 1;
2462          if(datal) templ++;
2463
2464          if(templ > 0x13) {
2465             if(queuedata[i + 1] == 0xFF) {
2466                ThresholdLow = 0x13;
2467                break;
2468             }
2469             i++;
2470          } else {
2471             ThresholdLow = templ;
2472             break;
2473          }
2474       } while(queuedata[i] != 0xFF);
2475
2476    } else {
2477
2478       if(SiS_Pr->ChipType != SIS_730) i = 9;
2479       ThresholdLow = 0x02;
2480
2481    }
2482
2483    /* Write CRT/CPU threshold low, CRT/Engine threshold high */
2484    data = ((ThresholdLow & 0x0f) << 4) | 0x0f;
2485    SiS_SetReg(SiS_Pr->SiS_P3c4,0x08,data);
2486
2487    data = (ThresholdLow & 0x10) << 1;
2488    SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x0F,0xDF,data);
2489
2490    /* What is this? */
2491    SiS_SetReg(SiS_Pr->SiS_P3c4,0x3B,0x09);
2492
2493    /* Write CRT/CPU threshold high (gap = 3) */
2494    data = ThresholdLow + 3;
2495    if(data > 0x0f) data = 0x0f;
2496    SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x09,0x80,data);
2497
2498   /* Write foreground and background queue */
2499    templ = sisfb_read_nbridge_pci_dword(SiS_Pr, 0x50);
2500
2501    if(SiS_Pr->ChipType == SIS_730) {
2502
2503       templ &= 0xfffff9ff;
2504       templ |= ((queuedata[i] & 0xc0) << 3);
2505
2506    } else {
2507
2508       templ &= 0xf0ffffff;
2509       if( (ModeNo <= 0x13) &&
2510           (SiS_Pr->ChipType == SIS_630) &&
2511           (SiS_Pr->ChipRevision >= 0x30) ) {
2512          templ |= 0x0b000000;
2513       } else {
2514          templ |= ((queuedata[i] & 0xf0) << 20);
2515       }
2516
2517    }
2518
2519    sisfb_write_nbridge_pci_dword(SiS_Pr, 0x50, templ);
2520    templ = sisfb_read_nbridge_pci_dword(SiS_Pr, 0xA0);
2521
2522    /* GUI grant timer (PCI config 0xA3) */
2523    if(SiS_Pr->ChipType == SIS_730) {
2524
2525       templ &= 0x00ffffff;
2526       datal = queuedata[i] << 8;
2527       templ |= (((datal & 0x0f00) | ((datal & 0x3000) >> 8)) << 20);
2528
2529    } else {
2530
2531       templ &= 0xf0ffffff;
2532       templ |= ((queuedata[i] & 0x0f) << 24);
2533
2534    }
2535
2536    sisfb_write_nbridge_pci_dword(SiS_Pr, 0xA0, templ);
2537 }
2538 #endif /* CONFIG_FB_SIS_300 */
2539
2540 #ifdef CONFIG_FB_SIS_315
2541 static void
2542 SiS_SetCRT1FIFO_310(struct SiS_Private *SiS_Pr, unsigned short ModeNo, unsigned short ModeIdIndex)
2543 {
2544    unsigned short modeflag;
2545
2546    /* disable auto-threshold */
2547    SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x3D,0xFE);
2548
2549    modeflag = SiS_GetModeFlag(SiS_Pr, ModeNo, ModeIdIndex);
2550
2551    SiS_SetReg(SiS_Pr->SiS_P3c4,0x08,0xAE);
2552    SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x09,0xF0);
2553    if(ModeNo > 0x13) {
2554       if(SiS_Pr->ChipType >= XGI_20) {
2555          SiS_SetReg(SiS_Pr->SiS_P3c4,0x08,0x34);
2556          SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x3D,0x01);
2557       } else if(SiS_Pr->ChipType >= SIS_661) {
2558          if(!(modeflag & HalfDCLK)) {
2559             SiS_SetReg(SiS_Pr->SiS_P3c4,0x08,0x34);
2560             SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x3D,0x01);
2561          }
2562       } else {
2563          if((!(modeflag & DoubleScanMode)) || (!(modeflag & HalfDCLK))) {
2564             SiS_SetReg(SiS_Pr->SiS_P3c4,0x08,0x34);
2565             SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x3D,0x01);
2566          }
2567       }
2568    }
2569 }
2570 #endif
2571
2572 /*********************************************/
2573 /*              MODE REGISTERS               */
2574 /*********************************************/
2575
2576 static void
2577 SiS_SetVCLKState(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
2578                 unsigned short RefreshRateTableIndex, unsigned short ModeIdIndex)
2579 {
2580    unsigned short data = 0, VCLK = 0, index = 0;
2581
2582    if(ModeNo > 0x13) {
2583       if(SiS_Pr->UseCustomMode) {
2584          VCLK = SiS_Pr->CSRClock;
2585       } else {
2586          index = SiS_GetVCLK2Ptr(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex);
2587          VCLK = SiS_Pr->SiS_VCLKData[index].CLOCK;
2588       }
2589    }
2590
2591    if(SiS_Pr->ChipType < SIS_315H) {
2592 #ifdef CONFIG_FB_SIS_300
2593       if(VCLK > 150) data |= 0x80;
2594       SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x07,0x7B,data);
2595
2596       data = 0x00;
2597       if(VCLK >= 150) data |= 0x08;
2598       SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x32,0xF7,data);
2599 #endif
2600    } else if(SiS_Pr->ChipType < XGI_20) {
2601 #ifdef CONFIG_FB_SIS_315
2602       if(VCLK >= 166) data |= 0x0c;
2603       SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x32,0xf3,data);
2604
2605       if(VCLK >= 166) {
2606          SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x1f,0xe7);
2607       }
2608 #endif
2609    } else {
2610 #ifdef CONFIG_FB_SIS_315
2611       if(VCLK >= 200) data |= 0x0c;
2612       if(SiS_Pr->ChipType == XGI_20) data &= ~0x04;
2613       SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x32,0xf3,data);
2614       if(SiS_Pr->ChipType != XGI_20) {
2615          data = SiS_GetReg(SiS_Pr->SiS_P3c4,0x1f) & 0xe7;
2616          if(VCLK < 200) data |= 0x10;
2617          SiS_SetReg(SiS_Pr->SiS_P3c4,0x1f,data);
2618       }
2619 #endif
2620    }
2621
2622    /* DAC speed */
2623    if(SiS_Pr->ChipType >= SIS_661) {
2624
2625       SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x07,0xE8,0x10);
2626
2627    } else {
2628
2629       data = 0x03;
2630       if(VCLK >= 260)      data = 0x00;
2631       else if(VCLK >= 160) data = 0x01;
2632       else if(VCLK >= 135) data = 0x02;
2633
2634       if(SiS_Pr->ChipType == SIS_540) {
2635          if((VCLK == 203) || (VCLK < 234)) data = 0x02;
2636       }
2637
2638       if(SiS_Pr->ChipType < SIS_315H) {
2639          SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x07,0xFC,data);
2640       } else {
2641          if(SiS_Pr->ChipType > SIS_315PRO) {
2642             if(ModeNo > 0x13) data &= 0xfc;
2643          }
2644          SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x07,0xF8,data);
2645       }
2646
2647    }
2648 }
2649
2650 static void
2651 SiS_SetCRT1ModeRegs(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
2652                 unsigned short ModeIdIndex, unsigned short RRTI)
2653 {
2654    unsigned short data, infoflag = 0, modeflag, resindex;
2655 #ifdef CONFIG_FB_SIS_315
2656    unsigned char  *ROMAddr  = SiS_Pr->VirtualRomBase;
2657    unsigned short data2, data3;
2658 #endif
2659
2660    modeflag = SiS_GetModeFlag(SiS_Pr, ModeNo, ModeIdIndex);
2661
2662    if(SiS_Pr->UseCustomMode) {
2663       infoflag = SiS_Pr->CInfoFlag;
2664    } else {
2665       resindex = SiS_GetResInfo(SiS_Pr, ModeNo, ModeIdIndex);
2666       if(ModeNo > 0x13) {
2667          infoflag = SiS_Pr->SiS_RefIndex[RRTI].Ext_InfoFlag;
2668       }
2669    }
2670
2671    /* Disable DPMS */
2672    SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x1F,0x3F);
2673
2674    data = 0;
2675    if(ModeNo > 0x13) {
2676       if(SiS_Pr->SiS_ModeType > ModeEGA) {
2677          data |= 0x02;
2678          data |= ((SiS_Pr->SiS_ModeType - ModeVGA) << 2);
2679       }
2680       if(infoflag & InterlaceMode) data |= 0x20;
2681    }
2682    SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x06,0xC0,data);
2683
2684    if(SiS_Pr->ChipType != SIS_300) {
2685       data = 0;
2686       if(infoflag & InterlaceMode) {
2687          /* data = (Hsync / 8) - ((Htotal / 8) / 2) + 3 */
2688          int hrs = (SiS_GetReg(SiS_Pr->SiS_P3d4,0x04) |
2689                     ((SiS_GetReg(SiS_Pr->SiS_P3c4,0x0b) & 0xc0) << 2)) - 3;
2690          int hto = (SiS_GetReg(SiS_Pr->SiS_P3d4,0x00) |
2691                     ((SiS_GetReg(SiS_Pr->SiS_P3c4,0x0b) & 0x03) << 8)) + 5;
2692          data = hrs - (hto >> 1) + 3;
2693       }
2694       SiS_SetReg(SiS_Pr->SiS_P3d4,0x19,data);
2695       SiS_SetRegANDOR(SiS_Pr->SiS_P3d4,0x1a,0xFC,((data >> 8) & 0x03));
2696    }
2697
2698    if(modeflag & HalfDCLK) {
2699       SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x01,0x08);
2700    }
2701
2702    data = 0;
2703    if(modeflag & LineCompareOff) data = 0x08;
2704    if(SiS_Pr->ChipType == SIS_300) {
2705       SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x0F,0xF7,data);
2706    } else {
2707       if(SiS_Pr->ChipType >= XGI_20) data |= 0x20;
2708       if(SiS_Pr->SiS_ModeType == ModeEGA) {
2709          if(ModeNo > 0x13) {
2710             data |= 0x40;
2711          }
2712       }
2713       SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x0F,0xB7,data);
2714    }
2715
2716 #ifdef CONFIG_FB_SIS_315
2717    if(SiS_Pr->ChipType >= SIS_315H) {
2718       SiS_SetRegAND(SiS_Pr->SiS_P3c4,0x31,0xfb);
2719    }
2720
2721    if(SiS_Pr->ChipType == SIS_315PRO) {
2722
2723       data = SiS_Pr->SiS_SR15[(2 * 4) + SiS_Get310DRAMType(SiS_Pr)];
2724       if(SiS_Pr->SiS_ModeType == ModeText) {
2725          data &= 0xc7;
2726       } else {
2727          data2 = SiS_GetOffset(SiS_Pr, ModeNo, ModeIdIndex, RRTI) >> 1;
2728          if(infoflag & InterlaceMode) data2 >>= 1;
2729          data3 = SiS_GetColorDepth(SiS_Pr, ModeNo, ModeIdIndex) >> 1;
2730          if(data3) data2 /= data3;
2731          if(data2 >= 0x50) {
2732             data &= 0x0f;
2733             data |= 0x50;
2734          }
2735       }
2736       SiS_SetReg(SiS_Pr->SiS_P3c4,0x17,data);
2737
2738    } else if((SiS_Pr->ChipType == SIS_330) || (SiS_Pr->SiS_SysFlags & SF_760LFB)) {
2739
2740       data = SiS_Get310DRAMType(SiS_Pr);
2741       if(SiS_Pr->ChipType == SIS_330) {
2742          data = SiS_Pr->SiS_SR15[(2 * 4) + data];
2743       } else {
2744          if(SiS_Pr->SiS_ROMNew)      data = ROMAddr[0xf6];
2745          else if(SiS_Pr->SiS_UseROM) data = ROMAddr[0x100 + data];
2746          else                        data = 0xba;
2747       }
2748       if(SiS_Pr->SiS_ModeType <= ModeEGA) {
2749          data &= 0xc7;
2750       } else {
2751          if(SiS_Pr->UseCustomMode) {
2752             data2 = SiS_Pr->CSRClock;
2753          } else {
2754             data2 = SiS_GetVCLK2Ptr(SiS_Pr, ModeNo, ModeIdIndex, RRTI);
2755             data2 = SiS_Pr->SiS_VCLKData[data2].CLOCK;
2756          }
2757
2758          data3 = SiS_GetColorDepth(SiS_Pr, ModeNo, ModeIdIndex) >> 1;
2759          if(data3) data2 *= data3;
2760
2761          data2 = ((unsigned int)(SiS_GetMCLK(SiS_Pr) * 1024)) / data2;
2762
2763          if(SiS_Pr->ChipType == SIS_330) {
2764             if(SiS_Pr->SiS_ModeType != Mode16Bpp) {
2765                if     (data2 >= 0x19c) data = 0xba;
2766                else if(data2 >= 0x140) data = 0x7a;
2767                else if(data2 >= 0x101) data = 0x3a;
2768                else if(data2 >= 0xf5)  data = 0x32;
2769                else if(data2 >= 0xe2)  data = 0x2a;
2770                else if(data2 >= 0xc4)  data = 0x22;
2771                else if(data2 >= 0xac)  data = 0x1a;
2772                else if(data2 >= 0x9e)  data = 0x12;
2773                else if(data2 >= 0x8e)  data = 0x0a;
2774                else                    data = 0x02;
2775             } else {
2776                if(data2 >= 0x127)      data = 0xba;
2777                else                    data = 0x7a;
2778             }
2779          } else {  /* 76x+LFB */
2780             if     (data2 >= 0x190) data = 0xba;
2781             else if(data2 >= 0xff)  data = 0x7a;
2782             else if(data2 >= 0xd3)  data = 0x3a;
2783             else if(data2 >= 0xa9)  data = 0x1a;
2784             else if(data2 >= 0x93)  data = 0x0a;
2785             else                    data = 0x02;
2786          }
2787       }
2788       SiS_SetReg(SiS_Pr->SiS_P3c4,0x17,data);
2789
2790    }
2791       /* XGI: Nothing. */
2792       /* TODO: Check SiS340 */
2793 #endif
2794
2795    data = 0x60;
2796    if(SiS_Pr->SiS_ModeType != ModeText) {
2797       data ^= 0x60;
2798       if(SiS_Pr->SiS_ModeType != ModeEGA) {
2799          data ^= 0xA0;
2800       }
2801    }
2802    SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x21,0x1F,data);
2803
2804    SiS_SetVCLKState(SiS_Pr, ModeNo, RRTI, ModeIdIndex);
2805
2806 #ifdef CONFIG_FB_SIS_315
2807    if(((SiS_Pr->ChipType >= SIS_315H) && (SiS_Pr->ChipType < SIS_661)) ||
2808        (SiS_Pr->ChipType == XGI_40)) {
2809       if(SiS_GetReg(SiS_Pr->SiS_P3d4,0x31) & 0x40) {
2810          SiS_SetReg(SiS_Pr->SiS_P3d4,0x52,0x2c);
2811       } else {
2812          SiS_SetReg(SiS_Pr->SiS_P3d4,0x52,0x6c);
2813       }
2814    } else if(SiS_Pr->ChipType == XGI_20) {
2815       if(SiS_GetReg(SiS_Pr->SiS_P3d4,0x31) & 0x40) {
2816          SiS_SetReg(SiS_Pr->SiS_P3d4,0x52,0x33);
2817       } else {
2818          SiS_SetReg(SiS_Pr->SiS_P3d4,0x52,0x73);
2819       }
2820       SiS_SetReg(SiS_Pr->SiS_P3d4,0x51,0x02);
2821    }
2822 #endif
2823 }
2824
2825 #ifdef CONFIG_FB_SIS_315
2826 static void
2827 SiS_SetupDualChip(struct SiS_Private *SiS_Pr)
2828 {
2829 #if 0
2830    /* TODO: Find out about IOAddress2 */
2831    SISIOADDRESS P2_3c2 = SiS_Pr->IOAddress2 + 0x12;
2832    SISIOADDRESS P2_3c4 = SiS_Pr->IOAddress2 + 0x14;
2833    SISIOADDRESS P2_3ce = SiS_Pr->IOAddress2 + 0x1e;
2834    int i;
2835
2836    if((SiS_Pr->ChipRevision != 0) ||
2837       (!(SiS_GetReg(SiS_Pr->SiS_P3c4,0x3a) & 0x04)))
2838       return;
2839
2840    for(i = 0; i <= 4; i++) {                                    /* SR00 - SR04 */
2841       SiS_SetReg(P2_3c4,i,SiS_GetReg(SiS_Pr->SiS_P3c4,i));
2842    }
2843    for(i = 0; i <= 8; i++) {                                    /* GR00 - GR08 */
2844       SiS_SetReg(P2_3ce,i,SiS_GetReg(SiS_Pr->SiS_P3ce,i));
2845    }
2846    SiS_SetReg(P2_3c4,0x05,0x86);
2847    SiS_SetReg(P2_3c4,0x06,SiS_GetReg(SiS_Pr->SiS_P3c4,0x06));   /* SR06 */
2848    SiS_SetReg(P2_3c4,0x21,SiS_GetReg(SiS_Pr->SiS_P3c4,0x21));   /* SR21 */
2849    SiS_SetRegByte(P2_3c2,SiS_GetRegByte(SiS_Pr->SiS_P3cc));     /* MISC */
2850    SiS_SetReg(P2_3c4,0x05,0x00);
2851 #endif
2852 }
2853 #endif
2854
2855 /*********************************************/
2856 /*                 LOAD DAC                  */
2857 /*********************************************/
2858
2859 static void
2860 SiS_WriteDAC(struct SiS_Private *SiS_Pr, SISIOADDRESS DACData, unsigned short shiftflag,
2861              unsigned short dl, unsigned short ah, unsigned short al, unsigned short dh)
2862 {
2863    unsigned short d1, d2, d3;
2864
2865    switch(dl) {
2866    case  0: d1 = dh; d2 = ah; d3 = al; break;
2867    case  1: d1 = ah; d2 = al; d3 = dh; break;
2868    default: d1 = al; d2 = dh; d3 = ah;
2869    }
2870    SiS_SetRegByte(DACData, (d1 << shiftflag));
2871    SiS_SetRegByte(DACData, (d2 << shiftflag));
2872    SiS_SetRegByte(DACData, (d3 << shiftflag));
2873 }
2874
2875 void
2876 SiS_LoadDAC(struct SiS_Private *SiS_Pr, unsigned short ModeNo, unsigned short ModeIdIndex)
2877 {
2878    unsigned short data, data2, time, i, j, k, m, n, o;
2879    unsigned short si, di, bx, sf;
2880    SISIOADDRESS DACAddr, DACData;
2881    const unsigned char *table = NULL;
2882
2883    data = SiS_GetModeFlag(SiS_Pr, ModeNo, ModeIdIndex) & DACInfoFlag;
2884
2885    j = time = 64;
2886    if(data == 0x00)      table = SiS_MDA_DAC;
2887    else if(data == 0x08) table = SiS_CGA_DAC;
2888    else if(data == 0x10) table = SiS_EGA_DAC;
2889    else if(data == 0x18) {
2890       j = 16;
2891       time = 256;
2892       table = SiS_VGA_DAC;
2893    }
2894
2895    if( ( (SiS_Pr->SiS_VBInfo & SetCRT2ToLCD) &&        /* 301B-DH LCD */
2896          (SiS_Pr->SiS_VBType & VB_NoLCD) )        ||
2897        (SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)       ||   /* LCDA */
2898        (!(SiS_Pr->SiS_SetFlag & ProgrammingCRT2)) ) {  /* Programming CRT1 */
2899       SiS_SetRegByte(SiS_Pr->SiS_P3c6,0xFF);
2900       DACAddr = SiS_Pr->SiS_P3c8;
2901       DACData = SiS_Pr->SiS_P3c9;
2902       sf = 0;
2903    } else {
2904       DACAddr = SiS_Pr->SiS_Part5Port;
2905       DACData = SiS_Pr->SiS_Part5Port + 1;
2906       sf = 2;
2907    }
2908
2909    SiS_SetRegByte(DACAddr,0x00);
2910
2911    for(i = 0; i < j; i++) {
2912       data = table[i];
2913       for(k = 0; k < 3; k++) {
2914         data2 = 0;
2915         if(data & 0x01) data2 += 0x2A;
2916         if(data & 0x02) data2 += 0x15;
2917         SiS_SetRegByte(DACData, (data2 << sf));
2918         data >>= 2;
2919       }
2920    }
2921
2922    if(time == 256) {
2923       for(i = 16; i < 32; i++) {
2924          data = table[i] << sf;
2925          for(k = 0; k < 3; k++) SiS_SetRegByte(DACData, data);
2926       }
2927       si = 32;
2928       for(m = 0; m < 9; m++) {
2929          di = si;
2930          bx = si + 4;
2931          for(n = 0; n < 3; n++) {
2932             for(o = 0; o < 5; o++) {
2933                SiS_WriteDAC(SiS_Pr, DACData, sf, n, table[di], table[bx], table[si]);
2934                si++;
2935             }
2936             si -= 2;
2937             for(o = 0; o < 3; o++) {
2938                SiS_WriteDAC(SiS_Pr, DACData, sf, n, table[di], table[si], table[bx]);
2939                si--;
2940             }
2941          }            /* for n < 3 */
2942          si += 5;
2943       }               /* for m < 9 */
2944    }
2945 }
2946
2947 /*********************************************/
2948 /*         SET CRT1 REGISTER GROUP           */
2949 /*********************************************/
2950
2951 static void
2952 SiS_SetCRT1Group(struct SiS_Private *SiS_Pr, unsigned short ModeNo, unsigned short ModeIdIndex)
2953 {
2954    unsigned short StandTableIndex, RefreshRateTableIndex;
2955
2956    SiS_Pr->SiS_CRT1Mode = ModeNo;
2957
2958    StandTableIndex = SiS_GetModePtr(SiS_Pr, ModeNo, ModeIdIndex);
2959
2960    if(SiS_Pr->SiS_SetFlag & LowModeTests) {
2961       if(SiS_Pr->SiS_VBInfo & (SetSimuScanMode | SwitchCRT2)) {
2962          SiS_DisableBridge(SiS_Pr);
2963       }
2964    }
2965
2966    SiS_ResetSegmentRegisters(SiS_Pr);
2967
2968    SiS_SetSeqRegs(SiS_Pr, StandTableIndex);
2969    SiS_SetMiscRegs(SiS_Pr, StandTableIndex);
2970    SiS_SetCRTCRegs(SiS_Pr, StandTableIndex);
2971    SiS_SetATTRegs(SiS_Pr, StandTableIndex);
2972    SiS_SetGRCRegs(SiS_Pr, StandTableIndex);
2973    SiS_ClearExt1Regs(SiS_Pr, ModeNo);
2974    SiS_ResetCRT1VCLK(SiS_Pr);
2975
2976    SiS_Pr->SiS_SelectCRT2Rate = 0;
2977    SiS_Pr->SiS_SetFlag &= (~ProgrammingCRT2);
2978
2979    if(SiS_Pr->SiS_VBInfo & SetSimuScanMode) {
2980       if(SiS_Pr->SiS_VBInfo & SetInSlaveMode) {
2981          SiS_Pr->SiS_SetFlag |= ProgrammingCRT2;
2982       }
2983    }
2984
2985    if(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA) {
2986       SiS_Pr->SiS_SetFlag |= ProgrammingCRT2;
2987    }
2988
2989    RefreshRateTableIndex = SiS_GetRatePtr(SiS_Pr, ModeNo, ModeIdIndex);
2990
2991    if(!(SiS_Pr->SiS_VBInfo & SetCRT2ToLCDA)) {
2992       SiS_Pr->SiS_SetFlag &= ~ProgrammingCRT2;
2993    }
2994
2995    if(RefreshRateTableIndex != 0xFFFF) {
2996       SiS_SetCRT1Sync(SiS_Pr, RefreshRateTableIndex);
2997       SiS_SetCRT1CRTC(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex);
2998       SiS_SetCRT1Offset(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex);
2999       SiS_SetCRT1VCLK(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex);
3000    }
3001
3002    switch(SiS_Pr->ChipType) {
3003 #ifdef CONFIG_FB_SIS_300
3004    case SIS_300:
3005       SiS_SetCRT1FIFO_300(SiS_Pr, ModeNo, RefreshRateTableIndex);
3006       break;
3007    case SIS_540:
3008    case SIS_630:
3009    case SIS_730:
3010       SiS_SetCRT1FIFO_630(SiS_Pr, ModeNo, RefreshRateTableIndex);
3011       break;
3012 #endif
3013    default:
3014 #ifdef CONFIG_FB_SIS_315
3015       if(SiS_Pr->ChipType == XGI_20) {
3016          unsigned char sr2b = 0, sr2c = 0;
3017          switch(ModeNo) {
3018          case 0x00:
3019          case 0x01: sr2b = 0x4e; sr2c = 0xe9; break;
3020          case 0x04:
3021          case 0x05:
3022          case 0x0d: sr2b = 0x1b; sr2c = 0xe3; break;
3023          }
3024          if(sr2b) {
3025             SiS_SetReg(SiS_Pr->SiS_P3c4,0x2b,sr2b);
3026             SiS_SetReg(SiS_Pr->SiS_P3c4,0x2c,sr2c);
3027             SiS_SetRegByte(SiS_Pr->SiS_P3c2,(SiS_GetRegByte(SiS_Pr->SiS_P3cc) | 0x0c));
3028          }
3029       }
3030       SiS_SetCRT1FIFO_310(SiS_Pr, ModeNo, ModeIdIndex);
3031 #endif
3032       break;
3033    }
3034
3035    SiS_SetCRT1ModeRegs(SiS_Pr, ModeNo, ModeIdIndex, RefreshRateTableIndex);
3036
3037 #ifdef CONFIG_FB_SIS_315
3038    if(SiS_Pr->ChipType == XGI_40) {
3039       SiS_SetupDualChip(SiS_Pr);
3040    }
3041 #endif
3042
3043    SiS_LoadDAC(SiS_Pr, ModeNo, ModeIdIndex);
3044
3045    if(SiS_Pr->SiS_flag_clearbuffer) {
3046       SiS_ClearBuffer(SiS_Pr, ModeNo);
3047    }
3048
3049    if(!(SiS_Pr->SiS_VBInfo & (SetSimuScanMode | SwitchCRT2 | SetCRT2ToLCDA))) {
3050       SiS_WaitRetrace1(SiS_Pr);
3051       SiS_DisplayOn(SiS_Pr);
3052    }
3053 }
3054
3055 /*********************************************/
3056 /*       HELPER: VIDEO BRIDGE PROG CLK       */
3057 /*********************************************/
3058
3059 static void
3060 SiS_InitVB(struct SiS_Private *SiS_Pr)
3061 {
3062    unsigned char *ROMAddr = SiS_Pr->VirtualRomBase;
3063
3064    SiS_Pr->Init_P4_0E = 0;
3065    if(SiS_Pr->SiS_ROMNew) {
3066       SiS_Pr->Init_P4_0E = ROMAddr[0x82];
3067    } else if(SiS_Pr->ChipType >= XGI_40) {
3068       if(SiS_Pr->SiS_XGIROM) {
3069          SiS_Pr->Init_P4_0E = ROMAddr[0x80];
3070       }
3071    }
3072 }
3073
3074 static void
3075 SiS_ResetVB(struct SiS_Private *SiS_Pr)
3076 {
3077 #ifdef CONFIG_FB_SIS_315
3078    unsigned char  *ROMAddr = SiS_Pr->VirtualRomBase;
3079    unsigned short temp;
3080
3081    /* VB programming clock */
3082    if(SiS_Pr->SiS_UseROM) {
3083       if(SiS_Pr->ChipType < SIS_330) {
3084          temp = ROMAddr[VB310Data_1_2_Offset] | 0x40;
3085          if(SiS_Pr->SiS_ROMNew) temp = ROMAddr[0x80] | 0x40;
3086          SiS_SetReg(SiS_Pr->SiS_Part1Port,0x02,temp);
3087       } else if(SiS_Pr->ChipType >= SIS_661 && SiS_Pr->ChipType < XGI_20) {
3088          temp = ROMAddr[0x7e] | 0x40;
3089          if(SiS_Pr->SiS_ROMNew) temp = ROMAddr[0x80] | 0x40;
3090          SiS_SetReg(SiS_Pr->SiS_Part1Port,0x02,temp);
3091       }
3092    } else if(SiS_Pr->ChipType >= XGI_40) {
3093       temp = 0x40;
3094       if(SiS_Pr->SiS_XGIROM) temp |= ROMAddr[0x7e];
3095       /* Can we do this on any chipset? */
3096       SiS_SetReg(SiS_Pr->SiS_Part1Port,0x02,temp);
3097    }
3098 #endif
3099 }
3100
3101 /*********************************************/
3102 /*    HELPER: SET VIDEO/CAPTURE REGISTERS    */
3103 /*********************************************/
3104
3105 static void
3106 SiS_StrangeStuff(struct SiS_Private *SiS_Pr)
3107 {
3108    /* SiS65x and XGI set up some sort of "lock mode" for text
3109     * which locks CRT2 in some way to CRT1 timing. Disable
3110     * this here.
3111     */
3112 #ifdef CONFIG_FB_SIS_315
3113    if((IS_SIS651) || (IS_SISM650) ||
3114       SiS_Pr->ChipType == SIS_340 ||
3115       SiS_Pr->ChipType == XGI_40) {
3116       SiS_SetReg(SiS_Pr->SiS_VidCapt, 0x3f, 0x00);   /* Fiddle with capture regs */
3117       SiS_SetReg(SiS_Pr->SiS_VidCapt, 0x00, 0x00);
3118       SiS_SetReg(SiS_Pr->SiS_VidPlay, 0x00, 0x86);   /* (BIOS does NOT unlock) */
3119       SiS_SetRegAND(SiS_Pr->SiS_VidPlay, 0x30, 0xfe); /* Fiddle with video regs */
3120       SiS_SetRegAND(SiS_Pr->SiS_VidPlay, 0x3f, 0xef);
3121    }
3122    /* !!! This does not support modes < 0x13 !!! */
3123 #endif
3124 }
3125
3126 /*********************************************/
3127 /*     HELPER: SET AGP TIMING FOR SiS760     */
3128 /*********************************************/
3129
3130 static void
3131 SiS_Handle760(struct SiS_Private *SiS_Pr)
3132 {
3133 #ifdef CONFIG_FB_SIS_315
3134    unsigned int somebase;
3135    unsigned char temp1, temp2, temp3;
3136
3137    if( (SiS_Pr->ChipType != SIS_760)                         ||
3138        ((SiS_GetReg(SiS_Pr->SiS_P3d4, 0x5c) & 0xf8) != 0x80) ||
3139        (!(SiS_Pr->SiS_SysFlags & SF_760LFB))                 ||
3140        (!(SiS_Pr->SiS_SysFlags & SF_760UMA)) )
3141       return;
3142
3143    somebase = sisfb_read_mio_pci_word(SiS_Pr, 0x74);
3144    somebase &= 0xffff;
3145
3146    if(somebase == 0) return;
3147
3148    temp3 = SiS_GetRegByte((somebase + 0x85)) & 0xb7;
3149
3150    if(SiS_GetReg(SiS_Pr->SiS_P3d4,0x31) & 0x40) {
3151       temp1 = 0x21;
3152       temp2 = 0x03;
3153       temp3 |= 0x08;
3154    } else {
3155       temp1 = 0x25;
3156       temp2 = 0x0b;
3157    }
3158
3159    sisfb_write_nbridge_pci_byte(SiS_Pr, 0x7e, temp1);
3160    sisfb_write_nbridge_pci_byte(SiS_Pr, 0x8d, temp2);
3161
3162    SiS_SetRegByte((somebase + 0x85), temp3);
3163 #endif
3164 }
3165
3166 /*********************************************/
3167 /*                 SiSSetMode()              */
3168 /*********************************************/
3169
3170 bool
3171 SiSSetMode(struct SiS_Private *SiS_Pr, unsigned short ModeNo)
3172 {
3173    SISIOADDRESS BaseAddr = SiS_Pr->IOAddress;
3174    unsigned short RealModeNo, ModeIdIndex;
3175    unsigned char  backupreg = 0;
3176    unsigned short KeepLockReg;
3177
3178    SiS_Pr->UseCustomMode = false;
3179    SiS_Pr->CRT1UsesCustomMode = false;
3180
3181    SiS_Pr->SiS_flag_clearbuffer = 0;
3182
3183    if(SiS_Pr->UseCustomMode) {
3184       ModeNo = 0xfe;
3185    } else {
3186       if(!(ModeNo & 0x80)) SiS_Pr->SiS_flag_clearbuffer = 1;
3187       ModeNo &= 0x7f;
3188    }
3189
3190    /* Don't use FSTN mode for CRT1 */
3191    RealModeNo = ModeNo;
3192    if(ModeNo == 0x5b) ModeNo = 0x56;
3193
3194    SiSInitPtr(SiS_Pr);
3195    SiSRegInit(SiS_Pr, BaseAddr);
3196    SiS_GetSysFlags(SiS_Pr);
3197
3198    SiS_Pr->SiS_VGAINFO = 0x11;
3199
3200    KeepLockReg = SiS_GetReg(SiS_Pr->SiS_P3c4,0x05);
3201    SiS_SetReg(SiS_Pr->SiS_P3c4,0x05,0x86);
3202
3203    SiSInitPCIetc(SiS_Pr);
3204    SiSSetLVDSetc(SiS_Pr);
3205    SiSDetermineROMUsage(SiS_Pr);
3206
3207    SiS_UnLockCRT2(SiS_Pr);
3208
3209    if(!SiS_Pr->UseCustomMode) {
3210       if(!(SiS_SearchModeID(SiS_Pr, &ModeNo, &ModeIdIndex))) return false;
3211    } else {
3212       ModeIdIndex = 0;
3213    }
3214
3215    SiS_GetVBType(SiS_Pr);
3216
3217    /* Init/restore some VB registers */
3218    SiS_InitVB(SiS_Pr);
3219    if(SiS_Pr->SiS_VBType & VB_SIS30xBLV) {
3220       if(SiS_Pr->ChipType >= SIS_315H) {
3221          SiS_ResetVB(SiS_Pr);
3222          SiS_SetRegOR(SiS_Pr->SiS_P3c4,0x32,0x10);
3223          SiS_SetRegOR(SiS_Pr->SiS_Part2Port,0x00,0x0c);
3224          backupreg = SiS_GetReg(SiS_Pr->SiS_P3d4,0x38);
3225       } else {
3226          backupreg = SiS_GetReg(SiS_Pr->SiS_P3d4,0x35);
3227       }
3228    }
3229
3230    /* Get VB information (connectors, connected devices) */
3231    SiS_GetVBInfo(SiS_Pr, ModeNo, ModeIdIndex, (SiS_Pr->UseCustomMode) ? 0 : 1);
3232    SiS_SetYPbPr(SiS_Pr);
3233    SiS_SetTVMode(SiS_Pr, ModeNo, ModeIdIndex);
3234    SiS_GetLCDResInfo(SiS_Pr, ModeNo, ModeIdIndex);
3235    SiS_SetLowModeTest(SiS_Pr, ModeNo);
3236
3237    /* Check memory size (kernel framebuffer driver only) */
3238    if(!SiS_CheckMemorySize(SiS_Pr, ModeNo, ModeIdIndex)) {
3239       return false;
3240    }
3241
3242    SiS_OpenCRTC(SiS_Pr);
3243
3244    if(SiS_Pr->UseCustomMode) {
3245       SiS_Pr->CRT1UsesCustomMode = true;
3246       SiS_Pr->CSRClock_CRT1 = SiS_Pr->CSRClock;
3247       SiS_Pr->CModeFlag_CRT1 = SiS_Pr->CModeFlag;
3248    } else {
3249       SiS_Pr->CRT1UsesCustomMode = false;
3250    }
3251
3252    /* Set mode on CRT1 */
3253    if( (SiS_Pr->SiS_VBInfo & (SetSimuScanMode | SetCRT2ToLCDA)) ||
3254        (!(SiS_Pr->SiS_VBInfo & SwitchCRT2)) ) {
3255       SiS_SetCRT1Group(SiS_Pr, ModeNo, ModeIdIndex);
3256    }
3257
3258    /* Set mode on CRT2 */
3259    if(SiS_Pr->SiS_VBInfo & (SetSimuScanMode | SwitchCRT2 | SetCRT2ToLCDA)) {
3260       if( (SiS_Pr->SiS_VBType & VB_SISVB)    ||
3261           (SiS_Pr->SiS_IF_DEF_LVDS     == 1) ||
3262           (SiS_Pr->SiS_IF_DEF_CH70xx   != 0) ||
3263           (SiS_Pr->SiS_IF_DEF_TRUMPION != 0) ) {
3264          SiS_SetCRT2Group(SiS_Pr, RealModeNo);
3265       }
3266    }
3267
3268    SiS_HandleCRT1(SiS_Pr);
3269
3270    SiS_StrangeStuff(SiS_Pr);
3271
3272    SiS_DisplayOn(SiS_Pr);
3273    SiS_SetRegByte(SiS_Pr->SiS_P3c6,0xFF);
3274
3275 #ifdef CONFIG_FB_SIS_315
3276    if(SiS_Pr->ChipType >= SIS_315H) {
3277       if(SiS_Pr->SiS_IF_DEF_LVDS == 1) {
3278          if(!(SiS_IsDualEdge(SiS_Pr))) {
3279             SiS_SetRegAND(SiS_Pr->SiS_Part1Port,0x13,0xfb);
3280          }
3281       }
3282    }
3283 #endif
3284
3285    if(SiS_Pr->SiS_VBType & VB_SIS30xBLV) {
3286       if(SiS_Pr->ChipType >= SIS_315H) {
3287 #ifdef CONFIG_FB_SIS_315
3288          if(!SiS_Pr->SiS_ROMNew) {
3289             if(SiS_IsVAMode(SiS_Pr)) {
3290                SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x35,0x01);
3291             } else {
3292                SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x35,0xFE);
3293             }
3294          }
3295
3296          SiS_SetReg(SiS_Pr->SiS_P3d4,0x38,backupreg);
3297
3298          if((IS_SIS650) && (SiS_GetReg(SiS_Pr->SiS_P3d4,0x30) & 0xfc)) {
3299             if((ModeNo == 0x03) || (ModeNo == 0x10)) {
3300                SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x51,0x80);
3301                SiS_SetRegOR(SiS_Pr->SiS_P3d4,0x56,0x08);
3302             }
3303          }
3304
3305          if(SiS_GetReg(SiS_Pr->SiS_P3d4,0x30) & SetCRT2ToLCD) {
3306             SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x38,0xfc);
3307          }
3308 #endif
3309       } else if((SiS_Pr->ChipType == SIS_630) ||
3310                 (SiS_Pr->ChipType == SIS_730)) {
3311          SiS_SetReg(SiS_Pr->SiS_P3d4,0x35,backupreg);
3312       }
3313    }
3314
3315    SiS_CloseCRTC(SiS_Pr);
3316
3317    SiS_Handle760(SiS_Pr);
3318
3319    /* We never lock registers in XF86 */
3320    if(KeepLockReg != 0xA1) SiS_SetReg(SiS_Pr->SiS_P3c4,0x05,0x00);
3321
3322    return true;
3323 }
3324
3325 #ifndef GETBITSTR
3326 #define BITMASK(h,l)            (((unsigned)(1U << ((h)-(l)+1))-1)<<(l))
3327 #define GENMASK(mask)           BITMASK(1?mask,0?mask)
3328 #define GETBITS(var,mask)       (((var) & GENMASK(mask)) >> (0?mask))
3329 #define GETBITSTR(val,from,to)  ((GETBITS(val,from)) << (0?to))
3330 #endif
3331
3332 void
3333 SiS_CalcCRRegisters(struct SiS_Private *SiS_Pr, int depth)
3334 {
3335    int x = 1; /* Fix sync */
3336
3337    SiS_Pr->CCRT1CRTC[0]  =  ((SiS_Pr->CHTotal >> 3) - 5) & 0xff;                /* CR0 */
3338    SiS_Pr->CCRT1CRTC[1]  =  (SiS_Pr->CHDisplay >> 3) - 1;                       /* CR1 */
3339    SiS_Pr->CCRT1CRTC[2]  =  (SiS_Pr->CHBlankStart >> 3) - 1;                    /* CR2 */
3340    SiS_Pr->CCRT1CRTC[3]  =  (((SiS_Pr->CHBlankEnd >> 3) - 1) & 0x1F) | 0x80;    /* CR3 */
3341    SiS_Pr->CCRT1CRTC[4]  =  (SiS_Pr->CHSyncStart >> 3) + 3;                     /* CR4 */
3342    SiS_Pr->CCRT1CRTC[5]  =  ((((SiS_Pr->CHBlankEnd >> 3) - 1) & 0x20) << 2) |   /* CR5 */
3343                             (((SiS_Pr->CHSyncEnd >> 3) + 3) & 0x1F);
3344
3345    SiS_Pr->CCRT1CRTC[6]  =  (SiS_Pr->CVTotal       - 2) & 0xFF;                 /* CR6 */
3346    SiS_Pr->CCRT1CRTC[7]  =  (((SiS_Pr->CVTotal     - 2) & 0x100) >> 8)          /* CR7 */
3347                           | (((SiS_Pr->CVDisplay   - 1) & 0x100) >> 7)
3348                           | (((SiS_Pr->CVSyncStart - x) & 0x100) >> 6)
3349                           | (((SiS_Pr->CVBlankStart- 1) & 0x100) >> 5)
3350                           | 0x10
3351                           | (((SiS_Pr->CVTotal     - 2) & 0x200) >> 4)
3352                           | (((SiS_Pr->CVDisplay   - 1) & 0x200) >> 3)
3353                           | (((SiS_Pr->CVSyncStart - x) & 0x200) >> 2);
3354
3355    SiS_Pr->CCRT1CRTC[16] = ((((SiS_Pr->CVBlankStart - 1) & 0x200) >> 4) >> 5);  /* CR9 */
3356
3357    if(depth != 8) {
3358       if(SiS_Pr->CHDisplay >= 1600)      SiS_Pr->CCRT1CRTC[16] |= 0x60;         /* SRE */
3359       else if(SiS_Pr->CHDisplay >= 640)  SiS_Pr->CCRT1CRTC[16] |= 0x40;
3360    }
3361
3362    SiS_Pr->CCRT1CRTC[8] =  (SiS_Pr->CVSyncStart  - x) & 0xFF;                   /* CR10 */
3363    SiS_Pr->CCRT1CRTC[9] =  ((SiS_Pr->CVSyncEnd   - x) & 0x0F) | 0x80;           /* CR11 */
3364    SiS_Pr->CCRT1CRTC[10] = (SiS_Pr->CVDisplay    - 1) & 0xFF;                   /* CR12 */
3365    SiS_Pr->CCRT1CRTC[11] = (SiS_Pr->CVBlankStart - 1) & 0xFF;                   /* CR15 */
3366    SiS_Pr->CCRT1CRTC[12] = (SiS_Pr->CVBlankEnd   - 1) & 0xFF;                   /* CR16 */
3367
3368    SiS_Pr->CCRT1CRTC[13] =                                                      /* SRA */
3369                         GETBITSTR((SiS_Pr->CVTotal     -2), 10:10, 0:0) |
3370                         GETBITSTR((SiS_Pr->CVDisplay   -1), 10:10, 1:1) |
3371                         GETBITSTR((SiS_Pr->CVBlankStart-1), 10:10, 2:2) |
3372                         GETBITSTR((SiS_Pr->CVSyncStart -x), 10:10, 3:3) |
3373                         GETBITSTR((SiS_Pr->CVBlankEnd  -1),   8:8, 4:4) |
3374                         GETBITSTR((SiS_Pr->CVSyncEnd     ),   4:4, 5:5) ;
3375
3376    SiS_Pr->CCRT1CRTC[14] =                                                      /* SRB */
3377                         GETBITSTR((SiS_Pr->CHTotal      >> 3) - 5, 9:8, 1:0) |
3378                         GETBITSTR((SiS_Pr->CHDisplay    >> 3) - 1, 9:8, 3:2) |
3379                         GETBITSTR((SiS_Pr->CHBlankStart >> 3) - 1, 9:8, 5:4) |
3380                         GETBITSTR((SiS_Pr->CHSyncStart  >> 3) + 3, 9:8, 7:6) ;
3381
3382
3383    SiS_Pr->CCRT1CRTC[15] =                                                      /* SRC */
3384                         GETBITSTR((SiS_Pr->CHBlankEnd >> 3) - 1, 7:6, 1:0) |
3385                         GETBITSTR((SiS_Pr->CHSyncEnd  >> 3) + 3, 5:5, 2:2) ;
3386 }
3387
3388 void
3389 SiS_CalcLCDACRT1Timing(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
3390                 unsigned short ModeIdIndex)
3391 {
3392    unsigned short modeflag, tempax, tempbx = 0, remaining = 0;
3393    unsigned short VGAHDE = SiS_Pr->SiS_VGAHDE;
3394    int i, j;
3395
3396    /* 1:1 data: use data set by setcrt1crtc() */
3397    if(SiS_Pr->SiS_LCDInfo & LCDPass11) return;
3398
3399    modeflag = SiS_GetModeFlag(SiS_Pr, ModeNo, ModeIdIndex);
3400
3401    if(modeflag & HalfDCLK) VGAHDE >>= 1;
3402
3403    SiS_Pr->CHDisplay = VGAHDE;
3404    SiS_Pr->CHBlankStart = VGAHDE;
3405
3406    SiS_Pr->CVDisplay = SiS_Pr->SiS_VGAVDE;
3407    SiS_Pr->CVBlankStart = SiS_Pr->SiS_VGAVDE;
3408
3409    if(SiS_Pr->ChipType < SIS_315H) {
3410 #ifdef CONFIG_FB_SIS_300
3411       tempbx = SiS_Pr->SiS_VGAHT;
3412       if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
3413          tempbx = SiS_Pr->PanelHT;
3414       }
3415       if(modeflag & HalfDCLK) tempbx >>= 1;
3416       remaining = tempbx % 8;
3417 #endif
3418    } else {
3419 #ifdef CONFIG_FB_SIS_315
3420       /* OK for LCDA, LVDS */
3421       tempbx = SiS_Pr->PanelHT - SiS_Pr->PanelXRes;
3422       tempax = SiS_Pr->SiS_VGAHDE;  /* not /2 ! */
3423       if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
3424          tempax = SiS_Pr->PanelXRes;
3425       }
3426       tempbx += tempax;
3427       if(modeflag & HalfDCLK) tempbx -= VGAHDE;
3428 #endif
3429    }
3430    SiS_Pr->CHTotal = SiS_Pr->CHBlankEnd = tempbx;
3431
3432    if(SiS_Pr->ChipType < SIS_315H) {
3433 #ifdef CONFIG_FB_SIS_300
3434       if(SiS_Pr->SiS_VGAHDE == SiS_Pr->PanelXRes) {
3435          SiS_Pr->CHSyncStart = SiS_Pr->SiS_VGAHDE + ((SiS_Pr->PanelHRS + 1) & ~1);
3436          SiS_Pr->CHSyncEnd = SiS_Pr->CHSyncStart + SiS_Pr->PanelHRE;
3437          if(modeflag & HalfDCLK) {
3438             SiS_Pr->CHSyncStart >>= 1;
3439             SiS_Pr->CHSyncEnd >>= 1;
3440          }
3441       } else if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
3442          tempax = (SiS_Pr->PanelXRes - SiS_Pr->SiS_VGAHDE) >> 1;
3443          tempbx = (SiS_Pr->PanelHRS + 1) & ~1;
3444          if(modeflag & HalfDCLK) {
3445             tempax >>= 1;
3446             tempbx >>= 1;
3447          }
3448          SiS_Pr->CHSyncStart = (VGAHDE + tempax + tempbx + 7) & ~7;
3449          tempax = SiS_Pr->PanelHRE + 7;
3450          if(modeflag & HalfDCLK) tempax >>= 1;
3451          SiS_Pr->CHSyncEnd = (SiS_Pr->CHSyncStart + tempax) & ~7;
3452       } else {
3453          SiS_Pr->CHSyncStart = SiS_Pr->SiS_VGAHDE;
3454          if(modeflag & HalfDCLK) {
3455             SiS_Pr->CHSyncStart >>= 1;
3456             tempax = ((SiS_Pr->CHTotal - SiS_Pr->CHSyncStart) / 3) << 1;
3457             SiS_Pr->CHSyncEnd = SiS_Pr->CHSyncStart + tempax;
3458          } else {
3459             SiS_Pr->CHSyncEnd = (SiS_Pr->CHSyncStart + (SiS_Pr->CHTotal / 10) + 7) & ~7;
3460             SiS_Pr->CHSyncStart += 8;
3461          }
3462       }
3463 #endif
3464    } else {
3465 #ifdef CONFIG_FB_SIS_315
3466       tempax = VGAHDE;
3467       if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
3468          tempbx = SiS_Pr->PanelXRes;
3469          if(modeflag & HalfDCLK) tempbx >>= 1;
3470          tempax += ((tempbx - tempax) >> 1);
3471       }
3472       tempax += SiS_Pr->PanelHRS;
3473       SiS_Pr->CHSyncStart = tempax;
3474       tempax += SiS_Pr->PanelHRE;
3475       SiS_Pr->CHSyncEnd = tempax;
3476 #endif
3477    }
3478
3479    tempbx = SiS_Pr->PanelVT - SiS_Pr->PanelYRes;
3480    tempax = SiS_Pr->SiS_VGAVDE;
3481    if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
3482       tempax = SiS_Pr->PanelYRes;
3483    } else if(SiS_Pr->ChipType < SIS_315H) {
3484 #ifdef CONFIG_FB_SIS_300
3485       /* Stupid hack for 640x400/320x200 */
3486       if(SiS_Pr->SiS_LCDResInfo == Panel_1024x768) {
3487          if((tempax + tempbx) == 438) tempbx += 16;
3488       } else if((SiS_Pr->SiS_LCDResInfo == Panel_800x600) ||
3489                 (SiS_Pr->SiS_LCDResInfo == Panel_1024x600)) {
3490          tempax = 0;
3491          tempbx = SiS_Pr->SiS_VGAVT;
3492       }
3493 #endif
3494    }
3495    SiS_Pr->CVTotal = SiS_Pr->CVBlankEnd = tempbx + tempax;
3496
3497    tempax = SiS_Pr->SiS_VGAVDE;
3498    if(SiS_Pr->SiS_LCDInfo & DontExpandLCD) {
3499       tempax += (SiS_Pr->PanelYRes - tempax) >> 1;
3500    }
3501    tempax += SiS_Pr->PanelVRS;
3502    SiS_Pr->CVSyncStart = tempax;
3503    tempax += SiS_Pr->PanelVRE;
3504    SiS_Pr->CVSyncEnd = tempax;
3505    if(SiS_Pr->ChipType < SIS_315H) {
3506       SiS_Pr->CVSyncStart--;
3507       SiS_Pr->CVSyncEnd--;
3508    }
3509
3510    SiS_CalcCRRegisters(SiS_Pr, 8);
3511    SiS_Pr->CCRT1CRTC[15] &= ~0xF8;
3512    SiS_Pr->CCRT1CRTC[15] |= (remaining << 4);
3513    SiS_Pr->CCRT1CRTC[16] &= ~0xE0;
3514
3515    SiS_SetRegAND(SiS_Pr->SiS_P3d4,0x11,0x7f);
3516
3517    for(i = 0, j = 0; i <= 7; i++, j++) {
3518       SiS_SetReg(SiS_Pr->SiS_P3d4,j,SiS_Pr->CCRT1CRTC[i]);
3519    }
3520    for(j = 0x10; i <= 10; i++, j++) {
3521       SiS_SetReg(SiS_Pr->SiS_P3d4,j,SiS_Pr->CCRT1CRTC[i]);
3522    }
3523    for(j = 0x15; i <= 12; i++, j++) {
3524       SiS_SetReg(SiS_Pr->SiS_P3d4,j,SiS_Pr->CCRT1CRTC[i]);
3525    }
3526    for(j = 0x0A; i <= 15; i++, j++) {
3527       SiS_SetReg(SiS_Pr->SiS_P3c4,j,SiS_Pr->CCRT1CRTC[i]);
3528    }
3529
3530    tempax = SiS_Pr->CCRT1CRTC[16] & 0xE0;
3531    SiS_SetRegANDOR(SiS_Pr->SiS_P3c4,0x0E,0x1F,tempax);
3532
3533    tempax = (SiS_Pr->CCRT1CRTC[16] & 0x01) << 5;
3534    if(modeflag & DoubleScanMode) tempax |= 0x80;
3535    SiS_SetRegANDOR(SiS_Pr->SiS_P3d4,0x09,0x5F,tempax);
3536
3537 }
3538
3539 void
3540 SiS_Generic_ConvertCRData(struct SiS_Private *SiS_Pr, unsigned char *crdata,
3541                         int xres, int yres,
3542                         struct fb_var_screeninfo *var, bool writeres
3543 )
3544 {
3545    unsigned short HRE, HBE, HRS, HBS, HDE, HT;
3546    unsigned short VRE, VBE, VRS, VBS, VDE, VT;
3547    unsigned char  sr_data, cr_data, cr_data2;
3548    int            A, B, C, D, E, F, temp;
3549
3550    sr_data = crdata[14];
3551
3552    /* Horizontal total */
3553    HT =  crdata[0] | ((unsigned short)(sr_data & 0x03) << 8);
3554    A = HT + 5;
3555
3556    /* Horizontal display enable end */
3557    HDE = crdata[1] | ((unsigned short)(sr_data & 0x0C) << 6);
3558    E = HDE + 1;
3559
3560    /* Horizontal retrace (=sync) start */
3561    HRS = crdata[4] | ((unsigned short)(sr_data & 0xC0) << 2);
3562    F = HRS - E - 3;
3563
3564    /* Horizontal blank start */
3565    HBS = crdata[2] | ((unsigned short)(sr_data & 0x30) << 4);
3566
3567    sr_data = crdata[15];
3568    cr_data = crdata[5];
3569
3570    /* Horizontal blank end */
3571    HBE = (crdata[3] & 0x1f) |
3572          ((unsigned short)(cr_data & 0x80) >> 2) |
3573          ((unsigned short)(sr_data & 0x03) << 6);
3574
3575    /* Horizontal retrace (=sync) end */
3576    HRE = (cr_data & 0x1f) | ((sr_data & 0x04) << 3);
3577
3578    temp = HBE - ((E - 1) & 255);
3579    B = (temp > 0) ? temp : (temp + 256);
3580
3581    temp = HRE - ((E + F + 3) & 63);
3582    C = (temp > 0) ? temp : (temp + 64);
3583
3584    D = B - F - C;
3585
3586    if(writeres) var->xres = xres = E * 8;
3587    var->left_margin = D * 8;
3588    var->right_margin = F * 8;
3589    var->hsync_len = C * 8;
3590
3591    /* Vertical */
3592    sr_data = crdata[13];
3593    cr_data = crdata[7];
3594
3595    /* Vertical total */
3596    VT  = crdata[6] |
3597          ((unsigned short)(cr_data & 0x01) << 8) |
3598          ((unsigned short)(cr_data & 0x20) << 4) |
3599          ((unsigned short)(sr_data & 0x01) << 10);
3600    A = VT + 2;
3601
3602    /* Vertical display enable end */
3603    VDE = crdata[10] |
3604          ((unsigned short)(cr_data & 0x02) << 7) |
3605          ((unsigned short)(cr_data & 0x40) << 3) |
3606          ((unsigned short)(sr_data & 0x02) << 9);
3607    E = VDE + 1;
3608
3609    /* Vertical retrace (=sync) start */
3610    VRS = crdata[8] |
3611          ((unsigned short)(cr_data & 0x04) << 6) |
3612          ((unsigned short)(cr_data & 0x80) << 2) |
3613          ((unsigned short)(sr_data & 0x08) << 7);
3614    F = VRS + 1 - E;
3615
3616    cr_data2 = (crdata[16] & 0x01) << 5;
3617
3618    /* Vertical blank start */
3619    VBS = crdata[11] |
3620          ((unsigned short)(cr_data  & 0x08) << 5) |
3621          ((unsigned short)(cr_data2 & 0x20) << 4) |
3622          ((unsigned short)(sr_data  & 0x04) << 8);
3623
3624    /* Vertical blank end */
3625    VBE = crdata[12] | ((unsigned short)(sr_data & 0x10) << 4);
3626    temp = VBE - ((E - 1) & 511);
3627    B = (temp > 0) ? temp : (temp + 512);
3628
3629    /* Vertical retrace (=sync) end */
3630    VRE = (crdata[9] & 0x0f) | ((sr_data & 0x20) >> 1);
3631    temp = VRE - ((E + F - 1) & 31);
3632    C = (temp > 0) ? temp : (temp + 32);
3633
3634    D = B - F - C;
3635
3636    if(writeres) var->yres = yres = E;
3637    var->upper_margin = D;
3638    var->lower_margin = F;
3639    var->vsync_len = C;
3640
3641    if((xres == 320) && ((yres == 200) || (yres == 240))) {
3642         /* Terrible hack, but correct CRTC data for
3643          * these modes only produces a black screen...
3644          * (HRE is 0, leading into a too large C and
3645          * a negative D. The CRT controller does not
3646          * seem to like correcting HRE to 50)
3647          */
3648       var->left_margin = (400 - 376);
3649       var->right_margin = (328 - 320);
3650       var->hsync_len = (376 - 328);
3651
3652    }
3653
3654 }
3655
3656
3657
3658