blob: faac9b469ea9ae402f4777231c1db96a5cefecbc [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:
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * - multihead support (basically need to support an array of fb_infos)
39 * - support other architectures (PPC, Alpha); does the fact that the VGA
40 * core can be accessed only thru I/O (not memory mapped) complicate
41 * things?
42 *
43 * Version history:
44 *
45 * 0.1.4 (released 2002-05-28) ported over to new fbdev api by James Simmons
46 *
47 * 0.1.3 (released 1999-11-02) added Attila's panning support, code
48 * reorg, hwcursor address page size alignment
49 * (for mmaping both frame buffer and regs),
50 * and my changes to get rid of hardcoded
51 * VGA i/o register locations (uses PCI
52 * configuration info now)
53 * 0.1.2 (released 1999-10-19) added Attila Kesmarki's bug fixes and
54 * improvements
55 * 0.1.1 (released 1999-10-07) added Voodoo3 support by Harold Oga.
56 * 0.1.0 (released 1999-10-06) initial version
57 *
58 */
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/module.h>
61#include <linux/kernel.h>
62#include <linux/errno.h>
63#include <linux/string.h>
64#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/fb.h>
67#include <linux/init.h>
68#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71#include <video/tdfx.h>
72
Krzysztof Helt8af1d502007-10-16 01:28:43 -070073#undef TDFXFB_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#ifdef TDFXFB_DEBUG
75#define DPRINTK(a,b...) printk(KERN_DEBUG "fb: %s: " a, __FUNCTION__ , ## b)
76#else
77#define DPRINTK(a,b...)
Krzysztof Helt8af1d502007-10-16 01:28:43 -070078#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Krzysztof Helt0960bd32007-10-16 01:28:49 -070080#ifdef CONFIG_MTRR
81#include <asm/mtrr.h>
82#else
83/* duplicate asm/mtrr.h defines to work on archs without mtrr */
84#define MTRR_TYPE_WRCOMB 1
85
86static inline int mtrr_add(unsigned long base, unsigned long size,
87 unsigned int type, char increment)
88{
89 return -ENODEV;
90}
91static inline int mtrr_del(int reg, unsigned long base,
92 unsigned long size)
93{
94 return -ENODEV;
95}
96#endif
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#define BANSHEE_MAX_PIXCLOCK 270000
99#define VOODOO3_MAX_PIXCLOCK 300000
100#define VOODOO5_MAX_PIXCLOCK 350000
101
102static struct fb_fix_screeninfo tdfx_fix __devinitdata = {
103 .id = "3Dfx",
104 .type = FB_TYPE_PACKED_PIXELS,
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700105 .visual = FB_VISUAL_PSEUDOCOLOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 .ypanstep = 1,
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700107 .ywrapstep = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 .accel = FB_ACCEL_3DFX_BANSHEE
109};
110
111static struct fb_var_screeninfo tdfx_var __devinitdata = {
112 /* "640x480, 8 bpp @ 60 Hz */
113 .xres = 640,
114 .yres = 480,
115 .xres_virtual = 640,
116 .yres_virtual = 1024,
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700117 .bits_per_pixel = 8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 .red = {0, 8, 0},
119 .blue = {0, 8, 0},
120 .green = {0, 8, 0},
121 .activate = FB_ACTIVATE_NOW,
122 .height = -1,
123 .width = -1,
124 .accel_flags = FB_ACCELF_TEXT,
125 .pixclock = 39722,
126 .left_margin = 40,
127 .right_margin = 24,
128 .upper_margin = 32,
129 .lower_margin = 11,
130 .hsync_len = 96,
131 .vsync_len = 2,
132 .vmode = FB_VMODE_NONINTERLACED
133};
134
135/*
136 * PCI driver prototypes
137 */
138static int __devinit tdfxfb_probe(struct pci_dev *pdev,
139 const struct pci_device_id *id);
140static void __devexit tdfxfb_remove(struct pci_dev *pdev);
141
142static struct pci_device_id tdfxfb_id_table[] = {
143 { PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_BANSHEE,
144 PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
145 0xff0000, 0 },
146 { PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_VOODOO3,
147 PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
148 0xff0000, 0 },
149 { PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_VOODOO5,
150 PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
151 0xff0000, 0 },
152 { 0, }
153};
154
155static struct pci_driver tdfxfb_driver = {
156 .name = "tdfxfb",
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700157 .id_table = tdfxfb_id_table,
158 .probe = tdfxfb_probe,
159 .remove = __devexit_p(tdfxfb_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160};
161
162MODULE_DEVICE_TABLE(pci, tdfxfb_id_table);
163
164/*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700165 * Driver data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 */
Krzysztof Helt90b0f082007-10-16 01:28:48 -0700167static int nopan;
168static int nowrap = 1; /* not implemented (yet) */
169static int hwcursor = 1;
Krzysztof Helt0960bd32007-10-16 01:28:49 -0700170static char *mode_option __devinitdata;
171/* mtrr option */
172static int nomtrr __devinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700174/* -------------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 * Hardware-specific funcions
176 * ------------------------------------------------------------------------- */
177
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700178static inline u8 vga_inb(struct tdfx_par *par, u32 reg)
179{
180 return inb(par->iobase + reg - 0x300);
181}
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700182
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700183static inline void vga_outb(struct tdfx_par *par, u32 reg, u8 val)
184{
185 outb(val, par->iobase + reg - 0x300);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700188static inline void gra_outb(struct tdfx_par *par, u32 idx, u8 val)
189{
190 vga_outb(par, GRA_I, idx);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700191 wmb();
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700192 vga_outb(par, GRA_D, val);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700193 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700196static inline void seq_outb(struct tdfx_par *par, u32 idx, u8 val)
197{
198 vga_outb(par, SEQ_I, idx);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700199 wmb();
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700200 vga_outb(par, SEQ_D, val);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700201 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700204static inline u8 seq_inb(struct tdfx_par *par, u32 idx)
205{
206 vga_outb(par, SEQ_I, idx);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700207 mb();
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700208 return vga_inb(par, SEQ_D);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700211static inline void crt_outb(struct tdfx_par *par, u32 idx, u8 val)
212{
213 vga_outb(par, CRT_I, idx);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700214 wmb();
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700215 vga_outb(par, CRT_D, val);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700216 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700219static inline u8 crt_inb(struct tdfx_par *par, u32 idx)
220{
221 vga_outb(par, CRT_I, idx);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700222 mb();
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700223 return vga_inb(par, CRT_D);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700226static inline void att_outb(struct tdfx_par *par, u32 idx, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 unsigned char tmp;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 tmp = vga_inb(par, IS1_R);
231 vga_outb(par, ATT_IW, idx);
232 vga_outb(par, ATT_IW, val);
233}
234
235static inline void vga_disable_video(struct tdfx_par *par)
236{
237 unsigned char s;
238
239 s = seq_inb(par, 0x01) | 0x20;
240 seq_outb(par, 0x00, 0x01);
241 seq_outb(par, 0x01, s);
242 seq_outb(par, 0x00, 0x03);
243}
244
245static inline void vga_enable_video(struct tdfx_par *par)
246{
247 unsigned char s;
248
249 s = seq_inb(par, 0x01) & 0xdf;
250 seq_outb(par, 0x00, 0x01);
251 seq_outb(par, 0x01, s);
252 seq_outb(par, 0x00, 0x03);
253}
254
255static inline void vga_enable_palette(struct tdfx_par *par)
256{
257 vga_inb(par, IS1_R);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700258 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 vga_outb(par, ATT_IW, 0x20);
260}
261
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700262static inline u32 tdfx_inl(struct tdfx_par *par, unsigned int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 return readl(par->regbase_virt + reg);
265}
266
267static inline void tdfx_outl(struct tdfx_par *par, unsigned int reg, u32 val)
268{
269 writel(val, par->regbase_virt + reg);
270}
271
272static inline void banshee_make_room(struct tdfx_par *par, int size)
273{
274 /* Note: The Voodoo3's onboard FIFO has 32 slots. This loop
275 * won't quit if you ask for more. */
Krzysztof Heltf67fd7c2007-10-16 01:29:26 -0700276 while ((tdfx_inl(par, STATUS) & 0x1f) < size - 1)
277 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280static int banshee_wait_idle(struct fb_info *info)
281{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800282 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 int i = 0;
284
285 banshee_make_room(par, 1);
286 tdfx_outl(par, COMMAND_3D, COMMAND_3D_NOP);
287
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700288 do {
289 if ((tdfx_inl(par, STATUS) & STATUS_BUSY) == 0)
290 i++;
291 } while (i < 3);
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return 0;
294}
295
296/*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700297 * Set the color of a palette entry in 8bpp mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 */
299static inline void do_setpalentry(struct tdfx_par *par, unsigned regno, u32 c)
300{
301 banshee_make_room(par, 2);
302 tdfx_outl(par, DACADDR, regno);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700303 /* read after write makes it working */
304 tdfx_inl(par, DACADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 tdfx_outl(par, DACDATA, c);
306}
307
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700308static u32 do_calc_pll(int freq, int *freq_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700310 int m, n, k, best_m, best_n, best_k, best_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 int fref = 14318;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 best_error = freq;
314 best_n = best_m = best_k = 0;
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700315
316 for (k = 3; k >= 0; k--) {
317 for (m = 63; m >= 0; m--) {
318 /*
319 * Estimate value of n that produces target frequency
320 * with current m and k
321 */
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700322 int n_estimated = ((freq * (m + 2) << k) / fref) - 2;
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700323
324 /* Search neighborhood of estimated n */
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700325 for (n = max(0, n_estimated);
326 n <= min(255, n_estimated + 1);
327 n++) {
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700328 /*
329 * Calculate PLL freqency with current m, k and
330 * estimated n
331 */
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700332 int f = (fref * (n + 2) / (m + 2)) >> k;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700333 int error = abs(f - freq);
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700334
335 /*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700336 * If this is the closest we've come to the
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700337 * target frequency then remember n, m and k
338 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700339 if (error < best_error) {
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700340 best_error = error;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700341 best_n = n;
342 best_m = m;
343 best_k = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345 }
346 }
347 }
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 n = best_n;
350 m = best_m;
351 k = best_k;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700352 *freq_out = (fref * (n + 2) / (m + 2)) >> k;
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return (n << 8) | (m << 2) | k;
355}
356
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700357static void do_write_regs(struct fb_info *info, struct banshee_reg *reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800359 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 int i;
361
362 banshee_wait_idle(info);
363
364 tdfx_outl(par, MISCINIT1, tdfx_inl(par, MISCINIT1) | 0x01);
365
366 crt_outb(par, 0x11, crt_inb(par, 0x11) & 0x7f); /* CRT unprotect */
367
368 banshee_make_room(par, 3);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700369 tdfx_outl(par, VGAINIT1, reg->vgainit1 & 0x001FFFFF);
370 tdfx_outl(par, VIDPROCCFG, reg->vidcfg & ~0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371#if 0
372 tdfx_outl(par, PLLCTRL1, reg->mempll);
373 tdfx_outl(par, PLLCTRL2, reg->gfxpll);
374#endif
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700375 tdfx_outl(par, PLLCTRL0, reg->vidpll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 vga_outb(par, MISC_W, reg->misc[0x00] | 0x01);
378
379 for (i = 0; i < 5; i++)
380 seq_outb(par, i, reg->seq[i]);
381
382 for (i = 0; i < 25; i++)
383 crt_outb(par, i, reg->crt[i]);
384
385 for (i = 0; i < 9; i++)
386 gra_outb(par, i, reg->gra[i]);
387
388 for (i = 0; i < 21; i++)
389 att_outb(par, i, reg->att[i]);
390
391 crt_outb(par, 0x1a, reg->ext[0]);
392 crt_outb(par, 0x1b, reg->ext[1]);
393
394 vga_enable_palette(par);
395 vga_enable_video(par);
396
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700397 banshee_make_room(par, 9);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700398 tdfx_outl(par, VGAINIT0, reg->vgainit0);
399 tdfx_outl(par, DACMODE, reg->dacmode);
400 tdfx_outl(par, VIDDESKSTRIDE, reg->stride);
Krzysztof Helt90b0f082007-10-16 01:28:48 -0700401 tdfx_outl(par, HWCURPATADDR, reg->curspataddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700403 tdfx_outl(par, VIDSCREENSIZE, reg->screensize);
404 tdfx_outl(par, VIDDESKSTART, reg->startaddr);
405 tdfx_outl(par, VIDPROCCFG, reg->vidcfg);
406 tdfx_outl(par, VGAINIT1, reg->vgainit1);
407 tdfx_outl(par, MISCINIT0, reg->miscinit0);
408
409 banshee_make_room(par, 8);
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700410 tdfx_outl(par, SRCBASE, reg->startaddr);
411 tdfx_outl(par, DSTBASE, reg->startaddr);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700412 tdfx_outl(par, COMMANDEXTRA_2D, 0);
413 tdfx_outl(par, CLIP0MIN, 0);
414 tdfx_outl(par, CLIP0MAX, 0x0fff0fff);
415 tdfx_outl(par, CLIP1MIN, 0);
416 tdfx_outl(par, CLIP1MAX, 0x0fff0fff);
417 tdfx_outl(par, SRCXY, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 banshee_wait_idle(info);
420}
421
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700422static unsigned long do_lfb_size(struct tdfx_par *par, unsigned short dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700424 u32 draminit0 = tdfx_inl(par, DRAMINIT0);
425 u32 draminit1 = tdfx_inl(par, DRAMINIT1);
Richard Drummond333f9812005-05-01 08:59:25 -0700426 u32 miscinit1;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700427 int num_chips = (draminit0 & DRAMINIT0_SGRAM_NUM) ? 8 : 4;
Richard Drummond333f9812005-05-01 08:59:25 -0700428 int chip_size; /* in MB */
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700429 int has_sgram = draminit1 & DRAMINIT1_MEM_SDRAM;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700430
Richard Drummond333f9812005-05-01 08:59:25 -0700431 if (dev_id < PCI_DEVICE_ID_3DFX_VOODOO5) {
432 /* Banshee/Voodoo3 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700433 chip_size = 2;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700434 if (has_sgram && (draminit0 & DRAMINIT0_SGRAM_TYPE))
435 chip_size = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 } else {
437 /* Voodoo4/5 */
Richard Drummond333f9812005-05-01 08:59:25 -0700438 has_sgram = 0;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700439 chip_size = draminit0 & DRAMINIT0_SGRAM_TYPE_MASK;
440 chip_size = 1 << (chip_size >> DRAMINIT0_SGRAM_TYPE_SHIFT);
Richard Drummond333f9812005-05-01 08:59:25 -0700441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Richard Drummond333f9812005-05-01 08:59:25 -0700443 /* disable block writes for SDRAM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 miscinit1 = tdfx_inl(par, MISCINIT1);
Richard Drummond333f9812005-05-01 08:59:25 -0700445 miscinit1 |= has_sgram ? 0 : MISCINIT1_2DBLOCK_DIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 miscinit1 |= MISCINIT1_CLUT_INV;
447
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700448 banshee_make_room(par, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 tdfx_outl(par, MISCINIT1, miscinit1);
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700450 return num_chips * chip_size * 1024l * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
453/* ------------------------------------------------------------------------- */
454
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700455static int tdfxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800457 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 u32 lpitch;
459
460 if (var->bits_per_pixel != 8 && var->bits_per_pixel != 16 &&
461 var->bits_per_pixel != 24 && var->bits_per_pixel != 32) {
462 DPRINTK("depth not supported: %u\n", var->bits_per_pixel);
463 return -EINVAL;
464 }
465
466 if (var->xres != var->xres_virtual)
467 var->xres_virtual = var->xres;
468
469 if (var->yres > var->yres_virtual)
470 var->yres_virtual = var->yres;
471
472 if (var->xoffset) {
473 DPRINTK("xoffset not supported\n");
474 return -EINVAL;
475 }
Krzysztof Helt90b0f082007-10-16 01:28:48 -0700476 var->yoffset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 /* Banshee doesn't support interlace, but Voodoo4/5 and probably Voodoo3 do. */
479 /* no direct information about device id now? use max_pixclock for this... */
480 if (((var->vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) &&
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700481 (par->max_pixclock < VOODOO3_MAX_PIXCLOCK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 DPRINTK("interlace not supported\n");
483 return -EINVAL;
484 }
485
486 var->xres = (var->xres + 15) & ~15; /* could sometimes be 8 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700487 lpitch = var->xres * ((var->bits_per_pixel + 7) >> 3);
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (var->xres < 320 || var->xres > 2048) {
490 DPRINTK("width not supported: %u\n", var->xres);
491 return -EINVAL;
492 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (var->yres < 200 || var->yres > 2048) {
495 DPRINTK("height not supported: %u\n", var->yres);
496 return -EINVAL;
497 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (lpitch * var->yres_virtual > info->fix.smem_len) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700500 var->yres_virtual = info->fix.smem_len / lpitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (var->yres_virtual < var->yres) {
502 DPRINTK("no memory for screen (%ux%ux%u)\n",
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700503 var->xres, var->yres_virtual,
504 var->bits_per_pixel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return -EINVAL;
506 }
507 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (PICOS2KHZ(var->pixclock) > par->max_pixclock) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700510 DPRINTK("pixclock too high (%ldKHz)\n",
511 PICOS2KHZ(var->pixclock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return -EINVAL;
513 }
514
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700515 var->transp.offset = 0;
516 var->transp.length = 0;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700517 switch (var->bits_per_pixel) {
518 case 8:
519 var->red.length = var->green.length = var->blue.length = 8;
520 break;
521 case 16:
522 var->red.offset = 11;
523 var->red.length = 5;
524 var->green.offset = 5;
525 var->green.length = 6;
526 var->blue.offset = 0;
527 var->blue.length = 5;
528 break;
529 case 32:
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700530 var->transp.offset = 24;
531 var->transp.length = 8;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700532 case 24:
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700533 var->red.offset = 16;
534 var->green.offset = 8;
535 var->blue.offset = 0;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700536 var->red.length = var->green.length = var->blue.length = 8;
537 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539 var->height = var->width = -1;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 var->accel_flags = FB_ACCELF_TEXT;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700542
543 DPRINTK("Checking graphics mode at %dx%d depth %d\n",
544 var->xres, var->yres, var->bits_per_pixel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return 0;
546}
547
548static int tdfxfb_set_par(struct fb_info *info)
549{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800550 struct tdfx_par *par = info->par;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700551 u32 hdispend = info->var.xres;
552 u32 hsyncsta = hdispend + info->var.right_margin;
553 u32 hsyncend = hsyncsta + info->var.hsync_len;
554 u32 htotal = hsyncend + info->var.left_margin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 u32 hd, hs, he, ht, hbs, hbe;
556 u32 vd, vs, ve, vt, vbs, vbe;
557 struct banshee_reg reg;
558 int fout, freq;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700559 u32 wd;
560 u32 cpp = (info->var.bits_per_pixel + 7) >> 3;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 memset(&reg, 0, sizeof(reg));
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700563
564 reg.vidcfg = VIDCFG_VIDPROC_ENABLE | VIDCFG_DESK_ENABLE |
565 VIDCFG_CURS_X11 |
566 ((cpp - 1) << VIDCFG_PIXFMT_SHIFT) |
567 (cpp != 1 ? VIDCFG_CLUT_BYPASS : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 /* PLL settings */
570 freq = PICOS2KHZ(info->var.pixclock);
571
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700572 reg.vidcfg &= ~VIDCFG_2X;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700574 if (freq > par->max_pixclock / 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 freq = freq > par->max_pixclock ? par->max_pixclock : freq;
576 reg.dacmode |= DACMODE_2X;
577 reg.vidcfg |= VIDCFG_2X;
578 hdispend >>= 1;
579 hsyncsta >>= 1;
580 hsyncend >>= 1;
581 htotal >>= 1;
582 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 hd = wd = (hdispend >> 3) - 1;
585 hs = (hsyncsta >> 3) - 1;
586 he = (hsyncend >> 3) - 1;
587 ht = (htotal >> 3) - 1;
588 hbs = hd;
589 hbe = ht;
590
591 if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE) {
592 vbs = vd = (info->var.yres << 1) - 1;
593 vs = vd + (info->var.lower_margin << 1);
594 ve = vs + (info->var.vsync_len << 1);
595 vbe = vt = ve + (info->var.upper_margin << 1) - 1;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700596 reg.screensize = info->var.xres | (info->var.yres << 13);
597 reg.vidcfg |= VIDCFG_HALF_MODE;
598 reg.crt[0x09] = 0x80;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 } else {
600 vbs = vd = info->var.yres - 1;
601 vs = vd + info->var.lower_margin;
602 ve = vs + info->var.vsync_len;
603 vbe = vt = ve + info->var.upper_margin - 1;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700604 reg.screensize = info->var.xres | (info->var.yres << 12);
605 reg.vidcfg &= ~VIDCFG_HALF_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /* this is all pretty standard VGA register stuffing */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700609 reg.misc[0x00] = 0x0f |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 (info->var.xres < 400 ? 0xa0 :
611 info->var.xres < 480 ? 0x60 :
612 info->var.xres < 768 ? 0xe0 : 0x20);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 reg.gra[0x05] = 0x40;
615 reg.gra[0x06] = 0x05;
616 reg.gra[0x07] = 0x0f;
617 reg.gra[0x08] = 0xff;
618
619 reg.att[0x00] = 0x00;
620 reg.att[0x01] = 0x01;
621 reg.att[0x02] = 0x02;
622 reg.att[0x03] = 0x03;
623 reg.att[0x04] = 0x04;
624 reg.att[0x05] = 0x05;
625 reg.att[0x06] = 0x06;
626 reg.att[0x07] = 0x07;
627 reg.att[0x08] = 0x08;
628 reg.att[0x09] = 0x09;
629 reg.att[0x0a] = 0x0a;
630 reg.att[0x0b] = 0x0b;
631 reg.att[0x0c] = 0x0c;
632 reg.att[0x0d] = 0x0d;
633 reg.att[0x0e] = 0x0e;
634 reg.att[0x0f] = 0x0f;
635 reg.att[0x10] = 0x41;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 reg.att[0x12] = 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 reg.seq[0x00] = 0x03;
639 reg.seq[0x01] = 0x01; /* fixme: clkdiv2? */
640 reg.seq[0x02] = 0x0f;
641 reg.seq[0x03] = 0x00;
642 reg.seq[0x04] = 0x0e;
643
644 reg.crt[0x00] = ht - 4;
645 reg.crt[0x01] = hd;
646 reg.crt[0x02] = hbs;
647 reg.crt[0x03] = 0x80 | (hbe & 0x1f);
648 reg.crt[0x04] = hs;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700649 reg.crt[0x05] = ((hbe & 0x20) << 2) | (he & 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 reg.crt[0x06] = vt;
651 reg.crt[0x07] = ((vs & 0x200) >> 2) |
652 ((vd & 0x200) >> 3) |
653 ((vt & 0x200) >> 4) | 0x10 |
654 ((vbs & 0x100) >> 5) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700655 ((vs & 0x100) >> 6) |
656 ((vd & 0x100) >> 7) |
657 ((vt & 0x100) >> 8);
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700658 reg.crt[0x09] |= 0x40 | ((vbs & 0x200) >> 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 reg.crt[0x10] = vs;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700660 reg.crt[0x11] = (ve & 0x0f) | 0x20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 reg.crt[0x12] = vd;
662 reg.crt[0x13] = wd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 reg.crt[0x15] = vbs;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700664 reg.crt[0x16] = vbe + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 reg.crt[0x17] = 0xc3;
666 reg.crt[0x18] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700668 /* Banshee's nonvga stuff */
669 reg.ext[0x00] = (((ht & 0x100) >> 8) |
670 ((hd & 0x100) >> 6) |
671 ((hbs & 0x100) >> 4) |
672 ((hbe & 0x40) >> 1) |
673 ((hs & 0x100) >> 2) |
674 ((he & 0x20) << 2));
675 reg.ext[0x01] = (((vt & 0x400) >> 10) |
676 ((vd & 0x400) >> 8) |
677 ((vbs & 0x400) >> 6) |
678 ((vbe & 0x400) >> 4));
679
680 reg.vgainit0 = VGAINIT0_8BIT_DAC |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 VGAINIT0_EXT_ENABLE |
682 VGAINIT0_WAKEUP_3C3 |
683 VGAINIT0_ALT_READBACK |
684 VGAINIT0_EXTSHIFTOUT;
685 reg.vgainit1 = tdfx_inl(par, VGAINIT1) & 0x1fffff;
686
Krzysztof Helt90b0f082007-10-16 01:28:48 -0700687 if (hwcursor)
688 reg.curspataddr = info->fix.smem_len;
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 reg.cursloc = 0;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700691
692 reg.cursc0 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 reg.cursc1 = 0xffffff;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 reg.stride = info->var.xres * cpp;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700696 reg.startaddr = info->var.yoffset * reg.stride
697 + info->var.xoffset * cpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 reg.vidpll = do_calc_pll(freq, &fout);
700#if 0
701 reg.mempll = do_calc_pll(..., &fout);
702 reg.gfxpll = do_calc_pll(..., &fout);
703#endif
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED)
706 reg.vidcfg |= VIDCFG_INTERLACE;
707 reg.miscinit0 = tdfx_inl(par, MISCINIT0);
708
709#if defined(__BIG_ENDIAN)
710 switch (info->var.bits_per_pixel) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700711 case 8:
712 case 24:
713 reg.miscinit0 &= ~(1 << 30);
714 reg.miscinit0 &= ~(1 << 31);
715 break;
716 case 16:
717 reg.miscinit0 |= (1 << 30);
718 reg.miscinit0 |= (1 << 31);
719 break;
720 case 32:
721 reg.miscinit0 |= (1 << 30);
722 reg.miscinit0 &= ~(1 << 31);
723 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700725#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 do_write_regs(info, &reg);
727
728 /* Now change fb_fix_screeninfo according to changes in par */
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700729 info->fix.line_length = reg.stride;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700730 info->fix.visual = (info->var.bits_per_pixel == 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 ? FB_VISUAL_PSEUDOCOLOR
732 : FB_VISUAL_TRUECOLOR;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700733 DPRINTK("Graphics mode is now set at %dx%d depth %d\n",
734 info->var.xres, info->var.yres, info->var.bits_per_pixel);
735 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
738/* A handy macro shamelessly pinched from matroxfb */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700739#define CNVT_TOHW(val, width) ((((val)<<(width))+0x7FFF-(val))>>16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700741static int tdfxfb_setcolreg(unsigned regno, unsigned red, unsigned green,
742 unsigned blue, unsigned transp,
743 struct fb_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800745 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 u32 rgbcol;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700747
748 if (regno >= info->cmap.len || regno > 255)
749 return 1;
750
Krzysztof Helt254c9472007-10-16 01:28:46 -0700751 /* grayscale works only partially under directcolor */
752 if (info->var.grayscale) {
753 /* grayscale = 0.30*R + 0.59*G + 0.11*B */
754 red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
755 }
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 switch (info->fix.visual) {
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800758 case FB_VISUAL_PSEUDOCOLOR:
759 rgbcol =(((u32)red & 0xff00) << 8) |
760 (((u32)green & 0xff00) << 0) |
761 (((u32)blue & 0xff00) >> 8);
762 do_setpalentry(par, regno, rgbcol);
763 break;
764 /* Truecolor has no hardware color palettes. */
765 case FB_VISUAL_TRUECOLOR:
766 if (regno < 16) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700767 rgbcol = (CNVT_TOHW(red, info->var.red.length) <<
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800768 info->var.red.offset) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700769 (CNVT_TOHW(green, info->var.green.length) <<
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800770 info->var.green.offset) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700771 (CNVT_TOHW(blue, info->var.blue.length) <<
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800772 info->var.blue.offset) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700773 (CNVT_TOHW(transp, info->var.transp.length) <<
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800774 info->var.transp.offset);
775 par->palette[regno] = rgbcol;
776 }
777
778 break;
779 default:
780 DPRINTK("bad depth %u\n", info->var.bits_per_pixel);
781 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return 0;
785}
786
787/* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */
788static int tdfxfb_blank(int blank, struct fb_info *info)
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700789{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800790 struct tdfx_par *par = info->par;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700791 int vgablank = 1;
792 u32 dacmode = tdfx_inl(par, DACMODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700794 dacmode &= ~(BIT(1) | BIT(3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 switch (blank) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700797 case FB_BLANK_UNBLANK: /* Screen: On; HSync: On, VSync: On */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700798 vgablank = 0;
799 break;
800 case FB_BLANK_NORMAL: /* Screen: Off; HSync: On, VSync: On */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700801 break;
802 case FB_BLANK_VSYNC_SUSPEND: /* Screen: Off; HSync: On, VSync: Off */
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700803 dacmode |= BIT(3);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700804 break;
805 case FB_BLANK_HSYNC_SUSPEND: /* Screen: Off; HSync: Off, VSync: On */
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700806 dacmode |= BIT(1);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700807 break;
808 case FB_BLANK_POWERDOWN: /* Screen: Off; HSync: Off, VSync: Off */
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700809 dacmode |= BIT(1) | BIT(3);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700810 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700813 banshee_make_room(par, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 tdfx_outl(par, DACMODE, dacmode);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700815 if (vgablank)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 vga_disable_video(par);
817 else
818 vga_enable_video(par);
819 return 0;
820}
821
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700822/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 * Set the starting position of the visible screen to var->yoffset
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700824 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825static int tdfxfb_pan_display(struct fb_var_screeninfo *var,
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700826 struct fb_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800828 struct tdfx_par *par = info->par;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700829 u32 addr = var->yoffset * info->fix.line_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 if (nopan || var->xoffset || (var->yoffset > var->yres_virtual))
832 return -EINVAL;
833 if ((var->yoffset + var->yres > var->yres_virtual && nowrap))
834 return -EINVAL;
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 banshee_make_room(par, 1);
837 tdfx_outl(par, VIDDESKSTART, addr);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 info->var.xoffset = var->xoffset;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700840 info->var.yoffset = var->yoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return 0;
842}
843
844#ifdef CONFIG_FB_3DFX_ACCEL
845/*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700846 * FillRect 2D command (solidfill or invert (via ROP_XOR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700848static void tdfxfb_fillrect(struct fb_info *info,
849 const struct fb_fillrect *rect)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800851 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 u32 bpp = info->var.bits_per_pixel;
853 u32 stride = info->fix.line_length;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700854 u32 fmt= stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 int tdfx_rop;
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700856 u32 dx = rect->dx;
857 u32 dy = rect->dy;
858 u32 dstbase = 0;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700859
860 if (rect->rop == ROP_COPY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 tdfx_rop = TDFX_ROP_COPY;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700862 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 tdfx_rop = TDFX_ROP_XOR;
864
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700865 /* asume always rect->height < 4096 */
866 if (dy + rect->height > 4095) {
867 dstbase = stride * dy;
868 dy = 0;
869 }
870 /* asume always rect->width < 4096 */
871 if (dx + rect->width > 4095) {
872 dstbase += dx * bpp >> 3;
873 dx = 0;
874 }
875 banshee_make_room(par, 6);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700876 tdfx_outl(par, DSTFORMAT, fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700878 tdfx_outl(par, COLORFORE, rect->color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 } else { /* FB_VISUAL_TRUECOLOR */
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800880 tdfx_outl(par, COLORFORE, par->palette[rect->color]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700882 tdfx_outl(par, COMMAND_2D, COMMAND_2D_FILLRECT | (tdfx_rop << 24));
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700883 tdfx_outl(par, DSTBASE, dstbase);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700884 tdfx_outl(par, DSTSIZE, rect->width | (rect->height << 16));
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700885 tdfx_outl(par, LAUNCH_2D, dx | (dy << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
888/*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700889 * Screen-to-Screen BitBlt 2D command (for the bmove fb op.)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700891static void tdfxfb_copyarea(struct fb_info *info,
892 const struct fb_copyarea *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800894 struct tdfx_par *par = info->par;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700895 u32 sx = area->sx, sy = area->sy, dx = area->dx, dy = area->dy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 u32 bpp = info->var.bits_per_pixel;
897 u32 stride = info->fix.line_length;
898 u32 blitcmd = COMMAND_2D_S2S_BITBLT | (TDFX_ROP_COPY << 24);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700899 u32 fmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700900 u32 dstbase = 0;
901 u32 srcbase = 0;
902
903 /* asume always area->height < 4096 */
904 if (sy + area->height > 4095) {
905 srcbase = stride * sy;
906 sy = 0;
907 }
908 /* asume always area->width < 4096 */
909 if (sx + area->width > 4095) {
910 srcbase += sx * bpp >> 3;
911 sx = 0;
912 }
913 /* asume always area->height < 4096 */
914 if (dy + area->height > 4095) {
915 dstbase = stride * dy;
916 dy = 0;
917 }
918 /* asume always area->width < 4096 */
919 if (dx + area->width > 4095) {
920 dstbase += dx * bpp >> 3;
921 dx = 0;
922 }
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (area->sx <= area->dx) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700925 //-X
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 blitcmd |= BIT(14);
927 sx += area->width - 1;
928 dx += area->width - 1;
929 }
930 if (area->sy <= area->dy) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700931 //-Y
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 blitcmd |= BIT(15);
933 sy += area->height - 1;
934 dy += area->height - 1;
935 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700936
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700937 banshee_make_room(par, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700939 tdfx_outl(par, SRCFORMAT, fmt);
940 tdfx_outl(par, DSTFORMAT, fmt);
941 tdfx_outl(par, COMMAND_2D, blitcmd);
942 tdfx_outl(par, DSTSIZE, area->width | (area->height << 16));
943 tdfx_outl(par, DSTXY, dx | (dy << 16));
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700944 tdfx_outl(par, SRCBASE, srcbase);
945 tdfx_outl(par, DSTBASE, dstbase);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700946 tdfx_outl(par, LAUNCH_2D, sx | (sy << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700949static void tdfxfb_imageblit(struct fb_info *info, const struct fb_image *image)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800951 struct tdfx_par *par = info->par;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700952 int size = image->height * ((image->width * image->depth + 7) >> 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 int fifo_free;
954 int i, stride = info->fix.line_length;
955 u32 bpp = info->var.bits_per_pixel;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700956 u32 dstfmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 u8 *chardata = (u8 *) image->data;
958 u32 srcfmt;
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700959 u32 dx = image->dx;
960 u32 dy = image->dy;
961 u32 dstbase = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 if (image->depth != 1) {
964 //banshee_make_room(par, 6 + ((size + 3) >> 2));
965 //srcfmt = stride | ((bpp+((bpp==8) ? 0 : 8)) << 13) | 0x400000;
966 cfb_imageblit(info, image);
967 return;
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700968 }
969 banshee_make_room(par, 9);
970 switch (info->fix.visual) {
971 case FB_VISUAL_PSEUDOCOLOR:
972 tdfx_outl(par, COLORFORE, image->fg_color);
973 tdfx_outl(par, COLORBACK, image->bg_color);
974 break;
975 case FB_VISUAL_TRUECOLOR:
976 default:
977 tdfx_outl(par, COLORFORE,
978 par->palette[image->fg_color]);
979 tdfx_outl(par, COLORBACK,
980 par->palette[image->bg_color]);
981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982#ifdef __BIG_ENDIAN
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700983 srcfmt = 0x400000 | BIT(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984#else
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700985 srcfmt = 0x400000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986#endif
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700987 /* asume always image->height < 4096 */
988 if (dy + image->height > 4095) {
989 dstbase = stride * dy;
990 dy = 0;
991 }
992 /* asume always image->width < 4096 */
993 if (dx + image->width > 4095) {
994 dstbase += dx * bpp >> 3;
995 dx = 0;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700998 tdfx_outl(par, DSTBASE, dstbase);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700999 tdfx_outl(par, SRCXY, 0);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001000 tdfx_outl(par, DSTXY, dx | (dy << 16));
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001001 tdfx_outl(par, COMMAND_2D, COMMAND_2D_H2S_BITBLT | (TDFX_ROP_COPY << 24));
1002 tdfx_outl(par, SRCFORMAT, srcfmt);
1003 tdfx_outl(par, DSTFORMAT, dstfmt);
1004 tdfx_outl(par, DSTSIZE, image->width | (image->height << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 /* A count of how many free FIFO entries we've requested.
1007 * When this goes negative, we need to request more. */
1008 fifo_free = 0;
1009
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001010 /* Send four bytes at a time of data */
1011 for (i = (size >> 2); i > 0; i--) {
1012 if (--fifo_free < 0) {
1013 fifo_free = 31;
1014 banshee_make_room(par, fifo_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001016 tdfx_outl(par, LAUNCH_2D, *(u32*)chardata);
1017 chardata += 4;
1018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001020 /* Send the leftovers now */
1021 banshee_make_room(par, 3);
Krzysztof Helt4f05b532007-10-16 01:28:48 -07001022 switch (size % 4) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001023 case 0:
1024 break;
1025 case 1:
1026 tdfx_outl(par, LAUNCH_2D, *chardata);
1027 break;
1028 case 2:
1029 tdfx_outl(par, LAUNCH_2D, *(u16*)chardata);
1030 break;
1031 case 3:
1032 tdfx_outl(par, LAUNCH_2D,
1033 *(u16*)chardata | ((chardata[3]) << 24));
1034 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036}
1037#endif /* CONFIG_FB_3DFX_ACCEL */
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039static int tdfxfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
1040{
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001041 struct tdfx_par *par = info->par;
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001042 u32 vidcfg;
1043
1044 if (!hwcursor)
1045 return -EINVAL; /* just to force soft_cursor() call */
1046
1047 /* Too large of a cursor or wrong bpp :-( */
1048 if (cursor->image.width > 64 ||
1049 cursor->image.height > 64 ||
1050 cursor->image.depth > 1)
1051 return -EINVAL;
1052
1053 vidcfg = tdfx_inl(par, VIDPROCCFG);
1054 if (cursor->enable)
1055 tdfx_outl(par, VIDPROCCFG, vidcfg | VIDCFG_HWCURSOR_ENABLE);
1056 else
1057 tdfx_outl(par, VIDPROCCFG, vidcfg & ~VIDCFG_HWCURSOR_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 /*
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001060 * If the cursor is not be changed this means either we want the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 * current cursor state (if enable is set) or we want to query what
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001062 * we can do with the cursor (if enable is not set)
1063 */
1064 if (!cursor->set)
1065 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 /* fix cursor color - XFree86 forgets to restore it properly */
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001068 if (cursor->set & FB_CUR_SETCMAP) {
1069 struct fb_cmap cmap = info->cmap;
1070 u32 bg_idx = cursor->image.bg_color;
1071 u32 fg_idx = cursor->image.fg_color;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 unsigned long bg_color, fg_color;
1073
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001074 fg_color = (((u32)cmap.red[fg_idx] & 0xff00) << 8) |
1075 (((u32)cmap.green[fg_idx] & 0xff00) << 0) |
1076 (((u32)cmap.blue[fg_idx] & 0xff00) >> 8);
1077 bg_color = (((u32)cmap.red[bg_idx] & 0xff00) << 8) |
1078 (((u32)cmap.green[bg_idx] & 0xff00) << 0) |
1079 (((u32)cmap.blue[bg_idx] & 0xff00) >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 banshee_make_room(par, 2);
1081 tdfx_outl(par, HWCURC0, bg_color);
1082 tdfx_outl(par, HWCURC1, fg_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
1084
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001085 if (cursor->set & FB_CUR_SETPOS) {
1086 int x = cursor->image.dx;
1087 int y = cursor->image.dy - info->var.yoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 x += 63;
1090 y += 63;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 banshee_make_room(par, 1);
1092 tdfx_outl(par, HWCURLOC, (y << 16) + x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001094 if (cursor->set & (FB_CUR_SETIMAGE | FB_CUR_SETSHAPE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 /*
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001096 * Voodoo 3 and above cards use 2 monochrome cursor patterns.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 * The reason is so the card can fetch 8 words at a time
1098 * and are stored on chip for use for the next 8 scanlines.
1099 * This reduces the number of times for access to draw the
1100 * cursor for each screen refresh.
1101 * Each pattern is a bitmap of 64 bit wide and 64 bit high
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001102 * (total of 8192 bits or 1024 bytes). The two patterns are
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 * stored in such a way that pattern 0 always resides in the
1104 * lower half (least significant 64 bits) of a 128 bit word
1105 * and pattern 1 the upper half. If you examine the data of
1106 * the cursor image the graphics card uses then from the
1107 * begining you see line one of pattern 0, line one of
1108 * pattern 1, line two of pattern 0, line two of pattern 1,
1109 * etc etc. The linear stride for the cursor is always 16 bytes
1110 * (128 bits) which is the maximum cursor width times two for
1111 * the two monochrome patterns.
1112 */
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001113 u8 __iomem *cursorbase = info->screen_base + info->fix.smem_len;
1114 u8 *bitmap = (u8 *)cursor->image.data;
1115 u8 *mask = (u8 *)cursor->mask;
1116 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001118 fb_memset(cursorbase, 0, 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001120 for (i = 0; i < cursor->image.height; i++) {
1121 int h = 0;
1122 int j = (cursor->image.width + 7) >> 3;
1123
1124 for (; j > 0; j--) {
1125 u8 data = *mask ^ *bitmap;
1126 if (cursor->rop == ROP_COPY)
1127 data = *mask & *bitmap;
1128 /* Pattern 0. Copy the cursor mask to it */
1129 fb_writeb(*mask, cursorbase + h);
1130 mask++;
1131 /* Pattern 1. Copy the cursor bitmap to it */
1132 fb_writeb(data, cursorbase + h + 8);
1133 bitmap++;
1134 h++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001136 cursorbase += 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return 0;
1140}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001142static struct fb_ops tdfxfb_ops = {
1143 .owner = THIS_MODULE,
1144 .fb_check_var = tdfxfb_check_var,
1145 .fb_set_par = tdfxfb_set_par,
1146 .fb_setcolreg = tdfxfb_setcolreg,
1147 .fb_blank = tdfxfb_blank,
1148 .fb_pan_display = tdfxfb_pan_display,
1149 .fb_sync = banshee_wait_idle,
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001150 .fb_cursor = tdfxfb_cursor,
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001151#ifdef CONFIG_FB_3DFX_ACCEL
1152 .fb_fillrect = tdfxfb_fillrect,
1153 .fb_copyarea = tdfxfb_copyarea,
1154 .fb_imageblit = tdfxfb_imageblit,
1155#else
1156 .fb_fillrect = cfb_fillrect,
1157 .fb_copyarea = cfb_copyarea,
1158 .fb_imageblit = cfb_imageblit,
1159#endif
1160};
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162/**
1163 * tdfxfb_probe - Device Initializiation
1164 *
1165 * @pdev: PCI Device to initialize
1166 * @id: PCI Device ID
1167 *
1168 * Initializes and allocates resources for PCI device @pdev.
1169 *
1170 */
1171static int __devinit tdfxfb_probe(struct pci_dev *pdev,
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001172 const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
1174 struct tdfx_par *default_par;
1175 struct fb_info *info;
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001176 int err, lpitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 if ((err = pci_enable_device(pdev))) {
1179 printk(KERN_WARNING "tdfxfb: Can't enable pdev: %d\n", err);
1180 return err;
1181 }
1182
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001183 info = framebuffer_alloc(sizeof(struct tdfx_par), &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001185 if (!info)
1186 return -ENOMEM;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 default_par = info->par;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 /* Configure the default fb_fix_screeninfo first */
1191 switch (pdev->device) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001192 case PCI_DEVICE_ID_3DFX_BANSHEE:
1193 strcat(tdfx_fix.id, " Banshee");
1194 default_par->max_pixclock = BANSHEE_MAX_PIXCLOCK;
1195 break;
1196 case PCI_DEVICE_ID_3DFX_VOODOO3:
1197 strcat(tdfx_fix.id, " Voodoo3");
1198 default_par->max_pixclock = VOODOO3_MAX_PIXCLOCK;
1199 break;
1200 case PCI_DEVICE_ID_3DFX_VOODOO5:
1201 strcat(tdfx_fix.id, " Voodoo5");
1202 default_par->max_pixclock = VOODOO5_MAX_PIXCLOCK;
1203 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
1205
1206 tdfx_fix.mmio_start = pci_resource_start(pdev, 0);
1207 tdfx_fix.mmio_len = pci_resource_len(pdev, 0);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001208 if (!request_mem_region(tdfx_fix.mmio_start, tdfx_fix.mmio_len,
1209 "tdfx regbase")) {
1210 printk(KERN_WARNING "tdfxfb: Can't reserve regbase\n");
1211 goto out_err;
1212 }
1213
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);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001218 goto out_err_regbase;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 tdfx_fix.smem_start = pci_resource_start(pdev, 1);
1222 if (!(tdfx_fix.smem_len = do_lfb_size(default_par, pdev->device))) {
1223 printk("fb: Can't count %s memory.\n", tdfx_fix.id);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001224 goto out_err_regbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
1226
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001227 if (!request_mem_region(tdfx_fix.smem_start,
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001228 pci_resource_len(pdev, 1), "tdfx smem")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 printk(KERN_WARNING "tdfxfb: Can't reserve smem\n");
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001230 goto out_err_regbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
1232
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001233 info->screen_base = ioremap_nocache(tdfx_fix.smem_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 tdfx_fix.smem_len);
1235 if (!info->screen_base) {
1236 printk("fb: Can't remap %s framebuffer.\n", tdfx_fix.id);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001237 goto out_err_screenbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
1239
1240 default_par->iobase = pci_resource_start(pdev, 2);
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (!request_region(pci_resource_start(pdev, 2),
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001243 pci_resource_len(pdev, 2), "tdfx iobase")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 printk(KERN_WARNING "tdfxfb: Can't reserve iobase\n");
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001245 goto out_err_screenbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247
1248 printk("fb: %s memory = %dK\n", tdfx_fix.id, tdfx_fix.smem_len >> 10);
1249
Krzysztof Helt0960bd32007-10-16 01:28:49 -07001250 default_par->mtrr_handle = -1;
1251 if (!nomtrr)
1252 default_par->mtrr_handle =
1253 mtrr_add(tdfx_fix.smem_start, tdfx_fix.smem_len,
1254 MTRR_TYPE_WRCOMB, 1);
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 tdfx_fix.ypanstep = nopan ? 0 : 1;
1257 tdfx_fix.ywrapstep = nowrap ? 0 : 1;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 info->fbops = &tdfxfb_ops;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001260 info->fix = tdfx_fix;
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001261 info->pseudo_palette = default_par->palette;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
1263#ifdef CONFIG_FB_3DFX_ACCEL
1264 info->flags |= FBINFO_HWACCEL_FILLRECT |
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001265 FBINFO_HWACCEL_COPYAREA |
1266 FBINFO_HWACCEL_IMAGEBLIT |
1267 FBINFO_READS_FAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268#endif
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001269 /* reserve 8192 bits for cursor */
1270 /* the 2.4 driver says PAGE_MASK boundary is not enough for Voodoo4 */
1271 if (hwcursor)
1272 info->fix.smem_len = (info->fix.smem_len - 1024) &
1273 (PAGE_MASK << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 if (!mode_option)
1276 mode_option = "640x480@60";
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001277
1278 err = fb_find_mode(&info->var, info, mode_option, NULL, 0, NULL, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 if (!err || err == 4)
1280 info->var = tdfx_var;
1281
1282 /* maximize virtual vertical length */
1283 lpitch = info->var.xres_virtual * ((info->var.bits_per_pixel + 7) >> 3);
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001284 info->var.yres_virtual = info->fix.smem_len / lpitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 if (info->var.yres_virtual < info->var.yres)
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001286 goto out_err_iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
1289 printk(KERN_WARNING "tdfxfb: Can't allocate color map\n");
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001290 goto out_err_iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
1292
1293 if (register_framebuffer(info) < 0) {
1294 printk("tdfxfb: can't register framebuffer\n");
1295 fb_dealloc_cmap(&info->cmap);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001296 goto out_err_iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298 /*
1299 * Our driver data
1300 */
1301 pci_set_drvdata(pdev, info);
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001302 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001304out_err_iobase:
Krzysztof Helt0960bd32007-10-16 01:28:49 -07001305 if (default_par->mtrr_handle >= 0)
1306 mtrr_del(default_par->mtrr_handle, info->fix.smem_start,
1307 info->fix.smem_len);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001308 release_mem_region(pci_resource_start(pdev, 2),
1309 pci_resource_len(pdev, 2));
1310out_err_screenbase:
1311 if (info->screen_base)
1312 iounmap(info->screen_base);
1313 release_mem_region(tdfx_fix.smem_start, pci_resource_len(pdev, 1));
1314out_err_regbase:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 /*
1316 * Cleanup after anything that was remapped/allocated.
1317 */
1318 if (default_par->regbase_virt)
1319 iounmap(default_par->regbase_virt);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001320 release_mem_region(tdfx_fix.mmio_start, tdfx_fix.mmio_len);
1321out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 framebuffer_release(info);
1323 return -ENXIO;
1324}
1325
1326#ifndef MODULE
Adrian Bunka0aa7d02006-01-09 20:54:04 -08001327static void tdfxfb_setup(char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001329 char *this_opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 if (!options || !*options)
1332 return;
1333
1334 while ((this_opt = strsep(&options, ",")) != NULL) {
1335 if (!*this_opt)
1336 continue;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001337 if (!strcmp(this_opt, "nopan")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 nopan = 1;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001339 } else if (!strcmp(this_opt, "nowrap")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 nowrap = 1;
Krzysztof Helt0960bd32007-10-16 01:28:49 -07001341 } else if (!strncmp(this_opt, "hwcursor=", 9)) {
1342 hwcursor = simple_strtoul(this_opt + 9, NULL, 0);
1343#ifdef CONFIG_MTRR
1344 } else if (!strncmp(this_opt, "nomtrr", 6)) {
1345 nomtrr = 1;
1346#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 } else {
1348 mode_option = this_opt;
1349 }
1350 }
1351}
1352#endif
1353
1354/**
1355 * tdfxfb_remove - Device removal
1356 *
1357 * @pdev: PCI Device to cleanup
1358 *
1359 * Releases all resources allocated during the course of the driver's
1360 * lifetime for the PCI device @pdev.
1361 *
1362 */
1363static void __devexit tdfxfb_remove(struct pci_dev *pdev)
1364{
1365 struct fb_info *info = pci_get_drvdata(pdev);
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001366 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 unregister_framebuffer(info);
Krzysztof Helt0960bd32007-10-16 01:28:49 -07001369 if (par->mtrr_handle >= 0)
1370 mtrr_del(par->mtrr_handle, info->fix.smem_start,
1371 info->fix.smem_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 iounmap(par->regbase_virt);
1373 iounmap(info->screen_base);
1374
1375 /* Clean up after reserved regions */
1376 release_region(pci_resource_start(pdev, 2),
1377 pci_resource_len(pdev, 2));
1378 release_mem_region(pci_resource_start(pdev, 1),
1379 pci_resource_len(pdev, 1));
1380 release_mem_region(pci_resource_start(pdev, 0),
1381 pci_resource_len(pdev, 0));
1382 pci_set_drvdata(pdev, NULL);
1383 framebuffer_release(info);
1384}
1385
1386static int __init tdfxfb_init(void)
1387{
1388#ifndef MODULE
1389 char *option = NULL;
1390
1391 if (fb_get_options("tdfxfb", &option))
1392 return -ENODEV;
1393
1394 tdfxfb_setup(option);
1395#endif
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001396 return pci_register_driver(&tdfxfb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397}
1398
1399static void __exit tdfxfb_exit(void)
1400{
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001401 pci_unregister_driver(&tdfxfb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402}
1403
1404MODULE_AUTHOR("Hannu Mallat <hmallat@cc.hut.fi>");
1405MODULE_DESCRIPTION("3Dfx framebuffer device driver");
1406MODULE_LICENSE("GPL");
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001407
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001408module_param(hwcursor, int, 0644);
1409MODULE_PARM_DESC(hwcursor, "Enable hardware cursor "
1410 "(1=enable, 0=disable, default=1)");
Krzysztof Helt0960bd32007-10-16 01:28:49 -07001411#ifdef CONFIG_MTRR
1412module_param(nomtrr, bool, 0);
1413MODULE_PARM_DESC(nomtrr, "Disable MTRR support (default: enabled)");
1414#endif
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416module_init(tdfxfb_init);
1417module_exit(tdfxfb_exit);