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