5 * Author: Hannu Mallat <hmallat@cc.hut.fi>
7 * Copyright © 1999 Hannu Mallat
10 * Created : Thu Sep 23 18:17:43 1999, hmallat
11 * Last modified: Tue Nov 2 21:19:47 1999, hmallat
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.
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!
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).
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.
34 * The structure of this driver comes pretty much from the Permedia
35 * driver by Ilario Nardinocchi, which in turn is based on skeletonfb.
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
45 * 0.1.4 (released 2002-05-28) ported over to new fbdev api by James Simmons
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
55 * 0.1.1 (released 1999-10-07) added Voodoo3 support by Harold Oga.
56 * 0.1.0 (released 1999-10-06) initial version
60 #include <linux/module.h>
61 #include <linux/kernel.h>
62 #include <linux/errno.h>
63 #include <linux/string.h>
65 #include <linux/slab.h>
66 #include <linux/delay.h>
67 #include <linux/interrupt.h>
69 #include <linux/init.h>
70 #include <linux/pci.h>
71 #include <linux/nvram.h>
73 #include <linux/timer.h>
74 #include <linux/spinlock.h>
76 #include <video/tdfx.h>
80 #define DPRINTK(a,b...) printk(KERN_DEBUG "fb: %s: " a, __FUNCTION__ , ## b)
82 #define DPRINTK(a,b...)
85 #define BANSHEE_MAX_PIXCLOCK 270000
86 #define VOODOO3_MAX_PIXCLOCK 300000
87 #define VOODOO5_MAX_PIXCLOCK 350000
89 static struct fb_fix_screeninfo tdfx_fix __devinitdata = {
91 .type = FB_TYPE_PACKED_PIXELS,
92 .visual = FB_VISUAL_PSEUDOCOLOR,
95 .accel = FB_ACCEL_3DFX_BANSHEE
98 static struct fb_var_screeninfo tdfx_var __devinitdata = {
99 /* "640x480, 8 bpp @ 60 Hz */
103 .yres_virtual = 1024,
108 .activate = FB_ACTIVATE_NOW,
111 .accel_flags = FB_ACCELF_TEXT,
119 .vmode = FB_VMODE_NONINTERLACED
123 * PCI driver prototypes
125 static int __devinit tdfxfb_probe(struct pci_dev *pdev,
126 const struct pci_device_id *id);
127 static void __devexit tdfxfb_remove(struct pci_dev *pdev);
129 static struct pci_device_id tdfxfb_id_table[] = {
130 { PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_BANSHEE,
131 PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
133 { PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_VOODOO3,
134 PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
136 { PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_VOODOO5,
137 PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
142 static struct pci_driver tdfxfb_driver = {
144 .id_table = tdfxfb_id_table,
145 .probe = tdfxfb_probe,
146 .remove = __devexit_p(tdfxfb_remove),
149 MODULE_DEVICE_TABLE(pci, tdfxfb_id_table);
154 static int nopan = 0;
155 static int nowrap = 1; // not implemented (yet)
156 static char *mode_option __devinitdata = NULL;
158 /* -------------------------------------------------------------------------
159 * Hardware-specific funcions
160 * ------------------------------------------------------------------------- */
163 static inline u8 vga_inb(struct tdfx_par *par, u32 reg)
168 static inline void vga_outb(struct tdfx_par *par, u32 reg, u8 val)
173 static inline u8 vga_inb(struct tdfx_par *par, u32 reg)
175 return inb(par->iobase + reg - 0x300);
177 static inline void vga_outb(struct tdfx_par *par, u32 reg, u8 val)
179 outb(val, par->iobase + reg - 0x300);
183 static inline void gra_outb(struct tdfx_par *par, u32 idx, u8 val)
185 vga_outb(par, GRA_I, idx);
187 vga_outb(par, GRA_D, val);
191 static inline void seq_outb(struct tdfx_par *par, u32 idx, u8 val)
193 vga_outb(par, SEQ_I, idx);
195 vga_outb(par, SEQ_D, val);
199 static inline u8 seq_inb(struct tdfx_par *par, u32 idx)
201 vga_outb(par, SEQ_I, idx);
203 return vga_inb(par, SEQ_D);
206 static inline void crt_outb(struct tdfx_par *par, u32 idx, u8 val)
208 vga_outb(par, CRT_I, idx);
210 vga_outb(par, CRT_D, val);
214 static inline u8 crt_inb(struct tdfx_par *par, u32 idx)
216 vga_outb(par, CRT_I, idx);
218 return vga_inb(par, CRT_D);
221 static inline void att_outb(struct tdfx_par *par, u32 idx, u8 val)
225 tmp = vga_inb(par, IS1_R);
226 vga_outb(par, ATT_IW, idx);
227 vga_outb(par, ATT_IW, val);
230 static inline void vga_disable_video(struct tdfx_par *par)
234 s = seq_inb(par, 0x01) | 0x20;
235 seq_outb(par, 0x00, 0x01);
236 seq_outb(par, 0x01, s);
237 seq_outb(par, 0x00, 0x03);
240 static inline void vga_enable_video(struct tdfx_par *par)
244 s = seq_inb(par, 0x01) & 0xdf;
245 seq_outb(par, 0x00, 0x01);
246 seq_outb(par, 0x01, s);
247 seq_outb(par, 0x00, 0x03);
250 static inline void vga_enable_palette(struct tdfx_par *par)
254 vga_outb(par, ATT_IW, 0x20);
257 static inline u32 tdfx_inl(struct tdfx_par *par, unsigned int reg)
259 return readl(par->regbase_virt + reg);
262 static inline void tdfx_outl(struct tdfx_par *par, unsigned int reg, u32 val)
264 writel(val, par->regbase_virt + reg);
267 static inline void banshee_make_room(struct tdfx_par *par, int size)
269 /* Note: The Voodoo3's onboard FIFO has 32 slots. This loop
270 * won't quit if you ask for more. */
271 while ((tdfx_inl(par, STATUS) & 0x1f) < size - 1) ;
274 static int banshee_wait_idle(struct fb_info *info)
276 struct tdfx_par *par = info->par;
279 banshee_make_room(par, 1);
280 tdfx_outl(par, COMMAND_3D, COMMAND_3D_NOP);
283 i = (tdfx_inl(par, STATUS) & STATUS_BUSY) ? 0 : i + 1;
291 * Set the color of a palette entry in 8bpp mode
293 static inline void do_setpalentry(struct tdfx_par *par, unsigned regno, u32 c)
295 banshee_make_room(par, 2);
296 tdfx_outl(par, DACADDR, regno);
297 /* read after write makes it working */
298 tdfx_inl(par, DACADDR);
299 tdfx_outl(par, DACDATA, c);
302 static u32 do_calc_pll(int freq, int *freq_out)
304 int m, n, k, best_m, best_n, best_k, best_error;
308 best_n = best_m = best_k = 0;
310 for (k = 3; k >= 0; k--) {
311 for (m = 63; m >= 0; m--) {
313 * Estimate value of n that produces target frequency
314 * with current m and k
316 int n_estimated = (freq * (m + 2) * (1 << k) / fref) - 2;
318 /* Search neighborhood of estimated n */
319 for (n = max(0, n_estimated - 1);
320 n <= min(255, n_estimated + 1); n++) {
322 * Calculate PLL freqency with current m, k and
325 int f = fref * (n + 2) / (m + 2) / (1 << k);
326 int error = abs(f - freq);
329 * If this is the closest we've come to the
330 * target frequency then remember n, m and k
332 if (error < best_error) {
345 *freq_out = fref * (n + 2) / (m + 2) / (1 << k);
347 return (n << 8) | (m << 2) | k;
350 static void do_write_regs(struct fb_info *info, struct banshee_reg *reg)
352 struct tdfx_par *par = info->par;
355 banshee_wait_idle(info);
357 tdfx_outl(par, MISCINIT1, tdfx_inl(par, MISCINIT1) | 0x01);
359 crt_outb(par, 0x11, crt_inb(par, 0x11) & 0x7f); /* CRT unprotect */
361 banshee_make_room(par, 3);
362 tdfx_outl(par, VGAINIT1, reg->vgainit1 & 0x001FFFFF);
363 tdfx_outl(par, VIDPROCCFG, reg->vidcfg & ~0x00000001);
365 tdfx_outl(par, PLLCTRL1, reg->mempll);
366 tdfx_outl(par, PLLCTRL2, reg->gfxpll);
368 tdfx_outl(par, PLLCTRL0, reg->vidpll);
370 vga_outb(par, MISC_W, reg->misc[0x00] | 0x01);
372 for (i = 0; i < 5; i++)
373 seq_outb(par, i, reg->seq[i]);
375 for (i = 0; i < 25; i++)
376 crt_outb(par, i, reg->crt[i]);
378 for (i = 0; i < 9; i++)
379 gra_outb(par, i, reg->gra[i]);
381 for (i = 0; i < 21; i++)
382 att_outb(par, i, reg->att[i]);
384 crt_outb(par, 0x1a, reg->ext[0]);
385 crt_outb(par, 0x1b, reg->ext[1]);
387 vga_enable_palette(par);
388 vga_enable_video(par);
390 banshee_make_room(par, 11);
391 tdfx_outl(par, VGAINIT0, reg->vgainit0);
392 tdfx_outl(par, DACMODE, reg->dacmode);
393 tdfx_outl(par, VIDDESKSTRIDE, reg->stride);
394 tdfx_outl(par, HWCURPATADDR, 0);
396 tdfx_outl(par, VIDSCREENSIZE, reg->screensize);
397 tdfx_outl(par, VIDDESKSTART, reg->startaddr);
398 tdfx_outl(par, VIDPROCCFG, reg->vidcfg);
399 tdfx_outl(par, VGAINIT1, reg->vgainit1);
400 tdfx_outl(par, MISCINIT0, reg->miscinit0);
402 banshee_make_room(par, 8);
403 tdfx_outl(par, SRCBASE, reg->srcbase);
404 tdfx_outl(par, DSTBASE, reg->dstbase);
405 tdfx_outl(par, COMMANDEXTRA_2D, 0);
406 tdfx_outl(par, CLIP0MIN, 0);
407 tdfx_outl(par, CLIP0MAX, 0x0fff0fff);
408 tdfx_outl(par, CLIP1MIN, 0);
409 tdfx_outl(par, CLIP1MAX, 0x0fff0fff);
410 tdfx_outl(par, SRCXY, 0);
412 banshee_wait_idle(info);
415 static unsigned long do_lfb_size(struct tdfx_par *par, unsigned short dev_id)
422 int chip_size; /* in MB */
426 draminit0 = tdfx_inl(par, DRAMINIT0);
427 draminit1 = tdfx_inl(par, DRAMINIT1);
429 num_chips = (draminit0 & DRAMINIT0_SGRAM_NUM) ? 8 : 4;
431 if (dev_id < PCI_DEVICE_ID_3DFX_VOODOO5) {
432 /* Banshee/Voodoo3 */
433 has_sgram = draminit1 & DRAMINIT1_MEM_SDRAM;
436 chip_size = (draminit0 & DRAMINIT0_SGRAM_TYPE) ? 2 : 1;
440 chip_size = 1 << ((draminit0 & DRAMINIT0_SGRAM_TYPE_MASK) >> DRAMINIT0_SGRAM_TYPE_SHIFT);
442 lfbsize = num_chips * chip_size * 1024 * 1024;
444 /* disable block writes for SDRAM */
445 miscinit1 = tdfx_inl(par, MISCINIT1);
446 miscinit1 |= has_sgram ? 0 : MISCINIT1_2DBLOCK_DIS;
447 miscinit1 |= MISCINIT1_CLUT_INV;
449 banshee_make_room(par, 1);
450 tdfx_outl(par, MISCINIT1, miscinit1);
454 /* ------------------------------------------------------------------------- */
456 static int tdfxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
458 struct tdfx_par *par = info->par;
461 if (var->bits_per_pixel != 8 && var->bits_per_pixel != 16 &&
462 var->bits_per_pixel != 24 && var->bits_per_pixel != 32) {
463 DPRINTK("depth not supported: %u\n", var->bits_per_pixel);
467 if (var->xres != var->xres_virtual)
468 var->xres_virtual = var->xres;
470 if (var->yres > var->yres_virtual)
471 var->yres_virtual = var->yres;
474 DPRINTK("xoffset not supported\n");
478 /* Banshee doesn't support interlace, but Voodoo4/5 and probably Voodoo3 do. */
479 /* no direct information about device id now? use max_pixclock for this... */
480 if (((var->vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) &&
481 (par->max_pixclock < VOODOO3_MAX_PIXCLOCK)) {
482 DPRINTK("interlace not supported\n");
486 var->xres = (var->xres + 15) & ~15; /* could sometimes be 8 */
487 lpitch = var->xres * ((var->bits_per_pixel + 7) >> 3);
489 if (var->xres < 320 || var->xres > 2048) {
490 DPRINTK("width not supported: %u\n", var->xres);
494 if (var->yres < 200 || var->yres > 2048) {
495 DPRINTK("height not supported: %u\n", var->yres);
499 if (lpitch * var->yres_virtual > info->fix.smem_len) {
500 var->yres_virtual = info->fix.smem_len / lpitch;
501 if (var->yres_virtual < var->yres) {
502 DPRINTK("no memory for screen (%ux%ux%u)\n",
503 var->xres, var->yres_virtual,
504 var->bits_per_pixel);
509 if (PICOS2KHZ(var->pixclock) > par->max_pixclock) {
510 DPRINTK("pixclock too high (%ldKHz)\n",
511 PICOS2KHZ(var->pixclock));
515 var->transp.offset = 0;
516 var->transp.length = 0;
517 switch (var->bits_per_pixel) {
519 var->red.length = var->green.length = var->blue.length = 8;
522 var->red.offset = 11;
524 var->green.offset = 5;
525 var->green.length = 6;
526 var->blue.offset = 0;
527 var->blue.length = 5;
530 var->transp.offset = 24;
531 var->transp.length = 8;
533 var->red.offset = 16;
534 var->green.offset = 8;
535 var->blue.offset = 0;
536 var->red.length = var->green.length = var->blue.length = 8;
539 var->height = var->width = -1;
541 var->accel_flags = FB_ACCELF_TEXT;
543 DPRINTK("Checking graphics mode at %dx%d depth %d\n",
544 var->xres, var->yres, var->bits_per_pixel);
548 static int tdfxfb_set_par(struct fb_info *info)
550 struct tdfx_par *par = info->par;
551 u32 hdispend, hsyncsta, hsyncend, htotal;
552 u32 hd, hs, he, ht, hbs, hbe;
553 u32 vd, vs, ve, vt, vbs, vbe;
554 struct banshee_reg reg;
560 memset(®, 0, sizeof(reg));
561 cpp = (info->var.bits_per_pixel + 7) / 8;
563 reg.vidcfg = VIDCFG_VIDPROC_ENABLE | VIDCFG_DESK_ENABLE |
565 ((cpp - 1) << VIDCFG_PIXFMT_SHIFT) |
566 (cpp != 1 ? VIDCFG_CLUT_BYPASS : 0);
569 freq = PICOS2KHZ(info->var.pixclock);
572 reg.vidcfg &= ~VIDCFG_2X;
574 hdispend = info->var.xres;
575 hsyncsta = hdispend + info->var.right_margin;
576 hsyncend = hsyncsta + info->var.hsync_len;
577 htotal = hsyncend + info->var.left_margin;
579 if (freq > par->max_pixclock / 2) {
580 freq = freq > par->max_pixclock ? par->max_pixclock : freq;
581 reg.dacmode |= DACMODE_2X;
582 reg.vidcfg |= VIDCFG_2X;
589 hd = wd = (hdispend >> 3) - 1;
590 hs = (hsyncsta >> 3) - 1;
591 he = (hsyncend >> 3) - 1;
592 ht = (htotal >> 3) - 1;
596 if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE) {
597 vbs = vd = (info->var.yres << 1) - 1;
598 vs = vd + (info->var.lower_margin << 1);
599 ve = vs + (info->var.vsync_len << 1);
600 vbe = vt = ve + (info->var.upper_margin << 1) - 1;
602 vbs = vd = info->var.yres - 1;
603 vs = vd + info->var.lower_margin;
604 ve = vs + info->var.vsync_len;
605 vbe = vt = ve + info->var.upper_margin - 1;
608 /* this is all pretty standard VGA register stuffing */
609 reg.misc[0x00] = 0x0f |
610 (info->var.xres < 400 ? 0xa0 :
611 info->var.xres < 480 ? 0x60 :
612 info->var.xres < 768 ? 0xe0 : 0x20);
614 reg.gra[0x00] = 0x00;
615 reg.gra[0x01] = 0x00;
616 reg.gra[0x02] = 0x00;
617 reg.gra[0x03] = 0x00;
618 reg.gra[0x04] = 0x00;
619 reg.gra[0x05] = 0x40;
620 reg.gra[0x06] = 0x05;
621 reg.gra[0x07] = 0x0f;
622 reg.gra[0x08] = 0xff;
624 reg.att[0x00] = 0x00;
625 reg.att[0x01] = 0x01;
626 reg.att[0x02] = 0x02;
627 reg.att[0x03] = 0x03;
628 reg.att[0x04] = 0x04;
629 reg.att[0x05] = 0x05;
630 reg.att[0x06] = 0x06;
631 reg.att[0x07] = 0x07;
632 reg.att[0x08] = 0x08;
633 reg.att[0x09] = 0x09;
634 reg.att[0x0a] = 0x0a;
635 reg.att[0x0b] = 0x0b;
636 reg.att[0x0c] = 0x0c;
637 reg.att[0x0d] = 0x0d;
638 reg.att[0x0e] = 0x0e;
639 reg.att[0x0f] = 0x0f;
640 reg.att[0x10] = 0x41;
641 reg.att[0x11] = 0x00;
642 reg.att[0x12] = 0x0f;
643 reg.att[0x13] = 0x00;
644 reg.att[0x14] = 0x00;
646 reg.seq[0x00] = 0x03;
647 reg.seq[0x01] = 0x01; /* fixme: clkdiv2? */
648 reg.seq[0x02] = 0x0f;
649 reg.seq[0x03] = 0x00;
650 reg.seq[0x04] = 0x0e;
652 reg.crt[0x00] = ht - 4;
655 reg.crt[0x03] = 0x80 | (hbe & 0x1f);
657 reg.crt[0x05] = ((hbe & 0x20) << 2) | (he & 0x1f);
659 reg.crt[0x07] = ((vs & 0x200) >> 2) |
660 ((vd & 0x200) >> 3) |
661 ((vt & 0x200) >> 4) | 0x10 |
662 ((vbs & 0x100) >> 5) |
663 ((vs & 0x100) >> 6) |
664 ((vd & 0x100) >> 7) |
666 reg.crt[0x08] = 0x00;
667 reg.crt[0x09] = 0x40 | ((vbs & 0x200) >> 4);
668 reg.crt[0x0a] = 0x00;
669 reg.crt[0x0b] = 0x00;
670 reg.crt[0x0c] = 0x00;
671 reg.crt[0x0d] = 0x00;
672 reg.crt[0x0e] = 0x00;
673 reg.crt[0x0f] = 0x00;
675 reg.crt[0x11] = (ve & 0x0f) | 0x20;
678 reg.crt[0x14] = 0x00;
680 reg.crt[0x16] = vbe + 1;
681 reg.crt[0x17] = 0xc3;
682 reg.crt[0x18] = 0xff;
684 /* Banshee's nonvga stuff */
685 reg.ext[0x00] = (((ht & 0x100) >> 8) |
686 ((hd & 0x100) >> 6) |
687 ((hbs & 0x100) >> 4) |
688 ((hbe & 0x40) >> 1) |
689 ((hs & 0x100) >> 2) |
691 reg.ext[0x01] = (((vt & 0x400) >> 10) |
692 ((vd & 0x400) >> 8) |
693 ((vbs & 0x400) >> 6) |
694 ((vbe & 0x400) >> 4));
696 reg.vgainit0 = VGAINIT0_8BIT_DAC |
697 VGAINIT0_EXT_ENABLE |
698 VGAINIT0_WAKEUP_3C3 |
699 VGAINIT0_ALT_READBACK |
700 VGAINIT0_EXTSHIFTOUT;
701 reg.vgainit1 = tdfx_inl(par, VGAINIT1) & 0x1fffff;
706 reg.cursc1 = 0xffffff;
708 reg.stride = info->var.xres * cpp;
709 reg.startaddr = par->baseline * reg.stride;
710 reg.srcbase = reg.startaddr;
711 reg.dstbase = reg.startaddr;
714 freq = PICOS2KHZ(info->var.pixclock);
716 reg.dacmode &= ~DACMODE_2X;
717 reg.vidcfg &= ~VIDCFG_2X;
718 if (freq > par->max_pixclock / 2) {
719 freq = freq > par->max_pixclock ? par->max_pixclock : freq;
720 reg.dacmode |= DACMODE_2X;
721 reg.vidcfg |= VIDCFG_2X;
723 reg.vidpll = do_calc_pll(freq, &fout);
725 reg.mempll = do_calc_pll(..., &fout);
726 reg.gfxpll = do_calc_pll(..., &fout);
729 if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE) {
730 reg.screensize = info->var.xres | (info->var.yres << 13);
731 reg.vidcfg |= VIDCFG_HALF_MODE;
732 reg.crt[0x09] |= 0x80;
734 reg.screensize = info->var.xres | (info->var.yres << 12);
735 reg.vidcfg &= ~VIDCFG_HALF_MODE;
737 if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED)
738 reg.vidcfg |= VIDCFG_INTERLACE;
739 reg.miscinit0 = tdfx_inl(par, MISCINIT0);
741 #if defined(__BIG_ENDIAN)
742 switch (info->var.bits_per_pixel) {
745 reg.miscinit0 &= ~(1 << 30);
746 reg.miscinit0 &= ~(1 << 31);
749 reg.miscinit0 |= (1 << 30);
750 reg.miscinit0 |= (1 << 31);
753 reg.miscinit0 |= (1 << 30);
754 reg.miscinit0 &= ~(1 << 31);
758 do_write_regs(info, ®);
760 /* Now change fb_fix_screeninfo according to changes in par */
761 info->fix.line_length =
762 info->var.xres * ((info->var.bits_per_pixel + 7) >> 3);
763 info->fix.visual = (info->var.bits_per_pixel == 8)
764 ? FB_VISUAL_PSEUDOCOLOR
765 : FB_VISUAL_TRUECOLOR;
766 DPRINTK("Graphics mode is now set at %dx%d depth %d\n",
767 info->var.xres, info->var.yres, info->var.bits_per_pixel);
771 /* A handy macro shamelessly pinched from matroxfb */
772 #define CNVT_TOHW(val, width) ((((val)<<(width))+0x7FFF-(val))>>16)
774 static int tdfxfb_setcolreg(unsigned regno, unsigned red, unsigned green,
775 unsigned blue, unsigned transp,
776 struct fb_info *info)
778 struct tdfx_par *par = info->par;
781 if (regno >= info->cmap.len || regno > 255)
784 /* grayscale works only partially under directcolor */
785 if (info->var.grayscale) {
786 /* grayscale = 0.30*R + 0.59*G + 0.11*B */
787 red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
790 switch (info->fix.visual) {
791 case FB_VISUAL_PSEUDOCOLOR:
792 rgbcol =(((u32)red & 0xff00) << 8) |
793 (((u32)green & 0xff00) << 0) |
794 (((u32)blue & 0xff00) >> 8);
795 do_setpalentry(par, regno, rgbcol);
797 /* Truecolor has no hardware color palettes. */
798 case FB_VISUAL_TRUECOLOR:
800 rgbcol = (CNVT_TOHW(red, info->var.red.length) <<
801 info->var.red.offset) |
802 (CNVT_TOHW(green, info->var.green.length) <<
803 info->var.green.offset) |
804 (CNVT_TOHW(blue, info->var.blue.length) <<
805 info->var.blue.offset) |
806 (CNVT_TOHW(transp, info->var.transp.length) <<
807 info->var.transp.offset);
808 par->palette[regno] = rgbcol;
813 DPRINTK("bad depth %u\n", info->var.bits_per_pixel);
820 /* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */
821 static int tdfxfb_blank(int blank, struct fb_info *info)
823 struct tdfx_par *par = info->par;
824 u32 dacmode, state = 0, vgablank = 0;
826 dacmode = tdfx_inl(par, DACMODE);
829 case FB_BLANK_UNBLANK: /* Screen: On; HSync: On, VSync: On */
833 case FB_BLANK_NORMAL: /* Screen: Off; HSync: On, VSync: On */
837 case FB_BLANK_VSYNC_SUSPEND: /* Screen: Off; HSync: On, VSync: Off */
841 case FB_BLANK_HSYNC_SUSPEND: /* Screen: Off; HSync: Off, VSync: On */
845 case FB_BLANK_POWERDOWN: /* Screen: Off; HSync: Off, VSync: Off */
846 state = BIT(1) | BIT(3);
851 dacmode &= ~(BIT(1) | BIT(3));
853 banshee_make_room(par, 1);
854 tdfx_outl(par, DACMODE, dacmode);
856 vga_disable_video(par);
858 vga_enable_video(par);
863 * Set the starting position of the visible screen to var->yoffset
865 static int tdfxfb_pan_display(struct fb_var_screeninfo *var,
866 struct fb_info *info)
868 struct tdfx_par *par = info->par;
871 if (nopan || var->xoffset || (var->yoffset > var->yres_virtual))
873 if ((var->yoffset + var->yres > var->yres_virtual && nowrap))
876 addr = var->yoffset * info->fix.line_length;
877 banshee_make_room(par, 1);
878 tdfx_outl(par, VIDDESKSTART, addr);
880 info->var.xoffset = var->xoffset;
881 info->var.yoffset = var->yoffset;
885 #ifdef CONFIG_FB_3DFX_ACCEL
887 * FillRect 2D command (solidfill or invert (via ROP_XOR))
889 static void tdfxfb_fillrect(struct fb_info *info,
890 const struct fb_fillrect *rect)
892 struct tdfx_par *par = info->par;
893 u32 bpp = info->var.bits_per_pixel;
894 u32 stride = info->fix.line_length;
895 u32 fmt= stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
901 if (rect->rop == ROP_COPY)
902 tdfx_rop = TDFX_ROP_COPY;
904 tdfx_rop = TDFX_ROP_XOR;
906 /* asume always rect->height < 4096 */
907 if (dy + rect->height > 4095) {
908 dstbase = stride * dy;
911 /* asume always rect->width < 4096 */
912 if (dx + rect->width > 4095) {
913 dstbase += dx * bpp >> 3;
916 banshee_make_room(par, 6);
917 tdfx_outl(par, DSTFORMAT, fmt);
918 if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR) {
919 tdfx_outl(par, COLORFORE, rect->color);
920 } else { /* FB_VISUAL_TRUECOLOR */
921 tdfx_outl(par, COLORFORE, par->palette[rect->color]);
923 tdfx_outl(par, COMMAND_2D, COMMAND_2D_FILLRECT | (tdfx_rop << 24));
924 tdfx_outl(par, DSTBASE, dstbase);
925 tdfx_outl(par, DSTSIZE, rect->width | (rect->height << 16));
926 tdfx_outl(par, LAUNCH_2D, dx | (dy << 16));
930 * Screen-to-Screen BitBlt 2D command (for the bmove fb op.)
932 static void tdfxfb_copyarea(struct fb_info *info,
933 const struct fb_copyarea *area)
935 struct tdfx_par *par = info->par;
936 u32 sx = area->sx, sy = area->sy, dx = area->dx, dy = area->dy;
937 u32 bpp = info->var.bits_per_pixel;
938 u32 stride = info->fix.line_length;
939 u32 blitcmd = COMMAND_2D_S2S_BITBLT | (TDFX_ROP_COPY << 24);
940 u32 fmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
944 /* asume always area->height < 4096 */
945 if (sy + area->height > 4095) {
946 srcbase = stride * sy;
949 /* asume always area->width < 4096 */
950 if (sx + area->width > 4095) {
951 srcbase += sx * bpp >> 3;
954 /* asume always area->height < 4096 */
955 if (dy + area->height > 4095) {
956 dstbase = stride * dy;
959 /* asume always area->width < 4096 */
960 if (dx + area->width > 4095) {
961 dstbase += dx * bpp >> 3;
966 if (area->sx <= area->dx) {
969 sx += area->width - 1;
970 dx += area->width - 1;
972 if (area->sy <= area->dy) {
975 sy += area->height - 1;
976 dy += area->height - 1;
979 banshee_make_room(par, 8);
981 tdfx_outl(par, SRCFORMAT, fmt);
982 tdfx_outl(par, DSTFORMAT, fmt);
983 tdfx_outl(par, COMMAND_2D, blitcmd);
984 tdfx_outl(par, DSTSIZE, area->width | (area->height << 16));
985 tdfx_outl(par, DSTXY, dx | (dy << 16));
986 tdfx_outl(par, SRCBASE, srcbase);
987 tdfx_outl(par, DSTBASE, dstbase);
988 tdfx_outl(par, LAUNCH_2D, sx | (sy << 16));
991 static void tdfxfb_imageblit(struct fb_info *info, const struct fb_image *image)
993 struct tdfx_par *par = info->par;
994 int size = image->height * ((image->width * image->depth + 7) >> 3);
996 int i, stride = info->fix.line_length;
997 u32 bpp = info->var.bits_per_pixel;
998 u32 dstfmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
999 u8 *chardata = (u8 *) image->data;
1005 if (image->depth != 1) {
1006 //banshee_make_room(par, 6 + ((size + 3) >> 2));
1007 //srcfmt = stride | ((bpp+((bpp==8) ? 0 : 8)) << 13) | 0x400000;
1008 cfb_imageblit(info, image);
1011 banshee_make_room(par, 9);
1012 switch (info->fix.visual) {
1013 case FB_VISUAL_PSEUDOCOLOR:
1014 tdfx_outl(par, COLORFORE, image->fg_color);
1015 tdfx_outl(par, COLORBACK, image->bg_color);
1017 case FB_VISUAL_TRUECOLOR:
1019 tdfx_outl(par, COLORFORE,
1020 par->palette[image->fg_color]);
1021 tdfx_outl(par, COLORBACK,
1022 par->palette[image->bg_color]);
1025 srcfmt = 0x400000 | BIT(20);
1029 /* asume always image->height < 4096 */
1030 if (dy + image->height > 4095) {
1031 dstbase = stride * dy;
1034 /* asume always image->width < 4096 */
1035 if (dx + image->width > 4095) {
1036 dstbase += dx * bpp >> 3;
1040 tdfx_outl(par, DSTBASE, dstbase);
1041 tdfx_outl(par, SRCXY, 0);
1042 tdfx_outl(par, DSTXY, dx | (dy << 16));
1043 tdfx_outl(par, COMMAND_2D, COMMAND_2D_H2S_BITBLT | (TDFX_ROP_COPY << 24));
1044 tdfx_outl(par, SRCFORMAT, srcfmt);
1045 tdfx_outl(par, DSTFORMAT, dstfmt);
1046 tdfx_outl(par, DSTSIZE, image->width | (image->height << 16));
1048 /* A count of how many free FIFO entries we've requested.
1049 * When this goes negative, we need to request more. */
1052 /* Send four bytes at a time of data */
1053 for (i = (size >> 2); i > 0; i--) {
1054 if (--fifo_free < 0) {
1056 banshee_make_room(par, fifo_free);
1058 tdfx_outl(par, LAUNCH_2D, *(u32*)chardata);
1062 /* Send the leftovers now */
1063 banshee_make_room(par, 3);
1069 tdfx_outl(par, LAUNCH_2D, *chardata);
1072 tdfx_outl(par, LAUNCH_2D, *(u16*)chardata);
1075 tdfx_outl(par, LAUNCH_2D,
1076 *(u16*)chardata | ((chardata[3]) << 24));
1080 #endif /* CONFIG_FB_3DFX_ACCEL */
1082 #ifdef TDFX_HARDWARE_CURSOR
1083 static int tdfxfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
1085 struct tdfx_par *par = info->par;
1086 unsigned long flags;
1089 * If the cursor is not be changed this means either we want the
1090 * current cursor state (if enable is set) or we want to query what
1091 * we can do with the cursor (if enable is not set)
1096 /* Too large of a cursor :-( */
1097 if (cursor->image.width > 64 || cursor->image.height > 64)
1101 * If we are going to be changing things we should disable
1104 if (info->cursor.enable) {
1105 spin_lock_irqsave(&par->DAClock, flags);
1106 info->cursor.enable = 0;
1107 del_timer(&(par->hwcursor.timer));
1108 tdfx_outl(par, VIDPROCCFG, par->hwcursor.disable);
1109 spin_unlock_irqrestore(&par->DAClock, flags);
1112 /* Disable the Cursor */
1113 if ((cursor->set && FB_CUR_SETCUR) && !cursor->enable)
1116 /* fix cursor color - XFree86 forgets to restore it properly */
1117 if (cursor->set && FB_CUR_SETCMAP) {
1118 struct fb_cmap cmap = cursor->image.cmap;
1119 unsigned long bg_color, fg_color;
1121 cmap.len = 2; /* Voodoo 3+ only support 2 color cursors */
1122 fg_color = ((cmap.red[cmap.start] << 16) |
1123 (cmap.green[cmap.start] << 8) |
1124 (cmap.blue[cmap.start]));
1125 bg_color = ((cmap.red[cmap.start + 1] << 16) |
1126 (cmap.green[cmap.start + 1] << 8) |
1127 (cmap.blue[cmap.start + 1]));
1128 fb_copy_cmap(&cmap, &info->cursor.image.cmap);
1129 spin_lock_irqsave(&par->DAClock, flags);
1130 banshee_make_room(par, 2);
1131 tdfx_outl(par, HWCURC0, bg_color);
1132 tdfx_outl(par, HWCURC1, fg_color);
1133 spin_unlock_irqrestore(&par->DAClock, flags);
1136 if (cursor->set && FB_CUR_SETPOS) {
1139 x = cursor->image.dx;
1140 y = cursor->image.dy;
1141 y -= info->var.yoffset;
1142 info->cursor.image.dx = x;
1143 info->cursor.image.dy = y;
1146 spin_lock_irqsave(&par->DAClock, flags);
1147 banshee_make_room(par, 1);
1148 tdfx_outl(par, HWCURLOC, (y << 16) + x);
1149 spin_unlock_irqrestore(&par->DAClock, flags);
1152 /* Not supported so we fake it */
1153 if (cursor->set && FB_CUR_SETHOT) {
1154 info->cursor.hot.x = cursor->hot.x;
1155 info->cursor.hot.y = cursor->hot.y;
1158 if (cursor->set && FB_CUR_SETSHAPE) {
1160 * Voodoo 3 and above cards use 2 monochrome cursor patterns.
1161 * The reason is so the card can fetch 8 words at a time
1162 * and are stored on chip for use for the next 8 scanlines.
1163 * This reduces the number of times for access to draw the
1164 * cursor for each screen refresh.
1165 * Each pattern is a bitmap of 64 bit wide and 64 bit high
1166 * (total of 8192 bits or 1024 Kbytes). The two patterns are
1167 * stored in such a way that pattern 0 always resides in the
1168 * lower half (least significant 64 bits) of a 128 bit word
1169 * and pattern 1 the upper half. If you examine the data of
1170 * the cursor image the graphics card uses then from the
1171 * begining you see line one of pattern 0, line one of
1172 * pattern 1, line two of pattern 0, line two of pattern 1,
1173 * etc etc. The linear stride for the cursor is always 16 bytes
1174 * (128 bits) which is the maximum cursor width times two for
1175 * the two monochrome patterns.
1177 u8 *cursorbase = (u8 *)info->cursor.image.data;
1178 char *bitmap = (char *)cursor->image.data;
1179 char *mask = (char *)cursor->mask;
1182 for (i = 0; i < 64; i++) {
1183 if (i < cursor->image.height) {
1184 j = (cursor->image.width + 7) >> 3;
1187 for (; j > 0; j--) {
1188 /* Pattern 0. Copy the cursor bitmap to it */
1189 fb_writeb(*bitmap, cursorbase + h);
1191 /* Pattern 1. Copy the cursor mask to it */
1192 fb_writeb(*mask, cursorbase + h + 8);
1196 for (; k > 0; k--) {
1197 fb_writeb(0, cursorbase + h);
1198 fb_writeb(~0, cursorbase + h + 8);
1202 fb_writel(0, cursorbase + h);
1203 fb_writel(0, cursorbase + h + 4);
1204 fb_writel(~0, cursorbase + h + 8);
1205 fb_writel(~0, cursorbase + h + 12);
1210 /* Turn the cursor on */
1212 info->cursor = *cursor;
1213 mod_timer(&par->hwcursor.timer, jiffies + HZ / 2);
1214 spin_lock_irqsave(&par->DAClock, flags);
1215 banshee_make_room(par, 1);
1216 tdfx_outl(par, VIDPROCCFG, par->hwcursor.enable);
1217 spin_unlock_irqrestore(&par->DAClock, flags);
1222 static struct fb_ops tdfxfb_ops = {
1223 .owner = THIS_MODULE,
1224 .fb_check_var = tdfxfb_check_var,
1225 .fb_set_par = tdfxfb_set_par,
1226 .fb_setcolreg = tdfxfb_setcolreg,
1227 .fb_blank = tdfxfb_blank,
1228 .fb_pan_display = tdfxfb_pan_display,
1229 .fb_sync = banshee_wait_idle,
1230 #ifdef CONFIG_FB_3DFX_ACCEL
1231 .fb_fillrect = tdfxfb_fillrect,
1232 .fb_copyarea = tdfxfb_copyarea,
1233 .fb_imageblit = tdfxfb_imageblit,
1235 .fb_fillrect = cfb_fillrect,
1236 .fb_copyarea = cfb_copyarea,
1237 .fb_imageblit = cfb_imageblit,
1242 * tdfxfb_probe - Device Initializiation
1244 * @pdev: PCI Device to initialize
1245 * @id: PCI Device ID
1247 * Initializes and allocates resources for PCI device @pdev.
1250 static int __devinit tdfxfb_probe(struct pci_dev *pdev,
1251 const struct pci_device_id *id)
1253 struct tdfx_par *default_par;
1254 struct fb_info *info;
1257 if ((err = pci_enable_device(pdev))) {
1258 printk(KERN_WARNING "tdfxfb: Can't enable pdev: %d\n", err);
1262 info = framebuffer_alloc(sizeof(struct tdfx_par), &pdev->dev);
1267 default_par = info->par;
1269 /* Configure the default fb_fix_screeninfo first */
1270 switch (pdev->device) {
1271 case PCI_DEVICE_ID_3DFX_BANSHEE:
1272 strcat(tdfx_fix.id, " Banshee");
1273 default_par->max_pixclock = BANSHEE_MAX_PIXCLOCK;
1275 case PCI_DEVICE_ID_3DFX_VOODOO3:
1276 strcat(tdfx_fix.id, " Voodoo3");
1277 default_par->max_pixclock = VOODOO3_MAX_PIXCLOCK;
1279 case PCI_DEVICE_ID_3DFX_VOODOO5:
1280 strcat(tdfx_fix.id, " Voodoo5");
1281 default_par->max_pixclock = VOODOO5_MAX_PIXCLOCK;
1285 tdfx_fix.mmio_start = pci_resource_start(pdev, 0);
1286 tdfx_fix.mmio_len = pci_resource_len(pdev, 0);
1287 if (!request_mem_region(tdfx_fix.mmio_start, tdfx_fix.mmio_len,
1289 printk(KERN_WARNING "tdfxfb: Can't reserve regbase\n");
1293 default_par->regbase_virt =
1294 ioremap_nocache(tdfx_fix.mmio_start, tdfx_fix.mmio_len);
1295 if (!default_par->regbase_virt) {
1296 printk("fb: Can't remap %s register area.\n", tdfx_fix.id);
1297 goto out_err_regbase;
1300 tdfx_fix.smem_start = pci_resource_start(pdev, 1);
1301 if (!(tdfx_fix.smem_len = do_lfb_size(default_par, pdev->device))) {
1302 printk("fb: Can't count %s memory.\n", tdfx_fix.id);
1303 goto out_err_regbase;
1306 if (!request_mem_region(tdfx_fix.smem_start,
1307 pci_resource_len(pdev, 1), "tdfx smem")) {
1308 printk(KERN_WARNING "tdfxfb: Can't reserve smem\n");
1309 goto out_err_regbase;
1312 info->screen_base = ioremap_nocache(tdfx_fix.smem_start,
1314 if (!info->screen_base) {
1315 printk("fb: Can't remap %s framebuffer.\n", tdfx_fix.id);
1316 goto out_err_screenbase;
1319 default_par->iobase = pci_resource_start(pdev, 2);
1321 if (!request_region(pci_resource_start(pdev, 2),
1322 pci_resource_len(pdev, 2), "tdfx iobase")) {
1323 printk(KERN_WARNING "tdfxfb: Can't reserve iobase\n");
1324 goto out_err_screenbase;
1327 printk("fb: %s memory = %dK\n", tdfx_fix.id, tdfx_fix.smem_len >> 10);
1329 tdfx_fix.ypanstep = nopan ? 0 : 1;
1330 tdfx_fix.ywrapstep = nowrap ? 0 : 1;
1332 info->fbops = &tdfxfb_ops;
1333 info->fix = tdfx_fix;
1334 info->pseudo_palette = default_par->palette;
1335 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
1336 #ifdef CONFIG_FB_3DFX_ACCEL
1337 info->flags |= FBINFO_HWACCEL_FILLRECT |
1338 FBINFO_HWACCEL_COPYAREA |
1339 FBINFO_HWACCEL_IMAGEBLIT |
1344 mode_option = "640x480@60";
1346 err = fb_find_mode(&info->var, info, mode_option, NULL, 0, NULL, 8);
1347 if (!err || err == 4)
1348 info->var = tdfx_var;
1350 /* maximize virtual vertical length */
1351 lpitch = info->var.xres_virtual * ((info->var.bits_per_pixel + 7) >> 3);
1352 info->var.yres_virtual = info->fix.smem_len / lpitch;
1353 if (info->var.yres_virtual < info->var.yres)
1354 goto out_err_iobase;
1356 if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
1357 printk(KERN_WARNING "tdfxfb: Can't allocate color map\n");
1358 goto out_err_iobase;
1361 if (register_framebuffer(info) < 0) {
1362 printk("tdfxfb: can't register framebuffer\n");
1363 fb_dealloc_cmap(&info->cmap);
1364 goto out_err_iobase;
1369 pci_set_drvdata(pdev, info);
1373 release_mem_region(pci_resource_start(pdev, 2),
1374 pci_resource_len(pdev, 2));
1376 if (info->screen_base)
1377 iounmap(info->screen_base);
1378 release_mem_region(tdfx_fix.smem_start, pci_resource_len(pdev, 1));
1381 * Cleanup after anything that was remapped/allocated.
1383 if (default_par->regbase_virt)
1384 iounmap(default_par->regbase_virt);
1385 release_mem_region(tdfx_fix.mmio_start, tdfx_fix.mmio_len);
1387 framebuffer_release(info);
1392 static void tdfxfb_setup(char *options)
1396 if (!options || !*options)
1399 while ((this_opt = strsep(&options, ",")) != NULL) {
1402 if (!strcmp(this_opt, "nopan")) {
1404 } else if (!strcmp(this_opt, "nowrap")) {
1407 mode_option = this_opt;
1414 * tdfxfb_remove - Device removal
1416 * @pdev: PCI Device to cleanup
1418 * Releases all resources allocated during the course of the driver's
1419 * lifetime for the PCI device @pdev.
1422 static void __devexit tdfxfb_remove(struct pci_dev *pdev)
1424 struct fb_info *info = pci_get_drvdata(pdev);
1425 struct tdfx_par *par = info->par;
1427 unregister_framebuffer(info);
1428 iounmap(par->regbase_virt);
1429 iounmap(info->screen_base);
1431 /* Clean up after reserved regions */
1432 release_region(pci_resource_start(pdev, 2),
1433 pci_resource_len(pdev, 2));
1434 release_mem_region(pci_resource_start(pdev, 1),
1435 pci_resource_len(pdev, 1));
1436 release_mem_region(pci_resource_start(pdev, 0),
1437 pci_resource_len(pdev, 0));
1438 pci_set_drvdata(pdev, NULL);
1439 framebuffer_release(info);
1442 static int __init tdfxfb_init(void)
1445 char *option = NULL;
1447 if (fb_get_options("tdfxfb", &option))
1450 tdfxfb_setup(option);
1452 return pci_register_driver(&tdfxfb_driver);
1455 static void __exit tdfxfb_exit(void)
1457 pci_unregister_driver(&tdfxfb_driver);
1460 MODULE_AUTHOR("Hannu Mallat <hmallat@cc.hut.fi>");
1461 MODULE_DESCRIPTION("3Dfx framebuffer device driver");
1462 MODULE_LICENSE("GPL");
1464 module_init(tdfxfb_init);
1465 module_exit(tdfxfb_exit);