blob: c5ba6477dfe2cd484f731739cb349ed89f5758b2 [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#include <linux/spinlock.h>
71
72#include <video/tdfx.h>
73
Krzysztof Helt8af1d502007-10-16 01:28:43 -070074#undef TDFXFB_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#ifdef TDFXFB_DEBUG
76#define DPRINTK(a,b...) printk(KERN_DEBUG "fb: %s: " a, __FUNCTION__ , ## b)
77#else
78#define DPRINTK(a,b...)
Krzysztof Helt8af1d502007-10-16 01:28:43 -070079#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81#define BANSHEE_MAX_PIXCLOCK 270000
82#define VOODOO3_MAX_PIXCLOCK 300000
83#define VOODOO5_MAX_PIXCLOCK 350000
84
85static struct fb_fix_screeninfo tdfx_fix __devinitdata = {
86 .id = "3Dfx",
87 .type = FB_TYPE_PACKED_PIXELS,
Krzysztof Helt8af1d502007-10-16 01:28:43 -070088 .visual = FB_VISUAL_PSEUDOCOLOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 .ypanstep = 1,
Krzysztof Helt8af1d502007-10-16 01:28:43 -070090 .ywrapstep = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 .accel = FB_ACCEL_3DFX_BANSHEE
92};
93
94static struct fb_var_screeninfo tdfx_var __devinitdata = {
95 /* "640x480, 8 bpp @ 60 Hz */
96 .xres = 640,
97 .yres = 480,
98 .xres_virtual = 640,
99 .yres_virtual = 1024,
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700100 .bits_per_pixel = 8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 .red = {0, 8, 0},
102 .blue = {0, 8, 0},
103 .green = {0, 8, 0},
104 .activate = FB_ACTIVATE_NOW,
105 .height = -1,
106 .width = -1,
107 .accel_flags = FB_ACCELF_TEXT,
108 .pixclock = 39722,
109 .left_margin = 40,
110 .right_margin = 24,
111 .upper_margin = 32,
112 .lower_margin = 11,
113 .hsync_len = 96,
114 .vsync_len = 2,
115 .vmode = FB_VMODE_NONINTERLACED
116};
117
118/*
119 * PCI driver prototypes
120 */
121static int __devinit tdfxfb_probe(struct pci_dev *pdev,
122 const struct pci_device_id *id);
123static void __devexit tdfxfb_remove(struct pci_dev *pdev);
124
125static struct pci_device_id tdfxfb_id_table[] = {
126 { PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_BANSHEE,
127 PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
128 0xff0000, 0 },
129 { PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_VOODOO3,
130 PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
131 0xff0000, 0 },
132 { PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_VOODOO5,
133 PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
134 0xff0000, 0 },
135 { 0, }
136};
137
138static struct pci_driver tdfxfb_driver = {
139 .name = "tdfxfb",
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700140 .id_table = tdfxfb_id_table,
141 .probe = tdfxfb_probe,
142 .remove = __devexit_p(tdfxfb_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144
145MODULE_DEVICE_TABLE(pci, tdfxfb_id_table);
146
147/*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700148 * Driver data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 */
Krzysztof Helt90b0f082007-10-16 01:28:48 -0700150static int nopan;
151static int nowrap = 1; /* not implemented (yet) */
152static int hwcursor = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static char *mode_option __devinitdata = NULL;
154
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700155/* -------------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * Hardware-specific funcions
157 * ------------------------------------------------------------------------- */
158
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700159static inline u8 vga_inb(struct tdfx_par *par, u32 reg)
160{
161 return inb(par->iobase + reg - 0x300);
162}
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700163
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700164static inline void vga_outb(struct tdfx_par *par, u32 reg, u8 val)
165{
166 outb(val, par->iobase + reg - 0x300);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700169static inline void gra_outb(struct tdfx_par *par, u32 idx, u8 val)
170{
171 vga_outb(par, GRA_I, idx);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700172 wmb();
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700173 vga_outb(par, GRA_D, val);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700174 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700177static inline void seq_outb(struct tdfx_par *par, u32 idx, u8 val)
178{
179 vga_outb(par, SEQ_I, idx);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700180 wmb();
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700181 vga_outb(par, SEQ_D, val);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700182 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700185static inline u8 seq_inb(struct tdfx_par *par, u32 idx)
186{
187 vga_outb(par, SEQ_I, idx);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700188 mb();
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700189 return vga_inb(par, SEQ_D);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700192static inline void crt_outb(struct tdfx_par *par, u32 idx, u8 val)
193{
194 vga_outb(par, CRT_I, idx);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700195 wmb();
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700196 vga_outb(par, CRT_D, val);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700197 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700200static inline u8 crt_inb(struct tdfx_par *par, u32 idx)
201{
202 vga_outb(par, CRT_I, idx);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700203 mb();
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700204 return vga_inb(par, CRT_D);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700207static inline void att_outb(struct tdfx_par *par, u32 idx, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 unsigned char tmp;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 tmp = vga_inb(par, IS1_R);
212 vga_outb(par, ATT_IW, idx);
213 vga_outb(par, ATT_IW, val);
214}
215
216static inline void vga_disable_video(struct tdfx_par *par)
217{
218 unsigned char s;
219
220 s = seq_inb(par, 0x01) | 0x20;
221 seq_outb(par, 0x00, 0x01);
222 seq_outb(par, 0x01, s);
223 seq_outb(par, 0x00, 0x03);
224}
225
226static inline void vga_enable_video(struct tdfx_par *par)
227{
228 unsigned char s;
229
230 s = seq_inb(par, 0x01) & 0xdf;
231 seq_outb(par, 0x00, 0x01);
232 seq_outb(par, 0x01, s);
233 seq_outb(par, 0x00, 0x03);
234}
235
236static inline void vga_enable_palette(struct tdfx_par *par)
237{
238 vga_inb(par, IS1_R);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700239 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 vga_outb(par, ATT_IW, 0x20);
241}
242
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700243static inline u32 tdfx_inl(struct tdfx_par *par, unsigned int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 return readl(par->regbase_virt + reg);
246}
247
248static inline void tdfx_outl(struct tdfx_par *par, unsigned int reg, u32 val)
249{
250 writel(val, par->regbase_virt + reg);
251}
252
253static inline void banshee_make_room(struct tdfx_par *par, int size)
254{
255 /* Note: The Voodoo3's onboard FIFO has 32 slots. This loop
256 * won't quit if you ask for more. */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700257 while ((tdfx_inl(par, STATUS) & 0x1f) < size - 1) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260static int banshee_wait_idle(struct fb_info *info)
261{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800262 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 int i = 0;
264
265 banshee_make_room(par, 1);
266 tdfx_outl(par, COMMAND_3D, COMMAND_3D_NOP);
267
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700268 do {
269 if ((tdfx_inl(par, STATUS) & STATUS_BUSY) == 0)
270 i++;
271 } while (i < 3);
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return 0;
274}
275
276/*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700277 * Set the color of a palette entry in 8bpp mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 */
279static inline void do_setpalentry(struct tdfx_par *par, unsigned regno, u32 c)
280{
281 banshee_make_room(par, 2);
282 tdfx_outl(par, DACADDR, regno);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700283 /* read after write makes it working */
284 tdfx_inl(par, DACADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 tdfx_outl(par, DACDATA, c);
286}
287
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700288static u32 do_calc_pll(int freq, int *freq_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700290 int m, n, k, best_m, best_n, best_k, best_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 int fref = 14318;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 best_error = freq;
294 best_n = best_m = best_k = 0;
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700295
296 for (k = 3; k >= 0; k--) {
297 for (m = 63; m >= 0; m--) {
298 /*
299 * Estimate value of n that produces target frequency
300 * with current m and k
301 */
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700302 int n_estimated = ((freq * (m + 2) << k) / fref) - 2;
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700303
304 /* Search neighborhood of estimated n */
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700305 for (n = max(0, n_estimated);
306 n <= min(255, n_estimated + 1);
307 n++) {
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700308 /*
309 * Calculate PLL freqency with current m, k and
310 * estimated n
311 */
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700312 int f = (fref * (n + 2) / (m + 2)) >> k;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700313 int error = abs(f - freq);
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700314
315 /*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700316 * If this is the closest we've come to the
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700317 * target frequency then remember n, m and k
318 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700319 if (error < best_error) {
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700320 best_error = error;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700321 best_n = n;
322 best_m = m;
323 best_k = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325 }
326 }
327 }
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 n = best_n;
330 m = best_m;
331 k = best_k;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700332 *freq_out = (fref * (n + 2) / (m + 2)) >> k;
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return (n << 8) | (m << 2) | k;
335}
336
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700337static void do_write_regs(struct fb_info *info, struct banshee_reg *reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800339 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 int i;
341
342 banshee_wait_idle(info);
343
344 tdfx_outl(par, MISCINIT1, tdfx_inl(par, MISCINIT1) | 0x01);
345
346 crt_outb(par, 0x11, crt_inb(par, 0x11) & 0x7f); /* CRT unprotect */
347
348 banshee_make_room(par, 3);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700349 tdfx_outl(par, VGAINIT1, reg->vgainit1 & 0x001FFFFF);
350 tdfx_outl(par, VIDPROCCFG, reg->vidcfg & ~0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351#if 0
352 tdfx_outl(par, PLLCTRL1, reg->mempll);
353 tdfx_outl(par, PLLCTRL2, reg->gfxpll);
354#endif
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700355 tdfx_outl(par, PLLCTRL0, reg->vidpll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 vga_outb(par, MISC_W, reg->misc[0x00] | 0x01);
358
359 for (i = 0; i < 5; i++)
360 seq_outb(par, i, reg->seq[i]);
361
362 for (i = 0; i < 25; i++)
363 crt_outb(par, i, reg->crt[i]);
364
365 for (i = 0; i < 9; i++)
366 gra_outb(par, i, reg->gra[i]);
367
368 for (i = 0; i < 21; i++)
369 att_outb(par, i, reg->att[i]);
370
371 crt_outb(par, 0x1a, reg->ext[0]);
372 crt_outb(par, 0x1b, reg->ext[1]);
373
374 vga_enable_palette(par);
375 vga_enable_video(par);
376
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700377 banshee_make_room(par, 9);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700378 tdfx_outl(par, VGAINIT0, reg->vgainit0);
379 tdfx_outl(par, DACMODE, reg->dacmode);
380 tdfx_outl(par, VIDDESKSTRIDE, reg->stride);
Krzysztof Helt90b0f082007-10-16 01:28:48 -0700381 tdfx_outl(par, HWCURPATADDR, reg->curspataddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700383 tdfx_outl(par, VIDSCREENSIZE, reg->screensize);
384 tdfx_outl(par, VIDDESKSTART, reg->startaddr);
385 tdfx_outl(par, VIDPROCCFG, reg->vidcfg);
386 tdfx_outl(par, VGAINIT1, reg->vgainit1);
387 tdfx_outl(par, MISCINIT0, reg->miscinit0);
388
389 banshee_make_room(par, 8);
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700390 tdfx_outl(par, SRCBASE, reg->startaddr);
391 tdfx_outl(par, DSTBASE, reg->startaddr);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700392 tdfx_outl(par, COMMANDEXTRA_2D, 0);
393 tdfx_outl(par, CLIP0MIN, 0);
394 tdfx_outl(par, CLIP0MAX, 0x0fff0fff);
395 tdfx_outl(par, CLIP1MIN, 0);
396 tdfx_outl(par, CLIP1MAX, 0x0fff0fff);
397 tdfx_outl(par, SRCXY, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 banshee_wait_idle(info);
400}
401
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700402static unsigned long do_lfb_size(struct tdfx_par *par, unsigned short dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700404 u32 draminit0 = tdfx_inl(par, DRAMINIT0);
405 u32 draminit1 = tdfx_inl(par, DRAMINIT1);
Richard Drummond333f9812005-05-01 08:59:25 -0700406 u32 miscinit1;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700407 int num_chips = (draminit0 & DRAMINIT0_SGRAM_NUM) ? 8 : 4;
Richard Drummond333f9812005-05-01 08:59:25 -0700408 int chip_size; /* in MB */
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700409 int has_sgram = draminit1 & DRAMINIT1_MEM_SDRAM;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700410
Richard Drummond333f9812005-05-01 08:59:25 -0700411 if (dev_id < PCI_DEVICE_ID_3DFX_VOODOO5) {
412 /* Banshee/Voodoo3 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700413 chip_size = 2;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700414 if (has_sgram && (draminit0 & DRAMINIT0_SGRAM_TYPE))
415 chip_size = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 } else {
417 /* Voodoo4/5 */
Richard Drummond333f9812005-05-01 08:59:25 -0700418 has_sgram = 0;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700419 chip_size = draminit0 & DRAMINIT0_SGRAM_TYPE_MASK;
420 chip_size = 1 << (chip_size >> DRAMINIT0_SGRAM_TYPE_SHIFT);
Richard Drummond333f9812005-05-01 08:59:25 -0700421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Richard Drummond333f9812005-05-01 08:59:25 -0700423 /* disable block writes for SDRAM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 miscinit1 = tdfx_inl(par, MISCINIT1);
Richard Drummond333f9812005-05-01 08:59:25 -0700425 miscinit1 |= has_sgram ? 0 : MISCINIT1_2DBLOCK_DIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 miscinit1 |= MISCINIT1_CLUT_INV;
427
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700428 banshee_make_room(par, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 tdfx_outl(par, MISCINIT1, miscinit1);
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700430 return num_chips * chip_size * 1024l * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
433/* ------------------------------------------------------------------------- */
434
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700435static int tdfxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800437 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 u32 lpitch;
439
440 if (var->bits_per_pixel != 8 && var->bits_per_pixel != 16 &&
441 var->bits_per_pixel != 24 && var->bits_per_pixel != 32) {
442 DPRINTK("depth not supported: %u\n", var->bits_per_pixel);
443 return -EINVAL;
444 }
445
446 if (var->xres != var->xres_virtual)
447 var->xres_virtual = var->xres;
448
449 if (var->yres > var->yres_virtual)
450 var->yres_virtual = var->yres;
451
452 if (var->xoffset) {
453 DPRINTK("xoffset not supported\n");
454 return -EINVAL;
455 }
Krzysztof Helt90b0f082007-10-16 01:28:48 -0700456 var->yoffset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 /* Banshee doesn't support interlace, but Voodoo4/5 and probably Voodoo3 do. */
459 /* no direct information about device id now? use max_pixclock for this... */
460 if (((var->vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) &&
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700461 (par->max_pixclock < VOODOO3_MAX_PIXCLOCK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 DPRINTK("interlace not supported\n");
463 return -EINVAL;
464 }
465
466 var->xres = (var->xres + 15) & ~15; /* could sometimes be 8 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700467 lpitch = var->xres * ((var->bits_per_pixel + 7) >> 3);
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (var->xres < 320 || var->xres > 2048) {
470 DPRINTK("width not supported: %u\n", var->xres);
471 return -EINVAL;
472 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (var->yres < 200 || var->yres > 2048) {
475 DPRINTK("height not supported: %u\n", var->yres);
476 return -EINVAL;
477 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (lpitch * var->yres_virtual > info->fix.smem_len) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700480 var->yres_virtual = info->fix.smem_len / lpitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (var->yres_virtual < var->yres) {
482 DPRINTK("no memory for screen (%ux%ux%u)\n",
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700483 var->xres, var->yres_virtual,
484 var->bits_per_pixel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return -EINVAL;
486 }
487 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (PICOS2KHZ(var->pixclock) > par->max_pixclock) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700490 DPRINTK("pixclock too high (%ldKHz)\n",
491 PICOS2KHZ(var->pixclock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return -EINVAL;
493 }
494
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700495 var->transp.offset = 0;
496 var->transp.length = 0;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700497 switch (var->bits_per_pixel) {
498 case 8:
499 var->red.length = var->green.length = var->blue.length = 8;
500 break;
501 case 16:
502 var->red.offset = 11;
503 var->red.length = 5;
504 var->green.offset = 5;
505 var->green.length = 6;
506 var->blue.offset = 0;
507 var->blue.length = 5;
508 break;
509 case 32:
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700510 var->transp.offset = 24;
511 var->transp.length = 8;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700512 case 24:
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700513 var->red.offset = 16;
514 var->green.offset = 8;
515 var->blue.offset = 0;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700516 var->red.length = var->green.length = var->blue.length = 8;
517 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519 var->height = var->width = -1;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 var->accel_flags = FB_ACCELF_TEXT;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700522
523 DPRINTK("Checking graphics mode at %dx%d depth %d\n",
524 var->xres, var->yres, var->bits_per_pixel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return 0;
526}
527
528static int tdfxfb_set_par(struct fb_info *info)
529{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800530 struct tdfx_par *par = info->par;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700531 u32 hdispend = info->var.xres;
532 u32 hsyncsta = hdispend + info->var.right_margin;
533 u32 hsyncend = hsyncsta + info->var.hsync_len;
534 u32 htotal = hsyncend + info->var.left_margin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 u32 hd, hs, he, ht, hbs, hbe;
536 u32 vd, vs, ve, vt, vbs, vbe;
537 struct banshee_reg reg;
538 int fout, freq;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700539 u32 wd;
540 u32 cpp = (info->var.bits_per_pixel + 7) >> 3;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 memset(&reg, 0, sizeof(reg));
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700543
544 reg.vidcfg = VIDCFG_VIDPROC_ENABLE | VIDCFG_DESK_ENABLE |
545 VIDCFG_CURS_X11 |
546 ((cpp - 1) << VIDCFG_PIXFMT_SHIFT) |
547 (cpp != 1 ? VIDCFG_CLUT_BYPASS : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 /* PLL settings */
550 freq = PICOS2KHZ(info->var.pixclock);
551
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700552 reg.vidcfg &= ~VIDCFG_2X;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700554 if (freq > par->max_pixclock / 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 freq = freq > par->max_pixclock ? par->max_pixclock : freq;
556 reg.dacmode |= DACMODE_2X;
557 reg.vidcfg |= VIDCFG_2X;
558 hdispend >>= 1;
559 hsyncsta >>= 1;
560 hsyncend >>= 1;
561 htotal >>= 1;
562 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 hd = wd = (hdispend >> 3) - 1;
565 hs = (hsyncsta >> 3) - 1;
566 he = (hsyncend >> 3) - 1;
567 ht = (htotal >> 3) - 1;
568 hbs = hd;
569 hbe = ht;
570
571 if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE) {
572 vbs = vd = (info->var.yres << 1) - 1;
573 vs = vd + (info->var.lower_margin << 1);
574 ve = vs + (info->var.vsync_len << 1);
575 vbe = vt = ve + (info->var.upper_margin << 1) - 1;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700576 reg.screensize = info->var.xres | (info->var.yres << 13);
577 reg.vidcfg |= VIDCFG_HALF_MODE;
578 reg.crt[0x09] = 0x80;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 } else {
580 vbs = vd = info->var.yres - 1;
581 vs = vd + info->var.lower_margin;
582 ve = vs + info->var.vsync_len;
583 vbe = vt = ve + info->var.upper_margin - 1;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700584 reg.screensize = info->var.xres | (info->var.yres << 12);
585 reg.vidcfg &= ~VIDCFG_HALF_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 /* this is all pretty standard VGA register stuffing */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700589 reg.misc[0x00] = 0x0f |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 (info->var.xres < 400 ? 0xa0 :
591 info->var.xres < 480 ? 0x60 :
592 info->var.xres < 768 ? 0xe0 : 0x20);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 reg.gra[0x05] = 0x40;
595 reg.gra[0x06] = 0x05;
596 reg.gra[0x07] = 0x0f;
597 reg.gra[0x08] = 0xff;
598
599 reg.att[0x00] = 0x00;
600 reg.att[0x01] = 0x01;
601 reg.att[0x02] = 0x02;
602 reg.att[0x03] = 0x03;
603 reg.att[0x04] = 0x04;
604 reg.att[0x05] = 0x05;
605 reg.att[0x06] = 0x06;
606 reg.att[0x07] = 0x07;
607 reg.att[0x08] = 0x08;
608 reg.att[0x09] = 0x09;
609 reg.att[0x0a] = 0x0a;
610 reg.att[0x0b] = 0x0b;
611 reg.att[0x0c] = 0x0c;
612 reg.att[0x0d] = 0x0d;
613 reg.att[0x0e] = 0x0e;
614 reg.att[0x0f] = 0x0f;
615 reg.att[0x10] = 0x41;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 reg.att[0x12] = 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 reg.seq[0x00] = 0x03;
619 reg.seq[0x01] = 0x01; /* fixme: clkdiv2? */
620 reg.seq[0x02] = 0x0f;
621 reg.seq[0x03] = 0x00;
622 reg.seq[0x04] = 0x0e;
623
624 reg.crt[0x00] = ht - 4;
625 reg.crt[0x01] = hd;
626 reg.crt[0x02] = hbs;
627 reg.crt[0x03] = 0x80 | (hbe & 0x1f);
628 reg.crt[0x04] = hs;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700629 reg.crt[0x05] = ((hbe & 0x20) << 2) | (he & 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 reg.crt[0x06] = vt;
631 reg.crt[0x07] = ((vs & 0x200) >> 2) |
632 ((vd & 0x200) >> 3) |
633 ((vt & 0x200) >> 4) | 0x10 |
634 ((vbs & 0x100) >> 5) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700635 ((vs & 0x100) >> 6) |
636 ((vd & 0x100) >> 7) |
637 ((vt & 0x100) >> 8);
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700638 reg.crt[0x09] |= 0x40 | ((vbs & 0x200) >> 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 reg.crt[0x10] = vs;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700640 reg.crt[0x11] = (ve & 0x0f) | 0x20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 reg.crt[0x12] = vd;
642 reg.crt[0x13] = wd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 reg.crt[0x15] = vbs;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700644 reg.crt[0x16] = vbe + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 reg.crt[0x17] = 0xc3;
646 reg.crt[0x18] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700648 /* Banshee's nonvga stuff */
649 reg.ext[0x00] = (((ht & 0x100) >> 8) |
650 ((hd & 0x100) >> 6) |
651 ((hbs & 0x100) >> 4) |
652 ((hbe & 0x40) >> 1) |
653 ((hs & 0x100) >> 2) |
654 ((he & 0x20) << 2));
655 reg.ext[0x01] = (((vt & 0x400) >> 10) |
656 ((vd & 0x400) >> 8) |
657 ((vbs & 0x400) >> 6) |
658 ((vbe & 0x400) >> 4));
659
660 reg.vgainit0 = VGAINIT0_8BIT_DAC |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 VGAINIT0_EXT_ENABLE |
662 VGAINIT0_WAKEUP_3C3 |
663 VGAINIT0_ALT_READBACK |
664 VGAINIT0_EXTSHIFTOUT;
665 reg.vgainit1 = tdfx_inl(par, VGAINIT1) & 0x1fffff;
666
Krzysztof Helt90b0f082007-10-16 01:28:48 -0700667 if (hwcursor)
668 reg.curspataddr = info->fix.smem_len;
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 reg.cursloc = 0;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700671
672 reg.cursc0 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 reg.cursc1 = 0xffffff;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 reg.stride = info->var.xres * cpp;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700676 reg.startaddr = info->var.yoffset * reg.stride
677 + info->var.xoffset * cpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 reg.vidpll = do_calc_pll(freq, &fout);
680#if 0
681 reg.mempll = do_calc_pll(..., &fout);
682 reg.gfxpll = do_calc_pll(..., &fout);
683#endif
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED)
686 reg.vidcfg |= VIDCFG_INTERLACE;
687 reg.miscinit0 = tdfx_inl(par, MISCINIT0);
688
689#if defined(__BIG_ENDIAN)
690 switch (info->var.bits_per_pixel) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700691 case 8:
692 case 24:
693 reg.miscinit0 &= ~(1 << 30);
694 reg.miscinit0 &= ~(1 << 31);
695 break;
696 case 16:
697 reg.miscinit0 |= (1 << 30);
698 reg.miscinit0 |= (1 << 31);
699 break;
700 case 32:
701 reg.miscinit0 |= (1 << 30);
702 reg.miscinit0 &= ~(1 << 31);
703 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700705#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 do_write_regs(info, &reg);
707
708 /* Now change fb_fix_screeninfo according to changes in par */
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700709 info->fix.line_length = reg.stride;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700710 info->fix.visual = (info->var.bits_per_pixel == 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 ? FB_VISUAL_PSEUDOCOLOR
712 : FB_VISUAL_TRUECOLOR;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700713 DPRINTK("Graphics mode is now set at %dx%d depth %d\n",
714 info->var.xres, info->var.yres, info->var.bits_per_pixel);
715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
718/* A handy macro shamelessly pinched from matroxfb */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700719#define CNVT_TOHW(val, width) ((((val)<<(width))+0x7FFF-(val))>>16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700721static int tdfxfb_setcolreg(unsigned regno, unsigned red, unsigned green,
722 unsigned blue, unsigned transp,
723 struct fb_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800725 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 u32 rgbcol;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700727
728 if (regno >= info->cmap.len || regno > 255)
729 return 1;
730
Krzysztof Helt254c9472007-10-16 01:28:46 -0700731 /* grayscale works only partially under directcolor */
732 if (info->var.grayscale) {
733 /* grayscale = 0.30*R + 0.59*G + 0.11*B */
734 red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
735 }
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 switch (info->fix.visual) {
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800738 case FB_VISUAL_PSEUDOCOLOR:
739 rgbcol =(((u32)red & 0xff00) << 8) |
740 (((u32)green & 0xff00) << 0) |
741 (((u32)blue & 0xff00) >> 8);
742 do_setpalentry(par, regno, rgbcol);
743 break;
744 /* Truecolor has no hardware color palettes. */
745 case FB_VISUAL_TRUECOLOR:
746 if (regno < 16) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700747 rgbcol = (CNVT_TOHW(red, info->var.red.length) <<
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800748 info->var.red.offset) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700749 (CNVT_TOHW(green, info->var.green.length) <<
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800750 info->var.green.offset) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700751 (CNVT_TOHW(blue, info->var.blue.length) <<
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800752 info->var.blue.offset) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700753 (CNVT_TOHW(transp, info->var.transp.length) <<
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800754 info->var.transp.offset);
755 par->palette[regno] = rgbcol;
756 }
757
758 break;
759 default:
760 DPRINTK("bad depth %u\n", info->var.bits_per_pixel);
761 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return 0;
765}
766
767/* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */
768static int tdfxfb_blank(int blank, struct fb_info *info)
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700769{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800770 struct tdfx_par *par = info->par;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700771 int vgablank = 1;
772 u32 dacmode = tdfx_inl(par, DACMODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700774 dacmode &= ~(BIT(1) | BIT(3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 switch (blank) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700777 case FB_BLANK_UNBLANK: /* Screen: On; HSync: On, VSync: On */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700778 vgablank = 0;
779 break;
780 case FB_BLANK_NORMAL: /* Screen: Off; HSync: On, VSync: On */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700781 break;
782 case FB_BLANK_VSYNC_SUSPEND: /* Screen: Off; HSync: On, VSync: Off */
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700783 dacmode |= BIT(3);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700784 break;
785 case FB_BLANK_HSYNC_SUSPEND: /* Screen: Off; HSync: Off, VSync: On */
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700786 dacmode |= BIT(1);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700787 break;
788 case FB_BLANK_POWERDOWN: /* Screen: Off; HSync: Off, VSync: Off */
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700789 dacmode |= BIT(1) | BIT(3);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700790 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700793 banshee_make_room(par, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 tdfx_outl(par, DACMODE, dacmode);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700795 if (vgablank)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 vga_disable_video(par);
797 else
798 vga_enable_video(par);
799 return 0;
800}
801
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700802/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 * Set the starting position of the visible screen to var->yoffset
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700804 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805static int tdfxfb_pan_display(struct fb_var_screeninfo *var,
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700806 struct fb_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800808 struct tdfx_par *par = info->par;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700809 u32 addr = var->yoffset * info->fix.line_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 if (nopan || var->xoffset || (var->yoffset > var->yres_virtual))
812 return -EINVAL;
813 if ((var->yoffset + var->yres > var->yres_virtual && nowrap))
814 return -EINVAL;
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 banshee_make_room(par, 1);
817 tdfx_outl(par, VIDDESKSTART, addr);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 info->var.xoffset = var->xoffset;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700820 info->var.yoffset = var->yoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return 0;
822}
823
824#ifdef CONFIG_FB_3DFX_ACCEL
825/*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700826 * FillRect 2D command (solidfill or invert (via ROP_XOR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700828static void tdfxfb_fillrect(struct fb_info *info,
829 const struct fb_fillrect *rect)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800831 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 u32 bpp = info->var.bits_per_pixel;
833 u32 stride = info->fix.line_length;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700834 u32 fmt= stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 int tdfx_rop;
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700836 u32 dx = rect->dx;
837 u32 dy = rect->dy;
838 u32 dstbase = 0;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700839
840 if (rect->rop == ROP_COPY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 tdfx_rop = TDFX_ROP_COPY;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700842 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 tdfx_rop = TDFX_ROP_XOR;
844
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700845 /* asume always rect->height < 4096 */
846 if (dy + rect->height > 4095) {
847 dstbase = stride * dy;
848 dy = 0;
849 }
850 /* asume always rect->width < 4096 */
851 if (dx + rect->width > 4095) {
852 dstbase += dx * bpp >> 3;
853 dx = 0;
854 }
855 banshee_make_room(par, 6);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700856 tdfx_outl(par, DSTFORMAT, fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700858 tdfx_outl(par, COLORFORE, rect->color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 } else { /* FB_VISUAL_TRUECOLOR */
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800860 tdfx_outl(par, COLORFORE, par->palette[rect->color]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700862 tdfx_outl(par, COMMAND_2D, COMMAND_2D_FILLRECT | (tdfx_rop << 24));
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700863 tdfx_outl(par, DSTBASE, dstbase);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700864 tdfx_outl(par, DSTSIZE, rect->width | (rect->height << 16));
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700865 tdfx_outl(par, LAUNCH_2D, dx | (dy << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
868/*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700869 * Screen-to-Screen BitBlt 2D command (for the bmove fb op.)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700871static void tdfxfb_copyarea(struct fb_info *info,
872 const struct fb_copyarea *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800874 struct tdfx_par *par = info->par;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700875 u32 sx = area->sx, sy = area->sy, dx = area->dx, dy = area->dy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 u32 bpp = info->var.bits_per_pixel;
877 u32 stride = info->fix.line_length;
878 u32 blitcmd = COMMAND_2D_S2S_BITBLT | (TDFX_ROP_COPY << 24);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700879 u32 fmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700880 u32 dstbase = 0;
881 u32 srcbase = 0;
882
883 /* asume always area->height < 4096 */
884 if (sy + area->height > 4095) {
885 srcbase = stride * sy;
886 sy = 0;
887 }
888 /* asume always area->width < 4096 */
889 if (sx + area->width > 4095) {
890 srcbase += sx * bpp >> 3;
891 sx = 0;
892 }
893 /* asume always area->height < 4096 */
894 if (dy + area->height > 4095) {
895 dstbase = stride * dy;
896 dy = 0;
897 }
898 /* asume always area->width < 4096 */
899 if (dx + area->width > 4095) {
900 dstbase += dx * bpp >> 3;
901 dx = 0;
902 }
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (area->sx <= area->dx) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700905 //-X
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 blitcmd |= BIT(14);
907 sx += area->width - 1;
908 dx += area->width - 1;
909 }
910 if (area->sy <= area->dy) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700911 //-Y
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 blitcmd |= BIT(15);
913 sy += area->height - 1;
914 dy += area->height - 1;
915 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700916
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700917 banshee_make_room(par, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700919 tdfx_outl(par, SRCFORMAT, fmt);
920 tdfx_outl(par, DSTFORMAT, fmt);
921 tdfx_outl(par, COMMAND_2D, blitcmd);
922 tdfx_outl(par, DSTSIZE, area->width | (area->height << 16));
923 tdfx_outl(par, DSTXY, dx | (dy << 16));
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700924 tdfx_outl(par, SRCBASE, srcbase);
925 tdfx_outl(par, DSTBASE, dstbase);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700926 tdfx_outl(par, LAUNCH_2D, sx | (sy << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700929static void tdfxfb_imageblit(struct fb_info *info, const struct fb_image *image)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800931 struct tdfx_par *par = info->par;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700932 int size = image->height * ((image->width * image->depth + 7) >> 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 int fifo_free;
934 int i, stride = info->fix.line_length;
935 u32 bpp = info->var.bits_per_pixel;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700936 u32 dstfmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 u8 *chardata = (u8 *) image->data;
938 u32 srcfmt;
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700939 u32 dx = image->dx;
940 u32 dy = image->dy;
941 u32 dstbase = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 if (image->depth != 1) {
944 //banshee_make_room(par, 6 + ((size + 3) >> 2));
945 //srcfmt = stride | ((bpp+((bpp==8) ? 0 : 8)) << 13) | 0x400000;
946 cfb_imageblit(info, image);
947 return;
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700948 }
949 banshee_make_room(par, 9);
950 switch (info->fix.visual) {
951 case FB_VISUAL_PSEUDOCOLOR:
952 tdfx_outl(par, COLORFORE, image->fg_color);
953 tdfx_outl(par, COLORBACK, image->bg_color);
954 break;
955 case FB_VISUAL_TRUECOLOR:
956 default:
957 tdfx_outl(par, COLORFORE,
958 par->palette[image->fg_color]);
959 tdfx_outl(par, COLORBACK,
960 par->palette[image->bg_color]);
961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962#ifdef __BIG_ENDIAN
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700963 srcfmt = 0x400000 | BIT(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964#else
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700965 srcfmt = 0x400000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966#endif
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700967 /* asume always image->height < 4096 */
968 if (dy + image->height > 4095) {
969 dstbase = stride * dy;
970 dy = 0;
971 }
972 /* asume always image->width < 4096 */
973 if (dx + image->width > 4095) {
974 dstbase += dx * bpp >> 3;
975 dx = 0;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700978 tdfx_outl(par, DSTBASE, dstbase);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700979 tdfx_outl(par, SRCXY, 0);
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700980 tdfx_outl(par, DSTXY, dx | (dy << 16));
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700981 tdfx_outl(par, COMMAND_2D, COMMAND_2D_H2S_BITBLT | (TDFX_ROP_COPY << 24));
982 tdfx_outl(par, SRCFORMAT, srcfmt);
983 tdfx_outl(par, DSTFORMAT, dstfmt);
984 tdfx_outl(par, DSTSIZE, image->width | (image->height << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 /* A count of how many free FIFO entries we've requested.
987 * When this goes negative, we need to request more. */
988 fifo_free = 0;
989
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700990 /* Send four bytes at a time of data */
991 for (i = (size >> 2); i > 0; i--) {
992 if (--fifo_free < 0) {
993 fifo_free = 31;
994 banshee_make_room(par, fifo_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700996 tdfx_outl(par, LAUNCH_2D, *(u32*)chardata);
997 chardata += 4;
998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001000 /* Send the leftovers now */
1001 banshee_make_room(par, 3);
Krzysztof Helt4f05b532007-10-16 01:28:48 -07001002 switch (size % 4) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001003 case 0:
1004 break;
1005 case 1:
1006 tdfx_outl(par, LAUNCH_2D, *chardata);
1007 break;
1008 case 2:
1009 tdfx_outl(par, LAUNCH_2D, *(u16*)chardata);
1010 break;
1011 case 3:
1012 tdfx_outl(par, LAUNCH_2D,
1013 *(u16*)chardata | ((chardata[3]) << 24));
1014 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016}
1017#endif /* CONFIG_FB_3DFX_ACCEL */
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019static int tdfxfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
1020{
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001021 struct tdfx_par *par = info->par;
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001022 u32 vidcfg;
1023
1024 if (!hwcursor)
1025 return -EINVAL; /* just to force soft_cursor() call */
1026
1027 /* Too large of a cursor or wrong bpp :-( */
1028 if (cursor->image.width > 64 ||
1029 cursor->image.height > 64 ||
1030 cursor->image.depth > 1)
1031 return -EINVAL;
1032
1033 vidcfg = tdfx_inl(par, VIDPROCCFG);
1034 if (cursor->enable)
1035 tdfx_outl(par, VIDPROCCFG, vidcfg | VIDCFG_HWCURSOR_ENABLE);
1036 else
1037 tdfx_outl(par, VIDPROCCFG, vidcfg & ~VIDCFG_HWCURSOR_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 /*
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001040 * If the cursor is not be changed this means either we want the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 * current cursor state (if enable is set) or we want to query what
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001042 * we can do with the cursor (if enable is not set)
1043 */
1044 if (!cursor->set)
1045 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 /* fix cursor color - XFree86 forgets to restore it properly */
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001048 if (cursor->set & FB_CUR_SETCMAP) {
1049 struct fb_cmap cmap = info->cmap;
1050 u32 bg_idx = cursor->image.bg_color;
1051 u32 fg_idx = cursor->image.fg_color;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 unsigned long bg_color, fg_color;
1053
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001054 fg_color = (((u32)cmap.red[fg_idx] & 0xff00) << 8) |
1055 (((u32)cmap.green[fg_idx] & 0xff00) << 0) |
1056 (((u32)cmap.blue[fg_idx] & 0xff00) >> 8);
1057 bg_color = (((u32)cmap.red[bg_idx] & 0xff00) << 8) |
1058 (((u32)cmap.green[bg_idx] & 0xff00) << 0) |
1059 (((u32)cmap.blue[bg_idx] & 0xff00) >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 banshee_make_room(par, 2);
1061 tdfx_outl(par, HWCURC0, bg_color);
1062 tdfx_outl(par, HWCURC1, fg_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
1064
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001065 if (cursor->set & FB_CUR_SETPOS) {
1066 int x = cursor->image.dx;
1067 int y = cursor->image.dy - info->var.yoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 x += 63;
1070 y += 63;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 banshee_make_room(par, 1);
1072 tdfx_outl(par, HWCURLOC, (y << 16) + x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001074 if (cursor->set & (FB_CUR_SETIMAGE | FB_CUR_SETSHAPE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 /*
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001076 * Voodoo 3 and above cards use 2 monochrome cursor patterns.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 * The reason is so the card can fetch 8 words at a time
1078 * and are stored on chip for use for the next 8 scanlines.
1079 * This reduces the number of times for access to draw the
1080 * cursor for each screen refresh.
1081 * Each pattern is a bitmap of 64 bit wide and 64 bit high
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001082 * (total of 8192 bits or 1024 bytes). The two patterns are
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 * stored in such a way that pattern 0 always resides in the
1084 * lower half (least significant 64 bits) of a 128 bit word
1085 * and pattern 1 the upper half. If you examine the data of
1086 * the cursor image the graphics card uses then from the
1087 * begining you see line one of pattern 0, line one of
1088 * pattern 1, line two of pattern 0, line two of pattern 1,
1089 * etc etc. The linear stride for the cursor is always 16 bytes
1090 * (128 bits) which is the maximum cursor width times two for
1091 * the two monochrome patterns.
1092 */
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001093 u8 __iomem *cursorbase = info->screen_base + info->fix.smem_len;
1094 u8 *bitmap = (u8 *)cursor->image.data;
1095 u8 *mask = (u8 *)cursor->mask;
1096 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001098 fb_memset(cursorbase, 0, 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001100 for (i = 0; i < cursor->image.height; i++) {
1101 int h = 0;
1102 int j = (cursor->image.width + 7) >> 3;
1103
1104 for (; j > 0; j--) {
1105 u8 data = *mask ^ *bitmap;
1106 if (cursor->rop == ROP_COPY)
1107 data = *mask & *bitmap;
1108 /* Pattern 0. Copy the cursor mask to it */
1109 fb_writeb(*mask, cursorbase + h);
1110 mask++;
1111 /* Pattern 1. Copy the cursor bitmap to it */
1112 fb_writeb(data, cursorbase + h + 8);
1113 bitmap++;
1114 h++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001116 cursorbase += 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return 0;
1120}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001122static struct fb_ops tdfxfb_ops = {
1123 .owner = THIS_MODULE,
1124 .fb_check_var = tdfxfb_check_var,
1125 .fb_set_par = tdfxfb_set_par,
1126 .fb_setcolreg = tdfxfb_setcolreg,
1127 .fb_blank = tdfxfb_blank,
1128 .fb_pan_display = tdfxfb_pan_display,
1129 .fb_sync = banshee_wait_idle,
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001130 .fb_cursor = tdfxfb_cursor,
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001131#ifdef CONFIG_FB_3DFX_ACCEL
1132 .fb_fillrect = tdfxfb_fillrect,
1133 .fb_copyarea = tdfxfb_copyarea,
1134 .fb_imageblit = tdfxfb_imageblit,
1135#else
1136 .fb_fillrect = cfb_fillrect,
1137 .fb_copyarea = cfb_copyarea,
1138 .fb_imageblit = cfb_imageblit,
1139#endif
1140};
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142/**
1143 * tdfxfb_probe - Device Initializiation
1144 *
1145 * @pdev: PCI Device to initialize
1146 * @id: PCI Device ID
1147 *
1148 * Initializes and allocates resources for PCI device @pdev.
1149 *
1150 */
1151static int __devinit tdfxfb_probe(struct pci_dev *pdev,
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001152 const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
1154 struct tdfx_par *default_par;
1155 struct fb_info *info;
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001156 int err, lpitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 if ((err = pci_enable_device(pdev))) {
1159 printk(KERN_WARNING "tdfxfb: Can't enable pdev: %d\n", err);
1160 return err;
1161 }
1162
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001163 info = framebuffer_alloc(sizeof(struct tdfx_par), &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001165 if (!info)
1166 return -ENOMEM;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 default_par = info->par;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 /* Configure the default fb_fix_screeninfo first */
1171 switch (pdev->device) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001172 case PCI_DEVICE_ID_3DFX_BANSHEE:
1173 strcat(tdfx_fix.id, " Banshee");
1174 default_par->max_pixclock = BANSHEE_MAX_PIXCLOCK;
1175 break;
1176 case PCI_DEVICE_ID_3DFX_VOODOO3:
1177 strcat(tdfx_fix.id, " Voodoo3");
1178 default_par->max_pixclock = VOODOO3_MAX_PIXCLOCK;
1179 break;
1180 case PCI_DEVICE_ID_3DFX_VOODOO5:
1181 strcat(tdfx_fix.id, " Voodoo5");
1182 default_par->max_pixclock = VOODOO5_MAX_PIXCLOCK;
1183 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
1185
1186 tdfx_fix.mmio_start = pci_resource_start(pdev, 0);
1187 tdfx_fix.mmio_len = pci_resource_len(pdev, 0);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001188 if (!request_mem_region(tdfx_fix.mmio_start, tdfx_fix.mmio_len,
1189 "tdfx regbase")) {
1190 printk(KERN_WARNING "tdfxfb: Can't reserve regbase\n");
1191 goto out_err;
1192 }
1193
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001194 default_par->regbase_virt =
1195 ioremap_nocache(tdfx_fix.mmio_start, tdfx_fix.mmio_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 if (!default_par->regbase_virt) {
1197 printk("fb: Can't remap %s register area.\n", tdfx_fix.id);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001198 goto out_err_regbase;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 tdfx_fix.smem_start = pci_resource_start(pdev, 1);
1202 if (!(tdfx_fix.smem_len = do_lfb_size(default_par, pdev->device))) {
1203 printk("fb: Can't count %s memory.\n", tdfx_fix.id);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001204 goto out_err_regbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001207 if (!request_mem_region(tdfx_fix.smem_start,
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001208 pci_resource_len(pdev, 1), "tdfx smem")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 printk(KERN_WARNING "tdfxfb: Can't reserve smem\n");
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001210 goto out_err_regbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
1212
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001213 info->screen_base = ioremap_nocache(tdfx_fix.smem_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 tdfx_fix.smem_len);
1215 if (!info->screen_base) {
1216 printk("fb: Can't remap %s framebuffer.\n", tdfx_fix.id);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001217 goto out_err_screenbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219
1220 default_par->iobase = pci_resource_start(pdev, 2);
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (!request_region(pci_resource_start(pdev, 2),
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001223 pci_resource_len(pdev, 2), "tdfx iobase")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 printk(KERN_WARNING "tdfxfb: Can't reserve iobase\n");
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001225 goto out_err_screenbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227
1228 printk("fb: %s memory = %dK\n", tdfx_fix.id, tdfx_fix.smem_len >> 10);
1229
1230 tdfx_fix.ypanstep = nopan ? 0 : 1;
1231 tdfx_fix.ywrapstep = nowrap ? 0 : 1;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 info->fbops = &tdfxfb_ops;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001234 info->fix = tdfx_fix;
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001235 info->pseudo_palette = default_par->palette;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
1237#ifdef CONFIG_FB_3DFX_ACCEL
1238 info->flags |= FBINFO_HWACCEL_FILLRECT |
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001239 FBINFO_HWACCEL_COPYAREA |
1240 FBINFO_HWACCEL_IMAGEBLIT |
1241 FBINFO_READS_FAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242#endif
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001243 /* reserve 8192 bits for cursor */
1244 /* the 2.4 driver says PAGE_MASK boundary is not enough for Voodoo4 */
1245 if (hwcursor)
1246 info->fix.smem_len = (info->fix.smem_len - 1024) &
1247 (PAGE_MASK << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 if (!mode_option)
1250 mode_option = "640x480@60";
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001251
1252 err = fb_find_mode(&info->var, info, mode_option, NULL, 0, NULL, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (!err || err == 4)
1254 info->var = tdfx_var;
1255
1256 /* maximize virtual vertical length */
1257 lpitch = info->var.xres_virtual * ((info->var.bits_per_pixel + 7) >> 3);
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001258 info->var.yres_virtual = info->fix.smem_len / lpitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (info->var.yres_virtual < info->var.yres)
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001260 goto out_err_iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
1263 printk(KERN_WARNING "tdfxfb: Can't allocate color map\n");
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001264 goto out_err_iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266
1267 if (register_framebuffer(info) < 0) {
1268 printk("tdfxfb: can't register framebuffer\n");
1269 fb_dealloc_cmap(&info->cmap);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001270 goto out_err_iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
1272 /*
1273 * Our driver data
1274 */
1275 pci_set_drvdata(pdev, info);
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001276 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001278out_err_iobase:
1279 release_mem_region(pci_resource_start(pdev, 2),
1280 pci_resource_len(pdev, 2));
1281out_err_screenbase:
1282 if (info->screen_base)
1283 iounmap(info->screen_base);
1284 release_mem_region(tdfx_fix.smem_start, pci_resource_len(pdev, 1));
1285out_err_regbase:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 /*
1287 * Cleanup after anything that was remapped/allocated.
1288 */
1289 if (default_par->regbase_virt)
1290 iounmap(default_par->regbase_virt);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001291 release_mem_region(tdfx_fix.mmio_start, tdfx_fix.mmio_len);
1292out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 framebuffer_release(info);
1294 return -ENXIO;
1295}
1296
1297#ifndef MODULE
Adrian Bunka0aa7d02006-01-09 20:54:04 -08001298static void tdfxfb_setup(char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001300 char *this_opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 if (!options || !*options)
1303 return;
1304
1305 while ((this_opt = strsep(&options, ",")) != NULL) {
1306 if (!*this_opt)
1307 continue;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001308 if (!strcmp(this_opt, "nopan")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 nopan = 1;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001310 } else if (!strcmp(this_opt, "nowrap")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 nowrap = 1;
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001312 } else if (!strcmp(this_opt, "hwcursor")) {
1313 hwcursor = simple_strtoul(opt + 9, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 } else {
1315 mode_option = this_opt;
1316 }
1317 }
1318}
1319#endif
1320
1321/**
1322 * tdfxfb_remove - Device removal
1323 *
1324 * @pdev: PCI Device to cleanup
1325 *
1326 * Releases all resources allocated during the course of the driver's
1327 * lifetime for the PCI device @pdev.
1328 *
1329 */
1330static void __devexit tdfxfb_remove(struct pci_dev *pdev)
1331{
1332 struct fb_info *info = pci_get_drvdata(pdev);
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001333 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 unregister_framebuffer(info);
1336 iounmap(par->regbase_virt);
1337 iounmap(info->screen_base);
1338
1339 /* Clean up after reserved regions */
1340 release_region(pci_resource_start(pdev, 2),
1341 pci_resource_len(pdev, 2));
1342 release_mem_region(pci_resource_start(pdev, 1),
1343 pci_resource_len(pdev, 1));
1344 release_mem_region(pci_resource_start(pdev, 0),
1345 pci_resource_len(pdev, 0));
1346 pci_set_drvdata(pdev, NULL);
1347 framebuffer_release(info);
1348}
1349
1350static int __init tdfxfb_init(void)
1351{
1352#ifndef MODULE
1353 char *option = NULL;
1354
1355 if (fb_get_options("tdfxfb", &option))
1356 return -ENODEV;
1357
1358 tdfxfb_setup(option);
1359#endif
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001360 return pci_register_driver(&tdfxfb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362
1363static void __exit tdfxfb_exit(void)
1364{
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001365 pci_unregister_driver(&tdfxfb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366}
1367
1368MODULE_AUTHOR("Hannu Mallat <hmallat@cc.hut.fi>");
1369MODULE_DESCRIPTION("3Dfx framebuffer device driver");
1370MODULE_LICENSE("GPL");
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001371
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001372module_param(hwcursor, int, 0644);
1373MODULE_PARM_DESC(hwcursor, "Enable hardware cursor "
1374 "(1=enable, 0=disable, default=1)");
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376module_init(tdfxfb_init);
1377module_exit(tdfxfb_exit);