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