blob: e6aced9df52df670171155a3ce670266a26ef514 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Krzysztof Helt8af1d502007-10-16 01:28:43 -070013 * Lots of the information here comes from the Daryll Strauss' Banshee
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * 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).
Krzysztof Helt8af1d502007-10-16 01:28:43 -070026 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * 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.
Krzysztof Helt8af1d502007-10-16 01:28:43 -070036 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/module.h>
62#include <linux/kernel.h>
63#include <linux/errno.h>
64#include <linux/string.h>
65#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#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
Krzysztof Helt8af1d502007-10-16 01:28:43 -070079#undef TDFXFB_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#ifdef TDFXFB_DEBUG
81#define DPRINTK(a,b...) printk(KERN_DEBUG "fb: %s: " a, __FUNCTION__ , ## b)
82#else
83#define DPRINTK(a,b...)
Krzysztof Helt8af1d502007-10-16 01:28:43 -070084#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86#define BANSHEE_MAX_PIXCLOCK 270000
87#define VOODOO3_MAX_PIXCLOCK 300000
88#define VOODOO5_MAX_PIXCLOCK 350000
89
90static struct fb_fix_screeninfo tdfx_fix __devinitdata = {
91 .id = "3Dfx",
92 .type = FB_TYPE_PACKED_PIXELS,
Krzysztof Helt8af1d502007-10-16 01:28:43 -070093 .visual = FB_VISUAL_PSEUDOCOLOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 .ypanstep = 1,
Krzysztof Helt8af1d502007-10-16 01:28:43 -070095 .ywrapstep = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 .accel = FB_ACCEL_3DFX_BANSHEE
97};
98
99static 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,
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700105 .bits_per_pixel = 8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 .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 */
126static int __devinit tdfxfb_probe(struct pci_dev *pdev,
127 const struct pci_device_id *id);
128static void __devexit tdfxfb_remove(struct pci_dev *pdev);
129
130static 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
143static struct pci_driver tdfxfb_driver = {
144 .name = "tdfxfb",
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700145 .id_table = tdfxfb_id_table,
146 .probe = tdfxfb_probe,
147 .remove = __devexit_p(tdfxfb_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
150MODULE_DEVICE_TABLE(pci, tdfxfb_id_table);
151
152/*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700153 * Driver data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 */
155static int nopan = 0;
156static int nowrap = 1; // not implemented (yet)
157static char *mode_option __devinitdata = NULL;
158
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700159/* -------------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 * Hardware-specific funcions
161 * ------------------------------------------------------------------------- */
162
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700163#ifdef VGA_REG_IO
164static inline u8 vga_inb(struct tdfx_par *par, u32 reg)
165{
166 return inb(reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700168
169static inline void vga_outb(struct tdfx_par *par, u32 reg, u8 val)
170{
171 outb(val, reg);
172}
173#else
174static inline u8 vga_inb(struct tdfx_par *par, u32 reg)
175{
176 return inb(par->iobase + reg - 0x300);
177}
178static inline void vga_outb(struct tdfx_par *par, u32 reg, u8 val)
179{
180 outb(val, par->iobase + reg - 0x300);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182#endif
183
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700184static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700190static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700196static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700202static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700208static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700214static inline void att_outb(struct tdfx_par *par, u32 idx, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 unsigned char tmp;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 tmp = vga_inb(par, IS1_R);
219 vga_outb(par, ATT_IW, idx);
220 vga_outb(par, ATT_IW, val);
221}
222
223static 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
233static 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
243static 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
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700249static inline u32 tdfx_inl(struct tdfx_par *par, unsigned int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 return readl(par->regbase_virt + reg);
252}
253
254static inline void tdfx_outl(struct tdfx_par *par, unsigned int reg, u32 val)
255{
256 writel(val, par->regbase_virt + reg);
257}
258
259static 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. */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700263 while ((tdfx_inl(par, STATUS) & 0x1f) < size - 1) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266static int banshee_wait_idle(struct fb_info *info)
267{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800268 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 int i = 0;
270
271 banshee_make_room(par, 1);
272 tdfx_outl(par, COMMAND_3D, COMMAND_3D_NOP);
273
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700274 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 i = (tdfx_inl(par, STATUS) & STATUS_BUSY) ? 0 : i + 1;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700276 if (i == 3)
277 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279 return 0;
280}
281
282/*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700283 * Set the color of a palette entry in 8bpp mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 */
285static 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
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700292static u32 do_calc_pll(int freq, int *freq_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700294 int m, n, k, best_m, best_n, best_k, best_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 int fref = 14318;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 best_error = freq;
298 best_n = best_m = best_k = 0;
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700299
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);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700316 int error = abs(f - freq);
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700317
318 /*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700319 * If this is the closest we've come to the
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700320 * target frequency then remember n, m and k
321 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700322 if (error < best_error) {
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700323 best_error = error;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700324 best_n = n;
325 best_m = m;
326 best_k = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328 }
329 }
330 }
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 n = best_n;
333 m = best_m;
334 k = best_k;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700335 *freq_out = fref * (n + 2) / (m + 2) / (1 << k);
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return (n << 8) | (m << 2) | k;
338}
339
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700340static void do_write_regs(struct fb_info *info, struct banshee_reg *reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800342 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 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);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700352 tdfx_outl(par, VGAINIT1, reg->vgainit1 & 0x001FFFFF);
353 tdfx_outl(par, VIDPROCCFG, reg->vidcfg & ~0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354#if 0
355 tdfx_outl(par, PLLCTRL1, reg->mempll);
356 tdfx_outl(par, PLLCTRL2, reg->gfxpll);
357#endif
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700358 tdfx_outl(par, PLLCTRL0, reg->vidpll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
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);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700381 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700386 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 banshee_wait_idle(info);
403}
404
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700405static unsigned long do_lfb_size(struct tdfx_par *par, unsigned short dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Richard Drummond333f9812005-05-01 08:59:25 -0700407 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700416 draminit0 = tdfx_inl(par, DRAMINIT0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 draminit1 = tdfx_inl(par, DRAMINIT1);
Richard Drummond333f9812005-05-01 08:59:25 -0700418
419 num_chips = (draminit0 & DRAMINIT0_SGRAM_NUM) ? 8 : 4;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700420
Richard Drummond333f9812005-05-01 08:59:25 -0700421 if (dev_id < PCI_DEVICE_ID_3DFX_VOODOO5) {
422 /* Banshee/Voodoo3 */
423 has_sgram = draminit1 & DRAMINIT1_MEM_SDRAM;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700424 chip_size = 2;
425 if (has_sgram)
426 chip_size = (draminit0 & DRAMINIT0_SGRAM_TYPE) ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 } else {
428 /* Voodoo4/5 */
Richard Drummond333f9812005-05-01 08:59:25 -0700429 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Richard Drummond333f9812005-05-01 08:59:25 -0700434 /* disable block writes for SDRAM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 miscinit1 = tdfx_inl(par, MISCINIT1);
Richard Drummond333f9812005-05-01 08:59:25 -0700436 miscinit1 |= has_sgram ? 0 : MISCINIT1_2DBLOCK_DIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 miscinit1 |= MISCINIT1_CLUT_INV;
438
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700439 banshee_make_room(par, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 tdfx_outl(par, MISCINIT1, miscinit1);
441 return lfbsize;
442}
443
444/* ------------------------------------------------------------------------- */
445
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700446static int tdfxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800448 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 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) &&
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700471 (par->max_pixclock < VOODOO3_MAX_PIXCLOCK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 DPRINTK("interlace not supported\n");
473 return -EINVAL;
474 }
475
476 var->xres = (var->xres + 15) & ~15; /* could sometimes be 8 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700477 lpitch = var->xres * ((var->bits_per_pixel + 7) >> 3);
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (var->xres < 320 || var->xres > 2048) {
480 DPRINTK("width not supported: %u\n", var->xres);
481 return -EINVAL;
482 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (var->yres < 200 || var->yres > 2048) {
485 DPRINTK("height not supported: %u\n", var->yres);
486 return -EINVAL;
487 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (lpitch * var->yres_virtual > info->fix.smem_len) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700490 var->yres_virtual = info->fix.smem_len / lpitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (var->yres_virtual < var->yres) {
492 DPRINTK("no memory for screen (%ux%ux%u)\n",
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700493 var->xres, var->yres_virtual,
494 var->bits_per_pixel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return -EINVAL;
496 }
497 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (PICOS2KHZ(var->pixclock) > par->max_pixclock) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700500 DPRINTK("pixclock too high (%ldKHz)\n",
501 PICOS2KHZ(var->pixclock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return -EINVAL;
503 }
504
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700505 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525 var->height = var->width = -1;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 var->accel_flags = FB_ACCELF_TEXT;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700528
529 DPRINTK("Checking graphics mode at %dx%d depth %d\n",
530 var->xres, var->yres, var->bits_per_pixel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return 0;
532}
533
534static int tdfxfb_set_par(struct fb_info *info)
535{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800536 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 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;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700543
544 par->baseline = 0;
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 memset(&reg, 0, sizeof(reg));
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700547 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 /* PLL settings */
555 freq = PICOS2KHZ(info->var.pixclock);
556
557 reg.dacmode = 0;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700558 reg.vidcfg &= ~VIDCFG_2X;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 hdispend = info->var.xres;
561 hsyncsta = hdispend + info->var.right_margin;
562 hsyncend = hsyncsta + info->var.hsync_len;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700563 htotal = hsyncend + info->var.left_margin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700565 if (freq > par->max_pixclock / 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 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 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 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 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 /* this is all pretty standard VGA register stuffing */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700595 reg.misc[0x00] = 0x0f |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 (info->var.xres < 400 ? 0xa0 :
597 info->var.xres < 480 ? 0x60 :
598 info->var.xres < 768 ? 0xe0 : 0x20);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 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;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700643 reg.crt[0x05] = ((hbe & 0x20) << 2) | (he & 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 reg.crt[0x06] = vt;
645 reg.crt[0x07] = ((vs & 0x200) >> 2) |
646 ((vd & 0x200) >> 3) |
647 ((vt & 0x200) >> 4) | 0x10 |
648 ((vbs & 0x100) >> 5) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700649 ((vs & 0x100) >> 6) |
650 ((vd & 0x100) >> 7) |
651 ((vt & 0x100) >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 reg.crt[0x08] = 0x00;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700653 reg.crt[0x09] = 0x40 | ((vbs & 0x200) >> 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 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;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700661 reg.crt[0x11] = (ve & 0x0f) | 0x20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 reg.crt[0x12] = vd;
663 reg.crt[0x13] = wd;
664 reg.crt[0x14] = 0x00;
665 reg.crt[0x15] = vbs;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700666 reg.crt[0x16] = vbe + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 reg.crt[0x17] = 0xc3;
668 reg.crt[0x18] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700670 /* 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 |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 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;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700690
691 reg.cursc0 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 reg.cursc1 = 0xffffff;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 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;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700704 if (freq > par->max_pixclock / 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 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) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700729 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700743#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 do_write_regs(info, &reg);
745
746 /* Now change fb_fix_screeninfo according to changes in par */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700747 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 ? FB_VISUAL_PSEUDOCOLOR
751 : FB_VISUAL_TRUECOLOR;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700752 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
757/* A handy macro shamelessly pinched from matroxfb */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700758#define CNVT_TOHW(val, width) ((((val)<<(width))+0x7FFF-(val))>>16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700760static int tdfxfb_setcolreg(unsigned regno, unsigned red, unsigned green,
761 unsigned blue, unsigned transp,
762 struct fb_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800764 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 u32 rgbcol;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700766
767 if (regno >= info->cmap.len || regno > 255)
768 return 1;
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 switch (info->fix.visual) {
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800771 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) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700780 rgbcol = (CNVT_TOHW(red, info->var.red.length) <<
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800781 info->var.red.offset) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700782 (CNVT_TOHW(green, info->var.green.length) <<
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800783 info->var.green.offset) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700784 (CNVT_TOHW(blue, info->var.blue.length) <<
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800785 info->var.blue.offset) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700786 (CNVT_TOHW(transp, info->var.transp.length) <<
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800787 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return 0;
798}
799
800/* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */
801static int tdfxfb_blank(int blank, struct fb_info *info)
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700802{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800803 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 u32 dacmode, state = 0, vgablank = 0;
805
806 dacmode = tdfx_inl(par, DACMODE);
807
808 switch (blank) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700809 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830
831 dacmode &= ~(BIT(1) | BIT(3));
832 dacmode |= state;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700833 banshee_make_room(par, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 tdfx_outl(par, DACMODE, dacmode);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700835 if (vgablank)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 vga_disable_video(par);
837 else
838 vga_enable_video(par);
839 return 0;
840}
841
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700842/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 * Set the starting position of the visible screen to var->yoffset
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700844 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845static int tdfxfb_pan_display(struct fb_var_screeninfo *var,
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700846 struct fb_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800848 struct tdfx_par *par = info->par;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700849 u32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
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);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 info->var.xoffset = var->xoffset;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700861 info->var.yoffset = var->yoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return 0;
863}
864
865#ifdef CONFIG_FB_3DFX_ACCEL
866/*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700867 * FillRect 2D command (solidfill or invert (via ROP_XOR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700869static void tdfxfb_fillrect(struct fb_info *info,
870 const struct fb_fillrect *rect)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800872 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 u32 bpp = info->var.bits_per_pixel;
874 u32 stride = info->fix.line_length;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700875 u32 fmt= stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 int tdfx_rop;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700877
878 if (rect->rop == ROP_COPY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 tdfx_rop = TDFX_ROP_COPY;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700880 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 tdfx_rop = TDFX_ROP_XOR;
882
883 banshee_make_room(par, 5);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700884 tdfx_outl(par, DSTFORMAT, fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700886 tdfx_outl(par, COLORFORE, rect->color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 } else { /* FB_VISUAL_TRUECOLOR */
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800888 tdfx_outl(par, COLORFORE, par->palette[rect->color]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700890 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
895/*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700896 * Screen-to-Screen BitBlt 2D command (for the bmove fb op.)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700898static void tdfxfb_copyarea(struct fb_info *info,
899 const struct fb_copyarea *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800901 struct tdfx_par *par = info->par;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700902 u32 sx = area->sx, sy = area->sy, dx = area->dx, dy = area->dy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 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);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700906 u32 fmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (area->sx <= area->dx) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700909 //-X
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 blitcmd |= BIT(14);
911 sx += area->width - 1;
912 dx += area->width - 1;
913 }
914 if (area->sy <= area->dy) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700915 //-Y
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 blitcmd |= BIT(15);
917 sy += area->height - 1;
918 dy += area->height - 1;
919 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 banshee_make_room(par, 6);
922
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700923 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700931static void tdfxfb_imageblit(struct fb_info *info, const struct fb_image *image)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800933 struct tdfx_par *par = info->par;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700934 int size = image->height * ((image->width * image->depth + 7) >> 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 int fifo_free;
936 int i, stride = info->fix.line_length;
937 u32 bpp = info->var.bits_per_pixel;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700938 u32 dstfmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 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) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700950 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]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961#ifdef __BIG_ENDIAN
962 srcfmt = 0x400000 | BIT(20);
963#else
964 srcfmt = 0x400000;
965#endif
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700968 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
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
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700979 /* 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700985 tdfx_outl(par, LAUNCH_2D, *(u32*)chardata);
986 chardata += 4;
987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700989 /* Send the leftovers now */
990 banshee_make_room(par, 3);
991 i = size % 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 switch (i) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700993 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
1006}
1007#endif /* CONFIG_FB_3DFX_ACCEL */
1008
1009#ifdef TDFX_HARDWARE_CURSOR
1010static int tdfxfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
1011{
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001012 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 unsigned long flags;
1014
1015 /*
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001016 * If the cursor is not be changed this means either we want the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 * current cursor state (if enable is set) or we want to query what
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001018 * we can do with the cursor (if enable is not set)
1019 */
1020 if (!cursor->set)
1021 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 /* Too large of a cursor :-( */
1024 if (cursor->image.width > 64 || cursor->image.height > 64)
1025 return -ENXIO;
1026
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001027 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 * If we are going to be changing things we should disable
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001029 * the cursor first
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 */
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) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001050 (cmap.green[cmap.start] << 8) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 (cmap.blue[cmap.start]));
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001052 bg_color = ((cmap.red[cmap.start + 1] << 16) |
1053 (cmap.green[cmap.start + 1] << 8) |
1054 (cmap.blue[cmap.start + 1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 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 /*
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001087 * Voodoo 3 and above cards use 2 monochrome cursor patterns.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 * 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 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001104 u8 *cursorbase = (u8 *)info->cursor.image.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 char *bitmap = (char *)cursor->image.data;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001106 char *mask = (char *)cursor->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 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
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001114 for (; j > 0; j--) {
1115 /* Pattern 0. Copy the cursor bitmap to it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 fb_writeb(*bitmap, cursorbase + h);
1117 bitmap++;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001118 /* Pattern 1. Copy the cursor mask to it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 fb_writeb(*mask, cursorbase + h + 8);
1120 mask++;
1121 h++;
1122 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001123 for (; k > 0; k--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 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;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001140 mod_timer(&par->hwcursor.timer, jiffies + HZ / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 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
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001149static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168/**
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 */
1177static int __devinit tdfxfb_probe(struct pci_dev *pdev,
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001178 const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
1180 struct tdfx_par *default_par;
1181 struct fb_info *info;
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001182 int err, lpitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 if ((err = pci_enable_device(pdev))) {
1185 printk(KERN_WARNING "tdfxfb: Can't enable pdev: %d\n", err);
1186 return err;
1187 }
1188
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001189 info = framebuffer_alloc(sizeof(struct tdfx_par), &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001191 if (!info)
1192 return -ENOMEM;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 default_par = info->par;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 /* Configure the default fb_fix_screeninfo first */
1197 switch (pdev->device) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001198 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
1211
1212 tdfx_fix.mmio_start = pci_resource_start(pdev, 0);
1213 tdfx_fix.mmio_len = pci_resource_len(pdev, 0);
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001214 default_par->regbase_virt =
1215 ioremap_nocache(tdfx_fix.mmio_start, tdfx_fix.mmio_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (!default_par->regbase_virt) {
1217 printk("fb: Can't remap %s register area.\n", tdfx_fix.id);
1218 goto out_err;
1219 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (!request_mem_region(pci_resource_start(pdev, 0),
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001222 pci_resource_len(pdev, 0), "tdfx regbase")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 printk(KERN_WARNING "tdfxfb: Can't reserve regbase\n");
1224 goto out_err;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
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));
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001232 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234
1235 if (!request_mem_region(pci_resource_start(pdev, 1),
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001236 pci_resource_len(pdev, 1), "tdfx smem")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 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
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001243 info->screen_base = ioremap_nocache(tdfx_fix.smem_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 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);
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (!request_region(pci_resource_start(pdev, 2),
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001257 pci_resource_len(pdev, 2), "tdfx iobase")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 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;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 info->fbops = &tdfxfb_ops;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001272 info->fix = tdfx_fix;
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001273 info->pseudo_palette = default_par->palette;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 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";
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001282
1283 err = fb_find_mode(&info->var, info, mode_option, NULL, 0, NULL, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 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);
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001289 info->var.yres_virtual = info->fix.smem_len / lpitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 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);
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001317 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319out_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
Adrian Bunka0aa7d02006-01-09 20:54:04 -08001332static void tdfxfb_setup(char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001334 char *this_opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 if (!options || !*options)
1337 return;
1338
1339 while ((this_opt = strsep(&options, ",")) != NULL) {
1340 if (!*this_opt)
1341 continue;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001342 if (!strcmp(this_opt, "nopan")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 nopan = 1;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001344 } else if (!strcmp(this_opt, "nowrap")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 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 */
1362static void __devexit tdfxfb_remove(struct pci_dev *pdev)
1363{
1364 struct fb_info *info = pci_get_drvdata(pdev);
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001365 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
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
1382static 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
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001392 return pci_register_driver(&tdfxfb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393}
1394
1395static void __exit tdfxfb_exit(void)
1396{
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001397 pci_unregister_driver(&tdfxfb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
1400MODULE_AUTHOR("Hannu Mallat <hmallat@cc.hut.fi>");
1401MODULE_DESCRIPTION("3Dfx framebuffer device driver");
1402MODULE_LICENSE("GPL");
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404module_init(tdfxfb_init);
1405module_exit(tdfxfb_exit);