tridentfb.c 32.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 * Frame buffer driver for Trident Blade and Image series
 *
4
 * Copyright 2001, 2002 - Jani Monoses   <jani@iv.ro>
L
Linus Torvalds 已提交
5 6 7
 *
 *
 * CREDITS:(in order of appearance)
8 9 10 11 12 13
 *	skeletonfb.c by Geert Uytterhoeven and other fb code in drivers/video
 *	Special thanks ;) to Mattia Crivellini <tia@mclink.it>
 *	much inspired by the XFree86 4.x Trident driver sources
 *	by Alan Hourihane the FreeVGA project
 *	Francesco Salvestrini <salvestrini@users.sf.net> XP support,
 *	code, suggestions
L
Linus Torvalds 已提交
14
 * TODO:
15 16
 *	timing value tweaking so it looks good on every monitor in every mode
 *	TGUI acceleration
L
Linus Torvalds 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29
 */

#include <linux/module.h>
#include <linux/fb.h>
#include <linux/init.h>
#include <linux/pci.h>

#include <linux/delay.h>
#include <video/trident.h>

#define VERSION		"0.7.8-NEWAPI"

struct tridentfb_par {
30 31
	int vclk;		/* in MHz */
	void __iomem *io_virt;	/* iospace virtual memory address */
L
Linus Torvalds 已提交
32 33
};

34
static unsigned char eng_oper;	/* engine operation... */
L
Linus Torvalds 已提交
35 36 37 38 39 40 41 42 43 44 45
static struct fb_ops tridentfb_ops;

static struct tridentfb_par default_par;

/* FIXME:kmalloc these 3 instead */
static struct fb_info fb_info;
static u32 pseudo_pal[16];

static struct fb_var_screeninfo default_var;

static struct fb_fix_screeninfo tridentfb_fix = {
46
	.id = "Trident",
L
Linus Torvalds 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60
	.type = FB_TYPE_PACKED_PIXELS,
	.ypanstep = 1,
	.visual = FB_VISUAL_PSEUDOCOLOR,
	.accel = FB_ACCEL_NONE,
};

static int chip_id;

static int defaultaccel;
static int displaytype;

/* defaults which are normally overriden by user values */

/* video mode */
61
static char *mode_option __devinitdata = "640x480";
L
Linus Torvalds 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75
static int bpp = 8;

static int noaccel;

static int center;
static int stretch;

static int fp;
static int crt;

static int memsize;
static int memdiff;
static int nativex;

76 77
module_param(mode_option, charp, 0);
MODULE_PARM_DESC(mode_option, "Initial video mode e.g. '648x480-8@60'");
78 79
module_param_named(mode, mode_option, charp, 0);
MODULE_PARM_DESC(mode, "Initial video mode e.g. '648x480-8@60' (deprecated)");
L
Linus Torvalds 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
module_param(bpp, int, 0);
module_param(center, int, 0);
module_param(stretch, int, 0);
module_param(noaccel, int, 0);
module_param(memsize, int, 0);
module_param(memdiff, int, 0);
module_param(nativex, int, 0);
module_param(fp, int, 0);
module_param(crt, int, 0);

static int chip3D;
static int chipcyber;

static int is3Dchip(int id)
{
95 96 97 98 99 100 101 102 103
	return ((id == BLADE3D) || (id == CYBERBLADEE4) ||
		(id == CYBERBLADEi7) || (id == CYBERBLADEi7D) ||
		(id == CYBER9397) || (id == CYBER9397DVD) ||
		(id == CYBER9520) || (id == CYBER9525DVD) ||
		(id == IMAGE975) || (id == IMAGE985) ||
		(id == CYBERBLADEi1) || (id == CYBERBLADEi1D) ||
		(id == CYBERBLADEAi1) || (id == CYBERBLADEAi1D) ||
		(id == CYBERBLADEXPm8) || (id == CYBERBLADEXPm16) ||
		(id == CYBERBLADEXPAi1));
L
Linus Torvalds 已提交
104 105 106 107 108
}

static int iscyber(int id)
{
	switch (id) {
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	case CYBER9388:
	case CYBER9382:
	case CYBER9385:
	case CYBER9397:
	case CYBER9397DVD:
	case CYBER9520:
	case CYBER9525DVD:
	case CYBERBLADEE4:
	case CYBERBLADEi7D:
	case CYBERBLADEi1:
	case CYBERBLADEi1D:
	case CYBERBLADEAi1:
	case CYBERBLADEAi1D:
	case CYBERBLADEXPAi1:
		return 1;
L
Linus Torvalds 已提交
124

125 126 127 128 129 130 131 132 133 134 135
	case CYBER9320:
	case TGUI9660:
	case IMAGE975:
	case IMAGE985:
	case BLADE3D:
	case CYBERBLADEi7:	/* VIA MPV4 integrated version */

	default:
		/* case CYBERBLDAEXPm8:  Strange */
		/* case CYBERBLDAEXPm16: Strange */
		return 0;
L
Linus Torvalds 已提交
136 137 138
	}
}

139
#define CRT 0x3D0		/* CRTC registers offset for color display */
L
Linus Torvalds 已提交
140 141 142 143 144 145

#ifndef TRIDENT_MMIO
	#define TRIDENT_MMIO 1
#endif

#if TRIDENT_MMIO
146
	#define t_outb(val, reg)	writeb(val,((struct tridentfb_par *)(fb_info.par))->io_virt + reg)
L
Linus Torvalds 已提交
147 148
	#define t_inb(reg)	readb(((struct tridentfb_par*)(fb_info.par))->io_virt + reg)
#else
149
	#define t_outb(val, reg) outb(val, reg)
L
Linus Torvalds 已提交
150 151 152 153 154
	#define t_inb(reg) inb(reg)
#endif


static struct accel_switch {
155 156 157 158
	void (*init_accel) (int, int);
	void (*wait_engine) (void);
	void (*fill_rect) (u32, u32, u32, u32, u32, u32);
	void (*copy_rect) (u32, u32, u32, u32, u32, u32);
L
Linus Torvalds 已提交
159 160
} *acc;

161
#define writemmr(r, v)	writel(v, ((struct tridentfb_par *)fb_info.par)->io_virt + r)
L
Linus Torvalds 已提交
162 163 164 165 166 167
#define readmmr(r)	readl(((struct tridentfb_par *)fb_info.par)->io_virt + r)

/*
 * Blade specific acceleration.
 */

168
#define point(x, y) ((y) << 16 | (x))
L
Linus Torvalds 已提交
169 170 171 172 173 174 175 176 177 178 179
#define STA	0x2120
#define CMD	0x2144
#define ROP	0x2148
#define CLR	0x2160
#define SR1	0x2100
#define SR2	0x2104
#define DR1	0x2108
#define DR2	0x210C

#define ROP_S	0xCC

180
static void blade_init_accel(int pitch, int bpp)
L
Linus Torvalds 已提交
181
{
182 183
	int v1 = (pitch >> 3) << 20;
	int tmp = 0, v2;
L
Linus Torvalds 已提交
184
	switch (bpp) {
185 186 187 188 189 190 191 192 193 194 195 196 197
	case 8:
		tmp = 0;
		break;
	case 15:
		tmp = 5;
		break;
	case 16:
		tmp = 1;
		break;
	case 24:
	case 32:
		tmp = 2;
		break;
L
Linus Torvalds 已提交
198
	}
199 200 201 202 203 204 205 206 207 208
	v2 = v1 | (tmp << 29);
	writemmr(0x21C0, v2);
	writemmr(0x21C4, v2);
	writemmr(0x21B8, v2);
	writemmr(0x21BC, v2);
	writemmr(0x21D0, v1);
	writemmr(0x21D4, v1);
	writemmr(0x21C8, v1);
	writemmr(0x21CC, v1);
	writemmr(0x216C, 0);
L
Linus Torvalds 已提交
209 210 211 212
}

static void blade_wait_engine(void)
{
213
	while (readmmr(STA) & 0xFA800000) ;
L
Linus Torvalds 已提交
214 215
}

216
static void blade_fill_rect(u32 x, u32 y, u32 w, u32 h, u32 c, u32 rop)
L
Linus Torvalds 已提交
217
{
218 219 220
	writemmr(CLR, c);
	writemmr(ROP, rop ? 0x66 : ROP_S);
	writemmr(CMD, 0x20000000 | 1 << 19 | 1 << 4 | 2 << 2);
L
Linus Torvalds 已提交
221

222 223
	writemmr(DR1, point(x, y));
	writemmr(DR2, point(x + w - 1, y + h - 1));
L
Linus Torvalds 已提交
224 225
}

226
static void blade_copy_rect(u32 x1, u32 y1, u32 x2, u32 y2, u32 w, u32 h)
L
Linus Torvalds 已提交
227
{
228
	u32 s1, s2, d1, d2;
L
Linus Torvalds 已提交
229
	int direction = 2;
230 231 232 233
	s1 = point(x1, y1);
	s2 = point(x1 + w - 1, y1 + h - 1);
	d1 = point(x2, y2);
	d2 = point(x2 + w - 1, y2 + h - 1);
L
Linus Torvalds 已提交
234 235

	if ((y1 > y2) || ((y1 == y2) && (x1 > x2)))
236
		direction = 0;
L
Linus Torvalds 已提交
237

238 239
	writemmr(ROP, ROP_S);
	writemmr(CMD, 0xE0000000 | 1 << 19 | 1 << 4 | 1 << 2 | direction);
L
Linus Torvalds 已提交
240

241 242 243 244
	writemmr(SR1, direction ? s2 : s1);
	writemmr(SR2, direction ? s1 : s2);
	writemmr(DR1, direction ? d2 : d1);
	writemmr(DR2, direction ? d1 : d2);
L
Linus Torvalds 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258
}

static struct accel_switch accel_blade = {
	blade_init_accel,
	blade_wait_engine,
	blade_fill_rect,
	blade_copy_rect,
};

/*
 * BladeXP specific acceleration functions
 */

#define ROP_P 0xF0
259
#define masked_point(x, y) ((y & 0xffff)<<16|(x & 0xffff))
L
Linus Torvalds 已提交
260

261
static void xp_init_accel(int pitch, int bpp)
L
Linus Torvalds 已提交
262
{
263
	int tmp = 0, v1;
L
Linus Torvalds 已提交
264 265 266
	unsigned char x = 0;

	switch (bpp) {
267 268 269 270 271 272 273 274 275 276 277 278
	case 8:
		x = 0;
		break;
	case 16:
		x = 1;
		break;
	case 24:
		x = 3;
		break;
	case 32:
		x = 2;
		break;
L
Linus Torvalds 已提交
279 280 281
	}

	switch (pitch << (bpp >> 3)) {
282 283 284 285 286 287 288 289 290 291 292 293 294
	case 8192:
	case 512:
		x |= 0x00;
		break;
	case 1024:
		x |= 0x04;
		break;
	case 2048:
		x |= 0x08;
		break;
	case 4096:
		x |= 0x0C;
		break;
L
Linus Torvalds 已提交
295 296
	}

297
	t_outb(x, 0x2125);
L
Linus Torvalds 已提交
298 299 300 301

	eng_oper = x | 0x40;

	switch (bpp) {
302 303 304 305 306 307 308 309 310 311 312
	case 8:
		tmp = 18;
		break;
	case 15:
	case 16:
		tmp = 19;
		break;
	case 24:
	case 32:
		tmp = 20;
		break;
L
Linus Torvalds 已提交
313 314 315 316
	}

	v1 = pitch << tmp;

317 318 319
	writemmr(0x2154, v1);
	writemmr(0x2150, v1);
	t_outb(3, 0x2126);
L
Linus Torvalds 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
}

static void xp_wait_engine(void)
{
	int busy;
	int count, timeout;

	count = 0;
	timeout = 0;
	for (;;) {
		busy = t_inb(STA) & 0x80;
		if (busy != 0x80)
			return;
		count++;
		if (count == 10000000) {
			/* Timeout */
			count = 9990000;
			timeout++;
			if (timeout == 8) {
				/* Reset engine */
				t_outb(0x00, 0x2120);
				return;
			}
		}
	}
}

347
static void xp_fill_rect(u32 x, u32 y, u32 w, u32 h, u32 c, u32 rop)
L
Linus Torvalds 已提交
348
{
349 350 351 352 353 354 355
	writemmr(0x2127, ROP_P);
	writemmr(0x2158, c);
	writemmr(0x2128, 0x4000);
	writemmr(0x2140, masked_point(h, w));
	writemmr(0x2138, masked_point(y, x));
	t_outb(0x01, 0x2124);
	t_outb(eng_oper, 0x2125);
L
Linus Torvalds 已提交
356 357
}

358
static void xp_copy_rect(u32 x1, u32 y1, u32 x2, u32 y2, u32 w, u32 h)
L
Linus Torvalds 已提交
359 360
{
	int direction;
361
	u32 x1_tmp, x2_tmp, y1_tmp, y2_tmp;
L
Linus Torvalds 已提交
362 363

	direction = 0x0004;
364

L
Linus Torvalds 已提交
365 366 367 368 369 370 371 372
	if ((x1 < x2) && (y1 == y2)) {
		direction |= 0x0200;
		x1_tmp = x1 + w - 1;
		x2_tmp = x2 + w - 1;
	} else {
		x1_tmp = x1;
		x2_tmp = x2;
	}
373

L
Linus Torvalds 已提交
374 375 376 377
	if (y1 < y2) {
		direction |= 0x0100;
		y1_tmp = y1 + h - 1;
		y2_tmp = y2 + h - 1;
378
	} else {
L
Linus Torvalds 已提交
379 380 381 382
		y1_tmp = y1;
		y2_tmp = y2;
	}

383 384 385 386 387 388
	writemmr(0x2128, direction);
	t_outb(ROP_S, 0x2127);
	writemmr(0x213C, masked_point(y1_tmp, x1_tmp));
	writemmr(0x2138, masked_point(y2_tmp, x2_tmp));
	writemmr(0x2140, masked_point(h, w));
	t_outb(0x01, 0x2124);
L
Linus Torvalds 已提交
389 390 391
}

static struct accel_switch accel_xp = {
392
	xp_init_accel,
L
Linus Torvalds 已提交
393 394 395 396 397 398 399 400
	xp_wait_engine,
	xp_fill_rect,
	xp_copy_rect,
};

/*
 * Image specific acceleration functions
 */
401
static void image_init_accel(int pitch, int bpp)
L
Linus Torvalds 已提交
402 403
{
	int tmp = 0;
404 405 406 407 408 409 410 411 412 413 414 415 416 417
	switch (bpp) {
	case 8:
		tmp = 0;
		break;
	case 15:
		tmp = 5;
		break;
	case 16:
		tmp = 1;
		break;
	case 24:
	case 32:
		tmp = 2;
		break;
L
Linus Torvalds 已提交
418 419
	}
	writemmr(0x2120, 0xF0000000);
420
	writemmr(0x2120, 0x40000000 | tmp);
L
Linus Torvalds 已提交
421 422 423 424 425
	writemmr(0x2120, 0x80000000);
	writemmr(0x2144, 0x00000000);
	writemmr(0x2148, 0x00000000);
	writemmr(0x2150, 0x00000000);
	writemmr(0x2154, 0x00000000);
426
	writemmr(0x2120, 0x60000000 | (pitch << 16) | pitch);
L
Linus Torvalds 已提交
427 428 429 430 431 432 433 434 435
	writemmr(0x216C, 0x00000000);
	writemmr(0x2170, 0x00000000);
	writemmr(0x217C, 0x00000000);
	writemmr(0x2120, 0x10000000);
	writemmr(0x2130, (2047 << 16) | 2047);
}

static void image_wait_engine(void)
{
436
	while (readmmr(0x2164) & 0xF0000000) ;
L
Linus Torvalds 已提交
437 438
}

439
static void image_fill_rect(u32 x, u32 y, u32 w, u32 h, u32 c, u32 rop)
L
Linus Torvalds 已提交
440
{
441 442
	writemmr(0x2120, 0x80000000);
	writemmr(0x2120, 0x90000000 | ROP_S);
L
Linus Torvalds 已提交
443

444
	writemmr(0x2144, c);
L
Linus Torvalds 已提交
445

446 447
	writemmr(DR1, point(x, y));
	writemmr(DR2, point(x + w - 1, y + h - 1));
L
Linus Torvalds 已提交
448

449
	writemmr(0x2124, 0x80000000 | 3 << 22 | 1 << 10 | 1 << 9);
L
Linus Torvalds 已提交
450 451
}

452
static void image_copy_rect(u32 x1, u32 y1, u32 x2, u32 y2, u32 w, u32 h)
L
Linus Torvalds 已提交
453
{
454
	u32 s1, s2, d1, d2;
L
Linus Torvalds 已提交
455
	int direction = 2;
456 457 458 459
	s1 = point(x1, y1);
	s2 = point(x1 + w - 1, y1 + h - 1);
	d1 = point(x2, y2);
	d2 = point(x2 + w - 1, y2 + h - 1);
L
Linus Torvalds 已提交
460

461 462 463 464 465 466 467 468 469 470 471 472
	if ((y1 > y2) || ((y1 == y2) && (x1 > x2)))
		direction = 0;

	writemmr(0x2120, 0x80000000);
	writemmr(0x2120, 0x90000000 | ROP_S);

	writemmr(SR1, direction ? s2 : s1);
	writemmr(SR2, direction ? s1 : s2);
	writemmr(DR1, direction ? d2 : d1);
	writemmr(DR2, direction ? d1 : d2);
	writemmr(0x2124, 0x80000000 | 1 << 22 | 1 << 10 | 1 << 7 | direction);
}
L
Linus Torvalds 已提交
473 474 475 476 477 478 479 480 481 482 483 484

static struct accel_switch accel_image = {
	image_init_accel,
	image_wait_engine,
	image_fill_rect,
	image_copy_rect,
};

/*
 * Accel functions called by the upper layers
 */
#ifdef CONFIG_FB_TRIDENT_ACCEL
485 486
static void tridentfb_fillrect(struct fb_info *info,
			       const struct fb_fillrect *fr)
L
Linus Torvalds 已提交
487 488
{
	int bpp = info->var.bits_per_pixel;
489
	int col = 0;
490

L
Linus Torvalds 已提交
491
	switch (bpp) {
492 493 494 495 496 497 498 499 500 501 502 503 504 505
	default:
	case 8:
		col |= fr->color;
		col |= col << 8;
		col |= col << 16;
		break;
	case 16:
		col = ((u32 *)(info->pseudo_palette))[fr->color];
		break;
	case 32:
		col = ((u32 *)(info->pseudo_palette))[fr->color];
		break;
	}

L
Linus Torvalds 已提交
506 507 508
	acc->fill_rect(fr->dx, fr->dy, fr->width, fr->height, col, fr->rop);
	acc->wait_engine();
}
509 510
static void tridentfb_copyarea(struct fb_info *info,
			       const struct fb_copyarea *ca)
L
Linus Torvalds 已提交
511
{
512
	acc->copy_rect(ca->sx, ca->sy, ca->dx, ca->dy, ca->width, ca->height);
L
Linus Torvalds 已提交
513 514 515 516 517 518 519 520 521 522 523 524 525 526
	acc->wait_engine();
}
#else /* !CONFIG_FB_TRIDENT_ACCEL */
#define tridentfb_fillrect cfb_fillrect
#define tridentfb_copyarea cfb_copyarea
#endif /* CONFIG_FB_TRIDENT_ACCEL */


/*
 * Hardware access functions
 */

static inline unsigned char read3X4(int reg)
{
527
	struct tridentfb_par *par = (struct tridentfb_par *)fb_info.par;
L
Linus Torvalds 已提交
528
	writeb(reg, par->io_virt + CRT + 4);
529
	return readb(par->io_virt + CRT + 5);
L
Linus Torvalds 已提交
530 531 532 533
}

static inline void write3X4(int reg, unsigned char val)
{
534
	struct tridentfb_par *par = (struct tridentfb_par *)fb_info.par;
L
Linus Torvalds 已提交
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
	writeb(reg, par->io_virt + CRT + 4);
	writeb(val, par->io_virt + CRT + 5);
}

static inline unsigned char read3C4(int reg)
{
	t_outb(reg, 0x3C4);
	return t_inb(0x3C5);
}

static inline void write3C4(int reg, unsigned char val)
{
	t_outb(reg, 0x3C4);
	t_outb(val, 0x3C5);
}

static inline unsigned char read3CE(int reg)
{
	t_outb(reg, 0x3CE);
	return t_inb(0x3CF);
}

static inline void writeAttr(int reg, unsigned char val)
{
559
	readb(((struct tridentfb_par *)fb_info.par)->io_virt + CRT + 0x0A);	/* flip-flop to index */
L
Linus Torvalds 已提交
560 561 562 563 564 565 566 567 568 569
	t_outb(reg, 0x3C0);
	t_outb(val, 0x3C0);
}

static inline void write3CE(int reg, unsigned char val)
{
	t_outb(reg, 0x3CE);
	t_outb(val, 0x3CF);
}

570
static void enable_mmio(void)
L
Linus Torvalds 已提交
571 572 573 574 575 576 577 578
{
	/* Goto New Mode */
	outb(0x0B, 0x3C4);
	inb(0x3C5);

	/* Unprotect registers */
	outb(NewMode1, 0x3C4);
	outb(0x80, 0x3C5);
579

L
Linus Torvalds 已提交
580
	/* Enable MMIO */
581
	outb(PCIReg, 0x3D4);
L
Linus Torvalds 已提交
582
	outb(inb(0x3D5) | 0x01, 0x3D5);
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
}

static void disable_mmio(void)
{
	/* Goto New Mode */
	t_outb(0x0B, 0x3C4);
	t_inb(0x3C5);

	/* Unprotect registers */
	t_outb(NewMode1, 0x3C4);
	t_outb(0x80, 0x3C5);

	/* Disable MMIO */
	t_outb(PCIReg, 0x3D4);
	t_outb(t_inb(0x3D5) & ~0x01, 0x3D5);
L
Linus Torvalds 已提交
598 599 600 601 602
}

#define crtc_unlock()	write3X4(CRTVSyncEnd, read3X4(CRTVSyncEnd) & 0x7F)

/*  Return flat panel's maximum x resolution */
R
Randy Dunlap 已提交
603
static int __devinit get_nativex(void)
L
Linus Torvalds 已提交
604
{
605
	int x, y, tmp;
L
Linus Torvalds 已提交
606 607 608 609

	if (nativex)
		return nativex;

610
	tmp = (read3CE(VertStretch) >> 4) & 3;
L
Linus Torvalds 已提交
611 612

	switch (tmp) {
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
	case 0:
		x = 1280; y = 1024;
		break;
	case 2:
		x = 1024; y = 768;
		break;
	case 3:
		x = 800; y = 600;
		break;
	case 4:
		x = 1400; y = 1050;
		break;
	case 1:
	default:
		x = 640;  y = 480;
		break;
L
Linus Torvalds 已提交
629 630 631 632 633 634 635 636 637 638
	}

	output("%dx%d flat panel found\n", x, y);
	return x;
}

/* Set pitch */
static void set_lwidth(int width)
{
	write3X4(Offset, width & 0xFF);
639 640
	write3X4(AddColReg,
		 (read3X4(AddColReg) & 0xCF) | ((width & 0x300) >> 4));
L
Linus Torvalds 已提交
641 642 643 644 645
}

/* For resolutions smaller than FP resolution stretch */
static void screen_stretch(void)
{
646 647 648 649 650 651
	if (chip_id != CYBERBLADEXPAi1)
		write3CE(BiosReg, 0);
	else
		write3CE(BiosReg, 8);
	write3CE(VertStretch, (read3CE(VertStretch) & 0x7C) | 1);
	write3CE(HorStretch, (read3CE(HorStretch) & 0x7C) | 1);
L
Linus Torvalds 已提交
652 653 654 655 656
}

/* For resolutions smaller than FP resolution center */
static void screen_center(void)
{
657 658
	write3CE(VertStretch, (read3CE(VertStretch) & 0x7C) | 0x80);
	write3CE(HorStretch, (read3CE(HorStretch) & 0x7C) | 0x80);
L
Linus Torvalds 已提交
659 660 661 662 663 664 665
}

/* Address of first shown pixel in display memory */
static void set_screen_start(int base)
{
	write3X4(StartAddrLow, base & 0xFF);
	write3X4(StartAddrHigh, (base & 0xFF00) >> 8);
666 667 668 669
	write3X4(CRTCModuleTest,
		 (read3X4(CRTCModuleTest) & 0xDF) | ((base & 0x10000) >> 11));
	write3X4(CRTHiOrd,
		 (read3X4(CRTHiOrd) & 0xF8) | ((base & 0xE0000) >> 17));
L
Linus Torvalds 已提交
670 671 672
}

/* Use 20.12 fixed-point for NTSC value and frequency calculation */
673
#define calc_freq(n, m, k)  ( ((unsigned long)0xE517 * (n + 8) / ((m + 2) * (1 << k))) >> 12 )
L
Linus Torvalds 已提交
674 675 676 677

/* Set dotclock frequency */
static void set_vclk(int freq)
{
678 679 680
	int m, n, k;
	int f, fi, d, di;
	unsigned char lo = 0, hi = 0;
L
Linus Torvalds 已提交
681 682

	d = 20;
683 684 685 686 687 688 689 690 691 692 693
	for (k = 2; k >= 0; k--)
		for (m = 0; m < 63; m++)
			for (n = 0; n < 128; n++) {
				fi = calc_freq(n, m, k);
				if ((di = abs(fi - freq)) < d) {
					d = di;
					f = fi;
					lo = n;
					hi = (k << 6) | m;
				}
			}
L
Linus Torvalds 已提交
694
	if (chip3D) {
695 696
		write3C4(ClockHigh, hi);
		write3C4(ClockLow, lo);
L
Linus Torvalds 已提交
697
	} else {
698 699
		outb(lo, 0x43C8);
		outb(hi, 0x43C9);
L
Linus Torvalds 已提交
700
	}
701
	debug("VCLK = %X %X\n", hi, lo);
L
Linus Torvalds 已提交
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
}

/* Set number of lines for flat panels*/
static void set_number_of_lines(int lines)
{
	int tmp = read3CE(CyberEnhance) & 0x8F;
	if (lines > 1024)
		tmp |= 0x50;
	else if (lines > 768)
		tmp |= 0x30;
	else if (lines > 600)
		tmp |= 0x20;
	else if (lines > 480)
		tmp |= 0x10;
	write3CE(CyberEnhance, tmp);
}

/*
 * If we see that FP is active we assume we have one.
 * Otherwise we have a CRT display.User can override.
 */
R
Randy Dunlap 已提交
723
static unsigned int __devinit get_displaytype(void)
L
Linus Torvalds 已提交
724 725 726 727 728
{
	if (fp)
		return DISPLAY_FP;
	if (crt || !chipcyber)
		return DISPLAY_CRT;
729
	return (read3CE(FPConfig) & 0x10) ? DISPLAY_FP : DISPLAY_CRT;
L
Linus Torvalds 已提交
730 731 732
}

/* Try detecting the video memory size */
R
Randy Dunlap 已提交
733
static unsigned int __devinit get_memsize(void)
L
Linus Torvalds 已提交
734 735 736 737 738 739 740 741
{
	unsigned char tmp, tmp2;
	unsigned int k;

	/* If memory size provided by user */
	if (memsize)
		k = memsize * Kb;
	else
742 743 744 745
		switch (chip_id) {
		case CYBER9525DVD:
			k = 2560 * Kb;
			break;
L
Linus Torvalds 已提交
746 747 748 749
		default:
			tmp = read3X4(SPR) & 0x0F;
			switch (tmp) {

750
			case 0x01:
751
				k = 512 * Kb;
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
				break;
			case 0x02:
				k = 6 * Mb;	/* XP */
				break;
			case 0x03:
				k = 1 * Mb;
				break;
			case 0x04:
				k = 8 * Mb;
				break;
			case 0x06:
				k = 10 * Mb;	/* XP */
				break;
			case 0x07:
				k = 2 * Mb;
				break;
			case 0x08:
				k = 12 * Mb;	/* XP */
				break;
			case 0x0A:
				k = 14 * Mb;	/* XP */
				break;
			case 0x0C:
				k = 16 * Mb;	/* XP */
				break;
			case 0x0E:		/* XP */

				tmp2 = read3C4(0xC1);
				switch (tmp2) {
				case 0x00:
					k = 20 * Mb;
					break;
				case 0x01:
					k = 24 * Mb;
					break;
				case 0x10:
					k = 28 * Mb;
					break;
				case 0x11:
					k = 32 * Mb;
					break;
				default:
					k = 1 * Mb;
					break;
				}
				break;

			case 0x0F:
				k = 4 * Mb;
				break;
			default:
				k = 1 * Mb;
L
Linus Torvalds 已提交
804 805
				break;
			}
806
		}
L
Linus Torvalds 已提交
807 808

	k -= memdiff * Kb;
809
	output("framebuffer size = %d Kb\n", k / Kb);
L
Linus Torvalds 已提交
810 811 812 813
	return k;
}

/* See if we can handle the video mode described in var */
814 815
static int tridentfb_check_var(struct fb_var_screeninfo *var,
			       struct fb_info *info)
L
Linus Torvalds 已提交
816 817 818 819 820
{
	int bpp = var->bits_per_pixel;
	debug("enter\n");

	/* check color depth */
821
	if (bpp == 24)
L
Linus Torvalds 已提交
822
		bpp = var->bits_per_pixel = 32;
823
	/* check whether resolution fits on panel and in memory */
L
Linus Torvalds 已提交
824 825
	if (flatpanel && nativex && var->xres > nativex)
		return -EINVAL;
826
	if (var->xres * var->yres_virtual * bpp / 8 > info->fix.smem_len)
L
Linus Torvalds 已提交
827 828 829
		return -EINVAL;

	switch (bpp) {
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
	case 8:
		var->red.offset = 0;
		var->green.offset = 0;
		var->blue.offset = 0;
		var->red.length = 6;
		var->green.length = 6;
		var->blue.length = 6;
		break;
	case 16:
		var->red.offset = 11;
		var->green.offset = 5;
		var->blue.offset = 0;
		var->red.length = 5;
		var->green.length = 6;
		var->blue.length = 5;
		break;
	case 32:
		var->red.offset = 16;
		var->green.offset = 8;
		var->blue.offset = 0;
		var->red.length = 8;
		var->green.length = 8;
		var->blue.length = 8;
		break;
	default:
		return -EINVAL;
L
Linus Torvalds 已提交
856 857 858 859 860 861
	}
	debug("exit\n");

	return 0;

}
862

L
Linus Torvalds 已提交
863 864
/* Pan the display */
static int tridentfb_pan_display(struct fb_var_screeninfo *var,
865
				 struct fb_info *info)
L
Linus Torvalds 已提交
866 867 868 869 870
{
	unsigned int offset;

	debug("enter\n");
	offset = (var->xoffset + (var->yoffset * var->xres))
871
		* var->bits_per_pixel / 32;
L
Linus Torvalds 已提交
872 873 874 875 876 877 878
	info->var.xoffset = var->xoffset;
	info->var.yoffset = var->yoffset;
	set_screen_start(offset);
	debug("exit\n");
	return 0;
}

879 880
#define shadowmode_on()  write3CE(CyberControl, read3CE(CyberControl) | 0x81)
#define shadowmode_off() write3CE(CyberControl, read3CE(CyberControl) & 0x7E)
L
Linus Torvalds 已提交
881 882 883 884

/* Set the hardware to the requested video mode */
static int tridentfb_set_par(struct fb_info *info)
{
885 886 887 888
	struct tridentfb_par *par = (struct tridentfb_par *)(info->par);
	u32 htotal, hdispend, hsyncstart, hsyncend, hblankstart, hblankend;
	u32 vtotal, vdispend, vsyncstart, vsyncend, vblankstart, vblankend;
	struct fb_var_screeninfo *var = &info->var;
L
Linus Torvalds 已提交
889 890 891
	int bpp = var->bits_per_pixel;
	unsigned char tmp;
	debug("enter\n");
892 893 894 895 896 897
	hdispend = var->xres / 8 - 1;
	hsyncstart = (var->xres + var->right_margin) / 8;
	hsyncend = var->hsync_len / 8;
	htotal =
		(var->xres + var->left_margin + var->right_margin +
		 var->hsync_len) / 8 - 10;
L
Linus Torvalds 已提交
898 899 900 901 902 903
	hblankstart = hdispend + 1;
	hblankend = htotal + 5;

	vdispend = var->yres - 1;
	vsyncstart = var->yres + var->lower_margin;
	vsyncend = var->vsync_len;
904
	vtotal = var->upper_margin + vsyncstart + vsyncend - 2;
L
Linus Torvalds 已提交
905 906 907 908
	vblankstart = var->yres;
	vblankend = vtotal + 2;

	crtc_unlock();
909
	write3CE(CyberControl, 8);
L
Linus Torvalds 已提交
910 911 912 913 914 915 916

	if (flatpanel && var->xres < nativex) {
		/*
		 * on flat panels with native size larger
		 * than requested resolution decide whether
		 * we stretch or center
		 */
917
		t_outb(0xEB, 0x3C2);
L
Linus Torvalds 已提交
918 919 920

		shadowmode_on();

921
		if (center)
L
Linus Torvalds 已提交
922 923 924 925 926
			screen_center();
		else if (stretch)
			screen_stretch();

	} else {
927 928
		t_outb(0x2B, 0x3C2);
		write3CE(CyberControl, 8);
L
Linus Torvalds 已提交
929 930 931 932 933 934 935 936
	}

	/* vertical timing values */
	write3X4(CRTVTotal, vtotal & 0xFF);
	write3X4(CRTVDispEnd, vdispend & 0xFF);
	write3X4(CRTVSyncStart, vsyncstart & 0xFF);
	write3X4(CRTVSyncEnd, (vsyncend & 0x0F));
	write3X4(CRTVBlankStart, vblankstart & 0xFF);
937
	write3X4(CRTVBlankEnd, 0 /* p->vblankend & 0xFF */ );
L
Linus Torvalds 已提交
938 939 940 941 942

	/* horizontal timing values */
	write3X4(CRTHTotal, htotal & 0xFF);
	write3X4(CRTHDispEnd, hdispend & 0xFF);
	write3X4(CRTHSyncStart, hsyncstart & 0xFF);
943
	write3X4(CRTHSyncEnd, (hsyncend & 0x1F) | ((hblankend & 0x20) << 2));
L
Linus Torvalds 已提交
944
	write3X4(CRTHBlankStart, hblankstart & 0xFF);
945
	write3X4(CRTHBlankEnd, 0 /* (p->hblankend & 0x1F) */ );
L
Linus Torvalds 已提交
946 947 948 949 950 951 952 953 954 955 956 957 958

	/* higher bits of vertical timing values */
	tmp = 0x10;
	if (vtotal & 0x100) tmp |= 0x01;
	if (vdispend & 0x100) tmp |= 0x02;
	if (vsyncstart & 0x100) tmp |= 0x04;
	if (vblankstart & 0x100) tmp |= 0x08;

	if (vtotal & 0x200) tmp |= 0x20;
	if (vdispend & 0x200) tmp |= 0x40;
	if (vsyncstart & 0x200) tmp |= 0x80;
	write3X4(CRTOverflow, tmp);

959
	tmp = read3X4(CRTHiOrd) | 0x08;	/* line compare bit 10 */
L
Linus Torvalds 已提交
960 961 962 963 964 965 966 967 968 969
	if (vtotal & 0x400) tmp |= 0x80;
	if (vblankstart & 0x400) tmp |= 0x40;
	if (vsyncstart & 0x400) tmp |= 0x20;
	if (vdispend & 0x400) tmp |= 0x10;
	write3X4(CRTHiOrd, tmp);

	tmp = 0;
	if (htotal & 0x800) tmp |= 0x800 >> 11;
	if (hblankstart & 0x800) tmp |= 0x800 >> 7;
	write3X4(HorizOverflow, tmp);
970

L
Linus Torvalds 已提交
971 972
	tmp = 0x40;
	if (vblankstart & 0x200) tmp |= 0x20;
973
//FIXME	if (info->var.vmode & FB_VMODE_DOUBLE) tmp |= 0x80;  /* double scan for 200 line modes */
L
Linus Torvalds 已提交
974 975
	write3X4(CRTMaxScanLine, tmp);

976 977 978
	write3X4(CRTLineCompare, 0xFF);
	write3X4(CRTPRowScan, 0);
	write3X4(CRTModeControl, 0xC3);
L
Linus Torvalds 已提交
979

980
	write3X4(LinearAddReg, 0x20);	/* enable linear addressing */
L
Linus Torvalds 已提交
981

982 983
	tmp = (info->var.vmode & FB_VMODE_INTERLACED) ? 0x84 : 0x80;
	write3X4(CRTCModuleTest, tmp);	/* enable access extended memory */
L
Linus Torvalds 已提交
984

985
	write3X4(GraphEngReg, 0x80);	/* enable GE for text acceleration */
L
Linus Torvalds 已提交
986

987 988
#ifdef CONFIG_FB_TRIDENT_ACCEL
	acc->init_accel(info->var.xres, bpp);
989
#endif
990

L
Linus Torvalds 已提交
991
	switch (bpp) {
992 993 994 995 996 997 998 999 1000 1001 1002 1003
	case 8:
		tmp = 0x00;
		break;
	case 16:
		tmp = 0x05;
		break;
	case 24:
		tmp = 0x29;
		break;
	case 32:
		tmp = 0x09;
		break;
L
Linus Torvalds 已提交
1004 1005 1006 1007 1008 1009
	}

	write3X4(PixelBusReg, tmp);

	tmp = 0x10;
	if (chipcyber)
1010 1011
		tmp |= 0x20;
	write3X4(DRAMControl, tmp);	/* both IO, linear enable */
L
Linus Torvalds 已提交
1012 1013

	write3X4(InterfaceSel, read3X4(InterfaceSel) | 0x40);
1014 1015
	write3X4(Performance, 0x92);
	write3X4(PCIReg, 0x07);		/* MMIO & PCI read and write burst enable */
L
Linus Torvalds 已提交
1016 1017

	/* convert from picoseconds to MHz */
1018
	par->vclk = 1000000 / info->var.pixclock;
L
Linus Torvalds 已提交
1019
	if (bpp == 32)
1020
		par->vclk *= 2;
L
Linus Torvalds 已提交
1021 1022
	set_vclk(par->vclk);

1023 1024 1025 1026 1027
	write3C4(0, 3);
	write3C4(1, 1);		/* set char clock 8 dots wide */
	write3C4(2, 0x0F);	/* enable 4 maps because needed in chain4 mode */
	write3C4(3, 0);
	write3C4(4, 0x0E);	/* memory mode enable bitmaps ?? */
L
Linus Torvalds 已提交
1028

1029 1030 1031 1032 1033
	write3CE(MiscExtFunc, (bpp == 32) ? 0x1A : 0x12);	/* divide clock by 2 if 32bpp */
							/* chain4 mode display and CPU path */
	write3CE(0x5, 0x40);	/* no CGA compat, allow 256 col */
	write3CE(0x6, 0x05);	/* graphics mode */
	write3CE(0x7, 0x0F);	/* planes? */
L
Linus Torvalds 已提交
1034 1035 1036

	if (chip_id == CYBERBLADEXPAi1) {
		/* This fixes snow-effect in 32 bpp */
1037
		write3X4(CRTHSyncStart, 0x84);
L
Linus Torvalds 已提交
1038 1039
	}

1040 1041 1042
	writeAttr(0x10, 0x41);	/* graphics mode and support 256 color modes */
	writeAttr(0x12, 0x0F);	/* planes */
	writeAttr(0x13, 0);	/* horizontal pel panning */
L
Linus Torvalds 已提交
1043

1044 1045 1046 1047 1048
	/* colors */
	for (tmp = 0; tmp < 0x10; tmp++)
		writeAttr(tmp, tmp);
	readb(par->io_virt + CRT + 0x0A);	/* flip-flop to index */
	t_outb(0x20, 0x3C0);			/* enable attr */
L
Linus Torvalds 已提交
1049 1050

	switch (bpp) {
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
	case 8:
		tmp = 0;
		break;
	case 15:
		tmp = 0x10;
		break;
	case 16:
		tmp = 0x30;
		break;
	case 24:
	case 32:
		tmp = 0xD0;
		break;
L
Linus Torvalds 已提交
1064 1065 1066 1067 1068 1069 1070
	}

	t_inb(0x3C8);
	t_inb(0x3C6);
	t_inb(0x3C6);
	t_inb(0x3C6);
	t_inb(0x3C6);
1071
	t_outb(tmp, 0x3C6);
L
Linus Torvalds 已提交
1072 1073 1074 1075
	t_inb(0x3C8);

	if (flatpanel)
		set_number_of_lines(info->var.yres);
1076
	set_lwidth(info->var.xres * bpp / (4 * 16));
L
Linus Torvalds 已提交
1077
	info->fix.visual = (bpp == 8) ? FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
1078 1079
	info->fix.line_length = info->var.xres * (bpp >> 3);
	info->cmap.len = (bpp == 8) ? 256 : 16;
L
Linus Torvalds 已提交
1080 1081 1082 1083 1084 1085
	debug("exit\n");
	return 0;
}

/* Set one color register */
static int tridentfb_setcolreg(unsigned regno, unsigned red, unsigned green,
1086 1087
			       unsigned blue, unsigned transp,
			       struct fb_info *info)
L
Linus Torvalds 已提交
1088 1089 1090 1091 1092 1093
{
	int bpp = info->var.bits_per_pixel;

	if (regno >= info->cmap.len)
		return 1;

1094
	if (bpp == 8) {
1095 1096
		t_outb(0xFF, 0x3C6);
		t_outb(regno, 0x3C8);
L
Linus Torvalds 已提交
1097

1098 1099 1100
		t_outb(red >> 10, 0x3C9);
		t_outb(green >> 10, 0x3C9);
		t_outb(blue >> 10, 0x3C9);
L
Linus Torvalds 已提交
1101

1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
	} else if (regno < 16) {
		if (bpp == 16) {	/* RGB 565 */
			u32 col;

			col = (red & 0xF800) | ((green & 0xFC00) >> 5) |
				((blue & 0xF800) >> 11);
			col |= col << 16;
			((u32 *)(info->pseudo_palette))[regno] = col;
		} else if (bpp == 32)		/* ARGB 8888 */
			((u32*)info->pseudo_palette)[regno] =
1112 1113
				((transp & 0xFF00) << 16)	|
				((red & 0xFF00) << 8)		|
1114
				((green & 0xFF00))		|
1115
				((blue & 0xFF00) >> 8);
1116
	}
L
Linus Torvalds 已提交
1117

1118
/* 	debug("exit\n"); */
L
Linus Torvalds 已提交
1119 1120 1121 1122 1123 1124
	return 0;
}

/* Try blanking the screen.For flat panels it does nothing */
static int tridentfb_blank(int blank_mode, struct fb_info *info)
{
1125
	unsigned char PMCont, DPMSCont;
L
Linus Torvalds 已提交
1126 1127 1128 1129

	debug("enter\n");
	if (flatpanel)
		return 0;
1130
	t_outb(0x04, 0x83C8); /* Read DPMS Control */
L
Linus Torvalds 已提交
1131 1132
	PMCont = t_inb(0x83C6) & 0xFC;
	DPMSCont = read3CE(PowerStatus) & 0xFC;
1133
	switch (blank_mode) {
L
Linus Torvalds 已提交
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
	case FB_BLANK_UNBLANK:
		/* Screen: On, HSync: On, VSync: On */
	case FB_BLANK_NORMAL:
		/* Screen: Off, HSync: On, VSync: On */
		PMCont |= 0x03;
		DPMSCont |= 0x00;
		break;
	case FB_BLANK_HSYNC_SUSPEND:
		/* Screen: Off, HSync: Off, VSync: On */
		PMCont |= 0x02;
		DPMSCont |= 0x01;
		break;
	case FB_BLANK_VSYNC_SUSPEND:
		/* Screen: Off, HSync: On, VSync: Off */
		PMCont |= 0x02;
		DPMSCont |= 0x02;
		break;
	case FB_BLANK_POWERDOWN:
		/* Screen: Off, HSync: Off, VSync: Off */
		PMCont |= 0x00;
		DPMSCont |= 0x03;
		break;
1156
	}
L
Linus Torvalds 已提交
1157

1158 1159 1160
	write3CE(PowerStatus, DPMSCont);
	t_outb(4, 0x83C8);
	t_outb(PMCont, 0x83C6);
L
Linus Torvalds 已提交
1161 1162 1163 1164 1165 1166 1167

	debug("exit\n");

	/* let fbcon do a softblank for us */
	return (blank_mode == FB_BLANK_NORMAL) ? 1 : 0;
}

1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
static struct fb_ops tridentfb_ops = {
	.owner = THIS_MODULE,
	.fb_setcolreg = tridentfb_setcolreg,
	.fb_pan_display = tridentfb_pan_display,
	.fb_blank = tridentfb_blank,
	.fb_check_var = tridentfb_check_var,
	.fb_set_par = tridentfb_set_par,
	.fb_fillrect = tridentfb_fillrect,
	.fb_copyarea = tridentfb_copyarea,
	.fb_imageblit = cfb_imageblit,
};

static int __devinit trident_pci_probe(struct pci_dev * dev,
				       const struct pci_device_id * id)
L
Linus Torvalds 已提交
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
{
	int err;
	unsigned char revision;

	err = pci_enable_device(dev);
	if (err)
		return err;

	chip_id = id->device;

1192
	if (chip_id == CYBERBLADEi1)
1193 1194 1195 1196
		output("*** Please do use cyblafb, Cyberblade/i1 support "
		       "will soon be removed from tridentfb!\n");


L
Linus Torvalds 已提交
1197
	/* If PCI id is 0x9660 then further detect chip type */
1198

L
Linus Torvalds 已提交
1199
	if (chip_id == TGUI9660) {
1200 1201 1202
		outb(RevisionID, 0x3C4);
		revision = inb(0x3C5);

L
Linus Torvalds 已提交
1203
		switch (revision) {
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
		case 0x22:
		case 0x23:
			chip_id = CYBER9397;
			break;
		case 0x2A:
			chip_id = CYBER9397DVD;
			break;
		case 0x30:
		case 0x33:
		case 0x34:
		case 0x35:
		case 0x38:
		case 0x3A:
		case 0xB3:
			chip_id = CYBER9385;
			break;
		case 0x40 ... 0x43:
			chip_id = CYBER9382;
			break;
		case 0x4A:
			chip_id = CYBER9388;
			break;
		default:
			break;
L
Linus Torvalds 已提交
1228 1229 1230 1231 1232 1233 1234 1235
		}
	}

	chip3D = is3Dchip(chip_id);
	chipcyber = iscyber(chip_id);

	if (is_xp(chip_id)) {
		acc = &accel_xp;
1236
	} else if (is_blade(chip_id)) {
L
Linus Torvalds 已提交
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
		acc = &accel_blade;
	} else {
		acc = &accel_image;
	}

	/* acceleration is on by default for 3D chips */
	defaultaccel = chip3D && !noaccel;

	fb_info.par = &default_par;

	/* setup MMIO region */
1248 1249
	tridentfb_fix.mmio_start = pci_resource_start(dev, 1);
	tridentfb_fix.mmio_len = chip3D ? 0x20000 : 0x10000;
L
Linus Torvalds 已提交
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259

	if (!request_mem_region(tridentfb_fix.mmio_start, tridentfb_fix.mmio_len, "tridentfb")) {
		debug("request_region failed!\n");
		return -1;
	}

	default_par.io_virt = ioremap_nocache(tridentfb_fix.mmio_start, tridentfb_fix.mmio_len);

	if (!default_par.io_virt) {
		debug("ioremap failed\n");
1260 1261
		err = -1;
		goto out_unmap1;
L
Linus Torvalds 已提交
1262 1263 1264
	}

	enable_mmio();
1265

L
Linus Torvalds 已提交
1266
	/* setup framebuffer memory */
1267
	tridentfb_fix.smem_start = pci_resource_start(dev, 0);
L
Linus Torvalds 已提交
1268
	tridentfb_fix.smem_len = get_memsize();
1269

L
Linus Torvalds 已提交
1270 1271
	if (!request_mem_region(tridentfb_fix.smem_start, tridentfb_fix.smem_len, "tridentfb")) {
		debug("request_mem_region failed!\n");
1272
		disable_mmio();
1273
		err = -1;
1274
		goto out_unmap1;
L
Linus Torvalds 已提交
1275 1276 1277
	}

	fb_info.screen_base = ioremap_nocache(tridentfb_fix.smem_start,
1278
					      tridentfb_fix.smem_len);
L
Linus Torvalds 已提交
1279 1280 1281

	if (!fb_info.screen_base) {
		debug("ioremap failed\n");
1282
		err = -1;
1283
		goto out_unmap2;
L
Linus Torvalds 已提交
1284 1285 1286 1287 1288
	}

	output("%s board found\n", pci_name(dev));
	displaytype = get_displaytype();

1289
	if (flatpanel)
L
Linus Torvalds 已提交
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
		nativex = get_nativex();

	fb_info.fix = tridentfb_fix;
	fb_info.fbops = &tridentfb_ops;


	fb_info.flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
#ifdef CONFIG_FB_TRIDENT_ACCEL
	fb_info.flags |= FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT;
#endif
	fb_info.pseudo_palette = pseudo_pal;

1302 1303
	if (!fb_find_mode(&default_var, &fb_info,
			  mode_option, NULL, 0, NULL, bpp)) {
1304
		err = -EINVAL;
1305
		goto out_unmap2;
1306
	}
1307 1308 1309 1310
	err = fb_alloc_cmap(&fb_info.cmap, 256, 0);
	if (err < 0)
		goto out_unmap2;

L
Linus Torvalds 已提交
1311 1312 1313 1314 1315 1316 1317 1318 1319
	if (defaultaccel && acc)
		default_var.accel_flags |= FB_ACCELF_TEXT;
	else
		default_var.accel_flags &= ~FB_ACCELF_TEXT;
	default_var.activate |= FB_ACTIVATE_NOW;
	fb_info.var = default_var;
	fb_info.device = &dev->dev;
	if (register_framebuffer(&fb_info) < 0) {
		printk(KERN_ERR "tridentfb: could not register Trident framebuffer\n");
1320
		fb_dealloc_cmap(&fb_info.cmap);
1321
		err = -EINVAL;
1322
		goto out_unmap2;
L
Linus Torvalds 已提交
1323 1324
	}
	output("fb%d: %s frame buffer device %dx%d-%dbpp\n",
1325 1326
	   fb_info.node, fb_info.fix.id, default_var.xres,
	   default_var.yres, default_var.bits_per_pixel);
L
Linus Torvalds 已提交
1327
	return 0;
1328

1329
out_unmap2:
1330 1331
	if (fb_info.screen_base)
		iounmap(fb_info.screen_base);
1332 1333 1334 1335 1336 1337
	release_mem_region(tridentfb_fix.smem_start, tridentfb_fix.smem_len);
	disable_mmio();
out_unmap1:
	if (default_par.io_virt)
		iounmap(default_par.io_virt);
	release_mem_region(tridentfb_fix.mmio_start, tridentfb_fix.mmio_len);
1338
	return err;
L
Linus Torvalds 已提交
1339 1340
}

1341
static void __devexit trident_pci_remove(struct pci_dev *dev)
L
Linus Torvalds 已提交
1342 1343 1344 1345 1346 1347
{
	struct tridentfb_par *par = (struct tridentfb_par*)fb_info.par;
	unregister_framebuffer(&fb_info);
	iounmap(par->io_virt);
	iounmap(fb_info.screen_base);
	release_mem_region(tridentfb_fix.smem_start, tridentfb_fix.smem_len);
1348
	release_mem_region(tridentfb_fix.mmio_start, tridentfb_fix.mmio_len);
L
Linus Torvalds 已提交
1349 1350 1351 1352
}

/* List of boards that we are trying to support */
static struct pci_device_id trident_devices[] = {
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
	{PCI_VENDOR_ID_TRIDENT,	BLADE3D, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_TRIDENT,	CYBERBLADEi7, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_TRIDENT,	CYBERBLADEi7D, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_TRIDENT,	CYBERBLADEi1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_TRIDENT,	CYBERBLADEi1D, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_TRIDENT,	CYBERBLADEAi1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_TRIDENT,	CYBERBLADEAi1D, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_TRIDENT,	CYBERBLADEE4, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_TRIDENT,	TGUI9660, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_TRIDENT,	IMAGE975, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_TRIDENT,	IMAGE985, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_TRIDENT,	CYBER9320, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_TRIDENT,	CYBER9388, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_TRIDENT,	CYBER9520, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_TRIDENT,	CYBER9525DVD, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_TRIDENT,	CYBER9397, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_TRIDENT,	CYBER9397DVD, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_TRIDENT,	CYBERBLADEXPAi1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_TRIDENT,	CYBERBLADEXPm8, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_TRIDENT,	CYBERBLADEXPm16, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
L
Linus Torvalds 已提交
1373
	{0,}
1374 1375 1376
};

MODULE_DEVICE_TABLE(pci, trident_devices);
L
Linus Torvalds 已提交
1377 1378

static struct pci_driver tridentfb_pci_driver = {
1379 1380 1381 1382
	.name = "tridentfb",
	.id_table = trident_devices,
	.probe = trident_pci_probe,
	.remove = __devexit_p(trident_pci_remove)
L
Linus Torvalds 已提交
1383 1384 1385 1386 1387
};

/*
 * Parse user specified options (`video=trident:')
 * example:
1388
 *	video=trident:800x600,bpp=16,noaccel
L
Linus Torvalds 已提交
1389 1390
 */
#ifndef MODULE
1391
static int __init tridentfb_setup(char *options)
L
Linus Torvalds 已提交
1392
{
1393
	char *opt;
L
Linus Torvalds 已提交
1394 1395
	if (!options || !*options)
		return 0;
1396 1397 1398 1399
	while ((opt = strsep(&options, ",")) != NULL) {
		if (!*opt)
			continue;
		if (!strncmp(opt, "noaccel", 7))
L
Linus Torvalds 已提交
1400
			noaccel = 1;
1401
		else if (!strncmp(opt, "fp", 2))
L
Linus Torvalds 已提交
1402
			displaytype = DISPLAY_FP;
1403
		else if (!strncmp(opt, "crt", 3))
L
Linus Torvalds 已提交
1404
			displaytype = DISPLAY_CRT;
1405 1406 1407
		else if (!strncmp(opt, "bpp=", 4))
			bpp = simple_strtoul(opt + 4, NULL, 0);
		else if (!strncmp(opt, "center", 6))
L
Linus Torvalds 已提交
1408
			center = 1;
1409
		else if (!strncmp(opt, "stretch", 7))
L
Linus Torvalds 已提交
1410
			stretch = 1;
1411 1412 1413 1414 1415 1416
		else if (!strncmp(opt, "memsize=", 8))
			memsize = simple_strtoul(opt + 8, NULL, 0);
		else if (!strncmp(opt, "memdiff=", 8))
			memdiff = simple_strtoul(opt + 8, NULL, 0);
		else if (!strncmp(opt, "nativex=", 8))
			nativex = simple_strtoul(opt + 8, NULL, 0);
L
Linus Torvalds 已提交
1417
		else
1418
			mode_option = opt;
L
Linus Torvalds 已提交
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
	}
	return 0;
}
#endif

static int __init tridentfb_init(void)
{
#ifndef MODULE
	char *option = NULL;

	if (fb_get_options("tridentfb", &option))
		return -ENODEV;
	tridentfb_setup(option);
#endif
	output("Trident framebuffer %s initializing\n", VERSION);
	return pci_register_driver(&tridentfb_pci_driver);
}

static void __exit tridentfb_exit(void)
{
	pci_unregister_driver(&tridentfb_pci_driver);
}

module_init(tridentfb_init);
module_exit(tridentfb_exit);

MODULE_AUTHOR("Jani Monoses <jani@iv.ro>");
MODULE_DESCRIPTION("Framebuffer driver for Trident cards");
MODULE_LICENSE("GPL");