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 #ifndef CONFIG_PCI /* sanity check */
60 #error This driver requires PCI support.
63 /* version number of this driver */
64 #define RIVAFB_VERSION "0.9.5b"
66 /* ------------------------------------------------------------------------- *
68 * various helpful macros and constants
70 * ------------------------------------------------------------------------- */
71 #ifdef CONFIG_FB_RIVA_DEBUG
72 #define NVTRACE printk
74 #define NVTRACE if(0) printk
77 #define NVTRACE_ENTER(...) NVTRACE("%s START\n", __FUNCTION__)
78 #define NVTRACE_LEAVE(...) NVTRACE("%s END\n", __FUNCTION__)
80 #ifdef CONFIG_FB_RIVA_DEBUG
81 #define assert(expr) \
83 printk( "Assertion failed! %s,%s,%s,line=%d\n",\
84 #expr,__FILE__,__FUNCTION__,__LINE__); \
91 #define PFX "rivafb: "
93 /* macro that allows you to set overflow bits */
94 #define SetBitField(value,from,to) SetBF(to,GetBF(value,from))
95 #define SetBit(n) (1<<(n))
96 #define Set8Bits(value) ((value)&0xff)
98 /* HW cursor parameters */
101 /* ------------------------------------------------------------------------- *
105 * ------------------------------------------------------------------------- */
107 static int rivafb_blank(int blank, struct fb_info *info);
109 /* ------------------------------------------------------------------------- *
111 * card identification
113 * ------------------------------------------------------------------------- */
115 static struct pci_device_id rivafb_pci_tbl[] = {
116 { PCI_VENDOR_ID_NVIDIA_SGS, PCI_DEVICE_ID_NVIDIA_SGS_RIVA128,
117 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
118 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_TNT,
119 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
120 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_TNT2,
121 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
122 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_UTNT2,
123 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
124 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_VTNT2,
125 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
126 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_UVTNT2,
127 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
128 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_ITNT2,
129 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
130 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE_SDR,
131 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
132 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE_DDR,
133 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
134 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO,
135 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
136 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE2_MX,
137 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
138 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE2_MX2,
139 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
140 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE2_GO,
141 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
142 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO2_MXR,
143 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
144 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE2_GTS,
145 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
146 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE2_GTS2,
147 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
148 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE2_ULTRA,
149 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
150 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO2_PRO,
151 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
152 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_MX_460,
153 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
154 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_MX_440,
155 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
156 // NF2/IGP version, GeForce 4 MX, NV18
157 { PCI_VENDOR_ID_NVIDIA, 0x01f0,
158 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
159 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_MX_420,
160 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
161 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_440_GO,
162 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
163 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_420_GO,
164 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
165 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_420_GO_M32,
166 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
167 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO4_500XGL,
168 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
169 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_440_GO_M64,
170 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
171 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO4_200,
172 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
173 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO4_550XGL,
174 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
175 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO4_500_GOGL,
176 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
177 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_IGEFORCE2,
178 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
179 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE3,
180 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
181 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE3_1,
182 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
183 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE3_2,
184 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
185 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO_DDC,
186 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
187 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_TI_4600,
188 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
189 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_TI_4400,
190 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
191 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_TI_4200,
192 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
193 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO4_900XGL,
194 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
195 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO4_750XGL,
196 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
197 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO4_700XGL,
198 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
199 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE_FX_GO_5200,
200 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
201 { 0, } /* terminate list */
203 MODULE_DEVICE_TABLE(pci, rivafb_pci_tbl);
205 /* ------------------------------------------------------------------------- *
209 * ------------------------------------------------------------------------- */
211 /* command line data, set in rivafb_setup() */
212 static int flatpanel __devinitdata = -1; /* Autodetect later */
213 static int forceCRTC __devinitdata = -1;
214 static int noaccel __devinitdata = 0;
216 static int nomtrr __devinitdata = 0;
219 static char *mode_option __devinitdata = NULL;
220 static int strictmode = 0;
222 static struct fb_fix_screeninfo __devinitdata rivafb_fix = {
223 .type = FB_TYPE_PACKED_PIXELS,
228 static struct fb_var_screeninfo __devinitdata rivafb_default_var = {
238 .activate = FB_ACTIVATE_NOW,
248 .vmode = FB_VMODE_NONINTERLACED
252 static const struct riva_regs reg_template = {
253 {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* ATTR */
254 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
255 0x41, 0x01, 0x0F, 0x00, 0x00},
256 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* CRT */
257 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
258 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE3, /* 0x10 */
259 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
260 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x20 */
261 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
262 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x30 */
263 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
266 {0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x05, 0x0F, /* GRA */
268 {0x03, 0x01, 0x0F, 0x00, 0x0E}, /* SEQ */
275 #ifdef CONFIG_FB_RIVA_BACKLIGHT
276 /* We do not have any information about which values are allowed, thus
277 * we used safe values.
279 #define MIN_LEVEL 0x158
280 #define MAX_LEVEL 0x534
281 #define LEVEL_STEP ((MAX_LEVEL - MIN_LEVEL) / FB_BACKLIGHT_MAX)
283 static struct backlight_properties riva_bl_data;
285 static int riva_bl_get_level_brightness(struct riva_par *par,
288 struct fb_info *info = pci_get_drvdata(par->pdev);
291 /* Get and convert the value */
292 /* No locking on bl_curve since accessing a single value */
293 nlevel = MIN_LEVEL + info->bl_curve[level] * LEVEL_STEP;
297 else if (nlevel < MIN_LEVEL)
299 else if (nlevel > MAX_LEVEL)
305 static int riva_bl_update_status(struct backlight_device *bd)
307 struct riva_par *par = class_get_devdata(&bd->class_dev);
308 U032 tmp_pcrt, tmp_pmc;
311 if (bd->props.power != FB_BLANK_UNBLANK ||
312 bd->props.fb_blank != FB_BLANK_UNBLANK)
315 level = bd->props.brightness;
317 tmp_pmc = par->riva.PMC[0x10F0/4] & 0x0000FFFF;
318 tmp_pcrt = par->riva.PCRTC0[0x081C/4] & 0xFFFFFFFC;
321 tmp_pmc |= (1 << 31); /* backlight bit */
322 tmp_pmc |= riva_bl_get_level_brightness(par, level) << 16; /* level */
324 par->riva.PCRTC0[0x081C/4] = tmp_pcrt;
325 par->riva.PMC[0x10F0/4] = tmp_pmc;
330 static int riva_bl_get_brightness(struct backlight_device *bd)
332 return bd->props.brightness;
335 static struct backlight_ops riva_bl_ops = {
336 .get_brightness = riva_bl_get_brightness,
337 .update_status = riva_bl_update_status,
340 static void riva_bl_init(struct riva_par *par)
342 struct fb_info *info = pci_get_drvdata(par->pdev);
343 struct backlight_device *bd;
349 #ifdef CONFIG_PMAC_BACKLIGHT
350 if (!machine_is(powermac) ||
351 !pmac_has_backlight_type("mnca"))
355 snprintf(name, sizeof(name), "rivabl%d", info->node);
357 bd = backlight_device_register(name, info->dev, par, &riva_bl_ops);
360 printk(KERN_WARNING "riva: Backlight registration failed\n");
365 fb_bl_default_curve(info, 0,
366 MIN_LEVEL * FB_BACKLIGHT_MAX / MAX_LEVEL,
369 bd->props.max_brightness = FB_BACKLIGHT_LEVELS - 1;
370 bd->props.brightness = riva_bl_data.max_brightness;
371 bd->props.power = FB_BLANK_UNBLANK;
372 backlight_update_status(bd);
374 printk("riva: Backlight initialized (%s)\n", name);
382 static void riva_bl_exit(struct fb_info *info)
384 struct backlight_device *bd = info->bl_dev;
386 backlight_device_unregister(bd);
387 printk("riva: Backlight unloaded\n");
390 static inline void riva_bl_init(struct riva_par *par) {}
391 static inline void riva_bl_exit(struct fb_info *info) {}
392 #endif /* CONFIG_FB_RIVA_BACKLIGHT */
394 /* ------------------------------------------------------------------------- *
398 * ------------------------------------------------------------------------- */
400 static inline void CRTCout(struct riva_par *par, unsigned char index,
403 VGA_WR08(par->riva.PCIO, 0x3d4, index);
404 VGA_WR08(par->riva.PCIO, 0x3d5, val);
407 static inline unsigned char CRTCin(struct riva_par *par,
410 VGA_WR08(par->riva.PCIO, 0x3d4, index);
411 return (VGA_RD08(par->riva.PCIO, 0x3d5));
414 static inline void GRAout(struct riva_par *par, unsigned char index,
417 VGA_WR08(par->riva.PVIO, 0x3ce, index);
418 VGA_WR08(par->riva.PVIO, 0x3cf, val);
421 static inline unsigned char GRAin(struct riva_par *par,
424 VGA_WR08(par->riva.PVIO, 0x3ce, index);
425 return (VGA_RD08(par->riva.PVIO, 0x3cf));
428 static inline void SEQout(struct riva_par *par, unsigned char index,
431 VGA_WR08(par->riva.PVIO, 0x3c4, index);
432 VGA_WR08(par->riva.PVIO, 0x3c5, val);
435 static inline unsigned char SEQin(struct riva_par *par,
438 VGA_WR08(par->riva.PVIO, 0x3c4, index);
439 return (VGA_RD08(par->riva.PVIO, 0x3c5));
442 static inline void ATTRout(struct riva_par *par, unsigned char index,
445 VGA_WR08(par->riva.PCIO, 0x3c0, index);
446 VGA_WR08(par->riva.PCIO, 0x3c0, val);
449 static inline unsigned char ATTRin(struct riva_par *par,
452 VGA_WR08(par->riva.PCIO, 0x3c0, index);
453 return (VGA_RD08(par->riva.PCIO, 0x3c1));
456 static inline void MISCout(struct riva_par *par, unsigned char val)
458 VGA_WR08(par->riva.PVIO, 0x3c2, val);
461 static inline unsigned char MISCin(struct riva_par *par)
463 return (VGA_RD08(par->riva.PVIO, 0x3cc));
466 static inline void reverse_order(u32 *l)
469 a[0] = bitrev8(a[0]);
470 a[1] = bitrev8(a[1]);
471 a[2] = bitrev8(a[2]);
472 a[3] = bitrev8(a[3]);
475 /* ------------------------------------------------------------------------- *
479 * ------------------------------------------------------------------------- */
482 * rivafb_load_cursor_image - load cursor image to hardware
483 * @data: address to monochrome bitmap (1 = foreground color, 0 = background)
484 * @par: pointer to private data
485 * @w: width of cursor image in pixels
486 * @h: height of cursor image in scanlines
487 * @bg: background color (ARGB1555) - alpha bit determines opacity
488 * @fg: foreground color (ARGB1555)
491 * Loads cursor image based on a monochrome source and mask bitmap. The
492 * image bits determines the color of the pixel, 0 for background, 1 for
493 * foreground. Only the affected region (as determined by @w and @h
494 * parameters) will be updated.
499 static void rivafb_load_cursor_image(struct riva_par *par, u8 *data8,
500 u16 bg, u16 fg, u32 w, u32 h)
504 u32 *data = (u32 *)data8;
505 bg = le16_to_cpu(bg);
506 fg = le16_to_cpu(fg);
510 for (i = 0; i < h; i++) {
514 for (j = 0; j < w/2; j++) {
516 #if defined (__BIG_ENDIAN)
517 tmp = (b & (1 << 31)) ? fg << 16 : bg << 16;
519 tmp |= (b & (1 << 31)) ? fg : bg;
522 tmp = (b & 1) ? fg : bg;
524 tmp |= (b & 1) ? fg << 16 : bg << 16;
527 writel(tmp, &par->riva.CURSOR[k++]);
529 k += (MAX_CURS - w)/2;
533 /* ------------------------------------------------------------------------- *
535 * general utility functions
537 * ------------------------------------------------------------------------- */
540 * riva_wclut - set CLUT entry
541 * @chip: pointer to RIVA_HW_INST object
542 * @regnum: register number
543 * @red: red component
544 * @green: green component
545 * @blue: blue component
548 * Sets color register @regnum.
553 static void riva_wclut(RIVA_HW_INST *chip,
554 unsigned char regnum, unsigned char red,
555 unsigned char green, unsigned char blue)
557 VGA_WR08(chip->PDIO, 0x3c8, regnum);
558 VGA_WR08(chip->PDIO, 0x3c9, red);
559 VGA_WR08(chip->PDIO, 0x3c9, green);
560 VGA_WR08(chip->PDIO, 0x3c9, blue);
564 * riva_rclut - read fromCLUT register
565 * @chip: pointer to RIVA_HW_INST object
566 * @regnum: register number
567 * @red: red component
568 * @green: green component
569 * @blue: blue component
572 * Reads red, green, and blue from color register @regnum.
577 static void riva_rclut(RIVA_HW_INST *chip,
578 unsigned char regnum, unsigned char *red,
579 unsigned char *green, unsigned char *blue)
582 VGA_WR08(chip->PDIO, 0x3c7, regnum);
583 *red = VGA_RD08(chip->PDIO, 0x3c9);
584 *green = VGA_RD08(chip->PDIO, 0x3c9);
585 *blue = VGA_RD08(chip->PDIO, 0x3c9);
589 * riva_save_state - saves current chip state
590 * @par: pointer to riva_par object containing info for current riva board
591 * @regs: pointer to riva_regs object
594 * Saves current chip state to @regs.
600 static void riva_save_state(struct riva_par *par, struct riva_regs *regs)
605 par->riva.LockUnlock(&par->riva, 0);
607 par->riva.UnloadStateExt(&par->riva, ®s->ext);
609 regs->misc_output = MISCin(par);
611 for (i = 0; i < NUM_CRT_REGS; i++)
612 regs->crtc[i] = CRTCin(par, i);
614 for (i = 0; i < NUM_ATC_REGS; i++)
615 regs->attr[i] = ATTRin(par, i);
617 for (i = 0; i < NUM_GRC_REGS; i++)
618 regs->gra[i] = GRAin(par, i);
620 for (i = 0; i < NUM_SEQ_REGS; i++)
621 regs->seq[i] = SEQin(par, i);
626 * riva_load_state - loads current chip state
627 * @par: pointer to riva_par object containing info for current riva board
628 * @regs: pointer to riva_regs object
631 * Loads chip state from @regs.
634 * riva_load_video_mode()
639 static void riva_load_state(struct riva_par *par, struct riva_regs *regs)
641 RIVA_HW_STATE *state = ®s->ext;
645 CRTCout(par, 0x11, 0x00);
647 par->riva.LockUnlock(&par->riva, 0);
649 par->riva.LoadStateExt(&par->riva, state);
651 MISCout(par, regs->misc_output);
653 for (i = 0; i < NUM_CRT_REGS; i++) {
659 CRTCout(par, i, regs->crtc[i]);
663 for (i = 0; i < NUM_ATC_REGS; i++)
664 ATTRout(par, i, regs->attr[i]);
666 for (i = 0; i < NUM_GRC_REGS; i++)
667 GRAout(par, i, regs->gra[i]);
669 for (i = 0; i < NUM_SEQ_REGS; i++)
670 SEQout(par, i, regs->seq[i]);
675 * riva_load_video_mode - calculate timings
676 * @info: pointer to fb_info object containing info for current riva board
679 * Calculate some timings and then send em off to riva_load_state().
684 static int riva_load_video_mode(struct fb_info *info)
686 int bpp, width, hDisplaySize, hDisplay, hStart,
687 hEnd, hTotal, height, vDisplay, vStart, vEnd, vTotal, dotClock;
688 int hBlankStart, hBlankEnd, vBlankStart, vBlankEnd;
690 struct riva_par *par = info->par;
691 struct riva_regs newmode;
694 /* time to calculate */
695 rivafb_blank(FB_BLANK_NORMAL, info);
697 bpp = info->var.bits_per_pixel;
698 if (bpp == 16 && info->var.green.length == 5)
700 width = info->var.xres_virtual;
701 hDisplaySize = info->var.xres;
702 hDisplay = (hDisplaySize / 8) - 1;
703 hStart = (hDisplaySize + info->var.right_margin) / 8 - 1;
704 hEnd = (hDisplaySize + info->var.right_margin +
705 info->var.hsync_len) / 8 - 1;
706 hTotal = (hDisplaySize + info->var.right_margin +
707 info->var.hsync_len + info->var.left_margin) / 8 - 5;
708 hBlankStart = hDisplay;
709 hBlankEnd = hTotal + 4;
711 height = info->var.yres_virtual;
712 vDisplay = info->var.yres - 1;
713 vStart = info->var.yres + info->var.lower_margin - 1;
714 vEnd = info->var.yres + info->var.lower_margin +
715 info->var.vsync_len - 1;
716 vTotal = info->var.yres + info->var.lower_margin +
717 info->var.vsync_len + info->var.upper_margin + 2;
718 vBlankStart = vDisplay;
719 vBlankEnd = vTotal + 1;
720 dotClock = 1000000000 / info->var.pixclock;
722 memcpy(&newmode, ®_template, sizeof(struct riva_regs));
724 if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED)
727 if (par->FlatPanel) {
730 vBlankStart = vStart;
733 hBlankEnd = hTotal + 4;
736 newmode.crtc[0x0] = Set8Bits (hTotal);
737 newmode.crtc[0x1] = Set8Bits (hDisplay);
738 newmode.crtc[0x2] = Set8Bits (hBlankStart);
739 newmode.crtc[0x3] = SetBitField (hBlankEnd, 4: 0, 4:0) | SetBit (7);
740 newmode.crtc[0x4] = Set8Bits (hStart);
741 newmode.crtc[0x5] = SetBitField (hBlankEnd, 5: 5, 7:7)
742 | SetBitField (hEnd, 4: 0, 4:0);
743 newmode.crtc[0x6] = SetBitField (vTotal, 7: 0, 7:0);
744 newmode.crtc[0x7] = SetBitField (vTotal, 8: 8, 0:0)
745 | SetBitField (vDisplay, 8: 8, 1:1)
746 | SetBitField (vStart, 8: 8, 2:2)
747 | SetBitField (vBlankStart, 8: 8, 3:3)
749 | SetBitField (vTotal, 9: 9, 5:5)
750 | SetBitField (vDisplay, 9: 9, 6:6)
751 | SetBitField (vStart, 9: 9, 7:7);
752 newmode.crtc[0x9] = SetBitField (vBlankStart, 9: 9, 5:5)
754 newmode.crtc[0x10] = Set8Bits (vStart);
755 newmode.crtc[0x11] = SetBitField (vEnd, 3: 0, 3:0)
757 newmode.crtc[0x12] = Set8Bits (vDisplay);
758 newmode.crtc[0x13] = (width / 8) * ((bpp + 1) / 8);
759 newmode.crtc[0x15] = Set8Bits (vBlankStart);
760 newmode.crtc[0x16] = Set8Bits (vBlankEnd);
762 newmode.ext.screen = SetBitField(hBlankEnd,6:6,4:4)
763 | SetBitField(vBlankStart,10:10,3:3)
764 | SetBitField(vStart,10:10,2:2)
765 | SetBitField(vDisplay,10:10,1:1)
766 | SetBitField(vTotal,10:10,0:0);
767 newmode.ext.horiz = SetBitField(hTotal,8:8,0:0)
768 | SetBitField(hDisplay,8:8,1:1)
769 | SetBitField(hBlankStart,8:8,2:2)
770 | SetBitField(hStart,8:8,3:3);
771 newmode.ext.extra = SetBitField(vTotal,11:11,0:0)
772 | SetBitField(vDisplay,11:11,2:2)
773 | SetBitField(vStart,11:11,4:4)
774 | SetBitField(vBlankStart,11:11,6:6);
776 if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) {
777 int tmp = (hTotal >> 1) & ~1;
778 newmode.ext.interlace = Set8Bits(tmp);
779 newmode.ext.horiz |= SetBitField(tmp, 8:8,4:4);
781 newmode.ext.interlace = 0xff; /* interlace off */
783 if (par->riva.Architecture >= NV_ARCH_10)
784 par->riva.CURSOR = (U032 __iomem *)(info->screen_base + par->riva.CursorStart);
786 if (info->var.sync & FB_SYNC_HOR_HIGH_ACT)
787 newmode.misc_output &= ~0x40;
789 newmode.misc_output |= 0x40;
790 if (info->var.sync & FB_SYNC_VERT_HIGH_ACT)
791 newmode.misc_output &= ~0x80;
793 newmode.misc_output |= 0x80;
795 rc = CalcStateExt(&par->riva, &newmode.ext, bpp, width,
796 hDisplaySize, height, dotClock);
800 newmode.ext.scale = NV_RD32(par->riva.PRAMDAC, 0x00000848) &
802 if (par->FlatPanel == 1) {
803 newmode.ext.pixel |= (1 << 7);
804 newmode.ext.scale |= (1 << 8);
806 if (par->SecondCRTC) {
807 newmode.ext.head = NV_RD32(par->riva.PCRTC0, 0x00000860) &
809 newmode.ext.head2 = NV_RD32(par->riva.PCRTC0, 0x00002860) |
811 newmode.ext.crtcOwner = 3;
812 newmode.ext.pllsel |= 0x20000800;
813 newmode.ext.vpll2 = newmode.ext.vpll;
814 } else if (par->riva.twoHeads) {
815 newmode.ext.head = NV_RD32(par->riva.PCRTC0, 0x00000860) |
817 newmode.ext.head2 = NV_RD32(par->riva.PCRTC0, 0x00002860) &
819 newmode.ext.crtcOwner = 0;
820 newmode.ext.vpll2 = NV_RD32(par->riva.PRAMDAC0, 0x00000520);
822 if (par->FlatPanel == 1) {
823 newmode.ext.pixel |= (1 << 7);
824 newmode.ext.scale |= (1 << 8);
826 newmode.ext.cursorConfig = 0x02000100;
827 par->current_state = newmode;
828 riva_load_state(par, &par->current_state);
829 par->riva.LockUnlock(&par->riva, 0); /* important for HW cursor */
832 rivafb_blank(FB_BLANK_UNBLANK, info);
838 static void riva_update_var(struct fb_var_screeninfo *var,
839 const struct fb_videomode *modedb)
842 var->xres = var->xres_virtual = modedb->xres;
843 var->yres = modedb->yres;
844 if (var->yres_virtual < var->yres)
845 var->yres_virtual = var->yres;
846 var->xoffset = var->yoffset = 0;
847 var->pixclock = modedb->pixclock;
848 var->left_margin = modedb->left_margin;
849 var->right_margin = modedb->right_margin;
850 var->upper_margin = modedb->upper_margin;
851 var->lower_margin = modedb->lower_margin;
852 var->hsync_len = modedb->hsync_len;
853 var->vsync_len = modedb->vsync_len;
854 var->sync = modedb->sync;
855 var->vmode = modedb->vmode;
860 * rivafb_do_maximize -
861 * @info: pointer to fb_info object containing info for current riva board
870 * -EINVAL on failure, 0 on success
876 static int rivafb_do_maximize(struct fb_info *info,
877 struct fb_var_screeninfo *var,
893 /* use highest possible virtual resolution */
894 if (var->xres_virtual == -1 && var->yres_virtual == -1) {
895 printk(KERN_WARNING PFX
896 "using maximum available virtual resolution\n");
897 for (i = 0; modes[i].xres != -1; i++) {
898 if (modes[i].xres * nom / den * modes[i].yres <
902 if (modes[i].xres == -1) {
904 "could not find a virtual resolution that fits into video memory!!\n");
905 NVTRACE("EXIT - EINVAL error\n");
908 var->xres_virtual = modes[i].xres;
909 var->yres_virtual = modes[i].yres;
912 "virtual resolution set to maximum of %dx%d\n",
913 var->xres_virtual, var->yres_virtual);
914 } else if (var->xres_virtual == -1) {
915 var->xres_virtual = (info->fix.smem_len * den /
916 (nom * var->yres_virtual)) & ~15;
917 printk(KERN_WARNING PFX
918 "setting virtual X resolution to %d\n", var->xres_virtual);
919 } else if (var->yres_virtual == -1) {
920 var->xres_virtual = (var->xres_virtual + 15) & ~15;
921 var->yres_virtual = info->fix.smem_len * den /
922 (nom * var->xres_virtual);
923 printk(KERN_WARNING PFX
924 "setting virtual Y resolution to %d\n", var->yres_virtual);
926 var->xres_virtual = (var->xres_virtual + 15) & ~15;
927 if (var->xres_virtual * nom / den * var->yres_virtual > info->fix.smem_len) {
929 "mode %dx%dx%d rejected...resolution too high to fit into video memory!\n",
930 var->xres, var->yres, var->bits_per_pixel);
931 NVTRACE("EXIT - EINVAL error\n");
936 if (var->xres_virtual * nom / den >= 8192) {
937 printk(KERN_WARNING PFX
938 "virtual X resolution (%d) is too high, lowering to %d\n",
939 var->xres_virtual, 8192 * den / nom - 16);
940 var->xres_virtual = 8192 * den / nom - 16;
943 if (var->xres_virtual < var->xres) {
945 "virtual X resolution (%d) is smaller than real\n", var->xres_virtual);
949 if (var->yres_virtual < var->yres) {
951 "virtual Y resolution (%d) is smaller than real\n", var->yres_virtual);
954 if (var->yres_virtual > 0x7fff/nom)
955 var->yres_virtual = 0x7fff/nom;
956 if (var->xres_virtual > 0x7fff/nom)
957 var->xres_virtual = 0x7fff/nom;
963 riva_set_pattern(struct riva_par *par, int clr0, int clr1, int pat0, int pat1)
965 RIVA_FIFO_FREE(par->riva, Patt, 4);
966 NV_WR32(&par->riva.Patt->Color0, 0, clr0);
967 NV_WR32(&par->riva.Patt->Color1, 0, clr1);
968 NV_WR32(par->riva.Patt->Monochrome, 0, pat0);
969 NV_WR32(par->riva.Patt->Monochrome, 4, pat1);
972 /* acceleration routines */
973 static inline void wait_for_idle(struct riva_par *par)
975 while (par->riva.Busy(&par->riva));
979 * Set ROP. Translate X rop into ROP3. Internal routine.
982 riva_set_rop_solid(struct riva_par *par, int rop)
984 riva_set_pattern(par, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
985 RIVA_FIFO_FREE(par->riva, Rop, 1);
986 NV_WR32(&par->riva.Rop->Rop3, 0, rop);
990 static void riva_setup_accel(struct fb_info *info)
992 struct riva_par *par = info->par;
994 RIVA_FIFO_FREE(par->riva, Clip, 2);
995 NV_WR32(&par->riva.Clip->TopLeft, 0, 0x0);
996 NV_WR32(&par->riva.Clip->WidthHeight, 0,
997 (info->var.xres_virtual & 0xffff) |
998 (info->var.yres_virtual << 16));
999 riva_set_rop_solid(par, 0xcc);
1004 * riva_get_cmap_len - query current color map length
1005 * @var: standard kernel fb changeable data
1008 * Get current color map length.
1011 * Length of color map
1014 * rivafb_setcolreg()
1016 static int riva_get_cmap_len(const struct fb_var_screeninfo *var)
1018 int rc = 256; /* reasonable default */
1020 switch (var->green.length) {
1022 rc = 256; /* 256 entries (2^8), 8 bpp and RGB8888 */
1025 rc = 32; /* 32 entries (2^5), 16 bpp, RGB555 */
1028 rc = 64; /* 64 entries (2^6), 16 bpp, RGB565 */
1031 /* should not occur */
1037 /* ------------------------------------------------------------------------- *
1039 * framebuffer operations
1041 * ------------------------------------------------------------------------- */
1043 static int rivafb_open(struct fb_info *info, int user)
1045 struct riva_par *par = info->par;
1048 mutex_lock(&par->open_lock);
1049 if (!par->ref_count) {
1051 memset(&par->state, 0, sizeof(struct vgastate));
1052 par->state.flags = VGA_SAVE_MODE | VGA_SAVE_FONTS;
1053 /* save the DAC for Riva128 */
1054 if (par->riva.Architecture == NV_ARCH_03)
1055 par->state.flags |= VGA_SAVE_CMAP;
1056 save_vga(&par->state);
1058 /* vgaHWunlock() + riva unlock (0x7F) */
1059 CRTCout(par, 0x11, 0xFF);
1060 par->riva.LockUnlock(&par->riva, 0);
1062 riva_save_state(par, &par->initial_state);
1065 mutex_unlock(&par->open_lock);
1070 static int rivafb_release(struct fb_info *info, int user)
1072 struct riva_par *par = info->par;
1075 mutex_lock(&par->open_lock);
1076 if (!par->ref_count) {
1077 mutex_unlock(&par->open_lock);
1080 if (par->ref_count == 1) {
1081 par->riva.LockUnlock(&par->riva, 0);
1082 par->riva.LoadStateExt(&par->riva, &par->initial_state.ext);
1083 riva_load_state(par, &par->initial_state);
1085 restore_vga(&par->state);
1087 par->riva.LockUnlock(&par->riva, 1);
1090 mutex_unlock(&par->open_lock);
1095 static int rivafb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
1097 const struct fb_videomode *mode;
1098 struct riva_par *par = info->par;
1099 int nom, den; /* translating from pixels->bytes */
1103 switch (var->bits_per_pixel) {
1105 var->red.offset = var->green.offset = var->blue.offset = 0;
1106 var->red.length = var->green.length = var->blue.length = 8;
1107 var->bits_per_pixel = 8;
1111 var->green.length = 5;
1114 var->bits_per_pixel = 16;
1115 /* The Riva128 supports RGB555 only */
1116 if (par->riva.Architecture == NV_ARCH_03)
1117 var->green.length = 5;
1118 if (var->green.length == 5) {
1119 /* 0rrrrrgg gggbbbbb */
1120 var->red.offset = 10;
1121 var->green.offset = 5;
1122 var->blue.offset = 0;
1123 var->red.length = 5;
1124 var->green.length = 5;
1125 var->blue.length = 5;
1127 /* rrrrrggg gggbbbbb */
1128 var->red.offset = 11;
1129 var->green.offset = 5;
1130 var->blue.offset = 0;
1131 var->red.length = 5;
1132 var->green.length = 6;
1133 var->blue.length = 5;
1139 var->red.length = var->green.length = var->blue.length = 8;
1140 var->bits_per_pixel = 32;
1141 var->red.offset = 16;
1142 var->green.offset = 8;
1143 var->blue.offset = 0;
1149 "mode %dx%dx%d rejected...color depth not supported.\n",
1150 var->xres, var->yres, var->bits_per_pixel);
1151 NVTRACE("EXIT, returning -EINVAL\n");
1156 if (!info->monspecs.vfmax || !info->monspecs.hfmax ||
1157 !info->monspecs.dclkmax || !fb_validate_mode(var, info))
1161 /* calculate modeline if supported by monitor */
1162 if (!mode_valid && info->monspecs.gtf) {
1163 if (!fb_get_mode(FB_MAXTIMINGS, 0, var, info))
1168 mode = fb_find_best_mode(var, &info->modelist);
1170 riva_update_var(var, mode);
1175 if (!mode_valid && info->monspecs.modedb_len)
1178 if (var->xres_virtual < var->xres)
1179 var->xres_virtual = var->xres;
1180 if (var->yres_virtual <= var->yres)
1181 var->yres_virtual = -1;
1182 if (rivafb_do_maximize(info, var, nom, den) < 0)
1185 if (var->xoffset < 0)
1187 if (var->yoffset < 0)
1190 /* truncate xoffset and yoffset to maximum if too high */
1191 if (var->xoffset > var->xres_virtual - var->xres)
1192 var->xoffset = var->xres_virtual - var->xres - 1;
1194 if (var->yoffset > var->yres_virtual - var->yres)
1195 var->yoffset = var->yres_virtual - var->yres - 1;
1197 var->red.msb_right =
1198 var->green.msb_right =
1199 var->blue.msb_right =
1200 var->transp.offset = var->transp.length = var->transp.msb_right = 0;
1205 static int rivafb_set_par(struct fb_info *info)
1207 struct riva_par *par = info->par;
1211 /* vgaHWunlock() + riva unlock (0x7F) */
1212 CRTCout(par, 0x11, 0xFF);
1213 par->riva.LockUnlock(&par->riva, 0);
1214 rc = riva_load_video_mode(info);
1217 if(!(info->flags & FBINFO_HWACCEL_DISABLED))
1218 riva_setup_accel(info);
1220 par->cursor_reset = 1;
1221 info->fix.line_length = (info->var.xres_virtual * (info->var.bits_per_pixel >> 3));
1222 info->fix.visual = (info->var.bits_per_pixel == 8) ?
1223 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_DIRECTCOLOR;
1225 if (info->flags & FBINFO_HWACCEL_DISABLED)
1226 info->pixmap.scan_align = 1;
1228 info->pixmap.scan_align = 4;
1236 * rivafb_pan_display
1237 * @var: standard kernel fb changeable data
1239 * @info: pointer to fb_info object containing info for current riva board
1242 * Pan (or wrap, depending on the `vmode' field) the display using the
1243 * `xoffset' and `yoffset' fields of the `var' structure.
1244 * If the values don't fit, return -EINVAL.
1246 * This call looks only at xoffset, yoffset and the FB_VMODE_YWRAP flag
1248 static int rivafb_pan_display(struct fb_var_screeninfo *var,
1249 struct fb_info *info)
1251 struct riva_par *par = info->par;
1255 base = var->yoffset * info->fix.line_length + var->xoffset;
1256 par->riva.SetStartAddress(&par->riva, base);
1261 static int rivafb_blank(int blank, struct fb_info *info)
1263 struct riva_par *par= info->par;
1264 unsigned char tmp, vesa;
1266 tmp = SEQin(par, 0x01) & ~0x20; /* screen on/off */
1267 vesa = CRTCin(par, 0x1a) & ~0xc0; /* sync on/off */
1275 case FB_BLANK_UNBLANK:
1276 case FB_BLANK_NORMAL:
1278 case FB_BLANK_VSYNC_SUSPEND:
1281 case FB_BLANK_HSYNC_SUSPEND:
1284 case FB_BLANK_POWERDOWN:
1289 SEQout(par, 0x01, tmp);
1290 CRTCout(par, 0x1a, vesa);
1299 * @regno: register index
1300 * @red: red component
1301 * @green: green component
1302 * @blue: blue component
1303 * @transp: transparency
1304 * @info: pointer to fb_info object containing info for current riva board
1307 * Set a single color register. The values supplied have a 16 bit
1311 * Return != 0 for invalid regno.
1314 * fbcmap.c:fb_set_cmap()
1316 static int rivafb_setcolreg(unsigned regno, unsigned red, unsigned green,
1317 unsigned blue, unsigned transp,
1318 struct fb_info *info)
1320 struct riva_par *par = info->par;
1321 RIVA_HW_INST *chip = &par->riva;
1324 if (regno >= riva_get_cmap_len(&info->var))
1327 if (info->var.grayscale) {
1328 /* gray = 0.30*R + 0.59*G + 0.11*B */
1329 red = green = blue =
1330 (red * 77 + green * 151 + blue * 28) >> 8;
1333 if (regno < 16 && info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
1334 ((u32 *) info->pseudo_palette)[regno] =
1335 (regno << info->var.red.offset) |
1336 (regno << info->var.green.offset) |
1337 (regno << info->var.blue.offset);
1339 * The Riva128 2D engine requires color information in
1340 * TrueColor format even if framebuffer is in DirectColor
1342 if (par->riva.Architecture == NV_ARCH_03) {
1343 switch (info->var.bits_per_pixel) {
1345 par->palette[regno] = ((red & 0xf800) >> 1) |
1346 ((green & 0xf800) >> 6) |
1347 ((blue & 0xf800) >> 11);
1350 par->palette[regno] = ((red & 0xff00) << 8) |
1351 ((green & 0xff00)) |
1352 ((blue & 0xff00) >> 8);
1358 switch (info->var.bits_per_pixel) {
1360 /* "transparent" stuff is completely ignored. */
1361 riva_wclut(chip, regno, red >> 8, green >> 8, blue >> 8);
1364 if (info->var.green.length == 5) {
1365 for (i = 0; i < 8; i++) {
1366 riva_wclut(chip, regno*8+i, red >> 8,
1367 green >> 8, blue >> 8);
1373 for (i = 0; i < 8; i++) {
1374 riva_wclut(chip, regno*8+i,
1375 red >> 8, green >> 8,
1379 riva_rclut(chip, regno*4, &r, &g, &b);
1380 for (i = 0; i < 4; i++)
1381 riva_wclut(chip, regno*4+i, r,
1386 riva_wclut(chip, regno, red >> 8, green >> 8, blue >> 8);
1396 * rivafb_fillrect - hardware accelerated color fill function
1397 * @info: pointer to fb_info structure
1398 * @rect: pointer to fb_fillrect structure
1401 * This function fills up a region of framebuffer memory with a solid
1402 * color with a choice of two different ROP's, copy or invert.
1407 static void rivafb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
1409 struct riva_par *par = info->par;
1410 u_int color, rop = 0;
1412 if ((info->flags & FBINFO_HWACCEL_DISABLED)) {
1413 cfb_fillrect(info, rect);
1417 if (info->var.bits_per_pixel == 8)
1418 color = rect->color;
1420 if (par->riva.Architecture != NV_ARCH_03)
1421 color = ((u32 *)info->pseudo_palette)[rect->color];
1423 color = par->palette[rect->color];
1426 switch (rect->rop) {
1436 riva_set_rop_solid(par, rop);
1438 RIVA_FIFO_FREE(par->riva, Bitmap, 1);
1439 NV_WR32(&par->riva.Bitmap->Color1A, 0, color);
1441 RIVA_FIFO_FREE(par->riva, Bitmap, 2);
1442 NV_WR32(&par->riva.Bitmap->UnclippedRectangle[0].TopLeft, 0,
1443 (rect->dx << 16) | rect->dy);
1445 NV_WR32(&par->riva.Bitmap->UnclippedRectangle[0].WidthHeight, 0,
1446 (rect->width << 16) | rect->height);
1448 riva_set_rop_solid(par, 0xcc);
1453 * rivafb_copyarea - hardware accelerated blit function
1454 * @info: pointer to fb_info structure
1455 * @region: pointer to fb_copyarea structure
1458 * This copies an area of pixels from one location to another
1463 static void rivafb_copyarea(struct fb_info *info, const struct fb_copyarea *region)
1465 struct riva_par *par = info->par;
1467 if ((info->flags & FBINFO_HWACCEL_DISABLED)) {
1468 cfb_copyarea(info, region);
1472 RIVA_FIFO_FREE(par->riva, Blt, 3);
1473 NV_WR32(&par->riva.Blt->TopLeftSrc, 0,
1474 (region->sy << 16) | region->sx);
1475 NV_WR32(&par->riva.Blt->TopLeftDst, 0,
1476 (region->dy << 16) | region->dx);
1478 NV_WR32(&par->riva.Blt->WidthHeight, 0,
1479 (region->height << 16) | region->width);
1483 static inline void convert_bgcolor_16(u32 *col)
1485 *col = ((*col & 0x0000F800) << 8)
1486 | ((*col & 0x00007E0) << 5)
1487 | ((*col & 0x0000001F) << 3)
1493 * rivafb_imageblit: hardware accelerated color expand function
1494 * @info: pointer to fb_info structure
1495 * @image: pointer to fb_image structure
1498 * If the source is a monochrome bitmap, the function fills up a a region
1499 * of framebuffer memory with pixels whose color is determined by the bit
1500 * setting of the bitmap, 1 - foreground, 0 - background.
1502 * If the source is not a monochrome bitmap, color expansion is not done.
1503 * In this case, it is channeled to a software function.
1508 static void rivafb_imageblit(struct fb_info *info,
1509 const struct fb_image *image)
1511 struct riva_par *par = info->par;
1512 u32 fgx = 0, bgx = 0, width, tmp;
1513 u8 *cdat = (u8 *) image->data;
1514 volatile u32 __iomem *d;
1517 if ((info->flags & FBINFO_HWACCEL_DISABLED) || image->depth != 1) {
1518 cfb_imageblit(info, image);
1522 switch (info->var.bits_per_pixel) {
1524 fgx = image->fg_color;
1525 bgx = image->bg_color;
1529 if (par->riva.Architecture != NV_ARCH_03) {
1530 fgx = ((u32 *)info->pseudo_palette)[image->fg_color];
1531 bgx = ((u32 *)info->pseudo_palette)[image->bg_color];
1533 fgx = par->palette[image->fg_color];
1534 bgx = par->palette[image->bg_color];
1536 if (info->var.green.length == 6)
1537 convert_bgcolor_16(&bgx);
1541 RIVA_FIFO_FREE(par->riva, Bitmap, 7);
1542 NV_WR32(&par->riva.Bitmap->ClipE.TopLeft, 0,
1543 (image->dy << 16) | (image->dx & 0xFFFF));
1544 NV_WR32(&par->riva.Bitmap->ClipE.BottomRight, 0,
1545 (((image->dy + image->height) << 16) |
1546 ((image->dx + image->width) & 0xffff)));
1547 NV_WR32(&par->riva.Bitmap->Color0E, 0, bgx);
1548 NV_WR32(&par->riva.Bitmap->Color1E, 0, fgx);
1549 NV_WR32(&par->riva.Bitmap->WidthHeightInE, 0,
1550 (image->height << 16) | ((image->width + 31) & ~31));
1551 NV_WR32(&par->riva.Bitmap->WidthHeightOutE, 0,
1552 (image->height << 16) | ((image->width + 31) & ~31));
1553 NV_WR32(&par->riva.Bitmap->PointE, 0,
1554 (image->dy << 16) | (image->dx & 0xFFFF));
1556 d = &par->riva.Bitmap->MonochromeData01E;
1558 width = (image->width + 31)/32;
1559 size = width * image->height;
1560 while (size >= 16) {
1561 RIVA_FIFO_FREE(par->riva, Bitmap, 16);
1562 for (i = 0; i < 16; i++) {
1563 tmp = *((u32 *)cdat);
1564 cdat = (u8 *)((u32 *)cdat + 1);
1565 reverse_order(&tmp);
1566 NV_WR32(d, i*4, tmp);
1571 RIVA_FIFO_FREE(par->riva, Bitmap, size);
1572 for (i = 0; i < size; i++) {
1573 tmp = *((u32 *) cdat);
1574 cdat = (u8 *)((u32 *)cdat + 1);
1575 reverse_order(&tmp);
1576 NV_WR32(d, i*4, tmp);
1582 * rivafb_cursor - hardware cursor function
1583 * @info: pointer to info structure
1584 * @cursor: pointer to fbcursor structure
1587 * A cursor function that supports displaying a cursor image via hardware.
1588 * Within the kernel, copy and invert rops are supported. If exported
1589 * to user space, only the copy rop will be supported.
1594 static int rivafb_cursor(struct fb_info *info, struct fb_cursor *cursor)
1596 struct riva_par *par = info->par;
1597 u8 data[MAX_CURS * MAX_CURS/8];
1598 int i, set = cursor->set;
1601 if (cursor->image.width > MAX_CURS || cursor->image.height > MAX_CURS)
1604 par->riva.ShowHideCursor(&par->riva, 0);
1606 if (par->cursor_reset) {
1607 set = FB_CUR_SETALL;
1608 par->cursor_reset = 0;
1611 if (set & FB_CUR_SETSIZE)
1612 memset_io(par->riva.CURSOR, 0, MAX_CURS * MAX_CURS * 2);
1614 if (set & FB_CUR_SETPOS) {
1617 yy = cursor->image.dy - info->var.yoffset;
1618 xx = cursor->image.dx - info->var.xoffset;
1622 NV_WR32(par->riva.PRAMDAC, 0x0000300, temp);
1626 if (set & (FB_CUR_SETSHAPE | FB_CUR_SETCMAP | FB_CUR_SETIMAGE)) {
1627 u32 bg_idx = cursor->image.bg_color;
1628 u32 fg_idx = cursor->image.fg_color;
1629 u32 s_pitch = (cursor->image.width+7) >> 3;
1630 u32 d_pitch = MAX_CURS/8;
1631 u8 *dat = (u8 *) cursor->image.data;
1632 u8 *msk = (u8 *) cursor->mask;
1635 src = kmalloc(s_pitch * cursor->image.height, GFP_ATOMIC);
1638 switch (cursor->rop) {
1640 for (i = 0; i < s_pitch * cursor->image.height; i++)
1641 src[i] = dat[i] ^ msk[i];
1645 for (i = 0; i < s_pitch * cursor->image.height; i++)
1646 src[i] = dat[i] & msk[i];
1650 fb_pad_aligned_buffer(data, d_pitch, src, s_pitch,
1651 cursor->image.height);
1653 bg = ((info->cmap.red[bg_idx] & 0xf8) << 7) |
1654 ((info->cmap.green[bg_idx] & 0xf8) << 2) |
1655 ((info->cmap.blue[bg_idx] & 0xf8) >> 3) |
1658 fg = ((info->cmap.red[fg_idx] & 0xf8) << 7) |
1659 ((info->cmap.green[fg_idx] & 0xf8) << 2) |
1660 ((info->cmap.blue[fg_idx] & 0xf8) >> 3) |
1663 par->riva.LockUnlock(&par->riva, 0);
1665 rivafb_load_cursor_image(par, data, bg, fg,
1666 cursor->image.width,
1667 cursor->image.height);
1673 par->riva.ShowHideCursor(&par->riva, 1);
1678 static int rivafb_sync(struct fb_info *info)
1680 struct riva_par *par = info->par;
1686 /* ------------------------------------------------------------------------- *
1688 * initialization helper functions
1690 * ------------------------------------------------------------------------- */
1692 /* kernel interface */
1693 static struct fb_ops riva_fb_ops = {
1694 .owner = THIS_MODULE,
1695 .fb_open = rivafb_open,
1696 .fb_release = rivafb_release,
1697 .fb_check_var = rivafb_check_var,
1698 .fb_set_par = rivafb_set_par,
1699 .fb_setcolreg = rivafb_setcolreg,
1700 .fb_pan_display = rivafb_pan_display,
1701 .fb_blank = rivafb_blank,
1702 .fb_fillrect = rivafb_fillrect,
1703 .fb_copyarea = rivafb_copyarea,
1704 .fb_imageblit = rivafb_imageblit,
1705 .fb_cursor = rivafb_cursor,
1706 .fb_sync = rivafb_sync,
1709 static int __devinit riva_set_fbinfo(struct fb_info *info)
1711 unsigned int cmap_len;
1712 struct riva_par *par = info->par;
1715 info->flags = FBINFO_DEFAULT
1716 | FBINFO_HWACCEL_XPAN
1717 | FBINFO_HWACCEL_YPAN
1718 | FBINFO_HWACCEL_COPYAREA
1719 | FBINFO_HWACCEL_FILLRECT
1720 | FBINFO_HWACCEL_IMAGEBLIT;
1722 /* Accel seems to not work properly on NV30 yet...*/
1723 if ((par->riva.Architecture == NV_ARCH_30) || noaccel) {
1724 printk(KERN_DEBUG PFX "disabling acceleration\n");
1725 info->flags |= FBINFO_HWACCEL_DISABLED;
1728 info->var = rivafb_default_var;
1729 info->fix.visual = (info->var.bits_per_pixel == 8) ?
1730 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_DIRECTCOLOR;
1732 info->pseudo_palette = par->pseudo_palette;
1734 cmap_len = riva_get_cmap_len(&info->var);
1735 fb_alloc_cmap(&info->cmap, cmap_len, 0);
1737 info->pixmap.size = 8 * 1024;
1738 info->pixmap.buf_align = 4;
1739 info->pixmap.access_align = 32;
1740 info->pixmap.flags = FB_PIXMAP_SYSTEM;
1741 info->var.yres_virtual = -1;
1743 return (rivafb_check_var(&info->var, info));
1746 #ifdef CONFIG_PPC_OF
1747 static int __devinit riva_get_EDID_OF(struct fb_info *info, struct pci_dev *pd)
1749 struct riva_par *par = info->par;
1750 struct device_node *dp;
1751 const unsigned char *pedid = NULL;
1752 const unsigned char *disptype = NULL;
1753 static char *propnames[] = {
1754 "DFP,EDID", "LCD,EDID", "EDID", "EDID1", "EDID,B", "EDID,A", NULL };
1758 dp = pci_device_to_OF_node(pd);
1759 for (; dp != NULL; dp = dp->child) {
1760 disptype = get_property(dp, "display-type", NULL);
1761 if (disptype == NULL)
1763 if (strncmp(disptype, "LCD", 3) != 0)
1765 for (i = 0; propnames[i] != NULL; ++i) {
1766 pedid = get_property(dp, propnames[i], NULL);
1767 if (pedid != NULL) {
1768 par->EDID = (unsigned char *)pedid;
1769 NVTRACE("LCD found.\n");
1777 #endif /* CONFIG_PPC_OF */
1779 #if defined(CONFIG_FB_RIVA_I2C) && !defined(CONFIG_PPC_OF)
1780 static int __devinit riva_get_EDID_i2c(struct fb_info *info)
1782 struct riva_par *par = info->par;
1783 struct fb_var_screeninfo var;
1787 riva_create_i2c_busses(par);
1788 for (i = 0; i < par->bus; i++) {
1789 riva_probe_i2c_connector(par, i+1, &par->EDID);
1790 if (par->EDID && !fb_parse_edid(par->EDID, &var)) {
1791 printk(PFX "Found EDID Block from BUS %i\n", i);
1797 return (par->EDID) ? 1 : 0;
1799 #endif /* CONFIG_FB_RIVA_I2C */
1801 static void __devinit riva_update_default_var(struct fb_var_screeninfo *var,
1802 struct fb_info *info)
1804 struct fb_monspecs *specs = &info->monspecs;
1805 struct fb_videomode modedb;
1808 /* respect mode options */
1810 fb_find_mode(var, info, mode_option,
1811 specs->modedb, specs->modedb_len,
1813 } else if (specs->modedb != NULL) {
1814 /* get preferred timing */
1815 if (info->monspecs.misc & FB_MISC_1ST_DETAIL) {
1818 for (i = 0; i < specs->modedb_len; i++) {
1819 if (specs->modedb[i].flag & FB_MODE_IS_FIRST) {
1820 modedb = specs->modedb[i];
1825 /* otherwise, get first mode in database */
1826 modedb = specs->modedb[0];
1828 var->bits_per_pixel = 8;
1829 riva_update_var(var, &modedb);
1835 static void __devinit riva_get_EDID(struct fb_info *info, struct pci_dev *pdev)
1838 #ifdef CONFIG_PPC_OF
1839 if (!riva_get_EDID_OF(info, pdev))
1840 printk(PFX "could not retrieve EDID from OF\n");
1841 #elif defined(CONFIG_FB_RIVA_I2C)
1842 if (!riva_get_EDID_i2c(info))
1843 printk(PFX "could not retrieve EDID from DDC/I2C\n");
1849 static void __devinit riva_get_edidinfo(struct fb_info *info)
1851 struct fb_var_screeninfo *var = &rivafb_default_var;
1852 struct riva_par *par = info->par;
1854 fb_edid_to_monspecs(par->EDID, &info->monspecs);
1855 fb_videomode_to_modelist(info->monspecs.modedb, info->monspecs.modedb_len,
1857 riva_update_default_var(var, info);
1859 /* if user specified flatpanel, we respect that */
1860 if (info->monspecs.input & FB_DISP_DDI)
1864 /* ------------------------------------------------------------------------- *
1868 * ------------------------------------------------------------------------- */
1870 static u32 __devinit riva_get_arch(struct pci_dev *pd)
1874 switch (pd->device & 0x0ff0) {
1875 case 0x0100: /* GeForce 256 */
1876 case 0x0110: /* GeForce2 MX */
1877 case 0x0150: /* GeForce2 */
1878 case 0x0170: /* GeForce4 MX */
1879 case 0x0180: /* GeForce4 MX (8x AGP) */
1880 case 0x01A0: /* nForce */
1881 case 0x01F0: /* nForce2 */
1884 case 0x0200: /* GeForce3 */
1885 case 0x0250: /* GeForce4 Ti */
1886 case 0x0280: /* GeForce4 Ti (8x AGP) */
1889 case 0x0300: /* GeForceFX 5800 */
1890 case 0x0310: /* GeForceFX 5600 */
1891 case 0x0320: /* GeForceFX 5200 */
1892 case 0x0330: /* GeForceFX 5900 */
1893 case 0x0340: /* GeForceFX 5700 */
1896 case 0x0020: /* TNT, TNT2 */
1899 case 0x0010: /* Riva128 */
1902 default: /* unknown architecture */
1908 static int __devinit rivafb_probe(struct pci_dev *pd,
1909 const struct pci_device_id *ent)
1911 struct riva_par *default_par;
1912 struct fb_info *info;
1918 info = framebuffer_alloc(sizeof(struct riva_par), &pd->dev);
1920 printk (KERN_ERR PFX "could not allocate memory\n");
1924 default_par = info->par;
1925 default_par->pdev = pd;
1927 info->pixmap.addr = kzalloc(8 * 1024, GFP_KERNEL);
1928 if (info->pixmap.addr == NULL) {
1930 goto err_framebuffer_release;
1933 ret = pci_enable_device(pd);
1935 printk(KERN_ERR PFX "cannot enable PCI device\n");
1936 goto err_free_pixmap;
1939 ret = pci_request_regions(pd, "rivafb");
1941 printk(KERN_ERR PFX "cannot request PCI regions\n");
1942 goto err_disable_device;
1945 mutex_init(&default_par->open_lock);
1946 default_par->riva.Architecture = riva_get_arch(pd);
1948 default_par->Chipset = (pd->vendor << 16) | pd->device;
1949 printk(KERN_INFO PFX "nVidia device/chipset %X\n",default_par->Chipset);
1951 if(default_par->riva.Architecture == 0) {
1952 printk(KERN_ERR PFX "unknown NV_ARCH\n");
1954 goto err_release_region;
1956 if(default_par->riva.Architecture == NV_ARCH_10 ||
1957 default_par->riva.Architecture == NV_ARCH_20 ||
1958 default_par->riva.Architecture == NV_ARCH_30) {
1959 sprintf(rivafb_fix.id, "NV%x", (pd->device & 0x0ff0) >> 4);
1961 sprintf(rivafb_fix.id, "NV%x", default_par->riva.Architecture);
1964 default_par->FlatPanel = flatpanel;
1966 printk(KERN_INFO PFX "flatpanel support enabled\n");
1967 default_par->forceCRTC = forceCRTC;
1969 rivafb_fix.mmio_len = pci_resource_len(pd, 0);
1970 rivafb_fix.smem_len = pci_resource_len(pd, 1);
1973 /* enable IO and mem if not already done */
1976 pci_read_config_word(pd, PCI_COMMAND, &cmd);
1977 cmd |= (PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
1978 pci_write_config_word(pd, PCI_COMMAND, cmd);
1981 rivafb_fix.mmio_start = pci_resource_start(pd, 0);
1982 rivafb_fix.smem_start = pci_resource_start(pd, 1);
1984 default_par->ctrl_base = ioremap(rivafb_fix.mmio_start,
1985 rivafb_fix.mmio_len);
1986 if (!default_par->ctrl_base) {
1987 printk(KERN_ERR PFX "cannot ioremap MMIO base\n");
1989 goto err_release_region;
1992 switch (default_par->riva.Architecture) {
1994 /* Riva128's PRAMIN is in the "framebuffer" space
1995 * Since these cards were never made with more than 8 megabytes
1996 * we can safely allocate this separately.
1998 default_par->riva.PRAMIN = ioremap(rivafb_fix.smem_start + 0x00C00000, 0x00008000);
1999 if (!default_par->riva.PRAMIN) {
2000 printk(KERN_ERR PFX "cannot ioremap PRAMIN region\n");
2002 goto err_iounmap_ctrl_base;
2009 default_par->riva.PCRTC0 =
2010 (u32 __iomem *)(default_par->ctrl_base + 0x00600000);
2011 default_par->riva.PRAMIN =
2012 (u32 __iomem *)(default_par->ctrl_base + 0x00710000);
2015 riva_common_setup(default_par);
2017 if (default_par->riva.Architecture == NV_ARCH_03) {
2018 default_par->riva.PCRTC = default_par->riva.PCRTC0
2019 = default_par->riva.PGRAPH;
2022 rivafb_fix.smem_len = riva_get_memlen(default_par) * 1024;
2023 default_par->dclk_max = riva_get_maxdclk(default_par) * 1000;
2024 info->screen_base = ioremap(rivafb_fix.smem_start,
2025 rivafb_fix.smem_len);
2026 if (!info->screen_base) {
2027 printk(KERN_ERR PFX "cannot ioremap FB base\n");
2029 goto err_iounmap_pramin;
2034 default_par->mtrr.vram = mtrr_add(rivafb_fix.smem_start,
2035 rivafb_fix.smem_len,
2036 MTRR_TYPE_WRCOMB, 1);
2037 if (default_par->mtrr.vram < 0) {
2038 printk(KERN_ERR PFX "unable to setup MTRR\n");
2040 default_par->mtrr.vram_valid = 1;
2041 /* let there be speed */
2042 printk(KERN_INFO PFX "RIVA MTRR set to ON\n");
2045 #endif /* CONFIG_MTRR */
2047 info->fbops = &riva_fb_ops;
2048 info->fix = rivafb_fix;
2049 riva_get_EDID(info, pd);
2050 riva_get_edidinfo(info);
2052 ret=riva_set_fbinfo(info);
2054 printk(KERN_ERR PFX "error setting initial video mode\n");
2055 goto err_iounmap_screen_base;
2058 fb_destroy_modedb(info->monspecs.modedb);
2059 info->monspecs.modedb = NULL;
2061 pci_set_drvdata(pd, info);
2062 riva_bl_init(info->par);
2063 ret = register_framebuffer(info);
2066 "error registering riva framebuffer\n");
2067 goto err_iounmap_screen_base;
2070 printk(KERN_INFO PFX
2071 "PCI nVidia %s framebuffer ver %s (%dMB @ 0x%lX)\n",
2074 info->fix.smem_len / (1024 * 1024),
2075 info->fix.smem_start);
2080 err_iounmap_screen_base:
2081 #ifdef CONFIG_FB_RIVA_I2C
2082 riva_delete_i2c_busses(info->par);
2084 iounmap(info->screen_base);
2086 if (default_par->riva.Architecture == NV_ARCH_03)
2087 iounmap(default_par->riva.PRAMIN);
2088 err_iounmap_ctrl_base:
2089 iounmap(default_par->ctrl_base);
2091 pci_release_regions(pd);
2094 kfree(info->pixmap.addr);
2095 err_framebuffer_release:
2096 framebuffer_release(info);
2101 static void __exit rivafb_remove(struct pci_dev *pd)
2103 struct fb_info *info = pci_get_drvdata(pd);
2104 struct riva_par *par = info->par;
2108 #ifdef CONFIG_FB_RIVA_I2C
2109 riva_delete_i2c_busses(par);
2113 unregister_framebuffer(info);
2118 if (par->mtrr.vram_valid)
2119 mtrr_del(par->mtrr.vram, info->fix.smem_start,
2120 info->fix.smem_len);
2121 #endif /* CONFIG_MTRR */
2123 iounmap(par->ctrl_base);
2124 iounmap(info->screen_base);
2125 if (par->riva.Architecture == NV_ARCH_03)
2126 iounmap(par->riva.PRAMIN);
2127 pci_release_regions(pd);
2128 kfree(info->pixmap.addr);
2129 framebuffer_release(info);
2130 pci_set_drvdata(pd, NULL);
2134 /* ------------------------------------------------------------------------- *
2138 * ------------------------------------------------------------------------- */
2141 static int __init rivafb_setup(char *options)
2146 if (!options || !*options)
2149 while ((this_opt = strsep(&options, ",")) != NULL) {
2150 if (!strncmp(this_opt, "forceCRTC", 9)) {
2154 if (!*p || !*(++p)) continue;
2155 forceCRTC = *p - '0';
2156 if (forceCRTC < 0 || forceCRTC > 1)
2158 } else if (!strncmp(this_opt, "flatpanel", 9)) {
2161 } else if (!strncmp(this_opt, "nomtrr", 6)) {
2164 } else if (!strncmp(this_opt, "strictmode", 10)) {
2166 } else if (!strncmp(this_opt, "noaccel", 7)) {
2169 mode_option = this_opt;
2174 #endif /* !MODULE */
2176 static struct pci_driver rivafb_driver = {
2178 .id_table = rivafb_pci_tbl,
2179 .probe = rivafb_probe,
2180 .remove = __exit_p(rivafb_remove),
2185 /* ------------------------------------------------------------------------- *
2189 * ------------------------------------------------------------------------- */
2191 static int __devinit rivafb_init(void)
2194 char *option = NULL;
2196 if (fb_get_options("rivafb", &option))
2198 rivafb_setup(option);
2200 return pci_register_driver(&rivafb_driver);
2204 module_init(rivafb_init);
2207 static void __exit rivafb_exit(void)
2209 pci_unregister_driver(&rivafb_driver);
2212 module_exit(rivafb_exit);
2215 module_param(noaccel, bool, 0);
2216 MODULE_PARM_DESC(noaccel, "bool: disable acceleration");
2217 module_param(flatpanel, int, 0);
2218 MODULE_PARM_DESC(flatpanel, "Enables experimental flat panel support for some chipsets. (0 or 1=enabled) (default=0)");
2219 module_param(forceCRTC, int, 0);
2220 MODULE_PARM_DESC(forceCRTC, "Forces usage of a particular CRTC in case autodetection fails. (0 or 1) (default=autodetect)");
2222 module_param(nomtrr, bool, 0);
2223 MODULE_PARM_DESC(nomtrr, "Disables MTRR support (0 or 1=disabled) (default=0)");
2225 module_param(strictmode, bool, 0);
2226 MODULE_PARM_DESC(strictmode, "Only use video modes from EDID");
2228 MODULE_AUTHOR("Ani Joshi, maintainer");
2229 MODULE_DESCRIPTION("Framebuffer driver for nVidia Riva 128, TNT, TNT2, and the GeForce series");
2230 MODULE_LICENSE("GPL");