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