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