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