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