fbdev: move FBIO_WAITFORVSYNC to linux/fb.h
[safe/jmp/linux-2.6] / drivers / video / pm2fb.c
index c3abb95..27f93aa 100644 (file)
@@ -11,7 +11,7 @@
  * and additional input from James Simmon's port of Hannu Mallat's tdfx
  * driver.
  *
- * I have a Creative Graphics Blaster Exxtreme card - pm2fb on x86.  I
+ * I have a Creative Graphics Blaster Exxtreme card - pm2fb on x86. I
  * have no access to other pm2fb implementations. Sparc (and thus
  * hopefully other big-endian) devices now work, thanks to a lot of
  * testing work by Ron Murray. I have no access to CVision hardware,
 
 #undef PM2FB_MASTER_DEBUG
 #ifdef PM2FB_MASTER_DEBUG
-#define DPRINTK(a,b...)        printk(KERN_DEBUG "pm2fb: %s: " a, __FUNCTION__ , ## b)
+#define DPRINTK(a, b...)       \
+       printk(KERN_DEBUG "pm2fb: %s: " a, __func__ , ## b)
 #else
-#define DPRINTK(a,b...)
+#define DPRINTK(a, b...)
 #endif
 
 #define PM2_PIXMAP_SIZE        (1600 * 4)
@@ -65,7 +66,8 @@
 /*
  * Driver data
  */
-static char *mode __devinitdata = NULL;
+static int hwcursor = 1;
+static char *mode_option __devinitdata;
 
 /*
  * The XFree GLINT driver will (I think to implement hardware cursor
@@ -146,60 +148,39 @@ static struct fb_var_screeninfo pm2fb_var __devinitdata = {
  * Utility functions
  */
 
-static inline u32 RD32(unsigned char __iomem *base, s32 off)
+static inline u32 pm2_RD(struct pm2fb_par *p, s32 off)
 {
-       return fb_readl(base + off);
+       return fb_readl(p->v_regs + off);
 }
 
-static inline void WR32(unsigned char __iomem *base, s32 off, u32 v)
+static inline void pm2_WR(struct pm2fb_par *p, s32 off, u32 v)
 {
-       fb_writel(v, base + off);
+       fb_writel(v, p->v_regs + off);
 }
 
-static inline u32 pm2_RD(struct pm2fb_par* p, s32 off)
+static inline u32 pm2_RDAC_RD(struct pm2fb_par *p, s32 idx)
 {
-       return RD32(p->v_regs, off);
-}
-
-static inline void pm2_WR(struct pm2fb_par* p, s32 off, u32 v)
-{
-       WR32(p->v_regs, off, v);
+       pm2_WR(p, PM2R_RD_PALETTE_WRITE_ADDRESS, idx);
+       mb();
+       return pm2_RD(p, PM2R_RD_INDEXED_DATA);
 }
 
-static inline u32 pm2_RDAC_RD(struct pm2fb_par* p, s32 idx)
+static inline u32 pm2v_RDAC_RD(struct pm2fb_par *p, s32 idx)
 {
-       int index = PM2R_RD_INDEXED_DATA;
-       switch (p->type) {
-       case PM2_TYPE_PERMEDIA2:
-               pm2_WR(p, PM2R_RD_PALETTE_WRITE_ADDRESS, idx);
-               break;
-       case PM2_TYPE_PERMEDIA2V:
-               pm2_WR(p, PM2VR_RD_INDEX_LOW, idx & 0xff);
-               index = PM2VR_RD_INDEXED_DATA;
-               break;
-       }
+       pm2_WR(p, PM2VR_RD_INDEX_LOW, idx & 0xff);
        mb();
-       return pm2_RD(p, index);
+       return pm2_RD(p,  PM2VR_RD_INDEXED_DATA);
 }
 
-static inline void pm2_RDAC_WR(struct pm2fb_parp, s32 idx, u32 v)
+static inline void pm2_RDAC_WR(struct pm2fb_par *p, s32 idx, u32 v)
 {
-       int index = PM2R_RD_INDEXED_DATA;
-       switch (p->type) {
-       case PM2_TYPE_PERMEDIA2:
-               pm2_WR(p, PM2R_RD_PALETTE_WRITE_ADDRESS, idx);
-               break;
-       case PM2_TYPE_PERMEDIA2V:
-               pm2_WR(p, PM2VR_RD_INDEX_LOW, idx & 0xff);
-               index = PM2VR_RD_INDEXED_DATA;
-               break;
-       }
+       pm2_WR(p, PM2R_RD_PALETTE_WRITE_ADDRESS, idx);
        wmb();
-       pm2_WR(p, index, v);
+       pm2_WR(p, PM2R_RD_INDEXED_DATA, v);
        wmb();
 }
 
-static inline void pm2v_RDAC_WR(struct pm2fb_parp, s32 idx, u32 v)
+static inline void pm2v_RDAC_WR(struct pm2fb_par *p, s32 idx, u32 v)
 {
        pm2_WR(p, PM2VR_RD_INDEX_LOW, idx & 0xff);
        wmb();
@@ -210,10 +191,10 @@ static inline void pm2v_RDAC_WR(struct pm2fb_par* p, s32 idx, u32 v)
 #ifdef CONFIG_FB_PM2_FIFO_DISCONNECT
 #define WAIT_FIFO(p, a)
 #else
-static inline void WAIT_FIFO(struct pm2fb_parp, u32 a)
+static inline void WAIT_FIFO(struct pm2fb_par *p, u32 a)
 {
-       while( pm2_RD(p, PM2R_IN_FIFO_SPACE) < a );
-       mb();
+       while (pm2_RD(p, PM2R_IN_FIFO_SPACE) < a)
+               cpu_relax();
 }
 #endif
 
@@ -249,7 +230,7 @@ static u32 partprod(u32 xres)
 
        for (i = 0; pp_table[i].width && pp_table[i].width != xres; i++)
                ;
-       if ( pp_table[i].width == 0 )
+       if (pp_table[i].width == 0)
                DPRINTK("invalid width %u\n", xres);
        return pp_table[i].pp;
 }
@@ -257,25 +238,22 @@ static u32 partprod(u32 xres)
 static u32 to3264(u32 timing, int bpp, int is64)
 {
        switch (bpp) {
+       case 24:
+               timing *= 3;
        case 8:
-               timing >>= 2 + is64;
-               break;
+               timing >>= 1;
        case 16:
-               timing >>= 1 + is64;
-               break;
-       case 24:
-               timing = (timing * 3) >> (2 + is64);
-               break;
+               timing >>= 1;
        case 32:
-               if (is64)
-                       timing >>= 1;
                break;
        }
+       if (is64)
+               timing >>= 1;
        return timing;
 }
 
-static void pm2_mnp(u32 clk, unsigned char* mm, unsigned char* nn,
-                   unsigned charpp)
+static void pm2_mnp(u32 clk, unsigned char *mm, unsigned char *nn,
+                   unsigned char *pp)
 {
        unsigned char m;
        unsigned char n;
@@ -289,13 +267,13 @@ static void pm2_mnp(u32 clk, unsigned char* mm, unsigned char* nn,
                for (m = 2; m; m++) {
                        f = PM2_REFERENCE_CLOCK * m / n;
                        if (f >= 150000 && f <= 300000) {
-                               for ( p = 0; p < 5; p++, f >>= 1) {
-                                       curr = ( clk > f ) ? clk - f : f - clk;
-                                       if ( curr < delta ) {
-                                               delta=curr;
-                                               *mm=m;
-                                               *nn=n;
-                                               *pp=p;
+                               for (p = 0; p < 5; p++, f >>= 1) {
+                                       curr = (clk > f) ? clk - f : f - clk;
+                                       if (curr < delta) {
+                                               delta = curr;
+                                               *mm = m;
+                                               *nn = n;
+                                               *pp = p;
                                        }
                                }
                        }
@@ -303,8 +281,8 @@ static void pm2_mnp(u32 clk, unsigned char* mm, unsigned char* nn,
        }
 }
 
-static void pm2v_mnp(u32 clk, unsigned char* mm, unsigned char* nn,
-                    unsigned charpp)
+static void pm2v_mnp(u32 clk, unsigned char *mm, unsigned char *nn,
+                    unsigned char *pp)
 {
        unsigned char m;
        unsigned char n;
@@ -313,23 +291,24 @@ static void pm2v_mnp(u32 clk, unsigned char* mm, unsigned char* nn,
        s32 delta = 1000;
 
        *mm = *nn = *pp = 0;
-       for ( m = 1; m < 128; m++) {
+       for (m = 1; m < 128; m++) {
                for (n = 2 * m + 1; n; n++) {
-                       for ( p = 0; p < 2; p++) {
-                               f = ( PM2_REFERENCE_CLOCK >> ( p + 1 )) * n / m;
-                               if ( clk > f - delta && clk < f + delta ) {
-                                       delta = ( clk > f ) ? clk - f : f - clk;
-                                       *mm=m;
-                                       *nn=n;
-                                       *pp=p;
+                       for (p = 0; p < 2; p++) {
+                               f = (PM2_REFERENCE_CLOCK >> (p + 1)) * n / m;
+                               if (clk > f - delta && clk < f + delta) {
+                                       delta = (clk > f) ? clk - f : f - clk;
+                                       *mm = m;
+                                       *nn = n;
+                                       *pp = p;
                                }
                        }
                }
        }
 }
 
-static void clear_palette(struct pm2fb_par* p) {
-       int i=256;
+static void clear_palette(struct pm2fb_par *p)
+{
+       int i = 256;
 
        WAIT_FIFO(p, 1);
        pm2_WR(p, PM2R_RD_PALETTE_WRITE_ADDRESS, 0);
@@ -342,14 +321,14 @@ static void clear_palette(struct pm2fb_par* p) {
        }
 }
 
-static void reset_card(struct pm2fb_parp)
+static void reset_card(struct pm2fb_par *p)
 {
        if (p->type == PM2_TYPE_PERMEDIA2V)
                pm2_WR(p, PM2VR_RD_INDEX_HIGH, 0);
        pm2_WR(p, PM2R_RESET_STATUS, 0);
        mb();
        while (pm2_RD(p, PM2R_RESET_STATUS) & PM2F_BEING_RESET)
-               ;
+               cpu_relax();
        mb();
 #ifdef CONFIG_FB_PM2_FIFO_DISCONNECT
        DPRINTK("FIFO disconnect enabled\n");
@@ -365,11 +344,11 @@ static void reset_card(struct pm2fb_par* p)
        pm2_WR(p, PM2R_MEM_CONFIG, p->mem_config);
 }
 
-static void reset_config(struct pm2fb_parp)
+static void reset_config(struct pm2fb_par *p)
 {
-       WAIT_FIFO(p, 52);
+       WAIT_FIFO(p, 53);
        pm2_WR(p, PM2R_CHIP_CONFIG, pm2_RD(p, PM2R_CHIP_CONFIG) &
-              ~(PM2F_VGA_ENABLE|PM2F_VGA_FIXED));
+                       ~(PM2F_VGA_ENABLE | PM2F_VGA_FIXED));
        pm2_WR(p, PM2R_BYPASS_WRITE_MASK, ~(0L));
        pm2_WR(p, PM2R_FRAMEBUFFER_WRITE_MASK, ~(0L));
        pm2_WR(p, PM2R_FIFO_CONTROL, 0);
@@ -404,31 +383,32 @@ static void reset_config(struct pm2fb_par* p)
        pm2_WR(p, PM2R_STATISTICS_MODE, 0);
        pm2_WR(p, PM2R_SCISSOR_MODE, 0);
        pm2_WR(p, PM2R_FILTER_MODE, PM2F_SYNCHRONIZATION);
+       pm2_WR(p, PM2R_RD_PIXEL_MASK, 0xff);
        switch (p->type) {
        case PM2_TYPE_PERMEDIA2:
                pm2_RDAC_WR(p, PM2I_RD_MODE_CONTROL, 0); /* no overlay */
                pm2_RDAC_WR(p, PM2I_RD_CURSOR_CONTROL, 0);
                pm2_RDAC_WR(p, PM2I_RD_MISC_CONTROL, PM2F_RD_PALETTE_WIDTH_8);
+               pm2_RDAC_WR(p, PM2I_RD_COLOR_KEY_CONTROL, 0);
+               pm2_RDAC_WR(p, PM2I_RD_OVERLAY_KEY, 0);
+               pm2_RDAC_WR(p, PM2I_RD_RED_KEY, 0);
+               pm2_RDAC_WR(p, PM2I_RD_GREEN_KEY, 0);
+               pm2_RDAC_WR(p, PM2I_RD_BLUE_KEY, 0);
                break;
        case PM2_TYPE_PERMEDIA2V:
                pm2v_RDAC_WR(p, PM2VI_RD_MISC_CONTROL, 1); /* 8bit */
                break;
        }
-       pm2_RDAC_WR(p, PM2I_RD_COLOR_KEY_CONTROL, 0);
-       pm2_RDAC_WR(p, PM2I_RD_OVERLAY_KEY, 0);
-       pm2_RDAC_WR(p, PM2I_RD_RED_KEY, 0);
-       pm2_RDAC_WR(p, PM2I_RD_GREEN_KEY, 0);
-       pm2_RDAC_WR(p, PM2I_RD_BLUE_KEY, 0);
 }
 
-static void set_aperture(struct pm2fb_parp, u32 depth)
+static void set_aperture(struct pm2fb_par *p, u32 depth)
 {
        /*
         * The hardware is little-endian. When used in big-endian
         * hosts, the on-chip aperture settings are used where
         * possible to translate from host to card byte order.
         */
-       WAIT_FIFO(p, 4);
+       WAIT_FIFO(p, 2);
 #ifdef __LITTLE_ENDIAN
        pm2_WR(p, PM2R_APERTURE_ONE, PM2F_APERTURE_STANDARD);
 #else
@@ -451,11 +431,11 @@ static void set_aperture(struct pm2fb_par* p, u32 depth)
        }
 #endif
 
-       // We don't use aperture two, so this may be superflous
+       /* We don't use aperture two, so this may be superflous */
        pm2_WR(p, PM2R_APERTURE_TWO, PM2F_APERTURE_STANDARD);
 }
 
-static void set_color(struct pm2fb_parp, unsigned char regno,
+static void set_color(struct pm2fb_par *p, unsigned char regno,
                      unsigned char r, unsigned char g, unsigned char b)
 {
        WAIT_FIFO(p, 4);
@@ -468,7 +448,7 @@ static void set_color(struct pm2fb_par* p, unsigned char regno,
        pm2_WR(p, PM2R_RD_PALETTE_DATA, b);
 }
 
-static void set_memclock(struct pm2fb_parpar, u32 clk)
+static void set_memclock(struct pm2fb_par *par, u32 clk)
 {
        int i;
        unsigned char m, n, p;
@@ -476,7 +456,7 @@ static void set_memclock(struct pm2fb_par* par, u32 clk)
        switch (par->type) {
        case PM2_TYPE_PERMEDIA2V:
                pm2v_mnp(clk/2, &m, &n, &p);
-               WAIT_FIFO(par, 8);
+               WAIT_FIFO(par, 12);
                pm2_WR(par, PM2VR_RD_INDEX_HIGH, PM2VI_RD_MCLK_CONTROL >> 8);
                pm2v_RDAC_WR(par, PM2VI_RD_MCLK_CONTROL, 0);
                pm2v_RDAC_WR(par, PM2VI_RD_MCLK_PRESCALE, m);
@@ -484,10 +464,9 @@ static void set_memclock(struct pm2fb_par* par, u32 clk)
                pm2v_RDAC_WR(par, PM2VI_RD_MCLK_POSTSCALE, p);
                pm2v_RDAC_WR(par, PM2VI_RD_MCLK_CONTROL, 1);
                rmb();
-               for (i = 256;
-                    i && !(pm2_RDAC_RD(par, PM2VI_RD_MCLK_CONTROL) & 2);
-                    i--)
-                       ;
+               for (i = 256; i; i--)
+                       if (pm2v_RDAC_RD(par, PM2VI_RD_MCLK_CONTROL) & 2)
+                               break;
                pm2_WR(par, PM2VR_RD_INDEX_HIGH, 0);
                break;
        case PM2_TYPE_PERMEDIA2:
@@ -499,15 +478,14 @@ static void set_memclock(struct pm2fb_par* par, u32 clk)
                pm2_RDAC_WR(par, PM2I_RD_MEMORY_CLOCK_3, 8|p);
                pm2_RDAC_RD(par, PM2I_RD_MEMORY_CLOCK_STATUS);
                rmb();
-               for (i = 256;
-                    i && !(pm2_RD(par, PM2R_RD_INDEXED_DATA) & PM2F_PLL_LOCKED);
-                    i--)
-                       ;
+               for (i = 256; i; i--)
+                       if (pm2_RD(par, PM2R_RD_INDEXED_DATA) & PM2F_PLL_LOCKED)
+                               break;
                break;
        }
 }
 
-static void set_pixclock(struct pm2fb_parpar, u32 clk)
+static void set_pixclock(struct pm2fb_par *par, u32 clk)
 {
        int i;
        unsigned char m, n, p;
@@ -515,17 +493,16 @@ static void set_pixclock(struct pm2fb_par* par, u32 clk)
        switch (par->type) {
        case PM2_TYPE_PERMEDIA2:
                pm2_mnp(clk, &m, &n, &p);
-               WAIT_FIFO(par, 8);
+               WAIT_FIFO(par, 10);
                pm2_RDAC_WR(par, PM2I_RD_PIXEL_CLOCK_A3, 0);
                pm2_RDAC_WR(par, PM2I_RD_PIXEL_CLOCK_A1, m);
                pm2_RDAC_WR(par, PM2I_RD_PIXEL_CLOCK_A2, n);
                pm2_RDAC_WR(par, PM2I_RD_PIXEL_CLOCK_A3, 8|p);
                pm2_RDAC_RD(par, PM2I_RD_PIXEL_CLOCK_STATUS);
                rmb();
-               for (i = 256;
-                    i && !(pm2_RD(par, PM2R_RD_INDEXED_DATA) & PM2F_PLL_LOCKED);
-                    i--)
-                       ;
+               for (i = 256; i; i--)
+                       if (pm2_RD(par, PM2R_RD_INDEXED_DATA) & PM2F_PLL_LOCKED)
+                               break;
                break;
        case PM2_TYPE_PERMEDIA2V:
                pm2v_mnp(clk/2, &m, &n, &p);
@@ -539,11 +516,10 @@ static void set_pixclock(struct pm2fb_par* par, u32 clk)
        }
 }
 
-static void set_video(struct pm2fb_par* p, u32 video) {
+static void set_video(struct pm2fb_par *p, u32 video)
+{
        u32 tmp;
-       u32 vsync;
-
-       vsync = video;
+       u32 vsync = video;
 
        DPRINTK("video = 0x%x\n", video);
 
@@ -553,10 +529,10 @@ static void set_video(struct pm2fb_par* p, u32 video) {
         * driver may well. So always set +hsync/+vsync and then set
         * the RAMDAC to invert the sync if necessary.
         */
-       vsync &= ~(PM2F_HSYNC_MASK|PM2F_VSYNC_MASK);
-       vsync |= PM2F_HSYNC_ACT_HIGH|PM2F_VSYNC_ACT_HIGH;
+       vsync &= ~(PM2F_HSYNC_MASK | PM2F_VSYNC_MASK);
+       vsync |= PM2F_HSYNC_ACT_HIGH | PM2F_VSYNC_ACT_HIGH;
 
-       WAIT_FIFO(p, 5);
+       WAIT_FIFO(p, 3);
        pm2_WR(p, PM2R_VIDEO_CONTROL, vsync);
 
        switch (p->type) {
@@ -575,16 +551,11 @@ static void set_video(struct pm2fb_par* p, u32 video) {
                if ((video & PM2F_VSYNC_MASK) == PM2F_VSYNC_ACT_LOW)
                        tmp |= 4; /* invert vsync */
                pm2v_RDAC_WR(p, PM2VI_RD_SYNC_CONTROL, tmp);
-               pm2v_RDAC_WR(p, PM2VI_RD_MISC_CONTROL, 1);
                break;
        }
 }
 
 /*
- *
- */
-
-/**
  *     pm2fb_check_var - Optional function. Validates a var passed in.
  *     @var: frame buffer variable screen structure
  *     @info: frame buffer structure that represents a single frame buffer
@@ -605,15 +576,22 @@ static int pm2fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
        }
 
        if (var->xres != var->xres_virtual) {
-               DPRINTK("virtual x resolution != physical x resolution not supported\n");
+               DPRINTK("virtual x resolution != "
+                       "physical x resolution not supported\n");
                return -EINVAL;
        }
 
        if (var->yres > var->yres_virtual) {
-               DPRINTK("virtual y resolution < physical y resolution not possible\n");
+               DPRINTK("virtual y resolution < "
+                       "physical y resolution not possible\n");
                return -EINVAL;
        }
 
+       /* permedia cannot blit over 2048 */
+       if (var->yres_virtual > 2047) {
+               var->yres_virtual = 2047;
+       }
+
        if (var->xoffset) {
                DPRINTK("xoffset not supported\n");
                return -EINVAL;
@@ -625,7 +603,7 @@ static int pm2fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
        }
 
        var->xres = (var->xres + 15) & ~15; /* could sometimes be 8 */
-       lpitch = var->xres * ((var->bits_per_pixel + 7)>>3);
+       lpitch = var->xres * ((var->bits_per_pixel + 7) >> 3);
 
        if (var->xres < 320 || var->xres > 1600) {
                DPRINTK("width not supported: %u\n", var->xres);
@@ -644,15 +622,18 @@ static int pm2fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
        }
 
        if (PICOS2KHZ(var->pixclock) > PM2_MAX_PIXCLOCK) {
-               DPRINTK("pixclock too high (%ldKHz)\n", PICOS2KHZ(var->pixclock));
+               DPRINTK("pixclock too high (%ldKHz)\n",
+                       PICOS2KHZ(var->pixclock));
                return -EINVAL;
        }
 
        var->transp.offset = 0;
        var->transp.length = 0;
-       switch(var->bits_per_pixel) {
+       switch (var->bits_per_pixel) {
        case 8:
-               var->red.length = var->green.length = var->blue.length = 8;
+               var->red.length = 8;
+               var->green.length = 8;
+               var->blue.length = 8;
                break;
        case 16:
                var->red.offset   = 11;
@@ -668,7 +649,9 @@ static int pm2fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
                var->red.offset   = 16;
                var->green.offset = 8;
                var->blue.offset  = 0;
-               var->red.length = var->green.length = var->blue.length = 8;
+               var->red.length = 8;
+               var->green.length = 8;
+               var->blue.length = 8;
                break;
        case 24:
 #ifdef __BIG_ENDIAN
@@ -679,10 +662,13 @@ static int pm2fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
                var->blue.offset  = 0;
 #endif
                var->green.offset = 8;
-               var->red.length = var->green.length = var->blue.length = 8;
+               var->red.length = 8;
+               var->green.length = 8;
+               var->blue.length = 8;
                break;
        }
-       var->height = var->width = -1;
+       var->height = -1;
+       var->width = -1;
 
        var->accel_flags = 0;   /* Can't mmap if this is on */
 
@@ -702,7 +688,9 @@ static int pm2fb_set_par(struct fb_info *info)
 {
        struct pm2fb_par *par = info->par;
        u32 pixclock;
-       u32 width, height, depth;
+       u32 width = (info->var.xres_virtual + 7) & ~7;
+       u32 height = info->var.yres_virtual;
+       u32 depth = (info->var.bits_per_pixel + 7) & ~7;
        u32 hsstart, hsend, hbend, htotal;
        u32 vsstart, vsend, vbend, vtotal;
        u32 stride;
@@ -712,22 +700,19 @@ static int pm2fb_set_par(struct fb_info *info)
        u32 txtmap = 0;
        u32 pixsize = 0;
        u32 clrformat = 0;
-       u32 xres;
+       u32 misc = 1; /* 8-bit DAC */
+       u32 xres = (info->var.xres + 31) & ~31;
        int data64;
 
        reset_card(par);
        reset_config(par);
        clear_palette(par);
-       if ( par->memclock )
+       if (par->memclock)
                set_memclock(par, par->memclock);
 
-       width = (info->var.xres_virtual + 7) & ~7;
-       height = info->var.yres_virtual;
-       depth = (info->var.bits_per_pixel + 7) & ~7;
        depth = (depth > 32) ? 32 : depth;
        data64 = depth > 8 || par->type == PM2_TYPE_PERMEDIA2V;
 
-       xres = (info->var.xres + 31) & ~31;
        pixclock = PICOS2KHZ(info->var.pixclock);
        if (pixclock > PM2_MAX_PIXCLOCK) {
                DPRINTK("pixclock too high (%uKHz)\n", pixclock);
@@ -742,7 +727,8 @@ static int pm2fb_set_par(struct fb_info *info)
                ? info->var.lower_margin - 1
                : 0;    /* FIXME! */
        vsend = info->var.lower_margin + info->var.vsync_len - 1;
-       vbend = info->var.lower_margin + info->var.vsync_len + info->var.upper_margin;
+       vbend = info->var.lower_margin + info->var.vsync_len +
+               info->var.upper_margin;
        vtotal = info->var.yres + vbend - 1;
        stride = to3264(width, depth, 1);
        base = to3264(info->var.yoffset * xres + info->var.xoffset, depth, 1);
@@ -755,25 +741,25 @@ static int pm2fb_set_par(struct fb_info *info)
                        video |= PM2F_HSYNC_ACT_LOW;
                } else
                        video |= PM2F_HSYNC_ACT_HIGH;
-       }
-       else
+       } else
                video |= PM2F_HSYNC_ACT_LOW;
+
        if (info->var.sync & FB_SYNC_VERT_HIGH_ACT) {
                if (lowvsync) {
                        DPRINTK("ignoring +vsync, using -vsync.\n");
                        video |= PM2F_VSYNC_ACT_LOW;
                } else
                        video |= PM2F_VSYNC_ACT_HIGH;
-       }
-       else
+       } else
                video |= PM2F_VSYNC_ACT_LOW;
-       if ((info->var.vmode & FB_VMODE_MASK)==FB_VMODE_INTERLACED) {
+
+       if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) {
                DPRINTK("interlaced not supported\n");
                return -EINVAL;
        }
-       if ((info->var.vmode & FB_VMODE_MASK)==FB_VMODE_DOUBLE)
+       if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE)
                video |= PM2F_LINE_DOUBLE;
-       if ((info->var.activate & FB_ACTIVATE_MASK)==FB_ACTIVATE_NOW)
+       if ((info->var.activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW)
                video |= PM2F_VIDEO_ENABLE;
        par->video = video;
 
@@ -794,12 +780,10 @@ static int pm2fb_set_par(struct fb_info *info)
 
        mb();
        WAIT_FIFO(par, 19);
-       pm2_RDAC_WR(par, PM2I_RD_COLOR_KEY_CONTROL,
-                   ( depth == 8 ) ? 0 : PM2F_COLOR_KEY_TEST_OFF);
        switch (depth) {
        case 8:
                pm2_WR(par, PM2R_FB_READ_PIXEL, 0);
-               clrformat = 0x0e;
+               clrformat = 0x2e;
                break;
        case 16:
                pm2_WR(par, PM2R_FB_READ_PIXEL, 1);
@@ -807,6 +791,7 @@ static int pm2fb_set_par(struct fb_info *info)
                txtmap = PM2F_TEXTEL_SIZE_16;
                pixsize = 1;
                clrformat = 0x70;
+               misc |= 8;
                break;
        case 32:
                pm2_WR(par, PM2R_FB_READ_PIXEL, 2);
@@ -814,6 +799,7 @@ static int pm2fb_set_par(struct fb_info *info)
                txtmap = PM2F_TEXTEL_SIZE_32;
                pixsize = 2;
                clrformat = 0x20;
+               misc |= 8;
                break;
        case 24:
                pm2_WR(par, PM2R_FB_READ_PIXEL, 4);
@@ -821,6 +807,7 @@ static int pm2fb_set_par(struct fb_info *info)
                txtmap = PM2F_TEXTEL_SIZE_24;
                pixsize = 4;
                clrformat = 0x20;
+               misc |= 8;
                break;
        }
        pm2_WR(par, PM2R_FB_WRITE_MODE, PM2F_FB_WRITE_ENABLE);
@@ -845,14 +832,19 @@ static int pm2fb_set_par(struct fb_info *info)
        pm2_WR(par, PM2R_SCREEN_BASE, base);
        wmb();
        set_video(par, video);
-       WAIT_FIFO(par, 4);
+       WAIT_FIFO(par, 10);
        switch (par->type) {
        case PM2_TYPE_PERMEDIA2:
                pm2_RDAC_WR(par, PM2I_RD_COLOR_MODE, clrmode);
+               pm2_RDAC_WR(par, PM2I_RD_COLOR_KEY_CONTROL,
+                               (depth == 8) ? 0 : PM2F_COLOR_KEY_TEST_OFF);
                break;
        case PM2_TYPE_PERMEDIA2V:
+               pm2v_RDAC_WR(par, PM2VI_RD_DAC_CONTROL, 0);
                pm2v_RDAC_WR(par, PM2VI_RD_PIXEL_SIZE, pixsize);
                pm2v_RDAC_WR(par, PM2VI_RD_COLOR_FORMAT, clrformat);
+               pm2v_RDAC_WR(par, PM2VI_RD_MISC_CONTROL, misc);
+               pm2v_RDAC_WR(par, PM2VI_RD_OVERLAY_KEY, 0);
                break;
        }
        set_pixclock(par, pixclock);
@@ -883,16 +875,15 @@ static int pm2fb_setcolreg(unsigned regno, unsigned red, unsigned green,
        struct pm2fb_par *par = info->par;
 
        if (regno >= info->cmap.len)  /* no. of hw registers */
-               return 1;
+               return -EINVAL;
        /*
         * Program hardware... do anything you want with transp
         */
 
        /* grayscale works only partially under directcolor */
-       if (info->var.grayscale) {
-               /* grayscale = 0.30*R + 0.59*G + 0.11*B */
+       /* grayscale = 0.30*R + 0.59*G + 0.11*B */
+       if (info->var.grayscale)
                red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
-       }
 
        /* Directcolor:
         *   var->{color}.offset contains start of bitfield
@@ -905,7 +896,7 @@ static int pm2fb_setcolreg(unsigned regno, unsigned red, unsigned green,
         * Pseudocolor:
         *    uses offset = 0 && length = DAC register width.
         *    var->{color}.offset is 0
-        *    var->{color}.length contains widht of DAC
+        *    var->{color}.length contains width of DAC
         *    cmap is not used
         *    DAC[X] is programmed to (red, green, blue)
         * Truecolor:
@@ -942,7 +933,7 @@ static int pm2fb_setcolreg(unsigned regno, unsigned red, unsigned green,
                u32 v;
 
                if (regno >= 16)
-                       return 1;
+                       return -EINVAL;
 
                v = (red << info->var.red.offset) |
                        (green << info->var.green.offset) |
@@ -959,8 +950,7 @@ static int pm2fb_setcolreg(unsigned regno, unsigned red, unsigned green,
                        break;
                }
                return 0;
-       }
-       else if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR)
+       } else if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR)
                set_color(par, regno, red, green, blue);
 
        return 0;
@@ -983,11 +973,9 @@ static int pm2fb_pan_display(struct fb_var_screeninfo *var,
 {
        struct pm2fb_par *p = info->par;
        u32 base;
-       u32 depth;
-       u32 xres;
+       u32 depth = (var->bits_per_pixel + 7) & ~7;
+       u32 xres = (var->xres + 31) & ~31;
 
-       xres = (var->xres + 31) & ~31;
-       depth = (var->bits_per_pixel + 7) & ~7;
        depth = (depth > 32) ? 32 : depth;
        base = to3264(var->yoffset * xres + var->xoffset, depth, 1);
        WAIT_FIFO(p, 1);
@@ -1029,15 +1017,15 @@ static int pm2fb_blank(int blank_mode, struct fb_info *info)
                break;
        case FB_BLANK_VSYNC_SUSPEND:
                /* VSync: Off */
-               video &= ~(PM2F_VSYNC_MASK | PM2F_BLANK_LOW );
+               video &= ~(PM2F_VSYNC_MASK | PM2F_BLANK_LOW);
                break;
        case FB_BLANK_HSYNC_SUSPEND:
                /* HSync: Off */
-               video &= ~(PM2F_HSYNC_MASK | PM2F_BLANK_LOW );
+               video &= ~(PM2F_HSYNC_MASK | PM2F_BLANK_LOW);
                break;
        case FB_BLANK_POWERDOWN:
                /* HSync: Off, VSync: Off */
-               video &= ~(PM2F_VSYNC_MASK | PM2F_HSYNC_MASK| PM2F_BLANK_LOW);
+               video &= ~(PM2F_VSYNC_MASK | PM2F_HSYNC_MASK | PM2F_BLANK_LOW);
                break;
        }
        set_video(par, video);
@@ -1053,48 +1041,20 @@ static int pm2fb_sync(struct fb_info *info)
        mb();
        do {
                while (pm2_RD(par, PM2R_OUT_FIFO_WORDS) == 0)
-                       udelay(10);
-               rmb();
+                       cpu_relax();
        } while (pm2_RD(par, PM2R_OUT_FIFO) != PM2TAG(PM2R_SYNC));
 
        return 0;
 }
 
-/*
- * block operation. copy=0: rectangle fill, copy=1: rectangle copy.
- */
-static void pm2fb_block_op(struct fb_info* info, int copy,
-                               s32 xsrc, s32 ysrc,
-                               s32 x, s32 y, s32 w, s32 h,
-                               u32 color) {
-       struct pm2fb_par *par = info->par;
-
-       if (!w || !h)
-               return;
-       WAIT_FIFO(par, 5);
-       pm2_WR(par, PM2R_CONFIG, PM2F_CONFIG_FB_WRITE_ENABLE |
-               PM2F_CONFIG_FB_READ_SOURCE_ENABLE);
-       if (copy)
-               pm2_WR(par, PM2R_FB_SOURCE_DELTA,
-                       ((ysrc-y) & 0xfff) << 16 | ((xsrc-x) & 0xfff));
-       else
-               pm2_WR(par, PM2R_FB_BLOCK_COLOR, color);
-       pm2_WR(par, PM2R_RECTANGLE_ORIGIN, (y << 16) | x);
-       pm2_WR(par, PM2R_RECTANGLE_SIZE, (h << 16) | w);
-       wmb();
-       pm2_WR(par, PM2R_RENDER, PM2F_RENDER_RECTANGLE |
-                               (x<xsrc ? PM2F_INCREASE_X : 0) |
-                               (y<ysrc ? PM2F_INCREASE_Y : 0) |
-                               (copy ? 0 : PM2F_RENDER_FASTFILL));
-}
-
-static void pm2fb_fillrect (struct fb_info *info,
+static void pm2fb_fillrect(struct fb_info *info,
                                const struct fb_fillrect *region)
 {
+       struct pm2fb_par *par = info->par;
        struct fb_fillrect modded;
        int vxres, vyres;
        u32 color = (info->fix.visual == FB_VISUAL_TRUECOLOR) ?
-               ((u32*)info->pseudo_palette)[region->color] : region->color;
+               ((u32 *)info->pseudo_palette)[region->color] : region->color;
 
        if (info->state != FBINFO_STATE_RUNNING)
                return;
@@ -1109,31 +1069,46 @@ static void pm2fb_fillrect (struct fb_info *info,
 
        memcpy(&modded, region, sizeof(struct fb_fillrect));
 
-       if(!modded.width || !modded.height ||
-          modded.dx >= vxres || modded.dy >= vyres)
+       if (!modded.width || !modded.height ||
+           modded.dx >= vxres || modded.dy >= vyres)
                return;
 
-       if(modded.dx + modded.width  > vxres)
+       if (modded.dx + modded.width  > vxres)
                modded.width  = vxres - modded.dx;
-       if(modded.dy + modded.height > vyres)
+       if (modded.dy + modded.height > vyres)
                modded.height = vyres - modded.dy;
 
-       if(info->var.bits_per_pixel == 8)
+       if (info->var.bits_per_pixel == 8)
                color |= color << 8;
-       if(info->var.bits_per_pixel <= 16)
+       if (info->var.bits_per_pixel <= 16)
                color |= color << 16;
 
-       if(info->var.bits_per_pixel != 24)
-               pm2fb_block_op(info, 0, 0, 0,
-                               modded.dx, modded.dy,
-                               modded.width, modded.height, color);
-       else
-               cfb_fillrect(info, region);
+       WAIT_FIFO(par, 3);
+       pm2_WR(par, PM2R_CONFIG, PM2F_CONFIG_FB_WRITE_ENABLE);
+       pm2_WR(par, PM2R_RECTANGLE_ORIGIN, (modded.dy << 16) | modded.dx);
+       pm2_WR(par, PM2R_RECTANGLE_SIZE, (modded.height << 16) | modded.width);
+       if (info->var.bits_per_pixel != 24) {
+               WAIT_FIFO(par, 2);
+               pm2_WR(par, PM2R_FB_BLOCK_COLOR, color);
+               wmb();
+               pm2_WR(par, PM2R_RENDER,
+                               PM2F_RENDER_RECTANGLE | PM2F_RENDER_FASTFILL);
+       } else {
+               WAIT_FIFO(par, 4);
+               pm2_WR(par, PM2R_COLOR_DDA_MODE, 1);
+               pm2_WR(par, PM2R_CONSTANT_COLOR, color);
+               wmb();
+               pm2_WR(par, PM2R_RENDER,
+                               PM2F_RENDER_RECTANGLE |
+                               PM2F_INCREASE_X | PM2F_INCREASE_Y );
+               pm2_WR(par, PM2R_COLOR_DDA_MODE, 0);
+       }
 }
 
 static void pm2fb_copyarea(struct fb_info *info,
                                const struct fb_copyarea *area)
 {
+       struct pm2fb_par *par = info->par;
        struct fb_copyarea modded;
        u32 vxres, vyres;
 
@@ -1149,23 +1124,32 @@ static void pm2fb_copyarea(struct fb_info *info,
        vxres = info->var.xres_virtual;
        vyres = info->var.yres_virtual;
 
-       if(!modded.width || !modded.height ||
-          modded.sx >= vxres || modded.sy >= vyres ||
-          modded.dx >= vxres || modded.dy >= vyres)
+       if (!modded.width || !modded.height ||
+           modded.sx >= vxres || modded.sy >= vyres ||
+           modded.dx >= vxres || modded.dy >= vyres)
                return;
 
-       if(modded.sx + modded.width > vxres)
+       if (modded.sx + modded.width > vxres)
                modded.width = vxres - modded.sx;
-       if(modded.dx + modded.width > vxres)
+       if (modded.dx + modded.width > vxres)
                modded.width = vxres - modded.dx;
-       if(modded.sy + modded.height > vyres)
+       if (modded.sy + modded.height > vyres)
                modded.height = vyres - modded.sy;
-       if(modded.dy + modded.height > vyres)
+       if (modded.dy + modded.height > vyres)
                modded.height = vyres - modded.dy;
 
-       pm2fb_block_op(info, 1, modded.sx, modded.sy,
-                       modded.dx, modded.dy,
-                       modded.width, modded.height, 0);
+       WAIT_FIFO(par, 5);
+       pm2_WR(par, PM2R_CONFIG, PM2F_CONFIG_FB_WRITE_ENABLE |
+               PM2F_CONFIG_FB_READ_SOURCE_ENABLE);
+       pm2_WR(par, PM2R_FB_SOURCE_DELTA,
+                       ((modded.sy - modded.dy) & 0xfff) << 16 |
+                       ((modded.sx - modded.dx) & 0xfff));
+       pm2_WR(par, PM2R_RECTANGLE_ORIGIN, (modded.dy << 16) | modded.dx);
+       pm2_WR(par, PM2R_RECTANGLE_SIZE, (modded.height << 16) | modded.width);
+       wmb();
+       pm2_WR(par, PM2R_RENDER, PM2F_RENDER_RECTANGLE |
+                               (modded.dx < modded.sx ? PM2F_INCREASE_X : 0) |
+                               (modded.dy < modded.sy ? PM2F_INCREASE_Y : 0));
 }
 
 static void pm2fb_imageblit(struct fb_info *info, const struct fb_image *image)
@@ -1173,8 +1157,13 @@ static void pm2fb_imageblit(struct fb_info *info, const struct fb_image *image)
        struct pm2fb_par *par = info->par;
        u32 height = image->height;
        u32 fgx, bgx;
-       const u32 *src = (const u32*)image->data;
+       const u32 *src = (const u32 *)image->data;
        u32 xres = (info->var.xres + 31) & ~31;
+       int raster_mode = 1; /* invert bits */
+
+#ifdef __LITTLE_ENDIAN
+       raster_mode |= 3 << 7; /* reverse byte order */
+#endif
 
        if (info->state != FBINFO_STATE_RUNNING)
                return;
@@ -1183,15 +1172,15 @@ static void pm2fb_imageblit(struct fb_info *info, const struct fb_image *image)
                return;
        }
        switch (info->fix.visual) {
-               case FB_VISUAL_PSEUDOCOLOR:
-                       fgx = image->fg_color;
-                       bgx = image->bg_color;
-                       break;
-               case FB_VISUAL_TRUECOLOR:
-               default:
-                       fgx = par->palette[image->fg_color];
-                       bgx = par->palette[image->bg_color];
-                       break;
+       case FB_VISUAL_PSEUDOCOLOR:
+               fgx = image->fg_color;
+               bgx = image->bg_color;
+               break;
+       case FB_VISUAL_TRUECOLOR:
+       default:
+               fgx = par->palette[image->fg_color];
+               bgx = par->palette[image->bg_color];
+               break;
        }
        if (info->var.bits_per_pixel == 8) {
                fgx |= fgx << 8;
@@ -1211,7 +1200,7 @@ static void pm2fb_imageblit(struct fb_info *info, const struct fb_image *image)
                        ((image->dx + image->width) & 0x0fff));
        pm2_WR(par, PM2R_SCISSOR_MODE, 1);
        /* GXcopy & UNIT_ENABLE */
-       pm2_WR(par, PM2R_LOGICAL_OP_MODE, (0x3 << 1) | 1 );
+       pm2_WR(par, PM2R_LOGICAL_OP_MODE, (0x3 << 1) | 1);
        pm2_WR(par, PM2R_RECTANGLE_ORIGIN,
                        ((image->dy & 0xfff) << 16) | (image->dx & 0x0fff));
        pm2_WR(par, PM2R_RECTANGLE_SIZE,
@@ -1223,10 +1212,9 @@ static void pm2fb_imageblit(struct fb_info *info, const struct fb_image *image)
                pm2_WR(par, PM2R_CONSTANT_COLOR, bgx);
                pm2_WR(par, PM2R_RENDER,
                        PM2F_RENDER_RECTANGLE |
-                       PM2F_INCREASE_X | PM2F_INCREASE_Y );
-               /* BitMapPackEachScanline & invert bits and byte order*/
-               /* force background */
-               pm2_WR(par, PM2R_RASTERIZER_MODE,  (1<<9) | 1 | (3<<7));
+                       PM2F_INCREASE_X | PM2F_INCREASE_Y);
+               /* BitMapPackEachScanline */
+               pm2_WR(par, PM2R_RASTERIZER_MODE, raster_mode | (1 << 9));
                pm2_WR(par, PM2R_CONSTANT_COLOR, fgx);
                pm2_WR(par, PM2R_RENDER,
                        PM2F_RENDER_RECTANGLE |
@@ -1239,9 +1227,8 @@ static void pm2fb_imageblit(struct fb_info *info, const struct fb_image *image)
                pm2_WR(par, PM2R_RENDER,
                        PM2F_RENDER_RECTANGLE |
                        PM2F_RENDER_FASTFILL |
-                       PM2F_INCREASE_X | PM2F_INCREASE_Y );
-               /* invert bits and byte order*/
-               pm2_WR(par, PM2R_RASTERIZER_MODE,  1 | (3<<7) );
+                       PM2F_INCREASE_X | PM2F_INCREASE_Y);
+               pm2_WR(par, PM2R_RASTERIZER_MODE, raster_mode);
                pm2_WR(par, PM2R_FB_BLOCK_COLOR, fgx);
                pm2_WR(par, PM2R_RENDER,
                        PM2F_RENDER_RECTANGLE |
@@ -1266,6 +1253,235 @@ static void pm2fb_imageblit(struct fb_info *info, const struct fb_image *image)
        pm2_WR(par, PM2R_SCISSOR_MODE, 0);
 }
 
+/*
+ *     Hardware cursor support.
+ */
+static const u8 cursor_bits_lookup[16] = {
+       0x00, 0x40, 0x10, 0x50, 0x04, 0x44, 0x14, 0x54,
+       0x01, 0x41, 0x11, 0x51, 0x05, 0x45, 0x15, 0x55
+};
+
+static int pm2vfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
+{
+       struct pm2fb_par *par = info->par;
+       u8 mode = PM2F_CURSORMODE_TYPE_X;
+       int x = cursor->image.dx - info->var.xoffset;
+       int y = cursor->image.dy - info->var.yoffset;
+
+       if (cursor->enable)
+               mode |= PM2F_CURSORMODE_CURSOR_ENABLE;
+
+       pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_MODE, mode);
+
+       if (!cursor->enable)
+               x = 2047;       /* push it outside display */
+       pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_X_LOW, x & 0xff);
+       pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_X_HIGH, (x >> 8) & 0xf);
+       pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_Y_LOW, y & 0xff);
+       pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_Y_HIGH, (y >> 8) & 0xf);
+
+       /*
+        * If the cursor is not be changed this means either we want the
+        * current cursor state (if enable is set) or we want to query what
+        * we can do with the cursor (if enable is not set)
+        */
+       if (!cursor->set)
+               return 0;
+
+       if (cursor->set & FB_CUR_SETHOT) {
+               pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_X_HOT,
+                            cursor->hot.x & 0x3f);
+               pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_Y_HOT,
+                            cursor->hot.y & 0x3f);
+       }
+
+       if (cursor->set & FB_CUR_SETCMAP) {
+               u32 fg_idx = cursor->image.fg_color;
+               u32 bg_idx = cursor->image.bg_color;
+               struct fb_cmap cmap = info->cmap;
+
+               /* the X11 driver says one should use these color registers */
+               pm2_WR(par, PM2VR_RD_INDEX_HIGH, PM2VI_RD_CURSOR_PALETTE >> 8);
+               pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_PALETTE + 0,
+                            cmap.red[bg_idx] >> 8 );
+               pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_PALETTE + 1,
+                            cmap.green[bg_idx] >> 8 );
+               pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_PALETTE + 2,
+                            cmap.blue[bg_idx] >> 8 );
+
+               pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_PALETTE + 3,
+                            cmap.red[fg_idx] >> 8 );
+               pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_PALETTE + 4,
+                            cmap.green[fg_idx] >> 8 );
+               pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_PALETTE + 5,
+                            cmap.blue[fg_idx] >> 8 );
+               pm2_WR(par, PM2VR_RD_INDEX_HIGH, 0);
+       }
+
+       if (cursor->set & (FB_CUR_SETSHAPE | FB_CUR_SETIMAGE)) {
+               u8 *bitmap = (u8 *)cursor->image.data;
+               u8 *mask = (u8 *)cursor->mask;
+               int i;
+               int pos = PM2VI_RD_CURSOR_PATTERN;
+
+               for (i = 0; i < cursor->image.height; i++) {
+                       int j = (cursor->image.width + 7) >> 3;
+                       int k = 8 - j;
+
+                       pm2_WR(par, PM2VR_RD_INDEX_HIGH, pos >> 8);
+
+                       for (; j > 0; j--) {
+                               u8 data = *bitmap ^ *mask;
+
+                               if (cursor->rop == ROP_COPY)
+                                       data = *mask & *bitmap;
+                               /* Upper 4 bits of bitmap data */
+                               pm2v_RDAC_WR(par, pos++,
+                                       cursor_bits_lookup[data >> 4] |
+                                       (cursor_bits_lookup[*mask >> 4] << 1));
+                               /* Lower 4 bits of bitmap */
+                               pm2v_RDAC_WR(par, pos++,
+                                       cursor_bits_lookup[data & 0xf] |
+                                       (cursor_bits_lookup[*mask & 0xf] << 1));
+                               bitmap++;
+                               mask++;
+                       }
+                       for (; k > 0; k--) {
+                               pm2v_RDAC_WR(par, pos++, 0);
+                               pm2v_RDAC_WR(par, pos++, 0);
+                       }
+               }
+
+               while (pos < (1024 + PM2VI_RD_CURSOR_PATTERN)) {
+                       pm2_WR(par, PM2VR_RD_INDEX_HIGH, pos >> 8);
+                       pm2v_RDAC_WR(par, pos++, 0);
+               }
+
+               pm2_WR(par, PM2VR_RD_INDEX_HIGH, 0);
+       }
+       return 0;
+}
+
+static int pm2fb_cursor(struct fb_info *info, struct fb_cursor *cursor)
+{
+       struct pm2fb_par *par = info->par;
+       u8 mode;
+
+       if (!hwcursor)
+               return -EINVAL; /* just to force soft_cursor() call */
+
+       /* Too large of a cursor or wrong bpp :-( */
+       if (cursor->image.width > 64 ||
+           cursor->image.height > 64 ||
+           cursor->image.depth > 1)
+               return -EINVAL;
+
+       if (par->type == PM2_TYPE_PERMEDIA2V)
+               return pm2vfb_cursor(info, cursor);
+
+       mode = 0x40;
+       if (cursor->enable)
+                mode = 0x43;
+
+       pm2_RDAC_WR(par, PM2I_RD_CURSOR_CONTROL, mode);
+
+       /*
+        * If the cursor is not be changed this means either we want the
+        * current cursor state (if enable is set) or we want to query what
+        * we can do with the cursor (if enable is not set)
+        */
+       if (!cursor->set)
+               return 0;
+
+       if (cursor->set & FB_CUR_SETPOS) {
+               int x = cursor->image.dx - info->var.xoffset + 63;
+               int y = cursor->image.dy - info->var.yoffset + 63;
+
+               WAIT_FIFO(par, 4);
+               pm2_WR(par, PM2R_RD_CURSOR_X_LSB, x & 0xff);
+               pm2_WR(par, PM2R_RD_CURSOR_X_MSB, (x >> 8) & 0x7);
+               pm2_WR(par, PM2R_RD_CURSOR_Y_LSB, y & 0xff);
+               pm2_WR(par, PM2R_RD_CURSOR_Y_MSB, (y >> 8) & 0x7);
+       }
+
+       if (cursor->set & FB_CUR_SETCMAP) {
+               u32 fg_idx = cursor->image.fg_color;
+               u32 bg_idx = cursor->image.bg_color;
+
+               WAIT_FIFO(par, 7);
+               pm2_WR(par, PM2R_RD_CURSOR_COLOR_ADDRESS, 1);
+               pm2_WR(par, PM2R_RD_CURSOR_COLOR_DATA,
+                       info->cmap.red[bg_idx] >> 8);
+               pm2_WR(par, PM2R_RD_CURSOR_COLOR_DATA,
+                       info->cmap.green[bg_idx] >> 8);
+               pm2_WR(par, PM2R_RD_CURSOR_COLOR_DATA,
+                       info->cmap.blue[bg_idx] >> 8);
+
+               pm2_WR(par, PM2R_RD_CURSOR_COLOR_DATA,
+                       info->cmap.red[fg_idx] >> 8);
+               pm2_WR(par, PM2R_RD_CURSOR_COLOR_DATA,
+                       info->cmap.green[fg_idx] >> 8);
+               pm2_WR(par, PM2R_RD_CURSOR_COLOR_DATA,
+                       info->cmap.blue[fg_idx] >> 8);
+       }
+
+       if (cursor->set & (FB_CUR_SETSHAPE | FB_CUR_SETIMAGE)) {
+               u8 *bitmap = (u8 *)cursor->image.data;
+               u8 *mask = (u8 *)cursor->mask;
+               int i;
+
+               WAIT_FIFO(par, 1);
+               pm2_WR(par, PM2R_RD_PALETTE_WRITE_ADDRESS, 0);
+
+               for (i = 0; i < cursor->image.height; i++) {
+                       int j = (cursor->image.width + 7) >> 3;
+                       int k = 8 - j;
+
+                       WAIT_FIFO(par, 8);
+                       for (; j > 0; j--) {
+                               u8 data = *bitmap ^ *mask;
+
+                               if (cursor->rop == ROP_COPY)
+                                       data = *mask & *bitmap;
+                               /* bitmap data */
+                               pm2_WR(par, PM2R_RD_CURSOR_DATA, data);
+                               bitmap++;
+                               mask++;
+                       }
+                       for (; k > 0; k--)
+                               pm2_WR(par, PM2R_RD_CURSOR_DATA, 0);
+               }
+               for (; i < 64; i++) {
+                       int j = 8;
+                       WAIT_FIFO(par, 8);
+                       while (j-- > 0)
+                               pm2_WR(par, PM2R_RD_CURSOR_DATA, 0);
+               }
+
+               mask = (u8 *)cursor->mask;
+               for (i = 0; i < cursor->image.height; i++) {
+                       int j = (cursor->image.width + 7) >> 3;
+                       int k = 8 - j;
+
+                       WAIT_FIFO(par, 8);
+                       for (; j > 0; j--) {
+                               /* mask */
+                               pm2_WR(par, PM2R_RD_CURSOR_DATA, *mask);
+                               mask++;
+                       }
+                       for (; k > 0; k--)
+                               pm2_WR(par, PM2R_RD_CURSOR_DATA, 0);
+               }
+               for (; i < 64; i++) {
+                       int j = 8;
+                       WAIT_FIFO(par, 8);
+                       while (j-- > 0)
+                               pm2_WR(par, PM2R_RD_CURSOR_DATA, 0);
+               }
+       }
+       return 0;
+}
+
 /* ------------ Hardware Independent Functions ------------ */
 
 /*
@@ -1283,6 +1499,7 @@ static struct fb_ops pm2fb_ops = {
        .fb_copyarea    = pm2fb_copyarea,
        .fb_imageblit   = pm2fb_imageblit,
        .fb_sync        = pm2fb_sync,
+       .fb_cursor      = pm2fb_cursor,
 };
 
 /*
@@ -1303,16 +1520,17 @@ static int __devinit pm2fb_probe(struct pci_dev *pdev,
 {
        struct pm2fb_par *default_par;
        struct fb_info *info;
-       int err, err_retval = -ENXIO;
+       int err;
+       int retval = -ENXIO;
 
        err = pci_enable_device(pdev);
-       if ( err ) {
+       if (err) {
                printk(KERN_WARNING "pm2fb: Can't enable pdev: %d\n", err);
                return err;
        }
 
        info = framebuffer_alloc(sizeof(struct pm2fb_par), &pdev->dev);
-       if ( !info )
+       if (!info)
                return -ENOMEM;
        default_par = info->par;
 
@@ -1345,14 +1563,14 @@ static int __devinit pm2fb_probe(struct pci_dev *pdev,
        DPRINTK("Register base at 0x%lx\n", pm2fb_fix.mmio_start);
 
        /* Registers - request region and map it. */
-       if ( !request_mem_region(pm2fb_fix.mmio_start, pm2fb_fix.mmio_len,
-                                "pm2fb regbase") ) {
+       if (!request_mem_region(pm2fb_fix.mmio_start, pm2fb_fix.mmio_len,
+                               "pm2fb regbase")) {
                printk(KERN_WARNING "pm2fb: Can't reserve regbase.\n");
                goto err_exit_neither;
        }
        default_par->v_regs =
                ioremap_nocache(pm2fb_fix.mmio_start, pm2fb_fix.mmio_len);
-       if ( !default_par->v_regs ) {
+       if (!default_par->v_regs) {
                printk(KERN_WARNING "pm2fb: Can't remap %s register area.\n",
                       pm2fb_fix.id);
                release_mem_region(pm2fb_fix.mmio_start, pm2fb_fix.mmio_len);
@@ -1367,59 +1585,61 @@ static int __devinit pm2fb_probe(struct pci_dev *pdev,
                default_par->mem_control, default_par->boot_address,
                default_par->mem_config);
 
-       if(default_par->mem_control == 0 &&
+       if (default_par->mem_control == 0 &&
                default_par->boot_address == 0x31 &&
                default_par->mem_config == 0x259fffff) {
                default_par->memclock = CVPPC_MEMCLOCK;
-               default_par->mem_control=0;
-               default_par->boot_address=0x20;
-               default_par->mem_config=0xe6002021;
+               default_par->mem_control = 0;
+               default_par->boot_address = 0x20;
+               default_par->mem_config = 0xe6002021;
                if (pdev->subsystem_vendor == 0x1048 &&
                        pdev->subsystem_device == 0x0a31) {
-                       DPRINTK("subsystem_vendor: %04x, subsystem_device: %04x\n",
+                       DPRINTK("subsystem_vendor: %04x, "
+                               "subsystem_device: %04x\n",
                                pdev->subsystem_vendor, pdev->subsystem_device);
-                       DPRINTK("We have not been initialized by VGA BIOS "
-                               "and are running on an Elsa Winner 2000 Office\n");
+                       DPRINTK("We have not been initialized by VGA BIOS and "
+                               "are running on an Elsa Winner 2000 Office\n");
                        DPRINTK("Initializing card timings manually...\n");
-                       default_par->memclock=70000;
+                       default_par->memclock = 100000;
                }
                if (pdev->subsystem_vendor == 0x3d3d &&
                        pdev->subsystem_device == 0x0100) {
-                       DPRINTK("subsystem_vendor: %04x, subsystem_device: %04x\n",
+                       DPRINTK("subsystem_vendor: %04x, "
+                               "subsystem_device: %04x\n",
                                pdev->subsystem_vendor, pdev->subsystem_device);
-                       DPRINTK("We have not been initialized by VGA BIOS "
-                               "and are running on an 3dlabs reference board\n");
+                       DPRINTK("We have not been initialized by VGA BIOS and "
+                               "are running on an 3dlabs reference board\n");
                        DPRINTK("Initializing card timings manually...\n");
-                       default_par->memclock=74894;
+                       default_par->memclock = 74894;
                }
        }
 
        /* Now work out how big lfb is going to be. */
-       switch(default_par->mem_config & PM2F_MEM_CONFIG_RAM_MASK) {
+       switch (default_par->mem_config & PM2F_MEM_CONFIG_RAM_MASK) {
        case PM2F_MEM_BANKS_1:
-               pm2fb_fix.smem_len=0x200000;
+               pm2fb_fix.smem_len = 0x200000;
                break;
        case PM2F_MEM_BANKS_2:
-               pm2fb_fix.smem_len=0x400000;
+               pm2fb_fix.smem_len = 0x400000;
                break;
        case PM2F_MEM_BANKS_3:
-               pm2fb_fix.smem_len=0x600000;
+               pm2fb_fix.smem_len = 0x600000;
                break;
        case PM2F_MEM_BANKS_4:
-               pm2fb_fix.smem_len=0x800000;
+               pm2fb_fix.smem_len = 0x800000;
                break;
        }
        pm2fb_fix.smem_start = pci_resource_start(pdev, 1);
 
        /* Linear frame buffer - request region and map it. */
-       if ( !request_mem_region(pm2fb_fix.smem_start, pm2fb_fix.smem_len,
-                                "pm2fb smem") ) {
+       if (!request_mem_region(pm2fb_fix.smem_start, pm2fb_fix.smem_len,
+                               "pm2fb smem")) {
                printk(KERN_WARNING "pm2fb: Can't reserve smem.\n");
                goto err_exit_mmio;
        }
        info->screen_base =
                ioremap_nocache(pm2fb_fix.smem_start, pm2fb_fix.smem_len);
-       if ( !info->screen_base ) {
+       if (!info->screen_base) {
                printk(KERN_WARNING "pm2fb: Can't ioremap smem area.\n");
                release_mem_region(pm2fb_fix.smem_start, pm2fb_fix.smem_len);
                goto err_exit_mmio;
@@ -1445,7 +1665,7 @@ static int __devinit pm2fb_probe(struct pci_dev *pdev,
 
        info->pixmap.addr = kmalloc(PM2_PIXMAP_SIZE, GFP_KERNEL);
        if (!info->pixmap.addr) {
-               err_retval = -ENOMEM;
+               retval = -ENOMEM;
                goto err_exit_pixmap;
        }
        info->pixmap.size = PM2_PIXMAP_SIZE;
@@ -1460,17 +1680,19 @@ static int __devinit pm2fb_probe(struct pci_dev *pdev,
                info->pixmap.scan_align = 1;
        }
 
-       if (!mode)
-               mode = "640x480@60";
+       if (!mode_option)
+               mode_option = "640x480@60";
 
-       err = fb_find_mode(&info->var, info, mode, NULL, 0, NULL, 8);
+       err = fb_find_mode(&info->var, info, mode_option, NULL, 0, NULL, 8);
        if (!err || err == 4)
                info->var = pm2fb_var;
 
-       if (fb_alloc_cmap(&info->cmap, 256, 0) < 0)
+       retval = fb_alloc_cmap(&info->cmap, 256, 0);
+       if (retval < 0)
                goto err_exit_both;
 
-       if (register_framebuffer(info) < 0)
+       retval = register_framebuffer(info);
+       if (retval < 0)
                goto err_exit_all;
 
        printk(KERN_INFO "fb%d: %s frame buffer device, memory = %dK.\n",
@@ -1495,7 +1717,7 @@ static int __devinit pm2fb_probe(struct pci_dev *pdev,
        release_mem_region(pm2fb_fix.mmio_start, pm2fb_fix.mmio_len);
  err_exit_neither:
        framebuffer_release(info);
-       return err_retval;
+       return retval;
 }
 
 /**
@@ -1507,8 +1729,8 @@ static int __devinit pm2fb_probe(struct pci_dev *pdev,
  */
 static void __devexit pm2fb_remove(struct pci_dev *pdev)
 {
-       struct fb_infoinfo = pci_get_drvdata(pdev);
-       struct fb_fix_screeninfofix = &info->fix;
+       struct fb_info *info = pci_get_drvdata(pdev);
+       struct fb_fix_screeninfo *fix = &info->fix;
        struct pm2fb_par *par = info->par;
 
        unregister_framebuffer(info);
@@ -1524,24 +1746,18 @@ static void __devexit pm2fb_remove(struct pci_dev *pdev)
        release_mem_region(fix->mmio_start, fix->mmio_len);
 
        pci_set_drvdata(pdev, NULL);
-       if (info->pixmap.addr)
-               kfree(info->pixmap.addr);
-       kfree(info);
+       fb_dealloc_cmap(&info->cmap);
+       kfree(info->pixmap.addr);
+       framebuffer_release(info);
 }
 
 static struct pci_device_id pm2fb_id_table[] = {
        { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_TVP4020,
-         PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
-         0xff0000, 0 },
+         PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
        { PCI_VENDOR_ID_3DLABS, PCI_DEVICE_ID_3DLABS_PERMEDIA2,
-         PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
-         0xff0000, 0 },
+         PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
        { PCI_VENDOR_ID_3DLABS, PCI_DEVICE_ID_3DLABS_PERMEDIA2V,
-         PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
-         0xff0000, 0 },
-       { PCI_VENDOR_ID_3DLABS, PCI_DEVICE_ID_3DLABS_PERMEDIA2V,
-         PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_NOT_DEFINED_VGA << 8,
-         0xff00, 0 },
+         PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
        { 0, }
 };
 
@@ -1563,7 +1779,7 @@ MODULE_DEVICE_TABLE(pci, pm2fb_id_table);
  */
 static int __init pm2fb_setup(char *options)
 {
-       charthis_opt;
+       char *this_opt;
 
        if (!options || !*options)
                return 0;
@@ -1571,19 +1787,20 @@ static int __init pm2fb_setup(char *options)
        while ((this_opt = strsep(&options, ",")) != NULL) {
                if (!*this_opt)
                        continue;
-               if(!strcmp(this_opt, "lowhsync")) {
+               if (!strcmp(this_opt, "lowhsync"))
                        lowhsync = 1;
-               } else if(!strcmp(this_opt, "lowvsync")) {
+               else if (!strcmp(this_opt, "lowvsync"))
                        lowvsync = 1;
+               else if (!strncmp(this_opt, "hwcursor=", 9))
+                       hwcursor = simple_strtoul(this_opt + 9, NULL, 0);
 #ifdef CONFIG_MTRR
-               } else if (!strncmp(this_opt, "nomtrr", 6)) {
+               else if (!strncmp(this_opt, "nomtrr", 6))
                        nomtrr = 1;
 #endif
-               } else if (!strncmp(this_opt, "noaccel", 7)) {
+               else if (!strncmp(this_opt, "noaccel", 7))
                        noaccel = 1;
-               } else {
-                       mode = this_opt;
-               }
+               else
+                       mode_option = this_opt;
        }
        return 0;
 }
@@ -1619,14 +1836,19 @@ static void __exit pm2fb_exit(void)
 #ifdef MODULE
 module_exit(pm2fb_exit);
 
-module_param(mode, charp, 0);
-MODULE_PARM_DESC(mode, "Preferred video mode e.g. '648x480-8@60'");
+module_param(mode_option, charp, 0);
+MODULE_PARM_DESC(mode_option, "Initial video mode e.g. '648x480-8@60'");
+module_param_named(mode, mode_option, charp, 0);
+MODULE_PARM_DESC(mode, "Initial video mode e.g. '648x480-8@60' (deprecated)");
 module_param(lowhsync, bool, 0);
 MODULE_PARM_DESC(lowhsync, "Force horizontal sync low regardless of mode");
 module_param(lowvsync, bool, 0);
 MODULE_PARM_DESC(lowvsync, "Force vertical sync low regardless of mode");
 module_param(noaccel, bool, 0);
 MODULE_PARM_DESC(noaccel, "Disable acceleration");
+module_param(hwcursor, int, 0644);
+MODULE_PARM_DESC(hwcursor, "Enable hardware cursor "
+                       "(1=enable, 0=disable, default=1)");
 #ifdef CONFIG_MTRR
 module_param(nomtrr, bool, 0);
 MODULE_PARM_DESC(nomtrr, "Disable MTRR support (0 or 1=disabled) (default=0)");