]> rtime.felk.cvut.cz Git - linux-imx.git/blob - drivers/staging/xgifb/vb_setmode.c
73ee8ecc33781cb004d282bb7b78fd5c9ec48580
[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 static void XGI_DisableBridge(struct xgifb_video_info *xgifb_info,
4545                 struct xgi_hw_device_info *HwDeviceExtension,
4546                 struct vb_device_info *pVBInfo)
4547 {
4548         unsigned short tempah = 0;
4549
4550         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
4551                         | VB_SIS302LV | VB_XGI301C)) {
4552                 tempah = 0x3F;
4553                 if (!(pVBInfo->VBInfo &
4554                     (DisableCRT2Display | SetSimuScanMode))) {
4555                         if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA) {
4556                                 if (pVBInfo->VBInfo & SetCRT2ToDualEdge) {
4557                                         tempah = 0x7F; /* Disable Channel A */
4558                                         if (!(pVBInfo->VBInfo &
4559                                               XGI_SetCRT2ToLCDA))
4560                                                 /* Disable Channel B */
4561                                                 tempah = 0xBF;
4562                                 }
4563                         }
4564                 }
4565
4566                 /* disable part4_1f */
4567                 xgifb_reg_and(pVBInfo->Part4Port, 0x1F, tempah);
4568
4569                 if (pVBInfo->VBType & (VB_SIS302LV | VB_XGI301C)) {
4570                         if (((pVBInfo->VBInfo &
4571                               (SetCRT2ToLCD | XGI_SetCRT2ToLCDA))) ||
4572                                 (XGI_IsLCDON(pVBInfo)))
4573                                 /* LVDS Driver power down */
4574                                 xgifb_reg_or(pVBInfo->Part4Port, 0x30, 0x80);
4575                 }
4576
4577                 if (pVBInfo->VBInfo & (DisableCRT2Display | XGI_SetCRT2ToLCDA |
4578                                        SetSimuScanMode))
4579                         XGI_DisplayOff(xgifb_info, HwDeviceExtension, pVBInfo);
4580
4581                 if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA) {
4582                         if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA)
4583                                 /* Power down */
4584                                 xgifb_reg_and(pVBInfo->Part1Port, 0x1e, 0xdf);
4585                 }
4586
4587                 /* disable TV as primary VGA swap */
4588                 xgifb_reg_and(pVBInfo->P3c4, 0x32, 0xdf);
4589
4590                 if ((pVBInfo->VBInfo & (SetSimuScanMode | SetCRT2ToDualEdge)))
4591                         xgifb_reg_and(pVBInfo->Part2Port, 0x00, 0xdf);
4592
4593                 if ((pVBInfo->VBInfo &
4594                         (DisableCRT2Display | SetSimuScanMode)) ||
4595                     ((!(pVBInfo->VBInfo & XGI_SetCRT2ToLCDA)) &&
4596                     (pVBInfo->VBInfo &
4597                         (SetCRT2ToRAMDAC | SetCRT2ToLCD | SetCRT2ToTV))))
4598                         xgifb_reg_or(pVBInfo->Part1Port, 0x00, 0x80);
4599
4600                 if ((pVBInfo->VBInfo &
4601                         (DisableCRT2Display | SetSimuScanMode)) ||
4602                     (!(pVBInfo->VBInfo & XGI_SetCRT2ToLCDA)) ||
4603                     (pVBInfo->VBInfo &
4604                         (SetCRT2ToRAMDAC | SetCRT2ToLCD | SetCRT2ToTV))) {
4605                         /* save Part1 index 0 */
4606                         tempah = xgifb_reg_get(pVBInfo->Part1Port, 0x00);
4607                         /* BTDAC = 1, avoid VB reset */
4608                         xgifb_reg_or(pVBInfo->Part1Port, 0x00, 0x10);
4609                         /* disable CRT2 */
4610                         xgifb_reg_and(pVBInfo->Part1Port, 0x1E, 0xDF);
4611                         /* restore Part1 index 0 */
4612                         xgifb_reg_set(pVBInfo->Part1Port, 0x00, tempah);
4613                 }
4614         } else { /* {301} */
4615                 if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToTV)) {
4616                         xgifb_reg_or(pVBInfo->Part1Port, 0x00, 0x80);
4617                         /* Disable CRT2 */
4618                         xgifb_reg_and(pVBInfo->Part1Port, 0x1E, 0xDF);
4619                         /* Disable TV asPrimary VGA swap */
4620                         xgifb_reg_and(pVBInfo->P3c4, 0x32, 0xDF);
4621                 }
4622
4623                 if (pVBInfo->VBInfo & (DisableCRT2Display | XGI_SetCRT2ToLCDA
4624                                 | SetSimuScanMode))
4625                         XGI_DisplayOff(xgifb_info, HwDeviceExtension, pVBInfo);
4626         }
4627 }
4628
4629 /* --------------------------------------------------------------------- */
4630 /* Function : XGI_GetTVPtrIndex */
4631 /* Input : */
4632 /* Output : */
4633 /* Description : bx 0 : ExtNTSC */
4634 /* 1 : StNTSC */
4635 /* 2 : ExtPAL */
4636 /* 3 : StPAL */
4637 /* 4 : ExtHiTV */
4638 /* 5 : StHiTV */
4639 /* 6 : Ext525i */
4640 /* 7 : St525i */
4641 /* 8 : Ext525p */
4642 /* 9 : St525p */
4643 /* A : Ext750p */
4644 /* B : St750p */
4645 /* --------------------------------------------------------------------- */
4646 static unsigned short XGI_GetTVPtrIndex(struct vb_device_info *pVBInfo)
4647 {
4648         unsigned short tempbx = 0;
4649
4650         if (pVBInfo->TVInfo & TVSetPAL)
4651                 tempbx = 2;
4652         if (pVBInfo->TVInfo & TVSetHiVision)
4653                 tempbx = 4;
4654         if (pVBInfo->TVInfo & TVSetYPbPr525i)
4655                 tempbx = 6;
4656         if (pVBInfo->TVInfo & TVSetYPbPr525p)
4657                 tempbx = 8;
4658         if (pVBInfo->TVInfo & TVSetYPbPr750p)
4659                 tempbx = 10;
4660         if (pVBInfo->TVInfo & TVSimuMode)
4661                 tempbx++;
4662
4663         return tempbx;
4664 }
4665
4666 /* --------------------------------------------------------------------- */
4667 /* Function : XGI_GetTVPtrIndex2 */
4668 /* Input : */
4669 /* Output : bx 0 : NTSC */
4670 /* 1 : PAL */
4671 /* 2 : PALM */
4672 /* 3 : PALN */
4673 /* 4 : NTSC1024x768 */
4674 /* 5 : PAL-M 1024x768 */
4675 /* 6-7: reserved */
4676 /* cl 0 : YFilter1 */
4677 /* 1 : YFilter2 */
4678 /* ch 0 : 301A */
4679 /* 1 : 301B/302B/301LV/302LV */
4680 /* Description : */
4681 /* --------------------------------------------------------------------- */
4682 static void XGI_GetTVPtrIndex2(unsigned short *tempbx, unsigned char *tempcl,
4683                 unsigned char *tempch, struct vb_device_info *pVBInfo)
4684 {
4685         *tempbx = 0;
4686         *tempcl = 0;
4687         *tempch = 0;
4688
4689         if (pVBInfo->TVInfo & TVSetPAL)
4690                 *tempbx = 1;
4691
4692         if (pVBInfo->TVInfo & TVSetPALM)
4693                 *tempbx = 2;
4694
4695         if (pVBInfo->TVInfo & TVSetPALN)
4696                 *tempbx = 3;
4697
4698         if (pVBInfo->TVInfo & NTSC1024x768) {
4699                 *tempbx = 4;
4700                 if (pVBInfo->TVInfo & TVSetPALM)
4701                         *tempbx = 5;
4702         }
4703
4704         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
4705                         | VB_SIS302LV | VB_XGI301C)) {
4706                 if ((!(pVBInfo->VBInfo & SetInSlaveMode)) || (pVBInfo->TVInfo
4707                                 & TVSimuMode)) {
4708                         *tempbx += 8;
4709                         *tempcl += 1;
4710                 }
4711         }
4712
4713         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
4714                         | VB_SIS302LV | VB_XGI301C))
4715                 (*tempch)++;
4716 }
4717
4718 static void XGI_SetDelayComp(struct vb_device_info *pVBInfo)
4719 {
4720         unsigned char tempah, tempbl, tempbh;
4721
4722         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
4723                         | VB_SIS302LV | VB_XGI301C)) {
4724                 if (pVBInfo->VBInfo & (SetCRT2ToLCD | XGI_SetCRT2ToLCDA
4725                                 | SetCRT2ToTV | SetCRT2ToRAMDAC)) {
4726                         tempbh = 0;
4727                         tempbl = XGI301TVDelay;
4728
4729                         if (pVBInfo->VBInfo & SetCRT2ToDualEdge)
4730                                 tempbl = tempbl >> 4;
4731                         if (pVBInfo->VBInfo &
4732                             (SetCRT2ToLCD | XGI_SetCRT2ToLCDA)) {
4733                                 tempbh = XGI301LCDDelay;
4734
4735                                 if (!(pVBInfo->VBInfo & XGI_SetCRT2ToLCDA))
4736                                         tempbl = tempbh;
4737                         }
4738
4739                         tempbl &= 0x0F;
4740                         tempbh &= 0xF0;
4741                         tempah = xgifb_reg_get(pVBInfo->Part1Port, 0x2D);
4742
4743                         if (pVBInfo->VBInfo & (SetCRT2ToRAMDAC | SetCRT2ToLCD
4744                                         | SetCRT2ToTV)) { /* Channel B */
4745                                 tempah &= 0xF0;
4746                                 tempah |= tempbl;
4747                         }
4748
4749                         if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA) {
4750                                 /* Channel A */
4751                                 tempah &= 0x0F;
4752                                 tempah |= tempbh;
4753                         }
4754                         xgifb_reg_set(pVBInfo->Part1Port, 0x2D, tempah);
4755                 }
4756         }
4757 }
4758
4759 static void XGI_SetLCDCap_A(unsigned short tempcx,
4760                             struct vb_device_info *pVBInfo)
4761 {
4762         unsigned short temp;
4763
4764         temp = xgifb_reg_get(pVBInfo->P3d4, 0x37);
4765
4766         if (temp & LCDRGB18Bit) {
4767                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x19, 0x0F,
4768                                 /* Enable Dither */
4769                                 (unsigned short) (0x20 | (tempcx & 0x00C0)));
4770                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x1A, 0x7F, 0x80);
4771         } else {
4772                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x19, 0x0F,
4773                                 (unsigned short) (0x30 | (tempcx & 0x00C0)));
4774                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x1A, 0x7F, 0x00);
4775         }
4776 }
4777
4778 /* --------------------------------------------------------------------- */
4779 /* Function : XGI_SetLCDCap_B */
4780 /* Input : cx -> LCD Capability */
4781 /* Output : */
4782 /* Description : */
4783 /* --------------------------------------------------------------------- */
4784 static void XGI_SetLCDCap_B(unsigned short tempcx,
4785                             struct vb_device_info *pVBInfo)
4786 {
4787         if (tempcx & EnableLCD24bpp) /* 24bits */
4788                 xgifb_reg_and_or(pVBInfo->Part2Port, 0x1A, 0xE0,
4789                                 (unsigned short) (((tempcx & 0x00ff) >> 6)
4790                                                 | 0x0c));
4791         else
4792                 xgifb_reg_and_or(pVBInfo->Part2Port, 0x1A, 0xE0,
4793                                 (unsigned short) (((tempcx & 0x00ff) >> 6)
4794                                                 | 0x18)); /* Enable Dither */
4795 }
4796
4797 static void XGI_LongWait(struct vb_device_info *pVBInfo)
4798 {
4799         unsigned short i;
4800
4801         i = xgifb_reg_get(pVBInfo->P3c4, 0x1F);
4802
4803         if (!(i & 0xC0)) {
4804                 for (i = 0; i < 0xFFFF; i++) {
4805                         if (!(inb(pVBInfo->P3da) & 0x08))
4806                                 break;
4807                 }
4808
4809                 for (i = 0; i < 0xFFFF; i++) {
4810                         if ((inb(pVBInfo->P3da) & 0x08))
4811                                 break;
4812                 }
4813         }
4814 }
4815
4816 static void SetSpectrum(struct vb_device_info *pVBInfo)
4817 {
4818         unsigned short index;
4819
4820         index = XGI_GetLCDCapPtr(pVBInfo);
4821
4822         /* disable down spectrum D[4] */
4823         xgifb_reg_and(pVBInfo->Part4Port, 0x30, 0x8F);
4824         XGI_LongWait(pVBInfo);
4825         xgifb_reg_or(pVBInfo->Part4Port, 0x30, 0x20); /* reset spectrum */
4826         XGI_LongWait(pVBInfo);
4827
4828         xgifb_reg_set(pVBInfo->Part4Port, 0x31,
4829                         pVBInfo->LCDCapList[index].Spectrum_31);
4830         xgifb_reg_set(pVBInfo->Part4Port, 0x32,
4831                         pVBInfo->LCDCapList[index].Spectrum_32);
4832         xgifb_reg_set(pVBInfo->Part4Port, 0x33,
4833                         pVBInfo->LCDCapList[index].Spectrum_33);
4834         xgifb_reg_set(pVBInfo->Part4Port, 0x34,
4835                         pVBInfo->LCDCapList[index].Spectrum_34);
4836         XGI_LongWait(pVBInfo);
4837         xgifb_reg_or(pVBInfo->Part4Port, 0x30, 0x40); /* enable spectrum */
4838 }
4839
4840 static void XGI_SetLCDCap(struct vb_device_info *pVBInfo)
4841 {
4842         unsigned short tempcx;
4843
4844         tempcx = pVBInfo->LCDCapList[XGI_GetLCDCapPtr(pVBInfo)].LCD_Capability;
4845
4846         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV |
4847                 VB_SIS302LV | VB_XGI301C)) {
4848                 if (pVBInfo->VBType &
4849                     (VB_SIS301LV | VB_SIS302LV | VB_XGI301C)) {
4850                         /* Set 301LV Capability */
4851                         xgifb_reg_set(pVBInfo->Part4Port, 0x24,
4852                                         (unsigned char) (tempcx & 0x1F));
4853                 }
4854                 /* VB Driving */
4855                 xgifb_reg_and_or(pVBInfo->Part4Port, 0x0D,
4856                                 ~((EnableVBCLKDRVLOW | EnablePLLSPLOW) >> 8),
4857                                 (unsigned short) ((tempcx & (EnableVBCLKDRVLOW
4858                                                 | EnablePLLSPLOW)) >> 8));
4859
4860                 if (pVBInfo->VBInfo & SetCRT2ToLCD)
4861                         XGI_SetLCDCap_B(tempcx, pVBInfo);
4862                 else if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA)
4863                         XGI_SetLCDCap_A(tempcx, pVBInfo);
4864
4865                 if (pVBInfo->VBType & (VB_SIS302LV | VB_XGI301C)) {
4866                         if (tempcx & EnableSpectrum)
4867                                 SetSpectrum(pVBInfo);
4868                 }
4869         } else {
4870                 /* LVDS,CH7017 */
4871                 XGI_SetLCDCap_A(tempcx, pVBInfo);
4872         }
4873 }
4874
4875 /* --------------------------------------------------------------------- */
4876 /* Function : XGI_SetAntiFlicker */
4877 /* Input : */
4878 /* Output : */
4879 /* Description : Set TV Customized Param. */
4880 /* --------------------------------------------------------------------- */
4881 static void XGI_SetAntiFlicker(unsigned short ModeNo,
4882                                unsigned short ModeIdIndex,
4883                                struct vb_device_info *pVBInfo)
4884 {
4885         unsigned short tempbx;
4886
4887         unsigned char tempah;
4888
4889         if (pVBInfo->TVInfo & (TVSetYPbPr525p | TVSetYPbPr750p))
4890                 return;
4891
4892         tempbx = XGI_GetTVPtrIndex(pVBInfo);
4893         tempbx &= 0xFE;
4894         tempah = TVAntiFlickList[tempbx];
4895         tempah = tempah << 4;
4896
4897         xgifb_reg_and_or(pVBInfo->Part2Port, 0x0A, 0x8F, tempah);
4898 }
4899
4900 static void XGI_SetEdgeEnhance(unsigned short ModeNo,
4901                                unsigned short ModeIdIndex,
4902                                struct vb_device_info *pVBInfo)
4903 {
4904         unsigned short tempbx;
4905
4906         unsigned char tempah;
4907
4908         tempbx = XGI_GetTVPtrIndex(pVBInfo);
4909         tempbx &= 0xFE;
4910         tempah = TVEdgeList[tempbx];
4911         tempah = tempah << 5;
4912
4913         xgifb_reg_and_or(pVBInfo->Part2Port, 0x3A, 0x1F, tempah);
4914 }
4915
4916 static void XGI_SetPhaseIncr(struct vb_device_info *pVBInfo)
4917 {
4918         unsigned short tempbx;
4919
4920         unsigned char tempcl, tempch;
4921
4922         unsigned long tempData;
4923
4924         XGI_GetTVPtrIndex2(&tempbx, &tempcl, &tempch, pVBInfo); /* bx, cl, ch */
4925         tempData = TVPhaseList[tempbx];
4926
4927         xgifb_reg_set(pVBInfo->Part2Port, 0x31, (unsigned short) (tempData
4928                         & 0x000000FF));
4929         xgifb_reg_set(pVBInfo->Part2Port, 0x32, (unsigned short) ((tempData
4930                         & 0x0000FF00) >> 8));
4931         xgifb_reg_set(pVBInfo->Part2Port, 0x33, (unsigned short) ((tempData
4932                         & 0x00FF0000) >> 16));
4933         xgifb_reg_set(pVBInfo->Part2Port, 0x34, (unsigned short) ((tempData
4934                         & 0xFF000000) >> 24));
4935 }
4936
4937 static void XGI_SetYFilter(unsigned short ModeNo, unsigned short ModeIdIndex,
4938                 struct vb_device_info *pVBInfo)
4939 {
4940         unsigned short tempbx, index;
4941         unsigned char const *filterPtr;
4942         unsigned char tempcl, tempch, tempal;
4943
4944         XGI_GetTVPtrIndex2(&tempbx, &tempcl, &tempch, pVBInfo); /* bx, cl, ch */
4945
4946         switch (tempbx) {
4947         case 0x00:
4948         case 0x04:
4949                 filterPtr = NTSCYFilter1;
4950                 break;
4951
4952         case 0x01:
4953                 filterPtr = PALYFilter1;
4954                 break;
4955
4956         case 0x02:
4957         case 0x05:
4958         case 0x0D:
4959         case 0x03:
4960                 filterPtr = xgifb_palmn_yfilter1;
4961                 break;
4962
4963         case 0x08:
4964         case 0x0C:
4965         case 0x0A:
4966         case 0x0B:
4967         case 0x09:
4968                 filterPtr = xgifb_yfilter2;
4969                 break;
4970
4971         default:
4972                 return;
4973         }
4974
4975         tempal = XGI330_EModeIDTable[ModeIdIndex].VB_ExtTVYFilterIndex;
4976         if (tempcl == 0)
4977                 index = tempal * 4;
4978         else
4979                 index = tempal * 7;
4980
4981         if ((tempcl == 0) && (tempch == 1)) {
4982                 xgifb_reg_set(pVBInfo->Part2Port, 0x35, 0);
4983                 xgifb_reg_set(pVBInfo->Part2Port, 0x36, 0);
4984                 xgifb_reg_set(pVBInfo->Part2Port, 0x37, 0);
4985                 xgifb_reg_set(pVBInfo->Part2Port, 0x38, filterPtr[index++]);
4986         } else {
4987                 xgifb_reg_set(pVBInfo->Part2Port, 0x35, filterPtr[index++]);
4988                 xgifb_reg_set(pVBInfo->Part2Port, 0x36, filterPtr[index++]);
4989                 xgifb_reg_set(pVBInfo->Part2Port, 0x37, filterPtr[index++]);
4990                 xgifb_reg_set(pVBInfo->Part2Port, 0x38, filterPtr[index++]);
4991         }
4992
4993         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
4994                         | VB_SIS302LV | VB_XGI301C)) {
4995                 xgifb_reg_set(pVBInfo->Part2Port, 0x48, filterPtr[index++]);
4996                 xgifb_reg_set(pVBInfo->Part2Port, 0x49, filterPtr[index++]);
4997                 xgifb_reg_set(pVBInfo->Part2Port, 0x4A, filterPtr[index++]);
4998         }
4999 }
5000
5001 /* --------------------------------------------------------------------- */
5002 /* Function : XGI_OEM310Setting */
5003 /* Input : */
5004 /* Output : */
5005 /* Description : Customized Param. for 301 */
5006 /* --------------------------------------------------------------------- */
5007 static void XGI_OEM310Setting(unsigned short ModeNo,
5008                               unsigned short ModeIdIndex,
5009                               struct vb_device_info *pVBInfo)
5010 {
5011         XGI_SetDelayComp(pVBInfo);
5012
5013         if (pVBInfo->VBInfo & (SetCRT2ToLCD | XGI_SetCRT2ToLCDA))
5014                 XGI_SetLCDCap(pVBInfo);
5015
5016         if (pVBInfo->VBInfo & SetCRT2ToTV) {
5017                 XGI_SetPhaseIncr(pVBInfo);
5018                 XGI_SetYFilter(ModeNo, ModeIdIndex, pVBInfo);
5019                 XGI_SetAntiFlicker(ModeNo, ModeIdIndex, pVBInfo);
5020
5021                 if (pVBInfo->VBType & VB_SIS301)
5022                         XGI_SetEdgeEnhance(ModeNo, ModeIdIndex, pVBInfo);
5023         }
5024 }
5025
5026 /* --------------------------------------------------------------------- */
5027 /* Function : XGI_SetCRT2ModeRegs */
5028 /* Input : */
5029 /* Output : */
5030 /* Description : Origin code for crt2group */
5031 /* --------------------------------------------------------------------- */
5032 static void XGI_SetCRT2ModeRegs(unsigned short ModeNo,
5033                 struct xgi_hw_device_info *HwDeviceExtension,
5034                 struct vb_device_info *pVBInfo)
5035 {
5036         unsigned short tempbl;
5037         short tempcl;
5038
5039         unsigned char tempah;
5040
5041         tempah = 0;
5042         if (!(pVBInfo->VBInfo & DisableCRT2Display)) {
5043                 tempah = xgifb_reg_get(pVBInfo->Part1Port, 0x00);
5044                 tempah &= ~0x10; /* BTRAMDAC */
5045                 tempah |= 0x40; /* BTRAM */
5046
5047                 if (pVBInfo->VBInfo & (SetCRT2ToRAMDAC | SetCRT2ToTV
5048                                 | SetCRT2ToLCD)) {
5049                         tempah = 0x40; /* BTDRAM */
5050                         tempcl = pVBInfo->ModeType;
5051                         tempcl -= ModeVGA;
5052                         if (tempcl >= 0) {
5053                                 /* BT Color */
5054                                 tempah = (0x008 >> tempcl);
5055                                 if (tempah == 0)
5056                                         tempah = 1;
5057                                 tempah |= 0x040;
5058                         }
5059                         if (pVBInfo->VBInfo & SetInSlaveMode)
5060                                 tempah ^= 0x50; /* BTDAC */
5061                 }
5062         }
5063
5064         xgifb_reg_set(pVBInfo->Part1Port, 0x00, tempah);
5065         tempah = 0x08;
5066         tempbl = 0xf0;
5067
5068         if (pVBInfo->VBInfo & DisableCRT2Display)
5069                 goto reg_and_or;
5070
5071         tempah = 0x00;
5072         tempbl = 0xff;
5073
5074         if (!(pVBInfo->VBInfo & (SetCRT2ToRAMDAC | SetCRT2ToTV |
5075                                  SetCRT2ToLCD | XGI_SetCRT2ToLCDA)))
5076                 goto reg_and_or;
5077
5078         if ((pVBInfo->VBInfo & XGI_SetCRT2ToLCDA) &&
5079             (!(pVBInfo->VBInfo & SetSimuScanMode))) {
5080                 tempbl &= 0xf7;
5081                 tempah |= 0x01;
5082                 goto reg_and_or;
5083         }
5084
5085         if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA) {
5086                 tempbl &= 0xf7;
5087                 tempah |= 0x01;
5088         }
5089
5090         if (!(pVBInfo->VBInfo & (SetCRT2ToRAMDAC | SetCRT2ToTV | SetCRT2ToLCD)))
5091                 goto reg_and_or;
5092
5093         tempbl &= 0xf8;
5094         tempah = 0x01;
5095
5096         if (!(pVBInfo->VBInfo & SetInSlaveMode))
5097                 tempah |= 0x02;
5098
5099         if (!(pVBInfo->VBInfo & SetCRT2ToRAMDAC)) {
5100                 tempah = tempah ^ 0x05;
5101                 if (!(pVBInfo->VBInfo & SetCRT2ToLCD))
5102                         tempah = tempah ^ 0x01;
5103         }
5104
5105         if (!(pVBInfo->VBInfo & SetCRT2ToDualEdge))
5106                 tempah |= 0x08;
5107
5108 reg_and_or:
5109         xgifb_reg_and_or(pVBInfo->Part1Port, 0x2e, tempbl, tempah);
5110
5111         if (pVBInfo->VBInfo & (SetCRT2ToRAMDAC | SetCRT2ToTV | SetCRT2ToLCD
5112                         | XGI_SetCRT2ToLCDA)) {
5113                 tempah &= (~0x08);
5114                 if ((pVBInfo->ModeType == ModeVGA) && (!(pVBInfo->VBInfo
5115                                 & SetInSlaveMode))) {
5116                         tempah |= 0x010;
5117                 }
5118                 tempah |= 0x080;
5119
5120                 if (pVBInfo->VBInfo & SetCRT2ToTV) {
5121                         tempah |= 0x020;
5122                         if (pVBInfo->VBInfo & DriverMode)
5123                                 tempah = tempah ^ 0x20;
5124                 }
5125
5126                 xgifb_reg_and_or(pVBInfo->Part4Port, 0x0D, ~0x0BF, tempah);
5127                 tempah = 0;
5128
5129                 if (pVBInfo->LCDInfo & SetLCDDualLink)
5130                         tempah |= 0x40;
5131
5132                 if (pVBInfo->VBInfo & SetCRT2ToTV) {
5133                         if (pVBInfo->TVInfo & RPLLDIV2XO)
5134                                 tempah |= 0x40;
5135                 }
5136
5137                 if ((pVBInfo->LCDResInfo == Panel_1280x1024)
5138                                 || (pVBInfo->LCDResInfo == Panel_1280x1024x75))
5139                         tempah |= 0x80;
5140
5141                 if (pVBInfo->LCDResInfo == Panel_1280x960)
5142                         tempah |= 0x80;
5143
5144                 xgifb_reg_set(pVBInfo->Part4Port, 0x0C, tempah);
5145         }
5146
5147         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
5148                         | VB_SIS302LV | VB_XGI301C)) {
5149                 tempah = 0;
5150                 tempbl = 0xfb;
5151
5152                 if (pVBInfo->VBInfo & SetCRT2ToDualEdge) {
5153                         tempbl = 0xff;
5154                         if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA)
5155                                 tempah |= 0x04; /* shampoo 0129 */
5156                 }
5157
5158                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x13, tempbl, tempah);
5159                 tempah = 0x00;
5160                 tempbl = 0xcf;
5161                 if (!(pVBInfo->VBInfo & DisableCRT2Display)) {
5162                         if (pVBInfo->VBInfo & SetCRT2ToDualEdge)
5163                                 tempah |= 0x30;
5164                 }
5165
5166                 xgifb_reg_and_or(pVBInfo->Part1Port, 0x2c, tempbl, tempah);
5167                 tempah = 0;
5168                 tempbl = 0x3f;
5169
5170                 if (!(pVBInfo->VBInfo & DisableCRT2Display)) {
5171                         if (pVBInfo->VBInfo & SetCRT2ToDualEdge)
5172                                 tempah |= 0xc0;
5173                 }
5174                 xgifb_reg_and_or(pVBInfo->Part4Port, 0x21, tempbl, tempah);
5175         }
5176
5177         tempah = 0;
5178         tempbl = 0x7f;
5179         if (!(pVBInfo->VBInfo & XGI_SetCRT2ToLCDA)) {
5180                 tempbl = 0xff;
5181                 if (!(pVBInfo->VBInfo & SetCRT2ToDualEdge))
5182                         tempah |= 0x80;
5183         }
5184
5185         xgifb_reg_and_or(pVBInfo->Part4Port, 0x23, tempbl, tempah);
5186
5187         if (pVBInfo->VBType & (VB_SIS302LV | VB_XGI301C)) {
5188                 if (pVBInfo->LCDInfo & SetLCDDualLink) {
5189                         xgifb_reg_or(pVBInfo->Part4Port, 0x27, 0x20);
5190                         xgifb_reg_or(pVBInfo->Part4Port, 0x34, 0x10);
5191                 }
5192         }
5193 }
5194
5195
5196 void XGI_UnLockCRT2(struct xgi_hw_device_info *HwDeviceExtension,
5197                 struct vb_device_info *pVBInfo)
5198 {
5199
5200         xgifb_reg_and_or(pVBInfo->Part1Port, 0x2f, 0xFF, 0x01);
5201
5202 }
5203
5204 void XGI_LockCRT2(struct xgi_hw_device_info *HwDeviceExtension,
5205                 struct vb_device_info *pVBInfo)
5206 {
5207
5208         xgifb_reg_and_or(pVBInfo->Part1Port, 0x2F, 0xFE, 0x00);
5209
5210 }
5211
5212 unsigned short XGI_GetRatePtrCRT2(struct xgi_hw_device_info *pXGIHWDE,
5213                 unsigned short ModeNo, unsigned short ModeIdIndex,
5214                 struct vb_device_info *pVBInfo)
5215 {
5216         const u8 LCDARefreshIndex[] = {
5217                 0x00, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x00 };
5218
5219         unsigned short RefreshRateTableIndex, i, index, temp;
5220
5221         index = xgifb_reg_get(pVBInfo->P3d4, 0x33);
5222         index = index >> pVBInfo->SelectCRT2Rate;
5223         index &= 0x0F;
5224
5225         if (pVBInfo->LCDInfo & LCDNonExpanding)
5226                 index = 0;
5227
5228         if (index > 0)
5229                 index--;
5230
5231         if (pVBInfo->SetFlag & ProgrammingCRT2) {
5232                 if (pVBInfo->VBInfo & (SetCRT2ToLCD | XGI_SetCRT2ToLCDA)) {
5233                         temp = LCDARefreshIndex[pVBInfo->LCDResInfo & 0x07];
5234
5235                         if (index > temp)
5236                                 index = temp;
5237                 }
5238         }
5239
5240         RefreshRateTableIndex = XGI330_EModeIDTable[ModeIdIndex].REFindex;
5241         ModeNo = XGI330_RefIndex[RefreshRateTableIndex].ModeID;
5242         if (pXGIHWDE->jChipType >= XG20) { /* for XG20, XG21, XG27 */
5243                 if ((XGI330_RefIndex[RefreshRateTableIndex].XRes == 800) &&
5244                     (XGI330_RefIndex[RefreshRateTableIndex].YRes == 600)) {
5245                         index++;
5246                 }
5247                 /* do the similar adjustment like XGISearchCRT1Rate() */
5248                 if ((XGI330_RefIndex[RefreshRateTableIndex].XRes == 1024) &&
5249                     (XGI330_RefIndex[RefreshRateTableIndex].YRes == 768)) {
5250                         index++;
5251                 }
5252                 if ((XGI330_RefIndex[RefreshRateTableIndex].XRes == 1280) &&
5253                     (XGI330_RefIndex[RefreshRateTableIndex].YRes == 1024)) {
5254                         index++;
5255                 }
5256         }
5257
5258         i = 0;
5259         do {
5260                 if (XGI330_RefIndex[RefreshRateTableIndex + i].
5261                         ModeID != ModeNo)
5262                         break;
5263                 temp = XGI330_RefIndex[RefreshRateTableIndex + i].Ext_InfoFlag;
5264                 temp &= ModeTypeMask;
5265                 if (temp < pVBInfo->ModeType)
5266                         break;
5267                 i++;
5268                 index--;
5269
5270         } while (index != 0xFFFF);
5271         if (!(pVBInfo->VBInfo & SetCRT2ToRAMDAC)) {
5272                 if (pVBInfo->VBInfo & SetInSlaveMode) {
5273                         temp = XGI330_RefIndex[RefreshRateTableIndex + i - 1].
5274                                 Ext_InfoFlag;
5275                         if (temp & InterlaceMode)
5276                                 i++;
5277                 }
5278         }
5279         i--;
5280         if ((pVBInfo->SetFlag & ProgrammingCRT2)) {
5281                 temp = XGI_AjustCRT2Rate(ModeNo, ModeIdIndex,
5282                                 RefreshRateTableIndex, &i, pVBInfo);
5283         }
5284         return RefreshRateTableIndex + i;
5285 }
5286
5287 static void XGI_SetLCDAGroup(unsigned short ModeNo, unsigned short ModeIdIndex,
5288                 struct xgi_hw_device_info *HwDeviceExtension,
5289                 struct vb_device_info *pVBInfo)
5290 {
5291         unsigned short RefreshRateTableIndex;
5292
5293         pVBInfo->SetFlag |= ProgrammingCRT2;
5294         RefreshRateTableIndex = XGI_GetRatePtrCRT2(HwDeviceExtension, ModeNo,
5295                         ModeIdIndex, pVBInfo);
5296         XGI_GetLVDSResInfo(ModeNo, ModeIdIndex, pVBInfo);
5297         XGI_GetLVDSData(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
5298         XGI_ModCRT1Regs(ModeNo, ModeIdIndex, RefreshRateTableIndex,
5299                         HwDeviceExtension, pVBInfo);
5300         XGI_SetLVDSRegs(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
5301         XGI_SetCRT2ECLK(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
5302 }
5303
5304 static unsigned char XGI_SetCRT2Group301(unsigned short ModeNo,
5305                 struct xgi_hw_device_info *HwDeviceExtension,
5306                 struct vb_device_info *pVBInfo)
5307 {
5308         unsigned short ModeIdIndex, RefreshRateTableIndex;
5309
5310         pVBInfo->SetFlag |= ProgrammingCRT2;
5311         XGI_SearchModeID(ModeNo, &ModeIdIndex, pVBInfo);
5312         pVBInfo->SelectCRT2Rate = 4;
5313         RefreshRateTableIndex = XGI_GetRatePtrCRT2(HwDeviceExtension, ModeNo,
5314                         ModeIdIndex, pVBInfo);
5315         XGI_SaveCRT2Info(ModeNo, pVBInfo);
5316         XGI_GetCRT2ResInfo(ModeNo, ModeIdIndex, pVBInfo);
5317         XGI_GetCRT2Data(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
5318         XGI_PreSetGroup1(ModeNo, ModeIdIndex, HwDeviceExtension,
5319                         RefreshRateTableIndex, pVBInfo);
5320         XGI_SetGroup1(ModeNo, ModeIdIndex, HwDeviceExtension,
5321                         RefreshRateTableIndex, pVBInfo);
5322         XGI_SetLockRegs(ModeNo, ModeIdIndex, HwDeviceExtension,
5323                         RefreshRateTableIndex, pVBInfo);
5324         XGI_SetGroup2(ModeNo, ModeIdIndex, RefreshRateTableIndex,
5325                         HwDeviceExtension, pVBInfo);
5326         XGI_SetLCDRegs(ModeNo, ModeIdIndex, HwDeviceExtension,
5327                         RefreshRateTableIndex, pVBInfo);
5328         XGI_SetTap4Regs(pVBInfo);
5329         XGI_SetGroup3(ModeNo, ModeIdIndex, pVBInfo);
5330         XGI_SetGroup4(ModeNo, ModeIdIndex, RefreshRateTableIndex,
5331                         HwDeviceExtension, pVBInfo);
5332         XGI_SetCRT2VCLK(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
5333         XGI_SetGroup5(ModeNo, ModeIdIndex, pVBInfo);
5334         XGI_AutoThreshold(pVBInfo);
5335         return 1;
5336 }
5337
5338 void XGI_SenseCRT1(struct vb_device_info *pVBInfo)
5339 {
5340         unsigned char CRTCData[17] = { 0x5F, 0x4F, 0x50, 0x82, 0x55, 0x81,
5341                         0x0B, 0x3E, 0xE9, 0x0B, 0xDF, 0xE7, 0x04, 0x00, 0x00,
5342                         0x05, 0x00 };
5343
5344         unsigned char SR01 = 0, SR1F = 0, SR07 = 0, SR06 = 0;
5345
5346         unsigned char CR17, CR63, SR31;
5347         unsigned short temp;
5348
5349         int i;
5350         xgifb_reg_set(pVBInfo->P3c4, 0x05, 0x86);
5351
5352         /* to fix XG42 single LCD sense to CRT+LCD */
5353         xgifb_reg_set(pVBInfo->P3d4, 0x57, 0x4A);
5354         xgifb_reg_set(pVBInfo->P3d4, 0x53, (xgifb_reg_get(
5355                         pVBInfo->P3d4, 0x53) | 0x02));
5356
5357         SR31 = xgifb_reg_get(pVBInfo->P3c4, 0x31);
5358         CR63 = xgifb_reg_get(pVBInfo->P3d4, 0x63);
5359         SR01 = xgifb_reg_get(pVBInfo->P3c4, 0x01);
5360
5361         xgifb_reg_set(pVBInfo->P3c4, 0x01, (unsigned char) (SR01 & 0xDF));
5362         xgifb_reg_set(pVBInfo->P3d4, 0x63, (unsigned char) (CR63 & 0xBF));
5363
5364         CR17 = xgifb_reg_get(pVBInfo->P3d4, 0x17);
5365         xgifb_reg_set(pVBInfo->P3d4, 0x17, (unsigned char) (CR17 | 0x80));
5366
5367         SR1F = xgifb_reg_get(pVBInfo->P3c4, 0x1F);
5368         xgifb_reg_set(pVBInfo->P3c4, 0x1F, (unsigned char) (SR1F | 0x04));
5369
5370         SR07 = xgifb_reg_get(pVBInfo->P3c4, 0x07);
5371         xgifb_reg_set(pVBInfo->P3c4, 0x07, (unsigned char) (SR07 & 0xFB));
5372         SR06 = xgifb_reg_get(pVBInfo->P3c4, 0x06);
5373         xgifb_reg_set(pVBInfo->P3c4, 0x06, (unsigned char) (SR06 & 0xC3));
5374
5375         xgifb_reg_set(pVBInfo->P3d4, 0x11, 0x00);
5376
5377         for (i = 0; i < 8; i++)
5378                 xgifb_reg_set(pVBInfo->P3d4, (unsigned short) i, CRTCData[i]);
5379
5380         for (i = 8; i < 11; i++)
5381                 xgifb_reg_set(pVBInfo->P3d4, (unsigned short) (i + 8),
5382                                 CRTCData[i]);
5383
5384         for (i = 11; i < 13; i++)
5385                 xgifb_reg_set(pVBInfo->P3d4, (unsigned short) (i + 4),
5386                                 CRTCData[i]);
5387
5388         for (i = 13; i < 16; i++)
5389                 xgifb_reg_set(pVBInfo->P3c4, (unsigned short) (i - 3),
5390                                 CRTCData[i]);
5391
5392         xgifb_reg_set(pVBInfo->P3c4, 0x0E, (unsigned char) (CRTCData[16]
5393                         & 0xE0));
5394
5395         xgifb_reg_set(pVBInfo->P3c4, 0x31, 0x00);
5396         xgifb_reg_set(pVBInfo->P3c4, 0x2B, 0x1B);
5397         xgifb_reg_set(pVBInfo->P3c4, 0x2C, 0xE1);
5398
5399         outb(0x00, pVBInfo->P3c8);
5400
5401         for (i = 0; i < 256 * 3; i++) {
5402                 outb(0x0F, (pVBInfo->P3c8 + 1)); /* DAC_TEST_PARMS */
5403         }
5404
5405         mdelay(1);
5406
5407         XGI_WaitDisply(pVBInfo);
5408         temp = inb(pVBInfo->P3c2);
5409
5410         if (temp & 0x10)
5411                 xgifb_reg_and_or(pVBInfo->P3d4, 0x32, 0xDF, 0x20);
5412         else
5413                 xgifb_reg_and_or(pVBInfo->P3d4, 0x32, 0xDF, 0x00);
5414
5415         /* avoid display something, set BLACK DAC if not restore DAC */
5416         outb(0x00, pVBInfo->P3c8);
5417
5418         for (i = 0; i < 256 * 3; i++) {
5419                 outb(0, (pVBInfo->P3c8 + 1));
5420         }
5421
5422         xgifb_reg_set(pVBInfo->P3c4, 0x01, SR01);
5423         xgifb_reg_set(pVBInfo->P3d4, 0x63, CR63);
5424         xgifb_reg_set(pVBInfo->P3c4, 0x31, SR31);
5425
5426         xgifb_reg_set(pVBInfo->P3d4, 0x53, (xgifb_reg_get(
5427                         pVBInfo->P3d4, 0x53) & 0xFD));
5428         xgifb_reg_set(pVBInfo->P3c4, 0x1F, (unsigned char) SR1F);
5429 }
5430
5431 static void XGI_EnableBridge(struct xgifb_video_info *xgifb_info,
5432                 struct xgi_hw_device_info *HwDeviceExtension,
5433                 struct vb_device_info *pVBInfo)
5434 {
5435         unsigned short tempah;
5436
5437         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
5438                         | VB_SIS302LV | VB_XGI301C)) {
5439                 if (pVBInfo->VBInfo & SetCRT2ToDualEdge)
5440                         /* Power on */
5441                         xgifb_reg_set(pVBInfo->Part1Port, 0x1E, 0x20);
5442
5443                 if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToTV |
5444                                        SetCRT2ToRAMDAC)) {
5445                         tempah = xgifb_reg_get(pVBInfo->P3c4, 0x32);
5446                         tempah &= 0xDF;
5447                         if (pVBInfo->VBInfo & SetInSlaveMode) {
5448                                 if (!(pVBInfo->VBInfo & SetCRT2ToRAMDAC))
5449                                         tempah |= 0x20;
5450                         }
5451                         xgifb_reg_set(pVBInfo->P3c4, 0x32, tempah);
5452                         xgifb_reg_or(pVBInfo->P3c4, 0x1E, 0x20);
5453
5454                         tempah = xgifb_reg_get(pVBInfo->Part1Port, 0x2E);
5455
5456                         if (!(tempah & 0x80))
5457                                 xgifb_reg_or(pVBInfo->Part1Port, 0x2E, 0x80);
5458                         xgifb_reg_and(pVBInfo->Part1Port, 0x00, 0x7F);
5459                 }
5460
5461                 if (!(pVBInfo->VBInfo & DisableCRT2Display)) {
5462                         xgifb_reg_and_or(pVBInfo->Part2Port, 0x00, ~0xE0,
5463                                         0x20); /* shampoo 0129 */
5464                         if (pVBInfo->VBType & (VB_SIS302LV | VB_XGI301C)) {
5465                                 if (pVBInfo->VBInfo &
5466                                         (SetCRT2ToLCD | XGI_SetCRT2ToLCDA))
5467                                         /* LVDS PLL power on */
5468                                         xgifb_reg_and(pVBInfo->Part4Port, 0x2A,
5469                                                       0x7F);
5470                                 /* LVDS Driver power on */
5471                                 xgifb_reg_and(pVBInfo->Part4Port, 0x30, 0x7F);
5472                         }
5473                 }
5474
5475                 tempah = 0x00;
5476
5477                 if (!(pVBInfo->VBInfo & DisableCRT2Display)) {
5478                         tempah = 0xc0;
5479
5480                         if (!(pVBInfo->VBInfo & SetSimuScanMode) &&
5481                             (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA) &&
5482                             (pVBInfo->VBInfo & SetCRT2ToDualEdge)) {
5483                                 tempah = tempah & 0x40;
5484                                 if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA)
5485                                         tempah = tempah ^ 0xC0;
5486                         }
5487                 }
5488
5489                 /* EnablePart4_1F */
5490                 xgifb_reg_or(pVBInfo->Part4Port, 0x1F, tempah);
5491
5492                 XGI_DisableGatingCRT(HwDeviceExtension, pVBInfo);
5493                 XGI_DisplayOn(xgifb_info, HwDeviceExtension, pVBInfo);
5494         } /* 301 */
5495         else { /* LVDS */
5496                 if (pVBInfo->VBInfo & (SetCRT2ToTV | SetCRT2ToLCD
5497                                 | XGI_SetCRT2ToLCDA))
5498                         /* enable CRT2 */
5499                         xgifb_reg_or(pVBInfo->Part1Port, 0x1E, 0x20);
5500
5501                 tempah = xgifb_reg_get(pVBInfo->Part1Port, 0x2E);
5502                 if (!(tempah & 0x80))
5503                         xgifb_reg_or(pVBInfo->Part1Port, 0x2E, 0x80);
5504
5505                 xgifb_reg_and(pVBInfo->Part1Port, 0x00, 0x7F);
5506                 XGI_DisplayOn(xgifb_info, HwDeviceExtension, pVBInfo);
5507         } /* End of VB */
5508 }
5509
5510 static void XGI_SetCRT1Group(struct xgifb_video_info *xgifb_info,
5511                 struct xgi_hw_device_info *HwDeviceExtension,
5512                 unsigned short ModeNo, unsigned short ModeIdIndex,
5513                 struct vb_device_info *pVBInfo)
5514 {
5515         unsigned short RefreshRateTableIndex, temp;
5516
5517         XGI_SetSeqRegs(ModeNo, ModeIdIndex, pVBInfo);
5518         outb(XGI330_StandTable.MISC, pVBInfo->P3c2);
5519         XGI_SetCRTCRegs(HwDeviceExtension, pVBInfo);
5520         XGI_SetATTRegs(ModeNo, ModeIdIndex, pVBInfo);
5521         XGI_SetGRCRegs(pVBInfo);
5522         XGI_ClearExt1Regs(pVBInfo);
5523
5524         if (HwDeviceExtension->jChipType == XG27) {
5525                 if (pVBInfo->IF_DEF_LVDS == 0)
5526                         XGI_SetDefaultVCLK(pVBInfo);
5527         }
5528
5529         temp = ~ProgrammingCRT2;
5530         pVBInfo->SetFlag &= temp;
5531         pVBInfo->SelectCRT2Rate = 0;
5532
5533         if (pVBInfo->VBType & (VB_SIS301B | VB_SIS302B | VB_SIS301LV
5534                         | VB_SIS302LV | VB_XGI301C)) {
5535                 if (pVBInfo->VBInfo & (SetSimuScanMode | XGI_SetCRT2ToLCDA
5536                                 | SetInSlaveMode)) {
5537                         pVBInfo->SetFlag |= ProgrammingCRT2;
5538                 }
5539         }
5540
5541         RefreshRateTableIndex = XGI_GetRatePtrCRT2(HwDeviceExtension, ModeNo,
5542                         ModeIdIndex, pVBInfo);
5543         if (RefreshRateTableIndex != 0xFFFF) {
5544                 XGI_SetSync(RefreshRateTableIndex, pVBInfo);
5545                 XGI_SetCRT1CRTC(ModeNo, ModeIdIndex, RefreshRateTableIndex,
5546                                 pVBInfo, HwDeviceExtension);
5547                 XGI_SetCRT1DE(HwDeviceExtension, ModeNo, ModeIdIndex,
5548                                 RefreshRateTableIndex, pVBInfo);
5549                 XGI_SetCRT1Offset(ModeNo, ModeIdIndex, RefreshRateTableIndex,
5550                                 HwDeviceExtension, pVBInfo);
5551                 XGI_SetCRT1VCLK(ModeNo, ModeIdIndex, HwDeviceExtension,
5552                                 RefreshRateTableIndex, pVBInfo);
5553         }
5554
5555         if (HwDeviceExtension->jChipType >= XG21) {
5556                 temp = xgifb_reg_get(pVBInfo->P3d4, 0x38);
5557                 if (temp & 0xA0) {
5558
5559                         if (HwDeviceExtension->jChipType == XG27)
5560                                 XGI_SetXG27CRTC(ModeNo, ModeIdIndex,
5561                                                 RefreshRateTableIndex, pVBInfo);
5562                         else
5563                                 XGI_SetXG21CRTC(ModeNo, ModeIdIndex,
5564                                                 RefreshRateTableIndex, pVBInfo);
5565
5566                         XGI_UpdateXG21CRTC(ModeNo, pVBInfo,
5567                                         RefreshRateTableIndex);
5568
5569                         xgifb_set_lcd(HwDeviceExtension->jChipType,
5570                                         pVBInfo, RefreshRateTableIndex, ModeNo);
5571
5572                         if (pVBInfo->IF_DEF_LVDS == 1)
5573                                 xgifb_set_lvds(xgifb_info,
5574                                                 HwDeviceExtension->jChipType,
5575                                                 ModeNo, ModeIdIndex, pVBInfo);
5576                 }
5577         }
5578
5579         pVBInfo->SetFlag &= (~ProgrammingCRT2);
5580         XGI_SetCRT1FIFO(ModeNo, HwDeviceExtension, pVBInfo);
5581         XGI_SetCRT1ModeRegs(HwDeviceExtension, ModeNo, ModeIdIndex,
5582                         RefreshRateTableIndex, pVBInfo);
5583         XGI_LoadDAC(ModeNo, ModeIdIndex, pVBInfo);
5584 }
5585
5586 unsigned char XGISetModeNew(struct xgifb_video_info *xgifb_info,
5587                         struct xgi_hw_device_info *HwDeviceExtension,
5588                         unsigned short ModeNo)
5589 {
5590         unsigned short ModeIdIndex;
5591         struct vb_device_info VBINF;
5592         struct vb_device_info *pVBInfo = &VBINF;
5593         pVBInfo->IF_DEF_LVDS = 0;
5594
5595         if (HwDeviceExtension->jChipType >= XG20) {
5596                 pVBInfo->IF_DEF_YPbPr = 0;
5597                 pVBInfo->IF_DEF_HiVision = 0;
5598                 pVBInfo->IF_DEF_CRT2Monitor = 0;
5599                 pVBInfo->VBType = 0; /*set VBType default 0*/
5600         } else {
5601                 pVBInfo->IF_DEF_YPbPr = 1;
5602                 pVBInfo->IF_DEF_HiVision = 1;
5603                 pVBInfo->IF_DEF_CRT2Monitor = 1;
5604         }
5605
5606         XGIRegInit(pVBInfo, xgifb_info->vga_base);
5607
5608         /* for x86 Linux, XG21 LVDS */
5609         if (HwDeviceExtension->jChipType == XG21) {
5610                 if ((xgifb_reg_get(pVBInfo->P3d4, 0x38) & 0xE0) == 0xC0)
5611                         pVBInfo->IF_DEF_LVDS = 1;
5612         }
5613         if (HwDeviceExtension->jChipType == XG27) {
5614                 if ((xgifb_reg_get(pVBInfo->P3d4, 0x38) & 0xE0) == 0xC0) {
5615                         if (xgifb_reg_get(pVBInfo->P3d4, 0x30) & 0x20)
5616                                 pVBInfo->IF_DEF_LVDS = 1;
5617                 }
5618         }
5619
5620         InitTo330Pointer(HwDeviceExtension->jChipType, pVBInfo);
5621         if (ModeNo & 0x80)
5622                 ModeNo = ModeNo & 0x7F;
5623         xgifb_reg_set(pVBInfo->P3c4, 0x05, 0x86);
5624
5625         if (HwDeviceExtension->jChipType < XG20)
5626                 XGI_UnLockCRT2(HwDeviceExtension, pVBInfo);
5627
5628         XGI_SearchModeID(ModeNo, &ModeIdIndex, pVBInfo);
5629
5630         if (HwDeviceExtension->jChipType < XG20) {
5631                 XGI_GetVBInfo(ModeNo, ModeIdIndex, HwDeviceExtension, pVBInfo);
5632                 XGI_GetTVInfo(ModeNo, ModeIdIndex, pVBInfo);
5633                 XGI_GetLCDInfo(ModeNo, ModeIdIndex, pVBInfo);
5634                 XGI_DisableBridge(xgifb_info, HwDeviceExtension, pVBInfo);
5635
5636                 if (pVBInfo->VBInfo & (SetSimuScanMode | XGI_SetCRT2ToLCDA) ||
5637                         (!(pVBInfo->VBInfo & SwitchCRT2))) {
5638                         XGI_SetCRT1Group(xgifb_info, HwDeviceExtension, ModeNo,
5639                                         ModeIdIndex, pVBInfo);
5640
5641                         if (pVBInfo->VBInfo & XGI_SetCRT2ToLCDA) {
5642                                 XGI_SetLCDAGroup(ModeNo, ModeIdIndex,
5643                                                 HwDeviceExtension, pVBInfo);
5644                         }
5645                 }
5646
5647                 if (pVBInfo->VBInfo & (SetSimuScanMode | SwitchCRT2)) {
5648                         switch (HwDeviceExtension->ujVBChipID) {
5649                         case VB_CHIP_301: /* fall through */
5650                         case VB_CHIP_302:
5651                                 XGI_SetCRT2Group301(ModeNo, HwDeviceExtension,
5652                                                 pVBInfo); /*add for CRT2 */
5653                                 break;
5654
5655                         default:
5656                                 break;
5657                         }
5658                 }
5659
5660                 XGI_SetCRT2ModeRegs(ModeNo, HwDeviceExtension, pVBInfo);
5661                 XGI_OEM310Setting(ModeNo, ModeIdIndex, pVBInfo); /*0212*/
5662                 XGI_EnableBridge(xgifb_info, HwDeviceExtension, pVBInfo);
5663         } /* !XG20 */
5664         else {
5665                 if (pVBInfo->IF_DEF_LVDS == 1)
5666                         if (!XGI_XG21CheckLVDSMode(xgifb_info, ModeNo,
5667                                                    ModeIdIndex,
5668                                                    pVBInfo))
5669                                 return 0;
5670
5671                 pVBInfo->ModeType = XGI330_EModeIDTable[ModeIdIndex].
5672                                                 Ext_ModeFlag & ModeTypeMask;
5673
5674                 pVBInfo->SetFlag = 0;
5675                 pVBInfo->VBInfo = DisableCRT2Display;
5676
5677                 XGI_DisplayOff(xgifb_info, HwDeviceExtension, pVBInfo);
5678
5679                 XGI_SetCRT1Group(xgifb_info, HwDeviceExtension, ModeNo,
5680                                 ModeIdIndex, pVBInfo);
5681
5682                 XGI_DisplayOn(xgifb_info, HwDeviceExtension, pVBInfo);
5683         }
5684
5685         XGI_UpdateModeInfo(HwDeviceExtension, pVBInfo);
5686
5687         if (HwDeviceExtension->jChipType < XG20)
5688                 XGI_LockCRT2(HwDeviceExtension, pVBInfo);
5689
5690         return 1;
5691 }