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