2 * linux/drivers/video/riva/fbdev.c - nVidia RIVA 128/TNT/TNT2 fb driver
4 * Maintained by Ani Joshi <ajoshi@shell.unixbox.com>
6 * Copyright 1999-2000 Jeff Garzik
10 * Ani Joshi: Lots of debugging and cleanup work, really helped
11 * get the driver going
13 * Ferenc Bakonyi: Bug fixes, cleanup, modularization
15 * Jindrich Makovicka: Accel code help, hw cursor, mtrr
17 * Paul Richards: Bug fixes, updates
19 * Initial template from skeletonfb.c, created 28 Dec 1997 by Geert Uytterhoeven
20 * Includes riva_hw.c from nVidia, see copyright below.
21 * KGI code provided the basis for state storage, init, and mode switching.
23 * This file is subject to the terms and conditions of the GNU General Public
24 * License. See the file COPYING in the main directory of this archive
27 * Known bugs and issues:
28 * restoring text mode fails
29 * doublescan modes are broken
32 #include <linux/module.h>
33 #include <linux/kernel.h>
34 #include <linux/errno.h>
35 #include <linux/string.h>
37 #include <linux/slab.h>
38 #include <linux/delay.h>
40 #include <linux/init.h>
41 #include <linux/pci.h>
42 #include <linux/backlight.h>
43 #include <linux/bitrev.h>
49 #include <asm/pci-bridge.h>
51 #ifdef CONFIG_PMAC_BACKLIGHT
52 #include <asm/machdep.h>
53 #include <asm/backlight.h>
59 /* version number of this driver */
60 #define RIVAFB_VERSION "0.9.5b"
62 /* ------------------------------------------------------------------------- *
64 * various helpful macros and constants
66 * ------------------------------------------------------------------------- */
67 #ifdef CONFIG_FB_RIVA_DEBUG
68 #define NVTRACE printk
70 #define NVTRACE if(0) printk
73 #define NVTRACE_ENTER(...) NVTRACE("%s START\n", __func__)
74 #define NVTRACE_LEAVE(...) NVTRACE("%s END\n", __func__)
76 #ifdef CONFIG_FB_RIVA_DEBUG
77 #define assert(expr) \
79 printk( "Assertion failed! %s,%s,%s,line=%d\n",\
80 #expr,__FILE__,__func__,__LINE__); \
87 #define PFX "rivafb: "
89 /* macro that allows you to set overflow bits */
90 #define SetBitField(value,from,to) SetBF(to,GetBF(value,from))
91 #define SetBit(n) (1<<(n))
92 #define Set8Bits(value) ((value)&0xff)
94 /* HW cursor parameters */
97 /* ------------------------------------------------------------------------- *
101 * ------------------------------------------------------------------------- */
103 static int rivafb_blank(int blank, struct fb_info *info);
105 /* ------------------------------------------------------------------------- *
107 * card identification
109 * ------------------------------------------------------------------------- */
111 static struct pci_device_id rivafb_pci_tbl[] = {
112 { PCI_VENDOR_ID_NVIDIA_SGS, PCI_DEVICE_ID_NVIDIA_SGS_RIVA128,
113 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
114 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_TNT,
115 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
116 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_TNT2,
117 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
118 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_UTNT2,
119 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
120 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_VTNT2,
121 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
122 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_UVTNT2,
123 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
124 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_ITNT2,
125 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
126 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE_SDR,
127 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
128 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE_DDR,
129 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
130 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO,
131 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
132 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE2_MX,
133 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
134 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE2_MX2,
135 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
136 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE2_GO,
137 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
138 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO2_MXR,
139 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
140 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE2_GTS,
141 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
142 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE2_GTS2,
143 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
144 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE2_ULTRA,
145 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
146 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO2_PRO,
147 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
148 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_MX_460,
149 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
150 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_MX_440,
151 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
152 // NF2/IGP version, GeForce 4 MX, NV18
153 { PCI_VENDOR_ID_NVIDIA, 0x01f0,
154 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
155 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_MX_420,
156 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
157 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_440_GO,
158 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
159 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_420_GO,
160 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
161 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_420_GO_M32,
162 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
163 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO4_500XGL,
164 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
165 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_440_GO_M64,
166 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
167 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO4_200,
168 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
169 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO4_550XGL,
170 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
171 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO4_500_GOGL,
172 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
173 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_IGEFORCE2,
174 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
175 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE3,
176 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
177 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE3_1,
178 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
179 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE3_2,
180 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
181 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO_DDC,
182 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
183 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_TI_4600,
184 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
185 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_TI_4400,
186 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
187 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_TI_4200,
188 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
189 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO4_900XGL,
190 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
191 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO4_750XGL,
192 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
193 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO4_700XGL,
194 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
195 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE_FX_GO_5200,
196 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
197 { 0, } /* terminate list */
199 MODULE_DEVICE_TABLE(pci, rivafb_pci_tbl);
201 /* ------------------------------------------------------------------------- *
205 * ------------------------------------------------------------------------- */
207 /* command line data, set in rivafb_setup() */
208 static int flatpanel __devinitdata = -1; /* Autodetect later */
209 static int forceCRTC __devinitdata = -1;
210 static int noaccel __devinitdata = 0;
212 static int nomtrr __devinitdata = 0;
214 #ifdef CONFIG_PMAC_BACKLIGHT
215 static int backlight __devinitdata = 1;
217 static int backlight __devinitdata = 0;
220 static char *mode_option __devinitdata = NULL;
221 static int strictmode = 0;
223 static struct fb_fix_screeninfo __devinitdata rivafb_fix = {
224 .type = FB_TYPE_PACKED_PIXELS,
229 static struct fb_var_screeninfo __devinitdata rivafb_default_var = {
239 .activate = FB_ACTIVATE_NOW,
249 .vmode = FB_VMODE_NONINTERLACED
253 static const struct riva_regs reg_template = {
254 {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* ATTR */
255 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
256 0x41, 0x01, 0x0F, 0x00, 0x00},
257 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* CRT */
258 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
259 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE3, /* 0x10 */
260 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
261 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x20 */
262 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
263 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x30 */
264 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
267 {0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x05, 0x0F, /* GRA */
269 {0x03, 0x01, 0x0F, 0x00, 0x0E}, /* SEQ */
276 #ifdef CONFIG_FB_RIVA_BACKLIGHT
277 /* We do not have any information about which values are allowed, thus
278 * we used safe values.
280 #define MIN_LEVEL 0x158
281 #define MAX_LEVEL 0x534
282 #define LEVEL_STEP ((MAX_LEVEL - MIN_LEVEL) / FB_BACKLIGHT_MAX)
284 static int riva_bl_get_level_brightness(struct riva_par *par,
287 struct fb_info *info = pci_get_drvdata(par->pdev);
290 /* Get and convert the value */
291 /* No locking on bl_curve since accessing a single value */
292 nlevel = MIN_LEVEL + info->bl_curve[level] * LEVEL_STEP;
296 else if (nlevel < MIN_LEVEL)
298 else if (nlevel > MAX_LEVEL)
304 static int riva_bl_update_status(struct backlight_device *bd)
306 struct riva_par *par = bl_get_data(bd);
307 U032 tmp_pcrt, tmp_pmc;
310 if (bd->props.power != FB_BLANK_UNBLANK ||
311 bd->props.fb_blank != FB_BLANK_UNBLANK)
314 level = bd->props.brightness;
316 tmp_pmc = NV_RD32(par->riva.PMC, 0x10F0) & 0x0000FFFF;
317 tmp_pcrt = NV_RD32(par->riva.PCRTC0, 0x081C) & 0xFFFFFFFC;
320 tmp_pmc |= (1 << 31); /* backlight bit */
321 tmp_pmc |= riva_bl_get_level_brightness(par, level) << 16; /* level */
323 NV_WR32(par->riva.PCRTC0, 0x081C, tmp_pcrt);
324 NV_WR32(par->riva.PMC, 0x10F0, tmp_pmc);
329 static int riva_bl_get_brightness(struct backlight_device *bd)
331 return bd->props.brightness;
334 static struct backlight_ops riva_bl_ops = {
335 .get_brightness = riva_bl_get_brightness,
336 .update_status = riva_bl_update_status,
339 static void riva_bl_init(struct riva_par *par)
341 struct fb_info *info = pci_get_drvdata(par->pdev);
342 struct backlight_device *bd;
348 #ifdef CONFIG_PMAC_BACKLIGHT
349 if (!machine_is(powermac) ||
350 !pmac_has_backlight_type("mnca"))
354 snprintf(name, sizeof(name), "rivabl%d", info->node);
356 bd = backlight_device_register(name, info->dev, par, &riva_bl_ops);
359 printk(KERN_WARNING "riva: Backlight registration failed\n");
364 fb_bl_default_curve(info, 0,
365 MIN_LEVEL * FB_BACKLIGHT_MAX / MAX_LEVEL,
368 bd->props.max_brightness = FB_BACKLIGHT_LEVELS - 1;
369 bd->props.brightness = bd->props.max_brightness;
370 bd->props.power = FB_BLANK_UNBLANK;
371 backlight_update_status(bd);
373 printk("riva: Backlight initialized (%s)\n", name);
381 static void riva_bl_exit(struct fb_info *info)
383 struct backlight_device *bd = info->bl_dev;
385 backlight_device_unregister(bd);
386 printk("riva: Backlight unloaded\n");
389 static inline void riva_bl_init(struct riva_par *par) {}
390 static inline void riva_bl_exit(struct fb_info *info) {}
391 #endif /* CONFIG_FB_RIVA_BACKLIGHT */
393 /* ------------------------------------------------------------------------- *
397 * ------------------------------------------------------------------------- */
399 static inline void CRTCout(struct riva_par *par, unsigned char index,
402 VGA_WR08(par->riva.PCIO, 0x3d4, index);
403 VGA_WR08(par->riva.PCIO, 0x3d5, val);
406 static inline unsigned char CRTCin(struct riva_par *par,
409 VGA_WR08(par->riva.PCIO, 0x3d4, index);
410 return (VGA_RD08(par->riva.PCIO, 0x3d5));
413 static inline void GRAout(struct riva_par *par, unsigned char index,
416 VGA_WR08(par->riva.PVIO, 0x3ce, index);
417 VGA_WR08(par->riva.PVIO, 0x3cf, val);
420 static inline unsigned char GRAin(struct riva_par *par,
423 VGA_WR08(par->riva.PVIO, 0x3ce, index);
424 return (VGA_RD08(par->riva.PVIO, 0x3cf));
427 static inline void SEQout(struct riva_par *par, unsigned char index,
430 VGA_WR08(par->riva.PVIO, 0x3c4, index);
431 VGA_WR08(par->riva.PVIO, 0x3c5, val);
434 static inline unsigned char SEQin(struct riva_par *par,
437 VGA_WR08(par->riva.PVIO, 0x3c4, index);
438 return (VGA_RD08(par->riva.PVIO, 0x3c5));
441 static inline void ATTRout(struct riva_par *par, unsigned char index,
444 VGA_WR08(par->riva.PCIO, 0x3c0, index);
445 VGA_WR08(par->riva.PCIO, 0x3c0, val);
448 static inline unsigned char ATTRin(struct riva_par *par,
451 VGA_WR08(par->riva.PCIO, 0x3c0, index);
452 return (VGA_RD08(par->riva.PCIO, 0x3c1));
455 static inline void MISCout(struct riva_par *par, unsigned char val)
457 VGA_WR08(par->riva.PVIO, 0x3c2, val);
460 static inline unsigned char MISCin(struct riva_par *par)
462 return (VGA_RD08(par->riva.PVIO, 0x3cc));
465 static inline void reverse_order(u32 *l)
468 a[0] = bitrev8(a[0]);
469 a[1] = bitrev8(a[1]);
470 a[2] = bitrev8(a[2]);
471 a[3] = bitrev8(a[3]);
474 /* ------------------------------------------------------------------------- *
478 * ------------------------------------------------------------------------- */
481 * rivafb_load_cursor_image - load cursor image to hardware
482 * @data: address to monochrome bitmap (1 = foreground color, 0 = background)
483 * @par: pointer to private data
484 * @w: width of cursor image in pixels
485 * @h: height of cursor image in scanlines
486 * @bg: background color (ARGB1555) - alpha bit determines opacity
487 * @fg: foreground color (ARGB1555)
490 * Loads cursor image based on a monochrome source and mask bitmap. The
491 * image bits determines the color of the pixel, 0 for background, 1 for
492 * foreground. Only the affected region (as determined by @w and @h
493 * parameters) will be updated.
498 static void rivafb_load_cursor_image(struct riva_par *par, u8 *data8,
499 u16 bg, u16 fg, u32 w, u32 h)
503 u32 *data = (u32 *)data8;
504 bg = le16_to_cpu(bg);
505 fg = le16_to_cpu(fg);
509 for (i = 0; i < h; i++) {
513 for (j = 0; j < w/2; j++) {
515 #if defined (__BIG_ENDIAN)
516 tmp = (b & (1 << 31)) ? fg << 16 : bg << 16;
518 tmp |= (b & (1 << 31)) ? fg : bg;
521 tmp = (b & 1) ? fg : bg;
523 tmp |= (b & 1) ? fg << 16 : bg << 16;
526 writel(tmp, &par->riva.CURSOR[k++]);
528 k += (MAX_CURS - w)/2;
532 /* ------------------------------------------------------------------------- *
534 * general utility functions
536 * ------------------------------------------------------------------------- */
539 * riva_wclut - set CLUT entry
540 * @chip: pointer to RIVA_HW_INST object
541 * @regnum: register number
542 * @red: red component
543 * @green: green component
544 * @blue: blue component
547 * Sets color register @regnum.
552 static void riva_wclut(RIVA_HW_INST *chip,
553 unsigned char regnum, unsigned char red,
554 unsigned char green, unsigned char blue)
556 VGA_WR08(chip->PDIO, 0x3c8, regnum);
557 VGA_WR08(chip->PDIO, 0x3c9, red);
558 VGA_WR08(chip->PDIO, 0x3c9, green);
559 VGA_WR08(chip->PDIO, 0x3c9, blue);
563 * riva_rclut - read fromCLUT register
564 * @chip: pointer to RIVA_HW_INST object
565 * @regnum: register number
566 * @red: red component
567 * @green: green component
568 * @blue: blue component
571 * Reads red, green, and blue from color register @regnum.
576 static void riva_rclut(RIVA_HW_INST *chip,
577 unsigned char regnum, unsigned char *red,
578 unsigned char *green, unsigned char *blue)
581 VGA_WR08(chip->PDIO, 0x3c7, regnum);
582 *red = VGA_RD08(chip->PDIO, 0x3c9);
583 *green = VGA_RD08(chip->PDIO, 0x3c9);
584 *blue = VGA_RD08(chip->PDIO, 0x3c9);
588 * riva_save_state - saves current chip state
589 * @par: pointer to riva_par object containing info for current riva board
590 * @regs: pointer to riva_regs object
593 * Saves current chip state to @regs.
599 static void riva_save_state(struct riva_par *par, struct riva_regs *regs)
604 par->riva.LockUnlock(&par->riva, 0);
606 par->riva.UnloadStateExt(&par->riva, ®s->ext);
608 regs->misc_output = MISCin(par);
610 for (i = 0; i < NUM_CRT_REGS; i++)
611 regs->crtc[i] = CRTCin(par, i);
613 for (i = 0; i < NUM_ATC_REGS; i++)
614 regs->attr[i] = ATTRin(par, i);
616 for (i = 0; i < NUM_GRC_REGS; i++)
617 regs->gra[i] = GRAin(par, i);
619 for (i = 0; i < NUM_SEQ_REGS; i++)
620 regs->seq[i] = SEQin(par, i);
625 * riva_load_state - loads current chip state
626 * @par: pointer to riva_par object containing info for current riva board
627 * @regs: pointer to riva_regs object
630 * Loads chip state from @regs.
633 * riva_load_video_mode()
638 static void riva_load_state(struct riva_par *par, struct riva_regs *regs)
640 RIVA_HW_STATE *state = ®s->ext;
644 CRTCout(par, 0x11, 0x00);
646 par->riva.LockUnlock(&par->riva, 0);
648 par->riva.LoadStateExt(&par->riva, state);
650 MISCout(par, regs->misc_output);
652 for (i = 0; i < NUM_CRT_REGS; i++) {
658 CRTCout(par, i, regs->crtc[i]);
662 for (i = 0; i < NUM_ATC_REGS; i++)
663 ATTRout(par, i, regs->attr[i]);
665 for (i = 0; i < NUM_GRC_REGS; i++)
666 GRAout(par, i, regs->gra[i]);
668 for (i = 0; i < NUM_SEQ_REGS; i++)
669 SEQout(par, i, regs->seq[i]);
674 * riva_load_video_mode - calculate timings
675 * @info: pointer to fb_info object containing info for current riva board
678 * Calculate some timings and then send em off to riva_load_state().
683 static int riva_load_video_mode(struct fb_info *info)
685 int bpp, width, hDisplaySize, hDisplay, hStart,
686 hEnd, hTotal, height, vDisplay, vStart, vEnd, vTotal, dotClock;
687 int hBlankStart, hBlankEnd, vBlankStart, vBlankEnd;
689 struct riva_par *par = info->par;
690 struct riva_regs newmode;
693 /* time to calculate */
694 rivafb_blank(FB_BLANK_NORMAL, info);
696 bpp = info->var.bits_per_pixel;
697 if (bpp == 16 && info->var.green.length == 5)
699 width = info->var.xres_virtual;
700 hDisplaySize = info->var.xres;
701 hDisplay = (hDisplaySize / 8) - 1;
702 hStart = (hDisplaySize + info->var.right_margin) / 8 - 1;
703 hEnd = (hDisplaySize + info->var.right_margin +
704 info->var.hsync_len) / 8 - 1;
705 hTotal = (hDisplaySize + info->var.right_margin +
706 info->var.hsync_len + info->var.left_margin) / 8 - 5;
707 hBlankStart = hDisplay;
708 hBlankEnd = hTotal + 4;
710 height = info->var.yres_virtual;
711 vDisplay = info->var.yres - 1;
712 vStart = info->var.yres + info->var.lower_margin - 1;
713 vEnd = info->var.yres + info->var.lower_margin +
714 info->var.vsync_len - 1;
715 vTotal = info->var.yres + info->var.lower_margin +
716 info->var.vsync_len + info->var.upper_margin + 2;
717 vBlankStart = vDisplay;
718 vBlankEnd = vTotal + 1;
719 dotClock = 1000000000 / info->var.pixclock;
721 memcpy(&newmode, ®_template, sizeof(struct riva_regs));
723 if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED)
726 if (par->FlatPanel) {
729 vBlankStart = vStart;
732 hBlankEnd = hTotal + 4;
735 newmode.crtc[0x0] = Set8Bits (hTotal);
736 newmode.crtc[0x1] = Set8Bits (hDisplay);
737 newmode.crtc[0x2] = Set8Bits (hBlankStart);
738 newmode.crtc[0x3] = SetBitField (hBlankEnd, 4: 0, 4:0) | SetBit (7);
739 newmode.crtc[0x4] = Set8Bits (hStart);
740 newmode.crtc[0x5] = SetBitField (hBlankEnd, 5: 5, 7:7)
741 | SetBitField (hEnd, 4: 0, 4:0);
742 newmode.crtc[0x6] = SetBitField (vTotal, 7: 0, 7:0);
743 newmode.crtc[0x7] = SetBitField (vTotal, 8: 8, 0:0)
744 | SetBitField (vDisplay, 8: 8, 1:1)
745 | SetBitField (vStart, 8: 8, 2:2)
746 | SetBitField (vBlankStart, 8: 8, 3:3)
748 | SetBitField (vTotal, 9: 9, 5:5)
749 | SetBitField (vDisplay, 9: 9, 6:6)
750 | SetBitField (vStart, 9: 9, 7:7);
751 newmode.crtc[0x9] = SetBitField (vBlankStart, 9: 9, 5:5)
753 newmode.crtc[0x10] = Set8Bits (vStart);
754 newmode.crtc[0x11] = SetBitField (vEnd, 3: 0, 3:0)
756 newmode.crtc[0x12] = Set8Bits (vDisplay);
757 newmode.crtc[0x13] = (width / 8) * ((bpp + 1) / 8);
758 newmode.crtc[0x15] = Set8Bits (vBlankStart);
759 newmode.crtc[0x16] = Set8Bits (vBlankEnd);
761 newmode.ext.screen = SetBitField(hBlankEnd,6:6,4:4)
762 | SetBitField(vBlankStart,10:10,3:3)
763 | SetBitField(vStart,10:10,2:2)
764 | SetBitField(vDisplay,10:10,1:1)
765 | SetBitField(vTotal,10:10,0:0);
766 newmode.ext.horiz = SetBitField(hTotal,8:8,0:0)
767 | SetBitField(hDisplay,8:8,1:1)
768 | SetBitField(hBlankStart,8:8,2:2)
769 | SetBitField(hStart,8:8,3:3);
770 newmode.ext.extra = SetBitField(vTotal,11:11,0:0)
771 | SetBitField(vDisplay,11:11,2:2)
772 | SetBitField(vStart,11:11,4:4)
773 | SetBitField(vBlankStart,11:11,6:6);
775 if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) {
776 int tmp = (hTotal >> 1) & ~1;
777 newmode.ext.interlace = Set8Bits(tmp);
778 newmode.ext.horiz |= SetBitField(tmp, 8:8,4:4);
780 newmode.ext.interlace = 0xff; /* interlace off */
782 if (par->riva.Architecture >= NV_ARCH_10)
783 par->riva.CURSOR = (U032 __iomem *)(info->screen_base + par->riva.CursorStart);
785 if (info->var.sync & FB_SYNC_HOR_HIGH_ACT)
786 newmode.misc_output &= ~0x40;
788 newmode.misc_output |= 0x40;
789 if (info->var.sync & FB_SYNC_VERT_HIGH_ACT)
790 newmode.misc_output &= ~0x80;
792 newmode.misc_output |= 0x80;
794 rc = CalcStateExt(&par->riva, &newmode.ext, bpp, width,
795 hDisplaySize, height, dotClock);
799 newmode.ext.scale = NV_RD32(par->riva.PRAMDAC, 0x00000848) &
801 if (par->FlatPanel == 1) {
802 newmode.ext.pixel |= (1 << 7);
803 newmode.ext.scale |= (1 << 8);
805 if (par->SecondCRTC) {
806 newmode.ext.head = NV_RD32(par->riva.PCRTC0, 0x00000860) &
808 newmode.ext.head2 = NV_RD32(par->riva.PCRTC0, 0x00002860) |
810 newmode.ext.crtcOwner = 3;
811 newmode.ext.pllsel |= 0x20000800;
812 newmode.ext.vpll2 = newmode.ext.vpll;
813 } else if (par->riva.twoHeads) {
814 newmode.ext.head = NV_RD32(par->riva.PCRTC0, 0x00000860) |
816 newmode.ext.head2 = NV_RD32(par->riva.PCRTC0, 0x00002860) &
818 newmode.ext.crtcOwner = 0;
819 newmode.ext.vpll2 = NV_RD32(par->riva.PRAMDAC0, 0x00000520);
821 if (par->FlatPanel == 1) {
822 newmode.ext.pixel |= (1 << 7);
823 newmode.ext.scale |= (1 << 8);
825 newmode.ext.cursorConfig = 0x02000100;
826 par->current_state = newmode;
827 riva_load_state(par, &par->current_state);
828 par->riva.LockUnlock(&par->riva, 0); /* important for HW cursor */
831 rivafb_blank(FB_BLANK_UNBLANK, info);
837 static void riva_update_var(struct fb_var_screeninfo *var,
838 const struct fb_videomode *modedb)
841 var->xres = var->xres_virtual = modedb->xres;
842 var->yres = modedb->yres;
843 if (var->yres_virtual < var->yres)
844 var->yres_virtual = var->yres;
845 var->xoffset = var->yoffset = 0;
846 var->pixclock = modedb->pixclock;
847 var->left_margin = modedb->left_margin;
848 var->right_margin = modedb->right_margin;
849 var->upper_margin = modedb->upper_margin;
850 var->lower_margin = modedb->lower_margin;
851 var->hsync_len = modedb->hsync_len;
852 var->vsync_len = modedb->vsync_len;
853 var->sync = modedb->sync;
854 var->vmode = modedb->vmode;
859 * rivafb_do_maximize -
860 * @info: pointer to fb_info object containing info for current riva board
869 * -EINVAL on failure, 0 on success
875 static int rivafb_do_maximize(struct fb_info *info,
876 struct fb_var_screeninfo *var,
892 /* use highest possible virtual resolution */
893 if (var->xres_virtual == -1 && var->yres_virtual == -1) {
894 printk(KERN_WARNING PFX
895 "using maximum available virtual resolution\n");
896 for (i = 0; modes[i].xres != -1; i++) {
897 if (modes[i].xres * nom / den * modes[i].yres <
901 if (modes[i].xres == -1) {
903 "could not find a virtual resolution that fits into video memory!!\n");
904 NVTRACE("EXIT - EINVAL error\n");
907 var->xres_virtual = modes[i].xres;
908 var->yres_virtual = modes[i].yres;
911 "virtual resolution set to maximum of %dx%d\n",
912 var->xres_virtual, var->yres_virtual);
913 } else if (var->xres_virtual == -1) {
914 var->xres_virtual = (info->fix.smem_len * den /
915 (nom * var->yres_virtual)) & ~15;
916 printk(KERN_WARNING PFX
917 "setting virtual X resolution to %d\n", var->xres_virtual);
918 } else if (var->yres_virtual == -1) {
919 var->xres_virtual = (var->xres_virtual + 15) & ~15;
920 var->yres_virtual = info->fix.smem_len * den /
921 (nom * var->xres_virtual);
922 printk(KERN_WARNING PFX
923 "setting virtual Y resolution to %d\n", var->yres_virtual);
925 var->xres_virtual = (var->xres_virtual + 15) & ~15;
926 if (var->xres_virtual * nom / den * var->yres_virtual > info->fix.smem_len) {
928 "mode %dx%dx%d rejected...resolution too high to fit into video memory!\n",
929 var->xres, var->yres, var->bits_per_pixel);
930 NVTRACE("EXIT - EINVAL error\n");
935 if (var->xres_virtual * nom / den >= 8192) {
936 printk(KERN_WARNING PFX
937 "virtual X resolution (%d) is too high, lowering to %d\n",
938 var->xres_virtual, 8192 * den / nom - 16);
939 var->xres_virtual = 8192 * den / nom - 16;
942 if (var->xres_virtual < var->xres) {
944 "virtual X resolution (%d) is smaller than real\n", var->xres_virtual);
948 if (var->yres_virtual < var->yres) {
950 "virtual Y resolution (%d) is smaller than real\n", var->yres_virtual);
953 if (var->yres_virtual > 0x7fff/nom)
954 var->yres_virtual = 0x7fff/nom;
955 if (var->xres_virtual > 0x7fff/nom)
956 var->xres_virtual = 0x7fff/nom;
962 riva_set_pattern(struct riva_par *par, int clr0, int clr1, int pat0, int pat1)
964 RIVA_FIFO_FREE(par->riva, Patt, 4);
965 NV_WR32(&par->riva.Patt->Color0, 0, clr0);
966 NV_WR32(&par->riva.Patt->Color1, 0, clr1);
967 NV_WR32(par->riva.Patt->Monochrome, 0, pat0);
968 NV_WR32(par->riva.Patt->Monochrome, 4, pat1);
971 /* acceleration routines */
972 static inline void wait_for_idle(struct riva_par *par)
974 while (par->riva.Busy(&par->riva));
978 * Set ROP. Translate X rop into ROP3. Internal routine.
981 riva_set_rop_solid(struct riva_par *par, int rop)
983 riva_set_pattern(par, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
984 RIVA_FIFO_FREE(par->riva, Rop, 1);
985 NV_WR32(&par->riva.Rop->Rop3, 0, rop);
989 static void riva_setup_accel(struct fb_info *info)
991 struct riva_par *par = info->par;
993 RIVA_FIFO_FREE(par->riva, Clip, 2);
994 NV_WR32(&par->riva.Clip->TopLeft, 0, 0x0);
995 NV_WR32(&par->riva.Clip->WidthHeight, 0,
996 (info->var.xres_virtual & 0xffff) |
997 (info->var.yres_virtual << 16));
998 riva_set_rop_solid(par, 0xcc);
1003 * riva_get_cmap_len - query current color map length
1004 * @var: standard kernel fb changeable data
1007 * Get current color map length.
1010 * Length of color map
1013 * rivafb_setcolreg()
1015 static int riva_get_cmap_len(const struct fb_var_screeninfo *var)
1017 int rc = 256; /* reasonable default */
1019 switch (var->green.length) {
1021 rc = 256; /* 256 entries (2^8), 8 bpp and RGB8888 */
1024 rc = 32; /* 32 entries (2^5), 16 bpp, RGB555 */
1027 rc = 64; /* 64 entries (2^6), 16 bpp, RGB565 */
1030 /* should not occur */
1036 /* ------------------------------------------------------------------------- *
1038 * framebuffer operations
1040 * ------------------------------------------------------------------------- */
1042 static int rivafb_open(struct fb_info *info, int user)
1044 struct riva_par *par = info->par;
1047 mutex_lock(&par->open_lock);
1048 if (!par->ref_count) {
1050 memset(&par->state, 0, sizeof(struct vgastate));
1051 par->state.flags = VGA_SAVE_MODE | VGA_SAVE_FONTS;
1052 /* save the DAC for Riva128 */
1053 if (par->riva.Architecture == NV_ARCH_03)
1054 par->state.flags |= VGA_SAVE_CMAP;
1055 save_vga(&par->state);
1057 /* vgaHWunlock() + riva unlock (0x7F) */
1058 CRTCout(par, 0x11, 0xFF);
1059 par->riva.LockUnlock(&par->riva, 0);
1061 riva_save_state(par, &par->initial_state);
1064 mutex_unlock(&par->open_lock);
1069 static int rivafb_release(struct fb_info *info, int user)
1071 struct riva_par *par = info->par;
1074 mutex_lock(&par->open_lock);
1075 if (!par->ref_count) {
1076 mutex_unlock(&par->open_lock);
1079 if (par->ref_count == 1) {
1080 par->riva.LockUnlock(&par->riva, 0);
1081 par->riva.LoadStateExt(&par->riva, &par->initial_state.ext);
1082 riva_load_state(par, &par->initial_state);
1084 restore_vga(&par->state);
1086 par->riva.LockUnlock(&par->riva, 1);
1089 mutex_unlock(&par->open_lock);
1094 static int rivafb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
1096 const struct fb_videomode *mode;
1097 struct riva_par *par = info->par;
1098 int nom, den; /* translating from pixels->bytes */
1102 switch (var->bits_per_pixel) {
1104 var->red.offset = var->green.offset = var->blue.offset = 0;
1105 var->red.length = var->green.length = var->blue.length = 8;
1106 var->bits_per_pixel = 8;
1110 var->green.length = 5;
1113 var->bits_per_pixel = 16;
1114 /* The Riva128 supports RGB555 only */
1115 if (par->riva.Architecture == NV_ARCH_03)
1116 var->green.length = 5;
1117 if (var->green.length == 5) {
1118 /* 0rrrrrgg gggbbbbb */
1119 var->red.offset = 10;
1120 var->green.offset = 5;
1121 var->blue.offset = 0;
1122 var->red.length = 5;
1123 var->green.length = 5;
1124 var->blue.length = 5;
1126 /* rrrrrggg gggbbbbb */
1127 var->red.offset = 11;
1128 var->green.offset = 5;
1129 var->blue.offset = 0;
1130 var->red.length = 5;
1131 var->green.length = 6;
1132 var->blue.length = 5;
1138 var->red.length = var->green.length = var->blue.length = 8;
1139 var->bits_per_pixel = 32;
1140 var->red.offset = 16;
1141 var->green.offset = 8;
1142 var->blue.offset = 0;
1148 "mode %dx%dx%d rejected...color depth not supported.\n",
1149 var->xres, var->yres, var->bits_per_pixel);
1150 NVTRACE("EXIT, returning -EINVAL\n");
1155 if (!info->monspecs.vfmax || !info->monspecs.hfmax ||
1156 !info->monspecs.dclkmax || !fb_validate_mode(var, info))
1160 /* calculate modeline if supported by monitor */
1161 if (!mode_valid && info->monspecs.gtf) {
1162 if (!fb_get_mode(FB_MAXTIMINGS, 0, var, info))
1167 mode = fb_find_best_mode(var, &info->modelist);
1169 riva_update_var(var, mode);
1174 if (!mode_valid && info->monspecs.modedb_len)
1177 if (var->xres_virtual < var->xres)
1178 var->xres_virtual = var->xres;
1179 if (var->yres_virtual <= var->yres)
1180 var->yres_virtual = -1;
1181 if (rivafb_do_maximize(info, var, nom, den) < 0)
1184 if (var->xoffset < 0)
1186 if (var->yoffset < 0)
1189 /* truncate xoffset and yoffset to maximum if too high */
1190 if (var->xoffset > var->xres_virtual - var->xres)
1191 var->xoffset = var->xres_virtual - var->xres - 1;
1193 if (var->yoffset > var->yres_virtual - var->yres)
1194 var->yoffset = var->yres_virtual - var->yres - 1;
1196 var->red.msb_right =
1197 var->green.msb_right =
1198 var->blue.msb_right =
1199 var->transp.offset = var->transp.length = var->transp.msb_right = 0;
1204 static int rivafb_set_par(struct fb_info *info)
1206 struct riva_par *par = info->par;
1210 /* vgaHWunlock() + riva unlock (0x7F) */
1211 CRTCout(par, 0x11, 0xFF);
1212 par->riva.LockUnlock(&par->riva, 0);
1213 rc = riva_load_video_mode(info);
1216 if(!(info->flags & FBINFO_HWACCEL_DISABLED))
1217 riva_setup_accel(info);
1219 par->cursor_reset = 1;
1220 info->fix.line_length = (info->var.xres_virtual * (info->var.bits_per_pixel >> 3));
1221 info->fix.visual = (info->var.bits_per_pixel == 8) ?
1222 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_DIRECTCOLOR;
1224 if (info->flags & FBINFO_HWACCEL_DISABLED)
1225 info->pixmap.scan_align = 1;
1227 info->pixmap.scan_align = 4;
1235 * rivafb_pan_display
1236 * @var: standard kernel fb changeable data
1238 * @info: pointer to fb_info object containing info for current riva board
1241 * Pan (or wrap, depending on the `vmode' field) the display using the
1242 * `xoffset' and `yoffset' fields of the `var' structure.
1243 * If the values don't fit, return -EINVAL.
1245 * This call looks only at xoffset, yoffset and the FB_VMODE_YWRAP flag
1247 static int rivafb_pan_display(struct fb_var_screeninfo *var,
1248 struct fb_info *info)
1250 struct riva_par *par = info->par;
1254 base = var->yoffset * info->fix.line_length + var->xoffset;
1255 par->riva.SetStartAddress(&par->riva, base);
1260 static int rivafb_blank(int blank, struct fb_info *info)
1262 struct riva_par *par= info->par;
1263 unsigned char tmp, vesa;
1265 tmp = SEQin(par, 0x01) & ~0x20; /* screen on/off */
1266 vesa = CRTCin(par, 0x1a) & ~0xc0; /* sync on/off */
1274 case FB_BLANK_UNBLANK:
1275 case FB_BLANK_NORMAL:
1277 case FB_BLANK_VSYNC_SUSPEND:
1280 case FB_BLANK_HSYNC_SUSPEND:
1283 case FB_BLANK_POWERDOWN:
1288 SEQout(par, 0x01, tmp);
1289 CRTCout(par, 0x1a, vesa);
1298 * @regno: register index
1299 * @red: red component
1300 * @green: green component
1301 * @blue: blue component
1302 * @transp: transparency
1303 * @info: pointer to fb_info object containing info for current riva board
1306 * Set a single color register. The values supplied have a 16 bit
1310 * Return != 0 for invalid regno.
1313 * fbcmap.c:fb_set_cmap()
1315 static int rivafb_setcolreg(unsigned regno, unsigned red, unsigned green,
1316 unsigned blue, unsigned transp,
1317 struct fb_info *info)
1319 struct riva_par *par = info->par;
1320 RIVA_HW_INST *chip = &par->riva;
1323 if (regno >= riva_get_cmap_len(&info->var))
1326 if (info->var.grayscale) {
1327 /* gray = 0.30*R + 0.59*G + 0.11*B */
1328 red = green = blue =
1329 (red * 77 + green * 151 + blue * 28) >> 8;
1332 if (regno < 16 && info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
1333 ((u32 *) info->pseudo_palette)[regno] =
1334 (regno << info->var.red.offset) |
1335 (regno << info->var.green.offset) |
1336 (regno << info->var.blue.offset);
1338 * The Riva128 2D engine requires color information in
1339 * TrueColor format even if framebuffer is in DirectColor
1341 if (par->riva.Architecture == NV_ARCH_03) {
1342 switch (info->var.bits_per_pixel) {
1344 par->palette[regno] = ((red & 0xf800) >> 1) |
1345 ((green & 0xf800) >> 6) |
1346 ((blue & 0xf800) >> 11);
1349 par->palette[regno] = ((red & 0xff00) << 8) |
1350 ((green & 0xff00)) |
1351 ((blue & 0xff00) >> 8);
1357 switch (info->var.bits_per_pixel) {
1359 /* "transparent" stuff is completely ignored. */
1360 riva_wclut(chip, regno, red >> 8, green >> 8, blue >> 8);
1363 if (info->var.green.length == 5) {
1364 for (i = 0; i < 8; i++) {
1365 riva_wclut(chip, regno*8+i, red >> 8,
1366 green >> 8, blue >> 8);
1372 for (i = 0; i < 8; i++) {
1373 riva_wclut(chip, regno*8+i,
1374 red >> 8, green >> 8,
1378 riva_rclut(chip, regno*4, &r, &g, &b);
1379 for (i = 0; i < 4; i++)
1380 riva_wclut(chip, regno*4+i, r,
1385 riva_wclut(chip, regno, red >> 8, green >> 8, blue >> 8);
1395 * rivafb_fillrect - hardware accelerated color fill function
1396 * @info: pointer to fb_info structure
1397 * @rect: pointer to fb_fillrect structure
1400 * This function fills up a region of framebuffer memory with a solid
1401 * color with a choice of two different ROP's, copy or invert.
1406 static void rivafb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
1408 struct riva_par *par = info->par;
1409 u_int color, rop = 0;
1411 if ((info->flags & FBINFO_HWACCEL_DISABLED)) {
1412 cfb_fillrect(info, rect);
1416 if (info->var.bits_per_pixel == 8)
1417 color = rect->color;
1419 if (par->riva.Architecture != NV_ARCH_03)
1420 color = ((u32 *)info->pseudo_palette)[rect->color];
1422 color = par->palette[rect->color];
1425 switch (rect->rop) {
1435 riva_set_rop_solid(par, rop);
1437 RIVA_FIFO_FREE(par->riva, Bitmap, 1);
1438 NV_WR32(&par->riva.Bitmap->Color1A, 0, color);
1440 RIVA_FIFO_FREE(par->riva, Bitmap, 2);
1441 NV_WR32(&par->riva.Bitmap->UnclippedRectangle[0].TopLeft, 0,
1442 (rect->dx << 16) | rect->dy);
1444 NV_WR32(&par->riva.Bitmap->UnclippedRectangle[0].WidthHeight, 0,
1445 (rect->width << 16) | rect->height);
1447 riva_set_rop_solid(par, 0xcc);
1452 * rivafb_copyarea - hardware accelerated blit function
1453 * @info: pointer to fb_info structure
1454 * @region: pointer to fb_copyarea structure
1457 * This copies an area of pixels from one location to another
1462 static void rivafb_copyarea(struct fb_info *info, const struct fb_copyarea *region)
1464 struct riva_par *par = info->par;
1466 if ((info->flags & FBINFO_HWACCEL_DISABLED)) {
1467 cfb_copyarea(info, region);
1471 RIVA_FIFO_FREE(par->riva, Blt, 3);
1472 NV_WR32(&par->riva.Blt->TopLeftSrc, 0,
1473 (region->sy << 16) | region->sx);
1474 NV_WR32(&par->riva.Blt->TopLeftDst, 0,
1475 (region->dy << 16) | region->dx);
1477 NV_WR32(&par->riva.Blt->WidthHeight, 0,
1478 (region->height << 16) | region->width);
1482 static inline void convert_bgcolor_16(u32 *col)
1484 *col = ((*col & 0x0000F800) << 8)
1485 | ((*col & 0x00007E0) << 5)
1486 | ((*col & 0x0000001F) << 3)
1492 * rivafb_imageblit: hardware accelerated color expand function
1493 * @info: pointer to fb_info structure
1494 * @image: pointer to fb_image structure
1497 * If the source is a monochrome bitmap, the function fills up a a region
1498 * of framebuffer memory with pixels whose color is determined by the bit
1499 * setting of the bitmap, 1 - foreground, 0 - background.
1501 * If the source is not a monochrome bitmap, color expansion is not done.
1502 * In this case, it is channeled to a software function.
1507 static void rivafb_imageblit(struct fb_info *info,
1508 const struct fb_image *image)
1510 struct riva_par *par = info->par;
1511 u32 fgx = 0, bgx = 0, width, tmp;
1512 u8 *cdat = (u8 *) image->data;
1513 volatile u32 __iomem *d;
1516 if ((info->flags & FBINFO_HWACCEL_DISABLED) || image->depth != 1) {
1517 cfb_imageblit(info, image);
1521 switch (info->var.bits_per_pixel) {
1523 fgx = image->fg_color;
1524 bgx = image->bg_color;
1528 if (par->riva.Architecture != NV_ARCH_03) {
1529 fgx = ((u32 *)info->pseudo_palette)[image->fg_color];
1530 bgx = ((u32 *)info->pseudo_palette)[image->bg_color];
1532 fgx = par->palette[image->fg_color];
1533 bgx = par->palette[image->bg_color];
1535 if (info->var.green.length == 6)
1536 convert_bgcolor_16(&bgx);
1540 RIVA_FIFO_FREE(par->riva, Bitmap, 7);
1541 NV_WR32(&par->riva.Bitmap->ClipE.TopLeft, 0,
1542 (image->dy << 16) | (image->dx & 0xFFFF));
1543 NV_WR32(&par->riva.Bitmap->ClipE.BottomRight, 0,
1544 (((image->dy + image->height) << 16) |
1545 ((image->dx + image->width) & 0xffff)));
1546 NV_WR32(&par->riva.Bitmap->Color0E, 0, bgx);
1547 NV_WR32(&par->riva.Bitmap->Color1E, 0, fgx);
1548 NV_WR32(&par->riva.Bitmap->WidthHeightInE, 0,
1549 (image->height << 16) | ((image->width + 31) & ~31));
1550 NV_WR32(&par->riva.Bitmap->WidthHeightOutE, 0,
1551 (image->height << 16) | ((image->width + 31) & ~31));
1552 NV_WR32(&par->riva.Bitmap->PointE, 0,
1553 (image->dy << 16) | (image->dx & 0xFFFF));
1555 d = &par->riva.Bitmap->MonochromeData01E;
1557 width = (image->width + 31)/32;
1558 size = width * image->height;
1559 while (size >= 16) {
1560 RIVA_FIFO_FREE(par->riva, Bitmap, 16);
1561 for (i = 0; i < 16; i++) {
1562 tmp = *((u32 *)cdat);
1563 cdat = (u8 *)((u32 *)cdat + 1);
1564 reverse_order(&tmp);
1565 NV_WR32(d, i*4, tmp);
1570 RIVA_FIFO_FREE(par->riva, Bitmap, size);
1571 for (i = 0; i < size; i++) {
1572 tmp = *((u32 *) cdat);
1573 cdat = (u8 *)((u32 *)cdat + 1);
1574 reverse_order(&tmp);
1575 NV_WR32(d, i*4, tmp);
1581 * rivafb_cursor - hardware cursor function
1582 * @info: pointer to info structure
1583 * @cursor: pointer to fbcursor structure
1586 * A cursor function that supports displaying a cursor image via hardware.
1587 * Within the kernel, copy and invert rops are supported. If exported
1588 * to user space, only the copy rop will be supported.
1593 static int rivafb_cursor(struct fb_info *info, struct fb_cursor *cursor)
1595 struct riva_par *par = info->par;
1596 u8 data[MAX_CURS * MAX_CURS/8];
1597 int i, set = cursor->set;
1600 if (cursor->image.width > MAX_CURS || cursor->image.height > MAX_CURS)
1603 par->riva.ShowHideCursor(&par->riva, 0);
1605 if (par->cursor_reset) {
1606 set = FB_CUR_SETALL;
1607 par->cursor_reset = 0;
1610 if (set & FB_CUR_SETSIZE)
1611 memset_io(par->riva.CURSOR, 0, MAX_CURS * MAX_CURS * 2);
1613 if (set & FB_CUR_SETPOS) {
1616 yy = cursor->image.dy - info->var.yoffset;
1617 xx = cursor->image.dx - info->var.xoffset;
1621 NV_WR32(par->riva.PRAMDAC, 0x0000300, temp);
1625 if (set & (FB_CUR_SETSHAPE | FB_CUR_SETCMAP | FB_CUR_SETIMAGE)) {
1626 u32 bg_idx = cursor->image.bg_color;
1627 u32 fg_idx = cursor->image.fg_color;
1628 u32 s_pitch = (cursor->image.width+7) >> 3;
1629 u32 d_pitch = MAX_CURS/8;
1630 u8 *dat = (u8 *) cursor->image.data;
1631 u8 *msk = (u8 *) cursor->mask;
1634 src = kmalloc(s_pitch * cursor->image.height, GFP_ATOMIC);
1637 switch (cursor->rop) {
1639 for (i = 0; i < s_pitch * cursor->image.height; i++)
1640 src[i] = dat[i] ^ msk[i];
1644 for (i = 0; i < s_pitch * cursor->image.height; i++)
1645 src[i] = dat[i] & msk[i];
1649 fb_pad_aligned_buffer(data, d_pitch, src, s_pitch,
1650 cursor->image.height);
1652 bg = ((info->cmap.red[bg_idx] & 0xf8) << 7) |
1653 ((info->cmap.green[bg_idx] & 0xf8) << 2) |
1654 ((info->cmap.blue[bg_idx] & 0xf8) >> 3) |
1657 fg = ((info->cmap.red[fg_idx] & 0xf8) << 7) |
1658 ((info->cmap.green[fg_idx] & 0xf8) << 2) |
1659 ((info->cmap.blue[fg_idx] & 0xf8) >> 3) |
1662 par->riva.LockUnlock(&par->riva, 0);
1664 rivafb_load_cursor_image(par, data, bg, fg,
1665 cursor->image.width,
1666 cursor->image.height);
1672 par->riva.ShowHideCursor(&par->riva, 1);
1677 static int rivafb_sync(struct fb_info *info)
1679 struct riva_par *par = info->par;
1685 /* ------------------------------------------------------------------------- *
1687 * initialization helper functions
1689 * ------------------------------------------------------------------------- */
1691 /* kernel interface */
1692 static struct fb_ops riva_fb_ops = {
1693 .owner = THIS_MODULE,
1694 .fb_open = rivafb_open,
1695 .fb_release = rivafb_release,
1696 .fb_check_var = rivafb_check_var,
1697 .fb_set_par = rivafb_set_par,
1698 .fb_setcolreg = rivafb_setcolreg,
1699 .fb_pan_display = rivafb_pan_display,
1700 .fb_blank = rivafb_blank,
1701 .fb_fillrect = rivafb_fillrect,
1702 .fb_copyarea = rivafb_copyarea,
1703 .fb_imageblit = rivafb_imageblit,
1704 .fb_cursor = rivafb_cursor,
1705 .fb_sync = rivafb_sync,
1708 static int __devinit riva_set_fbinfo(struct fb_info *info)
1710 unsigned int cmap_len;
1711 struct riva_par *par = info->par;
1714 info->flags = FBINFO_DEFAULT
1715 | FBINFO_HWACCEL_XPAN
1716 | FBINFO_HWACCEL_YPAN
1717 | FBINFO_HWACCEL_COPYAREA
1718 | FBINFO_HWACCEL_FILLRECT
1719 | FBINFO_HWACCEL_IMAGEBLIT;
1721 /* Accel seems to not work properly on NV30 yet...*/
1722 if ((par->riva.Architecture == NV_ARCH_30) || noaccel) {
1723 printk(KERN_DEBUG PFX "disabling acceleration\n");
1724 info->flags |= FBINFO_HWACCEL_DISABLED;
1727 info->var = rivafb_default_var;
1728 info->fix.visual = (info->var.bits_per_pixel == 8) ?
1729 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_DIRECTCOLOR;
1731 info->pseudo_palette = par->pseudo_palette;
1733 cmap_len = riva_get_cmap_len(&info->var);
1734 fb_alloc_cmap(&info->cmap, cmap_len, 0);
1736 info->pixmap.size = 8 * 1024;
1737 info->pixmap.buf_align = 4;
1738 info->pixmap.access_align = 32;
1739 info->pixmap.flags = FB_PIXMAP_SYSTEM;
1740 info->var.yres_virtual = -1;
1742 return (rivafb_check_var(&info->var, info));
1745 #ifdef CONFIG_PPC_OF
1746 static int __devinit riva_get_EDID_OF(struct fb_info *info, struct pci_dev *pd)
1748 struct riva_par *par = info->par;
1749 struct device_node *dp;
1750 const unsigned char *pedid = NULL;
1751 const unsigned char *disptype = NULL;
1752 static char *propnames[] = {
1753 "DFP,EDID", "LCD,EDID", "EDID", "EDID1", "EDID,B", "EDID,A", NULL };
1757 dp = pci_device_to_OF_node(pd);
1758 for (; dp != NULL; dp = dp->child) {
1759 disptype = of_get_property(dp, "display-type", NULL);
1760 if (disptype == NULL)
1762 if (strncmp(disptype, "LCD", 3) != 0)
1764 for (i = 0; propnames[i] != NULL; ++i) {
1765 pedid = of_get_property(dp, propnames[i], NULL);
1766 if (pedid != NULL) {
1767 par->EDID = (unsigned char *)pedid;
1768 NVTRACE("LCD found.\n");
1776 #endif /* CONFIG_PPC_OF */
1778 #if defined(CONFIG_FB_RIVA_I2C) && !defined(CONFIG_PPC_OF)
1779 static int __devinit riva_get_EDID_i2c(struct fb_info *info)
1781 struct riva_par *par = info->par;
1782 struct fb_var_screeninfo var;
1786 riva_create_i2c_busses(par);
1787 for (i = 0; i < 3; i++) {
1788 if (!par->chan[i].par)
1790 riva_probe_i2c_connector(par, i, &par->EDID);
1791 if (par->EDID && !fb_parse_edid(par->EDID, &var)) {
1792 printk(PFX "Found EDID Block from BUS %i\n", i);
1798 return (par->EDID) ? 1 : 0;
1800 #endif /* CONFIG_FB_RIVA_I2C */
1802 static void __devinit riva_update_default_var(struct fb_var_screeninfo *var,
1803 struct fb_info *info)
1805 struct fb_monspecs *specs = &info->monspecs;
1806 struct fb_videomode modedb;
1809 /* respect mode options */
1811 fb_find_mode(var, info, mode_option,
1812 specs->modedb, specs->modedb_len,
1814 } else if (specs->modedb != NULL) {
1815 /* get preferred timing */
1816 if (info->monspecs.misc & FB_MISC_1ST_DETAIL) {
1819 for (i = 0; i < specs->modedb_len; i++) {
1820 if (specs->modedb[i].flag & FB_MODE_IS_FIRST) {
1821 modedb = specs->modedb[i];
1826 /* otherwise, get first mode in database */
1827 modedb = specs->modedb[0];
1829 var->bits_per_pixel = 8;
1830 riva_update_var(var, &modedb);
1836 static void __devinit riva_get_EDID(struct fb_info *info, struct pci_dev *pdev)
1839 #ifdef CONFIG_PPC_OF
1840 if (!riva_get_EDID_OF(info, pdev))
1841 printk(PFX "could not retrieve EDID from OF\n");
1842 #elif defined(CONFIG_FB_RIVA_I2C)
1843 if (!riva_get_EDID_i2c(info))
1844 printk(PFX "could not retrieve EDID from DDC/I2C\n");
1850 static void __devinit riva_get_edidinfo(struct fb_info *info)
1852 struct fb_var_screeninfo *var = &rivafb_default_var;
1853 struct riva_par *par = info->par;
1855 fb_edid_to_monspecs(par->EDID, &info->monspecs);
1856 fb_videomode_to_modelist(info->monspecs.modedb, info->monspecs.modedb_len,
1858 riva_update_default_var(var, info);
1860 /* if user specified flatpanel, we respect that */
1861 if (info->monspecs.input & FB_DISP_DDI)
1865 /* ------------------------------------------------------------------------- *
1869 * ------------------------------------------------------------------------- */
1871 static u32 __devinit riva_get_arch(struct pci_dev *pd)
1875 switch (pd->device & 0x0ff0) {
1876 case 0x0100: /* GeForce 256 */
1877 case 0x0110: /* GeForce2 MX */
1878 case 0x0150: /* GeForce2 */
1879 case 0x0170: /* GeForce4 MX */
1880 case 0x0180: /* GeForce4 MX (8x AGP) */
1881 case 0x01A0: /* nForce */
1882 case 0x01F0: /* nForce2 */
1885 case 0x0200: /* GeForce3 */
1886 case 0x0250: /* GeForce4 Ti */
1887 case 0x0280: /* GeForce4 Ti (8x AGP) */
1890 case 0x0300: /* GeForceFX 5800 */
1891 case 0x0310: /* GeForceFX 5600 */
1892 case 0x0320: /* GeForceFX 5200 */
1893 case 0x0330: /* GeForceFX 5900 */
1894 case 0x0340: /* GeForceFX 5700 */
1897 case 0x0020: /* TNT, TNT2 */
1900 case 0x0010: /* Riva128 */
1903 default: /* unknown architecture */
1909 static int __devinit rivafb_probe(struct pci_dev *pd,
1910 const struct pci_device_id *ent)
1912 struct riva_par *default_par;
1913 struct fb_info *info;
1919 info = framebuffer_alloc(sizeof(struct riva_par), &pd->dev);
1921 printk (KERN_ERR PFX "could not allocate memory\n");
1925 default_par = info->par;
1926 default_par->pdev = pd;
1928 info->pixmap.addr = kzalloc(8 * 1024, GFP_KERNEL);
1929 if (info->pixmap.addr == NULL) {
1931 goto err_framebuffer_release;
1934 ret = pci_enable_device(pd);
1936 printk(KERN_ERR PFX "cannot enable PCI device\n");
1937 goto err_free_pixmap;
1940 ret = pci_request_regions(pd, "rivafb");
1942 printk(KERN_ERR PFX "cannot request PCI regions\n");
1943 goto err_disable_device;
1946 mutex_init(&default_par->open_lock);
1947 default_par->riva.Architecture = riva_get_arch(pd);
1949 default_par->Chipset = (pd->vendor << 16) | pd->device;
1950 printk(KERN_INFO PFX "nVidia device/chipset %X\n",default_par->Chipset);
1952 if(default_par->riva.Architecture == 0) {
1953 printk(KERN_ERR PFX "unknown NV_ARCH\n");
1955 goto err_release_region;
1957 if(default_par->riva.Architecture == NV_ARCH_10 ||
1958 default_par->riva.Architecture == NV_ARCH_20 ||
1959 default_par->riva.Architecture == NV_ARCH_30) {
1960 sprintf(rivafb_fix.id, "NV%x", (pd->device & 0x0ff0) >> 4);
1962 sprintf(rivafb_fix.id, "NV%x", default_par->riva.Architecture);
1965 default_par->FlatPanel = flatpanel;
1967 printk(KERN_INFO PFX "flatpanel support enabled\n");
1968 default_par->forceCRTC = forceCRTC;
1970 rivafb_fix.mmio_len = pci_resource_len(pd, 0);
1971 rivafb_fix.smem_len = pci_resource_len(pd, 1);
1974 /* enable IO and mem if not already done */
1977 pci_read_config_word(pd, PCI_COMMAND, &cmd);
1978 cmd |= (PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
1979 pci_write_config_word(pd, PCI_COMMAND, cmd);
1982 rivafb_fix.mmio_start = pci_resource_start(pd, 0);
1983 rivafb_fix.smem_start = pci_resource_start(pd, 1);
1985 default_par->ctrl_base = ioremap(rivafb_fix.mmio_start,
1986 rivafb_fix.mmio_len);
1987 if (!default_par->ctrl_base) {
1988 printk(KERN_ERR PFX "cannot ioremap MMIO base\n");
1990 goto err_release_region;
1993 switch (default_par->riva.Architecture) {
1995 /* Riva128's PRAMIN is in the "framebuffer" space
1996 * Since these cards were never made with more than 8 megabytes
1997 * we can safely allocate this separately.
1999 default_par->riva.PRAMIN = ioremap(rivafb_fix.smem_start + 0x00C00000, 0x00008000);
2000 if (!default_par->riva.PRAMIN) {
2001 printk(KERN_ERR PFX "cannot ioremap PRAMIN region\n");
2003 goto err_iounmap_ctrl_base;
2010 default_par->riva.PCRTC0 =
2011 (u32 __iomem *)(default_par->ctrl_base + 0x00600000);
2012 default_par->riva.PRAMIN =
2013 (u32 __iomem *)(default_par->ctrl_base + 0x00710000);
2016 riva_common_setup(default_par);
2018 if (default_par->riva.Architecture == NV_ARCH_03) {
2019 default_par->riva.PCRTC = default_par->riva.PCRTC0
2020 = default_par->riva.PGRAPH;
2023 rivafb_fix.smem_len = riva_get_memlen(default_par) * 1024;
2024 default_par->dclk_max = riva_get_maxdclk(default_par) * 1000;
2025 info->screen_base = ioremap(rivafb_fix.smem_start,
2026 rivafb_fix.smem_len);
2027 if (!info->screen_base) {
2028 printk(KERN_ERR PFX "cannot ioremap FB base\n");
2030 goto err_iounmap_pramin;
2035 default_par->mtrr.vram = mtrr_add(rivafb_fix.smem_start,
2036 rivafb_fix.smem_len,
2037 MTRR_TYPE_WRCOMB, 1);
2038 if (default_par->mtrr.vram < 0) {
2039 printk(KERN_ERR PFX "unable to setup MTRR\n");
2041 default_par->mtrr.vram_valid = 1;
2042 /* let there be speed */
2043 printk(KERN_INFO PFX "RIVA MTRR set to ON\n");
2046 #endif /* CONFIG_MTRR */
2048 info->fbops = &riva_fb_ops;
2049 info->fix = rivafb_fix;
2050 riva_get_EDID(info, pd);
2051 riva_get_edidinfo(info);
2053 ret=riva_set_fbinfo(info);
2055 printk(KERN_ERR PFX "error setting initial video mode\n");
2056 goto err_iounmap_screen_base;
2059 fb_destroy_modedb(info->monspecs.modedb);
2060 info->monspecs.modedb = NULL;
2062 pci_set_drvdata(pd, info);
2065 riva_bl_init(info->par);
2067 ret = register_framebuffer(info);
2070 "error registering riva framebuffer\n");
2071 goto err_iounmap_screen_base;
2074 printk(KERN_INFO PFX
2075 "PCI nVidia %s framebuffer ver %s (%dMB @ 0x%lX)\n",
2078 info->fix.smem_len / (1024 * 1024),
2079 info->fix.smem_start);
2084 err_iounmap_screen_base:
2085 #ifdef CONFIG_FB_RIVA_I2C
2086 riva_delete_i2c_busses(info->par);
2088 iounmap(info->screen_base);
2090 if (default_par->riva.Architecture == NV_ARCH_03)
2091 iounmap(default_par->riva.PRAMIN);
2092 err_iounmap_ctrl_base:
2093 iounmap(default_par->ctrl_base);
2095 pci_release_regions(pd);
2098 kfree(info->pixmap.addr);
2099 err_framebuffer_release:
2100 framebuffer_release(info);
2105 static void __devexit rivafb_remove(struct pci_dev *pd)
2107 struct fb_info *info = pci_get_drvdata(pd);
2108 struct riva_par *par = info->par;
2112 #ifdef CONFIG_FB_RIVA_I2C
2113 riva_delete_i2c_busses(par);
2117 unregister_framebuffer(info);
2122 if (par->mtrr.vram_valid)
2123 mtrr_del(par->mtrr.vram, info->fix.smem_start,
2124 info->fix.smem_len);
2125 #endif /* CONFIG_MTRR */
2127 iounmap(par->ctrl_base);
2128 iounmap(info->screen_base);
2129 if (par->riva.Architecture == NV_ARCH_03)
2130 iounmap(par->riva.PRAMIN);
2131 pci_release_regions(pd);
2132 kfree(info->pixmap.addr);
2133 framebuffer_release(info);
2134 pci_set_drvdata(pd, NULL);
2138 /* ------------------------------------------------------------------------- *
2142 * ------------------------------------------------------------------------- */
2145 static int __devinit rivafb_setup(char *options)
2150 if (!options || !*options)
2153 while ((this_opt = strsep(&options, ",")) != NULL) {
2154 if (!strncmp(this_opt, "forceCRTC", 9)) {
2158 if (!*p || !*(++p)) continue;
2159 forceCRTC = *p - '0';
2160 if (forceCRTC < 0 || forceCRTC > 1)
2162 } else if (!strncmp(this_opt, "flatpanel", 9)) {
2164 } else if (!strncmp(this_opt, "backlight:", 10)) {
2165 backlight = simple_strtoul(this_opt+10, NULL, 0);
2167 } else if (!strncmp(this_opt, "nomtrr", 6)) {
2170 } else if (!strncmp(this_opt, "strictmode", 10)) {
2172 } else if (!strncmp(this_opt, "noaccel", 7)) {
2175 mode_option = this_opt;
2180 #endif /* !MODULE */
2182 static struct pci_driver rivafb_driver = {
2184 .id_table = rivafb_pci_tbl,
2185 .probe = rivafb_probe,
2186 .remove = __devexit_p(rivafb_remove),
2191 /* ------------------------------------------------------------------------- *
2195 * ------------------------------------------------------------------------- */
2197 static int __devinit rivafb_init(void)
2200 char *option = NULL;
2202 if (fb_get_options("rivafb", &option))
2204 rivafb_setup(option);
2206 return pci_register_driver(&rivafb_driver);
2210 module_init(rivafb_init);
2212 static void __exit rivafb_exit(void)
2214 pci_unregister_driver(&rivafb_driver);
2217 module_exit(rivafb_exit);
2219 module_param(noaccel, bool, 0);
2220 MODULE_PARM_DESC(noaccel, "bool: disable acceleration");
2221 module_param(flatpanel, int, 0);
2222 MODULE_PARM_DESC(flatpanel, "Enables experimental flat panel support for some chipsets. (0 or 1=enabled) (default=0)");
2223 module_param(forceCRTC, int, 0);
2224 MODULE_PARM_DESC(forceCRTC, "Forces usage of a particular CRTC in case autodetection fails. (0 or 1) (default=autodetect)");
2226 module_param(nomtrr, bool, 0);
2227 MODULE_PARM_DESC(nomtrr, "Disables MTRR support (0 or 1=disabled) (default=0)");
2229 module_param(strictmode, bool, 0);
2230 MODULE_PARM_DESC(strictmode, "Only use video modes from EDID");
2232 MODULE_AUTHOR("Ani Joshi, maintainer");
2233 MODULE_DESCRIPTION("Framebuffer driver for nVidia Riva 128, TNT, TNT2, and the GeForce series");
2234 MODULE_LICENSE("GPL");