cyber2000fb.c 42.5 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5
/*
 *  linux/drivers/video/cyber2000fb.c
 *
 *  Copyright (C) 1998-2000 Russell King
 *
Linus Torvalds's avatar
Linus Torvalds committed
6 7 8
 *  MIPS and 50xx clock support
 *  Copyright (C) 2001 Bradley D. LaRonde <brad@ltc.com>
 *
Linus Torvalds's avatar
Linus Torvalds committed
9 10 11 12 13 14 15 16 17 18
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Integraphics CyberPro 2000, 2010 and 5000 frame buffer device
 *
 * Based on cyberfb.c.
 *
 * Note that we now use the new fbcon fix, var and cmap scheme.  We do still
 * have to check which console is the currently displayed one however, since
19
 * especially for the colourmap stuff.
Linus Torvalds's avatar
Linus Torvalds committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 *
 * We also use the new hotplug PCI subsystem.  I'm not sure if there are any
 * such cards, but I'm erring on the side of caution.  We don't want to go
 * pop just because someone does have one.
 *
 * Note that this doesn't work fully in the case of multiple CyberPro cards
 * with grabbers.  We currently can only attach to the first CyberPro card
 * found.
 */
#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/tty.h>
Linus Torvalds's avatar
Linus Torvalds committed
36
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#include <linux/delay.h>
#include <linux/fb.h>
#include <linux/pci.h>
#include <linux/init.h>

#include <asm/io.h>
#include <asm/irq.h>
#include <asm/pgtable.h>
#include <asm/system.h>
#include <asm/uaccess.h>

#include <video/fbcon.h>
#include <video/fbcon-cfb8.h>
#include <video/fbcon-cfb16.h>
#include <video/fbcon-cfb24.h>

/*
 * Define this if you don't want RGB565, but RGB555 for 16bpp displays.
 */
/*#define CFB16_IS_CFB15*/

#include "cyber2000fb.h"

struct cfb_info {
	struct fb_info		fb;
	struct display_switch	*dispsw;
63
	struct display		*display;
Linus Torvalds's avatar
Linus Torvalds committed
64
	struct pci_dev		*dev;
Linus Torvalds's avatar
Linus Torvalds committed
65 66
	unsigned char 		*region;
	unsigned char		*regs;
Linus Torvalds's avatar
Linus Torvalds committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
	int			func_use_count;
	u_long			ref_ps;

	/*
	 * Clock divisors
	 */
	u_int			divisors[4];

	struct {
		u8 red, green, blue;
	} palette[NR_PALETTE];

	u_char			mem_ctl1;
	u_char			mem_ctl2;
	u_char			mclk_mult;
	u_char			mclk_div;
};

Linus Torvalds's avatar
Linus Torvalds committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
static char default_font_storage[40];
static char *default_font = "Acorn8x8";
MODULE_PARM(default_font, "s");
MODULE_PARM_DESC(default_font, "Default font name");

/*
 * Our access methods.
 */
#define cyber2000fb_writel(val,reg,cfb)	writel(val, (cfb)->regs + (reg))
#define cyber2000fb_writew(val,reg,cfb)	writew(val, (cfb)->regs + (reg))
#define cyber2000fb_writeb(val,reg,cfb)	writeb(val, (cfb)->regs + (reg))

#define cyber2000fb_readb(reg,cfb)	readb((cfb)->regs + (reg))

static inline void
cyber2000_crtcw(unsigned int reg, unsigned int val, struct cfb_info *cfb)
{
	cyber2000fb_writew((reg & 255) | val << 8, 0x3d4, cfb);
}

static inline void
cyber2000_grphw(unsigned int reg, unsigned int val, struct cfb_info *cfb)
{
	cyber2000fb_writew((reg & 255) | val << 8, 0x3ce, cfb);
}

static inline unsigned int
cyber2000_grphr(unsigned int reg, struct cfb_info *cfb)
{
	cyber2000fb_writeb(reg, 0x3ce, cfb);
	return cyber2000fb_readb(0x3cf, cfb);
}

static inline void
cyber2000_attrw(unsigned int reg, unsigned int val, struct cfb_info *cfb)
{
	cyber2000fb_readb(0x3da, cfb);
	cyber2000fb_writeb(reg, 0x3c0, cfb);
	cyber2000fb_readb(0x3c1, cfb);
	cyber2000fb_writeb(val, 0x3c0, cfb);
}

static inline void
cyber2000_seqw(unsigned int reg, unsigned int val, struct cfb_info *cfb)
{
	cyber2000fb_writew((reg & 255) | val << 8, 0x3c4, cfb);
}

Linus Torvalds's avatar
Linus Torvalds committed
133 134 135 136 137
/* -------------------- Hardware specific routines ------------------------- */

/*
 * Hardware Cyber2000 Acceleration
 */
Linus Torvalds's avatar
Linus Torvalds committed
138
static void cyber2000_accel_wait(struct cfb_info *cfb)
Linus Torvalds's avatar
Linus Torvalds committed
139 140 141
{
	int count = 100000;

Linus Torvalds's avatar
Linus Torvalds committed
142
	while (cyber2000fb_readb(CO_REG_CONTROL, cfb) & 0x80) {
Linus Torvalds's avatar
Linus Torvalds committed
143 144
		if (!count--) {
			debug_printf("accel_wait timed out\n");
Linus Torvalds's avatar
Linus Torvalds committed
145
			cyber2000fb_writeb(0, CO_REG_CONTROL, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
146 147 148 149 150 151
			return;
		}
		udelay(1);
	}
}

152
static void cyber2000_accel_setup(struct display *display)
Linus Torvalds's avatar
Linus Torvalds committed
153
{
154
	struct cfb_info *cfb = (struct cfb_info *)display->fb_info;
Linus Torvalds's avatar
Linus Torvalds committed
155

156
	cfb->dispsw->setup(display);
Linus Torvalds's avatar
Linus Torvalds committed
157 158 159
}

static void
160
cyber2000_accel_bmove(struct display *display, int sy, int sx, int dy, int dx,
Linus Torvalds's avatar
Linus Torvalds committed
161 162
		      int height, int width)
{
163 164
	struct cfb_info *cfb = (struct cfb_info *)display->fb_info;
	struct fb_var_screeninfo *var = &display->var;
Linus Torvalds's avatar
Linus Torvalds committed
165 166 167 168
	u_long src, dst;
	u_int fh, fw;
	int cmd = CO_CMD_L_PATTERN_FGCOL;

169
	fw    = fontwidth(display);
Linus Torvalds's avatar
Linus Torvalds committed
170 171 172 173 174 175 176 177 178 179 180
	sx    *= fw;
	dx    *= fw;
	width *= fw;
	width -= 1;

	if (sx < dx) {
		sx += width;
		dx += width;
		cmd |= CO_CMD_L_INC_LEFT;
	}

181
	fh     = fontheight(display);
Linus Torvalds's avatar
Linus Torvalds committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195
	sy     *= fh;
	dy     *= fh;
	height *= fh;
	height -= 1;

	if (sy < dy) {
		sy += height;
		dy += height;
		cmd |= CO_CMD_L_INC_UP;
	}

	src    = sx + sy * var->xres_virtual;
	dst    = dx + dy * var->xres_virtual;

Linus Torvalds's avatar
Linus Torvalds committed
196 197 198 199
	cyber2000_accel_wait(cfb);
	cyber2000fb_writeb(0x00,  CO_REG_CONTROL, cfb);
	cyber2000fb_writeb(0x03,  CO_REG_FORE_MIX, cfb);
	cyber2000fb_writew(width, CO_REG_WIDTH, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
200 201

	if (var->bits_per_pixel != 24) {
Linus Torvalds's avatar
Linus Torvalds committed
202 203
		cyber2000fb_writel(dst, CO_REG_DEST_PTR, cfb);
		cyber2000fb_writel(src, CO_REG_SRC_PTR, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
204
	} else {
Linus Torvalds's avatar
Linus Torvalds committed
205 206 207
		cyber2000fb_writel(dst * 3, CO_REG_DEST_PTR, cfb);
		cyber2000fb_writeb(dst,     CO_REG_X_PHASE, cfb);
		cyber2000fb_writel(src * 3, CO_REG_SRC_PTR, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
208 209
	}

Linus Torvalds's avatar
Linus Torvalds committed
210 211 212
	cyber2000fb_writew(height, CO_REG_HEIGHT, cfb);
	cyber2000fb_writew(cmd,    CO_REG_CMD_L, cfb);
	cyber2000fb_writew(0x2800, CO_REG_CMD_H, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
213 214 215
}

static void
216 217
cyber2000_accel_clear(struct vc_data *conp, struct display *display, int sy,
		      int sx, int height, int width)
Linus Torvalds's avatar
Linus Torvalds committed
218
{
219 220
	struct cfb_info *cfb = (struct cfb_info *)display->fb_info;
	struct fb_var_screeninfo *var = &display->var;
Linus Torvalds's avatar
Linus Torvalds committed
221 222
	u_long dst;
	u_int fw, fh;
223
	u32 bgx = attr_bgcol_ec(display, conp);
Linus Torvalds's avatar
Linus Torvalds committed
224

225 226
	fw = fontwidth(display);
	fh = fontheight(display);
Linus Torvalds's avatar
Linus Torvalds committed
227 228 229 230 231

	dst    = sx * fw + sy * var->xres_virtual * fh;
	width  = width * fw - 1;
	height = height * fh - 1;

Linus Torvalds's avatar
Linus Torvalds committed
232 233 234 235 236
	cyber2000_accel_wait(cfb);
	cyber2000fb_writeb(0x00,   CO_REG_CONTROL,  cfb);
	cyber2000fb_writeb(0x03,   CO_REG_FORE_MIX, cfb);
	cyber2000fb_writew(width,  CO_REG_WIDTH,    cfb);
	cyber2000fb_writew(height, CO_REG_HEIGHT,   cfb);
Linus Torvalds's avatar
Linus Torvalds committed
237 238 239

	switch (var->bits_per_pixel) {
	case 16:
240
		bgx = ((u16 *)display->dispsw_data)[bgx];
Linus Torvalds's avatar
Linus Torvalds committed
241
	case 8:
Linus Torvalds's avatar
Linus Torvalds committed
242
		cyber2000fb_writel(dst, CO_REG_DEST_PTR, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
243 244 245
		break;

	case 24:
Linus Torvalds's avatar
Linus Torvalds committed
246 247
		cyber2000fb_writel(dst * 3, CO_REG_DEST_PTR, cfb);
		cyber2000fb_writeb(dst, CO_REG_X_PHASE, cfb);
248
		bgx = ((u32 *)display->dispsw_data)[bgx];
Linus Torvalds's avatar
Linus Torvalds committed
249 250 251
		break;
	}

Linus Torvalds's avatar
Linus Torvalds committed
252 253 254
	cyber2000fb_writel(bgx, CO_REG_FOREGROUND, cfb);
	cyber2000fb_writew(CO_CMD_L_PATTERN_FGCOL, CO_REG_CMD_L, cfb);
	cyber2000fb_writew(0x0800, CO_REG_CMD_H, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
255 256 257
}

static void
258
cyber2000_accel_putc(struct vc_data *conp, struct display *display, int c,
Linus Torvalds's avatar
Linus Torvalds committed
259 260
		     int yy, int xx)
{
261
	struct cfb_info *cfb = (struct cfb_info *)display->fb_info;
Linus Torvalds's avatar
Linus Torvalds committed
262

Linus Torvalds's avatar
Linus Torvalds committed
263
	cyber2000_accel_wait(cfb);
264
	cfb->dispsw->putc(conp, display, c, yy, xx);
Linus Torvalds's avatar
Linus Torvalds committed
265 266 267
}

static void
268
cyber2000_accel_putcs(struct vc_data *conp, struct display *display,
Linus Torvalds's avatar
Linus Torvalds committed
269 270
		      const unsigned short *s, int count, int yy, int xx)
{
271
	struct cfb_info *cfb = (struct cfb_info *)display->fb_info;
Linus Torvalds's avatar
Linus Torvalds committed
272

Linus Torvalds's avatar
Linus Torvalds committed
273
	cyber2000_accel_wait(cfb);
274
	cfb->dispsw->putcs(conp, display, s, count, yy, xx);
Linus Torvalds's avatar
Linus Torvalds committed
275 276
}

277
static void cyber2000_accel_revc(struct display *display, int xx, int yy)
Linus Torvalds's avatar
Linus Torvalds committed
278
{
279
	struct cfb_info *cfb = (struct cfb_info *)display->fb_info;
Linus Torvalds's avatar
Linus Torvalds committed
280

Linus Torvalds's avatar
Linus Torvalds committed
281
	cyber2000_accel_wait(cfb);
282
	cfb->dispsw->revc(display, xx, yy);
Linus Torvalds's avatar
Linus Torvalds committed
283 284 285
}

static void
286
cyber2000_accel_clear_margins(struct vc_data *conp, struct display *display,
Linus Torvalds's avatar
Linus Torvalds committed
287 288
			      int bottom_only)
{
289
	struct cfb_info *cfb = (struct cfb_info *)display->fb_info;
Linus Torvalds's avatar
Linus Torvalds committed
290

291
	cfb->dispsw->clear_margins(conp, display, bottom_only);
Linus Torvalds's avatar
Linus Torvalds committed
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
}

static struct display_switch fbcon_cyber_accel = {
	setup:		cyber2000_accel_setup,
	bmove:		cyber2000_accel_bmove,
	clear:		cyber2000_accel_clear,
	putc:		cyber2000_accel_putc,
	putcs:		cyber2000_accel_putcs,
	revc:		cyber2000_accel_revc,
	clear_margins:	cyber2000_accel_clear_margins,
	fontwidthmask:	FONTWIDTH(8)|FONTWIDTH(16)
};

/*
 *    Set a single color register. Return != 0 for invalid regno.
 */
static int
309 310
cyber2000fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
		      u_int transp, struct fb_info *info)
Linus Torvalds's avatar
Linus Torvalds committed
311 312
{
	struct cfb_info *cfb = (struct cfb_info *)info;
313
	struct fb_var_screeninfo *var = &cfb->display->var;
Linus Torvalds's avatar
Linus Torvalds committed
314 315 316 317 318 319 320 321 322 323 324 325

	if (regno >= NR_PALETTE)
		return 1;

	red   >>= 8;
	green >>= 8;
	blue  >>= 8;

	cfb->palette[regno].red   = red;
	cfb->palette[regno].green = green;
	cfb->palette[regno].blue  = blue;

326
	switch (var->bits_per_pixel) {
Linus Torvalds's avatar
Linus Torvalds committed
327 328
#ifdef FBCON_HAS_CFB8
	case 8:
Linus Torvalds's avatar
Linus Torvalds committed
329 330 331 332
		cyber2000fb_writeb(regno, 0x3c8, cfb);
		cyber2000fb_writeb(red,   0x3c9, cfb);
		cyber2000fb_writeb(green, 0x3c9, cfb);
		cyber2000fb_writeb(blue,  0x3c9, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
333 334 335 336 337 338
		break;
#endif

#ifdef FBCON_HAS_CFB16
	case 16:
#ifndef CFB16_IS_CFB15
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
		if (var->green.length == 6) {
			if (regno < 64) {
				/* write green */
				cyber2000fb_writeb(regno << 2, 0x3c8, cfb);
				cyber2000fb_writeb(cfb->palette[regno >> 1].red, 0x3c9, cfb);
				cyber2000fb_writeb(green, 0x3c9, cfb);
				cyber2000fb_writeb(cfb->palette[regno >> 1].blue, 0x3c9, cfb);
			}

			if (regno < 32) {
				/* write red,blue */
				cyber2000fb_writeb(regno << 3, 0x3c8, cfb);
				cyber2000fb_writeb(red, 0x3c9, cfb);
				cyber2000fb_writeb(cfb->palette[regno << 1].green, 0x3c9, cfb);
				cyber2000fb_writeb(blue, 0x3c9, cfb);
			}

			if (regno < 16)
				((u16 *)cfb->fb.pseudo_palette)[regno] =
					regno | regno << 5 | regno << 11;
			break;
Linus Torvalds's avatar
Linus Torvalds committed
360 361 362
		}
#endif
		if (regno < 32) {
Linus Torvalds's avatar
Linus Torvalds committed
363 364 365 366
			cyber2000fb_writeb(regno << 3, 0x3c8, cfb);
			cyber2000fb_writeb(red, 0x3c9, cfb);
			cyber2000fb_writeb(green, 0x3c9, cfb);
			cyber2000fb_writeb(blue, 0x3c9, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
367 368 369 370 371 372 373 374 375 376
		}
		if (regno < 16)
			((u16 *)cfb->fb.pseudo_palette)[regno] =
				regno | regno << 5 | regno << 10;
		break;

#endif

#ifdef FBCON_HAS_CFB24
	case 24:
Linus Torvalds's avatar
Linus Torvalds committed
377 378 379 380
		cyber2000fb_writeb(regno, 0x3c8, cfb);
		cyber2000fb_writeb(red,   0x3c9, cfb);
		cyber2000fb_writeb(green, 0x3c9, cfb);
		cyber2000fb_writeb(blue,  0x3c9, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400

		if (regno < 16)
			((u32 *)cfb->fb.pseudo_palette)[regno] =
				regno | regno << 8 | regno << 16;
		break;
#endif

	default:
		return 1;
	}

	return 0;
}

struct par_info {
	/*
	 * Hardware
	 */
	u_char	clock_mult;
	u_char	clock_div;
401
	u_char	extseqmisc;
Linus Torvalds's avatar
Linus Torvalds committed
402 403 404 405 406 407 408 409 410 411 412
	u_char	pixformat;
	u_char	crtc_ofl;
	u_char	crtc[19];
	u_int	width;
	u_int	pitch;
	u_int	fetch;

	/*
	 * Other
	 */
	u_char	palette_ctrl;
Linus Torvalds's avatar
Linus Torvalds committed
413
	u_int	vmode;
Linus Torvalds's avatar
Linus Torvalds committed
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
};

static const u_char crtc_idx[] = {
	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
	0x08, 0x09,
	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18
};

static void cyber2000fb_set_timing(struct cfb_info *cfb, struct par_info *hw)
{
	u_int i;

	/*
	 * Blank palette
	 */
	for (i = 0; i < NR_PALETTE; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
430 431 432 433
		cyber2000fb_writeb(i, 0x3c8, cfb);
		cyber2000fb_writeb(0, 0x3c9, cfb);
		cyber2000fb_writeb(0, 0x3c9, cfb);
		cyber2000fb_writeb(0, 0x3c9, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
434 435
	}

Linus Torvalds's avatar
Linus Torvalds committed
436 437 438
	cyber2000fb_writeb(0xef, 0x3c2, cfb);
	cyber2000_crtcw(0x11, 0x0b, cfb);
	cyber2000_attrw(0x11, 0x00, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
439

Linus Torvalds's avatar
Linus Torvalds committed
440 441 442 443 444 445
	cyber2000_seqw(0x00, 0x01, cfb);
	cyber2000_seqw(0x01, 0x01, cfb);
	cyber2000_seqw(0x02, 0x0f, cfb);
	cyber2000_seqw(0x03, 0x00, cfb);
	cyber2000_seqw(0x04, 0x0e, cfb);
	cyber2000_seqw(0x00, 0x03, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
446 447

	for (i = 0; i < sizeof(crtc_idx); i++)
Linus Torvalds's avatar
Linus Torvalds committed
448
		cyber2000_crtcw(crtc_idx[i], hw->crtc[i], cfb);
Linus Torvalds's avatar
Linus Torvalds committed
449 450

	for (i = 0x0a; i < 0x10; i++)
Linus Torvalds's avatar
Linus Torvalds committed
451 452 453 454 455 456 457 458 459 460 461 462
		cyber2000_crtcw(i, 0, cfb);

	cyber2000_grphw(0x11, hw->crtc_ofl, cfb);
	cyber2000_grphw(0x00, 0x00, cfb);
	cyber2000_grphw(0x01, 0x00, cfb);
	cyber2000_grphw(0x02, 0x00, cfb);
	cyber2000_grphw(0x03, 0x00, cfb);
	cyber2000_grphw(0x04, 0x00, cfb);
	cyber2000_grphw(0x05, 0x60, cfb);
	cyber2000_grphw(0x06, 0x05, cfb);
	cyber2000_grphw(0x07, 0x0f, cfb);
	cyber2000_grphw(0x08, 0xff, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
463 464 465

	/* Attribute controller registers */
	for (i = 0; i < 16; i++)
Linus Torvalds's avatar
Linus Torvalds committed
466
		cyber2000_attrw(i, i, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
467

Linus Torvalds's avatar
Linus Torvalds committed
468 469 470 471 472
	cyber2000_attrw(0x10, 0x01, cfb);
	cyber2000_attrw(0x11, 0x00, cfb);
	cyber2000_attrw(0x12, 0x0f, cfb);
	cyber2000_attrw(0x13, 0x00, cfb);
	cyber2000_attrw(0x14, 0x00, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
473

Linus Torvalds's avatar
Linus Torvalds committed
474 475
	/* woody: set the interlaced bit... */
	/* FIXME: what about doublescan? */
Linus Torvalds's avatar
Linus Torvalds committed
476 477
	cyber2000fb_writeb(0x11, 0x3ce, cfb);
	i = cyber2000fb_readb(0x3cf, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
478 479 480 481
	if (hw->vmode == FB_VMODE_INTERLACED)
		i |= 0x20;
	else
		i &= ~0x20;
Linus Torvalds's avatar
Linus Torvalds committed
482
	cyber2000fb_writeb(i, 0x3cf, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
483

Linus Torvalds's avatar
Linus Torvalds committed
484
	/* PLL registers */
485 486 487 488
	cyber2000_grphw(EXT_DCLK_MULT, hw->clock_mult, cfb);
	cyber2000_grphw(EXT_DCLK_DIV,  hw->clock_div, cfb);
	cyber2000_grphw(EXT_MCLK_MULT, cfb->mclk_mult, cfb);
	cyber2000_grphw(EXT_MCLK_DIV,  cfb->mclk_div, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
489 490 491 492 493 494 495 496 497 498 499 500 501 502
	cyber2000_grphw(0x90, 0x01, cfb);
	cyber2000_grphw(0xb9, 0x80, cfb);
	cyber2000_grphw(0xb9, 0x00, cfb);

	cyber2000fb_writeb(0x56, 0x3ce, cfb);
	i = cyber2000fb_readb(0x3cf, cfb);
	cyber2000fb_writeb(i | 4, 0x3cf, cfb);
	cyber2000fb_writeb(hw->palette_ctrl, 0x3c6, cfb);
	cyber2000fb_writeb(i,    0x3cf, cfb);

	cyber2000fb_writeb(0x20, 0x3c0, cfb);
	cyber2000fb_writeb(0xff, 0x3c6, cfb);

	cyber2000_grphw(0x14, hw->fetch, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
503
	cyber2000_grphw(0x15, ((hw->fetch >> 8) & 0x03) |
Linus Torvalds's avatar
Linus Torvalds committed
504
			      ((hw->pitch >> 4) & 0x30), cfb);
505
	cyber2000_grphw(EXT_SEQ_MISC, hw->extseqmisc, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
506

507 508 509
	cyber2000_grphw(EXT_BIU_MISC, EXT_BIU_MISC_LIN_ENABLE |
				      EXT_BIU_MISC_COP_ENABLE |
				      EXT_BIU_MISC_COP_BFC, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
510 511 512 513

	/*
	 * Set up accelerator registers
	 */
Linus Torvalds's avatar
Linus Torvalds committed
514 515 516
	cyber2000fb_writew(hw->width,     CO_REG_SRC_WIDTH,  cfb);
	cyber2000fb_writew(hw->width,     CO_REG_DEST_WIDTH, cfb);
	cyber2000fb_writeb(hw->pixformat, CO_REG_PIX_FORMAT, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
517 518 519 520 521 522 523
}

static inline int
cyber2000fb_update_start(struct cfb_info *cfb, struct fb_var_screeninfo *var)
{
	u_int base;

524 525
	base = var->yoffset * var->xres_virtual * var->bits_per_pixel +
		var->xoffset * var->bits_per_pixel;
Linus Torvalds's avatar
Linus Torvalds committed
526

527 528 529 530 531
	/*
	 * Convert to bytes and shift two extra bits because DAC
	 * can only start on 4 byte aligned data.
	 */
	base >>= 5;
Linus Torvalds's avatar
Linus Torvalds committed
532 533 534 535

	if (base >= 1 << 20)
		return -EINVAL;

Linus Torvalds's avatar
Linus Torvalds committed
536 537 538
	cyber2000_grphw(0x10, base >> 16 | 0x10, cfb);
	cyber2000_crtcw(0x0c, base >> 8, cfb);
	cyber2000_crtcw(0x0d, base, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 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 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645

	return 0;
}

static int
cyber2000fb_decode_crtc(struct par_info *hw, struct cfb_info *cfb,
			struct fb_var_screeninfo *var)
{
	u_int Htotal, Hblankend, Hsyncend;
	u_int Vtotal, Vdispend, Vblankstart, Vblankend, Vsyncstart, Vsyncend;
#define BIT(v,b1,m,b2) (((v >> b1) & m) << b2)

	hw->crtc[13] = hw->pitch;
	hw->crtc[17] = 0xe3;
	hw->crtc[14] = 0;
	hw->crtc[8]  = 0;

	Htotal      = var->xres + var->right_margin +
		      var->hsync_len + var->left_margin;

	if (Htotal > 2080)
		return -EINVAL;

	hw->crtc[0] = (Htotal >> 3) - 5;
	hw->crtc[1] = (var->xres >> 3) - 1;
	hw->crtc[2] = var->xres >> 3;
	hw->crtc[4] = (var->xres + var->right_margin) >> 3;

	Hblankend   = (Htotal - 4*8) >> 3;

	hw->crtc[3] = BIT(Hblankend,  0, 0x1f,  0) |
		      BIT(1,          0, 0x01,  7);

	Hsyncend    = (var->xres + var->right_margin + var->hsync_len) >> 3;

	hw->crtc[5] = BIT(Hsyncend,   0, 0x1f,  0) |
		      BIT(Hblankend,  5, 0x01,  7);

	Vdispend    = var->yres - 1;
	Vsyncstart  = var->yres + var->lower_margin;
	Vsyncend    = var->yres + var->lower_margin + var->vsync_len;
	Vtotal      = var->yres + var->lower_margin + var->vsync_len +
		      var->upper_margin - 2;

	if (Vtotal > 2047)
		return -EINVAL;

	Vblankstart = var->yres + 6;
	Vblankend   = Vtotal - 10;

	hw->crtc[6]  = Vtotal;
	hw->crtc[7]  = BIT(Vtotal,     8, 0x01,  0) |
			BIT(Vdispend,   8, 0x01,  1) |
			BIT(Vsyncstart, 8, 0x01,  2) |
			BIT(Vblankstart,8, 0x01,  3) |
			BIT(1,          0, 0x01,  4) |
	        	BIT(Vtotal,     9, 0x01,  5) |
			BIT(Vdispend,   9, 0x01,  6) |
			BIT(Vsyncstart, 9, 0x01,  7);
	hw->crtc[9]  = BIT(0,          0, 0x1f,  0) |
		        BIT(Vblankstart,9, 0x01,  5) |
			BIT(1,          0, 0x01,  6);
	hw->crtc[10] = Vsyncstart;
	hw->crtc[11] = BIT(Vsyncend,   0, 0x0f,  0) |
		       BIT(1,          0, 0x01,  7);
	hw->crtc[12] = Vdispend;
	hw->crtc[15] = Vblankstart;
	hw->crtc[16] = Vblankend;
	hw->crtc[18] = 0xff;

	/* overflow - graphics reg 0x11 */
	/* 0=VTOTAL:10 1=VDEND:10 2=VRSTART:10 3=VBSTART:10
	 * 4=LINECOMP:10 5-IVIDEO 6=FIXCNT
	 */
	hw->crtc_ofl =
		BIT(Vtotal,     10, 0x01,  0) |
		BIT(Vdispend,   10, 0x01,  1) |
		BIT(Vsyncstart, 10, 0x01,  2) |
		BIT(Vblankstart,10, 0x01,  3) |
		1 << 4;

	return 0;
}

/*
 * The following was discovered by a good monitor, bit twiddling, theorising
 * and but mostly luck.  Strangely, it looks like everyone elses' PLL!
 *
 * Clock registers:
 *   fclock = fpll / div2
 *   fpll   = fref * mult / div1
 * where:
 *   fref = 14.318MHz (69842ps)
 *   mult = reg0xb0.7:0
 *   div1 = (reg0xb1.5:0 + 1)
 *   div2 =  2^(reg0xb1.7:6)
 *   fpll should be between 115 and 260 MHz
 *  (8696ps and 3846ps)
 */
static int
cyber2000fb_decode_clock(struct par_info *hw, struct cfb_info *cfb,
			 struct fb_var_screeninfo *var)
{
	u_long pll_ps = var->pixclock;
	const u_long ref_ps = cfb->ref_ps;
	u_int div2, t_div1, best_div1, best_mult;
	int best_diff;
Linus Torvalds's avatar
Linus Torvalds committed
646
	int vco;
Linus Torvalds's avatar
Linus Torvalds committed
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720

	/*
	 * Step 1:
	 *   find div2 such that 115MHz < fpll < 260MHz
	 *   and 0 <= div2 < 4
	 */
	for (div2 = 0; div2 < 4; div2++) {
		u_long new_pll;

		new_pll = pll_ps / cfb->divisors[div2];
		if (8696 > new_pll && new_pll > 3846) {
			pll_ps = new_pll;
			break;
		}
	}

	if (div2 == 4)
		return -EINVAL;

	/*
	 * Step 2:
	 *  Given pll_ps and ref_ps, find:
	 *    pll_ps * 0.995 < pll_ps_calc < pll_ps * 1.005
	 *  where { 1 < best_div1 < 32, 1 < best_mult < 256 }
	 *    pll_ps_calc = best_div1 / (ref_ps * best_mult)
	 */
	best_diff = 0x7fffffff;
	best_mult = 32;
	best_div1 = 255;
	for (t_div1 = 32; t_div1 > 1; t_div1 -= 1) {
		u_int rr, t_mult, t_pll_ps;
		int diff;

		/*
		 * Find the multiplier for this divisor
		 */
		rr = ref_ps * t_div1;
		t_mult = (rr + pll_ps / 2) / pll_ps;

		/*
		 * Is the multiplier within the correct range?
		 */
		if (t_mult > 256 || t_mult < 2)
			continue;

		/*
		 * Calculate the actual clock period from this multiplier
		 * and divisor, and estimate the error.
		 */
		t_pll_ps = (rr + t_mult / 2) / t_mult;
		diff = pll_ps - t_pll_ps;
		if (diff < 0)
			diff = -diff;

		if (diff < best_diff) {
			best_diff = diff;
			best_mult = t_mult;
			best_div1 = t_div1;
		}

		/*
		 * If we hit an exact value, there is no point in continuing.
		 */
		if (diff == 0)
			break;
	}

	/*
	 * Step 3:
	 *  combine values
	 */
	hw->clock_mult = best_mult - 1;
	hw->clock_div  = div2 << 6 | (best_div1 - 1);

Linus Torvalds's avatar
Linus Torvalds committed
721 722 723
	vco = ref_ps * best_div1 / best_mult;
	if ((ref_ps == 40690) && (vco < 5556))
		/* Set VFSEL when VCO > 180MHz (5.556 ps). */
724
		hw->clock_div |= EXT_DCLK_DIV_VFSEL;
Linus Torvalds's avatar
Linus Torvalds committed
725

Linus Torvalds's avatar
Linus Torvalds committed
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
	return 0;
}

/*
 * Decode the info required for the hardware.
 * This involves the PLL parameters for the dot clock,
 * CRTC registers, and accelerator settings.
 */
static int
cyber2000fb_decode_var(struct fb_var_screeninfo *var, struct cfb_info *cfb,
		       struct par_info *hw)
{
	int err;

	hw->width = var->xres_virtual;
	hw->palette_ctrl = 0x06;
Linus Torvalds's avatar
Linus Torvalds committed
742
	hw->vmode = var->vmode;
Linus Torvalds's avatar
Linus Torvalds committed
743 744 745 746 747

	switch (var->bits_per_pixel) {
#ifdef FBCON_HAS_CFB8
	case 8:	/* PSEUDOCOLOUR, 256 */
		hw->pixformat		= PIXFORMAT_8BPP;
748
		hw->extseqmisc		= EXT_SEQ_MISC_8;
Linus Torvalds's avatar
Linus Torvalds committed
749 750 751 752
		hw->pitch		= hw->width >> 3;
		break;
#endif
#ifdef FBCON_HAS_CFB16
753
	case 16:
Linus Torvalds's avatar
Linus Torvalds committed
754 755 756
		hw->pixformat		= PIXFORMAT_16BPP;
		hw->pitch		= hw->width >> 2;
		hw->palette_ctrl	|= 0x10;
757 758 759 760 761 762 763

#ifndef CFB16_IS_CFB15
		/* DIRECTCOLOUR, 64k */
		if (var->green.length == 6) {
			hw->extseqmisc		= EXT_SEQ_MISC_16_RGB565;
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
764
#endif
765 766
		/* DIRECTCOLOUR, 32k */
		hw->extseqmisc		= EXT_SEQ_MISC_16_RGB555;
Linus Torvalds's avatar
Linus Torvalds committed
767 768 769 770 771 772
		break;

#endif
#ifdef FBCON_HAS_CFB24
	case 24:/* TRUECOLOUR, 16m */
		hw->pixformat		= PIXFORMAT_24BPP;
773
		hw->extseqmisc		= EXT_SEQ_MISC_24_RGB888;
Linus Torvalds's avatar
Linus Torvalds committed
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
		hw->width		*= 3;
		hw->pitch		= hw->width >> 3;
		hw->palette_ctrl	|= 0x10;
		break;
#endif
	default:
		return -EINVAL;
	}

	err = cyber2000fb_decode_clock(hw, cfb, var);
	if (err)
		return err;

	err = cyber2000fb_decode_crtc(hw, cfb, var);
	if (err)
		return err;

	hw->width -= 1;
	hw->fetch = hw->pitch;
	if (!(cfb->mem_ctl2 & MEM_CTL2_64BIT))
		hw->fetch <<= 1;
	hw->fetch += 1;

	return 0;
}

/*
 *    Set the User Defined Part of the Display
 */
static int
cyber2000fb_set_var(struct fb_var_screeninfo *var, int con,
		    struct fb_info *info)
{
	struct cfb_info *cfb = (struct cfb_info *)info;
	struct display *display;
	struct par_info hw;
	int err, chgvar = 0;

	/*
	 * CONUPDATE and SMOOTH_XPAN are equal.  However,
	 * SMOOTH_XPAN is only used internally by fbcon.
	 */
	if (var->vmode & FB_VMODE_CONUPDATE) {
		var->vmode |= FB_VMODE_YWRAP;
818 819
		var->xoffset = cfb->display->var.xoffset;
		var->yoffset = cfb->display->var.yoffset;
Linus Torvalds's avatar
Linus Torvalds committed
820 821 822 823 824 825 826 827 828 829 830 831
	}

	err = cyber2000fb_decode_var(var, (struct cfb_info *)info, &hw);
	if (err)
		return err;

	if (var->activate & FB_ACTIVATE_TEST)
		return 0;

	if ((var->activate & FB_ACTIVATE_MASK) != FB_ACTIVATE_NOW)
		return -EINVAL;

832 833 834 835 836 837 838 839
	if (con < 0) {
		display = cfb->fb.disp;
		chgvar = 0;
	} else {
		display = fb_display + con;
	}

	if (display->var.xres != var->xres)
Linus Torvalds's avatar
Linus Torvalds committed
840
		chgvar = 1;
841
	if (display->var.yres != var->yres)
Linus Torvalds's avatar
Linus Torvalds committed
842
		chgvar = 1;
843
	if (display->var.xres_virtual != var->xres_virtual)
Linus Torvalds's avatar
Linus Torvalds committed
844
		chgvar = 1;
845
	if (display->var.yres_virtual != var->yres_virtual)
Linus Torvalds's avatar
Linus Torvalds committed
846
		chgvar = 1;
847
	if (display->var.bits_per_pixel != var->bits_per_pixel)
Linus Torvalds's avatar
Linus Torvalds committed
848 849
		chgvar = 1;

850
	if (con < 0)
Linus Torvalds's avatar
Linus Torvalds committed
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
		chgvar = 0;

	var->red.msb_right	= 0;
	var->green.msb_right	= 0;
	var->blue.msb_right	= 0;

	switch (var->bits_per_pixel) {
#ifdef FBCON_HAS_CFB8
	case 8:	/* PSEUDOCOLOUR, 256 */
		var->red.offset		= 0;
		var->red.length		= 8;
		var->green.offset	= 0;
		var->green.length	= 8;
		var->blue.offset	= 0;
		var->blue.length	= 8;

		cfb->fb.fix.visual	= FB_VISUAL_PSEUDOCOLOR;
		cfb->dispsw		= &fbcon_cfb8;
		display->dispsw_data	= NULL;
		display->next_line	= var->xres_virtual;
		break;
#endif
#ifdef FBCON_HAS_CFB16
874 875
	case 16:
		var->bits_per_pixel	= 16;
Linus Torvalds's avatar
Linus Torvalds committed
876 877 878 879 880 881 882 883 884
		var->red.length		= 5;
		var->green.offset	= 5;
		var->blue.offset	= 0;
		var->blue.length	= 5;

		cfb->fb.fix.visual	= FB_VISUAL_DIRECTCOLOR;
		cfb->dispsw		= &fbcon_cfb16;
		display->dispsw_data	= cfb->fb.pseudo_palette;
		display->next_line	= var->xres_virtual * 2;
885 886 887 888 889 890 891 892

#ifndef CFB16_IS_CFB15
		/* DIRECTCOLOUR, 64k */
		if (var->green.length == 6) {
			var->red.offset		= 11;
			var->green.length	= 6;
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
893
#endif
894
		/* DIRECTCOLOUR, 32k */
Linus Torvalds's avatar
Linus Torvalds committed
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
		var->red.offset		= 10;
		var->green.length	= 5;
		break;
#endif
#ifdef FBCON_HAS_CFB24
	case 24:/* TRUECOLOUR, 16m */
		var->red.offset		= 16;
		var->red.length		= 8;
		var->green.offset	= 8;
		var->green.length	= 8;
		var->blue.offset	= 0;
		var->blue.length	= 8;

		cfb->fb.fix.visual	= FB_VISUAL_TRUECOLOR;
		cfb->dispsw		= &fbcon_cfb24;
		display->dispsw_data	= cfb->fb.pseudo_palette;
		display->next_line	= var->xres_virtual * 3;
		break;
#endif
	default:/* in theory this should never happen */
		printk(KERN_WARNING "%s: no support for %dbpp\n",
		       cfb->fb.fix.id, var->bits_per_pixel);
		cfb->dispsw = &fbcon_dummy;
		break;
	}

	if (var->accel_flags & FB_ACCELF_TEXT && cfb->dispsw != &fbcon_dummy)
		display->dispsw = &fbcon_cyber_accel;
	else
		display->dispsw = cfb->dispsw;

	cfb->fb.fix.line_length	= display->next_line;

	display->line_length	= cfb->fb.fix.line_length;
	display->visual		= cfb->fb.fix.visual;
	display->type		= cfb->fb.fix.type;
	display->type_aux	= cfb->fb.fix.type_aux;
	display->ypanstep	= cfb->fb.fix.ypanstep;
	display->ywrapstep	= cfb->fb.fix.ywrapstep;
	display->can_soft_blank = 1;
	display->inverse	= 0;
936 937
	display->var		= *var;
	display->var.activate	&= ~FB_ACTIVATE_ALL;
Linus Torvalds's avatar
Linus Torvalds committed
938

939
	cfb->fb.var = display->var;
Linus Torvalds's avatar
Linus Torvalds committed
940 941 942 943 944 945

	/*
	 * If we are setting all the virtual consoles, also set the
	 * defaults used to create new consoles.
	 */
	if (var->activate & FB_ACTIVATE_ALL)
946
		cfb->fb.disp->var = display->var;
Linus Torvalds's avatar
Linus Torvalds committed
947 948 949 950 951 952

	if (chgvar && info && cfb->fb.changevar)
		cfb->fb.changevar(con);

	cyber2000fb_update_start(cfb, var);
	cyber2000fb_set_timing(cfb, &hw);
953
	fb_set_cmap(&cfb->fb.cmap, 1, &cfb->fb);
Linus Torvalds's avatar
Linus Torvalds committed
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975

	return 0;
}


/*
 *    Pan or Wrap the Display
 */
static int
cyber2000fb_pan_display(struct fb_var_screeninfo *var, int con,
			struct fb_info *info)
{
	struct cfb_info *cfb = (struct cfb_info *)info;
	u_int y_bottom;

	y_bottom = var->yoffset;

	if (!(var->vmode & FB_VMODE_YWRAP))
		y_bottom += var->yres;

	if (var->xoffset > (var->xres_virtual - var->xres))
		return -EINVAL;
976
	if (y_bottom > cfb->display->var.yres_virtual)
Linus Torvalds's avatar
Linus Torvalds committed
977 978 979 980 981
		return -EINVAL;

	if (cyber2000fb_update_start(cfb, var))
		return -EINVAL;

982 983
	cfb->display->var.xoffset = var->xoffset;
	cfb->display->var.yoffset = var->yoffset;
Linus Torvalds's avatar
Linus Torvalds committed
984
	if (var->vmode & FB_VMODE_YWRAP) {
985
		cfb->display->var.vmode |= FB_VMODE_YWRAP;
Linus Torvalds's avatar
Linus Torvalds committed
986
	} else {
987
		cfb->display->var.vmode &= ~FB_VMODE_YWRAP;
Linus Torvalds's avatar
Linus Torvalds committed
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
	}

	return 0;
}


/*
 *    Update the `var' structure (called by fbcon.c)
 *
 *    This call looks only at yoffset and the FB_VMODE_YWRAP flag in `var'.
 *    Since it's called by a kernel driver, no range checking is done.
 */
static int cyber2000fb_updatevar(int con, struct fb_info *info)
{
	struct cfb_info *cfb = (struct cfb_info *)info;

	return cyber2000fb_update_start(cfb, &fb_display[con].var);
}

static int cyber2000fb_switch(int con, struct fb_info *info)
{
	struct cfb_info *cfb = (struct cfb_info *)info;
1010
	struct display *display = cfb->display;
Linus Torvalds's avatar
Linus Torvalds committed
1011 1012
	struct fb_cmap *cmap;

1013
	if (display) {
Linus Torvalds's avatar
Linus Torvalds committed
1014 1015 1016
		/*
		 * Save the old colormap and video mode.
		 */
1017 1018
		if (display->cmap.len)
			fb_copy_cmap(&cfb->fb.cmap, &display->cmap, 0);
Linus Torvalds's avatar
Linus Torvalds committed
1019 1020
	}

1021
	cfb->display = display = fb_display + con;
Linus Torvalds's avatar
Linus Torvalds committed
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031

	/*
	 * Install the new colormap and change the video mode.  By default,
	 * fbcon sets all the colormaps and video modes to the default
	 * values at bootup.
	 *
	 * Really, we want to set the colourmap size depending on the
	 * depth of the new video mode.  For now, we leave it at its
	 * default 256 entry.
	 */
1032 1033
	if (display->cmap.len)
		cmap = &display->cmap;
Linus Torvalds's avatar
Linus Torvalds committed
1034
	else
1035
		cmap = fb_default_cmap(1 << display->var.bits_per_pixel);
Linus Torvalds's avatar
Linus Torvalds committed
1036 1037 1038

	fb_copy_cmap(cmap, &cfb->fb.cmap, 0);

1039 1040
	display->var.activate = FB_ACTIVATE_NOW;
	cyber2000fb_set_var(&display->var, con, &cfb->fb);
Linus Torvalds's avatar
Linus Torvalds committed
1041 1042 1043 1044 1045 1046 1047

	return 0;
}

/*
 *    (Un)Blank the display.
 */
1048
static int cyber2000fb_blank(int blank, struct fb_info *info)
Linus Torvalds's avatar
Linus Torvalds committed
1049 1050
{
	struct cfb_info *cfb = (struct cfb_info *)info;
1051
	unsigned int sync = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
	int i;

	/*
	 *  Blank the screen if blank_mode != 0, else unblank. If
	 *  blank == NULL then the caller blanks by setting the CLUT
	 *  (Color Look Up Table) to all black. 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
	 *
	 *  wms...Enable VESA DMPS compatible powerdown mode
	 *  run "setterm -powersave powerdown" to take advantage
	 */
     
	switch (blank) {
	case 4:	/* powerdown - both sync lines down */
1072
		sync = EXT_SYNC_CTL_VS_0 | EXT_SYNC_CTL_HS_0;
Linus Torvalds's avatar
Linus Torvalds committed
1073 1074
		break;	
	case 3:	/* hsync off */
1075
		sync = EXT_SYNC_CTL_VS_NORMAL | EXT_SYNC_CTL_HS_0;
Linus Torvalds's avatar
Linus Torvalds committed
1076 1077
		break;	
	case 2:	/* vsync off */
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
		sync = EXT_SYNC_CTL_VS_0 | EXT_SYNC_CTL_HS_NORMAL;
		break;
	case 1:	/* soft blank */
		break;
	default: /* unblank */
		break;
	}
	cyber2000_grphw(EXT_SYNC_CTL, sync, cfb);

	switch (blank) {
	case 4:
	case 3:
	case 2:
Linus Torvalds's avatar
Linus Torvalds committed
1091 1092
	case 1:	/* soft blank */
		for (i = 0; i < NR_PALETTE; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
1093 1094 1095 1096
			cyber2000fb_writeb(i, 0x3c8, cfb);
			cyber2000fb_writeb(0, 0x3c9, cfb);
			cyber2000fb_writeb(0, 0x3c9, cfb);
			cyber2000fb_writeb(0, 0x3c9, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
1097 1098
		}
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1099 1100
	default: /* unblank */
		for (i = 0; i < NR_PALETTE; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
1101 1102 1103 1104
			cyber2000fb_writeb(i, 0x3c8, cfb);
			cyber2000fb_writeb(cfb->palette[i].red, 0x3c9, cfb);
			cyber2000fb_writeb(cfb->palette[i].green, 0x3c9, cfb);
			cyber2000fb_writeb(cfb->palette[i].blue, 0x3c9, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
1105 1106 1107
		}
		break;
	}
1108
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1109 1110 1111 1112 1113
}

static struct fb_ops cyber2000fb_ops = {
	owner:		THIS_MODULE,
	fb_set_var:	cyber2000fb_set_var,
1114
	fb_setcolreg:	cyber2000fb_setcolreg,
Linus Torvalds's avatar
Linus Torvalds committed
1115
	fb_pan_display:	cyber2000fb_pan_display,
1116
	fb_blank:	cyber2000fb_blank,
Linus Torvalds's avatar
Linus Torvalds committed
1117 1118 1119
	fb_get_fix:	gen_get_fix,
	fb_get_var:	gen_get_var,
	fb_get_cmap:	gen_get_cmap,
James Simmons's avatar
James Simmons committed
1120
	fb_set_cmap:	gen_set_cmap,
Linus Torvalds's avatar
Linus Torvalds committed
1121 1122
};

1123 1124 1125 1126 1127 1128 1129
/*
 * This is the only "static" reference to the internal data structures
 * of this driver.  It is here solely at the moment to support the other
 * CyberPro modules external to this driver.
 */
static struct cfb_info		*int_cfb_info;

Linus Torvalds's avatar
Linus Torvalds committed
1130 1131 1132
/*
 * Enable access to the extended registers
 */
1133
void cyber2000fb_enable_extregs(struct cfb_info *cfb)
Linus Torvalds's avatar
Linus Torvalds committed
1134 1135 1136 1137 1138 1139
{
	cfb->func_use_count += 1;

	if (cfb->func_use_count == 1) {
		int old;

1140 1141 1142
		old = cyber2000_grphr(EXT_FUNC_CTL, cfb);
		old |= EXT_FUNC_CTL_EXTREGENBL;
		cyber2000_grphw(EXT_FUNC_CTL, old, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
1143 1144 1145 1146 1147 1148
	}
}

/*
 * Disable access to the extended registers
 */
1149
void cyber2000fb_disable_extregs(struct cfb_info *cfb)
Linus Torvalds's avatar
Linus Torvalds committed
1150 1151 1152 1153
{
	if (cfb->func_use_count == 1) {
		int old;

1154 1155 1156
		old = cyber2000_grphr(EXT_FUNC_CTL, cfb);
		old &= ~EXT_FUNC_CTL_EXTREGENBL;
		cyber2000_grphw(EXT_FUNC_CTL, old, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
1157 1158
	}

1159 1160 1161 1162
	if (cfb->func_use_count == 0)
		printk(KERN_ERR "disable_extregs: count = 0\n");
	else
		cfb->func_use_count -= 1;
Linus Torvalds's avatar
Linus Torvalds committed
1163 1164
}

1165 1166 1167 1168
void cyber2000fb_get_fb_var(struct cfb_info *cfb, struct fb_var_screeninfo *var)
{
	memcpy(var, &cfb->display->var, sizeof(struct fb_var_screeninfo));
}
Linus Torvalds's avatar
Linus Torvalds committed
1169 1170 1171 1172 1173 1174 1175 1176

/*
 * Attach a capture/tv driver to the core CyberX0X0 driver.
 */
int cyber2000fb_attach(struct cyberpro_info *info, int idx)
{
	if (int_cfb_info != NULL) {
		info->dev	      = int_cfb_info->dev;
Linus Torvalds's avatar
Linus Torvalds committed
1177
		info->regs	      = int_cfb_info->regs;
Linus Torvalds's avatar
Linus Torvalds committed
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
		info->fb	      = int_cfb_info->fb.screen_base;
		info->fb_size	      = int_cfb_info->fb.fix.smem_len;
		info->enable_extregs  = cyber2000fb_enable_extregs;
		info->disable_extregs = cyber2000fb_disable_extregs;
		info->info            = int_cfb_info;

		strncpy(info->dev_name, int_cfb_info->fb.fix.id, sizeof(info->dev_name));

		MOD_INC_USE_COUNT;
	}

	return int_cfb_info != NULL;
}

/*
 * Detach a capture/tv driver from the core CyberX0X0 driver.
 */
void cyber2000fb_detach(int idx)
{
	MOD_DEC_USE_COUNT;
}

EXPORT_SYMBOL(cyber2000fb_attach);
EXPORT_SYMBOL(cyber2000fb_detach);
1202 1203 1204
EXPORT_SYMBOL(cyber2000fb_enable_extregs);
EXPORT_SYMBOL(cyber2000fb_disable_extregs);
EXPORT_SYMBOL(cyber2000fb_get_fb_var);
Linus Torvalds's avatar
Linus Torvalds committed
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225

/*
 * These parameters give
 * 640x480, hsync 31.5kHz, vsync 60Hz
 */
static struct fb_videomode __devinitdata cyber2000fb_default_mode = {
	refresh:	60,
	xres:		640,
	yres:		480,
	pixclock:	39722,
	left_margin:	56,
	right_margin:	16,
	upper_margin:	34,
	lower_margin:	9,
	hsync_len:	88,
	vsync_len:	2,
	sync:		FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
	vmode:		FB_VMODE_NONINTERLACED
};

static char igs_regs[] __devinitdata = {
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
	EXT_CRT_IRQ,		0,
	EXT_CRT_TEST,		0,
	EXT_SYNC_CTL,		0,
	EXT_SEG_WRITE_PTR,	0,
	EXT_SEG_READ_PTR,	0,
	EXT_BIU_MISC,		EXT_BIU_MISC_LIN_ENABLE |
				EXT_BIU_MISC_COP_ENABLE |
				EXT_BIU_MISC_COP_BFC,
	EXT_FUNC_CTL,		0,
	CURS_H_START,		0,
	CURS_H_START + 1,	0,
	CURS_H_PRESET,		0,
	CURS_V_START,		0,
	CURS_V_START + 1,	0,
	CURS_V_PRESET,		0,
	CURS_CTL,		0,
	EXT_ATTRIB_CTL,		EXT_ATTRIB_CTL_EXT,
	EXT_OVERSCAN_RED,	0,
	EXT_OVERSCAN_GREEN,	0,
	EXT_OVERSCAN_BLUE,	0,

	/* some of these are questionable when we have a BIOS */
	EXT_MEM_CTL0,		EXT_MEM_CTL0_7CLK |
				EXT_MEM_CTL0_RAS_1 |
				EXT_MEM_CTL0_MULTCAS,
	EXT_HIDDEN_CTL1,	0x30,
	EXT_FIFO_CTL,		0x0b,
	EXT_FIFO_CTL + 1,	0x17,
	0x76,			0x00,
	EXT_HIDDEN_CTL4,	0xc8
Linus Torvalds's avatar
Linus Torvalds committed
1256 1257 1258
};

/*
1259 1260 1261
 * Initialise the CyberPro hardware.  On the CyberPro5XXXX,
 * ensure that we're using the correct PLL (5XXX's may be
 * programmed to use an additional set of PLLs.)
Linus Torvalds's avatar
Linus Torvalds committed
1262
 */
1263
static void cyberpro_init_hw(struct cfb_info *cfb)
Linus Torvalds's avatar
Linus Torvalds committed
1264 1265 1266 1267
{
	int i;

	for (i = 0; i < sizeof(igs_regs); i += 2)
Linus Torvalds's avatar
Linus Torvalds committed
1268
		cyber2000_grphw(igs_regs[i], igs_regs[i+1], cfb);
Linus Torvalds's avatar
Linus Torvalds committed
1269

1270 1271 1272 1273 1274
	if (cfb->fb.fix.accel == FB_ACCEL_IGS_CYBER5000) {
		unsigned char val;
		cyber2000fb_writeb(0xba, 0x3ce, cfb);
		val = cyber2000fb_readb(0x3cf, cfb) & 0x80;
		cyber2000fb_writeb(val, 0x3cf, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
1275 1276 1277 1278
	}
}

static struct cfb_info * __devinit
1279
cyberpro_alloc_fb_info(unsigned int id, char *name)
Linus Torvalds's avatar
Linus Torvalds committed
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
{
	struct cfb_info *cfb;

	cfb = kmalloc(sizeof(struct cfb_info) + sizeof(struct display) +
		       sizeof(u32) * 16, GFP_KERNEL);

	if (!cfb)
		return NULL;

	memset(cfb, 0, sizeof(struct cfb_info) + sizeof(struct display));

1291
	if (id == ID_CYBERPRO_5000)
Linus Torvalds's avatar
Linus Torvalds committed
1292 1293 1294 1295
		cfb->ref_ps	= 40690; // 24.576 MHz
	else
		cfb->ref_ps	= 69842; // 14.31818 MHz (69841?)

Linus Torvalds's avatar
Linus Torvalds committed
1296 1297 1298 1299
	cfb->divisors[0]	= 1;
	cfb->divisors[1]	= 2;
	cfb->divisors[2]	= 4;

1300
	if (id == ID_CYBERPRO_2000)
Linus Torvalds's avatar
Linus Torvalds committed
1301
		cfb->divisors[3] = 8;
Linus Torvalds's avatar
Linus Torvalds committed
1302 1303
	else
		cfb->divisors[3] = 6;
Linus Torvalds's avatar
Linus Torvalds committed
1304

Linus Torvalds's avatar
Linus Torvalds committed
1305
	strcpy(cfb->fb.fix.id, name);
Linus Torvalds's avatar
Linus Torvalds committed
1306 1307 1308 1309 1310 1311

	cfb->fb.fix.type	= FB_TYPE_PACKED_PIXELS;
	cfb->fb.fix.type_aux	= 0;
	cfb->fb.fix.xpanstep	= 0;
	cfb->fb.fix.ypanstep	= 1;
	cfb->fb.fix.ywrapstep	= 0;
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329

	switch (id) {
	case ID_IGA_1682:
		cfb->fb.fix.accel = 0;
		break;

	case ID_CYBERPRO_2000:
		cfb->fb.fix.accel = FB_ACCEL_IGS_CYBER2000;
		break;

	case ID_CYBERPRO_2010:
		cfb->fb.fix.accel = FB_ACCEL_IGS_CYBER2010;
		break;

	case ID_CYBERPRO_5000:
		cfb->fb.fix.accel = FB_ACCEL_IGS_CYBER5000;
		break;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1330 1331 1332 1333 1334 1335 1336 1337

	cfb->fb.var.nonstd	= 0;
	cfb->fb.var.activate	= FB_ACTIVATE_NOW;
	cfb->fb.var.height	= -1;
	cfb->fb.var.width	= -1;
	cfb->fb.var.accel_flags	= FB_ACCELF_TEXT;

	strcpy(cfb->fb.modename, cfb->fb.fix.id);
Linus Torvalds's avatar
Linus Torvalds committed
1338
	strcpy(cfb->fb.fontname, default_font);
Linus Torvalds's avatar
Linus Torvalds committed
1339 1340 1341 1342 1343 1344

	cfb->fb.fbops		= &cyber2000fb_ops;
	cfb->fb.changevar	= NULL;
	cfb->fb.switch_con	= cyber2000fb_switch;
	cfb->fb.updatevar	= cyber2000fb_updatevar;
	cfb->fb.flags		= FBINFO_FLAG_DEFAULT;
1345
	cfb->fb.node		= NODEV;
Linus Torvalds's avatar
Linus Torvalds committed
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
	cfb->fb.disp		= (struct display *)(cfb + 1);
	cfb->fb.pseudo_palette	= (void *)(cfb->fb.disp + 1);

	fb_alloc_cmap(&cfb->fb.cmap, NR_PALETTE, 0);

	return cfb;
}

static void __devinit
cyberpro_free_fb_info(struct cfb_info *cfb)
{
	if (cfb) {
		/*
		 * Free the colourmap
		 */
		fb_alloc_cmap(&cfb->fb.cmap, 0, 0);

		kfree(cfb);
	}
}

/*
Linus Torvalds's avatar
Linus Torvalds committed
1368 1369
 * Parse Cyber2000fb options.  Usage:
 *  video=cyber2000:font:fontname
Linus Torvalds's avatar
Linus Torvalds committed
1370
 */
Linus Torvalds's avatar
Linus Torvalds committed
1371 1372
int
cyber2000fb_setup(char *options)
Linus Torvalds's avatar
Linus Torvalds committed
1373
{
Linus Torvalds's avatar
Linus Torvalds committed
1374
	char *opt;
Linus Torvalds's avatar
Linus Torvalds committed
1375

Linus Torvalds's avatar
Linus Torvalds committed
1376 1377
	if (!options || !*options)
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1378

Linus Torvalds's avatar
Linus Torvalds committed
1379 1380 1381
	while ((opt = strsep(&options, ",")) != NULL) {
		if (!*opt)
			continue;
Linus Torvalds's avatar
Linus Torvalds committed
1382

Linus Torvalds's avatar
Linus Torvalds committed
1383 1384 1385 1386 1387
		if (strncmp(opt, "font:", 5) == 0) {
			strncpy(default_font_storage, opt + 5, sizeof(default_font_storage));
			default_font = default_font_storage;
			continue;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1388

Linus Torvalds's avatar
Linus Torvalds committed
1389
		printk(KERN_ERR "CyberPro20x0: unknown parameter: %s\n", opt);
Linus Torvalds's avatar
Linus Torvalds committed
1390 1391 1392 1393
	}
	return 0;
}

1394 1395 1396 1397 1398 1399 1400 1401 1402
/*
 * The CyberPro chips can be placed on many different bus types.
 * This probe function is common to all bus types.  The bus-specific
 * probe function is expected to have:
 *  - enabled access to the linear memory region
 *  - memory mapped access to the registers
 *  - initialised mem_ctl1 and mem_ctl2 appropriately.
 */
static int __devinit cyberpro_common_probe(struct cfb_info *cfb)
Linus Torvalds's avatar
Linus Torvalds committed
1403 1404
{
	u_long smem_size;
1405
	u_int h_sync, v_sync;
Linus Torvalds's avatar
Linus Torvalds committed
1406 1407
	int err;

1408 1409 1410 1411 1412 1413 1414
	/*
	 * Get the video RAM size and width from the VGA register.
	 * This should have been already initialised by the BIOS,
	 * but if it's garbage, claim default 1MB VRAM (woody)
	 */
	cfb->mem_ctl1 = cyber2000_grphr(EXT_MEM_CTL1, cfb);
	cfb->mem_ctl2 = cyber2000_grphr(EXT_MEM_CTL2, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
1415

1416 1417 1418
	/*
	 * Determine the size of the memory.
	 */
Linus Torvalds's avatar
Linus Torvalds committed
1419 1420 1421
	switch (cfb->mem_ctl2 & MEM_CTL2_SIZE_MASK) {
	case MEM_CTL2_SIZE_4MB:	smem_size = 0x00400000; break;
	case MEM_CTL2_SIZE_2MB:	smem_size = 0x00200000; break;
1422
	case MEM_CTL2_SIZE_1MB: smem_size = 0x00100000; break;
Linus Torvalds's avatar
Linus Torvalds committed
1423 1424 1425
	default:		smem_size = 0x00100000; break;
	}

Linus Torvalds's avatar
Linus Torvalds committed
1426
	cfb->fb.fix.smem_len   = smem_size;
1427
	cfb->fb.fix.mmio_len   = MMIO_SIZE;
Linus Torvalds's avatar
Linus Torvalds committed
1428
	cfb->fb.screen_base    = cfb->region;
Linus Torvalds's avatar
Linus Torvalds committed
1429

1430
	err = -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
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
	if (!fb_find_mode(&cfb->fb.var, &cfb->fb, NULL, NULL, 0,
	    		  &cyber2000fb_default_mode, 8)) {
		printk("%s: no valid mode found\n", cfb->fb.fix.id);
		goto failed;
	}

	cfb->fb.var.yres_virtual = cfb->fb.fix.smem_len * 8 /
			(cfb->fb.var.bits_per_pixel * cfb->fb.var.xres_virtual);

	if (cfb->fb.var.yres_virtual < cfb->fb.var.yres)
		cfb->fb.var.yres_virtual = cfb->fb.var.yres;

	cyber2000fb_set_var(&cfb->fb.var, -1, &cfb->fb);

	/*
	 * Calculate the hsync and vsync frequencies.  Note that
	 * we split the 1e12 constant up so that we can preserve
	 * the precision and fit the results into 32-bit registers.
	 *  (1953125000 * 512 = 1e12)
	 */
	h_sync = 1953125000 / cfb->fb.var.pixclock;
	h_sync = h_sync * 512 / (cfb->fb.var.xres + cfb->fb.var.left_margin +
		 cfb->fb.var.right_margin + cfb->fb.var.hsync_len);
	v_sync = h_sync / (cfb->fb.var.yres + cfb->fb.var.upper_margin +
		 cfb->fb.var.lower_margin + cfb->fb.var.vsync_len);

1457
	printk(KERN_INFO "%s: %dKiB VRAM, using %dx%d, %d.%03dkHz, %dHz\n",
Linus Torvalds's avatar
Linus Torvalds committed
1458 1459 1460 1461 1462
		cfb->fb.fix.id, cfb->fb.fix.smem_len >> 10,
		cfb->fb.var.xres, cfb->fb.var.yres,
		h_sync / 1000, h_sync % 1000, v_sync);

	err = register_framebuffer(&cfb->fb);
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 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 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646

failed:
	return err;
}

static void cyberpro_common_resume(struct cfb_info *cfb)
{
	cyberpro_init_hw(cfb);

	/*
	 * Reprogram the MEM_CTL1 and MEM_CTL2 registers
	 */
	cyber2000_grphw(EXT_MEM_CTL1, cfb->mem_ctl1, cfb);
	cyber2000_grphw(EXT_MEM_CTL2, cfb->mem_ctl2, cfb);

	/*
	 * Restore the old video mode and the palette.
	 * We also need to tell fbcon to redraw the console.
	 */
	cfb->fb.var.activate = FB_ACTIVATE_NOW;
	cyber2000fb_set_var(&cfb->fb.var, -1, &cfb->fb);
}

#ifdef CONFIG_ARCH_SHARK

#include <asm/arch/hardware.h>

static int __devinit
cyberpro_vl_probe(void)
{
	struct cfb_info *cfb;
	int err = -ENOMEM;

	if (!request_mem_region(FB_START,FB_SIZE,"CyberPro2010")) return err;

	cfb = cyberpro_alloc_fb_info(ID_CYBERPRO_2010, "CyberPro2010");
	if (!cfb)
		goto failed_release;

	cfb->dev = NULL;
	cfb->region = ioremap(FB_START,FB_SIZE);
	if (!cfb->region)
		goto failed_ioremap;

	cfb->regs = cfb->region + MMIO_OFFSET;
	cfb->fb.fix.mmio_start = FB_START + MMIO_OFFSET;
	cfb->fb.fix.smem_start = FB_START;

	/*
	 * Bring up the hardware.  This is expected to enable access
	 * to the linear memory region, and allow access to the memory
	 * mapped registers.  Also, mem_ctl1 and mem_ctl2 must be
	 * initialised.
	 */
	cyber2000fb_writeb(0x18, 0x46e8, cfb);
	cyber2000fb_writeb(0x01, 0x102, cfb);
	cyber2000fb_writeb(0x08, 0x46e8, cfb);
	cyber2000fb_writeb(EXT_BIU_MISC, 0x3ce, cfb);
	cyber2000fb_writeb(EXT_BIU_MISC_LIN_ENABLE, 0x3cf, cfb);

	cfb->mclk_mult = 0xdb;
	cfb->mclk_div  = 0x54;

	cyberpro_init_hw(cfb);

	err = cyberpro_common_probe(cfb);
	if (err)
		goto failed;

	if (int_cfb_info == NULL)
		int_cfb_info = cfb;

	return 0;

failed:
	iounmap(cfb->region);
failed_ioremap:
	cyberpro_free_fb_info(cfb);
failed_release:
	release_mem_region(FB_START,FB_SIZE);

	return err;
}
#endif /* CONFIG_ARCH_SHARK */

/*
 * PCI specific support.
 */
#ifdef CONFIG_PCI
/*
 * We need to wake up the CyberPro, and make sure its in linear memory
 * mode.  Unfortunately, this is specific to the platform and card that
 * we are running on.
 *
 * On x86 and ARM, should we be initialising the CyberPro first via the
 * IO registers, and then the MMIO registers to catch all cases?  Can we
 * end up in the situation where the chip is in MMIO mode, but not awake
 * on an x86 system?
 */
static int cyberpro_pci_enable_mmio(struct cfb_info *cfb)
{
#if defined(__sparc_v9__)
#error "You loose, consult DaveM."
#elif defined(__sparc__)
	/*
	 * SPARC does not have an "outb" instruction, so we generate
	 * I/O cycles storing into a reserved memory space at
	 * physical address 0x3000000
	 */
	unsigned char *iop;

	iop = ioremap(0x3000000, 0x5000);
	if (iop == NULL) {
		prom_printf("iga5000: cannot map I/O\n");
		return -ENOMEM;
	}

	writeb(0x18, iop + 0x46e8);
	writeb(0x01, iop + 0x102);
	writeb(0x08, iop + 0x46e8);
	writeb(EXT_BIU_MISC, iop + 0x3ce);
	writeb(EXT_BIU_MISC_LIN_ENABLE, iop + 0x3cf);

	iounmap((void *)iop);
#else
	/*
	 * Most other machine types are "normal", so
	 * we use the standard IO-based wakeup.
	 */
	outb(0x18, 0x46e8);
	outb(0x01, 0x102);
	outb(0x08, 0x46e8);
	outb(EXT_BIU_MISC, 0x3ce);
	outb(EXT_BIU_MISC_LIN_ENABLE, 0x3cf);
#endif
	return 0;
}

static int __devinit
cyberpro_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
{
	struct cfb_info *cfb;
	char name[16];
	int err;

	sprintf(name, "CyberPro%4X", id->device);

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

	err = pci_request_regions(dev, name);
	if (err)
		return err;

	err = -ENOMEM;
	cfb = cyberpro_alloc_fb_info(id->driver_data, name);
	if (!cfb)
		goto failed_release;

	cfb->dev = dev;
	cfb->region = ioremap(pci_resource_start(dev, 0),
			      pci_resource_len(dev, 0));
	if (!cfb->region)
		goto failed_ioremap;

	cfb->regs = cfb->region + MMIO_OFFSET;
	cfb->fb.fix.mmio_start = pci_resource_start(dev, 0) + MMIO_OFFSET;
	cfb->fb.fix.smem_start = pci_resource_start(dev, 0);

	/*
	 * Bring up the hardware.  This is expected to enable access
	 * to the linear memory region, and allow access to the memory
	 * mapped registers.  Also, mem_ctl1 and mem_ctl2 must be
	 * initialised.
	 */
	err = cyberpro_pci_enable_mmio(cfb);
	if (err)
		goto failed;

	/*
	 * Use MCLK from BIOS. FIXME: what about hotplug?
	 */
#ifndef __arm__
1647 1648
	cfb->mclk_mult = cyber2000_grphr(EXT_MCLK_MULT, cfb);
	cfb->mclk_div  = cyber2000_grphr(EXT_MCLK_DIV, cfb);
1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660
#else
	/*
	 * MCLK on the NetWinder and the Shark is fixed at 75MHz
	 */
	cfb->mclk_mult = 0xdb;
	cfb->mclk_div  = 0x54;
#endif

	cyberpro_init_hw(cfb);

	err = cyberpro_common_probe(cfb);
	if (err)
Linus Torvalds's avatar
Linus Torvalds committed
1661 1662 1663 1664 1665
		goto failed;

	/*
	 * Our driver data
	 */
Linus Torvalds's avatar
Linus Torvalds committed
1666
	pci_set_drvdata(dev, cfb);
Linus Torvalds's avatar
Linus Torvalds committed
1667 1668 1669 1670 1671 1672
	if (int_cfb_info == NULL)
		int_cfb_info = cfb;

	return 0;

failed:
Linus Torvalds's avatar
Linus Torvalds committed
1673 1674
	iounmap(cfb->region);
failed_ioremap:
Linus Torvalds's avatar
Linus Torvalds committed
1675
	cyberpro_free_fb_info(cfb);
Linus Torvalds's avatar
Linus Torvalds committed
1676
failed_release:
Linus Torvalds's avatar
Linus Torvalds committed
1677 1678
	pci_release_regions(dev);

Linus Torvalds's avatar
Linus Torvalds committed
1679 1680 1681
	return err;
}

1682
static void __devexit cyberpro_pci_remove(struct pci_dev *dev)
Linus Torvalds's avatar
Linus Torvalds committed
1683
{
Linus Torvalds's avatar
Linus Torvalds committed
1684
	struct cfb_info *cfb = pci_get_drvdata(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695

	if (cfb) {
		/*
		 * If unregister_framebuffer fails, then
		 * we will be leaving hooks that could cause
		 * oopsen laying around.
		 */
		if (unregister_framebuffer(&cfb->fb))
			printk(KERN_WARNING "%s: danger Will Robinson, "
				"danger danger!  Oopsen imminent!\n",
				cfb->fb.fix.id);
Linus Torvalds's avatar
Linus Torvalds committed
1696
		iounmap(cfb->region);
Linus Torvalds's avatar
Linus Torvalds committed
1697 1698 1699 1700 1701 1702
		cyberpro_free_fb_info(cfb);

		/*
		 * Ensure that the driver data is no longer
		 * valid.
		 */
Linus Torvalds's avatar
Linus Torvalds committed
1703
		pci_set_drvdata(dev, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
1704 1705
		if (cfb == int_cfb_info)
			int_cfb_info = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1706 1707

		pci_release_regions(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1708 1709 1710
	}
}

1711
static int cyberpro_pci_suspend(struct pci_dev *dev, u32 state)
Linus Torvalds's avatar
Linus Torvalds committed
1712
{
Linus Torvalds's avatar
Linus Torvalds committed
1713
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1714 1715 1716 1717 1718
}

/*
 * Re-initialise the CyberPro hardware
 */
1719
static int cyberpro_pci_resume(struct pci_dev *dev)
Linus Torvalds's avatar
Linus Torvalds committed
1720
{
Linus Torvalds's avatar
Linus Torvalds committed
1721
	struct cfb_info *cfb = pci_get_drvdata(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1722 1723

	if (cfb) {
1724 1725
		cyberpro_pci_enable_mmio(cfb);
		cyberpro_common_resume(cfb);
Linus Torvalds's avatar
Linus Torvalds committed
1726
	}
Linus Torvalds's avatar
Linus Torvalds committed
1727 1728

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1729 1730 1731
}

static struct pci_device_id cyberpro_pci_table[] __devinitdata = {
1732 1733
	{ PCI_VENDOR_ID_INTERG, PCI_DEVICE_ID_INTERG_1682,
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, ID_IGA_1682 },
Linus Torvalds's avatar
Linus Torvalds committed
1734
	{ PCI_VENDOR_ID_INTERG, PCI_DEVICE_ID_INTERG_2000,
1735
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, ID_CYBERPRO_2000 },
Linus Torvalds's avatar
Linus Torvalds committed
1736
	{ PCI_VENDOR_ID_INTERG, PCI_DEVICE_ID_INTERG_2010,
1737
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, ID_CYBERPRO_2010 },
Linus Torvalds's avatar
Linus Torvalds committed
1738
	{ PCI_VENDOR_ID_INTERG, PCI_DEVICE_ID_INTERG_5000,
1739
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, ID_CYBERPRO_5000 },
Linus Torvalds's avatar
Linus Torvalds committed
1740 1741 1742
	{ 0, }
};

1743 1744
MODULE_DEVICE_TABLE(pci,cyberpro_pci_table);

Linus Torvalds's avatar
Linus Torvalds committed
1745 1746
static struct pci_driver cyberpro_driver = {
	name:		"CyberPro",
1747 1748 1749 1750
	probe:		cyberpro_pci_probe,
	remove:		__devexit_p(cyberpro_pci_remove),
	suspend:	cyberpro_pci_suspend,
	resume:		cyberpro_pci_resume,
Linus Torvalds's avatar
Linus Torvalds committed
1751 1752
	id_table:	cyberpro_pci_table
};
1753
#endif
Linus Torvalds's avatar
Linus Torvalds committed
1754 1755 1756 1757 1758 1759 1760 1761

/*
 * I don't think we can use the "module_init" stuff here because
 * the fbcon stuff may not be initialised yet.  Hence the #ifdef
 * around module_init.
 */
int __init cyber2000fb_init(void)
{
1762
	int ret = -1, err = -ENODEV;
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772
#ifdef CONFIG_ARCH_SHARK
	err = cyberpro_vl_probe();
	if (!err) {
		ret = err;
		MOD_INC_USE_COUNT;
	}
#endif
	err = pci_module_init(&cyberpro_driver);
	if (!err)
		ret = err;
1773

1774
	return ret ? err : 0;
Linus Torvalds's avatar
Linus Torvalds committed
1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785
}

static void __exit cyberpro_exit(void)
{
	pci_unregister_driver(&cyberpro_driver);
}

#ifdef MODULE
module_init(cyber2000fb_init);
#endif
module_exit(cyberpro_exit);
Linus Torvalds's avatar
Linus Torvalds committed
1786 1787 1788 1789

MODULE_AUTHOR("Russell King");
MODULE_DESCRIPTION("CyberPro 2000, 2010 and 5000 framebuffer driver");
MODULE_LICENSE("GPL");