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