]> rtime.felk.cvut.cz Git - linux-imx.git/blob - drivers/staging/xgifb/vb_setmode.c
staging: xgifb: XGI_GetVCLK2Ptr(): 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->IF_DEF_LVDS == 0) {
1998                 if (pVBInfo->VBType &
1999                     (VB_SIS302B |
2000                      VB_SIS301LV |
2001                      VB_SIS302LV |
2002                      VB_XGI301C)) {
2003                         if (temp & EnableDualEdge) {
2004                                 tempbx |= SetCRT2ToDualEdge;
2005                                 if (temp & SetToLCDA)
2006                                         tempbx |= XGI_SetCRT2ToLCDA;
2007                         }
2008                 }
2009         }
2010
2011         if (pVBInfo->IF_DEF_YPbPr == 1) {
2012                 if (((pVBInfo->IF_DEF_LVDS == 0) &&
2013                      ((pVBInfo->VBType & VB_SIS301LV) ||
2014                       (pVBInfo->VBType & VB_SIS302LV) ||
2015                       (pVBInfo->VBType & VB_XGI301C)))) {
2016                         if (temp & SetYPbPr) {
2017                                 if (pVBInfo->IF_DEF_HiVision == 1) {
2018                                         /* shampoo add for new
2019                                          * scratch */
2020                                         temp = xgifb_reg_get(
2021                                                 pVBInfo->P3d4,
2022                                                 0x35);
2023                                         temp &= YPbPrMode;
2024                                         tempbx |= SetCRT2ToHiVision;
2025
2026                                         if (temp != YPbPrMode1080i) {
2027                                                 tempbx &=
2028                                                         (~SetCRT2ToHiVision);
2029                                                 tempbx |=
2030                                                         SetCRT2ToYPbPr525750;
2031                                         }
2032                                 }
2033                         }
2034                 }
2035         }
2036
2037         tempax = push; /* restore CR31 */
2038
2039         if (pVBInfo->IF_DEF_LVDS == 0) {
2040                 if (pVBInfo->IF_DEF_YPbPr == 1) {
2041                         if (pVBInfo->IF_DEF_HiVision == 1)
2042                                 temp = 0x09FC;
2043                         else
2044                                 temp = 0x097C;
2045                 } else if (pVBInfo->IF_DEF_HiVision == 1) {
2046                         temp = 0x01FC;
2047                 } else {
2048                         temp = 0x017C;
2049                 }
2050         } else { /* 3rd party chip */
2051                 temp = SetCRT2ToLCD;
2052         }
2053
2054         if (!(tempbx & temp)) {
2055                 tempax |= DisableCRT2Display;
2056                 tempbx = 0;
2057         }
2058
2059         if (!(pVBInfo->VBType & VB_NoLCD)) {
2060                 if (tempbx & XGI_SetCRT2ToLCDA) {
2061                         if (tempbx & SetSimuScanMode)
2062                                 tempbx &= (~(SetCRT2ToLCD |
2063                                              SetCRT2ToRAMDAC |
2064                                              SwitchCRT2));
2065                         else
2066                                 tempbx &= (~(SetCRT2ToLCD |
2067                                              SetCRT2ToRAMDAC |
2068                                              SetCRT2ToTV |
2069                                              SwitchCRT2));
2070                 }
2071         }
2072
2073         /* shampoo add */
2074         /* for driver abnormal */
2075         if (!(tempbx & (SwitchCRT2 | SetSimuScanMode))) {
2076                 if (pVBInfo->IF_DEF_CRT2Monitor == 1) {
2077                         if (tempbx & SetCRT2ToRAMDAC) {
2078                                 tempbx &= (0xFF00 |
2079                                            SetCRT2ToRAMDAC |
2080                                            SwitchCRT2 |
2081                                            SetSimuScanMode);
2082                                 tempbx &= (0x00FF | (~SetCRT2ToYPbPr525750));
2083                         }
2084                 } else {
2085                         tempbx &= (~(SetCRT2ToRAMDAC |
2086                                      SetCRT2ToLCD |
2087                                      SetCRT2ToTV));
2088                 }
2089         }
2090
2091         if (!(pVBInfo->VBType & VB_NoLCD)) {
2092                 if (tempbx & SetCRT2ToLCD) {
2093                         tempbx &= (0xFF00 |
2094                                    SetCRT2ToLCD |
2095                                    SwitchCRT2 |
2096                                    SetSimuScanMode);
2097                         tempbx &= (0x00FF | (~SetCRT2ToYPbPr525750));
2098                 }
2099         }
2100
2101         if (tempbx & SetCRT2ToSCART) {
2102                 tempbx &= (0xFF00 |
2103                            SetCRT2ToSCART |
2104                            SwitchCRT2 |
2105                            SetSimuScanMode);
2106                 tempbx &= (0x00FF | (~SetCRT2ToYPbPr525750));
2107         }
2108
2109         if (pVBInfo->IF_DEF_YPbPr == 1) {
2110                 if (tempbx & SetCRT2ToYPbPr525750)
2111                         tempbx &= (0xFF00 |
2112                                    SwitchCRT2 |
2113                                    SetSimuScanMode);
2114         }
2115
2116         if (pVBInfo->IF_DEF_HiVision == 1) {
2117                 if (tempbx & SetCRT2ToHiVision)
2118                         tempbx &= (0xFF00 |
2119                                    SetCRT2ToHiVision |
2120                                    SwitchCRT2 |
2121                                    SetSimuScanMode);
2122         }
2123
2124         if (tempax & DisableCRT2Display) { /* Set Display Device Info */
2125                 if (!(tempbx & (SwitchCRT2 | SetSimuScanMode)))
2126                         tempbx = DisableCRT2Display;
2127         }
2128
2129         if (!(tempbx & DisableCRT2Display)) {
2130                 if ((!(tempbx & DriverMode)) ||
2131                     (!(modeflag & CRT2Mode))) {
2132                         if (!(tempbx & XGI_SetCRT2ToLCDA))
2133                                 tempbx |= (SetInSlaveMode |
2134                                            SetSimuScanMode);
2135                 }
2136
2137                 /* LCD+TV can't support in slave mode
2138                  * (Force LCDA+TV->LCDB) */
2139                 if ((tempbx & SetInSlaveMode) &&
2140                     (tempbx & XGI_SetCRT2ToLCDA)) {
2141                         tempbx ^= (SetCRT2ToLCD |
2142                                    XGI_SetCRT2ToLCDA |
2143                                    SetCRT2ToDualEdge);
2144                         pVBInfo->SetFlag |= ReserveTVOption;
2145                 }
2146         }
2147
2148         pVBInfo->VBInfo = tempbx;
2149 }
2150
2151 static void XGI_GetTVInfo(unsigned short ModeNo, unsigned short ModeIdIndex,
2152                 struct vb_device_info *pVBInfo)
2153 {
2154         unsigned short tempbx = 0, resinfo = 0, modeflag, index1;
2155
2156         if (pVBInfo->VBInfo & SetCRT2ToTV) {
2157                 modeflag = XGI330_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
2158                 resinfo = XGI330_EModeIDTable[ModeIdIndex].Ext_RESINFO;
2159
2160                 tempbx = xgifb_reg_get(pVBInfo->P3d4, 0x35);
2161                 if (tempbx & TVSetPAL) {
2162                         tempbx &= (SetCHTVOverScan |
2163                                    TVSetPALM |
2164                                    TVSetPALN |
2165                                    TVSetPAL);
2166                         if (tempbx & TVSetPALM)
2167                                 /* set to NTSC if PAL-M */
2168                                 tempbx &= ~TVSetPAL;
2169                 } else
2170                         tempbx &= (SetCHTVOverScan |
2171                                    TVSetNTSCJ |
2172                                    TVSetPAL);
2173
2174                 if (pVBInfo->IF_DEF_LVDS == 0) {
2175                         if (pVBInfo->VBInfo & SetCRT2ToSCART)
2176                                 tempbx |= TVSetPAL;
2177                 }
2178
2179                 if (pVBInfo->IF_DEF_YPbPr == 1) {
2180                         if (pVBInfo->VBInfo & SetCRT2ToYPbPr525750) {
2181                                 index1 = xgifb_reg_get(pVBInfo->P3d4, 0x35);
2182                                 index1 &= YPbPrMode;
2183
2184                                 if (index1 == YPbPrMode525i)
2185                                         tempbx |= TVSetYPbPr525i;
2186
2187                                 if (index1 == YPbPrMode525p)
2188                                         tempbx = tempbx | TVSetYPbPr525p;
2189                                 if (index1 == YPbPrMode750p)
2190                                         tempbx = tempbx | TVSetYPbPr750p;
2191                         }
2192                 }
2193
2194                 if (pVBInfo->IF_DEF_HiVision == 1) {
2195                         if (pVBInfo->VBInfo & SetCRT2ToHiVision)
2196                                 tempbx = tempbx | TVSetHiVision | TVSetPAL;
2197                 }
2198
2199                 if (pVBInfo->IF_DEF_LVDS == 0) { /* shampoo */
2200                         if ((pVBInfo->VBInfo & SetInSlaveMode) &&
2201                             (!(pVBInfo->VBInfo & SetNotSimuMode)))
2202                                 tempbx |= TVSimuMode;
2203
2204                         if (!(tempbx & TVSetPAL) &&
2205                             (modeflag > 13) &&
2206                             (resinfo == 8)) /* NTSC 1024x768, */
2207                                 tempbx |= NTSC1024x768;
2208
2209                         tempbx |= RPLLDIV2XO;
2210
2211                         if (pVBInfo->VBInfo & SetCRT2ToHiVision) {
2212                                 if (pVBInfo->VBInfo & SetInSlaveMode)
2213                                         tempbx &= (~RPLLDIV2XO);
2214                         } else if (tempbx &
2215                                     (TVSetYPbPr525p | TVSetYPbPr750p)) {
2216                                         tempbx &= (~RPLLDIV2XO);
2217                         } else if (!(pVBInfo->VBType &
2218                                          (VB_SIS301B |
2219                                           VB_SIS302B |
2220                                           VB_SIS301LV |
2221                                           VB_SIS302LV |
2222                                           VB_XGI301C))) {
2223                                 if (tempbx & TVSimuMode)
2224                                         tempbx &= (~RPLLDIV2XO);
2225                         }
2226                 }
2227         }
2228         pVBInfo->TVInfo = tempbx;
2229 }
2230
2231 static unsigned char XGI_GetLCDInfo(unsigned short ModeNo,
2232                 unsigned short ModeIdIndex, struct vb_device_info *pVBInfo)
2233 {
2234         unsigned short temp, tempax, tempbx, resinfo = 0, LCDIdIndex;
2235
2236         pVBInfo->LCDResInfo = 0;
2237         pVBInfo->LCDTypeInfo = 0;
2238         pVBInfo->LCDInfo = 0;
2239
2240         /* si+Ext_ResInfo // */
2241         resinfo = XGI330_EModeIDTable[ModeIdIndex].Ext_RESINFO;
2242         temp = xgifb_reg_get(pVBInfo->P3d4, 0x36); /* Get LCD Res.Info */
2243         tempbx = temp & 0x0F;
2244
2245         if (tempbx == 0)
2246                 tempbx = Panel_1024x768; /* default */
2247
2248         /* LCD75 */
2249         if ((tempbx == Panel_1024x768) || (tempbx == Panel_1280x1024)) {
2250                 if (pVBInfo->VBInfo & DriverMode) {
2251                         tempax = xgifb_reg_get(pVBInfo->P3d4, 0x33);
2252                         if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA)
2253                                 tempax &= 0x0F;
2254                         else
2255                                 tempax = tempax >> 4;
2256
2257                         if ((resinfo == 6) || (resinfo == 9)) {
2258                                 if (tempax >= 3)
2259                                         tempbx |= PanelRef75Hz;
2260                         } else if ((resinfo == 7) || (resinfo == 8)) {
2261                                 if (tempax >= 4)
2262                                         tempbx |= PanelRef75Hz;
2263                         }
2264                 }
2265         }
2266
2267         pVBInfo->LCDResInfo = tempbx;
2268
2269         /* End of LCD75 */
2270
2271         if (!(pVBInfo->VBInfo & (SetCRT2ToLCD | XGI_SetCRT2ToLCDA)))
2272                 return 0;
2273
2274         tempbx = 0;
2275
2276         temp = xgifb_reg_get(pVBInfo->P3d4, 0x37);
2277
2278         temp &= (ScalingLCD | LCDNonExpanding | LCDSyncBit | SetPWDEnable);
2279
2280         tempbx |= temp;
2281
2282         LCDIdIndex = XGI_GetLCDCapPtr1(pVBInfo);
2283
2284         tempax = pVBInfo->LCDCapList[LCDIdIndex].LCD_Capability;
2285
2286         if (pVBInfo->IF_DEF_LVDS == 0) { /* shampoo */
2287                 if (((pVBInfo->VBType & VB_SIS302LV) || (pVBInfo->VBType
2288                                 & VB_XGI301C)) && (tempax & XGI_LCDDualLink)) {
2289                         tempbx |= SetLCDDualLink;
2290                 }
2291
2292                 if ((pVBInfo->LCDResInfo == Panel_1400x1050) && (pVBInfo->VBInfo
2293                                 & SetCRT2ToLCD) && (resinfo == 9) &&
2294                                 (!(tempbx & EnableScalingLCD)))
2295                         /*
2296                          * set to center in 1280x1024 LCDB
2297                          * for Panel_1400x1050
2298                          */
2299                         tempbx |= SetLCDtoNonExpanding;
2300         }
2301
2302         if (pVBInfo->VBInfo & SetInSlaveMode) {
2303                 if (pVBInfo->VBInfo & SetNotSimuMode)
2304                         tempbx |= XGI_LCDVESATiming;
2305         } else {
2306                 tempbx |= XGI_LCDVESATiming;
2307         }
2308
2309         pVBInfo->LCDInfo = tempbx;
2310
2311         return 1;
2312 }
2313
2314 unsigned char XGI_SearchModeID(unsigned short ModeNo,
2315                 unsigned short *ModeIdIndex, struct vb_device_info *pVBInfo)
2316 {
2317         for (*ModeIdIndex = 0;; (*ModeIdIndex)++) {
2318                 if (XGI330_EModeIDTable[*ModeIdIndex].Ext_ModeID == ModeNo)
2319                         break;
2320                 if (XGI330_EModeIDTable[*ModeIdIndex].Ext_ModeID == 0xFF)
2321                         return 0;
2322         }
2323
2324         return 1;
2325 }
2326
2327 static unsigned char XG21GPIODataTransfer(unsigned char ujDate)
2328 {
2329         unsigned char ujRet = 0;
2330         unsigned char i = 0;
2331
2332         for (i = 0; i < 8; i++) {
2333                 ujRet = ujRet << 1;
2334                 ujRet |= (ujDate >> i) & 1;
2335         }
2336
2337         return ujRet;
2338 }
2339
2340 /*----------------------------------------------------------------------------*/
2341 /* output                                                                     */
2342 /*      bl[5] : LVDS signal                                                   */
2343 /*      bl[1] : LVDS backlight                                                */
2344 /*      bl[0] : LVDS VDD                                                      */
2345 /*----------------------------------------------------------------------------*/
2346 static unsigned char XGI_XG21GetPSCValue(struct vb_device_info *pVBInfo)
2347 {
2348         unsigned char CR4A, temp;
2349
2350         CR4A = xgifb_reg_get(pVBInfo->P3d4, 0x4A);
2351         xgifb_reg_and(pVBInfo->P3d4, 0x4A, ~0x23); /* enable GPIO write */
2352
2353         temp = xgifb_reg_get(pVBInfo->P3d4, 0x48);
2354
2355         temp = XG21GPIODataTransfer(temp);
2356         temp &= 0x23;
2357         xgifb_reg_set(pVBInfo->P3d4, 0x4A, CR4A);
2358         return temp;
2359 }
2360
2361 /*----------------------------------------------------------------------------*/
2362 /* output                                                                     */
2363 /*      bl[5] : LVDS signal                                                   */
2364 /*      bl[1] : LVDS backlight                                                */
2365 /*      bl[0] : LVDS VDD                                                      */
2366 /*----------------------------------------------------------------------------*/
2367 static unsigned char XGI_XG27GetPSCValue(struct vb_device_info *pVBInfo)
2368 {
2369         unsigned char CR4A, CRB4, temp;
2370
2371         CR4A = xgifb_reg_get(pVBInfo->P3d4, 0x4A);
2372         xgifb_reg_and(pVBInfo->P3d4, 0x4A, ~0x0C); /* enable GPIO write */
2373
2374         temp = xgifb_reg_get(pVBInfo->P3d4, 0x48);
2375
2376         temp &= 0x0C;
2377         temp >>= 2;
2378         xgifb_reg_set(pVBInfo->P3d4, 0x4A, CR4A);
2379         CRB4 = xgifb_reg_get(pVBInfo->P3d4, 0xB4);
2380         temp |= ((CRB4 & 0x04) << 3);
2381         return temp;
2382 }
2383
2384 /*----------------------------------------------------------------------------*/
2385 /* input                                                                      */
2386 /*      bl[5] : 1;LVDS signal on                                              */
2387 /*      bl[1] : 1;LVDS backlight on                                           */
2388 /*      bl[0] : 1:LVDS VDD on                                                 */
2389 /*      bh: 100000b : clear bit 5, to set bit5                                */
2390 /*          000010b : clear bit 1, to set bit1                                */
2391 /*          000001b : clear bit 0, to set bit0                                */
2392 /*----------------------------------------------------------------------------*/
2393 static void XGI_XG21BLSignalVDD(unsigned short tempbh, unsigned short tempbl,
2394                 struct vb_device_info *pVBInfo)
2395 {
2396         unsigned char CR4A, temp;
2397
2398         CR4A = xgifb_reg_get(pVBInfo->P3d4, 0x4A);
2399         tempbh &= 0x23;
2400         tempbl &= 0x23;
2401         xgifb_reg_and(pVBInfo->P3d4, 0x4A, ~tempbh); /* enable GPIO write */
2402
2403         if (tempbh & 0x20) {
2404                 temp = (tempbl >> 4) & 0x02;
2405
2406                 /* CR B4[1] */
2407                 xgifb_reg_and_or(pVBInfo->P3d4, 0xB4, ~0x02, temp);
2408
2409         }
2410
2411         temp = xgifb_reg_get(pVBInfo->P3d4, 0x48);
2412
2413         temp = XG21GPIODataTransfer(temp);
2414         temp &= ~tempbh;
2415         temp |= tempbl;
2416         xgifb_reg_set(pVBInfo->P3d4, 0x48, temp);
2417 }
2418
2419 static void XGI_XG27BLSignalVDD(unsigned short tempbh, unsigned short tempbl,
2420                 struct vb_device_info *pVBInfo)
2421 {
2422         unsigned char CR4A, temp;
2423         unsigned short tempbh0, tempbl0;
2424
2425         tempbh0 = tempbh;
2426         tempbl0 = tempbl;
2427         tempbh0 &= 0x20;
2428         tempbl0 &= 0x20;
2429         tempbh0 >>= 3;
2430         tempbl0 >>= 3;
2431
2432         if (tempbh & 0x20) {
2433                 temp = (tempbl >> 4) & 0x02;
2434
2435                 /* CR B4[1] */
2436                 xgifb_reg_and_or(pVBInfo->P3d4, 0xB4, ~0x02, temp);
2437
2438         }
2439         xgifb_reg_and_or(pVBInfo->P3d4, 0xB4, ~tempbh0, tempbl0);
2440
2441         CR4A = xgifb_reg_get(pVBInfo->P3d4, 0x4A);
2442         tempbh &= 0x03;
2443         tempbl &= 0x03;
2444         tempbh <<= 2;
2445         tempbl <<= 2; /* GPIOC,GPIOD */
2446         xgifb_reg_and(pVBInfo->P3d4, 0x4A, ~tempbh); /* enable GPIO write */
2447         xgifb_reg_and_or(pVBInfo->P3d4, 0x48, ~tempbh, tempbl);
2448 }
2449
2450 static void XGI_DisplayOn(struct xgifb_video_info *xgifb_info,
2451                 struct xgi_hw_device_info *pXGIHWDE,
2452                 struct vb_device_info *pVBInfo)
2453 {
2454
2455         xgifb_reg_and_or(pVBInfo->P3c4, 0x01, 0xDF, 0x00);
2456         if (pXGIHWDE->jChipType == XG21) {
2457                 if (pVBInfo->IF_DEF_LVDS == 1) {
2458                         if (!(XGI_XG21GetPSCValue(pVBInfo) & 0x1)) {
2459                                 /* LVDS VDD on */
2460                                 XGI_XG21BLSignalVDD(0x01, 0x01, pVBInfo);
2461                                 mdelay(xgifb_info->lvds_data.PSC_S2);
2462                         }
2463                         if (!(XGI_XG21GetPSCValue(pVBInfo) & 0x20))
2464                                 /* LVDS signal on */
2465                                 XGI_XG21BLSignalVDD(0x20, 0x20, pVBInfo);
2466                         mdelay(xgifb_info->lvds_data.PSC_S3);
2467                         /* LVDS backlight on */
2468                         XGI_XG21BLSignalVDD(0x02, 0x02, pVBInfo);
2469                 } else {
2470                         /* DVO/DVI signal on */
2471                         XGI_XG21BLSignalVDD(0x20, 0x20, pVBInfo);
2472                 }
2473
2474         }
2475
2476         if (pXGIHWDE->jChipType == XG27) {
2477                 if (pVBInfo->IF_DEF_LVDS == 1) {
2478                         if (!(XGI_XG27GetPSCValue(pVBInfo) & 0x1)) {
2479                                 /* LVDS VDD on */
2480                                 XGI_XG27BLSignalVDD(0x01, 0x01, pVBInfo);
2481                                 mdelay(xgifb_info->lvds_data.PSC_S2);
2482                         }
2483                         if (!(XGI_XG27GetPSCValue(pVBInfo) & 0x20))
2484                                 /* LVDS signal on */
2485                                 XGI_XG27BLSignalVDD(0x20, 0x20, pVBInfo);
2486                         mdelay(xgifb_info->lvds_data.PSC_S3);
2487                         /* LVDS backlight on */
2488                         XGI_XG27BLSignalVDD(0x02, 0x02, pVBInfo);
2489                 } else {
2490                         /* DVO/DVI signal on */
2491                         XGI_XG27BLSignalVDD(0x20, 0x20, pVBInfo);
2492                 }
2493
2494         }
2495 }
2496
2497 void XGI_DisplayOff(struct xgifb_video_info *xgifb_info,
2498                 struct xgi_hw_device_info *pXGIHWDE,
2499                 struct vb_device_info *pVBInfo)
2500 {
2501
2502         if (pXGIHWDE->jChipType == XG21) {
2503                 if (pVBInfo->IF_DEF_LVDS == 1) {
2504                         /* LVDS backlight off */
2505                         XGI_XG21BLSignalVDD(0x02, 0x00, pVBInfo);
2506                         mdelay(xgifb_info->lvds_data.PSC_S3);
2507                 } else {
2508                         /* DVO/DVI signal off */
2509                         XGI_XG21BLSignalVDD(0x20, 0x00, pVBInfo);
2510                 }
2511         }
2512
2513         if (pXGIHWDE->jChipType == XG27) {
2514                 if ((XGI_XG27GetPSCValue(pVBInfo) & 0x2)) {
2515                         /* LVDS backlight off */
2516                         XGI_XG27BLSignalVDD(0x02, 0x00, pVBInfo);
2517                         mdelay(xgifb_info->lvds_data.PSC_S3);
2518                 }
2519
2520                 if (pVBInfo->IF_DEF_LVDS == 0)
2521                         /* DVO/DVI signal off */
2522                         XGI_XG27BLSignalVDD(0x20, 0x00, pVBInfo);
2523         }
2524
2525         xgifb_reg_and_or(pVBInfo->P3c4, 0x01, 0xDF, 0x20);
2526 }
2527
2528 static void XGI_WaitDisply(struct vb_device_info *pVBInfo)
2529 {
2530         while ((inb(pVBInfo->P3da) & 0x01))
2531                 break;
2532
2533         while (!(inb(pVBInfo->P3da) & 0x01))
2534                 break;
2535 }
2536
2537 static void XGI_AutoThreshold(struct vb_device_info *pVBInfo)
2538 {
2539         xgifb_reg_or(pVBInfo->Part1Port, 0x01, 0x40);
2540 }
2541
2542 static void XGI_SaveCRT2Info(unsigned short ModeNo,
2543                              struct vb_device_info *pVBInfo)
2544 {
2545         unsigned short temp1, temp2;
2546
2547         /* reserve CR34 for CRT1 Mode No */
2548         xgifb_reg_set(pVBInfo->P3d4, 0x34, ModeNo);
2549         temp1 = (pVBInfo->VBInfo & SetInSlaveMode) >> 8;
2550         temp2 = ~(SetInSlaveMode >> 8);
2551         xgifb_reg_and_or(pVBInfo->P3d4, 0x31, temp2, temp1);
2552 }
2553
2554 static void XGI_GetCRT2ResInfo(unsigned short ModeNo,
2555                                unsigned short ModeIdIndex,
2556                                struct vb_device_info *pVBInfo)
2557 {
2558         unsigned short xres, yres, modeflag, resindex;
2559
2560         resindex = XGI330_EModeIDTable[ModeIdIndex].Ext_RESINFO;
2561         xres = XGI330_ModeResInfo[resindex].HTotal; /* xres->ax */
2562         yres = XGI330_ModeResInfo[resindex].VTotal; /* yres->bx */
2563         /* si+St_ModeFlag */
2564         modeflag = XGI330_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
2565
2566         if (modeflag & HalfDCLK)
2567                 xres *= 2;
2568
2569         if (modeflag & DoubleScanMode)
2570                 yres *= 2;
2571
2572         if (!(pVBInfo->VBInfo & SetCRT2ToLCD))
2573                 goto exit;
2574
2575         if (pVBInfo->IF_DEF_LVDS == 0) {
2576                 if (pVBInfo->LCDResInfo == Panel_1600x1200) {
2577                         if (!(pVBInfo->LCDInfo & XGI_LCDVESATiming)) {
2578                                 if (yres == 1024)
2579                                         yres = 1056;
2580                         }
2581                 }
2582
2583                 if (pVBInfo->LCDResInfo == Panel_1280x1024) {
2584                         if (yres == 400)
2585                                 yres = 405;
2586                         else if (yres == 350)
2587                                 yres = 360;
2588
2589                         if (pVBInfo->LCDInfo & XGI_LCDVESATiming) {
2590                                 if (yres == 360)
2591                                         yres = 375;
2592                         }
2593                 }
2594
2595                 if (pVBInfo->LCDResInfo == Panel_1024x768) {
2596                         if (!(pVBInfo->LCDInfo & XGI_LCDVESATiming)) {
2597                                 if (!(pVBInfo->LCDInfo & LCDNonExpanding)) {
2598                                         if (yres == 350)
2599                                                 yres = 357;
2600                                         else if (yres == 400)
2601                                                 yres = 420;
2602                                         else if (yres == 480)
2603                                                 yres = 525;
2604                                 }
2605                         }
2606                 }
2607         }
2608
2609         if (xres == 720)
2610                 xres = 640;
2611
2612 exit:
2613         pVBInfo->VGAHDE = xres;
2614         pVBInfo->HDE = xres;
2615         pVBInfo->VGAVDE = yres;
2616         pVBInfo->VDE = yres;
2617 }
2618
2619 static unsigned char XGI_IsLCDDualLink(struct vb_device_info *pVBInfo)
2620 {
2621
2622         if ((pVBInfo->VBInfo & (SetCRT2ToLCD | XGI_SetCRT2ToLCDA)) &&
2623                         (pVBInfo->LCDInfo & SetLCDDualLink)) /* shampoo0129 */
2624                 return 1;
2625
2626         return 0;
2627 }
2628
2629 static void XGI_GetRAMDAC2DATA(unsigned short ModeNo,
2630                                unsigned short ModeIdIndex,
2631                                unsigned short RefreshRateTableIndex,
2632                                struct vb_device_info *pVBInfo)
2633 {
2634         unsigned short tempax, tempbx, temp1, temp2, modeflag = 0, tempcx,
2635                         CRT1Index;
2636
2637         pVBInfo->RVBHCMAX = 1;
2638         pVBInfo->RVBHCFACT = 1;
2639         modeflag = XGI330_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
2640         CRT1Index = XGI330_RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
2641         CRT1Index &= IndexMask;
2642         temp1 = (unsigned short) XGI_CRT1Table[CRT1Index].CR[0];
2643         temp2 = (unsigned short) XGI_CRT1Table[CRT1Index].CR[5];
2644         tempax = (temp1 & 0xFF) | ((temp2 & 0x03) << 8);
2645         tempbx = (unsigned short) XGI_CRT1Table[CRT1Index].CR[8];
2646         tempcx = (unsigned short)
2647                         XGI_CRT1Table[CRT1Index].CR[14] << 8;
2648         tempcx &= 0x0100;
2649         tempcx = tempcx << 2;
2650         tempbx |= tempcx;
2651         temp1 = (unsigned short) XGI_CRT1Table[CRT1Index].CR[9];
2652
2653         if (temp1 & 0x01)
2654                 tempbx |= 0x0100;
2655
2656         if (temp1 & 0x20)
2657                 tempbx |= 0x0200;
2658         tempax += 5;
2659
2660         if (modeflag & Charx8Dot)
2661                 tempax *= 8;
2662         else
2663                 tempax *= 9;
2664
2665         pVBInfo->VGAHT = tempax;
2666         pVBInfo->HT = tempax;
2667         tempbx++;
2668         pVBInfo->VGAVT = tempbx;
2669         pVBInfo->VT = tempbx;
2670 }
2671
2672 static void XGI_GetCRT2Data(unsigned short ModeNo, unsigned short ModeIdIndex,
2673                 unsigned short RefreshRateTableIndex,
2674                 struct vb_device_info *pVBInfo)
2675 {
2676         unsigned short tempax = 0, tempbx = 0, modeflag, resinfo;
2677
2678         struct SiS_LCDData const *LCDPtr = NULL;
2679
2680         /* si+Ext_ResInfo */
2681         modeflag = XGI330_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
2682         resinfo = XGI330_EModeIDTable[ModeIdIndex].Ext_RESINFO;
2683         pVBInfo->NewFlickerMode = 0;
2684         pVBInfo->RVBHRS = 50;
2685
2686         if (pVBInfo->VBInfo & SetCRT2ToRAMDAC) {
2687                 XGI_GetRAMDAC2DATA(ModeNo, ModeIdIndex, RefreshRateTableIndex,
2688                                 pVBInfo);
2689                 return;
2690         }
2691
2692         if (pVBInfo->VBInfo & (SetCRT2ToLCD | XGI_SetCRT2ToLCDA)) {
2693                 LCDPtr = XGI_GetLcdPtr(XGI_LCDDataTable, ModeNo, ModeIdIndex,
2694                                        RefreshRateTableIndex, pVBInfo);
2695
2696                 pVBInfo->RVBHCMAX = LCDPtr->RVBHCMAX;
2697                 pVBInfo->RVBHCFACT = LCDPtr->RVBHCFACT;
2698                 pVBInfo->VGAHT = LCDPtr->VGAHT;
2699                 pVBInfo->VGAVT = LCDPtr->VGAVT;
2700                 pVBInfo->HT = LCDPtr->LCDHT;
2701                 pVBInfo->VT = LCDPtr->LCDVT;
2702
2703                 if (pVBInfo->LCDResInfo == Panel_1024x768) {
2704                         tempax = 1024;
2705                         tempbx = 768;
2706
2707                         if (!(pVBInfo->LCDInfo & XGI_LCDVESATiming)) {
2708                                 if (pVBInfo->VGAVDE == 357)
2709                                         tempbx = 527;
2710                                 else if (pVBInfo->VGAVDE == 420)
2711                                         tempbx = 620;
2712                                 else if (pVBInfo->VGAVDE == 525)
2713                                         tempbx = 775;
2714                                 else if (pVBInfo->VGAVDE == 600)
2715                                         tempbx = 775;
2716                                 else
2717                                         tempbx = 768;
2718                         } else
2719                                 tempbx = 768;
2720                 } else if (pVBInfo->LCDResInfo == Panel_1024x768x75) {
2721                         tempax = 1024;
2722                         tempbx = 768;
2723                 } else if (pVBInfo->LCDResInfo == Panel_1280x1024) {
2724                         tempax = 1280;
2725                         if (pVBInfo->VGAVDE == 360)
2726                                 tempbx = 768;
2727                         else if (pVBInfo->VGAVDE == 375)
2728                                 tempbx = 800;
2729                         else if (pVBInfo->VGAVDE == 405)
2730                                 tempbx = 864;
2731                         else
2732                                 tempbx = 1024;
2733                 } else if (pVBInfo->LCDResInfo == Panel_1280x1024x75) {
2734                         tempax = 1280;
2735                         tempbx = 1024;
2736                 } else if (pVBInfo->LCDResInfo == Panel_1280x960) {
2737                         tempax = 1280;
2738                         if (pVBInfo->VGAVDE == 350)
2739                                 tempbx = 700;
2740                         else if (pVBInfo->VGAVDE == 400)
2741                                 tempbx = 800;
2742                         else if (pVBInfo->VGAVDE == 1024)
2743                                 tempbx = 960;
2744                         else
2745                                 tempbx = 960;
2746                 } else if (pVBInfo->LCDResInfo == Panel_1400x1050) {
2747                         tempax = 1400;
2748                         tempbx = 1050;
2749
2750                         if (pVBInfo->VGAVDE == 1024) {
2751                                 tempax = 1280;
2752                                 tempbx = 1024;
2753                         }
2754                 } else if (pVBInfo->LCDResInfo == Panel_1600x1200) {
2755                         tempax = 1600;
2756                         tempbx = 1200; /* alan 10/14/2003 */
2757                         if (!(pVBInfo->LCDInfo & XGI_LCDVESATiming)) {
2758                                 if (pVBInfo->VGAVDE == 350)
2759                                         tempbx = 875;
2760                                 else if (pVBInfo->VGAVDE == 400)
2761                                         tempbx = 1000;
2762                         }
2763                 }
2764
2765                 if (pVBInfo->LCDInfo & LCDNonExpanding) {
2766                         tempax = pVBInfo->VGAHDE;
2767                         tempbx = pVBInfo->VGAVDE;
2768                 }
2769
2770                 pVBInfo->HDE = tempax;
2771                 pVBInfo->VDE = tempbx;
2772                 return;
2773         }
2774
2775         if (pVBInfo->VBInfo & (SetCRT2ToTV)) {
2776                 struct SiS_TVData const *TVPtr;
2777
2778                 TVPtr = XGI_GetTVPtr(ModeNo, ModeIdIndex, RefreshRateTableIndex,
2779                                      pVBInfo);
2780
2781                 pVBInfo->RVBHCMAX = TVPtr->RVBHCMAX;
2782                 pVBInfo->RVBHCFACT = TVPtr->RVBHCFACT;
2783                 pVBInfo->VGAHT = TVPtr->VGAHT;
2784                 pVBInfo->VGAVT = TVPtr->VGAVT;
2785                 pVBInfo->HDE = TVPtr->TVHDE;
2786                 pVBInfo->VDE = TVPtr->TVVDE;
2787                 pVBInfo->RVBHRS = TVPtr->RVBHRS;
2788                 pVBInfo->NewFlickerMode = TVPtr->FlickerMode;
2789
2790                 if (pVBInfo->VBInfo & SetCRT2ToHiVision) {
2791                         if (resinfo == 0x08)
2792                                 pVBInfo->NewFlickerMode = 0x40;
2793                         else if (resinfo == 0x09)
2794                                 pVBInfo->NewFlickerMode = 0x40;
2795                         else if (resinfo == 0x12)
2796                                 pVBInfo->NewFlickerMode = 0x40;
2797
2798                         if (pVBInfo->VGAVDE == 350)
2799                                 pVBInfo->TVInfo |= TVSimuMode;
2800
2801                         tempax = ExtHiTVHT;
2802                         tempbx = ExtHiTVVT;
2803
2804                         if (pVBInfo->VBInfo & SetInSlaveMode) {
2805                                 if (pVBInfo->TVInfo & TVSimuMode) {
2806                                         tempax = StHiTVHT;
2807                                         tempbx = StHiTVVT;
2808
2809                                         if (!(modeflag & Charx8Dot)) {
2810                                                 tempax = StHiTextTVHT;
2811                                                 tempbx = StHiTextTVVT;
2812                                         }
2813                                 }
2814                         }
2815                 } else if (pVBInfo->VBInfo & SetCRT2ToYPbPr525750) {
2816                         if (pVBInfo->TVInfo & TVSetYPbPr750p) {
2817                                 tempax = YPbPrTV750pHT; /* Ext750pTVHT */
2818                                 tempbx = YPbPrTV750pVT; /* Ext750pTVVT */
2819                         }
2820
2821                         if (pVBInfo->TVInfo & TVSetYPbPr525p) {
2822                                 tempax = YPbPrTV525pHT; /* Ext525pTVHT */
2823                                 tempbx = YPbPrTV525pVT; /* Ext525pTVVT */
2824                         } else if (pVBInfo->TVInfo & TVSetYPbPr525i) {
2825                                 tempax = YPbPrTV525iHT; /* Ext525iTVHT */
2826                                 tempbx = YPbPrTV525iVT; /* Ext525iTVVT */
2827                                 if (pVBInfo->TVInfo & NTSC1024x768)
2828                                         tempax = NTSC1024x768HT;
2829                         }
2830                 } else {
2831                         tempax = PALHT;
2832                         tempbx = PALVT;
2833                         if (!(pVBInfo->TVInfo & TVSetPAL)) {
2834                                 tempax = NTSCHT;
2835                                 tempbx = NTSCVT;
2836                                 if (pVBInfo->TVInfo & NTSC1024x768)
2837                                         tempax = NTSC1024x768HT;
2838                         }
2839                 }
2840
2841                 pVBInfo->HT = tempax;
2842                 pVBInfo->VT = tempbx;
2843                 return;
2844         }
2845 }
2846
2847 static void XGI_SetCRT2VCLK(unsigned short ModeNo, unsigned short ModeIdIndex,
2848                 unsigned short RefreshRateTableIndex,
2849                 struct vb_device_info *pVBInfo)
2850 {
2851         unsigned char di_0, di_1, tempal;
2852
2853         tempal = XGI_GetVCLKPtr(RefreshRateTableIndex, ModeNo, ModeIdIndex,
2854                         pVBInfo);
2855         XGI_GetVCLKLen(tempal, &di_0, &di_1, pVBInfo);
2856         XGI_GetLCDVCLKPtr(&di_0, &di_1, pVBInfo);
2857
2858         if (pVBInfo->VBType & VB_SIS301) { /* shampoo 0129 */
2859                 /* 301 */
2860                 xgifb_reg_set(pVBInfo->Part4Port, 0x0A, 0x10);
2861                 xgifb_reg_set(pVBInfo->Part4Port, 0x0B, di_1);
2862                 xgifb_reg_set(pVBInfo->Part4Port, 0x0A, di_0);
2863         } else { /* 301b/302b/301lv/302lv */
2864                 xgifb_reg_set(pVBInfo->Part4Port, 0x0A, di_0);
2865                 xgifb_reg_set(pVBInfo->Part4Port, 0x0B, di_1);
2866         }
2867
2868         xgifb_reg_set(pVBInfo->Part4Port, 0x00, 0x12);
2869
2870         if (pVBInfo->VBInfo & SetCRT2ToRAMDAC)
2871                 xgifb_reg_or(pVBInfo->Part4Port, 0x12, 0x28);
2872         else
2873                 xgifb_reg_or(pVBInfo->Part4Port, 0x12, 0x08);
2874 }
2875
2876 static unsigned short XGI_GetColorDepth(unsigned short ModeNo,
2877                 unsigned short ModeIdIndex, struct vb_device_info *pVBInfo)
2878 {
2879         unsigned short ColorDepth[6] = { 1, 2, 4, 4, 6, 8 };
2880         short index;
2881         unsigned short modeflag;
2882
2883         modeflag = XGI330_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
2884         index = (modeflag & ModeTypeMask) - ModeEGA;
2885
2886         if (index < 0)
2887                 index = 0;
2888
2889         return ColorDepth[index];
2890 }
2891
2892 static unsigned short XGI_GetOffset(unsigned short ModeNo,
2893                                     unsigned short ModeIdIndex,
2894                 unsigned short RefreshRateTableIndex,
2895                 struct xgi_hw_device_info *HwDeviceExtension,
2896                 struct vb_device_info *pVBInfo)
2897 {
2898         unsigned short temp, colordepth, modeinfo, index, infoflag,
2899                         ColorDepth[] = { 0x01, 0x02, 0x04 };
2900
2901         modeinfo = XGI330_EModeIDTable[ModeIdIndex].Ext_ModeInfo;
2902         infoflag = XGI330_RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
2903
2904         index = (modeinfo >> 8) & 0xFF;
2905
2906         temp = XGI330_ScreenOffset[index];
2907
2908         if (infoflag & InterlaceMode)
2909                 temp = temp << 1;
2910
2911         colordepth = XGI_GetColorDepth(ModeNo, ModeIdIndex, pVBInfo);
2912
2913         if ((ModeNo >= 0x7C) && (ModeNo <= 0x7E)) {
2914                 temp = ModeNo - 0x7C;
2915                 colordepth = ColorDepth[temp];
2916                 temp = 0x6B;
2917                 if (infoflag & InterlaceMode)
2918                         temp = temp << 1;
2919                 return temp * colordepth;
2920         } else {
2921                 return temp * colordepth;
2922         }
2923 }
2924
2925 static void XGI_SetCRT2Offset(unsigned short ModeNo,
2926                 unsigned short ModeIdIndex,
2927                 unsigned short RefreshRateTableIndex,
2928                 struct xgi_hw_device_info *HwDeviceExtension,
2929                 struct vb_device_info *pVBInfo)
2930 {
2931         unsigned short offset;
2932         unsigned char temp;
2933
2934         if (pVBInfo->VBInfo & SetInSlaveMode)
2935                 return;
2936
2937         offset = XGI_GetOffset(ModeNo, ModeIdIndex, RefreshRateTableIndex,
2938                         HwDeviceExtension, pVBInfo);
2939         temp = (unsigned char) (offset & 0xFF);
2940         xgifb_reg_set(pVBInfo->Part1Port, 0x07, temp);
2941         temp = (unsigned char) ((offset & 0xFF00) >> 8);
2942         xgifb_reg_set(pVBInfo->Part1Port, 0x09, temp);
2943         temp = (unsigned char) (((offset >> 3) & 0xFF) + 1);
2944         xgifb_reg_set(pVBInfo->Part1Port, 0x03, temp);
2945 }
2946
2947 static void XGI_SetCRT2FIFO(struct vb_device_info *pVBInfo)
2948 {
2949         /* threshold high ,disable auto threshold */
2950         xgifb_reg_set(pVBInfo->Part1Port, 0x01, 0x3B);
2951         /* threshold low default 04h */
2952         xgifb_reg_and_or(pVBInfo->Part1Port, 0x02, ~(0x3F), 0x04);
2953 }
2954
2955 static void XGI_PreSetGroup1(unsigned short ModeNo, unsigned short ModeIdIndex,
2956                 struct xgi_hw_device_info *HwDeviceExtension,
2957                 unsigned short RefreshRateTableIndex,
2958                 struct vb_device_info *pVBInfo)
2959 {
2960         u8 tempcx;
2961
2962         XGI_SetCRT2Offset(ModeNo, ModeIdIndex, RefreshRateTableIndex,
2963                         HwDeviceExtension, pVBInfo);
2964         XGI_SetCRT2FIFO(pVBInfo);
2965
2966         for (tempcx = 4; tempcx < 7; tempcx++)
2967                 xgifb_reg_set(pVBInfo->Part1Port, tempcx, 0x0);
2968
2969         xgifb_reg_set(pVBInfo->Part1Port, 0x50, 0x00);
2970         xgifb_reg_set(pVBInfo->Part1Port, 0x02, 0x44); /* temp 0206 */
2971 }
2972
2973 static void XGI_SetGroup1(unsigned short ModeNo, unsigned short ModeIdIndex,
2974                 struct xgi_hw_device_info *HwDeviceExtension,
2975                 unsigned short RefreshRateTableIndex,
2976                 struct vb_device_info *pVBInfo)
2977 {
2978         unsigned short temp = 0, tempax = 0, tempbx = 0, tempcx = 0,
2979                         pushbx = 0, CRT1Index, modeflag;
2980
2981         CRT1Index = XGI330_RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
2982         CRT1Index &= IndexMask;
2983         modeflag = XGI330_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
2984
2985         /* bainy change table name */
2986         if (modeflag & HalfDCLK) {
2987                 /* BTVGA2HT 0x08,0x09 */
2988                 temp = (pVBInfo->VGAHT / 2 - 1) & 0x0FF;
2989                 xgifb_reg_set(pVBInfo->Part1Port, 0x08, temp);
2990                 temp = (((pVBInfo->VGAHT / 2 - 1) & 0xFF00) >> 8) << 4;
2991                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x09, ~0x0F0, temp);
2992                 /* BTVGA2HDEE 0x0A,0x0C */
2993                 temp = (pVBInfo->VGAHDE / 2 + 16) & 0x0FF;
2994                 xgifb_reg_set(pVBInfo->Part1Port, 0x0A, temp);
2995                 tempcx = ((pVBInfo->VGAHT - pVBInfo->VGAHDE) / 2) >> 2;
2996                 pushbx = pVBInfo->VGAHDE / 2 + 16;
2997                 tempcx = tempcx >> 1;
2998                 tempbx = pushbx + tempcx; /* bx BTVGA@HRS 0x0B,0x0C */
2999                 tempcx += tempbx;
3000
3001                 if (pVBInfo->VBInfo & SetCRT2ToRAMDAC) {
3002                         tempbx = XGI_CRT1Table[CRT1Index].CR[4];
3003                         tempbx |= ((XGI_CRT1Table[CRT1Index].CR[14] &
3004                                                 0xC0) << 2);
3005                         tempbx = (tempbx - 3) << 3; /* (VGAHRS-3)*8 */
3006                         tempcx = XGI_CRT1Table[CRT1Index].CR[5];
3007                         tempcx &= 0x1F;
3008                         temp = XGI_CRT1Table[CRT1Index].CR[15];
3009                         temp = (temp & 0x04) << (5 - 2); /* VGAHRE D[5] */
3010                         tempcx = ((tempcx | temp) - 3) << 3; /* (VGAHRE-3)*8 */
3011                 }
3012
3013                 tempbx += 4;
3014                 tempcx += 4;
3015
3016                 if (tempcx > (pVBInfo->VGAHT / 2))
3017                         tempcx = pVBInfo->VGAHT / 2;
3018
3019                 temp = tempbx & 0x00FF;
3020
3021                 xgifb_reg_set(pVBInfo->Part1Port, 0x0B, temp);
3022         } else {
3023                 temp = (pVBInfo->VGAHT - 1) & 0x0FF; /* BTVGA2HT 0x08,0x09 */
3024                 xgifb_reg_set(pVBInfo->Part1Port, 0x08, temp);
3025                 temp = (((pVBInfo->VGAHT - 1) & 0xFF00) >> 8) << 4;
3026                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x09, ~0x0F0, temp);
3027                 /* BTVGA2HDEE 0x0A,0x0C */
3028                 temp = (pVBInfo->VGAHDE + 16) & 0x0FF;
3029                 xgifb_reg_set(pVBInfo->Part1Port, 0x0A, temp);
3030                 tempcx = (pVBInfo->VGAHT - pVBInfo->VGAHDE) >> 2; /* cx */
3031                 pushbx = pVBInfo->VGAHDE + 16;
3032                 tempcx = tempcx >> 1;
3033                 tempbx = pushbx + tempcx; /* bx BTVGA@HRS 0x0B,0x0C */
3034                 tempcx += tempbx;
3035
3036                 if (pVBInfo->VBInfo & SetCRT2ToRAMDAC) {
3037                         tempbx = XGI_CRT1Table[CRT1Index].CR[3];
3038                         tempbx |= ((XGI_CRT1Table[CRT1Index].CR[5] &
3039                                                 0xC0) << 2);
3040                         tempbx = (tempbx - 3) << 3; /* (VGAHRS-3)*8 */
3041                         tempcx = XGI_CRT1Table[CRT1Index].CR[4];
3042                         tempcx &= 0x1F;
3043                         temp = XGI_CRT1Table[CRT1Index].CR[6];
3044                         temp = (temp & 0x04) << (5 - 2); /* VGAHRE D[5] */
3045                         tempcx = ((tempcx | temp) - 3) << 3; /* (VGAHRE-3)*8 */
3046                         tempbx += 16;
3047                         tempcx += 16;
3048                 }
3049
3050                 if (tempcx > pVBInfo->VGAHT)
3051                         tempcx = pVBInfo->VGAHT;
3052
3053                 temp = tempbx & 0x00FF;
3054                 xgifb_reg_set(pVBInfo->Part1Port, 0x0B, temp);
3055         }
3056
3057         tempax = (tempax & 0x00FF) | (tempbx & 0xFF00);
3058         tempbx = pushbx;
3059         tempbx = (tempbx & 0x00FF) | ((tempbx & 0xFF00) << 4);
3060         tempax |= (tempbx & 0xFF00);
3061         temp = (tempax & 0xFF00) >> 8;
3062         xgifb_reg_set(pVBInfo->Part1Port, 0x0C, temp);
3063         temp = tempcx & 0x00FF;
3064         xgifb_reg_set(pVBInfo->Part1Port, 0x0D, temp);
3065         tempcx = (pVBInfo->VGAVT - 1);
3066         temp = tempcx & 0x00FF;
3067
3068         xgifb_reg_set(pVBInfo->Part1Port, 0x0E, temp);
3069         tempbx = pVBInfo->VGAVDE - 1;
3070         temp = tempbx & 0x00FF;
3071         xgifb_reg_set(pVBInfo->Part1Port, 0x0F, temp);
3072         temp = ((tempbx & 0xFF00) << 3) >> 8;
3073         temp |= ((tempcx & 0xFF00) >> 8);
3074         xgifb_reg_set(pVBInfo->Part1Port, 0x12, temp);
3075
3076         tempax = pVBInfo->VGAVDE;
3077         tempbx = pVBInfo->VGAVDE;
3078         tempcx = pVBInfo->VGAVT;
3079         /* BTVGA2VRS 0x10,0x11 */
3080         tempbx = (pVBInfo->VGAVT + pVBInfo->VGAVDE) >> 1;
3081         /* BTVGA2VRE 0x11 */
3082         tempcx = ((pVBInfo->VGAVT - pVBInfo->VGAVDE) >> 4) + tempbx + 1;
3083
3084         if (pVBInfo->VBInfo & SetCRT2ToRAMDAC) {
3085                 tempbx = XGI_CRT1Table[CRT1Index].CR[10];
3086                 temp = XGI_CRT1Table[CRT1Index].CR[9];
3087
3088                 if (temp & 0x04)
3089                         tempbx |= 0x0100;
3090
3091                 if (temp & 0x080)
3092                         tempbx |= 0x0200;
3093
3094                 temp = XGI_CRT1Table[CRT1Index].CR[14];
3095
3096                 if (temp & 0x08)
3097                         tempbx |= 0x0400;
3098
3099                 temp = XGI_CRT1Table[CRT1Index].CR[11];
3100                 tempcx = (tempcx & 0xFF00) | (temp & 0x00FF);
3101         }
3102
3103         temp = tempbx & 0x00FF;
3104         xgifb_reg_set(pVBInfo->Part1Port, 0x10, temp);
3105         temp = ((tempbx & 0xFF00) >> 8) << 4;
3106         temp = ((tempcx & 0x000F) | (temp));
3107         xgifb_reg_set(pVBInfo->Part1Port, 0x11, temp);
3108         tempax = 0;
3109
3110         if (modeflag & DoubleScanMode)
3111                 tempax |= 0x80;
3112
3113         if (modeflag & HalfDCLK)
3114                 tempax |= 0x40;
3115
3116         xgifb_reg_and_or(pVBInfo->Part1Port, 0x2C, ~0x0C0, tempax);
3117 }
3118
3119 static unsigned short XGI_GetVGAHT2(struct vb_device_info *pVBInfo)
3120 {
3121         unsigned long tempax, tempbx;
3122
3123         tempbx = ((pVBInfo->VGAVT - pVBInfo->VGAVDE) * pVBInfo->RVBHCMAX)
3124                         & 0xFFFF;
3125         tempax = (pVBInfo->VT - pVBInfo->VDE) * pVBInfo->RVBHCFACT;
3126         tempax = (tempax * pVBInfo->HT) / tempbx;
3127
3128         return (unsigned short) tempax;
3129 }
3130
3131 static void XGI_SetLockRegs(unsigned short ModeNo, unsigned short ModeIdIndex,
3132                 struct xgi_hw_device_info *HwDeviceExtension,
3133                 unsigned short RefreshRateTableIndex,
3134                 struct vb_device_info *pVBInfo)
3135 {
3136         unsigned short push1, push2, tempax, tempbx = 0, tempcx, temp, resinfo,
3137                         modeflag;
3138
3139         /* si+Ext_ResInfo */
3140         modeflag = XGI330_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
3141         resinfo = XGI330_EModeIDTable[ModeIdIndex].Ext_RESINFO;
3142
3143         if (!(pVBInfo->VBInfo & SetInSlaveMode))
3144                 return;
3145
3146         temp = 0xFF; /* set MAX HT */
3147         xgifb_reg_set(pVBInfo->Part1Port, 0x03, temp);
3148         tempcx = 0x08;
3149
3150         if (pVBInfo->VBType & (VB_SIS301LV | VB_SIS302LV | VB_XGI301C))
3151                 modeflag |= Charx8Dot;
3152
3153         tempax = pVBInfo->VGAHDE; /* 0x04 Horizontal Display End */
3154
3155         if (modeflag & HalfDCLK)
3156                 tempax = tempax >> 1;
3157
3158         tempax = (tempax / tempcx) - 1;
3159         tempbx |= ((tempax & 0x00FF) << 8);
3160         temp = tempax & 0x00FF;
3161         xgifb_reg_set(pVBInfo->Part1Port, 0x04, temp);
3162
3163         temp = (tempbx & 0xFF00) >> 8;
3164
3165         if (pVBInfo->VBInfo & SetCRT2ToTV) {
3166                 if (!(pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
3167                                 | VB_SIS302LV | VB_XGI301C)))
3168                         temp += 2;
3169
3170                 if ((pVBInfo->VBInfo & SetCRT2ToHiVision) &&
3171                     !(pVBInfo->VBType & VB_SIS301LV) && (resinfo == 7))
3172                                 temp -= 2;
3173         }
3174
3175         /* 0x05 Horizontal Display Start */
3176         xgifb_reg_set(pVBInfo->Part1Port, 0x05, temp);
3177         /* 0x06 Horizontal Blank end */
3178         xgifb_reg_set(pVBInfo->Part1Port, 0x06, 0x03);
3179
3180         if (!(pVBInfo->VBInfo & DisableCRT2Display)) { /* 030226 bainy */
3181                 if (pVBInfo->VBInfo & SetCRT2ToTV)
3182                         tempax = pVBInfo->VGAHT;
3183                 else
3184                         tempax = XGI_GetVGAHT2(pVBInfo);
3185         }
3186
3187         if (tempax >= pVBInfo->VGAHT)
3188                 tempax = pVBInfo->VGAHT;
3189
3190         if (modeflag & HalfDCLK)
3191                 tempax = tempax >> 1;
3192
3193         tempax = (tempax / tempcx) - 5;
3194         tempcx = tempax; /* 20030401 0x07 horizontal Retrace Start */
3195         if (pVBInfo->VBInfo & SetCRT2ToHiVision) {
3196                 temp = (tempbx & 0x00FF) - 1;
3197                 if (!(modeflag & HalfDCLK)) {
3198                         temp -= 6;
3199                         if (pVBInfo->TVInfo & TVSimuMode) {
3200                                 temp -= 4;
3201                                 temp -= 10;
3202                         }
3203                 }
3204         } else {
3205                 tempbx = (tempbx & 0xFF00) >> 8;
3206                 tempcx = (tempcx + tempbx) >> 1;
3207                 temp = (tempcx & 0x00FF) + 2;
3208
3209                 if (pVBInfo->VBInfo & SetCRT2ToTV) {
3210                         temp -= 1;
3211                         if (!(modeflag & HalfDCLK)) {
3212                                 if ((modeflag & Charx8Dot)) {
3213                                         temp += 4;
3214                                         if (pVBInfo->VGAHDE >= 800)
3215                                                 temp -= 6;
3216                                 }
3217                         }
3218                 } else if (!(modeflag & HalfDCLK)) {
3219                         temp -= 4;
3220                         if (pVBInfo->LCDResInfo != Panel_1280x960 &&
3221                             pVBInfo->VGAHDE >= 800) {
3222                                 temp -= 7;
3223                                 if (pVBInfo->VGAHDE >= 1280 &&
3224                                     pVBInfo->LCDResInfo != Panel_1280x960 &&
3225                                     (pVBInfo->LCDInfo & LCDNonExpanding))
3226                                         temp += 28;
3227                         }
3228                 }
3229         }
3230
3231         /* 0x07 Horizontal Retrace Start */
3232         xgifb_reg_set(pVBInfo->Part1Port, 0x07, temp);
3233         /* 0x08 Horizontal Retrace End */
3234         xgifb_reg_set(pVBInfo->Part1Port, 0x08, 0);
3235
3236         if (pVBInfo->VBInfo & SetCRT2ToTV) {
3237                 if (pVBInfo->TVInfo & TVSimuMode) {
3238                         if (ModeNo == 0x50) {
3239                                 if (pVBInfo->TVInfo & SetNTSCTV) {
3240                                         xgifb_reg_set(pVBInfo->Part1Port,
3241                                                         0x07, 0x30);
3242                                         xgifb_reg_set(pVBInfo->Part1Port,
3243                                                         0x08, 0x03);
3244                                 } else {
3245                                         xgifb_reg_set(pVBInfo->Part1Port,
3246                                                         0x07, 0x2f);
3247                                         xgifb_reg_set(pVBInfo->Part1Port,
3248                                                         0x08, 0x02);
3249                                 }
3250                         }
3251                 }
3252         }
3253
3254         xgifb_reg_set(pVBInfo->Part1Port, 0x18, 0x03); /* 0x18 SR0B */
3255         xgifb_reg_and_or(pVBInfo->Part1Port, 0x19, 0xF0, 0x00);
3256         xgifb_reg_set(pVBInfo->Part1Port, 0x09, 0xFF); /* 0x09 Set Max VT */
3257
3258         tempbx = pVBInfo->VGAVT;
3259         push1 = tempbx;
3260         tempcx = 0x121;
3261         tempbx = pVBInfo->VGAVDE; /* 0x0E Virtical Display End */
3262
3263         if (tempbx == 357)
3264                 tempbx = 350;
3265         if (tempbx == 360)
3266                 tempbx = 350;
3267         if (tempbx == 375)
3268                 tempbx = 350;
3269         if (tempbx == 405)
3270                 tempbx = 400;
3271         if (tempbx == 525)
3272                 tempbx = 480;
3273
3274         push2 = tempbx;
3275
3276         if (pVBInfo->VBInfo & SetCRT2ToLCD) {
3277                 if (pVBInfo->LCDResInfo == Panel_1024x768) {
3278                         if (!(pVBInfo->LCDInfo & XGI_LCDVESATiming)) {
3279                                 if (tempbx == 350)
3280                                         tempbx += 5;
3281                                 if (tempbx == 480)
3282                                         tempbx += 5;
3283                         }
3284                 }
3285         }
3286         tempbx--;
3287         temp = tempbx & 0x00FF;
3288         tempbx--;
3289         temp = tempbx & 0x00FF;
3290         /* 0x10 vertical Blank Start */
3291         xgifb_reg_set(pVBInfo->Part1Port, 0x10, temp);
3292         tempbx = push2;
3293         tempbx--;
3294         temp = tempbx & 0x00FF;
3295         xgifb_reg_set(pVBInfo->Part1Port, 0x0E, temp);
3296
3297         if (tempbx & 0x0100)
3298                 tempcx |= 0x0002;
3299
3300         tempax = 0x000B;
3301
3302         if (modeflag & DoubleScanMode)
3303                 tempax |= 0x08000;
3304
3305         if (tempbx & 0x0200)
3306                 tempcx |= 0x0040;
3307
3308         temp = (tempax & 0xFF00) >> 8;
3309         xgifb_reg_set(pVBInfo->Part1Port, 0x0B, temp);
3310
3311         if (tempbx & 0x0400)
3312                 tempcx |= 0x0600;
3313
3314         /* 0x11 Vertival Blank End */
3315         xgifb_reg_set(pVBInfo->Part1Port, 0x11, 0x00);
3316
3317         tempax = push1;
3318         tempax -= tempbx; /* 0x0C Vertical Retrace Start */
3319         tempax = tempax >> 2;
3320         push1 = tempax; /* push ax */
3321
3322         if (resinfo != 0x09) {
3323                 tempax = tempax << 1;
3324                 tempbx += tempax;
3325         }
3326
3327         if (pVBInfo->VBInfo & SetCRT2ToHiVision) {
3328                 if ((pVBInfo->VBType & VB_SIS301LV) &&
3329                     !(pVBInfo->TVInfo & TVSetHiVision)) {
3330                         if ((pVBInfo->TVInfo & TVSimuMode) &&
3331                             (pVBInfo->TVInfo & TVSetPAL)) {
3332                                 if (!(pVBInfo->VBType & VB_SIS301LV) ||
3333                                     !(pVBInfo->TVInfo &
3334                                       (TVSetYPbPr525p |
3335                                        TVSetYPbPr750p |
3336                                        TVSetHiVision)))
3337                                         tempbx += 40;
3338                         }
3339                 } else {
3340                         tempbx -= 10;
3341                 }
3342         } else if (pVBInfo->TVInfo & TVSimuMode) {
3343                 if (pVBInfo->TVInfo & TVSetPAL) {
3344                         if (pVBInfo->VBType & VB_SIS301LV) {
3345                                 if (!(pVBInfo->TVInfo &
3346                                     (TVSetYPbPr525p |
3347                                      TVSetYPbPr750p |
3348                                      TVSetHiVision)))
3349                                         tempbx += 40;
3350                         } else {
3351                                 tempbx += 40;
3352                         }
3353                 }
3354         }
3355         tempax = push1;
3356         tempax = tempax >> 2;
3357         tempax++;
3358         tempax += tempbx;
3359         push1 = tempax; /* push ax */
3360
3361         if ((pVBInfo->TVInfo & TVSetPAL)) {
3362                 if (tempbx <= 513) {
3363                         if (tempax >= 513)
3364                                 tempbx = 513;
3365                 }
3366         }
3367
3368         temp = tempbx & 0x00FF;
3369         xgifb_reg_set(pVBInfo->Part1Port, 0x0C, temp);
3370         tempbx--;
3371         temp = tempbx & 0x00FF;
3372         xgifb_reg_set(pVBInfo->Part1Port, 0x10, temp);
3373
3374         if (tempbx & 0x0100)
3375                 tempcx |= 0x0008;
3376
3377         if (tempbx & 0x0200)
3378                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x0B, 0x0FF, 0x20);
3379
3380         tempbx++;
3381
3382         if (tempbx & 0x0100)
3383                 tempcx |= 0x0004;
3384
3385         if (tempbx & 0x0200)
3386                 tempcx |= 0x0080;
3387
3388         if (tempbx & 0x0400)
3389                 tempcx |= 0x0C00;
3390
3391         tempbx = push1; /* pop ax */
3392         temp = tempbx & 0x00FF;
3393         temp &= 0x0F;
3394         /* 0x0D vertical Retrace End */
3395         xgifb_reg_set(pVBInfo->Part1Port, 0x0D, temp);
3396
3397         if (tempbx & 0x0010)
3398                 tempcx |= 0x2000;
3399
3400         temp = tempcx & 0x00FF;
3401         xgifb_reg_set(pVBInfo->Part1Port, 0x0A, temp); /* 0x0A CR07 */
3402         temp = (tempcx & 0x0FF00) >> 8;
3403         xgifb_reg_set(pVBInfo->Part1Port, 0x17, temp); /* 0x17 SR0A */
3404         tempax = modeflag;
3405         temp = (tempax & 0xFF00) >> 8;
3406
3407         temp = (temp >> 1) & 0x09;
3408
3409         if (pVBInfo->VBType & (VB_SIS301LV | VB_SIS302LV | VB_XGI301C))
3410                 temp |= 0x01;
3411
3412         xgifb_reg_set(pVBInfo->Part1Port, 0x16, temp); /* 0x16 SR01 */
3413         xgifb_reg_set(pVBInfo->Part1Port, 0x0F, 0); /* 0x0F CR14 */
3414         xgifb_reg_set(pVBInfo->Part1Port, 0x12, 0); /* 0x12 CR17 */
3415
3416         if (pVBInfo->LCDInfo & LCDRGB18Bit)
3417                 temp = 0x80;
3418         else
3419                 temp = 0x00;
3420
3421         xgifb_reg_set(pVBInfo->Part1Port, 0x1A, temp); /* 0x1A SR0E */
3422
3423         return;
3424 }
3425
3426 static void XGI_SetGroup2(unsigned short ModeNo, unsigned short ModeIdIndex,
3427                 unsigned short RefreshRateTableIndex,
3428                 struct xgi_hw_device_info *HwDeviceExtension,
3429                 struct vb_device_info *pVBInfo)
3430 {
3431         unsigned short i, j, tempax, tempbx, tempcx, temp, push1, push2,
3432                         modeflag;
3433         unsigned char const *TimingPoint;
3434
3435         unsigned long longtemp, tempeax, tempebx, temp2, tempecx;
3436
3437         /* si+Ext_ResInfo */
3438         modeflag = XGI330_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
3439
3440         tempax = 0;
3441
3442         if (!(pVBInfo->VBInfo & SetCRT2ToAVIDEO))
3443                 tempax |= 0x0800;
3444
3445         if (!(pVBInfo->VBInfo & SetCRT2ToSVIDEO))
3446                 tempax |= 0x0400;
3447
3448         if (pVBInfo->VBInfo & SetCRT2ToSCART)
3449                 tempax |= 0x0200;
3450
3451         if (!(pVBInfo->TVInfo & TVSetPAL))
3452                 tempax |= 0x1000;
3453
3454         if (pVBInfo->VBInfo & SetCRT2ToHiVision)
3455                 tempax |= 0x0100;
3456
3457         if (pVBInfo->TVInfo & (TVSetYPbPr525p | TVSetYPbPr750p))
3458                 tempax &= 0xfe00;
3459
3460         tempax = (tempax & 0xff00) >> 8;
3461
3462         xgifb_reg_set(pVBInfo->Part2Port, 0x0, tempax);
3463         TimingPoint = XGI330_NTSCTiming;
3464
3465         if (pVBInfo->TVInfo & TVSetPAL)
3466                 TimingPoint = XGI330_PALTiming;
3467
3468         if (pVBInfo->VBInfo & SetCRT2ToHiVision) {
3469                 TimingPoint = XGI330_HiTVExtTiming;
3470
3471                 if (pVBInfo->VBInfo & SetInSlaveMode)
3472                         TimingPoint = XGI330_HiTVSt2Timing;
3473
3474                 if (pVBInfo->SetFlag & TVSimuMode)
3475                         TimingPoint = XGI330_HiTVSt1Timing;
3476
3477                 if (!(modeflag & Charx8Dot))
3478                         TimingPoint = XGI330_HiTVTextTiming;
3479         }
3480
3481         if (pVBInfo->VBInfo & SetCRT2ToYPbPr525750) {
3482                 if (pVBInfo->TVInfo & TVSetYPbPr525i)
3483                         TimingPoint = XGI330_YPbPr525iTiming;
3484
3485                 if (pVBInfo->TVInfo & TVSetYPbPr525p)
3486                         TimingPoint = XGI330_YPbPr525pTiming;
3487
3488                 if (pVBInfo->TVInfo & TVSetYPbPr750p)
3489                         TimingPoint = XGI330_YPbPr750pTiming;
3490         }
3491
3492         for (i = 0x01, j = 0; i <= 0x2D; i++, j++)
3493                 xgifb_reg_set(pVBInfo->Part2Port, i, TimingPoint[j]);
3494
3495         for (i = 0x39; i <= 0x45; i++, j++)
3496                 /* di->temp2[j] */
3497                 xgifb_reg_set(pVBInfo->Part2Port, i, TimingPoint[j]);
3498
3499         if (pVBInfo->VBInfo & SetCRT2ToTV)
3500                 xgifb_reg_and_or(pVBInfo->Part2Port, 0x3A, 0x1F, 0x00);
3501
3502         temp = pVBInfo->NewFlickerMode;
3503         temp &= 0x80;
3504         xgifb_reg_and_or(pVBInfo->Part2Port, 0x0A, 0xFF, temp);
3505
3506         if (pVBInfo->VBInfo & SetCRT2ToHiVision)
3507                 tempax = 950;
3508
3509         if (pVBInfo->TVInfo & TVSetPAL)
3510                 tempax = 520;
3511         else
3512                 tempax = 440;
3513
3514         if (pVBInfo->VDE <= tempax) {
3515                 tempax -= pVBInfo->VDE;
3516                 tempax = tempax >> 2;
3517                 tempax = (tempax & 0x00FF) | ((tempax & 0x00FF) << 8);
3518                 push1 = tempax;
3519                 temp = (tempax & 0xFF00) >> 8;
3520                 temp += (unsigned short) TimingPoint[0];
3521
3522                 if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
3523                                 | VB_SIS302LV | VB_XGI301C)) {
3524                         if (pVBInfo->VBInfo & (SetCRT2ToAVIDEO
3525                                         | SetCRT2ToSVIDEO | SetCRT2ToSCART
3526                                         | SetCRT2ToYPbPr525750)) {
3527                                 tempcx = pVBInfo->VGAHDE;
3528                                 if (tempcx >= 1024) {
3529                                         temp = 0x17; /* NTSC */
3530                                         if (pVBInfo->TVInfo & TVSetPAL)
3531                                                 temp = 0x19; /* PAL */
3532                                 }
3533                         }
3534                 }
3535
3536                 xgifb_reg_set(pVBInfo->Part2Port, 0x01, temp);
3537                 tempax = push1;
3538                 temp = (tempax & 0xFF00) >> 8;
3539                 temp += TimingPoint[1];
3540
3541                 if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
3542                                 | VB_SIS302LV | VB_XGI301C)) {
3543                         if ((pVBInfo->VBInfo & (SetCRT2ToAVIDEO
3544                                         | SetCRT2ToSVIDEO | SetCRT2ToSCART
3545                                         | SetCRT2ToYPbPr525750))) {
3546                                 tempcx = pVBInfo->VGAHDE;
3547                                 if (tempcx >= 1024) {
3548                                         temp = 0x1D; /* NTSC */
3549                                         if (pVBInfo->TVInfo & TVSetPAL)
3550                                                 temp = 0x52; /* PAL */
3551                                 }
3552                         }
3553                 }
3554                 xgifb_reg_set(pVBInfo->Part2Port, 0x02, temp);
3555         }
3556
3557         /* 301b */
3558         tempcx = pVBInfo->HT;
3559
3560         if (XGI_IsLCDDualLink(pVBInfo))
3561                 tempcx = tempcx >> 1;
3562
3563         tempcx -= 2;
3564         temp = tempcx & 0x00FF;
3565         xgifb_reg_set(pVBInfo->Part2Port, 0x1B, temp);
3566
3567         temp = (tempcx & 0xFF00) >> 8;
3568         xgifb_reg_and_or(pVBInfo->Part2Port, 0x1D, ~0x0F, temp);
3569
3570         tempcx = pVBInfo->HT >> 1;
3571         push1 = tempcx; /* push cx */
3572         tempcx += 7;
3573
3574         if (pVBInfo->VBInfo & SetCRT2ToHiVision)
3575                 tempcx -= 4;
3576
3577         temp = tempcx & 0x00FF;
3578         temp = temp << 4;
3579         xgifb_reg_and_or(pVBInfo->Part2Port, 0x22, 0x0F, temp);
3580
3581         tempbx = TimingPoint[j] | ((TimingPoint[j + 1]) << 8);
3582         tempbx += tempcx;
3583         push2 = tempbx;
3584         temp = tempbx & 0x00FF;
3585         xgifb_reg_set(pVBInfo->Part2Port, 0x24, temp);
3586         temp = (tempbx & 0xFF00) >> 8;
3587         temp = temp << 4;
3588         xgifb_reg_and_or(pVBInfo->Part2Port, 0x25, 0x0F, temp);
3589
3590         tempbx = push2;
3591         tempbx = tempbx + 8;
3592         if (pVBInfo->VBInfo & SetCRT2ToHiVision) {
3593                 tempbx = tempbx - 4;
3594                 tempcx = tempbx;
3595         }
3596
3597         temp = (tempbx & 0x00FF) << 4;
3598         xgifb_reg_and_or(pVBInfo->Part2Port, 0x29, 0x0F, temp);
3599
3600         j += 2;
3601         tempcx += (TimingPoint[j] | ((TimingPoint[j + 1]) << 8));
3602         temp = tempcx & 0x00FF;
3603         xgifb_reg_set(pVBInfo->Part2Port, 0x27, temp);
3604         temp = ((tempcx & 0xFF00) >> 8) << 4;
3605         xgifb_reg_and_or(pVBInfo->Part2Port, 0x28, 0x0F, temp);
3606
3607         tempcx += 8;
3608         if (pVBInfo->VBInfo & SetCRT2ToHiVision)
3609                 tempcx -= 4;
3610
3611         temp = tempcx & 0xFF;
3612         temp = temp << 4;
3613         xgifb_reg_and_or(pVBInfo->Part2Port, 0x2A, 0x0F, temp);
3614
3615         tempcx = push1; /* pop cx */
3616         j += 2;
3617         temp = TimingPoint[j] | ((TimingPoint[j + 1]) << 8);
3618         tempcx -= temp;
3619         temp = tempcx & 0x00FF;
3620         temp = temp << 4;
3621         xgifb_reg_and_or(pVBInfo->Part2Port, 0x2D, 0x0F, temp);
3622
3623         tempcx -= 11;
3624
3625         if (!(pVBInfo->VBInfo & SetCRT2ToTV)) {
3626                 tempax = XGI_GetVGAHT2(pVBInfo);
3627                 tempcx = tempax - 1;
3628         }
3629         temp = tempcx & 0x00FF;
3630         xgifb_reg_set(pVBInfo->Part2Port, 0x2E, temp);
3631
3632         tempbx = pVBInfo->VDE;
3633
3634         if (pVBInfo->VGAVDE == 360)
3635                 tempbx = 746;
3636         if (pVBInfo->VGAVDE == 375)
3637                 tempbx = 746;
3638         if (pVBInfo->VGAVDE == 405)
3639                 tempbx = 853;
3640
3641         if (pVBInfo->VBInfo & SetCRT2ToTV) {
3642                 if (pVBInfo->VBType &
3643                     (VB_SIS301LV | VB_SIS302LV | VB_XGI301C)) {
3644                         if (!(pVBInfo->TVInfo &
3645                             (TVSetYPbPr525p | TVSetYPbPr750p)))
3646                                 tempbx = tempbx >> 1;
3647                 } else
3648                         tempbx = tempbx >> 1;
3649         }
3650
3651         tempbx -= 2;
3652         temp = tempbx & 0x00FF;
3653
3654         if (pVBInfo->VBInfo & SetCRT2ToHiVision) {
3655                 if (pVBInfo->VBType & VB_SIS301LV) {
3656                         if (pVBInfo->TVInfo & TVSetHiVision) {
3657                                 if (pVBInfo->VBInfo & SetInSlaveMode) {
3658                                         if (ModeNo == 0x2f)
3659                                                 temp += 1;
3660                                 }
3661                         }
3662                 } else if (pVBInfo->VBInfo & SetInSlaveMode) {
3663                         if (ModeNo == 0x2f)
3664                                 temp += 1;
3665                 }
3666         }
3667
3668         xgifb_reg_set(pVBInfo->Part2Port, 0x2F, temp);
3669
3670         temp = (tempcx & 0xFF00) >> 8;
3671         temp |= ((tempbx & 0xFF00) >> 8) << 6;
3672
3673         if (!(pVBInfo->VBInfo & SetCRT2ToHiVision)) {
3674                 if (pVBInfo->VBType & VB_SIS301LV) {
3675                         if (pVBInfo->TVInfo & TVSetHiVision) {
3676                                 temp |= 0x10;
3677
3678                                 if (!(pVBInfo->VBInfo & SetCRT2ToSVIDEO))
3679                                         temp |= 0x20;
3680                         }
3681                 } else {
3682                         temp |= 0x10;
3683                         if (!(pVBInfo->VBInfo & SetCRT2ToSVIDEO))
3684                                 temp |= 0x20;
3685                 }
3686         }
3687
3688         xgifb_reg_set(pVBInfo->Part2Port, 0x30, temp);
3689
3690         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
3691                         | VB_SIS302LV | VB_XGI301C)) { /* TV gatingno */
3692                 tempbx = pVBInfo->VDE;
3693                 tempcx = tempbx - 2;
3694
3695                 if (pVBInfo->VBInfo & SetCRT2ToTV) {
3696                         if (!(pVBInfo->TVInfo & (TVSetYPbPr525p
3697                                         | TVSetYPbPr750p)))
3698                                 tempbx = tempbx >> 1;
3699                 }
3700
3701                 if (pVBInfo->VBType & (VB_SIS302LV | VB_XGI301C)) {
3702                         temp = 0;
3703                         if (tempcx & 0x0400)
3704                                 temp |= 0x20;
3705
3706                         if (tempbx & 0x0400)
3707                                 temp |= 0x40;
3708
3709                         xgifb_reg_set(pVBInfo->Part4Port, 0x10, temp);
3710                 }
3711
3712                 temp = (((tempbx - 3) & 0x0300) >> 8) << 5;
3713                 xgifb_reg_set(pVBInfo->Part2Port, 0x46, temp);
3714                 temp = (tempbx - 3) & 0x00FF;
3715                 xgifb_reg_set(pVBInfo->Part2Port, 0x47, temp);
3716         }
3717
3718         tempbx = tempbx & 0x00FF;
3719
3720         if (!(modeflag & HalfDCLK)) {
3721                 tempcx = pVBInfo->VGAHDE;
3722                 if (tempcx >= pVBInfo->HDE) {
3723                         tempbx |= 0x2000;
3724                         tempax &= 0x00FF;
3725                 }
3726         }
3727
3728         tempcx = 0x0101;
3729
3730         if (pVBInfo->VBInfo & SetCRT2ToTV) { /*301b*/
3731                 if (pVBInfo->VGAHDE >= 1024) {
3732                         tempcx = 0x1920;
3733                         if (pVBInfo->VGAHDE >= 1280) {
3734                                 tempcx = 0x1420;
3735                                 tempbx = tempbx & 0xDFFF;
3736                         }
3737                 }
3738         }
3739
3740         if (!(tempbx & 0x2000)) {
3741                 if (modeflag & HalfDCLK)
3742                         tempcx = (tempcx & 0xFF00) | ((tempcx & 0x00FF) << 1);
3743
3744                 push1 = tempbx;
3745                 tempeax = pVBInfo->VGAHDE;
3746                 tempebx = (tempcx & 0xFF00) >> 8;
3747                 longtemp = tempeax * tempebx;
3748                 tempecx = tempcx & 0x00FF;
3749                 longtemp = longtemp / tempecx;
3750
3751                 /* 301b */
3752                 tempecx = 8 * 1024;
3753
3754                 if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
3755                                 | VB_SIS302LV | VB_XGI301C)) {
3756                         tempecx = tempecx * 8;
3757                 }
3758
3759                 longtemp = longtemp * tempecx;
3760                 tempecx = pVBInfo->HDE;
3761                 temp2 = longtemp % tempecx;
3762                 tempeax = longtemp / tempecx;
3763                 if (temp2 != 0)
3764                         tempeax += 1;
3765
3766                 tempax = (unsigned short) tempeax;
3767
3768                 /* 301b */
3769                 if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
3770                                 | VB_SIS302LV | VB_XGI301C)) {
3771                         tempcx = ((tempax & 0xFF00) >> 5) >> 8;
3772                 }
3773                 /* end 301b */
3774
3775                 tempbx = push1;
3776                 tempbx = (unsigned short) (((tempeax & 0x0000FF00) & 0x1F00)
3777                                 | (tempbx & 0x00FF));
3778                 tempax = (unsigned short) (((tempeax & 0x000000FF) << 8)
3779                                 | (tempax & 0x00FF));
3780                 temp = (tempax & 0xFF00) >> 8;
3781         } else {
3782                 temp = (tempax & 0x00FF) >> 8;
3783         }
3784
3785         xgifb_reg_set(pVBInfo->Part2Port, 0x44, temp);
3786         temp = (tempbx & 0xFF00) >> 8;
3787         xgifb_reg_and_or(pVBInfo->Part2Port, 0x45, ~0x03F, temp);
3788         temp = tempcx & 0x00FF;
3789
3790         if (tempbx & 0x2000)
3791                 temp = 0;
3792
3793         if (!(pVBInfo->VBInfo & SetCRT2ToLCD))
3794                 temp |= 0x18;
3795
3796         xgifb_reg_and_or(pVBInfo->Part2Port, 0x46, ~0x1F, temp);
3797         if (pVBInfo->TVInfo & TVSetPAL) {
3798                 tempbx = 0x0382;
3799                 tempcx = 0x007e;
3800         } else {
3801                 tempbx = 0x0369;
3802                 tempcx = 0x0061;
3803         }
3804
3805         temp = tempbx & 0x00FF;
3806         xgifb_reg_set(pVBInfo->Part2Port, 0x4b, temp);
3807         temp = tempcx & 0x00FF;
3808         xgifb_reg_set(pVBInfo->Part2Port, 0x4c, temp);
3809
3810         temp = ((tempcx & 0xFF00) >> 8) & 0x03;
3811         temp = temp << 2;
3812         temp |= ((tempbx & 0xFF00) >> 8) & 0x03;
3813
3814         if (pVBInfo->VBInfo & SetCRT2ToYPbPr525750) {
3815                 temp |= 0x10;
3816
3817                 if (pVBInfo->TVInfo & TVSetYPbPr525p)
3818                         temp |= 0x20;
3819
3820                 if (pVBInfo->TVInfo & TVSetYPbPr750p)
3821                         temp |= 0x60;
3822         }
3823
3824         xgifb_reg_set(pVBInfo->Part2Port, 0x4d, temp);
3825         temp = xgifb_reg_get(pVBInfo->Part2Port, 0x43); /* 301b change */
3826         xgifb_reg_set(pVBInfo->Part2Port, 0x43, (unsigned short) (temp - 3));
3827
3828         if (!(pVBInfo->TVInfo & (TVSetYPbPr525p | TVSetYPbPr750p))) {
3829                 if (pVBInfo->TVInfo & NTSC1024x768) {
3830                         TimingPoint = XGI_NTSC1024AdjTime;
3831                         for (i = 0x1c, j = 0; i <= 0x30; i++, j++) {
3832                                 xgifb_reg_set(pVBInfo->Part2Port, i,
3833                                                 TimingPoint[j]);
3834                         }
3835                         xgifb_reg_set(pVBInfo->Part2Port, 0x43, 0x72);
3836                 }
3837         }
3838
3839         /* Modify for 301C PALM Support */
3840         if (pVBInfo->VBType & VB_XGI301C) {
3841                 if (pVBInfo->TVInfo & TVSetPALM)
3842                         xgifb_reg_and_or(pVBInfo->Part2Port, 0x4E, ~0x08,
3843                                         0x08); /* PALM Mode */
3844         }
3845
3846         if (pVBInfo->TVInfo & TVSetPALM) {
3847                 tempax = (unsigned char) xgifb_reg_get(pVBInfo->Part2Port,
3848                                 0x01);
3849                 tempax--;
3850                 xgifb_reg_and(pVBInfo->Part2Port, 0x01, tempax);
3851
3852                 xgifb_reg_and(pVBInfo->Part2Port, 0x00, 0xEF);
3853         }
3854
3855         if (pVBInfo->VBInfo & SetCRT2ToHiVision) {
3856                 if (!(pVBInfo->VBInfo & SetInSlaveMode))
3857                         xgifb_reg_set(pVBInfo->Part2Port, 0x0B, 0x00);
3858         }
3859
3860         if (pVBInfo->VBInfo & SetCRT2ToTV)
3861                 return;
3862 }
3863
3864 static void XGI_SetLCDRegs(unsigned short ModeNo, unsigned short ModeIdIndex,
3865                 struct xgi_hw_device_info *HwDeviceExtension,
3866                 unsigned short RefreshRateTableIndex,
3867                 struct vb_device_info *pVBInfo)
3868 {
3869         unsigned short pushbx, tempax, tempbx, tempcx, temp, tempah,
3870                         tempbh, tempch;
3871
3872         struct XGI_LCDDesStruct const *LCDBDesPtr = NULL;
3873
3874         /* si+Ext_ResInfo */
3875         if (!(pVBInfo->VBInfo & SetCRT2ToLCD))
3876                 return;
3877
3878         tempbx = pVBInfo->HDE; /* RHACTE=HDE-1 */
3879
3880         if (XGI_IsLCDDualLink(pVBInfo))
3881                 tempbx = tempbx >> 1;
3882
3883         tempbx -= 1;
3884         temp = tempbx & 0x00FF;
3885         xgifb_reg_set(pVBInfo->Part2Port, 0x2C, temp);
3886         temp = (tempbx & 0xFF00) >> 8;
3887         temp = temp << 4;
3888         xgifb_reg_and_or(pVBInfo->Part2Port, 0x2B, 0x0F, temp);
3889         temp = 0x01;
3890
3891         xgifb_reg_set(pVBInfo->Part2Port, 0x0B, temp);
3892         tempbx = pVBInfo->VDE; /* RTVACTEO=(VDE-1)&0xFF */
3893         tempbx--;
3894         temp = tempbx & 0x00FF;
3895         xgifb_reg_set(pVBInfo->Part2Port, 0x03, temp);
3896         temp = ((tempbx & 0xFF00) >> 8) & 0x07;
3897         xgifb_reg_and_or(pVBInfo->Part2Port, 0x0C, ~0x07, temp);
3898
3899         tempcx = pVBInfo->VT - 1;
3900         temp = tempcx & 0x00FF; /* RVTVT=VT-1 */
3901         xgifb_reg_set(pVBInfo->Part2Port, 0x19, temp);
3902         temp = (tempcx & 0xFF00) >> 8;
3903         temp = temp << 5;
3904         xgifb_reg_set(pVBInfo->Part2Port, 0x1A, temp);
3905         xgifb_reg_and_or(pVBInfo->Part2Port, 0x09, 0xF0, 0x00);
3906         xgifb_reg_and_or(pVBInfo->Part2Port, 0x0A, 0xF0, 0x00);
3907         xgifb_reg_and_or(pVBInfo->Part2Port, 0x17, 0xFB, 0x00);
3908         xgifb_reg_and_or(pVBInfo->Part2Port, 0x18, 0xDF, 0x00);
3909
3910         /* Customized LCDB Does not add */
3911         if ((pVBInfo->VBType & VB_SIS301LV) || (pVBInfo->VBType & VB_SIS302LV))
3912                 LCDBDesPtr = XGI_GetLcdPtr(xgifb_lcddldes, ModeNo, ModeIdIndex,
3913                                            RefreshRateTableIndex, pVBInfo);
3914         else
3915                 LCDBDesPtr = XGI_GetLcdPtr(XGI_LCDDesDataTable, ModeNo,
3916                                            ModeIdIndex, RefreshRateTableIndex,
3917                                            pVBInfo);
3918
3919         tempah = pVBInfo->LCDResInfo;
3920         tempah &= PanelResInfo;
3921
3922         if ((tempah == Panel_1024x768) || (tempah == Panel_1024x768x75)) {
3923                 tempbx = 1024;
3924                 tempcx = 768;
3925         } else if ((tempah == Panel_1280x1024) ||
3926                    (tempah == Panel_1280x1024x75)) {
3927                 tempbx = 1280;
3928                 tempcx = 1024;
3929         } else if (tempah == Panel_1400x1050) {
3930                 tempbx = 1400;
3931                 tempcx = 1050;
3932         } else {
3933                 tempbx = 1600;
3934                 tempcx = 1200;
3935         }
3936
3937         if (pVBInfo->LCDInfo & EnableScalingLCD) {
3938                 tempbx = pVBInfo->HDE;
3939                 tempcx = pVBInfo->VDE;
3940         }
3941
3942         pushbx = tempbx;
3943         tempax = pVBInfo->VT;
3944         pVBInfo->LCDHDES = LCDBDesPtr->LCDHDES;
3945         pVBInfo->LCDHRS = LCDBDesPtr->LCDHRS;
3946         pVBInfo->LCDVDES = LCDBDesPtr->LCDVDES;
3947         pVBInfo->LCDVRS = LCDBDesPtr->LCDVRS;
3948         tempbx = pVBInfo->LCDVDES;
3949         tempcx += tempbx;
3950
3951         if (tempcx >= tempax)
3952                 tempcx -= tempax; /* lcdvdes */
3953
3954         temp = tempbx & 0x00FF; /* RVEQ1EQ=lcdvdes */
3955         xgifb_reg_set(pVBInfo->Part2Port, 0x05, temp);
3956         temp = tempcx & 0x00FF;
3957         xgifb_reg_set(pVBInfo->Part2Port, 0x06, temp);
3958         tempch = ((tempcx & 0xFF00) >> 8) & 0x07;
3959         tempbh = ((tempbx & 0xFF00) >> 8) & 0x07;
3960         tempah = tempch;
3961         tempah = tempah << 3;
3962         tempah |= tempbh;
3963         xgifb_reg_set(pVBInfo->Part2Port, 0x02, tempah);
3964
3965         /* getlcdsync() */
3966         XGI_GetLCDSync(&tempax, &tempbx, pVBInfo);
3967         tempcx = tempbx;
3968         tempax = pVBInfo->VT;
3969         tempbx = pVBInfo->LCDVRS;
3970
3971         tempcx += tempbx;
3972         if (tempcx >= tempax)
3973                 tempcx -= tempax;
3974
3975         temp = tempbx & 0x00FF; /* RTVACTEE=lcdvrs */
3976         xgifb_reg_set(pVBInfo->Part2Port, 0x04, temp);
3977         temp = (tempbx & 0xFF00) >> 8;
3978         temp = temp << 4;
3979         temp |= (tempcx & 0x000F);
3980         xgifb_reg_set(pVBInfo->Part2Port, 0x01, temp);
3981         tempcx = pushbx;
3982         tempax = pVBInfo->HT;
3983         tempbx = pVBInfo->LCDHDES;
3984         tempbx &= 0x0FFF;
3985
3986         if (XGI_IsLCDDualLink(pVBInfo)) {
3987                 tempax = tempax >> 1;
3988                 tempbx = tempbx >> 1;
3989                 tempcx = tempcx >> 1;
3990         }
3991
3992         if (pVBInfo->VBType & VB_SIS302LV)
3993                 tempbx += 1;
3994
3995         if (pVBInfo->VBType & VB_XGI301C) /* tap4 */
3996                 tempbx += 1;
3997
3998         tempcx += tempbx;
3999
4000         if (tempcx >= tempax)
4001                 tempcx -= tempax;
4002
4003         temp = tempbx & 0x00FF;
4004         xgifb_reg_set(pVBInfo->Part2Port, 0x1F, temp); /* RHBLKE=lcdhdes */
4005         temp = ((tempbx & 0xFF00) >> 8) << 4;
4006         xgifb_reg_set(pVBInfo->Part2Port, 0x20, temp);
4007         temp = tempcx & 0x00FF;
4008         xgifb_reg_set(pVBInfo->Part2Port, 0x23, temp); /* RHEQPLE=lcdhdee */
4009         temp = (tempcx & 0xFF00) >> 8;
4010         xgifb_reg_set(pVBInfo->Part2Port, 0x25, temp);
4011
4012         XGI_GetLCDSync(&tempax, &tempbx, pVBInfo);
4013         tempcx = tempax;
4014         tempax = pVBInfo->HT;
4015         tempbx = pVBInfo->LCDHRS;
4016         if (XGI_IsLCDDualLink(pVBInfo)) {
4017                 tempax = tempax >> 1;
4018                 tempbx = tempbx >> 1;
4019                 tempcx = tempcx >> 1;
4020         }
4021
4022         if (pVBInfo->VBType & VB_SIS302LV)
4023                 tempbx += 1;
4024
4025         tempcx += tempbx;
4026
4027         if (tempcx >= tempax)
4028                 tempcx -= tempax;
4029
4030         temp = tempbx & 0x00FF; /* RHBURSTS=lcdhrs */
4031         xgifb_reg_set(pVBInfo->Part2Port, 0x1C, temp);
4032
4033         temp = (tempbx & 0xFF00) >> 8;
4034         temp = temp << 4;
4035         xgifb_reg_and_or(pVBInfo->Part2Port, 0x1D, ~0x0F0, temp);
4036         temp = tempcx & 0x00FF; /* RHSYEXP2S=lcdhre */
4037         xgifb_reg_set(pVBInfo->Part2Port, 0x21, temp);
4038
4039         if (!(pVBInfo->LCDInfo & XGI_LCDVESATiming)) {
4040                 if (pVBInfo->VGAVDE == 525) {
4041                         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B
4042                                         | VB_SIS301LV | VB_SIS302LV
4043                                         | VB_XGI301C)) {
4044                                 temp = 0xC6;
4045                         } else
4046                                 temp = 0xC4;
4047
4048                         xgifb_reg_set(pVBInfo->Part2Port, 0x2f, temp);
4049                         xgifb_reg_set(pVBInfo->Part2Port, 0x30, 0xB3);
4050                 }
4051
4052                 if (pVBInfo->VGAVDE == 420) {
4053                         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B
4054                                         | VB_SIS301LV | VB_SIS302LV
4055                                         | VB_XGI301C)) {
4056                                 temp = 0x4F;
4057                         } else
4058                                 temp = 0x4E;
4059                         xgifb_reg_set(pVBInfo->Part2Port, 0x2f, temp);
4060                 }
4061         }
4062 }
4063
4064 /* --------------------------------------------------------------------- */
4065 /* Function : XGI_GetTap4Ptr */
4066 /* Input : */
4067 /* Output : di -> Tap4 Reg. Setting Pointer */
4068 /* Description : */
4069 /* --------------------------------------------------------------------- */
4070 static struct XGI301C_Tap4TimingStruct const
4071 *XGI_GetTap4Ptr(unsigned short tempcx, struct vb_device_info *pVBInfo)
4072 {
4073         unsigned short tempax, tempbx, i;
4074         struct XGI301C_Tap4TimingStruct const *Tap4TimingPtr;
4075
4076         if (tempcx == 0) {
4077                 tempax = pVBInfo->VGAHDE;
4078                 tempbx = pVBInfo->HDE;
4079         } else {
4080                 tempax = pVBInfo->VGAVDE;
4081                 tempbx = pVBInfo->VDE;
4082         }
4083
4084         if (tempax <= tempbx)
4085                 return &xgifb_tap4_timing[0];
4086         else
4087                 Tap4TimingPtr = xgifb_ntsc_525_tap4_timing; /* NTSC */
4088
4089         if (pVBInfo->TVInfo & TVSetPAL)
4090                 Tap4TimingPtr = PALTap4Timing;
4091
4092         if (pVBInfo->VBInfo & SetCRT2ToYPbPr525750) {
4093                 if ((pVBInfo->TVInfo & TVSetYPbPr525i) ||
4094                         (pVBInfo->TVInfo & TVSetYPbPr525p))
4095                         Tap4TimingPtr = xgifb_ntsc_525_tap4_timing;
4096                 if (pVBInfo->TVInfo & TVSetYPbPr750p)
4097                         Tap4TimingPtr = YPbPr750pTap4Timing;
4098         }
4099
4100         if (pVBInfo->VBInfo & SetCRT2ToHiVision)
4101                 Tap4TimingPtr = xgifb_tap4_timing;
4102
4103         i = 0;
4104         while (Tap4TimingPtr[i].DE != 0xFFFF) {
4105                 if (Tap4TimingPtr[i].DE == tempax)
4106                         break;
4107                 i++;
4108         }
4109         return &Tap4TimingPtr[i];
4110 }
4111
4112 static void XGI_SetTap4Regs(struct vb_device_info *pVBInfo)
4113 {
4114         unsigned short i, j;
4115         struct XGI301C_Tap4TimingStruct const *Tap4TimingPtr;
4116
4117         if (!(pVBInfo->VBType & VB_XGI301C))
4118                 return;
4119
4120         Tap4TimingPtr = XGI_GetTap4Ptr(0, pVBInfo); /* Set Horizontal Scaling */
4121         for (i = 0x80, j = 0; i <= 0xBF; i++, j++)
4122                 xgifb_reg_set(pVBInfo->Part2Port, i, Tap4TimingPtr->Reg[j]);
4123
4124         if ((pVBInfo->VBInfo & SetCRT2ToTV) &&
4125             (!(pVBInfo->VBInfo & SetCRT2ToHiVision))) {
4126                 /* Set Vertical Scaling */
4127                 Tap4TimingPtr = XGI_GetTap4Ptr(1, pVBInfo);
4128                 for (i = 0xC0, j = 0; i < 0xFF; i++, j++)
4129                         xgifb_reg_set(pVBInfo->Part2Port,
4130                                       i,
4131                                       Tap4TimingPtr->Reg[j]);
4132         }
4133
4134         if ((pVBInfo->VBInfo & SetCRT2ToTV) &&
4135             (!(pVBInfo->VBInfo & SetCRT2ToHiVision)))
4136                 /* Enable V.Scaling */
4137                 xgifb_reg_and_or(pVBInfo->Part2Port, 0x4E, ~0x14, 0x04);
4138         else
4139                 /* Enable H.Scaling */
4140                 xgifb_reg_and_or(pVBInfo->Part2Port, 0x4E, ~0x14, 0x10);
4141 }
4142
4143 static void XGI_SetGroup3(unsigned short ModeNo, unsigned short ModeIdIndex,
4144                 struct vb_device_info *pVBInfo)
4145 {
4146         unsigned short i;
4147         unsigned char const *tempdi;
4148         unsigned short modeflag;
4149
4150         /* si+Ext_ResInfo */
4151         modeflag = XGI330_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
4152
4153         xgifb_reg_set(pVBInfo->Part3Port, 0x00, 0x00);
4154         if (pVBInfo->TVInfo & TVSetPAL) {
4155                 xgifb_reg_set(pVBInfo->Part3Port, 0x13, 0xFA);
4156                 xgifb_reg_set(pVBInfo->Part3Port, 0x14, 0xC8);
4157         } else {
4158                 xgifb_reg_set(pVBInfo->Part3Port, 0x13, 0xF5);
4159                 xgifb_reg_set(pVBInfo->Part3Port, 0x14, 0xB7);
4160         }
4161
4162         if (!(pVBInfo->VBInfo & SetCRT2ToTV))
4163                 return;
4164
4165         if (pVBInfo->TVInfo & TVSetPALM) {
4166                 xgifb_reg_set(pVBInfo->Part3Port, 0x13, 0xFA);
4167                 xgifb_reg_set(pVBInfo->Part3Port, 0x14, 0xC8);
4168                 xgifb_reg_set(pVBInfo->Part3Port, 0x3D, 0xA8);
4169         }
4170
4171         if ((pVBInfo->VBInfo & SetCRT2ToHiVision) || (pVBInfo->VBInfo
4172                         & SetCRT2ToYPbPr525750)) {
4173                 if (pVBInfo->TVInfo & TVSetYPbPr525i)
4174                         return;
4175
4176                 tempdi = XGI330_HiTVGroup3Data;
4177                 if (pVBInfo->SetFlag & TVSimuMode) {
4178                         tempdi = XGI330_HiTVGroup3Simu;
4179                         if (!(modeflag & Charx8Dot))
4180                                 tempdi = XGI330_HiTVGroup3Text;
4181                 }
4182
4183                 if (pVBInfo->TVInfo & TVSetYPbPr525p)
4184                         tempdi = XGI330_Ren525pGroup3;
4185
4186                 if (pVBInfo->TVInfo & TVSetYPbPr750p)
4187                         tempdi = XGI330_Ren750pGroup3;
4188
4189                 for (i = 0; i <= 0x3E; i++)
4190                         xgifb_reg_set(pVBInfo->Part3Port, i, tempdi[i]);
4191
4192                 if (pVBInfo->VBType & VB_XGI301C) { /* Marcovision */
4193                         if (pVBInfo->TVInfo & TVSetYPbPr525p)
4194                                 xgifb_reg_set(pVBInfo->Part3Port, 0x28, 0x3f);
4195                 }
4196         }
4197         return;
4198 } /* {end of XGI_SetGroup3} */
4199
4200 static void XGI_SetGroup4(unsigned short ModeNo, unsigned short ModeIdIndex,
4201                 unsigned short RefreshRateTableIndex,
4202                 struct xgi_hw_device_info *HwDeviceExtension,
4203                 struct vb_device_info *pVBInfo)
4204 {
4205         unsigned short tempax, tempcx, tempbx, modeflag, temp, temp2;
4206
4207         unsigned long tempebx, tempeax, templong;
4208
4209         /* si+Ext_ResInfo */
4210         modeflag = XGI330_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
4211         temp = pVBInfo->RVBHCFACT;
4212         xgifb_reg_set(pVBInfo->Part4Port, 0x13, temp);
4213
4214         tempbx = pVBInfo->RVBHCMAX;
4215         temp = tempbx & 0x00FF;
4216         xgifb_reg_set(pVBInfo->Part4Port, 0x14, temp);
4217         temp2 = ((tempbx & 0xFF00) >> 8) << 7;
4218         tempcx = pVBInfo->VGAHT - 1;
4219         temp = tempcx & 0x00FF;
4220         xgifb_reg_set(pVBInfo->Part4Port, 0x16, temp);
4221
4222         temp = ((tempcx & 0xFF00) >> 8) << 3;
4223         temp2 |= temp;
4224
4225         tempcx = pVBInfo->VGAVT - 1;
4226         if (!(pVBInfo->VBInfo & SetCRT2ToTV))
4227                 tempcx -= 5;
4228
4229         temp = tempcx & 0x00FF;
4230         xgifb_reg_set(pVBInfo->Part4Port, 0x17, temp);
4231         temp = temp2 | ((tempcx & 0xFF00) >> 8);
4232         xgifb_reg_set(pVBInfo->Part4Port, 0x15, temp);
4233         xgifb_reg_or(pVBInfo->Part4Port, 0x0D, 0x08);
4234         tempcx = pVBInfo->VBInfo;
4235         tempbx = pVBInfo->VGAHDE;
4236
4237         if (modeflag & HalfDCLK)
4238                 tempbx = tempbx >> 1;
4239
4240         if (XGI_IsLCDDualLink(pVBInfo))
4241                 tempbx = tempbx >> 1;
4242
4243         if (tempcx & SetCRT2ToHiVision) {
4244                 temp = 0;
4245                 if (tempbx <= 1024)
4246                         temp = 0xA0;
4247                 if (tempbx == 1280)
4248                         temp = 0xC0;
4249         } else if (tempcx & SetCRT2ToTV) {
4250                 temp = 0xA0;
4251                 if (tempbx <= 800)
4252                         temp = 0x80;
4253         } else {
4254                 temp = 0x80;
4255                 if (pVBInfo->VBInfo & SetCRT2ToLCD) {
4256                         temp = 0;
4257                         if (tempbx > 800)
4258                                 temp = 0x60;
4259                 }
4260         }
4261
4262         if (pVBInfo->TVInfo & (TVSetYPbPr525p | TVSetYPbPr750p)) {
4263                 temp = 0x00;
4264                 if (pVBInfo->VGAHDE == 1280)
4265                         temp = 0x40;
4266                 if (pVBInfo->VGAHDE == 1024)
4267                         temp = 0x20;
4268         }
4269         xgifb_reg_and_or(pVBInfo->Part4Port, 0x0E, ~0xEF, temp);
4270
4271         tempebx = pVBInfo->VDE;
4272
4273         if (tempcx & SetCRT2ToHiVision) {
4274                 if (!(temp & 0xE000))
4275                         tempbx = tempbx >> 1;
4276         }
4277
4278         tempcx = pVBInfo->RVBHRS;
4279         temp = tempcx & 0x00FF;
4280         xgifb_reg_set(pVBInfo->Part4Port, 0x18, temp);
4281
4282         tempeax = pVBInfo->VGAVDE;
4283         tempcx |= 0x04000;
4284
4285         if (tempeax <= tempebx) {
4286                 tempcx = (tempcx & (~0x4000));
4287                 tempeax = pVBInfo->VGAVDE;
4288         } else {
4289                 tempeax -= tempebx;
4290         }
4291
4292         templong = (tempeax * 256 * 1024) % tempebx;
4293         tempeax = (tempeax * 256 * 1024) / tempebx;
4294         tempebx = tempeax;
4295
4296         if (templong != 0)
4297                 tempebx++;
4298
4299         temp = (unsigned short) (tempebx & 0x000000FF);
4300         xgifb_reg_set(pVBInfo->Part4Port, 0x1B, temp);
4301
4302         temp = (unsigned short) ((tempebx & 0x0000FF00) >> 8);
4303         xgifb_reg_set(pVBInfo->Part4Port, 0x1A, temp);
4304         tempbx = (unsigned short) (tempebx >> 16);
4305         temp = tempbx & 0x00FF;
4306         temp = temp << 4;
4307         temp |= ((tempcx & 0xFF00) >> 8);
4308         xgifb_reg_set(pVBInfo->Part4Port, 0x19, temp);
4309
4310         /* 301b */
4311         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
4312                         | VB_SIS302LV | VB_XGI301C)) {
4313                 temp = 0x0028;
4314                 xgifb_reg_set(pVBInfo->Part4Port, 0x1C, temp);
4315                 tempax = pVBInfo->VGAHDE;
4316                 if (modeflag & HalfDCLK)
4317                         tempax = tempax >> 1;
4318
4319                 if (XGI_IsLCDDualLink(pVBInfo))
4320                         tempax = tempax >> 1;
4321
4322                 if (pVBInfo->VBInfo & SetCRT2ToLCD) {
4323                         if (tempax > 800)
4324                                 tempax -= 800;
4325                 } else if (pVBInfo->VGAHDE > 800) {
4326                         if (pVBInfo->VGAHDE == 1024)
4327                                 tempax = (tempax * 25 / 32) - 1;
4328                         else
4329                                 tempax = (tempax * 20 / 32) - 1;
4330                 }
4331                 tempax -= 1;
4332
4333                 temp = (tempax & 0xFF00) >> 8;
4334                 temp = ((temp & 0x0003) << 4);
4335                 xgifb_reg_set(pVBInfo->Part4Port, 0x1E, temp);
4336                 temp = (tempax & 0x00FF);
4337                 xgifb_reg_set(pVBInfo->Part4Port, 0x1D, temp);
4338
4339                 if (pVBInfo->VBInfo & (SetCRT2ToTV | SetCRT2ToHiVision)) {
4340                         if (pVBInfo->VGAHDE > 800)
4341                                 xgifb_reg_or(pVBInfo->Part4Port, 0x1E, 0x08);
4342
4343                 }
4344                 temp = 0x0036;
4345
4346                 if (pVBInfo->VBInfo & SetCRT2ToTV) {
4347                         if (!(pVBInfo->TVInfo & (NTSC1024x768
4348                                         | TVSetYPbPr525p | TVSetYPbPr750p
4349                                         | TVSetHiVision))) {
4350                                 temp |= 0x0001;
4351                                 if ((pVBInfo->VBInfo & SetInSlaveMode)
4352                                                 && (!(pVBInfo->TVInfo
4353                                                                 & TVSimuMode)))
4354                                         temp &= (~0x0001);
4355                         }
4356                 }
4357
4358                 xgifb_reg_and_or(pVBInfo->Part4Port, 0x1F, 0x00C0, temp);
4359                 tempbx = pVBInfo->HT;
4360                 if (XGI_IsLCDDualLink(pVBInfo))
4361                         tempbx = tempbx >> 1;
4362                 tempbx = (tempbx >> 1) - 2;
4363                 temp = ((tempbx & 0x0700) >> 8) << 3;
4364                 xgifb_reg_and_or(pVBInfo->Part4Port, 0x21, 0x00C0, temp);
4365                 temp = tempbx & 0x00FF;
4366                 xgifb_reg_set(pVBInfo->Part4Port, 0x22, temp);
4367         }
4368         /* end 301b */
4369
4370         XGI_SetCRT2VCLK(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
4371 }
4372
4373 static void XGINew_EnableCRT2(struct vb_device_info *pVBInfo)
4374 {
4375         xgifb_reg_and_or(pVBInfo->P3c4, 0x1E, 0xFF, 0x20);
4376 }
4377
4378 static void XGI_SetGroup5(unsigned short ModeNo, unsigned short ModeIdIndex,
4379                 struct vb_device_info *pVBInfo)
4380 {
4381         if (pVBInfo->ModeType == ModeVGA) {
4382                 if (!(pVBInfo->VBInfo & (SetInSlaveMode | LoadDACFlag
4383                                 | DisableCRT2Display))) {
4384                         XGINew_EnableCRT2(pVBInfo);
4385                 }
4386         }
4387         return;
4388 }
4389
4390 static void XGI_EnableGatingCRT(struct xgi_hw_device_info *HwDeviceExtension,
4391                 struct vb_device_info *pVBInfo)
4392 {
4393         xgifb_reg_and_or(pVBInfo->P3d4, 0x63, 0xBF, 0x40);
4394 }
4395
4396 static void XGI_DisableGatingCRT(struct xgi_hw_device_info *HwDeviceExtension,
4397                 struct vb_device_info *pVBInfo)
4398 {
4399
4400         xgifb_reg_and_or(pVBInfo->P3d4, 0x63, 0xBF, 0x00);
4401 }
4402
4403 static unsigned char XGI_XG21CheckLVDSMode(struct xgifb_video_info *xgifb_info,
4404                 unsigned short ModeNo, unsigned short ModeIdIndex,
4405                 struct vb_device_info *pVBInfo)
4406 {
4407         unsigned short xres, yres, colordepth, modeflag, resindex;
4408
4409         resindex = XGI330_EModeIDTable[ModeIdIndex].Ext_RESINFO;
4410         xres = XGI330_ModeResInfo[resindex].HTotal; /* xres->ax */
4411         yres = XGI330_ModeResInfo[resindex].VTotal; /* yres->bx */
4412         /* si+St_ModeFlag */
4413         modeflag = XGI330_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
4414
4415         if (!(modeflag & Charx8Dot)) {
4416                 xres /= 9;
4417                 xres *= 8;
4418         }
4419
4420         if ((ModeNo > 0x13) && (modeflag & HalfDCLK))
4421                 xres *= 2;
4422
4423         if ((ModeNo > 0x13) && (modeflag & DoubleScanMode))
4424                 yres *= 2;
4425
4426         if (xres > xgifb_info->lvds_data.LVDSHDE)
4427                 return 0;
4428
4429         if (yres > xgifb_info->lvds_data.LVDSVDE)
4430                 return 0;
4431
4432         if (xres != xgifb_info->lvds_data.LVDSHDE ||
4433             yres != xgifb_info->lvds_data.LVDSVDE) {
4434                 colordepth = XGI_GetColorDepth(ModeNo, ModeIdIndex, pVBInfo);
4435                 if (colordepth > 2)
4436                         return 0;
4437         }
4438         return 1;
4439 }
4440
4441 static void xgifb_set_lvds(struct xgifb_video_info *xgifb_info,
4442                            int chip_id,
4443                            unsigned short ModeNo,
4444                            unsigned short ModeIdIndex,
4445                            struct vb_device_info *pVBInfo)
4446 {
4447         unsigned char temp, Miscdata;
4448         unsigned short xres, yres, modeflag, resindex;
4449         unsigned short LVDSHT, LVDSHBS, LVDSHRS, LVDSHRE, LVDSHBE;
4450         unsigned short LVDSVT, LVDSVBS, LVDSVRS, LVDSVRE, LVDSVBE;
4451         unsigned short value;
4452
4453         temp = (unsigned char) ((xgifb_info->lvds_data.LVDS_Capability &
4454                                 (LCDPolarity << 8)) >> 8);
4455         temp &= LCDPolarity;
4456         Miscdata = (unsigned char) inb(pVBInfo->P3cc);
4457
4458         outb((Miscdata & 0x3F) | temp, pVBInfo->P3c2);
4459
4460         temp = xgifb_info->lvds_data.LVDS_Capability & LCDPolarity;
4461         /* SR35[7] FP VSync polarity */
4462         xgifb_reg_and_or(pVBInfo->P3c4, 0x35, ~0x80, temp & 0x80);
4463         /* SR30[5] FP HSync polarity */
4464         xgifb_reg_and_or(pVBInfo->P3c4, 0x30, ~0x20, (temp & 0x40) >> 1);
4465
4466         if (chip_id == XG27)
4467                 XGI_SetXG27FPBits(pVBInfo);
4468         else
4469                 XGI_SetXG21FPBits(pVBInfo);
4470
4471         resindex = XGI330_EModeIDTable[ModeIdIndex].Ext_RESINFO;
4472         xres = XGI330_ModeResInfo[resindex].HTotal; /* xres->ax */
4473         yres = XGI330_ModeResInfo[resindex].VTotal; /* yres->bx */
4474         /* si+St_ModeFlag */
4475         modeflag = XGI330_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
4476
4477         if (!(modeflag & Charx8Dot))
4478                 xres = xres * 8 / 9;
4479
4480         LVDSHT = xgifb_info->lvds_data.LVDSHT;
4481
4482         LVDSHBS = xres + (xgifb_info->lvds_data.LVDSHDE - xres) / 2;
4483
4484         if (LVDSHBS > LVDSHT)
4485                 LVDSHBS -= LVDSHT;
4486
4487         LVDSHRS = LVDSHBS + xgifb_info->lvds_data.LVDSHFP;
4488         if (LVDSHRS > LVDSHT)
4489                 LVDSHRS -= LVDSHT;
4490
4491         LVDSHRE = LVDSHRS + xgifb_info->lvds_data.LVDSHSYNC;
4492         if (LVDSHRE > LVDSHT)
4493                 LVDSHRE -= LVDSHT;
4494
4495         LVDSHBE = LVDSHBS + LVDSHT - xgifb_info->lvds_data.LVDSHDE;
4496
4497         LVDSVT = xgifb_info->lvds_data.LVDSVT;
4498
4499         LVDSVBS = yres + (xgifb_info->lvds_data.LVDSVDE - yres) / 2;
4500         if (modeflag & DoubleScanMode)
4501                 LVDSVBS += yres / 2;
4502
4503         if (LVDSVBS > LVDSVT)
4504                 LVDSVBS -= LVDSVT;
4505
4506         LVDSVRS = LVDSVBS + xgifb_info->lvds_data.LVDSVFP;
4507         if (LVDSVRS > LVDSVT)
4508                 LVDSVRS -= LVDSVT;
4509
4510         LVDSVRE = LVDSVRS + xgifb_info->lvds_data.LVDSVSYNC;
4511         if (LVDSVRE > LVDSVT)
4512                 LVDSVRE -= LVDSVT;
4513
4514         LVDSVBE = LVDSVBS + LVDSVT - xgifb_info->lvds_data.LVDSVDE;
4515
4516         temp = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x11);
4517         xgifb_reg_set(pVBInfo->P3d4, 0x11, temp & 0x7f); /* Unlock CRTC */
4518
4519         if (!(modeflag & Charx8Dot))
4520                 xgifb_reg_or(pVBInfo->P3c4, 0x1, 0x1);
4521
4522         /* HT SR0B[1:0] CR00 */
4523         value = (LVDSHT >> 3) - 5;
4524         xgifb_reg_and_or(pVBInfo->P3c4, 0x0B, ~0x03, (value & 0x300) >> 8);
4525         xgifb_reg_set(pVBInfo->P3d4, 0x0, (value & 0xFF));
4526
4527         /* HBS SR0B[5:4] CR02 */
4528         value = (LVDSHBS >> 3) - 1;
4529         xgifb_reg_and_or(pVBInfo->P3c4, 0x0B, ~0x30, (value & 0x300) >> 4);
4530         xgifb_reg_set(pVBInfo->P3d4, 0x2, (value & 0xFF));
4531
4532         /* HBE SR0C[1:0] CR05[7] CR03[4:0] */
4533         value = (LVDSHBE >> 3) - 1;
4534         xgifb_reg_and_or(pVBInfo->P3c4, 0x0C, ~0x03, (value & 0xC0) >> 6);
4535         xgifb_reg_and_or(pVBInfo->P3d4, 0x05, ~0x80, (value & 0x20) << 2);
4536         xgifb_reg_and_or(pVBInfo->P3d4, 0x03, ~0x1F, value & 0x1F);
4537
4538         /* HRS SR0B[7:6] CR04 */
4539         value = (LVDSHRS >> 3) + 2;
4540         xgifb_reg_and_or(pVBInfo->P3c4, 0x0B, ~0xC0, (value & 0x300) >> 2);
4541         xgifb_reg_set(pVBInfo->P3d4, 0x4, (value & 0xFF));
4542
4543         /* Panel HRS SR2F[1:0] SR2E[7:0]  */
4544         value--;
4545         xgifb_reg_and_or(pVBInfo->P3c4, 0x2F, ~0x03, (value & 0x300) >> 8);
4546         xgifb_reg_set(pVBInfo->P3c4, 0x2E, (value & 0xFF));
4547
4548         /* HRE SR0C[2] CR05[4:0] */
4549         value = (LVDSHRE >> 3) + 2;
4550         xgifb_reg_and_or(pVBInfo->P3c4, 0x0C, ~0x04, (value & 0x20) >> 3);
4551         xgifb_reg_and_or(pVBInfo->P3d4, 0x05, ~0x1F, value & 0x1F);
4552
4553         /* Panel HRE SR2F[7:2]  */
4554         value--;
4555         xgifb_reg_and_or(pVBInfo->P3c4, 0x2F, ~0xFC, value << 2);
4556
4557         /* VT SR0A[0] CR07[5][0] CR06 */
4558         value = LVDSVT - 2;
4559         xgifb_reg_and_or(pVBInfo->P3c4, 0x0A, ~0x01, (value & 0x400) >> 10);
4560         xgifb_reg_and_or(pVBInfo->P3d4, 0x07, ~0x20, (value & 0x200) >> 4);
4561         xgifb_reg_and_or(pVBInfo->P3d4, 0x07, ~0x01, (value & 0x100) >> 8);
4562         xgifb_reg_set(pVBInfo->P3d4, 0x06, (value & 0xFF));
4563
4564         /* VBS SR0A[2] CR09[5] CR07[3] CR15 */
4565         value = LVDSVBS - 1;
4566         xgifb_reg_and_or(pVBInfo->P3c4, 0x0A, ~0x04, (value & 0x400) >> 8);
4567         xgifb_reg_and_or(pVBInfo->P3d4, 0x09, ~0x20, (value & 0x200) >> 4);
4568         xgifb_reg_and_or(pVBInfo->P3d4, 0x07, ~0x08, (value & 0x100) >> 5);
4569         xgifb_reg_set(pVBInfo->P3d4, 0x15, (value & 0xFF));
4570
4571         /* VBE SR0A[4] CR16 */
4572         value = LVDSVBE - 1;
4573         xgifb_reg_and_or(pVBInfo->P3c4, 0x0A, ~0x10, (value & 0x100) >> 4);
4574         xgifb_reg_set(pVBInfo->P3d4, 0x16, (value & 0xFF));
4575
4576         /* VRS SR0A[3] CR7[7][2] CR10 */
4577         value = LVDSVRS - 1;
4578         xgifb_reg_and_or(pVBInfo->P3c4, 0x0A, ~0x08, (value & 0x400) >> 7);
4579         xgifb_reg_and_or(pVBInfo->P3d4, 0x07, ~0x80, (value & 0x200) >> 2);
4580         xgifb_reg_and_or(pVBInfo->P3d4, 0x07, ~0x04, (value & 0x100) >> 6);
4581         xgifb_reg_set(pVBInfo->P3d4, 0x10, (value & 0xFF));
4582
4583         if (chip_id == XG27) {
4584                 /* Panel VRS SR35[2:0] SR34[7:0] */
4585                 xgifb_reg_and_or(pVBInfo->P3c4, 0x35, ~0x07,
4586                                         (value & 0x700) >> 8);
4587                 xgifb_reg_set(pVBInfo->P3c4, 0x34, value & 0xFF);
4588         } else {
4589                 /* Panel VRS SR3F[1:0] SR34[7:0] SR33[0] */
4590                 xgifb_reg_and_or(pVBInfo->P3c4, 0x3F, ~0x03,
4591                                         (value & 0x600) >> 9);
4592                 xgifb_reg_set(pVBInfo->P3c4, 0x34, (value >> 1) & 0xFF);
4593                 xgifb_reg_and_or(pVBInfo->P3d4, 0x33, ~0x01, value & 0x01);
4594         }
4595
4596         /* VRE SR0A[5] CR11[3:0] */
4597         value = LVDSVRE - 1;
4598         xgifb_reg_and_or(pVBInfo->P3c4, 0x0A, ~0x20, (value & 0x10) << 1);
4599         xgifb_reg_and_or(pVBInfo->P3d4, 0x11, ~0x0F, value & 0x0F);
4600
4601         /* Panel VRE SR3F[7:2] */
4602         if (chip_id == XG27)
4603                 xgifb_reg_and_or(pVBInfo->P3c4, 0x3F, ~0xFC,
4604                                         (value << 2) & 0xFC);
4605         else
4606                 /* SR3F[7] has to be 0, h/w bug */
4607                 xgifb_reg_and_or(pVBInfo->P3c4, 0x3F, ~0xFC,
4608                                         (value << 2) & 0x7C);
4609
4610         for (temp = 0, value = 0; temp < 3; temp++) {
4611
4612                 xgifb_reg_and_or(pVBInfo->P3c4, 0x31, ~0x30, value);
4613                 xgifb_reg_set(pVBInfo->P3c4,
4614                               0x2B, xgifb_info->lvds_data.VCLKData1);
4615                 xgifb_reg_set(pVBInfo->P3c4,
4616                               0x2C, xgifb_info->lvds_data.VCLKData2);
4617                 value += 0x10;
4618         }
4619
4620         if (!(modeflag & Charx8Dot)) {
4621                 inb(pVBInfo->P3da); /* reset 3da */
4622                 outb(0x13, pVBInfo->P3c0); /* set index */
4623                 /* set data, panning = 0, shift left 1 dot*/
4624                 outb(0x00, pVBInfo->P3c0);
4625
4626                 inb(pVBInfo->P3da); /* Enable Attribute */
4627                 outb(0x20, pVBInfo->P3c0);
4628
4629                 inb(pVBInfo->P3da); /* reset 3da */
4630         }
4631
4632 }
4633
4634 /* --------------------------------------------------------------------- */
4635 /* Function : XGI_IsLCDON */
4636 /* Input : */
4637 /* Output : 0 : Skip PSC Control */
4638 /* 1: Disable PSC */
4639 /* Description : */
4640 /* --------------------------------------------------------------------- */
4641 static unsigned char XGI_IsLCDON(struct vb_device_info *pVBInfo)
4642 {
4643         unsigned short tempax;
4644
4645         tempax = pVBInfo->VBInfo;
4646         if (tempax & SetCRT2ToDualEdge)
4647                 return 0;
4648         else if (tempax & (DisableCRT2Display | SwitchCRT2 | SetSimuScanMode))
4649                 return 1;
4650
4651         return 0;
4652 }
4653
4654 /* --------------------------------------------------------------------- */
4655 /* Function : XGI_EnableChISLCD */
4656 /* Input : */
4657 /* Output : 0 -> Not LCD mode */
4658 /* Description : if bool enable = true -> enable, else disable  */
4659 /* --------------------------------------------------------------------- */
4660 static unsigned char XGI_EnableChISLCD(struct vb_device_info *pVBInfo,
4661         bool enable)
4662 {
4663         unsigned short tempbx, tempah;
4664
4665         if (enable)
4666                 tempbx = pVBInfo->SetFlag & (EnableChA | EnableChB);
4667         else
4668                 tempbx = pVBInfo->SetFlag & (DisableChA | DisableChB);
4669
4670         tempah = ~((unsigned short) xgifb_reg_get(pVBInfo->Part1Port, 0x2E));
4671
4672         if (tempbx & (EnableChA | DisableChA)) {
4673                 if (!(tempah & 0x08)) /* Chk LCDA Mode */
4674                         return 0;
4675         }
4676
4677         if (!(tempbx & (EnableChB | DisableChB)))
4678                 return 0;
4679
4680         if (tempah & 0x01) /* Chk LCDB Mode */
4681                 return 1;
4682
4683         return 0;
4684 }
4685
4686 static void XGI_DisableBridge(struct xgifb_video_info *xgifb_info,
4687                 struct xgi_hw_device_info *HwDeviceExtension,
4688                 struct vb_device_info *pVBInfo)
4689 {
4690         unsigned short tempah = 0;
4691
4692         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
4693                         | VB_SIS302LV | VB_XGI301C)) {
4694                 tempah = 0x3F;
4695                 if (!(pVBInfo->VBInfo &
4696                     (DisableCRT2Display | SetSimuScanMode))) {
4697                         if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA) {
4698                                 if (pVBInfo->VBInfo & SetCRT2ToDualEdge) {
4699                                         tempah = 0x7F; /* Disable Channel A */
4700                                         if (!(pVBInfo->VBInfo &
4701                                               XGI_SetCRT2ToLCDA))
4702                                                 /* Disable Channel B */
4703                                                 tempah = 0xBF;
4704
4705                                         if (pVBInfo->SetFlag & DisableChB)
4706                                                 /* force to disable Cahnnel */
4707                                                 tempah &= 0xBF;
4708
4709                                         if (pVBInfo->SetFlag & DisableChA)
4710                                                 /* Force to disable Channel B */
4711                                                 tempah &= 0x7F;
4712                                 }
4713                         }
4714                 }
4715
4716                 /* disable part4_1f */
4717                 xgifb_reg_and(pVBInfo->Part4Port, 0x1F, tempah);
4718
4719                 if (pVBInfo->VBType & (VB_SIS302LV | VB_XGI301C)) {
4720                         if (((pVBInfo->VBInfo &
4721                               (SetCRT2ToLCD | XGI_SetCRT2ToLCDA))) ||
4722                                 (XGI_EnableChISLCD(pVBInfo, false)) ||
4723                                 (XGI_IsLCDON(pVBInfo)))
4724                                 /* LVDS Driver power down */
4725                                 xgifb_reg_or(pVBInfo->Part4Port, 0x30, 0x80);
4726                 }
4727
4728                 if ((pVBInfo->SetFlag & DisableChA) || (pVBInfo->VBInfo
4729                                 & (DisableCRT2Display | XGI_SetCRT2ToLCDA
4730                                                 | SetSimuScanMode))) {
4731                         if (pVBInfo->SetFlag & GatingCRT)
4732                                 XGI_EnableGatingCRT(HwDeviceExtension, pVBInfo);
4733                         XGI_DisplayOff(xgifb_info, HwDeviceExtension, pVBInfo);
4734                 }
4735
4736                 if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA) {
4737                         if ((pVBInfo->SetFlag & DisableChA) || (pVBInfo->VBInfo
4738                                         & XGI_SetCRT2ToLCDA))
4739                                 /* Power down */
4740                                 xgifb_reg_and(pVBInfo->Part1Port, 0x1e, 0xdf);
4741                 }
4742
4743                 /* disable TV as primary VGA swap */
4744                 xgifb_reg_and(pVBInfo->P3c4, 0x32, 0xdf);
4745
4746                 if ((pVBInfo->VBInfo & (SetSimuScanMode | SetCRT2ToDualEdge)))
4747                         xgifb_reg_and(pVBInfo->Part2Port, 0x00, 0xdf);
4748
4749                 if ((pVBInfo->SetFlag & DisableChB) ||
4750                     (pVBInfo->VBInfo &
4751                         (DisableCRT2Display | SetSimuScanMode)) ||
4752                     ((!(pVBInfo->VBInfo & XGI_SetCRT2ToLCDA)) &&
4753                     (pVBInfo->VBInfo &
4754                         (SetCRT2ToRAMDAC | SetCRT2ToLCD | SetCRT2ToTV))))
4755                         xgifb_reg_or(pVBInfo->Part1Port, 0x00, 0x80);
4756
4757                 if ((pVBInfo->SetFlag & DisableChB) ||
4758                     (pVBInfo->VBInfo &
4759                         (DisableCRT2Display | SetSimuScanMode)) ||
4760                     (!(pVBInfo->VBInfo & XGI_SetCRT2ToLCDA)) ||
4761                     (pVBInfo->VBInfo &
4762                         (SetCRT2ToRAMDAC | SetCRT2ToLCD | SetCRT2ToTV))) {
4763                         /* save Part1 index 0 */
4764                         tempah = xgifb_reg_get(pVBInfo->Part1Port, 0x00);
4765                         /* BTDAC = 1, avoid VB reset */
4766                         xgifb_reg_or(pVBInfo->Part1Port, 0x00, 0x10);
4767                         /* disable CRT2 */
4768                         xgifb_reg_and(pVBInfo->Part1Port, 0x1E, 0xDF);
4769                         /* restore Part1 index 0 */
4770                         xgifb_reg_set(pVBInfo->Part1Port, 0x00, tempah);
4771                 }
4772         } else { /* {301} */
4773                 if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToTV)) {
4774                         xgifb_reg_or(pVBInfo->Part1Port, 0x00, 0x80);
4775                         /* Disable CRT2 */
4776                         xgifb_reg_and(pVBInfo->Part1Port, 0x1E, 0xDF);
4777                         /* Disable TV asPrimary VGA swap */
4778                         xgifb_reg_and(pVBInfo->P3c4, 0x32, 0xDF);
4779                 }
4780
4781                 if (pVBInfo->VBInfo & (DisableCRT2Display | XGI_SetCRT2ToLCDA
4782                                 | SetSimuScanMode))
4783                         XGI_DisplayOff(xgifb_info, HwDeviceExtension, pVBInfo);
4784         }
4785 }
4786
4787 /* --------------------------------------------------------------------- */
4788 /* Function : XGI_GetTVPtrIndex */
4789 /* Input : */
4790 /* Output : */
4791 /* Description : bx 0 : ExtNTSC */
4792 /* 1 : StNTSC */
4793 /* 2 : ExtPAL */
4794 /* 3 : StPAL */
4795 /* 4 : ExtHiTV */
4796 /* 5 : StHiTV */
4797 /* 6 : Ext525i */
4798 /* 7 : St525i */
4799 /* 8 : Ext525p */
4800 /* 9 : St525p */
4801 /* A : Ext750p */
4802 /* B : St750p */
4803 /* --------------------------------------------------------------------- */
4804 static unsigned short XGI_GetTVPtrIndex(struct vb_device_info *pVBInfo)
4805 {
4806         unsigned short tempbx = 0;
4807
4808         if (pVBInfo->TVInfo & TVSetPAL)
4809                 tempbx = 2;
4810         if (pVBInfo->TVInfo & TVSetHiVision)
4811                 tempbx = 4;
4812         if (pVBInfo->TVInfo & TVSetYPbPr525i)
4813                 tempbx = 6;
4814         if (pVBInfo->TVInfo & TVSetYPbPr525p)
4815                 tempbx = 8;
4816         if (pVBInfo->TVInfo & TVSetYPbPr750p)
4817                 tempbx = 10;
4818         if (pVBInfo->TVInfo & TVSimuMode)
4819                 tempbx++;
4820
4821         return tempbx;
4822 }
4823
4824 /* --------------------------------------------------------------------- */
4825 /* Function : XGI_GetTVPtrIndex2 */
4826 /* Input : */
4827 /* Output : bx 0 : NTSC */
4828 /* 1 : PAL */
4829 /* 2 : PALM */
4830 /* 3 : PALN */
4831 /* 4 : NTSC1024x768 */
4832 /* 5 : PAL-M 1024x768 */
4833 /* 6-7: reserved */
4834 /* cl 0 : YFilter1 */
4835 /* 1 : YFilter2 */
4836 /* ch 0 : 301A */
4837 /* 1 : 301B/302B/301LV/302LV */
4838 /* Description : */
4839 /* --------------------------------------------------------------------- */
4840 static void XGI_GetTVPtrIndex2(unsigned short *tempbx, unsigned char *tempcl,
4841                 unsigned char *tempch, struct vb_device_info *pVBInfo)
4842 {
4843         *tempbx = 0;
4844         *tempcl = 0;
4845         *tempch = 0;
4846
4847         if (pVBInfo->TVInfo & TVSetPAL)
4848                 *tempbx = 1;
4849
4850         if (pVBInfo->TVInfo & TVSetPALM)
4851                 *tempbx = 2;
4852
4853         if (pVBInfo->TVInfo & TVSetPALN)
4854                 *tempbx = 3;
4855
4856         if (pVBInfo->TVInfo & NTSC1024x768) {
4857                 *tempbx = 4;
4858                 if (pVBInfo->TVInfo & TVSetPALM)
4859                         *tempbx = 5;
4860         }
4861
4862         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
4863                         | VB_SIS302LV | VB_XGI301C)) {
4864                 if ((!(pVBInfo->VBInfo & SetInSlaveMode)) || (pVBInfo->TVInfo
4865                                 & TVSimuMode)) {
4866                         *tempbx += 8;
4867                         *tempcl += 1;
4868                 }
4869         }
4870
4871         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
4872                         | VB_SIS302LV | VB_XGI301C))
4873                 (*tempch)++;
4874 }
4875
4876 static void XGI_SetDelayComp(struct vb_device_info *pVBInfo)
4877 {
4878         unsigned char tempah, tempbl, tempbh;
4879
4880         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
4881                         | VB_SIS302LV | VB_XGI301C)) {
4882                 if (pVBInfo->VBInfo & (SetCRT2ToLCD | XGI_SetCRT2ToLCDA
4883                                 | SetCRT2ToTV | SetCRT2ToRAMDAC)) {
4884                         tempbh = 0;
4885                         tempbl = XGI301TVDelay;
4886
4887                         if (pVBInfo->VBInfo & SetCRT2ToDualEdge)
4888                                 tempbl = tempbl >> 4;
4889                         if (pVBInfo->VBInfo &
4890                             (SetCRT2ToLCD | XGI_SetCRT2ToLCDA)) {
4891                                 tempbh = XGI301LCDDelay;
4892
4893                                 if (!(pVBInfo->VBInfo & XGI_SetCRT2ToLCDA))
4894                                         tempbl = tempbh;
4895                         }
4896
4897                         tempbl &= 0x0F;
4898                         tempbh &= 0xF0;
4899                         tempah = xgifb_reg_get(pVBInfo->Part1Port, 0x2D);
4900
4901                         if (pVBInfo->VBInfo & (SetCRT2ToRAMDAC | SetCRT2ToLCD
4902                                         | SetCRT2ToTV)) { /* Channel B */
4903                                 tempah &= 0xF0;
4904                                 tempah |= tempbl;
4905                         }
4906
4907                         if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA) {
4908                                 /* Channel A */
4909                                 tempah &= 0x0F;
4910                                 tempah |= tempbh;
4911                         }
4912                         xgifb_reg_set(pVBInfo->Part1Port, 0x2D, tempah);
4913                 }
4914         } else if (pVBInfo->IF_DEF_LVDS == 1) {
4915                 tempbl = 0;
4916                 tempbh = 0;
4917                 if (pVBInfo->VBInfo & SetCRT2ToLCD) {
4918                         tempah = XGI301LCDDelay;
4919                         tempah &= 0x0f;
4920                         tempah = tempah << 4;
4921                         xgifb_reg_and_or(pVBInfo->Part1Port, 0x2D, 0x0f,
4922                                         tempah);
4923                 }
4924         }
4925 }
4926
4927 static void XGI_SetLCDCap_A(unsigned short tempcx,
4928                             struct vb_device_info *pVBInfo)
4929 {
4930         unsigned short temp;
4931
4932         temp = xgifb_reg_get(pVBInfo->P3d4, 0x37);
4933
4934         if (temp & LCDRGB18Bit) {
4935                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x19, 0x0F,
4936                                 /* Enable Dither */
4937                                 (unsigned short) (0x20 | (tempcx & 0x00C0)));
4938                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x1A, 0x7F, 0x80);
4939         } else {
4940                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x19, 0x0F,
4941                                 (unsigned short) (0x30 | (tempcx & 0x00C0)));
4942                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x1A, 0x7F, 0x00);
4943         }
4944 }
4945
4946 /* --------------------------------------------------------------------- */
4947 /* Function : XGI_SetLCDCap_B */
4948 /* Input : cx -> LCD Capability */
4949 /* Output : */
4950 /* Description : */
4951 /* --------------------------------------------------------------------- */
4952 static void XGI_SetLCDCap_B(unsigned short tempcx,
4953                             struct vb_device_info *pVBInfo)
4954 {
4955         if (tempcx & EnableLCD24bpp) /* 24bits */
4956                 xgifb_reg_and_or(pVBInfo->Part2Port, 0x1A, 0xE0,
4957                                 (unsigned short) (((tempcx & 0x00ff) >> 6)
4958                                                 | 0x0c));
4959         else
4960                 xgifb_reg_and_or(pVBInfo->Part2Port, 0x1A, 0xE0,
4961                                 (unsigned short) (((tempcx & 0x00ff) >> 6)
4962                                                 | 0x18)); /* Enable Dither */
4963 }
4964
4965 static void XGI_LongWait(struct vb_device_info *pVBInfo)
4966 {
4967         unsigned short i;
4968
4969         i = xgifb_reg_get(pVBInfo->P3c4, 0x1F);
4970
4971         if (!(i & 0xC0)) {
4972                 for (i = 0; i < 0xFFFF; i++) {
4973                         if (!(inb(pVBInfo->P3da) & 0x08))
4974                                 break;
4975                 }
4976
4977                 for (i = 0; i < 0xFFFF; i++) {
4978                         if ((inb(pVBInfo->P3da) & 0x08))
4979                                 break;
4980                 }
4981         }
4982 }
4983
4984 static void SetSpectrum(struct vb_device_info *pVBInfo)
4985 {
4986         unsigned short index;
4987
4988         index = XGI_GetLCDCapPtr(pVBInfo);
4989
4990         /* disable down spectrum D[4] */
4991         xgifb_reg_and(pVBInfo->Part4Port, 0x30, 0x8F);
4992         XGI_LongWait(pVBInfo);
4993         xgifb_reg_or(pVBInfo->Part4Port, 0x30, 0x20); /* reset spectrum */
4994         XGI_LongWait(pVBInfo);
4995
4996         xgifb_reg_set(pVBInfo->Part4Port, 0x31,
4997                         pVBInfo->LCDCapList[index].Spectrum_31);
4998         xgifb_reg_set(pVBInfo->Part4Port, 0x32,
4999                         pVBInfo->LCDCapList[index].Spectrum_32);
5000         xgifb_reg_set(pVBInfo->Part4Port, 0x33,
5001                         pVBInfo->LCDCapList[index].Spectrum_33);
5002         xgifb_reg_set(pVBInfo->Part4Port, 0x34,
5003                         pVBInfo->LCDCapList[index].Spectrum_34);
5004         XGI_LongWait(pVBInfo);
5005         xgifb_reg_or(pVBInfo->Part4Port, 0x30, 0x40); /* enable spectrum */
5006 }
5007
5008 static void XGI_SetLCDCap(struct vb_device_info *pVBInfo)
5009 {
5010         unsigned short tempcx;
5011
5012         tempcx = pVBInfo->LCDCapList[XGI_GetLCDCapPtr(pVBInfo)].LCD_Capability;
5013
5014         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV |
5015                 VB_SIS302LV | VB_XGI301C)) {
5016                 if (pVBInfo->VBType &
5017                     (VB_SIS301LV | VB_SIS302LV | VB_XGI301C)) {
5018                         /* Set 301LV Capability */
5019                         xgifb_reg_set(pVBInfo->Part4Port, 0x24,
5020                                         (unsigned char) (tempcx & 0x1F));
5021                 }
5022                 /* VB Driving */
5023                 xgifb_reg_and_or(pVBInfo->Part4Port, 0x0D,
5024                                 ~((EnableVBCLKDRVLOW | EnablePLLSPLOW) >> 8),
5025                                 (unsigned short) ((tempcx & (EnableVBCLKDRVLOW
5026                                                 | EnablePLLSPLOW)) >> 8));
5027
5028                 if (pVBInfo->VBInfo & SetCRT2ToLCD)
5029                         XGI_SetLCDCap_B(tempcx, pVBInfo);
5030                 else if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA)
5031                         XGI_SetLCDCap_A(tempcx, pVBInfo);
5032
5033                 if (pVBInfo->VBType & (VB_SIS302LV | VB_XGI301C)) {
5034                         if (tempcx & EnableSpectrum)
5035                                 SetSpectrum(pVBInfo);
5036                 }
5037         } else {
5038                 /* LVDS,CH7017 */
5039                 XGI_SetLCDCap_A(tempcx, pVBInfo);
5040         }
5041 }
5042
5043 /* --------------------------------------------------------------------- */
5044 /* Function : XGI_SetAntiFlicker */
5045 /* Input : */
5046 /* Output : */
5047 /* Description : Set TV Customized Param. */
5048 /* --------------------------------------------------------------------- */
5049 static void XGI_SetAntiFlicker(unsigned short ModeNo,
5050                                unsigned short ModeIdIndex,
5051                                struct vb_device_info *pVBInfo)
5052 {
5053         unsigned short tempbx;
5054
5055         unsigned char tempah;
5056
5057         if (pVBInfo->TVInfo & (TVSetYPbPr525p | TVSetYPbPr750p))
5058                 return;
5059
5060         tempbx = XGI_GetTVPtrIndex(pVBInfo);
5061         tempbx &= 0xFE;
5062         tempah = TVAntiFlickList[tempbx];
5063         tempah = tempah << 4;
5064
5065         xgifb_reg_and_or(pVBInfo->Part2Port, 0x0A, 0x8F, tempah);
5066 }
5067
5068 static void XGI_SetEdgeEnhance(unsigned short ModeNo,
5069                                unsigned short ModeIdIndex,
5070                                struct vb_device_info *pVBInfo)
5071 {
5072         unsigned short tempbx;
5073
5074         unsigned char tempah;
5075
5076         tempbx = XGI_GetTVPtrIndex(pVBInfo);
5077         tempbx &= 0xFE;
5078         tempah = TVEdgeList[tempbx];
5079         tempah = tempah << 5;
5080
5081         xgifb_reg_and_or(pVBInfo->Part2Port, 0x3A, 0x1F, tempah);
5082 }
5083
5084 static void XGI_SetPhaseIncr(struct vb_device_info *pVBInfo)
5085 {
5086         unsigned short tempbx;
5087
5088         unsigned char tempcl, tempch;
5089
5090         unsigned long tempData;
5091
5092         XGI_GetTVPtrIndex2(&tempbx, &tempcl, &tempch, pVBInfo); /* bx, cl, ch */
5093         tempData = TVPhaseList[tempbx];
5094
5095         xgifb_reg_set(pVBInfo->Part2Port, 0x31, (unsigned short) (tempData
5096                         & 0x000000FF));
5097         xgifb_reg_set(pVBInfo->Part2Port, 0x32, (unsigned short) ((tempData
5098                         & 0x0000FF00) >> 8));
5099         xgifb_reg_set(pVBInfo->Part2Port, 0x33, (unsigned short) ((tempData
5100                         & 0x00FF0000) >> 16));
5101         xgifb_reg_set(pVBInfo->Part2Port, 0x34, (unsigned short) ((tempData
5102                         & 0xFF000000) >> 24));
5103 }
5104
5105 static void XGI_SetYFilter(unsigned short ModeNo, unsigned short ModeIdIndex,
5106                 struct vb_device_info *pVBInfo)
5107 {
5108         unsigned short tempbx, index;
5109         unsigned char const *filterPtr;
5110         unsigned char tempcl, tempch, tempal;
5111
5112         XGI_GetTVPtrIndex2(&tempbx, &tempcl, &tempch, pVBInfo); /* bx, cl, ch */
5113
5114         switch (tempbx) {
5115         case 0x00:
5116         case 0x04:
5117                 filterPtr = NTSCYFilter1;
5118                 break;
5119
5120         case 0x01:
5121                 filterPtr = PALYFilter1;
5122                 break;
5123
5124         case 0x02:
5125         case 0x05:
5126         case 0x0D:
5127         case 0x03:
5128                 filterPtr = xgifb_palmn_yfilter1;
5129                 break;
5130
5131         case 0x08:
5132         case 0x0C:
5133         case 0x0A:
5134         case 0x0B:
5135         case 0x09:
5136                 filterPtr = xgifb_yfilter2;
5137                 break;
5138
5139         default:
5140                 return;
5141         }
5142
5143         tempal = XGI330_EModeIDTable[ModeIdIndex].VB_ExtTVYFilterIndex;
5144         if (tempcl == 0)
5145                 index = tempal * 4;
5146         else
5147                 index = tempal * 7;
5148
5149         if ((tempcl == 0) && (tempch == 1)) {
5150                 xgifb_reg_set(pVBInfo->Part2Port, 0x35, 0);
5151                 xgifb_reg_set(pVBInfo->Part2Port, 0x36, 0);
5152                 xgifb_reg_set(pVBInfo->Part2Port, 0x37, 0);
5153                 xgifb_reg_set(pVBInfo->Part2Port, 0x38, filterPtr[index++]);
5154         } else {
5155                 xgifb_reg_set(pVBInfo->Part2Port, 0x35, filterPtr[index++]);
5156                 xgifb_reg_set(pVBInfo->Part2Port, 0x36, filterPtr[index++]);
5157                 xgifb_reg_set(pVBInfo->Part2Port, 0x37, filterPtr[index++]);
5158                 xgifb_reg_set(pVBInfo->Part2Port, 0x38, filterPtr[index++]);
5159         }
5160
5161         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
5162                         | VB_SIS302LV | VB_XGI301C)) {
5163                 xgifb_reg_set(pVBInfo->Part2Port, 0x48, filterPtr[index++]);
5164                 xgifb_reg_set(pVBInfo->Part2Port, 0x49, filterPtr[index++]);
5165                 xgifb_reg_set(pVBInfo->Part2Port, 0x4A, filterPtr[index++]);
5166         }
5167 }
5168
5169 /* --------------------------------------------------------------------- */
5170 /* Function : XGI_OEM310Setting */
5171 /* Input : */
5172 /* Output : */
5173 /* Description : Customized Param. for 301 */
5174 /* --------------------------------------------------------------------- */
5175 static void XGI_OEM310Setting(unsigned short ModeNo,
5176                               unsigned short ModeIdIndex,
5177                               struct vb_device_info *pVBInfo)
5178 {
5179         XGI_SetDelayComp(pVBInfo);
5180
5181         if (pVBInfo->VBInfo & (SetCRT2ToLCD | XGI_SetCRT2ToLCDA))
5182                 XGI_SetLCDCap(pVBInfo);
5183
5184         if (pVBInfo->VBInfo & SetCRT2ToTV) {
5185                 XGI_SetPhaseIncr(pVBInfo);
5186                 XGI_SetYFilter(ModeNo, ModeIdIndex, pVBInfo);
5187                 XGI_SetAntiFlicker(ModeNo, ModeIdIndex, pVBInfo);
5188
5189                 if (pVBInfo->VBType & VB_SIS301)
5190                         XGI_SetEdgeEnhance(ModeNo, ModeIdIndex, pVBInfo);
5191         }
5192 }
5193
5194 /* --------------------------------------------------------------------- */
5195 /* Function : XGI_SetCRT2ModeRegs */
5196 /* Input : */
5197 /* Output : */
5198 /* Description : Origin code for crt2group */
5199 /* --------------------------------------------------------------------- */
5200 static void XGI_SetCRT2ModeRegs(unsigned short ModeNo,
5201                 struct xgi_hw_device_info *HwDeviceExtension,
5202                 struct vb_device_info *pVBInfo)
5203 {
5204         unsigned short tempbl;
5205         short tempcl;
5206
5207         unsigned char tempah;
5208
5209         tempah = 0;
5210         if (!(pVBInfo->VBInfo & DisableCRT2Display)) {
5211                 tempah = xgifb_reg_get(pVBInfo->Part1Port, 0x00);
5212                 tempah &= ~0x10; /* BTRAMDAC */
5213                 tempah |= 0x40; /* BTRAM */
5214
5215                 if (pVBInfo->VBInfo & (SetCRT2ToRAMDAC | SetCRT2ToTV
5216                                 | SetCRT2ToLCD)) {
5217                         tempah = 0x40; /* BTDRAM */
5218                         tempcl = pVBInfo->ModeType;
5219                         tempcl -= ModeVGA;
5220                         if (tempcl >= 0) {
5221                                 /* BT Color */
5222                                 tempah = (0x008 >> tempcl);
5223                                 if (tempah == 0)
5224                                         tempah = 1;
5225                                 tempah |= 0x040;
5226                         }
5227                         if (pVBInfo->VBInfo & SetInSlaveMode)
5228                                 tempah ^= 0x50; /* BTDAC */
5229                 }
5230         }
5231
5232         xgifb_reg_set(pVBInfo->Part1Port, 0x00, tempah);
5233         tempah = 0x08;
5234         tempbl = 0xf0;
5235
5236         if (pVBInfo->VBInfo & DisableCRT2Display)
5237                 goto reg_and_or;
5238
5239         tempah = 0x00;
5240         tempbl = 0xff;
5241
5242         if (!(pVBInfo->VBInfo & (SetCRT2ToRAMDAC | SetCRT2ToTV |
5243                                  SetCRT2ToLCD | XGI_SetCRT2ToLCDA)))
5244                 goto reg_and_or;
5245
5246         if ((pVBInfo->VBInfo & XGI_SetCRT2ToLCDA) &&
5247             (!(pVBInfo->VBInfo & SetSimuScanMode))) {
5248                 tempbl &= 0xf7;
5249                 tempah |= 0x01;
5250                 goto reg_and_or;
5251         }
5252
5253         if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA) {
5254                 tempbl &= 0xf7;
5255                 tempah |= 0x01;
5256         }
5257
5258         if (!(pVBInfo->VBInfo & (SetCRT2ToRAMDAC | SetCRT2ToTV | SetCRT2ToLCD)))
5259                 goto reg_and_or;
5260
5261         tempbl &= 0xf8;
5262         tempah = 0x01;
5263
5264         if (!(pVBInfo->VBInfo & SetInSlaveMode))
5265                 tempah |= 0x02;
5266
5267         if (!(pVBInfo->VBInfo & SetCRT2ToRAMDAC)) {
5268                 tempah = tempah ^ 0x05;
5269                 if (!(pVBInfo->VBInfo & SetCRT2ToLCD))
5270                         tempah = tempah ^ 0x01;
5271         }
5272
5273         if (!(pVBInfo->VBInfo & SetCRT2ToDualEdge))
5274                 tempah |= 0x08;
5275
5276 reg_and_or:
5277         xgifb_reg_and_or(pVBInfo->Part1Port, 0x2e, tempbl, tempah);
5278
5279         if (pVBInfo->VBInfo & (SetCRT2ToRAMDAC | SetCRT2ToTV | SetCRT2ToLCD
5280                         | XGI_SetCRT2ToLCDA)) {
5281                 tempah &= (~0x08);
5282                 if ((pVBInfo->ModeType == ModeVGA) && (!(pVBInfo->VBInfo
5283                                 & SetInSlaveMode))) {
5284                         tempah |= 0x010;
5285                 }
5286                 tempah |= 0x080;
5287
5288                 if (pVBInfo->VBInfo & SetCRT2ToTV) {
5289                         tempah |= 0x020;
5290                         if (pVBInfo->VBInfo & DriverMode)
5291                                 tempah = tempah ^ 0x20;
5292                 }
5293
5294                 xgifb_reg_and_or(pVBInfo->Part4Port, 0x0D, ~0x0BF, tempah);
5295                 tempah = 0;
5296
5297                 if (pVBInfo->LCDInfo & SetLCDDualLink)
5298                         tempah |= 0x40;
5299
5300                 if (pVBInfo->VBInfo & SetCRT2ToTV) {
5301                         if (pVBInfo->TVInfo & RPLLDIV2XO)
5302                                 tempah |= 0x40;
5303                 }
5304
5305                 if ((pVBInfo->LCDResInfo == Panel_1280x1024)
5306                                 || (pVBInfo->LCDResInfo == Panel_1280x1024x75))
5307                         tempah |= 0x80;
5308
5309                 if (pVBInfo->LCDResInfo == Panel_1280x960)
5310                         tempah |= 0x80;
5311
5312                 xgifb_reg_set(pVBInfo->Part4Port, 0x0C, tempah);
5313         }
5314
5315         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
5316                         | VB_SIS302LV | VB_XGI301C)) {
5317                 tempah = 0;
5318                 tempbl = 0xfb;
5319
5320                 if (pVBInfo->VBInfo & SetCRT2ToDualEdge) {
5321                         tempbl = 0xff;
5322                         if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA)
5323                                 tempah |= 0x04; /* shampoo 0129 */
5324                 }
5325
5326                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x13, tempbl, tempah);
5327                 tempah = 0x00;
5328                 tempbl = 0xcf;
5329                 if (!(pVBInfo->VBInfo & DisableCRT2Display)) {
5330                         if (pVBInfo->VBInfo & SetCRT2ToDualEdge)
5331                                 tempah |= 0x30;
5332                 }
5333
5334                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x2c, tempbl, tempah);
5335                 tempah = 0;
5336                 tempbl = 0x3f;
5337
5338                 if (!(pVBInfo->VBInfo & DisableCRT2Display)) {
5339                         if (pVBInfo->VBInfo & SetCRT2ToDualEdge)
5340                                 tempah |= 0xc0;
5341                 }
5342                 xgifb_reg_and_or(pVBInfo->Part4Port, 0x21, tempbl, tempah);
5343         }
5344
5345         tempah = 0;
5346         tempbl = 0x7f;
5347         if (!(pVBInfo->VBInfo & XGI_SetCRT2ToLCDA)) {
5348                 tempbl = 0xff;
5349                 if (!(pVBInfo->VBInfo & SetCRT2ToDualEdge))
5350                         tempah |= 0x80;
5351         }
5352
5353         xgifb_reg_and_or(pVBInfo->Part4Port, 0x23, tempbl, tempah);
5354
5355         if (pVBInfo->VBType & (VB_SIS302LV | VB_XGI301C)) {
5356                 if (pVBInfo->LCDInfo & SetLCDDualLink) {
5357                         xgifb_reg_or(pVBInfo->Part4Port, 0x27, 0x20);
5358                         xgifb_reg_or(pVBInfo->Part4Port, 0x34, 0x10);
5359                 }
5360         }
5361 }
5362
5363
5364 void XGI_UnLockCRT2(struct xgi_hw_device_info *HwDeviceExtension,
5365                 struct vb_device_info *pVBInfo)
5366 {
5367
5368         xgifb_reg_and_or(pVBInfo->Part1Port, 0x2f, 0xFF, 0x01);
5369
5370 }
5371
5372 void XGI_LockCRT2(struct xgi_hw_device_info *HwDeviceExtension,
5373                 struct vb_device_info *pVBInfo)
5374 {
5375
5376         xgifb_reg_and_or(pVBInfo->Part1Port, 0x2F, 0xFE, 0x00);
5377
5378 }
5379
5380 unsigned char XGI_BridgeIsOn(struct vb_device_info *pVBInfo)
5381 {
5382         unsigned short flag;
5383
5384         if (pVBInfo->IF_DEF_LVDS == 1) {
5385                 return 1;
5386         } else {
5387                 flag = xgifb_reg_get(pVBInfo->Part4Port, 0x00);
5388                 if ((flag == 1) || (flag == 2))
5389                         return 1; /* 301b */
5390                 else
5391                         return 0;
5392         }
5393 }
5394
5395 unsigned short XGI_GetRatePtrCRT2(struct xgi_hw_device_info *pXGIHWDE,
5396                 unsigned short ModeNo, unsigned short ModeIdIndex,
5397                 struct vb_device_info *pVBInfo)
5398 {
5399         const u8 LCDARefreshIndex[] = {
5400                 0x00, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x00 };
5401
5402         unsigned short RefreshRateTableIndex, i, index, temp;
5403
5404         index = xgifb_reg_get(pVBInfo->P3d4, 0x33);
5405         index = index >> pVBInfo->SelectCRT2Rate;
5406         index &= 0x0F;
5407
5408         if (pVBInfo->LCDInfo & LCDNonExpanding)
5409                 index = 0;
5410
5411         if (index > 0)
5412                 index--;
5413
5414         if (pVBInfo->SetFlag & ProgrammingCRT2) {
5415                 if (pVBInfo->VBInfo & (SetCRT2ToLCD | XGI_SetCRT2ToLCDA)) {
5416                         if (pVBInfo->IF_DEF_LVDS == 0) {
5417                                 temp = LCDARefreshIndex[
5418                                         pVBInfo->LCDResInfo & 0x07];
5419
5420                                 if (index > temp)
5421                                         index = temp;
5422                         } else {
5423                                 index = 0;
5424                         }
5425                 }
5426         }
5427
5428         RefreshRateTableIndex = XGI330_EModeIDTable[ModeIdIndex].REFindex;
5429         ModeNo = XGI330_RefIndex[RefreshRateTableIndex].ModeID;
5430         if (pXGIHWDE->jChipType >= XG20) { /* for XG20, XG21, XG27 */
5431                 if ((XGI330_RefIndex[RefreshRateTableIndex].XRes == 800) &&
5432                     (XGI330_RefIndex[RefreshRateTableIndex].YRes == 600)) {
5433                         index++;
5434                 }
5435                 /* do the similar adjustment like XGISearchCRT1Rate() */
5436                 if ((XGI330_RefIndex[RefreshRateTableIndex].XRes == 1024) &&
5437                     (XGI330_RefIndex[RefreshRateTableIndex].YRes == 768)) {
5438                         index++;
5439                 }
5440                 if ((XGI330_RefIndex[RefreshRateTableIndex].XRes == 1280) &&
5441                     (XGI330_RefIndex[RefreshRateTableIndex].YRes == 1024)) {
5442                         index++;
5443                 }
5444         }
5445
5446         i = 0;
5447         do {
5448                 if (XGI330_RefIndex[RefreshRateTableIndex + i].
5449                         ModeID != ModeNo)
5450                         break;
5451                 temp = XGI330_RefIndex[RefreshRateTableIndex + i].Ext_InfoFlag;
5452                 temp &= ModeTypeMask;
5453                 if (temp < pVBInfo->ModeType)
5454                         break;
5455                 i++;
5456                 index--;
5457
5458         } while (index != 0xFFFF);
5459         if (!(pVBInfo->VBInfo & SetCRT2ToRAMDAC)) {
5460                 if (pVBInfo->VBInfo & SetInSlaveMode) {
5461                         temp = XGI330_RefIndex[RefreshRateTableIndex + i - 1].
5462                                 Ext_InfoFlag;
5463                         if (temp & InterlaceMode)
5464                                 i++;
5465                 }
5466         }
5467         i--;
5468         if ((pVBInfo->SetFlag & ProgrammingCRT2)) {
5469                 temp = XGI_AjustCRT2Rate(ModeNo, ModeIdIndex,
5470                                 RefreshRateTableIndex, &i, pVBInfo);
5471         }
5472         return RefreshRateTableIndex + i;
5473 }
5474
5475 static void XGI_SetLCDAGroup(unsigned short ModeNo, unsigned short ModeIdIndex,
5476                 struct xgi_hw_device_info *HwDeviceExtension,
5477                 struct vb_device_info *pVBInfo)
5478 {
5479         unsigned short RefreshRateTableIndex;
5480
5481         pVBInfo->SetFlag |= ProgrammingCRT2;
5482         RefreshRateTableIndex = XGI_GetRatePtrCRT2(HwDeviceExtension, ModeNo,
5483                         ModeIdIndex, pVBInfo);
5484         XGI_GetLVDSResInfo(ModeNo, ModeIdIndex, pVBInfo);
5485         XGI_GetLVDSData(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
5486         XGI_ModCRT1Regs(ModeNo, ModeIdIndex, RefreshRateTableIndex,
5487                         HwDeviceExtension, pVBInfo);
5488         XGI_SetLVDSRegs(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
5489         XGI_SetCRT2ECLK(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
5490 }
5491
5492 static unsigned char XGI_SetCRT2Group301(unsigned short ModeNo,
5493                 struct xgi_hw_device_info *HwDeviceExtension,
5494                 struct vb_device_info *pVBInfo)
5495 {
5496         unsigned short ModeIdIndex, RefreshRateTableIndex;
5497
5498         pVBInfo->SetFlag |= ProgrammingCRT2;
5499         XGI_SearchModeID(ModeNo, &ModeIdIndex, pVBInfo);
5500         pVBInfo->SelectCRT2Rate = 4;
5501         RefreshRateTableIndex = XGI_GetRatePtrCRT2(HwDeviceExtension, ModeNo,
5502                         ModeIdIndex, pVBInfo);
5503         XGI_SaveCRT2Info(ModeNo, pVBInfo);
5504         XGI_GetCRT2ResInfo(ModeNo, ModeIdIndex, pVBInfo);
5505         XGI_GetCRT2Data(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
5506         XGI_PreSetGroup1(ModeNo, ModeIdIndex, HwDeviceExtension,
5507                         RefreshRateTableIndex, pVBInfo);
5508         XGI_SetGroup1(ModeNo, ModeIdIndex, HwDeviceExtension,
5509                         RefreshRateTableIndex, pVBInfo);
5510         XGI_SetLockRegs(ModeNo, ModeIdIndex, HwDeviceExtension,
5511                         RefreshRateTableIndex, pVBInfo);
5512         XGI_SetGroup2(ModeNo, ModeIdIndex, RefreshRateTableIndex,
5513                         HwDeviceExtension, pVBInfo);
5514         XGI_SetLCDRegs(ModeNo, ModeIdIndex, HwDeviceExtension,
5515                         RefreshRateTableIndex, pVBInfo);
5516         XGI_SetTap4Regs(pVBInfo);
5517         XGI_SetGroup3(ModeNo, ModeIdIndex, pVBInfo);
5518         XGI_SetGroup4(ModeNo, ModeIdIndex, RefreshRateTableIndex,
5519                         HwDeviceExtension, pVBInfo);
5520         XGI_SetCRT2VCLK(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
5521         XGI_SetGroup5(ModeNo, ModeIdIndex, pVBInfo);
5522         XGI_AutoThreshold(pVBInfo);
5523         return 1;
5524 }
5525
5526 void XGI_SenseCRT1(struct vb_device_info *pVBInfo)
5527 {
5528         unsigned char CRTCData[17] = { 0x5F, 0x4F, 0x50, 0x82, 0x55, 0x81,
5529                         0x0B, 0x3E, 0xE9, 0x0B, 0xDF, 0xE7, 0x04, 0x00, 0x00,
5530                         0x05, 0x00 };
5531
5532         unsigned char SR01 = 0, SR1F = 0, SR07 = 0, SR06 = 0;
5533
5534         unsigned char CR17, CR63, SR31;
5535         unsigned short temp;
5536         unsigned char DAC_TEST_PARMS[3] = { 0x0F, 0x0F, 0x0F };
5537
5538         int i;
5539         xgifb_reg_set(pVBInfo->P3c4, 0x05, 0x86);
5540
5541         /* to fix XG42 single LCD sense to CRT+LCD */
5542         xgifb_reg_set(pVBInfo->P3d4, 0x57, 0x4A);
5543         xgifb_reg_set(pVBInfo->P3d4, 0x53, (unsigned char) (xgifb_reg_get(
5544                         pVBInfo->P3d4, 0x53) | 0x02));
5545
5546         SR31 = (unsigned char) xgifb_reg_get(pVBInfo->P3c4, 0x31);
5547         CR63 = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x63);
5548         SR01 = (unsigned char) xgifb_reg_get(pVBInfo->P3c4, 0x01);
5549
5550         xgifb_reg_set(pVBInfo->P3c4, 0x01, (unsigned char) (SR01 & 0xDF));
5551         xgifb_reg_set(pVBInfo->P3d4, 0x63, (unsigned char) (CR63 & 0xBF));
5552
5553         CR17 = (unsigned char) xgifb_reg_get(pVBInfo->P3d4, 0x17);
5554         xgifb_reg_set(pVBInfo->P3d4, 0x17, (unsigned char) (CR17 | 0x80));
5555
5556         SR1F = (unsigned char) xgifb_reg_get(pVBInfo->P3c4, 0x1F);
5557         xgifb_reg_set(pVBInfo->P3c4, 0x1F, (unsigned char) (SR1F | 0x04));
5558
5559         SR07 = (unsigned char) xgifb_reg_get(pVBInfo->P3c4, 0x07);
5560         xgifb_reg_set(pVBInfo->P3c4, 0x07, (unsigned char) (SR07 & 0xFB));
5561         SR06 = (unsigned char) xgifb_reg_get(pVBInfo->P3c4, 0x06);
5562         xgifb_reg_set(pVBInfo->P3c4, 0x06, (unsigned char) (SR06 & 0xC3));
5563
5564         xgifb_reg_set(pVBInfo->P3d4, 0x11, 0x00);
5565
5566         for (i = 0; i < 8; i++)
5567                 xgifb_reg_set(pVBInfo->P3d4, (unsigned short) i, CRTCData[i]);
5568
5569         for (i = 8; i < 11; i++)
5570                 xgifb_reg_set(pVBInfo->P3d4, (unsigned short) (i + 8),
5571                                 CRTCData[i]);
5572
5573         for (i = 11; i < 13; i++)
5574                 xgifb_reg_set(pVBInfo->P3d4, (unsigned short) (i + 4),
5575                                 CRTCData[i]);
5576
5577         for (i = 13; i < 16; i++)
5578                 xgifb_reg_set(pVBInfo->P3c4, (unsigned short) (i - 3),
5579                                 CRTCData[i]);
5580
5581         xgifb_reg_set(pVBInfo->P3c4, 0x0E, (unsigned char) (CRTCData[16]
5582                         & 0xE0));
5583
5584         xgifb_reg_set(pVBInfo->P3c4, 0x31, 0x00);
5585         xgifb_reg_set(pVBInfo->P3c4, 0x2B, 0x1B);
5586         xgifb_reg_set(pVBInfo->P3c4, 0x2C, 0xE1);
5587
5588         outb(0x00, pVBInfo->P3c8);
5589
5590         for (i = 0; i < 256; i++) {
5591                 outb((unsigned char) DAC_TEST_PARMS[0], (pVBInfo->P3c8 + 1));
5592                 outb((unsigned char) DAC_TEST_PARMS[1], (pVBInfo->P3c8 + 1));
5593                 outb((unsigned char) DAC_TEST_PARMS[2], (pVBInfo->P3c8 + 1));
5594         }
5595
5596         mdelay(1);
5597
5598         XGI_WaitDisply(pVBInfo);
5599         temp = inb(pVBInfo->P3c2);
5600
5601         if (temp & 0x10)
5602                 xgifb_reg_and_or(pVBInfo->P3d4, 0x32, 0xDF, 0x20);
5603         else
5604                 xgifb_reg_and_or(pVBInfo->P3d4, 0x32, 0xDF, 0x00);
5605
5606         /* avoid display something, set BLACK DAC if not restore DAC */
5607         outb(0x00, pVBInfo->P3c8);
5608
5609         for (i = 0; i < 256; i++) {
5610                 outb(0, (pVBInfo->P3c8 + 1));
5611                 outb(0, (pVBInfo->P3c8 + 1));
5612                 outb(0, (pVBInfo->P3c8 + 1));
5613         }
5614
5615         xgifb_reg_set(pVBInfo->P3c4, 0x01, SR01);
5616         xgifb_reg_set(pVBInfo->P3d4, 0x63, CR63);
5617         xgifb_reg_set(pVBInfo->P3c4, 0x31, SR31);
5618
5619         xgifb_reg_set(pVBInfo->P3d4, 0x53, (unsigned char) (xgifb_reg_get(
5620                         pVBInfo->P3d4, 0x53) & 0xFD));
5621         xgifb_reg_set(pVBInfo->P3c4, 0x1F, (unsigned char) SR1F);
5622 }
5623
5624 static void XGI_EnableBridge(struct xgifb_video_info *xgifb_info,
5625                 struct xgi_hw_device_info *HwDeviceExtension,
5626                 struct vb_device_info *pVBInfo)
5627 {
5628         unsigned short tempah;
5629
5630         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
5631                         | VB_SIS302LV | VB_XGI301C)) {
5632                 if (!(pVBInfo->SetFlag & DisableChA)) {
5633                         if (pVBInfo->SetFlag & EnableChA) {
5634                                 /* Power on */
5635                                 xgifb_reg_set(pVBInfo->Part1Port, 0x1E, 0x20);
5636                         } else if (pVBInfo->VBInfo & SetCRT2ToDualEdge) {
5637                                 /* Power on */
5638                                 xgifb_reg_set(pVBInfo->Part1Port,
5639                                                 0x1E, 0x20);
5640                         }
5641                 }
5642
5643                 if (!(pVBInfo->SetFlag & DisableChB)) {
5644                         if ((pVBInfo->SetFlag & EnableChB) || (pVBInfo->VBInfo
5645                                         & (SetCRT2ToLCD | SetCRT2ToTV
5646                                                         | SetCRT2ToRAMDAC))) {
5647                                 tempah = (unsigned char) xgifb_reg_get(
5648                                                 pVBInfo->P3c4, 0x32);
5649                                 tempah &= 0xDF;
5650                                 if (pVBInfo->VBInfo & SetInSlaveMode) {
5651                                         if (!(pVBInfo->VBInfo &
5652                                               SetCRT2ToRAMDAC))
5653                                                 tempah |= 0x20;
5654                                 }
5655                                 xgifb_reg_set(pVBInfo->P3c4, 0x32, tempah);
5656                                 xgifb_reg_or(pVBInfo->P3c4, 0x1E, 0x20);
5657
5658                                 tempah = (unsigned char) xgifb_reg_get(
5659                                                 pVBInfo->Part1Port, 0x2E);
5660
5661                                 if (!(tempah & 0x80))
5662                                         xgifb_reg_or(pVBInfo->Part1Port,
5663                                                         0x2E, 0x80);
5664                                 xgifb_reg_and(pVBInfo->Part1Port, 0x00, 0x7F);
5665                         }
5666                 }
5667
5668                 if ((pVBInfo->SetFlag & (EnableChA | EnableChB))
5669                                 || (!(pVBInfo->VBInfo & DisableCRT2Display))) {
5670                         xgifb_reg_and_or(pVBInfo->Part2Port, 0x00, ~0xE0,
5671                                         0x20); /* shampoo 0129 */
5672                         if (pVBInfo->VBType & (VB_SIS302LV | VB_XGI301C)) {
5673                                 if (!XGI_EnableChISLCD(pVBInfo, false)) {
5674                                         if (XGI_EnableChISLCD(pVBInfo, true) ||
5675                                             (pVBInfo->VBInfo &
5676                                             (SetCRT2ToLCD | XGI_SetCRT2ToLCDA)))
5677                                                 /* LVDS PLL power on */
5678                                                 xgifb_reg_and(
5679                                                         pVBInfo->Part4Port,
5680                                                         0x2A,
5681                                                         0x7F);
5682                                 }
5683                                 /* LVDS Driver power on */
5684                                 xgifb_reg_and(pVBInfo->Part4Port, 0x30, 0x7F);
5685                         }
5686                 }
5687
5688                 tempah = 0x00;
5689
5690                 if (!(pVBInfo->VBInfo & DisableCRT2Display)) {
5691                         tempah = 0xc0;
5692
5693                         if (!(pVBInfo->VBInfo & SetSimuScanMode) &&
5694                             (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA) &&
5695                             (pVBInfo->VBInfo & SetCRT2ToDualEdge)) {
5696                                 tempah = tempah & 0x40;
5697                                 if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA)
5698                                         tempah = tempah ^ 0xC0;
5699
5700                                 if (pVBInfo->SetFlag & DisableChB)
5701                                         tempah &= 0xBF;
5702
5703                                 if (pVBInfo->SetFlag &  DisableChA)
5704                                         tempah &= 0x7F;
5705
5706                                 if (pVBInfo->SetFlag &  EnableChB)
5707                                         tempah |= 0x40;
5708
5709                                 if (pVBInfo->SetFlag &  EnableChA)
5710                                         tempah |= 0x80;
5711                         }
5712                 }
5713
5714                 /* EnablePart4_1F */
5715                 xgifb_reg_or(pVBInfo->Part4Port, 0x1F, tempah);
5716
5717                 if (!(pVBInfo->SetFlag & DisableChA)) {
5718                         if (!(pVBInfo->SetFlag & GatingCRT)) {
5719                                 XGI_DisableGatingCRT(HwDeviceExtension,
5720                                                      pVBInfo);
5721                                 XGI_DisplayOn(xgifb_info, HwDeviceExtension,
5722                                                 pVBInfo);
5723                         }
5724                 }
5725         } /* 301 */
5726         else { /* LVDS */
5727                 if (pVBInfo->VBInfo & (SetCRT2ToTV | SetCRT2ToLCD
5728                                 | XGI_SetCRT2ToLCDA))
5729                         /* enable CRT2 */
5730                         xgifb_reg_or(pVBInfo->Part1Port, 0x1E, 0x20);
5731
5732                 tempah = (unsigned char) xgifb_reg_get(pVBInfo->Part1Port,
5733                                 0x2E);
5734                 if (!(tempah & 0x80))
5735                         xgifb_reg_or(pVBInfo->Part1Port, 0x2E, 0x80);
5736
5737                 xgifb_reg_and(pVBInfo->Part1Port, 0x00, 0x7F);
5738                 XGI_DisplayOn(xgifb_info, HwDeviceExtension, pVBInfo);
5739         } /* End of VB */
5740 }
5741
5742 static void XGI_SetCRT1Group(struct xgifb_video_info *xgifb_info,
5743                 struct xgi_hw_device_info *HwDeviceExtension,
5744                 unsigned short ModeNo, unsigned short ModeIdIndex,
5745                 struct vb_device_info *pVBInfo)
5746 {
5747         unsigned short RefreshRateTableIndex, temp;
5748
5749         XGI_SetSeqRegs(ModeNo, ModeIdIndex, pVBInfo);
5750         outb(XGI330_StandTable.MISC, pVBInfo->P3c2);
5751         XGI_SetCRTCRegs(HwDeviceExtension, pVBInfo);
5752         XGI_SetATTRegs(ModeNo, ModeIdIndex, pVBInfo);
5753         XGI_SetGRCRegs(pVBInfo);
5754         XGI_ClearExt1Regs(pVBInfo);
5755
5756         if (HwDeviceExtension->jChipType == XG27) {
5757                 if (pVBInfo->IF_DEF_LVDS == 0)
5758                         XGI_SetDefaultVCLK(pVBInfo);
5759         }
5760
5761         temp = ~ProgrammingCRT2;
5762         pVBInfo->SetFlag &= temp;
5763         pVBInfo->SelectCRT2Rate = 0;
5764
5765         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
5766                         | VB_SIS302LV | VB_XGI301C)) {
5767                 if (pVBInfo->VBInfo & (SetSimuScanMode | XGI_SetCRT2ToLCDA
5768                                 | SetInSlaveMode)) {
5769                         pVBInfo->SetFlag |= ProgrammingCRT2;
5770                 }
5771         }
5772
5773         RefreshRateTableIndex = XGI_GetRatePtrCRT2(HwDeviceExtension, ModeNo,
5774                         ModeIdIndex, pVBInfo);
5775         if (RefreshRateTableIndex != 0xFFFF) {
5776                 XGI_SetSync(RefreshRateTableIndex, pVBInfo);
5777                 XGI_SetCRT1CRTC(ModeNo, ModeIdIndex, RefreshRateTableIndex,
5778                                 pVBInfo, HwDeviceExtension);
5779                 XGI_SetCRT1DE(HwDeviceExtension, ModeNo, ModeIdIndex,
5780                                 RefreshRateTableIndex, pVBInfo);
5781                 XGI_SetCRT1Offset(ModeNo, ModeIdIndex, RefreshRateTableIndex,
5782                                 HwDeviceExtension, pVBInfo);
5783                 XGI_SetCRT1VCLK(ModeNo, ModeIdIndex, HwDeviceExtension,
5784                                 RefreshRateTableIndex, pVBInfo);
5785         }
5786
5787         if (HwDeviceExtension->jChipType >= XG21) {
5788                 temp = xgifb_reg_get(pVBInfo->P3d4, 0x38);
5789                 if (temp & 0xA0) {
5790
5791                         if (HwDeviceExtension->jChipType == XG27)
5792                                 XGI_SetXG27CRTC(ModeNo, ModeIdIndex,
5793                                                 RefreshRateTableIndex, pVBInfo);
5794                         else
5795                                 XGI_SetXG21CRTC(ModeNo, ModeIdIndex,
5796                                                 RefreshRateTableIndex, pVBInfo);
5797
5798                         XGI_UpdateXG21CRTC(ModeNo, pVBInfo,
5799                                         RefreshRateTableIndex);
5800
5801                         xgifb_set_lcd(HwDeviceExtension->jChipType,
5802                                         pVBInfo, RefreshRateTableIndex, ModeNo);
5803
5804                         if (pVBInfo->IF_DEF_LVDS == 1)
5805                                 xgifb_set_lvds(xgifb_info,
5806                                                 HwDeviceExtension->jChipType,
5807                                                 ModeNo, ModeIdIndex, pVBInfo);
5808                 }
5809         }
5810
5811         pVBInfo->SetFlag &= (~ProgrammingCRT2);
5812         XGI_SetCRT1FIFO(ModeNo, HwDeviceExtension, pVBInfo);
5813         XGI_SetCRT1ModeRegs(HwDeviceExtension, ModeNo, ModeIdIndex,
5814                         RefreshRateTableIndex, pVBInfo);
5815         XGI_LoadDAC(ModeNo, ModeIdIndex, pVBInfo);
5816 }
5817
5818 unsigned char XGISetModeNew(struct xgifb_video_info *xgifb_info,
5819                         struct xgi_hw_device_info *HwDeviceExtension,
5820                         unsigned short ModeNo)
5821 {
5822         unsigned short ModeIdIndex;
5823         struct vb_device_info VBINF;
5824         struct vb_device_info *pVBInfo = &VBINF;
5825         pVBInfo->IF_DEF_LVDS = 0;
5826
5827         if (HwDeviceExtension->jChipType >= XG20) {
5828                 pVBInfo->IF_DEF_YPbPr = 0;
5829                 pVBInfo->IF_DEF_HiVision = 0;
5830                 pVBInfo->IF_DEF_CRT2Monitor = 0;
5831                 pVBInfo->VBType = 0; /*set VBType default 0*/
5832         } else {
5833                 pVBInfo->IF_DEF_YPbPr = 1;
5834                 pVBInfo->IF_DEF_HiVision = 1;
5835                 pVBInfo->IF_DEF_CRT2Monitor = 1;
5836         }
5837
5838         XGIRegInit(pVBInfo, xgifb_info->vga_base);
5839
5840         /* for x86 Linux, XG21 LVDS */
5841         if (HwDeviceExtension->jChipType == XG21) {
5842                 if ((xgifb_reg_get(pVBInfo->P3d4, 0x38) & 0xE0) == 0xC0)
5843                         pVBInfo->IF_DEF_LVDS = 1;
5844         }
5845         if (HwDeviceExtension->jChipType == XG27) {
5846                 if ((xgifb_reg_get(pVBInfo->P3d4, 0x38) & 0xE0) == 0xC0) {
5847                         if (xgifb_reg_get(pVBInfo->P3d4, 0x30) & 0x20)
5848                                 pVBInfo->IF_DEF_LVDS = 1;
5849                 }
5850         }
5851
5852         if (HwDeviceExtension->jChipType < XG20)
5853                 XGI_GetVBType(pVBInfo);
5854
5855         InitTo330Pointer(HwDeviceExtension->jChipType, pVBInfo);
5856         if (ModeNo & 0x80)
5857                 ModeNo = ModeNo & 0x7F;
5858         xgifb_reg_set(pVBInfo->P3c4, 0x05, 0x86);
5859
5860         if (HwDeviceExtension->jChipType < XG20)
5861                 XGI_UnLockCRT2(HwDeviceExtension, pVBInfo);
5862
5863         XGI_SearchModeID(ModeNo, &ModeIdIndex, pVBInfo);
5864
5865         if (HwDeviceExtension->jChipType < XG20) {
5866                 XGI_GetVBInfo(ModeNo, ModeIdIndex, HwDeviceExtension, pVBInfo);
5867                 XGI_GetTVInfo(ModeNo, ModeIdIndex, pVBInfo);
5868                 XGI_GetLCDInfo(ModeNo, ModeIdIndex, pVBInfo);
5869                 XGI_DisableBridge(xgifb_info, HwDeviceExtension, pVBInfo);
5870
5871                 if (pVBInfo->VBInfo & (SetSimuScanMode | XGI_SetCRT2ToLCDA) ||
5872                         (!(pVBInfo->VBInfo & SwitchCRT2))) {
5873                         XGI_SetCRT1Group(xgifb_info, HwDeviceExtension, ModeNo,
5874                                         ModeIdIndex, pVBInfo);
5875
5876                         if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA) {
5877                                 XGI_SetLCDAGroup(ModeNo, ModeIdIndex,
5878                                                 HwDeviceExtension, pVBInfo);
5879                         }
5880                 }
5881
5882                 if (pVBInfo->VBInfo & (SetSimuScanMode | SwitchCRT2)) {
5883                         switch (HwDeviceExtension->ujVBChipID) {
5884                         case VB_CHIP_301: /* fall through */
5885                         case VB_CHIP_302:
5886                                 XGI_SetCRT2Group301(ModeNo, HwDeviceExtension,
5887                                                 pVBInfo); /*add for CRT2 */
5888                                 break;
5889
5890                         default:
5891                                 break;
5892                         }
5893                 }
5894
5895                 XGI_SetCRT2ModeRegs(ModeNo, HwDeviceExtension, pVBInfo);
5896                 XGI_OEM310Setting(ModeNo, ModeIdIndex, pVBInfo); /*0212*/
5897                 XGI_EnableBridge(xgifb_info, HwDeviceExtension, pVBInfo);
5898         } /* !XG20 */
5899         else {
5900                 if (pVBInfo->IF_DEF_LVDS == 1)
5901                         if (!XGI_XG21CheckLVDSMode(xgifb_info, ModeNo,
5902                                                    ModeIdIndex,
5903                                                    pVBInfo))
5904                                 return 0;
5905
5906                 pVBInfo->ModeType = XGI330_EModeIDTable[ModeIdIndex].
5907                                                 Ext_ModeFlag & ModeTypeMask;
5908
5909                 pVBInfo->SetFlag = 0;
5910                 pVBInfo->VBInfo = DisableCRT2Display;
5911
5912                 XGI_DisplayOff(xgifb_info, HwDeviceExtension, pVBInfo);
5913
5914                 XGI_SetCRT1Group(xgifb_info, HwDeviceExtension, ModeNo,
5915                                 ModeIdIndex, pVBInfo);
5916
5917                 XGI_DisplayOn(xgifb_info, HwDeviceExtension, pVBInfo);
5918         }
5919
5920         XGI_UpdateModeInfo(HwDeviceExtension, pVBInfo);
5921
5922         if (HwDeviceExtension->jChipType < XG20)
5923                 XGI_LockCRT2(HwDeviceExtension, pVBInfo);
5924
5925         return 1;
5926 }