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