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