2  *  linux/drivers/video/arkfb.c -- Frame buffer device driver for ARK 2000PV
 
   3  *  with ICS 5342 dac (it is easy to add support for different dacs).
 
   5  *  Copyright (c) 2007 Ondrej Zajicek <santiago@crfreenet.org>
 
   7  *  This file is subject to the terms and conditions of the GNU General Public
 
   8  *  License.  See the file COPYING in the main directory of this archive for
 
  11  *  Code is based on s3fb
 
  14 #include <linux/version.h>
 
  15 #include <linux/module.h>
 
  16 #include <linux/kernel.h>
 
  17 #include <linux/errno.h>
 
  18 #include <linux/string.h>
 
  20 #include <linux/tty.h>
 
  21 #include <linux/slab.h>
 
  22 #include <linux/delay.h>
 
  24 #include <linux/svga.h>
 
  25 #include <linux/init.h>
 
  26 #include <linux/pci.h>
 
  27 #include <linux/console.h> /* Why should fb driver call console functions? because acquire_console_sem() */
 
  28 #include <video/vga.h>
 
  39         struct vgastate state;
 
  40         struct mutex open_lock;
 
  41         unsigned int ref_count;
 
  42         u32 pseudo_palette[16];
 
  46 /* ------------------------------------------------------------------------- */
 
  49 static const struct svga_fb_format arkfb_formats[] = {
 
  50         { 0,  {0, 6, 0},  {0, 6, 0},  {0, 6, 0}, {0, 0, 0}, 0,
 
  51                 FB_TYPE_TEXT, FB_AUX_TEXT_SVGA_STEP4,   FB_VISUAL_PSEUDOCOLOR, 8, 8},
 
  52         { 4,  {0, 6, 0},  {0, 6, 0},  {0, 6, 0}, {0, 0, 0}, 0,
 
  53                 FB_TYPE_PACKED_PIXELS, 0,               FB_VISUAL_PSEUDOCOLOR, 8, 16},
 
  54         { 4,  {0, 6, 0},  {0, 6, 0},  {0, 6, 0}, {0, 0, 0}, 1,
 
  55                 FB_TYPE_INTERLEAVED_PLANES, 1,          FB_VISUAL_PSEUDOCOLOR, 8, 16},
 
  56         { 8,  {0, 6, 0},  {0, 6, 0},  {0, 6, 0}, {0, 0, 0}, 0,
 
  57                 FB_TYPE_PACKED_PIXELS, 0,               FB_VISUAL_PSEUDOCOLOR, 8, 8},
 
  58         {16,  {10, 5, 0}, {5, 5, 0},  {0, 5, 0}, {0, 0, 0}, 0,
 
  59                 FB_TYPE_PACKED_PIXELS, 0,               FB_VISUAL_TRUECOLOR, 4, 4},
 
  60         {16,  {11, 5, 0}, {5, 6, 0},  {0, 5, 0}, {0, 0, 0}, 0,
 
  61                 FB_TYPE_PACKED_PIXELS, 0,               FB_VISUAL_TRUECOLOR, 4, 4},
 
  62         {24,  {16, 8, 0}, {8, 8, 0},  {0, 8, 0}, {0, 0, 0}, 0,
 
  63                 FB_TYPE_PACKED_PIXELS, 0,               FB_VISUAL_TRUECOLOR, 8, 8},
 
  64         {32,  {16, 8, 0}, {8, 8, 0},  {0, 8, 0}, {0, 0, 0}, 0,
 
  65                 FB_TYPE_PACKED_PIXELS, 0,               FB_VISUAL_TRUECOLOR, 2, 2},
 
  70 /* CRT timing register sets */
 
  72 static const struct vga_regset ark_h_total_regs[]        = {{0x00, 0, 7}, {0x41, 7, 7}, VGA_REGSET_END};
 
  73 static const struct vga_regset ark_h_display_regs[]      = {{0x01, 0, 7}, {0x41, 6, 6}, VGA_REGSET_END};
 
  74 static const struct vga_regset ark_h_blank_start_regs[]  = {{0x02, 0, 7}, {0x41, 5, 5}, VGA_REGSET_END};
 
  75 static const struct vga_regset ark_h_blank_end_regs[]    = {{0x03, 0, 4}, {0x05, 7, 7   }, VGA_REGSET_END};
 
  76 static const struct vga_regset ark_h_sync_start_regs[]   = {{0x04, 0, 7}, {0x41, 4, 4}, VGA_REGSET_END};
 
  77 static const struct vga_regset ark_h_sync_end_regs[]     = {{0x05, 0, 4}, VGA_REGSET_END};
 
  79 static const struct vga_regset ark_v_total_regs[]        = {{0x06, 0, 7}, {0x07, 0, 0}, {0x07, 5, 5}, {0x40, 7, 7}, VGA_REGSET_END};
 
  80 static const struct vga_regset ark_v_display_regs[]      = {{0x12, 0, 7}, {0x07, 1, 1}, {0x07, 6, 6}, {0x40, 6, 6}, VGA_REGSET_END};
 
  81 static const struct vga_regset ark_v_blank_start_regs[]  = {{0x15, 0, 7}, {0x07, 3, 3}, {0x09, 5, 5}, {0x40, 5, 5}, VGA_REGSET_END};
 
  82 // const struct vga_regset ark_v_blank_end_regs[]    = {{0x16, 0, 6}, VGA_REGSET_END};
 
  83 static const struct vga_regset ark_v_blank_end_regs[]    = {{0x16, 0, 7}, VGA_REGSET_END};
 
  84 static const struct vga_regset ark_v_sync_start_regs[]   = {{0x10, 0, 7}, {0x07, 2, 2}, {0x07, 7, 7}, {0x40, 4, 4}, VGA_REGSET_END};
 
  85 static const struct vga_regset ark_v_sync_end_regs[]     = {{0x11, 0, 3}, VGA_REGSET_END};
 
  87 static const struct vga_regset ark_line_compare_regs[]   = {{0x18, 0, 7}, {0x07, 4, 4}, {0x09, 6, 6}, VGA_REGSET_END};
 
  88 static const struct vga_regset ark_start_address_regs[]  = {{0x0d, 0, 7}, {0x0c, 0, 7}, {0x40, 0, 2}, VGA_REGSET_END};
 
  89 static const struct vga_regset ark_offset_regs[]         = {{0x13, 0, 7}, {0x41, 3, 3}, VGA_REGSET_END};
 
  91 static const struct svga_timing_regs ark_timing_regs     = {
 
  92         ark_h_total_regs, ark_h_display_regs, ark_h_blank_start_regs,
 
  93         ark_h_blank_end_regs, ark_h_sync_start_regs, ark_h_sync_end_regs,
 
  94         ark_v_total_regs, ark_v_display_regs, ark_v_blank_start_regs,
 
  95         ark_v_blank_end_regs, ark_v_sync_start_regs, ark_v_sync_end_regs,
 
  99 /* ------------------------------------------------------------------------- */
 
 102 /* Module parameters */
 
 104 static char *mode = "640x480-8@60";
 
 110 MODULE_AUTHOR("(c) 2007 Ondrej Zajicek <santiago@crfreenet.org>");
 
 111 MODULE_LICENSE("GPL");
 
 112 MODULE_DESCRIPTION("fbdev driver for ARK 2000PV");
 
 114 module_param(mode, charp, 0444);
 
 115 MODULE_PARM_DESC(mode, "Default video mode ('640x480-8@60', etc)");
 
 118 module_param(mtrr, int, 0444);
 
 119 MODULE_PARM_DESC(mtrr, "Enable write-combining with MTRR (1=enable, 0=disable, default=1)");
 
 122 static int threshold = 4;
 
 124 module_param(threshold, int, 0644);
 
 125 MODULE_PARM_DESC(threshold, "FIFO threshold");
 
 128 /* ------------------------------------------------------------------------- */
 
 131 static void arkfb_settile(struct fb_info *info, struct fb_tilemap *map)
 
 133         const u8 *font = map->data;
 
 134         u8 __iomem *fb = (u8 __iomem *)info->screen_base;
 
 137         if ((map->width != 8) || (map->height != 16) ||
 
 138             (map->depth != 1) || (map->length != 256)) {
 
 139                 printk(KERN_ERR "fb%d: unsupported font parameters: width %d, "
 
 140                        "height %d, depth %d, length %d\n", info->node,
 
 141                        map->width, map->height, map->depth, map->length);
 
 146         for (c = 0; c < map->length; c++) {
 
 147                 for (i = 0; i < map->height; i++) {
 
 148                         fb_writeb(font[i], &fb[i * 4]);
 
 149                         fb_writeb(font[i], &fb[i * 4 + (128 * 8)]);
 
 160 static struct fb_tile_ops arkfb_tile_ops = {
 
 161         .fb_settile     = arkfb_settile,
 
 162         .fb_tilecopy    = svga_tilecopy,
 
 163         .fb_tilefill    = svga_tilefill,
 
 164         .fb_tileblit    = svga_tileblit,
 
 165         .fb_tilecursor  = svga_tilecursor,
 
 166         .fb_get_tilemax = svga_get_tilemax,
 
 170 /* ------------------------------------------------------------------------- */
 
 173 /* image data is MSB-first, fb structure is MSB-first too */
 
 174 static inline u32 expand_color(u32 c)
 
 176         return ((c & 1) | ((c & 2) << 7) | ((c & 4) << 14) | ((c & 8) << 21)) * 0xFF;
 
 179 /* arkfb_iplan_imageblit silently assumes that almost everything is 8-pixel aligned */
 
 180 static void arkfb_iplan_imageblit(struct fb_info *info, const struct fb_image *image)
 
 182         u32 fg = expand_color(image->fg_color);
 
 183         u32 bg = expand_color(image->bg_color);
 
 184         const u8 *src1, *src;
 
 191         dst1 = info->screen_base + (image->dy * info->fix.line_length)
 
 192                  + ((image->dx / 8) * 4);
 
 194         for (y = 0; y < image->height; y++) {
 
 196                 dst = (u32 __iomem *) dst1;
 
 197                 for (x = 0; x < image->width; x += 8) {
 
 198                         val = *(src++) * 0x01010101;
 
 199                         val = (val & fg) | (~val & bg);
 
 200                         fb_writel(val, dst++);
 
 202                 src1 += image->width / 8;
 
 203                 dst1 += info->fix.line_length;
 
 208 /* arkfb_iplan_fillrect silently assumes that almost everything is 8-pixel aligned */
 
 209 static void arkfb_iplan_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
 
 211         u32 fg = expand_color(rect->color);
 
 216         dst1 = info->screen_base + (rect->dy * info->fix.line_length)
 
 217                  + ((rect->dx / 8) * 4);
 
 219         for (y = 0; y < rect->height; y++) {
 
 220                 dst = (u32 __iomem *) dst1;
 
 221                 for (x = 0; x < rect->width; x += 8) {
 
 222                         fb_writel(fg, dst++);
 
 224                 dst1 += info->fix.line_length;
 
 230 /* image data is MSB-first, fb structure is high-nibble-in-low-byte-first */
 
 231 static inline u32 expand_pixel(u32 c)
 
 233         return (((c &  1) << 24) | ((c &  2) << 27) | ((c &  4) << 14) | ((c &   8) << 17) |
 
 234                 ((c & 16) <<  4) | ((c & 32) <<  7) | ((c & 64) >>  6) | ((c & 128) >>  3)) * 0xF;
 
 237 /* arkfb_cfb4_imageblit silently assumes that almost everything is 8-pixel aligned */
 
 238 static void arkfb_cfb4_imageblit(struct fb_info *info, const struct fb_image *image)
 
 240         u32 fg = image->fg_color * 0x11111111;
 
 241         u32 bg = image->bg_color * 0x11111111;
 
 242         const u8 *src1, *src;
 
 249         dst1 = info->screen_base + (image->dy * info->fix.line_length)
 
 250                  + ((image->dx / 8) * 4);
 
 252         for (y = 0; y < image->height; y++) {
 
 254                 dst = (u32 __iomem *) dst1;
 
 255                 for (x = 0; x < image->width; x += 8) {
 
 256                         val = expand_pixel(*(src++));
 
 257                         val = (val & fg) | (~val & bg);
 
 258                         fb_writel(val, dst++);
 
 260                 src1 += image->width / 8;
 
 261                 dst1 += info->fix.line_length;
 
 266 static void arkfb_imageblit(struct fb_info *info, const struct fb_image *image)
 
 268         if ((info->var.bits_per_pixel == 4) && (image->depth == 1)
 
 269             && ((image->width % 8) == 0) && ((image->dx % 8) == 0)) {
 
 270                 if (info->fix.type == FB_TYPE_INTERLEAVED_PLANES)
 
 271                         arkfb_iplan_imageblit(info, image);
 
 273                         arkfb_cfb4_imageblit(info, image);
 
 275                 cfb_imageblit(info, image);
 
 278 static void arkfb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
 
 280         if ((info->var.bits_per_pixel == 4)
 
 281             && ((rect->width % 8) == 0) && ((rect->dx % 8) == 0)
 
 282             && (info->fix.type == FB_TYPE_INTERLEAVED_PLANES))
 
 283                 arkfb_iplan_fillrect(info, rect);
 
 285                 cfb_fillrect(info, rect);
 
 289 /* ------------------------------------------------------------------------- */
 
 308         int (*dac_get_mode)(struct dac_info *info);
 
 309         int (*dac_set_mode)(struct dac_info *info, int mode);
 
 310         int (*dac_get_freq)(struct dac_info *info, int channel);
 
 311         int (*dac_set_freq)(struct dac_info *info, int channel, u32 freq);
 
 312         void (*dac_release)(struct dac_info *info);
 
 315 typedef void (*dac_read_regs_t)(void *data, u8 *code, int count);
 
 316 typedef void (*dac_write_regs_t)(void *data, u8 *code, int count);
 
 320         struct dac_ops *dacops;
 
 321         dac_read_regs_t dac_read_regs;
 
 322         dac_write_regs_t dac_write_regs;
 
 327 static inline u8 dac_read_reg(struct dac_info *info, u8 reg)
 
 329         u8 code[2] = {reg, 0};
 
 330         info->dac_read_regs(info->data, code, 1);
 
 334 static inline void dac_read_regs(struct dac_info *info, u8 *code, int count)
 
 336         info->dac_read_regs(info->data, code, count);
 
 339 static inline void dac_write_reg(struct dac_info *info, u8 reg, u8 val)
 
 341         u8 code[2] = {reg, val};
 
 342         info->dac_write_regs(info->data, code, 1);
 
 345 static inline void dac_write_regs(struct dac_info *info, u8 *code, int count)
 
 347         info->dac_write_regs(info->data, code, count);
 
 350 static inline int dac_set_mode(struct dac_info *info, int mode)
 
 352         return info->dacops->dac_set_mode(info, mode);
 
 355 static inline int dac_set_freq(struct dac_info *info, int channel, u32 freq)
 
 357         return info->dacops->dac_set_freq(info, channel, freq);
 
 360 static inline void dac_release(struct dac_info *info)
 
 362         info->dacops->dac_release(info);
 
 366 /* ------------------------------------------------------------------------- */
 
 377 #define DAC_PAR(info) ((struct ics5342_info *) info)
 
 379 /* LSB is set to distinguish unused slots */
 
 380 static const u8 ics5342_mode_table[DAC_MAX] = {
 
 381         [DAC_PSEUDO8_8]  = 0x01, [DAC_RGB1555_8]  = 0x21, [DAC_RGB0565_8]  = 0x61,
 
 382         [DAC_RGB0888_8]  = 0x41, [DAC_PSEUDO8_16] = 0x11, [DAC_RGB1555_16] = 0x31,
 
 383         [DAC_RGB0565_16] = 0x51, [DAC_RGB0888_16] = 0x91, [DAC_RGB8888_16] = 0x71
 
 386 static int ics5342_set_mode(struct dac_info *info, int mode)
 
 393         code = ics5342_mode_table[mode];
 
 398         dac_write_reg(info, 6, code & 0xF0);
 
 399         DAC_PAR(info)->mode = mode;
 
 404 static const struct svga_pll ics5342_pll = {3, 129, 3, 33, 0, 3,
 
 405         60000, 250000, 14318};
 
 407 /* pd4 - allow only posdivider 4 (r=2) */
 
 408 static const struct svga_pll ics5342_pll_pd4 = {3, 129, 3, 33, 2, 2,
 
 409         60000, 335000, 14318};
 
 411 /* 270 MHz should be upper bound for VCO clock according to specs,
 
 412    but that is too restrictive in pd4 case */
 
 414 static int ics5342_set_freq(struct dac_info *info, int channel, u32 freq)
 
 418         /* only postdivider 4 (r=2) is valid in mode DAC_PSEUDO8_16 */
 
 419         int rv = svga_compute_pll((DAC_PAR(info)->mode == DAC_PSEUDO8_16)
 
 420                                   ? &ics5342_pll_pd4 : &ics5342_pll,
 
 421                                   freq, &m, &n, &r, 0);
 
 426                 u8 code[6] = {4, 3, 5, m-2, 5, (n-2) | (r << 5)};
 
 427                 dac_write_regs(info, code, 3);
 
 432 static void ics5342_release(struct dac_info *info)
 
 434         ics5342_set_mode(info, DAC_PSEUDO8_8);
 
 438 static struct dac_ops ics5342_ops = {
 
 439         .dac_set_mode   = ics5342_set_mode,
 
 440         .dac_set_freq   = ics5342_set_freq,
 
 441         .dac_release    = ics5342_release
 
 445 static struct dac_info * ics5342_init(dac_read_regs_t drr, dac_write_regs_t dwr, void *data)
 
 447         struct dac_info *info = kzalloc(sizeof(struct ics5342_info), GFP_KERNEL);
 
 452         info->dacops = &ics5342_ops;
 
 453         info->dac_read_regs = drr;
 
 454         info->dac_write_regs = dwr;
 
 456         DAC_PAR(info)->mode = DAC_PSEUDO8_8; /* estimation */
 
 461 /* ------------------------------------------------------------------------- */
 
 464 static unsigned short dac_regs[4] = {0x3c8, 0x3c9, 0x3c6, 0x3c7};
 
 466 static void ark_dac_read_regs(void *data, u8 *code, int count)
 
 468         u8 regval = vga_rseq(NULL, 0x1C);
 
 472                 vga_wseq(NULL, 0x1C, regval | (code[0] & 4) ? 0x80 : 0);
 
 473                 code[1] = vga_r(NULL, dac_regs[code[0] & 3]);
 
 478         vga_wseq(NULL, 0x1C, regval);
 
 481 static void ark_dac_write_regs(void *data, u8 *code, int count)
 
 483         u8 regval = vga_rseq(NULL, 0x1C);
 
 487                 vga_wseq(NULL, 0x1C, regval | (code[0] & 4) ? 0x80 : 0);
 
 488                 vga_w(NULL, dac_regs[code[0] & 3], code[1]);
 
 493         vga_wseq(NULL, 0x1C, regval);
 
 497 static void ark_set_pixclock(struct fb_info *info, u32 pixclock)
 
 499         struct arkfb_info *par = info->par;
 
 502         int rv = dac_set_freq(par->dac, 0, 1000000000 / pixclock);
 
 504                 printk(KERN_ERR "fb%d: cannot set requested pixclock, keeping old value\n", info->node);
 
 508         /* Set VGA misc register  */
 
 509         regval = vga_r(NULL, VGA_MIS_R);
 
 510         vga_w(NULL, VGA_MIS_W, regval | VGA_MIS_ENB_PLL_LOAD);
 
 514 /* Open framebuffer */
 
 516 static int arkfb_open(struct fb_info *info, int user)
 
 518         struct arkfb_info *par = info->par;
 
 520         mutex_lock(&(par->open_lock));
 
 521         if (par->ref_count == 0) {
 
 522                 memset(&(par->state), 0, sizeof(struct vgastate));
 
 523                 par->state.flags = VGA_SAVE_MODE | VGA_SAVE_FONTS | VGA_SAVE_CMAP;
 
 524                 par->state.num_crtc = 0x60;
 
 525                 par->state.num_seq = 0x30;
 
 526                 save_vga(&(par->state));
 
 530         mutex_unlock(&(par->open_lock));
 
 535 /* Close framebuffer */
 
 537 static int arkfb_release(struct fb_info *info, int user)
 
 539         struct arkfb_info *par = info->par;
 
 541         mutex_lock(&(par->open_lock));
 
 542         if (par->ref_count == 0) {
 
 543                 mutex_unlock(&(par->open_lock));
 
 547         if (par->ref_count == 1) {
 
 548                 restore_vga(&(par->state));
 
 549                 dac_set_mode(par->dac, DAC_PSEUDO8_8);
 
 553         mutex_unlock(&(par->open_lock));
 
 558 /* Validate passed in var */
 
 560 static int arkfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
 
 564         /* Find appropriate format */
 
 565         rv = svga_match_format (arkfb_formats, var, NULL);
 
 568                 printk(KERN_ERR "fb%d: unsupported mode requested\n", info->node);
 
 572         /* Do not allow to have real resoulution larger than virtual */
 
 573         if (var->xres > var->xres_virtual)
 
 574                 var->xres_virtual = var->xres;
 
 576         if (var->yres > var->yres_virtual)
 
 577                 var->yres_virtual = var->yres;
 
 579         /* Round up xres_virtual to have proper alignment of lines */
 
 580         step = arkfb_formats[rv].xresstep - 1;
 
 581         var->xres_virtual = (var->xres_virtual+step) & ~step;
 
 584         /* Check whether have enough memory */
 
 585         mem = ((var->bits_per_pixel * var->xres_virtual) >> 3) * var->yres_virtual;
 
 586         if (mem > info->screen_size)
 
 588                 printk(KERN_ERR "fb%d: not enough framebuffer memory (%d kB requested , %d kB available)\n", info->node, mem >> 10, (unsigned int) (info->screen_size >> 10));
 
 592         rv = svga_check_timings (&ark_timing_regs, var, info->node);
 
 595                 printk(KERN_ERR "fb%d: invalid timings requested\n", info->node);
 
 599         /* Interlaced mode is broken */
 
 600         if (var->vmode & FB_VMODE_INTERLACED)
 
 606 /* Set video mode from par */
 
 608 static int arkfb_set_par(struct fb_info *info)
 
 610         struct arkfb_info *par = info->par;
 
 611         u32 value, mode, hmul, hdiv, offset_value, screen_size;
 
 612         u32 bpp = info->var.bits_per_pixel;
 
 616                 info->fix.ypanstep = 1;
 
 617                 info->fix.line_length = (info->var.xres_virtual * bpp) / 8;
 
 619                 info->flags &= ~FBINFO_MISC_TILEBLITTING;
 
 620                 info->tileops = NULL;
 
 622                 /* in 4bpp supports 8p wide tiles only, any tiles otherwise */
 
 623                 info->pixmap.blit_x = (bpp == 4) ? (1 << (8 - 1)) : (~(u32)0);
 
 624                 info->pixmap.blit_y = ~(u32)0;
 
 626                 offset_value = (info->var.xres_virtual * bpp) / 64;
 
 627                 screen_size = info->var.yres_virtual * info->fix.line_length;
 
 629                 info->fix.ypanstep = 16;
 
 630                 info->fix.line_length = 0;
 
 632                 info->flags |= FBINFO_MISC_TILEBLITTING;
 
 633                 info->tileops = &arkfb_tile_ops;
 
 635                 /* supports 8x16 tiles only */
 
 636                 info->pixmap.blit_x = 1 << (8 - 1);
 
 637                 info->pixmap.blit_y = 1 << (16 - 1);
 
 639                 offset_value = info->var.xres_virtual / 16;
 
 640                 screen_size = (info->var.xres_virtual * info->var.yres_virtual) / 64;
 
 643         info->var.xoffset = 0;
 
 644         info->var.yoffset = 0;
 
 645         info->var.activate = FB_ACTIVATE_NOW;
 
 647         /* Unlock registers */
 
 648         svga_wcrt_mask(0x11, 0x00, 0x80);
 
 650         /* Blank screen and turn off sync */
 
 651         svga_wseq_mask(0x01, 0x20, 0x20);
 
 652         svga_wcrt_mask(0x17, 0x00, 0x80);
 
 654         /* Set default values */
 
 655         svga_set_default_gfx_regs();
 
 656         svga_set_default_atc_regs();
 
 657         svga_set_default_seq_regs();
 
 658         svga_set_default_crt_regs();
 
 659         svga_wcrt_multi(ark_line_compare_regs, 0xFFFFFFFF);
 
 660         svga_wcrt_multi(ark_start_address_regs, 0);
 
 662         /* ARK specific initialization */
 
 663         svga_wseq_mask(0x10, 0x1F, 0x1F); /* enable linear framebuffer and full memory access */
 
 664         svga_wseq_mask(0x12, 0x03, 0x03); /* 4 MB linear framebuffer size */
 
 666         vga_wseq(NULL, 0x13, info->fix.smem_start >> 16);
 
 667         vga_wseq(NULL, 0x14, info->fix.smem_start >> 24);
 
 668         vga_wseq(NULL, 0x15, 0);
 
 669         vga_wseq(NULL, 0x16, 0);
 
 671         /* Set the FIFO threshold register */
 
 672         /* It is fascinating way to store 5-bit value in 8-bit register */
 
 673         regval = 0x10 | ((threshold & 0x0E) >> 1) | (threshold & 0x01) << 7 | (threshold & 0x10) << 1;
 
 674         vga_wseq(NULL, 0x18, regval);
 
 676         /* Set the offset register */
 
 677         pr_debug("fb%d: offset register       : %d\n", info->node, offset_value);
 
 678         svga_wcrt_multi(ark_offset_regs, offset_value);
 
 680         /* fix for hi-res textmode */
 
 681         svga_wcrt_mask(0x40, 0x08, 0x08);
 
 683         if (info->var.vmode & FB_VMODE_DOUBLE)
 
 684                 svga_wcrt_mask(0x09, 0x80, 0x80);
 
 686                 svga_wcrt_mask(0x09, 0x00, 0x80);
 
 688         if (info->var.vmode & FB_VMODE_INTERLACED)
 
 689                 svga_wcrt_mask(0x44, 0x04, 0x04);
 
 691                 svga_wcrt_mask(0x44, 0x00, 0x04);
 
 695         mode = svga_match_format(arkfb_formats, &(info->var), &(info->fix));
 
 697         /* Set mode-specific register values */
 
 700                 pr_debug("fb%d: text mode\n", info->node);
 
 701                 svga_set_textmode_vga_regs();
 
 703                 vga_wseq(NULL, 0x11, 0x10); /* basic VGA mode */
 
 704                 svga_wcrt_mask(0x46, 0x00, 0x04); /* 8bit pixel path */
 
 705                 dac_set_mode(par->dac, DAC_PSEUDO8_8);
 
 709                 pr_debug("fb%d: 4 bit pseudocolor\n", info->node);
 
 710                 vga_wgfx(NULL, VGA_GFX_MODE, 0x40);
 
 712                 vga_wseq(NULL, 0x11, 0x10); /* basic VGA mode */
 
 713                 svga_wcrt_mask(0x46, 0x00, 0x04); /* 8bit pixel path */
 
 714                 dac_set_mode(par->dac, DAC_PSEUDO8_8);
 
 717                 pr_debug("fb%d: 4 bit pseudocolor, planar\n", info->node);
 
 719                 vga_wseq(NULL, 0x11, 0x10); /* basic VGA mode */
 
 720                 svga_wcrt_mask(0x46, 0x00, 0x04); /* 8bit pixel path */
 
 721                 dac_set_mode(par->dac, DAC_PSEUDO8_8);
 
 724                 pr_debug("fb%d: 8 bit pseudocolor\n", info->node);
 
 726                 vga_wseq(NULL, 0x11, 0x16); /* 8bpp accel mode */
 
 728                 if (info->var.pixclock > 20000) {
 
 729                         pr_debug("fb%d: not using multiplex\n", info->node);
 
 730                         svga_wcrt_mask(0x46, 0x00, 0x04); /* 8bit pixel path */
 
 731                         dac_set_mode(par->dac, DAC_PSEUDO8_8);
 
 733                         pr_debug("fb%d: using multiplex\n", info->node);
 
 734                         svga_wcrt_mask(0x46, 0x04, 0x04); /* 16bit pixel path */
 
 735                         dac_set_mode(par->dac, DAC_PSEUDO8_16);
 
 740                 pr_debug("fb%d: 5/5/5 truecolor\n", info->node);
 
 742                 vga_wseq(NULL, 0x11, 0x1A); /* 16bpp accel mode */
 
 743                 svga_wcrt_mask(0x46, 0x04, 0x04); /* 16bit pixel path */
 
 744                 dac_set_mode(par->dac, DAC_RGB1555_16);
 
 747                 pr_debug("fb%d: 5/6/5 truecolor\n", info->node);
 
 749                 vga_wseq(NULL, 0x11, 0x1A); /* 16bpp accel mode */
 
 750                 svga_wcrt_mask(0x46, 0x04, 0x04); /* 16bit pixel path */
 
 751                 dac_set_mode(par->dac, DAC_RGB0565_16);
 
 754                 pr_debug("fb%d: 8/8/8 truecolor\n", info->node);
 
 756                 vga_wseq(NULL, 0x11, 0x16); /* 8bpp accel mode ??? */
 
 757                 svga_wcrt_mask(0x46, 0x04, 0x04); /* 16bit pixel path */
 
 758                 dac_set_mode(par->dac, DAC_RGB0888_16);
 
 763                 pr_debug("fb%d: 8/8/8/8 truecolor\n", info->node);
 
 765                 vga_wseq(NULL, 0x11, 0x1E); /* 32bpp accel mode */
 
 766                 svga_wcrt_mask(0x46, 0x04, 0x04); /* 16bit pixel path */
 
 767                 dac_set_mode(par->dac, DAC_RGB8888_16);
 
 771                 printk(KERN_ERR "fb%d: unsupported mode - bug\n", info->node);
 
 775         ark_set_pixclock(info, (hdiv * info->var.pixclock) / hmul);
 
 776         svga_set_timings(&ark_timing_regs, &(info->var), hmul, hdiv,
 
 777                          (info->var.vmode & FB_VMODE_DOUBLE)     ? 2 : 1,
 
 778                          (info->var.vmode & FB_VMODE_INTERLACED) ? 2 : 1,
 
 781         /* Set interlaced mode start/end register */
 
 782         value = info->var.xres + info->var.left_margin + info->var.right_margin + info->var.hsync_len;
 
 783         value = ((value * hmul / hdiv) / 8) - 5;
 
 784         vga_wcrt(NULL, 0x42, (value + 1) / 2);
 
 786         memset_io(info->screen_base, 0x00, screen_size);
 
 787         /* Device and screen back on */
 
 788         svga_wcrt_mask(0x17, 0x80, 0x80);
 
 789         svga_wseq_mask(0x01, 0x00, 0x20);
 
 794 /* Set a colour register */
 
 796 static int arkfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
 
 797                                 u_int transp, struct fb_info *fb)
 
 799         switch (fb->var.bits_per_pixel) {
 
 805                 if ((fb->var.bits_per_pixel == 4) &&
 
 806                     (fb->var.nonstd == 0)) {
 
 807                         outb(0xF0, VGA_PEL_MSK);
 
 808                         outb(regno*16, VGA_PEL_IW);
 
 810                         outb(0x0F, VGA_PEL_MSK);
 
 811                         outb(regno, VGA_PEL_IW);
 
 813                 outb(red >> 10, VGA_PEL_D);
 
 814                 outb(green >> 10, VGA_PEL_D);
 
 815                 outb(blue >> 10, VGA_PEL_D);
 
 821                 outb(0xFF, VGA_PEL_MSK);
 
 822                 outb(regno, VGA_PEL_IW);
 
 823                 outb(red >> 10, VGA_PEL_D);
 
 824                 outb(green >> 10, VGA_PEL_D);
 
 825                 outb(blue >> 10, VGA_PEL_D);
 
 831                 if (fb->var.green.length == 5)
 
 832                         ((u32*)fb->pseudo_palette)[regno] = ((red & 0xF800) >> 1) |
 
 833                                 ((green & 0xF800) >> 6) | ((blue & 0xF800) >> 11);
 
 834                 else if (fb->var.green.length == 6)
 
 835                         ((u32*)fb->pseudo_palette)[regno] = (red & 0xF800) |
 
 836                                 ((green & 0xFC00) >> 5) | ((blue & 0xF800) >> 11);
 
 845                 ((u32*)fb->pseudo_palette)[regno] = ((red & 0xFF00) << 8) |
 
 846                         (green & 0xFF00) | ((blue & 0xFF00) >> 8);
 
 855 /* Set the display blanking state */
 
 857 static int arkfb_blank(int blank_mode, struct fb_info *info)
 
 859         switch (blank_mode) {
 
 860         case FB_BLANK_UNBLANK:
 
 861                 pr_debug("fb%d: unblank\n", info->node);
 
 862                 svga_wseq_mask(0x01, 0x00, 0x20);
 
 863                 svga_wcrt_mask(0x17, 0x80, 0x80);
 
 865         case FB_BLANK_NORMAL:
 
 866                 pr_debug("fb%d: blank\n", info->node);
 
 867                 svga_wseq_mask(0x01, 0x20, 0x20);
 
 868                 svga_wcrt_mask(0x17, 0x80, 0x80);
 
 870         case FB_BLANK_POWERDOWN:
 
 871         case FB_BLANK_HSYNC_SUSPEND:
 
 872         case FB_BLANK_VSYNC_SUSPEND:
 
 873                 pr_debug("fb%d: sync down\n", info->node);
 
 874                 svga_wseq_mask(0x01, 0x20, 0x20);
 
 875                 svga_wcrt_mask(0x17, 0x00, 0x80);
 
 882 /* Pan the display */
 
 884 static int arkfb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
 
 888         /* Calculate the offset */
 
 889         if (var->bits_per_pixel == 0) {
 
 890                 offset = (var->yoffset / 16) * (var->xres_virtual / 2) + (var->xoffset / 2);
 
 891                 offset = offset >> 2;
 
 893                 offset = (var->yoffset * info->fix.line_length) +
 
 894                          (var->xoffset * var->bits_per_pixel / 8);
 
 895                 offset = offset >> ((var->bits_per_pixel == 4) ? 2 : 3);
 
 899         svga_wcrt_multi(ark_start_address_regs, offset);
 
 905 /* ------------------------------------------------------------------------- */
 
 908 /* Frame buffer operations */
 
 910 static struct fb_ops arkfb_ops = {
 
 911         .owner          = THIS_MODULE,
 
 912         .fb_open        = arkfb_open,
 
 913         .fb_release     = arkfb_release,
 
 914         .fb_check_var   = arkfb_check_var,
 
 915         .fb_set_par     = arkfb_set_par,
 
 916         .fb_setcolreg   = arkfb_setcolreg,
 
 917         .fb_blank       = arkfb_blank,
 
 918         .fb_pan_display = arkfb_pan_display,
 
 919         .fb_fillrect    = arkfb_fillrect,
 
 920         .fb_copyarea    = cfb_copyarea,
 
 921         .fb_imageblit   = arkfb_imageblit,
 
 922         .fb_get_caps    = svga_get_caps,
 
 926 /* ------------------------------------------------------------------------- */
 
 930 static int __devinit ark_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
 
 932         struct fb_info *info;
 
 933         struct arkfb_info *par;
 
 937         /* Ignore secondary VGA device because there is no VGA arbitration */
 
 938         if (! svga_primary_device(dev)) {
 
 939                 dev_info(&(dev->dev), "ignoring secondary device\n");
 
 943         /* Allocate and fill driver data structure */
 
 944         info = framebuffer_alloc(sizeof(struct arkfb_info), NULL);
 
 946                 dev_err(&(dev->dev), "cannot allocate memory\n");
 
 951         mutex_init(&par->open_lock);
 
 953         info->flags = FBINFO_PARTIAL_PAN_OK | FBINFO_HWACCEL_YPAN;
 
 954         info->fbops = &arkfb_ops;
 
 956         /* Prepare PCI device */
 
 957         rc = pci_enable_device(dev);
 
 959                 dev_err(&(dev->dev), "cannot enable PCI device\n");
 
 960                 goto err_enable_device;
 
 963         rc = pci_request_regions(dev, "arkfb");
 
 965                 dev_err(&(dev->dev), "cannot reserve framebuffer region\n");
 
 966                 goto err_request_regions;
 
 969         par->dac = ics5342_init(ark_dac_read_regs, ark_dac_write_regs, info);
 
 972                 dev_err(&(dev->dev), "RAMDAC initialization failed\n");
 
 976         info->fix.smem_start = pci_resource_start(dev, 0);
 
 977         info->fix.smem_len = pci_resource_len(dev, 0);
 
 979         /* Map physical IO memory address into kernel space */
 
 980         info->screen_base = pci_iomap(dev, 0, 0);
 
 981         if (! info->screen_base) {
 
 983                 dev_err(&(dev->dev), "iomap for framebuffer failed\n");
 
 987         /* FIXME get memsize */
 
 988         regval = vga_rseq(NULL, 0x10);
 
 989         info->screen_size = (1 << (regval >> 6)) << 20;
 
 990         info->fix.smem_len = info->screen_size;
 
 992         strcpy(info->fix.id, "ARK 2000PV");
 
 993         info->fix.mmio_start = 0;
 
 994         info->fix.mmio_len = 0;
 
 995         info->fix.type = FB_TYPE_PACKED_PIXELS;
 
 996         info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
 
 997         info->fix.ypanstep = 0;
 
 998         info->fix.accel = FB_ACCEL_NONE;
 
 999         info->pseudo_palette = (void*) (par->pseudo_palette);
 
1001         /* Prepare startup mode */
 
1002         rc = fb_find_mode(&(info->var), info, mode, NULL, 0, NULL, 8);
 
1003         if (! ((rc == 1) || (rc == 2))) {
 
1005                 dev_err(&(dev->dev), "mode %s not found\n", mode);
 
1009         rc = fb_alloc_cmap(&info->cmap, 256, 0);
 
1011                 dev_err(&(dev->dev), "cannot allocate colormap\n");
 
1012                 goto err_alloc_cmap;
 
1015         rc = register_framebuffer(info);
 
1017                 dev_err(&(dev->dev), "cannot register framebugger\n");
 
1021         printk(KERN_INFO "fb%d: %s on %s, %d MB RAM\n", info->node, info->fix.id,
 
1022                  pci_name(dev), info->fix.smem_len >> 20);
 
1024         /* Record a reference to the driver data */
 
1025         pci_set_drvdata(dev, info);
 
1030                 par->mtrr_reg = mtrr_add(info->fix.smem_start, info->fix.smem_len, MTRR_TYPE_WRCOMB, 1);
 
1036         /* Error handling */
 
1038         fb_dealloc_cmap(&info->cmap);
 
1041         pci_iounmap(dev, info->screen_base);
 
1043         dac_release(par->dac);
 
1045         pci_release_regions(dev);
 
1046 err_request_regions:
 
1047 /*      pci_disable_device(dev); */
 
1049         framebuffer_release(info);
 
1055 static void __devexit ark_pci_remove(struct pci_dev *dev)
 
1057         struct fb_info *info = pci_get_drvdata(dev);
 
1060                 struct arkfb_info *par = info->par;
 
1063                 if (par->mtrr_reg >= 0) {
 
1064                         mtrr_del(par->mtrr_reg, 0, 0);
 
1069                 dac_release(par->dac);
 
1070                 unregister_framebuffer(info);
 
1071                 fb_dealloc_cmap(&info->cmap);
 
1073                 pci_iounmap(dev, info->screen_base);
 
1074                 pci_release_regions(dev);
 
1075 /*              pci_disable_device(dev); */
 
1077                 pci_set_drvdata(dev, NULL);
 
1078                 framebuffer_release(info);
 
1086 static int ark_pci_suspend (struct pci_dev* dev, pm_message_t state)
 
1088         struct fb_info *info = pci_get_drvdata(dev);
 
1089         struct arkfb_info *par = info->par;
 
1091         dev_info(&(dev->dev), "suspend\n");
 
1093         acquire_console_sem();
 
1094         mutex_lock(&(par->open_lock));
 
1096         if ((state.event == PM_EVENT_FREEZE) || (par->ref_count == 0)) {
 
1097                 mutex_unlock(&(par->open_lock));
 
1098                 release_console_sem();
 
1102         fb_set_suspend(info, 1);
 
1104         pci_save_state(dev);
 
1105         pci_disable_device(dev);
 
1106         pci_set_power_state(dev, pci_choose_state(dev, state));
 
1108         mutex_unlock(&(par->open_lock));
 
1109         release_console_sem();
 
1117 static int ark_pci_resume (struct pci_dev* dev)
 
1119         struct fb_info *info = pci_get_drvdata(dev);
 
1120         struct arkfb_info *par = info->par;
 
1122         dev_info(&(dev->dev), "resume\n");
 
1124         acquire_console_sem();
 
1125         mutex_lock(&(par->open_lock));
 
1127         if (par->ref_count == 0) {
 
1128                 mutex_unlock(&(par->open_lock));
 
1129                 release_console_sem();
 
1133         pci_set_power_state(dev, PCI_D0);
 
1134         pci_restore_state(dev);
 
1136         if (pci_enable_device(dev))
 
1139         pci_set_master(dev);
 
1141         arkfb_set_par(info);
 
1142         fb_set_suspend(info, 0);
 
1144         mutex_unlock(&(par->open_lock));
 
1146         release_console_sem();
 
1150 #define ark_pci_suspend NULL
 
1151 #define ark_pci_resume NULL
 
1152 #endif /* CONFIG_PM */
 
1154 /* List of boards that we are trying to support */
 
1156 static struct pci_device_id ark_devices[] __devinitdata = {
 
1157         {PCI_DEVICE(0xEDD8, 0xA099)},
 
1158         {0, 0, 0, 0, 0, 0, 0}
 
1162 MODULE_DEVICE_TABLE(pci, ark_devices);
 
1164 static struct pci_driver arkfb_pci_driver = {
 
1166         .id_table       = ark_devices,
 
1167         .probe          = ark_pci_probe,
 
1168         .remove         = __devexit_p(ark_pci_remove),
 
1169         .suspend        = ark_pci_suspend,
 
1170         .resume         = ark_pci_resume,
 
1175 static void __exit arkfb_cleanup(void)
 
1177         pr_debug("arkfb: cleaning up\n");
 
1178         pci_unregister_driver(&arkfb_pci_driver);
 
1181 /* Driver Initialisation */
 
1183 static int __init arkfb_init(void)
 
1187         char *option = NULL;
 
1189         if (fb_get_options("arkfb", &option))
 
1192         if (option && *option)
 
1196         pr_debug("arkfb: initializing\n");
 
1197         return pci_register_driver(&arkfb_pci_driver);
 
1200 module_init(arkfb_init);
 
1201 module_exit(arkfb_cleanup);