tridentfb.c 35.5 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
 */

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

#include <linux/delay.h>
25
#include <video/vga.h>
L
Linus Torvalds 已提交
26 27
#include <video/trident.h>

28
#define VERSION		"0.7.9-NEWAPI"
L
Linus Torvalds 已提交
29 30

struct tridentfb_par {
31
	void __iomem *io_virt;	/* iospace virtual memory address */
32
	u32 pseudo_pal[16];
33
	int chip_id;
34
	int flatpanel;
35 36 37 38 39 40
	void (*init_accel) (struct tridentfb_par *, int, int);
	void (*wait_engine) (struct tridentfb_par *);
	void (*fill_rect)
		(struct tridentfb_par *par, u32, u32, u32, u32, u32, u32);
	void (*copy_rect)
		(struct tridentfb_par *par, u32, u32, u32, u32, u32, u32);
L
Linus Torvalds 已提交
41 42
};

43
static unsigned char eng_oper;	/* engine operation... */
L
Linus Torvalds 已提交
44 45 46
static struct fb_ops tridentfb_ops;

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

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

/* video mode */
57
static char *mode_option __devinitdata = "640x480";
58
static int bpp __devinitdata = 8;
L
Linus Torvalds 已提交
59

60
static int noaccel __devinitdata;
L
Linus Torvalds 已提交
61 62 63 64

static int center;
static int stretch;

65 66
static int fp __devinitdata;
static int crt __devinitdata;
L
Linus Torvalds 已提交
67

68 69
static int memsize __devinitdata;
static int memdiff __devinitdata;
L
Linus Torvalds 已提交
70 71
static int nativex;

72 73
module_param(mode_option, charp, 0);
MODULE_PARM_DESC(mode_option, "Initial video mode e.g. '648x480-8@60'");
74 75
module_param_named(mode, mode_option, charp, 0);
MODULE_PARM_DESC(mode, "Initial video mode e.g. '648x480-8@60' (deprecated)");
L
Linus Torvalds 已提交
76 77 78 79 80 81 82 83
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);
84
MODULE_PARM_DESC(fp, "Define if flatpanel is connected");
L
Linus Torvalds 已提交
85
module_param(crt, int, 0);
86
MODULE_PARM_DESC(crt, "Define if CRT is connected");
L
Linus Torvalds 已提交
87

88 89
static int is_oldclock(int id)
{
90 91
	return	(id == TGUI9440) ||
		(id == TGUI9660) ||
92 93 94 95 96
		(id == CYBER9320);
}

static int is_oldprotect(int id)
{
97 98
	return	(id == TGUI9440) ||
		(id == TGUI9660) ||
99 100 101 102
		(id == PROVIDIA9685) ||
		(id == CYBER9320) ||
		(id == CYBER9382) ||
		(id == CYBER9385);
103 104
}

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
static int is_blade(int id)
{
	return	(id == BLADE3D) ||
		(id == CYBERBLADEE4) ||
		(id == CYBERBLADEi7) ||
		(id == CYBERBLADEi7D) ||
		(id == CYBERBLADEi1) ||
		(id == CYBERBLADEi1D) ||
		(id == CYBERBLADEAi1) ||
		(id == CYBERBLADEAi1D);
}

static int is_xp(int id)
{
	return	(id == CYBERBLADEXPAi1) ||
		(id == CYBERBLADEXPm8) ||
		(id == CYBERBLADEXPm16);
}

L
Linus Torvalds 已提交
124 125
static int is3Dchip(int id)
{
126 127 128 129 130 131 132 133 134
	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 已提交
135 136 137 138 139
}

static int iscyber(int id)
{
	switch (id) {
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
	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 已提交
155

156 157
	case CYBER9320:
	case TGUI9660:
158
	case PROVIDIA9685:
159 160 161 162 163 164 165 166 167
	case IMAGE975:
	case IMAGE985:
	case BLADE3D:
	case CYBERBLADEi7:	/* VIA MPV4 integrated version */

	default:
		/* case CYBERBLDAEXPm8:  Strange */
		/* case CYBERBLDAEXPm16: Strange */
		return 0;
L
Linus Torvalds 已提交
168 169 170
	}
}

171 172 173 174
static inline void t_outb(struct tridentfb_par *p, u8 val, u16 reg)
{
	fb_writeb(val, p->io_virt + reg);
}
L
Linus Torvalds 已提交
175

176 177 178 179
static inline u8 t_inb(struct tridentfb_par *p, u16 reg)
{
	return fb_readb(p->io_virt + reg);
}
L
Linus Torvalds 已提交
180

181 182 183 184 185 186 187 188 189
static inline void writemmr(struct tridentfb_par *par, u16 r, u32 v)
{
	fb_writel(v, par->io_virt + r);
}

static inline u32 readmmr(struct tridentfb_par *par, u16 r)
{
	return fb_readl(par->io_virt + r);
}
L
Linus Torvalds 已提交
190 191 192 193 194

/*
 * Blade specific acceleration.
 */

195
#define point(x, y) ((y) << 16 | (x))
L
Linus Torvalds 已提交
196 197 198 199 200 201 202 203 204 205 206
#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

207
static void blade_init_accel(struct tridentfb_par *par, int pitch, int bpp)
L
Linus Torvalds 已提交
208
{
209 210
	int v1 = (pitch >> 3) << 20;
	int tmp = 0, v2;
L
Linus Torvalds 已提交
211
	switch (bpp) {
212 213 214 215 216 217 218 219 220 221 222 223 224
	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 已提交
225
	}
226
	v2 = v1 | (tmp << 29);
227 228 229 230 231 232 233 234 235
	writemmr(par, 0x21C0, v2);
	writemmr(par, 0x21C4, v2);
	writemmr(par, 0x21B8, v2);
	writemmr(par, 0x21BC, v2);
	writemmr(par, 0x21D0, v1);
	writemmr(par, 0x21D4, v1);
	writemmr(par, 0x21C8, v1);
	writemmr(par, 0x21CC, v1);
	writemmr(par, 0x216C, 0);
L
Linus Torvalds 已提交
236 237
}

238
static void blade_wait_engine(struct tridentfb_par *par)
L
Linus Torvalds 已提交
239
{
240
	while (readmmr(par, STA) & 0xFA800000) ;
L
Linus Torvalds 已提交
241 242
}

243 244
static void blade_fill_rect(struct tridentfb_par *par,
			    u32 x, u32 y, u32 w, u32 h, u32 c, u32 rop)
L
Linus Torvalds 已提交
245
{
246 247 248
	writemmr(par, CLR, c);
	writemmr(par, ROP, rop ? 0x66 : ROP_S);
	writemmr(par, CMD, 0x20000000 | 1 << 19 | 1 << 4 | 2 << 2);
L
Linus Torvalds 已提交
249

250 251
	writemmr(par, DR1, point(x, y));
	writemmr(par, DR2, point(x + w - 1, y + h - 1));
L
Linus Torvalds 已提交
252 253
}

254 255
static void blade_copy_rect(struct tridentfb_par *par,
			    u32 x1, u32 y1, u32 x2, u32 y2, u32 w, u32 h)
L
Linus Torvalds 已提交
256
{
257
	u32 s1, s2, d1, d2;
L
Linus Torvalds 已提交
258
	int direction = 2;
259 260 261 262
	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 已提交
263 264

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

267 268
	writemmr(par, ROP, ROP_S);
	writemmr(par, CMD, 0xE0000000 | 1 << 19 | 1 << 4 | 1 << 2 | direction);
L
Linus Torvalds 已提交
269

270 271 272 273
	writemmr(par, SR1, direction ? s2 : s1);
	writemmr(par, SR2, direction ? s1 : s2);
	writemmr(par, DR1, direction ? d2 : d1);
	writemmr(par, DR2, direction ? d1 : d2);
L
Linus Torvalds 已提交
274 275 276 277 278 279 280
}

/*
 * BladeXP specific acceleration functions
 */

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

283
static void xp_init_accel(struct tridentfb_par *par, int pitch, int bpp)
L
Linus Torvalds 已提交
284
{
285
	int tmp = 0, v1;
L
Linus Torvalds 已提交
286 287 288
	unsigned char x = 0;

	switch (bpp) {
289 290 291 292 293 294 295 296 297 298 299 300
	case 8:
		x = 0;
		break;
	case 16:
		x = 1;
		break;
	case 24:
		x = 3;
		break;
	case 32:
		x = 2;
		break;
L
Linus Torvalds 已提交
301 302 303
	}

	switch (pitch << (bpp >> 3)) {
304 305 306 307 308 309 310 311 312 313 314 315 316
	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 已提交
317 318
	}

319
	t_outb(par, x, 0x2125);
L
Linus Torvalds 已提交
320 321 322 323

	eng_oper = x | 0x40;

	switch (bpp) {
324 325 326 327 328 329 330 331 332 333 334
	case 8:
		tmp = 18;
		break;
	case 15:
	case 16:
		tmp = 19;
		break;
	case 24:
	case 32:
		tmp = 20;
		break;
L
Linus Torvalds 已提交
335 336 337 338
	}

	v1 = pitch << tmp;

339 340 341
	writemmr(par, 0x2154, v1);
	writemmr(par, 0x2150, v1);
	t_outb(par, 3, 0x2126);
L
Linus Torvalds 已提交
342 343
}

344
static void xp_wait_engine(struct tridentfb_par *par)
L
Linus Torvalds 已提交
345 346 347 348 349 350 351
{
	int busy;
	int count, timeout;

	count = 0;
	timeout = 0;
	for (;;) {
352
		busy = t_inb(par, STA) & 0x80;
L
Linus Torvalds 已提交
353 354 355 356 357 358 359 360 361
		if (busy != 0x80)
			return;
		count++;
		if (count == 10000000) {
			/* Timeout */
			count = 9990000;
			timeout++;
			if (timeout == 8) {
				/* Reset engine */
362
				t_outb(par, 0x00, 0x2120);
L
Linus Torvalds 已提交
363 364 365 366 367 368
				return;
			}
		}
	}
}

369 370
static void xp_fill_rect(struct tridentfb_par *par,
			 u32 x, u32 y, u32 w, u32 h, u32 c, u32 rop)
L
Linus Torvalds 已提交
371
{
372 373 374 375 376 377 378
	writemmr(par, 0x2127, ROP_P);
	writemmr(par, 0x2158, c);
	writemmr(par, 0x2128, 0x4000);
	writemmr(par, 0x2140, masked_point(h, w));
	writemmr(par, 0x2138, masked_point(y, x));
	t_outb(par, 0x01, 0x2124);
	t_outb(par, eng_oper, 0x2125);
L
Linus Torvalds 已提交
379 380
}

381 382
static void xp_copy_rect(struct tridentfb_par *par,
			 u32 x1, u32 y1, u32 x2, u32 y2, u32 w, u32 h)
L
Linus Torvalds 已提交
383 384
{
	int direction;
385
	u32 x1_tmp, x2_tmp, y1_tmp, y2_tmp;
L
Linus Torvalds 已提交
386 387

	direction = 0x0004;
388

L
Linus Torvalds 已提交
389 390 391 392 393 394 395 396
	if ((x1 < x2) && (y1 == y2)) {
		direction |= 0x0200;
		x1_tmp = x1 + w - 1;
		x2_tmp = x2 + w - 1;
	} else {
		x1_tmp = x1;
		x2_tmp = x2;
	}
397

L
Linus Torvalds 已提交
398 399 400 401
	if (y1 < y2) {
		direction |= 0x0100;
		y1_tmp = y1 + h - 1;
		y2_tmp = y2 + h - 1;
402
	} else {
L
Linus Torvalds 已提交
403 404 405 406
		y1_tmp = y1;
		y2_tmp = y2;
	}

407 408 409 410 411 412
	writemmr(par, 0x2128, direction);
	t_outb(par, ROP_S, 0x2127);
	writemmr(par, 0x213C, masked_point(y1_tmp, x1_tmp));
	writemmr(par, 0x2138, masked_point(y2_tmp, x2_tmp));
	writemmr(par, 0x2140, masked_point(h, w));
	t_outb(par, 0x01, 0x2124);
L
Linus Torvalds 已提交
413 414 415 416 417
}

/*
 * Image specific acceleration functions
 */
418
static void image_init_accel(struct tridentfb_par *par, int pitch, int bpp)
L
Linus Torvalds 已提交
419 420
{
	int tmp = 0;
421 422 423 424 425 426 427 428 429 430 431 432 433 434
	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 已提交
435
	}
436 437 438 439 440 441 442 443 444 445 446 447 448
	writemmr(par, 0x2120, 0xF0000000);
	writemmr(par, 0x2120, 0x40000000 | tmp);
	writemmr(par, 0x2120, 0x80000000);
	writemmr(par, 0x2144, 0x00000000);
	writemmr(par, 0x2148, 0x00000000);
	writemmr(par, 0x2150, 0x00000000);
	writemmr(par, 0x2154, 0x00000000);
	writemmr(par, 0x2120, 0x60000000 | (pitch << 16) | pitch);
	writemmr(par, 0x216C, 0x00000000);
	writemmr(par, 0x2170, 0x00000000);
	writemmr(par, 0x217C, 0x00000000);
	writemmr(par, 0x2120, 0x10000000);
	writemmr(par, 0x2130, (2047 << 16) | 2047);
L
Linus Torvalds 已提交
449 450
}

451
static void image_wait_engine(struct tridentfb_par *par)
L
Linus Torvalds 已提交
452
{
453
	while (readmmr(par, 0x2164) & 0xF0000000) ;
L
Linus Torvalds 已提交
454 455
}

456 457
static void image_fill_rect(struct tridentfb_par *par,
			    u32 x, u32 y, u32 w, u32 h, u32 c, u32 rop)
L
Linus Torvalds 已提交
458
{
459 460
	writemmr(par, 0x2120, 0x80000000);
	writemmr(par, 0x2120, 0x90000000 | ROP_S);
L
Linus Torvalds 已提交
461

462
	writemmr(par, 0x2144, c);
L
Linus Torvalds 已提交
463

464 465
	writemmr(par, DR1, point(x, y));
	writemmr(par, DR2, point(x + w - 1, y + h - 1));
L
Linus Torvalds 已提交
466

467
	writemmr(par, 0x2124, 0x80000000 | 3 << 22 | 1 << 10 | 1 << 9);
L
Linus Torvalds 已提交
468 469
}

470 471
static void image_copy_rect(struct tridentfb_par *par,
			    u32 x1, u32 y1, u32 x2, u32 y2, u32 w, u32 h)
L
Linus Torvalds 已提交
472
{
473
	u32 s1, s2, d1, d2;
L
Linus Torvalds 已提交
474
	int direction = 2;
475 476 477 478
	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 已提交
479

480 481 482
	if ((y1 > y2) || ((y1 == y2) && (x1 > x2)))
		direction = 0;

483 484
	writemmr(par, 0x2120, 0x80000000);
	writemmr(par, 0x2120, 0x90000000 | ROP_S);
485

486 487 488 489 490 491
	writemmr(par, SR1, direction ? s2 : s1);
	writemmr(par, SR2, direction ? s1 : s2);
	writemmr(par, DR1, direction ? d2 : d1);
	writemmr(par, DR2, direction ? d1 : d2);
	writemmr(par, 0x2124,
		 0x80000000 | 1 << 22 | 1 << 10 | 1 << 7 | direction);
492
}
L
Linus Torvalds 已提交
493 494 495 496 497

/*
 * Accel functions called by the upper layers
 */
#ifdef CONFIG_FB_TRIDENT_ACCEL
498 499
static void tridentfb_fillrect(struct fb_info *info,
			       const struct fb_fillrect *fr)
L
Linus Torvalds 已提交
500
{
501
	struct tridentfb_par *par = info->par;
L
Linus Torvalds 已提交
502
	int bpp = info->var.bits_per_pixel;
503
	int col = 0;
504

L
Linus Torvalds 已提交
505
	switch (bpp) {
506 507 508 509 510 511 512 513 514 515 516 517 518 519
	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;
	}

520
	par->fill_rect(par, fr->dx, fr->dy, fr->width,
521
		       fr->height, col, fr->rop);
522
	par->wait_engine(par);
L
Linus Torvalds 已提交
523
}
524 525
static void tridentfb_copyarea(struct fb_info *info,
			       const struct fb_copyarea *ca)
L
Linus Torvalds 已提交
526
{
527 528
	struct tridentfb_par *par = info->par;

529
	par->copy_rect(par, ca->sx, ca->sy, ca->dx, ca->dy,
530
		       ca->width, ca->height);
531
	par->wait_engine(par);
L
Linus Torvalds 已提交
532 533 534 535 536 537 538 539 540 541 542
}
#else /* !CONFIG_FB_TRIDENT_ACCEL */
#define tridentfb_fillrect cfb_fillrect
#define tridentfb_copyarea cfb_copyarea
#endif /* CONFIG_FB_TRIDENT_ACCEL */


/*
 * Hardware access functions
 */

543
static inline unsigned char read3X4(struct tridentfb_par *par, int reg)
L
Linus Torvalds 已提交
544
{
545
	return vga_mm_rcrt(par->io_virt, reg);
L
Linus Torvalds 已提交
546 547
}

548 549
static inline void write3X4(struct tridentfb_par *par, int reg,
			    unsigned char val)
L
Linus Torvalds 已提交
550
{
551
	vga_mm_wcrt(par->io_virt, reg, val);
L
Linus Torvalds 已提交
552 553
}

554 555
static inline unsigned char read3CE(struct tridentfb_par *par,
				    unsigned char reg)
L
Linus Torvalds 已提交
556
{
557
	return vga_mm_rgfx(par->io_virt, reg);
L
Linus Torvalds 已提交
558 559
}

560 561
static inline void writeAttr(struct tridentfb_par *par, int reg,
			     unsigned char val)
L
Linus Torvalds 已提交
562
{
563 564
	fb_readb(par->io_virt + VGA_IS1_RC);	/* flip-flop to index */
	vga_mm_wattr(par->io_virt, reg, val);
L
Linus Torvalds 已提交
565 566
}

567 568
static inline void write3CE(struct tridentfb_par *par, int reg,
			    unsigned char val)
L
Linus Torvalds 已提交
569
{
570
	vga_mm_wgfx(par->io_virt, reg, val);
L
Linus Torvalds 已提交
571 572
}

573
static void enable_mmio(void)
L
Linus Torvalds 已提交
574 575
{
	/* Goto New Mode */
576
	vga_io_rseq(0x0B);
L
Linus Torvalds 已提交
577 578

	/* Unprotect registers */
579
	vga_io_wseq(NewMode1, 0x80);
580

L
Linus Torvalds 已提交
581
	/* Enable MMIO */
582
	outb(PCIReg, 0x3D4);
L
Linus Torvalds 已提交
583
	outb(inb(0x3D5) | 0x01, 0x3D5);
584 585
}

586
static void disable_mmio(struct tridentfb_par *par)
587 588
{
	/* Goto New Mode */
589
	vga_mm_rseq(par->io_virt, 0x0B);
590 591

	/* Unprotect registers */
592
	vga_mm_wseq(par->io_virt, NewMode1, 0x80);
593 594

	/* Disable MMIO */
595 596
	t_outb(par, PCIReg, 0x3D4);
	t_outb(par, t_inb(par, 0x3D5) & ~0x01, 0x3D5);
L
Linus Torvalds 已提交
597 598
}

599 600
static void crtc_unlock(struct tridentfb_par *par)
{
601 602
	write3X4(par, VGA_CRTC_V_SYNC_END,
		 read3X4(par, VGA_CRTC_V_SYNC_END) & 0x7F);
603
}
L
Linus Torvalds 已提交
604 605

/*  Return flat panel's maximum x resolution */
606
static int __devinit get_nativex(struct tridentfb_par *par)
L
Linus Torvalds 已提交
607
{
608
	int x, y, tmp;
L
Linus Torvalds 已提交
609 610 611 612

	if (nativex)
		return nativex;

613
	tmp = (read3CE(par, VertStretch) >> 4) & 3;
L
Linus Torvalds 已提交
614 615

	switch (tmp) {
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
	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 已提交
632 633 634 635 636 637 638
	}

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

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

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

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

/* Address of first shown pixel in display memory */
665
static void set_screen_start(struct tridentfb_par *par, int base)
L
Linus Torvalds 已提交
666
{
667
	u8 tmp;
668 669
	write3X4(par, VGA_CRTC_START_LO, base & 0xFF);
	write3X4(par, VGA_CRTC_START_HI, (base & 0xFF00) >> 8);
670 671 672 673
	tmp = read3X4(par, CRTCModuleTest) & 0xDF;
	write3X4(par, CRTCModuleTest, tmp | ((base & 0x10000) >> 11));
	tmp = read3X4(par, CRTHiOrd) & 0xF8;
	write3X4(par, CRTHiOrd, tmp | ((base & 0xE0000) >> 17));
L
Linus Torvalds 已提交
674 675 676
}

/* Set dotclock frequency */
677
static void set_vclk(struct tridentfb_par *par, unsigned long freq)
L
Linus Torvalds 已提交
678
{
679
	int m, n, k;
680 681 682
	unsigned long fi, d, di;
	unsigned char best_m = 0, best_n = 0, best_k = 0;
	unsigned char hi, lo;
L
Linus Torvalds 已提交
683

684
	d = 20000;
685 686 687
	for (k = 1; k >= 0; k--)
		for (m = 0; m < 32; m++)
			for (n = 0; n < 122; n++) {
688
				fi = ((14318l * (n + 8)) / (m + 2)) >> k;
689 690
				if ((di = abs(fi - freq)) < d) {
					d = di;
691 692 693
					best_n = n;
					best_m = m;
					best_k = k;
694
				}
695 696
				if (fi > freq)
					break;
697
			}
698 699 700 701 702 703 704 705 706

	if (is_oldclock(par->chip_id)) {
		lo = best_n | (best_m << 7);
		hi = (best_m >> 1) | (best_k << 4);
	} else {
		lo = best_n;
		hi = best_m | (best_k << 6);
	}

707
	if (is3Dchip(par->chip_id)) {
708 709
		vga_mm_wseq(par->io_virt, ClockHigh, hi);
		vga_mm_wseq(par->io_virt, ClockLow, lo);
L
Linus Torvalds 已提交
710
	} else {
711 712
		t_outb(par, lo, 0x43C8);
		t_outb(par, hi, 0x43C9);
L
Linus Torvalds 已提交
713
	}
714
	debug("VCLK = %X %X\n", hi, lo);
L
Linus Torvalds 已提交
715 716 717
}

/* Set number of lines for flat panels*/
718
static void set_number_of_lines(struct tridentfb_par *par, int lines)
L
Linus Torvalds 已提交
719
{
720
	int tmp = read3CE(par, CyberEnhance) & 0x8F;
L
Linus Torvalds 已提交
721 722 723 724 725 726 727 728
	if (lines > 1024)
		tmp |= 0x50;
	else if (lines > 768)
		tmp |= 0x30;
	else if (lines > 600)
		tmp |= 0x20;
	else if (lines > 480)
		tmp |= 0x10;
729
	write3CE(par, CyberEnhance, tmp);
L
Linus Torvalds 已提交
730 731 732 733
}

/*
 * If we see that FP is active we assume we have one.
734
 * Otherwise we have a CRT display. User can override.
L
Linus Torvalds 已提交
735
 */
736
static int __devinit is_flatpanel(struct tridentfb_par *par)
L
Linus Torvalds 已提交
737 738
{
	if (fp)
739
		return 1;
740
	if (crt || !iscyber(par->chip_id))
741 742
		return 0;
	return (read3CE(par, FPConfig) & 0x10) ? 1 : 0;
L
Linus Torvalds 已提交
743 744 745
}

/* Try detecting the video memory size */
746
static unsigned int __devinit get_memsize(struct tridentfb_par *par)
L
Linus Torvalds 已提交
747 748 749 750 751 752 753 754
{
	unsigned char tmp, tmp2;
	unsigned int k;

	/* If memory size provided by user */
	if (memsize)
		k = memsize * Kb;
	else
755
		switch (par->chip_id) {
756 757 758
		case CYBER9525DVD:
			k = 2560 * Kb;
			break;
L
Linus Torvalds 已提交
759
		default:
760
			tmp = read3X4(par, SPR) & 0x0F;
L
Linus Torvalds 已提交
761 762
			switch (tmp) {

763
			case 0x01:
764
				k = 512 * Kb;
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
				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 */

792
				tmp2 = vga_mm_rseq(par->io_virt, 0xC1);
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
				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 已提交
817 818
				break;
			}
819
		}
L
Linus Torvalds 已提交
820 821

	k -= memdiff * Kb;
822
	output("framebuffer size = %d Kb\n", k / Kb);
L
Linus Torvalds 已提交
823 824 825 826
	return k;
}

/* See if we can handle the video mode described in var */
827 828
static int tridentfb_check_var(struct fb_var_screeninfo *var,
			       struct fb_info *info)
L
Linus Torvalds 已提交
829
{
830
	struct tridentfb_par *par = info->par;
L
Linus Torvalds 已提交
831 832 833 834
	int bpp = var->bits_per_pixel;
	debug("enter\n");

	/* check color depth */
835
	if (bpp == 24)
L
Linus Torvalds 已提交
836
		bpp = var->bits_per_pixel = 32;
837
	/* check whether resolution fits on panel and in memory */
838
	if (par->flatpanel && nativex && var->xres > nativex)
L
Linus Torvalds 已提交
839
		return -EINVAL;
840
	if (var->xres * var->yres_virtual * bpp / 8 > info->fix.smem_len)
L
Linus Torvalds 已提交
841 842 843
		return -EINVAL;

	switch (bpp) {
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
	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 已提交
870 871 872 873 874 875
	}
	debug("exit\n");

	return 0;

}
876

L
Linus Torvalds 已提交
877 878
/* Pan the display */
static int tridentfb_pan_display(struct fb_var_screeninfo *var,
879
				 struct fb_info *info)
L
Linus Torvalds 已提交
880
{
881
	struct tridentfb_par *par = info->par;
L
Linus Torvalds 已提交
882 883 884 885
	unsigned int offset;

	debug("enter\n");
	offset = (var->xoffset + (var->yoffset * var->xres))
886
		* var->bits_per_pixel / 32;
L
Linus Torvalds 已提交
887 888
	info->var.xoffset = var->xoffset;
	info->var.yoffset = var->yoffset;
889
	set_screen_start(par, offset);
L
Linus Torvalds 已提交
890 891 892 893
	debug("exit\n");
	return 0;
}

894 895 896 897 898 899 900 901 902
static void shadowmode_on(struct tridentfb_par *par)
{
	write3CE(par, CyberControl, read3CE(par, CyberControl) | 0x81);
}

static void shadowmode_off(struct tridentfb_par *par)
{
	write3CE(par, CyberControl, read3CE(par, CyberControl) & 0x7E);
}
L
Linus Torvalds 已提交
903 904 905 906

/* Set the hardware to the requested video mode */
static int tridentfb_set_par(struct fb_info *info)
{
907 908 909 910
	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 已提交
911 912
	int bpp = var->bits_per_pixel;
	unsigned char tmp;
913 914
	unsigned long vclk;

L
Linus Torvalds 已提交
915
	debug("enter\n");
916
	hdispend = var->xres / 8 - 1;
917 918 919 920
	hsyncstart = (var->xres + var->right_margin) / 8 - 1;
	hsyncend = (var->xres + var->right_margin + var->hsync_len) / 8 - 1;
	htotal = (var->xres + var->left_margin + var->right_margin +
		  var->hsync_len) / 8 - 5;
921
	hblankstart = hdispend + 1;
922
	hblankend = htotal + 3;
L
Linus Torvalds 已提交
923 924 925

	vdispend = var->yres - 1;
	vsyncstart = var->yres + var->lower_margin;
926 927
	vsyncend = vsyncstart + var->vsync_len;
	vtotal = var->upper_margin + vsyncend - 2;
928
	vblankstart = vdispend + 1;
929
	vblankend = vtotal;
L
Linus Torvalds 已提交
930

931 932
	crtc_unlock(par);
	write3CE(par, CyberControl, 8);
L
Linus Torvalds 已提交
933

934
	if (par->flatpanel && var->xres < nativex) {
L
Linus Torvalds 已提交
935 936 937 938 939
		/*
		 * on flat panels with native size larger
		 * than requested resolution decide whether
		 * we stretch or center
		 */
940
		t_outb(par, 0xEB, VGA_MIS_W);
L
Linus Torvalds 已提交
941

942
		shadowmode_on(par);
L
Linus Torvalds 已提交
943

944
		if (center)
945
			screen_center(par);
L
Linus Torvalds 已提交
946
		else if (stretch)
947
			screen_stretch(par);
L
Linus Torvalds 已提交
948 949

	} else {
950
		t_outb(par, 0x2B, VGA_MIS_W);
951
		write3CE(par, CyberControl, 8);
L
Linus Torvalds 已提交
952 953 954
	}

	/* vertical timing values */
955 956 957 958 959
	write3X4(par, VGA_CRTC_V_TOTAL, vtotal & 0xFF);
	write3X4(par, VGA_CRTC_V_DISP_END, vdispend & 0xFF);
	write3X4(par, VGA_CRTC_V_SYNC_START, vsyncstart & 0xFF);
	write3X4(par, VGA_CRTC_V_SYNC_END, (vsyncend & 0x0F));
	write3X4(par, VGA_CRTC_V_BLANK_START, vblankstart & 0xFF);
960
	write3X4(par, VGA_CRTC_V_BLANK_END, vblankend & 0xFF);
L
Linus Torvalds 已提交
961 962

	/* horizontal timing values */
963 964 965 966
	write3X4(par, VGA_CRTC_H_TOTAL, htotal & 0xFF);
	write3X4(par, VGA_CRTC_H_DISP, hdispend & 0xFF);
	write3X4(par, VGA_CRTC_H_SYNC_START, hsyncstart & 0xFF);
	write3X4(par, VGA_CRTC_H_SYNC_END,
967
		 (hsyncend & 0x1F) | ((hblankend & 0x20) << 2));
968
	write3X4(par, VGA_CRTC_H_BLANK_START, hblankstart & 0xFF);
969
	write3X4(par, VGA_CRTC_H_BLANK_END, hblankend & 0x1F);
L
Linus Torvalds 已提交
970 971 972 973 974 975 976 977 978 979 980

	/* 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;
981
	write3X4(par, VGA_CRTC_OVERFLOW, tmp);
L
Linus Torvalds 已提交
982

983 984
	tmp = read3X4(par, CRTHiOrd) & 0x07;
	tmp |= 0x08;	/* line compare bit 10 */
L
Linus Torvalds 已提交
985 986 987 988
	if (vtotal & 0x400) tmp |= 0x80;
	if (vblankstart & 0x400) tmp |= 0x40;
	if (vsyncstart & 0x400) tmp |= 0x20;
	if (vdispend & 0x400) tmp |= 0x10;
989
	write3X4(par, CRTHiOrd, tmp);
L
Linus Torvalds 已提交
990

991 992 993 994
	tmp = (htotal >> 8) & 0x01;
	tmp |= (hdispend >> 7) & 0x02;
	tmp |= (hsyncstart >> 5) & 0x08;
	tmp |= (hblankstart >> 4) & 0x10;
995
	write3X4(par, HorizOverflow, tmp);
996

L
Linus Torvalds 已提交
997 998
	tmp = 0x40;
	if (vblankstart & 0x200) tmp |= 0x20;
999
//FIXME	if (info->var.vmode & FB_VMODE_DOUBLE) tmp |= 0x80;  /* double scan for 200 line modes */
1000
	write3X4(par, VGA_CRTC_MAX_SCAN, tmp);
L
Linus Torvalds 已提交
1001

1002 1003 1004
	write3X4(par, VGA_CRTC_LINE_COMPARE, 0xFF);
	write3X4(par, VGA_CRTC_PRESET_ROW, 0);
	write3X4(par, VGA_CRTC_MODE, 0xC3);
L
Linus Torvalds 已提交
1005

1006
	write3X4(par, LinearAddReg, 0x20);	/* enable linear addressing */
L
Linus Torvalds 已提交
1007

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

1012 1013
	/* enable GE for text acceleration */
	write3X4(par, GraphEngReg, 0x80);
L
Linus Torvalds 已提交
1014

1015
#ifdef CONFIG_FB_TRIDENT_ACCEL
1016
	par->init_accel(par, info->var.xres, bpp);
1017
#endif
1018

L
Linus Torvalds 已提交
1019
	switch (bpp) {
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
	case 8:
		tmp = 0x00;
		break;
	case 16:
		tmp = 0x05;
		break;
	case 24:
		tmp = 0x29;
		break;
	case 32:
		tmp = 0x09;
		break;
L
Linus Torvalds 已提交
1032 1033
	}

1034
	write3X4(par, PixelBusReg, tmp);
L
Linus Torvalds 已提交
1035

1036 1037 1038
	tmp = read3X4(par, DRAMControl);
	if (!is_oldprotect(par->chip_id))
		tmp |= 0x10;
1039
	if (iscyber(par->chip_id))
1040
		tmp |= 0x20;
1041
	write3X4(par, DRAMControl, tmp);	/* both IO, linear enable */
L
Linus Torvalds 已提交
1042

1043
	write3X4(par, InterfaceSel, read3X4(par, InterfaceSel) | 0x40);
1044 1045
	if (!is_xp(par->chip_id))
		write3X4(par, Performance, read3X4(par, Performance) | 0x10);
1046
	/* MMIO & PCI read and write burst enable */
1047 1048
	if (par->chip_id != TGUI9440)
		write3X4(par, PCIReg, read3X4(par, PCIReg) | 0x06);
L
Linus Torvalds 已提交
1049

1050 1051
	/* convert from picoseconds to kHz */
	vclk = PICOS2KHZ(info->var.pixclock);
L
Linus Torvalds 已提交
1052
	if (bpp == 32)
1053
		vclk *= 2;
1054
	set_vclk(par, vclk);
L
Linus Torvalds 已提交
1055

1056 1057
	vga_mm_wseq(par->io_virt, 0, 3);
	vga_mm_wseq(par->io_virt, 1, 1); /* set char clock 8 dots wide */
1058
	/* enable 4 maps because needed in chain4 mode */
1059 1060 1061
	vga_mm_wseq(par->io_virt, 2, 0x0F);
	vga_mm_wseq(par->io_virt, 3, 0);
	vga_mm_wseq(par->io_virt, 4, 0x0E); /* memory mode enable bitmaps ?? */
L
Linus Torvalds 已提交
1062

1063 1064 1065 1066 1067
	/* divide clock by 2 if 32bpp chain4 mode display and CPU path */
	write3CE(par, MiscExtFunc, (bpp == 32) ? 0x1A : 0x12);
	write3CE(par, 0x5, 0x40);	/* no CGA compat, allow 256 col */
	write3CE(par, 0x6, 0x05);	/* graphics mode */
	write3CE(par, 0x7, 0x0F);	/* planes? */
L
Linus Torvalds 已提交
1068

1069
	if (par->chip_id == CYBERBLADEXPAi1) {
L
Linus Torvalds 已提交
1070
		/* This fixes snow-effect in 32 bpp */
1071
		write3X4(par, VGA_CRTC_H_SYNC_START, 0x84);
L
Linus Torvalds 已提交
1072 1073
	}

1074 1075 1076 1077
	/* graphics mode and support 256 color modes */
	writeAttr(par, 0x10, 0x41);
	writeAttr(par, 0x12, 0x0F);	/* planes */
	writeAttr(par, 0x13, 0);	/* horizontal pel panning */
L
Linus Torvalds 已提交
1078

1079 1080
	/* colors */
	for (tmp = 0; tmp < 0x10; tmp++)
1081
		writeAttr(par, tmp, tmp);
1082 1083
	fb_readb(par->io_virt + VGA_IS1_RC);	/* flip-flop to index */
	t_outb(par, 0x20, VGA_ATT_W);		/* enable attr */
L
Linus Torvalds 已提交
1084 1085

	switch (bpp) {
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
	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 已提交
1099 1100
	}

1101 1102 1103 1104 1105 1106 1107
	t_inb(par, VGA_PEL_IW);
	t_inb(par, VGA_PEL_MSK);
	t_inb(par, VGA_PEL_MSK);
	t_inb(par, VGA_PEL_MSK);
	t_inb(par, VGA_PEL_MSK);
	t_outb(par, tmp, VGA_PEL_MSK);
	t_inb(par, VGA_PEL_IW);
L
Linus Torvalds 已提交
1108

1109
	if (par->flatpanel)
1110 1111
		set_number_of_lines(par, info->var.yres);
	set_lwidth(par, info->var.xres * bpp / (4 * 16));
L
Linus Torvalds 已提交
1112
	info->fix.visual = (bpp == 8) ? FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
1113 1114
	info->fix.line_length = info->var.xres * (bpp >> 3);
	info->cmap.len = (bpp == 8) ? 256 : 16;
L
Linus Torvalds 已提交
1115 1116 1117 1118 1119 1120
	debug("exit\n");
	return 0;
}

/* Set one color register */
static int tridentfb_setcolreg(unsigned regno, unsigned red, unsigned green,
1121 1122
			       unsigned blue, unsigned transp,
			       struct fb_info *info)
L
Linus Torvalds 已提交
1123 1124
{
	int bpp = info->var.bits_per_pixel;
1125
	struct tridentfb_par *par = info->par;
L
Linus Torvalds 已提交
1126 1127 1128 1129

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

1130
	if (bpp == 8) {
1131 1132
		t_outb(par, 0xFF, VGA_PEL_MSK);
		t_outb(par, regno, VGA_PEL_IW);
L
Linus Torvalds 已提交
1133

1134 1135 1136
		t_outb(par, red >> 10, VGA_PEL_D);
		t_outb(par, green >> 10, VGA_PEL_D);
		t_outb(par, blue >> 10, VGA_PEL_D);
L
Linus Torvalds 已提交
1137

1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
	} 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] =
1148 1149
				((transp & 0xFF00) << 16)	|
				((red & 0xFF00) << 8)		|
1150
				((green & 0xFF00))		|
1151
				((blue & 0xFF00) >> 8);
1152
	}
L
Linus Torvalds 已提交
1153

1154
/* 	debug("exit\n"); */
L
Linus Torvalds 已提交
1155 1156 1157 1158 1159 1160
	return 0;
}

/* Try blanking the screen.For flat panels it does nothing */
static int tridentfb_blank(int blank_mode, struct fb_info *info)
{
1161
	unsigned char PMCont, DPMSCont;
1162
	struct tridentfb_par *par = info->par;
L
Linus Torvalds 已提交
1163 1164

	debug("enter\n");
1165
	if (par->flatpanel)
L
Linus Torvalds 已提交
1166
		return 0;
1167 1168 1169
	t_outb(par, 0x04, 0x83C8); /* Read DPMS Control */
	PMCont = t_inb(par, 0x83C6) & 0xFC;
	DPMSCont = read3CE(par, PowerStatus) & 0xFC;
1170
	switch (blank_mode) {
L
Linus Torvalds 已提交
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
	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;
1193
	}
L
Linus Torvalds 已提交
1194

1195 1196 1197
	write3CE(par, PowerStatus, DPMSCont);
	t_outb(par, 4, 0x83C8);
	t_outb(par, PMCont, 0x83C6);
L
Linus Torvalds 已提交
1198 1199 1200 1201 1202 1203 1204

	debug("exit\n");

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

1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
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,
};

1217 1218
static int __devinit trident_pci_probe(struct pci_dev *dev,
				       const struct pci_device_id *id)
L
Linus Torvalds 已提交
1219 1220 1221
{
	int err;
	unsigned char revision;
1222 1223
	struct fb_info *info;
	struct tridentfb_par *default_par;
1224 1225 1226
	int defaultaccel;
	int chip3D;
	int chip_id;
L
Linus Torvalds 已提交
1227 1228 1229 1230 1231

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

1232 1233 1234 1235 1236
	info = framebuffer_alloc(sizeof(struct tridentfb_par), &dev->dev);
	if (!info)
		return -ENOMEM;
	default_par = info->par;

L
Linus Torvalds 已提交
1237 1238
	chip_id = id->device;

1239
	if (chip_id == CYBERBLADEi1)
1240 1241 1242 1243
		output("*** Please do use cyblafb, Cyberblade/i1 support "
		       "will soon be removed from tridentfb!\n");


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

L
Linus Torvalds 已提交
1246
	if (chip_id == TGUI9660) {
1247
		revision = vga_io_rseq(RevisionID);
1248

L
Linus Torvalds 已提交
1249
		switch (revision) {
1250 1251 1252
		case 0x21:
			chip_id = PROVIDIA9685;
			break;
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
		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 已提交
1277 1278 1279 1280 1281 1282
		}
	}

	chip3D = is3Dchip(chip_id);

	if (is_xp(chip_id)) {
1283 1284 1285 1286
		default_par->init_accel = xp_init_accel;
		default_par->wait_engine = xp_wait_engine;
		default_par->fill_rect = xp_fill_rect;
		default_par->copy_rect = xp_copy_rect;
1287
	} else if (is_blade(chip_id)) {
1288 1289 1290 1291
		default_par->init_accel = blade_init_accel;
		default_par->wait_engine = blade_wait_engine;
		default_par->fill_rect = blade_fill_rect;
		default_par->copy_rect = blade_copy_rect;
L
Linus Torvalds 已提交
1292
	} else {
1293 1294 1295 1296
		default_par->init_accel = image_init_accel;
		default_par->wait_engine = image_wait_engine;
		default_par->fill_rect = image_fill_rect;
		default_par->copy_rect = image_copy_rect;
L
Linus Torvalds 已提交
1297 1298
	}

1299 1300
	default_par->chip_id = chip_id;

L
Linus Torvalds 已提交
1301 1302 1303 1304
	/* acceleration is on by default for 3D chips */
	defaultaccel = chip3D && !noaccel;

	/* setup MMIO region */
1305 1306
	tridentfb_fix.mmio_start = pci_resource_start(dev, 1);
	tridentfb_fix.mmio_len = chip3D ? 0x20000 : 0x10000;
L
Linus Torvalds 已提交
1307 1308 1309

	if (!request_mem_region(tridentfb_fix.mmio_start, tridentfb_fix.mmio_len, "tridentfb")) {
		debug("request_region failed!\n");
1310
		framebuffer_release(info);
L
Linus Torvalds 已提交
1311 1312 1313
		return -1;
	}

1314 1315
	default_par->io_virt = ioremap_nocache(tridentfb_fix.mmio_start,
					       tridentfb_fix.mmio_len);
L
Linus Torvalds 已提交
1316

1317
	if (!default_par->io_virt) {
L
Linus Torvalds 已提交
1318
		debug("ioremap failed\n");
1319 1320
		err = -1;
		goto out_unmap1;
L
Linus Torvalds 已提交
1321 1322 1323
	}

	/* setup framebuffer memory */
1324
	tridentfb_fix.smem_start = pci_resource_start(dev, 0);
1325
	tridentfb_fix.smem_len = get_memsize(default_par);
1326

L
Linus Torvalds 已提交
1327 1328
	if (!request_mem_region(tridentfb_fix.smem_start, tridentfb_fix.smem_len, "tridentfb")) {
		debug("request_mem_region failed!\n");
1329
		disable_mmio(info->par);
1330
		err = -1;
1331
		goto out_unmap1;
L
Linus Torvalds 已提交
1332 1333
	}

1334 1335
	enable_mmio();

1336 1337
	info->screen_base = ioremap_nocache(tridentfb_fix.smem_start,
					    tridentfb_fix.smem_len);
L
Linus Torvalds 已提交
1338

1339
	if (!info->screen_base) {
L
Linus Torvalds 已提交
1340
		debug("ioremap failed\n");
1341
		err = -1;
1342
		goto out_unmap2;
L
Linus Torvalds 已提交
1343 1344 1345
	}

	output("%s board found\n", pci_name(dev));
1346
	default_par->flatpanel = is_flatpanel(default_par);
L
Linus Torvalds 已提交
1347

1348
	if (default_par->flatpanel)
1349
		nativex = get_nativex(default_par);
L
Linus Torvalds 已提交
1350

1351 1352
	info->fix = tridentfb_fix;
	info->fbops = &tridentfb_ops;
1353
	info->pseudo_palette = default_par->pseudo_pal;
L
Linus Torvalds 已提交
1354

1355
	info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
L
Linus Torvalds 已提交
1356
#ifdef CONFIG_FB_TRIDENT_ACCEL
1357
	info->flags |= FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT;
L
Linus Torvalds 已提交
1358
#endif
1359
	if (!fb_find_mode(&info->var, info,
1360
			  mode_option, NULL, 0, NULL, bpp)) {
1361
		err = -EINVAL;
1362
		goto out_unmap2;
1363
	}
1364
	err = fb_alloc_cmap(&info->cmap, 256, 0);
1365 1366 1367
	if (err < 0)
		goto out_unmap2;

1368
	if (defaultaccel && default_par->init_accel)
1369
		info->var.accel_flags |= FB_ACCELF_TEXT;
L
Linus Torvalds 已提交
1370
	else
1371 1372
		info->var.accel_flags &= ~FB_ACCELF_TEXT;
	info->var.activate |= FB_ACTIVATE_NOW;
1373 1374
	info->device = &dev->dev;
	if (register_framebuffer(info) < 0) {
L
Linus Torvalds 已提交
1375
		printk(KERN_ERR "tridentfb: could not register Trident framebuffer\n");
1376
		fb_dealloc_cmap(&info->cmap);
1377
		err = -EINVAL;
1378
		goto out_unmap2;
L
Linus Torvalds 已提交
1379 1380
	}
	output("fb%d: %s frame buffer device %dx%d-%dbpp\n",
1381 1382
	   info->node, info->fix.id, info->var.xres,
	   info->var.yres, info->var.bits_per_pixel);
1383 1384

	pci_set_drvdata(dev, info);
L
Linus Torvalds 已提交
1385
	return 0;
1386

1387
out_unmap2:
1388 1389
	if (info->screen_base)
		iounmap(info->screen_base);
1390
	release_mem_region(tridentfb_fix.smem_start, tridentfb_fix.smem_len);
1391
	disable_mmio(info->par);
1392
out_unmap1:
1393 1394
	if (default_par->io_virt)
		iounmap(default_par->io_virt);
1395
	release_mem_region(tridentfb_fix.mmio_start, tridentfb_fix.mmio_len);
1396
	framebuffer_release(info);
1397
	return err;
L
Linus Torvalds 已提交
1398 1399
}

1400
static void __devexit trident_pci_remove(struct pci_dev *dev)
L
Linus Torvalds 已提交
1401
{
1402 1403 1404 1405
	struct fb_info *info = pci_get_drvdata(dev);
	struct tridentfb_par *par = info->par;

	unregister_framebuffer(info);
L
Linus Torvalds 已提交
1406
	iounmap(par->io_virt);
1407
	iounmap(info->screen_base);
L
Linus Torvalds 已提交
1408
	release_mem_region(tridentfb_fix.smem_start, tridentfb_fix.smem_len);
1409
	release_mem_region(tridentfb_fix.mmio_start, tridentfb_fix.mmio_len);
1410 1411
	pci_set_drvdata(dev, NULL);
	framebuffer_release(info);
L
Linus Torvalds 已提交
1412 1413 1414 1415
}

/* List of boards that we are trying to support */
static struct pci_device_id trident_devices[] = {
1416 1417 1418 1419 1420 1421 1422 1423
	{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},
1424
	{PCI_VENDOR_ID_TRIDENT,	TGUI9440, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
	{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 已提交
1437
	{0,}
1438 1439 1440
};

MODULE_DEVICE_TABLE(pci, trident_devices);
L
Linus Torvalds 已提交
1441 1442

static struct pci_driver tridentfb_pci_driver = {
1443 1444 1445 1446
	.name = "tridentfb",
	.id_table = trident_devices,
	.probe = trident_pci_probe,
	.remove = __devexit_p(trident_pci_remove)
L
Linus Torvalds 已提交
1447 1448 1449 1450 1451
};

/*
 * Parse user specified options (`video=trident:')
 * example:
1452
 *	video=trident:800x600,bpp=16,noaccel
L
Linus Torvalds 已提交
1453 1454
 */
#ifndef MODULE
1455
static int __init tridentfb_setup(char *options)
L
Linus Torvalds 已提交
1456
{
1457
	char *opt;
L
Linus Torvalds 已提交
1458 1459
	if (!options || !*options)
		return 0;
1460 1461 1462 1463
	while ((opt = strsep(&options, ",")) != NULL) {
		if (!*opt)
			continue;
		if (!strncmp(opt, "noaccel", 7))
L
Linus Torvalds 已提交
1464
			noaccel = 1;
1465
		else if (!strncmp(opt, "fp", 2))
1466
			fp = 1;
1467
		else if (!strncmp(opt, "crt", 3))
1468
			fp = 0;
1469 1470 1471
		else if (!strncmp(opt, "bpp=", 4))
			bpp = simple_strtoul(opt + 4, NULL, 0);
		else if (!strncmp(opt, "center", 6))
L
Linus Torvalds 已提交
1472
			center = 1;
1473
		else if (!strncmp(opt, "stretch", 7))
L
Linus Torvalds 已提交
1474
			stretch = 1;
1475 1476 1477 1478 1479 1480
		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 已提交
1481
		else
1482
			mode_option = opt;
L
Linus Torvalds 已提交
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
	}
	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");