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