tridentfb.c 44.6 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * Frame buffer driver for Trident TGUI, Blade and Image series
L
Linus Torvalds 已提交
3
 *
4
 * Copyright 2001, 2002 - Jani Monoses   <jani@iv.ro>
K
Krzysztof Helt 已提交
5
 * Copyright 2009 Krzysztof Helt <krzysztof.h1@wp.pl>
L
Linus Torvalds 已提交
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
 *	timing value tweaking so it looks good on every monitor in every mode
L
Linus Torvalds 已提交
16 17 18 19 20 21
 */

#include <linux/module.h>
#include <linux/fb.h>
#include <linux/init.h>
#include <linux/pci.h>
22
#include <linux/slab.h>
L
Linus Torvalds 已提交
23 24

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

O
Ondrej Zary 已提交
28 29 30
#include <linux/i2c.h>
#include <linux/i2c-algo-bit.h>

L
Linus Torvalds 已提交
31
struct tridentfb_par {
32
	void __iomem *io_virt;	/* iospace virtual memory address */
33
	u32 pseudo_pal[16];
34
	int chip_id;
35
	int flatpanel;
36 37 38 39 40 41
	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);
42 43 44
	void (*image_blit)
		(struct tridentfb_par *par, const char*,
		 u32, u32, u32, u32, u32, u32);
45
	unsigned char eng_oper;	/* engine operation... */
O
Ondrej Zary 已提交
46 47 48
	bool ddc_registered;
	struct i2c_adapter ddc_adapter;
	struct i2c_algo_bit_data ddc_algo;
L
Linus Torvalds 已提交
49 50 51
};

static struct fb_fix_screeninfo tridentfb_fix = {
52
	.id = "Trident",
L
Linus Torvalds 已提交
53 54 55 56 57 58 59 60 61
	.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 */
O
Ondrej Zary 已提交
62
static char *mode_option;
63
static int bpp = 8;
L
Linus Torvalds 已提交
64

65
static int noaccel;
L
Linus Torvalds 已提交
66 67 68 69

static int center;
static int stretch;

70 71
static int fp;
static int crt;
L
Linus Torvalds 已提交
72

73 74
static int memsize;
static int memdiff;
L
Linus Torvalds 已提交
75 76
static int nativex;

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

93
static inline int is_oldclock(int id)
94
{
95 96
	return	(id == TGUI9440) ||
		(id == TGUI9660) ||
97 98 99
		(id == CYBER9320);
}

100
static inline int is_oldprotect(int id)
101
{
102
	return	is_oldclock(id) ||
103 104 105
		(id == PROVIDIA9685) ||
		(id == CYBER9382) ||
		(id == CYBER9385);
106 107
}

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

120
static inline int is_xp(int id)
121 122 123 124 125 126
{
	return	(id == CYBERBLADEXPAi1) ||
		(id == CYBERBLADEXPm8) ||
		(id == CYBERBLADEXPm16);
}

127
static inline int is3Dchip(int id)
L
Linus Torvalds 已提交
128
{
129
	return	is_blade(id) || is_xp(id) ||
130 131
		(id == CYBER9397) || (id == CYBER9397DVD) ||
		(id == CYBER9520) || (id == CYBER9525DVD) ||
132
		(id == IMAGE975) || (id == IMAGE985);
L
Linus Torvalds 已提交
133 134
}

135
static inline int iscyber(int id)
L
Linus Torvalds 已提交
136 137
{
	switch (id) {
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	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 已提交
153

154 155 156 157 158 159
	case CYBER9320:
	case CYBERBLADEi7:	/* VIA MPV4 integrated version */
	default:
		/* case CYBERBLDAEXPm8:  Strange */
		/* case CYBERBLDAEXPm16: Strange */
		return 0;
L
Linus Torvalds 已提交
160 161 162
	}
}

163 164 165 166
static inline void t_outb(struct tridentfb_par *p, u8 val, u16 reg)
{
	fb_writeb(val, p->io_virt + reg);
}
L
Linus Torvalds 已提交
167

168 169 170 171
static inline u8 t_inb(struct tridentfb_par *p, u16 reg)
{
	return fb_readb(p->io_virt + reg);
}
L
Linus Torvalds 已提交
172

173 174 175 176 177 178 179 180 181
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 已提交
182

O
Ondrej Zary 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
#define DDC_SDA_TGUI		BIT(0)
#define DDC_SCL_TGUI		BIT(1)
#define DDC_SCL_DRIVE_TGUI	BIT(2)
#define DDC_SDA_DRIVE_TGUI	BIT(3)
#define DDC_MASK_TGUI		(DDC_SCL_DRIVE_TGUI | DDC_SDA_DRIVE_TGUI)

static void tridentfb_ddc_setscl_tgui(void *data, int val)
{
	struct tridentfb_par *par = data;
	u8 reg = vga_mm_rcrt(par->io_virt, I2C) & DDC_MASK_TGUI;

	if (val)
		reg &= ~DDC_SCL_DRIVE_TGUI; /* disable drive - don't drive hi */
	else
		reg |= DDC_SCL_DRIVE_TGUI; /* drive low */

	vga_mm_wcrt(par->io_virt, I2C, reg);
}

static void tridentfb_ddc_setsda_tgui(void *data, int val)
{
	struct tridentfb_par *par = data;
	u8 reg = vga_mm_rcrt(par->io_virt, I2C) & DDC_MASK_TGUI;

	if (val)
		reg &= ~DDC_SDA_DRIVE_TGUI; /* disable drive - don't drive hi */
	else
		reg |= DDC_SDA_DRIVE_TGUI; /* drive low */

	vga_mm_wcrt(par->io_virt, I2C, reg);
}

static int tridentfb_ddc_getsda_tgui(void *data)
{
	struct tridentfb_par *par = data;

	return !!(vga_mm_rcrt(par->io_virt, I2C) & DDC_SDA_TGUI);
}

#define DDC_SDA_IN	BIT(0)
#define DDC_SCL_OUT	BIT(1)
#define DDC_SDA_OUT	BIT(3)
#define DDC_SCL_IN	BIT(6)
#define DDC_MASK	(DDC_SCL_OUT | DDC_SDA_OUT)

static void tridentfb_ddc_setscl(void *data, int val)
{
	struct tridentfb_par *par = data;
	unsigned char reg;

	reg = vga_mm_rcrt(par->io_virt, I2C) & DDC_MASK;
	if (val)
		reg |= DDC_SCL_OUT;
	else
		reg &= ~DDC_SCL_OUT;
	vga_mm_wcrt(par->io_virt, I2C, reg);
}

static void tridentfb_ddc_setsda(void *data, int val)
{
	struct tridentfb_par *par = data;
	unsigned char reg;

	reg = vga_mm_rcrt(par->io_virt, I2C) & DDC_MASK;
	if (!val)
		reg |= DDC_SDA_OUT;
	else
		reg &= ~DDC_SDA_OUT;
	vga_mm_wcrt(par->io_virt, I2C, reg);
}

static int tridentfb_ddc_getscl(void *data)
{
	struct tridentfb_par *par = data;

	return !!(vga_mm_rcrt(par->io_virt, I2C) & DDC_SCL_IN);
}

static int tridentfb_ddc_getsda(void *data)
{
	struct tridentfb_par *par = data;

	return !!(vga_mm_rcrt(par->io_virt, I2C) & DDC_SDA_IN);
}

static int tridentfb_setup_ddc_bus(struct fb_info *info)
{
	struct tridentfb_par *par = info->par;

	strlcpy(par->ddc_adapter.name, info->fix.id,
		sizeof(par->ddc_adapter.name));
	par->ddc_adapter.owner		= THIS_MODULE;
	par->ddc_adapter.class		= I2C_CLASS_DDC;
	par->ddc_adapter.algo_data	= &par->ddc_algo;
	par->ddc_adapter.dev.parent	= info->device;
	if (is_oldclock(par->chip_id)) { /* not sure if this check is OK */
		par->ddc_algo.setsda	= tridentfb_ddc_setsda_tgui;
		par->ddc_algo.setscl	= tridentfb_ddc_setscl_tgui;
		par->ddc_algo.getsda	= tridentfb_ddc_getsda_tgui;
		/* no getscl */
	} else {
		par->ddc_algo.setsda	= tridentfb_ddc_setsda;
		par->ddc_algo.setscl	= tridentfb_ddc_setscl;
		par->ddc_algo.getsda	= tridentfb_ddc_getsda;
		par->ddc_algo.getscl	= tridentfb_ddc_getscl;
	}
	par->ddc_algo.udelay		= 10;
	par->ddc_algo.timeout		= 20;
	par->ddc_algo.data		= par;

	i2c_set_adapdata(&par->ddc_adapter, par);

	return i2c_bit_add_bus(&par->ddc_adapter);
}

L
Linus Torvalds 已提交
298 299 300 301
/*
 * Blade specific acceleration.
 */

302
#define point(x, y) ((y) << 16 | (x))
L
Linus Torvalds 已提交
303

304
static void blade_init_accel(struct tridentfb_par *par, int pitch, int bpp)
L
Linus Torvalds 已提交
305
{
306
	int v1 = (pitch >> 3) << 20;
307 308 309
	int tmp = bpp == 24 ? 2 : (bpp >> 4);
	int v2 = v1 | (tmp << 29);

310 311 312 313 314 315 316 317 318
	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 已提交
319 320
}

321
static void blade_wait_engine(struct tridentfb_par *par)
L
Linus Torvalds 已提交
322
{
323 324
	while (readmmr(par, STATUS) & 0xFA800000)
		cpu_relax();
L
Linus Torvalds 已提交
325 326
}

327 328
static void blade_fill_rect(struct tridentfb_par *par,
			    u32 x, u32 y, u32 w, u32 h, u32 c, u32 rop)
L
Linus Torvalds 已提交
329
{
330 331
	writemmr(par, COLOR, c);
	writemmr(par, ROP, rop ? ROP_X : ROP_S);
332
	writemmr(par, CMD, 0x20000000 | 1 << 19 | 1 << 4 | 2 << 2);
L
Linus Torvalds 已提交
333

334 335
	writemmr(par, DST1, point(x, y));
	writemmr(par, DST2, point(x + w - 1, y + h - 1));
L
Linus Torvalds 已提交
336 337
}

338 339 340 341 342 343 344 345 346 347 348 349
static void blade_image_blit(struct tridentfb_par *par, const char *data,
			     u32 x, u32 y, u32 w, u32 h, u32 c, u32 b)
{
	unsigned size = ((w + 31) >> 5) * h;

	writemmr(par, COLOR, c);
	writemmr(par, BGCOLOR, b);
	writemmr(par, CMD, 0xa0000000 | 3 << 19);

	writemmr(par, DST1, point(x, y));
	writemmr(par, DST2, point(x + w - 1, y + h - 1));

350
	iowrite32_rep(par->io_virt + 0x10000, data, size);
351 352
}

353 354
static void blade_copy_rect(struct tridentfb_par *par,
			    u32 x1, u32 y1, u32 x2, u32 y2, u32 w, u32 h)
L
Linus Torvalds 已提交
355 356
{
	int direction = 2;
357 358 359 360
	u32 s1 = point(x1, y1);
	u32 s2 = point(x1 + w - 1, y1 + h - 1);
	u32 d1 = point(x2, y2);
	u32 d2 = point(x2 + w - 1, y2 + h - 1);
L
Linus Torvalds 已提交
361 362

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

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

368 369 370 371
	writemmr(par, SRC1, direction ? s2 : s1);
	writemmr(par, SRC2, direction ? s1 : s2);
	writemmr(par, DST1, direction ? d2 : d1);
	writemmr(par, DST2, direction ? d1 : d2);
L
Linus Torvalds 已提交
372 373 374 375 376 377
}

/*
 * BladeXP specific acceleration functions
 */

378
static void xp_init_accel(struct tridentfb_par *par, int pitch, int bpp)
L
Linus Torvalds 已提交
379
{
380 381
	unsigned char x = bpp == 24 ? 3 : (bpp >> 4);
	int v1 = pitch << (bpp == 24 ? 20 : (18 + x));
L
Linus Torvalds 已提交
382 383

	switch (pitch << (bpp >> 3)) {
384 385 386 387 388 389 390 391 392 393 394 395 396
	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 已提交
397 398
	}

399
	t_outb(par, x, 0x2125);
L
Linus Torvalds 已提交
400

401
	par->eng_oper = x | 0x40;
L
Linus Torvalds 已提交
402

403 404 405
	writemmr(par, 0x2154, v1);
	writemmr(par, 0x2150, v1);
	t_outb(par, 3, 0x2126);
L
Linus Torvalds 已提交
406 407
}

408
static void xp_wait_engine(struct tridentfb_par *par)
L
Linus Torvalds 已提交
409
{
410 411
	int count = 0;
	int timeout = 0;
L
Linus Torvalds 已提交
412

413
	while (t_inb(par, STATUS) & 0x80) {
L
Linus Torvalds 已提交
414 415 416 417 418 419 420
		count++;
		if (count == 10000000) {
			/* Timeout */
			count = 9990000;
			timeout++;
			if (timeout == 8) {
				/* Reset engine */
421
				t_outb(par, 0x00, STATUS);
L
Linus Torvalds 已提交
422 423 424
				return;
			}
		}
425
		cpu_relax();
L
Linus Torvalds 已提交
426 427 428
	}
}

429 430
static void xp_fill_rect(struct tridentfb_par *par,
			 u32 x, u32 y, u32 w, u32 h, u32 c, u32 rop)
L
Linus Torvalds 已提交
431
{
432 433
	writemmr(par, 0x2127, ROP_P);
	writemmr(par, 0x2158, c);
434 435 436 437
	writemmr(par, DRAWFL, 0x4000);
	writemmr(par, OLDDIM, point(h, w));
	writemmr(par, OLDDST, point(y, x));
	t_outb(par, 0x01, OLDCMD);
438
	t_outb(par, par->eng_oper, 0x2125);
L
Linus Torvalds 已提交
439 440
}

441 442
static void xp_copy_rect(struct tridentfb_par *par,
			 u32 x1, u32 y1, u32 x2, u32 y2, u32 w, u32 h)
L
Linus Torvalds 已提交
443
{
444
	u32 x1_tmp, x2_tmp, y1_tmp, y2_tmp;
445
	int direction = 0x0004;
446

L
Linus Torvalds 已提交
447 448 449 450 451 452 453 454
	if ((x1 < x2) && (y1 == y2)) {
		direction |= 0x0200;
		x1_tmp = x1 + w - 1;
		x2_tmp = x2 + w - 1;
	} else {
		x1_tmp = x1;
		x2_tmp = x2;
	}
455

L
Linus Torvalds 已提交
456 457 458 459
	if (y1 < y2) {
		direction |= 0x0100;
		y1_tmp = y1 + h - 1;
		y2_tmp = y2 + h - 1;
460
	} else {
L
Linus Torvalds 已提交
461 462 463 464
		y1_tmp = y1;
		y2_tmp = y2;
	}

465
	writemmr(par, DRAWFL, direction);
466
	t_outb(par, ROP_S, 0x2127);
467 468 469 470
	writemmr(par, OLDSRC, point(y1_tmp, x1_tmp));
	writemmr(par, OLDDST, point(y2_tmp, x2_tmp));
	writemmr(par, OLDDIM, point(h, w));
	t_outb(par, 0x01, OLDCMD);
L
Linus Torvalds 已提交
471 472 473 474 475
}

/*
 * Image specific acceleration functions
 */
476
static void image_init_accel(struct tridentfb_par *par, int pitch, int bpp)
L
Linus Torvalds 已提交
477
{
478 479
	int tmp = bpp == 24 ? 2: (bpp >> 4);

480 481 482 483 484 485 486 487 488 489 490 491 492
	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 已提交
493 494
}

495
static void image_wait_engine(struct tridentfb_par *par)
L
Linus Torvalds 已提交
496
{
497 498
	while (readmmr(par, 0x2164) & 0xF0000000)
		cpu_relax();
L
Linus Torvalds 已提交
499 500
}

501 502
static void image_fill_rect(struct tridentfb_par *par,
			    u32 x, u32 y, u32 w, u32 h, u32 c, u32 rop)
L
Linus Torvalds 已提交
503
{
504 505
	writemmr(par, 0x2120, 0x80000000);
	writemmr(par, 0x2120, 0x90000000 | ROP_S);
L
Linus Torvalds 已提交
506

507
	writemmr(par, 0x2144, c);
L
Linus Torvalds 已提交
508

509 510
	writemmr(par, DST1, point(x, y));
	writemmr(par, DST2, point(x + w - 1, y + h - 1));
L
Linus Torvalds 已提交
511

512
	writemmr(par, 0x2124, 0x80000000 | 3 << 22 | 1 << 10 | 1 << 9);
L
Linus Torvalds 已提交
513 514
}

515 516
static void image_copy_rect(struct tridentfb_par *par,
			    u32 x1, u32 y1, u32 x2, u32 y2, u32 w, u32 h)
L
Linus Torvalds 已提交
517
{
518
	int direction = 0x4;
519 520 521 522
	u32 s1 = point(x1, y1);
	u32 s2 = point(x1 + w - 1, y1 + h - 1);
	u32 d1 = point(x2, y2);
	u32 d2 = point(x2 + w - 1, y2 + h - 1);
L
Linus Torvalds 已提交
523

524 525 526
	if ((y1 > y2) || ((y1 == y2) && (x1 > x2)))
		direction = 0;

527 528
	writemmr(par, 0x2120, 0x80000000);
	writemmr(par, 0x2120, 0x90000000 | ROP_S);
529

530 531 532 533
	writemmr(par, SRC1, direction ? s2 : s1);
	writemmr(par, SRC2, direction ? s1 : s2);
	writemmr(par, DST1, direction ? d2 : d1);
	writemmr(par, DST2, direction ? d1 : d2);
534 535
	writemmr(par, 0x2124,
		 0x80000000 | 1 << 22 | 1 << 10 | 1 << 7 | direction);
536
}
L
Linus Torvalds 已提交
537

538 539 540 541 542 543
/*
 * TGUI 9440/96XX acceleration
 */

static void tgui_init_accel(struct tridentfb_par *par, int pitch, int bpp)
{
544
	unsigned char x = bpp == 24 ? 3 : (bpp >> 4);
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572

	/* disable clipping */
	writemmr(par, 0x2148, 0);
	writemmr(par, 0x214C, point(4095, 2047));

	switch ((pitch * bpp) / 8) {
	case 8192:
	case 512:
		x |= 0x00;
		break;
	case 1024:
		x |= 0x04;
		break;
	case 2048:
		x |= 0x08;
		break;
	case 4096:
		x |= 0x0C;
		break;
	}

	fb_writew(x, par->io_virt + 0x2122);
}

static void tgui_fill_rect(struct tridentfb_par *par,
			   u32 x, u32 y, u32 w, u32 h, u32 c, u32 rop)
{
	t_outb(par, ROP_P, 0x2127);
573 574 575 576 577
	writemmr(par, OLDCLR, c);
	writemmr(par, DRAWFL, 0x4020);
	writemmr(par, OLDDIM, point(w - 1, h - 1));
	writemmr(par, OLDDST, point(x, y));
	t_outb(par, 1, OLDCMD);
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
}

static void tgui_copy_rect(struct tridentfb_par *par,
			   u32 x1, u32 y1, u32 x2, u32 y2, u32 w, u32 h)
{
	int flags = 0;
	u16 x1_tmp, x2_tmp, y1_tmp, y2_tmp;

	if ((x1 < x2) && (y1 == y2)) {
		flags |= 0x0200;
		x1_tmp = x1 + w - 1;
		x2_tmp = x2 + w - 1;
	} else {
		x1_tmp = x1;
		x2_tmp = x2;
	}

	if (y1 < y2) {
		flags |= 0x0100;
		y1_tmp = y1 + h - 1;
		y2_tmp = y2 + h - 1;
	} else {
		y1_tmp = y1;
		y2_tmp = y2;
	}

604
	writemmr(par, DRAWFL, 0x4 | flags);
605
	t_outb(par, ROP_S, 0x2127);
606 607 608 609
	writemmr(par, OLDSRC, point(x1_tmp, y1_tmp));
	writemmr(par, OLDDST, point(x2_tmp, y2_tmp));
	writemmr(par, OLDDIM, point(w - 1, h - 1));
	t_outb(par, 1, OLDCMD);
610 611
}

L
Linus Torvalds 已提交
612 613 614
/*
 * Accel functions called by the upper layers
 */
615 616
static void tridentfb_fillrect(struct fb_info *info,
			       const struct fb_fillrect *fr)
L
Linus Torvalds 已提交
617
{
618
	struct tridentfb_par *par = info->par;
619
	int col;
620

621 622 623 624
	if (info->flags & FBINFO_HWACCEL_DISABLED) {
		cfb_fillrect(info, fr);
		return;
	}
625 626
	if (info->var.bits_per_pixel == 8) {
		col = fr->color;
627 628
		col |= col << 8;
		col |= col << 16;
629
	} else
630 631
		col = ((u32 *)(info->pseudo_palette))[fr->color];

632
	par->wait_engine(par);
633
	par->fill_rect(par, fr->dx, fr->dy, fr->width,
634
		       fr->height, col, fr->rop);
L
Linus Torvalds 已提交
635
}
636

637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
static void tridentfb_imageblit(struct fb_info *info,
				const struct fb_image *img)
{
	struct tridentfb_par *par = info->par;
	int col, bgcol;

	if ((info->flags & FBINFO_HWACCEL_DISABLED) || img->depth != 1) {
		cfb_imageblit(info, img);
		return;
	}
	if (info->var.bits_per_pixel == 8) {
		col = img->fg_color;
		col |= col << 8;
		col |= col << 16;
		bgcol = img->bg_color;
		bgcol |= bgcol << 8;
		bgcol |= bgcol << 16;
	} else {
		col = ((u32 *)(info->pseudo_palette))[img->fg_color];
		bgcol = ((u32 *)(info->pseudo_palette))[img->bg_color];
	}

	par->wait_engine(par);
	if (par->image_blit)
		par->image_blit(par, img->data, img->dx, img->dy,
				img->width, img->height, col, bgcol);
	else
		cfb_imageblit(info, img);
}

667 668
static void tridentfb_copyarea(struct fb_info *info,
			       const struct fb_copyarea *ca)
L
Linus Torvalds 已提交
669
{
670 671
	struct tridentfb_par *par = info->par;

672 673 674 675
	if (info->flags & FBINFO_HWACCEL_DISABLED) {
		cfb_copyarea(info, ca);
		return;
	}
676
	par->wait_engine(par);
677
	par->copy_rect(par, ca->sx, ca->sy, ca->dx, ca->dy,
678
		       ca->width, ca->height);
679 680 681 682 683 684
}

static int tridentfb_sync(struct fb_info *info)
{
	struct tridentfb_par *par = info->par;

685 686
	if (!(info->flags & FBINFO_HWACCEL_DISABLED))
		par->wait_engine(par);
687
	return 0;
L
Linus Torvalds 已提交
688 689 690 691 692 693
}

/*
 * Hardware access functions
 */

694
static inline unsigned char read3X4(struct tridentfb_par *par, int reg)
L
Linus Torvalds 已提交
695
{
696
	return vga_mm_rcrt(par->io_virt, reg);
L
Linus Torvalds 已提交
697 698
}

699 700
static inline void write3X4(struct tridentfb_par *par, int reg,
			    unsigned char val)
L
Linus Torvalds 已提交
701
{
702
	vga_mm_wcrt(par->io_virt, reg, val);
L
Linus Torvalds 已提交
703 704
}

705 706
static inline unsigned char read3CE(struct tridentfb_par *par,
				    unsigned char reg)
L
Linus Torvalds 已提交
707
{
708
	return vga_mm_rgfx(par->io_virt, reg);
L
Linus Torvalds 已提交
709 710
}

711 712
static inline void writeAttr(struct tridentfb_par *par, int reg,
			     unsigned char val)
L
Linus Torvalds 已提交
713
{
714 715
	fb_readb(par->io_virt + VGA_IS1_RC);	/* flip-flop to index */
	vga_mm_wattr(par->io_virt, reg, val);
L
Linus Torvalds 已提交
716 717
}

718 719
static inline void write3CE(struct tridentfb_par *par, int reg,
			    unsigned char val)
L
Linus Torvalds 已提交
720
{
721
	vga_mm_wgfx(par->io_virt, reg, val);
L
Linus Torvalds 已提交
722 723
}

724
static void enable_mmio(struct tridentfb_par *par)
L
Linus Torvalds 已提交
725 726
{
	/* Goto New Mode */
727
	vga_io_rseq(0x0B);
L
Linus Torvalds 已提交
728 729

	/* Unprotect registers */
730
	vga_io_wseq(NewMode1, 0x80);
731 732
	if (!is_oldprotect(par->chip_id))
		vga_io_wseq(Protection, 0x92);
733

L
Linus Torvalds 已提交
734
	/* Enable MMIO */
735
	outb(PCIReg, 0x3D4);
L
Linus Torvalds 已提交
736
	outb(inb(0x3D5) | 0x01, 0x3D5);
737 738
}

739
static void disable_mmio(struct tridentfb_par *par)
740 741
{
	/* Goto New Mode */
742
	vga_mm_rseq(par->io_virt, 0x0B);
743 744

	/* Unprotect registers */
745
	vga_mm_wseq(par->io_virt, NewMode1, 0x80);
746 747
	if (!is_oldprotect(par->chip_id))
		vga_mm_wseq(par->io_virt, Protection, 0x92);
748 749

	/* Disable MMIO */
750 751
	t_outb(par, PCIReg, 0x3D4);
	t_outb(par, t_inb(par, 0x3D5) & ~0x01, 0x3D5);
L
Linus Torvalds 已提交
752 753
}

754
static inline void crtc_unlock(struct tridentfb_par *par)
755
{
756 757
	write3X4(par, VGA_CRTC_V_SYNC_END,
		 read3X4(par, VGA_CRTC_V_SYNC_END) & 0x7F);
758
}
L
Linus Torvalds 已提交
759 760

/*  Return flat panel's maximum x resolution */
761
static int get_nativex(struct tridentfb_par *par)
L
Linus Torvalds 已提交
762
{
763
	int x, y, tmp;
L
Linus Torvalds 已提交
764 765 766 767

	if (nativex)
		return nativex;

768
	tmp = (read3CE(par, VertStretch) >> 4) & 3;
L
Linus Torvalds 已提交
769 770

	switch (tmp) {
771 772 773 774 775 776 777 778 779 780 781 782 783
	case 0:
		x = 1280; y = 1024;
		break;
	case 2:
		x = 1024; y = 768;
		break;
	case 3:
		x = 800; y = 600;
		break;
	case 1:
	default:
		x = 640;  y = 480;
		break;
L
Linus Torvalds 已提交
784 785 786 787 788 789 790
	}

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

/* Set pitch */
791
static inline void set_lwidth(struct tridentfb_par *par, int width)
L
Linus Torvalds 已提交
792
{
793
	write3X4(par, VGA_CRTC_OFFSET, width & 0xFF);
794 795 796 797 798 799 800 801
	/* chips older than TGUI9660 have only 1 width bit in AddColReg */
	/* touching the other one breaks I2C/DDC */
	if (par->chip_id == TGUI9440 || par->chip_id == CYBER9320)
		write3X4(par, AddColReg,
		     (read3X4(par, AddColReg) & 0xEF) | ((width & 0x100) >> 4));
	else
		write3X4(par, AddColReg,
		     (read3X4(par, AddColReg) & 0xCF) | ((width & 0x300) >> 4));
L
Linus Torvalds 已提交
802 803 804
}

/* For resolutions smaller than FP resolution stretch */
805
static void screen_stretch(struct tridentfb_par *par)
L
Linus Torvalds 已提交
806
{
807
	if (par->chip_id != CYBERBLADEXPAi1)
808
		write3CE(par, BiosReg, 0);
809
	else
810 811 812
		write3CE(par, BiosReg, 8);
	write3CE(par, VertStretch, (read3CE(par, VertStretch) & 0x7C) | 1);
	write3CE(par, HorStretch, (read3CE(par, HorStretch) & 0x7C) | 1);
L
Linus Torvalds 已提交
813 814 815
}

/* For resolutions smaller than FP resolution center */
816
static inline void screen_center(struct tridentfb_par *par)
L
Linus Torvalds 已提交
817
{
818 819
	write3CE(par, VertStretch, (read3CE(par, VertStretch) & 0x7C) | 0x80);
	write3CE(par, HorStretch, (read3CE(par, HorStretch) & 0x7C) | 0x80);
L
Linus Torvalds 已提交
820 821 822
}

/* Address of first shown pixel in display memory */
823
static void set_screen_start(struct tridentfb_par *par, int base)
L
Linus Torvalds 已提交
824
{
825
	u8 tmp;
826 827
	write3X4(par, VGA_CRTC_START_LO, base & 0xFF);
	write3X4(par, VGA_CRTC_START_HI, (base & 0xFF00) >> 8);
828 829 830 831
	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 已提交
832 833 834
}

/* Set dotclock frequency */
835
static void set_vclk(struct tridentfb_par *par, unsigned long freq)
L
Linus Torvalds 已提交
836
{
837
	int m, n, k;
838 839 840
	unsigned long fi, d, di;
	unsigned char best_m = 0, best_n = 0, best_k = 0;
	unsigned char hi, lo;
K
Krzysztof Helt 已提交
841
	unsigned char shift = !is_oldclock(par->chip_id) ? 2 : 1;
L
Linus Torvalds 已提交
842

843
	d = 20000;
K
Krzysztof Helt 已提交
844 845 846
	for (k = shift; k >= 0; k--)
		for (m = 1; m < 32; m++) {
			n = ((m + 2) << shift) - 8;
847
			for (n = (n < 0 ? 0 : n); n < 122; n++) {
848
				fi = ((14318l * (n + 8)) / (m + 2)) >> k;
849
				di = abs(fi - freq);
K
Krzysztof Helt 已提交
850
				if (di < d || (di == d && k == best_k)) {
851
					d = di;
852 853 854
					best_n = n;
					best_m = m;
					best_k = k;
855
				}
856 857
				if (fi > freq)
					break;
858
			}
859
		}
860 861 862 863 864 865 866 867 868

	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);
	}

869
	if (is3Dchip(par->chip_id)) {
870 871
		vga_mm_wseq(par->io_virt, ClockHigh, hi);
		vga_mm_wseq(par->io_virt, ClockLow, lo);
L
Linus Torvalds 已提交
872
	} else {
873 874
		t_outb(par, lo, 0x43C8);
		t_outb(par, hi, 0x43C9);
L
Linus Torvalds 已提交
875
	}
876
	debug("VCLK = %X %X\n", hi, lo);
L
Linus Torvalds 已提交
877 878 879
}

/* Set number of lines for flat panels*/
880
static void set_number_of_lines(struct tridentfb_par *par, int lines)
L
Linus Torvalds 已提交
881
{
882
	int tmp = read3CE(par, CyberEnhance) & 0x8F;
L
Linus Torvalds 已提交
883 884 885 886 887 888 889 890
	if (lines > 1024)
		tmp |= 0x50;
	else if (lines > 768)
		tmp |= 0x30;
	else if (lines > 600)
		tmp |= 0x20;
	else if (lines > 480)
		tmp |= 0x10;
891
	write3CE(par, CyberEnhance, tmp);
L
Linus Torvalds 已提交
892 893 894 895
}

/*
 * If we see that FP is active we assume we have one.
896
 * Otherwise we have a CRT display. User can override.
L
Linus Torvalds 已提交
897
 */
898
static int is_flatpanel(struct tridentfb_par *par)
L
Linus Torvalds 已提交
899 900
{
	if (fp)
901
		return 1;
902
	if (crt || !iscyber(par->chip_id))
903 904
		return 0;
	return (read3CE(par, FPConfig) & 0x10) ? 1 : 0;
L
Linus Torvalds 已提交
905 906 907
}

/* Try detecting the video memory size */
908
static unsigned int get_memsize(struct tridentfb_par *par)
L
Linus Torvalds 已提交
909 910 911 912 913 914 915 916
{
	unsigned char tmp, tmp2;
	unsigned int k;

	/* If memory size provided by user */
	if (memsize)
		k = memsize * Kb;
	else
917
		switch (par->chip_id) {
918 919 920
		case CYBER9525DVD:
			k = 2560 * Kb;
			break;
L
Linus Torvalds 已提交
921
		default:
922
			tmp = read3X4(par, SPR) & 0x0F;
L
Linus Torvalds 已提交
923 924
			switch (tmp) {

925
			case 0x01:
926
				k = 512 * Kb;
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
				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 */

954
				tmp2 = vga_mm_rseq(par->io_virt, 0xC1);
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
				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 已提交
979 980
				break;
			}
981
		}
L
Linus Torvalds 已提交
982 983

	k -= memdiff * Kb;
984
	output("framebuffer size = %d Kb\n", k / Kb);
L
Linus Torvalds 已提交
985 986 987 988
	return k;
}

/* See if we can handle the video mode described in var */
989 990
static int tridentfb_check_var(struct fb_var_screeninfo *var,
			       struct fb_info *info)
L
Linus Torvalds 已提交
991
{
992
	struct tridentfb_par *par = info->par;
L
Linus Torvalds 已提交
993
	int bpp = var->bits_per_pixel;
994
	int line_length;
995
	int ramdac = 230000; /* 230MHz for most 3D chips */
L
Linus Torvalds 已提交
996 997 998
	debug("enter\n");

	/* check color depth */
999
	if (bpp == 24)
L
Linus Torvalds 已提交
1000
		bpp = var->bits_per_pixel = 32;
1001 1002
	if (bpp != 8 && bpp != 16 && bpp != 32)
		return -EINVAL;
1003 1004
	if (par->chip_id == TGUI9440 && bpp == 32)
		return -EINVAL;
1005
	/* check whether resolution fits on panel and in memory */
1006
	if (par->flatpanel && nativex && var->xres > nativex)
L
Linus Torvalds 已提交
1007
		return -EINVAL;
1008 1009
	/* various resolution checks */
	var->xres = (var->xres + 7) & ~0x7;
1010
	if (var->xres > var->xres_virtual)
1011
		var->xres_virtual = var->xres;
1012 1013 1014 1015 1016 1017 1018
	if (var->yres > var->yres_virtual)
		var->yres_virtual = var->yres;
	if (var->xres_virtual > 4095 || var->yres > 2048)
		return -EINVAL;
	/* prevent from position overflow for acceleration */
	if (var->yres_virtual > 0xffff)
		return -EINVAL;
1019
	line_length = var->xres_virtual * bpp / 8;
1020 1021 1022

	if (!is3Dchip(par->chip_id) &&
	    !(info->flags & FBINFO_HWACCEL_DISABLED)) {
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
		/* acceleration requires line length to be power of 2 */
		if (line_length <= 512)
			var->xres_virtual = 512 * 8 / bpp;
		else if (line_length <= 1024)
			var->xres_virtual = 1024 * 8 / bpp;
		else if (line_length <= 2048)
			var->xres_virtual = 2048 * 8 / bpp;
		else if (line_length <= 4096)
			var->xres_virtual = 4096 * 8 / bpp;
		else if (line_length <= 8192)
			var->xres_virtual = 8192 * 8 / bpp;
1034 1035
		else
			return -EINVAL;
1036 1037 1038

		line_length = var->xres_virtual * bpp / 8;
	}
1039

K
Krzysztof Helt 已提交
1040 1041 1042 1043
	/* datasheet specifies how to set panning only up to 4 MB */
	if (line_length * (var->yres_virtual - var->yres) > (4 << 20))
		var->yres_virtual = ((4 << 20) / line_length) + var->yres;

1044
	if (line_length * var->yres_virtual > info->fix.smem_len)
L
Linus Torvalds 已提交
1045 1046 1047
		return -EINVAL;

	switch (bpp) {
1048 1049
	case 8:
		var->red.offset = 0;
1050 1051 1052
		var->red.length = 8;
		var->green = var->red;
		var->blue = var->red;
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
		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 已提交
1072
	}
1073 1074 1075 1076 1077 1078

	if (is_xp(par->chip_id))
		ramdac = 350000;

	switch (par->chip_id) {
	case TGUI9440:
1079
		ramdac = (bpp >= 16) ? 45000 : 90000;
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
		break;
	case CYBER9320:
	case TGUI9660:
		ramdac = 135000;
		break;
	case PROVIDIA9685:
	case CYBER9388:
	case CYBER9382:
	case CYBER9385:
		ramdac = 170000;
		break;
	}

	/* The clock is doubled for 32 bpp */
	if (bpp == 32)
		ramdac /= 2;

	if (PICOS2KHZ(var->pixclock) > ramdac)
		return -EINVAL;

L
Linus Torvalds 已提交
1100 1101 1102 1103 1104
	debug("exit\n");

	return 0;

}
1105

L
Linus Torvalds 已提交
1106 1107
/* Pan the display */
static int tridentfb_pan_display(struct fb_var_screeninfo *var,
1108
				 struct fb_info *info)
L
Linus Torvalds 已提交
1109
{
1110
	struct tridentfb_par *par = info->par;
L
Linus Torvalds 已提交
1111 1112 1113
	unsigned int offset;

	debug("enter\n");
1114 1115
	offset = (var->xoffset + (var->yoffset * info->var.xres_virtual))
		* info->var.bits_per_pixel / 32;
1116
	set_screen_start(par, offset);
L
Linus Torvalds 已提交
1117 1118 1119 1120
	debug("exit\n");
	return 0;
}

1121
static inline void shadowmode_on(struct tridentfb_par *par)
1122 1123 1124 1125
{
	write3CE(par, CyberControl, read3CE(par, CyberControl) | 0x81);
}

1126
static inline void shadowmode_off(struct tridentfb_par *par)
1127 1128 1129
{
	write3CE(par, CyberControl, read3CE(par, CyberControl) & 0x7E);
}
L
Linus Torvalds 已提交
1130 1131 1132 1133

/* Set the hardware to the requested video mode */
static int tridentfb_set_par(struct fb_info *info)
{
1134
	struct tridentfb_par *par = info->par;
1135 1136 1137
	u32 htotal, hdispend, hsyncstart, hsyncend, hblankstart, hblankend;
	u32 vtotal, vdispend, vsyncstart, vsyncend, vblankstart, vblankend;
	struct fb_var_screeninfo *var = &info->var;
L
Linus Torvalds 已提交
1138 1139
	int bpp = var->bits_per_pixel;
	unsigned char tmp;
1140 1141
	unsigned long vclk;

L
Linus Torvalds 已提交
1142
	debug("enter\n");
1143
	hdispend = var->xres / 8 - 1;
1144 1145
	hsyncstart = (var->xres + var->right_margin) / 8;
	hsyncend = (var->xres + var->right_margin + var->hsync_len) / 8;
1146 1147
	htotal = (var->xres + var->left_margin + var->right_margin +
		  var->hsync_len) / 8 - 5;
1148
	hblankstart = hdispend + 1;
1149
	hblankend = htotal + 3;
L
Linus Torvalds 已提交
1150 1151 1152

	vdispend = var->yres - 1;
	vsyncstart = var->yres + var->lower_margin;
1153 1154
	vsyncend = vsyncstart + var->vsync_len;
	vtotal = var->upper_margin + vsyncend - 2;
1155
	vblankstart = vdispend + 1;
1156
	vblankend = vtotal;
L
Linus Torvalds 已提交
1157

1158 1159 1160 1161 1162 1163 1164 1165 1166
	if (info->var.vmode & FB_VMODE_INTERLACED) {
		vtotal /= 2;
		vdispend /= 2;
		vsyncstart /= 2;
		vsyncend /= 2;
		vblankstart /= 2;
		vblankend /= 2;
	}

1167
	enable_mmio(par);
1168 1169
	crtc_unlock(par);
	write3CE(par, CyberControl, 8);
1170 1171 1172 1173 1174
	tmp = 0xEB;
	if (var->sync & FB_SYNC_HOR_HIGH_ACT)
		tmp &= ~0x40;
	if (var->sync & FB_SYNC_VERT_HIGH_ACT)
		tmp &= ~0x80;
L
Linus Torvalds 已提交
1175

1176
	if (par->flatpanel && var->xres < nativex) {
L
Linus Torvalds 已提交
1177 1178 1179 1180 1181
		/*
		 * on flat panels with native size larger
		 * than requested resolution decide whether
		 * we stretch or center
		 */
1182
		t_outb(par, tmp | 0xC0, VGA_MIS_W);
L
Linus Torvalds 已提交
1183

1184
		shadowmode_on(par);
L
Linus Torvalds 已提交
1185

1186
		if (center)
1187
			screen_center(par);
L
Linus Torvalds 已提交
1188
		else if (stretch)
1189
			screen_stretch(par);
L
Linus Torvalds 已提交
1190 1191

	} else {
1192
		t_outb(par, tmp, VGA_MIS_W);
1193
		write3CE(par, CyberControl, 8);
L
Linus Torvalds 已提交
1194 1195 1196
	}

	/* vertical timing values */
1197 1198 1199 1200 1201
	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);
1202
	write3X4(par, VGA_CRTC_V_BLANK_END, vblankend & 0xFF);
L
Linus Torvalds 已提交
1203 1204

	/* horizontal timing values */
1205 1206 1207 1208
	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,
1209
		 (hsyncend & 0x1F) | ((hblankend & 0x20) << 2));
1210
	write3X4(par, VGA_CRTC_H_BLANK_START, hblankstart & 0xFF);
1211
	write3X4(par, VGA_CRTC_H_BLANK_END, hblankend & 0x1F);
L
Linus Torvalds 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222

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

1225 1226
	tmp = read3X4(par, CRTHiOrd) & 0x07;
	tmp |= 0x08;	/* line compare bit 10 */
L
Linus Torvalds 已提交
1227 1228 1229 1230
	if (vtotal & 0x400) tmp |= 0x80;
	if (vblankstart & 0x400) tmp |= 0x40;
	if (vsyncstart & 0x400) tmp |= 0x20;
	if (vdispend & 0x400) tmp |= 0x10;
1231
	write3X4(par, CRTHiOrd, tmp);
L
Linus Torvalds 已提交
1232

1233 1234 1235 1236
	tmp = (htotal >> 8) & 0x01;
	tmp |= (hdispend >> 7) & 0x02;
	tmp |= (hsyncstart >> 5) & 0x08;
	tmp |= (hblankstart >> 4) & 0x10;
1237
	write3X4(par, HorizOverflow, tmp);
1238

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

1244 1245 1246
	write3X4(par, VGA_CRTC_LINE_COMPARE, 0xFF);
	write3X4(par, VGA_CRTC_PRESET_ROW, 0);
	write3X4(par, VGA_CRTC_MODE, 0xC3);
L
Linus Torvalds 已提交
1247

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

1250
	tmp = (info->var.vmode & FB_VMODE_INTERLACED) ? 0x84 : 0x80;
1251 1252
	/* enable access extended memory */
	write3X4(par, CRTCModuleTest, tmp);
1253 1254 1255 1256
	tmp = read3CE(par, MiscIntContReg) & ~0x4;
	if (info->var.vmode & FB_VMODE_INTERLACED)
		tmp |= 0x4;
	write3CE(par, MiscIntContReg, tmp);
L
Linus Torvalds 已提交
1257

1258 1259
	/* enable GE for text acceleration */
	write3X4(par, GraphEngReg, 0x80);
L
Linus Torvalds 已提交
1260 1261

	switch (bpp) {
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
	case 8:
		tmp = 0x00;
		break;
	case 16:
		tmp = 0x05;
		break;
	case 24:
		tmp = 0x29;
		break;
	case 32:
		tmp = 0x09;
		break;
L
Linus Torvalds 已提交
1274 1275
	}

1276
	write3X4(par, PixelBusReg, tmp);
L
Linus Torvalds 已提交
1277

1278 1279 1280
	tmp = read3X4(par, DRAMControl);
	if (!is_oldprotect(par->chip_id))
		tmp |= 0x10;
1281
	if (iscyber(par->chip_id))
1282
		tmp |= 0x20;
1283
	write3X4(par, DRAMControl, tmp);	/* both IO, linear enable */
L
Linus Torvalds 已提交
1284

1285
	write3X4(par, InterfaceSel, read3X4(par, InterfaceSel) | 0x40);
1286 1287
	if (!is_xp(par->chip_id))
		write3X4(par, Performance, read3X4(par, Performance) | 0x10);
1288
	/* MMIO & PCI read and write burst enable */
1289
	if (par->chip_id != TGUI9440 && par->chip_id != IMAGE975)
1290
		write3X4(par, PCIReg, read3X4(par, PCIReg) | 0x06);
L
Linus Torvalds 已提交
1291

1292 1293
	vga_mm_wseq(par->io_virt, 0, 3);
	vga_mm_wseq(par->io_virt, 1, 1); /* set char clock 8 dots wide */
1294
	/* enable 4 maps because needed in chain4 mode */
1295 1296 1297
	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 已提交
1298

1299 1300 1301
	/* convert from picoseconds to kHz */
	vclk = PICOS2KHZ(info->var.pixclock);

1302
	/* divide clock by 2 if 32bpp chain4 mode display and CPU path */
1303
	tmp = read3CE(par, MiscExtFunc) & 0xF0;
1304
	if (bpp == 32 || (par->chip_id == TGUI9440 && bpp == 16)) {
1305
		tmp |= 8;
1306 1307 1308
		vclk *= 2;
	}
	set_vclk(par, vclk);
1309
	write3CE(par, MiscExtFunc, tmp | 0x12);
1310 1311 1312
	write3CE(par, 0x5, 0x40);	/* no CGA compat, allow 256 col */
	write3CE(par, 0x6, 0x05);	/* graphics mode */
	write3CE(par, 0x7, 0x0F);	/* planes? */
L
Linus Torvalds 已提交
1313

1314 1315 1316 1317
	/* 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 已提交
1318

1319 1320
	/* colors */
	for (tmp = 0; tmp < 0x10; tmp++)
1321
		writeAttr(par, tmp, tmp);
1322 1323
	fb_readb(par->io_virt + VGA_IS1_RC);	/* flip-flop to index */
	t_outb(par, 0x20, VGA_ATT_W);		/* enable attr */
L
Linus Torvalds 已提交
1324 1325

	switch (bpp) {
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
	case 8:
		tmp = 0;
		break;
	case 16:
		tmp = 0x30;
		break;
	case 24:
	case 32:
		tmp = 0xD0;
		break;
L
Linus Torvalds 已提交
1336 1337
	}

1338 1339 1340 1341 1342 1343 1344
	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 已提交
1345

1346
	if (par->flatpanel)
1347
		set_number_of_lines(par, info->var.yres);
1348 1349
	info->fix.line_length = info->var.xres_virtual * bpp / 8;
	set_lwidth(par, info->fix.line_length / 8);
1350 1351 1352

	if (!(info->flags & FBINFO_HWACCEL_DISABLED))
		par->init_accel(par, info->var.xres_virtual, bpp);
1353

L
Linus Torvalds 已提交
1354
	info->fix.visual = (bpp == 8) ? FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
1355
	info->cmap.len = (bpp == 8) ? 256 : 16;
L
Linus Torvalds 已提交
1356 1357 1358 1359 1360 1361
	debug("exit\n");
	return 0;
}

/* Set one color register */
static int tridentfb_setcolreg(unsigned regno, unsigned red, unsigned green,
1362 1363
			       unsigned blue, unsigned transp,
			       struct fb_info *info)
L
Linus Torvalds 已提交
1364 1365
{
	int bpp = info->var.bits_per_pixel;
1366
	struct tridentfb_par *par = info->par;
L
Linus Torvalds 已提交
1367 1368 1369 1370

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

1371
	if (bpp == 8) {
1372 1373
		t_outb(par, 0xFF, VGA_PEL_MSK);
		t_outb(par, regno, VGA_PEL_IW);
L
Linus Torvalds 已提交
1374

1375 1376 1377
		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 已提交
1378

1379 1380 1381 1382 1383 1384 1385 1386 1387
	} 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 */
1388
			((u32 *)info->pseudo_palette)[regno] =
1389 1390
				((transp & 0xFF00) << 16)	|
				((red & 0xFF00) << 8)		|
1391
				((green & 0xFF00))		|
1392
				((blue & 0xFF00) >> 8);
1393
	}
L
Linus Torvalds 已提交
1394 1395 1396 1397

	return 0;
}

1398
/* Try blanking the screen. For flat panels it does nothing */
L
Linus Torvalds 已提交
1399 1400
static int tridentfb_blank(int blank_mode, struct fb_info *info)
{
1401
	unsigned char PMCont, DPMSCont;
1402
	struct tridentfb_par *par = info->par;
L
Linus Torvalds 已提交
1403 1404

	debug("enter\n");
1405
	if (par->flatpanel)
L
Linus Torvalds 已提交
1406
		return 0;
1407 1408 1409
	t_outb(par, 0x04, 0x83C8); /* Read DPMS Control */
	PMCont = t_inb(par, 0x83C6) & 0xFC;
	DPMSCont = read3CE(par, PowerStatus) & 0xFC;
1410
	switch (blank_mode) {
L
Linus Torvalds 已提交
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
	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;
1433
	}
L
Linus Torvalds 已提交
1434

1435 1436 1437
	write3CE(par, PowerStatus, DPMSCont);
	t_outb(par, 4, 0x83C8);
	t_outb(par, PMCont, 0x83C6);
L
Linus Torvalds 已提交
1438 1439 1440 1441 1442 1443 1444

	debug("exit\n");

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

1445 1446 1447 1448 1449 1450 1451 1452 1453
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,
1454
	.fb_imageblit = tridentfb_imageblit,
1455
	.fb_sync = tridentfb_sync,
1456 1457
};

1458 1459
static int trident_pci_probe(struct pci_dev *dev,
			     const struct pci_device_id *id)
L
Linus Torvalds 已提交
1460 1461 1462
{
	int err;
	unsigned char revision;
1463 1464
	struct fb_info *info;
	struct tridentfb_par *default_par;
1465 1466
	int chip3D;
	int chip_id;
O
Ondrej Zary 已提交
1467
	bool found = false;
L
Linus Torvalds 已提交
1468 1469 1470 1471 1472

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

1473 1474 1475 1476 1477
	info = framebuffer_alloc(sizeof(struct tridentfb_par), &dev->dev);
	if (!info)
		return -ENOMEM;
	default_par = info->par;

L
Linus Torvalds 已提交
1478 1479 1480
	chip_id = id->device;

	/* If PCI id is 0x9660 then further detect chip type */
1481

L
Linus Torvalds 已提交
1482
	if (chip_id == TGUI9660) {
1483
		revision = vga_io_rseq(RevisionID);
1484

L
Linus Torvalds 已提交
1485
		switch (revision) {
1486 1487 1488
		case 0x21:
			chip_id = PROVIDIA9685;
			break;
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
		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 已提交
1513 1514 1515 1516 1517 1518
		}
	}

	chip3D = is3Dchip(chip_id);

	if (is_xp(chip_id)) {
1519 1520 1521 1522
		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;
1523
		tridentfb_fix.accel = FB_ACCEL_TRIDENT_BLADEXP;
1524
	} else if (is_blade(chip_id)) {
1525 1526 1527 1528
		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;
1529
		default_par->image_blit = blade_image_blit;
1530
		tridentfb_fix.accel = FB_ACCEL_TRIDENT_BLADE3D;
1531
	} else if (chip3D) {			/* 3DImage family left */
1532 1533 1534 1535
		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;
1536
		tridentfb_fix.accel = FB_ACCEL_TRIDENT_3DIMAGE;
1537 1538 1539 1540 1541
	} else { 				/* TGUI 9440/96XX family */
		default_par->init_accel = tgui_init_accel;
		default_par->wait_engine = xp_wait_engine;
		default_par->fill_rect = tgui_fill_rect;
		default_par->copy_rect = tgui_copy_rect;
1542
		tridentfb_fix.accel = FB_ACCEL_TRIDENT_TGUI;
L
Linus Torvalds 已提交
1543 1544
	}

1545 1546
	default_par->chip_id = chip_id;

L
Linus Torvalds 已提交
1547
	/* setup MMIO region */
1548
	tridentfb_fix.mmio_start = pci_resource_start(dev, 1);
1549
	tridentfb_fix.mmio_len = pci_resource_len(dev, 1);
L
Linus Torvalds 已提交
1550

1551 1552
	if (!request_mem_region(tridentfb_fix.mmio_start,
				tridentfb_fix.mmio_len, "tridentfb")) {
L
Linus Torvalds 已提交
1553
		debug("request_region failed!\n");
1554
		framebuffer_release(info);
L
Linus Torvalds 已提交
1555 1556 1557
		return -1;
	}

1558 1559
	default_par->io_virt = ioremap_nocache(tridentfb_fix.mmio_start,
					       tridentfb_fix.mmio_len);
L
Linus Torvalds 已提交
1560

1561
	if (!default_par->io_virt) {
L
Linus Torvalds 已提交
1562
		debug("ioremap failed\n");
1563 1564
		err = -1;
		goto out_unmap1;
L
Linus Torvalds 已提交
1565 1566
	}

1567
	enable_mmio(default_par);
1568

L
Linus Torvalds 已提交
1569
	/* setup framebuffer memory */
1570
	tridentfb_fix.smem_start = pci_resource_start(dev, 0);
1571
	tridentfb_fix.smem_len = get_memsize(default_par);
1572

1573 1574
	if (!request_mem_region(tridentfb_fix.smem_start,
				tridentfb_fix.smem_len, "tridentfb")) {
L
Linus Torvalds 已提交
1575
		debug("request_mem_region failed!\n");
1576
		disable_mmio(info->par);
1577
		err = -1;
1578
		goto out_unmap1;
L
Linus Torvalds 已提交
1579 1580
	}

1581 1582
	info->screen_base = ioremap_nocache(tridentfb_fix.smem_start,
					    tridentfb_fix.smem_len);
L
Linus Torvalds 已提交
1583

1584
	if (!info->screen_base) {
L
Linus Torvalds 已提交
1585
		debug("ioremap failed\n");
1586
		err = -1;
1587
		goto out_unmap2;
L
Linus Torvalds 已提交
1588 1589
	}

1590
	default_par->flatpanel = is_flatpanel(default_par);
L
Linus Torvalds 已提交
1591

1592
	if (default_par->flatpanel)
1593
		nativex = get_nativex(default_par);
L
Linus Torvalds 已提交
1594

1595 1596
	info->fix = tridentfb_fix;
	info->fbops = &tridentfb_ops;
1597
	info->pseudo_palette = default_par->pseudo_pal;
L
Linus Torvalds 已提交
1598

1599
	info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
1600 1601 1602 1603 1604 1605 1606
	if (!noaccel && default_par->init_accel) {
		info->flags &= ~FBINFO_HWACCEL_DISABLED;
		info->flags |= FBINFO_HWACCEL_COPYAREA;
		info->flags |= FBINFO_HWACCEL_FILLRECT;
	} else
		info->flags |= FBINFO_HWACCEL_DISABLED;

K
Krzysztof Helt 已提交
1607 1608 1609
	if (is_blade(chip_id) && chip_id != BLADE3D)
		info->flags |= FBINFO_READS_FAST;

1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
	info->pixmap.addr = kmalloc(4096, GFP_KERNEL);
	if (!info->pixmap.addr) {
		err = -ENOMEM;
		goto out_unmap2;
	}

	info->pixmap.size = 4096;
	info->pixmap.buf_align = 4;
	info->pixmap.scan_align = 1;
	info->pixmap.access_align = 32;
	info->pixmap.flags = FB_PIXMAP_SYSTEM;
O
Ondrej Zary 已提交
1621
	info->var.bits_per_pixel = 8;
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633

	if (default_par->image_blit) {
		info->flags |= FBINFO_HWACCEL_IMAGEBLIT;
		info->pixmap.scan_align = 4;
	}

	if (noaccel) {
		printk(KERN_DEBUG "disabling acceleration\n");
		info->flags |= FBINFO_HWACCEL_DISABLED;
		info->pixmap.scan_align = 1;
	}

O
Ondrej Zary 已提交
1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
	if (tridentfb_setup_ddc_bus(info) == 0) {
		u8 *edid = fb_ddc_read(&default_par->ddc_adapter);

		default_par->ddc_registered = true;
		if (edid) {
			fb_edid_to_monspecs(edid, &info->monspecs);
			kfree(edid);
			if (!info->monspecs.modedb)
				dev_err(info->device, "error getting mode database\n");
			else {
				const struct fb_videomode *m;

				fb_videomode_to_modelist(info->monspecs.modedb,
						 info->monspecs.modedb_len,
						 &info->modelist);
				m = fb_find_best_display(&info->monspecs,
							 &info->modelist);
				if (m) {
					fb_videomode_to_var(&info->var, m);
					/* fill all other info->var's fields */
					if (tridentfb_check_var(&info->var,
								info) == 0)
						found = true;
				}
			}
		}
1660
	}
O
Ondrej Zary 已提交
1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683

	if (!mode_option && !found)
		mode_option = "640x480-8@60";

	/* Prepare startup mode */
	if (mode_option) {
		err = fb_find_mode(&info->var, info, mode_option,
				   info->monspecs.modedb,
				   info->monspecs.modedb_len,
				   NULL, info->var.bits_per_pixel);
		if (!err || err == 4) {
			err = -EINVAL;
			dev_err(info->device, "mode %s not found\n",
								mode_option);
			fb_destroy_modedb(info->monspecs.modedb);
			info->monspecs.modedb = NULL;
			goto out_unmap2;
		}
	}

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

1684
	err = fb_alloc_cmap(&info->cmap, 256, 0);
1685 1686 1687
	if (err < 0)
		goto out_unmap2;

1688
	info->var.activate |= FB_ACTIVATE_NOW;
1689 1690
	info->device = &dev->dev;
	if (register_framebuffer(info) < 0) {
1691
		printk(KERN_ERR "tridentfb: could not register framebuffer\n");
1692
		fb_dealloc_cmap(&info->cmap);
1693
		err = -EINVAL;
1694
		goto out_unmap2;
L
Linus Torvalds 已提交
1695 1696
	}
	output("fb%d: %s frame buffer device %dx%d-%dbpp\n",
1697 1698
	   info->node, info->fix.id, info->var.xres,
	   info->var.yres, info->var.bits_per_pixel);
1699 1700

	pci_set_drvdata(dev, info);
L
Linus Torvalds 已提交
1701
	return 0;
1702

1703
out_unmap2:
O
Ondrej Zary 已提交
1704 1705
	if (default_par->ddc_registered)
		i2c_del_adapter(&default_par->ddc_adapter);
1706
	kfree(info->pixmap.addr);
1707 1708
	if (info->screen_base)
		iounmap(info->screen_base);
1709
	release_mem_region(tridentfb_fix.smem_start, tridentfb_fix.smem_len);
1710
	disable_mmio(info->par);
1711
out_unmap1:
1712 1713
	if (default_par->io_virt)
		iounmap(default_par->io_virt);
1714
	release_mem_region(tridentfb_fix.mmio_start, tridentfb_fix.mmio_len);
1715
	framebuffer_release(info);
1716
	return err;
L
Linus Torvalds 已提交
1717 1718
}

1719
static void trident_pci_remove(struct pci_dev *dev)
L
Linus Torvalds 已提交
1720
{
1721 1722 1723 1724
	struct fb_info *info = pci_get_drvdata(dev);
	struct tridentfb_par *par = info->par;

	unregister_framebuffer(info);
O
Ondrej Zary 已提交
1725 1726
	if (par->ddc_registered)
		i2c_del_adapter(&par->ddc_adapter);
L
Linus Torvalds 已提交
1727
	iounmap(par->io_virt);
1728
	iounmap(info->screen_base);
L
Linus Torvalds 已提交
1729
	release_mem_region(tridentfb_fix.smem_start, tridentfb_fix.smem_len);
1730
	release_mem_region(tridentfb_fix.mmio_start, tridentfb_fix.mmio_len);
1731
	kfree(info->pixmap.addr);
1732
	fb_dealloc_cmap(&info->cmap);
1733
	framebuffer_release(info);
L
Linus Torvalds 已提交
1734 1735 1736
}

/* List of boards that we are trying to support */
1737
static const struct pci_device_id trident_devices[] = {
1738 1739 1740 1741 1742 1743 1744 1745
	{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},
1746
	{PCI_VENDOR_ID_TRIDENT,	TGUI9440, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
	{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 已提交
1759
	{0,}
1760 1761 1762
};

MODULE_DEVICE_TABLE(pci, trident_devices);
L
Linus Torvalds 已提交
1763 1764

static struct pci_driver tridentfb_pci_driver = {
1765 1766 1767
	.name = "tridentfb",
	.id_table = trident_devices,
	.probe = trident_pci_probe,
1768
	.remove = trident_pci_remove,
L
Linus Torvalds 已提交
1769 1770 1771 1772 1773
};

/*
 * Parse user specified options (`video=trident:')
 * example:
1774
 *	video=trident:800x600,bpp=16,noaccel
L
Linus Torvalds 已提交
1775 1776
 */
#ifndef MODULE
1777
static int __init tridentfb_setup(char *options)
L
Linus Torvalds 已提交
1778
{
1779
	char *opt;
L
Linus Torvalds 已提交
1780 1781
	if (!options || !*options)
		return 0;
1782 1783 1784 1785
	while ((opt = strsep(&options, ",")) != NULL) {
		if (!*opt)
			continue;
		if (!strncmp(opt, "noaccel", 7))
L
Linus Torvalds 已提交
1786
			noaccel = 1;
1787
		else if (!strncmp(opt, "fp", 2))
1788
			fp = 1;
1789
		else if (!strncmp(opt, "crt", 3))
1790
			fp = 0;
1791 1792 1793
		else if (!strncmp(opt, "bpp=", 4))
			bpp = simple_strtoul(opt + 4, NULL, 0);
		else if (!strncmp(opt, "center", 6))
L
Linus Torvalds 已提交
1794
			center = 1;
1795
		else if (!strncmp(opt, "stretch", 7))
L
Linus Torvalds 已提交
1796
			stretch = 1;
1797 1798 1799 1800 1801 1802
		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 已提交
1803
		else
1804
			mode_option = opt;
L
Linus Torvalds 已提交
1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
	}
	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
	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");
K
Krzysztof Helt 已提交
1833
MODULE_ALIAS("cyblafb");
L
Linus Torvalds 已提交
1834