import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.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  * Initial template from skeletonfb.c, created 28 Dec 1997 by Geert Uytterhoeven
18  * Includes riva_hw.c from nVidia, see copyright below.
19  * KGI code provided the basis for state storage, init, and mode switching.
20  *
21  * This file is subject to the terms and conditions of the GNU General Public
22  * License.  See the file COPYING in the main directory of this archive
23  * for more details.
24  *
25  * Known bugs and issues:
26  *      restoring text mode fails
27  *      doublescan modes are broken
28  *      option 'noaccel' has no effect
29  */
30
31 #include <linux/config.h>
32 #include <linux/module.h>
33 #include <linux/kernel.h>
34 #include <linux/errno.h>
35 #include <linux/string.h>
36 #include <linux/mm.h>
37 #include <linux/selection.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 #include <linux/console.h>
45 #ifdef CONFIG_MTRR
46 #include <asm/mtrr.h>
47 #endif
48 #include "rivafb.h"
49 #include "nvreg.h"
50
51 #ifndef CONFIG_PCI              /* sanity check */
52 #error This driver requires PCI support.
53 #endif
54
55
56
57 /* version number of this driver */
58 #define RIVAFB_VERSION "0.9.3"
59
60
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 DEFAULT_CURSOR_BLINK_RATE       (40)
95 #define CURSOR_HIDE_DELAY               (20)
96 #define CURSOR_SHOW_DELAY               (3)
97
98 #define CURSOR_COLOR            0x7fff
99 #define TRANSPARENT_COLOR       0x0000
100 #define MAX_CURS                32
101
102
103
104 /* ------------------------------------------------------------------------- *
105  *
106  * prototypes
107  *
108  * ------------------------------------------------------------------------- */
109
110 static void rivafb_blank(int blank, struct fb_info *info);
111
112 extern void riva_setup_accel(struct rivafb_info *rinfo);
113 extern inline void wait_for_idle(struct rivafb_info *rinfo);
114
115
116
117 /* ------------------------------------------------------------------------- *
118  *
119  * card identification
120  *
121  * ------------------------------------------------------------------------- */
122
123 enum riva_chips {
124         CH_RIVA_128 = 0,
125         CH_RIVA_TNT,
126         CH_RIVA_TNT2,
127         CH_RIVA_UTNT2,  /* UTNT2 */
128         CH_RIVA_VTNT2,  /* VTNT2 */
129         CH_RIVA_UVTNT2, /* VTNT2 */
130         CH_RIVA_ITNT2,  /* ITNT2 */
131         CH_GEFORCE_SDR,
132         CH_GEFORCE_DDR,
133         CH_QUADRO,
134         CH_GEFORCE2_MX,
135         CH_QUADRO2_MXR,
136         CH_GEFORCE2_GTS,
137         CH_GEFORCE2_ULTRA,
138         CH_QUADRO2_PRO,
139         CH_GEFORCE2_GO,
140         CH_GEFORCE3,
141         CH_GEFORCE3_1,
142         CH_GEFORCE3_2,
143         CH_QUADRO_DDC
144 };
145
146 /* directly indexed by riva_chips enum, above */
147 static struct riva_chip_info {
148         const char *name;
149         unsigned arch_rev;
150 } riva_chip_info[] __devinitdata = {
151         { "RIVA-128", NV_ARCH_03 },
152         { "RIVA-TNT", NV_ARCH_04 },
153         { "RIVA-TNT2", NV_ARCH_04 },
154         { "RIVA-UTNT2", NV_ARCH_04 },
155         { "RIVA-VTNT2", NV_ARCH_04 },
156         { "RIVA-UVTNT2", NV_ARCH_04 },
157         { "RIVA-ITNT2", NV_ARCH_04 },
158         { "GeForce-SDR", NV_ARCH_10},
159         { "GeForce-DDR", NV_ARCH_10},
160         { "Quadro", NV_ARCH_10},
161         { "GeForce2-MX", NV_ARCH_10},
162         { "Quadro2-MXR", NV_ARCH_10},
163         { "GeForce2-GTS", NV_ARCH_10},
164         { "GeForce2-ULTRA", NV_ARCH_10},
165         { "Quadro2-PRO", NV_ARCH_10},
166         { "GeForce2-Go", NV_ARCH_10},
167         { "GeForce3", NV_ARCH_20}, 
168         { "GeForce3 Ti 200", NV_ARCH_20},
169         { "GeForce3 Ti 500", NV_ARCH_20},
170         { "Quadro DDC", NV_ARCH_20}
171 };
172
173 static struct pci_device_id rivafb_pci_tbl[] __devinitdata = {
174         { PCI_VENDOR_ID_NVIDIA_SGS, PCI_DEVICE_ID_NVIDIA_SGS_RIVA128,
175           PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_RIVA_128 },
176         { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_TNT,
177           PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_RIVA_TNT },
178         { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_TNT2,
179           PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_RIVA_TNT2 },
180         { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_UTNT2,
181           PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_RIVA_UTNT2 },
182         { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_VTNT2,
183           PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_RIVA_VTNT2 },
184         { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_UVTNT2,
185           PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_RIVA_VTNT2 },
186         { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_ITNT2,
187           PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_RIVA_ITNT2 },
188         { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE_SDR,
189           PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_GEFORCE_SDR },
190         { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE_DDR,
191           PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_GEFORCE_DDR },
192         { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO,
193           PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_QUADRO },
194         { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE2_MX,
195           PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_GEFORCE2_MX },
196         { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE2_MX2,
197           PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_GEFORCE2_MX },
198         { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO2_MXR,
199           PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_QUADRO2_MXR },
200         { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE2_GTS,
201           PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_GEFORCE2_GTS },
202         { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE2_GTS2,
203           PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_GEFORCE2_GTS },
204         { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE2_ULTRA,
205           PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_GEFORCE2_ULTRA },
206         { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO2_PRO,
207           PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_QUADRO2_PRO },
208         { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE2_GO,
209           PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_GEFORCE2_GO },
210         { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE3,
211           PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_GEFORCE3 },
212         { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE3_1,
213           PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_GEFORCE3_1 },
214         { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE3_2,
215           PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_GEFORCE3_2 },
216         { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO_DDC,
217           PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_QUADRO_DDC },
218         { 0, } /* terminate list */
219 };
220 MODULE_DEVICE_TABLE(pci, rivafb_pci_tbl);
221
222
223
224 /* ------------------------------------------------------------------------- *
225  *
226  * framebuffer related structures
227  *
228  * ------------------------------------------------------------------------- */
229
230 #ifdef FBCON_HAS_CFB8
231 extern struct display_switch fbcon_riva8;
232 #endif
233 #ifdef FBCON_HAS_CFB16
234 extern struct display_switch fbcon_riva16;
235 #endif
236 #ifdef FBCON_HAS_CFB32
237 extern struct display_switch fbcon_riva32;
238 #endif
239
240 #if 0
241 /* describes the state of a Riva board */
242 struct rivafb_par {
243         struct riva_regs state; /* state of hw board */
244         __u32 visual;           /* FB_VISUAL_xxx */
245         unsigned depth;         /* bpp of current mode */
246 };
247 #endif
248
249 struct riva_cursor {
250         int enable;
251         int on;
252         int vbl_cnt;
253         int last_move_delay;
254         int blink_rate;
255         struct {
256                 u16 x, y;
257         } pos, size;
258         unsigned short image[MAX_CURS*MAX_CURS];
259         struct timer_list *timer;
260 };
261
262
263
264 /* ------------------------------------------------------------------------- *
265  *
266  * global variables
267  *
268  * ------------------------------------------------------------------------- */
269
270 struct rivafb_info *riva_boards = NULL;
271
272 /* command line data, set in rivafb_setup() */
273 static char fontname[40] __initdata = { 0 };
274 static char noaccel __initdata = 0;
275 static char nomove = 0;
276 static char nohwcursor __initdata = 0;
277 static char noblink = 0;
278 #ifdef CONFIG_MTRR
279 static char nomtrr __initdata = 0;
280 #endif
281
282 #ifndef MODULE
283 static char *mode_option __initdata = NULL;
284 #else
285 static char *font = NULL;
286 #endif
287
288 static struct fb_var_screeninfo rivafb_default_var = {
289         xres:           640,
290         yres:           480,
291         xres_virtual:   640,
292         yres_virtual:   480,
293         xoffset:        0,
294         yoffset:        0,
295         bits_per_pixel: 8,
296         grayscale:      0,
297         red:            {0, 6, 0},
298         green:          {0, 6, 0},
299         blue:           {0, 6, 0},
300         transp:         {0, 0, 0},
301         nonstd:         0,
302         activate:       0,
303         height:         -1,
304         width:          -1,
305         accel_flags:    0,
306         pixclock:       39721,
307         left_margin:    40,
308         right_margin:   24,
309         upper_margin:   32,
310         lower_margin:   11,
311         hsync_len:      96,
312         vsync_len:      2,
313         sync:           0,
314         vmode:          FB_VMODE_NONINTERLACED
315 };
316
317 /* from GGI */
318 static const struct riva_regs reg_template = {
319         {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,        /* ATTR */
320          0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
321          0x41, 0x01, 0x0F, 0x00, 0x00},
322         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        /* CRT  */
323          0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
324          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE3,        /* 0x10 */
325          0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
326          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        /* 0x20 */
327          0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
328          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        /* 0x30 */
329          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
330          0x00,                                                  /* 0x40 */
331          },
332         {0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x05, 0x0F,        /* GRA  */
333          0xFF},
334         {0x03, 0x01, 0x0F, 0x00, 0x0E},                         /* SEQ  */
335         0xEB                                                    /* MISC */
336 };
337
338
339
340 /* ------------------------------------------------------------------------- *
341  *
342  * MMIO access macros
343  *
344  * ------------------------------------------------------------------------- */
345
346 static inline void CRTCout(struct rivafb_info *rinfo, unsigned char index,
347                            unsigned char val)
348 {
349         VGA_WR08(rinfo->riva.PCIO, 0x3d4, index);
350         VGA_WR08(rinfo->riva.PCIO, 0x3d5, val);
351 }
352
353 static inline unsigned char CRTCin(struct rivafb_info *rinfo,
354                                    unsigned char index)
355 {
356         VGA_WR08(rinfo->riva.PCIO, 0x3d4, index);
357         return (VGA_RD08(rinfo->riva.PCIO, 0x3d5));
358 }
359
360 static inline void GRAout(struct rivafb_info *rinfo, unsigned char index,
361                           unsigned char val)
362 {
363         VGA_WR08(rinfo->riva.PVIO, 0x3ce, index);
364         VGA_WR08(rinfo->riva.PVIO, 0x3cf, val);
365 }
366
367 static inline unsigned char GRAin(struct rivafb_info *rinfo,
368                                   unsigned char index)
369 {
370         VGA_WR08(rinfo->riva.PVIO, 0x3ce, index);
371         return (VGA_RD08(rinfo->riva.PVIO, 0x3cf));
372 }
373
374 static inline void SEQout(struct rivafb_info *rinfo, unsigned char index,
375                           unsigned char val)
376 {
377         VGA_WR08(rinfo->riva.PVIO, 0x3c4, index);
378         VGA_WR08(rinfo->riva.PVIO, 0x3c5, val);
379 }
380
381 static inline unsigned char SEQin(struct rivafb_info *rinfo,
382                                   unsigned char index)
383 {
384         VGA_WR08(rinfo->riva.PVIO, 0x3c4, index);
385         return (VGA_RD08(rinfo->riva.PVIO, 0x3c5));
386 }
387
388 static inline void ATTRout(struct rivafb_info *rinfo, unsigned char index,
389                            unsigned char val)
390 {
391         VGA_WR08(rinfo->riva.PCIO, 0x3c0, index);
392         VGA_WR08(rinfo->riva.PCIO, 0x3c0, val);
393 }
394
395 static inline unsigned char ATTRin(struct rivafb_info *rinfo,
396                                    unsigned char index)
397 {
398         VGA_WR08(rinfo->riva.PCIO, 0x3c0, index);
399         return (VGA_RD08(rinfo->riva.PCIO, 0x3c1));
400 }
401
402 static inline void MISCout(struct rivafb_info *rinfo, unsigned char val)
403 {
404         VGA_WR08(rinfo->riva.PVIO, 0x3c2, val);
405 }
406
407 static inline unsigned char MISCin(struct rivafb_info *rinfo)
408 {
409         return (VGA_RD08(rinfo->riva.PVIO, 0x3cc));
410 }
411
412
413
414 /* ------------------------------------------------------------------------- *
415  *
416  * cursor stuff
417  *
418  * ------------------------------------------------------------------------- */
419
420 /**
421  * riva_cursor_timer_handler - blink timer
422  * @dev_addr: pointer to rivafb_info object containing info for current riva board
423  *
424  * DESCRIPTION:
425  * Cursor blink timer.
426  */
427 static void riva_cursor_timer_handler(unsigned long dev_addr)
428 {
429         struct rivafb_info *rinfo = (struct rivafb_info *)dev_addr;
430
431         if (!rinfo->cursor) return;
432
433         if (!rinfo->cursor->enable) goto out;
434
435         if (rinfo->cursor->last_move_delay < 1000)
436                 rinfo->cursor->last_move_delay++;
437
438         if (rinfo->cursor->vbl_cnt && --rinfo->cursor->vbl_cnt == 0) {
439                 rinfo->cursor->on ^= 1;
440                 if (rinfo->cursor->on)
441                         *(rinfo->riva.CURSORPOS) = (rinfo->cursor->pos.x & 0xFFFF)
442                                                    | (rinfo->cursor->pos.y << 16);
443                 rinfo->riva.ShowHideCursor(&rinfo->riva, rinfo->cursor->on);
444                 if (!noblink)
445                         rinfo->cursor->vbl_cnt = rinfo->cursor->blink_rate;
446         }
447 out:
448         rinfo->cursor->timer->expires = jiffies + (HZ / 100);
449         add_timer(rinfo->cursor->timer);
450 }
451
452 /**
453  * rivafb_init_cursor - allocates cursor structure and starts blink timer
454  * @rinfo: pointer to rivafb_info object containing info for current riva board
455  *
456  * DESCRIPTION:
457  * Allocates cursor structure and starts blink timer.
458  *
459  * RETURNS:
460  * Pointer to allocated cursor structure.
461  *
462  * CALLED FROM:
463  * rivafb_init_one()
464  */
465 static struct riva_cursor * __init rivafb_init_cursor(struct rivafb_info *rinfo)
466 {
467         struct riva_cursor *cursor;
468
469         cursor = kmalloc(sizeof(struct riva_cursor), GFP_KERNEL);
470         if (!cursor) return 0;
471         memset(cursor, 0, sizeof(*cursor));
472
473         cursor->timer = kmalloc(sizeof(*cursor->timer), GFP_KERNEL);
474         if (!cursor->timer) {
475                 kfree(cursor);
476                 return 0;
477         }
478         memset(cursor->timer, 0, sizeof(*cursor->timer));
479
480         cursor->blink_rate = DEFAULT_CURSOR_BLINK_RATE;
481
482         init_timer(cursor->timer);
483         cursor->timer->expires = jiffies + (HZ / 100);
484         cursor->timer->data = (unsigned long)rinfo;
485         cursor->timer->function = riva_cursor_timer_handler;
486         add_timer(cursor->timer);
487
488         return cursor;
489 }
490
491 /**
492  * rivafb_exit_cursor - stops blink timer and releases cursor structure
493  * @rinfo: pointer to rivafb_info object containing info for current riva board
494  *
495  * DESCRIPTION:
496  * Stops blink timer and releases cursor structure.
497  *
498  * CALLED FROM:
499  * rivafb_init_one()
500  * rivafb_remove_one()
501  */
502 static void rivafb_exit_cursor(struct rivafb_info *rinfo)
503 {
504         struct riva_cursor *cursor = rinfo->cursor;
505
506         if (cursor) {
507                 if (cursor->timer) {
508                         del_timer_sync(cursor->timer);
509                         kfree(cursor->timer);
510                 }
511                 kfree(cursor);
512                 rinfo->cursor = 0;
513         }
514 }
515
516 /**
517  * rivafb_download_cursor - writes cursor shape into card registers
518  * @rinfo: pointer to rivafb_info object containing info for current riva board
519  *
520  * DESCRIPTION:
521  * Writes cursor shape into card registers.
522  *
523  * CALLED FROM:
524  * riva_load_video_mode()
525  */
526 static void rivafb_download_cursor(struct rivafb_info *rinfo)
527 {
528         int i, save;
529         int *image;
530         
531         if (!rinfo->cursor) return;
532
533         image = (int *)rinfo->cursor->image;
534         save = rinfo->riva.ShowHideCursor(&rinfo->riva, 0);
535         for (i = 0; i < (MAX_CURS*MAX_CURS*2)/sizeof(int); i++)
536                 writel(image[i], rinfo->riva.CURSOR + i);
537
538         rinfo->riva.ShowHideCursor(&rinfo->riva, save);
539 }
540
541 /**
542  * rivafb_create_cursor - sets rectangular cursor
543  * @rinfo: pointer to rivafb_info object containing info for current riva board
544  * @width: cursor width in pixels
545  * @height: cursor height in pixels
546  *
547  * DESCRIPTION:
548  * Sets rectangular cursor.
549  *
550  * CALLED FROM:
551  * rivafb_set_font()
552  * rivafb_set_var()
553  */
554 static void rivafb_create_cursor(struct rivafb_info *rinfo, int width, int height)
555 {
556         struct riva_cursor *c = rinfo->cursor;
557         int i, j, idx;
558
559         if (c) {
560                 if (width <= 0 || height <= 0) {
561                         width = 8;
562                         height = 16;
563                 }
564                 if (width > MAX_CURS) width = MAX_CURS;
565                 if (height > MAX_CURS) height = MAX_CURS;
566
567                 c->size.x = width;
568                 c->size.y = height;
569                 
570                 idx = 0;
571
572                 for (i = 0; i < height; i++) {
573                         for (j = 0; j < width; j++,idx++)
574                                 c->image[idx] = CURSOR_COLOR;
575                         for (j = width; j < MAX_CURS; j++,idx++)
576                                 c->image[idx] = TRANSPARENT_COLOR;
577                 }
578                 for (i = height; i < MAX_CURS; i++)
579                         for (j = 0; j < MAX_CURS; j++,idx++)
580                                 c->image[idx] = TRANSPARENT_COLOR;
581         }
582 }
583
584 /**
585  * rivafb_set_font - change font size
586  * @p: pointer to display object
587  * @width: font width in pixels
588  * @height: font height in pixels
589  *
590  * DESCRIPTION:
591  * Callback function called if font settings changed.
592  *
593  * RETURNS:
594  * 1 (Always succeeds.)
595  */
596 static int rivafb_set_font(struct display *p, int width, int height)
597 {
598         struct rivafb_info *fb = (struct rivafb_info *)(p->fb_info);
599
600         rivafb_create_cursor(fb, width, height);
601         return 1;
602 }
603
604 /**
605  * rivafb_cursor - cursor handler
606  * @p: pointer to display object
607  * @mode: cursor mode (see CM_*)
608  * @x: cursor x coordinate in characters
609  * @y: cursor y coordinate in characters
610  *
611  * DESCRIPTION:
612  * Cursor handler.
613  */
614 static void rivafb_cursor(struct display *p, int mode, int x, int y)
615 {
616         struct rivafb_info *rinfo = (struct rivafb_info *)(p->fb_info);
617         struct riva_cursor *c = rinfo->cursor;
618
619         if (!c) return;
620
621         x = x * fontwidth(p) - p->var.xoffset;
622         y = y * fontheight(p) - p->var.yoffset;
623
624         if (c->pos.x == x && c->pos.y == y && (mode == CM_ERASE) == !c->enable)
625                 return;
626
627         c->enable = 0;
628         if (c->on) rinfo->riva.ShowHideCursor(&rinfo->riva, 0);
629                 
630         c->pos.x = x;
631         c->pos.y = y;
632
633         switch (mode) {
634         case CM_ERASE:
635                 c->on = 0;
636                 break;
637         case CM_DRAW:
638         case CM_MOVE:
639                 if (c->last_move_delay <= 1) { /* rapid cursor movement */
640                         c->vbl_cnt = CURSOR_SHOW_DELAY;
641                 } else {
642                         *(rinfo->riva.CURSORPOS) = (x & 0xFFFF) | (y << 16);
643                         rinfo->riva.ShowHideCursor(&rinfo->riva, 1);
644                         if (!noblink) c->vbl_cnt = CURSOR_HIDE_DELAY;
645                         c->on = 1;
646                 }
647                 c->last_move_delay = 0;
648                 c->enable = 1;
649                 break;
650         }
651 }
652
653
654
655 /* ------------------------------------------------------------------------- *
656  *
657  * general utility functions
658  *
659  * ------------------------------------------------------------------------- */
660
661 /**
662  * riva_set_dispsw - sets dispsw
663  * @rinfo: pointer to internal driver struct for a given Riva card
664  * @disp: pointer to display object
665  *
666  * DESCRIPTION:
667  * Sets up console low level operations depending on the current? color depth
668  * of the display.
669  *
670  * CALLED FROM:
671  * rivafb_set_var()
672  * rivafb_switch()
673  * riva_init_disp()
674  */
675 static void riva_set_dispsw(struct rivafb_info *rinfo, struct display *disp)
676 {
677         int accel = disp->var.accel_flags & FB_ACCELF_TEXT;
678
679         DPRINTK("ENTER\n");
680
681         assert(rinfo != NULL);
682
683         disp->dispsw_data = NULL;
684
685         disp->screen_base = rinfo->fb_base;
686         disp->type = FB_TYPE_PACKED_PIXELS;
687         disp->type_aux = 0;
688         disp->ypanstep = 1;
689         disp->ywrapstep = 0;
690         disp->can_soft_blank = 1;
691         disp->inverse = 0;
692
693         switch (disp->var.bits_per_pixel) {
694 #ifdef FBCON_HAS_CFB8
695         case 8:
696                 rinfo->dispsw = accel ? fbcon_riva8 : fbcon_cfb8;
697                 disp->dispsw = &rinfo->dispsw;
698                 disp->line_length = disp->var.xres_virtual;
699                 disp->visual = FB_VISUAL_PSEUDOCOLOR;
700                 break;
701 #endif
702 #ifdef FBCON_HAS_CFB16
703         case 16:
704                 rinfo->dispsw = accel ? fbcon_riva16 : fbcon_cfb16;
705                 disp->dispsw_data = &rinfo->con_cmap.cfb16;
706                 disp->dispsw = &rinfo->dispsw;
707                 disp->line_length = disp->var.xres_virtual * 2;
708                 disp->visual = FB_VISUAL_DIRECTCOLOR;
709                 break;
710 #endif
711 #ifdef FBCON_HAS_CFB32
712         case 32:
713                 rinfo->dispsw = accel ? fbcon_riva32 : fbcon_cfb32;
714                 disp->dispsw_data = rinfo->con_cmap.cfb32;
715                 disp->dispsw = &rinfo->dispsw;
716                 disp->line_length = disp->var.xres_virtual * 4;
717                 disp->visual = FB_VISUAL_DIRECTCOLOR;
718                 break;
719 #endif
720         default:
721                 DPRINTK("Setting fbcon_dummy renderer\n");
722                 rinfo->dispsw = fbcon_dummy;
723                 disp->dispsw = &rinfo->dispsw;
724         }
725
726         /* FIXME: verify that the above code sets dsp->* fields correctly */
727
728         if (rinfo->cursor) {
729                 rinfo->dispsw.cursor = rivafb_cursor;
730                 rinfo->dispsw.set_font = rivafb_set_font;
731         }
732
733         DPRINTK("EXIT\n");
734 }
735
736 /**
737  * riva_wclut - set CLUT entry
738  * @chip: pointer to RIVA_HW_INST object
739  * @regnum: register number
740  * @red: red component
741  * @green: green component
742  * @blue: blue component
743  *
744  * DESCRIPTION:
745  * Sets color register @regnum.
746  *
747  * CALLED FROM:
748  * riva_setcolreg()
749  */
750 static void riva_wclut(RIVA_HW_INST *chip,
751                        unsigned char regnum, unsigned char red,
752                        unsigned char green, unsigned char blue)
753 {
754         VGA_WR08(chip->PDIO, 0x3c8, regnum);
755         VGA_WR08(chip->PDIO, 0x3c9, red);
756         VGA_WR08(chip->PDIO, 0x3c9, green);
757         VGA_WR08(chip->PDIO, 0x3c9, blue);
758 }
759
760 /**
761  * riva_save_state - saves current chip state
762  * @rinfo: pointer to rivafb_info object containing info for current riva board
763  * @regs: pointer to riva_regs object
764  *
765  * DESCRIPTION:
766  * Saves current chip state to @regs.
767  *
768  * CALLED FROM:
769  * rivafb_init_one()
770  */
771 /* from GGI */
772 static void riva_save_state(struct rivafb_info *rinfo, struct riva_regs *regs)
773 {
774         int i;
775
776         rinfo->riva.LockUnlock(&rinfo->riva, 0);
777
778         rinfo->riva.UnloadStateExt(&rinfo->riva, &regs->ext);
779
780         regs->misc_output = MISCin(rinfo);
781
782         for (i = 0; i < NUM_CRT_REGS; i++) {
783                 regs->crtc[i] = CRTCin(rinfo, i);
784         }
785
786         for (i = 0; i < NUM_ATC_REGS; i++) {
787                 regs->attr[i] = ATTRin(rinfo, i);
788         }
789
790         for (i = 0; i < NUM_GRC_REGS; i++) {
791                 regs->gra[i] = GRAin(rinfo, i);
792         }
793
794         for (i = 0; i < NUM_SEQ_REGS; i++) {
795                 regs->seq[i] = SEQin(rinfo, i);
796         }
797 }
798
799 /**
800  * riva_load_state - loads current chip state
801  * @rinfo: pointer to rivafb_info object containing info for current riva board
802  * @regs: pointer to riva_regs object
803  *
804  * DESCRIPTION:
805  * Loads chip state from @regs.
806  *
807  * CALLED FROM:
808  * riva_load_video_mode()
809  * rivafb_init_one()
810  * rivafb_remove_one()
811  */
812 /* from GGI */
813 static void riva_load_state(struct rivafb_info *rinfo, struct riva_regs *regs)
814 {
815         int i;
816         RIVA_HW_STATE *state = &regs->ext;
817
818         CRTCout(rinfo, 0x11, 0x00);
819
820         rinfo->riva.LockUnlock(&rinfo->riva, 0);
821
822         rinfo->riva.LoadStateExt(&rinfo->riva, state);
823
824         MISCout(rinfo, regs->misc_output);
825
826         for (i = 0; i < NUM_CRT_REGS; i++) {
827                 switch (i) {
828                 case 0x19:
829                 case 0x20 ... 0x40:
830                         break;
831                 default:
832                         CRTCout(rinfo, i, regs->crtc[i]);
833                 }
834         }
835
836         for (i = 0; i < NUM_ATC_REGS; i++) {
837                 ATTRout(rinfo, i, regs->attr[i]);
838         }
839
840         for (i = 0; i < NUM_GRC_REGS; i++) {
841                 GRAout(rinfo, i, regs->gra[i]);
842         }
843
844         for (i = 0; i < NUM_SEQ_REGS; i++) {
845                 SEQout(rinfo, i, regs->seq[i]);
846         }
847 }
848
849 /**
850  * riva_load_video_mode - calculate timings
851  * @rinfo: pointer to rivafb_info object containing info for current riva board
852  * @video_mode: video mode to set
853  *
854  * DESCRIPTION:
855  * Calculate some timings and then send em off to riva_load_state().
856  *
857  * CALLED FROM:
858  * rivafb_set_var()
859  */
860 static void riva_load_video_mode(struct rivafb_info *rinfo,
861                                  struct fb_var_screeninfo *video_mode)
862 {
863         struct riva_regs newmode;
864         int bpp, width, hDisplaySize, hDisplay, hStart,
865             hEnd, hTotal, height, vDisplay, vStart, vEnd, vTotal, dotClock;
866
867         /* time to calculate */
868
869         rivafb_blank(1, (struct fb_info *)rinfo);
870
871         bpp = video_mode->bits_per_pixel;
872         if (bpp == 16 && video_mode->green.length == 5)
873                 bpp = 15;
874         width = video_mode->xres_virtual;
875         hDisplaySize = video_mode->xres;
876         hDisplay = (hDisplaySize / 8) - 1;
877         hStart = (hDisplaySize + video_mode->right_margin) / 8 + 2;
878         hEnd = (hDisplaySize + video_mode->right_margin +
879                 video_mode->hsync_len) / 8 - 1;
880         hTotal = (hDisplaySize + video_mode->right_margin +
881                   video_mode->hsync_len + video_mode->left_margin) / 8 - 1;
882         height = video_mode->yres_virtual;
883         vDisplay = video_mode->yres - 1;
884         vStart = video_mode->yres + video_mode->lower_margin - 1;
885         vEnd = video_mode->yres + video_mode->lower_margin +
886                video_mode->vsync_len - 1;
887         vTotal = video_mode->yres + video_mode->lower_margin +
888                  video_mode->vsync_len + video_mode->upper_margin + 2;
889         dotClock = 1000000000 / video_mode->pixclock;
890
891         memcpy(&newmode, &reg_template, sizeof(struct riva_regs));
892
893         newmode.crtc[0x0] = Set8Bits (hTotal - 4);
894         newmode.crtc[0x1] = Set8Bits (hDisplay);
895         newmode.crtc[0x2] = Set8Bits (hDisplay);
896         newmode.crtc[0x3] = SetBitField (hTotal, 4: 0, 4:0) | SetBit (7);
897         newmode.crtc[0x4] = Set8Bits (hStart);
898         newmode.crtc[0x5] = SetBitField (hTotal, 5: 5, 7:7)
899                 | SetBitField (hEnd, 4: 0, 4:0);
900         newmode.crtc[0x6] = SetBitField (vTotal, 7: 0, 7:0);
901         newmode.crtc[0x7] = SetBitField (vTotal, 8: 8, 0:0)
902                 | SetBitField (vDisplay, 8: 8, 1:1)
903                 | SetBitField (vStart, 8: 8, 2:2)
904                 | SetBitField (vDisplay, 8: 8, 3:3)
905                 | SetBit (4)
906                 | SetBitField (vTotal, 9: 9, 5:5)
907                 | SetBitField (vDisplay, 9: 9, 6:6)
908                 | SetBitField (vStart, 9: 9, 7:7);
909         newmode.crtc[0x9] = SetBitField (vDisplay, 9: 9, 5:5)
910                 | SetBit (6);
911         newmode.crtc[0x10] = Set8Bits (vStart);
912         newmode.crtc[0x11] = SetBitField (vEnd, 3: 0, 3:0)
913                 | SetBit (5);
914         newmode.crtc[0x12] = Set8Bits (vDisplay);
915         newmode.crtc[0x13] = ((width / 8) * ((bpp + 1) / 8)) & 0xFF;
916         newmode.crtc[0x15] = Set8Bits (vDisplay);
917         newmode.crtc[0x16] = Set8Bits (vTotal + 1);
918
919         newmode.ext.bpp = bpp;
920         newmode.ext.width = width;
921         newmode.ext.height = height;
922
923         rinfo->riva.CalcStateExt(&rinfo->riva, &newmode.ext, bpp, width,
924                                   hDisplaySize, hDisplay, hStart, hEnd,
925                                   hTotal, height, vDisplay, vStart, vEnd,
926                                   vTotal, dotClock);
927
928         rinfo->current_state = newmode;
929         riva_load_state(rinfo, &rinfo->current_state);
930
931         rinfo->riva.LockUnlock(&rinfo->riva, 0); /* important for HW cursor */
932         rivafb_download_cursor(rinfo);
933 }
934
935 /**
936  * riva_board_list_add - maintains board list
937  * @board_list: root node of list of boards
938  * @new_node: new node to be added
939  *
940  * DESCRIPTION:
941  * Adds @new_node to the list referenced by @board_list.
942  *
943  * RETURNS:
944  * New root node
945  *
946  * CALLED FROM:
947  * rivafb_init_one()
948  */
949 static struct rivafb_info *riva_board_list_add(struct rivafb_info *board_list,
950                                                struct rivafb_info *new_node)
951 {
952         struct rivafb_info *i_p = board_list;
953
954         new_node->next = NULL;
955
956         if (board_list == NULL)
957                 return new_node;
958
959         while (i_p->next != NULL)
960                 i_p = i_p->next;
961         i_p->next = new_node;
962
963         return board_list;
964 }
965
966 /**
967  * riva_board_list_del - maintains board list
968  * @board_list: root node of list of boards
969  * @del_node: node to be removed
970  *
971  * DESCRIPTION:
972  * Removes @del_node from the list referenced by @board_list.
973  *
974  * RETURNS:
975  * New root node
976  *
977  * CALLED FROM:
978  * rivafb_remove_one()
979  */
980 static struct rivafb_info *riva_board_list_del(struct rivafb_info *board_list,
981                                                struct rivafb_info *del_node)
982 {
983         struct rivafb_info *i_p = board_list;
984
985         if (board_list == del_node)
986                 return del_node->next;
987
988         while (i_p->next != del_node)
989                 i_p = i_p->next;
990         i_p->next = del_node->next;
991
992         return board_list;
993 }
994
995 /**
996  * rivafb_do_maximize - 
997  * @rinfo: pointer to rivafb_info object containing info for current riva board
998  * @var:
999  * @v:
1000  * @nom:
1001  * @den:
1002  *
1003  * DESCRIPTION:
1004  * .
1005  *
1006  * RETURNS:
1007  * -EINVAL on failure, 0 on success
1008  * 
1009  *
1010  * CALLED FROM:
1011  * rivafb_set_var()
1012  */
1013 static int rivafb_do_maximize(struct rivafb_info *rinfo,
1014                               struct fb_var_screeninfo *var,
1015                               struct fb_var_screeninfo *v,
1016                               int nom, int den)
1017 {
1018         static struct {
1019                 int xres, yres;
1020         } modes[] = {
1021                 {1600, 1280},
1022                 {1280, 1024},
1023                 {1024, 768},
1024                 {800, 600},
1025                 {640, 480},
1026                 {-1, -1}
1027         };
1028         int i;
1029
1030         /* use highest possible virtual resolution */
1031         if (v->xres_virtual == -1 && v->yres_virtual == -1) {
1032                 printk(KERN_WARNING PFX
1033                        "using maximum available virtual resolution\n");
1034                 for (i = 0; modes[i].xres != -1; i++) {
1035                         if (modes[i].xres * nom / den * modes[i].yres <
1036                             rinfo->ram_amount / 2)
1037                                 break;
1038                 }
1039                 if (modes[i].xres == -1) {
1040                         printk(KERN_ERR PFX
1041                                "could not find a virtual resolution that fits into video memory!!\n");
1042                         DPRINTK("EXIT - EINVAL error\n");
1043                         return -EINVAL;
1044                 }
1045                 v->xres_virtual = modes[i].xres;
1046                 v->yres_virtual = modes[i].yres;
1047
1048                 printk(KERN_INFO PFX
1049                        "virtual resolution set to maximum of %dx%d\n",
1050                        v->xres_virtual, v->yres_virtual);
1051         } else if (v->xres_virtual == -1) {
1052                 v->xres_virtual = (rinfo->ram_amount * den /
1053                         (nom * v->yres_virtual * 2)) & ~15;
1054                 printk(KERN_WARNING PFX
1055                        "setting virtual X resolution to %d\n", v->xres_virtual);
1056         } else if (v->yres_virtual == -1) {
1057                 v->xres_virtual = (v->xres_virtual + 15) & ~15;
1058                 v->yres_virtual = rinfo->ram_amount * den /
1059                         (nom * v->xres_virtual * 2);
1060                 printk(KERN_WARNING PFX
1061                        "setting virtual Y resolution to %d\n", v->yres_virtual);
1062         } else {
1063                 v->xres_virtual = (v->xres_virtual + 15) & ~15;
1064                 if (v->xres_virtual * nom / den * v->yres_virtual > rinfo->ram_amount) {
1065                         printk(KERN_ERR PFX
1066                                "mode %dx%dx%d rejected...resolution too high to fit into video memory!\n",
1067                                var->xres, var->yres, var->bits_per_pixel);
1068                         DPRINTK("EXIT - EINVAL error\n");
1069                         return -EINVAL;
1070                 }
1071         }
1072         
1073         if (v->xres_virtual * nom / den >= 8192) {
1074                 printk(KERN_WARNING PFX
1075                        "virtual X resolution (%d) is too high, lowering to %d\n",
1076                        v->xres_virtual, 8192 * den / nom - 16);
1077                 v->xres_virtual = 8192 * den / nom - 16;
1078         }
1079         
1080         if (v->xres_virtual < v->xres) {
1081                 printk(KERN_ERR PFX
1082                        "virtual X resolution (%d) is smaller than real\n", v->xres_virtual);
1083                 return -EINVAL;
1084         }
1085
1086         if (v->yres_virtual < v->yres) {
1087                 printk(KERN_ERR PFX
1088                        "virtual Y resolution (%d) is smaller than real\n", v->yres_virtual);
1089                 return -EINVAL;
1090         }
1091         
1092         return 0;
1093 }
1094
1095
1096
1097 /* ------------------------------------------------------------------------- *
1098  *
1099  * internal fb_ops helper functions
1100  *
1101  * ------------------------------------------------------------------------- */
1102
1103 /**
1104  * riva_get_cmap_len - query current color map length
1105  * @var: standard kernel fb changeable data
1106  *
1107  * DESCRIPTION:
1108  * Get current color map length.
1109  *
1110  * RETURNS:
1111  * Length of color map
1112  *
1113  * CALLED FROM:
1114  * riva_getcolreg()
1115  * riva_setcolreg()
1116  * rivafb_get_cmap()
1117  * rivafb_set_cmap()
1118  */
1119 static int riva_get_cmap_len(const struct fb_var_screeninfo *var)
1120 {
1121         int rc = 16;            /* reasonable default */
1122
1123         assert(var != NULL);
1124
1125         switch (var->bits_per_pixel) {
1126 #ifdef FBCON_HAS_CFB8
1127         case 8:
1128                 rc = 256;       /* pseudocolor... 256 entries HW palette */
1129                 break;
1130 #endif
1131 #ifdef FBCON_HAS_CFB16
1132         case 15:
1133                 rc = 15;        /* fix for 15 bpp depths on Riva 128 based cards */
1134                 break;
1135         case 16:
1136                 rc = 16;        /* directcolor... 16 entries SW palette */
1137                 break;          /* Mystique: truecolor, 16 entries SW palette, HW palette hardwired into 1:1 mapping */
1138 #endif
1139 #ifdef FBCON_HAS_CFB32
1140         case 32:
1141                 rc = 16;        /* directcolor... 16 entries SW palette */
1142                 break;          /* Mystique: truecolor, 16 entries SW palette, HW palette hardwired into 1:1 mapping */
1143 #endif
1144         default:
1145                 /* should not occur */
1146                 break;
1147         }
1148
1149         return rc;
1150 }
1151
1152 /**
1153  * riva_getcolreg
1154  * @regno: register index
1155  * @red: red component
1156  * @green: green component
1157  * @blue: blue component
1158  * @transp: transparency
1159  * @info: pointer to rivafb_info object containing info for current riva board
1160  *
1161  * DESCRIPTION:
1162  * Read a single color register and split it into colors/transparent.
1163  * The return values must have a 16 bit magnitude.
1164  *
1165  * RETURNS:
1166  * Return != 0 for invalid regno.
1167  *
1168  * CALLED FROM:
1169  * rivafb_get_cmap()
1170  * rivafb_switch()
1171  * fbcmap.c:fb_get_cmap()
1172  *      fbgen.c:fbgen_get_cmap()
1173  *      fbgen.c:fbgen_switch()
1174  */
1175 static int riva_getcolreg(unsigned regno, unsigned *red, unsigned *green,
1176                           unsigned *blue, unsigned *transp,
1177                           struct fb_info *info)
1178 {
1179         struct rivafb_info *rivainfo = (struct rivafb_info *)info;
1180
1181         if (regno >= riva_get_cmap_len(&rivainfo->currcon_display->var))
1182                 return 1;
1183
1184         *red = rivainfo->palette[regno].red;
1185         *green = rivainfo->palette[regno].green;
1186         *blue = rivainfo->palette[regno].blue;
1187         *transp = 0;
1188
1189         return 0;
1190 }
1191
1192 /**
1193  * riva_setcolreg
1194  * @regno: register index
1195  * @red: red component
1196  * @green: green component
1197  * @blue: blue component
1198  * @transp: transparency
1199  * @info: pointer to rivafb_info object containing info for current riva board
1200  *
1201  * DESCRIPTION:
1202  * Set a single color register. The values supplied have a 16 bit
1203  * magnitude.
1204  *
1205  * RETURNS:
1206  * Return != 0 for invalid regno.
1207  *
1208  * CALLED FROM:
1209  * rivafb_set_cmap()
1210  * fbcmap.c:fb_set_cmap()
1211  *      fbgen.c:fbgen_get_cmap()
1212  *      fbgen.c:fbgen_install_cmap()
1213  *              fbgen.c:fbgen_set_var()
1214  *              fbgen.c:fbgen_switch()
1215  *              fbgen.c:fbgen_blank()
1216  *      fbgen.c:fbgen_blank()
1217  */
1218 static int riva_setcolreg(unsigned regno, unsigned red, unsigned green,
1219                           unsigned blue, unsigned transp,
1220                           struct fb_info *info)
1221 {
1222         struct rivafb_info *rivainfo = (struct rivafb_info *)info;
1223         RIVA_HW_INST *chip = &rivainfo->riva;
1224         struct display *p;
1225
1226         DPRINTK("ENTER\n");
1227
1228         assert(rivainfo != NULL);
1229         assert(rivainfo->currcon_display != NULL);
1230
1231         p = rivainfo->currcon_display;
1232
1233         if (regno >= riva_get_cmap_len(&p->var))
1234                 return -EINVAL;
1235
1236         rivainfo->palette[regno].red = red;
1237         rivainfo->palette[regno].green = green;
1238         rivainfo->palette[regno].blue = blue;
1239
1240         if (p->var.grayscale) {
1241                 /* gray = 0.30*R + 0.59*G + 0.11*B */
1242                 red = green = blue =
1243                     (red * 77 + green * 151 + blue * 28) >> 8;
1244         }
1245
1246         switch (p->var.bits_per_pixel) {
1247 #ifdef FBCON_HAS_CFB8
1248         case 8:
1249                 /* "transparent" stuff is completely ignored. */
1250                 riva_wclut(chip, regno, red >> 8, green >> 8, blue >> 8);
1251                 break;
1252 #endif /* FBCON_HAS_CFB8 */
1253 #ifdef FBCON_HAS_CFB16
1254         case 16:
1255                 assert(regno < 16);
1256                 if (p->var.green.length == 5) {
1257                         /* 0rrrrrgg gggbbbbb */
1258                         rivainfo->con_cmap.cfb16[regno] =
1259                             ((red & 0xf800) >> 1) |
1260                             ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
1261                 } else {
1262                         /* rrrrrggg gggbbbbb */
1263                         rivainfo->con_cmap.cfb16[regno] =
1264                             ((red & 0xf800) >> 0) |
1265                             ((green & 0xf800) >> 5) | ((blue & 0xf800) >> 11);
1266                 }
1267                 break;
1268 #endif /* FBCON_HAS_CFB16 */
1269 #ifdef FBCON_HAS_CFB32
1270         case 32:
1271                 assert(regno < 16);
1272                 rivainfo->con_cmap.cfb32[regno] =
1273                     ((red & 0xff00) << 8) |
1274                     ((green & 0xff00)) | ((blue & 0xff00) >> 8);
1275                 break;
1276 #endif /* FBCON_HAS_CFB32 */
1277         default:
1278                 /* do nothing */
1279                 break;
1280         }
1281
1282         return 0;
1283 }
1284
1285
1286
1287 /* ------------------------------------------------------------------------- *
1288  *
1289  * framebuffer operations
1290  *
1291  * ------------------------------------------------------------------------- */
1292
1293 static int rivafb_get_fix(struct fb_fix_screeninfo *fix, int con,
1294                           struct fb_info *info)
1295 {
1296         struct rivafb_info *rivainfo = (struct rivafb_info *)info;
1297         struct display *p;
1298
1299         DPRINTK("ENTER\n");
1300
1301         assert(fix != NULL);
1302         assert(info != NULL);
1303         assert(rivainfo->drvr_name && rivainfo->drvr_name[0]);
1304         assert(rivainfo->fb_base_phys > 0);
1305         assert(rivainfo->ram_amount > 0);
1306
1307         p = (con < 0) ? rivainfo->info.disp : &fb_display[con];
1308
1309         memset(fix, 0, sizeof(struct fb_fix_screeninfo));
1310         sprintf(fix->id, "nVidia %s", rivainfo->drvr_name);
1311
1312         fix->type = p->type;
1313         fix->type_aux = p->type_aux;
1314         fix->visual = p->visual;
1315
1316         fix->xpanstep = 1;
1317         fix->ypanstep = 1;
1318         fix->ywrapstep = 0;     /* FIXME: no ywrap for now */
1319
1320         fix->line_length = p->line_length;
1321
1322         fix->mmio_start = rivainfo->ctrl_base_phys;
1323         fix->mmio_len = rivainfo->base0_region_size;
1324         fix->smem_start = rivainfo->fb_base_phys;
1325         fix->smem_len = rivainfo->ram_amount;
1326
1327         switch (rivainfo->riva.Architecture) {
1328         case NV_ARCH_03:
1329                 fix->accel = FB_ACCEL_NV3;
1330                 break;
1331         case NV_ARCH_04:        /* riva_hw.c now doesn't distinguish between TNT & TNT2 */
1332                 fix->accel = FB_ACCEL_NV4;
1333                 break;
1334         case NV_ARCH_10:        /* FIXME: ID for GeForce */
1335         case NV_ARCH_20:
1336                 fix->accel = FB_ACCEL_NV4;
1337                 break;
1338
1339         }
1340
1341         DPRINTK("EXIT, returning 0\n");
1342
1343         return 0;
1344 }
1345
1346 static int rivafb_get_var(struct fb_var_screeninfo *var, int con,
1347                           struct fb_info *info)
1348 {
1349         struct rivafb_info *rivainfo = (struct rivafb_info *)info;
1350
1351         DPRINTK("ENTER\n");
1352
1353         assert(info != NULL);
1354         assert(var != NULL);
1355
1356         *var = (con < 0) ? rivainfo->disp.var : fb_display[con].var;
1357
1358         DPRINTK("EXIT, returning 0\n");
1359
1360         return 0;
1361 }
1362
1363 static int rivafb_set_var(struct fb_var_screeninfo *var, int con,
1364                           struct fb_info *info)
1365 {
1366         struct rivafb_info *rivainfo = (struct rivafb_info *)info;
1367         struct display *dsp;
1368         struct fb_var_screeninfo v;
1369         int nom, den;           /* translating from pixels->bytes */
1370         int accel;
1371         unsigned chgvar = 0;
1372
1373         DPRINTK("ENTER\n");
1374
1375         assert(info != NULL);
1376         assert(var != NULL);
1377
1378         DPRINTK("Requested: %dx%dx%d\n", var->xres, var->yres,
1379                 var->bits_per_pixel);
1380         DPRINTK("  virtual: %dx%d\n", var->xres_virtual,
1381                 var->yres_virtual);
1382         DPRINTK("   offset: (%d,%d)\n", var->xoffset, var->yoffset);
1383         DPRINTK("grayscale: %d\n", var->grayscale);
1384
1385         dsp = (con < 0) ? rivainfo->info.disp : &fb_display[con];
1386         assert(dsp != NULL);
1387
1388         /* if var has changed, we should call changevar() later */
1389         if (con >= 0) {
1390                 chgvar = ((dsp->var.xres != var->xres) ||
1391                           (dsp->var.yres != var->yres) ||
1392                           (dsp->var.xres_virtual != var->xres_virtual) ||
1393                           (dsp->var.yres_virtual != var->yres_virtual) ||
1394                           (dsp->var.accel_flags != var->accel_flags) ||
1395                           (dsp->var.bits_per_pixel != var->bits_per_pixel)
1396                           || memcmp(&dsp->var.red, &var->red,
1397                                     sizeof(var->red))
1398                           || memcmp(&dsp->var.green, &var->green,
1399                                     sizeof(var->green))
1400                           || memcmp(&dsp->var.blue, &var->blue,
1401                                     sizeof(var->blue)));
1402         }
1403
1404         memcpy(&v, var, sizeof(v));
1405
1406         accel = v.accel_flags & FB_ACCELF_TEXT;
1407
1408         switch (v.bits_per_pixel) {
1409 #ifdef FBCON_HAS_CFB8
1410         case 1 ... 8:
1411                 v.bits_per_pixel = 8;
1412                 nom = 1;
1413                 den = 1;
1414                 v.red.offset = 0;
1415                 v.red.length = 8;
1416                 v.green.offset = 0;
1417                 v.green.length = 8;
1418                 v.blue.offset = 0;
1419                 v.blue.length = 8;
1420                 break;
1421 #endif
1422 #ifdef FBCON_HAS_CFB16
1423         case 9 ... 15:
1424                 v.green.length = 5;
1425                 /* fall through */
1426         case 16:
1427                 v.bits_per_pixel = 16;
1428                 nom = 2;
1429                 den = 1;
1430                 if (v.green.length == 5) {
1431                         /* 0rrrrrgg gggbbbbb */
1432                         v.red.offset = 10;
1433                         v.green.offset = 5;
1434                         v.blue.offset = 0;
1435                         v.red.length = 5;
1436                         v.green.length = 5;
1437                         v.blue.length = 5;
1438                 } else {
1439                         /* rrrrrggg gggbbbbb */
1440                         v.red.offset = 11;
1441                         v.green.offset = 5;
1442                         v.blue.offset = 0;
1443                         v.red.length = 5;
1444                         v.green.length = 6;
1445                         v.blue.length = 5;
1446                 }
1447                 break;
1448 #endif
1449 #ifdef FBCON_HAS_CFB32
1450         case 17 ... 32:
1451                 v.bits_per_pixel = 32;
1452                 nom = 4;
1453                 den = 1;
1454                 v.red.offset = 16;
1455                 v.green.offset = 8;
1456                 v.blue.offset = 0;
1457                 v.red.length = 8;
1458                 v.green.length = 8;
1459                 v.blue.length = 8;
1460                 break;
1461 #endif
1462         default:
1463                 printk(KERN_ERR PFX
1464                        "mode %dx%dx%d rejected...color depth not supported.\n",
1465                        var->xres, var->yres, var->bits_per_pixel);
1466                 DPRINTK("EXIT, returning -EINVAL\n");
1467                 return -EINVAL;
1468         }
1469
1470         if (rivafb_do_maximize(rivainfo, var, &v, nom, den) < 0)
1471                 return -EINVAL;
1472
1473         if (v.xoffset < 0)
1474                 v.xoffset = 0;
1475         if (v.yoffset < 0)
1476                 v.yoffset = 0;
1477
1478         /* truncate xoffset and yoffset to maximum if too high */
1479         if (v.xoffset > v.xres_virtual - v.xres)
1480                 v.xoffset = v.xres_virtual - v.xres - 1;
1481
1482         if (v.yoffset > v.yres_virtual - v.yres)
1483                 v.yoffset = v.yres_virtual - v.yres - 1;
1484
1485         v.red.msb_right =
1486             v.green.msb_right =
1487             v.blue.msb_right =
1488             v.transp.offset = v.transp.length = v.transp.msb_right = 0;
1489
1490         switch (v.activate & FB_ACTIVATE_MASK) {
1491         case FB_ACTIVATE_TEST:
1492                 DPRINTK("EXIT - FB_ACTIVATE_TEST\n");
1493                 return 0;
1494         case FB_ACTIVATE_NXTOPEN:       /* ?? */
1495         case FB_ACTIVATE_NOW:
1496                 break;          /* continue */
1497         default:
1498                 DPRINTK("EXIT - unknown activation type\n");
1499                 return -EINVAL; /* unknown */
1500         }
1501
1502         memcpy(&dsp->var, &v, sizeof(v));
1503         if (chgvar) {
1504                 riva_set_dispsw(rivainfo, dsp);
1505
1506                 if (accel) {
1507                         if (nomove)
1508                                 dsp->scrollmode = SCROLL_YNOMOVE;
1509                         else
1510                                 dsp->scrollmode = 0;
1511                 } else
1512                         dsp->scrollmode = SCROLL_YREDRAW;
1513
1514                 if (info && info->changevar)
1515                         info->changevar(con);
1516         }
1517
1518         rivafb_create_cursor(rivainfo, fontwidth(dsp), fontheight(dsp));
1519         riva_load_video_mode(rivainfo, &v);
1520         if (accel) riva_setup_accel(rivainfo);
1521
1522         DPRINTK("EXIT, returning 0\n");
1523         return 0;
1524 }
1525
1526 static int rivafb_get_cmap(struct fb_cmap *cmap, int kspc, int con,
1527                            struct fb_info *info)
1528 {
1529         struct rivafb_info *rivainfo = (struct rivafb_info *)info;
1530         struct display *dsp;
1531
1532         DPRINTK("ENTER\n");
1533
1534         assert(rivainfo != NULL);
1535         assert(cmap != NULL);
1536
1537         dsp = (con < 0) ? rivainfo->info.disp : &fb_display[con];
1538
1539         if (con == rivainfo->currcon) { /* current console? */
1540                 int rc = fb_get_cmap(cmap, kspc, riva_getcolreg, info);
1541                 DPRINTK("EXIT - returning %d\n", rc);
1542                 return rc;
1543         } else if (dsp->cmap.len)       /* non default colormap? */
1544                 fb_copy_cmap(&dsp->cmap, cmap, kspc ? 0 : 2);
1545         else
1546                 fb_copy_cmap(fb_default_cmap
1547                              (riva_get_cmap_len(&dsp->var)), cmap,
1548                              kspc ? 0 : 2);
1549
1550         DPRINTK("EXIT, returning 0\n");
1551
1552         return 0;
1553 }
1554
1555 static int rivafb_set_cmap(struct fb_cmap *cmap, int kspc, int con,
1556                            struct fb_info *info)
1557 {
1558         struct rivafb_info *rivainfo = (struct rivafb_info *)info;
1559         struct display *dsp;
1560         unsigned int cmap_len;
1561
1562         DPRINTK("ENTER\n");
1563         
1564         assert(rivainfo != NULL);
1565         assert(cmap != NULL);
1566
1567         dsp = (con < 0) ? rivainfo->info.disp : &fb_display[con];
1568
1569         cmap_len = riva_get_cmap_len(&dsp->var);
1570         if (dsp->cmap.len != cmap_len) {
1571                 int err = fb_alloc_cmap(&dsp->cmap, cmap_len, 0);
1572                 if (err) {
1573                         DPRINTK("EXIT - returning %d\n", err);
1574                         return err;
1575                 }
1576         }
1577         if (con == rivainfo->currcon) { /* current console? */
1578                 int rc = fb_set_cmap(cmap, kspc, riva_setcolreg, info);
1579                 DPRINTK("EXIT - returning %d\n", rc);
1580                 return rc;
1581         } else
1582                 fb_copy_cmap(cmap, &dsp->cmap, kspc ? 0 : 1);
1583
1584         DPRINTK("EXIT, returning 0\n");
1585
1586         return 0;
1587 }
1588
1589 /**
1590  * rivafb_pan_display
1591  * @var: standard kernel fb changeable data
1592  * @con: TODO
1593  * @info: pointer to rivafb_info object containing info for current riva board
1594  *
1595  * DESCRIPTION:
1596  * Pan (or wrap, depending on the `vmode' field) the display using the
1597  * `xoffset' and `yoffset' fields of the `var' structure.
1598  * If the values don't fit, return -EINVAL.
1599  *
1600  * This call looks only at xoffset, yoffset and the FB_VMODE_YWRAP flag
1601  */
1602 static int rivafb_pan_display(struct fb_var_screeninfo *var, int con,
1603                               struct fb_info *info)
1604 {
1605         unsigned int base;
1606         struct display *dsp;
1607         struct rivafb_info *rivainfo = (struct rivafb_info *)info;
1608
1609         DPRINTK("ENTER\n");
1610
1611         assert(rivainfo != NULL);
1612
1613         if (var->xoffset > (var->xres_virtual - var->xres))
1614                 return -EINVAL;
1615         if (var->yoffset > (var->yres_virtual - var->yres))
1616                 return -EINVAL;
1617
1618         dsp = (con < 0) ? rivainfo->info.disp : &fb_display[con];
1619
1620         if (var->vmode & FB_VMODE_YWRAP) {
1621                 if (var->yoffset < 0
1622                     || var->yoffset >= dsp->var.yres_virtual
1623                     || var->xoffset) return -EINVAL;
1624         } else {
1625                 if (var->xoffset + dsp->var.xres > dsp->var.xres_virtual ||
1626                     var->yoffset + dsp->var.yres > dsp->var.yres_virtual)
1627                         return -EINVAL;
1628         }
1629
1630         base = var->yoffset * dsp->line_length + var->xoffset;
1631
1632         if (con == rivainfo->currcon) {
1633                 rivainfo->riva.SetStartAddress(&rivainfo->riva, base);
1634         }
1635
1636         dsp->var.xoffset = var->xoffset;
1637         dsp->var.yoffset = var->yoffset;
1638
1639         if (var->vmode & FB_VMODE_YWRAP)
1640                 dsp->var.vmode |= FB_VMODE_YWRAP;
1641         else
1642                 dsp->var.vmode &= ~FB_VMODE_YWRAP;
1643
1644         DPRINTK("EXIT, returning 0\n");
1645
1646         return 0;
1647 }
1648
1649 static int rivafb_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
1650                         unsigned long arg, int con, struct fb_info *info)
1651 {
1652         struct rivafb_info *rivainfo = (struct rivafb_info *)info;
1653
1654         DPRINTK("ENTER\n");
1655
1656         assert(rivainfo != NULL);
1657
1658         /* no rivafb-specific ioctls */
1659
1660         DPRINTK("EXIT, returning -EINVAL\n");
1661
1662         return -EINVAL;
1663 }
1664
1665 static int rivafb_rasterimg(struct fb_info *info, int start)
1666 {
1667         struct rivafb_info *rinfo = (struct rivafb_info *)info;
1668
1669         wait_for_idle(rinfo);
1670
1671         return 0;
1672 }
1673
1674 static int rivafb_switch(int con, struct fb_info *info)
1675 {
1676         struct rivafb_info *rivainfo = (struct rivafb_info *)info;
1677         struct fb_cmap *cmap;
1678         struct display *dsp;
1679         
1680         DPRINTK("ENTER\n");
1681         
1682         assert(rivainfo != NULL);
1683
1684         dsp = (con < 0) ? rivainfo->info.disp : &fb_display[con];
1685
1686         if (rivainfo->currcon >= 0) {
1687                 /* Do we have to save the colormap? */
1688                 cmap = &(rivainfo->currcon_display->cmap);
1689                 DPRINTK("switch1: con = %d, cmap.len = %d\n",
1690                          rivainfo->currcon, cmap->len);
1691
1692                 if (cmap->len) {
1693                         DPRINTK("switch1a: %p %p %p %p\n", cmap->red,
1694                                  cmap->green, cmap->blue, cmap->transp);
1695                         fb_get_cmap(cmap, 1, riva_getcolreg, info);
1696                 }
1697         }
1698         rivainfo->currcon = con;
1699         rivainfo->currcon_display = dsp;
1700
1701         rivafb_set_var(&dsp->var, con, info);
1702         riva_set_dispsw(rivainfo, dsp);
1703
1704         DPRINTK("EXIT, returning 0\n");
1705         return 0;
1706 }
1707
1708 static int rivafb_updatevar(int con, struct fb_info *info)
1709 {
1710         int rc;
1711
1712         DPRINTK("ENTER\n");
1713
1714         rc = (con < 0) ? -EINVAL : rivafb_pan_display(&fb_display[con].var,
1715                                                       con, info);
1716         DPRINTK("EXIT, returning %d\n", rc);
1717         return rc;
1718 }
1719
1720 static void rivafb_blank(int blank, struct fb_info *info)
1721 {
1722         unsigned char tmp, vesa;
1723         struct rivafb_info *rinfo = (struct rivafb_info *)info;
1724
1725         DPRINTK("ENTER\n");
1726
1727         assert(rinfo != NULL);
1728
1729         tmp = SEQin(rinfo, 0x01) & ~0x20;       /* screen on/off */
1730         vesa = CRTCin(rinfo, 0x1a) & ~0xc0;     /* sync on/off */
1731
1732         if (blank) {
1733                 tmp |= 0x20;
1734                 switch (blank - 1) {
1735                 case VESA_NO_BLANKING:
1736                         break;
1737                 case VESA_VSYNC_SUSPEND:
1738                         vesa |= 0x80;
1739                         break;
1740                 case VESA_HSYNC_SUSPEND:
1741                         vesa |= 0x40;
1742                         break;
1743                 case VESA_POWERDOWN:
1744                         vesa |= 0xc0;
1745                         break;
1746                 }
1747         }
1748
1749         SEQout(rinfo, 0x01, tmp);
1750         CRTCout(rinfo, 0x1a, vesa);
1751
1752         DPRINTK("EXIT\n");
1753 }
1754
1755
1756
1757 /* ------------------------------------------------------------------------- *
1758  *
1759  * initialization helper functions
1760  *
1761  * ------------------------------------------------------------------------- */
1762
1763 /* kernel interface */
1764 static struct fb_ops riva_fb_ops = {
1765         owner:          THIS_MODULE,
1766         fb_get_fix:     rivafb_get_fix,
1767         fb_get_var:     rivafb_get_var,
1768         fb_set_var:     rivafb_set_var,
1769         fb_get_cmap:    rivafb_get_cmap,
1770         fb_set_cmap:    rivafb_set_cmap,
1771         fb_pan_display: rivafb_pan_display,
1772         fb_ioctl:       rivafb_ioctl,
1773         fb_rasterimg:   rivafb_rasterimg,
1774 };
1775
1776 static int __devinit riva_init_disp_var(struct rivafb_info *rinfo)
1777 {
1778 #ifndef MODULE
1779         if (mode_option)
1780                 fb_find_mode(&rinfo->disp.var, &rinfo->info, mode_option,
1781                              NULL, 0, NULL, 8);
1782 #endif
1783         return 0;
1784 }
1785
1786 static int __devinit riva_init_disp(struct rivafb_info *rinfo)
1787 {
1788         struct fb_info *info;
1789         struct display *disp;
1790
1791         DPRINTK("ENTER\n");
1792
1793         assert(rinfo != NULL);
1794
1795         info = &rinfo->info;
1796         disp = &rinfo->disp;
1797
1798         disp->var = rivafb_default_var;
1799         
1800         if (noaccel)
1801                 disp->var.accel_flags &= ~FB_ACCELF_TEXT;
1802         else
1803                 disp->var.accel_flags |= FB_ACCELF_TEXT;
1804         
1805         info->disp = disp;
1806
1807         /* FIXME: assure that disp->cmap is completely filled out */
1808
1809         rinfo->currcon_display = disp;
1810
1811         if ((riva_init_disp_var(rinfo)) < 0) {
1812                 DPRINTK("EXIT, returning -1\n");
1813                 return -1;
1814         }
1815
1816         riva_set_dispsw(rinfo, disp);
1817
1818         DPRINTK("EXIT, returning 0\n");
1819         return 0;
1820
1821 }
1822
1823 static int __devinit riva_set_fbinfo(struct rivafb_info *rinfo)
1824 {
1825         struct fb_info *info;
1826
1827         assert(rinfo != NULL);
1828
1829         info = &rinfo->info;
1830
1831         strcpy(info->modename, rinfo->drvr_name);
1832         info->node = -1;
1833         info->flags = FBINFO_FLAG_DEFAULT;
1834         info->fbops = &riva_fb_ops;
1835
1836         /* FIXME: set monspecs to what??? */
1837
1838         info->display_fg = NULL;
1839         strncpy(info->fontname, fontname, sizeof(info->fontname));
1840         info->fontname[sizeof(info->fontname) - 1] = 0;
1841
1842         info->changevar = NULL;
1843         info->switch_con = rivafb_switch;
1844         info->updatevar = rivafb_updatevar;
1845         info->blank = rivafb_blank;
1846
1847         if (riva_init_disp(rinfo) < 0)  /* must be done last */
1848                 return -1;
1849
1850         return 0;
1851 }
1852
1853
1854
1855 /* ------------------------------------------------------------------------- *
1856  *
1857  * PCI bus
1858  *
1859  * ------------------------------------------------------------------------- */
1860
1861 static int __devinit rivafb_init_one(struct pci_dev *pd,
1862                                      const struct pci_device_id *ent)
1863 {
1864         struct rivafb_info *rinfo;
1865         struct riva_chip_info *rci = &riva_chip_info[ent->driver_data];
1866
1867         assert(pd != NULL);
1868         assert(rci != NULL);
1869
1870         rinfo = kmalloc(sizeof(struct rivafb_info), GFP_KERNEL);
1871         if (!rinfo)
1872                 goto err_out;
1873
1874         memset(rinfo, 0, sizeof(struct rivafb_info));
1875
1876         rinfo->drvr_name = rci->name;
1877         rinfo->riva.Architecture = rci->arch_rev;
1878
1879         rinfo->pd = pd;
1880         rinfo->base0_region_size = pci_resource_len(pd, 0);
1881         rinfo->base1_region_size = pci_resource_len(pd, 1);
1882
1883         rinfo->ctrl_base_phys = pci_resource_start(rinfo->pd, 0);
1884         rinfo->fb_base_phys = pci_resource_start(rinfo->pd, 1);
1885
1886         if (!request_mem_region(rinfo->ctrl_base_phys,
1887                                 rinfo->base0_region_size, "rivafb")) {
1888                 printk(KERN_ERR PFX "cannot reserve MMIO region\n");
1889                 goto err_out_kfree;
1890         }
1891
1892         rinfo->ctrl_base = ioremap(rinfo->ctrl_base_phys,
1893                                    rinfo->base0_region_size);
1894         if (!rinfo->ctrl_base) {
1895                 printk(KERN_ERR PFX "cannot ioremap MMIO base\n");
1896                 goto err_out_free_base1;
1897         }
1898         
1899         rinfo->riva.EnableIRQ = 0;
1900         rinfo->riva.PRAMDAC = (unsigned *)(rinfo->ctrl_base + 0x00680000);
1901         rinfo->riva.PFB = (unsigned *)(rinfo->ctrl_base + 0x00100000);
1902         rinfo->riva.PFIFO = (unsigned *)(rinfo->ctrl_base + 0x00002000);
1903         rinfo->riva.PGRAPH = (unsigned *)(rinfo->ctrl_base + 0x00400000);
1904         rinfo->riva.PEXTDEV = (unsigned *)(rinfo->ctrl_base + 0x00101000);
1905         rinfo->riva.PTIMER = (unsigned *)(rinfo->ctrl_base + 0x00009000);
1906         rinfo->riva.PMC = (unsigned *)(rinfo->ctrl_base + 0x00000000);
1907         rinfo->riva.FIFO = (unsigned *)(rinfo->ctrl_base + 0x00800000);
1908
1909         rinfo->riva.PCIO = (U008 *)(rinfo->ctrl_base + 0x00601000);
1910         rinfo->riva.PDIO = (U008 *)(rinfo->ctrl_base + 0x00681000);
1911         rinfo->riva.PVIO = (U008 *)(rinfo->ctrl_base + 0x000C0000);
1912
1913         rinfo->riva.IO = (MISCin(rinfo) & 0x01) ? 0x3D0 : 0x3B0;
1914
1915         if (rinfo->riva.Architecture == NV_ARCH_03) {
1916                 /*
1917                  * We have to map the full BASE_1 aperture for Riva128's
1918                  * because they use the PRAMIN set in "framebuffer" space
1919                  */
1920                 if (!request_mem_region(rinfo->fb_base_phys,
1921                                         rinfo->base1_region_size, "rivafb")) {
1922                         printk(KERN_ERR PFX "cannot reserve FB region\n");
1923                         goto err_out_free_base0;
1924                 }
1925         
1926                 rinfo->fb_base = ioremap(rinfo->fb_base_phys,
1927                                          rinfo->base1_region_size);
1928                 if (!rinfo->fb_base) {
1929                         printk(KERN_ERR PFX "cannot ioremap FB base\n");
1930                         goto err_out_iounmap_ctrl;
1931                 }
1932         }
1933
1934
1935         switch (rinfo->riva.Architecture) {
1936         case NV_ARCH_03:
1937                 rinfo->riva.PRAMIN = (unsigned *)(rinfo->fb_base + 0x00C00000);
1938                 break;
1939         case NV_ARCH_04:
1940         case NV_ARCH_10:
1941         case NV_ARCH_20:
1942                 rinfo->riva.PCRTC = (unsigned *)(rinfo->ctrl_base + 0x00600000);
1943                 rinfo->riva.PRAMIN = (unsigned *)(rinfo->ctrl_base + 0x00710000);
1944                 break;
1945         }
1946
1947         RivaGetConfig(&rinfo->riva);
1948
1949         rinfo->ram_amount = rinfo->riva.RamAmountKBytes * 1024;
1950         rinfo->dclk_max = rinfo->riva.MaxVClockFreqKHz * 1000;
1951
1952         if (rinfo->riva.Architecture != NV_ARCH_03) {
1953                 /*
1954                  * Now the _normal_ chipsets can just map the amount of
1955                  * real physical ram instead of the whole aperture
1956                  */
1957                 if (!request_mem_region(rinfo->fb_base_phys,
1958                                         rinfo->ram_amount, "rivafb")) {
1959                         printk(KERN_ERR PFX "cannot reserve FB region\n");
1960                         goto err_out_free_base0;
1961                 }
1962         
1963                 rinfo->fb_base = ioremap(rinfo->fb_base_phys,
1964                                          rinfo->ram_amount);
1965                 if (!rinfo->fb_base) {
1966                         printk(KERN_ERR PFX "cannot ioremap FB base\n");
1967                         goto err_out_iounmap_ctrl;
1968                 }
1969         }
1970
1971 #ifdef CONFIG_MTRR
1972         if (!nomtrr) {
1973                 rinfo->mtrr.vram = mtrr_add(rinfo->fb_base_phys,
1974                                             rinfo->ram_amount,
1975                                             MTRR_TYPE_WRCOMB, 1);
1976                 if (rinfo->mtrr.vram < 0) {
1977                         printk(KERN_ERR PFX "unable to setup MTRR\n");
1978                 } else {
1979                         rinfo->mtrr.vram_valid = 1;
1980                         /* let there be speed */
1981                         printk(KERN_INFO PFX "RIVA MTRR set to ON\n");
1982                 }
1983         }
1984 #endif /* CONFIG_MTRR */
1985
1986         /* unlock io */
1987         CRTCout(rinfo, 0x11, 0xFF);     /* vgaHWunlock() + riva unlock (0x7F) */
1988         rinfo->riva.LockUnlock(&rinfo->riva, 0);
1989
1990         riva_save_state(rinfo, &rinfo->initial_state);
1991
1992         if (!nohwcursor) rinfo->cursor = rivafb_init_cursor(rinfo);
1993
1994         if (riva_set_fbinfo(rinfo) < 0) {
1995                 printk(KERN_ERR PFX "error setting initial video mode\n");
1996                 goto err_out_cursor;
1997         }
1998
1999         if (register_framebuffer((struct fb_info *)rinfo) < 0) {
2000                 printk(KERN_ERR PFX
2001                         "error registering riva framebuffer\n");
2002                 goto err_out_load_state;
2003         }
2004
2005         riva_boards = riva_board_list_add(riva_boards, rinfo);
2006
2007         pci_set_drvdata(pd, rinfo);
2008
2009         printk(KERN_INFO PFX
2010                 "PCI nVidia NV%x framebuffer ver %s (%s, %dMB @ 0x%lX)\n",
2011                 rinfo->riva.Architecture,
2012                 RIVAFB_VERSION,
2013                 rinfo->drvr_name,
2014                 rinfo->ram_amount / (1024 * 1024),
2015                 rinfo->fb_base_phys);
2016
2017         return 0;
2018
2019 err_out_load_state:
2020         riva_load_state(rinfo, &rinfo->initial_state);
2021 err_out_cursor:
2022         rivafb_exit_cursor(rinfo);
2023 /* err_out_iounmap_fb: */
2024         iounmap(rinfo->fb_base);
2025 err_out_iounmap_ctrl:
2026         iounmap(rinfo->ctrl_base);
2027 err_out_free_base1:
2028         release_mem_region(rinfo->fb_base_phys, rinfo->base1_region_size);
2029 err_out_free_base0:
2030         release_mem_region(rinfo->ctrl_base_phys, rinfo->base0_region_size);
2031 err_out_kfree:
2032         kfree(rinfo);
2033 err_out:
2034         return -ENODEV;
2035 }
2036
2037 static void __devexit rivafb_remove_one(struct pci_dev *pd)
2038 {
2039         struct rivafb_info *board = pci_get_drvdata(pd);
2040         
2041         if (!board)
2042                 return;
2043
2044         riva_boards = riva_board_list_del(riva_boards, board);
2045
2046         riva_load_state(board, &board->initial_state);
2047
2048         unregister_framebuffer((struct fb_info *)board);
2049
2050         rivafb_exit_cursor(board);
2051
2052 #ifdef CONFIG_MTRR
2053         if (board->mtrr.vram_valid)
2054                 mtrr_del(board->mtrr.vram, board->fb_base_phys,
2055                          board->ram_amount);
2056 #endif /* CONFIG_MTRR */
2057
2058         iounmap(board->ctrl_base);
2059         iounmap(board->fb_base);
2060
2061         release_mem_region(board->ctrl_base_phys,
2062                            board->base0_region_size);
2063         release_mem_region(board->fb_base_phys,
2064                            board->ram_amount);
2065
2066         kfree(board);
2067
2068         pci_set_drvdata(pd, NULL);
2069 }
2070
2071
2072
2073 /* ------------------------------------------------------------------------- *
2074  *
2075  * initialization
2076  *
2077  * ------------------------------------------------------------------------- */
2078
2079 #ifndef MODULE
2080 int __init rivafb_setup(char *options)
2081 {
2082         char *this_opt;
2083
2084         if (!options || !*options)
2085                 return 0;
2086
2087         while ((this_opt = strsep(&options, ",")) != NULL) {
2088                 if (!*this_opt)
2089                         continue;
2090                 if (!strncmp(this_opt, "font:", 5)) {
2091                         char *p;
2092                         int i;
2093
2094                         p = this_opt + 5;
2095                         for (i = 0; i < sizeof(fontname) - 1; i++)
2096                                 if (!*p || *p == ' ' || *p == ',')
2097                                         break;
2098                         memcpy(fontname, this_opt + 5, i);
2099                         fontname[i] = 0;
2100
2101                 } else if (!strncmp(this_opt, "noblink", 7)) {
2102                         noblink = 1;
2103                 } else if (!strncmp(this_opt, "noaccel", 7)) {
2104                         noaccel = 1;
2105                 } else if (!strncmp(this_opt, "nomove", 6)) {
2106                         nomove = 1;
2107 #ifdef CONFIG_MTRR
2108                 } else if (!strncmp(this_opt, "nomtrr", 6)) {
2109                         nomtrr = 1;
2110 #endif
2111                 } else if (!strncmp(this_opt, "nohwcursor", 10)) {
2112                         nohwcursor = 1;
2113                 } else
2114                         mode_option = this_opt;
2115         }
2116         return 0;
2117 }
2118 #endif /* !MODULE */
2119
2120 static struct pci_driver rivafb_driver = {
2121         name:           "rivafb",
2122         id_table:       rivafb_pci_tbl,
2123         probe:          rivafb_init_one,
2124         remove:         __devexit_p(rivafb_remove_one),
2125 };
2126
2127
2128
2129 /* ------------------------------------------------------------------------- *
2130  *
2131  * modularization
2132  *
2133  * ------------------------------------------------------------------------- */
2134
2135 int __init rivafb_init(void)
2136 {
2137         int err;
2138 #ifdef MODULE
2139         if (font) strncpy(fontname, font, sizeof(fontname)-1);
2140 #endif
2141         err = pci_module_init(&rivafb_driver);
2142         if (err)
2143                 return err;
2144         return 0;
2145 }
2146
2147
2148 #ifdef MODULE
2149 static void __exit rivafb_exit(void)
2150 {
2151         pci_unregister_driver(&rivafb_driver);
2152 }
2153
2154 module_init(rivafb_init);
2155 module_exit(rivafb_exit);
2156
2157 MODULE_PARM(font, "s");
2158 MODULE_PARM_DESC(font, "Specifies one of the compiled-in fonts (default=none)");
2159 MODULE_PARM(noaccel, "i");
2160 MODULE_PARM_DESC(noaccel, "Disables hardware acceleration (0 or 1=disabled) (default=0)");
2161 MODULE_PARM(nomove, "i");
2162 MODULE_PARM_DESC(nomove, "Enables YSCROLL_NOMOVE (0 or 1=enabled) (default=0)");
2163 MODULE_PARM(nohwcursor, "i");
2164 MODULE_PARM_DESC(nohwcursor, "Disables hardware cursor (0 or 1=disabled) (default=0)");
2165 MODULE_PARM(noblink, "i");
2166 MODULE_PARM_DESC(noblink, "Disables hardware cursor blinking (0 or 1=disabled) (default=0)");
2167 #ifdef CONFIG_MTRR
2168 MODULE_PARM(nomtrr, "i");
2169 MODULE_PARM_DESC(nomtrr, "Disables MTRR support (0 or 1=disabled) (default=0)");
2170 #endif
2171 #endif /* MODULE */
2172
2173 MODULE_AUTHOR("Ani Joshi, maintainer");
2174 MODULE_DESCRIPTION("Framebuffer driver for nVidia Riva 128, TNT, TNT2");
2175 MODULE_LICENSE("GPL");