2  *  linux/drivers/video/acornfb.c
 
   4  *  Copyright (C) 1998-2001 Russell King
 
   6  * This program is free software; you can redistribute it and/or modify
 
   7  * it under the terms of the GNU General Public License version 2 as
 
   8  * published by the Free Software Foundation.
 
  10  * Frame buffer code for Acorn platforms
 
  12  * NOTE: Most of the modes with X!=640 will disappear shortly.
 
  13  * NOTE: Startup setting of HS & VS polarity not supported.
 
  14  *       (do we need to support it if we're coming up in 640x480?)
 
  16  * FIXME: (things broken by the "new improved" FBCON API)
 
  17  *  - Blanking 8bpp displays with VIDC
 
  20 #include <linux/module.h>
 
  21 #include <linux/kernel.h>
 
  22 #include <linux/errno.h>
 
  23 #include <linux/string.h>
 
  24 #include <linux/ctype.h>
 
  25 #include <linux/slab.h>
 
  27 #include <linux/init.h>
 
  29 #include <linux/platform_device.h>
 
  30 #include <linux/dma-mapping.h>
 
  32 #include <asm/hardware.h>
 
  35 #include <asm/mach-types.h>
 
  36 #include <asm/pgtable.h>
 
  41  * VIDC machines can't do 16 or 32BPP modes.
 
  44 #undef FBCON_HAS_CFB16
 
  45 #undef FBCON_HAS_CFB32
 
  50  * NOTE that it has to be supported in the table towards
 
  51  * the end of this file.
 
  53 #define DEFAULT_XRES    640
 
  54 #define DEFAULT_YRES    480
 
  58  * define this to debug the video mode selection
 
  60 #undef DEBUG_MODE_SELECTION
 
  63  * Translation from RISC OS monitor types to actual
 
  64  * HSYNC and VSYNC frequency ranges.  These are
 
  65  * probably not right, but they're the best info I
 
  66  * have.  Allow 1% either way on the nominal for TVs.
 
  69 static struct fb_monspecs monspecs[NR_MONTYPES] __initdata = {
 
  80         }, {    /* Hi-res mono  */
 
 103 static struct fb_info fb_info;
 
 104 static struct acornfb_par current_par;
 
 105 static struct vidc_timing current_vidc;
 
 107 extern unsigned int vram_size;  /* set by setup.c */
 
 111 #define MAX_SIZE        480*1024
 
 134 static struct pixclock arc_clocks[] = {
 
 135         /* we allow +/-1% on these */
 
 136         { 123750, 126250, VIDC_CTRL_DIV3,   VID_CTL_24MHz },    /*  8.000MHz */
 
 137         {  82500,  84167, VIDC_CTRL_DIV2,   VID_CTL_24MHz },    /* 12.000MHz */
 
 138         {  61875,  63125, VIDC_CTRL_DIV1_5, VID_CTL_24MHz },    /* 16.000MHz */
 
 139         {  41250,  42083, VIDC_CTRL_DIV1,   VID_CTL_24MHz },    /* 24.000MHz */
 
 142 static struct pixclock *
 
 143 acornfb_valid_pixrate(struct fb_var_screeninfo *var)
 
 145         u_long pixclock = var->pixclock;
 
 151         for (i = 0; i < ARRAY_SIZE(arc_clocks); i++)
 
 152                 if (pixclock > arc_clocks[i].min_clock &&
 
 153                     pixclock < arc_clocks[i].max_clock)
 
 154                         return arc_clocks + i;
 
 160  * hcr  : must be even (interlace, hcr/2 must be even)
 
 161  * hswr : must be even
 
 169  * if interlaced, then hcr/2 must be even
 
 172 acornfb_set_timing(struct fb_var_screeninfo *var)
 
 174         struct pixclock *pclk;
 
 175         struct vidc_timing vidc;
 
 176         u_int horiz_correction;
 
 177         u_int sync_len, display_start, display_end, cycle;
 
 179         u_int vid_ctl, vidc_ctl;
 
 182         memset(&vidc, 0, sizeof(vidc));
 
 184         pclk = acornfb_valid_pixrate(var);
 
 185         vidc_ctl = pclk->vidc_ctl;
 
 186         vid_ctl  = pclk->vid_ctl;
 
 188         bandwidth = var->pixclock * 8 / var->bits_per_pixel;
 
 189         /* 25.175, 4bpp = 79.444ns per byte, 317.776ns per word: fifo = 2,6 */
 
 190         if (bandwidth > 143500)
 
 191                 vidc_ctl |= VIDC_CTRL_FIFO_3_7;
 
 192         else if (bandwidth > 71750)
 
 193                 vidc_ctl |= VIDC_CTRL_FIFO_2_6;
 
 194         else if (bandwidth > 35875)
 
 195                 vidc_ctl |= VIDC_CTRL_FIFO_1_5;
 
 197                 vidc_ctl |= VIDC_CTRL_FIFO_0_4;
 
 199         switch (var->bits_per_pixel) {
 
 201                 horiz_correction = 19;
 
 202                 vidc_ctl |= VIDC_CTRL_1BPP;
 
 206                 horiz_correction = 11;
 
 207                 vidc_ctl |= VIDC_CTRL_2BPP;
 
 211                 horiz_correction = 7;
 
 212                 vidc_ctl |= VIDC_CTRL_4BPP;
 
 217                 horiz_correction = 5;
 
 218                 vidc_ctl |= VIDC_CTRL_8BPP;
 
 222         if (var->sync & FB_SYNC_COMP_HIGH_ACT) /* should be FB_SYNC_COMP */
 
 223                 vidc_ctl |= VIDC_CTRL_CSYNC;
 
 225                 if (!(var->sync & FB_SYNC_HOR_HIGH_ACT))
 
 226                         vid_ctl |= VID_CTL_HS_NHSYNC;
 
 228                 if (!(var->sync & FB_SYNC_VERT_HIGH_ACT))
 
 229                         vid_ctl |= VID_CTL_VS_NVSYNC;
 
 232         sync_len        = var->hsync_len;
 
 233         display_start   = sync_len + var->left_margin;
 
 234         display_end     = display_start + var->xres;
 
 235         cycle           = display_end + var->right_margin;
 
 237         /* if interlaced, then hcr/2 must be even */
 
 238         is_interlaced = (var->vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED;
 
 241                 vidc_ctl |= VIDC_CTRL_INTERLACE;
 
 244                         var->right_margin += 2;
 
 248         vidc.h_cycle            = (cycle - 2) / 2;
 
 249         vidc.h_sync_width       = (sync_len - 2) / 2;
 
 250         vidc.h_border_start     = (display_start - 1) / 2;
 
 251         vidc.h_display_start    = (display_start - horiz_correction) / 2;
 
 252         vidc.h_display_end      = (display_end - horiz_correction) / 2;
 
 253         vidc.h_border_end       = (display_end - 1) / 2;
 
 254         vidc.h_interlace        = (vidc.h_cycle + 1) / 2;
 
 256         sync_len        = var->vsync_len;
 
 257         display_start   = sync_len + var->upper_margin;
 
 258         display_end     = display_start + var->yres;
 
 259         cycle           = display_end + var->lower_margin;
 
 262                 cycle = (cycle - 3) / 2;
 
 266         vidc.v_cycle            = cycle;
 
 267         vidc.v_sync_width       = sync_len - 1;
 
 268         vidc.v_border_start     = display_start - 1;
 
 269         vidc.v_display_start    = vidc.v_border_start;
 
 270         vidc.v_display_end      = display_end - 1;
 
 271         vidc.v_border_end       = vidc.v_display_end;
 
 273         if (machine_is_a5k())
 
 274                 __raw_writeb(vid_ctl, IOEB_VID_CTL);
 
 276         if (memcmp(¤t_vidc, &vidc, sizeof(vidc))) {
 
 279                 vidc_writel(0xe0000000 | vidc_ctl);
 
 280                 vidc_writel(0x80000000 | (vidc.h_cycle << 14));
 
 281                 vidc_writel(0x84000000 | (vidc.h_sync_width << 14));
 
 282                 vidc_writel(0x88000000 | (vidc.h_border_start << 14));
 
 283                 vidc_writel(0x8c000000 | (vidc.h_display_start << 14));
 
 284                 vidc_writel(0x90000000 | (vidc.h_display_end << 14));
 
 285                 vidc_writel(0x94000000 | (vidc.h_border_end << 14));
 
 286                 vidc_writel(0x98000000);
 
 287                 vidc_writel(0x9c000000 | (vidc.h_interlace << 14));
 
 288                 vidc_writel(0xa0000000 | (vidc.v_cycle << 14));
 
 289                 vidc_writel(0xa4000000 | (vidc.v_sync_width << 14));
 
 290                 vidc_writel(0xa8000000 | (vidc.v_border_start << 14));
 
 291                 vidc_writel(0xac000000 | (vidc.v_display_start << 14));
 
 292                 vidc_writel(0xb0000000 | (vidc.v_display_end << 14));
 
 293                 vidc_writel(0xb4000000 | (vidc.v_border_end << 14));
 
 294                 vidc_writel(0xb8000000);
 
 295                 vidc_writel(0xbc000000);
 
 297 #ifdef DEBUG_MODE_SELECTION
 
 298         printk(KERN_DEBUG "VIDC registers for %dx%dx%d:\n", var->xres,
 
 299                var->yres, var->bits_per_pixel);
 
 300         printk(KERN_DEBUG " H-cycle          : %d\n", vidc.h_cycle);
 
 301         printk(KERN_DEBUG " H-sync-width     : %d\n", vidc.h_sync_width);
 
 302         printk(KERN_DEBUG " H-border-start   : %d\n", vidc.h_border_start);
 
 303         printk(KERN_DEBUG " H-display-start  : %d\n", vidc.h_display_start);
 
 304         printk(KERN_DEBUG " H-display-end    : %d\n", vidc.h_display_end);
 
 305         printk(KERN_DEBUG " H-border-end     : %d\n", vidc.h_border_end);
 
 306         printk(KERN_DEBUG " H-interlace      : %d\n", vidc.h_interlace);
 
 307         printk(KERN_DEBUG " V-cycle          : %d\n", vidc.v_cycle);
 
 308         printk(KERN_DEBUG " V-sync-width     : %d\n", vidc.v_sync_width);
 
 309         printk(KERN_DEBUG " V-border-start   : %d\n", vidc.v_border_start);
 
 310         printk(KERN_DEBUG " V-display-start  : %d\n", vidc.v_display_start);
 
 311         printk(KERN_DEBUG " V-display-end    : %d\n", vidc.v_display_end);
 
 312         printk(KERN_DEBUG " V-border-end     : %d\n", vidc.v_border_end);
 
 313         printk(KERN_DEBUG " VIDC Ctrl (E)    : 0x%08X\n", vidc_ctl);
 
 314         printk(KERN_DEBUG " IOEB Ctrl        : 0x%08X\n", vid_ctl);
 
 319 acornfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
 
 320                   u_int trans, struct fb_info *info)
 
 324         if (regno >= current_par.palette_size)
 
 328         pal.vidc.reg   = regno;
 
 329         pal.vidc.red   = red >> 12;
 
 330         pal.vidc.green = green >> 12;
 
 331         pal.vidc.blue  = blue >> 12;
 
 333         current_par.palette[regno] = pal;
 
 342 #include <asm/arch/acornfb.h>
 
 344 #define MAX_SIZE        2*1024*1024
 
 346 /* VIDC20 has a different set of rules from the VIDC:
 
 347  *  hcr  : must be multiple of 4
 
 348  *  hswr : must be even
 
 349  *  hdsr : must be even
 
 350  *  hder : must be even
 
 351  *  vcr  : >= 2, (interlace, must be odd)
 
 356 static void acornfb_set_timing(struct fb_info *info)
 
 358         struct fb_var_screeninfo *var = &info->var;
 
 359         struct vidc_timing vidc;
 
 361         u_int ext_ctl, dat_ctl;
 
 362         u_int words_per_line;
 
 364         memset(&vidc, 0, sizeof(vidc));
 
 366         vidc.h_sync_width       = var->hsync_len - 8;
 
 367         vidc.h_border_start     = vidc.h_sync_width + var->left_margin + 8 - 12;
 
 368         vidc.h_display_start    = vidc.h_border_start + 12 - 18;
 
 369         vidc.h_display_end      = vidc.h_display_start + var->xres;
 
 370         vidc.h_border_end       = vidc.h_display_end + 18 - 12;
 
 371         vidc.h_cycle            = vidc.h_border_end + var->right_margin + 12 - 8;
 
 372         vidc.h_interlace        = vidc.h_cycle / 2;
 
 373         vidc.v_sync_width       = var->vsync_len - 1;
 
 374         vidc.v_border_start     = vidc.v_sync_width + var->upper_margin;
 
 375         vidc.v_display_start    = vidc.v_border_start;
 
 376         vidc.v_display_end      = vidc.v_display_start + var->yres;
 
 377         vidc.v_border_end       = vidc.v_display_end;
 
 378         vidc.control            = acornfb_default_control();
 
 380         vcr = var->vsync_len + var->upper_margin + var->yres +
 
 383         if ((var->vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) {
 
 384                 vidc.v_cycle = (vcr - 3) / 2;
 
 385                 vidc.control |= VIDC20_CTRL_INT;
 
 387                 vidc.v_cycle = vcr - 2;
 
 389         switch (var->bits_per_pixel) {
 
 390         case  1: vidc.control |= VIDC20_CTRL_1BPP;      break;
 
 391         case  2: vidc.control |= VIDC20_CTRL_2BPP;      break;
 
 392         case  4: vidc.control |= VIDC20_CTRL_4BPP;      break;
 
 394         case  8: vidc.control |= VIDC20_CTRL_8BPP;      break;
 
 395         case 16: vidc.control |= VIDC20_CTRL_16BPP;     break;
 
 396         case 32: vidc.control |= VIDC20_CTRL_32BPP;     break;
 
 399         acornfb_vidc20_find_rates(&vidc, var);
 
 400         fsize = var->vsync_len + var->upper_margin + var->lower_margin - 1;
 
 402         if (memcmp(¤t_vidc, &vidc, sizeof(vidc))) {
 
 405                 vidc_writel(VIDC20_CTRL| vidc.control);
 
 406                 vidc_writel(0xd0000000 | vidc.pll_ctl);
 
 407                 vidc_writel(0x80000000 | vidc.h_cycle);
 
 408                 vidc_writel(0x81000000 | vidc.h_sync_width);
 
 409                 vidc_writel(0x82000000 | vidc.h_border_start);
 
 410                 vidc_writel(0x83000000 | vidc.h_display_start);
 
 411                 vidc_writel(0x84000000 | vidc.h_display_end);
 
 412                 vidc_writel(0x85000000 | vidc.h_border_end);
 
 413                 vidc_writel(0x86000000);
 
 414                 vidc_writel(0x87000000 | vidc.h_interlace);
 
 415                 vidc_writel(0x90000000 | vidc.v_cycle);
 
 416                 vidc_writel(0x91000000 | vidc.v_sync_width);
 
 417                 vidc_writel(0x92000000 | vidc.v_border_start);
 
 418                 vidc_writel(0x93000000 | vidc.v_display_start);
 
 419                 vidc_writel(0x94000000 | vidc.v_display_end);
 
 420                 vidc_writel(0x95000000 | vidc.v_border_end);
 
 421                 vidc_writel(0x96000000);
 
 422                 vidc_writel(0x97000000);
 
 425         iomd_writel(fsize, IOMD_FSIZE);
 
 427         ext_ctl = acornfb_default_econtrol();
 
 429         if (var->sync & FB_SYNC_COMP_HIGH_ACT) /* should be FB_SYNC_COMP */
 
 430                 ext_ctl |= VIDC20_ECTL_HS_NCSYNC | VIDC20_ECTL_VS_NCSYNC;
 
 432                 if (var->sync & FB_SYNC_HOR_HIGH_ACT)
 
 433                         ext_ctl |= VIDC20_ECTL_HS_HSYNC;
 
 435                         ext_ctl |= VIDC20_ECTL_HS_NHSYNC;
 
 437                 if (var->sync & FB_SYNC_VERT_HIGH_ACT)
 
 438                         ext_ctl |= VIDC20_ECTL_VS_VSYNC;
 
 440                         ext_ctl |= VIDC20_ECTL_VS_NVSYNC;
 
 443         vidc_writel(VIDC20_ECTL | ext_ctl);
 
 445         words_per_line = var->xres * var->bits_per_pixel / 32;
 
 447         if (current_par.using_vram && info->fix.smem_len == 2048*1024)
 
 450         /* RiscPC doesn't use the VIDC's VRAM control. */
 
 451         dat_ctl = VIDC20_DCTL_VRAM_DIS | VIDC20_DCTL_SNA | words_per_line;
 
 453         /* The data bus width is dependent on both the type
 
 454          * and amount of video memory.
 
 459         if (current_par.using_vram && current_par.vram_half_sam == 2048)
 
 460                 dat_ctl |= VIDC20_DCTL_BUS_D63_0;
 
 462                 dat_ctl |= VIDC20_DCTL_BUS_D31_0;
 
 464         vidc_writel(VIDC20_DCTL | dat_ctl);
 
 466 #ifdef DEBUG_MODE_SELECTION
 
 467         printk(KERN_DEBUG "VIDC registers for %dx%dx%d:\n", var->xres,
 
 468                var->yres, var->bits_per_pixel);
 
 469         printk(KERN_DEBUG " H-cycle          : %d\n", vidc.h_cycle);
 
 470         printk(KERN_DEBUG " H-sync-width     : %d\n", vidc.h_sync_width);
 
 471         printk(KERN_DEBUG " H-border-start   : %d\n", vidc.h_border_start);
 
 472         printk(KERN_DEBUG " H-display-start  : %d\n", vidc.h_display_start);
 
 473         printk(KERN_DEBUG " H-display-end    : %d\n", vidc.h_display_end);
 
 474         printk(KERN_DEBUG " H-border-end     : %d\n", vidc.h_border_end);
 
 475         printk(KERN_DEBUG " H-interlace      : %d\n", vidc.h_interlace);
 
 476         printk(KERN_DEBUG " V-cycle          : %d\n", vidc.v_cycle);
 
 477         printk(KERN_DEBUG " V-sync-width     : %d\n", vidc.v_sync_width);
 
 478         printk(KERN_DEBUG " V-border-start   : %d\n", vidc.v_border_start);
 
 479         printk(KERN_DEBUG " V-display-start  : %d\n", vidc.v_display_start);
 
 480         printk(KERN_DEBUG " V-display-end    : %d\n", vidc.v_display_end);
 
 481         printk(KERN_DEBUG " V-border-end     : %d\n", vidc.v_border_end);
 
 482         printk(KERN_DEBUG " Ext Ctrl  (C)    : 0x%08X\n", ext_ctl);
 
 483         printk(KERN_DEBUG " PLL Ctrl  (D)    : 0x%08X\n", vidc.pll_ctl);
 
 484         printk(KERN_DEBUG " Ctrl      (E)    : 0x%08X\n", vidc.control);
 
 485         printk(KERN_DEBUG " Data Ctrl (F)    : 0x%08X\n", dat_ctl);
 
 486         printk(KERN_DEBUG " Fsize            : 0x%08X\n", fsize);
 
 491  * We have to take note of the VIDC20's 16-bit palette here.
 
 492  * The VIDC20 looks up a 16 bit pixel as follows:
 
 496  *   red            ++++++++  (8 bits,  7 to 0)
 
 497  *  green       ++++++++      (8 bits, 11 to 4)
 
 498  *   blue   ++++++++          (8 bits, 15 to 8)
 
 500  * We use a pixel which looks like:
 
 504  *   red               +++++  (5 bits,  4 to  0)
 
 505  *  green         +++++       (5 bits,  9 to  5)
 
 506  *   blue    +++++            (5 bits, 14 to 10)
 
 509 acornfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
 
 510                   u_int trans, struct fb_info *info)
 
 514         if (regno >= current_par.palette_size)
 
 517         if (regno < 16 && info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
 
 520                 pseudo_val  = regno << info->var.red.offset;
 
 521                 pseudo_val |= regno << info->var.green.offset;
 
 522                 pseudo_val |= regno << info->var.blue.offset;
 
 524                 ((u32 *)info->pseudo_palette)[regno] = pseudo_val;
 
 528         pal.vidc20.red   = red >> 8;
 
 529         pal.vidc20.green = green >> 8;
 
 530         pal.vidc20.blue  = blue >> 8;
 
 532         current_par.palette[regno] = pal;
 
 534         if (info->var.bits_per_pixel == 16) {
 
 538                 vidc_writel(0x10000000);
 
 539                 for (i = 0; i < 256; i += 1) {
 
 540                         pal.vidc20.red   = current_par.palette[ i       & 31].vidc20.red;
 
 541                         pal.vidc20.green = current_par.palette[(i >> 1) & 31].vidc20.green;
 
 542                         pal.vidc20.blue  = current_par.palette[(i >> 2) & 31].vidc20.blue;
 
 544                         /* Palette register pointer auto-increments */
 
 547                 vidc_writel(0x10000000 | regno);
 
 556  * Before selecting the timing parameters, adjust
 
 557  * the resolution to fit the rules.
 
 560 acornfb_adjust_timing(struct fb_info *info, struct fb_var_screeninfo *var, u_int fontht)
 
 562         u_int font_line_len, sam_size, min_size, size, nr_y;
 
 564         /* xres must be even */
 
 565         var->xres = (var->xres + 1) & ~1;
 
 568          * We don't allow xres_virtual to differ from xres
 
 570         var->xres_virtual = var->xres;
 
 573         if (current_par.using_vram)
 
 574                 sam_size = current_par.vram_half_sam * 2;
 
 579          * Now, find a value for yres_virtual which allows
 
 580          * us to do ywrap scrolling.  The value of
 
 581          * yres_virtual must be such that the end of the
 
 582          * displayable frame buffer must be aligned with
 
 583          * the start of a font line.
 
 585         font_line_len = var->xres * var->bits_per_pixel * fontht / 8;
 
 586         min_size = var->xres * var->yres * var->bits_per_pixel / 8;
 
 589          * If minimum screen size is greater than that we have
 
 590          * available, reject it.
 
 592         if (min_size > info->fix.smem_len)
 
 595         /* Find int 'y', such that y * fll == s * sam < maxsize
 
 596          * y = s * sam / fll; s = maxsize / sam
 
 598         for (size = info->fix.smem_len;
 
 599              nr_y = size / font_line_len, min_size <= size;
 
 601                 if (nr_y * font_line_len == size)
 
 606         if (var->accel_flags & FB_ACCELF_TEXT) {
 
 607                 if (min_size > size) {
 
 611                         size = info->fix.smem_len;
 
 612                         var->yres_virtual = size / (font_line_len / fontht);
 
 614                         var->yres_virtual = nr_y;
 
 615         } else if (var->yres_virtual > nr_y)
 
 616                 var->yres_virtual = nr_y;
 
 618         current_par.screen_end = info->fix.smem_start + size;
 
 621          * Fix yres & yoffset if needed.
 
 623         if (var->yres > var->yres_virtual)
 
 624                 var->yres = var->yres_virtual;
 
 626         if (var->vmode & FB_VMODE_YWRAP) {
 
 627                 if (var->yoffset > var->yres_virtual)
 
 628                         var->yoffset = var->yres_virtual;
 
 630                 if (var->yoffset + var->yres > var->yres_virtual)
 
 631                         var->yoffset = var->yres_virtual - var->yres;
 
 634         /* hsync_len must be even */
 
 635         var->hsync_len = (var->hsync_len + 1) & ~1;
 
 638         /* left_margin must be odd */
 
 639         if ((var->left_margin & 1) == 0) {
 
 640                 var->left_margin -= 1;
 
 641                 var->right_margin += 1;
 
 644         /* right_margin must be odd */
 
 645         var->right_margin |= 1;
 
 646 #elif defined(HAS_VIDC20)
 
 647         /* left_margin must be even */
 
 648         if (var->left_margin & 1) {
 
 649                 var->left_margin += 1;
 
 650                 var->right_margin -= 1;
 
 653         /* right_margin must be even */
 
 654         if (var->right_margin & 1)
 
 655                 var->right_margin += 1;
 
 658         if (var->vsync_len < 1)
 
 665 acornfb_validate_timing(struct fb_var_screeninfo *var,
 
 666                         struct fb_monspecs *monspecs)
 
 668         unsigned long hs, vs;
 
 671          * hs(Hz) = 10^12 / (pixclock * xtotal)
 
 672          * vs(Hz) = hs(Hz) / ytotal
 
 674          * No need to do long long divisions or anything
 
 675          * like that if you factor it correctly
 
 677         hs = 1953125000 / var->pixclock;
 
 679              (var->xres + var->left_margin + var->right_margin + var->hsync_len);
 
 681              (var->yres + var->upper_margin + var->lower_margin + var->vsync_len);
 
 683         return (vs >= monspecs->vfmin && vs <= monspecs->vfmax &&
 
 684                 hs >= monspecs->hfmin && hs <= monspecs->hfmax) ? 0 : -EINVAL;
 
 688 acornfb_update_dma(struct fb_info *info, struct fb_var_screeninfo *var)
 
 690         u_int off = var->yoffset * info->fix.line_length;
 
 692 #if defined(HAS_MEMC)
 
 693         memc_write(VDMA_INIT, off >> 2);
 
 694 #elif defined(HAS_IOMD)
 
 695         iomd_writel(info->fix.smem_start + off, IOMD_VIDINIT);
 
 700 acornfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
 
 706          * FIXME: Find the font height
 
 710         var->red.msb_right = 0;
 
 711         var->green.msb_right = 0;
 
 712         var->blue.msb_right = 0;
 
 713         var->transp.msb_right = 0;
 
 715         switch (var->bits_per_pixel) {
 
 716         case 1: case 2: case 4: case 8:
 
 718                 var->red.length    = var->bits_per_pixel;
 
 719                 var->green         = var->red;
 
 720                 var->blue          = var->red;
 
 721                 var->transp.offset = 0;
 
 722                 var->transp.length = 0;
 
 729                 var->green.offset  = 5;
 
 730                 var->green.length  = 5;
 
 731                 var->blue.offset   = 10;
 
 732                 var->blue.length   = 5;
 
 733                 var->transp.offset = 15;
 
 734                 var->transp.length = 1;
 
 740                 var->green.offset  = 8;
 
 741                 var->green.length  = 8;
 
 742                 var->blue.offset   = 16;
 
 743                 var->blue.length   = 8;
 
 744                 var->transp.offset = 24;
 
 745                 var->transp.length = 4;
 
 753          * Check to see if the pixel rate is valid.
 
 755         if (!acornfb_valid_pixrate(var))
 
 759          * Validate and adjust the resolution to
 
 760          * match the video generator hardware.
 
 762         err = acornfb_adjust_timing(info, var, fontht);
 
 767          * Validate the timing against the
 
 770         return acornfb_validate_timing(var, &info->monspecs);
 
 773 static int acornfb_set_par(struct fb_info *info)
 
 775         switch (info->var.bits_per_pixel) {
 
 777                 current_par.palette_size = 2;
 
 778                 info->fix.visual = FB_VISUAL_MONO10;
 
 781                 current_par.palette_size = 4;
 
 782                 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
 
 785                 current_par.palette_size = 16;
 
 786                 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
 
 789                 current_par.palette_size = VIDC_PALETTE_SIZE;
 
 791                 info->fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
 
 793                 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
 
 798                 current_par.palette_size = 32;
 
 799                 info->fix.visual = FB_VISUAL_DIRECTCOLOR;
 
 802                 current_par.palette_size = VIDC_PALETTE_SIZE;
 
 803                 info->fix.visual = FB_VISUAL_DIRECTCOLOR;
 
 810         info->fix.line_length   = (info->var.xres * info->var.bits_per_pixel) / 8;
 
 812 #if defined(HAS_MEMC)
 
 814                 unsigned long size = info->fix.smem_len - VDMA_XFERSIZE;
 
 816                 memc_write(VDMA_START, 0);
 
 817                 memc_write(VDMA_END, size >> 2);
 
 819 #elif defined(HAS_IOMD)
 
 821                 unsigned long start, size;
 
 824                 start = info->fix.smem_start;
 
 825                 size  = current_par.screen_end;
 
 827                 if (current_par.using_vram) {
 
 828                         size -= current_par.vram_half_sam;
 
 829                         control = DMA_CR_E | (current_par.vram_half_sam / 256);
 
 832                         control = DMA_CR_E | DMA_CR_D | 16;
 
 835                 iomd_writel(start,   IOMD_VIDSTART);
 
 836                 iomd_writel(size,    IOMD_VIDEND);
 
 837                 iomd_writel(control, IOMD_VIDCR);
 
 841         acornfb_update_dma(info, &info->var);
 
 842         acornfb_set_timing(info);
 
 848 acornfb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
 
 850         u_int y_bottom = var->yoffset;
 
 852         if (!(var->vmode & FB_VMODE_YWRAP))
 
 853                 y_bottom += var->yres;
 
 855         BUG_ON(y_bottom > var->yres_virtual);
 
 857         acornfb_update_dma(info, var);
 
 863  * Note that we are entered with the kernel locked.
 
 866 acornfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
 
 868         unsigned long off, start;
 
 871         off = vma->vm_pgoff << PAGE_SHIFT;
 
 873         start = info->fix.smem_start;
 
 874         len = PAGE_ALIGN(start & ~PAGE_MASK) + info->fix.smem_len;
 
 876         if ((vma->vm_end - vma->vm_start + off) > len)
 
 879         vma->vm_pgoff = off >> PAGE_SHIFT;
 
 881         /* This is an IO map - tell maydump to skip this VMA */
 
 882         vma->vm_flags |= VM_IO;
 
 884         vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
 
 887          * Don't alter the page protection flags; we want to keep the area
 
 888          * cached for better performance.  This does mean that we may miss
 
 889          * some updates to the screen occasionally, but process switches
 
 890          * should cause the caches and buffers to be flushed often enough.
 
 892         if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
 
 893                                 vma->vm_end - vma->vm_start,
 
 899 static struct fb_ops acornfb_ops = {
 
 900         .owner          = THIS_MODULE,
 
 901         .fb_check_var   = acornfb_check_var,
 
 902         .fb_set_par     = acornfb_set_par,
 
 903         .fb_setcolreg   = acornfb_setcolreg,
 
 904         .fb_pan_display = acornfb_pan_display,
 
 905         .fb_fillrect    = cfb_fillrect,
 
 906         .fb_copyarea    = cfb_copyarea,
 
 907         .fb_imageblit   = cfb_imageblit,
 
 908         .fb_mmap        = acornfb_mmap,
 
 912  * Everything after here is initialisation!!!
 
 914 static struct fb_videomode modedb[] __initdata = {
 
 915         {       /* 320x256 @ 50Hz */
 
 916                 NULL, 50,  320,  256, 125000,  92,  62,  35, 19,  38, 2,
 
 917                 FB_SYNC_COMP_HIGH_ACT,
 
 918                 FB_VMODE_NONINTERLACED
 
 919         }, {    /* 640x250 @ 50Hz, 15.6 kHz hsync */
 
 920                 NULL, 50,  640,  250,  62500, 185, 123,  38, 21,  76, 3,
 
 922                 FB_VMODE_NONINTERLACED
 
 923         }, {    /* 640x256 @ 50Hz, 15.6 kHz hsync */
 
 924                 NULL, 50,  640,  256,  62500, 185, 123,  35, 18,  76, 3,
 
 926                 FB_VMODE_NONINTERLACED
 
 927         }, {    /* 640x512 @ 50Hz, 26.8 kHz hsync */
 
 928                 NULL, 50,  640,  512,  41667, 113,  87,  18,  1,  56, 3,
 
 930                 FB_VMODE_NONINTERLACED
 
 931         }, {    /* 640x250 @ 70Hz, 31.5 kHz hsync */
 
 932                 NULL, 70,  640,  250,  39722,  48,  16, 109, 88,  96, 2,
 
 934                 FB_VMODE_NONINTERLACED
 
 935         }, {    /* 640x256 @ 70Hz, 31.5 kHz hsync */
 
 936                 NULL, 70,  640,  256,  39722,  48,  16, 106, 85,  96, 2,
 
 938                 FB_VMODE_NONINTERLACED
 
 939         }, {    /* 640x352 @ 70Hz, 31.5 kHz hsync */
 
 940                 NULL, 70,  640,  352,  39722,  48,  16,  58, 37,  96, 2,
 
 942                 FB_VMODE_NONINTERLACED
 
 943         }, {    /* 640x480 @ 60Hz, 31.5 kHz hsync */
 
 944                 NULL, 60,  640,  480,  39722,  48,  16,  32, 11,  96, 2,
 
 946                 FB_VMODE_NONINTERLACED
 
 947         }, {    /* 800x600 @ 56Hz, 35.2 kHz hsync */
 
 948                 NULL, 56,  800,  600,  27778, 101,  23,  22,  1, 100, 2,
 
 950                 FB_VMODE_NONINTERLACED
 
 951         }, {    /* 896x352 @ 60Hz, 21.8 kHz hsync */
 
 952                 NULL, 60,  896,  352,  41667,  59,  27,   9,  0, 118, 3,
 
 954                 FB_VMODE_NONINTERLACED
 
 955         }, {    /* 1024x 768 @ 60Hz, 48.4 kHz hsync */
 
 956                 NULL, 60, 1024,  768,  15385, 160,  24,  29,  3, 136, 6,
 
 958                 FB_VMODE_NONINTERLACED
 
 959         }, {    /* 1280x1024 @ 60Hz, 63.8 kHz hsync */
 
 960                 NULL, 60, 1280, 1024,   9090, 186,  96,  38,  1, 160, 3,
 
 962                 FB_VMODE_NONINTERLACED
 
 966 static struct fb_videomode __initdata
 
 967 acornfb_default_mode = {
 
 980         .vmode =        FB_VMODE_NONINTERLACED
 
 983 static void __init acornfb_init_fbinfo(void)
 
 985         static int first = 1;
 
 991         fb_info.fbops           = &acornfb_ops;
 
 992         fb_info.flags           = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
 
 993         fb_info.pseudo_palette  = current_par.pseudo_palette;
 
 995         strcpy(fb_info.fix.id, "Acorn");
 
 996         fb_info.fix.type        = FB_TYPE_PACKED_PIXELS;
 
 997         fb_info.fix.type_aux    = 0;
 
 998         fb_info.fix.xpanstep    = 0;
 
 999         fb_info.fix.ypanstep    = 1;
 
1000         fb_info.fix.ywrapstep   = 1;
 
1001         fb_info.fix.line_length = 0;
 
1002         fb_info.fix.accel       = FB_ACCEL_NONE;
 
1005          * setup initial parameters
 
1007         memset(&fb_info.var, 0, sizeof(fb_info.var));
 
1009 #if defined(HAS_VIDC20)
 
1010         fb_info.var.red.length     = 8;
 
1011         fb_info.var.transp.length  = 4;
 
1012 #elif defined(HAS_VIDC)
 
1013         fb_info.var.red.length     = 4;
 
1014         fb_info.var.transp.length  = 1;
 
1016         fb_info.var.green          = fb_info.var.red;
 
1017         fb_info.var.blue           = fb_info.var.red;
 
1018         fb_info.var.nonstd         = 0;
 
1019         fb_info.var.activate       = FB_ACTIVATE_NOW;
 
1020         fb_info.var.height         = -1;
 
1021         fb_info.var.width          = -1;
 
1022         fb_info.var.vmode          = FB_VMODE_NONINTERLACED;
 
1023         fb_info.var.accel_flags    = FB_ACCELF_TEXT;
 
1025         current_par.dram_size      = 0;
 
1026         current_par.montype        = -1;
 
1027         current_par.dpms           = 0;
 
1031  * setup acornfb options:
 
1033  *  mon:hmin-hmax:vmin-vmax:dpms:width:height
 
1034  *      Set monitor parameters:
 
1035  *              hmin   = horizontal minimum frequency (Hz)
 
1036  *              hmax   = horizontal maximum frequency (Hz)      (optional)
 
1037  *              vmin   = vertical minimum frequency (Hz)
 
1038  *              vmax   = vertical maximum frequency (Hz)        (optional)
 
1039  *              dpms   = DPMS supported?                        (optional)
 
1040  *              width  = width of picture in mm.                (optional)
 
1041  *              height = height of picture in mm.               (optional)
 
1044  *      Set RISC-OS style monitor type:
 
1045  *              0 (or tv)       - TV frequency
 
1046  *              1 (or multi)    - Multi frequency
 
1047  *              2 (or hires)    - Hi-res monochrome
 
1049  *              4 (or svga)     - SVGA
 
1050  *              auto, or option missing
 
1051  *                              - try hardware detect
 
1054  *      Set the amount of DRAM to use for the frame buffer
 
1055  *      (even if you have VRAM).
 
1056  *      size can optionally be followed by 'M' or 'K' for
 
1057  *      MB or KB respectively.
 
1060 acornfb_parse_mon(char *opt)
 
1064         current_par.montype = -2;
 
1066         fb_info.monspecs.hfmin = simple_strtoul(p, &p, 0);
 
1068                 fb_info.monspecs.hfmax = simple_strtoul(p + 1, &p, 0);
 
1070                 fb_info.monspecs.hfmax = fb_info.monspecs.hfmin;
 
1075         fb_info.monspecs.vfmin = simple_strtoul(p + 1, &p, 0);
 
1077                 fb_info.monspecs.vfmax = simple_strtoul(p + 1, &p, 0);
 
1079                 fb_info.monspecs.vfmax = fb_info.monspecs.vfmin;
 
1084         fb_info.monspecs.dpms = simple_strtoul(p + 1, &p, 0);
 
1089         fb_info.var.width = simple_strtoul(p + 1, &p, 0);
 
1094         fb_info.var.height = simple_strtoul(p + 1, NULL, 0);
 
1097         if (fb_info.monspecs.hfmax < fb_info.monspecs.hfmin ||
 
1098             fb_info.monspecs.vfmax < fb_info.monspecs.vfmin)
 
1103         printk(KERN_ERR "Acornfb: bad monitor settings: %s\n", opt);
 
1104         current_par.montype = -1;
 
1108 acornfb_parse_montype(char *opt)
 
1110         current_par.montype = -2;
 
1112         if (strncmp(opt, "tv", 2) == 0) {
 
1114                 current_par.montype = 0;
 
1115         } else if (strncmp(opt, "multi", 5) == 0) {
 
1117                 current_par.montype = 1;
 
1118         } else if (strncmp(opt, "hires", 5) == 0) {
 
1120                 current_par.montype = 2;
 
1121         } else if (strncmp(opt, "vga", 3) == 0) {
 
1123                 current_par.montype = 3;
 
1124         } else if (strncmp(opt, "svga", 4) == 0) {
 
1126                 current_par.montype = 4;
 
1127         } else if (strncmp(opt, "auto", 4) == 0) {
 
1129                 current_par.montype = -1;
 
1130         } else if (isdigit(*opt))
 
1131                 current_par.montype = simple_strtoul(opt, &opt, 0);
 
1133         if (current_par.montype == -2 ||
 
1134             current_par.montype > NR_MONTYPES) {
 
1135                 printk(KERN_ERR "acornfb: unknown monitor type: %s\n",
 
1137                 current_par.montype = -1;
 
1140                 if (strcmp(opt, ",dpms") == 0)
 
1141                         current_par.dpms = 1;
 
1144                                "acornfb: unknown monitor option: %s\n",
 
1150 acornfb_parse_dram(char *opt)
 
1154         size = simple_strtoul(opt, &opt, 0);
 
1169         current_par.dram_size = size;
 
1172 static struct options {
 
1174         void (*parse)(char *opt);
 
1175 } opt_table[] __initdata = {
 
1176         { "mon",     acornfb_parse_mon     },
 
1177         { "montype", acornfb_parse_montype },
 
1178         { "dram",    acornfb_parse_dram    },
 
1183 acornfb_setup(char *options)
 
1185         struct options *optp;
 
1188         if (!options || !*options)
 
1191         acornfb_init_fbinfo();
 
1193         while ((opt = strsep(&options, ",")) != NULL) {
 
1197                 for (optp = opt_table; optp->name; optp++) {
 
1200                         optlen = strlen(optp->name);
 
1202                         if (strncmp(opt, optp->name, optlen) == 0 &&
 
1203                             opt[optlen] == ':') {
 
1204                                 optp->parse(opt + optlen + 1);
 
1210                         printk(KERN_ERR "acornfb: unknown parameter: %s\n",
 
1217  * Detect type of monitor connected
 
1218  *  For now, we just assume SVGA
 
1221 acornfb_detect_monitortype(void)
 
1227  * This enables the unused memory to be freed on older Acorn machines.
 
1228  * We are freeing memory on behalf of the architecture initialisation
 
1232 free_unused_pages(unsigned int virtual_start, unsigned int virtual_end)
 
1239         virtual_start = PAGE_ALIGN(virtual_start);
 
1240         virtual_end = PAGE_ALIGN(virtual_end);
 
1242         while (virtual_start < virtual_end) {
 
1246                  * Clear page reserved bit,
 
1247                  * set count to 1, and free
 
1250                 page = virt_to_page(virtual_start);
 
1251                 ClearPageReserved(page);
 
1252                 init_page_count(page);
 
1253                 free_page(virtual_start);
 
1255                 virtual_start += PAGE_SIZE;
 
1256                 mb_freed += PAGE_SIZE / 1024;
 
1259         printk("acornfb: freed %dK memory\n", mb_freed);
 
1262 static int __init acornfb_probe(struct platform_device *dev)
 
1265         u_int h_sync, v_sync;
 
1267         char *option = NULL;
 
1269         if (fb_get_options("acornfb", &option))
 
1271         acornfb_setup(option);
 
1273         acornfb_init_fbinfo();
 
1275         current_par.dev = &dev->dev;
 
1277         if (current_par.montype == -1)
 
1278                 current_par.montype = acornfb_detect_monitortype();
 
1280         if (current_par.montype == -1 || current_par.montype > NR_MONTYPES)
 
1281                 current_par.montype = 4;
 
1283         if (current_par.montype >= 0) {
 
1284                 fb_info.monspecs = monspecs[current_par.montype];
 
1285                 fb_info.monspecs.dpms = current_par.dpms;
 
1289          * Try to select a suitable default mode
 
1291         for (i = 0; i < ARRAY_SIZE(modedb); i++) {
 
1294                 hs = modedb[i].refresh *
 
1295                      (modedb[i].yres + modedb[i].upper_margin +
 
1296                       modedb[i].lower_margin + modedb[i].vsync_len);
 
1297                 if (modedb[i].xres == DEFAULT_XRES &&
 
1298                     modedb[i].yres == DEFAULT_YRES &&
 
1299                     modedb[i].refresh >= fb_info.monspecs.vfmin &&
 
1300                     modedb[i].refresh <= fb_info.monspecs.vfmax &&
 
1301                     hs                >= fb_info.monspecs.hfmin &&
 
1302                     hs                <= fb_info.monspecs.hfmax) {
 
1303                         acornfb_default_mode = modedb[i];
 
1308         fb_info.screen_base    = (char *)SCREEN_BASE;
 
1309         fb_info.fix.smem_start = SCREEN_START;
 
1310         current_par.using_vram = 0;
 
1313          * If vram_size is set, we are using VRAM in
 
1314          * a Risc PC.  However, if the user has specified
 
1315          * an amount of DRAM then use that instead.
 
1317         if (vram_size && !current_par.dram_size) {
 
1319                 current_par.vram_half_sam = vram_size / 1024;
 
1320                 current_par.using_vram = 1;
 
1321         } else if (current_par.dram_size)
 
1322                 size = current_par.dram_size;
 
1327          * Limit maximum screen size.
 
1329         if (size > MAX_SIZE)
 
1332         size = PAGE_ALIGN(size);
 
1334 #if defined(HAS_VIDC20)
 
1335         if (!current_par.using_vram) {
 
1340                  * RiscPC needs to allocate the DRAM memory
 
1341                  * for the framebuffer if we are not using
 
1344                 base = dma_alloc_writecombine(current_par.dev, size, &handle,
 
1347                         printk(KERN_ERR "acornfb: unable to allocate screen "
 
1352                 fb_info.screen_base = base;
 
1353                 fb_info.fix.smem_start = handle;
 
1356 #if defined(HAS_VIDC)
 
1358          * Archimedes/A5000 machines use a fixed address for their
 
1359          * framebuffers.  Free unused pages
 
1361         free_unused_pages(PAGE_OFFSET + size, PAGE_OFFSET + MAX_SIZE);
 
1364         fb_info.fix.smem_len = size;
 
1365         current_par.palette_size   = VIDC_PALETTE_SIZE;
 
1368          * Lookup the timing for this resolution.  If we can't
 
1369          * find it, then we can't restore it if we change
 
1370          * the resolution, so we disable this feature.
 
1373                 rc = fb_find_mode(&fb_info.var, &fb_info, NULL, modedb,
 
1375                                  &acornfb_default_mode, DEFAULT_BPP);
 
1377                  * If we found an exact match, all ok.
 
1382                 rc = fb_find_mode(&fb_info.var, &fb_info, NULL, NULL, 0,
 
1383                                   &acornfb_default_mode, DEFAULT_BPP);
 
1385                  * If we found an exact match, all ok.
 
1390                 rc = fb_find_mode(&fb_info.var, &fb_info, NULL, modedb,
 
1392                                  &acornfb_default_mode, DEFAULT_BPP);
 
1396                 rc = fb_find_mode(&fb_info.var, &fb_info, NULL, NULL, 0,
 
1397                                   &acornfb_default_mode, DEFAULT_BPP);
 
1401          * If we didn't find an exact match, try the
 
1405                 printk("Acornfb: no valid mode found\n");
 
1409         h_sync = 1953125000 / fb_info.var.pixclock;
 
1410         h_sync = h_sync * 512 / (fb_info.var.xres + fb_info.var.left_margin +
 
1411                  fb_info.var.right_margin + fb_info.var.hsync_len);
 
1412         v_sync = h_sync / (fb_info.var.yres + fb_info.var.upper_margin +
 
1413                  fb_info.var.lower_margin + fb_info.var.vsync_len);
 
1415         printk(KERN_INFO "Acornfb: %dkB %cRAM, %s, using %dx%d, "
 
1416                 "%d.%03dkHz, %dHz\n",
 
1417                 fb_info.fix.smem_len / 1024,
 
1418                 current_par.using_vram ? 'V' : 'D',
 
1419                 VIDC_NAME, fb_info.var.xres, fb_info.var.yres,
 
1420                 h_sync / 1000, h_sync % 1000, v_sync);
 
1422         printk(KERN_INFO "Acornfb: Monitor: %d.%03d-%d.%03dkHz, %d-%dHz%s\n",
 
1423                 fb_info.monspecs.hfmin / 1000, fb_info.monspecs.hfmin % 1000,
 
1424                 fb_info.monspecs.hfmax / 1000, fb_info.monspecs.hfmax % 1000,
 
1425                 fb_info.monspecs.vfmin, fb_info.monspecs.vfmax,
 
1426                 fb_info.monspecs.dpms ? ", DPMS" : "");
 
1428         if (fb_set_var(&fb_info, &fb_info.var))
 
1429                 printk(KERN_ERR "Acornfb: unable to set display parameters\n");
 
1431         if (register_framebuffer(&fb_info) < 0)
 
1436 static struct platform_driver acornfb_driver = {
 
1437         .probe  = acornfb_probe,
 
1443 static int __init acornfb_init(void)
 
1445         return platform_driver_register(&acornfb_driver);
 
1448 module_init(acornfb_init);
 
1450 MODULE_AUTHOR("Russell King");
 
1451 MODULE_DESCRIPTION("VIDC 1/1a/20 framebuffer driver");
 
1452 MODULE_LICENSE("GPL");