1/* Copyright (C) 2003-2006 by XGI Technology, Taiwan.
2 *
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation on the rights to use, copy, modify, merge,
9 * publish, distribute, sublicense, and/or sell copies of the Software,
10 * and to permit persons to whom the Software is furnished to do so,
11 * subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NON-INFRINGEMENT.  IN NO EVENT SHALL XGI AND/OR
21 *  ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 */
26#ifdef HAVE_CONFIG_H
27#include "config.h"
28#endif
29
30#include "osdef.h"
31
32#ifdef LINUX_XF86
33#include "xf86.h"
34#include "xf86PciInfo.h"
35#include "xgi.h"
36#include "xgi_regs.h"
37#endif
38
39#ifdef LINUX_KERNEL
40#include <asm/io.h>
41#include <linux/types.h>
42#include <linux/version.h>
43#include "XGIfb.h"
44#endif
45
46#include "vb_def.h"
47#include "vgatypes.h"
48#include "vb_struct.h"
49#include "vb_table.h"
50#include "vb_setmode.h"
51
52#define  IndexMask 0xff
53#ifndef XGI_MASK_DUAL_CHIP
54#define XGI_MASK_DUAL_CHIP	  0x04  /* SR3A */
55#endif
56
57
58BOOLEAN CheckDualChip(PVB_DEVICE_INFO pVBInfo);
59static BOOLEAN XGI_IsLCDDualLink(PVB_DEVICE_INFO pVBInfo);
60BOOLEAN XGI_SetCRT2Group301(USHORT ModeNo,
61                            PXGI_HW_DEVICE_INFO HwDeviceExtension,
62                            PVB_DEVICE_INFO pVBInfo);
63BOOLEAN XGI_BacklightByDrv(PVB_DEVICE_INFO pVBInfo);
64
65BOOLEAN XGI_IsLCDON(PVB_DEVICE_INFO pVBInfo);
66BOOLEAN XGI_DisableChISLCD(PVB_DEVICE_INFO pVBInfo);
67BOOLEAN XGI_EnableChISLCD(PVB_DEVICE_INFO pVBInfo);
68BOOLEAN XGI_AjustCRT2Rate(USHORT ModeNo, USHORT ModeIdIndex,
69                          USHORT RefreshRateTableIndex, USHORT * i,
70                          PVB_DEVICE_INFO pVBInfo);
71BOOLEAN XGI_GetLCDInfo(USHORT ModeNo, USHORT ModeIdIndex,
72                       PVB_DEVICE_INFO pVBInfo);
73BOOLEAN XGI_BridgeIsOn(PVB_DEVICE_INFO pVBInfo);
74USHORT XGI_GetOffset(USHORT ModeNo, USHORT ModeIdIndex,
75                     USHORT RefreshRateTableIndex,
76                     PXGI_HW_DEVICE_INFO HwDeviceExtension,
77                     PVB_DEVICE_INFO pVBInfo);
78USHORT XGI_GetRatePtrCRT2(PXGI_HW_DEVICE_INFO HwDeviceExtension, USHORT ModeNo, USHORT ModeIdIndex,
79                          PVB_DEVICE_INFO pVBInfo);
80USHORT XGI_GetResInfo(USHORT ModeNo, USHORT ModeIdIndex,
81                             PVB_DEVICE_INFO pVBInfo);
82USHORT XGI_GetVGAHT2(PVB_DEVICE_INFO pVBInfo);
83static unsigned XGI_GetVCLK2Ptr(USHORT ModeNo, USHORT ModeIdIndex,
84                                USHORT RefreshRateTableIndex,
85                                PVB_DEVICE_INFO pVBInfo);
86void XGI_VBLongWait(PVB_DEVICE_INFO pVBInfo);
87void XGI_SaveCRT2Info(USHORT ModeNo, PVB_DEVICE_INFO pVBInfo);
88void XGI_GetCRT2Data(USHORT ModeNo, USHORT ModeIdIndex,
89                     USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo);
90void XGI_GetCRT2ResInfo(USHORT ModeNo, USHORT ModeIdIndex,
91                        PVB_DEVICE_INFO pVBInfo);
92void XGI_PreSetGroup1(USHORT ModeNo, USHORT ModeIdIndex,
93                      PXGI_HW_DEVICE_INFO HwDeviceExtension,
94                      USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo);
95void XGI_SetGroup1(USHORT ModeNo, USHORT ModeIdIndex,
96                   PXGI_HW_DEVICE_INFO HwDeviceExtension,
97                   USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo);
98void XGI_SetLockRegs(USHORT ModeNo, USHORT ModeIdIndex,
99                     PXGI_HW_DEVICE_INFO HwDeviceExtension,
100                     USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo);
101void XGI_SetLCDRegs(USHORT ModeNo, USHORT ModeIdIndex,
102                    PXGI_HW_DEVICE_INFO HwDeviceExtension,
103                    USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo);
104void XGI_SetGroup2(USHORT ModeNo, USHORT ModeIdIndex,
105                   USHORT RefreshRateTableIndex,
106                   PXGI_HW_DEVICE_INFO HwDeviceExtension,
107                   PVB_DEVICE_INFO pVBInfo);
108void XGI_SetGroup3(USHORT ModeNo, USHORT ModeIdIndex,
109                   PVB_DEVICE_INFO pVBInfo);
110void XGI_SetGroup4(USHORT ModeNo, USHORT ModeIdIndex,
111                   USHORT RefreshRateTableIndex,
112                   PXGI_HW_DEVICE_INFO HwDeviceExtension,
113                   PVB_DEVICE_INFO pVBInfo);
114void XGI_SetGroup5(USHORT ModeNo, USHORT ModeIdIndex,
115                   PVB_DEVICE_INFO pVBInfo);
116static const void *XGI_GetLcdPtr(USHORT BX, USHORT ModeNo, USHORT ModeIdIndex,
117                    USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo);
118static const void *XGI_GetTVPtr(USHORT BX, USHORT ModeNo, USHORT ModeIdIndex,
119                   USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo);
120void XGI_FirePWDEnable(PVB_DEVICE_INFO pVBInfo);
121void XGI_EnableGatingCRT(PXGI_HW_DEVICE_INFO HwDeviceExtension,
122                         PVB_DEVICE_INFO pVBInfo);
123void XGI_DisableGatingCRT(PXGI_HW_DEVICE_INFO HwDeviceExtension,
124                          PVB_DEVICE_INFO pVBInfo);
125void XGI_SetPanelDelay(USHORT tempbl, PVB_DEVICE_INFO pVBInfo);
126void XGI_SetPanelPower(USHORT tempah, USHORT tempbl, PVB_DEVICE_INFO pVBInfo);
127void XGI_EnablePWD(PVB_DEVICE_INFO pVBInfo);
128void XGI_DisablePWD(PVB_DEVICE_INFO pVBInfo);
129void XGI_AutoThreshold(PVB_DEVICE_INFO pVBInfo);
130void XGI_SetTap4Regs(PVB_DEVICE_INFO pVBInfo);
131void SetDualChipRegs(PXGI_HW_DEVICE_INFO, PVB_DEVICE_INFO pVBInfo);
132void XGI_DisplayOn(PXGI_HW_DEVICE_INFO HwDeviceExtension, PVB_DEVICE_INFO pVBInfo);
133void XGI_DisplayOff(PXGI_HW_DEVICE_INFO HwDeviceExtension,PVB_DEVICE_INFO pVBInfo);
134void XGI_SetCRT1Group(PXGI_HW_DEVICE_INFO HwDeviceExtension, USHORT ModeNo,
135                      USHORT ModeIdIndex, PVB_DEVICE_INFO pVBInfo);
136/* Jong 10/03/2007 */
137void     XGI_SetXG21CRTC(USHORT ModeNo, USHORT ModeIdIndex, USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo);
138void     XGI_SetXG21LCD(PVB_DEVICE_INFO pVBInfo,USHORT RefreshRateTableIndex,USHORT ModeNo);
139void     XGI_SetXG27CRTC(USHORT ModeNo, USHORT ModeIdIndex, USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo);
140void     XGI_SetXG27LCD(PVB_DEVICE_INFO pVBInfo,USHORT RefreshRateTableIndex,USHORT ModeNo);
141void	 XGI_UpdateXG21CRTC(USHORT ModeNo, PVB_DEVICE_INFO pVBInfo, USHORT RefreshRateTableIndex);
142
143static void XGI_WaitDisplay(PVB_DEVICE_INFO pVBInfo);
144void XGI_SenseCRT1(PVB_DEVICE_INFO pVBInfo);
145
146void XGI_SetCRT1CRTC(USHORT ModeNo, USHORT ModeIdIndex,
147                     USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo,
148                     PXGI_HW_DEVICE_INFO HwDeviceExtension);
149void XGI_SetCRT1Timing_H(PVB_DEVICE_INFO pVBInfo,
150                         PXGI_HW_DEVICE_INFO HwDeviceExtension);
151void XGI_SetCRT1Timing_V(USHORT ModeIdIndex, USHORT ModeNo,
152                         PVB_DEVICE_INFO pVBInfo);
153void XGI_SetCRT1DE(PXGI_HW_DEVICE_INFO HwDeviceExtension, USHORT ModeNo,
154                   USHORT ModeIdIndex, USHORT RefreshRateTableIndex,
155                   PVB_DEVICE_INFO pVBInfo);
156void XGI_SetCRT1VCLK(USHORT ModeNo, USHORT ModeIdIndex,
157                     PXGI_HW_DEVICE_INFO HwDeviceExtension,
158                     USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo);
159void XGI_SetCRT1FIFO(USHORT ModeNo, PXGI_HW_DEVICE_INFO HwDeviceExtension,
160                     PVB_DEVICE_INFO pVBInfo);
161void XGI_SetCRT1ModeRegs(PXGI_HW_DEVICE_INFO HwDeviceExtension, USHORT ModeNo,
162                         USHORT ModeIdIndex, USHORT RefreshRateTableIndex,
163                         PVB_DEVICE_INFO pVBInfo);
164void XGI_SetVCLKState(PXGI_HW_DEVICE_INFO HwDeviceExtension, USHORT ModeNo,
165                      USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo);
166
167void XGI_LoadDAC(USHORT ModeNo, USHORT ModeIdIndex, PVB_DEVICE_INFO pVBInfo);
168void XGI_SetLCDAGroup(USHORT ModeNo, USHORT ModeIdIndex,
169                      PXGI_HW_DEVICE_INFO HwDeviceExtension,
170                      PVB_DEVICE_INFO pVBInfo);
171void XGI_GetLVDSResInfo(USHORT ModeNo, USHORT ModeIdIndex,
172                        PVB_DEVICE_INFO pVBInfo);
173void XGI_GetLVDSData(USHORT ModeNo, USHORT ModeIdIndex,
174                     USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo);
175void XGI_ModCRT1Regs(USHORT ModeNo, USHORT ModeIdIndex,
176                     USHORT RefreshRateTableIndex,
177                     PXGI_HW_DEVICE_INFO HwDeviceExtension,
178                     PVB_DEVICE_INFO pVBInfo);
179void XGI_SetLVDSRegs(USHORT ModeNo, USHORT ModeIdIndex,
180                     USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo);
181void XGI_UpdateModeInfo(PXGI_HW_DEVICE_INFO HwDeviceExtension,
182                        PVB_DEVICE_INFO pVBInfo);
183void XGI_GetVBType(PVB_DEVICE_INFO pVBInfo);
184void XGI_GetVBInfo(USHORT ModeNo, USHORT ModeIdIndex,
185                   PXGI_HW_DEVICE_INFO HwDeviceExtension,
186                   PVB_DEVICE_INFO pVBInfo);
187void XGI_GetTVInfo(USHORT ModeNo, USHORT ModeIdIndex,
188                   PVB_DEVICE_INFO pVBInfo);
189void XGI_SetCRT2ECLK(USHORT ModeNo, USHORT ModeIdIndex,
190                     USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo);
191void InitTo330Pointer(UCHAR, PVB_DEVICE_INFO pVBInfo);
192void XGI_GetLCDSync(ULONG * HSyncWidth, ULONG * VSyncWidth,
193                    PVB_DEVICE_INFO pVBInfo);
194void XGI_DisableBridge(PXGI_HW_DEVICE_INFO HwDeviceExtension,
195                       PVB_DEVICE_INFO pVBInfo);
196void XGI_EnableBridge(PXGI_HW_DEVICE_INFO HwDeviceExtension,
197                      PVB_DEVICE_INFO pVBInfo);
198void XGI_SetCRT2VCLK(USHORT ModeNo, USHORT ModeIdIndex,
199                     USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo);
200void XGI_OEM310Setting(USHORT ModeNo, USHORT ModeIdIndex,
201                       PVB_DEVICE_INFO pVBInfo);
202void XGI_SetDelayComp(PVB_DEVICE_INFO pVBInfo);
203void XGI_SetLCDCap(PVB_DEVICE_INFO pVBInfo);
204void XGI_SetLCDCap_A(USHORT tempcx, PVB_DEVICE_INFO pVBInfo);
205void XGI_SetLCDCap_B(USHORT tempcx, PVB_DEVICE_INFO pVBInfo);
206void SetSpectrum(PVB_DEVICE_INFO pVBInfo);
207void XGI_SetAntiFlicker(USHORT ModeNo, USHORT ModeIdIndex,
208                        PVB_DEVICE_INFO pVBInfo);
209void XGI_SetEdgeEnhance(USHORT ModeNo, USHORT ModeIdIndex,
210                        PVB_DEVICE_INFO pVBInfo);
211void XGI_SetPhaseIncr(PVB_DEVICE_INFO pVBInfo);
212void XGI_SetYFilter(USHORT ModeNo, USHORT ModeIdIndex,
213                    PVB_DEVICE_INFO pVBInfo);
214void XGI_GetTVPtrIndex2(USHORT * tempbx, UCHAR * tempcl, UCHAR * tempch,
215                        PVB_DEVICE_INFO pVBInfo);
216USHORT XGI_GetTVPtrIndex(PVB_DEVICE_INFO pVBInfo);
217void XGI_SetCRT2ModeRegs(USHORT ModeNo, PXGI_HW_DEVICE_INFO,
218                         PVB_DEVICE_INFO pVBInfo);
219void XGI_GetRAMDAC2DATA(USHORT ModeNo, USHORT ModeIdIndex,
220                        USHORT RefreshRateTableIndex,
221                        PVB_DEVICE_INFO pVBInfo);
222void XGI_UnLockCRT2(PXGI_HW_DEVICE_INFO, PVB_DEVICE_INFO pVBInfo);
223void XGI_LockCRT2(PXGI_HW_DEVICE_INFO, PVB_DEVICE_INFO pVBInfo);
224void XGINew_EnableCRT2(PVB_DEVICE_INFO pVBInfo);
225void XGINew_LCD_Wait_Time(UCHAR DelayTime, PVB_DEVICE_INFO pVBInfo);
226void XGI_SetCRT1Offset(USHORT ModeNo, USHORT ModeIdIndex,
227                       USHORT RefreshRateTableIndex,
228                       PXGI_HW_DEVICE_INFO HwDeviceExtension,
229                       PVB_DEVICE_INFO pVBInfo);
230static void XGI_GetLCDVCLKPtr(UCHAR *di, PVB_DEVICE_INFO pVBInfo);
231static unsigned XGI_GetVCLKPtr(USHORT RefreshRateTableIndex, USHORT ModeNo,
232                               USHORT ModeIdIndex, PVB_DEVICE_INFO pVBInfo);
233static void XGI_GetVCLKLen(unsigned vclkindex, UCHAR *di,
234                           PVB_DEVICE_INFO pVBInfo);
235USHORT XGI_GetLCDCapPtr(PVB_DEVICE_INFO pVBInfo);
236USHORT XGI_GetLCDCapPtr1(PVB_DEVICE_INFO pVBInfo);
237static const XGI301C_Tap4TimingStruct *XGI_GetTap4Ptr(USHORT tempcx, PVB_DEVICE_INFO pVBInfo);
238
239/* Jong 10/03/2007 */
240void     XGI_SetXG21FPBits(PVB_DEVICE_INFO pVBInfo);
241void     XGI_SetXG27FPBits(PVB_DEVICE_INFO pVBInfo);
242UCHAR    XGI_XG21GetPSCValue(PVB_DEVICE_INFO pVBInfo);
243UCHAR    XGI_XG27GetPSCValue(PVB_DEVICE_INFO pVBInfo);
244void     XGI_XG21BLSignalVDD(USHORT tempbh,USHORT tempbl, PVB_DEVICE_INFO pVBInfo);
245void     XGI_XG27BLSignalVDD(USHORT tempbh,USHORT tempbl, PVB_DEVICE_INFO pVBInfo);
246void     XGI_XG21SetPanelDelay(USHORT tempbl, PVB_DEVICE_INFO pVBInfo);
247BOOLEAN  XGI_XG21CheckLVDSMode(USHORT ModeNo,USHORT ModeIdIndex, PVB_DEVICE_INFO pVBInfo );
248void     XGI_SetXG21LVDSPara(USHORT ModeNo,USHORT ModeIdIndex, PVB_DEVICE_INFO pVBInfo );
249void     XGI_SetXG27LVDSPara(USHORT ModeNo,USHORT ModeIdIndex, PVB_DEVICE_INFO pVBInfo );
250UCHAR    XGI_SetDefaultVCLK( PVB_DEVICE_INFO pVBInfo );
251
252const uint8_t XGI_MDA_DAC[] = {
253    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
254    0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15,
255    0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15,
256    0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
257    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258    0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15,
259    0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15,
260    0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F
261};
262
263const uint8_t XGI_CGA_DAC[] = {
264    0x00, 0x10, 0x04, 0x14, 0x01, 0x11, 0x09, 0x15,
265    0x00, 0x10, 0x04, 0x14, 0x01, 0x11, 0x09, 0x15,
266    0x2A, 0x3A, 0x2E, 0x3E, 0x2B, 0x3B, 0x2F, 0x3F,
267    0x2A, 0x3A, 0x2E, 0x3E, 0x2B, 0x3B, 0x2F, 0x3F,
268    0x00, 0x10, 0x04, 0x14, 0x01, 0x11, 0x09, 0x15,
269    0x00, 0x10, 0x04, 0x14, 0x01, 0x11, 0x09, 0x15,
270    0x2A, 0x3A, 0x2E, 0x3E, 0x2B, 0x3B, 0x2F, 0x3F,
271    0x2A, 0x3A, 0x2E, 0x3E, 0x2B, 0x3B, 0x2F, 0x3F
272};
273
274const uint8_t XGI_EGA_DAC[] = {
275    0x00, 0x10, 0x04, 0x14, 0x01, 0x11, 0x05, 0x15,
276    0x20, 0x30, 0x24, 0x34, 0x21, 0x31, 0x25, 0x35,
277    0x08, 0x18, 0x0C, 0x1C, 0x09, 0x19, 0x0D, 0x1D,
278    0x28, 0x38, 0x2C, 0x3C, 0x29, 0x39, 0x2D, 0x3D,
279    0x02, 0x12, 0x06, 0x16, 0x03, 0x13, 0x07, 0x17,
280    0x22, 0x32, 0x26, 0x36, 0x23, 0x33, 0x27, 0x37,
281    0x0A, 0x1A, 0x0E, 0x1E, 0x0B, 0x1B, 0x0F, 0x1F,
282    0x2A, 0x3A, 0x2E, 0x3E, 0x2B, 0x3B, 0x2F, 0x3F
283};
284
285const uint8_t XGI_VGA_DAC[] = {
286    0x00, 0x10, 0x04, 0x14, 0x01, 0x11, 0x09, 0x15,
287    0x2A, 0x3A, 0x2E, 0x3E, 0x2B, 0x3B, 0x2F, 0x3F,
288    0x00, 0x05, 0x08, 0x0B, 0x0E, 0x11, 0x14, 0x18,
289    0x1C, 0x20, 0x24, 0x28, 0x2D, 0x32, 0x38, 0x3F,
290    0x00, 0x10, 0x1F, 0x2F, 0x3F, 0x1F, 0x27, 0x2F,
291    0x37, 0x3F, 0x2D, 0x31, 0x36, 0x3A, 0x3F, 0x00,
292    0x07, 0x0E, 0x15, 0x1C, 0x0E, 0x11, 0x15, 0x18,
293    0x1C, 0x14, 0x16, 0x18, 0x1A, 0x1C, 0x00, 0x04,
294    0x08, 0x0C, 0x10, 0x08, 0x0A, 0x0C, 0x0E, 0x10,
295    0x0B, 0x0C, 0x0D, 0x0F, 0x10
296};
297
298
299/* --------------------------------------------------------------------- */
300/* Function : InitTo330Pointer */
301/* Input : */
302/* Output : */
303/* Description : */
304/* --------------------------------------------------------------------- */
305void
306InitTo330Pointer(UCHAR ChipType, PVB_DEVICE_INFO pVBInfo)
307{
308    pVBInfo->SModeIDTable = XGI330_SModeIDTable;
309    pVBInfo->StandTable = XGI330_StandTable;
310    pVBInfo->EModeIDTable = XGI330_EModeIDTable;
311    pVBInfo->RefIndex = XGI330_RefIndex;
312    pVBInfo->XGINEWUB_CRT1Table = XGI_CRT1Table;
313
314    /* add for new UNIVGABIOS */
315    /* XGINew_UBLCDDataTable = (XGI_LCDDataTablStruct *) XGI_LCDDataTable ; */
316    /* XGINew_UBTVDataTable = (XGI_TVDataTablStruct *) XGI_TVDataTable ; */
317
318
319    if (ChipType >= XG40) {
320        (void) memcpy(pVBInfo->MCLKData, XGI340New_MCLKData, sizeof(XGI340New_MCLKData));
321        (void) memcpy(pVBInfo->ECLKData, XGI340_ECLKData, sizeof(XGI340_ECLKData));
322    }
323    else {
324        (void) memcpy(pVBInfo->MCLKData, XGI330New_MCLKData, sizeof(XGI330New_MCLKData));
325        (void) memcpy(pVBInfo->ECLKData, XGI330_ECLKData, sizeof(XGI330_ECLKData));
326    }
327
328    pVBInfo->VCLKData = (const XGI_VCLKDataStruct *)XGI_VCLKData;
329    pVBInfo->VBVCLKData = (const XGI_VBVCLKDataStruct *)XGI_VBVCLKData;
330    pVBInfo->ScreenOffset = XGI330_ScreenOffset;
331    pVBInfo->StResInfo = XGI330_StResInfo;
332    pVBInfo->ModeResInfo = XGI330_ModeResInfo;
333
334    pVBInfo->OutputSelect = XGI330_OutputSelect;
335    pVBInfo->SoftSetting = XGI330_SoftSetting;
336    pVBInfo->SR07 = XGI330_SR07;
337    pVBInfo->LCDResInfo = 0;
338    pVBInfo->LCDTypeInfo = 0;
339    pVBInfo->LCDInfo = 0;
340    pVBInfo->VBInfo = 0;
341    pVBInfo->TVInfo = 0;
342
343
344    (void) memcpy(pVBInfo->SR15, XGI340_SR13, sizeof(XGI340_SR13));
345    (void) memcpy(pVBInfo->CR40, XGI340_CR41, sizeof(XGI340_CR41));
346    (void) memcpy(pVBInfo->SR25, XGI330_SR25, sizeof(XGI330_SR25));
347    pVBInfo->SR31 = XGI330_SR31;
348    pVBInfo->SR32 = XGI330_SR32;
349    (void) memcpy(pVBInfo->CR6B, XGI340_CR6B, sizeof(XGI340_CR6B));
350    if (ChipType == XG45) {
351        (void) memcpy(pVBInfo->XG45CR6E, XGI45_CR6E, sizeof(XGI45_CR6E));
352        (void) memcpy(pVBInfo->XG45CR6F, XGI45_CR6F, sizeof(XGI45_CR6F));
353    }
354    else {
355        (void) memcpy(pVBInfo->CR6E, XGI340_CR6E, sizeof(XGI340_CR6E));
356        (void) memcpy(pVBInfo->CR6F, XGI340_CR6F, sizeof(XGI340_CR6F));
357    }
358    (void) memcpy(pVBInfo->CR89, XGI340_CR89, sizeof(XGI340_CR89));
359    (void) memcpy(pVBInfo->AGPReg, XGI340_AGPReg, sizeof(XGI340_AGPReg));
360    (void) memcpy(pVBInfo->SR16, XGI340_SR16, sizeof(XGI340_SR16));
361    pVBInfo->CRCF = XG40_CRCF;
362    pVBInfo->DRAMTypeDefinition = XG40_DRAMTypeDefinition;
363
364
365    (void) memcpy(pVBInfo->CR49, XGI330_CR49, sizeof(XGI330_CR49));
366    pVBInfo->SR1F = XGI330_SR1F;
367    pVBInfo->SR21 = XGI330_SR21;
368    pVBInfo->SR22 = XGI330_SR22;
369    pVBInfo->SR23 = XGI330_SR23;
370    pVBInfo->SR24 = XGI330_SR24;
371    pVBInfo->SR33 = XGI330_SR33;
372
373
374
375    pVBInfo->CRT2Data_1_2 = XGI330_CRT2Data_1_2;
376    pVBInfo->CRT2Data_4_D = XGI330_CRT2Data_4_D;
377    pVBInfo->CRT2Data_4_E = XGI330_CRT2Data_4_E;
378    pVBInfo->CRT2Data_4_10 = XGI330_CRT2Data_4_10;
379    pVBInfo->pRGBSenseData = &XGI330_RGBSenseData;
380    pVBInfo->pVideoSenseData = &XGI330_VideoSenseData;
381    pVBInfo->pYCSenseData = &XGI330_YCSenseData;
382    pVBInfo->pRGBSenseData2 = &XGI330_RGBSenseData2;
383    pVBInfo->pVideoSenseData2 = &XGI330_VideoSenseData2;
384    pVBInfo->pYCSenseData2 = &XGI330_YCSenseData2;
385
386    pVBInfo->NTSCTiming = XGI330_NTSCTiming;
387    pVBInfo->PALTiming = XGI330_PALTiming;
388    pVBInfo->HiTVExtTiming = XGI330_HiTVExtTiming;
389    pVBInfo->HiTVSt1Timing = XGI330_HiTVSt1Timing;
390    pVBInfo->HiTVSt2Timing = XGI330_HiTVSt2Timing;
391    pVBInfo->HiTVTextTiming = XGI330_HiTVTextTiming;
392    pVBInfo->YPbPr750pTiming = XGI330_YPbPr750pTiming;
393    pVBInfo->YPbPr525pTiming = XGI330_YPbPr525pTiming;
394    pVBInfo->YPbPr525iTiming = XGI330_YPbPr525iTiming;
395    pVBInfo->HiTVGroup3Data = XGI330_HiTVGroup3Data;
396    pVBInfo->HiTVGroup3Simu = XGI330_HiTVGroup3Simu;
397    pVBInfo->HiTVGroup3Text = XGI330_HiTVGroup3Text;
398    pVBInfo->Ren525pGroup3 = XGI330_Ren525pGroup3;
399    pVBInfo->Ren750pGroup3 = XGI330_Ren750pGroup3;
400
401
402    (void) memcpy(& pVBInfo->TimingH, XGI_TimingH, sizeof(XGI_TimingH));
403    (void) memcpy(& pVBInfo->TimingV, XGI_TimingV, sizeof(XGI_TimingV));
404
405    /* Jong 10/17/2007; merge code */
406    pVBInfo->UpdateCRT1 = (XGI_XG21CRT1Struct *) XGI_UpdateCRT1Table ;
407
408    pVBInfo->CHTVVCLKUNTSC = XGI330_CHTVVCLKUNTSC;
409    pVBInfo->CHTVVCLKONTSC = XGI330_CHTVVCLKONTSC;
410    pVBInfo->CHTVVCLKUPAL = XGI330_CHTVVCLKUPAL;
411    pVBInfo->CHTVVCLKOPAL = XGI330_CHTVVCLKOPAL;
412
413    /* 310 customization related */
414    if ((pVBInfo->VBType & VB_XGI301LV) || (pVBInfo->VBType & VB_XGI302LV))
415        pVBInfo->LCDCapList = XGI_LCDDLCapList;
416    else
417        pVBInfo->LCDCapList = XGI_LCDCapList;
418
419    /* Jong 10/03/2007 */
420    if ( ( ChipType == XG21 ) || ( ChipType == XG27 ) )
421        pVBInfo->XG21_LVDSCapList = XGI21_LCDCapList ;
422
423    pVBInfo->XGI_TVDelayList = XGI301TVDelayList;
424    pVBInfo->XGI_TVDelayList2 = XGI301TVDelayList2;
425
426
427    pVBInfo->I2CDefinition = XG40_I2CDefinition;
428
429    /* Jong 10/03/2007 */
430    if (ChipType >= XG20)
431        pVBInfo->CR97 = XG20_CR97;
432
433
434    /* Jong 10/03/2007 */
435    if ( ChipType == XG27 )
436    {
437        /* pVBInfo->MCLKData = (XGI_MCLKDataStruct *) XGI27New_MCLKData ; */
438		(void) memcpy(pVBInfo->MCLKData, XGI27New_MCLKData, sizeof(XGI27New_MCLKData));
439
440        /* pVBInfo->CR40 = XGI27_cr41 ; */
441		(void) memcpy(pVBInfo->CR40, XGI27_cr41, sizeof(XGI27_cr41));
442
443    	pVBInfo->CR97 = XG27_CR97 ;
444    	pVBInfo->pSR36 = &XG27_SR36 ;
445    	pVBInfo->pCR8F = &XG27_CR8F ;
446    	pVBInfo->pCRD0 = XG27_CRD0 ;
447    	pVBInfo->pCRDE = XG27_CRDE ;
448    	pVBInfo->pSR40 = &XG27_SR40 ;
449    	pVBInfo->pSR41 = &XG27_SR41 ;
450    }
451
452    if ( ChipType >= XG20 )
453    {
454    	pVBInfo->pDVOSetting = &XG21_DVOSetting ;
455    	pVBInfo->pCR2E = &XG21_CR2E ;
456    	pVBInfo->pCR2F = &XG21_CR2F ;
457    	pVBInfo->pCR46 = &XG21_CR46 ;
458    	pVBInfo->pCR47 = &XG21_CR47 ;
459    }
460
461}
462
463
464
465
466
467
468/* --------------------------------------------------------------------- */
469/* Function : XGISetModeNew */
470/* Input : */
471/* Output : */
472/* Description : */
473/* --------------------------------------------------------------------- */
474BOOLEAN
475XGISetModeNew(PXGI_HW_DEVICE_INFO HwDeviceExtension, PVB_DEVICE_INFO pVBInfo,
476	      USHORT ModeNo)
477{
478#ifndef LINUX_XF86
479    ULONG temp;
480    USHORT KeepLockReg;
481#endif
482    USHORT ModeIdIndex;
483    /* PUCHAR pVBInfo->FBAddr = HwDeviceExtension->pjVideoMemoryAddress ; */
484    USHORT temp_mode_no;
485
486    pVBInfo->IF_DEF_LVDS = 0 ;
487    pVBInfo->IF_DEF_VideoCapture = 1;
488    pVBInfo->IF_DEF_ScaleLCD = 1;
489
490	unsigned vga_info; /* Jong 11/28/2007 */
491
492	PDEBUG(ErrorF("XGISetModeNew()...begin\n"));
493
494    /* Jong 10/03/2007 */
495    if ( HwDeviceExtension->jChipType == XG27 )
496    {
497        if ( ( XGI_GetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x38 ) & 0xE0 ) == 0xC0 )
498        {
499          if ( XGI_GetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x30 ) & 0x20 )
500          {
501            pVBInfo->IF_DEF_LVDS = 1 ;
502          }
503        }
504    }
505
506    /* Jong 10/03/20007 */
507    if ( HwDeviceExtension->jChipType < XG20 )			/* kuku 2004/06/25 */
508		XGI_GetVBType( pVBInfo ) ;
509
510    /* Jong 10/17/2007; merge code */
511    InitTo330Pointer( HwDeviceExtension->jChipType, pVBInfo ) ;
512
513	PDEBUG(ErrorF("XGISetModeNew()...1\n"));
514
515    if (ModeNo & 0x80) {
516        ModeNo = ModeNo & 0x7F;
517    }
518
519    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x05, 0x86);
520
521    /* Jong 10/03/2007 */
522    if (HwDeviceExtension->jChipType < XG20)   /* kuku 2004/06/25 1.Openkey */
523        XGI_UnLockCRT2(HwDeviceExtension, pVBInfo);
524
525    /* Jong 10/03/2007 */
526    HwDeviceExtension->SpecialMode = FALSE;
527
528/* Jong 11/27/2007 */
529#if 0 /* can't get pScrn */
530#if (defined(i386) || defined(__i386) || defined(__i386__) || defined(__AMD64__))
531    vga_info = XGI_GetSetBIOSScratch(pScrn, 0x489, 0xff);
532#else
533    vga_info = 0x11; /* set default mode 3 */
534#endif
535#endif
536
537	PDEBUG(ErrorF("XGISetModeNew()...2\n"));
538
539    if ( (!XGI_SearchModeID(pVBInfo->SModeIDTable, pVBInfo->EModeIDTable,  0x11, &ModeNo , &ModeIdIndex)) ||
540		 (HwDeviceExtension->SpecifyTiming) )
541    {
542        switch (HwDeviceExtension->BPP)
543        {
544            case  8: ModeNo = 0x2E;
545                     HwDeviceExtension->SpecialMode = TRUE;
546                     break;
547            case 15: ModeNo = 0x43;
548                     HwDeviceExtension->SpecialMode = TRUE;
549                     break;
550            case 16: ModeNo = 0x44;
551                     HwDeviceExtension->SpecialMode = TRUE;
552                     break;
553            case 32: ModeNo = 0x62;
554                     HwDeviceExtension->SpecialMode = TRUE;
555                     break;
556            default:
557                     return FALSE;
558                     break;
559        }
560
561        /* Jong 10/03/2007 */
562        if (HwDeviceExtension->SpecialMode)
563        {
564
565		  /* Jong 11/28/2007; pVBInfo field is not matching VGAINFO argument */
566          /* XGI_SearchModeID( pVBInfo->SModeIDTable, pVBInfo->EModeIDTable, pVBInfo, &ModeNo , &ModeIdIndex ) ; */
567          XGI_SearchModeID( pVBInfo->SModeIDTable, pVBInfo->EModeIDTable, 0x11, &ModeNo , &ModeIdIndex ) ;
568          if ( !(HwDeviceExtension->SpecifyTiming) )
569          {
570            int i = 0;
571            while ( SpecialModeTiming[i].Horizontal_ACTIVE != 0 )
572            {
573              if ( ( SpecialModeTiming[i].Horizontal_ACTIVE==HwDeviceExtension->Horizontal_ACTIVE ) &&
574                   ( (SpecialModeTiming[i].Vertical_ACTIVE<<(SpecialModeTiming[i].Interlace&0x1))==HwDeviceExtension->Vertical_ACTIVE ) )
575              {
576                  if ( ( ( SpecialModeTiming[i].FrameRate-HwDeviceExtension->Frequency ) < 2.0 ) ||
577                       ( ( SpecialModeTiming[i].FrameRate-HwDeviceExtension->Frequency ) > -2.0 ) )
578                  {
579                    HwDeviceExtension->Horizontal_FP = SpecialModeTiming[i].Horizontal_FP;
580                    HwDeviceExtension->Horizontal_SYNC = SpecialModeTiming[i].Horizontal_SYNC;
581                    HwDeviceExtension->Horizontal_BP = SpecialModeTiming[i].Horizontal_BP;
582                    HwDeviceExtension->Vertical_FP = SpecialModeTiming[i].Vertical_FP;
583                    HwDeviceExtension->Vertical_SYNC = SpecialModeTiming[i].Vertical_SYNC;
584                    HwDeviceExtension->Vertical_BP = SpecialModeTiming[i].Vertical_BP;
585                    HwDeviceExtension->DCLK = SpecialModeTiming[i].DCLK;
586                    HwDeviceExtension->Interlace = SpecialModeTiming[i].Interlace & 0x1;
587                    break;
588                  }
589              }
590              i++;
591            }
592            if ( SpecialModeTiming[i].Horizontal_ACTIVE == 0 )
593            {
594                return FALSE;  /* currently not support */
595            }
596          }
597        }
598    }
599
600	PDEBUG(ErrorF("XGISetModeNew()...3\n"));
601
602    if (HwDeviceExtension->jChipType < XG20) { /* kuku 2004/06/25 */
603        PDEBUG(ErrorF("XGI_GetVBInfo \n"));
604        XGI_GetVBInfo(ModeNo, ModeIdIndex, HwDeviceExtension, pVBInfo);
605        PDEBUG(ErrorF("XGI_GetTVInfo \n"));
606        XGI_GetTVInfo(ModeNo, ModeIdIndex, pVBInfo);
607        PDEBUG(ErrorF("XGI_GetLCDInfo \n"));
608        XGI_GetLCDInfo(ModeNo, ModeIdIndex, pVBInfo);
609        PDEBUG(ErrorF("XGI_DisableBridge \n"));
610
611        /* Jong 10/17/2007; merge code */
612        if ( pVBInfo->VBInfo & ( SetSimuScanMode | SwitchToCRT2 ) )
613        {
614            if (HwDeviceExtension->SpecialMode)
615            {
616                return FALSE;
617            }
618        }
619
620        XGI_DisableBridge(HwDeviceExtension, pVBInfo);
621
622
623        if (pVBInfo->VBInfo & (SetSimuScanMode | SetCRT2ToLCDA)) {
624            XGI_SetCRT1Group(HwDeviceExtension, ModeNo, ModeIdIndex, pVBInfo);
625
626            if (pVBInfo->VBInfo & SetCRT2ToLCDA) {
627                XGI_SetLCDAGroup(ModeNo, ModeIdIndex, HwDeviceExtension,
628                                 pVBInfo);
629            }
630        }
631        else {
632            if (!(pVBInfo->VBInfo & SwitchToCRT2)) {
633                XGI_SetCRT1Group(HwDeviceExtension, ModeNo, ModeIdIndex,
634                                 pVBInfo);
635                if (pVBInfo->VBInfo & SetCRT2ToLCDA) {
636                    XGI_SetLCDAGroup(ModeNo, ModeIdIndex, HwDeviceExtension,
637                                     pVBInfo);
638                }
639            }
640        }
641
642        PDEBUG(ErrorF(" vb_setmode 474\n"));    // yilin
643        if (pVBInfo->VBInfo & (SetSimuScanMode | SwitchToCRT2)) {
644            switch (HwDeviceExtension->ujVBChipID) {
645            case VB_CHIP_301:
646                PDEBUG(ErrorF(" vb_setmode 301\n"));    //yilin
647                XGI_SetCRT2Group301(ModeNo, HwDeviceExtension, pVBInfo);        /*add for CRT2 */
648                break;
649
650            case VB_CHIP_302:
651                XGI_SetCRT2Group301(ModeNo, HwDeviceExtension, pVBInfo);        /*add for CRT2 */
652                break;
653
654            default:
655                break;
656            }
657        }
658        ErrorF("492 Part2 0 = %x ",
659               XGI_GetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x0));
660        XGI_SetCRT2ModeRegs(ModeNo, HwDeviceExtension, pVBInfo);
661        XGI_OEM310Setting(ModeNo, ModeIdIndex, pVBInfo);        /*0212 */
662        XGI_EnableBridge(HwDeviceExtension, pVBInfo);
663        ErrorF("497 Part2 0 = %x ",
664               XGI_GetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x0));
665    }                           /* !XG20 */
666    else
667    {
668		PDEBUG(ErrorF("XGISetModeNew()...4\n"));
669
670        /* Jong 10/04/2007 */
671        if ( pVBInfo->IF_DEF_LVDS == 1 )
672        {
673            if ( !XGI_XG21CheckLVDSMode(ModeNo , ModeIdIndex, pVBInfo) )
674            {
675              return FALSE;
676            }
677        }
678
679		PDEBUG(ErrorF("XGISetModeNew()...5\n"));
680
681        if (ModeNo <= 0x13) {
682            pVBInfo->ModeType =
683                pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag & ModeInfoFlag;
684        }
685        else {
686            pVBInfo->ModeType =
687                pVBInfo->EModeIDTable[ModeIdIndex].
688                Ext_ModeFlag & ModeInfoFlag;
689        }
690
691        pVBInfo->SetFlag = 0;
692        if ( pVBInfo->IF_DEF_CH7007 != 1 )
693        {
694            pVBInfo->VBInfo = DisableCRT2Display;
695        }
696
697		PDEBUG(ErrorF("XGISetModeNew()...6\n"));
698
699        XGI_DisplayOff(HwDeviceExtension,pVBInfo);
700		PDEBUG(ErrorF("XGISetModeNew()...7\n"));
701
702        XGI_SetCRT1Group(HwDeviceExtension, ModeNo, ModeIdIndex, pVBInfo);
703
704		PDEBUG(ErrorF("XGISetModeNew()...8\n"));
705
706        XGI_DisplayOn(HwDeviceExtension, pVBInfo);
707
708		PDEBUG(ErrorF("XGISetModeNew()...9\n"));
709    }
710
711/*
712    if ( ModeNo <= 0x13 )
713    {
714        modeflag = pVBInfo->SModeIDTable[ ModeIdIndex ].St_ModeFlag ;
715    }
716    else
717    {
718        modeflag = pVBInfo->EModeIDTable[ ModeIdIndex ].Ext_ModeFlag ;
719    }
720    pVBInfo->ModeType = modeflag&ModeInfoFlag ;
721    pVBInfo->SetFlag = 0x00 ;
722    pVBInfo->VBInfo = DisableCRT2Display ;
723    temp = XGINew_CheckMemorySize(  HwDeviceExtension , ModeNo , ModeIdIndex, pVBInfo ) ;
724
725    if ( temp == 0 )
726        return( 0 ) ;
727
728    XGI_DisplayOff( HwDeviceExtension,pVBInfo) ;
729    XGI_SetCRT1Group( HwDeviceExtension , ModeNo , ModeIdIndex, pVBInfo ) ;
730    XGI_DisplayOn( HwDeviceExtension, pVBInfo) ;
731*/
732    ErrorF("Part2 0 = %x ",
733           XGI_GetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x0));
734    XGI_UpdateModeInfo(HwDeviceExtension, pVBInfo);
735
736    /* Jong 10/04/2007 */
737    if (HwDeviceExtension->jChipType < XG20)   /* kuku 2004/06/25 */
738        XGI_LockCRT2(HwDeviceExtension, pVBInfo);
739
740	PDEBUG(ErrorF("XGISetModeNew()...End\n"));
741
742    return (TRUE);
743}
744
745#if 1
746void XGI_SetCRTVCLK(PVB_DEVICE_INFO pVBInfo, double dwPixelClock)
747{
748struct factor
749{
750  int sr2b7;
751  int sr2c7;
752  int sr2c6;
753  int sr2c5;
754  int dividend ;
755  int divisor ;
756};
757
758struct factor kind[16]=
759{
760  {0,0,0,0,1,1},
761  {0,0,0,1,1,2},
762  {0,0,1,0,1,3},
763  {0,0,1,1,1,4},
764  {1,0,0,0,2,1},
765  {1,0,0,1,2,2},
766  {1,0,1,0,2,3},
767  {1,0,1,1,2,4},
768  {0,1,0,0,1,1},
769  {0,1,0,1,1,4},
770  {0,1,1,0,1,6},
771  {0,1,1,1,1,8},
772  {1,1,0,0,2,1},
773  {1,1,0,1,2,4},
774  {1,1,1,0,2,6},
775  {1,1,1,1,2,8}
776};
777  int ii,jj,kk,ll,sr2b,sr2c,Numerator,DeNumerator;
778  double factor1,tempclock,vclk,temp1,min,clock;
779  double min_tempclock=150.0;
780
781  /* Alan 12/14/2007; support LVDS */
782  USHORT XGINew_P3cc =  pVBInfo->P3cc;
783  UCHAR  b3CC;
784
785  vclk=(double)dwPixelClock;
786  min=99.0;
787  while (min_tempclock>0.0)
788  {
789      for(ii=2;ii<=31;ii++)  /* (DeNumerator1)It's value must >=2 */
790      {
791          for(jj=0;jj<=127;jj++) /* (Numerator1) */
792          {
793              for(kk=0;kk<=15;kk++)
794              {
795                  tempclock=14.318*kind[kk].dividend*(jj+1)/(ii+1);
796                  if ( (tempclock >= min_tempclock ) && ( tempclock <= 380) )
797                  {
798                      tempclock = tempclock / kind[kk].divisor ;
799                      temp1=fabs(vclk-tempclock);
800                      if(temp1<min)
801                      {
802                          clock=tempclock;
803                          DeNumerator=ii;
804                          Numerator=jj;
805                          min=temp1;
806                          factor1=(double) (kind[kk].dividend / kind[kk].divisor);
807                          ll=kk;
808                      }
809                  }
810              }
811          }
812      }
813
814      if ((min/vclk)<0.01)
815      {
816          break;
817      }
818      else
819      {
820          min_tempclock -= 50.0;
821      }
822  }
823
824  sr2b=128*kind[ll].sr2b7+Numerator;
825  sr2c=128*kind[ll].sr2c7+64*kind[ll].sr2c6+32*kind[ll].sr2c5+DeNumerator;
826
827  if(pVBInfo->IF_DEF_LVDS == 1)
828  {
829	  b3CC = (UCHAR)XGI_GetRegByte((XGIIOADDRESS) XGINew_P3cc) ;
830	  switch (b3CC & 0x0c )
831	  {
832		  case 0x0 : XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x31, 0xCF, 0x10);
833					 break;
834		  case 0x4 : XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x31, 0xCF, 0x20);
835					 break;
836		  default  : XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x31, 0xCF, 0x00);
837					 break;
838	  }
839  }
840
841  XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4 , 0x2B , (unsigned char) sr2b) ;
842  XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4 , 0x2C , (unsigned char) sr2c) ;
843}
844#else
845/* Jong 10/17/2007; merge code */
846void XGI_SetCRTVCLK(PVB_DEVICE_INFO pVBInfo, double dwPixelClock)
847{
848struct factor
849{
850  int sr2b7;
851  int sr2c7;
852  int sr2c6;
853  int sr2c5;
854  int dividend ;
855  int divisor ;
856};
857
858struct factor kind[16]=
859{
860  {0,0,0,0,1,1},
861  {0,0,0,1,1,2},
862  {0,0,1,0,1,3},
863  {0,0,1,1,1,4},
864  {1,0,0,0,2,1},
865  {1,0,0,1,2,2},
866  {1,0,1,0,2,3},
867  {1,0,1,1,2,4},
868  {0,1,0,0,1,1},
869  {0,1,0,1,1,4},
870  {0,1,1,0,1,6},
871  {0,1,1,1,1,8},
872  {1,1,0,0,2,1},
873  {1,1,0,1,2,4},
874  {1,1,1,0,2,6},
875  {1,1,1,1,2,8}
876};
877  int ii,jj,kk,ll,sr2b,sr2c,Numerator,DeNumerator;
878  double factor1,tempclock,vclk,temp1,min,clock;
879
880
881  vclk=(double)dwPixelClock;
882  min=99.0;
883  for(ii=2;ii<=31;ii++)  /* (DeNumerator1)It's value must >=2 */
884  {
885      for(jj=0;jj<=127;jj++) /* (Numerator1) */
886      {
887
888          for(kk=0;kk<=15;kk++)
889          {
890              tempclock=14.318*kind[kk].dividend*(jj+1)/(ii+1);
891              if ( (tempclock >= 150 ) && ( tempclock <= 380) )
892              {
893                  tempclock = tempclock / kind[kk].divisor ;
894                  temp1=fabs(vclk-tempclock);
895                  if(temp1<min)
896                  {
897                      clock=tempclock;
898                      DeNumerator=ii;
899                      Numerator=jj;
900                      min=temp1;
901                      factor1=(double) (kind[kk].dividend / kind[kk].divisor);
902                      ll=kk;
903                  }
904              }
905          }
906      }
907  }
908  sr2b=128*kind[ll].sr2b7+Numerator;
909  sr2c=128*kind[ll].sr2c7+64*kind[ll].sr2c6+32*kind[ll].sr2c5+DeNumerator;
910
911  XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x2B , (unsigned char) sr2b) ;
912  XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x2C , (unsigned char) sr2c) ;
913}
914#endif
915
916/* Jong 10/17/2007; merge code */
917void XGI_SetCRTTiming(
918                PXGI_HW_DEVICE_INFO pXGIHWDE,
919                PVB_DEVICE_INFO pVBInfo
920               )
921{
922  int HT, VT, HDE, VDE, HRS, VRS, HRE, VRE, VGAHDE, VGAVDE, VGAHT, VGAVT;
923  int HorizontalActivePixel, HorizontalFrontPorch, HorizontalSyncWidth, HorizontalBackPorch;
924  int VerticalActivePixel, VerticalFrontPorch, VerticalSyncWidth, VerticalBackPorch;
925  int temp1;
926  UCHAR temp;
927
928  HorizontalActivePixel = pXGIHWDE->Horizontal_ACTIVE;
929  HorizontalFrontPorch = pXGIHWDE->Horizontal_FP;
930  HorizontalSyncWidth = pXGIHWDE->Horizontal_SYNC;
931  HorizontalBackPorch = pXGIHWDE->Horizontal_BP;
932  VerticalActivePixel = pXGIHWDE->Vertical_ACTIVE >> (pXGIHWDE->Interlace & 0x1);
933  VerticalFrontPorch = pXGIHWDE->Vertical_FP;
934  VerticalSyncWidth = pXGIHWDE->Vertical_SYNC;
935  VerticalBackPorch = pXGIHWDE->Vertical_BP;
936
937  PDEBUG(ErrorF("XGI_SetCRTTiming()...\n"));
938  PDEBUG(ErrorF("HorizontalActivePixel = %d...\n", HorizontalActivePixel));
939  PDEBUG(ErrorF("HorizontalFrontPorch = %d...\n", HorizontalFrontPorch));
940  PDEBUG(ErrorF("HorizontalSyncWidth = %d...\n", HorizontalSyncWidth));
941  PDEBUG(ErrorF("HorizontalBackPorch = %d...\n", HorizontalBackPorch));
942  PDEBUG(ErrorF("VerticalActivePixel = %d...\n", VerticalActivePixel));
943  PDEBUG(ErrorF("VerticalFrontPorch = %d...\n", VerticalFrontPorch));
944  PDEBUG(ErrorF("VerticalSyncWidth = %d...\n", VerticalSyncWidth));
945  PDEBUG(ErrorF("VerticalBackPorch = %d...\n", VerticalBackPorch));
946
947  HT  = HorizontalActivePixel + HorizontalFrontPorch + HorizontalSyncWidth + HorizontalBackPorch;
948  HDE = HorizontalActivePixel;
949  HRS = HorizontalActivePixel + HorizontalFrontPorch;
950  HRE = HorizontalActivePixel + HorizontalFrontPorch + HorizontalSyncWidth;
951  HT  = HT  / 8;
952  HDE = HDE / 8;
953  HRS = HRS / 8;
954  HRE = HRE / 8;
955  VGAHT = HT - 5;
956  VGAHDE = HDE - 1;
957  HDE = HDE - 1;
958  HT = HT - 1;
959  HRS = HRS + 3;
960  HRE = HRE + 3;
961
962  /*
963  HRS = HRS + 2;
964  HRE = HRE + 2;
965  */
966
967  VT  = VerticalActivePixel + VerticalFrontPorch + VerticalSyncWidth + VerticalBackPorch;
968  VDE = VerticalActivePixel;
969  VRS = VerticalActivePixel + VerticalFrontPorch;
970  VRE = VerticalActivePixel + VerticalFrontPorch + VerticalSyncWidth;
971  VGAVT = VT - 2;
972  VGAVDE = VDE - 1;
973  VRS = VRS - 1;
974  VRE = VRE - 1;
975  VDE = VDE - 1;
976  VT = VT - 1;
977
978
979  temp = XGI_GetReg( pVBInfo->P3c4 , 0x06 ) ;
980
981  temp = ((temp & 0x1c ) >> 2) * 8;
982  if (temp == 0)
983    temp = 8;
984  temp1 = HorizontalActivePixel * temp / 8;
985  temp1 = temp1 / 8;
986  temp = temp1 / 8 + 1;
987
988  if (pXGIHWDE->Interlace)
989  {
990    temp1 = temp1 << 1;
991  }
992
993  XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x05, 0x00, 0x86);
994  XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x11, 0x7f, 0x00);
995  XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x00, 0x00, (VGAHT & 0xff));    /* HT */
996  XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x01, 0x00, (VGAHDE & 0xff));   /* HDEE */
997  XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x02, 0x00, (HDE & 0xff));      /* HBS */
998  XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x03, 0xe0, (HT & 0x1f));       /* HBE */
999  XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x04, 0x00, (HRS & 0xff));      /* HRS */
1000  XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x05, 0x60, (((HT & 0x20) << 2) | (HRE & 0x1f)));   /* HRE */
1001  XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x06, 0x00, (VGAVT & 0xff));    /* VT */
1002  XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x07, 0x00, (((VRS & 0x0200) >> 2) | ((VDE & 0x0200) >> 3) | ((VGAVT & 0x0200) >> 4)| ((VGAVDE & 0x0100) >> 5) | ((VRS & 0x0100) >> 6) | ((VDE & 0x0100) >> 7) | ((VGAVT & 0x0100) >> 8)));
1003  XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x09, 0xdf, ((VGAVDE & 0x0200) >> 4));
1004  XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x10, 0x00, (VRS & 0xff));      /* VRS */
1005  XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x11, 0xf0, (VRE & 0x0f));      /* VRE */
1006  XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x12, 0x00, (VDE & 0xff));      /* VDEE */
1007  XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x13, 0x00, (temp1 & 0xff));
1008  XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x15, 0x00, (VGAVDE & 0xff));   /* VBS */
1009  XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x16, 0x00, (VT & 0xff));       /* VBE */
1010
1011  if ( pXGIHWDE->jChipType == XG21 )
1012  {
1013      XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x2e, 0x00, ((HRS-1) & 0xff));      /* HRS */
1014      XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x2f, 0x00, (((HRE-1) & 0x3f)<<2) | (((HRS-1) & 0x0300) >> 8));      /* HRS */
1015      XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x33, 0xFE, (((VRS) & 0x01)));      /* VRS */
1016      XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x34, 0x00, (((VRS) & 0x01FE)>>1));      /* VRS */
1017      XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x3F, 0x00, (((VRS) & 0x0600)>>9) | (((VRE) & 0x003F)<<2 ));      /* VRS */
1018
1019  }
1020
1021  if ( pXGIHWDE->jChipType == XG27 )
1022  {
1023      XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x2e, 0x00, ((HRS-1) & 0xff));      /* HRS SR2E[7:0] */
1024      XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x2f, 0x00, (((HRE-1) & 0x3f)<<2) | (((HRS-1) & 0x0300) >> 8));      /* HRE SR2F[7:2] HRS SR2F[1:0] */
1025      XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x34, 0x00, ((VRS) & 0x0FF) );      /* VRS SR34[7:0] */
1026      XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x35, 0xF8, (((VRS) & 0x0700)>>8));      /* VRS SR35[2:0] */
1027      XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x3F, 0xFC, (((VRE) & 0x003F)<<2 ));     /* VRE SR3F[7:2] */
1028
1029  }
1030
1031  if (VerticalActivePixel > 1024)
1032  {
1033    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x0f, 0xf7, 0x08);
1034  }
1035  else
1036  {
1037    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x07, 0xef, ((VGAVDE & 0x0100) >> 4));
1038    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x09, 0xbf, ((VGAVDE & 0x0200) >> 3));
1039    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x18, 0x00, (VGAVDE & 0x0ff));
1040  }
1041
1042  XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x11, 0xff, 0x80);
1043  XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x0a, 0xc0, (((VRE & 0x10) << 1) | ((VT & 0x0100) >> 4) | ((VRS & 0x0400) >> 7) | ((VGAVDE & 0x0400) >> 8) | ((VDE & 0x0400) >> 9) | ((VGAVT & 0x0400) >> 10)));
1044  XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x0b, 0x00, (((VGAHT & 0xff00) >> 8) | ((VGAHDE & 0xff00) >> 6) | ((HDE & 0xff00) >> 4) | ((HRS & 0xff00) >> 2)));
1045  XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x0c, 0xf8, (((HRE & 0x20) >> 3) | ((HT & 0xc0) >> 6)));
1046  XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x0e, 0xf0, ((temp1 & 0xff00) >> 8));
1047  XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x10, 0x00, temp);
1048
1049  XGI_SetCRTVCLK (pVBInfo, pXGIHWDE->DCLK/1000.0);
1050
1051  if (pXGIHWDE->BPP==0x20)
1052  {
1053     XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x14, 0xE0, 0x0f);
1054     XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3ce, 0x05, 0xBF, 0x0);
1055     XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x06, 0xE3, 0x10);
1056     temp = ((pXGIHWDE->Horizontal_ACTIVE / 8 * pXGIHWDE->BPP) / 64) + 1;
1057     XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x10, temp);
1058  }
1059  if (pXGIHWDE->BPP==0x10)
1060  {
1061     XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x14, 0xE0, 0x0f);
1062     XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3ce, 0x05, 0xBF, 0x0);
1063     XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x06, 0xE3, 0x08);
1064     temp = ((pXGIHWDE->Horizontal_ACTIVE / 8 * pXGIHWDE->BPP) / 64) + 1;
1065     XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x10, temp);
1066  }
1067  if (pXGIHWDE->BPP==0x8)
1068  {
1069     XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x06, 0xE3, 0x00);
1070     temp = ((pXGIHWDE->Horizontal_ACTIVE / 8 * pXGIHWDE->BPP) / 64) + 1;
1071     XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x10, temp);
1072  }
1073
1074}
1075
1076/* --------------------------------------------------------------------- */
1077/* Function : XGI_SetCRT1Group */
1078/* Input : */
1079/* Output : */
1080/* Description : */
1081/* --------------------------------------------------------------------- */
1082void
1083XGI_SetCRT1Group(PXGI_HW_DEVICE_INFO HwDeviceExtension, USHORT ModeNo,
1084                 USHORT ModeIdIndex, PVB_DEVICE_INFO pVBInfo)
1085{
1086    const USHORT StandTableIndex = XGI_GetModePtr(pVBInfo->SModeIDTable,
1087                                                  pVBInfo->ModeType,
1088                                                  ModeNo, ModeIdIndex);
1089    USHORT RefreshRateTableIndex;
1090    USHORT b3CC;
1091    USHORT temp;
1092
1093    USHORT XGINew_P3cc = pVBInfo->P3cc;
1094#ifndef LINUX_XF86
1095    USHORT XGINew_P3c2 = pVBInfo->P3c2;
1096#endif
1097
1098	PDEBUG(ErrorF("XGI_SetCRT1Group()...begin\n"));
1099
1100    /* XGINew_CRT1Mode = ModeNo ; // SaveModeID */
1101    XGI_SetSeqRegs(StandTableIndex, pVBInfo);
1102    XGI_SetMiscRegs(StandTableIndex, pVBInfo);
1103    XGI_SetCRTCRegs(StandTableIndex, pVBInfo);
1104    XGI_SetATTRegs(ModeNo, StandTableIndex, ModeIdIndex, pVBInfo);
1105    XGI_SetGRCRegs(StandTableIndex, pVBInfo);
1106    XGI_ClearExt1Regs(ModeNo, pVBInfo);
1107
1108	PDEBUG(ErrorF("XGI_SetCRT1Group()...1\n"));
1109
1110	/* Jong 10/19/2007; merge code */
1111	/* Jong 04/23/2008; All XG20,21,27 should do this */
1112    /* if ( HwDeviceExtension->jChipType == XG27 ) */
1113    if ( HwDeviceExtension->jChipType >= XG20 )
1114    {
1115      if ( pVBInfo->IF_DEF_LVDS == 0 )
1116      {
1117        XGI_SetDefaultVCLK( pVBInfo ) ;
1118      }
1119    }
1120
1121    temp = ~ProgrammingCRT2;
1122    pVBInfo->SetFlag &= temp;
1123    pVBInfo->SelectCRT2Rate = 0;
1124
1125	PDEBUG(ErrorF("XGI_SetCRT1Group()...2\n"));
1126
1127    if (pVBInfo->
1128        VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV |
1129                  VB_XGI301C)) {
1130        if (pVBInfo->
1131            VBInfo & (SetSimuScanMode | SetCRT2ToLCDA | SetInSlaveMode)) {
1132            pVBInfo->SetFlag |= ProgrammingCRT2;
1133        }
1134    }
1135
1136	/* Jong 10/05/2007; merge code */
1137    /* RefreshRateTableIndex = XGI_GetRatePtrCRT2( ModeNo, ModeIdIndex, pVBInfo); */
1138    RefreshRateTableIndex = XGI_GetRatePtrCRT2(HwDeviceExtension, ModeNo, ModeIdIndex, pVBInfo);
1139
1140	PDEBUG(ErrorF("XGI_SetCRT1Group()...3\n"));
1141
1142    if (RefreshRateTableIndex != 0xFFFF) {
1143        XGI_SetSync(RefreshRateTableIndex, pVBInfo);
1144        XGI_SetCRT1CRTC(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo,
1145                        HwDeviceExtension);
1146        XGI_SetCRT1DE(HwDeviceExtension, ModeNo, ModeIdIndex,
1147                      RefreshRateTableIndex, pVBInfo);
1148        XGI_SetCRT1Offset(ModeNo, ModeIdIndex, RefreshRateTableIndex,
1149                          HwDeviceExtension, pVBInfo);
1150        XGI_SetCRT1VCLK(ModeNo, ModeIdIndex, HwDeviceExtension,
1151                        RefreshRateTableIndex, pVBInfo);
1152    }
1153
1154	PDEBUG(ErrorF("XGI_SetCRT1Group()...4\n"));
1155
1156    /* Jong 10/04/2007; merge code */
1157    /* if (HwDeviceExtension->jChipType == XG20) { */
1158    if ( ( HwDeviceExtension->jChipType >= XG20 ) &&
1159         ( HwDeviceExtension->jChipType < XG27 ) ) /* fix H/W DCLK/2 bug */
1160    {
1161        if ((ModeNo == 0x00) | (ModeNo == 0x01)) {
1162            XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x2B, 0x4E);
1163            XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x2C, 0xE9);
1164            b3CC = (UCHAR) XGI_GetRegByte((XGIIOADDRESS) XGINew_P3cc);
1165            XGI_SetRegByte((XGIIOADDRESS) XGINew_P3cc, (b3CC |= 0x0C));
1166        }
1167        else if ((ModeNo == 0x04) | (ModeNo == 0x05) | (ModeNo == 0x0D)) {
1168            XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x2B, 0x1B);
1169            XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x2C, 0xE3);
1170            b3CC = (UCHAR) XGI_GetRegByte((XGIIOADDRESS) XGINew_P3cc);
1171            XGI_SetRegByte((XGIIOADDRESS) XGINew_P3cc, (b3CC |= 0x0C));
1172        }
1173    }
1174
1175    /* Jong 10/04/2007; merge code */
1176    if ( HwDeviceExtension->jChipType >= XG21 )
1177    {
1178	  PDEBUG(ErrorF("XGI_SetCRT1Group()...4-1\n"));
1179
1180      temp = XGI_GetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x38 ) ;
1181      if ( temp & 0xA0 )
1182      {
1183		PDEBUG(ErrorF("XGI_SetCRT1Group()...4-2\n"));
1184
1185        /*XGINew_SetRegAND( pVBInfo->P3d4 , 0x4A , ~0x20 ) ;*/	/* Enable write GPIOF */
1186        /*XGINew_SetRegAND( pVBInfo->P3d4 , 0x48 , ~0x20 ) ;*/ 	/* P. DWN */
1187        /* XG21 CRT1 Timing */
1188        if ( HwDeviceExtension->jChipType == XG27 )
1189			XGI_SetXG27CRTC( ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo );
1190        else
1191			XGI_SetXG21CRTC( ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo );
1192
1193		PDEBUG(ErrorF("XGI_SetCRT1Group()...4-3\n"));
1194
1195        XGI_UpdateXG21CRTC( ModeNo , pVBInfo , RefreshRateTableIndex) ;
1196
1197        if ( HwDeviceExtension->jChipType == XG27 )
1198          XGI_SetXG27LCD( pVBInfo , RefreshRateTableIndex , ModeNo );
1199        else
1200          XGI_SetXG21LCD( pVBInfo , RefreshRateTableIndex , ModeNo );
1201
1202		PDEBUG(ErrorF("XGI_SetCRT1Group()...4-4\n"));
1203
1204        if ( pVBInfo->IF_DEF_LVDS == 1 )
1205        {
1206          if ( HwDeviceExtension->jChipType == XG27 )
1207            XGI_SetXG27LVDSPara(ModeNo,ModeIdIndex, pVBInfo );
1208          else
1209            XGI_SetXG21LVDSPara(ModeNo,ModeIdIndex, pVBInfo );
1210        }
1211
1212		PDEBUG(ErrorF("XGI_SetCRT1Group()...4-5\n"));
1213        /*XGINew_SetRegOR( pVBInfo->P3d4 , 0x48 , 0x20 ) ;*/	/* P. ON */
1214      }
1215    }
1216
1217    pVBInfo->SetFlag &= (~ProgrammingCRT2);
1218    XGI_SetCRT1FIFO(ModeNo, HwDeviceExtension, pVBInfo);
1219    XGI_SetCRT1ModeRegs(HwDeviceExtension, ModeNo, ModeIdIndex,
1220                        RefreshRateTableIndex, pVBInfo);
1221
1222    if ( HwDeviceExtension->SpecialMode )
1223    {
1224        XGI_SetCRTTiming( HwDeviceExtension, pVBInfo );
1225    }
1226
1227	PDEBUG(ErrorF("XGI_SetCRT1Group()...5\n"));
1228
1229    if (HwDeviceExtension->jChipType == XG40) { /* Copy reg settings to 2nd chip */
1230        if (CheckDualChip(pVBInfo))
1231            SetDualChipRegs(HwDeviceExtension, pVBInfo);
1232    }
1233
1234	PDEBUG(ErrorF("XGI_SetCRT1Group()...6\n"));
1235
1236    /* XGI_LoadCharacter(); //dif ifdef TVFont */
1237
1238    XGI_LoadDAC(ModeNo, ModeIdIndex, pVBInfo);
1239	PDEBUG(ErrorF("XGI_SetCRT1Group()...End\n"));
1240}
1241
1242
1243/* --------------------------------------------------------------------- */
1244/* Function : XGI_SetSeqRegs */
1245/* Input : */
1246/* Output : */
1247/* Description : */
1248/* --------------------------------------------------------------------- */
1249void
1250XGI_SetSeqRegs(USHORT StandTableIndex, const VB_DEVICE_INFO *pVBInfo)
1251{
1252    unsigned SRdata;
1253    unsigned i;
1254
1255    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x00, 0x03);       /* Set SR0 */
1256    SRdata = pVBInfo->StandTable[StandTableIndex].SR[0];
1257
1258    if (pVBInfo->VBInfo & SetCRT2ToLCDA) {
1259        SRdata |= 0x01;
1260    }
1261    else {
1262        if (pVBInfo->VBInfo & (SetCRT2ToTV | SetCRT2ToLCD)) {
1263            if (pVBInfo->VBInfo & SetInSlaveMode)
1264                SRdata |= 0x01;
1265        }
1266    }
1267
1268    SRdata |= 0x20;             /* screen off */
1269    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x01, SRdata);     /* Set SR1 */
1270
1271    /* Get SR2, SR3, and SR4 from table and set in hardware.
1272     */
1273    for (i = 2; i <= 4; i++) {
1274        SRdata = pVBInfo->StandTable[StandTableIndex].SR[i - 1];
1275        XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, i, SRdata);
1276    }
1277}
1278
1279
1280/* --------------------------------------------------------------------- */
1281/* Function : XGI_SetMiscRegs */
1282/* Input : */
1283/* Output : */
1284/* Description : */
1285/* --------------------------------------------------------------------- */
1286void
1287XGI_SetMiscRegs(USHORT StandTableIndex, const VB_DEVICE_INFO *pVBInfo)
1288{
1289    UCHAR Miscdata;
1290
1291    Miscdata = pVBInfo->StandTable[StandTableIndex].MISC;       /* Get Misc from file */
1292/*
1293    if ( pVBInfo->VBType & ( VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV | VB_XGI301C ) )
1294    {
1295        if ( pVBInfo->VBInfo & SetCRT2ToLCDA )
1296        {
1297            Miscdata |= 0x0C ;
1298        }
1299    }
1300*/
1301
1302    XGI_SetRegByte((XGIIOADDRESS) pVBInfo->P3c2, Miscdata);     /* Set Misc(3c2) */
1303}
1304
1305
1306/* --------------------------------------------------------------------- */
1307/* Function : XGI_SetCRTCRegs */
1308/* Input : */
1309/* Output : */
1310/* Description : */
1311/* --------------------------------------------------------------------- */
1312void
1313XGI_SetCRTCRegs(unsigned StandTableIndex, const VB_DEVICE_INFO *pVBInfo)
1314{
1315    unsigned i;
1316
1317    /* Unlock CRTC */
1318    XGI_SetRegAND((XGIIOADDRESS) pVBInfo->P3d4, 0x11, 0x7f);
1319
1320    for (i = 0; i <= 0x18; i++) {
1321        /* Get CRTC from file */
1322        const unsigned CRTCdata =
1323            pVBInfo->StandTable[StandTableIndex].CRTC[i];
1324
1325        /* Set CRTC( 3d4 ) */
1326        XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, i, CRTCdata);
1327    }
1328}
1329
1330
1331/* --------------------------------------------------------------------- */
1332/* Function : */
1333/* Input : */
1334/* Output : */
1335/* Description : */
1336/* --------------------------------------------------------------------- */
1337void
1338XGI_SetATTRegs(unsigned ModeNo, unsigned StandTableIndex, unsigned ModeIdIndex,
1339               const VB_DEVICE_INFO *pVBInfo)
1340{
1341    unsigned i;
1342    const unsigned modeflag = (ModeNo <= 0x13)
1343        ? pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag
1344        : pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
1345
1346    for (i = 0; i <= 0x13; i++) {
1347        UCHAR ARdata = pVBInfo->StandTable[StandTableIndex].ATTR[i];
1348
1349        if (modeflag & Charx8Dot) {     /* ifndef Dot9 */
1350            if (i == 0x13) {
1351                /* Pixel shift. If screen on LCD or TV is shifted left or
1352                 * right, this might be the cause.
1353                 */
1354                if (pVBInfo->VBInfo & SetCRT2ToLCDA)
1355                    ARdata = 0;
1356                else {
1357                    if (pVBInfo->VBInfo & (SetCRT2ToTV | SetCRT2ToLCD)) {
1358                        if (pVBInfo->VBInfo & SetInSlaveMode)
1359                            ARdata = 0;
1360                    }
1361                }
1362            }
1363        }
1364
1365        XGI_GetRegByte((XGIIOADDRESS) pVBInfo->P3da);   /* reset 3da */
1366        XGI_SetRegByte((XGIIOADDRESS) pVBInfo->P3c0, i);        /* set index */
1367        XGI_SetRegByte((XGIIOADDRESS) pVBInfo->P3c0, ARdata);   /* set data */
1368    }
1369
1370    XGI_GetRegByte((XGIIOADDRESS) pVBInfo->P3da);       /* reset 3da */
1371    XGI_SetRegByte((XGIIOADDRESS) pVBInfo->P3c0, 0x14); /* set index */
1372    XGI_SetRegByte((XGIIOADDRESS) pVBInfo->P3c0, 0x00); /* set data */
1373
1374    XGI_GetRegByte((XGIIOADDRESS) pVBInfo->P3da);       /* Enable Attribute */
1375    XGI_SetRegByte((XGIIOADDRESS) pVBInfo->P3c0, 0x20);
1376    XGI_GetRegByte((XGIIOADDRESS) pVBInfo->P3da);
1377}
1378
1379
1380/* --------------------------------------------------------------------- */
1381/* Function : XGI_SetGRCRegs */
1382/* Input : */
1383/* Output : */
1384/* Description : */
1385/* --------------------------------------------------------------------- */
1386void
1387XGI_SetGRCRegs(unsigned StandTableIndex, const VB_DEVICE_INFO *pVBInfo)
1388{
1389    unsigned i;
1390
1391    for (i = 0; i <= 8; i++) {
1392        /* Get GR from file and set GR (3ce)
1393         */
1394        const unsigned GRdata = pVBInfo->StandTable[StandTableIndex].GRC[i];
1395        XGI_SetReg((XGIIOADDRESS) pVBInfo->P3ce, i, GRdata);
1396    }
1397
1398    if (pVBInfo->ModeType > ModeVGA) {
1399        /* 256 color disable */
1400        XGI_SetRegAND((XGIIOADDRESS) pVBInfo->P3ce, 0x05, 0xBF);
1401    }
1402}
1403
1404
1405/* --------------------------------------------------------------------- */
1406/* Function : XGI_ClearExt1Regs */
1407/* Input : */
1408/* Output : */
1409/* Description : */
1410/* --------------------------------------------------------------------- */
1411void
1412XGI_ClearExt1Regs(unsigned ModeNo, const VB_DEVICE_INFO *pVBInfo)
1413{
1414    unsigned i;
1415
1416    /* Clear SR0A-SR0E */
1417    for (i = 0x0A; i <= 0x0E; i++) {
1418        XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, i, 0x00);
1419    }
1420
1421    /* This code came from the old XGI_New_ClearExt1Regs in init.c.  Since
1422     * it wasn't included in the newer code drop from XGI, I'm not sure if
1423     * it's necessary on the Volari chips.  I've included it here, ifdefed
1424     * out, for future reference.
1425     * - idr
1426     */
1427#if 0
1428    XGI_SetRegAND(pVBInfo->P3c4, 0x37, 0xFE);
1429    if ((ModeNo == 0x06) || ((ModeNo >= 0x0e) && (ModeNo <= 0x13))) {
1430        XGI_SetReg(pVBInfo->P3c4, 0x0e, 0x20);
1431    }
1432#else
1433    (void) ModeNo;
1434#endif
1435}
1436
1437/* Jong 10/17/2007; merge code */
1438/* --------------------------------------------------------------------- */
1439/* Function : XGI_SetDefaultVCLK */
1440/* Input : */
1441/* Output : */
1442/* Description : */
1443/* --------------------------------------------------------------------- */
1444UCHAR XGI_SetDefaultVCLK( PVB_DEVICE_INFO pVBInfo )
1445{
1446	/* Jong 04/22/2008; XGINew_ -> XGI_*/
1447	/* Jong 04/23/2008; coding error: VCLKData[0]-> 0x10:25MHz;  VCLKData[1]-> 0x20:28MHz */
1448    /* XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x31 , ~0x30 , 0x20 ) ; */
1449    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x31 , ~0x30 , 0x10 ) ;
1450    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x2B , pVBInfo->VCLKData[ 0 ].SR2B ) ;
1451    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x2C , pVBInfo->VCLKData[ 0 ].SR2C ) ;
1452
1453    /* XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x31 , ~0x30 , 0x10 ) ; */
1454    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x31 , ~0x30 , 0x20 ) ;
1455    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x2B , pVBInfo->VCLKData[ 1 ].SR2B ) ;
1456    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x2C , pVBInfo->VCLKData[ 1 ].SR2C ) ;
1457
1458    XGI_SetRegAND( (XGIIOADDRESS) pVBInfo->P3c4 , 0x31 , ~0x30 ) ;
1459    return( 0 ) ;
1460}
1461
1462/* --------------------------------------------------------------------- */
1463/* Function : XGI_GetRatePtrCRT2 */
1464/* Input : */
1465/* Output : */
1466/* Description : */
1467/* --------------------------------------------------------------------- */
1468USHORT
1469XGI_GetRatePtrCRT2(PXGI_HW_DEVICE_INFO HwDeviceExtension, USHORT ModeNo, USHORT ModeIdIndex, PVB_DEVICE_INFO pVBInfo)
1470{
1471    SHORT LCDRefreshIndex[] = { 0x00, 0x00, 0x03, 0x01 }
1472    , LCDARefreshIndex[] = {
1473    0x00, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01};
1474
1475    USHORT RefreshRateTableIndex, i, modeflag, index, temp;
1476
1477    if (ModeNo <= 0x13) {
1478        modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
1479    }
1480    else {
1481        modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
1482    }
1483
1484    if (ModeNo < 0x14)
1485        return (0xFFFF);
1486
1487    index = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x33);
1488    index = index >> pVBInfo->SelectCRT2Rate;
1489    index &= 0x0F;
1490
1491    if (pVBInfo->LCDInfo & (LCDNonExpanding | EnableScalingLCD))
1492        index = 0;
1493
1494    if (index > 0)
1495        index--;
1496
1497    if (pVBInfo->SetFlag & ProgrammingCRT2) {
1498        if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
1499
1500          /* Jong 10/03/2007; merge code */
1501          if( pVBInfo->IF_DEF_LVDS == 0 )
1502          {
1503
1504            if (pVBInfo->
1505                VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV
1506                          | VB_XGI301C))
1507                temp = LCDARefreshIndex[pVBInfo->LCDResInfo & 0x0F];    /* 301b */
1508            else
1509                temp = LCDRefreshIndex[pVBInfo->LCDResInfo & 0x0F];
1510
1511            if (index > temp) {
1512                index = temp;
1513            }
1514          }
1515          else
1516          {
1517                index = 0 ;
1518          }
1519        }
1520    }
1521
1522    RefreshRateTableIndex = pVBInfo->EModeIDTable[ModeIdIndex].REFindex;
1523    ModeNo = pVBInfo->RefIndex[RefreshRateTableIndex].ModeID;
1524
1525    /* Jong 10/03/2007; merge code */
1526	/* Do the similiar adjustment like XGISearchCRT1Rate() */
1527    if ( HwDeviceExtension->jChipType >= XG20 )  /* for XG20, XG21, XG27 */
1528    {
1529      /*
1530      if ( pVBInfo->RefIndex[ RefreshRateTableIndex ].Ext_InfoFlag & XG2xNotSupport )
1531      {
1532        index++;
1533      }
1534      */
1535
1536      if ( ( pVBInfo->RefIndex[ RefreshRateTableIndex ].XRes == 800 ) &&
1537           ( pVBInfo->RefIndex[ RefreshRateTableIndex ].YRes == 600 ) )
1538      {
1539        index++;
1540      }
1541      if ( ( pVBInfo->RefIndex[ RefreshRateTableIndex ].XRes == 1024 ) &&
1542           ( pVBInfo->RefIndex[ RefreshRateTableIndex ].YRes == 768 ) )
1543      {
1544        index++;
1545      }
1546      if ( ( pVBInfo->RefIndex[ RefreshRateTableIndex ].XRes == 1280 ) &&
1547           ( pVBInfo->RefIndex[ RefreshRateTableIndex ].YRes == 1024 ) )
1548      {
1549        index++;
1550      }
1551
1552	  /* Jong 11/29/2007; fix bugs of 1600x1200; set limitation to 60Hz */
1553	  /* It should need to check refresh rate supporting of output device */
1554      if ( ( pVBInfo->RefIndex[ RefreshRateTableIndex ].XRes == 1600 ) &&
1555           ( pVBInfo->RefIndex[ RefreshRateTableIndex ].YRes == 1200 ) )
1556      {
1557        index=0;
1558      }
1559    }
1560
1561	/* Jong 11/29/2007; according to CR33(index) to update refresh table index */
1562    i = 0 ;
1563    do
1564    {
1565        if (pVBInfo->RefIndex[RefreshRateTableIndex + i].ModeID != ModeNo)
1566            break;
1567        temp = pVBInfo->RefIndex[RefreshRateTableIndex + i].Ext_InfoFlag;
1568        temp &= ModeInfoFlag;
1569        if (temp < pVBInfo->ModeType)
1570            break;
1571
1572        i++;
1573        index--;
1574
1575    } while (index != 0xFFFF);
1576
1577    if (!(pVBInfo->VBInfo & SetCRT2ToRAMDAC)) {
1578        if (pVBInfo->VBInfo & SetInSlaveMode) {
1579            temp =
1580                pVBInfo->RefIndex[RefreshRateTableIndex + i - 1].Ext_InfoFlag;
1581            if (temp & InterlaceMode) {
1582                i++;
1583            }
1584        }
1585    }
1586
1587    i--;
1588    if ((pVBInfo->SetFlag & ProgrammingCRT2)) {
1589        temp =
1590            XGI_AjustCRT2Rate(ModeNo, ModeIdIndex, RefreshRateTableIndex, &i,
1591                              pVBInfo);
1592    }
1593
1594    return (RefreshRateTableIndex + i); /*return(0x01|(temp1<<1));   */
1595}
1596
1597
1598/* --------------------------------------------------------------------- */
1599/* Function : XGI_AjustCRT2Rate */
1600/* Input : */
1601/* Output : */
1602/* Description : */
1603/* --------------------------------------------------------------------- */
1604BOOLEAN
1605XGI_AjustCRT2Rate(USHORT ModeNo, USHORT ModeIdIndex,
1606                  USHORT RefreshRateTableIndex, USHORT * i,
1607                  PVB_DEVICE_INFO pVBInfo)
1608{
1609    USHORT tempax, tempbx, resinfo, modeflag, infoflag;
1610
1611    if (ModeNo <= 0x13) {
1612        modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;      /* si+St_ModeFlag */
1613    }
1614    else {
1615        modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
1616    }
1617
1618    resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
1619    tempbx = pVBInfo->RefIndex[RefreshRateTableIndex + (*i)].ModeID;
1620    tempax = 0;
1621
1622   /* Jong 10/04/2007; merge code */
1623   if ( pVBInfo->IF_DEF_LVDS == 0 )
1624   {
1625    if (pVBInfo->VBInfo & SetCRT2ToRAMDAC) {
1626        tempax |= SupportRAMDAC2;
1627
1628        if (pVBInfo->VBType & VB_XGI301C)
1629            tempax |= SupportCRT2in301C;
1630    }
1631
1632    if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {     /* 301b */
1633        tempax |= SupportLCD;
1634
1635        if (pVBInfo->LCDResInfo != Panel1280x1024) {
1636            if (pVBInfo->LCDResInfo != Panel1280x960) {
1637                if (pVBInfo->LCDInfo & LCDNonExpanding) {
1638                    if (resinfo >= 9) {
1639                        tempax = 0;
1640                        return (0);
1641                    }
1642                }
1643            }
1644        }
1645    }
1646
1647    if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {        /* for HiTV */
1648        if ((pVBInfo->VBType & VB_XGI301LV)
1649            && (pVBInfo->VBExtInfo == VB_YPbPr1080i)) {
1650            tempax |= SupportYPbPr;
1651            if (pVBInfo->VBInfo & SetInSlaveMode) {
1652                if (resinfo == 4)
1653                    return (0);
1654
1655                if (resinfo == 3)
1656                    return (0);
1657
1658                if (resinfo > 7)
1659                    return (0);
1660            }
1661        }
1662        else {
1663            tempax |= SupportHiVisionTV;
1664            if (pVBInfo->VBInfo & SetInSlaveMode) {
1665                if (resinfo == 4)
1666                    return (0);
1667
1668                if (resinfo == 3) {
1669                    if (pVBInfo->SetFlag & TVSimuMode)
1670                        return (0);
1671                }
1672
1673                if (resinfo > 7)
1674                    return (0);
1675            }
1676        }
1677    }
1678    else {
1679        if (pVBInfo->
1680            VBInfo & (SetCRT2ToAVIDEO | SetCRT2ToSVIDEO | SetCRT2ToSCART |
1681                      SetCRT2ToYPbPr | SetCRT2ToHiVisionTV)) {
1682            tempax |= SupportTV;
1683
1684            if (pVBInfo->
1685                VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV
1686                          | VB_XGI301C)) {
1687                tempax |= SupportTV1024;
1688            }
1689
1690            if (!(pVBInfo->VBInfo & SetPALTV)) {
1691                if (modeflag & NoSupportSimuTV) {
1692                    if (pVBInfo->VBInfo & SetInSlaveMode) {
1693                        if (!(pVBInfo->VBInfo & SetNotSimuMode)) {
1694                            return (0);
1695                        }
1696                    }
1697                }
1698            }
1699        }
1700    }
1701    }
1702    else		/* for LVDS */
1703    {
1704        if ( pVBInfo->VBInfo & SetCRT2ToLCD )
1705        {
1706            tempax |= SupportLCD ;
1707
1708            if ( resinfo > 0x08 )
1709                return( 0 ) ;		/* 1024x768 */
1710
1711            if ( pVBInfo->LCDResInfo < Panel1024x768 )
1712            {
1713                if ( resinfo > 0x07 )
1714                    return( 0 ) ;	/* 800x600 */
1715
1716                if ( resinfo == 0x04 )
1717                    return( 0 ) ;	/* 512x384 */
1718            }
1719        }
1720    }
1721
1722    for (; pVBInfo->RefIndex[RefreshRateTableIndex + (*i)].ModeID == tempbx;
1723         (*i)--) {
1724        infoflag =
1725            pVBInfo->RefIndex[RefreshRateTableIndex + (*i)].Ext_InfoFlag;
1726        if (infoflag & tempax) {
1727            return (1);
1728        }
1729        if ((*i) == 0)
1730            break;
1731    }
1732
1733    for ((*i) = 0;; (*i)++) {
1734        infoflag =
1735            pVBInfo->RefIndex[RefreshRateTableIndex + (*i)].Ext_InfoFlag;
1736        if (pVBInfo->RefIndex[RefreshRateTableIndex + (*i)].ModeID != tempbx) {
1737            return (0);
1738        }
1739
1740        if (infoflag & tempax) {
1741            return (1);
1742        }
1743    }
1744    return (1);
1745}
1746
1747
1748/* --------------------------------------------------------------------- */
1749/* Function : XGI_SetSync */
1750/* Input : */
1751/* Output : */
1752/* Description : */
1753/* --------------------------------------------------------------------- */
1754void
1755XGI_SetSync(unsigned RefreshRateTableIndex, const VB_DEVICE_INFO *pVBInfo)
1756{
1757    const unsigned sync =
1758	(pVBInfo->RefIndex[RefreshRateTableIndex].Ext_InfoFlag >> 8) & 0xC0;
1759
1760    /* Set Misc(3c2) */
1761    XGI_SetRegByte((XGIIOADDRESS) pVBInfo->P3c2, sync | 0x2F);
1762}
1763
1764
1765/* --------------------------------------------------------------------- */
1766/* Function : XGI_SetCRT1CRTC */
1767/* Input : */
1768/* Output : */
1769/* Description : */
1770/* --------------------------------------------------------------------- */
1771void
1772XGI_SetCRT1CRTC(USHORT ModeNo, USHORT ModeIdIndex,
1773                USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo,
1774                PXGI_HW_DEVICE_INFO HwDeviceExtension)
1775{
1776    UCHAR index, data;
1777#ifndef LINUX_XF86
1778    USHORT temp, tempah, j, modeflag, ResInfo, DisplayType;
1779#endif
1780    USHORT i;
1781
1782    index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;      /* Get index */
1783    index = index & IndexMask;
1784
1785    data = (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x11);
1786    data &= 0x7F;
1787    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x11, data);       /* Unlock CRTC */
1788
1789    for (i = 0; i < 8; i++)
1790        pVBInfo->TimingH.data[i] =
1791            pVBInfo->XGINEWUB_CRT1Table[index].CR[i];
1792
1793    for (i = 0; i < 7; i++)
1794        pVBInfo->TimingV.data[i] =
1795            pVBInfo->XGINEWUB_CRT1Table[index].CR[i + 8];
1796
1797    XGI_SetCRT1Timing_H(pVBInfo, HwDeviceExtension);
1798
1799
1800
1801    XGI_SetCRT1Timing_V(ModeIdIndex, ModeNo, pVBInfo);
1802
1803
1804    if (pVBInfo->ModeType > 0x03)
1805        XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x14, 0x4F);
1806}
1807
1808
1809/* --------------------------------------------------------------------- */
1810/* Function : XGI_SetCRT1Timing_H */
1811/* Input : */
1812/* Output : */
1813/* Description : */
1814/* --------------------------------------------------------------------- */
1815void
1816XGI_SetCRT1Timing_H(PVB_DEVICE_INFO pVBInfo,
1817                    PXGI_HW_DEVICE_INFO HwDeviceExtension)
1818{
1819    UCHAR data, data1, pushax;
1820    USHORT i, j;
1821
1822    /* XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4 , 0x51 , 0 ) ; */
1823    /* XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4 , 0x56 , 0 ) ; */
1824    /* XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4 ,0x11 , 0x7f , 0x00 ) ; */
1825
1826    data = (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x11);      /* unlock cr0-7 */
1827    data &= 0x7F;
1828    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x11, data);
1829
1830    data = pVBInfo->TimingH.data[0];
1831    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, 0, data);
1832
1833    for (i = 0x01; i <= 0x04; i++) {
1834        data = pVBInfo->TimingH.data[i];
1835        XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, (USHORT) (i + 1), data);
1836    }
1837
1838    for (i = 0x05; i <= 0x06; i++) {
1839        data = pVBInfo->TimingH.data[i];
1840        XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, (USHORT) (i + 6), data);
1841    }
1842
1843    j = (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x0e);
1844    j &= 0x1F;
1845    data = pVBInfo->TimingH.data[7];
1846    data &= 0xE0;
1847    data |= j;
1848    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x0e, data);
1849
1850    /* Jong 10/04/2007; merge code */
1851    if (HwDeviceExtension->jChipType >= XG20) {
1852        data = (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x04);
1853        data = data - 1;
1854        XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x04, data);
1855        data = (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x05);
1856        data1 = data;
1857        data1 &= 0xE0;
1858        data &= 0x1F;
1859        if (data == 0) {
1860            pushax = data;
1861            data = (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x0c);
1862            data &= 0xFB;
1863            XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x0c, data);
1864            data = pushax;
1865        }
1866        data = data - 1;
1867        data |= data1;
1868        XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x05, data);
1869        data = (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x0e);
1870        data = data >> 5;
1871        data = data + 3;
1872        if (data > 7)
1873            data = data - 7;
1874        data = data << 5;
1875        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x0e, ~0xE0, data);
1876    }
1877}
1878
1879
1880/* --------------------------------------------------------------------- */
1881/* Function : XGI_SetCRT1Timing_V */
1882/* Input : */
1883/* Output : */
1884/* Description : */
1885/* --------------------------------------------------------------------- */
1886void
1887XGI_SetCRT1Timing_V(USHORT ModeIdIndex, USHORT ModeNo,
1888                    PVB_DEVICE_INFO pVBInfo)
1889{
1890    UCHAR data;
1891    USHORT i, j;
1892
1893    /* XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4 , 0x51 , 0 ) ; */
1894    /* XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4 , 0x56 , 0 ) ; */
1895    /* XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4 , 0x11 , 0x7f , 0x00 ) ; */
1896
1897    for (i = 0x00; i <= 0x01; i++) {
1898        data = pVBInfo->TimingV.data[i];
1899        XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, (USHORT) (i + 6), data);
1900    }
1901
1902    for (i = 0x02; i <= 0x03; i++) {
1903        data = pVBInfo->TimingV.data[i];
1904        XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, (USHORT) (i + 0x0e), data);
1905    }
1906
1907    for (i = 0x04; i <= 0x05; i++) {
1908        data = pVBInfo->TimingV.data[i];
1909        XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, (USHORT) (i + 0x11), data);
1910    }
1911
1912    j = (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x0a);
1913    j &= 0xC0;
1914    data = pVBInfo->TimingV.data[6];
1915    data &= 0x3F;
1916    data |= j;
1917    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x0a, data);
1918
1919    data = pVBInfo->TimingV.data[6];
1920    data &= 0x80;
1921    data = data >> 2;
1922
1923    if (ModeNo <= 0x13)
1924        i = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
1925    else
1926        i = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
1927
1928    i &= DoubleScanMode;
1929    if (i)
1930        data |= 0x80;
1931
1932    j = (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x09);
1933    j &= 0x5F;
1934    data |= j;
1935    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x09, data);
1936}
1937
1938/* Jong 10/04/2007; merge code */
1939/* --------------------------------------------------------------------- */
1940/* Function : XGI_SetXG21CRTC */
1941/* Input : Stand or enhance CRTC table */
1942/* Output : Fill CRT Hsync/Vsync to SR2E/SR2F/SR30/SR33/SR34/SR3F */
1943/* Description : Set LCD timing */
1944/* --------------------------------------------------------------------- */
1945void XGI_SetXG21CRTC(USHORT ModeNo, USHORT ModeIdIndex, USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo)
1946{
1947  UCHAR StandTableIndex, index, Tempax, Tempbx, Tempcx, Tempdx ;
1948  USHORT Temp1, Temp2, Temp3 ;
1949
1950  if ( ModeNo <= 0x13 )
1951  {
1952    StandTableIndex = XGI_GetModePtr( pVBInfo->SModeIDTable,
1953										pVBInfo->ModeType,
1954                                        ModeNo, ModeIdIndex);
1955    Tempax = pVBInfo->StandTable[ StandTableIndex ].CRTC[ 4 ] ;		/* CR04 HRS */
1956    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x2E , Tempax ) ;    		/* SR2E [7:0]->HRS */
1957    Tempbx = pVBInfo->StandTable[ StandTableIndex ].CRTC[ 5 ] ;		/* Tempbx: CR05 HRE */
1958    Tempbx &= 0x1F ;							/* Tempbx: HRE[4:0] */
1959    Tempcx = Tempax ;
1960    Tempcx &=  0xE0 ;							/* Tempcx: HRS[7:5] */
1961    Tempdx = Tempcx | Tempbx ;						/* Tempdx(HRE): HRS[7:5]HRE[4:0] */
1962    if ( Tempbx < ( Tempax & 0x1F ) )					/* IF HRE < HRS */
1963      Tempdx |= 0x20 ;							/* Tempdx: HRE = HRE + 0x20 */
1964    Tempdx <<= 2 ;							/* Tempdx << 2 */
1965    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x2F , Tempdx ) ;    		/* SR2F [7:2]->HRE */
1966    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x30 , 0xE3 , 00 ) ;
1967
1968    Tempax = pVBInfo->StandTable[ StandTableIndex ].CRTC[ 16 ] ;	/* Tempax: CR16 VRS */
1969    Tempbx = Tempax ;							/* Tempbx=Tempax */
1970    Tempax &= 0x01 ;							/* Tempax: VRS[0] */
1971    XGI_SetRegOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x33 , Tempax ) ;   		/* SR33[0]->VRS */
1972    Tempax = pVBInfo->StandTable[ StandTableIndex ].CRTC[ 7 ] ;		/* Tempax: CR7 VRS */
1973    Tempdx = Tempbx >> 1 ;						/* Tempdx: VRS[7:1] */
1974    Tempcx = Tempax & 0x04 ;						/* Tempcx: CR7[2] */
1975    Tempcx <<= 5 ;							/* Tempcx[7]: VRS[8] */
1976    Tempdx |= Tempcx ;							/* Tempdx: VRS[8:1] */
1977    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x34 , Tempdx ) ;    		/* SR34[7:0]: VRS[8:1] */
1978
1979    Temp1 = Tempcx << 1 ;						/* Temp1[8]: VRS[8] UCHAR -> USHORT */
1980    Temp1 |= Tempbx ;							/* Temp1[8:0]: VRS[8:0] */
1981    Tempax &= 0x80 ;							/* Tempax[7]: CR7[7] */
1982    Temp2 = Tempax << 2 ;						/* Temp2[9]: VRS[9] */
1983    Temp1 |= Temp2 ;							/* Temp1[9:0]: VRS[9:0] */
1984
1985    Tempax = pVBInfo->StandTable[ StandTableIndex ].CRTC[ 17 ] ;	/* CR16 VRE */
1986    Tempax &= 0x0F ;							/* Tempax[3:0]: VRE[3:0] */
1987    Temp2 = Temp1 & 0x3F0 ;						/* Temp2[9:4]: VRS[9:4] */
1988    Temp2 |= Tempax ;							/* Temp2[9:0]: VRE[9:0] */
1989    Temp3 = Temp1 & 0x0F ;						/* Temp3[3:0]: VRS[3:0] */
1990    if ( Tempax < Temp3 )						/* VRE[3:0]<VRS[3:0] */
1991      Temp2 |= 0x10 ;							/* Temp2: VRE + 0x10 */
1992    Temp2 &= 0xFF ;							/* Temp2[7:0]: VRE[7:0] */
1993    Tempax = (UCHAR)Temp2 ;						/* Tempax[7:0]: VRE[7:0] */
1994    Tempax <<= 2 ;							/* Tempax << 2: VRE[5:0] */
1995    Temp1 &= 0x600 ;							/* Temp1[10:9]: VRS[10:9] */
1996    Temp1 >>= 9 ;							/* [10:9]->[1:0] */
1997    Tempbx = (UCHAR)Temp1 ;						/* Tempbx[1:0]: VRS[10:9] */
1998    Tempax |= Tempbx ;							/* VRE[5:0]VRS[10:9] */
1999    Tempax &= 0x7F ;
2000    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x3F , Tempax ) ;   		/* SR3F D[7:2]->VRE D[1:0]->VRS */
2001  }
2002  else
2003  {
2004    index = pVBInfo->RefIndex[ RefreshRateTableIndex ].Ext_CRT1CRTC ;
2005    Tempax = pVBInfo->XGINEWUB_CRT1Table[ index ].CR[ 3 ] ;		/* Tempax: CR4 HRS */
2006    Tempcx = Tempax ;							/* Tempcx: HRS */
2007    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x2E , Tempax ) ;  		        /* SR2E[7:0]->HRS */
2008
2009    Tempdx = pVBInfo->XGINEWUB_CRT1Table[ index ].CR[ 5 ] ;		/* SRB */
2010    Tempdx &= 0xC0 ;							/* Tempdx[7:6]: SRB[7:6] */
2011    Temp1 = Tempdx ;							/* Temp1[7:6]: HRS[9:8] */
2012    Temp1 <<= 2 ;							/* Temp1[9:8]: HRS[9:8] */
2013    Temp1 |= Tempax ;							/* Temp1[9:0]: HRS[9:0] */
2014
2015    Tempax = pVBInfo->XGINEWUB_CRT1Table[ index ].CR[ 4 ] ;		/* CR5 HRE */
2016    Tempax &= 0x1F ;							/* Tempax[4:0]: HRE[4:0] */
2017
2018    Tempbx = pVBInfo->XGINEWUB_CRT1Table[ index ].CR[ 6 ] ;		/* SRC */
2019    Tempbx &= 0x04 ;							/* Tempbx[2]: HRE[5] */
2020    Tempbx <<= 3 ;							/* Tempbx[5]: HRE[5] */
2021    Tempax |= Tempbx ;							/* Tempax[5:0]: HRE[5:0] */
2022
2023    Temp2 = Temp1 & 0x3C0 ;						/* Temp2[9:6]: HRS[9:6] */
2024    Temp2 |= Tempax ;							/* Temp2[9:0]: HRE[9:0] */
2025
2026    Tempcx &= 0x3F ;							/* Tempcx[5:0]: HRS[5:0] */
2027    if( Tempax < Tempcx )						/* HRE < HRS */
2028      Temp2 |= 0x40 ;                                                   /* Temp2 + 0x40 */
2029
2030    Temp2 &= 0xFF ;
2031    Tempax = (UCHAR)Temp2 ;						/* Tempax: HRE[7:0] */
2032    Tempax <<= 2 ;							/* Tempax[7:2]: HRE[5:0] */
2033    Tempdx >>= 6 ;							/* Tempdx[7:6]->[1:0] HRS[9:8] */
2034    Tempax |= Tempdx ;							/* HRE[5:0]HRS[9:8] */
2035    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x2F , Tempax ) ;    		/* SR2F D[7:2]->HRE, D[1:0]->HRS */
2036    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x30 , 0xE3 , 00 ) ;
2037
2038    Tempax = pVBInfo->XGINEWUB_CRT1Table[ index ].CR[ 10 ] ;		/* CR10 VRS */
2039    Tempbx = Tempax ;							/* Tempbx: VRS */
2040    Tempax &= 0x01 ;							/* Tempax[0]: VRS[0] */
2041    XGI_SetRegOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x33 , Tempax ) ;   		/* SR33[0]->VRS[0] */
2042    Tempax = pVBInfo->XGINEWUB_CRT1Table[ index ].CR[ 9 ] ;		/* CR7[2][7] VRE */
2043    Tempcx = Tempbx >> 1 ;				   		/* Tempcx[6:0]: VRS[7:1] */
2044    Tempdx = Tempax & 0x04 ;						/* Tempdx[2]: CR7[2] */
2045    Tempdx <<= 5 ;							/* Tempdx[7]: VRS[8] */
2046    Tempcx |= Tempdx ;							/* Tempcx[7:0]: VRS[8:1] */
2047    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x34 , Tempcx ) ;   		/* SR34[8:1]->VRS */
2048
2049    Temp1 = Tempdx ;							/* Temp1[7]: Tempdx[7] */
2050    Temp1 <<= 1 ;							/* Temp1[8]: VRS[8] */
2051    Temp1 |= Tempbx ;							/* Temp1[8:0]: VRS[8:0] */
2052    Tempax &= 0x80 ;
2053    Temp2 = Tempax << 2 ;						/* Temp2[9]: VRS[9] */
2054    Temp1 |= Temp2 ;							/* Temp1[9:0]: VRS[9:0] */
2055    Tempax = pVBInfo->XGINEWUB_CRT1Table[ index ].CR[ 14 ] ;		/* Tempax: SRA */
2056    Tempax &= 0x08 ;							/* Tempax[3]: VRS[3] */
2057    Temp2 = Tempax ;
2058    Temp2 <<= 7 ;							/* Temp2[10]: VRS[10] */
2059    Temp1 |= Temp2 ;							/* Temp1[10:0]: VRS[10:0] */
2060
2061    Tempax = pVBInfo->XGINEWUB_CRT1Table[ index ].CR[ 11 ] ;		/* Tempax: CR11 VRE */
2062    Tempax &= 0x0F ;							/* Tempax[3:0]: VRE[3:0] */
2063    Tempbx = pVBInfo->XGINEWUB_CRT1Table[ index ].CR[ 14 ] ;		/* Tempbx: SRA */
2064    Tempbx &= 0x20 ;							/* Tempbx[5]: VRE[5] */
2065    Tempbx >>= 1 ;							/* Tempbx[4]: VRE[4] */
2066    Tempax |= Tempbx ;							/* Tempax[4:0]: VRE[4:0] */
2067    Temp2 = Temp1 & 0x7E0 ;						/* Temp2[10:5]: VRS[10:5] */
2068    Temp2 |= Tempax ;							/* Temp2[10:5]: VRE[10:5] */
2069
2070    Temp3 = Temp1 & 0x1F ;						/* Temp3[4:0]: VRS[4:0] */
2071    if ( Tempax < Temp3 )						/* VRE < VRS */
2072      Temp2 |= 0x20 ;							/* VRE + 0x20 */
2073
2074    Temp2 &= 0xFF ;
2075    Tempax = (UCHAR)Temp2 ;						/* Tempax: VRE[7:0] */
2076    Tempax <<= 2 ;							/* Tempax[7:0]; VRE[5:0]00 */
2077    Temp1 &= 0x600 ;							/* Temp1[10:9]: VRS[10:9] */
2078    Temp1 >>= 9 ;  							/* Temp1[1:0]: VRS[10:9] */
2079    Tempbx = (UCHAR)Temp1 ;
2080    Tempax |= Tempbx ;							/* Tempax[7:0]: VRE[5:0]VRS[10:9] */
2081    Tempax &= 0x7F ;
2082    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x3F , Tempax ) ;   		/* SR3F D[7:2]->VRE D[1:0]->VRS */
2083  }
2084}
2085
2086/* Jong 10/04/2007; merge code */
2087void XGI_SetXG27CRTC(USHORT ModeNo, USHORT ModeIdIndex, USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo)
2088{
2089  USHORT StandTableIndex, index, Tempax, Tempbx, Tempcx, Tempdx ;
2090
2091  if ( ModeNo <= 0x13 )
2092  {
2093    /* Jong 10/05/2007; merge code */
2094    /* StandTableIndex = XGI_GetModePtr( ModeNo , ModeIdIndex, pVBInfo ) ; */
2095    StandTableIndex = XGI_GetModePtr( pVBInfo->SModeIDTable,
2096										pVBInfo->ModeType,
2097                                        ModeNo, ModeIdIndex);
2098
2099    Tempax = pVBInfo->StandTable[ StandTableIndex ].CRTC[ 4 ] ;		/* CR04 HRS */
2100    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x2E , Tempax ) ;    		/* SR2E [7:0]->HRS */
2101    Tempbx = pVBInfo->StandTable[ StandTableIndex ].CRTC[ 5 ] ;		/* Tempbx: CR05 HRE */
2102    Tempbx &= 0x1F ;							/* Tempbx: HRE[4:0] */
2103    Tempcx = Tempax ;
2104    Tempcx &=  0xE0 ;							/* Tempcx: HRS[7:5] */
2105    Tempdx = Tempcx | Tempbx ;						/* Tempdx(HRE): HRS[7:5]HRE[4:0] */
2106    if ( Tempbx < ( Tempax & 0x1F ) )					/* IF HRE < HRS */
2107      Tempdx |= 0x20 ;							/* Tempdx: HRE = HRE + 0x20 */
2108    Tempdx <<= 2 ;							/* Tempdx << 2 */
2109    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x2F , Tempdx ) ;    		/* SR2F [7:2]->HRE */
2110    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x30 , 0xE3 , 00 ) ;
2111
2112    Tempax = pVBInfo->StandTable[ StandTableIndex ].CRTC[ 16 ] ;	/* Tempax: CR10 VRS */
2113    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x34 , Tempax ) ;   		/* SR34[7:0]->VRS */
2114    Tempcx = Tempax ;							/* Tempcx=Tempax=VRS[7:0] */
2115    Tempax = pVBInfo->StandTable[ StandTableIndex ].CRTC[ 7 ] ;		/* Tempax[7][2]: CR7[7][2] VRS[9][8] */
2116    Tempbx = Tempax ;							/* Tempbx=CR07 */
2117    Tempax &= 0x04 ;     						/* Tempax[2]: CR07[2] VRS[8] */
2118    Tempax >>= 2;
2119    XGI_SetRegANDOR( (XGIIOADDRESS)  pVBInfo->P3c4 , 0x35 , ~0x01, Tempax ) ;        /* SR35 D[0]->VRS D[8] */
2120    Tempcx |= (Tempax << 8) ;					        /* Tempcx[8] |= VRS[8] */
2121    Tempcx |= (Tempbx & 0x80)<<2;					/* Tempcx[9] |= VRS[9] */
2122
2123
2124    Tempax = pVBInfo->StandTable[ StandTableIndex ].CRTC[ 17 ] ;	/* CR11 VRE */
2125    Tempax &= 0x0F ;							/* Tempax: VRE[3:0] */
2126    Tempbx = Tempcx ;							/* Tempbx=Tempcx=VRS[9:0] */
2127    Tempbx &= 0x3F0 ;    						/* Tempbx[9:4]: VRS[9:4] */
2128    Tempbx |= Tempax ;							/* Tempbx[9:0]: VRE[9:0] */
2129    if ( Tempax <= (Tempcx & 0x0F) )					/* VRE[3:0]<=VRS[3:0] */
2130      Tempbx |= 0x10 ;							/* Tempbx: VRE + 0x10 */
2131    Tempax = (UCHAR)Tempbx & 0xFF;					/* Tempax[7:0]: VRE[7:0] */
2132    Tempax <<= 2 ;							/* Tempax << 2: VRE[5:0] */
2133    Tempcx = (Tempcx&0x600)>>8;                                         /* Tempcx VRS[10:9] */
2134    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x3F , ~0xFC, Tempax ) ;        /* SR3F D[7:2]->VRE D[5:0] */
2135    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x35 , ~0x06, Tempcx ) ;	/* SR35 D[2:1]->VRS[10:9] */
2136  }
2137  else
2138  {
2139    index = pVBInfo->RefIndex[ RefreshRateTableIndex ].Ext_CRT1CRTC ;
2140    Tempax = pVBInfo->XGINEWUB_CRT1Table[ index ].CR[ 3 ] ;		/* Tempax: CR4 HRS */
2141    Tempbx = Tempax ;							/* Tempbx: HRS[7:0] */
2142    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x2E , Tempax ) ;  		        /* SR2E[7:0]->HRS */
2143
2144    Tempax = pVBInfo->XGINEWUB_CRT1Table[ index ].CR[ 5 ] ;		/* SR0B */
2145    Tempax &= 0xC0 ;							/* Tempax[7:6]: SR0B[7:6]: HRS[9:8]*/
2146    Tempbx |= (Tempax << 2);					/* Tempbx: HRS[9:0] */
2147
2148    Tempax = pVBInfo->XGINEWUB_CRT1Table[ index ].CR[ 4 ] ;		/* CR5 HRE */
2149    Tempax &= 0x1F ;							/* Tempax[4:0]: HRE[4:0] */
2150    Tempcx = Tempax ;							/* Tempcx: HRE[4:0] */
2151
2152    Tempax = pVBInfo->XGINEWUB_CRT1Table[ index ].CR[ 6 ] ;		/* SRC */
2153    Tempax &= 0x04 ;							/* Tempax[2]: HRE[5] */
2154    Tempax <<= 3 ;							    /* Tempax[5]: HRE[5] */
2155    Tempcx |= Tempax ;							/* Tempcx[5:0]: HRE[5:0] */
2156
2157    Tempbx = Tempbx & 0x3C0 ;					/* Tempbx[9:6]: HRS[9:6] */
2158    Tempbx |= Tempcx ;							/* Tempbx: HRS[9:6]HRE[5:0] */
2159
2160    Tempax = pVBInfo->XGINEWUB_CRT1Table[ index ].CR[ 3 ] ;		/* Tempax: CR4 HRS */
2161    Tempax &= 0x3F ;							/* Tempax: HRS[5:0] */
2162    if( Tempcx <= Tempax )						/* HRE[5:0] < HRS[5:0] */
2163      Tempbx += 0x40 ;                          /* Tempbx= Tempbx + 0x40 : HRE[9:0]*/
2164
2165    Tempax = pVBInfo->XGINEWUB_CRT1Table[ index ].CR[ 5 ] ;		/* SR0B */
2166    Tempax &= 0xC0 ;							/* Tempax[7:6]: SR0B[7:6]: HRS[9:8]*/
2167    Tempax >>= 6;                               /* Tempax[1:0]: HRS[9:8]*/
2168    Tempax |= ((Tempbx << 2) & 0xFF);           /* Tempax[7:2]: HRE[5:0] */
2169    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x2F , Tempax ) ;    		/* SR2F [7:2][1:0]: HRE[5:0]HRS[9:8] */
2170    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x30 , 0xE3 , 00 ) ;
2171
2172    Tempax = pVBInfo->XGINEWUB_CRT1Table[ index ].CR[ 10 ] ;		/* CR10 VRS */
2173    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x34 , Tempax ) ;   		/* SR34[7:0]->VRS[7:0] */
2174
2175    Tempcx = Tempax ;							/* Tempcx <= VRS[7:0] */
2176    Tempax = pVBInfo->XGINEWUB_CRT1Table[ index ].CR[ 9 ] ;		/* CR7[7][2] VRS[9][8] */
2177    Tempbx = Tempax ;							/* Tempbx <= CR07[7:0] */
2178    Tempax = Tempax & 0x04 ;					/* Tempax[2]: CR7[2]: VRS[8] */
2179    Tempax >>= 2 ;							    /* Tempax[0]: VRS[8] */
2180    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x35 , ~0x01 , Tempax ) ;   	/* SR35[0]: VRS[8] */
2181    Tempcx |= (Tempax<<8) ;						/* Tempcx <= VRS[8:0] */
2182    Tempcx |= ((Tempbx&0x80)<<2) ;				/* Tempcx <= VRS[9:0] */
2183    Tempax = pVBInfo->XGINEWUB_CRT1Table[ index ].CR[ 14 ] ;		/* Tempax: SR0A */
2184    Tempax &= 0x08;                             /* SR0A[3] VRS[10] */
2185    Tempcx |= (Tempax<<7) ;        				/* Tempcx <= VRS[10:0] */
2186
2187
2188    Tempax = pVBInfo->XGINEWUB_CRT1Table[ index ].CR[ 11 ] ;		/* Tempax: CR11 VRE */
2189    Tempax &= 0x0F ;							/* Tempax[3:0]: VRE[3:0] */
2190    Tempbx = pVBInfo->XGINEWUB_CRT1Table[ index ].CR[ 14 ] ;		/* Tempbx: SR0A */
2191    Tempbx &= 0x20 ;							/* Tempbx[5]: SR0A[5]: VRE[4] */
2192    Tempbx >>= 1 ;							    /* Tempbx[4]: VRE[4] */
2193    Tempax |= Tempbx ;							/* Tempax[4:0]: VRE[4:0] */
2194    Tempbx = Tempcx ;							/* Tempbx: VRS[10:0] */
2195    Tempbx &= 0x7E0 ;						    /* Tempbx[10:5]: VRS[10:5] */
2196    Tempbx |= Tempax ;							/* Tempbx: VRS[10:5]VRE[4:0] */
2197
2198    if ( Tempbx <= Tempcx )						/* VRE <= VRS */
2199      Tempbx |= 0x20 ;							/* VRE + 0x20 */
2200
2201    Tempax = (Tempbx<<2) & 0xFF ;					/* Tempax: Tempax[7:0]; VRE[5:0]00 */
2202    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x3F , ~0xFC , Tempax ) ;	/* SR3F[7:2]:VRE[5:0] */
2203    Tempax = Tempcx >> 8;
2204    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x35 , ~0x07 , Tempax ) ;	/* SR35[2:0]:VRS[10:8] */
2205  }
2206}
2207
2208
2209/* Jong 10/04/2007; merge code */
2210/* --------------------------------------------------------------------- */
2211/* Function : XGI_SetXG21LCD */
2212/* Input : */
2213/* Output : FCLK duty cycle, FCLK delay compensation */
2214/* Description : All values set zero */
2215/* --------------------------------------------------------------------- */
2216void XGI_SetXG21LCD(PVB_DEVICE_INFO pVBInfo,USHORT RefreshRateTableIndex,USHORT ModeNo)
2217{
2218  ULONG Data , Temp , b3CC ;
2219  ULONG XGI_P3cc ;
2220
2221  if ( ModeNo > 0x13 )
2222    Data = pVBInfo->RefIndex[ RefreshRateTableIndex ].Ext_InfoFlag ;
2223  XGI_P3cc = pVBInfo->P3cc ;
2224
2225  XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x2E , 0x00 ) ;
2226  XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x2F , 0x00 ) ;
2227  XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x46 , 0x00 ) ;
2228  XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x47 , 0x00 ) ;
2229  if ( ((*pVBInfo->pDVOSetting)&0xC0) == 0xC0 )
2230  {
2231    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x2E , *pVBInfo->pCR2E ) ;
2232    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x2F , *pVBInfo->pCR2F ) ;
2233    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x46 , *pVBInfo->pCR46 ) ;
2234    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x47 , *pVBInfo->pCR47 ) ;
2235  }
2236
2237  Temp = XGI_GetReg( pVBInfo->P3d4 , 0x37 ) ;
2238
2239  if ( Temp & 0x01 )
2240  {
2241    XGI_SetRegOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x06 , 0x40 ) ; /* 18 bits FP */
2242    XGI_SetRegOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x09 , 0x40 ) ;
2243  }
2244
2245  XGI_SetRegOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x1E , 0x01 ) ;   /* Negative blank polarity */
2246
2247  XGI_SetRegAND( (XGIIOADDRESS) pVBInfo->P3c4 , 0x30 , ~0x20 ) ;
2248  XGI_SetRegAND( (XGIIOADDRESS) pVBInfo->P3c4 , 0x35 , ~0x80 ) ;
2249
2250  if ( ModeNo <= 0x13 )
2251  {
2252/* Jong 07/31/2009; might use XGINew_P3cc instead */
2253#if 1
2254    b3CC = (UCHAR) XGI_GetRegByte( (XGIIOADDRESS) XGI_P3cc ) ;
2255    if ( b3CC & 0x40 )
2256      XGI_SetRegOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x30 , 0x20 ) ; /* Hsync polarity */
2257    if ( b3CC & 0x80 )
2258      XGI_SetRegOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x35 , 0x80 ) ; /* Vsync polarity */
2259#endif
2260  }
2261  else
2262  {
2263    if ( Data & 0x4000 )
2264      XGI_SetRegOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x30 , 0x20 ) ; /* Hsync polarity */
2265    if ( Data & 0x8000 )
2266      XGI_SetRegOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x35 , 0x80 ) ; /* Vsync polarity */
2267  }
2268}
2269
2270/* Jong 10/04/2007; merge code */
2271void XGI_SetXG27LCD(PVB_DEVICE_INFO pVBInfo,USHORT RefreshRateTableIndex,USHORT ModeNo)
2272{
2273  ULONG Data , Temp , b3CC ;
2274  ULONG XGI_P3cc ;
2275
2276  PDEBUG(ErrorF("XGI_SetXG27LCD()...begin\n"));
2277
2278  if ( ModeNo > 0x13 )
2279    Data = pVBInfo->RefIndex[ RefreshRateTableIndex ].Ext_InfoFlag ;
2280
2281  XGI_P3cc = pVBInfo->P3cc ;
2282
2283  PDEBUG(ErrorF("XGI_SetXG27LCD()...1\n"));
2284
2285  XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x2E , 0x00 ) ;
2286  XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x2F , 0x00 ) ;
2287  XGI_SetReg( pVBInfo->P3d4 , 0x46 , 0x00 ) ;
2288  XGI_SetReg( pVBInfo->P3d4 , 0x47 , 0x00 ) ;
2289
2290  PDEBUG(ErrorF("XGI_SetXG27LCD()...2\n"));
2291
2292  Temp = XGI_GetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x37 ) ;
2293
2294  PDEBUG(ErrorF("XGI_SetXG27LCD()...3\n"));
2295
2296  if ( ( Temp & 0x03 ) == 0 )  /* dual 12 */
2297  {
2298    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x46 , 0x13 ) ;
2299    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x47 , 0x13 ) ;
2300  }
2301
2302  PDEBUG(ErrorF("XGI_SetXG27LCD()...4\n"));
2303
2304  if ( ((*pVBInfo->pDVOSetting)&0xC0) == 0xC0 )
2305  {
2306    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x2E , *pVBInfo->pCR2E ) ;
2307    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x2F , *pVBInfo->pCR2F ) ;
2308    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x46 , *pVBInfo->pCR46 ) ;
2309    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x47 , *pVBInfo->pCR47 ) ;
2310  }
2311
2312  PDEBUG(ErrorF("XGI_SetXG27LCD()...5\n"));
2313
2314  XGI_SetXG27FPBits(pVBInfo);
2315
2316  PDEBUG(ErrorF("XGI_SetXG27LCD()...6\n"));
2317
2318  XGI_SetRegOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x1E , 0x01 ) ;   /* Negative blank polarity */
2319
2320  XGI_SetRegAND( (XGIIOADDRESS) pVBInfo->P3c4 , 0x30 , ~0x20 ) ; /* Hsync polarity */
2321  XGI_SetRegAND( (XGIIOADDRESS) pVBInfo->P3c4 , 0x35 , ~0x80 ) ; /* Vsync polarity */
2322
2323  PDEBUG(ErrorF("XGI_SetXG27LCD()...7\n"));
2324
2325  if ( ModeNo <= 0x13 )
2326  {
2327	  PDEBUG(ErrorF("XGI_SetXG27LCD()...7-1-XGI_P3cc=%d\n", XGI_P3cc));
2328
2329/* Jong 07/31/2009; might use XGINew_P3cc instead */
2330#if 1
2331   b3CC = (UCHAR) XGI_GetRegByte( (XGIIOADDRESS) XGI_P3cc ) ;
2332    if ( b3CC & 0x40 )
2333      XGI_SetRegOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x30 , 0x20 ) ; /* Hsync polarity */
2334    if ( b3CC & 0x80 )
2335      XGI_SetRegOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x35 , 0x80 ) ; /* Vsync polarity */
2336#endif
2337
2338	PDEBUG(ErrorF("XGI_SetXG27LCD()...7-2\n"));
2339  }
2340  else
2341  {
2342    if ( Data & 0x4000 )
2343      XGI_SetRegOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x30 , 0x20 ) ; /* Hsync polarity */
2344    if ( Data & 0x8000 )
2345      XGI_SetRegOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x35 , 0x80 ) ; /* Vsync polarity */
2346  }
2347
2348  PDEBUG(ErrorF("XGI_SetXG27LCD()...End\n"));
2349}
2350
2351/* Jong 10/04/2007; merge code */
2352/* --------------------------------------------------------------------- */
2353/* Function : XGI_UpdateXG21CRTC */
2354/* Input : */
2355/* Output : CRT1 CRTC */
2356/* Description : Modify CRT1 Hsync/Vsync to fix LCD mode timing */
2357/* --------------------------------------------------------------------- */
2358void XGI_UpdateXG21CRTC( USHORT ModeNo , PVB_DEVICE_INFO pVBInfo , USHORT RefreshRateTableIndex )
2359{
2360  int i , index = -1;
2361
2362  PDEBUG(ErrorF("XGI_UpdateXG21CRTC()...begin\n"));
2363
2364  XGI_SetRegAND( (XGIIOADDRESS) pVBInfo->P3d4 , 0x11 , 0x7F ) ;		/* Unlock CR0~7 */
2365
2366  PDEBUG(ErrorF("XGI_UpdateXG21CRTC()...1\n"));
2367
2368  if ( ModeNo <= 0x13 )
2369  {
2370    for( i = 0 ; i < 12 ; i++ )
2371    {
2372      if ( ModeNo == pVBInfo->UpdateCRT1[ i ].ModeID )
2373        index = i ;
2374    }
2375  }
2376  else
2377  {
2378    if ( ModeNo == 0x2E && ( pVBInfo->RefIndex[ RefreshRateTableIndex ].Ext_CRT1CRTC ==  RES640x480x60 ) )
2379      index = 12 ;
2380    else if ( ModeNo == 0x2E && ( pVBInfo->RefIndex[ RefreshRateTableIndex ].Ext_CRT1CRTC == RES640x480x72 ) )
2381      index = 13 ;
2382    else if ( ModeNo == 0x2F )
2383      index = 14 ;
2384    else if ( ModeNo == 0x50 )
2385      index = 15 ;
2386    else if ( ModeNo == 0x59 )
2387      index = 16 ;
2388  }
2389
2390  PDEBUG(ErrorF("XGI_UpdateXG21CRTC()...2\n"));
2391
2392  if( index != -1 )
2393  {
2394    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x02 , pVBInfo->UpdateCRT1[ index ].CR02 ) ;
2395    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x03 , pVBInfo->UpdateCRT1[ index ].CR03 ) ;
2396    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x15 , pVBInfo->UpdateCRT1[ index ].CR15 ) ;
2397    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x16 , pVBInfo->UpdateCRT1[ index ].CR16 ) ;
2398  }
2399
2400  PDEBUG(ErrorF("XGI_UpdateXG21CRTC()...End\n"));
2401}
2402
2403/* --------------------------------------------------------------------- */
2404/* Function : XGI_SetCRT1DE */
2405/* Input : */
2406/* Output : */
2407/* Description : */
2408/* --------------------------------------------------------------------- */
2409void
2410XGI_SetCRT1DE(PXGI_HW_DEVICE_INFO HwDeviceExtension, USHORT ModeNo,
2411              USHORT ModeIdIndex, USHORT RefreshRateTableIndex,
2412              PVB_DEVICE_INFO pVBInfo)
2413{
2414    USHORT tempax, tempbx, tempcx, temp, modeflag;
2415    UCHAR data;
2416    const USHORT resindex = XGI_GetResInfo(ModeNo, ModeIdIndex, pVBInfo);
2417
2418
2419    if (ModeNo <= 0x13) {
2420        modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
2421        tempax = pVBInfo->StResInfo[resindex].HTotal;
2422        tempbx = pVBInfo->StResInfo[resindex].VTotal;
2423    }
2424    else {
2425        modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
2426        tempax = pVBInfo->ModeResInfo[resindex].HTotal;
2427        tempbx = pVBInfo->ModeResInfo[resindex].VTotal;
2428    }
2429
2430    if (modeflag & HalfDCLK)
2431        tempax = tempax >> 1;
2432
2433    if (ModeNo > 0x13) {
2434        if (modeflag & HalfDCLK)
2435            tempax = tempax << 1;
2436
2437        temp = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
2438
2439        if (temp & InterlaceMode)
2440            tempbx = tempbx >> 1;
2441
2442        if (modeflag & DoubleScanMode)
2443            tempbx = tempbx << 1;
2444    }
2445
2446    tempcx = 8;
2447
2448    /* if ( !( modeflag & Charx8Dot ) ) */
2449    /* tempcx = 9 ; */
2450
2451    tempax /= tempcx;
2452    tempax -= 1;
2453    tempbx -= 1;
2454    tempcx = tempax;
2455    temp = (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x11);
2456    data = (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x11);
2457    data &= 0x7F;
2458    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x11, data);       /* Unlock CRTC */
2459    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x01, (USHORT) (tempcx & 0xff));
2460    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x0b, ~0x0c,
2461                    (USHORT) ((tempcx & 0x0ff00) >> 10));
2462    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x12, (USHORT) (tempbx & 0xff));
2463    tempax = 0;
2464    tempbx = tempbx >> 8;
2465
2466    if (tempbx & 0x01)
2467        tempax |= 0x02;
2468
2469    if (tempbx & 0x02)
2470        tempax |= 0x40;
2471
2472    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x07, ~0x42, tempax);
2473    data = (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x07);
2474    data &= 0xFF;
2475    tempax = 0;
2476
2477    if (tempbx & 0x04)
2478        tempax |= 0x02;
2479
2480    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x0a, ~0x02, tempax);
2481    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x11, temp);
2482}
2483
2484
2485/* --------------------------------------------------------------------- */
2486/* Function : XGI_GetResInfo */
2487/* Input : */
2488/* Output : */
2489/* Description : */
2490/* --------------------------------------------------------------------- */
2491USHORT
2492XGI_GetResInfo(USHORT ModeNo, USHORT ModeIdIndex, PVB_DEVICE_INFO pVBInfo)
2493{
2494    return (ModeNo <= 0x13)
2495        ? pVBInfo->SModeIDTable[ModeIdIndex].St_ResInfo
2496        : pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
2497}
2498
2499
2500static void
2501get_mode_xres_yres(USHORT ModeNo, USHORT ModeIdIndex, PVB_DEVICE_INFO pVBInfo,
2502                   unsigned *width, unsigned *height)
2503{
2504    const USHORT resindex = XGI_GetResInfo(ModeNo, ModeIdIndex, pVBInfo);
2505    unsigned xres;
2506    unsigned yres;
2507
2508
2509    if (ModeNo <= 0x13) {
2510        xres = pVBInfo->StResInfo[resindex].HTotal;
2511        yres = pVBInfo->StResInfo[resindex].VTotal;
2512    }
2513    else {
2514        const unsigned modeflag =
2515            pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
2516
2517        xres = pVBInfo->ModeResInfo[resindex].HTotal;
2518        yres = pVBInfo->ModeResInfo[resindex].VTotal;
2519
2520        if (modeflag & HalfDCLK)
2521            xres *= 2;
2522
2523        if (modeflag & DoubleScanMode)
2524            yres *= 2;
2525    }
2526
2527    *width = xres;
2528    *height = yres;
2529}
2530
2531
2532/* --------------------------------------------------------------------- */
2533/* Function : XGI_SetCRT1Offset */
2534/* Input : */
2535/* Output : */
2536/* Description : */
2537/* --------------------------------------------------------------------- */
2538void
2539XGI_SetCRT1Offset(USHORT ModeNo, USHORT ModeIdIndex,
2540                  USHORT RefreshRateTableIndex,
2541                  PXGI_HW_DEVICE_INFO HwDeviceExtension,
2542                  PVB_DEVICE_INFO pVBInfo)
2543{
2544    USHORT temp, ah, al, temp2, i, DisplayUnit;
2545
2546    /* GetOffset */
2547    temp = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeInfo;
2548    temp = temp >> 8;
2549    temp = pVBInfo->ScreenOffset[temp];
2550
2551    temp2 = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
2552    temp2 &= InterlaceMode;
2553
2554    if (temp2)
2555        temp = temp << 1;
2556
2557    temp2 = pVBInfo->ModeType - ModeEGA;
2558
2559    switch (temp2) {
2560    case 0:
2561        temp2 = 1;
2562        break;
2563    case 1:
2564        temp2 = 2;
2565        break;
2566    case 2:
2567        temp2 = 4;
2568        break;
2569    case 3:
2570        temp2 = 4;
2571        break;
2572    case 4:
2573        temp2 = 6;
2574        break;
2575    case 5:
2576        temp2 = 8;
2577        break;
2578    default:
2579        break;
2580    }
2581
2582    if ((ModeNo >= 0x26) && (ModeNo <= 0x28))
2583        temp = temp * temp2 + temp2 / 2;
2584    else
2585        temp *= temp2;
2586
2587    /* SetOffset */
2588    DisplayUnit = temp;
2589    temp2 = temp;
2590    temp = temp >> 8;           /* ah */
2591    temp &= 0x0F;
2592    i = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x0E);
2593    i &= 0xF0;
2594    i |= temp;
2595    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x0E, i);
2596
2597    temp = (UCHAR) temp2;
2598    temp &= 0xFF;               /* al */
2599    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x13, temp);
2600
2601    /* SetDisplayUnit */
2602    temp2 = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
2603    temp2 &= InterlaceMode;
2604    if (temp2)
2605        DisplayUnit >>= 1;
2606
2607    DisplayUnit = DisplayUnit << 5;
2608    ah = (DisplayUnit & 0xff00) >> 8;
2609    al = DisplayUnit & 0x00ff;
2610    if (al == 0)
2611        ah += 1;
2612    else
2613        ah += 2;
2614
2615    /* Jong 10/04/2007; merge code */
2616    if (HwDeviceExtension->jChipType >= XG20)
2617        if ((ModeNo == 0x4A) | (ModeNo == 0x49))
2618            ah -= 1;
2619
2620    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x10, ah);
2621}
2622
2623
2624/* --------------------------------------------------------------------- */
2625/* Function : XGI_SetCRT1VCLK */
2626/* Input : */
2627/* Output : */
2628/* Description : */
2629/* --------------------------------------------------------------------- */
2630void
2631XGI_SetCRT1VCLK(USHORT ModeNo, USHORT ModeIdIndex,
2632                PXGI_HW_DEVICE_INFO HwDeviceExtension,
2633                USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo)
2634{
2635    unsigned index;
2636    unsigned clka;
2637    unsigned clkb;
2638    unsigned data; /* Jong 10/04/2007; merge code */
2639
2640    /* Jong 10/04/2007; merge code */
2641    if ( pVBInfo->IF_DEF_LVDS == 1 )
2642    {
2643        index = pVBInfo->RefIndex[ RefreshRateTableIndex ].Ext_CRTVCLK ;
2644        clka = pVBInfo->VCLKData[ index ].SR2B;
2645        clkb = pVBInfo->VCLKData[ index ].SR2C;
2646    }
2647    else if ((pVBInfo->VBType & VB_XGI301BLV302BLV)
2648        && (pVBInfo->VBInfo & SetCRT2ToLCDA)) {
2649        index = XGI_GetVCLK2Ptr(ModeNo, ModeIdIndex, RefreshRateTableIndex,
2650                                pVBInfo);
2651
2652        clka = pVBInfo->VBVCLKData[index].Part4_A;
2653        clkb = pVBInfo->VBVCLKData[index].Part4_B;
2654    }
2655    else {
2656        index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
2657
2658        clka = pVBInfo->VCLKData[index].SR2B;
2659        clkb = pVBInfo->VCLKData[index].SR2C;
2660    }
2661
2662    XGI_SetRegAND((XGIIOADDRESS) pVBInfo->P3c4, 0x31, 0xCF);
2663    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x2B, clka);
2664    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x2C, clkb);
2665    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x2D, 0x01);
2666
2667    /* Jong 10/04/2007; merge code */
2668    if ((HwDeviceExtension->jChipType >= XG20)
2669         && (pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag & HalfDCLK)) {
2670        UCHAR data;
2671
2672        /* FIXME: Does this actually serve any purpose?  This register is
2673         * FIXME: already written above.
2674         */
2675        data = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x2B);
2676        XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x2B, data);
2677
2678        /* FIXME: The logic here seems wrong.  It looks like its possible
2679         * FIXME: for the (data << 1) to cause a bit to creep into the index
2680         * FIXME: part.  THere's no documentation for this register, so I have
2681         * FIXME: no way of knowing. :(
2682         */
2683        data = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x2C);
2684        index = data;
2685        index &= 0xE0;
2686        data &= 0x1F;
2687        data = data << 1;
2688        data += 1;
2689        data |= index;
2690        XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x2C, data);
2691    }
2692}
2693
2694
2695/* --------------------------------------------------------------------- */
2696/* Function : XGI_SetCRT1FIFO */
2697/* Input : */
2698/* Output : */
2699/* Description : */
2700/* --------------------------------------------------------------------- */
2701void
2702XGI_SetCRT1FIFO(USHORT ModeNo, PXGI_HW_DEVICE_INFO HwDeviceExtension,
2703                PVB_DEVICE_INFO pVBInfo)
2704{
2705    USHORT data;
2706
2707    data = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x3D);
2708    data &= 0xfe;
2709    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x3D, data);       /* diable auto-threshold */
2710
2711    if (ModeNo > 0x13) {
2712        XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x08, 0x34);
2713        data = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x09);
2714
2715        /* Jong 02/06/2009; performance; add 10 for WinBench 99 */
2716		/* Jong 02/13/2009; might cause threshold noise of display; not sure */
2717        /* data &= 0xF0;
2718        XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x09, data); */
2719        data &= 0xC0 ;
2720        XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4 , 0x09 , data | 0x30) ;
2721
2722        data = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x3D);
2723        data |= 0x01;
2724        XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x3D, data);
2725    }
2726    else {
2727        if (HwDeviceExtension->jChipType == XG27)
2728        {
2729			  XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x08 , 0x0E ) ;
2730			  data = XGI_GetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x09 ) ;
2731			  data &= 0xC0 ;
2732			  XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x09 , data | 0x20 ) ;
2733        }
2734        else
2735        {
2736			XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x08, 0xAE);
2737			data = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x09);
2738			data &= 0xF0;
2739			XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x09, data);
2740        }
2741    }
2742
2743	/* Jong 10/17/2007; merge code */
2744    if (HwDeviceExtension->jChipType == XG21)
2745    {
2746        XGI_SetXG21FPBits(pVBInfo);                 /* Fix SR9[7:6] can't read back */
2747    }
2748
2749}
2750
2751
2752/* --------------------------------------------------------------------- */
2753/* Function : XGI_SetCRT1ModeRegs */
2754/* Input : */
2755/* Output : */
2756/* Description : */
2757/* --------------------------------------------------------------------- */
2758void
2759XGI_SetCRT1ModeRegs(PXGI_HW_DEVICE_INFO HwDeviceExtension,
2760                    USHORT ModeNo, USHORT ModeIdIndex,
2761                    USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo)
2762{
2763    USHORT data, data2, data3, infoflag = 0, modeflag, resindex, xres;
2764
2765    if (ModeNo > 0x13) {
2766        modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
2767        infoflag = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
2768    }
2769    else
2770        modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;      /* si+St_ModeFlag */
2771
2772    if (XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x31) & 0x01)
2773        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x1F, 0x3F, 0x00);
2774
2775    if (ModeNo > 0x13)
2776        data = infoflag;
2777    else
2778        data = 0;
2779
2780    data2 = 0;
2781
2782    if (ModeNo > 0x13) {
2783        if (pVBInfo->ModeType > 0x02) {
2784            data2 |= 0x02;
2785            data3 = pVBInfo->ModeType - ModeVGA;
2786            data3 = data3 << 2;
2787            data2 |= data3;
2788        }
2789    }
2790
2791    data &= InterlaceMode;
2792
2793    if (data)
2794        data2 |= 0x20;
2795
2796    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x06, ~0x3F, data2);
2797    /* XGI_SetReg((XGIIOADDRESS)pVBInfo->P3c4,0x06,data2); */
2798    resindex = XGI_GetResInfo(ModeNo, ModeIdIndex, pVBInfo);
2799    if (ModeNo <= 0x13)
2800        xres = pVBInfo->StResInfo[resindex].HTotal;
2801    else
2802        xres = pVBInfo->ModeResInfo[resindex].HTotal;   /* xres->ax */
2803
2804    data = 0x0000;
2805    if (infoflag & InterlaceMode) {
2806        if (xres == 1024)
2807            data = 0x0035;
2808        else if (xres == 1280)
2809            data = 0x0048;
2810    }
2811
2812    data2 = data & 0x00FF;
2813    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x19, 0xFF, data2);
2814    data2 = (data & 0xFF00) >> 8;
2815    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x19, 0xFC, data2);
2816
2817    if (modeflag & HalfDCLK)
2818        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x01, 0xF7, 0x08);
2819
2820    data2 = 0;
2821
2822    if (modeflag & LineCompareOff)
2823        data2 |= 0x08;
2824
2825    if (ModeNo > 0x13) {
2826        if (pVBInfo->ModeType == ModeEGA)
2827            data2 |= 0x40;
2828    }
2829
2830    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x0F, ~0x48, data2);
2831    data = 0x60;
2832    if (pVBInfo->ModeType != ModeText) {
2833        data = data ^ 0x60;
2834        if (pVBInfo->ModeType != ModeEGA) {
2835            data = data ^ 0xA0;
2836        }
2837    }
2838    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x21, 0x1F, data);
2839
2840    XGI_SetVCLKState(HwDeviceExtension, ModeNo, RefreshRateTableIndex,
2841                     pVBInfo);
2842
2843    data = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x31);
2844
2845    /* Jong 10/04/2007; merge code */
2846    if (HwDeviceExtension->jChipType == XG27 )
2847    {
2848    	if ( data & 0x40 )
2849    	    data = 0x2c ;
2850    	else
2851    	    data = 0x6c ;
2852    	XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x52 , data ) ;
2853    	XGI_SetRegOR( (XGIIOADDRESS) pVBInfo->P3d4 , 0x51 , 0x10 ) ;
2854    }
2855    else if (HwDeviceExtension->jChipType >= XG20)
2856    {
2857        if (data & 0x40)
2858            data = 0x33;
2859        else
2860            data = 0x73;
2861        XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x52, data);
2862        XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x51, 0x02);
2863    }
2864    else {
2865        if (data & 0x40)
2866            data = 0x2c;
2867        else
2868            data = 0x6c;
2869        XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x52, data);
2870    }
2871
2872}
2873
2874
2875/* --------------------------------------------------------------------- */
2876/* Function : XGI_SetVCLKState */
2877/* Input : */
2878/* Output : */
2879/* Description : */
2880/* --------------------------------------------------------------------- */
2881void
2882XGI_SetVCLKState(PXGI_HW_DEVICE_INFO HwDeviceExtension, USHORT ModeNo,
2883                 USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo)
2884{
2885    USHORT data, data2 = 0;
2886    SHORT VCLK;
2887
2888    UCHAR index;
2889
2890    if (ModeNo <= 0x13)
2891        VCLK = 0;
2892    else {
2893        index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
2894        index &= IndexMask;
2895        VCLK = pVBInfo->VCLKData[index].CLOCK;
2896    }
2897
2898    data = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x32);
2899    data &= 0xf3;
2900    if (VCLK >= 200)
2901        data |= 0x0c;           /* VCLK > 200 */
2902
2903    /* Jong 10/04/2007; merge code */
2904    if (HwDeviceExtension->jChipType >= XG20)
2905        data &= ~0x04;          /* 2 pixel mode */
2906
2907    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x32, data);
2908
2909    /* Jong 10/04/2007; merge code */
2910    if (HwDeviceExtension->jChipType < XG20) {
2911        data = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x1F);
2912        data &= 0xE7;
2913        if (VCLK < 200)
2914            data |= 0x10;
2915        XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x1F, data);
2916    }
2917
2918    /*  Jong for Adavantech LCD ripple issue
2919    if ((VCLK >= 0) && (VCLK < 135))
2920        data2 = 0x03;
2921    else if ((VCLK >= 135) && (VCLK < 160))
2922        data2 = 0x02;
2923    else if ((VCLK >= 160) && (VCLK < 260))
2924        data2 = 0x01;
2925    else if (VCLK > 260)
2926        data2 = 0x00; */
2927
2928    data2 = 0x00 ;
2929
2930    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x07, 0xFC, data2);
2931
2932    /* Jong 10/04/2007; merge code */
2933    if (HwDeviceExtension->jChipType >= XG27 )
2934    {
2935      XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x40 , 0xFC , data2&0x03 ) ;
2936    }
2937
2938}
2939
2940
2941/* --------------------------------------------------------------------- */
2942/* Function : XGI_LoadDAC */
2943/* Input : */
2944/* Output : */
2945/* Description : */
2946/* --------------------------------------------------------------------- */
2947void
2948XGI_LoadDAC(USHORT ModeNo, USHORT ModeIdIndex, PVB_DEVICE_INFO pVBInfo)
2949{
2950    USHORT data, data2, time, i, j, k, m, n, o, si, di, bx, dl, al, ah, dh;
2951    const uint8_t *table = NULL;
2952
2953    if (ModeNo <= 0x13)
2954        data = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
2955    else
2956        data = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
2957
2958    data &= DACInfoFlag;
2959    time = 64;
2960
2961    if (data == 0x00)
2962        table = XGI_MDA_DAC;
2963    else if (data == 0x08)
2964        table = XGI_CGA_DAC;
2965    else if (data == 0x10)
2966        table = XGI_EGA_DAC;
2967    else if (data == 0x18) {
2968        time = 256;
2969        table = XGI_VGA_DAC;
2970    }
2971
2972    if (time == 256)
2973        j = 16;
2974    else
2975        j = time;
2976
2977    XGI_SetRegByte((XGIIOADDRESS) pVBInfo->P3c6, 0xFF);
2978    XGI_SetRegByte((XGIIOADDRESS) pVBInfo->P3c8, 0x00);
2979
2980    for (i = 0; i < j; i++) {
2981        data = table[i];
2982
2983        for (k = 0; k < 3; k++) {
2984            data2 = 0;
2985
2986            if (data & 0x01)
2987                data2 = 0x2A;
2988
2989            if (data & 0x02)
2990                data2 += 0x15;
2991
2992            XGI_SetRegByte((XGIIOADDRESS) pVBInfo->P3c9, data2);
2993            data = data >> 2;
2994        }
2995    }
2996
2997    if (time == 256) {
2998        for (i = 16; i < 32; i++) {
2999            data = table[i];
3000
3001            for (k = 0; k < 3; k++)
3002                XGI_SetRegByte((XGIIOADDRESS) pVBInfo->P3c9, data);
3003        }
3004
3005        si = 32;
3006
3007        for (m = 0; m < 9; m++) {
3008            di = si;
3009            bx = si + 0x04;
3010            dl = 0;
3011
3012            for (n = 0; n < 3; n++) {
3013                for (o = 0; o < 5; o++) {
3014                    dh = table[si];
3015                    ah = table[di];
3016                    al = table[bx];
3017                    si++;
3018                    XGI_WriteDAC((XGIIOADDRESS) pVBInfo->P3c9, 0, dl,
3019				 ah, al, dh);
3020                }
3021
3022                si -= 2;
3023
3024                for (o = 0; o < 3; o++) {
3025                    dh = table[bx];
3026                    ah = table[di];
3027                    al = table[si];
3028                    si--;
3029                    XGI_WriteDAC((XGIIOADDRESS) pVBInfo->P3c9, 0, dl,
3030				 ah, al, dh);
3031                }
3032
3033                dl++;
3034            }
3035
3036            si += 5;
3037        }
3038    }
3039}
3040
3041
3042/* --------------------------------------------------------------------- */
3043/* Function : XGI_WriteDAC */
3044/* Input : */
3045/* Output : */
3046/* Description : */
3047/* --------------------------------------------------------------------- */
3048void
3049XGI_WriteDAC(XGIIOADDRESS dac_data, unsigned shift, unsigned ordering,
3050	     uint8_t ah, uint8_t al, uint8_t dh)
3051{
3052    USHORT temp, bh, bl;
3053
3054    if (shift) {
3055	ah <<= 2;
3056	al <<= 2;
3057	dh <<= 2;
3058    }
3059
3060    bh = ah;
3061    bl = al;
3062
3063    if (ordering != 0) {
3064        temp = bh;
3065        bh = dh;
3066        dh = temp;
3067        if (ordering == 1) {
3068            temp = bl;
3069            bl = dh;
3070            dh = temp;
3071        }
3072        else {
3073            temp = bl;
3074            bl = bh;
3075            bh = temp;
3076        }
3077    }
3078    XGI_SetRegByte(dac_data, dh);
3079    XGI_SetRegByte(dac_data, bh);
3080    XGI_SetRegByte(dac_data, bl);
3081}
3082
3083
3084/* --------------------------------------------------------------------- */
3085/* Function : XGI_SetLCDAGroup */
3086/* Input : */
3087/* Output : */
3088/* Description : */
3089/* --------------------------------------------------------------------- */
3090void
3091XGI_SetLCDAGroup(USHORT ModeNo, USHORT ModeIdIndex,
3092                 PXGI_HW_DEVICE_INFO HwDeviceExtension,
3093                 PVB_DEVICE_INFO pVBInfo)
3094{
3095    USHORT RefreshRateTableIndex;
3096    /* USHORT temp ; */
3097
3098    /* pVBInfo->SelectCRT2Rate = 0 ; */
3099
3100    pVBInfo->SetFlag |= ProgrammingCRT2;
3101    RefreshRateTableIndex = XGI_GetRatePtrCRT2(HwDeviceExtension, ModeNo, ModeIdIndex, pVBInfo);
3102    XGI_GetLVDSResInfo(ModeNo, ModeIdIndex, pVBInfo);
3103    XGI_GetLVDSData(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
3104    XGI_ModCRT1Regs(ModeNo, ModeIdIndex, RefreshRateTableIndex,
3105                    HwDeviceExtension, pVBInfo);
3106    XGI_SetLVDSRegs(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
3107    XGI_SetCRT2ECLK(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
3108}
3109
3110
3111/**
3112 * Get LVDS resolution information.
3113 */
3114void
3115XGI_GetLVDSResInfo(USHORT ModeNo, USHORT ModeIdIndex, PVB_DEVICE_INFO pVBInfo)
3116{
3117    unsigned xres;
3118    unsigned yres;
3119
3120
3121    get_mode_xres_yres(ModeNo, ModeIdIndex, pVBInfo, &xres, &yres);
3122
3123    if (xres == 720)
3124        xres = 640;
3125
3126    pVBInfo->VGAHDE = xres;
3127    pVBInfo->HDE = xres;
3128    pVBInfo->VGAVDE = yres;
3129    pVBInfo->VDE = yres;
3130}
3131
3132
3133static void
3134get_HDE_VDE(PVB_DEVICE_INFO pVBInfo, ULONG *HDE, ULONG *VDE)
3135{
3136    switch (pVBInfo->LCDResInfo) {
3137    case Panel1024x768:
3138    case Panel1024x768x75:
3139	*HDE = 1024;
3140	*VDE = 768;
3141	break;
3142
3143    case Panel1280x1024:
3144    case Panel1280x1024x75:
3145	*HDE = 1280;
3146	*VDE = 1024;
3147	break;
3148
3149    case Panel1400x1050:
3150	*HDE = 1400;
3151	*VDE = 1050;
3152	break;
3153
3154    default:
3155	*HDE = 1600;
3156	*VDE = 1200;
3157	break;
3158    }
3159}
3160
3161
3162/* --------------------------------------------------------------------- */
3163/* Function : XGI_GetLVDSData */
3164/* Input : */
3165/* Output : */
3166/* Description : */
3167/* --------------------------------------------------------------------- */
3168void
3169XGI_GetLVDSData(USHORT ModeNo, USHORT ModeIdIndex,
3170                USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo)
3171{
3172    USHORT tempbx;
3173    XGI330_LVDSDataStruct *LCDPtr = NULL;
3174
3175    tempbx = 2;
3176
3177    if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
3178        LCDPtr =
3179            (XGI330_LVDSDataStruct *) XGI_GetLcdPtr(tempbx, ModeNo,
3180                                                    ModeIdIndex,
3181                                                    RefreshRateTableIndex,
3182                                                    pVBInfo);
3183        pVBInfo->VGAHT = LCDPtr->VGAHT;
3184        pVBInfo->VGAVT = LCDPtr->VGAVT;
3185        pVBInfo->HT = LCDPtr->LCDHT;
3186        pVBInfo->VT = LCDPtr->LCDVT;
3187    }
3188
3189    if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
3190        if (!(pVBInfo->LCDInfo & (SetLCDtoNonExpanding | EnableScalingLCD))) {
3191	    get_HDE_VDE(pVBInfo, & pVBInfo->HDE, & pVBInfo->VDE);
3192        }
3193    }
3194}
3195
3196
3197/* --------------------------------------------------------------------- */
3198/* Function : XGI_ModCRT1Regs */
3199/* Input : */
3200/* Output : */
3201/* Description : */
3202/* --------------------------------------------------------------------- */
3203void
3204XGI_ModCRT1Regs(USHORT ModeNo, USHORT ModeIdIndex,
3205                USHORT RefreshRateTableIndex,
3206                PXGI_HW_DEVICE_INFO HwDeviceExtension,
3207                PVB_DEVICE_INFO pVBInfo)
3208{
3209    UCHAR index;
3210    USHORT tempbx, i;
3211    XGI_LVDSCRT1HDataStruct *LCDPtr = NULL;
3212    XGI_LVDSCRT1VDataStruct *LCDPtr1 = NULL;
3213    /* XGI330_CHTVDataStruct *TVPtr = NULL ; */
3214
3215    if (ModeNo <= 0x13)
3216        index = pVBInfo->SModeIDTable[ModeIdIndex].St_CRT2CRTC;
3217    else
3218        index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
3219
3220    index = index & IndexMask;
3221
3222    if ((pVBInfo->IF_DEF_ScaleLCD == 0)
3223        || ((pVBInfo->IF_DEF_ScaleLCD == 1)
3224            && (!(pVBInfo->LCDInfo & EnableScalingLCD)))) {
3225        tempbx = 0;
3226
3227        if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
3228            LCDPtr =
3229                (XGI_LVDSCRT1HDataStruct *) XGI_GetLcdPtr(tempbx, ModeNo,
3230                                                          ModeIdIndex,
3231                                                          RefreshRateTableIndex,
3232                                                          pVBInfo);
3233
3234            for (i = 0; i < 8; i++)
3235                pVBInfo->TimingH.data[i] = LCDPtr[0].Reg[i];
3236        }
3237
3238        XGI_SetCRT1Timing_H(pVBInfo, HwDeviceExtension);
3239
3240        tempbx = 1;
3241
3242        if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
3243            LCDPtr1 =
3244                (XGI_LVDSCRT1VDataStruct *) XGI_GetLcdPtr(tempbx, ModeNo,
3245                                                          ModeIdIndex,
3246                                                          RefreshRateTableIndex,
3247                                                          pVBInfo);
3248            for (i = 0; i < 7; i++)
3249                pVBInfo->TimingV.data[i] = LCDPtr1[0].Reg[i];
3250        }
3251
3252        XGI_SetCRT1Timing_V(ModeIdIndex, ModeNo, pVBInfo);
3253    }
3254}
3255
3256
3257
3258/* --------------------------------------------------------------------- */
3259/* Function : XGI_SetLVDSRegs */
3260/* Input : */
3261/* Output : */
3262/* Description : */
3263/* --------------------------------------------------------------------- */
3264void
3265XGI_SetLVDSRegs(USHORT ModeNo, USHORT ModeIdIndex,
3266                USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo)
3267{
3268    ULONG tempbx, tempax, tempcx, tempdx, push1, push2, modeflag;
3269    unsigned long temp, temp1, temp2, temp3, push3;
3270    XGI330_LCDDataDesStruct *LCDPtr = NULL;
3271    XGI330_LCDDataDesStruct2 *LCDPtr1 = NULL;
3272
3273    if (ModeNo > 0x13)
3274        modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
3275    else
3276        modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
3277
3278    if (!(pVBInfo->SetFlag & Win9xDOSMode)) {
3279        if (pVBInfo->IF_DEF_OEMUtil == 1) {
3280            tempbx = 8;
3281            LCDPtr =
3282                (XGI330_LCDDataDesStruct *) XGI_GetLcdPtr(tempbx, ModeNo,
3283                                                          ModeIdIndex,
3284                                                          RefreshRateTableIndex,
3285                                                          pVBInfo);
3286        }
3287
3288        if ((pVBInfo->IF_DEF_OEMUtil == 0) || (LCDPtr == 0)) {
3289            tempbx = 3;
3290            if (pVBInfo->LCDInfo & EnableScalingLCD)
3291                LCDPtr1 =
3292                    (XGI330_LCDDataDesStruct2 *) XGI_GetLcdPtr(tempbx, ModeNo,
3293                                                               ModeIdIndex,
3294                                                               RefreshRateTableIndex,
3295                                                               pVBInfo);
3296            else
3297                LCDPtr =
3298                    (XGI330_LCDDataDesStruct *) XGI_GetLcdPtr(tempbx, ModeNo,
3299                                                              ModeIdIndex,
3300                                                              RefreshRateTableIndex,
3301                                                              pVBInfo);
3302        }
3303
3304        XGI_GetLCDSync(&tempax, &tempbx, pVBInfo);
3305        push1 = tempbx;
3306        push2 = tempax;
3307
3308        /* GetLCDResInfo */
3309        if (pVBInfo->LCDInfo & SetLCDtoNonExpanding) {
3310	    get_HDE_VDE(pVBInfo, & pVBInfo->HDE, & pVBInfo->VDE);
3311
3312            pVBInfo->VGAHDE = pVBInfo->HDE;
3313            pVBInfo->VGAVDE = pVBInfo->VDE;
3314        }
3315
3316        tempax = pVBInfo->HT;
3317
3318	tempbx = (pVBInfo->LCDInfo & EnableScalingLCD)
3319	    ? LCDPtr1->LCDHDES : LCDPtr->LCDHDES;
3320
3321        tempcx = pVBInfo->HDE;
3322        tempbx = tempbx & 0x0fff;
3323        tempcx += tempbx;
3324
3325        if (tempcx >= tempax)
3326            tempcx -= tempax;
3327
3328        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x1A, tempbx & 0x07);
3329
3330        tempcx = tempcx >> 3;
3331        tempbx = tempbx >> 3;
3332
3333        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x16,
3334                   (USHORT) (tempbx & 0xff));
3335        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x17,
3336                   (USHORT) (tempcx & 0xff));
3337
3338        tempax = pVBInfo->HT;
3339
3340        if (pVBInfo->LCDInfo & EnableScalingLCD)
3341            tempbx = LCDPtr1->LCDHRS;
3342        else
3343            tempbx = LCDPtr->LCDHRS;
3344
3345        tempcx = push2;
3346
3347        if (pVBInfo->LCDInfo & EnableScalingLCD)
3348            tempcx = LCDPtr1->LCDHSync;
3349
3350        tempcx += tempbx;
3351
3352        if (tempcx >= tempax)
3353            tempcx -= tempax;
3354
3355	/* FIXME: Won't this *always* set tempax to zero? */
3356        tempax = tempbx & 0x07;
3357        tempax = tempax >> 5;
3358        tempcx = tempcx >> 3;
3359        tempbx = tempbx >> 3;
3360
3361        tempcx &= 0x1f;
3362        tempax |= tempcx;
3363
3364        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x15, tempax);
3365        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x14,
3366                   (USHORT) (tempbx & 0xff));
3367
3368        tempax = pVBInfo->VT;
3369        if (pVBInfo->LCDInfo & EnableScalingLCD)
3370            tempbx = LCDPtr1->LCDVDES;
3371        else
3372            tempbx = LCDPtr->LCDVDES;
3373        tempcx = pVBInfo->VDE;
3374
3375        tempbx = tempbx & 0x0fff;
3376        tempcx += tempbx;
3377        if (tempcx >= tempax)
3378            tempcx -= tempax;
3379
3380        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x1b,
3381                   (USHORT) (tempbx & 0xff));
3382        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x1c,
3383                   (USHORT) (tempcx & 0xff));
3384
3385        tempbx = (tempbx >> 8) & 0x07;
3386        tempcx = (tempcx >> 8) & 0x07;
3387
3388        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x1d,
3389                   (USHORT) ((tempcx << 3) | tempbx));
3390
3391        tempax = pVBInfo->VT;
3392        if (pVBInfo->LCDInfo & EnableScalingLCD)
3393            tempbx = LCDPtr1->LCDVRS;
3394        else
3395            tempbx = LCDPtr->LCDVRS;
3396
3397        /* tempbx = tempbx >> 4 ; */
3398        tempcx = push1;
3399
3400        if (pVBInfo->LCDInfo & EnableScalingLCD)
3401            tempcx = LCDPtr1->LCDVSync;
3402
3403        tempcx += tempbx;
3404        if (tempcx >= tempax)
3405            tempcx -= tempax;
3406
3407        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x18,
3408                   (USHORT) (tempbx & 0xff));
3409        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x19, ~0x0f,
3410                        (USHORT) (tempcx & 0x0f));
3411
3412        tempax = ((tempbx >> 8) & 0x07) << 3;
3413
3414        tempbx = pVBInfo->VGAVDE;
3415        if (tempbx != pVBInfo->VDE)
3416            tempax |= 0x40;
3417
3418        if (pVBInfo->LCDInfo & EnableLVDSDDA)
3419            tempax |= 0x40;
3420
3421        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x1a, 0x07,
3422                        tempax);
3423
3424        tempcx = pVBInfo->VGAVT;
3425        tempbx = pVBInfo->VDE;
3426        tempax = pVBInfo->VGAVDE;
3427        tempcx -= tempax;
3428
3429        temp = tempax;          /* 0430 ylshieh */
3430        temp1 = (temp << 18) / tempbx;
3431
3432        tempdx = (USHORT) ((temp << 18) % tempbx);
3433
3434        if (tempdx != 0)
3435            temp1 += 1;
3436
3437        temp2 = temp1;
3438        push3 = temp2;
3439
3440        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x37,
3441                   (USHORT) (temp2 & 0xff));
3442        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x36,
3443                   (USHORT) ((temp2 >> 8) & 0xff));
3444
3445        tempbx = (USHORT) (temp2 >> 16);
3446        tempax = tempbx & 0x03;
3447
3448        tempbx = pVBInfo->VGAVDE;
3449        if (tempbx == pVBInfo->VDE)
3450            tempax |= 0x04;
3451
3452        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x35, tempax);
3453
3454        if (pVBInfo->VBType & VB_XGI301C) {
3455            temp2 = push3;
3456            XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x3c,
3457                       (USHORT) (temp2 & 0xff));
3458            XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x3b,
3459                       (USHORT) ((temp2 >> 8) & 0xff));
3460            tempbx = (USHORT) (temp2 >> 16);
3461            XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part4Port, 0x3a, ~0xc0,
3462                            (USHORT) ((tempbx & 0xff) << 6));
3463
3464            tempcx = pVBInfo->VGAVDE;
3465            if (tempcx == pVBInfo->VDE)
3466                XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part4Port, 0x30,
3467                                ~0x0c, 0x00);
3468            else
3469                XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part4Port, 0x30,
3470                                ~0x0c, 0x08);
3471        }
3472
3473        tempcx = pVBInfo->VGAHDE;
3474        tempbx = pVBInfo->HDE;
3475
3476        temp1 = tempcx << 16;
3477
3478        tempax = (USHORT) (temp1 / tempbx);
3479
3480        if ((tempbx & 0xffff) == (tempcx & 0xffff))
3481            tempax = 65535;
3482
3483        temp3 = tempax;
3484        temp1 = pVBInfo->VGAHDE << 16;
3485
3486        temp1 /= temp3;
3487        temp3 = temp3 << 16;
3488        temp1 -= 1;
3489
3490        temp3 = (temp3 & 0xffff0000) + (temp1 & 0xffff);
3491
3492        tempax = (USHORT) (temp3 & 0xff);
3493        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x1f, tempax);
3494
3495        temp1 = pVBInfo->VGAVDE << 18;
3496        temp1 = temp1 / push3;
3497        tempbx = (USHORT) (temp1 & 0xffff);
3498
3499        if (pVBInfo->LCDResInfo == Panel1024x768)
3500            tempbx -= 1;
3501
3502        tempax = ((tempbx >> 8) & 0xff) << 3;
3503        tempax |= (USHORT) ((temp3 >> 8) & 0x07);
3504        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x20,
3505                   (USHORT) (tempax & 0xff));
3506        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x21,
3507                   (USHORT) (tempbx & 0xff));
3508
3509        temp3 = temp3 >> 16;
3510
3511        if (modeflag & HalfDCLK)
3512            temp3 = temp3 >> 1;
3513
3514        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x22,
3515                   (USHORT) ((temp3 >> 8) & 0xff));
3516        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x23,
3517                   (USHORT) (temp3 & 0xff));
3518    }
3519}
3520
3521
3522/* --------------------------------------------------------------------- */
3523/* Function : XGI_SetCRT2ECLK */
3524/* Input : */
3525/* Output : */
3526/* Description : */
3527/* --------------------------------------------------------------------- */
3528void
3529XGI_SetCRT2ECLK(USHORT ModeNo, USHORT ModeIdIndex,
3530                USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo)
3531{
3532    UCHAR di[2];
3533    int i;
3534    const unsigned vclkindex =
3535        XGI_GetVCLKPtr(RefreshRateTableIndex, ModeNo, ModeIdIndex, pVBInfo);
3536
3537    XGI_GetVCLKLen(vclkindex, di, pVBInfo);
3538    XGI_GetLCDVCLKPtr(di, pVBInfo);
3539
3540    for (i = 0; i < 4; i++) {
3541        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x31, ~0x30,
3542                        (USHORT) (0x10 * i));
3543        if ((!(pVBInfo->VBInfo & SetCRT2ToLCDA))
3544            && (!(pVBInfo->VBInfo & SetInSlaveMode))) {
3545            XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x2e, di[0]);
3546            XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x2f, di[1]);
3547        }
3548        else {
3549            XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x2b, di[0]);
3550            XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x2c, di[1]);
3551        }
3552    }
3553}
3554
3555
3556/* --------------------------------------------------------------------- */
3557/* Function : XGI_UpdateModeInfo */
3558/* Input : */
3559/* Output : */
3560/* Description : */
3561/* --------------------------------------------------------------------- */
3562void
3563XGI_UpdateModeInfo(PXGI_HW_DEVICE_INFO HwDeviceExtension,
3564                   PVB_DEVICE_INFO pVBInfo)
3565{
3566    USHORT tempcl, tempch, temp, tempbl, tempax;
3567
3568    if (pVBInfo->
3569        VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV |
3570                  VB_XGI301C)) {
3571        tempcl = 0;
3572        tempch = 0;
3573        temp = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x01);
3574
3575        if (!(temp & 0x20)) {
3576            temp = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x17);
3577            if (temp & 0x80) {
3578                /* Jong 10/04/2007; merge code */
3579                if ((HwDeviceExtension->jChipType >= XG20)
3580                    || (HwDeviceExtension->jChipType >= XG40))
3581                    temp = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x53);
3582                else
3583                    temp = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x63);
3584
3585                if (!(temp & 0x40))
3586                    tempcl |= ActiveCRT1;
3587            }
3588        }
3589
3590        temp = XGI_GetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x2e);
3591        temp &= 0x0f;
3592
3593        if (!(temp == 0x08)) {
3594            tempax = XGI_GetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x13);       /* Check ChannelA by Part1_13 [2003/10/03] */
3595            if (tempax & 0x04)
3596                tempcl = tempcl | ActiveLCD;
3597
3598            temp &= 0x05;
3599
3600            if (!(tempcl & ActiveLCD))
3601                if (temp == 0x01)
3602                    tempcl |= ActiveCRT2;
3603
3604            if (temp == 0x04)
3605                tempcl |= ActiveLCD;
3606
3607            if (temp == 0x05) {
3608                temp = XGI_GetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x00);
3609
3610                if (!(temp & 0x08))
3611                    tempch |= ActiveAVideo;
3612
3613                if (!(temp & 0x04))
3614                    tempch |= ActiveSVideo;
3615
3616                if (temp & 0x02)
3617                    tempch |= ActiveSCART;
3618
3619                if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {
3620                    if (temp & 0x01)
3621                        tempch |= ActiveHiTV;
3622                }
3623
3624                if (pVBInfo->VBInfo & SetCRT2ToYPbPr) {
3625                    temp =
3626                        XGI_GetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x4d);
3627
3628                    if (temp & 0x10)
3629                        tempch |= ActiveYPbPr;
3630                }
3631
3632                if (tempch != 0)
3633                    tempcl |= ActiveTV;
3634            }
3635        }
3636
3637        temp = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x3d);
3638        if (tempcl & ActiveLCD) {
3639            if ((pVBInfo->SetFlag & ReserveTVOption)) {
3640                if (temp & ActiveTV)
3641                    tempcl |= ActiveTV;
3642            }
3643        }
3644        temp = tempcl;
3645        tempbl = ~ModeSwitchStatus;
3646        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x3d, tempbl, temp);
3647
3648        if (!(pVBInfo->SetFlag & ReserveTVOption))
3649            XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x3e, tempch);
3650    }
3651    else {
3652        return;
3653    }
3654}
3655
3656
3657/* --------------------------------------------------------------------- */
3658/* Function : XGI_GetVBType */
3659/* Input : */
3660/* Output : */
3661/* Description : */
3662/* --------------------------------------------------------------------- */
3663void
3664XGI_GetVBType(PVB_DEVICE_INFO pVBInfo)
3665{
3666    USHORT flag, tempbx, tempah;
3667
3668   /* Jong 10/04/2007; merge code */
3669   if ( pVBInfo->IF_DEF_LVDS == 0 )
3670   {
3671    tempbx = VB_XGI302B;
3672    flag = XGI_GetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x00);
3673    if (flag != 0x02) {
3674	tempbx = VB_XGI301;
3675	flag = XGI_GetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x01);
3676	if (flag >= 0xB0) {
3677	    tempbx = VB_XGI301B;
3678	    if (flag >= 0xC0) {
3679		tempbx = VB_XGI301C;
3680		if (flag >= 0xD0) {
3681		    tempbx = VB_XGI301LV;
3682		    if (flag >= 0xE0) {
3683			tempbx = VB_XGI302LV;
3684			tempah = XGI_GetReg((XGIIOADDRESS) pVBInfo->Part4Port,
3685					    0x39);
3686			if (tempah != 0xFF)
3687			    tempbx = VB_XGI301C;
3688		    }
3689		}
3690	    }
3691
3692	    if (tempbx & (VB_XGI301B | VB_XGI302B)) {
3693		flag = XGI_GetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x23);
3694
3695		if (!(flag & 0x02))
3696		    tempbx = tempbx | VB_NoLCD;
3697	    }
3698	}
3699    }
3700
3701    pVBInfo->VBType = tempbx;
3702   }
3703}
3704
3705
3706/* --------------------------------------------------------------------- */
3707/* Function : XGI_GetVBInfo */
3708/* Input : */
3709/* Output : */
3710/* Description : */
3711/* --------------------------------------------------------------------- */
3712void
3713XGI_GetVBInfo(USHORT ModeNo, USHORT ModeIdIndex,
3714              PXGI_HW_DEVICE_INFO HwDeviceExtension, PVB_DEVICE_INFO pVBInfo)
3715{
3716    USHORT tempax, push, tempbx, temp, modeflag;
3717
3718    if (ModeNo <= 0x13) {
3719        modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
3720    }
3721    else {
3722        modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
3723    }
3724
3725    pVBInfo->SetFlag = 0;
3726    pVBInfo->ModeType = modeflag & ModeInfoFlag;
3727    tempbx = 0;
3728
3729    if (pVBInfo->VBType & 0xFFFF) {
3730        temp = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x30);  /* Check Display Device */
3731        tempbx = tempbx | temp;
3732        temp = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x31);
3733        push = temp;
3734        push = push << 8;
3735        tempax = temp << 8;
3736        tempbx = tempbx | tempax;
3737        temp =
3738            (SetCRT2ToDualEdge | SetCRT2ToYPbPr | SetCRT2ToLCDA |
3739             SetInSlaveMode | DisableCRT2Display);
3740        temp = 0xFFFF ^ temp;
3741        tempbx &= temp;
3742
3743        temp = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x38);
3744
3745        if (pVBInfo->IF_DEF_LCDA == 1) {
3746            /* if ( ( pVBInfo->VBType & VB_XGI302B ) || ( pVBInfo->VBType & VB_XGI301LV ) || ( pVBInfo->VBType & VB_XGI302LV ) || ( pVBInfo->VBType & VB_XGI301C ) ) */
3747            if (pVBInfo->
3748                VBType & (VB_XGI302B | VB_XGI301LV | VB_XGI302LV |
3749                          VB_XGI301C)) {
3750                if (temp & EnableDualEdge) {
3751                    tempbx |= SetCRT2ToDualEdge;
3752
3753                    if (temp & SetToLCDA)
3754                        tempbx |= SetCRT2ToLCDA;
3755                }
3756            }
3757        }
3758
3759        if (pVBInfo->IF_DEF_YPbPr == 1) {
3760            if ((pVBInfo->VBType & VB_XGI301LV)
3761                || (pVBInfo->VBType & VB_XGI302LV)
3762                || (pVBInfo->VBType & VB_XGI301C)) {
3763                if (temp & SetYPbPr) {  /* temp = CR38 */
3764                    if (pVBInfo->IF_DEF_HiVision == 1) {
3765                        temp = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x35);  /* shampoo add for new scratch */
3766                        temp &= YPbPrMode;
3767                        tempbx |= SetCRT2ToHiVisionTV;
3768
3769                        if (temp != YPbPrMode1080i) {
3770                            tempbx &= (~SetCRT2ToHiVisionTV);
3771                            tempbx |= SetCRT2ToYPbPr;
3772                        }
3773                    }
3774
3775                    /* tempbx |= SetCRT2ToYPbPr ; */
3776                }
3777            }
3778        }
3779
3780        tempax = push;          /* restore CR31 */
3781
3782       /* Jong 10/04/2007; merge code */
3783       if ( pVBInfo->IF_DEF_LVDS == 0 )
3784       {
3785        if (pVBInfo->IF_DEF_YPbPr == 1) {
3786            if (pVBInfo->IF_DEF_HiVision == 1)
3787                temp = 0x09FC;
3788            else
3789                temp = 0x097C;
3790        }
3791        else {
3792            if (pVBInfo->IF_DEF_HiVision == 1)
3793                temp = 0x01FC;
3794            else
3795                temp = 0x017C;
3796        }
3797       }
3798       else	/* 3nd party chip */
3799       {
3800            if ( pVBInfo->IF_DEF_CH7017 == 1 )
3801                temp = ( SetCRT2ToTV | SetCRT2ToLCD | SetCRT2ToLCDA ) ;
3802            else if ( pVBInfo->IF_DEF_CH7007 == 1 )  /* [Billy] 07/05/03 */
3803            {
3804                temp = SetCRT2ToTV ;
3805            }
3806            else
3807                temp = SetCRT2ToLCD ;
3808       }
3809
3810
3811        if (!(tempbx & temp)) {
3812            tempax |= DisableCRT2Display;
3813            tempbx = 0;
3814        }
3815
3816        if (pVBInfo->IF_DEF_LCDA == 1) {        /* Select Display Device */
3817            if (!(pVBInfo->VBType & VB_NoLCD)) {
3818                if (tempbx & SetCRT2ToLCDA) {
3819                    if (tempbx & SetSimuScanMode)
3820                        tempbx &=
3821                            (~
3822                             (SetCRT2ToLCD | SetCRT2ToRAMDAC | SwitchToCRT2));
3823                    else
3824                        tempbx &=
3825                            (~
3826                             (SetCRT2ToLCD | SetCRT2ToRAMDAC | SetCRT2ToTV |
3827                              SwitchToCRT2));
3828                }
3829            }
3830        }
3831
3832        /* shampoo add */
3833        if (!(tempbx & (SwitchToCRT2 | SetSimuScanMode))) {     /* for driver abnormal */
3834            if (pVBInfo->IF_DEF_CRT2Monitor == 1) {
3835                if (tempbx & SetCRT2ToRAMDAC) {
3836                    tempbx &=
3837                        (0xFF00 | SetCRT2ToRAMDAC | SwitchToCRT2 |
3838                         SetSimuScanMode);
3839                    tempbx &= (0x00FF | (~SetCRT2ToYPbPr));
3840                }
3841            }
3842            else
3843                tempbx &= (~(SetCRT2ToRAMDAC | SetCRT2ToLCD | SetCRT2ToTV));
3844        }
3845
3846        if (!(pVBInfo->VBType & VB_NoLCD)) {
3847            if (tempbx & SetCRT2ToLCD) {
3848                tempbx &=
3849                    (0xFF00 | SetCRT2ToLCD | SwitchToCRT2 | SetSimuScanMode);
3850                tempbx &= (0x00FF | (~SetCRT2ToYPbPr));
3851            }
3852        }
3853
3854        if (tempbx & SetCRT2ToSCART) {
3855            tempbx &=
3856                (0xFF00 | SetCRT2ToSCART | SwitchToCRT2 | SetSimuScanMode);
3857            tempbx &= (0x00FF | (~SetCRT2ToYPbPr));
3858        }
3859
3860        if (pVBInfo->IF_DEF_YPbPr == 1) {
3861            if (tempbx & SetCRT2ToYPbPr)
3862                tempbx &= (0xFF00 | SwitchToCRT2 | SetSimuScanMode);
3863        }
3864
3865        if (pVBInfo->IF_DEF_HiVision == 1) {
3866            if (tempbx & SetCRT2ToHiVisionTV)
3867                tempbx &=
3868                    (0xFF00 | SetCRT2ToHiVisionTV | SwitchToCRT2 |
3869                     SetSimuScanMode);
3870        }
3871
3872        if (tempax & DisableCRT2Display) {      /* Set Display Device Info */
3873            if (!(tempbx & (SwitchToCRT2 | SetSimuScanMode)))
3874                tempbx = DisableCRT2Display;
3875        }
3876
3877        if (!(tempbx & DisableCRT2Display)) {
3878            if ((!(tempbx & DriverMode)) || (!(modeflag & CRT2Mode))) {
3879                if (pVBInfo->IF_DEF_LCDA == 1) {
3880                    if (!(tempbx & SetCRT2ToLCDA))
3881                        tempbx |= (SetInSlaveMode | SetSimuScanMode);
3882                }
3883
3884                if (pVBInfo->IF_DEF_VideoCapture == 1) {
3885                    if ((HwDeviceExtension->jChipType >= XG40)
3886                        && (HwDeviceExtension->jChipType <= XG45)) {
3887                        if (ModeNo <= 13) {
3888                            /* CRT2 not need to support */
3889                            if (!(tempbx & SetCRT2ToRAMDAC)) {
3890                                tempbx &= (0x00FF | (~SetInSlaveMode));
3891                                pVBInfo->SetFlag |= EnableVCMode;
3892                            }
3893                        }
3894                    }
3895                }
3896            }
3897
3898            /*LCD+TV can't support in slave mode (Force LCDA+TV->LCDB) */
3899            if ((tempbx & SetInSlaveMode) && (tempbx & SetCRT2ToLCDA)) {
3900                tempbx ^= (SetCRT2ToLCD | SetCRT2ToLCDA | SetCRT2ToDualEdge);
3901                pVBInfo->SetFlag |= ReserveTVOption;
3902            }
3903        }
3904    }
3905
3906    pVBInfo->VBInfo = tempbx;
3907}
3908
3909
3910/* --------------------------------------------------------------------- */
3911/* Function : XGI_GetTVInfo */
3912/* Input : */
3913/* Output : */
3914/* Description : */
3915/* --------------------------------------------------------------------- */
3916void
3917XGI_GetTVInfo(USHORT ModeNo, USHORT ModeIdIndex, PVB_DEVICE_INFO pVBInfo)
3918{
3919    USHORT temp, tempbx = 0, resinfo = 0, modeflag, index1;
3920
3921    tempbx = 0;
3922    resinfo = 0;
3923
3924    if (pVBInfo->VBInfo & SetCRT2ToTV) {
3925        if (ModeNo <= 0x13) {
3926            modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;  /* si+St_ModeFlag */
3927            resinfo = pVBInfo->SModeIDTable[ModeIdIndex].St_ResInfo;    /* si+St_ResInfo */
3928        }
3929        else {
3930            modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
3931            resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;   /* si+Ext_ResInfo */
3932        }
3933
3934        if (pVBInfo->VBInfo & SetCRT2ToTV) {
3935            temp = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x35);
3936            tempbx = temp;
3937            if (tempbx & SetPALTV) {
3938                tempbx &=
3939                    (SetCHTVOverScan | SetPALMTV | SetPALNTV | SetPALTV);
3940                if (tempbx & SetPALMTV)
3941                    tempbx &= ~SetPALTV;        /* set to NTSC if PAL-M */
3942            }
3943            else
3944                tempbx &= (SetCHTVOverScan | SetNTSCJ | SetPALTV);
3945        }
3946
3947        /* Jong 10/04/2007; merge code */
3948        if ( pVBInfo->IF_DEF_LVDS == 0 )
3949        {
3950          if (pVBInfo->VBInfo & SetCRT2ToSCART)
3951              tempbx |= SetPALTV;
3952        }
3953
3954        if (pVBInfo->IF_DEF_YPbPr == 1) {
3955            if (pVBInfo->VBInfo & SetCRT2ToYPbPr) {
3956                index1 = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x35);
3957                index1 &= YPbPrMode;
3958
3959                if (index1 == YPbPrMode525i)
3960                    tempbx |= SetYPbPrMode525i;
3961
3962                if (index1 == YPbPrMode525p)
3963                    tempbx = tempbx | SetYPbPrMode525p;
3964                if (index1 == YPbPrMode750p)
3965                    tempbx = tempbx | SetYPbPrMode750p;
3966            }
3967        }
3968
3969        if (pVBInfo->IF_DEF_HiVision == 1) {
3970            if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {
3971                tempbx = tempbx | SetYPbPrMode1080i | SetPALTV;
3972            }
3973        }
3974
3975      /* Jong 10/17/2007; merge code */
3976      if ( pVBInfo->IF_DEF_LVDS == 0 )
3977      {
3978        if ((pVBInfo->VBInfo & SetInSlaveMode)
3979            && (!(pVBInfo->VBInfo & SetNotSimuMode)))
3980            tempbx |= TVSimuMode;
3981
3982        if (!(tempbx & SetPALTV) && (modeflag > 13) && (resinfo == 8))  /* NTSC 1024x768, */
3983            tempbx |= NTSC1024x768;
3984
3985        tempbx |= RPLLDIV2XO;
3986
3987        if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {
3988            if (pVBInfo->VBInfo & SetInSlaveMode)
3989                tempbx &= (~RPLLDIV2XO);
3990        }
3991        else {
3992            if (tempbx & (SetYPbPrMode525p | SetYPbPrMode750p))
3993                tempbx &= (~RPLLDIV2XO);
3994            else if (!
3995                     (pVBInfo->
3996                      VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV |
3997                                VB_XGI302LV | VB_XGI301C))) {
3998                if (tempbx & TVSimuMode)
3999                    tempbx &= (~RPLLDIV2XO);
4000            }
4001        }
4002      }
4003    }
4004    pVBInfo->TVInfo = tempbx;
4005}
4006
4007
4008/* --------------------------------------------------------------------- */
4009/* Function : XGI_GetLCDInfo */
4010/* Input : */
4011/* Output : */
4012/* Description : */
4013/* --------------------------------------------------------------------- */
4014BOOLEAN
4015XGI_GetLCDInfo(USHORT ModeNo, USHORT ModeIdIndex, PVB_DEVICE_INFO pVBInfo)
4016{
4017    USHORT temp, tempax, tempbx, modeflag, resinfo = 0, LCDIdIndex;
4018
4019    pVBInfo->LCDResInfo = 0;
4020    pVBInfo->LCDTypeInfo = 0;
4021    pVBInfo->LCDInfo = 0;
4022
4023    if (ModeNo <= 0x13) {
4024        modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;      /* si+St_ModeFlag // */
4025    }
4026    else {
4027        modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
4028        resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;       /* si+Ext_ResInfo// */
4029    }
4030
4031    temp = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x36);      /* Get LCD Res.Info */
4032    tempbx = temp & 0x0F;
4033
4034    if (tempbx == 0)
4035        tempbx = Panel1024x768; /* default */
4036
4037    /* LCD75 [2003/8/22] Vicent */
4038    if ((tempbx == Panel1024x768) || (tempbx == Panel1280x1024)) {
4039        if (pVBInfo->VBInfo & DriverMode) {
4040            tempax = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x33);
4041            if (pVBInfo->VBInfo & SetCRT2ToLCDA)
4042                tempax &= 0x0F;
4043            else
4044                tempax = tempax >> 4;
4045
4046            if ((resinfo == 6) || (resinfo == 9)) {
4047                if (tempax >= 3)
4048                    tempbx |= PanelRef75Hz;
4049            }
4050            else if ((resinfo == 7) || (resinfo == 8)) {
4051                if (tempax >= 4)
4052                    tempbx |= PanelRef75Hz;
4053            }
4054        }
4055    }
4056
4057    pVBInfo->LCDResInfo = tempbx;
4058
4059    /* End of LCD75 */
4060
4061    if (pVBInfo->IF_DEF_OEMUtil == 1) {
4062        pVBInfo->LCDTypeInfo = (temp & 0xf0) >> 4;
4063    }
4064
4065    if (!(pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA))) {
4066        return 0;
4067    }
4068
4069    tempbx = 0;
4070
4071    temp = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x37);
4072
4073    temp &= (ScalingLCD | LCDNonExpanding | LCDSyncBit | SetPWDEnable);
4074
4075    if ((pVBInfo->IF_DEF_ScaleLCD == 1) && (temp & LCDNonExpanding))
4076        temp &= ~EnableScalingLCD;
4077
4078    tempbx |= temp;
4079
4080    LCDIdIndex = XGI_GetLCDCapPtr1(pVBInfo);
4081
4082    tempax = pVBInfo->LCDCapList[LCDIdIndex].LCD_Capability;
4083
4084    /* Jong 10/17/2007; merge code */
4085    if ( pVBInfo->IF_DEF_LVDS == 0 )	/* shampoo */
4086    {
4087    if (((pVBInfo->VBType & VB_XGI302LV) || (pVBInfo->VBType & VB_XGI301C))
4088        && (tempax & LCDDualLink)) {
4089        tempbx |= SetLCDDualLink;
4090    }
4091    }
4092
4093    /* Jong 10/17/1007; merge code */
4094    if ( pVBInfo->IF_DEF_LVDS == 0 )
4095    {
4096    if ((pVBInfo->LCDResInfo == Panel1400x1050)
4097        && (pVBInfo->VBInfo & SetCRT2ToLCD) && (ModeNo > 0x13)
4098        && (resinfo == 9) && (!(tempbx & EnableScalingLCD)))
4099        tempbx |= SetLCDtoNonExpanding; /* set to center in 1280x1024 LCDB for Panel1400x1050 */
4100    }
4101
4102/*
4103    if ( tempax & LCDBToA )
4104    {
4105        tempbx |= SetLCDBToA ;
4106    }
4107*/
4108
4109    if (pVBInfo->IF_DEF_ExpLink == 1) {
4110        if (modeflag & HalfDCLK) {
4111            /* if ( !( pVBInfo->LCDInfo&LCDNonExpanding ) ) */
4112            if (!(tempbx & SetLCDtoNonExpanding)) {
4113                tempbx |= EnableLVDSDDA;
4114            }
4115            else {
4116                if (ModeNo > 0x13) {
4117                    if (pVBInfo->LCDResInfo == Panel1024x768) {
4118                        if (resinfo == 4) {     /* 512x384  */
4119                            tempbx |= EnableLVDSDDA;
4120                        }
4121                    }
4122                }
4123            }
4124        }
4125    }
4126
4127    if (pVBInfo->VBInfo & SetInSlaveMode) {
4128        if (pVBInfo->VBInfo & SetNotSimuMode) {
4129            tempbx |= LCDVESATiming;
4130        }
4131    }
4132    else {
4133        tempbx |= LCDVESATiming;
4134    }
4135
4136    temp = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x39);
4137    if (temp & ReduceTiming) {
4138        tempbx |= EnableReduceTiming;
4139    }
4140
4141    pVBInfo->LCDInfo = tempbx;
4142
4143    if (pVBInfo->IF_DEF_PWD == 1) {
4144        if (pVBInfo->LCDInfo & SetPWDEnable) {
4145            if ((pVBInfo->VBType & VB_XGI302LV)
4146                || (pVBInfo->VBType & VB_XGI301C)) {
4147                if (!(tempax & PWDEnable)) {
4148                    pVBInfo->LCDInfo &= ~SetPWDEnable;
4149                }
4150            }
4151        }
4152    }
4153
4154    /* Jong 10/04/2007; merge code */
4155    if ( pVBInfo->IF_DEF_LVDS == 0 )
4156    {
4157        if (tempax & (LockLCDBToA | StLCDBToA)) {
4158            if (pVBInfo->VBInfo & SetInSlaveMode) {
4159                if (!(tempax & LockLCDBToA)) {
4160                    if (ModeNo <= 0x13) {
4161                        pVBInfo->VBInfo &=
4162                            ~(SetSimuScanMode | SetInSlaveMode |
4163                              SetCRT2ToLCD);
4164                        pVBInfo->VBInfo |= SetCRT2ToLCDA | SetCRT2ToDualEdge;
4165                    }
4166                }
4167            }
4168        }
4169    }
4170
4171    return (1);
4172}
4173
4174/* Jong 10/04/2007; defined in init.c */
4175/* Function : XGI_SearchModeID */
4176/* Input : */
4177/* Output : */
4178/* Description : */
4179/* --------------------------------------------------------------------- */
4180
4181/* --------------------------------------------------------------------- */
4182/* Function : */
4183/* Input : */
4184/* Output : */
4185/* Description : */
4186/* --------------------------------------------------------------------- */
4187BOOLEAN
4188XGINew_CheckMemorySize(PXGI_HW_DEVICE_INFO HwDeviceExtension, USHORT ModeNo,
4189                       USHORT ModeIdIndex, PVB_DEVICE_INFO pVBInfo)
4190{
4191    USHORT memorysize, modeflag, temp, temp1, tmp;
4192
4193    if (ModeNo <= 0x13) {
4194        modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
4195    }
4196    else {
4197        modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
4198    }
4199
4200    /* ModeType = modeflag&ModeInfoFlag ; // Get mode type */
4201
4202    memorysize = modeflag & MemoryInfoFlag;
4203    memorysize = memorysize > MemorySizeShift;
4204    memorysize++;               /* Get memory size */
4205
4206    temp = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x14);      /* Get DRAM Size */
4207    tmp = temp;
4208
4209    if (HwDeviceExtension->jChipType == XG40) {
4210        temp = 1 << ((temp & 0x0F0) >> 4);      /* memory size per channel SR14[7:4] */
4211        if ((tmp & 0x0c) == 0x0C) {     /* Qual channels */
4212            temp <<= 2;
4213        }
4214        else if ((tmp & 0x0c) == 0x08) {        /* Dual channels */
4215            temp <<= 1;
4216        }
4217    }
4218    else if (HwDeviceExtension->jChipType == XG42) {
4219        temp = 1 << ((temp & 0x0F0) >> 4);      /* memory size per channel SR14[7:4] */
4220        if ((tmp & 0x04) == 0x04) {     /* Dual channels */
4221            temp <<= 1;
4222        }
4223    }
4224    else if (HwDeviceExtension->jChipType == XG45) {
4225        temp = 1 << ((temp & 0x0F0) >> 4);      /* memory size per channel SR14[7:4] */
4226        if ((tmp & 0x0c) == 0x0C) {     /* Qual channels */
4227            temp <<= 2;
4228        }
4229        else if ((tmp & 0x0c) == 0x08) {        /* triple channels */
4230            temp1 = temp;
4231            temp <<= 1;
4232            temp += temp1;
4233        }
4234        else if ((tmp & 0x0c) == 0x04) {        /* Dual channels */
4235            temp <<= 1;
4236        }
4237    }
4238    if (temp < memorysize)
4239        return (FALSE);
4240    else
4241        return (TRUE);
4242}
4243
4244
4245/* --------------------------------------------------------------------- */
4246/* Function : XGI_DisplayOn */
4247/* Input : */
4248/* Output : */
4249/* Description : */
4250/* --------------------------------------------------------------------- */
4251void
4252XGI_DisplayOn(PXGI_HW_DEVICE_INFO HwDeviceExtension, PVB_DEVICE_INFO pVBInfo)
4253{
4254    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x01, 0xDF, 0x00);
4255
4256    /* Jong 10/04/2007; merge code */
4257    if ( HwDeviceExtension->jChipType == XG21 )
4258    {
4259       if ( pVBInfo->IF_DEF_LVDS == 1 )
4260       {
4261         if (!(XGI_XG21GetPSCValue( pVBInfo )&0x1))
4262         {
4263            XGI_XG21BLSignalVDD( 0x01 , 0x01, pVBInfo ) ; /* LVDS VDD on */
4264            XGI_XG21SetPanelDelay( 2,pVBInfo ) ;
4265         }
4266         if (!(XGI_XG21GetPSCValue( pVBInfo )&0x20))
4267         {
4268            XGI_XG21BLSignalVDD( 0x20 , 0x20, pVBInfo ) ; /* LVDS signal on */
4269         }
4270         XGI_XG21SetPanelDelay( 3,pVBInfo ) ;
4271         XGI_XG21BLSignalVDD( 0x02 , 0x02, pVBInfo ) ; /* LVDS backlight on */
4272       }
4273       else
4274       {
4275            XGI_XG21BLSignalVDD( 0x20 , 0x20, pVBInfo ) ; /* DVO/DVI signal on */
4276       }
4277
4278    }
4279
4280    /* Jong 10/04/2007; merge code */
4281    if ( HwDeviceExtension->jChipType == XG27 )
4282    {
4283       if ( pVBInfo->IF_DEF_LVDS == 1 )
4284       {
4285         if (!(XGI_XG27GetPSCValue( pVBInfo )&0x1))
4286         {
4287            XGI_XG27BLSignalVDD( 0x01 , 0x01, pVBInfo ) ; /* LVDS VDD on */
4288            XGI_XG21SetPanelDelay( 2,pVBInfo ) ;
4289         }
4290         if (!(XGI_XG27GetPSCValue( pVBInfo )&0x20))
4291         {
4292            XGI_XG27BLSignalVDD( 0x20 , 0x20, pVBInfo ) ; /* LVDS signal on */
4293         }
4294         XGI_XG21SetPanelDelay( 3,pVBInfo ) ;
4295         XGI_XG27BLSignalVDD( 0x02 , 0x02, pVBInfo ) ; /* LVDS backlight on */
4296       }
4297       else
4298       {
4299            XGI_XG27BLSignalVDD( 0x20 , 0x20, pVBInfo ) ; /* DVO/DVI signal on */
4300       }
4301    }
4302
4303}
4304
4305
4306/* --------------------------------------------------------------------- */
4307/* Function : XGI_DisplayOff */
4308/* Input : */
4309/* Output : */
4310/* Description : */
4311/* --------------------------------------------------------------------- */
4312void
4313XGI_DisplayOff(PXGI_HW_DEVICE_INFO HwDeviceExtension, PVB_DEVICE_INFO pVBInfo)
4314{
4315	PDEBUG(ErrorF("XGI_DisplayOff()...begin\n"));
4316
4317    /* Jong 10/04/2007; merge code */
4318    if ( HwDeviceExtension->jChipType == XG21 )
4319    {
4320       if ( pVBInfo->IF_DEF_LVDS == 1 )
4321       {
4322         XGI_XG21BLSignalVDD( 0x02 , 0x00, pVBInfo ) ; /* LVDS backlight off */
4323         XGI_XG21SetPanelDelay( 3,pVBInfo ) ;
4324       }
4325       else
4326       {
4327            XGI_XG21BLSignalVDD( 0x20 , 0x00, pVBInfo ) ; /* DVO/DVI signal off */
4328       }
4329    }
4330
4331    /* Jong 10/04/2007; merge code */
4332    if ( HwDeviceExtension->jChipType == XG27 )
4333    {
4334	   PDEBUG(ErrorF("XG27\n"));
4335       if ( pVBInfo->IF_DEF_LVDS == 0 )
4336       {
4337		    PDEBUG(ErrorF("pVBInfo->IF_DEF_LVDS == 0 ...\n"));
4338            XGI_XG27BLSignalVDD( 0x20 , 0x00, pVBInfo ) ; /* DVO/DVI signal off */
4339		    PDEBUG(ErrorF("After XGI_XG27BLSignalVDD() ...\n"));
4340       }
4341	   else if ((XGI_XG27GetPSCValue( pVBInfo )&0x2)) /* Jong@09232009; bug fixing */
4342       {
4343		 PDEBUG(ErrorF("XGI_XG27GetPSCValue( pVBInfo )&0x2 is true...\n"));
4344         XGI_XG27BLSignalVDD( 0x02 , 0x00, pVBInfo ) ; /* LVDS backlight off */
4345		 PDEBUG(ErrorF("After XGI_XG27BLSignalVDD()...\n"));
4346         XGI_XG21SetPanelDelay( 3,pVBInfo ) ;
4347		 PDEBUG(ErrorF("After XGI_XG21SetPanelDelay()...\n"));
4348       }
4349    }
4350
4351   PDEBUG(ErrorF("Before XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x01, 0xDF, 0x20) ...\n"));
4352   XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x01, 0xDF, 0x20);
4353   PDEBUG(ErrorF("Return from XGI_DisplayOff() ...\n"));
4354}
4355
4356
4357/**
4358 * Wait for vertical or horizontal blanking period.
4359 */
4360void
4361XGI_WaitDisplay(PVB_DEVICE_INFO pVBInfo)
4362{
4363    while ((XGI_GetRegByte((XGIIOADDRESS) pVBInfo->P3da) & 0x01))
4364        break;
4365
4366    while (!(XGI_GetRegByte((XGIIOADDRESS) pVBInfo->P3da) & 0x01))
4367        break;
4368}
4369
4370
4371/* --------------------------------------------------------------------- */
4372/* Function : XGI_SenseCRT1 */
4373/* Input : */
4374/* Output : */
4375/* Description : */
4376/* --------------------------------------------------------------------- */
4377
4378void
4379XGI_SenseCRT1(PVB_DEVICE_INFO pVBInfo)
4380{
4381    UCHAR CRTCData[17] = { 0x5F, 0x4F, 0x50, 0x82, 0x55, 0x81,
4382        0x0B, 0x3E, 0xE9, 0x0B, 0xDF, 0xE7,
4383        0x04, 0x00, 0x00, 0x05, 0x00
4384    };
4385
4386    UCHAR SR01 = 0, SR1F = 0, SR07 = 0, SR06 = 0;
4387
4388    UCHAR CR17, CR63, SR31;
4389    USHORT temp;
4390    UCHAR DAC_TEST_PARMS[3] = { 0x0F, 0x0F, 0x0F };
4391
4392    int i;
4393#ifndef LINUX_XF86
4394    int j;
4395#endif
4396
4397    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x05, 0x86);
4398
4399    /* [2004/05/06] Vicent to fix XG42 single LCD sense to CRT+LCD */
4400    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x57, 0x4A);
4401    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x53,
4402               (UCHAR) (XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x53) |
4403                        0x02));
4404
4405    SR31 = (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x31);
4406    CR63 = (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x63);
4407    SR01 = (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x01);
4408
4409    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x01, (UCHAR) (SR01 & 0xDF));
4410    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x63, (UCHAR) (CR63 & 0xBF));
4411
4412    CR17 = (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x17);
4413    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x17, (UCHAR) (CR17 | 0x80));
4414
4415    SR1F = (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x1F);
4416    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x1F, (UCHAR) (SR1F | 0x04));
4417
4418    SR07 = (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x07);
4419    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x07, (UCHAR) (SR07 & 0xFB));
4420    SR06 = (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x06);
4421    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x06, (UCHAR) (SR06 & 0xC3));
4422
4423    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x11, 0x00);
4424
4425    for (i = 0; i < 8; i++)
4426        XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, (USHORT) i, CRTCData[i]);
4427
4428    for (i = 8; i < 11; i++)
4429        XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, (USHORT) (i + 8),
4430                   CRTCData[i]);
4431
4432    for (i = 11; i < 13; i++)
4433        XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, (USHORT) (i + 4),
4434                   CRTCData[i]);
4435
4436    for (i = 13; i < 16; i++)
4437        XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, (USHORT) (i - 3),
4438                   CRTCData[i]);
4439
4440    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x0E,
4441               (UCHAR) (CRTCData[16] & 0xE0));
4442
4443    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x31, 0x00);
4444    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x2B, 0x1B);
4445    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x2C, 0xE1);
4446
4447    XGI_SetRegByte((XGIIOADDRESS) pVBInfo->P3c8, 0x00);
4448    for (i = 0; i < 256; i++) {
4449        XGI_SetRegByte((XGIIOADDRESS) (USHORT) (pVBInfo->P3c8 + 1),
4450                       (UCHAR) DAC_TEST_PARMS[0]);
4451        XGI_SetRegByte((XGIIOADDRESS) (USHORT) (pVBInfo->P3c8 + 1),
4452                       (UCHAR) DAC_TEST_PARMS[1]);
4453        XGI_SetRegByte((XGIIOADDRESS) (USHORT) (pVBInfo->P3c8 + 1),
4454                       (UCHAR) DAC_TEST_PARMS[2]);
4455    }
4456
4457    XGI_VBLongWait(pVBInfo);
4458    XGI_VBLongWait(pVBInfo);
4459    XGI_VBLongWait(pVBInfo);
4460
4461    XGINew_LCD_Wait_Time(0x01, pVBInfo);
4462    XGI_WaitDisplay(pVBInfo);
4463
4464    temp = XGI_GetRegByte((XGIIOADDRESS) pVBInfo->P3c2);
4465    if (temp & 0x10) {
4466        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x32, 0xDF, 0x20);
4467    }
4468    else {
4469        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x32, 0xDF, 0x00);
4470    }
4471
4472    /* alan, avoid display something, set BLACK DAC if not restore DAC */
4473    XGI_SetRegByte((XGIIOADDRESS) pVBInfo->P3c8, 0x00);
4474
4475    for (i = 0; i < 256; i++) {
4476        XGI_SetRegByte((XGIIOADDRESS) (USHORT) (pVBInfo->P3c8 + 1), 0);
4477        XGI_SetRegByte((XGIIOADDRESS) (USHORT) (pVBInfo->P3c8 + 1), 0);
4478        XGI_SetRegByte((XGIIOADDRESS) (USHORT) (pVBInfo->P3c8 + 1), 0);
4479    }
4480
4481    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x01, SR01);
4482    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x63, CR63);
4483    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x31, SR31);
4484
4485    /* [2004/05/11] Vicent */
4486    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x53,
4487               (UCHAR) (XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x53) &
4488                        0xFD));
4489}
4490
4491
4492/* --------------------------------------------------------------------- */
4493/* Function : */
4494/* Input : */
4495/* Output : */
4496/* Description : */
4497/* --------------------------------------------------------------------- */
4498BOOLEAN
4499CheckDualChip(PVB_DEVICE_INFO pVBInfo)
4500{
4501    /* Check H/W trap that 2nd chip is present or not. */
4502    return ((BOOLEAN)
4503            (XGI_GetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x3A) &
4504             XGI_MASK_DUAL_CHIP));
4505}
4506
4507
4508
4509/* --------------------------------------------------------------------- */
4510/* Function : SetDualChipRegs */
4511/* Input : */
4512/* Output : */
4513/* Description : */
4514/* --------------------------------------------------------------------- */
4515void
4516SetDualChipRegs(PXGI_HW_DEVICE_INFO HwDeviceExtension,
4517                PVB_DEVICE_INFO pVBInfo)
4518{
4519
4520#ifdef LINUX_XF86
4521    USHORT BaseAddr2nd = (USHORT) (ULONG) HwDeviceExtension->pj2ndIOAddress;
4522#else
4523    USHORT BaseAddr2nd = (USHORT) HwDeviceExtension->pj2ndIOAddress;
4524#endif
4525    USHORT XGINew_P3CC = pVBInfo->BaseAddr + MISC_OUTPUT_REG_READ_PORT;
4526    USHORT XGINew_2ndP3CE = BaseAddr2nd + GRAPH_ADDRESS_PORT;
4527    USHORT XGINew_2ndP3C4 = BaseAddr2nd + SEQ_ADDRESS_PORT;
4528    USHORT XGINew_2ndP3C2 = BaseAddr2nd + MISC_OUTPUT_REG_WRITE_PORT;
4529    UCHAR tempal, i;
4530    pVBInfo->BaseAddr = (USHORT) HwDeviceExtension->pjIOAddress;
4531    for (i = 0x00; i <= 0x04; i++) {    /* SR0 - SR4 */
4532        tempal = (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->P3c4, i);
4533        XGI_SetReg((XGIIOADDRESS) XGINew_2ndP3C4, i, tempal);
4534    }
4535    for (i = 0x00; i <= 0x08; i++) {    /* GR0 - GR8 */
4536        tempal = (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->P3ce, i);
4537        XGI_SetReg((XGIIOADDRESS) XGINew_2ndP3CE, i, tempal);
4538    }
4539    /* OpenKey in 2nd chip */
4540    XGI_SetReg((XGIIOADDRESS) XGINew_2ndP3C4, 0x05, 0x86);
4541
4542    /* Copy SR06 to 2nd chip */
4543    tempal = (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x06);
4544    XGI_SetReg((XGIIOADDRESS) XGINew_2ndP3C4, 0x06, tempal);
4545
4546    /* Copy SR21 to 2nd chip */
4547    tempal = (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x21);
4548    XGI_SetReg((XGIIOADDRESS) XGINew_2ndP3C4, 0x21, tempal);
4549
4550    /* Miscellaneous reg(input port 3cch,output port 3c2h) */
4551    tempal = (UCHAR) XGI_GetRegByte((XGIIOADDRESS) XGINew_P3CC);        /* 3cc */
4552    XGI_SetRegByte((XGIIOADDRESS) XGINew_2ndP3C2, tempal);
4553
4554    /* Close key in 2nd chip */
4555    XGI_SetReg((XGIIOADDRESS) XGINew_2ndP3C4, 0x05, 0x00);
4556}
4557
4558
4559/* --------------------------------------------------------------------- */
4560/* Function : XGI_SetCRT2Group301 */
4561/* Input : */
4562/* Output : */
4563/* Description : */
4564/* --------------------------------------------------------------------- */
4565BOOLEAN
4566XGI_SetCRT2Group301(USHORT ModeNo, PXGI_HW_DEVICE_INFO HwDeviceExtension,
4567                    PVB_DEVICE_INFO pVBInfo)
4568{
4569    USHORT tempbx, ModeIdIndex, RefreshRateTableIndex;
4570    USHORT temp_mode_no;
4571
4572    tempbx = pVBInfo->VBInfo;
4573    pVBInfo->SetFlag |= ProgrammingCRT2;
4574
4575    temp_mode_no = ModeNo;
4576    XGI_SearchModeID(pVBInfo->SModeIDTable, pVBInfo->EModeIDTable, 0x11,
4577                     &temp_mode_no, &ModeIdIndex);
4578
4579
4580    pVBInfo->SelectCRT2Rate = 4;
4581    RefreshRateTableIndex = XGI_GetRatePtrCRT2(HwDeviceExtension, ModeNo, ModeIdIndex, pVBInfo);
4582    XGI_SaveCRT2Info(ModeNo, pVBInfo);
4583    XGI_GetCRT2ResInfo(ModeNo, ModeIdIndex, pVBInfo);
4584    XGI_GetCRT2Data(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
4585    XGI_PreSetGroup1(ModeNo, ModeIdIndex, HwDeviceExtension,
4586                     RefreshRateTableIndex, pVBInfo);
4587    XGI_SetGroup1(ModeNo, ModeIdIndex, HwDeviceExtension,
4588                  RefreshRateTableIndex, pVBInfo);
4589    XGI_SetLockRegs(ModeNo, ModeIdIndex, HwDeviceExtension,
4590                    RefreshRateTableIndex, pVBInfo);
4591    XGI_SetGroup2(ModeNo, ModeIdIndex, RefreshRateTableIndex,
4592                  HwDeviceExtension, pVBInfo);
4593    XGI_SetLCDRegs(ModeNo, ModeIdIndex, HwDeviceExtension,
4594                   RefreshRateTableIndex, pVBInfo);
4595    XGI_SetTap4Regs(pVBInfo);
4596    XGI_SetGroup3(ModeNo, ModeIdIndex, pVBInfo);
4597    XGI_SetGroup4(ModeNo, ModeIdIndex, RefreshRateTableIndex,
4598                  HwDeviceExtension, pVBInfo);
4599    XGI_SetCRT2VCLK(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
4600    XGI_SetGroup5(ModeNo, ModeIdIndex, pVBInfo);
4601    XGI_AutoThreshold(pVBInfo);
4602    return 1;
4603}
4604
4605
4606/* --------------------------------------------------------------------- */
4607/* Function : XGI_AutoThreshold */
4608/* Input : */
4609/* Output : */
4610/* Description : */
4611/* --------------------------------------------------------------------- */
4612void
4613XGI_AutoThreshold(PVB_DEVICE_INFO pVBInfo)
4614{
4615    if (!(pVBInfo->SetFlag & Win9xDOSMode))
4616        XGI_SetRegOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x01, 0x40);
4617}
4618
4619
4620/* --------------------------------------------------------------------- */
4621/* Function : XGI_SaveCRT2Info */
4622/* Input : */
4623/* Output : */
4624/* Description : */
4625/* --------------------------------------------------------------------- */
4626void
4627XGI_SaveCRT2Info(USHORT ModeNo, PVB_DEVICE_INFO pVBInfo)
4628{
4629    USHORT temp1, temp2;
4630
4631    XGI_SetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x34, ModeNo);     /* reserve CR34 for CRT1 Mode No */
4632    temp1 = (pVBInfo->VBInfo & SetInSlaveMode) >> 8;
4633    temp2 = ~(SetInSlaveMode >> 8);
4634    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x31, temp2, temp1);
4635}
4636
4637
4638/* --------------------------------------------------------------------- */
4639/* Function : XGI_GetCRT2ResInfo */
4640/* Input : */
4641/* Output : */
4642/* Description : */
4643/* --------------------------------------------------------------------- */
4644void
4645XGI_GetCRT2ResInfo(USHORT ModeNo, USHORT ModeIdIndex, PVB_DEVICE_INFO pVBInfo)
4646{
4647    unsigned xres;
4648    unsigned yres;
4649
4650
4651    get_mode_xres_yres(ModeNo, ModeIdIndex, pVBInfo, &xres, &yres);
4652
4653    if ((pVBInfo->VBInfo & SetCRT2ToLCD)
4654        && !(pVBInfo->LCDInfo & (EnableScalingLCD | LCDNonExpanding))) {
4655        switch (pVBInfo->LCDResInfo) {
4656        case Panel1600x1200:
4657            if (!(pVBInfo->LCDInfo & LCDVESATiming) && (yres == 1024)) {
4658                yres = 1056;
4659            }
4660            break;
4661
4662
4663        case Panel1280x1024:
4664            if (yres == 400)
4665                yres = 405;
4666            else if (yres == 350)
4667                yres = 360;
4668            else if ((pVBInfo->LCDInfo & LCDVESATiming) && (yres == 360)) {
4669                yres = 375;
4670            }
4671            break;
4672
4673
4674        case Panel1024x768:
4675            if (!(pVBInfo->LCDInfo & (LCDVESATiming | LCDNonExpanding))) {
4676                if (yres == 350) {
4677                    yres = 357;
4678                }
4679                else if (yres == 400) {
4680                    yres = 420;
4681                }
4682                else if (yres == 480) {
4683                    yres = 525;
4684                }
4685            }
4686
4687            break;
4688        }
4689
4690        if (xres == 720)
4691            xres = 640;
4692    }
4693
4694    pVBInfo->VGAHDE = xres;
4695    pVBInfo->HDE = xres;
4696    pVBInfo->VGAVDE = yres;
4697    pVBInfo->VDE = yres;
4698}
4699
4700
4701/* --------------------------------------------------------------------- */
4702/* Function : XGI_IsLCDDualLink */
4703/* Input : */
4704/* Output : */
4705/* Description : */
4706/* --------------------------------------------------------------------- */
4707BOOLEAN
4708XGI_IsLCDDualLink(PVB_DEVICE_INFO pVBInfo)
4709{
4710    return (((pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) != 0)
4711            && ((pVBInfo->LCDInfo & SetLCDDualLink) != 0));
4712}
4713
4714
4715/* --------------------------------------------------------------------- */
4716/* Function : XGI_GetCRT2Data */
4717/* Input : */
4718/* Output : */
4719/* Description : */
4720/* --------------------------------------------------------------------- */
4721void
4722XGI_GetCRT2Data(USHORT ModeNo, USHORT ModeIdIndex,
4723                USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo)
4724{
4725    USHORT tempax = 0, tempbx, modeflag, resinfo;
4726#ifndef LINUX_XF86
4727    USHORT CRT2Index, ResIndex;
4728#endif
4729
4730    XGI_LCDDataStruct *LCDPtr = NULL;
4731    XGI_TVDataStruct *TVPtr = NULL;
4732
4733    if (ModeNo <= 0x13) {
4734        modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;      /* si+St_ResInfo */
4735        resinfo = pVBInfo->SModeIDTable[ModeIdIndex].St_ResInfo;
4736    }
4737    else {
4738        modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;     /* si+Ext_ResInfo */
4739        resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
4740    }
4741
4742    pVBInfo->NewFlickerMode = 0;
4743    pVBInfo->RVBHRS = 50;
4744
4745    if (pVBInfo->VBInfo & SetCRT2ToRAMDAC) {
4746        XGI_GetRAMDAC2DATA(ModeNo, ModeIdIndex, RefreshRateTableIndex,
4747                           pVBInfo);
4748        return;
4749    }
4750
4751    tempbx = 4;
4752
4753    if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
4754        LCDPtr =
4755            (XGI_LCDDataStruct *) XGI_GetLcdPtr(tempbx, ModeNo, ModeIdIndex,
4756                                                RefreshRateTableIndex,
4757                                                pVBInfo);
4758
4759        PDEBUG(ErrorF
4760               ("C code setmode: ModeNo: 0x%08lX VGAHT:0x%081X  \n", ModeNo,
4761                LCDPtr->VGAHT));
4762        pVBInfo->RVBHCMAX = LCDPtr->RVBHCMAX;
4763        pVBInfo->RVBHCFACT = LCDPtr->RVBHCFACT;
4764        pVBInfo->VGAHT = LCDPtr->VGAHT;
4765        pVBInfo->VGAVT = LCDPtr->VGAVT;
4766        pVBInfo->HT = LCDPtr->LCDHT;
4767        pVBInfo->VT = LCDPtr->LCDVT;
4768
4769        if (pVBInfo->LCDResInfo == Panel1024x768) {
4770            tempax = 1024;
4771            tempbx = 768;
4772
4773            if (!(pVBInfo->LCDInfo & LCDVESATiming)) {
4774                if (pVBInfo->VGAVDE == 357)
4775                    tempbx = 527;
4776                else if (pVBInfo->VGAVDE == 420)
4777                    tempbx = 620;
4778                else if (pVBInfo->VGAVDE == 525)
4779                    tempbx = 775;
4780                else if (pVBInfo->VGAVDE == 600)
4781                    tempbx = 775;
4782                /* else if(pVBInfo->VGAVDE==350) tempbx=560; */
4783                /* else if(pVBInfo->VGAVDE==400) tempbx=640; */
4784                else
4785                    tempbx = 768;
4786            }
4787            else
4788                tempbx = 768;
4789        }
4790        else if (pVBInfo->LCDResInfo == Panel1024x768x75) {
4791            tempax = 1024;
4792            tempbx = 768;
4793        }
4794        else if (pVBInfo->LCDResInfo == Panel1280x1024) {
4795            tempax = 1280;
4796            if (pVBInfo->VGAVDE == 360)
4797                tempbx = 768;
4798            else if (pVBInfo->VGAVDE == 375)
4799                tempbx = 800;
4800            else if (pVBInfo->VGAVDE == 405)
4801                tempbx = 864;
4802            else
4803                tempbx = 1024;
4804        }
4805        else if (pVBInfo->LCDResInfo == Panel1280x1024x75) {
4806            tempax = 1280;
4807            tempbx = 1024;
4808        }
4809        else if (pVBInfo->LCDResInfo == Panel1280x960) {
4810            tempax = 1280;
4811            if (pVBInfo->VGAVDE == 350)
4812                tempbx = 700;
4813            else if (pVBInfo->VGAVDE == 400)
4814                tempbx = 800;
4815            else if (pVBInfo->VGAVDE == 1024)
4816                tempbx = 960;
4817            else
4818                tempbx = 960;
4819        }
4820        else if (pVBInfo->LCDResInfo == Panel1400x1050) {
4821            tempax = 1400;
4822            tempbx = 1050;
4823
4824            if (pVBInfo->VGAVDE == 1024) {
4825                tempax = 1280;
4826                tempbx = 1024;
4827            }
4828        }
4829        else if (pVBInfo->LCDResInfo == Panel1600x1200) {
4830            tempax = 1600;
4831            tempbx = 1200;      /* alan 10/14/2003 */
4832            if (!(pVBInfo->LCDInfo & LCDVESATiming)) {
4833                if (pVBInfo->VGAVDE == 350)
4834                    tempbx = 875;
4835                else if (pVBInfo->VGAVDE == 400)
4836                    tempbx = 1000;
4837            }
4838        }
4839
4840        if (pVBInfo->LCDInfo & (LCDNonExpanding | EnableScalingLCD)) {
4841            tempax = pVBInfo->VGAHDE;
4842            tempbx = pVBInfo->VGAVDE;
4843        }
4844
4845        pVBInfo->HDE = tempax;
4846        pVBInfo->VDE = tempbx;
4847        return;
4848    }
4849
4850    if (pVBInfo->VBInfo & (SetCRT2ToTV)) {
4851        tempbx = 4;
4852        TVPtr =
4853            (XGI_TVDataStruct *) XGI_GetTVPtr(tempbx, ModeNo, ModeIdIndex,
4854                                              RefreshRateTableIndex, pVBInfo);
4855        pVBInfo->RVBHCMAX = TVPtr->RVBHCMAX;
4856        pVBInfo->RVBHCFACT = TVPtr->RVBHCFACT;
4857        pVBInfo->VGAHT = TVPtr->VGAHT;
4858        pVBInfo->VGAVT = TVPtr->VGAVT;
4859        pVBInfo->HDE = TVPtr->TVHDE;
4860        pVBInfo->VDE = TVPtr->TVVDE;
4861        pVBInfo->RVBHRS = TVPtr->RVBHRS;
4862        pVBInfo->NewFlickerMode = TVPtr->FlickerMode;
4863
4864        if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {
4865            if (resinfo == 0x08)
4866                pVBInfo->NewFlickerMode = 0x40;
4867            else if (resinfo == 0x09)
4868                pVBInfo->NewFlickerMode = 0x40;
4869            else if (resinfo == 0x12)
4870                pVBInfo->NewFlickerMode = 0x40;
4871
4872            if (pVBInfo->VGAVDE == 350)
4873                pVBInfo->TVInfo |= TVSimuMode;
4874
4875            tempax = ExtHiTVHT;
4876            tempbx = ExtHiTVVT;
4877
4878            if (pVBInfo->VBInfo & SetInSlaveMode) {
4879                if (pVBInfo->TVInfo & TVSimuMode) {
4880                    tempax = StHiTVHT;
4881                    tempbx = StHiTVVT;
4882
4883                    if (!(modeflag & Charx8Dot)) {
4884                        tempax = StHiTextTVHT;
4885                        tempbx = StHiTextTVVT;
4886                    }
4887                }
4888            }
4889        }
4890        else if (pVBInfo->VBInfo & SetCRT2ToYPbPr) {
4891            if (pVBInfo->TVInfo & SetYPbPrMode750p) {
4892                tempax = YPbPrTV750pHT; /* Ext750pTVHT */
4893                tempbx = YPbPrTV750pVT; /* Ext750pTVVT */
4894            }
4895
4896            if (pVBInfo->TVInfo & SetYPbPrMode525p) {
4897                tempax = YPbPrTV525pHT; /* Ext525pTVHT */
4898                tempbx = YPbPrTV525pVT; /* Ext525pTVVT */
4899            }
4900            else if (pVBInfo->TVInfo & SetYPbPrMode525i) {
4901                tempax = YPbPrTV525iHT; /* Ext525iTVHT */
4902                tempbx = YPbPrTV525iVT; /* Ext525iTVVT */
4903                if (pVBInfo->TVInfo & NTSC1024x768)
4904                    tempax = NTSC1024x768HT;
4905            }
4906        }
4907        else {
4908            tempax = PALHT;
4909            tempbx = PALVT;
4910            if (!(pVBInfo->TVInfo & SetPALTV)) {
4911                tempax = NTSCHT;
4912                tempbx = NTSCVT;
4913                if (pVBInfo->TVInfo & NTSC1024x768)
4914                    tempax = NTSC1024x768HT;
4915            }
4916        }
4917
4918        pVBInfo->HT = tempax;
4919        pVBInfo->VT = tempbx;
4920        return;
4921    }
4922}
4923
4924
4925/* --------------------------------------------------------------------- */
4926/* Function : XGI_SetCRT2VCLK */
4927/* Input : */
4928/* Output : */
4929/* Description : */
4930/* --------------------------------------------------------------------- */
4931void
4932XGI_SetCRT2VCLK(USHORT ModeNo, USHORT ModeIdIndex,
4933                USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo)
4934{
4935    UCHAR di[2];
4936    const unsigned vclkindex =
4937        XGI_GetVCLKPtr(RefreshRateTableIndex, ModeNo, ModeIdIndex, pVBInfo);
4938
4939    XGI_GetVCLKLen(vclkindex, di, pVBInfo);
4940    XGI_GetLCDVCLKPtr(di, pVBInfo);
4941
4942    if (pVBInfo->VBType & VB_XGI301) {  /* shampoo 0129 *//* 301 */
4943        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x0A, 0x10);
4944        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x0B, di[1]);
4945        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x0A, di[0]);
4946    }
4947    else {                      /* 301b/302b/301lv/302lv */
4948        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x0A, di[0]);
4949        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x0B, di[1]);
4950    }
4951
4952    if ((pVBInfo->LCDInfo & EnableReduceTiming)
4953        && (pVBInfo->LCDResInfo == Panel1600x1200)) {
4954        if (pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO == 0x0A) {
4955            XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x0A, 0x5A);
4956            XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x0B, 0x24);
4957        }
4958    }
4959
4960    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x00, 0x12);
4961
4962    if (pVBInfo->VBInfo & SetCRT2ToRAMDAC)
4963        XGI_SetRegOR((XGIIOADDRESS) pVBInfo->Part4Port, 0x12, 0x28);
4964    else
4965        XGI_SetRegOR((XGIIOADDRESS) pVBInfo->Part4Port, 0x12, 0x08);
4966}
4967
4968
4969/* --------------------------------------------------------------------- */
4970/* Function : XGI_GETLCDVCLKPtr */
4971/* Input : */
4972/* Output : al -> VCLK Index */
4973/* Description : */
4974/* --------------------------------------------------------------------- */
4975void
4976XGI_GetLCDVCLKPtr(UCHAR *di, PVB_DEVICE_INFO pVBInfo)
4977{
4978    if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
4979        if ((pVBInfo->IF_DEF_ScaleLCD != 1)
4980            || !(pVBInfo->LCDInfo & EnableScalingLCD)) {
4981            const unsigned index = XGI_GetLCDCapPtr1(pVBInfo);
4982
4983            if (pVBInfo->VBInfo & SetCRT2ToLCD) {   /* LCDB */
4984                di[0] = pVBInfo->LCDCapList[index].LCUCHAR_VCLKData1;
4985                di[1] = pVBInfo->LCDCapList[index].LCUCHAR_VCLKData2;
4986            }
4987            else {                  /* LCDA */
4988                di[0] = pVBInfo->LCDCapList[index].LCDA_VCLKData1;
4989                di[1] = pVBInfo->LCDCapList[index].LCDA_VCLKData2;
4990            }
4991        }
4992    }
4993
4994    return;
4995}
4996
4997
4998/* --------------------------------------------------------------------- */
4999/* Function : XGI_GetVCLKPtr */
5000/* Input : */
5001/* Output : */
5002/* Description : */
5003/* --------------------------------------------------------------------- */
5004unsigned
5005XGI_GetVCLKPtr(USHORT RefreshRateTableIndex, USHORT ModeNo,
5006               USHORT ModeIdIndex, PVB_DEVICE_INFO pVBInfo)
5007{
5008    unsigned vclk;
5009    const unsigned modeflag = (ModeNo <= 0x13)
5010        ? pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag
5011        : pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
5012
5013
5014    if ((pVBInfo->SetFlag & ProgrammingCRT2)
5015        && (!(pVBInfo->LCDInfo & EnableScalingLCD))) {     /* {LCDA/LCDB} */
5016        const unsigned  index = XGI_GetLCDCapPtr(pVBInfo);
5017        vclk = pVBInfo->LCDCapList[index].LCD_VCLK;
5018
5019        if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA))
5020            return vclk;
5021
5022        /* {TV} */
5023        if (pVBInfo->
5024            VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV |
5025                      VB_XGI301C)) {
5026            if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {
5027                if (pVBInfo->TVInfo & TVSimuMode) {
5028                    vclk = (modeflag & Charx8Dot)
5029                        ? HiTVSimuVCLK : HiTVTextVCLK;
5030                }
5031                else {
5032                    vclk = (pVBInfo->TVInfo & RPLLDIV2XO)
5033                        ? HiTVVCLKDIV2 : HiTVVCLK;
5034                }
5035
5036                return vclk;
5037            }
5038            else if (pVBInfo->TVInfo & SetYPbPrMode750p) {
5039                return YPbPr750pVCLK;
5040            }
5041            else if (pVBInfo->TVInfo & SetYPbPrMode525p) {
5042                return YPbPr525pVCLK;
5043            }
5044
5045            vclk = NTSC1024VCLK;
5046
5047            if (!(pVBInfo->TVInfo & NTSC1024x768)) {
5048                vclk = (pVBInfo->TVInfo & RPLLDIV2XO)
5049                    ? TVVCLKDIV2 : TVVCLK;
5050            }
5051
5052            if (pVBInfo->VBInfo & SetCRT2ToTV)
5053                return vclk;
5054        }
5055    }                           /* {End of VB} */
5056
5057    vclk = XGI_GetRegByte((XGIIOADDRESS) (pVBInfo->P3ca + 0x02));
5058    vclk = (vclk >> 2) & 0x03;
5059
5060    /* for Dot8 Scaling LCD */
5061    if ((pVBInfo->LCDInfo & EnableScalingLCD)
5062        && (modeflag & Charx8Dot)
5063        && ((pVBInfo->IF_DEF_VideoCapture) == 1)) {
5064        vclk = VCLK25_175;       /* ; set to VCLK25MHz always */
5065    }
5066
5067    if (ModeNo <= 0x13)
5068        return vclk;
5069
5070    return pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
5071}
5072
5073
5074/* --------------------------------------------------------------------- */
5075/* Function : XGI_GetVCLKLen */
5076/* Input : */
5077/* Output : */
5078/* Description : */
5079/* --------------------------------------------------------------------- */
5080void
5081XGI_GetVCLKLen(unsigned vclkindex, UCHAR *di, PVB_DEVICE_INFO pVBInfo)
5082{
5083    if (pVBInfo->
5084        VBType & (VB_XGI301 | VB_XGI301B | VB_XGI302B | VB_XGI301LV |
5085                  VB_XGI302LV | VB_XGI301C)) {
5086        if ((!(pVBInfo->VBInfo & SetCRT2ToLCDA))
5087            && (pVBInfo->SetFlag & ProgrammingCRT2)) {
5088            di[0] = XGI_VBVCLKData[vclkindex].SR2B;
5089            di[1] = XGI_VBVCLKData[vclkindex].SR2C;
5090        }
5091    }
5092    else {
5093        di[0] = XGI_VCLKData[vclkindex].SR2B;
5094        di[1] = XGI_VCLKData[vclkindex].SR2C;
5095    }
5096}
5097
5098
5099/* --------------------------------------------------------------------- */
5100/* Function : XGI_SetCRT2Offset */
5101/* Input : */
5102/* Output : */
5103/* Description : */
5104/* --------------------------------------------------------------------- */
5105void
5106XGI_SetCRT2Offset(USHORT ModeNo,
5107                  USHORT ModeIdIndex, USHORT RefreshRateTableIndex,
5108                  PXGI_HW_DEVICE_INFO HwDeviceExtension,
5109                  PVB_DEVICE_INFO pVBInfo)
5110{
5111    USHORT offset;
5112    UCHAR temp;
5113
5114    if (pVBInfo->VBInfo & SetInSlaveMode) {
5115        return;
5116    }
5117
5118    offset =
5119        XGI_GetOffset(ModeNo, ModeIdIndex, RefreshRateTableIndex,
5120                      HwDeviceExtension, pVBInfo);
5121    temp = (UCHAR) (offset & 0xFF);
5122    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x07, temp);
5123    temp = (UCHAR) ((offset & 0xFF00) >> 8);
5124    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x09, temp);
5125    temp = (UCHAR) (((offset >> 3) & 0xFF) + 1);
5126    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x03, temp);
5127}
5128
5129
5130/* --------------------------------------------------------------------- */
5131/* Function : XGI_GetOffset */
5132/* Input : */
5133/* Output : */
5134/* Description : */
5135/* --------------------------------------------------------------------- */
5136USHORT
5137XGI_GetOffset(USHORT ModeNo, USHORT ModeIdIndex, USHORT RefreshRateTableIndex,
5138              PXGI_HW_DEVICE_INFO HwDeviceExtension, PVB_DEVICE_INFO pVBInfo)
5139{
5140    USHORT	temp,
5141	        colordepth,
5142		    modeinfo, index, infoflag,
5143			ColorDepth[] = { 0x01 , 0x02 , 0x04 } ;
5144
5145    modeinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeInfo;
5146    if (ModeNo <= 0x14)
5147        infoflag = 0;
5148    else
5149        infoflag = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_InfoFlag;
5150
5151
5152    index = (modeinfo >> 8) & 0xFF;
5153
5154    temp = pVBInfo->ScreenOffset[index];
5155
5156    if (infoflag & InterlaceMode) {
5157        temp = temp << 1;
5158    }
5159
5160    colordepth = XGI_GetColorDepth(ModeNo, ModeIdIndex, pVBInfo);
5161
5162    /* Jong 10/04/2007; merge code */
5163    if ( ( ModeNo >= 0x7C ) && ( ModeNo <= 0x7E ) )
5164    {
5165        temp = ModeNo - 0x7C ;
5166	colordepth = ColorDepth[ temp ] ;
5167	temp = 0x6B ;
5168	if ( infoflag & InterlaceMode )
5169	{
5170            temp = temp << 1 ;
5171	}
5172	return( temp * colordepth ) ;
5173    }
5174    else
5175        return( temp * colordepth ) ;
5176
5177    /*
5178    if ((ModeNo >= 0x26) && (ModeNo <= 0x28)) {
5179        return (temp * colordepth + (colordepth >> 1));
5180    }
5181    else
5182        return (temp * colordepth); */
5183}
5184
5185
5186/* --------------------------------------------------------------------- */
5187/* Function : XGI_SetCRT2FIFO */
5188/* Input : */
5189/* Output : */
5190/* Description : */
5191/* --------------------------------------------------------------------- */
5192void
5193XGI_SetCRT2FIFO(PVB_DEVICE_INFO pVBInfo)
5194{
5195    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x01, 0x3B);  /* threshold high ,disable auto threshold */
5196    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x02, ~(0x3F), 0x04);    /* threshold low default 04h */
5197}
5198
5199
5200/* --------------------------------------------------------------------- */
5201/* Function : XGI_PreSetGroup1 */
5202/* Input : */
5203/* Output : */
5204/* Description : */
5205/* --------------------------------------------------------------------- */
5206void
5207XGI_PreSetGroup1(USHORT ModeNo, USHORT ModeIdIndex,
5208                 PXGI_HW_DEVICE_INFO HwDeviceExtension,
5209                 USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo)
5210{
5211    USHORT tempcx = 0, CRT1Index = 0, resinfo = 0;
5212#ifndef LINUX_XF86
5213    USHORT temp = 0, tempax = 0, tempbx = 0, pushbx = 0, modeflag;
5214#endif
5215
5216    if (ModeNo > 0x13) {
5217        CRT1Index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
5218        CRT1Index &= IndexMask;
5219        resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
5220    }
5221
5222    XGI_SetCRT2Offset(ModeNo, ModeIdIndex, RefreshRateTableIndex,
5223                      HwDeviceExtension, pVBInfo);
5224    XGI_SetCRT2FIFO(pVBInfo);
5225    /* XGI_SetCRT2Sync(ModeNo,RefreshRateTableIndex); */
5226
5227    for (tempcx = 4; tempcx < 7; tempcx++) {
5228        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, tempcx, 0x0);
5229    }
5230
5231    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x02, 0x44);  /* temp 0206 */
5232}
5233
5234
5235/* --------------------------------------------------------------------- */
5236/* Function : XGI_SetGroup1 */
5237/* Input : */
5238/* Output : */
5239/* Description : */
5240/* --------------------------------------------------------------------- */
5241void
5242XGI_SetGroup1(USHORT ModeNo, USHORT ModeIdIndex,
5243              PXGI_HW_DEVICE_INFO HwDeviceExtension,
5244              USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo)
5245{
5246    USHORT temp = 0,
5247        tempax = 0,
5248        tempbx = 0,
5249        tempcx = 0, pushbx = 0, CRT1Index = 0, modeflag, resinfo = 0;
5250
5251    if (ModeNo > 0x13) {
5252        CRT1Index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
5253        CRT1Index &= IndexMask;
5254        resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
5255    }
5256
5257    if (ModeNo <= 0x13) {
5258        modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
5259    }
5260    else {
5261        modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
5262    }
5263
5264    /* bainy change table name */
5265    if (modeflag & HalfDCLK) {
5266        temp = (pVBInfo->VGAHT / 2 - 1) & 0x0FF;        /* BTVGA2HT 0x08,0x09 */
5267        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x08, temp);
5268        temp = (((pVBInfo->VGAHT / 2 - 1) & 0xFF00) >> 8) << 4;
5269        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x09, ~0x0F0,
5270                        temp);
5271        temp = (pVBInfo->VGAHDE / 2 + 16) & 0x0FF;      /* BTVGA2HDEE 0x0A,0x0C */
5272        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x0A, temp);
5273        tempcx = ((pVBInfo->VGAHT - pVBInfo->VGAHDE) / 2) >> 2;
5274        pushbx = pVBInfo->VGAHDE / 2 + 16;
5275        tempcx = tempcx >> 1;
5276        tempbx = pushbx + tempcx;       /* bx BTVGA@HRS 0x0B,0x0C */
5277        tempcx += tempbx;
5278
5279        if (pVBInfo->VBInfo & SetCRT2ToRAMDAC) {
5280            tempbx = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[4];
5281            tempbx |=
5282                ((pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[14] & 0xC0) << 2);
5283            tempbx = (tempbx - 3) << 3; /* (VGAHRS-3)*8 */
5284            tempcx = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[5];
5285            tempcx &= 0x1F;
5286            temp = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[15];
5287            temp = (temp & 0x04) << (5 - 2);    /* VGAHRE D[5] */
5288            tempcx = ((tempcx | temp) - 3) << 3;        /* (VGAHRE-3)*8 */
5289        }
5290
5291        tempbx += 4;
5292        tempcx += 4;
5293
5294        if (tempcx > (pVBInfo->VGAHT / 2))
5295            tempcx = pVBInfo->VGAHT / 2;
5296
5297        temp = tempbx & 0x00FF;
5298
5299        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x0B, temp);
5300    }
5301    else {
5302        temp = (pVBInfo->VGAHT - 1) & 0x0FF;    /* BTVGA2HT 0x08,0x09 */
5303        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x08, temp);
5304        temp = (((pVBInfo->VGAHT - 1) & 0xFF00) >> 8) << 4;
5305        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x09, ~0x0F0,
5306                        temp);
5307        temp = (pVBInfo->VGAHDE + 16) & 0x0FF;  /* BTVGA2HDEE 0x0A,0x0C */
5308        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x0A, temp);
5309        tempcx = (pVBInfo->VGAHT - pVBInfo->VGAHDE) >> 2;       /* cx */
5310        pushbx = pVBInfo->VGAHDE + 16;
5311        tempcx = tempcx >> 1;
5312        tempbx = pushbx + tempcx;       /* bx BTVGA@HRS 0x0B,0x0C */
5313        tempcx += tempbx;
5314
5315        if (pVBInfo->VBInfo & SetCRT2ToRAMDAC) {
5316            tempbx = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[3];
5317            tempbx |=
5318                ((pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[5] & 0xC0) << 2);
5319            tempbx = (tempbx - 3) << 3; /* (VGAHRS-3)*8 */
5320            tempcx = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[4];
5321            tempcx &= 0x1F;
5322            temp = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[6];
5323            temp = (temp & 0x04) << (5 - 2);    /* VGAHRE D[5] */
5324            tempcx = ((tempcx | temp) - 3) << 3;        /* (VGAHRE-3)*8 */
5325            tempbx += 16;
5326            tempcx += 16;
5327        }
5328
5329        if (tempcx > pVBInfo->VGAHT)
5330            tempcx = pVBInfo->VGAHT;
5331
5332        temp = tempbx & 0x00FF;
5333        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x0B, temp);
5334    }
5335
5336    tempax = (tempax & 0x00FF) | (tempbx & 0xFF00);
5337    tempbx = pushbx;
5338    tempbx = (tempbx & 0x00FF) | ((tempbx & 0xFF00) << 4);
5339    tempax |= (tempbx & 0xFF00);
5340    temp = (tempax & 0xFF00) >> 8;
5341    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x0C, temp);
5342    temp = tempcx & 0x00FF;
5343    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x0D, temp);
5344    tempcx = (pVBInfo->VGAVT - 1);
5345    temp = tempcx & 0x00FF;
5346
5347    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x0E, temp);
5348    tempbx = pVBInfo->VGAVDE - 1;
5349    temp = tempbx & 0x00FF;
5350    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x0F, temp);
5351    temp = ((tempbx & 0xFF00) << 3) >> 8;
5352    temp |= ((tempcx & 0xFF00) >> 8);
5353    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x12, temp);
5354
5355    tempax = pVBInfo->VGAVDE;
5356    tempbx = pVBInfo->VGAVDE;
5357    tempcx = pVBInfo->VGAVT;
5358    tempbx = (pVBInfo->VGAVT + pVBInfo->VGAVDE) >> 1;   /* BTVGA2VRS 0x10,0x11 */
5359    tempcx = ((pVBInfo->VGAVT - pVBInfo->VGAVDE) >> 4) + tempbx + 1;    /* BTVGA2VRE 0x11 */
5360
5361    if (pVBInfo->VBInfo & SetCRT2ToRAMDAC) {
5362        tempbx = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[10];
5363        temp = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[9];
5364
5365        if (temp & 0x04)
5366            tempbx |= 0x0100;
5367
5368        if (temp & 0x080)
5369            tempbx |= 0x0200;
5370
5371        temp = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[14];
5372
5373        if (temp & 0x08)
5374            tempbx |= 0x0400;
5375
5376        temp = pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[11];
5377        tempcx = (tempcx & 0xFF00) | (temp & 0x00FF);
5378    }
5379
5380    temp = tempbx & 0x00FF;
5381    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x10, temp);
5382    temp = ((tempbx & 0xFF00) >> 8) << 4;
5383    temp = ((tempcx & 0x000F) | (temp));
5384    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x11, temp);
5385    tempax = 0;
5386
5387    if (modeflag & DoubleScanMode)
5388        tempax |= 0x80;
5389
5390    if (modeflag & HalfDCLK)
5391        tempax |= 0x40;
5392
5393    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x2C, ~0x0C0, tempax);
5394}
5395
5396
5397/* --------------------------------------------------------------------- */
5398/* Function : XGI_SetLockRegs */
5399/* Input : */
5400/* Output : */
5401/* Description : */
5402/* --------------------------------------------------------------------- */
5403void
5404XGI_SetLockRegs(USHORT ModeNo, USHORT ModeIdIndex,
5405                PXGI_HW_DEVICE_INFO HwDeviceExtension,
5406                USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo)
5407{
5408    USHORT push1,
5409        push2, tempax, tempbx = 0, tempcx, temp, resinfo, modeflag, CRT1Index;
5410
5411    if (ModeNo <= 0x13) {
5412        modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;      /* si+St_ResInfo */
5413        resinfo = pVBInfo->SModeIDTable[ModeIdIndex].St_ResInfo;
5414    }
5415    else {
5416        modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;     /* si+Ext_ResInfo */
5417        resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
5418        CRT1Index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
5419        CRT1Index &= IndexMask;
5420    }
5421
5422    if (!(pVBInfo->VBInfo & SetInSlaveMode)) {
5423        return;
5424    }
5425
5426    temp = 0xFF;                /* set MAX HT */
5427    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x03, temp);
5428    /* if ( modeflag & Charx8Dot ) tempcx = 0x08 ; */
5429    /* else */
5430    tempcx = 0x08;
5431
5432    if (pVBInfo->VBType & (VB_XGI301LV | VB_XGI302LV | VB_XGI301C))
5433        modeflag |= Charx8Dot;
5434
5435    tempax = pVBInfo->VGAHDE;   /* 0x04 Horizontal Display End */
5436
5437    if (modeflag & HalfDCLK)
5438        tempax = tempax >> 1;
5439
5440    tempax = (tempax / tempcx) - 1;
5441    tempbx |= ((tempax & 0x00FF) << 8);
5442    temp = tempax & 0x00FF;
5443    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x04, temp);
5444
5445    temp = (tempbx & 0xFF00) >> 8;
5446
5447    if (pVBInfo->VBInfo & SetCRT2ToTV) {
5448        if (!
5449            (pVBInfo->
5450             VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV |
5451                       VB_XGI301C)))
5452            temp += 2;
5453
5454        if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {
5455            if (pVBInfo->VBType & VB_XGI301LV) {
5456                if (pVBInfo->VBExtInfo == VB_YPbPr1080i) {
5457                    if (resinfo == 7)
5458                        temp -= 2;
5459                }
5460            }
5461            else if (resinfo == 7)
5462                temp -= 2;
5463        }
5464    }
5465
5466    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x05, temp);  /* 0x05 Horizontal Display Start */
5467    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x06, 0x03);  /* 0x06 Horizontal Blank end */
5468
5469    if (!(pVBInfo->VBInfo & DisableCRT2Display)) {      /* 030226 bainy */
5470        if (pVBInfo->VBInfo & SetCRT2ToTV)
5471            tempax = pVBInfo->VGAHT;
5472        else
5473            tempax = XGI_GetVGAHT2(pVBInfo);
5474    }
5475
5476    if (tempax >= pVBInfo->VGAHT) {
5477        tempax = pVBInfo->VGAHT;
5478    }
5479
5480    if (modeflag & HalfDCLK) {
5481        tempax = tempax >> 1;
5482    }
5483
5484    tempax = (tempax / tempcx) - 5;
5485    tempcx = tempax;            /* 20030401 0x07 horizontal Retrace Start */
5486    if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {
5487        temp = (tempbx & 0x00FF) - 1;
5488        if (!(modeflag & HalfDCLK)) {
5489            temp -= 6;
5490            if (pVBInfo->TVInfo & TVSimuMode) {
5491                temp -= 4;
5492                if (ModeNo > 0x13)
5493                    temp -= 10;
5494            }
5495        }
5496    }
5497    else {
5498        /* tempcx = tempbx & 0x00FF ; */
5499        tempbx = (tempbx & 0xFF00) >> 8;
5500        tempcx = (tempcx + tempbx) >> 1;
5501        temp = (tempcx & 0x00FF) + 2;
5502
5503        if (pVBInfo->VBInfo & SetCRT2ToTV) {
5504            temp -= 1;
5505            if (!(modeflag & HalfDCLK)) {
5506                if ((modeflag & Charx8Dot)) {
5507                    temp += 4;
5508                    if (pVBInfo->VGAHDE >= 800) {
5509                        temp -= 6;
5510                    }
5511                }
5512            }
5513        }
5514        else {
5515            if (!(modeflag & HalfDCLK)) {
5516                temp -= 4;
5517                if (pVBInfo->LCDResInfo != Panel1280x960) {
5518                    if (pVBInfo->VGAHDE >= 800) {
5519                        temp -= 7;
5520                        if (pVBInfo->ModeType == ModeEGA) {
5521                            if (pVBInfo->VGAVDE == 1024) {
5522                                temp += 15;
5523                                if (pVBInfo->LCDResInfo != Panel1280x1024) {
5524                                    temp += 7;
5525                                }
5526                            }
5527                        }
5528
5529                        if (pVBInfo->VGAHDE >= 1280) {
5530                            if (pVBInfo->LCDResInfo != Panel1280x960) {
5531                                if (!
5532                                    (pVBInfo->
5533                                     LCDInfo & (LCDNonExpanding |
5534                                                EnableScalingLCD))) {
5535                                    temp += 28;
5536                                }
5537                            }
5538                        }
5539                    }
5540                }
5541            }
5542        }
5543    }
5544
5545    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x07, temp);  /* 0x07 Horizontal Retrace Start */
5546    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x08, 0);     /* 0x08 Horizontal Retrace End */
5547
5548    if (pVBInfo->VBInfo & SetCRT2ToTV) {
5549        if (pVBInfo->TVInfo & TVSimuMode) {
5550            if ((ModeNo == 0x06) || (ModeNo == 0x10) || (ModeNo == 0x11)
5551                || (ModeNo == 0x13) || (ModeNo == 0x0F)) {
5552                XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x07, 0x5b);
5553                XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x08, 0x03);
5554            }
5555
5556            if ((ModeNo == 0x00) || (ModeNo == 0x01)) {
5557                if (pVBInfo->TVInfo & SetNTSCTV) {
5558                    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x07, 0x2A);
5559                    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x08, 0x61);
5560                }
5561                else {
5562                    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x07, 0x2A);
5563                    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x08, 0x41);
5564                    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x0C, 0xF0);
5565                }
5566            }
5567
5568            if ((ModeNo == 0x02) || (ModeNo == 0x03) || (ModeNo == 0x07)) {
5569                if (pVBInfo->TVInfo & SetNTSCTV) {
5570                    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x07, 0x54);
5571                    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x08, 0x00);
5572                }
5573                else {
5574                    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x07, 0x55);
5575                    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x08, 0x00);
5576                    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x0C, 0xF0);
5577                }
5578            }
5579
5580            if ((ModeNo == 0x04) || (ModeNo == 0x05) || (ModeNo == 0x0D)
5581                || (ModeNo == 0x50)) {
5582                if (pVBInfo->TVInfo & SetNTSCTV) {
5583                    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x07, 0x30);
5584                    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x08, 0x03);
5585                }
5586                else {
5587                    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x07, 0x2f);
5588                    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x08, 0x02);
5589                }
5590            }
5591        }
5592    }
5593
5594    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x18, 0x03);  /* 0x18 SR0B */
5595    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x19, 0xF0, 0x00);
5596    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x09, 0xFF);  /* 0x09 Set Max VT */
5597
5598    tempbx = pVBInfo->VGAVT;
5599    push1 = tempbx;
5600    tempcx = 0x121;
5601    tempbx = pVBInfo->VGAVDE;   /* 0x0E Virtical Display End */
5602
5603    if (tempbx == 357)
5604        tempbx = 350;
5605    if (tempbx == 360)
5606        tempbx = 350;
5607    if (tempbx == 375)
5608        tempbx = 350;
5609    if (tempbx == 405)
5610        tempbx = 400;
5611    if (tempbx == 525)
5612        tempbx = 480;
5613
5614    push2 = tempbx;
5615
5616    if (pVBInfo->VBInfo & SetCRT2ToLCD) {
5617        if (pVBInfo->LCDResInfo == Panel1024x768) {
5618            if (!(pVBInfo->LCDInfo & LCDVESATiming)) {
5619                if (tempbx == 350)
5620                    tempbx += 5;
5621                if (tempbx == 480)
5622                    tempbx += 5;
5623            }
5624        }
5625    }
5626    tempbx--;
5627    temp = tempbx & 0x00FF;
5628    tempbx--;
5629    temp = tempbx & 0x00FF;
5630    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x10, temp);  /* 0x10 vertical Blank Start */
5631    tempbx = push2;
5632    tempbx--;
5633    temp = tempbx & 0x00FF;
5634    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x0E, temp);
5635
5636    if (tempbx & 0x0100) {
5637        tempcx |= 0x0002;
5638    }
5639
5640    tempax = 0x000B;
5641
5642    if (modeflag & DoubleScanMode) {
5643        tempax |= 0x08000;
5644    }
5645
5646    if (tempbx & 0x0200) {
5647        tempcx |= 0x0040;
5648    }
5649
5650    temp = (tempax & 0xFF00) >> 8;
5651    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x0B, temp);
5652
5653    if (tempbx & 0x0400) {
5654        tempcx |= 0x0600;
5655    }
5656
5657    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x11, 0x00);  /* 0x11 Vertival Blank End */
5658
5659    tempax = push1;
5660    tempax -= tempbx;           /* 0x0C Vertical Retrace Start */
5661    tempax = tempax >> 2;
5662    push1 = tempax;             /* push ax */
5663
5664    if (resinfo != 0x09) {
5665        tempax = tempax << 1;
5666        tempbx += tempax;
5667    }
5668
5669    if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {
5670        if (pVBInfo->VBType & VB_XGI301LV) {
5671            if (pVBInfo->TVInfo & SetYPbPrMode1080i)
5672                tempbx -= 10;
5673            else {
5674                if (pVBInfo->TVInfo & TVSimuMode) {
5675                    if (pVBInfo->TVInfo & SetPALTV) {
5676                        if (pVBInfo->VBType & VB_XGI301LV) {
5677                            if (!
5678                                (pVBInfo->
5679                                 TVInfo & (SetYPbPrMode525p | SetYPbPrMode750p
5680                                           | SetYPbPrMode1080i)))
5681                                tempbx += 40;
5682                        }
5683                        else
5684                            tempbx += 40;
5685                    }
5686                }
5687            }
5688        }
5689        else
5690            tempbx -= 10;
5691    }
5692    else {
5693        if (pVBInfo->TVInfo & TVSimuMode) {
5694            if (pVBInfo->TVInfo & SetPALTV) {
5695                if (pVBInfo->VBType & VB_XGI301LV) {
5696                    if (!
5697                        (pVBInfo->
5698                         TVInfo & (SetYPbPrMode525p | SetYPbPrMode750p |
5699                                   SetYPbPrMode1080i)))
5700                        tempbx += 40;
5701                }
5702                else
5703                    tempbx += 40;
5704            }
5705        }
5706    }
5707    tempax = push1;
5708    tempax = tempax >> 2;
5709    tempax++;
5710    tempax += tempbx;
5711    push1 = tempax;             /* push ax */
5712
5713    if ((pVBInfo->TVInfo & SetPALTV)) {
5714        if (tempbx <= 513) {
5715            if (tempax >= 513) {
5716                tempbx = 513;
5717            }
5718        }
5719    }
5720
5721    temp = tempbx & 0x00FF;
5722    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x0C, temp);
5723    tempbx--;
5724    temp = tempbx & 0x00FF;
5725    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x10, temp);
5726
5727    if (tempbx & 0x0100) {
5728        tempcx |= 0x0008;
5729    }
5730
5731    if (tempbx & 0x0200) {
5732        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x0B, 0x0FF, 0x20);
5733    }
5734
5735    tempbx++;
5736
5737    if (tempbx & 0x0100) {
5738        tempcx |= 0x0004;
5739    }
5740
5741    if (tempbx & 0x0200) {
5742        tempcx |= 0x0080;
5743    }
5744
5745    if (tempbx & 0x0400) {
5746        tempcx |= 0x0C00;
5747    }
5748
5749    tempbx = push1;             /* pop ax */
5750    temp = tempbx & 0x00FF;
5751    temp &= 0x0F;
5752    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x0D, temp);  /* 0x0D vertical Retrace End */
5753
5754    if (tempbx & 0x0010) {
5755        tempcx |= 0x2000;
5756    }
5757
5758    temp = tempcx & 0x00FF;
5759    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x0A, temp);  /* 0x0A CR07 */
5760    temp = (tempcx & 0x0FF00) >> 8;
5761    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x17, temp);  /* 0x17 SR0A */
5762    tempax = modeflag;
5763    temp = (tempax & 0xFF00) >> 8;
5764
5765    temp = (temp >> 1) & 0x09;
5766
5767    if (pVBInfo->VBType & (VB_XGI301LV | VB_XGI302LV | VB_XGI301C))
5768        temp |= 0x01;
5769
5770    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x16, temp);  /* 0x16 SR01 */
5771    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x0F, 0);     /* 0x0F CR14 */
5772    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x12, 0);     /* 0x12 CR17 */
5773
5774    if (pVBInfo->LCDInfo & LCDRGB18Bit)
5775        temp = 0x80;
5776    else
5777        temp = 0x00;
5778
5779    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x1A, temp);  /* 0x1A SR0E */
5780
5781    return;
5782}
5783
5784
5785/* --------------------------------------------------------------------- */
5786/* Function : XGI_SetGroup2 */
5787/* Input : */
5788/* Output : */
5789/* Description : */
5790/* --------------------------------------------------------------------- */
5791void
5792XGI_SetGroup2(USHORT ModeNo, USHORT ModeIdIndex, USHORT RefreshRateTableIndex,
5793              PXGI_HW_DEVICE_INFO HwDeviceExtension, PVB_DEVICE_INFO pVBInfo)
5794{
5795    USHORT i,
5796        j,
5797        tempax,
5798        tempbx, tempcx, temp, push1, push2, modeflag, resinfo, crt2crtc;
5799#ifndef LINUX_XF86
5800    USHORT temp1, temp3, resindex, xres;
5801#endif
5802/*           XGINew_RY1COE = 0 ,
5803           XGINew_RY2COE = 0 ,
5804           XGINew_RY3COE = 0 ,
5805           XGINew_RY4COE = 0 ,
5806           XGINew_RY5COE = 0 ,
5807           XGINew_RY6COE = 0 ,
5808           XGINew_RY7COE = 0 ;
5809*/
5810
5811#ifndef LINUX_XF86
5812    UCHAR *PhasePoint;
5813#endif
5814    const UCHAR *TimingPoint;
5815
5816    ULONG longtemp, tempeax, tempebx, temp2, tempecx;
5817
5818    if (ModeNo <= 0x13) {
5819        modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;      /* si+St_ResInfo */
5820        resinfo = pVBInfo->SModeIDTable[ModeIdIndex].St_ResInfo;
5821        crt2crtc = pVBInfo->SModeIDTable[ModeIdIndex].St_CRT2CRTC;
5822    }
5823    else {
5824        modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;     /* si+Ext_ResInfo */
5825        resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
5826        crt2crtc = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
5827    }
5828
5829    tempax = 0;
5830
5831    if (!(pVBInfo->VBInfo & SetCRT2ToAVIDEO))
5832        tempax |= 0x0800;
5833
5834    if (!(pVBInfo->VBInfo & SetCRT2ToSVIDEO))
5835        tempax |= 0x0400;
5836
5837    if (pVBInfo->VBInfo & SetCRT2ToSCART)
5838        tempax |= 0x0200;
5839
5840    if (!(pVBInfo->TVInfo & SetPALTV))
5841        tempax |= 0x1000;
5842
5843    if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV)
5844        tempax |= 0x0100;
5845
5846    if (pVBInfo->TVInfo & (SetYPbPrMode525p | SetYPbPrMode750p))
5847        tempax &= 0xfe00;
5848
5849    ErrorF("Part2 0 = %x ",
5850           XGI_GetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x0));
5851    ErrorF(" pVBInfo->VBInfo =%x", pVBInfo->VBInfo);
5852
5853    tempax = (tempax & 0xff00) >> 8;
5854    ErrorF("tempax = %x ", tempax);
5855    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x0, tempax);
5856    TimingPoint = pVBInfo->NTSCTiming;
5857
5858    if (pVBInfo->TVInfo & SetPALTV) {
5859        TimingPoint = pVBInfo->PALTiming;
5860    }
5861
5862    if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {
5863        TimingPoint = pVBInfo->HiTVExtTiming;
5864
5865        if (pVBInfo->VBInfo & SetInSlaveMode)
5866            TimingPoint = pVBInfo->HiTVSt2Timing;
5867
5868        if (pVBInfo->SetFlag & TVSimuMode)
5869            TimingPoint = pVBInfo->HiTVSt1Timing;
5870
5871        if (!(modeflag & Charx8Dot))
5872            TimingPoint = pVBInfo->HiTVTextTiming;
5873    }
5874
5875    if (pVBInfo->VBInfo & SetCRT2ToYPbPr) {
5876        if (pVBInfo->TVInfo & SetYPbPrMode525i)
5877            TimingPoint = pVBInfo->YPbPr525iTiming;
5878
5879        if (pVBInfo->TVInfo & SetYPbPrMode525p)
5880            TimingPoint = pVBInfo->YPbPr525pTiming;
5881
5882        if (pVBInfo->TVInfo & SetYPbPrMode750p)
5883            TimingPoint = pVBInfo->YPbPr750pTiming;
5884    }
5885
5886    for (i = 0x01, j = 0; i <= 0x2D; i++, j++) {
5887        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, i, TimingPoint[j]);
5888    }
5889
5890    for (i = 0x39; i <= 0x45; i++, j++) {
5891        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, i, TimingPoint[j]);       /* di->temp2[j] */
5892    }
5893
5894    if (pVBInfo->VBInfo & SetCRT2ToTV) {
5895        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part2Port, 0x3A, 0x1F, 0x00);
5896    }
5897
5898    temp = pVBInfo->NewFlickerMode;
5899    temp &= 0x80;
5900    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part2Port, 0x0A, 0xFF, temp);
5901
5902    if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV)
5903        tempax = 950;
5904
5905    if (pVBInfo->TVInfo & SetPALTV)
5906        tempax = 520;
5907    else
5908        tempax = 440;
5909
5910    if (pVBInfo->VDE <= tempax) {
5911        tempax -= pVBInfo->VDE;
5912        tempax = tempax >> 2;
5913        tempax = (tempax & 0x00FF) | ((tempax & 0x00FF) << 8);
5914        push1 = tempax;
5915        temp = (tempax & 0xFF00) >> 8;
5916        temp += (USHORT) TimingPoint[0];
5917
5918        if (pVBInfo->
5919            VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV |
5920                      VB_XGI301C)) {
5921            if (pVBInfo->
5922                VBInfo & (SetCRT2ToAVIDEO | SetCRT2ToSVIDEO | SetCRT2ToSCART |
5923                          SetCRT2ToYPbPr)) {
5924                tempcx = pVBInfo->VGAHDE;
5925                if (tempcx >= 1024) {
5926                    temp = 0x17;        /* NTSC */
5927                    if (pVBInfo->TVInfo & SetPALTV)
5928                        temp = 0x19;    /* PAL */
5929                }
5930            }
5931        }
5932
5933        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x01, temp);
5934        tempax = push1;
5935        temp = (tempax & 0xFF00) >> 8;
5936        temp += TimingPoint[1];
5937
5938        if (pVBInfo->
5939            VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV |
5940                      VB_XGI301C)) {
5941            if ((pVBInfo->
5942                 VBInfo & (SetCRT2ToAVIDEO | SetCRT2ToSVIDEO | SetCRT2ToSCART
5943                           | SetCRT2ToYPbPr))) {
5944                tempcx = pVBInfo->VGAHDE;
5945                if (tempcx >= 1024) {
5946                    temp = 0x1D;        /* NTSC */
5947                    if (pVBInfo->TVInfo & SetPALTV)
5948                        temp = 0x52;    /* PAL */
5949                }
5950            }
5951        }
5952        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x02, temp);
5953    }
5954
5955    /* 301b */
5956    tempcx = pVBInfo->HT;
5957
5958    if (XGI_IsLCDDualLink(pVBInfo))
5959        tempcx = tempcx >> 1;
5960
5961    tempcx -= 2;
5962    temp = tempcx & 0x00FF;
5963    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x1B, temp);
5964
5965    temp = (tempcx & 0xFF00) >> 8;
5966    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part2Port, 0x1D, ~0x0F, temp);
5967
5968    tempcx = pVBInfo->HT >> 1;
5969    push1 = tempcx;             /* push cx */
5970    tempcx += 7;
5971
5972    if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {
5973        tempcx -= 4;
5974    }
5975
5976    temp = tempcx & 0x00FF;
5977    temp = temp << 4;
5978    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part2Port, 0x22, 0x0F, temp);
5979
5980    tempbx = TimingPoint[j] | ((TimingPoint[j + 1]) << 8);
5981    tempbx += tempcx;
5982    push2 = tempbx;
5983    temp = tempbx & 0x00FF;
5984    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x24, temp);
5985    temp = (tempbx & 0xFF00) >> 8;
5986    temp = temp << 4;
5987    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part2Port, 0x25, 0x0F, temp);
5988
5989    tempbx = push2;
5990    tempbx = tempbx + 8;
5991    if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {
5992        tempbx = tempbx - 4;
5993        tempcx = tempbx;
5994    }
5995
5996    temp = (tempbx & 0x00FF) << 4;
5997    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part2Port, 0x29, 0x0F, temp);
5998
5999    j += 2;
6000    tempcx += (TimingPoint[j] | ((TimingPoint[j + 1]) << 8));
6001    temp = tempcx & 0x00FF;
6002    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x27, temp);
6003    temp = ((tempcx & 0xFF00) >> 8) << 4;
6004    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part2Port, 0x28, 0x0F, temp);
6005
6006    tempcx += 8;
6007    if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {
6008        tempcx -= 4;
6009    }
6010
6011    temp = tempcx & 0xFF;
6012    temp = temp << 4;
6013    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part2Port, 0x2A, 0x0F, temp);
6014
6015    tempcx = push1;             /* pop cx */
6016    j += 2;
6017    temp = TimingPoint[j] | ((TimingPoint[j + 1]) << 8);
6018    tempcx -= temp;
6019    temp = tempcx & 0x00FF;
6020    temp = temp << 4;
6021    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part2Port, 0x2D, 0x0F, temp);
6022
6023    tempcx -= 11;
6024
6025    if (!(pVBInfo->VBInfo & SetCRT2ToTV)) {
6026        tempax = XGI_GetVGAHT2(pVBInfo);
6027        tempcx = tempax - 1;
6028    }
6029    temp = tempcx & 0x00FF;
6030    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x2E, temp);
6031
6032    tempbx = pVBInfo->VDE;
6033
6034    if (pVBInfo->VGAVDE == 360)
6035        tempbx = 746;
6036    if (pVBInfo->VGAVDE == 375)
6037        tempbx = 746;
6038    if (pVBInfo->VGAVDE == 405)
6039        tempbx = 853;
6040
6041    if (pVBInfo->VBInfo & SetCRT2ToTV) {
6042        if (pVBInfo->VBType & (VB_XGI301LV | VB_XGI302LV | VB_XGI301C)) {
6043            if (!(pVBInfo->TVInfo & (SetYPbPrMode525p | SetYPbPrMode750p)))
6044                tempbx = tempbx >> 1;
6045        }
6046        else
6047            tempbx = tempbx >> 1;
6048    }
6049
6050    tempbx -= 2;
6051    temp = tempbx & 0x00FF;
6052
6053    if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {
6054        if (pVBInfo->VBType & VB_XGI301LV) {
6055            if (pVBInfo->TVInfo & SetYPbPrMode1080i) {
6056                if (pVBInfo->VBInfo & SetInSlaveMode) {
6057                    if (ModeNo == 0x2f)
6058                        temp += 1;
6059                }
6060            }
6061        }
6062        else {
6063            if (pVBInfo->VBInfo & SetInSlaveMode) {
6064                if (ModeNo == 0x2f)
6065                    temp += 1;
6066            }
6067        }
6068    }
6069
6070    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x2F, temp);
6071
6072    temp = (tempcx & 0xFF00) >> 8;
6073    temp |= ((tempbx & 0xFF00) >> 8) << 6;
6074
6075    if (!(pVBInfo->VBInfo & SetCRT2ToHiVisionTV)) {
6076        if (pVBInfo->VBType & VB_XGI301LV) {
6077            if (pVBInfo->TVInfo & SetYPbPrMode1080i) {
6078                temp |= 0x10;
6079
6080                if (!(pVBInfo->VBInfo & SetCRT2ToSVIDEO))
6081                    temp |= 0x20;
6082            }
6083        }
6084        else {
6085            temp |= 0x10;
6086            if (!(pVBInfo->VBInfo & SetCRT2ToSVIDEO))
6087                temp |= 0x20;
6088        }
6089    }
6090
6091    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x30, temp);
6092
6093    if (pVBInfo->VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV | VB_XGI301C)) { /* TV gatingno */
6094        tempbx = pVBInfo->VDE;
6095        tempcx = tempbx - 2;
6096
6097        if (pVBInfo->VBInfo & SetCRT2ToTV) {
6098            if (!(pVBInfo->TVInfo & (SetYPbPrMode525p | SetYPbPrMode750p)))
6099                tempbx = tempbx >> 1;
6100        }
6101
6102        if (pVBInfo->VBType & (VB_XGI302LV | VB_XGI301C)) {
6103            temp = 0;
6104            if (tempcx & 0x0400)
6105                temp |= 0x20;
6106
6107            if (tempbx & 0x0400)
6108                temp |= 0x40;
6109
6110            XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x10, temp);
6111        }
6112
6113        temp = (((tempbx - 3) & 0x0300) >> 8) << 5;
6114        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x46, temp);
6115        temp = (tempbx - 3) & 0x00FF;
6116        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x47, temp);
6117    }
6118
6119    tempbx = tempbx & 0x00FF;
6120
6121    if (!(modeflag & HalfDCLK)) {
6122        tempcx = pVBInfo->VGAHDE;
6123        if (tempcx >= pVBInfo->HDE) {
6124            tempbx |= 0x2000;
6125            tempax &= 0x00FF;
6126        }
6127    }
6128
6129    tempcx = 0x0101;
6130
6131    if (pVBInfo->VBInfo & SetCRT2ToTV) {        /*301b */
6132        if (pVBInfo->VGAHDE >= 1024) {
6133            tempcx = 0x1920;
6134            if (pVBInfo->VGAHDE >= 1280) {
6135                tempcx = 0x1420;
6136                tempbx = tempbx & 0xDFFF;
6137            }
6138        }
6139    }
6140
6141    if (!(tempbx & 0x2000)) {
6142        if (modeflag & HalfDCLK) {
6143            tempcx = (tempcx & 0xFF00) | ((tempcx & 0x00FF) << 1);
6144        }
6145
6146        push1 = tempbx;
6147        tempeax = pVBInfo->VGAHDE;
6148        tempebx = (tempcx & 0xFF00) >> 8;
6149        longtemp = tempeax * tempebx;
6150        tempecx = tempcx & 0x00FF;
6151        longtemp = longtemp / tempecx;
6152
6153        /* 301b */
6154        tempecx = 8 * 1024;
6155
6156        if (pVBInfo->
6157            VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV |
6158                      VB_XGI301C)) {
6159            tempecx = tempecx * 8;
6160        }
6161
6162        longtemp = longtemp * tempecx;
6163        tempecx = pVBInfo->HDE;
6164        temp2 = longtemp % tempecx;
6165        tempeax = longtemp / tempecx;
6166        if (temp2 != 0) {
6167            tempeax += 1;
6168        }
6169
6170        tempax = (USHORT) tempeax;
6171
6172        /* 301b */
6173        if (pVBInfo->
6174            VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV |
6175                      VB_XGI301C)) {
6176            tempcx = ((tempax & 0xFF00) >> 5) >> 8;
6177        }
6178        /* end 301b */
6179
6180        tempbx = push1;
6181        tempbx =
6182            (USHORT) (((tempeax & 0x0000FF00) & 0x1F00) | (tempbx & 0x00FF));
6183        tempax = (USHORT) (((tempeax & 0x000000FF) << 8) | (tempax & 0x00FF));
6184        temp = (tempax & 0xFF00) >> 8;
6185    }
6186    else {
6187        temp = (tempax & 0x00FF) >> 8;
6188    }
6189
6190    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x44, temp);
6191    temp = (tempbx & 0xFF00) >> 8;
6192    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part2Port, 0x45, ~0x03F, temp);
6193    temp = tempcx & 0x00FF;
6194
6195    if (tempbx & 0x2000)
6196        temp = 0;
6197
6198    if (!(pVBInfo->VBInfo & SetCRT2ToLCD))
6199        temp |= 0x18;
6200
6201    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part2Port, 0x46, ~0x1F, temp);
6202    if (pVBInfo->TVInfo & SetPALTV) {
6203        tempbx = 0x0382;
6204        tempcx = 0x007e;
6205    }
6206    else {
6207        tempbx = 0x0369;
6208        tempcx = 0x0061;
6209    }
6210
6211    temp = tempbx & 0x00FF;
6212    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x4b, temp);
6213    temp = tempcx & 0x00FF;
6214    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x4c, temp);
6215
6216    temp = ((tempcx & 0xFF00) >> 8) & 0x03;
6217    temp = temp << 2;
6218    temp |= ((tempbx & 0xFF00) >> 8) & 0x03;
6219
6220    if (pVBInfo->VBInfo & SetCRT2ToYPbPr) {
6221        temp |= 0x10;
6222
6223        if (pVBInfo->TVInfo & SetYPbPrMode525p)
6224            temp |= 0x20;
6225
6226        if (pVBInfo->TVInfo & SetYPbPrMode750p)
6227            temp |= 0x60;
6228    }
6229
6230    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x4d, temp);
6231    temp = XGI_GetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x43); /* 301b change */
6232    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x43, (USHORT) (temp - 3));
6233
6234    if (!(pVBInfo->TVInfo & (SetYPbPrMode525p | SetYPbPrMode750p))) {
6235        if (pVBInfo->TVInfo & NTSC1024x768) {
6236            TimingPoint = XGI_NTSC1024AdjTime;
6237            for (i = 0x1c, j = 0; i <= 0x30; i++, j++) {
6238                XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, i,
6239                           TimingPoint[j]);
6240            }
6241            XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x43, 0x72);
6242        }
6243    }
6244
6245    /* [ycchen] 01/14/03 Modify for 301C PALM Support */
6246    if (pVBInfo->VBType & VB_XGI301C) {
6247        if (pVBInfo->TVInfo & SetPALMTV)
6248            XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part2Port, 0x4E, ~0x08, 0x08);      /* PALM Mode */
6249    }
6250
6251    if (pVBInfo->TVInfo & SetPALMTV) {
6252        tempax = (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x01);
6253        tempax--;
6254        XGI_SetRegAND((XGIIOADDRESS) pVBInfo->Part2Port, 0x01, tempax);
6255
6256        /* if ( !( pVBInfo->VBType & VB_XGI301C ) ) */
6257        XGI_SetRegAND((XGIIOADDRESS) pVBInfo->Part2Port, 0x00, 0xEF);
6258    }
6259
6260    if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) {
6261        if (!(pVBInfo->VBInfo & SetInSlaveMode)) {
6262            XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x0B, 0x00);
6263        }
6264    }
6265
6266    if (pVBInfo->VBInfo & SetCRT2ToTV) {
6267        return;
6268    }
6269    ErrorF("5935 Part2 0 = %x ",
6270           XGI_GetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x0));
6271}
6272
6273
6274/* --------------------------------------------------------------------- */
6275/* Function : XGI_SetLCDRegs */
6276/* Input : */
6277/* Output : */
6278/* Description : */
6279/* --------------------------------------------------------------------- */
6280void
6281XGI_SetLCDRegs(USHORT ModeNo, USHORT ModeIdIndex,
6282               PXGI_HW_DEVICE_INFO HwDeviceExtension,
6283               USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo)
6284{
6285    ULONG push1,
6286        push2,
6287        pushbx,
6288        tempax,
6289        tempbx,
6290        tempcx, temp, tempah, tempbh, tempch, resinfo, modeflag, CRT1Index;
6291
6292    XGI_LCDDesStruct *LCDBDesPtr = NULL;
6293    XGI330_LCDDataDesStruct2 *LCDPtr1 = NULL;
6294
6295
6296    if (ModeNo <= 0x13) {
6297        modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;      /* si+St_ResInfo */
6298        resinfo = pVBInfo->SModeIDTable[ModeIdIndex].St_ResInfo;
6299    }
6300    else {
6301        modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;     /* si+Ext_ResInfo */
6302        resinfo = pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO;
6303        CRT1Index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
6304        CRT1Index &= IndexMask;
6305    }
6306
6307    if (!(pVBInfo->VBInfo & SetCRT2ToLCD)) {
6308        return;
6309    }
6310
6311    tempbx = pVBInfo->HDE;      /* RHACTE=HDE-1 */
6312
6313    if (XGI_IsLCDDualLink(pVBInfo))
6314        tempbx = tempbx >> 1;
6315
6316    tempbx -= 1;
6317    temp = tempbx & 0x00FF;
6318    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x2C, temp);
6319    temp = (tempbx & 0xFF00) >> 8;
6320    temp = temp << 4;
6321    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part2Port, 0x2B, 0x0F, temp);
6322    temp = 0x01;
6323
6324    if (pVBInfo->LCDResInfo == Panel1280x1024) {
6325        if (pVBInfo->ModeType == ModeEGA) {
6326            if (pVBInfo->VGAHDE >= 1024) {
6327                temp = 0x02;
6328                if (pVBInfo->LCDInfo & LCDVESATiming)
6329                    temp = 0x01;
6330            }
6331        }
6332    }
6333
6334    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x0B, temp);
6335    tempbx = pVBInfo->VDE;      /* RTVACTEO=(VDE-1)&0xFF */
6336    push1 = tempbx;
6337    tempbx--;
6338    temp = tempbx & 0x00FF;
6339    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x03, temp);
6340    temp = ((tempbx & 0xFF00) >> 8) & 0x07;
6341    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part2Port, 0x0C, ~0x07, temp);
6342
6343    tempcx = pVBInfo->VT - 1;
6344    push2 = tempcx + 1;
6345    temp = tempcx & 0x00FF;     /* RVTVT=VT-1 */
6346    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x19, temp);
6347    temp = (tempcx & 0xFF00) >> 8;
6348    temp = temp << 5;
6349    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x1A, temp);
6350    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part2Port, 0x09, 0xF0, 0x00);
6351    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part2Port, 0x0A, 0xF0, 0x00);
6352    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part2Port, 0x17, 0xFB, 0x00);
6353    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part2Port, 0x18, 0xDF, 0x00);
6354
6355    /* Customized LCDB Des no add */
6356    LCDBDesPtr = (XGI_LCDDesStruct *) XGI_GetLcdPtr(5, ModeNo, ModeIdIndex,
6357						    RefreshRateTableIndex,
6358						    pVBInfo);
6359
6360    if (pVBInfo->LCDInfo & EnableScalingLCD) {
6361        tempbx = pVBInfo->HDE;
6362        tempcx = pVBInfo->VDE;
6363    }
6364    else {
6365	get_HDE_VDE(pVBInfo, & tempbx, & tempcx);
6366    }
6367
6368    pushbx = tempbx;
6369    tempax = pVBInfo->VT;
6370    pVBInfo->LCDHDES = LCDBDesPtr->LCDHDES;
6371    pVBInfo->LCDHRS = LCDBDesPtr->LCDHRS;
6372    pVBInfo->LCDVDES = LCDBDesPtr->LCDVDES;
6373    pVBInfo->LCDVRS = LCDBDesPtr->LCDVRS;
6374    tempbx = pVBInfo->LCDVDES;
6375    tempcx += tempbx;
6376
6377    if (tempcx >= tempax)
6378        tempcx -= tempax;       /* lcdvdes */
6379
6380    temp = tempbx & 0x00FF;     /* RVEQ1EQ=lcdvdes */
6381    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x05, temp);
6382    temp = tempcx & 0x00FF;
6383    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x06, temp);
6384    tempch = ((tempcx & 0xFF00) >> 8) & 0x07;
6385    tempbh = ((tempbx & 0xFF00) >> 8) & 0x07;
6386    tempah = tempch;
6387    tempah = tempah << 3;
6388    tempah |= tempbh;
6389    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x02, tempah);
6390
6391    /* getlcdsync() */
6392    XGI_GetLCDSync(&tempax, &tempbx, pVBInfo);
6393    if (pVBInfo->LCDInfo & EnableScalingLCD) {
6394        LCDPtr1 =
6395            (XGI330_LCDDataDesStruct2 *) XGI_GetLcdPtr(4, ModeNo, ModeIdIndex,
6396                                                       RefreshRateTableIndex,
6397                                                       pVBInfo);
6398        tempbx = LCDPtr1->LCDVSync;
6399    }
6400    tempcx = tempbx;
6401    tempax = pVBInfo->VT;
6402    tempbx = pVBInfo->LCDVRS;
6403
6404    /* if ( SetLCD_Info & EnableScalingLCD ) */
6405    tempcx += tempbx;
6406    if (tempcx >= tempax)
6407        tempcx -= tempax;
6408
6409    temp = tempbx & 0x00FF;     /* RTVACTEE=lcdvrs */
6410    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x04, temp);
6411    temp = (tempbx & 0xFF00) >> 8;
6412    temp = temp << 4;
6413    temp |= (tempcx & 0x000F);
6414    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x01, temp);
6415    tempcx = pushbx;
6416    tempax = pVBInfo->HT;
6417    tempbx = pVBInfo->LCDHDES;
6418    tempbx &= 0x0FFF;
6419
6420    if (XGI_IsLCDDualLink(pVBInfo)) {
6421        tempax = tempax >> 1;
6422        tempbx = tempbx >> 1;
6423        tempcx = tempcx >> 1;
6424    }
6425
6426    if (pVBInfo->VBType & VB_XGI302LV)
6427        tempbx += 1;
6428
6429    if (pVBInfo->VBType & VB_XGI301C)   /* tap4 */
6430        tempbx += 1;
6431
6432    tempcx += tempbx;
6433
6434    if (tempcx >= tempax)
6435        tempcx -= tempax;
6436
6437    temp = tempbx & 0x00FF;
6438    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x1F, temp);  /* RHBLKE=lcdhdes */
6439    temp = ((tempbx & 0xFF00) >> 8) << 4;
6440    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x20, temp);
6441    temp = tempcx & 0x00FF;
6442    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x23, temp);  /* RHEQPLE=lcdhdee */
6443    temp = (tempcx & 0xFF00) >> 8;
6444    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x25, temp);
6445
6446    /* getlcdsync() */
6447    XGI_GetLCDSync(&tempax, &tempbx, pVBInfo);
6448    if (pVBInfo->LCDInfo & EnableScalingLCD) {
6449        LCDPtr1 =
6450            (XGI330_LCDDataDesStruct2 *) XGI_GetLcdPtr(4, ModeNo, ModeIdIndex,
6451                                                       RefreshRateTableIndex,
6452                                                       pVBInfo);
6453        tempax = LCDPtr1->LCDHSync;
6454    }
6455    tempcx = tempax;
6456    tempax = pVBInfo->HT;
6457    tempbx = pVBInfo->LCDHRS;
6458    /* if ( SetLCD_Info & EnableScalingLCD) */
6459    if (XGI_IsLCDDualLink(pVBInfo)) {
6460        tempax = tempax >> 1;
6461        tempbx = tempbx >> 1;
6462        tempcx = tempcx >> 1;
6463    }
6464
6465    if (pVBInfo->VBType & VB_XGI302LV)
6466        tempbx += 1;
6467
6468    tempcx += tempbx;
6469
6470    if (tempcx >= tempax)
6471        tempcx -= tempax;
6472
6473    temp = tempbx & 0x00FF;     /* RHBURSTS=lcdhrs */
6474    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x1C, temp);
6475
6476    temp = (tempbx & 0xFF00) >> 8;
6477    temp = temp << 4;
6478    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part2Port, 0x1D, ~0x0F0, temp);
6479    temp = tempcx & 0x00FF;     /* RHSYEXP2S=lcdhre */
6480    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x21, temp);
6481
6482    if (!(pVBInfo->LCDInfo & LCDVESATiming)) {
6483        if (pVBInfo->VGAVDE == 525) {
6484            if (pVBInfo->
6485                VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV
6486                          | VB_XGI301C)) {
6487                temp = 0xC6;
6488            }
6489            else
6490                temp = 0xC4;
6491
6492            XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x2f, temp);
6493            XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x30, 0xB3);
6494        }
6495
6496        if (pVBInfo->VGAVDE == 420) {
6497            if (pVBInfo->
6498                VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV
6499                          | VB_XGI301C)) {
6500                temp = 0x4F;
6501            }
6502            else
6503                temp = 0x4E;
6504            XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x2f, temp);
6505        }
6506    }
6507}
6508
6509
6510/* --------------------------------------------------------------------- */
6511/* Function : XGI_GetTap4Ptr */
6512/* Input : */
6513/* Output : di -> Tap4 Reg. Setting Pointer */
6514/* Description : */
6515/* --------------------------------------------------------------------- */
6516const XGI301C_Tap4TimingStruct *
6517XGI_GetTap4Ptr(USHORT tempcx, PVB_DEVICE_INFO pVBInfo)
6518{
6519    USHORT tempax, tempbx, i;
6520
6521    const XGI301C_Tap4TimingStruct *Tap4TimingPtr;
6522
6523    if (tempcx == 0) {
6524        tempax = pVBInfo->VGAHDE;
6525        tempbx = pVBInfo->HDE;
6526    }
6527    else {
6528        tempax = pVBInfo->VGAVDE;
6529        tempbx = pVBInfo->VDE;
6530    }
6531
6532    if (tempax < tempbx)
6533        return &EnlargeTap4Timing[0];
6534    else if (tempax == tempbx)
6535        return &NoScaleTap4Timing[0];   /* 1:1 */
6536    else
6537        Tap4TimingPtr = NTSCTap4Timing; /* NTSC */
6538
6539    if (pVBInfo->TVInfo & SetPALTV)
6540        Tap4TimingPtr = PALTap4Timing;
6541
6542
6543    if (pVBInfo->VBInfo & SetCRT2ToYPbPr) {
6544        if (pVBInfo->TVInfo & SetYPbPrMode525i)
6545            Tap4TimingPtr = YPbPr525iTap4Timing;
6546        if (pVBInfo->TVInfo & SetYPbPrMode525p)
6547            Tap4TimingPtr = YPbPr525pTap4Timing;
6548        if (pVBInfo->TVInfo & SetYPbPrMode750p)
6549            Tap4TimingPtr = YPbPr750pTap4Timing;
6550    }
6551
6552    if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV)
6553        Tap4TimingPtr = HiTVTap4Timing;
6554
6555    i = 0;
6556    while (Tap4TimingPtr[i].DE != 0xFFFF) {
6557        if (Tap4TimingPtr[i].DE == tempax)
6558            break;
6559        i++;
6560    }
6561    return &Tap4TimingPtr[i];
6562}
6563
6564
6565/* --------------------------------------------------------------------- */
6566/* Function : XGI_SetTap4Regs */
6567/* Input : */
6568/* Output : */
6569/* Description : */
6570/* --------------------------------------------------------------------- */
6571void
6572XGI_SetTap4Regs(PVB_DEVICE_INFO pVBInfo)
6573{
6574
6575#ifndef LINUX_XF86
6576    USHORT tempcx;
6577#endif
6578    USHORT i, j;
6579
6580    const XGI301C_Tap4TimingStruct *Tap4TimingPtr;
6581
6582    if (!(pVBInfo->VBType & VB_XGI301C))
6583        return;
6584
6585#ifndef Tap4
6586    XGI_SetRegAND((XGIIOADDRESS) pVBInfo->Part2Port, 0x4E, 0xEB);       /* Disable Tap4 */
6587#else /* Tap4 Setting */
6588
6589    Tap4TimingPtr = XGI_GetTap4Ptr(0, pVBInfo); /* Set Horizontal Scaling */
6590    for (i = 0x80, j = 0; i <= 0xBF; i++, j++)
6591        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, i,
6592                   Tap4TimingPtr->Reg[j]);
6593
6594    if ((pVBInfo->VBInfo & SetCRT2ToTV)
6595        && (!(pVBInfo->VBInfo & SetCRT2ToHiVisionTV))) {
6596        Tap4TimingPtr = XGI_GetTap4Ptr(1, pVBInfo);     /* Set Vertical Scaling */
6597        for (i = 0xC0, j = 0; i < 0xFF; i++, j++)
6598            XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, i,
6599                       Tap4TimingPtr->Reg[j]);
6600    }
6601
6602    if ((pVBInfo->VBInfo & SetCRT2ToTV)
6603        && (!(pVBInfo->VBInfo & SetCRT2ToHiVisionTV)))
6604        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part2Port, 0x4E, ~0x14, 0x04);  /* Enable V.Scaling */
6605    else
6606        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part2Port, 0x4E, ~0x14, 0x10);  /* Enable H.Scaling */
6607#endif
6608}
6609
6610/* --------------------------------------------------------------------- */
6611/* Function : XGI_SetGroup3 */
6612/* Input : */
6613/* Output : */
6614/* Description : */
6615/* --------------------------------------------------------------------- */
6616void
6617XGI_SetGroup3(USHORT ModeNo, USHORT ModeIdIndex, PVB_DEVICE_INFO pVBInfo)
6618{
6619    USHORT i;
6620    const UCHAR *tempdi;
6621    USHORT modeflag;
6622
6623    if (ModeNo <= 0x13) {
6624        modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;      /* si+St_ResInfo */
6625    }
6626    else {
6627        modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;     /* si+Ext_ResInfo */
6628    }
6629
6630
6631    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part3Port, 0x00, 0x00);
6632    if (pVBInfo->TVInfo & SetPALTV) {
6633        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part3Port, 0x13, 0xFA);
6634        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part3Port, 0x14, 0xC8);
6635    }
6636    else {
6637        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part3Port, 0x13, 0xF5);
6638        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part3Port, 0x14, 0xB7);
6639    }
6640
6641    if (!(pVBInfo->VBInfo & SetCRT2ToTV)) {
6642        return;
6643    }
6644
6645    if (pVBInfo->TVInfo & SetPALMTV) {
6646        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part3Port, 0x13, 0xFA);
6647        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part3Port, 0x14, 0xC8);
6648        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part3Port, 0x3D, 0xA8);
6649    }
6650
6651    if ((pVBInfo->VBInfo & SetCRT2ToHiVisionTV)
6652        || (pVBInfo->VBInfo & SetCRT2ToYPbPr)) {
6653        if (pVBInfo->TVInfo & SetYPbPrMode525i) {
6654            return;
6655        }
6656        tempdi = pVBInfo->HiTVGroup3Data;
6657        if (pVBInfo->SetFlag & TVSimuMode) {
6658            tempdi = pVBInfo->HiTVGroup3Simu;
6659            if (!(modeflag & Charx8Dot)) {
6660                tempdi = pVBInfo->HiTVGroup3Text;
6661            }
6662        }
6663
6664        if (pVBInfo->TVInfo & SetYPbPrMode525p) {
6665            tempdi = pVBInfo->Ren525pGroup3;
6666        }
6667        if (pVBInfo->TVInfo & SetYPbPrMode750p) {
6668            tempdi = pVBInfo->Ren750pGroup3;
6669        }
6670
6671        for (i = 0; i <= 0x3E; i++) {
6672            XGI_SetReg((XGIIOADDRESS) pVBInfo->Part3Port, i, tempdi[i]);
6673        }
6674        if (pVBInfo->VBType & VB_XGI301C) {     /* Marcovision */
6675            if (pVBInfo->TVInfo & SetYPbPrMode525p) {
6676                XGI_SetReg((XGIIOADDRESS) pVBInfo->Part3Port, 0x28, 0x3f);
6677            }
6678        }
6679    }
6680    return;
6681}                               /* {end of XGI_SetGroup3} */
6682
6683
6684/* --------------------------------------------------------------------- */
6685/* Function : XGI_SetGroup4 */
6686/* Input : */
6687/* Output : */
6688/* Description : */
6689/* --------------------------------------------------------------------- */
6690void
6691XGI_SetGroup4(USHORT ModeNo, USHORT ModeIdIndex, USHORT RefreshRateTableIndex,
6692              PXGI_HW_DEVICE_INFO HwDeviceExtension, PVB_DEVICE_INFO pVBInfo)
6693{
6694    USHORT tempax, tempcx, tempbx, modeflag, temp, temp2;
6695#ifndef LINUX_XF86
6696    USHORT push1;
6697#endif
6698
6699    ULONG tempebx, tempeax, templong;
6700
6701
6702    if (ModeNo <= 0x13) {
6703        modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;      /* si+St_ResInfo */
6704    }
6705    else {
6706        modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;     /* si+Ext_ResInfo */
6707    }
6708
6709    temp = pVBInfo->RVBHCFACT;
6710    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x13, temp);
6711
6712    tempbx = pVBInfo->RVBHCMAX;
6713    temp = tempbx & 0x00FF;
6714    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x14, temp);
6715    temp2 = ((tempbx & 0xFF00) >> 8) << 7;
6716    tempcx = pVBInfo->VGAHT - 1;
6717    temp = tempcx & 0x00FF;
6718    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x16, temp);
6719
6720    temp = ((tempcx & 0xFF00) >> 8) << 3;
6721    temp2 |= temp;
6722
6723    tempcx = pVBInfo->VGAVT - 1;
6724    if (!(pVBInfo->VBInfo & SetCRT2ToTV)) {
6725        tempcx -= 5;
6726    }
6727
6728    temp = tempcx & 0x00FF;
6729    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x17, temp);
6730    temp = temp2 | ((tempcx & 0xFF00) >> 8);
6731    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x15, temp);
6732    XGI_SetRegOR((XGIIOADDRESS) pVBInfo->Part4Port, 0x0D, 0x08);
6733    tempcx = pVBInfo->VBInfo;
6734    tempbx = pVBInfo->VGAHDE;
6735
6736    if (modeflag & HalfDCLK) {
6737        tempbx = tempbx >> 1;
6738    }
6739
6740    if (XGI_IsLCDDualLink(pVBInfo))
6741        tempbx = tempbx >> 1;
6742
6743    if (tempcx & SetCRT2ToHiVisionTV) {
6744        temp = 0;
6745        if (tempbx <= 1024)
6746            temp = 0xA0;
6747        if (tempbx == 1280)
6748            temp = 0xC0;
6749    }
6750    else if (tempcx & SetCRT2ToTV) {
6751        temp = 0xA0;
6752        if (tempbx <= 800)
6753            temp = 0x80;
6754    }
6755    else {
6756        temp = 0x80;
6757        if (pVBInfo->VBInfo & SetCRT2ToLCD) {
6758            temp = 0;
6759            if (tempbx > 800)
6760                temp = 0x60;
6761        }
6762    }
6763
6764    if (pVBInfo->TVInfo & (SetYPbPrMode525p | SetYPbPrMode750p)) {
6765        temp = 0x00;
6766        if (pVBInfo->VGAHDE == 1280)
6767            temp = 0x40;
6768        if (pVBInfo->VGAHDE == 1024)
6769            temp = 0x20;
6770    }
6771    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part4Port, 0x0E, ~0xEF, temp);
6772
6773    tempebx = pVBInfo->VDE;
6774
6775    if (tempcx & SetCRT2ToHiVisionTV) {
6776        if (!(temp & 0xE000))
6777            tempbx = tempbx >> 1;
6778    }
6779
6780    tempcx = pVBInfo->RVBHRS;
6781    temp = tempcx & 0x00FF;
6782    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x18, temp);
6783
6784    tempeax = pVBInfo->VGAVDE;
6785    tempcx |= 0x04000;
6786
6787
6788    if (tempeax <= tempebx) {
6789        tempcx = (tempcx & (~0x4000));
6790        tempeax = pVBInfo->VGAVDE;
6791    }
6792    else {
6793        tempeax -= tempebx;
6794    }
6795
6796
6797    templong = (tempeax * 256 * 1024) % tempebx;
6798    tempeax = (tempeax * 256 * 1024) / tempebx;
6799    tempebx = tempeax;
6800
6801    if (templong != 0) {
6802        tempebx++;
6803    }
6804
6805
6806    temp = (USHORT) (tempebx & 0x000000FF);
6807    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x1B, temp);
6808
6809    temp = (USHORT) ((tempebx & 0x0000FF00) >> 8);
6810    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x1A, temp);
6811    tempbx = (USHORT) (tempebx >> 16);
6812    temp = tempbx & 0x00FF;
6813    temp = temp << 4;
6814    temp |= ((tempcx & 0xFF00) >> 8);
6815    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x19, temp);
6816
6817    /* 301b */
6818    if (pVBInfo->
6819        VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV |
6820                  VB_XGI301C)) {
6821        temp = 0x0028;
6822        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x1C, temp);
6823        tempax = pVBInfo->VGAHDE;
6824        if (modeflag & HalfDCLK) {
6825            tempax = tempax >> 1;
6826        }
6827
6828        if (XGI_IsLCDDualLink(pVBInfo))
6829            tempax = tempax >> 1;
6830
6831        /* if((pVBInfo->VBInfo&(SetCRT2ToLCD))||((pVBInfo->TVInfo&SetYPbPrMode525p)||(pVBInfo->TVInfo&SetYPbPrMode750p))) { */
6832        if (pVBInfo->VBInfo & SetCRT2ToLCD) {
6833            if (tempax > 800)
6834                tempax -= 800;
6835        }
6836        else {
6837            if (pVBInfo->VGAHDE > 800) {
6838                if (pVBInfo->VGAHDE == 1024)
6839                    tempax = (tempax * 25 / 32) - 1;
6840                else
6841                    tempax = (tempax * 20 / 32) - 1;
6842            }
6843        }
6844        tempax -= 1;
6845
6846/*
6847        if ( pVBInfo->VBInfo & ( SetCRT2ToTV | SetCRT2ToHiVisionTV ) )
6848        {
6849            if ( pVBInfo->VBType & VB_XGI301LV )
6850            {
6851                if ( !( pVBInfo->TVInfo & ( SetYPbPrMode525p | SetYPbPrMode750p | SetYPbPrMode1080i ) ) )
6852                {
6853                    if ( pVBInfo->VGAHDE > 800 )
6854                    {
6855                        if ( pVBInfo->VGAHDE == 1024 )
6856                            tempax = ( tempax * 25 / 32 ) - 1 ;
6857                        else
6858                            tempax = ( tempax * 20 / 32 ) - 1 ;
6859                    }
6860                }
6861            }
6862            else
6863            {
6864                if ( pVBInfo->VGAHDE > 800 )
6865                {
6866                    if ( pVBInfo->VGAHDE == 1024 )
6867                        tempax = ( tempax * 25 / 32 ) - 1 ;
6868                    else
6869                        tempax = ( tempax * 20 / 32 ) - 1 ;
6870                }
6871            }
6872        }
6873*/
6874
6875        temp = (tempax & 0xFF00) >> 8;
6876        temp = ((temp & 0x0003) << 4);
6877        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x1E, temp);
6878        temp = (tempax & 0x00FF);
6879        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x1D, temp);
6880
6881        if (pVBInfo->VBInfo & (SetCRT2ToTV | SetCRT2ToHiVisionTV)) {
6882            if (pVBInfo->VGAHDE > 800) {
6883                XGI_SetRegOR((XGIIOADDRESS) pVBInfo->Part4Port, 0x1E, 0x08);
6884            }
6885        }
6886        temp = 0x0036;
6887
6888        if (pVBInfo->VBInfo & SetCRT2ToTV) {
6889            if (!
6890                (pVBInfo->
6891                 TVInfo & (NTSC1024x768 | SetYPbPrMode525p | SetYPbPrMode750p
6892                           | SetYPbPrMode1080i))) {
6893                temp |= 0x0001;
6894                if ((pVBInfo->VBInfo & SetInSlaveMode)
6895                    && (!(pVBInfo->TVInfo & TVSimuMode)))
6896                    temp &= (~0x0001);
6897            }
6898        }
6899
6900        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part4Port, 0x1F, 0x00C0,
6901                        temp);
6902        tempbx = pVBInfo->HT;
6903        if (XGI_IsLCDDualLink(pVBInfo))
6904            tempbx = tempbx >> 1;
6905        tempbx = (tempbx >> 1) - 2;
6906        temp = ((tempbx & 0x0700) >> 8) << 3;
6907        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part4Port, 0x21, 0x00C0,
6908                        temp);
6909        temp = tempbx & 0x00FF;
6910        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x22, temp);
6911    }
6912    /* end 301b */
6913
6914    if (pVBInfo->ISXPDOS == 0)
6915        XGI_SetCRT2VCLK(ModeNo, ModeIdIndex, RefreshRateTableIndex, pVBInfo);
6916}
6917
6918
6919/* --------------------------------------------------------------------- */
6920/* Function : XGI_SetGroup5 */
6921/* Input : */
6922/* Output : */
6923/* Description : */
6924/* --------------------------------------------------------------------- */
6925void
6926XGI_SetGroup5(USHORT ModeNo, USHORT ModeIdIndex, PVB_DEVICE_INFO pVBInfo)
6927{
6928    USHORT Pindex, Pdata;
6929
6930    Pindex = pVBInfo->Part5Port;
6931    Pdata = pVBInfo->Part5Port + 1;
6932    if (pVBInfo->ModeType == ModeVGA) {
6933        if (!
6934            (pVBInfo->
6935             VBInfo & (SetInSlaveMode | LoadDACFlag | CRT2DisplayFlag))) {
6936            XGINew_EnableCRT2(pVBInfo);
6937            /* LoadDAC2(pVBInfo->Part5Port,ModeNo,ModeIdIndex); */
6938        }
6939    }
6940    return;
6941}
6942
6943
6944/* --------------------------------------------------------------------- */
6945/* Function : XGI_GetLcdPtr */
6946/* Input : */
6947/* Output : */
6948/* Description : */
6949/* --------------------------------------------------------------------- */
6950const void *
6951XGI_GetLcdPtr(USHORT BX, USHORT ModeNo, USHORT ModeIdIndex,
6952              USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo)
6953{
6954    USHORT i, tempdx, tempcx, tempbx, tempal, modeflag, table;
6955
6956    const XGI330_LCDDataTablStruct *tempdi = 0;
6957
6958
6959    tempbx = BX;
6960
6961    if (ModeNo <= 0x13) {
6962        modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
6963        tempal = pVBInfo->SModeIDTable[ModeIdIndex].St_CRT2CRTC;
6964    }
6965    else {
6966        modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
6967        tempal = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
6968    }
6969
6970    if (pVBInfo->LCDInfo & EnableScalingLCD) {  /* ScaleLCD */
6971        if (pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO == 0x14) {
6972            tempal = 0x0A;
6973        }
6974        else if (pVBInfo->EModeIDTable[ModeIdIndex].Ext_RESINFO == 0x0F) {
6975            tempal = 0x0B;
6976        }
6977    }
6978
6979    tempal = tempal & 0x0f;
6980
6981    if (tempbx <= 1) {          /* ExpLink */
6982        if (ModeNo <= 0x13) {
6983            tempal = pVBInfo->SModeIDTable[ModeIdIndex].St_CRT2CRTC;    /* find no Ext_CRT2CRTC2 */
6984        }
6985        else {
6986            tempal = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
6987        }
6988
6989        if (pVBInfo->VBInfo & SetCRT2ToLCDA) {
6990            if (ModeNo <= 0x13)
6991                tempal = pVBInfo->SModeIDTable[ModeIdIndex].St_CRT2CRTC2;
6992            else
6993                tempal =
6994                    pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC2;
6995        }
6996
6997        if (tempbx & 0x01)
6998            tempal = (tempal >> 4);
6999
7000        tempal = (tempal & 0x0f);
7001    }
7002
7003    tempcx = LCDLenList[tempbx];        /* mov cl,byte ptr cs:LCDLenList[bx] */
7004
7005    if (pVBInfo->LCDInfo & EnableScalingLCD) {  /* ScaleLCD */
7006        if ((tempbx == 5) || (tempbx) == 7)
7007            tempcx = LCDDesDataLen2;
7008        else if ((tempbx == 3) || (tempbx == 8))
7009            tempcx = LVDSDesDataLen2;
7010    }
7011    /* mov di, word ptr cs:LCDDataList[bx] */
7012    /* tempdi=pVideoMemory[LCDDataList+tempbx*2]|(pVideoMemory[LCDDataList+tempbx*2+1]<<8); */
7013
7014    switch (tempbx) {
7015    case 0:
7016        tempdi = XGI_EPLLCDCRT1Ptr_H;
7017        break;
7018    case 1:
7019        tempdi = XGI_EPLLCDCRT1Ptr_V;
7020        break;
7021    case 2:
7022        tempdi = XGI_EPLLCDDataPtr;
7023        break;
7024    case 3:
7025        tempdi = XGI_EPLLCDDesDataPtr;
7026        break;
7027    case 4:
7028        tempdi = XGI_LCDDataTable;
7029        break;
7030    case 5:
7031        tempdi = XGI_LCDDesDataTable;
7032        break;
7033    case 6:
7034        tempdi = XGI_EPLCHLCDRegPtr;
7035        break;
7036    case 7:
7037    case 8:
7038    case 9:
7039        tempdi = 0;
7040        break;
7041    default:
7042        break;
7043    }
7044
7045    if (tempdi == 0x00)         /* OEMUtil */
7046        return 0;
7047
7048    table = tempbx;
7049    i = 0;
7050
7051    while (tempdi[i].PANELID != 0xff) {
7052        tempdx = pVBInfo->LCDResInfo;
7053        if (tempbx & 0x0080) {  /* OEMUtil */
7054            tempbx &= (~0x0080);
7055            tempdx = pVBInfo->LCDTypeInfo;
7056        }
7057
7058        if (pVBInfo->LCDInfo & EnableScalingLCD) {
7059            if ((pVBInfo->LCDInfo & EnableReduceTiming)
7060                && (pVBInfo->LCDResInfo == Panel1600x1200)) {
7061                tempdx = Panel1600x1200_1;
7062            }
7063            else {
7064                tempdx &= (~PanelResInfo);
7065            }
7066        }
7067        if (tempdi[i].PANELID == tempdx) {
7068            tempbx = tempdi[i].MASK;
7069            tempdx = pVBInfo->LCDInfo;
7070
7071            if (ModeNo <= 0x13) /* alan 09/10/2003 */
7072                tempdx |= SetLCDStdMode;
7073
7074            if (modeflag & HalfDCLK)
7075                tempdx |= SetLCDLowResolution;
7076
7077            tempbx &= tempdx;
7078            if (tempbx == tempdi[i].CAP)
7079                break;
7080        }
7081        i++;
7082    }
7083
7084    if (table == 0) {
7085        switch (tempdi[i].DATAPTR) {
7086        case 0:
7087            return &XGI_LVDSCRT11024x768_1_H[tempal];
7088            break;
7089        case 1:
7090            return &XGI_LVDSCRT11024x768_2_H[tempal];
7091            break;
7092        case 2:
7093            return &XGI_LVDSCRT11280x1024_1_H[tempal];
7094            break;
7095        case 3:
7096            return &XGI_LVDSCRT11280x1024_2_H[tempal];
7097            break;
7098        case 4:
7099            return &XGI_LVDSCRT11400x1050_1_H[tempal];
7100            break;
7101        case 5:
7102            return &XGI_LVDSCRT11400x1050_2_H[tempal];
7103            break;
7104        case 6:
7105            return &XGI_LVDSCRT11600x1200_1_H[tempal];
7106            break;
7107        case 7:
7108            return &XGI_LVDSCRT11024x768_1_Hx75[tempal];
7109            break;
7110        case 8:
7111            return &XGI_LVDSCRT11024x768_2_Hx75[tempal];
7112            break;
7113        case 9:
7114            return &XGI_LVDSCRT11280x1024_1_Hx75[tempal];
7115            break;
7116        case 10:
7117            return &XGI_LVDSCRT11280x1024_2_Hx75[tempal];
7118            break;
7119        default:
7120            break;
7121        }
7122    }
7123    else if (table == 1) {
7124        switch (tempdi[i].DATAPTR) {
7125        case 0:
7126            return &XGI_LVDSCRT11024x768_1_V[tempal];
7127            break;
7128        case 1:
7129            return &XGI_LVDSCRT11024x768_2_V[tempal];
7130            break;
7131        case 2:
7132            return &XGI_LVDSCRT11280x1024_1_V[tempal];
7133            break;
7134        case 3:
7135            return &XGI_LVDSCRT11280x1024_2_V[tempal];
7136            break;
7137        case 4:
7138            return &XGI_LVDSCRT11400x1050_1_V[tempal];
7139            break;
7140        case 5:
7141            return &XGI_LVDSCRT11400x1050_2_V[tempal];
7142            break;
7143        case 6:
7144            return &XGI_LVDSCRT11600x1200_1_V[tempal];
7145            break;
7146        case 7:
7147            return &XGI_LVDSCRT11024x768_1_Vx75[tempal];
7148            break;
7149        case 8:
7150            return &XGI_LVDSCRT11024x768_2_Vx75[tempal];
7151            break;
7152        case 9:
7153            return &XGI_LVDSCRT11280x1024_1_Vx75[tempal];
7154            break;
7155        case 10:
7156            return &XGI_LVDSCRT11280x1024_2_Vx75[tempal];
7157            break;
7158        default:
7159            break;
7160        }
7161    }
7162    else if (table == 2) {
7163        switch (tempdi[i].DATAPTR) {
7164        case 0:
7165            return &XGI_LVDS1024x768Data_1[tempal];
7166            break;
7167        case 1:
7168            return &XGI_LVDS1024x768Data_2[tempal];
7169            break;
7170        case 2:
7171            return &XGI_LVDS1280x1024Data_1[tempal];
7172            break;
7173        case 3:
7174            return &XGI_LVDS1280x1024Data_2[tempal];
7175            break;
7176        case 4:
7177            return &XGI_LVDS1400x1050Data_1[tempal];
7178            break;
7179        case 5:
7180            return &XGI_LVDS1400x1050Data_2[tempal];
7181            break;
7182        case 6:
7183            return &XGI_LVDS1600x1200Data_1[tempal];
7184            break;
7185        case 7:
7186            return &XGI_LVDSNoScalingData[tempal];
7187            break;
7188        case 8:
7189            return &XGI_LVDS1024x768Data_1x75[tempal];
7190            break;
7191        case 9:
7192            return &XGI_LVDS1024x768Data_2x75[tempal];
7193            break;
7194        case 10:
7195            return &XGI_LVDS1280x1024Data_1x75[tempal];
7196            break;
7197        case 11:
7198            return &XGI_LVDS1280x1024Data_2x75[tempal];
7199            break;
7200        case 12:
7201            return &XGI_LVDSNoScalingDatax75[tempal];
7202            break;
7203        default:
7204            break;
7205        }
7206    }
7207    else if (table == 3) {
7208        switch (tempdi[i].DATAPTR) {
7209        case 0:
7210            return &XGI_LVDS1024x768Des_1[tempal];
7211            break;
7212        case 1:
7213            return &XGI_LVDS1024x768Des_3[tempal];
7214            break;
7215        case 2:
7216            return &XGI_LVDS1024x768Des_2[tempal];
7217            break;
7218        case 3:
7219            return &XGI_LVDS1280x1024Des_1[tempal];
7220            break;
7221        case 4:
7222            return &XGI_LVDS1280x1024Des_2[tempal];
7223            break;
7224        case 5:
7225            return &XGI_LVDS1400x1050Des_1[tempal];
7226            break;
7227        case 6:
7228            return &XGI_LVDS1400x1050Des_2[tempal];
7229            break;
7230        case 7:
7231            return &XGI_LVDS1600x1200Des_1[tempal];
7232            break;
7233        case 8:
7234            return &XGI_LVDSNoScalingDesData[tempal];
7235            break;
7236        case 9:
7237            return &XGI_LVDS1024x768Des_1x75[tempal];
7238            break;
7239        case 10:
7240            return &XGI_LVDS1024x768Des_3x75[tempal];
7241            break;
7242        case 11:
7243            return &XGI_LVDS1024x768Des_2x75[tempal];
7244            break;
7245        case 12:
7246            return &XGI_LVDS1280x1024Des_1x75[tempal];
7247            break;
7248        case 13:
7249            return &XGI_LVDS1280x1024Des_2x75[tempal];
7250            break;
7251        case 14:
7252            return &XGI_LVDSNoScalingDesDatax75[tempal];
7253            break;
7254        default:
7255            break;
7256        }
7257    }
7258    else if (table == 4) {
7259        switch (tempdi[i].DATAPTR) {
7260        case 0:
7261            return &XGI_ExtLCD1024x768Data[tempal];
7262            break;
7263        case 1:
7264            return &XGI_StLCD1024x768Data[tempal];
7265            break;
7266        case 2:
7267            return &XGI_CetLCD1024x768Data[tempal];
7268            break;
7269        case 3:
7270            return &XGI_ExtLCD1280x1024Data[tempal];
7271            break;
7272        case 4:
7273            return &XGI_StLCD1280x1024Data[tempal];
7274            break;
7275        case 5:
7276            return &XGI_CetLCD1280x1024Data[tempal];
7277            break;
7278        case 6:
7279            return &XGI_ExtLCD1400x1050Data[tempal];
7280            break;
7281        case 7:
7282            return &XGI_StLCD1400x1050Data[tempal];
7283            break;
7284        case 8:
7285            return &XGI_CetLCD1400x1050Data[tempal];
7286            break;
7287        case 9:
7288            return &XGI_ExtLCD1600x1200Data[tempal];
7289            break;
7290        case 10:
7291            return &XGI_StLCD1600x1200Data[tempal];
7292            break;
7293        case 11:
7294            return &XGI_NoScalingData[tempal];
7295            break;
7296        case 12:
7297            return &XGI_ExtLCD1024x768x75Data[tempal];
7298            break;
7299        case 13:
7300            return &XGI_ExtLCD1024x768x75Data[tempal];
7301            break;
7302        case 14:
7303            return &XGI_CetLCD1024x768x75Data[tempal];
7304            break;
7305        case 15:
7306            return &XGI_ExtLCD1280x1024x75Data[tempal];
7307            break;
7308        case 16:
7309            return &XGI_StLCD1280x1024x75Data[tempal];
7310            break;
7311        case 17:
7312            return &XGI_CetLCD1280x1024x75Data[tempal];
7313            break;
7314        case 18:
7315            return &XGI_NoScalingDatax75[tempal];
7316            break;
7317        case 19:
7318            return &XGI_NoScalingData_1[tempal];
7319            break;
7320        default:
7321            break;
7322        }
7323    }
7324    else if (table == 5) {
7325        switch (tempdi[i].DATAPTR) {
7326        case 0:
7327            return &XGI_ExtLCDDes1024x768Data[tempal];
7328            break;
7329        case 1:
7330            return &XGI_StLCDDes1024x768Data[tempal];
7331            break;
7332        case 2:
7333            return &XGI_CetLCDDes1024x768Data[tempal];
7334            break;
7335        case 3:
7336            if ((pVBInfo->VBType & VB_XGI301LV)
7337                || (pVBInfo->VBType & VB_XGI302LV))
7338                return &XGI_ExtLCDDLDes1280x1024Data[tempal];
7339            else
7340                return &XGI_ExtLCDDes1280x1024Data[tempal];
7341            break;
7342        case 4:
7343            if ((pVBInfo->VBType & VB_XGI301LV)
7344                || (pVBInfo->VBType & VB_XGI302LV))
7345                return &XGI_StLCDDLDes1280x1024Data[tempal];
7346            else
7347                return &XGI_StLCDDes1280x1024Data[tempal];
7348            break;
7349        case 5:
7350            if ((pVBInfo->VBType & VB_XGI301LV)
7351                || (pVBInfo->VBType & VB_XGI302LV))
7352                return &XGI_CetLCDDLDes1280x1024Data[tempal];
7353            else
7354                return &XGI_CetLCDDes1280x1024Data[tempal];
7355            break;
7356        case 6:
7357            if ((pVBInfo->VBType & VB_XGI301LV)
7358                || (pVBInfo->VBType & VB_XGI302LV))
7359                return &XGI_ExtLCDDLDes1400x1050Data[tempal];
7360            else
7361                return &XGI_ExtLCDDes1400x1050Data[tempal];
7362            break;
7363        case 7:
7364            if ((pVBInfo->VBType & VB_XGI301LV)
7365                || (pVBInfo->VBType & VB_XGI302LV))
7366                return &XGI_StLCDDLDes1400x1050Data[tempal];
7367            else
7368                return &XGI_StLCDDes1400x1050Data[tempal];
7369            break;
7370        case 8:
7371            return &XGI_CetLCDDes1400x1050Data[tempal];
7372            break;
7373        case 9:
7374            return &XGI_CetLCDDes1400x1050Data2[tempal];
7375            break;
7376        case 10:
7377            if ((pVBInfo->VBType & VB_XGI301LV)
7378                || (pVBInfo->VBType & VB_XGI302LV))
7379                return &XGI_ExtLCDDLDes1600x1200Data[tempal];
7380            else
7381                return &XGI_ExtLCDDes1600x1200Data[tempal];
7382            break;
7383        case 11:
7384            if ((pVBInfo->VBType & VB_XGI301LV)
7385                || (pVBInfo->VBType & VB_XGI302LV))
7386                return &XGI_StLCDDLDes1600x1200Data[tempal];
7387            else
7388                return &XGI_StLCDDes1600x1200Data[tempal];
7389            break;
7390        case 12:
7391            return &XGI_NoScalingDesData[tempal];
7392            break;
7393        case 13:
7394            return &XGI_ExtLCDDes1024x768x75Data[tempal];
7395            break;
7396        case 14:
7397            return &XGI_StLCDDes1024x768x75Data[tempal];
7398            break;
7399        case 15:
7400            return &XGI_CetLCDDes1024x768x75Data[tempal];
7401            break;
7402        case 16:
7403            if ((pVBInfo->VBType & VB_XGI301LV)
7404                || (pVBInfo->VBType & VB_XGI302LV))
7405                return &XGI_ExtLCDDLDes1280x1024x75Data[tempal];
7406            else
7407                return &XGI_ExtLCDDes1280x1024x75Data[tempal];
7408            break;
7409        case 17:
7410            if ((pVBInfo->VBType & VB_XGI301LV)
7411                || (pVBInfo->VBType & VB_XGI302LV))
7412                return &XGI_StLCDDLDes1280x1024x75Data[tempal];
7413            else
7414                return &XGI_StLCDDes1280x1024x75Data[tempal];
7415            break;
7416        case 18:
7417            if ((pVBInfo->VBType & VB_XGI301LV)
7418                || (pVBInfo->VBType & VB_XGI302LV))
7419                return &XGI_CetLCDDLDes1280x1024x75Data[tempal];
7420            else
7421                return &XGI_CetLCDDes1280x1024x75Data[tempal];
7422            break;
7423        case 19:
7424            return &XGI_NoScalingDesDatax75[tempal];
7425            break;
7426        case 20:
7427            return &XGI_NoScalingDesData_1[tempal];
7428            break;
7429        default:
7430            break;
7431        }
7432    }
7433    else if (table == 6) {
7434        switch (tempdi[i].DATAPTR) {
7435        case 0:
7436            return &XGI_CH7017LV1024x768[tempal];
7437            break;
7438        case 1:
7439            return &XGI_CH7017LV1400x1050[tempal];
7440            break;
7441        default:
7442            break;
7443        }
7444    }
7445    return 0;
7446}
7447
7448
7449/* --------------------------------------------------------------------- */
7450/* Function : XGI_GetTVPtr */
7451/* Input : */
7452/* Output : */
7453/* Description : */
7454/* --------------------------------------------------------------------- */
7455const void *
7456XGI_GetTVPtr(USHORT BX, USHORT ModeNo, USHORT ModeIdIndex,
7457             USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo)
7458{
7459    USHORT i, tempdx, tempbx, tempal, modeflag, table;
7460    const XGI330_TVDataTablStruct *tempdi = NULL;
7461
7462    tempbx = BX;
7463
7464    if (ModeNo <= 0x13) {
7465        modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
7466        tempal = pVBInfo->SModeIDTable[ModeIdIndex].St_CRT2CRTC;
7467    }
7468    else {
7469        modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
7470        tempal = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT2CRTC;
7471    }
7472
7473    tempal = tempal & 0x3f;
7474    table = tempbx;
7475
7476    switch (tempbx) {
7477    case 0:
7478        tempdi = 0;             /*EPLCHTVCRT1Ptr_H; */
7479        break;
7480    case 1:
7481        tempdi = 0;             /*EPLCHTVCRT1Ptr_V; */
7482        break;
7483    case 2:
7484        tempdi = XGI_EPLCHTVDataPtr;
7485        break;
7486    case 3:
7487        tempdi = 0;
7488        break;
7489    case 4:
7490        tempdi = XGI_TVDataTable;
7491        break;
7492    case 5:
7493        tempdi = 0;
7494        break;
7495    case 6:
7496        tempdi = XGI_EPLCHTVRegPtr;
7497        break;
7498    default:
7499        break;
7500    }
7501
7502    if (tempdi == 0x00)         /* OEMUtil */
7503        return (0);
7504
7505    tempdx = pVBInfo->TVInfo;
7506
7507    if (pVBInfo->VBInfo & SetInSlaveMode)
7508        tempdx = tempdx | SetTVLockMode;
7509
7510    if (modeflag & HalfDCLK)
7511        tempdx = tempdx | SetTVLowResolution;
7512
7513    i = 0;
7514
7515    while (tempdi[i].MASK != 0xffff) {
7516        if ((tempdx & tempdi[i].MASK) == tempdi[i].CAP)
7517            break;
7518        i++;
7519    }
7520
7521    if (table == 0x04) {
7522        switch (tempdi[i].DATAPTR) {
7523        case 0:
7524            return &XGI_ExtPALData[tempal];
7525            break;
7526        case 1:
7527            return &XGI_ExtNTSCData[tempal];
7528            break;
7529        case 2:
7530            return &XGI_StPALData[tempal];
7531            break;
7532        case 3:
7533            return &XGI_StNTSCData[tempal];
7534            break;
7535        case 4:
7536            return &XGI_ExtHiTVData[tempal];
7537            break;
7538        case 5:
7539            return &XGI_St2HiTVData[tempal];
7540            break;
7541        case 6:
7542            return &XGI_ExtYPbPr525iData[tempal];
7543            break;
7544        case 7:
7545            return &XGI_ExtYPbPr525pData[tempal];
7546            break;
7547        case 8:
7548            return &XGI_ExtYPbPr750pData[tempal];
7549            break;
7550        case 9:
7551            return &XGI_StYPbPr525iData[tempal];
7552            break;
7553        case 10:
7554            return &XGI_StYPbPr525pData[tempal];
7555            break;
7556        case 11:
7557            return &XGI_StYPbPr750pData[tempal];
7558            break;
7559        case 12:               /* avoid system hang */
7560            return &XGI_ExtNTSCData[tempal];
7561            break;
7562        case 13:
7563            return &XGI_St1HiTVData[tempal];
7564            break;
7565        default:
7566            break;
7567        }
7568    }
7569    else if (table == 0x02) {
7570        switch (tempdi[i].DATAPTR) {
7571        case 0:
7572            return &XGI_CHTVUNTSCData[tempal];
7573            break;
7574        case 1:
7575            return &XGI_CHTVONTSCData[tempal];
7576            break;
7577        case 2:
7578            return &XGI_CHTVUPALData[tempal];
7579            break;
7580        case 3:
7581            return &XGI_CHTVOPALData[tempal];
7582            break;
7583        default:
7584            break;
7585        }
7586    }
7587    else if (table == 0x06) {
7588        switch (tempdi[i].DATAPTR) {
7589        case 0:
7590            return &XGI_CHTVRegUNTSC[tempal];
7591            break;
7592        case 1:
7593            return &XGI_CHTVRegONTSC[tempal];
7594            break;
7595        case 2:
7596            return &XGI_CHTVRegUPAL[tempal];
7597            break;
7598        case 3:
7599            return &XGI_CHTVRegOPAL[tempal];
7600            break;
7601        default:
7602            break;
7603        }
7604    }
7605    return (0);
7606}
7607
7608
7609/* --------------------------------------------------------------------- */
7610/* Function : XGI_BacklightByDrv */
7611/* Input : */
7612/* Output : TRUE -> Skip backlight control */
7613/* Description : */
7614/* --------------------------------------------------------------------- */
7615BOOLEAN
7616XGI_BacklightByDrv(PVB_DEVICE_INFO pVBInfo)
7617{
7618    UCHAR tempah;
7619
7620    tempah = (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x3A);
7621    if (tempah & BacklightControlBit)
7622        return TRUE;
7623    else
7624        return FALSE;
7625}
7626
7627
7628/* --------------------------------------------------------------------- */
7629/* Function : XGI_FirePWDDisable */
7630/* Input : */
7631/* Output : */
7632/* Description : Turn off VDD & Backlight : Fire disable procedure */
7633/* --------------------------------------------------------------------- */
7634/*
7635void XGI_FirePWDDisable( PVB_DEVICE_INFO pVBInfo )
7636{
7637    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part1Port , 0x26 , 0x00 , 0xFC ) ;
7638}
7639*/
7640
7641/* --------------------------------------------------------------------- */
7642/* Function : XGI_FirePWDEnable */
7643/* Input : */
7644/* Output : */
7645/* Description : Turn on VDD & Backlight : Fire enable procedure */
7646/* --------------------------------------------------------------------- */
7647void
7648XGI_FirePWDEnable(PVB_DEVICE_INFO pVBInfo)
7649{
7650    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x26, 0x03, 0xFC);
7651}
7652
7653
7654/* --------------------------------------------------------------------- */
7655/* Function : XGI_EnableGatingCRT */
7656/* Input : */
7657/* Output : */
7658/* Description : */
7659/* --------------------------------------------------------------------- */
7660void
7661XGI_EnableGatingCRT(PXGI_HW_DEVICE_INFO HwDeviceExtension,
7662                    PVB_DEVICE_INFO pVBInfo)
7663{
7664    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x63, 0xBF, 0x40);
7665}
7666
7667
7668/* --------------------------------------------------------------------- */
7669/* Function : XGI_DisableGatingCRT */
7670/* Input : */
7671/* Output : */
7672/* Description : */
7673/* --------------------------------------------------------------------- */
7674void
7675XGI_DisableGatingCRT(PXGI_HW_DEVICE_INFO HwDeviceExtension,
7676                     PVB_DEVICE_INFO pVBInfo)
7677{
7678
7679    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3d4, 0x63, 0xBF, 0x00);
7680}
7681
7682
7683/* --------------------------------------------------------------------- */
7684/* Function : XGI_SetPanelDelay */
7685/* Input : */
7686/* Output : */
7687/* Description : */
7688/* I/P : bl : 1 ; T1 : the duration between CPL on and signal on */
7689/* : bl : 2 ; T2 : the duration signal on and Vdd on */
7690/* : bl : 3 ; T3 : the duration between CPL off and signal off */
7691/* : bl : 4 ; T4 : the duration signal off and Vdd off */
7692/* --------------------------------------------------------------------- */
7693void
7694XGI_SetPanelDelay(USHORT tempbl, PVB_DEVICE_INFO pVBInfo)
7695{
7696    USHORT index;
7697#ifndef LINUX_XF86
7698    USHORT temp;
7699#endif
7700
7701    index = XGI_GetLCDCapPtr(pVBInfo);
7702
7703    if (tempbl == 1)
7704        XGINew_LCD_Wait_Time(pVBInfo->LCDCapList[index].PSC_S1, pVBInfo);
7705
7706    if (tempbl == 2)
7707        XGINew_LCD_Wait_Time(pVBInfo->LCDCapList[index].PSC_S2, pVBInfo);
7708
7709    if (tempbl == 3)
7710        XGINew_LCD_Wait_Time(pVBInfo->LCDCapList[index].PSC_S3, pVBInfo);
7711
7712    if (tempbl == 4)
7713        XGINew_LCD_Wait_Time(pVBInfo->LCDCapList[index].PSC_S4, pVBInfo);
7714}
7715
7716
7717/* --------------------------------------------------------------------- */
7718/* Function : XGI_SetPanelPower */
7719/* Input : */
7720/* Output : */
7721/* Description : */
7722/* I/O : ah = 0011b = 03h ; Backlight on, Power on */
7723/* = 0111b = 07h ; Backlight on, Power off */
7724/* = 1011b = 0Bh ; Backlight off, Power on */
7725/* = 1111b = 0Fh ; Backlight off, Power off */
7726/* --------------------------------------------------------------------- */
7727void
7728XGI_SetPanelPower(USHORT tempah, USHORT tempbl, PVB_DEVICE_INFO pVBInfo)
7729{
7730    if (pVBInfo->VBType & (VB_XGI301LV | VB_XGI302LV | VB_XGI301C))
7731        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part4Port, 0x26, tempbl,
7732                        tempah);
7733    else
7734        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->P3c4, 0x11, tempbl, tempah);
7735}
7736
7737/* Jong 10/04/2007; merge code */
7738UCHAR XG21GPIODataTransfer(UCHAR ujDate)
7739{
7740    UCHAR  ujRet = 0;
7741    UCHAR  i = 0;
7742
7743    for (i=0; i<8; i++)
7744	{
7745    	ujRet = ujRet << 1;
7746	/* ujRet |= GETBITS(ujDate >> i, 0:0); */
7747        ujRet |= (ujDate >> i) & 1;
7748    }
7749
7750	return ujRet;
7751}
7752
7753/* Jong 10/04/2007; merge code */
7754/*----------------------------------------------------------------------------*/
7755/* output                                                                     */
7756/*      bl[5] : LVDS signal                                                   */
7757/*      bl[1] : LVDS backlight                                                */
7758/*      bl[0] : LVDS VDD                                                      */
7759/*----------------------------------------------------------------------------*/
7760UCHAR XGI_XG21GetPSCValue(PVB_DEVICE_INFO pVBInfo)
7761{
7762    UCHAR CR4A,temp;
7763
7764    CR4A = XGI_GetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x4A ) ;
7765    XGI_SetRegAND( (XGIIOADDRESS) pVBInfo->P3d4 , 0x4A , ~0x23 ) ; /* enable GPIO write */
7766
7767    temp = XGI_GetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x48 ) ;
7768
7769    temp = XG21GPIODataTransfer(temp);
7770    temp &= 0x23;
7771    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x4A , CR4A ) ;
7772    return temp;
7773}
7774
7775/* Jong 10/04/2007; merge code */
7776/*----------------------------------------------------------------------------*/
7777/* output                                                                     */
7778/*      bl[5] : LVDS signal                                                   */
7779/*      bl[1] : LVDS backlight                                                */
7780/*      bl[0] : LVDS VDD                                                      */
7781/*----------------------------------------------------------------------------*/
7782UCHAR XGI_XG27GetPSCValue(PVB_DEVICE_INFO pVBInfo)
7783{
7784    UCHAR CR4A,CRB4,temp;
7785
7786	PDEBUG(ErrorF("XGI_XG27GetPSCValue()...begin\n"));
7787
7788    CR4A = XGI_GetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x4A ) ;
7789	PDEBUG(ErrorF("XGI_XG27GetPSCValue()...1\n"));
7790    XGI_SetRegAND( (XGIIOADDRESS) pVBInfo->P3d4 , 0x4A , ~0x0C ) ; /* enable GPIO write */
7791	PDEBUG(ErrorF("XGI_XG27GetPSCValue()...2\n"));
7792
7793    temp = XGI_GetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x48 ) ;
7794	PDEBUG(ErrorF("XGI_XG27GetPSCValue()...3\n"));
7795
7796    temp &= 0x0C;
7797    temp >>= 2;
7798
7799	XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x4A , CR4A ) ;
7800	PDEBUG(ErrorF("XGI_XG27GetPSCValue()...4\n"));
7801
7802    CRB4 = XGI_GetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0xB4 ) ;
7803	PDEBUG(ErrorF("XGI_XG27GetPSCValue()...5\n"));
7804
7805    temp |= ((CRB4&0x04)<<3);
7806	PDEBUG(ErrorF("XGI_XG27GetPSCValue()...return\n"));
7807
7808    return temp;
7809}
7810
7811/* Jong 10/04/2007; merge code */
7812/*----------------------------------------------------------------------------*/
7813/* input                                                                      */
7814/*      bl[5] : 1;LVDS signal on                                              */
7815/*      bl[1] : 1;LVDS backlight on                                           */
7816/*      bl[0] : 1:LVDS VDD on                                                 */
7817/*      bh: 100000b : clear bit 5, to set bit5                                */
7818/*          000010b : clear bit 1, to set bit1                                */
7819/*          000001b : clear bit 0, to set bit0                                */
7820/*----------------------------------------------------------------------------*/
7821void XGI_XG21BLSignalVDD(USHORT tempbh,USHORT tempbl, PVB_DEVICE_INFO pVBInfo)
7822{
7823    UCHAR CR4A,temp;
7824
7825    CR4A = XGI_GetReg( (XGIIOADDRESS)pVBInfo->P3d4 , 0x4A ) ;
7826    tempbh &= 0x23;
7827    tempbl &= 0x23;
7828    XGI_SetRegAND( (XGIIOADDRESS)pVBInfo->P3d4 , 0x4A , ~tempbh ) ; /* enable GPIO write */
7829
7830    if (tempbh&0x20)
7831    {
7832      temp = (tempbl>>4)&0x02;
7833
7834      XGI_SetRegANDOR( (XGIIOADDRESS)pVBInfo->P3d4 , 0xB4 , ~0x02 , temp) ; /* CR B4[1] */
7835
7836    }
7837
7838    temp = XGI_GetReg( (XGIIOADDRESS)pVBInfo->P3d4 , 0x48 ) ;
7839
7840    temp = XG21GPIODataTransfer(temp);
7841
7842    temp &= ~tempbh;
7843    temp |= tempbl;
7844    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x48 , temp ) ;
7845}
7846
7847/* Jong 10/04/2007; merge code */
7848void XGI_XG27BLSignalVDD(USHORT tempbh,USHORT tempbl, PVB_DEVICE_INFO pVBInfo)
7849{
7850    UCHAR CR4A,temp;
7851    USHORT tempbh0,tempbl0;
7852
7853	PDEBUG(ErrorF("XGI_XG27BLSignalVDD()...begin\n"));
7854
7855    tempbh0 = tempbh;
7856    tempbl0 = tempbl;
7857    tempbh0 &= 0x20;
7858    tempbl0 &= 0x20;
7859    tempbh0 >>= 3;
7860    tempbl0 >>= 3;
7861
7862    if (tempbh&0x20)
7863    {
7864      temp = (tempbl>>4)&0x02;
7865
7866	  PDEBUG(ErrorF("XGI_XG27BLSignalVDD()...1\n"));
7867      XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3d4 , 0xB4 , ~0x02 , temp) ;		/* CR B4[1] */
7868
7869    }
7870
7871	PDEBUG(ErrorF("XGI_XG27BLSignalVDD()...2\n"));
7872    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3d4 , 0xB4 , ~tempbh0 , tempbl0 ) ;	/* CR B4[0] if tempbh0 = 0x01 */
7873
7874	/* Enable related GPIO R/W */
7875	PDEBUG(ErrorF("XGI_XG27BLSignalVDD()...3\n"));
7876    CR4A = XGI_GetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x4A ) ;
7877    tempbh &= 0x03;
7878    tempbl &= 0x03;
7879    tempbh <<= 2;
7880    tempbl <<= 2;																	/* GPIOC,GPIOD */
7881
7882	PDEBUG(ErrorF("XGI_XG27BLSignalVDD()...4\n"));
7883    XGI_SetRegAND( (XGIIOADDRESS) pVBInfo->P3d4 , 0x4A , ~tempbh ) ;				/* enable GPIO write */
7884	PDEBUG(ErrorF("XGI_XG27BLSignalVDD()...5\n"));
7885    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3d4 , 0x48 , ~tempbh , tempbl ) ;
7886	PDEBUG(ErrorF("XGI_XG27BLSignalVDD()...return\n"));
7887}
7888
7889/* Jong 10/04/2007; merge code */
7890/* --------------------------------------------------------------------- */
7891USHORT XGI_GetLVDSOEMTableIndex(PVB_DEVICE_INFO pVBInfo)
7892{
7893    USHORT index ;
7894
7895	PDEBUG(ErrorF("XGI_GetLVDSOEMTableIndex()...begin\n"));
7896    index = XGI_GetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x36 ) ;
7897	PDEBUG(ErrorF("XGI_GetLVDSOEMTableIndex()...1\n"));
7898    if (index<sizeof(XGI21_LCDCapList)/sizeof(XGI21_LVDSCapStruct))
7899    {
7900	  PDEBUG(ErrorF("XGI_GetLVDSOEMTableIndex()...2-return\n"));
7901      return index;
7902    }
7903
7904	PDEBUG(ErrorF("XGI_GetLVDSOEMTableIndex()...3-return\n"));
7905    return 0;
7906}
7907
7908/* Jong 10/04/2007; merge code */
7909/* --------------------------------------------------------------------- */
7910/* Function : XGI_XG21SetPanelDelay */
7911/* Input : */
7912/* Output : */
7913/* Description : */
7914/* I/P : bl : 1 ; T1 : the duration between CPL on and signal on */
7915/* : bl : 2 ; T2 : the duration signal on and Vdd on */
7916/* : bl : 3 ; T3 : the duration between CPL off and signal off */
7917/* : bl : 4 ; T4 : the duration signal off and Vdd off */
7918/* --------------------------------------------------------------------- */
7919void XGI_XG21SetPanelDelay(USHORT tempbl, PVB_DEVICE_INFO pVBInfo)
7920{
7921    USHORT index ;
7922
7923	PDEBUG(ErrorF("XGI_XG21SetPanelDelay()...begin\n"));
7924
7925    index = XGI_GetLVDSOEMTableIndex( pVBInfo );
7926	PDEBUG(ErrorF("After XGI_GetLVDSOEMTableIndex()\n"));
7927
7928    if ( tempbl == 1 )
7929	{
7930		PDEBUG(ErrorF("tempbl == 1\n"));
7931        XGINew_LCD_Wait_Time( pVBInfo->XG21_LVDSCapList[ index ].PSC_S1, pVBInfo ) ;
7932		PDEBUG(ErrorF("After XGINew_LCD_Wait_Time()\n"));
7933	}
7934
7935    if ( tempbl == 2 )
7936	{
7937		PDEBUG(ErrorF("tempbl == 2\n"));
7938        XGINew_LCD_Wait_Time( pVBInfo->XG21_LVDSCapList[ index ].PSC_S2, pVBInfo ) ;
7939		PDEBUG(ErrorF("After XGINew_LCD_Wait_Time()\n"));
7940	}
7941
7942    if ( tempbl == 3 )
7943	{
7944		PDEBUG(ErrorF("tempbl == 3\n"));
7945        XGINew_LCD_Wait_Time( pVBInfo->XG21_LVDSCapList[ index ].PSC_S3, pVBInfo ) ;
7946		PDEBUG(ErrorF("After XGINew_LCD_Wait_Time()\n"));
7947	}
7948
7949    if ( tempbl == 4 )
7950	{
7951		PDEBUG(ErrorF("tempbl == 4\n"));
7952        XGINew_LCD_Wait_Time( pVBInfo->XG21_LVDSCapList[ index ].PSC_S4, pVBInfo ) ;
7953		PDEBUG(ErrorF("After XGINew_LCD_Wait_Time()\n"));
7954	}
7955}
7956
7957/* Jong 10/04/2007; merge code */
7958BOOLEAN XGI_XG21CheckLVDSMode(USHORT ModeNo,USHORT ModeIdIndex, PVB_DEVICE_INFO pVBInfo )
7959{
7960    USHORT xres ,
7961           yres ,
7962           colordepth ,
7963           modeflag ,
7964           resindex ,
7965           lvdstableindex;
7966
7967    resindex = XGI_GetResInfo( ModeNo , ModeIdIndex, pVBInfo ) ;
7968    if ( ModeNo <= 0x13 )
7969    {
7970        xres = pVBInfo->StResInfo[ resindex ].HTotal ;
7971        yres = pVBInfo->StResInfo[ resindex ].VTotal ;
7972        modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;    /* si+St_ResInfo */
7973    }
7974    else
7975    {
7976        xres = pVBInfo->ModeResInfo[ resindex ].HTotal ;			  /* xres->ax */
7977        yres = pVBInfo->ModeResInfo[ resindex ].VTotal ;			  /* yres->bx */
7978        modeflag = pVBInfo->EModeIDTable[ ModeIdIndex].Ext_ModeFlag ; /* si+St_ModeFlag */
7979    }
7980
7981    if ( !( modeflag & Charx8Dot ) )
7982    {
7983        xres /= 9;
7984        xres *= 8;
7985    }
7986
7987    if ( ModeNo > 0x13 )
7988    {
7989        if ( ( ModeNo>0x13 ) && ( modeflag & HalfDCLK ) )
7990        {
7991          xres *=  2 ;
7992        }
7993        if ( ( ModeNo>0x13 ) && ( modeflag & DoubleScanMode ) )
7994        {
7995          yres *=  2 ;
7996        }
7997    }
7998
7999    lvdstableindex = XGI_GetLVDSOEMTableIndex( pVBInfo );
8000    if ( xres > (pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSHDE) )
8001      return FALSE;
8002
8003    if ( yres > (pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSVDE) )
8004      return FALSE;
8005
8006    if ( ModeNo > 0x13 )
8007    {
8008      if ( ( xres != (pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSHDE) ) ||
8009           ( yres != (pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSVDE)) )
8010      {
8011          colordepth = XGI_GetColorDepth( ModeNo , ModeIdIndex, pVBInfo ) ;
8012          if ( colordepth > 2 )
8013          {
8014            return FALSE;
8015          }
8016      }
8017    }
8018    return TRUE;
8019}
8020
8021/* Jong 10/04/2007; merge code */
8022void XGI_SetXG21FPBits(PVB_DEVICE_INFO pVBInfo)
8023{
8024    UCHAR temp;
8025
8026    temp = XGI_GetReg( (XGIIOADDRESS) pVBInfo->P3d4  , 0x37 ) ;  /* D[0] 1: 18bit */
8027    temp = ( temp & 1 ) << 6;
8028    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x06 , ~0x40 , temp ) ;      /* SR06[6] 18bit Dither */
8029    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x09 , ~0xc0 , temp | 0x80 ) ;  /* SR09[7] enable FP output, SR09[6] 1: sigle 18bits, 0: dual 12bits */
8030
8031}
8032
8033/* Jong 10/04/2007; merge code */
8034void XGI_SetXG27FPBits(PVB_DEVICE_INFO pVBInfo)
8035{
8036    UCHAR temp;
8037
8038    temp = XGI_GetReg( (XGIIOADDRESS) pVBInfo->P3d4  , 0x37 ) ;  /* D[1:0] 01: 18bit, 00: dual 12, 10: single 24 */
8039    temp = ( temp & 3 ) << 6;
8040    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x06 , ~0xc0 , temp & 0x80 ) ;  /* SR06[7]0: dual 12/1: single 24 [6] 18bit Dither <= 0 h/w recommend */
8041    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x09 , ~0xc0 , temp | 0x80 ) ;  /* SR09[7] enable FP output, SR09[6] 1: sigle 18bits, 0: 24bits */
8042
8043}
8044
8045/* Jong 10/04/2007; merge code */
8046void XGI_SetXG21LVDSPara(USHORT ModeNo,USHORT ModeIdIndex, PVB_DEVICE_INFO pVBInfo )
8047{
8048    UCHAR temp,Miscdata;
8049    USHORT xres ,
8050           yres ,
8051           colordepth ,
8052           modeflag ,
8053           resindex ,
8054           lvdstableindex ;
8055    USHORT LVDSHT,LVDSHBS,LVDSHRS,LVDSHRE,LVDSHBE;
8056    USHORT LVDSVT,LVDSVBS,LVDSVRS,LVDSVRE,LVDSVBE;
8057    USHORT value;
8058
8059    lvdstableindex = XGI_GetLVDSOEMTableIndex( pVBInfo );
8060
8061    temp = (UCHAR) ( ( pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDS_Capability & (LCDPolarity << 8 ) ) >> 8 );
8062    temp &= LCDPolarity;
8063    Miscdata =(UCHAR) XGI_GetRegByte(pVBInfo->P3cc) ;
8064
8065    XGI_SetRegByte( (XGIIOADDRESS) pVBInfo->P3c2 , (Miscdata & 0x3F) | temp ) ;
8066
8067    temp = (UCHAR) ( pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDS_Capability & LCDPolarity ) ;
8068    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x35 , ~0x80 , temp&0x80 ) ;      /* SR35[7] FP VSync polarity */
8069    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x30 , ~0x20 , (temp&0x40)>>1 ) ;   /* SR30[5] FP HSync polarity */
8070
8071    XGI_SetXG21FPBits(pVBInfo);
8072    resindex = XGI_GetResInfo( ModeNo , ModeIdIndex, pVBInfo ) ;
8073    if ( ModeNo <= 0x13 )
8074    {
8075        xres = pVBInfo->StResInfo[ resindex ].HTotal ;
8076        yres = pVBInfo->StResInfo[ resindex ].VTotal ;
8077        modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;    /* si+St_ResInfo */
8078    }
8079    else
8080    {
8081        xres = pVBInfo->ModeResInfo[ resindex ].HTotal ;			  /* xres->ax */
8082        yres = pVBInfo->ModeResInfo[ resindex ].VTotal ;			  /* yres->bx */
8083        modeflag = pVBInfo->EModeIDTable[ ModeIdIndex].Ext_ModeFlag ; /* si+St_ModeFlag */
8084    }
8085
8086    if (!( modeflag & Charx8Dot ))
8087      xres = xres * 8 / 9;
8088
8089    LVDSHT = pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSHT;
8090
8091    LVDSHBS = xres + ( pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSHDE - xres ) / 2 ;
8092    if ( ( ModeNo<=0x13 ) && ( modeflag & HalfDCLK ) )
8093    {
8094      LVDSHBS -=  xres/4 ;
8095    }
8096    if (LVDSHBS > LVDSHT) LVDSHBS -= LVDSHT ;
8097
8098    LVDSHRS = LVDSHBS + pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSHFP ;
8099    if (LVDSHRS > LVDSHT) LVDSHRS -= LVDSHT ;
8100
8101    LVDSHRE = LVDSHRS + pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSHSYNC ;
8102    if (LVDSHRE > LVDSHT) LVDSHRE -= LVDSHT ;
8103
8104    LVDSHBE = LVDSHBS + LVDSHT - pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSHDE ;
8105
8106    LVDSVT = pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSVT;
8107
8108    LVDSVBS = yres + ( pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSVDE - yres ) / 2 ;
8109    if ( ( ModeNo>0x13 ) && ( modeflag & DoubleScanMode ) )
8110    {
8111      LVDSVBS +=  yres/2 ;
8112    }
8113    if (LVDSVBS > LVDSVT) LVDSVBS -= LVDSVT ;
8114
8115    LVDSVRS = LVDSVBS + pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSVFP ;
8116    if (LVDSVRS > LVDSVT) LVDSVRS -= LVDSVT ;
8117
8118    LVDSVRE = LVDSVRS + pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSVSYNC ;
8119    if (LVDSVRE > LVDSVT) LVDSVRE -= LVDSVT ;
8120
8121    LVDSVBE = LVDSVBS + LVDSVT - pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSVDE ;
8122
8123    temp = ( UCHAR )XGI_GetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x11 ) ;
8124    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x11 , temp & 0x7f ) ;		/* Unlock CRTC */
8125
8126    if (!( modeflag & Charx8Dot ))
8127    {
8128        XGI_SetRegOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x1 , 0x1 ) ;
8129    }
8130
8131    /* HT SR0B[1:0] CR00 */
8132    value = ( LVDSHT >> 3 ) - 5;
8133    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x0B , ~0x03 , ( value & 0x300 ) >> 8 ) ;
8134    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x0 , (value & 0xFF) ) ;
8135
8136    /* HBS SR0B[5:4] CR02 */
8137    value = ( LVDSHBS >> 3 ) - 1;
8138    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x0B , ~0x30 , ( value & 0x300 ) >> 4 ) ;
8139    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x2 , (value & 0xFF) ) ;
8140
8141    /* HBE SR0C[1:0] CR05[7] CR03[4:0] */
8142    value = ( LVDSHBE >> 3 ) - 1;
8143    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x0C , ~0x03 , ( value & 0xC0 ) >> 6 ) ;
8144    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3d4 , 0x05 , ~0x80 , ( value & 0x20 ) << 2 ) ;
8145    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3d4 , 0x03 , ~0x1F , value & 0x1F ) ;
8146
8147    /* HRS SR0B[7:6] CR04 */
8148    value = ( LVDSHRS >> 3 ) + 2;
8149    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x0B , ~0xC0 , ( value & 0x300 ) >> 2 ) ;
8150    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x4 , (value & 0xFF) ) ;
8151
8152    /* Panel HRS SR2F[1:0] SR2E[7:0]  */
8153    value--;
8154    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x2F , ~0x03 , ( value & 0x300 ) >> 8 ) ;
8155    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x2E , (value & 0xFF) ) ;
8156
8157    /* HRE SR0C[2] CR05[4:0] */
8158    value = ( LVDSHRE >> 3 ) + 2;
8159    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x0C , ~0x04 , ( value & 0x20 ) >> 3 ) ;
8160    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3d4 , 0x05 , ~0x1F , value & 0x1F ) ;
8161
8162    /* Panel HRE SR2F[7:2]  */
8163    value--;
8164    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x2F , ~0xFC , value << 2 ) ;
8165
8166    /* VT SR0A[0] CR07[5][0] CR06 */
8167    value = LVDSVT - 2 ;
8168    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x0A , ~0x01 , ( value & 0x400 ) >> 10 ) ;
8169    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3d4 , 0x07 , ~0x20 , ( value & 0x200 ) >> 4 ) ;
8170    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3d4 , 0x07 , ~0x01 , ( value & 0x100 ) >> 8 ) ;
8171    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x06 , (value & 0xFF) ) ;
8172
8173    /* VBS SR0A[2] CR09[5] CR07[3] CR15 */
8174    value = LVDSVBS - 1 ;
8175    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x0A , ~0x04 , ( value & 0x400 ) >> 8 ) ;
8176    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3d4 , 0x09 , ~0x20 , ( value & 0x200 ) >> 4 ) ;
8177    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3d4 , 0x07 , ~0x08 , ( value & 0x100 ) >> 5 ) ;
8178    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x15 , (value & 0xFF) ) ;
8179
8180    /* VBE SR0A[4] CR16 */
8181    value = LVDSVBE - 1;
8182    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x0A , ~0x10 , ( value & 0x100 ) >> 4 ) ;
8183    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x16 , (value & 0xFF) ) ;
8184
8185    /* VRS SR0A[3] CR7[7][2] CR10 */
8186    value = LVDSVRS - 1 ;
8187    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x0A , ~0x08 , ( value & 0x400 ) >> 7 ) ;
8188    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3d4 , 0x07 , ~0x80 , ( value & 0x200 ) >> 2 ) ;
8189    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3d4 , 0x07 , ~0x04 , ( value & 0x100 ) >> 6 ) ;
8190    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x10 , (value & 0xFF) ) ;
8191
8192    /* Panel VRS SR3F[1:0] SR34[7:0] SR33[0] */
8193    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x3F , ~0x03 , ( value & 0x600 ) >> 9 ) ;
8194    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x34 , (value >> 1) & 0xFF ) ;
8195    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3d4 , 0x33 , ~0x01 , value & 0x01 ) ;
8196
8197    /* VRE SR0A[5] CR11[3:0] */
8198    value = LVDSVRE - 1;
8199    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x0A , ~0x20 , ( value & 0x10 ) << 1 ) ;
8200    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3d4 , 0x11 , ~0x0F , value & 0x0F ) ;
8201
8202    /* Panel VRE SR3F[7:2] */ /* SR3F[7] has to be 0, h/w bug */
8203    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x3F , ~0xFC , ( value << 2 ) & 0x7C ) ;
8204
8205    for ( temp=0, value = 0; temp < 3; temp++)
8206    {
8207
8208        XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x31 , ~0x30 , value ) ;
8209        XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x2B , pVBInfo->XG21_LVDSCapList[lvdstableindex].VCLKData1) ;
8210        XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x2C , pVBInfo->XG21_LVDSCapList[lvdstableindex].VCLKData2) ;
8211        value += 0x10;
8212    }
8213
8214    if (!( modeflag & Charx8Dot ))
8215    {
8216        XGI_GetRegByte( (XGIIOADDRESS) pVBInfo->P3da ) ;           /* reset 3da */
8217        XGI_SetRegByte( (XGIIOADDRESS) pVBInfo->P3c0 , 0x13 ) ;    /* set index */
8218        XGI_SetRegByte( (XGIIOADDRESS) pVBInfo->P3c0 , 0x00 ) ;    /* set data, panning = 0, shift left 1 dot*/
8219
8220        XGI_GetRegByte( (XGIIOADDRESS) pVBInfo->P3da ) ;           /* Enable Attribute */
8221        XGI_SetRegByte( (XGIIOADDRESS) pVBInfo->P3c0 , 0x20 ) ;
8222
8223        XGI_GetRegByte( (XGIIOADDRESS) pVBInfo->P3da ) ;           /* reset 3da */
8224    }
8225
8226
8227}
8228
8229/* Jong 10/04/2007; merge code */
8230/* no shadow case */
8231void XGI_SetXG27LVDSPara(USHORT ModeNo,USHORT ModeIdIndex, PVB_DEVICE_INFO pVBInfo )
8232{
8233    UCHAR temp,Miscdata;
8234    USHORT xres ,
8235           yres ,
8236           colordepth ,
8237           modeflag ,
8238           resindex ,
8239           lvdstableindex ;
8240    USHORT LVDSHT,LVDSHBS,LVDSHRS,LVDSHRE,LVDSHBE;
8241    USHORT LVDSVT,LVDSVBS,LVDSVRS,LVDSVRE,LVDSVBE;
8242    USHORT value;
8243
8244    lvdstableindex = XGI_GetLVDSOEMTableIndex( pVBInfo );
8245    temp = (UCHAR) ( ( pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDS_Capability & (LCDPolarity << 8 ) ) >> 8 );
8246    temp &= LCDPolarity;
8247    Miscdata =(UCHAR) XGI_GetRegByte((XGIIOADDRESS) pVBInfo->P3cc) ;
8248
8249    XGI_SetRegByte( (XGIIOADDRESS) pVBInfo->P3c2 , (Miscdata & 0x3F) | temp ) ;
8250
8251    temp = (UCHAR) ( pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDS_Capability & LCDPolarity ) ;
8252    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x35 , ~0x80 , temp&0x80 ) ;      /* SR35[7] FP VSync polarity */
8253    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x30 , ~0x20 , (temp&0x40)>>1 ) ;   /* SR30[5] FP HSync polarity */
8254
8255    XGI_SetXG27FPBits(pVBInfo);
8256    resindex = XGI_GetResInfo( ModeNo , ModeIdIndex, pVBInfo ) ;
8257    if ( ModeNo <= 0x13 )
8258    {
8259        xres = pVBInfo->StResInfo[ resindex ].HTotal ;
8260        yres = pVBInfo->StResInfo[ resindex ].VTotal ;
8261        modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;    /* si+St_ResInfo */
8262    }
8263    else
8264    {
8265        xres = pVBInfo->ModeResInfo[ resindex ].HTotal ;			  /* xres->ax */
8266        yres = pVBInfo->ModeResInfo[ resindex ].VTotal ;			  /* yres->bx */
8267        modeflag = pVBInfo->EModeIDTable[ ModeIdIndex].Ext_ModeFlag ; /* si+St_ModeFlag */
8268    }
8269
8270    if (!( modeflag & Charx8Dot ))
8271      xres = xres * 8 / 9;
8272
8273    LVDSHT = pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSHT;
8274
8275    LVDSHBS = xres + ( pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSHDE - xres ) / 2 ;
8276    if ( ( ModeNo<=0x13 ) && ( modeflag & HalfDCLK ) )
8277    {
8278      LVDSHBS -=  xres/4 ;
8279    }
8280    if (LVDSHBS > LVDSHT) LVDSHBS -= LVDSHT ;
8281
8282    LVDSHRS = LVDSHBS + pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSHFP ;
8283    if (LVDSHRS > LVDSHT) LVDSHRS -= LVDSHT ;
8284
8285    LVDSHRE = LVDSHRS + pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSHSYNC ;
8286    if (LVDSHRE > LVDSHT) LVDSHRE -= LVDSHT ;
8287
8288    LVDSHBE = LVDSHBS + LVDSHT - pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSHDE ;
8289
8290    LVDSVT = pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSVT;
8291
8292    LVDSVBS = yres + ( pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSVDE - yres ) / 2 ;
8293    if ( ( ModeNo>0x13 ) && ( modeflag & DoubleScanMode ) )
8294    {
8295      LVDSVBS +=  yres/2 ;
8296    }
8297    if (LVDSVBS > LVDSVT) LVDSVBS -= LVDSVT ;
8298
8299    LVDSVRS = LVDSVBS + pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSVFP ;
8300    if (LVDSVRS > LVDSVT) LVDSVRS -= LVDSVT ;
8301
8302    LVDSVRE = LVDSVRS + pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSVSYNC ;
8303    if (LVDSVRE > LVDSVT) LVDSVRE -= LVDSVT ;
8304
8305    LVDSVBE = LVDSVBS + LVDSVT - pVBInfo->XG21_LVDSCapList[lvdstableindex].LVDSVDE ;
8306
8307    temp = ( UCHAR )XGI_GetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x11 ) ;
8308    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x11 , temp & 0x7f ) ;		/* Unlock CRTC */
8309
8310    if (!( modeflag & Charx8Dot ))
8311    {
8312        XGI_SetRegOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x1 , 0x1 ) ;
8313    }
8314
8315    /* HT SR0B[1:0] CR00 */
8316    value = ( LVDSHT >> 3 ) - 5;
8317    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x0B , ~0x03 , ( value & 0x300 ) >> 8 ) ;
8318    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x0 , (value & 0xFF) ) ;
8319
8320    /* HBS SR0B[5:4] CR02 */
8321    value = ( LVDSHBS >> 3 ) - 1;
8322    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x0B , ~0x30 , ( value & 0x300 ) >> 4 ) ;
8323    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x2 , (value & 0xFF) ) ;
8324
8325    /* HBE SR0C[1:0] CR05[7] CR03[4:0] */
8326    value = ( LVDSHBE >> 3 ) - 1;
8327    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x0C , ~0x03 , ( value & 0xC0 ) >> 6 ) ;
8328    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3d4 , 0x05 , ~0x80 , ( value & 0x20 ) << 2 ) ;
8329    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3d4 , 0x03 , ~0x1F , value & 0x1F ) ;
8330
8331    /* HRS SR0B[7:6] CR04 */
8332    value = ( LVDSHRS >> 3 ) + 2;
8333    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x0B , ~0xC0 , ( value & 0x300 ) >> 2 ) ;
8334    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x4 , (value & 0xFF) ) ;
8335
8336    /* Panel HRS SR2F[1:0] SR2E[7:0]  */
8337    value--;
8338    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x2F , ~0x03 , ( value & 0x300 ) >> 8 ) ;
8339    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x2E , (value & 0xFF) ) ;
8340
8341    /* HRE SR0C[2] CR05[4:0] */
8342    value = ( LVDSHRE >> 3 ) + 2;
8343    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x0C , ~0x04 , ( value & 0x20 ) >> 3 ) ;
8344    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3d4 , 0x05 , ~0x1F , value & 0x1F ) ;
8345
8346    /* Panel HRE SR2F[7:2]  */
8347    value--;
8348    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x2F , ~0xFC , value << 2 ) ;
8349
8350    /* VT SR0A[0] CR07[5][0] CR06 */
8351    value = LVDSVT - 2 ;
8352    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x0A , ~0x01 , ( value & 0x400 ) >> 10 ) ;
8353    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3d4 , 0x07 , ~0x20 , ( value & 0x200 ) >> 4 ) ;
8354    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3d4 , 0x07 , ~0x01 , ( value & 0x100 ) >> 8 ) ;
8355    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x06 , (value & 0xFF) ) ;
8356
8357    /* VBS SR0A[2] CR09[5] CR07[3] CR15 */
8358    value = LVDSVBS - 1 ;
8359    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x0A , ~0x04 , ( value & 0x400 ) >> 8 ) ;
8360    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3d4 , 0x09 , ~0x20 , ( value & 0x200 ) >> 4 ) ;
8361    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3d4 , 0x07 , ~0x08 , ( value & 0x100 ) >> 5 ) ;
8362    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x15 , (value & 0xFF) ) ;
8363
8364    /* VBE SR0A[4] CR16 */
8365    value = LVDSVBE - 1;
8366    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x0A , ~0x10 , ( value & 0x100 ) >> 4 ) ;
8367    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x16 , (value & 0xFF) ) ;
8368
8369    /* VRS SR0A[3] CR7[7][2] CR10 */
8370    value = LVDSVRS - 1 ;
8371    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x0A , ~0x08 , ( value & 0x400 ) >> 7 ) ;
8372    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3d4 , 0x07 , ~0x80 , ( value & 0x200 ) >> 2 ) ;
8373    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3d4 , 0x07 , ~0x04 , ( value & 0x100 ) >> 6 ) ;
8374    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3d4 , 0x10 , (value & 0xFF) ) ;
8375
8376    /* Panel VRS SR35[2:0] SR34[7:0] */
8377    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x35 , ~0x07 , ( value & 0x700 ) >> 8 ) ;
8378    XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x34 , value & 0xFF ) ;
8379
8380    /* VRE SR0A[5] CR11[3:0] */
8381    value = LVDSVRE - 1;
8382    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x0A , ~0x20 , ( value & 0x10 ) << 1 ) ;
8383    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3d4 , 0x11 , ~0x0F , value & 0x0F ) ;
8384
8385    /* Panel VRE SR3F[7:2] */
8386    XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x3F , ~0xFC , ( value << 2 ) & 0xFC ) ;
8387
8388    for ( temp=0, value = 0; temp < 3; temp++)
8389    {
8390
8391        XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->P3c4 , 0x31 , ~0x30 , value ) ;
8392        XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x2B , pVBInfo->XG21_LVDSCapList[lvdstableindex].VCLKData1) ;
8393        XGI_SetReg( (XGIIOADDRESS) pVBInfo->P3c4 , 0x2C , pVBInfo->XG21_LVDSCapList[lvdstableindex].VCLKData2) ;
8394        value += 0x10;
8395    }
8396
8397    if (!( modeflag & Charx8Dot ))
8398    {
8399        XGI_GetRegByte( (XGIIOADDRESS) pVBInfo->P3da ) ;           /* reset 3da */
8400        XGI_SetRegByte( (XGIIOADDRESS) pVBInfo->P3c0 , 0x13 ) ;    /* set index */
8401        XGI_SetRegByte( (XGIIOADDRESS) pVBInfo->P3c0 , 0x00 ) ;    /* set data, panning = 0, shift left 1 dot*/
8402
8403        XGI_GetRegByte( (XGIIOADDRESS) pVBInfo->P3da ) ;           /* Enable Attribute */
8404        XGI_SetRegByte( (XGIIOADDRESS) pVBInfo->P3c0 , 0x20 ) ;
8405
8406        XGI_GetRegByte( (XGIIOADDRESS) pVBInfo->P3da ) ;           /* reset 3da */
8407    }
8408}
8409
8410
8411/* --------------------------------------------------------------------- */
8412/* Function : XGI_IsLCDON */
8413/* Input : */
8414/* Output : FALSE : Skip PSC Control */
8415/* TRUE: Disable PSC */
8416/* Description : */
8417/* --------------------------------------------------------------------- */
8418BOOLEAN
8419XGI_IsLCDON(PVB_DEVICE_INFO pVBInfo)
8420{
8421    USHORT tempax;
8422
8423    tempax = pVBInfo->VBInfo;
8424    if (tempax & SetCRT2ToDualEdge)
8425        return FALSE;
8426    else if (tempax & (DisableCRT2Display | SwitchToCRT2 | SetSimuScanMode))
8427        return TRUE;
8428
8429    return FALSE;
8430}
8431
8432
8433/* --------------------------------------------------------------------- */
8434/* Function : XGI_EnablePWD */
8435/* Input : */
8436/* Output : */
8437/* Description : */
8438/* --------------------------------------------------------------------- */
8439void
8440XGI_EnablePWD(PVB_DEVICE_INFO pVBInfo)
8441{
8442    USHORT index, temp;
8443
8444    index = XGI_GetLCDCapPtr(pVBInfo);
8445    temp = pVBInfo->LCDCapList[index].PWD_2B;
8446    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x2B, temp);
8447    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x2C,
8448               pVBInfo->LCDCapList[index].PWD_2C);
8449    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x2D,
8450               pVBInfo->LCDCapList[index].PWD_2D);
8451    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x2E,
8452               pVBInfo->LCDCapList[index].PWD_2E);
8453    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x2F,
8454               pVBInfo->LCDCapList[index].PWD_2F);
8455    XGI_SetRegOR((XGIIOADDRESS) pVBInfo->Part4Port, 0x27, 0x80);        /* enable PWD */
8456}
8457
8458
8459/* --------------------------------------------------------------------- */
8460/* Function : XGI_DisablePWD */
8461/* Input : */
8462/* Output : */
8463/* Description : */
8464/* --------------------------------------------------------------------- */
8465void
8466XGI_DisablePWD(PVB_DEVICE_INFO pVBInfo)
8467{
8468    XGI_SetRegAND((XGIIOADDRESS) pVBInfo->Part4Port, 0x27, 0x7F);       /* disable PWD */
8469}
8470
8471
8472/* --------------------------------------------------------------------- */
8473/* Function : XGI_DisableChISLCD */
8474/* Input : */
8475/* Output : FALSE -> Not LCD Mode */
8476/* Description : */
8477/* --------------------------------------------------------------------- */
8478BOOLEAN
8479XGI_DisableChISLCD(PVB_DEVICE_INFO pVBInfo)
8480{
8481    USHORT tempbx, tempah;
8482
8483    tempbx = pVBInfo->SetFlag & (DisableChA | DisableChB);
8484    tempah = ~((USHORT) XGI_GetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x2E));
8485
8486    if (tempbx & (EnableChA | DisableChA)) {
8487        if (!(tempah & 0x08))   /* Chk LCDA Mode */
8488            return FALSE;
8489    }
8490
8491    if (!(tempbx & (EnableChB | DisableChB)))
8492        return FALSE;
8493
8494    if (tempah & 0x01)          /* Chk LCDB Mode */
8495        return TRUE;
8496
8497    return FALSE;
8498}
8499
8500
8501/* --------------------------------------------------------------------- */
8502/* Function : XGI_EnableChISLCD */
8503/* Input : */
8504/* Output : 0 -> Not LCD mode */
8505/* Description : */
8506/* --------------------------------------------------------------------- */
8507BOOLEAN
8508XGI_EnableChISLCD(PVB_DEVICE_INFO pVBInfo)
8509{
8510    USHORT tempbx, tempah;
8511
8512
8513    tempbx = pVBInfo->SetFlag & (EnableChA | EnableChB);
8514    tempah = ~((USHORT) XGI_GetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x2E));
8515
8516    if (tempbx & (EnableChA | DisableChA)) {
8517        if (!(tempah & 0x08))   /* Chk LCDA Mode */
8518            return FALSE;
8519    }
8520
8521    if (!(tempbx & (EnableChB | DisableChB)))
8522        return FALSE;
8523
8524    if (tempah & 0x01)          /* Chk LCDB Mode */
8525        return TRUE;
8526
8527    return FALSE;
8528}
8529
8530
8531/* --------------------------------------------------------------------- */
8532/* Function : XGI_GetLCDCapPtr */
8533/* Input : */
8534/* Output : */
8535/* Description : */
8536/* --------------------------------------------------------------------- */
8537USHORT
8538XGI_GetLCDCapPtr(PVB_DEVICE_INFO pVBInfo)
8539{
8540    UCHAR tempal, tempah, tempbl, i;
8541
8542    tempah = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x36);
8543    tempal = tempah & 0x0F;
8544    tempah = tempah & 0xF0;
8545    i = 0;
8546    tempbl = pVBInfo->LCDCapList[i].LCD_ID;
8547
8548    while (tempbl != 0xFF) {
8549        if (tempbl & 0x80) {    /* OEMUtil */
8550            tempal = tempah;
8551            tempbl = tempbl & ~(0x80);
8552        }
8553
8554        if (tempal == tempbl)
8555            break;
8556
8557        i++;
8558
8559        tempbl = pVBInfo->LCDCapList[i].LCD_ID;
8560    }
8561
8562    return i;
8563}
8564
8565
8566/* --------------------------------------------------------------------- */
8567/* Function : XGI_GetLCDCapPtr1 */
8568/* Input : */
8569/* Output : */
8570/* Description : */
8571/* --------------------------------------------------------------------- */
8572USHORT
8573XGI_GetLCDCapPtr1(PVB_DEVICE_INFO pVBInfo)
8574{
8575    USHORT tempah, tempal, tempbl, i;
8576
8577    tempal = pVBInfo->LCDResInfo;
8578    tempah = pVBInfo->LCDTypeInfo;
8579
8580    i = 0;
8581    tempbl = pVBInfo->LCDCapList[i].LCD_ID;
8582
8583    while (tempbl != 0xFF) {
8584        if ((tempbl & 0x80) && (tempbl != 0x80)) {
8585            tempal = tempah;
8586            tempbl &= ~0x80;
8587        }
8588
8589        if (tempal == tempbl)
8590            break;
8591
8592        i++;
8593        tempbl = pVBInfo->LCDCapList[i].LCD_ID;
8594    }
8595
8596    if (tempbl == 0xFF) {
8597        pVBInfo->LCDResInfo = Panel1024x768;
8598        pVBInfo->LCDTypeInfo = 0;
8599        i = 0;
8600    }
8601
8602    return i;
8603}
8604
8605
8606/* --------------------------------------------------------------------- */
8607/* Function : XGI_GetLCDSync */
8608/* Input : */
8609/* Output : */
8610/* Description : */
8611/* --------------------------------------------------------------------- */
8612void
8613XGI_GetLCDSync(ULONG * HSyncWidth, ULONG * VSyncWidth,
8614               PVB_DEVICE_INFO pVBInfo)
8615{
8616    USHORT Index;
8617
8618    Index = XGI_GetLCDCapPtr(pVBInfo);
8619    *HSyncWidth = pVBInfo->LCDCapList[Index].LCD_HSyncWidth;
8620    *VSyncWidth = pVBInfo->LCDCapList[Index].LCD_VSyncWidth;
8621
8622    return;
8623}
8624
8625
8626
8627/* --------------------------------------------------------------------- */
8628/* Function : XGI_EnableBridge */
8629/* Input : */
8630/* Output : */
8631/* Description : */
8632/* --------------------------------------------------------------------- */
8633void
8634XGI_EnableBridge(PXGI_HW_DEVICE_INFO HwDeviceExtension,
8635                 PVB_DEVICE_INFO pVBInfo)
8636{
8637#ifndef LINUX_XF86
8638    USHORT tempax;
8639#endif
8640    USHORT tempbl, tempah;
8641
8642    if (pVBInfo->SetFlag == Win9xDOSMode) {
8643        if (pVBInfo->
8644            VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV |
8645                      VB_XGI301C)) {
8646            XGI_DisplayOn(HwDeviceExtension, pVBInfo);
8647            return;
8648        }
8649        else                    /* LVDS or CH7017 */
8650            return;
8651    }
8652
8653
8654    if (HwDeviceExtension->jChipType < XG40) {
8655        if (!XGI_DisableChISLCD(pVBInfo)) {
8656            if ((XGI_EnableChISLCD(pVBInfo))
8657                || (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA))) {
8658                if (pVBInfo->LCDInfo & SetPWDEnable) {
8659                    XGI_EnablePWD(pVBInfo);
8660                }
8661                else {
8662                    pVBInfo->LCDInfo &= (~SetPWDEnable);
8663                    if (pVBInfo->
8664                        VBType & (VB_XGI301LV | VB_XGI302LV | VB_XGI301C)) {
8665                        tempbl = 0xFD;
8666                        tempah = 0x02;
8667                    }
8668                    else {
8669                        tempbl = 0xFB;
8670                        tempah = 0x00;
8671                    }
8672
8673                    XGI_SetPanelPower(tempah, tempbl, pVBInfo);
8674                    XGI_SetPanelDelay(1, pVBInfo);
8675                }
8676            }
8677        }
8678    }                           /* Not 340 */
8679
8680
8681
8682    if (pVBInfo->
8683        VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV |
8684                  VB_XGI301C)) {
8685        if (!(pVBInfo->SetFlag & DisableChA)) {
8686            if (pVBInfo->SetFlag & EnableChA) {
8687                XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x1E, 0x20);      /* Power on */
8688            }
8689            else {
8690                if (pVBInfo->VBInfo & SetCRT2ToDualEdge) {      /* SetCRT2ToLCDA ) */
8691                    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x1E, 0x20);  /* Power on */
8692                }
8693            }
8694        }
8695
8696        if (!(pVBInfo->SetFlag & DisableChB)) {
8697            if ((pVBInfo->SetFlag & EnableChB)
8698                || (pVBInfo->
8699                    VBInfo & (SetCRT2ToLCD | SetCRT2ToTV | SetCRT2ToRAMDAC)))
8700            {
8701                tempah =
8702                    (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x32);
8703                tempah &= 0xDF;
8704                if (pVBInfo->VBInfo & SetInSlaveMode) {
8705                    if (!(pVBInfo->VBInfo & SetCRT2ToRAMDAC))
8706                        tempah |= 0x20;
8707                }
8708                XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x32, tempah);
8709                XGI_SetRegOR((XGIIOADDRESS) pVBInfo->P3c4, 0x1E,
8710			     SR1E_ENABLE_CRT2);
8711
8712
8713                tempah =
8714                    (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->Part1Port,
8715                                       0x2E);
8716
8717                if (!(tempah & 0x80))
8718                    XGI_SetRegOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x2E, 0x80);        /* BVBDOENABLE = 1 */
8719
8720                XGI_SetRegAND((XGIIOADDRESS) pVBInfo->Part1Port, 0x00, 0x7F);   /* BScreenOFF = 0 */
8721            }
8722        }
8723
8724        if ((pVBInfo->SetFlag & (EnableChA | EnableChB))
8725            || (!(pVBInfo->VBInfo & DisableCRT2Display))) {
8726            XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part2Port, 0x00, ~0xE0, 0x20);      /* shampoo 0129 */
8727            if (pVBInfo->VBType & (VB_XGI302LV | VB_XGI301C)) {
8728                if (!XGI_DisableChISLCD(pVBInfo)) {
8729                    if (XGI_EnableChISLCD(pVBInfo)
8730                        || (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)))
8731                        XGI_SetRegAND((XGIIOADDRESS) pVBInfo->Part4Port, 0x2A, 0x7F);   /* LVDS PLL power on */
8732                }
8733                XGI_SetRegAND((XGIIOADDRESS) pVBInfo->Part4Port, 0x30, 0x7F);   /* LVDS Driver power on */
8734            }
8735        }
8736
8737        tempah = 0x00;
8738
8739        if (!(pVBInfo->VBInfo & DisableCRT2Display)) {
8740            tempah = 0xc0;
8741
8742            if (!(pVBInfo->VBInfo & SetSimuScanMode)) {
8743                if (pVBInfo->VBInfo & SetCRT2ToLCDA) {
8744                    if (pVBInfo->VBInfo & SetCRT2ToDualEdge) {
8745                        tempah = tempah & 0x40;
8746                        if (pVBInfo->VBInfo & SetCRT2ToLCDA)
8747                            tempah = tempah ^ 0xC0;
8748
8749                        if (pVBInfo->SetFlag & DisableChB)
8750                            tempah &= 0xBF;
8751
8752                        if (pVBInfo->SetFlag & DisableChA)
8753                            tempah &= 0x7F;
8754
8755                        if (pVBInfo->SetFlag & EnableChB)
8756                            tempah |= 0x40;
8757
8758                        if (pVBInfo->SetFlag & EnableChA)
8759                            tempah |= 0x80;
8760                    }
8761                }
8762            }
8763        }
8764
8765        XGI_SetRegOR((XGIIOADDRESS) pVBInfo->Part4Port, 0x1F, tempah);  /* EnablePart4_1F */
8766
8767        if (pVBInfo->SetFlag & Win9xDOSMode) {
8768            XGI_DisplayOn(HwDeviceExtension, pVBInfo);
8769            return;
8770        }
8771
8772        if (!(pVBInfo->SetFlag & DisableChA)) {
8773            XGI_VBLongWait(pVBInfo);
8774            if (!(pVBInfo->SetFlag & GatingCRT)) {
8775                XGI_DisableGatingCRT(HwDeviceExtension, pVBInfo);
8776                XGI_DisplayOn(HwDeviceExtension, pVBInfo);
8777                XGI_VBLongWait(pVBInfo);
8778            }
8779        }
8780    }                           /* 301 */
8781    else {                      /* LVDS */
8782
8783        if (pVBInfo->VBInfo & (SetCRT2ToTV | SetCRT2ToLCD | SetCRT2ToLCDA))
8784            XGI_SetRegOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x1E, 0x20);        /* enable CRT2 */
8785
8786
8787
8788        tempah = (UCHAR) XGI_GetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x2E);
8789        if (!(tempah & 0x80))
8790            XGI_SetRegOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x2E, 0x80);        /* BVBDOENABLE = 1 */
8791
8792        XGI_SetRegAND((XGIIOADDRESS) pVBInfo->Part1Port, 0x00, 0x7F);
8793        XGI_DisplayOn(HwDeviceExtension, pVBInfo);
8794    }                           /* End of VB */
8795
8796
8797    if (HwDeviceExtension->jChipType < XG40) {
8798        if (!XGI_EnableChISLCD(pVBInfo)) {
8799            if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
8800                if (XGI_BacklightByDrv(pVBInfo))
8801                    return;
8802            }
8803            else
8804                return;
8805        }
8806
8807        if (pVBInfo->LCDInfo & SetPWDEnable) {
8808            XGI_FirePWDEnable(pVBInfo);
8809            return;
8810        }
8811
8812        XGI_SetPanelDelay(2, pVBInfo);
8813
8814        if (pVBInfo->VBType & (VB_XGI301LV | VB_XGI302LV | VB_XGI301C)) {
8815            tempah = 0x01;
8816            tempbl = 0xFE;      /* turn on backlght */
8817        }
8818        else {
8819            tempbl = 0xF7;
8820            tempah = 0x00;
8821        }
8822        XGI_SetPanelPower(tempah, tempbl, pVBInfo);
8823    }
8824}
8825
8826
8827/* --------------------------------------------------------------------- */
8828/* Function : XGI_DisableBridge */
8829/* Input : */
8830/* Output : */
8831/* Description : */
8832/* --------------------------------------------------------------------- */
8833void
8834XGI_DisableBridge(PXGI_HW_DEVICE_INFO HwDeviceExtension,
8835                  PVB_DEVICE_INFO pVBInfo)
8836{
8837    USHORT tempax, tempbx, tempah = 0, tempbl = 0;
8838
8839    if (pVBInfo->SetFlag == Win9xDOSMode)
8840        return;
8841
8842
8843    if (HwDeviceExtension->jChipType < XG40) {
8844        if ((!(pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)))
8845            || (XGI_DisableChISLCD(pVBInfo))) {
8846            if (!XGI_IsLCDON(pVBInfo)) {
8847                if (pVBInfo->LCDInfo & SetPWDEnable)
8848                    XGI_EnablePWD(pVBInfo);
8849                else {
8850                    pVBInfo->LCDInfo &= ~SetPWDEnable;
8851                    XGI_DisablePWD(pVBInfo);
8852                    if (pVBInfo->
8853                        VBType & (VB_XGI301LV | VB_XGI302LV | VB_XGI301C)) {
8854                        tempbx = 0xFE;  /* not 01h */
8855                        tempax = 0;
8856                    }
8857                    else {
8858                        tempbx = 0xF7;  /* not 08h */
8859                        tempax = 0x08;
8860                    }
8861                    XGI_SetPanelPower(tempax, tempbx, pVBInfo);
8862                    XGI_SetPanelDelay(3, pVBInfo);
8863                }
8864            }                   /* end if(!XGI_IsLCDON(pVBInfo)) */
8865        }
8866    }
8867
8868
8869    if (pVBInfo->
8870        VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV |
8871                  VB_XGI301C)) {
8872        tempah = 0x3F;
8873        if (!(pVBInfo->VBInfo & (DisableCRT2Display | SetSimuScanMode))) {
8874            if (pVBInfo->VBInfo & SetCRT2ToLCDA) {
8875                if (pVBInfo->VBInfo & SetCRT2ToDualEdge) {
8876                    tempah = 0x7F;      /* Disable Channel A */
8877                    if (!(pVBInfo->VBInfo & SetCRT2ToLCDA))
8878                        tempah = 0xBF;  /* Disable Channel B */
8879
8880                    if (pVBInfo->SetFlag & DisableChB)
8881                        tempah &= 0xBF; /* force to disable Cahnnel */
8882
8883                    if (pVBInfo->SetFlag & DisableChA)
8884                        tempah &= 0x7F; /* Force to disable Channel B */
8885                }
8886            }
8887        }
8888
8889        XGI_SetRegAND((XGIIOADDRESS) pVBInfo->Part4Port, 0x1F, tempah); /* disable part4_1f */
8890
8891        if (pVBInfo->VBType & (VB_XGI302LV | VB_XGI301C)) {
8892            if (((pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)))
8893                || (XGI_DisableChISLCD(pVBInfo)) || (XGI_IsLCDON(pVBInfo)))
8894                XGI_SetRegOR((XGIIOADDRESS) pVBInfo->Part4Port, 0x30, 0x80);    /* LVDS Driver power down */
8895        }
8896
8897        if ((pVBInfo->SetFlag & DisableChA)
8898            || (pVBInfo->
8899                VBInfo & (DisableCRT2Display | SetCRT2ToLCDA |
8900                          SetSimuScanMode))) {
8901            if (pVBInfo->SetFlag & GatingCRT)
8902                XGI_EnableGatingCRT(HwDeviceExtension, pVBInfo);
8903            XGI_DisplayOff(HwDeviceExtension, pVBInfo);
8904        }
8905
8906        if (pVBInfo->VBInfo & SetCRT2ToLCDA) {
8907            if ((pVBInfo->SetFlag & DisableChA)
8908                || (pVBInfo->VBInfo & SetCRT2ToLCDA))
8909                XGI_SetRegAND((XGIIOADDRESS) pVBInfo->Part1Port, 0x1e, 0xdf);   /* Power down */
8910        }
8911
8912        XGI_SetRegAND((XGIIOADDRESS) pVBInfo->P3c4, 0x32, 0xdf);        /* disable TV as primary VGA swap */
8913
8914        if ((pVBInfo->VBInfo & (SetSimuScanMode | SetCRT2ToDualEdge)))
8915            XGI_SetRegAND((XGIIOADDRESS) pVBInfo->Part2Port, 0x00, 0xdf);
8916
8917        if ((pVBInfo->SetFlag & DisableChB)
8918            || (pVBInfo->VBInfo & (DisableCRT2Display | SetSimuScanMode))
8919            || ((!(pVBInfo->VBInfo & SetCRT2ToLCDA))
8920                && (pVBInfo->
8921                    VBInfo & (SetCRT2ToRAMDAC | SetCRT2ToLCD | SetCRT2ToTV))))
8922            XGI_SetRegOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x00, 0x80);        /* BScreenOff=1 */
8923
8924        if ((pVBInfo->SetFlag & DisableChB)
8925            || (pVBInfo->VBInfo & (DisableCRT2Display | SetSimuScanMode))
8926            || (!(pVBInfo->VBInfo & SetCRT2ToLCDA))
8927            || (pVBInfo->
8928                VBInfo & (SetCRT2ToRAMDAC | SetCRT2ToLCD | SetCRT2ToTV))) {
8929            tempah = XGI_GetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x00);       /* save Part1 index 0 */
8930            XGI_SetRegOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x00, 0x10);        /* BTDAC = 1, avoid VB reset */
8931            XGI_SetRegAND((XGIIOADDRESS) pVBInfo->Part1Port, 0x1E, 0xDF);       /* disable CRT2 */
8932            XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x00, tempah);        /* restore Part1 index 0 */
8933        }
8934    }
8935    else {                      /* {301} */
8936
8937        if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToTV)) {
8938            XGI_SetRegOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x00, 0x80);        /* BScreenOff=1 */
8939            XGI_SetRegAND((XGIIOADDRESS) pVBInfo->Part1Port, 0x1E, 0xDF);       /* Disable CRT2 */
8940            XGI_SetRegAND((XGIIOADDRESS) pVBInfo->P3c4, 0x32, 0xDF);    /* Disable TV asPrimary VGA swap */
8941        }
8942
8943        if (pVBInfo->
8944            VBInfo & (DisableCRT2Display | SetCRT2ToLCDA | SetSimuScanMode))
8945            XGI_DisplayOff(HwDeviceExtension,pVBInfo);
8946    }
8947
8948
8949
8950
8951    if (HwDeviceExtension->jChipType < XG40) {
8952        if (!(pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA))
8953            || (XGI_DisableChISLCD(pVBInfo)) || (XGI_IsLCDON(pVBInfo))) {
8954            if (pVBInfo->LCDInfo & SetPWDEnable) {
8955                if (pVBInfo->LCDInfo & SetPWDEnable)
8956                    XGI_BacklightByDrv(pVBInfo);
8957                else {
8958                    XGI_SetPanelDelay(4, pVBInfo);
8959                    if (pVBInfo->VBType & VB_XGI301LV) {
8960                        tempbl = 0xFD;
8961                        tempah = 0x00;
8962                    }
8963                    else {
8964                        tempbl = 0xFB;
8965                        tempah = 0x04;
8966                    }
8967                }
8968            }
8969            XGI_SetPanelPower(tempah, tempbl, pVBInfo);
8970        }
8971    }
8972}
8973
8974
8975/* --------------------------------------------------------------------- */
8976/* Function : XGI_GetTVPtrIndex */
8977/* Input : */
8978/* Output : */
8979/* Description : bx 0 : ExtNTSC */
8980/* 1 : StNTSC */
8981/* 2 : ExtPAL */
8982/* 3 : StPAL */
8983/* 4 : ExtHiTV */
8984/* 5 : StHiTV */
8985/* 6 : Ext525i */
8986/* 7 : St525i */
8987/* 8 : Ext525p */
8988/* 9 : St525p */
8989/* A : Ext750p */
8990/* B : St750p */
8991/* --------------------------------------------------------------------- */
8992USHORT
8993XGI_GetTVPtrIndex(PVB_DEVICE_INFO pVBInfo)
8994{
8995    USHORT tempbx = 0;
8996
8997    if (pVBInfo->TVInfo & SetPALTV)
8998        tempbx = 2;
8999    if (pVBInfo->TVInfo & SetYPbPrMode1080i)
9000        tempbx = 4;
9001    if (pVBInfo->TVInfo & SetYPbPrMode525i)
9002        tempbx = 6;
9003    if (pVBInfo->TVInfo & SetYPbPrMode525p)
9004        tempbx = 8;
9005    if (pVBInfo->TVInfo & SetYPbPrMode750p)
9006        tempbx = 10;
9007    if (pVBInfo->TVInfo & TVSimuMode)
9008        tempbx++;
9009
9010    return tempbx;
9011}
9012
9013
9014/* --------------------------------------------------------------------- */
9015/* Function : XGI_OEM310Setting */
9016/* Input : */
9017/* Output : */
9018/* Description : Customized Param. for 301 */
9019/* --------------------------------------------------------------------- */
9020void
9021XGI_OEM310Setting(USHORT ModeNo, USHORT ModeIdIndex, PVB_DEVICE_INFO pVBInfo)
9022{
9023    if (pVBInfo->SetFlag & Win9xDOSMode)
9024        return;
9025
9026    /* GetPart1IO(); */
9027    XGI_SetDelayComp(pVBInfo);
9028
9029    if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA))
9030        XGI_SetLCDCap(pVBInfo);
9031
9032    if (pVBInfo->VBInfo & SetCRT2ToTV) {
9033        /* GetPart2IO() */
9034        XGI_SetPhaseIncr(pVBInfo);
9035        XGI_SetYFilter(ModeNo, ModeIdIndex, pVBInfo);
9036        XGI_SetAntiFlicker(ModeNo, ModeIdIndex, pVBInfo);
9037
9038        if (pVBInfo->VBType & VB_XGI301)
9039            XGI_SetEdgeEnhance(ModeNo, ModeIdIndex, pVBInfo);
9040    }
9041}
9042
9043
9044/* --------------------------------------------------------------------- */
9045/* Function : XGI_SetDelayComp */
9046/* Input : */
9047/* Output : */
9048/* Description : */
9049/* --------------------------------------------------------------------- */
9050void
9051XGI_SetDelayComp(PVB_DEVICE_INFO pVBInfo)
9052{
9053    USHORT index;
9054
9055    UCHAR tempah, tempbl, tempbh;
9056#ifndef LINUX_XF86
9057    UCHAR temp;
9058#endif
9059
9060    if (pVBInfo->
9061        VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV |
9062                  VB_XGI301C)) {
9063        if (pVBInfo->
9064            VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA | SetCRT2ToTV |
9065                      SetCRT2ToRAMDAC)) {
9066            tempbl = 0;
9067            tempbh = 0;
9068
9069            index = XGI_GetTVPtrIndex(pVBInfo); /* Get TV Delay */
9070            tempbl = pVBInfo->XGI_TVDelayList[index];
9071
9072            if (pVBInfo->
9073                VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV
9074                          | VB_XGI301C))
9075                tempbl = pVBInfo->XGI_TVDelayList2[index];
9076
9077            if (pVBInfo->VBInfo & SetCRT2ToDualEdge)
9078                tempbl = tempbl >> 4;
9079/*
9080            if ( pVBInfo->VBInfo & SetCRT2ToRAMDAC )
9081                tempbl = CRT2Delay1 ;			// Get CRT2 Delay
9082
9083            if ( pVBInfo->VBType & ( VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV | VB_XGI301C ) )
9084                tempbl = CRT2Delay2 ;
9085*/
9086            if (pVBInfo->VBInfo & (SetCRT2ToLCD | SetCRT2ToLCDA)) {
9087                index = XGI_GetLCDCapPtr(pVBInfo);      /* Get LCD Delay */
9088                tempbh = pVBInfo->LCDCapList[index].LCD_DelayCompensation;
9089
9090                if (!(pVBInfo->VBInfo & SetCRT2ToLCDA))
9091                    tempbl = tempbh;
9092            }
9093
9094            tempbl &= 0x0F;
9095            tempbh &= 0xF0;
9096            tempah = XGI_GetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x2D);
9097
9098            if (pVBInfo->VBInfo & (SetCRT2ToRAMDAC | SetCRT2ToLCD | SetCRT2ToTV)) {     /* Channel B */
9099                tempah &= 0xF0;
9100                tempah |= tempbl;
9101            }
9102
9103            if (pVBInfo->VBInfo & SetCRT2ToLCDA) {      /* Channel A */
9104                tempah &= 0x0F;
9105                tempah |= tempbh;
9106            }
9107            XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x2D, tempah);
9108        }
9109    }
9110    /* Jong 10/04/2007; merge code */
9111    else if ( pVBInfo->IF_DEF_LVDS == 1 )
9112    {
9113        tempbl = 0;
9114        tempbh = 0;
9115        if ( pVBInfo->VBInfo & SetCRT2ToLCD )
9116        {
9117            tempah = pVBInfo->LCDCapList[ XGI_GetLCDCapPtr(pVBInfo) ].LCD_DelayCompensation ;		/* / Get LCD Delay */
9118            tempah &= 0x0f ;
9119            tempah = tempah << 4 ;
9120            XGI_SetRegANDOR( (XGIIOADDRESS) pVBInfo->Part1Port , 0x2D , 0x0f , tempah ) ;
9121        }
9122    }
9123
9124}
9125
9126
9127/* --------------------------------------------------------------------- */
9128/* Function : XGI_SetLCDCap */
9129/* Input : */
9130/* Output : */
9131/* Description : */
9132/* --------------------------------------------------------------------- */
9133void
9134XGI_SetLCDCap(PVB_DEVICE_INFO pVBInfo)
9135{
9136    USHORT tempcx;
9137
9138    tempcx = pVBInfo->LCDCapList[XGI_GetLCDCapPtr(pVBInfo)].LCD_Capability;
9139
9140    if (pVBInfo->
9141        VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV |
9142                  VB_XGI301C)) {
9143        if (pVBInfo->VBType & (VB_XGI301LV | VB_XGI302LV | VB_XGI301C)) {       /* 301LV/302LV only */
9144            /* Set 301LV Capability */
9145            XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x24,
9146                       (UCHAR) (tempcx & 0x1F));
9147        }
9148        /* VB Driving */
9149        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part4Port, 0x0D,
9150                        ~((EnableVBCLKDRVLOW | EnablePLLSPLOW) >> 8),
9151                        (USHORT) ((tempcx &
9152                                   (EnableVBCLKDRVLOW | EnablePLLSPLOW)) >>
9153                                  8));
9154    }
9155
9156    if (pVBInfo->
9157        VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV |
9158                  VB_XGI301C)) {
9159        if (pVBInfo->VBInfo & SetCRT2ToLCD)
9160            XGI_SetLCDCap_B(tempcx, pVBInfo);
9161        else if (pVBInfo->VBInfo & SetCRT2ToLCDA)
9162            XGI_SetLCDCap_A(tempcx, pVBInfo);
9163
9164        if (pVBInfo->VBType & (VB_XGI302LV | VB_XGI301C)) {
9165            if (tempcx & EnableSpectrum)
9166                SetSpectrum(pVBInfo);
9167        }
9168    }
9169    else                        /* LVDS,CH7017 */
9170        XGI_SetLCDCap_A(tempcx, pVBInfo);
9171}
9172
9173
9174/* --------------------------------------------------------------------- */
9175/* Function : XGI_SetLCDCap_A */
9176/* Input : */
9177/* Output : */
9178/* Description : */
9179/* --------------------------------------------------------------------- */
9180void
9181XGI_SetLCDCap_A(USHORT tempcx, PVB_DEVICE_INFO pVBInfo)
9182{
9183    USHORT temp;
9184
9185    temp = XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x37);
9186
9187    if (temp & LCDRGB18Bit) {
9188        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x19, 0x0F, (USHORT) (0x20 | (tempcx & 0x00C0)));    /* Enable Dither */
9189        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x1A, 0x7F, 0x80);
9190    }
9191    else {
9192        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x19, 0x0F,
9193                        (USHORT) (0x30 | (tempcx & 0x00C0)));
9194        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x1A, 0x7F, 0x00);
9195    }
9196
9197/*
9198    if ( tempcx & EnableLCD24bpp )	// 24bits
9199    {
9200        XGI_SetRegANDOR((XGIIOADDRESS)pVBInfo->Part1Port,0x19, 0x0F,(USHORT)(0x30|(tempcx&0x00C0)) );
9201        XGI_SetRegANDOR((XGIIOADDRESS)pVBInfo->Part1Port,0x1A,0x7F,0x00);
9202    }
9203    else
9204    {
9205        XGI_SetRegANDOR((XGIIOADDRESS)pVBInfo->Part1Port,0x19, 0x0F,(USHORT)(0x20|(tempcx&0x00C0)) );//Enable Dither
9206        XGI_SetRegANDOR((XGIIOADDRESS)pVBInfo->Part1Port,0x1A,0x7F,0x80);
9207    }
9208*/
9209}
9210
9211
9212/* --------------------------------------------------------------------- */
9213/* Function : XGI_SetLCDCap_B */
9214/* Input : cx -> LCD Capability */
9215/* Output : */
9216/* Description : */
9217/* --------------------------------------------------------------------- */
9218void
9219XGI_SetLCDCap_B(USHORT tempcx, PVB_DEVICE_INFO pVBInfo)
9220{
9221    if (tempcx & EnableLCD24bpp)        /* 24bits */
9222        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part2Port, 0x1A, 0xE0,
9223                        (USHORT) (((tempcx & 0x00ff) >> 6) | 0x0c));
9224    else
9225        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part2Port, 0x1A, 0xE0, (USHORT) (((tempcx & 0x00ff) >> 6) | 0x18));     /* Enable Dither */
9226}
9227
9228
9229/* --------------------------------------------------------------------- */
9230/* Function : SetSpectrum */
9231/* Input : */
9232/* Output : */
9233/* Description : */
9234/* --------------------------------------------------------------------- */
9235void
9236SetSpectrum(PVB_DEVICE_INFO pVBInfo)
9237{
9238    USHORT index;
9239
9240    index = XGI_GetLCDCapPtr(pVBInfo);
9241
9242    XGI_SetRegAND((XGIIOADDRESS) pVBInfo->Part4Port, 0x30, 0x8F);       /* disable down spectrum D[4] */
9243    XGI_WaitEndRetrace(pVBInfo->RelIO);
9244    XGI_SetRegOR((XGIIOADDRESS) pVBInfo->Part4Port, 0x30, 0x20);        /* reset spectrum */
9245    XGI_WaitEndRetrace(pVBInfo->RelIO);
9246
9247    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x31,
9248               pVBInfo->LCDCapList[index].Spectrum_31);
9249    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x32,
9250               pVBInfo->LCDCapList[index].Spectrum_32);
9251    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x33,
9252               pVBInfo->LCDCapList[index].Spectrum_33);
9253    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x34,
9254               pVBInfo->LCDCapList[index].Spectrum_34);
9255    XGI_WaitEndRetrace(pVBInfo->RelIO);
9256    XGI_SetRegOR((XGIIOADDRESS) pVBInfo->Part4Port, 0x30, 0x40);        /* enable spectrum */
9257}
9258
9259
9260/* --------------------------------------------------------------------- */
9261/* Function : XGI_SetAntiFlicker */
9262/* Input : */
9263/* Output : */
9264/* Description : Set TV Customized Param. */
9265/* --------------------------------------------------------------------- */
9266void
9267XGI_SetAntiFlicker(USHORT ModeNo, USHORT ModeIdIndex, PVB_DEVICE_INFO pVBInfo)
9268{
9269    USHORT tempbx, index;
9270
9271    UCHAR tempah;
9272
9273    if (pVBInfo->TVInfo & (SetYPbPrMode525p | SetYPbPrMode750p))
9274        return;
9275
9276    tempbx = XGI_GetTVPtrIndex(pVBInfo);
9277    tempbx &= 0xFE;
9278
9279    if (ModeNo <= 0x13) {
9280        index = pVBInfo->SModeIDTable[ModeIdIndex].VB_StTVFlickerIndex;
9281    }
9282    else {
9283        index = pVBInfo->EModeIDTable[ModeIdIndex].VB_ExtTVFlickerIndex;
9284    }
9285
9286    tempbx += index;
9287    tempah = TVAntiFlickList[tempbx];
9288    tempah = tempah << 4;
9289
9290    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part2Port, 0x0A, 0x8F, tempah);
9291}
9292
9293
9294/* --------------------------------------------------------------------- */
9295/* Function : XGI_SetEdgeEnhance */
9296/* Input : */
9297/* Output : */
9298/* Description : */
9299/* --------------------------------------------------------------------- */
9300void
9301XGI_SetEdgeEnhance(USHORT ModeNo, USHORT ModeIdIndex, PVB_DEVICE_INFO pVBInfo)
9302{
9303    USHORT tempbx, index;
9304
9305    UCHAR tempah;
9306
9307
9308    tempbx = XGI_GetTVPtrIndex(pVBInfo);
9309    tempbx &= 0xFE;
9310
9311    if (ModeNo <= 0x13) {
9312        index = pVBInfo->SModeIDTable[ModeIdIndex].VB_StTVEdgeIndex;
9313    }
9314    else {
9315        index = pVBInfo->EModeIDTable[ModeIdIndex].VB_ExtTVEdgeIndex;
9316    }
9317
9318    tempbx += index;
9319    tempah = TVEdgeList[tempbx];
9320    tempah = tempah << 5;
9321
9322    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part2Port, 0x3A, 0x1F, tempah);
9323}
9324
9325
9326/* --------------------------------------------------------------------- */
9327/* Function : XGI_SetPhaseIncr */
9328/* Input : */
9329/* Output : */
9330/* Description : */
9331/* --------------------------------------------------------------------- */
9332void
9333XGI_SetPhaseIncr(PVB_DEVICE_INFO pVBInfo)
9334{
9335    USHORT tempbx;
9336
9337    UCHAR tempcl, tempch;
9338
9339    ULONG tempData;
9340
9341    XGI_GetTVPtrIndex2(&tempbx, &tempcl, &tempch, pVBInfo);     /* bx, cl, ch */
9342    tempData = TVPhaseList[tempbx];
9343
9344    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x31,
9345               (USHORT) (tempData & 0x000000FF));
9346    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x32,
9347               (USHORT) ((tempData & 0x0000FF00) >> 8));
9348    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x33,
9349               (USHORT) ((tempData & 0x00FF0000) >> 16));
9350    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x34,
9351               (USHORT) ((tempData & 0xFF000000) >> 24));
9352}
9353
9354
9355/* --------------------------------------------------------------------- */
9356/* Function : XGI_SetYFilter */
9357/* Input : */
9358/* Output : */
9359/* Description : */
9360/* --------------------------------------------------------------------- */
9361void
9362XGI_SetYFilter(USHORT ModeNo, USHORT ModeIdIndex, PVB_DEVICE_INFO pVBInfo)
9363{
9364    USHORT tempbx, index;
9365
9366    UCHAR tempcl, tempch, tempal;
9367    const UCHAR *filterPtr;
9368
9369    XGI_GetTVPtrIndex2(&tempbx, &tempcl, &tempch, pVBInfo);     /* bx, cl, ch */
9370
9371    switch (tempbx) {
9372    case 0x00:
9373    case 0x04:
9374        filterPtr = NTSCYFilter1;
9375        break;
9376
9377    case 0x01:
9378        filterPtr = PALYFilter1;
9379        break;
9380
9381    case 0x02:
9382    case 0x05:
9383    case 0x0D:
9384        filterPtr = PALMYFilter1;
9385        break;
9386
9387    case 0x03:
9388        filterPtr = PALNYFilter1;
9389        break;
9390
9391    case 0x08:
9392    case 0x0C:
9393        filterPtr = NTSCYFilter2;
9394        break;
9395
9396    case 0x0A:
9397        filterPtr = PALMYFilter2;
9398        break;
9399
9400    case 0x0B:
9401        filterPtr = PALNYFilter2;
9402        break;
9403
9404    case 0x09:
9405        filterPtr = PALYFilter2;
9406        break;
9407
9408    default:
9409        return;
9410    }
9411
9412    if (ModeNo <= 0x13)
9413        tempal = pVBInfo->SModeIDTable[ModeIdIndex].VB_StTVYFilterIndex;
9414    else
9415        tempal = pVBInfo->EModeIDTable[ModeIdIndex].VB_ExtTVYFilterIndex;
9416
9417    if (tempcl == 0)
9418        index = tempal * 4;
9419    else
9420        index = tempal * 7;
9421
9422    if ((tempcl == 0) && (tempch == 1)) {
9423        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x35, 0);
9424        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x36, 0);
9425        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x37, 0);
9426        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x38,
9427                   filterPtr[index++]);
9428    }
9429    else {
9430        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x35,
9431                   filterPtr[index++]);
9432        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x36,
9433                   filterPtr[index++]);
9434        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x37,
9435                   filterPtr[index++]);
9436        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x38,
9437                   filterPtr[index++]);
9438    }
9439
9440    if (pVBInfo->
9441        VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV |
9442                  VB_XGI301C)) {
9443        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x48,
9444                   filterPtr[index++]);
9445        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x49,
9446                   filterPtr[index++]);
9447        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part2Port, 0x4A,
9448                   filterPtr[index++]);
9449    }
9450}
9451
9452
9453/* --------------------------------------------------------------------- */
9454/* Function : XGI_GetTVPtrIndex2 */
9455/* Input : */
9456/* Output : bx 0 : NTSC */
9457/* 1 : PAL */
9458/* 2 : PALM */
9459/* 3 : PALN */
9460/* 4 : NTSC1024x768 */
9461/* 5 : PAL-M 1024x768 */
9462/* 6-7: reserved */
9463/* cl 0 : YFilter1 */
9464/* 1 : YFilter2 */
9465/* ch 0 : 301A */
9466/* 1 : 301B/302B/301LV/302LV */
9467/* Description : */
9468/* --------------------------------------------------------------------- */
9469void
9470XGI_GetTVPtrIndex2(USHORT * tempbx, UCHAR * tempcl, UCHAR * tempch,
9471                   PVB_DEVICE_INFO pVBInfo)
9472{
9473    *tempbx = 0;
9474    *tempcl = 0;
9475    *tempch = 0;
9476
9477    if (pVBInfo->TVInfo & SetPALTV)
9478        *tempbx = 1;
9479
9480    if (pVBInfo->TVInfo & SetPALMTV)
9481        *tempbx = 2;
9482
9483    if (pVBInfo->TVInfo & SetPALNTV)
9484        *tempbx = 3;
9485
9486    if (pVBInfo->TVInfo & NTSC1024x768) {
9487        *tempbx = 4;
9488        if (pVBInfo->TVInfo & SetPALMTV)
9489            *tempbx = 5;
9490    }
9491
9492    if (pVBInfo->
9493        VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV |
9494                  VB_XGI301C)) {
9495        if ((!(pVBInfo->VBInfo & SetInSlaveMode))
9496            || (pVBInfo->TVInfo & TVSimuMode)) {
9497            *tempbx += 8;
9498            *tempcl += 1;
9499        }
9500    }
9501
9502    if (pVBInfo->
9503        VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV |
9504                  VB_XGI301C))
9505        (*tempch)++;
9506}
9507
9508
9509/* --------------------------------------------------------------------- */
9510/* Function : XGI_SetCRT2ModeRegs */
9511/* Input : */
9512/* Output : */
9513/* Description : Origin code for crt2group */
9514/* --------------------------------------------------------------------- */
9515void
9516XGI_SetCRT2ModeRegs(USHORT ModeNo, PXGI_HW_DEVICE_INFO HwDeviceExtension,
9517                    PVB_DEVICE_INFO pVBInfo)
9518{
9519#ifndef LINUX_XF86
9520    USHORT i, j;
9521#endif
9522    USHORT tempbl;
9523    SHORT tempcl;
9524
9525    UCHAR tempah;
9526
9527    /* XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port , 0x03 , 0x00 ) ; // fix write part1 index 0 BTDRAM bit Bug */
9528    tempah = 0;
9529    if (!(pVBInfo->VBInfo & DisableCRT2Display)) {
9530        tempah = XGI_GetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x00);
9531        tempah &= ~0x10;        /* BTRAMDAC */
9532        tempah |= 0x40;         /* BTRAM */
9533
9534        if (pVBInfo->VBInfo & (SetCRT2ToRAMDAC | SetCRT2ToTV | SetCRT2ToLCD)) {
9535            tempah = 0x40;      /* BTDRAM */
9536            if (ModeNo > 0x13) {
9537                tempcl = pVBInfo->ModeType;
9538                tempcl -= ModeVGA;
9539                if (tempcl >= 0) {
9540                    tempah = (0x008 >> tempcl); /* BT Color */
9541                    if (tempah == 0)
9542                        tempah = 1;
9543                    tempah |= 0x040;
9544                }
9545            }
9546            if (pVBInfo->VBInfo & SetInSlaveMode)
9547                tempah ^= 0x50; /* BTDAC */
9548        }
9549    }
9550
9551/*	0210 shampoo
9552    if ( pVBInfo->VBInfo & DisableCRT2Display )
9553    {
9554        tempah = 0 ;
9555    }
9556
9557    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port , 0x00 , tempah ) ;
9558    if ( pVBInfo->VBInfo & ( SetCRT2ToRAMDAC | SetCRT2ToTV | SetCRT2ToLCD ) )
9559    {
9560        tempcl = pVBInfo->ModeType ;
9561        if ( ModeNo > 0x13 )
9562        {
9563            tempcl -= ModeVGA ;
9564            if ( ( tempcl > 0 ) || ( tempcl == 0 ) )
9565            {
9566                tempah=(0x008>>tempcl) ;
9567                if ( tempah == 0 )
9568                    tempah = 1 ;
9569                tempah |= 0x040;
9570            }
9571        }
9572        else
9573        {
9574            tempah = 0x040 ;
9575        }
9576
9577        if ( pVBInfo->VBInfo & SetInSlaveMode )
9578        {
9579            tempah = ( tempah ^ 0x050 ) ;
9580        }
9581    }
9582*/
9583
9584    XGI_SetReg((XGIIOADDRESS) pVBInfo->Part1Port, 0x00, tempah);
9585    tempah = 0x08;
9586    tempbl = 0xf0;
9587
9588    if (pVBInfo->VBInfo & DisableCRT2Display)
9589        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x2e, tempbl,
9590                        tempah);
9591    else {
9592        tempah = 0x00;
9593        tempbl = 0xff;
9594
9595        if (pVBInfo->
9596            VBInfo & (SetCRT2ToRAMDAC | SetCRT2ToTV | SetCRT2ToLCD |
9597                      SetCRT2ToLCDA)) {
9598            if ((pVBInfo->VBInfo & SetCRT2ToLCDA)
9599                && (!(pVBInfo->VBInfo & SetSimuScanMode))) {
9600                tempbl &= 0xf7;
9601                tempah |= 0x01;
9602                XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x2e,
9603                                tempbl, tempah);
9604            }
9605            else {
9606                if (pVBInfo->VBInfo & SetCRT2ToLCDA) {
9607                    tempbl &= 0xf7;
9608                    tempah |= 0x01;
9609                }
9610
9611                if (pVBInfo->
9612                    VBInfo & (SetCRT2ToRAMDAC | SetCRT2ToTV | SetCRT2ToLCD)) {
9613                    tempbl &= 0xf8;
9614                    tempah = 0x01;
9615
9616                    if (!(pVBInfo->VBInfo & SetInSlaveMode))
9617                        tempah |= 0x02;
9618
9619                    if (!(pVBInfo->VBInfo & SetCRT2ToRAMDAC)) {
9620                        tempah = tempah ^ 0x05;
9621                        if (!(pVBInfo->VBInfo & SetCRT2ToLCD))
9622                            tempah = tempah ^ 0x01;
9623                    }
9624
9625                    if (!(pVBInfo->VBInfo & SetCRT2ToDualEdge))
9626                        tempah |= 0x08;
9627                    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x2e,
9628                                    tempbl, tempah);
9629                }
9630                else
9631                    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x2e,
9632                                    tempbl, tempah);
9633            }
9634        }
9635        else
9636            XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x2e, tempbl,
9637                            tempah);
9638    }
9639
9640    if (pVBInfo->
9641        VBInfo & (SetCRT2ToRAMDAC | SetCRT2ToTV | SetCRT2ToLCD |
9642                  SetCRT2ToLCDA)) {
9643        tempah &= (~0x08);
9644        if ((pVBInfo->ModeType == ModeVGA)
9645            && (!(pVBInfo->VBInfo & SetInSlaveMode))) {
9646            tempah |= 0x010;
9647        }
9648        tempah |= 0x080;
9649
9650        if (pVBInfo->VBInfo & SetCRT2ToTV) {
9651            /* if ( !( pVBInfo->TVInfo & ( SetYPbPrMode525p | SetYPbPrMode750p ) ) ) */
9652            /* { */
9653            tempah |= 0x020;
9654            if (ModeNo > 0x13) {
9655                if (pVBInfo->VBInfo & DriverMode)
9656                    tempah = tempah ^ 0x20;
9657            }
9658            /* } */
9659        }
9660
9661        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part4Port, 0x0D, ~0x0BF,
9662                        tempah);
9663        tempah = 0;
9664
9665        if (pVBInfo->LCDInfo & SetLCDDualLink)
9666            tempah |= 0x40;
9667
9668        if (pVBInfo->VBInfo & SetCRT2ToTV) {
9669            /* if ( ( !( pVBInfo->VBInfo & SetCRT2ToHiVisionTV ) ) && ( !( pVBInfo->TVInfo & ( SetYPbPrMode525p | SetYPbPrMode750p ) ) ) ) */
9670            /* { */
9671            if (pVBInfo->TVInfo & RPLLDIV2XO)
9672                tempah |= 0x40;
9673            /* } */
9674        }
9675
9676        if ((pVBInfo->LCDResInfo == Panel1280x1024)
9677            || (pVBInfo->LCDResInfo == Panel1280x1024x75))
9678            tempah |= 0x80;
9679
9680        if (pVBInfo->LCDResInfo == Panel1280x960)
9681            tempah |= 0x80;
9682
9683        XGI_SetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x0C, tempah);
9684    }
9685
9686    if (pVBInfo->
9687        VBType & (VB_XGI301B | VB_XGI302B | VB_XGI301LV | VB_XGI302LV |
9688                  VB_XGI301C)) {
9689        tempah = 0;
9690        tempbl = 0xfb;
9691
9692        if (pVBInfo->VBInfo & SetCRT2ToDualEdge) {
9693            tempbl = 0xff;
9694            if (pVBInfo->VBInfo & SetCRT2ToLCDA)
9695                tempah |= 0x04; /* shampoo 0129 */
9696        }
9697
9698        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x13, tempbl,
9699                        tempah);
9700        tempah = 0x00;
9701        tempbl = 0xcf;
9702        if (!(pVBInfo->VBInfo & DisableCRT2Display)) {
9703            if (pVBInfo->VBInfo & SetCRT2ToDualEdge)
9704                tempah |= 0x30;
9705        }
9706
9707        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x2c, tempbl,
9708                        tempah);
9709        tempah = 0;
9710        tempbl = 0x3f;
9711
9712        if (!(pVBInfo->VBInfo & DisableCRT2Display)) {
9713            if (pVBInfo->VBInfo & SetCRT2ToDualEdge)
9714                tempah |= 0xc0;
9715        }
9716        XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part4Port, 0x21, tempbl,
9717                        tempah);
9718    }
9719
9720    tempah = 0;
9721    tempbl = 0x7f;
9722    if (!(pVBInfo->VBInfo & SetCRT2ToLCDA)) {
9723        tempbl = 0xff;
9724        if (!(pVBInfo->VBInfo & SetCRT2ToDualEdge))
9725            tempah |= 0x80;
9726    }
9727
9728    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part4Port, 0x23, tempbl, tempah);
9729
9730    if (pVBInfo->VBType & (VB_XGI302LV | VB_XGI301C)) {
9731        if (pVBInfo->LCDInfo & SetLCDDualLink) {
9732            XGI_SetRegOR((XGIIOADDRESS) pVBInfo->Part4Port, 0x27, 0x20);
9733            XGI_SetRegOR((XGIIOADDRESS) pVBInfo->Part4Port, 0x34, 0x10);
9734        }
9735    }
9736}
9737
9738
9739/* --------------------------------------------------------------------- */
9740/* Function : XGI_GetRAMDAC2DATA */
9741/* Input : */
9742/* Output : */
9743/* Description : */
9744/* --------------------------------------------------------------------- */
9745void
9746XGI_GetRAMDAC2DATA(USHORT ModeNo, USHORT ModeIdIndex,
9747                   USHORT RefreshRateTableIndex, PVB_DEVICE_INFO pVBInfo)
9748{
9749    USHORT tempax, tempbx, temp1, temp2, modeflag = 0, tempcx, CRT1Index;
9750#ifndef LINUX_XF86
9751    USHORT temp, ResInfo, DisplayType;
9752#endif
9753
9754    pVBInfo->RVBHCMAX = 1;
9755    pVBInfo->RVBHCFACT = 1;
9756
9757    if (ModeNo <= 0x13) {
9758        const USHORT StandTableIndex = XGI_GetModePtr(pVBInfo->SModeIDTable,
9759                                                      pVBInfo->ModeType,
9760                                                      ModeNo, ModeIdIndex);
9761
9762        modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
9763        tempax = pVBInfo->StandTable[StandTableIndex].CRTC[0];
9764        tempbx = pVBInfo->StandTable[StandTableIndex].CRTC[6];
9765        temp1 = pVBInfo->StandTable[StandTableIndex].CRTC[7];
9766    }
9767    else {
9768        modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
9769        CRT1Index = pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
9770        CRT1Index &= IndexMask;
9771        temp1 = (USHORT) pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[0];
9772        temp2 = (USHORT) pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[5];
9773        tempax = (temp1 & 0xFF) | ((temp2 & 0x03) << 8);
9774        tempbx = (USHORT) pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[8];
9775        tempcx = (USHORT) pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[14] << 8;
9776        tempcx &= 0x0100;
9777        tempcx = tempcx << 2;
9778        tempbx |= tempcx;
9779        temp1 = (USHORT) pVBInfo->XGINEWUB_CRT1Table[CRT1Index].CR[9];
9780    }
9781
9782    if (temp1 & 0x01)
9783        tempbx |= 0x0100;
9784
9785    if (temp1 & 0x20)
9786        tempbx |= 0x0200;
9787    tempax += 5;
9788
9789    if (modeflag & Charx8Dot)
9790        tempax *= 8;
9791    else
9792        tempax *= 9;
9793
9794    pVBInfo->VGAHT = tempax;
9795    pVBInfo->HT = tempax;
9796    tempbx++;
9797    pVBInfo->VGAVT = tempbx;
9798    pVBInfo->VT = tempbx;
9799}
9800
9801
9802
9803/* --------------------------------------------------------------------- */
9804/* Function : XGI_GetColorDepth */
9805/* Input : */
9806/* Output : */
9807/* Description : */
9808/* --------------------------------------------------------------------- */
9809USHORT
9810XGI_GetColorDepth(USHORT ModeNo, USHORT ModeIdIndex,
9811                  const VB_DEVICE_INFO *pVBInfo)
9812{
9813    USHORT ColorDepth[6] = { 1, 2, 4, 4, 6, 8 };
9814    SHORT index;
9815    USHORT modeflag;
9816
9817    if (ModeNo <= 0x13) {
9818        modeflag = pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag;
9819    }
9820    else {
9821        modeflag = pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
9822    }
9823
9824    index = (modeflag & ModeInfoFlag) - ModeEGA;
9825
9826    if (index < 0)
9827        index = 0;
9828
9829    return (ColorDepth[index]);
9830}
9831
9832
9833
9834/* --------------------------------------------------------------------- */
9835/* Function : XGI_UnLockCRT2 */
9836/* Input : */
9837/* Output : */
9838/* Description : */
9839/* --------------------------------------------------------------------- */
9840void
9841XGI_UnLockCRT2(PXGI_HW_DEVICE_INFO HwDeviceExtension, PVB_DEVICE_INFO pVBInfo)
9842{
9843
9844    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x2f, 0xFF, 0x01);
9845
9846}
9847
9848
9849/* --------------------------------------------------------------------- */
9850/* Function : XGI_LockCRT2 */
9851/* Input : */
9852/* Output : */
9853/* Description : */
9854/* --------------------------------------------------------------------- */
9855void
9856XGI_LockCRT2(PXGI_HW_DEVICE_INFO HwDeviceExtension, PVB_DEVICE_INFO pVBInfo)
9857{
9858    XGI_SetRegANDOR((XGIIOADDRESS) pVBInfo->Part1Port, 0x2F, 0xFE, 0x00);
9859}
9860
9861
9862/* --------------------------------------------------------------------- */
9863/* Function : XGINew_EnableCRT2 */
9864/* Input : */
9865/* Output : */
9866/* Description : */
9867/* --------------------------------------------------------------------- */
9868void
9869XGINew_EnableCRT2(PVB_DEVICE_INFO pVBInfo)
9870{
9871    XGI_SetRegOR((XGIIOADDRESS) pVBInfo->P3c4, 0x1E, SR1E_ENABLE_CRT2);
9872}
9873
9874
9875
9876/* --------------------------------------------------------------------- */
9877/* Function : */
9878/* Input : */
9879/* Output : */
9880/* Description : */
9881/* --------------------------------------------------------------------- */
9882void
9883XGINew_LCD_Wait_Time(UCHAR DelayTime, PVB_DEVICE_INFO pVBInfo)
9884{
9885    USHORT i, j;
9886
9887    ULONG temp, flag;
9888
9889    flag = 0;
9890
9891	PDEBUG(ErrorF("XGINew_LCD_Wait_Time()...begin\n"));
9892
9893    for (i = 0; i < DelayTime; i++) {
9894        for (j = 0; j < 66; j++) {
9895
9896			PDEBUG(ErrorF("i=%d, j=%d\n", i, j));
9897			temp = XGI_GetRegLong((XGIIOADDRESS) 0x61);
9898            temp &= 0x10;
9899
9900            if (temp == flag)
9901                continue;
9902
9903            flag = temp;
9904        }
9905    }
9906
9907	PDEBUG(ErrorF("XGINew_LCD_Wait_Time()...end\n"));
9908}
9909
9910
9911
9912
9913/* --------------------------------------------------------------------- */
9914/* Function : XGI_BridgeIsOn */
9915/* Input : */
9916/* Output : */
9917/* Description : */
9918/* --------------------------------------------------------------------- */
9919BOOLEAN
9920XGI_BridgeIsOn(PVB_DEVICE_INFO pVBInfo)
9921{
9922    USHORT flag;
9923
9924    /* Jong 10/04/2007; merge code */
9925    if ( pVBInfo->IF_DEF_LVDS == 1 )
9926    {
9927        return( 1 ) ;
9928    }
9929    else
9930    {
9931      flag = XGI_GetReg((XGIIOADDRESS) pVBInfo->Part4Port, 0x00);
9932      if ((flag == 1) || (flag == 2))
9933        return (1);             /* 301b */
9934      else
9935        return (0);
9936    }
9937}
9938
9939
9940/* --------------------------------------------------------------------- */
9941/* Function : XGI_VBLongWait */
9942/* Input : */
9943/* Output : */
9944/* Description : */
9945/* --------------------------------------------------------------------- */
9946void
9947XGI_VBLongWait(PVB_DEVICE_INFO pVBInfo)
9948{
9949    USHORT tempal, temp, i, j;
9950
9951    if (!(pVBInfo->VBInfo & SetCRT2ToTV)) {
9952        temp = 0;
9953        for (i = 0; i < 3; i++) {
9954            for (j = 0; j < 100; j++) {
9955                tempal = XGI_GetRegByte((XGIIOADDRESS) pVBInfo->P3da);
9956                if (temp & 0x01) {      /* VBWaitMode2 */
9957                    if ((tempal & 0x08)) {
9958                        continue;
9959                    }
9960
9961                    if (!(tempal & 0x08)) {
9962                        break;
9963                    }
9964                }
9965                else {          /* VBWaitMode1 */
9966                    if (!(tempal & 0x08)) {
9967                        continue;
9968                    }
9969
9970                    if ((tempal & 0x08)) {
9971                        break;
9972                    }
9973                }
9974            }
9975            temp = temp ^ 0x01;
9976        }
9977    }
9978    else {
9979        XGI_WaitEndRetrace(pVBInfo->RelIO);
9980    }
9981    return;
9982}
9983
9984
9985
9986
9987/* --------------------------------------------------------------------- */
9988/* Function : XGI_GetVGAHT2 */
9989/* Input : */
9990/* Output : */
9991/* Description : */
9992/* --------------------------------------------------------------------- */
9993USHORT
9994XGI_GetVGAHT2(PVB_DEVICE_INFO pVBInfo)
9995{
9996    ULONG tempax, tempbx;
9997
9998    tempbx =
9999        ((pVBInfo->VGAVT - pVBInfo->VGAVDE) * pVBInfo->RVBHCMAX) & 0xFFFF;
10000    tempax = (pVBInfo->VT - pVBInfo->VDE) * pVBInfo->RVBHCFACT;
10001    tempax = (tempax * pVBInfo->HT) / tempbx;
10002
10003    return ((USHORT) tempax);
10004}
10005
10006
10007/**
10008 * Get magic index into clock table.
10009 *
10010 * \bugs
10011 * I'm pretty sure the first if-statement is wrong.  It will \b always
10012 * evaluate to true.
10013 */
10014unsigned
10015XGI_GetVCLK2Ptr(USHORT ModeNo, USHORT ModeIdIndex,
10016                USHORT RefreshRateTableIndex,
10017                PVB_DEVICE_INFO pVBInfo)
10018{
10019	/* Jong 10/08/2007; merge code */
10020    USHORT tempbx ;
10021    const UCHAR *CHTVVCLKPtr = NULL ;
10022
10023	unsigned VCLKIndex;
10024    USHORT CRT2Index;
10025
10026	/* Jong 10/08/2007; merge code */
10027    USHORT LCDXlat1VCLK[ 4 ] = { VCLK65 + 2 , VCLK65 + 2 , VCLK65 + 2 , VCLK65 + 2 } ;
10028    USHORT LCDXlat2VCLK[ 4 ] = { VCLK108_2 + 5 , VCLK108_2 + 5 , VCLK108_2 + 5 , VCLK108_2 + 5 } ;
10029    USHORT LVDSXlat1VCLK[ 4 ] = { VCLK40 , VCLK40 , VCLK40 , VCLK40 } ;
10030    USHORT LVDSXlat2VCLK[ 4 ] = { VCLK65 + 2 , VCLK65 + 2 , VCLK65 + 2 , VCLK65 + 2 } ;
10031    USHORT LVDSXlat3VCLK[ 4 ] = { VCLK65 + 2 , VCLK65 + 2 , VCLK65 + 2 , VCLK65 + 2 } ;
10032
10033	const unsigned modeflag = (ModeNo <= 0x13)
10034        ? pVBInfo->SModeIDTable[ModeIdIndex].St_ModeFlag
10035        : pVBInfo->EModeIDTable[ModeIdIndex].Ext_ModeFlag;
10036
10037  /* Jong 10/04/2007; merge code */
10038  if ( pVBInfo->IF_DEF_LVDS == 0 )
10039  {
10040    CRT2Index = CRT2Index >> 6 ;        /*  for LCD */
10041
10042    if (((pVBInfo->VBInfo & SetCRT2ToLCD) | SetCRT2ToLCDA)) {   /*301b */
10043        VCLKIndex = (pVBInfo->LCDResInfo != Panel1024x768)
10044            ? (VCLK108_2 + 5) : (VCLK65 + 2);
10045    }
10046    else
10047	{
10048		if (pVBInfo->VBInfo & SetCRT2ToTV) /* for TV */
10049		{
10050            if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV)
10051			{
10052                VCLKIndex = (pVBInfo->SetFlag & RPLLDIV2XO)
10053                    ? HiTVVCLKDIV2 : HiTVVCLK;
10054
10055                VCLKIndex += 25;
10056
10057                if (pVBInfo->SetFlag & TVSimuMode) {
10058                    VCLKIndex = (modeflag & Charx8Dot)
10059                        ? HiTVSimuVCLK : HiTVTextVCLK;
10060
10061                    VCLKIndex += 25;
10062                }
10063
10064                if (pVBInfo->VBType & VB_XGI301LV) {
10065                    switch (pVBInfo->VBExtInfo) {
10066                    case VB_YPbPr1080i:
10067                        /* VCLKIndex already set to correct value? */
10068                        break;
10069                    case VB_YPbPr750p:
10070                        VCLKIndex = YPbPr750pVCLK;
10071                        break;
10072                    case VB_YPbPr525p:
10073                        VCLKIndex = YPbPr525pVCLK;
10074                        break;
10075                    case VB_YPbPr525i:
10076                        VCLKIndex = (pVBInfo->SetFlag & RPLLDIV2XO)
10077                            ? YPbPr525iVCLK_2 : YPbPr525iVCLK;
10078                        break;
10079                    }
10080                }
10081            }
10082            else {
10083                VCLKIndex = (pVBInfo->SetFlag & RPLLDIV2XO)
10084                    ? TVVCLKDIV2 : TVVCLK;
10085
10086                VCLKIndex += 25;
10087            }
10088        }
10089        else /* for CRT2 */
10090		{
10091            VCLKIndex = XGI_GetRegByte((XGIIOADDRESS) (pVBInfo->P3ca + 0x02));
10092            VCLKIndex = ((VCLKIndex >> 2) & 0x03);
10093            if (ModeNo > 0x13) {
10094                VCLKIndex =
10095                    (pVBInfo->RefIndex[RefreshRateTableIndex].Ext_CRTVCLK
10096                     & IndexMask);
10097            }
10098        }
10099    }
10100  }
10101  else /* Jong 10/04/2007; merge code */
10102  {		/* LVDS */
10103        if ( ModeNo <= 0x13 )
10104            VCLKIndex = CRT2Index ;
10105		else
10106			VCLKIndex = CRT2Index ;
10107
10108        if ( pVBInfo->IF_DEF_CH7005 == 1 )
10109        {
10110            if ( !( pVBInfo->VBInfo & SetCRT2ToLCD ) )
10111            {
10112                VCLKIndex &= 0x1f ;
10113				tempbx = 0 ;
10114
10115                if ( pVBInfo->VBInfo & SetPALTV )
10116                    tempbx += 2 ;
10117
10118                if ( pVBInfo->VBInfo & SetCHTVOverScan )
10119                    tempbx += 1 ;
10120
10121                switch( tempbx )
10122                {
10123                    case 0:
10124                        CHTVVCLKPtr = pVBInfo->CHTVVCLKUNTSC ;
10125                        break ;
10126                    case 1:
10127                        CHTVVCLKPtr = pVBInfo->CHTVVCLKONTSC ;
10128                        break;
10129                    case 2:
10130                        CHTVVCLKPtr = pVBInfo->CHTVVCLKUPAL ;
10131                        break ;
10132                    case 3:
10133                        CHTVVCLKPtr = pVBInfo->CHTVVCLKOPAL ;
10134                        break ;
10135                    default:
10136                        break ;
10137                }
10138
10139                VCLKIndex = CHTVVCLKPtr[ VCLKIndex ] ;
10140            }
10141        }
10142        else
10143        {
10144            VCLKIndex = VCLKIndex >> 6 ;
10145            if ( ( pVBInfo->LCDResInfo == Panel800x600 ) || ( pVBInfo->LCDResInfo == Panel320x480 ) )
10146                VCLKIndex = LVDSXlat1VCLK[ VCLKIndex ] ;
10147            else if ( ( pVBInfo->LCDResInfo == Panel1024x768 ) || ( pVBInfo->LCDResInfo == Panel1024x768x75 ) )
10148                VCLKIndex = LVDSXlat2VCLK[ VCLKIndex ] ;
10149            else
10150                VCLKIndex = LVDSXlat3VCLK[ VCLKIndex ] ;
10151        }
10152  }
10153
10154    return VCLKIndex;
10155}
10156
10157/* Jong@08212009 */
10158void XGIInitMiscVBInfo(PXGI_HW_DEVICE_INFO HwDeviceExtension, PVB_DEVICE_INFO pVBInfo)
10159{
10160	PDEBUG(ErrorF("XGIInitMiscVBInfo()...Begin\n"));
10161    pVBInfo->ROMAddr = HwDeviceExtension->pjVirtualRomBase ;
10162    pVBInfo->BaseAddr = ( ULONG )HwDeviceExtension->pjIOAddress ;
10163
10164    pVBInfo->P3c4 = pVBInfo->BaseAddr + 0x14 ;
10165    pVBInfo->P3d4 = pVBInfo->BaseAddr + 0x24 ;
10166    pVBInfo->P3c0 = pVBInfo->BaseAddr + 0x10 ;
10167    pVBInfo->P3ce = pVBInfo->BaseAddr + 0x1e ;
10168    pVBInfo->P3c2 = pVBInfo->BaseAddr + 0x12 ;
10169    pVBInfo->P3cc = pVBInfo->BaseAddr + 0x1C ;
10170    pVBInfo->P3ca = pVBInfo->BaseAddr + 0x1a ;
10171    pVBInfo->P3c6 = pVBInfo->BaseAddr + 0x16 ;
10172    pVBInfo->P3c7 = pVBInfo->BaseAddr + 0x17 ;
10173    pVBInfo->P3c8 = pVBInfo->BaseAddr + 0x18 ;
10174    pVBInfo->P3c9 = pVBInfo->BaseAddr + 0x19 ;
10175    pVBInfo->P3da = pVBInfo->BaseAddr + 0x2A ;
10176
10177    pVBInfo->Part0Port = pVBInfo->BaseAddr + XGI_CRT2_PORT_00 ;
10178    pVBInfo->Part1Port = pVBInfo->BaseAddr + XGI_CRT2_PORT_04 ;
10179    pVBInfo->Part2Port = pVBInfo->BaseAddr + XGI_CRT2_PORT_10 ;
10180    pVBInfo->Part3Port = pVBInfo->BaseAddr + XGI_CRT2_PORT_12 ;
10181    pVBInfo->Part4Port = pVBInfo->BaseAddr + XGI_CRT2_PORT_14 ;
10182    pVBInfo->Part5Port = pVBInfo->BaseAddr + XGI_CRT2_PORT_14 + 2 ;
10183
10184    pVBInfo->IF_DEF_LVDS = 0 ;
10185    pVBInfo->IF_DEF_CH7005 = 0 ;
10186    pVBInfo->IF_DEF_LCDA = 1 ;
10187    pVBInfo->IF_DEF_CH7017 = 0 ;
10188    pVBInfo->IF_DEF_CH7007 = 0 ;                                /* [Billy] 2007/05/14 */
10189    pVBInfo->IF_DEF_VideoCapture = 0 ;
10190    pVBInfo->IF_DEF_ScaleLCD = 0 ;
10191    pVBInfo->IF_DEF_OEMUtil = 0 ;
10192    pVBInfo->IF_DEF_PWD = 0 ;
10193
10194
10195    if ( HwDeviceExtension->jChipType >= XG20 )			/* kuku 2004/06/25 */
10196    {
10197    	pVBInfo->IF_DEF_YPbPr = 0 ;
10198        pVBInfo->IF_DEF_HiVision = 0 ;
10199        pVBInfo->IF_DEF_CRT2Monitor = 0 ;
10200    }
10201    else if ( HwDeviceExtension->jChipType >= XG40 )
10202    {
10203        pVBInfo->IF_DEF_YPbPr = 1 ;
10204        pVBInfo->IF_DEF_HiVision = 1 ;
10205        pVBInfo->IF_DEF_CRT2Monitor = 1 ;
10206    }
10207    else
10208    {
10209        pVBInfo->IF_DEF_YPbPr = 1 ;
10210        pVBInfo->IF_DEF_HiVision = 1 ;
10211        pVBInfo->IF_DEF_CRT2Monitor = 0 ;
10212    }
10213
10214    if ( HwDeviceExtension->jChipType == XG21 )  /* for x86 Linux, XG21 LVDS */
10215    {
10216        if ( ( XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4 , 0x38 ) & 0xE0 ) == 0xC0 )
10217        {
10218            pVBInfo->IF_DEF_LVDS = 1 ;
10219        }
10220
10221        if ( (XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4, 0x38) >> 5 & 0x07) == 0x03 )  /*CH7007 CR38 D[7-5]=011b*/
10222        {
10223            pVBInfo->IF_DEF_CH7007 = 1 ;
10224            /* HwDeviceExtension->bCH7007 = 1; */
10225            XGI_SetReg((XGIIOADDRESS) pVBInfo->P3c4, 0x30, 0x09);         /* For if (pHWDE->bVGAEnabled)== 0 */
10226/*            if( HwDeviceExtension->pDevice == NULL ) */
10227
10228            HwDeviceExtension->pDevice= HwDeviceExtension;
10229
10230        }
10231    }
10232
10233    if ( HwDeviceExtension->jChipType == XG27 )
10234    {
10235        if ( ( XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4 , 0x38 ) & 0xE0 ) == 0xC0 )
10236        {
10237          if ( XGI_GetReg((XGIIOADDRESS) pVBInfo->P3d4 , 0x30 ) & 0x20 )
10238          {
10239            pVBInfo->IF_DEF_LVDS = 1 ;
10240          }
10241        }
10242    }
10243
10244	PDEBUG(ErrorF("XGIInitMiscVBInfo()...End\n"));
10245}
10246