]> Pileus Git - ~andy/linux/blob - drivers/staging/xgifb/vb_setmode.c
53135c54596522aade00f511e6227f28c5ff3df5
[~andy/linux] / drivers / staging / xgifb / vb_setmode.c
1 #include <linux/delay.h>
2 #include "XGIfb.h"
3
4 #include "vb_def.h"
5 #include "vb_util.h"
6 #include "vb_table.h"
7 #include "vb_setmode.h"
8
9 #define  IndexMask 0xff
10 #define TVCLKBASE_315_25 (TVCLKBASE_315 + 25)
11
12 static const unsigned short XGINew_VGA_DAC[] = {
13         0x00, 0x10, 0x04, 0x14, 0x01, 0x11, 0x09, 0x15,
14         0x2A, 0x3A, 0x2E, 0x3E, 0x2B, 0x3B, 0x2F, 0x3F,
15         0x00, 0x05, 0x08, 0x0B, 0x0E, 0x11, 0x14, 0x18,
16         0x1C, 0x20, 0x24, 0x28, 0x2D, 0x32, 0x38, 0x3F,
17         0x00, 0x10, 0x1F, 0x2F, 0x3F, 0x1F, 0x27, 0x2F,
18         0x37, 0x3F, 0x2D, 0x31, 0x36, 0x3A, 0x3F, 0x00,
19         0x07, 0x0E, 0x15, 0x1C, 0x0E, 0x11, 0x15, 0x18,
20         0x1C, 0x14, 0x16, 0x18, 0x1A, 0x1C, 0x00, 0x04,
21         0x08, 0x0C, 0x10, 0x08, 0x0A, 0x0C, 0x0E, 0x10,
22         0x0B, 0x0C, 0x0D, 0x0F, 0x10};
23
24 void InitTo330Pointer(unsigned char ChipType, struct vb_device_info *pVBInfo)
25 {
26         pVBInfo->StandTable = (struct SiS_StandTable_S *) &XGI330_StandTable;
27         pVBInfo->EModeIDTable = (struct XGI_ExtStruct *) XGI330_EModeIDTable;
28         pVBInfo->RefIndex = (struct XGI_Ext2Struct *) XGI330_RefIndex;
29         pVBInfo->XGINEWUB_CRT1Table
30                         = (struct XGI_CRT1TableStruct *) XGI_CRT1Table;
31
32         pVBInfo->MCLKData = (struct SiS_MCLKData *) XGI340New_MCLKData;
33         pVBInfo->ECLKData = (struct XGI_ECLKDataStruct *) XGI340_ECLKData;
34         pVBInfo->VCLKData = (struct SiS_VCLKData *) XGI_VCLKData;
35         pVBInfo->VBVCLKData = (struct SiS_VBVCLKData *) XGI_VBVCLKData;
36         pVBInfo->ScreenOffset = XGI330_ScreenOffset;
37         pVBInfo->StResInfo = (struct SiS_StResInfo_S *) XGI330_StResInfo;
38         pVBInfo->ModeResInfo
39                         = (struct SiS_ModeResInfo_S *) XGI330_ModeResInfo;
40
41         pVBInfo->LCDResInfo = 0;
42         pVBInfo->LCDTypeInfo = 0;
43         pVBInfo->LCDInfo = 0;
44         pVBInfo->VBInfo = 0;
45         pVBInfo->TVInfo = 0;
46
47         pVBInfo->SR15 = XGI340_SR13;
48         pVBInfo->CR40 = XGI340_cr41;
49         pVBInfo->CR6B = XGI340_CR6B;
50         pVBInfo->CR6E = XGI340_CR6E;
51         pVBInfo->CR6F = XGI340_CR6F;
52         pVBInfo->CR89 = XGI340_CR89;
53         pVBInfo->AGPReg = XGI340_AGPReg;
54         pVBInfo->SR16 = XGI340_SR16;
55
56         pVBInfo->SR21 = 0xa3;
57         pVBInfo->SR22 = 0xfb;
58
59         pVBInfo->NTSCTiming = XGI330_NTSCTiming;
60         pVBInfo->PALTiming = XGI330_PALTiming;
61         pVBInfo->HiTVExtTiming = XGI330_HiTVExtTiming;
62         pVBInfo->HiTVSt1Timing = XGI330_HiTVSt1Timing;
63         pVBInfo->HiTVSt2Timing = XGI330_HiTVSt2Timing;
64         pVBInfo->HiTVTextTiming = XGI330_HiTVTextTiming;
65         pVBInfo->YPbPr750pTiming = XGI330_YPbPr750pTiming;
66         pVBInfo->YPbPr525pTiming = XGI330_YPbPr525pTiming;
67         pVBInfo->YPbPr525iTiming = XGI330_YPbPr525iTiming;
68         pVBInfo->HiTVGroup3Data = XGI330_HiTVGroup3Data;
69         pVBInfo->HiTVGroup3Simu = XGI330_HiTVGroup3Simu;
70         pVBInfo->HiTVGroup3Text = XGI330_HiTVGroup3Text;
71         pVBInfo->Ren525pGroup3 = XGI330_Ren525pGroup3;
72         pVBInfo->Ren750pGroup3 = XGI330_Ren750pGroup3;
73
74         pVBInfo->TimingH = (struct XGI_TimingHStruct *) XGI_TimingH;
75         pVBInfo->TimingV = (struct XGI_TimingVStruct *) XGI_TimingV;
76         pVBInfo->UpdateCRT1 = (struct XGI_XG21CRT1Struct *) XGI_UpdateCRT1Table;
77
78         /* 310 customization related */
79         if ((pVBInfo->VBType & VB_SIS301LV) || (pVBInfo->VBType & VB_SIS302LV))
80                 pVBInfo->LCDCapList = XGI_LCDDLCapList;
81         else
82                 pVBInfo->LCDCapList = XGI_LCDCapList;
83
84         if (ChipType >= XG20)
85                 pVBInfo->XGINew_CR97 = 0x10;
86
87         if (ChipType == XG27) {
88                 unsigned char temp;
89                 pVBInfo->MCLKData
90                         = (struct SiS_MCLKData *) XGI27New_MCLKData;
91                 pVBInfo->CR40 = XGI27_cr41;
92                 pVBInfo->XGINew_CR97 = 0xc1;
93                 pVBInfo->SR15 = XG27_SR13;
94
95                 /*Z11m DDR*/
96                 temp = xgifb_reg_get(pVBInfo->P3c4, 0x3B);
97                 /* SR3B[7][3]MAA15 MAA11 (Power on Trapping) */
98                 if (((temp & 0x88) == 0x80) || ((temp & 0x88) == 0x08))
99                         pVBInfo->XGINew_CR97 = 0x80;
100         }
101
102 }
103
104 static void XGI_SetSeqRegs(unsigned short ModeNo,
105                            unsigned short ModeIdIndex,
106                            struct vb_device_info *pVBInfo)
107 {
108         unsigned char tempah, SRdata;
109         unsigned short i, modeflag;
110
111         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
112
113         xgifb_reg_set(pVBInfo->P3c4, 0x00, 0x03); /* Set SR0 */
114         tempah = pVBInfo->StandTable->SR[0];
115
116         i = XGI_SetCRT2ToLCDA;
117         if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA) {
118                 tempah |= 0x01;
119         } else {
120                 if (pVBInfo->VBInfo & (SetCRT2ToTV | SetCRT2ToLCD)) {
121                         if (pVBInfo->VBInfo & SetInSlaveMode)
122                                 tempah |= 0x01;
123                 }
124         }
125
126         tempah |= 0x20; /* screen off */
127         xgifb_reg_set(pVBInfo->P3c4, 0x01, tempah); /* Set SR1 */
128
129         for (i = 02; i <= 04; i++) {
130                 /* Get SR2,3,4 from file */
131                 SRdata = pVBInfo->StandTable->SR[i - 1];
132                 xgifb_reg_set(pVBInfo->P3c4, i, SRdata); /* Set SR2 3 4 */
133         }
134 }
135
136 static void XGI_SetCRTCRegs(struct xgi_hw_device_info *HwDeviceExtension,
137                             struct vb_device_info *pVBInfo)
138 {
139         unsigned char CRTCdata;
140         unsigned short i;
141
142         CRTCdata = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x11);
143         CRTCdata &= 0x7f;
144         xgifb_reg_set(pVBInfo->P3d4, 0x11, CRTCdata); /* Unlock CRTC */
145
146         for (i = 0; i <= 0x18; i++) {
147                 /* Get CRTC from file */
148                 CRTCdata = pVBInfo->StandTable->CRTC[i];
149                 xgifb_reg_set(pVBInfo->P3d4, i, CRTCdata); /* Set CRTC(3d4) */
150         }
151 }
152
153 static void XGI_SetATTRegs(unsigned short ModeNo,
154                            unsigned short ModeIdIndex,
155                            struct vb_device_info *pVBInfo)
156 {
157         unsigned char ARdata;
158         unsigned short i, modeflag;
159
160         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
161
162         for (i = 0; i <= 0x13; i++) {
163                 ARdata = pVBInfo->StandTable->ATTR[i];
164
165                 if ((modeflag & Charx8Dot) && i == 0x13) { /* ifndef Dot9 */
166                         if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA) {
167                                 ARdata = 0;
168                         } else {
169                                 if ((pVBInfo->VBInfo &
170                                      (SetCRT2ToTV | SetCRT2ToLCD)) &&
171                                     (pVBInfo->VBInfo & SetInSlaveMode))
172                                         ARdata = 0;
173                         }
174                 }
175
176                 inb(pVBInfo->P3da); /* reset 3da */
177                 outb(i, pVBInfo->P3c0); /* set index */
178                 outb(ARdata, pVBInfo->P3c0); /* set data */
179         }
180
181         inb(pVBInfo->P3da); /* reset 3da */
182         outb(0x14, pVBInfo->P3c0); /* set index */
183         outb(0x00, pVBInfo->P3c0); /* set data */
184         inb(pVBInfo->P3da); /* Enable Attribute */
185         outb(0x20, pVBInfo->P3c0);
186 }
187
188 static void XGI_SetGRCRegs(struct vb_device_info *pVBInfo)
189 {
190         unsigned char GRdata;
191         unsigned short i;
192
193         for (i = 0; i <= 0x08; i++) {
194                 /* Get GR from file */
195                 GRdata = pVBInfo->StandTable->GRC[i];
196                 xgifb_reg_set(pVBInfo->P3ce, i, GRdata); /* Set GR(3ce) */
197         }
198
199         if (pVBInfo->ModeType > ModeVGA) {
200                 GRdata = (unsigned char) xgifb_reg_get(pVBInfo->P3ce, 0x05);
201                 GRdata &= 0xBF; /* 256 color disable */
202                 xgifb_reg_set(pVBInfo->P3ce, 0x05, GRdata);
203         }
204 }
205
206 static void XGI_ClearExt1Regs(struct vb_device_info *pVBInfo)
207 {
208         unsigned short i;
209
210         for (i = 0x0A; i <= 0x0E; i++)
211                 xgifb_reg_set(pVBInfo->P3c4, i, 0x00); /* Clear SR0A-SR0E */
212 }
213
214 static unsigned char XGI_SetDefaultVCLK(struct vb_device_info *pVBInfo)
215 {
216
217         xgifb_reg_and_or(pVBInfo->P3c4, 0x31, ~0x30, 0x20);
218         xgifb_reg_set(pVBInfo->P3c4, 0x2B, pVBInfo->VCLKData[0].SR2B);
219         xgifb_reg_set(pVBInfo->P3c4, 0x2C, pVBInfo->VCLKData[0].SR2C);
220
221         xgifb_reg_and_or(pVBInfo->P3c4, 0x31, ~0x30, 0x10);
222         xgifb_reg_set(pVBInfo->P3c4, 0x2B, pVBInfo->VCLKData[1].SR2B);
223         xgifb_reg_set(pVBInfo->P3c4, 0x2C, pVBInfo->VCLKData[1].SR2C);
224
225         xgifb_reg_and(pVBInfo->P3c4, 0x31, ~0x30);
226         return 0;
227 }
228
229 static unsigned char XGI_AjustCRT2Rate(unsigned short ModeNo,
230                 unsigned short ModeIdIndex,
231                 unsigned short RefreshRateTableIndex, unsigned short *i,
232                 struct vb_device_info *pVBInfo)
233 {
234         unsigned short tempax, tempbx, resinfo, modeflag, infoflag;
235
236         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
237         resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
238         tempbx = pVBInfo->RefIndex[RefreshRateTableIndex + (*i)].ModeID;
239         tempax = 0;
240
241         if (pVBInfo->IF_DEF_LVDS == 0) {
242                 if (pVBInfo->VBInfo & SetCRT2ToRAMDAC) {
243                         tempax |= SupportRAMDAC2;
244
245                         if (pVBInfo->VBType & VB_XGI301C)
246                                 tempax |= SupportCRT2in301C;
247                 }
248
249                 /* 301b */
250                 if (pVBInfo->VBInfo & (SetCRT2ToLCD | XGI_SetCRT2ToLCDA)) {
251                         tempax |= SupportLCD;
252
253                         if (pVBInfo->LCDResInfo != Panel_1280x1024 &&
254                             pVBInfo->LCDResInfo != Panel_1280x960 &&
255                             (pVBInfo->LCDInfo & LCDNonExpanding) &&
256                             resinfo >= 9)
257                                 return 0;
258                 }
259
260                 if (pVBInfo->VBInfo & SetCRT2ToHiVision) { /* for HiTV */
261                         if ((pVBInfo->VBType & VB_SIS301LV) &&
262                             (pVBInfo->VBExtInfo == VB_YPbPr1080i)) {
263                                 tempax |= SupportYPbPr750p;
264                                 if ((pVBInfo->VBInfo & SetInSlaveMode) &&
265                                     ((resinfo == 3) ||
266                                      (resinfo == 4) ||
267                                      (resinfo > 7)))
268                                         return 0;
269                         } else {
270                                 tempax |= SupportHiVision;
271                                 if ((pVBInfo->VBInfo & SetInSlaveMode) &&
272                                     ((resinfo == 4) ||
273                                      (resinfo == 3 &&
274                                       (pVBInfo->SetFlag & TVSimuMode)) ||
275                                      (resinfo > 7)))
276                                                 return 0;
277                         }
278                 } else {
279                         if (pVBInfo->VBInfo & (SetCRT2ToAVIDEO |
280                                                SetCRT2ToSVIDEO |
281                                                SetCRT2ToSCART |
282                                                SetCRT2ToYPbPr525750 |
283                                                SetCRT2ToHiVision)) {
284                                 tempax |= SupportTV;
285
286                                 if (pVBInfo->VBType & (VB_SIS301B |
287                                                        VB_SIS302B |
288                                                        VB_SIS301LV |
289                                                        VB_SIS302LV |
290                                                        VB_XGI301C))
291                                         tempax |= SupportTV1024;
292
293                                 if (!(pVBInfo->VBInfo & TVSetPAL) &&
294                                     (modeflag & NoSupportSimuTV) &&
295                                     (pVBInfo->VBInfo & SetInSlaveMode) &&
296                                     (!(pVBInfo->VBInfo & SetNotSimuMode)))
297                                         return 0;
298                         }
299                 }
300         } else { /* for LVDS */
301                 if (pVBInfo->VBInfo & SetCRT2ToLCD) {
302                         tempax |= SupportLCD;
303
304                         if (resinfo > 0x08)
305                                 return 0; /* 1024x768 */
306
307                         if (pVBInfo->LCDResInfo < Panel_1024x768) {
308                                 if (resinfo > 0x07)
309                                         return 0; /* 800x600 */
310
311                                 if (resinfo == 0x04)
312                                         return 0; /* 512x384 */
313                         }
314                 }
315         }
316
317         for (; pVBInfo->RefIndex[RefreshRateTableIndex + (*i)].ModeID ==
318                tempbx; (*i)--) {
319                 infoflag = pVBInfo->RefIndex[RefreshRateTableIndex + (*i)].
320                                 Ext_InfoFlag;
321                 if (infoflag & tempax)
322                         return 1;
323
324                 if ((*i) == 0)
325                         break;
326         }
327
328         for ((*i) = 0;; (*i)++) {
329                 infoflag = pVBInfo->RefIndex[RefreshRateTableIndex + (*i)].
330                                 Ext_InfoFlag;
331                 if (pVBInfo->RefIndex[RefreshRateTableIndex + (*i)].ModeID
332                                 != tempbx) {
333                         return 0;
334                 }
335
336                 if (infoflag & tempax)
337                         return 1;
338         }
339         return 1;
340 }
341
342 static void XGI_SetSync(unsigned short RefreshRateTableIndex,
343                 struct vb_device_info *pVBInfo)
344 {
345         unsigned short sync, temp;
346
347         /* di+0x00 */
348         sync = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_InfoFlag >> 8;
349         sync &= 0xC0;
350         temp = 0x2F;
351         temp |= sync;
352         outb(temp, pVBInfo->P3c2); /* Set Misc(3c2) */
353 }
354
355 static void XGI_SetCRT1Timing_H(struct vb_device_info *pVBInfo,
356                 struct xgi_hw_device_info *HwDeviceExtension)
357 {
358         unsigned char data, data1, pushax;
359         unsigned short i, j;
360
361         /* unlock cr0-7 */
362         data = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x11);
363         data &= 0x7F;
364         xgifb_reg_set(pVBInfo->P3d4, 0x11, data);
365
366         data = pVBInfo->TimingH[0].data[0];
367         xgifb_reg_set(pVBInfo->P3d4, 0, data);
368
369         for (i = 0x01; i <= 0x04; i++) {
370                 data = pVBInfo->TimingH[0].data[i];
371                 xgifb_reg_set(pVBInfo->P3d4, (unsigned short) (i + 1), data);
372         }
373
374         for (i = 0x05; i <= 0x06; i++) {
375                 data = pVBInfo->TimingH[0].data[i];
376                 xgifb_reg_set(pVBInfo->P3c4, (unsigned short) (i + 6), data);
377         }
378
379         j = (unsigned char) xgifb_reg_get(pVBInfo->P3c4, 0x0e);
380         j &= 0x1F;
381         data = pVBInfo->TimingH[0].data[7];
382         data &= 0xE0;
383         data |= j;
384         xgifb_reg_set(pVBInfo->P3c4, 0x0e, data);
385
386         if (HwDeviceExtension->jChipType >= XG20) {
387                 data = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x04);
388                 data = data - 1;
389                 xgifb_reg_set(pVBInfo->P3d4, 0x04, data);
390                 data = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x05);
391                 data1 = data;
392                 data1 &= 0xE0;
393                 data &= 0x1F;
394                 if (data == 0) {
395                         pushax = data;
396                         data = (unsigned char) xgifb_reg_get(pVBInfo->P3c4,
397                                         0x0c);
398                         data &= 0xFB;
399                         xgifb_reg_set(pVBInfo->P3c4, 0x0c, data);
400                         data = pushax;
401                 }
402                 data = data - 1;
403                 data |= data1;
404                 xgifb_reg_set(pVBInfo->P3d4, 0x05, data);
405                 data = (unsigned char) xgifb_reg_get(pVBInfo->P3c4, 0x0e);
406                 data = data >> 5;
407                 data = data + 3;
408                 if (data > 7)
409                         data = data - 7;
410                 data = data << 5;
411                 xgifb_reg_and_or(pVBInfo->P3c4, 0x0e, ~0xE0, data);
412         }
413 }
414
415 static void XGI_SetCRT1Timing_V(unsigned short ModeIdIndex,
416                                 unsigned short ModeNo,
417                                 struct vb_device_info *pVBInfo)
418 {
419         unsigned char data;
420         unsigned short i, j;
421
422         for (i = 0x00; i <= 0x01; i++) {
423                 data = pVBInfo->TimingV[0].data[i];
424                 xgifb_reg_set(pVBInfo->P3d4, (unsigned short) (i + 6), data);
425         }
426
427         for (i = 0x02; i <= 0x03; i++) {
428                 data = pVBInfo->TimingV[0].data[i];
429                 xgifb_reg_set(pVBInfo->P3d4, (unsigned short) (i + 0x0e), data);
430         }
431
432         for (i = 0x04; i <= 0x05; i++) {
433                 data = pVBInfo->TimingV[0].data[i];
434                 xgifb_reg_set(pVBInfo->P3d4, (unsigned short) (i + 0x11), data);
435         }
436
437         j = (unsigned char) xgifb_reg_get(pVBInfo->P3c4, 0x0a);
438         j &= 0xC0;
439         data = pVBInfo->TimingV[0].data[6];
440         data &= 0x3F;
441         data |= j;
442         xgifb_reg_set(pVBInfo->P3c4, 0x0a, data);
443
444         data = pVBInfo->TimingV[0].data[6];
445         data &= 0x80;
446         data = data >> 2;
447
448         i = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
449         i &= DoubleScanMode;
450         if (i)
451                 data |= 0x80;
452
453         j = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x09);
454         j &= 0x5F;
455         data |= j;
456         xgifb_reg_set(pVBInfo->P3d4, 0x09, data);
457 }
458
459 static void XGI_SetCRT1CRTC(unsigned short ModeNo, unsigned short ModeIdIndex,
460                 unsigned short RefreshRateTableIndex,
461                 struct vb_device_info *pVBInfo,
462                 struct xgi_hw_device_info *HwDeviceExtension)
463 {
464         unsigned char index, data;
465         unsigned short i;
466
467         /* Get index */
468         index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
469         index = index & IndexMask;
470
471         data = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x11);
472         data &= 0x7F;
473         xgifb_reg_set(pVBInfo->P3d4, 0x11, data); /* Unlock CRTC */
474
475         for (i = 0; i < 8; i++)
476                 pVBInfo->TimingH[0].data[i]
477                                 = pVBInfo->XGINEWUB_CRT1Table[index].CR[i];
478
479         for (i = 0; i < 7; i++)
480                 pVBInfo->TimingV[0].data[i]
481                                 = pVBInfo->XGINEWUB_CRT1Table[index].CR[i + 8];
482
483         XGI_SetCRT1Timing_H(pVBInfo, HwDeviceExtension);
484
485         XGI_SetCRT1Timing_V(ModeIdIndex, ModeNo, pVBInfo);
486
487         if (pVBInfo->ModeType > 0x03)
488                 xgifb_reg_set(pVBInfo->P3d4, 0x14, 0x4F);
489 }
490
491 /* --------------------------------------------------------------------- */
492 /* Function : XGI_SetXG21CRTC */
493 /* Input : Stand or enhance CRTC table */
494 /* Output : Fill CRT Hsync/Vsync to SR2E/SR2F/SR30/SR33/SR34/SR3F */
495 /* Description : Set LCD timing */
496 /* --------------------------------------------------------------------- */
497 static void XGI_SetXG21CRTC(unsigned short ModeNo, unsigned short ModeIdIndex,
498                 unsigned short RefreshRateTableIndex,
499                 struct vb_device_info *pVBInfo)
500 {
501         unsigned char index, Tempax, Tempbx, Tempcx, Tempdx;
502         unsigned short Temp1, Temp2, Temp3;
503
504         index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
505         /* Tempax: CR4 HRS */
506         Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[3];
507         Tempcx = Tempax; /* Tempcx: HRS */
508         /* SR2E[7:0]->HRS */
509         xgifb_reg_set(pVBInfo->P3c4, 0x2E, Tempax);
510
511         Tempdx = pVBInfo->XGINEWUB_CRT1Table[index].CR[5]; /* SRB */
512         Tempdx &= 0xC0; /* Tempdx[7:6]: SRB[7:6] */
513         Temp1 = Tempdx; /* Temp1[7:6]: HRS[9:8] */
514         Temp1 <<= 2; /* Temp1[9:8]: HRS[9:8] */
515         Temp1 |= Tempax; /* Temp1[9:0]: HRS[9:0] */
516
517         Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[4]; /* CR5 HRE */
518         Tempax &= 0x1F; /* Tempax[4:0]: HRE[4:0] */
519
520         Tempbx = pVBInfo->XGINEWUB_CRT1Table[index].CR[6]; /* SRC */
521         Tempbx &= 0x04; /* Tempbx[2]: HRE[5] */
522         Tempbx <<= 3; /* Tempbx[5]: HRE[5] */
523         Tempax |= Tempbx; /* Tempax[5:0]: HRE[5:0] */
524
525         Temp2 = Temp1 & 0x3C0; /* Temp2[9:6]: HRS[9:6] */
526         Temp2 |= Tempax; /* Temp2[9:0]: HRE[9:0] */
527
528         Tempcx &= 0x3F; /* Tempcx[5:0]: HRS[5:0] */
529         if (Tempax < Tempcx) /* HRE < HRS */
530                 Temp2 |= 0x40; /* Temp2 + 0x40 */
531
532         Temp2 &= 0xFF;
533         Tempax = (unsigned char) Temp2; /* Tempax: HRE[7:0] */
534         Tempax <<= 2; /* Tempax[7:2]: HRE[5:0] */
535         Tempdx >>= 6; /* Tempdx[7:6]->[1:0] HRS[9:8] */
536         Tempax |= Tempdx; /* HRE[5:0]HRS[9:8] */
537         /* SR2F D[7:2]->HRE, D[1:0]->HRS */
538         xgifb_reg_set(pVBInfo->P3c4, 0x2F, Tempax);
539         xgifb_reg_and_or(pVBInfo->P3c4, 0x30, 0xE3, 00);
540
541         /* CR10 VRS */
542         Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[10];
543         Tempbx = Tempax; /* Tempbx: VRS */
544         Tempax &= 0x01; /* Tempax[0]: VRS[0] */
545         xgifb_reg_or(pVBInfo->P3c4, 0x33, Tempax); /* SR33[0]->VRS[0] */
546         /* CR7[2][7] VRE */
547         Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[9];
548         Tempcx = Tempbx >> 1; /* Tempcx[6:0]: VRS[7:1] */
549         Tempdx = Tempax & 0x04; /* Tempdx[2]: CR7[2] */
550         Tempdx <<= 5; /* Tempdx[7]: VRS[8] */
551         Tempcx |= Tempdx; /* Tempcx[7:0]: VRS[8:1] */
552         xgifb_reg_set(pVBInfo->P3c4, 0x34, Tempcx); /* SR34[8:1]->VRS */
553
554         Temp1 = Tempdx; /* Temp1[7]: Tempdx[7] */
555         Temp1 <<= 1; /* Temp1[8]: VRS[8] */
556         Temp1 |= Tempbx; /* Temp1[8:0]: VRS[8:0] */
557         Tempax &= 0x80;
558         Temp2 = Tempax << 2; /* Temp2[9]: VRS[9] */
559         Temp1 |= Temp2; /* Temp1[9:0]: VRS[9:0] */
560         /* Tempax: SRA */
561         Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[14];
562         Tempax &= 0x08; /* Tempax[3]: VRS[3] */
563         Temp2 = Tempax;
564         Temp2 <<= 7; /* Temp2[10]: VRS[10] */
565         Temp1 |= Temp2; /* Temp1[10:0]: VRS[10:0] */
566
567         /* Tempax: CR11 VRE */
568         Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[11];
569         Tempax &= 0x0F; /* Tempax[3:0]: VRE[3:0] */
570         /* Tempbx: SRA */
571         Tempbx = pVBInfo->XGINEWUB_CRT1Table[index].CR[14];
572         Tempbx &= 0x20; /* Tempbx[5]: VRE[5] */
573         Tempbx >>= 1; /* Tempbx[4]: VRE[4] */
574         Tempax |= Tempbx; /* Tempax[4:0]: VRE[4:0] */
575         Temp2 = Temp1 & 0x7E0; /* Temp2[10:5]: VRS[10:5] */
576         Temp2 |= Tempax; /* Temp2[10:5]: VRE[10:5] */
577
578         Temp3 = Temp1 & 0x1F; /* Temp3[4:0]: VRS[4:0] */
579         if (Tempax < Temp3) /* VRE < VRS */
580                 Temp2 |= 0x20; /* VRE + 0x20 */
581
582         Temp2 &= 0xFF;
583         Tempax = (unsigned char) Temp2; /* Tempax: VRE[7:0] */
584         Tempax <<= 2; /* Tempax[7:0]; VRE[5:0]00 */
585         Temp1 &= 0x600; /* Temp1[10:9]: VRS[10:9] */
586         Temp1 >>= 9; /* Temp1[1:0]: VRS[10:9] */
587         Tempbx = (unsigned char) Temp1;
588         Tempax |= Tempbx; /* Tempax[7:0]: VRE[5:0]VRS[10:9] */
589         Tempax &= 0x7F;
590         /* SR3F D[7:2]->VRE D[1:0]->VRS */
591         xgifb_reg_set(pVBInfo->P3c4, 0x3F, Tempax);
592 }
593
594 static void XGI_SetXG27CRTC(unsigned short ModeNo,
595                             unsigned short ModeIdIndex,
596                             unsigned short RefreshRateTableIndex,
597                             struct vb_device_info *pVBInfo)
598 {
599         unsigned short index, Tempax, Tempbx, Tempcx;
600
601         index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
602         /* Tempax: CR4 HRS */
603         Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[3];
604         Tempbx = Tempax; /* Tempbx: HRS[7:0] */
605         /* SR2E[7:0]->HRS */
606         xgifb_reg_set(pVBInfo->P3c4, 0x2E, Tempax);
607
608         /* SR0B */
609         Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[5];
610         Tempax &= 0xC0; /* Tempax[7:6]: SR0B[7:6]: HRS[9:8]*/
611         Tempbx |= (Tempax << 2); /* Tempbx: HRS[9:0] */
612
613         Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[4]; /* CR5 HRE */
614         Tempax &= 0x1F; /* Tempax[4:0]: HRE[4:0] */
615         Tempcx = Tempax; /* Tempcx: HRE[4:0] */
616
617         Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[6]; /* SRC */
618         Tempax &= 0x04; /* Tempax[2]: HRE[5] */
619         Tempax <<= 3; /* Tempax[5]: HRE[5] */
620         Tempcx |= Tempax; /* Tempcx[5:0]: HRE[5:0] */
621
622         Tempbx = Tempbx & 0x3C0; /* Tempbx[9:6]: HRS[9:6] */
623         Tempbx |= Tempcx; /* Tempbx: HRS[9:6]HRE[5:0] */
624
625         /* Tempax: CR4 HRS */
626         Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[3];
627         Tempax &= 0x3F; /* Tempax: HRS[5:0] */
628         if (Tempcx <= Tempax) /* HRE[5:0] < HRS[5:0] */
629                 Tempbx += 0x40; /* Tempbx= Tempbx + 0x40 : HRE[9:0]*/
630
631         Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[5]; /* SR0B */
632         Tempax &= 0xC0; /* Tempax[7:6]: SR0B[7:6]: HRS[9:8]*/
633         Tempax >>= 6; /* Tempax[1:0]: HRS[9:8]*/
634         Tempax |= ((Tempbx << 2) & 0xFF); /* Tempax[7:2]: HRE[5:0] */
635         /* SR2F [7:2][1:0]: HRE[5:0]HRS[9:8] */
636         xgifb_reg_set(pVBInfo->P3c4, 0x2F, Tempax);
637         xgifb_reg_and_or(pVBInfo->P3c4, 0x30, 0xE3, 00);
638
639         /* CR10 VRS */
640         Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[10];
641         /* SR34[7:0]->VRS[7:0] */
642         xgifb_reg_set(pVBInfo->P3c4, 0x34, Tempax);
643
644         Tempcx = Tempax; /* Tempcx <= VRS[7:0] */
645         /* CR7[7][2] VRS[9][8] */
646         Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[9];
647         Tempbx = Tempax; /* Tempbx <= CR07[7:0] */
648         Tempax = Tempax & 0x04; /* Tempax[2]: CR7[2]: VRS[8] */
649         Tempax >>= 2; /* Tempax[0]: VRS[8] */
650         /* SR35[0]: VRS[8] */
651         xgifb_reg_and_or(pVBInfo->P3c4, 0x35, ~0x01, Tempax);
652         Tempcx |= (Tempax << 8); /* Tempcx <= VRS[8:0] */
653         Tempcx |= ((Tempbx & 0x80) << 2); /* Tempcx <= VRS[9:0] */
654         /* Tempax: SR0A */
655         Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[14];
656         Tempax &= 0x08; /* SR0A[3] VRS[10] */
657         Tempcx |= (Tempax << 7); /* Tempcx <= VRS[10:0] */
658
659         /* Tempax: CR11 VRE */
660         Tempax = pVBInfo->XGINEWUB_CRT1Table[index].CR[11];
661         Tempax &= 0x0F; /* Tempax[3:0]: VRE[3:0] */
662         /* Tempbx: SR0A */
663         Tempbx = pVBInfo->XGINEWUB_CRT1Table[index].CR[14];
664         Tempbx &= 0x20; /* Tempbx[5]: SR0A[5]: VRE[4] */
665         Tempbx >>= 1; /* Tempbx[4]: VRE[4] */
666         Tempax |= Tempbx; /* Tempax[4:0]: VRE[4:0] */
667         Tempbx = Tempcx; /* Tempbx: VRS[10:0] */
668         Tempbx &= 0x7E0; /* Tempbx[10:5]: VRS[10:5] */
669         Tempbx |= Tempax; /* Tempbx: VRS[10:5]VRE[4:0] */
670
671         if (Tempbx <= Tempcx) /* VRE <= VRS */
672                 Tempbx |= 0x20; /* VRE + 0x20 */
673
674         /* Tempax: Tempax[7:0]; VRE[5:0]00 */
675         Tempax = (Tempbx << 2) & 0xFF;
676         /* SR3F[7:2]:VRE[5:0] */
677         xgifb_reg_and_or(pVBInfo->P3c4, 0x3F, ~0xFC, Tempax);
678         Tempax = Tempcx >> 8;
679         /* SR35[2:0]:VRS[10:8] */
680         xgifb_reg_and_or(pVBInfo->P3c4, 0x35, ~0x07, Tempax);
681 }
682
683 static void XGI_SetXG27FPBits(struct vb_device_info *pVBInfo)
684 {
685         unsigned char temp;
686
687         /* D[1:0] 01: 18bit, 00: dual 12, 10: single 24 */
688         temp = xgifb_reg_get(pVBInfo->P3d4, 0x37);
689         temp = (temp & 3) << 6;
690         /* SR06[7]0: dual 12/1: single 24 [6] 18bit Dither <= 0 h/w recommend */
691         xgifb_reg_and_or(pVBInfo->P3c4, 0x06, ~0xc0, temp & 0x80);
692         /* SR09[7] enable FP output, SR09[6] 1: sigle 18bits, 0: 24bits */
693         xgifb_reg_and_or(pVBInfo->P3c4, 0x09, ~0xc0, temp | 0x80);
694
695 }
696
697 static void xgifb_set_lcd(int chip_id,
698                           struct vb_device_info *pVBInfo,
699                           unsigned short RefreshRateTableIndex,
700                           unsigned short ModeNo)
701 {
702         unsigned short Data, Temp;
703         unsigned short XGI_P3cc;
704
705         XGI_P3cc = pVBInfo->P3cc;
706
707         xgifb_reg_set(pVBInfo->P3d4, 0x2E, 0x00);
708         xgifb_reg_set(pVBInfo->P3d4, 0x2F, 0x00);
709         xgifb_reg_set(pVBInfo->P3d4, 0x46, 0x00);
710         xgifb_reg_set(pVBInfo->P3d4, 0x47, 0x00);
711
712         if (chip_id == XG27) {
713                 Temp = xgifb_reg_get(pVBInfo->P3d4, 0x37);
714                 if ((Temp & 0x03) == 0) { /* dual 12 */
715                         xgifb_reg_set(pVBInfo->P3d4, 0x46, 0x13);
716                         xgifb_reg_set(pVBInfo->P3d4, 0x47, 0x13);
717                 }
718         }
719
720         if (chip_id == XG27) {
721                 XGI_SetXG27FPBits(pVBInfo);
722         } else {
723                 Temp = xgifb_reg_get(pVBInfo->P3d4, 0x37);
724                 if (Temp & 0x01) {
725                         /* 18 bits FP */
726                         xgifb_reg_or(pVBInfo->P3c4, 0x06, 0x40);
727                         xgifb_reg_or(pVBInfo->P3c4, 0x09, 0x40);
728                 }
729         }
730
731         xgifb_reg_or(pVBInfo->P3c4, 0x1E, 0x01); /* Negative blank polarity */
732
733         xgifb_reg_and(pVBInfo->P3c4, 0x30, ~0x20); /* Hsync polarity */
734         xgifb_reg_and(pVBInfo->P3c4, 0x35, ~0x80); /* Vsync polarity */
735
736         Data = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
737         if (Data & 0x4000)
738                 /* Hsync polarity */
739                 xgifb_reg_or(pVBInfo->P3c4, 0x30, 0x20);
740         if (Data & 0x8000)
741                 /* Vsync polarity */
742                 xgifb_reg_or(pVBInfo->P3c4, 0x35, 0x80);
743 }
744
745 /* --------------------------------------------------------------------- */
746 /* Function : XGI_UpdateXG21CRTC */
747 /* Input : */
748 /* Output : CRT1 CRTC */
749 /* Description : Modify CRT1 Hsync/Vsync to fix LCD mode timing */
750 /* --------------------------------------------------------------------- */
751 static void XGI_UpdateXG21CRTC(unsigned short ModeNo,
752                                struct vb_device_info *pVBInfo,
753                                unsigned short RefreshRateTableIndex)
754 {
755         int index = -1;
756
757         xgifb_reg_and(pVBInfo->P3d4, 0x11, 0x7F); /* Unlock CR0~7 */
758         if (ModeNo == 0x2E &&
759             (pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC ==
760                                                       RES640x480x60))
761                 index = 12;
762         else if (ModeNo == 0x2E && (pVBInfo->RefIndex[RefreshRateTableIndex].
763                                 Ext_CRT1CRTC == RES640x480x72))
764                 index = 13;
765         else if (ModeNo == 0x2F)
766                 index = 14;
767         else if (ModeNo == 0x50)
768                 index = 15;
769         else if (ModeNo == 0x59)
770                 index = 16;
771
772         if (index != -1) {
773                 xgifb_reg_set(pVBInfo->P3d4, 0x02,
774                                 pVBInfo->UpdateCRT1[index].CR02);
775                 xgifb_reg_set(pVBInfo->P3d4, 0x03,
776                                 pVBInfo->UpdateCRT1[index].CR03);
777                 xgifb_reg_set(pVBInfo->P3d4, 0x15,
778                                 pVBInfo->UpdateCRT1[index].CR15);
779                 xgifb_reg_set(pVBInfo->P3d4, 0x16,
780                                 pVBInfo->UpdateCRT1[index].CR16);
781         }
782 }
783
784 static void XGI_SetCRT1DE(struct xgi_hw_device_info *HwDeviceExtension,
785                 unsigned short ModeNo, unsigned short ModeIdIndex,
786                 unsigned short RefreshRateTableIndex,
787                 struct vb_device_info *pVBInfo)
788 {
789         unsigned short resindex, tempax, tempbx, tempcx, temp, modeflag;
790
791         unsigned char data;
792
793         resindex = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
794
795         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
796         tempax = pVBInfo->ModeResInfo[resindex].HTotal;
797         tempbx = pVBInfo->ModeResInfo[resindex].VTotal;
798
799         if (modeflag & HalfDCLK)
800                 tempax = tempax >> 1;
801
802         if (modeflag & HalfDCLK)
803                 tempax = tempax << 1;
804
805         temp = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
806
807         if (temp & InterlaceMode)
808                 tempbx = tempbx >> 1;
809
810         if (modeflag & DoubleScanMode)
811                 tempbx = tempbx << 1;
812
813         tempcx = 8;
814
815         tempax /= tempcx;
816         tempax -= 1;
817         tempbx -= 1;
818         tempcx = tempax;
819         temp = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x11);
820         data = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x11);
821         data &= 0x7F;
822         xgifb_reg_set(pVBInfo->P3d4, 0x11, data); /* Unlock CRTC */
823         xgifb_reg_set(pVBInfo->P3d4, 0x01, (unsigned short) (tempcx & 0xff));
824         xgifb_reg_and_or(pVBInfo->P3d4, 0x0b, ~0x0c,
825                         (unsigned short) ((tempcx & 0x0ff00) >> 10));
826         xgifb_reg_set(pVBInfo->P3d4, 0x12, (unsigned short) (tempbx & 0xff));
827         tempax = 0;
828         tempbx = tempbx >> 8;
829
830         if (tempbx & 0x01)
831                 tempax |= 0x02;
832
833         if (tempbx & 0x02)
834                 tempax |= 0x40;
835
836         xgifb_reg_and_or(pVBInfo->P3d4, 0x07, ~0x42, tempax);
837         data = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x07);
838         data &= 0xFF;
839         tempax = 0;
840
841         if (tempbx & 0x04)
842                 tempax |= 0x02;
843
844         xgifb_reg_and_or(pVBInfo->P3d4, 0x0a, ~0x02, tempax);
845         xgifb_reg_set(pVBInfo->P3d4, 0x11, temp);
846 }
847
848 static void XGI_SetCRT1Offset(unsigned short ModeNo,
849                               unsigned short ModeIdIndex,
850                               unsigned short RefreshRateTableIndex,
851                               struct xgi_hw_device_info *HwDeviceExtension,
852                               struct vb_device_info *pVBInfo)
853 {
854         unsigned short temp, ah, al, temp2, i, DisplayUnit;
855
856         /* GetOffset */
857         temp = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeInfo;
858         temp = temp >> 8;
859         temp = pVBInfo->ScreenOffset[temp];
860
861         temp2 = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
862         temp2 &= InterlaceMode;
863
864         if (temp2)
865                 temp = temp << 1;
866
867         temp2 = pVBInfo->ModeType - ModeEGA;
868
869         switch (temp2) {
870         case 0:
871                 temp2 = 1;
872                 break;
873         case 1:
874                 temp2 = 2;
875                 break;
876         case 2:
877                 temp2 = 4;
878                 break;
879         case 3:
880                 temp2 = 4;
881                 break;
882         case 4:
883                 temp2 = 6;
884                 break;
885         case 5:
886                 temp2 = 8;
887                 break;
888         default:
889                 break;
890         }
891
892         if ((ModeNo >= 0x26) && (ModeNo <= 0x28))
893                 temp = temp * temp2 + temp2 / 2;
894         else
895                 temp *= temp2;
896
897         /* SetOffset */
898         DisplayUnit = temp;
899         temp2 = temp;
900         temp = temp >> 8; /* ah */
901         temp &= 0x0F;
902         i = xgifb_reg_get(pVBInfo->P3c4, 0x0E);
903         i &= 0xF0;
904         i |= temp;
905         xgifb_reg_set(pVBInfo->P3c4, 0x0E, i);
906
907         temp = (unsigned char) temp2;
908         temp &= 0xFF; /* al */
909         xgifb_reg_set(pVBInfo->P3d4, 0x13, temp);
910
911         /* SetDisplayUnit */
912         temp2 = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
913         temp2 &= InterlaceMode;
914         if (temp2)
915                 DisplayUnit >>= 1;
916
917         DisplayUnit = DisplayUnit << 5;
918         ah = (DisplayUnit & 0xff00) >> 8;
919         al = DisplayUnit & 0x00ff;
920         if (al == 0)
921                 ah += 1;
922         else
923                 ah += 2;
924
925         if (HwDeviceExtension->jChipType >= XG20)
926                 if ((ModeNo == 0x4A) | (ModeNo == 0x49))
927                         ah -= 1;
928
929         xgifb_reg_set(pVBInfo->P3c4, 0x10, ah);
930 }
931
932 static unsigned short XGI_GetVCLK2Ptr(unsigned short ModeNo,
933                 unsigned short ModeIdIndex,
934                 unsigned short RefreshRateTableIndex,
935                 struct xgi_hw_device_info *HwDeviceExtension,
936                 struct vb_device_info *pVBInfo)
937 {
938         unsigned short CRT2Index, VCLKIndex;
939         unsigned short modeflag, resinfo;
940
941         /* si+Ext_ResInfo */
942         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
943         resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
944         CRT2Index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
945
946         if (pVBInfo->IF_DEF_LVDS == 0) {
947                 CRT2Index = CRT2Index >> 6; /*  for LCD */
948                 if (pVBInfo->VBInfo & (SetCRT2ToLCD | XGI_SetCRT2ToLCDA)) { /*301b*/
949                         if (pVBInfo->LCDResInfo != Panel_1024x768)
950                                 VCLKIndex =  VCLK108_2_315 + 5; /* LCDXlat2VCLK */
951                         else
952                                 VCLKIndex = VCLK65_315 + 2; /* LCDXlat1VCLK */
953                 } else if (pVBInfo->VBInfo & SetCRT2ToHiVision) {
954                         if (pVBInfo->SetFlag & RPLLDIV2XO) {
955                                 VCLKIndex = TVCLKBASE_315_25 + HiTVVCLKDIV2;
956                         } else {
957                                 VCLKIndex = TVCLKBASE_315_25 + HiTVVCLK;
958                         }
959
960                         if (pVBInfo->SetFlag & TVSimuMode) {
961                                 if (modeflag & Charx8Dot) {
962                                         VCLKIndex = TVCLKBASE_315_25 + HiTVSimuVCLK;
963                                 } else {
964                                         VCLKIndex = TVCLKBASE_315_25 + HiTVTextVCLK;
965                                 }
966                         }
967
968                         /* 301lv */
969                         if ((pVBInfo->VBType & VB_SIS301LV) &&
970                             !(pVBInfo->VBExtInfo == VB_YPbPr1080i)) {
971                                 if (pVBInfo->VBExtInfo == YPbPr750p)
972                                         VCLKIndex = XGI_YPbPr750pVCLK;
973                                 else if (pVBInfo->VBExtInfo == YPbPr525p)
974                                         VCLKIndex = YPbPr525pVCLK;
975                                 else if (pVBInfo->SetFlag & RPLLDIV2XO)
976                                         VCLKIndex = YPbPr525iVCLK_2;
977                                 else
978                                         VCLKIndex = YPbPr525iVCLK;
979                         }
980                 } else if (pVBInfo->VBInfo & SetCRT2ToTV) {
981                         if (pVBInfo->SetFlag & RPLLDIV2XO) {
982                                 VCLKIndex = TVCLKBASE_315_25 + TVVCLKDIV2;
983                         } else {
984                                 VCLKIndex = TVCLKBASE_315_25 + TVVCLK;
985                         }
986                 } else { /* for CRT2 */
987                         /* di+Ext_CRTVCLK */
988                         VCLKIndex = pVBInfo->RefIndex[RefreshRateTableIndex].
989                                                                 Ext_CRTVCLK;
990                         VCLKIndex &= IndexMask;
991                 }
992         } else { /* LVDS */
993                 if ((pVBInfo->LCDResInfo == Panel_800x600) ||
994                     (pVBInfo->LCDResInfo == Panel_320x480))
995                         VCLKIndex = VCLK40; /* LVDSXlat1VCLK */
996                 else
997                         VCLKIndex = VCLK65_315 + 2; /* LVDSXlat2VCLK, LVDSXlat3VCLK  */
998         }
999
1000         return VCLKIndex;
1001 }
1002
1003 static void XGI_SetCRT1VCLK(unsigned short ModeNo,
1004                             unsigned short ModeIdIndex,
1005                             struct xgi_hw_device_info *HwDeviceExtension,
1006                             unsigned short RefreshRateTableIndex,
1007                             struct vb_device_info *pVBInfo)
1008 {
1009         unsigned char index, data;
1010         unsigned short vclkindex;
1011
1012         if (pVBInfo->IF_DEF_LVDS == 1) {
1013                 index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
1014                 data = xgifb_reg_get(pVBInfo->P3c4, 0x31) & 0xCF;
1015                 xgifb_reg_set(pVBInfo->P3c4, 0x31, data);
1016                 xgifb_reg_set(pVBInfo->P3c4, 0x2B,
1017                                 pVBInfo->VCLKData[index].SR2B);
1018                 xgifb_reg_set(pVBInfo->P3c4, 0x2C,
1019                                 pVBInfo->VCLKData[index].SR2C);
1020                 xgifb_reg_set(pVBInfo->P3c4, 0x2D, 0x01);
1021         } else if ((pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
1022                         | VB_SIS302LV | VB_XGI301C)) && (pVBInfo->VBInfo
1023                         & XGI_SetCRT2ToLCDA)) {
1024                 vclkindex = XGI_GetVCLK2Ptr(ModeNo, ModeIdIndex,
1025                                 RefreshRateTableIndex, HwDeviceExtension,
1026                                 pVBInfo);
1027                 data = xgifb_reg_get(pVBInfo->P3c4, 0x31) & 0xCF;
1028                 xgifb_reg_set(pVBInfo->P3c4, 0x31, data);
1029                 data = pVBInfo->VBVCLKData[vclkindex].Part4_A;
1030                 xgifb_reg_set(pVBInfo->P3c4, 0x2B, data);
1031                 data = pVBInfo->VBVCLKData[vclkindex].Part4_B;
1032                 xgifb_reg_set(pVBInfo->P3c4, 0x2C, data);
1033                 xgifb_reg_set(pVBInfo->P3c4, 0x2D, 0x01);
1034         } else {
1035                 index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
1036                 data = xgifb_reg_get(pVBInfo->P3c4, 0x31) & 0xCF;
1037                 xgifb_reg_set(pVBInfo->P3c4, 0x31, data);
1038                 xgifb_reg_set(pVBInfo->P3c4, 0x2B,
1039                                 pVBInfo->VCLKData[index].SR2B);
1040                 xgifb_reg_set(pVBInfo->P3c4, 0x2C,
1041                                 pVBInfo->VCLKData[index].SR2C);
1042                 xgifb_reg_set(pVBInfo->P3c4, 0x2D, 0x01);
1043         }
1044
1045         if (HwDeviceExtension->jChipType >= XG20) {
1046                 if (pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag &
1047                     HalfDCLK) {
1048                         data = xgifb_reg_get(pVBInfo->P3c4, 0x2B);
1049                         xgifb_reg_set(pVBInfo->P3c4, 0x2B, data);
1050                         data = xgifb_reg_get(pVBInfo->P3c4, 0x2C);
1051                         index = data;
1052                         index &= 0xE0;
1053                         data &= 0x1F;
1054                         data = data << 1;
1055                         data += 1;
1056                         data |= index;
1057                         xgifb_reg_set(pVBInfo->P3c4, 0x2C, data);
1058                 }
1059         }
1060 }
1061
1062 static void XGI_SetXG21FPBits(struct vb_device_info *pVBInfo)
1063 {
1064         unsigned char temp;
1065
1066         temp = xgifb_reg_get(pVBInfo->P3d4, 0x37); /* D[0] 1: 18bit */
1067         temp = (temp & 1) << 6;
1068         /* SR06[6] 18bit Dither */
1069         xgifb_reg_and_or(pVBInfo->P3c4, 0x06, ~0x40, temp);
1070         /* SR09[7] enable FP output, SR09[6] 1: sigle 18bits, 0: dual 12bits */
1071         xgifb_reg_and_or(pVBInfo->P3c4, 0x09, ~0xc0, temp | 0x80);
1072
1073 }
1074
1075 static void XGI_SetCRT1FIFO(unsigned short ModeNo,
1076                 struct xgi_hw_device_info *HwDeviceExtension,
1077                 struct vb_device_info *pVBInfo)
1078 {
1079         unsigned short data;
1080
1081         data = xgifb_reg_get(pVBInfo->P3c4, 0x3D);
1082         data &= 0xfe;
1083         xgifb_reg_set(pVBInfo->P3c4, 0x3D, data); /* diable auto-threshold */
1084
1085         xgifb_reg_set(pVBInfo->P3c4, 0x08, 0x34);
1086         data = xgifb_reg_get(pVBInfo->P3c4, 0x09);
1087         data &= 0xC0;
1088         xgifb_reg_set(pVBInfo->P3c4, 0x09, data | 0x30);
1089         data = xgifb_reg_get(pVBInfo->P3c4, 0x3D);
1090         data |= 0x01;
1091         xgifb_reg_set(pVBInfo->P3c4, 0x3D, data);
1092
1093         if (HwDeviceExtension->jChipType == XG21)
1094                 XGI_SetXG21FPBits(pVBInfo); /* Fix SR9[7:6] can't read back */
1095 }
1096
1097 static void XGI_SetVCLKState(struct xgi_hw_device_info *HwDeviceExtension,
1098                 unsigned short ModeNo, unsigned short RefreshRateTableIndex,
1099                 struct vb_device_info *pVBInfo)
1100 {
1101         unsigned short data, data2 = 0;
1102         short VCLK;
1103
1104         unsigned char index;
1105
1106         index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
1107         index &= IndexMask;
1108         VCLK = pVBInfo->VCLKData[index].CLOCK;
1109
1110         data = xgifb_reg_get(pVBInfo->P3c4, 0x32);
1111         data &= 0xf3;
1112         if (VCLK >= 200)
1113                 data |= 0x0c; /* VCLK > 200 */
1114
1115         if (HwDeviceExtension->jChipType >= XG20)
1116                 data &= ~0x04; /* 2 pixel mode */
1117
1118         xgifb_reg_set(pVBInfo->P3c4, 0x32, data);
1119
1120         if (HwDeviceExtension->jChipType < XG20) {
1121                 data = xgifb_reg_get(pVBInfo->P3c4, 0x1F);
1122                 data &= 0xE7;
1123                 if (VCLK < 200)
1124                         data |= 0x10;
1125                 xgifb_reg_set(pVBInfo->P3c4, 0x1F, data);
1126         }
1127
1128         data2 = 0x00;
1129
1130         xgifb_reg_and_or(pVBInfo->P3c4, 0x07, 0xFC, data2);
1131         if (HwDeviceExtension->jChipType >= XG27)
1132                 xgifb_reg_and_or(pVBInfo->P3c4, 0x40, 0xFC, data2 & 0x03);
1133
1134 }
1135
1136 static void XGI_SetCRT1ModeRegs(struct xgi_hw_device_info *HwDeviceExtension,
1137                 unsigned short ModeNo, unsigned short ModeIdIndex,
1138                 unsigned short RefreshRateTableIndex,
1139                 struct vb_device_info *pVBInfo)
1140 {
1141         unsigned short data, data2, data3, infoflag = 0, modeflag, resindex,
1142                         xres;
1143
1144         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
1145         infoflag = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
1146
1147         if (xgifb_reg_get(pVBInfo->P3d4, 0x31) & 0x01)
1148                 xgifb_reg_and_or(pVBInfo->P3c4, 0x1F, 0x3F, 0x00);
1149
1150         data = infoflag;
1151         data2 = 0;
1152         data2 |= 0x02;
1153         data3 = pVBInfo->ModeType - ModeVGA;
1154         data3 = data3 << 2;
1155         data2 |= data3;
1156         data &= InterlaceMode;
1157
1158         if (data)
1159                 data2 |= 0x20;
1160
1161         xgifb_reg_and_or(pVBInfo->P3c4, 0x06, ~0x3F, data2);
1162         resindex = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
1163         xres = pVBInfo->ModeResInfo[resindex].HTotal; /* xres->ax */
1164
1165         data = 0x0000;
1166         if (infoflag & InterlaceMode) {
1167                 if (xres == 1024)
1168                         data = 0x0035;
1169                 else if (xres == 1280)
1170                         data = 0x0048;
1171         }
1172
1173         data2 = data & 0x00FF;
1174         xgifb_reg_and_or(pVBInfo->P3d4, 0x19, 0xFF, data2);
1175         data2 = (data & 0xFF00) >> 8;
1176         xgifb_reg_and_or(pVBInfo->P3d4, 0x19, 0xFC, data2);
1177
1178         if (modeflag & HalfDCLK)
1179                 xgifb_reg_and_or(pVBInfo->P3c4, 0x01, 0xF7, 0x08);
1180
1181         data2 = 0;
1182
1183         if (modeflag & LineCompareOff)
1184                 data2 |= 0x08;
1185
1186         xgifb_reg_and_or(pVBInfo->P3c4, 0x0F, ~0x48, data2);
1187         data = 0x60;
1188         data = data ^ 0x60;
1189         data = data ^ 0xA0;
1190         xgifb_reg_and_or(pVBInfo->P3c4, 0x21, 0x1F, data);
1191
1192         XGI_SetVCLKState(HwDeviceExtension, ModeNo, RefreshRateTableIndex,
1193                         pVBInfo);
1194
1195         data = xgifb_reg_get(pVBInfo->P3d4, 0x31);
1196
1197         if (HwDeviceExtension->jChipType == XG27) {
1198                 if (data & 0x40)
1199                         data = 0x2c;
1200                 else
1201                         data = 0x6c;
1202                 xgifb_reg_set(pVBInfo->P3d4, 0x52, data);
1203                 xgifb_reg_or(pVBInfo->P3d4, 0x51, 0x10);
1204         } else if (HwDeviceExtension->jChipType >= XG20) {
1205                 if (data & 0x40)
1206                         data = 0x33;
1207                 else
1208                         data = 0x73;
1209                 xgifb_reg_set(pVBInfo->P3d4, 0x52, data);
1210                 xgifb_reg_set(pVBInfo->P3d4, 0x51, 0x02);
1211         } else {
1212                 if (data & 0x40)
1213                         data = 0x2c;
1214                 else
1215                         data = 0x6c;
1216                 xgifb_reg_set(pVBInfo->P3d4, 0x52, data);
1217         }
1218
1219 }
1220
1221 static void XGI_WriteDAC(unsigned short dl,
1222                          unsigned short ah,
1223                          unsigned short al,
1224                          unsigned short dh,
1225                          struct vb_device_info *pVBInfo)
1226 {
1227         unsigned short temp, bh, bl;
1228
1229         bh = ah;
1230         bl = al;
1231
1232         if (dl != 0) {
1233                 temp = bh;
1234                 bh = dh;
1235                 dh = temp;
1236                 if (dl == 1) {
1237                         temp = bl;
1238                         bl = dh;
1239                         dh = temp;
1240                 } else {
1241                         temp = bl;
1242                         bl = bh;
1243                         bh = temp;
1244                 }
1245         }
1246         outb((unsigned short) dh, pVBInfo->P3c9);
1247         outb((unsigned short) bh, pVBInfo->P3c9);
1248         outb((unsigned short) bl, pVBInfo->P3c9);
1249 }
1250
1251 static void XGI_LoadDAC(unsigned short ModeNo, unsigned short ModeIdIndex,
1252                 struct vb_device_info *pVBInfo)
1253 {
1254         unsigned short data, data2, i, k, m, n, o, si, di, bx, dl, al, ah, dh;
1255         const unsigned short *table = XGINew_VGA_DAC;
1256
1257         outb(0xFF, pVBInfo->P3c6);
1258         outb(0x00, pVBInfo->P3c8);
1259
1260         for (i = 0; i < 16; i++) {
1261                 data = table[i];
1262
1263                 for (k = 0; k < 3; k++) {
1264                         data2 = 0;
1265
1266                         if (data & 0x01)
1267                                 data2 = 0x2A;
1268
1269                         if (data & 0x02)
1270                                 data2 += 0x15;
1271
1272                         outb(data2, pVBInfo->P3c9);
1273                         data = data >> 2;
1274                 }
1275         }
1276
1277         for (i = 16; i < 32; i++) {
1278                 data = table[i];
1279
1280                 for (k = 0; k < 3; k++)
1281                         outb(data, pVBInfo->P3c9);
1282         }
1283
1284         si = 32;
1285
1286         for (m = 0; m < 9; m++) {
1287                 di = si;
1288                 bx = si + 0x04;
1289                 dl = 0;
1290
1291                 for (n = 0; n < 3; n++) {
1292                         for (o = 0; o < 5; o++) {
1293                                 dh = table[si];
1294                                 ah = table[di];
1295                                 al = table[bx];
1296                                 si++;
1297                                 XGI_WriteDAC(dl, ah, al, dh, pVBInfo);
1298                         }
1299
1300                         si -= 2;
1301
1302                         for (o = 0; o < 3; o++) {
1303                                 dh = table[bx];
1304                                 ah = table[di];
1305                                 al = table[si];
1306                                 si--;
1307                                 XGI_WriteDAC(dl, ah, al, dh, pVBInfo);
1308                         }
1309
1310                         dl++;
1311                 }
1312
1313                 si += 5;
1314         }
1315 }
1316
1317 static void XGI_GetLVDSResInfo(unsigned short ModeNo,
1318                                unsigned short ModeIdIndex,
1319                                struct vb_device_info *pVBInfo)
1320 {
1321         unsigned short resindex, xres, yres, modeflag;
1322
1323         /* si+Ext_ResInfo */
1324         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
1325
1326         /* si+Ext_ResInfo */
1327         resindex = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
1328
1329         xres = pVBInfo->ModeResInfo[resindex].HTotal;
1330         yres = pVBInfo->ModeResInfo[resindex].VTotal;
1331
1332         if (modeflag & HalfDCLK)
1333                 xres = xres << 1;
1334
1335         if (modeflag & DoubleScanMode)
1336                 yres = yres << 1;
1337
1338         if (xres == 720)
1339                 xres = 640;
1340
1341         pVBInfo->VGAHDE = xres;
1342         pVBInfo->HDE = xres;
1343         pVBInfo->VGAVDE = yres;
1344         pVBInfo->VDE = yres;
1345 }
1346
1347 static void *XGI_GetLcdPtr(unsigned short BX, unsigned short ModeNo,
1348                 unsigned short ModeIdIndex,
1349                 unsigned short RefreshRateTableIndex,
1350                 struct vb_device_info *pVBInfo)
1351 {
1352         unsigned short i, tempdx, tempcx, tempbx, tempal, modeflag, table;
1353
1354         struct XGI330_LCDDataTablStruct *tempdi = NULL;
1355
1356         tempbx = BX;
1357
1358         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
1359         tempal = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
1360
1361         tempal = tempal & 0x0f;
1362
1363         if (tempbx <= 1) { /* ExpLink */
1364                 tempal = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
1365
1366                 if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA) {
1367                         tempal = pVBInfo->RefIndex[RefreshRateTableIndex].
1368                                                         Ext_CRT2CRTC2;
1369                 }
1370
1371                 if (tempbx & 0x01)
1372                         tempal = (tempal >> 4);
1373
1374                 tempal = (tempal & 0x0f);
1375         }
1376
1377         tempcx = LCDLenList[tempbx];
1378
1379         if (pVBInfo->LCDInfo & EnableScalingLCD) { /* ScaleLCD */
1380                 if ((tempbx == 5) || (tempbx) == 7)
1381                         tempcx = LCDDesDataLen2;
1382                 else if ((tempbx == 3) || (tempbx == 8))
1383                         tempcx = LVDSDesDataLen2;
1384         }
1385
1386         switch (tempbx) {
1387         case 0:
1388         case 1:
1389                 tempdi = xgifb_epllcd_crt1;
1390                 break;
1391         case 2:
1392                 tempdi = XGI_EPLLCDDataPtr;
1393                 break;
1394         case 3:
1395                 tempdi = XGI_EPLLCDDesDataPtr;
1396                 break;
1397         case 4:
1398                 tempdi = XGI_LCDDataTable;
1399                 break;
1400         case 5:
1401                 tempdi = XGI_LCDDesDataTable;
1402                 break;
1403         case 6:
1404                 tempdi = XGI_EPLCHLCDRegPtr;
1405                 break;
1406         case 7:
1407         case 8:
1408         case 9:
1409                 tempdi = NULL;
1410                 break;
1411         default:
1412                 break;
1413         }
1414
1415         if (tempdi == NULL) /* OEMUtil */
1416                 return NULL;
1417
1418         table = tempbx;
1419         i = 0;
1420
1421         while (tempdi[i].PANELID != 0xff) {
1422                 tempdx = pVBInfo->LCDResInfo;
1423                 if (tempbx & 0x0080) { /* OEMUtil */
1424                         tempbx &= (~0x0080);
1425                         tempdx = pVBInfo->LCDTypeInfo;
1426                 }
1427
1428                 if (pVBInfo->LCDInfo & EnableScalingLCD)
1429                         tempdx &= (~PanelResInfo);
1430
1431                 if (tempdi[i].PANELID == tempdx) {
1432                         tempbx = tempdi[i].MASK;
1433                         tempdx = pVBInfo->LCDInfo;
1434
1435                         if (modeflag & HalfDCLK)
1436                                 tempdx |= SetLCDLowResolution;
1437
1438                         tempbx &= tempdx;
1439                         if (tempbx == tempdi[i].CAP)
1440                                 break;
1441                 }
1442                 i++;
1443         }
1444
1445         if (table == 0) {
1446                 switch (tempdi[i].DATAPTR) {
1447                 case 0:
1448                         return &XGI_LVDSCRT11024x768_1_H[tempal];
1449                         break;
1450                 case 1:
1451                         return &XGI_LVDSCRT11024x768_2_H[tempal];
1452                         break;
1453                 case 2:
1454                         return &XGI_LVDSCRT11280x1024_1_H[tempal];
1455                         break;
1456                 case 3:
1457                         return &XGI_LVDSCRT11280x1024_2_H[tempal];
1458                         break;
1459                 case 4:
1460                         return &XGI_LVDSCRT11400x1050_1_H[tempal];
1461                         break;
1462                 case 5:
1463                         return &XGI_LVDSCRT11400x1050_2_H[tempal];
1464                         break;
1465                 case 6:
1466                         return &XGI_LVDSCRT11600x1200_1_H[tempal];
1467                         break;
1468                 case 7:
1469                         return &XGI_LVDSCRT11024x768_1_Hx75[tempal];
1470                         break;
1471                 case 8:
1472                         return &XGI_LVDSCRT11024x768_2_Hx75[tempal];
1473                         break;
1474                 case 9:
1475                         return &XGI_LVDSCRT11280x1024_1_Hx75[tempal];
1476                         break;
1477                 case 10:
1478                         return &XGI_LVDSCRT11280x1024_2_Hx75[tempal];
1479                         break;
1480                 default:
1481                         break;
1482                 }
1483         } else if (table == 1) {
1484                 switch (tempdi[i].DATAPTR) {
1485                 case 0:
1486                         return &XGI_LVDSCRT11024x768_1_V[tempal];
1487                         break;
1488                 case 1:
1489                         return &XGI_LVDSCRT11024x768_2_V[tempal];
1490                         break;
1491                 case 2:
1492                         return &XGI_LVDSCRT11280x1024_1_V[tempal];
1493                         break;
1494                 case 3:
1495                         return &XGI_LVDSCRT11280x1024_2_V[tempal];
1496                         break;
1497                 case 4:
1498                         return &XGI_LVDSCRT11400x1050_1_V[tempal];
1499                         break;
1500                 case 5:
1501                         return &XGI_LVDSCRT11400x1050_2_V[tempal];
1502                         break;
1503                 case 6:
1504                         return &XGI_LVDSCRT11600x1200_1_V[tempal];
1505                         break;
1506                 case 7:
1507                         return &XGI_LVDSCRT11024x768_1_Vx75[tempal];
1508                         break;
1509                 case 8:
1510                         return &XGI_LVDSCRT11024x768_2_Vx75[tempal];
1511                         break;
1512                 case 9:
1513                         return &XGI_LVDSCRT11280x1024_1_Vx75[tempal];
1514                         break;
1515                 case 10:
1516                         return &XGI_LVDSCRT11280x1024_2_Vx75[tempal];
1517                         break;
1518                 default:
1519                         break;
1520                 }
1521         } else if (table == 2) {
1522                 switch (tempdi[i].DATAPTR) {
1523                 case 0:
1524                         return &XGI_LVDS1024x768Data_1[tempal];
1525                         break;
1526                 case 1:
1527                         return &XGI_LVDS1024x768Data_2[tempal];
1528                         break;
1529                 case 2:
1530                         return &XGI_LVDS1280x1024Data_1[tempal];
1531                         break;
1532                 case 3:
1533                         return &XGI_LVDS1280x1024Data_2[tempal];
1534                         break;
1535                 case 4:
1536                         return &XGI_LVDS1400x1050Data_1[tempal];
1537                         break;
1538                 case 5:
1539                         return &XGI_LVDS1400x1050Data_2[tempal];
1540                         break;
1541                 case 6:
1542                         return &XGI_LVDS1600x1200Data_1[tempal];
1543                         break;
1544                 case 7:
1545                         return &XGI_LVDSNoScalingData[tempal];
1546                         break;
1547                 case 8:
1548                         return &XGI_LVDS1024x768Data_1x75[tempal];
1549                         break;
1550                 case 9:
1551                         return &XGI_LVDS1024x768Data_2x75[tempal];
1552                         break;
1553                 case 10:
1554                         return &XGI_LVDS1280x1024Data_1x75[tempal];
1555                         break;
1556                 case 11:
1557                         return &XGI_LVDS1280x1024Data_2x75[tempal];
1558                         break;
1559                 case 12:
1560                         return &XGI_LVDSNoScalingDatax75[tempal];
1561                         break;
1562                 default:
1563                         break;
1564                 }
1565         } else if (table == 3) {
1566                 switch (tempdi[i].DATAPTR) {
1567                 case 0:
1568                         return &XGI_LVDS1024x768Des_1[tempal];
1569                         break;
1570                 case 1:
1571                         return &XGI_LVDS1024x768Des_3[tempal];
1572                         break;
1573                 case 2:
1574                         return &XGI_LVDS1024x768Des_2[tempal];
1575                         break;
1576                 case 3:
1577                         return &XGI_LVDS1280x1024Des_1[tempal];
1578                         break;
1579                 case 4:
1580                         return &XGI_LVDS1280x1024Des_2[tempal];
1581                         break;
1582                 case 5:
1583                         return &XGI_LVDS1400x1050Des_1[tempal];
1584                         break;
1585                 case 6:
1586                         return &XGI_LVDS1400x1050Des_2[tempal];
1587                         break;
1588                 case 7:
1589                         return &XGI_LVDS1600x1200Des_1[tempal];
1590                         break;
1591                 case 8:
1592                         return &XGI_LVDSNoScalingDesData[tempal];
1593                         break;
1594                 case 9:
1595                         return &XGI_LVDS1024x768Des_1x75[tempal];
1596                         break;
1597                 case 10:
1598                         return &XGI_LVDS1024x768Des_3x75[tempal];
1599                         break;
1600                 case 11:
1601                         return &XGI_LVDS1024x768Des_2x75[tempal];
1602                         break;
1603                 case 12:
1604                         return &XGI_LVDS1280x1024Des_1x75[tempal];
1605                         break;
1606                 case 13:
1607                         return &XGI_LVDS1280x1024Des_2x75[tempal];
1608                         break;
1609                 case 14:
1610                         return &XGI_LVDSNoScalingDesDatax75[tempal];
1611                         break;
1612                 default:
1613                         break;
1614                 }
1615         } else if (table == 4) {
1616                 switch (tempdi[i].DATAPTR) {
1617                 case 0:
1618                         return &XGI_ExtLCD1024x768Data[tempal];
1619                         break;
1620                 case 1:
1621                         return &XGI_StLCD1024x768Data[tempal];
1622                         break;
1623                 case 2:
1624                         return &XGI_CetLCD1024x768Data[tempal];
1625                         break;
1626                 case 3:
1627                         return &XGI_ExtLCD1280x1024Data[tempal];
1628                         break;
1629                 case 4:
1630                         return &XGI_StLCD1280x1024Data[tempal];
1631                         break;
1632                 case 5:
1633                         return &XGI_CetLCD1280x1024Data[tempal];
1634                         break;
1635                 case 6:
1636                 case 7:
1637                         return &xgifb_lcd_1400x1050[tempal];
1638                         break;
1639                 case 8:
1640                         return &XGI_CetLCD1400x1050Data[tempal];
1641                         break;
1642                 case 9:
1643                         return &XGI_ExtLCD1600x1200Data[tempal];
1644                         break;
1645                 case 10:
1646                         return &XGI_StLCD1600x1200Data[tempal];
1647                         break;
1648                 case 11:
1649                         return &XGI_NoScalingData[tempal];
1650                         break;
1651                 case 12:
1652                         return &XGI_ExtLCD1024x768x75Data[tempal];
1653                         break;
1654                 case 13:
1655                         return &XGI_ExtLCD1024x768x75Data[tempal];
1656                         break;
1657                 case 14:
1658                         return &XGI_CetLCD1024x768x75Data[tempal];
1659                         break;
1660                 case 15:
1661                 case 16:
1662                         return &xgifb_lcd_1280x1024x75[tempal];
1663                         break;
1664                 case 17:
1665                         return &XGI_CetLCD1280x1024x75Data[tempal];
1666                         break;
1667                 case 18:
1668                         return &XGI_NoScalingDatax75[tempal];
1669                         break;
1670                 default:
1671                         break;
1672                 }
1673         } else if (table == 5) {
1674                 switch (tempdi[i].DATAPTR) {
1675                 case 0:
1676                         return &XGI_ExtLCDDes1024x768Data[tempal];
1677                         break;
1678                 case 1:
1679                         return &XGI_StLCDDes1024x768Data[tempal];
1680                         break;
1681                 case 2:
1682                         return &XGI_CetLCDDes1024x768Data[tempal];
1683                         break;
1684                 case 3:
1685                         if ((pVBInfo->VBType & VB_SIS301LV) ||
1686                                 (pVBInfo->VBType & VB_SIS302LV))
1687                                 return &XGI_ExtLCDDLDes1280x1024Data[tempal];
1688                         else
1689                                 return &XGI_ExtLCDDes1280x1024Data[tempal];
1690                         break;
1691                 case 4:
1692                         if ((pVBInfo->VBType & VB_SIS301LV) ||
1693                             (pVBInfo->VBType & VB_SIS302LV))
1694                                 return &XGI_StLCDDLDes1280x1024Data[tempal];
1695                         else
1696                                 return &XGI_StLCDDes1280x1024Data[tempal];
1697                         break;
1698                 case 5:
1699                         if ((pVBInfo->VBType & VB_SIS301LV) ||
1700                             (pVBInfo->VBType & VB_SIS302LV))
1701                                 return &XGI_CetLCDDLDes1280x1024Data[tempal];
1702                         else
1703                                 return &XGI_CetLCDDes1280x1024Data[tempal];
1704                         break;
1705                 case 6:
1706                 case 7:
1707                         if ((pVBInfo->VBType & VB_SIS301LV) ||
1708                             (pVBInfo->VBType & VB_SIS302LV))
1709                                 return &xgifb_lcddldes_1400x1050[tempal];
1710                         else
1711                                 return &xgifb_lcddes_1400x1050[tempal];
1712                         break;
1713                 case 8:
1714                         return &XGI_CetLCDDes1400x1050Data[tempal];
1715                         break;
1716                 case 9:
1717                         return &XGI_CetLCDDes1400x1050Data2[tempal];
1718                         break;
1719                 case 10:
1720                         if ((pVBInfo->VBType & VB_SIS301LV) ||
1721                             (pVBInfo->VBType & VB_SIS302LV))
1722                                 return &XGI_ExtLCDDLDes1600x1200Data[tempal];
1723                         else
1724                                 return &XGI_ExtLCDDes1600x1200Data[tempal];
1725                         break;
1726                 case 11:
1727                         if ((pVBInfo->VBType & VB_SIS301LV) ||
1728                             (pVBInfo->VBType & VB_SIS302LV))
1729                                 return &XGI_StLCDDLDes1600x1200Data[tempal];
1730                         else
1731                                 return &XGI_StLCDDes1600x1200Data[tempal];
1732                         break;
1733                 case 12:
1734                         return &XGI_NoScalingDesData[tempal];
1735                         break;
1736                 case 13:
1737                 case 14:
1738                         return &xgifb_lcddes_1024x768x75[tempal];
1739                         break;
1740                 case 15:
1741                         return &XGI_CetLCDDes1024x768x75Data[tempal];
1742                         break;
1743                 case 16:
1744                 case 17:
1745                         if ((pVBInfo->VBType & VB_SIS301LV) ||
1746                             (pVBInfo->VBType & VB_SIS302LV))
1747                                 return &xgifb_lcddldes_1280x1024x75[tempal];
1748                         else
1749                                 return &xgifb_lcddes_1280x1024x75[tempal];
1750                         break;
1751                 case 18:
1752                         if ((pVBInfo->VBType & VB_SIS301LV) ||
1753                             (pVBInfo->VBType & VB_SIS302LV))
1754                                 return &XGI_CetLCDDLDes1280x1024x75Data[tempal];
1755                         else
1756                                 return &XGI_CetLCDDes1280x1024x75Data[tempal];
1757                         break;
1758                 case 19:
1759                         return &XGI_NoScalingDesDatax75[tempal];
1760                         break;
1761                 default:
1762                         break;
1763                 }
1764         } else if (table == 6) {
1765                 switch (tempdi[i].DATAPTR) {
1766                 case 0:
1767                         return &XGI_CH7017LV1024x768[tempal];
1768                         break;
1769                 case 1:
1770                         return &XGI_CH7017LV1400x1050[tempal];
1771                         break;
1772                 default:
1773                         break;
1774                 }
1775         }
1776         return NULL;
1777 }
1778
1779 static void *XGI_GetTVPtr(unsigned short BX, unsigned short ModeNo,
1780                 unsigned short ModeIdIndex,
1781                 unsigned short RefreshRateTableIndex,
1782                 struct vb_device_info *pVBInfo)
1783 {
1784         unsigned short i, tempdx, tempbx, tempal, modeflag, table;
1785         struct XGI330_TVDataTablStruct *tempdi = NULL;
1786
1787         tempbx = BX;
1788         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
1789         tempal = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
1790         tempal = tempal & 0x3f;
1791         table = tempbx;
1792
1793         switch (tempbx) {
1794         case 0:
1795                 tempdi = NULL;
1796                 break;
1797         case 1:
1798                 tempdi = NULL;
1799                 break;
1800         case 2:
1801         case 6:
1802                 tempdi = xgifb_chrontel_tv;
1803                 break;
1804         case 3:
1805                 tempdi = NULL;
1806                 break;
1807         case 4:
1808                 tempdi = XGI_TVDataTable;
1809                 break;
1810         case 5:
1811                 tempdi = NULL;
1812                 break;
1813         default:
1814                 break;
1815         }
1816
1817         if (tempdi == NULL) /* OEMUtil */
1818                 return NULL;
1819
1820         tempdx = pVBInfo->TVInfo;
1821
1822         if (pVBInfo->VBInfo & SetInSlaveMode)
1823                 tempdx = tempdx | SetTVLockMode;
1824
1825         if (modeflag & HalfDCLK)
1826                 tempdx = tempdx | SetTVLowResolution;
1827
1828         i = 0;
1829
1830         while (tempdi[i].MASK != 0xffff) {
1831                 if ((tempdx & tempdi[i].MASK) == tempdi[i].CAP)
1832                         break;
1833                 i++;
1834         }
1835
1836         if (table == 0x04) {
1837                 switch (tempdi[i].DATAPTR) {
1838                 case 0:
1839                         return &XGI_ExtPALData[tempal];
1840                         break;
1841                 case 1:
1842                         return &XGI_ExtNTSCData[tempal];
1843                         break;
1844                 case 2:
1845                         return &XGI_StPALData[tempal];
1846                         break;
1847                 case 3:
1848                         return &XGI_StNTSCData[tempal];
1849                         break;
1850                 case 4:
1851                         return &XGI_ExtHiTVData[tempal];
1852                         break;
1853                 case 5:
1854                         return &XGI_St2HiTVData[tempal];
1855                         break;
1856                 case 6:
1857                         return &XGI_ExtYPbPr525iData[tempal];
1858                         break;
1859                 case 7:
1860                         return &XGI_ExtYPbPr525pData[tempal];
1861                         break;
1862                 case 8:
1863                         return &XGI_ExtYPbPr750pData[tempal];
1864                         break;
1865                 case 9:
1866                         return &XGI_StYPbPr525iData[tempal];
1867                         break;
1868                 case 10:
1869                         return &XGI_StYPbPr525pData[tempal];
1870                         break;
1871                 case 11:
1872                         return &XGI_StYPbPr750pData[tempal];
1873                         break;
1874                 case 12: /* avoid system hang */
1875                         return &XGI_ExtNTSCData[tempal];
1876                         break;
1877                 case 13:
1878                         return &XGI_St1HiTVData[tempal];
1879                         break;
1880                 default:
1881                         break;
1882                 }
1883         } else if (table == 0x02) {
1884                 switch (tempdi[i].DATAPTR) {
1885                 case 0:
1886                         return &XGI_CHTVUNTSCData[tempal];
1887                         break;
1888                 case 1:
1889                         return &XGI_CHTVONTSCData[tempal];
1890                         break;
1891                 case 2:
1892                         return &XGI_CHTVUPALData[tempal];
1893                         break;
1894                 case 3:
1895                         return &XGI_CHTVOPALData[tempal];
1896                         break;
1897                 default:
1898                         break;
1899                 }
1900         }
1901         return NULL;
1902 }
1903
1904 static void XGI_GetLVDSData(unsigned short ModeNo, unsigned short ModeIdIndex,
1905                 unsigned short RefreshRateTableIndex,
1906                 struct vb_device_info *pVBInfo)
1907 {
1908         unsigned short tempbx;
1909         struct SiS_LVDSData *LCDPtr = NULL;
1910
1911         tempbx = 2;
1912
1913         if (pVBInfo->VBInfo & (SetCRT2ToLCD | XGI_SetCRT2ToLCDA)) {
1914                 LCDPtr = (struct SiS_LVDSData *)XGI_GetLcdPtr(tempbx,
1915                                 ModeNo, ModeIdIndex, RefreshRateTableIndex,
1916                                 pVBInfo);
1917                 pVBInfo->VGAHT = LCDPtr->VGAHT;
1918                 pVBInfo->VGAVT = LCDPtr->VGAVT;
1919                 pVBInfo->HT = LCDPtr->LCDHT;
1920                 pVBInfo->VT = LCDPtr->LCDVT;
1921         }
1922
1923         if (pVBInfo->VBInfo & (SetCRT2ToLCD | XGI_SetCRT2ToLCDA)) {
1924                 if (!(pVBInfo->LCDInfo & (SetLCDtoNonExpanding
1925                                 | EnableScalingLCD))) {
1926                         if ((pVBInfo->LCDResInfo == Panel_1024x768) ||
1927                             (pVBInfo->LCDResInfo == Panel_1024x768x75)) {
1928                                 pVBInfo->HDE = 1024;
1929                                 pVBInfo->VDE = 768;
1930                         } else if ((pVBInfo->LCDResInfo == Panel_1280x1024) ||
1931                                    (pVBInfo->LCDResInfo == Panel_1280x1024x75)) {
1932                                 pVBInfo->HDE = 1280;
1933                                 pVBInfo->VDE = 1024;
1934                         } else if (pVBInfo->LCDResInfo == Panel_1400x1050) {
1935                                 pVBInfo->HDE = 1400;
1936                                 pVBInfo->VDE = 1050;
1937                         } else {
1938                                 pVBInfo->HDE = 1600;
1939                                 pVBInfo->VDE = 1200;
1940                         }
1941                 }
1942         }
1943 }
1944
1945 static void XGI_ModCRT1Regs(unsigned short ModeNo, unsigned short ModeIdIndex,
1946                 unsigned short RefreshRateTableIndex,
1947                 struct xgi_hw_device_info *HwDeviceExtension,
1948                 struct vb_device_info *pVBInfo)
1949 {
1950         unsigned char index;
1951         unsigned short tempbx, i;
1952         struct XGI_LVDSCRT1HDataStruct *LCDPtr = NULL;
1953         struct XGI_LVDSCRT1VDataStruct *LCDPtr1 = NULL;
1954
1955         index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
1956         index = index & IndexMask;
1957
1958         tempbx = 0;
1959
1960         if (pVBInfo->VBInfo & (SetCRT2ToLCD | XGI_SetCRT2ToLCDA)) {
1961                 LCDPtr = (struct XGI_LVDSCRT1HDataStruct *)
1962                                 XGI_GetLcdPtr(tempbx, ModeNo,
1963                                               ModeIdIndex,
1964                                               RefreshRateTableIndex,
1965                                               pVBInfo);
1966
1967                 for (i = 0; i < 8; i++)
1968                         pVBInfo->TimingH[0].data[i] = LCDPtr[0].Reg[i];
1969         }
1970
1971         XGI_SetCRT1Timing_H(pVBInfo, HwDeviceExtension);
1972
1973         tempbx = 1;
1974
1975         if (pVBInfo->VBInfo & (SetCRT2ToLCD | XGI_SetCRT2ToLCDA)) {
1976                 LCDPtr1 = (struct XGI_LVDSCRT1VDataStruct *)
1977                                 XGI_GetLcdPtr(
1978                                         tempbx,
1979                                         ModeNo,
1980                                         ModeIdIndex,
1981                                         RefreshRateTableIndex,
1982                                         pVBInfo);
1983                 for (i = 0; i < 7; i++)
1984                         pVBInfo->TimingV[0].data[i] = LCDPtr1[0].Reg[i];
1985         }
1986
1987         XGI_SetCRT1Timing_V(ModeIdIndex, ModeNo, pVBInfo);
1988 }
1989
1990 static unsigned short XGI_GetLCDCapPtr(struct vb_device_info *pVBInfo)
1991 {
1992         unsigned char tempal, tempah, tempbl, i;
1993
1994         tempah = xgifb_reg_get(pVBInfo->P3d4, 0x36);
1995         tempal = tempah & 0x0F;
1996         tempah = tempah & 0xF0;
1997         i = 0;
1998         tempbl = pVBInfo->LCDCapList[i].LCD_ID;
1999
2000         while (tempbl != 0xFF) {
2001                 if (tempbl & 0x80) { /* OEMUtil */
2002                         tempal = tempah;
2003                         tempbl = tempbl & ~(0x80);
2004                 }
2005
2006                 if (tempal == tempbl)
2007                         break;
2008
2009                 i++;
2010
2011                 tempbl = pVBInfo->LCDCapList[i].LCD_ID;
2012         }
2013
2014         return i;
2015 }
2016
2017 static unsigned short XGI_GetLCDCapPtr1(struct vb_device_info *pVBInfo)
2018 {
2019         unsigned short tempah, tempal, tempbl, i;
2020
2021         tempal = pVBInfo->LCDResInfo;
2022         tempah = pVBInfo->LCDTypeInfo;
2023
2024         i = 0;
2025         tempbl = pVBInfo->LCDCapList[i].LCD_ID;
2026
2027         while (tempbl != 0xFF) {
2028                 if ((tempbl & 0x80) && (tempbl != 0x80)) {
2029                         tempal = tempah;
2030                         tempbl &= ~0x80;
2031                 }
2032
2033                 if (tempal == tempbl)
2034                         break;
2035
2036                 i++;
2037                 tempbl = pVBInfo->LCDCapList[i].LCD_ID;
2038         }
2039
2040         if (tempbl == 0xFF) {
2041                 pVBInfo->LCDResInfo = Panel_1024x768;
2042                 pVBInfo->LCDTypeInfo = 0;
2043                 i = 0;
2044         }
2045
2046         return i;
2047 }
2048
2049 static void XGI_GetLCDSync(unsigned short *HSyncWidth,
2050                            unsigned short *VSyncWidth,
2051                            struct vb_device_info *pVBInfo)
2052 {
2053         unsigned short Index;
2054
2055         Index = XGI_GetLCDCapPtr(pVBInfo);
2056         *HSyncWidth = pVBInfo->LCDCapList[Index].LCD_HSyncWidth;
2057         *VSyncWidth = pVBInfo->LCDCapList[Index].LCD_VSyncWidth;
2058
2059         return;
2060 }
2061
2062 static void XGI_SetLVDSRegs(unsigned short ModeNo, unsigned short ModeIdIndex,
2063                 unsigned short RefreshRateTableIndex,
2064                 struct vb_device_info *pVBInfo)
2065 {
2066         unsigned short tempbx, tempax, tempcx, tempdx, push1, push2, modeflag;
2067         unsigned long temp, temp1, temp2, temp3, push3;
2068         struct XGI_LCDDesStruct *LCDPtr = NULL;
2069         struct XGI330_LCDDataDesStruct2 *LCDPtr1 = NULL;
2070
2071         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
2072         tempbx = 3;
2073         if (pVBInfo->LCDInfo & EnableScalingLCD)
2074                 LCDPtr1 =
2075                     (struct XGI330_LCDDataDesStruct2 *)
2076                                 XGI_GetLcdPtr(
2077                                           tempbx,
2078                                           ModeNo,
2079                                           ModeIdIndex,
2080                                           RefreshRateTableIndex,
2081                                           pVBInfo);
2082         else
2083                 LCDPtr =
2084                     (struct XGI_LCDDesStruct *)
2085                                 XGI_GetLcdPtr(
2086                                           tempbx,
2087                                           ModeNo,
2088                                           ModeIdIndex,
2089                                           RefreshRateTableIndex,
2090                                           pVBInfo);
2091
2092         XGI_GetLCDSync(&tempax, &tempbx, pVBInfo);
2093         push1 = tempbx;
2094         push2 = tempax;
2095
2096         /* GetLCDResInfo */
2097         if ((pVBInfo->LCDResInfo == Panel_1024x768) ||
2098             (pVBInfo->LCDResInfo == Panel_1024x768x75)) {
2099                 tempax = 1024;
2100                 tempbx = 768;
2101         } else if ((pVBInfo->LCDResInfo == Panel_1280x1024) ||
2102                    (pVBInfo->LCDResInfo == Panel_1280x1024x75)) {
2103                 tempax = 1280;
2104                 tempbx = 1024;
2105         } else if (pVBInfo->LCDResInfo == Panel_1400x1050) {
2106                 tempax = 1400;
2107                 tempbx = 1050;
2108         } else {
2109                 tempax = 1600;
2110                 tempbx = 1200;
2111         }
2112
2113         if (pVBInfo->LCDInfo & SetLCDtoNonExpanding) {
2114                 pVBInfo->HDE = tempax;
2115                 pVBInfo->VDE = tempbx;
2116                 pVBInfo->VGAHDE = tempax;
2117                 pVBInfo->VGAVDE = tempbx;
2118         }
2119
2120         tempax = pVBInfo->HT;
2121
2122         if (pVBInfo->LCDInfo & EnableScalingLCD)
2123                 tempbx = LCDPtr1->LCDHDES;
2124         else
2125                 tempbx = LCDPtr->LCDHDES;
2126
2127         tempcx = pVBInfo->HDE;
2128         tempbx = tempbx & 0x0fff;
2129         tempcx += tempbx;
2130
2131         if (tempcx >= tempax)
2132                 tempcx -= tempax;
2133
2134         xgifb_reg_set(pVBInfo->Part1Port, 0x1A, tempbx & 0x07);
2135
2136         tempcx = tempcx >> 3;
2137         tempbx = tempbx >> 3;
2138
2139         xgifb_reg_set(pVBInfo->Part1Port, 0x16,
2140                         (unsigned short) (tempbx & 0xff));
2141         xgifb_reg_set(pVBInfo->Part1Port, 0x17,
2142                         (unsigned short) (tempcx & 0xff));
2143
2144         tempax = pVBInfo->HT;
2145
2146         if (pVBInfo->LCDInfo & EnableScalingLCD)
2147                 tempbx = LCDPtr1->LCDHRS;
2148         else
2149                 tempbx = LCDPtr->LCDHRS;
2150
2151         tempcx = push2;
2152
2153         if (pVBInfo->LCDInfo & EnableScalingLCD)
2154                 tempcx = LCDPtr1->LCDHSync;
2155
2156         tempcx += tempbx;
2157
2158         if (tempcx >= tempax)
2159                 tempcx -= tempax;
2160
2161         tempax = tempbx & 0x07;
2162         tempax = tempax >> 5;
2163         tempcx = tempcx >> 3;
2164         tempbx = tempbx >> 3;
2165
2166         tempcx &= 0x1f;
2167         tempax |= tempcx;
2168
2169         xgifb_reg_set(pVBInfo->Part1Port, 0x15, tempax);
2170         xgifb_reg_set(pVBInfo->Part1Port, 0x14,
2171                         (unsigned short) (tempbx & 0xff));
2172
2173         tempax = pVBInfo->VT;
2174         if (pVBInfo->LCDInfo & EnableScalingLCD)
2175                 tempbx = LCDPtr1->LCDVDES;
2176         else
2177                 tempbx = LCDPtr->LCDVDES;
2178         tempcx = pVBInfo->VDE;
2179
2180         tempbx = tempbx & 0x0fff;
2181         tempcx += tempbx;
2182         if (tempcx >= tempax)
2183                 tempcx -= tempax;
2184
2185         xgifb_reg_set(pVBInfo->Part1Port, 0x1b,
2186                         (unsigned short) (tempbx & 0xff));
2187         xgifb_reg_set(pVBInfo->Part1Port, 0x1c,
2188                         (unsigned short) (tempcx & 0xff));
2189
2190         tempbx = (tempbx >> 8) & 0x07;
2191         tempcx = (tempcx >> 8) & 0x07;
2192
2193         xgifb_reg_set(pVBInfo->Part1Port, 0x1d,
2194                         (unsigned short) ((tempcx << 3)
2195                                         | tempbx));
2196
2197         tempax = pVBInfo->VT;
2198         if (pVBInfo->LCDInfo & EnableScalingLCD)
2199                 tempbx = LCDPtr1->LCDVRS;
2200         else
2201                 tempbx = LCDPtr->LCDVRS;
2202
2203         tempcx = push1;
2204
2205         if (pVBInfo->LCDInfo & EnableScalingLCD)
2206                 tempcx = LCDPtr1->LCDVSync;
2207
2208         tempcx += tempbx;
2209         if (tempcx >= tempax)
2210                 tempcx -= tempax;
2211
2212         xgifb_reg_set(pVBInfo->Part1Port, 0x18,
2213                         (unsigned short) (tempbx & 0xff));
2214         xgifb_reg_and_or(pVBInfo->Part1Port, 0x19, ~0x0f,
2215                         (unsigned short) (tempcx & 0x0f));
2216
2217         tempax = ((tempbx >> 8) & 0x07) << 3;
2218
2219         tempbx = pVBInfo->VGAVDE;
2220         if (tempbx != pVBInfo->VDE)
2221                 tempax |= 0x40;
2222
2223         if (pVBInfo->LCDInfo & XGI_EnableLVDSDDA)
2224                 tempax |= 0x40;
2225
2226         xgifb_reg_and_or(pVBInfo->Part1Port, 0x1a, 0x07,
2227                                 tempax);
2228
2229         tempcx = pVBInfo->VGAVT;
2230         tempbx = pVBInfo->VDE;
2231         tempax = pVBInfo->VGAVDE;
2232         tempcx -= tempax;
2233
2234         temp = tempax; /* 0430 ylshieh */
2235         temp1 = (temp << 18) / tempbx;
2236
2237         tempdx = (unsigned short) ((temp << 18) % tempbx);
2238
2239         if (tempdx != 0)
2240                 temp1 += 1;
2241
2242         temp2 = temp1;
2243         push3 = temp2;
2244
2245         xgifb_reg_set(pVBInfo->Part1Port, 0x37,
2246                         (unsigned short) (temp2 & 0xff));
2247         xgifb_reg_set(pVBInfo->Part1Port, 0x36,
2248                         (unsigned short) ((temp2 >> 8) & 0xff));
2249
2250         tempbx = (unsigned short) (temp2 >> 16);
2251         tempax = tempbx & 0x03;
2252
2253         tempbx = pVBInfo->VGAVDE;
2254         if (tempbx == pVBInfo->VDE)
2255                 tempax |= 0x04;
2256
2257         xgifb_reg_set(pVBInfo->Part1Port, 0x35, tempax);
2258
2259         if (pVBInfo->VBType & VB_XGI301C) {
2260                 temp2 = push3;
2261                 xgifb_reg_set(pVBInfo->Part4Port,
2262                               0x3c,
2263                               (unsigned short) (temp2 & 0xff));
2264                 xgifb_reg_set(pVBInfo->Part4Port,
2265                               0x3b,
2266                               (unsigned short) ((temp2 >> 8) &
2267                               0xff));
2268                 tempbx = (unsigned short) (temp2 >> 16);
2269                 xgifb_reg_and_or(pVBInfo->Part4Port, 0x3a,
2270                                 ~0xc0,
2271                                 (unsigned short) ((tempbx &
2272                                                    0xff) << 6));
2273
2274                 tempcx = pVBInfo->VGAVDE;
2275                 if (tempcx == pVBInfo->VDE)
2276                         xgifb_reg_and_or(pVBInfo->Part4Port,
2277                                         0x30, ~0x0c, 0x00);
2278                 else
2279                         xgifb_reg_and_or(pVBInfo->Part4Port,
2280                                         0x30, ~0x0c, 0x08);
2281         }
2282
2283         tempcx = pVBInfo->VGAHDE;
2284         tempbx = pVBInfo->HDE;
2285
2286         temp1 = tempcx << 16;
2287
2288         tempax = (unsigned short) (temp1 / tempbx);
2289
2290         if ((tempbx & 0xffff) == (tempcx & 0xffff))
2291                 tempax = 65535;
2292
2293         temp3 = tempax;
2294         temp1 = pVBInfo->VGAHDE << 16;
2295
2296         temp1 /= temp3;
2297         temp3 = temp3 << 16;
2298         temp1 -= 1;
2299
2300         temp3 = (temp3 & 0xffff0000) + (temp1 & 0xffff);
2301
2302         tempax = (unsigned short) (temp3 & 0xff);
2303         xgifb_reg_set(pVBInfo->Part1Port, 0x1f, tempax);
2304
2305         temp1 = pVBInfo->VGAVDE << 18;
2306         temp1 = temp1 / push3;
2307         tempbx = (unsigned short) (temp1 & 0xffff);
2308
2309         if (pVBInfo->LCDResInfo == Panel_1024x768)
2310                 tempbx -= 1;
2311
2312         tempax = ((tempbx >> 8) & 0xff) << 3;
2313         tempax |= (unsigned short) ((temp3 >> 8) & 0x07);
2314         xgifb_reg_set(pVBInfo->Part1Port, 0x20,
2315                         (unsigned short) (tempax & 0xff));
2316         xgifb_reg_set(pVBInfo->Part1Port, 0x21,
2317                         (unsigned short) (tempbx & 0xff));
2318
2319         temp3 = temp3 >> 16;
2320
2321         if (modeflag & HalfDCLK)
2322                 temp3 = temp3 >> 1;
2323
2324         xgifb_reg_set(pVBInfo->Part1Port, 0x22,
2325                         (unsigned short) ((temp3 >> 8) & 0xff));
2326         xgifb_reg_set(pVBInfo->Part1Port, 0x23,
2327                         (unsigned short) (temp3 & 0xff));
2328 }
2329
2330 /* --------------------------------------------------------------------- */
2331 /* Function : XGI_GETLCDVCLKPtr */
2332 /* Input : */
2333 /* Output : al -> VCLK Index */
2334 /* Description : */
2335 /* --------------------------------------------------------------------- */
2336 static void XGI_GetLCDVCLKPtr(unsigned char *di_0, unsigned char *di_1,
2337                 struct vb_device_info *pVBInfo)
2338 {
2339         unsigned short index;
2340
2341         if (pVBInfo->VBInfo & (SetCRT2ToLCD | XGI_SetCRT2ToLCDA)) {
2342                 index = XGI_GetLCDCapPtr1(pVBInfo);
2343
2344                 if (pVBInfo->VBInfo & SetCRT2ToLCD) { /* LCDB */
2345                         *di_0 = pVBInfo->LCDCapList[index].LCUCHAR_VCLKData1;
2346                         *di_1 = pVBInfo->LCDCapList[index].LCUCHAR_VCLKData2;
2347                 } else { /* LCDA */
2348                         *di_0 = pVBInfo->LCDCapList[index].LCDA_VCLKData1;
2349                         *di_1 = pVBInfo->LCDCapList[index].LCDA_VCLKData2;
2350                 }
2351         }
2352         return;
2353 }
2354
2355 static unsigned char XGI_GetVCLKPtr(unsigned short RefreshRateTableIndex,
2356                 unsigned short ModeNo, unsigned short ModeIdIndex,
2357                 struct vb_device_info *pVBInfo)
2358 {
2359
2360         unsigned short index, modeflag;
2361         unsigned char tempal;
2362
2363         /* si+Ext_ResInfo */
2364         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
2365
2366         if ((pVBInfo->SetFlag & ProgrammingCRT2) &&
2367             (!(pVBInfo->LCDInfo & EnableScalingLCD))) { /* {LCDA/LCDB} */
2368                 index = XGI_GetLCDCapPtr(pVBInfo);
2369                 tempal = pVBInfo->LCDCapList[index].LCD_VCLK;
2370
2371                 if (pVBInfo->VBInfo & (SetCRT2ToLCD | XGI_SetCRT2ToLCDA))
2372                         return tempal;
2373
2374                 /* {TV} */
2375                 if (pVBInfo->VBType &
2376                     (VB_SIS301B |
2377                      VB_SIS302B |
2378                      VB_SIS301LV |
2379                      VB_SIS302LV |
2380                      VB_XGI301C)) {
2381                         if (pVBInfo->VBInfo & SetCRT2ToHiVision) {
2382                                 tempal = TVCLKBASE_315 + HiTVVCLKDIV2;
2383                                 if (!(pVBInfo->TVInfo & RPLLDIV2XO))
2384                                         tempal = TVCLKBASE_315 + HiTVVCLK;
2385                                 if (pVBInfo->TVInfo & TVSimuMode) {
2386                                         tempal = TVCLKBASE_315 + HiTVSimuVCLK;
2387                                         if (!(modeflag & Charx8Dot))
2388                                                 tempal = TVCLKBASE_315 + HiTVTextVCLK;
2389
2390                                 }
2391                                 return tempal;
2392                         }
2393
2394                         if (pVBInfo->TVInfo & TVSetYPbPr750p) {
2395                                 tempal = XGI_YPbPr750pVCLK;
2396                                 return tempal;
2397                         }
2398
2399                         if (pVBInfo->TVInfo & TVSetYPbPr525p) {
2400                                 tempal = YPbPr525pVCLK;
2401                                 return tempal;
2402                         }
2403
2404                         tempal = NTSC1024VCLK;
2405
2406                         if (!(pVBInfo->TVInfo & NTSC1024x768)) {
2407                                 tempal = TVCLKBASE_315 + TVVCLKDIV2;
2408                                 if (!(pVBInfo->TVInfo & RPLLDIV2XO))
2409                                         tempal = TVCLKBASE_315 + TVVCLK;
2410                         }
2411
2412                         if (pVBInfo->VBInfo & SetCRT2ToTV)
2413                                 return tempal;
2414                 }
2415         } /* {End of VB} */
2416
2417         tempal = (unsigned char) inb((pVBInfo->P3ca + 0x02));
2418         tempal = tempal >> 2;
2419         tempal &= 0x03;
2420
2421         /* for Dot8 Scaling LCD */
2422         if ((pVBInfo->LCDInfo & EnableScalingLCD) && (modeflag & Charx8Dot))
2423                 tempal = tempal ^ tempal; /* ; set to VCLK25MHz always */
2424
2425         tempal = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
2426         return tempal;
2427 }
2428
2429 static void XGI_GetVCLKLen(unsigned char tempal, unsigned char *di_0,
2430                 unsigned char *di_1, struct vb_device_info *pVBInfo)
2431 {
2432         if (pVBInfo->VBType & (VB_SIS301 | VB_SIS301B | VB_SIS302B
2433                         | VB_SIS301LV | VB_SIS302LV | VB_XGI301C)) {
2434                 if ((!(pVBInfo->VBInfo & XGI_SetCRT2ToLCDA)) && (pVBInfo->SetFlag
2435                                 & ProgrammingCRT2)) {
2436                         *di_0 = (unsigned char) XGI_VBVCLKData[tempal].SR2B;
2437                         *di_1 = XGI_VBVCLKData[tempal].SR2C;
2438                 }
2439         } else {
2440                 *di_0 = XGI_VCLKData[tempal].SR2B;
2441                 *di_1 = XGI_VCLKData[tempal].SR2C;
2442         }
2443 }
2444
2445 static void XGI_SetCRT2ECLK(unsigned short ModeNo, unsigned short ModeIdIndex,
2446                 unsigned short RefreshRateTableIndex,
2447                 struct vb_device_info *pVBInfo)
2448 {
2449         unsigned char di_0, di_1, tempal;
2450         int i;
2451
2452         tempal = XGI_GetVCLKPtr(RefreshRateTableIndex, ModeNo, ModeIdIndex,
2453                         pVBInfo);
2454         XGI_GetVCLKLen(tempal, &di_0, &di_1, pVBInfo);
2455         XGI_GetLCDVCLKPtr(&di_0, &di_1, pVBInfo);
2456
2457         for (i = 0; i < 4; i++) {
2458                 xgifb_reg_and_or(pVBInfo->P3d4, 0x31, ~0x30,
2459                                 (unsigned short) (0x10 * i));
2460                 if ((!(pVBInfo->VBInfo & XGI_SetCRT2ToLCDA))
2461                                 && (!(pVBInfo->VBInfo & SetInSlaveMode))) {
2462                         xgifb_reg_set(pVBInfo->P3c4, 0x2e, di_0);
2463                         xgifb_reg_set(pVBInfo->P3c4, 0x2f, di_1);
2464                 } else {
2465                         xgifb_reg_set(pVBInfo->P3c4, 0x2b, di_0);
2466                         xgifb_reg_set(pVBInfo->P3c4, 0x2c, di_1);
2467                 }
2468         }
2469 }
2470
2471 static void XGI_UpdateModeInfo(struct xgi_hw_device_info *HwDeviceExtension,
2472                 struct vb_device_info *pVBInfo)
2473 {
2474         unsigned short tempcl, tempch, temp, tempbl, tempax;
2475
2476         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
2477                         | VB_SIS302LV | VB_XGI301C)) {
2478                 tempcl = 0;
2479                 tempch = 0;
2480                 temp = xgifb_reg_get(pVBInfo->P3c4, 0x01);
2481
2482                 if (!(temp & 0x20)) {
2483                         temp = xgifb_reg_get(pVBInfo->P3d4, 0x17);
2484                         if (temp & 0x80) {
2485                                 temp = xgifb_reg_get(pVBInfo->P3d4, 0x53);
2486                                 if (!(temp & 0x40))
2487                                         tempcl |= ActiveCRT1;
2488                         }
2489                 }
2490
2491                 temp = xgifb_reg_get(pVBInfo->Part1Port, 0x2e);
2492                 temp &= 0x0f;
2493
2494                 if (!(temp == 0x08)) {
2495                         /* Check ChannelA */
2496                         tempax = xgifb_reg_get(pVBInfo->Part1Port, 0x13);
2497                         if (tempax & 0x04)
2498                                 tempcl = tempcl | ActiveLCD;
2499
2500                         temp &= 0x05;
2501
2502                         if (!(tempcl & ActiveLCD))
2503                                 if (temp == 0x01)
2504                                         tempcl |= ActiveCRT2;
2505
2506                         if (temp == 0x04)
2507                                 tempcl |= ActiveLCD;
2508
2509                         if (temp == 0x05) {
2510                                 temp = xgifb_reg_get(pVBInfo->Part2Port, 0x00);
2511
2512                                 if (!(temp & 0x08))
2513                                         tempch |= ActiveAVideo;
2514
2515                                 if (!(temp & 0x04))
2516                                         tempch |= ActiveSVideo;
2517
2518                                 if (temp & 0x02)
2519                                         tempch |= ActiveSCART;
2520
2521                                 if (pVBInfo->VBInfo & SetCRT2ToHiVision) {
2522                                         if (temp & 0x01)
2523                                                 tempch |= ActiveHiTV;
2524                                 }
2525
2526                                 if (pVBInfo->VBInfo & SetCRT2ToYPbPr525750) {
2527                                         temp = xgifb_reg_get(
2528                                                         pVBInfo->Part2Port,
2529                                                         0x4d);
2530
2531                                         if (temp & 0x10)
2532                                                 tempch |= ActiveYPbPr;
2533                                 }
2534
2535                                 if (tempch != 0)
2536                                         tempcl |= ActiveTV;
2537                         }
2538                 }
2539
2540                 temp = xgifb_reg_get(pVBInfo->P3d4, 0x3d);
2541                 if (tempcl & ActiveLCD) {
2542                         if ((pVBInfo->SetFlag & ReserveTVOption)) {
2543                                 if (temp & ActiveTV)
2544                                         tempcl |= ActiveTV;
2545                         }
2546                 }
2547                 temp = tempcl;
2548                 tempbl = ~XGI_ModeSwitchStatus;
2549                 xgifb_reg_and_or(pVBInfo->P3d4, 0x3d, tempbl, temp);
2550
2551                 if (!(pVBInfo->SetFlag & ReserveTVOption))
2552                         xgifb_reg_set(pVBInfo->P3d4, 0x3e, tempch);
2553         } else {
2554                 return;
2555         }
2556 }
2557
2558 void XGI_GetVBType(struct vb_device_info *pVBInfo)
2559 {
2560         unsigned short flag, tempbx, tempah;
2561
2562         if (pVBInfo->IF_DEF_LVDS != 0)
2563                 return;
2564
2565         tempbx = VB_SIS302B;
2566         flag = xgifb_reg_get(pVBInfo->Part4Port, 0x00);
2567         if (flag == 0x02)
2568                 goto finish;
2569
2570         tempbx = VB_SIS301;
2571         flag = xgifb_reg_get(pVBInfo->Part4Port, 0x01);
2572         if (flag < 0xB0)
2573                 goto finish;
2574
2575         tempbx = VB_SIS301B;
2576         if (flag < 0xC0)
2577                 goto bigger_than_0xB0;
2578
2579         tempbx = VB_XGI301C;
2580         if (flag < 0xD0)
2581                 goto bigger_than_0xB0;
2582
2583         tempbx = VB_SIS301LV;
2584         if (flag < 0xE0)
2585                 goto bigger_than_0xB0;
2586
2587         tempbx = VB_SIS302LV;
2588         tempah = xgifb_reg_get(pVBInfo->Part4Port, 0x39);
2589         if (tempah != 0xFF)
2590                 tempbx = VB_XGI301C;
2591
2592 bigger_than_0xB0:
2593         if (tempbx & (VB_SIS301B | VB_SIS302B)) {
2594                 flag = xgifb_reg_get(pVBInfo->Part4Port, 0x23);
2595                 if (!(flag & 0x02))
2596                         tempbx = tempbx | VB_NoLCD;
2597         }
2598
2599 finish:
2600         pVBInfo->VBType = tempbx;
2601 }
2602
2603 static void XGI_GetVBInfo(unsigned short ModeNo, unsigned short ModeIdIndex,
2604                 struct xgi_hw_device_info *HwDeviceExtension,
2605                 struct vb_device_info *pVBInfo)
2606 {
2607         unsigned short tempax, push, tempbx, temp, modeflag;
2608
2609         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
2610         pVBInfo->SetFlag = 0;
2611         pVBInfo->ModeType = modeflag & ModeTypeMask;
2612         tempbx = 0;
2613
2614         if (!(pVBInfo->VBType & 0xFFFF))
2615                 return;
2616
2617         /* Check Display Device */
2618         temp = xgifb_reg_get(pVBInfo->P3d4, 0x30);
2619         tempbx = tempbx | temp;
2620         temp = xgifb_reg_get(pVBInfo->P3d4, 0x31);
2621         push = temp;
2622         push = push << 8;
2623         tempax = temp << 8;
2624         tempbx = tempbx | tempax;
2625         temp = (SetCRT2ToDualEdge | SetCRT2ToYPbPr525750 | XGI_SetCRT2ToLCDA
2626                 | SetInSlaveMode | DisableCRT2Display);
2627         temp = 0xFFFF ^ temp;
2628         tempbx &= temp;
2629
2630         temp = xgifb_reg_get(pVBInfo->P3d4, 0x38);
2631
2632         if (pVBInfo->IF_DEF_LCDA == 1) {
2633
2634                 if (((HwDeviceExtension->jChipType >= XG20) ||
2635                      (HwDeviceExtension->jChipType >= XG40)) &&
2636                     (pVBInfo->IF_DEF_LVDS == 0)) {
2637                         if (pVBInfo->VBType &
2638                             (VB_SIS302B |
2639                              VB_SIS301LV |
2640                              VB_SIS302LV |
2641                              VB_XGI301C)) {
2642                                 if (temp & EnableDualEdge) {
2643                                         tempbx |= SetCRT2ToDualEdge;
2644                                         if (temp & SetToLCDA)
2645                                                 tempbx |= XGI_SetCRT2ToLCDA;
2646                                 }
2647                         }
2648                 }
2649         }
2650
2651         if (pVBInfo->IF_DEF_YPbPr == 1) {
2652                 if (((pVBInfo->IF_DEF_LVDS == 0) &&
2653                      ((pVBInfo->VBType & VB_SIS301LV) ||
2654                       (pVBInfo->VBType & VB_SIS302LV) ||
2655                       (pVBInfo->VBType & VB_XGI301C)))) {
2656                         if (temp & SetYPbPr) {
2657                                 if (pVBInfo->IF_DEF_HiVision == 1) {
2658                                         /* shampoo add for new
2659                                          * scratch */
2660                                         temp = xgifb_reg_get(
2661                                                 pVBInfo->P3d4,
2662                                                 0x35);
2663                                         temp &= YPbPrMode;
2664                                         tempbx |= SetCRT2ToHiVision;
2665
2666                                         if (temp != YPbPrMode1080i) {
2667                                                 tempbx &=
2668                                                         (~SetCRT2ToHiVision);
2669                                                 tempbx |=
2670                                                         SetCRT2ToYPbPr525750;
2671                                         }
2672                                 }
2673                         }
2674                 }
2675         }
2676
2677         tempax = push; /* restore CR31 */
2678
2679         if (pVBInfo->IF_DEF_LVDS == 0) {
2680                 if (pVBInfo->IF_DEF_YPbPr == 1) {
2681                         if (pVBInfo->IF_DEF_HiVision == 1)
2682                                 temp = 0x09FC;
2683                         else
2684                                 temp = 0x097C;
2685                 } else {
2686                         if (pVBInfo->IF_DEF_HiVision == 1)
2687                                 temp = 0x01FC;
2688                         else
2689                                 temp = 0x017C;
2690                 }
2691         } else { /* 3nd party chip */
2692                 temp = SetCRT2ToLCD;
2693         }
2694
2695         if (!(tempbx & temp)) {
2696                 tempax |= DisableCRT2Display;
2697                 tempbx = 0;
2698         }
2699
2700         if (pVBInfo->IF_DEF_LCDA == 1) { /* Select Display Device */
2701                 if (!(pVBInfo->VBType & VB_NoLCD)) {
2702                         if (tempbx & XGI_SetCRT2ToLCDA) {
2703                                 if (tempbx & SetSimuScanMode)
2704                                         tempbx &= (~(SetCRT2ToLCD |
2705                                                      SetCRT2ToRAMDAC |
2706                                                      SwitchCRT2));
2707                                 else
2708                                         tempbx &= (~(SetCRT2ToLCD |
2709                                                      SetCRT2ToRAMDAC |
2710                                                      SetCRT2ToTV |
2711                                                      SwitchCRT2));
2712                         }
2713                 }
2714         }
2715
2716         /* shampoo add */
2717         /* for driver abnormal */
2718         if (!(tempbx & (SwitchCRT2 | SetSimuScanMode))) {
2719                 if (pVBInfo->IF_DEF_CRT2Monitor == 1) {
2720                         if (tempbx & SetCRT2ToRAMDAC) {
2721                                 tempbx &= (0xFF00 |
2722                                            SetCRT2ToRAMDAC |
2723                                            SwitchCRT2 |
2724                                            SetSimuScanMode);
2725                                 tempbx &= (0x00FF | (~SetCRT2ToYPbPr525750));
2726                         }
2727                 } else {
2728                         tempbx &= (~(SetCRT2ToRAMDAC |
2729                                      SetCRT2ToLCD |
2730                                      SetCRT2ToTV));
2731                 }
2732         }
2733
2734         if (!(pVBInfo->VBType & VB_NoLCD)) {
2735                 if (tempbx & SetCRT2ToLCD) {
2736                         tempbx &= (0xFF00 |
2737                                    SetCRT2ToLCD |
2738                                    SwitchCRT2 |
2739                                    SetSimuScanMode);
2740                         tempbx &= (0x00FF | (~SetCRT2ToYPbPr525750));
2741                 }
2742         }
2743
2744         if (tempbx & SetCRT2ToSCART) {
2745                 tempbx &= (0xFF00 |
2746                            SetCRT2ToSCART |
2747                            SwitchCRT2 |
2748                            SetSimuScanMode);
2749                 tempbx &= (0x00FF | (~SetCRT2ToYPbPr525750));
2750         }
2751
2752         if (pVBInfo->IF_DEF_YPbPr == 1) {
2753                 if (tempbx & SetCRT2ToYPbPr525750)
2754                         tempbx &= (0xFF00 |
2755                                    SwitchCRT2 |
2756                                    SetSimuScanMode);
2757         }
2758
2759         if (pVBInfo->IF_DEF_HiVision == 1) {
2760                 if (tempbx & SetCRT2ToHiVision)
2761                         tempbx &= (0xFF00 |
2762                                    SetCRT2ToHiVision |
2763                                    SwitchCRT2 |
2764                                    SetSimuScanMode);
2765         }
2766
2767         if (tempax & DisableCRT2Display) { /* Set Display Device Info */
2768                 if (!(tempbx & (SwitchCRT2 | SetSimuScanMode)))
2769                         tempbx = DisableCRT2Display;
2770         }
2771
2772         if (!(tempbx & DisableCRT2Display)) {
2773                 if ((!(tempbx & DriverMode)) ||
2774                     (!(modeflag & CRT2Mode))) {
2775                         if (pVBInfo->IF_DEF_LCDA == 1) {
2776                                 if (!(tempbx & XGI_SetCRT2ToLCDA))
2777                                         tempbx |= (SetInSlaveMode |
2778                                                    SetSimuScanMode);
2779                         }
2780                 }
2781
2782                 /* LCD+TV can't support in slave mode
2783                  * (Force LCDA+TV->LCDB) */
2784                 if ((tempbx & SetInSlaveMode) &&
2785                     (tempbx & XGI_SetCRT2ToLCDA)) {
2786                         tempbx ^= (SetCRT2ToLCD |
2787                                    XGI_SetCRT2ToLCDA |
2788                                    SetCRT2ToDualEdge);
2789                         pVBInfo->SetFlag |= ReserveTVOption;
2790                 }
2791         }
2792
2793         pVBInfo->VBInfo = tempbx;
2794 }
2795
2796 static void XGI_GetTVInfo(unsigned short ModeNo, unsigned short ModeIdIndex,
2797                 struct vb_device_info *pVBInfo)
2798 {
2799         unsigned short temp, tempbx = 0, resinfo = 0, modeflag, index1;
2800
2801         tempbx = 0;
2802         resinfo = 0;
2803
2804         if (pVBInfo->VBInfo & SetCRT2ToTV) {
2805                 modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
2806                 resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
2807
2808                 if (pVBInfo->VBInfo & SetCRT2ToTV) {
2809                         temp = xgifb_reg_get(pVBInfo->P3d4, 0x35);
2810                         tempbx = temp;
2811                         if (tempbx & TVSetPAL) {
2812                                 tempbx &= (SetCHTVOverScan |
2813                                            TVSetPALM |
2814                                            TVSetPALN |
2815                                            TVSetPAL);
2816                                 if (tempbx & TVSetPALM)
2817                                         /* set to NTSC if PAL-M */
2818                                         tempbx &= ~TVSetPAL;
2819                         } else
2820                                 tempbx &= (SetCHTVOverScan |
2821                                            TVSetNTSCJ |
2822                                            TVSetPAL);
2823                 }
2824
2825                 if (pVBInfo->IF_DEF_LVDS == 0) {
2826                         if (pVBInfo->VBInfo & SetCRT2ToSCART)
2827                                 tempbx |= TVSetPAL;
2828                 }
2829
2830                 if (pVBInfo->IF_DEF_YPbPr == 1) {
2831                         if (pVBInfo->VBInfo & SetCRT2ToYPbPr525750) {
2832                                 index1 = xgifb_reg_get(pVBInfo->P3d4, 0x35);
2833                                 index1 &= YPbPrMode;
2834
2835                                 if (index1 == YPbPrMode525i)
2836                                         tempbx |= TVSetYPbPr525i;
2837
2838                                 if (index1 == YPbPrMode525p)
2839                                         tempbx = tempbx | TVSetYPbPr525p;
2840                                 if (index1 == YPbPrMode750p)
2841                                         tempbx = tempbx | TVSetYPbPr750p;
2842                         }
2843                 }
2844
2845                 if (pVBInfo->IF_DEF_HiVision == 1) {
2846                         if (pVBInfo->VBInfo & SetCRT2ToHiVision)
2847                                 tempbx = tempbx | TVSetHiVision | TVSetPAL;
2848                 }
2849
2850                 if (pVBInfo->IF_DEF_LVDS == 0) { /* shampoo */
2851                         if ((pVBInfo->VBInfo & SetInSlaveMode) &&
2852                             (!(pVBInfo->VBInfo & SetNotSimuMode)))
2853                                 tempbx |= TVSimuMode;
2854
2855                         if (!(tempbx & TVSetPAL) &&
2856                             (modeflag > 13) &&
2857                             (resinfo == 8)) /* NTSC 1024x768, */
2858                                 tempbx |= NTSC1024x768;
2859
2860                         tempbx |= RPLLDIV2XO;
2861
2862                         if (pVBInfo->VBInfo & SetCRT2ToHiVision) {
2863                                 if (pVBInfo->VBInfo & SetInSlaveMode)
2864                                         tempbx &= (~RPLLDIV2XO);
2865                         } else {
2866                                 if (tempbx &
2867                                     (TVSetYPbPr525p | TVSetYPbPr750p))
2868                                         tempbx &= (~RPLLDIV2XO);
2869                                 else if (!(pVBInfo->VBType &
2870                                          (VB_SIS301B |
2871                                           VB_SIS302B |
2872                                           VB_SIS301LV |
2873                                           VB_SIS302LV |
2874                                           VB_XGI301C))) {
2875                                         if (tempbx & TVSimuMode)
2876                                                 tempbx &= (~RPLLDIV2XO);
2877                                 }
2878                         }
2879                 }
2880         }
2881         pVBInfo->TVInfo = tempbx;
2882 }
2883
2884 static unsigned char XGI_GetLCDInfo(unsigned short ModeNo,
2885                 unsigned short ModeIdIndex, struct vb_device_info *pVBInfo)
2886 {
2887         unsigned short temp, tempax, tempbx, modeflag, resinfo = 0, LCDIdIndex;
2888
2889         pVBInfo->LCDResInfo = 0;
2890         pVBInfo->LCDTypeInfo = 0;
2891         pVBInfo->LCDInfo = 0;
2892
2893         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
2894         /* si+Ext_ResInfo // */
2895         resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
2896         temp = xgifb_reg_get(pVBInfo->P3d4, 0x36); /* Get LCD Res.Info */
2897         tempbx = temp & 0x0F;
2898
2899         if (tempbx == 0)
2900                 tempbx = Panel_1024x768; /* default */
2901
2902         /* LCD75 */
2903         if ((tempbx == Panel_1024x768) || (tempbx == Panel_1280x1024)) {
2904                 if (pVBInfo->VBInfo & DriverMode) {
2905                         tempax = xgifb_reg_get(pVBInfo->P3d4, 0x33);
2906                         if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA)
2907                                 tempax &= 0x0F;
2908                         else
2909                                 tempax = tempax >> 4;
2910
2911                         if ((resinfo == 6) || (resinfo == 9)) {
2912                                 if (tempax >= 3)
2913                                         tempbx |= PanelRef75Hz;
2914                         } else if ((resinfo == 7) || (resinfo == 8)) {
2915                                 if (tempax >= 4)
2916                                         tempbx |= PanelRef75Hz;
2917                         }
2918                 }
2919         }
2920
2921         pVBInfo->LCDResInfo = tempbx;
2922
2923         /* End of LCD75 */
2924
2925         if (!(pVBInfo->VBInfo & (SetCRT2ToLCD | XGI_SetCRT2ToLCDA)))
2926                 return 0;
2927
2928         tempbx = 0;
2929
2930         temp = xgifb_reg_get(pVBInfo->P3d4, 0x37);
2931
2932         temp &= (ScalingLCD | LCDNonExpanding | LCDSyncBit | SetPWDEnable);
2933
2934         tempbx |= temp;
2935
2936         LCDIdIndex = XGI_GetLCDCapPtr1(pVBInfo);
2937
2938         tempax = pVBInfo->LCDCapList[LCDIdIndex].LCD_Capability;
2939
2940         if (pVBInfo->IF_DEF_LVDS == 0) { /* shampoo */
2941                 if (((pVBInfo->VBType & VB_SIS302LV) || (pVBInfo->VBType
2942                                 & VB_XGI301C)) && (tempax & XGI_LCDDualLink)) {
2943                         tempbx |= SetLCDDualLink;
2944                 }
2945         }
2946
2947         if (pVBInfo->IF_DEF_LVDS == 0) {
2948                 if ((pVBInfo->LCDResInfo == Panel_1400x1050) && (pVBInfo->VBInfo
2949                                 & SetCRT2ToLCD) && (resinfo == 9) &&
2950                                 (!(tempbx & EnableScalingLCD)))
2951                         /* set to center in 1280x1024 LCDB for Panel_1400x1050 */
2952                         tempbx |= SetLCDtoNonExpanding;
2953         }
2954
2955         if (pVBInfo->IF_DEF_ExpLink == 1) {
2956                 if (modeflag & HalfDCLK) {
2957                         if (!(tempbx & SetLCDtoNonExpanding)) {
2958                                 tempbx |= XGI_EnableLVDSDDA;
2959                         } else {
2960                                 if (pVBInfo->LCDResInfo == Panel_1024x768) {
2961                                         if (resinfo == 4) {/* 512x384 */
2962                                                 tempbx |= XGI_EnableLVDSDDA;
2963                                         }
2964                                 }
2965                         }
2966                 }
2967         }
2968
2969         if (pVBInfo->VBInfo & SetInSlaveMode) {
2970                 if (pVBInfo->VBInfo & SetNotSimuMode)
2971                         tempbx |= XGI_LCDVESATiming;
2972         } else {
2973                 tempbx |= XGI_LCDVESATiming;
2974         }
2975
2976         pVBInfo->LCDInfo = tempbx;
2977
2978         return 1;
2979 }
2980
2981 unsigned char XGI_SearchModeID(unsigned short ModeNo,
2982                 unsigned short *ModeIdIndex, struct vb_device_info *pVBInfo)
2983 {
2984         for (*ModeIdIndex = 0;; (*ModeIdIndex)++) {
2985                 if (pVBInfo->EModeIDTable[*ModeIdIndex].Ext_ModeID == ModeNo)
2986                         break;
2987                 if (pVBInfo->EModeIDTable[*ModeIdIndex].Ext_ModeID == 0xFF)
2988                         return 0;
2989         }
2990
2991         return 1;
2992 }
2993
2994 static unsigned char XG21GPIODataTransfer(unsigned char ujDate)
2995 {
2996         unsigned char ujRet = 0;
2997         unsigned char i = 0;
2998
2999         for (i = 0; i < 8; i++) {
3000                 ujRet = ujRet << 1;
3001                 ujRet |= (ujDate >> i) & 1;
3002         }
3003
3004         return ujRet;
3005 }
3006
3007 /*----------------------------------------------------------------------------*/
3008 /* output                                                                     */
3009 /*      bl[5] : LVDS signal                                                   */
3010 /*      bl[1] : LVDS backlight                                                */
3011 /*      bl[0] : LVDS VDD                                                      */
3012 /*----------------------------------------------------------------------------*/
3013 static unsigned char XGI_XG21GetPSCValue(struct vb_device_info *pVBInfo)
3014 {
3015         unsigned char CR4A, temp;
3016
3017         CR4A = xgifb_reg_get(pVBInfo->P3d4, 0x4A);
3018         xgifb_reg_and(pVBInfo->P3d4, 0x4A, ~0x23); /* enable GPIO write */
3019
3020         temp = xgifb_reg_get(pVBInfo->P3d4, 0x48);
3021
3022         temp = XG21GPIODataTransfer(temp);
3023         temp &= 0x23;
3024         xgifb_reg_set(pVBInfo->P3d4, 0x4A, CR4A);
3025         return temp;
3026 }
3027
3028 /*----------------------------------------------------------------------------*/
3029 /* output                                                                     */
3030 /*      bl[5] : LVDS signal                                                   */
3031 /*      bl[1] : LVDS backlight                                                */
3032 /*      bl[0] : LVDS VDD                                                      */
3033 /*----------------------------------------------------------------------------*/
3034 static unsigned char XGI_XG27GetPSCValue(struct vb_device_info *pVBInfo)
3035 {
3036         unsigned char CR4A, CRB4, temp;
3037
3038         CR4A = xgifb_reg_get(pVBInfo->P3d4, 0x4A);
3039         xgifb_reg_and(pVBInfo->P3d4, 0x4A, ~0x0C); /* enable GPIO write */
3040
3041         temp = xgifb_reg_get(pVBInfo->P3d4, 0x48);
3042
3043         temp &= 0x0C;
3044         temp >>= 2;
3045         xgifb_reg_set(pVBInfo->P3d4, 0x4A, CR4A);
3046         CRB4 = xgifb_reg_get(pVBInfo->P3d4, 0xB4);
3047         temp |= ((CRB4 & 0x04) << 3);
3048         return temp;
3049 }
3050
3051 /*----------------------------------------------------------------------------*/
3052 /* input                                                                      */
3053 /*      bl[5] : 1;LVDS signal on                                              */
3054 /*      bl[1] : 1;LVDS backlight on                                           */
3055 /*      bl[0] : 1:LVDS VDD on                                                 */
3056 /*      bh: 100000b : clear bit 5, to set bit5                                */
3057 /*          000010b : clear bit 1, to set bit1                                */
3058 /*          000001b : clear bit 0, to set bit0                                */
3059 /*----------------------------------------------------------------------------*/
3060 static void XGI_XG21BLSignalVDD(unsigned short tempbh, unsigned short tempbl,
3061                 struct vb_device_info *pVBInfo)
3062 {
3063         unsigned char CR4A, temp;
3064
3065         CR4A = xgifb_reg_get(pVBInfo->P3d4, 0x4A);
3066         tempbh &= 0x23;
3067         tempbl &= 0x23;
3068         xgifb_reg_and(pVBInfo->P3d4, 0x4A, ~tempbh); /* enable GPIO write */
3069
3070         if (tempbh & 0x20) {
3071                 temp = (tempbl >> 4) & 0x02;
3072
3073                 /* CR B4[1] */
3074                 xgifb_reg_and_or(pVBInfo->P3d4, 0xB4, ~0x02, temp);
3075
3076         }
3077
3078         temp = xgifb_reg_get(pVBInfo->P3d4, 0x48);
3079
3080         temp = XG21GPIODataTransfer(temp);
3081         temp &= ~tempbh;
3082         temp |= tempbl;
3083         xgifb_reg_set(pVBInfo->P3d4, 0x48, temp);
3084 }
3085
3086 static void XGI_XG27BLSignalVDD(unsigned short tempbh, unsigned short tempbl,
3087                 struct vb_device_info *pVBInfo)
3088 {
3089         unsigned char CR4A, temp;
3090         unsigned short tempbh0, tempbl0;
3091
3092         tempbh0 = tempbh;
3093         tempbl0 = tempbl;
3094         tempbh0 &= 0x20;
3095         tempbl0 &= 0x20;
3096         tempbh0 >>= 3;
3097         tempbl0 >>= 3;
3098
3099         if (tempbh & 0x20) {
3100                 temp = (tempbl >> 4) & 0x02;
3101
3102                 /* CR B4[1] */
3103                 xgifb_reg_and_or(pVBInfo->P3d4, 0xB4, ~0x02, temp);
3104
3105         }
3106         xgifb_reg_and_or(pVBInfo->P3d4, 0xB4, ~tempbh0, tempbl0);
3107
3108         CR4A = xgifb_reg_get(pVBInfo->P3d4, 0x4A);
3109         tempbh &= 0x03;
3110         tempbl &= 0x03;
3111         tempbh <<= 2;
3112         tempbl <<= 2; /* GPIOC,GPIOD */
3113         xgifb_reg_and(pVBInfo->P3d4, 0x4A, ~tempbh); /* enable GPIO write */
3114         xgifb_reg_and_or(pVBInfo->P3d4, 0x48, ~tempbh, tempbl);
3115 }
3116
3117 /* --------------------------------------------------------------------- */
3118 /* Function : XGI_XG21SetPanelDelay */
3119 /* Input : */
3120 /* Output : */
3121 /* Description : */
3122 /* I/P : bl : 1 ; T1 : the duration between CPL on and signal on */
3123 /* : bl : 2 ; T2 : the duration signal on and Vdd on */
3124 /* : bl : 3 ; T3 : the duration between CPL off and signal off */
3125 /* : bl : 4 ; T4 : the duration signal off and Vdd off */
3126 /* --------------------------------------------------------------------- */
3127 static void XGI_XG21SetPanelDelay(struct xgifb_video_info *xgifb_info,
3128                 unsigned short tempbl,
3129                 struct vb_device_info *pVBInfo)
3130 {
3131         if (tempbl == 1)
3132                 mdelay(xgifb_info->lvds_data.PSC_S1);
3133
3134         if (tempbl == 2)
3135                 mdelay(xgifb_info->lvds_data.PSC_S2);
3136
3137         if (tempbl == 3)
3138                 mdelay(xgifb_info->lvds_data.PSC_S3);
3139
3140         if (tempbl == 4)
3141                 mdelay(xgifb_info->lvds_data.PSC_S4);
3142 }
3143
3144 static void XGI_DisplayOn(struct xgifb_video_info *xgifb_info,
3145                 struct xgi_hw_device_info *pXGIHWDE,
3146                 struct vb_device_info *pVBInfo)
3147 {
3148
3149         xgifb_reg_and_or(pVBInfo->P3c4, 0x01, 0xDF, 0x00);
3150         if (pXGIHWDE->jChipType == XG21) {
3151                 if (pVBInfo->IF_DEF_LVDS == 1) {
3152                         if (!(XGI_XG21GetPSCValue(pVBInfo) & 0x1)) {
3153                                 /* LVDS VDD on */
3154                                 XGI_XG21BLSignalVDD(0x01, 0x01, pVBInfo);
3155                                 XGI_XG21SetPanelDelay(xgifb_info, 2, pVBInfo);
3156                         }
3157                         if (!(XGI_XG21GetPSCValue(pVBInfo) & 0x20))
3158                                 /* LVDS signal on */
3159                                 XGI_XG21BLSignalVDD(0x20, 0x20, pVBInfo);
3160                         XGI_XG21SetPanelDelay(xgifb_info, 3, pVBInfo);
3161                         /* LVDS backlight on */
3162                         XGI_XG21BLSignalVDD(0x02, 0x02, pVBInfo);
3163                 } else {
3164                         /* DVO/DVI signal on */
3165                         XGI_XG21BLSignalVDD(0x20, 0x20, pVBInfo);
3166                 }
3167
3168         }
3169
3170         if (pXGIHWDE->jChipType == XG27) {
3171                 if (pVBInfo->IF_DEF_LVDS == 1) {
3172                         if (!(XGI_XG27GetPSCValue(pVBInfo) & 0x1)) {
3173                                 /* LVDS VDD on */
3174                                 XGI_XG27BLSignalVDD(0x01, 0x01, pVBInfo);
3175                                 XGI_XG21SetPanelDelay(xgifb_info, 2, pVBInfo);
3176                         }
3177                         if (!(XGI_XG27GetPSCValue(pVBInfo) & 0x20))
3178                                 /* LVDS signal on */
3179                                 XGI_XG27BLSignalVDD(0x20, 0x20, pVBInfo);
3180                         XGI_XG21SetPanelDelay(xgifb_info, 3, pVBInfo);
3181                         /* LVDS backlight on */
3182                         XGI_XG27BLSignalVDD(0x02, 0x02, pVBInfo);
3183                 } else {
3184                         /* DVO/DVI signal on */
3185                         XGI_XG27BLSignalVDD(0x20, 0x20, pVBInfo);
3186                 }
3187
3188         }
3189 }
3190
3191 void XGI_DisplayOff(struct xgifb_video_info *xgifb_info,
3192                 struct xgi_hw_device_info *pXGIHWDE,
3193                 struct vb_device_info *pVBInfo)
3194 {
3195
3196         if (pXGIHWDE->jChipType == XG21) {
3197                 if (pVBInfo->IF_DEF_LVDS == 1) {
3198                         /* LVDS backlight off */
3199                         XGI_XG21BLSignalVDD(0x02, 0x00, pVBInfo);
3200                         XGI_XG21SetPanelDelay(xgifb_info, 3, pVBInfo);
3201                 } else {
3202                         /* DVO/DVI signal off */
3203                         XGI_XG21BLSignalVDD(0x20, 0x00, pVBInfo);
3204                 }
3205         }
3206
3207         if (pXGIHWDE->jChipType == XG27) {
3208                 if ((XGI_XG27GetPSCValue(pVBInfo) & 0x2)) {
3209                         /* LVDS backlight off */
3210                         XGI_XG27BLSignalVDD(0x02, 0x00, pVBInfo);
3211                         XGI_XG21SetPanelDelay(xgifb_info, 3, pVBInfo);
3212                 }
3213
3214                 if (pVBInfo->IF_DEF_LVDS == 0)
3215                         /* DVO/DVI signal off */
3216                         XGI_XG27BLSignalVDD(0x20, 0x00, pVBInfo);
3217         }
3218
3219         xgifb_reg_and_or(pVBInfo->P3c4, 0x01, 0xDF, 0x20);
3220 }
3221
3222 static void XGI_WaitDisply(struct vb_device_info *pVBInfo)
3223 {
3224         while ((inb(pVBInfo->P3da) & 0x01))
3225                 break;
3226
3227         while (!(inb(pVBInfo->P3da) & 0x01))
3228                 break;
3229 }
3230
3231 static void XGI_AutoThreshold(struct vb_device_info *pVBInfo)
3232 {
3233         xgifb_reg_or(pVBInfo->Part1Port, 0x01, 0x40);
3234 }
3235
3236 static void XGI_SaveCRT2Info(unsigned short ModeNo,
3237                              struct vb_device_info *pVBInfo)
3238 {
3239         unsigned short temp1, temp2;
3240
3241         /* reserve CR34 for CRT1 Mode No */
3242         xgifb_reg_set(pVBInfo->P3d4, 0x34, ModeNo);
3243         temp1 = (pVBInfo->VBInfo & SetInSlaveMode) >> 8;
3244         temp2 = ~(SetInSlaveMode >> 8);
3245         xgifb_reg_and_or(pVBInfo->P3d4, 0x31, temp2, temp1);
3246 }
3247
3248 static void XGI_GetCRT2ResInfo(unsigned short ModeNo,
3249                                unsigned short ModeIdIndex,
3250                                struct vb_device_info *pVBInfo)
3251 {
3252         unsigned short xres, yres, modeflag, resindex;
3253
3254         resindex = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
3255         xres = pVBInfo->ModeResInfo[resindex].HTotal; /* xres->ax */
3256         yres = pVBInfo->ModeResInfo[resindex].VTotal; /* yres->bx */
3257         /* si+St_ModeFlag */
3258         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
3259
3260         if (modeflag & HalfDCLK)
3261                 xres *= 2;
3262
3263         if (modeflag & DoubleScanMode)
3264                 yres *= 2;
3265
3266         if (!(pVBInfo->VBInfo & SetCRT2ToLCD))
3267                 goto exit;
3268
3269         if (pVBInfo->IF_DEF_LVDS == 0) {
3270                 if (pVBInfo->LCDResInfo == Panel_1600x1200) {
3271                         if (!(pVBInfo->LCDInfo & XGI_LCDVESATiming)) {
3272                                 if (yres == 1024)
3273                                         yres = 1056;
3274                         }
3275                 }
3276
3277                 if (pVBInfo->LCDResInfo == Panel_1280x1024) {
3278                         if (yres == 400)
3279                                 yres = 405;
3280                         else if (yres == 350)
3281                                 yres = 360;
3282
3283                         if (pVBInfo->LCDInfo & XGI_LCDVESATiming) {
3284                                 if (yres == 360)
3285                                         yres = 375;
3286                         }
3287                 }
3288
3289                 if (pVBInfo->LCDResInfo == Panel_1024x768) {
3290                         if (!(pVBInfo->LCDInfo & XGI_LCDVESATiming)) {
3291                                 if (!(pVBInfo->LCDInfo & LCDNonExpanding)) {
3292                                         if (yres == 350)
3293                                                 yres = 357;
3294                                         else if (yres == 400)
3295                                                 yres = 420;
3296                                         else if (yres == 480)
3297                                                 yres = 525;
3298                                 }
3299                         }
3300                 }
3301         }
3302
3303         if (xres == 720)
3304                 xres = 640;
3305
3306 exit:
3307         pVBInfo->VGAHDE = xres;
3308         pVBInfo->HDE = xres;
3309         pVBInfo->VGAVDE = yres;
3310         pVBInfo->VDE = yres;
3311 }
3312
3313 static unsigned char XGI_IsLCDDualLink(struct vb_device_info *pVBInfo)
3314 {
3315
3316         if ((pVBInfo->VBInfo & (SetCRT2ToLCD | XGI_SetCRT2ToLCDA)) &&
3317                         (pVBInfo->LCDInfo & SetLCDDualLink)) /* shampoo0129 */
3318                 return 1;
3319
3320         return 0;
3321 }
3322
3323 static void XGI_GetRAMDAC2DATA(unsigned short ModeNo,
3324                                unsigned short ModeIdIndex,
3325                                unsigned short RefreshRateTableIndex,
3326                                struct vb_device_info *pVBInfo)
3327 {
3328         unsigned short tempax, tempbx, temp1, temp2, modeflag = 0, tempcx,
3329                         CRT1Index;
3330
3331         pVBInfo->RVBHCMAX = 1;
3332         pVBInfo->RVBHCFACT = 1;
3333         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
3334         CRT1Index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
3335         CRT1Index &= IndexMask;
3336         temp1 = (unsigned short) pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[0];
3337         temp2 = (unsigned short) pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[5];
3338         tempax = (temp1 & 0xFF) | ((temp2 & 0x03) << 8);
3339         tempbx = (unsigned short) pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[8];
3340         tempcx = (unsigned short)
3341                         pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[14] << 8;
3342         tempcx &= 0x0100;
3343         tempcx = tempcx << 2;
3344         tempbx |= tempcx;
3345         temp1 = (unsigned short) pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[9];
3346
3347         if (temp1 & 0x01)
3348                 tempbx |= 0x0100;
3349
3350         if (temp1 & 0x20)
3351                 tempbx |= 0x0200;
3352         tempax += 5;
3353
3354         if (modeflag & Charx8Dot)
3355                 tempax *= 8;
3356         else
3357                 tempax *= 9;
3358
3359         pVBInfo->VGAHT = tempax;
3360         pVBInfo->HT = tempax;
3361         tempbx++;
3362         pVBInfo->VGAVT = tempbx;
3363         pVBInfo->VT = tempbx;
3364 }
3365
3366 static void XGI_GetCRT2Data(unsigned short ModeNo, unsigned short ModeIdIndex,
3367                 unsigned short RefreshRateTableIndex,
3368                 struct vb_device_info *pVBInfo)
3369 {
3370         unsigned short tempax = 0, tempbx, modeflag, resinfo;
3371
3372         struct SiS_LCDData *LCDPtr = NULL;
3373         struct SiS_TVData *TVPtr = NULL;
3374
3375         /* si+Ext_ResInfo */
3376         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
3377         resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
3378         pVBInfo->NewFlickerMode = 0;
3379         pVBInfo->RVBHRS = 50;
3380
3381         if (pVBInfo->VBInfo & SetCRT2ToRAMDAC) {
3382                 XGI_GetRAMDAC2DATA(ModeNo, ModeIdIndex, RefreshRateTableIndex,
3383                                 pVBInfo);
3384                 return;
3385         }
3386
3387         tempbx = 4;
3388
3389         if (pVBInfo->VBInfo & (SetCRT2ToLCD | XGI_SetCRT2ToLCDA)) {
3390                 LCDPtr = (struct SiS_LCDData *) XGI_GetLcdPtr(tempbx,
3391                                 ModeNo, ModeIdIndex, RefreshRateTableIndex,
3392                                 pVBInfo);
3393
3394                 pVBInfo->RVBHCMAX = LCDPtr->RVBHCMAX;
3395                 pVBInfo->RVBHCFACT = LCDPtr->RVBHCFACT;
3396                 pVBInfo->VGAHT = LCDPtr->VGAHT;
3397                 pVBInfo->VGAVT = LCDPtr->VGAVT;
3398                 pVBInfo->HT = LCDPtr->LCDHT;
3399                 pVBInfo->VT = LCDPtr->LCDVT;
3400
3401                 if (pVBInfo->LCDResInfo == Panel_1024x768) {
3402                         tempax = 1024;
3403                         tempbx = 768;
3404
3405                         if (!(pVBInfo->LCDInfo & XGI_LCDVESATiming)) {
3406                                 if (pVBInfo->VGAVDE == 357)
3407                                         tempbx = 527;
3408                                 else if (pVBInfo->VGAVDE == 420)
3409                                         tempbx = 620;
3410                                 else if (pVBInfo->VGAVDE == 525)
3411                                         tempbx = 775;
3412                                 else if (pVBInfo->VGAVDE == 600)
3413                                         tempbx = 775;
3414                                 else
3415                                         tempbx = 768;
3416                         } else
3417                                 tempbx = 768;
3418                 } else if (pVBInfo->LCDResInfo == Panel_1024x768x75) {
3419                         tempax = 1024;
3420                         tempbx = 768;
3421                 } else if (pVBInfo->LCDResInfo == Panel_1280x1024) {
3422                         tempax = 1280;
3423                         if (pVBInfo->VGAVDE == 360)
3424                                 tempbx = 768;
3425                         else if (pVBInfo->VGAVDE == 375)
3426                                 tempbx = 800;
3427                         else if (pVBInfo->VGAVDE == 405)
3428                                 tempbx = 864;
3429                         else
3430                                 tempbx = 1024;
3431                 } else if (pVBInfo->LCDResInfo == Panel_1280x1024x75) {
3432                         tempax = 1280;
3433                         tempbx = 1024;
3434                 } else if (pVBInfo->LCDResInfo == Panel_1280x960) {
3435                         tempax = 1280;
3436                         if (pVBInfo->VGAVDE == 350)
3437                                 tempbx = 700;
3438                         else if (pVBInfo->VGAVDE == 400)
3439                                 tempbx = 800;
3440                         else if (pVBInfo->VGAVDE == 1024)
3441                                 tempbx = 960;
3442                         else
3443                                 tempbx = 960;
3444                 } else if (pVBInfo->LCDResInfo == Panel_1400x1050) {
3445                         tempax = 1400;
3446                         tempbx = 1050;
3447
3448                         if (pVBInfo->VGAVDE == 1024) {
3449                                 tempax = 1280;
3450                                 tempbx = 1024;
3451                         }
3452                 } else if (pVBInfo->LCDResInfo == Panel_1600x1200) {
3453                         tempax = 1600;
3454                         tempbx = 1200; /* alan 10/14/2003 */
3455                         if (!(pVBInfo->LCDInfo & XGI_LCDVESATiming)) {
3456                                 if (pVBInfo->VGAVDE == 350)
3457                                         tempbx = 875;
3458                                 else if (pVBInfo->VGAVDE == 400)
3459                                         tempbx = 1000;
3460                         }
3461                 }
3462
3463                 if (pVBInfo->LCDInfo & LCDNonExpanding) {
3464                         tempax = pVBInfo->VGAHDE;
3465                         tempbx = pVBInfo->VGAVDE;
3466                 }
3467
3468                 pVBInfo->HDE = tempax;
3469                 pVBInfo->VDE = tempbx;
3470                 return;
3471         }
3472
3473         if (pVBInfo->VBInfo & (SetCRT2ToTV)) {
3474                 tempbx = 4;
3475                 TVPtr = (struct SiS_TVData *) XGI_GetTVPtr(tempbx,
3476                                 ModeNo, ModeIdIndex, RefreshRateTableIndex,
3477                                 pVBInfo);
3478
3479                 pVBInfo->RVBHCMAX = TVPtr->RVBHCMAX;
3480                 pVBInfo->RVBHCFACT = TVPtr->RVBHCFACT;
3481                 pVBInfo->VGAHT = TVPtr->VGAHT;
3482                 pVBInfo->VGAVT = TVPtr->VGAVT;
3483                 pVBInfo->HDE = TVPtr->TVHDE;
3484                 pVBInfo->VDE = TVPtr->TVVDE;
3485                 pVBInfo->RVBHRS = TVPtr->RVBHRS;
3486                 pVBInfo->NewFlickerMode = TVPtr->FlickerMode;
3487
3488                 if (pVBInfo->VBInfo & SetCRT2ToHiVision) {
3489                         if (resinfo == 0x08)
3490                                 pVBInfo->NewFlickerMode = 0x40;
3491                         else if (resinfo == 0x09)
3492                                 pVBInfo->NewFlickerMode = 0x40;
3493                         else if (resinfo == 0x12)
3494                                 pVBInfo->NewFlickerMode = 0x40;
3495
3496                         if (pVBInfo->VGAVDE == 350)
3497                                 pVBInfo->TVInfo |= TVSimuMode;
3498
3499                         tempax = ExtHiTVHT;
3500                         tempbx = ExtHiTVVT;
3501
3502                         if (pVBInfo->VBInfo & SetInSlaveMode) {
3503                                 if (pVBInfo->TVInfo & TVSimuMode) {
3504                                         tempax = StHiTVHT;
3505                                         tempbx = StHiTVVT;
3506
3507                                         if (!(modeflag & Charx8Dot)) {
3508                                                 tempax = StHiTextTVHT;
3509                                                 tempbx = StHiTextTVVT;
3510                                         }
3511                                 }
3512                         }
3513                 } else if (pVBInfo->VBInfo & SetCRT2ToYPbPr525750) {
3514                         if (pVBInfo->TVInfo & TVSetYPbPr750p) {
3515                                 tempax = YPbPrTV750pHT; /* Ext750pTVHT */
3516                                 tempbx = YPbPrTV750pVT; /* Ext750pTVVT */
3517                         }
3518
3519                         if (pVBInfo->TVInfo & TVSetYPbPr525p) {
3520                                 tempax = YPbPrTV525pHT; /* Ext525pTVHT */
3521                                 tempbx = YPbPrTV525pVT; /* Ext525pTVVT */
3522                         } else if (pVBInfo->TVInfo & TVSetYPbPr525i) {
3523                                 tempax = YPbPrTV525iHT; /* Ext525iTVHT */
3524                                 tempbx = YPbPrTV525iVT; /* Ext525iTVVT */
3525                                 if (pVBInfo->TVInfo & NTSC1024x768)
3526                                         tempax = NTSC1024x768HT;
3527                         }
3528                 } else {
3529                         tempax = PALHT;
3530                         tempbx = PALVT;
3531                         if (!(pVBInfo->TVInfo & TVSetPAL)) {
3532                                 tempax = NTSCHT;
3533                                 tempbx = NTSCVT;
3534                                 if (pVBInfo->TVInfo & NTSC1024x768)
3535                                         tempax = NTSC1024x768HT;
3536                         }
3537                 }
3538
3539                 pVBInfo->HT = tempax;
3540                 pVBInfo->VT = tempbx;
3541                 return;
3542         }
3543 }
3544
3545 static void XGI_SetCRT2VCLK(unsigned short ModeNo, unsigned short ModeIdIndex,
3546                 unsigned short RefreshRateTableIndex,
3547                 struct vb_device_info *pVBInfo)
3548 {
3549         unsigned char di_0, di_1, tempal;
3550
3551         tempal = XGI_GetVCLKPtr(RefreshRateTableIndex, ModeNo, ModeIdIndex,
3552                         pVBInfo);
3553         XGI_GetVCLKLen(tempal, &di_0, &di_1, pVBInfo);
3554         XGI_GetLCDVCLKPtr(&di_0, &di_1, pVBInfo);
3555
3556         if (pVBInfo->VBType & VB_SIS301) { /* shampoo 0129 */
3557                 /* 301 */
3558                 xgifb_reg_set(pVBInfo->Part4Port, 0x0A, 0x10);
3559                 xgifb_reg_set(pVBInfo->Part4Port, 0x0B, di_1);
3560                 xgifb_reg_set(pVBInfo->Part4Port, 0x0A, di_0);
3561         } else { /* 301b/302b/301lv/302lv */
3562                 xgifb_reg_set(pVBInfo->Part4Port, 0x0A, di_0);
3563                 xgifb_reg_set(pVBInfo->Part4Port, 0x0B, di_1);
3564         }
3565
3566         xgifb_reg_set(pVBInfo->Part4Port, 0x00, 0x12);
3567
3568         if (pVBInfo->VBInfo & SetCRT2ToRAMDAC)
3569                 xgifb_reg_or(pVBInfo->Part4Port, 0x12, 0x28);
3570         else
3571                 xgifb_reg_or(pVBInfo->Part4Port, 0x12, 0x08);
3572 }
3573
3574 static unsigned short XGI_GetColorDepth(unsigned short ModeNo,
3575                 unsigned short ModeIdIndex, struct vb_device_info *pVBInfo)
3576 {
3577         unsigned short ColorDepth[6] = { 1, 2, 4, 4, 6, 8 };
3578         short index;
3579         unsigned short modeflag;
3580
3581         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
3582         index = (modeflag & ModeTypeMask) - ModeEGA;
3583
3584         if (index < 0)
3585                 index = 0;
3586
3587         return ColorDepth[index];
3588 }
3589
3590 static unsigned short XGI_GetOffset(unsigned short ModeNo,
3591                                     unsigned short ModeIdIndex,
3592                 unsigned short RefreshRateTableIndex,
3593                 struct xgi_hw_device_info *HwDeviceExtension,
3594                 struct vb_device_info *pVBInfo)
3595 {
3596         unsigned short temp, colordepth, modeinfo, index, infoflag,
3597                         ColorDepth[] = { 0x01, 0x02, 0x04 };
3598
3599         modeinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeInfo;
3600         infoflag = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
3601
3602         index = (modeinfo >> 8) & 0xFF;
3603
3604         temp = pVBInfo->ScreenOffset[index];
3605
3606         if (infoflag & InterlaceMode)
3607                 temp = temp << 1;
3608
3609         colordepth = XGI_GetColorDepth(ModeNo, ModeIdIndex, pVBInfo);
3610
3611         if ((ModeNo >= 0x7C) && (ModeNo <= 0x7E)) {
3612                 temp = ModeNo - 0x7C;
3613                 colordepth = ColorDepth[temp];
3614                 temp = 0x6B;
3615                 if (infoflag & InterlaceMode)
3616                         temp = temp << 1;
3617                 return temp * colordepth;
3618         } else {
3619                 return temp * colordepth;
3620         }
3621 }
3622
3623 static void XGI_SetCRT2Offset(unsigned short ModeNo,
3624                 unsigned short ModeIdIndex,
3625                 unsigned short RefreshRateTableIndex,
3626                 struct xgi_hw_device_info *HwDeviceExtension,
3627                 struct vb_device_info *pVBInfo)
3628 {
3629         unsigned short offset;
3630         unsigned char temp;
3631
3632         if (pVBInfo->VBInfo & SetInSlaveMode)
3633                 return;
3634
3635         offset = XGI_GetOffset(ModeNo, ModeIdIndex, RefreshRateTableIndex,
3636                         HwDeviceExtension, pVBInfo);
3637         temp = (unsigned char) (offset & 0xFF);
3638         xgifb_reg_set(pVBInfo->Part1Port, 0x07, temp);
3639         temp = (unsigned char) ((offset & 0xFF00) >> 8);
3640         xgifb_reg_set(pVBInfo->Part1Port, 0x09, temp);
3641         temp = (unsigned char) (((offset >> 3) & 0xFF) + 1);
3642         xgifb_reg_set(pVBInfo->Part1Port, 0x03, temp);
3643 }
3644
3645 static void XGI_SetCRT2FIFO(struct vb_device_info *pVBInfo)
3646 {
3647         /* threshold high ,disable auto threshold */
3648         xgifb_reg_set(pVBInfo->Part1Port, 0x01, 0x3B);
3649         /* threshold low default 04h */
3650         xgifb_reg_and_or(pVBInfo->Part1Port, 0x02, ~(0x3F), 0x04);
3651 }
3652
3653 static void XGI_PreSetGroup1(unsigned short ModeNo, unsigned short ModeIdIndex,
3654                 struct xgi_hw_device_info *HwDeviceExtension,
3655                 unsigned short RefreshRateTableIndex,
3656                 struct vb_device_info *pVBInfo)
3657 {
3658         unsigned short tempcx = 0, CRT1Index = 0, resinfo = 0;
3659
3660         CRT1Index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
3661         CRT1Index &= IndexMask;
3662         resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
3663
3664         XGI_SetCRT2Offset(ModeNo, ModeIdIndex, RefreshRateTableIndex,
3665                         HwDeviceExtension, pVBInfo);
3666         XGI_SetCRT2FIFO(pVBInfo);
3667
3668         for (tempcx = 4; tempcx < 7; tempcx++)
3669                 xgifb_reg_set(pVBInfo->Part1Port, tempcx, 0x0);
3670
3671         xgifb_reg_set(pVBInfo->Part1Port, 0x50, 0x00);
3672         xgifb_reg_set(pVBInfo->Part1Port, 0x02, 0x44); /* temp 0206 */
3673 }
3674
3675 static void XGI_SetGroup1(unsigned short ModeNo, unsigned short ModeIdIndex,
3676                 struct xgi_hw_device_info *HwDeviceExtension,
3677                 unsigned short RefreshRateTableIndex,
3678                 struct vb_device_info *pVBInfo)
3679 {
3680         unsigned short temp = 0, tempax = 0, tempbx = 0, tempcx = 0,
3681                         pushbx = 0, CRT1Index = 0, modeflag, resinfo = 0;
3682
3683         CRT1Index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
3684         CRT1Index &= IndexMask;
3685         resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
3686         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
3687
3688         /* bainy change table name */
3689         if (modeflag & HalfDCLK) {
3690                 /* BTVGA2HT 0x08,0x09 */
3691                 temp = (pVBInfo->VGAHT / 2 - 1) & 0x0FF;
3692                 xgifb_reg_set(pVBInfo->Part1Port, 0x08, temp);
3693                 temp = (((pVBInfo->VGAHT / 2 - 1) & 0xFF00) >> 8) << 4;
3694                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x09, ~0x0F0, temp);
3695                 /* BTVGA2HDEE 0x0A,0x0C */
3696                 temp = (pVBInfo->VGAHDE / 2 + 16) & 0x0FF;
3697                 xgifb_reg_set(pVBInfo->Part1Port, 0x0A, temp);
3698                 tempcx = ((pVBInfo->VGAHT - pVBInfo->VGAHDE) / 2) >> 2;
3699                 pushbx = pVBInfo->VGAHDE / 2 + 16;
3700                 tempcx = tempcx >> 1;
3701                 tempbx = pushbx + tempcx; /* bx BTVGA@HRS 0x0B,0x0C */
3702                 tempcx += tempbx;
3703
3704                 if (pVBInfo->VBInfo & SetCRT2ToRAMDAC) {
3705                         tempbx = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[4];
3706                         tempbx |= ((pVBInfo->
3707                                         XGINEWUB_CRT1Table[CRT1Index].CR[14] &
3708                                                 0xC0) << 2);
3709                         tempbx = (tempbx - 3) << 3; /* (VGAHRS-3)*8 */
3710                         tempcx = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[5];
3711                         tempcx &= 0x1F;
3712                         temp = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[15];
3713                         temp = (temp & 0x04) << (5 - 2); /* VGAHRE D[5] */
3714                         tempcx = ((tempcx | temp) - 3) << 3; /* (VGAHRE-3)*8 */
3715                 }
3716
3717                 tempbx += 4;
3718                 tempcx += 4;
3719
3720                 if (tempcx > (pVBInfo->VGAHT / 2))
3721                         tempcx = pVBInfo->VGAHT / 2;
3722
3723                 temp = tempbx & 0x00FF;
3724
3725                 xgifb_reg_set(pVBInfo->Part1Port, 0x0B, temp);
3726         } else {
3727                 temp = (pVBInfo->VGAHT - 1) & 0x0FF; /* BTVGA2HT 0x08,0x09 */
3728                 xgifb_reg_set(pVBInfo->Part1Port, 0x08, temp);
3729                 temp = (((pVBInfo->VGAHT - 1) & 0xFF00) >> 8) << 4;
3730                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x09, ~0x0F0, temp);
3731                 /* BTVGA2HDEE 0x0A,0x0C */
3732                 temp = (pVBInfo->VGAHDE + 16) & 0x0FF;
3733                 xgifb_reg_set(pVBInfo->Part1Port, 0x0A, temp);
3734                 tempcx = (pVBInfo->VGAHT - pVBInfo->VGAHDE) >> 2; /* cx */
3735                 pushbx = pVBInfo->VGAHDE + 16;
3736                 tempcx = tempcx >> 1;
3737                 tempbx = pushbx + tempcx; /* bx BTVGA@HRS 0x0B,0x0C */
3738                 tempcx += tempbx;
3739
3740                 if (pVBInfo->VBInfo & SetCRT2ToRAMDAC) {
3741                         tempbx = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[3];
3742                         tempbx |= ((pVBInfo->
3743                                         XGINEWUB_CRT1Table[CRT1Index].CR[5] &
3744                                                 0xC0) << 2);
3745                         tempbx = (tempbx - 3) << 3; /* (VGAHRS-3)*8 */
3746                         tempcx = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[4];
3747                         tempcx &= 0x1F;
3748                         temp = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[6];
3749                         temp = (temp & 0x04) << (5 - 2); /* VGAHRE D[5] */
3750                         tempcx = ((tempcx | temp) - 3) << 3; /* (VGAHRE-3)*8 */
3751                         tempbx += 16;
3752                         tempcx += 16;
3753                 }
3754
3755                 if (tempcx > pVBInfo->VGAHT)
3756                         tempcx = pVBInfo->VGAHT;
3757
3758                 temp = tempbx & 0x00FF;
3759                 xgifb_reg_set(pVBInfo->Part1Port, 0x0B, temp);
3760         }
3761
3762         tempax = (tempax & 0x00FF) | (tempbx & 0xFF00);
3763         tempbx = pushbx;
3764         tempbx = (tempbx & 0x00FF) | ((tempbx & 0xFF00) << 4);
3765         tempax |= (tempbx & 0xFF00);
3766         temp = (tempax & 0xFF00) >> 8;
3767         xgifb_reg_set(pVBInfo->Part1Port, 0x0C, temp);
3768         temp = tempcx & 0x00FF;
3769         xgifb_reg_set(pVBInfo->Part1Port, 0x0D, temp);
3770         tempcx = (pVBInfo->VGAVT - 1);
3771         temp = tempcx & 0x00FF;
3772
3773         xgifb_reg_set(pVBInfo->Part1Port, 0x0E, temp);
3774         tempbx = pVBInfo->VGAVDE - 1;
3775         temp = tempbx & 0x00FF;
3776         xgifb_reg_set(pVBInfo->Part1Port, 0x0F, temp);
3777         temp = ((tempbx & 0xFF00) << 3) >> 8;
3778         temp |= ((tempcx & 0xFF00) >> 8);
3779         xgifb_reg_set(pVBInfo->Part1Port, 0x12, temp);
3780
3781         tempax = pVBInfo->VGAVDE;
3782         tempbx = pVBInfo->VGAVDE;
3783         tempcx = pVBInfo->VGAVT;
3784         /* BTVGA2VRS 0x10,0x11 */
3785         tempbx = (pVBInfo->VGAVT + pVBInfo->VGAVDE) >> 1;
3786         /* BTVGA2VRE 0x11 */
3787         tempcx = ((pVBInfo->VGAVT - pVBInfo->VGAVDE) >> 4) + tempbx + 1;
3788
3789         if (pVBInfo->VBInfo & SetCRT2ToRAMDAC) {
3790                 tempbx = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[10];
3791                 temp = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[9];
3792
3793                 if (temp & 0x04)
3794                         tempbx |= 0x0100;
3795
3796                 if (temp & 0x080)
3797                         tempbx |= 0x0200;
3798
3799                 temp = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[14];
3800
3801                 if (temp & 0x08)
3802                         tempbx |= 0x0400;
3803
3804                 temp = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[11];
3805                 tempcx = (tempcx & 0xFF00) | (temp & 0x00FF);
3806         }
3807
3808         temp = tempbx & 0x00FF;
3809         xgifb_reg_set(pVBInfo->Part1Port, 0x10, temp);
3810         temp = ((tempbx & 0xFF00) >> 8) << 4;
3811         temp = ((tempcx & 0x000F) | (temp));
3812         xgifb_reg_set(pVBInfo->Part1Port, 0x11, temp);
3813         tempax = 0;
3814
3815         if (modeflag & DoubleScanMode)
3816                 tempax |= 0x80;
3817
3818         if (modeflag & HalfDCLK)
3819                 tempax |= 0x40;
3820
3821         xgifb_reg_and_or(pVBInfo->Part1Port, 0x2C, ~0x0C0, tempax);
3822 }
3823
3824 static unsigned short XGI_GetVGAHT2(struct vb_device_info *pVBInfo)
3825 {
3826         unsigned long tempax, tempbx;
3827
3828         tempbx = ((pVBInfo->VGAVT - pVBInfo->VGAVDE) * pVBInfo->RVBHCMAX)
3829                         & 0xFFFF;
3830         tempax = (pVBInfo->VT - pVBInfo->VDE) * pVBInfo->RVBHCFACT;
3831         tempax = (tempax * pVBInfo->HT) / tempbx;
3832
3833         return (unsigned short) tempax;
3834 }
3835
3836 static void XGI_SetLockRegs(unsigned short ModeNo, unsigned short ModeIdIndex,
3837                 struct xgi_hw_device_info *HwDeviceExtension,
3838                 unsigned short RefreshRateTableIndex,
3839                 struct vb_device_info *pVBInfo)
3840 {
3841         unsigned short push1, push2, tempax, tempbx = 0, tempcx, temp, resinfo,
3842                         modeflag, CRT1Index;
3843
3844         /* si+Ext_ResInfo */
3845         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
3846         resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
3847         CRT1Index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
3848         CRT1Index &= IndexMask;
3849
3850         if (!(pVBInfo->VBInfo & SetInSlaveMode))
3851                 return;
3852
3853         temp = 0xFF; /* set MAX HT */
3854         xgifb_reg_set(pVBInfo->Part1Port, 0x03, temp);
3855         tempcx = 0x08;
3856
3857         if (pVBInfo->VBType & (VB_SIS301LV | VB_SIS302LV | VB_XGI301C))
3858                 modeflag |= Charx8Dot;
3859
3860         tempax = pVBInfo->VGAHDE; /* 0x04 Horizontal Display End */
3861
3862         if (modeflag & HalfDCLK)
3863                 tempax = tempax >> 1;
3864
3865         tempax = (tempax / tempcx) - 1;
3866         tempbx |= ((tempax & 0x00FF) << 8);
3867         temp = tempax & 0x00FF;
3868         xgifb_reg_set(pVBInfo->Part1Port, 0x04, temp);
3869
3870         temp = (tempbx & 0xFF00) >> 8;
3871
3872         if (pVBInfo->VBInfo & SetCRT2ToTV) {
3873                 if (!(pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
3874                                 | VB_SIS302LV | VB_XGI301C)))
3875                         temp += 2;
3876
3877                 if (pVBInfo->VBInfo & SetCRT2ToHiVision) {
3878                         if (pVBInfo->VBType & VB_SIS301LV) {
3879                                 if (pVBInfo->VBExtInfo == VB_YPbPr1080i) {
3880                                         if (resinfo == 7)
3881                                                 temp -= 2;
3882                                 }
3883                         } else if (resinfo == 7) {
3884                                 temp -= 2;
3885                         }
3886                 }
3887         }
3888
3889         /* 0x05 Horizontal Display Start */
3890         xgifb_reg_set(pVBInfo->Part1Port, 0x05, temp);
3891         /* 0x06 Horizontal Blank end */
3892         xgifb_reg_set(pVBInfo->Part1Port, 0x06, 0x03);
3893
3894         if (!(pVBInfo->VBInfo & DisableCRT2Display)) { /* 030226 bainy */
3895                 if (pVBInfo->VBInfo & SetCRT2ToTV)
3896                         tempax = pVBInfo->VGAHT;
3897                 else
3898                         tempax = XGI_GetVGAHT2(pVBInfo);
3899         }
3900
3901         if (tempax >= pVBInfo->VGAHT)
3902                 tempax = pVBInfo->VGAHT;
3903
3904         if (modeflag & HalfDCLK)
3905                 tempax = tempax >> 1;
3906
3907         tempax = (tempax / tempcx) - 5;
3908         tempcx = tempax; /* 20030401 0x07 horizontal Retrace Start */
3909         if (pVBInfo->VBInfo & SetCRT2ToHiVision) {
3910                 temp = (tempbx & 0x00FF) - 1;
3911                 if (!(modeflag & HalfDCLK)) {
3912                         temp -= 6;
3913                         if (pVBInfo->TVInfo & TVSimuMode) {
3914                                 temp -= 4;
3915                                 temp -= 10;
3916                         }
3917                 }
3918         } else {
3919                 tempbx = (tempbx & 0xFF00) >> 8;
3920                 tempcx = (tempcx + tempbx) >> 1;
3921                 temp = (tempcx & 0x00FF) + 2;
3922
3923                 if (pVBInfo->VBInfo & SetCRT2ToTV) {
3924                         temp -= 1;
3925                         if (!(modeflag & HalfDCLK)) {
3926                                 if ((modeflag & Charx8Dot)) {
3927                                         temp += 4;
3928                                         if (pVBInfo->VGAHDE >= 800)
3929                                                 temp -= 6;
3930                                 }
3931                         }
3932                 } else if (!(modeflag & HalfDCLK)) {
3933                         temp -= 4;
3934                         if (pVBInfo->LCDResInfo != Panel_1280x960 &&
3935                             pVBInfo->VGAHDE >= 800) {
3936                                 temp -= 7;
3937                                 if (pVBInfo->VGAHDE >= 1280 &&
3938                                     pVBInfo->LCDResInfo != Panel_1280x960 &&
3939                                     (pVBInfo->LCDInfo & LCDNonExpanding))
3940                                         temp += 28;
3941                         }
3942                 }
3943         }
3944
3945         /* 0x07 Horizontal Retrace Start */
3946         xgifb_reg_set(pVBInfo->Part1Port, 0x07, temp);
3947         /* 0x08 Horizontal Retrace End */
3948         xgifb_reg_set(pVBInfo->Part1Port, 0x08, 0);
3949
3950         if (pVBInfo->VBInfo & SetCRT2ToTV) {
3951                 if (pVBInfo->TVInfo & TVSimuMode) {
3952                         if (ModeNo == 0x50) {
3953                                 if (pVBInfo->TVInfo & SetNTSCTV) {
3954                                         xgifb_reg_set(pVBInfo->Part1Port,
3955                                                         0x07, 0x30);
3956                                         xgifb_reg_set(pVBInfo->Part1Port,
3957                                                         0x08, 0x03);
3958                                 } else {
3959                                         xgifb_reg_set(pVBInfo->Part1Port,
3960                                                         0x07, 0x2f);
3961                                         xgifb_reg_set(pVBInfo->Part1Port,
3962                                                         0x08, 0x02);
3963                                 }
3964                         }
3965                 }
3966         }
3967
3968         xgifb_reg_set(pVBInfo->Part1Port, 0x18, 0x03); /* 0x18 SR0B */
3969         xgifb_reg_and_or(pVBInfo->Part1Port, 0x19, 0xF0, 0x00);
3970         xgifb_reg_set(pVBInfo->Part1Port, 0x09, 0xFF); /* 0x09 Set Max VT */
3971
3972         tempbx = pVBInfo->VGAVT;
3973         push1 = tempbx;
3974         tempcx = 0x121;
3975         tempbx = pVBInfo->VGAVDE; /* 0x0E Virtical Display End */
3976
3977         if (tempbx == 357)
3978                 tempbx = 350;
3979         if (tempbx == 360)
3980                 tempbx = 350;
3981         if (tempbx == 375)
3982                 tempbx = 350;
3983         if (tempbx == 405)
3984                 tempbx = 400;
3985         if (tempbx == 525)
3986                 tempbx = 480;
3987
3988         push2 = tempbx;
3989
3990         if (pVBInfo->VBInfo & SetCRT2ToLCD) {
3991                 if (pVBInfo->LCDResInfo == Panel_1024x768) {
3992                         if (!(pVBInfo->LCDInfo & XGI_LCDVESATiming)) {
3993                                 if (tempbx == 350)
3994                                         tempbx += 5;
3995                                 if (tempbx == 480)
3996                                         tempbx += 5;
3997                         }
3998                 }
3999         }
4000         tempbx--;
4001         temp = tempbx & 0x00FF;
4002         tempbx--;
4003         temp = tempbx & 0x00FF;
4004         /* 0x10 vertical Blank Start */
4005         xgifb_reg_set(pVBInfo->Part1Port, 0x10, temp);
4006         tempbx = push2;
4007         tempbx--;
4008         temp = tempbx & 0x00FF;
4009         xgifb_reg_set(pVBInfo->Part1Port, 0x0E, temp);
4010
4011         if (tempbx & 0x0100)
4012                 tempcx |= 0x0002;
4013
4014         tempax = 0x000B;
4015
4016         if (modeflag & DoubleScanMode)
4017                 tempax |= 0x08000;
4018
4019         if (tempbx & 0x0200)
4020                 tempcx |= 0x0040;
4021
4022         temp = (tempax & 0xFF00) >> 8;
4023         xgifb_reg_set(pVBInfo->Part1Port, 0x0B, temp);
4024
4025         if (tempbx & 0x0400)
4026                 tempcx |= 0x0600;
4027
4028         /* 0x11 Vertival Blank End */
4029         xgifb_reg_set(pVBInfo->Part1Port, 0x11, 0x00);
4030
4031         tempax = push1;
4032         tempax -= tempbx; /* 0x0C Vertical Retrace Start */
4033         tempax = tempax >> 2;
4034         push1 = tempax; /* push ax */
4035
4036         if (resinfo != 0x09) {
4037                 tempax = tempax << 1;
4038                 tempbx += tempax;
4039         }
4040
4041         if (pVBInfo->VBInfo & SetCRT2ToHiVision) {
4042                 if ((pVBInfo->VBType & VB_SIS301LV) &&
4043                     !(pVBInfo->TVInfo & TVSetHiVision)) {
4044                         if ((pVBInfo->TVInfo & TVSimuMode) &&
4045                             (pVBInfo->TVInfo & TVSetPAL)) {
4046                                 if (!(pVBInfo->VBType & VB_SIS301LV) ||
4047                                     !(pVBInfo->TVInfo &
4048                                       (TVSetYPbPr525p |
4049                                        TVSetYPbPr750p |
4050                                        TVSetHiVision)))
4051                                         tempbx += 40;
4052                         }
4053                 } else {
4054                         tempbx -= 10;
4055                 }
4056         } else {
4057                 if (pVBInfo->TVInfo & TVSimuMode) {
4058                         if (pVBInfo->TVInfo & TVSetPAL) {
4059                                 if (pVBInfo->VBType & VB_SIS301LV) {
4060                                         if (!(pVBInfo->TVInfo &
4061                                             (TVSetYPbPr525p |
4062                                              TVSetYPbPr750p |
4063                                              TVSetHiVision)))
4064                                                 tempbx += 40;
4065                                 } else {
4066                                         tempbx += 40;
4067                                 }
4068                         }
4069                 }
4070         }
4071         tempax = push1;
4072         tempax = tempax >> 2;
4073         tempax++;
4074         tempax += tempbx;
4075         push1 = tempax; /* push ax */
4076
4077         if ((pVBInfo->TVInfo & TVSetPAL)) {
4078                 if (tempbx <= 513) {
4079                         if (tempax >= 513)
4080                                 tempbx = 513;
4081                 }
4082         }
4083
4084         temp = tempbx & 0x00FF;
4085         xgifb_reg_set(pVBInfo->Part1Port, 0x0C, temp);
4086         tempbx--;
4087         temp = tempbx & 0x00FF;
4088         xgifb_reg_set(pVBInfo->Part1Port, 0x10, temp);
4089
4090         if (tempbx & 0x0100)
4091                 tempcx |= 0x0008;
4092
4093         if (tempbx & 0x0200)
4094                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x0B, 0x0FF, 0x20);
4095
4096         tempbx++;
4097
4098         if (tempbx & 0x0100)
4099                 tempcx |= 0x0004;
4100
4101         if (tempbx & 0x0200)
4102                 tempcx |= 0x0080;
4103
4104         if (tempbx & 0x0400)
4105                 tempcx |= 0x0C00;
4106
4107         tempbx = push1; /* pop ax */
4108         temp = tempbx & 0x00FF;
4109         temp &= 0x0F;
4110         /* 0x0D vertical Retrace End */
4111         xgifb_reg_set(pVBInfo->Part1Port, 0x0D, temp);
4112
4113         if (tempbx & 0x0010)
4114                 tempcx |= 0x2000;
4115
4116         temp = tempcx & 0x00FF;
4117         xgifb_reg_set(pVBInfo->Part1Port, 0x0A, temp); /* 0x0A CR07 */
4118         temp = (tempcx & 0x0FF00) >> 8;
4119         xgifb_reg_set(pVBInfo->Part1Port, 0x17, temp); /* 0x17 SR0A */
4120         tempax = modeflag;
4121         temp = (tempax & 0xFF00) >> 8;
4122
4123         temp = (temp >> 1) & 0x09;
4124
4125         if (pVBInfo->VBType & (VB_SIS301LV | VB_SIS302LV | VB_XGI301C))
4126                 temp |= 0x01;
4127
4128         xgifb_reg_set(pVBInfo->Part1Port, 0x16, temp); /* 0x16 SR01 */
4129         xgifb_reg_set(pVBInfo->Part1Port, 0x0F, 0); /* 0x0F CR14 */
4130         xgifb_reg_set(pVBInfo->Part1Port, 0x12, 0); /* 0x12 CR17 */
4131
4132         if (pVBInfo->LCDInfo & LCDRGB18Bit)
4133                 temp = 0x80;
4134         else
4135                 temp = 0x00;
4136
4137         xgifb_reg_set(pVBInfo->Part1Port, 0x1A, temp); /* 0x1A SR0E */
4138
4139         return;
4140 }
4141
4142 static void XGI_SetGroup2(unsigned short ModeNo, unsigned short ModeIdIndex,
4143                 unsigned short RefreshRateTableIndex,
4144                 struct xgi_hw_device_info *HwDeviceExtension,
4145                 struct vb_device_info *pVBInfo)
4146 {
4147         unsigned short i, j, tempax, tempbx, tempcx, temp, push1, push2,
4148                         modeflag, resinfo, crt2crtc;
4149         unsigned char *TimingPoint;
4150
4151         unsigned long longtemp, tempeax, tempebx, temp2, tempecx;
4152
4153         /* si+Ext_ResInfo */
4154         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
4155         resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
4156         crt2crtc = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
4157
4158         tempax = 0;
4159
4160         if (!(pVBInfo->VBInfo & SetCRT2ToAVIDEO))
4161                 tempax |= 0x0800;
4162
4163         if (!(pVBInfo->VBInfo & SetCRT2ToSVIDEO))
4164                 tempax |= 0x0400;
4165
4166         if (pVBInfo->VBInfo & SetCRT2ToSCART)
4167                 tempax |= 0x0200;
4168
4169         if (!(pVBInfo->TVInfo & TVSetPAL))
4170                 tempax |= 0x1000;
4171
4172         if (pVBInfo->VBInfo & SetCRT2ToHiVision)
4173                 tempax |= 0x0100;
4174
4175         if (pVBInfo->TVInfo & (TVSetYPbPr525p | TVSetYPbPr750p))
4176                 tempax &= 0xfe00;
4177
4178         tempax = (tempax & 0xff00) >> 8;
4179
4180         xgifb_reg_set(pVBInfo->Part2Port, 0x0, tempax);
4181         TimingPoint = pVBInfo->NTSCTiming;
4182
4183         if (pVBInfo->TVInfo & TVSetPAL)
4184                 TimingPoint = pVBInfo->PALTiming;
4185
4186         if (pVBInfo->VBInfo & SetCRT2ToHiVision) {
4187                 TimingPoint = pVBInfo->HiTVExtTiming;
4188
4189                 if (pVBInfo->VBInfo & SetInSlaveMode)
4190                         TimingPoint = pVBInfo->HiTVSt2Timing;
4191
4192                 if (pVBInfo->SetFlag & TVSimuMode)
4193                         TimingPoint = pVBInfo->HiTVSt1Timing;
4194
4195                 if (!(modeflag & Charx8Dot))
4196                         TimingPoint = pVBInfo->HiTVTextTiming;
4197         }
4198
4199         if (pVBInfo->VBInfo & SetCRT2ToYPbPr525750) {
4200                 if (pVBInfo->TVInfo & TVSetYPbPr525i)
4201                         TimingPoint = pVBInfo->YPbPr525iTiming;
4202
4203                 if (pVBInfo->TVInfo & TVSetYPbPr525p)
4204                         TimingPoint = pVBInfo->YPbPr525pTiming;
4205
4206                 if (pVBInfo->TVInfo & TVSetYPbPr750p)
4207                         TimingPoint = pVBInfo->YPbPr750pTiming;
4208         }
4209
4210         for (i = 0x01, j = 0; i <= 0x2D; i++, j++)
4211                 xgifb_reg_set(pVBInfo->Part2Port, i, TimingPoint[j]);
4212
4213         for (i = 0x39; i <= 0x45; i++, j++)
4214                 /* di->temp2[j] */
4215                 xgifb_reg_set(pVBInfo->Part2Port, i, TimingPoint[j]);
4216
4217         if (pVBInfo->VBInfo & SetCRT2ToTV)
4218                 xgifb_reg_and_or(pVBInfo->Part2Port, 0x3A, 0x1F, 0x00);
4219
4220         temp = pVBInfo->NewFlickerMode;
4221         temp &= 0x80;
4222         xgifb_reg_and_or(pVBInfo->Part2Port, 0x0A, 0xFF, temp);
4223
4224         if (pVBInfo->VBInfo & SetCRT2ToHiVision)
4225                 tempax = 950;
4226
4227         if (pVBInfo->TVInfo & TVSetPAL)
4228                 tempax = 520;
4229         else
4230                 tempax = 440;
4231
4232         if (pVBInfo->VDE <= tempax) {
4233                 tempax -= pVBInfo->VDE;
4234                 tempax = tempax >> 2;
4235                 tempax = (tempax & 0x00FF) | ((tempax & 0x00FF) << 8);
4236                 push1 = tempax;
4237                 temp = (tempax & 0xFF00) >> 8;
4238                 temp += (unsigned short) TimingPoint[0];
4239
4240                 if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
4241                                 | VB_SIS302LV | VB_XGI301C)) {
4242                         if (pVBInfo->VBInfo & (SetCRT2ToAVIDEO
4243                                         | SetCRT2ToSVIDEO | SetCRT2ToSCART
4244                                         | SetCRT2ToYPbPr525750)) {
4245                                 tempcx = pVBInfo->VGAHDE;
4246                                 if (tempcx >= 1024) {
4247                                         temp = 0x17; /* NTSC */
4248                                         if (pVBInfo->TVInfo & TVSetPAL)
4249                                                 temp = 0x19; /* PAL */
4250                                 }
4251                         }
4252                 }
4253
4254                 xgifb_reg_set(pVBInfo->Part2Port, 0x01, temp);
4255                 tempax = push1;
4256                 temp = (tempax & 0xFF00) >> 8;
4257                 temp += TimingPoint[1];
4258
4259                 if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
4260                                 | VB_SIS302LV | VB_XGI301C)) {
4261                         if ((pVBInfo->VBInfo & (SetCRT2ToAVIDEO
4262                                         | SetCRT2ToSVIDEO | SetCRT2ToSCART
4263                                         | SetCRT2ToYPbPr525750))) {
4264                                 tempcx = pVBInfo->VGAHDE;
4265                                 if (tempcx >= 1024) {
4266                                         temp = 0x1D; /* NTSC */
4267                                         if (pVBInfo->TVInfo & TVSetPAL)
4268                                                 temp = 0x52; /* PAL */
4269                                 }
4270                         }
4271                 }
4272                 xgifb_reg_set(pVBInfo->Part2Port, 0x02, temp);
4273         }
4274
4275         /* 301b */
4276         tempcx = pVBInfo->HT;
4277
4278         if (XGI_IsLCDDualLink(pVBInfo))
4279                 tempcx = tempcx >> 1;
4280
4281         tempcx -= 2;
4282         temp = tempcx & 0x00FF;
4283         xgifb_reg_set(pVBInfo->Part2Port, 0x1B, temp);
4284
4285         temp = (tempcx & 0xFF00) >> 8;
4286         xgifb_reg_and_or(pVBInfo->Part2Port, 0x1D, ~0x0F, temp);
4287
4288         tempcx = pVBInfo->HT >> 1;
4289         push1 = tempcx; /* push cx */
4290         tempcx += 7;
4291
4292         if (pVBInfo->VBInfo & SetCRT2ToHiVision)
4293                 tempcx -= 4;
4294
4295         temp = tempcx & 0x00FF;
4296         temp = temp << 4;
4297         xgifb_reg_and_or(pVBInfo->Part2Port, 0x22, 0x0F, temp);
4298
4299         tempbx = TimingPoint[j] | ((TimingPoint[j + 1]) << 8);
4300         tempbx += tempcx;
4301         push2 = tempbx;
4302         temp = tempbx & 0x00FF;
4303         xgifb_reg_set(pVBInfo->Part2Port, 0x24, temp);
4304         temp = (tempbx & 0xFF00) >> 8;
4305         temp = temp << 4;
4306         xgifb_reg_and_or(pVBInfo->Part2Port, 0x25, 0x0F, temp);
4307
4308         tempbx = push2;
4309         tempbx = tempbx + 8;
4310         if (pVBInfo->VBInfo & SetCRT2ToHiVision) {
4311                 tempbx = tempbx - 4;
4312                 tempcx = tempbx;
4313         }
4314
4315         temp = (tempbx & 0x00FF) << 4;
4316         xgifb_reg_and_or(pVBInfo->Part2Port, 0x29, 0x0F, temp);
4317
4318         j += 2;
4319         tempcx += (TimingPoint[j] | ((TimingPoint[j + 1]) << 8));
4320         temp = tempcx & 0x00FF;
4321         xgifb_reg_set(pVBInfo->Part2Port, 0x27, temp);
4322         temp = ((tempcx & 0xFF00) >> 8) << 4;
4323         xgifb_reg_and_or(pVBInfo->Part2Port, 0x28, 0x0F, temp);
4324
4325         tempcx += 8;
4326         if (pVBInfo->VBInfo & SetCRT2ToHiVision)
4327                 tempcx -= 4;
4328
4329         temp = tempcx & 0xFF;
4330         temp = temp << 4;
4331         xgifb_reg_and_or(pVBInfo->Part2Port, 0x2A, 0x0F, temp);
4332
4333         tempcx = push1; /* pop cx */
4334         j += 2;
4335         temp = TimingPoint[j] | ((TimingPoint[j + 1]) << 8);
4336         tempcx -= temp;
4337         temp = tempcx & 0x00FF;
4338         temp = temp << 4;
4339         xgifb_reg_and_or(pVBInfo->Part2Port, 0x2D, 0x0F, temp);
4340
4341         tempcx -= 11;
4342
4343         if (!(pVBInfo->VBInfo & SetCRT2ToTV)) {
4344                 tempax = XGI_GetVGAHT2(pVBInfo);
4345                 tempcx = tempax - 1;
4346         }
4347         temp = tempcx & 0x00FF;
4348         xgifb_reg_set(pVBInfo->Part2Port, 0x2E, temp);
4349
4350         tempbx = pVBInfo->VDE;
4351
4352         if (pVBInfo->VGAVDE == 360)
4353                 tempbx = 746;
4354         if (pVBInfo->VGAVDE == 375)
4355                 tempbx = 746;
4356         if (pVBInfo->VGAVDE == 405)
4357                 tempbx = 853;
4358
4359         if (pVBInfo->VBInfo & SetCRT2ToTV) {
4360                 if (pVBInfo->VBType &
4361                     (VB_SIS301LV | VB_SIS302LV | VB_XGI301C)) {
4362                         if (!(pVBInfo->TVInfo &
4363                             (TVSetYPbPr525p | TVSetYPbPr750p)))
4364                                 tempbx = tempbx >> 1;
4365                 } else
4366                         tempbx = tempbx >> 1;
4367         }
4368
4369         tempbx -= 2;
4370         temp = tempbx & 0x00FF;
4371
4372         if (pVBInfo->VBInfo & SetCRT2ToHiVision) {
4373                 if (pVBInfo->VBType & VB_SIS301LV) {
4374                         if (pVBInfo->TVInfo & TVSetHiVision) {
4375                                 if (pVBInfo->VBInfo & SetInSlaveMode) {
4376                                         if (ModeNo == 0x2f)
4377                                                 temp += 1;
4378                                 }
4379                         }
4380                 } else {
4381                         if (pVBInfo->VBInfo & SetInSlaveMode) {
4382                                 if (ModeNo == 0x2f)
4383                                         temp += 1;
4384                         }
4385                 }
4386         }
4387
4388         xgifb_reg_set(pVBInfo->Part2Port, 0x2F, temp);
4389
4390         temp = (tempcx & 0xFF00) >> 8;
4391         temp |= ((tempbx & 0xFF00) >> 8) << 6;
4392
4393         if (!(pVBInfo->VBInfo & SetCRT2ToHiVision)) {
4394                 if (pVBInfo->VBType & VB_SIS301LV) {
4395                         if (pVBInfo->TVInfo & TVSetHiVision) {
4396                                 temp |= 0x10;
4397
4398                                 if (!(pVBInfo->VBInfo & SetCRT2ToSVIDEO))
4399                                         temp |= 0x20;
4400                         }
4401                 } else {
4402                         temp |= 0x10;
4403                         if (!(pVBInfo->VBInfo & SetCRT2ToSVIDEO))
4404                                 temp |= 0x20;
4405                 }
4406         }
4407
4408         xgifb_reg_set(pVBInfo->Part2Port, 0x30, temp);
4409
4410         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
4411                         | VB_SIS302LV | VB_XGI301C)) { /* TV gatingno */
4412                 tempbx = pVBInfo->VDE;
4413                 tempcx = tempbx - 2;
4414
4415                 if (pVBInfo->VBInfo & SetCRT2ToTV) {
4416                         if (!(pVBInfo->TVInfo & (TVSetYPbPr525p
4417                                         | TVSetYPbPr750p)))
4418                                 tempbx = tempbx >> 1;
4419                 }
4420
4421                 if (pVBInfo->VBType & (VB_SIS302LV | VB_XGI301C)) {
4422                         temp = 0;
4423                         if (tempcx & 0x0400)
4424                                 temp |= 0x20;
4425
4426                         if (tempbx & 0x0400)
4427                                 temp |= 0x40;
4428
4429                         xgifb_reg_set(pVBInfo->Part4Port, 0x10, temp);
4430                 }
4431
4432                 temp = (((tempbx - 3) & 0x0300) >> 8) << 5;
4433                 xgifb_reg_set(pVBInfo->Part2Port, 0x46, temp);
4434                 temp = (tempbx - 3) & 0x00FF;
4435                 xgifb_reg_set(pVBInfo->Part2Port, 0x47, temp);
4436         }
4437
4438         tempbx = tempbx & 0x00FF;
4439
4440         if (!(modeflag & HalfDCLK)) {
4441                 tempcx = pVBInfo->VGAHDE;
4442                 if (tempcx >= pVBInfo->HDE) {
4443                         tempbx |= 0x2000;
4444                         tempax &= 0x00FF;
4445                 }
4446         }
4447
4448         tempcx = 0x0101;
4449
4450         if (pVBInfo->VBInfo & SetCRT2ToTV) { /*301b*/
4451                 if (pVBInfo->VGAHDE >= 1024) {
4452                         tempcx = 0x1920;
4453                         if (pVBInfo->VGAHDE >= 1280) {
4454                                 tempcx = 0x1420;
4455                                 tempbx = tempbx & 0xDFFF;
4456                         }
4457                 }
4458         }
4459
4460         if (!(tempbx & 0x2000)) {
4461                 if (modeflag & HalfDCLK)
4462                         tempcx = (tempcx & 0xFF00) | ((tempcx & 0x00FF) << 1);
4463
4464                 push1 = tempbx;
4465                 tempeax = pVBInfo->VGAHDE;
4466                 tempebx = (tempcx & 0xFF00) >> 8;
4467                 longtemp = tempeax * tempebx;
4468                 tempecx = tempcx & 0x00FF;
4469                 longtemp = longtemp / tempecx;
4470
4471                 /* 301b */
4472                 tempecx = 8 * 1024;
4473
4474                 if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
4475                                 | VB_SIS302LV | VB_XGI301C)) {
4476                         tempecx = tempecx * 8;
4477                 }
4478
4479                 longtemp = longtemp * tempecx;
4480                 tempecx = pVBInfo->HDE;
4481                 temp2 = longtemp % tempecx;
4482                 tempeax = longtemp / tempecx;
4483                 if (temp2 != 0)
4484                         tempeax += 1;
4485
4486                 tempax = (unsigned short) tempeax;
4487
4488                 /* 301b */
4489                 if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
4490                                 | VB_SIS302LV | VB_XGI301C)) {
4491                         tempcx = ((tempax & 0xFF00) >> 5) >> 8;
4492                 }
4493                 /* end 301b */
4494
4495                 tempbx = push1;
4496                 tempbx = (unsigned short) (((tempeax & 0x0000FF00) & 0x1F00)
4497                                 | (tempbx & 0x00FF));
4498                 tempax = (unsigned short) (((tempeax & 0x000000FF) << 8)
4499                                 | (tempax & 0x00FF));
4500                 temp = (tempax & 0xFF00) >> 8;
4501         } else {
4502                 temp = (tempax & 0x00FF) >> 8;
4503         }
4504
4505         xgifb_reg_set(pVBInfo->Part2Port, 0x44, temp);
4506         temp = (tempbx & 0xFF00) >> 8;
4507         xgifb_reg_and_or(pVBInfo->Part2Port, 0x45, ~0x03F, temp);
4508         temp = tempcx & 0x00FF;
4509
4510         if (tempbx & 0x2000)
4511                 temp = 0;
4512
4513         if (!(pVBInfo->VBInfo & SetCRT2ToLCD))
4514                 temp |= 0x18;
4515
4516         xgifb_reg_and_or(pVBInfo->Part2Port, 0x46, ~0x1F, temp);
4517         if (pVBInfo->TVInfo & TVSetPAL) {
4518                 tempbx = 0x0382;
4519                 tempcx = 0x007e;
4520         } else {
4521                 tempbx = 0x0369;
4522                 tempcx = 0x0061;
4523         }
4524
4525         temp = tempbx & 0x00FF;
4526         xgifb_reg_set(pVBInfo->Part2Port, 0x4b, temp);
4527         temp = tempcx & 0x00FF;
4528         xgifb_reg_set(pVBInfo->Part2Port, 0x4c, temp);
4529
4530         temp = ((tempcx & 0xFF00) >> 8) & 0x03;
4531         temp = temp << 2;
4532         temp |= ((tempbx & 0xFF00) >> 8) & 0x03;
4533
4534         if (pVBInfo->VBInfo & SetCRT2ToYPbPr525750) {
4535                 temp |= 0x10;
4536
4537                 if (pVBInfo->TVInfo & TVSetYPbPr525p)
4538                         temp |= 0x20;
4539
4540                 if (pVBInfo->TVInfo & TVSetYPbPr750p)
4541                         temp |= 0x60;
4542         }
4543
4544         xgifb_reg_set(pVBInfo->Part2Port, 0x4d, temp);
4545         temp = xgifb_reg_get(pVBInfo->Part2Port, 0x43); /* 301b change */
4546         xgifb_reg_set(pVBInfo->Part2Port, 0x43, (unsigned short) (temp - 3));
4547
4548         if (!(pVBInfo->TVInfo & (TVSetYPbPr525p | TVSetYPbPr750p))) {
4549                 if (pVBInfo->TVInfo & NTSC1024x768) {
4550                         TimingPoint = XGI_NTSC1024AdjTime;
4551                         for (i = 0x1c, j = 0; i <= 0x30; i++, j++) {
4552                                 xgifb_reg_set(pVBInfo->Part2Port, i,
4553                                                 TimingPoint[j]);
4554                         }
4555                         xgifb_reg_set(pVBInfo->Part2Port, 0x43, 0x72);
4556                 }
4557         }
4558
4559         /* Modify for 301C PALM Support */
4560         if (pVBInfo->VBType & VB_XGI301C) {
4561                 if (pVBInfo->TVInfo & TVSetPALM)
4562                         xgifb_reg_and_or(pVBInfo->Part2Port, 0x4E, ~0x08,
4563                                         0x08); /* PALM Mode */
4564         }
4565
4566         if (pVBInfo->TVInfo & TVSetPALM) {
4567                 tempax = (unsigned char) xgifb_reg_get(pVBInfo->Part2Port,
4568                                 0x01);
4569                 tempax--;
4570                 xgifb_reg_and(pVBInfo->Part2Port, 0x01, tempax);
4571
4572                 xgifb_reg_and(pVBInfo->Part2Port, 0x00, 0xEF);
4573         }
4574
4575         if (pVBInfo->VBInfo & SetCRT2ToHiVision) {
4576                 if (!(pVBInfo->VBInfo & SetInSlaveMode))
4577                         xgifb_reg_set(pVBInfo->Part2Port, 0x0B, 0x00);
4578         }
4579
4580         if (pVBInfo->VBInfo & SetCRT2ToTV)
4581                 return;
4582 }
4583
4584 static void XGI_SetLCDRegs(unsigned short ModeNo, unsigned short ModeIdIndex,
4585                 struct xgi_hw_device_info *HwDeviceExtension,
4586                 unsigned short RefreshRateTableIndex,
4587                 struct vb_device_info *pVBInfo)
4588 {
4589         unsigned short push1, push2, pushbx, tempax, tempbx, tempcx, temp,
4590                         tempah, tempbh, tempch, resinfo, modeflag, CRT1Index;
4591
4592         struct XGI_LCDDesStruct *LCDBDesPtr = NULL;
4593
4594         /* si+Ext_ResInfo */
4595         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
4596         resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
4597         CRT1Index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
4598         CRT1Index &= IndexMask;
4599
4600         if (!(pVBInfo->VBInfo & SetCRT2ToLCD))
4601                 return;
4602
4603         tempbx = pVBInfo->HDE; /* RHACTE=HDE-1 */
4604
4605         if (XGI_IsLCDDualLink(pVBInfo))
4606                 tempbx = tempbx >> 1;
4607
4608         tempbx -= 1;
4609         temp = tempbx & 0x00FF;
4610         xgifb_reg_set(pVBInfo->Part2Port, 0x2C, temp);
4611         temp = (tempbx & 0xFF00) >> 8;
4612         temp = temp << 4;
4613         xgifb_reg_and_or(pVBInfo->Part2Port, 0x2B, 0x0F, temp);
4614         temp = 0x01;
4615
4616         xgifb_reg_set(pVBInfo->Part2Port, 0x0B, temp);
4617         tempbx = pVBInfo->VDE; /* RTVACTEO=(VDE-1)&0xFF */
4618         push1 = tempbx;
4619         tempbx--;
4620         temp = tempbx & 0x00FF;
4621         xgifb_reg_set(pVBInfo->Part2Port, 0x03, temp);
4622         temp = ((tempbx & 0xFF00) >> 8) & 0x07;
4623         xgifb_reg_and_or(pVBInfo->Part2Port, 0x0C, ~0x07, temp);
4624
4625         tempcx = pVBInfo->VT - 1;
4626         push2 = tempcx + 1;
4627         temp = tempcx & 0x00FF; /* RVTVT=VT-1 */
4628         xgifb_reg_set(pVBInfo->Part2Port, 0x19, temp);
4629         temp = (tempcx & 0xFF00) >> 8;
4630         temp = temp << 5;
4631         xgifb_reg_set(pVBInfo->Part2Port, 0x1A, temp);
4632         xgifb_reg_and_or(pVBInfo->Part2Port, 0x09, 0xF0, 0x00);
4633         xgifb_reg_and_or(pVBInfo->Part2Port, 0x0A, 0xF0, 0x00);
4634         xgifb_reg_and_or(pVBInfo->Part2Port, 0x17, 0xFB, 0x00);
4635         xgifb_reg_and_or(pVBInfo->Part2Port, 0x18, 0xDF, 0x00);
4636
4637         /* Customized LCDB Des no add */
4638         tempbx = 5;
4639         LCDBDesPtr = (struct XGI_LCDDesStruct *) XGI_GetLcdPtr(tempbx, ModeNo,
4640                         ModeIdIndex, RefreshRateTableIndex, pVBInfo);
4641         tempah = pVBInfo->LCDResInfo;
4642         tempah &= PanelResInfo;
4643
4644         if ((tempah == Panel_1024x768) || (tempah == Panel_1024x768x75)) {
4645                 tempbx = 1024;
4646                 tempcx = 768;
4647         } else if ((tempah == Panel_1280x1024) ||
4648                    (tempah == Panel_1280x1024x75)) {
4649                 tempbx = 1280;
4650                 tempcx = 1024;
4651         } else if (tempah == Panel_1400x1050) {
4652                 tempbx = 1400;
4653                 tempcx = 1050;
4654         } else {
4655                 tempbx = 1600;
4656                 tempcx = 1200;
4657         }
4658
4659         if (pVBInfo->LCDInfo & EnableScalingLCD) {
4660                 tempbx = pVBInfo->HDE;
4661                 tempcx = pVBInfo->VDE;
4662         }
4663
4664         pushbx = tempbx;
4665         tempax = pVBInfo->VT;
4666         pVBInfo->LCDHDES = LCDBDesPtr->LCDHDES;
4667         pVBInfo->LCDHRS = LCDBDesPtr->LCDHRS;
4668         pVBInfo->LCDVDES = LCDBDesPtr->LCDVDES;
4669         pVBInfo->LCDVRS = LCDBDesPtr->LCDVRS;
4670         tempbx = pVBInfo->LCDVDES;
4671         tempcx += tempbx;
4672
4673         if (tempcx >= tempax)
4674                 tempcx -= tempax; /* lcdvdes */
4675
4676         temp = tempbx & 0x00FF; /* RVEQ1EQ=lcdvdes */
4677         xgifb_reg_set(pVBInfo->Part2Port, 0x05, temp);
4678         temp = tempcx & 0x00FF;
4679         xgifb_reg_set(pVBInfo->Part2Port, 0x06, temp);
4680         tempch = ((tempcx & 0xFF00) >> 8) & 0x07;
4681         tempbh = ((tempbx & 0xFF00) >> 8) & 0x07;
4682         tempah = tempch;
4683         tempah = tempah << 3;
4684         tempah |= tempbh;
4685         xgifb_reg_set(pVBInfo->Part2Port, 0x02, tempah);
4686
4687         /* getlcdsync() */
4688         XGI_GetLCDSync(&tempax, &tempbx, pVBInfo);
4689         tempcx = tempbx;
4690         tempax = pVBInfo->VT;
4691         tempbx = pVBInfo->LCDVRS;
4692
4693         tempcx += tempbx;
4694         if (tempcx >= tempax)
4695                 tempcx -= tempax;
4696
4697         temp = tempbx & 0x00FF; /* RTVACTEE=lcdvrs */
4698         xgifb_reg_set(pVBInfo->Part2Port, 0x04, temp);
4699         temp = (tempbx & 0xFF00) >> 8;
4700         temp = temp << 4;
4701         temp |= (tempcx & 0x000F);
4702         xgifb_reg_set(pVBInfo->Part2Port, 0x01, temp);
4703         tempcx = pushbx;
4704         tempax = pVBInfo->HT;
4705         tempbx = pVBInfo->LCDHDES;
4706         tempbx &= 0x0FFF;
4707
4708         if (XGI_IsLCDDualLink(pVBInfo)) {
4709                 tempax = tempax >> 1;
4710                 tempbx = tempbx >> 1;
4711                 tempcx = tempcx >> 1;
4712         }
4713
4714         if (pVBInfo->VBType & VB_SIS302LV)
4715                 tempbx += 1;
4716
4717         if (pVBInfo->VBType & VB_XGI301C) /* tap4 */
4718                 tempbx += 1;
4719
4720         tempcx += tempbx;
4721
4722         if (tempcx >= tempax)
4723                 tempcx -= tempax;
4724
4725         temp = tempbx & 0x00FF;
4726         xgifb_reg_set(pVBInfo->Part2Port, 0x1F, temp); /* RHBLKE=lcdhdes */
4727         temp = ((tempbx & 0xFF00) >> 8) << 4;
4728         xgifb_reg_set(pVBInfo->Part2Port, 0x20, temp);
4729         temp = tempcx & 0x00FF;
4730         xgifb_reg_set(pVBInfo->Part2Port, 0x23, temp); /* RHEQPLE=lcdhdee */
4731         temp = (tempcx & 0xFF00) >> 8;
4732         xgifb_reg_set(pVBInfo->Part2Port, 0x25, temp);
4733
4734         XGI_GetLCDSync(&tempax, &tempbx, pVBInfo);
4735         tempcx = tempax;
4736         tempax = pVBInfo->HT;
4737         tempbx = pVBInfo->LCDHRS;
4738         if (XGI_IsLCDDualLink(pVBInfo)) {
4739                 tempax = tempax >> 1;
4740                 tempbx = tempbx >> 1;
4741                 tempcx = tempcx >> 1;
4742         }
4743
4744         if (pVBInfo->VBType & VB_SIS302LV)
4745                 tempbx += 1;
4746
4747         tempcx += tempbx;
4748
4749         if (tempcx >= tempax)
4750                 tempcx -= tempax;
4751
4752         temp = tempbx & 0x00FF; /* RHBURSTS=lcdhrs */
4753         xgifb_reg_set(pVBInfo->Part2Port, 0x1C, temp);
4754
4755         temp = (tempbx & 0xFF00) >> 8;
4756         temp = temp << 4;
4757         xgifb_reg_and_or(pVBInfo->Part2Port, 0x1D, ~0x0F0, temp);
4758         temp = tempcx & 0x00FF; /* RHSYEXP2S=lcdhre */
4759         xgifb_reg_set(pVBInfo->Part2Port, 0x21, temp);
4760
4761         if (!(pVBInfo->LCDInfo & XGI_LCDVESATiming)) {
4762                 if (pVBInfo->VGAVDE == 525) {
4763                         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B
4764                                         | VB_SIS301LV | VB_SIS302LV
4765                                         | VB_XGI301C)) {
4766                                 temp = 0xC6;
4767                         } else
4768                                 temp = 0xC4;
4769
4770                         xgifb_reg_set(pVBInfo->Part2Port, 0x2f, temp);
4771                         xgifb_reg_set(pVBInfo->Part2Port, 0x30, 0xB3);
4772                 }
4773
4774                 if (pVBInfo->VGAVDE == 420) {
4775                         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B
4776                                         | VB_SIS301LV | VB_SIS302LV
4777                                         | VB_XGI301C)) {
4778                                 temp = 0x4F;
4779                         } else
4780                                 temp = 0x4E;
4781                         xgifb_reg_set(pVBInfo->Part2Port, 0x2f, temp);
4782                 }
4783         }
4784 }
4785
4786 /* --------------------------------------------------------------------- */
4787 /* Function : XGI_GetTap4Ptr */
4788 /* Input : */
4789 /* Output : di -> Tap4 Reg. Setting Pointer */
4790 /* Description : */
4791 /* --------------------------------------------------------------------- */
4792 static struct XGI301C_Tap4TimingStruct *XGI_GetTap4Ptr(unsigned short tempcx,
4793                 struct vb_device_info *pVBInfo)
4794 {
4795         unsigned short tempax, tempbx, i;
4796
4797         struct XGI301C_Tap4TimingStruct *Tap4TimingPtr;
4798
4799         if (tempcx == 0) {
4800                 tempax = pVBInfo->VGAHDE;
4801                 tempbx = pVBInfo->HDE;
4802         } else {
4803                 tempax = pVBInfo->VGAVDE;
4804                 tempbx = pVBInfo->VDE;
4805         }
4806
4807         if (tempax <= tempbx)
4808                 return &xgifb_tap4_timing[0];
4809         else
4810                 Tap4TimingPtr = xgifb_ntsc_525_tap4_timing; /* NTSC */
4811
4812         if (pVBInfo->TVInfo & TVSetPAL)
4813                 Tap4TimingPtr = PALTap4Timing;
4814
4815         if (pVBInfo->VBInfo & SetCRT2ToYPbPr525750) {
4816                 if ((pVBInfo->TVInfo & TVSetYPbPr525i) ||
4817                         (pVBInfo->TVInfo & TVSetYPbPr525p))
4818                         Tap4TimingPtr = xgifb_ntsc_525_tap4_timing;
4819                 if (pVBInfo->TVInfo & TVSetYPbPr750p)
4820                         Tap4TimingPtr = YPbPr750pTap4Timing;
4821         }
4822
4823         if (pVBInfo->VBInfo & SetCRT2ToHiVision)
4824                 Tap4TimingPtr = xgifb_tap4_timing;
4825
4826         i = 0;
4827         while (Tap4TimingPtr[i].DE != 0xFFFF) {
4828                 if (Tap4TimingPtr[i].DE == tempax)
4829                         break;
4830                 i++;
4831         }
4832         return &Tap4TimingPtr[i];
4833 }
4834
4835 static void XGI_SetTap4Regs(struct vb_device_info *pVBInfo)
4836 {
4837         unsigned short i, j;
4838
4839         struct XGI301C_Tap4TimingStruct *Tap4TimingPtr;
4840
4841         if (!(pVBInfo->VBType & VB_XGI301C))
4842                 return;
4843
4844         Tap4TimingPtr = XGI_GetTap4Ptr(0, pVBInfo); /* Set Horizontal Scaling */
4845         for (i = 0x80, j = 0; i <= 0xBF; i++, j++)
4846                 xgifb_reg_set(pVBInfo->Part2Port, i, Tap4TimingPtr->Reg[j]);
4847
4848         if ((pVBInfo->VBInfo & SetCRT2ToTV) &&
4849             (!(pVBInfo->VBInfo & SetCRT2ToHiVision))) {
4850                 /* Set Vertical Scaling */
4851                 Tap4TimingPtr = XGI_GetTap4Ptr(1, pVBInfo);
4852                 for (i = 0xC0, j = 0; i < 0xFF; i++, j++)
4853                         xgifb_reg_set(pVBInfo->Part2Port,
4854                                       i,
4855                                       Tap4TimingPtr->Reg[j]);
4856         }
4857
4858         if ((pVBInfo->VBInfo & SetCRT2ToTV) &&
4859             (!(pVBInfo->VBInfo & SetCRT2ToHiVision)))
4860                 /* Enable V.Scaling */
4861                 xgifb_reg_and_or(pVBInfo->Part2Port, 0x4E, ~0x14, 0x04);
4862         else
4863                 /* Enable H.Scaling */
4864                 xgifb_reg_and_or(pVBInfo->Part2Port, 0x4E, ~0x14, 0x10);
4865 }
4866
4867 static void XGI_SetGroup3(unsigned short ModeNo, unsigned short ModeIdIndex,
4868                 struct vb_device_info *pVBInfo)
4869 {
4870         unsigned short i;
4871         unsigned char *tempdi;
4872         unsigned short modeflag;
4873
4874         /* si+Ext_ResInfo */
4875         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
4876
4877         xgifb_reg_set(pVBInfo->Part3Port, 0x00, 0x00);
4878         if (pVBInfo->TVInfo & TVSetPAL) {
4879                 xgifb_reg_set(pVBInfo->Part3Port, 0x13, 0xFA);
4880                 xgifb_reg_set(pVBInfo->Part3Port, 0x14, 0xC8);
4881         } else {
4882                 xgifb_reg_set(pVBInfo->Part3Port, 0x13, 0xF5);
4883                 xgifb_reg_set(pVBInfo->Part3Port, 0x14, 0xB7);
4884         }
4885
4886         if (!(pVBInfo->VBInfo & SetCRT2ToTV))
4887                 return;
4888
4889         if (pVBInfo->TVInfo & TVSetPALM) {
4890                 xgifb_reg_set(pVBInfo->Part3Port, 0x13, 0xFA);
4891                 xgifb_reg_set(pVBInfo->Part3Port, 0x14, 0xC8);
4892                 xgifb_reg_set(pVBInfo->Part3Port, 0x3D, 0xA8);
4893         }
4894
4895         if ((pVBInfo->VBInfo & SetCRT2ToHiVision) || (pVBInfo->VBInfo
4896                         & SetCRT2ToYPbPr525750)) {
4897                 if (pVBInfo->TVInfo & TVSetYPbPr525i)
4898                         return;
4899
4900                 tempdi = pVBInfo->HiTVGroup3Data;
4901                 if (pVBInfo->SetFlag & TVSimuMode) {
4902                         tempdi = pVBInfo->HiTVGroup3Simu;
4903                         if (!(modeflag & Charx8Dot))
4904                                 tempdi = pVBInfo->HiTVGroup3Text;
4905                 }
4906
4907                 if (pVBInfo->TVInfo & TVSetYPbPr525p)
4908                         tempdi = pVBInfo->Ren525pGroup3;
4909
4910                 if (pVBInfo->TVInfo & TVSetYPbPr750p)
4911                         tempdi = pVBInfo->Ren750pGroup3;
4912
4913                 for (i = 0; i <= 0x3E; i++)
4914                         xgifb_reg_set(pVBInfo->Part3Port, i, tempdi[i]);
4915
4916                 if (pVBInfo->VBType & VB_XGI301C) { /* Marcovision */
4917                         if (pVBInfo->TVInfo & TVSetYPbPr525p)
4918                                 xgifb_reg_set(pVBInfo->Part3Port, 0x28, 0x3f);
4919                 }
4920         }
4921         return;
4922 } /* {end of XGI_SetGroup3} */
4923
4924 static void XGI_SetGroup4(unsigned short ModeNo, unsigned short ModeIdIndex,
4925                 unsigned short RefreshRateTableIndex,
4926                 struct xgi_hw_device_info *HwDeviceExtension,
4927                 struct vb_device_info *pVBInfo)
4928 {
4929         unsigned short tempax, tempcx, tempbx, modeflag, temp, temp2;
4930
4931         unsigned long tempebx, tempeax, templong;
4932
4933         /* si+Ext_ResInfo */
4934         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
4935         temp = pVBInfo->RVBHCFACT;
4936         xgifb_reg_set(pVBInfo->Part4Port, 0x13, temp);
4937
4938         tempbx = pVBInfo->RVBHCMAX;
4939         temp = tempbx & 0x00FF;
4940         xgifb_reg_set(pVBInfo->Part4Port, 0x14, temp);
4941         temp2 = ((tempbx & 0xFF00) >> 8) << 7;
4942         tempcx = pVBInfo->VGAHT - 1;
4943         temp = tempcx & 0x00FF;
4944         xgifb_reg_set(pVBInfo->Part4Port, 0x16, temp);
4945
4946         temp = ((tempcx & 0xFF00) >> 8) << 3;
4947         temp2 |= temp;
4948
4949         tempcx = pVBInfo->VGAVT - 1;
4950         if (!(pVBInfo->VBInfo & SetCRT2ToTV))
4951                 tempcx -= 5;
4952
4953         temp = tempcx & 0x00FF;
4954         xgifb_reg_set(pVBInfo->Part4Port, 0x17, temp);
4955         temp = temp2 | ((tempcx & 0xFF00) >> 8);
4956         xgifb_reg_set(pVBInfo->Part4Port, 0x15, temp);
4957         xgifb_reg_or(pVBInfo->Part4Port, 0x0D, 0x08);
4958         tempcx = pVBInfo->VBInfo;
4959         tempbx = pVBInfo->VGAHDE;
4960
4961         if (modeflag & HalfDCLK)
4962                 tempbx = tempbx >> 1;
4963
4964         if (XGI_IsLCDDualLink(pVBInfo))
4965                 tempbx = tempbx >> 1;
4966
4967         if (tempcx & SetCRT2ToHiVision) {
4968                 temp = 0;
4969                 if (tempbx <= 1024)
4970                         temp = 0xA0;
4971                 if (tempbx == 1280)
4972                         temp = 0xC0;
4973         } else if (tempcx & SetCRT2ToTV) {
4974                 temp = 0xA0;
4975                 if (tempbx <= 800)
4976                         temp = 0x80;
4977         } else {
4978                 temp = 0x80;
4979                 if (pVBInfo->VBInfo & SetCRT2ToLCD) {
4980                         temp = 0;
4981                         if (tempbx > 800)
4982                                 temp = 0x60;
4983                 }
4984         }
4985
4986         if (pVBInfo->TVInfo & (TVSetYPbPr525p | TVSetYPbPr750p)) {
4987                 temp = 0x00;
4988                 if (pVBInfo->VGAHDE == 1280)
4989                         temp = 0x40;
4990                 if (pVBInfo->VGAHDE == 1024)
4991                         temp = 0x20;
4992         }
4993         xgifb_reg_and_or(pVBInfo->Part4Port, 0x0E, ~0xEF, temp);
4994
4995         tempebx = pVBInfo->VDE;
4996
4997         if (tempcx & SetCRT2ToHiVision) {
4998                 if (!(temp & 0xE000))
4999                         tempbx = tempbx >> 1;
5000         }
5001
5002         tempcx = pVBInfo->RVBHRS;
5003         temp = tempcx & 0x00FF;
5004         xgifb_reg_set(pVBInfo->Part4Port, 0x18, temp);
5005
5006         tempeax = pVBInfo->VGAVDE;
5007         tempcx |= 0x04000;
5008
5009         if (tempeax <= tempebx) {
5010                 tempcx = (tempcx & (~0x4000));
5011                 tempeax = pVBInfo->VGAVDE;
5012         } else {
5013                 tempeax -= tempebx;
5014         }
5015
5016         templong = (tempeax * 256 * 1024) % tempebx;
5017         tempeax = (tempeax * 256 * 1024) / tempebx;
5018         tempebx = tempeax;
5019
5020         if (templong != 0)
5021                 tempebx++;
5022
5023         temp = (unsigned short) (tempebx & 0x000000FF);
5024         xgifb_reg_set(pVBInfo->Part4Port, 0x1B, temp);
5025
5026         temp = (unsigned short) ((tempebx & 0x0000FF00) >> 8);
5027         xgifb_reg_set(pVBInfo->Part4Port, 0x1A, temp);
5028         tempbx = (unsigned short) (tempebx >> 16);
5029         temp = tempbx & 0x00FF;
5030         temp = temp << 4;
5031         temp |= ((tempcx & 0xFF00) >> 8);
5032         xgifb_reg_set(pVBInfo->Part4Port, 0x19, temp);
5033
5034         /* 301b */
5035         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
5036                         | VB_SIS302LV | VB_XGI301C)) {
5037                 temp = 0x0028;
5038                 xgifb_reg_set(pVBInfo->Part4Port, 0x1C, temp);
5039                 tempax = pVBInfo->VGAHDE;
5040                 if (modeflag & HalfDCLK)
5041                         tempax = tempax >> 1;
5042
5043                 if (XGI_IsLCDDualLink(pVBInfo))
5044                         tempax = tempax >> 1;
5045
5046                 if (pVBInfo->VBInfo & SetCRT2ToLCD) {
5047                         if (tempax > 800)
5048                                 tempax -= 800;
5049                 } else {
5050                         if (pVBInfo->VGAHDE > 800) {
5051                                 if (pVBInfo->VGAHDE == 1024)
5052                                         tempax = (tempax * 25 / 32) - 1;
5053                                 else
5054                                         tempax = (tempax * 20 / 32) - 1;
5055                         }
5056                 }
5057                 tempax -= 1;
5058
5059                 temp = (tempax & 0xFF00) >> 8;
5060                 temp = ((temp & 0x0003) << 4);
5061                 xgifb_reg_set(pVBInfo->Part4Port, 0x1E, temp);
5062                 temp = (tempax & 0x00FF);
5063                 xgifb_reg_set(pVBInfo->Part4Port, 0x1D, temp);
5064
5065                 if (pVBInfo->VBInfo & (SetCRT2ToTV | SetCRT2ToHiVision)) {
5066                         if (pVBInfo->VGAHDE > 800)
5067                                 xgifb_reg_or(pVBInfo->Part4Port, 0x1E, 0x08);
5068
5069                 }
5070                 temp = 0x0036;
5071
5072                 if (pVBInfo->VBInfo & SetCRT2ToTV) {
5073                         if (!(pVBInfo->TVInfo & (NTSC1024x768
5074                                         | TVSetYPbPr525p | TVSetYPbPr750p
5075                                         | TVSetHiVision))) {
5076                                 temp |= 0x0001;
5077                                 if ((pVBInfo->VBInfo & SetInSlaveMode)
5078                                                 && (!(pVBInfo->TVInfo
5079                                                                 & TVSimuMode)))
5080                                         temp &= (~0x0001);
5081                         }
5082                 }
5083
5084                 xgifb_reg_and_or(pVBInfo->Part4Port, 0x1F, 0x00C0, temp);
5085                 tempbx = pVBInfo->HT;
5086                 if (XGI_IsLCDDualLink(pVBInfo))
5087                         tempbx = tempbx >> 1;
5088                 tempbx = (tempbx >> 1) - 2;
5089                 temp = ((tempbx & 0x0700) >> 8) << 3;
5090                 xgifb_reg_and_or(pVBInfo->Part4Port, 0x21, 0x00C0, temp);
5091                 temp = tempbx & 0x00FF;
5092                 xgifb_reg_set(pVBInfo->Part4Port, 0x22, temp);
5093         }
5094         /* end 301b */
5095
5096         if (pVBInfo->ISXPDOS == 0)
5097                 XGI_SetCRT2VCLK(ModeNo, ModeIdIndex, RefreshRateTableIndex,
5098                                 pVBInfo);
5099 }
5100
5101 static void XGINew_EnableCRT2(struct vb_device_info *pVBInfo)
5102 {
5103         xgifb_reg_and_or(pVBInfo->P3c4, 0x1E, 0xFF, 0x20);
5104 }
5105
5106 static void XGI_SetGroup5(unsigned short ModeNo, unsigned short ModeIdIndex,
5107                 struct vb_device_info *pVBInfo)
5108 {
5109         unsigned short Pindex, Pdata;
5110
5111         Pindex = pVBInfo->Part5Port;
5112         Pdata = pVBInfo->Part5Port + 1;
5113         if (pVBInfo->ModeType == ModeVGA) {
5114                 if (!(pVBInfo->VBInfo & (SetInSlaveMode | LoadDACFlag
5115                                 | DisableCRT2Display))) {
5116                         XGINew_EnableCRT2(pVBInfo);
5117                 }
5118         }
5119         return;
5120 }
5121
5122 static void XGI_EnableGatingCRT(struct xgi_hw_device_info *HwDeviceExtension,
5123                 struct vb_device_info *pVBInfo)
5124 {
5125         xgifb_reg_and_or(pVBInfo->P3d4, 0x63, 0xBF, 0x40);
5126 }
5127
5128 static void XGI_DisableGatingCRT(struct xgi_hw_device_info *HwDeviceExtension,
5129                 struct vb_device_info *pVBInfo)
5130 {
5131
5132         xgifb_reg_and_or(pVBInfo->P3d4, 0x63, 0xBF, 0x00);
5133 }
5134
5135 static unsigned char XGI_XG21CheckLVDSMode(struct xgifb_video_info *xgifb_info,
5136                 unsigned short ModeNo, unsigned short ModeIdIndex,
5137                 struct vb_device_info *pVBInfo)
5138 {
5139         unsigned short xres, yres, colordepth, modeflag, resindex;
5140
5141         resindex = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
5142         xres = pVBInfo->ModeResInfo[resindex].HTotal; /* xres->ax */
5143         yres = pVBInfo->ModeResInfo[resindex].VTotal; /* yres->bx */
5144         /* si+St_ModeFlag */
5145         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
5146
5147         if (!(modeflag & Charx8Dot)) {
5148                 xres /= 9;
5149                 xres *= 8;
5150         }
5151
5152         if ((ModeNo > 0x13) && (modeflag & HalfDCLK))
5153                 xres *= 2;
5154
5155         if ((ModeNo > 0x13) && (modeflag & DoubleScanMode))
5156                 yres *= 2;
5157
5158         if (xres > xgifb_info->lvds_data.LVDSHDE)
5159                 return 0;
5160
5161         if (yres > xgifb_info->lvds_data.LVDSVDE)
5162                 return 0;
5163
5164         if (xres != xgifb_info->lvds_data.LVDSHDE ||
5165             yres != xgifb_info->lvds_data.LVDSVDE) {
5166                 colordepth = XGI_GetColorDepth(ModeNo, ModeIdIndex, pVBInfo);
5167                 if (colordepth > 2)
5168                         return 0;
5169         }
5170         return 1;
5171 }
5172
5173 static void xgifb_set_lvds(struct xgifb_video_info *xgifb_info,
5174                            int chip_id,
5175                            unsigned short ModeNo,
5176                            unsigned short ModeIdIndex,
5177                            struct vb_device_info *pVBInfo)
5178 {
5179         unsigned char temp, Miscdata;
5180         unsigned short xres, yres, modeflag, resindex;
5181         unsigned short LVDSHT, LVDSHBS, LVDSHRS, LVDSHRE, LVDSHBE;
5182         unsigned short LVDSVT, LVDSVBS, LVDSVRS, LVDSVRE, LVDSVBE;
5183         unsigned short value;
5184
5185         temp = (unsigned char) ((xgifb_info->lvds_data.LVDS_Capability &
5186                                 (LCDPolarity << 8)) >> 8);
5187         temp &= LCDPolarity;
5188         Miscdata = (unsigned char) inb(pVBInfo->P3cc);
5189
5190         outb((Miscdata & 0x3F) | temp, pVBInfo->P3c2);
5191
5192         temp = xgifb_info->lvds_data.LVDS_Capability & LCDPolarity;
5193         /* SR35[7] FP VSync polarity */
5194         xgifb_reg_and_or(pVBInfo->P3c4, 0x35, ~0x80, temp & 0x80);
5195         /* SR30[5] FP HSync polarity */
5196         xgifb_reg_and_or(pVBInfo->P3c4, 0x30, ~0x20, (temp & 0x40) >> 1);
5197
5198         if (chip_id == XG27)
5199                 XGI_SetXG27FPBits(pVBInfo);
5200         else
5201                 XGI_SetXG21FPBits(pVBInfo);
5202
5203         resindex = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
5204         xres = pVBInfo->ModeResInfo[resindex].HTotal; /* xres->ax */
5205         yres = pVBInfo->ModeResInfo[resindex].VTotal; /* yres->bx */
5206         /* si+St_ModeFlag */
5207         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
5208
5209         if (!(modeflag & Charx8Dot))
5210                 xres = xres * 8 / 9;
5211
5212         LVDSHT = xgifb_info->lvds_data.LVDSHT;
5213
5214         LVDSHBS = xres + (xgifb_info->lvds_data.LVDSHDE - xres) / 2;
5215
5216         if (LVDSHBS > LVDSHT)
5217                 LVDSHBS -= LVDSHT;
5218
5219         LVDSHRS = LVDSHBS + xgifb_info->lvds_data.LVDSHFP;
5220         if (LVDSHRS > LVDSHT)
5221                 LVDSHRS -= LVDSHT;
5222
5223         LVDSHRE = LVDSHRS + xgifb_info->lvds_data.LVDSHSYNC;
5224         if (LVDSHRE > LVDSHT)
5225                 LVDSHRE -= LVDSHT;
5226
5227         LVDSHBE = LVDSHBS + LVDSHT - xgifb_info->lvds_data.LVDSHDE;
5228
5229         LVDSVT = xgifb_info->lvds_data.LVDSVT;
5230
5231         LVDSVBS = yres + (xgifb_info->lvds_data.LVDSVDE - yres) / 2;
5232         if (modeflag & DoubleScanMode)
5233                 LVDSVBS += yres / 2;
5234
5235         if (LVDSVBS > LVDSVT)
5236                 LVDSVBS -= LVDSVT;
5237
5238         LVDSVRS = LVDSVBS + xgifb_info->lvds_data.LVDSVFP;
5239         if (LVDSVRS > LVDSVT)
5240                 LVDSVRS -= LVDSVT;
5241
5242         LVDSVRE = LVDSVRS + xgifb_info->lvds_data.LVDSVSYNC;
5243         if (LVDSVRE > LVDSVT)
5244                 LVDSVRE -= LVDSVT;
5245
5246         LVDSVBE = LVDSVBS + LVDSVT - xgifb_info->lvds_data.LVDSVDE;
5247
5248         temp = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x11);
5249         xgifb_reg_set(pVBInfo->P3d4, 0x11, temp & 0x7f); /* Unlock CRTC */
5250
5251         if (!(modeflag & Charx8Dot))
5252                 xgifb_reg_or(pVBInfo->P3c4, 0x1, 0x1);
5253
5254         /* HT SR0B[1:0] CR00 */
5255         value = (LVDSHT >> 3) - 5;
5256         xgifb_reg_and_or(pVBInfo->P3c4, 0x0B, ~0x03, (value & 0x300) >> 8);
5257         xgifb_reg_set(pVBInfo->P3d4, 0x0, (value & 0xFF));
5258
5259         /* HBS SR0B[5:4] CR02 */
5260         value = (LVDSHBS >> 3) - 1;
5261         xgifb_reg_and_or(pVBInfo->P3c4, 0x0B, ~0x30, (value & 0x300) >> 4);
5262         xgifb_reg_set(pVBInfo->P3d4, 0x2, (value & 0xFF));
5263
5264         /* HBE SR0C[1:0] CR05[7] CR03[4:0] */
5265         value = (LVDSHBE >> 3) - 1;
5266         xgifb_reg_and_or(pVBInfo->P3c4, 0x0C, ~0x03, (value & 0xC0) >> 6);
5267         xgifb_reg_and_or(pVBInfo->P3d4, 0x05, ~0x80, (value & 0x20) << 2);
5268         xgifb_reg_and_or(pVBInfo->P3d4, 0x03, ~0x1F, value & 0x1F);
5269
5270         /* HRS SR0B[7:6] CR04 */
5271         value = (LVDSHRS >> 3) + 2;
5272         xgifb_reg_and_or(pVBInfo->P3c4, 0x0B, ~0xC0, (value & 0x300) >> 2);
5273         xgifb_reg_set(pVBInfo->P3d4, 0x4, (value & 0xFF));
5274
5275         /* Panel HRS SR2F[1:0] SR2E[7:0]  */
5276         value--;
5277         xgifb_reg_and_or(pVBInfo->P3c4, 0x2F, ~0x03, (value & 0x300) >> 8);
5278         xgifb_reg_set(pVBInfo->P3c4, 0x2E, (value & 0xFF));
5279
5280         /* HRE SR0C[2] CR05[4:0] */
5281         value = (LVDSHRE >> 3) + 2;
5282         xgifb_reg_and_or(pVBInfo->P3c4, 0x0C, ~0x04, (value & 0x20) >> 3);
5283         xgifb_reg_and_or(pVBInfo->P3d4, 0x05, ~0x1F, value & 0x1F);
5284
5285         /* Panel HRE SR2F[7:2]  */
5286         value--;
5287         xgifb_reg_and_or(pVBInfo->P3c4, 0x2F, ~0xFC, value << 2);
5288
5289         /* VT SR0A[0] CR07[5][0] CR06 */
5290         value = LVDSVT - 2;
5291         xgifb_reg_and_or(pVBInfo->P3c4, 0x0A, ~0x01, (value & 0x400) >> 10);
5292         xgifb_reg_and_or(pVBInfo->P3d4, 0x07, ~0x20, (value & 0x200) >> 4);
5293         xgifb_reg_and_or(pVBInfo->P3d4, 0x07, ~0x01, (value & 0x100) >> 8);
5294         xgifb_reg_set(pVBInfo->P3d4, 0x06, (value & 0xFF));
5295
5296         /* VBS SR0A[2] CR09[5] CR07[3] CR15 */
5297         value = LVDSVBS - 1;
5298         xgifb_reg_and_or(pVBInfo->P3c4, 0x0A, ~0x04, (value & 0x400) >> 8);
5299         xgifb_reg_and_or(pVBInfo->P3d4, 0x09, ~0x20, (value & 0x200) >> 4);
5300         xgifb_reg_and_or(pVBInfo->P3d4, 0x07, ~0x08, (value & 0x100) >> 5);
5301         xgifb_reg_set(pVBInfo->P3d4, 0x15, (value & 0xFF));
5302
5303         /* VBE SR0A[4] CR16 */
5304         value = LVDSVBE - 1;
5305         xgifb_reg_and_or(pVBInfo->P3c4, 0x0A, ~0x10, (value & 0x100) >> 4);
5306         xgifb_reg_set(pVBInfo->P3d4, 0x16, (value & 0xFF));
5307
5308         /* VRS SR0A[3] CR7[7][2] CR10 */
5309         value = LVDSVRS - 1;
5310         xgifb_reg_and_or(pVBInfo->P3c4, 0x0A, ~0x08, (value & 0x400) >> 7);
5311         xgifb_reg_and_or(pVBInfo->P3d4, 0x07, ~0x80, (value & 0x200) >> 2);
5312         xgifb_reg_and_or(pVBInfo->P3d4, 0x07, ~0x04, (value & 0x100) >> 6);
5313         xgifb_reg_set(pVBInfo->P3d4, 0x10, (value & 0xFF));
5314
5315         if (chip_id == XG27) {
5316                 /* Panel VRS SR35[2:0] SR34[7:0] */
5317                 xgifb_reg_and_or(pVBInfo->P3c4, 0x35, ~0x07,
5318                                         (value & 0x700) >> 8);
5319                 xgifb_reg_set(pVBInfo->P3c4, 0x34, value & 0xFF);
5320         } else {
5321                 /* Panel VRS SR3F[1:0] SR34[7:0] SR33[0] */
5322                 xgifb_reg_and_or(pVBInfo->P3c4, 0x3F, ~0x03,
5323                                         (value & 0x600) >> 9);
5324                 xgifb_reg_set(pVBInfo->P3c4, 0x34, (value >> 1) & 0xFF);
5325                 xgifb_reg_and_or(pVBInfo->P3d4, 0x33, ~0x01, value & 0x01);
5326         }
5327
5328         /* VRE SR0A[5] CR11[3:0] */
5329         value = LVDSVRE - 1;
5330         xgifb_reg_and_or(pVBInfo->P3c4, 0x0A, ~0x20, (value & 0x10) << 1);
5331         xgifb_reg_and_or(pVBInfo->P3d4, 0x11, ~0x0F, value & 0x0F);
5332
5333         /* Panel VRE SR3F[7:2] */
5334         if (chip_id == XG27)
5335                 xgifb_reg_and_or(pVBInfo->P3c4, 0x3F, ~0xFC,
5336                                         (value << 2) & 0xFC);
5337         else
5338                 /* SR3F[7] has to be 0, h/w bug */
5339                 xgifb_reg_and_or(pVBInfo->P3c4, 0x3F, ~0xFC,
5340                                         (value << 2) & 0x7C);
5341
5342         for (temp = 0, value = 0; temp < 3; temp++) {
5343
5344                 xgifb_reg_and_or(pVBInfo->P3c4, 0x31, ~0x30, value);
5345                 xgifb_reg_set(pVBInfo->P3c4,
5346                               0x2B, xgifb_info->lvds_data.VCLKData1);
5347                 xgifb_reg_set(pVBInfo->P3c4,
5348                               0x2C, xgifb_info->lvds_data.VCLKData2);
5349                 value += 0x10;
5350         }
5351
5352         if (!(modeflag & Charx8Dot)) {
5353                 inb(pVBInfo->P3da); /* reset 3da */
5354                 outb(0x13, pVBInfo->P3c0); /* set index */
5355                 /* set data, panning = 0, shift left 1 dot*/
5356                 outb(0x00, pVBInfo->P3c0);
5357
5358                 inb(pVBInfo->P3da); /* Enable Attribute */
5359                 outb(0x20, pVBInfo->P3c0);
5360
5361                 inb(pVBInfo->P3da); /* reset 3da */
5362         }
5363
5364 }
5365
5366 /* --------------------------------------------------------------------- */
5367 /* Function : XGI_IsLCDON */
5368 /* Input : */
5369 /* Output : 0 : Skip PSC Control */
5370 /* 1: Disable PSC */
5371 /* Description : */
5372 /* --------------------------------------------------------------------- */
5373 static unsigned char XGI_IsLCDON(struct vb_device_info *pVBInfo)
5374 {
5375         unsigned short tempax;
5376
5377         tempax = pVBInfo->VBInfo;
5378         if (tempax & SetCRT2ToDualEdge)
5379                 return 0;
5380         else if (tempax & (DisableCRT2Display | SwitchCRT2 | SetSimuScanMode))
5381                 return 1;
5382
5383         return 0;
5384 }
5385
5386 /* --------------------------------------------------------------------- */
5387 /* Function : XGI_DisableChISLCD */
5388 /* Input : */
5389 /* Output : 0 -> Not LCD Mode */
5390 /* Description : */
5391 /* --------------------------------------------------------------------- */
5392 static unsigned char XGI_DisableChISLCD(struct vb_device_info *pVBInfo)
5393 {
5394         unsigned short tempbx, tempah;
5395
5396         tempbx = pVBInfo->SetFlag & (DisableChA | DisableChB);
5397         tempah = ~((unsigned short) xgifb_reg_get(pVBInfo->Part1Port, 0x2E));
5398
5399         if (tempbx & (EnableChA | DisableChA)) {
5400                 if (!(tempah & 0x08)) /* Chk LCDA Mode */
5401                         return 0;
5402         }
5403
5404         if (!(tempbx & (EnableChB | DisableChB)))
5405                 return 0;
5406
5407         if (tempah & 0x01) /* Chk LCDB Mode */
5408                 return 1;
5409
5410         return 0;
5411 }
5412
5413 /* --------------------------------------------------------------------- */
5414 /* Function : XGI_EnableChISLCD */
5415 /* Input : */
5416 /* Output : 0 -> Not LCD mode */
5417 /* Description : */
5418 /* --------------------------------------------------------------------- */
5419 static unsigned char XGI_EnableChISLCD(struct vb_device_info *pVBInfo)
5420 {
5421         unsigned short tempbx, tempah;
5422
5423         tempbx = pVBInfo->SetFlag & (EnableChA | EnableChB);
5424         tempah = ~((unsigned short) xgifb_reg_get(pVBInfo->Part1Port, 0x2E));
5425
5426         if (tempbx & (EnableChA | DisableChA)) {
5427                 if (!(tempah & 0x08)) /* Chk LCDA Mode */
5428                         return 0;
5429         }
5430
5431         if (!(tempbx & (EnableChB | DisableChB)))
5432                 return 0;
5433
5434         if (tempah & 0x01) /* Chk LCDB Mode */
5435                 return 1;
5436
5437         return 0;
5438 }
5439
5440 static void XGI_DisableBridge(struct xgifb_video_info *xgifb_info,
5441                 struct xgi_hw_device_info *HwDeviceExtension,
5442                 struct vb_device_info *pVBInfo)
5443 {
5444         unsigned short tempah = 0;
5445
5446         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
5447                         | VB_SIS302LV | VB_XGI301C)) {
5448                 tempah = 0x3F;
5449                 if (!(pVBInfo->VBInfo &
5450                     (DisableCRT2Display | SetSimuScanMode))) {
5451                         if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA) {
5452                                 if (pVBInfo->VBInfo & SetCRT2ToDualEdge) {
5453                                         tempah = 0x7F; /* Disable Channel A */
5454                                         if (!(pVBInfo->VBInfo & XGI_SetCRT2ToLCDA))
5455                                                 /* Disable Channel B */
5456                                                 tempah = 0xBF;
5457
5458                                         if (pVBInfo->SetFlag & DisableChB)
5459                                                 /* force to disable Cahnnel */
5460                                                 tempah &= 0xBF;
5461
5462                                         if (pVBInfo->SetFlag & DisableChA)
5463                                                 /* Force to disable Channel B */
5464                                                 tempah &= 0x7F;
5465                                 }
5466                         }
5467                 }
5468
5469                 /* disable part4_1f */
5470                 xgifb_reg_and(pVBInfo->Part4Port, 0x1F, tempah);
5471
5472                 if (pVBInfo->VBType & (VB_SIS302LV | VB_XGI301C)) {
5473                         if (((pVBInfo->VBInfo & (SetCRT2ToLCD | XGI_SetCRT2ToLCDA)))
5474                                         || (XGI_DisableChISLCD(pVBInfo))
5475                                         || (XGI_IsLCDON(pVBInfo)))
5476                                 /* LVDS Driver power down */
5477                                 xgifb_reg_or(pVBInfo->Part4Port, 0x30, 0x80);
5478                 }
5479
5480                 if ((pVBInfo->SetFlag & DisableChA) || (pVBInfo->VBInfo
5481                                 & (DisableCRT2Display | XGI_SetCRT2ToLCDA
5482                                                 | SetSimuScanMode))) {
5483                         if (pVBInfo->SetFlag & GatingCRT)
5484                                 XGI_EnableGatingCRT(HwDeviceExtension, pVBInfo);
5485                         XGI_DisplayOff(xgifb_info, HwDeviceExtension, pVBInfo);
5486                 }
5487
5488                 if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA) {
5489                         if ((pVBInfo->SetFlag & DisableChA) || (pVBInfo->VBInfo
5490                                         & XGI_SetCRT2ToLCDA))
5491                                 /* Power down */
5492                                 xgifb_reg_and(pVBInfo->Part1Port, 0x1e, 0xdf);
5493                 }
5494
5495                 /* disable TV as primary VGA swap */
5496                 xgifb_reg_and(pVBInfo->P3c4, 0x32, 0xdf);
5497
5498                 if ((pVBInfo->VBInfo & (SetSimuScanMode | SetCRT2ToDualEdge)))
5499                         xgifb_reg_and(pVBInfo->Part2Port, 0x00, 0xdf);
5500
5501                 if ((pVBInfo->SetFlag & DisableChB) ||
5502                     (pVBInfo->VBInfo &
5503                         (DisableCRT2Display | SetSimuScanMode)) ||
5504                     ((!(pVBInfo->VBInfo & XGI_SetCRT2ToLCDA)) &&
5505                     (pVBInfo->VBInfo &
5506                         (SetCRT2ToRAMDAC | SetCRT2ToLCD | SetCRT2ToTV))))
5507                         xgifb_reg_or(pVBInfo->Part1Port, 0x00, 0x80);
5508
5509                 if ((pVBInfo->SetFlag & DisableChB) ||
5510                     (pVBInfo->VBInfo &
5511                         (DisableCRT2Display | SetSimuScanMode)) ||
5512                     (!(pVBInfo->VBInfo & XGI_SetCRT2ToLCDA)) ||
5513                     (pVBInfo->VBInfo &
5514                         (SetCRT2ToRAMDAC | SetCRT2ToLCD | SetCRT2ToTV))) {
5515                         /* save Part1 index 0 */
5516                         tempah = xgifb_reg_get(pVBInfo->Part1Port, 0x00);
5517                         /* BTDAC = 1, avoid VB reset */
5518                         xgifb_reg_or(pVBInfo->Part1Port, 0x00, 0x10);
5519                         /* disable CRT2 */
5520                         xgifb_reg_and(pVBInfo->Part1Port, 0x1E, 0xDF);
5521                         /* restore Part1 index 0 */
5522                         xgifb_reg_set(pVBInfo->Part1Port, 0x00, tempah);
5523                 }
5524         } else { /* {301} */
5525                 if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToTV)) {
5526                         xgifb_reg_or(pVBInfo->Part1Port, 0x00, 0x80);
5527                         /* Disable CRT2 */
5528                         xgifb_reg_and(pVBInfo->Part1Port, 0x1E, 0xDF);
5529                         /* Disable TV asPrimary VGA swap */
5530                         xgifb_reg_and(pVBInfo->P3c4, 0x32, 0xDF);
5531                 }
5532
5533                 if (pVBInfo->VBInfo & (DisableCRT2Display | XGI_SetCRT2ToLCDA
5534                                 | SetSimuScanMode))
5535                         XGI_DisplayOff(xgifb_info, HwDeviceExtension, pVBInfo);
5536         }
5537 }
5538
5539 /* --------------------------------------------------------------------- */
5540 /* Function : XGI_GetTVPtrIndex */
5541 /* Input : */
5542 /* Output : */
5543 /* Description : bx 0 : ExtNTSC */
5544 /* 1 : StNTSC */
5545 /* 2 : ExtPAL */
5546 /* 3 : StPAL */
5547 /* 4 : ExtHiTV */
5548 /* 5 : StHiTV */
5549 /* 6 : Ext525i */
5550 /* 7 : St525i */
5551 /* 8 : Ext525p */
5552 /* 9 : St525p */
5553 /* A : Ext750p */
5554 /* B : St750p */
5555 /* --------------------------------------------------------------------- */
5556 static unsigned short XGI_GetTVPtrIndex(struct vb_device_info *pVBInfo)
5557 {
5558         unsigned short tempbx = 0;
5559
5560         if (pVBInfo->TVInfo & TVSetPAL)
5561                 tempbx = 2;
5562         if (pVBInfo->TVInfo & TVSetHiVision)
5563                 tempbx = 4;
5564         if (pVBInfo->TVInfo & TVSetYPbPr525i)
5565                 tempbx = 6;
5566         if (pVBInfo->TVInfo & TVSetYPbPr525p)
5567                 tempbx = 8;
5568         if (pVBInfo->TVInfo & TVSetYPbPr750p)
5569                 tempbx = 10;
5570         if (pVBInfo->TVInfo & TVSimuMode)
5571                 tempbx++;
5572
5573         return tempbx;
5574 }
5575
5576 /* --------------------------------------------------------------------- */
5577 /* Function : XGI_GetTVPtrIndex2 */
5578 /* Input : */
5579 /* Output : bx 0 : NTSC */
5580 /* 1 : PAL */
5581 /* 2 : PALM */
5582 /* 3 : PALN */
5583 /* 4 : NTSC1024x768 */
5584 /* 5 : PAL-M 1024x768 */
5585 /* 6-7: reserved */
5586 /* cl 0 : YFilter1 */
5587 /* 1 : YFilter2 */
5588 /* ch 0 : 301A */
5589 /* 1 : 301B/302B/301LV/302LV */
5590 /* Description : */
5591 /* --------------------------------------------------------------------- */
5592 static void XGI_GetTVPtrIndex2(unsigned short *tempbx, unsigned char *tempcl,
5593                 unsigned char *tempch, struct vb_device_info *pVBInfo)
5594 {
5595         *tempbx = 0;
5596         *tempcl = 0;
5597         *tempch = 0;
5598
5599         if (pVBInfo->TVInfo & TVSetPAL)
5600                 *tempbx = 1;
5601
5602         if (pVBInfo->TVInfo & TVSetPALM)
5603                 *tempbx = 2;
5604
5605         if (pVBInfo->TVInfo & TVSetPALN)
5606                 *tempbx = 3;
5607
5608         if (pVBInfo->TVInfo & NTSC1024x768) {
5609                 *tempbx = 4;
5610                 if (pVBInfo->TVInfo & TVSetPALM)
5611                         *tempbx = 5;
5612         }
5613
5614         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
5615                         | VB_SIS302LV | VB_XGI301C)) {
5616                 if ((!(pVBInfo->VBInfo & SetInSlaveMode)) || (pVBInfo->TVInfo
5617                                 & TVSimuMode)) {
5618                         *tempbx += 8;
5619                         *tempcl += 1;
5620                 }
5621         }
5622
5623         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
5624                         | VB_SIS302LV | VB_XGI301C))
5625                 (*tempch)++;
5626 }
5627
5628 static void XGI_SetDelayComp(struct vb_device_info *pVBInfo)
5629 {
5630         unsigned char tempah, tempbl, tempbh;
5631
5632         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
5633                         | VB_SIS302LV | VB_XGI301C)) {
5634                 if (pVBInfo->VBInfo & (SetCRT2ToLCD | XGI_SetCRT2ToLCDA
5635                                 | SetCRT2ToTV | SetCRT2ToRAMDAC)) {
5636                         tempbh = 0;
5637                         tempbl = XGI301TVDelay;
5638
5639                         if (pVBInfo->VBInfo & SetCRT2ToDualEdge)
5640                                 tempbl = tempbl >> 4;
5641                         if (pVBInfo->VBInfo & (SetCRT2ToLCD | XGI_SetCRT2ToLCDA)) {
5642                                 tempbh = XGI301LCDDelay;
5643
5644                                 if (!(pVBInfo->VBInfo & XGI_SetCRT2ToLCDA))
5645                                         tempbl = tempbh;
5646                         }
5647
5648                         tempbl &= 0x0F;
5649                         tempbh &= 0xF0;
5650                         tempah = xgifb_reg_get(pVBInfo->Part1Port, 0x2D);
5651
5652                         if (pVBInfo->VBInfo & (SetCRT2ToRAMDAC | SetCRT2ToLCD
5653                                         | SetCRT2ToTV)) { /* Channel B */
5654                                 tempah &= 0xF0;
5655                                 tempah |= tempbl;
5656                         }
5657
5658                         if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA) { /* Channel A */
5659                                 tempah &= 0x0F;
5660                                 tempah |= tempbh;
5661                         }
5662                         xgifb_reg_set(pVBInfo->Part1Port, 0x2D, tempah);
5663                 }
5664         } else if (pVBInfo->IF_DEF_LVDS == 1) {
5665                 tempbl = 0;
5666                 tempbh = 0;
5667                 if (pVBInfo->VBInfo & SetCRT2ToLCD) {
5668                         tempah = XGI301LCDDelay;
5669                         tempah &= 0x0f;
5670                         tempah = tempah << 4;
5671                         xgifb_reg_and_or(pVBInfo->Part1Port, 0x2D, 0x0f,
5672                                         tempah);
5673                 }
5674         }
5675 }
5676
5677 static void XGI_SetLCDCap_A(unsigned short tempcx,
5678                             struct vb_device_info *pVBInfo)
5679 {
5680         unsigned short temp;
5681
5682         temp = xgifb_reg_get(pVBInfo->P3d4, 0x37);
5683
5684         if (temp & LCDRGB18Bit) {
5685                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x19, 0x0F,
5686                                 /* Enable Dither */
5687                                 (unsigned short) (0x20 | (tempcx & 0x00C0)));
5688                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x1A, 0x7F, 0x80);
5689         } else {
5690                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x19, 0x0F,
5691                                 (unsigned short) (0x30 | (tempcx & 0x00C0)));
5692                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x1A, 0x7F, 0x00);
5693         }
5694 }
5695
5696 /* --------------------------------------------------------------------- */
5697 /* Function : XGI_SetLCDCap_B */
5698 /* Input : cx -> LCD Capability */
5699 /* Output : */
5700 /* Description : */
5701 /* --------------------------------------------------------------------- */
5702 static void XGI_SetLCDCap_B(unsigned short tempcx,
5703                             struct vb_device_info *pVBInfo)
5704 {
5705         if (tempcx & EnableLCD24bpp) /* 24bits */
5706                 xgifb_reg_and_or(pVBInfo->Part2Port, 0x1A, 0xE0,
5707                                 (unsigned short) (((tempcx & 0x00ff) >> 6)
5708                                                 | 0x0c));
5709         else
5710                 xgifb_reg_and_or(pVBInfo->Part2Port, 0x1A, 0xE0,
5711                                 (unsigned short) (((tempcx & 0x00ff) >> 6)
5712                                                 | 0x18)); /* Enable Dither */
5713 }
5714
5715 static void XGI_LongWait(struct vb_device_info *pVBInfo)
5716 {
5717         unsigned short i;
5718
5719         i = xgifb_reg_get(pVBInfo->P3c4, 0x1F);
5720
5721         if (!(i & 0xC0)) {
5722                 for (i = 0; i < 0xFFFF; i++) {
5723                         if (!(inb(pVBInfo->P3da) & 0x08))
5724                                 break;
5725                 }
5726
5727                 for (i = 0; i < 0xFFFF; i++) {
5728                         if ((inb(pVBInfo->P3da) & 0x08))
5729                                 break;
5730                 }
5731         }
5732 }
5733
5734 static void SetSpectrum(struct vb_device_info *pVBInfo)
5735 {
5736         unsigned short index;
5737
5738         index = XGI_GetLCDCapPtr(pVBInfo);
5739
5740         /* disable down spectrum D[4] */
5741         xgifb_reg_and(pVBInfo->Part4Port, 0x30, 0x8F);
5742         XGI_LongWait(pVBInfo);
5743         xgifb_reg_or(pVBInfo->Part4Port, 0x30, 0x20); /* reset spectrum */
5744         XGI_LongWait(pVBInfo);
5745
5746         xgifb_reg_set(pVBInfo->Part4Port, 0x31,
5747                         pVBInfo->LCDCapList[index].Spectrum_31);
5748         xgifb_reg_set(pVBInfo->Part4Port, 0x32,
5749                         pVBInfo->LCDCapList[index].Spectrum_32);
5750         xgifb_reg_set(pVBInfo->Part4Port, 0x33,
5751                         pVBInfo->LCDCapList[index].Spectrum_33);
5752         xgifb_reg_set(pVBInfo->Part4Port, 0x34,
5753                         pVBInfo->LCDCapList[index].Spectrum_34);
5754         XGI_LongWait(pVBInfo);
5755         xgifb_reg_or(pVBInfo->Part4Port, 0x30, 0x40); /* enable spectrum */
5756 }
5757
5758 static void XGI_SetLCDCap(struct vb_device_info *pVBInfo)
5759 {
5760         unsigned short tempcx;
5761
5762         tempcx = pVBInfo->LCDCapList[XGI_GetLCDCapPtr(pVBInfo)].LCD_Capability;
5763
5764         if (pVBInfo->VBType &
5765             (VB_SIS301B |
5766              VB_SIS302B |
5767              VB_SIS301LV |
5768              VB_SIS302LV |
5769              VB_XGI301C)) { /* 301LV/302LV only */
5770                 if (pVBInfo->VBType &
5771                     (VB_SIS301LV | VB_SIS302LV | VB_XGI301C)) {
5772                         /* Set 301LV Capability */
5773                         xgifb_reg_set(pVBInfo->Part4Port, 0x24,
5774                                         (unsigned char) (tempcx & 0x1F));
5775                 }
5776                 /* VB Driving */
5777                 xgifb_reg_and_or(pVBInfo->Part4Port, 0x0D,
5778                                 ~((EnableVBCLKDRVLOW | EnablePLLSPLOW) >> 8),
5779                                 (unsigned short) ((tempcx & (EnableVBCLKDRVLOW
5780                                                 | EnablePLLSPLOW)) >> 8));
5781         }
5782
5783         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
5784                         | VB_SIS302LV | VB_XGI301C)) {
5785                 if (pVBInfo->VBInfo & SetCRT2ToLCD)
5786                         XGI_SetLCDCap_B(tempcx, pVBInfo);
5787                 else if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA)
5788                         XGI_SetLCDCap_A(tempcx, pVBInfo);
5789
5790                 if (pVBInfo->VBType & (VB_SIS302LV | VB_XGI301C)) {
5791                         if (tempcx & EnableSpectrum)
5792                                 SetSpectrum(pVBInfo);
5793                 }
5794         } else {
5795                 /* LVDS,CH7017 */
5796                 XGI_SetLCDCap_A(tempcx, pVBInfo);
5797         }
5798 }
5799
5800 /* --------------------------------------------------------------------- */
5801 /* Function : XGI_SetAntiFlicker */
5802 /* Input : */
5803 /* Output : */
5804 /* Description : Set TV Customized Param. */
5805 /* --------------------------------------------------------------------- */
5806 static void XGI_SetAntiFlicker(unsigned short ModeNo,
5807                                unsigned short ModeIdIndex,
5808                                struct vb_device_info *pVBInfo)
5809 {
5810         unsigned short tempbx;
5811
5812         unsigned char tempah;
5813
5814         if (pVBInfo->TVInfo & (TVSetYPbPr525p | TVSetYPbPr750p))
5815                 return;
5816
5817         tempbx = XGI_GetTVPtrIndex(pVBInfo);
5818         tempbx &= 0xFE;
5819         tempah = TVAntiFlickList[tempbx];
5820         tempah = tempah << 4;
5821
5822         xgifb_reg_and_or(pVBInfo->Part2Port, 0x0A, 0x8F, tempah);
5823 }
5824
5825 static void XGI_SetEdgeEnhance(unsigned short ModeNo,
5826                                unsigned short ModeIdIndex,
5827                                struct vb_device_info *pVBInfo)
5828 {
5829         unsigned short tempbx;
5830
5831         unsigned char tempah;
5832
5833         tempbx = XGI_GetTVPtrIndex(pVBInfo);
5834         tempbx &= 0xFE;
5835         tempah = TVEdgeList[tempbx];
5836         tempah = tempah << 5;
5837
5838         xgifb_reg_and_or(pVBInfo->Part2Port, 0x3A, 0x1F, tempah);
5839 }
5840
5841 static void XGI_SetPhaseIncr(struct vb_device_info *pVBInfo)
5842 {
5843         unsigned short tempbx;
5844
5845         unsigned char tempcl, tempch;
5846
5847         unsigned long tempData;
5848
5849         XGI_GetTVPtrIndex2(&tempbx, &tempcl, &tempch, pVBInfo); /* bx, cl, ch */
5850         tempData = TVPhaseList[tempbx];
5851
5852         xgifb_reg_set(pVBInfo->Part2Port, 0x31, (unsigned short) (tempData
5853                         & 0x000000FF));
5854         xgifb_reg_set(pVBInfo->Part2Port, 0x32, (unsigned short) ((tempData
5855                         & 0x0000FF00) >> 8));
5856         xgifb_reg_set(pVBInfo->Part2Port, 0x33, (unsigned short) ((tempData
5857                         & 0x00FF0000) >> 16));
5858         xgifb_reg_set(pVBInfo->Part2Port, 0x34, (unsigned short) ((tempData
5859                         & 0xFF000000) >> 24));
5860 }
5861
5862 static void XGI_SetYFilter(unsigned short ModeNo, unsigned short ModeIdIndex,
5863                 struct vb_device_info *pVBInfo)
5864 {
5865         unsigned short tempbx, index;
5866
5867         unsigned char tempcl, tempch, tempal, *filterPtr;
5868
5869         XGI_GetTVPtrIndex2(&tempbx, &tempcl, &tempch, pVBInfo); /* bx, cl, ch */
5870
5871         switch (tempbx) {
5872         case 0x00:
5873         case 0x04:
5874                 filterPtr = NTSCYFilter1;
5875                 break;
5876
5877         case 0x01:
5878                 filterPtr = PALYFilter1;
5879                 break;
5880
5881         case 0x02:
5882         case 0x05:
5883         case 0x0D:
5884         case 0x03:
5885                 filterPtr = xgifb_palmn_yfilter1;
5886                 break;
5887
5888         case 0x08:
5889         case 0x0C:
5890         case 0x0A:
5891         case 0x0B:
5892         case 0x09:
5893                 filterPtr = xgifb_yfilter2;
5894                 break;
5895
5896         default:
5897                 return;
5898         }
5899
5900         tempal = pVBInfo->EModeIDTable[ModeIdIndex].VB_ExtTVYFilterIndex;
5901         if (tempcl == 0)
5902                 index = tempal * 4;
5903         else
5904                 index = tempal * 7;
5905
5906         if ((tempcl == 0) && (tempch == 1)) {
5907                 xgifb_reg_set(pVBInfo->Part2Port, 0x35, 0);
5908                 xgifb_reg_set(pVBInfo->Part2Port, 0x36, 0);
5909                 xgifb_reg_set(pVBInfo->Part2Port, 0x37, 0);
5910                 xgifb_reg_set(pVBInfo->Part2Port, 0x38, filterPtr[index++]);
5911         } else {
5912                 xgifb_reg_set(pVBInfo->Part2Port, 0x35, filterPtr[index++]);
5913                 xgifb_reg_set(pVBInfo->Part2Port, 0x36, filterPtr[index++]);
5914                 xgifb_reg_set(pVBInfo->Part2Port, 0x37, filterPtr[index++]);
5915                 xgifb_reg_set(pVBInfo->Part2Port, 0x38, filterPtr[index++]);
5916         }
5917
5918         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
5919                         | VB_SIS302LV | VB_XGI301C)) {
5920                 xgifb_reg_set(pVBInfo->Part2Port, 0x48, filterPtr[index++]);
5921                 xgifb_reg_set(pVBInfo->Part2Port, 0x49, filterPtr[index++]);
5922                 xgifb_reg_set(pVBInfo->Part2Port, 0x4A, filterPtr[index++]);
5923         }
5924 }
5925
5926 /* --------------------------------------------------------------------- */
5927 /* Function : XGI_OEM310Setting */
5928 /* Input : */
5929 /* Output : */
5930 /* Description : Customized Param. for 301 */
5931 /* --------------------------------------------------------------------- */
5932 static void XGI_OEM310Setting(unsigned short ModeNo,
5933                               unsigned short ModeIdIndex,
5934                               struct vb_device_info *pVBInfo)
5935 {
5936         XGI_SetDelayComp(pVBInfo);
5937
5938         if (pVBInfo->VBInfo & (SetCRT2ToLCD | XGI_SetCRT2ToLCDA))
5939                 XGI_SetLCDCap(pVBInfo);
5940
5941         if (pVBInfo->VBInfo & SetCRT2ToTV) {
5942                 XGI_SetPhaseIncr(pVBInfo);
5943                 XGI_SetYFilter(ModeNo, ModeIdIndex, pVBInfo);
5944                 XGI_SetAntiFlicker(ModeNo, ModeIdIndex, pVBInfo);
5945
5946                 if (pVBInfo->VBType & VB_SIS301)
5947                         XGI_SetEdgeEnhance(ModeNo, ModeIdIndex, pVBInfo);
5948         }
5949 }
5950
5951 /* --------------------------------------------------------------------- */
5952 /* Function : XGI_SetCRT2ModeRegs */
5953 /* Input : */
5954 /* Output : */
5955 /* Description : Origin code for crt2group */
5956 /* --------------------------------------------------------------------- */
5957 static void XGI_SetCRT2ModeRegs(unsigned short ModeNo,
5958                 struct xgi_hw_device_info *HwDeviceExtension,
5959                 struct vb_device_info *pVBInfo)
5960 {
5961         unsigned short tempbl;
5962         short tempcl;
5963
5964         unsigned char tempah;
5965
5966         tempah = 0;
5967         if (!(pVBInfo->VBInfo & DisableCRT2Display)) {
5968                 tempah = xgifb_reg_get(pVBInfo->Part1Port, 0x00);
5969                 tempah &= ~0x10; /* BTRAMDAC */
5970                 tempah |= 0x40; /* BTRAM */
5971
5972                 if (pVBInfo->VBInfo & (SetCRT2ToRAMDAC | SetCRT2ToTV
5973                                 | SetCRT2ToLCD)) {
5974                         tempah = 0x40; /* BTDRAM */
5975                         tempcl = pVBInfo->ModeType;
5976                         tempcl -= ModeVGA;
5977                         if (tempcl >= 0) {
5978                                 /* BT Color */
5979                                 tempah = (0x008 >> tempcl);
5980                                 if (tempah == 0)
5981                                         tempah = 1;
5982                                 tempah |= 0x040;
5983                         }
5984                         if (pVBInfo->VBInfo & SetInSlaveMode)
5985                                 tempah ^= 0x50; /* BTDAC */
5986                 }
5987         }
5988
5989         xgifb_reg_set(pVBInfo->Part1Port, 0x00, tempah);
5990         tempah = 0x08;
5991         tempbl = 0xf0;
5992
5993         if (pVBInfo->VBInfo & DisableCRT2Display) {
5994                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x2e, tempbl, tempah);
5995         } else {
5996                 tempah = 0x00;
5997                 tempbl = 0xff;
5998
5999                 if (pVBInfo->VBInfo & (SetCRT2ToRAMDAC | SetCRT2ToTV
6000                                 | SetCRT2ToLCD | XGI_SetCRT2ToLCDA)) {
6001                         if ((pVBInfo->VBInfo & XGI_SetCRT2ToLCDA) &&
6002                             (!(pVBInfo->VBInfo & SetSimuScanMode))) {
6003                                 tempbl &= 0xf7;
6004                                 tempah |= 0x01;
6005                                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x2e,
6006                                                 tempbl, tempah);
6007                         } else {
6008                                 if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA) {
6009                                         tempbl &= 0xf7;
6010                                         tempah |= 0x01;
6011                                 }
6012
6013                                 if (pVBInfo->VBInfo &
6014                                     (SetCRT2ToRAMDAC |
6015                                      SetCRT2ToTV |
6016                                      SetCRT2ToLCD)) {
6017                                         tempbl &= 0xf8;
6018                                         tempah = 0x01;
6019
6020                                         if (!(pVBInfo->VBInfo & SetInSlaveMode))
6021                                                 tempah |= 0x02;
6022
6023                                         if (!(pVBInfo->VBInfo &
6024                                               SetCRT2ToRAMDAC)) {
6025                                                 tempah = tempah ^ 0x05;
6026                                                 if (!(pVBInfo->VBInfo &
6027                                                       SetCRT2ToLCD))
6028                                                         tempah = tempah ^ 0x01;
6029                                         }
6030
6031                                         if (!(pVBInfo->VBInfo &
6032                                               SetCRT2ToDualEdge))
6033                                                 tempah |= 0x08;
6034                                         xgifb_reg_and_or(pVBInfo->Part1Port,
6035                                                         0x2e, tempbl, tempah);
6036                                 } else {
6037                                         xgifb_reg_and_or(pVBInfo->Part1Port,
6038                                                         0x2e, tempbl, tempah);
6039                                 }
6040                         }
6041                 } else {
6042                         xgifb_reg_and_or(pVBInfo->Part1Port, 0x2e, tempbl,
6043                                         tempah);
6044                 }
6045         }
6046
6047         if (pVBInfo->VBInfo & (SetCRT2ToRAMDAC | SetCRT2ToTV | SetCRT2ToLCD
6048                         | XGI_SetCRT2ToLCDA)) {
6049                 tempah &= (~0x08);
6050                 if ((pVBInfo->ModeType == ModeVGA) && (!(pVBInfo->VBInfo
6051                                 & SetInSlaveMode))) {
6052                         tempah |= 0x010;
6053                 }
6054                 tempah |= 0x080;
6055
6056                 if (pVBInfo->VBInfo & SetCRT2ToTV) {
6057                         tempah |= 0x020;
6058                         if (pVBInfo->VBInfo & DriverMode)
6059                                 tempah = tempah ^ 0x20;
6060                 }
6061
6062                 xgifb_reg_and_or(pVBInfo->Part4Port, 0x0D, ~0x0BF, tempah);
6063                 tempah = 0;
6064
6065                 if (pVBInfo->LCDInfo & SetLCDDualLink)
6066                         tempah |= 0x40;
6067
6068                 if (pVBInfo->VBInfo & SetCRT2ToTV) {
6069                         if (pVBInfo->TVInfo & RPLLDIV2XO)
6070                                 tempah |= 0x40;
6071                 }
6072
6073                 if ((pVBInfo->LCDResInfo == Panel_1280x1024)
6074                                 || (pVBInfo->LCDResInfo == Panel_1280x1024x75))
6075                         tempah |= 0x80;
6076
6077                 if (pVBInfo->LCDResInfo == Panel_1280x960)
6078                         tempah |= 0x80;
6079
6080                 xgifb_reg_set(pVBInfo->Part4Port, 0x0C, tempah);
6081         }
6082
6083         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
6084                         | VB_SIS302LV | VB_XGI301C)) {
6085                 tempah = 0;
6086                 tempbl = 0xfb;
6087
6088                 if (pVBInfo->VBInfo & SetCRT2ToDualEdge) {
6089                         tempbl = 0xff;
6090                         if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA)
6091                                 tempah |= 0x04; /* shampoo 0129 */
6092                 }
6093
6094                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x13, tempbl, tempah);
6095                 tempah = 0x00;
6096                 tempbl = 0xcf;
6097                 if (!(pVBInfo->VBInfo & DisableCRT2Display)) {
6098                         if (pVBInfo->VBInfo & SetCRT2ToDualEdge)
6099                                 tempah |= 0x30;
6100                 }
6101
6102                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x2c, tempbl, tempah);
6103                 tempah = 0;
6104                 tempbl = 0x3f;
6105
6106                 if (!(pVBInfo->VBInfo & DisableCRT2Display)) {
6107                         if (pVBInfo->VBInfo & SetCRT2ToDualEdge)
6108                                 tempah |= 0xc0;
6109                 }
6110                 xgifb_reg_and_or(pVBInfo->Part4Port, 0x21, tempbl, tempah);
6111         }
6112
6113         tempah = 0;
6114         tempbl = 0x7f;
6115         if (!(pVBInfo->VBInfo & XGI_SetCRT2ToLCDA)) {
6116                 tempbl = 0xff;
6117                 if (!(pVBInfo->VBInfo & SetCRT2ToDualEdge))
6118                         tempah |= 0x80;
6119         }
6120
6121         xgifb_reg_and_or(pVBInfo->Part4Port, 0x23, tempbl, tempah);
6122
6123         if (pVBInfo->VBType & (VB_SIS302LV | VB_XGI301C)) {
6124                 if (pVBInfo->LCDInfo & SetLCDDualLink) {
6125                         xgifb_reg_or(pVBInfo->Part4Port, 0x27, 0x20);
6126                         xgifb_reg_or(pVBInfo->Part4Port, 0x34, 0x10);
6127                 }
6128         }
6129 }
6130
6131
6132 void XGI_UnLockCRT2(struct xgi_hw_device_info *HwDeviceExtension,
6133                 struct vb_device_info *pVBInfo)
6134 {
6135
6136         xgifb_reg_and_or(pVBInfo->Part1Port, 0x2f, 0xFF, 0x01);
6137
6138 }
6139
6140 void XGI_LockCRT2(struct xgi_hw_device_info *HwDeviceExtension,
6141                 struct vb_device_info *pVBInfo)
6142 {
6143
6144         xgifb_reg_and_or(pVBInfo->Part1Port, 0x2F, 0xFE, 0x00);
6145
6146 }
6147
6148 unsigned char XGI_BridgeIsOn(struct vb_device_info *pVBInfo)
6149 {
6150         unsigned short flag;
6151
6152         if (pVBInfo->IF_DEF_LVDS == 1) {
6153                 return 1;
6154         } else {
6155                 flag = xgifb_reg_get(pVBInfo->Part4Port, 0x00);
6156                 if ((flag == 1) || (flag == 2))
6157                         return 1; /* 301b */
6158                 else
6159                         return 0;
6160         }
6161 }
6162
6163 unsigned short XGI_GetRatePtrCRT2(struct xgi_hw_device_info *pXGIHWDE,
6164                 unsigned short ModeNo, unsigned short ModeIdIndex,
6165                 struct vb_device_info *pVBInfo)
6166 {
6167         short LCDRefreshIndex[] = { 0x00, 0x00, 0x03, 0x01 },
6168                         LCDARefreshIndex[] = { 0x00, 0x00, 0x03, 0x01, 0x01,
6169                                         0x01, 0x01 };
6170
6171         unsigned short RefreshRateTableIndex, i, modeflag, index, temp;
6172
6173         modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
6174
6175         index = xgifb_reg_get(pVBInfo->P3d4, 0x33);
6176         index = index >> pVBInfo->SelectCRT2Rate;
6177         index &= 0x0F;
6178
6179         if (pVBInfo->LCDInfo & LCDNonExpanding)
6180                 index = 0;
6181
6182         if (index > 0)
6183                 index--;
6184
6185         if (pVBInfo->SetFlag & ProgrammingCRT2) {
6186                 if (pVBInfo->VBInfo & (SetCRT2ToLCD | XGI_SetCRT2ToLCDA)) {
6187                         if (pVBInfo->IF_DEF_LVDS == 0) {
6188                                 if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B
6189                                                 | VB_SIS301LV | VB_SIS302LV
6190                                                 | VB_XGI301C))
6191                                         /* 301b */
6192                                         temp = LCDARefreshIndex[
6193                                                 pVBInfo->LCDResInfo & 0x0F];
6194                                 else
6195                                         temp = LCDRefreshIndex[
6196                                                 pVBInfo->LCDResInfo & 0x0F];
6197
6198                                 if (index > temp)
6199                                         index = temp;
6200                         } else {
6201                                 index = 0;
6202                         }
6203                 }
6204         }
6205
6206         RefreshRateTableIndex = pVBInfo->EModeIDTable[ModeIdIndex].REFindex;
6207         ModeNo = pVBInfo->RefIndex[RefreshRateTableIndex].ModeID;
6208         if (pXGIHWDE->jChipType >= XG20) { /* for XG20, XG21, XG27 */
6209                 if ((pVBInfo->RefIndex[RefreshRateTableIndex].XRes == 800) &&
6210                     (pVBInfo->RefIndex[RefreshRateTableIndex].YRes == 600)) {
6211                         index++;
6212                 }
6213                 /* do the similar adjustment like XGISearchCRT1Rate() */
6214                 if ((pVBInfo->RefIndex[RefreshRateTableIndex].XRes == 1024) &&
6215                     (pVBInfo->RefIndex[RefreshRateTableIndex].YRes == 768)) {
6216                         index++;
6217                 }
6218                 if ((pVBInfo->RefIndex[RefreshRateTableIndex].XRes == 1280) &&
6219                     (pVBInfo->RefIndex[RefreshRateTableIndex].YRes == 1024)) {
6220                         index++;
6221                 }
6222         }
6223
6224         i = 0;
6225         do {
6226                 if (pVBInfo->RefIndex[RefreshRateTableIndex + i].
6227                         ModeID != ModeNo)
6228                         break;
6229                 temp = pVBInfo->RefIndex[RefreshRateTableIndex + i].
6230                         Ext_InfoFlag;
6231                 temp &= ModeTypeMask;
6232                 if (temp < pVBInfo->ModeType)
6233                         break;
6234                 i++;
6235                 index--;
6236
6237         } while (index != 0xFFFF);
6238         if (!(pVBInfo->VBInfo & SetCRT2ToRAMDAC)) {
6239                 if (pVBInfo->VBInfo & SetInSlaveMode) {
6240                         temp = pVBInfo->RefIndex[RefreshRateTableIndex + i - 1].
6241                                 Ext_InfoFlag;
6242                         if (temp & InterlaceMode)
6243                                 i++;
6244                 }
6245         }
6246         i--;
6247         if ((pVBInfo->SetFlag & ProgrammingCRT2)) {
6248                 temp = XGI_AjustCRT2Rate(ModeNo, ModeIdIndex,
6249                                 RefreshRateTableIndex, &i, pVBInfo);
6250         }
6251         return RefreshRateTableIndex + i;
6252 }
6253
6254 static void XGI_SetLCDAGroup(unsigned short ModeNo, unsigned short ModeIdIndex,
6255                 struct xgi_hw_device_info *HwDeviceExtension,
6256                 struct vb_device_info *pVBInfo)
6257 {
6258         unsigned short RefreshRateTableIndex;
6259
6260         pVBInfo->SetFlag |= ProgrammingCRT2;
6261         RefreshRateTableIndex = XGI_GetRatePtrCRT2(HwDeviceExtension, ModeNo,
6262                         ModeIdIndex, pVBInfo);
6263         XGI_GetLVDSResInfo(ModeNo, ModeIdIndex, pVBInfo);
6264         XGI_GetLVDSData(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
6265         XGI_ModCRT1Regs(ModeNo, ModeIdIndex, RefreshRateTableIndex,
6266                         HwDeviceExtension, pVBInfo);
6267         XGI_SetLVDSRegs(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
6268         XGI_SetCRT2ECLK(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
6269 }
6270
6271 static unsigned char XGI_SetCRT2Group301(unsigned short ModeNo,
6272                 struct xgi_hw_device_info *HwDeviceExtension,
6273                 struct vb_device_info *pVBInfo)
6274 {
6275         unsigned short tempbx, ModeIdIndex, RefreshRateTableIndex;
6276
6277         tempbx = pVBInfo->VBInfo;
6278         pVBInfo->SetFlag |= ProgrammingCRT2;
6279         XGI_SearchModeID(ModeNo, &ModeIdIndex, pVBInfo);
6280         pVBInfo->SelectCRT2Rate = 4;
6281         RefreshRateTableIndex = XGI_GetRatePtrCRT2(HwDeviceExtension, ModeNo,
6282                         ModeIdIndex, pVBInfo);
6283         XGI_SaveCRT2Info(ModeNo, pVBInfo);
6284         XGI_GetCRT2ResInfo(ModeNo, ModeIdIndex, pVBInfo);
6285         XGI_GetCRT2Data(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
6286         XGI_PreSetGroup1(ModeNo, ModeIdIndex, HwDeviceExtension,
6287                         RefreshRateTableIndex, pVBInfo);
6288         XGI_SetGroup1(ModeNo, ModeIdIndex, HwDeviceExtension,
6289                         RefreshRateTableIndex, pVBInfo);
6290         XGI_SetLockRegs(ModeNo, ModeIdIndex, HwDeviceExtension,
6291                         RefreshRateTableIndex, pVBInfo);
6292         XGI_SetGroup2(ModeNo, ModeIdIndex, RefreshRateTableIndex,
6293                         HwDeviceExtension, pVBInfo);
6294         XGI_SetLCDRegs(ModeNo, ModeIdIndex, HwDeviceExtension,
6295                         RefreshRateTableIndex, pVBInfo);
6296         XGI_SetTap4Regs(pVBInfo);
6297         XGI_SetGroup3(ModeNo, ModeIdIndex, pVBInfo);
6298         XGI_SetGroup4(ModeNo, ModeIdIndex, RefreshRateTableIndex,
6299                         HwDeviceExtension, pVBInfo);
6300         XGI_SetCRT2VCLK(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
6301         XGI_SetGroup5(ModeNo, ModeIdIndex, pVBInfo);
6302         XGI_AutoThreshold(pVBInfo);
6303         return 1;
6304 }
6305
6306 void XGI_SenseCRT1(struct vb_device_info *pVBInfo)
6307 {
6308         unsigned char CRTCData[17] = { 0x5F, 0x4F, 0x50, 0x82, 0x55, 0x81,
6309                         0x0B, 0x3E, 0xE9, 0x0B, 0xDF, 0xE7, 0x04, 0x00, 0x00,
6310                         0x05, 0x00 };
6311
6312         unsigned char SR01 = 0, SR1F = 0, SR07 = 0, SR06 = 0;
6313
6314         unsigned char CR17, CR63, SR31;
6315         unsigned short temp;
6316         unsigned char DAC_TEST_PARMS[3] = { 0x0F, 0x0F, 0x0F };
6317
6318         int i;
6319         xgifb_reg_set(pVBInfo->P3c4, 0x05, 0x86);
6320
6321         /* to fix XG42 single LCD sense to CRT+LCD */
6322         xgifb_reg_set(pVBInfo->P3d4, 0x57, 0x4A);
6323         xgifb_reg_set(pVBInfo->P3d4, 0x53, (unsigned char) (xgifb_reg_get(
6324                         pVBInfo->P3d4, 0x53) | 0x02));
6325
6326         SR31 = (unsigned char) xgifb_reg_get(pVBInfo->P3c4, 0x31);
6327         CR63 = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x63);
6328         SR01 = (unsigned char) xgifb_reg_get(pVBInfo->P3c4, 0x01);
6329
6330         xgifb_reg_set(pVBInfo->P3c4, 0x01, (unsigned char) (SR01 & 0xDF));
6331         xgifb_reg_set(pVBInfo->P3d4, 0x63, (unsigned char) (CR63 & 0xBF));
6332
6333         CR17 = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x17);
6334         xgifb_reg_set(pVBInfo->P3d4, 0x17, (unsigned char) (CR17 | 0x80));
6335
6336         SR1F = (unsigned char) xgifb_reg_get(pVBInfo->P3c4, 0x1F);
6337         xgifb_reg_set(pVBInfo->P3c4, 0x1F, (unsigned char) (SR1F | 0x04));
6338
6339         SR07 = (unsigned char) xgifb_reg_get(pVBInfo->P3c4, 0x07);
6340         xgifb_reg_set(pVBInfo->P3c4, 0x07, (unsigned char) (SR07 & 0xFB));
6341         SR06 = (unsigned char) xgifb_reg_get(pVBInfo->P3c4, 0x06);
6342         xgifb_reg_set(pVBInfo->P3c4, 0x06, (unsigned char) (SR06 & 0xC3));
6343
6344         xgifb_reg_set(pVBInfo->P3d4, 0x11, 0x00);
6345
6346         for (i = 0; i < 8; i++)
6347                 xgifb_reg_set(pVBInfo->P3d4, (unsigned short) i, CRTCData[i]);
6348
6349         for (i = 8; i < 11; i++)
6350                 xgifb_reg_set(pVBInfo->P3d4, (unsigned short) (i + 8),
6351                                 CRTCData[i]);
6352
6353         for (i = 11; i < 13; i++)
6354                 xgifb_reg_set(pVBInfo->P3d4, (unsigned short) (i + 4),
6355                                 CRTCData[i]);
6356
6357         for (i = 13; i < 16; i++)
6358                 xgifb_reg_set(pVBInfo->P3c4, (unsigned short) (i - 3),
6359                                 CRTCData[i]);
6360
6361         xgifb_reg_set(pVBInfo->P3c4, 0x0E, (unsigned char) (CRTCData[16]
6362                         & 0xE0));
6363
6364         xgifb_reg_set(pVBInfo->P3c4, 0x31, 0x00);
6365         xgifb_reg_set(pVBInfo->P3c4, 0x2B, 0x1B);
6366         xgifb_reg_set(pVBInfo->P3c4, 0x2C, 0xE1);
6367
6368         outb(0x00, pVBInfo->P3c8);
6369
6370         for (i = 0; i < 256; i++) {
6371                 outb((unsigned char) DAC_TEST_PARMS[0], (pVBInfo->P3c8 + 1));
6372                 outb((unsigned char) DAC_TEST_PARMS[1], (pVBInfo->P3c8 + 1));
6373                 outb((unsigned char) DAC_TEST_PARMS[2], (pVBInfo->P3c8 + 1));
6374         }
6375
6376         mdelay(1);
6377
6378         XGI_WaitDisply(pVBInfo);
6379         temp = inb(pVBInfo->P3c2);
6380
6381         if (temp & 0x10)
6382                 xgifb_reg_and_or(pVBInfo->P3d4, 0x32, 0xDF, 0x20);
6383         else
6384                 xgifb_reg_and_or(pVBInfo->P3d4, 0x32, 0xDF, 0x00);
6385
6386         /* avoid display something, set BLACK DAC if not restore DAC */
6387         outb(0x00, pVBInfo->P3c8);
6388
6389         for (i = 0; i < 256; i++) {
6390                 outb(0, (pVBInfo->P3c8 + 1));
6391                 outb(0, (pVBInfo->P3c8 + 1));
6392                 outb(0, (pVBInfo->P3c8 + 1));
6393         }
6394
6395         xgifb_reg_set(pVBInfo->P3c4, 0x01, SR01);
6396         xgifb_reg_set(pVBInfo->P3d4, 0x63, CR63);
6397         xgifb_reg_set(pVBInfo->P3c4, 0x31, SR31);
6398
6399         xgifb_reg_set(pVBInfo->P3d4, 0x53, (unsigned char) (xgifb_reg_get(
6400                         pVBInfo->P3d4, 0x53) & 0xFD));
6401         xgifb_reg_set(pVBInfo->P3c4, 0x1F, (unsigned char) SR1F);
6402 }
6403
6404 static void XGI_EnableBridge(struct xgifb_video_info *xgifb_info,
6405                 struct xgi_hw_device_info *HwDeviceExtension,
6406                 struct vb_device_info *pVBInfo)
6407 {
6408         unsigned short tempah;
6409
6410         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
6411                         | VB_SIS302LV | VB_XGI301C)) {
6412                 if (!(pVBInfo->SetFlag & DisableChA)) {
6413                         if (pVBInfo->SetFlag & EnableChA) {
6414                                 /* Power on */
6415                                 xgifb_reg_set(pVBInfo->Part1Port, 0x1E, 0x20);
6416                         } else {
6417                                 if (pVBInfo->VBInfo & SetCRT2ToDualEdge) {
6418                                         /* Power on */
6419                                         xgifb_reg_set(pVBInfo->Part1Port,
6420                                                         0x1E, 0x20);
6421                                 }
6422                         }
6423                 }
6424
6425                 if (!(pVBInfo->SetFlag & DisableChB)) {
6426                         if ((pVBInfo->SetFlag & EnableChB) || (pVBInfo->VBInfo
6427                                         & (SetCRT2ToLCD | SetCRT2ToTV
6428                                                         | SetCRT2ToRAMDAC))) {
6429                                 tempah = (unsigned char) xgifb_reg_get(
6430                                                 pVBInfo->P3c4, 0x32);
6431                                 tempah &= 0xDF;
6432                                 if (pVBInfo->VBInfo & SetInSlaveMode) {
6433                                         if (!(pVBInfo->VBInfo &
6434                                               SetCRT2ToRAMDAC))
6435                                                 tempah |= 0x20;
6436                                 }
6437                                 xgifb_reg_set(pVBInfo->P3c4, 0x32, tempah);
6438                                 xgifb_reg_or(pVBInfo->P3c4, 0x1E, 0x20);
6439
6440                                 tempah = (unsigned char) xgifb_reg_get(
6441                                                 pVBInfo->Part1Port, 0x2E);
6442
6443                                 if (!(tempah & 0x80))
6444                                         xgifb_reg_or(pVBInfo->Part1Port,
6445                                                         0x2E, 0x80);
6446                                 xgifb_reg_and(pVBInfo->Part1Port, 0x00, 0x7F);
6447                         }
6448                 }
6449
6450                 if ((pVBInfo->SetFlag & (EnableChA | EnableChB))
6451                                 || (!(pVBInfo->VBInfo & DisableCRT2Display))) {
6452                         xgifb_reg_and_or(pVBInfo->Part2Port, 0x00, ~0xE0,
6453                                         0x20); /* shampoo 0129 */
6454                         if (pVBInfo->VBType & (VB_SIS302LV | VB_XGI301C)) {
6455                                 if (!XGI_DisableChISLCD(pVBInfo)) {
6456                                         if (XGI_EnableChISLCD(pVBInfo) ||
6457                                             (pVBInfo->VBInfo &
6458                                             (SetCRT2ToLCD | XGI_SetCRT2ToLCDA)))
6459                                                 /* LVDS PLL power on */
6460                                                 xgifb_reg_and(
6461                                                         pVBInfo->Part4Port,
6462                                                         0x2A,
6463                                                         0x7F);
6464                                 }
6465                                 /* LVDS Driver power on */
6466                                 xgifb_reg_and(pVBInfo->Part4Port, 0x30, 0x7F);
6467                         }
6468                 }
6469
6470                 tempah = 0x00;
6471
6472                 if (!(pVBInfo->VBInfo & DisableCRT2Display)) {
6473                         tempah = 0xc0;
6474
6475                         if (!(pVBInfo->VBInfo & SetSimuScanMode) &&
6476                             (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA) &&
6477                             (pVBInfo->VBInfo & SetCRT2ToDualEdge)) {
6478                                 tempah = tempah & 0x40;
6479                                 if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA)
6480                                         tempah = tempah ^ 0xC0;
6481
6482                                 if (pVBInfo->SetFlag & DisableChB)
6483                                         tempah &= 0xBF;
6484
6485                                 if (pVBInfo->SetFlag &  DisableChA)
6486                                         tempah &= 0x7F;
6487
6488                                 if (pVBInfo->SetFlag &  EnableChB)
6489                                         tempah |= 0x40;
6490
6491                                 if (pVBInfo->SetFlag &  EnableChA)
6492                                         tempah |= 0x80;
6493                         }
6494                 }
6495
6496                 /* EnablePart4_1F */
6497                 xgifb_reg_or(pVBInfo->Part4Port, 0x1F, tempah);
6498
6499                 if (!(pVBInfo->SetFlag & DisableChA)) {
6500                         if (!(pVBInfo->SetFlag & GatingCRT)) {
6501                                 XGI_DisableGatingCRT(HwDeviceExtension,
6502                                                      pVBInfo);
6503                                 XGI_DisplayOn(xgifb_info, HwDeviceExtension,
6504                                                 pVBInfo);
6505                         }
6506                 }
6507         } /* 301 */
6508         else { /* LVDS */
6509                 if (pVBInfo->VBInfo & (SetCRT2ToTV | SetCRT2ToLCD
6510                                 | XGI_SetCRT2ToLCDA))
6511                         /* enable CRT2 */
6512                         xgifb_reg_or(pVBInfo->Part1Port, 0x1E, 0x20);
6513
6514                 tempah = (unsigned char) xgifb_reg_get(pVBInfo->Part1Port,
6515                                 0x2E);
6516                 if (!(tempah & 0x80))
6517                         xgifb_reg_or(pVBInfo->Part1Port, 0x2E, 0x80);
6518
6519                 xgifb_reg_and(pVBInfo->Part1Port, 0x00, 0x7F);
6520                 XGI_DisplayOn(xgifb_info, HwDeviceExtension, pVBInfo);
6521         } /* End of VB */
6522 }
6523
6524 static void XGI_SetCRT1Group(struct xgifb_video_info *xgifb_info,
6525                 struct xgi_hw_device_info *HwDeviceExtension,
6526                 unsigned short ModeNo, unsigned short ModeIdIndex,
6527                 struct vb_device_info *pVBInfo)
6528 {
6529         unsigned short RefreshRateTableIndex, temp;
6530
6531         XGI_SetSeqRegs(ModeNo, ModeIdIndex, pVBInfo);
6532         outb(pVBInfo->StandTable->MISC, pVBInfo->P3c2);
6533         XGI_SetCRTCRegs(HwDeviceExtension, pVBInfo);
6534         XGI_SetATTRegs(ModeNo, ModeIdIndex, pVBInfo);
6535         XGI_SetGRCRegs(pVBInfo);
6536         XGI_ClearExt1Regs(pVBInfo);
6537
6538         if (HwDeviceExtension->jChipType == XG27) {
6539                 if (pVBInfo->IF_DEF_LVDS == 0)
6540                         XGI_SetDefaultVCLK(pVBInfo);
6541         }
6542
6543         temp = ~ProgrammingCRT2;
6544         pVBInfo->SetFlag &= temp;
6545         pVBInfo->SelectCRT2Rate = 0;
6546
6547         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
6548                         | VB_SIS302LV | VB_XGI301C)) {
6549                 if (pVBInfo->VBInfo & (SetSimuScanMode | XGI_SetCRT2ToLCDA
6550                                 | SetInSlaveMode)) {
6551                         pVBInfo->SetFlag |= ProgrammingCRT2;
6552                 }
6553         }
6554
6555         RefreshRateTableIndex = XGI_GetRatePtrCRT2(HwDeviceExtension, ModeNo,
6556                         ModeIdIndex, pVBInfo);
6557         if (RefreshRateTableIndex != 0xFFFF) {
6558                 XGI_SetSync(RefreshRateTableIndex, pVBInfo);
6559                 XGI_SetCRT1CRTC(ModeNo, ModeIdIndex, RefreshRateTableIndex,
6560                                 pVBInfo, HwDeviceExtension);
6561                 XGI_SetCRT1DE(HwDeviceExtension, ModeNo, ModeIdIndex,
6562                                 RefreshRateTableIndex, pVBInfo);
6563                 XGI_SetCRT1Offset(ModeNo, ModeIdIndex, RefreshRateTableIndex,
6564                                 HwDeviceExtension, pVBInfo);
6565                 XGI_SetCRT1VCLK(ModeNo, ModeIdIndex, HwDeviceExtension,
6566                                 RefreshRateTableIndex, pVBInfo);
6567         }
6568
6569         if (HwDeviceExtension->jChipType >= XG21) {
6570                 temp = xgifb_reg_get(pVBInfo->P3d4, 0x38);
6571                 if (temp & 0xA0) {
6572
6573                         if (HwDeviceExtension->jChipType == XG27)
6574                                 XGI_SetXG27CRTC(ModeNo, ModeIdIndex,
6575                                                 RefreshRateTableIndex, pVBInfo);
6576                         else
6577                                 XGI_SetXG21CRTC(ModeNo, ModeIdIndex,
6578                                                 RefreshRateTableIndex, pVBInfo);
6579
6580                         XGI_UpdateXG21CRTC(ModeNo, pVBInfo,
6581                                         RefreshRateTableIndex);
6582
6583                         xgifb_set_lcd(HwDeviceExtension->jChipType,
6584                                         pVBInfo, RefreshRateTableIndex, ModeNo);
6585
6586                         if (pVBInfo->IF_DEF_LVDS == 1)
6587                                 xgifb_set_lvds(xgifb_info,
6588                                                 HwDeviceExtension->jChipType,
6589                                                 ModeNo, ModeIdIndex, pVBInfo);
6590                 }
6591         }
6592
6593         pVBInfo->SetFlag &= (~ProgrammingCRT2);
6594         XGI_SetCRT1FIFO(ModeNo, HwDeviceExtension, pVBInfo);
6595         XGI_SetCRT1ModeRegs(HwDeviceExtension, ModeNo, ModeIdIndex,
6596                         RefreshRateTableIndex, pVBInfo);
6597         XGI_LoadDAC(ModeNo, ModeIdIndex, pVBInfo);
6598 }
6599
6600 unsigned char XGISetModeNew(struct xgifb_video_info *xgifb_info,
6601                         struct xgi_hw_device_info *HwDeviceExtension,
6602                         unsigned short ModeNo)
6603 {
6604         unsigned short ModeIdIndex;
6605         struct vb_device_info VBINF;
6606         struct vb_device_info *pVBInfo = &VBINF;
6607         pVBInfo->BaseAddr = xgifb_info->vga_base;
6608         pVBInfo->IF_DEF_LVDS = 0;
6609         pVBInfo->IF_DEF_LCDA = 1;
6610
6611         if (HwDeviceExtension->jChipType >= XG20) {
6612                 pVBInfo->IF_DEF_YPbPr = 0;
6613                 pVBInfo->IF_DEF_HiVision = 0;
6614                 pVBInfo->IF_DEF_CRT2Monitor = 0;
6615                 pVBInfo->VBType = 0; /*set VBType default 0*/
6616         } else {
6617                 pVBInfo->IF_DEF_YPbPr = 1;
6618                 pVBInfo->IF_DEF_HiVision = 1;
6619                 pVBInfo->IF_DEF_CRT2Monitor = 1;
6620         }
6621
6622         pVBInfo->P3c4 = pVBInfo->BaseAddr + 0x14;
6623         pVBInfo->P3d4 = pVBInfo->BaseAddr + 0x24;
6624         pVBInfo->P3c0 = pVBInfo->BaseAddr + 0x10;
6625         pVBInfo->P3ce = pVBInfo->BaseAddr + 0x1e;
6626         pVBInfo->P3c2 = pVBInfo->BaseAddr + 0x12;
6627         pVBInfo->P3cc = pVBInfo->BaseAddr + 0x1C;
6628         pVBInfo->P3ca = pVBInfo->BaseAddr + 0x1a;
6629         pVBInfo->P3c6 = pVBInfo->BaseAddr + 0x16;
6630         pVBInfo->P3c7 = pVBInfo->BaseAddr + 0x17;
6631         pVBInfo->P3c8 = pVBInfo->BaseAddr + 0x18;
6632         pVBInfo->P3c9 = pVBInfo->BaseAddr + 0x19;
6633         pVBInfo->P3da = pVBInfo->BaseAddr + 0x2A;
6634         pVBInfo->Part0Port = pVBInfo->BaseAddr + XGI_CRT2_PORT_00;
6635         pVBInfo->Part1Port = pVBInfo->BaseAddr + SIS_CRT2_PORT_04;
6636         pVBInfo->Part2Port = pVBInfo->BaseAddr + SIS_CRT2_PORT_10;
6637         pVBInfo->Part3Port = pVBInfo->BaseAddr + SIS_CRT2_PORT_12;
6638         pVBInfo->Part4Port = pVBInfo->BaseAddr + SIS_CRT2_PORT_14;
6639         pVBInfo->Part5Port = pVBInfo->BaseAddr + SIS_CRT2_PORT_14 + 2;
6640
6641         /* for x86 Linux, XG21 LVDS */
6642         if (HwDeviceExtension->jChipType == XG21) {
6643                 if ((xgifb_reg_get(pVBInfo->P3d4, 0x38) & 0xE0) == 0xC0)
6644                         pVBInfo->IF_DEF_LVDS = 1;
6645         }
6646         if (HwDeviceExtension->jChipType == XG27) {
6647                 if ((xgifb_reg_get(pVBInfo->P3d4, 0x38) & 0xE0) == 0xC0) {
6648                         if (xgifb_reg_get(pVBInfo->P3d4, 0x30) & 0x20)
6649                                 pVBInfo->IF_DEF_LVDS = 1;
6650                 }
6651         }
6652
6653         if (HwDeviceExtension->jChipType < XG20)
6654                 XGI_GetVBType(pVBInfo);
6655
6656         InitTo330Pointer(HwDeviceExtension->jChipType, pVBInfo);
6657         if (ModeNo & 0x80)
6658                 ModeNo = ModeNo & 0x7F;
6659         xgifb_reg_set(pVBInfo->P3c4, 0x05, 0x86);
6660
6661         if (HwDeviceExtension->jChipType < XG20)
6662                 XGI_UnLockCRT2(HwDeviceExtension, pVBInfo);
6663
6664         XGI_SearchModeID(ModeNo, &ModeIdIndex, pVBInfo);
6665
6666         if (HwDeviceExtension->jChipType < XG20) {
6667                 XGI_GetVBInfo(ModeNo, ModeIdIndex, HwDeviceExtension, pVBInfo);
6668                 XGI_GetTVInfo(ModeNo, ModeIdIndex, pVBInfo);
6669                 XGI_GetLCDInfo(ModeNo, ModeIdIndex, pVBInfo);
6670                 XGI_DisableBridge(xgifb_info, HwDeviceExtension, pVBInfo);
6671
6672                 if (pVBInfo->VBInfo & (SetSimuScanMode | XGI_SetCRT2ToLCDA)) {
6673                         XGI_SetCRT1Group(xgifb_info, HwDeviceExtension, ModeNo,
6674                                         ModeIdIndex, pVBInfo);
6675
6676                         if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA) {
6677                                 XGI_SetLCDAGroup(ModeNo, ModeIdIndex,
6678                                                 HwDeviceExtension, pVBInfo);
6679                         }
6680                 } else {
6681                         if (!(pVBInfo->VBInfo & SwitchCRT2)) {
6682                                 XGI_SetCRT1Group(xgifb_info,
6683                                                 HwDeviceExtension, ModeNo,
6684                                                 ModeIdIndex, pVBInfo);
6685                                 if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA) {
6686                                         XGI_SetLCDAGroup(ModeNo, ModeIdIndex,
6687                                                         HwDeviceExtension,
6688                                                         pVBInfo);
6689                                 }
6690                         }
6691                 }
6692
6693                 if (pVBInfo->VBInfo & (SetSimuScanMode | SwitchCRT2)) {
6694                         switch (HwDeviceExtension->ujVBChipID) {
6695                         case VB_CHIP_301:
6696                                 XGI_SetCRT2Group301(ModeNo, HwDeviceExtension,
6697                                                 pVBInfo); /*add for CRT2 */
6698                                 break;
6699
6700                         case VB_CHIP_302:
6701                                 XGI_SetCRT2Group301(ModeNo, HwDeviceExtension,
6702                                                 pVBInfo); /*add for CRT2 */
6703                                 break;
6704
6705                         default:
6706                                 break;
6707                         }
6708                 }
6709
6710                 XGI_SetCRT2ModeRegs(ModeNo, HwDeviceExtension, pVBInfo);
6711                 XGI_OEM310Setting(ModeNo, ModeIdIndex, pVBInfo); /*0212*/
6712                 XGI_EnableBridge(xgifb_info, HwDeviceExtension, pVBInfo);
6713         } /* !XG20 */
6714         else {
6715                 if (pVBInfo->IF_DEF_LVDS == 1)
6716                         if (!XGI_XG21CheckLVDSMode(xgifb_info, ModeNo,
6717                                                    ModeIdIndex,
6718                                                    pVBInfo))
6719                                 return 0;
6720
6721                 pVBInfo->ModeType = pVBInfo->EModeIDTable[ModeIdIndex].
6722                                                 Ext_ModeFlag & ModeTypeMask;
6723
6724                 pVBInfo->SetFlag = 0;
6725                 pVBInfo->VBInfo = DisableCRT2Display;
6726
6727                 XGI_DisplayOff(xgifb_info, HwDeviceExtension, pVBInfo);
6728
6729                 XGI_SetCRT1Group(xgifb_info, HwDeviceExtension, ModeNo,
6730                                 ModeIdIndex, pVBInfo);
6731
6732                 XGI_DisplayOn(xgifb_info, HwDeviceExtension, pVBInfo);
6733         }
6734
6735         XGI_UpdateModeInfo(HwDeviceExtension, pVBInfo);
6736
6737         if (HwDeviceExtension->jChipType < XG20) {
6738                 XGI_LockCRT2(HwDeviceExtension, pVBInfo);
6739         }
6740
6741         return 1;
6742 }