pm3fb.c 25.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 *  linux/drivers/video/pm3fb.c -- 3DLabs Permedia3 frame buffer device
3 4 5 6 7 8
 *
 *  Copyright (C) 2001 Romain Dolbeau <romain@dolbeau.org>.
 *
 *  Ported to 2.6 kernel on 1 May 2007 by Krzysztof Helt <krzysztof.h1@wp.pl>
 *	based on pm2fb.c
 *
L
Linus Torvalds 已提交
9
 *  Based on code written by:
10 11 12
 *	   Sven Luther, <luther@dpt-info.u-strasbg.fr>
 *	   Alan Hourihane, <alanh@fairlite.demon.co.uk>
 *	   Russell King, <rmk@arm.linux.org.uk>
L
Linus Torvalds 已提交
13 14 15
 *  Based on linux/drivers/video/skeletonfb.c:
 *	Copyright (C) 1997 Geert Uytterhoeven
 *  Based on linux/driver/video/pm2fb.c:
16 17
 *	Copyright (C) 1998-1999 Ilario Nardinocchi (nardinoc@CS.UniBO.IT)
 *	Copyright (C) 1999 Jakub Jelinek (jakub@redhat.com)
L
Linus Torvalds 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 *
 *  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.
 *
 */

#include <linux/module.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>

36
#include <video/pm3fb.h>
L
Linus Torvalds 已提交
37

38 39
#if !defined(CONFIG_PCI)
#error "Only generic PCI cards supported."
L
Linus Torvalds 已提交
40 41
#endif

42 43 44 45 46
#undef PM3FB_MASTER_DEBUG
#ifdef PM3FB_MASTER_DEBUG
#define DPRINTK(a,b...)	printk(KERN_DEBUG "pm3fb: %s: " a, __FUNCTION__ , ## b)
#else
#define DPRINTK(a,b...)
L
Linus Torvalds 已提交
47 48
#endif

49 50 51 52
/*
 * Driver data
 */
static char *mode_option __devinitdata;
L
Linus Torvalds 已提交
53

54 55 56 57 58 59 60 61 62 63 64
/*
 * This structure defines the hardware state of the graphics card. Normally
 * you place this in a header file in linux/include/video. This file usually
 * also includes register information. That allows other driver subsystems
 * and userland applications the ability to use the same header file to
 * avoid duplicate work and easy porting of software.
 */
struct pm3_par {
	unsigned char	__iomem *v_regs;/* virtual address of p_regs */
	u32		video;		/* video flags before blanking */
	u32		base;		/* screen base (xoffset+yoffset) in 128 bits unit */
K
Krzysztof Helt 已提交
65
	u32		palette[16];
L
Linus Torvalds 已提交
66 67
};

68 69 70 71 72 73 74 75 76 77 78 79 80
/*
 * Here we define the default structs fb_fix_screeninfo and fb_var_screeninfo
 * if we don't use modedb. If we do use modedb see pm3fb_init how to use it
 * to get a fb_var_screeninfo. Otherwise define a default var as well.
 */
static struct fb_fix_screeninfo pm3fb_fix __devinitdata = {
	.id =		"Permedia3",
	.type =		FB_TYPE_PACKED_PIXELS,
	.visual =	FB_VISUAL_PSEUDOCOLOR,
	.xpanstep =	1,
	.ypanstep =	1,
	.ywrapstep =	0,
	.accel =	FB_ACCEL_NONE,
L
Linus Torvalds 已提交
81 82
};

83 84 85
/*
 * Utility functions
 */
L
Linus Torvalds 已提交
86

87 88 89 90
static inline u32 PM3_READ_REG(struct pm3_par *par, s32 off)
{
	return fb_readl(par->v_regs + off);
}
L
Linus Torvalds 已提交
91

92 93 94 95
static inline void PM3_WRITE_REG(struct pm3_par *par, s32 off, u32 v)
{
	fb_writel(v, par->v_regs + off);
}
L
Linus Torvalds 已提交
96

97 98 99
static inline void PM3_WAIT(struct pm3_par *par, u32 n)
{
	while (PM3_READ_REG(par, PM3InFIFOSpace) < n);
L
Linus Torvalds 已提交
100 101
}

102
static inline void PM3_WRITE_DAC_REG(struct pm3_par *par, unsigned r, u8 v)
L
Linus Torvalds 已提交
103
{
K
Krzysztof Helt 已提交
104 105 106
	PM3_WAIT(par, 3);
	PM3_WRITE_REG(par, PM3RD_IndexHigh, (r >> 8) & 0xff);
	PM3_WRITE_REG(par, PM3RD_IndexLow, r & 0xff);
107 108
	wmb();
	PM3_WRITE_REG(par, PM3RD_IndexedData, v);
K
Krzysztof Helt 已提交
109
	wmb();
L
Linus Torvalds 已提交
110 111
}

112 113
static inline void pm3fb_set_color(struct pm3_par *par, unsigned char regno,
			unsigned char r, unsigned char g, unsigned char b)
L
Linus Torvalds 已提交
114
{
K
Krzysztof Helt 已提交
115 116 117 118 119 120 121 122 123
	PM3_WAIT(par, 4);
	PM3_WRITE_REG(par, PM3RD_PaletteWriteAddress, regno);
	wmb();
	PM3_WRITE_REG(par, PM3RD_PaletteData, r);
	wmb();
	PM3_WRITE_REG(par, PM3RD_PaletteData, g);
	wmb();
	PM3_WRITE_REG(par, PM3RD_PaletteData, b);
	wmb();
124 125 126 127 128 129 130
}

static void pm3fb_clear_colormap(struct pm3_par *par,
			unsigned char r, unsigned char g, unsigned char b)
{
	int i;

K
Krzysztof Helt 已提交
131
	for (i = 0; i < 256 ; i++)
132 133
		pm3fb_set_color(par, i, r, g, b);

L
Linus Torvalds 已提交
134 135 136
}

/* Calculating various clock parameter */
137 138 139 140
static void pm3fb_calculate_clock(unsigned long reqclock,
				unsigned char *prescale,
				unsigned char *feedback,
				unsigned char *postscale)
L
Linus Torvalds 已提交
141 142 143 144
{
	int f, pre, post;
	unsigned long freq;
	long freqerr = 1000;
145
	long currerr;
L
Linus Torvalds 已提交
146 147 148 149

	for (f = 1; f < 256; f++) {
		for (pre = 1; pre < 256; pre++) {
			for (post = 0; post < 5; post++) {
150 151 152 153 154 155
				freq = ((2*PM3_REF_CLOCK * f) >> post) / pre;
				currerr = (reqclock > freq)
					? reqclock - freq
					: freq - reqclock;
				if (currerr < freqerr) {
					freqerr = currerr;
L
Linus Torvalds 已提交
156 157 158 159 160 161 162 163 164
					*feedback = f;
					*prescale = pre;
					*postscale = post;
				}
			}
		}
	}
}

K
Krzysztof Helt 已提交
165
static inline int pm3fb_depth(const struct fb_var_screeninfo *var)
L
Linus Torvalds 已提交
166
{
K
Krzysztof Helt 已提交
167 168 169 170 171 172 173 174 175 176
	if ( var->bits_per_pixel == 16 )
		return var->red.length + var->green.length
			+ var->blue.length;

	return var->bits_per_pixel;
}

static inline int pm3fb_shift_bpp(unsigned bpp, int v)
{
	switch (bpp) {
L
Linus Torvalds 已提交
177 178 179 180 181 182 183
	case 8:
		return (v >> 4);
	case 16:
		return (v >> 3);
	case 32:
		return (v >> 2);
	}
K
Krzysztof Helt 已提交
184
	DPRINTK("Unsupported depth %u\n", bpp);
185
	return 0;
L
Linus Torvalds 已提交
186 187 188
}

/* write the mode to registers */
189
static void pm3fb_write_mode(struct fb_info *info)
L
Linus Torvalds 已提交
190
{
191
	struct pm3_par *par = info->par;
L
Linus Torvalds 已提交
192
	char tempsync = 0x00, tempmisc = 0x00;
193 194 195 196 197 198 199 200 201 202
	const u32 hsstart = info->var.right_margin;
	const u32 hsend = hsstart + info->var.hsync_len;
	const u32 hbend = hsend + info->var.left_margin;
	const u32 xres = (info->var.xres + 31) & ~31;
	const u32 htotal = xres + hbend;
	const u32 vsstart = info->var.lower_margin;
	const u32 vsend = vsstart + info->var.vsync_len;
	const u32 vbend = vsend + info->var.upper_margin;
	const u32 vtotal = info->var.yres + vbend;
	const u32 width = (info->var.xres_virtual + 7) & ~7;
K
Krzysztof Helt 已提交
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
	const unsigned bpp = info->var.bits_per_pixel;

	PM3_WAIT(par, 20);
	PM3_WRITE_REG(par, PM3MemBypassWriteMask, 0xffffffff);
	PM3_WRITE_REG(par, PM3Aperture0, 0x00000000);
	PM3_WRITE_REG(par, PM3Aperture1, 0x00000000);
	PM3_WRITE_REG(par, PM3FIFODis, 0x00000007);

	PM3_WRITE_REG(par, PM3HTotal,
			   pm3fb_shift_bpp(bpp, htotal - 1));
	PM3_WRITE_REG(par, PM3HsEnd,
			   pm3fb_shift_bpp(bpp, hsend));
	PM3_WRITE_REG(par, PM3HsStart,
			   pm3fb_shift_bpp(bpp, hsstart));
	PM3_WRITE_REG(par, PM3HbEnd,
			   pm3fb_shift_bpp(bpp, hbend));
	PM3_WRITE_REG(par, PM3HgEnd,
			   pm3fb_shift_bpp(bpp, hbend));
	PM3_WRITE_REG(par, PM3ScreenStride,
			   pm3fb_shift_bpp(bpp, width));
	PM3_WRITE_REG(par, PM3VTotal, vtotal - 1);
	PM3_WRITE_REG(par, PM3VsEnd, vsend - 1);
	PM3_WRITE_REG(par, PM3VsStart, vsstart - 1);
	PM3_WRITE_REG(par, PM3VbEnd, vbend);

	switch (bpp) {
L
Linus Torvalds 已提交
229
	case 8:
K
Krzysztof Helt 已提交
230
		PM3_WRITE_REG(par, PM3ByAperture1Mode,
L
Linus Torvalds 已提交
231
				   PM3ByApertureMode_PIXELSIZE_8BIT);
K
Krzysztof Helt 已提交
232
		PM3_WRITE_REG(par, PM3ByAperture2Mode,
L
Linus Torvalds 已提交
233 234 235 236 237
				   PM3ByApertureMode_PIXELSIZE_8BIT);
		break;

	case 16:
#ifndef __BIG_ENDIAN
K
Krzysztof Helt 已提交
238
		PM3_WRITE_REG(par, PM3ByAperture1Mode,
L
Linus Torvalds 已提交
239
				   PM3ByApertureMode_PIXELSIZE_16BIT);
K
Krzysztof Helt 已提交
240
		PM3_WRITE_REG(par, PM3ByAperture2Mode,
L
Linus Torvalds 已提交
241 242
				   PM3ByApertureMode_PIXELSIZE_16BIT);
#else
K
Krzysztof Helt 已提交
243
		PM3_WRITE_REG(par, PM3ByAperture1Mode,
L
Linus Torvalds 已提交
244 245
				   PM3ByApertureMode_PIXELSIZE_16BIT |
				   PM3ByApertureMode_BYTESWAP_BADC);
K
Krzysztof Helt 已提交
246
		PM3_WRITE_REG(par, PM3ByAperture2Mode,
L
Linus Torvalds 已提交
247 248 249 250 251 252 253
				   PM3ByApertureMode_PIXELSIZE_16BIT |
				   PM3ByApertureMode_BYTESWAP_BADC);
#endif /* ! __BIG_ENDIAN */
		break;

	case 32:
#ifndef __BIG_ENDIAN
K
Krzysztof Helt 已提交
254
		PM3_WRITE_REG(par, PM3ByAperture1Mode,
L
Linus Torvalds 已提交
255
				   PM3ByApertureMode_PIXELSIZE_32BIT);
K
Krzysztof Helt 已提交
256
		PM3_WRITE_REG(par, PM3ByAperture2Mode,
L
Linus Torvalds 已提交
257 258
				   PM3ByApertureMode_PIXELSIZE_32BIT);
#else
K
Krzysztof Helt 已提交
259
		PM3_WRITE_REG(par, PM3ByAperture1Mode,
L
Linus Torvalds 已提交
260 261
				   PM3ByApertureMode_PIXELSIZE_32BIT |
				   PM3ByApertureMode_BYTESWAP_DCBA);
K
Krzysztof Helt 已提交
262
		PM3_WRITE_REG(par, PM3ByAperture2Mode,
L
Linus Torvalds 已提交
263 264 265 266 267 268
				   PM3ByApertureMode_PIXELSIZE_32BIT |
				   PM3ByApertureMode_BYTESWAP_DCBA);
#endif /* ! __BIG_ENDIAN */
		break;

	default:
K
Krzysztof Helt 已提交
269
		DPRINTK("Unsupported depth %d\n", bpp);
L
Linus Torvalds 已提交
270 271 272 273 274 275 276 277 278 279
		break;
	}

	/*
	 * Oxygen VX1 - it appears that setting PM3VideoControl and
	 * then PM3RD_SyncControl to the same SYNC settings undoes
	 * any net change - they seem to xor together.  Only set the
	 * sync options in PM3RD_SyncControl.  --rmk
	 */
	{
280
		unsigned int video = par->video;
L
Linus Torvalds 已提交
281 282 283 284 285

		video &= ~(PM3VideoControl_HSYNC_MASK |
			   PM3VideoControl_VSYNC_MASK);
		video |= PM3VideoControl_HSYNC_ACTIVE_HIGH |
			 PM3VideoControl_VSYNC_ACTIVE_HIGH;
K
Krzysztof Helt 已提交
286
		PM3_WRITE_REG(par, PM3VideoControl, video);
L
Linus Torvalds 已提交
287
	}
K
Krzysztof Helt 已提交
288
	PM3_WRITE_REG(par, PM3VClkCtl,
289
			   (PM3_READ_REG(par, PM3VClkCtl) & 0xFFFFFFFC));
K
Krzysztof Helt 已提交
290 291
	PM3_WRITE_REG(par, PM3ScreenBase, par->base);
	PM3_WRITE_REG(par, PM3ChipConfig,
292
			   (PM3_READ_REG(par, PM3ChipConfig) & 0xFFFFFFFD));
L
Linus Torvalds 已提交
293

K
Krzysztof Helt 已提交
294
	wmb();
L
Linus Torvalds 已提交
295
	{
296 297 298 299 300 301 302 303 304 305 306 307 308
		unsigned char uninitialized_var(m);	/* ClkPreScale */
		unsigned char uninitialized_var(n);	/* ClkFeedBackScale */
		unsigned char uninitialized_var(p);	/* ClkPostScale */
		unsigned long pixclock = PICOS2KHZ(info->var.pixclock);

		(void)pm3fb_calculate_clock(pixclock, &m, &n, &p);

		DPRINTK("Pixclock: %ld, Pre: %d, Feedback: %d, Post: %d\n",
			pixclock, (int) m, (int) n, (int) p);

		PM3_WRITE_DAC_REG(par, PM3RD_DClk0PreScale, m);
		PM3_WRITE_DAC_REG(par, PM3RD_DClk0FeedbackScale, n);
		PM3_WRITE_DAC_REG(par, PM3RD_DClk0PostScale, p);
L
Linus Torvalds 已提交
309 310
	}
	/*
311
	   PM3_WRITE_DAC_REG(par, PM3RD_IndexControl, 0x00);
L
Linus Torvalds 已提交
312 313
	 */
	/*
314
	   PM3_SLOW_WRITE_REG(par, PM3RD_IndexControl, 0x00);
L
Linus Torvalds 已提交
315
	 */
316
	if ((par->video & PM3VideoControl_HSYNC_MASK) ==
L
Linus Torvalds 已提交
317 318
	    PM3VideoControl_HSYNC_ACTIVE_HIGH)
		tempsync |= PM3RD_SyncControl_HSYNC_ACTIVE_HIGH;
319
	if ((par->video & PM3VideoControl_VSYNC_MASK) ==
L
Linus Torvalds 已提交
320 321 322
	    PM3VideoControl_VSYNC_ACTIVE_HIGH)
		tempsync |= PM3RD_SyncControl_VSYNC_ACTIVE_HIGH;

323 324 325 326 327
	PM3_WRITE_DAC_REG(par, PM3RD_SyncControl, tempsync);
	DPRINTK("PM3RD_SyncControl: %d\n", tempsync);

	PM3_WRITE_DAC_REG(par, PM3RD_DACControl, 0x00);

K
Krzysztof Helt 已提交
328
	switch (pm3fb_depth(&info->var)) {
L
Linus Torvalds 已提交
329
	case 8:
330
		PM3_WRITE_DAC_REG(par, PM3RD_PixelSize,
L
Linus Torvalds 已提交
331
				  PM3RD_PixelSize_8_BIT_PIXELS);
332
		PM3_WRITE_DAC_REG(par, PM3RD_ColorFormat,
L
Linus Torvalds 已提交
333 334 335 336 337
				  PM3RD_ColorFormat_CI8_COLOR |
				  PM3RD_ColorFormat_COLOR_ORDER_BLUE_LOW);
		tempmisc |= PM3RD_MiscControl_HIGHCOLOR_RES_ENABLE;
		break;
	case 12:
338
		PM3_WRITE_DAC_REG(par, PM3RD_PixelSize,
L
Linus Torvalds 已提交
339
				  PM3RD_PixelSize_16_BIT_PIXELS);
340
		PM3_WRITE_DAC_REG(par, PM3RD_ColorFormat,
L
Linus Torvalds 已提交
341 342 343 344 345
				  PM3RD_ColorFormat_4444_COLOR |
				  PM3RD_ColorFormat_COLOR_ORDER_BLUE_LOW |
				  PM3RD_ColorFormat_LINEAR_COLOR_EXT_ENABLE);
		tempmisc |= PM3RD_MiscControl_DIRECTCOLOR_ENABLE |
			PM3RD_MiscControl_HIGHCOLOR_RES_ENABLE;
346
		break;
L
Linus Torvalds 已提交
347
	case 15:
348
		PM3_WRITE_DAC_REG(par, PM3RD_PixelSize,
L
Linus Torvalds 已提交
349
				  PM3RD_PixelSize_16_BIT_PIXELS);
350
		PM3_WRITE_DAC_REG(par, PM3RD_ColorFormat,
L
Linus Torvalds 已提交
351 352 353 354 355
				  PM3RD_ColorFormat_5551_FRONT_COLOR |
				  PM3RD_ColorFormat_COLOR_ORDER_BLUE_LOW |
				  PM3RD_ColorFormat_LINEAR_COLOR_EXT_ENABLE);
		tempmisc |= PM3RD_MiscControl_DIRECTCOLOR_ENABLE |
			PM3RD_MiscControl_HIGHCOLOR_RES_ENABLE;
356
		break;
L
Linus Torvalds 已提交
357
	case 16:
358
		PM3_WRITE_DAC_REG(par, PM3RD_PixelSize,
L
Linus Torvalds 已提交
359
				  PM3RD_PixelSize_16_BIT_PIXELS);
360
		PM3_WRITE_DAC_REG(par, PM3RD_ColorFormat,
L
Linus Torvalds 已提交
361 362 363 364 365 366 367
				  PM3RD_ColorFormat_565_FRONT_COLOR |
				  PM3RD_ColorFormat_COLOR_ORDER_BLUE_LOW |
				  PM3RD_ColorFormat_LINEAR_COLOR_EXT_ENABLE);
		tempmisc |= PM3RD_MiscControl_DIRECTCOLOR_ENABLE |
			PM3RD_MiscControl_HIGHCOLOR_RES_ENABLE;
		break;
	case 32:
368
		PM3_WRITE_DAC_REG(par, PM3RD_PixelSize,
L
Linus Torvalds 已提交
369
				  PM3RD_PixelSize_32_BIT_PIXELS);
370
		PM3_WRITE_DAC_REG(par, PM3RD_ColorFormat,
L
Linus Torvalds 已提交
371 372 373 374 375 376
				  PM3RD_ColorFormat_8888_COLOR |
				  PM3RD_ColorFormat_COLOR_ORDER_BLUE_LOW);
		tempmisc |= PM3RD_MiscControl_DIRECTCOLOR_ENABLE |
			PM3RD_MiscControl_HIGHCOLOR_RES_ENABLE;
		break;
	}
377
	PM3_WRITE_DAC_REG(par, PM3RD_MiscControl, tempmisc);
L
Linus Torvalds 已提交
378 379
}

380 381 382 383
/*
 * hardware independent functions
 */
int pm3fb_init(void);
L
Linus Torvalds 已提交
384

385 386 387
static int pm3fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
{
	u32 lpitch;
K
Krzysztof Helt 已提交
388 389
	unsigned bpp = var->red.length + var->green.length
			+ var->blue.length + var->transp.length;
L
Linus Torvalds 已提交
390

K
Krzysztof Helt 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
	if ( bpp != var->bits_per_pixel ) {
		/* set predefined mode for bits_per_pixel settings */

		switch(var->bits_per_pixel) {
		case 8:
			var->red.length = var->green.length = var->blue.length = 8;
			var->red.offset = var->green.offset = var->blue.offset = 0;
			var->transp.offset = 0;
			var->transp.length = 0;
			break;
		case 16:
			var->red.length = var->blue.length = 5;
			var->green.length = 6;
			var->transp.length = 0;
			break;
		case 32:
			var->red.length = var->green.length = var->blue.length = 8;
			var->transp.length = 8;
			break;
		default:
			DPRINTK("depth not supported: %u\n", var->bits_per_pixel);
			return -EINVAL;
		}
	}
	/* it is assumed BGRA order */
	if (var->bits_per_pixel > 8 )
	{
		var->blue.offset = 0;
		var->green.offset = var->blue.length;
		var->red.offset = var->green.offset + var->green.length;
		var->transp.offset = var->red.offset + var->red.length;
L
Linus Torvalds 已提交
422
	}
423
	var->height = var->width = -1;
L
Linus Torvalds 已提交
424

425 426 427 428
	if (var->xres != var->xres_virtual) {
		DPRINTK("virtual x resolution != physical x resolution not supported\n");
		return -EINVAL;
	}
L
Linus Torvalds 已提交
429

430 431 432
	if (var->yres > var->yres_virtual) {
		DPRINTK("virtual y resolution < physical y resolution not possible\n");
		return -EINVAL;
L
Linus Torvalds 已提交
433 434
	}

435 436 437
	if (var->xoffset) {
		DPRINTK("xoffset not supported\n");
		return -EINVAL;
L
Linus Torvalds 已提交
438 439
	}

440 441 442
	if ((var->vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) {
		DPRINTK("interlace not supported\n");
		return -EINVAL;
L
Linus Torvalds 已提交
443 444
	}

445 446
	var->xres = (var->xres + 31) & ~31; /* could sometimes be 8 */
	lpitch = var->xres * ((var->bits_per_pixel + 7)>>3);
L
Linus Torvalds 已提交
447

448 449 450 451
	if (var->xres < 200 || var->xres > 2048) {
		DPRINTK("width not supported: %u\n", var->xres);
		return -EINVAL;
	}
L
Linus Torvalds 已提交
452

453 454 455 456
	if (var->yres < 200 || var->yres > 4095) {
		DPRINTK("height not supported: %u\n", var->yres);
		return -EINVAL;
	}
L
Linus Torvalds 已提交
457

458 459 460 461 462 463 464 465 466
	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;
	}

	if (PICOS2KHZ(var->pixclock) > PM3_MAX_PIXCLOCK) {
		DPRINTK("pixclock too high (%ldKHz)\n", PICOS2KHZ(var->pixclock));
		return -EINVAL;
L
Linus Torvalds 已提交
467 468
	}

469
	var->accel_flags = 0;	/* Can't mmap if this is on */
L
Linus Torvalds 已提交
470

471 472 473 474
	DPRINTK("Checking graphics mode at %dx%d depth %d\n",
		var->xres, var->yres, var->bits_per_pixel);
	return 0;
}
L
Linus Torvalds 已提交
475

476 477 478 479
static int pm3fb_set_par(struct fb_info *info)
{
	struct pm3_par *par = info->par;
	const u32 xres = (info->var.xres + 31) & ~31;
K
Krzysztof Helt 已提交
480
	const unsigned bpp = info->var.bits_per_pixel;
L
Linus Torvalds 已提交
481

K
Krzysztof Helt 已提交
482
	par->base = pm3fb_shift_bpp(bpp,(info->var.yoffset * xres)
483 484
					+ info->var.xoffset);
	par->video = 0;
L
Linus Torvalds 已提交
485

486 487 488 489
	if (info->var.sync & FB_SYNC_HOR_HIGH_ACT)
		par->video |= PM3VideoControl_HSYNC_ACTIVE_HIGH;
	else
		par->video |= PM3VideoControl_HSYNC_ACTIVE_LOW;
L
Linus Torvalds 已提交
490

491 492 493 494
	if (info->var.sync & FB_SYNC_VERT_HIGH_ACT)
		par->video |= PM3VideoControl_VSYNC_ACTIVE_HIGH;
	else
		par->video |= PM3VideoControl_VSYNC_ACTIVE_LOW;
L
Linus Torvalds 已提交
495

496 497 498 499
	if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE)
		par->video |= PM3VideoControl_LINE_DOUBLE_ON;
	else
		par->video |= PM3VideoControl_LINE_DOUBLE_OFF;
L
Linus Torvalds 已提交
500

501 502 503 504 505
	if (info->var.activate == FB_ACTIVATE_NOW)
		par->video |= PM3VideoControl_ENABLE;
	else {
		par->video |= PM3VideoControl_DISABLE;
		DPRINTK("PM3Video disabled\n");
L
Linus Torvalds 已提交
506
	}
K
Krzysztof Helt 已提交
507
	switch (bpp) {
508 509 510 511 512 513 514 515 516 517 518 519
	case 8:
		par->video |= PM3VideoControl_PIXELSIZE_8BIT;
		break;
	case 16:
		par->video |= PM3VideoControl_PIXELSIZE_16BIT;
		break;
	case 32:
		par->video |= PM3VideoControl_PIXELSIZE_32BIT;
		break;
	default:
		DPRINTK("Unsupported depth\n");
		break;
L
Linus Torvalds 已提交
520 521
	}

522
	info->fix.visual =
K
Krzysztof Helt 已提交
523
		(bpp == 8) ? FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
524
	info->fix.line_length = ((info->var.xres_virtual + 7)  & ~7)
K
Krzysztof Helt 已提交
525
					* bpp / 8;
L
Linus Torvalds 已提交
526

527 528 529 530 531 532
/*	pm3fb_clear_memory(info, 0);*/
	pm3fb_clear_colormap(par, 0, 0, 0);
	PM3_WRITE_DAC_REG(par, PM3RD_CursorMode,
			  PM3RD_CursorMode_CURSOR_DISABLE);
	pm3fb_write_mode(info);
	return 0;
L
Linus Torvalds 已提交
533 534
}

535 536 537
static int pm3fb_setcolreg(unsigned regno, unsigned red, unsigned green,
			   unsigned blue, unsigned transp,
			   struct fb_info *info)
L
Linus Torvalds 已提交
538
{
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
	struct pm3_par *par = info->par;

	if (regno >= 256)  /* no. of hw registers */
	   return -EINVAL;

	/* grayscale works only partially under directcolor */
	if (info->var.grayscale) {
	   /* grayscale = 0.30*R + 0.59*G + 0.11*B */
	   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
	 *   pseudo_palette[X] is programmed to (X << red.offset) |
K
Krzysztof Helt 已提交
555 556
	 *					(X << green.offset) |
	 *					(X << blue.offset)
557 558 559 560 561 562 563 564 565 566 567
	 *   RAMDAC[X] is programmed to (red, green, blue)
	 *   color depth = SUM(var->{color}.length)
	 *
	 * Pseudocolor:
	 *	var->{color}.offset is 0
	 *	var->{color}.length contains width of DAC or the number of unique
	 *			colors available (color depth)
	 *	pseudo_palette is not used
	 *	RAMDAC[X] is programmed to (red, green, blue)
	 *	color depth = var->{color}.length
	 */
L
Linus Torvalds 已提交
568

569 570 571 572 573 574 575 576 577 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
	/*
	 * This is the point where the color is converted to something that
	 * is acceptable by the hardware.
	 */
#define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
	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);
#undef CNVT_TOHW

	if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
	info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
		u32 v;

		if (regno >= 16)
			return -EINVAL;

		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:
			break;
		case 16:
		case 32:
			((u32*)(info->pseudo_palette))[regno] = v;
			break;
		}
		return 0;
	}
	else if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR)
		pm3fb_set_color(par, regno, red, green, blue);
L
Linus Torvalds 已提交
604

605
	return 0;
L
Linus Torvalds 已提交
606 607
}

608 609
static int pm3fb_pan_display(struct fb_var_screeninfo *var,
				 struct fb_info *info)
L
Linus Torvalds 已提交
610
{
611 612
	struct pm3_par *par = info->par;
	const u32 xres = (var->xres + 31) & ~31;
L
Linus Torvalds 已提交
613

614 615 616
	par->base = pm3fb_shift_bpp(var->bits_per_pixel,
					(var->yoffset * xres)
					+ var->xoffset);
K
Krzysztof Helt 已提交
617 618
	PM3_WAIT(par, 1);
	PM3_WRITE_REG(par, PM3ScreenBase, par->base);
619 620
	return 0;
}
L
Linus Torvalds 已提交
621

622 623 624 625
static int pm3fb_blank(int blank_mode, struct fb_info *info)
{
	struct pm3_par *par = info->par;
	u32 video = par->video;
L
Linus Torvalds 已提交
626

627 628 629 630 631 632 633 634 635 636
	/*
	 * Oxygen VX1 - it appears that setting PM3VideoControl and
	 * then PM3RD_SyncControl to the same SYNC settings undoes
	 * any net change - they seem to xor together.  Only set the
	 * sync options in PM3RD_SyncControl.  --rmk
	 */
	video &= ~(PM3VideoControl_HSYNC_MASK |
		   PM3VideoControl_VSYNC_MASK);
	video |= PM3VideoControl_HSYNC_ACTIVE_HIGH |
		 PM3VideoControl_VSYNC_ACTIVE_HIGH;
L
Linus Torvalds 已提交
637

638 639
	switch (blank_mode) {
	case FB_BLANK_UNBLANK:
K
Krzysztof Helt 已提交
640
		video |= PM3VideoControl_ENABLE;
641
		break;
K
Krzysztof Helt 已提交
642 643
	case FB_BLANK_NORMAL:
		video &= ~(PM3VideoControl_ENABLE);
644 645
		break;
	case FB_BLANK_HSYNC_SUSPEND:
K
Krzysztof Helt 已提交
646 647
		video &= ~(PM3VideoControl_HSYNC_MASK |
			  PM3VideoControl_BLANK_ACTIVE_LOW);
648 649
		break;
	case FB_BLANK_VSYNC_SUSPEND:
K
Krzysztof Helt 已提交
650 651
		video &= ~(PM3VideoControl_VSYNC_MASK |
			  PM3VideoControl_BLANK_ACTIVE_LOW);
652 653
		break;
	case FB_BLANK_POWERDOWN:
K
Krzysztof Helt 已提交
654 655 656
		video &= ~(PM3VideoControl_HSYNC_MASK |
			  PM3VideoControl_VSYNC_MASK |
			  PM3VideoControl_BLANK_ACTIVE_LOW);
657 658 659 660
		break;
	default:
		DPRINTK("Unsupported blanking %d\n", blank_mode);
		return 1;
L
Linus Torvalds 已提交
661 662
	}

K
Krzysztof Helt 已提交
663 664
	PM3_WAIT(par, 1);
	PM3_WRITE_REG(par,PM3VideoControl, video);
665
	return 0;
L
Linus Torvalds 已提交
666 667
}

668 669 670
	/*
	 *  Frame buffer operations
	 */
L
Linus Torvalds 已提交
671

672 673 674 675 676 677
static struct fb_ops pm3fb_ops = {
	.owner		= THIS_MODULE,
	.fb_check_var	= pm3fb_check_var,
	.fb_set_par	= pm3fb_set_par,
	.fb_setcolreg	= pm3fb_setcolreg,
	.fb_pan_display	= pm3fb_pan_display,
K
Krzysztof Helt 已提交
678 679 680
	.fb_fillrect	= cfb_fillrect,
	.fb_copyarea	= cfb_copyarea,
	.fb_imageblit	= cfb_imageblit,
681 682
	.fb_blank	= pm3fb_blank,
};
L
Linus Torvalds 已提交
683

684
/* ------------------------------------------------------------------------- */
L
Linus Torvalds 已提交
685

686 687 688
	/*
	 *  Initialization
	 */
L
Linus Torvalds 已提交
689

690 691 692
/* mmio register are already mapped when this function is called */
/* the pm3fb_fix.smem_start is also set */
static unsigned long pm3fb_size_memory(struct pm3_par *par)
L
Linus Torvalds 已提交
693
{
694 695
	unsigned long	memsize = 0, tempBypass, i, temp1, temp2;
	unsigned char	__iomem *screen_mem;
L
Linus Torvalds 已提交
696

K
Krzysztof Helt 已提交
697
	pm3fb_fix.smem_len = 64 * 1024l * 1024; /* request full aperture size */
698 699 700 701 702
	/* Linear frame buffer - request region and map it. */
	if (!request_mem_region(pm3fb_fix.smem_start, pm3fb_fix.smem_len,
				 "pm3fb smem")) {
		printk(KERN_WARNING "pm3fb: Can't reserve smem.\n");
		return 0;
L
Linus Torvalds 已提交
703
	}
704 705 706 707 708 709
	screen_mem =
		ioremap_nocache(pm3fb_fix.smem_start, pm3fb_fix.smem_len);
	if (!screen_mem) {
		printk(KERN_WARNING "pm3fb: Can't ioremap smem area.\n");
		release_mem_region(pm3fb_fix.smem_start, pm3fb_fix.smem_len);
		return 0;
L
Linus Torvalds 已提交
710 711
	}

712 713
	/* TODO: card-specific stuff, *before* accessing *any* FB memory */
	/* For Appian Jeronimo 2000 board second head */
L
Linus Torvalds 已提交
714

715
	tempBypass = PM3_READ_REG(par, PM3MemBypassWriteMask);
L
Linus Torvalds 已提交
716

717
	DPRINTK("PM3MemBypassWriteMask was: 0x%08lx\n", tempBypass);
L
Linus Torvalds 已提交
718

K
Krzysztof Helt 已提交
719 720
	PM3_WAIT(par, 1);
	PM3_WRITE_REG(par, PM3MemBypassWriteMask, 0xFFFFFFFF);
L
Linus Torvalds 已提交
721

722 723 724 725 726 727
	/* pm3 split up memory, replicates, and do a lot of nasty stuff IMHO ;-) */
	for (i = 0; i < 32; i++) {
		fb_writel(i * 0x00345678,
			  (screen_mem + (i * 1048576)));
		mb();
		temp1 = fb_readl((screen_mem + (i * 1048576)));
L
Linus Torvalds 已提交
728

729 730 731
		/* Let's check for wrapover, write will fail at 16MB boundary */
		if (temp1 == (i * 0x00345678))
			memsize = i;
L
Linus Torvalds 已提交
732
		else
733
			break;
L
Linus Torvalds 已提交
734 735
	}

736
	DPRINTK("First detect pass already got %ld MB\n", memsize + 1);
L
Linus Torvalds 已提交
737

738 739 740
	if (memsize + 1 == i) {
		for (i = 0; i < 32; i++) {
			/* Clear first 32MB ; 0 is 0, no need to byteswap */
K
Krzysztof Helt 已提交
741
			writel(0x0000000, (screen_mem + (i * 1048576)));
L
Linus Torvalds 已提交
742
		}
K
Krzysztof Helt 已提交
743
		wmb();
L
Linus Torvalds 已提交
744

745 746 747 748 749 750 751 752 753 754 755 756 757
		for (i = 32; i < 64; i++) {
			fb_writel(i * 0x00345678,
				  (screen_mem + (i * 1048576)));
			mb();
			temp1 =
			    fb_readl((screen_mem + (i * 1048576)));
			temp2 =
			    fb_readl((screen_mem + ((i - 32) * 1048576)));
			/* different value, different RAM... */
			if ((temp1 == (i * 0x00345678)) && (temp2 == 0))
				memsize = i;
			else
				break;
L
Linus Torvalds 已提交
758 759
		}
	}
760
	DPRINTK("Second detect pass got %ld MB\n", memsize + 1);
L
Linus Torvalds 已提交
761

K
Krzysztof Helt 已提交
762 763
	PM3_WAIT(par, 1);
	PM3_WRITE_REG(par, PM3MemBypassWriteMask, tempBypass);
L
Linus Torvalds 已提交
764

765 766 767
	iounmap(screen_mem);
	release_mem_region(pm3fb_fix.smem_start, pm3fb_fix.smem_len);
	memsize = 1048576 * (memsize + 1);
L
Linus Torvalds 已提交
768

769
	DPRINTK("Returning 0x%08lx bytes\n", memsize);
L
Linus Torvalds 已提交
770

771
	return memsize;
L
Linus Torvalds 已提交
772 773
}

774 775
static int __devinit pm3fb_probe(struct pci_dev *dev,
				  const struct pci_device_id *ent)
L
Linus Torvalds 已提交
776
{
777 778 779 780
	struct fb_info *info;
	struct pm3_par *par;
	struct device* device = &dev->dev; /* for pci drivers */
	int err, retval = -ENXIO;
L
Linus Torvalds 已提交
781

782 783 784 785
	err = pci_enable_device(dev);
	if (err) {
		printk(KERN_WARNING "pm3fb: Can't enable PCI dev: %d\n", err);
		return err;
L
Linus Torvalds 已提交
786
	}
787 788 789 790
	/*
	 * Dynamically allocate info and par
	 */
	info = framebuffer_alloc(sizeof(struct pm3_par), device);
L
Linus Torvalds 已提交
791

792 793 794
	if (!info)
		return -ENOMEM;
	par = info->par;
L
Linus Torvalds 已提交
795

796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
	/*
	 * Here we set the screen_base to the virtual memory address
	 * for the framebuffer.
	 */
	pm3fb_fix.mmio_start = pci_resource_start(dev, 0);
	pm3fb_fix.mmio_len = PM3_REGS_SIZE;

	/* Registers - request region and map it. */
	if (!request_mem_region(pm3fb_fix.mmio_start, pm3fb_fix.mmio_len,
				 "pm3fb regbase")) {
		printk(KERN_WARNING "pm3fb: Can't reserve regbase.\n");
		goto err_exit_neither;
	}
	par->v_regs =
		ioremap_nocache(pm3fb_fix.mmio_start, pm3fb_fix.mmio_len);
	if (!par->v_regs) {
		printk(KERN_WARNING "pm3fb: Can't remap %s register area.\n",
			pm3fb_fix.id);
		release_mem_region(pm3fb_fix.mmio_start, pm3fb_fix.mmio_len);
		goto err_exit_neither;
	}

#if defined(__BIG_ENDIAN)
	pm3fb_fix.mmio_start += PM3_REGS_SIZE;
	DPRINTK("Adjusting register base for big-endian.\n");
#endif
	/* Linear frame buffer - request region and map it. */
	pm3fb_fix.smem_start = pci_resource_start(dev, 1);
	pm3fb_fix.smem_len = pm3fb_size_memory(par);
	if (!pm3fb_fix.smem_len)
	{
		printk(KERN_WARNING "pm3fb: Can't find memory on board.\n");
		goto err_exit_mmio;
L
Linus Torvalds 已提交
829
	}
830 831 832 833
	if (!request_mem_region(pm3fb_fix.smem_start, pm3fb_fix.smem_len,
				 "pm3fb smem")) {
		printk(KERN_WARNING "pm3fb: Can't reserve smem.\n");
		goto err_exit_mmio;
L
Linus Torvalds 已提交
834
	}
835 836 837 838 839 840
	info->screen_base =
		ioremap_nocache(pm3fb_fix.smem_start, pm3fb_fix.smem_len);
	if (!info->screen_base) {
		printk(KERN_WARNING "pm3fb: Can't ioremap smem area.\n");
		release_mem_region(pm3fb_fix.smem_start, pm3fb_fix.smem_len);
		goto err_exit_mmio;
L
Linus Torvalds 已提交
841
	}
842
	info->screen_size = pm3fb_fix.smem_len;
L
Linus Torvalds 已提交
843

844
	info->fbops = &pm3fb_ops;
L
Linus Torvalds 已提交
845

846
	par->video = PM3_READ_REG(par, PM3VideoControl);
L
Linus Torvalds 已提交
847

848 849 850
	info->fix = pm3fb_fix;
	info->pseudo_palette = par->palette;
	info->flags = FBINFO_DEFAULT;/* | FBINFO_HWACCEL_YPAN;*/
L
Linus Torvalds 已提交
851

852 853 854 855 856 857
	/*
	 * This should give a reasonable default video mode. The following is
	 * done when we can set a video mode.
	 */
	if (!mode_option)
		mode_option = "640x480@60";
L
Linus Torvalds 已提交
858

859
	retval = fb_find_mode(&info->var, info, mode_option, NULL, 0, NULL, 8);
L
Linus Torvalds 已提交
860

861 862 863
	if (!retval || retval == 4) {
		retval = -EINVAL;
		goto err_exit_both;
L
Linus Torvalds 已提交
864 865
	}

866 867 868
	if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
		retval = -ENOMEM;
		goto err_exit_both;
L
Linus Torvalds 已提交
869 870
	}

871 872 873 874
	/*
	 * For drivers that can...
	 */
	pm3fb_check_var(&info->var, info);
L
Linus Torvalds 已提交
875

876 877 878
	if (register_framebuffer(info) < 0) {
		retval = -EINVAL;
		goto err_exit_all;
L
Linus Torvalds 已提交
879
	}
880 881
	printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
	   info->fix.id);
K
Krzysztof Helt 已提交
882
	pci_set_drvdata(dev, info);
883
	return 0;
L
Linus Torvalds 已提交
884

885 886 887 888 889 890 891 892 893 894 895
 err_exit_all:
	fb_dealloc_cmap(&info->cmap);
 err_exit_both:
	iounmap(info->screen_base);
	release_mem_region(pm3fb_fix.smem_start, pm3fb_fix.smem_len);
 err_exit_mmio:
	iounmap(par->v_regs);
	release_mem_region(pm3fb_fix.mmio_start, pm3fb_fix.mmio_len);
 err_exit_neither:
	framebuffer_release(info);
	return retval;
L
Linus Torvalds 已提交
896 897
}

898 899 900 901
	/*
	 *  Cleanup
	 */
static void __devexit pm3fb_remove(struct pci_dev *dev)
L
Linus Torvalds 已提交
902
{
903
	struct fb_info *info = pci_get_drvdata(dev);
L
Linus Torvalds 已提交
904

905 906 907
	if (info) {
		struct fb_fix_screeninfo *fix = &info->fix;
		struct pm3_par *par = info->par;
L
Linus Torvalds 已提交
908

909 910
		unregister_framebuffer(info);
		fb_dealloc_cmap(&info->cmap);
L
Linus Torvalds 已提交
911

912 913 914 915
		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);
L
Linus Torvalds 已提交
916

917 918
		pci_set_drvdata(dev, NULL);
		framebuffer_release(info);
L
Linus Torvalds 已提交
919 920 921
	}
}

922 923
static struct pci_device_id pm3fb_id_table[] = {
	{ PCI_VENDOR_ID_3DLABS, 0x0a,
K
Krzysztof Helt 已提交
924
	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
925 926
	{ 0, }
};
L
Linus Torvalds 已提交
927

928 929 930 931 932 933 934
/* For PCI drivers */
static struct pci_driver pm3fb_driver = {
	.name =		"pm3fb",
	.id_table =	pm3fb_id_table,
	.probe =	pm3fb_probe,
	.remove =	__devexit_p(pm3fb_remove),
};
L
Linus Torvalds 已提交
935

936
MODULE_DEVICE_TABLE(pci, pm3fb_id_table);
L
Linus Torvalds 已提交
937

K
Krzysztof Helt 已提交
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
#ifndef MODULE
	/*
	 *  Setup
	 */

/*
 * Only necessary if your driver takes special options,
 * otherwise we fall back on the generic fb_setup().
 */
static int __init pm3fb_setup(char *options)
{
	/* Parse user speficied options (`video=pm3fb:') */
	return 0;
}
#endif /* MODULE */

954
int __init pm3fb_init(void)
L
Linus Torvalds 已提交
955 956
{
	/*
957
	 *  For kernel boot options (in 'video=pm3fb:<options>' format)
L
Linus Torvalds 已提交
958
	 */
959 960
#ifndef MODULE
	char *option = NULL;
L
Linus Torvalds 已提交
961

962 963 964
	if (fb_get_options("pm3fb", &option))
		return -ENODEV;
	pm3fb_setup(option);
L
Linus Torvalds 已提交
965 966
#endif

967
	return pci_register_driver(&pm3fb_driver);
L
Linus Torvalds 已提交
968 969
}

970
static void __exit pm3fb_exit(void)
L
Linus Torvalds 已提交
971
{
972
	pci_unregister_driver(&pm3fb_driver);
L
Linus Torvalds 已提交
973 974
}

975 976 977 978
module_init(pm3fb_init);
module_exit(pm3fb_exit);

MODULE_LICENSE("GPL");