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