1 /* drivers/video/pvr2fb.c
3 * Frame buffer and fbcon support for the NEC PowerVR2 found within the Sega
6 * Copyright (c) 2001 M. R. Brown <mrbrown@0xd6.org>
7 * Copyright (c) 2001, 2002, 2003, 2004, 2005 Paul Mundt <lethal@linux-sh.org>
9 * This file is part of the LinuxDC project (linuxdc.sourceforge.net).
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,
16 * here are some hacked-up formulas:
18 * The Dreamcast has screen offsets from each side of its four borders and
19 * the start offsets of the display window. I used these values to calculate
20 * 'pseudo' values (think of them as placeholders) for the fb video mode, so
21 * that when it came time to convert these values back into their hardware
22 * values, I could just add mode- specific offsets to get the correct mode
25 * left_margin = diwstart_h - borderstart_h;
26 * right_margin = borderstop_h - (diwstart_h + xres);
27 * upper_margin = diwstart_v - borderstart_v;
28 * lower_margin = borderstop_v - (diwstart_h + yres);
30 * hsync_len = borderstart_h + (hsync_total - borderstop_h);
31 * vsync_len = borderstart_v + (vsync_total - borderstop_v);
33 * Then, when it's time to convert back to hardware settings, the only
34 * constants are the borderstart_* offsets, all other values are derived from
38 * borderstart_h = 116;
41 * borderstop_h = borderstart_h + hsync_total - hsync_len;
43 * diwstart_v = borderstart_v - upper_margin;
45 * However, in the current implementation, the borderstart values haven't had
46 * the benefit of being fully researched, so some modes may be broken.
51 #include <linux/module.h>
52 #include <linux/kernel.h>
53 #include <linux/errno.h>
54 #include <linux/string.h>
56 #include <linux/tty.h>
57 #include <linux/slab.h>
58 #include <linux/delay.h>
59 #include <linux/config.h>
60 #include <linux/interrupt.h>
62 #include <linux/init.h>
63 #include <linux/pci.h>
65 #ifdef CONFIG_SH_DREAMCAST
66 #include <asm/machvec.h>
67 #include <asm/mach/sysasic.h>
71 #include <linux/pagemap.h>
72 #include <asm/mach/dma.h>
76 #ifdef CONFIG_SH_STORE_QUEUES
77 #include <asm/uaccess.h>
78 #include <asm/cpu/sq.h>
81 #ifndef PCI_DEVICE_ID_NEC_NEON250
82 # define PCI_DEVICE_ID_NEC_NEON250 0x0067
85 /* 2D video registers */
86 #define DISP_BASE par->mmio_base
87 #define DISP_BRDRCOLR (DISP_BASE + 0x40)
88 #define DISP_DIWMODE (DISP_BASE + 0x44)
89 #define DISP_DIWADDRL (DISP_BASE + 0x50)
90 #define DISP_DIWADDRS (DISP_BASE + 0x54)
91 #define DISP_DIWSIZE (DISP_BASE + 0x5c)
92 #define DISP_SYNCCONF (DISP_BASE + 0xd0)
93 #define DISP_BRDRHORZ (DISP_BASE + 0xd4)
94 #define DISP_SYNCSIZE (DISP_BASE + 0xd8)
95 #define DISP_BRDRVERT (DISP_BASE + 0xdc)
96 #define DISP_DIWCONF (DISP_BASE + 0xe8)
97 #define DISP_DIWHSTRT (DISP_BASE + 0xec)
98 #define DISP_DIWVSTRT (DISP_BASE + 0xf0)
100 /* Pixel clocks, one for TV output, doubled for VGA output */
102 #define VGA_CLK 37119
104 /* This is for 60Hz - the VTOTAL is doubled for interlaced modes */
105 #define PAL_HTOTAL 863
106 #define PAL_VTOTAL 312
107 #define NTSC_HTOTAL 857
108 #define NTSC_VTOTAL 262
110 /* Supported cable types */
111 enum { CT_VGA
, CT_NONE
, CT_RGB
, CT_COMPOSITE
};
113 /* Supported video output types */
114 enum { VO_PAL
, VO_NTSC
, VO_VGA
};
116 /* Supported palette types */
117 enum { PAL_ARGB1555
, PAL_RGB565
, PAL_ARGB4444
, PAL_ARGB8888
};
119 struct pvr2_params
{ unsigned int val
; char *name
; };
120 static struct pvr2_params cables
[] __initdata
= {
121 { CT_VGA
, "VGA" }, { CT_RGB
, "RGB" }, { CT_COMPOSITE
, "COMPOSITE" },
124 static struct pvr2_params outputs
[] __initdata
= {
125 { VO_PAL
, "PAL" }, { VO_NTSC
, "NTSC" }, { VO_VGA
, "VGA" },
129 * This describes the current video mode
132 static struct pvr2fb_par
{
133 unsigned int hsync_total
; /* Clocks/line */
134 unsigned int vsync_total
; /* Lines/field */
135 unsigned int borderstart_h
;
136 unsigned int borderstop_h
;
137 unsigned int borderstart_v
;
138 unsigned int borderstop_v
;
139 unsigned int diwstart_h
; /* Horizontal offset of the display field */
140 unsigned int diwstart_v
; /* Vertical offset of the display field, for
141 interlaced modes, this is the long field */
142 unsigned long disp_start
; /* Address of image within VRAM */
143 unsigned char is_interlaced
; /* Is the display interlaced? */
144 unsigned char is_doublescan
; /* Are scanlines output twice? (doublescan) */
145 unsigned char is_lowres
; /* Is horizontal pixel-doubling enabled? */
147 unsigned long mmio_base
; /* MMIO base */
150 static struct fb_info
*fb_info
;
152 static struct fb_fix_screeninfo pvr2_fix __initdata
= {
153 .id
= "NEC PowerVR2",
154 .type
= FB_TYPE_PACKED_PIXELS
,
155 .visual
= FB_VISUAL_TRUECOLOR
,
158 .accel
= FB_ACCEL_NONE
,
161 static struct fb_var_screeninfo pvr2_var __initdata
= {
168 .green
= { 5, 6, 0 },
170 .activate
= FB_ACTIVATE_NOW
,
173 .vmode
= FB_VMODE_NONINTERLACED
,
176 static int cable_type
= CT_VGA
;
177 static int video_output
= VO_VGA
;
179 static int nopan
= 0;
180 static int nowrap
= 1;
183 * We do all updating, blanking, etc. during the vertical retrace period
185 static unsigned int do_vmode_full
= 0; /* Change the video mode */
186 static unsigned int do_vmode_pan
= 0; /* Update the video mode */
187 static short do_blank
= 0; /* (Un)Blank the screen */
189 static unsigned int is_blanked
= 0; /* Is the screen blanked? */
191 #ifdef CONFIG_SH_STORE_QUEUES
192 static struct sq_mapping
*pvr2fb_map
;
196 static unsigned int shdma
= PVR2_CASCADE_CHAN
;
197 static unsigned int pvr2dma
= ONCHIP_NR_DMA_CHANNELS
;
200 /* Interface used by the world */
202 int pvr2fb_setup(char*);
204 static int pvr2fb_setcolreg(unsigned int regno
, unsigned int red
, unsigned int green
, unsigned int blue
,
205 unsigned int transp
, struct fb_info
*info
);
206 static int pvr2fb_blank(int blank
, struct fb_info
*info
);
207 static unsigned long get_line_length(int xres_virtual
, int bpp
);
208 static void set_color_bitfields(struct fb_var_screeninfo
*var
);
209 static int pvr2fb_check_var(struct fb_var_screeninfo
*var
, struct fb_info
*info
);
210 static int pvr2fb_set_par(struct fb_info
*info
);
211 static void pvr2_update_display(struct fb_info
*info
);
212 static void pvr2_init_display(struct fb_info
*info
);
213 static void pvr2_do_blank(void);
214 static irqreturn_t
pvr2fb_interrupt(int irq
, void *dev_id
, struct pt_regs
*fp
);
215 static int pvr2_init_cable(void);
216 static int pvr2_get_param(const struct pvr2_params
*p
, const char *s
,
218 static ssize_t
pvr2fb_write(struct file
*file
, const char *buf
,
219 size_t count
, loff_t
*ppos
);
221 static struct fb_ops pvr2fb_ops
= {
222 .owner
= THIS_MODULE
,
223 .fb_setcolreg
= pvr2fb_setcolreg
,
224 .fb_blank
= pvr2fb_blank
,
225 .fb_check_var
= pvr2fb_check_var
,
226 .fb_set_par
= pvr2fb_set_par
,
228 .fb_write
= pvr2fb_write
,
230 .fb_fillrect
= cfb_fillrect
,
231 .fb_copyarea
= cfb_copyarea
,
232 .fb_imageblit
= cfb_imageblit
,
235 static struct fb_videomode pvr2_modedb
[] __initdata
= {
237 * Broadcast video modes (PAL and NTSC). I'm unfamiliar with
238 * PAL-M and PAL-N, but from what I've read both modes parallel PAL and
239 * NTSC, so it shouldn't be a problem (I hope).
243 /* 640x480 @ 60Hz interlaced (NTSC) */
244 "ntsc_640x480i", 60, 640, 480, TV_CLK
, 38, 33, 0, 18, 146, 26,
245 FB_SYNC_BROADCAST
, FB_VMODE_INTERLACED
| FB_VMODE_YWRAP
247 /* 640x240 @ 60Hz (NTSC) */
248 /* XXX: Broken! Don't use... */
249 "ntsc_640x240", 60, 640, 240, TV_CLK
, 38, 33, 0, 0, 146, 22,
250 FB_SYNC_BROADCAST
, FB_VMODE_YWRAP
252 /* 640x480 @ 60hz (VGA) */
253 "vga_640x480", 60, 640, 480, VGA_CLK
, 38, 33, 0, 18, 146, 26,
258 #define NUM_TOTAL_MODES ARRAY_SIZE(pvr2_modedb)
260 #define DEFMODE_NTSC 0
261 #define DEFMODE_PAL 0
262 #define DEFMODE_VGA 2
264 static int defmode
= DEFMODE_NTSC
;
265 static char *mode_option __initdata
= NULL
;
267 static inline void pvr2fb_set_pal_type(unsigned int type
)
269 struct pvr2fb_par
*par
= (struct pvr2fb_par
*)fb_info
->par
;
271 fb_writel(type
, par
->mmio_base
+ 0x108);
274 static inline void pvr2fb_set_pal_entry(struct pvr2fb_par
*par
,
278 fb_writel(val
, par
->mmio_base
+ 0x1000 + (4 * regno
));
281 static int pvr2fb_blank(int blank
, struct fb_info
*info
)
283 do_blank
= blank
? blank
: -1;
287 static inline unsigned long get_line_length(int xres_virtual
, int bpp
)
289 return (unsigned long)((((xres_virtual
*bpp
)+31)&~31) >> 3);
292 static void set_color_bitfields(struct fb_var_screeninfo
*var
)
294 switch (var
->bits_per_pixel
) {
295 case 16: /* RGB 565 */
296 pvr2fb_set_pal_type(PAL_RGB565
);
297 var
->red
.offset
= 11; var
->red
.length
= 5;
298 var
->green
.offset
= 5; var
->green
.length
= 6;
299 var
->blue
.offset
= 0; var
->blue
.length
= 5;
300 var
->transp
.offset
= 0; var
->transp
.length
= 0;
302 case 24: /* RGB 888 */
303 var
->red
.offset
= 16; var
->red
.length
= 8;
304 var
->green
.offset
= 8; var
->green
.length
= 8;
305 var
->blue
.offset
= 0; var
->blue
.length
= 8;
306 var
->transp
.offset
= 0; var
->transp
.length
= 0;
308 case 32: /* ARGB 8888 */
309 pvr2fb_set_pal_type(PAL_ARGB8888
);
310 var
->red
.offset
= 16; var
->red
.length
= 8;
311 var
->green
.offset
= 8; var
->green
.length
= 8;
312 var
->blue
.offset
= 0; var
->blue
.length
= 8;
313 var
->transp
.offset
= 24; var
->transp
.length
= 8;
318 static int pvr2fb_setcolreg(unsigned int regno
, unsigned int red
,
319 unsigned int green
, unsigned int blue
,
320 unsigned int transp
, struct fb_info
*info
)
322 struct pvr2fb_par
*par
= (struct pvr2fb_par
*)info
->par
;
325 if (regno
> info
->cmap
.len
)
329 * We only support the hardware palette for 16 and 32bpp. It's also
330 * expected that the palette format has been set by the time we get
331 * here, so we don't waste time setting it again.
333 switch (info
->var
.bits_per_pixel
) {
334 case 16: /* RGB 565 */
335 tmp
= (red
& 0xf800) |
336 ((green
& 0xfc00) >> 5) |
337 ((blue
& 0xf800) >> 11);
339 pvr2fb_set_pal_entry(par
, regno
, tmp
);
340 ((u16
*)(info
->pseudo_palette
))[regno
] = tmp
;
342 case 24: /* RGB 888 */
343 red
>>= 8; green
>>= 8; blue
>>= 8;
344 ((u32
*)(info
->pseudo_palette
))[regno
] = (red
<< 16) | (green
<< 8) | blue
;
346 case 32: /* ARGB 8888 */
347 red
>>= 8; green
>>= 8; blue
>>= 8;
348 tmp
= (transp
<< 24) | (red
<< 16) | (green
<< 8) | blue
;
350 pvr2fb_set_pal_entry(par
, regno
, tmp
);
351 ((u32
*)(info
->pseudo_palette
))[regno
] = tmp
;
354 pr_debug("Invalid bit depth %d?!?\n", info
->var
.bits_per_pixel
);
361 static int pvr2fb_set_par(struct fb_info
*info
)
363 struct pvr2fb_par
*par
= (struct pvr2fb_par
*)info
->par
;
364 struct fb_var_screeninfo
*var
= &info
->var
;
365 unsigned long line_length
;
369 * XXX: It's possible that a user could use a VGA box, change the cable
370 * type in hardware (i.e. switch from VGA<->composite), then change
371 * modes (i.e. switching to another VT). If that happens we should
372 * automagically change the output format to cope, but currently I
373 * don't have a VGA box to make sure this works properly.
375 cable_type
= pvr2_init_cable();
376 if (cable_type
== CT_VGA
&& video_output
!= VO_VGA
)
377 video_output
= VO_VGA
;
379 var
->vmode
&= FB_VMODE_MASK
;
380 if (var
->vmode
& FB_VMODE_INTERLACED
&& video_output
!= VO_VGA
)
381 par
->is_interlaced
= 1;
383 * XXX: Need to be more creative with this (i.e. allow doublecan for
386 if (var
->vmode
& FB_VMODE_DOUBLE
&& video_output
== VO_VGA
)
387 par
->is_doublescan
= 1;
389 par
->hsync_total
= var
->left_margin
+ var
->xres
+ var
->right_margin
+
391 par
->vsync_total
= var
->upper_margin
+ var
->yres
+ var
->lower_margin
+
394 if (var
->sync
& FB_SYNC_BROADCAST
) {
395 vtotal
= par
->vsync_total
;
396 if (par
->is_interlaced
)
398 if (vtotal
> (PAL_VTOTAL
+ NTSC_VTOTAL
)/2) {
399 /* XXX: Check for start values here... */
400 /* XXX: Check hardware for PAL-compatibility */
401 par
->borderstart_h
= 116;
402 par
->borderstart_v
= 44;
404 /* NTSC video output */
405 par
->borderstart_h
= 126;
406 par
->borderstart_v
= 18;
410 /* XXX: What else needs to be checked? */
412 * XXX: We have a little freedom in VGA modes, what ranges
413 * should be here (i.e. hsync/vsync totals, etc.)?
415 par
->borderstart_h
= 126;
416 par
->borderstart_v
= 40;
419 /* Calculate the remainding offsets */
420 par
->diwstart_h
= par
->borderstart_h
+ var
->left_margin
;
421 par
->diwstart_v
= par
->borderstart_v
+ var
->upper_margin
;
422 par
->borderstop_h
= par
->diwstart_h
+ var
->xres
+
424 par
->borderstop_v
= par
->diwstart_v
+ var
->yres
+
427 if (!par
->is_interlaced
)
428 par
->borderstop_v
/= 2;
429 if (info
->var
.xres
< 640)
432 line_length
= get_line_length(var
->xres_virtual
, var
->bits_per_pixel
);
433 par
->disp_start
= info
->fix
.smem_start
+ (line_length
* var
->yoffset
) * line_length
;
434 info
->fix
.line_length
= line_length
;
438 static int pvr2fb_check_var(struct fb_var_screeninfo
*var
, struct fb_info
*info
)
440 struct pvr2fb_par
*par
= (struct pvr2fb_par
*)info
->par
;
441 unsigned int vtotal
, hsync_total
;
442 unsigned long line_length
;
444 if (var
->pixclock
!= TV_CLK
&& var
->pixclock
!= VGA_CLK
) {
445 pr_debug("Invalid pixclock value %d\n", var
->pixclock
);
453 if (var
->xres_virtual
< var
->xres
)
454 var
->xres_virtual
= var
->xres
;
455 if (var
->yres_virtual
< var
->yres
)
456 var
->yres_virtual
= var
->yres
;
458 if (var
->bits_per_pixel
<= 16)
459 var
->bits_per_pixel
= 16;
460 else if (var
->bits_per_pixel
<= 24)
461 var
->bits_per_pixel
= 24;
462 else if (var
->bits_per_pixel
<= 32)
463 var
->bits_per_pixel
= 32;
465 set_color_bitfields(var
);
467 if (var
->vmode
& FB_VMODE_YWRAP
) {
468 if (var
->xoffset
|| var
->yoffset
< 0 ||
469 var
->yoffset
>= var
->yres_virtual
) {
470 var
->xoffset
= var
->yoffset
= 0;
472 if (var
->xoffset
> var
->xres_virtual
- var
->xres
||
473 var
->yoffset
> var
->yres_virtual
- var
->yres
||
474 var
->xoffset
< 0 || var
->yoffset
< 0)
475 var
->xoffset
= var
->yoffset
= 0;
478 var
->xoffset
= var
->yoffset
= 0;
482 * XXX: Need to be more creative with this (i.e. allow doublecan for
485 if (var
->yres
< 480 && video_output
== VO_VGA
)
486 var
->vmode
|= FB_VMODE_DOUBLE
;
488 if (video_output
!= VO_VGA
) {
489 var
->sync
|= FB_SYNC_BROADCAST
;
490 var
->vmode
|= FB_VMODE_INTERLACED
;
492 var
->sync
&= ~FB_SYNC_BROADCAST
;
493 var
->vmode
&= ~FB_VMODE_INTERLACED
;
494 var
->vmode
|= pvr2_var
.vmode
;
497 if ((var
->activate
& FB_ACTIVATE_MASK
) != FB_ACTIVATE_TEST
) {
498 var
->right_margin
= par
->borderstop_h
-
499 (par
->diwstart_h
+ var
->xres
);
500 var
->left_margin
= par
->diwstart_h
- par
->borderstart_h
;
501 var
->hsync_len
= par
->borderstart_h
+
502 (par
->hsync_total
- par
->borderstop_h
);
504 var
->upper_margin
= par
->diwstart_v
- par
->borderstart_v
;
505 var
->lower_margin
= par
->borderstop_v
-
506 (par
->diwstart_v
+ var
->yres
);
507 var
->vsync_len
= par
->borderstop_v
+
508 (par
->vsync_total
- par
->borderstop_v
);
511 hsync_total
= var
->left_margin
+ var
->xres
+ var
->right_margin
+
513 vtotal
= var
->upper_margin
+ var
->yres
+ var
->lower_margin
+
516 if (var
->sync
& FB_SYNC_BROADCAST
) {
517 if (var
->vmode
& FB_VMODE_INTERLACED
)
519 if (vtotal
> (PAL_VTOTAL
+ NTSC_VTOTAL
)/2) {
520 /* PAL video output */
521 /* XXX: Should be using a range here ... ? */
522 if (hsync_total
!= PAL_HTOTAL
) {
523 pr_debug("invalid hsync total for PAL\n");
527 /* NTSC video output */
528 if (hsync_total
!= NTSC_HTOTAL
) {
529 pr_debug("invalid hsync total for NTSC\n");
535 /* Check memory sizes */
536 line_length
= get_line_length(var
->xres_virtual
, var
->bits_per_pixel
);
537 if (line_length
* var
->yres_virtual
> info
->fix
.smem_len
)
543 static void pvr2_update_display(struct fb_info
*info
)
545 struct pvr2fb_par
*par
= (struct pvr2fb_par
*) info
->par
;
546 struct fb_var_screeninfo
*var
= &info
->var
;
548 /* Update the start address of the display image */
549 fb_writel(par
->disp_start
, DISP_DIWADDRL
);
550 fb_writel(par
->disp_start
+
551 get_line_length(var
->xoffset
+var
->xres
, var
->bits_per_pixel
),
556 * Initialize the video mode. Currently, the 16bpp and 24bpp modes aren't
557 * very stable. It's probably due to the fact that a lot of the 2D video
558 * registers are still undocumented.
561 static void pvr2_init_display(struct fb_info
*info
)
563 struct pvr2fb_par
*par
= (struct pvr2fb_par
*) info
->par
;
564 struct fb_var_screeninfo
*var
= &info
->var
;
565 unsigned int diw_height
, diw_width
, diw_modulo
= 1;
566 unsigned int bytesperpixel
= var
->bits_per_pixel
>> 3;
568 /* hsync and vsync totals */
569 fb_writel((par
->vsync_total
<< 16) | par
->hsync_total
, DISP_SYNCSIZE
);
571 /* column height, modulo, row width */
572 /* since we're "panning" within vram, we need to offset things based
573 * on the offset from the virtual x start to our real gfx. */
574 if (video_output
!= VO_VGA
&& par
->is_interlaced
)
575 diw_modulo
+= info
->fix
.line_length
/ 4;
576 diw_height
= (par
->is_interlaced
? var
->yres
/ 2 : var
->yres
);
577 diw_width
= get_line_length(var
->xres
, var
->bits_per_pixel
) / 4;
578 fb_writel((diw_modulo
<< 20) | (--diw_height
<< 10) | --diw_width
,
581 /* display address, long and short fields */
582 fb_writel(par
->disp_start
, DISP_DIWADDRL
);
583 fb_writel(par
->disp_start
+
584 get_line_length(var
->xoffset
+var
->xres
, var
->bits_per_pixel
),
587 /* border horizontal, border vertical, border color */
588 fb_writel((par
->borderstart_h
<< 16) | par
->borderstop_h
, DISP_BRDRHORZ
);
589 fb_writel((par
->borderstart_v
<< 16) | par
->borderstop_v
, DISP_BRDRVERT
);
590 fb_writel(0, DISP_BRDRCOLR
);
592 /* display window start position */
593 fb_writel(par
->diwstart_h
, DISP_DIWHSTRT
);
594 fb_writel((par
->diwstart_v
<< 16) | par
->diwstart_v
, DISP_DIWVSTRT
);
597 fb_writel((0x16 << 16) | par
->is_lowres
, DISP_DIWCONF
);
599 /* clock doubler (for VGA), scan doubler, display enable */
600 fb_writel(((video_output
== VO_VGA
) << 23) |
601 (par
->is_doublescan
<< 1) | 1, DISP_DIWMODE
);
604 fb_writel(fb_readl(DISP_DIWMODE
) | (--bytesperpixel
<< 2), DISP_DIWMODE
);
606 /* video enable, color sync, interlace,
607 * hsync and vsync polarity (currently unused) */
608 fb_writel(0x100 | ((par
->is_interlaced
/*|4*/) << 4), DISP_SYNCCONF
);
611 /* Simulate blanking by making the border cover the entire screen */
613 #define BLANK_BIT (1<<3)
615 static void pvr2_do_blank(void)
617 struct pvr2fb_par
*par
= currentpar
;
618 unsigned long diwconf
;
620 diwconf
= fb_readl(DISP_DIWCONF
);
622 fb_writel(diwconf
| BLANK_BIT
, DISP_DIWCONF
);
624 fb_writel(diwconf
& ~BLANK_BIT
, DISP_DIWCONF
);
626 is_blanked
= do_blank
> 0 ? do_blank
: 0;
629 static irqreturn_t
pvr2fb_interrupt(int irq
, void *dev_id
, struct pt_regs
*fp
)
631 struct fb_info
*info
= dev_id
;
633 if (do_vmode_pan
|| do_vmode_full
)
634 pvr2_update_display(info
);
636 pvr2_init_display(info
);
649 * Determine the cable type and initialize the cable output format. Don't do
650 * anything if the cable type has been overidden (via "cable:XX").
653 #define PCTRA 0xff80002c
654 #define PDTRA 0xff800030
655 #define VOUTC 0xa0702c00
657 static int pvr2_init_cable(void)
659 if (cable_type
< 0) {
660 fb_writel((fb_readl(PCTRA
) & 0xfff0ffff) | 0x000a0000,
662 cable_type
= (fb_readw(PDTRA
) >> 8) & 3;
665 /* Now select the output format (either composite or other) */
666 /* XXX: Save the previous val first, as this reg is also AICA
668 if (cable_type
== CT_COMPOSITE
)
669 fb_writel(3 << 8, VOUTC
);
677 static ssize_t
pvr2fb_write(struct file
*file
, const char *buf
,
678 size_t count
, loff_t
*ppos
)
680 unsigned long dst
, start
, end
, len
;
681 unsigned int nr_pages
;
685 nr_pages
= (count
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
687 pages
= kmalloc(nr_pages
* sizeof(struct page
*), GFP_KERNEL
);
691 down_read(¤t
->mm
->mmap_sem
);
692 ret
= get_user_pages(current
, current
->mm
, (unsigned long)buf
,
693 nr_pages
, WRITE
, 0, pages
, NULL
);
694 up_read(¤t
->mm
->mmap_sem
);
696 if (ret
< nr_pages
) {
702 dma_configure_channel(shdma
, 0x12c1);
704 dst
= (unsigned long)fb_info
->screen_base
+ *ppos
;
705 start
= (unsigned long)page_address(pages
[0]);
706 end
= (unsigned long)page_address(pages
[nr_pages
]);
707 len
= nr_pages
<< PAGE_SHIFT
;
709 /* Half-assed contig check */
710 if (start
+ len
== end
) {
711 /* As we do this in one shot, it's either all or nothing.. */
712 if ((*ppos
+ len
) > fb_info
->fix
.smem_len
) {
717 dma_write(shdma
, start
, 0, len
);
718 dma_write(pvr2dma
, 0, dst
, len
);
719 dma_wait_for_completion(pvr2dma
);
724 /* Not contiguous, writeout per-page instead.. */
725 for (i
= 0; i
< nr_pages
; i
++, dst
+= PAGE_SIZE
) {
726 if ((*ppos
+ (i
<< PAGE_SHIFT
)) > fb_info
->fix
.smem_len
) {
731 dma_write_page(shdma
, (unsigned long)page_address(pages
[i
]), 0);
732 dma_write_page(pvr2dma
, 0, dst
);
733 dma_wait_for_completion(pvr2dma
);
741 for (i
= 0; i
< nr_pages
; i
++)
742 page_cache_release(pages
[i
]);
748 #endif /* CONFIG_SH_DMA */
753 * Common init code for the PVR2 chips.
755 * This mostly takes care of the common aspects of the fb setup and
756 * registration. It's expected that the board-specific init code has
757 * already setup pvr2_fix with something meaningful at this point.
759 * Device info reporting is also done here, as well as picking a sane
760 * default from the modedb. For board-specific modelines, simply define
761 * a per-board modedb.
763 * Also worth noting is that the cable and video output types are likely
764 * always going to be VGA for the PCI-based PVR2 boards, but we leave this
765 * in for flexibility anyways. Who knows, maybe someone has tv-out on a
766 * PCI-based version of these things ;-)
768 static int __init
pvr2fb_common_init(void)
770 struct pvr2fb_par
*par
= currentpar
;
771 unsigned long modememused
, rev
;
773 fb_info
->screen_base
= ioremap_nocache(pvr2_fix
.smem_start
,
776 if (!fb_info
->screen_base
) {
777 printk(KERN_ERR
"pvr2fb: Failed to remap smem space\n");
781 par
->mmio_base
= (unsigned long)ioremap_nocache(pvr2_fix
.mmio_start
,
783 if (!par
->mmio_base
) {
784 printk(KERN_ERR
"pvr2fb: Failed to remap mmio space\n");
788 fb_memset((unsigned long)fb_info
->screen_base
, 0, pvr2_fix
.smem_len
);
790 pvr2_fix
.ypanstep
= nopan
? 0 : 1;
791 pvr2_fix
.ywrapstep
= nowrap
? 0 : 1;
793 fb_info
->fbops
= &pvr2fb_ops
;
794 fb_info
->fix
= pvr2_fix
;
795 fb_info
->par
= currentpar
;
796 fb_info
->pseudo_palette
= (void *)(fb_info
->par
+ 1);
797 fb_info
->flags
= FBINFO_DEFAULT
| FBINFO_HWACCEL_YPAN
;
799 if (video_output
== VO_VGA
)
800 defmode
= DEFMODE_VGA
;
803 mode_option
= "640x480@60";
805 if (!fb_find_mode(&fb_info
->var
, fb_info
, mode_option
, pvr2_modedb
,
806 NUM_TOTAL_MODES
, &pvr2_modedb
[defmode
], 16))
807 fb_info
->var
= pvr2_var
;
809 fb_alloc_cmap(&fb_info
->cmap
, 256, 0);
811 if (register_framebuffer(fb_info
) < 0)
814 modememused
= get_line_length(fb_info
->var
.xres_virtual
,
815 fb_info
->var
.bits_per_pixel
);
816 modememused
*= fb_info
->var
.yres_virtual
;
818 rev
= fb_readl(par
->mmio_base
+ 0x04);
820 printk("fb%d: %s (rev %ld.%ld) frame buffer device, using %ldk/%ldk of video memory\n",
821 fb_info
->node
, fb_info
->fix
.id
, (rev
>> 4) & 0x0f, rev
& 0x0f,
822 modememused
>> 10, (unsigned long)(fb_info
->fix
.smem_len
>> 10));
823 printk("fb%d: Mode %dx%d-%d pitch = %ld cable: %s video output: %s\n",
824 fb_info
->node
, fb_info
->var
.xres
, fb_info
->var
.yres
,
825 fb_info
->var
.bits_per_pixel
,
826 get_line_length(fb_info
->var
.xres
, fb_info
->var
.bits_per_pixel
),
827 (char *)pvr2_get_param(cables
, NULL
, cable_type
, 3),
828 (char *)pvr2_get_param(outputs
, NULL
, video_output
, 3));
830 #ifdef CONFIG_SH_STORE_QUEUES
831 printk(KERN_NOTICE
"fb%d: registering with SQ API\n", fb_info
->node
);
833 pvr2fb_map
= sq_remap(fb_info
->fix
.smem_start
, fb_info
->fix
.smem_len
,
836 printk(KERN_NOTICE
"fb%d: Mapped video memory to SQ addr 0x%lx\n",
837 fb_info
->node
, pvr2fb_map
->sq_addr
);
843 if (fb_info
->screen_base
)
844 iounmap(fb_info
->screen_base
);
846 iounmap((void *)par
->mmio_base
);
851 #ifdef CONFIG_SH_DREAMCAST
852 static int __init
pvr2fb_dc_init(void)
854 if (!mach_is_dreamcast())
857 /* Make a guess at the monitor based on the attached cable */
858 if (pvr2_init_cable() == CT_VGA
) {
859 fb_info
->monspecs
.hfmin
= 30000;
860 fb_info
->monspecs
.hfmax
= 70000;
861 fb_info
->monspecs
.vfmin
= 60;
862 fb_info
->monspecs
.vfmax
= 60;
864 /* Not VGA, using a TV (taken from acornfb) */
865 fb_info
->monspecs
.hfmin
= 15469;
866 fb_info
->monspecs
.hfmax
= 15781;
867 fb_info
->monspecs
.vfmin
= 49;
868 fb_info
->monspecs
.vfmax
= 51;
872 * XXX: This needs to pull default video output via BIOS or other means
874 if (video_output
< 0) {
875 if (cable_type
== CT_VGA
) {
876 video_output
= VO_VGA
;
878 video_output
= VO_NTSC
;
883 * Nothing exciting about the DC PVR2 .. only a measly 8MiB.
885 pvr2_fix
.smem_start
= 0xa5000000; /* RAM starts here */
886 pvr2_fix
.smem_len
= 8 << 20;
888 pvr2_fix
.mmio_start
= 0xa05f8000; /* registers start here */
889 pvr2_fix
.mmio_len
= 0x2000;
891 if (request_irq(HW_EVENT_VSYNC
, pvr2fb_interrupt
, 0,
892 "pvr2 VBL handler", fb_info
)) {
897 if (request_dma(pvr2dma
, "pvr2") != 0) {
898 free_irq(HW_EVENT_VSYNC
, 0);
903 return pvr2fb_common_init();
906 static void pvr2fb_dc_exit(void)
908 free_irq(HW_EVENT_VSYNC
, 0);
913 #endif /* CONFIG_SH_DREAMCAST */
916 static int __devinit
pvr2fb_pci_probe(struct pci_dev
*pdev
,
917 const struct pci_device_id
*ent
)
921 ret
= pci_enable_device(pdev
);
923 printk(KERN_ERR
"pvr2fb: PCI enable failed\n");
927 ret
= pci_request_regions(pdev
, "pvr2fb");
929 printk(KERN_ERR
"pvr2fb: PCI request regions failed\n");
934 * Slightly more exciting than the DC PVR2 .. 16MiB!
936 pvr2_fix
.smem_start
= pci_resource_start(pdev
, 0);
937 pvr2_fix
.smem_len
= pci_resource_len(pdev
, 0);
939 pvr2_fix
.mmio_start
= pci_resource_start(pdev
, 1);
940 pvr2_fix
.mmio_len
= pci_resource_len(pdev
, 1);
942 fb_info
->device
= &pdev
->dev
;
944 return pvr2fb_common_init();
947 static void __devexit
pvr2fb_pci_remove(struct pci_dev
*pdev
)
949 pci_release_regions(pdev
);
952 static struct pci_device_id pvr2fb_pci_tbl
[] __devinitdata
= {
953 { PCI_VENDOR_ID_NEC
, PCI_DEVICE_ID_NEC_NEON250
,
954 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0 },
958 MODULE_DEVICE_TABLE(pci
, pvr2fb_pci_tbl
);
960 static struct pci_driver pvr2fb_pci_driver
= {
962 .id_table
= pvr2fb_pci_tbl
,
963 .probe
= pvr2fb_pci_probe
,
964 .remove
= __devexit_p(pvr2fb_pci_remove
),
967 static int __init
pvr2fb_pci_init(void)
969 return pci_register_driver(&pvr2fb_pci_driver
);
972 static void pvr2fb_pci_exit(void)
974 pci_unregister_driver(&pvr2fb_pci_driver
);
976 #endif /* CONFIG_PCI */
978 static int __init
pvr2_get_param(const struct pvr2_params
*p
, const char *s
,
983 for (i
= 0 ; i
< size
; i
++ ) {
985 if (!strnicmp(p
[i
].name
, s
, strlen(s
)))
989 return (int)p
[i
].name
;
996 * Parse command arguments. Supported arguments are:
997 * inverse Use inverse color maps
998 * cable:composite|rgb|vga Override the video cable type
999 * output:NTSC|PAL|VGA Override the video output format
1001 * <xres>x<yres>[-<bpp>][@<refresh>] or,
1002 * <name>[-<bpp>][@<refresh>] Startup using this video mode
1006 int __init
pvr2fb_setup(char *options
)
1010 char output_arg
[80];
1012 if (!options
|| !*options
)
1015 while ((this_opt
= strsep(&options
, ","))) {
1018 if (!strcmp(this_opt
, "inverse")) {
1020 } else if (!strncmp(this_opt
, "cable:", 6)) {
1021 strcpy(cable_arg
, this_opt
+ 6);
1022 } else if (!strncmp(this_opt
, "output:", 7)) {
1023 strcpy(output_arg
, this_opt
+ 7);
1024 } else if (!strncmp(this_opt
, "nopan", 5)) {
1026 } else if (!strncmp(this_opt
, "nowrap", 6)) {
1029 mode_option
= this_opt
;
1034 cable_type
= pvr2_get_param(cables
, cable_arg
, 0, 3);
1036 video_output
= pvr2_get_param(outputs
, output_arg
, 0, 3);
1042 static struct pvr2_board
{
1047 #ifdef CONFIG_SH_DREAMCAST
1048 { pvr2fb_dc_init
, pvr2fb_dc_exit
, "Sega DC PVR2" },
1051 { pvr2fb_pci_init
, pvr2fb_pci_exit
, "PCI PVR2" },
1056 int __init
pvr2fb_init(void)
1058 int i
, ret
= -ENODEV
;
1062 char *option
= NULL
;
1064 if (fb_get_options("pvr2fb", &option
))
1066 pvr2fb_setup(option
);
1068 size
= sizeof(struct fb_info
) + sizeof(struct pvr2fb_par
) + 16 * sizeof(u32
);
1070 fb_info
= kmalloc(size
, GFP_KERNEL
);
1072 printk(KERN_ERR
"Failed to allocate memory for fb_info\n");
1076 memset(fb_info
, 0, size
);
1078 currentpar
= (struct pvr2fb_par
*)(fb_info
+ 1);
1080 for (i
= 0; i
< ARRAY_SIZE(board_list
); i
++) {
1081 struct pvr2_board
*pvr_board
= board_list
+ i
;
1083 if (!pvr_board
->init
)
1086 ret
= pvr_board
->init();
1089 printk(KERN_ERR
"pvr2fb: Failed init of %s device\n",
1099 static void __exit
pvr2fb_exit(void)
1103 for (i
= 0; i
< ARRAY_SIZE(board_list
); i
++) {
1104 struct pvr2_board
*pvr_board
= board_list
+ i
;
1106 if (pvr_board
->exit
)
1110 #ifdef CONFIG_SH_STORE_QUEUES
1111 sq_unmap(pvr2fb_map
);
1114 unregister_framebuffer(fb_info
);
1118 module_init(pvr2fb_init
);
1119 module_exit(pvr2fb_exit
);
1121 MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, M. R. Brown <mrbrown@0xd6.org>");
1122 MODULE_DESCRIPTION("Framebuffer driver for NEC PowerVR 2 based graphics boards");
1123 MODULE_LICENSE("GPL");