sis_main.c 192.8 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
T
Thomas Winischhofer 已提交
2 3 4
 * SiS 300/540/630[S]/730[S],
 * SiS 315[E|PRO]/550/[M]65x/[M]66x[F|M|G]X/[M]74x[GX]/330/[M]76x[GX],
 * XGI V3XT/V5/V8, Z7
L
Linus Torvalds 已提交
5 6
 * frame buffer driver for Linux kernels >= 2.4.14 and >=2.6.3
 *
T
Thomas Winischhofer 已提交
7
 * Copyright (C) 2001-2005 Thomas Winischhofer, Vienna, Austria.
L
Linus Torvalds 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the named License,
 * or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
 *
T
Thomas Winischhofer 已提交
23
 * Author:	Thomas Winischhofer <thomas@winischhofer.net>
L
Linus Torvalds 已提交
24 25 26
 *
 * Author of (practically wiped) code base:
 *		SiS (www.sis.com)
T
Thomas Winischhofer 已提交
27
 *		Copyright (C) 1999 Silicon Integrated Systems, Inc.
L
Linus Torvalds 已提交
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
 *
 * See http://www.winischhofer.net/ for more information and updates
 *
 * Originally based on the VBE 2.0 compliant graphic boards framebuffer driver,
 * which is (c) 1998 Gerd Knorr <kraxel@goldbach.in-berlin.de>
 *
 */

#include <linux/config.h>
#include <linux/version.h>
#include <linux/module.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
#include <linux/moduleparam.h>
#endif
#include <linux/kernel.h>
#include <linux/smp_lock.h>
#include <linux/spinlock.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/tty.h>
#include <linux/slab.h>
#include <linux/fb.h>
#include <linux/selection.h>
#include <linux/ioport.h>
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/vmalloc.h>
T
Thomas Winischhofer 已提交
56
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
L
Linus Torvalds 已提交
57
#include <linux/vt_kern.h>
T
Thomas Winischhofer 已提交
58
#endif
L
Linus Torvalds 已提交
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 92 93 94 95 96
#include <linux/capability.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#ifdef CONFIG_MTRR
#include <asm/mtrr.h>
#endif

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
#include <video/fbcon.h>
#include <video/fbcon-cfb8.h>
#include <video/fbcon-cfb16.h>
#include <video/fbcon-cfb24.h>
#include <video/fbcon-cfb32.h>
#endif

#include "sis.h"
#include "sis_main.h"

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,3)
#error "This version of sisfb requires at least 2.6.3"
#endif
#endif

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
#ifdef FBCON_HAS_CFB8
extern struct display_switch fbcon_sis8;
#endif
#ifdef FBCON_HAS_CFB16
extern struct display_switch fbcon_sis16;
#endif
#ifdef FBCON_HAS_CFB32
extern struct display_switch fbcon_sis32;
#endif
#endif

T
Thomas Winischhofer 已提交
97 98 99
static void sisfb_handle_command(struct sis_video_info *ivideo,
				 struct sisfb_cmd *sisfb_command);

L
Linus Torvalds 已提交
100 101 102 103 104
/* ------------------ Internal helper routines ----------------- */

static void __init
sisfb_setdefaultparms(void)
{
T
Thomas Winischhofer 已提交
105 106 107 108 109 110 111
	sisfb_off		= 0;
	sisfb_parm_mem		= 0;
	sisfb_accel		= -1;
	sisfb_ypan		= -1;
	sisfb_max		= -1;
	sisfb_userom		= -1;
	sisfb_useoem		= -1;
L
Linus Torvalds 已提交
112 113 114
#ifdef MODULE
	/* Module: "None" for 2.4, default mode for 2.5+ */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
T
Thomas Winischhofer 已提交
115
	sisfb_mode_idx		= -1;
L
Linus Torvalds 已提交
116
#else
T
Thomas Winischhofer 已提交
117
	sisfb_mode_idx		= MODE_INDEX_NONE;
L
Linus Torvalds 已提交
118 119 120
#endif
#else
	/* Static: Default mode */
T
Thomas Winischhofer 已提交
121 122 123 124 125 126 127 128 129 130
	sisfb_mode_idx		= -1;
#endif
	sisfb_parm_rate		= -1;
	sisfb_crt1off		= 0;
	sisfb_forcecrt1		= -1;
	sisfb_crt2type		= -1;
	sisfb_crt2flags		= 0;
	sisfb_pdc		= 0xff;
	sisfb_pdca		= 0xff;
	sisfb_scalelcd		= -1;
L
Linus Torvalds 已提交
131
	sisfb_specialtiming 	= CUT_NONE;
T
Thomas Winischhofer 已提交
132 133 134 135 136 137 138 139
	sisfb_lvdshl		= -1;
	sisfb_dstn		= 0;
	sisfb_fstn		= 0;
	sisfb_tvplug		= -1;
	sisfb_tvstd		= -1;
	sisfb_tvxposoffset	= 0;
	sisfb_tvyposoffset	= 0;
	sisfb_nocrt2rate	= 0;
L
Linus Torvalds 已提交
140
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
T
Thomas Winischhofer 已提交
141 142
	sisfb_inverse		= 0;
	sisfb_fontname[0]	= 0;
L
Linus Torvalds 已提交
143 144
#endif
#if !defined(__i386__) && !defined(__x86_64__)
T
Thomas Winischhofer 已提交
145 146
	sisfb_resetcard		= 0;
	sisfb_videoram		= 0;
L
Linus Torvalds 已提交
147 148 149
#endif
}

T
Thomas Winischhofer 已提交
150 151
/* ------------- Parameter parsing -------------- */

L
Linus Torvalds 已提交
152 153 154 155 156
static void __devinit
sisfb_search_vesamode(unsigned int vesamode, BOOLEAN quiet)
{
	int i = 0, j = 0;

T
Thomas Winischhofer 已提交
157
	/* We don't know the hardware specs yet and there is no ivideo */
L
Linus Torvalds 已提交
158 159 160 161 162

	if(vesamode == 0) {
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
		sisfb_mode_idx = MODE_INDEX_NONE;
#else
T
Thomas Winischhofer 已提交
163 164 165
		if(!quiet)
			printk(KERN_ERR "sisfb: Invalid mode. Using default.\n");

L
Linus Torvalds 已提交
166 167 168 169 170 171 172 173 174 175
		sisfb_mode_idx = DEFAULT_MODE;
#endif
		return;
	}

	vesamode &= 0x1dff;  /* Clean VESA mode number from other flags */

	while(sisbios_mode[i++].mode_no[0] != 0) {
		if( (sisbios_mode[i-1].vesa_mode_no_1 == vesamode) ||
		    (sisbios_mode[i-1].vesa_mode_no_2 == vesamode) ) {
T
Thomas Winischhofer 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188
			if(sisfb_fstn) {
				if(sisbios_mode[i-1].mode_no[1] == 0x50 ||
				   sisbios_mode[i-1].mode_no[1] == 0x56 ||
				   sisbios_mode[i-1].mode_no[1] == 0x53)
					continue;
			} else {
				if(sisbios_mode[i-1].mode_no[1] == 0x5a ||
				   sisbios_mode[i-1].mode_no[1] == 0x5b)
					continue;
			}
			sisfb_mode_idx = i - 1;
			j = 1;
			break;
L
Linus Torvalds 已提交
189 190
		}
	}
T
Thomas Winischhofer 已提交
191 192
	if((!j) && !quiet)
		printk(KERN_ERR "sisfb: Invalid VESA mode 0x%x'\n", vesamode);
L
Linus Torvalds 已提交
193 194
}

T
Thomas Winischhofer 已提交
195
static void __devinit
L
Linus Torvalds 已提交
196 197 198
sisfb_search_mode(char *name, BOOLEAN quiet)
{
	unsigned int j = 0, xres = 0, yres = 0, depth = 0, rate = 0;
T
Thomas Winischhofer 已提交
199
	int i = 0;
L
Linus Torvalds 已提交
200 201 202
	char strbuf[16], strbuf1[20];
	char *nameptr = name;

T
Thomas Winischhofer 已提交
203
	/* We don't know the hardware specs yet and there is no ivideo */
L
Linus Torvalds 已提交
204 205

	if(name == NULL) {
T
Thomas Winischhofer 已提交
206 207 208 209 210
		if(!quiet)
			printk(KERN_ERR "sisfb: Internal error, using default mode.\n");

		sisfb_mode_idx = DEFAULT_MODE;
		return;
L
Linus Torvalds 已提交
211 212 213
	}

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
T
Thomas Winischhofer 已提交
214 215 216 217 218 219
	if(!strnicmp(name, sisbios_mode[MODE_INDEX_NONE].name, strlen(name))) {
		if(!quiet)
			printk(KERN_ERR "sisfb: Mode 'none' not supported anymore. Using default.\n");

		sisfb_mode_idx = DEFAULT_MODE;
		return;
L
Linus Torvalds 已提交
220 221 222
	}
#endif
	if(strlen(name) <= 19) {
T
Thomas Winischhofer 已提交
223 224 225 226
		strcpy(strbuf1, name);
		for(i = 0; i < strlen(strbuf1); i++) {
			if(strbuf1[i] < '0' || strbuf1[i] > '9') strbuf1[i] = ' ';
		}
L
Linus Torvalds 已提交
227

T
Thomas Winischhofer 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
		/* This does some fuzzy mode naming detection */
		if(sscanf(strbuf1, "%u %u %u %u", &xres, &yres, &depth, &rate) == 4) {
			if((rate <= 32) || (depth > 32)) {
				j = rate; rate = depth; depth = j;
			}
			sprintf(strbuf, "%ux%ux%u", xres, yres, depth);
			nameptr = strbuf;
			sisfb_parm_rate = rate;
		} else if(sscanf(strbuf1, "%u %u %u", &xres, &yres, &depth) == 3) {
			sprintf(strbuf, "%ux%ux%u", xres, yres, depth);
			nameptr = strbuf;
		} else {
			xres = 0;
			if((sscanf(strbuf1, "%u %u", &xres, &yres) == 2) && (xres != 0)) {
				sprintf(strbuf, "%ux%ux8", xres, yres);
				nameptr = strbuf;
			} else {
				sisfb_search_vesamode(simple_strtoul(name, NULL, 0), quiet);
				return;
			}
		}
L
Linus Torvalds 已提交
249 250 251 252 253
	}

	i = 0; j = 0;
	while(sisbios_mode[i].mode_no[0] != 0) {
		if(!strnicmp(nameptr, sisbios_mode[i++].name, strlen(nameptr))) {
T
Thomas Winischhofer 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
			if(sisfb_fstn) {
				if(sisbios_mode[i-1].mode_no[1] == 0x50 ||
				   sisbios_mode[i-1].mode_no[1] == 0x56 ||
				   sisbios_mode[i-1].mode_no[1] == 0x53)
					continue;
			} else {
				if(sisbios_mode[i-1].mode_no[1] == 0x5a ||
				   sisbios_mode[i-1].mode_no[1] == 0x5b)
					continue;
			}
			sisfb_mode_idx = i - 1;
			j = 1;
			break;
		}
	}

	if((!j) && !quiet)
		printk(KERN_ERR "sisfb: Invalid mode '%s'\n", nameptr);
L
Linus Torvalds 已提交
272 273 274 275 276 277 278
}

#ifndef MODULE
static void __devinit
sisfb_get_vga_mode_from_kernel(void)
{
#if (defined(__i386__) || defined(__x86_64__)) && defined(CONFIG_VIDEO_SELECT)
T
Thomas Winischhofer 已提交
279
	char mymode[32];
L
Linus Torvalds 已提交
280 281 282 283 284 285 286 287
	int  mydepth = screen_info.lfb_depth;

	if(screen_info.orig_video_isVGA != VIDEO_TYPE_VLFB) return;

	if( (screen_info.lfb_width >= 320) && (screen_info.lfb_width <= 2048) &&
	    (screen_info.lfb_height >= 200) && (screen_info.lfb_height <= 1536) &&
	    (mydepth >= 8) && (mydepth <= 32) ) {

T
Thomas Winischhofer 已提交
288
		if(mydepth == 24) mydepth = 32;
L
Linus Torvalds 已提交
289

T
Thomas Winischhofer 已提交
290 291
		sprintf(mymode, "%ux%ux%u", screen_info.lfb_width,
					screen_info.lfb_height,
L
Linus Torvalds 已提交
292 293
					mydepth);

T
Thomas Winischhofer 已提交
294 295 296
		printk(KERN_DEBUG
			"sisfb: Using vga mode %s pre-set by kernel as default\n",
			mymode);
L
Linus Torvalds 已提交
297

T
Thomas Winischhofer 已提交
298
		sisfb_search_mode(mymode, TRUE);
L
Linus Torvalds 已提交
299 300 301 302 303 304 305 306 307 308 309
	}
#endif
	return;
}
#endif

static void __init
sisfb_search_crt2type(const char *name)
{
	int i = 0;

T
Thomas Winischhofer 已提交
310
	/* We don't know the hardware specs yet and there is no ivideo */
L
Linus Torvalds 已提交
311 312 313 314

	if(name == NULL) return;

	while(sis_crt2type[i].type_no != -1) {
T
Thomas Winischhofer 已提交
315 316 317 318 319 320 321
		if(!strnicmp(name, sis_crt2type[i].name, strlen(sis_crt2type[i].name))) {
			sisfb_crt2type = sis_crt2type[i].type_no;
			sisfb_tvplug = sis_crt2type[i].tvplug_no;
			sisfb_crt2flags = sis_crt2type[i].flags;
			break;
		}
		i++;
L
Linus Torvalds 已提交
322 323 324 325 326
	}

	sisfb_dstn = (sisfb_crt2flags & FL_550_DSTN) ? 1 : 0;
	sisfb_fstn = (sisfb_crt2flags & FL_550_FSTN) ? 1 : 0;

T
Thomas Winischhofer 已提交
327
	if(sisfb_crt2type < 0)
L
Linus Torvalds 已提交
328 329 330 331 332 333 334 335
		printk(KERN_ERR "sisfb: Invalid CRT2 type: %s\n", name);
}

static void __init
sisfb_search_tvstd(const char *name)
{
	int i = 0;

T
Thomas Winischhofer 已提交
336
	/* We don't know the hardware specs yet and there is no ivideo */
L
Linus Torvalds 已提交
337

T
Thomas Winischhofer 已提交
338 339
	if(name == NULL)
		return;
L
Linus Torvalds 已提交
340 341

	while(sis_tvtype[i].type_no != -1) {
T
Thomas Winischhofer 已提交
342 343 344 345 346
		if(!strnicmp(name, sis_tvtype[i].name, strlen(sis_tvtype[i].name))) {
			sisfb_tvstd = sis_tvtype[i].type_no;
			break;
		}
		i++;
L
Linus Torvalds 已提交
347 348 349 350 351 352 353 354 355
	}
}

static void __init
sisfb_search_specialtiming(const char *name)
{
	int i = 0;
	BOOLEAN found = FALSE;

T
Thomas Winischhofer 已提交
356
	/* We don't know the hardware specs yet and there is no ivideo */
L
Linus Torvalds 已提交
357

T
Thomas Winischhofer 已提交
358 359
	if(name == NULL)
		return;
L
Linus Torvalds 已提交
360 361

	if(!strnicmp(name, "none", 4)) {
T
Thomas Winischhofer 已提交
362
		sisfb_specialtiming = CUT_FORCENONE;
L
Linus Torvalds 已提交
363 364
		printk(KERN_DEBUG "sisfb: Special timing disabled\n");
	} else {
T
Thomas Winischhofer 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377 378 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
		while(mycustomttable[i].chipID != 0) {
			if(!strnicmp(name,mycustomttable[i].optionName,
			   strlen(mycustomttable[i].optionName))) {
				sisfb_specialtiming = mycustomttable[i].SpecialID;
				found = TRUE;
				printk(KERN_INFO "sisfb: Special timing for %s %s forced (\"%s\")\n",
					mycustomttable[i].vendorName,
					mycustomttable[i].cardName,
					mycustomttable[i].optionName);
				break;
			}
			i++;
		}
		if(!found) {
			printk(KERN_WARNING "sisfb: Invalid SpecialTiming parameter, valid are:");
			printk(KERN_WARNING "\t\"none\" (to disable special timings)\n");
			i = 0;
			while(mycustomttable[i].chipID != 0) {
				printk(KERN_WARNING "\t\"%s\" (for %s %s)\n",
					mycustomttable[i].optionName,
					mycustomttable[i].vendorName,
					mycustomttable[i].cardName);
				i++;
			}
		}
	}
}

/* ----------- Various detection routines ----------- */

static void __devinit
sisfb_detect_custom_timing(struct sis_video_info *ivideo)
{
	unsigned char *biosver = NULL;
	unsigned char *biosdate = NULL;
	BOOLEAN footprint;
	u32 chksum = 0;
	int i, j;

	if(ivideo->SiS_Pr.UseROM) {
		biosver = ivideo->SiS_Pr.VirtualRomBase + 0x06;
		biosdate = ivideo->SiS_Pr.VirtualRomBase + 0x2c;
		for(i = 0; i < 32768; i++)
			chksum += ivideo->SiS_Pr.VirtualRomBase[i];
	}

	i = 0;
	do {
		if( (mycustomttable[i].chipID == ivideo->chip)			&&
		    ((!strlen(mycustomttable[i].biosversion)) ||
		     (ivideo->SiS_Pr.UseROM &&
		      (!strncmp(mycustomttable[i].biosversion, biosver,
				strlen(mycustomttable[i].biosversion)))))	&&
		    ((!strlen(mycustomttable[i].biosdate)) ||
		     (ivideo->SiS_Pr.UseROM &&
		      (!strncmp(mycustomttable[i].biosdate, biosdate,
				strlen(mycustomttable[i].biosdate)))))		&&
		    ((!mycustomttable[i].bioschksum) ||
		     (ivideo->SiS_Pr.UseROM &&
		      (mycustomttable[i].bioschksum == chksum)))		&&
		    (mycustomttable[i].pcisubsysvendor == ivideo->subsysvendor) &&
		    (mycustomttable[i].pcisubsyscard == ivideo->subsysdevice) ) {
			footprint = TRUE;
			for(j = 0; j < 5; j++) {
				if(mycustomttable[i].biosFootprintAddr[j]) {
					if(ivideo->SiS_Pr.UseROM) {
						if(ivideo->SiS_Pr.VirtualRomBase[mycustomttable[i].biosFootprintAddr[j]] !=
							mycustomttable[i].biosFootprintData[j]) {
							footprint = FALSE;
						}
					} else
						footprint = FALSE;
				}
			}
			if(footprint) {
				ivideo->SiS_Pr.SiS_CustomT = mycustomttable[i].SpecialID;
				printk(KERN_DEBUG "sisfb: Identified [%s %s], special timing applies\n",
					mycustomttable[i].vendorName,
				mycustomttable[i].cardName);
				printk(KERN_DEBUG "sisfb: [specialtiming parameter name: %s]\n",
					mycustomttable[i].optionName);
				break;
			}
		}
		i++;
	} while(mycustomttable[i].chipID);
L
Linus Torvalds 已提交
451 452 453 454 455 456 457 458 459 460 461 462
}

static BOOLEAN __devinit
sisfb_interpret_edid(struct sisfb_monitor *monitor, u8 *buffer)
{
	int i, j, xres, yres, refresh, index;
	u32 emodes;

	if(buffer[0] != 0x00 || buffer[1] != 0xff ||
	   buffer[2] != 0xff || buffer[3] != 0xff ||
	   buffer[4] != 0xff || buffer[5] != 0xff ||
	   buffer[6] != 0xff || buffer[7] != 0x00) {
T
Thomas Winischhofer 已提交
463 464
		printk(KERN_DEBUG "sisfb: Bad EDID header\n");
		return FALSE;
L
Linus Torvalds 已提交
465 466 467
	}

	if(buffer[0x12] != 0x01) {
T
Thomas Winischhofer 已提交
468 469 470
		printk(KERN_INFO "sisfb: EDID version %d not supported\n",
			buffer[0x12]);
		return FALSE;
L
Linus Torvalds 已提交
471 472 473 474 475
	}

	monitor->feature = buffer[0x18];

	if(!buffer[0x14] & 0x80) {
T
Thomas Winischhofer 已提交
476 477 478 479
		if(!(buffer[0x14] & 0x08)) {
			printk(KERN_INFO
				"sisfb: WARNING: Monitor does not support separate syncs\n");
		}
L
Linus Torvalds 已提交
480 481 482 483 484 485 486 487 488
	}

	if(buffer[0x13] >= 0x01) {
	   /* EDID V1 rev 1 and 2: Search for monitor descriptor
	    * to extract ranges
	    */
	    j = 0x36;
	    for(i=0; i<4; i++) {
	       if(buffer[j]     == 0x00 && buffer[j + 1] == 0x00 &&
T
Thomas Winischhofer 已提交
489
		  buffer[j + 2] == 0x00 && buffer[j + 3] == 0xfd &&
L
Linus Torvalds 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
		  buffer[j + 4] == 0x00) {
		  monitor->hmin = buffer[j + 7];
		  monitor->hmax = buffer[j + 8];
		  monitor->vmin = buffer[j + 5];
		  monitor->vmax = buffer[j + 6];
		  monitor->dclockmax = buffer[j + 9] * 10 * 1000;
		  monitor->datavalid = TRUE;
		  break;
	       }
	       j += 18;
	    }
	}

	if(!monitor->datavalid) {
	   /* Otherwise: Get a range from the list of supported
	    * Estabished Timings. This is not entirely accurate,
	    * because fixed frequency monitors are not supported
	    * that way.
	    */
	   monitor->hmin = 65535; monitor->hmax = 0;
	   monitor->vmin = 65535; monitor->vmax = 0;
	   monitor->dclockmax = 0;
	   emodes = buffer[0x23] | (buffer[0x24] << 8) | (buffer[0x25] << 16);
	   for(i = 0; i < 13; i++) {
	      if(emodes & sisfb_ddcsmodes[i].mask) {
T
Thomas Winischhofer 已提交
515
		 if(monitor->hmin > sisfb_ddcsmodes[i].h) monitor->hmin = sisfb_ddcsmodes[i].h;
L
Linus Torvalds 已提交
516 517 518 519 520 521 522 523 524 525
		 if(monitor->hmax < sisfb_ddcsmodes[i].h) monitor->hmax = sisfb_ddcsmodes[i].h + 1;
		 if(monitor->vmin > sisfb_ddcsmodes[i].v) monitor->vmin = sisfb_ddcsmodes[i].v;
		 if(monitor->vmax < sisfb_ddcsmodes[i].v) monitor->vmax = sisfb_ddcsmodes[i].v;
		 if(monitor->dclockmax < sisfb_ddcsmodes[i].d) monitor->dclockmax = sisfb_ddcsmodes[i].d;
	      }
	   }
	   index = 0x26;
	   for(i = 0; i < 8; i++) {
	      xres = (buffer[index] + 31) * 8;
	      switch(buffer[index + 1] & 0xc0) {
T
Thomas Winischhofer 已提交
526 527 528 529
		 case 0xc0: yres = (xres * 9) / 16; break;
		 case 0x80: yres = (xres * 4) /  5; break;
		 case 0x40: yres = (xres * 3) /  4; break;
		 default:   yres = xres;	    break;
L
Linus Torvalds 已提交
530 531 532
	      }
	      refresh = (buffer[index + 1] & 0x3f) + 60;
	      if((xres >= 640) && (yres >= 480)) {
T
Thomas Winischhofer 已提交
533 534 535
		 for(j = 0; j < 8; j++) {
		    if((xres == sisfb_ddcfmodes[j].x) &&
		       (yres == sisfb_ddcfmodes[j].y) &&
L
Linus Torvalds 已提交
536 537 538 539 540
		       (refresh == sisfb_ddcfmodes[j].v)) {
		      if(monitor->hmin > sisfb_ddcfmodes[j].h) monitor->hmin = sisfb_ddcfmodes[j].h;
		      if(monitor->hmax < sisfb_ddcfmodes[j].h) monitor->hmax = sisfb_ddcfmodes[j].h + 1;
		      if(monitor->vmin > sisfb_ddcsmodes[j].v) monitor->vmin = sisfb_ddcsmodes[j].v;
		      if(monitor->vmax < sisfb_ddcsmodes[j].v) monitor->vmax = sisfb_ddcsmodes[j].v;
T
Thomas Winischhofer 已提交
541 542 543
		      if(monitor->dclockmax < sisfb_ddcsmodes[j].d) monitor->dclockmax = sisfb_ddcsmodes[j].d;
		    }
		 }
L
Linus Torvalds 已提交
544 545
	      }
	      index += 2;
T
Thomas Winischhofer 已提交
546
	   }
L
Linus Torvalds 已提交
547 548 549 550 551
	   if((monitor->hmin <= monitor->hmax) && (monitor->vmin <= monitor->vmax)) {
	      monitor->datavalid = TRUE;
	   }
	}

T
Thomas Winischhofer 已提交
552
	return monitor->datavalid;
L
Linus Torvalds 已提交
553 554 555 556 557
}

static void __devinit
sisfb_handle_ddc(struct sis_video_info *ivideo, struct sisfb_monitor *monitor, int crtno)
{
T
Thomas Winischhofer 已提交
558 559
	unsigned short temp, i, realcrtno = crtno;
	unsigned char  buffer[256];
L
Linus Torvalds 已提交
560 561 562 563

	monitor->datavalid = FALSE;

	if(crtno) {
T
Thomas Winischhofer 已提交
564 565 566 567
	   if(ivideo->vbflags & CRT2_LCD)      realcrtno = 1;
	   else if(ivideo->vbflags & CRT2_VGA) realcrtno = 2;
	   else return;
	}
L
Linus Torvalds 已提交
568

T
Thomas Winischhofer 已提交
569 570
	if((ivideo->sisfb_crt1off) && (!crtno))
		return;
L
Linus Torvalds 已提交
571

T
Thomas Winischhofer 已提交
572 573 574 575
	temp = SiS_HandleDDC(&ivideo->SiS_Pr, ivideo->vbflags, ivideo->sisvga_engine,
				realcrtno, 0, &buffer[0], ivideo->vbflags2);
	if((!temp) || (temp == 0xffff)) {
	   printk(KERN_INFO "sisfb: CRT%d DDC probing failed\n", crtno + 1);
L
Linus Torvalds 已提交
576
	   return;
T
Thomas Winischhofer 已提交
577 578 579 580 581 582 583 584 585
	} else {
	   printk(KERN_INFO "sisfb: CRT%d DDC supported\n", crtno + 1);
	   printk(KERN_INFO "sisfb: CRT%d DDC level: %s%s%s%s\n",
		crtno + 1,
		(temp & 0x1a) ? "" : "[none of the supported]",
		(temp & 0x02) ? "2 " : "",
		(temp & 0x08) ? "D&P" : "",
		(temp & 0x10) ? "FPDI-2" : "");
	   if(temp & 0x02) {
L
Linus Torvalds 已提交
586 587
	      i = 3;  /* Number of retrys */
	      do {
T
Thomas Winischhofer 已提交
588 589
		 temp = SiS_HandleDDC(&ivideo->SiS_Pr, ivideo->vbflags, ivideo->sisvga_engine,
				     realcrtno, 1, &buffer[0], ivideo->vbflags2);
L
Linus Torvalds 已提交
590
	      } while((temp) && i--);
T
Thomas Winischhofer 已提交
591 592
	      if(!temp) {
		 if(sisfb_interpret_edid(monitor, &buffer[0])) {
L
Linus Torvalds 已提交
593
		    printk(KERN_INFO "sisfb: Monitor range H %d-%dKHz, V %d-%dHz, Max. dotclock %dMHz\n",
T
Thomas Winischhofer 已提交
594
			monitor->hmin, monitor->hmax, monitor->vmin, monitor->vmax,
L
Linus Torvalds 已提交
595 596
			monitor->dclockmax / 1000);
		 } else {
T
Thomas Winischhofer 已提交
597 598
		    printk(KERN_INFO "sisfb: CRT%d DDC EDID corrupt\n", crtno + 1);
		 }
L
Linus Torvalds 已提交
599
	      } else {
T
Thomas Winischhofer 已提交
600
		 printk(KERN_INFO "sisfb: CRT%d DDC reading failed\n", crtno + 1);
L
Linus Torvalds 已提交
601 602 603 604 605 606 607
	      }
	   } else {
	      printk(KERN_INFO "sisfb: VESA D&P and FPDI-2 not supported yet\n");
	   }
	}
}

T
Thomas Winischhofer 已提交
608 609
/* -------------- Mode validation --------------- */

L
Linus Torvalds 已提交
610 611 612 613 614 615 616
static BOOLEAN
sisfb_verify_rate(struct sis_video_info *ivideo, struct sisfb_monitor *monitor,
		int mode_idx, int rate_idx, int rate)
{
	int htotal, vtotal;
	unsigned int dclock, hsync;

T
Thomas Winischhofer 已提交
617 618
	if(!monitor->datavalid)
		return TRUE;
L
Linus Torvalds 已提交
619

T
Thomas Winischhofer 已提交
620 621
	if(mode_idx < 0)
		return FALSE;
L
Linus Torvalds 已提交
622 623

	/* Skip for 320x200, 320x240, 640x400 */
T
Thomas Winischhofer 已提交
624 625 626 627 628 629 630 631 632 633 634
	switch(sisbios_mode[mode_idx].mode_no[ivideo->mni]) {
	case 0x59:
	case 0x41:
	case 0x4f:
	case 0x50:
	case 0x56:
	case 0x53:
	case 0x2f:
	case 0x5d:
	case 0x5e:
		return TRUE;
L
Linus Torvalds 已提交
635 636 637 638 639
#ifdef CONFIG_FB_SIS_315
	case 0x5a:
	case 0x5b:
		if(ivideo->sisvga_engine == SIS_315_VGA) return TRUE;
#endif
T
Thomas Winischhofer 已提交
640
	}
L
Linus Torvalds 已提交
641

T
Thomas Winischhofer 已提交
642 643 644 645
	if(rate < (monitor->vmin - 1))
		return FALSE;
	if(rate > (monitor->vmax + 1))
		return FALSE;
L
Linus Torvalds 已提交
646

T
Thomas Winischhofer 已提交
647
	if(sisfb_gettotalfrommode(&ivideo->SiS_Pr,
L
Linus Torvalds 已提交
648
				  sisbios_mode[mode_idx].mode_no[ivideo->mni],
T
Thomas Winischhofer 已提交
649
				  &htotal, &vtotal, rate_idx)) {
L
Linus Torvalds 已提交
650
		dclock = (htotal * vtotal * rate) / 1000;
T
Thomas Winischhofer 已提交
651 652
		if(dclock > (monitor->dclockmax + 1000))
			return FALSE;
L
Linus Torvalds 已提交
653
		hsync = dclock / htotal;
T
Thomas Winischhofer 已提交
654 655 656 657
		if(hsync < (monitor->hmin - 1))
			return FALSE;
		if(hsync > (monitor->hmax + 1))
			return FALSE;
L
Linus Torvalds 已提交
658
        } else {
T
Thomas Winischhofer 已提交
659
		return FALSE;
L
Linus Torvalds 已提交
660 661 662 663 664 665 666
	}
	return TRUE;
}

static int
sisfb_validate_mode(struct sis_video_info *ivideo, int myindex, u32 vbflags)
{
T
Thomas Winischhofer 已提交
667
	u16 xres=0, yres, myres;
L
Linus Torvalds 已提交
668 669

#ifdef CONFIG_FB_SIS_300
T
Thomas Winischhofer 已提交
670 671 672 673
	if(ivideo->sisvga_engine == SIS_300_VGA) {
		if(!(sisbios_mode[myindex].chipset & MD_SIS300))
			return -1 ;
	}
L
Linus Torvalds 已提交
674 675
#endif
#ifdef CONFIG_FB_SIS_315
T
Thomas Winischhofer 已提交
676 677 678 679
	if(ivideo->sisvga_engine == SIS_315_VGA) {
		if(!(sisbios_mode[myindex].chipset & MD_SIS315))
			return -1;
	}
L
Linus Torvalds 已提交
680 681
#endif

T
Thomas Winischhofer 已提交
682
	myres = sisbios_mode[myindex].yres;
L
Linus Torvalds 已提交
683

T
Thomas Winischhofer 已提交
684
	switch(vbflags & VB_DISPTYPE_DISP2) {
L
Linus Torvalds 已提交
685

T
Thomas Winischhofer 已提交
686 687
	case CRT2_LCD:
		xres = ivideo->lcdxres; yres = ivideo->lcdyres;
L
Linus Torvalds 已提交
688

T
Thomas Winischhofer 已提交
689 690 691 692 693 694 695
		if((ivideo->SiS_Pr.SiS_CustomT != CUT_PANEL848) &&
		   (ivideo->SiS_Pr.SiS_CustomT != CUT_PANEL856)) {
			if(sisbios_mode[myindex].xres > xres)
				return -1;
			if(myres > yres)
				return -1;
		}
L
Linus Torvalds 已提交
696

T
Thomas Winischhofer 已提交
697 698 699 700 701 702 703 704 705 706 707
		if(ivideo->sisfb_fstn) {
			if(sisbios_mode[myindex].xres == 320) {
				if(myres == 240) {
					switch(sisbios_mode[myindex].mode_no[1]) {
						case 0x50: myindex = MODE_FSTN_8;  break;
						case 0x56: myindex = MODE_FSTN_16; break;
						case 0x53: return -1;
					}
				}
			}
		}
L
Linus Torvalds 已提交
708

T
Thomas Winischhofer 已提交
709 710 711 712 713 714
		if(SiS_GetModeID_LCD(ivideo->sisvga_engine, vbflags, sisbios_mode[myindex].xres,
			 	sisbios_mode[myindex].yres, 0, ivideo->sisfb_fstn,
			 	ivideo->SiS_Pr.SiS_CustomT, xres, yres, ivideo->vbflags2) < 0x14) {
			return -1;
		}
		break;
L
Linus Torvalds 已提交
715

T
Thomas Winischhofer 已提交
716 717 718 719 720 721
	case CRT2_TV:
		if(SiS_GetModeID_TV(ivideo->sisvga_engine, vbflags, sisbios_mode[myindex].xres,
				sisbios_mode[myindex].yres, 0, ivideo->vbflags2) < 0x14) {
			return -1;
		}
		break;
L
Linus Torvalds 已提交
722

T
Thomas Winischhofer 已提交
723 724 725 726 727 728
	case CRT2_VGA:
		if(SiS_GetModeID_VGA2(ivideo->sisvga_engine, vbflags, sisbios_mode[myindex].xres,
				sisbios_mode[myindex].yres, 0, ivideo->vbflags2) < 0x14) {
			return -1;
		}
		break;
L
Linus Torvalds 已提交
729 730
	}

T
Thomas Winischhofer 已提交
731
	return myindex;
L
Linus Torvalds 已提交
732 733 734 735 736 737
}

static u8
sisfb_search_refresh_rate(struct sis_video_info *ivideo, unsigned int rate, int mode_idx)
{
	int i = 0;
T
Thomas Winischhofer 已提交
738 739
	u16 xres = sisbios_mode[mode_idx].xres;
	u16 yres = sisbios_mode[mode_idx].yres;
L
Linus Torvalds 已提交
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758

	ivideo->rate_idx = 0;
	while((sisfb_vrate[i].idx != 0) && (sisfb_vrate[i].xres <= xres)) {
		if((sisfb_vrate[i].xres == xres) && (sisfb_vrate[i].yres == yres)) {
			if(sisfb_vrate[i].refresh == rate) {
				ivideo->rate_idx = sisfb_vrate[i].idx;
				break;
			} else if(sisfb_vrate[i].refresh > rate) {
				if((sisfb_vrate[i].refresh - rate) <= 3) {
					DPRINTK("sisfb: Adjusting rate from %d up to %d\n",
						rate, sisfb_vrate[i].refresh);
					ivideo->rate_idx = sisfb_vrate[i].idx;
					ivideo->refresh_rate = sisfb_vrate[i].refresh;
				} else if(((rate - sisfb_vrate[i-1].refresh) <= 2)
						&& (sisfb_vrate[i].idx != 1)) {
					DPRINTK("sisfb: Adjusting rate from %d down to %d\n",
						rate, sisfb_vrate[i-1].refresh);
					ivideo->rate_idx = sisfb_vrate[i-1].idx;
					ivideo->refresh_rate = sisfb_vrate[i-1].refresh;
T
Thomas Winischhofer 已提交
759
				}
L
Linus Torvalds 已提交
760 761 762 763
				break;
			} else if((rate - sisfb_vrate[i].refresh) <= 2) {
				DPRINTK("sisfb: Adjusting rate from %d down to %d\n",
						rate, sisfb_vrate[i].refresh);
T
Thomas Winischhofer 已提交
764 765 766
				ivideo->rate_idx = sisfb_vrate[i].idx;
				break;
			}
L
Linus Torvalds 已提交
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
		}
		i++;
	}
	if(ivideo->rate_idx > 0) {
		return ivideo->rate_idx;
	} else {
		printk(KERN_INFO "sisfb: Unsupported rate %d for %dx%d\n",
				rate, xres, yres);
		return 0;
	}
}

static BOOLEAN
sisfb_bridgeisslave(struct sis_video_info *ivideo)
{
T
Thomas Winischhofer 已提交
782
	unsigned char P1_00;
L
Linus Torvalds 已提交
783

T
Thomas Winischhofer 已提交
784 785
	if(!(ivideo->vbflags2 & VB2_VIDEOBRIDGE))
		return FALSE;
L
Linus Torvalds 已提交
786

T
Thomas Winischhofer 已提交
787 788 789 790 791 792 793
	inSISIDXREG(SISPART1,0x00,P1_00);
	if( ((ivideo->sisvga_engine == SIS_300_VGA) && (P1_00 & 0xa0) == 0x20) ||
	    ((ivideo->sisvga_engine == SIS_315_VGA) && (P1_00 & 0x50) == 0x10) ) {
		return TRUE;
	} else {
		return FALSE;
	}
L
Linus Torvalds 已提交
794 795 796 797 798
}

static BOOLEAN
sisfballowretracecrt1(struct sis_video_info *ivideo)
{
T
Thomas Winischhofer 已提交
799
	u8 temp;
L
Linus Torvalds 已提交
800

T
Thomas Winischhofer 已提交
801 802 803
	inSISIDXREG(SISCR,0x17,temp);
	if(!(temp & 0x80))
		return FALSE;
L
Linus Torvalds 已提交
804

T
Thomas Winischhofer 已提交
805 806 807
	inSISIDXREG(SISSR,0x1f,temp);
	if(temp & 0xc0)
		return FALSE;
L
Linus Torvalds 已提交
808

T
Thomas Winischhofer 已提交
809
	return TRUE;
L
Linus Torvalds 已提交
810 811 812 813 814
}

static BOOLEAN
sisfbcheckvretracecrt1(struct sis_video_info *ivideo)
{
T
Thomas Winischhofer 已提交
815 816
	if(!sisfballowretracecrt1(ivideo))
		return FALSE;
L
Linus Torvalds 已提交
817

T
Thomas Winischhofer 已提交
818 819 820 821
	if(inSISREG(SISINPSTAT) & 0x08)
		return TRUE;
	else
		return FALSE;
L
Linus Torvalds 已提交
822 823 824 825 826
}

static void
sisfbwaitretracecrt1(struct sis_video_info *ivideo)
{
T
Thomas Winischhofer 已提交
827
	int watchdog;
L
Linus Torvalds 已提交
828

T
Thomas Winischhofer 已提交
829 830
	if(!sisfballowretracecrt1(ivideo))
		return;
L
Linus Torvalds 已提交
831

T
Thomas Winischhofer 已提交
832 833 834 835
	watchdog = 65536;
	while((!(inSISREG(SISINPSTAT) & 0x08)) && --watchdog);
	watchdog = 65536;
	while((inSISREG(SISINPSTAT) & 0x08) && --watchdog);
L
Linus Torvalds 已提交
836 837 838 839 840
}

static BOOLEAN
sisfbcheckvretracecrt2(struct sis_video_info *ivideo)
{
T
Thomas Winischhofer 已提交
841
	unsigned char temp, reg;
L
Linus Torvalds 已提交
842

T
Thomas Winischhofer 已提交
843 844 845 846 847
	switch(ivideo->sisvga_engine) {
	case SIS_300_VGA: reg = 0x25; break;
	case SIS_315_VGA: reg = 0x30; break;
	default:	  return FALSE;
	}
L
Linus Torvalds 已提交
848

T
Thomas Winischhofer 已提交
849 850 851 852 853
	inSISIDXREG(SISPART1, reg, temp);
	if(temp & 0x02)
		return TRUE;
	else
		return FALSE;
L
Linus Torvalds 已提交
854 855 856 857 858
}

static BOOLEAN
sisfb_CheckVBRetrace(struct sis_video_info *ivideo)
{
T
Thomas Winischhofer 已提交
859 860 861 862 863 864
	if(ivideo->currentvbflags & VB_DISPTYPE_DISP2) {
		if(!sisfb_bridgeisslave(ivideo)) {
			return sisfbcheckvretracecrt2(ivideo);
		}
	}
	return sisfbcheckvretracecrt1(ivideo);
L
Linus Torvalds 已提交
865 866 867 868 869
}

static u32
sisfb_setupvbblankflags(struct sis_video_info *ivideo, u32 *vcount, u32 *hcount)
{
T
Thomas Winischhofer 已提交
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
	u8 idx, reg1, reg2, reg3, reg4;
	u32 ret = 0;

	(*vcount) = (*hcount) = 0;

	if((ivideo->currentvbflags & VB_DISPTYPE_DISP2) && (!(sisfb_bridgeisslave(ivideo)))) {

		ret |= (FB_VBLANK_HAVE_VSYNC  |
			FB_VBLANK_HAVE_HBLANK |
			FB_VBLANK_HAVE_VBLANK |
			FB_VBLANK_HAVE_VCOUNT |
			FB_VBLANK_HAVE_HCOUNT);
		switch(ivideo->sisvga_engine) {
			case SIS_300_VGA: idx = 0x25; break;
			default:
			case SIS_315_VGA: idx = 0x30; break;
		}
		inSISIDXREG(SISPART1,(idx+0),reg1); /* 30 */
		inSISIDXREG(SISPART1,(idx+1),reg2); /* 31 */
		inSISIDXREG(SISPART1,(idx+2),reg3); /* 32 */
		inSISIDXREG(SISPART1,(idx+3),reg4); /* 33 */
		if(reg1 & 0x01) ret |= FB_VBLANK_VBLANKING;
		if(reg1 & 0x02) ret |= FB_VBLANK_VSYNCING;
		if(reg4 & 0x80) ret |= FB_VBLANK_HBLANKING;
		(*vcount) = reg3 | ((reg4 & 0x70) << 4);
		(*hcount) = reg2 | ((reg4 & 0x0f) << 8);

	} else if(sisfballowretracecrt1(ivideo)) {

		ret |= (FB_VBLANK_HAVE_VSYNC  |
			FB_VBLANK_HAVE_VBLANK |
			FB_VBLANK_HAVE_VCOUNT |
			FB_VBLANK_HAVE_HCOUNT);
		reg1 = inSISREG(SISINPSTAT);
		if(reg1 & 0x08) ret |= FB_VBLANK_VSYNCING;
		if(reg1 & 0x01) ret |= FB_VBLANK_VBLANKING;
		inSISIDXREG(SISCR,0x20,reg1);
		inSISIDXREG(SISCR,0x1b,reg1);
		inSISIDXREG(SISCR,0x1c,reg2);
		inSISIDXREG(SISCR,0x1d,reg3);
		(*vcount) = reg2 | ((reg3 & 0x07) << 8);
		(*hcount) = (reg1 | ((reg3 & 0x10) << 4)) << 3;
	}

	return ret;
L
Linus Torvalds 已提交
915 916 917 918 919
}

static int
sisfb_myblank(struct sis_video_info *ivideo, int blank)
{
T
Thomas Winischhofer 已提交
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 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 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 1040 1041 1042 1043 1044 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
	u8 sr01, sr11, sr1f, cr63=0, p2_0, p1_13;
	BOOLEAN backlight = TRUE;

	switch(blank) {
		case FB_BLANK_UNBLANK:	/* on */
			sr01  = 0x00;
			sr11  = 0x00;
			sr1f  = 0x00;
			cr63  = 0x00;
			p2_0  = 0x20;
			p1_13 = 0x00;
			backlight = TRUE;
			break;
		case FB_BLANK_NORMAL:	/* blank */
			sr01  = 0x20;
			sr11  = 0x00;
			sr1f  = 0x00;
			cr63  = 0x00;
			p2_0  = 0x20;
			p1_13 = 0x00;
			backlight = TRUE;
			break;
		case FB_BLANK_VSYNC_SUSPEND:	/* no vsync */
			sr01  = 0x20;
			sr11  = 0x08;
			sr1f  = 0x80;
			cr63  = 0x40;
			p2_0  = 0x40;
			p1_13 = 0x80;
			backlight = FALSE;
			break;
		case FB_BLANK_HSYNC_SUSPEND:	/* no hsync */
			sr01  = 0x20;
			sr11  = 0x08;
			sr1f  = 0x40;
			cr63  = 0x40;
			p2_0  = 0x80;
			p1_13 = 0x40;
			backlight = FALSE;
			break;
		case FB_BLANK_POWERDOWN:	/* off */
			sr01  = 0x20;
			sr11  = 0x08;
			sr1f  = 0xc0;
			cr63  = 0x40;
			p2_0  = 0xc0;
			p1_13 = 0xc0;
			backlight = FALSE;
			break;
		default:
			return 1;
	}

	if(ivideo->currentvbflags & VB_DISPTYPE_CRT1) {

		if( (!ivideo->sisfb_thismonitor.datavalid) ||
		    ((ivideo->sisfb_thismonitor.datavalid) &&
		     (ivideo->sisfb_thismonitor.feature & 0xe0))) {

			if(ivideo->sisvga_engine == SIS_315_VGA) {
				setSISIDXREG(SISCR, ivideo->SiS_Pr.SiS_MyCR63, 0xbf, cr63);
			}

			if(!(sisfb_bridgeisslave(ivideo))) {
				setSISIDXREG(SISSR, 0x01, ~0x20, sr01);
				setSISIDXREG(SISSR, 0x1f, 0x3f, sr1f);
			}
		}

	}

	if(ivideo->currentvbflags & CRT2_LCD) {

		if(ivideo->vbflags2 & VB2_SISLVDSBRIDGE) {
			if(backlight) {
				SiS_SiS30xBLOn(&ivideo->SiS_Pr);
			} else {
				SiS_SiS30xBLOff(&ivideo->SiS_Pr);
			}
		} else if(ivideo->sisvga_engine == SIS_315_VGA) {
#ifdef CONFIG_FB_SIS_315
			if(ivideo->vbflags2 & VB2_CHRONTEL) {
				if(backlight) {
					SiS_Chrontel701xBLOn(&ivideo->SiS_Pr);
				} else {
					SiS_Chrontel701xBLOff(&ivideo->SiS_Pr);
				}
			}
#endif
		}

		if(((ivideo->sisvga_engine == SIS_300_VGA) &&
		    (ivideo->vbflags2 & (VB2_301|VB2_30xBDH|VB2_LVDS))) ||
		   ((ivideo->sisvga_engine == SIS_315_VGA) &&
		    ((ivideo->vbflags2 & (VB2_LVDS | VB2_CHRONTEL)) == VB2_LVDS))) {
			setSISIDXREG(SISSR, 0x11, ~0x0c, sr11);
		}

		if(ivideo->sisvga_engine == SIS_300_VGA) {
			if((ivideo->vbflags2 & VB2_30xB) &&
			   (!(ivideo->vbflags2 & VB2_30xBDH))) {
				setSISIDXREG(SISPART1, 0x13, 0x3f, p1_13);
			}
		} else if(ivideo->sisvga_engine == SIS_315_VGA) {
			if((ivideo->vbflags2 & VB2_30xB) &&
			   (!(ivideo->vbflags2 & VB2_30xBDH))) {
				setSISIDXREG(SISPART2, 0x00, 0x1f, p2_0);
			}
		}

	} else if(ivideo->currentvbflags & CRT2_VGA) {

		if(ivideo->vbflags2 & VB2_30xB) {
			setSISIDXREG(SISPART2, 0x00, 0x1f, p2_0);
		}

	}

	return 0;
}

/* ------------- Callbacks from init.c/init301.c  -------------- */

#ifdef CONFIG_FB_SIS_300
unsigned int
sisfb_read_nbridge_pci_dword(struct SiS_Private *SiS_Pr, int reg)
{
   struct sis_video_info *ivideo = (struct sis_video_info *)SiS_Pr->ivideo;
   u32 val = 0;

   pci_read_config_dword(ivideo->nbridge, reg, &val);
   return (unsigned int)val;
}

void
sisfb_write_nbridge_pci_dword(struct SiS_Private *SiS_Pr, int reg, unsigned int val)
{
   struct sis_video_info *ivideo = (struct sis_video_info *)SiS_Pr->ivideo;

   pci_write_config_dword(ivideo->nbridge, reg, (u32)val);
}

unsigned int
sisfb_read_lpc_pci_dword(struct SiS_Private *SiS_Pr, int reg)
{
   struct sis_video_info *ivideo = (struct sis_video_info *)SiS_Pr->ivideo;
   u32 val = 0;

   if(!ivideo->lpcdev) return 0;

   pci_read_config_dword(ivideo->lpcdev, reg, &val);
   return (unsigned int)val;
}
#endif

#ifdef CONFIG_FB_SIS_315
void
sisfb_write_nbridge_pci_byte(struct SiS_Private *SiS_Pr, int reg, unsigned char val)
{
   struct sis_video_info *ivideo = (struct sis_video_info *)SiS_Pr->ivideo;

   pci_write_config_byte(ivideo->nbridge, reg, (u8)val);
L
Linus Torvalds 已提交
1082 1083
}

T
Thomas Winischhofer 已提交
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
unsigned int
sisfb_read_mio_pci_word(struct SiS_Private *SiS_Pr, int reg)
{
   struct sis_video_info *ivideo = (struct sis_video_info *)SiS_Pr->ivideo;
   u16 val = 0;

   if(!ivideo->lpcdev) return 0;

   pci_read_config_word(ivideo->lpcdev, reg, &val);
   return (unsigned int)val;
}
#endif

L
Linus Torvalds 已提交
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
/* ----------- FBDev related routines for all series ----------- */

static int
sisfb_get_cmap_len(const struct fb_var_screeninfo *var)
{
	return (var->bits_per_pixel == 8) ? 256 : 16;
}

static void
sisfb_set_vparms(struct sis_video_info *ivideo)
{
T
Thomas Winischhofer 已提交
1108
	switch(ivideo->video_bpp) {
L
Linus Torvalds 已提交
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
	case 8:
		ivideo->DstColor = 0x0000;
		ivideo->SiS310_AccelDepth = 0x00000000;
		ivideo->video_cmap_len = 256;
		break;
	case 16:
		ivideo->DstColor = 0x8000;
		ivideo->SiS310_AccelDepth = 0x00010000;
		ivideo->video_cmap_len = 16;
		break;
	case 32:
		ivideo->DstColor = 0xC000;
		ivideo->SiS310_AccelDepth = 0x00020000;
		ivideo->video_cmap_len = 16;
		break;
	default:
		ivideo->video_cmap_len = 16;
		printk(KERN_ERR "sisfb: Unsupported depth %d", ivideo->video_bpp);
		ivideo->accel = 0;
T
Thomas Winischhofer 已提交
1128
	}
L
Linus Torvalds 已提交
1129 1130 1131 1132 1133
}

static int
sisfb_calc_maxyres(struct sis_video_info *ivideo, struct fb_var_screeninfo *var)
{
T
Thomas Winischhofer 已提交
1134
	int maxyres = ivideo->sisfb_mem / (var->xres_virtual * (var->bits_per_pixel >> 3));
L
Linus Torvalds 已提交
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155

	if(maxyres > 32767) maxyres = 32767;

	return maxyres;
}

static void
sisfb_calc_pitch(struct sis_video_info *ivideo, struct fb_var_screeninfo *var)
{
	ivideo->video_linelength = var->xres_virtual * (var->bits_per_pixel >> 3);
	ivideo->scrnpitchCRT1 = ivideo->video_linelength;
	if(!(ivideo->currentvbflags & CRT1_LCDA)) {
		if((var->vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) {
			ivideo->scrnpitchCRT1 <<= 1;
		}
	}
}

static void
sisfb_set_pitch(struct sis_video_info *ivideo)
{
T
Thomas Winischhofer 已提交
1156
	BOOLEAN isslavemode = FALSE;
L
Linus Torvalds 已提交
1157 1158 1159
	unsigned short HDisplay1 = ivideo->scrnpitchCRT1 >> 3;
	unsigned short HDisplay2 = ivideo->video_linelength >> 3;

T
Thomas Winischhofer 已提交
1160
	if(sisfb_bridgeisslave(ivideo)) isslavemode = TRUE;
L
Linus Torvalds 已提交
1161

T
Thomas Winischhofer 已提交
1162 1163 1164 1165
	/* We need to set pitch for CRT1 if bridge is in slave mode, too */
	if((ivideo->currentvbflags & VB_DISPTYPE_DISP1) || (isslavemode)) {
		outSISIDXREG(SISCR,0x13,(HDisplay1 & 0xFF));
		setSISIDXREG(SISSR,0x0E,0xF0,(HDisplay1 >> 8));
L
Linus Torvalds 已提交
1166 1167
	}

T
Thomas Winischhofer 已提交
1168 1169
	/* We must not set the pitch for CRT2 if bridge is in slave mode */
	if((ivideo->currentvbflags & VB_DISPTYPE_DISP2) && (!isslavemode)) {
L
Linus Torvalds 已提交
1170
		orSISIDXREG(SISPART1,ivideo->CRT2_write_enable,0x01);
T
Thomas Winischhofer 已提交
1171 1172 1173
		outSISIDXREG(SISPART1,0x07,(HDisplay2 & 0xFF));
		setSISIDXREG(SISPART1,0x09,0xF0,(HDisplay2 >> 8));
	}
L
Linus Torvalds 已提交
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
}

static void
sisfb_bpp_to_var(struct sis_video_info *ivideo, struct fb_var_screeninfo *var)
{
	ivideo->video_cmap_len = sisfb_get_cmap_len(var);

	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 = 6;
		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;
		var->transp.offset = 0;
		var->transp.length = 0;
		break;
	case 32:
		var->red.offset = 16;
		var->red.length = 8;
		var->green.offset = 8;
		var->green.length = 8;
		var->blue.offset = 0;
		var->blue.length = 8;
		var->transp.offset = 24;
		var->transp.length = 8;
		break;
	}
}

T
Thomas Winischhofer 已提交
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
static int
sisfb_set_mode(struct sis_video_info *ivideo, int clrscrn)
{
	unsigned short modeno = ivideo->mode_no;

	/* >=2.6.12's fbcon clears the screen anyway */
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
	if(!clrscrn) modeno |= 0x80;
#else
	modeno |= 0x80;
#endif

	outSISIDXREG(SISSR, IND_SIS_PASSWORD, SIS_PASSWORD);

	sisfb_pre_setmode(ivideo);

	if(SiSSetMode(&ivideo->SiS_Pr, modeno) == 0) {
		printk(KERN_ERR "sisfb: Setting mode[0x%x] failed\n", ivideo->mode_no);
		return -EINVAL;
	}

	outSISIDXREG(SISSR, IND_SIS_PASSWORD, SIS_PASSWORD);

	sisfb_post_setmode(ivideo);

	return 0;
}


L
Linus Torvalds 已提交
1238 1239 1240 1241 1242 1243
static int
sisfb_do_set_var(struct fb_var_screeninfo *var, int isactive, struct fb_info *info)
{
	struct sis_video_info *ivideo = (struct sis_video_info *)info->par;
	unsigned int htotal = 0, vtotal = 0;
	unsigned int drate = 0, hrate = 0;
T
Thomas Winischhofer 已提交
1244
	int found_mode = 0, ret;
L
Linus Torvalds 已提交
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
	int old_mode;
	u32 pixclock;

	htotal = var->left_margin + var->xres + var->right_margin + var->hsync_len;

	vtotal = var->upper_margin + var->lower_margin + var->vsync_len;

	pixclock = var->pixclock;

	if((var->vmode & FB_VMODE_MASK) == FB_VMODE_NONINTERLACED) {
		vtotal += var->yres;
		vtotal <<= 1;
	} else if((var->vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE) {
		vtotal += var->yres;
		vtotal <<= 2;
	} else if((var->vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) {
		vtotal += var->yres;
		vtotal <<= 1;
	} else 	vtotal += var->yres;

	if(!(htotal) || !(vtotal)) {
		DPRINTK("sisfb: Invalid 'var' information\n");
		return -EINVAL;
	}

	if(pixclock && htotal && vtotal) {
T
Thomas Winischhofer 已提交
1271 1272 1273
		drate = 1000000000 / pixclock;
		hrate = (drate * 1000) / htotal;
		ivideo->refresh_rate = (unsigned int) (hrate * 2 / vtotal);
L
Linus Torvalds 已提交
1274
	} else {
T
Thomas Winischhofer 已提交
1275
		ivideo->refresh_rate = 60;
L
Linus Torvalds 已提交
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
	}

	old_mode = ivideo->sisfb_mode_idx;
	ivideo->sisfb_mode_idx = 0;

	while( (sisbios_mode[ivideo->sisfb_mode_idx].mode_no[0] != 0) &&
	       (sisbios_mode[ivideo->sisfb_mode_idx].xres <= var->xres) ) {
		if( (sisbios_mode[ivideo->sisfb_mode_idx].xres == var->xres) &&
		    (sisbios_mode[ivideo->sisfb_mode_idx].yres == var->yres) &&
		    (sisbios_mode[ivideo->sisfb_mode_idx].bpp == var->bits_per_pixel)) {
			ivideo->mode_no = sisbios_mode[ivideo->sisfb_mode_idx].mode_no[ivideo->mni];
			found_mode = 1;
			break;
		}
		ivideo->sisfb_mode_idx++;
	}

	if(found_mode) {
		ivideo->sisfb_mode_idx = sisfb_validate_mode(ivideo,
				ivideo->sisfb_mode_idx, ivideo->currentvbflags);
T
Thomas Winischhofer 已提交
1296
		ivideo->mode_no = sisbios_mode[ivideo->sisfb_mode_idx].mode_no[ivideo->mni];
L
Linus Torvalds 已提交
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
	} else {
		ivideo->sisfb_mode_idx = -1;
	}

       	if(ivideo->sisfb_mode_idx < 0) {
		printk(KERN_ERR "sisfb: Mode %dx%dx%d not supported\n", var->xres,
		       var->yres, var->bits_per_pixel);
		ivideo->sisfb_mode_idx = old_mode;
		return -EINVAL;
	}

	if(sisfb_search_refresh_rate(ivideo, ivideo->refresh_rate, ivideo->sisfb_mode_idx) == 0) {
		ivideo->rate_idx = sisbios_mode[ivideo->sisfb_mode_idx].rate_idx;
		ivideo->refresh_rate = 60;
	}

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
	if(ivideo->sisfb_thismonitor.datavalid) {
T
Thomas Winischhofer 已提交
1315
		if(!sisfb_verify_rate(ivideo, &ivideo->sisfb_thismonitor, ivideo->sisfb_mode_idx,
L
Linus Torvalds 已提交
1316
	                         ivideo->rate_idx, ivideo->refresh_rate)) {
T
Thomas Winischhofer 已提交
1317 1318
			printk(KERN_INFO "sisfb: WARNING: Refresh rate exceeds monitor specs!\n");
		}
L
Linus Torvalds 已提交
1319 1320 1321 1322 1323 1324 1325 1326
	}
#endif

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
	if(((var->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW) && isactive) {
#else
	if(isactive) {
#endif
T
Thomas Winischhofer 已提交
1327 1328 1329
		/* If acceleration to be used? Need to know
		 * before pre/post_set_mode()
		 */
L
Linus Torvalds 已提交
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
		ivideo->accel = 0;
#if defined(FBINFO_HWACCEL_DISABLED) && defined(FBINFO_HWACCEL_XPAN)
#ifdef STUPID_ACCELF_TEXT_SHIT
		if(var->accel_flags & FB_ACCELF_TEXT) {
			info->flags &= ~FBINFO_HWACCEL_DISABLED;
		} else {
			info->flags |= FBINFO_HWACCEL_DISABLED;
		}
#endif
		if(!(info->flags & FBINFO_HWACCEL_DISABLED)) ivideo->accel = -1;
#else
		if(var->accel_flags & FB_ACCELF_TEXT) ivideo->accel = -1;
#endif

T
Thomas Winischhofer 已提交
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
		if((ret = sisfb_set_mode(ivideo, 1))) {
			return ret;
		}

		ivideo->video_bpp    = sisbios_mode[ivideo->sisfb_mode_idx].bpp;
		ivideo->video_width  = sisbios_mode[ivideo->sisfb_mode_idx].xres;
		ivideo->video_height = sisbios_mode[ivideo->sisfb_mode_idx].yres;

		sisfb_calc_pitch(ivideo, var);
		sisfb_set_pitch(ivideo);

L
Linus Torvalds 已提交
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
		sisfb_set_vparms(ivideo);

		ivideo->current_width = ivideo->video_width;
		ivideo->current_height = ivideo->video_height;
		ivideo->current_bpp = ivideo->video_bpp;
		ivideo->current_htotal = htotal;
		ivideo->current_vtotal = vtotal;
		ivideo->current_linelength = ivideo->video_linelength;
		ivideo->current_pixclock = var->pixclock;
		ivideo->current_refresh_rate = ivideo->refresh_rate;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
T
Thomas Winischhofer 已提交
1366
		ivideo->sisfb_lastrates[ivideo->mode_no] = ivideo->refresh_rate;
L
Linus Torvalds 已提交
1367 1368 1369 1370 1371 1372
#endif
	}

	return 0;
}

T
Thomas Winischhofer 已提交
1373 1374
static void
sisfb_set_base_CRT1(struct sis_video_info *ivideo, unsigned int base)
L
Linus Torvalds 已提交
1375 1376 1377
{
	outSISIDXREG(SISSR, IND_SIS_PASSWORD, SIS_PASSWORD);

T
Thomas Winischhofer 已提交
1378
	outSISIDXREG(SISCR, 0x0D, base & 0xFF);
L
Linus Torvalds 已提交
1379 1380 1381 1382 1383
	outSISIDXREG(SISCR, 0x0C, (base >> 8) & 0xFF);
	outSISIDXREG(SISSR, 0x0D, (base >> 16) & 0xFF);
	if(ivideo->sisvga_engine == SIS_315_VGA) {
		setSISIDXREG(SISSR, 0x37, 0xFE, (base >> 24) & 0x01);
	}
T
Thomas Winischhofer 已提交
1384 1385 1386 1387 1388 1389
}

static void
sisfb_set_base_CRT2(struct sis_video_info *ivideo, unsigned int base)
{
	if(ivideo->currentvbflags & VB_DISPTYPE_DISP2) {
L
Linus Torvalds 已提交
1390
		orSISIDXREG(SISPART1, ivideo->CRT2_write_enable, 0x01);
T
Thomas Winischhofer 已提交
1391 1392 1393
		outSISIDXREG(SISPART1, 0x06, (base & 0xFF));
		outSISIDXREG(SISPART1, 0x05, ((base >> 8) & 0xFF));
		outSISIDXREG(SISPART1, 0x04, ((base >> 16) & 0xFF));
L
Linus Torvalds 已提交
1394 1395 1396
		if(ivideo->sisvga_engine == SIS_315_VGA) {
			setSISIDXREG(SISPART1, 0x02, 0x7F, ((base >> 24) & 0x01) << 7);
		}
T
Thomas Winischhofer 已提交
1397
	}
L
Linus Torvalds 已提交
1398 1399
}

T
Thomas Winischhofer 已提交
1400 1401
static int
sisfb_pan_var(struct sis_video_info *ivideo, struct fb_var_screeninfo *var)
L
Linus Torvalds 已提交
1402
{
T
Thomas Winischhofer 已提交
1403 1404 1405 1406 1407 1408
	if(var->xoffset > (var->xres_virtual - var->xres)) {
		return -EINVAL;
	}
	if(var->yoffset > (var->yres_virtual - var->yres)) {
		return -EINVAL;
	}
L
Linus Torvalds 已提交
1409

T
Thomas Winischhofer 已提交
1410
	ivideo->current_base = (var->yoffset * var->xres_virtual) + var->xoffset;
L
Linus Torvalds 已提交
1411

T
Thomas Winischhofer 已提交
1412 1413 1414
	/* calculate base bpp dep. */
	switch(var->bits_per_pixel) {
	case 32:
L
Linus Torvalds 已提交
1415
		break;
T
Thomas Winischhofer 已提交
1416 1417
	case 16:
		ivideo->current_base >>= 1;
L
Linus Torvalds 已提交
1418
		break;
T
Thomas Winischhofer 已提交
1419 1420 1421
	case 8:
	default:
		ivideo->current_base >>= 2;
L
Linus Torvalds 已提交
1422 1423 1424
		break;
	}

T
Thomas Winischhofer 已提交
1425
	ivideo->current_base += (ivideo->video_offset >> 2);
L
Linus Torvalds 已提交
1426

T
Thomas Winischhofer 已提交
1427 1428
	sisfb_set_base_CRT1(ivideo, ivideo->current_base);
	sisfb_set_base_CRT2(ivideo, ivideo->current_base);
L
Linus Torvalds 已提交
1429

T
Thomas Winischhofer 已提交
1430 1431
	return 0;
}
L
Linus Torvalds 已提交
1432

T
Thomas Winischhofer 已提交
1433
/* ------------ FBDev related routines for 2.4 series ----------- */
L
Linus Torvalds 已提交
1434

T
Thomas Winischhofer 已提交
1435
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
L
Linus Torvalds 已提交
1436

T
Thomas Winischhofer 已提交
1437
#include "sisfb_fbdev_2_4.h"
L
Linus Torvalds 已提交
1438

T
Thomas Winischhofer 已提交
1439
#endif
L
Linus Torvalds 已提交
1440

T
Thomas Winischhofer 已提交
1441
/* ------------ FBDev related routines for 2.6 series ----------- */
L
Linus Torvalds 已提交
1442

T
Thomas Winischhofer 已提交
1443
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
L
Linus Torvalds 已提交
1444

T
Thomas Winischhofer 已提交
1445 1446 1447 1448
static int
sisfb_open(struct fb_info *info, int user)
{
	return 0;
L
Linus Torvalds 已提交
1449 1450 1451
}

static int
T
Thomas Winischhofer 已提交
1452
sisfb_release(struct fb_info *info, int user)
L
Linus Torvalds 已提交
1453 1454 1455 1456 1457 1458
{
	return 0;
}

static int
sisfb_setcolreg(unsigned regno, unsigned red, unsigned green, unsigned blue,
T
Thomas Winischhofer 已提交
1459
		unsigned transp, struct fb_info *info)
L
Linus Torvalds 已提交
1460 1461 1462
{
	struct sis_video_info *ivideo = (struct sis_video_info *)info->par;

T
Thomas Winischhofer 已提交
1463 1464
	if(regno >= sisfb_get_cmap_len(&info->var))
		return 1;
L
Linus Torvalds 已提交
1465

T
Thomas Winischhofer 已提交
1466
	switch(info->var.bits_per_pixel) {
L
Linus Torvalds 已提交
1467
	case 8:
T
Thomas Winischhofer 已提交
1468
		outSISREG(SISDACA, regno);
L
Linus Torvalds 已提交
1469 1470 1471 1472
		outSISREG(SISDACD, (red >> 10));
		outSISREG(SISDACD, (green >> 10));
		outSISREG(SISDACD, (blue >> 10));
		if(ivideo->currentvbflags & VB_DISPTYPE_DISP2) {
T
Thomas Winischhofer 已提交
1473
			outSISREG(SISDAC2A, regno);
L
Linus Torvalds 已提交
1474 1475 1476 1477 1478 1479
			outSISREG(SISDAC2D, (red >> 8));
			outSISREG(SISDAC2D, (green >> 8));
			outSISREG(SISDAC2D, (blue >> 8));
		}
		break;
	case 16:
T
Thomas Winischhofer 已提交
1480 1481 1482 1483
		((u32 *)(info->pseudo_palette))[regno] =
				(red & 0xf800)          |
				((green & 0xfc00) >> 5) |
				((blue & 0xf800) >> 11);
L
Linus Torvalds 已提交
1484 1485
		break;
	case 32:
T
Thomas Winischhofer 已提交
1486
		red >>= 8;
L
Linus Torvalds 已提交
1487
		green >>= 8;
T
Thomas Winischhofer 已提交
1488 1489 1490
		blue >>= 8;
		((u32 *)(info->pseudo_palette))[regno] =
				(red << 16) | (green << 8) | (blue);
L
Linus Torvalds 已提交
1491 1492 1493 1494 1495 1496
		break;
	}
	return 0;
}

static int
T
Thomas Winischhofer 已提交
1497
sisfb_set_par(struct fb_info *info)
L
Linus Torvalds 已提交
1498
{
T
Thomas Winischhofer 已提交
1499
	int err;
L
Linus Torvalds 已提交
1500

T
Thomas Winischhofer 已提交
1501 1502
	if((err = sisfb_do_set_var(&info->var, 1, info)))
		return err;
L
Linus Torvalds 已提交
1503

T
Thomas Winischhofer 已提交
1504 1505 1506 1507 1508
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)
	sisfb_get_fix(&info->fix, info->currcon, info);
#else
	sisfb_get_fix(&info->fix, -1, info);
#endif
L
Linus Torvalds 已提交
1509 1510 1511 1512
	return 0;
}

static int
T
Thomas Winischhofer 已提交
1513
sisfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
L
Linus Torvalds 已提交
1514 1515
{
	struct sis_video_info *ivideo = (struct sis_video_info *)info->par;
T
Thomas Winischhofer 已提交
1516 1517 1518 1519 1520 1521
	unsigned int htotal = 0, vtotal = 0, myrateindex = 0;
	unsigned int drate = 0, hrate = 0, maxyres;
	int found_mode = 0;
	int refresh_rate, search_idx, tidx;
	BOOLEAN recalc_clock = FALSE;
	u32 pixclock;
L
Linus Torvalds 已提交
1522

T
Thomas Winischhofer 已提交
1523
	htotal = var->left_margin + var->xres + var->right_margin + var->hsync_len;
L
Linus Torvalds 已提交
1524

T
Thomas Winischhofer 已提交
1525
	vtotal = var->upper_margin + var->lower_margin + var->vsync_len;
L
Linus Torvalds 已提交
1526

T
Thomas Winischhofer 已提交
1527
	pixclock = var->pixclock;
L
Linus Torvalds 已提交
1528

T
Thomas Winischhofer 已提交
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
	if((var->vmode & FB_VMODE_MASK) == FB_VMODE_NONINTERLACED) {
		vtotal += var->yres;
		vtotal <<= 1;
	} else if((var->vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE) {
		vtotal += var->yres;
		vtotal <<= 2;
	} else if((var->vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) {
		vtotal += var->yres;
		vtotal <<= 1;
	} else
		vtotal += var->yres;
L
Linus Torvalds 已提交
1540

T
Thomas Winischhofer 已提交
1541 1542
	if(!(htotal) || !(vtotal)) {
		SISFAIL("sisfb: no valid timing data");
L
Linus Torvalds 已提交
1543 1544
	}

T
Thomas Winischhofer 已提交
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
	search_idx = 0;
	while( (sisbios_mode[search_idx].mode_no[0] != 0) &&
	       (sisbios_mode[search_idx].xres <= var->xres) ) {
		if( (sisbios_mode[search_idx].xres == var->xres) &&
		    (sisbios_mode[search_idx].yres == var->yres) &&
		    (sisbios_mode[search_idx].bpp == var->bits_per_pixel)) {
			if((tidx = sisfb_validate_mode(ivideo, search_idx,
						ivideo->currentvbflags)) > 0) {
				found_mode = 1;
				search_idx = tidx;
				break;
			}
		}
		search_idx++;
L
Linus Torvalds 已提交
1559 1560
	}

T
Thomas Winischhofer 已提交
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
	if(!found_mode) {
		search_idx = 0;
		while(sisbios_mode[search_idx].mode_no[0] != 0) {
		   if( (var->xres <= sisbios_mode[search_idx].xres) &&
		       (var->yres <= sisbios_mode[search_idx].yres) &&
		       (var->bits_per_pixel == sisbios_mode[search_idx].bpp) ) {
			if((tidx = sisfb_validate_mode(ivideo,search_idx,
						ivideo->currentvbflags)) > 0) {
				found_mode = 1;
				search_idx = tidx;
				break;
			}
		   }
		   search_idx++;
		}
		if(found_mode) {
			printk(KERN_DEBUG
				"sisfb: Adapted from %dx%dx%d to %dx%dx%d\n",
				var->xres, var->yres, var->bits_per_pixel,
				sisbios_mode[search_idx].xres,
				sisbios_mode[search_idx].yres,
				var->bits_per_pixel);
			var->xres = sisbios_mode[search_idx].xres;
			var->yres = sisbios_mode[search_idx].yres;
		} else {
			printk(KERN_ERR
				"sisfb: Failed to find supported mode near %dx%dx%d\n",
				var->xres, var->yres, var->bits_per_pixel);
			return -EINVAL;
		}
	}
L
Linus Torvalds 已提交
1592

T
Thomas Winischhofer 已提交
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
	if( ((ivideo->vbflags2 & VB2_LVDS) ||
	     ((ivideo->vbflags2 & VB2_30xBDH) && (ivideo->currentvbflags & CRT2_LCD))) &&
	    (var->bits_per_pixel == 8) ) {
		/* Slave modes on LVDS and 301B-DH */
		refresh_rate = 60;
		recalc_clock = TRUE;
	} else if( (ivideo->current_htotal == htotal) &&
		   (ivideo->current_vtotal == vtotal) &&
		   (ivideo->current_pixclock == pixclock) ) {
		/* x=x & y=y & c=c -> assume depth change */
		drate = 1000000000 / pixclock;
		hrate = (drate * 1000) / htotal;
		refresh_rate = (unsigned int) (hrate * 2 / vtotal);
	} else if( ( (ivideo->current_htotal != htotal) ||
		     (ivideo->current_vtotal != vtotal) ) &&
		   (ivideo->current_pixclock == var->pixclock) ) {
		/* x!=x | y!=y & c=c -> invalid pixclock */
		if(ivideo->sisfb_lastrates[sisbios_mode[search_idx].mode_no[ivideo->mni]]) {
			refresh_rate =
				ivideo->sisfb_lastrates[sisbios_mode[search_idx].mode_no[ivideo->mni]];
		} else if(ivideo->sisfb_parm_rate != -1) {
			/* Sic, sisfb_parm_rate - want to know originally desired rate here */
			refresh_rate = ivideo->sisfb_parm_rate;
		} else {
			refresh_rate = 60;
		}
		recalc_clock = TRUE;
	} else if((pixclock) && (htotal) && (vtotal)) {
		drate = 1000000000 / pixclock;
		hrate = (drate * 1000) / htotal;
		refresh_rate = (unsigned int) (hrate * 2 / vtotal);
	} else if(ivideo->current_refresh_rate) {
		refresh_rate = ivideo->current_refresh_rate;
		recalc_clock = TRUE;
	} else {
		refresh_rate = 60;
		recalc_clock = TRUE;
	}
L
Linus Torvalds 已提交
1631

T
Thomas Winischhofer 已提交
1632
	myrateindex = sisfb_search_refresh_rate(ivideo, refresh_rate, search_idx);
L
Linus Torvalds 已提交
1633

T
Thomas Winischhofer 已提交
1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
	/* Eventually recalculate timing and clock */
	if(recalc_clock) {
		if(!myrateindex) myrateindex = sisbios_mode[search_idx].rate_idx;
		var->pixclock = (u32) (1000000000 / sisfb_mode_rate_to_dclock(&ivideo->SiS_Pr,
						sisbios_mode[search_idx].mode_no[ivideo->mni],
						myrateindex));
		sisfb_mode_rate_to_ddata(&ivideo->SiS_Pr,
					sisbios_mode[search_idx].mode_no[ivideo->mni],
					myrateindex, var);
		if((var->vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE) {
			var->pixclock <<= 1;
		}
	}
L
Linus Torvalds 已提交
1647

T
Thomas Winischhofer 已提交
1648 1649 1650 1651 1652 1653 1654
	if(ivideo->sisfb_thismonitor.datavalid) {
		if(!sisfb_verify_rate(ivideo, &ivideo->sisfb_thismonitor, search_idx,
				myrateindex, refresh_rate)) {
			printk(KERN_INFO
				"sisfb: WARNING: Refresh rate exceeds monitor specs!\n");
		}
	}
L
Linus Torvalds 已提交
1655

T
Thomas Winischhofer 已提交
1656 1657
	/* Adapt RGB settings */
	sisfb_bpp_to_var(ivideo, var);
L
Linus Torvalds 已提交
1658

T
Thomas Winischhofer 已提交
1659 1660 1661
	/* Sanity check for offsets */
	if(var->xoffset < 0) var->xoffset = 0;
	if(var->yoffset < 0) var->yoffset = 0;
L
Linus Torvalds 已提交
1662

T
Thomas Winischhofer 已提交
1663 1664
	if(var->xres > var->xres_virtual)
		var->xres_virtual = var->xres;
L
Linus Torvalds 已提交
1665

T
Thomas Winischhofer 已提交
1666 1667 1668 1669
	if(ivideo->sisfb_ypan) {
		maxyres = sisfb_calc_maxyres(ivideo, var);
		if(ivideo->sisfb_max) {
			var->yres_virtual = maxyres;
L
Linus Torvalds 已提交
1670
		} else {
T
Thomas Winischhofer 已提交
1671 1672 1673
			if(var->yres_virtual > maxyres) {
				var->yres_virtual = maxyres;
			}
L
Linus Torvalds 已提交
1674
		}
T
Thomas Winischhofer 已提交
1675 1676
		if(var->yres_virtual <= var->yres) {
			var->yres_virtual = var->yres;
L
Linus Torvalds 已提交
1677 1678
		}
	} else {
T
Thomas Winischhofer 已提交
1679 1680 1681 1682 1683
		if(var->yres != var->yres_virtual) {
			var->yres_virtual = var->yres;
		}
		var->xoffset = 0;
		var->yoffset = 0;
L
Linus Torvalds 已提交
1684 1685 1686 1687
	}

	/* Truncate offsets to maximum if too high */
	if(var->xoffset > var->xres_virtual - var->xres) {
T
Thomas Winischhofer 已提交
1688
		var->xoffset = var->xres_virtual - var->xres - 1;
L
Linus Torvalds 已提交
1689 1690 1691
	}

	if(var->yoffset > var->yres_virtual - var->yres) {
T
Thomas Winischhofer 已提交
1692
		var->yoffset = var->yres_virtual - var->yres - 1;
L
Linus Torvalds 已提交
1693
	}
T
Thomas Winischhofer 已提交
1694

L
Linus Torvalds 已提交
1695
	/* Set everything else to 0 */
T
Thomas Winischhofer 已提交
1696 1697 1698 1699 1700 1701
	var->red.msb_right =
		var->green.msb_right =
		var->blue.msb_right =
		var->transp.offset =
		var->transp.length =
		var->transp.msb_right = 0;
L
Linus Torvalds 已提交
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711

	return 0;
}

static int
sisfb_pan_display(struct fb_var_screeninfo *var, struct fb_info* info)
{
	struct sis_video_info *ivideo = (struct sis_video_info *)info->par;
	int err;

T
Thomas Winischhofer 已提交
1712
	if(var->xoffset > (var->xres_virtual - var->xres))
L
Linus Torvalds 已提交
1713
		return -EINVAL;
T
Thomas Winischhofer 已提交
1714 1715

	if(var->yoffset > (var->yres_virtual - var->yres))
L
Linus Torvalds 已提交
1716 1717
		return -EINVAL;

T
Thomas Winischhofer 已提交
1718 1719
	if(var->vmode & FB_VMODE_YWRAP)
		return -EINVAL;
L
Linus Torvalds 已提交
1720 1721

	if(var->xoffset + info->var.xres > info->var.xres_virtual ||
T
Thomas Winischhofer 已提交
1722
	   var->yoffset + info->var.yres > info->var.yres_virtual)
L
Linus Torvalds 已提交
1723 1724
		return -EINVAL;

T
Thomas Winischhofer 已提交
1725 1726
	if((err = sisfb_pan_var(ivideo, var)) < 0)
		return err;
L
Linus Torvalds 已提交
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738

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

	return 0;
}

static int
sisfb_blank(int blank, struct fb_info *info)
{
	struct sis_video_info *ivideo = (struct sis_video_info *)info->par;

T
Thomas Winischhofer 已提交
1739
	return sisfb_myblank(ivideo, blank);
L
Linus Torvalds 已提交
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
}

#endif

/* ----------- FBDev related routines for all series ---------- */

static int
sisfb_ioctl(struct inode *inode, struct file *file,
            unsigned int cmd, unsigned long arg,
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
	    int con,
#endif
	    struct fb_info *info)
{
	struct sis_video_info	*ivideo = (struct sis_video_info *)info->par;
T
Thomas Winischhofer 已提交
1755 1756
	struct sis_memreq	sismemreq;
	struct fb_vblank	sisvbblank;
L
Linus Torvalds 已提交
1757 1758 1759 1760 1761 1762
	u32			gpu32 = 0;
#ifndef __user
#define __user
#endif
	u32 __user 		*argp = (u32 __user *)arg;

T
Thomas Winischhofer 已提交
1763
	switch(cmd) {
L
Linus Torvalds 已提交
1764
	   case FBIO_ALLOC:
T
Thomas Winischhofer 已提交
1765
		if(!capable(CAP_SYS_RAWIO))
L
Linus Torvalds 已提交
1766
			return -EPERM;
T
Thomas Winischhofer 已提交
1767 1768 1769 1770

		if(copy_from_user(&sismemreq, (void __user *)arg, sizeof(sismemreq)))
			return -EFAULT;

L
Linus Torvalds 已提交
1771
		sis_malloc(&sismemreq);
T
Thomas Winischhofer 已提交
1772

L
Linus Torvalds 已提交
1773 1774
		if(copy_to_user((void __user *)arg, &sismemreq, sizeof(sismemreq))) {
			sis_free((u32)sismemreq.offset);
T
Thomas Winischhofer 已提交
1775
			return -EFAULT;
L
Linus Torvalds 已提交
1776 1777 1778 1779
		}
		break;

	   case FBIO_FREE:
T
Thomas Winischhofer 已提交
1780
		if(!capable(CAP_SYS_RAWIO))
L
Linus Torvalds 已提交
1781
			return -EPERM;
T
Thomas Winischhofer 已提交
1782 1783

		if(get_user(gpu32, argp))
L
Linus Torvalds 已提交
1784
			return -EFAULT;
T
Thomas Winischhofer 已提交
1785

L
Linus Torvalds 已提交
1786 1787 1788 1789 1790 1791
		sis_free(gpu32);
		break;

	   case FBIOGET_VBLANK:
		sisvbblank.count = 0;
		sisvbblank.flags = sisfb_setupvbblankflags(ivideo, &sisvbblank.vcount, &sisvbblank.hcount);
T
Thomas Winischhofer 已提交
1792 1793

		if(copy_to_user((void __user *)arg, &sisvbblank, sizeof(sisvbblank)))
L
Linus Torvalds 已提交
1794
			return -EFAULT;
T
Thomas Winischhofer 已提交
1795

L
Linus Torvalds 已提交
1796 1797 1798
		break;

	   case SISFB_GET_INFO_SIZE:
T
Thomas Winischhofer 已提交
1799
		return put_user(sizeof(struct sisfb_info), argp);
L
Linus Torvalds 已提交
1800 1801

	   case SISFB_GET_INFO_OLD:
T
Thomas Winischhofer 已提交
1802 1803 1804
		if(ivideo->warncount++ < 10)
			printk(KERN_INFO
				"sisfb: Deprecated ioctl call received - update your application!\n");
L
Linus Torvalds 已提交
1805
	   case SISFB_GET_INFO:  /* For communication with X driver */
T
Thomas Winischhofer 已提交
1806 1807 1808 1809 1810 1811 1812 1813
		ivideo->sisfb_infoblock.sisfb_id         = SISFB_ID;
		ivideo->sisfb_infoblock.sisfb_version    = VER_MAJOR;
		ivideo->sisfb_infoblock.sisfb_revision   = VER_MINOR;
		ivideo->sisfb_infoblock.sisfb_patchlevel = VER_LEVEL;
		ivideo->sisfb_infoblock.chip_id = ivideo->chip_id;
		ivideo->sisfb_infoblock.sisfb_pci_vendor = ivideo->chip_vendor;
		ivideo->sisfb_infoblock.memory = ivideo->video_size / 1024;
		ivideo->sisfb_infoblock.heapstart = ivideo->heapstart / 1024;
L
Linus Torvalds 已提交
1814
		if(ivideo->modechanged) {
T
Thomas Winischhofer 已提交
1815
			ivideo->sisfb_infoblock.fbvidmode = ivideo->mode_no;
L
Linus Torvalds 已提交
1816
		} else {
T
Thomas Winischhofer 已提交
1817
			ivideo->sisfb_infoblock.fbvidmode = ivideo->modeprechange;
L
Linus Torvalds 已提交
1818
		}
T
Thomas Winischhofer 已提交
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851
		ivideo->sisfb_infoblock.sisfb_caps = ivideo->caps;
		ivideo->sisfb_infoblock.sisfb_tqlen = ivideo->cmdQueueSize / 1024;
		ivideo->sisfb_infoblock.sisfb_pcibus = ivideo->pcibus;
		ivideo->sisfb_infoblock.sisfb_pcislot = ivideo->pcislot;
		ivideo->sisfb_infoblock.sisfb_pcifunc = ivideo->pcifunc;
		ivideo->sisfb_infoblock.sisfb_lcdpdc = ivideo->detectedpdc;
		ivideo->sisfb_infoblock.sisfb_lcdpdca = ivideo->detectedpdca;
		ivideo->sisfb_infoblock.sisfb_lcda = ivideo->detectedlcda;
		ivideo->sisfb_infoblock.sisfb_vbflags = ivideo->vbflags;
		ivideo->sisfb_infoblock.sisfb_currentvbflags = ivideo->currentvbflags;
		ivideo->sisfb_infoblock.sisfb_scalelcd = ivideo->SiS_Pr.UsePanelScaler;
		ivideo->sisfb_infoblock.sisfb_specialtiming = ivideo->SiS_Pr.SiS_CustomT;
		ivideo->sisfb_infoblock.sisfb_haveemi = ivideo->SiS_Pr.HaveEMI ? 1 : 0;
		ivideo->sisfb_infoblock.sisfb_haveemilcd = ivideo->SiS_Pr.HaveEMILCD ? 1 : 0;
		ivideo->sisfb_infoblock.sisfb_emi30 = ivideo->SiS_Pr.EMI_30;
		ivideo->sisfb_infoblock.sisfb_emi31 = ivideo->SiS_Pr.EMI_31;
		ivideo->sisfb_infoblock.sisfb_emi32 = ivideo->SiS_Pr.EMI_32;
		ivideo->sisfb_infoblock.sisfb_emi33 = ivideo->SiS_Pr.EMI_33;
		ivideo->sisfb_infoblock.sisfb_tvxpos = (u16)(ivideo->tvxpos + 32);
		ivideo->sisfb_infoblock.sisfb_tvypos = (u16)(ivideo->tvypos + 32);
		ivideo->sisfb_infoblock.sisfb_heapsize = ivideo->sisfb_heap_size / 1024;
		ivideo->sisfb_infoblock.sisfb_videooffset = ivideo->video_offset;
		ivideo->sisfb_infoblock.sisfb_curfstn = ivideo->curFSTN;
		ivideo->sisfb_infoblock.sisfb_curdstn = ivideo->curDSTN;
		ivideo->sisfb_infoblock.sisfb_vbflags2 = ivideo->vbflags2;
		ivideo->sisfb_infoblock.sisfb_can_post = ivideo->sisfb_can_post ? 1 : 0;
		ivideo->sisfb_infoblock.sisfb_card_posted = ivideo->sisfb_card_posted ? 1 : 0;
		ivideo->sisfb_infoblock.sisfb_was_boot_device = ivideo->sisfb_was_boot_device ? 1 : 0;

		if(copy_to_user((void __user *)arg, &ivideo->sisfb_infoblock,
						sizeof(ivideo->sisfb_infoblock)))
			return -EFAULT;

L
Linus Torvalds 已提交
1852 1853 1854
	        break;

	   case SISFB_GET_VBRSTATUS_OLD:
T
Thomas Winischhofer 已提交
1855 1856 1857
		if(ivideo->warncount++ < 10)
			printk(KERN_INFO
				"sisfb: Deprecated ioctl call received - update your application!\n");
L
Linus Torvalds 已提交
1858
	   case SISFB_GET_VBRSTATUS:
T
Thomas Winischhofer 已提交
1859
		if(sisfb_CheckVBRetrace(ivideo))
L
Linus Torvalds 已提交
1860
			return put_user((u32)1, argp);
T
Thomas Winischhofer 已提交
1861
		else
L
Linus Torvalds 已提交
1862 1863 1864
			return put_user((u32)0, argp);

	   case SISFB_GET_AUTOMAXIMIZE_OLD:
T
Thomas Winischhofer 已提交
1865 1866 1867
		if(ivideo->warncount++ < 10)
			printk(KERN_INFO
				"sisfb: Deprecated ioctl call received - update your application!\n");
L
Linus Torvalds 已提交
1868
	   case SISFB_GET_AUTOMAXIMIZE:
T
Thomas Winischhofer 已提交
1869 1870 1871 1872
		if(ivideo->sisfb_max)
			return put_user((u32)1, argp);
		else
			return put_user((u32)0, argp);
L
Linus Torvalds 已提交
1873 1874

	   case SISFB_SET_AUTOMAXIMIZE_OLD:
T
Thomas Winischhofer 已提交
1875 1876 1877
		if(ivideo->warncount++ < 10)
			printk(KERN_INFO
				"sisfb: Deprecated ioctl call received - update your application!\n");
L
Linus Torvalds 已提交
1878
	   case SISFB_SET_AUTOMAXIMIZE:
T
Thomas Winischhofer 已提交
1879
		if(get_user(gpu32, argp))
L
Linus Torvalds 已提交
1880
			return -EFAULT;
T
Thomas Winischhofer 已提交
1881

L
Linus Torvalds 已提交
1882 1883 1884 1885
		ivideo->sisfb_max = (gpu32) ? 1 : 0;
		break;

	   case SISFB_SET_TVPOSOFFSET:
T
Thomas Winischhofer 已提交
1886
		if(get_user(gpu32, argp))
L
Linus Torvalds 已提交
1887
			return -EFAULT;
T
Thomas Winischhofer 已提交
1888

L
Linus Torvalds 已提交
1889 1890 1891 1892 1893
		sisfb_set_TVxposoffset(ivideo, ((int)(gpu32 >> 16)) - 32);
		sisfb_set_TVyposoffset(ivideo, ((int)(gpu32 & 0xffff)) - 32);
		break;

	   case SISFB_GET_TVPOSOFFSET:
T
Thomas Winischhofer 已提交
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908
		return put_user((u32)(((ivideo->tvxpos+32)<<16)|((ivideo->tvypos+32)&0xffff)),
							argp);

	   case SISFB_COMMAND:
		if(copy_from_user(&ivideo->sisfb_command, (void __user *)arg,
							sizeof(struct sisfb_cmd)))
			return -EFAULT;

		sisfb_handle_command(ivideo, &ivideo->sisfb_command);

		if(copy_to_user((void __user *)arg, &ivideo->sisfb_command,
							sizeof(struct sisfb_cmd)))
			return -EFAULT;

		break;
L
Linus Torvalds 已提交
1909 1910

	   case SISFB_SET_LOCK:
T
Thomas Winischhofer 已提交
1911
		if(get_user(gpu32, argp))
L
Linus Torvalds 已提交
1912
			return -EFAULT;
T
Thomas Winischhofer 已提交
1913

L
Linus Torvalds 已提交
1914 1915 1916 1917
		ivideo->sisfblocked = (gpu32) ? 1 : 0;
		break;

	   default:
T
Thomas Winischhofer 已提交
1918
#ifdef SIS_NEW_CONFIG_COMPAT
L
Linus Torvalds 已提交
1919
		return -ENOIOCTLCMD;
T
Thomas Winischhofer 已提交
1920 1921 1922
#else
		return -EINVAL;
#endif
L
Linus Torvalds 已提交
1923 1924 1925 1926
	}
	return 0;
}

T
Thomas Winischhofer 已提交
1927 1928 1929
#ifdef SIS_NEW_CONFIG_COMPAT
static long
sisfb_compat_ioctl(struct file *f, unsigned int cmd, unsigned long arg, struct fb_info *info)
L
Linus Torvalds 已提交
1930 1931
{
	int ret;
T
Thomas Winischhofer 已提交
1932

L
Linus Torvalds 已提交
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948
	lock_kernel();
	ret = sisfb_ioctl(NULL, f, cmd, arg, info);
	unlock_kernel();
	return ret;
}
#endif

static int
sisfb_get_fix(struct fb_fix_screeninfo *fix, int con, struct fb_info *info)
{
	struct sis_video_info *ivideo = (struct sis_video_info *)info->par;

	memset(fix, 0, sizeof(struct fb_fix_screeninfo));

	strcpy(fix->id, ivideo->myid);

T
Thomas Winischhofer 已提交
1949
	fix->smem_start  = ivideo->video_base + ivideo->video_offset;
L
Linus Torvalds 已提交
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
	fix->smem_len    = ivideo->sisfb_mem;
	fix->type        = FB_TYPE_PACKED_PIXELS;
	fix->type_aux    = 0;
	fix->visual      = (ivideo->video_bpp == 8) ? FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
	fix->xpanstep    = 1;
	fix->ypanstep 	 = (ivideo->sisfb_ypan) ? 1 : 0;
	fix->ywrapstep   = 0;
	fix->line_length = ivideo->video_linelength;
	fix->mmio_start  = ivideo->mmio_base;
	fix->mmio_len    = ivideo->mmio_size;
	if(ivideo->sisvga_engine == SIS_300_VGA) {
T
Thomas Winischhofer 已提交
1961 1962 1963 1964 1965 1966 1967 1968 1969
		fix->accel = FB_ACCEL_SIS_GLAMOUR;
	} else if((ivideo->chip == SIS_330) ||
		  (ivideo->chip == SIS_760) ||
		  (ivideo->chip == SIS_761)) {
		fix->accel = FB_ACCEL_SIS_XABRE;
	} else if(ivideo->chip == XGI_20) {
		fix->accel = FB_ACCEL_XGI_VOLARI_Z;
	} else if(ivideo->chip >= XGI_40) {
		fix->accel = FB_ACCEL_XGI_VOLARI_V;
L
Linus Torvalds 已提交
1970
	} else {
T
Thomas Winischhofer 已提交
1971
		fix->accel = FB_ACCEL_SIS_GLAMOUR_2;
L
Linus Torvalds 已提交
1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986
	}

	return 0;
}

/* ----------------  fb_ops structures ----------------- */

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
static struct fb_ops sisfb_ops = {
	.owner		= THIS_MODULE,
	.fb_get_fix	= sisfb_get_fix,
	.fb_get_var	= sisfb_get_var,
	.fb_set_var	= sisfb_set_var,
	.fb_get_cmap	= sisfb_get_cmap,
	.fb_set_cmap	= sisfb_set_cmap,
T
Thomas Winischhofer 已提交
1987
	.fb_pan_display = sisfb_pan_display,
L
Linus Torvalds 已提交
1988 1989 1990 1991 1992 1993
	.fb_ioctl	= sisfb_ioctl
};
#endif

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
static struct fb_ops sisfb_ops = {
T
Thomas Winischhofer 已提交
1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
	.owner		= THIS_MODULE,
	.fb_open	= sisfb_open,
	.fb_release	= sisfb_release,
	.fb_check_var	= sisfb_check_var,
	.fb_set_par	= sisfb_set_par,
	.fb_setcolreg	= sisfb_setcolreg,
	.fb_pan_display	= sisfb_pan_display,
	.fb_blank	= sisfb_blank,
	.fb_fillrect	= fbcon_sis_fillrect,
	.fb_copyarea	= fbcon_sis_copyarea,
	.fb_imageblit	= cfb_imageblit,
2005
#ifdef CONFIG_FB_SOFT_CURSOR
T
Thomas Winischhofer 已提交
2006
	.fb_cursor	= soft_cursor,
2007
#endif
T
Thomas Winischhofer 已提交
2008 2009 2010
	.fb_sync	= fbcon_sis_sync,
#ifdef SIS_NEW_CONFIG_COMPAT
	.fb_compat_ioctl= sisfb_compat_ioctl,
L
Linus Torvalds 已提交
2011
#endif
T
Thomas Winischhofer 已提交
2012
	.fb_ioctl	= sisfb_ioctl
L
Linus Torvalds 已提交
2013 2014 2015 2016 2017
};
#endif

/* ---------------- Chip generation dependent routines ---------------- */

T
Thomas Winischhofer 已提交
2018 2019
static struct pci_dev * __devinit
sisfb_get_northbridge(int basechipid)
L
Linus Torvalds 已提交
2020 2021 2022
{
	struct pci_dev *pdev = NULL;
	int nbridgenum, nbridgeidx, i;
T
Thomas Winischhofer 已提交
2023
	static const unsigned short nbridgeids[] = {
L
Linus Torvalds 已提交
2024 2025 2026 2027 2028 2029 2030
		PCI_DEVICE_ID_SI_540,	/* for SiS 540 VGA */
		PCI_DEVICE_ID_SI_630,	/* for SiS 630/730 VGA */
		PCI_DEVICE_ID_SI_730,
		PCI_DEVICE_ID_SI_550,   /* for SiS 550 VGA */
		PCI_DEVICE_ID_SI_650,   /* for SiS 650/651/740 VGA */
		PCI_DEVICE_ID_SI_651,
		PCI_DEVICE_ID_SI_740,
T
Thomas Winischhofer 已提交
2031
		PCI_DEVICE_ID_SI_661,	/* for SiS 661/741/660/760/761 VGA */
L
Linus Torvalds 已提交
2032 2033
		PCI_DEVICE_ID_SI_741,
		PCI_DEVICE_ID_SI_660,
T
Thomas Winischhofer 已提交
2034 2035
		PCI_DEVICE_ID_SI_760,
		PCI_DEVICE_ID_SI_761
L
Linus Torvalds 已提交
2036 2037
	};

T
Thomas Winischhofer 已提交
2038
	switch(basechipid) {
L
Linus Torvalds 已提交
2039 2040 2041 2042 2043 2044 2045
#ifdef CONFIG_FB_SIS_300
	case SIS_540:	nbridgeidx = 0; nbridgenum = 1; break;
	case SIS_630:	nbridgeidx = 1; nbridgenum = 2; break;
#endif
#ifdef CONFIG_FB_SIS_315
	case SIS_550:   nbridgeidx = 3; nbridgenum = 1; break;
	case SIS_650:	nbridgeidx = 4; nbridgenum = 3; break;
T
Thomas Winischhofer 已提交
2046
	case SIS_660:	nbridgeidx = 7; nbridgenum = 5; break;
L
Linus Torvalds 已提交
2047 2048 2049 2050
#endif
	default:	return NULL;
	}
	for(i = 0; i < nbridgenum; i++) {
T
Thomas Winischhofer 已提交
2051 2052 2053
		if((pdev = SIS_PCI_GET_DEVICE(PCI_VENDOR_ID_SI,
				nbridgeids[nbridgeidx+i], NULL)))
			break;
L
Linus Torvalds 已提交
2054 2055 2056 2057
	}
	return pdev;
}

T
Thomas Winischhofer 已提交
2058 2059
static int __devinit
sisfb_get_dram_size(struct sis_video_info *ivideo)
L
Linus Torvalds 已提交
2060 2061 2062 2063 2064 2065
{
#if defined(CONFIG_FB_SIS_300) || defined(CONFIG_FB_SIS_315)
	u8 reg;
#endif

	ivideo->video_size = 0;
T
Thomas Winischhofer 已提交
2066
	ivideo->UMAsize = ivideo->LFBsize = 0;
L
Linus Torvalds 已提交
2067 2068 2069 2070

	switch(ivideo->chip) {
#ifdef CONFIG_FB_SIS_300
	case SIS_300:
T
Thomas Winischhofer 已提交
2071
		inSISIDXREG(SISSR, 0x14, reg);
L
Linus Torvalds 已提交
2072 2073 2074 2075 2076
		ivideo->video_size = ((reg & 0x3F) + 1) << 20;
		break;
	case SIS_540:
	case SIS_630:
	case SIS_730:
T
Thomas Winischhofer 已提交
2077 2078 2079
		if(!ivideo->nbridge)
			return -1;
		pci_read_config_byte(ivideo->nbridge, 0x63, &reg);
L
Linus Torvalds 已提交
2080 2081 2082 2083 2084 2085 2086
		ivideo->video_size = 1 << (((reg & 0x70) >> 4) + 21);
		break;
#endif
#ifdef CONFIG_FB_SIS_315
	case SIS_315H:
	case SIS_315PRO:
	case SIS_315:
T
Thomas Winischhofer 已提交
2087
		inSISIDXREG(SISSR, 0x14, reg);
L
Linus Torvalds 已提交
2088 2089 2090 2091
		ivideo->video_size = (1 << ((reg & 0xf0) >> 4)) << 20;
		switch((reg >> 2) & 0x03) {
		case 0x01:
		case 0x03:
T
Thomas Winischhofer 已提交
2092 2093
			ivideo->video_size <<= 1;
			break;
L
Linus Torvalds 已提交
2094
		case 0x02:
T
Thomas Winischhofer 已提交
2095
			ivideo->video_size += (ivideo->video_size/2);
L
Linus Torvalds 已提交
2096
		}
T
Thomas Winischhofer 已提交
2097
		break;
L
Linus Torvalds 已提交
2098
	case SIS_330:
T
Thomas Winischhofer 已提交
2099
		inSISIDXREG(SISSR, 0x14, reg);
L
Linus Torvalds 已提交
2100 2101
		ivideo->video_size = (1 << ((reg & 0xf0) >> 4)) << 20;
		if(reg & 0x0c) ivideo->video_size <<= 1;
T
Thomas Winischhofer 已提交
2102
		break;
L
Linus Torvalds 已提交
2103 2104 2105
	case SIS_550:
	case SIS_650:
	case SIS_740:
T
Thomas Winischhofer 已提交
2106
		inSISIDXREG(SISSR, 0x14, reg);
L
Linus Torvalds 已提交
2107 2108 2109 2110
		ivideo->video_size = (((reg & 0x3f) + 1) << 2) << 20;
		break;
	case SIS_661:
	case SIS_741:
T
Thomas Winischhofer 已提交
2111
		inSISIDXREG(SISCR, 0x79, reg);
L
Linus Torvalds 已提交
2112
		ivideo->video_size = (1 << ((reg & 0xf0) >> 4)) << 20;
T
Thomas Winischhofer 已提交
2113
		break;
L
Linus Torvalds 已提交
2114 2115
	case SIS_660:
	case SIS_760:
T
Thomas Winischhofer 已提交
2116
	case SIS_761:
L
Linus Torvalds 已提交
2117 2118
		inSISIDXREG(SISCR, 0x79, reg);
		reg = (reg & 0xf0) >> 4;
T
Thomas Winischhofer 已提交
2119 2120 2121 2122
		if(reg)	{
			ivideo->video_size = (1 << reg) << 20;
			ivideo->UMAsize = ivideo->video_size;
		}
L
Linus Torvalds 已提交
2123 2124 2125
		inSISIDXREG(SISCR, 0x78, reg);
		reg &= 0x30;
		if(reg) {
T
Thomas Winischhofer 已提交
2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146
			if(reg == 0x10) {
				ivideo->LFBsize = (32 << 20);
			} else {
				ivideo->LFBsize = (64 << 20);
			}
			ivideo->video_size += ivideo->LFBsize;
		}
		break;
	case SIS_340:
	case XGI_20:
	case XGI_40:
		inSISIDXREG(SISSR, 0x14, reg);
		ivideo->video_size = (1 << ((reg & 0xf0) >> 4)) << 20;
		if(ivideo->chip != XGI_20) {
			reg = (reg & 0x0c) >> 2;
			if(ivideo->revision_id == 2) {
				if(reg & 0x01) reg = 0x02;
				else	       reg = 0x00;
			}
			if(reg == 0x02)		ivideo->video_size <<= 1;
			else if(reg == 0x03)	ivideo->video_size <<= 2;
L
Linus Torvalds 已提交
2147
		}
T
Thomas Winischhofer 已提交
2148
		break;
L
Linus Torvalds 已提交
2149 2150 2151 2152 2153 2154 2155 2156 2157
#endif
	default:
		return -1;
	}
	return 0;
}

/* -------------- video bridge device detection --------------- */

T
Thomas Winischhofer 已提交
2158 2159
static void __devinit
sisfb_detect_VB_connect(struct sis_video_info *ivideo)
L
Linus Torvalds 已提交
2160 2161 2162
{
	u8 cr32, temp;

T
Thomas Winischhofer 已提交
2163 2164 2165 2166 2167 2168
	/* No CRT2 on XGI Z7 */
	if(ivideo->chip == XGI_20) {
		ivideo->sisfb_crt1off = 0;
		return;
	}

L
Linus Torvalds 已提交
2169 2170 2171 2172 2173 2174
#ifdef CONFIG_FB_SIS_300
	if(ivideo->sisvga_engine == SIS_300_VGA) {
		inSISIDXREG(SISSR, 0x17, temp);
		if((temp & 0x0F) && (ivideo->chip != SIS_300)) {
			/* PAL/NTSC is stored on SR16 on such machines */
			if(!(ivideo->vbflags & (TV_PAL | TV_NTSC | TV_PALM | TV_PALN))) {
T
Thomas Winischhofer 已提交
2175
				inSISIDXREG(SISSR, 0x16, temp);
L
Linus Torvalds 已提交
2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209
				if(temp & 0x20)
					ivideo->vbflags |= TV_PAL;
				else
					ivideo->vbflags |= TV_NTSC;
			}
		}
	}
#endif

	inSISIDXREG(SISCR, 0x32, cr32);

	if(cr32 & SIS_CRT1) {
		ivideo->sisfb_crt1off = 0;
	} else {
		ivideo->sisfb_crt1off = (cr32 & 0xDF) ? 1 : 0;
	}

	ivideo->vbflags &= ~(CRT2_TV | CRT2_LCD | CRT2_VGA);

	if(cr32 & SIS_VB_TV)   ivideo->vbflags |= CRT2_TV;
	if(cr32 & SIS_VB_LCD)  ivideo->vbflags |= CRT2_LCD;
	if(cr32 & SIS_VB_CRT2) ivideo->vbflags |= CRT2_VGA;

	/* Check given parms for hardware compatibility.
	 * (Cannot do this in the search_xx routines since we don't
	 * know what hardware we are running on then)
	 */

	if(ivideo->chip != SIS_550) {
	   ivideo->sisfb_dstn = ivideo->sisfb_fstn = 0;
	}

	if(ivideo->sisfb_tvplug != -1) {
	   if( (ivideo->sisvga_engine != SIS_315_VGA) ||
T
Thomas Winischhofer 已提交
2210
	       (!(ivideo->vbflags2 & VB2_SISYPBPRBRIDGE)) ) {
L
Linus Torvalds 已提交
2211
	      if(ivideo->sisfb_tvplug & TV_YPBPR) {
T
Thomas Winischhofer 已提交
2212
		 ivideo->sisfb_tvplug = -1;
L
Linus Torvalds 已提交
2213 2214 2215 2216 2217 2218
		 printk(KERN_ERR "sisfb: YPbPr not supported\n");
	      }
	   }
	}
	if(ivideo->sisfb_tvplug != -1) {
	   if( (ivideo->sisvga_engine != SIS_315_VGA) ||
T
Thomas Winischhofer 已提交
2219
	       (!(ivideo->vbflags2 & VB2_SISHIVISIONBRIDGE)) ) {
L
Linus Torvalds 已提交
2220
	      if(ivideo->sisfb_tvplug & TV_HIVISION) {
T
Thomas Winischhofer 已提交
2221
		 ivideo->sisfb_tvplug = -1;
L
Linus Torvalds 已提交
2222 2223 2224 2225 2226
		 printk(KERN_ERR "sisfb: HiVision not supported\n");
	      }
	   }
	}
	if(ivideo->sisfb_tvstd != -1) {
T
Thomas Winischhofer 已提交
2227 2228 2229
	   if( (!(ivideo->vbflags2 & VB2_SISBRIDGE)) &&
	       (!((ivideo->sisvga_engine == SIS_315_VGA) &&
			(ivideo->vbflags2 & VB2_CHRONTEL))) ) {
L
Linus Torvalds 已提交
2230
	      if(ivideo->sisfb_tvstd & (TV_PALN | TV_PALN | TV_NTSCJ)) {
T
Thomas Winischhofer 已提交
2231 2232
		 ivideo->sisfb_tvstd = -1;
		 printk(KERN_ERR "sisfb: PALM/PALN/NTSCJ not supported\n");
L
Linus Torvalds 已提交
2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243
	      }
	   }
	}

	/* Detect/set TV plug & type */
	if(ivideo->sisfb_tvplug != -1) {
		ivideo->vbflags |= ivideo->sisfb_tvplug;
	} else {
		if(cr32 & SIS_VB_YPBPR)     	 ivideo->vbflags |= (TV_YPBPR|TV_YPBPR525I); /* default: 480i */
		else if(cr32 & SIS_VB_HIVISION)  ivideo->vbflags |= TV_HIVISION;
		else if(cr32 & SIS_VB_SCART)     ivideo->vbflags |= TV_SCART;
T
Thomas Winischhofer 已提交
2244
		else {
L
Linus Torvalds 已提交
2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260
			if(cr32 & SIS_VB_SVIDEO)    ivideo->vbflags |= TV_SVIDEO;
			if(cr32 & SIS_VB_COMPOSITE) ivideo->vbflags |= TV_AVIDEO;
		}
	}

	if(!(ivideo->vbflags & (TV_YPBPR | TV_HIVISION))) {
	    if(ivideo->sisfb_tvstd != -1) {
	       ivideo->vbflags &= ~(TV_NTSC | TV_PAL | TV_PALM | TV_PALN | TV_NTSCJ);
	       ivideo->vbflags |= ivideo->sisfb_tvstd;
	    }
	    if(ivideo->vbflags & TV_SCART) {
	       ivideo->vbflags &= ~(TV_NTSC | TV_PALM | TV_PALN | TV_NTSCJ);
	       ivideo->vbflags |= TV_PAL;
	    }
	    if(!(ivideo->vbflags & (TV_PAL | TV_NTSC | TV_PALM | TV_PALN | TV_NTSCJ))) {
		if(ivideo->sisvga_engine == SIS_300_VGA) {
T
Thomas Winischhofer 已提交
2261
			inSISIDXREG(SISSR, 0x38, temp);
L
Linus Torvalds 已提交
2262 2263 2264
			if(temp & 0x01) ivideo->vbflags |= TV_PAL;
			else		ivideo->vbflags |= TV_NTSC;
		} else if((ivideo->chip <= SIS_315PRO) || (ivideo->chip >= SIS_330)) {
T
Thomas Winischhofer 已提交
2265
			inSISIDXREG(SISSR, 0x38, temp);
L
Linus Torvalds 已提交
2266 2267
			if(temp & 0x01) ivideo->vbflags |= TV_PAL;
			else		ivideo->vbflags |= TV_NTSC;
T
Thomas Winischhofer 已提交
2268 2269
		} else {
			inSISIDXREG(SISCR, 0x79, temp);
L
Linus Torvalds 已提交
2270 2271
			if(temp & 0x20)	ivideo->vbflags |= TV_PAL;
			else		ivideo->vbflags |= TV_NTSC;
T
Thomas Winischhofer 已提交
2272
		}
L
Linus Torvalds 已提交
2273 2274 2275 2276
	    }
	}

	/* Copy forceCRT1 option to CRT1off if option is given */
T
Thomas Winischhofer 已提交
2277 2278
	if(ivideo->sisfb_forcecrt1 != -1) {
	   ivideo->sisfb_crt1off = (ivideo->sisfb_forcecrt1) ? 0 : 1;
L
Linus Torvalds 已提交
2279 2280 2281 2282 2283
	}
}

/* ------------------ Sensing routines ------------------ */

T
Thomas Winischhofer 已提交
2284 2285
static BOOLEAN __devinit
sisfb_test_DDC1(struct sis_video_info *ivideo)
L
Linus Torvalds 已提交
2286 2287 2288 2289 2290 2291
{
    unsigned short old;
    int count = 48;

    old = SiS_ReadDDC1Bit(&ivideo->SiS_Pr);
    do {
T
Thomas Winischhofer 已提交
2292
	if(old != SiS_ReadDDC1Bit(&ivideo->SiS_Pr)) break;
L
Linus Torvalds 已提交
2293 2294 2295 2296
    } while(count--);
    return (count == -1) ? FALSE : TRUE;
}

T
Thomas Winischhofer 已提交
2297 2298
static void __devinit
sisfb_sense_crt1(struct sis_video_info *ivideo)
L
Linus Torvalds 已提交
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
{
    BOOLEAN mustwait = FALSE;
    u8  sr1F, cr17;
#ifdef CONFIG_FB_SIS_315
    u8  cr63=0;
#endif
    u16 temp = 0xffff;
    int i;

    inSISIDXREG(SISSR,0x1F,sr1F);
    orSISIDXREG(SISSR,0x1F,0x04);
    andSISIDXREG(SISSR,0x1F,0x3F);
    if(sr1F & 0xc0) mustwait = TRUE;

#ifdef CONFIG_FB_SIS_315
    if(ivideo->sisvga_engine == SIS_315_VGA) {
       inSISIDXREG(SISCR,ivideo->SiS_Pr.SiS_MyCR63,cr63);
       cr63 &= 0x40;
       andSISIDXREG(SISCR,ivideo->SiS_Pr.SiS_MyCR63,0xBF);
    }
#endif

    inSISIDXREG(SISCR,0x17,cr17);
    cr17 &= 0x80;
    if(!cr17) {
       orSISIDXREG(SISCR,0x17,0x80);
       mustwait = TRUE;
       outSISIDXREG(SISSR, 0x00, 0x01);
       outSISIDXREG(SISSR, 0x00, 0x03);
    }

    if(mustwait) {
       for(i=0; i < 10; i++) sisfbwaitretracecrt1(ivideo);
    }

#ifdef CONFIG_FB_SIS_315
    if(ivideo->chip >= SIS_330) {
       andSISIDXREG(SISCR,0x32,~0x20);
       if(ivideo->chip >= SIS_340) {
          outSISIDXREG(SISCR, 0x57, 0x4a);
       } else {
          outSISIDXREG(SISCR, 0x57, 0x5f);
       }
       orSISIDXREG(SISCR, 0x53, 0x02);
       while((inSISREG(SISINPSTAT)) & 0x01)    break;
       while(!((inSISREG(SISINPSTAT)) & 0x01)) break;
       if((inSISREG(SISMISCW)) & 0x10) temp = 1;
       andSISIDXREG(SISCR, 0x53, 0xfd);
       andSISIDXREG(SISCR, 0x57, 0x00);
    }
#endif

    if(temp == 0xffff) {
       i = 3;
       do {
T
Thomas Winischhofer 已提交
2354 2355
	  temp = SiS_HandleDDC(&ivideo->SiS_Pr, ivideo->vbflags,
		ivideo->sisvga_engine, 0, 0, NULL, ivideo->vbflags2);
L
Linus Torvalds 已提交
2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378
       } while(((temp == 0) || (temp == 0xffff)) && i--);

       if((temp == 0) || (temp == 0xffff)) {
          if(sisfb_test_DDC1(ivideo)) temp = 1;
       }
    }

    if((temp) && (temp != 0xffff)) {
       orSISIDXREG(SISCR,0x32,0x20);
    }

#ifdef CONFIG_FB_SIS_315
    if(ivideo->sisvga_engine == SIS_315_VGA) {
       setSISIDXREG(SISCR,ivideo->SiS_Pr.SiS_MyCR63,0xBF,cr63);
    }
#endif

    setSISIDXREG(SISCR,0x17,0x7F,cr17);

    outSISIDXREG(SISSR,0x1F,sr1F);
}

/* Determine and detect attached devices on SiS30x */
T
Thomas Winischhofer 已提交
2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468
static void __devinit
SiS_SenseLCD(struct sis_video_info *ivideo)
{
	unsigned char buffer[256];
	unsigned short temp, realcrtno, i;
	u8 reg, cr37 = 0, paneltype = 0;
	u16 xres, yres;

	ivideo->SiS_Pr.PanelSelfDetected = FALSE;

	/* LCD detection only for TMDS bridges */
	if(!(ivideo->vbflags2 & VB2_SISTMDSBRIDGE))
		return;
	if(ivideo->vbflags2 & VB2_30xBDH)
		return;

	/* If LCD already set up by BIOS, skip it */
	inSISIDXREG(SISCR, 0x32, reg);
	if(reg & 0x08)
		return;

	realcrtno = 1;
	if(ivideo->SiS_Pr.DDCPortMixup)
		realcrtno = 0;

	/* Check DDC capabilities */
	temp = SiS_HandleDDC(&ivideo->SiS_Pr, ivideo->vbflags, ivideo->sisvga_engine,
				realcrtno, 0, &buffer[0], ivideo->vbflags2);

	if((!temp) || (temp == 0xffff) || (!(temp & 0x02)))
		return;

	/* Read DDC data */
	i = 3;  /* Number of retrys */
	do {
		temp = SiS_HandleDDC(&ivideo->SiS_Pr, ivideo->vbflags,
				ivideo->sisvga_engine, realcrtno, 1,
				&buffer[0], ivideo->vbflags2);
	} while((temp) && i--);

	if(temp)
		return;

	/* No digital device */
	if(!(buffer[0x14] & 0x80))
		return;

	/* First detailed timing preferred timing? */
	if(!(buffer[0x18] & 0x02))
		return;

	xres = buffer[0x38] | ((buffer[0x3a] & 0xf0) << 4);
	yres = buffer[0x3b] | ((buffer[0x3d] & 0xf0) << 4);

	switch(xres) {
		case 1024:
			if(yres == 768)
				paneltype = 0x02;
			break;
		case 1280:
			if(yres == 1024)
				paneltype = 0x03;
			break;
		case 1600:
			if((yres == 1200) && (ivideo->vbflags2 & VB2_30xC))
				paneltype = 0x0b;
			break;
	}

	if(!paneltype)
		return;

	if(buffer[0x23])
		cr37 |= 0x10;

	if((buffer[0x47] & 0x18) == 0x18)
		cr37 |= ((((buffer[0x47] & 0x06) ^ 0x06) << 5) | 0x20);
	else
		cr37 |= 0xc0;

	outSISIDXREG(SISCR, 0x36, paneltype);
	cr37 &= 0xf1;
	setSISIDXREG(SISCR, 0x37, 0x0c, cr37);
	orSISIDXREG(SISCR, 0x32, 0x08);

	ivideo->SiS_Pr.PanelSelfDetected = TRUE;
}

static int __devinit
SISDoSense(struct sis_video_info *ivideo, u16 type, u16 test)
L
Linus Torvalds 已提交
2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493
{
    int temp, mytest, result, i, j;

    for(j = 0; j < 10; j++) {
       result = 0;
       for(i = 0; i < 3; i++) {
          mytest = test;
          outSISIDXREG(SISPART4,0x11,(type & 0x00ff));
          temp = (type >> 8) | (mytest & 0x00ff);
          setSISIDXREG(SISPART4,0x10,0xe0,temp);
          SiS_DDC2Delay(&ivideo->SiS_Pr, 0x1500);
          mytest >>= 8;
          mytest &= 0x7f;
          inSISIDXREG(SISPART4,0x03,temp);
          temp ^= 0x0e;
          temp &= mytest;
          if(temp == mytest) result++;
#if 1
	  outSISIDXREG(SISPART4,0x11,0x00);
	  andSISIDXREG(SISPART4,0x10,0xe0);
	  SiS_DDC2Delay(&ivideo->SiS_Pr, 0x1000);
#endif
       }
       if((result == 0) || (result >= 2)) break;
    }
T
Thomas Winischhofer 已提交
2494
    return result;
L
Linus Torvalds 已提交
2495 2496
}

T
Thomas Winischhofer 已提交
2497 2498
static void __devinit
SiS_Sense30x(struct sis_video_info *ivideo)
L
Linus Torvalds 已提交
2499 2500 2501 2502 2503 2504 2505 2506 2507
{
    u8  backupP4_0d,backupP2_00,backupP2_4d,backupSR_1e,biosflag=0;
    u16 svhs=0, svhs_c=0;
    u16 cvbs=0, cvbs_c=0;
    u16 vga2=0, vga2_c=0;
    int myflag, result;
    char stdstr[] = "sisfb: Detected";
    char tvstr[]  = "TV connected to";

T
Thomas Winischhofer 已提交
2508
    if(ivideo->vbflags2 & VB2_301) {
L
Linus Torvalds 已提交
2509 2510 2511 2512 2513
       svhs = 0x00b9; cvbs = 0x00b3; vga2 = 0x00d1;
       inSISIDXREG(SISPART4,0x01,myflag);
       if(myflag & 0x04) {
	  svhs = 0x00dd; cvbs = 0x00ee; vga2 = 0x00fd;
       }
T
Thomas Winischhofer 已提交
2514
    } else if(ivideo->vbflags2 & (VB2_301B | VB2_302B)) {
L
Linus Torvalds 已提交
2515
       svhs = 0x016b; cvbs = 0x0174; vga2 = 0x0190;
T
Thomas Winischhofer 已提交
2516
    } else if(ivideo->vbflags2 & (VB2_301LV | VB2_302LV)) {
L
Linus Torvalds 已提交
2517
       svhs = 0x0200; cvbs = 0x0100;
T
Thomas Winischhofer 已提交
2518
    } else if(ivideo->vbflags2 & (VB2_301C | VB2_302ELV | VB2_307T | VB2_307LV)) {
L
Linus Torvalds 已提交
2519
       svhs = 0x016b; cvbs = 0x0110; vga2 = 0x0190;
T
Thomas Winischhofer 已提交
2520 2521
    } else
       return;
L
Linus Torvalds 已提交
2522 2523

    vga2_c = 0x0e08; svhs_c = 0x0404; cvbs_c = 0x0804;
T
Thomas Winischhofer 已提交
2524
    if(ivideo->vbflags & (VB2_301LV|VB2_302LV|VB2_302ELV|VB2_307LV)) {
L
Linus Torvalds 已提交
2525 2526
       svhs_c = 0x0408; cvbs_c = 0x0808;
    }
T
Thomas Winischhofer 已提交
2527

L
Linus Torvalds 已提交
2528
    biosflag = 2;
T
Thomas Winischhofer 已提交
2529 2530 2531 2532 2533 2534 2535 2536 2537
    if(ivideo->haveXGIROM) {
       biosflag = ivideo->bios_abase[0x58] & 0x03;
    } else if(ivideo->newrom) {
       if(ivideo->bios_abase[0x5d] & 0x04) biosflag |= 0x01;
    } else if(ivideo->sisvga_engine == SIS_300_VGA) {
       if(ivideo->bios_abase) {
          biosflag = ivideo->bios_abase[0xfe] & 0x03;
       }
    }
L
Linus Torvalds 已提交
2538 2539 2540 2541 2542 2543

    if(ivideo->chip == SIS_300) {
       inSISIDXREG(SISSR,0x3b,myflag);
       if(!(myflag & 0x01)) vga2 = vga2_c = 0;
    }

T
Thomas Winischhofer 已提交
2544 2545 2546 2547
    if(!(ivideo->vbflags2 & VB2_SISVGA2BRIDGE)) {
       vga2 = vga2_c = 0;
    }

L
Linus Torvalds 已提交
2548 2549 2550 2551
    inSISIDXREG(SISSR,0x1e,backupSR_1e);
    orSISIDXREG(SISSR,0x1e,0x20);

    inSISIDXREG(SISPART4,0x0d,backupP4_0d);
T
Thomas Winischhofer 已提交
2552
    if(ivideo->vbflags2 & VB2_30xC) {
L
Linus Torvalds 已提交
2553 2554 2555 2556 2557 2558 2559 2560 2561 2562
       setSISIDXREG(SISPART4,0x0d,~0x07,0x01);
    } else {
       orSISIDXREG(SISPART4,0x0d,0x04);
    }
    SiS_DDC2Delay(&ivideo->SiS_Pr, 0x2000);

    inSISIDXREG(SISPART2,0x00,backupP2_00);
    outSISIDXREG(SISPART2,0x00,((backupP2_00 | 0x1c) & 0xfc));

    inSISIDXREG(SISPART2,0x4d,backupP2_4d);
T
Thomas Winischhofer 已提交
2563
    if(ivideo->vbflags2 & VB2_SISYPBPRBRIDGE) {
L
Linus Torvalds 已提交
2564 2565 2566
       outSISIDXREG(SISPART2,0x4d,(backupP2_4d & ~0x10));
    }

T
Thomas Winischhofer 已提交
2567
    if(!(ivideo->vbflags2 & VB2_30xCLV)) {
L
Linus Torvalds 已提交
2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586
       SISDoSense(ivideo, 0, 0);
    }

    andSISIDXREG(SISCR, 0x32, ~0x14);

    if(vga2_c || vga2) {
       if(SISDoSense(ivideo, vga2, vga2_c)) {
          if(biosflag & 0x01) {
	     printk(KERN_INFO "%s %s SCART output\n", stdstr, tvstr);
	     orSISIDXREG(SISCR, 0x32, 0x04);
	  } else {
	     printk(KERN_INFO "%s secondary VGA connection\n", stdstr);
	     orSISIDXREG(SISCR, 0x32, 0x10);
	  }
       }
    }

    andSISIDXREG(SISCR, 0x32, 0x3f);

T
Thomas Winischhofer 已提交
2587
    if(ivideo->vbflags2 & VB2_30xCLV) {
L
Linus Torvalds 已提交
2588 2589 2590
       orSISIDXREG(SISPART4,0x0d,0x04);
    }

T
Thomas Winischhofer 已提交
2591
    if((ivideo->sisvga_engine == SIS_315_VGA) && (ivideo->vbflags2 & VB2_SISYPBPRBRIDGE)) {
L
Linus Torvalds 已提交
2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623
       outSISIDXREG(SISPART2,0x4d,(backupP2_4d | 0x10));
       SiS_DDC2Delay(&ivideo->SiS_Pr, 0x2000);
       if((result = SISDoSense(ivideo, svhs, 0x0604))) {
          if((result = SISDoSense(ivideo, cvbs, 0x0804))) {
	     printk(KERN_INFO "%s %s YPbPr component output\n", stdstr, tvstr);
	     orSISIDXREG(SISCR,0x32,0x80);
	  }
       }
       outSISIDXREG(SISPART2,0x4d,backupP2_4d);
    }

    andSISIDXREG(SISCR, 0x32, ~0x03);

    if(!(ivideo->vbflags & TV_YPBPR)) {
       if((result = SISDoSense(ivideo, svhs, svhs_c))) {
          printk(KERN_INFO "%s %s SVIDEO output\n", stdstr, tvstr);
          orSISIDXREG(SISCR, 0x32, 0x02);
       }
       if((biosflag & 0x02) || (!result)) {
          if(SISDoSense(ivideo, cvbs, cvbs_c)) {
	     printk(KERN_INFO "%s %s COMPOSITE output\n", stdstr, tvstr);
	     orSISIDXREG(SISCR, 0x32, 0x01);
          }
       }
    }

    SISDoSense(ivideo, 0, 0);

    outSISIDXREG(SISPART2,0x00,backupP2_00);
    outSISIDXREG(SISPART4,0x0d,backupP4_0d);
    outSISIDXREG(SISSR,0x1e,backupSR_1e);

T
Thomas Winischhofer 已提交
2624
    if(ivideo->vbflags2 & VB2_30xCLV) {
L
Linus Torvalds 已提交
2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637
       inSISIDXREG(SISPART2,0x00,biosflag);
       if(biosflag & 0x20) {
          for(myflag = 2; myflag > 0; myflag--) {
	     biosflag ^= 0x20;
	     outSISIDXREG(SISPART2,0x00,biosflag);
	  }
       }
    }

    outSISIDXREG(SISPART2,0x00,backupP2_00);
}

/* Determine and detect attached TV's on Chrontel */
T
Thomas Winischhofer 已提交
2638 2639
static void __devinit
SiS_SenseCh(struct sis_video_info *ivideo)
L
Linus Torvalds 已提交
2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659
{
#if defined(CONFIG_FB_SIS_300) || defined(CONFIG_FB_SIS_315)
    u8 temp1, temp2;
    char stdstr[] = "sisfb: Chrontel: Detected TV connected to";
#endif
#ifdef CONFIG_FB_SIS_300
    unsigned char test[3];
    int i;
#endif

    if(ivideo->chip < SIS_315H) {

#ifdef CONFIG_FB_SIS_300
       ivideo->SiS_Pr.SiS_IF_DEF_CH70xx = 1;		/* Chrontel 700x */
       SiS_SetChrontelGPIO(&ivideo->SiS_Pr, 0x9c);	/* Set general purpose IO for Chrontel communication */
       SiS_DDC2Delay(&ivideo->SiS_Pr, 1000);
       temp1 = SiS_GetCH700x(&ivideo->SiS_Pr, 0x25);
       /* See Chrontel TB31 for explanation */
       temp2 = SiS_GetCH700x(&ivideo->SiS_Pr, 0x0e);
       if(((temp2 & 0x07) == 0x01) || (temp2 & 0x04)) {
T
Thomas Winischhofer 已提交
2660
	  SiS_SetCH700x(&ivideo->SiS_Pr, 0x0e, 0x0b);
L
Linus Torvalds 已提交
2661 2662 2663 2664 2665 2666 2667 2668 2669
	  SiS_DDC2Delay(&ivideo->SiS_Pr, 300);
       }
       temp2 = SiS_GetCH700x(&ivideo->SiS_Pr, 0x25);
       if(temp2 != temp1) temp1 = temp2;

       if((temp1 >= 0x22) && (temp1 <= 0x50)) {
	   /* Read power status */
	   temp1 = SiS_GetCH700x(&ivideo->SiS_Pr, 0x0e);
	   if((temp1 & 0x03) != 0x03) {
T
Thomas Winischhofer 已提交
2670 2671
		/* Power all outputs */
		SiS_SetCH700x(&ivideo->SiS_Pr, 0x0e,0x0b);
L
Linus Torvalds 已提交
2672 2673 2674 2675
		SiS_DDC2Delay(&ivideo->SiS_Pr, 300);
	   }
	   /* Sense connected TV devices */
	   for(i = 0; i < 3; i++) {
T
Thomas Winischhofer 已提交
2676
	       SiS_SetCH700x(&ivideo->SiS_Pr, 0x10, 0x01);
L
Linus Torvalds 已提交
2677
	       SiS_DDC2Delay(&ivideo->SiS_Pr, 0x96);
T
Thomas Winischhofer 已提交
2678
	       SiS_SetCH700x(&ivideo->SiS_Pr, 0x10, 0x00);
L
Linus Torvalds 已提交
2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690
	       SiS_DDC2Delay(&ivideo->SiS_Pr, 0x96);
	       temp1 = SiS_GetCH700x(&ivideo->SiS_Pr, 0x10);
	       if(!(temp1 & 0x08))       test[i] = 0x02;
	       else if(!(temp1 & 0x02))  test[i] = 0x01;
	       else                      test[i] = 0;
	       SiS_DDC2Delay(&ivideo->SiS_Pr, 0x96);
	   }

	   if(test[0] == test[1])      temp1 = test[0];
	   else if(test[0] == test[2]) temp1 = test[0];
	   else if(test[1] == test[2]) temp1 = test[1];
	   else {
T
Thomas Winischhofer 已提交
2691
		printk(KERN_INFO
L
Linus Torvalds 已提交
2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705
			"sisfb: TV detection unreliable - test results varied\n");
		temp1 = test[2];
	   }
	   if(temp1 == 0x02) {
		printk(KERN_INFO "%s SVIDEO output\n", stdstr);
		ivideo->vbflags |= TV_SVIDEO;
		orSISIDXREG(SISCR, 0x32, 0x02);
		andSISIDXREG(SISCR, 0x32, ~0x05);
	   } else if (temp1 == 0x01) {
		printk(KERN_INFO "%s CVBS output\n", stdstr);
		ivideo->vbflags |= TV_AVIDEO;
		orSISIDXREG(SISCR, 0x32, 0x01);
		andSISIDXREG(SISCR, 0x32, ~0x06);
	   } else {
T
Thomas Winischhofer 已提交
2706
		SiS_SetCH70xxANDOR(&ivideo->SiS_Pr, 0x0e, 0x01, 0xF8);
L
Linus Torvalds 已提交
2707 2708 2709
		andSISIDXREG(SISCR, 0x32, ~0x07);
	   }
       } else if(temp1 == 0) {
T
Thomas Winischhofer 已提交
2710
	  SiS_SetCH70xxANDOR(&ivideo->SiS_Pr, 0x0e, 0x01, 0xF8);
L
Linus Torvalds 已提交
2711 2712 2713 2714 2715 2716 2717 2718 2719 2720
	  andSISIDXREG(SISCR, 0x32, ~0x07);
       }
       /* Set general purpose IO for Chrontel communication */
       SiS_SetChrontelGPIO(&ivideo->SiS_Pr, 0x00);
#endif

    } else {

#ifdef CONFIG_FB_SIS_315
	ivideo->SiS_Pr.SiS_IF_DEF_CH70xx = 2;		/* Chrontel 7019 */
T
Thomas Winischhofer 已提交
2721 2722
	temp1 = SiS_GetCH701x(&ivideo->SiS_Pr, 0x49);
	SiS_SetCH701x(&ivideo->SiS_Pr, 0x49, 0x20);
L
Linus Torvalds 已提交
2723 2724 2725
	SiS_DDC2Delay(&ivideo->SiS_Pr, 0x96);
	temp2 = SiS_GetCH701x(&ivideo->SiS_Pr, 0x20);
	temp2 |= 0x01;
T
Thomas Winischhofer 已提交
2726
	SiS_SetCH701x(&ivideo->SiS_Pr, 0x20, temp2);
L
Linus Torvalds 已提交
2727 2728
	SiS_DDC2Delay(&ivideo->SiS_Pr, 0x96);
	temp2 ^= 0x01;
T
Thomas Winischhofer 已提交
2729
	SiS_SetCH701x(&ivideo->SiS_Pr, 0x20, temp2);
L
Linus Torvalds 已提交
2730 2731
	SiS_DDC2Delay(&ivideo->SiS_Pr, 0x96);
	temp2 = SiS_GetCH701x(&ivideo->SiS_Pr, 0x20);
T
Thomas Winischhofer 已提交
2732 2733
	SiS_SetCH701x(&ivideo->SiS_Pr, 0x49, temp1);
	temp1 = 0;
L
Linus Torvalds 已提交
2734 2735 2736 2737 2738 2739 2740 2741 2742 2743
	if(temp2 & 0x02) temp1 |= 0x01;
	if(temp2 & 0x10) temp1 |= 0x01;
	if(temp2 & 0x04) temp1 |= 0x02;
	if( (temp1 & 0x01) && (temp1 & 0x02) ) temp1 = 0x04;
	switch(temp1) {
	case 0x01:
	     printk(KERN_INFO "%s CVBS output\n", stdstr);
	     ivideo->vbflags |= TV_AVIDEO;
	     orSISIDXREG(SISCR, 0x32, 0x01);
	     andSISIDXREG(SISCR, 0x32, ~0x06);
T
Thomas Winischhofer 已提交
2744
	     break;
L
Linus Torvalds 已提交
2745 2746 2747 2748 2749
	case 0x02:
	     printk(KERN_INFO "%s SVIDEO output\n", stdstr);
	     ivideo->vbflags |= TV_SVIDEO;
	     orSISIDXREG(SISCR, 0x32, 0x02);
	     andSISIDXREG(SISCR, 0x32, ~0x05);
T
Thomas Winischhofer 已提交
2750
	     break;
L
Linus Torvalds 已提交
2751 2752 2753 2754
	case 0x04:
	     printk(KERN_INFO "%s SCART output\n", stdstr);
	     orSISIDXREG(SISCR, 0x32, 0x04);
	     andSISIDXREG(SISCR, 0x32, ~0x03);
T
Thomas Winischhofer 已提交
2755
	     break;
L
Linus Torvalds 已提交
2756 2757 2758 2759 2760 2761 2762
	default:
	     andSISIDXREG(SISCR, 0x32, ~0x07);
	}
#endif
    }
}

T
Thomas Winischhofer 已提交
2763 2764
static void __devinit
sisfb_get_VB_type(struct sis_video_info *ivideo)
L
Linus Torvalds 已提交
2765
{
T
Thomas Winischhofer 已提交
2766 2767 2768 2769
	char stdstr[]    = "sisfb: Detected";
	char bridgestr[] = "video bridge";
	u8 vb_chipid;
	u8 reg;
L
Linus Torvalds 已提交
2770

T
Thomas Winischhofer 已提交
2771 2772 2773
	/* No CRT2 on XGI Z7 */
	if(ivideo->chip == XGI_20)
		return;
L
Linus Torvalds 已提交
2774

T
Thomas Winischhofer 已提交
2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824
	inSISIDXREG(SISPART4, 0x00, vb_chipid);
	switch(vb_chipid) {
	case 0x01:
		inSISIDXREG(SISPART4, 0x01, reg);
		if(reg < 0xb0) {
			ivideo->vbflags |= VB_301;	/* Deprecated */
			ivideo->vbflags2 |= VB2_301;
			printk(KERN_INFO "%s SiS301 %s\n", stdstr, bridgestr);
		} else if(reg < 0xc0) {
			ivideo->vbflags |= VB_301B;	/* Deprecated */
			ivideo->vbflags2 |= VB2_301B;
			inSISIDXREG(SISPART4,0x23,reg);
			if(!(reg & 0x02)) {
			   ivideo->vbflags |= VB_30xBDH;	/* Deprecated */
			   ivideo->vbflags2 |= VB2_30xBDH;
			   printk(KERN_INFO "%s SiS301B-DH %s\n", stdstr, bridgestr);
			} else {
			   printk(KERN_INFO "%s SiS301B %s\n", stdstr, bridgestr);
			}
		} else if(reg < 0xd0) {
			ivideo->vbflags |= VB_301C;	/* Deprecated */
			ivideo->vbflags2 |= VB2_301C;
			printk(KERN_INFO "%s SiS301C %s\n", stdstr, bridgestr);
		} else if(reg < 0xe0) {
			ivideo->vbflags |= VB_301LV;	/* Deprecated */
			ivideo->vbflags2 |= VB2_301LV;
			printk(KERN_INFO "%s SiS301LV %s\n", stdstr, bridgestr);
		} else if(reg <= 0xe1) {
			inSISIDXREG(SISPART4,0x39,reg);
			if(reg == 0xff) {
			   ivideo->vbflags |= VB_302LV;	/* Deprecated */
			   ivideo->vbflags2 |= VB2_302LV;
			   printk(KERN_INFO "%s SiS302LV %s\n", stdstr, bridgestr);
			} else {
			   ivideo->vbflags |= VB_301C;	/* Deprecated */
			   ivideo->vbflags2 |= VB2_301C;
			   printk(KERN_INFO "%s SiS301C(P4) %s\n", stdstr, bridgestr);
#if 0
			   ivideo->vbflags |= VB_302ELV;	/* Deprecated */
			   ivideo->vbflags2 |= VB2_302ELV;
			   printk(KERN_INFO "%s SiS302ELV %s\n", stdstr, bridgestr);
#endif
			}
		}
		break;
	case 0x02:
		ivideo->vbflags |= VB_302B;	/* Deprecated */
		ivideo->vbflags2 |= VB2_302B;
		printk(KERN_INFO "%s SiS302B %s\n", stdstr, bridgestr);
		break;
L
Linus Torvalds 已提交
2825 2826
	}

T
Thomas Winischhofer 已提交
2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913
	if((!(ivideo->vbflags2 & VB2_VIDEOBRIDGE)) && (ivideo->chip != SIS_300)) {
		inSISIDXREG(SISCR, 0x37, reg);
		reg &= SIS_EXTERNAL_CHIP_MASK;
		reg >>= 1;
		if(ivideo->sisvga_engine == SIS_300_VGA) {
#ifdef CONFIG_FB_SIS_300
			switch(reg) {
			   case SIS_EXTERNAL_CHIP_LVDS:
				ivideo->vbflags |= VB_LVDS;	/* Deprecated */
				ivideo->vbflags2 |= VB2_LVDS;
				break;
			   case SIS_EXTERNAL_CHIP_TRUMPION:
				ivideo->vbflags |= (VB_LVDS | VB_TRUMPION);	/* Deprecated */
				ivideo->vbflags2 |= (VB2_LVDS | VB2_TRUMPION);
				break;
			   case SIS_EXTERNAL_CHIP_CHRONTEL:
				ivideo->vbflags |= VB_CHRONTEL;	/* Deprecated */
				ivideo->vbflags2 |= VB2_CHRONTEL;
				break;
			   case SIS_EXTERNAL_CHIP_LVDS_CHRONTEL:
				ivideo->vbflags |= (VB_LVDS | VB_CHRONTEL);	/* Deprecated */
				ivideo->vbflags2 |= (VB2_LVDS | VB2_CHRONTEL);
				break;
			}
			if(ivideo->vbflags2 & VB2_CHRONTEL) ivideo->chronteltype = 1;
#endif
		} else if(ivideo->chip < SIS_661) {
#ifdef CONFIG_FB_SIS_315
			switch (reg) {
			   case SIS310_EXTERNAL_CHIP_LVDS:
				ivideo->vbflags |= VB_LVDS;	/* Deprecated */
				ivideo->vbflags2 |= VB2_LVDS;
				break;
			   case SIS310_EXTERNAL_CHIP_LVDS_CHRONTEL:
				ivideo->vbflags |= (VB_LVDS | VB_CHRONTEL);	/* Deprecated */
				ivideo->vbflags2 |= (VB2_LVDS | VB2_CHRONTEL);
				break;
			}
			if(ivideo->vbflags2 & VB2_CHRONTEL) ivideo->chronteltype = 2;
#endif
		} else if(ivideo->chip >= SIS_661) {
#ifdef CONFIG_FB_SIS_315
			inSISIDXREG(SISCR, 0x38, reg);
			reg >>= 5;
			switch(reg) {
			   case 0x02:
				ivideo->vbflags |= VB_LVDS;	/* Deprecated */
				ivideo->vbflags2 |= VB2_LVDS;
				break;
			   case 0x03:
				ivideo->vbflags |= (VB_LVDS | VB_CHRONTEL);	/* Deprecated */
				ivideo->vbflags2 |= (VB2_LVDS | VB2_CHRONTEL);
				break;
			   case 0x04:
				ivideo->vbflags |= (VB_LVDS | VB_CONEXANT);	/* Deprecated */
				ivideo->vbflags2 |= (VB2_LVDS | VB2_CONEXANT);
				break;
			}
			if(ivideo->vbflags2 & VB2_CHRONTEL) ivideo->chronteltype = 2;
#endif
		}
		if(ivideo->vbflags2 & VB2_LVDS) {
		   printk(KERN_INFO "%s LVDS transmitter\n", stdstr);
		}
		if((ivideo->sisvga_engine == SIS_300_VGA) && (ivideo->vbflags2 & VB2_TRUMPION)) {
		   printk(KERN_INFO "%s Trumpion Zurac LCD scaler\n", stdstr);
		}
		if(ivideo->vbflags2 & VB2_CHRONTEL) {
		   printk(KERN_INFO "%s Chrontel TV encoder\n", stdstr);
		}
		if((ivideo->chip >= SIS_661) && (ivideo->vbflags2 & VB2_CONEXANT)) {
		   printk(KERN_INFO "%s Conexant external device\n", stdstr);
		}
	}

	if(ivideo->vbflags2 & VB2_SISBRIDGE) {
		SiS_SenseLCD(ivideo);
		SiS_Sense30x(ivideo);
	} else if(ivideo->vbflags2 & VB2_CHRONTEL) {
		SiS_SenseCh(ivideo);
	}
}

/* ---------- Engine initialization routines ------------ */

static void
sisfb_engine_init(struct sis_video_info *ivideo)
L
Linus Torvalds 已提交
2914 2915
{

T
Thomas Winischhofer 已提交
2916
	/* Initialize command queue (we use MMIO only) */
L
Linus Torvalds 已提交
2917

T
Thomas Winischhofer 已提交
2918
	/* BEFORE THIS IS CALLED, THE ENGINES *MUST* BE SYNC'ED */
L
Linus Torvalds 已提交
2919

T
Thomas Winischhofer 已提交
2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942
	ivideo->caps &= ~(TURBO_QUEUE_CAP    |
			  MMIO_CMD_QUEUE_CAP |
			  VM_CMD_QUEUE_CAP   |
			  AGP_CMD_QUEUE_CAP);

#ifdef CONFIG_FB_SIS_300
	if(ivideo->sisvga_engine == SIS_300_VGA) {
		u32 tqueue_pos;
		u8 tq_state;

		tqueue_pos = (ivideo->video_size - ivideo->cmdQueueSize) / (64 * 1024);

		inSISIDXREG(SISSR, IND_SIS_TURBOQUEUE_SET, tq_state);
		tq_state |= 0xf0;
		tq_state &= 0xfc;
		tq_state |= (u8)(tqueue_pos >> 8);
		outSISIDXREG(SISSR, IND_SIS_TURBOQUEUE_SET, tq_state);

		outSISIDXREG(SISSR, IND_SIS_TURBOQUEUE_ADR, (u8)(tqueue_pos & 0xff));

		ivideo->caps |= TURBO_QUEUE_CAP;
	}
#endif
L
Linus Torvalds 已提交
2943 2944

#ifdef CONFIG_FB_SIS_315
T
Thomas Winischhofer 已提交
2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991
	if(ivideo->sisvga_engine == SIS_315_VGA) {
		u32 tempq = 0, templ;
		u8  temp;

		if(ivideo->chip == XGI_20) {
			switch(ivideo->cmdQueueSize) {
			case (64 * 1024):
				temp = SIS_CMD_QUEUE_SIZE_Z7_64k;
				break;
			case (128 * 1024):
			default:
				temp = SIS_CMD_QUEUE_SIZE_Z7_128k;
			}
		} else {
			switch(ivideo->cmdQueueSize) {
			case (4 * 1024 * 1024):
				temp = SIS_CMD_QUEUE_SIZE_4M;
				break;
			case (2 * 1024 * 1024):
				temp = SIS_CMD_QUEUE_SIZE_2M;
				break;
			case (1 * 1024 * 1024):
				temp = SIS_CMD_QUEUE_SIZE_1M;
				break;
			default:
			case (512 * 1024):
				temp = SIS_CMD_QUEUE_SIZE_512k;
			}
		}

		outSISIDXREG(SISSR, IND_SIS_CMDQUEUE_THRESHOLD, COMMAND_QUEUE_THRESHOLD);
		outSISIDXREG(SISSR, IND_SIS_CMDQUEUE_SET, SIS_CMD_QUEUE_RESET);

		if((ivideo->chip >= XGI_40) && ivideo->modechanged) {
			/* Must disable dual pipe on XGI_40. Can't do
			 * this in MMIO mode, because it requires
			 * setting/clearing a bit in the MMIO fire trigger
			 * register.
			 */
			if(!((templ = MMIO_IN32(ivideo->mmio_vbase, 0x8240)) & (1 << 10))) {

				MMIO_OUT32(ivideo->mmio_vbase, Q_WRITE_PTR, 0);

				outSISIDXREG(SISSR, IND_SIS_CMDQUEUE_SET, (temp | SIS_VRAM_CMDQUEUE_ENABLE));

				tempq = MMIO_IN32(ivideo->mmio_vbase, Q_READ_PTR);
				MMIO_OUT32(ivideo->mmio_vbase, Q_WRITE_PTR, tempq);
L
Linus Torvalds 已提交
2992

T
Thomas Winischhofer 已提交
2993 2994
				tempq = (u32)(ivideo->video_size - ivideo->cmdQueueSize);
				MMIO_OUT32(ivideo->mmio_vbase, Q_BASE_ADDR, tempq);
L
Linus Torvalds 已提交
2995

T
Thomas Winischhofer 已提交
2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030
				writel(0x16800000 + 0x8240, ivideo->video_vbase + tempq);
				writel(templ | (1 << 10), ivideo->video_vbase + tempq + 4);
				writel(0x168F0000, ivideo->video_vbase + tempq + 8);
				writel(0x168F0000, ivideo->video_vbase + tempq + 12);

				MMIO_OUT32(ivideo->mmio_vbase, Q_WRITE_PTR, (tempq + 16));

				sisfb_syncaccel(ivideo);

				outSISIDXREG(SISSR, IND_SIS_CMDQUEUE_SET, SIS_CMD_QUEUE_RESET);

			}
		}

		tempq = MMIO_IN32(ivideo->mmio_vbase, MMIO_QUEUE_READPORT);
		MMIO_OUT32(ivideo->mmio_vbase, MMIO_QUEUE_WRITEPORT, tempq);

		temp |= (SIS_MMIO_CMD_ENABLE | SIS_CMD_AUTO_CORR);
		outSISIDXREG(SISSR, IND_SIS_CMDQUEUE_SET, temp);

		tempq = (u32)(ivideo->video_size - ivideo->cmdQueueSize);
		MMIO_OUT32(ivideo->mmio_vbase, MMIO_QUEUE_PHYBASE, tempq);

		ivideo->caps |= MMIO_CMD_QUEUE_CAP;
	}
#endif

	ivideo->engineok = 1;
}

static void __devinit
sisfb_detect_lcd_type(struct sis_video_info *ivideo)
{
	u8 reg;
	int i;
L
Linus Torvalds 已提交
3031

T
Thomas Winischhofer 已提交
3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046
	inSISIDXREG(SISCR, 0x36, reg);
	reg &= 0x0f;
	if(ivideo->sisvga_engine == SIS_300_VGA) {
		ivideo->CRT2LCDType = sis300paneltype[reg];
	} else if(ivideo->chip >= SIS_661) {
		ivideo->CRT2LCDType = sis661paneltype[reg];
	} else {
		ivideo->CRT2LCDType = sis310paneltype[reg];
		if((ivideo->chip == SIS_550) && (sisfb_fstn)) {
			if((ivideo->CRT2LCDType != LCD_320x240_2) &&
			   (ivideo->CRT2LCDType != LCD_320x240_3)) {
				ivideo->CRT2LCDType = LCD_320x240;
			}
		}
	}
L
Linus Torvalds 已提交
3047

T
Thomas Winischhofer 已提交
3048 3049 3050 3051 3052 3053 3054
	if(ivideo->CRT2LCDType == LCD_UNKNOWN) {
		/* For broken BIOSes: Assume 1024x768, RGB18 */
		ivideo->CRT2LCDType = LCD_1024x768;
		setSISIDXREG(SISCR,0x36,0xf0,0x02);
		setSISIDXREG(SISCR,0x37,0xee,0x01);
		printk(KERN_DEBUG "sisfb: Invalid panel ID (%02x), assuming 1024x768, RGB18\n", reg);
	}
L
Linus Torvalds 已提交
3055

T
Thomas Winischhofer 已提交
3056 3057 3058 3059 3060 3061 3062 3063
	for(i = 0; i < SIS_LCD_NUMBER; i++) {
		if(ivideo->CRT2LCDType == sis_lcd_data[i].lcdtype) {
			ivideo->lcdxres = sis_lcd_data[i].xres;
			ivideo->lcdyres = sis_lcd_data[i].yres;
			ivideo->lcddefmodeidx = sis_lcd_data[i].default_mode_idx;
			break;
		}
	}
L
Linus Torvalds 已提交
3064

T
Thomas Winischhofer 已提交
3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075
#ifdef CONFIG_FB_SIS_300
	if(ivideo->SiS_Pr.SiS_CustomT == CUT_BARCO1366) {
		ivideo->lcdxres = 1360; ivideo->lcdyres = 1024;
		ivideo->lcddefmodeidx = DEFAULT_MODE_1360;
	} else if(ivideo->SiS_Pr.SiS_CustomT == CUT_PANEL848) {
		ivideo->lcdxres =  848; ivideo->lcdyres =  480;
		ivideo->lcddefmodeidx = DEFAULT_MODE_848;
	} else if(ivideo->SiS_Pr.SiS_CustomT == CUT_PANEL856) {
		ivideo->lcdxres =  856; ivideo->lcdyres =  480;
		ivideo->lcddefmodeidx = DEFAULT_MODE_856;
	}
L
Linus Torvalds 已提交
3076 3077
#endif

T
Thomas Winischhofer 已提交
3078 3079 3080 3081 3082 3083 3084
	printk(KERN_DEBUG "sisfb: Detected %dx%d flat panel\n",
			ivideo->lcdxres, ivideo->lcdyres);
}

static void __devinit
sisfb_save_pdc_emi(struct sis_video_info *ivideo)
{
L
Linus Torvalds 已提交
3085
#ifdef CONFIG_FB_SIS_300
T
Thomas Winischhofer 已提交
3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112
	/* Save the current PanelDelayCompensation if the LCD is currently used */
	if(ivideo->sisvga_engine == SIS_300_VGA) {
		if(ivideo->vbflags2 & (VB2_LVDS | VB2_30xBDH)) {
			int tmp;
			inSISIDXREG(SISCR,0x30,tmp);
			if(tmp & 0x20) {
				/* Currently on LCD? If yes, read current pdc */
				inSISIDXREG(SISPART1,0x13,ivideo->detectedpdc);
				ivideo->detectedpdc &= 0x3c;
				if(ivideo->SiS_Pr.PDC == -1) {
					/* Let option override detection */
					ivideo->SiS_Pr.PDC = ivideo->detectedpdc;
				}
				printk(KERN_INFO "sisfb: Detected LCD PDC 0x%02x\n",
					ivideo->detectedpdc);
			}
			if((ivideo->SiS_Pr.PDC != -1) &&
			   (ivideo->SiS_Pr.PDC != ivideo->detectedpdc)) {
				printk(KERN_INFO "sisfb: Using LCD PDC 0x%02x\n",
					ivideo->SiS_Pr.PDC);
			}
		}
	}
#endif

#ifdef CONFIG_FB_SIS_315
	if(ivideo->sisvga_engine == SIS_315_VGA) {
L
Linus Torvalds 已提交
3113

T
Thomas Winischhofer 已提交
3114 3115 3116 3117 3118 3119 3120 3121 3122
		/* Try to find about LCDA */
		if(ivideo->vbflags2 & VB2_SISLCDABRIDGE) {
			int tmp;
			inSISIDXREG(SISPART1,0x13,tmp);
			if(tmp & 0x04) {
				ivideo->SiS_Pr.SiS_UseLCDA = TRUE;
				ivideo->detectedlcda = 0x03;
			}
		}
L
Linus Torvalds 已提交
3123

T
Thomas Winischhofer 已提交
3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166
		/* Save PDC */
		if(ivideo->vbflags2 & VB2_SISLVDSBRIDGE) {
			int tmp;
			inSISIDXREG(SISCR,0x30,tmp);
			if((tmp & 0x20) || (ivideo->detectedlcda != 0xff)) {
				/* Currently on LCD? If yes, read current pdc */
				u8 pdc;
				inSISIDXREG(SISPART1,0x2D,pdc);
				ivideo->detectedpdc  = (pdc & 0x0f) << 1;
				ivideo->detectedpdca = (pdc & 0xf0) >> 3;
				inSISIDXREG(SISPART1,0x35,pdc);
				ivideo->detectedpdc |= ((pdc >> 7) & 0x01);
				inSISIDXREG(SISPART1,0x20,pdc);
				ivideo->detectedpdca |= ((pdc >> 6) & 0x01);
				if(ivideo->newrom) {
					/* New ROM invalidates other PDC resp. */
					if(ivideo->detectedlcda != 0xff) {
						ivideo->detectedpdc = 0xff;
					} else {
						ivideo->detectedpdca = 0xff;
					}
				}
				if(ivideo->SiS_Pr.PDC == -1) {
					if(ivideo->detectedpdc != 0xff) {
						ivideo->SiS_Pr.PDC = ivideo->detectedpdc;
					}
				}
				if(ivideo->SiS_Pr.PDCA == -1) {
					if(ivideo->detectedpdca != 0xff) {
						ivideo->SiS_Pr.PDCA = ivideo->detectedpdca;
					}
				}
				if(ivideo->detectedpdc != 0xff) {
					printk(KERN_INFO
						"sisfb: Detected LCD PDC 0x%02x (for LCD=CRT2)\n",
						ivideo->detectedpdc);
				}
				if(ivideo->detectedpdca != 0xff) {
					printk(KERN_INFO
						"sisfb: Detected LCD PDC1 0x%02x (for LCD=CRT1)\n",
						ivideo->detectedpdca);
				}
			}
L
Linus Torvalds 已提交
3167

T
Thomas Winischhofer 已提交
3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179
			/* Save EMI */
			if(ivideo->vbflags2 & VB2_SISEMIBRIDGE) {
				inSISIDXREG(SISPART4,0x30,ivideo->SiS_Pr.EMI_30);
				inSISIDXREG(SISPART4,0x31,ivideo->SiS_Pr.EMI_31);
				inSISIDXREG(SISPART4,0x32,ivideo->SiS_Pr.EMI_32);
				inSISIDXREG(SISPART4,0x33,ivideo->SiS_Pr.EMI_33);
				ivideo->SiS_Pr.HaveEMI = TRUE;
				if((tmp & 0x20) || (ivideo->detectedlcda != 0xff)) {
					ivideo->SiS_Pr.HaveEMILCD = TRUE;
				}
			}
		}
L
Linus Torvalds 已提交
3180

T
Thomas Winischhofer 已提交
3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193
		/* Let user override detected PDCs (all bridges) */
		if(ivideo->vbflags2 & VB2_30xBLV) {
			if((ivideo->SiS_Pr.PDC != -1) &&
			   (ivideo->SiS_Pr.PDC != ivideo->detectedpdc)) {
				printk(KERN_INFO "sisfb: Using LCD PDC 0x%02x (for LCD=CRT2)\n",
					ivideo->SiS_Pr.PDC);
			}
			if((ivideo->SiS_Pr.PDCA != -1) &&
			   (ivideo->SiS_Pr.PDCA != ivideo->detectedpdca)) {
				printk(KERN_INFO "sisfb: Using LCD PDC1 0x%02x (for LCD=CRT1)\n",
				 ivideo->SiS_Pr.PDCA);
			}
		}
L
Linus Torvalds 已提交
3194

T
Thomas Winischhofer 已提交
3195
	}
L
Linus Torvalds 已提交
3196
#endif
T
Thomas Winischhofer 已提交
3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269
}

/* -------------------- Memory manager routines ---------------------- */

static u32 __devinit
sisfb_getheapstart(struct sis_video_info *ivideo)
{
	u32 ret = ivideo->sisfb_parm_mem * 1024;
	u32 maxoffs = ivideo->video_size - ivideo->hwcursor_size - ivideo->cmdQueueSize;
	u32 def;

	/* Calculate heap start = end of memory for console
	 *
	 * CCCCCCCCDDDDDDDDDDDDDDDDDDDDDDDDDDDDHHHHQQQQQQQQQQ
	 * C = console, D = heap, H = HWCursor, Q = cmd-queue
	 *
	 * On 76x in UMA+LFB mode, the layout is as follows:
	 * DDDDDDDDDDDCCCCCCCCCCCCCCCCCCCCCCCCHHHHQQQQQQQQQQQ
	 * where the heap is the entire UMA area, eventually
	 * into the LFB area if the given mem parameter is
	 * higher than the size of the UMA memory.
	 *
	 * Basically given by "mem" parameter
	 *
	 * maximum = videosize - cmd_queue - hwcursor
	 *           (results in a heap of size 0)
	 * default = SiS 300: depends on videosize
	 *           SiS 315/330/340/XGI: 32k below max
	 */

	if(ivideo->sisvga_engine == SIS_300_VGA) {
		if(ivideo->video_size > 0x1000000) {
			def = 0xc00000;
		} else if(ivideo->video_size > 0x800000) {
			def = 0x800000;
		} else {
			def = 0x400000;
		}
	} else if(ivideo->UMAsize && ivideo->LFBsize) {
		ret = def = 0;
	} else {
		def = maxoffs - 0x8000;
	}

	/* Use default for secondary card for now (FIXME) */
	if((!ret) || (ret > maxoffs) || (ivideo->cardnumber != 0))
		ret = def;

	return ret;
}

static u32 __devinit
sisfb_getheapsize(struct sis_video_info *ivideo)
{
	u32 max = ivideo->video_size - ivideo->hwcursor_size - ivideo->cmdQueueSize;
	u32 ret = 0;

	if(ivideo->UMAsize && ivideo->LFBsize) {
		if( (!ivideo->sisfb_parm_mem)			||
		    ((ivideo->sisfb_parm_mem * 1024) > max)	||
		    ((max - (ivideo->sisfb_parm_mem * 1024)) < ivideo->UMAsize) ) {
			ret = ivideo->UMAsize;
			max -= ivideo->UMAsize;
		} else {
			ret = max - (ivideo->sisfb_parm_mem * 1024);
			max = ivideo->sisfb_parm_mem * 1024;
		}
		ivideo->video_offset = ret;
		ivideo->sisfb_mem = max;
	} else {
		ret = max - ivideo->heapstart;
		ivideo->sisfb_mem = ivideo->heapstart;
	}
L
Linus Torvalds 已提交
3270

T
Thomas Winischhofer 已提交
3271 3272
	return ret;
}
L
Linus Torvalds 已提交
3273

T
Thomas Winischhofer 已提交
3274 3275 3276 3277
static int __devinit
sisfb_heap_init(struct sis_video_info *ivideo)
{
	struct SIS_OH *poh;
L
Linus Torvalds 已提交
3278

T
Thomas Winischhofer 已提交
3279 3280 3281 3282 3283 3284 3285
	ivideo->video_offset = 0;
	if(ivideo->sisfb_parm_mem) {
		if( (ivideo->sisfb_parm_mem < (2 * 1024 * 1024)) ||
		    (ivideo->sisfb_parm_mem > ivideo->video_size) ) {
			ivideo->sisfb_parm_mem = 0;
		}
	}
L
Linus Torvalds 已提交
3286

T
Thomas Winischhofer 已提交
3287 3288
	ivideo->heapstart = sisfb_getheapstart(ivideo);
	ivideo->sisfb_heap_size = sisfb_getheapsize(ivideo);
L
Linus Torvalds 已提交
3289

T
Thomas Winischhofer 已提交
3290 3291
	ivideo->sisfb_heap_start = ivideo->video_vbase + ivideo->heapstart;
	ivideo->sisfb_heap_end   = ivideo->sisfb_heap_start + ivideo->sisfb_heap_size;
L
Linus Torvalds 已提交
3292

T
Thomas Winischhofer 已提交
3293 3294
	printk(KERN_INFO "sisfb: Memory heap starting at %dK, size %dK\n",
		(int)(ivideo->heapstart / 1024), (int)(ivideo->sisfb_heap_size / 1024));
L
Linus Torvalds 已提交
3295

T
Thomas Winischhofer 已提交
3296
	ivideo->sisfb_heap.vinfo = ivideo;
L
Linus Torvalds 已提交
3297

T
Thomas Winischhofer 已提交
3298 3299
	ivideo->sisfb_heap.poha_chain = NULL;
	ivideo->sisfb_heap.poh_freelist = NULL;
L
Linus Torvalds 已提交
3300

T
Thomas Winischhofer 已提交
3301 3302 3303
	poh = sisfb_poh_new_node(&ivideo->sisfb_heap);
	if(poh == NULL)
		return 1;
L
Linus Torvalds 已提交
3304

T
Thomas Winischhofer 已提交
3305 3306 3307 3308
	poh->poh_next = &ivideo->sisfb_heap.oh_free;
	poh->poh_prev = &ivideo->sisfb_heap.oh_free;
	poh->size = ivideo->sisfb_heap_size;
	poh->offset = ivideo->heapstart;
L
Linus Torvalds 已提交
3309

T
Thomas Winischhofer 已提交
3310 3311 3312 3313
	ivideo->sisfb_heap.oh_free.poh_next = poh;
	ivideo->sisfb_heap.oh_free.poh_prev = poh;
	ivideo->sisfb_heap.oh_free.size = 0;
	ivideo->sisfb_heap.max_freesize = poh->size;
L
Linus Torvalds 已提交
3314

T
Thomas Winischhofer 已提交
3315 3316 3317
	ivideo->sisfb_heap.oh_used.poh_next = &ivideo->sisfb_heap.oh_used;
	ivideo->sisfb_heap.oh_used.poh_prev = &ivideo->sisfb_heap.oh_used;
	ivideo->sisfb_heap.oh_used.size = SENTINEL;
L
Linus Torvalds 已提交
3318

T
Thomas Winischhofer 已提交
3319 3320 3321 3322 3323 3324
	if(ivideo->cardnumber == 0) {
		/* For the first card, make this heap the "global" one
		 * for old DRM (which could handle only one card)
		 */
		sisfb_heap = &ivideo->sisfb_heap;
	}
L
Linus Torvalds 已提交
3325

T
Thomas Winischhofer 已提交
3326
	return 0;
L
Linus Torvalds 已提交
3327 3328
}

T
Thomas Winischhofer 已提交
3329 3330
static struct SIS_OH *
sisfb_poh_new_node(struct SIS_HEAP *memheap)
L
Linus Torvalds 已提交
3331
{
T
Thomas Winischhofer 已提交
3332 3333 3334 3335
	struct SIS_OHALLOC	*poha;
	struct SIS_OH		*poh;
	unsigned long		cOhs;
	int			i;
L
Linus Torvalds 已提交
3336

T
Thomas Winischhofer 已提交
3337
	if(memheap->poh_freelist == NULL) {
L
Linus Torvalds 已提交
3338
		poha = kmalloc(SIS_OH_ALLOC_SIZE, GFP_KERNEL);
T
Thomas Winischhofer 已提交
3339 3340
		if(!poha)
			return NULL;
L
Linus Torvalds 已提交
3341

T
Thomas Winischhofer 已提交
3342 3343
		poha->poha_next = memheap->poha_chain;
		memheap->poha_chain = poha;
L
Linus Torvalds 已提交
3344

T
Thomas Winischhofer 已提交
3345
		cOhs = (SIS_OH_ALLOC_SIZE - sizeof(struct SIS_OHALLOC)) / sizeof(struct SIS_OH) + 1;
L
Linus Torvalds 已提交
3346 3347 3348 3349 3350 3351 3352 3353

		poh = &poha->aoh[0];
		for(i = cOhs - 1; i != 0; i--) {
			poh->poh_next = poh + 1;
			poh = poh + 1;
		}

		poh->poh_next = NULL;
T
Thomas Winischhofer 已提交
3354
		memheap->poh_freelist = &poha->aoh[0];
L
Linus Torvalds 已提交
3355 3356
	}

T
Thomas Winischhofer 已提交
3357 3358
	poh = memheap->poh_freelist;
	memheap->poh_freelist = poh->poh_next;
L
Linus Torvalds 已提交
3359

T
Thomas Winischhofer 已提交
3360
	return poh;
L
Linus Torvalds 已提交
3361 3362
}

T
Thomas Winischhofer 已提交
3363 3364
static struct SIS_OH *
sisfb_poh_allocate(struct SIS_HEAP *memheap, u32 size)
L
Linus Torvalds 已提交
3365
{
T
Thomas Winischhofer 已提交
3366 3367 3368
	struct SIS_OH	*pohThis;
	struct SIS_OH	*pohRoot;
	int		bAllocated = 0;
L
Linus Torvalds 已提交
3369

T
Thomas Winischhofer 已提交
3370
	if(size > memheap->max_freesize) {
L
Linus Torvalds 已提交
3371 3372
		DPRINTK("sisfb: Can't allocate %dk video memory\n",
			(unsigned int) size / 1024);
T
Thomas Winischhofer 已提交
3373
		return NULL;
L
Linus Torvalds 已提交
3374 3375
	}

T
Thomas Winischhofer 已提交
3376
	pohThis = memheap->oh_free.poh_next;
L
Linus Torvalds 已提交
3377

T
Thomas Winischhofer 已提交
3378 3379
	while(pohThis != &memheap->oh_free) {
		if(size <= pohThis->size) {
L
Linus Torvalds 已提交
3380 3381 3382 3383 3384 3385 3386 3387 3388
			bAllocated = 1;
			break;
		}
		pohThis = pohThis->poh_next;
	}

	if(!bAllocated) {
		DPRINTK("sisfb: Can't allocate %dk video memory\n",
			(unsigned int) size / 1024);
T
Thomas Winischhofer 已提交
3389
		return NULL;
L
Linus Torvalds 已提交
3390 3391 3392 3393 3394 3395
	}

	if(size == pohThis->size) {
		pohRoot = pohThis;
		sisfb_delete_node(pohThis);
	} else {
T
Thomas Winischhofer 已提交
3396 3397 3398
		pohRoot = sisfb_poh_new_node(memheap);
		if(pohRoot == NULL)
			return NULL;
L
Linus Torvalds 已提交
3399 3400 3401 3402 3403 3404 3405 3406

		pohRoot->offset = pohThis->offset;
		pohRoot->size = size;

		pohThis->offset += size;
		pohThis->size -= size;
	}

T
Thomas Winischhofer 已提交
3407
	memheap->max_freesize -= size;
L
Linus Torvalds 已提交
3408

T
Thomas Winischhofer 已提交
3409
	pohThis = &memheap->oh_used;
L
Linus Torvalds 已提交
3410 3411
	sisfb_insert_node(pohThis, pohRoot);

T
Thomas Winischhofer 已提交
3412
	return pohRoot;
L
Linus Torvalds 已提交
3413 3414 3415
}

static void
T
Thomas Winischhofer 已提交
3416
sisfb_delete_node(struct SIS_OH *poh)
L
Linus Torvalds 已提交
3417
{
T
Thomas Winischhofer 已提交
3418 3419
	poh->poh_prev->poh_next = poh->poh_next;
	poh->poh_next->poh_prev = poh->poh_prev;
L
Linus Torvalds 已提交
3420 3421 3422
}

static void
T
Thomas Winischhofer 已提交
3423
sisfb_insert_node(struct SIS_OH *pohList, struct SIS_OH *poh)
L
Linus Torvalds 已提交
3424
{
T
Thomas Winischhofer 已提交
3425
	struct SIS_OH *pohTemp = pohList->poh_next;
L
Linus Torvalds 已提交
3426 3427 3428 3429 3430 3431 3432 3433

	pohList->poh_next = poh;
	pohTemp->poh_prev = poh;

	poh->poh_prev = pohList;
	poh->poh_next = pohTemp;
}

T
Thomas Winischhofer 已提交
3434 3435
static struct SIS_OH *
sisfb_poh_free(struct SIS_HEAP *memheap, u32 base)
L
Linus Torvalds 已提交
3436
{
T
Thomas Winischhofer 已提交
3437 3438 3439 3440 3441 3442 3443
	struct SIS_OH *pohThis;
	struct SIS_OH *poh_freed;
	struct SIS_OH *poh_prev;
	struct SIS_OH *poh_next;
	u32    ulUpper;
	u32    ulLower;
	int    foundNode = 0;
L
Linus Torvalds 已提交
3444

T
Thomas Winischhofer 已提交
3445
	poh_freed = memheap->oh_used.poh_next;
L
Linus Torvalds 已提交
3446

T
Thomas Winischhofer 已提交
3447
	while(poh_freed != &memheap->oh_used) {
L
Linus Torvalds 已提交
3448 3449 3450 3451 3452 3453 3454 3455
		if(poh_freed->offset == base) {
			foundNode = 1;
			break;
		}

		poh_freed = poh_freed->poh_next;
	}

T
Thomas Winischhofer 已提交
3456 3457
	if(!foundNode)
		return NULL;
L
Linus Torvalds 已提交
3458

T
Thomas Winischhofer 已提交
3459
	memheap->max_freesize += poh_freed->size;
L
Linus Torvalds 已提交
3460 3461 3462 3463 3464

	poh_prev = poh_next = NULL;
	ulUpper = poh_freed->offset + poh_freed->size;
	ulLower = poh_freed->offset;

T
Thomas Winischhofer 已提交
3465
	pohThis = memheap->oh_free.poh_next;
L
Linus Torvalds 已提交
3466

T
Thomas Winischhofer 已提交
3467
	while(pohThis != &memheap->oh_free) {
L
Linus Torvalds 已提交
3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480
		if(pohThis->offset == ulUpper) {
			poh_next = pohThis;
		} else if((pohThis->offset + pohThis->size) == ulLower) {
			poh_prev = pohThis;
		}
		pohThis = pohThis->poh_next;
	}

	sisfb_delete_node(poh_freed);

	if(poh_prev && poh_next) {
		poh_prev->size += (poh_freed->size + poh_next->size);
		sisfb_delete_node(poh_next);
T
Thomas Winischhofer 已提交
3481 3482 3483
		sisfb_free_node(memheap, poh_freed);
		sisfb_free_node(memheap, poh_next);
		return poh_prev;
L
Linus Torvalds 已提交
3484 3485 3486 3487
	}

	if(poh_prev) {
		poh_prev->size += poh_freed->size;
T
Thomas Winischhofer 已提交
3488 3489
		sisfb_free_node(memheap, poh_freed);
		return poh_prev;
L
Linus Torvalds 已提交
3490 3491 3492 3493 3494
	}

	if(poh_next) {
		poh_next->size += poh_freed->size;
		poh_next->offset = poh_freed->offset;
T
Thomas Winischhofer 已提交
3495 3496
		sisfb_free_node(memheap, poh_freed);
		return poh_next;
L
Linus Torvalds 已提交
3497 3498
	}

T
Thomas Winischhofer 已提交
3499
	sisfb_insert_node(&memheap->oh_free, poh_freed);
L
Linus Torvalds 已提交
3500

T
Thomas Winischhofer 已提交
3501
	return poh_freed;
L
Linus Torvalds 已提交
3502 3503 3504
}

static void
T
Thomas Winischhofer 已提交
3505
sisfb_free_node(struct SIS_HEAP *memheap, struct SIS_OH *poh)
L
Linus Torvalds 已提交
3506
{
T
Thomas Winischhofer 已提交
3507 3508
	if(poh == NULL)
		return;
L
Linus Torvalds 已提交
3509

T
Thomas Winischhofer 已提交
3510 3511
	poh->poh_next = memheap->poh_freelist;
	memheap->poh_freelist = poh;
L
Linus Torvalds 已提交
3512 3513
}

T
Thomas Winischhofer 已提交
3514 3515
static void
sis_int_malloc(struct sis_video_info *ivideo, struct sis_memreq *req)
L
Linus Torvalds 已提交
3516
{
T
Thomas Winischhofer 已提交
3517
	struct SIS_OH *poh = NULL;
L
Linus Torvalds 已提交
3518

T
Thomas Winischhofer 已提交
3519 3520
	if((ivideo) && (ivideo->sisfb_id == SISFB_ID) && (!ivideo->havenoheap))
		poh = sisfb_poh_allocate(&ivideo->sisfb_heap, (u32)req->size);
L
Linus Torvalds 已提交
3521 3522

	if(poh == NULL) {
T
Thomas Winischhofer 已提交
3523 3524
		req->offset = req->size = 0;
		DPRINTK("sisfb: Video RAM allocation failed\n");
L
Linus Torvalds 已提交
3525
	} else {
T
Thomas Winischhofer 已提交
3526 3527 3528 3529
		req->offset = poh->offset;
		req->size = poh->size;
		DPRINTK("sisfb: Video RAM allocation succeeded: 0x%lx\n",
			(poh->offset + ivideo->video_vbase));
L
Linus Torvalds 已提交
3530 3531 3532
	}
}

T
Thomas Winischhofer 已提交
3533 3534 3535 3536 3537 3538 3539 3540 3541 3542
void
sis_malloc(struct sis_memreq *req)
{
	struct sis_video_info *ivideo = sisfb_heap->vinfo;

	if(&ivideo->sisfb_heap == sisfb_heap)
		sis_int_malloc(ivideo, req);
	else
		req->offset = req->size = 0;
}
L
Linus Torvalds 已提交
3543 3544

void
T
Thomas Winischhofer 已提交
3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555
sis_malloc_new(struct pci_dev *pdev, struct sis_memreq *req)
{
	struct sis_video_info *ivideo = pci_get_drvdata(pdev);

	sis_int_malloc(ivideo, req);
}

/* sis_free: u32 because "base" is offset inside video ram, can never be >4GB */

static void
sis_int_free(struct sis_video_info *ivideo, u32 base)
L
Linus Torvalds 已提交
3556
{
T
Thomas Winischhofer 已提交
3557
	struct SIS_OH *poh;
L
Linus Torvalds 已提交
3558

T
Thomas Winischhofer 已提交
3559 3560
	if((!ivideo) || (ivideo->sisfb_id != SISFB_ID) || (ivideo->havenoheap))
		return;
L
Linus Torvalds 已提交
3561

T
Thomas Winischhofer 已提交
3562
	poh = sisfb_poh_free(&ivideo->sisfb_heap, base);
L
Linus Torvalds 已提交
3563 3564 3565 3566 3567 3568 3569

	if(poh == NULL) {
		DPRINTK("sisfb: sisfb_poh_free() failed at base 0x%x\n",
			(unsigned int) base);
	}
}

T
Thomas Winischhofer 已提交
3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585
void
sis_free(u32 base)
{
	struct sis_video_info *ivideo = sisfb_heap->vinfo;

	sis_int_free(ivideo, base);
}

void
sis_free_new(struct pci_dev *pdev, u32 base)
{
	struct sis_video_info *ivideo = pci_get_drvdata(pdev);

	sis_int_free(ivideo, base);
}

L
Linus Torvalds 已提交
3586 3587
/* --------------------- SetMode routines ------------------------- */

T
Thomas Winischhofer 已提交
3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625
static void
sisfb_check_engine_and_sync(struct sis_video_info *ivideo)
{
	u8 cr30, cr31;

	/* Check if MMIO and engines are enabled,
	 * and sync in case they are. Can't use
	 * ivideo->accel here, as this might have
	 * been changed before this is called.
	 */
	inSISIDXREG(SISSR, IND_SIS_PCI_ADDRESS_SET, cr30);
	inSISIDXREG(SISSR, IND_SIS_MODULE_ENABLE, cr31);
	/* MMIO and 2D/3D engine enabled? */
	if((cr30 & SIS_MEM_MAP_IO_ENABLE) && (cr31 & 0x42)) {
#ifdef CONFIG_FB_SIS_300
		if(ivideo->sisvga_engine == SIS_300_VGA) {
			/* Don't care about TurboQueue. It's
			 * enough to know that the engines
			 * are enabled
			 */
			sisfb_syncaccel(ivideo);
		}
#endif
#ifdef CONFIG_FB_SIS_315
		if(ivideo->sisvga_engine == SIS_315_VGA) {
			/* Check that any queue mode is
			 * enabled, and that the queue
			 * is not in the state of "reset"
			 */
			inSISIDXREG(SISSR, 0x26, cr30);
			if((cr30 & 0xe0) && (!(cr30 & 0x01))) {
				sisfb_syncaccel(ivideo);
			}
		}
#endif
	}
}

L
Linus Torvalds 已提交
3626 3627 3628 3629 3630 3631 3632 3633
static void
sisfb_pre_setmode(struct sis_video_info *ivideo)
{
	u8 cr30 = 0, cr31 = 0, cr33 = 0, cr35 = 0, cr38 = 0;
	int tvregnum = 0;

	ivideo->currentvbflags &= (VB_VIDEOBRIDGE | VB_DISPTYPE_DISP2);

T
Thomas Winischhofer 已提交
3634 3635
	outSISIDXREG(SISSR, 0x05, 0x86);

L
Linus Torvalds 已提交
3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662
	inSISIDXREG(SISCR, 0x31, cr31);
	cr31 &= ~0x60;
	cr31 |= 0x04;

	cr33 = ivideo->rate_idx & 0x0F;

#ifdef CONFIG_FB_SIS_315
	if(ivideo->sisvga_engine == SIS_315_VGA) {
	   if(ivideo->chip >= SIS_661) {
	      inSISIDXREG(SISCR, 0x38, cr38);
	      cr38 &= ~0x07;  /* Clear LCDA/DualEdge and YPbPr bits */
	   } else {
	      tvregnum = 0x38;
	      inSISIDXREG(SISCR, tvregnum, cr38);
	      cr38 &= ~0x3b;  /* Clear LCDA/DualEdge and YPbPr bits */
	   }
	}
#endif
#ifdef CONFIG_FB_SIS_300
	if(ivideo->sisvga_engine == SIS_300_VGA) {
	   tvregnum = 0x35;
	   inSISIDXREG(SISCR, tvregnum, cr38);
	}
#endif

	SiS_SetEnableDstn(&ivideo->SiS_Pr, FALSE);
	SiS_SetEnableFstn(&ivideo->SiS_Pr, FALSE);
T
Thomas Winischhofer 已提交
3663
	ivideo->curFSTN = ivideo->curDSTN = 0;
L
Linus Torvalds 已提交
3664 3665 3666 3667 3668

	switch(ivideo->currentvbflags & VB_DISPTYPE_DISP2) {

	   case CRT2_TV:
	      cr38 &= ~0xc0;   /* Clear PAL-M / PAL-N bits */
T
Thomas Winischhofer 已提交
3669
	      if((ivideo->vbflags & TV_YPBPR) && (ivideo->vbflags2 & VB2_SISYPBPRBRIDGE)) {
L
Linus Torvalds 已提交
3670
#ifdef CONFIG_FB_SIS_315
T
Thomas Winischhofer 已提交
3671 3672 3673
		 if(ivideo->chip >= SIS_661) {
		    cr38 |= 0x04;
		    if(ivideo->vbflags & TV_YPBPR525P)       cr35 |= 0x20;
L
Linus Torvalds 已提交
3674 3675 3676 3677 3678
		    else if(ivideo->vbflags & TV_YPBPR750P)  cr35 |= 0x40;
		    else if(ivideo->vbflags & TV_YPBPR1080I) cr35 |= 0x60;
		    cr30 |= SIS_SIMULTANEOUS_VIEW_ENABLE;
		    cr35 &= ~0x01;
		    ivideo->currentvbflags |= (TV_YPBPR | (ivideo->vbflags & TV_YPBPRALL));
T
Thomas Winischhofer 已提交
3679 3680
		 } else if(ivideo->sisvga_engine == SIS_315_VGA) {
		    cr30 |= (0x80 | SIS_SIMULTANEOUS_VIEW_ENABLE);
L
Linus Torvalds 已提交
3681
		    cr38 |= 0x08;
T
Thomas Winischhofer 已提交
3682
		    if(ivideo->vbflags & TV_YPBPR525P)       cr38 |= 0x10;
L
Linus Torvalds 已提交
3683 3684 3685 3686
		    else if(ivideo->vbflags & TV_YPBPR750P)  cr38 |= 0x20;
		    else if(ivideo->vbflags & TV_YPBPR1080I) cr38 |= 0x30;
		    cr31 &= ~0x01;
		    ivideo->currentvbflags |= (TV_YPBPR | (ivideo->vbflags & TV_YPBPRALL));
T
Thomas Winischhofer 已提交
3687 3688 3689 3690 3691 3692 3693 3694 3695 3696
		 }
#endif
	      } else if((ivideo->vbflags & TV_HIVISION) &&
				(ivideo->vbflags2 & VB2_SISHIVISIONBRIDGE)) {
		 if(ivideo->chip >= SIS_661) {
		    cr38 |= 0x04;
		    cr35 |= 0x60;
		 } else {
		    cr30 |= 0x80;
		 }
L
Linus Torvalds 已提交
3697
		 cr30 |= SIS_SIMULTANEOUS_VIEW_ENABLE;
T
Thomas Winischhofer 已提交
3698 3699
		 cr31 |= 0x01;
		 cr35 |= 0x01;
L
Linus Torvalds 已提交
3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717
		 ivideo->currentvbflags |= TV_HIVISION;
	      } else if(ivideo->vbflags & TV_SCART) {
		 cr30 = (SIS_VB_OUTPUT_SCART | SIS_SIMULTANEOUS_VIEW_ENABLE);
		 cr31 |= 0x01;
		 cr35 |= 0x01;
		 ivideo->currentvbflags |= TV_SCART;
	      } else {
		 if(ivideo->vbflags & TV_SVIDEO) {
		    cr30 = (SIS_VB_OUTPUT_SVIDEO | SIS_SIMULTANEOUS_VIEW_ENABLE);
		    ivideo->currentvbflags |= TV_SVIDEO;
		 }
		 if(ivideo->vbflags & TV_AVIDEO) {
		    cr30 = (SIS_VB_OUTPUT_COMPOSITE | SIS_SIMULTANEOUS_VIEW_ENABLE);
		    ivideo->currentvbflags |= TV_AVIDEO;
		 }
	      }
	      cr31 |= SIS_DRIVER_MODE;

T
Thomas Winischhofer 已提交
3718 3719
	      if(ivideo->vbflags & (TV_AVIDEO | TV_SVIDEO)) {
		 if(ivideo->vbflags & TV_PAL) {
L
Linus Torvalds 已提交
3720 3721 3722 3723 3724 3725 3726 3727
		    cr31 |= 0x01; cr35 |= 0x01;
		    ivideo->currentvbflags |= TV_PAL;
		    if(ivideo->vbflags & TV_PALM) {
		       cr38 |= 0x40; cr35 |= 0x04;
		       ivideo->currentvbflags |= TV_PALM;
		    } else if(ivideo->vbflags & TV_PALN) {
		       cr38 |= 0x80; cr35 |= 0x08;
		       ivideo->currentvbflags |= TV_PALN;
T
Thomas Winischhofer 已提交
3728 3729
		    }
		 } else {
L
Linus Torvalds 已提交
3730 3731 3732 3733 3734
		    cr31 &= ~0x01; cr35 &= ~0x01;
		    ivideo->currentvbflags |= TV_NTSC;
		    if(ivideo->vbflags & TV_NTSCJ) {
		       cr38 |= 0x40; cr35 |= 0x02;
		       ivideo->currentvbflags |= TV_NTSCJ;
T
Thomas Winischhofer 已提交
3735
		    }
L
Linus Torvalds 已提交
3736 3737 3738 3739 3740 3741 3742 3743 3744
		 }
	      }
	      break;

	   case CRT2_LCD:
	      cr30  = (SIS_VB_OUTPUT_LCD | SIS_SIMULTANEOUS_VIEW_ENABLE);
	      cr31 |= SIS_DRIVER_MODE;
	      SiS_SetEnableDstn(&ivideo->SiS_Pr, ivideo->sisfb_dstn);
	      SiS_SetEnableFstn(&ivideo->SiS_Pr, ivideo->sisfb_fstn);
T
Thomas Winischhofer 已提交
3745 3746
	      ivideo->curFSTN = ivideo->sisfb_fstn;
	      ivideo->curDSTN = ivideo->sisfb_dstn;
L
Linus Torvalds 已提交
3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779
	      break;

	   case CRT2_VGA:
	      cr30 = (SIS_VB_OUTPUT_CRT2 | SIS_SIMULTANEOUS_VIEW_ENABLE);
	      cr31 |= SIS_DRIVER_MODE;
	      if(ivideo->sisfb_nocrt2rate) {
		 cr33 |= (sisbios_mode[ivideo->sisfb_mode_idx].rate_idx << 4);
	      } else {
		 cr33 |= ((ivideo->rate_idx & 0x0F) << 4);
	      }
	      break;

	   default:	/* disable CRT2 */
	      cr30 = 0x00;
	      cr31 |= (SIS_DRIVER_MODE | SIS_VB_OUTPUT_DISABLE);
	}

	outSISIDXREG(SISCR, 0x30, cr30);
	outSISIDXREG(SISCR, 0x33, cr33);

	if(ivideo->chip >= SIS_661) {
#ifdef CONFIG_FB_SIS_315
	   cr31 &= ~0x01;                          /* Clear PAL flag (now in CR35) */
	   setSISIDXREG(SISCR, 0x35, ~0x10, cr35); /* Leave overscan bit alone */
	   cr38 &= 0x07;                           /* Use only LCDA and HiVision/YPbPr bits */
	   setSISIDXREG(SISCR, 0x38, 0xf8, cr38);
#endif
	} else if(ivideo->chip != SIS_300) {
	   outSISIDXREG(SISCR, tvregnum, cr38);
	}
	outSISIDXREG(SISCR, 0x31, cr31);

	ivideo->SiS_Pr.SiS_UseOEM = ivideo->sisfb_useoem;
T
Thomas Winischhofer 已提交
3780 3781

	sisfb_check_engine_and_sync(ivideo);
L
Linus Torvalds 已提交
3782 3783 3784 3785 3786 3787 3788
}

/* Fix SR11 for 661 and later */
#ifdef CONFIG_FB_SIS_315
static void
sisfb_fixup_SR11(struct sis_video_info *ivideo)
{
T
Thomas Winischhofer 已提交
3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802
	u8  tmpreg;

	if(ivideo->chip >= SIS_661) {
		inSISIDXREG(SISSR,0x11,tmpreg);
		if(tmpreg & 0x20) {
			inSISIDXREG(SISSR,0x3e,tmpreg);
			tmpreg = (tmpreg + 1) & 0xff;
			outSISIDXREG(SISSR,0x3e,tmpreg);
			inSISIDXREG(SISSR,0x11,tmpreg);
		}
		if(tmpreg & 0xf0) {
			andSISIDXREG(SISSR,0x11,0x0f);
		}
	}
L
Linus Torvalds 已提交
3803 3804 3805
}
#endif

T
Thomas Winischhofer 已提交
3806 3807
static void
sisfb_set_TVxposoffset(struct sis_video_info *ivideo, int val)
L
Linus Torvalds 已提交
3808
{
T
Thomas Winischhofer 已提交
3809 3810 3811
	if(val > 32) val = 32;
	if(val < -32) val = -32;
	ivideo->tvxpos = val;
L
Linus Torvalds 已提交
3812

T
Thomas Winischhofer 已提交
3813 3814
	if(ivideo->sisfblocked) return;
	if(!ivideo->modechanged) return;
L
Linus Torvalds 已提交
3815

T
Thomas Winischhofer 已提交
3816
	if(ivideo->currentvbflags & CRT2_TV) {
L
Linus Torvalds 已提交
3817

T
Thomas Winischhofer 已提交
3818
		if(ivideo->vbflags2 & VB2_CHRONTEL) {
L
Linus Torvalds 已提交
3819

T
Thomas Winischhofer 已提交
3820
			int x = ivideo->tvx;
L
Linus Torvalds 已提交
3821

T
Thomas Winischhofer 已提交
3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861
			switch(ivideo->chronteltype) {
			case 1:
				x += val;
				if(x < 0) x = 0;
				outSISIDXREG(SISSR,0x05,0x86);
				SiS_SetCH700x(&ivideo->SiS_Pr, 0x0a, (x & 0xff));
				SiS_SetCH70xxANDOR(&ivideo->SiS_Pr, 0x08, ((x & 0x0100) >> 7), 0xFD);
				break;
			case 2:
				/* Not supported by hardware */
				break;
			}

		} else if(ivideo->vbflags2 & VB2_SISBRIDGE) {

			u8 p2_1f,p2_20,p2_2b,p2_42,p2_43;
			unsigned short temp;

			p2_1f = ivideo->p2_1f;
			p2_20 = ivideo->p2_20;
			p2_2b = ivideo->p2_2b;
			p2_42 = ivideo->p2_42;
			p2_43 = ivideo->p2_43;

			temp = p2_1f | ((p2_20 & 0xf0) << 4);
			temp += (val * 2);
			p2_1f = temp & 0xff;
			p2_20 = (temp & 0xf00) >> 4;
			p2_2b = ((p2_2b & 0x0f) + (val * 2)) & 0x0f;
			temp = p2_43 | ((p2_42 & 0xf0) << 4);
			temp += (val * 2);
			p2_43 = temp & 0xff;
			p2_42 = (temp & 0xf00) >> 4;
			outSISIDXREG(SISPART2,0x1f,p2_1f);
			setSISIDXREG(SISPART2,0x20,0x0F,p2_20);
			setSISIDXREG(SISPART2,0x2b,0xF0,p2_2b);
			setSISIDXREG(SISPART2,0x42,0x0F,p2_42);
			outSISIDXREG(SISPART2,0x43,p2_43);
		}
	}
L
Linus Torvalds 已提交
3862 3863
}

T
Thomas Winischhofer 已提交
3864 3865
static void
sisfb_set_TVyposoffset(struct sis_video_info *ivideo, int val)
L
Linus Torvalds 已提交
3866
{
T
Thomas Winischhofer 已提交
3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911
	if(val > 32) val = 32;
	if(val < -32) val = -32;
	ivideo->tvypos = val;

	if(ivideo->sisfblocked) return;
	if(!ivideo->modechanged) return;

	if(ivideo->currentvbflags & CRT2_TV) {

		if(ivideo->vbflags2 & VB2_CHRONTEL) {

			int y = ivideo->tvy;

			switch(ivideo->chronteltype) {
			case 1:
				y -= val;
				if(y < 0) y = 0;
				outSISIDXREG(SISSR,0x05,0x86);
				SiS_SetCH700x(&ivideo->SiS_Pr, 0x0b, (y & 0xff));
				SiS_SetCH70xxANDOR(&ivideo->SiS_Pr, 0x08, ((y & 0x0100) >> 8), 0xFE);
				break;
			case 2:
				/* Not supported by hardware */
				break;
			}

		} else if(ivideo->vbflags2 & VB2_SISBRIDGE) {

			char p2_01, p2_02;
			val /= 2;
			p2_01 = ivideo->p2_01;
			p2_02 = ivideo->p2_02;

			p2_01 += val;
			p2_02 += val;
			if(!(ivideo->currentvbflags & (TV_HIVISION | TV_YPBPR))) {
				while((p2_01 <= 0) || (p2_02 <= 0)) {
					p2_01 += 2;
					p2_02 += 2;
				}
			}
			outSISIDXREG(SISPART2,0x01,p2_01);
			outSISIDXREG(SISPART2,0x02,p2_02);
		}
	}
L
Linus Torvalds 已提交
3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925
}

static void
sisfb_post_setmode(struct sis_video_info *ivideo)
{
	BOOLEAN crt1isoff = FALSE;
	BOOLEAN doit = TRUE;
#if defined(CONFIG_FB_SIS_300) || defined(CONFIG_FB_SIS_315)
	u8 reg;
#endif
#ifdef CONFIG_FB_SIS_315
	u8 reg1;
#endif

T
Thomas Winischhofer 已提交
3926
	outSISIDXREG(SISSR, 0x05, 0x86);
L
Linus Torvalds 已提交
3927 3928 3929 3930 3931 3932

#ifdef CONFIG_FB_SIS_315
	sisfb_fixup_SR11(ivideo);
#endif

	/* Now we actually HAVE changed the display mode */
T
Thomas Winischhofer 已提交
3933
	ivideo->modechanged = 1;
L
Linus Torvalds 已提交
3934 3935

	/* We can't switch off CRT1 if bridge is in slave mode */
T
Thomas Winischhofer 已提交
3936
	if(ivideo->vbflags2 & VB2_VIDEOBRIDGE) {
L
Linus Torvalds 已提交
3937
		if(sisfb_bridgeisslave(ivideo)) doit = FALSE;
T
Thomas Winischhofer 已提交
3938 3939
	} else
		ivideo->sisfb_crt1off = 0;
L
Linus Torvalds 已提交
3940 3941 3942

#ifdef CONFIG_FB_SIS_300
	if(ivideo->sisvga_engine == SIS_300_VGA) {
T
Thomas Winischhofer 已提交
3943 3944 3945 3946 3947 3948 3949 3950
		if((ivideo->sisfb_crt1off) && (doit)) {
			crt1isoff = TRUE;
			reg = 0x00;
		} else {
			crt1isoff = FALSE;
			reg = 0x80;
		}
		setSISIDXREG(SISCR, 0x17, 0x7f, reg);
L
Linus Torvalds 已提交
3951 3952 3953 3954
	}
#endif
#ifdef CONFIG_FB_SIS_315
	if(ivideo->sisvga_engine == SIS_315_VGA) {
T
Thomas Winischhofer 已提交
3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965
		if((ivideo->sisfb_crt1off) && (doit)) {
			crt1isoff = TRUE;
			reg  = 0x40;
			reg1 = 0xc0;
		} else {
			crt1isoff = FALSE;
			reg  = 0x00;
			reg1 = 0x00;
		}
		setSISIDXREG(SISCR, ivideo->SiS_Pr.SiS_MyCR63, ~0x40, reg);
		setSISIDXREG(SISSR, 0x1f, ~0xc0, reg1);
L
Linus Torvalds 已提交
3966 3967 3968 3969
	}
#endif

	if(crt1isoff) {
T
Thomas Winischhofer 已提交
3970 3971
		ivideo->currentvbflags &= ~VB_DISPTYPE_CRT1;
		ivideo->currentvbflags |= VB_SINGLE_MODE;
L
Linus Torvalds 已提交
3972
	} else {
T
Thomas Winischhofer 已提交
3973 3974 3975 3976 3977 3978
		ivideo->currentvbflags |= VB_DISPTYPE_CRT1;
		if(ivideo->currentvbflags & VB_DISPTYPE_CRT2) {
			ivideo->currentvbflags |= VB_MIRROR_MODE;
		} else {
			ivideo->currentvbflags |= VB_SINGLE_MODE;
		}
L
Linus Torvalds 已提交
3979 3980
	}

T
Thomas Winischhofer 已提交
3981
	andSISIDXREG(SISSR, IND_SIS_RAMDAC_CONTROL, ~0x04);
L
Linus Torvalds 已提交
3982 3983

	if(ivideo->currentvbflags & CRT2_TV) {
T
Thomas Winischhofer 已提交
3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999
		if(ivideo->vbflags2 & VB2_SISBRIDGE) {
			inSISIDXREG(SISPART2,0x1f,ivideo->p2_1f);
			inSISIDXREG(SISPART2,0x20,ivideo->p2_20);
			inSISIDXREG(SISPART2,0x2b,ivideo->p2_2b);
			inSISIDXREG(SISPART2,0x42,ivideo->p2_42);
			inSISIDXREG(SISPART2,0x43,ivideo->p2_43);
			inSISIDXREG(SISPART2,0x01,ivideo->p2_01);
			inSISIDXREG(SISPART2,0x02,ivideo->p2_02);
		} else if(ivideo->vbflags2 & VB2_CHRONTEL) {
			if(ivideo->chronteltype == 1) {
				ivideo->tvx = SiS_GetCH700x(&ivideo->SiS_Pr, 0x0a);
				ivideo->tvx |= (((SiS_GetCH700x(&ivideo->SiS_Pr, 0x08) & 0x02) >> 1) << 8);
				ivideo->tvy = SiS_GetCH700x(&ivideo->SiS_Pr, 0x0b);
				ivideo->tvy |= ((SiS_GetCH700x(&ivideo->SiS_Pr, 0x08) & 0x01) << 8);
			}
		}
L
Linus Torvalds 已提交
4000 4001 4002
	}

	if(ivideo->tvxpos) {
T
Thomas Winischhofer 已提交
4003
		sisfb_set_TVxposoffset(ivideo, ivideo->tvxpos);
L
Linus Torvalds 已提交
4004 4005
	}
	if(ivideo->tvypos) {
T
Thomas Winischhofer 已提交
4006
		sisfb_set_TVyposoffset(ivideo, ivideo->tvypos);
L
Linus Torvalds 已提交
4007 4008
	}

T
Thomas Winischhofer 已提交
4009 4010
	/* Eventually sync engines */
	sisfb_check_engine_and_sync(ivideo);
L
Linus Torvalds 已提交
4011

T
Thomas Winischhofer 已提交
4012 4013 4014 4015 4016 4017 4018
	/* (Re-)Initialize chip engines */
	if(ivideo->accel) {
		sisfb_engine_init(ivideo);
	} else {
		ivideo->engineok = 0;
	}
}
L
Linus Torvalds 已提交
4019

T
Thomas Winischhofer 已提交
4020 4021 4022 4023 4024
static int
sisfb_reset_mode(struct sis_video_info *ivideo)
{
	if(sisfb_set_mode(ivideo, 0))
		return 1;
L
Linus Torvalds 已提交
4025

T
Thomas Winischhofer 已提交
4026 4027 4028
	sisfb_set_pitch(ivideo);
	sisfb_set_base_CRT1(ivideo, ivideo->current_base);
	sisfb_set_base_CRT2(ivideo, ivideo->current_base);
L
Linus Torvalds 已提交
4029

T
Thomas Winischhofer 已提交
4030 4031 4032 4033 4034 4035 4036
	return 0;
}

static void
sisfb_handle_command(struct sis_video_info *ivideo, struct sisfb_cmd *sisfb_command)
{
	int mycrt1off;
L
Linus Torvalds 已提交
4037

T
Thomas Winischhofer 已提交
4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068
	switch(sisfb_command->sisfb_cmd) {
	case SISFB_CMD_GETVBFLAGS:
		if(!ivideo->modechanged) {
			sisfb_command->sisfb_result[0] = SISFB_CMD_ERR_EARLY;
		} else {
			sisfb_command->sisfb_result[0] = SISFB_CMD_ERR_OK;
			sisfb_command->sisfb_result[1] = ivideo->currentvbflags;
			sisfb_command->sisfb_result[2] = ivideo->vbflags2;
		}
		break;
	case SISFB_CMD_SWITCHCRT1:
		/* arg[0]: 0 = off, 1 = on, 99 = query */
		if(!ivideo->modechanged) {
			sisfb_command->sisfb_result[0] = SISFB_CMD_ERR_EARLY;
		} else if(sisfb_command->sisfb_arg[0] == 99) {
			/* Query */
			sisfb_command->sisfb_result[1] = ivideo->sisfb_crt1off ? 0 : 1;
			sisfb_command->sisfb_result[0] = SISFB_CMD_ERR_OK;
		} else if(ivideo->sisfblocked) {
			sisfb_command->sisfb_result[0] = SISFB_CMD_ERR_LOCKED;
		} else if((!(ivideo->currentvbflags & CRT2_ENABLE)) &&
					(sisfb_command->sisfb_arg[0] == 0)) {
			sisfb_command->sisfb_result[0] = SISFB_CMD_ERR_NOCRT2;
		} else {
			sisfb_command->sisfb_result[0] = SISFB_CMD_ERR_OK;
			mycrt1off = sisfb_command->sisfb_arg[0] ? 0 : 1;
			if( ((ivideo->currentvbflags & VB_DISPTYPE_CRT1) && mycrt1off) ||
			    ((!(ivideo->currentvbflags & VB_DISPTYPE_CRT1)) && !mycrt1off) ) {
				ivideo->sisfb_crt1off = mycrt1off;
				if(sisfb_reset_mode(ivideo)) {
					sisfb_command->sisfb_result[0] = SISFB_CMD_ERR_OTHER;
L
Linus Torvalds 已提交
4069 4070
				}
			}
T
Thomas Winischhofer 已提交
4071
			sisfb_command->sisfb_result[1] = ivideo->sisfb_crt1off ? 0 : 1;
L
Linus Torvalds 已提交
4072
		}
T
Thomas Winischhofer 已提交
4073 4074 4075 4076 4077 4078
		break;
	/* more to come */
	default:
		sisfb_command->sisfb_result[0] = SISFB_CMD_ERR_UNKNOWN;
		printk(KERN_ERR "sisfb: Unknown command 0x%x\n",
			sisfb_command->sisfb_cmd);
L
Linus Torvalds 已提交
4079 4080 4081 4082
	}
}

#ifndef MODULE
T
Thomas Winischhofer 已提交
4083 4084
SISINITSTATIC int __init
sisfb_setup(char *options)
L
Linus Torvalds 已提交
4085 4086 4087
{
	char *this_opt;

T
Thomas Winischhofer 已提交
4088
	sisfb_setdefaultparms();
L
Linus Torvalds 已提交
4089

T
Thomas Winischhofer 已提交
4090
	if(!options || !(*options))
L
Linus Torvalds 已提交
4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103
		return 0;

	while((this_opt = strsep(&options, ",")) != NULL) {

		if(!(*this_opt)) continue;

		if(!strnicmp(this_opt, "off", 3)) {
			sisfb_off = 1;
		} else if(!strnicmp(this_opt, "forcecrt2type:", 14)) {
			/* Need to check crt2 type first for fstn/dstn */
			sisfb_search_crt2type(this_opt + 14);
		} else if(!strnicmp(this_opt, "tvmode:",7)) {
			sisfb_search_tvstd(this_opt + 7);
T
Thomas Winischhofer 已提交
4104 4105
		} else if(!strnicmp(this_opt, "tvstandard:",11)) {
			sisfb_search_tvstd(this_opt + 11);
L
Linus Torvalds 已提交
4106 4107 4108 4109 4110 4111 4112 4113 4114
		} else if(!strnicmp(this_opt, "mode:", 5)) {
			sisfb_search_mode(this_opt + 5, FALSE);
		} else if(!strnicmp(this_opt, "vesa:", 5)) {
			sisfb_search_vesamode(simple_strtoul(this_opt + 5, NULL, 0), FALSE);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
		} else if(!strnicmp(this_opt, "inverse", 7)) {
			sisfb_inverse = 1;
			/* fb_invert_cmaps(); */
		} else if(!strnicmp(this_opt, "font:", 5)) {
T
Thomas Winischhofer 已提交
4115
			if(strlen(this_opt + 5) < 40) {
L
Linus Torvalds 已提交
4116 4117 4118 4119 4120 4121 4122 4123
			   strncpy(sisfb_fontname, this_opt + 5, sizeof(sisfb_fontname) - 1);
			   sisfb_fontname[sizeof(sisfb_fontname) - 1] = '\0';
			}
#endif
		} else if(!strnicmp(this_opt, "rate:", 5)) {
			sisfb_parm_rate = simple_strtoul(this_opt + 5, NULL, 0);
		} else if(!strnicmp(this_opt, "forcecrt1:", 10)) {
			sisfb_forcecrt1 = (int)simple_strtoul(this_opt + 10, NULL, 0);
T
Thomas Winischhofer 已提交
4124 4125
		} else if(!strnicmp(this_opt, "mem:",4)) {
			sisfb_parm_mem = simple_strtoul(this_opt + 4, NULL, 0);
L
Linus Torvalds 已提交
4126
		} else if(!strnicmp(this_opt, "pdc:", 4)) {
T
Thomas Winischhofer 已提交
4127
			sisfb_pdc = simple_strtoul(this_opt + 4, NULL, 0);
L
Linus Torvalds 已提交
4128
		} else if(!strnicmp(this_opt, "pdc1:", 5)) {
T
Thomas Winischhofer 已提交
4129
			sisfb_pdca = simple_strtoul(this_opt + 5, NULL, 0);
L
Linus Torvalds 已提交
4130 4131 4132 4133 4134
		} else if(!strnicmp(this_opt, "noaccel", 7)) {
			sisfb_accel = 0;
		} else if(!strnicmp(this_opt, "accel", 5)) {
			sisfb_accel = -1;
		} else if(!strnicmp(this_opt, "noypan", 6)) {
T
Thomas Winischhofer 已提交
4135
			sisfb_ypan = 0;
L
Linus Torvalds 已提交
4136
		} else if(!strnicmp(this_opt, "ypan", 4)) {
T
Thomas Winischhofer 已提交
4137
			sisfb_ypan = -1;
L
Linus Torvalds 已提交
4138
		} else if(!strnicmp(this_opt, "nomax", 5)) {
T
Thomas Winischhofer 已提交
4139
			sisfb_max = 0;
L
Linus Torvalds 已提交
4140
		} else if(!strnicmp(this_opt, "max", 3)) {
T
Thomas Winischhofer 已提交
4141
			sisfb_max = -1;
L
Linus Torvalds 已提交
4142 4143 4144 4145 4146 4147
		} else if(!strnicmp(this_opt, "userom:", 7)) {
			sisfb_userom = (int)simple_strtoul(this_opt + 7, NULL, 0);
		} else if(!strnicmp(this_opt, "useoem:", 7)) {
			sisfb_useoem = (int)simple_strtoul(this_opt + 7, NULL, 0);
		} else if(!strnicmp(this_opt, "nocrt2rate", 10)) {
			sisfb_nocrt2rate = 1;
T
Thomas Winischhofer 已提交
4148 4149 4150 4151
		} else if(!strnicmp(this_opt, "scalelcd:", 9)) {
			unsigned long temp = 2;
			temp = simple_strtoul(this_opt + 9, NULL, 0);
			if((temp == 0) || (temp == 1)) {
L
Linus Torvalds 已提交
4152
			   sisfb_scalelcd = temp ^ 1;
T
Thomas Winischhofer 已提交
4153
			}
L
Linus Torvalds 已提交
4154
		} else if(!strnicmp(this_opt, "tvxposoffset:", 13)) {
T
Thomas Winischhofer 已提交
4155 4156 4157
			int temp = 0;
			temp = (int)simple_strtol(this_opt + 13, NULL, 0);
			if((temp >= -32) && (temp <= 32)) {
L
Linus Torvalds 已提交
4158
			   sisfb_tvxposoffset = temp;
T
Thomas Winischhofer 已提交
4159
			}
L
Linus Torvalds 已提交
4160
		} else if(!strnicmp(this_opt, "tvyposoffset:", 13)) {
T
Thomas Winischhofer 已提交
4161 4162 4163
			int temp = 0;
			temp = (int)simple_strtol(this_opt + 13, NULL, 0);
			if((temp >= -32) && (temp <= 32)) {
L
Linus Torvalds 已提交
4164
			   sisfb_tvyposoffset = temp;
T
Thomas Winischhofer 已提交
4165
			}
L
Linus Torvalds 已提交
4166 4167 4168
		} else if(!strnicmp(this_opt, "specialtiming:", 14)) {
			sisfb_search_specialtiming(this_opt + 14);
		} else if(!strnicmp(this_opt, "lvdshl:", 7)) {
T
Thomas Winischhofer 已提交
4169 4170 4171
			int temp = 4;
			temp = simple_strtoul(this_opt + 7, NULL, 0);
			if((temp >= 0) && (temp <= 3)) {
L
Linus Torvalds 已提交
4172
			   sisfb_lvdshl = temp;
T
Thomas Winischhofer 已提交
4173
			}
L
Linus Torvalds 已提交
4174 4175 4176
		} else if(this_opt[0] >= '0' && this_opt[0] <= '9') {
			sisfb_search_mode(this_opt, TRUE);
#if !defined(__i386__) && !defined(__x86_64__)
T
Thomas Winischhofer 已提交
4177 4178
		} else if(!strnicmp(this_opt, "resetcard", 9)) {
			sisfb_resetcard = 1;
L
Linus Torvalds 已提交
4179
	        } else if(!strnicmp(this_opt, "videoram:", 9)) {
T
Thomas Winischhofer 已提交
4180
			sisfb_videoram = simple_strtoul(this_opt + 9, NULL, 0);
L
Linus Torvalds 已提交
4181 4182 4183 4184 4185 4186 4187
#endif
		} else {
			printk(KERN_INFO "sisfb: Invalid option %s\n", this_opt);
		}

	}

T
Thomas Winischhofer 已提交
4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016
	return 0;
}
#endif

static int __devinit
sisfb_check_rom(SIS_IOTYPE1 *rom_base, struct sis_video_info *ivideo)
{
	SIS_IOTYPE1 *rom;
	int romptr;

	if((readb(rom_base) != 0x55) || (readb(rom_base + 1) != 0xaa))
		return 0;

	romptr = (readb(rom_base + 0x18) | (readb(rom_base + 0x19) << 8));
	if(romptr > (0x10000 - 8))
		return 0;

	rom = rom_base + romptr;

	if((readb(rom)     != 'P') || (readb(rom + 1) != 'C') ||
	   (readb(rom + 2) != 'I') || (readb(rom + 3) != 'R'))
		return 0;

	if((readb(rom + 4) | (readb(rom + 5) << 8)) != ivideo->chip_vendor)
		return 0;

	if((readb(rom + 6) | (readb(rom + 7) << 8)) != ivideo->chip_id)
		return 0;

	return 1;
}

static unsigned char * __devinit
sisfb_find_rom(struct pci_dev *pdev)
{
	struct sis_video_info *ivideo = pci_get_drvdata(pdev);
	SIS_IOTYPE1 *rom_base;
	unsigned char *myrombase = NULL;
	u32 temp;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,11)
	size_t romsize;

	/* First, try the official pci ROM functions (except
	 * on integrated chipsets which have no ROM).
	 */

	if(!ivideo->nbridge) {

		if((rom_base = pci_map_rom(pdev, &romsize))) {

			if(sisfb_check_rom(rom_base, ivideo)) {

				if((myrombase = vmalloc(65536))) {

					/* Work around bug in pci/rom.c: Folks forgot to check
					 * whether the size retrieved from the BIOS image eventually
					 * is larger than the mapped size
					 */
					if(pci_resource_len(pdev, PCI_ROM_RESOURCE) < romsize)
						romsize = pci_resource_len(pdev, PCI_ROM_RESOURCE);

					memcpy_fromio(myrombase, rom_base,
							(romsize > 65536) ? 65536 : romsize);
				}
			}
			pci_unmap_rom(pdev, rom_base);
		}
	}

	if(myrombase) return myrombase;
#endif

	/* Otherwise do it the conventional way. */

#if defined(__i386__) || defined(__x86_64__)

	for(temp = 0x000c0000; temp < 0x000f0000; temp += 0x00001000) {

		rom_base = ioremap(temp, 65536);
		if(!rom_base)
			continue;

		if(!sisfb_check_rom(rom_base, ivideo)) {
			iounmap(rom_base);
			continue;
		}

		if((myrombase = vmalloc(65536)))
			memcpy_fromio(myrombase, rom_base, 65536);

		iounmap(rom_base);
		break;

        }

#else

	pci_read_config_dword(pdev, PCI_ROM_ADDRESS, &temp);
	pci_write_config_dword(pdev, PCI_ROM_ADDRESS,
			(ivideo->video_base & PCI_ROM_ADDRESS_MASK) | PCI_ROM_ADDRESS_ENABLE);

	rom_base = ioremap(ivideo->video_base, 65536);
	if(rom_base) {
		if(sisfb_check_rom(rom_base, ivideo)) {
			if((myrombase = vmalloc(65536)))
				memcpy_fromio(myrombase, rom_base, 65536);
		}
		iounmap(rom_base);
	}

	pci_write_config_dword(pdev, PCI_ROM_ADDRESS, temp);

#endif

	return myrombase;
}

static void __devinit
sisfb_post_map_vram(struct sis_video_info *ivideo, unsigned int *mapsize,
			unsigned int min)
{
	ivideo->video_vbase = ioremap(ivideo->video_base, (*mapsize));

	if(!ivideo->video_vbase) {
		printk(KERN_ERR
			"sisfb: Unable to map maximum video RAM for size detection\n");
		(*mapsize) >>= 1;
		while((!(ivideo->video_vbase = ioremap(ivideo->video_base, (*mapsize))))) {
			(*mapsize) >>= 1;
			if((*mapsize) < (min << 20))
				break;
		}
		if(ivideo->video_vbase) {
			printk(KERN_ERR
				"sisfb: Video RAM size detection limited to %dMB\n",
				(int)((*mapsize) >> 20));
		}
	}
}

#ifdef CONFIG_FB_SIS_300
static int __devinit
sisfb_post_300_buswidth(struct sis_video_info *ivideo)
{
	SIS_IOTYPE1 *FBAddress = ivideo->video_vbase;
	unsigned short temp;
	unsigned char reg;
	int i, j;

	andSISIDXREG(SISSR, 0x15, 0xFB);
	orSISIDXREG(SISSR, 0x15, 0x04);
	outSISIDXREG(SISSR, 0x13, 0x00);
	outSISIDXREG(SISSR, 0x14, 0xBF);

	for(i = 0; i < 2; i++) {
		temp = 0x1234;
		for(j = 0; j < 4; j++) {
			writew(temp, FBAddress);
			if(readw(FBAddress) == temp)
				break;
			orSISIDXREG(SISSR, 0x3c, 0x01);
			inSISIDXREG(SISSR, 0x05, reg);
			inSISIDXREG(SISSR, 0x05, reg);
			andSISIDXREG(SISSR, 0x3c, 0xfe);
			inSISIDXREG(SISSR, 0x05, reg);
			inSISIDXREG(SISSR, 0x05, reg);
			temp++;
		}
	}

	writel(0x01234567L, FBAddress);
	writel(0x456789ABL, (FBAddress + 4));
	writel(0x89ABCDEFL, (FBAddress + 8));
	writel(0xCDEF0123L, (FBAddress + 12));

	inSISIDXREG(SISSR, 0x3b, reg);
	if(reg & 0x01) {
		if(readl((FBAddress + 12)) == 0xCDEF0123L)
			return 4;	/* Channel A 128bit */
	}

	if(readl((FBAddress + 4)) == 0x456789ABL)
		return 2;		/* Channel B 64bit */

	return 1;			/* 32bit */
}

static int __devinit
sisfb_post_300_rwtest(struct sis_video_info *ivideo, int iteration, int buswidth,
			int PseudoRankCapacity, int PseudoAdrPinCount,
			unsigned int mapsize)
{
	SIS_IOTYPE1 *FBAddr = ivideo->video_vbase;
	unsigned short sr14;
	unsigned int k, RankCapacity, PageCapacity, BankNumHigh, BankNumMid;
	unsigned int PhysicalAdrOtherPage, PhysicalAdrHigh, PhysicalAdrHalfPage;
	static const unsigned short SiS_DRAMType[17][5] = {
		{0x0C,0x0A,0x02,0x40,0x39},
		{0x0D,0x0A,0x01,0x40,0x48},
		{0x0C,0x09,0x02,0x20,0x35},
		{0x0D,0x09,0x01,0x20,0x44},
		{0x0C,0x08,0x02,0x10,0x31},
		{0x0D,0x08,0x01,0x10,0x40},
		{0x0C,0x0A,0x01,0x20,0x34},
		{0x0C,0x09,0x01,0x08,0x32},
		{0x0B,0x08,0x02,0x08,0x21},
		{0x0C,0x08,0x01,0x08,0x30},
		{0x0A,0x08,0x02,0x04,0x11},
		{0x0B,0x0A,0x01,0x10,0x28},
		{0x09,0x08,0x02,0x02,0x01},
		{0x0B,0x09,0x01,0x08,0x24},
		{0x0B,0x08,0x01,0x04,0x20},
		{0x0A,0x08,0x01,0x02,0x10},
		{0x09,0x08,0x01,0x01,0x00}
	};

	 for(k = 0; k <= 16; k++) {

		RankCapacity = buswidth * SiS_DRAMType[k][3];

		if(RankCapacity != PseudoRankCapacity)
			continue;

		if((SiS_DRAMType[k][2] + SiS_DRAMType[k][0]) > PseudoAdrPinCount)
			continue;

		BankNumHigh = RankCapacity * 16 * iteration - 1;
		if(iteration == 3) {             /* Rank No */
			BankNumMid  = RankCapacity * 16 - 1;
		} else {
			BankNumMid  = RankCapacity * 16 * iteration / 2 - 1;
		}

		PageCapacity = (1 << SiS_DRAMType[k][1]) * buswidth * 4;
		PhysicalAdrHigh = BankNumHigh;
		PhysicalAdrHalfPage = (PageCapacity / 2 + PhysicalAdrHigh) % PageCapacity;
		PhysicalAdrOtherPage = PageCapacity * SiS_DRAMType[k][2] + PhysicalAdrHigh;

		andSISIDXREG(SISSR, 0x15, 0xFB); /* Test */
		orSISIDXREG(SISSR, 0x15, 0x04);  /* Test */
		sr14 = (SiS_DRAMType[k][3] * buswidth) - 1;
		if(buswidth == 4)      sr14 |= 0x80;
		else if(buswidth == 2) sr14 |= 0x40;
		outSISIDXREG(SISSR, 0x13, SiS_DRAMType[k][4]);
		outSISIDXREG(SISSR, 0x14, sr14);

		BankNumHigh <<= 16;
		BankNumMid <<= 16;

		if((BankNumHigh + PhysicalAdrHigh      >= mapsize) ||
		   (BankNumMid  + PhysicalAdrHigh      >= mapsize) ||
		   (BankNumHigh + PhysicalAdrHalfPage  >= mapsize) ||
		   (BankNumHigh + PhysicalAdrOtherPage >= mapsize))
			continue;

		/* Write data */
		writew(((unsigned short)PhysicalAdrHigh),
				(FBAddr + BankNumHigh + PhysicalAdrHigh));
		writew(((unsigned short)BankNumMid),
				(FBAddr + BankNumMid  + PhysicalAdrHigh));
		writew(((unsigned short)PhysicalAdrHalfPage),
				(FBAddr + BankNumHigh + PhysicalAdrHalfPage));
		writew(((unsigned short)PhysicalAdrOtherPage),
				(FBAddr + BankNumHigh + PhysicalAdrOtherPage));

		/* Read data */
		if(readw(FBAddr + BankNumHigh + PhysicalAdrHigh) == PhysicalAdrHigh)
			return 1;
	}

	return 0;
}

static void __devinit
sisfb_post_300_ramsize(struct pci_dev *pdev, unsigned int mapsize)
{
	struct	sis_video_info *ivideo = pci_get_drvdata(pdev);
	int	i, j, buswidth;
	int	PseudoRankCapacity, PseudoAdrPinCount;

	buswidth = sisfb_post_300_buswidth(ivideo);

	for(i = 6; i >= 0; i--) {
		PseudoRankCapacity = 1 << i;
		for(j = 4; j >= 1; j--) {
			PseudoAdrPinCount = 15 - j;
			if((PseudoRankCapacity * j) <= 64) {
				if(sisfb_post_300_rwtest(ivideo,
						j,
						buswidth,
						PseudoRankCapacity,
						PseudoAdrPinCount,
						mapsize))
					return;
			}
		}
	}
}

static void __devinit
sisfb_post_sis300(struct pci_dev *pdev)
{
	struct sis_video_info *ivideo = pci_get_drvdata(pdev);
	unsigned char *bios = ivideo->SiS_Pr.VirtualRomBase;
	u8  reg, v1, v2, v3, v4, v5, v6, v7, v8;
	u16 index, rindex, memtype = 0;
	unsigned int mapsize;

	if(!ivideo->SiS_Pr.UseROM)
		bios = NULL;

	outSISIDXREG(SISSR, 0x05, 0x86);

	if(bios) {
		if(bios[0x52] & 0x80) {
			memtype = bios[0x52];
		} else {
			inSISIDXREG(SISSR, 0x3a, memtype);
		}
		memtype &= 0x07;
	}

	v3 = 0x80; v6 = 0x80;
	if(ivideo->revision_id <= 0x13) {
		v1 = 0x44; v2 = 0x42;
		v4 = 0x44; v5 = 0x42;
	} else {
		v1 = 0x68; v2 = 0x43; /* Assume 125Mhz MCLK */
		v4 = 0x68; v5 = 0x43; /* Assume 125Mhz ECLK */
		if(bios) {
			index = memtype * 5;
			rindex = index + 0x54;
			v1 = bios[rindex++];
			v2 = bios[rindex++];
			v3 = bios[rindex++];
			rindex = index + 0x7c;
			v4 = bios[rindex++];
			v5 = bios[rindex++];
			v6 = bios[rindex++];
		}
	}
	outSISIDXREG(SISSR, 0x28, v1);
	outSISIDXREG(SISSR, 0x29, v2);
	outSISIDXREG(SISSR, 0x2a, v3);
	outSISIDXREG(SISSR, 0x2e, v4);
	outSISIDXREG(SISSR, 0x2f, v5);
	outSISIDXREG(SISSR, 0x30, v6);

	v1 = 0x10;
	if(bios)
		v1 = bios[0xa4];
	outSISIDXREG(SISSR, 0x07, v1);       /* DAC speed */

	outSISIDXREG(SISSR, 0x11, 0x0f);     /* DDC, power save */

	v1 = 0x01; v2 = 0x43; v3 = 0x1e; v4 = 0x2a;
	v5 = 0x06; v6 = 0x00; v7 = 0x00; v8 = 0x00;
	if(bios) {
		memtype += 0xa5;
		v1 = bios[memtype];
		v2 = bios[memtype + 8];
		v3 = bios[memtype + 16];
		v4 = bios[memtype + 24];
		v5 = bios[memtype + 32];
		v6 = bios[memtype + 40];
		v7 = bios[memtype + 48];
		v8 = bios[memtype + 56];
	}
	if(ivideo->revision_id >= 0x80)
		v3 &= 0xfd;
	outSISIDXREG(SISSR, 0x15, v1);       /* Ram type (assuming 0, BIOS 0xa5 step 8) */
	outSISIDXREG(SISSR, 0x16, v2);
	outSISIDXREG(SISSR, 0x17, v3);
	outSISIDXREG(SISSR, 0x18, v4);
	outSISIDXREG(SISSR, 0x19, v5);
	outSISIDXREG(SISSR, 0x1a, v6);
	outSISIDXREG(SISSR, 0x1b, v7);
	outSISIDXREG(SISSR, 0x1c, v8);	   /* ---- */
	andSISIDXREG(SISSR, 0x15 ,0xfb);
	orSISIDXREG(SISSR, 0x15, 0x04);
	if(bios) {
		if(bios[0x53] & 0x02) {
			orSISIDXREG(SISSR, 0x19, 0x20);
		}
	}
	v1 = 0x04;			   /* DAC pedestal (BIOS 0xe5) */
	if(ivideo->revision_id >= 0x80)
		v1 |= 0x01;
	outSISIDXREG(SISSR, 0x1f, v1);
	outSISIDXREG(SISSR, 0x20, 0xa4);     /* linear & relocated io & disable a0000 */
	v1 = 0xf6; v2 = 0x0d; v3 = 0x00;
	if(bios) {
		v1 = bios[0xe8];
		v2 = bios[0xe9];
		v3 = bios[0xea];
	}
	outSISIDXREG(SISSR, 0x23, v1);
	outSISIDXREG(SISSR, 0x24, v2);
	outSISIDXREG(SISSR, 0x25, v3);
	outSISIDXREG(SISSR, 0x21, 0x84);
	outSISIDXREG(SISSR, 0x22, 0x00);
	outSISIDXREG(SISCR, 0x37, 0x00);
	orSISIDXREG(SISPART1, 0x24, 0x01);   /* unlock crt2 */
	outSISIDXREG(SISPART1, 0x00, 0x00);
	v1 = 0x40; v2 = 0x11;
	if(bios) {
		v1 = bios[0xec];
		v2 = bios[0xeb];
	}
	outSISIDXREG(SISPART1, 0x02, v1);

	if(ivideo->revision_id >= 0x80)
		v2 &= ~0x01;

	inSISIDXREG(SISPART4, 0x00, reg);
	if((reg == 1) || (reg == 2)) {
		outSISIDXREG(SISCR, 0x37, 0x02);
		outSISIDXREG(SISPART2, 0x00, 0x1c);
		v4 = 0x00; v5 = 0x00; v6 = 0x10;
		if(ivideo->SiS_Pr.UseROM) {
			v4 = bios[0xf5];
			v5 = bios[0xf6];
			v6 = bios[0xf7];
		}
		outSISIDXREG(SISPART4, 0x0d, v4);
		outSISIDXREG(SISPART4, 0x0e, v5);
		outSISIDXREG(SISPART4, 0x10, v6);
		outSISIDXREG(SISPART4, 0x0f, 0x3f);
		inSISIDXREG(SISPART4, 0x01, reg);
		if(reg >= 0xb0) {
			inSISIDXREG(SISPART4, 0x23, reg);
			reg &= 0x20;
			reg <<= 1;
			outSISIDXREG(SISPART4, 0x23, reg);
		}
	} else {
		v2 &= ~0x10;
	}
	outSISIDXREG(SISSR, 0x32, v2);

	andSISIDXREG(SISPART1, 0x24, 0xfe);  /* Lock CRT2 */

	inSISIDXREG(SISSR, 0x16, reg);
	reg &= 0xc3;
	outSISIDXREG(SISCR, 0x35, reg);
	outSISIDXREG(SISCR, 0x83, 0x00);
#if !defined(__i386__) && !defined(__x86_64__)
	if(sisfb_videoram) {
		outSISIDXREG(SISSR, 0x13, 0x28);  /* ? */
		reg = ((sisfb_videoram >> 10) - 1) | 0x40;
		outSISIDXREG(SISSR, 0x14, reg);
	} else {
#endif
		/* Need to map max FB size for finding out about RAM size */
		mapsize = 64 << 20;
		sisfb_post_map_vram(ivideo, &mapsize, 4);

		if(ivideo->video_vbase) {
			sisfb_post_300_ramsize(pdev, mapsize);
			iounmap(ivideo->video_vbase);
		} else {
			printk(KERN_DEBUG
				"sisfb: Failed to map memory for size detection, assuming 8MB\n");
			outSISIDXREG(SISSR, 0x13, 0x28);  /* ? */
			outSISIDXREG(SISSR, 0x14, 0x47);  /* 8MB, 64bit default */
		}
#if !defined(__i386__) && !defined(__x86_64__)
	}
#endif
	if(bios) {
		v1 = bios[0xe6];
		v2 = bios[0xe7];
	} else {
		inSISIDXREG(SISSR, 0x3a, reg);
		if((reg & 0x30) == 0x30) {
			v1 = 0x04; /* PCI */
			v2 = 0x92;
		} else {
			v1 = 0x14; /* AGP */
			v2 = 0xb2;
		}
	}
	outSISIDXREG(SISSR, 0x21, v1);
	outSISIDXREG(SISSR, 0x22, v2);

	/* Sense CRT1 */
	sisfb_sense_crt1(ivideo);

	/* Set default mode, don't clear screen */
	ivideo->SiS_Pr.SiS_UseOEM = FALSE;
	SiS_SetEnableDstn(&ivideo->SiS_Pr, FALSE);
	SiS_SetEnableFstn(&ivideo->SiS_Pr, FALSE);
	ivideo->curFSTN = ivideo->curDSTN = 0;
	ivideo->SiS_Pr.VideoMemorySize = 8 << 20;
	SiSSetMode(&ivideo->SiS_Pr, 0x2e | 0x80);

	outSISIDXREG(SISSR, 0x05, 0x86);

	/* Display off */
	orSISIDXREG(SISSR, 0x01, 0x20);

	/* Save mode number in CR34 */
	outSISIDXREG(SISCR, 0x34, 0x2e);

	/* Let everyone know what the current mode is */
	ivideo->modeprechange = 0x2e;
}
#endif

#ifdef CONFIG_FB_SIS_315
#if 0
static void __devinit
sisfb_post_sis315330(struct pci_dev *pdev)
{
	/* TODO */
}
#endif

static void __devinit
sisfb_post_xgi_delay(struct sis_video_info *ivideo, int delay)
{
	unsigned int i;
	u8 reg;

	for(i = 0; i <= (delay * 10 * 36); i++) {
		inSISIDXREG(SISSR, 0x05, reg);
		reg++;
	}
}

static int __devinit
sisfb_find_host_bridge(struct sis_video_info *ivideo, struct pci_dev *mypdev,
				unsigned short pcivendor)
{
	struct pci_dev *pdev = NULL;
	unsigned short temp;
	int ret = 0;

	while((pdev = SIS_PCI_GET_CLASS(PCI_CLASS_BRIDGE_HOST, pdev))) {
		temp = pdev->vendor;
		SIS_PCI_PUT_DEVICE(pdev);
		if(temp == pcivendor) {
			ret = 1;
			break;
		}
	}

	return ret;
}

static int __devinit
sisfb_post_xgi_rwtest(struct sis_video_info *ivideo, int starta,
			unsigned int enda, unsigned int mapsize)
{
	unsigned int pos;
	int i;

	writel(0, ivideo->video_vbase);

	for(i = starta; i <= enda; i++) {
		pos = 1 << i;
		if(pos < mapsize)
			writel(pos, ivideo->video_vbase + pos);
	}

	sisfb_post_xgi_delay(ivideo, 150);

	if(readl(ivideo->video_vbase) != 0)
		return 0;

	for(i = starta; i <= enda; i++) {
		pos = 1 << i;
		if(pos < mapsize) {
			if(readl(ivideo->video_vbase + pos) != pos)
				return 0;
		} else
			return 0;
	}

	return 1;
}

static void __devinit
sisfb_post_xgi_ramsize(struct sis_video_info *ivideo)
{
	unsigned int buswidth, ranksize, channelab, mapsize;
	int i, j, k, l;
	u8 reg, sr14;
	static const u8 dramsr13[12 * 5] = {
		0x02, 0x0e, 0x0b, 0x80, 0x5d,
		0x02, 0x0e, 0x0a, 0x40, 0x59,
		0x02, 0x0d, 0x0b, 0x40, 0x4d,
		0x02, 0x0e, 0x09, 0x20, 0x55,
		0x02, 0x0d, 0x0a, 0x20, 0x49,
		0x02, 0x0c, 0x0b, 0x20, 0x3d,
		0x02, 0x0e, 0x08, 0x10, 0x51,
		0x02, 0x0d, 0x09, 0x10, 0x45,
		0x02, 0x0c, 0x0a, 0x10, 0x39,
		0x02, 0x0d, 0x08, 0x08, 0x41,
		0x02, 0x0c, 0x09, 0x08, 0x35,
		0x02, 0x0c, 0x08, 0x04, 0x31
	};
	static const u8 dramsr13_4[4 * 5] = {
		0x02, 0x0d, 0x09, 0x40, 0x45,
		0x02, 0x0c, 0x09, 0x20, 0x35,
		0x02, 0x0c, 0x08, 0x10, 0x31,
		0x02, 0x0b, 0x08, 0x08, 0x21
	};

	/* Enable linear mode, disable 0xa0000 address decoding */
	/* We disable a0000 address decoding, because
	 * - if running on x86, if the card is disabled, it means
	 *   that another card is in the system. We don't want
	 *   to interphere with that primary card's textmode.
	 * - if running on non-x86, there usually is no VGA window
	 *   at a0000.
	 */
	orSISIDXREG(SISSR, 0x20, (0x80 | 0x04));

	/* Need to map max FB size for finding out about RAM size */
	mapsize = 256 << 20;
	sisfb_post_map_vram(ivideo, &mapsize, 32);

	if(!ivideo->video_vbase) {
		printk(KERN_ERR "sisfb: Unable to detect RAM size. Setting default.\n");
		outSISIDXREG(SISSR, 0x13, 0x35);
		outSISIDXREG(SISSR, 0x14, 0x41);
		/* TODO */
		return;
	}

	/* Non-interleaving */
	outSISIDXREG(SISSR, 0x15, 0x00);
	/* No tiling */
	outSISIDXREG(SISSR, 0x1c, 0x00);

	if(ivideo->chip == XGI_20) {

		channelab = 1;
		inSISIDXREG(SISCR, 0x97, reg);
		if(!(reg & 0x01)) {	/* Single 32/16 */
			buswidth = 32;
			outSISIDXREG(SISSR, 0x13, 0xb1);
			outSISIDXREG(SISSR, 0x14, 0x52);
			sisfb_post_xgi_delay(ivideo, 1);
			sr14 = 0x02;
			if(sisfb_post_xgi_rwtest(ivideo, 23, 24, mapsize))
				goto bail_out;

			outSISIDXREG(SISSR, 0x13, 0x31);
			outSISIDXREG(SISSR, 0x14, 0x42);
			sisfb_post_xgi_delay(ivideo, 1);
			if(sisfb_post_xgi_rwtest(ivideo, 23, 23, mapsize))
				goto bail_out;

			buswidth = 16;
			outSISIDXREG(SISSR, 0x13, 0xb1);
			outSISIDXREG(SISSR, 0x14, 0x41);
			sisfb_post_xgi_delay(ivideo, 1);
			sr14 = 0x01;
			if(sisfb_post_xgi_rwtest(ivideo, 22, 23, mapsize))
				goto bail_out;
			else
				outSISIDXREG(SISSR, 0x13, 0x31);
		} else {		/* Dual 16/8 */
			buswidth = 16;
			outSISIDXREG(SISSR, 0x13, 0xb1);
			outSISIDXREG(SISSR, 0x14, 0x41);
			sisfb_post_xgi_delay(ivideo, 1);
			sr14 = 0x01;
			if(sisfb_post_xgi_rwtest(ivideo, 22, 23, mapsize))
				goto bail_out;

			outSISIDXREG(SISSR, 0x13, 0x31);
			outSISIDXREG(SISSR, 0x14, 0x31);
			sisfb_post_xgi_delay(ivideo, 1);
			if(sisfb_post_xgi_rwtest(ivideo, 22, 22, mapsize))
				goto bail_out;

			buswidth = 8;
			outSISIDXREG(SISSR, 0x13, 0xb1);
			outSISIDXREG(SISSR, 0x14, 0x30);
			sisfb_post_xgi_delay(ivideo, 1);
			sr14 = 0x00;
			if(sisfb_post_xgi_rwtest(ivideo, 21, 22, mapsize))
				goto bail_out;
			else
				outSISIDXREG(SISSR, 0x13, 0x31);
		}

	} else {	/* XGI_40 */

		inSISIDXREG(SISCR, 0x97, reg);
		if(!(reg & 0x10)) {
			inSISIDXREG(SISSR, 0x39, reg);
			reg >>= 1;
		}

		if(reg & 0x01) {	/* DDRII */
			buswidth = 32;
			if(ivideo->revision_id == 2) {
				channelab = 2;
				outSISIDXREG(SISSR, 0x13, 0xa1);
				outSISIDXREG(SISSR, 0x14, 0x44);
				sr14 = 0x04;
				sisfb_post_xgi_delay(ivideo, 1);
				if(sisfb_post_xgi_rwtest(ivideo, 23, 24, mapsize))
					goto bail_out;

				outSISIDXREG(SISSR, 0x13, 0x21);
				outSISIDXREG(SISSR, 0x14, 0x34);
				if(sisfb_post_xgi_rwtest(ivideo, 22, 23, mapsize))
					goto bail_out;

				channelab = 1;
				outSISIDXREG(SISSR, 0x13, 0xa1);
				outSISIDXREG(SISSR, 0x14, 0x40);
				sr14 = 0x00;
				if(sisfb_post_xgi_rwtest(ivideo, 22, 23, mapsize))
					goto bail_out;

				outSISIDXREG(SISSR, 0x13, 0x21);
				outSISIDXREG(SISSR, 0x14, 0x30);
			} else {
				channelab = 3;
				outSISIDXREG(SISSR, 0x13, 0xa1);
				outSISIDXREG(SISSR, 0x14, 0x4c);
				sr14 = 0x0c;
				sisfb_post_xgi_delay(ivideo, 1);
				if(sisfb_post_xgi_rwtest(ivideo, 23, 25, mapsize))
					goto bail_out;

				channelab = 2;
				outSISIDXREG(SISSR, 0x14, 0x48);
				sisfb_post_xgi_delay(ivideo, 1);
				sr14 = 0x08;
				if(sisfb_post_xgi_rwtest(ivideo, 23, 24, mapsize))
					goto bail_out;

				outSISIDXREG(SISSR, 0x13, 0x21);
				outSISIDXREG(SISSR, 0x14, 0x3c);
				sr14 = 0x0c;

				if(sisfb_post_xgi_rwtest(ivideo, 23, 24, mapsize)) {
					channelab = 3;
				} else {
					channelab = 2;
					outSISIDXREG(SISSR, 0x14, 0x38);
					sr14 = 0x08;
				}
			}
			sisfb_post_xgi_delay(ivideo, 1);

		} else {	/* DDR */

			buswidth = 64;
			if(ivideo->revision_id == 2) {
				channelab = 1;
				outSISIDXREG(SISSR, 0x13, 0xa1);
				outSISIDXREG(SISSR, 0x14, 0x52);
				sisfb_post_xgi_delay(ivideo, 1);
				sr14 = 0x02;
				if(sisfb_post_xgi_rwtest(ivideo, 23, 24, mapsize))
					goto bail_out;

				outSISIDXREG(SISSR, 0x13, 0x21);
				outSISIDXREG(SISSR, 0x14, 0x42);
			} else {
				channelab = 2;
				outSISIDXREG(SISSR, 0x13, 0xa1);
				outSISIDXREG(SISSR, 0x14, 0x5a);
				sisfb_post_xgi_delay(ivideo, 1);
				sr14 = 0x0a;
				if(sisfb_post_xgi_rwtest(ivideo, 24, 25, mapsize))
					goto bail_out;

				outSISIDXREG(SISSR, 0x13, 0x21);
				outSISIDXREG(SISSR, 0x14, 0x4a);
			}
			sisfb_post_xgi_delay(ivideo, 1);

		}
	}

bail_out:
	setSISIDXREG(SISSR, 0x14, 0xf0, sr14);
	sisfb_post_xgi_delay(ivideo, 1);

	j = (ivideo->chip == XGI_20) ? 5 : 9;
	k = (ivideo->chip == XGI_20) ? 12 : 4;

	for(i = 0; i < k; i++) {

		reg = (ivideo->chip == XGI_20) ?
				dramsr13[(i * 5) + 4] : dramsr13_4[(i * 5) + 4];
		setSISIDXREG(SISSR, 0x13, 0x80, reg);
		sisfb_post_xgi_delay(ivideo, 50);

		ranksize = (ivideo->chip == XGI_20) ?
				dramsr13[(i * 5) + 3] : dramsr13_4[(i * 5) + 3];

		inSISIDXREG(SISSR, 0x13, reg);
		if(reg & 0x80) ranksize <<= 1;

		if(ivideo->chip == XGI_20) {
			if(buswidth == 16)      ranksize <<= 1;
			else if(buswidth == 32) ranksize <<= 2;
		} else {
			if(buswidth == 64)      ranksize <<= 1;
		}

		reg = 0;
		l = channelab;
		if(l == 3) l = 4;
		if((ranksize * l) <= 256) {
			while((ranksize >>= 1)) reg += 0x10;
		}

		if(!reg) continue;

		setSISIDXREG(SISSR, 0x14, 0x0f, (reg & 0xf0));
		sisfb_post_xgi_delay(ivideo, 1);

		if(sisfb_post_xgi_rwtest(ivideo, j, ((reg >> 4) + channelab - 2 + 20), mapsize))
			break;
	}

	iounmap(ivideo->video_vbase);
}
L
Linus Torvalds 已提交
5017

T
Thomas Winischhofer 已提交
5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042
static void __devinit
sisfb_post_xgi_setclocks(struct sis_video_info *ivideo, u8 regb)
{
	u8 v1, v2, v3;
	int index;
	static const u8 cs90[8 * 3] = {
		0x16, 0x01, 0x01,
		0x3e, 0x03, 0x01,
		0x7c, 0x08, 0x01,
		0x79, 0x06, 0x01,
		0x29, 0x01, 0x81,
		0x5c, 0x23, 0x01,
		0x5c, 0x23, 0x01,
		0x5c, 0x23, 0x01
	};
	static const u8 csb8[8 * 3] = {
		0x5c, 0x23, 0x01,
		0x29, 0x01, 0x01,
		0x7c, 0x08, 0x01,
		0x79, 0x06, 0x01,
		0x29, 0x01, 0x81,
		0x5c, 0x23, 0x01,
		0x5c, 0x23, 0x01,
		0x5c, 0x23, 0x01
	};
L
Linus Torvalds 已提交
5043

T
Thomas Winischhofer 已提交
5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071
	regb = 0;  /* ! */

	index = regb * 3;
	v1 = cs90[index]; v2 = cs90[index + 1]; v3 = cs90[index + 2];
	if(ivideo->haveXGIROM) {
		v1 = ivideo->bios_abase[0x90 + index];
		v2 = ivideo->bios_abase[0x90 + index + 1];
		v3 = ivideo->bios_abase[0x90 + index + 2];
	}
	outSISIDXREG(SISSR, 0x28, v1);
	outSISIDXREG(SISSR, 0x29, v2);
	outSISIDXREG(SISSR, 0x2a, v3);
	sisfb_post_xgi_delay(ivideo, 0x43);
	sisfb_post_xgi_delay(ivideo, 0x43);
	sisfb_post_xgi_delay(ivideo, 0x43);
	index = regb * 3;
	v1 = csb8[index]; v2 = csb8[index + 1]; v3 = csb8[index + 2];
	if(ivideo->haveXGIROM) {
		v1 = ivideo->bios_abase[0xb8 + index];
		v2 = ivideo->bios_abase[0xb8 + index + 1];
		v3 = ivideo->bios_abase[0xb8 + index + 2];
	}
	outSISIDXREG(SISSR, 0x2e, v1);
	outSISIDXREG(SISSR, 0x2f, v2);
	outSISIDXREG(SISSR, 0x30, v3);
	sisfb_post_xgi_delay(ivideo, 0x43);
	sisfb_post_xgi_delay(ivideo, 0x43);
	sisfb_post_xgi_delay(ivideo, 0x43);
L
Linus Torvalds 已提交
5072 5073
}

T
Thomas Winischhofer 已提交
5074 5075
static int __devinit
sisfb_post_xgi(struct pci_dev *pdev)
L
Linus Torvalds 已提交
5076 5077
{
	struct sis_video_info *ivideo = pci_get_drvdata(pdev);
T
Thomas Winischhofer 已提交
5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138
	unsigned char *bios = ivideo->bios_abase;
	struct pci_dev *mypdev = NULL;
	const u8 *ptr, *ptr2;
	u8 v1, v2, v3, v4, v5, reg, ramtype;
	u32 rega, regb, regd;
	int i, j, k, index;
	static const u8 cs78[3] = { 0xf6, 0x0d, 0x00 };
	static const u8 cs76[2] = { 0xa3, 0xfb };
	static const u8 cs7b[3] = { 0xc0, 0x11, 0x00 };
	static const u8 cs158[8] = {
		0x88, 0xaa, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00
	};
	static const u8 cs160[8] = {
		0x44, 0x77, 0x77, 0x00, 0x00, 0x00, 0x00, 0x00
	};
	static const u8 cs168[8] = {
		0x48, 0x78, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00
	};
	static const u8 cs128[3 * 8] = {
		0x90, 0x28, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x77, 0x44, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x77, 0x44, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00
	};
	static const u8 cs148[2 * 8] = {
		0x55, 0x55, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	};
	static const u8 cs31a[8 * 4] = {
		0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
		0xaa, 0xaa, 0xaa, 0xaa, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	};
	static const u8 cs33a[8 * 4] = {
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	};
	static const u8 cs45a[8 * 2] = {
		0x00, 0x00, 0xa0, 0x00, 0xa0, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	};
	static const u8 cs170[7 * 8] = {
		0x54, 0x32, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x54, 0x43, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x0a, 0x05, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x44, 0x34, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x10, 0x0a, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x11, 0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00
	};
	static const u8 cs1a8[3 * 8] = {
		0xf0, 0xf0, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x05, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	};
	static const u8 cs100[2 * 8] = {
		0xc4, 0x04, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00,
		0xc4, 0x04, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00
	};
L
Linus Torvalds 已提交
5139

T
Thomas Winischhofer 已提交
5140 5141 5142
	/* VGA enable */
	reg = inSISREG(SISVGAENABLE) | 0x01;
	outSISREG(SISVGAENABLE, reg);
L
Linus Torvalds 已提交
5143

T
Thomas Winischhofer 已提交
5144 5145 5146
	/* Misc */
	reg = inSISREG(SISMISCR) | 0x01;
	outSISREG(SISMISCW, reg);
L
Linus Torvalds 已提交
5147

T
Thomas Winischhofer 已提交
5148 5149 5150 5151 5152
	/* Unlock SR */
	outSISIDXREG(SISSR, 0x05, 0x86);
	inSISIDXREG(SISSR, 0x05, reg);
	if(reg != 0xa1)
		return 0;
L
Linus Torvalds 已提交
5153

T
Thomas Winischhofer 已提交
5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164
	/* Clear some regs */
	for(i = 0; i < 0x22; i++) {
		if(0x06 + i == 0x20) continue;
		outSISIDXREG(SISSR, 0x06 + i, 0x00);
	}
	for(i = 0; i < 0x0b; i++) {
		outSISIDXREG(SISSR, 0x31 + i, 0x00);
	}
	for(i = 0; i < 0x10; i++) {
		outSISIDXREG(SISCR, 0x30 + i, 0x00);
	}
L
Linus Torvalds 已提交
5165

T
Thomas Winischhofer 已提交
5166 5167 5168 5169 5170 5171 5172
	ptr = cs78;
	if(ivideo->haveXGIROM) {
		ptr = (const u8 *)&bios[0x78];
	}
	for(i = 0; i < 3; i++) {
		outSISIDXREG(SISSR, 0x23 + i, ptr[i]);
	}
L
Linus Torvalds 已提交
5173

T
Thomas Winischhofer 已提交
5174 5175 5176 5177 5178 5179 5180
	ptr = cs76;
	if(ivideo->haveXGIROM) {
		ptr = (const u8 *)&bios[0x76];
	}
	for(i = 0; i < 2; i++) {
		outSISIDXREG(SISSR, 0x21 + i, ptr[i]);
	}
L
Linus Torvalds 已提交
5181

T
Thomas Winischhofer 已提交
5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192
	v1 = 0x18; v2 = 0x00;
	if(ivideo->haveXGIROM) {
		v1 = bios[0x74];
		v2 = bios[0x75];
	}
	outSISIDXREG(SISSR, 0x07, v1);
	outSISIDXREG(SISSR, 0x11, 0x0f);
	outSISIDXREG(SISSR, 0x1f, v2);
	/* PCI linear mode, RelIO enabled, A0000 decoding disabled */
	outSISIDXREG(SISSR, 0x20, 0x80 | 0x20 | 0x04);
	outSISIDXREG(SISSR, 0x27, 0x74);
L
Linus Torvalds 已提交
5193

T
Thomas Winischhofer 已提交
5194 5195 5196 5197 5198 5199 5200
	ptr = cs7b;
	if(ivideo->haveXGIROM) {
		ptr = (const u8 *)&bios[0x7b];
	}
	for(i = 0; i < 3; i++) {
		outSISIDXREG(SISSR, 0x31 + i, ptr[i]);
	}
L
Linus Torvalds 已提交
5201

T
Thomas Winischhofer 已提交
5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215
	if(ivideo->chip == XGI_40) {
		if(ivideo->revision_id == 2) {
			setSISIDXREG(SISSR, 0x3b, 0x3f, 0xc0);
		}
		outSISIDXREG(SISCR, 0x7d, 0xfe);
		outSISIDXREG(SISCR, 0x7e, 0x0f);
	}
	if(ivideo->revision_id == 0) {	/* 40 *and* 20? */
		andSISIDXREG(SISCR, 0x58, 0xd7);
		inSISIDXREG(SISCR, 0xcb, reg);
		if(reg & 0x20) {
			setSISIDXREG(SISCR, 0x58, 0xd7, (reg & 0x10) ? 0x08 : 0x20); /* =0x28 Z7 ? */
		}
	}
L
Linus Torvalds 已提交
5216

T
Thomas Winischhofer 已提交
5217 5218
	reg = (ivideo->chip == XGI_40) ? 0x20 : 0x00;
	setSISIDXREG(SISCR, 0x38, 0x1f, reg);
L
Linus Torvalds 已提交
5219

T
Thomas Winischhofer 已提交
5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254
	if(ivideo->chip == XGI_20) {
		outSISIDXREG(SISSR, 0x36, 0x70);
	} else {
		outSISIDXREG(SISVID, 0x00, 0x86);
		outSISIDXREG(SISVID, 0x32, 0x00);
		outSISIDXREG(SISVID, 0x30, 0x00);
		outSISIDXREG(SISVID, 0x32, 0x01);
		outSISIDXREG(SISVID, 0x30, 0x00);
		andSISIDXREG(SISVID, 0x2f, 0xdf);
		andSISIDXREG(SISCAP, 0x00, 0x3f);

		outSISIDXREG(SISPART1, 0x2f, 0x01);
		outSISIDXREG(SISPART1, 0x00, 0x00);
		outSISIDXREG(SISPART1, 0x02, bios[0x7e]);
		outSISIDXREG(SISPART1, 0x2e, 0x08);
		andSISIDXREG(SISPART1, 0x35, 0x7f);
		andSISIDXREG(SISPART1, 0x50, 0xfe);

		inSISIDXREG(SISPART4, 0x00, reg);
		if(reg == 1 || reg == 2) {
			outSISIDXREG(SISPART2, 0x00, 0x1c);
			outSISIDXREG(SISPART4, 0x0d, bios[0x7f]);
			outSISIDXREG(SISPART4, 0x0e, bios[0x80]);
			outSISIDXREG(SISPART4, 0x10, bios[0x81]);
			andSISIDXREG(SISPART4, 0x0f, 0x3f);

			inSISIDXREG(SISPART4, 0x01, reg);
			if((reg & 0xf0) >= 0xb0) {
				inSISIDXREG(SISPART4, 0x23, reg);
				if(reg & 0x20) reg |= 0x40;
				outSISIDXREG(SISPART4, 0x23, reg);
				reg = (reg & 0x20) ? 0x02 : 0x00;
				setSISIDXREG(SISPART1, 0x1e, 0xfd, reg);
			}
		}
L
Linus Torvalds 已提交
5255

T
Thomas Winischhofer 已提交
5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315
		v1 = bios[0x77];

		inSISIDXREG(SISSR, 0x3b, reg);
		if(reg & 0x02) {
			inSISIDXREG(SISSR, 0x3a, reg);
			v2 = (reg & 0x30) >> 3;
			if(!(v2 & 0x04)) v2 ^= 0x02;
			inSISIDXREG(SISSR, 0x39, reg);
			if(reg & 0x80) v2 |= 0x80;
			v2 |= 0x01;

			if((mypdev = SIS_PCI_GET_DEVICE(PCI_VENDOR_ID_SI, 0x0730, NULL))) {
				SIS_PCI_PUT_DEVICE(mypdev);
				if(((v2 & 0x06) == 2) || ((v2 & 0x06) == 4))
					v2 &= 0xf9;
				v2 |= 0x08;
				v1 &= 0xfe;
			} else {
				mypdev = SIS_PCI_GET_DEVICE(PCI_VENDOR_ID_SI, 0x0735, NULL);
				if(!mypdev)
					mypdev = SIS_PCI_GET_DEVICE(PCI_VENDOR_ID_SI, 0x0645, NULL);
				if(!mypdev)
					mypdev = SIS_PCI_GET_DEVICE(PCI_VENDOR_ID_SI, 0x0650, NULL);
				if(mypdev) {
					pci_read_config_dword(mypdev, 0x94, &regd);
					regd &= 0xfffffeff;
					pci_write_config_dword(mypdev, 0x94, regd);
					v1 &= 0xfe;
					SIS_PCI_PUT_DEVICE(mypdev);
				} else if(sisfb_find_host_bridge(ivideo, pdev, PCI_VENDOR_ID_SI)) {
					v1 &= 0xfe;
				} else if(sisfb_find_host_bridge(ivideo, pdev, 0x1106) ||
					  sisfb_find_host_bridge(ivideo, pdev, 0x1022) ||
					  sisfb_find_host_bridge(ivideo, pdev, 0x700e) ||
					  sisfb_find_host_bridge(ivideo, pdev, 0x10de)) {
					if((v2 & 0x06) == 4)
						v2 ^= 0x06;
					v2 |= 0x08;
				}
			}
			setSISIDXREG(SISCR, 0x5f, 0xf0, v2);
		}
		outSISIDXREG(SISSR, 0x22, v1);

		if(ivideo->revision_id == 2) {
			inSISIDXREG(SISSR, 0x3b, v1);
			inSISIDXREG(SISSR, 0x3a, v2);
			regd = bios[0x90 + 3] | (bios[0x90 + 4] << 8);
			if( (!(v1 & 0x02)) && (v2 & 0x30) && (regd < 0xcf) )
				setSISIDXREG(SISCR, 0x5f, 0xf1, 0x01);

			if((mypdev = SIS_PCI_GET_DEVICE(0x10de, 0x01e0, NULL))) {
				/* TODO: set CR5f &0xf1 | 0x01 for version 6570
				 * of nforce 2 ROM
				 */
				if(0)
					setSISIDXREG(SISCR, 0x5f, 0xf1, 0x01);
				SIS_PCI_PUT_DEVICE(mypdev);
			}
		}
L
Linus Torvalds 已提交
5316

T
Thomas Winischhofer 已提交
5317 5318 5319 5320 5321 5322
		v1 = 0x30;
		inSISIDXREG(SISSR, 0x3b, reg);
		inSISIDXREG(SISCR, 0x5f, v2);
		if((!(reg & 0x02)) && (v2 & 0x0e))
			v1 |= 0x08;
		outSISIDXREG(SISSR, 0x27, v1);
L
Linus Torvalds 已提交
5323

T
Thomas Winischhofer 已提交
5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353
		if(bios[0x64] & 0x01) {
			setSISIDXREG(SISCR, 0x5f, 0xf0, bios[0x64]);
		}

		v1 = bios[0x4f7];
		pci_read_config_dword(pdev, 0x50, &regd);
		regd = (regd >> 20) & 0x0f;
		if(regd == 1) {
			v1 &= 0xfc;
			orSISIDXREG(SISCR, 0x5f, 0x08);
		}
		outSISIDXREG(SISCR, 0x48, v1);

		setSISIDXREG(SISCR, 0x47, 0x04, bios[0x4f6] & 0xfb);
		setSISIDXREG(SISCR, 0x49, 0xf0, bios[0x4f8] & 0x0f);
		setSISIDXREG(SISCR, 0x4a, 0x60, bios[0x4f9] & 0x9f);
		setSISIDXREG(SISCR, 0x4b, 0x08, bios[0x4fa] & 0xf7);
		setSISIDXREG(SISCR, 0x4c, 0x80, bios[0x4fb] & 0x7f);
		outSISIDXREG(SISCR, 0x70, bios[0x4fc]);
		setSISIDXREG(SISCR, 0x71, 0xf0, bios[0x4fd] & 0x0f);
		outSISIDXREG(SISCR, 0x74, 0xd0);
		setSISIDXREG(SISCR, 0x74, 0xcf, bios[0x4fe] & 0x30);
		setSISIDXREG(SISCR, 0x75, 0xe0, bios[0x4ff] & 0x1f);
		setSISIDXREG(SISCR, 0x76, 0xe0, bios[0x500] & 0x1f);
		v1 = bios[0x501];
		if((mypdev = SIS_PCI_GET_DEVICE(0x8086, 0x2530, NULL))) {
			v1 = 0xf0;
			SIS_PCI_PUT_DEVICE(mypdev);
		}
		outSISIDXREG(SISCR, 0x77, v1);
L
Linus Torvalds 已提交
5354 5355
	}

T
Thomas Winischhofer 已提交
5356
	/* RAM type */
L
Linus Torvalds 已提交
5357

T
Thomas Winischhofer 已提交
5358
	regb = 0;	/* ! */
L
Linus Torvalds 已提交
5359

T
Thomas Winischhofer 已提交
5360 5361 5362
	v1 = 0xff;
	if(ivideo->haveXGIROM) {
		v1 = bios[0x140 + regb];
L
Linus Torvalds 已提交
5363
	}
T
Thomas Winischhofer 已提交
5364
	outSISIDXREG(SISCR, 0x6d, v1);
L
Linus Torvalds 已提交
5365

T
Thomas Winischhofer 已提交
5366 5367 5368
	ptr = cs128;
	if(ivideo->haveXGIROM) {
		ptr = (const u8 *)&bios[0x128];
L
Linus Torvalds 已提交
5369
	}
T
Thomas Winischhofer 已提交
5370 5371
	for(i = 0, j = 0; i < 3; i++, j += 8) {
		outSISIDXREG(SISCR, 0x68 + i, ptr[j + regb]);
L
Linus Torvalds 已提交
5372 5373
	}

T
Thomas Winischhofer 已提交
5374 5375 5376 5377 5378 5379
	ptr  = cs31a;
	ptr2 = cs33a;
	if(ivideo->haveXGIROM) {
		index = (ivideo->chip == XGI_20) ? 0x31a : 0x3a6;
		ptr  = (const u8 *)&bios[index];
		ptr2 = (const u8 *)&bios[index + 0x20];
L
Linus Torvalds 已提交
5380
	}
T
Thomas Winischhofer 已提交
5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399
	for(i = 0; i < 2; i++) {
		if(i == 0) {
			regd = le32_to_cpu(((u32 *)ptr)[regb]);
			rega = 0x6b;
		} else {
			regd = le32_to_cpu(((u32 *)ptr2)[regb]);
			rega = 0x6e;
		}
		reg = 0x00;
		for(j = 0; j < 16; j++) {
			reg &= 0xf3;
			if(regd & 0x01) reg |= 0x04;
			if(regd & 0x02) reg |= 0x08;
			regd >>= 2;
			outSISIDXREG(SISCR, rega, reg);
			inSISIDXREG(SISCR, rega, reg);
			inSISIDXREG(SISCR, rega, reg);
			reg += 0x10;
		}
L
Linus Torvalds 已提交
5400
	}
T
Thomas Winischhofer 已提交
5401 5402 5403 5404 5405 5406 5407

	andSISIDXREG(SISCR, 0x6e, 0xfc);

	ptr  = NULL;
	if(ivideo->haveXGIROM) {
		index = (ivideo->chip == XGI_20) ? 0x35a : 0x3e6;
		ptr  = (const u8 *)&bios[index];
L
Linus Torvalds 已提交
5408
	}
T
Thomas Winischhofer 已提交
5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429
	for(i = 0; i < 4; i++) {
		setSISIDXREG(SISCR, 0x6e, 0xfc, i);
		reg = 0x00;
		for(j = 0; j < 2; j++) {
			regd = 0;
			if(ptr) {
				regd = le32_to_cpu(((u32 *)ptr)[regb * 8]);
				ptr += 4;
			}
			/* reg = 0x00; */
			for(k = 0; k < 16; k++) {
				reg &= 0xfc;
				if(regd & 0x01) reg |= 0x01;
				if(regd & 0x02) reg |= 0x02;
				regd >>= 2;
				outSISIDXREG(SISCR, 0x6f, reg);
				inSISIDXREG(SISCR, 0x6f, reg);
				inSISIDXREG(SISCR, 0x6f, reg);
				reg += 0x08;
			}
		}
L
Linus Torvalds 已提交
5430
	}
T
Thomas Winischhofer 已提交
5431 5432 5433 5434

	ptr  = cs148;
	if(ivideo->haveXGIROM) {
		ptr  = (const u8 *)&bios[0x148];
L
Linus Torvalds 已提交
5435
	}
T
Thomas Winischhofer 已提交
5436 5437
	for(i = 0, j = 0; i < 2; i++, j += 8) {
		outSISIDXREG(SISCR, 0x80 + i, ptr[j + regb]);
L
Linus Torvalds 已提交
5438 5439
	}

T
Thomas Winischhofer 已提交
5440
	andSISIDXREG(SISCR, 0x89, 0x8f);
L
Linus Torvalds 已提交
5441

T
Thomas Winischhofer 已提交
5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458
	ptr  = cs45a;
	if(ivideo->haveXGIROM) {
		index = (ivideo->chip == XGI_20) ? 0x45a : 0x4e6;
		ptr  = (const u8 *)&bios[index];
	}
	regd = le16_to_cpu(((const u16 *)ptr)[regb]);
	reg = 0x80;
	for(i = 0; i < 5; i++) {
		reg &= 0xfc;
		if(regd & 0x01) reg |= 0x01;
		if(regd & 0x02) reg |= 0x02;
		regd >>= 2;
		outSISIDXREG(SISCR, 0x89, reg);
		inSISIDXREG(SISCR, 0x89, reg);
		inSISIDXREG(SISCR, 0x89, reg);
		reg += 0x10;
	}
L
Linus Torvalds 已提交
5459

T
Thomas Winischhofer 已提交
5460 5461 5462 5463 5464 5465
	v1 = 0xb5; v2 = 0x20; v3 = 0xf0; v4 = 0x13;
	if(ivideo->haveXGIROM) {
		v1 = bios[0x118 + regb];
		v2 = bios[0xf8 + regb];
		v3 = bios[0x120 + regb];
		v4 = bios[0x1ca];
L
Linus Torvalds 已提交
5466
	}
T
Thomas Winischhofer 已提交
5467 5468 5469 5470 5471 5472 5473 5474
	outSISIDXREG(SISCR, 0x45, v1 & 0x0f);
	outSISIDXREG(SISCR, 0x99, (v1 >> 4) & 0x07);
	orSISIDXREG(SISCR, 0x40, v1 & 0x80);
	outSISIDXREG(SISCR, 0x41, v2);

	ptr  = cs170;
	if(ivideo->haveXGIROM) {
		ptr  = (const u8 *)&bios[0x170];
L
Linus Torvalds 已提交
5475
	}
T
Thomas Winischhofer 已提交
5476 5477
	for(i = 0, j = 0; i < 7; i++, j += 8) {
		outSISIDXREG(SISCR, 0x90 + i, ptr[j + regb]);
L
Linus Torvalds 已提交
5478 5479
	}

T
Thomas Winischhofer 已提交
5480 5481 5482 5483 5484 5485 5486 5487
	outSISIDXREG(SISCR, 0x59, v3);

	ptr  = cs1a8;
	if(ivideo->haveXGIROM) {
		ptr  = (const u8 *)&bios[0x1a8];
	}
	for(i = 0, j = 0; i < 3; i++, j += 8) {
		outSISIDXREG(SISCR, 0xc3 + i, ptr[j + regb]);
L
Linus Torvalds 已提交
5488 5489
	}

T
Thomas Winischhofer 已提交
5490 5491 5492 5493 5494 5495 5496
	ptr  = cs100;
	if(ivideo->haveXGIROM) {
		ptr  = (const u8 *)&bios[0x100];
	}
	for(i = 0, j = 0; i < 2; i++, j += 8) {
		outSISIDXREG(SISCR, 0x8a + i, ptr[j + regb]);
	}
L
Linus Torvalds 已提交
5497

T
Thomas Winischhofer 已提交
5498
	outSISIDXREG(SISCR, 0xcf, v4);
L
Linus Torvalds 已提交
5499

T
Thomas Winischhofer 已提交
5500 5501
	outSISIDXREG(SISCR, 0x83, 0x09);
	outSISIDXREG(SISCR, 0x87, 0x00);
L
Linus Torvalds 已提交
5502

T
Thomas Winischhofer 已提交
5503 5504 5505 5506 5507 5508
	if(ivideo->chip == XGI_40) {
		if( (ivideo->revision_id == 1) ||
		    (ivideo->revision_id == 2) ) {
			outSISIDXREG(SISCR, 0x8c, 0x87);
		}
	}
L
Linus Torvalds 已提交
5509

T
Thomas Winischhofer 已提交
5510 5511
	outSISIDXREG(SISSR, 0x17, 0x00);
	outSISIDXREG(SISSR, 0x1a, 0x87);
L
Linus Torvalds 已提交
5512

T
Thomas Winischhofer 已提交
5513 5514 5515
	if(ivideo->chip == XGI_20) {
		outSISIDXREG(SISSR, 0x15, 0x00);
		outSISIDXREG(SISSR, 0x1c, 0x00);
L
Linus Torvalds 已提交
5516 5517
	}

T
Thomas Winischhofer 已提交
5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537
	ramtype = 0x00; v1 = 0x10;
	if(ivideo->haveXGIROM) {
		ramtype = bios[0x62];
		v1 = bios[0x1d2];
	}
	if(!(ramtype & 0x80)) {
		if(ivideo->chip == XGI_20) {
			outSISIDXREG(SISCR, 0x97, v1);
			inSISIDXREG(SISCR, 0x97, reg);
			if(reg & 0x10) {
				ramtype = (reg & 0x01) << 1;
			}
		} else {
			inSISIDXREG(SISSR, 0x39, reg);
			ramtype = reg & 0x02;
			if(!(ramtype)) {
				inSISIDXREG(SISSR, 0x3a, reg);
				ramtype = (reg >> 1) & 0x01;
			}
		}
L
Linus Torvalds 已提交
5538
	}
T
Thomas Winischhofer 已提交
5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577
	ramtype &= 0x07;

	regb = 0;	/* ! */

	switch(ramtype) {
	case 0:
		sisfb_post_xgi_setclocks(ivideo, regb);
		if((ivideo->chip == XGI_20) ||
		   (ivideo->revision_id == 1)   ||
		   (ivideo->revision_id == 2)) {
			v1 = cs158[regb]; v2 = cs160[regb]; v3 = cs168[regb];
			if(ivideo->haveXGIROM) {
				v1 = bios[regb + 0x158];
				v2 = bios[regb + 0x160];
				v3 = bios[regb + 0x168];
			}
			outSISIDXREG(SISCR, 0x82, v1);
			outSISIDXREG(SISCR, 0x85, v2);
			outSISIDXREG(SISCR, 0x86, v3);
		} else {
			outSISIDXREG(SISCR, 0x82, 0x88);
			outSISIDXREG(SISCR, 0x86, 0x00);
			inSISIDXREG(SISCR, 0x86, reg);
			outSISIDXREG(SISCR, 0x86, 0x88);
			inSISIDXREG(SISCR, 0x86, reg);
			outSISIDXREG(SISCR, 0x86, bios[regb + 0x168]);
			outSISIDXREG(SISCR, 0x82, 0x77);
			outSISIDXREG(SISCR, 0x85, 0x00);
			inSISIDXREG(SISCR, 0x85, reg);
			outSISIDXREG(SISCR, 0x85, 0x88);
			inSISIDXREG(SISCR, 0x85, reg);
			outSISIDXREG(SISCR, 0x85, bios[regb + 0x160]);
			outSISIDXREG(SISCR, 0x82, bios[regb + 0x158]);
		}
		if(ivideo->chip == XGI_40) {
			outSISIDXREG(SISCR, 0x97, 0x00);
		}
		outSISIDXREG(SISCR, 0x98, 0x01);
		outSISIDXREG(SISCR, 0x9a, 0x02);
L
Linus Torvalds 已提交
5578

T
Thomas Winischhofer 已提交
5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718
		outSISIDXREG(SISSR, 0x18, 0x01);
		if((ivideo->chip == XGI_20) ||
		   (ivideo->revision_id == 2)) {
			outSISIDXREG(SISSR, 0x19, 0x40);
		} else {
			outSISIDXREG(SISSR, 0x19, 0x20);
		}
		outSISIDXREG(SISSR, 0x16, 0x00);
		outSISIDXREG(SISSR, 0x16, 0x80);
		if((ivideo->chip == XGI_20) || (bios[0x1cb] != 0x0c)) {
			sisfb_post_xgi_delay(ivideo, 0x43);
			sisfb_post_xgi_delay(ivideo, 0x43);
			sisfb_post_xgi_delay(ivideo, 0x43);
			outSISIDXREG(SISSR, 0x18, 0x00);
			if((ivideo->chip == XGI_20) ||
			   (ivideo->revision_id == 2)) {
				outSISIDXREG(SISSR, 0x19, 0x40);
			} else {
				outSISIDXREG(SISSR, 0x19, 0x20);
			}
		} else if((ivideo->chip == XGI_40) && (bios[0x1cb] == 0x0c)) {
			/* outSISIDXREG(SISSR, 0x16, 0x0c); */ /* ? */
		}
		outSISIDXREG(SISSR, 0x16, 0x00);
		outSISIDXREG(SISSR, 0x16, 0x80);
		sisfb_post_xgi_delay(ivideo, 4);
		v1 = 0x31; v2 = 0x03; v3 = 0x83; v4 = 0x03; v5 = 0x83;
		if(ivideo->haveXGIROM) {
			v1 = bios[0xf0];
			index = (ivideo->chip == XGI_20) ? 0x4b2 : 0x53e;
			v2 = bios[index];
			v3 = bios[index + 1];
			v4 = bios[index + 2];
			v5 = bios[index + 3];
		}
		outSISIDXREG(SISSR, 0x18, v1);
		outSISIDXREG(SISSR, 0x19, ((ivideo->chip == XGI_20) ? 0x02 : 0x01));
		outSISIDXREG(SISSR, 0x16, v2);
		outSISIDXREG(SISSR, 0x16, v3);
		sisfb_post_xgi_delay(ivideo, 0x43);
		outSISIDXREG(SISSR, 0x1b, 0x03);
		sisfb_post_xgi_delay(ivideo, 0x22);
		outSISIDXREG(SISSR, 0x18, v1);
		outSISIDXREG(SISSR, 0x19, 0x00);
		outSISIDXREG(SISSR, 0x16, v4);
		outSISIDXREG(SISSR, 0x16, v5);
		outSISIDXREG(SISSR, 0x1b, 0x00);
		break;
	case 1:
		outSISIDXREG(SISCR, 0x82, 0x77);
		outSISIDXREG(SISCR, 0x86, 0x00);
		inSISIDXREG(SISCR, 0x86, reg);
		outSISIDXREG(SISCR, 0x86, 0x88);
		inSISIDXREG(SISCR, 0x86, reg);
		v1 = cs168[regb]; v2 = cs160[regb]; v3 = cs158[regb];
		if(ivideo->haveXGIROM) {
			v1 = bios[regb + 0x168];
			v2 = bios[regb + 0x160];
			v3 = bios[regb + 0x158];
		}
		outSISIDXREG(SISCR, 0x86, v1);
		outSISIDXREG(SISCR, 0x82, 0x77);
		outSISIDXREG(SISCR, 0x85, 0x00);
		inSISIDXREG(SISCR, 0x85, reg);
		outSISIDXREG(SISCR, 0x85, 0x88);
		inSISIDXREG(SISCR, 0x85, reg);
		outSISIDXREG(SISCR, 0x85, v2);
		outSISIDXREG(SISCR, 0x82, v3);
		outSISIDXREG(SISCR, 0x98, 0x01);
		outSISIDXREG(SISCR, 0x9a, 0x02);

		outSISIDXREG(SISSR, 0x28, 0x64);
		outSISIDXREG(SISSR, 0x29, 0x63);
		sisfb_post_xgi_delay(ivideo, 15);
		outSISIDXREG(SISSR, 0x18, 0x00);
		outSISIDXREG(SISSR, 0x19, 0x20);
		outSISIDXREG(SISSR, 0x16, 0x00);
		outSISIDXREG(SISSR, 0x16, 0x80);
		outSISIDXREG(SISSR, 0x18, 0xc5);
		outSISIDXREG(SISSR, 0x19, 0x23);
		outSISIDXREG(SISSR, 0x16, 0x00);
		outSISIDXREG(SISSR, 0x16, 0x80);
		sisfb_post_xgi_delay(ivideo, 1);
		outSISIDXREG(SISCR, 0x97,0x11);
		sisfb_post_xgi_setclocks(ivideo, regb);
		sisfb_post_xgi_delay(ivideo, 0x46);
		outSISIDXREG(SISSR, 0x18, 0xc5);
		outSISIDXREG(SISSR, 0x19, 0x23);
		outSISIDXREG(SISSR, 0x16, 0x00);
		outSISIDXREG(SISSR, 0x16, 0x80);
		sisfb_post_xgi_delay(ivideo, 1);
		outSISIDXREG(SISSR, 0x1b, 0x04);
		sisfb_post_xgi_delay(ivideo, 1);
		outSISIDXREG(SISSR, 0x1b, 0x00);
		sisfb_post_xgi_delay(ivideo, 1);
		v1 = 0x31;
		if(ivideo->haveXGIROM) {
			v1 = bios[0xf0];
		}
		outSISIDXREG(SISSR, 0x18, v1);
		outSISIDXREG(SISSR, 0x19, 0x06);
		outSISIDXREG(SISSR, 0x16, 0x04);
		outSISIDXREG(SISSR, 0x16, 0x84);
		sisfb_post_xgi_delay(ivideo, 1);
		break;
	default:
		sisfb_post_xgi_setclocks(ivideo, regb);
		if((ivideo->chip == XGI_40) &&
		   ((ivideo->revision_id == 1) ||
		    (ivideo->revision_id == 2))) {
			outSISIDXREG(SISCR, 0x82, bios[regb + 0x158]);
			outSISIDXREG(SISCR, 0x85, bios[regb + 0x160]);
			outSISIDXREG(SISCR, 0x86, bios[regb + 0x168]);
		} else {
			outSISIDXREG(SISCR, 0x82, 0x88);
			outSISIDXREG(SISCR, 0x86, 0x00);
			inSISIDXREG(SISCR, 0x86, reg);
			outSISIDXREG(SISCR, 0x86, 0x88);
			outSISIDXREG(SISCR, 0x82, 0x77);
			outSISIDXREG(SISCR, 0x85, 0x00);
			inSISIDXREG(SISCR, 0x85, reg);
			outSISIDXREG(SISCR, 0x85, 0x88);
			inSISIDXREG(SISCR, 0x85, reg);
			v1 = cs160[regb]; v2 = cs158[regb];
			if(ivideo->haveXGIROM) {
				v1 = bios[regb + 0x160];
				v2 = bios[regb + 0x158];
			}
			outSISIDXREG(SISCR, 0x85, v1);
			outSISIDXREG(SISCR, 0x82, v2);
		}
		if(ivideo->chip == XGI_40) {
			outSISIDXREG(SISCR, 0x97, 0x11);
		}
		if((ivideo->chip == XGI_40) && (ivideo->revision_id == 2)) {
			outSISIDXREG(SISCR, 0x98, 0x01);
		} else {
			outSISIDXREG(SISCR, 0x98, 0x03);
		}
		outSISIDXREG(SISCR, 0x9a, 0x02);
L
Linus Torvalds 已提交
5719

T
Thomas Winischhofer 已提交
5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767
		if(ivideo->chip == XGI_40) {
			outSISIDXREG(SISSR, 0x18, 0x01);
		} else {
			outSISIDXREG(SISSR, 0x18, 0x00);
		}
		outSISIDXREG(SISSR, 0x19, 0x40);
		outSISIDXREG(SISSR, 0x16, 0x00);
		outSISIDXREG(SISSR, 0x16, 0x80);
		if((ivideo->chip == XGI_40) && (bios[0x1cb] != 0x0c)) {
			sisfb_post_xgi_delay(ivideo, 0x43);
			sisfb_post_xgi_delay(ivideo, 0x43);
			sisfb_post_xgi_delay(ivideo, 0x43);
			outSISIDXREG(SISSR, 0x18, 0x00);
			outSISIDXREG(SISSR, 0x19, 0x40);
			outSISIDXREG(SISSR, 0x16, 0x00);
			outSISIDXREG(SISSR, 0x16, 0x80);
		}
		sisfb_post_xgi_delay(ivideo, 4);
		v1 = 0x31;
		if(ivideo->haveXGIROM) {
			v1 = bios[0xf0];
		}
		outSISIDXREG(SISSR, 0x18, v1);
		outSISIDXREG(SISSR, 0x19, 0x01);
		if(ivideo->chip == XGI_40) {
			outSISIDXREG(SISSR, 0x16, bios[0x53e]);
			outSISIDXREG(SISSR, 0x16, bios[0x53f]);
		} else {
			outSISIDXREG(SISSR, 0x16, 0x05);
			outSISIDXREG(SISSR, 0x16, 0x85);
		}
		sisfb_post_xgi_delay(ivideo, 0x43);
		if(ivideo->chip == XGI_40) {
			outSISIDXREG(SISSR, 0x1b, 0x01);
		} else {
			outSISIDXREG(SISSR, 0x1b, 0x03);
		}
		sisfb_post_xgi_delay(ivideo, 0x22);
		outSISIDXREG(SISSR, 0x18, v1);
		outSISIDXREG(SISSR, 0x19, 0x00);
		if(ivideo->chip == XGI_40) {
			outSISIDXREG(SISSR, 0x16, bios[0x540]);
			outSISIDXREG(SISSR, 0x16, bios[0x541]);
		} else {
			outSISIDXREG(SISSR, 0x16, 0x05);
			outSISIDXREG(SISSR, 0x16, 0x85);
		}
		outSISIDXREG(SISSR, 0x1b, 0x00);
L
Linus Torvalds 已提交
5768 5769
	}

T
Thomas Winischhofer 已提交
5770 5771 5772 5773
	regb = 0;	/* ! */
	v1 = 0x03;
	if(ivideo->haveXGIROM) {
		v1 = bios[0x110 + regb];
L
Linus Torvalds 已提交
5774
	}
T
Thomas Winischhofer 已提交
5775
	outSISIDXREG(SISSR, 0x1b, v1);
L
Linus Torvalds 已提交
5776

T
Thomas Winischhofer 已提交
5777 5778 5779 5780 5781
	/* RAM size */
	v1 = 0x00; v2 = 0x00;
	if(ivideo->haveXGIROM) {
		v1 = bios[0x62];
		v2 = bios[0x63];
L
Linus Torvalds 已提交
5782
	}
T
Thomas Winischhofer 已提交
5783 5784 5785
	regb = 0;	/* ! */
	regd = 1 << regb;
	if((v1 & 0x40) && (v2 & regd) && ivideo->haveXGIROM) {
L
Linus Torvalds 已提交
5786

T
Thomas Winischhofer 已提交
5787 5788
		outSISIDXREG(SISSR, 0x13, bios[regb + 0xe0]);
		outSISIDXREG(SISSR, 0x14, bios[regb + 0xe0 + 8]);
L
Linus Torvalds 已提交
5789

T
Thomas Winischhofer 已提交
5790
	} else {
L
Linus Torvalds 已提交
5791

T
Thomas Winischhofer 已提交
5792 5793 5794 5795 5796 5797 5798
		/* Set default mode, don't clear screen */
		ivideo->SiS_Pr.SiS_UseOEM = FALSE;
		SiS_SetEnableDstn(&ivideo->SiS_Pr, FALSE);
		SiS_SetEnableFstn(&ivideo->SiS_Pr, FALSE);
		ivideo->curFSTN = ivideo->curDSTN = 0;
		ivideo->SiS_Pr.VideoMemorySize = 8 << 20;
		SiSSetMode(&ivideo->SiS_Pr, 0x2e | 0x80);
L
Linus Torvalds 已提交
5799

T
Thomas Winischhofer 已提交
5800
		outSISIDXREG(SISSR, 0x05, 0x86);
L
Linus Torvalds 已提交
5801

T
Thomas Winischhofer 已提交
5802 5803 5804 5805 5806
		/* Disable read-cache */
		andSISIDXREG(SISSR, 0x21, 0xdf);
		sisfb_post_xgi_ramsize(ivideo);
		/* Enable read-cache */
		orSISIDXREG(SISSR, 0x21, 0x20);
L
Linus Torvalds 已提交
5807

T
Thomas Winischhofer 已提交
5808
	}
L
Linus Torvalds 已提交
5809

T
Thomas Winischhofer 已提交
5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821
#if 0
	printk(KERN_DEBUG "-----------------\n");
	for(i = 0; i < 0xff; i++) {
		inSISIDXREG(SISCR, i, reg);
		printk(KERN_DEBUG "CR%02x(%x) = 0x%02x\n", i, SISCR, reg);
	}
	for(i = 0; i < 0x40; i++) {
		inSISIDXREG(SISSR, i, reg);
		printk(KERN_DEBUG "SR%02x(%x) = 0x%02x\n", i, SISSR, reg);
	}
	printk(KERN_DEBUG "-----------------\n");
#endif
L
Linus Torvalds 已提交
5822

T
Thomas Winischhofer 已提交
5823 5824 5825
	/* Sense CRT1 */
	if(ivideo->chip == XGI_20) {
		orSISIDXREG(SISCR, 0x32, 0x20);
L
Linus Torvalds 已提交
5826
	} else {
T
Thomas Winischhofer 已提交
5827 5828 5829 5830 5831 5832
		inSISIDXREG(SISPART4, 0x00, reg);
		if((reg == 1) || (reg == 2)) {
			sisfb_sense_crt1(ivideo);
		} else {
			orSISIDXREG(SISCR, 0x32, 0x20);
		}
L
Linus Torvalds 已提交
5833 5834
	}

T
Thomas Winischhofer 已提交
5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860
	/* Set default mode, don't clear screen */
	ivideo->SiS_Pr.SiS_UseOEM = FALSE;
	SiS_SetEnableDstn(&ivideo->SiS_Pr, FALSE);
	SiS_SetEnableFstn(&ivideo->SiS_Pr, FALSE);
	ivideo->curFSTN = ivideo->curDSTN = 0;
	SiSSetMode(&ivideo->SiS_Pr, 0x2e | 0x80);

	outSISIDXREG(SISSR, 0x05, 0x86);

	/* Display off */
	orSISIDXREG(SISSR, 0x01, 0x20);

	/* Save mode number in CR34 */
	outSISIDXREG(SISCR, 0x34, 0x2e);

	/* Let everyone know what the current mode is */
	ivideo->modeprechange = 0x2e;

	if(ivideo->chip == XGI_40) {
		inSISIDXREG(SISCR, 0xca, reg);
		inSISIDXREG(SISCR, 0xcc, v1);
		if((reg & 0x10) && (!(v1 & 0x04))) {
			printk(KERN_ERR
				"sisfb: Please connect power to the card.\n");
			return 0;
		}
L
Linus Torvalds 已提交
5861 5862
	}

T
Thomas Winischhofer 已提交
5863
	return 1;
L
Linus Torvalds 已提交
5864 5865 5866
}
#endif

T
Thomas Winischhofer 已提交
5867 5868
static int __devinit
sisfb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
L
Linus Torvalds 已提交
5869
{
T
Thomas Winischhofer 已提交
5870 5871 5872
	struct sisfb_chip_info	*chipinfo = &sisfb_chip_info[ent->driver_data];
	struct sis_video_info	*ivideo = NULL;
	struct fb_info		*sis_fb_info = NULL;
L
Linus Torvalds 已提交
5873 5874
	u16 reg16;
	u8  reg;
T
Thomas Winischhofer 已提交
5875
	int i, ret;
L
Linus Torvalds 已提交
5876

T
Thomas Winischhofer 已提交
5877 5878
	if(sisfb_off)
		return -ENXIO;
L
Linus Torvalds 已提交
5879 5880 5881

#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,3))
	sis_fb_info = framebuffer_alloc(sizeof(*ivideo), &pdev->dev);
T
Thomas Winischhofer 已提交
5882 5883
	if(!sis_fb_info)
		return -ENOMEM;
L
Linus Torvalds 已提交
5884 5885
#else
	sis_fb_info = kmalloc(sizeof(*sis_fb_info) + sizeof(*ivideo), GFP_KERNEL);
T
Thomas Winischhofer 已提交
5886 5887
	if(!sis_fb_info)
		return -ENOMEM;
L
Linus Torvalds 已提交
5888 5889 5890 5891 5892 5893 5894
	memset(sis_fb_info, 0, sizeof(*sis_fb_info) + sizeof(*ivideo));
	sis_fb_info->par = ((char *)sis_fb_info + sizeof(*sis_fb_info));
#endif

	ivideo = (struct sis_video_info *)sis_fb_info->par;
	ivideo->memyselfandi = sis_fb_info;

T
Thomas Winischhofer 已提交
5895 5896
	ivideo->sisfb_id = SISFB_ID;

L
Linus Torvalds 已提交
5897
	if(card_list == NULL) {
T
Thomas Winischhofer 已提交
5898
		ivideo->cardnumber = 0;
L
Linus Torvalds 已提交
5899
	} else {
T
Thomas Winischhofer 已提交
5900 5901 5902 5903
		struct sis_video_info *countvideo = card_list;
		ivideo->cardnumber = 1;
		while((countvideo = countvideo->next) != 0)
			ivideo->cardnumber++;
L
Linus Torvalds 已提交
5904 5905 5906 5907 5908 5909
	}

	strncpy(ivideo->myid, chipinfo->chip_name, 30);

	ivideo->warncount = 0;
	ivideo->chip_id = pdev->device;
T
Thomas Winischhofer 已提交
5910
	ivideo->chip_vendor = pdev->vendor;
L
Linus Torvalds 已提交
5911
	pci_read_config_byte(pdev, PCI_REVISION_ID, &ivideo->revision_id);
T
Thomas Winischhofer 已提交
5912
	ivideo->SiS_Pr.ChipRevision = ivideo->revision_id;
L
Linus Torvalds 已提交
5913
	pci_read_config_word(pdev, PCI_COMMAND, &reg16);
T
Thomas Winischhofer 已提交
5914
	ivideo->sisvga_enabled = reg16 & 0x01;
L
Linus Torvalds 已提交
5915 5916 5917 5918 5919
	ivideo->pcibus = pdev->bus->number;
	ivideo->pcislot = PCI_SLOT(pdev->devfn);
	ivideo->pcifunc = PCI_FUNC(pdev->devfn);
	ivideo->subsysvendor = pdev->subsystem_vendor;
	ivideo->subsysdevice = pdev->subsystem_device;
T
Thomas Winischhofer 已提交
5920 5921 5922
#ifdef SIS_OLD_CONFIG_COMPAT
	ivideo->ioctl32registered = 0;
#endif
L
Linus Torvalds 已提交
5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941

#ifndef MODULE
	if(sisfb_mode_idx == -1) {
		sisfb_get_vga_mode_from_kernel();
	}
#endif

	ivideo->chip = chipinfo->chip;
	ivideo->sisvga_engine = chipinfo->vgaengine;
	ivideo->hwcursor_size = chipinfo->hwcursor_size;
	ivideo->CRT2_write_enable = chipinfo->CRT2_write_enable;
	ivideo->mni = chipinfo->mni;

	ivideo->detectedpdc  = 0xff;
	ivideo->detectedpdca = 0xff;
	ivideo->detectedlcda = 0xff;

	ivideo->sisfb_thismonitor.datavalid = FALSE;

T
Thomas Winischhofer 已提交
5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959
	ivideo->current_base = 0;

	ivideo->engineok = 0;

	ivideo->sisfb_was_boot_device = 0;
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12))
	if(pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW) {
		if(ivideo->sisvga_enabled)
			ivideo->sisfb_was_boot_device = 1;
		else {
			printk(KERN_DEBUG "sisfb: PCI device is disabled, "
				"but marked as boot video device ???\n");
			printk(KERN_DEBUG "sisfb: I will not accept this "
				"as the primary VGA device\n");
		}
	}
#endif

L
Linus Torvalds 已提交
5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985
	ivideo->sisfb_parm_mem = sisfb_parm_mem;
	ivideo->sisfb_accel = sisfb_accel;
	ivideo->sisfb_ypan = sisfb_ypan;
	ivideo->sisfb_max = sisfb_max;
	ivideo->sisfb_userom = sisfb_userom;
	ivideo->sisfb_useoem = sisfb_useoem;
	ivideo->sisfb_mode_idx = sisfb_mode_idx;
	ivideo->sisfb_parm_rate = sisfb_parm_rate;
	ivideo->sisfb_crt1off = sisfb_crt1off;
	ivideo->sisfb_forcecrt1 = sisfb_forcecrt1;
	ivideo->sisfb_crt2type = sisfb_crt2type;
	ivideo->sisfb_crt2flags = sisfb_crt2flags;
	/* pdc(a), scalelcd, special timing, lvdshl handled below */
	ivideo->sisfb_dstn = sisfb_dstn;
	ivideo->sisfb_fstn = sisfb_fstn;
	ivideo->sisfb_tvplug = sisfb_tvplug;
	ivideo->sisfb_tvstd = sisfb_tvstd;
	ivideo->tvxpos = sisfb_tvxposoffset;
	ivideo->tvypos = sisfb_tvyposoffset;
	ivideo->sisfb_nocrt2rate = sisfb_nocrt2rate;
#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0)
	ivideo->sisfb_inverse = sisfb_inverse;
#endif

	ivideo->refresh_rate = 0;
	if(ivideo->sisfb_parm_rate != -1) {
T
Thomas Winischhofer 已提交
5986
		ivideo->refresh_rate = ivideo->sisfb_parm_rate;
L
Linus Torvalds 已提交
5987 5988 5989 5990 5991 5992 5993 5994
	}

	ivideo->SiS_Pr.UsePanelScaler = sisfb_scalelcd;
	ivideo->SiS_Pr.CenterScreen = -1;
	ivideo->SiS_Pr.SiS_CustomT = sisfb_specialtiming;
	ivideo->SiS_Pr.LVDSHL = sisfb_lvdshl;

	ivideo->SiS_Pr.SiS_Backup70xx = 0xff;
T
Thomas Winischhofer 已提交
5995 5996
	ivideo->SiS_Pr.SiS_CHOverScan = -1;
	ivideo->SiS_Pr.SiS_ChSW = FALSE;
L
Linus Torvalds 已提交
5997 5998 5999 6000 6001 6002 6003 6004
	ivideo->SiS_Pr.SiS_UseLCDA = FALSE;
	ivideo->SiS_Pr.HaveEMI = FALSE;
	ivideo->SiS_Pr.HaveEMILCD = FALSE;
	ivideo->SiS_Pr.OverruleEMI = FALSE;
	ivideo->SiS_Pr.SiS_SensibleSR11 = FALSE;
	ivideo->SiS_Pr.SiS_MyCR63 = 0x63;
	ivideo->SiS_Pr.PDC  = -1;
	ivideo->SiS_Pr.PDCA = -1;
T
Thomas Winischhofer 已提交
6005
	ivideo->SiS_Pr.DDCPortMixup = FALSE;
L
Linus Torvalds 已提交
6006 6007
#ifdef CONFIG_FB_SIS_315
	if(ivideo->chip >= SIS_330) {
T
Thomas Winischhofer 已提交
6008 6009 6010 6011
		ivideo->SiS_Pr.SiS_MyCR63 = 0x53;
		if(ivideo->chip >= SIS_661) {
			ivideo->SiS_Pr.SiS_SensibleSR11 = TRUE;
		}
L
Linus Torvalds 已提交
6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023
	}
#endif

	memcpy(&ivideo->default_var, &my_default_var, sizeof(my_default_var));

	pci_set_drvdata(pdev, ivideo);

	/* Patch special cases */
	if((ivideo->nbridge = sisfb_get_northbridge(ivideo->chip))) {
		switch(ivideo->nbridge->device) {
#ifdef CONFIG_FB_SIS_300
		case PCI_DEVICE_ID_SI_730:
T
Thomas Winischhofer 已提交
6024
			ivideo->chip = SIS_730;
L
Linus Torvalds 已提交
6025
			strcpy(ivideo->myid, "SiS 730");
T
Thomas Winischhofer 已提交
6026
			break;
L
Linus Torvalds 已提交
6027 6028 6029 6030 6031 6032 6033
#endif
#ifdef CONFIG_FB_SIS_315
		case PCI_DEVICE_ID_SI_651:
			/* ivideo->chip is ok */
			strcpy(ivideo->myid, "SiS 651");
			break;
		case PCI_DEVICE_ID_SI_740:
T
Thomas Winischhofer 已提交
6034
			ivideo->chip = SIS_740;
L
Linus Torvalds 已提交
6035 6036 6037
			strcpy(ivideo->myid, "SiS 740");
			break;
		case PCI_DEVICE_ID_SI_661:
T
Thomas Winischhofer 已提交
6038
			ivideo->chip = SIS_661;
L
Linus Torvalds 已提交
6039 6040 6041
			strcpy(ivideo->myid, "SiS 661");
			break;
		case PCI_DEVICE_ID_SI_741:
T
Thomas Winischhofer 已提交
6042
			ivideo->chip = SIS_741;
L
Linus Torvalds 已提交
6043 6044 6045
			strcpy(ivideo->myid, "SiS 741");
			break;
		case PCI_DEVICE_ID_SI_760:
T
Thomas Winischhofer 已提交
6046
			ivideo->chip = SIS_760;
L
Linus Torvalds 已提交
6047 6048
			strcpy(ivideo->myid, "SiS 760");
			break;
T
Thomas Winischhofer 已提交
6049 6050 6051 6052
		case PCI_DEVICE_ID_SI_761:
			ivideo->chip = SIS_761;
			strcpy(ivideo->myid, "SiS 761");
			break;
L
Linus Torvalds 已提交
6053
#endif
T
Thomas Winischhofer 已提交
6054 6055
		default:
			break;
L
Linus Torvalds 已提交
6056 6057 6058 6059 6060 6061 6062
		}
	}

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
	strcpy(sis_fb_info->modename, ivideo->myid);
#endif

T
Thomas Winischhofer 已提交
6063 6064 6065
	ivideo->SiS_Pr.ChipType = ivideo->chip;

	ivideo->SiS_Pr.ivideo = (void *)ivideo;
L
Linus Torvalds 已提交
6066 6067

#ifdef CONFIG_FB_SIS_315
T
Thomas Winischhofer 已提交
6068 6069 6070
	if((ivideo->SiS_Pr.ChipType == SIS_315PRO) ||
	   (ivideo->SiS_Pr.ChipType == SIS_315)) {
		ivideo->SiS_Pr.ChipType = SIS_315H;
L
Linus Torvalds 已提交
6071 6072 6073
	}
#endif

T
Thomas Winischhofer 已提交
6074 6075 6076 6077 6078 6079 6080 6081 6082
	if(!ivideo->sisvga_enabled) {
		if(pci_enable_device(pdev)) {
			if(ivideo->nbridge) SIS_PCI_PUT_DEVICE(ivideo->nbridge);
			pci_set_drvdata(pdev, NULL);
			kfree(sis_fb_info);
			return -EIO;
		}
	}

L
Linus Torvalds 已提交
6083 6084 6085
	ivideo->video_base = pci_resource_start(pdev, 0);
	ivideo->mmio_base  = pci_resource_start(pdev, 1);
	ivideo->mmio_size  = pci_resource_len(pdev, 1);
T
Thomas Winischhofer 已提交
6086 6087
	ivideo->SiS_Pr.RelIO = pci_resource_start(pdev, 2) + 0x30;
	ivideo->SiS_Pr.IOAddress = ivideo->vga_base = ivideo->SiS_Pr.RelIO;
L
Linus Torvalds 已提交
6088

T
Thomas Winischhofer 已提交
6089
	SiSRegInit(&ivideo->SiS_Pr, ivideo->SiS_Pr.IOAddress);
L
Linus Torvalds 已提交
6090 6091 6092 6093

#ifdef CONFIG_FB_SIS_300
	/* Find PCI systems for Chrontel/GPIO communication setup */
	if(ivideo->chip == SIS_630) {
T
Thomas Winischhofer 已提交
6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113
		i = 0;
        	do {
			if(mychswtable[i].subsysVendor == ivideo->subsysvendor &&
			   mychswtable[i].subsysCard   == ivideo->subsysdevice) {
				ivideo->SiS_Pr.SiS_ChSW = TRUE;
				printk(KERN_DEBUG "sisfb: Identified [%s %s] "
					"requiring Chrontel/GPIO setup\n",
					mychswtable[i].vendorName,
					mychswtable[i].cardName);
				ivideo->lpcdev = SIS_PCI_GET_DEVICE(PCI_VENDOR_ID_SI, 0x0008, NULL);
				break;
			}
			i++;
		} while(mychswtable[i].subsysVendor != 0);
	}
#endif

#ifdef CONFIG_FB_SIS_315
	if((ivideo->chip == SIS_760) && (ivideo->nbridge)) {
		ivideo->lpcdev = SIS_PCI_GET_SLOT(ivideo->nbridge->bus, (2 << 3));
L
Linus Torvalds 已提交
6114 6115 6116
	}
#endif

T
Thomas Winischhofer 已提交
6117
	outSISIDXREG(SISSR, 0x05, 0x86);
L
Linus Torvalds 已提交
6118

T
Thomas Winischhofer 已提交
6119
	if( (!ivideo->sisvga_enabled)
L
Linus Torvalds 已提交
6120
#if !defined(__i386__) && !defined(__x86_64__)
T
Thomas Winischhofer 已提交
6121
			      || (sisfb_resetcard)
L
Linus Torvalds 已提交
6122
#endif
T
Thomas Winischhofer 已提交
6123 6124 6125 6126
						   ) {
		for(i = 0x30; i <= 0x3f; i++) {
			outSISIDXREG(SISCR, i, 0x00);
		}
L
Linus Torvalds 已提交
6127 6128 6129 6130
	}

	/* Find out about current video mode */
	ivideo->modeprechange = 0x03;
T
Thomas Winischhofer 已提交
6131
	inSISIDXREG(SISCR, 0x34, reg);
L
Linus Torvalds 已提交
6132 6133
	if(reg & 0x7f) {
		ivideo->modeprechange = reg & 0x7f;
T
Thomas Winischhofer 已提交
6134
	} else if(ivideo->sisvga_enabled) {
L
Linus Torvalds 已提交
6135
#if defined(__i386__) || defined(__x86_64__)
T
Thomas Winischhofer 已提交
6136
		unsigned char SIS_IOTYPE2 *tt = ioremap(0x400, 0x100);
L
Linus Torvalds 已提交
6137
		if(tt) {
T
Thomas Winischhofer 已提交
6138 6139
			ivideo->modeprechange = readb(tt + 0x49);
			iounmap(tt);
L
Linus Torvalds 已提交
6140 6141 6142 6143 6144 6145 6146
		}
#endif
	}

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
#ifdef MODULE
	if((reg & 0x80) && (reg != 0xff)) {
T
Thomas Winischhofer 已提交
6147 6148 6149 6150 6151 6152 6153
		if((sisbios_mode[ivideo->sisfb_mode_idx].mode_no[ivideo->mni])
									!= 0xFF) {
			printk(KERN_INFO "sisfb: Cannot initialize display mode, "
					 "X server is active\n");
			ret = -EBUSY;
			goto error_4;
		}
L
Linus Torvalds 已提交
6154 6155 6156 6157
	}
#endif
#endif

T
Thomas Winischhofer 已提交
6158
	/* Search and copy ROM image */
L
Linus Torvalds 已提交
6159
	ivideo->bios_abase = NULL;
T
Thomas Winischhofer 已提交
6160 6161 6162
	ivideo->SiS_Pr.VirtualRomBase = NULL;
	ivideo->SiS_Pr.UseROM = FALSE;
	ivideo->haveXGIROM = ivideo->SiS_Pr.SiS_XGIROM = FALSE;
L
Linus Torvalds 已提交
6163
	if(ivideo->sisfb_userom) {
T
Thomas Winischhofer 已提交
6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176
		ivideo->SiS_Pr.VirtualRomBase = sisfb_find_rom(pdev);
		ivideo->bios_abase = ivideo->SiS_Pr.VirtualRomBase;
		ivideo->SiS_Pr.UseROM = (ivideo->SiS_Pr.VirtualRomBase) ? TRUE : FALSE;
		printk(KERN_INFO "sisfb: Video ROM %sfound\n",
			ivideo->SiS_Pr.UseROM ? "" : "not ");
		if((ivideo->SiS_Pr.UseROM) && (ivideo->chip >= XGI_20)) {
		   ivideo->SiS_Pr.UseROM = FALSE;
		   ivideo->haveXGIROM = ivideo->SiS_Pr.SiS_XGIROM = TRUE;
		   if( (ivideo->revision_id == 2) &&
		       (!(ivideo->bios_abase[0x1d1] & 0x01)) ) {
			ivideo->SiS_Pr.DDCPortMixup = TRUE;
		   }
		}
L
Linus Torvalds 已提交
6177
	} else {
T
Thomas Winischhofer 已提交
6178
		printk(KERN_INFO "sisfb: Video ROM usage disabled\n");
L
Linus Torvalds 已提交
6179 6180
	}

T
Thomas Winischhofer 已提交
6181
	/* Find systems for special custom timing */
L
Linus Torvalds 已提交
6182
	if(ivideo->SiS_Pr.SiS_CustomT == CUT_NONE) {
T
Thomas Winischhofer 已提交
6183
		sisfb_detect_custom_timing(ivideo);
L
Linus Torvalds 已提交
6184 6185
	}

T
Thomas Winischhofer 已提交
6186 6187
	/* POST card in case this has not been done by the BIOS */
	if( (!ivideo->sisvga_enabled)
L
Linus Torvalds 已提交
6188
#if !defined(__i386__) && !defined(__x86_64__)
T
Thomas Winischhofer 已提交
6189
			     || (sisfb_resetcard)
L
Linus Torvalds 已提交
6190
#endif
T
Thomas Winischhofer 已提交
6191 6192 6193
						 ) {
#ifdef CONFIG_FB_SIS_300
		if(ivideo->sisvga_engine == SIS_300_VGA) {
L
Linus Torvalds 已提交
6194 6195
			if(ivideo->chip == SIS_300) {
				sisfb_post_sis300(pdev);
T
Thomas Winischhofer 已提交
6196
				ivideo->sisfb_can_post = 1;
L
Linus Torvalds 已提交
6197 6198 6199 6200 6201
			}
		}
#endif

#ifdef CONFIG_FB_SIS_315
T
Thomas Winischhofer 已提交
6202 6203 6204
		if(ivideo->sisvga_engine == SIS_315_VGA) {
			int result = 1;
		/*	if((ivideo->chip == SIS_315H)   ||
L
Linus Torvalds 已提交
6205 6206 6207 6208
			   (ivideo->chip == SIS_315)    ||
			   (ivideo->chip == SIS_315PRO) ||
			   (ivideo->chip == SIS_330)) {
				sisfb_post_sis315330(pdev);
T
Thomas Winischhofer 已提交
6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222
			} else */ if(ivideo->chip == XGI_20) {
				result = sisfb_post_xgi(pdev);
				ivideo->sisfb_can_post = 1;
			} else if((ivideo->chip == XGI_40) && ivideo->haveXGIROM) {
				result = sisfb_post_xgi(pdev);
				ivideo->sisfb_can_post = 1;
			} else {
				printk(KERN_INFO "sisfb: Card is not "
					"POSTed and sisfb can't do this either.\n");
			}
			if(!result) {
				printk(KERN_ERR "sisfb: Failed to POST card\n");
				ret = -ENODEV;
				goto error_3;
L
Linus Torvalds 已提交
6223 6224 6225
			}
		}
#endif
T
Thomas Winischhofer 已提交
6226
	}
L
Linus Torvalds 已提交
6227

T
Thomas Winischhofer 已提交
6228 6229 6230
	ivideo->sisfb_card_posted = 1;

	/* Find out about RAM size */
L
Linus Torvalds 已提交
6231
	if(sisfb_get_dram_size(ivideo)) {
T
Thomas Winischhofer 已提交
6232 6233 6234
		printk(KERN_INFO "sisfb: Fatal error: Unable to determine VRAM size.\n");
		ret = -ENODEV;
		goto error_3;
L
Linus Torvalds 已提交
6235 6236
	}

T
Thomas Winischhofer 已提交
6237 6238

	/* Enable PCI addressing and MMIO */
L
Linus Torvalds 已提交
6239 6240
	if((ivideo->sisfb_mode_idx < 0) ||
	   ((sisbios_mode[ivideo->sisfb_mode_idx].mode_no[ivideo->mni]) != 0xFF)) {
T
Thomas Winischhofer 已提交
6241 6242 6243 6244
		/* Enable PCI_LINEAR_ADDRESSING and MMIO_ENABLE  */
		orSISIDXREG(SISSR, IND_SIS_PCI_ADDRESS_SET, (SIS_PCI_ADDR_ENABLE | SIS_MEM_MAP_IO_ENABLE));
		/* Enable 2D accelerator engine */
		orSISIDXREG(SISSR, IND_SIS_MODULE_ENABLE, SIS_ENABLE_2D);
L
Linus Torvalds 已提交
6245 6246 6247
	}

	if(sisfb_pdc != 0xff) {
T
Thomas Winischhofer 已提交
6248 6249 6250 6251 6252
		if(ivideo->sisvga_engine == SIS_300_VGA)
			sisfb_pdc &= 0x3c;
		else
			sisfb_pdc &= 0x1f;
		ivideo->SiS_Pr.PDC = sisfb_pdc;
L
Linus Torvalds 已提交
6253 6254 6255
	}
#ifdef CONFIG_FB_SIS_315
	if(ivideo->sisvga_engine == SIS_315_VGA) {
T
Thomas Winischhofer 已提交
6256 6257
		if(sisfb_pdca != 0xff)
			ivideo->SiS_Pr.PDCA = sisfb_pdca & 0x1f;
L
Linus Torvalds 已提交
6258 6259 6260 6261
	}
#endif

	if(!request_mem_region(ivideo->video_base, ivideo->video_size, "sisfb FB")) {
T
Thomas Winischhofer 已提交
6262 6263
		printk(KERN_ERR "sisfb: Fatal error: Unable to reserve %dMB framebuffer memory\n",
				(int)(ivideo->video_size >> 20));
L
Linus Torvalds 已提交
6264
		printk(KERN_ERR "sisfb: Is there another framebuffer driver active?\n");
T
Thomas Winischhofer 已提交
6265 6266
		ret = -ENODEV;
		goto error_3;
L
Linus Torvalds 已提交
6267 6268 6269 6270
	}

	if(!request_mem_region(ivideo->mmio_base, ivideo->mmio_size, "sisfb MMIO")) {
		printk(KERN_ERR "sisfb: Fatal error: Unable to reserve MMIO region\n");
T
Thomas Winischhofer 已提交
6271 6272
		ret = -ENODEV;
		goto error_2;
L
Linus Torvalds 已提交
6273 6274 6275
	}

	ivideo->video_vbase = ioremap(ivideo->video_base, ivideo->video_size);
T
Thomas Winischhofer 已提交
6276
	ivideo->SiS_Pr.VideoMemoryAddress = ivideo->video_vbase;
L
Linus Torvalds 已提交
6277
	if(!ivideo->video_vbase) {
T
Thomas Winischhofer 已提交
6278 6279 6280
		printk(KERN_ERR "sisfb: Fatal error: Unable to map framebuffer memory\n");
		ret = -ENODEV;
		goto error_1;
L
Linus Torvalds 已提交
6281 6282 6283 6284
	}

	ivideo->mmio_vbase = ioremap(ivideo->mmio_base, ivideo->mmio_size);
	if(!ivideo->mmio_vbase) {
T
Thomas Winischhofer 已提交
6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297
		printk(KERN_ERR "sisfb: Fatal error: Unable to map MMIO region\n");
		ret = -ENODEV;
error_0:	iounmap(ivideo->video_vbase);
error_1:	release_mem_region(ivideo->video_base, ivideo->video_size);
error_2:	release_mem_region(ivideo->mmio_base, ivideo->mmio_size);
error_3:	vfree(ivideo->bios_abase);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
error_4:
#endif
		if(ivideo->lpcdev)
			SIS_PCI_PUT_DEVICE(ivideo->lpcdev);
		if(ivideo->nbridge)
			SIS_PCI_PUT_DEVICE(ivideo->nbridge);
L
Linus Torvalds 已提交
6298
		pci_set_drvdata(pdev, NULL);
T
Thomas Winischhofer 已提交
6299 6300 6301 6302
		if(!ivideo->sisvga_enabled)
			pci_disable_device(pdev);
		kfree(sis_fb_info);
		return ret;
L
Linus Torvalds 已提交
6303 6304
	}

T
Thomas Winischhofer 已提交
6305 6306 6307 6308 6309 6310 6311
	printk(KERN_INFO "sisfb: Video RAM at 0x%lx, mapped to 0x%lx, size %ldk\n",
		ivideo->video_base, (unsigned long)ivideo->video_vbase, ivideo->video_size / 1024);

	if(ivideo->video_offset) {
		printk(KERN_INFO "sisfb: Viewport offset %ldk\n",
			ivideo->video_offset / 1024);
	}
L
Linus Torvalds 已提交
6312 6313

	printk(KERN_INFO "sisfb: MMIO at 0x%lx, mapped to 0x%lx, size %ldk\n",
T
Thomas Winischhofer 已提交
6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326
		ivideo->mmio_base, (unsigned long)ivideo->mmio_vbase, ivideo->mmio_size / 1024);


	/* Determine the size of the command queue */
	if(ivideo->sisvga_engine == SIS_300_VGA) {
		ivideo->cmdQueueSize = TURBO_QUEUE_AREA_SIZE;
	} else {
		if(ivideo->chip == XGI_20) {
			ivideo->cmdQueueSize = COMMAND_QUEUE_AREA_SIZE_Z7;
		} else {
			ivideo->cmdQueueSize = COMMAND_QUEUE_AREA_SIZE;
		}
	}
L
Linus Torvalds 已提交
6327

T
Thomas Winischhofer 已提交
6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340
	/* Engines are no longer initialized here; this is
	 * now done after the first mode-switch (if the
	 * submitted var has its acceleration flags set).
	 */

	/* Calculate the base of the (unused) hw cursor */
	ivideo->hwcursor_vbase = ivideo->video_vbase
				 + ivideo->video_size
				 - ivideo->cmdQueueSize
				 - ivideo->hwcursor_size;
	ivideo->caps |= HW_CURSOR_CAP;

	/* Initialize offscreen memory manager */
L
Linus Torvalds 已提交
6341 6342 6343 6344 6345
	if((ivideo->havenoheap = sisfb_heap_init(ivideo))) {
		printk(KERN_WARNING "sisfb: Failed to initialize offscreen memory heap\n");
	}

	/* Used for clearing the screen only, therefore respect our mem limit */
T
Thomas Winischhofer 已提交
6346 6347
	ivideo->SiS_Pr.VideoMemoryAddress += ivideo->video_offset;
	ivideo->SiS_Pr.VideoMemorySize = ivideo->sisfb_mem;
L
Linus Torvalds 已提交
6348

T
Thomas Winischhofer 已提交
6349
	ivideo->mtrr = -1;
L
Linus Torvalds 已提交
6350 6351 6352 6353 6354 6355

	ivideo->vbflags = 0;
	ivideo->lcddefmodeidx = DEFAULT_LCDMODE;
	ivideo->tvdefmodeidx  = DEFAULT_TVMODE;
	ivideo->defmodeidx    = DEFAULT_MODE;

T
Thomas Winischhofer 已提交
6356 6357 6358 6359 6360 6361
	ivideo->newrom = 0;
	if(ivideo->chip < XGI_20) {
		if(ivideo->bios_abase) {
			ivideo->newrom = SiSDetermineROMLayout661(&ivideo->SiS_Pr);
		}
	}
L
Linus Torvalds 已提交
6362 6363 6364 6365 6366 6367 6368 6369

	if((ivideo->sisfb_mode_idx < 0) ||
	   ((sisbios_mode[ivideo->sisfb_mode_idx].mode_no[ivideo->mni]) != 0xFF)) {

		sisfb_sense_crt1(ivideo);

		sisfb_get_VB_type(ivideo);

T
Thomas Winischhofer 已提交
6370
		if(ivideo->vbflags2 & VB2_VIDEOBRIDGE) {
L
Linus Torvalds 已提交
6371 6372 6373 6374 6375
			sisfb_detect_VB_connect(ivideo);
		}

		ivideo->currentvbflags = ivideo->vbflags & (VB_VIDEOBRIDGE | TV_STANDARD);

T
Thomas Winischhofer 已提交
6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405
		/* Decide on which CRT2 device to use */
		if(ivideo->vbflags2 & VB2_VIDEOBRIDGE) {
			if(ivideo->sisfb_crt2type != -1) {
				if((ivideo->sisfb_crt2type == CRT2_LCD) &&
				   (ivideo->vbflags & CRT2_LCD)) {
					ivideo->currentvbflags |= CRT2_LCD;
				} else if(ivideo->sisfb_crt2type != CRT2_LCD) {
					ivideo->currentvbflags |= ivideo->sisfb_crt2type;
				}
			} else {
				/* Chrontel 700x TV detection often unreliable, therefore
				 * use a different default order on such machines
				 */
				if((ivideo->sisvga_engine == SIS_300_VGA) &&
				   (ivideo->vbflags2 & VB2_CHRONTEL)) {
					if(ivideo->vbflags & CRT2_LCD)
						ivideo->currentvbflags |= CRT2_LCD;
					else if(ivideo->vbflags & CRT2_TV)
						ivideo->currentvbflags |= CRT2_TV;
					else if(ivideo->vbflags & CRT2_VGA)
						ivideo->currentvbflags |= CRT2_VGA;
				} else {
					if(ivideo->vbflags & CRT2_TV)
						ivideo->currentvbflags |= CRT2_TV;
					else if(ivideo->vbflags & CRT2_LCD)
						ivideo->currentvbflags |= CRT2_LCD;
					else if(ivideo->vbflags & CRT2_VGA)
						ivideo->currentvbflags |= CRT2_VGA;
				}
			}
L
Linus Torvalds 已提交
6406 6407 6408
		}

		if(ivideo->vbflags & CRT2_LCD) {
T
Thomas Winischhofer 已提交
6409
			sisfb_detect_lcd_type(ivideo);
L
Linus Torvalds 已提交
6410 6411
		}

T
Thomas Winischhofer 已提交
6412
		sisfb_save_pdc_emi(ivideo);
L
Linus Torvalds 已提交
6413 6414

		if(!ivideo->sisfb_crt1off) {
T
Thomas Winischhofer 已提交
6415
			sisfb_handle_ddc(ivideo, &ivideo->sisfb_thismonitor, 0);
L
Linus Torvalds 已提交
6416
		} else {
T
Thomas Winischhofer 已提交
6417 6418 6419 6420
			if((ivideo->vbflags2 & VB2_SISTMDSBRIDGE) &&
			   (ivideo->vbflags & (CRT2_VGA | CRT2_LCD))) {
				sisfb_handle_ddc(ivideo, &ivideo->sisfb_thismonitor, 1);
			}
L
Linus Torvalds 已提交
6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451
		}

		if(ivideo->sisfb_mode_idx >= 0) {
			int bu = ivideo->sisfb_mode_idx;
			ivideo->sisfb_mode_idx = sisfb_validate_mode(ivideo,
					ivideo->sisfb_mode_idx, ivideo->currentvbflags);
			if(bu != ivideo->sisfb_mode_idx) {
				printk(KERN_ERR "Mode %dx%dx%d failed validation\n",
					sisbios_mode[bu].xres,
					sisbios_mode[bu].yres,
					sisbios_mode[bu].bpp);
			}
		}

		if(ivideo->sisfb_mode_idx < 0) {
			switch(ivideo->currentvbflags & VB_DISPTYPE_DISP2) {
			   case CRT2_LCD:
				ivideo->sisfb_mode_idx = ivideo->lcddefmodeidx;
				break;
			   case CRT2_TV:
				ivideo->sisfb_mode_idx = ivideo->tvdefmodeidx;
				break;
			   default:
				ivideo->sisfb_mode_idx = ivideo->defmodeidx;
				break;
			}
		}

		ivideo->mode_no = sisbios_mode[ivideo->sisfb_mode_idx].mode_no[ivideo->mni];

		if(ivideo->refresh_rate != 0) {
T
Thomas Winischhofer 已提交
6452 6453
			sisfb_search_refresh_rate(ivideo, ivideo->refresh_rate,
						ivideo->sisfb_mode_idx);
L
Linus Torvalds 已提交
6454 6455 6456 6457 6458 6459 6460 6461
		}

		if(ivideo->rate_idx == 0) {
			ivideo->rate_idx = sisbios_mode[ivideo->sisfb_mode_idx].rate_idx;
			ivideo->refresh_rate = 60;
		}

		if(ivideo->sisfb_thismonitor.datavalid) {
T
Thomas Winischhofer 已提交
6462 6463 6464 6465 6466 6467
			if(!sisfb_verify_rate(ivideo, &ivideo->sisfb_thismonitor,
						ivideo->sisfb_mode_idx,
						ivideo->rate_idx,
						ivideo->refresh_rate)) {
				printk(KERN_INFO "sisfb: WARNING: Refresh rate "
							"exceeds monitor specs!\n");
L
Linus Torvalds 已提交
6468 6469 6470 6471 6472 6473 6474 6475 6476
			}
		}

		ivideo->video_bpp = sisbios_mode[ivideo->sisfb_mode_idx].bpp;
		ivideo->video_width = sisbios_mode[ivideo->sisfb_mode_idx].xres;
		ivideo->video_height = sisbios_mode[ivideo->sisfb_mode_idx].yres;

		sisfb_set_vparms(ivideo);

T
Thomas Winischhofer 已提交
6477 6478 6479 6480 6481 6482
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)

		/* ---------------- For 2.4: Now switch the mode ------------------ */

		printk(KERN_INFO "sisfb: Setting mode %dx%dx%d (%dHz)\n",
			ivideo->video_width, ivideo->video_height, ivideo->video_bpp,
L
Linus Torvalds 已提交
6483 6484
			ivideo->refresh_rate);

T
Thomas Winischhofer 已提交
6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495
		/* Determine whether or not acceleration is to be
		 * used. Need to know before pre/post_set_mode()
		 */
		ivideo->accel = 0;
		ivideo->default_var.accel_flags &= ~FB_ACCELF_TEXT;
		if(ivideo->sisfb_accel) {
			ivideo->accel = -1;
			ivideo->default_var.accel_flags |= FB_ACCELF_TEXT;
		}

		/* Now switch the mode */
L
Linus Torvalds 已提交
6496 6497
		sisfb_pre_setmode(ivideo);

T
Thomas Winischhofer 已提交
6498
		if(SiSSetMode(&ivideo->SiS_Pr, ivideo->mode_no) == 0) {
L
Linus Torvalds 已提交
6499 6500
			printk(KERN_ERR "sisfb: Fatal error: Setting mode[0x%x] failed\n",
									ivideo->mode_no);
T
Thomas Winischhofer 已提交
6501
			ret = -EINVAL;
L
Linus Torvalds 已提交
6502
			iounmap(ivideo->mmio_vbase);
T
Thomas Winischhofer 已提交
6503
			goto error_0;
L
Linus Torvalds 已提交
6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515
		}

		outSISIDXREG(SISSR, IND_SIS_PASSWORD, SIS_PASSWORD);

		sisfb_post_setmode(ivideo);

		/* Maximize regardless of sisfb_max at startup */
		ivideo->default_var.yres_virtual = 32767;

		/* Force reset of x virtual in crtc_to_var */
		ivideo->default_var.xres_virtual = 0;

T
Thomas Winischhofer 已提交
6516
		/* Copy mode timing to var */
L
Linus Torvalds 已提交
6517 6518
		sisfb_crtc_to_var(ivideo, &ivideo->default_var);

T
Thomas Winischhofer 已提交
6519
		/* Find out about screen pitch */
L
Linus Torvalds 已提交
6520 6521 6522
		sisfb_calc_pitch(ivideo, &ivideo->default_var);
		sisfb_set_pitch(ivideo);

T
Thomas Winischhofer 已提交
6523
		/* Init the accelerator (does nothing currently) */
L
Linus Torvalds 已提交
6524
		sisfb_initaccel(ivideo);
T
Thomas Winischhofer 已提交
6525 6526

		/* Init some fbinfo entries */
L
Linus Torvalds 已提交
6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541
		sis_fb_info->node  = -1;
		sis_fb_info->flags = FBINFO_FLAG_DEFAULT;
		sis_fb_info->fbops = &sisfb_ops;
		sis_fb_info->disp  = &ivideo->sis_disp;
		sis_fb_info->blank = &sisfb_blank;
		sis_fb_info->switch_con = &sisfb_switch;
		sis_fb_info->updatevar  = &sisfb_update_var;
		sis_fb_info->changevar  = NULL;
		strcpy(sis_fb_info->fontname, sisfb_fontname);

		sisfb_set_disp(-1, &ivideo->default_var, sis_fb_info);

#else		/* --------- For 2.6: Setup a somewhat sane default var ------------ */

		printk(KERN_INFO "sisfb: Default mode is %dx%dx%d (%dHz)\n",
T
Thomas Winischhofer 已提交
6542
			ivideo->video_width, ivideo->video_height, ivideo->video_bpp,
L
Linus Torvalds 已提交
6543 6544
			ivideo->refresh_rate);

T
Thomas Winischhofer 已提交
6545
		/* Set up the default var according to chosen default display mode */
L
Linus Torvalds 已提交
6546 6547 6548 6549 6550
		ivideo->default_var.xres = ivideo->default_var.xres_virtual = ivideo->video_width;
		ivideo->default_var.yres = ivideo->default_var.yres_virtual = ivideo->video_height;
		ivideo->default_var.bits_per_pixel = ivideo->video_bpp;

		sisfb_bpp_to_var(ivideo, &ivideo->default_var);
T
Thomas Winischhofer 已提交
6551

L
Linus Torvalds 已提交
6552
		ivideo->default_var.pixclock = (u32) (1000000000 /
T
Thomas Winischhofer 已提交
6553 6554 6555 6556 6557 6558 6559 6560
			sisfb_mode_rate_to_dclock(&ivideo->SiS_Pr, ivideo->mode_no, ivideo->rate_idx));

		if(sisfb_mode_rate_to_ddata(&ivideo->SiS_Pr, ivideo->mode_no,
						ivideo->rate_idx, &ivideo->default_var)) {
			if((ivideo->default_var.vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE) {
				ivideo->default_var.pixclock <<= 1;
			}
		}
L
Linus Torvalds 已提交
6561 6562

		if(ivideo->sisfb_ypan) {
T
Thomas Winischhofer 已提交
6563 6564 6565 6566 6567 6568
			/* Maximize regardless of sisfb_max at startup */
			ivideo->default_var.yres_virtual =
				sisfb_calc_maxyres(ivideo, &ivideo->default_var);
			if(ivideo->default_var.yres_virtual < ivideo->default_var.yres) {
				ivideo->default_var.yres_virtual = ivideo->default_var.yres;
			}
L
Linus Torvalds 已提交
6569 6570 6571 6572 6573 6574
		}

		sisfb_calc_pitch(ivideo, &ivideo->default_var);

		ivideo->accel = 0;
		if(ivideo->sisfb_accel) {
T
Thomas Winischhofer 已提交
6575
			ivideo->accel = -1;
L
Linus Torvalds 已提交
6576
#ifdef STUPID_ACCELF_TEXT_SHIT
T
Thomas Winischhofer 已提交
6577
			ivideo->default_var.accel_flags |= FB_ACCELF_TEXT;
L
Linus Torvalds 已提交
6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593
#endif
		}
		sisfb_initaccel(ivideo);

#if defined(FBINFO_HWACCEL_DISABLED) && defined(FBINFO_HWACCEL_XPAN)
		sis_fb_info->flags = FBINFO_DEFAULT 		|
				     FBINFO_HWACCEL_YPAN 	|
				     FBINFO_HWACCEL_XPAN 	|
				     FBINFO_HWACCEL_COPYAREA 	|
				     FBINFO_HWACCEL_FILLRECT 	|
				     ((ivideo->accel) ? 0 : FBINFO_HWACCEL_DISABLED);
#else
		sis_fb_info->flags = FBINFO_FLAG_DEFAULT;
#endif
		sis_fb_info->var = ivideo->default_var;
		sis_fb_info->fix = ivideo->sisfb_fix;
T
Thomas Winischhofer 已提交
6594
		sis_fb_info->screen_base = ivideo->video_vbase + ivideo->video_offset;
L
Linus Torvalds 已提交
6595 6596 6597 6598
		sis_fb_info->fbops = &sisfb_ops;

		sisfb_get_fix(&sis_fb_info->fix, -1, sis_fb_info);
		sis_fb_info->pseudo_palette = ivideo->pseudo_palette;
T
Thomas Winischhofer 已提交
6599

L
Linus Torvalds 已提交
6600 6601 6602
		fb_alloc_cmap(&sis_fb_info->cmap, 256 , 0);
#endif		/* 2.6 */

T
Thomas Winischhofer 已提交
6603
		printk(KERN_DEBUG "sisfb: Initial vbflags 0x%x\n", (int)ivideo->vbflags);
L
Linus Torvalds 已提交
6604 6605 6606 6607

#ifdef CONFIG_MTRR
		ivideo->mtrr = mtrr_add(ivideo->video_base, ivideo->video_size,
					MTRR_TYPE_WRCOMB, 1);
T
Thomas Winischhofer 已提交
6608
		if(ivideo->mtrr < 0) {
L
Linus Torvalds 已提交
6609 6610 6611 6612 6613 6614 6615 6616 6617 6618
			printk(KERN_DEBUG "sisfb: Failed to add MTRRs\n");
		}
#endif

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
		vc_resize_con(1, 1, 0);
#endif

		if(register_framebuffer(sis_fb_info) < 0) {
			printk(KERN_ERR "sisfb: Fatal error: Failed to register framebuffer\n");
T
Thomas Winischhofer 已提交
6619
			ret = -EINVAL;
L
Linus Torvalds 已提交
6620
			iounmap(ivideo->mmio_vbase);
T
Thomas Winischhofer 已提交
6621
			goto error_0;
L
Linus Torvalds 已提交
6622 6623 6624 6625 6626 6627 6628 6629
		}

		ivideo->registered = 1;

		/* Enlist us */
		ivideo->next = card_list;
		card_list = ivideo;

T
Thomas Winischhofer 已提交
6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653
#ifdef SIS_OLD_CONFIG_COMPAT
		{
		int ret;
		/* Our ioctls are all "32/64bit compatible" */
		ret =  register_ioctl32_conversion(FBIO_ALLOC,             NULL);
		ret |= register_ioctl32_conversion(FBIO_FREE,              NULL);
		ret |= register_ioctl32_conversion(FBIOGET_VBLANK,         NULL);
		ret |= register_ioctl32_conversion(SISFB_GET_INFO_SIZE,    NULL);
		ret |= register_ioctl32_conversion(SISFB_GET_INFO,         NULL);
		ret |= register_ioctl32_conversion(SISFB_GET_TVPOSOFFSET,  NULL);
		ret |= register_ioctl32_conversion(SISFB_SET_TVPOSOFFSET,  NULL);
		ret |= register_ioctl32_conversion(SISFB_SET_LOCK,         NULL);
		ret |= register_ioctl32_conversion(SISFB_GET_VBRSTATUS,    NULL);
		ret |= register_ioctl32_conversion(SISFB_GET_AUTOMAXIMIZE, NULL);
		ret |= register_ioctl32_conversion(SISFB_SET_AUTOMAXIMIZE, NULL);
		ret |= register_ioctl32_conversion(SISFB_COMMAND,          NULL);
		if(ret)
			printk(KERN_ERR
				"sisfb: Error registering ioctl32 translations\n");
		else
			ivideo->ioctl32registered = 1;
		}
#endif

L
Linus Torvalds 已提交
6654
		printk(KERN_INFO "sisfb: 2D acceleration is %s, y-panning %s\n",
T
Thomas Winischhofer 已提交
6655 6656 6657 6658 6659
			ivideo->sisfb_accel ? "enabled" : "disabled",
			ivideo->sisfb_ypan  ?
				(ivideo->sisfb_max ? "enabled (auto-max)" :
						"enabled (no auto-max)") :
									"disabled");
L
Linus Torvalds 已提交
6660 6661


T
Thomas Winischhofer 已提交
6662
		printk(KERN_INFO "fb%d: %s frame buffer device version %d.%d.%d\n",
L
Linus Torvalds 已提交
6663
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
T
Thomas Winischhofer 已提交
6664
			GET_FB_IDX(sis_fb_info->node),
L
Linus Torvalds 已提交
6665
#else
T
Thomas Winischhofer 已提交
6666
			sis_fb_info->node,
L
Linus Torvalds 已提交
6667 6668 6669
#endif
			ivideo->myid, VER_MAJOR, VER_MINOR, VER_LEVEL);

T
Thomas Winischhofer 已提交
6670
		printk(KERN_INFO "sisfb: Copyright (C) 2001-2005 Thomas Winischhofer\n");
L
Linus Torvalds 已提交
6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682

	}	/* if mode = "none" */

	return 0;
}

/*****************************************************/
/*                PCI DEVICE HANDLING                */
/*****************************************************/

static void __devexit sisfb_remove(struct pci_dev *pdev)
{
T
Thomas Winischhofer 已提交
6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707
	struct sis_video_info	*ivideo = pci_get_drvdata(pdev);
	struct fb_info		*sis_fb_info = ivideo->memyselfandi;
	int			registered = ivideo->registered;
	int			modechanged = ivideo->modechanged;

#ifdef SIS_OLD_CONFIG_COMPAT
	if(ivideo->ioctl32registered) {
		int ret;
		ret =  unregister_ioctl32_conversion(FBIO_ALLOC);
		ret |= unregister_ioctl32_conversion(FBIO_FREE);
		ret |= unregister_ioctl32_conversion(FBIOGET_VBLANK);
		ret |= unregister_ioctl32_conversion(SISFB_GET_INFO_SIZE);
		ret |= unregister_ioctl32_conversion(SISFB_GET_INFO);
		ret |= unregister_ioctl32_conversion(SISFB_GET_TVPOSOFFSET);
		ret |= unregister_ioctl32_conversion(SISFB_SET_TVPOSOFFSET);
		ret |= unregister_ioctl32_conversion(SISFB_SET_LOCK);
		ret |= unregister_ioctl32_conversion(SISFB_GET_VBRSTATUS);
		ret |= unregister_ioctl32_conversion(SISFB_GET_AUTOMAXIMIZE);
		ret |= unregister_ioctl32_conversion(SISFB_SET_AUTOMAXIMIZE);
		ret |= unregister_ioctl32_conversion(SISFB_COMMAND);
		if(ret)
			printk(KERN_ERR
			     "sisfb: Error unregistering ioctl32 translations\n");
	}
#endif
L
Linus Torvalds 已提交
6708 6709 6710

	/* Unmap */
	iounmap(ivideo->mmio_vbase);
T
Thomas Winischhofer 已提交
6711
	iounmap(ivideo->video_vbase);
L
Linus Torvalds 已提交
6712 6713 6714 6715 6716

	/* Release mem regions */
	release_mem_region(ivideo->video_base, ivideo->video_size);
	release_mem_region(ivideo->mmio_base, ivideo->mmio_size);

T
Thomas Winischhofer 已提交
6717 6718 6719 6720 6721 6722 6723 6724
	vfree(ivideo->bios_abase);

	if(ivideo->lpcdev)
		SIS_PCI_PUT_DEVICE(ivideo->lpcdev);

	if(ivideo->nbridge)
		SIS_PCI_PUT_DEVICE(ivideo->nbridge);

L
Linus Torvalds 已提交
6725 6726
#ifdef CONFIG_MTRR
	/* Release MTRR region */
T
Thomas Winischhofer 已提交
6727
	if(ivideo->mtrr >= 0)
L
Linus Torvalds 已提交
6728 6729 6730
		mtrr_del(ivideo->mtrr, ivideo->video_base, ivideo->video_size);
#endif

T
Thomas Winischhofer 已提交
6731 6732 6733 6734 6735 6736 6737 6738
	pci_set_drvdata(pdev, NULL);

	/* If device was disabled when starting, disable
	 * it when quitting.
	 */
	if(!ivideo->sisvga_enabled)
		pci_disable_device(pdev);

L
Linus Torvalds 已提交
6739 6740 6741 6742 6743 6744 6745 6746 6747 6748
	/* Unregister the framebuffer */
	if(ivideo->registered) {
		unregister_framebuffer(sis_fb_info);
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,3))
		framebuffer_release(sis_fb_info);
#else
		kfree(sis_fb_info);
#endif
	}

T
Thomas Winischhofer 已提交
6749
	/* OK, our ivideo is gone for good from here. */
L
Linus Torvalds 已提交
6750 6751 6752 6753 6754 6755 6756 6757

	/* TODO: Restore the initial mode
	 * This sounds easy but is as good as impossible
	 * on many machines with SiS chip and video bridge
	 * since text modes are always set up differently
	 * from machine to machine. Depends on the type
	 * of integration between chipset and bridge.
	 */
T
Thomas Winischhofer 已提交
6758 6759 6760
	if(registered && modechanged)
		printk(KERN_INFO
			"sisfb: Restoring of text mode not supported yet\n");
L
Linus Torvalds 已提交
6761 6762 6763 6764 6765
};

static struct pci_driver sisfb_driver = {
	.name		= "sisfb",
	.id_table 	= sisfb_pci_table,
T
Thomas Winischhofer 已提交
6766
	.probe		= sisfb_probe,
L
Linus Torvalds 已提交
6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777
	.remove 	= __devexit_p(sisfb_remove)
};

SISINITSTATIC int __init sisfb_init(void)
{
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,8)
#ifndef MODULE
	char *options = NULL;

	if(fb_get_options("sisfb", &options))
		return -ENODEV;
T
Thomas Winischhofer 已提交
6778

L
Linus Torvalds 已提交
6779 6780 6781
	sisfb_setup(options);
#endif
#endif
T
Thomas Winischhofer 已提交
6782
	return pci_register_driver(&sisfb_driver);
L
Linus Torvalds 已提交
6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796
}

#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,8)
#ifndef MODULE
module_init(sisfb_init);
#endif
#endif

/*****************************************************/
/*                      MODULE                       */
/*****************************************************/

#ifdef MODULE

T
Thomas Winischhofer 已提交
6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870
static char		*mode = NULL;
static int		vesa = -1;
static unsigned int	rate = 0;
static unsigned int	crt1off = 1;
static unsigned int	mem = 0;
static char		*forcecrt2type = NULL;
static int		forcecrt1 = -1;
static int		pdc = -1;
static int		pdc1 = -1;
static int		noaccel = -1;
static int		noypan  = -1;
static int		nomax = -1;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
static int		inverse = 0;
#endif
static int		userom = -1;
static int		useoem = -1;
static char		*tvstandard = NULL;
static int		nocrt2rate = 0;
static int		scalelcd = -1;
static char		*specialtiming = NULL;
static int		lvdshl = -1;
static int		tvxposoffset = 0, tvyposoffset = 0;
#if !defined(__i386__) && !defined(__x86_64__)
static int		resetcard = 0;
static int		videoram = 0;
#endif

static int __init sisfb_init_module(void)
{
	sisfb_setdefaultparms();

	if(rate)
		sisfb_parm_rate = rate;

	if((scalelcd == 0) || (scalelcd == 1))
		sisfb_scalelcd = scalelcd ^ 1;

	/* Need to check crt2 type first for fstn/dstn */

	if(forcecrt2type)
		sisfb_search_crt2type(forcecrt2type);

	if(tvstandard)
		sisfb_search_tvstd(tvstandard);

	if(mode)
		sisfb_search_mode(mode, FALSE);
	else if(vesa != -1)
		sisfb_search_vesamode(vesa, FALSE);

	sisfb_crt1off = (crt1off == 0) ? 1 : 0;

	sisfb_forcecrt1 = forcecrt1;
	if(forcecrt1 == 1)
		sisfb_crt1off = 0;
	else if(forcecrt1 == 0)
		sisfb_crt1off = 1;

	if(noaccel == 1)
		sisfb_accel = 0;
	else if(noaccel == 0)
		sisfb_accel = 1;

	if(noypan == 1)
		sisfb_ypan = 0;
	else if(noypan == 0)
		sisfb_ypan = 1;

	if(nomax == 1)
		sisfb_max = 0;
	else if(nomax == 0)
		sisfb_max = 1;

L
Linus Torvalds 已提交
6871
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
T
Thomas Winischhofer 已提交
6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900
	if(inverse) sisfb_inverse = 1;
#endif

	if(mem)
		sisfb_parm_mem = mem;

	if(userom != -1)
		sisfb_userom = userom;

	if(useoem != -1)
		sisfb_useoem = useoem;

        if(pdc != -1)
		sisfb_pdc  = (pdc  & 0x7f);

	if(pdc1 != -1)
		sisfb_pdca = (pdc1 & 0x1f);

	sisfb_nocrt2rate = nocrt2rate;

	if(specialtiming)
		sisfb_search_specialtiming(specialtiming);

	if((lvdshl >= 0) && (lvdshl <= 3))
		sisfb_lvdshl = lvdshl;

	sisfb_tvxposoffset = tvxposoffset;
	sisfb_tvyposoffset = tvyposoffset;

L
Linus Torvalds 已提交
6901
#if !defined(__i386__) && !defined(__x86_64__)
T
Thomas Winischhofer 已提交
6902 6903 6904
	sisfb_resetcard = (resetcard) ? 1 : 0;
	if(videoram)
		sisfb_videoram = videoram;
L
Linus Torvalds 已提交
6905 6906
#endif

T
Thomas Winischhofer 已提交
6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919
	return sisfb_init();
}

static void __exit sisfb_remove_module(void)
{
	pci_unregister_driver(&sisfb_driver);
	printk(KERN_DEBUG "sisfb: Module unloaded\n");
}

module_init(sisfb_init_module);
module_exit(sisfb_remove_module);

MODULE_DESCRIPTION("SiS 300/540/630/730/315/55x/65x/661/74x/330/76x/34x, XGI V3XT/V5/V8/Z7 framebuffer device driver");
L
Linus Torvalds 已提交
6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Thomas Winischhofer <thomas@winischhofer.net>, Others");

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
MODULE_PARM(mem, "i");
MODULE_PARM(noaccel, "i");
MODULE_PARM(noypan, "i");
MODULE_PARM(nomax, "i");
MODULE_PARM(userom, "i");
MODULE_PARM(useoem, "i");
MODULE_PARM(mode, "s");
MODULE_PARM(vesa, "i");
MODULE_PARM(rate, "i");
MODULE_PARM(forcecrt1, "i");
MODULE_PARM(forcecrt2type, "s");
MODULE_PARM(scalelcd, "i");
MODULE_PARM(pdc, "i");
MODULE_PARM(pdc1, "i");
MODULE_PARM(specialtiming, "s");
MODULE_PARM(lvdshl, "i");
MODULE_PARM(tvstandard, "s");
MODULE_PARM(tvxposoffset, "i");
MODULE_PARM(tvyposoffset, "i");
MODULE_PARM(nocrt2rate, "i");
MODULE_PARM(inverse, "i");
#if !defined(__i386__) && !defined(__x86_64__)
MODULE_PARM(resetcard, "i");
MODULE_PARM(videoram, "i");
#endif
#endif

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
module_param(mem, int, 0);
module_param(noaccel, int, 0);
module_param(noypan, int, 0);
module_param(nomax, int, 0);
module_param(userom, int, 0);
module_param(useoem, int, 0);
module_param(mode, charp, 0);
module_param(vesa, int, 0);
module_param(rate, int, 0);
module_param(forcecrt1, int, 0);
module_param(forcecrt2type, charp, 0);
module_param(scalelcd, int, 0);
module_param(pdc, int, 0);
module_param(pdc1, int, 0);
module_param(specialtiming, charp, 0);
module_param(lvdshl, int, 0);
module_param(tvstandard, charp, 0);
module_param(tvxposoffset, int, 0);
module_param(tvyposoffset, int, 0);
module_param(nocrt2rate, int, 0);
#if !defined(__i386__) && !defined(__x86_64__)
module_param(resetcard, int, 0);
module_param(videoram, int, 0);
#endif
#endif

T
Thomas Winischhofer 已提交
6978
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
L
Linus Torvalds 已提交
6979 6980 6981 6982 6983
MODULE_PARM_DESC(mem,
	"\nDetermines the beginning of the video memory heap in KB. This heap is used\n"
	  "for video RAM management for eg. DRM/DRI. On 300 series, the default depends\n"
	  "on the amount of video RAM available. If 8MB of video RAM or less is available,\n"
	  "the heap starts at 4096KB, if between 8 and 16MB are available at 8192KB,\n"
T
Thomas Winischhofer 已提交
6984
	  "otherwise at 12288KB. On 315/330/340 series, the heap size is 32KB by default.\n"
L
Linus Torvalds 已提交
6985 6986
	  "The value is to be specified without 'KB' and must match the MaxXFBMem setting\n"
	  "for XFree86 4.x/X.org 6.7 and later.\n");
T
Thomas Winischhofer 已提交
6987 6988 6989 6990 6991 6992 6993 6994 6995
#else
MODULE_PARM_DESC(mem,
	"\nDetermines the beginning of the video memory heap in KB. This heap is used\n"
	  "for video RAM management for eg. DRM/DRI. On 300 series, the default depends\n"
	  "on the amount of video RAM available. If 8MB of video RAM or less is available,\n"
	  "the heap starts at 4096KB, if between 8 and 16MB are available at 8192KB,\n"
	  "otherwise at 12288KB. On 315/330/340 series, the heap size is 32KB by default.\n"
	  "The value is to be specified without 'KB'.\n");
#endif
L
Linus Torvalds 已提交
6996 6997

MODULE_PARM_DESC(noaccel,
T
Thomas Winischhofer 已提交
6998
	"\nIf set to anything other than 0, 2D acceleration will be disabled.\n"
L
Linus Torvalds 已提交
6999 7000 7001
	  "(default: 0)\n");

MODULE_PARM_DESC(noypan,
T
Thomas Winischhofer 已提交
7002 7003
	"\nIf set to anything other than 0, y-panning will be disabled and scrolling\n"
	  "will be performed by redrawing the screen. (default: 0)\n");
L
Linus Torvalds 已提交
7004 7005

MODULE_PARM_DESC(nomax,
T
Thomas Winischhofer 已提交
7006
	"\nIf y-panning is enabled, sisfb will by default use the entire available video\n"
L
Linus Torvalds 已提交
7007 7008 7009 7010 7011 7012 7013
	  "memory for the virtual screen in order to optimize scrolling performance. If\n"
	  "this is set to anything other than 0, sisfb will not do this and thereby \n"
	  "enable the user to positively specify a virtual Y size of the screen using\n"
	  "fbset. (default: 0)\n");

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
MODULE_PARM_DESC(mode,
T
Thomas Winischhofer 已提交
7014 7015 7016
	"\nSelects the desired display mode in the format [X]x[Y]x[Depth], eg.\n"
	  "1024x768x16. Other formats supported include XxY-Depth and\n"
	  "XxY-Depth@Rate. If the parameter is only one (decimal or hexadecimal)\n"
L
Linus Torvalds 已提交
7017 7018 7019 7020 7021
	  "number, it will be interpreted as a VESA mode number. (default: none if\n"
	  "sisfb is a module; this leaves the console untouched and the driver will\n"
	  "only do the video memory management for eg. DRM/DRI; 800x600x8 if sisfb\n"
	  "is in the kernel)\n");
MODULE_PARM_DESC(vesa,
T
Thomas Winischhofer 已提交
7022 7023
	"\nSelects the desired display mode by VESA defined mode number, eg. 0x117\n"
	  "(default: 0x0000 if sisfb is a module; this leaves the console untouched\n"
L
Linus Torvalds 已提交
7024 7025 7026 7027 7028 7029
	  "and the driver will only do the video memory management for eg. DRM/DRI;\n"
	  "0x0103 if sisfb is in the kernel)\n");
#endif

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
MODULE_PARM_DESC(mode,
T
Thomas Winischhofer 已提交
7030 7031
	"\nSelects the desired default display mode in the format XxYxDepth,\n"
	 "eg. 1024x768x16. Other formats supported include XxY-Depth and\n"
L
Linus Torvalds 已提交
7032 7033 7034 7035
	 "XxY-Depth@Rate. If the parameter is only one (decimal or hexadecimal)\n"
	 "number, it will be interpreted as a VESA mode number. (default: 800x600x8)\n");

MODULE_PARM_DESC(vesa,
T
Thomas Winischhofer 已提交
7036 7037
	"\nSelects the desired default display mode by VESA defined mode number, eg.\n"
	 "0x117 (default: 0x0103)\n");
L
Linus Torvalds 已提交
7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066
#endif

MODULE_PARM_DESC(rate,
	"\nSelects the desired vertical refresh rate for CRT1 (external VGA) in Hz.\n"
	  "If the mode is specified in the format XxY-Depth@Rate, this parameter\n"
	  "will be ignored (default: 60)\n");

MODULE_PARM_DESC(forcecrt1,
	"\nNormally, the driver autodetects whether or not CRT1 (external VGA) is \n"
	  "connected. With this option, the detection can be overridden (1=CRT1 ON,\n"
	  "0=CRT1 OFF) (default: [autodetected])\n");

MODULE_PARM_DESC(forcecrt2type,
	"\nIf this option is omitted, the driver autodetects CRT2 output devices, such as\n"
	  "LCD, TV or secondary VGA. With this option, this autodetection can be\n"
	  "overridden. Possible parameters are LCD, TV, VGA or NONE. NONE disables CRT2.\n"
	  "On systems with a SiS video bridge, parameters SVIDEO, COMPOSITE or SCART can\n"
	  "be used instead of TV to override the TV detection. Furthermore, on systems\n"
	  "with a SiS video bridge, SVIDEO+COMPOSITE, HIVISION, YPBPR480I, YPBPR480P,\n"
	  "YPBPR720P and YPBPR1080I are understood. However, whether or not these work\n"
	  "depends on the very hardware in use. (default: [autodetected])\n");

MODULE_PARM_DESC(scalelcd,
	"\nSetting this to 1 will force the driver to scale the LCD image to the panel's\n"
	  "native resolution. Setting it to 0 will disable scaling; LVDS panels will\n"
	  "show black bars around the image, TMDS panels will probably do the scaling\n"
	  "themselves. Default: 1 on LVDS panels, 0 on TMDS panels\n");

MODULE_PARM_DESC(pdc,
T
Thomas Winischhofer 已提交
7067
	"\nThis is for manually selecting the LCD panel delay compensation. The driver\n"
L
Linus Torvalds 已提交
7068 7069
	  "should detect this correctly in most cases; however, sometimes this is not\n"
	  "possible. If you see 'small waves' on the LCD, try setting this to 4, 32 or 24\n"
T
Thomas Winischhofer 已提交
7070 7071 7072
	  "on a 300 series chipset; 6 on other chipsets. If the problem persists, try\n"
	  "other values (on 300 series: between 4 and 60 in steps of 4; otherwise: any\n"
	  "value from 0 to 31). (default: autodetected, if LCD is active during start)\n");
L
Linus Torvalds 已提交
7073 7074 7075

#ifdef CONFIG_FB_SIS_315
MODULE_PARM_DESC(pdc1,
T
Thomas Winischhofer 已提交
7076
	"\nThis is same as pdc, but for LCD-via CRT1. Hence, this is for the 315/330/340\n"
L
Linus Torvalds 已提交
7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105
	  "series only. (default: autodetected if LCD is in LCD-via-CRT1 mode during\n"
	  "startup) - Note: currently, this has no effect because LCD-via-CRT1 is not\n"
	  "implemented yet.\n");
#endif

MODULE_PARM_DESC(specialtiming,
	"\nPlease refer to documentation for more information on this option.\n");

MODULE_PARM_DESC(lvdshl,
	"\nPlease refer to documentation for more information on this option.\n");

MODULE_PARM_DESC(tvstandard,
	"\nThis allows overriding the BIOS default for the TV standard. Valid choices are\n"
	  "pal, ntsc, palm and paln. (default: [auto; pal or ntsc only])\n");

MODULE_PARM_DESC(tvxposoffset,
	"\nRelocate TV output horizontally. Possible parameters: -32 through 32.\n"
	  "Default: 0\n");

MODULE_PARM_DESC(tvyposoffset,
	"\nRelocate TV output vertically. Possible parameters: -32 through 32.\n"
	  "Default: 0\n");

MODULE_PARM_DESC(nocrt2rate,
	"\nSetting this to 1 will force the driver to use the default refresh rate for\n"
	  "CRT2 if CRT2 type is VGA. (default: 0, use same rate as CRT1)\n");

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
MODULE_PARM_DESC(inverse,
T
Thomas Winischhofer 已提交
7106
	"\nSetting this to anything but 0 should invert the display colors, but this\n"
L
Linus Torvalds 已提交
7107 7108 7109 7110 7111 7112 7113
	  "does not seem to work. (default: 0)\n");
#endif

#if !defined(__i386__) && !defined(__x86_64__)
#ifdef CONFIG_FB_SIS_300
MODULE_PARM_DESC(resetcard,
	"\nSet this to 1 in order to reset (POST) the card on non-x86 machines where\n"
T
Thomas Winischhofer 已提交
7114 7115
	  "the BIOS did not POST the card (only supported for SiS 300/305 and XGI cards\n"
	  "currently). Default: 0\n");
L
Linus Torvalds 已提交
7116 7117 7118 7119

MODULE_PARM_DESC(videoram,
	"\nSet this to the amount of video RAM (in kilobyte) the card has. Required on\n"
	  "some non-x86 architectures where the memory auto detection fails. Only\n"
T
Thomas Winischhofer 已提交
7120
	  "relevant if resetcard is set, too. SiS300/305 only. Default: [auto-detect]\n");
L
Linus Torvalds 已提交
7121 7122 7123 7124 7125
#endif
#endif

#endif 	   /*  /MODULE  */

T
Thomas Winischhofer 已提交
7126
/* _GPL only for new symbols. */
L
Linus Torvalds 已提交
7127 7128
EXPORT_SYMBOL(sis_malloc);
EXPORT_SYMBOL(sis_free);
T
Thomas Winischhofer 已提交
7129 7130 7131
EXPORT_SYMBOL_GPL(sis_malloc_new);
EXPORT_SYMBOL_GPL(sis_free_new);

L
Linus Torvalds 已提交
7132 7133