Merge branch 'devel' of master.kernel.org:/home/rmk/linux-2.6-serial
[pandora-kernel.git] / drivers / video / tdfxfb.c
1 /*
2  *
3  * tdfxfb.c
4  *
5  * Author: Hannu Mallat <hmallat@cc.hut.fi>
6  *
7  * Copyright © 1999 Hannu Mallat
8  * All rights reserved
9  *
10  * Created      : Thu Sep 23 18:17:43 1999, hmallat
11  * Last modified: Tue Nov  2 21:19:47 1999, hmallat
12  *
13  * Lots of the information here comes from the Daryll Strauss' Banshee 
14  * patches to the XF86 server, and the rest comes from the 3dfx
15  * Banshee specification. I'm very much indebted to Daryll for his
16  * work on the X server.
17  *
18  * Voodoo3 support was contributed Harold Oga. Lots of additions
19  * (proper acceleration, 24 bpp, hardware cursor) and bug fixes by Attila
20  * Kesmarki. Thanks guys!
21  *
22  * Voodoo1 and Voodoo2 support aren't relevant to this driver as they
23  * behave very differently from the Voodoo3/4/5. For anyone wanting to
24  * use frame buffer on the Voodoo1/2, see the sstfb driver (which is
25  * located at http://www.sourceforge.net/projects/sstfb).
26  * 
27  * While I _am_ grateful to 3Dfx for releasing the specs for Banshee,
28  * I do wish the next version is a bit more complete. Without the XF86
29  * patches I couldn't have gotten even this far... for instance, the
30  * extensions to the VGA register set go completely unmentioned in the
31  * spec! Also, lots of references are made to the 'SST core', but no
32  * spec is publicly available, AFAIK.
33  *
34  * The structure of this driver comes pretty much from the Permedia
35  * driver by Ilario Nardinocchi, which in turn is based on skeletonfb.
36  * 
37  * TODO:
38  * - support for 16/32 bpp needs fixing (funky bootup penguin)
39  * - multihead support (basically need to support an array of fb_infos)
40  * - support other architectures (PPC, Alpha); does the fact that the VGA
41  *   core can be accessed only thru I/O (not memory mapped) complicate
42  *   things?
43  *
44  * Version history:
45  *
46  * 0.1.4 (released 2002-05-28) ported over to new fbdev api by James Simmons
47  *
48  * 0.1.3 (released 1999-11-02) added Attila's panning support, code
49  *                             reorg, hwcursor address page size alignment
50  *                             (for mmaping both frame buffer and regs),
51  *                             and my changes to get rid of hardcoded
52  *                             VGA i/o register locations (uses PCI
53  *                             configuration info now)
54  * 0.1.2 (released 1999-10-19) added Attila Kesmarki's bug fixes and
55  *                             improvements
56  * 0.1.1 (released 1999-10-07) added Voodoo3 support by Harold Oga.
57  * 0.1.0 (released 1999-10-06) initial version
58  *
59  */
60
61 #include <linux/module.h>
62 #include <linux/kernel.h>
63 #include <linux/errno.h>
64 #include <linux/string.h>
65 #include <linux/mm.h>
66 #include <linux/tty.h>
67 #include <linux/slab.h>
68 #include <linux/delay.h>
69 #include <linux/interrupt.h>
70 #include <linux/fb.h>
71 #include <linux/init.h>
72 #include <linux/pci.h>
73 #include <linux/nvram.h>
74 #include <asm/io.h>
75 #include <linux/timer.h>
76 #include <linux/spinlock.h>
77
78 #include <video/tdfx.h>
79
80 #undef TDFXFB_DEBUG 
81 #ifdef TDFXFB_DEBUG
82 #define DPRINTK(a,b...) printk(KERN_DEBUG "fb: %s: " a, __FUNCTION__ , ## b)
83 #else
84 #define DPRINTK(a,b...)
85 #endif 
86
87 #define BANSHEE_MAX_PIXCLOCK 270000
88 #define VOODOO3_MAX_PIXCLOCK 300000
89 #define VOODOO5_MAX_PIXCLOCK 350000
90
91 static struct fb_fix_screeninfo tdfx_fix __devinitdata = {
92         .id =           "3Dfx",
93         .type =         FB_TYPE_PACKED_PIXELS,
94         .visual =       FB_VISUAL_PSEUDOCOLOR, 
95         .ypanstep =     1,
96         .ywrapstep =    1, 
97         .accel =        FB_ACCEL_3DFX_BANSHEE
98 };
99
100 static struct fb_var_screeninfo tdfx_var __devinitdata = {
101         /* "640x480, 8 bpp @ 60 Hz */
102         .xres =         640,
103         .yres =         480,
104         .xres_virtual = 640,
105         .yres_virtual = 1024,
106         .bits_per_pixel =8,
107         .red =          {0, 8, 0},
108         .blue =         {0, 8, 0},
109         .green =        {0, 8, 0},
110         .activate =     FB_ACTIVATE_NOW,
111         .height =       -1,
112         .width =        -1,
113         .accel_flags =  FB_ACCELF_TEXT,
114         .pixclock =     39722,
115         .left_margin =  40,
116         .right_margin = 24,
117         .upper_margin = 32,
118         .lower_margin = 11,
119         .hsync_len =    96,
120         .vsync_len =    2,
121         .vmode =        FB_VMODE_NONINTERLACED
122 };
123
124 /*
125  * PCI driver prototypes
126  */
127 static int __devinit tdfxfb_probe(struct pci_dev *pdev,
128                                   const struct pci_device_id *id);
129 static void __devexit tdfxfb_remove(struct pci_dev *pdev);
130
131 static struct pci_device_id tdfxfb_id_table[] = {
132         { PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_BANSHEE,
133           PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
134           0xff0000, 0 },
135         { PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_VOODOO3,
136           PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
137           0xff0000, 0 },
138         { PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_VOODOO5,
139           PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
140           0xff0000, 0 },
141         { 0, }
142 };
143
144 static struct pci_driver tdfxfb_driver = {
145         .name           = "tdfxfb",
146         .id_table       = tdfxfb_id_table,
147         .probe          = tdfxfb_probe,
148         .remove         = __devexit_p(tdfxfb_remove),
149 };
150
151 MODULE_DEVICE_TABLE(pci, tdfxfb_id_table);
152
153 /*
154  *  Frame buffer device API
155  */
156 static int tdfxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *fb); 
157 static int tdfxfb_set_par(struct fb_info *info); 
158 static int tdfxfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue, 
159                             u_int transp, struct fb_info *info); 
160 static int tdfxfb_blank(int blank, struct fb_info *info); 
161 static int tdfxfb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info);
162 static int banshee_wait_idle(struct fb_info *info);
163 #ifdef CONFIG_FB_3DFX_ACCEL
164 static void tdfxfb_fillrect(struct fb_info *info, const struct fb_fillrect *rect);
165 static void tdfxfb_copyarea(struct fb_info *info, const struct fb_copyarea *area);  
166 static void tdfxfb_imageblit(struct fb_info *info, const struct fb_image *image); 
167 #endif /* CONFIG_FB_3DFX_ACCEL */
168
169 static struct fb_ops tdfxfb_ops = {
170         .owner          = THIS_MODULE,
171         .fb_check_var   = tdfxfb_check_var,
172         .fb_set_par     = tdfxfb_set_par,
173         .fb_setcolreg   = tdfxfb_setcolreg,
174         .fb_blank       = tdfxfb_blank,
175         .fb_pan_display = tdfxfb_pan_display,
176         .fb_sync        = banshee_wait_idle,
177 #ifdef CONFIG_FB_3DFX_ACCEL
178         .fb_fillrect    = tdfxfb_fillrect,
179         .fb_copyarea    = tdfxfb_copyarea,
180         .fb_imageblit   = tdfxfb_imageblit,
181 #else
182         .fb_fillrect    = cfb_fillrect,
183         .fb_copyarea    = cfb_copyarea,
184         .fb_imageblit   = cfb_imageblit,
185 #endif
186 };
187
188 /*
189  * do_xxx: Hardware-specific functions
190  */
191 static u32 do_calc_pll(int freq, int *freq_out);
192 static void  do_write_regs(struct fb_info *info, struct banshee_reg *reg);
193 static unsigned long do_lfb_size(struct tdfx_par *par, unsigned short);
194
195 /*
196  * Driver data 
197  */
198 static int  nopan   = 0;
199 static int  nowrap  = 1;      // not implemented (yet)
200 static char *mode_option __devinitdata = NULL;
201
202 /* ------------------------------------------------------------------------- 
203  *                      Hardware-specific funcions
204  * ------------------------------------------------------------------------- */
205
206 #ifdef VGA_REG_IO 
207 static inline  u8 vga_inb(struct tdfx_par *par, u32 reg) { return inb(reg); }
208
209 static inline void vga_outb(struct tdfx_par *par, u32 reg,  u8 val) { outb(val, reg); }
210 #else
211 static inline  u8 vga_inb(struct tdfx_par *par, u32 reg) { 
212         return inb(par->iobase + reg - 0x300); 
213 }
214 static inline void vga_outb(struct tdfx_par *par, u32 reg,  u8 val) { 
215         outb(val, par->iobase + reg - 0x300); 
216 }
217 #endif
218
219 static inline void gra_outb(struct tdfx_par *par, u32 idx, u8 val) {
220         vga_outb(par, GRA_I, idx); vga_outb(par, GRA_D, val);
221 }
222
223 static inline void seq_outb(struct tdfx_par *par, u32 idx, u8 val) {
224         vga_outb(par, SEQ_I, idx); vga_outb(par, SEQ_D, val);
225 }
226
227 static inline u8 seq_inb(struct tdfx_par *par, u32 idx) {
228         vga_outb(par, SEQ_I, idx); return vga_inb(par, SEQ_D);
229 }
230
231 static inline void crt_outb(struct tdfx_par *par, u32 idx, u8 val) {
232         vga_outb(par, CRT_I, idx); vga_outb(par, CRT_D, val);
233 }
234
235 static inline u8 crt_inb(struct tdfx_par *par, u32 idx) {
236         vga_outb(par, CRT_I, idx); return vga_inb(par, CRT_D);
237 }
238
239 static inline void att_outb(struct tdfx_par *par, u32 idx, u8 val) 
240 {
241         unsigned char tmp;
242         
243         tmp = vga_inb(par, IS1_R);
244         vga_outb(par, ATT_IW, idx);
245         vga_outb(par, ATT_IW, val);
246 }
247
248 static inline void vga_disable_video(struct tdfx_par *par)
249 {
250         unsigned char s;
251
252         s = seq_inb(par, 0x01) | 0x20;
253         seq_outb(par, 0x00, 0x01);
254         seq_outb(par, 0x01, s);
255         seq_outb(par, 0x00, 0x03);
256 }
257
258 static inline void vga_enable_video(struct tdfx_par *par)
259 {
260         unsigned char s;
261
262         s = seq_inb(par, 0x01) & 0xdf;
263         seq_outb(par, 0x00, 0x01);
264         seq_outb(par, 0x01, s);
265         seq_outb(par, 0x00, 0x03);
266 }
267
268 static inline void vga_enable_palette(struct tdfx_par *par)
269 {
270         vga_inb(par, IS1_R);
271         vga_outb(par, ATT_IW, 0x20);
272 }
273
274 static inline u32 tdfx_inl(struct tdfx_par *par, unsigned int reg) 
275 {
276         return readl(par->regbase_virt + reg);
277 }
278
279 static inline void tdfx_outl(struct tdfx_par *par, unsigned int reg, u32 val)
280 {
281         writel(val, par->regbase_virt + reg);
282 }
283
284 static inline void banshee_make_room(struct tdfx_par *par, int size)
285 {
286         /* Note: The Voodoo3's onboard FIFO has 32 slots. This loop
287          * won't quit if you ask for more. */
288         while((tdfx_inl(par, STATUS) & 0x1f) < size-1);
289 }
290  
291 static int banshee_wait_idle(struct fb_info *info)
292 {
293         struct tdfx_par *par = info->par;
294         int i = 0;
295
296         banshee_make_room(par, 1);
297         tdfx_outl(par, COMMAND_3D, COMMAND_3D_NOP);
298
299         while(1) {
300                 i = (tdfx_inl(par, STATUS) & STATUS_BUSY) ? 0 : i + 1;
301                 if(i == 3) break;
302         }
303         return 0;
304 }
305
306 /*
307  * Set the color of a palette entry in 8bpp mode 
308  */
309 static inline void do_setpalentry(struct tdfx_par *par, unsigned regno, u32 c)
310 {  
311         banshee_make_room(par, 2);
312         tdfx_outl(par, DACADDR, regno);
313         tdfx_outl(par, DACDATA, c);
314 }
315
316 static u32 do_calc_pll(int freq, int* freq_out) 
317 {
318         int m, n, k, best_m, best_n, best_k, best_error;
319         int fref = 14318;
320   
321         best_error = freq;
322         best_n = best_m = best_k = 0;
323
324         for (k = 3; k >= 0; k--) {
325                 for (m = 63; m >= 0; m--) {
326                         /*
327                          * Estimate value of n that produces target frequency
328                          * with current m and k
329                          */
330                         int n_estimated = (freq * (m + 2) * (1 << k) / fref) - 2;
331
332                         /* Search neighborhood of estimated n */
333                         for (n = max(0, n_estimated - 1);
334                                         n <= min(255, n_estimated + 1); n++) {
335                                 /*
336                                  * Calculate PLL freqency with current m, k and
337                                  * estimated n
338                                  */
339                                 int f = fref * (n + 2) / (m + 2) / (1 << k);
340                                 int error = abs (f - freq);
341
342                                 /*
343                                  *  If this is the closest we've come to the
344                                  * target frequency then remember n, m and k
345                                  */
346                                 if (error  < best_error) {
347                                         best_error = error;
348                                         best_n     = n;
349                                         best_m     = m;
350                                         best_k     = k;
351                                 }
352                         }
353                 }
354         }
355
356         n = best_n;
357         m = best_m;
358         k = best_k;
359         *freq_out = fref*(n + 2)/(m + 2)/(1 << k);
360
361         return (n << 8) | (m << 2) | k;
362 }
363
364 static void do_write_regs(struct fb_info *info, struct banshee_reg* reg) 
365 {
366         struct tdfx_par *par = info->par;
367         int i;
368
369         banshee_wait_idle(info);
370
371         tdfx_outl(par, MISCINIT1, tdfx_inl(par, MISCINIT1) | 0x01);
372
373         crt_outb(par, 0x11, crt_inb(par, 0x11) & 0x7f); /* CRT unprotect */
374
375         banshee_make_room(par, 3);
376         tdfx_outl(par, VGAINIT1,        reg->vgainit1 &  0x001FFFFF);
377         tdfx_outl(par, VIDPROCCFG,      reg->vidcfg   & ~0x00000001);
378 #if 0
379         tdfx_outl(par, PLLCTRL1, reg->mempll);
380         tdfx_outl(par, PLLCTRL2, reg->gfxpll);
381 #endif
382         tdfx_outl(par, PLLCTRL0,        reg->vidpll);
383
384         vga_outb(par, MISC_W, reg->misc[0x00] | 0x01);
385
386         for (i = 0; i < 5; i++)
387                 seq_outb(par, i, reg->seq[i]);
388
389         for (i = 0; i < 25; i++)
390                 crt_outb(par, i, reg->crt[i]);
391
392         for (i = 0; i < 9; i++)
393                 gra_outb(par, i, reg->gra[i]);
394
395         for (i = 0; i < 21; i++)
396                 att_outb(par, i, reg->att[i]);
397
398         crt_outb(par, 0x1a, reg->ext[0]);
399         crt_outb(par, 0x1b, reg->ext[1]);
400
401         vga_enable_palette(par);
402         vga_enable_video(par);
403
404         banshee_make_room(par, 11);
405         tdfx_outl(par,  VGAINIT0,      reg->vgainit0);
406         tdfx_outl(par,  DACMODE,       reg->dacmode);
407         tdfx_outl(par,  VIDDESKSTRIDE, reg->stride);
408         tdfx_outl(par,  HWCURPATADDR,  0);
409    
410         tdfx_outl(par,  VIDSCREENSIZE,reg->screensize);
411         tdfx_outl(par,  VIDDESKSTART,   reg->startaddr);
412         tdfx_outl(par,  VIDPROCCFG,     reg->vidcfg);
413         tdfx_outl(par,  VGAINIT1,       reg->vgainit1);  
414         tdfx_outl(par,  MISCINIT0,      reg->miscinit0);        
415
416         banshee_make_room(par,  8);
417         tdfx_outl(par,  SRCBASE,         reg->srcbase);
418         tdfx_outl(par,  DSTBASE,         reg->dstbase);
419         tdfx_outl(par,  COMMANDEXTRA_2D, 0);
420         tdfx_outl(par,  CLIP0MIN,        0);
421         tdfx_outl(par,  CLIP0MAX,        0x0fff0fff);
422         tdfx_outl(par,  CLIP1MIN,        0);
423         tdfx_outl(par,  CLIP1MAX,        0x0fff0fff);
424         tdfx_outl(par,  SRCXY,     0);
425
426         banshee_wait_idle(info);
427 }
428
429 static unsigned long do_lfb_size(struct tdfx_par *par, unsigned short dev_id) 
430 {
431         u32 draminit0;
432         u32 draminit1;
433         u32 miscinit1;
434
435         int num_chips;
436         int chip_size; /* in MB */
437         u32 lfbsize;
438         int has_sgram;
439
440         draminit0 = tdfx_inl(par, DRAMINIT0);  
441         draminit1 = tdfx_inl(par, DRAMINIT1);
442
443         num_chips = (draminit0 & DRAMINIT0_SGRAM_NUM) ? 8 : 4;
444  
445         if (dev_id < PCI_DEVICE_ID_3DFX_VOODOO5) {
446                 /* Banshee/Voodoo3 */
447                 has_sgram = draminit1 & DRAMINIT1_MEM_SDRAM;
448                 chip_size = has_sgram ? ((draminit0 & DRAMINIT0_SGRAM_TYPE) ? 2 : 1)
449                                       : 2;
450         } else {
451                 /* Voodoo4/5 */
452                 has_sgram = 0;
453                 chip_size = 1 << ((draminit0 & DRAMINIT0_SGRAM_TYPE_MASK) >> DRAMINIT0_SGRAM_TYPE_SHIFT);
454         }
455         lfbsize = num_chips * chip_size * 1024 * 1024;
456
457         /* disable block writes for SDRAM */
458         miscinit1 = tdfx_inl(par, MISCINIT1);
459         miscinit1 |= has_sgram ? 0 : MISCINIT1_2DBLOCK_DIS;
460         miscinit1 |= MISCINIT1_CLUT_INV;
461
462         banshee_make_room(par, 1); 
463         tdfx_outl(par, MISCINIT1, miscinit1);
464         return lfbsize;
465 }
466
467 /* ------------------------------------------------------------------------- */
468
469 static int tdfxfb_check_var(struct fb_var_screeninfo *var,struct fb_info *info) 
470 {
471         struct tdfx_par *par = info->par;
472         u32 lpitch;
473
474         if (var->bits_per_pixel != 8  && var->bits_per_pixel != 16 &&
475             var->bits_per_pixel != 24 && var->bits_per_pixel != 32) {
476                 DPRINTK("depth not supported: %u\n", var->bits_per_pixel);
477                 return -EINVAL;
478         }
479
480         if (var->xres != var->xres_virtual)
481                 var->xres_virtual = var->xres;
482
483         if (var->yres > var->yres_virtual)
484                 var->yres_virtual = var->yres;
485
486         if (var->xoffset) {
487                 DPRINTK("xoffset not supported\n");
488                 return -EINVAL;
489         }
490
491         /* Banshee doesn't support interlace, but Voodoo4/5 and probably Voodoo3 do. */
492         /* no direct information about device id now? use max_pixclock for this... */
493         if (((var->vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) &&
494                         (par->max_pixclock < VOODOO3_MAX_PIXCLOCK)) {
495                 DPRINTK("interlace not supported\n");
496                 return -EINVAL;
497         }
498
499         var->xres = (var->xres + 15) & ~15; /* could sometimes be 8 */
500         lpitch = var->xres * ((var->bits_per_pixel + 7)>>3);
501   
502         if (var->xres < 320 || var->xres > 2048) {
503                 DPRINTK("width not supported: %u\n", var->xres);
504                 return -EINVAL;
505         }
506   
507         if (var->yres < 200 || var->yres > 2048) {
508                 DPRINTK("height not supported: %u\n", var->yres);
509                 return -EINVAL;
510         }
511   
512         if (lpitch * var->yres_virtual > info->fix.smem_len) {
513                 var->yres_virtual = info->fix.smem_len/lpitch;
514                 if (var->yres_virtual < var->yres) {
515                         DPRINTK("no memory for screen (%ux%ux%u)\n",
516                         var->xres, var->yres_virtual, var->bits_per_pixel);
517                         return -EINVAL;
518                 }
519         }
520   
521         if (PICOS2KHZ(var->pixclock) > par->max_pixclock) {
522                 DPRINTK("pixclock too high (%ldKHz)\n",PICOS2KHZ(var->pixclock));
523                 return -EINVAL;
524         }
525
526         switch(var->bits_per_pixel) {
527                 case 8:
528                         var->red.length = var->green.length = var->blue.length = 8;
529                         break;
530                 case 16:
531                         var->red.offset   = 11;
532                         var->red.length   = 5;
533                         var->green.offset = 5;
534                         var->green.length = 6;
535                         var->blue.offset  = 0;
536                         var->blue.length  = 5;
537                         break;
538                 case 24:
539                         var->red.offset=16;
540                         var->green.offset=8;
541                         var->blue.offset=0;
542                         var->red.length = var->green.length = var->blue.length = 8;
543                 case 32:
544                         var->red.offset   = 16;
545                         var->green.offset = 8;
546                         var->blue.offset  = 0;
547                         var->red.length = var->green.length = var->blue.length = 8;
548                         break;
549         }
550         var->height = var->width = -1;
551   
552         var->accel_flags = FB_ACCELF_TEXT;
553         
554         DPRINTK("Checking graphics mode at %dx%d depth %d\n",  var->xres, var->yres, var->bits_per_pixel);
555         return 0;
556 }
557
558 static int tdfxfb_set_par(struct fb_info *info)
559 {
560         struct tdfx_par *par = info->par;
561         u32 hdispend, hsyncsta, hsyncend, htotal;
562         u32 hd, hs, he, ht, hbs, hbe;
563         u32 vd, vs, ve, vt, vbs, vbe;
564         struct banshee_reg reg;
565         int fout, freq;
566         u32 wd, cpp;
567   
568         par->baseline  = 0;
569  
570         memset(&reg, 0, sizeof(reg));
571         cpp = (info->var.bits_per_pixel + 7)/8;
572  
573         reg.vidcfg = VIDCFG_VIDPROC_ENABLE | VIDCFG_DESK_ENABLE | VIDCFG_CURS_X11 | ((cpp - 1) << VIDCFG_PIXFMT_SHIFT) | (cpp != 1 ? VIDCFG_CLUT_BYPASS : 0);
574
575         /* PLL settings */
576         freq = PICOS2KHZ(info->var.pixclock);
577
578         reg.dacmode = 0;
579         reg.vidcfg  &= ~VIDCFG_2X;
580
581         hdispend = info->var.xres;
582         hsyncsta = hdispend + info->var.right_margin;
583         hsyncend = hsyncsta + info->var.hsync_len;
584         htotal   = hsyncend + info->var.left_margin;    
585
586         if (freq > par->max_pixclock/2) {
587                 freq = freq > par->max_pixclock ? par->max_pixclock : freq;
588                 reg.dacmode |= DACMODE_2X;
589                 reg.vidcfg  |= VIDCFG_2X;
590                 hdispend >>= 1;
591                 hsyncsta >>= 1;
592                 hsyncend >>= 1;
593                 htotal   >>= 1;
594         }
595   
596         hd  = wd = (hdispend >> 3) - 1;
597         hs  = (hsyncsta >> 3) - 1;
598         he  = (hsyncend >> 3) - 1;
599         ht  = (htotal >> 3) - 1;
600         hbs = hd;
601         hbe = ht;
602
603         if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE) {
604                 vbs = vd = (info->var.yres << 1) - 1;
605                 vs  = vd + (info->var.lower_margin << 1);
606                 ve  = vs + (info->var.vsync_len << 1);
607                 vbe = vt = ve + (info->var.upper_margin << 1) - 1;
608         } else {
609                 vbs = vd = info->var.yres - 1;
610                 vs  = vd + info->var.lower_margin;
611                 ve  = vs + info->var.vsync_len;
612                 vbe = vt = ve + info->var.upper_margin - 1;
613         }
614   
615         /* this is all pretty standard VGA register stuffing */
616         reg.misc[0x00] = 0x0f | 
617                         (info->var.xres < 400 ? 0xa0 :
618                          info->var.xres < 480 ? 0x60 :
619                          info->var.xres < 768 ? 0xe0 : 0x20);
620      
621         reg.gra[0x00] = 0x00;
622         reg.gra[0x01] = 0x00;
623         reg.gra[0x02] = 0x00;
624         reg.gra[0x03] = 0x00;
625         reg.gra[0x04] = 0x00;
626         reg.gra[0x05] = 0x40;
627         reg.gra[0x06] = 0x05;
628         reg.gra[0x07] = 0x0f;
629         reg.gra[0x08] = 0xff;
630
631         reg.att[0x00] = 0x00;
632         reg.att[0x01] = 0x01;
633         reg.att[0x02] = 0x02;
634         reg.att[0x03] = 0x03;
635         reg.att[0x04] = 0x04;
636         reg.att[0x05] = 0x05;
637         reg.att[0x06] = 0x06;
638         reg.att[0x07] = 0x07;
639         reg.att[0x08] = 0x08;
640         reg.att[0x09] = 0x09;
641         reg.att[0x0a] = 0x0a;
642         reg.att[0x0b] = 0x0b;
643         reg.att[0x0c] = 0x0c;
644         reg.att[0x0d] = 0x0d;
645         reg.att[0x0e] = 0x0e;
646         reg.att[0x0f] = 0x0f;
647         reg.att[0x10] = 0x41;
648         reg.att[0x11] = 0x00;
649         reg.att[0x12] = 0x0f;
650         reg.att[0x13] = 0x00;
651         reg.att[0x14] = 0x00;
652
653         reg.seq[0x00] = 0x03;
654         reg.seq[0x01] = 0x01; /* fixme: clkdiv2? */
655         reg.seq[0x02] = 0x0f;
656         reg.seq[0x03] = 0x00;
657         reg.seq[0x04] = 0x0e;
658
659         reg.crt[0x00] = ht - 4;
660         reg.crt[0x01] = hd;
661         reg.crt[0x02] = hbs;
662         reg.crt[0x03] = 0x80 | (hbe & 0x1f);
663         reg.crt[0x04] = hs;
664         reg.crt[0x05] = ((hbe & 0x20) << 2) | (he & 0x1f); 
665         reg.crt[0x06] = vt;
666         reg.crt[0x07] = ((vs & 0x200) >> 2) |
667                         ((vd & 0x200) >> 3) |
668                         ((vt & 0x200) >> 4) | 0x10 |
669                         ((vbs & 0x100) >> 5) |
670                         ((vs  & 0x100) >> 6) |
671                         ((vd  & 0x100) >> 7) |
672                         ((vt  & 0x100) >> 8);
673         reg.crt[0x08] = 0x00;
674         reg.crt[0x09] = 0x40 | ((vbs & 0x200) >> 4); 
675         reg.crt[0x0a] = 0x00;
676         reg.crt[0x0b] = 0x00;
677         reg.crt[0x0c] = 0x00;
678         reg.crt[0x0d] = 0x00;
679         reg.crt[0x0e] = 0x00;
680         reg.crt[0x0f] = 0x00;
681         reg.crt[0x10] = vs;
682         reg.crt[0x11] = (ve & 0x0f) | 0x20; 
683         reg.crt[0x12] = vd;
684         reg.crt[0x13] = wd;
685         reg.crt[0x14] = 0x00;
686         reg.crt[0x15] = vbs;
687         reg.crt[0x16] = vbe + 1; 
688         reg.crt[0x17] = 0xc3;
689         reg.crt[0x18] = 0xff;
690  
691         /* Banshee's nonvga stuff */
692         reg.ext[0x00] = (((ht  & 0x100) >> 8) | 
693                         ((hd  & 0x100) >> 6) |
694                         ((hbs & 0x100) >> 4) |
695                         ((hbe &  0x40) >> 1) |
696                         ((hs  & 0x100) >> 2) |
697                         ((he  &  0x20) << 2)); 
698         reg.ext[0x01] = (((vt  & 0x400) >> 10) |
699                         ((vd  & 0x400) >>  8) | 
700                         ((vbs & 0x400) >>  6) |
701                         ((vbe & 0x400) >>  4));
702
703         reg.vgainit0 =  VGAINIT0_8BIT_DAC     |
704                         VGAINIT0_EXT_ENABLE   |
705                         VGAINIT0_WAKEUP_3C3   |
706                         VGAINIT0_ALT_READBACK |
707                         VGAINIT0_EXTSHIFTOUT;
708         reg.vgainit1 = tdfx_inl(par, VGAINIT1) & 0x1fffff;
709
710         reg.cursloc   = 0;
711    
712         reg.cursc0    = 0; 
713         reg.cursc1    = 0xffffff;
714    
715         reg.stride    = info->var.xres * cpp;
716         reg.startaddr = par->baseline * reg.stride;
717         reg.srcbase   = reg.startaddr;
718         reg.dstbase   = reg.startaddr;
719
720         /* PLL settings */
721         freq = PICOS2KHZ(info->var.pixclock);
722
723         reg.dacmode &= ~DACMODE_2X;
724         reg.vidcfg  &= ~VIDCFG_2X;
725         if (freq > par->max_pixclock/2) {
726                 freq = freq > par->max_pixclock ? par->max_pixclock : freq;
727                 reg.dacmode |= DACMODE_2X;
728                 reg.vidcfg  |= VIDCFG_2X;
729         }
730         reg.vidpll = do_calc_pll(freq, &fout);
731 #if 0
732         reg.mempll = do_calc_pll(..., &fout);
733         reg.gfxpll = do_calc_pll(..., &fout);
734 #endif
735
736         if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE) {
737                 reg.screensize = info->var.xres | (info->var.yres << 13);
738                 reg.vidcfg |= VIDCFG_HALF_MODE;
739                 reg.crt[0x09] |= 0x80;
740         } else {
741                 reg.screensize = info->var.xres | (info->var.yres << 12);
742                 reg.vidcfg &= ~VIDCFG_HALF_MODE;
743         }
744         if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED)
745                 reg.vidcfg |= VIDCFG_INTERLACE;
746         reg.miscinit0 = tdfx_inl(par, MISCINIT0);
747
748 #if defined(__BIG_ENDIAN)
749         switch (info->var.bits_per_pixel) {
750                 case 8:
751                 case 24:
752                         reg.miscinit0 &= ~(1 << 30);
753                         reg.miscinit0 &= ~(1 << 31);
754                         break;
755                 case 16:
756                         reg.miscinit0 |= (1 << 30);
757                         reg.miscinit0 |= (1 << 31);
758                         break;
759                 case 32:
760                         reg.miscinit0 |= (1 << 30);
761                         reg.miscinit0 &= ~(1 << 31);
762                         break;
763         }
764 #endif 
765         do_write_regs(info, &reg);
766
767         /* Now change fb_fix_screeninfo according to changes in par */
768         info->fix.line_length = info->var.xres * ((info->var.bits_per_pixel + 7)>>3);
769         info->fix.visual = (info->var.bits_per_pixel == 8) 
770                                 ? FB_VISUAL_PSEUDOCOLOR
771                                 : FB_VISUAL_TRUECOLOR;
772         DPRINTK("Graphics mode is now set at %dx%d depth %d\n", info->var.xres, info->var.yres, info->var.bits_per_pixel);
773         return 0;       
774 }
775
776 /* A handy macro shamelessly pinched from matroxfb */
777 #define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
778
779 static int tdfxfb_setcolreg(unsigned regno, unsigned red, unsigned green,  
780                             unsigned blue,unsigned transp,struct fb_info *info) 
781 {
782         struct tdfx_par *par = info->par;
783         u32 rgbcol;
784    
785         if (regno >= info->cmap.len || regno > 255) return 1;
786    
787         switch (info->fix.visual) {
788         case FB_VISUAL_PSEUDOCOLOR:
789                 rgbcol =(((u32)red   & 0xff00) << 8) |
790                         (((u32)green & 0xff00) << 0) |
791                         (((u32)blue  & 0xff00) >> 8);
792                 do_setpalentry(par, regno, rgbcol);
793                 break;
794         /* Truecolor has no hardware color palettes. */
795         case FB_VISUAL_TRUECOLOR:
796                 if (regno < 16) {
797                         rgbcol = (CNVT_TOHW( red, info->var.red.length) <<
798                                   info->var.red.offset) |
799                                 (CNVT_TOHW( green, info->var.green.length) <<
800                                  info->var.green.offset) |
801                                 (CNVT_TOHW( blue, info->var.blue.length) <<
802                                  info->var.blue.offset) |
803                                 (CNVT_TOHW( transp, info->var.transp.length) <<
804                                  info->var.transp.offset);
805                         par->palette[regno] = rgbcol;
806                 }
807
808                 break;
809         default:
810                 DPRINTK("bad depth %u\n", info->var.bits_per_pixel);
811                 break;
812         }
813
814         return 0;
815 }
816
817 /* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */
818 static int tdfxfb_blank(int blank, struct fb_info *info)
819
820         struct tdfx_par *par = info->par;
821         u32 dacmode, state = 0, vgablank = 0;
822
823         dacmode = tdfx_inl(par, DACMODE);
824
825         switch (blank) {
826                 case FB_BLANK_UNBLANK: /* Screen: On; HSync: On, VSync: On */
827                         state    = 0;
828                         vgablank = 0;
829                         break;
830                 case FB_BLANK_NORMAL: /* Screen: Off; HSync: On, VSync: On */
831                         state    = 0;
832                         vgablank = 1;
833                         break;
834                 case FB_BLANK_VSYNC_SUSPEND: /* Screen: Off; HSync: On, VSync: Off */
835                         state    = BIT(3);
836                         vgablank = 1;
837                         break;
838                 case FB_BLANK_HSYNC_SUSPEND: /* Screen: Off; HSync: Off, VSync: On */
839                         state    = BIT(1);
840                         vgablank = 1;
841                         break;
842                 case FB_BLANK_POWERDOWN: /* Screen: Off; HSync: Off, VSync: Off */
843                         state    = BIT(1) | BIT(3);
844                         vgablank = 1;
845                         break;
846         }
847
848         dacmode &= ~(BIT(1) | BIT(3));
849         dacmode |= state;
850         banshee_make_room(par, 1); 
851         tdfx_outl(par, DACMODE, dacmode);
852         if (vgablank) 
853                 vga_disable_video(par);
854         else
855                 vga_enable_video(par);
856         return 0;
857 }
858
859 /*   
860  * Set the starting position of the visible screen to var->yoffset
861  */   
862 static int tdfxfb_pan_display(struct fb_var_screeninfo *var,
863                               struct fb_info *info) 
864 {
865         struct tdfx_par *par = info->par;
866         u32 addr;       
867
868         if (nopan || var->xoffset || (var->yoffset > var->yres_virtual))
869                 return -EINVAL;
870         if ((var->yoffset + var->yres > var->yres_virtual && nowrap))
871                 return -EINVAL;
872
873         addr = var->yoffset * info->fix.line_length;
874         banshee_make_room(par, 1);
875         tdfx_outl(par, VIDDESKSTART, addr);
876    
877         info->var.xoffset = var->xoffset;
878         info->var.yoffset = var->yoffset; 
879         return 0;
880 }
881
882 #ifdef CONFIG_FB_3DFX_ACCEL
883 /*
884  * FillRect 2D command (solidfill or invert (via ROP_XOR))   
885  */
886 static void tdfxfb_fillrect(struct fb_info *info, const struct fb_fillrect *rect) 
887 {
888         struct tdfx_par *par = info->par;
889         u32 bpp = info->var.bits_per_pixel;
890         u32 stride = info->fix.line_length;
891         u32 fmt= stride | ((bpp+((bpp==8) ? 0 : 8)) << 13); 
892         int tdfx_rop;
893         
894         if (rect->rop == ROP_COPY) 
895                 tdfx_rop = TDFX_ROP_COPY;
896         else                     
897                 tdfx_rop = TDFX_ROP_XOR;
898
899         banshee_make_room(par, 5);
900         tdfx_outl(par,  DSTFORMAT, fmt);
901         if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR) {
902                 tdfx_outl(par,  COLORFORE, rect->color);
903         } else { /* FB_VISUAL_TRUECOLOR */
904                 tdfx_outl(par, COLORFORE, par->palette[rect->color]);
905         }
906         tdfx_outl(par,  COMMAND_2D, COMMAND_2D_FILLRECT | (tdfx_rop << 24));
907         tdfx_outl(par,  DSTSIZE,    rect->width | (rect->height << 16));
908         tdfx_outl(par,  LAUNCH_2D,  rect->dx | (rect->dy << 16));
909 }
910
911 /*
912  * Screen-to-Screen BitBlt 2D command (for the bmove fb op.) 
913  */
914 static void tdfxfb_copyarea(struct fb_info *info, const struct fb_copyarea *area)  
915 {
916         struct tdfx_par *par = info->par;
917         u32 sx = area->sx, sy = area->sy, dx = area->dx, dy = area->dy;
918         u32 bpp = info->var.bits_per_pixel;
919         u32 stride = info->fix.line_length;
920         u32 blitcmd = COMMAND_2D_S2S_BITBLT | (TDFX_ROP_COPY << 24);
921         u32 fmt = stride | ((bpp+((bpp==8) ? 0 : 8)) << 13); 
922         
923         if (area->sx <= area->dx) {
924                 //-X 
925                 blitcmd |= BIT(14);
926                 sx += area->width - 1;
927                 dx += area->width - 1;
928         }
929         if (area->sy <= area->dy) {
930                 //-Y  
931                 blitcmd |= BIT(15);
932                 sy += area->height - 1;
933                 dy += area->height - 1;
934         }
935    
936         banshee_make_room(par, 6);
937
938         tdfx_outl(par,  SRCFORMAT, fmt);
939         tdfx_outl(par,  DSTFORMAT, fmt);
940         tdfx_outl(par,  COMMAND_2D, blitcmd); 
941         tdfx_outl(par,  DSTSIZE,   area->width | (area->height << 16));
942         tdfx_outl(par,  DSTXY,     dx | (dy << 16));
943         tdfx_outl(par,  LAUNCH_2D, sx | (sy << 16)); 
944 }
945
946 static void tdfxfb_imageblit(struct fb_info *info, const struct fb_image *image) 
947 {
948         struct tdfx_par *par = info->par;
949         int size = image->height * ((image->width * image->depth + 7)>>3);
950         int fifo_free;
951         int i, stride = info->fix.line_length;
952         u32 bpp = info->var.bits_per_pixel;
953         u32 dstfmt = stride | ((bpp+((bpp==8) ? 0 : 8)) << 13); 
954         u8 *chardata = (u8 *) image->data;
955         u32 srcfmt;
956
957         if (image->depth != 1) {
958                 //banshee_make_room(par, 6 + ((size + 3) >> 2));
959                 //srcfmt = stride | ((bpp+((bpp==8) ? 0 : 8)) << 13) | 0x400000;
960                 cfb_imageblit(info, image);
961                 return;
962         } else {
963                 banshee_make_room(par, 8);
964                 switch (info->fix.visual) {
965                         case FB_VISUAL_PSEUDOCOLOR:
966                 tdfx_outl(par, COLORFORE, image->fg_color);
967                 tdfx_outl(par, COLORBACK, image->bg_color);
968                                 break;
969                         case FB_VISUAL_TRUECOLOR:
970                         default:
971                                 tdfx_outl(par, COLORFORE,
972                                           par->palette[image->fg_color]);
973                                 tdfx_outl(par, COLORBACK,
974                                           par->palette[image->bg_color]);
975                 }
976 #ifdef __BIG_ENDIAN
977                 srcfmt = 0x400000 | BIT(20);
978 #else
979                 srcfmt = 0x400000;
980 #endif
981         }       
982
983         tdfx_outl(par,  SRCXY,     0);
984         tdfx_outl(par,  DSTXY,     image->dx | (image->dy << 16));
985         tdfx_outl(par,  COMMAND_2D, COMMAND_2D_H2S_BITBLT | (TDFX_ROP_COPY << 24));
986         tdfx_outl(par,  SRCFORMAT, srcfmt);
987         tdfx_outl(par,  DSTFORMAT, dstfmt);
988         tdfx_outl(par,  DSTSIZE,   image->width | (image->height << 16));
989
990         /* A count of how many free FIFO entries we've requested.
991          * When this goes negative, we need to request more. */
992         fifo_free = 0;
993
994         /* Send four bytes at a time of data */ 
995         for (i = (size >> 2) ; i > 0; i--) { 
996                 if(--fifo_free < 0) {
997                         fifo_free=31;
998                         banshee_make_room(par,fifo_free);
999                 }
1000                 tdfx_outl(par,  LAUNCH_2D,*(u32*)chardata);
1001                 chardata += 4; 
1002         }       
1003
1004         /* Send the leftovers now */    
1005         banshee_make_room(par,3);
1006         i = size%4;     
1007         switch (i) {
1008                 case 0: break;
1009                 case 1:  tdfx_outl(par, LAUNCH_2D,*chardata); break;
1010                 case 2:  tdfx_outl(par, LAUNCH_2D,*(u16*)chardata); break;
1011                 case 3:  tdfx_outl(par, LAUNCH_2D,*(u16*)chardata | ((chardata[3]) << 24)); break;
1012         }
1013 }
1014 #endif /* CONFIG_FB_3DFX_ACCEL */
1015
1016 #ifdef TDFX_HARDWARE_CURSOR
1017 static int tdfxfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
1018 {
1019         struct tdfx_par *par = info->par;
1020         unsigned long flags;
1021
1022         /*
1023          * If the cursor is not be changed this means either we want the 
1024          * current cursor state (if enable is set) or we want to query what
1025          * we can do with the cursor (if enable is not set) 
1026          */
1027         if (!cursor->set) return 0;
1028
1029         /* Too large of a cursor :-( */
1030         if (cursor->image.width > 64 || cursor->image.height > 64)
1031                 return -ENXIO;
1032
1033         /* 
1034          * If we are going to be changing things we should disable
1035          * the cursor first 
1036          */
1037         if (info->cursor.enable) {
1038                 spin_lock_irqsave(&par->DAClock, flags);
1039                 info->cursor.enable = 0;
1040                 del_timer(&(par->hwcursor.timer));
1041                 tdfx_outl(par, VIDPROCCFG, par->hwcursor.disable);
1042                 spin_unlock_irqrestore(&par->DAClock, flags);
1043         }
1044
1045         /* Disable the Cursor */
1046         if ((cursor->set && FB_CUR_SETCUR) && !cursor->enable)
1047                 return 0;
1048
1049         /* fix cursor color - XFree86 forgets to restore it properly */
1050         if (cursor->set && FB_CUR_SETCMAP) {
1051                 struct fb_cmap cmap = cursor->image.cmap;
1052                 unsigned long bg_color, fg_color;
1053
1054                 cmap.len = 2; /* Voodoo 3+ only support 2 color cursors */
1055                 fg_color = ((cmap.red[cmap.start] << 16) |
1056                             (cmap.green[cmap.start] << 8)  |
1057                             (cmap.blue[cmap.start]));
1058                 bg_color = ((cmap.red[cmap.start+1] << 16) |
1059                             (cmap.green[cmap.start+1] << 8) |
1060                             (cmap.blue[cmap.start+1]));
1061                 fb_copy_cmap(&cmap, &info->cursor.image.cmap);
1062                 spin_lock_irqsave(&par->DAClock, flags);
1063                 banshee_make_room(par, 2);
1064                 tdfx_outl(par, HWCURC0, bg_color);
1065                 tdfx_outl(par, HWCURC1, fg_color);
1066                 spin_unlock_irqrestore(&par->DAClock, flags);
1067         }
1068
1069         if (cursor->set && FB_CUR_SETPOS) {
1070                 int x, y;
1071
1072                 x = cursor->image.dx;
1073                 y = cursor->image.dy;
1074                 y -= info->var.yoffset;
1075                 info->cursor.image.dx = x;
1076                 info->cursor.image.dy = y;
1077                 x += 63;
1078                 y += 63;
1079                 spin_lock_irqsave(&par->DAClock, flags);
1080                 banshee_make_room(par, 1);
1081                 tdfx_outl(par, HWCURLOC, (y << 16) + x);
1082                 spin_unlock_irqrestore(&par->DAClock, flags);
1083         }
1084
1085         /* Not supported so we fake it */
1086         if (cursor->set && FB_CUR_SETHOT) {
1087                 info->cursor.hot.x = cursor->hot.x;
1088                 info->cursor.hot.y = cursor->hot.y;
1089         }
1090
1091         if (cursor->set && FB_CUR_SETSHAPE) {
1092                 /*
1093                  * Voodoo 3 and above cards use 2 monochrome cursor patterns.
1094                  *    The reason is so the card can fetch 8 words at a time
1095                  * and are stored on chip for use for the next 8 scanlines.
1096                  * This reduces the number of times for access to draw the
1097                  * cursor for each screen refresh.
1098                  *    Each pattern is a bitmap of 64 bit wide and 64 bit high
1099                  * (total of 8192 bits or 1024 Kbytes). The two patterns are
1100                  * stored in such a way that pattern 0 always resides in the
1101                  * lower half (least significant 64 bits) of a 128 bit word
1102                  * and pattern 1 the upper half. If you examine the data of
1103                  * the cursor image the graphics card uses then from the
1104                  * begining you see line one of pattern 0, line one of
1105                  * pattern 1, line two of pattern 0, line two of pattern 1,
1106                  * etc etc. The linear stride for the cursor is always 16 bytes
1107                  * (128 bits) which is the maximum cursor width times two for
1108                  * the two monochrome patterns.
1109                  */
1110                 u8 *cursorbase = (u8 *) info->cursor.image.data;
1111                 char *bitmap = (char *)cursor->image.data;
1112                 char *mask = (char *) cursor->mask;
1113                 int i, j, k, h = 0;
1114
1115                 for (i = 0; i < 64; i++) {
1116                         if (i < cursor->image.height) {
1117                                 j = (cursor->image.width + 7) >> 3;
1118                                 k = 8 - j;
1119
1120                                 for (;j > 0; j--) {
1121                                 /* Pattern 0. Copy the cursor bitmap to it */
1122                                         fb_writeb(*bitmap, cursorbase + h);
1123                                         bitmap++;
1124                                 /* Pattern 1. Copy the cursor mask to it */
1125                                         fb_writeb(*mask, cursorbase + h + 8);
1126                                         mask++;
1127                                         h++;
1128                                 }
1129                                 for (;k > 0; k--) {
1130                                         fb_writeb(0, cursorbase + h);
1131                                         fb_writeb(~0, cursorbase + h + 8);
1132                                         h++;
1133                                 }
1134                         } else {
1135                                 fb_writel(0, cursorbase + h);
1136                                 fb_writel(0, cursorbase + h + 4);
1137                                 fb_writel(~0, cursorbase + h + 8);
1138                                 fb_writel(~0, cursorbase + h + 12);
1139                                 h += 16;
1140                         }
1141                 }
1142         }
1143         /* Turn the cursor on */
1144         cursor->enable = 1;
1145         info->cursor = *cursor;
1146         mod_timer(&par->hwcursor.timer, jiffies+HZ/2);
1147         spin_lock_irqsave(&par->DAClock, flags);
1148         banshee_make_room(par, 1);
1149         tdfx_outl(par, VIDPROCCFG, par->hwcursor.enable);
1150         spin_unlock_irqrestore(&par->DAClock, flags);
1151         return 0;
1152 }
1153 #endif
1154
1155 /**
1156  *      tdfxfb_probe - Device Initializiation
1157  *
1158  *      @pdev:  PCI Device to initialize
1159  *      @id:    PCI Device ID
1160  *
1161  *      Initializes and allocates resources for PCI device @pdev.
1162  *
1163  */
1164 static int __devinit tdfxfb_probe(struct pci_dev *pdev,
1165                                   const struct pci_device_id *id)
1166 {
1167         struct tdfx_par *default_par;
1168         struct fb_info *info;
1169         int err, lpitch;
1170
1171         if ((err = pci_enable_device(pdev))) {
1172                 printk(KERN_WARNING "tdfxfb: Can't enable pdev: %d\n", err);
1173                 return err;
1174         }
1175
1176         info = framebuffer_alloc(sizeof(struct tdfx_par), &pdev->dev);
1177
1178         if (!info)
1179                 return -ENOMEM;
1180                 
1181         default_par = info->par;
1182  
1183         /* Configure the default fb_fix_screeninfo first */
1184         switch (pdev->device) {
1185                 case PCI_DEVICE_ID_3DFX_BANSHEE:        
1186                         strcat(tdfx_fix.id, " Banshee");
1187                         default_par->max_pixclock = BANSHEE_MAX_PIXCLOCK;
1188                         break;
1189                 case PCI_DEVICE_ID_3DFX_VOODOO3:
1190                         strcat(tdfx_fix.id, " Voodoo3");
1191                         default_par->max_pixclock = VOODOO3_MAX_PIXCLOCK;
1192                         break;
1193                 case PCI_DEVICE_ID_3DFX_VOODOO5:
1194                         strcat(tdfx_fix.id, " Voodoo5");
1195                         default_par->max_pixclock = VOODOO5_MAX_PIXCLOCK;
1196                         break;
1197         }
1198
1199         tdfx_fix.mmio_start = pci_resource_start(pdev, 0);
1200         tdfx_fix.mmio_len = pci_resource_len(pdev, 0);
1201         default_par->regbase_virt = ioremap_nocache(tdfx_fix.mmio_start, tdfx_fix.mmio_len);
1202         if (!default_par->regbase_virt) {
1203                 printk("fb: Can't remap %s register area.\n", tdfx_fix.id);
1204                 goto out_err;
1205         }
1206     
1207         if (!request_mem_region(pci_resource_start(pdev, 0),
1208             pci_resource_len(pdev, 0), "tdfx regbase")) {
1209                 printk(KERN_WARNING "tdfxfb: Can't reserve regbase\n");
1210                 goto out_err;
1211         } 
1212
1213         tdfx_fix.smem_start = pci_resource_start(pdev, 1);
1214         if (!(tdfx_fix.smem_len = do_lfb_size(default_par, pdev->device))) {
1215                 printk("fb: Can't count %s memory.\n", tdfx_fix.id);
1216                 release_mem_region(pci_resource_start(pdev, 0),
1217                                    pci_resource_len(pdev, 0));
1218                 goto out_err;   
1219         }
1220
1221         if (!request_mem_region(pci_resource_start(pdev, 1),
1222              pci_resource_len(pdev, 1), "tdfx smem")) {
1223                 printk(KERN_WARNING "tdfxfb: Can't reserve smem\n");
1224                 release_mem_region(pci_resource_start(pdev, 0),
1225                                    pci_resource_len(pdev, 0));
1226                 goto out_err;
1227         }
1228
1229         info->screen_base = ioremap_nocache(tdfx_fix.smem_start, 
1230                                             tdfx_fix.smem_len);
1231         if (!info->screen_base) {
1232                 printk("fb: Can't remap %s framebuffer.\n", tdfx_fix.id);
1233                 release_mem_region(pci_resource_start(pdev, 1),
1234                                    pci_resource_len(pdev, 1));
1235                 release_mem_region(pci_resource_start(pdev, 0),
1236                                    pci_resource_len(pdev, 0));
1237                 goto out_err;
1238         }
1239
1240         default_par->iobase = pci_resource_start(pdev, 2);
1241     
1242         if (!request_region(pci_resource_start(pdev, 2),
1243             pci_resource_len(pdev, 2), "tdfx iobase")) {
1244                 printk(KERN_WARNING "tdfxfb: Can't reserve iobase\n");
1245                 release_mem_region(pci_resource_start(pdev, 1),
1246                                    pci_resource_len(pdev, 1));
1247                 release_mem_region(pci_resource_start(pdev, 0),
1248                                    pci_resource_len(pdev, 0));
1249                 goto out_err;
1250         }
1251
1252         printk("fb: %s memory = %dK\n", tdfx_fix.id, tdfx_fix.smem_len >> 10);
1253
1254         tdfx_fix.ypanstep       = nopan ? 0 : 1;
1255         tdfx_fix.ywrapstep      = nowrap ? 0 : 1;
1256    
1257         info->fbops             = &tdfxfb_ops;
1258         info->fix               = tdfx_fix;     
1259         info->pseudo_palette    = default_par->palette;
1260         info->flags             = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
1261 #ifdef CONFIG_FB_3DFX_ACCEL
1262         info->flags             |= FBINFO_HWACCEL_FILLRECT |
1263                 FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_IMAGEBLIT;
1264 #endif
1265
1266         if (!mode_option)
1267                 mode_option = "640x480@60";
1268          
1269         err = fb_find_mode(&info->var, info, mode_option, NULL, 0, NULL, 8); 
1270         if (!err || err == 4)
1271                 info->var = tdfx_var;
1272
1273         /* maximize virtual vertical length */
1274         lpitch = info->var.xres_virtual * ((info->var.bits_per_pixel + 7) >> 3);
1275         info->var.yres_virtual = info->fix.smem_len/lpitch;
1276         if (info->var.yres_virtual < info->var.yres)
1277                 goto out_err;
1278
1279 #ifdef CONFIG_FB_3DFX_ACCEL
1280         /*
1281          * FIXME: Limit var->yres_virtual to 4096 because of screen artifacts
1282          * during scrolling. This is only present if 2D acceleration is
1283          * enabled.
1284          */
1285         if (info->var.yres_virtual > 4096)
1286                 info->var.yres_virtual = 4096;
1287 #endif /* CONFIG_FB_3DFX_ACCEL */
1288
1289         if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
1290                 printk(KERN_WARNING "tdfxfb: Can't allocate color map\n");
1291                 goto out_err;
1292         }
1293
1294         if (register_framebuffer(info) < 0) {
1295                 printk("tdfxfb: can't register framebuffer\n");
1296                 fb_dealloc_cmap(&info->cmap);
1297                 goto out_err;
1298         }
1299         /*
1300          * Our driver data
1301          */
1302         pci_set_drvdata(pdev, info);
1303         return 0; 
1304
1305 out_err:
1306         /*
1307          * Cleanup after anything that was remapped/allocated.
1308          */
1309         if (default_par->regbase_virt)
1310                 iounmap(default_par->regbase_virt);
1311         if (info->screen_base)
1312                 iounmap(info->screen_base);
1313         framebuffer_release(info);
1314         return -ENXIO;
1315 }
1316
1317 #ifndef MODULE
1318 static void tdfxfb_setup(char *options)
1319 {
1320         char* this_opt;
1321
1322         if (!options || !*options)
1323                 return;
1324
1325         while ((this_opt = strsep(&options, ",")) != NULL) {
1326                 if (!*this_opt)
1327                         continue;
1328                 if(!strcmp(this_opt, "nopan")) {
1329                         nopan = 1;
1330                 } else if(!strcmp(this_opt, "nowrap")) {
1331                         nowrap = 1;
1332                 } else {
1333                         mode_option = this_opt;
1334                 }
1335         }
1336 }
1337 #endif
1338
1339 /**
1340  *      tdfxfb_remove - Device removal
1341  *
1342  *      @pdev:  PCI Device to cleanup
1343  *
1344  *      Releases all resources allocated during the course of the driver's
1345  *      lifetime for the PCI device @pdev.
1346  *
1347  */
1348 static void __devexit tdfxfb_remove(struct pci_dev *pdev)
1349 {
1350         struct fb_info *info = pci_get_drvdata(pdev);
1351         struct tdfx_par *par = info->par;
1352
1353         unregister_framebuffer(info);
1354         iounmap(par->regbase_virt);
1355         iounmap(info->screen_base);
1356
1357         /* Clean up after reserved regions */
1358         release_region(pci_resource_start(pdev, 2),
1359                        pci_resource_len(pdev, 2));
1360         release_mem_region(pci_resource_start(pdev, 1),
1361                            pci_resource_len(pdev, 1));
1362         release_mem_region(pci_resource_start(pdev, 0),
1363                            pci_resource_len(pdev, 0));
1364         pci_set_drvdata(pdev, NULL);
1365         framebuffer_release(info);
1366 }
1367
1368 static int __init tdfxfb_init(void)
1369 {
1370 #ifndef MODULE
1371         char *option = NULL;
1372
1373         if (fb_get_options("tdfxfb", &option))
1374                 return -ENODEV;
1375
1376         tdfxfb_setup(option);
1377 #endif
1378         return pci_register_driver(&tdfxfb_driver);
1379 }
1380
1381 static void __exit tdfxfb_exit(void)
1382 {
1383         pci_unregister_driver(&tdfxfb_driver);
1384 }
1385
1386 MODULE_AUTHOR("Hannu Mallat <hmallat@cc.hut.fi>");
1387 MODULE_DESCRIPTION("3Dfx framebuffer device driver");
1388 MODULE_LICENSE("GPL");
1389  
1390 module_init(tdfxfb_init);
1391 module_exit(tdfxfb_exit);