diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 15:20:36 -0700 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 15:20:36 -0700 |
commit | 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch) | |
tree | 0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/video/neofb.c |
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history,
even though we have it. We can create a separate "historical" git
archive of that later if we want to, and in the meantime it's about
3.2GB when imported into git - space that would just make the early
git days unnecessarily complicated, when we don't have a lot of good
infrastructure for it.
Let it rip!
Diffstat (limited to 'drivers/video/neofb.c')
-rw-r--r-- | drivers/video/neofb.c | 2315 |
1 files changed, 2315 insertions, 0 deletions
diff --git a/drivers/video/neofb.c b/drivers/video/neofb.c new file mode 100644 index 00000000000..5d424a30270 --- /dev/null +++ b/drivers/video/neofb.c @@ -0,0 +1,2315 @@ +/* + * linux/drivers/video/neofb.c -- NeoMagic Framebuffer Driver + * + * Copyright (c) 2001-2002 Denis Oliver Kropp <dok@directfb.org> + * + * + * Card specific code is based on XFree86's neomagic driver. + * Framebuffer framework code is based on code of cyber2000fb. + * + * This file is subject to the terms and conditions of the GNU General + * Public License. See the file COPYING in the main directory of this + * archive for more details. + * + * + * 0.4.1 + * - Cosmetic changes (dok) + * + * 0.4 + * - Toshiba Libretto support, allow modes larger than LCD size if + * LCD is disabled, keep BIOS settings if internal/external display + * haven't been enabled explicitly + * (Thomas J. Moore <dark@mama.indstate.edu>) + * + * 0.3.3 + * - Porting over to new fbdev api. (jsimmons) + * + * 0.3.2 + * - got rid of all floating point (dok) + * + * 0.3.1 + * - added module license (dok) + * + * 0.3 + * - hardware accelerated clear and move for 2200 and above (dok) + * - maximum allowed dotclock is handled now (dok) + * + * 0.2.1 + * - correct panning after X usage (dok) + * - added module and kernel parameters (dok) + * - no stretching if external display is enabled (dok) + * + * 0.2 + * - initial version (dok) + * + * + * TODO + * - ioctl for internal/external switching + * - blanking + * - 32bit depth support, maybe impossible + * - disable pan-on-sync, need specs + * + * BUGS + * - white margin on bootup like with tdfxfb (colormap problem?) + * + */ + +#include <linux/config.h> +#include <linux/module.h> +#include <linux/kernel.h> +#include <linux/errno.h> +#include <linux/string.h> +#include <linux/mm.h> +#include <linux/tty.h> +#include <linux/slab.h> +#include <linux/delay.h> +#include <linux/fb.h> +#include <linux/pci.h> +#include <linux/init.h> +#ifdef CONFIG_TOSHIBA +#include <linux/toshiba.h> +extern int tosh_smm(SMMRegisters *regs); +#endif + +#include <asm/io.h> +#include <asm/irq.h> +#include <asm/pgtable.h> +#include <asm/system.h> +#include <asm/uaccess.h> + +#ifdef CONFIG_MTRR +#include <asm/mtrr.h> +#endif + +#include <video/vga.h> +#include <video/neomagic.h> + +#define NEOFB_VERSION "0.4.2" + +/* --------------------------------------------------------------------- */ + +static int internal; +static int external; +static int libretto; +static int nostretch; +static int nopciburst; +static char *mode_option __devinitdata = NULL; + +#ifdef MODULE + +MODULE_AUTHOR("(c) 2001-2002 Denis Oliver Kropp <dok@convergence.de>"); +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("FBDev driver for NeoMagic PCI Chips"); +module_param(internal, bool, 0); +MODULE_PARM_DESC(internal, "Enable output on internal LCD Display."); +module_param(external, bool, 0); +MODULE_PARM_DESC(external, "Enable output on external CRT."); +module_param(libretto, bool, 0); +MODULE_PARM_DESC(libretto, "Force Libretto 100/110 800x480 LCD."); +module_param(nostretch, bool, 0); +MODULE_PARM_DESC(nostretch, + "Disable stretching of modes smaller than LCD."); +module_param(nopciburst, bool, 0); +MODULE_PARM_DESC(nopciburst, "Disable PCI burst mode."); +module_param(mode_option, charp, 0); +MODULE_PARM_DESC(mode_option, "Preferred video mode ('640x480-8@60', etc)"); + +#endif + + +/* --------------------------------------------------------------------- */ + +static biosMode bios8[] = { + {320, 240, 0x40}, + {300, 400, 0x42}, + {640, 400, 0x20}, + {640, 480, 0x21}, + {800, 600, 0x23}, + {1024, 768, 0x25}, +}; + +static biosMode bios16[] = { + {320, 200, 0x2e}, + {320, 240, 0x41}, + {300, 400, 0x43}, + {640, 480, 0x31}, + {800, 600, 0x34}, + {1024, 768, 0x37}, +}; + +static biosMode bios24[] = { + {640, 480, 0x32}, + {800, 600, 0x35}, + {1024, 768, 0x38} +}; + +#ifdef NO_32BIT_SUPPORT_YET +/* FIXME: guessed values, wrong */ +static biosMode bios32[] = { + {640, 480, 0x33}, + {800, 600, 0x36}, + {1024, 768, 0x39} +}; +#endif + +static inline void write_le32(int regindex, u32 val, const struct neofb_par *par) +{ + writel(val, par->neo2200 + par->cursorOff + regindex); +} + +static int neoFindMode(int xres, int yres, int depth) +{ + int xres_s; + int i, size; + biosMode *mode; + + switch (depth) { + case 8: + size = sizeof(bios8) / sizeof(biosMode); + mode = bios8; + break; + case 16: + size = sizeof(bios16) / sizeof(biosMode); + mode = bios16; + break; + case 24: + size = sizeof(bios24) / sizeof(biosMode); + mode = bios24; + break; +#ifdef NO_32BIT_SUPPORT_YET + case 32: + size = sizeof(bios32) / sizeof(biosMode); + mode = bios32; + break; +#endif + default: + return 0; + } + + for (i = 0; i < size; i++) { + if (xres <= mode[i].x_res) { + xres_s = mode[i].x_res; + for (; i < size; i++) { + if (mode[i].x_res != xres_s) + return mode[i - 1].mode; + if (yres <= mode[i].y_res) + return mode[i].mode; + } + } + } + return mode[size - 1].mode; +} + +/* + * neoCalcVCLK -- + * + * Determine the closest clock frequency to the one requested. + */ +#define REF_FREQ 0xe517 /* 14.31818 in 20.12 fixed point */ +#define MAX_N 127 +#define MAX_D 31 +#define MAX_F 1 + +static void neoCalcVCLK(const struct fb_info *info, + struct neofb_par *par, long freq) +{ + int n, d, f; + int n_best = 0, d_best = 0, f_best = 0; + long f_best_diff = (0x7ffff << 12); /* 20.12 */ + long f_target = (freq << 12) / 1000; /* 20.12 */ + + for (f = 0; f <= MAX_F; f++) + for (n = 0; n <= MAX_N; n++) + for (d = 0; d <= MAX_D; d++) { + long f_out; /* 20.12 */ + long f_diff; /* 20.12 */ + + f_out = + ((((n + 1) << 12) / ((d + + 1) * + (1 << f))) >> 12) + * REF_FREQ; + f_diff = abs(f_out - f_target); + if (f_diff < f_best_diff) { + f_best_diff = f_diff; + n_best = n; + d_best = d; + f_best = f; + } + } + + if (info->fix.accel == FB_ACCEL_NEOMAGIC_NM2200 || + info->fix.accel == FB_ACCEL_NEOMAGIC_NM2230 || + info->fix.accel == FB_ACCEL_NEOMAGIC_NM2360 || + info->fix.accel == FB_ACCEL_NEOMAGIC_NM2380) { + /* NOT_DONE: We are trying the full range of the 2200 clock. + We should be able to try n up to 2047 */ + par->VCLK3NumeratorLow = n_best; + par->VCLK3NumeratorHigh = (f_best << 7); + } else + par->VCLK3NumeratorLow = n_best | (f_best << 7); + + par->VCLK3Denominator = d_best; + +#ifdef NEOFB_DEBUG + printk("neoVCLK: f:%d NumLow=%d NumHi=%d Den=%d Df=%d\n", + f_target >> 12, + par->VCLK3NumeratorLow, + par->VCLK3NumeratorHigh, + par->VCLK3Denominator, f_best_diff >> 12); +#endif +} + +/* + * vgaHWInit -- + * Handle the initialization, etc. of a screen. + * Return FALSE on failure. + */ + +static int vgaHWInit(const struct fb_var_screeninfo *var, + const struct fb_info *info, + struct neofb_par *par, struct xtimings *timings) +{ + par->MiscOutReg = 0x23; + + if (!(timings->sync & FB_SYNC_HOR_HIGH_ACT)) + par->MiscOutReg |= 0x40; + + if (!(timings->sync & FB_SYNC_VERT_HIGH_ACT)) + par->MiscOutReg |= 0x80; + + /* + * Time Sequencer + */ + par->Sequencer[0] = 0x00; + par->Sequencer[1] = 0x01; + par->Sequencer[2] = 0x0F; + par->Sequencer[3] = 0x00; /* Font select */ + par->Sequencer[4] = 0x0E; /* Misc */ + + /* + * CRTC Controller + */ + par->CRTC[0] = (timings->HTotal >> 3) - 5; + par->CRTC[1] = (timings->HDisplay >> 3) - 1; + par->CRTC[2] = (timings->HDisplay >> 3) - 1; + par->CRTC[3] = (((timings->HTotal >> 3) - 1) & 0x1F) | 0x80; + par->CRTC[4] = (timings->HSyncStart >> 3); + par->CRTC[5] = ((((timings->HTotal >> 3) - 1) & 0x20) << 2) + | (((timings->HSyncEnd >> 3)) & 0x1F); + par->CRTC[6] = (timings->VTotal - 2) & 0xFF; + par->CRTC[7] = (((timings->VTotal - 2) & 0x100) >> 8) + | (((timings->VDisplay - 1) & 0x100) >> 7) + | ((timings->VSyncStart & 0x100) >> 6) + | (((timings->VDisplay - 1) & 0x100) >> 5) + | 0x10 | (((timings->VTotal - 2) & 0x200) >> 4) + | (((timings->VDisplay - 1) & 0x200) >> 3) + | ((timings->VSyncStart & 0x200) >> 2); + par->CRTC[8] = 0x00; + par->CRTC[9] = (((timings->VDisplay - 1) & 0x200) >> 4) | 0x40; + + if (timings->dblscan) + par->CRTC[9] |= 0x80; + + par->CRTC[10] = 0x00; + par->CRTC[11] = 0x00; + par->CRTC[12] = 0x00; + par->CRTC[13] = 0x00; + par->CRTC[14] = 0x00; + par->CRTC[15] = 0x00; + par->CRTC[16] = timings->VSyncStart & 0xFF; + par->CRTC[17] = (timings->VSyncEnd & 0x0F) | 0x20; + par->CRTC[18] = (timings->VDisplay - 1) & 0xFF; + par->CRTC[19] = var->xres_virtual >> 4; + par->CRTC[20] = 0x00; + par->CRTC[21] = (timings->VDisplay - 1) & 0xFF; + par->CRTC[22] = (timings->VTotal - 1) & 0xFF; + par->CRTC[23] = 0xC3; + par->CRTC[24] = 0xFF; + + /* + * are these unnecessary? + * vgaHWHBlankKGA(mode, regp, 0, KGA_FIX_OVERSCAN | KGA_ENABLE_ON_ZERO); + * vgaHWVBlankKGA(mode, regp, 0, KGA_FIX_OVERSCAN | KGA_ENABLE_ON_ZERO); + */ + + /* + * Graphics Display Controller + */ + par->Graphics[0] = 0x00; + par->Graphics[1] = 0x00; + par->Graphics[2] = 0x00; + par->Graphics[3] = 0x00; + par->Graphics[4] = 0x00; + par->Graphics[5] = 0x40; + par->Graphics[6] = 0x05; /* only map 64k VGA memory !!!! */ + par->Graphics[7] = 0x0F; + par->Graphics[8] = 0xFF; + + + par->Attribute[0] = 0x00; /* standard colormap translation */ + par->Attribute[1] = 0x01; + par->Attribute[2] = 0x02; + par->Attribute[3] = 0x03; + par->Attribute[4] = 0x04; + par->Attribute[5] = 0x05; + par->Attribute[6] = 0x06; + par->Attribute[7] = 0x07; + par->Attribute[8] = 0x08; + par->Attribute[9] = 0x09; + par->Attribute[10] = 0x0A; + par->Attribute[11] = 0x0B; + par->Attribute[12] = 0x0C; + par->Attribute[13] = 0x0D; + par->Attribute[14] = 0x0E; + par->Attribute[15] = 0x0F; + par->Attribute[16] = 0x41; + par->Attribute[17] = 0xFF; + par->Attribute[18] = 0x0F; + par->Attribute[19] = 0x00; + par->Attribute[20] = 0x00; + return 0; +} + +static void vgaHWLock(struct vgastate *state) +{ + /* Protect CRTC[0-7] */ + vga_wcrt(state->vgabase, 0x11, vga_rcrt(state->vgabase, 0x11) | 0x80); +} + +static void vgaHWUnlock(void) +{ + /* Unprotect CRTC[0-7] */ + vga_wcrt(NULL, 0x11, vga_rcrt(NULL, 0x11) & ~0x80); +} + +static void neoLock(struct vgastate *state) +{ + vga_wgfx(state->vgabase, 0x09, 0x00); + vgaHWLock(state); +} + +static void neoUnlock(void) +{ + vgaHWUnlock(); + vga_wgfx(NULL, 0x09, 0x26); +} + +/* + * VGA Palette management + */ +static int paletteEnabled = 0; + +static inline void VGAenablePalette(void) +{ + vga_r(NULL, VGA_IS1_RC); + vga_w(NULL, VGA_ATT_W, 0x00); + paletteEnabled = 1; +} + +static inline void VGAdisablePalette(void) +{ + vga_r(NULL, VGA_IS1_RC); + vga_w(NULL, VGA_ATT_W, 0x20); + paletteEnabled = 0; +} + +static inline void VGAwATTR(u8 index, u8 value) +{ + if (paletteEnabled) + index &= ~0x20; + else + index |= 0x20; + + vga_r(NULL, VGA_IS1_RC); + vga_wattr(NULL, index, value); +} + +static void vgaHWProtect(int on) +{ + unsigned char tmp; + + if (on) { + /* + * Turn off screen and disable sequencer. + */ + tmp = vga_rseq(NULL, 0x01); + vga_wseq(NULL, 0x00, 0x01); /* Synchronous Reset */ + vga_wseq(NULL, 0x01, tmp | 0x20); /* disable the display */ + + VGAenablePalette(); + } else { + /* + * Reenable sequencer, then turn on screen. + */ + tmp = vga_rseq(NULL, 0x01); + vga_wseq(NULL, 0x01, tmp & ~0x20); /* reenable display */ + vga_wseq(NULL, 0x00, 0x03); /* clear synchronousreset */ + + VGAdisablePalette(); + } +} + +static void vgaHWRestore(const struct fb_info *info, + const struct neofb_par *par) +{ + int i; + + vga_w(NULL, VGA_MIS_W, par->MiscOutReg); + + for (i = 1; i < 5; i++) + vga_wseq(NULL, i, par->Sequencer[i]); + + /* Ensure CRTC registers 0-7 are unlocked by clearing bit 7 or CRTC[17] */ + vga_wcrt(NULL, 17, par->CRTC[17] & ~0x80); + + for (i = 0; i < 25; i++) + vga_wcrt(NULL, i, par->CRTC[i]); + + for (i = 0; i < 9; i++) + vga_wgfx(NULL, i, par->Graphics[i]); + + VGAenablePalette(); + + for (i = 0; i < 21; i++) + VGAwATTR(i, par->Attribute[i]); + + VGAdisablePalette(); +} + + +/* -------------------- Hardware specific routines ------------------------- */ + +/* + * Hardware Acceleration for Neo2200+ + */ +static inline int neo2200_sync(struct fb_info *info) +{ + struct neofb_par *par = (struct neofb_par *) info->par; + int waitcycles; + + while (readl(&par->neo2200->bltStat) & 1) + waitcycles++; + return 0; +} + +static inline void neo2200_wait_fifo(struct fb_info *info, + int requested_fifo_space) +{ + // ndev->neo.waitfifo_calls++; + // ndev->neo.waitfifo_sum += requested_fifo_space; + + /* FIXME: does not work + if (neo_fifo_space < requested_fifo_space) + { + neo_fifo_waitcycles++; + + while (1) + { + neo_fifo_space = (neo2200->bltStat >> 8); + if (neo_fifo_space >= requested_fifo_space) + break; + } + } + else + { + neo_fifo_cache_hits++; + } + + neo_fifo_space -= requested_fifo_space; + */ + + neo2200_sync(info); +} + +static inline void neo2200_accel_init(struct fb_info *info, + struct fb_var_screeninfo *var) +{ + struct neofb_par *par = (struct neofb_par *) info->par; + Neo2200 __iomem *neo2200 = par->neo2200; + u32 bltMod, pitch; + + neo2200_sync(info); + + switch (var->bits_per_pixel) { + case 8: + bltMod = NEO_MODE1_DEPTH8; + pitch = var->xres_virtual; + break; + case 15: + case 16: + bltMod = NEO_MODE1_DEPTH16; + pitch = var->xres_virtual * 2; + break; + case 24: + bltMod = NEO_MODE1_DEPTH24; + pitch = var->xres_virtual * 3; + break; + default: + printk(KERN_ERR + "neofb: neo2200_accel_init: unexpected bits per pixel!\n"); + return; + } + + writel(bltMod << 16, &neo2200->bltStat); + writel((pitch << 16) | pitch, &neo2200->pitch); +} + +/* --------------------------------------------------------------------- */ + +static int +neofb_open(struct fb_info *info, int user) +{ + struct neofb_par *par = (struct neofb_par *) info->par; + int cnt = atomic_read(&par->ref_count); + + if (!cnt) { + memset(&par->state, 0, sizeof(struct vgastate)); + par->state.flags = VGA_SAVE_MODE | VGA_SAVE_FONTS; + save_vga(&par->state); + } + atomic_inc(&par->ref_count); + return 0; +} + +static int +neofb_release(struct fb_info *info, int user) +{ + struct neofb_par *par = (struct neofb_par *) info->par; + int cnt = atomic_read(&par->ref_count); + + if (!cnt) + return -EINVAL; + if (cnt == 1) { + restore_vga(&par->state); + } + atomic_dec(&par->ref_count); + return 0; +} + +static int +neofb_check_var(struct fb_var_screeninfo *var, struct fb_info *info) +{ + struct neofb_par *par = (struct neofb_par *) info->par; + unsigned int pixclock = var->pixclock; + struct xtimings timings; + int memlen, vramlen; + int mode_ok = 0; + + DBG("neofb_check_var"); + + if (!pixclock) + pixclock = 10000; /* 10ns = 100MHz */ + timings.pixclock = 1000000000 / pixclock; + if (timings.pixclock < 1) + timings.pixclock = 1; + + if (timings.pixclock > par->maxClock) + return -EINVAL; + + timings.dblscan = var->vmode & FB_VMODE_DOUBLE; + timings.interlaced = var->vmode & FB_VMODE_INTERLACED; + timings.HDisplay = var->xres; + timings.HSyncStart = timings.HDisplay + var->right_margin; + timings.HSyncEnd = timings.HSyncStart + var->hsync_len; + timings.HTotal = timings.HSyncEnd + var->left_margin; + timings.VDisplay = var->yres; + timings.VSyncStart = timings.VDisplay + var->lower_margin; + timings.VSyncEnd = timings.VSyncStart + var->vsync_len; + timings.VTotal = timings.VSyncEnd + var->upper_margin; + timings.sync = var->sync; + + /* Is the mode larger than the LCD panel? */ + if (par->internal_display && + ((var->xres > par->NeoPanelWidth) || + (var->yres > par->NeoPanelHeight))) { + printk(KERN_INFO + "Mode (%dx%d) larger than the LCD panel (%dx%d)\n", + var->xres, var->yres, par->NeoPanelWidth, + par->NeoPanelHeight); + return -EINVAL; + } + + /* Is the mode one of the acceptable sizes? */ + if (!par->internal_display) + mode_ok = 1; + else { + switch (var->xres) { + case 1280: + if (var->yres == 1024) + mode_ok = 1; + break; + case 1024: + if (var->yres == 768) + mode_ok = 1; + break; + case 800: + if (var->yres == (par->libretto ? 480 : 600)) + mode_ok = 1; + break; + case 640: + if (var->yres == 480) + mode_ok = 1; + break; + } + } + + if (!mode_ok) { + printk(KERN_INFO + "Mode (%dx%d) won't display properly on LCD\n", + var->xres, var->yres); + return -EINVAL; + } + + var->red.msb_right = 0; + var->green.msb_right = 0; + var->blue.msb_right = 0; + + switch (var->bits_per_pixel) { + case 8: /* PSEUDOCOLOUR, 256 */ + var->transp.offset = 0; + var->transp.length = 0; + var->red.offset = 0; + var->red.length = 8; + var->green.offset = 0; + var->green.length = 8; + var->blue.offset = 0; + var->blue.length = 8; + break; + + case 16: /* DIRECTCOLOUR, 64k */ + var->transp.offset = 0; + var->transp.length = 0; + var->red.offset = 11; + var->red.length = 5; + var->green.offset = 5; + var->green.length = 6; + var->blue.offset = 0; + var->blue.length = 5; + break; + + case 24: /* TRUECOLOUR, 16m */ + var->transp.offset = 0; + var->transp.length = 0; + var->red.offset = 16; + var->red.length = 8; + var->green.offset = 8; + var->green.length = 8; + var->blue.offset = 0; + var->blue.length = 8; + break; + +#ifdef NO_32BIT_SUPPORT_YET + case 32: /* TRUECOLOUR, 16m */ + var->transp.offset = 24; + var->transp.length = 8; + var->red.offset = 16; + var->red.length = 8; + var->green.offset = 8; + var->green.length = 8; + var->blue.offset = 0; + var->blue.length = 8; + break; +#endif + default: + printk(KERN_WARNING "neofb: no support for %dbpp\n", + var->bits_per_pixel); + return -EINVAL; + } + + vramlen = info->fix.smem_len; + if (vramlen > 4 * 1024 * 1024) + vramlen = 4 * 1024 * 1024; + + if (var->yres_virtual < var->yres) + var->yres_virtual = var->yres; + if (var->xres_virtual < var->xres) + var->xres_virtual = var->xres; + + memlen = var->xres_virtual * var->bits_per_pixel * var->yres_virtual >> 3; + + if (memlen > vramlen) { + var->yres_virtual = vramlen * 8 / (var->xres_virtual * + var->bits_per_pixel); + memlen = var->xres_virtual * var->bits_per_pixel * + var->yres_virtual / 8; + } + + /* we must round yres/xres down, we already rounded y/xres_virtual up + if it was possible. We should return -EINVAL, but I disagree */ + if (var->yres_virtual < var->yres) + var->yres = var->yres_virtual; + if (var->xres_virtual < var->xres) + var->xres = var->xres_virtual; + if (var->xoffset + var->xres > var->xres_virtual) + var->xoffset = var->xres_virtual - var->xres; + if (var->yoffset + var->yres > var->yres_virtual) + var->yoffset = var->yres_virtual - var->yres; + + var->nonstd = 0; + var->height = -1; + var->width = -1; + + if (var->bits_per_pixel >= 24 || !par->neo2200) + var->accel_flags &= ~FB_ACCELF_TEXT; + return 0; +} + +static int neofb_set_par(struct fb_info *info) +{ + struct neofb_par *par = (struct neofb_par *) info->par; + struct xtimings timings; + unsigned char temp; + int i, clock_hi = 0; + int lcd_stretch; + int hoffset, voffset; + + DBG("neofb_set_par"); + + neoUnlock(); + + vgaHWProtect(1); /* Blank the screen */ + + timings.dblscan = info->var.vmode & FB_VMODE_DOUBLE; + timings.interlaced = info->var.vmode & FB_VMODE_INTERLACED; + timings.HDisplay = info->var.xres; + timings.HSyncStart = timings.HDisplay + info->var.right_margin; + timings.HSyncEnd = timings.HSyncStart + info->var.hsync_len; + timings.HTotal = timings.HSyncEnd + info->var.left_margin; + timings.VDisplay = info->var.yres; + timings.VSyncStart = timings.VDisplay + info->var.lower_margin; + timings.VSyncEnd = timings.VSyncStart + info->var.vsync_len; + timings.VTotal = timings.VSyncEnd + info->var.upper_margin; + timings.sync = info->var.sync; + timings.pixclock = PICOS2KHZ(info->var.pixclock); + + if (timings.pixclock < 1) + timings.pixclock = 1; + + /* + * This will allocate the datastructure and initialize all of the + * generic VGA registers. + */ + + if (vgaHWInit(&info->var, info, par, &timings)) + return -EINVAL; + + /* + * The default value assigned by vgaHW.c is 0x41, but this does + * not work for NeoMagic. + */ + par->Attribute[16] = 0x01; + + switch (info->var.bits_per_pixel) { + case 8: + par->CRTC[0x13] = info->var.xres_virtual >> 3; + par->ExtCRTOffset = info->var.xres_virtual >> 11; + par->ExtColorModeSelect = 0x11; + break; + case 16: + par->CRTC[0x13] = info->var.xres_virtual >> 2; + par->ExtCRTOffset = info->var.xres_virtual >> 10; + par->ExtColorModeSelect = 0x13; + break; + case 24: + par->CRTC[0x13] = (info->var.xres_virtual * 3) >> 3; + par->ExtCRTOffset = (info->var.xres_virtual * 3) >> 11; + par->ExtColorModeSelect = 0x14; + break; +#ifdef NO_32BIT_SUPPORT_YET + case 32: /* FIXME: guessed values */ + par->CRTC[0x13] = info->var.xres_virtual >> 1; + par->ExtCRTOffset = info->var.xres_virtual >> 9; + par->ExtColorModeSelect = 0x15; + break; +#endif + default: + break; + } + + par->ExtCRTDispAddr = 0x10; + + /* Vertical Extension */ + par->VerticalExt = (((timings.VTotal - 2) & 0x400) >> 10) + | (((timings.VDisplay - 1) & 0x400) >> 9) + | (((timings.VSyncStart) & 0x400) >> 8) + | (((timings.VSyncStart) & 0x400) >> 7); + + /* Fast write bursts on unless disabled. */ + if (par->pci_burst) + par->SysIfaceCntl1 = 0x30; + else + par->SysIfaceCntl1 = 0x00; + + par->SysIfaceCntl2 = 0xc0; /* VESA Bios sets this to 0x80! */ + + /* Enable any user specified display devices. */ + par->PanelDispCntlReg1 = 0x00; + if (par->internal_display) + par->PanelDispCntlReg1 |= 0x02; + if (par->external_display) + par->PanelDispCntlReg1 |= 0x01; + + /* If the user did not specify any display devices, then... */ + if (par->PanelDispCntlReg1 == 0x00) { + /* Default to internal (i.e., LCD) only. */ + par->PanelDispCntlReg1 |= 0x02; + } + + /* If we are using a fixed mode, then tell the chip we are. */ + switch (info->var.xres) { + case 1280: + par->PanelDispCntlReg1 |= 0x60; + break; + case 1024: + par->PanelDispCntlReg1 |= 0x40; + break; + case 800: + par->PanelDispCntlReg1 |= 0x20; + break; + case 640: + default: + break; + } + + /* Setup shadow register locking. */ + switch (par->PanelDispCntlReg1 & 0x03) { + case 0x01: /* External CRT only mode: */ + par->GeneralLockReg = 0x00; + /* We need to program the VCLK for external display only mode. */ + par->ProgramVCLK = 1; + break; + case 0x02: /* Internal LCD only mode: */ + case 0x03: /* Simultaneous internal/external (LCD/CRT) mode: */ + par->GeneralLockReg = 0x01; + /* Don't program the VCLK when using the LCD. */ + par->ProgramVCLK = 0; + break; + } + + /* + * If the screen is to be stretched, turn on stretching for the + * various modes. + * + * OPTION_LCD_STRETCH means stretching should be turned off! + */ + par->PanelDispCntlReg2 = 0x00; + par->PanelDispCntlReg3 = 0x00; + + if (par->lcd_stretch && (par->PanelDispCntlReg1 == 0x02) && /* LCD only */ + (info->var.xres != par->NeoPanelWidth)) { + switch (info->var.xres) { + case 320: /* Needs testing. KEM -- 24 May 98 */ + case 400: /* Needs testing. KEM -- 24 May 98 */ + case 640: + case 800: + case 1024: + lcd_stretch = 1; + par->PanelDispCntlReg2 |= 0xC6; + break; + default: + lcd_stretch = 0; + /* No stretching in these modes. */ + } + } else + lcd_stretch = 0; + + /* + * If the screen is to be centerd, turn on the centering for the + * various modes. + */ + par->PanelVertCenterReg1 = 0x00; + par->PanelVertCenterReg2 = 0x00; + par->PanelVertCenterReg3 = 0x00; + par->PanelVertCenterReg4 = 0x00; + par->PanelVertCenterReg5 = 0x00; + par->PanelHorizCenterReg1 = 0x00; + par->PanelHorizCenterReg2 = 0x00; + par->PanelHorizCenterReg3 = 0x00; + par->PanelHorizCenterReg4 = 0x00; + par->PanelHorizCenterReg5 = 0x00; + + + if (par->PanelDispCntlReg1 & 0x02) { + if (info->var.xres == par->NeoPanelWidth) { + /* + * No centering required when the requested display width + * equals the panel width. + */ + } else { + par->PanelDispCntlReg2 |= 0x01; + par->PanelDispCntlReg3 |= 0x10; + + /* Calculate the horizontal and vertical offsets. */ + if (!lcd_stretch) { + hoffset = + ((par->NeoPanelWidth - + info->var.xres) >> 4) - 1; + voffset = + ((par->NeoPanelHeight - + info->var.yres) >> 1) - 2; + } else { + /* Stretched modes cannot be centered. */ + hoffset = 0; + voffset = 0; + } + + switch (info->var.xres) { + case 320: /* Needs testing. KEM -- 24 May 98 */ + par->PanelHorizCenterReg3 = hoffset; + par->PanelVertCenterReg2 = voffset; + break; + case 400: /* Needs testing. KEM -- 24 May 98 */ + par->PanelHorizCenterReg4 = hoffset; + par->PanelVertCenterReg1 = voffset; + break; + case 640: + par->PanelHorizCenterReg1 = hoffset; + par->PanelVertCenterReg3 = voffset; + break; + case 800: + par->PanelHorizCenterReg2 = hoffset; + par->PanelVertCenterReg4 = voffset; + break; + case 1024: + par->PanelHorizCenterReg5 = hoffset; + par->PanelVertCenterReg5 = voffset; + break; + case 1280: + default: + /* No centering in these modes. */ + break; + } + } + } + + par->biosMode = + neoFindMode(info->var.xres, info->var.yres, + info->var.bits_per_pixel); + + /* + * Calculate the VCLK that most closely matches the requested dot + * clock. + */ + neoCalcVCLK(info, par, timings.pixclock); + + /* Since we program the clocks ourselves, always use VCLK3. */ + par->MiscOutReg |= 0x0C; + + /* alread unlocked above */ + /* BOGUS vga_wgfx(NULL, 0x09, 0x26); */ + + /* don't know what this is, but it's 0 from bootup anyway */ + vga_wgfx(NULL, 0x15, 0x00); + + /* was set to 0x01 by my bios in text and vesa modes */ + vga_wgfx(NULL, 0x0A, par->GeneralLockReg); + + /* + * The color mode needs to be set before calling vgaHWRestore + * to ensure the DAC is initialized properly. + * + * NOTE: Make sure we don't change bits make sure we don't change + * any reserved bits. + */ + temp = vga_rgfx(NULL, 0x90); + switch (info->fix.accel) { + case FB_ACCEL_NEOMAGIC_NM2070: + temp &= 0xF0; /* Save bits 7:4 */ + temp |= (par->ExtColorModeSelect & ~0xF0); + break; + case FB_ACCEL_NEOMAGIC_NM2090: + case FB_ACCEL_NEOMAGIC_NM2093: + case FB_ACCEL_NEOMAGIC_NM2097: + case FB_ACCEL_NEOMAGIC_NM2160: + case FB_ACCEL_NEOMAGIC_NM2200: + case FB_ACCEL_NEOMAGIC_NM2230: + case FB_ACCEL_NEOMAGIC_NM2360: + case FB_ACCEL_NEOMAGIC_NM2380: + temp &= 0x70; /* Save bits 6:4 */ + temp |= (par->ExtColorModeSelect & ~0x70); + break; + } + + vga_wgfx(NULL, 0x90, temp); + + /* + * In some rare cases a lockup might occur if we don't delay + * here. (Reported by Miles Lane) + */ + //mdelay(200); + + /* + * Disable horizontal and vertical graphics and text expansions so + * that vgaHWRestore works properly. + */ + temp = vga_rgfx(NULL, 0x25); + temp &= 0x39; + vga_wgfx(NULL, 0x25, temp); + + /* + * Sleep for 200ms to make sure that the two operations above have + * had time to take effect. + */ + mdelay(200); + + /* + * This function handles restoring the generic VGA registers. */ + vgaHWRestore(info, par); + + /* linear colormap for non palettized modes */ + switch (info->var.bits_per_pixel) { + case 8: + /* PseudoColor, 256 */ + info->fix.visual = FB_VISUAL_PSEUDOCOLOR; + break; + case 16: + /* TrueColor, 64k */ + info->fix.visual = FB_VISUAL_TRUECOLOR; + + for (i = 0; i < 64; i++) { + outb(i, 0x3c8); + + outb(i << 1, 0x3c9); + outb(i, 0x3c9); + outb(i << 1, 0x3c9); + } + break; + case 24: +#ifdef NO_32BIT_SUPPORT_YET + case 32: +#endif + /* TrueColor, 16m */ + info->fix.visual = FB_VISUAL_TRUECOLOR; + + for (i = 0; i < 256; i++) { + outb(i, 0x3c8); + + outb(i, 0x3c9); + outb(i, 0x3c9); + outb(i, 0x3c9); + } + break; + } + + vga_wgfx(NULL, 0x0E, par->ExtCRTDispAddr); + vga_wgfx(NULL, 0x0F, par->ExtCRTOffset); + temp = vga_rgfx(NULL, 0x10); + temp &= 0x0F; /* Save bits 3:0 */ + temp |= (par->SysIfaceCntl1 & ~0x0F); /* VESA Bios sets bit 1! */ + vga_wgfx(NULL, 0x10, temp); + + vga_wgfx(NULL, 0x11, par->SysIfaceCntl2); + vga_wgfx(NULL, 0x15, 0 /*par->SingleAddrPage */ ); + vga_wgfx(NULL, 0x16, 0 /*par->DualAddrPage */ ); + + temp = vga_rgfx(NULL, 0x20); + switch (info->fix.accel) { + case FB_ACCEL_NEOMAGIC_NM2070: + temp &= 0xFC; /* Save bits 7:2 */ + temp |= (par->PanelDispCntlReg1 & ~0xFC); + break; + case FB_ACCEL_NEOMAGIC_NM2090: + case FB_ACCEL_NEOMAGIC_NM2093: + case FB_ACCEL_NEOMAGIC_NM2097: + case FB_ACCEL_NEOMAGIC_NM2160: + temp &= 0xDC; /* Save bits 7:6,4:2 */ + temp |= (par->PanelDispCntlReg1 & ~0xDC); + break; + case FB_ACCEL_NEOMAGIC_NM2200: + case FB_ACCEL_NEOMAGIC_NM2230: + case FB_ACCEL_NEOMAGIC_NM2360: + case FB_ACCEL_NEOMAGIC_NM2380: + temp &= 0x98; /* Save bits 7,4:3 */ + temp |= (par->PanelDispCntlReg1 & ~0x98); + break; + } + vga_wgfx(NULL, 0x20, temp); + + temp = vga_rgfx(NULL, 0x25); + temp &= 0x38; /* Save bits 5:3 */ + temp |= (par->PanelDispCntlReg2 & ~0x38); + vga_wgfx(NULL, 0x25, temp); + + if (info->fix.accel != FB_ACCEL_NEOMAGIC_NM2070) { + temp = vga_rgfx(NULL, 0x30); + temp &= 0xEF; /* Save bits 7:5 and bits 3:0 */ + temp |= (par->PanelDispCntlReg3 & ~0xEF); + vga_wgfx(NULL, 0x30, temp); + } + + vga_wgfx(NULL, 0x28, par->PanelVertCenterReg1); + vga_wgfx(NULL, 0x29, par->PanelVertCenterReg2); + vga_wgfx(NULL, 0x2a, par->PanelVertCenterReg3); + + if (info->fix.accel != FB_ACCEL_NEOMAGIC_NM2070) { + vga_wgfx(NULL, 0x32, par->PanelVertCenterReg4); + vga_wgfx(NULL, 0x33, par->PanelHorizCenterReg1); + vga_wgfx(NULL, 0x34, par->PanelHorizCenterReg2); + vga_wgfx(NULL, 0x35, par->PanelHorizCenterReg3); + } + + if (info->fix.accel == FB_ACCEL_NEOMAGIC_NM2160) + vga_wgfx(NULL, 0x36, par->PanelHorizCenterReg4); + + if (info->fix.accel == FB_ACCEL_NEOMAGIC_NM2200 || + info->fix.accel == FB_ACCEL_NEOMAGIC_NM2230 || + info->fix.accel == FB_ACCEL_NEOMAGIC_NM2360 || + info->fix.accel == FB_ACCEL_NEOMAGIC_NM2380) { + vga_wgfx(NULL, 0x36, par->PanelHorizCenterReg4); + vga_wgfx(NULL, 0x37, par->PanelVertCenterReg5); + vga_wgfx(NULL, 0x38, par->PanelHorizCenterReg5); + + clock_hi = 1; + } + + /* Program VCLK3 if needed. */ + if (par->ProgramVCLK && ((vga_rgfx(NULL, 0x9B) != par->VCLK3NumeratorLow) + || (vga_rgfx(NULL, 0x9F) != par->VCLK3Denominator) + || (clock_hi && ((vga_rgfx(NULL, 0x8F) & ~0x0f) + != (par->VCLK3NumeratorHigh & + ~0x0F))))) { + vga_wgfx(NULL, 0x9B, par->VCLK3NumeratorLow); + if (clock_hi) { + temp = vga_rgfx(NULL, 0x8F); + temp &= 0x0F; /* Save bits 3:0 */ + temp |= (par->VCLK3NumeratorHigh & ~0x0F); + vga_wgfx(NULL, 0x8F, te |