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