blob: 2566683cbfeff4587eec050c7ed9b3fff9e51cf6 [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 Helt92744dd2007-10-16 01:28:45 -0700505 var->transp.offset = 0;
506 var->transp.length = 0;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700507 switch (var->bits_per_pixel) {
508 case 8:
509 var->red.length = var->green.length = var->blue.length = 8;
510 break;
511 case 16:
512 var->red.offset = 11;
513 var->red.length = 5;
514 var->green.offset = 5;
515 var->green.length = 6;
516 var->blue.offset = 0;
517 var->blue.length = 5;
518 break;
519 case 32:
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700520 var->transp.offset = 24;
521 var->transp.length = 8;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700522 case 24:
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700523 var->red.offset = 16;
524 var->green.offset = 8;
525 var->blue.offset = 0;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700526 var->red.length = var->green.length = var->blue.length = 8;
527 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529 var->height = var->width = -1;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 var->accel_flags = FB_ACCELF_TEXT;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700532
533 DPRINTK("Checking graphics mode at %dx%d depth %d\n",
534 var->xres, var->yres, var->bits_per_pixel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return 0;
536}
537
538static int tdfxfb_set_par(struct fb_info *info)
539{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800540 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 u32 hdispend, hsyncsta, hsyncend, htotal;
542 u32 hd, hs, he, ht, hbs, hbe;
543 u32 vd, vs, ve, vt, vbs, vbe;
544 struct banshee_reg reg;
545 int fout, freq;
546 u32 wd, cpp;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700547
548 par->baseline = 0;
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 memset(&reg, 0, sizeof(reg));
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700551 cpp = (info->var.bits_per_pixel + 7) / 8;
552
553 reg.vidcfg = VIDCFG_VIDPROC_ENABLE | VIDCFG_DESK_ENABLE |
554 VIDCFG_CURS_X11 |
555 ((cpp - 1) << VIDCFG_PIXFMT_SHIFT) |
556 (cpp != 1 ? VIDCFG_CLUT_BYPASS : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 /* PLL settings */
559 freq = PICOS2KHZ(info->var.pixclock);
560
561 reg.dacmode = 0;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700562 reg.vidcfg &= ~VIDCFG_2X;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 hdispend = info->var.xres;
565 hsyncsta = hdispend + info->var.right_margin;
566 hsyncend = hsyncsta + info->var.hsync_len;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700567 htotal = hsyncend + info->var.left_margin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700569 if (freq > par->max_pixclock / 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 freq = freq > par->max_pixclock ? par->max_pixclock : freq;
571 reg.dacmode |= DACMODE_2X;
572 reg.vidcfg |= VIDCFG_2X;
573 hdispend >>= 1;
574 hsyncsta >>= 1;
575 hsyncend >>= 1;
576 htotal >>= 1;
577 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 hd = wd = (hdispend >> 3) - 1;
580 hs = (hsyncsta >> 3) - 1;
581 he = (hsyncend >> 3) - 1;
582 ht = (htotal >> 3) - 1;
583 hbs = hd;
584 hbe = ht;
585
586 if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE) {
587 vbs = vd = (info->var.yres << 1) - 1;
588 vs = vd + (info->var.lower_margin << 1);
589 ve = vs + (info->var.vsync_len << 1);
590 vbe = vt = ve + (info->var.upper_margin << 1) - 1;
591 } else {
592 vbs = vd = info->var.yres - 1;
593 vs = vd + info->var.lower_margin;
594 ve = vs + info->var.vsync_len;
595 vbe = vt = ve + info->var.upper_margin - 1;
596 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 /* this is all pretty standard VGA register stuffing */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700599 reg.misc[0x00] = 0x0f |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 (info->var.xres < 400 ? 0xa0 :
601 info->var.xres < 480 ? 0x60 :
602 info->var.xres < 768 ? 0xe0 : 0x20);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 reg.gra[0x00] = 0x00;
605 reg.gra[0x01] = 0x00;
606 reg.gra[0x02] = 0x00;
607 reg.gra[0x03] = 0x00;
608 reg.gra[0x04] = 0x00;
609 reg.gra[0x05] = 0x40;
610 reg.gra[0x06] = 0x05;
611 reg.gra[0x07] = 0x0f;
612 reg.gra[0x08] = 0xff;
613
614 reg.att[0x00] = 0x00;
615 reg.att[0x01] = 0x01;
616 reg.att[0x02] = 0x02;
617 reg.att[0x03] = 0x03;
618 reg.att[0x04] = 0x04;
619 reg.att[0x05] = 0x05;
620 reg.att[0x06] = 0x06;
621 reg.att[0x07] = 0x07;
622 reg.att[0x08] = 0x08;
623 reg.att[0x09] = 0x09;
624 reg.att[0x0a] = 0x0a;
625 reg.att[0x0b] = 0x0b;
626 reg.att[0x0c] = 0x0c;
627 reg.att[0x0d] = 0x0d;
628 reg.att[0x0e] = 0x0e;
629 reg.att[0x0f] = 0x0f;
630 reg.att[0x10] = 0x41;
631 reg.att[0x11] = 0x00;
632 reg.att[0x12] = 0x0f;
633 reg.att[0x13] = 0x00;
634 reg.att[0x14] = 0x00;
635
636 reg.seq[0x00] = 0x03;
637 reg.seq[0x01] = 0x01; /* fixme: clkdiv2? */
638 reg.seq[0x02] = 0x0f;
639 reg.seq[0x03] = 0x00;
640 reg.seq[0x04] = 0x0e;
641
642 reg.crt[0x00] = ht - 4;
643 reg.crt[0x01] = hd;
644 reg.crt[0x02] = hbs;
645 reg.crt[0x03] = 0x80 | (hbe & 0x1f);
646 reg.crt[0x04] = hs;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700647 reg.crt[0x05] = ((hbe & 0x20) << 2) | (he & 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 reg.crt[0x06] = vt;
649 reg.crt[0x07] = ((vs & 0x200) >> 2) |
650 ((vd & 0x200) >> 3) |
651 ((vt & 0x200) >> 4) | 0x10 |
652 ((vbs & 0x100) >> 5) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700653 ((vs & 0x100) >> 6) |
654 ((vd & 0x100) >> 7) |
655 ((vt & 0x100) >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 reg.crt[0x08] = 0x00;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700657 reg.crt[0x09] = 0x40 | ((vbs & 0x200) >> 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 reg.crt[0x0a] = 0x00;
659 reg.crt[0x0b] = 0x00;
660 reg.crt[0x0c] = 0x00;
661 reg.crt[0x0d] = 0x00;
662 reg.crt[0x0e] = 0x00;
663 reg.crt[0x0f] = 0x00;
664 reg.crt[0x10] = vs;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700665 reg.crt[0x11] = (ve & 0x0f) | 0x20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 reg.crt[0x12] = vd;
667 reg.crt[0x13] = wd;
668 reg.crt[0x14] = 0x00;
669 reg.crt[0x15] = vbs;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700670 reg.crt[0x16] = vbe + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 reg.crt[0x17] = 0xc3;
672 reg.crt[0x18] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700674 /* Banshee's nonvga stuff */
675 reg.ext[0x00] = (((ht & 0x100) >> 8) |
676 ((hd & 0x100) >> 6) |
677 ((hbs & 0x100) >> 4) |
678 ((hbe & 0x40) >> 1) |
679 ((hs & 0x100) >> 2) |
680 ((he & 0x20) << 2));
681 reg.ext[0x01] = (((vt & 0x400) >> 10) |
682 ((vd & 0x400) >> 8) |
683 ((vbs & 0x400) >> 6) |
684 ((vbe & 0x400) >> 4));
685
686 reg.vgainit0 = VGAINIT0_8BIT_DAC |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 VGAINIT0_EXT_ENABLE |
688 VGAINIT0_WAKEUP_3C3 |
689 VGAINIT0_ALT_READBACK |
690 VGAINIT0_EXTSHIFTOUT;
691 reg.vgainit1 = tdfx_inl(par, VGAINIT1) & 0x1fffff;
692
693 reg.cursloc = 0;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700694
695 reg.cursc0 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 reg.cursc1 = 0xffffff;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 reg.stride = info->var.xres * cpp;
699 reg.startaddr = par->baseline * reg.stride;
700 reg.srcbase = reg.startaddr;
701 reg.dstbase = reg.startaddr;
702
703 /* PLL settings */
704 freq = PICOS2KHZ(info->var.pixclock);
705
706 reg.dacmode &= ~DACMODE_2X;
707 reg.vidcfg &= ~VIDCFG_2X;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700708 if (freq > par->max_pixclock / 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 freq = freq > par->max_pixclock ? par->max_pixclock : freq;
710 reg.dacmode |= DACMODE_2X;
711 reg.vidcfg |= VIDCFG_2X;
712 }
713 reg.vidpll = do_calc_pll(freq, &fout);
714#if 0
715 reg.mempll = do_calc_pll(..., &fout);
716 reg.gfxpll = do_calc_pll(..., &fout);
717#endif
718
719 if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE) {
720 reg.screensize = info->var.xres | (info->var.yres << 13);
721 reg.vidcfg |= VIDCFG_HALF_MODE;
722 reg.crt[0x09] |= 0x80;
723 } else {
724 reg.screensize = info->var.xres | (info->var.yres << 12);
725 reg.vidcfg &= ~VIDCFG_HALF_MODE;
726 }
727 if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED)
728 reg.vidcfg |= VIDCFG_INTERLACE;
729 reg.miscinit0 = tdfx_inl(par, MISCINIT0);
730
731#if defined(__BIG_ENDIAN)
732 switch (info->var.bits_per_pixel) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700733 case 8:
734 case 24:
735 reg.miscinit0 &= ~(1 << 30);
736 reg.miscinit0 &= ~(1 << 31);
737 break;
738 case 16:
739 reg.miscinit0 |= (1 << 30);
740 reg.miscinit0 |= (1 << 31);
741 break;
742 case 32:
743 reg.miscinit0 |= (1 << 30);
744 reg.miscinit0 &= ~(1 << 31);
745 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700747#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 do_write_regs(info, &reg);
749
750 /* Now change fb_fix_screeninfo according to changes in par */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700751 info->fix.line_length =
752 info->var.xres * ((info->var.bits_per_pixel + 7) >> 3);
753 info->fix.visual = (info->var.bits_per_pixel == 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 ? FB_VISUAL_PSEUDOCOLOR
755 : FB_VISUAL_TRUECOLOR;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700756 DPRINTK("Graphics mode is now set at %dx%d depth %d\n",
757 info->var.xres, info->var.yres, info->var.bits_per_pixel);
758 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
761/* A handy macro shamelessly pinched from matroxfb */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700762#define CNVT_TOHW(val, width) ((((val)<<(width))+0x7FFF-(val))>>16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700764static int tdfxfb_setcolreg(unsigned regno, unsigned red, unsigned green,
765 unsigned blue, unsigned transp,
766 struct fb_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800768 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 u32 rgbcol;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700770
771 if (regno >= info->cmap.len || regno > 255)
772 return 1;
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 switch (info->fix.visual) {
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800775 case FB_VISUAL_PSEUDOCOLOR:
776 rgbcol =(((u32)red & 0xff00) << 8) |
777 (((u32)green & 0xff00) << 0) |
778 (((u32)blue & 0xff00) >> 8);
779 do_setpalentry(par, regno, rgbcol);
780 break;
781 /* Truecolor has no hardware color palettes. */
782 case FB_VISUAL_TRUECOLOR:
783 if (regno < 16) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700784 rgbcol = (CNVT_TOHW(red, info->var.red.length) <<
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800785 info->var.red.offset) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700786 (CNVT_TOHW(green, info->var.green.length) <<
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800787 info->var.green.offset) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700788 (CNVT_TOHW(blue, info->var.blue.length) <<
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800789 info->var.blue.offset) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700790 (CNVT_TOHW(transp, info->var.transp.length) <<
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800791 info->var.transp.offset);
792 par->palette[regno] = rgbcol;
793 }
794
795 break;
796 default:
797 DPRINTK("bad depth %u\n", info->var.bits_per_pixel);
798 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return 0;
802}
803
804/* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */
805static int tdfxfb_blank(int blank, struct fb_info *info)
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700806{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800807 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 u32 dacmode, state = 0, vgablank = 0;
809
810 dacmode = tdfx_inl(par, DACMODE);
811
812 switch (blank) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700813 case FB_BLANK_UNBLANK: /* Screen: On; HSync: On, VSync: On */
814 state = 0;
815 vgablank = 0;
816 break;
817 case FB_BLANK_NORMAL: /* Screen: Off; HSync: On, VSync: On */
818 state = 0;
819 vgablank = 1;
820 break;
821 case FB_BLANK_VSYNC_SUSPEND: /* Screen: Off; HSync: On, VSync: Off */
822 state = BIT(3);
823 vgablank = 1;
824 break;
825 case FB_BLANK_HSYNC_SUSPEND: /* Screen: Off; HSync: Off, VSync: On */
826 state = BIT(1);
827 vgablank = 1;
828 break;
829 case FB_BLANK_POWERDOWN: /* Screen: Off; HSync: Off, VSync: Off */
830 state = BIT(1) | BIT(3);
831 vgablank = 1;
832 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834
835 dacmode &= ~(BIT(1) | BIT(3));
836 dacmode |= state;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700837 banshee_make_room(par, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 tdfx_outl(par, DACMODE, dacmode);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700839 if (vgablank)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 vga_disable_video(par);
841 else
842 vga_enable_video(par);
843 return 0;
844}
845
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700846/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 * Set the starting position of the visible screen to var->yoffset
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700848 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849static int tdfxfb_pan_display(struct fb_var_screeninfo *var,
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700850 struct fb_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800852 struct tdfx_par *par = info->par;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700853 u32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 if (nopan || var->xoffset || (var->yoffset > var->yres_virtual))
856 return -EINVAL;
857 if ((var->yoffset + var->yres > var->yres_virtual && nowrap))
858 return -EINVAL;
859
860 addr = var->yoffset * info->fix.line_length;
861 banshee_make_room(par, 1);
862 tdfx_outl(par, VIDDESKSTART, addr);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 info->var.xoffset = var->xoffset;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700865 info->var.yoffset = var->yoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return 0;
867}
868
869#ifdef CONFIG_FB_3DFX_ACCEL
870/*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700871 * FillRect 2D command (solidfill or invert (via ROP_XOR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700873static void tdfxfb_fillrect(struct fb_info *info,
874 const struct fb_fillrect *rect)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800876 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 u32 bpp = info->var.bits_per_pixel;
878 u32 stride = info->fix.line_length;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700879 u32 fmt= stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 int tdfx_rop;
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700881 u32 dx = rect->dx;
882 u32 dy = rect->dy;
883 u32 dstbase = 0;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700884
885 if (rect->rop == ROP_COPY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 tdfx_rop = TDFX_ROP_COPY;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700887 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 tdfx_rop = TDFX_ROP_XOR;
889
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700890 /* asume always rect->height < 4096 */
891 if (dy + rect->height > 4095) {
892 dstbase = stride * dy;
893 dy = 0;
894 }
895 /* asume always rect->width < 4096 */
896 if (dx + rect->width > 4095) {
897 dstbase += dx * bpp >> 3;
898 dx = 0;
899 }
900 banshee_make_room(par, 6);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700901 tdfx_outl(par, DSTFORMAT, fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700903 tdfx_outl(par, COLORFORE, rect->color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 } else { /* FB_VISUAL_TRUECOLOR */
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800905 tdfx_outl(par, COLORFORE, par->palette[rect->color]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700907 tdfx_outl(par, COMMAND_2D, COMMAND_2D_FILLRECT | (tdfx_rop << 24));
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700908 tdfx_outl(par, DSTBASE, dstbase);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700909 tdfx_outl(par, DSTSIZE, rect->width | (rect->height << 16));
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700910 tdfx_outl(par, LAUNCH_2D, dx | (dy << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
913/*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700914 * Screen-to-Screen BitBlt 2D command (for the bmove fb op.)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700916static void tdfxfb_copyarea(struct fb_info *info,
917 const struct fb_copyarea *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800919 struct tdfx_par *par = info->par;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700920 u32 sx = area->sx, sy = area->sy, dx = area->dx, dy = area->dy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 u32 bpp = info->var.bits_per_pixel;
922 u32 stride = info->fix.line_length;
923 u32 blitcmd = COMMAND_2D_S2S_BITBLT | (TDFX_ROP_COPY << 24);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700924 u32 fmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700925 u32 dstbase = 0;
926 u32 srcbase = 0;
927
928 /* asume always area->height < 4096 */
929 if (sy + area->height > 4095) {
930 srcbase = stride * sy;
931 sy = 0;
932 }
933 /* asume always area->width < 4096 */
934 if (sx + area->width > 4095) {
935 srcbase += sx * bpp >> 3;
936 sx = 0;
937 }
938 /* asume always area->height < 4096 */
939 if (dy + area->height > 4095) {
940 dstbase = stride * dy;
941 dy = 0;
942 }
943 /* asume always area->width < 4096 */
944 if (dx + area->width > 4095) {
945 dstbase += dx * bpp >> 3;
946 dx = 0;
947 }
948
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (area->sx <= area->dx) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700951 //-X
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 blitcmd |= BIT(14);
953 sx += area->width - 1;
954 dx += area->width - 1;
955 }
956 if (area->sy <= area->dy) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700957 //-Y
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 blitcmd |= BIT(15);
959 sy += area->height - 1;
960 dy += area->height - 1;
961 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700962
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700963 banshee_make_room(par, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700965 tdfx_outl(par, SRCFORMAT, fmt);
966 tdfx_outl(par, DSTFORMAT, fmt);
967 tdfx_outl(par, COMMAND_2D, blitcmd);
968 tdfx_outl(par, DSTSIZE, area->width | (area->height << 16));
969 tdfx_outl(par, DSTXY, dx | (dy << 16));
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700970 tdfx_outl(par, SRCBASE, srcbase);
971 tdfx_outl(par, DSTBASE, dstbase);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700972 tdfx_outl(par, LAUNCH_2D, sx | (sy << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700975static void tdfxfb_imageblit(struct fb_info *info, const struct fb_image *image)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800977 struct tdfx_par *par = info->par;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700978 int size = image->height * ((image->width * image->depth + 7) >> 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 int fifo_free;
980 int i, stride = info->fix.line_length;
981 u32 bpp = info->var.bits_per_pixel;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700982 u32 dstfmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 u8 *chardata = (u8 *) image->data;
984 u32 srcfmt;
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700985 u32 dx = image->dx;
986 u32 dy = image->dy;
987 u32 dstbase = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 if (image->depth != 1) {
990 //banshee_make_room(par, 6 + ((size + 3) >> 2));
991 //srcfmt = stride | ((bpp+((bpp==8) ? 0 : 8)) << 13) | 0x400000;
992 cfb_imageblit(info, image);
993 return;
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700994 }
995 banshee_make_room(par, 9);
996 switch (info->fix.visual) {
997 case FB_VISUAL_PSEUDOCOLOR:
998 tdfx_outl(par, COLORFORE, image->fg_color);
999 tdfx_outl(par, COLORBACK, image->bg_color);
1000 break;
1001 case FB_VISUAL_TRUECOLOR:
1002 default:
1003 tdfx_outl(par, COLORFORE,
1004 par->palette[image->fg_color]);
1005 tdfx_outl(par, COLORBACK,
1006 par->palette[image->bg_color]);
1007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008#ifdef __BIG_ENDIAN
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001009 srcfmt = 0x400000 | BIT(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010#else
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001011 srcfmt = 0x400000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012#endif
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001013 /* asume always image->height < 4096 */
1014 if (dy + image->height > 4095) {
1015 dstbase = stride * dy;
1016 dy = 0;
1017 }
1018 /* asume always image->width < 4096 */
1019 if (dx + image->width > 4095) {
1020 dstbase += dx * bpp >> 3;
1021 dx = 0;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001024 tdfx_outl(par, DSTBASE, dstbase);
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001025 tdfx_outl(par, SRCXY, 0);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001026 tdfx_outl(par, DSTXY, dx | (dy << 16));
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001027 tdfx_outl(par, COMMAND_2D, COMMAND_2D_H2S_BITBLT | (TDFX_ROP_COPY << 24));
1028 tdfx_outl(par, SRCFORMAT, srcfmt);
1029 tdfx_outl(par, DSTFORMAT, dstfmt);
1030 tdfx_outl(par, DSTSIZE, image->width | (image->height << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 /* A count of how many free FIFO entries we've requested.
1033 * When this goes negative, we need to request more. */
1034 fifo_free = 0;
1035
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001036 /* Send four bytes at a time of data */
1037 for (i = (size >> 2); i > 0; i--) {
1038 if (--fifo_free < 0) {
1039 fifo_free = 31;
1040 banshee_make_room(par, fifo_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001042 tdfx_outl(par, LAUNCH_2D, *(u32*)chardata);
1043 chardata += 4;
1044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001046 /* Send the leftovers now */
1047 banshee_make_room(par, 3);
1048 i = size % 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 switch (i) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001050 case 0:
1051 break;
1052 case 1:
1053 tdfx_outl(par, LAUNCH_2D, *chardata);
1054 break;
1055 case 2:
1056 tdfx_outl(par, LAUNCH_2D, *(u16*)chardata);
1057 break;
1058 case 3:
1059 tdfx_outl(par, LAUNCH_2D,
1060 *(u16*)chardata | ((chardata[3]) << 24));
1061 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063}
1064#endif /* CONFIG_FB_3DFX_ACCEL */
1065
1066#ifdef TDFX_HARDWARE_CURSOR
1067static int tdfxfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
1068{
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001069 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 unsigned long flags;
1071
1072 /*
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001073 * If the cursor is not be changed this means either we want the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 * current cursor state (if enable is set) or we want to query what
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001075 * we can do with the cursor (if enable is not set)
1076 */
1077 if (!cursor->set)
1078 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 /* Too large of a cursor :-( */
1081 if (cursor->image.width > 64 || cursor->image.height > 64)
1082 return -ENXIO;
1083
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001084 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 * If we are going to be changing things we should disable
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001086 * the cursor first
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 */
1088 if (info->cursor.enable) {
1089 spin_lock_irqsave(&par->DAClock, flags);
1090 info->cursor.enable = 0;
1091 del_timer(&(par->hwcursor.timer));
1092 tdfx_outl(par, VIDPROCCFG, par->hwcursor.disable);
1093 spin_unlock_irqrestore(&par->DAClock, flags);
1094 }
1095
1096 /* Disable the Cursor */
1097 if ((cursor->set && FB_CUR_SETCUR) && !cursor->enable)
1098 return 0;
1099
1100 /* fix cursor color - XFree86 forgets to restore it properly */
1101 if (cursor->set && FB_CUR_SETCMAP) {
1102 struct fb_cmap cmap = cursor->image.cmap;
1103 unsigned long bg_color, fg_color;
1104
1105 cmap.len = 2; /* Voodoo 3+ only support 2 color cursors */
1106 fg_color = ((cmap.red[cmap.start] << 16) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001107 (cmap.green[cmap.start] << 8) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 (cmap.blue[cmap.start]));
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001109 bg_color = ((cmap.red[cmap.start + 1] << 16) |
1110 (cmap.green[cmap.start + 1] << 8) |
1111 (cmap.blue[cmap.start + 1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 fb_copy_cmap(&cmap, &info->cursor.image.cmap);
1113 spin_lock_irqsave(&par->DAClock, flags);
1114 banshee_make_room(par, 2);
1115 tdfx_outl(par, HWCURC0, bg_color);
1116 tdfx_outl(par, HWCURC1, fg_color);
1117 spin_unlock_irqrestore(&par->DAClock, flags);
1118 }
1119
1120 if (cursor->set && FB_CUR_SETPOS) {
1121 int x, y;
1122
1123 x = cursor->image.dx;
1124 y = cursor->image.dy;
1125 y -= info->var.yoffset;
1126 info->cursor.image.dx = x;
1127 info->cursor.image.dy = y;
1128 x += 63;
1129 y += 63;
1130 spin_lock_irqsave(&par->DAClock, flags);
1131 banshee_make_room(par, 1);
1132 tdfx_outl(par, HWCURLOC, (y << 16) + x);
1133 spin_unlock_irqrestore(&par->DAClock, flags);
1134 }
1135
1136 /* Not supported so we fake it */
1137 if (cursor->set && FB_CUR_SETHOT) {
1138 info->cursor.hot.x = cursor->hot.x;
1139 info->cursor.hot.y = cursor->hot.y;
1140 }
1141
1142 if (cursor->set && FB_CUR_SETSHAPE) {
1143 /*
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001144 * Voodoo 3 and above cards use 2 monochrome cursor patterns.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 * The reason is so the card can fetch 8 words at a time
1146 * and are stored on chip for use for the next 8 scanlines.
1147 * This reduces the number of times for access to draw the
1148 * cursor for each screen refresh.
1149 * Each pattern is a bitmap of 64 bit wide and 64 bit high
1150 * (total of 8192 bits or 1024 Kbytes). The two patterns are
1151 * stored in such a way that pattern 0 always resides in the
1152 * lower half (least significant 64 bits) of a 128 bit word
1153 * and pattern 1 the upper half. If you examine the data of
1154 * the cursor image the graphics card uses then from the
1155 * begining you see line one of pattern 0, line one of
1156 * pattern 1, line two of pattern 0, line two of pattern 1,
1157 * etc etc. The linear stride for the cursor is always 16 bytes
1158 * (128 bits) which is the maximum cursor width times two for
1159 * the two monochrome patterns.
1160 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001161 u8 *cursorbase = (u8 *)info->cursor.image.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 char *bitmap = (char *)cursor->image.data;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001163 char *mask = (char *)cursor->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 int i, j, k, h = 0;
1165
1166 for (i = 0; i < 64; i++) {
1167 if (i < cursor->image.height) {
1168 j = (cursor->image.width + 7) >> 3;
1169 k = 8 - j;
1170
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001171 for (; j > 0; j--) {
1172 /* Pattern 0. Copy the cursor bitmap to it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 fb_writeb(*bitmap, cursorbase + h);
1174 bitmap++;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001175 /* Pattern 1. Copy the cursor mask to it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 fb_writeb(*mask, cursorbase + h + 8);
1177 mask++;
1178 h++;
1179 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001180 for (; k > 0; k--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 fb_writeb(0, cursorbase + h);
1182 fb_writeb(~0, cursorbase + h + 8);
1183 h++;
1184 }
1185 } else {
1186 fb_writel(0, cursorbase + h);
1187 fb_writel(0, cursorbase + h + 4);
1188 fb_writel(~0, cursorbase + h + 8);
1189 fb_writel(~0, cursorbase + h + 12);
1190 h += 16;
1191 }
1192 }
1193 }
1194 /* Turn the cursor on */
1195 cursor->enable = 1;
1196 info->cursor = *cursor;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001197 mod_timer(&par->hwcursor.timer, jiffies + HZ / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 spin_lock_irqsave(&par->DAClock, flags);
1199 banshee_make_room(par, 1);
1200 tdfx_outl(par, VIDPROCCFG, par->hwcursor.enable);
1201 spin_unlock_irqrestore(&par->DAClock, flags);
1202 return 0;
1203}
1204#endif
1205
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001206static struct fb_ops tdfxfb_ops = {
1207 .owner = THIS_MODULE,
1208 .fb_check_var = tdfxfb_check_var,
1209 .fb_set_par = tdfxfb_set_par,
1210 .fb_setcolreg = tdfxfb_setcolreg,
1211 .fb_blank = tdfxfb_blank,
1212 .fb_pan_display = tdfxfb_pan_display,
1213 .fb_sync = banshee_wait_idle,
1214#ifdef CONFIG_FB_3DFX_ACCEL
1215 .fb_fillrect = tdfxfb_fillrect,
1216 .fb_copyarea = tdfxfb_copyarea,
1217 .fb_imageblit = tdfxfb_imageblit,
1218#else
1219 .fb_fillrect = cfb_fillrect,
1220 .fb_copyarea = cfb_copyarea,
1221 .fb_imageblit = cfb_imageblit,
1222#endif
1223};
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225/**
1226 * tdfxfb_probe - Device Initializiation
1227 *
1228 * @pdev: PCI Device to initialize
1229 * @id: PCI Device ID
1230 *
1231 * Initializes and allocates resources for PCI device @pdev.
1232 *
1233 */
1234static int __devinit tdfxfb_probe(struct pci_dev *pdev,
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001235 const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
1237 struct tdfx_par *default_par;
1238 struct fb_info *info;
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001239 int err, lpitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 if ((err = pci_enable_device(pdev))) {
1242 printk(KERN_WARNING "tdfxfb: Can't enable pdev: %d\n", err);
1243 return err;
1244 }
1245
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001246 info = framebuffer_alloc(sizeof(struct tdfx_par), &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001248 if (!info)
1249 return -ENOMEM;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 default_par = info->par;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 /* Configure the default fb_fix_screeninfo first */
1254 switch (pdev->device) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001255 case PCI_DEVICE_ID_3DFX_BANSHEE:
1256 strcat(tdfx_fix.id, " Banshee");
1257 default_par->max_pixclock = BANSHEE_MAX_PIXCLOCK;
1258 break;
1259 case PCI_DEVICE_ID_3DFX_VOODOO3:
1260 strcat(tdfx_fix.id, " Voodoo3");
1261 default_par->max_pixclock = VOODOO3_MAX_PIXCLOCK;
1262 break;
1263 case PCI_DEVICE_ID_3DFX_VOODOO5:
1264 strcat(tdfx_fix.id, " Voodoo5");
1265 default_par->max_pixclock = VOODOO5_MAX_PIXCLOCK;
1266 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268
1269 tdfx_fix.mmio_start = pci_resource_start(pdev, 0);
1270 tdfx_fix.mmio_len = pci_resource_len(pdev, 0);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001271 if (!request_mem_region(tdfx_fix.mmio_start, tdfx_fix.mmio_len,
1272 "tdfx regbase")) {
1273 printk(KERN_WARNING "tdfxfb: Can't reserve regbase\n");
1274 goto out_err;
1275 }
1276
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001277 default_par->regbase_virt =
1278 ioremap_nocache(tdfx_fix.mmio_start, tdfx_fix.mmio_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 if (!default_par->regbase_virt) {
1280 printk("fb: Can't remap %s register area.\n", tdfx_fix.id);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001281 goto out_err_regbase;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 tdfx_fix.smem_start = pci_resource_start(pdev, 1);
1285 if (!(tdfx_fix.smem_len = do_lfb_size(default_par, pdev->device))) {
1286 printk("fb: Can't count %s memory.\n", tdfx_fix.id);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001287 goto out_err_regbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
1289
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001290 if (!request_mem_region(tdfx_fix.smem_start,
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001291 pci_resource_len(pdev, 1), "tdfx smem")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 printk(KERN_WARNING "tdfxfb: Can't reserve smem\n");
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001293 goto out_err_regbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
1295
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001296 info->screen_base = ioremap_nocache(tdfx_fix.smem_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 tdfx_fix.smem_len);
1298 if (!info->screen_base) {
1299 printk("fb: Can't remap %s framebuffer.\n", tdfx_fix.id);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001300 goto out_err_screenbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
1302
1303 default_par->iobase = pci_resource_start(pdev, 2);
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (!request_region(pci_resource_start(pdev, 2),
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001306 pci_resource_len(pdev, 2), "tdfx iobase")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 printk(KERN_WARNING "tdfxfb: Can't reserve iobase\n");
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001308 goto out_err_screenbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
1310
1311 printk("fb: %s memory = %dK\n", tdfx_fix.id, tdfx_fix.smem_len >> 10);
1312
1313 tdfx_fix.ypanstep = nopan ? 0 : 1;
1314 tdfx_fix.ywrapstep = nowrap ? 0 : 1;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 info->fbops = &tdfxfb_ops;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001317 info->fix = tdfx_fix;
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001318 info->pseudo_palette = default_par->palette;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
1320#ifdef CONFIG_FB_3DFX_ACCEL
1321 info->flags |= FBINFO_HWACCEL_FILLRECT |
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001322 FBINFO_HWACCEL_COPYAREA |
1323 FBINFO_HWACCEL_IMAGEBLIT |
1324 FBINFO_READS_FAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325#endif
1326
1327 if (!mode_option)
1328 mode_option = "640x480@60";
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001329
1330 err = fb_find_mode(&info->var, info, mode_option, NULL, 0, NULL, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (!err || err == 4)
1332 info->var = tdfx_var;
1333
1334 /* maximize virtual vertical length */
1335 lpitch = info->var.xres_virtual * ((info->var.bits_per_pixel + 7) >> 3);
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001336 info->var.yres_virtual = info->fix.smem_len / lpitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (info->var.yres_virtual < info->var.yres)
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001338 goto out_err_iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
1341 printk(KERN_WARNING "tdfxfb: Can't allocate color map\n");
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001342 goto out_err_iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344
1345 if (register_framebuffer(info) < 0) {
1346 printk("tdfxfb: can't register framebuffer\n");
1347 fb_dealloc_cmap(&info->cmap);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001348 goto out_err_iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350 /*
1351 * Our driver data
1352 */
1353 pci_set_drvdata(pdev, info);
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001354 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001356out_err_iobase:
1357 release_mem_region(pci_resource_start(pdev, 2),
1358 pci_resource_len(pdev, 2));
1359out_err_screenbase:
1360 if (info->screen_base)
1361 iounmap(info->screen_base);
1362 release_mem_region(tdfx_fix.smem_start, pci_resource_len(pdev, 1));
1363out_err_regbase:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 /*
1365 * Cleanup after anything that was remapped/allocated.
1366 */
1367 if (default_par->regbase_virt)
1368 iounmap(default_par->regbase_virt);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001369 release_mem_region(tdfx_fix.mmio_start, tdfx_fix.mmio_len);
1370out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 framebuffer_release(info);
1372 return -ENXIO;
1373}
1374
1375#ifndef MODULE
Adrian Bunka0aa7d02006-01-09 20:54:04 -08001376static void tdfxfb_setup(char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001378 char *this_opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 if (!options || !*options)
1381 return;
1382
1383 while ((this_opt = strsep(&options, ",")) != NULL) {
1384 if (!*this_opt)
1385 continue;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001386 if (!strcmp(this_opt, "nopan")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 nopan = 1;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001388 } else if (!strcmp(this_opt, "nowrap")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 nowrap = 1;
1390 } else {
1391 mode_option = this_opt;
1392 }
1393 }
1394}
1395#endif
1396
1397/**
1398 * tdfxfb_remove - Device removal
1399 *
1400 * @pdev: PCI Device to cleanup
1401 *
1402 * Releases all resources allocated during the course of the driver's
1403 * lifetime for the PCI device @pdev.
1404 *
1405 */
1406static void __devexit tdfxfb_remove(struct pci_dev *pdev)
1407{
1408 struct fb_info *info = pci_get_drvdata(pdev);
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001409 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 unregister_framebuffer(info);
1412 iounmap(par->regbase_virt);
1413 iounmap(info->screen_base);
1414
1415 /* Clean up after reserved regions */
1416 release_region(pci_resource_start(pdev, 2),
1417 pci_resource_len(pdev, 2));
1418 release_mem_region(pci_resource_start(pdev, 1),
1419 pci_resource_len(pdev, 1));
1420 release_mem_region(pci_resource_start(pdev, 0),
1421 pci_resource_len(pdev, 0));
1422 pci_set_drvdata(pdev, NULL);
1423 framebuffer_release(info);
1424}
1425
1426static int __init tdfxfb_init(void)
1427{
1428#ifndef MODULE
1429 char *option = NULL;
1430
1431 if (fb_get_options("tdfxfb", &option))
1432 return -ENODEV;
1433
1434 tdfxfb_setup(option);
1435#endif
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001436 return pci_register_driver(&tdfxfb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437}
1438
1439static void __exit tdfxfb_exit(void)
1440{
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001441 pci_unregister_driver(&tdfxfb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
1443
1444MODULE_AUTHOR("Hannu Mallat <hmallat@cc.hut.fi>");
1445MODULE_DESCRIPTION("3Dfx framebuffer device driver");
1446MODULE_LICENSE("GPL");
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448module_init(tdfxfb_init);
1449module_exit(tdfxfb_exit);