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