pm2fb.c 49.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Permedia2 framebuffer driver.
 *
 * 2.5/2.6 driver:
 * Copyright (c) 2003 Jim Hague (jim.hague@acm.org)
 *
 * based on 2.4 driver:
 * Copyright (c) 1998-2000 Ilario Nardinocchi (nardinoc@CS.UniBO.IT)
 * Copyright (c) 1999 Jakub Jelinek (jakub@redhat.com)
 *
 * and additional input from James Simmon's port of Hannu Mallat's tdfx
 * driver.
 *
K
Krzysztof Helt 已提交
14
 * I have a Creative Graphics Blaster Exxtreme card - pm2fb on x86. I
L
Linus Torvalds 已提交
15 16 17 18 19 20 21 22 23 24 25 26
 * have no access to other pm2fb implementations. Sparc (and thus
 * hopefully other big-endian) devices now work, thanks to a lot of
 * testing work by Ron Murray. I have no access to CVision hardware,
 * and therefore for now I am omitting the CVision code.
 *
 * Multiple boards support has been on the TODO list for ages.
 * Don't expect this to change.
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License. See the file COPYING in the main directory of this archive for
 * more details.
 *
K
Krzysztof Helt 已提交
27
 *
L
Linus Torvalds 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/fb.h>
#include <linux/init.h>
#include <linux/pci.h>
41 42 43
#ifdef CONFIG_MTRR
#include <asm/mtrr.h>
#endif
L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57

#include <video/permedia2.h>
#include <video/cvisionppc.h>

#if !defined(__LITTLE_ENDIAN) && !defined(__BIG_ENDIAN)
#error	"The endianness of the target host has not been defined."
#endif

#if !defined(CONFIG_PCI)
#error "Only generic PCI cards supported."
#endif

#undef PM2FB_MASTER_DEBUG
#ifdef PM2FB_MASTER_DEBUG
K
Krzysztof Helt 已提交
58
#define DPRINTK(a, b...)	\
59
	printk(KERN_DEBUG "pm2fb: %s: " a, __func__ , ## b)
L
Linus Torvalds 已提交
60
#else
K
Krzysztof Helt 已提交
61
#define DPRINTK(a, b...)
L
Linus Torvalds 已提交
62 63
#endif

K
Krzysztof Helt 已提交
64 65
#define PM2_PIXMAP_SIZE	(1600 * 4)

L
Linus Torvalds 已提交
66
/*
K
Krzysztof Helt 已提交
67
 * Driver data
L
Linus Torvalds 已提交
68
 */
69
static int hwcursor = 1;
70
static char *mode_option __devinitdata;
L
Linus Torvalds 已提交
71 72 73 74 75 76 77 78 79 80

/*
 * The XFree GLINT driver will (I think to implement hardware cursor
 * support on TVP4010 and similar where there is no RAMDAC - see
 * comment in set_video) always request +ve sync regardless of what
 * the mode requires. This screws me because I have a Sun
 * fixed-frequency monitor which absolutely has to have -ve sync. So
 * these flags allow the user to specify that requests for +ve sync
 * should be silently turned in -ve sync.
 */
81 82
static int lowhsync;
static int lowvsync;
83 84 85 86 87
static int noaccel __devinitdata;
/* mtrr option */
#ifdef CONFIG_MTRR
static int nomtrr __devinitdata;
#endif
L
Linus Torvalds 已提交
88 89 90 91 92 93 94 95 96

/*
 * The hardware state of the graphics card that isn't part of the
 * screeninfo.
 */
struct pm2fb_par
{
	pm2type_t	type;		/* Board type */
	unsigned char	__iomem *v_regs;/* virtual address of p_regs */
K
Krzysztof Helt 已提交
97
	u32		memclock;	/* memclock */
L
Linus Torvalds 已提交
98 99 100 101
	u32		video;		/* video flags before blanking */
	u32		mem_config;	/* MemConfig reg at probe */
	u32		mem_control;	/* MemControl reg at probe */
	u32		boot_address;	/* BootAddress reg at probe */
K
Krzysztof Helt 已提交
102
	u32		palette[16];
103
	int		mtrr_handle;
L
Linus Torvalds 已提交
104 105 106 107 108 109 110
};

/*
 * Here we define the default structs fb_fix_screeninfo and fb_var_screeninfo
 * if we don't use modedb.
 */
static struct fb_fix_screeninfo pm2fb_fix __devinitdata = {
K
Krzysztof Helt 已提交
111
	.id =		"",
L
Linus Torvalds 已提交
112 113 114 115
	.type =		FB_TYPE_PACKED_PIXELS,
	.visual =	FB_VISUAL_PSEUDOCOLOR,
	.xpanstep =	1,
	.ypanstep =	1,
K
Krzysztof Helt 已提交
116
	.ywrapstep =	0,
117
	.accel =	FB_ACCEL_3DLABS_PERMEDIA2,
L
Linus Torvalds 已提交
118 119 120 121 122 123 124
};

/*
 * Default video mode. In case the modedb doesn't work.
 */
static struct fb_var_screeninfo pm2fb_var __devinitdata = {
	/* "640x480, 8 bpp @ 60 Hz */
K
Krzysztof Helt 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	.xres =			640,
	.yres =			480,
	.xres_virtual =		640,
	.yres_virtual =		480,
	.bits_per_pixel =	8,
	.red =			{0, 8, 0},
	.blue =			{0, 8, 0},
	.green =		{0, 8, 0},
	.activate =		FB_ACTIVATE_NOW,
	.height =		-1,
	.width =		-1,
	.accel_flags =		0,
	.pixclock =		39721,
	.left_margin =		40,
	.right_margin =		24,
	.upper_margin =		32,
	.lower_margin =		11,
	.hsync_len =		96,
	.vsync_len =		2,
	.vmode =		FB_VMODE_NONINTERLACED
L
Linus Torvalds 已提交
145 146 147 148 149 150
};

/*
 * Utility functions
 */

K
Krzysztof Helt 已提交
151
static inline u32 pm2_RD(struct pm2fb_par *p, s32 off)
L
Linus Torvalds 已提交
152
{
K
Krzysztof Helt 已提交
153
	return fb_readl(p->v_regs + off);
L
Linus Torvalds 已提交
154 155
}

K
Krzysztof Helt 已提交
156
static inline void pm2_WR(struct pm2fb_par *p, s32 off, u32 v)
L
Linus Torvalds 已提交
157
{
K
Krzysztof Helt 已提交
158
	fb_writel(v, p->v_regs + off);
L
Linus Torvalds 已提交
159 160
}

K
Krzysztof Helt 已提交
161
static inline u32 pm2_RDAC_RD(struct pm2fb_par *p, s32 idx)
L
Linus Torvalds 已提交
162
{
K
Krzysztof Helt 已提交
163
	pm2_WR(p, PM2R_RD_PALETTE_WRITE_ADDRESS, idx);
L
Linus Torvalds 已提交
164
	mb();
K
Krzysztof Helt 已提交
165 166 167
	return pm2_RD(p, PM2R_RD_INDEXED_DATA);
}

K
Krzysztof Helt 已提交
168
static inline u32 pm2v_RDAC_RD(struct pm2fb_par *p, s32 idx)
K
Krzysztof Helt 已提交
169 170 171 172
{
	pm2_WR(p, PM2VR_RD_INDEX_LOW, idx & 0xff);
	mb();
	return pm2_RD(p,  PM2VR_RD_INDEXED_DATA);
L
Linus Torvalds 已提交
173 174
}

K
Krzysztof Helt 已提交
175
static inline void pm2_RDAC_WR(struct pm2fb_par *p, s32 idx, u32 v)
L
Linus Torvalds 已提交
176
{
K
Krzysztof Helt 已提交
177
	pm2_WR(p, PM2R_RD_PALETTE_WRITE_ADDRESS, idx);
178
	wmb();
K
Krzysztof Helt 已提交
179
	pm2_WR(p, PM2R_RD_INDEXED_DATA, v);
180
	wmb();
L
Linus Torvalds 已提交
181 182
}

K
Krzysztof Helt 已提交
183
static inline void pm2v_RDAC_WR(struct pm2fb_par *p, s32 idx, u32 v)
L
Linus Torvalds 已提交
184 185
{
	pm2_WR(p, PM2VR_RD_INDEX_LOW, idx & 0xff);
186
	wmb();
L
Linus Torvalds 已提交
187
	pm2_WR(p, PM2VR_RD_INDEXED_DATA, v);
188
	wmb();
L
Linus Torvalds 已提交
189 190 191
}

#ifdef CONFIG_FB_PM2_FIFO_DISCONNECT
K
Krzysztof Helt 已提交
192
#define WAIT_FIFO(p, a)
L
Linus Torvalds 已提交
193
#else
K
Krzysztof Helt 已提交
194
static inline void WAIT_FIFO(struct pm2fb_par *p, u32 a)
L
Linus Torvalds 已提交
195
{
196 197
	while (pm2_RD(p, PM2R_IN_FIFO_SPACE) < a)
		cpu_relax();
L
Linus Torvalds 已提交
198 199 200 201 202 203
}
#endif

/*
 * partial products for the supported horizontal resolutions.
 */
K
Krzysztof Helt 已提交
204
#define PACKPP(p0, p1, p2)	(((p2) << 6) | ((p1) << 3) | (p0))
L
Linus Torvalds 已提交
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
static const struct {
	u16 width;
	u16 pp;
} pp_table[] = {
	{ 32,	PACKPP(1, 0, 0) }, { 64,	PACKPP(1, 1, 0) },
	{ 96,	PACKPP(1, 1, 1) }, { 128,	PACKPP(2, 1, 1) },
	{ 160,	PACKPP(2, 2, 1) }, { 192,	PACKPP(2, 2, 2) },
	{ 224,	PACKPP(3, 2, 1) }, { 256,	PACKPP(3, 2, 2) },
	{ 288,	PACKPP(3, 3, 1) }, { 320,	PACKPP(3, 3, 2) },
	{ 384,	PACKPP(3, 3, 3) }, { 416,	PACKPP(4, 3, 1) },
	{ 448,	PACKPP(4, 3, 2) }, { 512,	PACKPP(4, 3, 3) },
	{ 544,	PACKPP(4, 4, 1) }, { 576,	PACKPP(4, 4, 2) },
	{ 640,	PACKPP(4, 4, 3) }, { 768,	PACKPP(4, 4, 4) },
	{ 800,	PACKPP(5, 4, 1) }, { 832,	PACKPP(5, 4, 2) },
	{ 896,	PACKPP(5, 4, 3) }, { 1024,	PACKPP(5, 4, 4) },
	{ 1056,	PACKPP(5, 5, 1) }, { 1088,	PACKPP(5, 5, 2) },
	{ 1152,	PACKPP(5, 5, 3) }, { 1280,	PACKPP(5, 5, 4) },
	{ 1536,	PACKPP(5, 5, 5) }, { 1568,	PACKPP(6, 5, 1) },
	{ 1600,	PACKPP(6, 5, 2) }, { 1664,	PACKPP(6, 5, 3) },
	{ 1792,	PACKPP(6, 5, 4) }, { 2048,	PACKPP(6, 5, 5) },
	{ 0,	0 } };

static u32 partprod(u32 xres)
{
	int i;

	for (i = 0; pp_table[i].width && pp_table[i].width != xres; i++)
		;
K
Krzysztof Helt 已提交
233
	if (pp_table[i].width == 0)
L
Linus Torvalds 已提交
234 235 236 237 238 239 240
		DPRINTK("invalid width %u\n", xres);
	return pp_table[i].pp;
}

static u32 to3264(u32 timing, int bpp, int is64)
{
	switch (bpp) {
K
Krzysztof Helt 已提交
241 242
	case 24:
		timing *= 3;
L
Linus Torvalds 已提交
243
	case 8:
K
Krzysztof Helt 已提交
244
		timing >>= 1;
L
Linus Torvalds 已提交
245
	case 16:
K
Krzysztof Helt 已提交
246
		timing >>= 1;
L
Linus Torvalds 已提交
247 248 249
	case 32:
		break;
	}
K
Krzysztof Helt 已提交
250 251
	if (is64)
		timing >>= 1;
L
Linus Torvalds 已提交
252 253 254
	return timing;
}

K
Krzysztof Helt 已提交
255 256
static void pm2_mnp(u32 clk, unsigned char *mm, unsigned char *nn,
		    unsigned char *pp)
L
Linus Torvalds 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269
{
	unsigned char m;
	unsigned char n;
	unsigned char p;
	u32 f;
	s32 curr;
	s32 delta = 100000;

	*mm = *nn = *pp = 0;
	for (n = 2; n < 15; n++) {
		for (m = 2; m; m++) {
			f = PM2_REFERENCE_CLOCK * m / n;
			if (f >= 150000 && f <= 300000) {
K
Krzysztof Helt 已提交
270 271 272 273 274 275 276
				for (p = 0; p < 5; p++, f >>= 1) {
					curr = (clk > f) ? clk - f : f - clk;
					if (curr < delta) {
						delta = curr;
						*mm = m;
						*nn = n;
						*pp = p;
L
Linus Torvalds 已提交
277 278 279 280 281 282 283
					}
				}
			}
		}
	}
}

K
Krzysztof Helt 已提交
284 285
static void pm2v_mnp(u32 clk, unsigned char *mm, unsigned char *nn,
		     unsigned char *pp)
L
Linus Torvalds 已提交
286 287 288 289 290 291 292 293
{
	unsigned char m;
	unsigned char n;
	unsigned char p;
	u32 f;
	s32 delta = 1000;

	*mm = *nn = *pp = 0;
K
Krzysztof Helt 已提交
294
	for (m = 1; m < 128; m++) {
295
		for (n = 2 * m + 1; n; n++) {
K
Krzysztof Helt 已提交
296 297 298 299 300 301 302
			for (p = 0; p < 2; p++) {
				f = (PM2_REFERENCE_CLOCK >> (p + 1)) * n / m;
				if (clk > f - delta && clk < f + delta) {
					delta = (clk > f) ? clk - f : f - clk;
					*mm = m;
					*nn = n;
					*pp = p;
L
Linus Torvalds 已提交
303 304 305 306 307 308
				}
			}
		}
	}
}

K
Krzysztof Helt 已提交
309 310
static void clear_palette(struct pm2fb_par *p)
{
K
Krzysztof Helt 已提交
311
	int i = 256;
L
Linus Torvalds 已提交
312 313 314 315 316 317 318 319 320 321 322 323

	WAIT_FIFO(p, 1);
	pm2_WR(p, PM2R_RD_PALETTE_WRITE_ADDRESS, 0);
	wmb();
	while (i--) {
		WAIT_FIFO(p, 3);
		pm2_WR(p, PM2R_RD_PALETTE_DATA, 0);
		pm2_WR(p, PM2R_RD_PALETTE_DATA, 0);
		pm2_WR(p, PM2R_RD_PALETTE_DATA, 0);
	}
}

K
Krzysztof Helt 已提交
324
static void reset_card(struct pm2fb_par *p)
L
Linus Torvalds 已提交
325 326 327 328 329 330
{
	if (p->type == PM2_TYPE_PERMEDIA2V)
		pm2_WR(p, PM2VR_RD_INDEX_HIGH, 0);
	pm2_WR(p, PM2R_RESET_STATUS, 0);
	mb();
	while (pm2_RD(p, PM2R_RESET_STATUS) & PM2F_BEING_RESET)
331
		cpu_relax();
L
Linus Torvalds 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
	mb();
#ifdef CONFIG_FB_PM2_FIFO_DISCONNECT
	DPRINTK("FIFO disconnect enabled\n");
	pm2_WR(p, PM2R_FIFO_DISCON, 1);
	mb();
#endif

	/* Restore stashed memory config information from probe */
	WAIT_FIFO(p, 3);
	pm2_WR(p, PM2R_MEM_CONTROL, p->mem_control);
	pm2_WR(p, PM2R_BOOT_ADDRESS, p->boot_address);
	wmb();
	pm2_WR(p, PM2R_MEM_CONFIG, p->mem_config);
}

K
Krzysztof Helt 已提交
347
static void reset_config(struct pm2fb_par *p)
L
Linus Torvalds 已提交
348
{
349
	WAIT_FIFO(p, 53);
K
Krzysztof Helt 已提交
350
	pm2_WR(p, PM2R_CHIP_CONFIG, pm2_RD(p, PM2R_CHIP_CONFIG) &
K
Krzysztof Helt 已提交
351
			~(PM2F_VGA_ENABLE | PM2F_VGA_FIXED));
L
Linus Torvalds 已提交
352 353 354 355 356 357 358 359
	pm2_WR(p, PM2R_BYPASS_WRITE_MASK, ~(0L));
	pm2_WR(p, PM2R_FRAMEBUFFER_WRITE_MASK, ~(0L));
	pm2_WR(p, PM2R_FIFO_CONTROL, 0);
	pm2_WR(p, PM2R_APERTURE_ONE, 0);
	pm2_WR(p, PM2R_APERTURE_TWO, 0);
	pm2_WR(p, PM2R_RASTERIZER_MODE, 0);
	pm2_WR(p, PM2R_DELTA_MODE, PM2F_DELTA_ORDER_RGB);
	pm2_WR(p, PM2R_LB_READ_FORMAT, 0);
K
Krzysztof Helt 已提交
360
	pm2_WR(p, PM2R_LB_WRITE_FORMAT, 0);
L
Linus Torvalds 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
	pm2_WR(p, PM2R_LB_READ_MODE, 0);
	pm2_WR(p, PM2R_LB_SOURCE_OFFSET, 0);
	pm2_WR(p, PM2R_FB_SOURCE_OFFSET, 0);
	pm2_WR(p, PM2R_FB_PIXEL_OFFSET, 0);
	pm2_WR(p, PM2R_FB_WINDOW_BASE, 0);
	pm2_WR(p, PM2R_LB_WINDOW_BASE, 0);
	pm2_WR(p, PM2R_FB_SOFT_WRITE_MASK, ~(0L));
	pm2_WR(p, PM2R_FB_HARD_WRITE_MASK, ~(0L));
	pm2_WR(p, PM2R_FB_READ_PIXEL, 0);
	pm2_WR(p, PM2R_DITHER_MODE, 0);
	pm2_WR(p, PM2R_AREA_STIPPLE_MODE, 0);
	pm2_WR(p, PM2R_DEPTH_MODE, 0);
	pm2_WR(p, PM2R_STENCIL_MODE, 0);
	pm2_WR(p, PM2R_TEXTURE_ADDRESS_MODE, 0);
	pm2_WR(p, PM2R_TEXTURE_READ_MODE, 0);
	pm2_WR(p, PM2R_TEXEL_LUT_MODE, 0);
	pm2_WR(p, PM2R_YUV_MODE, 0);
	pm2_WR(p, PM2R_COLOR_DDA_MODE, 0);
	pm2_WR(p, PM2R_TEXTURE_COLOR_MODE, 0);
	pm2_WR(p, PM2R_FOG_MODE, 0);
	pm2_WR(p, PM2R_ALPHA_BLEND_MODE, 0);
	pm2_WR(p, PM2R_LOGICAL_OP_MODE, 0);
	pm2_WR(p, PM2R_STATISTICS_MODE, 0);
	pm2_WR(p, PM2R_SCISSOR_MODE, 0);
	pm2_WR(p, PM2R_FILTER_MODE, PM2F_SYNCHRONIZATION);
386
	pm2_WR(p, PM2R_RD_PIXEL_MASK, 0xff);
L
Linus Torvalds 已提交
387 388 389 390 391
	switch (p->type) {
	case PM2_TYPE_PERMEDIA2:
		pm2_RDAC_WR(p, PM2I_RD_MODE_CONTROL, 0); /* no overlay */
		pm2_RDAC_WR(p, PM2I_RD_CURSOR_CONTROL, 0);
		pm2_RDAC_WR(p, PM2I_RD_MISC_CONTROL, PM2F_RD_PALETTE_WIDTH_8);
K
Krzysztof Helt 已提交
392 393 394 395 396
		pm2_RDAC_WR(p, PM2I_RD_COLOR_KEY_CONTROL, 0);
		pm2_RDAC_WR(p, PM2I_RD_OVERLAY_KEY, 0);
		pm2_RDAC_WR(p, PM2I_RD_RED_KEY, 0);
		pm2_RDAC_WR(p, PM2I_RD_GREEN_KEY, 0);
		pm2_RDAC_WR(p, PM2I_RD_BLUE_KEY, 0);
L
Linus Torvalds 已提交
397 398 399 400 401 402 403
		break;
	case PM2_TYPE_PERMEDIA2V:
		pm2v_RDAC_WR(p, PM2VI_RD_MISC_CONTROL, 1); /* 8bit */
		break;
	}
}

K
Krzysztof Helt 已提交
404
static void set_aperture(struct pm2fb_par *p, u32 depth)
L
Linus Torvalds 已提交
405 406 407 408 409 410
{
	/*
	 * The hardware is little-endian. When used in big-endian
	 * hosts, the on-chip aperture settings are used where
	 * possible to translate from host to card byte order.
	 */
K
Krzysztof Helt 已提交
411
	WAIT_FIFO(p, 2);
L
Linus Torvalds 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
#ifdef __LITTLE_ENDIAN
	pm2_WR(p, PM2R_APERTURE_ONE, PM2F_APERTURE_STANDARD);
#else
	switch (depth) {
	case 24:	/* RGB->BGR */
		/*
		 * We can't use the aperture to translate host to
		 * card byte order here, so we switch to BGR mode
		 * in pm2fb_set_par().
		 */
	case 8:		/* B->B */
		pm2_WR(p, PM2R_APERTURE_ONE, PM2F_APERTURE_STANDARD);
		break;
	case 16:	/* HL->LH */
		pm2_WR(p, PM2R_APERTURE_ONE, PM2F_APERTURE_HALFWORDSWAP);
		break;
	case 32:	/* RGBA->ABGR */
		pm2_WR(p, PM2R_APERTURE_ONE, PM2F_APERTURE_BYTESWAP);
		break;
	}
#endif

K
Krzysztof Helt 已提交
434
	/* We don't use aperture two, so this may be superflous */
L
Linus Torvalds 已提交
435 436 437
	pm2_WR(p, PM2R_APERTURE_TWO, PM2F_APERTURE_STANDARD);
}

K
Krzysztof Helt 已提交
438
static void set_color(struct pm2fb_par *p, unsigned char regno,
L
Linus Torvalds 已提交
439 440 441 442 443 444 445 446 447 448 449 450
		      unsigned char r, unsigned char g, unsigned char b)
{
	WAIT_FIFO(p, 4);
	pm2_WR(p, PM2R_RD_PALETTE_WRITE_ADDRESS, regno);
	wmb();
	pm2_WR(p, PM2R_RD_PALETTE_DATA, r);
	wmb();
	pm2_WR(p, PM2R_RD_PALETTE_DATA, g);
	wmb();
	pm2_WR(p, PM2R_RD_PALETTE_DATA, b);
}

K
Krzysztof Helt 已提交
451
static void set_memclock(struct pm2fb_par *par, u32 clk)
L
Linus Torvalds 已提交
452 453 454 455
{
	int i;
	unsigned char m, n, p;

456 457 458
	switch (par->type) {
	case PM2_TYPE_PERMEDIA2V:
		pm2v_mnp(clk/2, &m, &n, &p);
K
Krzysztof Helt 已提交
459
		WAIT_FIFO(par, 12);
460 461 462 463 464 465 466
		pm2_WR(par, PM2VR_RD_INDEX_HIGH, PM2VI_RD_MCLK_CONTROL >> 8);
		pm2v_RDAC_WR(par, PM2VI_RD_MCLK_CONTROL, 0);
		pm2v_RDAC_WR(par, PM2VI_RD_MCLK_PRESCALE, m);
		pm2v_RDAC_WR(par, PM2VI_RD_MCLK_FEEDBACK, n);
		pm2v_RDAC_WR(par, PM2VI_RD_MCLK_POSTSCALE, p);
		pm2v_RDAC_WR(par, PM2VI_RD_MCLK_CONTROL, 1);
		rmb();
K
Krzysztof Helt 已提交
467 468 469
		for (i = 256; i; i--)
			if (pm2v_RDAC_RD(par, PM2VI_RD_MCLK_CONTROL) & 2)
				break;
470 471 472 473 474 475 476 477 478 479 480
		pm2_WR(par, PM2VR_RD_INDEX_HIGH, 0);
		break;
	case PM2_TYPE_PERMEDIA2:
		pm2_mnp(clk, &m, &n, &p);
		WAIT_FIFO(par, 10);
		pm2_RDAC_WR(par, PM2I_RD_MEMORY_CLOCK_3, 6);
		pm2_RDAC_WR(par, PM2I_RD_MEMORY_CLOCK_1, m);
		pm2_RDAC_WR(par, PM2I_RD_MEMORY_CLOCK_2, n);
		pm2_RDAC_WR(par, PM2I_RD_MEMORY_CLOCK_3, 8|p);
		pm2_RDAC_RD(par, PM2I_RD_MEMORY_CLOCK_STATUS);
		rmb();
K
Krzysztof Helt 已提交
481 482 483
		for (i = 256; i; i--)
			if (pm2_RD(par, PM2R_RD_INDEXED_DATA) & PM2F_PLL_LOCKED)
				break;
484 485
		break;
	}
L
Linus Torvalds 已提交
486 487
}

K
Krzysztof Helt 已提交
488
static void set_pixclock(struct pm2fb_par *par, u32 clk)
L
Linus Torvalds 已提交
489 490 491 492 493 494 495
{
	int i;
	unsigned char m, n, p;

	switch (par->type) {
	case PM2_TYPE_PERMEDIA2:
		pm2_mnp(clk, &m, &n, &p);
K
Krzysztof Helt 已提交
496
		WAIT_FIFO(par, 10);
L
Linus Torvalds 已提交
497 498 499 500 501 502
		pm2_RDAC_WR(par, PM2I_RD_PIXEL_CLOCK_A3, 0);
		pm2_RDAC_WR(par, PM2I_RD_PIXEL_CLOCK_A1, m);
		pm2_RDAC_WR(par, PM2I_RD_PIXEL_CLOCK_A2, n);
		pm2_RDAC_WR(par, PM2I_RD_PIXEL_CLOCK_A3, 8|p);
		pm2_RDAC_RD(par, PM2I_RD_PIXEL_CLOCK_STATUS);
		rmb();
K
Krzysztof Helt 已提交
503 504 505
		for (i = 256; i; i--)
			if (pm2_RD(par, PM2R_RD_INDEXED_DATA) & PM2F_PLL_LOCKED)
				break;
L
Linus Torvalds 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518
		break;
	case PM2_TYPE_PERMEDIA2V:
		pm2v_mnp(clk/2, &m, &n, &p);
		WAIT_FIFO(par, 8);
		pm2_WR(par, PM2VR_RD_INDEX_HIGH, PM2VI_RD_CLK0_PRESCALE >> 8);
		pm2v_RDAC_WR(par, PM2VI_RD_CLK0_PRESCALE, m);
		pm2v_RDAC_WR(par, PM2VI_RD_CLK0_FEEDBACK, n);
		pm2v_RDAC_WR(par, PM2VI_RD_CLK0_POSTSCALE, p);
		pm2_WR(par, PM2VR_RD_INDEX_HIGH, 0);
		break;
	}
}

K
Krzysztof Helt 已提交
519 520
static void set_video(struct pm2fb_par *p, u32 video)
{
L
Linus Torvalds 已提交
521
	u32 tmp;
K
Krzysztof Helt 已提交
522
	u32 vsync = video;
L
Linus Torvalds 已提交
523 524

	DPRINTK("video = 0x%x\n", video);
K
Krzysztof Helt 已提交
525

L
Linus Torvalds 已提交
526 527 528 529 530 531
	/*
	 * The hardware cursor needs +vsync to recognise vert retrace.
	 * We may not be using the hardware cursor, but the X Glint
	 * driver may well. So always set +hsync/+vsync and then set
	 * the RAMDAC to invert the sync if necessary.
	 */
K
Krzysztof Helt 已提交
532 533
	vsync &= ~(PM2F_HSYNC_MASK | PM2F_VSYNC_MASK);
	vsync |= PM2F_HSYNC_ACT_HIGH | PM2F_VSYNC_ACT_HIGH;
L
Linus Torvalds 已提交
534

535
	WAIT_FIFO(p, 3);
L
Linus Torvalds 已提交
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
	pm2_WR(p, PM2R_VIDEO_CONTROL, vsync);

	switch (p->type) {
	case PM2_TYPE_PERMEDIA2:
		tmp = PM2F_RD_PALETTE_WIDTH_8;
		if ((video & PM2F_HSYNC_MASK) == PM2F_HSYNC_ACT_LOW)
			tmp |= 4; /* invert hsync */
		if ((video & PM2F_VSYNC_MASK) == PM2F_VSYNC_ACT_LOW)
			tmp |= 8; /* invert vsync */
		pm2_RDAC_WR(p, PM2I_RD_MISC_CONTROL, tmp);
		break;
	case PM2_TYPE_PERMEDIA2V:
		tmp = 0;
		if ((video & PM2F_HSYNC_MASK) == PM2F_HSYNC_ACT_LOW)
			tmp |= 1; /* invert hsync */
		if ((video & PM2F_VSYNC_MASK) == PM2F_VSYNC_ACT_LOW)
			tmp |= 4; /* invert vsync */
		pm2v_RDAC_WR(p, PM2VI_RD_SYNC_CONTROL, tmp);
		break;
	}
}

/*
K
Krzysztof Helt 已提交
559 560 561
 *	pm2fb_check_var - Optional function. Validates a var passed in.
 *	@var: frame buffer variable screen structure
 *	@info: frame buffer structure that represents a single frame buffer
L
Linus Torvalds 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
 *
 *	Checks to see if the hardware supports the state requested by
 *	var passed in.
 *
 *	Returns negative errno on error, or zero on success.
 */
static int pm2fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
{
	u32 lpitch;

	if (var->bits_per_pixel != 8  && var->bits_per_pixel != 16 &&
	    var->bits_per_pixel != 24 && var->bits_per_pixel != 32) {
		DPRINTK("depth not supported: %u\n", var->bits_per_pixel);
		return -EINVAL;
	}

	if (var->xres != var->xres_virtual) {
K
Krzysztof Helt 已提交
579 580
		DPRINTK("virtual x resolution != "
			"physical x resolution not supported\n");
L
Linus Torvalds 已提交
581 582 583 584
		return -EINVAL;
	}

	if (var->yres > var->yres_virtual) {
K
Krzysztof Helt 已提交
585 586
		DPRINTK("virtual y resolution < "
			"physical y resolution not possible\n");
L
Linus Torvalds 已提交
587 588 589
		return -EINVAL;
	}

590 591 592 593 594
	/* permedia cannot blit over 2048 */
	if (var->yres_virtual > 2047) {
		var->yres_virtual = 2047;
	}

L
Linus Torvalds 已提交
595 596 597 598 599 600 601 602 603 604 605
	if (var->xoffset) {
		DPRINTK("xoffset not supported\n");
		return -EINVAL;
	}

	if ((var->vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) {
		DPRINTK("interlace not supported\n");
		return -EINVAL;
	}

	var->xres = (var->xres + 15) & ~15; /* could sometimes be 8 */
K
Krzysztof Helt 已提交
606
	lpitch = var->xres * ((var->bits_per_pixel + 7) >> 3);
K
Krzysztof Helt 已提交
607

L
Linus Torvalds 已提交
608 609 610 611
	if (var->xres < 320 || var->xres > 1600) {
		DPRINTK("width not supported: %u\n", var->xres);
		return -EINVAL;
	}
K
Krzysztof Helt 已提交
612

L
Linus Torvalds 已提交
613 614 615 616
	if (var->yres < 200 || var->yres > 1200) {
		DPRINTK("height not supported: %u\n", var->yres);
		return -EINVAL;
	}
K
Krzysztof Helt 已提交
617

L
Linus Torvalds 已提交
618 619 620 621 622
	if (lpitch * var->yres_virtual > info->fix.smem_len) {
		DPRINTK("no memory for screen (%ux%ux%u)\n",
			var->xres, var->yres_virtual, var->bits_per_pixel);
		return -EINVAL;
	}
K
Krzysztof Helt 已提交
623

L
Linus Torvalds 已提交
624
	if (PICOS2KHZ(var->pixclock) > PM2_MAX_PIXCLOCK) {
K
Krzysztof Helt 已提交
625 626
		DPRINTK("pixclock too high (%ldKHz)\n",
			PICOS2KHZ(var->pixclock));
L
Linus Torvalds 已提交
627 628 629
		return -EINVAL;
	}

630 631
	var->transp.offset = 0;
	var->transp.length = 0;
K
Krzysztof Helt 已提交
632
	switch (var->bits_per_pixel) {
L
Linus Torvalds 已提交
633
	case 8:
K
Krzysztof Helt 已提交
634 635 636
		var->red.length = 8;
		var->green.length = 8;
		var->blue.length = 8;
L
Linus Torvalds 已提交
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
		break;
	case 16:
		var->red.offset   = 11;
		var->red.length   = 5;
		var->green.offset = 5;
		var->green.length = 6;
		var->blue.offset  = 0;
		var->blue.length  = 5;
		break;
	case 32:
		var->transp.offset = 24;
		var->transp.length = 8;
		var->red.offset	  = 16;
		var->green.offset = 8;
		var->blue.offset  = 0;
K
Krzysztof Helt 已提交
652 653 654
		var->red.length = 8;
		var->green.length = 8;
		var->blue.length = 8;
L
Linus Torvalds 已提交
655 656 657 658 659 660 661 662 663 664
		break;
	case 24:
#ifdef __BIG_ENDIAN
		var->red.offset   = 0;
		var->blue.offset  = 16;
#else
		var->red.offset   = 16;
		var->blue.offset  = 0;
#endif
		var->green.offset = 8;
K
Krzysztof Helt 已提交
665 666 667
		var->red.length = 8;
		var->green.length = 8;
		var->blue.length = 8;
L
Linus Torvalds 已提交
668 669
		break;
	}
K
Krzysztof Helt 已提交
670 671
	var->height = -1;
	var->width = -1;
K
Krzysztof Helt 已提交
672

L
Linus Torvalds 已提交
673
	var->accel_flags = 0;	/* Can't mmap if this is on */
K
Krzysztof Helt 已提交
674

L
Linus Torvalds 已提交
675 676 677 678 679 680
	DPRINTK("Checking graphics mode at %dx%d depth %d\n",
		var->xres, var->yres, var->bits_per_pixel);
	return 0;
}

/**
K
Krzysztof Helt 已提交
681 682
 *	pm2fb_set_par - Alters the hardware state.
 *	@info: frame buffer structure that represents a single frame buffer
L
Linus Torvalds 已提交
683 684 685 686 687 688
 *
 *	Using the fb_var_screeninfo in fb_info we set the resolution of the
 *	this particular framebuffer.
 */
static int pm2fb_set_par(struct fb_info *info)
{
689
	struct pm2fb_par *par = info->par;
L
Linus Torvalds 已提交
690
	u32 pixclock;
K
Krzysztof Helt 已提交
691 692 693
	u32 width = (info->var.xres_virtual + 7) & ~7;
	u32 height = info->var.yres_virtual;
	u32 depth = (info->var.bits_per_pixel + 7) & ~7;
L
Linus Torvalds 已提交
694 695 696 697 698 699 700 701 702
	u32 hsstart, hsend, hbend, htotal;
	u32 vsstart, vsend, vbend, vtotal;
	u32 stride;
	u32 base;
	u32 video = 0;
	u32 clrmode = PM2F_RD_COLOR_MODE_RGB | PM2F_RD_GUI_ACTIVE;
	u32 txtmap = 0;
	u32 pixsize = 0;
	u32 clrformat = 0;
703
	u32 misc = 1; /* 8-bit DAC */
K
Krzysztof Helt 已提交
704
	u32 xres = (info->var.xres + 31) & ~31;
L
Linus Torvalds 已提交
705 706 707 708 709
	int data64;

	reset_card(par);
	reset_config(par);
	clear_palette(par);
K
Krzysztof Helt 已提交
710
	if (par->memclock)
L
Linus Torvalds 已提交
711
		set_memclock(par, par->memclock);
K
Krzysztof Helt 已提交
712

L
Linus Torvalds 已提交
713 714 715 716 717 718 719 720
	depth = (depth > 32) ? 32 : depth;
	data64 = depth > 8 || par->type == PM2_TYPE_PERMEDIA2V;

	pixclock = PICOS2KHZ(info->var.pixclock);
	if (pixclock > PM2_MAX_PIXCLOCK) {
		DPRINTK("pixclock too high (%uKHz)\n", pixclock);
		return -EINVAL;
	}
K
Krzysztof Helt 已提交
721

L
Linus Torvalds 已提交
722 723 724 725 726 727 728 729
	hsstart = to3264(info->var.right_margin, depth, data64);
	hsend = hsstart + to3264(info->var.hsync_len, depth, data64);
	hbend = hsend + to3264(info->var.left_margin, depth, data64);
	htotal = to3264(xres, depth, data64) + hbend - 1;
	vsstart = (info->var.lower_margin)
		? info->var.lower_margin - 1
		: 0;	/* FIXME! */
	vsend = info->var.lower_margin + info->var.vsync_len - 1;
K
Krzysztof Helt 已提交
730 731
	vbend = info->var.lower_margin + info->var.vsync_len +
		info->var.upper_margin;
L
Linus Torvalds 已提交
732 733 734 735 736
	vtotal = info->var.yres + vbend - 1;
	stride = to3264(width, depth, 1);
	base = to3264(info->var.yoffset * xres + info->var.xoffset, depth, 1);
	if (data64)
		video |= PM2F_DATA_64_ENABLE;
K
Krzysztof Helt 已提交
737

L
Linus Torvalds 已提交
738 739 740 741 742 743
	if (info->var.sync & FB_SYNC_HOR_HIGH_ACT) {
		if (lowhsync) {
			DPRINTK("ignoring +hsync, using -hsync.\n");
			video |= PM2F_HSYNC_ACT_LOW;
		} else
			video |= PM2F_HSYNC_ACT_HIGH;
K
Krzysztof Helt 已提交
744
	} else
L
Linus Torvalds 已提交
745
		video |= PM2F_HSYNC_ACT_LOW;
K
Krzysztof Helt 已提交
746

L
Linus Torvalds 已提交
747 748 749 750 751 752
	if (info->var.sync & FB_SYNC_VERT_HIGH_ACT) {
		if (lowvsync) {
			DPRINTK("ignoring +vsync, using -vsync.\n");
			video |= PM2F_VSYNC_ACT_LOW;
		} else
			video |= PM2F_VSYNC_ACT_HIGH;
K
Krzysztof Helt 已提交
753
	} else
L
Linus Torvalds 已提交
754
		video |= PM2F_VSYNC_ACT_LOW;
K
Krzysztof Helt 已提交
755

K
Krzysztof Helt 已提交
756
	if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) {
L
Linus Torvalds 已提交
757 758 759
		DPRINTK("interlaced not supported\n");
		return -EINVAL;
	}
K
Krzysztof Helt 已提交
760
	if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE)
L
Linus Torvalds 已提交
761
		video |= PM2F_LINE_DOUBLE;
K
Krzysztof Helt 已提交
762
	if ((info->var.activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW)
L
Linus Torvalds 已提交
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
		video |= PM2F_VIDEO_ENABLE;
	par->video = video;

	info->fix.visual =
		(depth == 8) ? FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
	info->fix.line_length = info->var.xres * depth / 8;
	info->cmap.len = 256;

	/*
	 * Settings calculated. Now write them out.
	 */
	if (par->type == PM2_TYPE_PERMEDIA2V) {
		WAIT_FIFO(par, 1);
		pm2_WR(par, PM2VR_RD_INDEX_HIGH, 0);
	}
K
Krzysztof Helt 已提交
778

L
Linus Torvalds 已提交
779
	set_aperture(par, depth);
K
Krzysztof Helt 已提交
780

L
Linus Torvalds 已提交
781 782 783 784 785
	mb();
	WAIT_FIFO(par, 19);
	switch (depth) {
	case 8:
		pm2_WR(par, PM2R_FB_READ_PIXEL, 0);
786
		clrformat = 0x2e;
L
Linus Torvalds 已提交
787 788 789 790 791 792 793
		break;
	case 16:
		pm2_WR(par, PM2R_FB_READ_PIXEL, 1);
		clrmode |= PM2F_RD_TRUECOLOR | PM2F_RD_PIXELFORMAT_RGB565;
		txtmap = PM2F_TEXTEL_SIZE_16;
		pixsize = 1;
		clrformat = 0x70;
794
		misc |= 8;
L
Linus Torvalds 已提交
795 796 797 798 799 800 801
		break;
	case 32:
		pm2_WR(par, PM2R_FB_READ_PIXEL, 2);
		clrmode |= PM2F_RD_TRUECOLOR | PM2F_RD_PIXELFORMAT_RGBA8888;
		txtmap = PM2F_TEXTEL_SIZE_32;
		pixsize = 2;
		clrformat = 0x20;
802
		misc |= 8;
L
Linus Torvalds 已提交
803 804 805 806 807 808 809
		break;
	case 24:
		pm2_WR(par, PM2R_FB_READ_PIXEL, 4);
		clrmode |= PM2F_RD_TRUECOLOR | PM2F_RD_PIXELFORMAT_RGB888;
		txtmap = PM2F_TEXTEL_SIZE_24;
		pixsize = 4;
		clrformat = 0x20;
810
		misc |= 8;
L
Linus Torvalds 已提交
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
		break;
	}
	pm2_WR(par, PM2R_FB_WRITE_MODE, PM2F_FB_WRITE_ENABLE);
	pm2_WR(par, PM2R_FB_READ_MODE, partprod(xres));
	pm2_WR(par, PM2R_LB_READ_MODE, partprod(xres));
	pm2_WR(par, PM2R_TEXTURE_MAP_FORMAT, txtmap | partprod(xres));
	pm2_WR(par, PM2R_H_TOTAL, htotal);
	pm2_WR(par, PM2R_HS_START, hsstart);
	pm2_WR(par, PM2R_HS_END, hsend);
	pm2_WR(par, PM2R_HG_END, hbend);
	pm2_WR(par, PM2R_HB_END, hbend);
	pm2_WR(par, PM2R_V_TOTAL, vtotal);
	pm2_WR(par, PM2R_VS_START, vsstart);
	pm2_WR(par, PM2R_VS_END, vsend);
	pm2_WR(par, PM2R_VB_END, vbend);
	pm2_WR(par, PM2R_SCREEN_STRIDE, stride);
	wmb();
	pm2_WR(par, PM2R_WINDOW_ORIGIN, 0);
	pm2_WR(par, PM2R_SCREEN_SIZE, (height << 16) | width);
	pm2_WR(par, PM2R_SCISSOR_MODE, PM2F_SCREEN_SCISSOR_ENABLE);
	wmb();
	pm2_WR(par, PM2R_SCREEN_BASE, base);
	wmb();
	set_video(par, video);
835
	WAIT_FIFO(par, 10);
L
Linus Torvalds 已提交
836 837 838
	switch (par->type) {
	case PM2_TYPE_PERMEDIA2:
		pm2_RDAC_WR(par, PM2I_RD_COLOR_MODE, clrmode);
K
Krzysztof Helt 已提交
839
		pm2_RDAC_WR(par, PM2I_RD_COLOR_KEY_CONTROL,
K
Krzysztof Helt 已提交
840
				(depth == 8) ? 0 : PM2F_COLOR_KEY_TEST_OFF);
L
Linus Torvalds 已提交
841 842
		break;
	case PM2_TYPE_PERMEDIA2V:
843
		pm2v_RDAC_WR(par, PM2VI_RD_DAC_CONTROL, 0);
L
Linus Torvalds 已提交
844 845
		pm2v_RDAC_WR(par, PM2VI_RD_PIXEL_SIZE, pixsize);
		pm2v_RDAC_WR(par, PM2VI_RD_COLOR_FORMAT, clrformat);
846 847
		pm2v_RDAC_WR(par, PM2VI_RD_MISC_CONTROL, misc);
		pm2v_RDAC_WR(par, PM2VI_RD_OVERLAY_KEY, 0);
L
Linus Torvalds 已提交
848 849 850 851 852
		break;
	}
	set_pixclock(par, pixclock);
	DPRINTK("Setting graphics mode at %dx%d depth %d\n",
		info->var.xres, info->var.yres, info->var.bits_per_pixel);
K
Krzysztof Helt 已提交
853
	return 0;
L
Linus Torvalds 已提交
854 855 856
}

/**
K
Krzysztof Helt 已提交
857 858 859 860
 *	pm2fb_setcolreg - Sets a color register.
 *	@regno: boolean, 0 copy local, 1 get_user() function
 *	@red: frame buffer colormap structure
 *	@green: The green value which can be up to 16 bits wide
L
Linus Torvalds 已提交
861
 *	@blue:  The blue value which can be up to 16 bits wide.
K
Krzysztof Helt 已提交
862 863 864 865 866
 *	@transp: If supported the alpha value which can be up to 16 bits wide.
 *	@info: frame buffer info structure
 *
 *	Set a single color register. The values supplied have a 16 bit
 *	magnitude which needs to be scaled in this function for the hardware.
L
Linus Torvalds 已提交
867
 *	Pretty much a direct lift from tdfxfb.c.
K
Krzysztof Helt 已提交
868
 *
L
Linus Torvalds 已提交
869 870 871 872 873 874
 *	Returns negative errno on error, or zero on success.
 */
static int pm2fb_setcolreg(unsigned regno, unsigned red, unsigned green,
			   unsigned blue, unsigned transp,
			   struct fb_info *info)
{
875
	struct pm2fb_par *par = info->par;
L
Linus Torvalds 已提交
876 877

	if (regno >= info->cmap.len)  /* no. of hw registers */
878
		return -EINVAL;
L
Linus Torvalds 已提交
879 880 881 882 883
	/*
	 * Program hardware... do anything you want with transp
	 */

	/* grayscale works only partially under directcolor */
K
Krzysztof Helt 已提交
884 885
	/* grayscale = 0.30*R + 0.59*G + 0.11*B */
	if (info->var.grayscale)
L
Linus Torvalds 已提交
886 887 888 889 890 891 892 893 894 895 896 897 898
		red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;

	/* Directcolor:
	 *   var->{color}.offset contains start of bitfield
	 *   var->{color}.length contains length of bitfield
	 *   {hardwarespecific} contains width of DAC
	 *   cmap[X] is programmed to
	 *   (X << red.offset) | (X << green.offset) | (X << blue.offset)
	 *   RAMDAC[X] is programmed to (red, green, blue)
	 *
	 * Pseudocolor:
	 *    uses offset = 0 && length = DAC register width.
	 *    var->{color}.offset is 0
899
	 *    var->{color}.length contains width of DAC
L
Linus Torvalds 已提交
900 901 902 903 904 905 906 907 908 909 910
	 *    cmap is not used
	 *    DAC[X] is programmed to (red, green, blue)
	 * Truecolor:
	 *    does not use RAMDAC (usually has 3 of them).
	 *    var->{color}.offset contains start of bitfield
	 *    var->{color}.length contains length of bitfield
	 *    cmap is programmed to
	 *    (red << red.offset) | (green << green.offset) |
	 *    (blue << blue.offset) | (transp << transp.offset)
	 *    RAMDAC does not exist
	 */
K
Krzysztof Helt 已提交
911
#define CNVT_TOHW(val, width) ((((val) << (width)) + 0x7FFF -(val)) >> 16)
L
Linus Torvalds 已提交
912 913 914 915 916 917 918 919 920
	switch (info->fix.visual) {
	case FB_VISUAL_TRUECOLOR:
	case FB_VISUAL_PSEUDOCOLOR:
		red = CNVT_TOHW(red, info->var.red.length);
		green = CNVT_TOHW(green, info->var.green.length);
		blue = CNVT_TOHW(blue, info->var.blue.length);
		transp = CNVT_TOHW(transp, info->var.transp.length);
		break;
	case FB_VISUAL_DIRECTCOLOR:
K
Krzysztof Helt 已提交
921 922 923
		/* example here assumes 8 bit DAC. Might be different
		 * for your hardware */
		red = CNVT_TOHW(red, 8);
L
Linus Torvalds 已提交
924 925 926 927 928 929 930 931 932 933 934 935
		green = CNVT_TOHW(green, 8);
		blue = CNVT_TOHW(blue, 8);
		/* hey, there is bug in transp handling... */
		transp = CNVT_TOHW(transp, 8);
		break;
	}
#undef CNVT_TOHW
	/* Truecolor has hardware independent palette */
	if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
		u32 v;

		if (regno >= 16)
936
			return -EINVAL;
L
Linus Torvalds 已提交
937 938 939 940 941 942 943 944

		v = (red << info->var.red.offset) |
			(green << info->var.green.offset) |
			(blue << info->var.blue.offset) |
			(transp << info->var.transp.offset);

		switch (info->var.bits_per_pixel) {
		case 8:
K
Krzysztof Helt 已提交
945 946
			break;
		case 16:
L
Linus Torvalds 已提交
947
		case 24:
K
Krzysztof Helt 已提交
948 949
		case 32:
			par->palette[regno] = v;
L
Linus Torvalds 已提交
950 951 952
			break;
		}
		return 0;
K
Krzysztof Helt 已提交
953
	} else if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR)
L
Linus Torvalds 已提交
954 955 956 957 958 959
		set_color(par, regno, red, green, blue);

	return 0;
}

/**
K
Krzysztof Helt 已提交
960 961 962
 *	pm2fb_pan_display - Pans the display.
 *	@var: frame buffer variable screen structure
 *	@info: frame buffer structure that represents a single frame buffer
L
Linus Torvalds 已提交
963 964
 *
 *	Pan (or wrap, depending on the `vmode' field) the display using the
K
Krzysztof Helt 已提交
965 966
 *	`xoffset' and `yoffset' fields of the `var' structure.
 *	If the values don't fit, return -EINVAL.
L
Linus Torvalds 已提交
967
 *
K
Krzysztof Helt 已提交
968
 *	Returns negative errno on error, or zero on success.
L
Linus Torvalds 已提交
969 970 971 972 973
 *
 */
static int pm2fb_pan_display(struct fb_var_screeninfo *var,
			     struct fb_info *info)
{
974
	struct pm2fb_par *p = info->par;
L
Linus Torvalds 已提交
975
	u32 base;
K
Krzysztof Helt 已提交
976 977
	u32 depth = (var->bits_per_pixel + 7) & ~7;
	u32 xres = (var->xres + 31) & ~31;
L
Linus Torvalds 已提交
978 979 980 981

	depth = (depth > 32) ? 32 : depth;
	base = to3264(var->yoffset * xres + var->xoffset, depth, 1);
	WAIT_FIFO(p, 1);
K
Krzysztof Helt 已提交
982
	pm2_WR(p, PM2R_SCREEN_BASE, base);
L
Linus Torvalds 已提交
983 984 985 986
	return 0;
}

/**
K
Krzysztof Helt 已提交
987 988 989
 *	pm2fb_blank - Blanks the display.
 *	@blank_mode: the blank mode we want.
 *	@info: frame buffer structure that represents a single frame buffer
L
Linus Torvalds 已提交
990
 *
K
Krzysztof Helt 已提交
991 992 993 994 995 996 997
 *	Blank the screen if blank_mode != 0, else unblank. Return 0 if
 *	blanking succeeded, != 0 if un-/blanking failed due to e.g. a
 *	video mode which doesn't support it. Implements VESA suspend
 *	and powerdown modes on hardware that supports disabling hsync/vsync:
 *	blank_mode == 2: suspend vsync
 *	blank_mode == 3: suspend hsync
 *	blank_mode == 4: powerdown
L
Linus Torvalds 已提交
998
 *
K
Krzysztof Helt 已提交
999
 *	Returns negative errno on error, or zero on success.
L
Linus Torvalds 已提交
1000 1001 1002 1003
 *
 */
static int pm2fb_blank(int blank_mode, struct fb_info *info)
{
1004
	struct pm2fb_par *par = info->par;
L
Linus Torvalds 已提交
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
	u32 video = par->video;

	DPRINTK("blank_mode %d\n", blank_mode);

	switch (blank_mode) {
	case FB_BLANK_UNBLANK:
		/* Screen: On */
		video |= PM2F_VIDEO_ENABLE;
		break;
	case FB_BLANK_NORMAL:
		/* Screen: Off */
		video &= ~PM2F_VIDEO_ENABLE;
		break;
	case FB_BLANK_VSYNC_SUSPEND:
		/* VSync: Off */
K
Krzysztof Helt 已提交
1020
		video &= ~(PM2F_VSYNC_MASK | PM2F_BLANK_LOW);
L
Linus Torvalds 已提交
1021 1022 1023
		break;
	case FB_BLANK_HSYNC_SUSPEND:
		/* HSync: Off */
K
Krzysztof Helt 已提交
1024
		video &= ~(PM2F_HSYNC_MASK | PM2F_BLANK_LOW);
L
Linus Torvalds 已提交
1025 1026 1027
		break;
	case FB_BLANK_POWERDOWN:
		/* HSync: Off, VSync: Off */
K
Krzysztof Helt 已提交
1028
		video &= ~(PM2F_VSYNC_MASK | PM2F_HSYNC_MASK | PM2F_BLANK_LOW);
L
Linus Torvalds 已提交
1029 1030 1031 1032 1033 1034
		break;
	}
	set_video(par, video);
	return 0;
}

A
Antonino A. Daplas 已提交
1035 1036 1037 1038 1039 1040 1041 1042 1043
static int pm2fb_sync(struct fb_info *info)
{
	struct pm2fb_par *par = info->par;

	WAIT_FIFO(par, 1);
	pm2_WR(par, PM2R_SYNC, 0);
	mb();
	do {
		while (pm2_RD(par, PM2R_OUT_FIFO_WORDS) == 0)
1044
			cpu_relax();
A
Antonino A. Daplas 已提交
1045 1046 1047 1048 1049
	} while (pm2_RD(par, PM2R_OUT_FIFO) != PM2TAG(PM2R_SYNC));

	return 0;
}

K
Krzysztof Helt 已提交
1050
static void pm2fb_fillrect(struct fb_info *info,
1051 1052
				const struct fb_fillrect *region)
{
K
Krzysztof Helt 已提交
1053
	struct pm2fb_par *par = info->par;
1054 1055 1056
	struct fb_fillrect modded;
	int vxres, vyres;
	u32 color = (info->fix.visual == FB_VISUAL_TRUECOLOR) ?
K
Krzysztof Helt 已提交
1057
		((u32 *)info->pseudo_palette)[region->color] : region->color;
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071

	if (info->state != FBINFO_STATE_RUNNING)
		return;
	if ((info->flags & FBINFO_HWACCEL_DISABLED) ||
		region->rop != ROP_COPY ) {
		cfb_fillrect(info, region);
		return;
	}

	vxres = info->var.xres_virtual;
	vyres = info->var.yres_virtual;

	memcpy(&modded, region, sizeof(struct fb_fillrect));

K
Krzysztof Helt 已提交
1072 1073
	if (!modded.width || !modded.height ||
	    modded.dx >= vxres || modded.dy >= vyres)
1074 1075
		return;

K
Krzysztof Helt 已提交
1076
	if (modded.dx + modded.width  > vxres)
1077
		modded.width  = vxres - modded.dx;
K
Krzysztof Helt 已提交
1078
	if (modded.dy + modded.height > vyres)
1079 1080
		modded.height = vyres - modded.dy;

K
Krzysztof Helt 已提交
1081
	if (info->var.bits_per_pixel == 8)
1082
		color |= color << 8;
K
Krzysztof Helt 已提交
1083
	if (info->var.bits_per_pixel <= 16)
1084 1085
		color |= color << 16;

K
Krzysztof Helt 已提交
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
	WAIT_FIFO(par, 3);
	pm2_WR(par, PM2R_CONFIG, PM2F_CONFIG_FB_WRITE_ENABLE);
	pm2_WR(par, PM2R_RECTANGLE_ORIGIN, (modded.dy << 16) | modded.dx);
	pm2_WR(par, PM2R_RECTANGLE_SIZE, (modded.height << 16) | modded.width);
	if (info->var.bits_per_pixel != 24) {
		WAIT_FIFO(par, 2);
		pm2_WR(par, PM2R_FB_BLOCK_COLOR, color);
		wmb();
		pm2_WR(par, PM2R_RENDER,
				PM2F_RENDER_RECTANGLE | PM2F_RENDER_FASTFILL);
	} else {
1097 1098 1099 1100 1101
		WAIT_FIFO(par, 4);
		pm2_WR(par, PM2R_COLOR_DDA_MODE, 1);
		pm2_WR(par, PM2R_CONSTANT_COLOR, color);
		wmb();
		pm2_WR(par, PM2R_RENDER,
K
Krzysztof Helt 已提交
1102 1103
				PM2F_RENDER_RECTANGLE |
				PM2F_INCREASE_X | PM2F_INCREASE_Y );
1104
		pm2_WR(par, PM2R_COLOR_DDA_MODE, 0);
K
Krzysztof Helt 已提交
1105
	}
1106 1107 1108 1109 1110
}

static void pm2fb_copyarea(struct fb_info *info,
				const struct fb_copyarea *area)
{
K
Krzysztof Helt 已提交
1111
	struct pm2fb_par *par = info->par;
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
	struct fb_copyarea modded;
	u32 vxres, vyres;

	if (info->state != FBINFO_STATE_RUNNING)
		return;
	if (info->flags & FBINFO_HWACCEL_DISABLED) {
		cfb_copyarea(info, area);
		return;
	}

	memcpy(&modded, area, sizeof(struct fb_copyarea));

	vxres = info->var.xres_virtual;
	vyres = info->var.yres_virtual;

K
Krzysztof Helt 已提交
1127 1128 1129
	if (!modded.width || !modded.height ||
	    modded.sx >= vxres || modded.sy >= vyres ||
	    modded.dx >= vxres || modded.dy >= vyres)
1130 1131
		return;

K
Krzysztof Helt 已提交
1132
	if (modded.sx + modded.width > vxres)
1133
		modded.width = vxres - modded.sx;
K
Krzysztof Helt 已提交
1134
	if (modded.dx + modded.width > vxres)
1135
		modded.width = vxres - modded.dx;
K
Krzysztof Helt 已提交
1136
	if (modded.sy + modded.height > vyres)
1137
		modded.height = vyres - modded.sy;
K
Krzysztof Helt 已提交
1138
	if (modded.dy + modded.height > vyres)
1139 1140
		modded.height = vyres - modded.dy;

K
Krzysztof Helt 已提交
1141 1142 1143 1144
	WAIT_FIFO(par, 5);
	pm2_WR(par, PM2R_CONFIG, PM2F_CONFIG_FB_WRITE_ENABLE |
		PM2F_CONFIG_FB_READ_SOURCE_ENABLE);
	pm2_WR(par, PM2R_FB_SOURCE_DELTA,
K
Krzysztof Helt 已提交
1145 1146
			((modded.sy - modded.dy) & 0xfff) << 16 |
			((modded.sx - modded.dx) & 0xfff));
K
Krzysztof Helt 已提交
1147 1148 1149 1150
	pm2_WR(par, PM2R_RECTANGLE_ORIGIN, (modded.dy << 16) | modded.dx);
	pm2_WR(par, PM2R_RECTANGLE_SIZE, (modded.height << 16) | modded.width);
	wmb();
	pm2_WR(par, PM2R_RENDER, PM2F_RENDER_RECTANGLE |
K
Krzysztof Helt 已提交
1151 1152
				(modded.dx < modded.sx ? PM2F_INCREASE_X : 0) |
				(modded.dy < modded.sy ? PM2F_INCREASE_Y : 0));
1153 1154
}

K
Krzysztof Helt 已提交
1155 1156 1157 1158 1159
static void pm2fb_imageblit(struct fb_info *info, const struct fb_image *image)
{
	struct pm2fb_par *par = info->par;
	u32 height = image->height;
	u32 fgx, bgx;
K
Krzysztof Helt 已提交
1160
	const u32 *src = (const u32 *)image->data;
K
Krzysztof Helt 已提交
1161
	u32 xres = (info->var.xres + 31) & ~31;
K
Krzysztof Helt 已提交
1162 1163 1164 1165 1166
	int raster_mode = 1; /* invert bits */

#ifdef __LITTLE_ENDIAN
	raster_mode |= 3 << 7; /* reverse byte order */
#endif
K
Krzysztof Helt 已提交
1167 1168 1169 1170 1171 1172 1173 1174

	if (info->state != FBINFO_STATE_RUNNING)
		return;
	if (info->flags & FBINFO_HWACCEL_DISABLED || image->depth != 1) {
		cfb_imageblit(info, image);
		return;
	}
	switch (info->fix.visual) {
K
Krzysztof Helt 已提交
1175 1176 1177 1178 1179 1180 1181 1182 1183
	case FB_VISUAL_PSEUDOCOLOR:
		fgx = image->fg_color;
		bgx = image->bg_color;
		break;
	case FB_VISUAL_TRUECOLOR:
	default:
		fgx = par->palette[image->fg_color];
		bgx = par->palette[image->bg_color];
		break;
K
Krzysztof Helt 已提交
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
	}
	if (info->var.bits_per_pixel == 8) {
		fgx |= fgx << 8;
		bgx |= bgx << 8;
	}
	if (info->var.bits_per_pixel <= 16) {
		fgx |= fgx << 16;
		bgx |= bgx << 16;
	}

	WAIT_FIFO(par, 13);
	pm2_WR(par, PM2R_FB_READ_MODE, partprod(xres));
	pm2_WR(par, PM2R_SCISSOR_MIN_XY,
			((image->dy & 0xfff) << 16) | (image->dx & 0x0fff));
	pm2_WR(par, PM2R_SCISSOR_MAX_XY,
			(((image->dy + image->height) & 0x0fff) << 16) |
			((image->dx + image->width) & 0x0fff));
	pm2_WR(par, PM2R_SCISSOR_MODE, 1);
	/* GXcopy & UNIT_ENABLE */
K
Krzysztof Helt 已提交
1203
	pm2_WR(par, PM2R_LOGICAL_OP_MODE, (0x3 << 1) | 1);
K
Krzysztof Helt 已提交
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
	pm2_WR(par, PM2R_RECTANGLE_ORIGIN,
			((image->dy & 0xfff) << 16) | (image->dx & 0x0fff));
	pm2_WR(par, PM2R_RECTANGLE_SIZE,
			((image->height & 0x0fff) << 16) |
			((image->width) & 0x0fff));
	if (info->var.bits_per_pixel == 24) {
		pm2_WR(par, PM2R_COLOR_DDA_MODE, 1);
		/* clear area */
		pm2_WR(par, PM2R_CONSTANT_COLOR, bgx);
		pm2_WR(par, PM2R_RENDER,
			PM2F_RENDER_RECTANGLE |
K
Krzysztof Helt 已提交
1215
			PM2F_INCREASE_X | PM2F_INCREASE_Y);
K
Krzysztof Helt 已提交
1216 1217
		/* BitMapPackEachScanline */
		pm2_WR(par, PM2R_RASTERIZER_MODE, raster_mode | (1 << 9));
K
Krzysztof Helt 已提交
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
		pm2_WR(par, PM2R_CONSTANT_COLOR, fgx);
		pm2_WR(par, PM2R_RENDER,
			PM2F_RENDER_RECTANGLE |
			PM2F_INCREASE_X | PM2F_INCREASE_Y |
			PM2F_RENDER_SYNC_ON_BIT_MASK);
	} else {
		pm2_WR(par, PM2R_COLOR_DDA_MODE, 0);
		/* clear area */
		pm2_WR(par, PM2R_FB_BLOCK_COLOR, bgx);
		pm2_WR(par, PM2R_RENDER,
			PM2F_RENDER_RECTANGLE |
			PM2F_RENDER_FASTFILL |
K
Krzysztof Helt 已提交
1230
			PM2F_INCREASE_X | PM2F_INCREASE_Y);
K
Krzysztof Helt 已提交
1231
		pm2_WR(par, PM2R_RASTERIZER_MODE, raster_mode);
K
Krzysztof Helt 已提交
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
		pm2_WR(par, PM2R_FB_BLOCK_COLOR, fgx);
		pm2_WR(par, PM2R_RENDER,
			PM2F_RENDER_RECTANGLE |
			PM2F_INCREASE_X | PM2F_INCREASE_Y |
			PM2F_RENDER_FASTFILL |
			PM2F_RENDER_SYNC_ON_BIT_MASK);
	}

	while (height--) {
		int width = ((image->width + 7) >> 3)
				+ info->pixmap.scan_align - 1;
		width >>= 2;
		WAIT_FIFO(par, width);
		while (width--) {
			pm2_WR(par, PM2R_BIT_MASK_PATTERN, *src);
			src++;
		}
	}
	WAIT_FIFO(par, 3);
	pm2_WR(par, PM2R_RASTERIZER_MODE, 0);
	pm2_WR(par, PM2R_COLOR_DDA_MODE, 0);
	pm2_WR(par, PM2R_SCISSOR_MODE, 0);
}

1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
/*
 *	Hardware cursor support.
 */
static const u8 cursor_bits_lookup[16] = {
	0x00, 0x40, 0x10, 0x50, 0x04, 0x44, 0x14, 0x54,
	0x01, 0x41, 0x11, 0x51, 0x05, 0x45, 0x15, 0x55
};

static int pm2vfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
{
	struct pm2fb_par *par = info->par;
1267
	u8 mode = PM2F_CURSORMODE_TYPE_X;
1268 1269
	int x = cursor->image.dx - info->var.xoffset;
	int y = cursor->image.dy - info->var.yoffset;
1270 1271 1272 1273 1274 1275

	if (cursor->enable)
		mode |= PM2F_CURSORMODE_CURSOR_ENABLE;

	pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_MODE, mode);

1276 1277 1278 1279 1280 1281 1282
	if (!cursor->enable)
		x = 2047;	/* push it outside display */
	pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_X_LOW, x & 0xff);
	pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_X_HIGH, (x >> 8) & 0xf);
	pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_Y_LOW, y & 0xff);
	pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_Y_HIGH, (y >> 8) & 0xf);

1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
	/*
	 * If the cursor is not be changed this means either we want the
	 * current cursor state (if enable is set) or we want to query what
	 * we can do with the cursor (if enable is not set)
	 */
	if (!cursor->set)
		return 0;

	if (cursor->set & FB_CUR_SETHOT) {
		pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_X_HOT,
			     cursor->hot.x & 0x3f);
		pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_Y_HOT,
			     cursor->hot.y & 0x3f);
	}

	if (cursor->set & FB_CUR_SETCMAP) {
		u32 fg_idx = cursor->image.fg_color;
		u32 bg_idx = cursor->image.bg_color;
		struct fb_cmap cmap = info->cmap;

		/* the X11 driver says one should use these color registers */
		pm2_WR(par, PM2VR_RD_INDEX_HIGH, PM2VI_RD_CURSOR_PALETTE >> 8);
		pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_PALETTE + 0,
			     cmap.red[bg_idx] >> 8 );
		pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_PALETTE + 1,
			     cmap.green[bg_idx] >> 8 );
		pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_PALETTE + 2,
			     cmap.blue[bg_idx] >> 8 );

		pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_PALETTE + 3,
			     cmap.red[fg_idx] >> 8 );
		pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_PALETTE + 4,
			     cmap.green[fg_idx] >> 8 );
		pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_PALETTE + 5,
			     cmap.blue[fg_idx] >> 8 );
		pm2_WR(par, PM2VR_RD_INDEX_HIGH, 0);
	}

	if (cursor->set & (FB_CUR_SETSHAPE | FB_CUR_SETIMAGE)) {
		u8 *bitmap = (u8 *)cursor->image.data;
		u8 *mask = (u8 *)cursor->mask;
		int i;
		int pos = PM2VI_RD_CURSOR_PATTERN;

		for (i = 0; i < cursor->image.height; i++) {
			int j = (cursor->image.width + 7) >> 3;
			int k = 8 - j;

			pm2_WR(par, PM2VR_RD_INDEX_HIGH, pos >> 8);

			for (; j > 0; j--) {
				u8 data = *bitmap ^ *mask;

				if (cursor->rop == ROP_COPY)
					data = *mask & *bitmap;
				/* Upper 4 bits of bitmap data */
				pm2v_RDAC_WR(par, pos++,
					cursor_bits_lookup[data >> 4] |
					(cursor_bits_lookup[*mask >> 4] << 1));
				/* Lower 4 bits of bitmap */
				pm2v_RDAC_WR(par, pos++,
					cursor_bits_lookup[data & 0xf] |
					(cursor_bits_lookup[*mask & 0xf] << 1));
				bitmap++;
				mask++;
			}
			for (; k > 0; k--) {
				pm2v_RDAC_WR(par, pos++, 0);
				pm2v_RDAC_WR(par, pos++, 0);
			}
		}

		while (pos < (1024 + PM2VI_RD_CURSOR_PATTERN)) {
			pm2_WR(par, PM2VR_RD_INDEX_HIGH, pos >> 8);
			pm2v_RDAC_WR(par, pos++, 0);
		}

		pm2_WR(par, PM2VR_RD_INDEX_HIGH, 0);
	}
	return 0;
}

static int pm2fb_cursor(struct fb_info *info, struct fb_cursor *cursor)
{
	struct pm2fb_par *par = info->par;
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
	u8 mode;

	if (!hwcursor)
		return -EINVAL;	/* just to force soft_cursor() call */

	/* Too large of a cursor or wrong bpp :-( */
	if (cursor->image.width > 64 ||
	    cursor->image.height > 64 ||
	    cursor->image.depth > 1)
		return -EINVAL;
1378 1379 1380 1381

	if (par->type == PM2_TYPE_PERMEDIA2V)
		return pm2vfb_cursor(info, cursor);

1382
	mode = 0x40;
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
	if (cursor->enable)
		 mode = 0x43;

	pm2_RDAC_WR(par, PM2I_RD_CURSOR_CONTROL, mode);

	/*
	 * If the cursor is not be changed this means either we want the
	 * current cursor state (if enable is set) or we want to query what
	 * we can do with the cursor (if enable is not set)
	 */
	if (!cursor->set)
		return 0;

	if (cursor->set & FB_CUR_SETPOS) {
1397 1398
		int x = cursor->image.dx - info->var.xoffset + 63;
		int y = cursor->image.dy - info->var.yoffset + 63;
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482

		WAIT_FIFO(par, 4);
		pm2_WR(par, PM2R_RD_CURSOR_X_LSB, x & 0xff);
		pm2_WR(par, PM2R_RD_CURSOR_X_MSB, (x >> 8) & 0x7);
		pm2_WR(par, PM2R_RD_CURSOR_Y_LSB, y & 0xff);
		pm2_WR(par, PM2R_RD_CURSOR_Y_MSB, (y >> 8) & 0x7);
	}

	if (cursor->set & FB_CUR_SETCMAP) {
		u32 fg_idx = cursor->image.fg_color;
		u32 bg_idx = cursor->image.bg_color;

		WAIT_FIFO(par, 7);
		pm2_WR(par, PM2R_RD_CURSOR_COLOR_ADDRESS, 1);
		pm2_WR(par, PM2R_RD_CURSOR_COLOR_DATA,
			info->cmap.red[bg_idx] >> 8);
		pm2_WR(par, PM2R_RD_CURSOR_COLOR_DATA,
			info->cmap.green[bg_idx] >> 8);
		pm2_WR(par, PM2R_RD_CURSOR_COLOR_DATA,
			info->cmap.blue[bg_idx] >> 8);

		pm2_WR(par, PM2R_RD_CURSOR_COLOR_DATA,
			info->cmap.red[fg_idx] >> 8);
		pm2_WR(par, PM2R_RD_CURSOR_COLOR_DATA,
			info->cmap.green[fg_idx] >> 8);
		pm2_WR(par, PM2R_RD_CURSOR_COLOR_DATA,
			info->cmap.blue[fg_idx] >> 8);
	}

	if (cursor->set & (FB_CUR_SETSHAPE | FB_CUR_SETIMAGE)) {
		u8 *bitmap = (u8 *)cursor->image.data;
		u8 *mask = (u8 *)cursor->mask;
		int i;

		WAIT_FIFO(par, 1);
		pm2_WR(par, PM2R_RD_PALETTE_WRITE_ADDRESS, 0);

		for (i = 0; i < cursor->image.height; i++) {
			int j = (cursor->image.width + 7) >> 3;
			int k = 8 - j;

			WAIT_FIFO(par, 8);
			for (; j > 0; j--) {
				u8 data = *bitmap ^ *mask;

				if (cursor->rop == ROP_COPY)
					data = *mask & *bitmap;
				/* bitmap data */
				pm2_WR(par, PM2R_RD_CURSOR_DATA, data);
				bitmap++;
				mask++;
			}
			for (; k > 0; k--)
				pm2_WR(par, PM2R_RD_CURSOR_DATA, 0);
		}
		for (; i < 64; i++) {
			int j = 8;
			WAIT_FIFO(par, 8);
			while (j-- > 0)
				pm2_WR(par, PM2R_RD_CURSOR_DATA, 0);
		}

		mask = (u8 *)cursor->mask;
		for (i = 0; i < cursor->image.height; i++) {
			int j = (cursor->image.width + 7) >> 3;
			int k = 8 - j;

			WAIT_FIFO(par, 8);
			for (; j > 0; j--) {
				/* mask */
				pm2_WR(par, PM2R_RD_CURSOR_DATA, *mask);
				mask++;
			}
			for (; k > 0; k--)
				pm2_WR(par, PM2R_RD_CURSOR_DATA, 0);
		}
		for (; i < 64; i++) {
			int j = 8;
			WAIT_FIFO(par, 8);
			while (j-- > 0)
				pm2_WR(par, PM2R_RD_CURSOR_DATA, 0);
		}
	}
	return 0;
1483 1484
}

L
Linus Torvalds 已提交
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
/* ------------ Hardware Independent Functions ------------ */

/*
 *  Frame buffer operations
 */

static struct fb_ops pm2fb_ops = {
	.owner		= THIS_MODULE,
	.fb_check_var	= pm2fb_check_var,
	.fb_set_par	= pm2fb_set_par,
	.fb_setcolreg	= pm2fb_setcolreg,
	.fb_blank	= pm2fb_blank,
	.fb_pan_display	= pm2fb_pan_display,
1498 1499
	.fb_fillrect	= pm2fb_fillrect,
	.fb_copyarea	= pm2fb_copyarea,
K
Krzysztof Helt 已提交
1500
	.fb_imageblit	= pm2fb_imageblit,
A
Antonino A. Daplas 已提交
1501
	.fb_sync	= pm2fb_sync,
1502
	.fb_cursor	= pm2fb_cursor,
L
Linus Torvalds 已提交
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522
};

/*
 * PCI stuff
 */


/**
 * Device initialisation
 *
 * Initialise and allocate resource for PCI device.
 *
 * @param	pdev	PCI device.
 * @param	id	PCI device ID.
 */
static int __devinit pm2fb_probe(struct pci_dev *pdev,
				 const struct pci_device_id *id)
{
	struct pm2fb_par *default_par;
	struct fb_info *info;
K
Krzysztof Helt 已提交
1523 1524
	int err;
	int retval = -ENXIO;
L
Linus Torvalds 已提交
1525 1526

	err = pci_enable_device(pdev);
K
Krzysztof Helt 已提交
1527
	if (err) {
L
Linus Torvalds 已提交
1528 1529 1530 1531
		printk(KERN_WARNING "pm2fb: Can't enable pdev: %d\n", err);
		return err;
	}

1532
	info = framebuffer_alloc(sizeof(struct pm2fb_par), &pdev->dev);
K
Krzysztof Helt 已提交
1533
	if (!info)
L
Linus Torvalds 已提交
1534
		return -ENOMEM;
1535
	default_par = info->par;
L
Linus Torvalds 已提交
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563

	switch (pdev->device) {
	case  PCI_DEVICE_ID_TI_TVP4020:
		strcpy(pm2fb_fix.id, "TVP4020");
		default_par->type = PM2_TYPE_PERMEDIA2;
		break;
	case  PCI_DEVICE_ID_3DLABS_PERMEDIA2:
		strcpy(pm2fb_fix.id, "Permedia2");
		default_par->type = PM2_TYPE_PERMEDIA2;
		break;
	case  PCI_DEVICE_ID_3DLABS_PERMEDIA2V:
		strcpy(pm2fb_fix.id, "Permedia2v");
		default_par->type = PM2_TYPE_PERMEDIA2V;
		break;
	}

	pm2fb_fix.mmio_start = pci_resource_start(pdev, 0);
	pm2fb_fix.mmio_len = PM2_REGS_SIZE;

#if defined(__BIG_ENDIAN)
	/*
	 * PM2 has a 64k register file, mapped twice in 128k. Lower
	 * map is little-endian, upper map is big-endian.
	 */
	pm2fb_fix.mmio_start += PM2_REGS_SIZE;
	DPRINTK("Adjusting register base for big-endian.\n");
#endif
	DPRINTK("Register base at 0x%lx\n", pm2fb_fix.mmio_start);
K
Krzysztof Helt 已提交
1564

L
Linus Torvalds 已提交
1565
	/* Registers - request region and map it. */
K
Krzysztof Helt 已提交
1566 1567
	if (!request_mem_region(pm2fb_fix.mmio_start, pm2fb_fix.mmio_len,
				"pm2fb regbase")) {
L
Linus Torvalds 已提交
1568 1569 1570 1571 1572
		printk(KERN_WARNING "pm2fb: Can't reserve regbase.\n");
		goto err_exit_neither;
	}
	default_par->v_regs =
		ioremap_nocache(pm2fb_fix.mmio_start, pm2fb_fix.mmio_len);
K
Krzysztof Helt 已提交
1573
	if (!default_par->v_regs) {
L
Linus Torvalds 已提交
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
		printk(KERN_WARNING "pm2fb: Can't remap %s register area.\n",
		       pm2fb_fix.id);
		release_mem_region(pm2fb_fix.mmio_start, pm2fb_fix.mmio_len);
		goto err_exit_neither;
	}

	/* Stash away memory register info for use when we reset the board */
	default_par->mem_control = pm2_RD(default_par, PM2R_MEM_CONTROL);
	default_par->boot_address = pm2_RD(default_par, PM2R_BOOT_ADDRESS);
	default_par->mem_config = pm2_RD(default_par, PM2R_MEM_CONFIG);
	DPRINTK("MemControl 0x%x BootAddress 0x%x MemConfig 0x%x\n",
		default_par->mem_control, default_par->boot_address,
		default_par->mem_config);

K
Krzysztof Helt 已提交
1588
	if (default_par->mem_control == 0 &&
1589
		default_par->boot_address == 0x31 &&
1590
		default_par->mem_config == 0x259fffff) {
1591
		default_par->memclock = CVPPC_MEMCLOCK;
K
Krzysztof Helt 已提交
1592 1593 1594
		default_par->mem_control = 0;
		default_par->boot_address = 0x20;
		default_par->mem_config = 0xe6002021;
1595 1596
		if (pdev->subsystem_vendor == 0x1048 &&
			pdev->subsystem_device == 0x0a31) {
K
Krzysztof Helt 已提交
1597 1598
			DPRINTK("subsystem_vendor: %04x, "
				"subsystem_device: %04x\n",
1599
				pdev->subsystem_vendor, pdev->subsystem_device);
K
Krzysztof Helt 已提交
1600 1601
			DPRINTK("We have not been initialized by VGA BIOS and "
				"are running on an Elsa Winner 2000 Office\n");
1602
			DPRINTK("Initializing card timings manually...\n");
1603
			default_par->memclock = 100000;
1604 1605 1606
		}
		if (pdev->subsystem_vendor == 0x3d3d &&
			pdev->subsystem_device == 0x0100) {
K
Krzysztof Helt 已提交
1607 1608
			DPRINTK("subsystem_vendor: %04x, "
				"subsystem_device: %04x\n",
1609
				pdev->subsystem_vendor, pdev->subsystem_device);
K
Krzysztof Helt 已提交
1610 1611
			DPRINTK("We have not been initialized by VGA BIOS and "
				"are running on an 3dlabs reference board\n");
1612
			DPRINTK("Initializing card timings manually...\n");
K
Krzysztof Helt 已提交
1613
			default_par->memclock = 74894;
1614
		}
1615 1616
	}

L
Linus Torvalds 已提交
1617
	/* Now work out how big lfb is going to be. */
K
Krzysztof Helt 已提交
1618
	switch (default_par->mem_config & PM2F_MEM_CONFIG_RAM_MASK) {
L
Linus Torvalds 已提交
1619
	case PM2F_MEM_BANKS_1:
K
Krzysztof Helt 已提交
1620
		pm2fb_fix.smem_len = 0x200000;
L
Linus Torvalds 已提交
1621 1622
		break;
	case PM2F_MEM_BANKS_2:
K
Krzysztof Helt 已提交
1623
		pm2fb_fix.smem_len = 0x400000;
L
Linus Torvalds 已提交
1624 1625
		break;
	case PM2F_MEM_BANKS_3:
K
Krzysztof Helt 已提交
1626
		pm2fb_fix.smem_len = 0x600000;
L
Linus Torvalds 已提交
1627 1628
		break;
	case PM2F_MEM_BANKS_4:
K
Krzysztof Helt 已提交
1629
		pm2fb_fix.smem_len = 0x800000;
L
Linus Torvalds 已提交
1630 1631 1632 1633 1634
		break;
	}
	pm2fb_fix.smem_start = pci_resource_start(pdev, 1);

	/* Linear frame buffer - request region and map it. */
K
Krzysztof Helt 已提交
1635 1636
	if (!request_mem_region(pm2fb_fix.smem_start, pm2fb_fix.smem_len,
				"pm2fb smem")) {
L
Linus Torvalds 已提交
1637 1638 1639
		printk(KERN_WARNING "pm2fb: Can't reserve smem.\n");
		goto err_exit_mmio;
	}
1640
	info->screen_base =
L
Linus Torvalds 已提交
1641
		ioremap_nocache(pm2fb_fix.smem_start, pm2fb_fix.smem_len);
K
Krzysztof Helt 已提交
1642
	if (!info->screen_base) {
L
Linus Torvalds 已提交
1643 1644 1645 1646 1647
		printk(KERN_WARNING "pm2fb: Can't ioremap smem area.\n");
		release_mem_region(pm2fb_fix.smem_start, pm2fb_fix.smem_len);
		goto err_exit_mmio;
	}

1648 1649 1650 1651 1652 1653 1654 1655 1656
#ifdef CONFIG_MTRR
	default_par->mtrr_handle = -1;
	if (!nomtrr)
		default_par->mtrr_handle =
			mtrr_add(pm2fb_fix.smem_start,
				 pm2fb_fix.smem_len,
				 MTRR_TYPE_WRCOMB, 1);
#endif

L
Linus Torvalds 已提交
1657
	info->fbops		= &pm2fb_ops;
K
Krzysztof Helt 已提交
1658
	info->fix		= pm2fb_fix;
1659
	info->pseudo_palette	= default_par->palette;
L
Linus Torvalds 已提交
1660
	info->flags		= FBINFO_DEFAULT |
K
Krzysztof Helt 已提交
1661 1662
				  FBINFO_HWACCEL_YPAN |
				  FBINFO_HWACCEL_COPYAREA |
K
Krzysztof Helt 已提交
1663
				  FBINFO_HWACCEL_IMAGEBLIT |
K
Krzysztof Helt 已提交
1664
				  FBINFO_HWACCEL_FILLRECT;
L
Linus Torvalds 已提交
1665

K
Krzysztof Helt 已提交
1666 1667
	info->pixmap.addr = kmalloc(PM2_PIXMAP_SIZE, GFP_KERNEL);
	if (!info->pixmap.addr) {
K
Krzysztof Helt 已提交
1668
		retval = -ENOMEM;
K
Krzysztof Helt 已提交
1669 1670 1671 1672 1673 1674 1675 1676
		goto err_exit_pixmap;
	}
	info->pixmap.size = PM2_PIXMAP_SIZE;
	info->pixmap.buf_align = 4;
	info->pixmap.scan_align = 4;
	info->pixmap.access_align = 32;
	info->pixmap.flags = FB_PIXMAP_SYSTEM;

1677
	if (noaccel) {
K
Krzysztof Helt 已提交
1678 1679 1680
		printk(KERN_DEBUG "disabling acceleration\n");
		info->flags |= FBINFO_HWACCEL_DISABLED;
		info->pixmap.scan_align = 1;
1681 1682
	}

1683 1684
	if (!mode_option)
		mode_option = "640x480@60";
K
Krzysztof Helt 已提交
1685

1686
	err = fb_find_mode(&info->var, info, mode_option, NULL, 0, NULL, 8);
L
Linus Torvalds 已提交
1687 1688 1689
	if (!err || err == 4)
		info->var = pm2fb_var;

1690 1691
	retval = fb_alloc_cmap(&info->cmap, 256, 0);
	if (retval < 0)
1692
		goto err_exit_both;
L
Linus Torvalds 已提交
1693

1694 1695
	retval = register_framebuffer(info);
	if (retval < 0)
1696
		goto err_exit_all;
L
Linus Torvalds 已提交
1697 1698

	printk(KERN_INFO "fb%d: %s frame buffer device, memory = %dK.\n",
1699
	       info->node, info->fix.id, pm2fb_fix.smem_len / 1024);
L
Linus Torvalds 已提交
1700 1701 1702 1703 1704 1705 1706 1707 1708

	/*
	 * Our driver data
	 */
	pci_set_drvdata(pdev, info);

	return 0;

 err_exit_all:
K
Krzysztof Helt 已提交
1709 1710
	fb_dealloc_cmap(&info->cmap);
 err_exit_both:
K
Krzysztof Helt 已提交
1711 1712
	kfree(info->pixmap.addr);
 err_exit_pixmap:
L
Linus Torvalds 已提交
1713 1714 1715 1716 1717 1718 1719
	iounmap(info->screen_base);
	release_mem_region(pm2fb_fix.smem_start, pm2fb_fix.smem_len);
 err_exit_mmio:
	iounmap(default_par->v_regs);
	release_mem_region(pm2fb_fix.mmio_start, pm2fb_fix.mmio_len);
 err_exit_neither:
	framebuffer_release(info);
K
Krzysztof Helt 已提交
1720
	return retval;
L
Linus Torvalds 已提交
1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731
}

/**
 * Device removal.
 *
 * Release all device resources.
 *
 * @param	pdev	PCI device to clean up.
 */
static void __devexit pm2fb_remove(struct pci_dev *pdev)
{
K
Krzysztof Helt 已提交
1732 1733
	struct fb_info *info = pci_get_drvdata(pdev);
	struct fb_fix_screeninfo *fix = &info->fix;
L
Linus Torvalds 已提交
1734 1735 1736
	struct pm2fb_par *par = info->par;

	unregister_framebuffer(info);
K
Krzysztof Helt 已提交
1737

1738 1739 1740 1741 1742
#ifdef CONFIG_MTRR
	if (par->mtrr_handle >= 0)
		mtrr_del(par->mtrr_handle, info->fix.smem_start,
			 info->fix.smem_len);
#endif /* CONFIG_MTRR */
L
Linus Torvalds 已提交
1743 1744 1745 1746 1747 1748
	iounmap(info->screen_base);
	release_mem_region(fix->smem_start, fix->smem_len);
	iounmap(par->v_regs);
	release_mem_region(fix->mmio_start, fix->mmio_len);

	pci_set_drvdata(pdev, NULL);
1749
	fb_dealloc_cmap(&info->cmap);
K
Krzysztof Helt 已提交
1750
	kfree(info->pixmap.addr);
1751
	framebuffer_release(info);
L
Linus Torvalds 已提交
1752 1753 1754 1755
}

static struct pci_device_id pm2fb_id_table[] = {
	{ PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_TVP4020,
1756
	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
L
Linus Torvalds 已提交
1757
	{ PCI_VENDOR_ID_3DLABS, PCI_DEVICE_ID_3DLABS_PERMEDIA2,
1758
	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1759
	{ PCI_VENDOR_ID_3DLABS, PCI_DEVICE_ID_3DLABS_PERMEDIA2V,
1760
	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
L
Linus Torvalds 已提交
1761 1762 1763 1764 1765
	{ 0, }
};

static struct pci_driver pm2fb_driver = {
	.name		= "pm2fb",
K
Krzysztof Helt 已提交
1766 1767 1768
	.id_table	= pm2fb_id_table,
	.probe		= pm2fb_probe,
	.remove		= __devexit_p(pm2fb_remove),
L
Linus Torvalds 已提交
1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
};

MODULE_DEVICE_TABLE(pci, pm2fb_id_table);


#ifndef MODULE
/**
 * Parse user speficied options.
 *
 * This is, comma-separated options following `video=pm2fb:'.
 */
static int __init pm2fb_setup(char *options)
{
K
Krzysztof Helt 已提交
1782
	char *this_opt;
L
Linus Torvalds 已提交
1783 1784 1785 1786

	if (!options || !*options)
		return 0;

K
Krzysztof Helt 已提交
1787
	while ((this_opt = strsep(&options, ",")) != NULL) {
L
Linus Torvalds 已提交
1788 1789
		if (!*this_opt)
			continue;
K
Krzysztof Helt 已提交
1790
		if (!strcmp(this_opt, "lowhsync"))
L
Linus Torvalds 已提交
1791
			lowhsync = 1;
K
Krzysztof Helt 已提交
1792
		else if (!strcmp(this_opt, "lowvsync"))
L
Linus Torvalds 已提交
1793
			lowvsync = 1;
1794 1795
		else if (!strncmp(this_opt, "hwcursor=", 9))
			hwcursor = simple_strtoul(this_opt + 9, NULL, 0);
1796
#ifdef CONFIG_MTRR
K
Krzysztof Helt 已提交
1797
		else if (!strncmp(this_opt, "nomtrr", 6))
1798 1799
			nomtrr = 1;
#endif
K
Krzysztof Helt 已提交
1800
		else if (!strncmp(this_opt, "noaccel", 7))
1801
			noaccel = 1;
K
Krzysztof Helt 已提交
1802
		else
1803
			mode_option = this_opt;
L
Linus Torvalds 已提交
1804 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 1833 1834 1835 1836 1837 1838
	}
	return 0;
}
#endif


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

	if (fb_get_options("pm2fb", &option))
		return -ENODEV;
	pm2fb_setup(option);
#endif

	return pci_register_driver(&pm2fb_driver);
}

module_init(pm2fb_init);

#ifdef MODULE
/*
 *  Cleanup
 */

static void __exit pm2fb_exit(void)
{
	pci_unregister_driver(&pm2fb_driver);
}
#endif

#ifdef MODULE
module_exit(pm2fb_exit);

1839 1840
module_param(mode_option, charp, 0);
MODULE_PARM_DESC(mode_option, "Initial video mode e.g. '648x480-8@60'");
1841 1842
module_param_named(mode, mode_option, charp, 0);
MODULE_PARM_DESC(mode, "Initial video mode e.g. '648x480-8@60' (deprecated)");
L
Linus Torvalds 已提交
1843 1844 1845 1846
module_param(lowhsync, bool, 0);
MODULE_PARM_DESC(lowhsync, "Force horizontal sync low regardless of mode");
module_param(lowvsync, bool, 0);
MODULE_PARM_DESC(lowvsync, "Force vertical sync low regardless of mode");
1847 1848
module_param(noaccel, bool, 0);
MODULE_PARM_DESC(noaccel, "Disable acceleration");
1849 1850
module_param(hwcursor, int, 0644);
MODULE_PARM_DESC(hwcursor, "Enable hardware cursor "
1851
			"(1=enable, 0=disable, default=1)");
1852 1853 1854 1855
#ifdef CONFIG_MTRR
module_param(nomtrr, bool, 0);
MODULE_PARM_DESC(nomtrr, "Disable MTRR support (0 or 1=disabled) (default=0)");
#endif
L
Linus Torvalds 已提交
1856 1857 1858 1859 1860

MODULE_AUTHOR("Jim Hague <jim.hague@acm.org>");
MODULE_DESCRIPTION("Permedia2 framebuffer device driver");
MODULE_LICENSE("GPL");
#endif