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