staging: xgifb: delete redundant Chrontel TV data
[pandora-kernel.git] / drivers / staging / xgifb / vb_setmode.c
1
2 #include <linux/io.h>
3 #include <linux/delay.h>
4 #include <linux/types.h>
5 #include "XGIfb.h"
6
7
8 #include "vb_def.h"
9 #include "vgatypes.h"
10 #include "vb_struct.h"
11 #include "vb_util.h"
12 #include "vb_table.h"
13 #include "vb_setmode.h"
14
15
16 #define  IndexMask 0xff
17
18 static unsigned short XGINew_MDA_DAC[] = {
19         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
20         0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15,
21         0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15,
22         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
23         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
24         0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15,
25         0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15,
26         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F};
27
28 static unsigned short XGINew_CGA_DAC[] = {
29         0x00, 0x10, 0x04, 0x14, 0x01, 0x11, 0x09, 0x15,
30         0x00, 0x10, 0x04, 0x14, 0x01, 0x11, 0x09, 0x15,
31         0x2A, 0x3A, 0x2E, 0x3E, 0x2B, 0x3B, 0x2F, 0x3F,
32         0x2A, 0x3A, 0x2E, 0x3E, 0x2B, 0x3B, 0x2F, 0x3F,
33         0x00, 0x10, 0x04, 0x14, 0x01, 0x11, 0x09, 0x15,
34         0x00, 0x10, 0x04, 0x14, 0x01, 0x11, 0x09, 0x15,
35         0x2A, 0x3A, 0x2E, 0x3E, 0x2B, 0x3B, 0x2F, 0x3F,
36         0x2A, 0x3A, 0x2E, 0x3E, 0x2B, 0x3B, 0x2F, 0x3F};
37
38 static unsigned short XGINew_EGA_DAC[] = {
39         0x00, 0x10, 0x04, 0x14, 0x01, 0x11, 0x05, 0x15,
40         0x20, 0x30, 0x24, 0x34, 0x21, 0x31, 0x25, 0x35,
41         0x08, 0x18, 0x0C, 0x1C, 0x09, 0x19, 0x0D, 0x1D,
42         0x28, 0x38, 0x2C, 0x3C, 0x29, 0x39, 0x2D, 0x3D,
43         0x02, 0x12, 0x06, 0x16, 0x03, 0x13, 0x07, 0x17,
44         0x22, 0x32, 0x26, 0x36, 0x23, 0x33, 0x27, 0x37,
45         0x0A, 0x1A, 0x0E, 0x1E, 0x0B, 0x1B, 0x0F, 0x1F,
46         0x2A, 0x3A, 0x2E, 0x3E, 0x2B, 0x3B, 0x2F, 0x3F};
47
48 static unsigned short XGINew_VGA_DAC[] = {
49         0x00, 0x10, 0x04, 0x14, 0x01, 0x11, 0x09, 0x15,
50         0x2A, 0x3A, 0x2E, 0x3E, 0x2B, 0x3B, 0x2F, 0x3F,
51         0x00, 0x05, 0x08, 0x0B, 0x0E, 0x11, 0x14, 0x18,
52         0x1C, 0x20, 0x24, 0x28, 0x2D, 0x32, 0x38, 0x3F,
53         0x00, 0x10, 0x1F, 0x2F, 0x3F, 0x1F, 0x27, 0x2F,
54         0x37, 0x3F, 0x2D, 0x31, 0x36, 0x3A, 0x3F, 0x00,
55         0x07, 0x0E, 0x15, 0x1C, 0x0E, 0x11, 0x15, 0x18,
56         0x1C, 0x14, 0x16, 0x18, 0x1A, 0x1C, 0x00, 0x04,
57         0x08, 0x0C, 0x10, 0x08, 0x0A, 0x0C, 0x0E, 0x10,
58         0x0B, 0x0C, 0x0D, 0x0F, 0x10};
59
60 void InitTo330Pointer(unsigned char ChipType, struct vb_device_info *pVBInfo)
61 {
62         pVBInfo->SModeIDTable = (struct XGI_StStruct *) XGI330_SModeIDTable;
63         pVBInfo->StandTable = (struct XGI_StandTableStruct *) XGI330_StandTable;
64         pVBInfo->EModeIDTable = (struct XGI_ExtStruct *) XGI330_EModeIDTable;
65         pVBInfo->RefIndex = (struct XGI_Ext2Struct *) XGI330_RefIndex;
66         pVBInfo->XGINEWUB_CRT1Table
67                         = (struct XGI_CRT1TableStruct *) XGI_CRT1Table;
68
69         /* add for new UNIVGABIOS */
70         /* XGINew_UBLCDDataTable =
71          *      (struct XGI_LCDDataTablStruct *) XGI_LCDDataTable; */
72         /* XGINew_UBTVDataTable = (XGI_TVDataTablStruct *) XGI_TVDataTable; */
73
74         pVBInfo->MCLKData = (struct XGI_MCLKDataStruct *) XGI340New_MCLKData;
75         pVBInfo->ECLKData = (struct XGI_ECLKDataStruct *) XGI340_ECLKData;
76         pVBInfo->VCLKData = (struct XGI_VCLKDataStruct *) XGI_VCLKData;
77         pVBInfo->VBVCLKData = (struct XGI_VBVCLKDataStruct *) XGI_VBVCLKData;
78         pVBInfo->ScreenOffset = XGI330_ScreenOffset;
79         pVBInfo->StResInfo = (struct XGI_StResInfoStruct *) XGI330_StResInfo;
80         pVBInfo->ModeResInfo
81                         = (struct XGI_ModeResInfoStruct *) XGI330_ModeResInfo;
82
83         pVBInfo->pOutputSelect = &XGI330_OutputSelect;
84         pVBInfo->pSoftSetting = &XGI330_SoftSetting;
85         pVBInfo->pSR07 = &XGI330_SR07;
86         pVBInfo->LCDResInfo = 0;
87         pVBInfo->LCDTypeInfo = 0;
88         pVBInfo->LCDInfo = 0;
89         pVBInfo->VBInfo = 0;
90         pVBInfo->TVInfo = 0;
91
92         pVBInfo->SR15 = XGI340_SR13;
93         pVBInfo->CR40 = XGI340_cr41;
94         pVBInfo->SR25 = XGI330_sr25;
95         pVBInfo->pSR31 = &XGI330_sr31;
96         pVBInfo->pSR32 = &XGI330_sr32;
97         pVBInfo->CR6B = XGI340_CR6B;
98         pVBInfo->CR6E = XGI340_CR6E;
99         pVBInfo->CR6F = XGI340_CR6F;
100         pVBInfo->CR89 = XGI340_CR89;
101         pVBInfo->AGPReg = XGI340_AGPReg;
102         pVBInfo->SR16 = XGI340_SR16;
103         pVBInfo->pCRCF = &XG40_CRCF;
104         pVBInfo->pXGINew_DRAMTypeDefinition = &XG40_DRAMTypeDefinition;
105
106         pVBInfo->CR49 = XGI330_CR49;
107         pVBInfo->pSR1F = &XGI330_SR1F;
108         pVBInfo->pSR21 = &XGI330_SR21;
109         pVBInfo->pSR22 = &XGI330_SR22;
110         pVBInfo->pSR23 = &XGI330_SR23;
111         pVBInfo->pSR24 = &XGI330_SR24;
112         pVBInfo->pSR33 = &XGI330_SR33;
113
114         pVBInfo->pCRT2Data_1_2 = &XGI330_CRT2Data_1_2;
115         pVBInfo->pCRT2Data_4_D = &XGI330_CRT2Data_4_D;
116         pVBInfo->pCRT2Data_4_E = &XGI330_CRT2Data_4_E;
117         pVBInfo->pCRT2Data_4_10 = &XGI330_CRT2Data_4_10;
118         pVBInfo->pRGBSenseData = &XGI330_RGBSenseData;
119         pVBInfo->pVideoSenseData = &XGI330_VideoSenseData;
120         pVBInfo->pYCSenseData = &XGI330_YCSenseData;
121         pVBInfo->pRGBSenseData2 = &XGI330_RGBSenseData2;
122         pVBInfo->pVideoSenseData2 = &XGI330_VideoSenseData2;
123         pVBInfo->pYCSenseData2 = &XGI330_YCSenseData2;
124
125         pVBInfo->NTSCTiming = XGI330_NTSCTiming;
126         pVBInfo->PALTiming = XGI330_PALTiming;
127         pVBInfo->HiTVExtTiming = XGI330_HiTVExtTiming;
128         pVBInfo->HiTVSt1Timing = XGI330_HiTVSt1Timing;
129         pVBInfo->HiTVSt2Timing = XGI330_HiTVSt2Timing;
130         pVBInfo->HiTVTextTiming = XGI330_HiTVTextTiming;
131         pVBInfo->YPbPr750pTiming = XGI330_YPbPr750pTiming;
132         pVBInfo->YPbPr525pTiming = XGI330_YPbPr525pTiming;
133         pVBInfo->YPbPr525iTiming = XGI330_YPbPr525iTiming;
134         pVBInfo->HiTVGroup3Data = XGI330_HiTVGroup3Data;
135         pVBInfo->HiTVGroup3Simu = XGI330_HiTVGroup3Simu;
136         pVBInfo->HiTVGroup3Text = XGI330_HiTVGroup3Text;
137         pVBInfo->Ren525pGroup3 = XGI330_Ren525pGroup3;
138         pVBInfo->Ren750pGroup3 = XGI330_Ren750pGroup3;
139
140         pVBInfo->TimingH = (struct XGI_TimingHStruct *) XGI_TimingH;
141         pVBInfo->TimingV = (struct XGI_TimingVStruct *) XGI_TimingV;
142         pVBInfo->UpdateCRT1 = (struct XGI_XG21CRT1Struct *) XGI_UpdateCRT1Table;
143
144         /* 310 customization related */
145         if ((pVBInfo->VBType & VB_XGI301LV) || (pVBInfo->VBType & VB_XGI302LV))
146                 pVBInfo->LCDCapList = XGI_LCDDLCapList;
147         else
148                 pVBInfo->LCDCapList = XGI_LCDCapList;
149
150         if ((ChipType == XG21) || (ChipType == XG27))
151                 pVBInfo->XG21_LVDSCapList = XGI21_LCDCapList;
152
153         pVBInfo->XGI_TVDelayList = XGI301TVDelayList;
154         pVBInfo->XGI_TVDelayList2 = XGI301TVDelayList2;
155
156         pVBInfo->pXGINew_I2CDefinition = &XG40_I2CDefinition;
157
158         if (ChipType >= XG20)
159                 pVBInfo->pXGINew_CR97 = &XG20_CR97;
160
161         if (ChipType == XG27) {
162                 pVBInfo->MCLKData
163                         = (struct XGI_MCLKDataStruct *) XGI27New_MCLKData;
164                 pVBInfo->CR40 = XGI27_cr41;
165                 pVBInfo->pXGINew_CR97 = &XG27_CR97;
166                 pVBInfo->pSR36 = &XG27_SR36;
167                 pVBInfo->pCR8F = &XG27_CR8F;
168                 pVBInfo->pCRD0 = XG27_CRD0;
169                 pVBInfo->pCRDE = XG27_CRDE;
170                 pVBInfo->pSR40 = &XG27_SR40;
171                 pVBInfo->pSR41 = &XG27_SR41;
172
173         }
174
175         if (ChipType >= XG20) {
176                 pVBInfo->pDVOSetting = &XG21_DVOSetting;
177                 pVBInfo->pCR2E = &XG21_CR2E;
178                 pVBInfo->pCR2F = &XG21_CR2F;
179                 pVBInfo->pCR46 = &XG21_CR46;
180                 pVBInfo->pCR47 = &XG21_CR47;
181         }
182
183 }
184
185 static unsigned char XGI_GetModePtr(unsigned short ModeNo,
186                                     unsigned short ModeIdIndex,
187                                     struct vb_device_info *pVBInfo)
188 {
189         unsigned char index;
190
191         if (ModeNo <= 0x13)
192                 index = pVBInfo->SModeIDTable[ModeIdIndex].St_StTableIndex;
193         else {
194                 if (pVBInfo->ModeType <= 0x02)
195                         index = 0x1B; /* 02 -> ModeEGA */
196                 else
197                         index = 0x0F;
198         }
199         return index; /* Get pVBInfo->StandTable index */
200 }
201
202 /*
203 unsigned char XGI_SetBIOSData(unsigned short ModeNo,
204                               unsigned short ModeIdIndex) {
205         return (0);
206 }
207 */
208
209 /* unsigned char XGI_ClearBankRegs(unsigned short ModeNo,
210                                    unsigned short ModeIdIndex) {
211         return( 0 ) ;
212 }
213 */
214
215 static void XGI_SetSeqRegs(unsigned short ModeNo,
216                            unsigned short StandTableIndex,
217                            unsigned short ModeIdIndex,
218                            struct vb_device_info *pVBInfo)
219 {
220         unsigned char tempah, SRdata;
221         unsigned short i, modeflag;
222
223         if (ModeNo <= 0x13)
224                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
225         else
226                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
227
228         xgifb_reg_set(pVBInfo->P3c4, 0x00, 0x03); /* Set SR0 */
229         tempah = pVBInfo->StandTable[StandTableIndex].SR[0];
230
231         i = SetCRT2ToLCDA;
232         if (pVBInfo->VBInfo & SetCRT2ToLCDA) {
233                 tempah |= 0x01;
234         } else {
235                 if (pVBInfo->VBInfo & (SetCRT2ToTV | SetCRT2ToLCD)) {
236                         if (pVBInfo->VBInfo & SetInSlaveMode)
237                                 tempah |= 0x01;
238                 }
239         }
240
241         tempah |= 0x20; /* screen off */
242         xgifb_reg_set(pVBInfo->P3c4, 0x01, tempah); /* Set SR1 */
243
244         for (i = 02; i <= 04; i++) {
245                 /* Get SR2,3,4 from file */
246                 SRdata = pVBInfo->StandTable[StandTableIndex].SR[i - 1];
247                 xgifb_reg_set(pVBInfo->P3c4, i, SRdata); /* Set SR2 3 4 */
248         }
249 }
250
251 static void XGI_SetMiscRegs(unsigned short StandTableIndex,
252                             struct vb_device_info *pVBInfo)
253 {
254         unsigned char Miscdata;
255
256         /* Get Misc from file */
257         Miscdata = pVBInfo->StandTable[StandTableIndex].MISC;
258         /*
259         if (pVBInfo->VBType & (VB_XGI301B |
260                                VB_XGI302B |
261                                VB_XGI301LV |
262                                VB_XGI302LV |
263                                VB_XGI301C)) {
264                 if (pVBInfo->VBInfo & SetCRT2ToLCDA) {
265                         Miscdata |= 0x0C;
266                 }
267         }
268         */
269
270         outb(Miscdata, pVBInfo->P3c2); /* Set Misc(3c2) */
271 }
272
273 static void XGI_SetCRTCRegs(struct xgi_hw_device_info *HwDeviceExtension,
274                             unsigned short StandTableIndex,
275                             struct vb_device_info *pVBInfo)
276 {
277         unsigned char CRTCdata;
278         unsigned short i;
279
280         CRTCdata = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x11);
281         CRTCdata &= 0x7f;
282         xgifb_reg_set(pVBInfo->P3d4, 0x11, CRTCdata); /* Unlock CRTC */
283
284         for (i = 0; i <= 0x18; i++) {
285                 /* Get CRTC from file */
286                 CRTCdata = pVBInfo->StandTable[StandTableIndex].CRTC[i];
287                 xgifb_reg_set(pVBInfo->P3d4, i, CRTCdata); /* Set CRTC(3d4) */
288         }
289         /*
290         if ((HwDeviceExtension->jChipType == XGI_630) &&
291             (HwDeviceExtension->jChipRevision == 0x30)) {
292                 if (pVBInfo->VBInfo & SetInSlaveMode) {
293                         if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToTV)) {
294                                 xgifb_reg_set(pVBInfo->P3d4, 0x18, 0xFE);
295                         }
296                 }
297         }
298         */
299 }
300
301 static void XGI_SetATTRegs(unsigned short ModeNo,
302                            unsigned short StandTableIndex,
303                            unsigned short ModeIdIndex,
304                            struct vb_device_info *pVBInfo)
305 {
306         unsigned char ARdata;
307         unsigned short i, modeflag;
308
309         if (ModeNo <= 0x13)
310                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
311         else
312                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
313
314         for (i = 0; i <= 0x13; i++) {
315                 ARdata = pVBInfo->StandTable[StandTableIndex].ATTR[i];
316                 if (modeflag & Charx8Dot) { /* ifndef Dot9 */
317                         if (i == 0x13) {
318                                 if (pVBInfo->VBInfo & SetCRT2ToLCDA) {
319                                         ARdata = 0;
320                                 } else {
321                                         if (pVBInfo->VBInfo & (SetCRT2ToTV
322                                                         | SetCRT2ToLCD)) {
323                                                 if (pVBInfo->VBInfo &
324                                                     SetInSlaveMode)
325                                                         ARdata = 0;
326                                         }
327                                 }
328                         }
329                 }
330
331                 inb(pVBInfo->P3da); /* reset 3da */
332                 outb(i, pVBInfo->P3c0); /* set index */
333                 outb(ARdata, pVBInfo->P3c0); /* set data */
334         }
335
336         inb(pVBInfo->P3da); /* reset 3da */
337         outb(0x14, pVBInfo->P3c0); /* set index */
338         outb(0x00, pVBInfo->P3c0); /* set data */
339         inb(pVBInfo->P3da); /* Enable Attribute */
340         outb(0x20, pVBInfo->P3c0);
341 }
342
343 static void XGI_SetGRCRegs(unsigned short StandTableIndex,
344                            struct vb_device_info *pVBInfo)
345 {
346         unsigned char GRdata;
347         unsigned short i;
348
349         for (i = 0; i <= 0x08; i++) {
350                 /* Get GR from file */
351                 GRdata = pVBInfo->StandTable[StandTableIndex].GRC[i];
352                 xgifb_reg_set(pVBInfo->P3ce, i, GRdata); /* Set GR(3ce) */
353         }
354
355         if (pVBInfo->ModeType > ModeVGA) {
356                 GRdata = (unsigned char) xgifb_reg_get(pVBInfo->P3ce, 0x05);
357                 GRdata &= 0xBF; /* 256 color disable */
358                 xgifb_reg_set(pVBInfo->P3ce, 0x05, GRdata);
359         }
360 }
361
362 static void XGI_ClearExt1Regs(struct vb_device_info *pVBInfo)
363 {
364         unsigned short i;
365
366         for (i = 0x0A; i <= 0x0E; i++)
367                 xgifb_reg_set(pVBInfo->P3c4, i, 0x00); /* Clear SR0A-SR0E */
368 }
369
370 static unsigned char XGI_SetDefaultVCLK(struct vb_device_info *pVBInfo)
371 {
372
373         xgifb_reg_and_or(pVBInfo->P3c4, 0x31, ~0x30, 0x20);
374         xgifb_reg_set(pVBInfo->P3c4, 0x2B, pVBInfo->VCLKData[0].SR2B);
375         xgifb_reg_set(pVBInfo->P3c4, 0x2C, pVBInfo->VCLKData[0].SR2C);
376
377         xgifb_reg_and_or(pVBInfo->P3c4, 0x31, ~0x30, 0x10);
378         xgifb_reg_set(pVBInfo->P3c4, 0x2B, pVBInfo->VCLKData[1].SR2B);
379         xgifb_reg_set(pVBInfo->P3c4, 0x2C, pVBInfo->VCLKData[1].SR2C);
380
381         xgifb_reg_and(pVBInfo->P3c4, 0x31, ~0x30);
382         return 0;
383 }
384
385 static unsigned char XGI_AjustCRT2Rate(unsigned short ModeNo,
386                 unsigned short ModeIdIndex,
387                 unsigned short RefreshRateTableIndex, unsigned short *i,
388                 struct vb_device_info *pVBInfo)
389 {
390         unsigned short tempax, tempbx, resinfo, modeflag, infoflag;
391
392         if (ModeNo <= 0x13)
393                 /* si+St_ModeFlag */
394                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
395         else
396                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
397
398         resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
399         tempbx = pVBInfo->RefIndex[RefreshRateTableIndex + (*i)].ModeID;
400         tempax = 0;
401
402         if (pVBInfo->IF_DEF_LVDS == 0) {
403                 if (pVBInfo->VBInfo & SetCRT2ToRAMDAC) {
404                         tempax |= SupportRAMDAC2;
405
406                         if (pVBInfo->VBType & VB_XGI301C)
407                                 tempax |= SupportCRT2in301C;
408                 }
409
410                 /* 301b */
411                 if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
412                         tempax |= SupportLCD;
413
414                         if (pVBInfo->LCDResInfo != Panel1280x1024) {
415                                 if (pVBInfo->LCDResInfo != Panel1280x960) {
416                                         if (pVBInfo->LCDInfo &
417                                             LCDNonExpanding) {
418                                                 if (resinfo >= 9) {
419                                                         tempax = 0;
420                                                         return 0;
421                                                 }
422                                         }
423                                 }
424                         }
425                 }
426
427                 if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) { /* for HiTV */
428                         if ((pVBInfo->VBType & VB_XGI301LV) &&
429                             (pVBInfo->VBExtInfo == VB_YPbPr1080i)) {
430                                 tempax |= SupportYPbPr;
431                                 if (pVBInfo->VBInfo & SetInSlaveMode) {
432                                         if (resinfo == 4)
433                                                 return 0;
434
435                                         if (resinfo == 3)
436                                                 return 0;
437
438                                         if (resinfo > 7)
439                                                 return 0;
440                                 }
441                         } else {
442                                 tempax |= SupportHiVisionTV;
443                                 if (pVBInfo->VBInfo & SetInSlaveMode) {
444                                         if (resinfo == 4)
445                                                 return 0;
446
447                                         if (resinfo == 3) {
448                                                 if (pVBInfo->SetFlag
449                                                                 & TVSimuMode)
450                                                         return 0;
451                                         }
452
453                                         if (resinfo > 7)
454                                                 return 0;
455                                 }
456                         }
457                 } else {
458                         if (pVBInfo->VBInfo & (SetCRT2ToAVIDEO |
459                                                SetCRT2ToSVIDEO |
460                                                SetCRT2ToSCART |
461                                                SetCRT2ToYPbPr |
462                                                SetCRT2ToHiVisionTV)) {
463                                 tempax |= SupportTV;
464
465                                 if (pVBInfo->VBType & (VB_XGI301B | VB_XGI302B
466                                                 | VB_XGI301LV | VB_XGI302LV
467                                                 | VB_XGI301C)) {
468                                         tempax |= SupportTV1024;
469                                 }
470
471                                 if (!(pVBInfo->VBInfo & SetPALTV)) {
472                                         if (modeflag & NoSupportSimuTV) {
473                                                 if (pVBInfo->VBInfo &
474                                                     SetInSlaveMode) {
475                                                         if (!(pVBInfo->VBInfo &
476                                                               SetNotSimuMode)) {
477                                                                 return 0;
478                                                         }
479                                                 }
480                                         }
481                                 }
482                         }
483                 }
484         } else { /* for LVDS */
485                 if (pVBInfo->VBInfo & SetCRT2ToLCD) {
486                         tempax |= SupportLCD;
487
488                         if (resinfo > 0x08)
489                                 return 0; /* 1024x768 */
490
491                         if (pVBInfo->LCDResInfo < Panel1024x768) {
492                                 if (resinfo > 0x07)
493                                         return 0; /* 800x600 */
494
495                                 if (resinfo == 0x04)
496                                         return 0; /* 512x384 */
497                         }
498                 }
499         }
500
501         for (; pVBInfo->RefIndex[RefreshRateTableIndex + (*i)].ModeID ==
502                tempbx; (*i)--) {
503                 infoflag = pVBInfo->RefIndex[RefreshRateTableIndex + (*i)].
504                                 Ext_InfoFlag;
505                 if (infoflag & tempax)
506                         return 1;
507
508                 if ((*i) == 0)
509                         break;
510         }
511
512         for ((*i) = 0;; (*i)++) {
513                 infoflag = pVBInfo->RefIndex[RefreshRateTableIndex + (*i)].
514                                 Ext_InfoFlag;
515                 if (pVBInfo->RefIndex[RefreshRateTableIndex + (*i)].ModeID
516                                 != tempbx) {
517                         return 0;
518                 }
519
520                 if (infoflag & tempax)
521                         return 1;
522         }
523         return 1;
524 }
525
526 static void XGI_SetSync(unsigned short RefreshRateTableIndex,
527                 struct vb_device_info *pVBInfo)
528 {
529         unsigned short sync, temp;
530
531         /* di+0x00 */
532         sync = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_InfoFlag >> 8;
533         sync &= 0xC0;
534         temp = 0x2F;
535         temp |= sync;
536         outb(temp, pVBInfo->P3c2); /* Set Misc(3c2) */
537 }
538
539 static void XGI_SetCRT1Timing_H(struct vb_device_info *pVBInfo,
540                 struct xgi_hw_device_info *HwDeviceExtension)
541 {
542         unsigned char data, data1, pushax;
543         unsigned short i, j;
544
545         /* xgifb_reg_set(pVBInfo->P3d4, 0x51, 0); */
546         /* xgifb_reg_set(pVBInfo->P3d4, 0x56, 0); */
547         /* xgifb_reg_and_or(pVBInfo->P3d4, 0x11, 0x7f, 0x00); */
548
549         /* unlock cr0-7 */
550         data = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x11);
551         data &= 0x7F;
552         xgifb_reg_set(pVBInfo->P3d4, 0x11, data);
553
554         data = pVBInfo->TimingH[0].data[0];
555         xgifb_reg_set(pVBInfo->P3d4, 0, data);
556
557         for (i = 0x01; i <= 0x04; i++) {
558                 data = pVBInfo->TimingH[0].data[i];
559                 xgifb_reg_set(pVBInfo->P3d4, (unsigned short) (i + 1), data);
560         }
561
562         for (i = 0x05; i <= 0x06; i++) {
563                 data = pVBInfo->TimingH[0].data[i];
564                 xgifb_reg_set(pVBInfo->P3c4, (unsigned short) (i + 6), data);
565         }
566
567         j = (unsigned char) xgifb_reg_get(pVBInfo->P3c4, 0x0e);
568         j &= 0x1F;
569         data = pVBInfo->TimingH[0].data[7];
570         data &= 0xE0;
571         data |= j;
572         xgifb_reg_set(pVBInfo->P3c4, 0x0e, data);
573
574         if (HwDeviceExtension->jChipType >= XG20) {
575                 data = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x04);
576                 data = data - 1;
577                 xgifb_reg_set(pVBInfo->P3d4, 0x04, data);
578                 data = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x05);
579                 data1 = data;
580                 data1 &= 0xE0;
581                 data &= 0x1F;
582                 if (data == 0) {
583                         pushax = data;
584                         data = (unsigned char) xgifb_reg_get(pVBInfo->P3c4,
585                                         0x0c);
586                         data &= 0xFB;
587                         xgifb_reg_set(pVBInfo->P3c4, 0x0c, data);
588                         data = pushax;
589                 }
590                 data = data - 1;
591                 data |= data1;
592                 xgifb_reg_set(pVBInfo->P3d4, 0x05, data);
593                 data = (unsigned char) xgifb_reg_get(pVBInfo->P3c4, 0x0e);
594                 data = data >> 5;
595                 data = data + 3;
596                 if (data > 7)
597                         data = data - 7;
598                 data = data << 5;
599                 xgifb_reg_and_or(pVBInfo->P3c4, 0x0e, ~0xE0, data);
600         }
601 }
602
603 static void XGI_SetCRT1Timing_V(unsigned short ModeIdIndex,
604                                 unsigned short ModeNo,
605                                 struct vb_device_info *pVBInfo)
606 {
607         unsigned char data;
608         unsigned short i, j;
609
610         /* xgifb_reg_set(pVBInfo->P3d4, 0x51, 0); */
611         /* xgifb_reg_set(pVBInfo->P3d4, 0x56, 0); */
612         /* xgifb_reg_and_or(pVBInfo->P3d4, 0x11, 0x7f, 0x00); */
613
614         for (i = 0x00; i <= 0x01; i++) {
615                 data = pVBInfo->TimingV[0].data[i];
616                 xgifb_reg_set(pVBInfo->P3d4, (unsigned short) (i + 6), data);
617         }
618
619         for (i = 0x02; i <= 0x03; i++) {
620                 data = pVBInfo->TimingV[0].data[i];
621                 xgifb_reg_set(pVBInfo->P3d4, (unsigned short) (i + 0x0e), data);
622         }
623
624         for (i = 0x04; i <= 0x05; i++) {
625                 data = pVBInfo->TimingV[0].data[i];
626                 xgifb_reg_set(pVBInfo->P3d4, (unsigned short) (i + 0x11), data);
627         }
628
629         j = (unsigned char) xgifb_reg_get(pVBInfo->P3c4, 0x0a);
630         j &= 0xC0;
631         data = pVBInfo->TimingV[0].data[6];
632         data &= 0x3F;
633         data |= j;
634         xgifb_reg_set(pVBInfo->P3c4, 0x0a, data);
635
636         data = pVBInfo->TimingV[0].data[6];
637         data &= 0x80;
638         data = data >> 2;
639
640         if (ModeNo <= 0x13)
641                 i = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
642         else
643                 i = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
644
645         i &= DoubleScanMode;
646         if (i)
647                 data |= 0x80;
648
649         j = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x09);
650         j &= 0x5F;
651         data |= j;
652         xgifb_reg_set(pVBInfo->P3d4, 0x09, data);
653 }
654
655 static void XGI_SetCRT1CRTC(unsigned short ModeNo, unsigned short ModeIdIndex,
656                 unsigned short RefreshRateTableIndex,
657                 struct vb_device_info *pVBInfo,
658                 struct xgi_hw_device_info *HwDeviceExtension)
659 {
660         unsigned char index, data;
661         unsigned short i;
662
663         /* Get index */
664         index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
665         index = index & IndexMask;
666
667         data = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x11);
668         data &= 0x7F;
669         xgifb_reg_set(pVBInfo->P3d4, 0x11, data); /* Unlock CRTC */
670
671         for (i = 0; i < 8; i++)
672                 pVBInfo->TimingH[0].data[i]
673                                 = pVBInfo->XGINEWUB_CRT1Table[index].CR[i];
674
675         for (i = 0; i < 7; i++)
676                 pVBInfo->TimingV[0].data[i]
677                                 = pVBInfo->XGINEWUB_CRT1Table[index].CR[i + 8];
678
679         XGI_SetCRT1Timing_H(pVBInfo, HwDeviceExtension);
680
681         XGI_SetCRT1Timing_V(ModeIdIndex, ModeNo, pVBInfo);
682
683         if (pVBInfo->ModeType > 0x03)
684                 xgifb_reg_set(pVBInfo->P3d4, 0x14, 0x4F);
685 }
686
687 /* --------------------------------------------------------------------- */
688 /* Function : XGI_SetXG21CRTC */
689 /* Input : Stand or enhance CRTC table */
690 /* Output : Fill CRT Hsync/Vsync to SR2E/SR2F/SR30/SR33/SR34/SR3F */
691 /* Description : Set LCD timing */
692 /* --------------------------------------------------------------------- */
693 static void XGI_SetXG21CRTC(unsigned short ModeNo, unsigned short ModeIdIndex,
694                 unsigned short RefreshRateTableIndex,
695                 struct vb_device_info *pVBInfo)
696 {
697         unsigned char StandTableIndex, index, Tempax, Tempbx, Tempcx, Tempdx;
698         unsigned short Temp1, Temp2, Temp3;
699
700         if (ModeNo <= 0x13) {
701                 StandTableIndex = XGI_GetModePtr(ModeNo, ModeIdIndex, pVBInfo);
702                 /* CR04 HRS */
703                 Tempax = pVBInfo->StandTable[StandTableIndex].CRTC[4];
704                 /* SR2E [7:0]->HRS */
705                 xgifb_reg_set(pVBInfo->P3c4, 0x2E, Tempax);
706                 /* Tempbx: CR05 HRE */
707                 Tempbx = pVBInfo->StandTable[StandTableIndex].CRTC[5];
708                 Tempbx &= 0x1F; /* Tempbx: HRE[4:0] */
709                 Tempcx = Tempax;
710                 Tempcx &= 0xE0; /* Tempcx: HRS[7:5] */
711                 Tempdx = Tempcx | Tempbx; /* Tempdx(HRE): HRS[7:5]HRE[4:0] */
712                 if (Tempbx < (Tempax & 0x1F)) /* IF HRE < HRS */
713                         Tempdx |= 0x20; /* Tempdx: HRE = HRE + 0x20 */
714                 Tempdx <<= 2; /* Tempdx << 2 */
715                 /* SR2F [7:2]->HRE */
716                 xgifb_reg_set(pVBInfo->P3c4, 0x2F, Tempdx);
717                 xgifb_reg_and_or(pVBInfo->P3c4, 0x30, 0xE3, 00);
718
719                 /* Tempax: CR16 VRS */
720                 Tempax = pVBInfo->StandTable[StandTableIndex].CRTC[16];
721                 Tempbx = Tempax; /* Tempbx=Tempax */
722                 Tempax &= 0x01; /* Tempax: VRS[0] */
723                 xgifb_reg_or(pVBInfo->P3c4, 0x33, Tempax); /* SR33[0]->VRS */
724
725                 /* Tempax: CR7 VRS */
726                 Tempax = pVBInfo->StandTable[StandTableIndex].CRTC[7];
727                 Tempdx = Tempbx >> 1; /* Tempdx: VRS[7:1] */
728                 Tempcx = Tempax & 0x04; /* Tempcx: CR7[2] */
729                 Tempcx <<= 5; /* Tempcx[7]: VRS[8] */
730                 Tempdx |= Tempcx; /* Tempdx: VRS[8:1] */
731                 /* SR34[7:0]: VRS[8:1] */
732                 xgifb_reg_set(pVBInfo->P3c4, 0x34, Tempdx);
733
734                 /* Temp1[8]: VRS[8] unsigned char -> unsigned short */
735                 Temp1 = Tempcx << 1;
736                 Temp1 |= Tempbx; /* Temp1[8:0]: VRS[8:0] */
737                 Tempax &= 0x80; /* Tempax[7]: CR7[7] */
738                 Temp2 = Tempax << 2; /* Temp2[9]: VRS[9] */
739                 Temp1 |= Temp2; /* Temp1[9:0]: VRS[9:0] */
740
741                 /* CR16 VRE */
742                 Tempax = pVBInfo->StandTable[StandTableIndex].CRTC[17];
743                 Tempax &= 0x0F; /* Tempax[3:0]: VRE[3:0] */
744                 Temp2 = Temp1 & 0x3F0; /* Temp2[9:4]: VRS[9:4] */
745                 Temp2 |= Tempax; /* Temp2[9:0]: VRE[9:0] */
746                 Temp3 = Temp1 & 0x0F; /* Temp3[3:0]: VRS[3:0] */
747                 if (Tempax < Temp3) /* VRE[3:0]<VRS[3:0] */
748                         Temp2 |= 0x10; /* Temp2: VRE + 0x10 */
749                 Temp2 &= 0xFF; /* Temp2[7:0]: VRE[7:0] */
750                 Tempax = (unsigned char) Temp2; /* Tempax[7:0]: VRE[7:0] */
751                 Tempax <<= 2; /* Tempax << 2: VRE[5:0] */
752                 Temp1 &= 0x600; /* Temp1[10:9]: VRS[10:9] */
753                 Temp1 >>= 9; /* [10:9]->[1:0] */
754                 Tempbx = (unsigned char) Temp1; /* Tempbx[1:0]: VRS[10:9] */
755                 Tempax |= Tempbx; /* VRE[5:0]VRS[10:9] */
756                 Tempax &= 0x7F;
757                 /* SR3F D[7:2]->VRE D[1:0]->VRS */
758                 xgifb_reg_set(pVBInfo->P3c4, 0x3F, Tempax);
759         } else {
760                 index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
761                 /* Tempax: CR4 HRS */
762                 Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[3];
763                 Tempcx = Tempax; /* Tempcx: HRS */
764                 /* SR2E[7:0]->HRS */
765                 xgifb_reg_set(pVBInfo->P3c4, 0x2E, Tempax);
766
767                 Tempdx = pVBInfo->XGINEWUB_CRT1Table[index].CR[5]; /* SRB */
768                 Tempdx &= 0xC0; /* Tempdx[7:6]: SRB[7:6] */
769                 Temp1 = Tempdx; /* Temp1[7:6]: HRS[9:8] */
770                 Temp1 <<= 2; /* Temp1[9:8]: HRS[9:8] */
771                 Temp1 |= Tempax; /* Temp1[9:0]: HRS[9:0] */
772
773                 Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[4]; /* CR5 HRE */
774                 Tempax &= 0x1F; /* Tempax[4:0]: HRE[4:0] */
775
776                 Tempbx = pVBInfo->XGINEWUB_CRT1Table[index].CR[6]; /* SRC */
777                 Tempbx &= 0x04; /* Tempbx[2]: HRE[5] */
778                 Tempbx <<= 3; /* Tempbx[5]: HRE[5] */
779                 Tempax |= Tempbx; /* Tempax[5:0]: HRE[5:0] */
780
781                 Temp2 = Temp1 & 0x3C0; /* Temp2[9:6]: HRS[9:6] */
782                 Temp2 |= Tempax; /* Temp2[9:0]: HRE[9:0] */
783
784                 Tempcx &= 0x3F; /* Tempcx[5:0]: HRS[5:0] */
785                 if (Tempax < Tempcx) /* HRE < HRS */
786                         Temp2 |= 0x40; /* Temp2 + 0x40 */
787
788                 Temp2 &= 0xFF;
789                 Tempax = (unsigned char) Temp2; /* Tempax: HRE[7:0] */
790                 Tempax <<= 2; /* Tempax[7:2]: HRE[5:0] */
791                 Tempdx >>= 6; /* Tempdx[7:6]->[1:0] HRS[9:8] */
792                 Tempax |= Tempdx; /* HRE[5:0]HRS[9:8] */
793                 /* SR2F D[7:2]->HRE, D[1:0]->HRS */
794                 xgifb_reg_set(pVBInfo->P3c4, 0x2F, Tempax);
795                 xgifb_reg_and_or(pVBInfo->P3c4, 0x30, 0xE3, 00);
796
797                 /* CR10 VRS */
798                 Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[10];
799                 Tempbx = Tempax; /* Tempbx: VRS */
800                 Tempax &= 0x01; /* Tempax[0]: VRS[0] */
801                 xgifb_reg_or(pVBInfo->P3c4, 0x33, Tempax); /* SR33[0]->VRS[0] */
802                 /* CR7[2][7] VRE */
803                 Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[9];
804                 Tempcx = Tempbx >> 1; /* Tempcx[6:0]: VRS[7:1] */
805                 Tempdx = Tempax & 0x04; /* Tempdx[2]: CR7[2] */
806                 Tempdx <<= 5; /* Tempdx[7]: VRS[8] */
807                 Tempcx |= Tempdx; /* Tempcx[7:0]: VRS[8:1] */
808                 xgifb_reg_set(pVBInfo->P3c4, 0x34, Tempcx); /* SR34[8:1]->VRS */
809
810                 Temp1 = Tempdx; /* Temp1[7]: Tempdx[7] */
811                 Temp1 <<= 1; /* Temp1[8]: VRS[8] */
812                 Temp1 |= Tempbx; /* Temp1[8:0]: VRS[8:0] */
813                 Tempax &= 0x80;
814                 Temp2 = Tempax << 2; /* Temp2[9]: VRS[9] */
815                 Temp1 |= Temp2; /* Temp1[9:0]: VRS[9:0] */
816                 /* Tempax: SRA */
817                 Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[14];
818                 Tempax &= 0x08; /* Tempax[3]: VRS[3] */
819                 Temp2 = Tempax;
820                 Temp2 <<= 7; /* Temp2[10]: VRS[10] */
821                 Temp1 |= Temp2; /* Temp1[10:0]: VRS[10:0] */
822
823                 /* Tempax: CR11 VRE */
824                 Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[11];
825                 Tempax &= 0x0F; /* Tempax[3:0]: VRE[3:0] */
826                 /* Tempbx: SRA */
827                 Tempbx = pVBInfo->XGINEWUB_CRT1Table[index].CR[14];
828                 Tempbx &= 0x20; /* Tempbx[5]: VRE[5] */
829                 Tempbx >>= 1; /* Tempbx[4]: VRE[4] */
830                 Tempax |= Tempbx; /* Tempax[4:0]: VRE[4:0] */
831                 Temp2 = Temp1 & 0x7E0; /* Temp2[10:5]: VRS[10:5] */
832                 Temp2 |= Tempax; /* Temp2[10:5]: VRE[10:5] */
833
834                 Temp3 = Temp1 & 0x1F; /* Temp3[4:0]: VRS[4:0] */
835                 if (Tempax < Temp3) /* VRE < VRS */
836                         Temp2 |= 0x20; /* VRE + 0x20 */
837
838                 Temp2 &= 0xFF;
839                 Tempax = (unsigned char) Temp2; /* Tempax: VRE[7:0] */
840                 Tempax <<= 2; /* Tempax[7:0]; VRE[5:0]00 */
841                 Temp1 &= 0x600; /* Temp1[10:9]: VRS[10:9] */
842                 Temp1 >>= 9; /* Temp1[1:0]: VRS[10:9] */
843                 Tempbx = (unsigned char) Temp1;
844                 Tempax |= Tempbx; /* Tempax[7:0]: VRE[5:0]VRS[10:9] */
845                 Tempax &= 0x7F;
846                 /* SR3F D[7:2]->VRE D[1:0]->VRS */
847                 xgifb_reg_set(pVBInfo->P3c4, 0x3F, Tempax);
848         }
849 }
850
851 static void XGI_SetXG27CRTC(unsigned short ModeNo,
852                             unsigned short ModeIdIndex,
853                             unsigned short RefreshRateTableIndex,
854                             struct vb_device_info *pVBInfo)
855 {
856         unsigned short StandTableIndex, index, Tempax, Tempbx, Tempcx, Tempdx;
857
858         if (ModeNo <= 0x13) {
859                 StandTableIndex = XGI_GetModePtr(ModeNo, ModeIdIndex, pVBInfo);
860                 /* CR04 HRS */
861                 Tempax = pVBInfo->StandTable[StandTableIndex].CRTC[4];
862                 /* SR2E [7:0]->HRS */
863                 xgifb_reg_set(pVBInfo->P3c4, 0x2E, Tempax);
864                 /* Tempbx: CR05 HRE */
865                 Tempbx = pVBInfo->StandTable[StandTableIndex].CRTC[5];
866                 Tempbx &= 0x1F; /* Tempbx: HRE[4:0] */
867                 Tempcx = Tempax;
868                 Tempcx &= 0xE0; /* Tempcx: HRS[7:5] */
869                 Tempdx = Tempcx | Tempbx; /* Tempdx(HRE): HRS[7:5]HRE[4:0] */
870                 if (Tempbx < (Tempax & 0x1F)) /* IF HRE < HRS */
871                         Tempdx |= 0x20; /* Tempdx: HRE = HRE + 0x20 */
872                 Tempdx <<= 2; /* Tempdx << 2 */
873                 /* SR2F [7:2]->HRE */
874                 xgifb_reg_set(pVBInfo->P3c4, 0x2F, Tempdx);
875                 xgifb_reg_and_or(pVBInfo->P3c4, 0x30, 0xE3, 00);
876
877                 /* Tempax: CR10 VRS */
878                 Tempax = pVBInfo->StandTable[StandTableIndex].CRTC[16];
879                 xgifb_reg_set(pVBInfo->P3c4, 0x34, Tempax); /* SR34[7:0]->VRS */
880                 Tempcx = Tempax; /* Tempcx=Tempax=VRS[7:0] */
881                 /* Tempax[7][2]: CR7[7][2] VRS[9][8] */
882                 Tempax = pVBInfo->StandTable[StandTableIndex].CRTC[7];
883                 Tempbx = Tempax; /* Tempbx=CR07 */
884                 Tempax &= 0x04; /* Tempax[2]: CR07[2] VRS[8] */
885                 Tempax >>= 2;
886                 /* SR35 D[0]->VRS D[8] */
887                 xgifb_reg_and_or(pVBInfo->P3c4, 0x35, ~0x01, Tempax);
888                 Tempcx |= (Tempax << 8); /* Tempcx[8] |= VRS[8] */
889                 Tempcx |= (Tempbx & 0x80) << 2; /* Tempcx[9] |= VRS[9] */
890
891                 /* CR11 VRE */
892                 Tempax = pVBInfo->StandTable[StandTableIndex].CRTC[17];
893                 Tempax &= 0x0F; /* Tempax: VRE[3:0] */
894                 Tempbx = Tempcx; /* Tempbx=Tempcx=VRS[9:0] */
895                 Tempbx &= 0x3F0; /* Tempbx[9:4]: VRS[9:4] */
896                 Tempbx |= Tempax; /* Tempbx[9:0]: VRE[9:0] */
897                 if (Tempax <= (Tempcx & 0x0F)) /* VRE[3:0]<=VRS[3:0] */
898                         Tempbx |= 0x10; /* Tempbx: VRE + 0x10 */
899                 /* Tempax[7:0]: VRE[7:0] */
900                 Tempax = (unsigned char) Tempbx & 0xFF;
901                 Tempax <<= 2; /* Tempax << 2: VRE[5:0] */
902                 Tempcx = (Tempcx & 0x600) >> 8; /* Tempcx VRS[10:9] */
903                 /* SR3F D[7:2]->VRE D[5:0] */
904                 xgifb_reg_and_or(pVBInfo->P3c4, 0x3F, ~0xFC, Tempax);
905                 /* SR35 D[2:1]->VRS[10:9] */
906                 xgifb_reg_and_or(pVBInfo->P3c4, 0x35, ~0x06, Tempcx);
907         } else {
908                 index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
909                 /* Tempax: CR4 HRS */
910                 Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[3];
911                 Tempbx = Tempax; /* Tempbx: HRS[7:0] */
912                 /* SR2E[7:0]->HRS */
913                 xgifb_reg_set(pVBInfo->P3c4, 0x2E, Tempax);
914
915                 /* SR0B */
916                 Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[5];
917                 Tempax &= 0xC0; /* Tempax[7:6]: SR0B[7:6]: HRS[9:8]*/
918                 Tempbx |= (Tempax << 2); /* Tempbx: HRS[9:0] */
919
920                 Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[4]; /* CR5 HRE */
921                 Tempax &= 0x1F; /* Tempax[4:0]: HRE[4:0] */
922                 Tempcx = Tempax; /* Tempcx: HRE[4:0] */
923
924                 Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[6]; /* SRC */
925                 Tempax &= 0x04; /* Tempax[2]: HRE[5] */
926                 Tempax <<= 3; /* Tempax[5]: HRE[5] */
927                 Tempcx |= Tempax; /* Tempcx[5:0]: HRE[5:0] */
928
929                 Tempbx = Tempbx & 0x3C0; /* Tempbx[9:6]: HRS[9:6] */
930                 Tempbx |= Tempcx; /* Tempbx: HRS[9:6]HRE[5:0] */
931
932                 /* Tempax: CR4 HRS */
933                 Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[3];
934                 Tempax &= 0x3F; /* Tempax: HRS[5:0] */
935                 if (Tempcx <= Tempax) /* HRE[5:0] < HRS[5:0] */
936                         Tempbx += 0x40; /* Tempbx= Tempbx + 0x40 : HRE[9:0]*/
937
938                 Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[5]; /* SR0B */
939                 Tempax &= 0xC0; /* Tempax[7:6]: SR0B[7:6]: HRS[9:8]*/
940                 Tempax >>= 6; /* Tempax[1:0]: HRS[9:8]*/
941                 Tempax |= ((Tempbx << 2) & 0xFF); /* Tempax[7:2]: HRE[5:0] */
942                 /* SR2F [7:2][1:0]: HRE[5:0]HRS[9:8] */
943                 xgifb_reg_set(pVBInfo->P3c4, 0x2F, Tempax);
944                 xgifb_reg_and_or(pVBInfo->P3c4, 0x30, 0xE3, 00);
945
946                 /* CR10 VRS */
947                 Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[10];
948                 /* SR34[7:0]->VRS[7:0] */
949                 xgifb_reg_set(pVBInfo->P3c4, 0x34, Tempax);
950
951                 Tempcx = Tempax; /* Tempcx <= VRS[7:0] */
952                 /* CR7[7][2] VRS[9][8] */
953                 Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[9];
954                 Tempbx = Tempax; /* Tempbx <= CR07[7:0] */
955                 Tempax = Tempax & 0x04; /* Tempax[2]: CR7[2]: VRS[8] */
956                 Tempax >>= 2; /* Tempax[0]: VRS[8] */
957                 /* SR35[0]: VRS[8] */
958                 xgifb_reg_and_or(pVBInfo->P3c4, 0x35, ~0x01, Tempax);
959                 Tempcx |= (Tempax << 8); /* Tempcx <= VRS[8:0] */
960                 Tempcx |= ((Tempbx & 0x80) << 2); /* Tempcx <= VRS[9:0] */
961                 /* Tempax: SR0A */
962                 Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[14];
963                 Tempax &= 0x08; /* SR0A[3] VRS[10] */
964                 Tempcx |= (Tempax << 7); /* Tempcx <= VRS[10:0] */
965
966                 /* Tempax: CR11 VRE */
967                 Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[11];
968                 Tempax &= 0x0F; /* Tempax[3:0]: VRE[3:0] */
969                 /* Tempbx: SR0A */
970                 Tempbx = pVBInfo->XGINEWUB_CRT1Table[index].CR[14];
971                 Tempbx &= 0x20; /* Tempbx[5]: SR0A[5]: VRE[4] */
972                 Tempbx >>= 1; /* Tempbx[4]: VRE[4] */
973                 Tempax |= Tempbx; /* Tempax[4:0]: VRE[4:0] */
974                 Tempbx = Tempcx; /* Tempbx: VRS[10:0] */
975                 Tempbx &= 0x7E0; /* Tempbx[10:5]: VRS[10:5] */
976                 Tempbx |= Tempax; /* Tempbx: VRS[10:5]VRE[4:0] */
977
978                 if (Tempbx <= Tempcx) /* VRE <= VRS */
979                         Tempbx |= 0x20; /* VRE + 0x20 */
980
981                 /* Tempax: Tempax[7:0]; VRE[5:0]00 */
982                 Tempax = (Tempbx << 2) & 0xFF;
983                 /* SR3F[7:2]:VRE[5:0] */
984                 xgifb_reg_and_or(pVBInfo->P3c4, 0x3F, ~0xFC, Tempax);
985                 Tempax = Tempcx >> 8;
986                 /* SR35[2:0]:VRS[10:8] */
987                 xgifb_reg_and_or(pVBInfo->P3c4, 0x35, ~0x07, Tempax);
988         }
989 }
990
991 /* --------------------------------------------------------------------- */
992 /* Function : XGI_SetXG21LCD */
993 /* Input : */
994 /* Output : FCLK duty cycle, FCLK delay compensation */
995 /* Description : All values set zero */
996 /* --------------------------------------------------------------------- */
997 static void XGI_SetXG21LCD(struct vb_device_info *pVBInfo,
998                 unsigned short RefreshRateTableIndex, unsigned short ModeNo)
999 {
1000         unsigned short Data, Temp, b3CC;
1001         unsigned short XGI_P3cc;
1002
1003         XGI_P3cc = pVBInfo->P3cc;
1004
1005         xgifb_reg_set(pVBInfo->P3d4, 0x2E, 0x00);
1006         xgifb_reg_set(pVBInfo->P3d4, 0x2F, 0x00);
1007         xgifb_reg_set(pVBInfo->P3d4, 0x46, 0x00);
1008         xgifb_reg_set(pVBInfo->P3d4, 0x47, 0x00);
1009         if (((*pVBInfo->pDVOSetting) & 0xC0) == 0xC0) {
1010                 xgifb_reg_set(pVBInfo->P3d4, 0x2E, *pVBInfo->pCR2E);
1011                 xgifb_reg_set(pVBInfo->P3d4, 0x2F, *pVBInfo->pCR2F);
1012                 xgifb_reg_set(pVBInfo->P3d4, 0x46, *pVBInfo->pCR46);
1013                 xgifb_reg_set(pVBInfo->P3d4, 0x47, *pVBInfo->pCR47);
1014         }
1015
1016         Temp = xgifb_reg_get(pVBInfo->P3d4, 0x37);
1017
1018         if (Temp & 0x01) {
1019                 xgifb_reg_or(pVBInfo->P3c4, 0x06, 0x40); /* 18 bits FP */
1020                 xgifb_reg_or(pVBInfo->P3c4, 0x09, 0x40);
1021         }
1022
1023         xgifb_reg_or(pVBInfo->P3c4, 0x1E, 0x01); /* Negative blank polarity */
1024
1025         xgifb_reg_and(pVBInfo->P3c4, 0x30, ~0x20);
1026         xgifb_reg_and(pVBInfo->P3c4, 0x35, ~0x80);
1027
1028         if (ModeNo <= 0x13) {
1029                 b3CC = (unsigned char) inb(XGI_P3cc);
1030                 if (b3CC & 0x40)
1031                         /* Hsync polarity */
1032                         xgifb_reg_or(pVBInfo->P3c4, 0x30, 0x20);
1033                 if (b3CC & 0x80)
1034                         /* Vsync polarity */
1035                         xgifb_reg_or(pVBInfo->P3c4, 0x35, 0x80);
1036         } else {
1037                 Data = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
1038                 if (Data & 0x4000)
1039                         /* Hsync polarity */
1040                         xgifb_reg_or(pVBInfo->P3c4, 0x30, 0x20);
1041                 if (Data & 0x8000)
1042                         /* Vsync polarity */
1043                         xgifb_reg_or(pVBInfo->P3c4, 0x35, 0x80);
1044         }
1045 }
1046
1047 static void XGI_SetXG27LCD(struct vb_device_info *pVBInfo,
1048                            unsigned short RefreshRateTableIndex,
1049                            unsigned short ModeNo)
1050 {
1051         unsigned short Data, Temp, b3CC;
1052         unsigned short XGI_P3cc;
1053
1054         XGI_P3cc = pVBInfo->P3cc;
1055
1056         xgifb_reg_set(pVBInfo->P3d4, 0x2E, 0x00);
1057         xgifb_reg_set(pVBInfo->P3d4, 0x2F, 0x00);
1058         xgifb_reg_set(pVBInfo->P3d4, 0x46, 0x00);
1059         xgifb_reg_set(pVBInfo->P3d4, 0x47, 0x00);
1060
1061         Temp = xgifb_reg_get(pVBInfo->P3d4, 0x37);
1062         if ((Temp & 0x03) == 0) { /* dual 12 */
1063                 xgifb_reg_set(pVBInfo->P3d4, 0x46, 0x13);
1064                 xgifb_reg_set(pVBInfo->P3d4, 0x47, 0x13);
1065         }
1066
1067         if (((*pVBInfo->pDVOSetting) & 0xC0) == 0xC0) {
1068                 xgifb_reg_set(pVBInfo->P3d4, 0x2E, *pVBInfo->pCR2E);
1069                 xgifb_reg_set(pVBInfo->P3d4, 0x2F, *pVBInfo->pCR2F);
1070                 xgifb_reg_set(pVBInfo->P3d4, 0x46, *pVBInfo->pCR46);
1071                 xgifb_reg_set(pVBInfo->P3d4, 0x47, *pVBInfo->pCR47);
1072         }
1073
1074         XGI_SetXG27FPBits(pVBInfo);
1075
1076         xgifb_reg_or(pVBInfo->P3c4, 0x1E, 0x01); /* Negative blank polarity */
1077
1078         xgifb_reg_and(pVBInfo->P3c4, 0x30, ~0x20); /* Hsync polarity */
1079         xgifb_reg_and(pVBInfo->P3c4, 0x35, ~0x80); /* Vsync polarity */
1080
1081         if (ModeNo <= 0x13) {
1082                 b3CC = (unsigned char) inb(XGI_P3cc);
1083                 if (b3CC & 0x40)
1084                         /* Hsync polarity */
1085                         xgifb_reg_or(pVBInfo->P3c4, 0x30, 0x20);
1086                 if (b3CC & 0x80)
1087                         /* Vsync polarity */
1088                         xgifb_reg_or(pVBInfo->P3c4, 0x35, 0x80);
1089         } else {
1090                 Data = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
1091                 if (Data & 0x4000)
1092                         /* Hsync polarity */
1093                         xgifb_reg_or(pVBInfo->P3c4, 0x30, 0x20);
1094                 if (Data & 0x8000)
1095                         /* Vsync polarity */
1096                         xgifb_reg_or(pVBInfo->P3c4, 0x35, 0x80);
1097         }
1098 }
1099
1100 /* --------------------------------------------------------------------- */
1101 /* Function : XGI_UpdateXG21CRTC */
1102 /* Input : */
1103 /* Output : CRT1 CRTC */
1104 /* Description : Modify CRT1 Hsync/Vsync to fix LCD mode timing */
1105 /* --------------------------------------------------------------------- */
1106 static void XGI_UpdateXG21CRTC(unsigned short ModeNo,
1107                                struct vb_device_info *pVBInfo,
1108                                unsigned short RefreshRateTableIndex)
1109 {
1110         int i, index = -1;
1111
1112         xgifb_reg_and(pVBInfo->P3d4, 0x11, 0x7F); /* Unlock CR0~7 */
1113         if (ModeNo <= 0x13) {
1114                 for (i = 0; i < 12; i++) {
1115                         if (ModeNo == pVBInfo->UpdateCRT1[i].ModeID)
1116                                 index = i;
1117                 }
1118         } else {
1119                 if (ModeNo == 0x2E &&
1120                     (pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC ==
1121                                                               RES640x480x60))
1122                         index = 12;
1123                 else if (ModeNo == 0x2E &&
1124                          (pVBInfo->RefIndex[RefreshRateTableIndex].
1125                                 Ext_CRT1CRTC == RES640x480x72))
1126                         index = 13;
1127                 else if (ModeNo == 0x2F)
1128                         index = 14;
1129                 else if (ModeNo == 0x50)
1130                         index = 15;
1131                 else if (ModeNo == 0x59)
1132                         index = 16;
1133         }
1134
1135         if (index != -1) {
1136                 xgifb_reg_set(pVBInfo->P3d4, 0x02,
1137                                 pVBInfo->UpdateCRT1[index].CR02);
1138                 xgifb_reg_set(pVBInfo->P3d4, 0x03,
1139                                 pVBInfo->UpdateCRT1[index].CR03);
1140                 xgifb_reg_set(pVBInfo->P3d4, 0x15,
1141                                 pVBInfo->UpdateCRT1[index].CR15);
1142                 xgifb_reg_set(pVBInfo->P3d4, 0x16,
1143                                 pVBInfo->UpdateCRT1[index].CR16);
1144         }
1145 }
1146
1147 static void XGI_SetCRT1DE(struct xgi_hw_device_info *HwDeviceExtension,
1148                 unsigned short ModeNo, unsigned short ModeIdIndex,
1149                 unsigned short RefreshRateTableIndex,
1150                 struct vb_device_info *pVBInfo)
1151 {
1152         unsigned short resindex, tempax, tempbx, tempcx, temp, modeflag;
1153
1154         unsigned char data;
1155
1156         resindex = XGI_GetResInfo(ModeNo, ModeIdIndex, pVBInfo);
1157
1158         if (ModeNo <= 0x13) {
1159                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
1160                 tempax = pVBInfo->StResInfo[resindex].HTotal;
1161                 tempbx = pVBInfo->StResInfo[resindex].VTotal;
1162         } else {
1163                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
1164                 tempax = pVBInfo->ModeResInfo[resindex].HTotal;
1165                 tempbx = pVBInfo->ModeResInfo[resindex].VTotal;
1166         }
1167
1168         if (modeflag & HalfDCLK)
1169                 tempax = tempax >> 1;
1170
1171         if (ModeNo > 0x13) {
1172                 if (modeflag & HalfDCLK)
1173                         tempax = tempax << 1;
1174
1175                 temp = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
1176
1177                 if (temp & InterlaceMode)
1178                         tempbx = tempbx >> 1;
1179
1180                 if (modeflag & DoubleScanMode)
1181                         tempbx = tempbx << 1;
1182         }
1183
1184         tempcx = 8;
1185
1186         /* if (!(modeflag & Charx8Dot)) */
1187         /* tempcx = 9; */
1188
1189         tempax /= tempcx;
1190         tempax -= 1;
1191         tempbx -= 1;
1192         tempcx = tempax;
1193         temp = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x11);
1194         data = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x11);
1195         data &= 0x7F;
1196         xgifb_reg_set(pVBInfo->P3d4, 0x11, data); /* Unlock CRTC */
1197         xgifb_reg_set(pVBInfo->P3d4, 0x01, (unsigned short) (tempcx & 0xff));
1198         xgifb_reg_and_or(pVBInfo->P3d4, 0x0b, ~0x0c,
1199                         (unsigned short) ((tempcx & 0x0ff00) >> 10));
1200         xgifb_reg_set(pVBInfo->P3d4, 0x12, (unsigned short) (tempbx & 0xff));
1201         tempax = 0;
1202         tempbx = tempbx >> 8;
1203
1204         if (tempbx & 0x01)
1205                 tempax |= 0x02;
1206
1207         if (tempbx & 0x02)
1208                 tempax |= 0x40;
1209
1210         xgifb_reg_and_or(pVBInfo->P3d4, 0x07, ~0x42, tempax);
1211         data = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x07);
1212         data &= 0xFF;
1213         tempax = 0;
1214
1215         if (tempbx & 0x04)
1216                 tempax |= 0x02;
1217
1218         xgifb_reg_and_or(pVBInfo->P3d4, 0x0a, ~0x02, tempax);
1219         xgifb_reg_set(pVBInfo->P3d4, 0x11, temp);
1220 }
1221
1222 unsigned short XGI_GetResInfo(unsigned short ModeNo,
1223                 unsigned short ModeIdIndex, struct vb_device_info *pVBInfo)
1224 {
1225         unsigned short resindex;
1226
1227         if (ModeNo <= 0x13)
1228                 /* si+St_ResInfo */
1229                 resindex = pVBInfo->SModeIDTable[ModeIdIndex].St_ResInfo;
1230         else
1231                 /* si+Ext_ResInfo */
1232                 resindex = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
1233         return resindex;
1234 }
1235
1236 static void XGI_SetCRT1Offset(unsigned short ModeNo,
1237                               unsigned short ModeIdIndex,
1238                               unsigned short RefreshRateTableIndex,
1239                               struct xgi_hw_device_info *HwDeviceExtension,
1240                               struct vb_device_info *pVBInfo)
1241 {
1242         unsigned short temp, ah, al, temp2, i, DisplayUnit;
1243
1244         /* GetOffset */
1245         temp = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeInfo;
1246         temp = temp >> 8;
1247         temp = pVBInfo->ScreenOffset[temp];
1248
1249         temp2 = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
1250         temp2 &= InterlaceMode;
1251
1252         if (temp2)
1253                 temp = temp << 1;
1254
1255         temp2 = pVBInfo->ModeType - ModeEGA;
1256
1257         switch (temp2) {
1258         case 0:
1259                 temp2 = 1;
1260                 break;
1261         case 1:
1262                 temp2 = 2;
1263                 break;
1264         case 2:
1265                 temp2 = 4;
1266                 break;
1267         case 3:
1268                 temp2 = 4;
1269                 break;
1270         case 4:
1271                 temp2 = 6;
1272                 break;
1273         case 5:
1274                 temp2 = 8;
1275                 break;
1276         default:
1277                 break;
1278         }
1279
1280         if ((ModeNo >= 0x26) && (ModeNo <= 0x28))
1281                 temp = temp * temp2 + temp2 / 2;
1282         else
1283                 temp *= temp2;
1284
1285         /* SetOffset */
1286         DisplayUnit = temp;
1287         temp2 = temp;
1288         temp = temp >> 8; /* ah */
1289         temp &= 0x0F;
1290         i = xgifb_reg_get(pVBInfo->P3c4, 0x0E);
1291         i &= 0xF0;
1292         i |= temp;
1293         xgifb_reg_set(pVBInfo->P3c4, 0x0E, i);
1294
1295         temp = (unsigned char) temp2;
1296         temp &= 0xFF; /* al */
1297         xgifb_reg_set(pVBInfo->P3d4, 0x13, temp);
1298
1299         /* SetDisplayUnit */
1300         temp2 = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
1301         temp2 &= InterlaceMode;
1302         if (temp2)
1303                 DisplayUnit >>= 1;
1304
1305         DisplayUnit = DisplayUnit << 5;
1306         ah = (DisplayUnit & 0xff00) >> 8;
1307         al = DisplayUnit & 0x00ff;
1308         if (al == 0)
1309                 ah += 1;
1310         else
1311                 ah += 2;
1312
1313         if (HwDeviceExtension->jChipType >= XG20)
1314                 if ((ModeNo == 0x4A) | (ModeNo == 0x49))
1315                         ah -= 1;
1316
1317         xgifb_reg_set(pVBInfo->P3c4, 0x10, ah);
1318 }
1319
1320 static unsigned short XGI_GetVCLK2Ptr(unsigned short ModeNo,
1321                 unsigned short ModeIdIndex,
1322                 unsigned short RefreshRateTableIndex,
1323                 struct xgi_hw_device_info *HwDeviceExtension,
1324                 struct vb_device_info *pVBInfo)
1325 {
1326         unsigned short LCDXlat1VCLK[4] = { VCLK65 + 2,
1327                                            VCLK65 + 2,
1328                                            VCLK65 + 2,
1329                                            VCLK65 + 2 };
1330         unsigned short LCDXlat2VCLK[4] = { VCLK108_2 + 5,
1331                                            VCLK108_2 + 5,
1332                                            VCLK108_2 + 5,
1333                                            VCLK108_2 + 5 };
1334         unsigned short LVDSXlat1VCLK[4] = { VCLK40, VCLK40, VCLK40, VCLK40 };
1335         unsigned short LVDSXlat2VCLK[4] = { VCLK65 + 2,
1336                                             VCLK65 + 2,
1337                                             VCLK65 + 2,
1338                                             VCLK65 + 2 };
1339         unsigned short LVDSXlat3VCLK[4] = { VCLK65 + 2,
1340                                             VCLK65 + 2,
1341                                             VCLK65 + 2,
1342                                             VCLK65 + 2 };
1343
1344         unsigned short CRT2Index, VCLKIndex;
1345         unsigned short modeflag, resinfo;
1346
1347         if (ModeNo <= 0x13) {
1348                 /* si+St_ResInfo */
1349                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
1350                 resinfo = pVBInfo->SModeIDTable[ModeIdIndex].St_ResInfo;
1351                 CRT2Index = pVBInfo->SModeIDTable[ModeIdIndex].St_CRT2CRTC;
1352         } else {
1353                 /* si+Ext_ResInfo */
1354                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
1355                 resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
1356                 CRT2Index = pVBInfo->RefIndex[RefreshRateTableIndex].
1357                                 Ext_CRT2CRTC;
1358         }
1359
1360         if (pVBInfo->IF_DEF_LVDS == 0) {
1361                 CRT2Index = CRT2Index >> 6; /*  for LCD */
1362                 if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) { /*301b*/
1363                         if (pVBInfo->LCDResInfo != Panel1024x768)
1364                                 VCLKIndex = LCDXlat2VCLK[CRT2Index];
1365                         else
1366                                 VCLKIndex = LCDXlat1VCLK[CRT2Index];
1367                 } else { /* for TV */
1368                         if (pVBInfo->VBInfo & SetCRT2ToTV) {
1369                                 if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {
1370                                         if (pVBInfo->SetFlag & RPLLDIV2XO) {
1371                                                 VCLKIndex = HiTVVCLKDIV2;
1372                                                 VCLKIndex += 25;
1373                                         } else {
1374                                                 VCLKIndex = HiTVVCLK;
1375                                                 VCLKIndex += 25;
1376                                         }
1377
1378                                         if (pVBInfo->SetFlag & TVSimuMode) {
1379                                                 if (modeflag & Charx8Dot) {
1380                                                         VCLKIndex =
1381                                                                 HiTVSimuVCLK;
1382                                                         VCLKIndex += 25;
1383                                                 } else {
1384                                                         VCLKIndex =
1385                                                                 HiTVTextVCLK;
1386                                                         VCLKIndex += 25;
1387                                                 }
1388                                         }
1389
1390                                         /* 301lv */
1391                                         if (pVBInfo->VBType & VB_XGI301LV) {
1392                                                 if (!(pVBInfo->VBExtInfo ==
1393                                                      VB_YPbPr1080i)) {
1394                                                         VCLKIndex =
1395                                                                 YPbPr750pVCLK;
1396                                                         if (!(pVBInfo->VBExtInfo
1397                                                                         ==
1398                                                              VB_YPbPr750p)) {
1399                                                                 VCLKIndex =
1400                                                                   YPbPr525pVCLK;
1401                                                                 if (!(pVBInfo->VBExtInfo
1402                                                                                 == VB_YPbPr525p)) {
1403                                                                         VCLKIndex
1404                                                                                         = YPbPr525iVCLK_2;
1405                                                                         if (!(pVBInfo->SetFlag
1406                                                                                         & RPLLDIV2XO))
1407                                                                                 VCLKIndex
1408                                                                                                 = YPbPr525iVCLK;
1409                                                                 }
1410                                                         }
1411                                                 }
1412                                         }
1413                                 } else {
1414                                         if (pVBInfo->VBInfo & SetCRT2ToTV) {
1415                                                 if (pVBInfo->SetFlag &
1416                                                     RPLLDIV2XO) {
1417                                                         VCLKIndex = TVVCLKDIV2;
1418                                                         VCLKIndex += 25;
1419                                                 } else {
1420                                                         VCLKIndex = TVVCLK;
1421                                                         VCLKIndex += 25;
1422                                                 }
1423                                         }
1424                                 }
1425                         } else { /* for CRT2 */
1426                                 /* Port 3cch */
1427                                 VCLKIndex = (unsigned char) inb(
1428                                                 (pVBInfo->P3ca + 0x02));
1429                                 VCLKIndex = ((VCLKIndex >> 2) & 0x03);
1430                                 if (ModeNo > 0x13) {
1431                                         /* di+Ext_CRTVCLK */
1432                                         VCLKIndex =
1433                                                 pVBInfo->RefIndex[
1434                                                         RefreshRateTableIndex].
1435                                                                 Ext_CRTVCLK;
1436                                         VCLKIndex &= IndexMask;
1437                                 }
1438                         }
1439                 }
1440         } else { /* LVDS */
1441                 if (ModeNo <= 0x13)
1442                         VCLKIndex = CRT2Index;
1443                 else
1444                         VCLKIndex = CRT2Index;
1445
1446                 VCLKIndex = VCLKIndex >> 6;
1447                 if ((pVBInfo->LCDResInfo == Panel800x600) ||
1448                     (pVBInfo->LCDResInfo == Panel320x480))
1449                         VCLKIndex = LVDSXlat1VCLK[VCLKIndex];
1450                 else if ((pVBInfo->LCDResInfo == Panel1024x768) ||
1451                          (pVBInfo->LCDResInfo == Panel1024x768x75))
1452                         VCLKIndex = LVDSXlat2VCLK[VCLKIndex];
1453                 else
1454                         VCLKIndex = LVDSXlat3VCLK[VCLKIndex];
1455         }
1456         /* VCLKIndex = VCLKIndex&IndexMask; */
1457
1458         return VCLKIndex;
1459 }
1460
1461 static void XGI_SetCRT1VCLK(unsigned short ModeNo,
1462                             unsigned short ModeIdIndex,
1463                             struct xgi_hw_device_info *HwDeviceExtension,
1464                             unsigned short RefreshRateTableIndex,
1465                             struct vb_device_info *pVBInfo)
1466 {
1467         unsigned char index, data;
1468         unsigned short vclkindex;
1469
1470         if (pVBInfo->IF_DEF_LVDS == 1) {
1471                 index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
1472                 data = xgifb_reg_get(pVBInfo->P3c4, 0x31) & 0xCF;
1473                 xgifb_reg_set(pVBInfo->P3c4, 0x31, data);
1474                 xgifb_reg_set(pVBInfo->P3c4, 0x2B,
1475                                 pVBInfo->VCLKData[index].SR2B);
1476                 xgifb_reg_set(pVBInfo->P3c4, 0x2C,
1477                                 pVBInfo->VCLKData[index].SR2C);
1478                 xgifb_reg_set(pVBInfo->P3c4, 0x2D, 0x01);
1479         } else if ((pVBInfo->VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV
1480                         | VB_XGI302LV | VB_XGI301C)) && (pVBInfo->VBInfo
1481                         & SetCRT2ToLCDA)) {
1482                 vclkindex = XGI_GetVCLK2Ptr(ModeNo, ModeIdIndex,
1483                                 RefreshRateTableIndex, HwDeviceExtension,
1484                                 pVBInfo);
1485                 data = xgifb_reg_get(pVBInfo->P3c4, 0x31) & 0xCF;
1486                 xgifb_reg_set(pVBInfo->P3c4, 0x31, data);
1487                 data = pVBInfo->VBVCLKData[vclkindex].Part4_A;
1488                 xgifb_reg_set(pVBInfo->P3c4, 0x2B, data);
1489                 data = pVBInfo->VBVCLKData[vclkindex].Part4_B;
1490                 xgifb_reg_set(pVBInfo->P3c4, 0x2C, data);
1491                 xgifb_reg_set(pVBInfo->P3c4, 0x2D, 0x01);
1492         } else {
1493                 index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
1494                 data = xgifb_reg_get(pVBInfo->P3c4, 0x31) & 0xCF;
1495                 xgifb_reg_set(pVBInfo->P3c4, 0x31, data);
1496                 xgifb_reg_set(pVBInfo->P3c4, 0x2B,
1497                                 pVBInfo->VCLKData[index].SR2B);
1498                 xgifb_reg_set(pVBInfo->P3c4, 0x2C,
1499                                 pVBInfo->VCLKData[index].SR2C);
1500                 xgifb_reg_set(pVBInfo->P3c4, 0x2D, 0x01);
1501         }
1502
1503         if (HwDeviceExtension->jChipType >= XG20) {
1504                 if (pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag &
1505                     HalfDCLK) {
1506                         data = xgifb_reg_get(pVBInfo->P3c4, 0x2B);
1507                         xgifb_reg_set(pVBInfo->P3c4, 0x2B, data);
1508                         data = xgifb_reg_get(pVBInfo->P3c4, 0x2C);
1509                         index = data;
1510                         index &= 0xE0;
1511                         data &= 0x1F;
1512                         data = data << 1;
1513                         data += 1;
1514                         data |= index;
1515                         xgifb_reg_set(pVBInfo->P3c4, 0x2C, data);
1516                 }
1517         }
1518 }
1519
1520 static void XGI_SetCRT1FIFO(unsigned short ModeNo,
1521                 struct xgi_hw_device_info *HwDeviceExtension,
1522                 struct vb_device_info *pVBInfo)
1523 {
1524         unsigned short data;
1525
1526         data = xgifb_reg_get(pVBInfo->P3c4, 0x3D);
1527         data &= 0xfe;
1528         xgifb_reg_set(pVBInfo->P3c4, 0x3D, data); /* diable auto-threshold */
1529
1530         if (ModeNo > 0x13) {
1531                 xgifb_reg_set(pVBInfo->P3c4, 0x08, 0x34);
1532                 data = xgifb_reg_get(pVBInfo->P3c4, 0x09);
1533                 data &= 0xC0;
1534                 xgifb_reg_set(pVBInfo->P3c4, 0x09, data | 0x30);
1535                 data = xgifb_reg_get(pVBInfo->P3c4, 0x3D);
1536                 data |= 0x01;
1537                 xgifb_reg_set(pVBInfo->P3c4, 0x3D, data);
1538         } else {
1539                 if (HwDeviceExtension->jChipType == XG27) {
1540                         xgifb_reg_set(pVBInfo->P3c4, 0x08, 0x0E);
1541                         data = xgifb_reg_get(pVBInfo->P3c4, 0x09);
1542                         data &= 0xC0;
1543                         xgifb_reg_set(pVBInfo->P3c4, 0x09, data | 0x20);
1544                 } else {
1545                         xgifb_reg_set(pVBInfo->P3c4, 0x08, 0xAE);
1546                         data = xgifb_reg_get(pVBInfo->P3c4, 0x09);
1547                         data &= 0xF0;
1548                         xgifb_reg_set(pVBInfo->P3c4, 0x09, data);
1549                 }
1550         }
1551
1552         if (HwDeviceExtension->jChipType == XG21)
1553                 XGI_SetXG21FPBits(pVBInfo); /* Fix SR9[7:6] can't read back */
1554 }
1555
1556 static void XGI_SetVCLKState(struct xgi_hw_device_info *HwDeviceExtension,
1557                 unsigned short ModeNo, unsigned short RefreshRateTableIndex,
1558                 struct vb_device_info *pVBInfo)
1559 {
1560         unsigned short data, data2 = 0;
1561         short VCLK;
1562
1563         unsigned char index;
1564
1565         if (ModeNo <= 0x13)
1566                 VCLK = 0;
1567         else {
1568                 index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
1569                 index &= IndexMask;
1570                 VCLK = pVBInfo->VCLKData[index].CLOCK;
1571         }
1572
1573         data = xgifb_reg_get(pVBInfo->P3c4, 0x32);
1574         data &= 0xf3;
1575         if (VCLK >= 200)
1576                 data |= 0x0c; /* VCLK > 200 */
1577
1578         if (HwDeviceExtension->jChipType >= XG20)
1579                 data &= ~0x04; /* 2 pixel mode */
1580
1581         xgifb_reg_set(pVBInfo->P3c4, 0x32, data);
1582
1583         if (HwDeviceExtension->jChipType < XG20) {
1584                 data = xgifb_reg_get(pVBInfo->P3c4, 0x1F);
1585                 data &= 0xE7;
1586                 if (VCLK < 200)
1587                         data |= 0x10;
1588                 xgifb_reg_set(pVBInfo->P3c4, 0x1F, data);
1589         }
1590
1591         /*  Jong for Adavantech LCD ripple issue
1592         if ((VCLK >= 0) && (VCLK < 135))
1593                 data2 = 0x03;
1594         else if ((VCLK >= 135) && (VCLK < 160))
1595                 data2 = 0x02;
1596         else if ((VCLK >= 160) && (VCLK < 260))
1597                 data2 = 0x01;
1598         else if (VCLK > 260)
1599                 data2 = 0x00;
1600         */
1601         data2 = 0x00;
1602
1603         xgifb_reg_and_or(pVBInfo->P3c4, 0x07, 0xFC, data2);
1604         if (HwDeviceExtension->jChipType >= XG27)
1605                 xgifb_reg_and_or(pVBInfo->P3c4, 0x40, 0xFC, data2 & 0x03);
1606
1607 }
1608
1609 static void XGI_SetCRT1ModeRegs(struct xgi_hw_device_info *HwDeviceExtension,
1610                 unsigned short ModeNo, unsigned short ModeIdIndex,
1611                 unsigned short RefreshRateTableIndex,
1612                 struct vb_device_info *pVBInfo)
1613 {
1614         unsigned short data, data2, data3, infoflag = 0, modeflag, resindex,
1615                         xres;
1616
1617         if (ModeNo > 0x13) {
1618                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
1619                 infoflag = pVBInfo->RefIndex[RefreshRateTableIndex].
1620                                 Ext_InfoFlag;
1621         } else
1622                 /* si+St_ModeFlag */
1623                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
1624
1625         if (xgifb_reg_get(pVBInfo->P3d4, 0x31) & 0x01)
1626                 xgifb_reg_and_or(pVBInfo->P3c4, 0x1F, 0x3F, 0x00);
1627
1628         if (ModeNo > 0x13)
1629                 data = infoflag;
1630         else
1631                 data = 0;
1632
1633         data2 = 0;
1634
1635         if (ModeNo > 0x13) {
1636                 if (pVBInfo->ModeType > 0x02) {
1637                         data2 |= 0x02;
1638                         data3 = pVBInfo->ModeType - ModeVGA;
1639                         data3 = data3 << 2;
1640                         data2 |= data3;
1641                 }
1642         }
1643
1644         data &= InterlaceMode;
1645
1646         if (data)
1647                 data2 |= 0x20;
1648
1649         xgifb_reg_and_or(pVBInfo->P3c4, 0x06, ~0x3F, data2);
1650         /* xgifb_reg_set(pVBInfo->P3c4,0x06,data2); */
1651         resindex = XGI_GetResInfo(ModeNo, ModeIdIndex, pVBInfo);
1652         if (ModeNo <= 0x13)
1653                 xres = pVBInfo->StResInfo[resindex].HTotal;
1654         else
1655                 xres = pVBInfo->ModeResInfo[resindex].HTotal; /* xres->ax */
1656
1657         data = 0x0000;
1658         if (infoflag & InterlaceMode) {
1659                 if (xres == 1024)
1660                         data = 0x0035;
1661                 else if (xres == 1280)
1662                         data = 0x0048;
1663         }
1664
1665         data2 = data & 0x00FF;
1666         xgifb_reg_and_or(pVBInfo->P3d4, 0x19, 0xFF, data2);
1667         data2 = (data & 0xFF00) >> 8;
1668         xgifb_reg_and_or(pVBInfo->P3d4, 0x19, 0xFC, data2);
1669
1670         if (modeflag & HalfDCLK)
1671                 xgifb_reg_and_or(pVBInfo->P3c4, 0x01, 0xF7, 0x08);
1672
1673         data2 = 0;
1674
1675         if (modeflag & LineCompareOff)
1676                 data2 |= 0x08;
1677
1678         if (ModeNo > 0x13) {
1679                 if (pVBInfo->ModeType == ModeEGA)
1680                         data2 |= 0x40;
1681         }
1682
1683         xgifb_reg_and_or(pVBInfo->P3c4, 0x0F, ~0x48, data2);
1684         data = 0x60;
1685         if (pVBInfo->ModeType != ModeText) {
1686                 data = data ^ 0x60;
1687                 if (pVBInfo->ModeType != ModeEGA)
1688                         data = data ^ 0xA0;
1689         }
1690         xgifb_reg_and_or(pVBInfo->P3c4, 0x21, 0x1F, data);
1691
1692         XGI_SetVCLKState(HwDeviceExtension, ModeNo, RefreshRateTableIndex,
1693                         pVBInfo);
1694
1695         /* if (modeflag&HalfDCLK) //030305 fix lowresolution bug */
1696         /* if (XGINew_IF_DEF_NEW_LOWRES) */
1697         /* XGI_VesaLowResolution(ModeNo, ModeIdIndex);
1698          * //030305 fix lowresolution bug */
1699
1700         data = xgifb_reg_get(pVBInfo->P3d4, 0x31);
1701
1702         if (HwDeviceExtension->jChipType == XG27) {
1703                 if (data & 0x40)
1704                         data = 0x2c;
1705                 else
1706                         data = 0x6c;
1707                 xgifb_reg_set(pVBInfo->P3d4, 0x52, data);
1708                 xgifb_reg_or(pVBInfo->P3d4, 0x51, 0x10);
1709         } else if (HwDeviceExtension->jChipType >= XG20) {
1710                 if (data & 0x40)
1711                         data = 0x33;
1712                 else
1713                         data = 0x73;
1714                 xgifb_reg_set(pVBInfo->P3d4, 0x52, data);
1715                 xgifb_reg_set(pVBInfo->P3d4, 0x51, 0x02);
1716         } else {
1717                 if (data & 0x40)
1718                         data = 0x2c;
1719                 else
1720                         data = 0x6c;
1721                 xgifb_reg_set(pVBInfo->P3d4, 0x52, data);
1722         }
1723
1724 }
1725
1726 /*
1727 void XGI_VesaLowResolution(unsigned short ModeNo,
1728                            unsigned short ModeIdIndex,
1729                            struct vb_device_info *pVBInfo)
1730 {
1731         unsigned short modeflag;
1732
1733         if (ModeNo > 0x13)
1734                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
1735         else
1736                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
1737
1738         if (ModeNo > 0x13) {
1739                 if (modeflag & DoubleScanMode) {
1740                         if (modeflag & HalfDCLK) {
1741                                 if (pVBInfo->VBType & VB_XGI301B |
1742                                                       VB_XGI302B |
1743                                                       VB_XGI301LV |
1744                                                       VB_XGI302LV |
1745                                                       VB_XGI301C)) {
1746                                         if (!(pVBInfo->VBInfo &
1747                                              SetCRT2ToRAMDAC)) {
1748                                                 if (pVBInfo->VBInfo &
1749                                                     SetInSlaveMode) {
1750                                                         xgifb_reg_and_or(
1751                                                                 pVBInfo->P3c4,
1752                                                                 0x01,
1753                                                                 0xf7,
1754                                                                 0x00);
1755                                                         xgifb_reg_and_or(
1756                                                                 pVBInfo->P3c4,
1757                                                                 0x0f,
1758                                                                 0x7f,
1759                                                                 0x00);
1760                                                         return;
1761                                                 }
1762                                         }
1763                                 }
1764                                 xgifb_reg_and_or(pVBInfo->P3c4,
1765                                                  0x0f,
1766                                                  0xff,
1767                                                  0x80);
1768                                 xgifb_reg_and_or(pVBInfo->P3c4,
1769                                                  0x01,
1770                                                  0xf7,
1771                                                  0x00);
1772                                 return;
1773                         }
1774                 }
1775         }
1776         xgifb_reg_and_or(pVBInfo->P3c4, 0x0f, 0x7f, 0x00);
1777 }
1778 */
1779
1780 static void XGI_WriteDAC(unsigned short dl,
1781                          unsigned short ah,
1782                          unsigned short al,
1783                          unsigned short dh,
1784                          struct vb_device_info *pVBInfo)
1785 {
1786         unsigned short temp, bh, bl;
1787
1788         bh = ah;
1789         bl = al;
1790
1791         if (dl != 0) {
1792                 temp = bh;
1793                 bh = dh;
1794                 dh = temp;
1795                 if (dl == 1) {
1796                         temp = bl;
1797                         bl = dh;
1798                         dh = temp;
1799                 } else {
1800                         temp = bl;
1801                         bl = bh;
1802                         bh = temp;
1803                 }
1804         }
1805         outb((unsigned short) dh, pVBInfo->P3c9);
1806         outb((unsigned short) bh, pVBInfo->P3c9);
1807         outb((unsigned short) bl, pVBInfo->P3c9);
1808 }
1809
1810 static void XGI_LoadDAC(unsigned short ModeNo, unsigned short ModeIdIndex,
1811                 struct vb_device_info *pVBInfo)
1812 {
1813         unsigned short data, data2, time, i, j, k, m, n, o, si, di, bx, dl, al,
1814                         ah, dh, *table = NULL;
1815
1816         if (ModeNo <= 0x13)
1817                 data = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
1818         else
1819                 data = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
1820
1821         data &= DACInfoFlag;
1822         time = 64;
1823
1824         if (data == 0x00)
1825                 table = XGINew_MDA_DAC;
1826         else if (data == 0x08)
1827                 table = XGINew_CGA_DAC;
1828         else if (data == 0x10)
1829                 table = XGINew_EGA_DAC;
1830         else if (data == 0x18) {
1831                 time = 256;
1832                 table = XGINew_VGA_DAC;
1833         }
1834
1835         if (time == 256)
1836                 j = 16;
1837         else
1838                 j = time;
1839
1840         outb(0xFF, pVBInfo->P3c6);
1841         outb(0x00, pVBInfo->P3c8);
1842
1843         for (i = 0; i < j; i++) {
1844                 data = table[i];
1845
1846                 for (k = 0; k < 3; k++) {
1847                         data2 = 0;
1848
1849                         if (data & 0x01)
1850                                 data2 = 0x2A;
1851
1852                         if (data & 0x02)
1853                                 data2 += 0x15;
1854
1855                         outb(data2, pVBInfo->P3c9);
1856                         data = data >> 2;
1857                 }
1858         }
1859
1860         if (time == 256) {
1861                 for (i = 16; i < 32; i++) {
1862                         data = table[i];
1863
1864                         for (k = 0; k < 3; k++)
1865                                 outb(data, pVBInfo->P3c9);
1866                 }
1867
1868                 si = 32;
1869
1870                 for (m = 0; m < 9; m++) {
1871                         di = si;
1872                         bx = si + 0x04;
1873                         dl = 0;
1874
1875                         for (n = 0; n < 3; n++) {
1876                                 for (o = 0; o < 5; o++) {
1877                                         dh = table[si];
1878                                         ah = table[di];
1879                                         al = table[bx];
1880                                         si++;
1881                                         XGI_WriteDAC(dl, ah, al, dh, pVBInfo);
1882                                 }
1883
1884                                 si -= 2;
1885
1886                                 for (o = 0; o < 3; o++) {
1887                                         dh = table[bx];
1888                                         ah = table[di];
1889                                         al = table[si];
1890                                         si--;
1891                                         XGI_WriteDAC(dl, ah, al, dh, pVBInfo);
1892                                 }
1893
1894                                 dl++;
1895                         }
1896
1897                         si += 5;
1898                 }
1899         }
1900 }
1901
1902 static void XGI_GetLVDSResInfo(unsigned short ModeNo,
1903                                unsigned short ModeIdIndex,
1904                                struct vb_device_info *pVBInfo)
1905 {
1906         unsigned short resindex, xres, yres, modeflag;
1907
1908         if (ModeNo <= 0x13)
1909                 /* si+St_ResInfo */
1910                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ResInfo;
1911         else
1912                 /* si+Ext_ResInfo */
1913                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
1914
1915         /* if (ModeNo > 0x13) */
1916         /*      modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag; */
1917         /* else */
1918         /*      modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag; */
1919
1920         if (ModeNo <= 0x13)
1921                 /* si+St_ResInfo */
1922                 resindex = pVBInfo->SModeIDTable[ModeIdIndex].St_ResInfo;
1923         else
1924                 /* si+Ext_ResInfo */
1925                 resindex = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
1926
1927         /* resindex = XGI_GetResInfo(ModeNo, ModeIdIndex, pVBInfo); */
1928
1929         if (ModeNo <= 0x13) {
1930                 xres = pVBInfo->StResInfo[resindex].HTotal;
1931                 yres = pVBInfo->StResInfo[resindex].VTotal;
1932         } else {
1933                 xres = pVBInfo->ModeResInfo[resindex].HTotal;
1934                 yres = pVBInfo->ModeResInfo[resindex].VTotal;
1935         }
1936         if (ModeNo > 0x13) {
1937                 if (modeflag & HalfDCLK)
1938                         xres = xres << 1;
1939
1940                 if (modeflag & DoubleScanMode)
1941                         yres = yres << 1;
1942         }
1943         /* if (modeflag & Charx8Dot) */
1944         /* { */
1945
1946         if (xres == 720)
1947                 xres = 640;
1948
1949         /* } */
1950         pVBInfo->VGAHDE = xres;
1951         pVBInfo->HDE = xres;
1952         pVBInfo->VGAVDE = yres;
1953         pVBInfo->VDE = yres;
1954 }
1955
1956 static void *XGI_GetLcdPtr(unsigned short BX, unsigned short ModeNo,
1957                 unsigned short ModeIdIndex,
1958                 unsigned short RefreshRateTableIndex,
1959                 struct vb_device_info *pVBInfo)
1960 {
1961         unsigned short i, tempdx, tempcx, tempbx, tempal, modeflag, table;
1962
1963         struct XGI330_LCDDataTablStruct *tempdi = NULL;
1964
1965         tempbx = BX;
1966
1967         if (ModeNo <= 0x13) {
1968                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
1969                 tempal = pVBInfo->SModeIDTable[ModeIdIndex].St_CRT2CRTC;
1970         } else {
1971                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
1972                 tempal = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
1973         }
1974
1975         tempal = tempal & 0x0f;
1976
1977         if (tempbx <= 1) { /* ExpLink */
1978                 if (ModeNo <= 0x13) {
1979                         /* find no Ext_CRT2CRTC2 */
1980                         tempal = pVBInfo->SModeIDTable[ModeIdIndex].St_CRT2CRTC;
1981                 } else {
1982                         tempal = pVBInfo->RefIndex[RefreshRateTableIndex].
1983                                         Ext_CRT2CRTC;
1984                 }
1985
1986                 if (pVBInfo->VBInfo & SetCRT2ToLCDA) {
1987                         if (ModeNo <= 0x13)
1988                                 tempal = pVBInfo->SModeIDTable[ModeIdIndex].
1989                                                 St_CRT2CRTC2;
1990                         else
1991                                 tempal = pVBInfo->RefIndex[
1992                                                 RefreshRateTableIndex].
1993                                                         Ext_CRT2CRTC2;
1994                 }
1995
1996                 if (tempbx & 0x01)
1997                         tempal = (tempal >> 4);
1998
1999                 tempal = (tempal & 0x0f);
2000         }
2001
2002         tempcx = LCDLenList[tempbx]; /* mov cl,byte ptr cs:LCDLenList[bx] */
2003
2004         if (pVBInfo->LCDInfo & EnableScalingLCD) { /* ScaleLCD */
2005                 if ((tempbx == 5) || (tempbx) == 7)
2006                         tempcx = LCDDesDataLen2;
2007                 else if ((tempbx == 3) || (tempbx == 8))
2008                         tempcx = LVDSDesDataLen2;
2009         }
2010         /* mov di, word ptr cs:LCDDataList[bx] */
2011         /* tempdi = pVideoMemory[LCDDataList + tempbx * 2] |
2012                     (pVideoMemory[LCDDataList + tempbx * 2 + 1] << 8); */
2013
2014         switch (tempbx) {
2015         case 0:
2016                 tempdi = XGI_EPLLCDCRT1Ptr_H;
2017                 break;
2018         case 1:
2019                 tempdi = XGI_EPLLCDCRT1Ptr_V;
2020                 break;
2021         case 2:
2022                 tempdi = XGI_EPLLCDDataPtr;
2023                 break;
2024         case 3:
2025                 tempdi = XGI_EPLLCDDesDataPtr;
2026                 break;
2027         case 4:
2028                 tempdi = XGI_LCDDataTable;
2029                 break;
2030         case 5:
2031                 tempdi = XGI_LCDDesDataTable;
2032                 break;
2033         case 6:
2034                 tempdi = XGI_EPLCHLCDRegPtr;
2035                 break;
2036         case 7:
2037         case 8:
2038         case 9:
2039                 tempdi = NULL;
2040                 break;
2041         default:
2042                 break;
2043         }
2044
2045         if (tempdi == NULL) /* OEMUtil */
2046                 return NULL;
2047
2048         table = tempbx;
2049         i = 0;
2050
2051         while (tempdi[i].PANELID != 0xff) {
2052                 tempdx = pVBInfo->LCDResInfo;
2053                 if (tempbx & 0x0080) { /* OEMUtil */
2054                         tempbx &= (~0x0080);
2055                         tempdx = pVBInfo->LCDTypeInfo;
2056                 }
2057
2058                 if (pVBInfo->LCDInfo & EnableScalingLCD)
2059                         tempdx &= (~PanelResInfo);
2060
2061                 if (tempdi[i].PANELID == tempdx) {
2062                         tempbx = tempdi[i].MASK;
2063                         tempdx = pVBInfo->LCDInfo;
2064
2065                         if (ModeNo <= 0x13) /* alan 09/10/2003 */
2066                                 tempdx |= SetLCDStdMode;
2067
2068                         if (modeflag & HalfDCLK)
2069                                 tempdx |= SetLCDLowResolution;
2070
2071                         tempbx &= tempdx;
2072                         if (tempbx == tempdi[i].CAP)
2073                                 break;
2074                 }
2075                 i++;
2076         }
2077
2078         if (table == 0) {
2079                 switch (tempdi[i].DATAPTR) {
2080                 case 0:
2081                         return &XGI_LVDSCRT11024x768_1_H[tempal];
2082                         break;
2083                 case 1:
2084                         return &XGI_LVDSCRT11024x768_2_H[tempal];
2085                         break;
2086                 case 2:
2087                         return &XGI_LVDSCRT11280x1024_1_H[tempal];
2088                         break;
2089                 case 3:
2090                         return &XGI_LVDSCRT11280x1024_2_H[tempal];
2091                         break;
2092                 case 4:
2093                         return &XGI_LVDSCRT11400x1050_1_H[tempal];
2094                         break;
2095                 case 5:
2096                         return &XGI_LVDSCRT11400x1050_2_H[tempal];
2097                         break;
2098                 case 6:
2099                         return &XGI_LVDSCRT11600x1200_1_H[tempal];
2100                         break;
2101                 case 7:
2102                         return &XGI_LVDSCRT11024x768_1_Hx75[tempal];
2103                         break;
2104                 case 8:
2105                         return &XGI_LVDSCRT11024x768_2_Hx75[tempal];
2106                         break;
2107                 case 9:
2108                         return &XGI_LVDSCRT11280x1024_1_Hx75[tempal];
2109                         break;
2110                 case 10:
2111                         return &XGI_LVDSCRT11280x1024_2_Hx75[tempal];
2112                         break;
2113                 default:
2114                         break;
2115                 }
2116         } else if (table == 1) {
2117                 switch (tempdi[i].DATAPTR) {
2118                 case 0:
2119                         return &XGI_LVDSCRT11024x768_1_V[tempal];
2120                         break;
2121                 case 1:
2122                         return &XGI_LVDSCRT11024x768_2_V[tempal];
2123                         break;
2124                 case 2:
2125                         return &XGI_LVDSCRT11280x1024_1_V[tempal];
2126                         break;
2127                 case 3:
2128                         return &XGI_LVDSCRT11280x1024_2_V[tempal];
2129                         break;
2130                 case 4:
2131                         return &XGI_LVDSCRT11400x1050_1_V[tempal];
2132                         break;
2133                 case 5:
2134                         return &XGI_LVDSCRT11400x1050_2_V[tempal];
2135                         break;
2136                 case 6:
2137                         return &XGI_LVDSCRT11600x1200_1_V[tempal];
2138                         break;
2139                 case 7:
2140                         return &XGI_LVDSCRT11024x768_1_Vx75[tempal];
2141                         break;
2142                 case 8:
2143                         return &XGI_LVDSCRT11024x768_2_Vx75[tempal];
2144                         break;
2145                 case 9:
2146                         return &XGI_LVDSCRT11280x1024_1_Vx75[tempal];
2147                         break;
2148                 case 10:
2149                         return &XGI_LVDSCRT11280x1024_2_Vx75[tempal];
2150                         break;
2151                 default:
2152                         break;
2153                 }
2154         } else if (table == 2) {
2155                 switch (tempdi[i].DATAPTR) {
2156                 case 0:
2157                         return &XGI_LVDS1024x768Data_1[tempal];
2158                         break;
2159                 case 1:
2160                         return &XGI_LVDS1024x768Data_2[tempal];
2161                         break;
2162                 case 2:
2163                         return &XGI_LVDS1280x1024Data_1[tempal];
2164                         break;
2165                 case 3:
2166                         return &XGI_LVDS1280x1024Data_2[tempal];
2167                         break;
2168                 case 4:
2169                         return &XGI_LVDS1400x1050Data_1[tempal];
2170                         break;
2171                 case 5:
2172                         return &XGI_LVDS1400x1050Data_2[tempal];
2173                         break;
2174                 case 6:
2175                         return &XGI_LVDS1600x1200Data_1[tempal];
2176                         break;
2177                 case 7:
2178                         return &XGI_LVDSNoScalingData[tempal];
2179                         break;
2180                 case 8:
2181                         return &XGI_LVDS1024x768Data_1x75[tempal];
2182                         break;
2183                 case 9:
2184                         return &XGI_LVDS1024x768Data_2x75[tempal];
2185                         break;
2186                 case 10:
2187                         return &XGI_LVDS1280x1024Data_1x75[tempal];
2188                         break;
2189                 case 11:
2190                         return &XGI_LVDS1280x1024Data_2x75[tempal];
2191                         break;
2192                 case 12:
2193                         return &XGI_LVDSNoScalingDatax75[tempal];
2194                         break;
2195                 default:
2196                         break;
2197                 }
2198         } else if (table == 3) {
2199                 switch (tempdi[i].DATAPTR) {
2200                 case 0:
2201                         return &XGI_LVDS1024x768Des_1[tempal];
2202                         break;
2203                 case 1:
2204                         return &XGI_LVDS1024x768Des_3[tempal];
2205                         break;
2206                 case 2:
2207                         return &XGI_LVDS1024x768Des_2[tempal];
2208                         break;
2209                 case 3:
2210                         return &XGI_LVDS1280x1024Des_1[tempal];
2211                         break;
2212                 case 4:
2213                         return &XGI_LVDS1280x1024Des_2[tempal];
2214                         break;
2215                 case 5:
2216                         return &XGI_LVDS1400x1050Des_1[tempal];
2217                         break;
2218                 case 6:
2219                         return &XGI_LVDS1400x1050Des_2[tempal];
2220                         break;
2221                 case 7:
2222                         return &XGI_LVDS1600x1200Des_1[tempal];
2223                         break;
2224                 case 8:
2225                         return &XGI_LVDSNoScalingDesData[tempal];
2226                         break;
2227                 case 9:
2228                         return &XGI_LVDS1024x768Des_1x75[tempal];
2229                         break;
2230                 case 10:
2231                         return &XGI_LVDS1024x768Des_3x75[tempal];
2232                         break;
2233                 case 11:
2234                         return &XGI_LVDS1024x768Des_2x75[tempal];
2235                         break;
2236                 case 12:
2237                         return &XGI_LVDS1280x1024Des_1x75[tempal];
2238                         break;
2239                 case 13:
2240                         return &XGI_LVDS1280x1024Des_2x75[tempal];
2241                         break;
2242                 case 14:
2243                         return &XGI_LVDSNoScalingDesDatax75[tempal];
2244                         break;
2245                 default:
2246                         break;
2247                 }
2248         } else if (table == 4) {
2249                 switch (tempdi[i].DATAPTR) {
2250                 case 0:
2251                         return &XGI_ExtLCD1024x768Data[tempal];
2252                         break;
2253                 case 1:
2254                         return &XGI_StLCD1024x768Data[tempal];
2255                         break;
2256                 case 2:
2257                         return &XGI_CetLCD1024x768Data[tempal];
2258                         break;
2259                 case 3:
2260                         return &XGI_ExtLCD1280x1024Data[tempal];
2261                         break;
2262                 case 4:
2263                         return &XGI_StLCD1280x1024Data[tempal];
2264                         break;
2265                 case 5:
2266                         return &XGI_CetLCD1280x1024Data[tempal];
2267                         break;
2268                 case 6:
2269                         return &XGI_ExtLCD1400x1050Data[tempal];
2270                         break;
2271                 case 7:
2272                         return &XGI_StLCD1400x1050Data[tempal];
2273                         break;
2274                 case 8:
2275                         return &XGI_CetLCD1400x1050Data[tempal];
2276                         break;
2277                 case 9:
2278                         return &XGI_ExtLCD1600x1200Data[tempal];
2279                         break;
2280                 case 10:
2281                         return &XGI_StLCD1600x1200Data[tempal];
2282                         break;
2283                 case 11:
2284                         return &XGI_NoScalingData[tempal];
2285                         break;
2286                 case 12:
2287                         return &XGI_ExtLCD1024x768x75Data[tempal];
2288                         break;
2289                 case 13:
2290                         return &XGI_ExtLCD1024x768x75Data[tempal];
2291                         break;
2292                 case 14:
2293                         return &XGI_CetLCD1024x768x75Data[tempal];
2294                         break;
2295                 case 15:
2296                         return &XGI_ExtLCD1280x1024x75Data[tempal];
2297                         break;
2298                 case 16:
2299                         return &XGI_StLCD1280x1024x75Data[tempal];
2300                         break;
2301                 case 17:
2302                         return &XGI_CetLCD1280x1024x75Data[tempal];
2303                         break;
2304                 case 18:
2305                         return &XGI_NoScalingDatax75[tempal];
2306                         break;
2307                 default:
2308                         break;
2309                 }
2310         } else if (table == 5) {
2311                 switch (tempdi[i].DATAPTR) {
2312                 case 0:
2313                         return &XGI_ExtLCDDes1024x768Data[tempal];
2314                         break;
2315                 case 1:
2316                         return &XGI_StLCDDes1024x768Data[tempal];
2317                         break;
2318                 case 2:
2319                         return &XGI_CetLCDDes1024x768Data[tempal];
2320                         break;
2321                 case 3:
2322                         if ((pVBInfo->VBType & VB_XGI301LV) ||
2323                                 (pVBInfo->VBType & VB_XGI302LV))
2324                                 return &XGI_ExtLCDDLDes1280x1024Data[tempal];
2325                         else
2326                                 return &XGI_ExtLCDDes1280x1024Data[tempal];
2327                         break;
2328                 case 4:
2329                         if ((pVBInfo->VBType & VB_XGI301LV) ||
2330                             (pVBInfo->VBType & VB_XGI302LV))
2331                                 return &XGI_StLCDDLDes1280x1024Data[tempal];
2332                         else
2333                                 return &XGI_StLCDDes1280x1024Data[tempal];
2334                         break;
2335                 case 5:
2336                         if ((pVBInfo->VBType & VB_XGI301LV) ||
2337                             (pVBInfo->VBType & VB_XGI302LV))
2338                                 return &XGI_CetLCDDLDes1280x1024Data[tempal];
2339                         else
2340                                 return &XGI_CetLCDDes1280x1024Data[tempal];
2341                         break;
2342                 case 6:
2343                         if ((pVBInfo->VBType & VB_XGI301LV) ||
2344                             (pVBInfo->VBType & VB_XGI302LV))
2345                                 return &XGI_ExtLCDDLDes1400x1050Data[tempal];
2346                         else
2347                                 return &XGI_ExtLCDDes1400x1050Data[tempal];
2348                         break;
2349                 case 7:
2350                         if ((pVBInfo->VBType & VB_XGI301LV) ||
2351                             (pVBInfo->VBType & VB_XGI302LV))
2352                                 return &XGI_StLCDDLDes1400x1050Data[tempal];
2353                         else
2354                                 return &XGI_StLCDDes1400x1050Data[tempal];
2355                         break;
2356                 case 8:
2357                         return &XGI_CetLCDDes1400x1050Data[tempal];
2358                         break;
2359                 case 9:
2360                         return &XGI_CetLCDDes1400x1050Data2[tempal];
2361                         break;
2362                 case 10:
2363                         if ((pVBInfo->VBType & VB_XGI301LV) ||
2364                             (pVBInfo->VBType & VB_XGI302LV))
2365                                 return &XGI_ExtLCDDLDes1600x1200Data[tempal];
2366                         else
2367                                 return &XGI_ExtLCDDes1600x1200Data[tempal];
2368                         break;
2369                 case 11:
2370                         if ((pVBInfo->VBType & VB_XGI301LV) ||
2371                             (pVBInfo->VBType & VB_XGI302LV))
2372                                 return &XGI_StLCDDLDes1600x1200Data[tempal];
2373                         else
2374                                 return &XGI_StLCDDes1600x1200Data[tempal];
2375                         break;
2376                 case 12:
2377                         return &XGI_NoScalingDesData[tempal];
2378                         break;
2379                 case 13:
2380                         return &XGI_ExtLCDDes1024x768x75Data[tempal];
2381                         break;
2382                 case 14:
2383                         return &XGI_StLCDDes1024x768x75Data[tempal];
2384                         break;
2385                 case 15:
2386                         return &XGI_CetLCDDes1024x768x75Data[tempal];
2387                         break;
2388                 case 16:
2389                         if ((pVBInfo->VBType & VB_XGI301LV) ||
2390                             (pVBInfo->VBType & VB_XGI302LV))
2391                                 return &XGI_ExtLCDDLDes1280x1024x75Data[tempal];
2392                         else
2393                                 return &XGI_ExtLCDDes1280x1024x75Data[tempal];
2394                         break;
2395                 case 17:
2396                         if ((pVBInfo->VBType & VB_XGI301LV) ||
2397                             (pVBInfo->VBType & VB_XGI302LV))
2398                                 return &XGI_StLCDDLDes1280x1024x75Data[tempal];
2399                         else
2400                                 return &XGI_StLCDDes1280x1024x75Data[tempal];
2401                         break;
2402                 case 18:
2403                         if ((pVBInfo->VBType & VB_XGI301LV) ||
2404                             (pVBInfo->VBType & VB_XGI302LV))
2405                                 return &XGI_CetLCDDLDes1280x1024x75Data[tempal];
2406                         else
2407                                 return &XGI_CetLCDDes1280x1024x75Data[tempal];
2408                         break;
2409                 case 19:
2410                         return &XGI_NoScalingDesDatax75[tempal];
2411                         break;
2412                 default:
2413                         break;
2414                 }
2415         } else if (table == 6) {
2416                 switch (tempdi[i].DATAPTR) {
2417                 case 0:
2418                         return &XGI_CH7017LV1024x768[tempal];
2419                         break;
2420                 case 1:
2421                         return &XGI_CH7017LV1400x1050[tempal];
2422                         break;
2423                 default:
2424                         break;
2425                 }
2426         }
2427         return NULL;
2428 }
2429
2430 static void *XGI_GetTVPtr(unsigned short BX, unsigned short ModeNo,
2431                 unsigned short ModeIdIndex,
2432                 unsigned short RefreshRateTableIndex,
2433                 struct vb_device_info *pVBInfo)
2434 {
2435         unsigned short i, tempdx, tempbx, tempal, modeflag, table;
2436         struct XGI330_TVDataTablStruct *tempdi = NULL;
2437
2438         tempbx = BX;
2439
2440         if (ModeNo <= 0x13) {
2441                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
2442                 tempal = pVBInfo->SModeIDTable[ModeIdIndex].St_CRT2CRTC;
2443         } else {
2444                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
2445                 tempal = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
2446         }
2447
2448         tempal = tempal & 0x3f;
2449         table = tempbx;
2450
2451         switch (tempbx) {
2452         case 0:
2453                 tempdi = NULL; /*EPLCHTVCRT1Ptr_H;*/
2454                 break;
2455         case 1:
2456                 tempdi = NULL; /*EPLCHTVCRT1Ptr_V;*/
2457                 break;
2458         case 2:
2459                 tempdi = XGI_EPLCHTVDataPtr;
2460                 break;
2461         case 3:
2462                 tempdi = NULL;
2463                 break;
2464         case 4:
2465                 tempdi = XGI_TVDataTable;
2466                 break;
2467         case 5:
2468                 tempdi = NULL;
2469                 break;
2470         case 6:
2471                 tempdi = XGI_EPLCHTVRegPtr;
2472                 break;
2473         default:
2474                 break;
2475         }
2476
2477         if (tempdi == NULL) /* OEMUtil */
2478                 return NULL;
2479
2480         tempdx = pVBInfo->TVInfo;
2481
2482         if (pVBInfo->VBInfo & SetInSlaveMode)
2483                 tempdx = tempdx | SetTVLockMode;
2484
2485         if (modeflag & HalfDCLK)
2486                 tempdx = tempdx | SetTVLowResolution;
2487
2488         i = 0;
2489
2490         while (tempdi[i].MASK != 0xffff) {
2491                 if ((tempdx & tempdi[i].MASK) == tempdi[i].CAP)
2492                         break;
2493                 i++;
2494         }
2495
2496         /* 07/05/22 */
2497         if (table == 0x00) {
2498         } else if (table == 0x01) {
2499         } else if (table == 0x04) {
2500                 switch (tempdi[i].DATAPTR) {
2501                 case 0:
2502                         return &XGI_ExtPALData[tempal];
2503                         break;
2504                 case 1:
2505                         return &XGI_ExtNTSCData[tempal];
2506                         break;
2507                 case 2:
2508                         return &XGI_StPALData[tempal];
2509                         break;
2510                 case 3:
2511                         return &XGI_StNTSCData[tempal];
2512                         break;
2513                 case 4:
2514                         return &XGI_ExtHiTVData[tempal];
2515                         break;
2516                 case 5:
2517                         return &XGI_St2HiTVData[tempal];
2518                         break;
2519                 case 6:
2520                         return &XGI_ExtYPbPr525iData[tempal];
2521                         break;
2522                 case 7:
2523                         return &XGI_ExtYPbPr525pData[tempal];
2524                         break;
2525                 case 8:
2526                         return &XGI_ExtYPbPr750pData[tempal];
2527                         break;
2528                 case 9:
2529                         return &XGI_StYPbPr525iData[tempal];
2530                         break;
2531                 case 10:
2532                         return &XGI_StYPbPr525pData[tempal];
2533                         break;
2534                 case 11:
2535                         return &XGI_StYPbPr750pData[tempal];
2536                         break;
2537                 case 12: /* avoid system hang */
2538                         return &XGI_ExtNTSCData[tempal];
2539                         break;
2540                 case 13:
2541                         return &XGI_St1HiTVData[tempal];
2542                         break;
2543                 default:
2544                         break;
2545                 }
2546         } else if (table == 0x02) {
2547                 switch (tempdi[i].DATAPTR) {
2548                 case 0:
2549                         return &XGI_CHTVUNTSCData[tempal];
2550                         break;
2551                 case 1:
2552                         return &XGI_CHTVONTSCData[tempal];
2553                         break;
2554                 case 2:
2555                         return &XGI_CHTVUPALData[tempal];
2556                         break;
2557                 case 3:
2558                         return &XGI_CHTVOPALData[tempal];
2559                         break;
2560                 default:
2561                         break;
2562                 }
2563         } else if (table == 0x06) {
2564         }
2565         return NULL;
2566 }
2567
2568 static void XGI_GetLVDSData(unsigned short ModeNo, unsigned short ModeIdIndex,
2569                 unsigned short RefreshRateTableIndex,
2570                 struct vb_device_info *pVBInfo)
2571 {
2572         unsigned short tempbx;
2573         struct XGI330_LVDSDataStruct *LCDPtr = NULL;
2574
2575         tempbx = 2;
2576
2577         if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
2578                 LCDPtr = (struct XGI330_LVDSDataStruct *) XGI_GetLcdPtr(tempbx,
2579                                 ModeNo, ModeIdIndex, RefreshRateTableIndex,
2580                                 pVBInfo);
2581                 pVBInfo->VGAHT = LCDPtr->VGAHT;
2582                 pVBInfo->VGAVT = LCDPtr->VGAVT;
2583                 pVBInfo->HT = LCDPtr->LCDHT;
2584                 pVBInfo->VT = LCDPtr->LCDVT;
2585         }
2586
2587         if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
2588                 if (!(pVBInfo->LCDInfo & (SetLCDtoNonExpanding
2589                                 | EnableScalingLCD))) {
2590                         if ((pVBInfo->LCDResInfo == Panel1024x768) ||
2591                             (pVBInfo->LCDResInfo == Panel1024x768x75)) {
2592                                 pVBInfo->HDE = 1024;
2593                                 pVBInfo->VDE = 768;
2594                         } else if ((pVBInfo->LCDResInfo == Panel1280x1024) ||
2595                                    (pVBInfo->LCDResInfo == Panel1280x1024x75)) {
2596                                 pVBInfo->HDE = 1280;
2597                                 pVBInfo->VDE = 1024;
2598                         } else if (pVBInfo->LCDResInfo == Panel1400x1050) {
2599                                 pVBInfo->HDE = 1400;
2600                                 pVBInfo->VDE = 1050;
2601                         } else {
2602                                 pVBInfo->HDE = 1600;
2603                                 pVBInfo->VDE = 1200;
2604                         }
2605                 }
2606         }
2607 }
2608
2609 static void XGI_ModCRT1Regs(unsigned short ModeNo, unsigned short ModeIdIndex,
2610                 unsigned short RefreshRateTableIndex,
2611                 struct xgi_hw_device_info *HwDeviceExtension,
2612                 struct vb_device_info *pVBInfo)
2613 {
2614         unsigned char index;
2615         unsigned short tempbx, i;
2616         struct XGI_LVDSCRT1HDataStruct *LCDPtr = NULL;
2617         struct XGI_LVDSCRT1VDataStruct *LCDPtr1 = NULL;
2618
2619         if (ModeNo <= 0x13)
2620                 index = pVBInfo->SModeIDTable[ModeIdIndex].St_CRT2CRTC;
2621         else
2622                 index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
2623
2624         index = index & IndexMask;
2625
2626         if ((pVBInfo->IF_DEF_ScaleLCD == 0) ||
2627             ((pVBInfo->IF_DEF_ScaleLCD == 1) &&
2628             (!(pVBInfo->LCDInfo & EnableScalingLCD)))) {
2629                 tempbx = 0;
2630
2631                 if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
2632                         LCDPtr = (struct XGI_LVDSCRT1HDataStruct *)
2633                                         XGI_GetLcdPtr(tempbx, ModeNo,
2634                                                       ModeIdIndex,
2635                                                       RefreshRateTableIndex,
2636                                                       pVBInfo);
2637
2638                         for (i = 0; i < 8; i++)
2639                                 pVBInfo->TimingH[0].data[i] = LCDPtr[0].Reg[i];
2640                 }
2641
2642                 XGI_SetCRT1Timing_H(pVBInfo, HwDeviceExtension);
2643
2644                 tempbx = 1;
2645
2646                 if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
2647                         LCDPtr1 = (struct XGI_LVDSCRT1VDataStruct *)
2648                                         XGI_GetLcdPtr(
2649                                                 tempbx,
2650                                                 ModeNo,
2651                                                 ModeIdIndex,
2652                                                 RefreshRateTableIndex,
2653                                                 pVBInfo);
2654                         for (i = 0; i < 7; i++)
2655                                 pVBInfo->TimingV[0].data[i] = LCDPtr1[0].Reg[i];
2656                 }
2657
2658                 XGI_SetCRT1Timing_V(ModeIdIndex, ModeNo, pVBInfo);
2659         }
2660 }
2661
2662 static unsigned short XGI_GetLCDCapPtr(struct vb_device_info *pVBInfo)
2663 {
2664         unsigned char tempal, tempah, tempbl, i;
2665
2666         tempah = xgifb_reg_get(pVBInfo->P3d4, 0x36);
2667         tempal = tempah & 0x0F;
2668         tempah = tempah & 0xF0;
2669         i = 0;
2670         tempbl = pVBInfo->LCDCapList[i].LCD_ID;
2671
2672         while (tempbl != 0xFF) {
2673                 if (tempbl & 0x80) { /* OEMUtil */
2674                         tempal = tempah;
2675                         tempbl = tempbl & ~(0x80);
2676                 }
2677
2678                 if (tempal == tempbl)
2679                         break;
2680
2681                 i++;
2682
2683                 tempbl = pVBInfo->LCDCapList[i].LCD_ID;
2684         }
2685
2686         return i;
2687 }
2688
2689 static unsigned short XGI_GetLCDCapPtr1(struct vb_device_info *pVBInfo)
2690 {
2691         unsigned short tempah, tempal, tempbl, i;
2692
2693         tempal = pVBInfo->LCDResInfo;
2694         tempah = pVBInfo->LCDTypeInfo;
2695
2696         i = 0;
2697         tempbl = pVBInfo->LCDCapList[i].LCD_ID;
2698
2699         while (tempbl != 0xFF) {
2700                 if ((tempbl & 0x80) && (tempbl != 0x80)) {
2701                         tempal = tempah;
2702                         tempbl &= ~0x80;
2703                 }
2704
2705                 if (tempal == tempbl)
2706                         break;
2707
2708                 i++;
2709                 tempbl = pVBInfo->LCDCapList[i].LCD_ID;
2710         }
2711
2712         if (tempbl == 0xFF) {
2713                 pVBInfo->LCDResInfo = Panel1024x768;
2714                 pVBInfo->LCDTypeInfo = 0;
2715                 i = 0;
2716         }
2717
2718         return i;
2719 }
2720
2721 static void XGI_GetLCDSync(unsigned short *HSyncWidth,
2722                            unsigned short *VSyncWidth,
2723                            struct vb_device_info *pVBInfo)
2724 {
2725         unsigned short Index;
2726
2727         Index = XGI_GetLCDCapPtr(pVBInfo);
2728         *HSyncWidth = pVBInfo->LCDCapList[Index].LCD_HSyncWidth;
2729         *VSyncWidth = pVBInfo->LCDCapList[Index].LCD_VSyncWidth;
2730
2731         return;
2732 }
2733
2734 static void XGI_SetLVDSRegs(unsigned short ModeNo, unsigned short ModeIdIndex,
2735                 unsigned short RefreshRateTableIndex,
2736                 struct vb_device_info *pVBInfo)
2737 {
2738         unsigned short tempbx, tempax, tempcx, tempdx, push1, push2, modeflag;
2739         unsigned long temp, temp1, temp2, temp3, push3;
2740         struct XGI330_LCDDataDesStruct *LCDPtr = NULL;
2741         struct XGI330_LCDDataDesStruct2 *LCDPtr1 = NULL;
2742
2743         if (ModeNo > 0x13)
2744                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
2745         else
2746                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
2747
2748         if (!(pVBInfo->SetFlag & Win9xDOSMode)) {
2749                 if (pVBInfo->IF_DEF_OEMUtil == 1) {
2750                         tempbx = 8;
2751                         LCDPtr = (struct XGI330_LCDDataDesStruct *)
2752                                 XGI_GetLcdPtr(tempbx,
2753                                               ModeNo,
2754                                               ModeIdIndex,
2755                                               RefreshRateTableIndex,
2756                                               pVBInfo);
2757                 }
2758
2759                 if ((pVBInfo->IF_DEF_OEMUtil == 0) ||
2760                     (LCDPtr == NULL)) {
2761                         tempbx = 3;
2762                         if (pVBInfo->LCDInfo & EnableScalingLCD)
2763                                 LCDPtr1 =
2764                                     (struct XGI330_LCDDataDesStruct2 *)
2765                                                 XGI_GetLcdPtr(
2766                                                   tempbx,
2767                                                   ModeNo,
2768                                                   ModeIdIndex,
2769                                                   RefreshRateTableIndex,
2770                                                   pVBInfo);
2771                         else
2772                                 LCDPtr =
2773                                     (struct XGI330_LCDDataDesStruct *)
2774                                                 XGI_GetLcdPtr(
2775                                                   tempbx,
2776                                                   ModeNo,
2777                                                   ModeIdIndex,
2778                                                   RefreshRateTableIndex,
2779                                                   pVBInfo);
2780                 }
2781
2782                 XGI_GetLCDSync(&tempax, &tempbx, pVBInfo);
2783                 push1 = tempbx;
2784                 push2 = tempax;
2785
2786                 /* GetLCDResInfo */
2787                 if ((pVBInfo->LCDResInfo == Panel1024x768) ||
2788                     (pVBInfo->LCDResInfo == Panel1024x768x75)) {
2789                         tempax = 1024;
2790                         tempbx = 768;
2791                 } else if ((pVBInfo->LCDResInfo == Panel1280x1024) ||
2792                            (pVBInfo->LCDResInfo == Panel1280x1024x75)) {
2793                         tempax = 1280;
2794                         tempbx = 1024;
2795                 } else if (pVBInfo->LCDResInfo == Panel1400x1050) {
2796                         tempax = 1400;
2797                         tempbx = 1050;
2798                 } else {
2799                         tempax = 1600;
2800                         tempbx = 1200;
2801                 }
2802
2803                 if (pVBInfo->LCDInfo & SetLCDtoNonExpanding) {
2804                         pVBInfo->HDE = tempax;
2805                         pVBInfo->VDE = tempbx;
2806                         pVBInfo->VGAHDE = tempax;
2807                         pVBInfo->VGAVDE = tempbx;
2808                 }
2809
2810                 if ((pVBInfo->IF_DEF_ScaleLCD == 1) &&
2811                     (pVBInfo->LCDInfo & EnableScalingLCD)) {
2812                         tempax = pVBInfo->HDE;
2813                         tempbx = pVBInfo->VDE;
2814                 }
2815
2816                 tempax = pVBInfo->HT;
2817
2818                 if (pVBInfo->LCDInfo & EnableScalingLCD)
2819                         tempbx = LCDPtr1->LCDHDES;
2820                 else
2821                         tempbx = LCDPtr->LCDHDES;
2822
2823                 tempcx = pVBInfo->HDE;
2824                 tempbx = tempbx & 0x0fff;
2825                 tempcx += tempbx;
2826
2827                 if (tempcx >= tempax)
2828                         tempcx -= tempax;
2829
2830                 xgifb_reg_set(pVBInfo->Part1Port, 0x1A, tempbx & 0x07);
2831
2832                 tempcx = tempcx >> 3;
2833                 tempbx = tempbx >> 3;
2834
2835                 xgifb_reg_set(pVBInfo->Part1Port, 0x16,
2836                                 (unsigned short) (tempbx & 0xff));
2837                 xgifb_reg_set(pVBInfo->Part1Port, 0x17,
2838                                 (unsigned short) (tempcx & 0xff));
2839
2840                 tempax = pVBInfo->HT;
2841
2842                 if (pVBInfo->LCDInfo & EnableScalingLCD)
2843                         tempbx = LCDPtr1->LCDHRS;
2844                 else
2845                         tempbx = LCDPtr->LCDHRS;
2846
2847                 tempcx = push2;
2848
2849                 if (pVBInfo->LCDInfo & EnableScalingLCD)
2850                         tempcx = LCDPtr1->LCDHSync;
2851
2852                 tempcx += tempbx;
2853
2854                 if (tempcx >= tempax)
2855                         tempcx -= tempax;
2856
2857                 tempax = tempbx & 0x07;
2858                 tempax = tempax >> 5;
2859                 tempcx = tempcx >> 3;
2860                 tempbx = tempbx >> 3;
2861
2862                 tempcx &= 0x1f;
2863                 tempax |= tempcx;
2864
2865                 xgifb_reg_set(pVBInfo->Part1Port, 0x15, tempax);
2866                 xgifb_reg_set(pVBInfo->Part1Port, 0x14,
2867                                 (unsigned short) (tempbx & 0xff));
2868
2869                 tempax = pVBInfo->VT;
2870                 if (pVBInfo->LCDInfo & EnableScalingLCD)
2871                         tempbx = LCDPtr1->LCDVDES;
2872                 else
2873                         tempbx = LCDPtr->LCDVDES;
2874                 tempcx = pVBInfo->VDE;
2875
2876                 tempbx = tempbx & 0x0fff;
2877                 tempcx += tempbx;
2878                 if (tempcx >= tempax)
2879                         tempcx -= tempax;
2880
2881                 xgifb_reg_set(pVBInfo->Part1Port, 0x1b,
2882                                 (unsigned short) (tempbx & 0xff));
2883                 xgifb_reg_set(pVBInfo->Part1Port, 0x1c,
2884                                 (unsigned short) (tempcx & 0xff));
2885
2886                 tempbx = (tempbx >> 8) & 0x07;
2887                 tempcx = (tempcx >> 8) & 0x07;
2888
2889                 xgifb_reg_set(pVBInfo->Part1Port, 0x1d,
2890                                 (unsigned short) ((tempcx << 3)
2891                                                 | tempbx));
2892
2893                 tempax = pVBInfo->VT;
2894                 if (pVBInfo->LCDInfo & EnableScalingLCD)
2895                         tempbx = LCDPtr1->LCDVRS;
2896                 else
2897                         tempbx = LCDPtr->LCDVRS;
2898
2899                 /* tempbx = tempbx >> 4; */
2900                 tempcx = push1;
2901
2902                 if (pVBInfo->LCDInfo & EnableScalingLCD)
2903                         tempcx = LCDPtr1->LCDVSync;
2904
2905                 tempcx += tempbx;
2906                 if (tempcx >= tempax)
2907                         tempcx -= tempax;
2908
2909                 xgifb_reg_set(pVBInfo->Part1Port, 0x18,
2910                                 (unsigned short) (tempbx & 0xff));
2911                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x19, ~0x0f,
2912                                 (unsigned short) (tempcx & 0x0f));
2913
2914                 tempax = ((tempbx >> 8) & 0x07) << 3;
2915
2916                 tempbx = pVBInfo->VGAVDE;
2917                 if (tempbx != pVBInfo->VDE)
2918                         tempax |= 0x40;
2919
2920                 if (pVBInfo->LCDInfo & EnableLVDSDDA)
2921                         tempax |= 0x40;
2922
2923                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x1a, 0x07,
2924                                         tempax);
2925
2926                 tempcx = pVBInfo->VGAVT;
2927                 tempbx = pVBInfo->VDE;
2928                 tempax = pVBInfo->VGAVDE;
2929                 tempcx -= tempax;
2930
2931                 temp = tempax; /* 0430 ylshieh */
2932                 temp1 = (temp << 18) / tempbx;
2933
2934                 tempdx = (unsigned short) ((temp << 18) % tempbx);
2935
2936                 if (tempdx != 0)
2937                         temp1 += 1;
2938
2939                 temp2 = temp1;
2940                 push3 = temp2;
2941
2942                 xgifb_reg_set(pVBInfo->Part1Port, 0x37,
2943                                 (unsigned short) (temp2 & 0xff));
2944                 xgifb_reg_set(pVBInfo->Part1Port, 0x36,
2945                                 (unsigned short) ((temp2 >> 8) & 0xff));
2946
2947                 tempbx = (unsigned short) (temp2 >> 16);
2948                 tempax = tempbx & 0x03;
2949
2950                 tempbx = pVBInfo->VGAVDE;
2951                 if (tempbx == pVBInfo->VDE)
2952                         tempax |= 0x04;
2953
2954                 xgifb_reg_set(pVBInfo->Part1Port, 0x35, tempax);
2955
2956                 if (pVBInfo->VBType & VB_XGI301C) {
2957                         temp2 = push3;
2958                         xgifb_reg_set(pVBInfo->Part4Port,
2959                                       0x3c,
2960                                       (unsigned short) (temp2 & 0xff));
2961                         xgifb_reg_set(pVBInfo->Part4Port,
2962                                       0x3b,
2963                                       (unsigned short) ((temp2 >> 8) &
2964                                       0xff));
2965                         tempbx = (unsigned short) (temp2 >> 16);
2966                         xgifb_reg_and_or(pVBInfo->Part4Port, 0x3a,
2967                                         ~0xc0,
2968                                         (unsigned short) ((tempbx &
2969                                                            0xff) << 6));
2970
2971                         tempcx = pVBInfo->VGAVDE;
2972                         if (tempcx == pVBInfo->VDE)
2973                                 xgifb_reg_and_or(pVBInfo->Part4Port,
2974                                                 0x30, ~0x0c, 0x00);
2975                         else
2976                                 xgifb_reg_and_or(pVBInfo->Part4Port,
2977                                                 0x30, ~0x0c, 0x08);
2978                 }
2979
2980                 tempcx = pVBInfo->VGAHDE;
2981                 tempbx = pVBInfo->HDE;
2982
2983                 temp1 = tempcx << 16;
2984
2985                 tempax = (unsigned short) (temp1 / tempbx);
2986
2987                 if ((tempbx & 0xffff) == (tempcx & 0xffff))
2988                         tempax = 65535;
2989
2990                 temp3 = tempax;
2991                 temp1 = pVBInfo->VGAHDE << 16;
2992
2993                 temp1 /= temp3;
2994                 temp3 = temp3 << 16;
2995                 temp1 -= 1;
2996
2997                 temp3 = (temp3 & 0xffff0000) + (temp1 & 0xffff);
2998
2999                 tempax = (unsigned short) (temp3 & 0xff);
3000                 xgifb_reg_set(pVBInfo->Part1Port, 0x1f, tempax);
3001
3002                 temp1 = pVBInfo->VGAVDE << 18;
3003                 temp1 = temp1 / push3;
3004                 tempbx = (unsigned short) (temp1 & 0xffff);
3005
3006                 if (pVBInfo->LCDResInfo == Panel1024x768)
3007                         tempbx -= 1;
3008
3009                 tempax = ((tempbx >> 8) & 0xff) << 3;
3010                 tempax |= (unsigned short) ((temp3 >> 8) & 0x07);
3011                 xgifb_reg_set(pVBInfo->Part1Port, 0x20,
3012                                 (unsigned short) (tempax & 0xff));
3013                 xgifb_reg_set(pVBInfo->Part1Port, 0x21,
3014                                 (unsigned short) (tempbx & 0xff));
3015
3016                 temp3 = temp3 >> 16;
3017
3018                 if (modeflag & HalfDCLK)
3019                         temp3 = temp3 >> 1;
3020
3021                 xgifb_reg_set(pVBInfo->Part1Port, 0x22,
3022                                 (unsigned short) ((temp3 >> 8) & 0xff));
3023                 xgifb_reg_set(pVBInfo->Part1Port, 0x23,
3024                                 (unsigned short) (temp3 & 0xff));
3025         }
3026 }
3027
3028 /* --------------------------------------------------------------------- */
3029 /* Function : XGI_GETLCDVCLKPtr */
3030 /* Input : */
3031 /* Output : al -> VCLK Index */
3032 /* Description : */
3033 /* --------------------------------------------------------------------- */
3034 static void XGI_GetLCDVCLKPtr(unsigned char *di_0, unsigned char *di_1,
3035                 struct vb_device_info *pVBInfo)
3036 {
3037         unsigned short index;
3038
3039         if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
3040                 if (pVBInfo->IF_DEF_ScaleLCD == 1) {
3041                         if (pVBInfo->LCDInfo & EnableScalingLCD)
3042                                 return;
3043                 }
3044
3045                 /* index = XGI_GetLCDCapPtr(pVBInfo); */
3046                 index = XGI_GetLCDCapPtr1(pVBInfo);
3047
3048                 if (pVBInfo->VBInfo & SetCRT2ToLCD) { /* LCDB */
3049                         *di_0 = pVBInfo->LCDCapList[index].LCUCHAR_VCLKData1;
3050                         *di_1 = pVBInfo->LCDCapList[index].LCUCHAR_VCLKData2;
3051                 } else { /* LCDA */
3052                         *di_0 = pVBInfo->LCDCapList[index].LCDA_VCLKData1;
3053                         *di_1 = pVBInfo->LCDCapList[index].LCDA_VCLKData2;
3054                 }
3055         }
3056         return;
3057 }
3058
3059 static unsigned char XGI_GetVCLKPtr(unsigned short RefreshRateTableIndex,
3060                 unsigned short ModeNo, unsigned short ModeIdIndex,
3061                 struct vb_device_info *pVBInfo)
3062 {
3063
3064         unsigned short index, modeflag;
3065         unsigned char tempal;
3066
3067         if (ModeNo <= 0x13)
3068                 /* si+St_ResInfo */
3069                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
3070         else
3071                 /* si+Ext_ResInfo */
3072                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
3073
3074         if ((pVBInfo->SetFlag & ProgrammingCRT2) &&
3075             (!(pVBInfo->LCDInfo & EnableScalingLCD))) { /* {LCDA/LCDB} */
3076                 index = XGI_GetLCDCapPtr(pVBInfo);
3077                 tempal = pVBInfo->LCDCapList[index].LCD_VCLK;
3078
3079                 if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA))
3080                         return tempal;
3081
3082                 /* {TV} */
3083                 if (pVBInfo->VBType &
3084                     (VB_XGI301B |
3085                      VB_XGI302B |
3086                      VB_XGI301LV |
3087                      VB_XGI302LV |
3088                      VB_XGI301C)) {
3089                         if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {
3090                                 tempal = HiTVVCLKDIV2;
3091                                 if (!(pVBInfo->TVInfo & RPLLDIV2XO))
3092                                         tempal = HiTVVCLK;
3093                                 if (pVBInfo->TVInfo & TVSimuMode) {
3094                                         tempal = HiTVSimuVCLK;
3095                                         if (!(modeflag & Charx8Dot))
3096                                                 tempal = HiTVTextVCLK;
3097
3098                                 }
3099                                 return tempal;
3100                         }
3101
3102                         if (pVBInfo->TVInfo & SetYPbPrMode750p) {
3103                                 tempal = YPbPr750pVCLK;
3104                                 return tempal;
3105                         }
3106
3107                         if (pVBInfo->TVInfo & SetYPbPrMode525p) {
3108                                 tempal = YPbPr525pVCLK;
3109                                 return tempal;
3110                         }
3111
3112                         tempal = NTSC1024VCLK;
3113
3114                         if (!(pVBInfo->TVInfo & NTSC1024x768)) {
3115                                 tempal = TVVCLKDIV2;
3116                                 if (!(pVBInfo->TVInfo & RPLLDIV2XO))
3117                                         tempal = TVVCLK;
3118                         }
3119
3120                         if (pVBInfo->VBInfo & SetCRT2ToTV)
3121                                 return tempal;
3122                 }
3123         } /* {End of VB} */
3124
3125         tempal = (unsigned char) inb((pVBInfo->P3ca + 0x02));
3126         tempal = tempal >> 2;
3127         tempal &= 0x03;
3128
3129         /* for Dot8 Scaling LCD */
3130         if ((pVBInfo->LCDInfo & EnableScalingLCD) && (modeflag & Charx8Dot))
3131                 tempal = tempal ^ tempal; /* ; set to VCLK25MHz always */
3132
3133         if (ModeNo <= 0x13)
3134                 return tempal;
3135
3136         tempal = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
3137         return tempal;
3138 }
3139
3140 static void XGI_GetVCLKLen(unsigned char tempal, unsigned char *di_0,
3141                 unsigned char *di_1, struct vb_device_info *pVBInfo)
3142 {
3143         if (pVBInfo->VBType & (VB_XGI301 | VB_XGI301B | VB_XGI302B
3144                         | VB_XGI301LV | VB_XGI302LV | VB_XGI301C)) {
3145                 if ((!(pVBInfo->VBInfo & SetCRT2ToLCDA)) && (pVBInfo->SetFlag
3146                                 & ProgrammingCRT2)) {
3147                         *di_0 = (unsigned char) XGI_VBVCLKData[tempal].SR2B;
3148                         *di_1 = XGI_VBVCLKData[tempal].SR2C;
3149                 }
3150         } else {
3151                 *di_0 = XGI_VCLKData[tempal].SR2B;
3152                 *di_1 = XGI_VCLKData[tempal].SR2C;
3153         }
3154 }
3155
3156 static void XGI_SetCRT2ECLK(unsigned short ModeNo, unsigned short ModeIdIndex,
3157                 unsigned short RefreshRateTableIndex,
3158                 struct vb_device_info *pVBInfo)
3159 {
3160         unsigned char di_0, di_1, tempal;
3161         int i;
3162
3163         tempal = XGI_GetVCLKPtr(RefreshRateTableIndex, ModeNo, ModeIdIndex,
3164                         pVBInfo);
3165         XGI_GetVCLKLen(tempal, &di_0, &di_1, pVBInfo);
3166         XGI_GetLCDVCLKPtr(&di_0, &di_1, pVBInfo);
3167
3168         for (i = 0; i < 4; i++) {
3169                 xgifb_reg_and_or(pVBInfo->P3d4, 0x31, ~0x30,
3170                                 (unsigned short) (0x10 * i));
3171                 if ((!(pVBInfo->VBInfo & SetCRT2ToLCDA))
3172                                 && (!(pVBInfo->VBInfo & SetInSlaveMode))) {
3173                         xgifb_reg_set(pVBInfo->P3c4, 0x2e, di_0);
3174                         xgifb_reg_set(pVBInfo->P3c4, 0x2f, di_1);
3175                 } else {
3176                         xgifb_reg_set(pVBInfo->P3c4, 0x2b, di_0);
3177                         xgifb_reg_set(pVBInfo->P3c4, 0x2c, di_1);
3178                 }
3179         }
3180 }
3181
3182 static void XGI_UpdateModeInfo(struct xgi_hw_device_info *HwDeviceExtension,
3183                 struct vb_device_info *pVBInfo)
3184 {
3185         unsigned short tempcl, tempch, temp, tempbl, tempax;
3186
3187         if (pVBInfo->VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV
3188                         | VB_XGI302LV | VB_XGI301C)) {
3189                 tempcl = 0;
3190                 tempch = 0;
3191                 temp = xgifb_reg_get(pVBInfo->P3c4, 0x01);
3192
3193                 if (!(temp & 0x20)) {
3194                         temp = xgifb_reg_get(pVBInfo->P3d4, 0x17);
3195                         if (temp & 0x80) {
3196                                 temp = xgifb_reg_get(pVBInfo->P3d4, 0x53);
3197                                 if (!(temp & 0x40))
3198                                         tempcl |= ActiveCRT1;
3199                         }
3200                 }
3201
3202                 temp = xgifb_reg_get(pVBInfo->Part1Port, 0x2e);
3203                 temp &= 0x0f;
3204
3205                 if (!(temp == 0x08)) {
3206                         /* Check ChannelA by Part1_13 [2003/10/03] */
3207                         tempax = xgifb_reg_get(pVBInfo->Part1Port, 0x13);
3208                         if (tempax & 0x04)
3209                                 tempcl = tempcl | ActiveLCD;
3210
3211                         temp &= 0x05;
3212
3213                         if (!(tempcl & ActiveLCD))
3214                                 if (temp == 0x01)
3215                                         tempcl |= ActiveCRT2;
3216
3217                         if (temp == 0x04)
3218                                 tempcl |= ActiveLCD;
3219
3220                         if (temp == 0x05) {
3221                                 temp = xgifb_reg_get(pVBInfo->Part2Port, 0x00);
3222
3223                                 if (!(temp & 0x08))
3224                                         tempch |= ActiveAVideo;
3225
3226                                 if (!(temp & 0x04))
3227                                         tempch |= ActiveSVideo;
3228
3229                                 if (temp & 0x02)
3230                                         tempch |= ActiveSCART;
3231
3232                                 if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {
3233                                         if (temp & 0x01)
3234                                                 tempch |= ActiveHiTV;
3235                                 }
3236
3237                                 if (pVBInfo->VBInfo & SetCRT2ToYPbPr) {
3238                                         temp = xgifb_reg_get(
3239                                                         pVBInfo->Part2Port,
3240                                                         0x4d);
3241
3242                                         if (temp & 0x10)
3243                                                 tempch |= ActiveYPbPr;
3244                                 }
3245
3246                                 if (tempch != 0)
3247                                         tempcl |= ActiveTV;
3248                         }
3249                 }
3250
3251                 temp = xgifb_reg_get(pVBInfo->P3d4, 0x3d);
3252                 if (tempcl & ActiveLCD) {
3253                         if ((pVBInfo->SetFlag & ReserveTVOption)) {
3254                                 if (temp & ActiveTV)
3255                                         tempcl |= ActiveTV;
3256                         }
3257                 }
3258                 temp = tempcl;
3259                 tempbl = ~ModeSwitchStatus;
3260                 xgifb_reg_and_or(pVBInfo->P3d4, 0x3d, tempbl, temp);
3261
3262                 if (!(pVBInfo->SetFlag & ReserveTVOption))
3263                         xgifb_reg_set(pVBInfo->P3d4, 0x3e, tempch);
3264         } else {
3265                 return;
3266         }
3267 }
3268
3269 void XGI_GetVGAType(struct xgi_hw_device_info *HwDeviceExtension,
3270                 struct vb_device_info *pVBInfo)
3271 {
3272         /*
3273         if ( HwDeviceExtension->jChipType >= XG20 ) {
3274                 pVBInfo->Set_VGAType = XG20;
3275         } else {
3276                 pVBInfo->Set_VGAType = VGA_XGI340;
3277         }
3278         */
3279         pVBInfo->Set_VGAType = HwDeviceExtension->jChipType;
3280 }
3281
3282 void XGI_GetVBType(struct vb_device_info *pVBInfo)
3283 {
3284         unsigned short flag, tempbx, tempah;
3285
3286         if (pVBInfo->IF_DEF_LVDS == 0) {
3287                 tempbx = VB_XGI302B;
3288                 flag = xgifb_reg_get(pVBInfo->Part4Port, 0x00);
3289                 if (flag != 0x02) {
3290                         tempbx = VB_XGI301;
3291                         flag = xgifb_reg_get(pVBInfo->Part4Port, 0x01);
3292                         if (flag >= 0xB0) {
3293                                 tempbx = VB_XGI301B;
3294                                 if (flag >= 0xC0) {
3295                                         tempbx = VB_XGI301C;
3296                                         if (flag >= 0xD0) {
3297                                                 tempbx = VB_XGI301LV;
3298                                                 if (flag >= 0xE0) {
3299                                                         tempbx = VB_XGI302LV;
3300                                                         tempah = xgifb_reg_get(
3301                                                             pVBInfo->Part4Port,
3302                                                             0x39);
3303                                                         if (tempah != 0xFF)
3304                                                                 tempbx =
3305                                                                     VB_XGI301C;
3306                                                 }
3307                                         }
3308                                 }
3309
3310                                 if (tempbx & (VB_XGI301B | VB_XGI302B)) {
3311                                         flag = xgifb_reg_get(
3312                                                         pVBInfo->Part4Port,
3313                                                         0x23);
3314
3315                                         if (!(flag & 0x02))
3316                                                 tempbx = tempbx | VB_NoLCD;
3317                                 }
3318                         }
3319                 }
3320                 pVBInfo->VBType = tempbx;
3321         }
3322 }
3323
3324 void XGI_GetVBInfo(unsigned short ModeNo, unsigned short ModeIdIndex,
3325                 struct xgi_hw_device_info *HwDeviceExtension,
3326                 struct vb_device_info *pVBInfo)
3327 {
3328         unsigned short tempax, push, tempbx, temp, modeflag;
3329
3330         if (ModeNo <= 0x13)
3331                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
3332         else
3333                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
3334
3335         pVBInfo->SetFlag = 0;
3336         pVBInfo->ModeType = modeflag & ModeInfoFlag;
3337         tempbx = 0;
3338
3339         if (pVBInfo->VBType & 0xFFFF) {
3340                 /* Check Display Device */
3341                 temp = xgifb_reg_get(pVBInfo->P3d4, 0x30);
3342                 tempbx = tempbx | temp;
3343                 temp = xgifb_reg_get(pVBInfo->P3d4, 0x31);
3344                 push = temp;
3345                 push = push << 8;
3346                 tempax = temp << 8;
3347                 tempbx = tempbx | tempax;
3348                 temp = (SetCRT2ToDualEdge | SetCRT2ToYPbPr | SetCRT2ToLCDA
3349                                 | SetInSlaveMode | DisableCRT2Display);
3350                 temp = 0xFFFF ^ temp;
3351                 tempbx &= temp;
3352
3353                 temp = xgifb_reg_get(pVBInfo->P3d4, 0x38);
3354
3355                 if (pVBInfo->IF_DEF_LCDA == 1) {
3356
3357                         if ((pVBInfo->Set_VGAType >= XG20)
3358                                         || (pVBInfo->Set_VGAType >= XG40)) {
3359                                 if (pVBInfo->IF_DEF_LVDS == 0) {
3360                                         /* if ((pVBInfo->VBType & VB_XGI302B)
3361                                             || (pVBInfo->VBType & VB_XGI301LV)
3362                                             || (pVBInfo->VBType & VB_XGI302LV)
3363                                             || (pVBInfo->VBType & VB_XGI301C))
3364                                         */
3365                                         if (pVBInfo->VBType &
3366                                             (VB_XGI302B |
3367                                              VB_XGI301LV |
3368                                              VB_XGI302LV |
3369                                              VB_XGI301C)) {
3370                                                 if (temp & EnableDualEdge) {
3371                                                         tempbx |=
3372                                                             SetCRT2ToDualEdge;
3373
3374                                                         if (temp & SetToLCDA)
3375                                                                 tempbx |=
3376                                                                   SetCRT2ToLCDA;
3377                                                 }
3378                                         }
3379                                 }
3380                         }
3381                 }
3382
3383                 if (pVBInfo->IF_DEF_YPbPr == 1) {
3384                         /* [Billy] 07/05/04 */
3385                         if (((pVBInfo->IF_DEF_LVDS == 0) &&
3386                             ((pVBInfo->VBType & VB_XGI301LV) ||
3387                             (pVBInfo->VBType & VB_XGI302LV) ||
3388                             (pVBInfo->VBType & VB_XGI301C)))) {
3389                                 if (temp & SetYPbPr) { /* temp = CR38 */
3390                                         if (pVBInfo->IF_DEF_HiVision == 1) {
3391                                                 /* shampoo add for new
3392                                                  * scratch */
3393                                                 temp = xgifb_reg_get(
3394                                                                 pVBInfo->P3d4,
3395                                                                 0x35);
3396                                                 temp &= YPbPrMode;
3397                                                 tempbx |= SetCRT2ToHiVisionTV;
3398
3399                                                 if (temp != YPbPrMode1080i) {
3400                                                         tempbx &=
3401                                                          (~SetCRT2ToHiVisionTV);
3402                                                         tempbx |=
3403                                                          SetCRT2ToYPbPr;
3404                                                 }
3405                                         }
3406
3407                                         /* tempbx |= SetCRT2ToYPbPr; */
3408                                 }
3409                         }
3410                 }
3411
3412                 tempax = push; /* restore CR31 */
3413
3414                 if (pVBInfo->IF_DEF_LVDS == 0) {
3415                         if (pVBInfo->IF_DEF_YPbPr == 1) {
3416                                 if (pVBInfo->IF_DEF_HiVision == 1)
3417                                         temp = 0x09FC;
3418                                 else
3419                                         temp = 0x097C;
3420                         } else {
3421                                 if (pVBInfo->IF_DEF_HiVision == 1)
3422                                         temp = 0x01FC;
3423                                 else
3424                                         temp = 0x017C;
3425                         }
3426                 } else { /* 3nd party chip */
3427                         temp = SetCRT2ToLCD;
3428                 }
3429
3430                 if (!(tempbx & temp)) {
3431                         tempax |= DisableCRT2Display;
3432                         tempbx = 0;
3433                 }
3434
3435                 if (pVBInfo->IF_DEF_LCDA == 1) { /* Select Display Device */
3436                         if (!(pVBInfo->VBType & VB_NoLCD)) {
3437                                 if (tempbx & SetCRT2ToLCDA) {
3438                                         if (tempbx & SetSimuScanMode)
3439                                                 tempbx &= (~(SetCRT2ToLCD |
3440                                                            SetCRT2ToRAMDAC |
3441                                                            SwitchToCRT2));
3442                                         else
3443                                                 tempbx &= (~(SetCRT2ToLCD |
3444                                                              SetCRT2ToRAMDAC |
3445                                                              SetCRT2ToTV |
3446                                                              SwitchToCRT2));
3447                                 }
3448                         }
3449                 }
3450
3451                 /* shampoo add */
3452                 /* for driver abnormal */
3453                 if (!(tempbx & (SwitchToCRT2 | SetSimuScanMode))) {
3454                         if (pVBInfo->IF_DEF_CRT2Monitor == 1) {
3455                                 if (tempbx & SetCRT2ToRAMDAC) {
3456                                         tempbx &= (0xFF00 |
3457                                                    SetCRT2ToRAMDAC |
3458                                                    SwitchToCRT2 |
3459                                                    SetSimuScanMode);
3460                                         tempbx &= (0x00FF | (~SetCRT2ToYPbPr));
3461                                 }
3462                         } else {
3463                                 tempbx &= (~(SetCRT2ToRAMDAC |
3464                                            SetCRT2ToLCD |
3465                                            SetCRT2ToTV));
3466                         }
3467                 }
3468
3469                 if (!(pVBInfo->VBType & VB_NoLCD)) {
3470                         if (tempbx & SetCRT2ToLCD) {
3471                                 tempbx &= (0xFF00 |
3472                                            SetCRT2ToLCD |
3473                                            SwitchToCRT2 |
3474                                            SetSimuScanMode);
3475                                 tempbx &= (0x00FF | (~SetCRT2ToYPbPr));
3476                         }
3477                 }
3478
3479                 if (tempbx & SetCRT2ToSCART) {
3480                         tempbx &= (0xFF00 |
3481                                    SetCRT2ToSCART |
3482                                    SwitchToCRT2 |
3483                                    SetSimuScanMode);
3484                         tempbx &= (0x00FF | (~SetCRT2ToYPbPr));
3485                 }
3486
3487                 if (pVBInfo->IF_DEF_YPbPr == 1) {
3488                         if (tempbx & SetCRT2ToYPbPr)
3489                                 tempbx &= (0xFF00 |
3490                                            SwitchToCRT2 |
3491                                            SetSimuScanMode);
3492                 }
3493
3494                 if (pVBInfo->IF_DEF_HiVision == 1) {
3495                         if (tempbx & SetCRT2ToHiVisionTV)
3496                                 tempbx &= (0xFF00 |
3497                                            SetCRT2ToHiVisionTV |
3498                                            SwitchToCRT2 |
3499                                            SetSimuScanMode);
3500                 }
3501
3502                 if (tempax & DisableCRT2Display) { /* Set Display Device Info */
3503                         if (!(tempbx & (SwitchToCRT2 | SetSimuScanMode)))
3504                                 tempbx = DisableCRT2Display;
3505                 }
3506
3507                 if (!(tempbx & DisableCRT2Display)) {
3508                         if ((!(tempbx & DriverMode)) ||
3509                             (!(modeflag & CRT2Mode))) {
3510                                 if (pVBInfo->IF_DEF_LCDA == 1) {
3511                                         if (!(tempbx & SetCRT2ToLCDA))
3512                                                 tempbx |= (SetInSlaveMode |
3513                                                            SetSimuScanMode);
3514                                 }
3515
3516                                 if (pVBInfo->IF_DEF_VideoCapture == 1) {
3517                                         if (((HwDeviceExtension->jChipType ==
3518                                               XG40) &&
3519                                              (pVBInfo->Set_VGAType == XG40)) ||
3520                                             ((HwDeviceExtension->jChipType ==
3521                                               XG41) &&
3522                                              (pVBInfo->Set_VGAType == XG41)) ||
3523                                             ((HwDeviceExtension->jChipType ==
3524                                               XG42) &&
3525                                              (pVBInfo->Set_VGAType == XG42)) ||
3526                                             ((HwDeviceExtension->jChipType ==
3527                                               XG45) &&
3528                                              (pVBInfo->Set_VGAType == XG45))) {
3529                                                 if (ModeNo <= 13) {
3530                                                         if (!(tempbx &
3531                                                              SetCRT2ToRAMDAC)) {
3532                                                                 /*CRT2 not need
3533                                                                  * to support*/
3534                                                                 tempbx &=
3535                                                                   (0x00FF |
3536                                                                   (~SetInSlaveMode));
3537                                                                 pVBInfo->SetFlag
3538                                                                                 |= EnableVCMode;
3539                                                         }
3540                                                 }
3541                                         }
3542                                 }
3543                         }
3544
3545                         /* LCD+TV can't support in slave mode
3546                          * (Force LCDA+TV->LCDB) */
3547                         if ((tempbx & SetInSlaveMode) &&
3548                             (tempbx & SetCRT2ToLCDA)) {
3549                                 tempbx ^= (SetCRT2ToLCD |
3550                                           SetCRT2ToLCDA |
3551                                           SetCRT2ToDualEdge);
3552                                 pVBInfo->SetFlag |= ReserveTVOption;
3553                         }
3554                 }
3555         }
3556
3557         pVBInfo->VBInfo = tempbx;
3558 }
3559
3560 void XGI_GetTVInfo(unsigned short ModeNo, unsigned short ModeIdIndex,
3561                 struct vb_device_info *pVBInfo)
3562 {
3563         unsigned short temp, tempbx = 0, resinfo = 0, modeflag, index1;
3564
3565         tempbx = 0;
3566         resinfo = 0;
3567
3568         if (pVBInfo->VBInfo & SetCRT2ToTV) {
3569                 if (ModeNo <= 0x13) {
3570                         modeflag = pVBInfo->SModeIDTable[ModeIdIndex].
3571                                         St_ModeFlag; /* si+St_ModeFlag */
3572                         resinfo = pVBInfo->SModeIDTable[ModeIdIndex].
3573                                         St_ResInfo; /* si+St_ResInfo */
3574                 } else {
3575                         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].
3576                                         Ext_ModeFlag;
3577                         resinfo = pVBInfo->EModeIDTable[ModeIdIndex].
3578                                         Ext_RESINFO; /* si+Ext_ResInfo */
3579                 }
3580
3581                 if (pVBInfo->VBInfo & SetCRT2ToTV) {
3582                         temp = xgifb_reg_get(pVBInfo->P3d4, 0x35);
3583                         tempbx = temp;
3584                         if (tempbx & SetPALTV) {
3585                                 tempbx &= (SetCHTVOverScan |
3586                                            SetPALMTV |
3587                                            SetPALNTV |
3588                                            SetPALTV);
3589                                 if (tempbx & SetPALMTV)
3590                                         /* set to NTSC if PAL-M */
3591                                         tempbx &= ~SetPALTV;
3592                         } else
3593                                 tempbx &= (SetCHTVOverScan |
3594                                            SetNTSCJ |
3595                                            SetPALTV);
3596                         /*
3597                         if (pVBInfo->IF_DEF_LVDS == 0) {
3598                                 //PAL-M/PAL-N Info
3599                                 index1 = xgifb_reg_get(pVBInfo->P3d4, 0x38);
3600                                 //00:PAL, 01:PAL-M, 10:PAL-N
3601                                 temp2 = (index1 & 0xC0) >> 5;
3602                                 tempbx |= temp2;
3603                                 if (temp2 & 0x02) //PAL-M
3604                                         tempbx &= (~SetPALTV);
3605                         }
3606                         */
3607                 }
3608
3609                 if (pVBInfo->IF_DEF_LVDS == 0) {
3610                         if (pVBInfo->VBInfo & SetCRT2ToSCART)
3611                                 tempbx |= SetPALTV;
3612                 }
3613
3614                 if (pVBInfo->IF_DEF_YPbPr == 1) {
3615                         if (pVBInfo->VBInfo & SetCRT2ToYPbPr) {
3616                                 index1 = xgifb_reg_get(pVBInfo->P3d4, 0x35);
3617                                 index1 &= YPbPrMode;
3618
3619                                 if (index1 == YPbPrMode525i)
3620                                         tempbx |= SetYPbPrMode525i;
3621
3622                                 if (index1 == YPbPrMode525p)
3623                                         tempbx = tempbx | SetYPbPrMode525p;
3624                                 if (index1 == YPbPrMode750p)
3625                                         tempbx = tempbx | SetYPbPrMode750p;
3626                         }
3627                 }
3628
3629                 if (pVBInfo->IF_DEF_HiVision == 1) {
3630                         if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV)
3631                                 tempbx = tempbx | SetYPbPrMode1080i | SetPALTV;
3632                 }
3633
3634                 if (pVBInfo->IF_DEF_LVDS == 0) { /* shampoo */
3635                         if ((pVBInfo->VBInfo & SetInSlaveMode) &&
3636                             (!(pVBInfo->VBInfo & SetNotSimuMode)))
3637                                 tempbx |= TVSimuMode;
3638
3639                         if (!(tempbx & SetPALTV) &&
3640                             (modeflag > 13) &&
3641                             (resinfo == 8)) /* NTSC 1024x768, */
3642                                 tempbx |= NTSC1024x768;
3643
3644                         tempbx |= RPLLDIV2XO;
3645
3646                         if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {
3647                                 if (pVBInfo->VBInfo & SetInSlaveMode)
3648                                         tempbx &= (~RPLLDIV2XO);
3649                         } else {
3650                                 if (tempbx &
3651                                     (SetYPbPrMode525p | SetYPbPrMode750p))
3652                                         tempbx &= (~RPLLDIV2XO);
3653                                 else if (!(pVBInfo->VBType &
3654                                          (VB_XGI301B |
3655                                           VB_XGI302B |
3656                                           VB_XGI301LV |
3657                                           VB_XGI302LV |
3658                                           VB_XGI301C))) {
3659                                         if (tempbx & TVSimuMode)
3660                                                 tempbx &= (~RPLLDIV2XO);
3661                                 }
3662                         }
3663                 }
3664         }
3665         pVBInfo->TVInfo = tempbx;
3666 }
3667
3668 unsigned char XGI_GetLCDInfo(unsigned short ModeNo, unsigned short ModeIdIndex,
3669                 struct vb_device_info *pVBInfo)
3670 {
3671         unsigned short temp, tempax, tempbx, modeflag, resinfo = 0, LCDIdIndex;
3672
3673         pVBInfo->LCDResInfo = 0;
3674         pVBInfo->LCDTypeInfo = 0;
3675         pVBInfo->LCDInfo = 0;
3676
3677         if (ModeNo <= 0x13) {
3678                 /* si+St_ModeFlag // */
3679                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
3680         } else {
3681                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
3682                 /* si+Ext_ResInfo // */
3683                 resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
3684         }
3685
3686         temp = xgifb_reg_get(pVBInfo->P3d4, 0x36); /* Get LCD Res.Info */
3687         tempbx = temp & 0x0F;
3688
3689         if (tempbx == 0)
3690                 tempbx = Panel1024x768; /* default */
3691
3692         /* LCD75 [2003/8/22] Vicent */
3693         if ((tempbx == Panel1024x768) || (tempbx == Panel1280x1024)) {
3694                 if (pVBInfo->VBInfo & DriverMode) {
3695                         tempax = xgifb_reg_get(pVBInfo->P3d4, 0x33);
3696                         if (pVBInfo->VBInfo & SetCRT2ToLCDA)
3697                                 tempax &= 0x0F;
3698                         else
3699                                 tempax = tempax >> 4;
3700
3701                         if ((resinfo == 6) || (resinfo == 9)) {
3702                                 if (tempax >= 3)
3703                                         tempbx |= PanelRef75Hz;
3704                         } else if ((resinfo == 7) || (resinfo == 8)) {
3705                                 if (tempax >= 4)
3706                                         tempbx |= PanelRef75Hz;
3707                         }
3708                 }
3709         }
3710
3711         pVBInfo->LCDResInfo = tempbx;
3712
3713         /* End of LCD75 */
3714
3715         if (pVBInfo->IF_DEF_OEMUtil == 1)
3716                 pVBInfo->LCDTypeInfo = (temp & 0xf0) >> 4;
3717
3718         if (!(pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)))
3719                 return 0;
3720
3721         tempbx = 0;
3722
3723         temp = xgifb_reg_get(pVBInfo->P3d4, 0x37);
3724
3725         temp &= (ScalingLCD | LCDNonExpanding | LCDSyncBit | SetPWDEnable);
3726
3727         if ((pVBInfo->IF_DEF_ScaleLCD == 1) && (temp & LCDNonExpanding))
3728                 temp &= ~EnableScalingLCD;
3729
3730         tempbx |= temp;
3731
3732         LCDIdIndex = XGI_GetLCDCapPtr1(pVBInfo);
3733
3734         tempax = pVBInfo->LCDCapList[LCDIdIndex].LCD_Capability;
3735
3736         if (pVBInfo->IF_DEF_LVDS == 0) { /* shampoo */
3737                 if (((pVBInfo->VBType & VB_XGI302LV) || (pVBInfo->VBType
3738                                 & VB_XGI301C)) && (tempax & LCDDualLink)) {
3739                         tempbx |= SetLCDDualLink;
3740                 }
3741         }
3742
3743         if (pVBInfo->IF_DEF_LVDS == 0) {
3744                 if ((pVBInfo->LCDResInfo == Panel1400x1050) && (pVBInfo->VBInfo
3745                                 & SetCRT2ToLCD) && (ModeNo > 0x13) && (resinfo
3746                                 == 9) && (!(tempbx & EnableScalingLCD)))
3747                         /* set to center in 1280x1024 LCDB for Panel1400x1050 */
3748                         tempbx |= SetLCDtoNonExpanding;
3749         }
3750
3751         /*
3752         if (tempax & LCDBToA) {
3753                 tempbx |= SetLCDBToA;
3754         }
3755         */
3756
3757         if (pVBInfo->IF_DEF_ExpLink == 1) {
3758                 if (modeflag & HalfDCLK) {
3759                         /* if (!(pVBInfo->LCDInfo&LCDNonExpanding)) */
3760                         if (!(tempbx & SetLCDtoNonExpanding)) {
3761                                 tempbx |= EnableLVDSDDA;
3762                         } else {
3763                                 if (ModeNo > 0x13) {
3764                                         if (pVBInfo->LCDResInfo
3765                                                         == Panel1024x768) {
3766                                                 if (resinfo == 4) {/* 512x384 */
3767                                                         tempbx |= EnableLVDSDDA;
3768                                                 }
3769                                         }
3770                                 }
3771                         }
3772                 }
3773         }
3774
3775         if (pVBInfo->VBInfo & SetInSlaveMode) {
3776                 if (pVBInfo->VBInfo & SetNotSimuMode)
3777                         tempbx |= LCDVESATiming;
3778         } else {
3779                 tempbx |= LCDVESATiming;
3780         }
3781
3782         pVBInfo->LCDInfo = tempbx;
3783
3784         if (pVBInfo->IF_DEF_PWD == 1) {
3785                 if (pVBInfo->LCDInfo & SetPWDEnable) {
3786                         if ((pVBInfo->VBType & VB_XGI302LV) ||
3787                             (pVBInfo->VBType & VB_XGI301C)) {
3788                                 if (!(tempax & PWDEnable))
3789                                         pVBInfo->LCDInfo &= ~SetPWDEnable;
3790                         }
3791                 }
3792         }
3793
3794         if (pVBInfo->IF_DEF_LVDS == 0) {
3795                 if (tempax & (LockLCDBToA | StLCDBToA)) {
3796                         if (pVBInfo->VBInfo & SetInSlaveMode) {
3797                                 if (!(tempax & LockLCDBToA)) {
3798                                         if (ModeNo <= 0x13) {
3799                                                 pVBInfo->VBInfo &=
3800                                                         ~(SetSimuScanMode |
3801                                                           SetInSlaveMode |
3802                                                           SetCRT2ToLCD);
3803                                                 pVBInfo->VBInfo |=
3804                                                         SetCRT2ToLCDA |
3805                                                         SetCRT2ToDualEdge;
3806                                         }
3807                                 }
3808                         }
3809                 }
3810         }
3811
3812         /*
3813         if (pVBInfo->IF_DEF_LVDS == 0) {
3814                 if (tempax & (LockLCDBToA | StLCDBToA)) {
3815                         if (pVBInfo->VBInfo & SetInSlaveMode) {
3816                                 if (!((!(tempax & LockLCDBToA)) &&
3817                                     (ModeNo > 0x13))) {
3818                                         pVBInfo->VBInfo &=
3819                                                 ~(SetSimuScanMode |
3820                                                   SetInSlaveMode |
3821                                                   SetCRT2ToLCD);
3822                                         pVBInfo->VBInfo |=
3823                                                 SetCRT2ToLCDA |
3824                                                 SetCRT2ToDualEdge;
3825                                 }
3826                         }
3827                 }
3828         }
3829         */
3830
3831         return 1;
3832 }
3833
3834 unsigned char XGI_SearchModeID(unsigned short ModeNo,
3835                 unsigned short *ModeIdIndex, struct vb_device_info *pVBInfo)
3836 {
3837         if (ModeNo <= 5)
3838                 ModeNo |= 1;
3839         if (ModeNo <= 0x13) {
3840                 /* for (*ModeIdIndex=0;
3841                         *ModeIdIndex < sizeof(pVBInfo->SModeIDTable)
3842                                 / sizeof(struct XGI_StStruct);
3843                         (*ModeIdIndex)++) */
3844                 for (*ModeIdIndex = 0;; (*ModeIdIndex)++) {
3845                         if (pVBInfo->SModeIDTable[*ModeIdIndex].St_ModeID ==
3846                             ModeNo)
3847                                 break;
3848                         if (pVBInfo->SModeIDTable[*ModeIdIndex].St_ModeID ==
3849                             0xFF)
3850                                 return 0;
3851                 }
3852
3853                 if (ModeNo == 0x07)
3854                         (*ModeIdIndex)++; /* 400 lines */
3855                 if (ModeNo <= 3)
3856                         (*ModeIdIndex) += 2; /* 400 lines */
3857                 /* else 350 lines */
3858         } else {
3859                 /* for (*ModeIdIndex=0;
3860                         *ModeIdIndex < sizeof(pVBInfo->EModeIDTable)
3861                                 / sizeof(struct XGI_ExtStruct);
3862                         (*ModeIdIndex)++) */
3863                 for (*ModeIdIndex = 0;; (*ModeIdIndex)++) {
3864                         if (pVBInfo->EModeIDTable[*ModeIdIndex].Ext_ModeID ==
3865                             ModeNo)
3866                                 break;
3867                         if (pVBInfo->EModeIDTable[*ModeIdIndex].Ext_ModeID ==
3868                             0xFF)
3869                                 return 0;
3870                 }
3871         }
3872
3873         return 1;
3874 }
3875
3876 /* win2000 MM adapter not support standard mode! */
3877
3878 #if 0
3879 static unsigned char XGINew_CheckMemorySize(
3880                 struct xgi_hw_device_info *HwDeviceExtension,
3881                 unsigned short ModeNo,
3882                 unsigned short ModeIdIndex,
3883                 struct vb_device_info *pVBInfo)
3884 {
3885         unsigned short memorysize, modeflag, temp, temp1, tmp;
3886
3887         /*
3888         if ((HwDeviceExtension->jChipType == XGI_650) ||
3889         (HwDeviceExtension->jChipType == XGI_650M)) {
3890                 return 1;
3891         }
3892         */
3893
3894         if (ModeNo <= 0x13)
3895                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
3896         else
3897                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
3898
3899         /* ModeType = modeflag&ModeInfoFlag; // Get mode type */
3900
3901         memorysize = modeflag & MemoryInfoFlag;
3902         memorysize = memorysize > MemorySizeShift;
3903         memorysize++; /* Get memory size */
3904
3905         temp = xgifb_reg_get(pVBInfo->P3c4, 0x14); /* Get DRAM Size */
3906         tmp = temp;
3907
3908         if (HwDeviceExtension->jChipType == XG40) {
3909                 /* memory size per channel SR14[7:4] */
3910                 temp = 1 << ((temp & 0x0F0) >> 4);
3911                 if ((tmp & 0x0c) == 0x0C) { /* Qual channels */
3912                         temp <<= 2;
3913                 } else if ((tmp & 0x0c) == 0x08) { /* Dual channels */
3914                         temp <<= 1;
3915                 }
3916         } else if (HwDeviceExtension->jChipType == XG42) {
3917                 /* memory size per channel SR14[7:4] */
3918                 temp = 1 << ((temp & 0x0F0) >> 4);
3919                 if ((tmp & 0x04) == 0x04) { /* Dual channels */
3920                         temp <<= 1;
3921                 }
3922         } else if (HwDeviceExtension->jChipType == XG45) {
3923                 /* memory size per channel SR14[7:4] */
3924                 temp = 1 << ((temp & 0x0F0) >> 4);
3925                 if ((tmp & 0x0c) == 0x0C) { /* Qual channels */
3926                         temp <<= 2;
3927                 } else if ((tmp & 0x0c) == 0x08) { /* triple channels */
3928                         temp1 = temp;
3929                         temp <<= 1;
3930                         temp += temp1;
3931                 } else if ((tmp & 0x0c) == 0x04) { /* Dual channels */
3932                         temp <<= 1;
3933                 }
3934         }
3935         if (temp < memorysize)
3936                 return 0;
3937         else
3938                 return 1;
3939 }
3940 #endif
3941
3942 /*
3943 void XGINew_IsLowResolution(unsigned short ModeNo,
3944                             unsigned short ModeIdIndex,
3945                             unsigned char XGINew_CheckMemorySize(
3946                                 struct xgi_hw_device_info *HwDeviceExtension,
3947                                 unsigned short ModeNo,
3948                                 unsigned short ModeIdIndex,
3949                                 struct vb_device_info *pVBInfo)
3950 {
3951         unsigned short data ;
3952         unsigned short ModeFlag ;
3953
3954         data = xgifb_reg_get(pVBInfo->P3c4, 0x0F);
3955         data &= 0x7F;
3956         xgifb_reg_set(pVBInfo->P3c4, 0x0F, data);
3957
3958         if (ModeNo > 0x13) {
3959                 ModeFlag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
3960                 if ((ModeFlag & HalfDCLK) && (ModeFlag & DoubleScanMode)) {
3961                         data = xgifb_reg_get(pVBInfo->P3c4, 0x0F);
3962                         data |= 0x80;
3963                         xgifb_reg_set(pVBInfo->P3c4, 0x0F, data);
3964                         data = xgifb_reg_get(pVBInfo->P3c4, 0x01);
3965                         data &= 0xF7;
3966                         xgifb_reg_set(pVBInfo->P3c4, 0x01, data);
3967                 }
3968         }
3969 }
3970 */
3971
3972 static unsigned char XG21GPIODataTransfer(unsigned char ujDate)
3973 {
3974         unsigned char ujRet = 0;
3975         unsigned char i = 0;
3976
3977         for (i = 0; i < 8; i++) {
3978                 ujRet = ujRet << 1;
3979                 /* ujRet |= GETBITS(ujDate >> i, 0:0); */
3980                 ujRet |= (ujDate >> i) & 1;
3981         }
3982
3983         return ujRet;
3984 }
3985
3986 /*----------------------------------------------------------------------------*/
3987 /* output                                                                     */
3988 /*      bl[5] : LVDS signal                                                   */
3989 /*      bl[1] : LVDS backlight                                                */
3990 /*      bl[0] : LVDS VDD                                                      */
3991 /*----------------------------------------------------------------------------*/
3992 static unsigned char XGI_XG21GetPSCValue(struct vb_device_info *pVBInfo)
3993 {
3994         unsigned char CR4A, temp;
3995
3996         CR4A = xgifb_reg_get(pVBInfo->P3d4, 0x4A);
3997         xgifb_reg_and(pVBInfo->P3d4, 0x4A, ~0x23); /* enable GPIO write */
3998
3999         temp = xgifb_reg_get(pVBInfo->P3d4, 0x48);
4000
4001         temp = XG21GPIODataTransfer(temp);
4002         temp &= 0x23;
4003         xgifb_reg_set(pVBInfo->P3d4, 0x4A, CR4A);
4004         return temp;
4005 }
4006
4007 /*----------------------------------------------------------------------------*/
4008 /* output                                                                     */
4009 /*      bl[5] : LVDS signal                                                   */
4010 /*      bl[1] : LVDS backlight                                                */
4011 /*      bl[0] : LVDS VDD                                                      */
4012 /*----------------------------------------------------------------------------*/
4013 static unsigned char XGI_XG27GetPSCValue(struct vb_device_info *pVBInfo)
4014 {
4015         unsigned char CR4A, CRB4, temp;
4016
4017         CR4A = xgifb_reg_get(pVBInfo->P3d4, 0x4A);
4018         xgifb_reg_and(pVBInfo->P3d4, 0x4A, ~0x0C); /* enable GPIO write */
4019
4020         temp = xgifb_reg_get(pVBInfo->P3d4, 0x48);
4021
4022         temp &= 0x0C;
4023         temp >>= 2;
4024         xgifb_reg_set(pVBInfo->P3d4, 0x4A, CR4A);
4025         CRB4 = xgifb_reg_get(pVBInfo->P3d4, 0xB4);
4026         temp |= ((CRB4 & 0x04) << 3);
4027         return temp;
4028 }
4029
4030 void XGI_DisplayOn(struct xgi_hw_device_info *pXGIHWDE,
4031                 struct vb_device_info *pVBInfo)
4032 {
4033
4034         xgifb_reg_and_or(pVBInfo->P3c4, 0x01, 0xDF, 0x00);
4035         if (pXGIHWDE->jChipType == XG21) {
4036                 if (pVBInfo->IF_DEF_LVDS == 1) {
4037                         if (!(XGI_XG21GetPSCValue(pVBInfo) & 0x1)) {
4038                                 /* LVDS VDD on */
4039                                 XGI_XG21BLSignalVDD(0x01, 0x01, pVBInfo);
4040                                 XGI_XG21SetPanelDelay(2, pVBInfo);
4041                         }
4042                         if (!(XGI_XG21GetPSCValue(pVBInfo) & 0x20))
4043                                 /* LVDS signal on */
4044                                 XGI_XG21BLSignalVDD(0x20, 0x20, pVBInfo);
4045                         XGI_XG21SetPanelDelay(3, pVBInfo);
4046                         /* LVDS backlight on */
4047                         XGI_XG21BLSignalVDD(0x02, 0x02, pVBInfo);
4048                 } else {
4049                         /* DVO/DVI signal on */
4050                         XGI_XG21BLSignalVDD(0x20, 0x20, pVBInfo);
4051                 }
4052
4053         }
4054
4055         if (pXGIHWDE->jChipType == XG27) {
4056                 if (pVBInfo->IF_DEF_LVDS == 1) {
4057                         if (!(XGI_XG27GetPSCValue(pVBInfo) & 0x1)) {
4058                                 /* LVDS VDD on */
4059                                 XGI_XG27BLSignalVDD(0x01, 0x01, pVBInfo);
4060                                 XGI_XG21SetPanelDelay(2, pVBInfo);
4061                         }
4062                         if (!(XGI_XG27GetPSCValue(pVBInfo) & 0x20))
4063                                 /* LVDS signal on */
4064                                 XGI_XG27BLSignalVDD(0x20, 0x20, pVBInfo);
4065                         XGI_XG21SetPanelDelay(3, pVBInfo);
4066                         /* LVDS backlight on */
4067                         XGI_XG27BLSignalVDD(0x02, 0x02, pVBInfo);
4068                 } else {
4069                         /* DVO/DVI signal on */
4070                         XGI_XG27BLSignalVDD(0x20, 0x20, pVBInfo);
4071                 }
4072
4073         }
4074 }
4075
4076 void XGI_DisplayOff(struct xgi_hw_device_info *pXGIHWDE,
4077                 struct vb_device_info *pVBInfo)
4078 {
4079
4080         if (pXGIHWDE->jChipType == XG21) {
4081                 if (pVBInfo->IF_DEF_LVDS == 1) {
4082                         /* LVDS backlight off */
4083                         XGI_XG21BLSignalVDD(0x02, 0x00, pVBInfo);
4084                         XGI_XG21SetPanelDelay(3, pVBInfo);
4085                 } else {
4086                         /* DVO/DVI signal off */
4087                         XGI_XG21BLSignalVDD(0x20, 0x00, pVBInfo);
4088                 }
4089         }
4090
4091         if (pXGIHWDE->jChipType == XG27) {
4092                 if ((XGI_XG27GetPSCValue(pVBInfo) & 0x2)) {
4093                         /* LVDS backlight off */
4094                         XGI_XG27BLSignalVDD(0x02, 0x00, pVBInfo);
4095                         XGI_XG21SetPanelDelay(3, pVBInfo);
4096                 }
4097
4098                 if (pVBInfo->IF_DEF_LVDS == 0)
4099                         /* DVO/DVI signal off */
4100                         XGI_XG27BLSignalVDD(0x20, 0x00, pVBInfo);
4101         }
4102
4103         xgifb_reg_and_or(pVBInfo->P3c4, 0x01, 0xDF, 0x20);
4104 }
4105
4106 static void XGI_WaitDisply(struct vb_device_info *pVBInfo)
4107 {
4108         while ((inb(pVBInfo->P3da) & 0x01))
4109                 break;
4110
4111         while (!(inb(pVBInfo->P3da) & 0x01))
4112                 break;
4113 }
4114
4115 #if 0
4116 static void XGI_WaitDisplay(struct vb_device_info *pVBInfo)
4117 {
4118         while (!(inb(pVBInfo->P3da) & 0x01))
4119                 ;
4120         while (inb(pVBInfo->P3da) & 0x01)
4121                 ;
4122 }
4123 #endif
4124
4125 static void XGI_AutoThreshold(struct vb_device_info *pVBInfo)
4126 {
4127         if (!(pVBInfo->SetFlag & Win9xDOSMode))
4128                 xgifb_reg_or(pVBInfo->Part1Port, 0x01, 0x40);
4129 }
4130
4131 static void XGI_SaveCRT2Info(unsigned short ModeNo,
4132                              struct vb_device_info *pVBInfo)
4133 {
4134         unsigned short temp1, temp2;
4135
4136         /* reserve CR34 for CRT1 Mode No */
4137         xgifb_reg_set(pVBInfo->P3d4, 0x34, ModeNo);
4138         temp1 = (pVBInfo->VBInfo & SetInSlaveMode) >> 8;
4139         temp2 = ~(SetInSlaveMode >> 8);
4140         xgifb_reg_and_or(pVBInfo->P3d4, 0x31, temp2, temp1);
4141 }
4142
4143 static void XGI_GetCRT2ResInfo(unsigned short ModeNo,
4144                                unsigned short ModeIdIndex,
4145                                struct vb_device_info *pVBInfo)
4146 {
4147         unsigned short xres, yres, modeflag, resindex;
4148
4149         resindex = XGI_GetResInfo(ModeNo, ModeIdIndex, pVBInfo);
4150         if (ModeNo <= 0x13) {
4151                 xres = pVBInfo->StResInfo[resindex].HTotal;
4152                 yres = pVBInfo->StResInfo[resindex].VTotal;
4153                 /* si+St_ResInfo */
4154                 /* modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;*/
4155         } else {
4156                 xres = pVBInfo->ModeResInfo[resindex].HTotal; /* xres->ax */
4157                 yres = pVBInfo->ModeResInfo[resindex].VTotal; /* yres->bx */
4158                 /* si+St_ModeFlag */
4159                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
4160
4161                 /*
4162                 if (pVBInfo->IF_DEF_FSTN) {
4163                         xres *= 2;
4164                         yres *= 2;
4165                  } else {
4166                 */
4167                 if (modeflag & HalfDCLK)
4168                         xres *= 2;
4169
4170                 if (modeflag & DoubleScanMode)
4171                         yres *= 2;
4172                 /* } */
4173         }
4174
4175         if (pVBInfo->VBInfo & SetCRT2ToLCD) {
4176                 if (pVBInfo->IF_DEF_LVDS == 0) {
4177                         if (pVBInfo->LCDResInfo == Panel1600x1200) {
4178                                 if (!(pVBInfo->LCDInfo & LCDVESATiming)) {
4179                                         if (yres == 1024)
4180                                                 yres = 1056;
4181                                 }
4182                         }
4183
4184                         if (pVBInfo->LCDResInfo == Panel1280x1024) {
4185                                 if (yres == 400)
4186                                         yres = 405;
4187                                 else if (yres == 350)
4188                                         yres = 360;
4189
4190                                 if (pVBInfo->LCDInfo & LCDVESATiming) {
4191                                         if (yres == 360)
4192                                                 yres = 375;
4193                                 }
4194                         }
4195
4196                         if (pVBInfo->LCDResInfo == Panel1024x768) {
4197                                 if (!(pVBInfo->LCDInfo & LCDVESATiming)) {
4198                                         if (!(pVBInfo->LCDInfo
4199                                                         & LCDNonExpanding)) {
4200                                                 if (yres == 350)
4201                                                         yres = 357;
4202                                                 else if (yres == 400)
4203                                                         yres = 420;
4204                                                 else if (yres == 480)
4205                                                         yres = 525;
4206                                         }
4207                                 }
4208                         }
4209                 }
4210
4211                 if (xres == 720)
4212                         xres = 640;
4213         }
4214
4215         pVBInfo->VGAHDE = xres;
4216         pVBInfo->HDE = xres;
4217         pVBInfo->VGAVDE = yres;
4218         pVBInfo->VDE = yres;
4219 }
4220
4221 static unsigned char XGI_IsLCDDualLink(struct vb_device_info *pVBInfo)
4222 {
4223
4224         if ((pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) &&
4225                         (pVBInfo->LCDInfo & SetLCDDualLink)) /* shampoo0129 */
4226                 return 1;
4227
4228         return 0;
4229 }
4230
4231 static void XGI_GetRAMDAC2DATA(unsigned short ModeNo,
4232                                unsigned short ModeIdIndex,
4233                                unsigned short RefreshRateTableIndex,
4234                                struct vb_device_info *pVBInfo)
4235 {
4236         unsigned short tempax, tempbx, temp1, temp2, modeflag = 0, tempcx,
4237                         StandTableIndex, CRT1Index;
4238
4239         pVBInfo->RVBHCMAX = 1;
4240         pVBInfo->RVBHCFACT = 1;
4241
4242         if (ModeNo <= 0x13) {
4243                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
4244                 StandTableIndex = XGI_GetModePtr(ModeNo, ModeIdIndex, pVBInfo);
4245                 tempax = pVBInfo->StandTable[StandTableIndex].CRTC[0];
4246                 tempbx = pVBInfo->StandTable[StandTableIndex].CRTC[6];
4247                 temp1 = pVBInfo->StandTable[StandTableIndex].CRTC[7];
4248         } else {
4249                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
4250                 CRT1Index = pVBInfo->RefIndex[RefreshRateTableIndex].
4251                                 Ext_CRT1CRTC;
4252                 CRT1Index &= IndexMask;
4253                 temp1 = (unsigned short) pVBInfo->
4254                         XGINEWUB_CRT1Table[CRT1Index].CR[0];
4255                 temp2 = (unsigned short) pVBInfo->
4256                         XGINEWUB_CRT1Table[CRT1Index].CR[5];
4257                 tempax = (temp1 & 0xFF) | ((temp2 & 0x03) << 8);
4258                 tempbx = (unsigned short) pVBInfo->
4259                         XGINEWUB_CRT1Table[CRT1Index].CR[8];
4260                 tempcx = (unsigned short) pVBInfo->
4261                         XGINEWUB_CRT1Table[CRT1Index].CR[14] << 8;
4262                 tempcx &= 0x0100;
4263                 tempcx = tempcx << 2;
4264                 tempbx |= tempcx;
4265                 temp1 = (unsigned short) pVBInfo->
4266                         XGINEWUB_CRT1Table[CRT1Index].CR[9];
4267         }
4268
4269         if (temp1 & 0x01)
4270                 tempbx |= 0x0100;
4271
4272         if (temp1 & 0x20)
4273                 tempbx |= 0x0200;
4274         tempax += 5;
4275
4276         if (modeflag & Charx8Dot)
4277                 tempax *= 8;
4278         else
4279                 tempax *= 9;
4280
4281         pVBInfo->VGAHT = tempax;
4282         pVBInfo->HT = tempax;
4283         tempbx++;
4284         pVBInfo->VGAVT = tempbx;
4285         pVBInfo->VT = tempbx;
4286 }
4287
4288 static void XGI_GetCRT2Data(unsigned short ModeNo, unsigned short ModeIdIndex,
4289                 unsigned short RefreshRateTableIndex,
4290                 struct vb_device_info *pVBInfo)
4291 {
4292         unsigned short tempax = 0, tempbx, modeflag, resinfo;
4293
4294         struct XGI_LCDDataStruct *LCDPtr = NULL;
4295         struct XGI_TVDataStruct *TVPtr = NULL;
4296
4297         if (ModeNo <= 0x13) {
4298                 /* si+St_ResInfo */
4299                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
4300                 resinfo = pVBInfo->SModeIDTable[ModeIdIndex].St_ResInfo;
4301         } else {
4302                 /* si+Ext_ResInfo */
4303                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
4304                 resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
4305         }
4306
4307         pVBInfo->NewFlickerMode = 0;
4308         pVBInfo->RVBHRS = 50;
4309
4310         if (pVBInfo->VBInfo & SetCRT2ToRAMDAC) {
4311                 XGI_GetRAMDAC2DATA(ModeNo, ModeIdIndex, RefreshRateTableIndex,
4312                                 pVBInfo);
4313                 return;
4314         }
4315
4316         tempbx = 4;
4317
4318         if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
4319                 LCDPtr = (struct XGI_LCDDataStruct *) XGI_GetLcdPtr(tempbx,
4320                                 ModeNo, ModeIdIndex, RefreshRateTableIndex,
4321                                 pVBInfo);
4322
4323                 pVBInfo->RVBHCMAX = LCDPtr->RVBHCMAX;
4324                 pVBInfo->RVBHCFACT = LCDPtr->RVBHCFACT;
4325                 pVBInfo->VGAHT = LCDPtr->VGAHT;
4326                 pVBInfo->VGAVT = LCDPtr->VGAVT;
4327                 pVBInfo->HT = LCDPtr->LCDHT;
4328                 pVBInfo->VT = LCDPtr->LCDVT;
4329
4330                 if (pVBInfo->LCDResInfo == Panel1024x768) {
4331                         tempax = 1024;
4332                         tempbx = 768;
4333
4334                         if (!(pVBInfo->LCDInfo & LCDVESATiming)) {
4335                                 if (pVBInfo->VGAVDE == 357)
4336                                         tempbx = 527;
4337                                 else if (pVBInfo->VGAVDE == 420)
4338                                         tempbx = 620;
4339                                 else if (pVBInfo->VGAVDE == 525)
4340                                         tempbx = 775;
4341                                 else if (pVBInfo->VGAVDE == 600)
4342                                         tempbx = 775;
4343                                 /* else if (pVBInfo->VGAVDE==350) tempbx=560; */
4344                                 /* else if (pVBInfo->VGAVDE==400) tempbx=640; */
4345                                 else
4346                                         tempbx = 768;
4347                         } else
4348                                 tempbx = 768;
4349                 } else if (pVBInfo->LCDResInfo == Panel1024x768x75) {
4350                         tempax = 1024;
4351                         tempbx = 768;
4352                 } else if (pVBInfo->LCDResInfo == Panel1280x1024) {
4353                         tempax = 1280;
4354                         if (pVBInfo->VGAVDE == 360)
4355                                 tempbx = 768;
4356                         else if (pVBInfo->VGAVDE == 375)
4357                                 tempbx = 800;
4358                         else if (pVBInfo->VGAVDE == 405)
4359                                 tempbx = 864;
4360                         else
4361                                 tempbx = 1024;
4362                 } else if (pVBInfo->LCDResInfo == Panel1280x1024x75) {
4363                         tempax = 1280;
4364                         tempbx = 1024;
4365                 } else if (pVBInfo->LCDResInfo == Panel1280x960) {
4366                         tempax = 1280;
4367                         if (pVBInfo->VGAVDE == 350)
4368                                 tempbx = 700;
4369                         else if (pVBInfo->VGAVDE == 400)
4370                                 tempbx = 800;
4371                         else if (pVBInfo->VGAVDE == 1024)
4372                                 tempbx = 960;
4373                         else
4374                                 tempbx = 960;
4375                 } else if (pVBInfo->LCDResInfo == Panel1400x1050) {
4376                         tempax = 1400;
4377                         tempbx = 1050;
4378
4379                         if (pVBInfo->VGAVDE == 1024) {
4380                                 tempax = 1280;
4381                                 tempbx = 1024;
4382                         }
4383                 } else if (pVBInfo->LCDResInfo == Panel1600x1200) {
4384                         tempax = 1600;
4385                         tempbx = 1200; /* alan 10/14/2003 */
4386                         if (!(pVBInfo->LCDInfo & LCDVESATiming)) {
4387                                 if (pVBInfo->VGAVDE == 350)
4388                                         tempbx = 875;
4389                                 else if (pVBInfo->VGAVDE == 400)
4390                                         tempbx = 1000;
4391                         }
4392                 }
4393
4394                 if (pVBInfo->LCDInfo & LCDNonExpanding) {
4395                         tempax = pVBInfo->VGAHDE;
4396                         tempbx = pVBInfo->VGAVDE;
4397                 }
4398
4399                 pVBInfo->HDE = tempax;
4400                 pVBInfo->VDE = tempbx;
4401                 return;
4402         }
4403
4404         if (pVBInfo->VBInfo & (SetCRT2ToTV)) {
4405                 tempbx = 4;
4406                 TVPtr = (struct XGI_TVDataStruct *) XGI_GetTVPtr(tempbx,
4407                                 ModeNo, ModeIdIndex, RefreshRateTableIndex,
4408                                 pVBInfo);
4409
4410                 pVBInfo->RVBHCMAX = TVPtr->RVBHCMAX;
4411                 pVBInfo->RVBHCFACT = TVPtr->RVBHCFACT;
4412                 pVBInfo->VGAHT = TVPtr->VGAHT;
4413                 pVBInfo->VGAVT = TVPtr->VGAVT;
4414                 pVBInfo->HDE = TVPtr->TVHDE;
4415                 pVBInfo->VDE = TVPtr->TVVDE;
4416                 pVBInfo->RVBHRS = TVPtr->RVBHRS;
4417                 pVBInfo->NewFlickerMode = TVPtr->FlickerMode;
4418
4419                 if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {
4420                         if (resinfo == 0x08)
4421                                 pVBInfo->NewFlickerMode = 0x40;
4422                         else if (resinfo == 0x09)
4423                                 pVBInfo->NewFlickerMode = 0x40;
4424                         else if (resinfo == 0x12)
4425                                 pVBInfo->NewFlickerMode = 0x40;
4426
4427                         if (pVBInfo->VGAVDE == 350)
4428                                 pVBInfo->TVInfo |= TVSimuMode;
4429
4430                         tempax = ExtHiTVHT;
4431                         tempbx = ExtHiTVVT;
4432
4433                         if (pVBInfo->VBInfo & SetInSlaveMode) {
4434                                 if (pVBInfo->TVInfo & TVSimuMode) {
4435                                         tempax = StHiTVHT;
4436                                         tempbx = StHiTVVT;
4437
4438                                         if (!(modeflag & Charx8Dot)) {
4439                                                 tempax = StHiTextTVHT;
4440                                                 tempbx = StHiTextTVVT;
4441                                         }
4442                                 }
4443                         }
4444                 } else if (pVBInfo->VBInfo & SetCRT2ToYPbPr) {
4445                         if (pVBInfo->TVInfo & SetYPbPrMode750p) {
4446                                 tempax = YPbPrTV750pHT; /* Ext750pTVHT */
4447                                 tempbx = YPbPrTV750pVT; /* Ext750pTVVT */
4448                         }
4449
4450                         if (pVBInfo->TVInfo & SetYPbPrMode525p) {
4451                                 tempax = YPbPrTV525pHT; /* Ext525pTVHT */
4452                                 tempbx = YPbPrTV525pVT; /* Ext525pTVVT */
4453                         } else if (pVBInfo->TVInfo & SetYPbPrMode525i) {
4454                                 tempax = YPbPrTV525iHT; /* Ext525iTVHT */
4455                                 tempbx = YPbPrTV525iVT; /* Ext525iTVVT */
4456                                 if (pVBInfo->TVInfo & NTSC1024x768)
4457                                         tempax = NTSC1024x768HT;
4458                         }
4459                 } else {
4460                         tempax = PALHT;
4461                         tempbx = PALVT;
4462                         if (!(pVBInfo->TVInfo & SetPALTV)) {
4463                                 tempax = NTSCHT;
4464                                 tempbx = NTSCVT;
4465                                 if (pVBInfo->TVInfo & NTSC1024x768)
4466                                         tempax = NTSC1024x768HT;
4467                         }
4468                 }
4469
4470                 pVBInfo->HT = tempax;
4471                 pVBInfo->VT = tempbx;
4472                 return;
4473         }
4474 }
4475
4476 static void XGI_SetCRT2VCLK(unsigned short ModeNo, unsigned short ModeIdIndex,
4477                 unsigned short RefreshRateTableIndex,
4478                 struct vb_device_info *pVBInfo)
4479 {
4480         unsigned char di_0, di_1, tempal;
4481
4482         tempal = XGI_GetVCLKPtr(RefreshRateTableIndex, ModeNo, ModeIdIndex,
4483                         pVBInfo);
4484         XGI_GetVCLKLen(tempal, &di_0, &di_1, pVBInfo);
4485         XGI_GetLCDVCLKPtr(&di_0, &di_1, pVBInfo);
4486
4487         if (pVBInfo->VBType & VB_XGI301) { /* shampoo 0129 */
4488                 /* 301 */
4489                 xgifb_reg_set(pVBInfo->Part4Port, 0x0A, 0x10);
4490                 xgifb_reg_set(pVBInfo->Part4Port, 0x0B, di_1);
4491                 xgifb_reg_set(pVBInfo->Part4Port, 0x0A, di_0);
4492         } else { /* 301b/302b/301lv/302lv */
4493                 xgifb_reg_set(pVBInfo->Part4Port, 0x0A, di_0);
4494                 xgifb_reg_set(pVBInfo->Part4Port, 0x0B, di_1);
4495         }
4496
4497         xgifb_reg_set(pVBInfo->Part4Port, 0x00, 0x12);
4498
4499         if (pVBInfo->VBInfo & SetCRT2ToRAMDAC)
4500                 xgifb_reg_or(pVBInfo->Part4Port, 0x12, 0x28);
4501         else
4502                 xgifb_reg_or(pVBInfo->Part4Port, 0x12, 0x08);
4503 }
4504
4505 static unsigned short XGI_GetColorDepth(unsigned short ModeNo,
4506                 unsigned short ModeIdIndex, struct vb_device_info *pVBInfo)
4507 {
4508         unsigned short ColorDepth[6] = { 1, 2, 4, 4, 6, 8 };
4509         short index;
4510         unsigned short modeflag;
4511
4512         if (ModeNo <= 0x13)
4513                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
4514         else
4515                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
4516
4517         index = (modeflag & ModeInfoFlag) - ModeEGA;
4518
4519         if (index < 0)
4520                 index = 0;
4521
4522         return ColorDepth[index];
4523 }
4524
4525 static unsigned short XGI_GetOffset(unsigned short ModeNo,
4526                                     unsigned short ModeIdIndex,
4527                 unsigned short RefreshRateTableIndex,
4528                 struct xgi_hw_device_info *HwDeviceExtension,
4529                 struct vb_device_info *pVBInfo)
4530 {
4531         unsigned short temp, colordepth, modeinfo, index, infoflag,
4532                         ColorDepth[] = { 0x01, 0x02, 0x04 };
4533
4534         modeinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeInfo;
4535         if (ModeNo <= 0x14)
4536                 infoflag = 0;
4537         else
4538                 infoflag = pVBInfo->
4539                                 RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
4540
4541         index = (modeinfo >> 8) & 0xFF;
4542
4543         temp = pVBInfo->ScreenOffset[index];
4544
4545         if (infoflag & InterlaceMode)
4546                 temp = temp << 1;
4547
4548         colordepth = XGI_GetColorDepth(ModeNo, ModeIdIndex, pVBInfo);
4549
4550         if ((ModeNo >= 0x7C) && (ModeNo <= 0x7E)) {
4551                 temp = ModeNo - 0x7C;
4552                 colordepth = ColorDepth[temp];
4553                 temp = 0x6B;
4554                 if (infoflag & InterlaceMode)
4555                         temp = temp << 1;
4556                 return temp * colordepth;
4557         } else {
4558                 return temp * colordepth;
4559         }
4560 }
4561
4562 static void XGI_SetCRT2Offset(unsigned short ModeNo,
4563                 unsigned short ModeIdIndex,
4564                 unsigned short RefreshRateTableIndex,
4565                 struct xgi_hw_device_info *HwDeviceExtension,
4566                 struct vb_device_info *pVBInfo)
4567 {
4568         unsigned short offset;
4569         unsigned char temp;
4570
4571         if (pVBInfo->VBInfo & SetInSlaveMode)
4572                 return;
4573
4574         offset = XGI_GetOffset(ModeNo, ModeIdIndex, RefreshRateTableIndex,
4575                         HwDeviceExtension, pVBInfo);
4576         temp = (unsigned char) (offset & 0xFF);
4577         xgifb_reg_set(pVBInfo->Part1Port, 0x07, temp);
4578         temp = (unsigned char) ((offset & 0xFF00) >> 8);
4579         xgifb_reg_set(pVBInfo->Part1Port, 0x09, temp);
4580         temp = (unsigned char) (((offset >> 3) & 0xFF) + 1);
4581         xgifb_reg_set(pVBInfo->Part1Port, 0x03, temp);
4582 }
4583
4584 static void XGI_SetCRT2FIFO(struct vb_device_info *pVBInfo)
4585 {
4586         /* threshold high ,disable auto threshold */
4587         xgifb_reg_set(pVBInfo->Part1Port, 0x01, 0x3B);
4588         /* threshold low default 04h */
4589         xgifb_reg_and_or(pVBInfo->Part1Port, 0x02, ~(0x3F), 0x04);
4590 }
4591
4592 static void XGI_PreSetGroup1(unsigned short ModeNo, unsigned short ModeIdIndex,
4593                 struct xgi_hw_device_info *HwDeviceExtension,
4594                 unsigned short RefreshRateTableIndex,
4595                 struct vb_device_info *pVBInfo)
4596 {
4597         unsigned short tempcx = 0, CRT1Index = 0, resinfo = 0;
4598
4599         if (ModeNo > 0x13) {
4600                 CRT1Index = pVBInfo->
4601                                 RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
4602                 CRT1Index &= IndexMask;
4603                 resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
4604         }
4605
4606         XGI_SetCRT2Offset(ModeNo, ModeIdIndex, RefreshRateTableIndex,
4607                         HwDeviceExtension, pVBInfo);
4608         XGI_SetCRT2FIFO(pVBInfo);
4609         /* XGI_SetCRT2Sync(ModeNo,RefreshRateTableIndex); */
4610
4611         for (tempcx = 4; tempcx < 7; tempcx++)
4612                 xgifb_reg_set(pVBInfo->Part1Port, tempcx, 0x0);
4613
4614         xgifb_reg_set(pVBInfo->Part1Port, 0x50, 0x00);
4615         xgifb_reg_set(pVBInfo->Part1Port, 0x02, 0x44); /* temp 0206 */
4616 }
4617
4618 static void XGI_SetGroup1(unsigned short ModeNo, unsigned short ModeIdIndex,
4619                 struct xgi_hw_device_info *HwDeviceExtension,
4620                 unsigned short RefreshRateTableIndex,
4621                 struct vb_device_info *pVBInfo)
4622 {
4623         unsigned short temp = 0, tempax = 0, tempbx = 0, tempcx = 0,
4624                         pushbx = 0, CRT1Index = 0, modeflag, resinfo = 0;
4625
4626         if (ModeNo > 0x13) {
4627                 CRT1Index = pVBInfo->
4628                                 RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
4629                 CRT1Index &= IndexMask;
4630                 resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
4631         }
4632
4633         if (ModeNo <= 0x13)
4634                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
4635         else
4636                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
4637
4638         /* bainy change table name */
4639         if (modeflag & HalfDCLK) {
4640                 /* BTVGA2HT 0x08,0x09 */
4641                 temp = (pVBInfo->VGAHT / 2 - 1) & 0x0FF;
4642                 xgifb_reg_set(pVBInfo->Part1Port, 0x08, temp);
4643                 temp = (((pVBInfo->VGAHT / 2 - 1) & 0xFF00) >> 8) << 4;
4644                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x09, ~0x0F0, temp);
4645                 /* BTVGA2HDEE 0x0A,0x0C */
4646                 temp = (pVBInfo->VGAHDE / 2 + 16) & 0x0FF;
4647                 xgifb_reg_set(pVBInfo->Part1Port, 0x0A, temp);
4648                 tempcx = ((pVBInfo->VGAHT - pVBInfo->VGAHDE) / 2) >> 2;
4649                 pushbx = pVBInfo->VGAHDE / 2 + 16;
4650                 tempcx = tempcx >> 1;
4651                 tempbx = pushbx + tempcx; /* bx BTVGA@HRS 0x0B,0x0C */
4652                 tempcx += tempbx;
4653
4654                 if (pVBInfo->VBInfo & SetCRT2ToRAMDAC) {
4655                         tempbx = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[4];
4656                         tempbx |= ((pVBInfo->
4657                                         XGINEWUB_CRT1Table[CRT1Index].CR[14] &
4658                                                 0xC0) << 2);
4659                         tempbx = (tempbx - 3) << 3; /* (VGAHRS-3)*8 */
4660                         tempcx = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[5];
4661                         tempcx &= 0x1F;
4662                         temp = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[15];
4663                         temp = (temp & 0x04) << (5 - 2); /* VGAHRE D[5] */
4664                         tempcx = ((tempcx | temp) - 3) << 3; /* (VGAHRE-3)*8 */
4665                 }
4666
4667                 tempbx += 4;
4668                 tempcx += 4;
4669
4670                 if (tempcx > (pVBInfo->VGAHT / 2))
4671                         tempcx = pVBInfo->VGAHT / 2;
4672
4673                 temp = tempbx & 0x00FF;
4674
4675                 xgifb_reg_set(pVBInfo->Part1Port, 0x0B, temp);
4676         } else {
4677                 temp = (pVBInfo->VGAHT - 1) & 0x0FF; /* BTVGA2HT 0x08,0x09 */
4678                 xgifb_reg_set(pVBInfo->Part1Port, 0x08, temp);
4679                 temp = (((pVBInfo->VGAHT - 1) & 0xFF00) >> 8) << 4;
4680                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x09, ~0x0F0, temp);
4681                 /* BTVGA2HDEE 0x0A,0x0C */
4682                 temp = (pVBInfo->VGAHDE + 16) & 0x0FF;
4683                 xgifb_reg_set(pVBInfo->Part1Port, 0x0A, temp);
4684                 tempcx = (pVBInfo->VGAHT - pVBInfo->VGAHDE) >> 2; /* cx */
4685                 pushbx = pVBInfo->VGAHDE + 16;
4686                 tempcx = tempcx >> 1;
4687                 tempbx = pushbx + tempcx; /* bx BTVGA@HRS 0x0B,0x0C */
4688                 tempcx += tempbx;
4689
4690                 if (pVBInfo->VBInfo & SetCRT2ToRAMDAC) {
4691                         tempbx = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[3];
4692                         tempbx |= ((pVBInfo->
4693                                         XGINEWUB_CRT1Table[CRT1Index].CR[5] &
4694                                                 0xC0) << 2);
4695                         tempbx = (tempbx - 3) << 3; /* (VGAHRS-3)*8 */
4696                         tempcx = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[4];
4697                         tempcx &= 0x1F;
4698                         temp = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[6];
4699                         temp = (temp & 0x04) << (5 - 2); /* VGAHRE D[5] */
4700                         tempcx = ((tempcx | temp) - 3) << 3; /* (VGAHRE-3)*8 */
4701                         tempbx += 16;
4702                         tempcx += 16;
4703                 }
4704
4705                 if (tempcx > pVBInfo->VGAHT)
4706                         tempcx = pVBInfo->VGAHT;
4707
4708                 temp = tempbx & 0x00FF;
4709                 xgifb_reg_set(pVBInfo->Part1Port, 0x0B, temp);
4710         }
4711
4712         tempax = (tempax & 0x00FF) | (tempbx & 0xFF00);
4713         tempbx = pushbx;
4714         tempbx = (tempbx & 0x00FF) | ((tempbx & 0xFF00) << 4);
4715         tempax |= (tempbx & 0xFF00);
4716         temp = (tempax & 0xFF00) >> 8;
4717         xgifb_reg_set(pVBInfo->Part1Port, 0x0C, temp);
4718         temp = tempcx & 0x00FF;
4719         xgifb_reg_set(pVBInfo->Part1Port, 0x0D, temp);
4720         tempcx = (pVBInfo->VGAVT - 1);
4721         temp = tempcx & 0x00FF;
4722
4723         xgifb_reg_set(pVBInfo->Part1Port, 0x0E, temp);
4724         tempbx = pVBInfo->VGAVDE - 1;
4725         temp = tempbx & 0x00FF;
4726         xgifb_reg_set(pVBInfo->Part1Port, 0x0F, temp);
4727         temp = ((tempbx & 0xFF00) << 3) >> 8;
4728         temp |= ((tempcx & 0xFF00) >> 8);
4729         xgifb_reg_set(pVBInfo->Part1Port, 0x12, temp);
4730
4731         tempax = pVBInfo->VGAVDE;
4732         tempbx = pVBInfo->VGAVDE;
4733         tempcx = pVBInfo->VGAVT;
4734         /* BTVGA2VRS 0x10,0x11 */
4735         tempbx = (pVBInfo->VGAVT + pVBInfo->VGAVDE) >> 1;
4736         /* BTVGA2VRE 0x11 */
4737         tempcx = ((pVBInfo->VGAVT - pVBInfo->VGAVDE) >> 4) + tempbx + 1;
4738
4739         if (pVBInfo->VBInfo & SetCRT2ToRAMDAC) {
4740                 tempbx = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[10];
4741                 temp = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[9];
4742
4743                 if (temp & 0x04)
4744                         tempbx |= 0x0100;
4745
4746                 if (temp & 0x080)
4747                         tempbx |= 0x0200;
4748
4749                 temp = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[14];
4750
4751                 if (temp & 0x08)
4752                         tempbx |= 0x0400;
4753
4754                 temp = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[11];
4755                 tempcx = (tempcx & 0xFF00) | (temp & 0x00FF);
4756         }
4757
4758         temp = tempbx & 0x00FF;
4759         xgifb_reg_set(pVBInfo->Part1Port, 0x10, temp);
4760         temp = ((tempbx & 0xFF00) >> 8) << 4;
4761         temp = ((tempcx & 0x000F) | (temp));
4762         xgifb_reg_set(pVBInfo->Part1Port, 0x11, temp);
4763         tempax = 0;
4764
4765         if (modeflag & DoubleScanMode)
4766                 tempax |= 0x80;
4767
4768         if (modeflag & HalfDCLK)
4769                 tempax |= 0x40;
4770
4771         xgifb_reg_and_or(pVBInfo->Part1Port, 0x2C, ~0x0C0, tempax);
4772 }
4773
4774 static unsigned short XGI_GetVGAHT2(struct vb_device_info *pVBInfo)
4775 {
4776         unsigned long tempax, tempbx;
4777
4778         tempbx = ((pVBInfo->VGAVT - pVBInfo->VGAVDE) * pVBInfo->RVBHCMAX)
4779                         & 0xFFFF;
4780         tempax = (pVBInfo->VT - pVBInfo->VDE) * pVBInfo->RVBHCFACT;
4781         tempax = (tempax * pVBInfo->HT) / tempbx;
4782
4783         return (unsigned short) tempax;
4784 }
4785
4786 static void XGI_SetLockRegs(unsigned short ModeNo, unsigned short ModeIdIndex,
4787                 struct xgi_hw_device_info *HwDeviceExtension,
4788                 unsigned short RefreshRateTableIndex,
4789                 struct vb_device_info *pVBInfo)
4790 {
4791         unsigned short push1, push2, tempax, tempbx = 0, tempcx, temp, resinfo,
4792                         modeflag, CRT1Index;
4793
4794         if (ModeNo <= 0x13) {
4795                 /* si+St_ResInfo */
4796                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
4797                 resinfo = pVBInfo->SModeIDTable[ModeIdIndex].St_ResInfo;
4798         } else {
4799                 /* si+Ext_ResInfo */
4800                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
4801                 resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
4802                 CRT1Index = pVBInfo->
4803                                 RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
4804                 CRT1Index &= IndexMask;
4805         }
4806
4807         if (!(pVBInfo->VBInfo & SetInSlaveMode))
4808                 return;
4809
4810         temp = 0xFF; /* set MAX HT */
4811         xgifb_reg_set(pVBInfo->Part1Port, 0x03, temp);
4812         /* if (modeflag & Charx8Dot) */
4813         /*      tempcx = 0x08; */
4814         /* else */
4815         tempcx = 0x08;
4816
4817         if (pVBInfo->VBType & (VB_XGI301LV | VB_XGI302LV | VB_XGI301C))
4818                 modeflag |= Charx8Dot;
4819
4820         tempax = pVBInfo->VGAHDE; /* 0x04 Horizontal Display End */
4821
4822         if (modeflag & HalfDCLK)
4823                 tempax = tempax >> 1;
4824
4825         tempax = (tempax / tempcx) - 1;
4826         tempbx |= ((tempax & 0x00FF) << 8);
4827         temp = tempax & 0x00FF;
4828         xgifb_reg_set(pVBInfo->Part1Port, 0x04, temp);
4829
4830         temp = (tempbx & 0xFF00) >> 8;
4831
4832         if (pVBInfo->VBInfo & SetCRT2ToTV) {
4833                 if (!(pVBInfo->VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV
4834                                 | VB_XGI302LV | VB_XGI301C)))
4835                         temp += 2;
4836
4837                 if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {
4838                         if (pVBInfo->VBType & VB_XGI301LV) {
4839                                 if (pVBInfo->VBExtInfo == VB_YPbPr1080i) {
4840                                         if (resinfo == 7)
4841                                                 temp -= 2;
4842                                 }
4843                         } else if (resinfo == 7) {
4844                                 temp -= 2;
4845                         }
4846                 }
4847         }
4848
4849         /* 0x05 Horizontal Display Start */
4850         xgifb_reg_set(pVBInfo->Part1Port, 0x05, temp);
4851         /* 0x06 Horizontal Blank end */
4852         xgifb_reg_set(pVBInfo->Part1Port, 0x06, 0x03);
4853
4854         if (!(pVBInfo->VBInfo & DisableCRT2Display)) { /* 030226 bainy */
4855                 if (pVBInfo->VBInfo & SetCRT2ToTV)
4856                         tempax = pVBInfo->VGAHT;
4857                 else
4858                         tempax = XGI_GetVGAHT2(pVBInfo);
4859         }
4860
4861         if (tempax >= pVBInfo->VGAHT)
4862                 tempax = pVBInfo->VGAHT;
4863
4864         if (modeflag & HalfDCLK)
4865                 tempax = tempax >> 1;
4866
4867         tempax = (tempax / tempcx) - 5;
4868         tempcx = tempax; /* 20030401 0x07 horizontal Retrace Start */
4869         if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {
4870                 temp = (tempbx & 0x00FF) - 1;
4871                 if (!(modeflag & HalfDCLK)) {
4872                         temp -= 6;
4873                         if (pVBInfo->TVInfo & TVSimuMode) {
4874                                 temp -= 4;
4875                                 if (ModeNo > 0x13)
4876                                         temp -= 10;
4877                         }
4878                 }
4879         } else {
4880                 /* tempcx = tempbx & 0x00FF ; */
4881                 tempbx = (tempbx & 0xFF00) >> 8;
4882                 tempcx = (tempcx + tempbx) >> 1;
4883                 temp = (tempcx & 0x00FF) + 2;
4884
4885                 if (pVBInfo->VBInfo & SetCRT2ToTV) {
4886                         temp -= 1;
4887                         if (!(modeflag & HalfDCLK)) {
4888                                 if ((modeflag & Charx8Dot)) {
4889                                         temp += 4;
4890                                         if (pVBInfo->VGAHDE >= 800)
4891                                                 temp -= 6;
4892                                 }
4893                         }
4894                 } else {
4895                         if (!(modeflag & HalfDCLK)) {
4896                                 temp -= 4;
4897                                 if (pVBInfo->LCDResInfo != Panel1280x960) {
4898                                         if (pVBInfo->VGAHDE >= 800) {
4899                                                 temp -= 7;
4900                                                 if (pVBInfo->ModeType ==
4901                                                         ModeEGA) {
4902                                                         if (pVBInfo->VGAVDE ==
4903                                                             1024) {
4904                                                                 temp += 15;
4905                                                                 if (pVBInfo->LCDResInfo != Panel1280x1024) {
4906                                                                         temp +=
4907                                                                             7;
4908                                                                 }
4909                                                         }
4910                                                 }
4911
4912                                                 if (pVBInfo->VGAHDE >= 1280) {
4913                                                         if (pVBInfo->LCDResInfo
4914                                                                         != Panel1280x960) {
4915                                                                 if (pVBInfo->LCDInfo
4916                                                                                 & LCDNonExpanding) {
4917                                                                         temp
4918                                                                                         += 28;
4919                                                                 }
4920                                                         }
4921                                                 }
4922                                         }
4923                                 }
4924                         }
4925                 }
4926         }
4927
4928         /* 0x07 Horizontal Retrace Start */
4929         xgifb_reg_set(pVBInfo->Part1Port, 0x07, temp);
4930         /* 0x08 Horizontal Retrace End */
4931         xgifb_reg_set(pVBInfo->Part1Port, 0x08, 0);
4932
4933         if (pVBInfo->VBInfo & SetCRT2ToTV) {
4934                 if (pVBInfo->TVInfo & TVSimuMode) {
4935                         if ((ModeNo == 0x06) || (ModeNo == 0x10) || (ModeNo
4936                                         == 0x11) || (ModeNo == 0x13) || (ModeNo
4937                                         == 0x0F)) {
4938                                 xgifb_reg_set(pVBInfo->Part1Port, 0x07, 0x5b);
4939                                 xgifb_reg_set(pVBInfo->Part1Port, 0x08, 0x03);
4940                         }
4941
4942                         if ((ModeNo == 0x00) || (ModeNo == 0x01)) {
4943                                 if (pVBInfo->TVInfo & SetNTSCTV) {
4944                                         xgifb_reg_set(pVBInfo->Part1Port,
4945                                                         0x07, 0x2A);
4946                                         xgifb_reg_set(pVBInfo->Part1Port,
4947                                                         0x08, 0x61);
4948                                 } else {
4949                                         xgifb_reg_set(pVBInfo->Part1Port,
4950                                                         0x07, 0x2A);
4951                                         xgifb_reg_set(pVBInfo->Part1Port,
4952                                                         0x08, 0x41);
4953                                         xgifb_reg_set(pVBInfo->Part1Port,
4954                                                         0x0C, 0xF0);
4955                                 }
4956                         }
4957
4958                         if ((ModeNo == 0x02) || (ModeNo == 0x03) || (ModeNo
4959                                         == 0x07)) {
4960                                 if (pVBInfo->TVInfo & SetNTSCTV) {
4961                                         xgifb_reg_set(pVBInfo->Part1Port,
4962                                                         0x07, 0x54);
4963                                         xgifb_reg_set(pVBInfo->Part1Port,
4964                                                         0x08, 0x00);
4965                                 } else {
4966                                         xgifb_reg_set(pVBInfo->Part1Port,
4967                                                         0x07, 0x55);
4968                                         xgifb_reg_set(pVBInfo->Part1Port,
4969                                                         0x08, 0x00);
4970                                         xgifb_reg_set(pVBInfo->Part1Port,
4971                                                         0x0C, 0xF0);
4972                                 }
4973                         }
4974
4975                         if ((ModeNo == 0x04) || (ModeNo == 0x05) || (ModeNo
4976                                         == 0x0D) || (ModeNo == 0x50)) {
4977                                 if (pVBInfo->TVInfo & SetNTSCTV) {
4978                                         xgifb_reg_set(pVBInfo->Part1Port,
4979                                                         0x07, 0x30);
4980                                         xgifb_reg_set(pVBInfo->Part1Port,
4981                                                         0x08, 0x03);
4982                                 } else {
4983                                         xgifb_reg_set(pVBInfo->Part1Port,
4984                                                         0x07, 0x2f);
4985                                         xgifb_reg_set(pVBInfo->Part1Port,
4986                                                         0x08, 0x02);
4987                                 }
4988                         }
4989                 }
4990         }
4991
4992         xgifb_reg_set(pVBInfo->Part1Port, 0x18, 0x03); /* 0x18 SR0B */
4993         xgifb_reg_and_or(pVBInfo->Part1Port, 0x19, 0xF0, 0x00);
4994         xgifb_reg_set(pVBInfo->Part1Port, 0x09, 0xFF); /* 0x09 Set Max VT */
4995
4996         tempbx = pVBInfo->VGAVT;
4997         push1 = tempbx;
4998         tempcx = 0x121;
4999         tempbx = pVBInfo->VGAVDE; /* 0x0E Virtical Display End */
5000
5001         if (tempbx == 357)
5002                 tempbx = 350;
5003         if (tempbx == 360)
5004                 tempbx = 350;
5005         if (tempbx == 375)
5006                 tempbx = 350;
5007         if (tempbx == 405)
5008                 tempbx = 400;
5009         if (tempbx == 525)
5010                 tempbx = 480;
5011
5012         push2 = tempbx;
5013
5014         if (pVBInfo->VBInfo & SetCRT2ToLCD) {
5015                 if (pVBInfo->LCDResInfo == Panel1024x768) {
5016                         if (!(pVBInfo->LCDInfo & LCDVESATiming)) {
5017                                 if (tempbx == 350)
5018                                         tempbx += 5;
5019                                 if (tempbx == 480)
5020                                         tempbx += 5;
5021                         }
5022                 }
5023         }
5024         tempbx--;
5025         temp = tempbx & 0x00FF;
5026         tempbx--;
5027         temp = tempbx & 0x00FF;
5028         /* 0x10 vertical Blank Start */
5029         xgifb_reg_set(pVBInfo->Part1Port, 0x10, temp);
5030         tempbx = push2;
5031         tempbx--;
5032         temp = tempbx & 0x00FF;
5033         xgifb_reg_set(pVBInfo->Part1Port, 0x0E, temp);
5034
5035         if (tempbx & 0x0100)
5036                 tempcx |= 0x0002;
5037
5038         tempax = 0x000B;
5039
5040         if (modeflag & DoubleScanMode)
5041                 tempax |= 0x08000;
5042
5043         if (tempbx & 0x0200)
5044                 tempcx |= 0x0040;
5045
5046         temp = (tempax & 0xFF00) >> 8;
5047         xgifb_reg_set(pVBInfo->Part1Port, 0x0B, temp);
5048
5049         if (tempbx & 0x0400)
5050                 tempcx |= 0x0600;
5051
5052         /* 0x11 Vertival Blank End */
5053         xgifb_reg_set(pVBInfo->Part1Port, 0x11, 0x00);
5054
5055         tempax = push1;
5056         tempax -= tempbx; /* 0x0C Vertical Retrace Start */
5057         tempax = tempax >> 2;
5058         push1 = tempax; /* push ax */
5059
5060         if (resinfo != 0x09) {
5061                 tempax = tempax << 1;
5062                 tempbx += tempax;
5063         }
5064
5065         if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {
5066                 if (pVBInfo->VBType & VB_XGI301LV) {
5067                         if (pVBInfo->TVInfo & SetYPbPrMode1080i) {
5068                                 tempbx -= 10;
5069                         } else {
5070                                 if (pVBInfo->TVInfo & TVSimuMode) {
5071                                         if (pVBInfo->TVInfo & SetPALTV) {
5072                                                 if (pVBInfo->VBType &
5073                                                     VB_XGI301LV) {
5074                                                         if (!(pVBInfo->TVInfo &
5075                                                             (SetYPbPrMode525p |
5076                                                             SetYPbPrMode750p |
5077                                                             SetYPbPrMode1080i)))
5078                                                                 tempbx += 40;
5079                                                 } else {
5080                                                         tempbx += 40;
5081                                                 }
5082                                         }
5083                                 }
5084                         }
5085                 } else {
5086                         tempbx -= 10;
5087                 }
5088         } else {
5089                 if (pVBInfo->TVInfo & TVSimuMode) {
5090                         if (pVBInfo->TVInfo & SetPALTV) {
5091                                 if (pVBInfo->VBType & VB_XGI301LV) {
5092                                         if (!(pVBInfo->TVInfo &
5093                                             (SetYPbPrMode525p |
5094                                              SetYPbPrMode750p |
5095                                              SetYPbPrMode1080i)))
5096                                                 tempbx += 40;
5097                                 } else {
5098                                         tempbx += 40;
5099                                 }
5100                         }
5101                 }
5102         }
5103         tempax = push1;
5104         tempax = tempax >> 2;
5105         tempax++;
5106         tempax += tempbx;
5107         push1 = tempax; /* push ax */
5108
5109         if ((pVBInfo->TVInfo & SetPALTV)) {
5110                 if (tempbx <= 513) {
5111                         if (tempax >= 513)
5112                                 tempbx = 513;
5113                 }
5114         }
5115
5116         temp = tempbx & 0x00FF;
5117         xgifb_reg_set(pVBInfo->Part1Port, 0x0C, temp);
5118         tempbx--;
5119         temp = tempbx & 0x00FF;
5120         xgifb_reg_set(pVBInfo->Part1Port, 0x10, temp);
5121
5122         if (tempbx & 0x0100)
5123                 tempcx |= 0x0008;
5124
5125         if (tempbx & 0x0200)
5126                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x0B, 0x0FF, 0x20);
5127
5128         tempbx++;
5129
5130         if (tempbx & 0x0100)
5131                 tempcx |= 0x0004;
5132
5133         if (tempbx & 0x0200)
5134                 tempcx |= 0x0080;
5135
5136         if (tempbx & 0x0400)
5137                 tempcx |= 0x0C00;
5138
5139         tempbx = push1; /* pop ax */
5140         temp = tempbx & 0x00FF;
5141         temp &= 0x0F;
5142         /* 0x0D vertical Retrace End */
5143         xgifb_reg_set(pVBInfo->Part1Port, 0x0D, temp);
5144
5145         if (tempbx & 0x0010)
5146                 tempcx |= 0x2000;
5147
5148         temp = tempcx & 0x00FF;
5149         xgifb_reg_set(pVBInfo->Part1Port, 0x0A, temp); /* 0x0A CR07 */
5150         temp = (tempcx & 0x0FF00) >> 8;
5151         xgifb_reg_set(pVBInfo->Part1Port, 0x17, temp); /* 0x17 SR0A */
5152         tempax = modeflag;
5153         temp = (tempax & 0xFF00) >> 8;
5154
5155         temp = (temp >> 1) & 0x09;
5156
5157         if (pVBInfo->VBType & (VB_XGI301LV | VB_XGI302LV | VB_XGI301C))
5158                 temp |= 0x01;
5159
5160         xgifb_reg_set(pVBInfo->Part1Port, 0x16, temp); /* 0x16 SR01 */
5161         xgifb_reg_set(pVBInfo->Part1Port, 0x0F, 0); /* 0x0F CR14 */
5162         xgifb_reg_set(pVBInfo->Part1Port, 0x12, 0); /* 0x12 CR17 */
5163
5164         if (pVBInfo->LCDInfo & LCDRGB18Bit)
5165                 temp = 0x80;
5166         else
5167                 temp = 0x00;
5168
5169         xgifb_reg_set(pVBInfo->Part1Port, 0x1A, temp); /* 0x1A SR0E */
5170
5171         return;
5172 }
5173
5174 static void XGI_SetGroup2(unsigned short ModeNo, unsigned short ModeIdIndex,
5175                 unsigned short RefreshRateTableIndex,
5176                 struct xgi_hw_device_info *HwDeviceExtension,
5177                 struct vb_device_info *pVBInfo)
5178 {
5179         unsigned short i, j, tempax, tempbx, tempcx, temp, push1, push2,
5180                         modeflag, resinfo, crt2crtc;
5181         unsigned char *TimingPoint;
5182
5183         unsigned long longtemp, tempeax, tempebx, temp2, tempecx;
5184
5185         if (ModeNo <= 0x13) {
5186                 /* si+St_ResInfo */
5187                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
5188                 resinfo = pVBInfo->SModeIDTable[ModeIdIndex].St_ResInfo;
5189                 crt2crtc = pVBInfo->SModeIDTable[ModeIdIndex].St_CRT2CRTC;
5190         } else {
5191                 /* si+Ext_ResInfo */
5192                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
5193                 resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
5194                 crt2crtc = pVBInfo->RefIndex[RefreshRateTableIndex].
5195                                 Ext_CRT2CRTC;
5196         }
5197
5198         tempax = 0;
5199
5200         if (!(pVBInfo->VBInfo & SetCRT2ToAVIDEO))
5201                 tempax |= 0x0800;
5202
5203         if (!(pVBInfo->VBInfo & SetCRT2ToSVIDEO))
5204                 tempax |= 0x0400;
5205
5206         if (pVBInfo->VBInfo & SetCRT2ToSCART)
5207                 tempax |= 0x0200;
5208
5209         if (!(pVBInfo->TVInfo & SetPALTV))
5210                 tempax |= 0x1000;
5211
5212         if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV)
5213                 tempax |= 0x0100;
5214
5215         if (pVBInfo->TVInfo & (SetYPbPrMode525p | SetYPbPrMode750p))
5216                 tempax &= 0xfe00;
5217
5218         tempax = (tempax & 0xff00) >> 8;
5219
5220         xgifb_reg_set(pVBInfo->Part2Port, 0x0, tempax);
5221         TimingPoint = pVBInfo->NTSCTiming;
5222
5223         if (pVBInfo->TVInfo & SetPALTV)
5224                 TimingPoint = pVBInfo->PALTiming;
5225
5226         if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {
5227                 TimingPoint = pVBInfo->HiTVExtTiming;
5228
5229                 if (pVBInfo->VBInfo & SetInSlaveMode)
5230                         TimingPoint = pVBInfo->HiTVSt2Timing;
5231
5232                 if (pVBInfo->SetFlag & TVSimuMode)
5233                         TimingPoint = pVBInfo->HiTVSt1Timing;
5234
5235                 if (!(modeflag & Charx8Dot))
5236                         TimingPoint = pVBInfo->HiTVTextTiming;
5237         }
5238
5239         if (pVBInfo->VBInfo & SetCRT2ToYPbPr) {
5240                 if (pVBInfo->TVInfo & SetYPbPrMode525i)
5241                         TimingPoint = pVBInfo->YPbPr525iTiming;
5242
5243                 if (pVBInfo->TVInfo & SetYPbPrMode525p)
5244                         TimingPoint = pVBInfo->YPbPr525pTiming;
5245
5246                 if (pVBInfo->TVInfo & SetYPbPrMode750p)
5247                         TimingPoint = pVBInfo->YPbPr750pTiming;
5248         }
5249
5250         for (i = 0x01, j = 0; i <= 0x2D; i++, j++)
5251                 xgifb_reg_set(pVBInfo->Part2Port, i, TimingPoint[j]);
5252
5253         for (i = 0x39; i <= 0x45; i++, j++)
5254                 /* di->temp2[j] */
5255                 xgifb_reg_set(pVBInfo->Part2Port, i, TimingPoint[j]);
5256
5257         if (pVBInfo->VBInfo & SetCRT2ToTV)
5258                 xgifb_reg_and_or(pVBInfo->Part2Port, 0x3A, 0x1F, 0x00);
5259
5260         temp = pVBInfo->NewFlickerMode;
5261         temp &= 0x80;
5262         xgifb_reg_and_or(pVBInfo->Part2Port, 0x0A, 0xFF, temp);
5263
5264         if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV)
5265                 tempax = 950;
5266
5267         if (pVBInfo->TVInfo & SetPALTV)
5268                 tempax = 520;
5269         else
5270                 tempax = 440;
5271
5272         if (pVBInfo->VDE <= tempax) {
5273                 tempax -= pVBInfo->VDE;
5274                 tempax = tempax >> 2;
5275                 tempax = (tempax & 0x00FF) | ((tempax & 0x00FF) << 8);
5276                 push1 = tempax;
5277                 temp = (tempax & 0xFF00) >> 8;
5278                 temp += (unsigned short) TimingPoint[0];
5279
5280                 if (pVBInfo->VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV
5281                                 | VB_XGI302LV | VB_XGI301C)) {
5282                         if (pVBInfo->VBInfo & (SetCRT2ToAVIDEO
5283                                         | SetCRT2ToSVIDEO | SetCRT2ToSCART
5284                                         | SetCRT2ToYPbPr)) {
5285                                 tempcx = pVBInfo->VGAHDE;
5286                                 if (tempcx >= 1024) {
5287                                         temp = 0x17; /* NTSC */
5288                                         if (pVBInfo->TVInfo & SetPALTV)
5289                                                 temp = 0x19; /* PAL */
5290                                 }
5291                         }
5292                 }
5293
5294                 xgifb_reg_set(pVBInfo->Part2Port, 0x01, temp);
5295                 tempax = push1;
5296                 temp = (tempax & 0xFF00) >> 8;
5297                 temp += TimingPoint[1];
5298
5299                 if (pVBInfo->VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV
5300                                 | VB_XGI302LV | VB_XGI301C)) {
5301                         if ((pVBInfo->VBInfo & (SetCRT2ToAVIDEO
5302                                         | SetCRT2ToSVIDEO | SetCRT2ToSCART
5303                                         | SetCRT2ToYPbPr))) {
5304                                 tempcx = pVBInfo->VGAHDE;
5305                                 if (tempcx >= 1024) {
5306                                         temp = 0x1D; /* NTSC */
5307                                         if (pVBInfo->TVInfo & SetPALTV)
5308                                                 temp = 0x52; /* PAL */
5309                                 }
5310                         }
5311                 }
5312                 xgifb_reg_set(pVBInfo->Part2Port, 0x02, temp);
5313         }
5314
5315         /* 301b */
5316         tempcx = pVBInfo->HT;
5317
5318         if (XGI_IsLCDDualLink(pVBInfo))
5319                 tempcx = tempcx >> 1;
5320
5321         tempcx -= 2;
5322         temp = tempcx & 0x00FF;
5323         xgifb_reg_set(pVBInfo->Part2Port, 0x1B, temp);
5324
5325         temp = (tempcx & 0xFF00) >> 8;
5326         xgifb_reg_and_or(pVBInfo->Part2Port, 0x1D, ~0x0F, temp);
5327
5328         tempcx = pVBInfo->HT >> 1;
5329         push1 = tempcx; /* push cx */
5330         tempcx += 7;
5331
5332         if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV)
5333                 tempcx -= 4;
5334
5335         temp = tempcx & 0x00FF;
5336         temp = temp << 4;
5337         xgifb_reg_and_or(pVBInfo->Part2Port, 0x22, 0x0F, temp);
5338
5339         tempbx = TimingPoint[j] | ((TimingPoint[j + 1]) << 8);
5340         tempbx += tempcx;
5341         push2 = tempbx;
5342         temp = tempbx & 0x00FF;
5343         xgifb_reg_set(pVBInfo->Part2Port, 0x24, temp);
5344         temp = (tempbx & 0xFF00) >> 8;
5345         temp = temp << 4;
5346         xgifb_reg_and_or(pVBInfo->Part2Port, 0x25, 0x0F, temp);
5347
5348         tempbx = push2;
5349         tempbx = tempbx + 8;
5350         if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {
5351                 tempbx = tempbx - 4;
5352                 tempcx = tempbx;
5353         }
5354
5355         temp = (tempbx & 0x00FF) << 4;
5356         xgifb_reg_and_or(pVBInfo->Part2Port, 0x29, 0x0F, temp);
5357
5358         j += 2;
5359         tempcx += (TimingPoint[j] | ((TimingPoint[j + 1]) << 8));
5360         temp = tempcx & 0x00FF;
5361         xgifb_reg_set(pVBInfo->Part2Port, 0x27, temp);
5362         temp = ((tempcx & 0xFF00) >> 8) << 4;
5363         xgifb_reg_and_or(pVBInfo->Part2Port, 0x28, 0x0F, temp);
5364
5365         tempcx += 8;
5366         if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV)
5367                 tempcx -= 4;
5368
5369         temp = tempcx & 0xFF;
5370         temp = temp << 4;
5371         xgifb_reg_and_or(pVBInfo->Part2Port, 0x2A, 0x0F, temp);
5372
5373         tempcx = push1; /* pop cx */
5374         j += 2;
5375         temp = TimingPoint[j] | ((TimingPoint[j + 1]) << 8);
5376         tempcx -= temp;
5377         temp = tempcx & 0x00FF;
5378         temp = temp << 4;
5379         xgifb_reg_and_or(pVBInfo->Part2Port, 0x2D, 0x0F, temp);
5380
5381         tempcx -= 11;
5382
5383         if (!(pVBInfo->VBInfo & SetCRT2ToTV)) {
5384                 tempax = XGI_GetVGAHT2(pVBInfo);
5385                 tempcx = tempax - 1;
5386         }
5387         temp = tempcx & 0x00FF;
5388         xgifb_reg_set(pVBInfo->Part2Port, 0x2E, temp);
5389
5390         tempbx = pVBInfo->VDE;
5391
5392         if (pVBInfo->VGAVDE == 360)
5393                 tempbx = 746;
5394         if (pVBInfo->VGAVDE == 375)
5395                 tempbx = 746;
5396         if (pVBInfo->VGAVDE == 405)
5397                 tempbx = 853;
5398
5399         if (pVBInfo->VBInfo & SetCRT2ToTV) {
5400                 if (pVBInfo->VBType &
5401                     (VB_XGI301LV | VB_XGI302LV | VB_XGI301C)) {
5402                         if (!(pVBInfo->TVInfo &
5403                             (SetYPbPrMode525p | SetYPbPrMode750p)))
5404                                 tempbx = tempbx >> 1;
5405                 } else
5406                         tempbx = tempbx >> 1;
5407         }
5408
5409         tempbx -= 2;
5410         temp = tempbx & 0x00FF;
5411
5412         if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {
5413                 if (pVBInfo->VBType & VB_XGI301LV) {
5414                         if (pVBInfo->TVInfo & SetYPbPrMode1080i) {
5415                                 if (pVBInfo->VBInfo & SetInSlaveMode) {
5416                                         if (ModeNo == 0x2f)
5417                                                 temp += 1;
5418                                 }
5419                         }
5420                 } else {
5421                         if (pVBInfo->VBInfo & SetInSlaveMode) {
5422                                 if (ModeNo == 0x2f)
5423                                         temp += 1;
5424                         }
5425                 }
5426         }
5427
5428         xgifb_reg_set(pVBInfo->Part2Port, 0x2F, temp);
5429
5430         temp = (tempcx & 0xFF00) >> 8;
5431         temp |= ((tempbx & 0xFF00) >> 8) << 6;
5432
5433         if (!(pVBInfo->VBInfo & SetCRT2ToHiVisionTV)) {
5434                 if (pVBInfo->VBType & VB_XGI301LV) {
5435                         if (pVBInfo->TVInfo & SetYPbPrMode1080i) {
5436                                 temp |= 0x10;
5437
5438                                 if (!(pVBInfo->VBInfo & SetCRT2ToSVIDEO))
5439                                         temp |= 0x20;
5440                         }
5441                 } else {
5442                         temp |= 0x10;
5443                         if (!(pVBInfo->VBInfo & SetCRT2ToSVIDEO))
5444                                 temp |= 0x20;
5445                 }
5446         }
5447
5448         xgifb_reg_set(pVBInfo->Part2Port, 0x30, temp);
5449
5450         if (pVBInfo->VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV
5451                         | VB_XGI302LV | VB_XGI301C)) { /* TV gatingno */
5452                 tempbx = pVBInfo->VDE;
5453                 tempcx = tempbx - 2;
5454
5455                 if (pVBInfo->VBInfo & SetCRT2ToTV) {
5456                         if (!(pVBInfo->TVInfo & (SetYPbPrMode525p
5457                                         | SetYPbPrMode750p)))
5458                                 tempbx = tempbx >> 1;
5459                 }
5460
5461                 if (pVBInfo->VBType & (VB_XGI302LV | VB_XGI301C)) {
5462                         temp = 0;
5463                         if (tempcx & 0x0400)
5464                                 temp |= 0x20;
5465
5466                         if (tempbx & 0x0400)
5467                                 temp |= 0x40;
5468
5469                         xgifb_reg_set(pVBInfo->Part4Port, 0x10, temp);
5470                 }
5471
5472                 temp = (((tempbx - 3) & 0x0300) >> 8) << 5;
5473                 xgifb_reg_set(pVBInfo->Part2Port, 0x46, temp);
5474                 temp = (tempbx - 3) & 0x00FF;
5475                 xgifb_reg_set(pVBInfo->Part2Port, 0x47, temp);
5476         }
5477
5478         tempbx = tempbx & 0x00FF;
5479
5480         if (!(modeflag & HalfDCLK)) {
5481                 tempcx = pVBInfo->VGAHDE;
5482                 if (tempcx >= pVBInfo->HDE) {
5483                         tempbx |= 0x2000;
5484                         tempax &= 0x00FF;
5485                 }
5486         }
5487
5488         tempcx = 0x0101;
5489
5490         if (pVBInfo->VBInfo & SetCRT2ToTV) { /*301b*/
5491                 if (pVBInfo->VGAHDE >= 1024) {
5492                         tempcx = 0x1920;
5493                         if (pVBInfo->VGAHDE >= 1280) {
5494                                 tempcx = 0x1420;
5495                                 tempbx = tempbx & 0xDFFF;
5496                         }
5497                 }
5498         }
5499
5500         if (!(tempbx & 0x2000)) {
5501                 if (modeflag & HalfDCLK)
5502                         tempcx = (tempcx & 0xFF00) | ((tempcx & 0x00FF) << 1);
5503
5504                 push1 = tempbx;
5505                 tempeax = pVBInfo->VGAHDE;
5506                 tempebx = (tempcx & 0xFF00) >> 8;
5507                 longtemp = tempeax * tempebx;
5508                 tempecx = tempcx & 0x00FF;
5509                 longtemp = longtemp / tempecx;
5510
5511                 /* 301b */
5512                 tempecx = 8 * 1024;
5513
5514                 if (pVBInfo->VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV
5515                                 | VB_XGI302LV | VB_XGI301C)) {
5516                         tempecx = tempecx * 8;
5517                 }
5518
5519                 longtemp = longtemp * tempecx;
5520                 tempecx = pVBInfo->HDE;
5521                 temp2 = longtemp % tempecx;
5522                 tempeax = longtemp / tempecx;
5523                 if (temp2 != 0)
5524                         tempeax += 1;
5525
5526                 tempax = (unsigned short) tempeax;
5527
5528                 /* 301b */
5529                 if (pVBInfo->VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV
5530                                 | VB_XGI302LV | VB_XGI301C)) {
5531                         tempcx = ((tempax & 0xFF00) >> 5) >> 8;
5532                 }
5533                 /* end 301b */
5534
5535                 tempbx = push1;
5536                 tempbx = (unsigned short) (((tempeax & 0x0000FF00) & 0x1F00)
5537                                 | (tempbx & 0x00FF));
5538                 tempax = (unsigned short) (((tempeax & 0x000000FF) << 8)
5539                                 | (tempax & 0x00FF));
5540                 temp = (tempax & 0xFF00) >> 8;
5541         } else {
5542                 temp = (tempax & 0x00FF) >> 8;
5543         }
5544
5545         xgifb_reg_set(pVBInfo->Part2Port, 0x44, temp);
5546         temp = (tempbx & 0xFF00) >> 8;
5547         xgifb_reg_and_or(pVBInfo->Part2Port, 0x45, ~0x03F, temp);
5548         temp = tempcx & 0x00FF;
5549
5550         if (tempbx & 0x2000)
5551                 temp = 0;
5552
5553         if (!(pVBInfo->VBInfo & SetCRT2ToLCD))
5554                 temp |= 0x18;
5555
5556         xgifb_reg_and_or(pVBInfo->Part2Port, 0x46, ~0x1F, temp);
5557         if (pVBInfo->TVInfo & SetPALTV) {
5558                 tempbx = 0x0382;
5559                 tempcx = 0x007e;
5560         } else {
5561                 tempbx = 0x0369;
5562                 tempcx = 0x0061;
5563         }
5564
5565         temp = tempbx & 0x00FF;
5566         xgifb_reg_set(pVBInfo->Part2Port, 0x4b, temp);
5567         temp = tempcx & 0x00FF;
5568         xgifb_reg_set(pVBInfo->Part2Port, 0x4c, temp);
5569
5570         temp = ((tempcx & 0xFF00) >> 8) & 0x03;
5571         temp = temp << 2;
5572         temp |= ((tempbx & 0xFF00) >> 8) & 0x03;
5573
5574         if (pVBInfo->VBInfo & SetCRT2ToYPbPr) {
5575                 temp |= 0x10;
5576
5577                 if (pVBInfo->TVInfo & SetYPbPrMode525p)
5578                         temp |= 0x20;
5579
5580                 if (pVBInfo->TVInfo & SetYPbPrMode750p)
5581                         temp |= 0x60;
5582         }
5583
5584         xgifb_reg_set(pVBInfo->Part2Port, 0x4d, temp);
5585         temp = xgifb_reg_get(pVBInfo->Part2Port, 0x43); /* 301b change */
5586         xgifb_reg_set(pVBInfo->Part2Port, 0x43, (unsigned short) (temp - 3));
5587
5588         if (!(pVBInfo->TVInfo & (SetYPbPrMode525p | SetYPbPrMode750p))) {
5589                 if (pVBInfo->TVInfo & NTSC1024x768) {
5590                         TimingPoint = XGI_NTSC1024AdjTime;
5591                         for (i = 0x1c, j = 0; i <= 0x30; i++, j++) {
5592                                 xgifb_reg_set(pVBInfo->Part2Port, i,
5593                                                 TimingPoint[j]);
5594                         }
5595                         xgifb_reg_set(pVBInfo->Part2Port, 0x43, 0x72);
5596                 }
5597         }
5598
5599         /* [ycchen] 01/14/03 Modify for 301C PALM Support */
5600         if (pVBInfo->VBType & VB_XGI301C) {
5601                 if (pVBInfo->TVInfo & SetPALMTV)
5602                         xgifb_reg_and_or(pVBInfo->Part2Port, 0x4E, ~0x08,
5603                                         0x08); /* PALM Mode */
5604         }
5605
5606         if (pVBInfo->TVInfo & SetPALMTV) {
5607                 tempax = (unsigned char) xgifb_reg_get(pVBInfo->Part2Port,
5608                                 0x01);
5609                 tempax--;
5610                 xgifb_reg_and(pVBInfo->Part2Port, 0x01, tempax);
5611
5612                 /* if ( !( pVBInfo->VBType & VB_XGI301C ) ) */
5613                 xgifb_reg_and(pVBInfo->Part2Port, 0x00, 0xEF);
5614         }
5615
5616         if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {
5617                 if (!(pVBInfo->VBInfo & SetInSlaveMode))
5618                         xgifb_reg_set(pVBInfo->Part2Port, 0x0B, 0x00);
5619         }
5620
5621         if (pVBInfo->VBInfo & SetCRT2ToTV)
5622                 return;
5623 }
5624
5625 static void XGI_SetLCDRegs(unsigned short ModeNo, unsigned short ModeIdIndex,
5626                 struct xgi_hw_device_info *HwDeviceExtension,
5627                 unsigned short RefreshRateTableIndex,
5628                 struct vb_device_info *pVBInfo)
5629 {
5630         unsigned short push1, push2, pushbx, tempax, tempbx, tempcx, temp,
5631                         tempah, tempbh, tempch, resinfo, modeflag, CRT1Index;
5632
5633         struct XGI_LCDDesStruct *LCDBDesPtr = NULL;
5634
5635         if (ModeNo <= 0x13) {
5636                 /* si+St_ResInfo */
5637                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
5638                 resinfo = pVBInfo->SModeIDTable[ModeIdIndex].St_ResInfo;
5639         } else {
5640                 /* si+Ext_ResInfo */
5641                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
5642                 resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
5643                 CRT1Index = pVBInfo->RefIndex[RefreshRateTableIndex].
5644                         Ext_CRT1CRTC;
5645                 CRT1Index &= IndexMask;
5646         }
5647
5648         if (!(pVBInfo->VBInfo & SetCRT2ToLCD))
5649                 return;
5650
5651         tempbx = pVBInfo->HDE; /* RHACTE=HDE-1 */
5652
5653         if (XGI_IsLCDDualLink(pVBInfo))
5654                 tempbx = tempbx >> 1;
5655
5656         tempbx -= 1;
5657         temp = tempbx & 0x00FF;
5658         xgifb_reg_set(pVBInfo->Part2Port, 0x2C, temp);
5659         temp = (tempbx & 0xFF00) >> 8;
5660         temp = temp << 4;
5661         xgifb_reg_and_or(pVBInfo->Part2Port, 0x2B, 0x0F, temp);
5662         temp = 0x01;
5663
5664         if (pVBInfo->LCDResInfo == Panel1280x1024) {
5665                 if (pVBInfo->ModeType == ModeEGA) {
5666                         if (pVBInfo->VGAHDE >= 1024) {
5667                                 temp = 0x02;
5668                                 if (pVBInfo->LCDInfo & LCDVESATiming)
5669                                         temp = 0x01;
5670                         }
5671                 }
5672         }
5673
5674         xgifb_reg_set(pVBInfo->Part2Port, 0x0B, temp);
5675         tempbx = pVBInfo->VDE; /* RTVACTEO=(VDE-1)&0xFF */
5676         push1 = tempbx;
5677         tempbx--;
5678         temp = tempbx & 0x00FF;
5679         xgifb_reg_set(pVBInfo->Part2Port, 0x03, temp);
5680         temp = ((tempbx & 0xFF00) >> 8) & 0x07;
5681         xgifb_reg_and_or(pVBInfo->Part2Port, 0x0C, ~0x07, temp);
5682
5683         tempcx = pVBInfo->VT - 1;
5684         push2 = tempcx + 1;
5685         temp = tempcx & 0x00FF; /* RVTVT=VT-1 */
5686         xgifb_reg_set(pVBInfo->Part2Port, 0x19, temp);
5687         temp = (tempcx & 0xFF00) >> 8;
5688         temp = temp << 5;
5689         xgifb_reg_set(pVBInfo->Part2Port, 0x1A, temp);
5690         xgifb_reg_and_or(pVBInfo->Part2Port, 0x09, 0xF0, 0x00);
5691         xgifb_reg_and_or(pVBInfo->Part2Port, 0x0A, 0xF0, 0x00);
5692         xgifb_reg_and_or(pVBInfo->Part2Port, 0x17, 0xFB, 0x00);
5693         xgifb_reg_and_or(pVBInfo->Part2Port, 0x18, 0xDF, 0x00);
5694
5695         /* Customized LCDB Des no add */
5696         tempbx = 5;
5697         LCDBDesPtr = (struct XGI_LCDDesStruct *) XGI_GetLcdPtr(tempbx, ModeNo,
5698                         ModeIdIndex, RefreshRateTableIndex, pVBInfo);
5699         tempah = pVBInfo->LCDResInfo;
5700         tempah &= PanelResInfo;
5701
5702         if ((tempah == Panel1024x768) || (tempah == Panel1024x768x75)) {
5703                 tempbx = 1024;
5704                 tempcx = 768;
5705         } else if ((tempah == Panel1280x1024) ||
5706                    (tempah == Panel1280x1024x75)) {
5707                 tempbx = 1280;
5708                 tempcx = 1024;
5709         } else if (tempah == Panel1400x1050) {
5710                 tempbx = 1400;
5711                 tempcx = 1050;
5712         } else {
5713                 tempbx = 1600;
5714                 tempcx = 1200;
5715         }
5716
5717         if (pVBInfo->LCDInfo & EnableScalingLCD) {
5718                 tempbx = pVBInfo->HDE;
5719                 tempcx = pVBInfo->VDE;
5720         }
5721
5722         pushbx = tempbx;
5723         tempax = pVBInfo->VT;
5724         pVBInfo->LCDHDES = LCDBDesPtr->LCDHDES;
5725         pVBInfo->LCDHRS = LCDBDesPtr->LCDHRS;
5726         pVBInfo->LCDVDES = LCDBDesPtr->LCDVDES;
5727         pVBInfo->LCDVRS = LCDBDesPtr->LCDVRS;
5728         tempbx = pVBInfo->LCDVDES;
5729         tempcx += tempbx;
5730
5731         if (tempcx >= tempax)
5732                 tempcx -= tempax; /* lcdvdes */
5733
5734         temp = tempbx & 0x00FF; /* RVEQ1EQ=lcdvdes */
5735         xgifb_reg_set(pVBInfo->Part2Port, 0x05, temp);
5736         temp = tempcx & 0x00FF;
5737         xgifb_reg_set(pVBInfo->Part2Port, 0x06, temp);
5738         tempch = ((tempcx & 0xFF00) >> 8) & 0x07;
5739         tempbh = ((tempbx & 0xFF00) >> 8) & 0x07;
5740         tempah = tempch;
5741         tempah = tempah << 3;
5742         tempah |= tempbh;
5743         xgifb_reg_set(pVBInfo->Part2Port, 0x02, tempah);
5744
5745         /* getlcdsync() */
5746         XGI_GetLCDSync(&tempax, &tempbx, pVBInfo);
5747         tempcx = tempbx;
5748         tempax = pVBInfo->VT;
5749         tempbx = pVBInfo->LCDVRS;
5750
5751         /* if (SetLCD_Info & EnableScalingLCD) */
5752         tempcx += tempbx;
5753         if (tempcx >= tempax)
5754                 tempcx -= tempax;
5755
5756         temp = tempbx & 0x00FF; /* RTVACTEE=lcdvrs */
5757         xgifb_reg_set(pVBInfo->Part2Port, 0x04, temp);
5758         temp = (tempbx & 0xFF00) >> 8;
5759         temp = temp << 4;
5760         temp |= (tempcx & 0x000F);
5761         xgifb_reg_set(pVBInfo->Part2Port, 0x01, temp);
5762         tempcx = pushbx;
5763         tempax = pVBInfo->HT;
5764         tempbx = pVBInfo->LCDHDES;
5765         tempbx &= 0x0FFF;
5766
5767         if (XGI_IsLCDDualLink(pVBInfo)) {
5768                 tempax = tempax >> 1;
5769                 tempbx = tempbx >> 1;
5770                 tempcx = tempcx >> 1;
5771         }
5772
5773         if (pVBInfo->VBType & VB_XGI302LV)
5774                 tempbx += 1;
5775
5776         if (pVBInfo->VBType & VB_XGI301C) /* tap4 */
5777                 tempbx += 1;
5778
5779         tempcx += tempbx;
5780
5781         if (tempcx >= tempax)
5782                 tempcx -= tempax;
5783
5784         temp = tempbx & 0x00FF;
5785         xgifb_reg_set(pVBInfo->Part2Port, 0x1F, temp); /* RHBLKE=lcdhdes */
5786         temp = ((tempbx & 0xFF00) >> 8) << 4;
5787         xgifb_reg_set(pVBInfo->Part2Port, 0x20, temp);
5788         temp = tempcx & 0x00FF;
5789         xgifb_reg_set(pVBInfo->Part2Port, 0x23, temp); /* RHEQPLE=lcdhdee */
5790         temp = (tempcx & 0xFF00) >> 8;
5791         xgifb_reg_set(pVBInfo->Part2Port, 0x25, temp);
5792
5793         /* getlcdsync() */
5794         XGI_GetLCDSync(&tempax, &tempbx, pVBInfo);
5795         tempcx = tempax;
5796         tempax = pVBInfo->HT;
5797         tempbx = pVBInfo->LCDHRS;
5798         /* if ( SetLCD_Info & EnableScalingLCD) */
5799         if (XGI_IsLCDDualLink(pVBInfo)) {
5800                 tempax = tempax >> 1;
5801                 tempbx = tempbx >> 1;
5802                 tempcx = tempcx >> 1;
5803         }
5804
5805         if (pVBInfo->VBType & VB_XGI302LV)
5806                 tempbx += 1;
5807
5808         tempcx += tempbx;
5809
5810         if (tempcx >= tempax)
5811                 tempcx -= tempax;
5812
5813         temp = tempbx & 0x00FF; /* RHBURSTS=lcdhrs */
5814         xgifb_reg_set(pVBInfo->Part2Port, 0x1C, temp);
5815
5816         temp = (tempbx & 0xFF00) >> 8;
5817         temp = temp << 4;
5818         xgifb_reg_and_or(pVBInfo->Part2Port, 0x1D, ~0x0F0, temp);
5819         temp = tempcx & 0x00FF; /* RHSYEXP2S=lcdhre */
5820         xgifb_reg_set(pVBInfo->Part2Port, 0x21, temp);
5821
5822         if (!(pVBInfo->LCDInfo & LCDVESATiming)) {
5823                 if (pVBInfo->VGAVDE == 525) {
5824                         if (pVBInfo->VBType & (VB_XGI301B | VB_XGI302B
5825                                         | VB_XGI301LV | VB_XGI302LV
5826                                         | VB_XGI301C)) {
5827                                 temp = 0xC6;
5828                         } else
5829                                 temp = 0xC4;
5830
5831                         xgifb_reg_set(pVBInfo->Part2Port, 0x2f, temp);
5832                         xgifb_reg_set(pVBInfo->Part2Port, 0x30, 0xB3);
5833                 }
5834
5835                 if (pVBInfo->VGAVDE == 420) {
5836                         if (pVBInfo->VBType & (VB_XGI301B | VB_XGI302B
5837                                         | VB_XGI301LV | VB_XGI302LV
5838                                         | VB_XGI301C)) {
5839                                 temp = 0x4F;
5840                         } else
5841                                 temp = 0x4E;
5842                         xgifb_reg_set(pVBInfo->Part2Port, 0x2f, temp);
5843                 }
5844         }
5845 }
5846
5847 /* --------------------------------------------------------------------- */
5848 /* Function : XGI_GetTap4Ptr */
5849 /* Input : */
5850 /* Output : di -> Tap4 Reg. Setting Pointer */
5851 /* Description : */
5852 /* --------------------------------------------------------------------- */
5853 static struct XGI301C_Tap4TimingStruct *XGI_GetTap4Ptr(unsigned short tempcx,
5854                 struct vb_device_info *pVBInfo)
5855 {
5856         unsigned short tempax, tempbx, i;
5857
5858         struct XGI301C_Tap4TimingStruct *Tap4TimingPtr;
5859
5860         if (tempcx == 0) {
5861                 tempax = pVBInfo->VGAHDE;
5862                 tempbx = pVBInfo->HDE;
5863         } else {
5864                 tempax = pVBInfo->VGAVDE;
5865                 tempbx = pVBInfo->VDE;
5866         }
5867
5868         if (tempax < tempbx)
5869                 return &EnlargeTap4Timing[0];
5870         else if (tempax == tempbx)
5871                 return &NoScaleTap4Timing[0]; /* 1:1 */
5872         else
5873                 Tap4TimingPtr = NTSCTap4Timing; /* NTSC */
5874
5875         if (pVBInfo->TVInfo & SetPALTV)
5876                 Tap4TimingPtr = PALTap4Timing;
5877
5878         if (pVBInfo->VBInfo & SetCRT2ToYPbPr) {
5879                 if (pVBInfo->TVInfo & SetYPbPrMode525i)
5880                         Tap4TimingPtr = YPbPr525iTap4Timing;
5881                 if (pVBInfo->TVInfo & SetYPbPrMode525p)
5882                         Tap4TimingPtr = YPbPr525pTap4Timing;
5883                 if (pVBInfo->TVInfo & SetYPbPrMode750p)
5884                         Tap4TimingPtr = YPbPr750pTap4Timing;
5885         }
5886
5887         if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV)
5888                 Tap4TimingPtr = HiTVTap4Timing;
5889
5890         i = 0;
5891         while (Tap4TimingPtr[i].DE != 0xFFFF) {
5892                 if (Tap4TimingPtr[i].DE == tempax)
5893                         break;
5894                 i++;
5895         }
5896         return &Tap4TimingPtr[i];
5897 }
5898
5899 static void XGI_SetTap4Regs(struct vb_device_info *pVBInfo)
5900 {
5901         unsigned short i, j;
5902
5903         struct XGI301C_Tap4TimingStruct *Tap4TimingPtr;
5904
5905         if (!(pVBInfo->VBType & VB_XGI301C))
5906                 return;
5907
5908 #ifndef Tap4
5909         xgifb_reg_and(pVBInfo->Part2Port, 0x4E, 0xEB); /* Disable Tap4 */
5910 #else            /* Tap4 Setting */
5911
5912         Tap4TimingPtr = XGI_GetTap4Ptr(0, pVBInfo); /* Set Horizontal Scaling */
5913         for (i = 0x80, j = 0; i <= 0xBF; i++, j++)
5914                 xgifb_reg_set(pVBInfo->Part2Port, i, Tap4TimingPtr->Reg[j]);
5915
5916         if ((pVBInfo->VBInfo & SetCRT2ToTV) &&
5917             (!(pVBInfo->VBInfo & SetCRT2ToHiVisionTV))) {
5918                 /* Set Vertical Scaling */
5919                 Tap4TimingPtr = XGI_GetTap4Ptr(1, pVBInfo);
5920                 for (i = 0xC0, j = 0; i < 0xFF; i++, j++)
5921                         xgifb_reg_set(pVBInfo->Part2Port,
5922                                       i,
5923                                       Tap4TimingPtr->Reg[j]);
5924         }
5925
5926         if ((pVBInfo->VBInfo & SetCRT2ToTV) &&
5927             (!(pVBInfo->VBInfo & SetCRT2ToHiVisionTV)))
5928                 /* Enable V.Scaling */
5929                 xgifb_reg_and_or(pVBInfo->Part2Port, 0x4E, ~0x14, 0x04);
5930         else
5931                 /* Enable H.Scaling */
5932                 xgifb_reg_and_or(pVBInfo->Part2Port, 0x4E, ~0x14, 0x10);
5933 #endif
5934 }
5935
5936 static void XGI_SetGroup3(unsigned short ModeNo, unsigned short ModeIdIndex,
5937                 struct vb_device_info *pVBInfo)
5938 {
5939         unsigned short i;
5940         unsigned char *tempdi;
5941         unsigned short modeflag;
5942
5943         if (ModeNo <= 0x13)
5944                 /* si+St_ResInfo */
5945                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
5946         else
5947                 /* si+Ext_ResInfo */
5948                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
5949
5950         xgifb_reg_set(pVBInfo->Part3Port, 0x00, 0x00);
5951         if (pVBInfo->TVInfo & SetPALTV) {
5952                 xgifb_reg_set(pVBInfo->Part3Port, 0x13, 0xFA);
5953                 xgifb_reg_set(pVBInfo->Part3Port, 0x14, 0xC8);
5954         } else {
5955                 xgifb_reg_set(pVBInfo->Part3Port, 0x13, 0xF5);
5956                 xgifb_reg_set(pVBInfo->Part3Port, 0x14, 0xB7);
5957         }
5958
5959         if (!(pVBInfo->VBInfo & SetCRT2ToTV))
5960                 return;
5961
5962         if (pVBInfo->TVInfo & SetPALMTV) {
5963                 xgifb_reg_set(pVBInfo->Part3Port, 0x13, 0xFA);
5964                 xgifb_reg_set(pVBInfo->Part3Port, 0x14, 0xC8);
5965                 xgifb_reg_set(pVBInfo->Part3Port, 0x3D, 0xA8);
5966         }
5967
5968         if ((pVBInfo->VBInfo & SetCRT2ToHiVisionTV) || (pVBInfo->VBInfo
5969                         & SetCRT2ToYPbPr)) {
5970                 if (pVBInfo->TVInfo & SetYPbPrMode525i)
5971                         return;
5972
5973                 tempdi = pVBInfo->HiTVGroup3Data;
5974                 if (pVBInfo->SetFlag & TVSimuMode) {
5975                         tempdi = pVBInfo->HiTVGroup3Simu;
5976                         if (!(modeflag & Charx8Dot))
5977                                 tempdi = pVBInfo->HiTVGroup3Text;
5978                 }
5979
5980                 if (pVBInfo->TVInfo & SetYPbPrMode525p)
5981                         tempdi = pVBInfo->Ren525pGroup3;
5982
5983                 if (pVBInfo->TVInfo & SetYPbPrMode750p)
5984                         tempdi = pVBInfo->Ren750pGroup3;
5985
5986                 for (i = 0; i <= 0x3E; i++)
5987                         xgifb_reg_set(pVBInfo->Part3Port, i, tempdi[i]);
5988
5989                 if (pVBInfo->VBType & VB_XGI301C) { /* Marcovision */
5990                         if (pVBInfo->TVInfo & SetYPbPrMode525p)
5991                                 xgifb_reg_set(pVBInfo->Part3Port, 0x28, 0x3f);
5992                 }
5993         }
5994         return;
5995 } /* {end of XGI_SetGroup3} */
5996
5997 static void XGI_SetGroup4(unsigned short ModeNo, unsigned short ModeIdIndex,
5998                 unsigned short RefreshRateTableIndex,
5999                 struct xgi_hw_device_info *HwDeviceExtension,
6000                 struct vb_device_info *pVBInfo)
6001 {
6002         unsigned short tempax, tempcx, tempbx, modeflag, temp, temp2;
6003
6004         unsigned long tempebx, tempeax, templong;
6005
6006         if (ModeNo <= 0x13)
6007                 /* si+St_ResInfo */
6008                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
6009         else
6010                 /* si+Ext_ResInfo */
6011                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
6012
6013         temp = pVBInfo->RVBHCFACT;
6014         xgifb_reg_set(pVBInfo->Part4Port, 0x13, temp);
6015
6016         tempbx = pVBInfo->RVBHCMAX;
6017         temp = tempbx & 0x00FF;
6018         xgifb_reg_set(pVBInfo->Part4Port, 0x14, temp);
6019         temp2 = ((tempbx & 0xFF00) >> 8) << 7;
6020         tempcx = pVBInfo->VGAHT - 1;
6021         temp = tempcx & 0x00FF;
6022         xgifb_reg_set(pVBInfo->Part4Port, 0x16, temp);
6023
6024         temp = ((tempcx & 0xFF00) >> 8) << 3;
6025         temp2 |= temp;
6026
6027         tempcx = pVBInfo->VGAVT - 1;
6028         if (!(pVBInfo->VBInfo & SetCRT2ToTV))
6029                 tempcx -= 5;
6030
6031         temp = tempcx & 0x00FF;
6032         xgifb_reg_set(pVBInfo->Part4Port, 0x17, temp);
6033         temp = temp2 | ((tempcx & 0xFF00) >> 8);
6034         xgifb_reg_set(pVBInfo->Part4Port, 0x15, temp);
6035         xgifb_reg_or(pVBInfo->Part4Port, 0x0D, 0x08);
6036         tempcx = pVBInfo->VBInfo;
6037         tempbx = pVBInfo->VGAHDE;
6038
6039         if (modeflag & HalfDCLK)
6040                 tempbx = tempbx >> 1;
6041
6042         if (XGI_IsLCDDualLink(pVBInfo))
6043                 tempbx = tempbx >> 1;
6044
6045         if (tempcx & SetCRT2ToHiVisionTV) {
6046                 temp = 0;
6047                 if (tempbx <= 1024)
6048                         temp = 0xA0;
6049                 if (tempbx == 1280)
6050                         temp = 0xC0;
6051         } else if (tempcx & SetCRT2ToTV) {
6052                 temp = 0xA0;
6053                 if (tempbx <= 800)
6054                         temp = 0x80;
6055         } else {
6056                 temp = 0x80;
6057                 if (pVBInfo->VBInfo & SetCRT2ToLCD) {
6058                         temp = 0;
6059                         if (tempbx > 800)
6060                                 temp = 0x60;
6061                 }
6062         }
6063
6064         if (pVBInfo->TVInfo & (SetYPbPrMode525p | SetYPbPrMode750p)) {
6065                 temp = 0x00;
6066                 if (pVBInfo->VGAHDE == 1280)
6067                         temp = 0x40;
6068                 if (pVBInfo->VGAHDE == 1024)
6069                         temp = 0x20;
6070         }
6071         xgifb_reg_and_or(pVBInfo->Part4Port, 0x0E, ~0xEF, temp);
6072
6073         tempebx = pVBInfo->VDE;
6074
6075         if (tempcx & SetCRT2ToHiVisionTV) {
6076                 if (!(temp & 0xE000))
6077                         tempbx = tempbx >> 1;
6078         }
6079
6080         tempcx = pVBInfo->RVBHRS;
6081         temp = tempcx & 0x00FF;
6082         xgifb_reg_set(pVBInfo->Part4Port, 0x18, temp);
6083
6084         tempeax = pVBInfo->VGAVDE;
6085         tempcx |= 0x04000;
6086
6087         if (tempeax <= tempebx) {
6088                 tempcx = (tempcx & (~0x4000));
6089                 tempeax = pVBInfo->VGAVDE;
6090         } else {
6091                 tempeax -= tempebx;
6092         }
6093
6094         templong = (tempeax * 256 * 1024) % tempebx;
6095         tempeax = (tempeax * 256 * 1024) / tempebx;
6096         tempebx = tempeax;
6097
6098         if (templong != 0)
6099                 tempebx++;
6100
6101         temp = (unsigned short) (tempebx & 0x000000FF);
6102         xgifb_reg_set(pVBInfo->Part4Port, 0x1B, temp);
6103
6104         temp = (unsigned short) ((tempebx & 0x0000FF00) >> 8);
6105         xgifb_reg_set(pVBInfo->Part4Port, 0x1A, temp);
6106         tempbx = (unsigned short) (tempebx >> 16);
6107         temp = tempbx & 0x00FF;
6108         temp = temp << 4;
6109         temp |= ((tempcx & 0xFF00) >> 8);
6110         xgifb_reg_set(pVBInfo->Part4Port, 0x19, temp);
6111
6112         /* 301b */
6113         if (pVBInfo->VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV
6114                         | VB_XGI302LV | VB_XGI301C)) {
6115                 temp = 0x0028;
6116                 xgifb_reg_set(pVBInfo->Part4Port, 0x1C, temp);
6117                 tempax = pVBInfo->VGAHDE;
6118                 if (modeflag & HalfDCLK)
6119                         tempax = tempax >> 1;
6120
6121                 if (XGI_IsLCDDualLink(pVBInfo))
6122                         tempax = tempax >> 1;
6123
6124                 /* if((pVBInfo->VBInfo&(SetCRT2ToLCD)) ||
6125                       ((pVBInfo->TVInfo&SetYPbPrMode525p) ||
6126                       (pVBInfo->TVInfo&SetYPbPrMode750p))) { */
6127                 if (pVBInfo->VBInfo & SetCRT2ToLCD) {
6128                         if (tempax > 800)
6129                                 tempax -= 800;
6130                 } else {
6131                         if (pVBInfo->VGAHDE > 800) {
6132                                 if (pVBInfo->VGAHDE == 1024)
6133                                         tempax = (tempax * 25 / 32) - 1;
6134                                 else
6135                                         tempax = (tempax * 20 / 32) - 1;
6136                         }
6137                 }
6138                 tempax -= 1;
6139
6140                 /*
6141                 if (pVBInfo->VBInfo & (SetCRT2ToTV | SetCRT2ToHiVisionTV)) {
6142                         if (pVBInfo->VBType & VB_XGI301LV) {
6143                                 if (!(pVBInfo->TVInfo &
6144                                       (SetYPbPrMode525p |
6145                                        SetYPbPrMode750p |
6146                                        SetYPbPrMode1080i))) {
6147                                         if (pVBInfo->VGAHDE > 800) {
6148                                                 if (pVBInfo->VGAHDE == 1024)
6149                                                         tempax =(tempax * 25 /
6150                                                                  32) - 1;
6151                                                 else
6152                                                         tempax = (tempax * 20 /
6153                                                                   32) - 1;
6154                                         }
6155                                 }
6156                         } else {
6157                                 if (pVBInfo->VGAHDE > 800) {
6158                                         if (pVBInfo->VGAHDE == 1024)
6159                                                 tempax = (tempax * 25 / 32) - 1;
6160                                         else
6161                                                 tempax = (tempax * 20 / 32) - 1;
6162                                 }
6163                         }
6164                 }
6165                 */
6166
6167                 temp = (tempax & 0xFF00) >> 8;
6168                 temp = ((temp & 0x0003) << 4);
6169                 xgifb_reg_set(pVBInfo->Part4Port, 0x1E, temp);
6170                 temp = (tempax & 0x00FF);
6171                 xgifb_reg_set(pVBInfo->Part4Port, 0x1D, temp);
6172
6173                 if (pVBInfo->VBInfo & (SetCRT2ToTV | SetCRT2ToHiVisionTV)) {
6174                         if (pVBInfo->VGAHDE > 800)
6175                                 xgifb_reg_or(pVBInfo->Part4Port, 0x1E, 0x08);
6176
6177                 }
6178                 temp = 0x0036;
6179
6180                 if (pVBInfo->VBInfo & SetCRT2ToTV) {
6181                         if (!(pVBInfo->TVInfo & (NTSC1024x768
6182                                         | SetYPbPrMode525p | SetYPbPrMode750p
6183                                         | SetYPbPrMode1080i))) {
6184                                 temp |= 0x0001;
6185                                 if ((pVBInfo->VBInfo & SetInSlaveMode)
6186                                                 && (!(pVBInfo->TVInfo
6187                                                                 & TVSimuMode)))
6188                                         temp &= (~0x0001);
6189                         }
6190                 }
6191
6192                 xgifb_reg_and_or(pVBInfo->Part4Port, 0x1F, 0x00C0, temp);
6193                 tempbx = pVBInfo->HT;
6194                 if (XGI_IsLCDDualLink(pVBInfo))
6195                         tempbx = tempbx >> 1;
6196                 tempbx = (tempbx >> 1) - 2;
6197                 temp = ((tempbx & 0x0700) >> 8) << 3;
6198                 xgifb_reg_and_or(pVBInfo->Part4Port, 0x21, 0x00C0, temp);
6199                 temp = tempbx & 0x00FF;
6200                 xgifb_reg_set(pVBInfo->Part4Port, 0x22, temp);
6201         }
6202         /* end 301b */
6203
6204         if (pVBInfo->ISXPDOS == 0)
6205                 XGI_SetCRT2VCLK(ModeNo, ModeIdIndex, RefreshRateTableIndex,
6206                                 pVBInfo);
6207 }
6208
6209 static void XGINew_EnableCRT2(struct vb_device_info *pVBInfo)
6210 {
6211         xgifb_reg_and_or(pVBInfo->P3c4, 0x1E, 0xFF, 0x20);
6212 }
6213
6214 static void XGI_SetGroup5(unsigned short ModeNo, unsigned short ModeIdIndex,
6215                 struct vb_device_info *pVBInfo)
6216 {
6217         unsigned short Pindex, Pdata;
6218
6219         Pindex = pVBInfo->Part5Port;
6220         Pdata = pVBInfo->Part5Port + 1;
6221         if (pVBInfo->ModeType == ModeVGA) {
6222                 if (!(pVBInfo->VBInfo & (SetInSlaveMode | LoadDACFlag
6223                                 | CRT2DisplayFlag))) {
6224                         XGINew_EnableCRT2(pVBInfo);
6225                         /* LoadDAC2(pVBInfo->Part5Port, ModeNo, ModeIdIndex); */
6226                 }
6227         }
6228         return;
6229 }
6230
6231 static void XGI_EnableGatingCRT(struct xgi_hw_device_info *HwDeviceExtension,
6232                 struct vb_device_info *pVBInfo)
6233 {
6234         xgifb_reg_and_or(pVBInfo->P3d4, 0x63, 0xBF, 0x40);
6235 }
6236
6237 static void XGI_DisableGatingCRT(struct xgi_hw_device_info *HwDeviceExtension,
6238                 struct vb_device_info *pVBInfo)
6239 {
6240
6241         xgifb_reg_and_or(pVBInfo->P3d4, 0x63, 0xBF, 0x00);
6242 }
6243
6244 /*----------------------------------------------------------------------------*/
6245 /* input                                                                      */
6246 /*      bl[5] : 1;LVDS signal on                                              */
6247 /*      bl[1] : 1;LVDS backlight on                                           */
6248 /*      bl[0] : 1:LVDS VDD on                                                 */
6249 /*      bh: 100000b : clear bit 5, to set bit5                                */
6250 /*          000010b : clear bit 1, to set bit1                                */
6251 /*          000001b : clear bit 0, to set bit0                                */
6252 /*----------------------------------------------------------------------------*/
6253 void XGI_XG21BLSignalVDD(unsigned short tempbh, unsigned short tempbl,
6254                 struct vb_device_info *pVBInfo)
6255 {
6256         unsigned char CR4A, temp;
6257
6258         CR4A = xgifb_reg_get(pVBInfo->P3d4, 0x4A);
6259         tempbh &= 0x23;
6260         tempbl &= 0x23;
6261         xgifb_reg_and(pVBInfo->P3d4, 0x4A, ~tempbh); /* enable GPIO write */
6262
6263         if (tempbh & 0x20) {
6264                 temp = (tempbl >> 4) & 0x02;
6265
6266                 /* CR B4[1] */
6267                 xgifb_reg_and_or(pVBInfo->P3d4, 0xB4, ~0x02, temp);
6268
6269         }
6270
6271         temp = xgifb_reg_get(pVBInfo->P3d4, 0x48);
6272
6273         temp = XG21GPIODataTransfer(temp);
6274         temp &= ~tempbh;
6275         temp |= tempbl;
6276         xgifb_reg_set(pVBInfo->P3d4, 0x48, temp);
6277 }
6278
6279 void XGI_XG27BLSignalVDD(unsigned short tempbh, unsigned short tempbl,
6280                 struct vb_device_info *pVBInfo)
6281 {
6282         unsigned char CR4A, temp;
6283         unsigned short tempbh0, tempbl0;
6284
6285         tempbh0 = tempbh;
6286         tempbl0 = tempbl;
6287         tempbh0 &= 0x20;
6288         tempbl0 &= 0x20;
6289         tempbh0 >>= 3;
6290         tempbl0 >>= 3;
6291
6292         if (tempbh & 0x20) {
6293                 temp = (tempbl >> 4) & 0x02;
6294
6295                 /* CR B4[1] */
6296                 xgifb_reg_and_or(pVBInfo->P3d4, 0xB4, ~0x02, temp);
6297
6298         }
6299         xgifb_reg_and_or(pVBInfo->P3d4, 0xB4, ~tempbh0, tempbl0);
6300
6301         CR4A = xgifb_reg_get(pVBInfo->P3d4, 0x4A);
6302         tempbh &= 0x03;
6303         tempbl &= 0x03;
6304         tempbh <<= 2;
6305         tempbl <<= 2; /* GPIOC,GPIOD */
6306         xgifb_reg_and(pVBInfo->P3d4, 0x4A, ~tempbh); /* enable GPIO write */
6307         xgifb_reg_and_or(pVBInfo->P3d4, 0x48, ~tempbh, tempbl);
6308 }
6309
6310 /* --------------------------------------------------------------------- */
6311 unsigned short XGI_GetLVDSOEMTableIndex(struct vb_device_info *pVBInfo)
6312 {
6313         unsigned short index;
6314
6315         index = xgifb_reg_get(pVBInfo->P3d4, 0x36);
6316         if (index < sizeof(XGI21_LCDCapList)
6317                         / sizeof(struct XGI21_LVDSCapStruct))
6318                 return index;
6319         return 0;
6320 }
6321
6322 /* --------------------------------------------------------------------- */
6323 /* Function : XGI_XG21SetPanelDelay */
6324 /* Input : */
6325 /* Output : */
6326 /* Description : */
6327 /* I/P : bl : 1 ; T1 : the duration between CPL on and signal on */
6328 /* : bl : 2 ; T2 : the duration signal on and Vdd on */
6329 /* : bl : 3 ; T3 : the duration between CPL off and signal off */
6330 /* : bl : 4 ; T4 : the duration signal off and Vdd off */
6331 /* --------------------------------------------------------------------- */
6332 void XGI_XG21SetPanelDelay(unsigned short tempbl,
6333                 struct vb_device_info *pVBInfo)
6334 {
6335         unsigned short index;
6336
6337         index = XGI_GetLVDSOEMTableIndex(pVBInfo);
6338         if (tempbl == 1)
6339                 mdelay(pVBInfo->XG21_LVDSCapList[index].PSC_S1);
6340
6341         if (tempbl == 2)
6342                 mdelay(pVBInfo->XG21_LVDSCapList[index].PSC_S2);
6343
6344         if (tempbl == 3)
6345                 mdelay(pVBInfo->XG21_LVDSCapList[index].PSC_S3);
6346
6347         if (tempbl == 4)
6348                 mdelay(pVBInfo->XG21_LVDSCapList[index].PSC_S4);
6349 }
6350
6351 unsigned char XGI_XG21CheckLVDSMode(unsigned short ModeNo,
6352                 unsigned short ModeIdIndex, struct vb_device_info *pVBInfo)
6353 {
6354         unsigned short xres, yres, colordepth, modeflag, resindex,
6355                         lvdstableindex;
6356
6357         resindex = XGI_GetResInfo(ModeNo, ModeIdIndex, pVBInfo);
6358         if (ModeNo <= 0x13) {
6359                 xres = pVBInfo->StResInfo[resindex].HTotal;
6360                 yres = pVBInfo->StResInfo[resindex].VTotal;
6361                 /* si+St_ResInfo */
6362                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
6363         } else {
6364                 xres = pVBInfo->ModeResInfo[resindex].HTotal; /* xres->ax */
6365                 yres = pVBInfo->ModeResInfo[resindex].VTotal; /* yres->bx */
6366                 /* si+St_ModeFlag */
6367                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
6368         }
6369
6370         if (!(modeflag & Charx8Dot)) {
6371                 xres /= 9;
6372                 xres *= 8;
6373         }
6374
6375         if (ModeNo > 0x13) {
6376                 if ((ModeNo > 0x13) && (modeflag & HalfDCLK))
6377                         xres *= 2;
6378
6379                 if ((ModeNo > 0x13) && (modeflag & DoubleScanMode))
6380                         yres *= 2;
6381
6382         }
6383
6384         lvdstableindex = XGI_GetLVDSOEMTableIndex(pVBInfo);
6385         if (xres > (pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSHDE))
6386                 return 0;
6387
6388         if (yres > (pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSVDE))
6389                 return 0;
6390
6391         if (ModeNo > 0x13) {
6392                 if ((xres != (pVBInfo->XG21_LVDSCapList[lvdstableindex].
6393                                 LVDSHDE)) ||
6394                     (yres != (pVBInfo->XG21_LVDSCapList[lvdstableindex].
6395                                 LVDSVDE))) {
6396                         colordepth = XGI_GetColorDepth(ModeNo,
6397                                                        ModeIdIndex,
6398                                                        pVBInfo);
6399                         if (colordepth > 2)
6400                                 return 0;
6401
6402                 }
6403         }
6404         return 1;
6405 }
6406
6407 void XGI_SetXG21FPBits(struct vb_device_info *pVBInfo)
6408 {
6409         unsigned char temp;
6410
6411         temp = xgifb_reg_get(pVBInfo->P3d4, 0x37); /* D[0] 1: 18bit */
6412         temp = (temp & 1) << 6;
6413         /* SR06[6] 18bit Dither */
6414         xgifb_reg_and_or(pVBInfo->P3c4, 0x06, ~0x40, temp);
6415         /* SR09[7] enable FP output, SR09[6] 1: sigle 18bits, 0: dual 12bits */
6416         xgifb_reg_and_or(pVBInfo->P3c4, 0x09, ~0xc0, temp | 0x80);
6417
6418 }
6419
6420 void XGI_SetXG27FPBits(struct vb_device_info *pVBInfo)
6421 {
6422         unsigned char temp;
6423
6424         /* D[1:0] 01: 18bit, 00: dual 12, 10: single 24 */
6425         temp = xgifb_reg_get(pVBInfo->P3d4, 0x37);
6426         temp = (temp & 3) << 6;
6427         /* SR06[7]0: dual 12/1: single 24 [6] 18bit Dither <= 0 h/w recommend */
6428         xgifb_reg_and_or(pVBInfo->P3c4, 0x06, ~0xc0, temp & 0x80);
6429         /* SR09[7] enable FP output, SR09[6] 1: sigle 18bits, 0: 24bits */
6430         xgifb_reg_and_or(pVBInfo->P3c4, 0x09, ~0xc0, temp | 0x80);
6431
6432 }
6433
6434 static void XGI_SetXG21LVDSPara(unsigned short ModeNo,
6435                                 unsigned short ModeIdIndex,
6436                                 struct vb_device_info *pVBInfo)
6437 {
6438         unsigned char temp, Miscdata;
6439         unsigned short xres, yres, modeflag, resindex, lvdstableindex;
6440         unsigned short LVDSHT, LVDSHBS, LVDSHRS, LVDSHRE, LVDSHBE;
6441         unsigned short LVDSVT, LVDSVBS, LVDSVRS, LVDSVRE, LVDSVBE;
6442         unsigned short value;
6443
6444         lvdstableindex = XGI_GetLVDSOEMTableIndex(pVBInfo);
6445
6446         temp = (unsigned char) ((pVBInfo->XG21_LVDSCapList[lvdstableindex].
6447                                                 LVDS_Capability &
6448                                 (LCDPolarity << 8)) >> 8);
6449         temp &= LCDPolarity;
6450         Miscdata = (unsigned char) inb(pVBInfo->P3cc);
6451
6452         outb((Miscdata & 0x3F) | temp, pVBInfo->P3c2);
6453
6454         temp = (unsigned char) (pVBInfo->XG21_LVDSCapList[lvdstableindex].
6455                                                 LVDS_Capability & LCDPolarity);
6456         /* SR35[7] FP VSync polarity */
6457         xgifb_reg_and_or(pVBInfo->P3c4, 0x35, ~0x80, temp & 0x80);
6458         /* SR30[5] FP HSync polarity */
6459         xgifb_reg_and_or(pVBInfo->P3c4, 0x30, ~0x20, (temp & 0x40) >> 1);
6460
6461         XGI_SetXG21FPBits(pVBInfo);
6462         resindex = XGI_GetResInfo(ModeNo, ModeIdIndex, pVBInfo);
6463         if (ModeNo <= 0x13) {
6464                 xres = pVBInfo->StResInfo[resindex].HTotal;
6465                 yres = pVBInfo->StResInfo[resindex].VTotal;
6466                 /* si+St_ResInfo */
6467                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
6468         } else {
6469                 xres = pVBInfo->ModeResInfo[resindex].HTotal; /* xres->ax */
6470                 yres = pVBInfo->ModeResInfo[resindex].VTotal; /* yres->bx */
6471                 /* si+St_ModeFlag */
6472                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
6473         }
6474
6475         if (!(modeflag & Charx8Dot))
6476                 xres = xres * 8 / 9;
6477
6478         LVDSHT = pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSHT;
6479
6480         LVDSHBS = xres + (pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSHDE
6481                         - xres) / 2;
6482         if ((ModeNo <= 0x13) && (modeflag & HalfDCLK))
6483                 LVDSHBS -= xres / 4;
6484
6485         if (LVDSHBS > LVDSHT)
6486                 LVDSHBS -= LVDSHT;
6487
6488         LVDSHRS = LVDSHBS + pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSHFP;
6489         if (LVDSHRS > LVDSHT)
6490                 LVDSHRS -= LVDSHT;
6491
6492         LVDSHRE = LVDSHRS + pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSHSYNC;
6493         if (LVDSHRE > LVDSHT)
6494                 LVDSHRE -= LVDSHT;
6495
6496         LVDSHBE = LVDSHBS + LVDSHT
6497                         - pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSHDE;
6498
6499         LVDSVT = pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSVT;
6500
6501         LVDSVBS = yres + (pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSVDE
6502                         - yres) / 2;
6503         if ((ModeNo > 0x13) && (modeflag & DoubleScanMode))
6504                 LVDSVBS += yres / 2;
6505
6506         if (LVDSVBS > LVDSVT)
6507                 LVDSVBS -= LVDSVT;
6508
6509         LVDSVRS = LVDSVBS + pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSVFP;
6510         if (LVDSVRS > LVDSVT)
6511                 LVDSVRS -= LVDSVT;
6512
6513         LVDSVRE = LVDSVRS + pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSVSYNC;
6514         if (LVDSVRE > LVDSVT)
6515                 LVDSVRE -= LVDSVT;
6516
6517         LVDSVBE = LVDSVBS + LVDSVT
6518                         - pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSVDE;
6519
6520         temp = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x11);
6521         xgifb_reg_set(pVBInfo->P3d4, 0x11, temp & 0x7f); /* Unlock CRTC */
6522
6523         if (!(modeflag & Charx8Dot))
6524                 xgifb_reg_or(pVBInfo->P3c4, 0x1, 0x1);
6525
6526         /* HT SR0B[1:0] CR00 */
6527         value = (LVDSHT >> 3) - 5;
6528         xgifb_reg_and_or(pVBInfo->P3c4, 0x0B, ~0x03, (value & 0x300) >> 8);
6529         xgifb_reg_set(pVBInfo->P3d4, 0x0, (value & 0xFF));
6530
6531         /* HBS SR0B[5:4] CR02 */
6532         value = (LVDSHBS >> 3) - 1;
6533         xgifb_reg_and_or(pVBInfo->P3c4, 0x0B, ~0x30, (value & 0x300) >> 4);
6534         xgifb_reg_set(pVBInfo->P3d4, 0x2, (value & 0xFF));
6535
6536         /* HBE SR0C[1:0] CR05[7] CR03[4:0] */
6537         value = (LVDSHBE >> 3) - 1;
6538         xgifb_reg_and_or(pVBInfo->P3c4, 0x0C, ~0x03, (value & 0xC0) >> 6);
6539         xgifb_reg_and_or(pVBInfo->P3d4, 0x05, ~0x80, (value & 0x20) << 2);
6540         xgifb_reg_and_or(pVBInfo->P3d4, 0x03, ~0x1F, value & 0x1F);
6541
6542         /* HRS SR0B[7:6] CR04 */
6543         value = (LVDSHRS >> 3) + 2;
6544         xgifb_reg_and_or(pVBInfo->P3c4, 0x0B, ~0xC0, (value & 0x300) >> 2);
6545         xgifb_reg_set(pVBInfo->P3d4, 0x4, (value & 0xFF));
6546
6547         /* Panel HRS SR2F[1:0] SR2E[7:0]  */
6548         value--;
6549         xgifb_reg_and_or(pVBInfo->P3c4, 0x2F, ~0x03, (value & 0x300) >> 8);
6550         xgifb_reg_set(pVBInfo->P3c4, 0x2E, (value & 0xFF));
6551
6552         /* HRE SR0C[2] CR05[4:0] */
6553         value = (LVDSHRE >> 3) + 2;
6554         xgifb_reg_and_or(pVBInfo->P3c4, 0x0C, ~0x04, (value & 0x20) >> 3);
6555         xgifb_reg_and_or(pVBInfo->P3d4, 0x05, ~0x1F, value & 0x1F);
6556
6557         /* Panel HRE SR2F[7:2]  */
6558         value--;
6559         xgifb_reg_and_or(pVBInfo->P3c4, 0x2F, ~0xFC, value << 2);
6560
6561         /* VT SR0A[0] CR07[5][0] CR06 */
6562         value = LVDSVT - 2;
6563         xgifb_reg_and_or(pVBInfo->P3c4, 0x0A, ~0x01, (value & 0x400) >> 10);
6564         xgifb_reg_and_or(pVBInfo->P3d4, 0x07, ~0x20, (value & 0x200) >> 4);
6565         xgifb_reg_and_or(pVBInfo->P3d4, 0x07, ~0x01, (value & 0x100) >> 8);
6566         xgifb_reg_set(pVBInfo->P3d4, 0x06, (value & 0xFF));
6567
6568         /* VBS SR0A[2] CR09[5] CR07[3] CR15 */
6569         value = LVDSVBS - 1;
6570         xgifb_reg_and_or(pVBInfo->P3c4, 0x0A, ~0x04, (value & 0x400) >> 8);
6571         xgifb_reg_and_or(pVBInfo->P3d4, 0x09, ~0x20, (value & 0x200) >> 4);
6572         xgifb_reg_and_or(pVBInfo->P3d4, 0x07, ~0x08, (value & 0x100) >> 5);
6573         xgifb_reg_set(pVBInfo->P3d4, 0x15, (value & 0xFF));
6574
6575         /* VBE SR0A[4] CR16 */
6576         value = LVDSVBE - 1;
6577         xgifb_reg_and_or(pVBInfo->P3c4, 0x0A, ~0x10, (value & 0x100) >> 4);
6578         xgifb_reg_set(pVBInfo->P3d4, 0x16, (value & 0xFF));
6579
6580         /* VRS SR0A[3] CR7[7][2] CR10 */
6581         value = LVDSVRS - 1;
6582         xgifb_reg_and_or(pVBInfo->P3c4, 0x0A, ~0x08, (value & 0x400) >> 7);
6583         xgifb_reg_and_or(pVBInfo->P3d4, 0x07, ~0x80, (value & 0x200) >> 2);
6584         xgifb_reg_and_or(pVBInfo->P3d4, 0x07, ~0x04, (value & 0x100) >> 6);
6585         xgifb_reg_set(pVBInfo->P3d4, 0x10, (value & 0xFF));
6586
6587         /* Panel VRS SR3F[1:0] SR34[7:0] SR33[0] */
6588         xgifb_reg_and_or(pVBInfo->P3c4, 0x3F, ~0x03, (value & 0x600) >> 9);
6589         xgifb_reg_set(pVBInfo->P3c4, 0x34, (value >> 1) & 0xFF);
6590         xgifb_reg_and_or(pVBInfo->P3d4, 0x33, ~0x01, value & 0x01);
6591
6592         /* VRE SR0A[5] CR11[3:0] */
6593         value = LVDSVRE - 1;
6594         xgifb_reg_and_or(pVBInfo->P3c4, 0x0A, ~0x20, (value & 0x10) << 1);
6595         xgifb_reg_and_or(pVBInfo->P3d4, 0x11, ~0x0F, value & 0x0F);
6596
6597         /* Panel VRE SR3F[7:2] *//* SR3F[7] has to be 0, h/w bug */
6598         xgifb_reg_and_or(pVBInfo->P3c4, 0x3F, ~0xFC, (value << 2) & 0x7C);
6599
6600         for (temp = 0, value = 0; temp < 3; temp++) {
6601
6602                 xgifb_reg_and_or(pVBInfo->P3c4, 0x31, ~0x30, value);
6603                 xgifb_reg_set(pVBInfo->P3c4,
6604                               0x2B,
6605                               pVBInfo->XG21_LVDSCapList[lvdstableindex].
6606                                                 VCLKData1);
6607                 xgifb_reg_set(pVBInfo->P3c4,
6608                               0x2C,
6609                               pVBInfo->XG21_LVDSCapList[lvdstableindex].
6610                                                 VCLKData2);
6611                 value += 0x10;
6612         }
6613
6614         if (!(modeflag & Charx8Dot)) {
6615                 inb(pVBInfo->P3da); /* reset 3da */
6616                 outb(0x13, pVBInfo->P3c0); /* set index */
6617                 /* set data, panning = 0, shift left 1 dot*/
6618                 outb(0x00, pVBInfo->P3c0);
6619
6620                 inb(pVBInfo->P3da); /* Enable Attribute */
6621                 outb(0x20, pVBInfo->P3c0);
6622
6623                 inb(pVBInfo->P3da); /* reset 3da */
6624         }
6625
6626 }
6627
6628 /* no shadow case */
6629 static void XGI_SetXG27LVDSPara(unsigned short ModeNo,
6630                                 unsigned short ModeIdIndex,
6631                                 struct vb_device_info *pVBInfo)
6632 {
6633         unsigned char temp, Miscdata;
6634         unsigned short xres, yres, modeflag, resindex, lvdstableindex;
6635         unsigned short LVDSHT, LVDSHBS, LVDSHRS, LVDSHRE, LVDSHBE;
6636         unsigned short LVDSVT, LVDSVBS, LVDSVRS, LVDSVRE, LVDSVBE;
6637         unsigned short value;
6638
6639         lvdstableindex = XGI_GetLVDSOEMTableIndex(pVBInfo);
6640         temp = (unsigned char) ((pVBInfo->XG21_LVDSCapList[lvdstableindex].
6641                                         LVDS_Capability &
6642                                 (LCDPolarity << 8)) >> 8);
6643         temp &= LCDPolarity;
6644         Miscdata = (unsigned char) inb(pVBInfo->P3cc);
6645
6646         outb((Miscdata & 0x3F) | temp, pVBInfo->P3c2);
6647
6648         temp = (unsigned char) (pVBInfo->XG21_LVDSCapList[lvdstableindex].
6649                                         LVDS_Capability & LCDPolarity);
6650         /* SR35[7] FP VSync polarity */
6651         xgifb_reg_and_or(pVBInfo->P3c4, 0x35, ~0x80, temp & 0x80);
6652         /* SR30[5] FP HSync polarity */
6653         xgifb_reg_and_or(pVBInfo->P3c4, 0x30, ~0x20, (temp & 0x40) >> 1);
6654
6655         XGI_SetXG27FPBits(pVBInfo);
6656         resindex = XGI_GetResInfo(ModeNo, ModeIdIndex, pVBInfo);
6657         if (ModeNo <= 0x13) {
6658                 xres = pVBInfo->StResInfo[resindex].HTotal;
6659                 yres = pVBInfo->StResInfo[resindex].VTotal;
6660                 /* si+St_ResInfo */
6661                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
6662         } else {
6663                 xres = pVBInfo->ModeResInfo[resindex].HTotal; /* xres->ax */
6664                 yres = pVBInfo->ModeResInfo[resindex].VTotal; /* yres->bx */
6665                 /* si+St_ModeFlag */
6666                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
6667         }
6668
6669         if (!(modeflag & Charx8Dot))
6670                 xres = xres * 8 / 9;
6671
6672         LVDSHT = pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSHT;
6673
6674         LVDSHBS = xres + (pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSHDE
6675                         - xres) / 2;
6676         if ((ModeNo <= 0x13) && (modeflag & HalfDCLK))
6677                 LVDSHBS -= xres / 4;
6678
6679         if (LVDSHBS > LVDSHT)
6680                 LVDSHBS -= LVDSHT;
6681
6682         LVDSHRS = LVDSHBS + pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSHFP;
6683         if (LVDSHRS > LVDSHT)
6684                 LVDSHRS -= LVDSHT;
6685
6686         LVDSHRE = LVDSHRS + pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSHSYNC;
6687         if (LVDSHRE > LVDSHT)
6688                 LVDSHRE -= LVDSHT;
6689
6690         LVDSHBE = LVDSHBS + LVDSHT
6691                         - pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSHDE;
6692
6693         LVDSVT = pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSVT;
6694
6695         LVDSVBS = yres + (pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSVDE
6696                         - yres) / 2;
6697         if ((ModeNo > 0x13) && (modeflag & DoubleScanMode))
6698                 LVDSVBS += yres / 2;
6699
6700         if (LVDSVBS > LVDSVT)
6701                 LVDSVBS -= LVDSVT;
6702
6703         LVDSVRS = LVDSVBS + pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSVFP;
6704         if (LVDSVRS > LVDSVT)
6705                 LVDSVRS -= LVDSVT;
6706
6707         LVDSVRE = LVDSVRS + pVBInfo->XG21_LVDSCapList[lvdstableindex].
6708                                         LVDSVSYNC;
6709         if (LVDSVRE > LVDSVT)
6710                 LVDSVRE -= LVDSVT;
6711
6712         LVDSVBE = LVDSVBS + LVDSVT
6713                         - pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSVDE;
6714
6715         temp = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x11);
6716         xgifb_reg_set(pVBInfo->P3d4, 0x11, temp & 0x7f); /* Unlock CRTC */
6717
6718         if (!(modeflag & Charx8Dot))
6719                 xgifb_reg_or(pVBInfo->P3c4, 0x1, 0x1);
6720
6721         /* HT SR0B[1:0] CR00 */
6722         value = (LVDSHT >> 3) - 5;
6723         xgifb_reg_and_or(pVBInfo->P3c4, 0x0B, ~0x03, (value & 0x300) >> 8);
6724         xgifb_reg_set(pVBInfo->P3d4, 0x0, (value & 0xFF));
6725
6726         /* HBS SR0B[5:4] CR02 */
6727         value = (LVDSHBS >> 3) - 1;
6728         xgifb_reg_and_or(pVBInfo->P3c4, 0x0B, ~0x30, (value & 0x300) >> 4);
6729         xgifb_reg_set(pVBInfo->P3d4, 0x2, (value & 0xFF));
6730
6731         /* HBE SR0C[1:0] CR05[7] CR03[4:0] */
6732         value = (LVDSHBE >> 3) - 1;
6733         xgifb_reg_and_or(pVBInfo->P3c4, 0x0C, ~0x03, (value & 0xC0) >> 6);
6734         xgifb_reg_and_or(pVBInfo->P3d4, 0x05, ~0x80, (value & 0x20) << 2);
6735         xgifb_reg_and_or(pVBInfo->P3d4, 0x03, ~0x1F, value & 0x1F);
6736
6737         /* HRS SR0B[7:6] CR04 */
6738         value = (LVDSHRS >> 3) + 2;
6739         xgifb_reg_and_or(pVBInfo->P3c4, 0x0B, ~0xC0, (value & 0x300) >> 2);
6740         xgifb_reg_set(pVBInfo->P3d4, 0x4, (value & 0xFF));
6741
6742         /* Panel HRS SR2F[1:0] SR2E[7:0]  */
6743         value--;
6744         xgifb_reg_and_or(pVBInfo->P3c4, 0x2F, ~0x03, (value & 0x300) >> 8);
6745         xgifb_reg_set(pVBInfo->P3c4, 0x2E, (value & 0xFF));
6746
6747         /* HRE SR0C[2] CR05[4:0] */
6748         value = (LVDSHRE >> 3) + 2;
6749         xgifb_reg_and_or(pVBInfo->P3c4, 0x0C, ~0x04, (value & 0x20) >> 3);
6750         xgifb_reg_and_or(pVBInfo->P3d4, 0x05, ~0x1F, value & 0x1F);
6751
6752         /* Panel HRE SR2F[7:2]  */
6753         value--;
6754         xgifb_reg_and_or(pVBInfo->P3c4, 0x2F, ~0xFC, value << 2);
6755
6756         /* VT SR0A[0] CR07[5][0] CR06 */
6757         value = LVDSVT - 2;
6758         xgifb_reg_and_or(pVBInfo->P3c4, 0x0A, ~0x01, (value & 0x400) >> 10);
6759         xgifb_reg_and_or(pVBInfo->P3d4, 0x07, ~0x20, (value & 0x200) >> 4);
6760         xgifb_reg_and_or(pVBInfo->P3d4, 0x07, ~0x01, (value & 0x100) >> 8);
6761         xgifb_reg_set(pVBInfo->P3d4, 0x06, (value & 0xFF));
6762
6763         /* VBS SR0A[2] CR09[5] CR07[3] CR15 */
6764         value = LVDSVBS - 1;
6765         xgifb_reg_and_or(pVBInfo->P3c4, 0x0A, ~0x04, (value & 0x400) >> 8);
6766         xgifb_reg_and_or(pVBInfo->P3d4, 0x09, ~0x20, (value & 0x200) >> 4);
6767         xgifb_reg_and_or(pVBInfo->P3d4, 0x07, ~0x08, (value & 0x100) >> 5);
6768         xgifb_reg_set(pVBInfo->P3d4, 0x15, (value & 0xFF));
6769
6770         /* VBE SR0A[4] CR16 */
6771         value = LVDSVBE - 1;
6772         xgifb_reg_and_or(pVBInfo->P3c4, 0x0A, ~0x10, (value & 0x100) >> 4);
6773         xgifb_reg_set(pVBInfo->P3d4, 0x16, (value & 0xFF));
6774
6775         /* VRS SR0A[3] CR7[7][2] CR10 */
6776         value = LVDSVRS - 1;
6777         xgifb_reg_and_or(pVBInfo->P3c4, 0x0A, ~0x08, (value & 0x400) >> 7);
6778         xgifb_reg_and_or(pVBInfo->P3d4, 0x07, ~0x80, (value & 0x200) >> 2);
6779         xgifb_reg_and_or(pVBInfo->P3d4, 0x07, ~0x04, (value & 0x100) >> 6);
6780         xgifb_reg_set(pVBInfo->P3d4, 0x10, (value & 0xFF));
6781
6782         /* Panel VRS SR35[2:0] SR34[7:0] */
6783         xgifb_reg_and_or(pVBInfo->P3c4, 0x35, ~0x07, (value & 0x700) >> 8);
6784         xgifb_reg_set(pVBInfo->P3c4, 0x34, value & 0xFF);
6785
6786         /* VRE SR0A[5] CR11[3:0] */
6787         value = LVDSVRE - 1;
6788         xgifb_reg_and_or(pVBInfo->P3c4, 0x0A, ~0x20, (value & 0x10) << 1);
6789         xgifb_reg_and_or(pVBInfo->P3d4, 0x11, ~0x0F, value & 0x0F);
6790
6791         /* Panel VRE SR3F[7:2] */
6792         xgifb_reg_and_or(pVBInfo->P3c4, 0x3F, ~0xFC, (value << 2) & 0xFC);
6793
6794         for (temp = 0, value = 0; temp < 3; temp++) {
6795
6796                 xgifb_reg_and_or(pVBInfo->P3c4, 0x31, ~0x30, value);
6797                 xgifb_reg_set(pVBInfo->P3c4,
6798                               0x2B,
6799                               pVBInfo->XG21_LVDSCapList[lvdstableindex].
6800                                         VCLKData1);
6801                 xgifb_reg_set(pVBInfo->P3c4,
6802                               0x2C,
6803                               pVBInfo->XG21_LVDSCapList[lvdstableindex].
6804                                         VCLKData2);
6805                 value += 0x10;
6806         }
6807
6808         if (!(modeflag & Charx8Dot)) {
6809                 inb(pVBInfo->P3da); /* reset 3da */
6810                 outb(0x13, pVBInfo->P3c0); /* set index */
6811                 /* set data, panning = 0, shift left 1 dot*/
6812                 outb(0x00, pVBInfo->P3c0);
6813
6814                 inb(pVBInfo->P3da); /* Enable Attribute */
6815                 outb(0x20, pVBInfo->P3c0);
6816
6817                 inb(pVBInfo->P3da); /* reset 3da */
6818         }
6819
6820 }
6821
6822 /* --------------------------------------------------------------------- */
6823 /* Function : XGI_IsLCDON */
6824 /* Input : */
6825 /* Output : 0 : Skip PSC Control */
6826 /* 1: Disable PSC */
6827 /* Description : */
6828 /* --------------------------------------------------------------------- */
6829 static unsigned char XGI_IsLCDON(struct vb_device_info *pVBInfo)
6830 {
6831         unsigned short tempax;
6832
6833         tempax = pVBInfo->VBInfo;
6834         if (tempax & SetCRT2ToDualEdge)
6835                 return 0;
6836         else if (tempax & (DisableCRT2Display | SwitchToCRT2 | SetSimuScanMode))
6837                 return 1;
6838
6839         return 0;
6840 }
6841
6842 /* --------------------------------------------------------------------- */
6843 /* Function : XGI_DisableChISLCD */
6844 /* Input : */
6845 /* Output : 0 -> Not LCD Mode */
6846 /* Description : */
6847 /* --------------------------------------------------------------------- */
6848 static unsigned char XGI_DisableChISLCD(struct vb_device_info *pVBInfo)
6849 {
6850         unsigned short tempbx, tempah;
6851
6852         tempbx = pVBInfo->SetFlag & (DisableChA | DisableChB);
6853         tempah = ~((unsigned short) xgifb_reg_get(pVBInfo->Part1Port, 0x2E));
6854
6855         if (tempbx & (EnableChA | DisableChA)) {
6856                 if (!(tempah & 0x08)) /* Chk LCDA Mode */
6857                         return 0;
6858         }
6859
6860         if (!(tempbx & (EnableChB | DisableChB)))
6861                 return 0;
6862
6863         if (tempah & 0x01) /* Chk LCDB Mode */
6864                 return 1;
6865
6866         return 0;
6867 }
6868
6869 /* --------------------------------------------------------------------- */
6870 /* Function : XGI_EnableChISLCD */
6871 /* Input : */
6872 /* Output : 0 -> Not LCD mode */
6873 /* Description : */
6874 /* --------------------------------------------------------------------- */
6875 static unsigned char XGI_EnableChISLCD(struct vb_device_info *pVBInfo)
6876 {
6877         unsigned short tempbx, tempah;
6878
6879         tempbx = pVBInfo->SetFlag & (EnableChA | EnableChB);
6880         tempah = ~((unsigned short) xgifb_reg_get(pVBInfo->Part1Port, 0x2E));
6881
6882         if (tempbx & (EnableChA | DisableChA)) {
6883                 if (!(tempah & 0x08)) /* Chk LCDA Mode */
6884                         return 0;
6885         }
6886
6887         if (!(tempbx & (EnableChB | DisableChB)))
6888                 return 0;
6889
6890         if (tempah & 0x01) /* Chk LCDB Mode */
6891                 return 1;
6892
6893         return 0;
6894 }
6895
6896 void XGI_DisableBridge(struct xgi_hw_device_info *HwDeviceExtension,
6897                 struct vb_device_info *pVBInfo)
6898 {
6899         unsigned short tempah = 0;
6900
6901         if (pVBInfo->SetFlag == Win9xDOSMode)
6902                 return;
6903
6904         if (pVBInfo->VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV
6905                         | VB_XGI302LV | VB_XGI301C)) {
6906                 tempah = 0x3F;
6907                 if (!(pVBInfo->VBInfo &
6908                     (DisableCRT2Display | SetSimuScanMode))) {
6909                         if (pVBInfo->VBInfo & SetCRT2ToLCDA) {
6910                                 if (pVBInfo->VBInfo & SetCRT2ToDualEdge) {
6911                                         tempah = 0x7F; /* Disable Channel A */
6912                                         if (!(pVBInfo->VBInfo & SetCRT2ToLCDA))
6913                                                 /* Disable Channel B */
6914                                                 tempah = 0xBF;
6915
6916                                         if (pVBInfo->SetFlag & DisableChB)
6917                                                 /* force to disable Cahnnel */
6918                                                 tempah &= 0xBF;
6919
6920                                         if (pVBInfo->SetFlag & DisableChA)
6921                                                 /* Force to disable Channel B */
6922                                                 tempah &= 0x7F;
6923                                 }
6924                         }
6925                 }
6926
6927                 /* disable part4_1f */
6928                 xgifb_reg_and(pVBInfo->Part4Port, 0x1F, tempah);
6929
6930                 if (pVBInfo->VBType & (VB_XGI302LV | VB_XGI301C)) {
6931                         if (((pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)))
6932                                         || (XGI_DisableChISLCD(pVBInfo))
6933                                         || (XGI_IsLCDON(pVBInfo)))
6934                                 /* LVDS Driver power down */
6935                                 xgifb_reg_or(pVBInfo->Part4Port, 0x30, 0x80);
6936                 }
6937
6938                 if ((pVBInfo->SetFlag & DisableChA) || (pVBInfo->VBInfo
6939                                 & (DisableCRT2Display | SetCRT2ToLCDA
6940                                                 | SetSimuScanMode))) {
6941                         if (pVBInfo->SetFlag & GatingCRT)
6942                                 XGI_EnableGatingCRT(HwDeviceExtension, pVBInfo);
6943                         XGI_DisplayOff(HwDeviceExtension, pVBInfo);
6944                 }
6945
6946                 if (pVBInfo->VBInfo & SetCRT2ToLCDA) {
6947                         if ((pVBInfo->SetFlag & DisableChA) || (pVBInfo->VBInfo
6948                                         & SetCRT2ToLCDA))
6949                                 /* Power down */
6950                                 xgifb_reg_and(pVBInfo->Part1Port, 0x1e, 0xdf);
6951                 }
6952
6953                 /* disable TV as primary VGA swap */
6954                 xgifb_reg_and(pVBInfo->P3c4, 0x32, 0xdf);
6955
6956                 if ((pVBInfo->VBInfo & (SetSimuScanMode | SetCRT2ToDualEdge)))
6957                         xgifb_reg_and(pVBInfo->Part2Port, 0x00, 0xdf);
6958
6959                 if ((pVBInfo->SetFlag & DisableChB) ||
6960                     (pVBInfo->VBInfo &
6961                         (DisableCRT2Display | SetSimuScanMode)) ||
6962                     ((!(pVBInfo->VBInfo & SetCRT2ToLCDA)) &&
6963                     (pVBInfo->VBInfo &
6964                         (SetCRT2ToRAMDAC | SetCRT2ToLCD | SetCRT2ToTV))))
6965                         /* BScreenOff=1 */
6966                         xgifb_reg_or(pVBInfo->Part1Port, 0x00, 0x80);
6967
6968                 if ((pVBInfo->SetFlag & DisableChB) ||
6969                     (pVBInfo->VBInfo &
6970                         (DisableCRT2Display | SetSimuScanMode)) ||
6971                     (!(pVBInfo->VBInfo & SetCRT2ToLCDA)) ||
6972                     (pVBInfo->VBInfo &
6973                         (SetCRT2ToRAMDAC | SetCRT2ToLCD | SetCRT2ToTV))) {
6974                         /* save Part1 index 0 */
6975                         tempah = xgifb_reg_get(pVBInfo->Part1Port, 0x00);
6976                         /* BTDAC = 1, avoid VB reset */
6977                         xgifb_reg_or(pVBInfo->Part1Port, 0x00, 0x10);
6978                         /* disable CRT2 */
6979                         xgifb_reg_and(pVBInfo->Part1Port, 0x1E, 0xDF);
6980                         /* restore Part1 index 0 */
6981                         xgifb_reg_set(pVBInfo->Part1Port, 0x00, tempah);
6982                 }
6983         } else { /* {301} */
6984                 if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToTV)) {
6985                         /* BScreenOff=1 */
6986                         xgifb_reg_or(pVBInfo->Part1Port, 0x00, 0x80);
6987                         /* Disable CRT2 */
6988                         xgifb_reg_and(pVBInfo->Part1Port, 0x1E, 0xDF);
6989                         /* Disable TV asPrimary VGA swap */
6990                         xgifb_reg_and(pVBInfo->P3c4, 0x32, 0xDF);
6991                 }
6992
6993                 if (pVBInfo->VBInfo & (DisableCRT2Display | SetCRT2ToLCDA
6994                                 | SetSimuScanMode))
6995                         XGI_DisplayOff(HwDeviceExtension, pVBInfo);
6996         }
6997 }
6998
6999 /* --------------------------------------------------------------------- */
7000 /* Function : XGI_GetTVPtrIndex */
7001 /* Input : */
7002 /* Output : */
7003 /* Description : bx 0 : ExtNTSC */
7004 /* 1 : StNTSC */
7005 /* 2 : ExtPAL */
7006 /* 3 : StPAL */
7007 /* 4 : ExtHiTV */
7008 /* 5 : StHiTV */
7009 /* 6 : Ext525i */
7010 /* 7 : St525i */
7011 /* 8 : Ext525p */
7012 /* 9 : St525p */
7013 /* A : Ext750p */
7014 /* B : St750p */
7015 /* --------------------------------------------------------------------- */
7016 static unsigned short XGI_GetTVPtrIndex(struct vb_device_info *pVBInfo)
7017 {
7018         unsigned short tempbx = 0;
7019
7020         if (pVBInfo->TVInfo & SetPALTV)
7021                 tempbx = 2;
7022         if (pVBInfo->TVInfo & SetYPbPrMode1080i)
7023                 tempbx = 4;
7024         if (pVBInfo->TVInfo & SetYPbPrMode525i)
7025                 tempbx = 6;
7026         if (pVBInfo->TVInfo & SetYPbPrMode525p)
7027                 tempbx = 8;
7028         if (pVBInfo->TVInfo & SetYPbPrMode750p)
7029                 tempbx = 10;
7030         if (pVBInfo->TVInfo & TVSimuMode)
7031                 tempbx++;
7032
7033         return tempbx;
7034 }
7035
7036 /* --------------------------------------------------------------------- */
7037 /* Function : XGI_GetTVPtrIndex2 */
7038 /* Input : */
7039 /* Output : bx 0 : NTSC */
7040 /* 1 : PAL */
7041 /* 2 : PALM */
7042 /* 3 : PALN */
7043 /* 4 : NTSC1024x768 */
7044 /* 5 : PAL-M 1024x768 */
7045 /* 6-7: reserved */
7046 /* cl 0 : YFilter1 */
7047 /* 1 : YFilter2 */
7048 /* ch 0 : 301A */
7049 /* 1 : 301B/302B/301LV/302LV */
7050 /* Description : */
7051 /* --------------------------------------------------------------------- */
7052 static void XGI_GetTVPtrIndex2(unsigned short *tempbx, unsigned char *tempcl,
7053                 unsigned char *tempch, struct vb_device_info *pVBInfo)
7054 {
7055         *tempbx = 0;
7056         *tempcl = 0;
7057         *tempch = 0;
7058
7059         if (pVBInfo->TVInfo & SetPALTV)
7060                 *tempbx = 1;
7061
7062         if (pVBInfo->TVInfo & SetPALMTV)
7063                 *tempbx = 2;
7064
7065         if (pVBInfo->TVInfo & SetPALNTV)
7066                 *tempbx = 3;
7067
7068         if (pVBInfo->TVInfo & NTSC1024x768) {
7069                 *tempbx = 4;
7070                 if (pVBInfo->TVInfo & SetPALMTV)
7071                         *tempbx = 5;
7072         }
7073
7074         if (pVBInfo->VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV
7075                         | VB_XGI302LV | VB_XGI301C)) {
7076                 if ((!(pVBInfo->VBInfo & SetInSlaveMode)) || (pVBInfo->TVInfo
7077                                 & TVSimuMode)) {
7078                         *tempbx += 8;
7079                         *tempcl += 1;
7080                 }
7081         }
7082
7083         if (pVBInfo->VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV
7084                         | VB_XGI302LV | VB_XGI301C))
7085                 (*tempch)++;
7086 }
7087
7088 static void XGI_SetDelayComp(struct vb_device_info *pVBInfo)
7089 {
7090         unsigned short index;
7091
7092         unsigned char tempah, tempbl, tempbh;
7093
7094         if (pVBInfo->VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV
7095                         | VB_XGI302LV | VB_XGI301C)) {
7096                 if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA
7097                                 | SetCRT2ToTV | SetCRT2ToRAMDAC)) {
7098                         tempbl = 0;
7099                         tempbh = 0;
7100
7101                         index = XGI_GetTVPtrIndex(pVBInfo); /* Get TV Delay */
7102                         tempbl = pVBInfo->XGI_TVDelayList[index];
7103
7104                         if (pVBInfo->VBType & (VB_XGI301B | VB_XGI302B
7105                                         | VB_XGI301LV | VB_XGI302LV
7106                                         | VB_XGI301C))
7107                                 tempbl = pVBInfo->XGI_TVDelayList2[index];
7108
7109                         if (pVBInfo->VBInfo & SetCRT2ToDualEdge)
7110                                 tempbl = tempbl >> 4;
7111                         /*
7112                         if (pVBInfo->VBInfo & SetCRT2ToRAMDAC)
7113                                 tempbl = CRT2Delay1;    // Get CRT2 Delay
7114                         if (pVBInfo->VBType &
7115                             (VB_XGI301B |
7116                              VB_XGI302B |
7117                              VB_XGI301LV |
7118                              VB_XGI302LV |
7119                              VB_XGI301C))
7120                                 tempbl = CRT2Delay2;
7121                         */
7122                         if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
7123                                 /* Get LCD Delay */
7124                                 index = XGI_GetLCDCapPtr(pVBInfo);
7125                                 tempbh = pVBInfo->LCDCapList[index].
7126                                                 LCD_DelayCompensation;
7127
7128                                 if (!(pVBInfo->VBInfo & SetCRT2ToLCDA))
7129                                         tempbl = tempbh;
7130                         }
7131
7132                         tempbl &= 0x0F;
7133                         tempbh &= 0xF0;
7134                         tempah = xgifb_reg_get(pVBInfo->Part1Port, 0x2D);
7135
7136                         if (pVBInfo->VBInfo & (SetCRT2ToRAMDAC | SetCRT2ToLCD
7137                                         | SetCRT2ToTV)) { /* Channel B */
7138                                 tempah &= 0xF0;
7139                                 tempah |= tempbl;
7140                         }
7141
7142                         if (pVBInfo->VBInfo & SetCRT2ToLCDA) { /* Channel A */
7143                                 tempah &= 0x0F;
7144                                 tempah |= tempbh;
7145                         }
7146                         xgifb_reg_set(pVBInfo->Part1Port, 0x2D, tempah);
7147                 }
7148         } else if (pVBInfo->IF_DEF_LVDS == 1) {
7149                 tempbl = 0;
7150                 tempbh = 0;
7151                 if (pVBInfo->VBInfo & SetCRT2ToLCD) {
7152                         /* / Get LCD Delay */
7153                         tempah = pVBInfo->LCDCapList[
7154                                         XGI_GetLCDCapPtr(pVBInfo)].
7155                                                 LCD_DelayCompensation;
7156                         tempah &= 0x0f;
7157                         tempah = tempah << 4;
7158                         xgifb_reg_and_or(pVBInfo->Part1Port, 0x2D, 0x0f,
7159                                         tempah);
7160                 }
7161         }
7162 }
7163
7164 static void XGI_SetLCDCap_A(unsigned short tempcx,
7165                             struct vb_device_info *pVBInfo)
7166 {
7167         unsigned short temp;
7168
7169         temp = xgifb_reg_get(pVBInfo->P3d4, 0x37);
7170
7171         if (temp & LCDRGB18Bit) {
7172                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x19, 0x0F,
7173                                 /* Enable Dither */
7174                                 (unsigned short) (0x20 | (tempcx & 0x00C0)));
7175                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x1A, 0x7F, 0x80);
7176         } else {
7177                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x19, 0x0F,
7178                                 (unsigned short) (0x30 | (tempcx & 0x00C0)));
7179                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x1A, 0x7F, 0x00);
7180         }
7181
7182         /*
7183         if (tempcx & EnableLCD24bpp) {  // 24bits
7184                 xgifb_reg_and_or(pVBInfo->Part1Port,
7185                                  0x19,
7186                                  0x0F,
7187                                  (unsigned short)(0x30 | (tempcx&0x00C0)));
7188                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x1A, 0x7F, 0x00);
7189         } else {
7190                 xgifb_reg_and_or(pVBInfo->Part1Port,
7191                                  0x19,
7192                                  0x0F,
7193                                  // Enable Dither
7194                                  (unsigned short)(0x20 | (tempcx&0x00C0)));
7195                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x1A, 0x7F, 0x80);
7196         }
7197         */
7198 }
7199
7200 /* --------------------------------------------------------------------- */
7201 /* Function : XGI_SetLCDCap_B */
7202 /* Input : cx -> LCD Capability */
7203 /* Output : */
7204 /* Description : */
7205 /* --------------------------------------------------------------------- */
7206 static void XGI_SetLCDCap_B(unsigned short tempcx,
7207                             struct vb_device_info *pVBInfo)
7208 {
7209         if (tempcx & EnableLCD24bpp) /* 24bits */
7210                 xgifb_reg_and_or(pVBInfo->Part2Port, 0x1A, 0xE0,
7211                                 (unsigned short) (((tempcx & 0x00ff) >> 6)
7212                                                 | 0x0c));
7213         else
7214                 xgifb_reg_and_or(pVBInfo->Part2Port, 0x1A, 0xE0,
7215                                 (unsigned short) (((tempcx & 0x00ff) >> 6)
7216                                                 | 0x18)); /* Enable Dither */
7217 }
7218
7219 static void SetSpectrum(struct vb_device_info *pVBInfo)
7220 {
7221         unsigned short index;
7222
7223         index = XGI_GetLCDCapPtr(pVBInfo);
7224
7225         /* disable down spectrum D[4] */
7226         xgifb_reg_and(pVBInfo->Part4Port, 0x30, 0x8F);
7227         XGI_LongWait(pVBInfo);
7228         xgifb_reg_or(pVBInfo->Part4Port, 0x30, 0x20); /* reset spectrum */
7229         XGI_LongWait(pVBInfo);
7230
7231         xgifb_reg_set(pVBInfo->Part4Port, 0x31,
7232                         pVBInfo->LCDCapList[index].Spectrum_31);
7233         xgifb_reg_set(pVBInfo->Part4Port, 0x32,
7234                         pVBInfo->LCDCapList[index].Spectrum_32);
7235         xgifb_reg_set(pVBInfo->Part4Port, 0x33,
7236                         pVBInfo->LCDCapList[index].Spectrum_33);
7237         xgifb_reg_set(pVBInfo->Part4Port, 0x34,
7238                         pVBInfo->LCDCapList[index].Spectrum_34);
7239         XGI_LongWait(pVBInfo);
7240         xgifb_reg_or(pVBInfo->Part4Port, 0x30, 0x40); /* enable spectrum */
7241 }
7242
7243 static void XGI_SetLCDCap(struct vb_device_info *pVBInfo)
7244 {
7245         unsigned short tempcx;
7246
7247         tempcx = pVBInfo->LCDCapList[XGI_GetLCDCapPtr(pVBInfo)].LCD_Capability;
7248
7249         if (pVBInfo->VBType &
7250             (VB_XGI301B |
7251              VB_XGI302B |
7252              VB_XGI301LV |
7253              VB_XGI302LV |
7254              VB_XGI301C)) { /* 301LV/302LV only */
7255                 if (pVBInfo->VBType &
7256                     (VB_XGI301LV | VB_XGI302LV | VB_XGI301C)) {
7257                         /* Set 301LV Capability */
7258                         xgifb_reg_set(pVBInfo->Part4Port, 0x24,
7259                                         (unsigned char) (tempcx & 0x1F));
7260                 }
7261                 /* VB Driving */
7262                 xgifb_reg_and_or(pVBInfo->Part4Port, 0x0D,
7263                                 ~((EnableVBCLKDRVLOW | EnablePLLSPLOW) >> 8),
7264                                 (unsigned short) ((tempcx & (EnableVBCLKDRVLOW
7265                                                 | EnablePLLSPLOW)) >> 8));
7266         }
7267
7268         if (pVBInfo->VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV
7269                         | VB_XGI302LV | VB_XGI301C)) {
7270                 if (pVBInfo->VBInfo & SetCRT2ToLCD)
7271                         XGI_SetLCDCap_B(tempcx, pVBInfo);
7272                 else if (pVBInfo->VBInfo & SetCRT2ToLCDA)
7273                         XGI_SetLCDCap_A(tempcx, pVBInfo);
7274
7275                 if (pVBInfo->VBType & (VB_XGI302LV | VB_XGI301C)) {
7276                         if (tempcx & EnableSpectrum)
7277                                 SetSpectrum(pVBInfo);
7278                 }
7279         } else {
7280                 /* LVDS,CH7017 */
7281                 XGI_SetLCDCap_A(tempcx, pVBInfo);
7282         }
7283 }
7284
7285 /* --------------------------------------------------------------------- */
7286 /* Function : XGI_SetAntiFlicker */
7287 /* Input : */
7288 /* Output : */
7289 /* Description : Set TV Customized Param. */
7290 /* --------------------------------------------------------------------- */
7291 static void XGI_SetAntiFlicker(unsigned short ModeNo,
7292                                unsigned short ModeIdIndex,
7293                                struct vb_device_info *pVBInfo)
7294 {
7295         unsigned short tempbx, index;
7296
7297         unsigned char tempah;
7298
7299         if (pVBInfo->TVInfo & (SetYPbPrMode525p | SetYPbPrMode750p))
7300                 return;
7301
7302         tempbx = XGI_GetTVPtrIndex(pVBInfo);
7303         tempbx &= 0xFE;
7304
7305         if (ModeNo <= 0x13)
7306                 index = pVBInfo->SModeIDTable[ModeIdIndex].VB_StTVFlickerIndex;
7307         else
7308                 index = pVBInfo->EModeIDTable[ModeIdIndex].VB_ExtTVFlickerIndex;
7309
7310         tempbx += index;
7311         tempah = TVAntiFlickList[tempbx];
7312         tempah = tempah << 4;
7313
7314         xgifb_reg_and_or(pVBInfo->Part2Port, 0x0A, 0x8F, tempah);
7315 }
7316
7317 static void XGI_SetEdgeEnhance(unsigned short ModeNo,
7318                                unsigned short ModeIdIndex,
7319                                struct vb_device_info *pVBInfo)
7320 {
7321         unsigned short tempbx, index;
7322
7323         unsigned char tempah;
7324
7325         tempbx = XGI_GetTVPtrIndex(pVBInfo);
7326         tempbx &= 0xFE;
7327
7328         if (ModeNo <= 0x13)
7329                 index = pVBInfo->SModeIDTable[ModeIdIndex].VB_StTVEdgeIndex;
7330         else
7331                 index = pVBInfo->EModeIDTable[ModeIdIndex].VB_ExtTVEdgeIndex;
7332
7333         tempbx += index;
7334         tempah = TVEdgeList[tempbx];
7335         tempah = tempah << 5;
7336
7337         xgifb_reg_and_or(pVBInfo->Part2Port, 0x3A, 0x1F, tempah);
7338 }
7339
7340 static void XGI_SetPhaseIncr(struct vb_device_info *pVBInfo)
7341 {
7342         unsigned short tempbx;
7343
7344         unsigned char tempcl, tempch;
7345
7346         unsigned long tempData;
7347
7348         XGI_GetTVPtrIndex2(&tempbx, &tempcl, &tempch, pVBInfo); /* bx, cl, ch */
7349         tempData = TVPhaseList[tempbx];
7350
7351         xgifb_reg_set(pVBInfo->Part2Port, 0x31, (unsigned short) (tempData
7352                         & 0x000000FF));
7353         xgifb_reg_set(pVBInfo->Part2Port, 0x32, (unsigned short) ((tempData
7354                         & 0x0000FF00) >> 8));
7355         xgifb_reg_set(pVBInfo->Part2Port, 0x33, (unsigned short) ((tempData
7356                         & 0x00FF0000) >> 16));
7357         xgifb_reg_set(pVBInfo->Part2Port, 0x34, (unsigned short) ((tempData
7358                         & 0xFF000000) >> 24));
7359 }
7360
7361 static void XGI_SetYFilter(unsigned short ModeNo, unsigned short ModeIdIndex,
7362                 struct vb_device_info *pVBInfo)
7363 {
7364         unsigned short tempbx, index;
7365
7366         unsigned char tempcl, tempch, tempal, *filterPtr;
7367
7368         XGI_GetTVPtrIndex2(&tempbx, &tempcl, &tempch, pVBInfo); /* bx, cl, ch */
7369
7370         switch (tempbx) {
7371         case 0x00:
7372         case 0x04:
7373                 filterPtr = NTSCYFilter1;
7374                 break;
7375
7376         case 0x01:
7377                 filterPtr = PALYFilter1;
7378                 break;
7379
7380         case 0x02:
7381         case 0x05:
7382         case 0x0D:
7383                 filterPtr = PALMYFilter1;
7384                 break;
7385
7386         case 0x03:
7387                 filterPtr = PALNYFilter1;
7388                 break;
7389
7390         case 0x08:
7391         case 0x0C:
7392                 filterPtr = NTSCYFilter2;
7393                 break;
7394
7395         case 0x0A:
7396                 filterPtr = PALMYFilter2;
7397                 break;
7398
7399         case 0x0B:
7400                 filterPtr = PALNYFilter2;
7401                 break;
7402
7403         case 0x09:
7404                 filterPtr = PALYFilter2;
7405                 break;
7406
7407         default:
7408                 return;
7409         }
7410
7411         if (ModeNo <= 0x13)
7412                 tempal = pVBInfo->SModeIDTable[ModeIdIndex].
7413                                 VB_StTVYFilterIndex;
7414         else
7415                 tempal = pVBInfo->EModeIDTable[ModeIdIndex].
7416                                 VB_ExtTVYFilterIndex;
7417
7418         if (tempcl == 0)
7419                 index = tempal * 4;
7420         else
7421                 index = tempal * 7;
7422
7423         if ((tempcl == 0) && (tempch == 1)) {
7424                 xgifb_reg_set(pVBInfo->Part2Port, 0x35, 0);
7425                 xgifb_reg_set(pVBInfo->Part2Port, 0x36, 0);
7426                 xgifb_reg_set(pVBInfo->Part2Port, 0x37, 0);
7427                 xgifb_reg_set(pVBInfo->Part2Port, 0x38, filterPtr[index++]);
7428         } else {
7429                 xgifb_reg_set(pVBInfo->Part2Port, 0x35, filterPtr[index++]);
7430                 xgifb_reg_set(pVBInfo->Part2Port, 0x36, filterPtr[index++]);
7431                 xgifb_reg_set(pVBInfo->Part2Port, 0x37, filterPtr[index++]);
7432                 xgifb_reg_set(pVBInfo->Part2Port, 0x38, filterPtr[index++]);
7433         }
7434
7435         if (pVBInfo->VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV
7436                         | VB_XGI302LV | VB_XGI301C)) {
7437                 xgifb_reg_set(pVBInfo->Part2Port, 0x48, filterPtr[index++]);
7438                 xgifb_reg_set(pVBInfo->Part2Port, 0x49, filterPtr[index++]);
7439                 xgifb_reg_set(pVBInfo->Part2Port, 0x4A, filterPtr[index++]);
7440         }
7441 }
7442
7443 /* --------------------------------------------------------------------- */
7444 /* Function : XGI_OEM310Setting */
7445 /* Input : */
7446 /* Output : */
7447 /* Description : Customized Param. for 301 */
7448 /* --------------------------------------------------------------------- */
7449 static void XGI_OEM310Setting(unsigned short ModeNo,
7450                               unsigned short ModeIdIndex,
7451                               struct vb_device_info *pVBInfo)
7452 {
7453         if (pVBInfo->SetFlag & Win9xDOSMode)
7454                 return;
7455
7456         /* GetPart1IO(); */
7457         XGI_SetDelayComp(pVBInfo);
7458
7459         if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA))
7460                 XGI_SetLCDCap(pVBInfo);
7461
7462         if (pVBInfo->VBInfo & SetCRT2ToTV) {
7463                 /* GetPart2IO() */
7464                 XGI_SetPhaseIncr(pVBInfo);
7465                 XGI_SetYFilter(ModeNo, ModeIdIndex, pVBInfo);
7466                 XGI_SetAntiFlicker(ModeNo, ModeIdIndex, pVBInfo);
7467
7468                 if (pVBInfo->VBType & VB_XGI301)
7469                         XGI_SetEdgeEnhance(ModeNo, ModeIdIndex, pVBInfo);
7470         }
7471 }
7472
7473 /* --------------------------------------------------------------------- */
7474 /* Function : XGI_SetCRT2ModeRegs */
7475 /* Input : */
7476 /* Output : */
7477 /* Description : Origin code for crt2group */
7478 /* --------------------------------------------------------------------- */
7479 void XGI_SetCRT2ModeRegs(unsigned short ModeNo,
7480                 struct xgi_hw_device_info *HwDeviceExtension,
7481                 struct vb_device_info *pVBInfo)
7482 {
7483         unsigned short tempbl;
7484         short tempcl;
7485
7486         unsigned char tempah;
7487
7488         /* // fix write part1 index 0 BTDRAM bit Bug
7489          * xgifb_reg_set(pVBInfo->Part1Port, 0x03, 0x00); */
7490         tempah = 0;
7491         if (!(pVBInfo->VBInfo & DisableCRT2Display)) {
7492                 tempah = xgifb_reg_get(pVBInfo->Part1Port, 0x00);
7493                 tempah &= ~0x10; /* BTRAMDAC */
7494                 tempah |= 0x40; /* BTRAM */
7495
7496                 if (pVBInfo->VBInfo & (SetCRT2ToRAMDAC | SetCRT2ToTV
7497                                 | SetCRT2ToLCD)) {
7498                         tempah = 0x40; /* BTDRAM */
7499                         if (ModeNo > 0x13) {
7500                                 tempcl = pVBInfo->ModeType;
7501                                 tempcl -= ModeVGA;
7502                                 if (tempcl >= 0) {
7503                                         /* BT Color */
7504                                         tempah = (0x008 >> tempcl);
7505                                         if (tempah == 0)
7506                                                 tempah = 1;
7507                                         tempah |= 0x040;
7508                                 }
7509                         }
7510                         if (pVBInfo->VBInfo & SetInSlaveMode)
7511                                 tempah ^= 0x50; /* BTDAC */
7512                 }
7513         }
7514
7515         /* 0210 shampoo
7516         if (pVBInfo->VBInfo & DisableCRT2Display) {
7517                 tempah = 0;
7518         }
7519
7520         xgifb_reg_set(pVBInfo->Part1Port, 0x00, tempah);
7521         if (pVBInfo->VBInfo & (SetCRT2ToRAMDAC | SetCRT2ToTV | SetCRT2ToLCD)) {
7522                 tempcl = pVBInfo->ModeType;
7523                 if (ModeNo > 0x13) {
7524                         tempcl -= ModeVGA;
7525                         if ((tempcl > 0) || (tempcl == 0)) {
7526                                 tempah=(0x008>>tempcl) ;
7527                                 if (tempah == 0)
7528                                         tempah = 1;
7529                                 tempah |= 0x040;
7530                         }
7531                 } else {
7532                         tempah = 0x040;
7533                 }
7534
7535                 if (pVBInfo->VBInfo & SetInSlaveMode) {
7536                         tempah = (tempah ^ 0x050);
7537                 }
7538         }
7539         */
7540
7541         xgifb_reg_set(pVBInfo->Part1Port, 0x00, tempah);
7542         tempah = 0x08;
7543         tempbl = 0xf0;
7544
7545         if (pVBInfo->VBInfo & DisableCRT2Display) {
7546                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x2e, tempbl, tempah);
7547         } else {
7548                 tempah = 0x00;
7549                 tempbl = 0xff;
7550
7551                 if (pVBInfo->VBInfo & (SetCRT2ToRAMDAC | SetCRT2ToTV
7552                                 | SetCRT2ToLCD | SetCRT2ToLCDA)) {
7553                         if ((pVBInfo->VBInfo & SetCRT2ToLCDA) &&
7554                             (!(pVBInfo->VBInfo & SetSimuScanMode))) {
7555                                 tempbl &= 0xf7;
7556                                 tempah |= 0x01;
7557                                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x2e,
7558                                                 tempbl, tempah);
7559                         } else {
7560                                 if (pVBInfo->VBInfo & SetCRT2ToLCDA) {
7561                                         tempbl &= 0xf7;
7562                                         tempah |= 0x01;
7563                                 }
7564
7565                                 if (pVBInfo->VBInfo &
7566                                     (SetCRT2ToRAMDAC |
7567                                      SetCRT2ToTV |
7568                                      SetCRT2ToLCD)) {
7569                                         tempbl &= 0xf8;
7570                                         tempah = 0x01;
7571
7572                                         if (!(pVBInfo->VBInfo & SetInSlaveMode))
7573                                                 tempah |= 0x02;
7574
7575                                         if (!(pVBInfo->VBInfo &
7576                                               SetCRT2ToRAMDAC)) {
7577                                                 tempah = tempah ^ 0x05;
7578                                                 if (!(pVBInfo->VBInfo &
7579                                                       SetCRT2ToLCD))
7580                                                         tempah = tempah ^ 0x01;
7581                                         }
7582
7583                                         if (!(pVBInfo->VBInfo &
7584                                               SetCRT2ToDualEdge))
7585                                                 tempah |= 0x08;
7586                                         xgifb_reg_and_or(pVBInfo->Part1Port,
7587                                                         0x2e, tempbl, tempah);
7588                                 } else {
7589                                         xgifb_reg_and_or(pVBInfo->Part1Port,
7590                                                         0x2e, tempbl, tempah);
7591                                 }
7592                         }
7593                 } else {
7594                         xgifb_reg_and_or(pVBInfo->Part1Port, 0x2e, tempbl,
7595                                         tempah);
7596                 }
7597         }
7598
7599         if (pVBInfo->VBInfo & (SetCRT2ToRAMDAC | SetCRT2ToTV | SetCRT2ToLCD
7600                         | SetCRT2ToLCDA)) {
7601                 tempah &= (~0x08);
7602                 if ((pVBInfo->ModeType == ModeVGA) && (!(pVBInfo->VBInfo
7603                                 & SetInSlaveMode))) {
7604                         tempah |= 0x010;
7605                 }
7606                 tempah |= 0x080;
7607
7608                 if (pVBInfo->VBInfo & SetCRT2ToTV) {
7609                         /* if (!(pVBInfo->TVInfo &
7610                                  (SetYPbPrMode525p | SetYPbPrMode750p))) { */
7611                         tempah |= 0x020;
7612                         if (ModeNo > 0x13) {
7613                                 if (pVBInfo->VBInfo & DriverMode)
7614                                         tempah = tempah ^ 0x20;
7615                         }
7616                         /* } */
7617                 }
7618
7619                 xgifb_reg_and_or(pVBInfo->Part4Port, 0x0D, ~0x0BF, tempah);
7620                 tempah = 0;
7621
7622                 if (pVBInfo->LCDInfo & SetLCDDualLink)
7623                         tempah |= 0x40;
7624
7625                 if (pVBInfo->VBInfo & SetCRT2ToTV) {
7626                         /* if ((!(pVBInfo->VBInfo & SetCRT2ToHiVisionTV)) &&
7627                                (!(pVBInfo->TVInfo &
7628                                   (SetYPbPrMode525p | SetYPbPrMode750p)))) { */
7629                         if (pVBInfo->TVInfo & RPLLDIV2XO)
7630                                 tempah |= 0x40;
7631                         /* } */
7632                 }
7633
7634                 if ((pVBInfo->LCDResInfo == Panel1280x1024)
7635                                 || (pVBInfo->LCDResInfo == Panel1280x1024x75))
7636                         tempah |= 0x80;
7637
7638                 if (pVBInfo->LCDResInfo == Panel1280x960)
7639                         tempah |= 0x80;
7640
7641                 xgifb_reg_set(pVBInfo->Part4Port, 0x0C, tempah);
7642         }
7643
7644         if (pVBInfo->VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV
7645                         | VB_XGI302LV | VB_XGI301C)) {
7646                 tempah = 0;
7647                 tempbl = 0xfb;
7648
7649                 if (pVBInfo->VBInfo & SetCRT2ToDualEdge) {
7650                         tempbl = 0xff;
7651                         if (pVBInfo->VBInfo & SetCRT2ToLCDA)
7652                                 tempah |= 0x04; /* shampoo 0129 */
7653                 }
7654
7655                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x13, tempbl, tempah);
7656                 tempah = 0x00;
7657                 tempbl = 0xcf;
7658                 if (!(pVBInfo->VBInfo & DisableCRT2Display)) {
7659                         if (pVBInfo->VBInfo & SetCRT2ToDualEdge)
7660                                 tempah |= 0x30;
7661                 }
7662
7663                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x2c, tempbl, tempah);
7664                 tempah = 0;
7665                 tempbl = 0x3f;
7666
7667                 if (!(pVBInfo->VBInfo & DisableCRT2Display)) {
7668                         if (pVBInfo->VBInfo & SetCRT2ToDualEdge)
7669                                 tempah |= 0xc0;
7670                 }
7671                 xgifb_reg_and_or(pVBInfo->Part4Port, 0x21, tempbl, tempah);
7672         }
7673
7674         tempah = 0;
7675         tempbl = 0x7f;
7676         if (!(pVBInfo->VBInfo & SetCRT2ToLCDA)) {
7677                 tempbl = 0xff;
7678                 if (!(pVBInfo->VBInfo & SetCRT2ToDualEdge))
7679                         tempah |= 0x80;
7680         }
7681
7682         xgifb_reg_and_or(pVBInfo->Part4Port, 0x23, tempbl, tempah);
7683
7684         if (pVBInfo->VBType & (VB_XGI302LV | VB_XGI301C)) {
7685                 if (pVBInfo->LCDInfo & SetLCDDualLink) {
7686                         xgifb_reg_or(pVBInfo->Part4Port, 0x27, 0x20);
7687                         xgifb_reg_or(pVBInfo->Part4Port, 0x34, 0x10);
7688                 }
7689         }
7690 }
7691
7692 static void XGI_CloseCRTC(struct xgi_hw_device_info *HwDeviceExtension,
7693                 struct vb_device_info *pVBInfo)
7694 {
7695         unsigned short tempbx;
7696
7697         tempbx = 0;
7698
7699         if (pVBInfo->VBInfo & SetCRT2ToLCDA)
7700                 tempbx = 0x08A0;
7701
7702 }
7703
7704 void XGI_UnLockCRT2(struct xgi_hw_device_info *HwDeviceExtension,
7705                 struct vb_device_info *pVBInfo)
7706 {
7707
7708         xgifb_reg_and_or(pVBInfo->Part1Port, 0x2f, 0xFF, 0x01);
7709
7710 }
7711
7712 void XGI_LockCRT2(struct xgi_hw_device_info *HwDeviceExtension,
7713                 struct vb_device_info *pVBInfo)
7714 {
7715
7716         xgifb_reg_and_or(pVBInfo->Part1Port, 0x2F, 0xFE, 0x00);
7717
7718 }
7719
7720 unsigned char XGI_BridgeIsOn(struct vb_device_info *pVBInfo)
7721 {
7722         unsigned short flag;
7723
7724         if (pVBInfo->IF_DEF_LVDS == 1) {
7725                 return 1;
7726         } else {
7727                 flag = xgifb_reg_get(pVBInfo->Part4Port, 0x00);
7728                 if ((flag == 1) || (flag == 2))
7729                         return 1; /* 301b */
7730                 else
7731                         return 0;
7732         }
7733 }
7734
7735 void XGI_LongWait(struct vb_device_info *pVBInfo)
7736 {
7737         unsigned short i;
7738
7739         i = xgifb_reg_get(pVBInfo->P3c4, 0x1F);
7740
7741         if (!(i & 0xC0)) {
7742                 for (i = 0; i < 0xFFFF; i++) {
7743                         if (!(inb(pVBInfo->P3da) & 0x08))
7744                                 break;
7745                 }
7746
7747                 for (i = 0; i < 0xFFFF; i++) {
7748                         if ((inb(pVBInfo->P3da) & 0x08))
7749                                 break;
7750                 }
7751         }
7752 }
7753
7754 static void XGI_VBLongWait(struct vb_device_info *pVBInfo)
7755 {
7756         unsigned short tempal, temp, i, j;
7757         return;
7758         if (!(pVBInfo->VBInfo & SetCRT2ToTV)) {
7759                 temp = 0;
7760                 for (i = 0; i < 3; i++) {
7761                         for (j = 0; j < 100; j++) {
7762                                 tempal = inb(pVBInfo->P3da);
7763                                 if (temp & 0x01) { /* VBWaitMode2 */
7764                                         if ((tempal & 0x08))
7765                                                 continue;
7766
7767                                         if (!(tempal & 0x08))
7768                                                 break;
7769
7770                                 } else { /* VBWaitMode1 */
7771                                         if (!(tempal & 0x08))
7772                                                 continue;
7773
7774                                         if ((tempal & 0x08))
7775                                                 break;
7776                                 }
7777                         }
7778                         temp = temp ^ 0x01;
7779                 }
7780         } else {
7781                 XGI_LongWait(pVBInfo);
7782         }
7783         return;
7784 }
7785
7786 unsigned short XGI_GetRatePtrCRT2(struct xgi_hw_device_info *pXGIHWDE,
7787                 unsigned short ModeNo, unsigned short ModeIdIndex,
7788                 struct vb_device_info *pVBInfo)
7789 {
7790         short LCDRefreshIndex[] = { 0x00, 0x00, 0x03, 0x01 },
7791                         LCDARefreshIndex[] = { 0x00, 0x00, 0x03, 0x01, 0x01,
7792                                         0x01, 0x01 };
7793
7794         unsigned short RefreshRateTableIndex, i, modeflag, index, temp;
7795
7796         if (ModeNo <= 0x13)
7797                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
7798         else
7799                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
7800
7801         if (ModeNo < 0x14)
7802                 return 0xFFFF;
7803
7804         index = xgifb_reg_get(pVBInfo->P3d4, 0x33);
7805         index = index >> pVBInfo->SelectCRT2Rate;
7806         index &= 0x0F;
7807
7808         if (pVBInfo->LCDInfo & LCDNonExpanding)
7809                 index = 0;
7810
7811         if (index > 0)
7812                 index--;
7813
7814         if (pVBInfo->SetFlag & ProgrammingCRT2) {
7815                 if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
7816                         if (pVBInfo->IF_DEF_LVDS == 0) {
7817                                 if (pVBInfo->VBType & (VB_XGI301B | VB_XGI302B
7818                                                 | VB_XGI301LV | VB_XGI302LV
7819                                                 | VB_XGI301C))
7820                                         /* 301b */
7821                                         temp = LCDARefreshIndex[
7822                                                 pVBInfo->LCDResInfo & 0x0F];
7823                                 else
7824                                         temp = LCDRefreshIndex[
7825                                                 pVBInfo->LCDResInfo & 0x0F];
7826
7827                                 if (index > temp)
7828                                         index = temp;
7829                         } else {
7830                                 index = 0;
7831                         }
7832                 }
7833         }
7834
7835         RefreshRateTableIndex = pVBInfo->EModeIDTable[ModeIdIndex].REFindex;
7836         ModeNo = pVBInfo->RefIndex[RefreshRateTableIndex].ModeID;
7837         if (pXGIHWDE->jChipType >= XG20) { /* for XG20, XG21, XG27 */
7838                 /*
7839                 if (pVBInfo->RefIndex[RefreshRateTableIndex].Ext_InfoFlag &
7840                     XG2xNotSupport) {
7841                         index++;
7842                 }
7843                 */
7844                 if ((pVBInfo->RefIndex[RefreshRateTableIndex].XRes == 800) &&
7845                     (pVBInfo->RefIndex[RefreshRateTableIndex].YRes == 600)) {
7846                         index++;
7847                 }
7848                 /* Alan 10/19/2007;
7849                  * do the similar adjustment like XGISearchCRT1Rate() */
7850                 if ((pVBInfo->RefIndex[RefreshRateTableIndex].XRes == 1024) &&
7851                     (pVBInfo->RefIndex[RefreshRateTableIndex].YRes == 768)) {
7852                         index++;
7853                 }
7854                 if ((pVBInfo->RefIndex[RefreshRateTableIndex].XRes == 1280) &&
7855                     (pVBInfo->RefIndex[RefreshRateTableIndex].YRes == 1024)) {
7856                         index++;
7857                 }
7858         }
7859
7860         i = 0;
7861         do {
7862                 if (pVBInfo->RefIndex[RefreshRateTableIndex + i].
7863                         ModeID != ModeNo)
7864                         break;
7865                 temp = pVBInfo->RefIndex[RefreshRateTableIndex + i].
7866                         Ext_InfoFlag;
7867                 temp &= ModeInfoFlag;
7868                 if (temp < pVBInfo->ModeType)
7869                         break;
7870                 i++;
7871                 index--;
7872
7873         } while (index != 0xFFFF);
7874         if (!(pVBInfo->VBInfo & SetCRT2ToRAMDAC)) {
7875                 if (pVBInfo->VBInfo & SetInSlaveMode) {
7876                         temp = pVBInfo->RefIndex[RefreshRateTableIndex + i - 1].
7877                                 Ext_InfoFlag;
7878                         if (temp & InterlaceMode)
7879                                 i++;
7880                 }
7881         }
7882         i--;
7883         if ((pVBInfo->SetFlag & ProgrammingCRT2)) {
7884                 temp = XGI_AjustCRT2Rate(ModeNo, ModeIdIndex,
7885                                 RefreshRateTableIndex, &i, pVBInfo);
7886         }
7887         return RefreshRateTableIndex + i; /* return (0x01 | (temp1<<1)); */
7888 }
7889
7890 static void XGI_SetLCDAGroup(unsigned short ModeNo, unsigned short ModeIdIndex,
7891                 struct xgi_hw_device_info *HwDeviceExtension,
7892                 struct vb_device_info *pVBInfo)
7893 {
7894         unsigned short RefreshRateTableIndex;
7895         /* unsigned short temp ; */
7896
7897         /* pVBInfo->SelectCRT2Rate = 0; */
7898
7899         pVBInfo->SetFlag |= ProgrammingCRT2;
7900         RefreshRateTableIndex = XGI_GetRatePtrCRT2(HwDeviceExtension, ModeNo,
7901                         ModeIdIndex, pVBInfo);
7902         XGI_GetLVDSResInfo(ModeNo, ModeIdIndex, pVBInfo);
7903         XGI_GetLVDSData(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
7904         XGI_ModCRT1Regs(ModeNo, ModeIdIndex, RefreshRateTableIndex,
7905                         HwDeviceExtension, pVBInfo);
7906         XGI_SetLVDSRegs(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
7907         XGI_SetCRT2ECLK(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
7908 }
7909
7910 unsigned char XGI_SetCRT2Group301(unsigned short ModeNo,
7911                 struct xgi_hw_device_info *HwDeviceExtension,
7912                 struct vb_device_info *pVBInfo)
7913 {
7914         unsigned short tempbx, ModeIdIndex, RefreshRateTableIndex;
7915
7916         tempbx = pVBInfo->VBInfo;
7917         pVBInfo->SetFlag |= ProgrammingCRT2;
7918         XGI_SearchModeID(ModeNo, &ModeIdIndex, pVBInfo);
7919         pVBInfo->SelectCRT2Rate = 4;
7920         RefreshRateTableIndex = XGI_GetRatePtrCRT2(HwDeviceExtension, ModeNo,
7921                         ModeIdIndex, pVBInfo);
7922         XGI_SaveCRT2Info(ModeNo, pVBInfo);
7923         XGI_GetCRT2ResInfo(ModeNo, ModeIdIndex, pVBInfo);
7924         XGI_GetCRT2Data(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
7925         XGI_PreSetGroup1(ModeNo, ModeIdIndex, HwDeviceExtension,
7926                         RefreshRateTableIndex, pVBInfo);
7927         XGI_SetGroup1(ModeNo, ModeIdIndex, HwDeviceExtension,
7928                         RefreshRateTableIndex, pVBInfo);
7929         XGI_SetLockRegs(ModeNo, ModeIdIndex, HwDeviceExtension,
7930                         RefreshRateTableIndex, pVBInfo);
7931         XGI_SetGroup2(ModeNo, ModeIdIndex, RefreshRateTableIndex,
7932                         HwDeviceExtension, pVBInfo);
7933         XGI_SetLCDRegs(ModeNo, ModeIdIndex, HwDeviceExtension,
7934                         RefreshRateTableIndex, pVBInfo);
7935         XGI_SetTap4Regs(pVBInfo);
7936         XGI_SetGroup3(ModeNo, ModeIdIndex, pVBInfo);
7937         XGI_SetGroup4(ModeNo, ModeIdIndex, RefreshRateTableIndex,
7938                         HwDeviceExtension, pVBInfo);
7939         XGI_SetCRT2VCLK(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
7940         XGI_SetGroup5(ModeNo, ModeIdIndex, pVBInfo);
7941         XGI_AutoThreshold(pVBInfo);
7942         return 1;
7943 }
7944
7945 void XGI_SenseCRT1(struct vb_device_info *pVBInfo)
7946 {
7947         unsigned char CRTCData[17] = { 0x5F, 0x4F, 0x50, 0x82, 0x55, 0x81,
7948                         0x0B, 0x3E, 0xE9, 0x0B, 0xDF, 0xE7, 0x04, 0x00, 0x00,
7949                         0x05, 0x00 };
7950
7951         unsigned char SR01 = 0, SR1F = 0, SR07 = 0, SR06 = 0;
7952
7953         unsigned char CR17, CR63, SR31;
7954         unsigned short temp;
7955         unsigned char DAC_TEST_PARMS[3] = { 0x0F, 0x0F, 0x0F };
7956
7957         int i;
7958         xgifb_reg_set(pVBInfo->P3c4, 0x05, 0x86);
7959
7960         /* [2004/05/06] Vicent to fix XG42 single LCD sense to CRT+LCD */
7961         xgifb_reg_set(pVBInfo->P3d4, 0x57, 0x4A);
7962         xgifb_reg_set(pVBInfo->P3d4, 0x53, (unsigned char) (xgifb_reg_get(
7963                         pVBInfo->P3d4, 0x53) | 0x02));
7964
7965         SR31 = (unsigned char) xgifb_reg_get(pVBInfo->P3c4, 0x31);
7966         CR63 = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x63);
7967         SR01 = (unsigned char) xgifb_reg_get(pVBInfo->P3c4, 0x01);
7968
7969         xgifb_reg_set(pVBInfo->P3c4, 0x01, (unsigned char) (SR01 & 0xDF));
7970         xgifb_reg_set(pVBInfo->P3d4, 0x63, (unsigned char) (CR63 & 0xBF));
7971
7972         CR17 = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x17);
7973         xgifb_reg_set(pVBInfo->P3d4, 0x17, (unsigned char) (CR17 | 0x80));
7974
7975         SR1F = (unsigned char) xgifb_reg_get(pVBInfo->P3c4, 0x1F);
7976         xgifb_reg_set(pVBInfo->P3c4, 0x1F, (unsigned char) (SR1F | 0x04));
7977
7978         SR07 = (unsigned char) xgifb_reg_get(pVBInfo->P3c4, 0x07);
7979         xgifb_reg_set(pVBInfo->P3c4, 0x07, (unsigned char) (SR07 & 0xFB));
7980         SR06 = (unsigned char) xgifb_reg_get(pVBInfo->P3c4, 0x06);
7981         xgifb_reg_set(pVBInfo->P3c4, 0x06, (unsigned char) (SR06 & 0xC3));
7982
7983         xgifb_reg_set(pVBInfo->P3d4, 0x11, 0x00);
7984
7985         for (i = 0; i < 8; i++)
7986                 xgifb_reg_set(pVBInfo->P3d4, (unsigned short) i, CRTCData[i]);
7987
7988         for (i = 8; i < 11; i++)
7989                 xgifb_reg_set(pVBInfo->P3d4, (unsigned short) (i + 8),
7990                                 CRTCData[i]);
7991
7992         for (i = 11; i < 13; i++)
7993                 xgifb_reg_set(pVBInfo->P3d4, (unsigned short) (i + 4),
7994                                 CRTCData[i]);
7995
7996         for (i = 13; i < 16; i++)
7997                 xgifb_reg_set(pVBInfo->P3c4, (unsigned short) (i - 3),
7998                                 CRTCData[i]);
7999
8000         xgifb_reg_set(pVBInfo->P3c4, 0x0E, (unsigned char) (CRTCData[16]
8001                         & 0xE0));
8002
8003         xgifb_reg_set(pVBInfo->P3c4, 0x31, 0x00);
8004         xgifb_reg_set(pVBInfo->P3c4, 0x2B, 0x1B);
8005         xgifb_reg_set(pVBInfo->P3c4, 0x2C, 0xE1);
8006
8007         outb(0x00, pVBInfo->P3c8);
8008
8009         for (i = 0; i < 256; i++) {
8010                 outb((unsigned char) DAC_TEST_PARMS[0], (pVBInfo->P3c8 + 1));
8011                 outb((unsigned char) DAC_TEST_PARMS[1], (pVBInfo->P3c8 + 1));
8012                 outb((unsigned char) DAC_TEST_PARMS[2], (pVBInfo->P3c8 + 1));
8013         }
8014
8015         XGI_VBLongWait(pVBInfo);
8016         XGI_VBLongWait(pVBInfo);
8017         XGI_VBLongWait(pVBInfo);
8018
8019         mdelay(1);
8020
8021         XGI_WaitDisply(pVBInfo);
8022         temp = inb(pVBInfo->P3c2);
8023
8024         if (temp & 0x10)
8025                 xgifb_reg_and_or(pVBInfo->P3d4, 0x32, 0xDF, 0x20);
8026         else
8027                 xgifb_reg_and_or(pVBInfo->P3d4, 0x32, 0xDF, 0x00);
8028
8029         /* alan, avoid display something, set BLACK DAC if not restore DAC */
8030         outb(0x00, pVBInfo->P3c8);
8031
8032         for (i = 0; i < 256; i++) {
8033                 outb(0, (pVBInfo->P3c8 + 1));
8034                 outb(0, (pVBInfo->P3c8 + 1));
8035                 outb(0, (pVBInfo->P3c8 + 1));
8036         }
8037
8038         xgifb_reg_set(pVBInfo->P3c4, 0x01, SR01);
8039         xgifb_reg_set(pVBInfo->P3d4, 0x63, CR63);
8040         xgifb_reg_set(pVBInfo->P3c4, 0x31, SR31);
8041
8042         /* [2004/05/11] Vicent */
8043         xgifb_reg_set(pVBInfo->P3d4, 0x53, (unsigned char) (xgifb_reg_get(
8044                         pVBInfo->P3d4, 0x53) & 0xFD));
8045         xgifb_reg_set(pVBInfo->P3c4, 0x1F, (unsigned char) SR1F);
8046 }
8047
8048 void XGI_EnableBridge(struct xgi_hw_device_info *HwDeviceExtension,
8049                 struct vb_device_info *pVBInfo)
8050 {
8051         unsigned short tempah;
8052
8053         if (pVBInfo->SetFlag == Win9xDOSMode) {
8054                 if (pVBInfo->VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV
8055                                 | VB_XGI302LV | VB_XGI301C)) {
8056                         XGI_DisplayOn(HwDeviceExtension, pVBInfo);
8057                         return;
8058                 } else
8059                         /* LVDS or CH7017 */
8060                         return;
8061         }
8062
8063         if (pVBInfo->VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV
8064                         | VB_XGI302LV | VB_XGI301C)) {
8065                 if (!(pVBInfo->SetFlag & DisableChA)) {
8066                         if (pVBInfo->SetFlag & EnableChA) {
8067                                 /* Power on */
8068                                 xgifb_reg_set(pVBInfo->Part1Port, 0x1E, 0x20);
8069                         } else {
8070                                 /* SetCRT2ToLCDA ) */
8071                                 if (pVBInfo->VBInfo & SetCRT2ToDualEdge) {
8072                                         /* Power on */
8073                                         xgifb_reg_set(pVBInfo->Part1Port,
8074                                                         0x1E, 0x20);
8075                                 }
8076                         }
8077                 }
8078
8079                 if (!(pVBInfo->SetFlag & DisableChB)) {
8080                         if ((pVBInfo->SetFlag & EnableChB) || (pVBInfo->VBInfo
8081                                         & (SetCRT2ToLCD | SetCRT2ToTV
8082                                                         | SetCRT2ToRAMDAC))) {
8083                                 tempah = (unsigned char) xgifb_reg_get(
8084                                                 pVBInfo->P3c4, 0x32);
8085                                 tempah &= 0xDF;
8086                                 if (pVBInfo->VBInfo & SetInSlaveMode) {
8087                                         if (!(pVBInfo->VBInfo &
8088                                               SetCRT2ToRAMDAC))
8089                                                 tempah |= 0x20;
8090                                 }
8091                                 xgifb_reg_set(pVBInfo->P3c4, 0x32, tempah);
8092                                 xgifb_reg_or(pVBInfo->P3c4, 0x1E, 0x20);
8093
8094                                 tempah = (unsigned char) xgifb_reg_get(
8095                                                 pVBInfo->Part1Port, 0x2E);
8096
8097                                 if (!(tempah & 0x80))
8098                                         /* BVBDOENABLE = 1 */
8099                                         xgifb_reg_or(pVBInfo->Part1Port,
8100                                                         0x2E, 0x80);
8101                                 /* BScreenOFF = 0 */
8102                                 xgifb_reg_and(pVBInfo->Part1Port, 0x00, 0x7F);
8103                         }
8104                 }
8105
8106                 if ((pVBInfo->SetFlag & (EnableChA | EnableChB))
8107                                 || (!(pVBInfo->VBInfo & DisableCRT2Display))) {
8108                         xgifb_reg_and_or(pVBInfo->Part2Port, 0x00, ~0xE0,
8109                                         0x20); /* shampoo 0129 */
8110                         if (pVBInfo->VBType & (VB_XGI302LV | VB_XGI301C)) {
8111                                 if (!XGI_DisableChISLCD(pVBInfo)) {
8112                                         if (XGI_EnableChISLCD(pVBInfo) ||
8113                                             (pVBInfo->VBInfo &
8114                                             (SetCRT2ToLCD | SetCRT2ToLCDA)))
8115                                                 /* LVDS PLL power on */
8116                                                 xgifb_reg_and(
8117                                                         pVBInfo->Part4Port,
8118                                                         0x2A,
8119                                                         0x7F);
8120                                 }
8121                                 /* LVDS Driver power on */
8122                                 xgifb_reg_and(pVBInfo->Part4Port, 0x30, 0x7F);
8123                         }
8124                 }
8125
8126                 tempah = 0x00;
8127
8128                 if (!(pVBInfo->VBInfo & DisableCRT2Display)) {
8129                         tempah = 0xc0;
8130
8131                         if (!(pVBInfo->VBInfo & SetSimuScanMode)) {
8132                                 if (pVBInfo->VBInfo & SetCRT2ToLCDA) {
8133                                         if (pVBInfo->VBInfo &
8134                                             SetCRT2ToDualEdge) {
8135                                                 tempah = tempah & 0x40;
8136                                                 if (pVBInfo->VBInfo &
8137                                                     SetCRT2ToLCDA)
8138                                                         tempah = tempah ^ 0xC0;
8139
8140                                                 if (pVBInfo->SetFlag &
8141                                                     DisableChB)
8142                                                         tempah &= 0xBF;
8143
8144                                                 if (pVBInfo->SetFlag &
8145                                                     DisableChA)
8146                                                         tempah &= 0x7F;
8147
8148                                                 if (pVBInfo->SetFlag &
8149                                                     EnableChB)
8150                                                         tempah |= 0x40;
8151
8152                                                 if (pVBInfo->SetFlag &
8153                                                     EnableChA)
8154                                                         tempah |= 0x80;
8155                                         }
8156                                 }
8157                         }
8158                 }
8159
8160                 /* EnablePart4_1F */
8161                 xgifb_reg_or(pVBInfo->Part4Port, 0x1F, tempah);
8162
8163                 if (pVBInfo->SetFlag & Win9xDOSMode) {
8164                         XGI_DisplayOn(HwDeviceExtension, pVBInfo);
8165                         return;
8166                 }
8167
8168                 if (!(pVBInfo->SetFlag & DisableChA)) {
8169                         XGI_VBLongWait(pVBInfo);
8170                         if (!(pVBInfo->SetFlag & GatingCRT)) {
8171                                 XGI_DisableGatingCRT(HwDeviceExtension,
8172                                                      pVBInfo);
8173                                 XGI_DisplayOn(HwDeviceExtension, pVBInfo);
8174                                 XGI_VBLongWait(pVBInfo);
8175                         }
8176                 }
8177         } /* 301 */
8178         else { /* LVDS */
8179                 if (pVBInfo->VBInfo & (SetCRT2ToTV | SetCRT2ToLCD
8180                                 | SetCRT2ToLCDA))
8181                         /* enable CRT2 */
8182                         xgifb_reg_or(pVBInfo->Part1Port, 0x1E, 0x20);
8183
8184                 tempah = (unsigned char) xgifb_reg_get(pVBInfo->Part1Port,
8185                                 0x2E);
8186                 if (!(tempah & 0x80))
8187                         /* BVBDOENABLE = 1 */
8188                         xgifb_reg_or(pVBInfo->Part1Port, 0x2E, 0x80);
8189
8190                 xgifb_reg_and(pVBInfo->Part1Port, 0x00, 0x7F);
8191                 XGI_DisplayOn(HwDeviceExtension, pVBInfo);
8192         } /* End of VB */
8193 }
8194
8195 static void XGI_SetCRT1Group(struct xgi_hw_device_info *HwDeviceExtension,
8196                 unsigned short ModeNo, unsigned short ModeIdIndex,
8197                 struct vb_device_info *pVBInfo)
8198 {
8199         unsigned short StandTableIndex, RefreshRateTableIndex, b3CC, temp;
8200
8201         unsigned short XGINew_P3cc = pVBInfo->P3cc;
8202
8203         /* XGINew_CRT1Mode = ModeNo; // SaveModeID */
8204         StandTableIndex = XGI_GetModePtr(ModeNo, ModeIdIndex, pVBInfo);
8205         /* XGI_SetBIOSData(ModeNo, ModeIdIndex); */
8206         /* XGI_ClearBankRegs(ModeNo, ModeIdIndex); */
8207         XGI_SetSeqRegs(ModeNo, StandTableIndex, ModeIdIndex, pVBInfo);
8208         XGI_SetMiscRegs(StandTableIndex, pVBInfo);
8209         XGI_SetCRTCRegs(HwDeviceExtension, StandTableIndex, pVBInfo);
8210         XGI_SetATTRegs(ModeNo, StandTableIndex, ModeIdIndex, pVBInfo);
8211         XGI_SetGRCRegs(StandTableIndex, pVBInfo);
8212         XGI_ClearExt1Regs(pVBInfo);
8213
8214         /* if (pVBInfo->IF_DEF_ExpLink) */
8215         if (HwDeviceExtension->jChipType == XG27) {
8216                 if (pVBInfo->IF_DEF_LVDS == 0)
8217                         XGI_SetDefaultVCLK(pVBInfo);
8218         }
8219
8220         temp = ~ProgrammingCRT2;
8221         pVBInfo->SetFlag &= temp;
8222         pVBInfo->SelectCRT2Rate = 0;
8223
8224         if (pVBInfo->VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV
8225                         | VB_XGI302LV | VB_XGI301C)) {
8226                 if (pVBInfo->VBInfo & (SetSimuScanMode | SetCRT2ToLCDA
8227                                 | SetInSlaveMode)) {
8228                         pVBInfo->SetFlag |= ProgrammingCRT2;
8229                 }
8230         }
8231
8232         RefreshRateTableIndex = XGI_GetRatePtrCRT2(HwDeviceExtension, ModeNo,
8233                         ModeIdIndex, pVBInfo);
8234         if (RefreshRateTableIndex != 0xFFFF) {
8235                 XGI_SetSync(RefreshRateTableIndex, pVBInfo);
8236                 XGI_SetCRT1CRTC(ModeNo, ModeIdIndex, RefreshRateTableIndex,
8237                                 pVBInfo, HwDeviceExtension);
8238                 XGI_SetCRT1DE(HwDeviceExtension, ModeNo, ModeIdIndex,
8239                                 RefreshRateTableIndex, pVBInfo);
8240                 XGI_SetCRT1Offset(ModeNo, ModeIdIndex, RefreshRateTableIndex,
8241                                 HwDeviceExtension, pVBInfo);
8242                 XGI_SetCRT1VCLK(ModeNo, ModeIdIndex, HwDeviceExtension,
8243                                 RefreshRateTableIndex, pVBInfo);
8244         }
8245
8246         if ((HwDeviceExtension->jChipType >= XG20) &&
8247             (HwDeviceExtension->jChipType < XG27)) { /* fix H/W DCLK/2 bug */
8248                 if ((ModeNo == 0x00) | (ModeNo == 0x01)) {
8249                         xgifb_reg_set(pVBInfo->P3c4, 0x2B, 0x4E);
8250                         xgifb_reg_set(pVBInfo->P3c4, 0x2C, 0xE9);
8251                         b3CC = (unsigned char) inb(XGINew_P3cc);
8252                         outb((b3CC |= 0x0C), XGINew_P3cc);
8253                 } else if ((ModeNo == 0x04) | (ModeNo == 0x05) | (ModeNo
8254                                 == 0x0D)) {
8255                         xgifb_reg_set(pVBInfo->P3c4, 0x2B, 0x1B);
8256                         xgifb_reg_set(pVBInfo->P3c4, 0x2C, 0xE3);
8257                         b3CC = (unsigned char) inb(XGINew_P3cc);
8258                         outb((b3CC |= 0x0C), XGINew_P3cc);
8259                 }
8260         }
8261
8262         if (HwDeviceExtension->jChipType >= XG21) {
8263                 temp = xgifb_reg_get(pVBInfo->P3d4, 0x38);
8264                 if (temp & 0xA0) {
8265
8266                         /* Enable write GPIOF */
8267                         /* xgifb_reg_and(pVBInfo->P3d4, 0x4A, ~0x20); */
8268                         /* P. DWN */
8269                         /* xgifb_reg_and(pVBInfo->P3d4, 0x48, ~0x20); */
8270                         /* XG21 CRT1 Timing */
8271                         if (HwDeviceExtension->jChipType == XG27)
8272                                 XGI_SetXG27CRTC(ModeNo, ModeIdIndex,
8273                                                 RefreshRateTableIndex, pVBInfo);
8274                         else
8275                                 XGI_SetXG21CRTC(ModeNo, ModeIdIndex,
8276                                                 RefreshRateTableIndex, pVBInfo);
8277
8278                         XGI_UpdateXG21CRTC(ModeNo, pVBInfo,
8279                                         RefreshRateTableIndex);
8280
8281                         if (HwDeviceExtension->jChipType == XG27)
8282                                 XGI_SetXG27LCD(pVBInfo, RefreshRateTableIndex,
8283                                                 ModeNo);
8284                         else
8285                                 XGI_SetXG21LCD(pVBInfo, RefreshRateTableIndex,
8286                                                 ModeNo);
8287
8288                         if (pVBInfo->IF_DEF_LVDS == 1) {
8289                                 if (HwDeviceExtension->jChipType == XG27)
8290                                         XGI_SetXG27LVDSPara(ModeNo,
8291                                                         ModeIdIndex, pVBInfo);
8292                                 else
8293                                         XGI_SetXG21LVDSPara(ModeNo,
8294                                                         ModeIdIndex, pVBInfo);
8295                         }
8296                         /* P. ON */
8297                         /* xgifb_reg_or(pVBInfo->P3d4, 0x48, 0x20); */
8298                 }
8299         }
8300
8301         pVBInfo->SetFlag &= (~ProgrammingCRT2);
8302         XGI_SetCRT1FIFO(ModeNo, HwDeviceExtension, pVBInfo);
8303         XGI_SetCRT1ModeRegs(HwDeviceExtension, ModeNo, ModeIdIndex,
8304                         RefreshRateTableIndex, pVBInfo);
8305
8306         /* XGI_LoadCharacter(); //dif ifdef TVFont */
8307
8308         XGI_LoadDAC(ModeNo, ModeIdIndex, pVBInfo);
8309         /* XGI_ClearBuffer(HwDeviceExtension, ModeNo, pVBInfo); */
8310 }
8311
8312 unsigned char XGISetModeNew(struct xgi_hw_device_info *HwDeviceExtension,
8313                         unsigned short ModeNo)
8314 {
8315         unsigned short ModeIdIndex;
8316         /* unsigned char *pVBInfo->FBAddr =
8317                                 HwDeviceExtension->pjVideoMemoryAddress; */
8318         struct vb_device_info VBINF;
8319         struct vb_device_info *pVBInfo = &VBINF;
8320         pVBInfo->ROMAddr = HwDeviceExtension->pjVirtualRomBase;
8321         pVBInfo->BaseAddr = (unsigned long) HwDeviceExtension->pjIOAddress;
8322         pVBInfo->IF_DEF_LVDS = 0;
8323         pVBInfo->IF_DEF_LCDA = 1;
8324         pVBInfo->IF_DEF_VideoCapture = 0;
8325         pVBInfo->IF_DEF_ScaleLCD = 0;
8326         pVBInfo->IF_DEF_OEMUtil = 0;
8327         pVBInfo->IF_DEF_PWD = 0;
8328
8329         if (HwDeviceExtension->jChipType >= XG20) { /* kuku 2004/06/25 */
8330                 pVBInfo->IF_DEF_YPbPr = 0;
8331                 pVBInfo->IF_DEF_HiVision = 0;
8332                 pVBInfo->IF_DEF_CRT2Monitor = 0;
8333                 pVBInfo->VBType = 0; /*set VBType default 0*/
8334         } else {
8335                 pVBInfo->IF_DEF_YPbPr = 1;
8336                 pVBInfo->IF_DEF_HiVision = 1;
8337                 pVBInfo->IF_DEF_CRT2Monitor = 1;
8338         }
8339
8340         pVBInfo->P3c4 = pVBInfo->BaseAddr + 0x14;
8341         pVBInfo->P3d4 = pVBInfo->BaseAddr + 0x24;
8342         pVBInfo->P3c0 = pVBInfo->BaseAddr + 0x10;
8343         pVBInfo->P3ce = pVBInfo->BaseAddr + 0x1e;
8344         pVBInfo->P3c2 = pVBInfo->BaseAddr + 0x12;
8345         pVBInfo->P3cc = pVBInfo->BaseAddr + 0x1C;
8346         pVBInfo->P3ca = pVBInfo->BaseAddr + 0x1a;
8347         pVBInfo->P3c6 = pVBInfo->BaseAddr + 0x16;
8348         pVBInfo->P3c7 = pVBInfo->BaseAddr + 0x17;
8349         pVBInfo->P3c8 = pVBInfo->BaseAddr + 0x18;
8350         pVBInfo->P3c9 = pVBInfo->BaseAddr + 0x19;
8351         pVBInfo->P3da = pVBInfo->BaseAddr + 0x2A;
8352         pVBInfo->Part0Port = pVBInfo->BaseAddr + XGI_CRT2_PORT_00;
8353         pVBInfo->Part1Port = pVBInfo->BaseAddr + XGI_CRT2_PORT_04;
8354         pVBInfo->Part2Port = pVBInfo->BaseAddr + XGI_CRT2_PORT_10;
8355         pVBInfo->Part3Port = pVBInfo->BaseAddr + XGI_CRT2_PORT_12;
8356         pVBInfo->Part4Port = pVBInfo->BaseAddr + XGI_CRT2_PORT_14;
8357         pVBInfo->Part5Port = pVBInfo->BaseAddr + XGI_CRT2_PORT_14 + 2;
8358
8359         /* for x86 Linux, XG21 LVDS */
8360         if (HwDeviceExtension->jChipType == XG21) {
8361                 if ((xgifb_reg_get(pVBInfo->P3d4, 0x38) & 0xE0) == 0xC0)
8362                         pVBInfo->IF_DEF_LVDS = 1;
8363         }
8364         if (HwDeviceExtension->jChipType == XG27) {
8365                 if ((xgifb_reg_get(pVBInfo->P3d4, 0x38) & 0xE0) == 0xC0) {
8366                         if (xgifb_reg_get(pVBInfo->P3d4, 0x30) & 0x20)
8367                                 pVBInfo->IF_DEF_LVDS = 1;
8368                 }
8369         }
8370
8371         if (HwDeviceExtension->jChipType < XG20) /* kuku 2004/06/25 */
8372                 XGI_GetVBType(pVBInfo);
8373
8374         InitTo330Pointer(HwDeviceExtension->jChipType, pVBInfo);
8375         if (ModeNo & 0x80) {
8376                 ModeNo = ModeNo & 0x7F;
8377                 /* XGINew_flag_clearbuffer = 0; */
8378         }
8379         /* else {
8380                 XGINew_flag_clearbuffer = 1;
8381         }
8382         */
8383         xgifb_reg_set(pVBInfo->P3c4, 0x05, 0x86);
8384
8385         if (HwDeviceExtension->jChipType < XG20) /* kuku 2004/06/25 1.Openkey */
8386                 XGI_UnLockCRT2(HwDeviceExtension, pVBInfo);
8387
8388         XGI_SearchModeID(ModeNo, &ModeIdIndex, pVBInfo);
8389
8390         XGI_GetVGAType(HwDeviceExtension, pVBInfo);
8391
8392         if (HwDeviceExtension->jChipType < XG20) { /* kuku 2004/06/25 */
8393                 XGI_GetVBInfo(ModeNo, ModeIdIndex, HwDeviceExtension, pVBInfo);
8394                 XGI_GetTVInfo(ModeNo, ModeIdIndex, pVBInfo);
8395                 XGI_GetLCDInfo(ModeNo, ModeIdIndex, pVBInfo);
8396                 XGI_DisableBridge(HwDeviceExtension, pVBInfo);
8397
8398                 if (pVBInfo->VBInfo & (SetSimuScanMode | SetCRT2ToLCDA)) {
8399                         XGI_SetCRT1Group(HwDeviceExtension, ModeNo,
8400                                         ModeIdIndex, pVBInfo);
8401
8402                         if (pVBInfo->VBInfo & SetCRT2ToLCDA) {
8403                                 XGI_SetLCDAGroup(ModeNo, ModeIdIndex,
8404                                                 HwDeviceExtension, pVBInfo);
8405                         }
8406                 } else {
8407                         if (!(pVBInfo->VBInfo & SwitchToCRT2)) {
8408                                 XGI_SetCRT1Group(HwDeviceExtension, ModeNo,
8409                                                 ModeIdIndex, pVBInfo);
8410                                 if (pVBInfo->VBInfo & SetCRT2ToLCDA) {
8411                                         XGI_SetLCDAGroup(ModeNo, ModeIdIndex,
8412                                                         HwDeviceExtension,
8413                                                         pVBInfo);
8414                                 }
8415                         }
8416                 }
8417
8418                 if (pVBInfo->VBInfo & (SetSimuScanMode | SwitchToCRT2)) {
8419                         switch (HwDeviceExtension->ujVBChipID) {
8420                         case VB_CHIP_301:
8421                                 XGI_SetCRT2Group301(ModeNo, HwDeviceExtension,
8422                                                 pVBInfo); /*add for CRT2 */
8423                                 break;
8424
8425                         case VB_CHIP_302:
8426                                 XGI_SetCRT2Group301(ModeNo, HwDeviceExtension,
8427                                                 pVBInfo); /*add for CRT2 */
8428                                 break;
8429
8430                         default:
8431                                 break;
8432                         }
8433                 }
8434
8435                 XGI_SetCRT2ModeRegs(ModeNo, HwDeviceExtension, pVBInfo);
8436                 XGI_OEM310Setting(ModeNo, ModeIdIndex, pVBInfo); /*0212*/
8437                 XGI_CloseCRTC(HwDeviceExtension, pVBInfo);
8438                 XGI_EnableBridge(HwDeviceExtension, pVBInfo);
8439         } /* !XG20 */
8440         else {
8441                 if (pVBInfo->IF_DEF_LVDS == 1)
8442                         if (!XGI_XG21CheckLVDSMode(ModeNo,
8443                                                    ModeIdIndex,
8444                                                    pVBInfo))
8445                                 return 0;
8446
8447                 if (ModeNo <= 0x13) {
8448                         pVBInfo->ModeType = pVBInfo->SModeIDTable[ModeIdIndex].
8449                                                 St_ModeFlag & ModeInfoFlag;
8450                 } else {
8451                         pVBInfo->ModeType = pVBInfo->EModeIDTable[ModeIdIndex].
8452                                                 Ext_ModeFlag & ModeInfoFlag;
8453                 }
8454
8455                 pVBInfo->SetFlag = 0;
8456                 pVBInfo->VBInfo = DisableCRT2Display;
8457
8458                 XGI_DisplayOff(HwDeviceExtension, pVBInfo);
8459
8460                 XGI_SetCRT1Group(HwDeviceExtension, ModeNo, ModeIdIndex,
8461                                 pVBInfo);
8462
8463                 XGI_DisplayOn(HwDeviceExtension, pVBInfo);
8464                 /*
8465                 if (HwDeviceExtension->jChipType == XG21)
8466                         xgifb_reg_and_or(pVBInfo->P3c4, 0x09, ~0x80, 0x80);
8467                 */
8468         }
8469
8470         /*
8471         if (ModeNo <= 0x13) {
8472                 modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
8473         } else {
8474                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
8475         }
8476         pVBInfo->ModeType = modeflag&ModeInfoFlag;
8477         pVBInfo->SetFlag = 0x00;
8478         pVBInfo->VBInfo = DisableCRT2Display;
8479         temp = XGINew_CheckMemorySize(HwDeviceExtension,
8480                                       ModeNo,
8481                                       ModeIdIndex,
8482                                       pVBInfo);
8483
8484         if (temp == 0)
8485                 return (0);
8486
8487         XGI_DisplayOff(HwDeviceExtension, pVBInfo) ;
8488         XGI_SetCRT1Group(HwDeviceExtension, ModeNo, ModeIdIndex, pVBInfo);
8489         XGI_DisplayOn(HwDeviceExtension, pVBInfo);
8490         */
8491
8492         XGI_UpdateModeInfo(HwDeviceExtension, pVBInfo);
8493
8494         if (HwDeviceExtension->jChipType < XG20) { /* kuku 2004/06/25 */
8495                 XGI_LockCRT2(HwDeviceExtension, pVBInfo);
8496         }
8497
8498         return 1;
8499 }