File: /usr/src/linux/drivers/video/pvr2fb.c

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, 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     
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     
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     
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->visual = fix.visual;
386     			display->type = fix.type;
387     			display->type_aux = fix.type_aux;
388     			display->ypanstep = fix.ypanstep;
389     			display->ywrapstep = fix.ywrapstep;
390     			display->line_length = fix.line_length;
391     			display->can_soft_blank = 1;
392     			display->inverse = pvr2fb_inverse;
393     			switch (var->bits_per_pixel) {
394     #ifdef FBCON_HAS_CFB16
395     			    case 16:
396     				display->dispsw = &fbcon_cfb16;
397     				display->dispsw_data = fbcon_cmap.cfb16;
398     				break;
399     #endif
400     #ifdef FBCON_HAS_CFB24
401     			    case 24:
402     				display->dispsw = &fbcon_cfb24;
403     				display->dispsw_data = fbcon_cmap.cfb24;
404     				break;
405     #endif
406     #ifdef FBCON_HAS_CFB32
407     			    case 32:
408     				display->dispsw = &fbcon_cfb32;
409     				display->dispsw_data = fbcon_cmap.cfb32;
410     				break;
411     #endif
412     			    default:
413     				display->dispsw = &fbcon_dummy;
414     				break;
415     			}
416     			if (fb_info.changevar)
417     				(*fb_info.changevar)(con);
418     		}
419     		if (oldbpp != var->bits_per_pixel) {
420     			if ((err = fb_alloc_cmap(&display->cmap, 0, 0)))
421     				return err;
422     			do_install_cmap(con, info);
423     		}
424     		if (con == currcon)
425     			pvr2_set_var(&display->var);
426     	}
427     
428     	return 0;
429     }
430     
431     /*
432      * Pan or wrap the display.
433      * This call looks only at xoffset, yoffset and the FB_VMODE_YRAP flag
434      */
435     
436     static int pvr2fb_pan_display(struct fb_var_screeninfo *var, int con,
437                                     struct fb_info *info)
438     {
439     	if (var->vmode & FB_VMODE_YWRAP) {
440     		if (var->yoffset<0 || var->yoffset >=
441     		    fb_display[con].var.yres_virtual || var->xoffset)
442     			return -EINVAL;
443     	 } else {
444     		if (var->xoffset+fb_display[con].var.xres >
445     		    fb_display[con].var.xres_virtual ||
446     		    var->yoffset+fb_display[con].var.yres >
447     		    fb_display[con].var.yres_virtual)
448     		    return -EINVAL;
449     	}
450     	if (con == currcon)
451     		pvr2_pan_var(var);
452     	fb_display[con].var.xoffset = var->xoffset;
453     	fb_display[con].var.yoffset = var->yoffset;
454     	if (var->vmode & FB_VMODE_YWRAP)
455     		fb_display[con].var.vmode |= FB_VMODE_YWRAP;
456     	else
457     		fb_display[con].var.vmode &= ~FB_VMODE_YWRAP;
458     			
459     	return 0;
460     }
461     
462     /* Get the colormap */
463     
464     static int pvr2fb_get_cmap(struct fb_cmap *cmap, int kspc, int con,
465                                  struct fb_info *info)
466     {
467     	if (con == currcon) /* current console? */
468     		return fb_get_cmap(cmap, kspc, pvr2_getcolreg, info);
469     	else if (fb_display[con].cmap.len) /* non default colormap? */
470     		fb_copy_cmap(&fb_display[con].cmap, cmap, kspc ? 0 : 2);
471     	else
472     		fb_copy_cmap(fb_default_cmap(1<<fb_display[con].var.bits_per_pixel),
473     		             cmap, kspc ? 0 : 2);
474     	return 0;
475     }
476     
477     /* Set the colormap */
478     
479     static int pvr2fb_set_cmap(struct fb_cmap *cmap, int kspc, int con,
480     	                     struct fb_info *info)
481     {
482     	int err;
483     
484     	if (!fb_display[con].cmap.len) {        /* no colormap allocated? */
485     		if ((err = fb_alloc_cmap(&fb_display[con].cmap,
486     		                         1<<fb_display[con].var.bits_per_pixel,
487     					 0)))
488     			 return err;
489     	}
490     	if (con == currcon)                     /* current console? */
491     		return fb_set_cmap(cmap, kspc, pvr2_setcolreg, info);
492     	else
493     		fb_copy_cmap(cmap, &fb_display[con].cmap, kspc ? 0 : 1);
494     
495     	return 0;
496     }
497     
498     static int pvr2fbcon_switch(int con, struct fb_info *info)
499     {
500     	/* Do we have to save the colormap? */
501     	if (fb_display[currcon].cmap.len)
502     		fb_get_cmap(&fb_display[currcon].cmap, 1, pvr2_getcolreg, info);
503     
504     	currcon = con;
505     	pvr2_set_var(&fb_display[con].var);
506     	/* Install new colormap */
507     	do_install_cmap(con, info);
508     	return 0;
509     }
510     
511     static int pvr2fbcon_updatevar(int con, struct fb_info *info)
512     {
513     	pvr2_pan_var(&fb_display[con].var);
514     	return 0;
515     }
516     
517     static void pvr2fbcon_blank(int blank, struct fb_info *info)
518     {
519     	do_blank = blank ? blank : -1;
520     }
521     
522     /* Setup the colormap */
523     
524     static void do_install_cmap(int con, struct fb_info *info)
525     {
526     	if (con != currcon)
527     		return;
528     	if (fb_display[con].cmap.len)
529     		fb_set_cmap(&fb_display[con].cmap, 1, pvr2_setcolreg, info);
530     	else
531     		fb_set_cmap(fb_default_cmap(1<<fb_display[con].var.bits_per_pixel),
532                                 1, pvr2_setcolreg, info);
533     }
534     
535     static inline u_long get_line_length(int xres_virtual, int bpp)
536     {
537     	return (u_long)((((xres_virtual*bpp)+31)&~31) >> 3);
538     }
539     
540     static void set_color_bitfields(struct fb_var_screeninfo *var)
541     {
542     	switch (var->bits_per_pixel) {
543     	    case 16:        /* RGB 565 */
544     		var->red.offset = 11;    var->red.length = 5;
545     		var->green.offset = 5;   var->green.length = 6;
546     		var->blue.offset = 0;    var->blue.length = 5;
547     		var->transp.offset = 0;  var->transp.length = 0;
548     		break;
549     	    case 24:        /* RGB 888 */
550     		var->red.offset = 16;    var->red.length = 8;
551     		var->green.offset = 8;   var->green.length = 8;
552     		var->blue.offset = 0;    var->blue.length = 8;
553     		var->transp.offset = 0;  var->transp.length = 0;
554     		break;
555     	    case 32:        /* ARGB 8888 */
556     		var->red.offset = 16;    var->red.length = 8;
557     		var->green.offset = 8;   var->green.length = 8;
558     		var->blue.offset = 0;    var->blue.length = 8;
559     		var->transp.offset = 24; var->transp.length = 8;
560     		break;
561     	}
562     }
563     
564     static int pvr2_getcolreg(u_int regno, u_int *red, u_int *green, u_int *blue,
565                                 u_int *transp, struct fb_info *info)
566     {
567     	if (regno > 255)
568     	    return 1;
569     	
570     	*red = palette[regno].red;
571     	*green = palette[regno].green;
572     	*blue = palette[regno].blue;
573     	*transp = 0;
574     	return 0;
575     }
576     	
577     static int pvr2_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
578                                 u_int transp, struct fb_info *info)
579     {
580     	if (regno > 255)
581     		return 1;
582     
583     	palette[regno].red = red;
584     	palette[regno].green = green;
585     	palette[regno].blue = blue;
586     
587     	if (regno < 16) {
588     		switch (currbpp) {
589     #ifdef FBCON_HAS_CFB16
590     		    case 16: /* RGB 565 */
591     			fbcon_cmap.cfb16[regno] = (red & 0xf800) |
592     			                          ((green & 0xf800) >> 6) |
593     						  ((blue & 0xf800) >> 11);
594     			break;
595     #endif
596     #ifdef FBCON_HAS_CFB24
597     		    case 24: /* RGB 888 */
598     			red >>= 8; green >>= 8; blue >>= 8;
599     			fbcon_cmap.cfb24[regno] = (red << 16) | (green << 8) | blue;
600     			break;
601     #endif
602     #ifdef FBCON_HAS_CFB32
603     		    case 32: /* ARGB 8888 */
604     			red >>= 8; green >>= 8; blue >>= 8;
605     			fbcon_cmap.cfb32[regno] = (red << 16) | (green << 8) | blue;
606     			break;
607     #endif
608     		    default:
609     			DPRINTK("Invalid bit depth %d?!?\n", currbpp);
610     			return 1;
611     		}
612     	}
613     
614     	return 0;
615     }
616     
617     
618     static int pvr2_encode_fix(struct fb_fix_screeninfo *fix,
619                                  struct pvr2fb_par *par)
620     {
621     	memset(fix, 0, sizeof(struct fb_fix_screeninfo));
622     	strcpy(fix->id, pvr2fb_name);
623     	fix->smem_start = videomemory;
624     	fix->smem_len = videomemorysize;
625     	fix->type = FB_TYPE_PACKED_PIXELS;
626     	fix->type_aux = 0;
627     	fix->visual = FB_VISUAL_TRUECOLOR;
628     
629     	if (par->vmode & FB_VMODE_YWRAP) {
630     		fix->ywrapstep = 1;
631     		fix->xpanstep = fix->ypanstep = 0;
632     	} else {
633     		fix->ywrapstep = 0;
634     		fix->xpanstep = 1;
635     		fix->ypanstep = 1;
636     	}
637     	fix->line_length = par->next_line;
638     
639     	return 0;
640     }
641     
642     /*
643      * Create a hardware video mode using the framebuffer values.  If a value needs
644      * to be clipped or constrained it's done here.  This routine needs a bit more
645      * work to make sure we're doing the right tests at the right time.
646      */
647     static int pvr2_decode_var(struct fb_var_screeninfo *var,
648                                  struct pvr2fb_par *par)
649     {
650     	u_long line_length;
651     	u_short vtotal;
652     
653     	if (var->pixclock != TV_CLK && var->pixclock != VGA_CLK) {
654     		DPRINTK("Invalid pixclock value %d\n", var->pixclock);
655     		return -EINVAL;
656     	}
657     	par->pixclock = var->pixclock;
658     	
659     	if ((par->xres = var->xres) < 320)
660     		par->xres = 320;
661     	if ((par->yres = var->yres) < 240)
662     		par->yres = 240;
663     	if ((par->vxres = var->xres_virtual) < par->xres)
664     		par->vxres = par->xres;
665     	if ((par->vyres = var->yres_virtual) < par->yres)
666     		par->vyres = par->yres;
667     
668     	if ((par->bpp = var->bits_per_pixel) <= 16)
669     		par->bpp = 16;
670     	else if ((par->bpp = var->bits_per_pixel) <= 24)
671     		par->bpp = 24;
672     	else if ((par->bpp = var->bits_per_pixel) <= 32)
673     		par->bpp = 32;
674     
675     	currbpp = par->bpp;
676     
677     	/*
678     	 * XXX: It's possible that a user could use a VGA box, change the cable
679     	 * type in hardware (i.e. switch from VGA<->composite), then change modes
680     	 * (i.e. switching to another VT).  If that happens we should automagically
681     	 * change the output format to cope, but currently I don't have a VGA box
682     	 * to make sure this works properly.
683     	 */
684     	cable_type = pvr2_init_cable();
685     	if (cable_type == CT_VGA && video_output != VO_VGA)
686     		video_output = VO_VGA;
687     
688     	par->vmode = var->vmode & FB_VMODE_MASK;
689     	if (par->vmode & FB_VMODE_INTERLACED && video_output != VO_VGA)
690     		par->is_interlaced = 1;
691     	/* 
692     	 * XXX: Need to be more creative with this (i.e. allow doublecan for
693     	 * PAL/NTSC output).
694     	 */
695     	par->is_doublescan = (par->yres < 480 && video_output == VO_VGA);
696     	
697     	par->hsync_total = var->left_margin + var->xres + var->right_margin +
698     	                   var->hsync_len;
699     	par->vsync_total = var->upper_margin + var->yres + var->lower_margin +
700     	                   var->vsync_len;
701     
702     	if (var->sync & FB_SYNC_BROADCAST) {
703     		vtotal = par->vsync_total;
704     		if (par->is_interlaced)
705     			vtotal /= 2;
706     		if (vtotal > (PAL_VTOTAL + NTSC_VTOTAL)/2) {
707     			/* PAL video output */
708     			/* XXX: Should be using a range here ... ? */
709     			if (par->hsync_total != PAL_HTOTAL) {
710     				DPRINTK("invalid hsync total for PAL\n");
711     				return -EINVAL;
712     			}
713     			/* XXX: Check for start values here... */
714     			/* XXX: Check hardware for PAL-compatibility */
715     			par->borderstart_h = 116;
716     			par->borderstart_v = 44;
717     		} else {
718     			/* NTSC video output */
719     			if (par->hsync_total != NTSC_HTOTAL) {
720     				DPRINTK("invalid hsync total for NTSC\n");
721     				return -EINVAL;
722     			}
723     			par->borderstart_h = 126;
724     			par->borderstart_v = 18;
725     		}
726     	} else {
727     		/* VGA mode */
728     		/* XXX: What else needs to be checked? */
729     		/* 
730     		 * XXX: We have a little freedom in VGA modes, what ranges should
731     		 * be here (i.e. hsync/vsync totals, etc.)?
732     		 */
733     		par->borderstart_h = 126;
734     		par->borderstart_v = 40;
735     	}
736     
737     	/* Calculate the remainding offsets */
738     	par->borderstop_h = par->borderstart_h + par->hsync_total -
739     	                    var->hsync_len;
740     	par->borderstop_v = par->borderstart_v + par->vsync_total -
741     	                    var->vsync_len;
742     	par->diwstart_h = par->borderstart_h + var->left_margin;
743     	par->diwstart_v = par->borderstart_v + var->upper_margin;
744     	if (!par->is_interlaced)
745     		par->borderstop_v /= 2;
746     
747     	if (par->xres < 640)
748     		par->is_lowres = 1;
749     
750     	/* XXX: Needs testing. */
751     	if (!((par->vmode ^ var->vmode) & FB_VMODE_YWRAP)) {
752     		par->xoffset = var->xoffset;
753     		par->yoffset = var->yoffset;
754     		if (par->vmode & FB_VMODE_YWRAP) {
755     			if (par->xoffset || par->yoffset < 0 || par->yoffset >=
756     			    par->vyres)
757     				par->xoffset = par->yoffset = 0;
758     		} else {
759     			if (par->xoffset < 0 || par->xoffset > par->vxres-par->xres ||
760     			    par->yoffset < 0 || par->yoffset > par->vyres-par->yres)
761     				par->xoffset = par->yoffset = 0;
762     		}
763     	} else
764     		par->xoffset = par->yoffset = 0;
765     
766     	/* Check memory sizes */
767     	line_length = get_line_length(var->xres_virtual, var->bits_per_pixel);
768     	if (line_length * var->yres_virtual > videomemorysize)
769     		return -ENOMEM;
770     	par->disp_start = videomemory + (get_line_length(par->vxres, par->bpp) *
771     	                  par->yoffset) * get_line_length(par->xoffset, par->bpp);
772     	par->next_line = line_length;
773     	
774     	return 0;
775     }
776     
777     static int pvr2_encode_var(struct fb_var_screeninfo *var,
778                                  struct pvr2fb_par *par)
779     {
780     	memset(var, 0, sizeof(struct fb_var_screeninfo));
781     
782     	var->xres = par->xres;
783     	var->yres = par->yres;
784     	var->xres_virtual = par->vxres;
785     	var->yres_virtual = par->vyres;
786     	var->xoffset = par->xoffset;
787     	var->yoffset = par->yoffset;
788     
789     	var->bits_per_pixel = par->bpp;
790     	set_color_bitfields(var);
791     
792     	var->activate = FB_ACTIVATE_NOW;
793     	var->height = -1;
794     	var->width = -1;
795     
796     	var->pixclock = par->pixclock;
797     
798     	if (par->is_doublescan)
799     		var->vmode = FB_VMODE_DOUBLE;
800     
801     	if (par->is_interlaced)
802     		var->vmode |= FB_VMODE_INTERLACED;
803     	else
804     		var->vmode |= FB_VMODE_NONINTERLACED;
805     
806     	var->right_margin = par->borderstop_h - (par->diwstart_h + par->xres);
807     	var->left_margin = par->diwstart_h - par->borderstart_h;
808     	var->hsync_len = par->borderstart_h + (par->hsync_total - par->borderstop_h);
809     	var->upper_margin = par->diwstart_v - par->borderstart_v;
810     	var->lower_margin = par->borderstop_v - (par->diwstart_v + par->yres);
811     	var->vsync_len = par->borderstart_v + (par->vsync_total - par->borderstop_v);
812     	if (video_output != VO_VGA)
813     		var->sync = FB_SYNC_BROADCAST;
814     
815     	if (par->vmode & FB_VMODE_YWRAP)
816     		var->vmode |= FB_VMODE_YWRAP;
817     	
818     	return 0;
819     }
820     
821     static void pvr2_get_par(struct pvr2fb_par *par)
822     {
823     	*par = currentpar;
824     }
825     
826     /* Setup the new videomode in hardware */
827     
828     static void pvr2_set_var(struct fb_var_screeninfo *var)
829     {
830     	do_vmode_pan = 0;
831     	do_vmode_full = 0;
832     	pvr2_decode_var(var, &currentpar);
833     
834     	do_vmode_full = 1;
835     }
836     
837     /* 
838      * Pan or wrap the display
839      * This call looks only at xoffset, yoffset and the FB_VMODE_YWRAP flag in `var'.
840      */
841     static void pvr2_pan_var(struct fb_var_screeninfo *var)
842     {
843     	struct pvr2fb_par *par = &currentpar;
844     
845     	par->xoffset = var->xoffset;
846     	par->yoffset = var->yoffset;
847     	if (var->vmode & FB_VMODE_YWRAP)
848     		par->vmode |= FB_VMODE_YWRAP;
849     	else
850     		par->vmode &= ~FB_VMODE_YWRAP;
851     
852     	do_vmode_pan = 0;
853     	pvr2_update_par();
854     	do_vmode_pan = 1;
855     }
856     
857     static int pvr2_update_par(void)
858     {
859     	struct pvr2fb_par *par = &currentpar;
860     	u_long move;
861     
862     	move = get_line_length(par->xoffset, par->bpp);
863     	if (par->yoffset) {
864     		par->disp_start += (par->next_line * par->yoffset) + move;
865     	} else
866     		par->disp_start += move;
867     
868     	return 0;
869     }
870     
871     static void pvr2_update_display(void)
872     {
873     	struct pvr2fb_par *par = &currentpar;
874     
875     	/* Update the start address of the display image */
876     	ctrl_outl(par->disp_start, DISP_DIWADDRL);
877     	ctrl_outl(par->disp_start +
878     		  get_line_length(par->xoffset + par->xres, par->bpp),
879     	          DISP_DIWADDRS);
880     }
881     
882     /* 
883      * Initialize the video mode.  Currently, the 16bpp and 24bpp modes aren't
884      * very stable.  It's probably due to the fact that a lot of the 2D video
885      * registers are still undocumented.
886      */
887     
888     static void pvr2_init_display(void)
889     {
890     	struct pvr2fb_par *par = &currentpar;
891     	u_short diw_height, diw_width, diw_modulo = 1;
892     	u_short bytesperpixel = par->bpp / 8;
893     
894     	/* hsync and vsync totals */
895     	ctrl_outl((par->vsync_total << 16) | par->hsync_total, DISP_SYNCSIZE);
896     
897     	/* column height, modulo, row width */
898     	/* since we're "panning" within vram, we need to offset things based
899     	 * on the offset from the virtual x start to our real gfx. */
900     	if (video_output != VO_VGA && par->is_interlaced)
901     		diw_modulo += par->next_line / 4;
902     	diw_height = (par->is_interlaced ? par->yres / 2 : par->yres);
903     	diw_width = get_line_length(par->xres, par->bpp) / 4;
904     	ctrl_outl((diw_modulo << 20) | (--diw_height << 10) | --diw_width,
905     	          DISP_DIWSIZE);
906     
907     	/* display address, long and short fields */
908     	ctrl_outl(par->disp_start, DISP_DIWADDRL);
909     	ctrl_outl(par->disp_start +
910     	          get_line_length(par->xoffset + par->xres, par->bpp),
911     	          DISP_DIWADDRS);
912     
913     	/* border horizontal, border vertical, border color */
914     	ctrl_outl((par->borderstart_h << 16) | par->borderstop_h, DISP_BRDRHORZ);
915     	ctrl_outl((par->borderstart_v << 16) | par->borderstop_v, DISP_BRDRVERT);
916     	ctrl_outl(0, DISP_BRDRCOLR);
917     
918     	/* display window start position */
919     	ctrl_outl(par->diwstart_h, DISP_DIWHSTRT);
920     	ctrl_outl((par->diwstart_v << 16) | par->diwstart_v, DISP_DIWVSTRT);
921     	
922     	/* misc. settings */
923     	ctrl_outl((0x16 << 16) | par->is_lowres, DISP_DIWCONF);
924     
925     	/* clock doubler (for VGA), scan doubler, display enable */
926     	ctrl_outl(((video_output == VO_VGA) << 23) | 
927     	          (par->is_doublescan << 1) | 1, DISP_DIWMODE);
928     
929     	/* bits per pixel */
930     	ctrl_outl(ctrl_inl(DISP_DIWMODE) | (--bytesperpixel << 2), DISP_DIWMODE);
931     
932     	/* video enable, color sync, interlace, 
933     	 * hsync and vsync polarity (currently unused) */
934     	ctrl_outl(0x100 | ((par->is_interlaced /*|4*/) << 4), DISP_SYNCCONF);
935     
936     }
937     
938     /* Simulate blanking by making the border cover the entire screen */
939     
940     #define BLANK_BIT (1<<3)
941     
942     static void pvr2_do_blank(void)
943     {
944     	u_long diwconf;
945     
946     	diwconf = ctrl_inl(DISP_DIWCONF);
947     	if (do_blank > 0)
948     		ctrl_outl(diwconf | BLANK_BIT, DISP_DIWCONF);
949     	else
950     		ctrl_outl(diwconf & ~BLANK_BIT, DISP_DIWCONF);
951     
952     	is_blanked = do_blank > 0 ? do_blank : 0;
953     }
954     
955     static void pvr2fb_interrupt(int irq, void *dev_id, struct pt_regs *fp)
956     {
957     	if (do_vmode_pan || do_vmode_full)
958     		pvr2_update_display();
959     
960     	if (do_vmode_full)
961     		pvr2_init_display();
962     
963     	if (do_vmode_pan)
964     		do_vmode_pan = 0;
965     
966     	if (do_blank) {
967     		pvr2_do_blank();
968     		do_blank = 0;
969     	}
970     
971     	if (do_vmode_full) {
972     		do_vmode_full = 0;
973     	}
974     }
975     
976     /*
977      * Determine the cable type and initialize the cable output format.  Don't do
978      * anything if the cable type has been overidden (via "cable:XX").
979      */
980     
981     #define PCTRA 0xff80002c
982     #define PDTRA 0xff800030
983     #define VOUTC 0xa0702c00
984     
985     static int pvr2_init_cable(void)
986     {
987     	if (cable_type < 0) {
988     		ctrl_outl((ctrl_inl(PCTRA) & 0xfff0ffff) | 0x000a0000, 
989     	                  PCTRA);
990     		cable_type = (ctrl_inw(PDTRA) >> 8) & 3;
991     	}
992     
993     	/* Now select the output format (either composite or other) */
994     	/* XXX: Save the previous val first, as this reg is also AICA
995     	  related */
996     	if (cable_type == CT_COMPOSITE)
997     		ctrl_outl(3 << 8, VOUTC);
998     	else
999     		ctrl_outl(0, VOUTC);
1000     
1001     	return cable_type;
1002     }
1003     
1004     int __init pvr2fb_init(void)
1005     {
1006     	struct fb_var_screeninfo var;
1007     	u_long modememused;
1008     
1009     	if (!MACH_DREAMCAST)
1010     		return -ENXIO;
1011     
1012     	/* Make a guess at the monitor based on the attached cable */
1013     	if (pvr2_init_cable() == CT_VGA) {
1014     		fb_info.monspecs.hfmin = 30000;
1015     		fb_info.monspecs.hfmax = 70000;
1016     		fb_info.monspecs.vfmin = 60;
1017     		fb_info.monspecs.vfmax = 60;
1018     	}
1019     	else { /* Not VGA, using a TV (taken from acornfb) */
1020     		fb_info.monspecs.hfmin = 15469;
1021     		fb_info.monspecs.hfmax = 15781;
1022     		fb_info.monspecs.vfmin = 49;
1023     		fb_info.monspecs.vfmax = 51;
1024     	}
1025     
1026     	/* XXX: This needs to pull default video output via BIOS or other means */
1027     	if (video_output < 0) {
1028     		if (cable_type == CT_VGA)
1029     			video_output = VO_VGA;
1030     		else
1031     			video_output = VO_NTSC;
1032     	}
1033     	
1034     	strcpy(fb_info.modename, pvr2fb_name);
1035     	fb_info.changevar = NULL;
1036     	fb_info.node = -1;
1037     	fb_info.fbops = &pvr2fb_ops;
1038     	fb_info.disp = &disp;
1039     	fb_info.switch_con = &pvr2fbcon_switch;
1040     	fb_info.updatevar = &pvr2fbcon_updatevar;
1041     	fb_info.blank = &pvr2fbcon_blank;
1042     	fb_info.flags = FBINFO_FLAG_DEFAULT;
1043     	memset(&var, 0, sizeof(var));
1044     
1045     	if (video_output == VO_VGA)
1046     		defmode = DEFMODE_VGA;
1047     
1048     	if (!fb_find_mode(&var, &fb_info, mode_option, pvr2_modedb,
1049     	                  NUM_TOTAL_MODES, &pvr2_modedb[defmode], 16)) {
1050     		return -EINVAL;
1051     	}
1052     
1053     	if (request_irq(HW_EVENT_VSYNC, pvr2fb_interrupt, 0,
1054     	                "pvr2 VBL handler", &currentpar)) {
1055     		DPRINTK("couldn't register VBL int\n");
1056     		return -EBUSY;
1057     	}
1058     
1059     #ifdef CONFIG_MTRR
1060     	if (enable_mtrr) {
1061     		mtrr_handle = mtrr_add(videomemory, videomemorysize, MTRR_TYPE_WRCOMB, 1);
1062     		printk("pvr2fb: MTRR turned on\n");
1063     	}
1064     #endif
1065     
1066     	pvr2fb_set_var(&var, -1, &fb_info);
1067     
1068     	if (register_framebuffer(&fb_info) < 0)
1069     		return -EINVAL;
1070     
1071     	modememused = get_line_length(var.xres_virtual, var.bits_per_pixel);
1072     	modememused *= var.yres_virtual;
1073     	printk("fb%d: %s frame buffer device, using %ldk/%ldk of video memory\n",
1074     	       GET_FB_IDX(fb_info.node), fb_info.modename, modememused>>10,
1075     	       videomemorysize>>10);
1076     	printk("fb%d: Mode %dx%d-%d pitch = %ld cable: %s video output: %s\n", 
1077     	       GET_FB_IDX(fb_info.node), var.xres, var.yres, var.bits_per_pixel, 
1078     	       get_line_length(var.xres, var.bits_per_pixel),
1079     	       (char *)pvr2_get_param(cables, NULL, cable_type, 6),
1080     	       (char *)pvr2_get_param(outputs, NULL, video_output, 6));
1081     
1082     	return 0;
1083     }
1084     
1085     static void __exit pvr2fb_exit(void)
1086     {
1087     #ifdef CONFIG_MTRR
1088     	if (enable_mtrr) {
1089     		mtrr_del(mtrr_handle, videomemory, videomemorysize);
1090     		printk("pvr2fb: MTRR turned off\n");
1091     	}
1092     #endif
1093     	unregister_framebuffer(&fb_info);
1094     }
1095     
1096     static int __init pvr2_get_param(const struct pvr2_params *p, const char *s,
1097                                        int val, int size)
1098     {
1099     	int i;
1100     
1101     	for (i = 0 ; i < size ; i++ ) {
1102     		if (s != NULL) {
1103     			if (!strnicmp(p[i].name, s, strlen(s)))
1104     				return p[i].val;
1105     		} else {
1106     			if (p[i].val == val)
1107     				return (int)p[i].name;
1108     		}
1109     	}
1110     	return -1;
1111     }
1112     
1113     /*
1114      * Parse command arguments.  Supported arguments are:
1115      *    inverse                             Use inverse color maps
1116      *    nomtrr                              Disable MTRR usage
1117      *    font:<fontname>                     Specify console font
1118      *    cable:composite|rgb|vga             Override the video cable type
1119      *    output:NTSC|PAL|VGA                 Override the video output format
1120      *
1121      *    <xres>x<yres>[-<bpp>][@<refresh>]   or,
1122      *    <name>[-<bpp>][@<refresh>]          Startup using this video mode
1123      */
1124     
1125     #ifndef MODULE
1126     int __init pvr2fb_setup(char *options)
1127     {
1128     	char *this_opt;
1129     	char cable_arg[80];
1130     	char output_arg[80];
1131     
1132     	fb_info.fontname[0] = '\0';
1133     
1134     	if (!options || !*options)
1135     		return 0;
1136     
1137     	while ((this_opt = strsep(&options, ","))) {
1138     		if (!*this_opt)
1139     			continue;
1140     		if (!strcmp(this_opt, "inverse")) {
1141     			pvr2fb_inverse = 1;
1142     			fb_invert_cmaps();
1143     		} else if (!strncmp(this_opt, "font:", 5))
1144     			strcpy(fb_info.fontname, this_opt + 5);
1145     		else if (!strncmp(this_opt, "cable:", 6))
1146     			strcpy(cable_arg, this_opt + 6);
1147     		else if (!strncmp(this_opt, "output:", 7))
1148     			strcpy(output_arg, this_opt + 7);
1149     #ifdef CONFIG_MTRR
1150     		else if (!strncmp(this_opt, "nomtrr", 6))
1151     			enable_mtrr = 0;
1152     #endif
1153     		else
1154     			mode_option = this_opt;
1155     	}
1156     
1157     	if (*cable_arg)
1158     		cable_type = pvr2_get_param(cables, cable_arg, 0, 6);
1159     
1160     	if (*output_arg)
1161     		video_output = pvr2_get_param(outputs, output_arg, 0, 6);
1162     
1163     	return 0;
1164     }
1165     #endif
1166     
1167     #ifdef MODULE
1168     MODULE_LICENSE("GPL");
1169     module_init(pvr2fb_init);
1170     #endif
1171     module_exit(pvr2fb_exit);
1172     
1173