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