cyrix_helper.c 13 KB
Newer Older
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
 * Copyright 2000 by Richard A. Hecker, California, United States
 * Copyright 2002 by Red Hat Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Richard Hecker not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Richard Hecker makes no representations
 * about the suitability of this software for any purpose.  It is provided
 * "as is" without express or implied warranty.
 *
 * RICHARD HECKER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL RICHARD HECKER BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 *
 * RED HAT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL RICHARD HECKER BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 *
 * Author:  Richard Hecker, hecker@cat.dfrc.nasa.gov
 *          Re-written for XFree86 v4.0
 *
 * Chunks re-written again for XFree86 v4.2
 *	    Alan Cox <alan@redhat.com>
 *
 * Previous driver (pre-XFree86 v4.0) by
 *          Annius V. Groenink (A.V.Groenink@zfc.nl, avg@cwi.nl),
 *          Dirk H. Hohndel (hohndel@suse.de),
 *          Portions: the GGI project & confidential CYRIX databooks.
 */
42
/* $XFree86: xc/programs/Xserver/hw/xfree86/drivers/cyrix/cyrix_helper.c,v 1.5 2003/09/24 02:43:21 dawes Exp $ */
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
43
44
45
46
47

#include "cyrix.h"
#include "vgaHW.h"
#include "xf86_ansic.h"
#include "compiler.h"
48
#include "xf86.h"
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
49
50
51
52
53
54
55
56
57
58
59
60
61

#define CYRIXmarkLinesDirty { \
                               int k = 0; \
                               while (k < 1024) { \
	                         GX_REG(MC_DR_ADD) = k++; \
                                 GX_REG(MC_DR_ACC) = 0; \
                               } \
                            }

static void CYRIXresetVGA(ScrnInfoPtr pScrn, unsigned long vgaIOBase);

void  Cyrix1bppColorMap(ScrnInfoPtr pScrn)
{	/* use dedicated color map routines on new chipsets in 8bpp */
62
	xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "Cyrix 1BPP is only a stub for now.\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
	return;
}


int CyrixHWCursor(ScreenPtr pScreen)
{
	return 1024;
}

void CyrixRestore(ScrnInfoPtr pScrn, CYRIXRegPtr cyrixReg)
{
	unsigned char temp;
	CYRIXPrvPtr pCyrix;
	vgaHWPtr hwp;
	vgaRegPtr vgaReg;
	unsigned long vgaIOBase;

	vgaHWProtect(pScrn, TRUE);		/* Blank the screen */
	pCyrix = CYRIXPTR(pScrn);
/*	vgaReg = &VGAHWPTR(pScrn)->SavedReg; */
	hwp = VGAHWPTR(pScrn);
	vgaHWUnlock(hwp);
	vgaReg = &hwp->ModeReg;

	vgaIOBase = VGAHWPTR(pScrn)->IOBase;

	/* restore standard VGA portion */
	outb(vgaIOBase + 4, CrtcModeSwitchControl);
        outb(vgaIOBase + 5, 0x01);
	CYRIXresetVGA(pScrn,vgaIOBase);
	vgaHWRestore(pScrn, vgaReg, VGA_SR_ALL);

	vgaHWProtect(pScrn, TRUE);		/* Blank the screen */

	CYRIXmarkLinesDirty;

	/* restore miscellaneous output registers */
	outb(0x3C2, vgaReg->MiscOutReg);

	/* restore SoftVGA extended registers */
	outb(vgaIOBase + 4, CrtcDriverControl);
	temp = inb(vgaIOBase + 5);
	outb(vgaIOBase + 5, (pCyrix->PrevExt.DriverControl & 0x01)
	                  | (temp & 0xfe));

	outb(vgaIOBase + 4, CrtcVerticalTimingExtension);
	temp = inb(vgaIOBase + 5);
	outb(vgaIOBase + 5, (pCyrix->PrevExt.VerticalTimingExtension & 0x55)
	                  | (temp & 0xaa));

	outb(vgaIOBase + 4, CrtcExtendedAddressControl);
	temp = inb(vgaIOBase + 5);
	outb(vgaIOBase + 5, (pCyrix->PrevExt.ExtendedAddressControl & 0x07)
	                  | (temp & 0xf8));

	outb(vgaIOBase + 4, 19);
	outb(vgaIOBase + 5, pCyrix->PrevExt.Offset);
	
	outb(vgaIOBase + 4, CrtcExtendedOffset);
	temp = inb(vgaIOBase + 5);
	outb(vgaIOBase + 5, ((pCyrix->PrevExt.ExtendedOffset) & 0x03)
	                  | (temp & 0xfc));

	outb(vgaIOBase + 4, CrtcExtendedColorControl);
	temp = inb(vgaIOBase + 5);
	outb(vgaIOBase + 5, (pCyrix->PrevExt.ExtendedColorControl & 0x07)
	                  | (temp & 0xf8));

	outb(vgaIOBase + 4, CrtcDisplayCompression);
	temp = inb(vgaIOBase + 5);
	outb(vgaIOBase + 5, (pCyrix->PrevExt.DisplayCompression & 0x0f)
	                  | (temp & 0xf0));

	outb(vgaIOBase + 4, CrtcDACControl);
	temp = inb(vgaIOBase + 5);
	outb(vgaIOBase + 5, (pCyrix->PrevExt.DACControl & 0x0e)
	                  | (temp & 0xf1));

	outb(vgaIOBase + 4, CrtcModeSwitchControl);
        outb(vgaIOBase + 5, 0x00);

	/* let SoftVGA programming settle before we access DC registers,
	   but don't wait too long */
	usleep(1000);
	CYRIXmarkLinesDirty;

	/* restore display controller hardware registers */
#ifndef MONOVGA
#define DCFG_MASK       (DC_GCFG_FDTY | DC_GCFG_DECE | DC_GCFG_CMPE | DC_GCFG_FBLC | DC_GCFG_CURE)
#define GPBS_MASK       (BC_16BPP | BC_FB_WIDTH_2048)

	GX_REG(DC_UNLOCK) = DC_UNLOCK_VALUE;

	GX_REG(DC_CURS_ST_OFFSET) = pCyrix->PrevExt.DcCursStOffset;
	GX_REG(DC_CB_ST_OFFSET)  = pCyrix->PrevExt.DcCbStOffset;
	GX_REG(DC_LINE_DELTA)    = (GX_REG(DC_LINE_DELTA) & 0xFFC00FFF)
	                         | (pCyrix->PrevExt.DcLineDelta & 0x003FF000);
	GX_REG(DC_BUF_SIZE)      = (GX_REG(DC_BUF_SIZE) & 0xFFFF01FF)
                                 | (pCyrix->PrevExt.DcBufSize & 0x0000FE00);
	GX_REG(DC_CURSOR_X)      = pCyrix->PrevExt.DcCursorX;
	GX_REG(DC_CURSOR_Y)      = pCyrix->PrevExt.DcCursorY;
	GX_REG(DC_CURSOR_COLOR)  = pCyrix->PrevExt.DcCursorColor;

	GX_REG(DC_GENERAL_CFG)   = (GX_REG(DC_GENERAL_CFG) & (~DCFG_MASK))
	                         | (pCyrix->PrevExt.DcGeneralCfg & DCFG_MASK);

	GX_REG(DC_UNLOCK) = 0;

	GX_REG(GP_BLIT_STATUS)   = (GX_REG(GP_BLIT_STATUS) & (~GPBS_MASK))
	                         | (pCyrix->PrevExt.GpBlitStatus & GPBS_MASK);

#endif

	vgaHWProtect(pScrn, FALSE);		/* Turn on screen */
}

void *
CyrixSave(ScrnInfoPtr pScrn, CYRIXRegPtr cyrixReg)
{
	CYRIXPrvPtr	pCyrix;
	vgaRegPtr vgaReg;
	unsigned long	vgaIOBase;

#ifndef MONOVGA
	/* If we don't turn on the screen we end up restoring a 
	   blanked display on some boxes whose APM is a bit too smart.
	   Save the display turned -on- therefore */
	   
	vgaHWProtect(pScrn, FALSE);		/* Turn on screen */
	
	/* save graphics pipeline registers */
	pCyrix = CYRIXPTR(pScrn);
	vgaIOBase = VGAHWPTR(pScrn)->IOBase;
	vgaReg = &VGAHWPTR(pScrn)->SavedReg;
	pCyrix->PrevExt.GpBlitStatus   = GX_REG(GP_BLIT_STATUS);

	/* save miscellaneous output registers */
	vgaReg->MiscOutReg   = inb(0x3CC);

	/* save graphics pipeline registers */
	GX_REG(DC_UNLOCK)  = DC_UNLOCK_VALUE;
	pCyrix->PrevExt.DcGeneralCfg   = GX_REG(DC_GENERAL_CFG);
	pCyrix->PrevExt.DcCursStOffset = GX_REG(DC_CURS_ST_OFFSET);
	pCyrix->PrevExt.DcCbStOffset   = GX_REG(DC_CB_ST_OFFSET);
	pCyrix->PrevExt.DcLineDelta    = GX_REG(DC_LINE_DELTA);
	pCyrix->PrevExt.DcBufSize      = GX_REG(DC_BUF_SIZE);
	pCyrix->PrevExt.DcCursorX      = GX_REG(DC_CURSOR_X);
	pCyrix->PrevExt.DcCursorY      = GX_REG(DC_CURSOR_Y);
	pCyrix->PrevExt.DcCursorColor  = GX_REG(DC_CURSOR_COLOR);
	GX_REG(DC_UNLOCK)  = 0;

#endif

	/* save SoftVGA pCyrix->PrevExt.nded registers */
	outb(vgaIOBase + 4, CrtcVerticalTimingExtension);
	pCyrix->PrevExt.VerticalTimingExtension = inb(vgaIOBase + 5);

	outb(vgaIOBase + 4, CrtcExtendedAddressControl);
	pCyrix->PrevExt.ExtendedAddressControl = inb(vgaIOBase + 5);

	outb(vgaIOBase + 4, 19);
	pCyrix->PrevExt.Offset = inb(vgaIOBase + 5);
	
	outb(vgaIOBase + 4, CrtcExtendedOffset);
	pCyrix->PrevExt.ExtendedOffset = inb(vgaIOBase + 5);

	outb(vgaIOBase + 4, CrtcExtendedColorControl);
	pCyrix->PrevExt.ExtendedColorControl = inb(vgaIOBase + 5);

	outb(vgaIOBase + 4, CrtcDisplayCompression);
	pCyrix->PrevExt.DisplayCompression = inb(vgaIOBase + 5);

	outb(vgaIOBase + 4, CrtcDriverControl);
	pCyrix->PrevExt.DriverControl = inb(vgaIOBase + 5);

	outb(vgaIOBase + 4, CrtcDACControl);
	pCyrix->PrevExt.DACControl = inb(vgaIOBase + 5);

	outb(vgaIOBase + 4, CrtcClockControl);
	pCyrix->PrevExt.ClockControl = inb(vgaIOBase + 5);

	outb(vgaIOBase + 4, CrtcClockFrequency);
	pCyrix->PrevExt.CrtClockFrequency = inb(vgaIOBase + 5);

	outb(vgaIOBase + 4, CrtcClockFrequencyFraction);
	pCyrix->PrevExt.CrtClockFrequencyFraction = inb(vgaIOBase + 5);

	outb(vgaIOBase + 4, CrtcRefreshRate);
	pCyrix->PrevExt.RefreshRate = inb(vgaIOBase + 5);

	/* save standard VGA portion */
	CYRIXresetVGA(pScrn,vgaIOBase);

	return((void *)vgaIOBase);
}


Bool
CyrixInit(ScrnInfoPtr pScrn, DisplayModePtr mode)
{
	CYRIXPrvPtr	pCyrix;
	int offset_shift = (pScrn->bitsPerPixel == 16) ? 2 :
                           (pScrn->bitsPerPixel == 8) ? 3 : 4;
	int line_offset = pScrn->displayWidth >> offset_shift;

	pCyrix = CYRIXPTR(pScrn);

	/* initialize standard VGA portion */
	if (!vgaHWInit(pScrn, mode))
		return(FALSE);

	/* initialize SoftVGA extended registers */
	pCyrix->PrevExt.VerticalTimingExtension =
		((mode->CrtcVSyncStart & 0x400) >> 4) |
		(((mode->CrtcVDisplay - 1) & 0x400) >> 8) |
		(((mode->CrtcVTotal - 2) & 0x400) >> 10) |
		((mode->CrtcVSyncStart & 0x400) >> 6);

	if (pScrn->bitsPerPixel < 8)
		pCyrix->PrevExt.ExtendedAddressControl = EAC_DIRECT_FRAME_BUFFER;
	else
		pCyrix->PrevExt.ExtendedAddressControl = EAC_DIRECT_FRAME_BUFFER |
		                                       EAC_PACKED_CHAIN4;

	pCyrix->PrevExt.ExtendedOffset = ((line_offset >> 8) & 0x03);
	pCyrix->PrevExt.Offset = line_offset;

	pCyrix->PrevExt.ExtendedColorControl = (pScrn->bitsPerPixel == 16)
                                           ? ECC_16BPP | ECC_565_FORMAT
                                           : ECC_8BPP;

	/* display compression is set using the DC registers */
	pCyrix->PrevExt.DisplayCompression = 0x00;

	/* we drive the palette through the display controller (in new
	   chipsets only) in 8bpp and 16bpp (that is, whenever the
	   hardware cursor is used). */
	if (pScrn->bitsPerPixel < 8)
		pCyrix->PrevExt.DriverControl = 0x00;
	else
		pCyrix->PrevExt.DriverControl = DRVCT_DISPLAY_DRIVER_ACTIVE;

	/* set `16 bit bus' or else compression will hang the
	   system in 16bpp mode */
	if (pScrn->bitsPerPixel == 16)
		pCyrix->PrevExt.DACControl = DACCT_ENABLE_16BIT_BUS;
	else
		pCyrix->PrevExt.DACControl = 0;


#ifndef MONOVGA
	/* initialize masked contents of display controller
	   hardware registers. */
	pCyrix->PrevExt.DcCursStOffset =  pCyrix->CYRIXcursorAddress;
	pCyrix->PrevExt.DcLineDelta   =  0 << 12;
	pCyrix->PrevExt.DcBufSize     =  0x41 << 9;
	pCyrix->PrevExt.DcCursorX     =  0;
	pCyrix->PrevExt.DcCursorY     =  0;
	pCyrix->PrevExt.DcCursorColor =  0;

	/* Compression  is enabled only  when a buffer  was allocated by
	   FbInit  and provided that the displayed screen is the virtual
	   screen.  If the line delta is not 1024 or 2048, entire frames
	   will be flagged dirty as opposed to lines.  Problems with 16bpp
	   and line-dirty flagging seem to have been solved now.  */
	   
	if (pCyrix->NoCompress == FALSE &&
		mode->CrtcVDisplay == pScrn->virtualY &&
		mode->CrtcHDisplay == pScrn->virtualX &&
		0 == GX_REG(DC_FB_ST_OFFSET))
	{
			pCyrix->PrevExt.DcGeneralCfg = DC_GCFG_DECE | DC_GCFG_CMPE;
			if (/* pScrn->bitsPerPixel != 8 ||   -- this is OK now */
			   (pScrn->displayWidth * (pScrn->bitsPerPixel / 8)) & 0x03FF)
				pCyrix->PrevExt.DcGeneralCfg |= DC_GCFG_FDTY;
	}
	else
		pCyrix->PrevExt.DcGeneralCfg = 0;


	/* initialize the graphics pipeline registers */
	pCyrix->PrevExt.GpBlitStatus  =  ((pScrn->displayWidth == 2048) ?
	                                BC_FB_WIDTH_2048 : BC_FB_WIDTH_1024) |
	                               ((pScrn->bitsPerPixel == 16) ?
	                                BC_16BPP : BC_8BPP);
#endif

	return(TRUE);
}


static void
CYRIXresetVGA(ScrnInfoPtr pScrn, unsigned long vgaIOBase)
{
    CYRIXPrvPtr pCyrix = CYRIXPTR(pScrn);
    unsigned char temp;
    /* switch off compression and cursor the hard way */
    GX_REG(DC_UNLOCK)  = DC_UNLOCK_VALUE;
	GX_REG(DC_GENERAL_CFG) &= ~(DC_GCFG_CMPE | DC_GCFG_DECE | DC_GCFG_FDTY | DC_GCFG_CURE);
	GX_REG(DC_UNLOCK)  = 0;
	CYRIXmarkLinesDirty;

	/* reset SoftVGA extensions to standard VGA behaviour */
	outb(vgaIOBase + 4, CrtcExtendedAddressControl);
	temp = inb(vgaIOBase + 5);
	outb(vgaIOBase + 5, temp & 0xf8);
	outb(vgaIOBase + 4, CrtcExtendedStartAddress);
	outb(vgaIOBase + 5, 0x00);
	outb(vgaIOBase + 4, CrtcWriteMemoryAperture);
	temp = inb(vgaIOBase + 5);
	outb(vgaIOBase + 5, temp & 0xe0);
	outb(vgaIOBase + 4, CrtcReadMemoryAperture);
	temp = inb(vgaIOBase + 5);
	outb(vgaIOBase + 5, temp & 0xe0);
	outb(vgaIOBase + 4, CrtcDriverControl);
	temp = inb(vgaIOBase + 5);
	outb(vgaIOBase + 5, temp & 0xfe);
	outb(vgaIOBase + 4, CrtcDisplayCompression);
	temp = inb(vgaIOBase + 5);
	outb(vgaIOBase + 5, temp & 0xf0);
}