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

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

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

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

struct tridentfb_par {
30
	void __iomem *io_virt;	/* iospace virtual memory address */
31
	u32 pseudo_pal[16];
32
	int chip_id;
33
	int flatpanel;
L
Linus Torvalds 已提交
34 35
};

36
static unsigned char eng_oper;	/* engine operation... */
L
Linus Torvalds 已提交
37 38 39
static struct fb_ops tridentfb_ops;

static struct fb_fix_screeninfo tridentfb_fix = {
40
	.id = "Trident",
L
Linus Torvalds 已提交
41 42 43 44 45 46 47 48 49
	.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 */
50
static char *mode_option __devinitdata = "640x480";
51
static int bpp __devinitdata = 8;
L
Linus Torvalds 已提交
52

53
static int noaccel __devinitdata;
L
Linus Torvalds 已提交
54 55 56 57

static int center;
static int stretch;

58 59
static int fp __devinitdata;
static int crt __devinitdata;
L
Linus Torvalds 已提交
60

61 62
static int memsize __devinitdata;
static int memdiff __devinitdata;
L
Linus Torvalds 已提交
63 64
static int nativex;

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

static int is3Dchip(int id)
{
83 84 85 86 87 88 89 90 91
	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 已提交
92 93 94 95 96
}

static int iscyber(int id)
{
	switch (id) {
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	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 已提交
112

113 114 115 116 117 118 119 120 121 122 123
	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 已提交
124 125 126
	}
}

127
#define CRT 0x3D0		/* CRTC registers offset for color display */
L
Linus Torvalds 已提交
128

129 130 131 132
static inline void t_outb(struct tridentfb_par *p, u8 val, u16 reg)
{
	fb_writeb(val, p->io_virt + reg);
}
L
Linus Torvalds 已提交
133

134 135 136 137
static inline u8 t_inb(struct tridentfb_par *p, u16 reg)
{
	return fb_readb(p->io_virt + reg);
}
L
Linus Torvalds 已提交
138 139

static struct accel_switch {
140 141 142 143 144 145
	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 已提交
146 147
} *acc;

148 149 150 151 152 153 154 155 156
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 已提交
157 158 159 160 161

/*
 * Blade specific acceleration.
 */

162
#define point(x, y) ((y) << 16 | (x))
L
Linus Torvalds 已提交
163 164 165 166 167 168 169 170 171 172 173
#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

174
static void blade_init_accel(struct tridentfb_par *par, int pitch, int bpp)
L
Linus Torvalds 已提交
175
{
176 177
	int v1 = (pitch >> 3) << 20;
	int tmp = 0, v2;
L
Linus Torvalds 已提交
178
	switch (bpp) {
179 180 181 182 183 184 185 186 187 188 189 190 191
	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 已提交
192
	}
193
	v2 = v1 | (tmp << 29);
194 195 196 197 198 199 200 201 202
	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 已提交
203 204
}

205
static void blade_wait_engine(struct tridentfb_par *par)
L
Linus Torvalds 已提交
206
{
207
	while (readmmr(par, STA) & 0xFA800000) ;
L
Linus Torvalds 已提交
208 209
}

210 211
static void blade_fill_rect(struct tridentfb_par *par,
			    u32 x, u32 y, u32 w, u32 h, u32 c, u32 rop)
L
Linus Torvalds 已提交
212
{
213 214 215
	writemmr(par, CLR, c);
	writemmr(par, ROP, rop ? 0x66 : ROP_S);
	writemmr(par, CMD, 0x20000000 | 1 << 19 | 1 << 4 | 2 << 2);
L
Linus Torvalds 已提交
216

217 218
	writemmr(par, DR1, point(x, y));
	writemmr(par, DR2, point(x + w - 1, y + h - 1));
L
Linus Torvalds 已提交
219 220
}

221 222
static void blade_copy_rect(struct tridentfb_par *par,
			    u32 x1, u32 y1, u32 x2, u32 y2, u32 w, u32 h)
L
Linus Torvalds 已提交
223
{
224
	u32 s1, s2, d1, d2;
L
Linus Torvalds 已提交
225
	int direction = 2;
226 227 228 229
	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 已提交
230 231

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

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

237 238 239 240
	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 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254
}

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
255
#define masked_point(x, y) ((y & 0xffff)<<16|(x & 0xffff))
L
Linus Torvalds 已提交
256

257
static void xp_init_accel(struct tridentfb_par *par, int pitch, int bpp)
L
Linus Torvalds 已提交
258
{
259
	int tmp = 0, v1;
L
Linus Torvalds 已提交
260 261 262
	unsigned char x = 0;

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

	switch (pitch << (bpp >> 3)) {
278 279 280 281 282 283 284 285 286 287 288 289 290
	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 已提交
291 292
	}

293
	t_outb(par, x, 0x2125);
L
Linus Torvalds 已提交
294 295 296 297

	eng_oper = x | 0x40;

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

	v1 = pitch << tmp;

313 314 315
	writemmr(par, 0x2154, v1);
	writemmr(par, 0x2150, v1);
	t_outb(par, 3, 0x2126);
L
Linus Torvalds 已提交
316 317
}

318
static void xp_wait_engine(struct tridentfb_par *par)
L
Linus Torvalds 已提交
319 320 321 322 323 324 325
{
	int busy;
	int count, timeout;

	count = 0;
	timeout = 0;
	for (;;) {
326
		busy = t_inb(par, STA) & 0x80;
L
Linus Torvalds 已提交
327 328 329 330 331 332 333 334 335
		if (busy != 0x80)
			return;
		count++;
		if (count == 10000000) {
			/* Timeout */
			count = 9990000;
			timeout++;
			if (timeout == 8) {
				/* Reset engine */
336
				t_outb(par, 0x00, 0x2120);
L
Linus Torvalds 已提交
337 338 339 340 341 342
				return;
			}
		}
	}
}

343 344
static void xp_fill_rect(struct tridentfb_par *par,
			 u32 x, u32 y, u32 w, u32 h, u32 c, u32 rop)
L
Linus Torvalds 已提交
345
{
346 347 348 349 350 351 352
	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 已提交
353 354
}

355 356
static void xp_copy_rect(struct tridentfb_par *par,
			 u32 x1, u32 y1, u32 x2, u32 y2, u32 w, u32 h)
L
Linus Torvalds 已提交
357 358
{
	int direction;
359
	u32 x1_tmp, x2_tmp, y1_tmp, y2_tmp;
L
Linus Torvalds 已提交
360 361

	direction = 0x0004;
362

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

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

381 382 383 384 385 386
	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 已提交
387 388 389
}

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

/*
 * Image specific acceleration functions
 */
399
static void image_init_accel(struct tridentfb_par *par, int pitch, int bpp)
L
Linus Torvalds 已提交
400 401
{
	int tmp = 0;
402 403 404 405 406 407 408 409 410 411 412 413 414 415
	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 已提交
416
	}
417 418 419 420 421 422 423 424 425 426 427 428 429
	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 已提交
430 431
}

432
static void image_wait_engine(struct tridentfb_par *par)
L
Linus Torvalds 已提交
433
{
434
	while (readmmr(par, 0x2164) & 0xF0000000) ;
L
Linus Torvalds 已提交
435 436
}

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

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

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

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

451 452
static void image_copy_rect(struct tridentfb_par *par,
			    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
	if ((y1 > y2) || ((y1 == y2) && (x1 > x2)))
		direction = 0;

464 465
	writemmr(par, 0x2120, 0x80000000);
	writemmr(par, 0x2120, 0x90000000 | ROP_S);
466

467 468 469 470 471 472
	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);
473
}
L
Linus Torvalds 已提交
474 475 476 477 478 479 480 481 482 483 484 485

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
486 487
static void tridentfb_fillrect(struct fb_info *info,
			       const struct fb_fillrect *fr)
L
Linus Torvalds 已提交
488
{
489
	struct tridentfb_par *par = info->par;
L
Linus Torvalds 已提交
490
	int bpp = info->var.bits_per_pixel;
491
	int col = 0;
492

L
Linus Torvalds 已提交
493
	switch (bpp) {
494 495 496 497 498 499 500 501 502 503 504 505 506 507
	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;
	}

508 509 510
	acc->fill_rect(par, fr->dx, fr->dy, fr->width,
		       fr->height, col, fr->rop);
	acc->wait_engine(par);
L
Linus Torvalds 已提交
511
}
512 513
static void tridentfb_copyarea(struct fb_info *info,
			       const struct fb_copyarea *ca)
L
Linus Torvalds 已提交
514
{
515 516 517 518 519
	struct tridentfb_par *par = info->par;

	acc->copy_rect(par, ca->sx, ca->sy, ca->dx, ca->dy,
		       ca->width, ca->height);
	acc->wait_engine(par);
L
Linus Torvalds 已提交
520 521 522 523 524 525 526 527 528 529 530
}
#else /* !CONFIG_FB_TRIDENT_ACCEL */
#define tridentfb_fillrect cfb_fillrect
#define tridentfb_copyarea cfb_copyarea
#endif /* CONFIG_FB_TRIDENT_ACCEL */


/*
 * Hardware access functions
 */

531
static inline unsigned char read3X4(struct tridentfb_par *par, int reg)
L
Linus Torvalds 已提交
532 533
{
	writeb(reg, par->io_virt + CRT + 4);
534
	return readb(par->io_virt + CRT + 5);
L
Linus Torvalds 已提交
535 536
}

537 538
static inline void write3X4(struct tridentfb_par *par, int reg,
			    unsigned char val)
L
Linus Torvalds 已提交
539 540 541 542 543
{
	writeb(reg, par->io_virt + CRT + 4);
	writeb(val, par->io_virt + CRT + 5);
}

544
static inline unsigned char read3C4(struct tridentfb_par *par, int reg)
L
Linus Torvalds 已提交
545
{
546 547
	t_outb(par, reg, 0x3C4);
	return t_inb(par, 0x3C5);
L
Linus Torvalds 已提交
548 549
}

550 551
static inline void write3C4(struct tridentfb_par *par, int reg,
			    unsigned char val)
L
Linus Torvalds 已提交
552
{
553 554
	t_outb(par, reg, 0x3C4);
	t_outb(par, val, 0x3C5);
L
Linus Torvalds 已提交
555 556
}

557
static inline unsigned char read3CE(struct tridentfb_par *par, int reg)
L
Linus Torvalds 已提交
558
{
559 560
	t_outb(par, reg, 0x3CE);
	return t_inb(par, 0x3CF);
L
Linus Torvalds 已提交
561 562
}

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

571 572
static inline void write3CE(struct tridentfb_par *par, int reg,
			    unsigned char val)
L
Linus Torvalds 已提交
573
{
574 575
	t_outb(par, reg, 0x3CE);
	t_outb(par, val, 0x3CF);
L
Linus Torvalds 已提交
576 577
}

578
static void enable_mmio(void)
L
Linus Torvalds 已提交
579 580 581 582 583 584 585 586
{
	/* Goto New Mode */
	outb(0x0B, 0x3C4);
	inb(0x3C5);

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

L
Linus Torvalds 已提交
588
	/* Enable MMIO */
589
	outb(PCIReg, 0x3D4);
L
Linus Torvalds 已提交
590
	outb(inb(0x3D5) | 0x01, 0x3D5);
591 592
}

593
static void disable_mmio(struct tridentfb_par *par)
594 595
{
	/* Goto New Mode */
596 597
	t_outb(par, 0x0B, 0x3C4);
	t_inb(par, 0x3C5);
598 599

	/* Unprotect registers */
600 601
	t_outb(par, NewMode1, 0x3C4);
	t_outb(par, 0x80, 0x3C5);
602 603

	/* Disable MMIO */
604 605
	t_outb(par, PCIReg, 0x3D4);
	t_outb(par, t_inb(par, 0x3D5) & ~0x01, 0x3D5);
L
Linus Torvalds 已提交
606 607
}

608 609 610 611
static void crtc_unlock(struct tridentfb_par *par)
{
	write3X4(par, CRTVSyncEnd, read3X4(par, CRTVSyncEnd) & 0x7F);
}
L
Linus Torvalds 已提交
612 613

/*  Return flat panel's maximum x resolution */
614
static int __devinit get_nativex(struct tridentfb_par *par)
L
Linus Torvalds 已提交
615
{
616
	int x, y, tmp;
L
Linus Torvalds 已提交
617 618 619 620

	if (nativex)
		return nativex;

621
	tmp = (read3CE(par, VertStretch) >> 4) & 3;
L
Linus Torvalds 已提交
622 623

	switch (tmp) {
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
	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 已提交
640 641 642 643 644 645 646
	}

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

/* Set pitch */
647
static void set_lwidth(struct tridentfb_par *par, int width)
L
Linus Torvalds 已提交
648
{
649 650 651
	write3X4(par, Offset, width & 0xFF);
	write3X4(par, AddColReg,
		 (read3X4(par, AddColReg) & 0xCF) | ((width & 0x300) >> 4));
L
Linus Torvalds 已提交
652 653 654
}

/* For resolutions smaller than FP resolution stretch */
655
static void screen_stretch(struct tridentfb_par *par)
L
Linus Torvalds 已提交
656
{
657
	if (par->chip_id != CYBERBLADEXPAi1)
658
		write3CE(par, BiosReg, 0);
659
	else
660 661 662
		write3CE(par, BiosReg, 8);
	write3CE(par, VertStretch, (read3CE(par, VertStretch) & 0x7C) | 1);
	write3CE(par, HorStretch, (read3CE(par, HorStretch) & 0x7C) | 1);
L
Linus Torvalds 已提交
663 664 665
}

/* For resolutions smaller than FP resolution center */
666
static void screen_center(struct tridentfb_par *par)
L
Linus Torvalds 已提交
667
{
668 669
	write3CE(par, VertStretch, (read3CE(par, VertStretch) & 0x7C) | 0x80);
	write3CE(par, HorStretch, (read3CE(par, HorStretch) & 0x7C) | 0x80);
L
Linus Torvalds 已提交
670 671 672
}

/* Address of first shown pixel in display memory */
673
static void set_screen_start(struct tridentfb_par *par, int base)
L
Linus Torvalds 已提交
674
{
675 676 677 678 679 680 681
	u8 tmp;
	write3X4(par, StartAddrLow, base & 0xFF);
	write3X4(par, StartAddrHigh, (base & 0xFF00) >> 8);
	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 已提交
682 683 684
}

/* Set dotclock frequency */
685
static void set_vclk(struct tridentfb_par *par, unsigned long freq)
L
Linus Torvalds 已提交
686
{
687
	int m, n, k;
688
	unsigned long f, fi, d, di;
689
	unsigned char lo = 0, hi = 0;
L
Linus Torvalds 已提交
690

691
	d = 20000;
692 693 694
	for (k = 2; k >= 0; k--)
		for (m = 0; m < 63; m++)
			for (n = 0; n < 128; n++) {
695
				fi = ((14318l * (n + 8)) / (m + 2)) >> k;
696 697 698 699 700 701
				if ((di = abs(fi - freq)) < d) {
					d = di;
					f = fi;
					lo = n;
					hi = (k << 6) | m;
				}
702 703
				if (fi > freq)
					break;
704
			}
705
	if (is3Dchip(par->chip_id)) {
706 707
		write3C4(par, ClockHigh, hi);
		write3C4(par, ClockLow, lo);
L
Linus Torvalds 已提交
708
	} else {
709 710
		outb(lo, 0x43C8);
		outb(hi, 0x43C9);
L
Linus Torvalds 已提交
711
	}
712
	debug("VCLK = %X %X\n", hi, lo);
L
Linus Torvalds 已提交
713 714 715
}

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

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

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

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

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

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

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

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

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

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

	return 0;

}
874

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

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

892 893 894 895 896 897 898 899 900
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 已提交
901 902 903 904

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

L
Linus Torvalds 已提交
913
	debug("enter\n");
914 915 916 917 918 919
	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 已提交
920 921 922 923 924 925
	hblankstart = hdispend + 1;
	hblankend = htotal + 5;

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

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

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

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

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

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

	/* vertical timing values */
954 955 956 957 958 959
	write3X4(par, CRTVTotal, vtotal & 0xFF);
	write3X4(par, CRTVDispEnd, vdispend & 0xFF);
	write3X4(par, CRTVSyncStart, vsyncstart & 0xFF);
	write3X4(par, CRTVSyncEnd, (vsyncend & 0x0F));
	write3X4(par, CRTVBlankStart, vblankstart & 0xFF);
	write3X4(par, CRTVBlankEnd, 0 /* p->vblankend & 0xFF */);
L
Linus Torvalds 已提交
960 961

	/* horizontal timing values */
962 963 964 965 966 967 968
	write3X4(par, CRTHTotal, htotal & 0xFF);
	write3X4(par, CRTHDispEnd, hdispend & 0xFF);
	write3X4(par, CRTHSyncStart, hsyncstart & 0xFF);
	write3X4(par, CRTHSyncEnd,
		 (hsyncend & 0x1F) | ((hblankend & 0x20) << 2));
	write3X4(par, CRTHBlankStart, hblankstart & 0xFF);
	write3X4(par, CRTHBlankEnd, 0 /* (p->hblankend & 0x1F) */);
L
Linus Torvalds 已提交
969 970 971 972 973 974 975 976 977 978 979

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

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

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

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

999 1000 1001
	write3X4(par, CRTLineCompare, 0xFF);
	write3X4(par, CRTPRowScan, 0);
	write3X4(par, CRTModeControl, 0xC3);
L
Linus Torvalds 已提交
1002

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

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

1009 1010
	/* enable GE for text acceleration */
	write3X4(par, GraphEngReg, 0x80);
L
Linus Torvalds 已提交
1011

1012
#ifdef CONFIG_FB_TRIDENT_ACCEL
1013
	acc->init_accel(par, info->var.xres, bpp);
1014
#endif
1015

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

1031
	write3X4(par, PixelBusReg, tmp);
L
Linus Torvalds 已提交
1032 1033

	tmp = 0x10;
1034
	if (iscyber(par->chip_id))
1035
		tmp |= 0x20;
1036
	write3X4(par, DRAMControl, tmp);	/* both IO, linear enable */
L
Linus Torvalds 已提交
1037

1038 1039 1040 1041
	write3X4(par, InterfaceSel, read3X4(par, InterfaceSel) | 0x40);
	write3X4(par, Performance, 0x92);
	/* MMIO & PCI read and write burst enable */
	write3X4(par, PCIReg, 0x07);
L
Linus Torvalds 已提交
1042

1043 1044
	/* convert from picoseconds to kHz */
	vclk = PICOS2KHZ(info->var.pixclock);
L
Linus Torvalds 已提交
1045
	if (bpp == 32)
1046
		vclk *= 2;
1047
	set_vclk(par, vclk);
L
Linus Torvalds 已提交
1048

1049 1050 1051 1052 1053 1054
	write3C4(par, 0, 3);
	write3C4(par, 1, 1);		/* set char clock 8 dots wide */
	/* enable 4 maps because needed in chain4 mode */
	write3C4(par, 2, 0x0F);
	write3C4(par, 3, 0);
	write3C4(par, 4, 0x0E);	/* memory mode enable bitmaps ?? */
L
Linus Torvalds 已提交
1055

1056 1057 1058 1059 1060
	/* 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 已提交
1061

1062
	if (par->chip_id == CYBERBLADEXPAi1) {
L
Linus Torvalds 已提交
1063
		/* This fixes snow-effect in 32 bpp */
1064
		write3X4(par, CRTHSyncStart, 0x84);
L
Linus Torvalds 已提交
1065 1066
	}

1067 1068 1069 1070
	/* 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 已提交
1071

1072 1073
	/* colors */
	for (tmp = 0; tmp < 0x10; tmp++)
1074 1075 1076
		writeAttr(par, tmp, tmp);
	fb_readb(par->io_virt + CRT + 0x0A);	/* flip-flop to index */
	t_outb(par, 0x20, 0x3C0);		/* enable attr */
L
Linus Torvalds 已提交
1077 1078

	switch (bpp) {
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
	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 已提交
1092 1093
	}

1094 1095 1096 1097 1098 1099 1100
	t_inb(par, 0x3C8);
	t_inb(par, 0x3C6);
	t_inb(par, 0x3C6);
	t_inb(par, 0x3C6);
	t_inb(par, 0x3C6);
	t_outb(par, tmp, 0x3C6);
	t_inb(par, 0x3C8);
L
Linus Torvalds 已提交
1101

1102
	if (par->flatpanel)
1103 1104
		set_number_of_lines(par, info->var.yres);
	set_lwidth(par, info->var.xres * bpp / (4 * 16));
L
Linus Torvalds 已提交
1105
	info->fix.visual = (bpp == 8) ? FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
1106 1107
	info->fix.line_length = info->var.xres * (bpp >> 3);
	info->cmap.len = (bpp == 8) ? 256 : 16;
L
Linus Torvalds 已提交
1108 1109 1110 1111 1112 1113
	debug("exit\n");
	return 0;
}

/* Set one color register */
static int tridentfb_setcolreg(unsigned regno, unsigned red, unsigned green,
1114 1115
			       unsigned blue, unsigned transp,
			       struct fb_info *info)
L
Linus Torvalds 已提交
1116 1117
{
	int bpp = info->var.bits_per_pixel;
1118
	struct tridentfb_par *par = info->par;
L
Linus Torvalds 已提交
1119 1120 1121 1122

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

1123
	if (bpp == 8) {
1124 1125
		t_outb(par, 0xFF, 0x3C6);
		t_outb(par, regno, 0x3C8);
L
Linus Torvalds 已提交
1126

1127 1128 1129
		t_outb(par, red >> 10, 0x3C9);
		t_outb(par, green >> 10, 0x3C9);
		t_outb(par, blue >> 10, 0x3C9);
L
Linus Torvalds 已提交
1130

1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
	} 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] =
1141 1142
				((transp & 0xFF00) << 16)	|
				((red & 0xFF00) << 8)		|
1143
				((green & 0xFF00))		|
1144
				((blue & 0xFF00) >> 8);
1145
	}
L
Linus Torvalds 已提交
1146

1147
/* 	debug("exit\n"); */
L
Linus Torvalds 已提交
1148 1149 1150 1151 1152 1153
	return 0;
}

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

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

1188 1189 1190
	write3CE(par, PowerStatus, DPMSCont);
	t_outb(par, 4, 0x83C8);
	t_outb(par, PMCont, 0x83C6);
L
Linus Torvalds 已提交
1191 1192 1193 1194 1195 1196 1197

	debug("exit\n");

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

1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
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,
};

1210 1211
static int __devinit trident_pci_probe(struct pci_dev *dev,
				       const struct pci_device_id *id)
L
Linus Torvalds 已提交
1212 1213 1214
{
	int err;
	unsigned char revision;
1215 1216
	struct fb_info *info;
	struct tridentfb_par *default_par;
1217 1218 1219
	int defaultaccel;
	int chip3D;
	int chip_id;
L
Linus Torvalds 已提交
1220 1221 1222 1223 1224

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

1225 1226 1227 1228 1229
	info = framebuffer_alloc(sizeof(struct tridentfb_par), &dev->dev);
	if (!info)
		return -ENOMEM;
	default_par = info->par;

L
Linus Torvalds 已提交
1230 1231
	chip_id = id->device;

1232
	if (chip_id == CYBERBLADEi1)
1233 1234 1235 1236
		output("*** Please do use cyblafb, Cyberblade/i1 support "
		       "will soon be removed from tridentfb!\n");


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

L
Linus Torvalds 已提交
1239
	if (chip_id == TGUI9660) {
1240 1241 1242
		outb(RevisionID, 0x3C4);
		revision = inb(0x3C5);

L
Linus Torvalds 已提交
1243
		switch (revision) {
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
		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 已提交
1268 1269 1270 1271 1272 1273 1274
		}
	}

	chip3D = is3Dchip(chip_id);

	if (is_xp(chip_id)) {
		acc = &accel_xp;
1275
	} else if (is_blade(chip_id)) {
L
Linus Torvalds 已提交
1276 1277 1278 1279 1280
		acc = &accel_blade;
	} else {
		acc = &accel_image;
	}

1281 1282
	default_par->chip_id = chip_id;

L
Linus Torvalds 已提交
1283 1284 1285 1286
	/* acceleration is on by default for 3D chips */
	defaultaccel = chip3D && !noaccel;

	/* setup MMIO region */
1287 1288
	tridentfb_fix.mmio_start = pci_resource_start(dev, 1);
	tridentfb_fix.mmio_len = chip3D ? 0x20000 : 0x10000;
L
Linus Torvalds 已提交
1289 1290 1291 1292 1293 1294

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

1295 1296
	default_par->io_virt = ioremap_nocache(tridentfb_fix.mmio_start,
					       tridentfb_fix.mmio_len);
L
Linus Torvalds 已提交
1297

1298
	if (!default_par->io_virt) {
L
Linus Torvalds 已提交
1299
		debug("ioremap failed\n");
1300 1301
		err = -1;
		goto out_unmap1;
L
Linus Torvalds 已提交
1302 1303 1304
	}

	enable_mmio();
1305

L
Linus Torvalds 已提交
1306
	/* setup framebuffer memory */
1307
	tridentfb_fix.smem_start = pci_resource_start(dev, 0);
1308
	tridentfb_fix.smem_len = get_memsize(default_par);
1309

L
Linus Torvalds 已提交
1310 1311
	if (!request_mem_region(tridentfb_fix.smem_start, tridentfb_fix.smem_len, "tridentfb")) {
		debug("request_mem_region failed!\n");
1312
		disable_mmio(info->par);
1313
		err = -1;
1314
		goto out_unmap1;
L
Linus Torvalds 已提交
1315 1316
	}

1317 1318
	info->screen_base = ioremap_nocache(tridentfb_fix.smem_start,
					    tridentfb_fix.smem_len);
L
Linus Torvalds 已提交
1319

1320
	if (!info->screen_base) {
L
Linus Torvalds 已提交
1321
		debug("ioremap failed\n");
1322
		err = -1;
1323
		goto out_unmap2;
L
Linus Torvalds 已提交
1324 1325 1326
	}

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

1329
	if (default_par->flatpanel)
1330
		nativex = get_nativex(default_par);
L
Linus Torvalds 已提交
1331

1332 1333
	info->fix = tridentfb_fix;
	info->fbops = &tridentfb_ops;
L
Linus Torvalds 已提交
1334 1335


1336
	info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
L
Linus Torvalds 已提交
1337
#ifdef CONFIG_FB_TRIDENT_ACCEL
1338
	info->flags |= FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT;
L
Linus Torvalds 已提交
1339
#endif
1340
	if (!fb_find_mode(&info->var, info,
1341
			  mode_option, NULL, 0, NULL, bpp)) {
1342
		err = -EINVAL;
1343
		goto out_unmap2;
1344
	}
1345
	err = fb_alloc_cmap(&info->cmap, 256, 0);
1346 1347 1348
	if (err < 0)
		goto out_unmap2;

L
Linus Torvalds 已提交
1349
	if (defaultaccel && acc)
1350
		info->var.accel_flags |= FB_ACCELF_TEXT;
L
Linus Torvalds 已提交
1351
	else
1352 1353
		info->var.accel_flags &= ~FB_ACCELF_TEXT;
	info->var.activate |= FB_ACTIVATE_NOW;
1354 1355
	info->device = &dev->dev;
	if (register_framebuffer(info) < 0) {
L
Linus Torvalds 已提交
1356
		printk(KERN_ERR "tridentfb: could not register Trident framebuffer\n");
1357
		fb_dealloc_cmap(&info->cmap);
1358
		err = -EINVAL;
1359
		goto out_unmap2;
L
Linus Torvalds 已提交
1360 1361
	}
	output("fb%d: %s frame buffer device %dx%d-%dbpp\n",
1362 1363
	   info->node, info->fix.id, info->var.xres,
	   info->var.yres, info->var.bits_per_pixel);
1364 1365

	pci_set_drvdata(dev, info);
L
Linus Torvalds 已提交
1366
	return 0;
1367

1368
out_unmap2:
1369 1370
	if (info->screen_base)
		iounmap(info->screen_base);
1371
	release_mem_region(tridentfb_fix.smem_start, tridentfb_fix.smem_len);
1372
	disable_mmio(info->par);
1373
out_unmap1:
1374 1375
	if (default_par->io_virt)
		iounmap(default_par->io_virt);
1376
	release_mem_region(tridentfb_fix.mmio_start, tridentfb_fix.mmio_len);
1377
	framebuffer_release(info);
1378
	return err;
L
Linus Torvalds 已提交
1379 1380
}

1381
static void __devexit trident_pci_remove(struct pci_dev *dev)
L
Linus Torvalds 已提交
1382
{
1383 1384 1385 1386
	struct fb_info *info = pci_get_drvdata(dev);
	struct tridentfb_par *par = info->par;

	unregister_framebuffer(info);
L
Linus Torvalds 已提交
1387
	iounmap(par->io_virt);
1388
	iounmap(info->screen_base);
L
Linus Torvalds 已提交
1389
	release_mem_region(tridentfb_fix.smem_start, tridentfb_fix.smem_len);
1390
	release_mem_region(tridentfb_fix.mmio_start, tridentfb_fix.mmio_len);
1391 1392
	pci_set_drvdata(dev, NULL);
	framebuffer_release(info);
L
Linus Torvalds 已提交
1393 1394 1395 1396
}

/* List of boards that we are trying to support */
static struct pci_device_id trident_devices[] = {
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
	{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 已提交
1417
	{0,}
1418 1419 1420
};

MODULE_DEVICE_TABLE(pci, trident_devices);
L
Linus Torvalds 已提交
1421 1422

static struct pci_driver tridentfb_pci_driver = {
1423 1424 1425 1426
	.name = "tridentfb",
	.id_table = trident_devices,
	.probe = trident_pci_probe,
	.remove = __devexit_p(trident_pci_remove)
L
Linus Torvalds 已提交
1427 1428 1429 1430 1431
};

/*
 * Parse user specified options (`video=trident:')
 * example:
1432
 *	video=trident:800x600,bpp=16,noaccel
L
Linus Torvalds 已提交
1433 1434
 */
#ifndef MODULE
1435
static int __init tridentfb_setup(char *options)
L
Linus Torvalds 已提交
1436
{
1437
	char *opt;
L
Linus Torvalds 已提交
1438 1439
	if (!options || !*options)
		return 0;
1440 1441 1442 1443
	while ((opt = strsep(&options, ",")) != NULL) {
		if (!*opt)
			continue;
		if (!strncmp(opt, "noaccel", 7))
L
Linus Torvalds 已提交
1444
			noaccel = 1;
1445
		else if (!strncmp(opt, "fp", 2))
1446
			fp = 1;
1447
		else if (!strncmp(opt, "crt", 3))
1448
			fp = 0;
1449 1450 1451
		else if (!strncmp(opt, "bpp=", 4))
			bpp = simple_strtoul(opt + 4, NULL, 0);
		else if (!strncmp(opt, "center", 6))
L
Linus Torvalds 已提交
1452
			center = 1;
1453
		else if (!strncmp(opt, "stretch", 7))
L
Linus Torvalds 已提交
1454
			stretch = 1;
1455 1456 1457 1458 1459 1460
		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 已提交
1461
		else
1462
			mode_option = opt;
L
Linus Torvalds 已提交
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
	}
	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");