savagefb_driver.c 59.8 KB
Newer Older
L
Linus Torvalds 已提交
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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
/*
 * linux/drivers/video/savagefb.c -- S3 Savage Framebuffer Driver
 *
 * Copyright (c) 2001-2002  Denis Oliver Kropp <dok@directfb.org>
 *                          Sven Neumann <neo@directfb.org>
 *
 *
 * Card specific code is based on XFree86's savage driver.
 * Framebuffer framework code is based on code of cyber2000fb and tdfxfb.
 *
 * This file is subject to the terms and conditions of the GNU General
 * Public License.  See the file COPYING in the main directory of this
 * archive for more details.
 *
 * 0.4.0 (neo)
 *  - hardware accelerated clear and move
 *
 * 0.3.2 (dok)
 *  - wait for vertical retrace before writing to cr67
 *    at the beginning of savagefb_set_par
 *  - use synchronization registers cr23 and cr26
 *
 * 0.3.1 (dok)
 *  - reset 3D engine
 *  - don't return alpha bits for 32bit format
 *
 * 0.3.0 (dok)
 *  - added WaitIdle functions for all Savage types
 *  - do WaitIdle before mode switching
 *  - code cleanup
 *
 * 0.2.0 (dok)
 *  - first working version
 *
 *
 * TODO
 * - clock validations in decode_var
 *
 * BUGS
 * - white margin on bootup
 *
 */

#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/tty.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/fb.h>
#include <linux/pci.h>
#include <linux/init.h>
#include <linux/console.h>

#include <asm/io.h>
#include <asm/irq.h>
#include <asm/pgtable.h>
#include <asm/system.h>
#include <asm/uaccess.h>

#ifdef CONFIG_MTRR
#include <asm/mtrr.h>
#endif

#include "savagefb.h"


#define SAVAGEFB_VERSION "0.4.0_2.6"

/* --------------------------------------------------------------------- */


static char *mode_option __initdata = NULL;

#ifdef MODULE

MODULE_AUTHOR("(c) 2001-2002  Denis Oliver Kropp <dok@directfb.org>");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("FBDev driver for S3 Savage PCI/AGP Chips");

#endif


/* --------------------------------------------------------------------- */

static void vgaHWSeqReset (struct savagefb_par *par, int start)
{
	if (start)
92
		VGAwSEQ (0x00, 0x01, par);	/* Synchronous Reset */
L
Linus Torvalds 已提交
93
	else
94
		VGAwSEQ (0x00, 0x03, par);	/* End Reset */
L
Linus Torvalds 已提交
95 96 97 98 99 100 101 102 103 104
}

static void vgaHWProtect (struct savagefb_par *par, int on)
{
	unsigned char tmp;

	if (on) {
		/*
		 * Turn off screen and disable sequencer.
		 */
105
		tmp = VGArSEQ (0x01, par);
L
Linus Torvalds 已提交
106 107

		vgaHWSeqReset (par, 1);	        /* start synchronous reset */
108
		VGAwSEQ (0x01, tmp | 0x20, par);/* disable the display */
L
Linus Torvalds 已提交
109

110
		VGAenablePalette(par);
L
Linus Torvalds 已提交
111 112 113 114 115
	} else {
		/*
		 * Reenable sequencer, then turn on screen.
		 */

116
		tmp = VGArSEQ (0x01, par);
L
Linus Torvalds 已提交
117

118
		VGAwSEQ (0x01, tmp & ~0x20, par);/* reenable display */
L
Linus Torvalds 已提交
119 120
		vgaHWSeqReset (par, 0);	        /* clear synchronous reset */

121
		VGAdisablePalette(par);
L
Linus Torvalds 已提交
122 123 124 125 126 127 128
	}
}

static void vgaHWRestore (struct savagefb_par  *par)
{
	int i;

129
	VGAwMISC (par->MiscOutReg, par);
L
Linus Torvalds 已提交
130 131

	for (i = 1; i < 5; i++)
132
		VGAwSEQ (i, par->Sequencer[i], par);
L
Linus Torvalds 已提交
133 134 135

	/* Ensure CRTC registers 0-7 are unlocked by clearing bit 7 or
	   CRTC[17] */
136
	VGAwCR (17, par->CRTC[17] & ~0x80, par);
L
Linus Torvalds 已提交
137 138

	for (i = 0; i < 25; i++)
139
		VGAwCR (i, par->CRTC[i], par);
L
Linus Torvalds 已提交
140 141

	for (i = 0; i < 9; i++)
142
		VGAwGR (i, par->Graphics[i], par);
L
Linus Torvalds 已提交
143

144
	VGAenablePalette(par);
L
Linus Torvalds 已提交
145 146

	for (i = 0; i < 21; i++)
147
		VGAwATTR (i, par->Attribute[i], par);
L
Linus Torvalds 已提交
148

149
	VGAdisablePalette(par);
L
Linus Torvalds 已提交
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
}

static void vgaHWInit (struct fb_var_screeninfo *var,
		       struct savagefb_par            *par,
		       struct xtimings                *timings)
{
	par->MiscOutReg = 0x23;

	if (!(timings->sync & FB_SYNC_HOR_HIGH_ACT))
		par->MiscOutReg |= 0x40;

	if (!(timings->sync & FB_SYNC_VERT_HIGH_ACT))
		par->MiscOutReg |= 0x80;

	/*
	 * Time Sequencer
	 */
	par->Sequencer[0x00] = 0x00;
	par->Sequencer[0x01] = 0x01;
	par->Sequencer[0x02] = 0x0F;
	par->Sequencer[0x03] = 0x00;          /* Font select */
	par->Sequencer[0x04] = 0x0E;          /* Misc */

	/*
	 * CRTC Controller
	 */
	par->CRTC[0x00] = (timings->HTotal >> 3) - 5;
	par->CRTC[0x01] = (timings->HDisplay >> 3) - 1;
	par->CRTC[0x02] = (timings->HSyncStart >> 3) - 1;
	par->CRTC[0x03] = (((timings->HSyncEnd >> 3)  - 1) & 0x1f) | 0x80;
	par->CRTC[0x04] = (timings->HSyncStart >> 3);
	par->CRTC[0x05] = ((((timings->HSyncEnd >> 3) - 1) & 0x20) << 2) |
		(((timings->HSyncEnd >> 3)) & 0x1f);
	par->CRTC[0x06] = (timings->VTotal - 2) & 0xFF;
	par->CRTC[0x07] = (((timings->VTotal - 2) & 0x100) >> 8) |
		(((timings->VDisplay - 1) & 0x100) >> 7) |
		((timings->VSyncStart & 0x100) >> 6) |
		(((timings->VSyncStart - 1) & 0x100) >> 5) |
		0x10 |
		(((timings->VTotal - 2) & 0x200) >> 4) |
		(((timings->VDisplay - 1) & 0x200) >> 3) |
		((timings->VSyncStart & 0x200) >> 2);
	par->CRTC[0x08] = 0x00;
	par->CRTC[0x09] = (((timings->VSyncStart - 1) & 0x200) >> 4) | 0x40;

	if (timings->dblscan)
		par->CRTC[0x09] |= 0x80;

	par->CRTC[0x0a] = 0x00;
	par->CRTC[0x0b] = 0x00;
	par->CRTC[0x0c] = 0x00;
	par->CRTC[0x0d] = 0x00;
	par->CRTC[0x0e] = 0x00;
	par->CRTC[0x0f] = 0x00;
	par->CRTC[0x10] = timings->VSyncStart & 0xff;
	par->CRTC[0x11] = (timings->VSyncEnd & 0x0f) | 0x20;
	par->CRTC[0x12] = (timings->VDisplay - 1) & 0xff;
	par->CRTC[0x13] = var->xres_virtual >> 4;
	par->CRTC[0x14] = 0x00;
	par->CRTC[0x15] = (timings->VSyncStart - 1) & 0xff;
	par->CRTC[0x16] = (timings->VSyncEnd - 1) & 0xff;
	par->CRTC[0x17] = 0xc3;
	par->CRTC[0x18] = 0xff;

	/*
	 * are these unnecessary?
	 * vgaHWHBlankKGA(mode, regp, 0, KGA_FIX_OVERSCAN|KGA_ENABLE_ON_ZERO);
	 * vgaHWVBlankKGA(mode, regp, 0, KGA_FIX_OVERSCAN|KGA_ENABLE_ON_ZERO);
	 */

	/*
	 * Graphics Display Controller
	 */
	par->Graphics[0x00] = 0x00;
	par->Graphics[0x01] = 0x00;
	par->Graphics[0x02] = 0x00;
	par->Graphics[0x03] = 0x00;
	par->Graphics[0x04] = 0x00;
	par->Graphics[0x05] = 0x40;
	par->Graphics[0x06] = 0x05;   /* only map 64k VGA memory !!!! */
	par->Graphics[0x07] = 0x0F;
	par->Graphics[0x08] = 0xFF;


	par->Attribute[0x00]  = 0x00; /* standard colormap translation */
	par->Attribute[0x01]  = 0x01;
	par->Attribute[0x02]  = 0x02;
	par->Attribute[0x03]  = 0x03;
	par->Attribute[0x04]  = 0x04;
	par->Attribute[0x05]  = 0x05;
	par->Attribute[0x06]  = 0x06;
	par->Attribute[0x07]  = 0x07;
	par->Attribute[0x08]  = 0x08;
	par->Attribute[0x09]  = 0x09;
	par->Attribute[0x0a] = 0x0A;
	par->Attribute[0x0b] = 0x0B;
	par->Attribute[0x0c] = 0x0C;
	par->Attribute[0x0d] = 0x0D;
	par->Attribute[0x0e] = 0x0E;
	par->Attribute[0x0f] = 0x0F;
	par->Attribute[0x10] = 0x41;
	par->Attribute[0x11] = 0xFF;
	par->Attribute[0x12] = 0x0F;
	par->Attribute[0x13] = 0x00;
	par->Attribute[0x14] = 0x00;
}

/* -------------------- Hardware specific routines ------------------------- */

/*
 * Hardware Acceleration for SavageFB
 */

/* Wait for fifo space */
static void
savage3D_waitfifo(struct savagefb_par *par, int space)
{
	int slots = MAXFIFO - space;

269
	while ((savage_in32(0x48C00, par) & 0x0000ffff) > slots);
L
Linus Torvalds 已提交
270 271 272 273 274 275 276
}

static void
savage4_waitfifo(struct savagefb_par *par, int space)
{
	int slots = MAXFIFO - space;

277
	while ((savage_in32(0x48C60, par) & 0x001fffff) > slots);
L
Linus Torvalds 已提交
278 279 280 281 282 283 284
}

static void
savage2000_waitfifo(struct savagefb_par *par, int space)
{
	int slots = MAXFIFO - space;

285
	while ((savage_in32(0x48C60, par) & 0x0000ffff) > slots);
L
Linus Torvalds 已提交
286 287 288 289 290 291
}

/* Wait for idle accelerator */
static void
savage3D_waitidle(struct savagefb_par *par)
{
292
	while ((savage_in32(0x48C00, par) & 0x0008ffff) != 0x80000);
L
Linus Torvalds 已提交
293 294 295 296 297
}

static void
savage4_waitidle(struct savagefb_par *par)
{
298
	while ((savage_in32(0x48C60, par) & 0x00a00000) != 0x00a00000);
L
Linus Torvalds 已提交
299 300 301 302 303
}

static void
savage2000_waitidle(struct savagefb_par *par)
{
304
	while ((savage_in32(0x48C60, par) & 0x009fffff));
L
Linus Torvalds 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
}


static void
SavageSetup2DEngine (struct savagefb_par  *par)
{
	unsigned long GlobalBitmapDescriptor;

	GlobalBitmapDescriptor = 1 | 8 | BCI_BD_BW_DISABLE;
	BCI_BD_SET_BPP (GlobalBitmapDescriptor, par->depth);
	BCI_BD_SET_STRIDE (GlobalBitmapDescriptor, par->vwidth);

	switch(par->chip) {
	case S3_SAVAGE3D:
	case S3_SAVAGE_MX:
		/* Disable BCI */
321
		savage_out32(0x48C18, savage_in32(0x48C18, par) & 0x3FF0, par);
L
Linus Torvalds 已提交
322
		/* Setup BCI command overflow buffer */
323 324 325
		savage_out32(0x48C14,
			     (par->cob_offset >> 11) | (par->cob_index << 29),
			     par);
L
Linus Torvalds 已提交
326
		/* Program shadow status update. */
327 328
		savage_out32(0x48C10, 0x78207220, par);
		savage_out32(0x48C0C, 0, par);
L
Linus Torvalds 已提交
329
		/* Enable BCI and command overflow buffer */
330
		savage_out32(0x48C18, savage_in32(0x48C18, par) | 0x0C, par);
L
Linus Torvalds 已提交
331 332 333 334 335
		break;
	case S3_SAVAGE4:
	case S3_PROSAVAGE:
	case S3_SUPERSAVAGE:
		/* Disable BCI */
336
		savage_out32(0x48C18, savage_in32(0x48C18, par) & 0x3FF0, par);
L
Linus Torvalds 已提交
337
		/* Program shadow status update */
338 339
		savage_out32(0x48C10, 0x00700040, par);
		savage_out32(0x48C0C, 0, par);
L
Linus Torvalds 已提交
340
		/* Enable BCI without the COB */
341
		savage_out32(0x48C18, savage_in32(0x48C18, par) | 0x08, par);
L
Linus Torvalds 已提交
342 343 344
		break;
	case S3_SAVAGE2000:
		/* Disable BCI */
345
		savage_out32(0x48C18, 0, par);
L
Linus Torvalds 已提交
346
		/* Setup BCI command overflow buffer */
347 348 349
		savage_out32(0x48C18,
			     (par->cob_offset >> 7) | (par->cob_index),
			     par);
L
Linus Torvalds 已提交
350
		/* Disable shadow status update */
351
		savage_out32(0x48A30, 0, par);
L
Linus Torvalds 已提交
352
		/* Enable BCI and command overflow buffer */
353 354
		savage_out32(0x48C18, savage_in32(0x48C18, par) | 0x00280000,
			     par);
L
Linus Torvalds 已提交
355 356 357 358 359
		break;
	    default:
		break;
	}
	/* Turn on 16-bit register access. */
360 361
	vga_out8(0x3d4, 0x31, par);
	vga_out8(0x3d5, 0x0c, par);
L
Linus Torvalds 已提交
362 363

	/* Set stride to use GBD. */
364 365
	vga_out8 (0x3d4, 0x50, par);
	vga_out8 (0x3d5, vga_in8(0x3d5, par) | 0xC1, par);
L
Linus Torvalds 已提交
366 367

	/* Enable 2D engine. */
368 369
	vga_out8 (0x3d4, 0x40, par);
	vga_out8 (0x3d5, 0x01, par);
L
Linus Torvalds 已提交
370

371 372
	savage_out32 (MONO_PAT_0, ~0, par);
	savage_out32 (MONO_PAT_1, ~0, par);
L
Linus Torvalds 已提交
373 374

	/* Setup plane masks */
375 376 377 378
	savage_out32 (0x8128, ~0, par); /* enable all write planes */
	savage_out32 (0x812C, ~0, par); /* enable all read planes */
	savage_out16 (0x8134, 0x27, par);
	savage_out16 (0x8136, 0x07, par);
L
Linus Torvalds 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495

	/* Now set the GBD */
	par->bci_ptr = 0;
	par->SavageWaitFifo (par, 4);

	BCI_SEND( BCI_CMD_SETREG | (1 << 16) | BCI_GBD1 );
	BCI_SEND( 0 );
	BCI_SEND( BCI_CMD_SETREG | (1 << 16) | BCI_GBD2 );
	BCI_SEND( GlobalBitmapDescriptor );
}


static void SavageCalcClock(long freq, int min_m, int min_n1, int max_n1,
			    int min_n2, int max_n2, long freq_min,
			    long freq_max, unsigned int *mdiv,
			    unsigned int *ndiv, unsigned int *r)
{
	long diff, best_diff;
	unsigned int m;
	unsigned char n1, n2, best_n1=16+2, best_n2=2, best_m=125+2;

	if (freq < freq_min / (1 << max_n2)) {
		printk (KERN_ERR "invalid frequency %ld Khz\n", freq);
		freq = freq_min / (1 << max_n2);
	}
	if (freq > freq_max / (1 << min_n2)) {
		printk (KERN_ERR "invalid frequency %ld Khz\n", freq);
		freq = freq_max / (1 << min_n2);
	}

	/* work out suitable timings */
	best_diff = freq;

	for (n2=min_n2; n2<=max_n2; n2++) {
		for (n1=min_n1+2; n1<=max_n1+2; n1++) {
			m = (freq * n1 * (1 << n2) + HALF_BASE_FREQ) /
				BASE_FREQ;
			if (m < min_m+2 || m > 127+2)
				continue;
			if ((m * BASE_FREQ >= freq_min * n1) &&
			    (m * BASE_FREQ <= freq_max * n1)) {
				diff = freq * (1 << n2) * n1 - BASE_FREQ * m;
				if (diff < 0)
					diff = -diff;
				if (diff < best_diff) {
					best_diff = diff;
					best_m = m;
					best_n1 = n1;
					best_n2 = n2;
				}
			}
		}
	}

	*ndiv = best_n1 - 2;
	*r = best_n2;
	*mdiv = best_m - 2;
}

static int common_calc_clock(long freq, int min_m, int min_n1, int max_n1,
			     int min_n2, int max_n2, long freq_min,
			     long freq_max, unsigned char *mdiv,
			     unsigned char *ndiv)
{
	long diff, best_diff;
	unsigned int m;
	unsigned char n1, n2;
	unsigned char best_n1 = 16+2, best_n2 = 2, best_m = 125+2;

	best_diff = freq;

	for (n2 = min_n2; n2 <= max_n2; n2++) {
		for (n1 = min_n1+2; n1 <= max_n1+2; n1++) {
			m = (freq * n1 * (1 << n2) + HALF_BASE_FREQ) /
				BASE_FREQ;
			if (m < min_m + 2 || m > 127+2)
				continue;
			if((m * BASE_FREQ >= freq_min * n1) &&
			   (m * BASE_FREQ <= freq_max * n1)) {
				diff = freq * (1 << n2) * n1 - BASE_FREQ * m;
				if(diff < 0)
					diff = -diff;
				if(diff < best_diff) {
					best_diff = diff;
					best_m = m;
					best_n1 = n1;
					best_n2 = n2;
				}
			}
		}
	}

	if(max_n1 == 63)
		*ndiv = (best_n1 - 2) | (best_n2 << 6);
	else
		*ndiv = (best_n1 - 2) | (best_n2 << 5);

	*mdiv = best_m - 2;

	return 0;
}

#ifdef SAVAGEFB_DEBUG
/* This function is used to debug, it prints out the contents of s3 regs */

static void SavagePrintRegs(void)
{
	unsigned char i;
	int vgaCRIndex = 0x3d4;
	int vgaCRReg = 0x3d5;

	printk(KERN_DEBUG "SR    x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE "
	       "xF" );

	for( i = 0; i < 0x70; i++ ) {
		if( !(i % 16) )
			printk(KERN_DEBUG "\nSR%xx ", i >> 4 );
496 497
		vga_out8( 0x3c4, i, par);
		printk(KERN_DEBUG " %02x", vga_in8(0x3c5, par) );
L
Linus Torvalds 已提交
498 499 500 501 502 503 504 505
	}

	printk(KERN_DEBUG "\n\nCR    x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC "
	       "xD xE xF" );

	for( i = 0; i < 0xB7; i++ ) {
		if( !(i % 16) )
			printk(KERN_DEBUG "\nCR%xx ", i >> 4 );
506 507
		vga_out8( vgaCRIndex, i, par);
		printk(KERN_DEBUG " %02x", vga_in8(vgaCRReg, par) );
L
Linus Torvalds 已提交
508 509 510 511 512 513 514 515 516 517 518 519
	}

	printk(KERN_DEBUG "\n\n");
}
#endif

/* --------------------------------------------------------------------- */

static void savage_get_default_par(struct savagefb_par *par)
{
	unsigned char cr3a, cr53, cr66;

520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
	vga_out16 (0x3d4, 0x4838, par);
	vga_out16 (0x3d4, 0xa039, par);
	vga_out16 (0x3c4, 0x0608, par);

	vga_out8 (0x3d4, 0x66, par);
	cr66 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d5, cr66 | 0x80, par);
	vga_out8 (0x3d4, 0x3a, par);
	cr3a = vga_in8 (0x3d5, par);
	vga_out8 (0x3d5, cr3a | 0x80, par);
	vga_out8 (0x3d4, 0x53, par);
	cr53 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d5, cr53 & 0x7f, par);

	vga_out8 (0x3d4, 0x66, par);
	vga_out8 (0x3d5, cr66, par);
	vga_out8 (0x3d4, 0x3a, par);
	vga_out8 (0x3d5, cr3a, par);

	vga_out8 (0x3d4, 0x66, par);
	vga_out8 (0x3d5, cr66, par);
	vga_out8 (0x3d4, 0x3a, par);
	vga_out8 (0x3d5, cr3a, par);
L
Linus Torvalds 已提交
543 544

	/* unlock extended seq regs */
545 546 547
	vga_out8 (0x3c4, 0x08, par);
	par->SR08 = vga_in8 (0x3c5, par);
	vga_out8 (0x3c5, 0x06, par);
L
Linus Torvalds 已提交
548 549

	/* now save all the extended regs we need */
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
	vga_out8 (0x3d4, 0x31, par);
	par->CR31 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x32, par);
	par->CR32 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x34, par);
	par->CR34 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x36, par);
	par->CR36 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x3a, par);
	par->CR3A = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x40, par);
	par->CR40 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x42, par);
	par->CR42 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x45, par);
	par->CR45 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x50, par);
	par->CR50 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x51, par);
	par->CR51 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x53, par);
	par->CR53 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x58, par);
	par->CR58 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x60, par);
	par->CR60 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x66, par);
	par->CR66 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x67, par);
	par->CR67 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x68, par);
	par->CR68 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x69, par);
	par->CR69 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x6f, par);
	par->CR6F = vga_in8 (0x3d5, par);

	vga_out8 (0x3d4, 0x33, par);
	par->CR33 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x86, par);
	par->CR86 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x88, par);
	par->CR88 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x90, par);
	par->CR90 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x91, par);
	par->CR91 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0xb0, par);
	par->CRB0 = vga_in8 (0x3d5, par) | 0x80;
L
Linus Torvalds 已提交
599 600

	/* extended mode timing regs */
601 602 603 604 605 606 607 608 609 610 611 612
	vga_out8 (0x3d4, 0x3b, par);
	par->CR3B = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x3c, par);
	par->CR3C = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x43, par);
	par->CR43 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x5d, par);
	par->CR5D = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x5e, par);
	par->CR5E = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x65, par);
	par->CR65 = vga_in8 (0x3d5, par);
L
Linus Torvalds 已提交
613 614

	/* save seq extended regs for DCLK PLL programming */
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
	vga_out8 (0x3c4, 0x0e, par);
	par->SR0E = vga_in8 (0x3c5, par);
	vga_out8 (0x3c4, 0x0f, par);
	par->SR0F = vga_in8 (0x3c5, par);
	vga_out8 (0x3c4, 0x10, par);
	par->SR10 = vga_in8 (0x3c5, par);
	vga_out8 (0x3c4, 0x11, par);
	par->SR11 = vga_in8 (0x3c5, par);
	vga_out8 (0x3c4, 0x12, par);
	par->SR12 = vga_in8 (0x3c5, par);
	vga_out8 (0x3c4, 0x13, par);
	par->SR13 = vga_in8 (0x3c5, par);
	vga_out8 (0x3c4, 0x29, par);
	par->SR29 = vga_in8 (0x3c5, par);

	vga_out8 (0x3c4, 0x15, par);
	par->SR15 = vga_in8 (0x3c5, par);
	vga_out8 (0x3c4, 0x30, par);
	par->SR30 = vga_in8 (0x3c5, par);
	vga_out8 (0x3c4, 0x18, par);
	par->SR18 = vga_in8 (0x3c5, par);
L
Linus Torvalds 已提交
636 637 638 639 640 641

	/* Save flat panel expansion regsters. */
	if (par->chip == S3_SAVAGE_MX) {
		int i;

		for (i = 0; i < 8; i++) {
642 643
			vga_out8 (0x3c4, 0x54+i, par);
			par->SR54[i] = vga_in8 (0x3c5, par);
L
Linus Torvalds 已提交
644 645 646
		}
	}

647 648 649 650 651 652
	vga_out8 (0x3d4, 0x66, par);
	cr66 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d5, cr66 | 0x80, par);
	vga_out8 (0x3d4, 0x3a, par);
	cr3a = vga_in8 (0x3d5, par);
	vga_out8 (0x3d5, cr3a | 0x80, par);
L
Linus Torvalds 已提交
653 654 655

	/* now save MIU regs */
	if (par->chip != S3_SAVAGE_MX) {
656 657 658 659
		par->MMPR0 = savage_in32(FIFO_CONTROL_REG, par);
		par->MMPR1 = savage_in32(MIU_CONTROL_REG, par);
		par->MMPR2 = savage_in32(STREAMS_TIMEOUT_REG, par);
		par->MMPR3 = savage_in32(MISC_TIMEOUT_REG, par);
L
Linus Torvalds 已提交
660 661
	}

662 663 664 665
	vga_out8 (0x3d4, 0x3a, par);
	vga_out8 (0x3d5, cr3a, par);
	vga_out8 (0x3d4, 0x66, par);
	vga_out8 (0x3d5, cr66, par);
L
Linus Torvalds 已提交
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
}

static void savage_update_var(struct fb_var_screeninfo *var, struct fb_videomode *modedb)
{
	var->xres = var->xres_virtual = modedb->xres;
	var->yres = modedb->yres;
        if (var->yres_virtual < var->yres)
	    var->yres_virtual = var->yres;
        var->xoffset = var->yoffset = 0;
        var->pixclock = modedb->pixclock;
        var->left_margin = modedb->left_margin;
        var->right_margin = modedb->right_margin;
        var->upper_margin = modedb->upper_margin;
        var->lower_margin = modedb->lower_margin;
        var->hsync_len = modedb->hsync_len;
        var->vsync_len = modedb->vsync_len;
        var->sync = modedb->sync;
        var->vmode = modedb->vmode;
}

static int savagefb_check_var (struct fb_var_screeninfo   *var,
			       struct fb_info *info)
{
	struct savagefb_par *par = (struct savagefb_par *)info->par;
	int memlen, vramlen, mode_valid = 0;

	DBG("savagefb_check_var");

	var->transp.offset = 0;
	var->transp.length = 0;
	switch (var->bits_per_pixel) {
	case 8:
		var->red.offset = var->green.offset =
			var->blue.offset = 0;
		var->red.length = var->green.length =
			var->blue.length = var->bits_per_pixel;
		break;
	case 16:
		var->red.offset = 11;
		var->red.length = 5;
		var->green.offset = 5;
		var->green.length = 6;
		var->blue.offset = 0;
		var->blue.length = 5;
		break;
	case 32:
		var->transp.offset = 24;
		var->transp.length = 8;
		var->red.offset = 16;
		var->red.length = 8;
		var->green.offset = 8;
		var->green.length = 8;
		var->blue.offset = 0;
		var->blue.length = 8;
		break;

	default:
		return -EINVAL;
	}

	if (!info->monspecs.hfmax || !info->monspecs.vfmax ||
	    !info->monspecs.dclkmax || !fb_validate_mode(var, info))
		mode_valid = 1;

	/* calculate modeline if supported by monitor */
	if (!mode_valid && info->monspecs.gtf) {
		if (!fb_get_mode(FB_MAXTIMINGS, 0, var, info))
			mode_valid = 1;
	}

	if (!mode_valid) {
		struct fb_videomode *mode;

		mode = fb_find_best_mode(var, &info->modelist);
		if (mode) {
			savage_update_var(var, mode);
			mode_valid = 1;
		}
	}

	if (!mode_valid && info->monspecs.modedb_len)
		return -EINVAL;

	/* Is the mode larger than the LCD panel? */
	if (par->SavagePanelWidth &&
	    (var->xres > par->SavagePanelWidth ||
	     var->yres > par->SavagePanelHeight)) {
		printk (KERN_INFO "Mode (%dx%d) larger than the LCD panel "
			"(%dx%d)\n", var->xres,  var->yres,
			par->SavagePanelWidth,
			par->SavagePanelHeight);
		return -1;
	}

	if (var->yres_virtual < var->yres)
		var->yres_virtual = var->yres;
	if (var->xres_virtual < var->xres)
		var->xres_virtual = var->xres;

	vramlen = info->fix.smem_len;

	memlen = var->xres_virtual * var->bits_per_pixel *
		var->yres_virtual / 8;
	if (memlen > vramlen) {
		var->yres_virtual = vramlen * 8 /
			(var->xres_virtual * var->bits_per_pixel);
		memlen = var->xres_virtual * var->bits_per_pixel *
			var->yres_virtual / 8;
	}

	/* we must round yres/xres down, we already rounded y/xres_virtual up
	   if it was possible. We should return -EINVAL, but I disagree */
	if (var->yres_virtual < var->yres)
		var->yres = var->yres_virtual;
	if (var->xres_virtual < var->xres)
		var->xres = var->xres_virtual;
	if (var->xoffset + var->xres > var->xres_virtual)
		var->xoffset = var->xres_virtual - var->xres;
	if (var->yoffset + var->yres > var->yres_virtual)
		var->yoffset = var->yres_virtual - var->yres;

	return 0;
}


static int savagefb_decode_var (struct fb_var_screeninfo   *var,
				struct savagefb_par        *par)
{
	struct xtimings timings;
	int width, dclk, i, j; /*, refresh; */
	unsigned int m, n, r;
	unsigned char tmp = 0;
	unsigned int pixclock = var->pixclock;

	DBG("savagefb_decode_var");

	memset (&timings, 0, sizeof(timings));

	if (!pixclock) pixclock = 10000;	/* 10ns = 100MHz */
	timings.Clock = 1000000000 / pixclock;
	if (timings.Clock < 1) timings.Clock = 1;
	timings.dblscan = var->vmode & FB_VMODE_DOUBLE;
	timings.interlaced = var->vmode & FB_VMODE_INTERLACED;
	timings.HDisplay = var->xres;
	timings.HSyncStart = timings.HDisplay + var->right_margin;
	timings.HSyncEnd = timings.HSyncStart + var->hsync_len;
	timings.HTotal = timings.HSyncEnd + var->left_margin;
	timings.VDisplay = var->yres;
	timings.VSyncStart = timings.VDisplay + var->lower_margin;
	timings.VSyncEnd = timings.VSyncStart + var->vsync_len;
	timings.VTotal = timings.VSyncEnd + var->upper_margin;
	timings.sync = var->sync;


	par->depth  = var->bits_per_pixel;
	par->vwidth = var->xres_virtual;

	if (var->bits_per_pixel == 16  &&  par->chip == S3_SAVAGE3D) {
		timings.HDisplay *= 2;
		timings.HSyncStart *= 2;
		timings.HSyncEnd *= 2;
		timings.HTotal *= 2;
	}

	/*
	 * This will allocate the datastructure and initialize all of the
	 * generic VGA registers.
	 */
	vgaHWInit (var, par, &timings);

	/* We need to set CR67 whether or not we use the BIOS. */

	dclk = timings.Clock;
	par->CR67 = 0x00;

	switch( var->bits_per_pixel ) {
	case 8:
		if( (par->chip == S3_SAVAGE2000) && (dclk >= 230000) )
			par->CR67 = 0x10;	/* 8bpp, 2 pixels/clock */
		else
			par->CR67 = 0x00;	/* 8bpp, 1 pixel/clock */
		break;
	case 15:
		if ( S3_SAVAGE_MOBILE_SERIES(par->chip) ||
		     ((par->chip == S3_SAVAGE2000) && (dclk >= 230000)) )
			par->CR67 = 0x30;	/* 15bpp, 2 pixel/clock */
		else
			par->CR67 = 0x20;	/* 15bpp, 1 pixels/clock */
		break;
	case 16:
		if( S3_SAVAGE_MOBILE_SERIES(par->chip) ||
		    ((par->chip == S3_SAVAGE2000) && (dclk >= 230000)) )
			par->CR67 = 0x50;	/* 16bpp, 2 pixel/clock */
		else
			par->CR67 = 0x40;	/* 16bpp, 1 pixels/clock */
		break;
	case 24:
		par->CR67 = 0x70;
		break;
	case 32:
		par->CR67 = 0xd0;
		break;
	}

	/*
	 * Either BIOS use is disabled, or we failed to find a suitable
	 * match.  Fall back to traditional register-crunching.
	 */

875 876
	vga_out8 (0x3d4, 0x3a, par);
	tmp = vga_in8 (0x3d5, par);
L
Linus Torvalds 已提交
877 878 879 880 881 882 883 884 885
	if (1 /*FIXME:psav->pci_burst*/)
		par->CR3A = (tmp & 0x7f) | 0x15;
	else
		par->CR3A = tmp | 0x95;

	par->CR53 = 0x00;
	par->CR31 = 0x8c;
	par->CR66 = 0x89;

886 887
	vga_out8 (0x3d4, 0x58, par);
	par->CR58 = vga_in8 (0x3d5, par) & 0x80;
L
Linus Torvalds 已提交
888 889 890 891 892 893
	par->CR58 |= 0x13;

	par->SR15 = 0x03 | 0x80;
	par->SR18 = 0x00;
	par->CR43 = par->CR45 = par->CR65 = 0x00;

894 895
	vga_out8 (0x3d4, 0x40, par);
	par->CR40 = vga_in8 (0x3d5, par) & ~0x01;
L
Linus Torvalds 已提交
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998

	par->MMPR0 = 0x010400;
	par->MMPR1 = 0x00;
	par->MMPR2 = 0x0808;
	par->MMPR3 = 0x08080810;

	SavageCalcClock (dclk, 1, 1, 127, 0, 4, 180000, 360000, &m, &n, &r);
	/* m = 107; n = 4; r = 2; */

	if (par->MCLK <= 0) {
		par->SR10 = 255;
		par->SR11 = 255;
	} else {
		common_calc_clock (par->MCLK, 1, 1, 31, 0, 3, 135000, 270000,
				   &par->SR11, &par->SR10);
		/*      par->SR10 = 80; // MCLK == 286000 */
		/*      par->SR11 = 125; */
	}

	par->SR12 = (r << 6) | (n & 0x3f);
	par->SR13 = m & 0xff;
	par->SR29 = (r & 4) | (m & 0x100) >> 5 | (n & 0x40) >> 2;

	if (var->bits_per_pixel < 24)
		par->MMPR0 -= 0x8000;
	else
		par->MMPR0 -= 0x4000;

	if (timings.interlaced)
		par->CR42 = 0x20;
	else
		par->CR42 = 0x00;

	par->CR34 = 0x10; /* display fifo */

	i = ((((timings.HTotal >> 3) - 5) & 0x100) >> 8) |
		((((timings.HDisplay >> 3) - 1) & 0x100) >> 7) |
		((((timings.HSyncStart >> 3) - 1) & 0x100) >> 6) |
		((timings.HSyncStart & 0x800) >> 7);

	if ((timings.HSyncEnd >> 3) - (timings.HSyncStart >> 3) > 64)
		i |= 0x08;
	if ((timings.HSyncEnd >> 3) - (timings.HSyncStart >> 3) > 32)
		i |= 0x20;

	j = (par->CRTC[0] + ((i & 0x01) << 8) +
	     par->CRTC[4] + ((i & 0x10) << 4) + 1) / 2;

	if (j - (par->CRTC[4] + ((i & 0x10) << 4)) < 4) {
		if (par->CRTC[4] + ((i & 0x10) << 4) + 4 <=
		    par->CRTC[0] + ((i & 0x01) << 8))
			j = par->CRTC[4] + ((i & 0x10) << 4) + 4;
		else
			j = par->CRTC[0] + ((i & 0x01) << 8) + 1;
	}

	par->CR3B = j & 0xff;
	i |= (j & 0x100) >> 2;
	par->CR3C = (par->CRTC[0] + ((i & 0x01) << 8)) / 2;
	par->CR5D = i;
	par->CR5E = (((timings.VTotal - 2) & 0x400) >> 10) |
		(((timings.VDisplay - 1) & 0x400) >> 9) |
		(((timings.VSyncStart) & 0x400) >> 8) |
		(((timings.VSyncStart) & 0x400) >> 6) | 0x40;
	width = (var->xres_virtual * ((var->bits_per_pixel+7) / 8)) >> 3;
	par->CR91 = par->CRTC[19] = 0xff & width;
	par->CR51 = (0x300 & width) >> 4;
	par->CR90 = 0x80 | (width >> 8);
	par->MiscOutReg |= 0x0c;

	/* Set frame buffer description. */

	if (var->bits_per_pixel <= 8)
		par->CR50 = 0;
	else if (var->bits_per_pixel <= 16)
		par->CR50 = 0x10;
	else
		par->CR50 = 0x30;

	if (var->xres_virtual <= 640)
		par->CR50 |= 0x40;
	else if (var->xres_virtual == 800)
		par->CR50 |= 0x80;
	else if (var->xres_virtual == 1024)
		par->CR50 |= 0x00;
	else if (var->xres_virtual == 1152)
		par->CR50 |= 0x01;
	else if (var->xres_virtual == 1280)
		par->CR50 |= 0xc0;
	else if (var->xres_virtual == 1600)
		par->CR50 |= 0x81;
	else
		par->CR50 |= 0xc1;	/* Use GBD */

	if( par->chip == S3_SAVAGE2000 )
		par->CR33 = 0x08;
	else
		par->CR33 = 0x20;

	par->CRTC[0x17] = 0xeb;

	par->CR67 |= 1;

999 1000 1001 1002
	vga_out8(0x3d4, 0x36, par);
	par->CR36 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x68, par);
	par->CR68 = vga_in8 (0x3d5, par);
L
Linus Torvalds 已提交
1003
	par->CR69 = 0;
1004 1005 1006 1007 1008 1009 1010 1011
	vga_out8 (0x3d4, 0x6f, par);
	par->CR6F = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x86, par);
	par->CR86 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d4, 0x88, par);
	par->CR88 = vga_in8 (0x3d5, par) | 0x08;
	vga_out8 (0x3d4, 0xb0, par);
	par->CRB0 = vga_in8 (0x3d5, par) | 0x80;
L
Linus Torvalds 已提交
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039

	return 0;
}

/* --------------------------------------------------------------------- */

/*
 *    Set a single color register. Return != 0 for invalid regno.
 */
static int savagefb_setcolreg(unsigned        regno,
			      unsigned        red,
			      unsigned        green,
			      unsigned        blue,
			      unsigned        transp,
			      struct fb_info *info)
{
	struct savagefb_par *par = (struct savagefb_par *)info->par;

	if (regno >= NR_PALETTE)
		return -EINVAL;

	par->palette[regno].red    = red;
	par->palette[regno].green  = green;
	par->palette[regno].blue   = blue;
	par->palette[regno].transp = transp;

	switch (info->var.bits_per_pixel) {
	case 8:
1040
		vga_out8 (0x3c8, regno, par);
L
Linus Torvalds 已提交
1041

1042 1043 1044
		vga_out8 (0x3c9, red   >> 10, par);
		vga_out8 (0x3c9, green >> 10, par);
		vga_out8 (0x3c9, blue  >> 10, par);
L
Linus Torvalds 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
		break;

	case 16:
		if (regno < 16)
			((u32 *)info->pseudo_palette)[regno] =
				((red   & 0xf800)      ) |
				((green & 0xfc00) >>  5) |
				((blue  & 0xf800) >> 11);
		break;

	case 24:
		if (regno < 16)
			((u32 *)info->pseudo_palette)[regno] =
				((red    & 0xff00) <<  8) |
				((green  & 0xff00)      ) |
				((blue   & 0xff00) >>  8);
		break;
	case 32:
		if (regno < 16)
			((u32 *)info->pseudo_palette)[regno] =
				((transp & 0xff00) << 16) |
				((red    & 0xff00) <<  8) |
				((green  & 0xff00)      ) |
				((blue   & 0xff00) >>  8);
		break;

	default:
		return 1;
	}

	return 0;
}

static void savagefb_set_par_int (struct savagefb_par  *par)
{
	unsigned char tmp, cr3a, cr66, cr67;

	DBG ("savagefb_set_par_int");

	par->SavageWaitIdle (par);

1086
	vga_out8 (0x3c2, 0x23, par);
L
Linus Torvalds 已提交
1087

1088 1089 1090
	vga_out16 (0x3d4, 0x4838, par);
	vga_out16 (0x3d4, 0xa539, par);
	vga_out16 (0x3c4, 0x0608, par);
L
Linus Torvalds 已提交
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100

	vgaHWProtect (par, 1);

	/*
	 * Some Savage/MX and /IX systems go nuts when trying to exit the
	 * server after WindowMaker has displayed a gradient background.  I
	 * haven't been able to find what causes it, but a non-destructive
	 * switch to mode 3 here seems to eliminate the issue.
	 */

1101 1102 1103 1104
	VerticalRetraceWait(par);
	vga_out8 (0x3d4, 0x67, par);
	cr67 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d5, cr67/*par->CR67*/ & ~0x0c, par); /* no STREAMS yet */
L
Linus Torvalds 已提交
1105

1106 1107 1108 1109
	vga_out8 (0x3d4, 0x23, par);
	vga_out8 (0x3d5, 0x00, par);
	vga_out8 (0x3d4, 0x26, par);
	vga_out8 (0x3d5, 0x00, par);
L
Linus Torvalds 已提交
1110 1111

	/* restore extended regs */
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
	vga_out8 (0x3d4, 0x66, par);
	vga_out8 (0x3d5, par->CR66, par);
	vga_out8 (0x3d4, 0x3a, par);
	vga_out8 (0x3d5, par->CR3A, par);
	vga_out8 (0x3d4, 0x31, par);
	vga_out8 (0x3d5, par->CR31, par);
	vga_out8 (0x3d4, 0x32, par);
	vga_out8 (0x3d5, par->CR32, par);
	vga_out8 (0x3d4, 0x58, par);
	vga_out8 (0x3d5, par->CR58, par);
	vga_out8 (0x3d4, 0x53, par);
	vga_out8 (0x3d5, par->CR53 & 0x7f, par);

	vga_out16 (0x3c4, 0x0608, par);
L
Linus Torvalds 已提交
1126 1127 1128

	/* Restore DCLK registers. */

1129 1130 1131 1132 1133 1134 1135 1136
	vga_out8 (0x3c4, 0x0e, par);
	vga_out8 (0x3c5, par->SR0E, par);
	vga_out8 (0x3c4, 0x0f, par);
	vga_out8 (0x3c5, par->SR0F, par);
	vga_out8 (0x3c4, 0x29, par);
	vga_out8 (0x3c5, par->SR29, par);
	vga_out8 (0x3c4, 0x15, par);
	vga_out8 (0x3c5, par->SR15, par);
L
Linus Torvalds 已提交
1137 1138 1139 1140 1141 1142

	/* Restore flat panel expansion regsters. */
	if( par->chip == S3_SAVAGE_MX ) {
		int i;

		for( i = 0; i < 8; i++ ) {
1143 1144
			vga_out8 (0x3c4, 0x54+i, par);
			vga_out8 (0x3c5, par->SR54[i], par);
L
Linus Torvalds 已提交
1145 1146 1147 1148 1149 1150
		}
	}

	vgaHWRestore (par);

	/* extended mode timing registers */
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
	vga_out8 (0x3d4, 0x53, par);
	vga_out8 (0x3d5, par->CR53, par);
	vga_out8 (0x3d4, 0x5d, par);
	vga_out8 (0x3d5, par->CR5D, par);
	vga_out8 (0x3d4, 0x5e, par);
	vga_out8 (0x3d5, par->CR5E, par);
	vga_out8 (0x3d4, 0x3b, par);
	vga_out8 (0x3d5, par->CR3B, par);
	vga_out8 (0x3d4, 0x3c, par);
	vga_out8 (0x3d5, par->CR3C, par);
	vga_out8 (0x3d4, 0x43, par);
	vga_out8 (0x3d5, par->CR43, par);
	vga_out8 (0x3d4, 0x65, par);
	vga_out8 (0x3d5, par->CR65, par);
L
Linus Torvalds 已提交
1165 1166

	/* restore the desired video mode with cr67 */
1167
	vga_out8 (0x3d4, 0x67, par);
L
Linus Torvalds 已提交
1168
	/* following part not present in X11 driver */
1169 1170
	cr67 = vga_in8 (0x3d5, par) & 0xf;
	vga_out8 (0x3d5, 0x50 | cr67, par);
L
Linus Torvalds 已提交
1171
	udelay (10000);
1172
	vga_out8 (0x3d4, 0x67, par);
L
Linus Torvalds 已提交
1173
	/* end of part */
1174
	vga_out8 (0x3d5, par->CR67 & ~0x0c, par);
L
Linus Torvalds 已提交
1175 1176

	/* other mode timing and extended regs */
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
	vga_out8 (0x3d4, 0x34, par);
	vga_out8 (0x3d5, par->CR34, par);
	vga_out8 (0x3d4, 0x40, par);
	vga_out8 (0x3d5, par->CR40, par);
	vga_out8 (0x3d4, 0x42, par);
	vga_out8 (0x3d5, par->CR42, par);
	vga_out8 (0x3d4, 0x45, par);
	vga_out8 (0x3d5, par->CR45, par);
	vga_out8 (0x3d4, 0x50, par);
	vga_out8 (0x3d5, par->CR50, par);
	vga_out8 (0x3d4, 0x51, par);
	vga_out8 (0x3d5, par->CR51, par);
L
Linus Torvalds 已提交
1189 1190

	/* memory timings */
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
	vga_out8 (0x3d4, 0x36, par);
	vga_out8 (0x3d5, par->CR36, par);
	vga_out8 (0x3d4, 0x60, par);
	vga_out8 (0x3d5, par->CR60, par);
	vga_out8 (0x3d4, 0x68, par);
	vga_out8 (0x3d5, par->CR68, par);
	vga_out8 (0x3d4, 0x69, par);
	vga_out8 (0x3d5, par->CR69, par);
	vga_out8 (0x3d4, 0x6f, par);
	vga_out8 (0x3d5, par->CR6F, par);

	vga_out8 (0x3d4, 0x33, par);
	vga_out8 (0x3d5, par->CR33, par);
	vga_out8 (0x3d4, 0x86, par);
	vga_out8 (0x3d5, par->CR86, par);
	vga_out8 (0x3d4, 0x88, par);
	vga_out8 (0x3d5, par->CR88, par);
	vga_out8 (0x3d4, 0x90, par);
	vga_out8 (0x3d5, par->CR90, par);
	vga_out8 (0x3d4, 0x91, par);
	vga_out8 (0x3d5, par->CR91, par);
L
Linus Torvalds 已提交
1212 1213

	if (par->chip == S3_SAVAGE4) {
1214 1215
		vga_out8 (0x3d4, 0xb0, par);
		vga_out8 (0x3d5, par->CRB0, par);
L
Linus Torvalds 已提交
1216 1217
	}

1218 1219
	vga_out8 (0x3d4, 0x32, par);
	vga_out8 (0x3d5, par->CR32, par);
L
Linus Torvalds 已提交
1220 1221

	/* unlock extended seq regs */
1222 1223
	vga_out8 (0x3c4, 0x08, par);
	vga_out8 (0x3c5, 0x06, par);
L
Linus Torvalds 已提交
1224 1225 1226 1227 1228

	/* Restore extended sequencer regs for MCLK. SR10 == 255 indicates
	 * that we should leave the default SR10 and SR11 values there.
	 */
	if (par->SR10 != 255) {
1229 1230 1231 1232
		vga_out8 (0x3c4, 0x10, par);
		vga_out8 (0x3c5, par->SR10, par);
		vga_out8 (0x3c4, 0x11, par);
		vga_out8 (0x3c5, par->SR11, par);
L
Linus Torvalds 已提交
1233 1234 1235
	}

	/* restore extended seq regs for dclk */
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
	vga_out8 (0x3c4, 0x0e, par);
	vga_out8 (0x3c5, par->SR0E, par);
	vga_out8 (0x3c4, 0x0f, par);
	vga_out8 (0x3c5, par->SR0F, par);
	vga_out8 (0x3c4, 0x12, par);
	vga_out8 (0x3c5, par->SR12, par);
	vga_out8 (0x3c4, 0x13, par);
	vga_out8 (0x3c5, par->SR13, par);
	vga_out8 (0x3c4, 0x29, par);
	vga_out8 (0x3c5, par->SR29, par);

	vga_out8 (0x3c4, 0x18, par);
	vga_out8 (0x3c5, par->SR18, par);
L
Linus Torvalds 已提交
1249 1250

	/* load new m, n pll values for dclk & mclk */
1251 1252
	vga_out8 (0x3c4, 0x15, par);
	tmp = vga_in8 (0x3c5, par) & ~0x21;
L
Linus Torvalds 已提交
1253

1254 1255 1256 1257
	vga_out8 (0x3c5, tmp | 0x03, par);
	vga_out8 (0x3c5, tmp | 0x23, par);
	vga_out8 (0x3c5, tmp | 0x03, par);
	vga_out8 (0x3c5, par->SR15, par);
L
Linus Torvalds 已提交
1258 1259
	udelay (100);

1260 1261 1262 1263
	vga_out8 (0x3c4, 0x30, par);
	vga_out8 (0x3c5, par->SR30, par);
	vga_out8 (0x3c4, 0x08, par);
	vga_out8 (0x3c5, par->SR08, par);
L
Linus Torvalds 已提交
1264 1265

	/* now write out cr67 in full, possibly starting STREAMS */
1266 1267 1268
	VerticalRetraceWait(par);
	vga_out8 (0x3d4, 0x67, par);
	vga_out8 (0x3d5, par->CR67, par);
L
Linus Torvalds 已提交
1269

1270 1271 1272 1273 1274 1275
	vga_out8 (0x3d4, 0x66, par);
	cr66 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d5, cr66 | 0x80, par);
	vga_out8 (0x3d4, 0x3a, par);
	cr3a = vga_in8 (0x3d5, par);
	vga_out8 (0x3d5, cr3a | 0x80, par);
L
Linus Torvalds 已提交
1276 1277

	if (par->chip != S3_SAVAGE_MX) {
1278 1279
		VerticalRetraceWait(par);
		savage_out32 (FIFO_CONTROL_REG, par->MMPR0, par);
L
Linus Torvalds 已提交
1280
		par->SavageWaitIdle (par);
1281
		savage_out32 (MIU_CONTROL_REG, par->MMPR1, par);
L
Linus Torvalds 已提交
1282
		par->SavageWaitIdle (par);
1283
		savage_out32 (STREAMS_TIMEOUT_REG, par->MMPR2, par);
L
Linus Torvalds 已提交
1284
		par->SavageWaitIdle (par);
1285
		savage_out32 (MISC_TIMEOUT_REG, par->MMPR3, par);
L
Linus Torvalds 已提交
1286 1287
	}

1288 1289 1290 1291
	vga_out8 (0x3d4, 0x66, par);
	vga_out8 (0x3d5, cr66, par);
	vga_out8 (0x3d4, 0x3a, par);
	vga_out8 (0x3d5, cr3a, par);
L
Linus Torvalds 已提交
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305

	SavageSetup2DEngine (par);
	vgaHWProtect (par, 0);
}

static void savagefb_update_start (struct savagefb_par      *par,
				   struct fb_var_screeninfo *var)
{
	int base;

	base = ((var->yoffset * var->xres_virtual + (var->xoffset & ~1))
		* ((var->bits_per_pixel+7) / 8)) >> 2;

	/* now program the start address registers */
1306 1307 1308 1309
	vga_out16(0x3d4, (base & 0x00ff00) | 0x0c, par);
	vga_out16(0x3d4, ((base & 0x00ff) << 8) | 0x0d, par);
	vga_out8 (0x3d4, 0x69, par);
	vga_out8 (0x3d5, (base & 0x7f0000) >> 16, par);
L
Linus Torvalds 已提交
1310 1311 1312 1313 1314 1315 1316 1317
}


static void savagefb_set_fix(struct fb_info *info)
{
	info->fix.line_length = info->var.xres_virtual *
		info->var.bits_per_pixel / 8;

1318
	if (info->var.bits_per_pixel == 8) {
L
Linus Torvalds 已提交
1319
		info->fix.visual      = FB_VISUAL_PSEUDOCOLOR;
1320 1321
		info->fix.xpanstep    = 4;
	} else {
L
Linus Torvalds 已提交
1322
		info->fix.visual      = FB_VISUAL_TRUECOLOR;
1323 1324 1325
		info->fix.xpanstep    = 2;
	}

L
Linus Torvalds 已提交
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
}

#if defined(CONFIG_FB_SAVAGE_ACCEL)
static void savagefb_set_clip(struct fb_info *info)
{
    struct savagefb_par *par = (struct savagefb_par *)info->par;
    int cmd;

    cmd = BCI_CMD_NOP | BCI_CMD_CLIP_NEW;
    par->bci_ptr = 0;
    par->SavageWaitFifo(par,3);
    BCI_SEND(cmd);
    BCI_SEND(BCI_CLIP_TL(0, 0));
    BCI_SEND(BCI_CLIP_BR(0xfff, 0xfff));
}
#endif

static int savagefb_set_par (struct fb_info *info)
{
	struct savagefb_par *par = (struct savagefb_par *)info->par;
	struct fb_var_screeninfo *var = &info->var;
	int err;

	DBG("savagefb_set_par");
	err = savagefb_decode_var (var, par);
	if (err)
		return err;

	if (par->dacSpeedBpp <= 0) {
		if (var->bits_per_pixel > 24)
			par->dacSpeedBpp = par->clock[3];
		else if (var->bits_per_pixel >= 24)
			par->dacSpeedBpp = par->clock[2];
		else if ((var->bits_per_pixel > 8) && (var->bits_per_pixel < 24))
			par->dacSpeedBpp = par->clock[1];
		else if (var->bits_per_pixel <= 8)
			par->dacSpeedBpp = par->clock[0];
	}

	/* Set ramdac limits */
	par->maxClock = par->dacSpeedBpp;
	par->minClock = 10000;

	savagefb_set_par_int (par);
	fb_set_cmap (&info->cmap, info);
	savagefb_set_fix(info);
	savagefb_set_clip(info);

	SavagePrintRegs();
	return 0;
}

/*
 *    Pan or Wrap the Display
 */
static int savagefb_pan_display (struct fb_var_screeninfo *var,
				 struct fb_info           *info)
{
	struct savagefb_par *par = (struct savagefb_par *)info->par;
	u_int y_bottom;

	y_bottom = var->yoffset;

	if (!(var->vmode & FB_VMODE_YWRAP))
		y_bottom += var->yres;

	if (var->xoffset > (var->xres_virtual - var->xres))
		return -EINVAL;
	if (y_bottom > info->var.yres_virtual)
		return -EINVAL;

	savagefb_update_start (par, var);

	info->var.xoffset = var->xoffset;
	info->var.yoffset = var->yoffset;

	if (var->vmode & FB_VMODE_YWRAP)
		info->var.vmode |= FB_VMODE_YWRAP;
	else
		info->var.vmode &= ~FB_VMODE_YWRAP;

	return 0;
}

1410 1411 1412 1413 1414 1415
static int savagefb_blank(int blank, struct fb_info *info)
{
	struct savagefb_par *par = info->par;
	u8 sr8 = 0, srd = 0;

	if (par->display_type == DISP_CRT) {
1416 1417
		vga_out8(0x3c4, 0x08, par);
		sr8 = vga_in8(0x3c5, par);
1418
		sr8 |= 0x06;
1419 1420 1421
		vga_out8(0x3c5, sr8, par);
		vga_out8(0x3c4, 0x0d, par);
		srd = vga_in8(0x3c5, par);
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
		srd &= 0x03;

		switch (blank) {
		case FB_BLANK_UNBLANK:
		case FB_BLANK_NORMAL:
			break;
		case FB_BLANK_VSYNC_SUSPEND:
			srd |= 0x10;
			break;
		case FB_BLANK_HSYNC_SUSPEND:
			srd |= 0x40;
			break;
		case FB_BLANK_POWERDOWN:
			srd |= 0x50;
			break;
		}

1439 1440
		vga_out8(0x3c4, 0x0d, par);
		vga_out8(0x3c5, srd, par);
1441 1442 1443 1444 1445 1446 1447
	}

	if (par->display_type == DISP_LCD ||
	    par->display_type == DISP_DFP) {
		switch(blank) {
		case FB_BLANK_UNBLANK:
		case FB_BLANK_NORMAL:
1448 1449
			vga_out8(0x3c4, 0x31, par); /* SR31 bit 4 - FP enable */
			vga_out8(0x3c5, vga_in8(0x3c5, par) | 0x10, par);
1450 1451 1452 1453
			break;
		case FB_BLANK_VSYNC_SUSPEND:
		case FB_BLANK_HSYNC_SUSPEND:
		case FB_BLANK_POWERDOWN:
1454 1455
			vga_out8(0x3c4, 0x31, par); /* SR31 bit 4 - FP enable */
			vga_out8(0x3c5, vga_in8(0x3c5, par) & ~0x10, par);
1456 1457 1458 1459 1460 1461
			break;
		}
	}

	return (blank == FB_BLANK_NORMAL) ? 1 : 0;
}
L
Linus Torvalds 已提交
1462 1463 1464 1465 1466 1467 1468

static struct fb_ops savagefb_ops = {
	.owner          = THIS_MODULE,
	.fb_check_var   = savagefb_check_var,
	.fb_set_par     = savagefb_set_par,
	.fb_setcolreg   = savagefb_setcolreg,
	.fb_pan_display = savagefb_pan_display,
1469
	.fb_blank       = savagefb_blank,
L
Linus Torvalds 已提交
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
#if defined(CONFIG_FB_SAVAGE_ACCEL)
	.fb_fillrect    = savagefb_fillrect,
	.fb_copyarea    = savagefb_copyarea,
	.fb_imageblit   = savagefb_imageblit,
	.fb_sync        = savagefb_sync,
#else
	.fb_fillrect    = cfb_fillrect,
	.fb_copyarea    = cfb_copyarea,
	.fb_imageblit   = cfb_imageblit,
#endif
};

/* --------------------------------------------------------------------- */

static struct fb_var_screeninfo __devinitdata savagefb_var800x600x8 = {
	.accel_flags =	FB_ACCELF_TEXT,
	.xres =		800,
	.yres =		600,
	.xres_virtual =  800,
	.yres_virtual =  600,
	.bits_per_pixel = 8,
	.pixclock =	25000,
	.left_margin =	88,
	.right_margin =	40,
	.upper_margin =	23,
	.lower_margin =	1,
	.hsync_len =	128,
	.vsync_len =	4,
	.sync =		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
	.vmode =	FB_VMODE_NONINTERLACED
};

static void savage_enable_mmio (struct savagefb_par *par)
{
	unsigned char val;

	DBG ("savage_enable_mmio\n");

1508 1509 1510 1511
	val = vga_in8 (0x3c3, par);
	vga_out8 (0x3c3, val | 0x01, par);
	val = vga_in8 (0x3cc, par);
	vga_out8 (0x3c2, val | 0x01, par);
L
Linus Torvalds 已提交
1512 1513

	if (par->chip >= S3_SAVAGE4) {
1514 1515 1516
		vga_out8 (0x3d4, 0x40, par);
		val = vga_in8 (0x3d5, par);
		vga_out8 (0x3d5, val | 1, par);
L
Linus Torvalds 已提交
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
	}
}


static void savage_disable_mmio (struct savagefb_par *par)
{
	unsigned char val;

	DBG ("savage_disable_mmio\n");

	if(par->chip >= S3_SAVAGE4 ) {
1528 1529 1530
		vga_out8 (0x3d4, 0x40, par);
		val = vga_in8 (0x3d5, par);
		vga_out8 (0x3d5, val | 1, par);
L
Linus Torvalds 已提交
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
	}
}


static int __devinit savage_map_mmio (struct fb_info *info)
{
	struct savagefb_par *par = (struct savagefb_par *)info->par;
	DBG ("savage_map_mmio");

	if (S3_SAVAGE3D_SERIES (par->chip))
		par->mmio.pbase = pci_resource_start (par->pcidev, 0) +
			SAVAGE_NEWMMIO_REGBASE_S3;
	else
		par->mmio.pbase = pci_resource_start (par->pcidev, 0) +
			SAVAGE_NEWMMIO_REGBASE_S4;

	par->mmio.len = SAVAGE_NEWMMIO_REGSIZE;

	par->mmio.vbase = ioremap (par->mmio.pbase, par->mmio.len);
	if (!par->mmio.vbase) {
		printk ("savagefb: unable to map memory mapped IO\n");
		return -ENOMEM;
	} else
		printk (KERN_INFO "savagefb: mapped io at %p\n",
			par->mmio.vbase);

	info->fix.mmio_start = par->mmio.pbase;
	info->fix.mmio_len   = par->mmio.len;

A
Al Viro 已提交
1560
	par->bci_base = (u32 __iomem *)(par->mmio.vbase + BCI_BUFFER_OFFSET);
L
Linus Torvalds 已提交
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
	par->bci_ptr  = 0;

	savage_enable_mmio (par);

	return 0;
}

static void __devinit savage_unmap_mmio (struct fb_info *info)
{
	struct savagefb_par *par = (struct savagefb_par *)info->par;
	DBG ("savage_unmap_mmio");

	savage_disable_mmio(par);

	if (par->mmio.vbase) {
A
Al Viro 已提交
1576
		iounmap(par->mmio.vbase);
L
Linus Torvalds 已提交
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
		par->mmio.vbase = NULL;
	}
}

static int __devinit savage_map_video (struct fb_info *info,
				       int video_len)
{
	struct savagefb_par *par = (struct savagefb_par *)info->par;
	int resource;

	DBG("savage_map_video");

	if (S3_SAVAGE3D_SERIES (par->chip))
		resource = 0;
	else
		resource = 1;

	par->video.pbase = pci_resource_start (par->pcidev, resource);
	par->video.len   = video_len;
	par->video.vbase = ioremap (par->video.pbase, par->video.len);

	if (!par->video.vbase) {
		printk ("savagefb: unable to map screen memory\n");
		return -ENOMEM;
	} else
		printk (KERN_INFO "savagefb: mapped framebuffer at %p, "
			"pbase == %x\n", par->video.vbase, par->video.pbase);

	info->fix.smem_start = par->video.pbase;
	info->fix.smem_len   = par->video.len - par->cob_size;
	info->screen_base    = par->video.vbase;

#ifdef CONFIG_MTRR
	par->video.mtrr = mtrr_add (par->video.pbase, video_len,
				     MTRR_TYPE_WRCOMB, 1);
#endif

	/* Clear framebuffer, it's all white in memory after boot */
A
Al Viro 已提交
1615
	memset_io (par->video.vbase, 0, par->video.len);
L
Linus Torvalds 已提交
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644

	return 0;
}

static void __devinit savage_unmap_video (struct fb_info *info)
{
	struct savagefb_par *par = (struct savagefb_par *)info->par;

	DBG("savage_unmap_video");

	if (par->video.vbase) {
#ifdef CONFIG_MTRR
		mtrr_del (par->video.mtrr, par->video.pbase, par->video.len);
#endif

		iounmap (par->video.vbase);
		par->video.vbase = NULL;
		info->screen_base = NULL;
	}
}

static int __devinit savage_init_hw (struct savagefb_par *par)
{
	unsigned char config1, m, n, n1, n2, sr8, cr3f, cr66 = 0, tmp;

	static unsigned char RamSavage3D[] = { 8, 4, 4, 2 };
	static unsigned char RamSavage4[] =  { 2, 4, 8, 12, 16, 32, 64, 32 };
	static unsigned char RamSavageMX[] = { 2, 8, 4, 16, 8, 16, 4, 16 };
	static unsigned char RamSavageNB[] = { 0, 2, 4, 8, 16, 32, 2, 2 };
1645
	int videoRam, videoRambytes, dvi;
L
Linus Torvalds 已提交
1646 1647 1648 1649

	DBG("savage_init_hw");

	/* unprotect CRTC[0-7] */
1650 1651 1652
	vga_out8(0x3d4, 0x11, par);
	tmp = vga_in8(0x3d5, par);
	vga_out8(0x3d5, tmp & 0x7f, par);
L
Linus Torvalds 已提交
1653 1654

	/* unlock extended regs */
1655 1656 1657
	vga_out16(0x3d4, 0x4838, par);
	vga_out16(0x3d4, 0xa039, par);
	vga_out16(0x3c4, 0x0608, par);
L
Linus Torvalds 已提交
1658

1659 1660 1661
	vga_out8(0x3d4, 0x40, par);
	tmp = vga_in8(0x3d5, par);
	vga_out8(0x3d5, tmp & ~0x01, par);
L
Linus Torvalds 已提交
1662 1663

	/* unlock sys regs */
1664 1665
	vga_out8(0x3d4, 0x38, par);
	vga_out8(0x3d5, 0x48, par);
L
Linus Torvalds 已提交
1666 1667

	/* Unlock system registers. */
1668
	vga_out16(0x3d4, 0x4838, par);
L
Linus Torvalds 已提交
1669 1670 1671

	/* Next go on to detect amount of installed ram */

1672 1673
	vga_out8(0x3d4, 0x36, par);            /* for register CR36 (CONFG_REG1), */
	config1 = vga_in8(0x3d5, par);    /* get amount of vram installed */
L
Linus Torvalds 已提交
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688

	/* Compute the amount of video memory and offscreen memory. */

	switch  (par->chip) {
	case S3_SAVAGE3D:
		videoRam = RamSavage3D[ (config1 & 0xC0) >> 6 ] * 1024;
		break;

	case S3_SAVAGE4:
		/*
		 * The Savage4 has one ugly special case to consider.  On
		 * systems with 4 banks of 2Mx32 SDRAM, the BIOS says 4MB
		 * when it really means 8MB.  Why do it the same when you
		 * can do it different...
		 */
1689 1690
		vga_out8(0x3d4, 0x68, par);	/* memory control 1 */
		if( (vga_in8(0x3d5, par) & 0xC0) == (0x01 << 6) )
L
Linus Torvalds 已提交
1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
			RamSavage4[1] = 8;

		/*FALLTHROUGH*/

	case S3_SAVAGE2000:
		videoRam = RamSavage4[ (config1 & 0xE0) >> 5 ] * 1024;
		break;

	case S3_SAVAGE_MX:
	case S3_SUPERSAVAGE:
		videoRam = RamSavageMX[ (config1 & 0x0E) >> 1 ] * 1024;
		break;

	case S3_PROSAVAGE:
		videoRam = RamSavageNB[ (config1 & 0xE0) >> 5 ] * 1024;
		break;

	default:
		/* How did we get here? */
		videoRam = 0;
		break;
	}

	videoRambytes = videoRam * 1024;

	printk (KERN_INFO "savagefb: probed videoram:  %dk\n", videoRam);

	/* reset graphics engine to avoid memory corruption */
1719 1720 1721
	vga_out8 (0x3d4, 0x66, par);
	cr66 = vga_in8 (0x3d5, par);
	vga_out8 (0x3d5, cr66 | 0x02, par);
L
Linus Torvalds 已提交
1722 1723
	udelay (10000);

1724 1725
	vga_out8 (0x3d4, 0x66, par);
	vga_out8 (0x3d5, cr66 & ~0x02, par);	/* clear reset flag */
L
Linus Torvalds 已提交
1726 1727 1728 1729 1730 1731 1732
	udelay (10000);


	/*
	 * reset memory interface, 3D engine, AGP master, PCI master,
	 * master engine unit, motion compensation/LPB
	 */
1733 1734 1735
	vga_out8 (0x3d4, 0x3f, par);
	cr3f = vga_in8 (0x3d5, par);
	vga_out8 (0x3d5, cr3f | 0x08, par);
L
Linus Torvalds 已提交
1736 1737
	udelay (10000);

1738 1739
	vga_out8 (0x3d4, 0x3f, par);
	vga_out8 (0x3d5, cr3f & ~0x08, par);	/* clear reset flags */
L
Linus Torvalds 已提交
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
	udelay (10000);

	/* Savage ramdac speeds */
	par->numClocks = 4;
	par->clock[0] = 250000;
	par->clock[1] = 250000;
	par->clock[2] = 220000;
	par->clock[3] = 220000;

	/* detect current mclk */
1750 1751 1752 1753 1754 1755 1756 1757 1758
	vga_out8(0x3c4, 0x08, par);
	sr8 = vga_in8(0x3c5, par);
	vga_out8(0x3c5, 0x06, par);
	vga_out8(0x3c4, 0x10, par);
	n = vga_in8(0x3c5, par);
	vga_out8(0x3c4, 0x11, par);
	m = vga_in8(0x3c5, par);
	vga_out8(0x3c4, 0x08, par);
	vga_out8(0x3c5, sr8, par);
L
Linus Torvalds 已提交
1759 1760 1761 1762 1763 1764 1765
	m &= 0x7f;
	n1 = n & 0x1f;
	n2 = (n >> 5) & 0x03;
	par->MCLK = ((1431818 * (m+2)) / (n1+2) / (1 << n2) + 50) / 100;
	printk (KERN_INFO "savagefb: Detected current MCLK value of %d kHz\n",
		par->MCLK);

1766 1767 1768 1769 1770 1771
	/* check for DVI/flat panel */
	dvi = 0;

	if (par->chip == S3_SAVAGE4) {
		unsigned char sr30 = 0x00;

1772
		vga_out8(0x3c4, 0x30, par);
1773
		/* clear bit 1 */
1774 1775
		vga_out8(0x3c5, vga_in8(0x3c5, par) & ~0x02, par);
		sr30 = vga_in8(0x3c5, par);
1776 1777 1778 1779 1780 1781
		if (sr30 & 0x02 /*0x04 */) {
			dvi = 1;
			printk("savagefb: Digital Flat Panel Detected\n");
		}
	}

1782
	if (S3_SAVAGE_MOBILE_SERIES(par->chip) && !par->crtonly)
1783 1784 1785 1786 1787 1788
		par->display_type = DISP_LCD;
	else if (dvi || (par->chip == S3_SAVAGE4 && par->dvi))
		par->display_type = DISP_DFP;
	else
		par->display_type = DISP_CRT;

L
Linus Torvalds 已提交
1789 1790
	/* Check LCD panel parrmation */

1791
	if (par->display_type == DISP_LCD) {
1792
		unsigned char cr6b = VGArCR( 0x6b, par);
L
Linus Torvalds 已提交
1793

1794 1795 1796 1797
		int panelX = (VGArSEQ (0x61, par) +
			      ((VGArSEQ (0x66, par) & 0x02) << 7) + 1) * 8;
		int panelY = (VGArSEQ (0x69, par) +
			      ((VGArSEQ (0x6e, par) & 0x70) << 4) + 1);
L
Linus Torvalds 已提交
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818

		char * sTechnology = "Unknown";

		/* OK, I admit it.  I don't know how to limit the max dot clock
		 * for LCD panels of various sizes.  I thought I copied the
		 * formula from the BIOS, but many users have parrmed me of
		 * my folly.
		 *
		 * Instead, I'll abandon any attempt to automatically limit the
		 * clock, and add an LCDClock option to XF86Config.  Some day,
		 * I should come back to this.
		 */

		enum ACTIVE_DISPLAYS { /* These are the bits in CR6B */
			ActiveCRT = 0x01,
			ActiveLCD = 0x02,
			ActiveTV = 0x04,
			ActiveCRT2 = 0x20,
			ActiveDUO = 0x80
		};

1819
		if ((VGArSEQ (0x39, par) & 0x03) == 0) {
L
Linus Torvalds 已提交
1820
			sTechnology = "TFT";
1821
		} else if ((VGArSEQ (0x30, par) & 0x01) == 0) {
L
Linus Torvalds 已提交
1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842
			sTechnology = "DSTN";
		} else 	{
			sTechnology = "STN";
		}

		printk (KERN_INFO "savagefb: %dx%d %s LCD panel detected %s\n",
			panelX, panelY, sTechnology,
			cr6b & ActiveLCD ? "and active" : "but not active");

		if( cr6b & ActiveLCD ) 	{
			/*
			 * If the LCD is active and panel expansion is enabled,
			 * we probably want to kill the HW cursor.
			 */

			printk (KERN_INFO "savagefb: Limiting video mode to "
				"%dx%d\n", panelX, panelY );

			par->SavagePanelWidth = panelX;
			par->SavagePanelHeight = panelY;

1843 1844
		} else
			par->display_type = DISP_CRT;
L
Linus Torvalds 已提交
1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928
	}

	savage_get_default_par (par);

	if( S3_SAVAGE4_SERIES(par->chip) ) {
		/*
		 * The Savage4 and ProSavage have COB coherency bugs which
		 * render the buffer useless.  We disable it.
		 */
		par->cob_index = 2;
		par->cob_size = 0x8000 << par->cob_index;
		par->cob_offset = videoRambytes;
	} else {
		/* We use 128kB for the COB on all chips. */

		par->cob_index  = 7;
		par->cob_size   = 0x400 << par->cob_index;
		par->cob_offset = videoRambytes - par->cob_size;
	}

	return videoRambytes;
}

static int __devinit savage_init_fb_info (struct fb_info *info,
					  struct pci_dev *dev,
					  const struct pci_device_id *id)
{
	struct savagefb_par *par = (struct savagefb_par *)info->par;
	int err = 0;

	par->pcidev  = dev;

	info->fix.type	   = FB_TYPE_PACKED_PIXELS;
	info->fix.type_aux	   = 0;
	info->fix.ypanstep	   = 1;
	info->fix.ywrapstep   = 0;
	info->fix.accel       = id->driver_data;

	switch (info->fix.accel) {
	case FB_ACCEL_SUPERSAVAGE:
		par->chip = S3_SUPERSAVAGE;
		snprintf (info->fix.id, 16, "SuperSavage");
		break;
	case FB_ACCEL_SAVAGE4:
		par->chip = S3_SAVAGE4;
		snprintf (info->fix.id, 16, "Savage4");
		break;
	case FB_ACCEL_SAVAGE3D:
		par->chip = S3_SAVAGE3D;
		snprintf (info->fix.id, 16, "Savage3D");
		break;
	case FB_ACCEL_SAVAGE3D_MV:
		par->chip = S3_SAVAGE3D;
		snprintf (info->fix.id, 16, "Savage3D-MV");
		break;
	case FB_ACCEL_SAVAGE2000:
		par->chip = S3_SAVAGE2000;
		snprintf (info->fix.id, 16, "Savage2000");
		break;
	case FB_ACCEL_SAVAGE_MX_MV:
		par->chip = S3_SAVAGE_MX;
		snprintf (info->fix.id, 16, "Savage/MX-MV");
		break;
	case FB_ACCEL_SAVAGE_MX:
		par->chip = S3_SAVAGE_MX;
		snprintf (info->fix.id, 16, "Savage/MX");
		break;
	case FB_ACCEL_SAVAGE_IX_MV:
		par->chip = S3_SAVAGE_MX;
		snprintf (info->fix.id, 16, "Savage/IX-MV");
		break;
	case FB_ACCEL_SAVAGE_IX:
		par->chip = S3_SAVAGE_MX;
		snprintf (info->fix.id, 16, "Savage/IX");
		break;
	case FB_ACCEL_PROSAVAGE_PM:
		par->chip = S3_PROSAVAGE;
		snprintf (info->fix.id, 16, "ProSavagePM");
		break;
	case FB_ACCEL_PROSAVAGE_KM:
		par->chip = S3_PROSAVAGE;
		snprintf (info->fix.id, 16, "ProSavageKM");
		break;
	case FB_ACCEL_S3TWISTER_P:
1929
		par->chip = S3_PROSAVAGE;
L
Linus Torvalds 已提交
1930 1931 1932
		snprintf (info->fix.id, 16, "TwisterP");
		break;
	case FB_ACCEL_S3TWISTER_K:
1933
		par->chip = S3_PROSAVAGE;
L
Linus Torvalds 已提交
1934 1935 1936
		snprintf (info->fix.id, 16, "TwisterK");
		break;
	case FB_ACCEL_PROSAVAGE_DDR:
1937
		par->chip = S3_PROSAVAGE;
L
Linus Torvalds 已提交
1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
		snprintf (info->fix.id, 16, "ProSavageDDR");
		break;
	case FB_ACCEL_PROSAVAGE_DDRK:
		par->chip = S3_PROSAVAGE;
		snprintf (info->fix.id, 16, "ProSavage8");
		break;
	}

	if (S3_SAVAGE3D_SERIES(par->chip)) {
		par->SavageWaitIdle = savage3D_waitidle;
		par->SavageWaitFifo = savage3D_waitfifo;
	} else if (S3_SAVAGE4_SERIES(par->chip) ||
		   S3_SUPERSAVAGE == par->chip) {
		par->SavageWaitIdle = savage4_waitidle;
		par->SavageWaitFifo = savage4_waitfifo;
	} else {
		par->SavageWaitIdle = savage2000_waitidle;
		par->SavageWaitFifo = savage2000_waitfifo;
	}

	info->var.nonstd      = 0;
	info->var.activate    = FB_ACTIVATE_NOW;
	info->var.width       = -1;
	info->var.height      = -1;
	info->var.accel_flags = 0;

	info->fbops          = &savagefb_ops;
	info->flags          = FBINFO_DEFAULT |
		               FBINFO_HWACCEL_YPAN |
		               FBINFO_HWACCEL_XPAN;

	info->pseudo_palette = par->pseudo_palette;

#if defined(CONFIG_FB_SAVAGE_ACCEL)
	/* FIFO size + padding for commands */
	info->pixmap.addr = kmalloc(8*1024, GFP_KERNEL);

	err = -ENOMEM;
	if (info->pixmap.addr) {
		memset(info->pixmap.addr, 0, 8*1024);
		info->pixmap.size = 8*1024;
		info->pixmap.scan_align = 4;
		info->pixmap.buf_align = 4;
1981
		info->pixmap.access_align = 32;
L
Linus Torvalds 已提交
1982

1983 1984
		err = fb_alloc_cmap (&info->cmap, NR_PALETTE, 0);
		if (!err)
L
Linus Torvalds 已提交
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
		info->flags |= FBINFO_HWACCEL_COPYAREA |
	                       FBINFO_HWACCEL_FILLRECT |
		               FBINFO_HWACCEL_IMAGEBLIT;
	}
#endif
	return err;
}

/* --------------------------------------------------------------------- */

static int __devinit savagefb_probe (struct pci_dev* dev,
				     const struct pci_device_id* id)
{
	struct fb_info *info;
	struct savagefb_par *par;
	u_int h_sync, v_sync;
	int err, lpitch;
	int video_len;

	DBG("savagefb_probe");
	SavagePrintRegs();

	info = framebuffer_alloc(sizeof(struct savagefb_par), &dev->dev);
	if (!info)
		return -ENOMEM;
	par = info->par;
	err = pci_enable_device(dev);
	if (err)
		goto failed_enable;

2015
	if ((err = pci_request_regions(dev, "savagefb"))) {
L
Linus Torvalds 已提交
2016 2017 2018 2019 2020 2021
		printk(KERN_ERR "cannot request PCI regions\n");
		goto failed_enable;
	}

	err = -ENOMEM;

2022
	if ((err = savage_init_fb_info(info, dev, id)))
L
Linus Torvalds 已提交
2023 2024 2025 2026 2027 2028 2029
		goto failed_init;

	err = savage_map_mmio(info);
	if (err)
		goto failed_mmio;

	video_len = savage_init_hw(par);
2030
	/* FIXME: cant be negative */
L
Linus Torvalds 已提交
2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042
	if (video_len < 0) {
		err = video_len;
		goto failed_mmio;
	}

	err = savage_map_video(info, video_len);
	if (err)
		goto failed_video;

	INIT_LIST_HEAD(&info->modelist);
#if defined(CONFIG_FB_SAVAGE_I2C)
	savagefb_create_i2c_busses(info);
2043 2044
	savagefb_probe_i2c_connector(info, &par->edid);
	kfree(par->edid);
L
Linus Torvalds 已提交
2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056
	fb_edid_to_monspecs(par->edid, &info->monspecs);
	fb_videomode_to_modelist(info->monspecs.modedb,
				 info->monspecs.modedb_len,
				 &info->modelist);
#endif
	info->var = savagefb_var800x600x8;

	if (mode_option) {
		fb_find_mode(&info->var, info, mode_option,
			     info->monspecs.modedb, info->monspecs.modedb_len,
			     NULL, 8);
	} else if (info->monspecs.modedb != NULL) {
2057
		struct fb_videomode *modedb;
L
Linus Torvalds 已提交
2058

2059 2060 2061
		modedb = fb_find_best_display(&info->monspecs,
					      &info->modelist);
		savage_update_var(&info->var, modedb);
L
Linus Torvalds 已提交
2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174
	}

	/* maximize virtual vertical length */
	lpitch = info->var.xres_virtual*((info->var.bits_per_pixel + 7) >> 3);
	info->var.yres_virtual = info->fix.smem_len/lpitch;

	if (info->var.yres_virtual < info->var.yres)
		goto failed;

#if defined(CONFIG_FB_SAVAGE_ACCEL)
	/*
	 * The clipping coordinates are masked with 0xFFF, so limit our
	 * virtual resolutions to these sizes.
	 */
	if (info->var.yres_virtual > 0x1000)
		info->var.yres_virtual = 0x1000;

	if (info->var.xres_virtual > 0x1000)
		info->var.xres_virtual = 0x1000;
#endif
	savagefb_check_var(&info->var, info);
	savagefb_set_fix(info);

	/*
	 * Calculate the hsync and vsync frequencies.  Note that
	 * we split the 1e12 constant up so that we can preserve
	 * the precision and fit the results into 32-bit registers.
	 *  (1953125000 * 512 = 1e12)
	 */
	h_sync = 1953125000 / info->var.pixclock;
	h_sync = h_sync * 512 / (info->var.xres + info->var.left_margin +
				 info->var.right_margin +
				 info->var.hsync_len);
	v_sync = h_sync / (info->var.yres + info->var.upper_margin +
			   info->var.lower_margin + info->var.vsync_len);

	printk(KERN_INFO "savagefb v" SAVAGEFB_VERSION ": "
	       "%dkB VRAM, using %dx%d, %d.%03dkHz, %dHz\n",
	       info->fix.smem_len >> 10,
	       info->var.xres, info->var.yres,
	       h_sync / 1000, h_sync % 1000, v_sync);


	fb_destroy_modedb(info->monspecs.modedb);
	info->monspecs.modedb = NULL;

	err = register_framebuffer (info);
	if (err < 0)
		goto failed;

	printk (KERN_INFO "fb: S3 %s frame buffer device\n",
		info->fix.id);

	/*
	 * Our driver data
	 */
	pci_set_drvdata(dev, info);

	return 0;

 failed:
#ifdef CONFIG_FB_SAVAGE_I2C
	savagefb_delete_i2c_busses(info);
#endif
	fb_alloc_cmap (&info->cmap, 0, 0);
	savage_unmap_video(info);
 failed_video:
	savage_unmap_mmio (info);
 failed_mmio:
	kfree(info->pixmap.addr);
 failed_init:
	pci_release_regions(dev);
 failed_enable:
	framebuffer_release(info);

	return err;
}

static void __devexit savagefb_remove (struct pci_dev *dev)
{
	struct fb_info *info =
		(struct fb_info *)pci_get_drvdata(dev);

	DBG("savagefb_remove");

	if (info) {
		/*
		 * If unregister_framebuffer fails, then
		 * we will be leaving hooks that could cause
		 * oopsen laying around.
		 */
		if (unregister_framebuffer (info))
			printk (KERN_WARNING "savagefb: danger danger! "
				"Oopsen imminent!\n");

#ifdef CONFIG_FB_SAVAGE_I2C
		savagefb_delete_i2c_busses(info);
#endif
		fb_alloc_cmap (&info->cmap, 0, 0);
		savage_unmap_video (info);
		savage_unmap_mmio (info);
		kfree(info->pixmap.addr);
		pci_release_regions(dev);
		framebuffer_release(info);

		/*
		 * Ensure that the driver data is no longer
		 * valid.
		 */
		pci_set_drvdata(dev, NULL);
	}
}

2175
static int savagefb_suspend (struct pci_dev* dev, pm_message_t state)
L
Linus Torvalds 已提交
2176 2177 2178 2179 2180 2181 2182
{
	struct fb_info *info =
		(struct fb_info *)pci_get_drvdata(dev);
	struct savagefb_par *par = (struct savagefb_par *)info->par;

	DBG("savagefb_suspend");

2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194

	par->pm_state = state.event;

	/*
	 * For PM_EVENT_FREEZE, do not power down so the console
	 * can remain active.
	 */
	if (state.event == PM_EVENT_FREEZE) {
		dev->dev.power.power_state = state;
		return 0;
	}

L
Linus Torvalds 已提交
2195
	acquire_console_sem();
2196
	fb_set_suspend(info, 1);
L
Linus Torvalds 已提交
2197

2198 2199 2200 2201 2202 2203
	if (info->fbops->fb_sync)
		info->fbops->fb_sync(info);

	savagefb_blank(FB_BLANK_POWERDOWN, info);
	savage_disable_mmio(par);
	pci_save_state(dev);
L
Linus Torvalds 已提交
2204
	pci_disable_device(dev);
2205
	pci_set_power_state(dev, pci_choose_state(dev, state));
2206
	release_console_sem();
L
Linus Torvalds 已提交
2207 2208 2209 2210 2211 2212 2213 2214 2215

	return 0;
}

static int savagefb_resume (struct pci_dev* dev)
{
	struct fb_info *info =
		(struct fb_info *)pci_get_drvdata(dev);
	struct savagefb_par *par = (struct savagefb_par *)info->par;
2216
	int cur_state = par->pm_state;
L
Linus Torvalds 已提交
2217 2218 2219

	DBG("savage_resume");

2220
	par->pm_state = PM_EVENT_ON;
L
Linus Torvalds 已提交
2221

2222 2223 2224 2225 2226 2227 2228 2229
	/*
	 * The adapter was not powered down coming back from a
	 * PM_EVENT_FREEZE.
	 */
	if (cur_state == PM_EVENT_FREEZE) {
		pci_set_power_state(dev, PCI_D0);
		return 0;
	}
L
Linus Torvalds 已提交
2230 2231 2232

	acquire_console_sem();

2233 2234 2235 2236 2237 2238 2239
	pci_set_power_state(dev, PCI_D0);
	pci_restore_state(dev);

	if(pci_enable_device(dev))
		DBG("err");

	pci_set_master(dev);
L
Linus Torvalds 已提交
2240 2241 2242
	savage_enable_mmio(par);
	savage_init_hw(par);
	savagefb_set_par (info);
2243
	savagefb_blank(FB_BLANK_UNBLANK, info);
L
Linus Torvalds 已提交
2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376
	fb_set_suspend (info, 0);
	release_console_sem();

	return 0;
}


static struct pci_device_id savagefb_devices[] __devinitdata = {
	{PCI_VENDOR_ID_S3, PCI_CHIP_SUPSAV_MX128,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SUPERSAVAGE},

	{PCI_VENDOR_ID_S3, PCI_CHIP_SUPSAV_MX64,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SUPERSAVAGE},

	{PCI_VENDOR_ID_S3, PCI_CHIP_SUPSAV_MX64C,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SUPERSAVAGE},

	{PCI_VENDOR_ID_S3, PCI_CHIP_SUPSAV_IX128SDR,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SUPERSAVAGE},

	{PCI_VENDOR_ID_S3, PCI_CHIP_SUPSAV_IX128DDR,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SUPERSAVAGE},

	{PCI_VENDOR_ID_S3, PCI_CHIP_SUPSAV_IX64SDR,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SUPERSAVAGE},

	{PCI_VENDOR_ID_S3, PCI_CHIP_SUPSAV_IX64DDR,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SUPERSAVAGE},

	{PCI_VENDOR_ID_S3, PCI_CHIP_SUPSAV_IXCSDR,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SUPERSAVAGE},

	{PCI_VENDOR_ID_S3, PCI_CHIP_SUPSAV_IXCDDR,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SUPERSAVAGE},

	{PCI_VENDOR_ID_S3, PCI_CHIP_SAVAGE4,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SAVAGE4},

	{PCI_VENDOR_ID_S3, PCI_CHIP_SAVAGE3D,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SAVAGE3D},

	{PCI_VENDOR_ID_S3, PCI_CHIP_SAVAGE3D_MV,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SAVAGE3D_MV},

	{PCI_VENDOR_ID_S3, PCI_CHIP_SAVAGE2000,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SAVAGE2000},

	{PCI_VENDOR_ID_S3, PCI_CHIP_SAVAGE_MX_MV,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SAVAGE_MX_MV},

	{PCI_VENDOR_ID_S3, PCI_CHIP_SAVAGE_MX,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SAVAGE_MX},

	{PCI_VENDOR_ID_S3, PCI_CHIP_SAVAGE_IX_MV,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SAVAGE_IX_MV},

	{PCI_VENDOR_ID_S3, PCI_CHIP_SAVAGE_IX,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SAVAGE_IX},

	{PCI_VENDOR_ID_S3, PCI_CHIP_PROSAVAGE_PM,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_PROSAVAGE_PM},

	{PCI_VENDOR_ID_S3, PCI_CHIP_PROSAVAGE_KM,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_PROSAVAGE_KM},

	{PCI_VENDOR_ID_S3, PCI_CHIP_S3TWISTER_P,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_S3TWISTER_P},

	{PCI_VENDOR_ID_S3, PCI_CHIP_S3TWISTER_K,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_S3TWISTER_K},

	{PCI_VENDOR_ID_S3, PCI_CHIP_PROSAVAGE_DDR,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_PROSAVAGE_DDR},

	{PCI_VENDOR_ID_S3, PCI_CHIP_PROSAVAGE_DDRK,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_PROSAVAGE_DDRK},

	{0, 0, 0, 0, 0, 0, 0}
};

MODULE_DEVICE_TABLE(pci, savagefb_devices);

static struct pci_driver savagefb_driver = {
	.name =     "savagefb",
	.id_table = savagefb_devices,
	.probe =    savagefb_probe,
	.suspend =  savagefb_suspend,
	.resume =   savagefb_resume,
	.remove =   __devexit_p(savagefb_remove)
};

/* **************************** exit-time only **************************** */

static void __exit savage_done (void)
{
	DBG("savage_done");
	pci_unregister_driver (&savagefb_driver);
}


/* ************************* init in-kernel code ************************** */

static int __init savagefb_setup(char *options)
{
#ifndef MODULE
	char *this_opt;

	if (!options || !*options)
		return 0;

	while ((this_opt = strsep(&options, ",")) != NULL) {
		mode_option = this_opt;
	}
#endif /* !MODULE */
	return 0;
}

static int __init savagefb_init(void)
{
	char *option;

	DBG("savagefb_init");

	if (fb_get_options("savagefb", &option))
		return -ENODEV;

	savagefb_setup(option);
	return pci_register_driver (&savagefb_driver);

}

module_init(savagefb_init);
module_exit(savage_done);
2377 2378 2379

module_param(mode_option, charp, 0);
MODULE_PARM_DESC(mode_option, "Specify initial video mode");