1 /* drivers/video/pvr2fb.c
2  *
3  * Frame buffer and fbcon support for the NEC PowerVR2 found within the Sega
4  * Dreamcast.
5  *
6  * Copyright (c) 2001 M. R. Brown <mrbrown@0xd6.org>
7  * Copyright (c) 2001 Paul Mundt  <lethal@chaoticdreams.org>
8  *
9  * This file is part of the LinuxDC project (linuxdc.sourceforge.net).
10  *
11  */
12 
13 /*
14  * This driver is mostly based on the excellent amifb and vfb sources.  It uses
15  * an odd scheme for converting hardware values to/from framebuffer values, here are
16  * some hacked-up formulas:
17  *
18  *  The Dreamcast has screen offsets from each side of it's four borders and the start
19  *  offsets of the display window.  I used these values to calculate 'pseudo' values
20  *  (think of them as placeholders) for the fb video mode, so that when it came time
21  *  to convert these values back into their hardware values, I could just add mode-
22  *  specific offsets to get the correct mode settings:
23  *
24  *      left_margin = diwstart_h - borderstart_h;
25  *      right_margin = borderstop_h - (diwstart_h + xres);
26  *      upper_margin = diwstart_v - borderstart_v;
27  *      lower_margin = borderstop_v - (diwstart_h + yres);
28  *
29  *      hsync_len = borderstart_h + (hsync_total - borderstop_h);
30  *      vsync_len = borderstart_v + (vsync_total - borderstop_v);
31  *
32  *  Then, when it's time to convert back to hardware settings, the only constants
33  *  are the borderstart_* offsets, all other values are derived from the fb video
34  *  mode:
35  *
36  *      // PAL
37  *      borderstart_h = 116;
38  *      borderstart_v = 44;
39  *      ...
40  *      borderstop_h = borderstart_h + hsync_total - hsync_len;
41  *      ...
42  *      diwstart_v = borderstart_v - upper_margin;
43  *
44  *  However, in the current implementation, the borderstart values haven't had
45  *  the benefit of being fully researched, so some modes may be broken.
46  */
47 
48 #include <linux/module.h>
49 #include <linux/kernel.h>
50 #include <linux/errno.h>
51 #include <linux/string.h>
52 #include <linux/mm.h>
53 #include <linux/tty.h>
54 #include <linux/slab.h>
55 #include <linux/delay.h>
56 #include <linux/config.h>
57 #include <linux/interrupt.h>
58 #include <linux/fb.h>
59 #include <linux/init.h>
60 #include <linux/console.h>
61 
62 #ifdef CONFIG_SH_DREAMCAST
63 #include <asm/io.h>
64 #include <asm/machvec.h>
65 #include <asm/dc_sysasic.h>
66 #endif
67 
68 #ifdef CONFIG_MTRR
69   #include <asm/mtrr.h>
70 #endif
71 
72 #include <video/fbcon.h>
73 #include <video/fbcon-cfb16.h>
74 #include <video/fbcon-cfb24.h>
75 #include <video/fbcon-cfb32.h>
76 
77 #ifdef CONFIG_FB_PVR2_DEBUG
78 #  define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __FUNCTION__ , ## args)
79 #else
80 #  define DPRINTK(fmt, args...)
81 #endif
82 
83 /* 2D video registers */
84 #define DISP_BASE 0xa05f8000
85 
86 #define DISP_BRDRCOLR (DISP_BASE + 0x40)
87 #define DISP_DIWMODE (DISP_BASE + 0x44)
88 #define DISP_DIWADDRL (DISP_BASE + 0x50)
89 #define DISP_DIWADDRS (DISP_BASE + 0x54)
90 #define DISP_DIWSIZE (DISP_BASE + 0x5c)
91 #define DISP_SYNCCONF (DISP_BASE + 0xd0)
92 #define DISP_BRDRHORZ (DISP_BASE + 0xd4)
93 #define DISP_SYNCSIZE (DISP_BASE + 0xd8)
94 #define DISP_BRDRVERT (DISP_BASE + 0xdc)
95 #define DISP_DIWCONF (DISP_BASE + 0xe8)
96 #define DISP_DIWHSTRT (DISP_BASE + 0xec)
97 #define DISP_DIWVSTRT (DISP_BASE + 0xf0)
98 
99 /* Pixel clocks, one for TV output, doubled for VGA output */
100 #define TV_CLK 74239
101 #define VGA_CLK 37119
102 
103 /* This is for 60Hz - the VTOTAL is doubled for interlaced modes */
104 #define PAL_HTOTAL 863
105 #define PAL_VTOTAL 312
106 #define NTSC_HTOTAL 857
107 #define NTSC_VTOTAL 262
108 
109 enum { CT_VGA, CT_NONE, CT_RGB, CT_COMPOSITE };
110 
111 enum { VO_PAL, VO_NTSC, VO_VGA };
112 
113 struct pvr2_params { u_short val; char *name; };
114 static struct pvr2_params cables[] __initdata = {
115 	{ CT_VGA, "VGA" }, { CT_RGB, "RGB" }, { CT_COMPOSITE, "COMPOSITE" },
116 };
117 
118 static struct pvr2_params outputs[] __initdata = {
119 	{ VO_PAL, "PAL" }, { VO_NTSC, "NTSC" }, { VO_VGA, "VGA" },
120 };
121 
122 /*
123  * This describes the current video mode
124  */
125 
126 static struct pvr2fb_par {
127 
128 	int xres;
129 	int yres;
130 	int vxres;
131 	int vyres;
132 	int xoffset;
133 	int yoffset;
134 	u_short bpp;
135 
136 	u_long pixclock;
137 	u_short hsync_total;	/* Clocks/line */
138 	u_short vsync_total;	/* Lines/field */
139 	u_short borderstart_h;
140 	u_short borderstop_h;
141 	u_short borderstart_v;
142 	u_short borderstop_v;
143 	u_short diwstart_h;	/* Horizontal offset of the display field */
144 	u_short diwstart_v;	/* Vertical offset of the display field, for
145 				   interlaced modes, this is the long field */
146 	u_long disp_start;	/* Address of image within VRAM */
147 
148 	u_long next_line;	/* Modulo for next line */
149 
150 	u_char is_interlaced;	/* Is the display interlaced? */
151 	u_char is_doublescan;	/* Are scanlines output twice? (doublescan) */
152 	u_char is_lowres;	/* Is horizontal pixel-doubling enabled? */
153 
154 	u_long bordercolor;	/* RGB888 format border color */
155 
156 	u_long vmode;
157 
158 } currentpar;
159 
160 static int currcon = 0;
161 static int currbpp;
162 static struct display disp;
163 static struct fb_info fb_info;
164 static int pvr2fb_inverse = 0;
165 
166 static struct { u_short red, green, blue, alpha; } palette[256];
167 static union {
168 #ifdef FBCON_HAS_CFB16
169 	u16 cfb16[16];
170 #endif
171 #ifdef FBCON_HAS_CFB24
172 	u32 cfb24[16];
173 #endif
174 #ifdef FBCON_HAS_CFB32
175 	u32 cfb32[16];
176 #endif
177 } fbcon_cmap;
178 
179 static char pvr2fb_name[16] = "NEC PowerVR2";
180 
181 #define VIDEOMEMSIZE (8*1024*1024)
182 static u_long videomemory = 0xa5000000, videomemorysize = VIDEOMEMSIZE;
183 static int cable_type = -1;
184 static int video_output = -1;
185 
186 #ifdef CONFIG_MTRR
187 static int enable_mtrr = 1;
188 static int mtrr_handle;
189 #endif
190 
191 /*
192  * We do all updating, blanking, etc. during the vertical retrace period
193  */
194 
195 static u_short do_vmode_full = 0;	/* Change the video mode */
196 static u_short do_vmode_pan = 0;	/* Update the video mode */
197 static short do_blank = 0;		/* (Un)Blank the screen */
198 
199 static u_short is_blanked = 0;		/* Is the screen blanked? */
200 
201 /* Interface used by the world */
202 
203 int pvr2fb_setup(char*);
204 
205 static int pvr2fb_get_fix(struct fb_fix_screeninfo *fix, int con,
206                             struct fb_info *info);
207 static int pvr2fb_get_var(struct fb_var_screeninfo *var, int con,
208                             struct fb_info *info);
209 static int pvr2fb_set_var(struct fb_var_screeninfo *var, int con,
210                             struct fb_info *info);
211 static int pvr2fb_pan_display(struct fb_var_screeninfo *var, int con,
212                                 struct fb_info *info);
213 static int pvr2fb_get_cmap(struct fb_cmap *cmap, int kspc, int con,
214                              struct fb_info *info);
215 static int pvr2fb_set_cmap(struct fb_cmap *cmap, int kspc, int con,
216                              struct fb_info *info);
217 
218 	/*
219 	 * Interface to the low level console driver
220 	 */
221 
222 static int pvr2fbcon_switch(int con, struct fb_info *info);
223 static int pvr2fbcon_updatevar(int con, struct fb_info *info);
224 static void pvr2fbcon_blank(int blank, struct fb_info *info);
225 
226 	/*
227 	 * Internal/hardware-specific routines
228 	 */
229 
230 static void do_install_cmap(int con, struct fb_info *info);
231 static u_long get_line_length(int xres_virtual, int bpp);
232 static void set_color_bitfields(struct fb_var_screeninfo *var);
233 static int pvr2_getcolreg(u_int regno, u_int *red, u_int *green, u_int *blue,
234                             u_int *transp, struct fb_info *info);
235 static int pvr2_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
236                             u_int transp, struct fb_info *info);
237 
238 static int pvr2_encode_fix(struct fb_fix_screeninfo *fix,
239                              struct pvr2fb_par *par);
240 static int pvr2_decode_var(struct fb_var_screeninfo *var,
241                           struct pvr2fb_par *par);
242 static int pvr2_encode_var(struct fb_var_screeninfo *var,
243                           struct pvr2fb_par *par);
244 static void pvr2_get_par(struct pvr2fb_par *par);
245 static void pvr2_set_var(struct fb_var_screeninfo *var);
246 static void pvr2_pan_var(struct fb_var_screeninfo *var);
247 static int pvr2_update_par(void);
248 static void pvr2_update_display(void);
249 static void pvr2_init_display(void);
250 static void pvr2_do_blank(void);
251 static void pvr2fb_interrupt(int irq, void *dev_id, struct pt_regs *fp);
252 static int pvr2_init_cable(void);
253 static int pvr2_get_param(const struct pvr2_params *p, const char *s,
254                             int val, int size);
255 
256 static struct fb_ops pvr2fb_ops = {
257 	owner:		THIS_MODULE,
258 	fb_get_fix:	pvr2fb_get_fix,
259 	fb_get_var:	pvr2fb_get_var,
260 	fb_set_var:	pvr2fb_set_var,
261 	fb_get_cmap:	pvr2fb_get_cmap,
262 	fb_set_cmap:	pvr2fb_set_cmap,
263 	fb_pan_display: pvr2fb_pan_display,
264 };
265 
266 static struct fb_videomode pvr2_modedb[] __initdata = {
267 
268     /*
269      * Broadcast video modes (PAL and NTSC).  I'm unfamiliar with
270      * PAL-M and PAL-N, but from what I've read both modes parallel PAL and
271      * NTSC, so it shouldn't be a problem (I hope).
272      */
273 
274     {
275 	/* 640x480 @ 60Hz interlaced (NTSC) */
276 	"ntsc_640x480i", 60, 640, 480, TV_CLK, 38, 33, 0, 18, 146, 26,
277 	FB_SYNC_BROADCAST, FB_VMODE_INTERLACED | FB_VMODE_YWRAP
278     },
279 
280     {
281 	/* 640x240 @ 60Hz (NTSC) */
282 	/* XXX: Broken! Don't use... */
283 	"ntsc_640x240", 60, 640, 240, TV_CLK, 38, 33, 0, 0, 146, 22,
284 	FB_SYNC_BROADCAST, FB_VMODE_YWRAP
285     },
286 
287     {
288 	/* 640x480 @ 60hz (VGA) */
289 	"vga_640x480", 60, 640, 480, VGA_CLK, 38, 33, 0, 18, 146, 26,
290 	0, FB_VMODE_YWRAP
291     },
292 
293 };
294 
295 #define NUM_TOTAL_MODES  ARRAY_SIZE(pvr2_modedb)
296 
297 #define DEFMODE_NTSC	0
298 #define DEFMODE_PAL	0
299 #define DEFMODE_VGA	2
300 
301 static int defmode = DEFMODE_NTSC;
302 static char *mode_option __initdata = NULL;
303 
304 /* Get the fixed part of the display */
305 
pvr2fb_get_fix(struct fb_fix_screeninfo * fix,int con,struct fb_info * info)306 static int pvr2fb_get_fix(struct fb_fix_screeninfo *fix, int con,
307                             struct fb_info *info)
308 {
309 	struct pvr2fb_par par;
310 
311 	if (con == -1)
312 		pvr2_get_par(&par);
313 	else {
314 		int err;
315 
316 		if ((err = pvr2_decode_var(&fb_display[con].var, &par)))
317 			return err;
318 	}
319 	return pvr2_encode_fix(fix, &par);
320 }
321 
322 /* Get the user-defined part of the display */
323 
pvr2fb_get_var(struct fb_var_screeninfo * var,int con,struct fb_info * info)324 static int pvr2fb_get_var(struct fb_var_screeninfo *var, int con,
325                             struct fb_info *info)
326 {
327 	int err = 0;
328 
329 	if (con == -1) {
330 		struct pvr2fb_par par;
331 
332 		pvr2_get_par(&par);
333 		err = pvr2_encode_var(var, &par);
334 	} else
335 		*var = fb_display[con].var;
336 
337 	return err;
338 }
339 
340 /* Set the user-defined part of the display */
341 
pvr2fb_set_var(struct fb_var_screeninfo * var,int con,struct fb_info * info)342 static int pvr2fb_set_var(struct fb_var_screeninfo *var, int con,
343                             struct fb_info *info)
344 {
345 	int err, activate = var->activate;
346 	int oldxres, oldyres, oldvxres, oldvyres, oldbpp;
347 	struct pvr2fb_par par;
348 
349 	struct display *display;
350 	if (con >= 0)
351 		display = &fb_display[con];
352 	else
353 		display = &disp;        /* used during initialization */
354 
355 	/*
356 	 * FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
357 	 * as FB_VMODE_SMOOTH_XPAN is only used internally
358 	 */
359 
360 	if (var->vmode & FB_VMODE_CONUPDATE) {
361 		var->vmode |= FB_VMODE_YWRAP;
362 		var->xoffset = display->var.xoffset;
363 		var->yoffset = display->var.yoffset;
364 	}
365 	if ((err = pvr2_decode_var(var, &par)))
366 		return err;
367 	pvr2_encode_var(var, &par);
368 
369 	/* Do memory check and bitfield set here?? */
370 
371 	if ((activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW) {
372 		oldxres = display->var.xres;
373 		oldyres = display->var.yres;
374 		oldvxres = display->var.xres_virtual;
375 		oldvyres = display->var.yres_virtual;
376 		oldbpp = display->var.bits_per_pixel;
377 		display->var = *var;
378 		if (oldxres != var->xres || oldyres != var->yres ||
379 		    oldvxres != var->xres_virtual || oldvyres != var->yres_virtual ||
380 		    oldbpp != var->bits_per_pixel) {
381 			struct fb_fix_screeninfo fix;
382 
383 			pvr2_encode_fix(&fix, &par);
384 			display->screen_base = (char *)fix.smem_start;
385 			display->scrollmode = SCROLL_YREDRAW;
386 			display->visual = fix.visual;
387 			display->type = fix.type;
388 			display->type_aux = fix.type_aux;
389 			display->ypanstep = fix.ypanstep;
390 			display->ywrapstep = fix.ywrapstep;
391 			display->line_length = fix.line_length;
392 			display->can_soft_blank = 1;
393 			display->inverse = pvr2fb_inverse;
394 			switch (var->bits_per_pixel) {
395 #ifdef FBCON_HAS_CFB16
396 			    case 16:
397 				display->dispsw = &fbcon_cfb16;
398 				display->dispsw_data = fbcon_cmap.cfb16;
399 				break;
400 #endif
401 #ifdef FBCON_HAS_CFB24
402 			    case 24:
403 				display->dispsw = &fbcon_cfb24;
404 				display->dispsw_data = fbcon_cmap.cfb24;
405 				break;
406 #endif
407 #ifdef FBCON_HAS_CFB32
408 			    case 32:
409 				display->dispsw = &fbcon_cfb32;
410 				display->dispsw_data = fbcon_cmap.cfb32;
411 				break;
412 #endif
413 			    default:
414 				display->dispsw = &fbcon_dummy;
415 				break;
416 			}
417 			if (fb_info.changevar)
418 				(*fb_info.changevar)(con);
419 		}
420 		if (oldbpp != var->bits_per_pixel) {
421 			if ((err = fb_alloc_cmap(&display->cmap, 0, 0)))
422 				return err;
423 			do_install_cmap(con, info);
424 		}
425 		if (con == currcon)
426 			pvr2_set_var(&display->var);
427 	}
428 
429 	return 0;
430 }
431 
432 /*
433  * Pan or wrap the display.
434  * This call looks only at xoffset, yoffset and the FB_VMODE_YRAP flag
435  */
436 
pvr2fb_pan_display(struct fb_var_screeninfo * var,int con,struct fb_info * info)437 static int pvr2fb_pan_display(struct fb_var_screeninfo *var, int con,
438                                 struct fb_info *info)
439 {
440 	if (var->vmode & FB_VMODE_YWRAP) {
441 		if (var->yoffset<0 || var->yoffset >=
442 		    fb_display[con].var.yres_virtual || var->xoffset)
443 			return -EINVAL;
444 	 } else {
445 		if (var->xoffset+fb_display[con].var.xres >
446 		    fb_display[con].var.xres_virtual ||
447 		    var->yoffset+fb_display[con].var.yres >
448 		    fb_display[con].var.yres_virtual)
449 		    return -EINVAL;
450 	}
451 	if (con == currcon)
452 		pvr2_pan_var(var);
453 	fb_display[con].var.xoffset = var->xoffset;
454 	fb_display[con].var.yoffset = var->yoffset;
455 	if (var->vmode & FB_VMODE_YWRAP)
456 		fb_display[con].var.vmode |= FB_VMODE_YWRAP;
457 	else
458 		fb_display[con].var.vmode &= ~FB_VMODE_YWRAP;
459 
460 	return 0;
461 }
462 
463 /* Get the colormap */
464 
pvr2fb_get_cmap(struct fb_cmap * cmap,int kspc,int con,struct fb_info * info)465 static int pvr2fb_get_cmap(struct fb_cmap *cmap, int kspc, int con,
466                              struct fb_info *info)
467 {
468 	if (con == currcon) /* current console? */
469 		return fb_get_cmap(cmap, kspc, pvr2_getcolreg, info);
470 	else if (fb_display[con].cmap.len) /* non default colormap? */
471 		fb_copy_cmap(&fb_display[con].cmap, cmap, kspc ? 0 : 2);
472 	else
473 		fb_copy_cmap(fb_default_cmap(1<<fb_display[con].var.bits_per_pixel),
474 		             cmap, kspc ? 0 : 2);
475 	return 0;
476 }
477 
478 /* Set the colormap */
479 
pvr2fb_set_cmap(struct fb_cmap * cmap,int kspc,int con,struct fb_info * info)480 static int pvr2fb_set_cmap(struct fb_cmap *cmap, int kspc, int con,
481 	                     struct fb_info *info)
482 {
483 	int err;
484 
485 	if (!fb_display[con].cmap.len) {        /* no colormap allocated? */
486 		if ((err = fb_alloc_cmap(&fb_display[con].cmap,
487 		                         1<<fb_display[con].var.bits_per_pixel,
488 					 0)))
489 			 return err;
490 	}
491 	if (con == currcon)                     /* current console? */
492 		return fb_set_cmap(cmap, kspc, pvr2_setcolreg, info);
493 	else
494 		fb_copy_cmap(cmap, &fb_display[con].cmap, kspc ? 0 : 1);
495 
496 	return 0;
497 }
498 
pvr2fbcon_switch(int con,struct fb_info * info)499 static int pvr2fbcon_switch(int con, struct fb_info *info)
500 {
501 	/* Do we have to save the colormap? */
502 	if (fb_display[currcon].cmap.len)
503 		fb_get_cmap(&fb_display[currcon].cmap, 1, pvr2_getcolreg, info);
504 
505 	currcon = con;
506 	pvr2_set_var(&fb_display[con].var);
507 	/* Install new colormap */
508 	do_install_cmap(con, info);
509 	return 0;
510 }
511 
pvr2fbcon_updatevar(int con,struct fb_info * info)512 static int pvr2fbcon_updatevar(int con, struct fb_info *info)
513 {
514 	pvr2_pan_var(&fb_display[con].var);
515 	return 0;
516 }
517 
pvr2fbcon_blank(int blank,struct fb_info * info)518 static void pvr2fbcon_blank(int blank, struct fb_info *info)
519 {
520 	do_blank = blank ? blank : -1;
521 }
522 
523 /* Setup the colormap */
524 
do_install_cmap(int con,struct fb_info * info)525 static void do_install_cmap(int con, struct fb_info *info)
526 {
527 	if (con != currcon)
528 		return;
529 	if (fb_display[con].cmap.len)
530 		fb_set_cmap(&fb_display[con].cmap, 1, pvr2_setcolreg, info);
531 	else
532 		fb_set_cmap(fb_default_cmap(1<<fb_display[con].var.bits_per_pixel),
533                             1, pvr2_setcolreg, info);
534 }
535 
get_line_length(int xres_virtual,int bpp)536 static inline u_long get_line_length(int xres_virtual, int bpp)
537 {
538 	return (u_long)((((xres_virtual*bpp)+31)&~31) >> 3);
539 }
540 
set_color_bitfields(struct fb_var_screeninfo * var)541 static void set_color_bitfields(struct fb_var_screeninfo *var)
542 {
543 	switch (var->bits_per_pixel) {
544 	    case 16:        /* RGB 565 */
545 		var->red.offset = 11;    var->red.length = 5;
546 		var->green.offset = 5;   var->green.length = 6;
547 		var->blue.offset = 0;    var->blue.length = 5;
548 		var->transp.offset = 0;  var->transp.length = 0;
549 		break;
550 	    case 24:        /* RGB 888 */
551 		var->red.offset = 16;    var->red.length = 8;
552 		var->green.offset = 8;   var->green.length = 8;
553 		var->blue.offset = 0;    var->blue.length = 8;
554 		var->transp.offset = 0;  var->transp.length = 0;
555 		break;
556 	    case 32:        /* ARGB 8888 */
557 		var->red.offset = 16;    var->red.length = 8;
558 		var->green.offset = 8;   var->green.length = 8;
559 		var->blue.offset = 0;    var->blue.length = 8;
560 		var->transp.offset = 24; var->transp.length = 8;
561 		break;
562 	}
563 }
564 
pvr2_getcolreg(u_int regno,u_int * red,u_int * green,u_int * blue,u_int * transp,struct fb_info * info)565 static int pvr2_getcolreg(u_int regno, u_int *red, u_int *green, u_int *blue,
566                             u_int *transp, struct fb_info *info)
567 {
568 	if (regno > 255)
569 	    return 1;
570 
571 	*red = palette[regno].red;
572 	*green = palette[regno].green;
573 	*blue = palette[regno].blue;
574 	*transp = 0;
575 	return 0;
576 }
577 
pvr2_setcolreg(u_int regno,u_int red,u_int green,u_int blue,u_int transp,struct fb_info * info)578 static int pvr2_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
579                             u_int transp, struct fb_info *info)
580 {
581 	if (regno > 255)
582 		return 1;
583 
584 	palette[regno].red = red;
585 	palette[regno].green = green;
586 	palette[regno].blue = blue;
587 
588 	if (regno < 16) {
589 		switch (currbpp) {
590 #ifdef FBCON_HAS_CFB16
591 		    case 16: /* RGB 565 */
592 			fbcon_cmap.cfb16[regno] = (red & 0xf800) |
593 			                          ((green & 0xfc00) >> 5) |
594 						  ((blue & 0xf800) >> 11);
595 			break;
596 #endif
597 #ifdef FBCON_HAS_CFB24
598 		    case 24: /* RGB 888 */
599 			red >>= 8; green >>= 8; blue >>= 8;
600 			fbcon_cmap.cfb24[regno] = (red << 16) | (green << 8) | blue;
601 			break;
602 #endif
603 #ifdef FBCON_HAS_CFB32
604 		    case 32: /* ARGB 8888 */
605 			red >>= 8; green >>= 8; blue >>= 8;
606 			fbcon_cmap.cfb32[regno] = (red << 16) | (green << 8) | blue;
607 			break;
608 #endif
609 		    default:
610 			DPRINTK("Invalid bit depth %d?!?\n", currbpp);
611 			return 1;
612 		}
613 	}
614 
615 	return 0;
616 }
617 
618 
pvr2_encode_fix(struct fb_fix_screeninfo * fix,struct pvr2fb_par * par)619 static int pvr2_encode_fix(struct fb_fix_screeninfo *fix,
620                              struct pvr2fb_par *par)
621 {
622 	memset(fix, 0, sizeof(struct fb_fix_screeninfo));
623 	strcpy(fix->id, pvr2fb_name);
624 	fix->smem_start = videomemory;
625 	fix->smem_len = videomemorysize;
626 	fix->type = FB_TYPE_PACKED_PIXELS;
627 	fix->type_aux = 0;
628 	fix->visual = FB_VISUAL_TRUECOLOR;
629 
630 	if (par->vmode & FB_VMODE_YWRAP) {
631 		fix->ywrapstep = 1;
632 		fix->xpanstep = fix->ypanstep = 0;
633 	} else {
634 		fix->ywrapstep = 0;
635 		fix->xpanstep = 1;
636 		fix->ypanstep = 1;
637 	}
638 	fix->line_length = par->next_line;
639 
640 	return 0;
641 }
642 
643 /*
644  * Create a hardware video mode using the framebuffer values.  If a value needs
645  * to be clipped or constrained it's done here.  This routine needs a bit more
646  * work to make sure we're doing the right tests at the right time.
647  */
pvr2_decode_var(struct fb_var_screeninfo * var,struct pvr2fb_par * par)648 static int pvr2_decode_var(struct fb_var_screeninfo *var,
649                              struct pvr2fb_par *par)
650 {
651 	u_long line_length;
652 	u_short vtotal;
653 
654 	if (var->pixclock != TV_CLK && var->pixclock != VGA_CLK) {
655 		DPRINTK("Invalid pixclock value %d\n", var->pixclock);
656 		return -EINVAL;
657 	}
658 	par->pixclock = var->pixclock;
659 
660 	if ((par->xres = var->xres) < 320)
661 		par->xres = 320;
662 	if ((par->yres = var->yres) < 240)
663 		par->yres = 240;
664 	if ((par->vxres = var->xres_virtual) < par->xres)
665 		par->vxres = par->xres;
666 	if ((par->vyres = var->yres_virtual) < par->yres)
667 		par->vyres = par->yres;
668 
669 	if ((par->bpp = var->bits_per_pixel) <= 16)
670 		par->bpp = 16;
671 	else if ((par->bpp = var->bits_per_pixel) <= 24)
672 		par->bpp = 24;
673 	else if ((par->bpp = var->bits_per_pixel) <= 32)
674 		par->bpp = 32;
675 
676 	currbpp = par->bpp;
677 
678 	/*
679 	 * XXX: It's possible that a user could use a VGA box, change the cable
680 	 * type in hardware (i.e. switch from VGA<->composite), then change modes
681 	 * (i.e. switching to another VT).  If that happens we should automagically
682 	 * change the output format to cope, but currently I don't have a VGA box
683 	 * to make sure this works properly.
684 	 */
685 	cable_type = pvr2_init_cable();
686 	if (cable_type == CT_VGA && video_output != VO_VGA)
687 		video_output = VO_VGA;
688 
689 	par->vmode = var->vmode & FB_VMODE_MASK;
690 	if (par->vmode & FB_VMODE_INTERLACED && video_output != VO_VGA)
691 		par->is_interlaced = 1;
692 	/*
693 	 * XXX: Need to be more creative with this (i.e. allow doublecan for
694 	 * PAL/NTSC output).
695 	 */
696 	par->is_doublescan = (par->yres < 480 && video_output == VO_VGA);
697 
698 	par->hsync_total = var->left_margin + var->xres + var->right_margin +
699 	                   var->hsync_len;
700 	par->vsync_total = var->upper_margin + var->yres + var->lower_margin +
701 	                   var->vsync_len;
702 
703 	if (var->sync & FB_SYNC_BROADCAST) {
704 		vtotal = par->vsync_total;
705 		if (par->is_interlaced)
706 			vtotal /= 2;
707 		if (vtotal > (PAL_VTOTAL + NTSC_VTOTAL)/2) {
708 			/* PAL video output */
709 			/* XXX: Should be using a range here ... ? */
710 			if (par->hsync_total != PAL_HTOTAL) {
711 				DPRINTK("invalid hsync total for PAL\n");
712 				return -EINVAL;
713 			}
714 			/* XXX: Check for start values here... */
715 			/* XXX: Check hardware for PAL-compatibility */
716 			par->borderstart_h = 116;
717 			par->borderstart_v = 44;
718 		} else {
719 			/* NTSC video output */
720 			if (par->hsync_total != NTSC_HTOTAL) {
721 				DPRINTK("invalid hsync total for NTSC\n");
722 				return -EINVAL;
723 			}
724 			par->borderstart_h = 126;
725 			par->borderstart_v = 18;
726 		}
727 	} else {
728 		/* VGA mode */
729 		/* XXX: What else needs to be checked? */
730 		/*
731 		 * XXX: We have a little freedom in VGA modes, what ranges should
732 		 * be here (i.e. hsync/vsync totals, etc.)?
733 		 */
734 		par->borderstart_h = 126;
735 		par->borderstart_v = 40;
736 	}
737 
738 	/* Calculate the remainding offsets */
739 	par->borderstop_h = par->borderstart_h + par->hsync_total -
740 	                    var->hsync_len;
741 	par->borderstop_v = par->borderstart_v + par->vsync_total -
742 	                    var->vsync_len;
743 	par->diwstart_h = par->borderstart_h + var->left_margin;
744 	par->diwstart_v = par->borderstart_v + var->upper_margin;
745 	if (!par->is_interlaced)
746 		par->borderstop_v /= 2;
747 
748 	if (par->xres < 640)
749 		par->is_lowres = 1;
750 
751 	/* XXX: Needs testing. */
752 	if (!((par->vmode ^ var->vmode) & FB_VMODE_YWRAP)) {
753 		par->xoffset = var->xoffset;
754 		par->yoffset = var->yoffset;
755 		if (par->vmode & FB_VMODE_YWRAP) {
756 			if (par->xoffset || par->yoffset < 0 || par->yoffset >=
757 			    par->vyres)
758 				par->xoffset = par->yoffset = 0;
759 		} else {
760 			if (par->xoffset < 0 || par->xoffset > par->vxres-par->xres ||
761 			    par->yoffset < 0 || par->yoffset > par->vyres-par->yres)
762 				par->xoffset = par->yoffset = 0;
763 		}
764 	} else
765 		par->xoffset = par->yoffset = 0;
766 
767 	/* Check memory sizes */
768 	line_length = get_line_length(var->xres_virtual, var->bits_per_pixel);
769 	if (line_length * var->yres_virtual > videomemorysize)
770 		return -ENOMEM;
771 	par->disp_start = videomemory + (get_line_length(par->vxres, par->bpp) *
772 	                  par->yoffset) * get_line_length(par->xoffset, par->bpp);
773 	par->next_line = line_length;
774 
775 	return 0;
776 }
777 
pvr2_encode_var(struct fb_var_screeninfo * var,struct pvr2fb_par * par)778 static int pvr2_encode_var(struct fb_var_screeninfo *var,
779                              struct pvr2fb_par *par)
780 {
781 	memset(var, 0, sizeof(struct fb_var_screeninfo));
782 
783 	var->xres = par->xres;
784 	var->yres = par->yres;
785 	var->xres_virtual = par->vxres;
786 	var->yres_virtual = par->vyres;
787 	var->xoffset = par->xoffset;
788 	var->yoffset = par->yoffset;
789 
790 	var->bits_per_pixel = par->bpp;
791 	set_color_bitfields(var);
792 
793 	var->activate = FB_ACTIVATE_NOW;
794 	var->height = -1;
795 	var->width = -1;
796 
797 	var->pixclock = par->pixclock;
798 
799 	if (par->is_doublescan)
800 		var->vmode = FB_VMODE_DOUBLE;
801 
802 	if (par->is_interlaced)
803 		var->vmode |= FB_VMODE_INTERLACED;
804 	else
805 		var->vmode |= FB_VMODE_NONINTERLACED;
806 
807 	var->right_margin = par->borderstop_h - (par->diwstart_h + par->xres);
808 	var->left_margin = par->diwstart_h - par->borderstart_h;
809 	var->hsync_len = par->borderstart_h + (par->hsync_total - par->borderstop_h);
810 	var->upper_margin = par->diwstart_v - par->borderstart_v;
811 	var->lower_margin = par->borderstop_v - (par->diwstart_v + par->yres);
812 	var->vsync_len = par->borderstart_v + (par->vsync_total - par->borderstop_v);
813 	if (video_output != VO_VGA)
814 		var->sync = FB_SYNC_BROADCAST;
815 
816 	if (par->vmode & FB_VMODE_YWRAP)
817 		var->vmode |= FB_VMODE_YWRAP;
818 
819 	return 0;
820 }
821 
pvr2_get_par(struct pvr2fb_par * par)822 static void pvr2_get_par(struct pvr2fb_par *par)
823 {
824 	*par = currentpar;
825 }
826 
827 /* Setup the new videomode in hardware */
828 
pvr2_set_var(struct fb_var_screeninfo * var)829 static void pvr2_set_var(struct fb_var_screeninfo *var)
830 {
831 	do_vmode_pan = 0;
832 	do_vmode_full = 0;
833 	pvr2_decode_var(var, &currentpar);
834 
835 	do_vmode_full = 1;
836 }
837 
838 /*
839  * Pan or wrap the display
840  * This call looks only at xoffset, yoffset and the FB_VMODE_YWRAP flag in `var'.
841  */
pvr2_pan_var(struct fb_var_screeninfo * var)842 static void pvr2_pan_var(struct fb_var_screeninfo *var)
843 {
844 	struct pvr2fb_par *par = &currentpar;
845 
846 	par->xoffset = var->xoffset;
847 	par->yoffset = var->yoffset;
848 	if (var->vmode & FB_VMODE_YWRAP)
849 		par->vmode |= FB_VMODE_YWRAP;
850 	else
851 		par->vmode &= ~FB_VMODE_YWRAP;
852 
853 	do_vmode_pan = 0;
854 	pvr2_update_par();
855 	do_vmode_pan = 1;
856 }
857 
pvr2_update_par(void)858 static int pvr2_update_par(void)
859 {
860 	struct pvr2fb_par *par = &currentpar;
861 	u_long move;
862 
863 	move = get_line_length(par->xoffset, par->bpp);
864 	if (par->yoffset) {
865 		par->disp_start += (par->next_line * par->yoffset) + move;
866 	} else
867 		par->disp_start += move;
868 
869 	return 0;
870 }
871 
pvr2_update_display(void)872 static void pvr2_update_display(void)
873 {
874 	struct pvr2fb_par *par = &currentpar;
875 
876 	/* Update the start address of the display image */
877 	ctrl_outl(par->disp_start, DISP_DIWADDRL);
878 	ctrl_outl(par->disp_start +
879 		  get_line_length(par->xoffset + par->xres, par->bpp),
880 	          DISP_DIWADDRS);
881 }
882 
883 /*
884  * Initialize the video mode.  Currently, the 16bpp and 24bpp modes aren't
885  * very stable.  It's probably due to the fact that a lot of the 2D video
886  * registers are still undocumented.
887  */
888 
pvr2_init_display(void)889 static void pvr2_init_display(void)
890 {
891 	struct pvr2fb_par *par = &currentpar;
892 	u_short diw_height, diw_width, diw_modulo = 1;
893 	u_short bytesperpixel = par->bpp / 8;
894 
895 	/* hsync and vsync totals */
896 	ctrl_outl((par->vsync_total << 16) | par->hsync_total, DISP_SYNCSIZE);
897 
898 	/* column height, modulo, row width */
899 	/* since we're "panning" within vram, we need to offset things based
900 	 * on the offset from the virtual x start to our real gfx. */
901 	if (video_output != VO_VGA && par->is_interlaced)
902 		diw_modulo += par->next_line / 4;
903 	diw_height = (par->is_interlaced ? par->yres / 2 : par->yres);
904 	diw_width = get_line_length(par->xres, par->bpp) / 4;
905 	ctrl_outl((diw_modulo << 20) | (--diw_height << 10) | --diw_width,
906 	          DISP_DIWSIZE);
907 
908 	/* display address, long and short fields */
909 	ctrl_outl(par->disp_start, DISP_DIWADDRL);
910 	ctrl_outl(par->disp_start +
911 	          get_line_length(par->xoffset + par->xres, par->bpp),
912 	          DISP_DIWADDRS);
913 
914 	/* border horizontal, border vertical, border color */
915 	ctrl_outl((par->borderstart_h << 16) | par->borderstop_h, DISP_BRDRHORZ);
916 	ctrl_outl((par->borderstart_v << 16) | par->borderstop_v, DISP_BRDRVERT);
917 	ctrl_outl(0, DISP_BRDRCOLR);
918 
919 	/* display window start position */
920 	ctrl_outl(par->diwstart_h, DISP_DIWHSTRT);
921 	ctrl_outl((par->diwstart_v << 16) | par->diwstart_v, DISP_DIWVSTRT);
922 
923 	/* misc. settings */
924 	ctrl_outl((0x16 << 16) | par->is_lowres, DISP_DIWCONF);
925 
926 	/* clock doubler (for VGA), scan doubler, display enable */
927 	ctrl_outl(((video_output == VO_VGA) << 23) |
928 	          (par->is_doublescan << 1) | 1, DISP_DIWMODE);
929 
930 	/* bits per pixel */
931 	ctrl_outl(ctrl_inl(DISP_DIWMODE) | (--bytesperpixel << 2), DISP_DIWMODE);
932 
933 	/* video enable, color sync, interlace,
934 	 * hsync and vsync polarity (currently unused) */
935 	ctrl_outl(0x100 | ((par->is_interlaced /*|4*/) << 4), DISP_SYNCCONF);
936 
937 }
938 
939 /* Simulate blanking by making the border cover the entire screen */
940 
941 #define BLANK_BIT (1<<3)
942 
pvr2_do_blank(void)943 static void pvr2_do_blank(void)
944 {
945 	u_long diwconf;
946 
947 	diwconf = ctrl_inl(DISP_DIWCONF);
948 	if (do_blank > 0)
949 		ctrl_outl(diwconf | BLANK_BIT, DISP_DIWCONF);
950 	else
951 		ctrl_outl(diwconf & ~BLANK_BIT, DISP_DIWCONF);
952 
953 	is_blanked = do_blank > 0 ? do_blank : 0;
954 }
955 
pvr2fb_interrupt(int irq,void * dev_id,struct pt_regs * fp)956 static void pvr2fb_interrupt(int irq, void *dev_id, struct pt_regs *fp)
957 {
958 	if (do_vmode_pan || do_vmode_full)
959 		pvr2_update_display();
960 
961 	if (do_vmode_full)
962 		pvr2_init_display();
963 
964 	if (do_vmode_pan)
965 		do_vmode_pan = 0;
966 
967 	if (do_blank) {
968 		pvr2_do_blank();
969 		do_blank = 0;
970 	}
971 
972 	if (do_vmode_full) {
973 		do_vmode_full = 0;
974 	}
975 }
976 
977 /*
978  * Determine the cable type and initialize the cable output format.  Don't do
979  * anything if the cable type has been overidden (via "cable:XX").
980  */
981 
982 #define PCTRA 0xff80002c
983 #define PDTRA 0xff800030
984 #define VOUTC 0xa0702c00
985 
pvr2_init_cable(void)986 static int pvr2_init_cable(void)
987 {
988 	if (cable_type < 0) {
989 		ctrl_outl((ctrl_inl(PCTRA) & 0xfff0ffff) | 0x000a0000,
990 	                  PCTRA);
991 		cable_type = (ctrl_inw(PDTRA) >> 8) & 3;
992 	}
993 
994 	/* Now select the output format (either composite or other) */
995 	/* XXX: Save the previous val first, as this reg is also AICA
996 	  related */
997 	if (cable_type == CT_COMPOSITE)
998 		ctrl_outl(3 << 8, VOUTC);
999 	else
1000 		ctrl_outl(0, VOUTC);
1001 
1002 	return cable_type;
1003 }
1004 
pvr2fb_init(void)1005 int __init pvr2fb_init(void)
1006 {
1007 	struct fb_var_screeninfo var;
1008 	u_long modememused;
1009 
1010 	if (!MACH_DREAMCAST)
1011 		return -ENXIO;
1012 
1013 	/* Make a guess at the monitor based on the attached cable */
1014 	if (pvr2_init_cable() == CT_VGA) {
1015 		fb_info.monspecs.hfmin = 30000;
1016 		fb_info.monspecs.hfmax = 70000;
1017 		fb_info.monspecs.vfmin = 60;
1018 		fb_info.monspecs.vfmax = 60;
1019 	}
1020 	else { /* Not VGA, using a TV (taken from acornfb) */
1021 		fb_info.monspecs.hfmin = 15469;
1022 		fb_info.monspecs.hfmax = 15781;
1023 		fb_info.monspecs.vfmin = 49;
1024 		fb_info.monspecs.vfmax = 51;
1025 	}
1026 
1027 	/* XXX: This needs to pull default video output via BIOS or other means */
1028 	if (video_output < 0) {
1029 		if (cable_type == CT_VGA)
1030 			video_output = VO_VGA;
1031 		else
1032 			video_output = VO_NTSC;
1033 	}
1034 
1035 	strcpy(fb_info.modename, pvr2fb_name);
1036 	fb_info.changevar = NULL;
1037 	fb_info.node = -1;
1038 	fb_info.fbops = &pvr2fb_ops;
1039 	fb_info.disp = &disp;
1040 	fb_info.switch_con = &pvr2fbcon_switch;
1041 	fb_info.updatevar = &pvr2fbcon_updatevar;
1042 	fb_info.blank = &pvr2fbcon_blank;
1043 	fb_info.flags = FBINFO_FLAG_DEFAULT;
1044 	memset(&var, 0, sizeof(var));
1045 
1046 	if (video_output == VO_VGA)
1047 		defmode = DEFMODE_VGA;
1048 
1049 	if (!fb_find_mode(&var, &fb_info, mode_option, pvr2_modedb,
1050 	                  NUM_TOTAL_MODES, &pvr2_modedb[defmode], 16)) {
1051 		return -EINVAL;
1052 	}
1053 
1054 	if (request_irq(HW_EVENT_VSYNC, pvr2fb_interrupt, 0,
1055 	                "pvr2 VBL handler", &currentpar)) {
1056 		DPRINTK("couldn't register VBL int\n");
1057 		return -EBUSY;
1058 	}
1059 
1060 #ifdef CONFIG_MTRR
1061 	if (enable_mtrr) {
1062 		mtrr_handle = mtrr_add(videomemory, videomemorysize, MTRR_TYPE_WRCOMB, 1);
1063 		printk("pvr2fb: MTRR turned on\n");
1064 	}
1065 #endif
1066 
1067 	pvr2fb_set_var(&var, -1, &fb_info);
1068 
1069 	if (register_framebuffer(&fb_info) < 0)
1070 		return -EINVAL;
1071 
1072 	modememused = get_line_length(var.xres_virtual, var.bits_per_pixel);
1073 	modememused *= var.yres_virtual;
1074 	printk("fb%d: %s frame buffer device, using %ldk/%ldk of video memory\n",
1075 	       GET_FB_IDX(fb_info.node), fb_info.modename, modememused>>10,
1076 	       videomemorysize>>10);
1077 	printk("fb%d: Mode %dx%d-%d pitch = %ld cable: %s video output: %s\n",
1078 	       GET_FB_IDX(fb_info.node), var.xres, var.yres, var.bits_per_pixel,
1079 	       get_line_length(var.xres, var.bits_per_pixel),
1080 	       (char *)pvr2_get_param(cables, NULL, cable_type, 3),
1081 	       (char *)pvr2_get_param(outputs, NULL, video_output, 3));
1082 
1083 	return 0;
1084 }
1085 
pvr2fb_exit(void)1086 static void __exit pvr2fb_exit(void)
1087 {
1088 #ifdef CONFIG_MTRR
1089 	if (enable_mtrr) {
1090 		mtrr_del(mtrr_handle, videomemory, videomemorysize);
1091 		printk("pvr2fb: MTRR turned off\n");
1092 	}
1093 #endif
1094 	unregister_framebuffer(&fb_info);
1095 }
1096 
pvr2_get_param(const struct pvr2_params * p,const char * s,int val,int size)1097 static int __init pvr2_get_param(const struct pvr2_params *p, const char *s,
1098                                    int val, int size)
1099 {
1100 	int i;
1101 
1102 	for (i = 0 ; i < size ; i++ ) {
1103 		if (s != NULL) {
1104 			if (!strnicmp(p[i].name, s, strlen(s)))
1105 				return p[i].val;
1106 		} else {
1107 			if (p[i].val == val)
1108 				return (int)p[i].name;
1109 		}
1110 	}
1111 	return -1;
1112 }
1113 
1114 /*
1115  * Parse command arguments.  Supported arguments are:
1116  *    inverse                             Use inverse color maps
1117  *    nomtrr                              Disable MTRR usage
1118  *    font:<fontname>                     Specify console font
1119  *    cable:composite|rgb|vga             Override the video cable type
1120  *    output:NTSC|PAL|VGA                 Override the video output format
1121  *
1122  *    <xres>x<yres>[-<bpp>][@<refresh>]   or,
1123  *    <name>[-<bpp>][@<refresh>]          Startup using this video mode
1124  */
1125 
1126 #ifndef MODULE
pvr2fb_setup(char * options)1127 int __init pvr2fb_setup(char *options)
1128 {
1129 	char *this_opt;
1130 	char cable_arg[80];
1131 	char output_arg[80];
1132 
1133 	fb_info.fontname[0] = '\0';
1134 
1135 	if (!options || !*options)
1136 		return 0;
1137 
1138 	while ((this_opt = strsep(&options, ","))) {
1139 		if (!*this_opt)
1140 			continue;
1141 		if (!strcmp(this_opt, "inverse")) {
1142 			pvr2fb_inverse = 1;
1143 			fb_invert_cmaps();
1144 		} else if (!strncmp(this_opt, "font:", 5))
1145 			strcpy(fb_info.fontname, this_opt + 5);
1146 		else if (!strncmp(this_opt, "cable:", 6))
1147 			strcpy(cable_arg, this_opt + 6);
1148 		else if (!strncmp(this_opt, "output:", 7))
1149 			strcpy(output_arg, this_opt + 7);
1150 #ifdef CONFIG_MTRR
1151 		else if (!strncmp(this_opt, "nomtrr", 6))
1152 			enable_mtrr = 0;
1153 #endif
1154 		else
1155 			mode_option = this_opt;
1156 	}
1157 
1158 	if (*cable_arg)
1159 		cable_type = pvr2_get_param(cables, cable_arg, 0, 3);
1160 
1161 	if (*output_arg)
1162 		video_output = pvr2_get_param(outputs, output_arg, 0, 3);
1163 
1164 	return 0;
1165 }
1166 #endif
1167 
1168 #ifdef MODULE
1169 MODULE_LICENSE("GPL");
1170 module_init(pvr2fb_init);
1171 #endif
1172 module_exit(pvr2fb_exit);
1173 
1174