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