]> err.no Git - linux-2.6/blob - drivers/video/riva/fbdev.c
Merge master.kernel.org:/pub/scm/linux/kernel/git/lethal/sh-2.6
[linux-2.6] / drivers / video / riva / fbdev.c
1 /*
2  * linux/drivers/video/riva/fbdev.c - nVidia RIVA 128/TNT/TNT2 fb driver
3  *
4  * Maintained by Ani Joshi <ajoshi@shell.unixbox.com>
5  *
6  * Copyright 1999-2000 Jeff Garzik
7  *
8  * Contributors:
9  *
10  *      Ani Joshi:  Lots of debugging and cleanup work, really helped
11  *      get the driver going
12  *
13  *      Ferenc Bakonyi:  Bug fixes, cleanup, modularization
14  *
15  *      Jindrich Makovicka:  Accel code help, hw cursor, mtrr
16  *
17  *      Paul Richards:  Bug fixes, updates
18  *
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.
22  *
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
25  * for more details.
26  *
27  * Known bugs and issues:
28  *      restoring text mode fails
29  *      doublescan modes are broken
30  */
31
32 #include <linux/module.h>
33 #include <linux/kernel.h>
34 #include <linux/errno.h>
35 #include <linux/string.h>
36 #include <linux/mm.h>
37 #include <linux/slab.h>
38 #include <linux/delay.h>
39 #include <linux/fb.h>
40 #include <linux/init.h>
41 #include <linux/pci.h>
42 #include <linux/backlight.h>
43 #include <linux/bitrev.h>
44 #ifdef CONFIG_MTRR
45 #include <asm/mtrr.h>
46 #endif
47 #ifdef CONFIG_PPC_OF
48 #include <asm/prom.h>
49 #include <asm/pci-bridge.h>
50 #endif
51 #ifdef CONFIG_PMAC_BACKLIGHT
52 #include <asm/machdep.h>
53 #include <asm/backlight.h>
54 #endif
55
56 #include "rivafb.h"
57 #include "nvreg.h"
58
59 #ifndef CONFIG_PCI              /* sanity check */
60 #error This driver requires PCI support.
61 #endif
62
63 /* version number of this driver */
64 #define RIVAFB_VERSION "0.9.5b"
65
66 /* ------------------------------------------------------------------------- *
67  *
68  * various helpful macros and constants
69  *
70  * ------------------------------------------------------------------------- */
71 #ifdef CONFIG_FB_RIVA_DEBUG
72 #define NVTRACE          printk
73 #else
74 #define NVTRACE          if(0) printk
75 #endif
76
77 #define NVTRACE_ENTER(...)  NVTRACE("%s START\n", __FUNCTION__)
78 #define NVTRACE_LEAVE(...)  NVTRACE("%s END\n", __FUNCTION__)
79
80 #ifdef CONFIG_FB_RIVA_DEBUG
81 #define assert(expr) \
82         if(!(expr)) { \
83         printk( "Assertion failed! %s,%s,%s,line=%d\n",\
84         #expr,__FILE__,__FUNCTION__,__LINE__); \
85         BUG(); \
86         }
87 #else
88 #define assert(expr)
89 #endif
90
91 #define PFX "rivafb: "
92
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)
97
98 /* HW cursor parameters */
99 #define MAX_CURS                32
100
101 /* ------------------------------------------------------------------------- *
102  *
103  * prototypes
104  *
105  * ------------------------------------------------------------------------- */
106
107 static int rivafb_blank(int blank, struct fb_info *info);
108
109 /* ------------------------------------------------------------------------- *
110  *
111  * card identification
112  *
113  * ------------------------------------------------------------------------- */
114
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 */
202 };
203 MODULE_DEVICE_TABLE(pci, rivafb_pci_tbl);
204
205 /* ------------------------------------------------------------------------- *
206  *
207  * global variables
208  *
209  * ------------------------------------------------------------------------- */
210
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;
215 #ifdef CONFIG_MTRR
216 static int nomtrr __devinitdata = 0;
217 #endif
218 #ifdef CONFIG_PMAC_BACKLIGHT
219 static int backlight __devinitdata = 1;
220 #else
221 static int backlight __devinitdata = 0;
222 #endif
223
224 static char *mode_option __devinitdata = NULL;
225 static int  strictmode       = 0;
226
227 static struct fb_fix_screeninfo __devinitdata rivafb_fix = {
228         .type           = FB_TYPE_PACKED_PIXELS,
229         .xpanstep       = 1,
230         .ypanstep       = 1,
231 };
232
233 static struct fb_var_screeninfo __devinitdata rivafb_default_var = {
234         .xres           = 640,
235         .yres           = 480,
236         .xres_virtual   = 640,
237         .yres_virtual   = 480,
238         .bits_per_pixel = 8,
239         .red            = {0, 8, 0},
240         .green          = {0, 8, 0},
241         .blue           = {0, 8, 0},
242         .transp         = {0, 0, 0},
243         .activate       = FB_ACTIVATE_NOW,
244         .height         = -1,
245         .width          = -1,
246         .pixclock       = 39721,
247         .left_margin    = 40,
248         .right_margin   = 24,
249         .upper_margin   = 32,
250         .lower_margin   = 11,
251         .hsync_len      = 96,
252         .vsync_len      = 2,
253         .vmode          = FB_VMODE_NONINTERLACED
254 };
255
256 /* from GGI */
257 static const struct riva_regs reg_template = {
258         {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,        /* ATTR */
259          0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
260          0x41, 0x01, 0x0F, 0x00, 0x00},
261         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        /* CRT  */
262          0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
263          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE3,        /* 0x10 */
264          0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
265          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        /* 0x20 */
266          0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
267          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        /* 0x30 */
268          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
269          0x00,                                                  /* 0x40 */
270          },
271         {0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x05, 0x0F,        /* GRA  */
272          0xFF},
273         {0x03, 0x01, 0x0F, 0x00, 0x0E},                         /* SEQ  */
274         0xEB                                                    /* MISC */
275 };
276
277 /*
278  * Backlight control
279  */
280 #ifdef CONFIG_FB_RIVA_BACKLIGHT
281 /* We do not have any information about which values are allowed, thus
282  * we used safe values.
283  */
284 #define MIN_LEVEL 0x158
285 #define MAX_LEVEL 0x534
286 #define LEVEL_STEP ((MAX_LEVEL - MIN_LEVEL) / FB_BACKLIGHT_MAX)
287
288 static struct backlight_properties riva_bl_data;
289
290 static int riva_bl_get_level_brightness(struct riva_par *par,
291                 int level)
292 {
293         struct fb_info *info = pci_get_drvdata(par->pdev);
294         int nlevel;
295
296         /* Get and convert the value */
297         /* No locking on bl_curve since accessing a single value */
298         nlevel = MIN_LEVEL + info->bl_curve[level] * LEVEL_STEP;
299
300         if (nlevel < 0)
301                 nlevel = 0;
302         else if (nlevel < MIN_LEVEL)
303                 nlevel = MIN_LEVEL;
304         else if (nlevel > MAX_LEVEL)
305                 nlevel = MAX_LEVEL;
306
307         return nlevel;
308 }
309
310 static int riva_bl_update_status(struct backlight_device *bd)
311 {
312         struct riva_par *par = class_get_devdata(&bd->class_dev);
313         U032 tmp_pcrt, tmp_pmc;
314         int level;
315
316         if (bd->props.power != FB_BLANK_UNBLANK ||
317             bd->props.fb_blank != FB_BLANK_UNBLANK)
318                 level = 0;
319         else
320                 level = bd->props.brightness;
321
322         tmp_pmc = par->riva.PMC[0x10F0/4] & 0x0000FFFF;
323         tmp_pcrt = par->riva.PCRTC0[0x081C/4] & 0xFFFFFFFC;
324         if(level > 0) {
325                 tmp_pcrt |= 0x1;
326                 tmp_pmc |= (1 << 31); /* backlight bit */
327                 tmp_pmc |= riva_bl_get_level_brightness(par, level) << 16; /* level */
328         }
329         par->riva.PCRTC0[0x081C/4] = tmp_pcrt;
330         par->riva.PMC[0x10F0/4] = tmp_pmc;
331
332         return 0;
333 }
334
335 static int riva_bl_get_brightness(struct backlight_device *bd)
336 {
337         return bd->props.brightness;
338 }
339
340 static struct backlight_ops riva_bl_ops = {
341         .get_brightness = riva_bl_get_brightness,
342         .update_status  = riva_bl_update_status,
343 };
344
345 static void riva_bl_init(struct riva_par *par)
346 {
347         struct fb_info *info = pci_get_drvdata(par->pdev);
348         struct backlight_device *bd;
349         char name[12];
350
351         if (!par->FlatPanel)
352                 return;
353
354 #ifdef CONFIG_PMAC_BACKLIGHT
355         if (!machine_is(powermac) ||
356             !pmac_has_backlight_type("mnca"))
357                 return;
358 #endif
359
360         snprintf(name, sizeof(name), "rivabl%d", info->node);
361
362         bd = backlight_device_register(name, info->dev, par, &riva_bl_ops);
363         if (IS_ERR(bd)) {
364                 info->bl_dev = NULL;
365                 printk(KERN_WARNING "riva: Backlight registration failed\n");
366                 goto error;
367         }
368
369         info->bl_dev = bd;
370         fb_bl_default_curve(info, 0,
371                 MIN_LEVEL * FB_BACKLIGHT_MAX / MAX_LEVEL,
372                 FB_BACKLIGHT_MAX);
373
374         bd->props.max_brightness = FB_BACKLIGHT_LEVELS - 1;
375         bd->props.brightness = riva_bl_data.max_brightness;
376         bd->props.power = FB_BLANK_UNBLANK;
377         backlight_update_status(bd);
378
379         printk("riva: Backlight initialized (%s)\n", name);
380
381         return;
382
383 error:
384         return;
385 }
386
387 static void riva_bl_exit(struct fb_info *info)
388 {
389         struct backlight_device *bd = info->bl_dev;
390
391         backlight_device_unregister(bd);
392         printk("riva: Backlight unloaded\n");
393 }
394 #else
395 static inline void riva_bl_init(struct riva_par *par) {}
396 static inline void riva_bl_exit(struct fb_info *info) {}
397 #endif /* CONFIG_FB_RIVA_BACKLIGHT */
398
399 /* ------------------------------------------------------------------------- *
400  *
401  * MMIO access macros
402  *
403  * ------------------------------------------------------------------------- */
404
405 static inline void CRTCout(struct riva_par *par, unsigned char index,
406                            unsigned char val)
407 {
408         VGA_WR08(par->riva.PCIO, 0x3d4, index);
409         VGA_WR08(par->riva.PCIO, 0x3d5, val);
410 }
411
412 static inline unsigned char CRTCin(struct riva_par *par,
413                                    unsigned char index)
414 {
415         VGA_WR08(par->riva.PCIO, 0x3d4, index);
416         return (VGA_RD08(par->riva.PCIO, 0x3d5));
417 }
418
419 static inline void GRAout(struct riva_par *par, unsigned char index,
420                           unsigned char val)
421 {
422         VGA_WR08(par->riva.PVIO, 0x3ce, index);
423         VGA_WR08(par->riva.PVIO, 0x3cf, val);
424 }
425
426 static inline unsigned char GRAin(struct riva_par *par,
427                                   unsigned char index)
428 {
429         VGA_WR08(par->riva.PVIO, 0x3ce, index);
430         return (VGA_RD08(par->riva.PVIO, 0x3cf));
431 }
432
433 static inline void SEQout(struct riva_par *par, unsigned char index,
434                           unsigned char val)
435 {
436         VGA_WR08(par->riva.PVIO, 0x3c4, index);
437         VGA_WR08(par->riva.PVIO, 0x3c5, val);
438 }
439
440 static inline unsigned char SEQin(struct riva_par *par,
441                                   unsigned char index)
442 {
443         VGA_WR08(par->riva.PVIO, 0x3c4, index);
444         return (VGA_RD08(par->riva.PVIO, 0x3c5));
445 }
446
447 static inline void ATTRout(struct riva_par *par, unsigned char index,
448                            unsigned char val)
449 {
450         VGA_WR08(par->riva.PCIO, 0x3c0, index);
451         VGA_WR08(par->riva.PCIO, 0x3c0, val);
452 }
453
454 static inline unsigned char ATTRin(struct riva_par *par,
455                                    unsigned char index)
456 {
457         VGA_WR08(par->riva.PCIO, 0x3c0, index);
458         return (VGA_RD08(par->riva.PCIO, 0x3c1));
459 }
460
461 static inline void MISCout(struct riva_par *par, unsigned char val)
462 {
463         VGA_WR08(par->riva.PVIO, 0x3c2, val);
464 }
465
466 static inline unsigned char MISCin(struct riva_par *par)
467 {
468         return (VGA_RD08(par->riva.PVIO, 0x3cc));
469 }
470
471 static inline void reverse_order(u32 *l)
472 {
473         u8 *a = (u8 *)l;
474         a[0] = bitrev8(a[0]);
475         a[1] = bitrev8(a[1]);
476         a[2] = bitrev8(a[2]);
477         a[3] = bitrev8(a[3]);
478 }
479
480 /* ------------------------------------------------------------------------- *
481  *
482  * cursor stuff
483  *
484  * ------------------------------------------------------------------------- */
485
486 /**
487  * rivafb_load_cursor_image - load cursor image to hardware
488  * @data: address to monochrome bitmap (1 = foreground color, 0 = background)
489  * @par:  pointer to private data
490  * @w:    width of cursor image in pixels
491  * @h:    height of cursor image in scanlines
492  * @bg:   background color (ARGB1555) - alpha bit determines opacity
493  * @fg:   foreground color (ARGB1555)
494  *
495  * DESCRIPTiON:
496  * Loads cursor image based on a monochrome source and mask bitmap.  The
497  * image bits determines the color of the pixel, 0 for background, 1 for
498  * foreground.  Only the affected region (as determined by @w and @h 
499  * parameters) will be updated.
500  *
501  * CALLED FROM:
502  * rivafb_cursor()
503  */
504 static void rivafb_load_cursor_image(struct riva_par *par, u8 *data8,
505                                      u16 bg, u16 fg, u32 w, u32 h)
506 {
507         int i, j, k = 0;
508         u32 b, tmp;
509         u32 *data = (u32 *)data8;
510         bg = le16_to_cpu(bg);
511         fg = le16_to_cpu(fg);
512
513         w = (w + 1) & ~1;
514
515         for (i = 0; i < h; i++) {
516                 b = *data++;
517                 reverse_order(&b);
518                 
519                 for (j = 0; j < w/2; j++) {
520                         tmp = 0;
521 #if defined (__BIG_ENDIAN)
522                         tmp = (b & (1 << 31)) ? fg << 16 : bg << 16;
523                         b <<= 1;
524                         tmp |= (b & (1 << 31)) ? fg : bg;
525                         b <<= 1;
526 #else
527                         tmp = (b & 1) ? fg : bg;
528                         b >>= 1;
529                         tmp |= (b & 1) ? fg << 16 : bg << 16;
530                         b >>= 1;
531 #endif
532                         writel(tmp, &par->riva.CURSOR[k++]);
533                 }
534                 k += (MAX_CURS - w)/2;
535         }
536 }
537
538 /* ------------------------------------------------------------------------- *
539  *
540  * general utility functions
541  *
542  * ------------------------------------------------------------------------- */
543
544 /**
545  * riva_wclut - set CLUT entry
546  * @chip: pointer to RIVA_HW_INST object
547  * @regnum: register number
548  * @red: red component
549  * @green: green component
550  * @blue: blue component
551  *
552  * DESCRIPTION:
553  * Sets color register @regnum.
554  *
555  * CALLED FROM:
556  * rivafb_setcolreg()
557  */
558 static void riva_wclut(RIVA_HW_INST *chip,
559                        unsigned char regnum, unsigned char red,
560                        unsigned char green, unsigned char blue)
561 {
562         VGA_WR08(chip->PDIO, 0x3c8, regnum);
563         VGA_WR08(chip->PDIO, 0x3c9, red);
564         VGA_WR08(chip->PDIO, 0x3c9, green);
565         VGA_WR08(chip->PDIO, 0x3c9, blue);
566 }
567
568 /**
569  * riva_rclut - read fromCLUT register
570  * @chip: pointer to RIVA_HW_INST object
571  * @regnum: register number
572  * @red: red component
573  * @green: green component
574  * @blue: blue component
575  *
576  * DESCRIPTION:
577  * Reads red, green, and blue from color register @regnum.
578  *
579  * CALLED FROM:
580  * rivafb_setcolreg()
581  */
582 static void riva_rclut(RIVA_HW_INST *chip,
583                        unsigned char regnum, unsigned char *red,
584                        unsigned char *green, unsigned char *blue)
585 {
586         
587         VGA_WR08(chip->PDIO, 0x3c7, regnum);
588         *red = VGA_RD08(chip->PDIO, 0x3c9);
589         *green = VGA_RD08(chip->PDIO, 0x3c9);
590         *blue = VGA_RD08(chip->PDIO, 0x3c9);
591 }
592
593 /**
594  * riva_save_state - saves current chip state
595  * @par: pointer to riva_par object containing info for current riva board
596  * @regs: pointer to riva_regs object
597  *
598  * DESCRIPTION:
599  * Saves current chip state to @regs.
600  *
601  * CALLED FROM:
602  * rivafb_probe()
603  */
604 /* from GGI */
605 static void riva_save_state(struct riva_par *par, struct riva_regs *regs)
606 {
607         int i;
608
609         NVTRACE_ENTER();
610         par->riva.LockUnlock(&par->riva, 0);
611
612         par->riva.UnloadStateExt(&par->riva, &regs->ext);
613
614         regs->misc_output = MISCin(par);
615
616         for (i = 0; i < NUM_CRT_REGS; i++)
617                 regs->crtc[i] = CRTCin(par, i);
618
619         for (i = 0; i < NUM_ATC_REGS; i++)
620                 regs->attr[i] = ATTRin(par, i);
621
622         for (i = 0; i < NUM_GRC_REGS; i++)
623                 regs->gra[i] = GRAin(par, i);
624
625         for (i = 0; i < NUM_SEQ_REGS; i++)
626                 regs->seq[i] = SEQin(par, i);
627         NVTRACE_LEAVE();
628 }
629
630 /**
631  * riva_load_state - loads current chip state
632  * @par: pointer to riva_par object containing info for current riva board
633  * @regs: pointer to riva_regs object
634  *
635  * DESCRIPTION:
636  * Loads chip state from @regs.
637  *
638  * CALLED FROM:
639  * riva_load_video_mode()
640  * rivafb_probe()
641  * rivafb_remove()
642  */
643 /* from GGI */
644 static void riva_load_state(struct riva_par *par, struct riva_regs *regs)
645 {
646         RIVA_HW_STATE *state = &regs->ext;
647         int i;
648
649         NVTRACE_ENTER();
650         CRTCout(par, 0x11, 0x00);
651
652         par->riva.LockUnlock(&par->riva, 0);
653
654         par->riva.LoadStateExt(&par->riva, state);
655
656         MISCout(par, regs->misc_output);
657
658         for (i = 0; i < NUM_CRT_REGS; i++) {
659                 switch (i) {
660                 case 0x19:
661                 case 0x20 ... 0x40:
662                         break;
663                 default:
664                         CRTCout(par, i, regs->crtc[i]);
665                 }
666         }
667
668         for (i = 0; i < NUM_ATC_REGS; i++)
669                 ATTRout(par, i, regs->attr[i]);
670
671         for (i = 0; i < NUM_GRC_REGS; i++)
672                 GRAout(par, i, regs->gra[i]);
673
674         for (i = 0; i < NUM_SEQ_REGS; i++)
675                 SEQout(par, i, regs->seq[i]);
676         NVTRACE_LEAVE();
677 }
678
679 /**
680  * riva_load_video_mode - calculate timings
681  * @info: pointer to fb_info object containing info for current riva board
682  *
683  * DESCRIPTION:
684  * Calculate some timings and then send em off to riva_load_state().
685  *
686  * CALLED FROM:
687  * rivafb_set_par()
688  */
689 static int riva_load_video_mode(struct fb_info *info)
690 {
691         int bpp, width, hDisplaySize, hDisplay, hStart,
692             hEnd, hTotal, height, vDisplay, vStart, vEnd, vTotal, dotClock;
693         int hBlankStart, hBlankEnd, vBlankStart, vBlankEnd;
694         int rc;
695         struct riva_par *par = info->par;
696         struct riva_regs newmode;
697         
698         NVTRACE_ENTER();
699         /* time to calculate */
700         rivafb_blank(FB_BLANK_NORMAL, info);
701
702         bpp = info->var.bits_per_pixel;
703         if (bpp == 16 && info->var.green.length == 5)
704                 bpp = 15;
705         width = info->var.xres_virtual;
706         hDisplaySize = info->var.xres;
707         hDisplay = (hDisplaySize / 8) - 1;
708         hStart = (hDisplaySize + info->var.right_margin) / 8 - 1;
709         hEnd = (hDisplaySize + info->var.right_margin +
710                 info->var.hsync_len) / 8 - 1;
711         hTotal = (hDisplaySize + info->var.right_margin +
712                   info->var.hsync_len + info->var.left_margin) / 8 - 5;
713         hBlankStart = hDisplay;
714         hBlankEnd = hTotal + 4;
715
716         height = info->var.yres_virtual;
717         vDisplay = info->var.yres - 1;
718         vStart = info->var.yres + info->var.lower_margin - 1;
719         vEnd = info->var.yres + info->var.lower_margin +
720                info->var.vsync_len - 1;
721         vTotal = info->var.yres + info->var.lower_margin +
722                  info->var.vsync_len + info->var.upper_margin + 2;
723         vBlankStart = vDisplay;
724         vBlankEnd = vTotal + 1;
725         dotClock = 1000000000 / info->var.pixclock;
726
727         memcpy(&newmode, &reg_template, sizeof(struct riva_regs));
728
729         if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED)
730                 vTotal |= 1;
731
732         if (par->FlatPanel) {
733                 vStart = vTotal - 3;
734                 vEnd = vTotal - 2;
735                 vBlankStart = vStart;
736                 hStart = hTotal - 3;
737                 hEnd = hTotal - 2;
738                 hBlankEnd = hTotal + 4;
739         }
740
741         newmode.crtc[0x0] = Set8Bits (hTotal); 
742         newmode.crtc[0x1] = Set8Bits (hDisplay);
743         newmode.crtc[0x2] = Set8Bits (hBlankStart);
744         newmode.crtc[0x3] = SetBitField (hBlankEnd, 4: 0, 4:0) | SetBit (7);
745         newmode.crtc[0x4] = Set8Bits (hStart);
746         newmode.crtc[0x5] = SetBitField (hBlankEnd, 5: 5, 7:7)
747                 | SetBitField (hEnd, 4: 0, 4:0);
748         newmode.crtc[0x6] = SetBitField (vTotal, 7: 0, 7:0);
749         newmode.crtc[0x7] = SetBitField (vTotal, 8: 8, 0:0)
750                 | SetBitField (vDisplay, 8: 8, 1:1)
751                 | SetBitField (vStart, 8: 8, 2:2)
752                 | SetBitField (vBlankStart, 8: 8, 3:3)
753                 | SetBit (4)
754                 | SetBitField (vTotal, 9: 9, 5:5)
755                 | SetBitField (vDisplay, 9: 9, 6:6)
756                 | SetBitField (vStart, 9: 9, 7:7);
757         newmode.crtc[0x9] = SetBitField (vBlankStart, 9: 9, 5:5)
758                 | SetBit (6);
759         newmode.crtc[0x10] = Set8Bits (vStart);
760         newmode.crtc[0x11] = SetBitField (vEnd, 3: 0, 3:0)
761                 | SetBit (5);
762         newmode.crtc[0x12] = Set8Bits (vDisplay);
763         newmode.crtc[0x13] = (width / 8) * ((bpp + 1) / 8);
764         newmode.crtc[0x15] = Set8Bits (vBlankStart);
765         newmode.crtc[0x16] = Set8Bits (vBlankEnd);
766
767         newmode.ext.screen = SetBitField(hBlankEnd,6:6,4:4)
768                 | SetBitField(vBlankStart,10:10,3:3)
769                 | SetBitField(vStart,10:10,2:2)
770                 | SetBitField(vDisplay,10:10,1:1)
771                 | SetBitField(vTotal,10:10,0:0);
772         newmode.ext.horiz  = SetBitField(hTotal,8:8,0:0) 
773                 | SetBitField(hDisplay,8:8,1:1)
774                 | SetBitField(hBlankStart,8:8,2:2)
775                 | SetBitField(hStart,8:8,3:3);
776         newmode.ext.extra  = SetBitField(vTotal,11:11,0:0)
777                 | SetBitField(vDisplay,11:11,2:2)
778                 | SetBitField(vStart,11:11,4:4)
779                 | SetBitField(vBlankStart,11:11,6:6); 
780
781         if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) {
782                 int tmp = (hTotal >> 1) & ~1;
783                 newmode.ext.interlace = Set8Bits(tmp);
784                 newmode.ext.horiz |= SetBitField(tmp, 8:8,4:4);
785         } else 
786                 newmode.ext.interlace = 0xff; /* interlace off */
787
788         if (par->riva.Architecture >= NV_ARCH_10)
789                 par->riva.CURSOR = (U032 __iomem *)(info->screen_base + par->riva.CursorStart);
790
791         if (info->var.sync & FB_SYNC_HOR_HIGH_ACT)
792                 newmode.misc_output &= ~0x40;
793         else
794                 newmode.misc_output |= 0x40;
795         if (info->var.sync & FB_SYNC_VERT_HIGH_ACT)
796                 newmode.misc_output &= ~0x80;
797         else
798                 newmode.misc_output |= 0x80;    
799
800         rc = CalcStateExt(&par->riva, &newmode.ext, bpp, width,
801                           hDisplaySize, height, dotClock);
802         if (rc)
803                 goto out;
804
805         newmode.ext.scale = NV_RD32(par->riva.PRAMDAC, 0x00000848) &
806                 0xfff000ff;
807         if (par->FlatPanel == 1) {
808                 newmode.ext.pixel |= (1 << 7);
809                 newmode.ext.scale |= (1 << 8);
810         }
811         if (par->SecondCRTC) {
812                 newmode.ext.head  = NV_RD32(par->riva.PCRTC0, 0x00000860) &
813                         ~0x00001000;
814                 newmode.ext.head2 = NV_RD32(par->riva.PCRTC0, 0x00002860) |
815                         0x00001000;
816                 newmode.ext.crtcOwner = 3;
817                 newmode.ext.pllsel |= 0x20000800;
818                 newmode.ext.vpll2 = newmode.ext.vpll;
819         } else if (par->riva.twoHeads) {
820                 newmode.ext.head  =  NV_RD32(par->riva.PCRTC0, 0x00000860) |
821                         0x00001000;
822                 newmode.ext.head2 =  NV_RD32(par->riva.PCRTC0, 0x00002860) &
823                         ~0x00001000;
824                 newmode.ext.crtcOwner = 0;
825                 newmode.ext.vpll2 = NV_RD32(par->riva.PRAMDAC0, 0x00000520);
826         }
827         if (par->FlatPanel == 1) {
828                 newmode.ext.pixel |= (1 << 7);
829                 newmode.ext.scale |= (1 << 8);
830         }
831         newmode.ext.cursorConfig = 0x02000100;
832         par->current_state = newmode;
833         riva_load_state(par, &par->current_state);
834         par->riva.LockUnlock(&par->riva, 0); /* important for HW cursor */
835
836 out:
837         rivafb_blank(FB_BLANK_UNBLANK, info);
838         NVTRACE_LEAVE();
839
840         return rc;
841 }
842
843 static void riva_update_var(struct fb_var_screeninfo *var,
844                             const struct fb_videomode *modedb)
845 {
846         NVTRACE_ENTER();
847         var->xres = var->xres_virtual = modedb->xres;
848         var->yres = modedb->yres;
849         if (var->yres_virtual < var->yres)
850             var->yres_virtual = var->yres;
851         var->xoffset = var->yoffset = 0;
852         var->pixclock = modedb->pixclock;
853         var->left_margin = modedb->left_margin;
854         var->right_margin = modedb->right_margin;
855         var->upper_margin = modedb->upper_margin;
856         var->lower_margin = modedb->lower_margin;
857         var->hsync_len = modedb->hsync_len;
858         var->vsync_len = modedb->vsync_len;
859         var->sync = modedb->sync;
860         var->vmode = modedb->vmode;
861         NVTRACE_LEAVE();
862 }
863
864 /**
865  * rivafb_do_maximize - 
866  * @info: pointer to fb_info object containing info for current riva board
867  * @var:
868  * @nom:
869  * @den:
870  *
871  * DESCRIPTION:
872  * .
873  *
874  * RETURNS:
875  * -EINVAL on failure, 0 on success
876  * 
877  *
878  * CALLED FROM:
879  * rivafb_check_var()
880  */
881 static int rivafb_do_maximize(struct fb_info *info,
882                               struct fb_var_screeninfo *var,
883                               int nom, int den)
884 {
885         static struct {
886                 int xres, yres;
887         } modes[] = {
888                 {1600, 1280},
889                 {1280, 1024},
890                 {1024, 768},
891                 {800, 600},
892                 {640, 480},
893                 {-1, -1}
894         };
895         int i;
896
897         NVTRACE_ENTER();
898         /* use highest possible virtual resolution */
899         if (var->xres_virtual == -1 && var->yres_virtual == -1) {
900                 printk(KERN_WARNING PFX
901                        "using maximum available virtual resolution\n");
902                 for (i = 0; modes[i].xres != -1; i++) {
903                         if (modes[i].xres * nom / den * modes[i].yres <
904                             info->fix.smem_len)
905                                 break;
906                 }
907                 if (modes[i].xres == -1) {
908                         printk(KERN_ERR PFX
909                                "could not find a virtual resolution that fits into video memory!!\n");
910                         NVTRACE("EXIT - EINVAL error\n");
911                         return -EINVAL;
912                 }
913                 var->xres_virtual = modes[i].xres;
914                 var->yres_virtual = modes[i].yres;
915
916                 printk(KERN_INFO PFX
917                        "virtual resolution set to maximum of %dx%d\n",
918                        var->xres_virtual, var->yres_virtual);
919         } else if (var->xres_virtual == -1) {
920                 var->xres_virtual = (info->fix.smem_len * den /
921                         (nom * var->yres_virtual)) & ~15;
922                 printk(KERN_WARNING PFX
923                        "setting virtual X resolution to %d\n", var->xres_virtual);
924         } else if (var->yres_virtual == -1) {
925                 var->xres_virtual = (var->xres_virtual + 15) & ~15;
926                 var->yres_virtual = info->fix.smem_len * den /
927                         (nom * var->xres_virtual);
928                 printk(KERN_WARNING PFX
929                        "setting virtual Y resolution to %d\n", var->yres_virtual);
930         } else {
931                 var->xres_virtual = (var->xres_virtual + 15) & ~15;
932                 if (var->xres_virtual * nom / den * var->yres_virtual > info->fix.smem_len) {
933                         printk(KERN_ERR PFX
934                                "mode %dx%dx%d rejected...resolution too high to fit into video memory!\n",
935                                var->xres, var->yres, var->bits_per_pixel);
936                         NVTRACE("EXIT - EINVAL error\n");
937                         return -EINVAL;
938                 }
939         }
940         
941         if (var->xres_virtual * nom / den >= 8192) {
942                 printk(KERN_WARNING PFX
943                        "virtual X resolution (%d) is too high, lowering to %d\n",
944                        var->xres_virtual, 8192 * den / nom - 16);
945                 var->xres_virtual = 8192 * den / nom - 16;
946         }
947         
948         if (var->xres_virtual < var->xres) {
949                 printk(KERN_ERR PFX
950                        "virtual X resolution (%d) is smaller than real\n", var->xres_virtual);
951                 return -EINVAL;
952         }
953
954         if (var->yres_virtual < var->yres) {
955                 printk(KERN_ERR PFX
956                        "virtual Y resolution (%d) is smaller than real\n", var->yres_virtual);
957                 return -EINVAL;
958         }
959         if (var->yres_virtual > 0x7fff/nom)
960                 var->yres_virtual = 0x7fff/nom;
961         if (var->xres_virtual > 0x7fff/nom)
962                 var->xres_virtual = 0x7fff/nom;
963         NVTRACE_LEAVE();
964         return 0;
965 }
966
967 static void
968 riva_set_pattern(struct riva_par *par, int clr0, int clr1, int pat0, int pat1)
969 {
970         RIVA_FIFO_FREE(par->riva, Patt, 4);
971         NV_WR32(&par->riva.Patt->Color0, 0, clr0);
972         NV_WR32(&par->riva.Patt->Color1, 0, clr1);
973         NV_WR32(par->riva.Patt->Monochrome, 0, pat0);
974         NV_WR32(par->riva.Patt->Monochrome, 4, pat1);
975 }
976
977 /* acceleration routines */
978 static inline void wait_for_idle(struct riva_par *par)
979 {
980         while (par->riva.Busy(&par->riva));
981 }
982
983 /*
984  * Set ROP.  Translate X rop into ROP3.  Internal routine.
985  */
986 static void
987 riva_set_rop_solid(struct riva_par *par, int rop)
988 {
989         riva_set_pattern(par, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
990         RIVA_FIFO_FREE(par->riva, Rop, 1);
991         NV_WR32(&par->riva.Rop->Rop3, 0, rop);
992
993 }
994
995 static void riva_setup_accel(struct fb_info *info)
996 {
997         struct riva_par *par = info->par;
998
999         RIVA_FIFO_FREE(par->riva, Clip, 2);
1000         NV_WR32(&par->riva.Clip->TopLeft, 0, 0x0);
1001         NV_WR32(&par->riva.Clip->WidthHeight, 0,
1002                 (info->var.xres_virtual & 0xffff) |
1003                 (info->var.yres_virtual << 16));
1004         riva_set_rop_solid(par, 0xcc);
1005         wait_for_idle(par);
1006 }
1007
1008 /**
1009  * riva_get_cmap_len - query current color map length
1010  * @var: standard kernel fb changeable data
1011  *
1012  * DESCRIPTION:
1013  * Get current color map length.
1014  *
1015  * RETURNS:
1016  * Length of color map
1017  *
1018  * CALLED FROM:
1019  * rivafb_setcolreg()
1020  */
1021 static int riva_get_cmap_len(const struct fb_var_screeninfo *var)
1022 {
1023         int rc = 256;           /* reasonable default */
1024
1025         switch (var->green.length) {
1026         case 8:
1027                 rc = 256;       /* 256 entries (2^8), 8 bpp and RGB8888 */
1028                 break;
1029         case 5:
1030                 rc = 32;        /* 32 entries (2^5), 16 bpp, RGB555 */
1031                 break;
1032         case 6:
1033                 rc = 64;        /* 64 entries (2^6), 16 bpp, RGB565 */
1034                 break;          
1035         default:
1036                 /* should not occur */
1037                 break;
1038         }
1039         return rc;
1040 }
1041
1042 /* ------------------------------------------------------------------------- *
1043  *
1044  * framebuffer operations
1045  *
1046  * ------------------------------------------------------------------------- */
1047
1048 static int rivafb_open(struct fb_info *info, int user)
1049 {
1050         struct riva_par *par = info->par;
1051
1052         NVTRACE_ENTER();
1053         mutex_lock(&par->open_lock);
1054         if (!par->ref_count) {
1055 #ifdef CONFIG_X86
1056                 memset(&par->state, 0, sizeof(struct vgastate));
1057                 par->state.flags = VGA_SAVE_MODE  | VGA_SAVE_FONTS;
1058                 /* save the DAC for Riva128 */
1059                 if (par->riva.Architecture == NV_ARCH_03)
1060                         par->state.flags |= VGA_SAVE_CMAP;
1061                 save_vga(&par->state);
1062 #endif
1063                 /* vgaHWunlock() + riva unlock (0x7F) */
1064                 CRTCout(par, 0x11, 0xFF);
1065                 par->riva.LockUnlock(&par->riva, 0);
1066         
1067                 riva_save_state(par, &par->initial_state);
1068         }
1069         par->ref_count++;
1070         mutex_unlock(&par->open_lock);
1071         NVTRACE_LEAVE();
1072         return 0;
1073 }
1074
1075 static int rivafb_release(struct fb_info *info, int user)
1076 {
1077         struct riva_par *par = info->par;
1078
1079         NVTRACE_ENTER();
1080         mutex_lock(&par->open_lock);
1081         if (!par->ref_count) {
1082                 mutex_unlock(&par->open_lock);
1083                 return -EINVAL;
1084         }
1085         if (par->ref_count == 1) {
1086                 par->riva.LockUnlock(&par->riva, 0);
1087                 par->riva.LoadStateExt(&par->riva, &par->initial_state.ext);
1088                 riva_load_state(par, &par->initial_state);
1089 #ifdef CONFIG_X86
1090                 restore_vga(&par->state);
1091 #endif
1092                 par->riva.LockUnlock(&par->riva, 1);
1093         }
1094         par->ref_count--;
1095         mutex_unlock(&par->open_lock);
1096         NVTRACE_LEAVE();
1097         return 0;
1098 }
1099
1100 static int rivafb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
1101 {
1102         const struct fb_videomode *mode;
1103         struct riva_par *par = info->par;
1104         int nom, den;           /* translating from pixels->bytes */
1105         int mode_valid = 0;
1106         
1107         NVTRACE_ENTER();
1108         switch (var->bits_per_pixel) {
1109         case 1 ... 8:
1110                 var->red.offset = var->green.offset = var->blue.offset = 0;
1111                 var->red.length = var->green.length = var->blue.length = 8;
1112                 var->bits_per_pixel = 8;
1113                 nom = den = 1;
1114                 break;
1115         case 9 ... 15:
1116                 var->green.length = 5;
1117                 /* fall through */
1118         case 16:
1119                 var->bits_per_pixel = 16;
1120                 /* The Riva128 supports RGB555 only */
1121                 if (par->riva.Architecture == NV_ARCH_03)
1122                         var->green.length = 5;
1123                 if (var->green.length == 5) {
1124                         /* 0rrrrrgg gggbbbbb */
1125                         var->red.offset = 10;
1126                         var->green.offset = 5;
1127                         var->blue.offset = 0;
1128                         var->red.length = 5;
1129                         var->green.length = 5;
1130                         var->blue.length = 5;
1131                 } else {
1132                         /* rrrrrggg gggbbbbb */
1133                         var->red.offset = 11;
1134                         var->green.offset = 5;
1135                         var->blue.offset = 0;
1136                         var->red.length = 5;
1137                         var->green.length = 6;
1138                         var->blue.length = 5;
1139                 }
1140                 nom = 2;
1141                 den = 1;
1142                 break;
1143         case 17 ... 32:
1144                 var->red.length = var->green.length = var->blue.length = 8;
1145                 var->bits_per_pixel = 32;
1146                 var->red.offset = 16;
1147                 var->green.offset = 8;
1148                 var->blue.offset = 0;
1149                 nom = 4;
1150                 den = 1;
1151                 break;
1152         default:
1153                 printk(KERN_ERR PFX
1154                        "mode %dx%dx%d rejected...color depth not supported.\n",
1155                        var->xres, var->yres, var->bits_per_pixel);
1156                 NVTRACE("EXIT, returning -EINVAL\n");
1157                 return -EINVAL;
1158         }
1159
1160         if (!strictmode) {
1161                 if (!info->monspecs.vfmax || !info->monspecs.hfmax ||
1162                     !info->monspecs.dclkmax || !fb_validate_mode(var, info))
1163                         mode_valid = 1;
1164         }
1165
1166         /* calculate modeline if supported by monitor */
1167         if (!mode_valid && info->monspecs.gtf) {
1168                 if (!fb_get_mode(FB_MAXTIMINGS, 0, var, info))
1169                         mode_valid = 1;
1170         }
1171
1172         if (!mode_valid) {
1173                 mode = fb_find_best_mode(var, &info->modelist);
1174                 if (mode) {
1175                         riva_update_var(var, mode);
1176                         mode_valid = 1;
1177                 }
1178         }
1179
1180         if (!mode_valid && info->monspecs.modedb_len)
1181                 return -EINVAL;
1182
1183         if (var->xres_virtual < var->xres)
1184                 var->xres_virtual = var->xres;
1185         if (var->yres_virtual <= var->yres)
1186                 var->yres_virtual = -1;
1187         if (rivafb_do_maximize(info, var, nom, den) < 0)
1188                 return -EINVAL;
1189
1190         if (var->xoffset < 0)
1191                 var->xoffset = 0;
1192         if (var->yoffset < 0)
1193                 var->yoffset = 0;
1194
1195         /* truncate xoffset and yoffset to maximum if too high */
1196         if (var->xoffset > var->xres_virtual - var->xres)
1197                 var->xoffset = var->xres_virtual - var->xres - 1;
1198
1199         if (var->yoffset > var->yres_virtual - var->yres)
1200                 var->yoffset = var->yres_virtual - var->yres - 1;
1201
1202         var->red.msb_right = 
1203             var->green.msb_right =
1204             var->blue.msb_right =
1205             var->transp.offset = var->transp.length = var->transp.msb_right = 0;
1206         NVTRACE_LEAVE();
1207         return 0;
1208 }
1209
1210 static int rivafb_set_par(struct fb_info *info)
1211 {
1212         struct riva_par *par = info->par;
1213         int rc = 0;
1214
1215         NVTRACE_ENTER();
1216         /* vgaHWunlock() + riva unlock (0x7F) */
1217         CRTCout(par, 0x11, 0xFF);
1218         par->riva.LockUnlock(&par->riva, 0);
1219         rc = riva_load_video_mode(info);
1220         if (rc)
1221                 goto out;
1222         if(!(info->flags & FBINFO_HWACCEL_DISABLED))
1223                 riva_setup_accel(info);
1224         
1225         par->cursor_reset = 1;
1226         info->fix.line_length = (info->var.xres_virtual * (info->var.bits_per_pixel >> 3));
1227         info->fix.visual = (info->var.bits_per_pixel == 8) ?
1228                                 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_DIRECTCOLOR;
1229
1230         if (info->flags & FBINFO_HWACCEL_DISABLED)
1231                 info->pixmap.scan_align = 1;
1232         else
1233                 info->pixmap.scan_align = 4;
1234
1235 out:
1236         NVTRACE_LEAVE();
1237         return rc;
1238 }
1239
1240 /**
1241  * rivafb_pan_display
1242  * @var: standard kernel fb changeable data
1243  * @con: TODO
1244  * @info: pointer to fb_info object containing info for current riva board
1245  *
1246  * DESCRIPTION:
1247  * Pan (or wrap, depending on the `vmode' field) the display using the
1248  * `xoffset' and `yoffset' fields of the `var' structure.
1249  * If the values don't fit, return -EINVAL.
1250  *
1251  * This call looks only at xoffset, yoffset and the FB_VMODE_YWRAP flag
1252  */
1253 static int rivafb_pan_display(struct fb_var_screeninfo *var,
1254                               struct fb_info *info)
1255 {
1256         struct riva_par *par = info->par;
1257         unsigned int base;
1258
1259         NVTRACE_ENTER();
1260         base = var->yoffset * info->fix.line_length + var->xoffset;
1261         par->riva.SetStartAddress(&par->riva, base);
1262         NVTRACE_LEAVE();
1263         return 0;
1264 }
1265
1266 static int rivafb_blank(int blank, struct fb_info *info)
1267 {
1268         struct riva_par *par= info->par;
1269         unsigned char tmp, vesa;
1270
1271         tmp = SEQin(par, 0x01) & ~0x20; /* screen on/off */
1272         vesa = CRTCin(par, 0x1a) & ~0xc0;       /* sync on/off */
1273
1274         NVTRACE_ENTER();
1275
1276         if (blank)
1277                 tmp |= 0x20;
1278
1279         switch (blank) {
1280         case FB_BLANK_UNBLANK:
1281         case FB_BLANK_NORMAL:
1282                 break;
1283         case FB_BLANK_VSYNC_SUSPEND:
1284                 vesa |= 0x80;
1285                 break;
1286         case FB_BLANK_HSYNC_SUSPEND:
1287                 vesa |= 0x40;
1288                 break;
1289         case FB_BLANK_POWERDOWN:
1290                 vesa |= 0xc0;
1291                 break;
1292         }
1293
1294         SEQout(par, 0x01, tmp);
1295         CRTCout(par, 0x1a, vesa);
1296
1297         NVTRACE_LEAVE();
1298
1299         return 0;
1300 }
1301
1302 /**
1303  * rivafb_setcolreg
1304  * @regno: register index
1305  * @red: red component
1306  * @green: green component
1307  * @blue: blue component
1308  * @transp: transparency
1309  * @info: pointer to fb_info object containing info for current riva board
1310  *
1311  * DESCRIPTION:
1312  * Set a single color register. The values supplied have a 16 bit
1313  * magnitude.
1314  *
1315  * RETURNS:
1316  * Return != 0 for invalid regno.
1317  *
1318  * CALLED FROM:
1319  * fbcmap.c:fb_set_cmap()
1320  */
1321 static int rivafb_setcolreg(unsigned regno, unsigned red, unsigned green,
1322                           unsigned blue, unsigned transp,
1323                           struct fb_info *info)
1324 {
1325         struct riva_par *par = info->par;
1326         RIVA_HW_INST *chip = &par->riva;
1327         int i;
1328
1329         if (regno >= riva_get_cmap_len(&info->var))
1330                         return -EINVAL;
1331
1332         if (info->var.grayscale) {
1333                 /* gray = 0.30*R + 0.59*G + 0.11*B */
1334                 red = green = blue =
1335                     (red * 77 + green * 151 + blue * 28) >> 8;
1336         }
1337
1338         if (regno < 16 && info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
1339                 ((u32 *) info->pseudo_palette)[regno] =
1340                         (regno << info->var.red.offset) |
1341                         (regno << info->var.green.offset) |
1342                         (regno << info->var.blue.offset);
1343                 /*
1344                  * The Riva128 2D engine requires color information in
1345                  * TrueColor format even if framebuffer is in DirectColor
1346                  */
1347                 if (par->riva.Architecture == NV_ARCH_03) {
1348                         switch (info->var.bits_per_pixel) {
1349                         case 16:
1350                                 par->palette[regno] = ((red & 0xf800) >> 1) |
1351                                         ((green & 0xf800) >> 6) |
1352                                         ((blue & 0xf800) >> 11);
1353                                 break;
1354                         case 32:
1355                                 par->palette[regno] = ((red & 0xff00) << 8) |
1356                                         ((green & 0xff00)) |
1357                                         ((blue & 0xff00) >> 8);
1358                                 break;
1359                         }
1360                 }
1361         }
1362
1363         switch (info->var.bits_per_pixel) {
1364         case 8:
1365                 /* "transparent" stuff is completely ignored. */
1366                 riva_wclut(chip, regno, red >> 8, green >> 8, blue >> 8);
1367                 break;
1368         case 16:
1369                 if (info->var.green.length == 5) {
1370                         for (i = 0; i < 8; i++) {
1371                                 riva_wclut(chip, regno*8+i, red >> 8,
1372                                            green >> 8, blue >> 8);
1373                         }
1374                 } else {
1375                         u8 r, g, b;
1376
1377                         if (regno < 32) {
1378                                 for (i = 0; i < 8; i++) {
1379                                         riva_wclut(chip, regno*8+i,
1380                                                    red >> 8, green >> 8,
1381                                                    blue >> 8);
1382                                 }
1383                         }
1384                         riva_rclut(chip, regno*4, &r, &g, &b);
1385                         for (i = 0; i < 4; i++)
1386                                 riva_wclut(chip, regno*4+i, r,
1387                                            green >> 8, b);
1388                 }
1389                 break;
1390         case 32:
1391                 riva_wclut(chip, regno, red >> 8, green >> 8, blue >> 8);
1392                 break;
1393         default:
1394                 /* do nothing */
1395                 break;
1396         }
1397         return 0;
1398 }
1399
1400 /**
1401  * rivafb_fillrect - hardware accelerated color fill function
1402  * @info: pointer to fb_info structure
1403  * @rect: pointer to fb_fillrect structure
1404  *
1405  * DESCRIPTION:
1406  * This function fills up a region of framebuffer memory with a solid
1407  * color with a choice of two different ROP's, copy or invert.
1408  *
1409  * CALLED FROM:
1410  * framebuffer hook
1411  */
1412 static void rivafb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
1413 {
1414         struct riva_par *par = info->par;
1415         u_int color, rop = 0;
1416
1417         if ((info->flags & FBINFO_HWACCEL_DISABLED)) {
1418                 cfb_fillrect(info, rect);
1419                 return;
1420         }
1421
1422         if (info->var.bits_per_pixel == 8)
1423                 color = rect->color;
1424         else {
1425                 if (par->riva.Architecture != NV_ARCH_03)
1426                         color = ((u32 *)info->pseudo_palette)[rect->color];
1427                 else
1428                         color = par->palette[rect->color];
1429         }
1430
1431         switch (rect->rop) {
1432         case ROP_XOR:
1433                 rop = 0x66;
1434                 break;
1435         case ROP_COPY:
1436         default:
1437                 rop = 0xCC;
1438                 break;
1439         }
1440
1441         riva_set_rop_solid(par, rop);
1442
1443         RIVA_FIFO_FREE(par->riva, Bitmap, 1);
1444         NV_WR32(&par->riva.Bitmap->Color1A, 0, color);
1445
1446         RIVA_FIFO_FREE(par->riva, Bitmap, 2);
1447         NV_WR32(&par->riva.Bitmap->UnclippedRectangle[0].TopLeft, 0,
1448                 (rect->dx << 16) | rect->dy);
1449         mb();
1450         NV_WR32(&par->riva.Bitmap->UnclippedRectangle[0].WidthHeight, 0,
1451                 (rect->width << 16) | rect->height);
1452         mb();
1453         riva_set_rop_solid(par, 0xcc);
1454
1455 }
1456
1457 /**
1458  * rivafb_copyarea - hardware accelerated blit function
1459  * @info: pointer to fb_info structure
1460  * @region: pointer to fb_copyarea structure
1461  *
1462  * DESCRIPTION:
1463  * This copies an area of pixels from one location to another
1464  *
1465  * CALLED FROM:
1466  * framebuffer hook
1467  */
1468 static void rivafb_copyarea(struct fb_info *info, const struct fb_copyarea *region)
1469 {
1470         struct riva_par *par = info->par;
1471
1472         if ((info->flags & FBINFO_HWACCEL_DISABLED)) {
1473                 cfb_copyarea(info, region);
1474                 return;
1475         }
1476
1477         RIVA_FIFO_FREE(par->riva, Blt, 3);
1478         NV_WR32(&par->riva.Blt->TopLeftSrc, 0,
1479                 (region->sy << 16) | region->sx);
1480         NV_WR32(&par->riva.Blt->TopLeftDst, 0,
1481                 (region->dy << 16) | region->dx);
1482         mb();
1483         NV_WR32(&par->riva.Blt->WidthHeight, 0,
1484                 (region->height << 16) | region->width);
1485         mb();
1486 }
1487
1488 static inline void convert_bgcolor_16(u32 *col)
1489 {
1490         *col = ((*col & 0x0000F800) << 8)
1491                 | ((*col & 0x00007E0) << 5)
1492                 | ((*col & 0x0000001F) << 3)
1493                 |          0xFF000000;
1494         mb();
1495 }
1496
1497 /**
1498  * rivafb_imageblit: hardware accelerated color expand function
1499  * @info: pointer to fb_info structure
1500  * @image: pointer to fb_image structure
1501  *
1502  * DESCRIPTION:
1503  * If the source is a monochrome bitmap, the function fills up a a region
1504  * of framebuffer memory with pixels whose color is determined by the bit
1505  * setting of the bitmap, 1 - foreground, 0 - background.
1506  *
1507  * If the source is not a monochrome bitmap, color expansion is not done.
1508  * In this case, it is channeled to a software function.
1509  *
1510  * CALLED FROM:
1511  * framebuffer hook
1512  */
1513 static void rivafb_imageblit(struct fb_info *info, 
1514                              const struct fb_image *image)
1515 {
1516         struct riva_par *par = info->par;
1517         u32 fgx = 0, bgx = 0, width, tmp;
1518         u8 *cdat = (u8 *) image->data;
1519         volatile u32 __iomem *d;
1520         int i, size;
1521
1522         if ((info->flags & FBINFO_HWACCEL_DISABLED) || image->depth != 1) {
1523                 cfb_imageblit(info, image);
1524                 return;
1525         }
1526
1527         switch (info->var.bits_per_pixel) {
1528         case 8:
1529                 fgx = image->fg_color;
1530                 bgx = image->bg_color;
1531                 break;
1532         case 16:
1533         case 32:
1534                 if (par->riva.Architecture != NV_ARCH_03) {
1535                         fgx = ((u32 *)info->pseudo_palette)[image->fg_color];
1536                         bgx = ((u32 *)info->pseudo_palette)[image->bg_color];
1537                 } else {
1538                         fgx = par->palette[image->fg_color];
1539                         bgx = par->palette[image->bg_color];
1540                 }
1541                 if (info->var.green.length == 6)
1542                         convert_bgcolor_16(&bgx);       
1543                 break;
1544         }
1545
1546         RIVA_FIFO_FREE(par->riva, Bitmap, 7);
1547         NV_WR32(&par->riva.Bitmap->ClipE.TopLeft, 0,
1548                 (image->dy << 16) | (image->dx & 0xFFFF));
1549         NV_WR32(&par->riva.Bitmap->ClipE.BottomRight, 0,
1550                 (((image->dy + image->height) << 16) |
1551                  ((image->dx + image->width) & 0xffff)));
1552         NV_WR32(&par->riva.Bitmap->Color0E, 0, bgx);
1553         NV_WR32(&par->riva.Bitmap->Color1E, 0, fgx);
1554         NV_WR32(&par->riva.Bitmap->WidthHeightInE, 0,
1555                 (image->height << 16) | ((image->width + 31) & ~31));
1556         NV_WR32(&par->riva.Bitmap->WidthHeightOutE, 0,
1557                 (image->height << 16) | ((image->width + 31) & ~31));
1558         NV_WR32(&par->riva.Bitmap->PointE, 0,
1559                 (image->dy << 16) | (image->dx & 0xFFFF));
1560
1561         d = &par->riva.Bitmap->MonochromeData01E;
1562
1563         width = (image->width + 31)/32;
1564         size = width * image->height;
1565         while (size >= 16) {
1566                 RIVA_FIFO_FREE(par->riva, Bitmap, 16);
1567                 for (i = 0; i < 16; i++) {
1568                         tmp = *((u32 *)cdat);
1569                         cdat = (u8 *)((u32 *)cdat + 1);
1570                         reverse_order(&tmp);
1571                         NV_WR32(d, i*4, tmp);
1572                 }
1573                 size -= 16;
1574         }
1575         if (size) {
1576                 RIVA_FIFO_FREE(par->riva, Bitmap, size);
1577                 for (i = 0; i < size; i++) {
1578                         tmp = *((u32 *) cdat);
1579                         cdat = (u8 *)((u32 *)cdat + 1);
1580                         reverse_order(&tmp);
1581                         NV_WR32(d, i*4, tmp);
1582                 }
1583         }
1584 }
1585
1586 /**
1587  * rivafb_cursor - hardware cursor function
1588  * @info: pointer to info structure
1589  * @cursor: pointer to fbcursor structure
1590  *
1591  * DESCRIPTION:
1592  * A cursor function that supports displaying a cursor image via hardware.
1593  * Within the kernel, copy and invert rops are supported.  If exported
1594  * to user space, only the copy rop will be supported.
1595  *
1596  * CALLED FROM
1597  * framebuffer hook
1598  */
1599 static int rivafb_cursor(struct fb_info *info, struct fb_cursor *cursor)
1600 {
1601         struct riva_par *par = info->par;
1602         u8 data[MAX_CURS * MAX_CURS/8];
1603         int i, set = cursor->set;
1604         u16 fg, bg;
1605
1606         if (cursor->image.width > MAX_CURS || cursor->image.height > MAX_CURS)
1607                 return -ENXIO;
1608
1609         par->riva.ShowHideCursor(&par->riva, 0);
1610
1611         if (par->cursor_reset) {
1612                 set = FB_CUR_SETALL;
1613                 par->cursor_reset = 0;
1614         }
1615
1616         if (set & FB_CUR_SETSIZE)
1617                 memset_io(par->riva.CURSOR, 0, MAX_CURS * MAX_CURS * 2);
1618
1619         if (set & FB_CUR_SETPOS) {
1620                 u32 xx, yy, temp;
1621
1622                 yy = cursor->image.dy - info->var.yoffset;
1623                 xx = cursor->image.dx - info->var.xoffset;
1624                 temp = xx & 0xFFFF;
1625                 temp |= yy << 16;
1626
1627                 NV_WR32(par->riva.PRAMDAC, 0x0000300, temp);
1628         }
1629
1630
1631         if (set & (FB_CUR_SETSHAPE | FB_CUR_SETCMAP | FB_CUR_SETIMAGE)) {
1632                 u32 bg_idx = cursor->image.bg_color;
1633                 u32 fg_idx = cursor->image.fg_color;
1634                 u32 s_pitch = (cursor->image.width+7) >> 3;
1635                 u32 d_pitch = MAX_CURS/8;
1636                 u8 *dat = (u8 *) cursor->image.data;
1637                 u8 *msk = (u8 *) cursor->mask;
1638                 u8 *src;
1639                 
1640                 src = kmalloc(s_pitch * cursor->image.height, GFP_ATOMIC);
1641
1642                 if (src) {
1643                         switch (cursor->rop) {
1644                         case ROP_XOR:
1645                                 for (i = 0; i < s_pitch * cursor->image.height; i++)
1646                                         src[i] = dat[i] ^ msk[i];
1647                                 break;
1648                         case ROP_COPY:
1649                         default:
1650                                 for (i = 0; i < s_pitch * cursor->image.height; i++)
1651                                         src[i] = dat[i] & msk[i];
1652                                 break;
1653                         }
1654
1655                         fb_pad_aligned_buffer(data, d_pitch, src, s_pitch,
1656                                                 cursor->image.height);
1657
1658                         bg = ((info->cmap.red[bg_idx] & 0xf8) << 7) |
1659                                 ((info->cmap.green[bg_idx] & 0xf8) << 2) |
1660                                 ((info->cmap.blue[bg_idx] & 0xf8) >> 3) |
1661                                 1 << 15;
1662
1663                         fg = ((info->cmap.red[fg_idx] & 0xf8) << 7) |
1664                                 ((info->cmap.green[fg_idx] & 0xf8) << 2) |
1665                                 ((info->cmap.blue[fg_idx] & 0xf8) >> 3) |
1666                                 1 << 15;
1667
1668                         par->riva.LockUnlock(&par->riva, 0);
1669
1670                         rivafb_load_cursor_image(par, data, bg, fg,
1671                                                  cursor->image.width,
1672                                                  cursor->image.height);
1673                         kfree(src);
1674                 }
1675         }
1676
1677         if (cursor->enable)
1678                 par->riva.ShowHideCursor(&par->riva, 1);
1679
1680         return 0;
1681 }
1682
1683 static int rivafb_sync(struct fb_info *info)
1684 {
1685         struct riva_par *par = info->par;
1686
1687         wait_for_idle(par);
1688         return 0;
1689 }
1690
1691 /* ------------------------------------------------------------------------- *
1692  *
1693  * initialization helper functions
1694  *
1695  * ------------------------------------------------------------------------- */
1696
1697 /* kernel interface */
1698 static struct fb_ops riva_fb_ops = {
1699         .owner          = THIS_MODULE,
1700         .fb_open        = rivafb_open,
1701         .fb_release     = rivafb_release,
1702         .fb_check_var   = rivafb_check_var,
1703         .fb_set_par     = rivafb_set_par,
1704         .fb_setcolreg   = rivafb_setcolreg,
1705         .fb_pan_display = rivafb_pan_display,
1706         .fb_blank       = rivafb_blank,
1707         .fb_fillrect    = rivafb_fillrect,
1708         .fb_copyarea    = rivafb_copyarea,
1709         .fb_imageblit   = rivafb_imageblit,
1710         .fb_cursor      = rivafb_cursor,        
1711         .fb_sync        = rivafb_sync,
1712 };
1713
1714 static int __devinit riva_set_fbinfo(struct fb_info *info)
1715 {
1716         unsigned int cmap_len;
1717         struct riva_par *par = info->par;
1718
1719         NVTRACE_ENTER();
1720         info->flags = FBINFO_DEFAULT
1721                     | FBINFO_HWACCEL_XPAN
1722                     | FBINFO_HWACCEL_YPAN
1723                     | FBINFO_HWACCEL_COPYAREA
1724                     | FBINFO_HWACCEL_FILLRECT
1725                     | FBINFO_HWACCEL_IMAGEBLIT;
1726
1727         /* Accel seems to not work properly on NV30 yet...*/
1728         if ((par->riva.Architecture == NV_ARCH_30) || noaccel) {
1729                 printk(KERN_DEBUG PFX "disabling acceleration\n");
1730                 info->flags |= FBINFO_HWACCEL_DISABLED;
1731         }
1732
1733         info->var = rivafb_default_var;
1734         info->fix.visual = (info->var.bits_per_pixel == 8) ?
1735                                 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_DIRECTCOLOR;
1736
1737         info->pseudo_palette = par->pseudo_palette;
1738
1739         cmap_len = riva_get_cmap_len(&info->var);
1740         fb_alloc_cmap(&info->cmap, cmap_len, 0);        
1741
1742         info->pixmap.size = 8 * 1024;
1743         info->pixmap.buf_align = 4;
1744         info->pixmap.access_align = 32;
1745         info->pixmap.flags = FB_PIXMAP_SYSTEM;
1746         info->var.yres_virtual = -1;
1747         NVTRACE_LEAVE();
1748         return (rivafb_check_var(&info->var, info));
1749 }
1750
1751 #ifdef CONFIG_PPC_OF
1752 static int __devinit riva_get_EDID_OF(struct fb_info *info, struct pci_dev *pd)
1753 {
1754         struct riva_par *par = info->par;
1755         struct device_node *dp;
1756         const unsigned char *pedid = NULL;
1757         const unsigned char *disptype = NULL;
1758         static char *propnames[] = {
1759                 "DFP,EDID", "LCD,EDID", "EDID", "EDID1", "EDID,B", "EDID,A", NULL };
1760         int i;
1761
1762         NVTRACE_ENTER();
1763         dp = pci_device_to_OF_node(pd);
1764         for (; dp != NULL; dp = dp->child) {
1765                 disptype = get_property(dp, "display-type", NULL);
1766                 if (disptype == NULL)
1767                         continue;
1768                 if (strncmp(disptype, "LCD", 3) != 0)
1769                         continue;
1770                 for (i = 0; propnames[i] != NULL; ++i) {
1771                         pedid = get_property(dp, propnames[i], NULL);
1772                         if (pedid != NULL) {
1773                                 par->EDID = (unsigned char *)pedid;
1774                                 NVTRACE("LCD found.\n");
1775                                 return 1;
1776                         }
1777                 }
1778         }
1779         NVTRACE_LEAVE();
1780         return 0;
1781 }
1782 #endif /* CONFIG_PPC_OF */
1783
1784 #if defined(CONFIG_FB_RIVA_I2C) && !defined(CONFIG_PPC_OF)
1785 static int __devinit riva_get_EDID_i2c(struct fb_info *info)
1786 {
1787         struct riva_par *par = info->par;
1788         struct fb_var_screeninfo var;
1789         int i;
1790
1791         NVTRACE_ENTER();
1792         riva_create_i2c_busses(par);
1793         for (i = 0; i < par->bus; i++) {
1794                 riva_probe_i2c_connector(par, i+1, &par->EDID);
1795                 if (par->EDID && !fb_parse_edid(par->EDID, &var)) {
1796                         printk(PFX "Found EDID Block from BUS %i\n", i);
1797                         break;
1798                 }
1799         }
1800
1801         NVTRACE_LEAVE();
1802         return (par->EDID) ? 1 : 0;
1803 }
1804 #endif /* CONFIG_FB_RIVA_I2C */
1805
1806 static void __devinit riva_update_default_var(struct fb_var_screeninfo *var,
1807                                               struct fb_info *info)
1808 {
1809         struct fb_monspecs *specs = &info->monspecs;
1810         struct fb_videomode modedb;
1811
1812         NVTRACE_ENTER();
1813         /* respect mode options */
1814         if (mode_option) {
1815                 fb_find_mode(var, info, mode_option,
1816                              specs->modedb, specs->modedb_len,
1817                              NULL, 8);
1818         } else if (specs->modedb != NULL) {
1819                 /* get preferred timing */
1820                 if (info->monspecs.misc & FB_MISC_1ST_DETAIL) {
1821                         int i;
1822
1823                         for (i = 0; i < specs->modedb_len; i++) {
1824                                 if (specs->modedb[i].flag & FB_MODE_IS_FIRST) {
1825                                         modedb = specs->modedb[i];
1826                                         break;
1827                                 }
1828                         }
1829                 } else {
1830                         /* otherwise, get first mode in database */
1831                         modedb = specs->modedb[0];
1832                 }
1833                 var->bits_per_pixel = 8;
1834                 riva_update_var(var, &modedb);
1835         }
1836         NVTRACE_LEAVE();
1837 }
1838
1839
1840 static void __devinit riva_get_EDID(struct fb_info *info, struct pci_dev *pdev)
1841 {
1842         NVTRACE_ENTER();
1843 #ifdef CONFIG_PPC_OF
1844         if (!riva_get_EDID_OF(info, pdev))
1845                 printk(PFX "could not retrieve EDID from OF\n");
1846 #elif defined(CONFIG_FB_RIVA_I2C)
1847         if (!riva_get_EDID_i2c(info))
1848                 printk(PFX "could not retrieve EDID from DDC/I2C\n");
1849 #endif
1850         NVTRACE_LEAVE();
1851 }
1852
1853
1854 static void __devinit riva_get_edidinfo(struct fb_info *info)
1855 {
1856         struct fb_var_screeninfo *var = &rivafb_default_var;
1857         struct riva_par *par = info->par;
1858
1859         fb_edid_to_monspecs(par->EDID, &info->monspecs);
1860         fb_videomode_to_modelist(info->monspecs.modedb, info->monspecs.modedb_len,
1861                                  &info->modelist);
1862         riva_update_default_var(var, info);
1863
1864         /* if user specified flatpanel, we respect that */
1865         if (info->monspecs.input & FB_DISP_DDI)
1866                 par->FlatPanel = 1;
1867 }
1868
1869 /* ------------------------------------------------------------------------- *
1870  *
1871  * PCI bus
1872  *
1873  * ------------------------------------------------------------------------- */
1874
1875 static u32 __devinit riva_get_arch(struct pci_dev *pd)
1876 {
1877         u32 arch = 0;
1878
1879         switch (pd->device & 0x0ff0) {
1880                 case 0x0100:   /* GeForce 256 */
1881                 case 0x0110:   /* GeForce2 MX */
1882                 case 0x0150:   /* GeForce2 */
1883                 case 0x0170:   /* GeForce4 MX */
1884                 case 0x0180:   /* GeForce4 MX (8x AGP) */
1885                 case 0x01A0:   /* nForce */
1886                 case 0x01F0:   /* nForce2 */
1887                      arch =  NV_ARCH_10;
1888                      break;
1889                 case 0x0200:   /* GeForce3 */
1890                 case 0x0250:   /* GeForce4 Ti */
1891                 case 0x0280:   /* GeForce4 Ti (8x AGP) */
1892                      arch =  NV_ARCH_20;
1893                      break;
1894                 case 0x0300:   /* GeForceFX 5800 */
1895                 case 0x0310:   /* GeForceFX 5600 */
1896                 case 0x0320:   /* GeForceFX 5200 */
1897                 case 0x0330:   /* GeForceFX 5900 */
1898                 case 0x0340:   /* GeForceFX 5700 */
1899                      arch =  NV_ARCH_30;
1900                      break;
1901                 case 0x0020:   /* TNT, TNT2 */
1902                      arch =  NV_ARCH_04;
1903                      break;
1904                 case 0x0010:   /* Riva128 */
1905                      arch =  NV_ARCH_03;
1906                      break;
1907                 default:   /* unknown architecture */
1908                      break;
1909         }
1910         return arch;
1911 }
1912
1913 static int __devinit rivafb_probe(struct pci_dev *pd,
1914                                 const struct pci_device_id *ent)
1915 {
1916         struct riva_par *default_par;
1917         struct fb_info *info;
1918         int ret;
1919
1920         NVTRACE_ENTER();
1921         assert(pd != NULL);
1922
1923         info = framebuffer_alloc(sizeof(struct riva_par), &pd->dev);
1924         if (!info) {
1925                 printk (KERN_ERR PFX "could not allocate memory\n");
1926                 ret = -ENOMEM;
1927                 goto err_ret;
1928         }
1929         default_par = info->par;
1930         default_par->pdev = pd;
1931
1932         info->pixmap.addr = kzalloc(8 * 1024, GFP_KERNEL);
1933         if (info->pixmap.addr == NULL) {
1934                 ret = -ENOMEM;
1935                 goto err_framebuffer_release;
1936         }
1937
1938         ret = pci_enable_device(pd);
1939         if (ret < 0) {
1940                 printk(KERN_ERR PFX "cannot enable PCI device\n");
1941                 goto err_free_pixmap;
1942         }
1943
1944         ret = pci_request_regions(pd, "rivafb");
1945         if (ret < 0) {
1946                 printk(KERN_ERR PFX "cannot request PCI regions\n");
1947                 goto err_disable_device;
1948         }
1949
1950         mutex_init(&default_par->open_lock);
1951         default_par->riva.Architecture = riva_get_arch(pd);
1952
1953         default_par->Chipset = (pd->vendor << 16) | pd->device;
1954         printk(KERN_INFO PFX "nVidia device/chipset %X\n",default_par->Chipset);
1955         
1956         if(default_par->riva.Architecture == 0) {
1957                 printk(KERN_ERR PFX "unknown NV_ARCH\n");
1958                 ret=-ENODEV;
1959                 goto err_release_region;
1960         }
1961         if(default_par->riva.Architecture == NV_ARCH_10 ||
1962            default_par->riva.Architecture == NV_ARCH_20 ||
1963            default_par->riva.Architecture == NV_ARCH_30) {
1964                 sprintf(rivafb_fix.id, "NV%x", (pd->device & 0x0ff0) >> 4);
1965         } else {
1966                 sprintf(rivafb_fix.id, "NV%x", default_par->riva.Architecture);
1967         }
1968
1969         default_par->FlatPanel = flatpanel;
1970         if (flatpanel == 1)
1971                 printk(KERN_INFO PFX "flatpanel support enabled\n");
1972         default_par->forceCRTC = forceCRTC;
1973         
1974         rivafb_fix.mmio_len = pci_resource_len(pd, 0);
1975         rivafb_fix.smem_len = pci_resource_len(pd, 1);
1976
1977         {
1978                 /* enable IO and mem if not already done */
1979                 unsigned short cmd;
1980
1981                 pci_read_config_word(pd, PCI_COMMAND, &cmd);
1982                 cmd |= (PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
1983                 pci_write_config_word(pd, PCI_COMMAND, cmd);
1984         }
1985         
1986         rivafb_fix.mmio_start = pci_resource_start(pd, 0);
1987         rivafb_fix.smem_start = pci_resource_start(pd, 1);
1988
1989         default_par->ctrl_base = ioremap(rivafb_fix.mmio_start,
1990                                          rivafb_fix.mmio_len);
1991         if (!default_par->ctrl_base) {
1992                 printk(KERN_ERR PFX "cannot ioremap MMIO base\n");
1993                 ret = -EIO;
1994                 goto err_release_region;
1995         }
1996
1997         switch (default_par->riva.Architecture) {
1998         case NV_ARCH_03:
1999                 /* Riva128's PRAMIN is in the "framebuffer" space
2000                  * Since these cards were never made with more than 8 megabytes
2001                  * we can safely allocate this separately.
2002                  */
2003                 default_par->riva.PRAMIN = ioremap(rivafb_fix.smem_start + 0x00C00000, 0x00008000);
2004                 if (!default_par->riva.PRAMIN) {
2005                         printk(KERN_ERR PFX "cannot ioremap PRAMIN region\n");
2006                         ret = -EIO;
2007                         goto err_iounmap_ctrl_base;
2008                 }
2009                 break;
2010         case NV_ARCH_04:
2011         case NV_ARCH_10:
2012         case NV_ARCH_20:
2013         case NV_ARCH_30:
2014                 default_par->riva.PCRTC0 =
2015                         (u32 __iomem *)(default_par->ctrl_base + 0x00600000);
2016                 default_par->riva.PRAMIN =
2017                         (u32 __iomem *)(default_par->ctrl_base + 0x00710000);
2018                 break;
2019         }
2020         riva_common_setup(default_par);
2021
2022         if (default_par->riva.Architecture == NV_ARCH_03) {
2023                 default_par->riva.PCRTC = default_par->riva.PCRTC0
2024                                         = default_par->riva.PGRAPH;
2025         }
2026
2027         rivafb_fix.smem_len = riva_get_memlen(default_par) * 1024;
2028         default_par->dclk_max = riva_get_maxdclk(default_par) * 1000;
2029         info->screen_base = ioremap(rivafb_fix.smem_start,
2030                                     rivafb_fix.smem_len);
2031         if (!info->screen_base) {
2032                 printk(KERN_ERR PFX "cannot ioremap FB base\n");
2033                 ret = -EIO;
2034                 goto err_iounmap_pramin;
2035         }
2036
2037 #ifdef CONFIG_MTRR
2038         if (!nomtrr) {
2039                 default_par->mtrr.vram = mtrr_add(rivafb_fix.smem_start,
2040                                                   rivafb_fix.smem_len,
2041                                                   MTRR_TYPE_WRCOMB, 1);
2042                 if (default_par->mtrr.vram < 0) {
2043                         printk(KERN_ERR PFX "unable to setup MTRR\n");
2044                 } else {
2045                         default_par->mtrr.vram_valid = 1;
2046                         /* let there be speed */
2047                         printk(KERN_INFO PFX "RIVA MTRR set to ON\n");
2048                 }
2049         }
2050 #endif /* CONFIG_MTRR */
2051
2052         info->fbops = &riva_fb_ops;
2053         info->fix = rivafb_fix;
2054         riva_get_EDID(info, pd);
2055         riva_get_edidinfo(info);
2056
2057         ret=riva_set_fbinfo(info);
2058         if (ret < 0) {
2059                 printk(KERN_ERR PFX "error setting initial video mode\n");
2060                 goto err_iounmap_screen_base;
2061         }
2062
2063         fb_destroy_modedb(info->monspecs.modedb);
2064         info->monspecs.modedb = NULL;
2065
2066         pci_set_drvdata(pd, info);
2067
2068         if (backlight)
2069                 riva_bl_init(info->par);
2070
2071         ret = register_framebuffer(info);
2072         if (ret < 0) {
2073                 printk(KERN_ERR PFX
2074                         "error registering riva framebuffer\n");
2075                 goto err_iounmap_screen_base;
2076         }
2077
2078         printk(KERN_INFO PFX
2079                 "PCI nVidia %s framebuffer ver %s (%dMB @ 0x%lX)\n",
2080                 info->fix.id,
2081                 RIVAFB_VERSION,
2082                 info->fix.smem_len / (1024 * 1024),
2083                 info->fix.smem_start);
2084
2085         NVTRACE_LEAVE();
2086         return 0;
2087
2088 err_iounmap_screen_base:
2089 #ifdef CONFIG_FB_RIVA_I2C
2090         riva_delete_i2c_busses(info->par);
2091 #endif
2092         iounmap(info->screen_base);
2093 err_iounmap_pramin:
2094         if (default_par->riva.Architecture == NV_ARCH_03) 
2095                 iounmap(default_par->riva.PRAMIN);
2096 err_iounmap_ctrl_base:
2097         iounmap(default_par->ctrl_base);
2098 err_release_region:
2099         pci_release_regions(pd);
2100 err_disable_device:
2101 err_free_pixmap:
2102         kfree(info->pixmap.addr);
2103 err_framebuffer_release:
2104         framebuffer_release(info);
2105 err_ret:
2106         return ret;
2107 }
2108
2109 static void __exit rivafb_remove(struct pci_dev *pd)
2110 {
2111         struct fb_info *info = pci_get_drvdata(pd);
2112         struct riva_par *par = info->par;
2113         
2114         NVTRACE_ENTER();
2115
2116 #ifdef CONFIG_FB_RIVA_I2C
2117         riva_delete_i2c_busses(par);
2118         kfree(par->EDID);
2119 #endif
2120
2121         unregister_framebuffer(info);
2122
2123         riva_bl_exit(info);
2124
2125 #ifdef CONFIG_MTRR
2126         if (par->mtrr.vram_valid)
2127                 mtrr_del(par->mtrr.vram, info->fix.smem_start,
2128                          info->fix.smem_len);
2129 #endif /* CONFIG_MTRR */
2130
2131         iounmap(par->ctrl_base);
2132         iounmap(info->screen_base);
2133         if (par->riva.Architecture == NV_ARCH_03)
2134                 iounmap(par->riva.PRAMIN);
2135         pci_release_regions(pd);
2136         kfree(info->pixmap.addr);
2137         framebuffer_release(info);
2138         pci_set_drvdata(pd, NULL);
2139         NVTRACE_LEAVE();
2140 }
2141
2142 /* ------------------------------------------------------------------------- *
2143  *
2144  * initialization
2145  *
2146  * ------------------------------------------------------------------------- */
2147
2148 #ifndef MODULE
2149 static int __init rivafb_setup(char *options)
2150 {
2151         char *this_opt;
2152
2153         NVTRACE_ENTER();
2154         if (!options || !*options)
2155                 return 0;
2156
2157         while ((this_opt = strsep(&options, ",")) != NULL) {
2158                 if (!strncmp(this_opt, "forceCRTC", 9)) {
2159                         char *p;
2160                         
2161                         p = this_opt + 9;
2162                         if (!*p || !*(++p)) continue; 
2163                         forceCRTC = *p - '0';
2164                         if (forceCRTC < 0 || forceCRTC > 1) 
2165                                 forceCRTC = -1;
2166                 } else if (!strncmp(this_opt, "flatpanel", 9)) {
2167                         flatpanel = 1;
2168                 } else if (!strncmp(this_opt, "backlight:", 10)) {
2169                         backlight = simple_strtoul(this_opt+10, NULL, 0);
2170 #ifdef CONFIG_MTRR
2171                 } else if (!strncmp(this_opt, "nomtrr", 6)) {
2172                         nomtrr = 1;
2173 #endif
2174                 } else if (!strncmp(this_opt, "strictmode", 10)) {
2175                         strictmode = 1;
2176                 } else if (!strncmp(this_opt, "noaccel", 7)) {
2177                         noaccel = 1;
2178                 } else
2179                         mode_option = this_opt;
2180         }
2181         NVTRACE_LEAVE();
2182         return 0;
2183 }
2184 #endif /* !MODULE */
2185
2186 static struct pci_driver rivafb_driver = {
2187         .name           = "rivafb",
2188         .id_table       = rivafb_pci_tbl,
2189         .probe          = rivafb_probe,
2190         .remove         = __exit_p(rivafb_remove),
2191 };
2192
2193
2194
2195 /* ------------------------------------------------------------------------- *
2196  *
2197  * modularization
2198  *
2199  * ------------------------------------------------------------------------- */
2200
2201 static int __devinit rivafb_init(void)
2202 {
2203 #ifndef MODULE
2204         char *option = NULL;
2205
2206         if (fb_get_options("rivafb", &option))
2207                 return -ENODEV;
2208         rivafb_setup(option);
2209 #endif
2210         return pci_register_driver(&rivafb_driver);
2211 }
2212
2213
2214 module_init(rivafb_init);
2215
2216 #ifdef MODULE
2217 static void __exit rivafb_exit(void)
2218 {
2219         pci_unregister_driver(&rivafb_driver);
2220 }
2221
2222 module_exit(rivafb_exit);
2223 #endif /* MODULE */
2224
2225 module_param(noaccel, bool, 0);
2226 MODULE_PARM_DESC(noaccel, "bool: disable acceleration");
2227 module_param(flatpanel, int, 0);
2228 MODULE_PARM_DESC(flatpanel, "Enables experimental flat panel support for some chipsets. (0 or 1=enabled) (default=0)");
2229 module_param(forceCRTC, int, 0);
2230 MODULE_PARM_DESC(forceCRTC, "Forces usage of a particular CRTC in case autodetection fails. (0 or 1) (default=autodetect)");
2231 #ifdef CONFIG_MTRR
2232 module_param(nomtrr, bool, 0);
2233 MODULE_PARM_DESC(nomtrr, "Disables MTRR support (0 or 1=disabled) (default=0)");
2234 #endif
2235 module_param(strictmode, bool, 0);
2236 MODULE_PARM_DESC(strictmode, "Only use video modes from EDID");
2237
2238 MODULE_AUTHOR("Ani Joshi, maintainer");
2239 MODULE_DESCRIPTION("Framebuffer driver for nVidia Riva 128, TNT, TNT2, and the GeForce series");
2240 MODULE_LICENSE("GPL");