blob: c032f6feb13eb8720d5c1b12dabed97ff338193d [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>
66#include <linux/delay.h>
67#include <linux/interrupt.h>
68#include <linux/fb.h>
69#include <linux/init.h>
70#include <linux/pci.h>
71#include <linux/nvram.h>
72#include <asm/io.h>
73#include <linux/timer.h>
74#include <linux/spinlock.h>
75
76#include <video/tdfx.h>
77
Krzysztof Helt8af1d502007-10-16 01:28:43 -070078#undef TDFXFB_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#ifdef TDFXFB_DEBUG
80#define DPRINTK(a,b...) printk(KERN_DEBUG "fb: %s: " a, __FUNCTION__ , ## b)
81#else
82#define DPRINTK(a,b...)
Krzysztof Helt8af1d502007-10-16 01:28:43 -070083#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85#define BANSHEE_MAX_PIXCLOCK 270000
86#define VOODOO3_MAX_PIXCLOCK 300000
87#define VOODOO5_MAX_PIXCLOCK 350000
88
89static struct fb_fix_screeninfo tdfx_fix __devinitdata = {
90 .id = "3Dfx",
91 .type = FB_TYPE_PACKED_PIXELS,
Krzysztof Helt8af1d502007-10-16 01:28:43 -070092 .visual = FB_VISUAL_PSEUDOCOLOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 .ypanstep = 1,
Krzysztof Helt8af1d502007-10-16 01:28:43 -070094 .ywrapstep = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 .accel = FB_ACCEL_3DFX_BANSHEE
96};
97
98static struct fb_var_screeninfo tdfx_var __devinitdata = {
99 /* "640x480, 8 bpp @ 60 Hz */
100 .xres = 640,
101 .yres = 480,
102 .xres_virtual = 640,
103 .yres_virtual = 1024,
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700104 .bits_per_pixel = 8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 .red = {0, 8, 0},
106 .blue = {0, 8, 0},
107 .green = {0, 8, 0},
108 .activate = FB_ACTIVATE_NOW,
109 .height = -1,
110 .width = -1,
111 .accel_flags = FB_ACCELF_TEXT,
112 .pixclock = 39722,
113 .left_margin = 40,
114 .right_margin = 24,
115 .upper_margin = 32,
116 .lower_margin = 11,
117 .hsync_len = 96,
118 .vsync_len = 2,
119 .vmode = FB_VMODE_NONINTERLACED
120};
121
122/*
123 * PCI driver prototypes
124 */
125static int __devinit tdfxfb_probe(struct pci_dev *pdev,
126 const struct pci_device_id *id);
127static void __devexit tdfxfb_remove(struct pci_dev *pdev);
128
129static struct pci_device_id tdfxfb_id_table[] = {
130 { PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_BANSHEE,
131 PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
132 0xff0000, 0 },
133 { PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_VOODOO3,
134 PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
135 0xff0000, 0 },
136 { PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_VOODOO5,
137 PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
138 0xff0000, 0 },
139 { 0, }
140};
141
142static struct pci_driver tdfxfb_driver = {
143 .name = "tdfxfb",
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700144 .id_table = tdfxfb_id_table,
145 .probe = tdfxfb_probe,
146 .remove = __devexit_p(tdfxfb_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147};
148
149MODULE_DEVICE_TABLE(pci, tdfxfb_id_table);
150
151/*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700152 * Driver data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 */
154static int nopan = 0;
155static int nowrap = 1; // not implemented (yet)
156static char *mode_option __devinitdata = NULL;
157
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700158/* -------------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 * Hardware-specific funcions
160 * ------------------------------------------------------------------------- */
161
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700162#ifdef VGA_REG_IO
163static inline u8 vga_inb(struct tdfx_par *par, u32 reg)
164{
165 return inb(reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700167
168static inline void vga_outb(struct tdfx_par *par, u32 reg, u8 val)
169{
170 outb(val, reg);
171}
172#else
173static inline u8 vga_inb(struct tdfx_par *par, u32 reg)
174{
175 return inb(par->iobase + reg - 0x300);
176}
177static inline void vga_outb(struct tdfx_par *par, u32 reg, u8 val)
178{
179 outb(val, par->iobase + reg - 0x300);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181#endif
182
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700183static inline void gra_outb(struct tdfx_par *par, u32 idx, u8 val)
184{
185 vga_outb(par, GRA_I, idx);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700186 wmb();
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700187 vga_outb(par, GRA_D, val);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700188 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700191static inline void seq_outb(struct tdfx_par *par, u32 idx, u8 val)
192{
193 vga_outb(par, SEQ_I, idx);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700194 wmb();
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700195 vga_outb(par, SEQ_D, val);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700196 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700199static inline u8 seq_inb(struct tdfx_par *par, u32 idx)
200{
201 vga_outb(par, SEQ_I, idx);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700202 mb();
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700203 return vga_inb(par, SEQ_D);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700206static inline void crt_outb(struct tdfx_par *par, u32 idx, u8 val)
207{
208 vga_outb(par, CRT_I, idx);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700209 wmb();
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700210 vga_outb(par, CRT_D, val);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700211 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700214static inline u8 crt_inb(struct tdfx_par *par, u32 idx)
215{
216 vga_outb(par, CRT_I, idx);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700217 mb();
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700218 return vga_inb(par, CRT_D);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700221static inline void att_outb(struct tdfx_par *par, u32 idx, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 unsigned char tmp;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 tmp = vga_inb(par, IS1_R);
226 vga_outb(par, ATT_IW, idx);
227 vga_outb(par, ATT_IW, val);
228}
229
230static inline void vga_disable_video(struct tdfx_par *par)
231{
232 unsigned char s;
233
234 s = seq_inb(par, 0x01) | 0x20;
235 seq_outb(par, 0x00, 0x01);
236 seq_outb(par, 0x01, s);
237 seq_outb(par, 0x00, 0x03);
238}
239
240static inline void vga_enable_video(struct tdfx_par *par)
241{
242 unsigned char s;
243
244 s = seq_inb(par, 0x01) & 0xdf;
245 seq_outb(par, 0x00, 0x01);
246 seq_outb(par, 0x01, s);
247 seq_outb(par, 0x00, 0x03);
248}
249
250static inline void vga_enable_palette(struct tdfx_par *par)
251{
252 vga_inb(par, IS1_R);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700253 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 vga_outb(par, ATT_IW, 0x20);
255}
256
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700257static inline u32 tdfx_inl(struct tdfx_par *par, unsigned int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 return readl(par->regbase_virt + reg);
260}
261
262static inline void tdfx_outl(struct tdfx_par *par, unsigned int reg, u32 val)
263{
264 writel(val, par->regbase_virt + reg);
265}
266
267static inline void banshee_make_room(struct tdfx_par *par, int size)
268{
269 /* Note: The Voodoo3's onboard FIFO has 32 slots. This loop
270 * won't quit if you ask for more. */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700271 while ((tdfx_inl(par, STATUS) & 0x1f) < size - 1) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
274static int banshee_wait_idle(struct fb_info *info)
275{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800276 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 int i = 0;
278
279 banshee_make_room(par, 1);
280 tdfx_outl(par, COMMAND_3D, COMMAND_3D_NOP);
281
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700282 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 i = (tdfx_inl(par, STATUS) & STATUS_BUSY) ? 0 : i + 1;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700284 if (i == 3)
285 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287 return 0;
288}
289
290/*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700291 * Set the color of a palette entry in 8bpp mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
293static inline void do_setpalentry(struct tdfx_par *par, unsigned regno, u32 c)
294{
295 banshee_make_room(par, 2);
296 tdfx_outl(par, DACADDR, regno);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700297 /* read after write makes it working */
298 tdfx_inl(par, DACADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 tdfx_outl(par, DACDATA, c);
300}
301
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700302static u32 do_calc_pll(int freq, int *freq_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700304 int m, n, k, best_m, best_n, best_k, best_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 int fref = 14318;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 best_error = freq;
308 best_n = best_m = best_k = 0;
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700309
310 for (k = 3; k >= 0; k--) {
311 for (m = 63; m >= 0; m--) {
312 /*
313 * Estimate value of n that produces target frequency
314 * with current m and k
315 */
316 int n_estimated = (freq * (m + 2) * (1 << k) / fref) - 2;
317
318 /* Search neighborhood of estimated n */
319 for (n = max(0, n_estimated - 1);
320 n <= min(255, n_estimated + 1); n++) {
321 /*
322 * Calculate PLL freqency with current m, k and
323 * estimated n
324 */
325 int f = fref * (n + 2) / (m + 2) / (1 << k);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700326 int error = abs(f - freq);
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700327
328 /*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700329 * If this is the closest we've come to the
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700330 * target frequency then remember n, m and k
331 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700332 if (error < best_error) {
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700333 best_error = error;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700334 best_n = n;
335 best_m = m;
336 best_k = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338 }
339 }
340 }
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 n = best_n;
343 m = best_m;
344 k = best_k;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700345 *freq_out = fref * (n + 2) / (m + 2) / (1 << k);
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return (n << 8) | (m << 2) | k;
348}
349
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700350static void do_write_regs(struct fb_info *info, struct banshee_reg *reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800352 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 int i;
354
355 banshee_wait_idle(info);
356
357 tdfx_outl(par, MISCINIT1, tdfx_inl(par, MISCINIT1) | 0x01);
358
359 crt_outb(par, 0x11, crt_inb(par, 0x11) & 0x7f); /* CRT unprotect */
360
361 banshee_make_room(par, 3);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700362 tdfx_outl(par, VGAINIT1, reg->vgainit1 & 0x001FFFFF);
363 tdfx_outl(par, VIDPROCCFG, reg->vidcfg & ~0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364#if 0
365 tdfx_outl(par, PLLCTRL1, reg->mempll);
366 tdfx_outl(par, PLLCTRL2, reg->gfxpll);
367#endif
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700368 tdfx_outl(par, PLLCTRL0, reg->vidpll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 vga_outb(par, MISC_W, reg->misc[0x00] | 0x01);
371
372 for (i = 0; i < 5; i++)
373 seq_outb(par, i, reg->seq[i]);
374
375 for (i = 0; i < 25; i++)
376 crt_outb(par, i, reg->crt[i]);
377
378 for (i = 0; i < 9; i++)
379 gra_outb(par, i, reg->gra[i]);
380
381 for (i = 0; i < 21; i++)
382 att_outb(par, i, reg->att[i]);
383
384 crt_outb(par, 0x1a, reg->ext[0]);
385 crt_outb(par, 0x1b, reg->ext[1]);
386
387 vga_enable_palette(par);
388 vga_enable_video(par);
389
390 banshee_make_room(par, 11);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700391 tdfx_outl(par, VGAINIT0, reg->vgainit0);
392 tdfx_outl(par, DACMODE, reg->dacmode);
393 tdfx_outl(par, VIDDESKSTRIDE, reg->stride);
394 tdfx_outl(par, HWCURPATADDR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700396 tdfx_outl(par, VIDSCREENSIZE, reg->screensize);
397 tdfx_outl(par, VIDDESKSTART, reg->startaddr);
398 tdfx_outl(par, VIDPROCCFG, reg->vidcfg);
399 tdfx_outl(par, VGAINIT1, reg->vgainit1);
400 tdfx_outl(par, MISCINIT0, reg->miscinit0);
401
402 banshee_make_room(par, 8);
403 tdfx_outl(par, SRCBASE, reg->srcbase);
404 tdfx_outl(par, DSTBASE, reg->dstbase);
405 tdfx_outl(par, COMMANDEXTRA_2D, 0);
406 tdfx_outl(par, CLIP0MIN, 0);
407 tdfx_outl(par, CLIP0MAX, 0x0fff0fff);
408 tdfx_outl(par, CLIP1MIN, 0);
409 tdfx_outl(par, CLIP1MAX, 0x0fff0fff);
410 tdfx_outl(par, SRCXY, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 banshee_wait_idle(info);
413}
414
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700415static unsigned long do_lfb_size(struct tdfx_par *par, unsigned short dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Richard Drummond333f9812005-05-01 08:59:25 -0700417 u32 draminit0;
418 u32 draminit1;
419 u32 miscinit1;
420
421 int num_chips;
422 int chip_size; /* in MB */
423 u32 lfbsize;
424 int has_sgram;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700426 draminit0 = tdfx_inl(par, DRAMINIT0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 draminit1 = tdfx_inl(par, DRAMINIT1);
Richard Drummond333f9812005-05-01 08:59:25 -0700428
429 num_chips = (draminit0 & DRAMINIT0_SGRAM_NUM) ? 8 : 4;
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 */
433 has_sgram = draminit1 & DRAMINIT1_MEM_SDRAM;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700434 chip_size = 2;
435 if (has_sgram)
436 chip_size = (draminit0 & DRAMINIT0_SGRAM_TYPE) ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 } else {
438 /* Voodoo4/5 */
Richard Drummond333f9812005-05-01 08:59:25 -0700439 has_sgram = 0;
440 chip_size = 1 << ((draminit0 & DRAMINIT0_SGRAM_TYPE_MASK) >> DRAMINIT0_SGRAM_TYPE_SHIFT);
441 }
442 lfbsize = num_chips * chip_size * 1024 * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Richard Drummond333f9812005-05-01 08:59:25 -0700444 /* disable block writes for SDRAM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 miscinit1 = tdfx_inl(par, MISCINIT1);
Richard Drummond333f9812005-05-01 08:59:25 -0700446 miscinit1 |= has_sgram ? 0 : MISCINIT1_2DBLOCK_DIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 miscinit1 |= MISCINIT1_CLUT_INV;
448
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700449 banshee_make_room(par, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 tdfx_outl(par, MISCINIT1, miscinit1);
451 return lfbsize;
452}
453
454/* ------------------------------------------------------------------------- */
455
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700456static int tdfxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800458 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 u32 lpitch;
460
461 if (var->bits_per_pixel != 8 && var->bits_per_pixel != 16 &&
462 var->bits_per_pixel != 24 && var->bits_per_pixel != 32) {
463 DPRINTK("depth not supported: %u\n", var->bits_per_pixel);
464 return -EINVAL;
465 }
466
467 if (var->xres != var->xres_virtual)
468 var->xres_virtual = var->xres;
469
470 if (var->yres > var->yres_virtual)
471 var->yres_virtual = var->yres;
472
473 if (var->xoffset) {
474 DPRINTK("xoffset not supported\n");
475 return -EINVAL;
476 }
477
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 u32 hdispend, hsyncsta, hsyncend, htotal;
552 u32 hd, hs, he, ht, hbs, hbe;
553 u32 vd, vs, ve, vt, vbs, vbe;
554 struct banshee_reg reg;
555 int fout, freq;
556 u32 wd, cpp;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700557
558 par->baseline = 0;
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 memset(&reg, 0, sizeof(reg));
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700561 cpp = (info->var.bits_per_pixel + 7) / 8;
562
563 reg.vidcfg = VIDCFG_VIDPROC_ENABLE | VIDCFG_DESK_ENABLE |
564 VIDCFG_CURS_X11 |
565 ((cpp - 1) << VIDCFG_PIXFMT_SHIFT) |
566 (cpp != 1 ? VIDCFG_CLUT_BYPASS : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 /* PLL settings */
569 freq = PICOS2KHZ(info->var.pixclock);
570
571 reg.dacmode = 0;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700572 reg.vidcfg &= ~VIDCFG_2X;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 hdispend = info->var.xres;
575 hsyncsta = hdispend + info->var.right_margin;
576 hsyncend = hsyncsta + info->var.hsync_len;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700577 htotal = hsyncend + info->var.left_margin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700579 if (freq > par->max_pixclock / 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 freq = freq > par->max_pixclock ? par->max_pixclock : freq;
581 reg.dacmode |= DACMODE_2X;
582 reg.vidcfg |= VIDCFG_2X;
583 hdispend >>= 1;
584 hsyncsta >>= 1;
585 hsyncend >>= 1;
586 htotal >>= 1;
587 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 hd = wd = (hdispend >> 3) - 1;
590 hs = (hsyncsta >> 3) - 1;
591 he = (hsyncend >> 3) - 1;
592 ht = (htotal >> 3) - 1;
593 hbs = hd;
594 hbe = ht;
595
596 if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE) {
597 vbs = vd = (info->var.yres << 1) - 1;
598 vs = vd + (info->var.lower_margin << 1);
599 ve = vs + (info->var.vsync_len << 1);
600 vbe = vt = ve + (info->var.upper_margin << 1) - 1;
601 } else {
602 vbs = vd = info->var.yres - 1;
603 vs = vd + info->var.lower_margin;
604 ve = vs + info->var.vsync_len;
605 vbe = vt = ve + info->var.upper_margin - 1;
606 }
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[0x00] = 0x00;
615 reg.gra[0x01] = 0x00;
616 reg.gra[0x02] = 0x00;
617 reg.gra[0x03] = 0x00;
618 reg.gra[0x04] = 0x00;
619 reg.gra[0x05] = 0x40;
620 reg.gra[0x06] = 0x05;
621 reg.gra[0x07] = 0x0f;
622 reg.gra[0x08] = 0xff;
623
624 reg.att[0x00] = 0x00;
625 reg.att[0x01] = 0x01;
626 reg.att[0x02] = 0x02;
627 reg.att[0x03] = 0x03;
628 reg.att[0x04] = 0x04;
629 reg.att[0x05] = 0x05;
630 reg.att[0x06] = 0x06;
631 reg.att[0x07] = 0x07;
632 reg.att[0x08] = 0x08;
633 reg.att[0x09] = 0x09;
634 reg.att[0x0a] = 0x0a;
635 reg.att[0x0b] = 0x0b;
636 reg.att[0x0c] = 0x0c;
637 reg.att[0x0d] = 0x0d;
638 reg.att[0x0e] = 0x0e;
639 reg.att[0x0f] = 0x0f;
640 reg.att[0x10] = 0x41;
641 reg.att[0x11] = 0x00;
642 reg.att[0x12] = 0x0f;
643 reg.att[0x13] = 0x00;
644 reg.att[0x14] = 0x00;
645
646 reg.seq[0x00] = 0x03;
647 reg.seq[0x01] = 0x01; /* fixme: clkdiv2? */
648 reg.seq[0x02] = 0x0f;
649 reg.seq[0x03] = 0x00;
650 reg.seq[0x04] = 0x0e;
651
652 reg.crt[0x00] = ht - 4;
653 reg.crt[0x01] = hd;
654 reg.crt[0x02] = hbs;
655 reg.crt[0x03] = 0x80 | (hbe & 0x1f);
656 reg.crt[0x04] = hs;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700657 reg.crt[0x05] = ((hbe & 0x20) << 2) | (he & 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 reg.crt[0x06] = vt;
659 reg.crt[0x07] = ((vs & 0x200) >> 2) |
660 ((vd & 0x200) >> 3) |
661 ((vt & 0x200) >> 4) | 0x10 |
662 ((vbs & 0x100) >> 5) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700663 ((vs & 0x100) >> 6) |
664 ((vd & 0x100) >> 7) |
665 ((vt & 0x100) >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 reg.crt[0x08] = 0x00;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700667 reg.crt[0x09] = 0x40 | ((vbs & 0x200) >> 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 reg.crt[0x0a] = 0x00;
669 reg.crt[0x0b] = 0x00;
670 reg.crt[0x0c] = 0x00;
671 reg.crt[0x0d] = 0x00;
672 reg.crt[0x0e] = 0x00;
673 reg.crt[0x0f] = 0x00;
674 reg.crt[0x10] = vs;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700675 reg.crt[0x11] = (ve & 0x0f) | 0x20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 reg.crt[0x12] = vd;
677 reg.crt[0x13] = wd;
678 reg.crt[0x14] = 0x00;
679 reg.crt[0x15] = vbs;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700680 reg.crt[0x16] = vbe + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 reg.crt[0x17] = 0xc3;
682 reg.crt[0x18] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700684 /* Banshee's nonvga stuff */
685 reg.ext[0x00] = (((ht & 0x100) >> 8) |
686 ((hd & 0x100) >> 6) |
687 ((hbs & 0x100) >> 4) |
688 ((hbe & 0x40) >> 1) |
689 ((hs & 0x100) >> 2) |
690 ((he & 0x20) << 2));
691 reg.ext[0x01] = (((vt & 0x400) >> 10) |
692 ((vd & 0x400) >> 8) |
693 ((vbs & 0x400) >> 6) |
694 ((vbe & 0x400) >> 4));
695
696 reg.vgainit0 = VGAINIT0_8BIT_DAC |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 VGAINIT0_EXT_ENABLE |
698 VGAINIT0_WAKEUP_3C3 |
699 VGAINIT0_ALT_READBACK |
700 VGAINIT0_EXTSHIFTOUT;
701 reg.vgainit1 = tdfx_inl(par, VGAINIT1) & 0x1fffff;
702
703 reg.cursloc = 0;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700704
705 reg.cursc0 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 reg.cursc1 = 0xffffff;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 reg.stride = info->var.xres * cpp;
709 reg.startaddr = par->baseline * reg.stride;
710 reg.srcbase = reg.startaddr;
711 reg.dstbase = reg.startaddr;
712
713 /* PLL settings */
714 freq = PICOS2KHZ(info->var.pixclock);
715
716 reg.dacmode &= ~DACMODE_2X;
717 reg.vidcfg &= ~VIDCFG_2X;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700718 if (freq > par->max_pixclock / 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 freq = freq > par->max_pixclock ? par->max_pixclock : freq;
720 reg.dacmode |= DACMODE_2X;
721 reg.vidcfg |= VIDCFG_2X;
722 }
723 reg.vidpll = do_calc_pll(freq, &fout);
724#if 0
725 reg.mempll = do_calc_pll(..., &fout);
726 reg.gfxpll = do_calc_pll(..., &fout);
727#endif
728
729 if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE) {
730 reg.screensize = info->var.xres | (info->var.yres << 13);
731 reg.vidcfg |= VIDCFG_HALF_MODE;
732 reg.crt[0x09] |= 0x80;
733 } else {
734 reg.screensize = info->var.xres | (info->var.yres << 12);
735 reg.vidcfg &= ~VIDCFG_HALF_MODE;
736 }
737 if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED)
738 reg.vidcfg |= VIDCFG_INTERLACE;
739 reg.miscinit0 = tdfx_inl(par, MISCINIT0);
740
741#if defined(__BIG_ENDIAN)
742 switch (info->var.bits_per_pixel) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700743 case 8:
744 case 24:
745 reg.miscinit0 &= ~(1 << 30);
746 reg.miscinit0 &= ~(1 << 31);
747 break;
748 case 16:
749 reg.miscinit0 |= (1 << 30);
750 reg.miscinit0 |= (1 << 31);
751 break;
752 case 32:
753 reg.miscinit0 |= (1 << 30);
754 reg.miscinit0 &= ~(1 << 31);
755 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700757#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 do_write_regs(info, &reg);
759
760 /* Now change fb_fix_screeninfo according to changes in par */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700761 info->fix.line_length =
762 info->var.xres * ((info->var.bits_per_pixel + 7) >> 3);
763 info->fix.visual = (info->var.bits_per_pixel == 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 ? FB_VISUAL_PSEUDOCOLOR
765 : FB_VISUAL_TRUECOLOR;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700766 DPRINTK("Graphics mode is now set at %dx%d depth %d\n",
767 info->var.xres, info->var.yres, info->var.bits_per_pixel);
768 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
771/* A handy macro shamelessly pinched from matroxfb */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700772#define CNVT_TOHW(val, width) ((((val)<<(width))+0x7FFF-(val))>>16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700774static int tdfxfb_setcolreg(unsigned regno, unsigned red, unsigned green,
775 unsigned blue, unsigned transp,
776 struct fb_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800778 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 u32 rgbcol;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700780
781 if (regno >= info->cmap.len || regno > 255)
782 return 1;
783
Krzysztof Helt254c9472007-10-16 01:28:46 -0700784 /* grayscale works only partially under directcolor */
785 if (info->var.grayscale) {
786 /* grayscale = 0.30*R + 0.59*G + 0.11*B */
787 red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
788 }
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 switch (info->fix.visual) {
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800791 case FB_VISUAL_PSEUDOCOLOR:
792 rgbcol =(((u32)red & 0xff00) << 8) |
793 (((u32)green & 0xff00) << 0) |
794 (((u32)blue & 0xff00) >> 8);
795 do_setpalentry(par, regno, rgbcol);
796 break;
797 /* Truecolor has no hardware color palettes. */
798 case FB_VISUAL_TRUECOLOR:
799 if (regno < 16) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700800 rgbcol = (CNVT_TOHW(red, info->var.red.length) <<
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800801 info->var.red.offset) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700802 (CNVT_TOHW(green, info->var.green.length) <<
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800803 info->var.green.offset) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700804 (CNVT_TOHW(blue, info->var.blue.length) <<
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800805 info->var.blue.offset) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700806 (CNVT_TOHW(transp, info->var.transp.length) <<
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800807 info->var.transp.offset);
808 par->palette[regno] = rgbcol;
809 }
810
811 break;
812 default:
813 DPRINTK("bad depth %u\n", info->var.bits_per_pixel);
814 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return 0;
818}
819
820/* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */
821static int tdfxfb_blank(int blank, struct fb_info *info)
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700822{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800823 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 u32 dacmode, state = 0, vgablank = 0;
825
826 dacmode = tdfx_inl(par, DACMODE);
827
828 switch (blank) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700829 case FB_BLANK_UNBLANK: /* Screen: On; HSync: On, VSync: On */
830 state = 0;
831 vgablank = 0;
832 break;
833 case FB_BLANK_NORMAL: /* Screen: Off; HSync: On, VSync: On */
834 state = 0;
835 vgablank = 1;
836 break;
837 case FB_BLANK_VSYNC_SUSPEND: /* Screen: Off; HSync: On, VSync: Off */
838 state = BIT(3);
839 vgablank = 1;
840 break;
841 case FB_BLANK_HSYNC_SUSPEND: /* Screen: Off; HSync: Off, VSync: On */
842 state = BIT(1);
843 vgablank = 1;
844 break;
845 case FB_BLANK_POWERDOWN: /* Screen: Off; HSync: Off, VSync: Off */
846 state = BIT(1) | BIT(3);
847 vgablank = 1;
848 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850
851 dacmode &= ~(BIT(1) | BIT(3));
852 dacmode |= state;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700853 banshee_make_room(par, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 tdfx_outl(par, DACMODE, dacmode);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700855 if (vgablank)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 vga_disable_video(par);
857 else
858 vga_enable_video(par);
859 return 0;
860}
861
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700862/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 * Set the starting position of the visible screen to var->yoffset
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700864 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865static int tdfxfb_pan_display(struct fb_var_screeninfo *var,
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700866 struct fb_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800868 struct tdfx_par *par = info->par;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700869 u32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 if (nopan || var->xoffset || (var->yoffset > var->yres_virtual))
872 return -EINVAL;
873 if ((var->yoffset + var->yres > var->yres_virtual && nowrap))
874 return -EINVAL;
875
876 addr = var->yoffset * info->fix.line_length;
877 banshee_make_room(par, 1);
878 tdfx_outl(par, VIDDESKSTART, addr);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 info->var.xoffset = var->xoffset;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700881 info->var.yoffset = var->yoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return 0;
883}
884
885#ifdef CONFIG_FB_3DFX_ACCEL
886/*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700887 * FillRect 2D command (solidfill or invert (via ROP_XOR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700889static void tdfxfb_fillrect(struct fb_info *info,
890 const struct fb_fillrect *rect)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800892 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 u32 bpp = info->var.bits_per_pixel;
894 u32 stride = info->fix.line_length;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700895 u32 fmt= stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 int tdfx_rop;
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700897 u32 dx = rect->dx;
898 u32 dy = rect->dy;
899 u32 dstbase = 0;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700900
901 if (rect->rop == ROP_COPY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 tdfx_rop = TDFX_ROP_COPY;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700903 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 tdfx_rop = TDFX_ROP_XOR;
905
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700906 /* asume always rect->height < 4096 */
907 if (dy + rect->height > 4095) {
908 dstbase = stride * dy;
909 dy = 0;
910 }
911 /* asume always rect->width < 4096 */
912 if (dx + rect->width > 4095) {
913 dstbase += dx * bpp >> 3;
914 dx = 0;
915 }
916 banshee_make_room(par, 6);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700917 tdfx_outl(par, DSTFORMAT, fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700919 tdfx_outl(par, COLORFORE, rect->color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 } else { /* FB_VISUAL_TRUECOLOR */
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800921 tdfx_outl(par, COLORFORE, par->palette[rect->color]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700923 tdfx_outl(par, COMMAND_2D, COMMAND_2D_FILLRECT | (tdfx_rop << 24));
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700924 tdfx_outl(par, DSTBASE, dstbase);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700925 tdfx_outl(par, DSTSIZE, rect->width | (rect->height << 16));
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700926 tdfx_outl(par, LAUNCH_2D, dx | (dy << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
929/*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700930 * Screen-to-Screen BitBlt 2D command (for the bmove fb op.)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700932static void tdfxfb_copyarea(struct fb_info *info,
933 const struct fb_copyarea *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800935 struct tdfx_par *par = info->par;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700936 u32 sx = area->sx, sy = area->sy, dx = area->dx, dy = area->dy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 u32 bpp = info->var.bits_per_pixel;
938 u32 stride = info->fix.line_length;
939 u32 blitcmd = COMMAND_2D_S2S_BITBLT | (TDFX_ROP_COPY << 24);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700940 u32 fmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700941 u32 dstbase = 0;
942 u32 srcbase = 0;
943
944 /* asume always area->height < 4096 */
945 if (sy + area->height > 4095) {
946 srcbase = stride * sy;
947 sy = 0;
948 }
949 /* asume always area->width < 4096 */
950 if (sx + area->width > 4095) {
951 srcbase += sx * bpp >> 3;
952 sx = 0;
953 }
954 /* asume always area->height < 4096 */
955 if (dy + area->height > 4095) {
956 dstbase = stride * dy;
957 dy = 0;
958 }
959 /* asume always area->width < 4096 */
960 if (dx + area->width > 4095) {
961 dstbase += dx * bpp >> 3;
962 dx = 0;
963 }
964
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (area->sx <= area->dx) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700967 //-X
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 blitcmd |= BIT(14);
969 sx += area->width - 1;
970 dx += area->width - 1;
971 }
972 if (area->sy <= area->dy) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700973 //-Y
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 blitcmd |= BIT(15);
975 sy += area->height - 1;
976 dy += area->height - 1;
977 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700978
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700979 banshee_make_room(par, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700981 tdfx_outl(par, SRCFORMAT, fmt);
982 tdfx_outl(par, DSTFORMAT, fmt);
983 tdfx_outl(par, COMMAND_2D, blitcmd);
984 tdfx_outl(par, DSTSIZE, area->width | (area->height << 16));
985 tdfx_outl(par, DSTXY, dx | (dy << 16));
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700986 tdfx_outl(par, SRCBASE, srcbase);
987 tdfx_outl(par, DSTBASE, dstbase);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700988 tdfx_outl(par, LAUNCH_2D, sx | (sy << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700991static void tdfxfb_imageblit(struct fb_info *info, const struct fb_image *image)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800993 struct tdfx_par *par = info->par;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700994 int size = image->height * ((image->width * image->depth + 7) >> 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 int fifo_free;
996 int i, stride = info->fix.line_length;
997 u32 bpp = info->var.bits_per_pixel;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700998 u32 dstfmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 u8 *chardata = (u8 *) image->data;
1000 u32 srcfmt;
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001001 u32 dx = image->dx;
1002 u32 dy = image->dy;
1003 u32 dstbase = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 if (image->depth != 1) {
1006 //banshee_make_room(par, 6 + ((size + 3) >> 2));
1007 //srcfmt = stride | ((bpp+((bpp==8) ? 0 : 8)) << 13) | 0x400000;
1008 cfb_imageblit(info, image);
1009 return;
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001010 }
1011 banshee_make_room(par, 9);
1012 switch (info->fix.visual) {
1013 case FB_VISUAL_PSEUDOCOLOR:
1014 tdfx_outl(par, COLORFORE, image->fg_color);
1015 tdfx_outl(par, COLORBACK, image->bg_color);
1016 break;
1017 case FB_VISUAL_TRUECOLOR:
1018 default:
1019 tdfx_outl(par, COLORFORE,
1020 par->palette[image->fg_color]);
1021 tdfx_outl(par, COLORBACK,
1022 par->palette[image->bg_color]);
1023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024#ifdef __BIG_ENDIAN
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001025 srcfmt = 0x400000 | BIT(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026#else
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001027 srcfmt = 0x400000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028#endif
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001029 /* asume always image->height < 4096 */
1030 if (dy + image->height > 4095) {
1031 dstbase = stride * dy;
1032 dy = 0;
1033 }
1034 /* asume always image->width < 4096 */
1035 if (dx + image->width > 4095) {
1036 dstbase += dx * bpp >> 3;
1037 dx = 0;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001040 tdfx_outl(par, DSTBASE, dstbase);
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001041 tdfx_outl(par, SRCXY, 0);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001042 tdfx_outl(par, DSTXY, dx | (dy << 16));
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001043 tdfx_outl(par, COMMAND_2D, COMMAND_2D_H2S_BITBLT | (TDFX_ROP_COPY << 24));
1044 tdfx_outl(par, SRCFORMAT, srcfmt);
1045 tdfx_outl(par, DSTFORMAT, dstfmt);
1046 tdfx_outl(par, DSTSIZE, image->width | (image->height << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 /* A count of how many free FIFO entries we've requested.
1049 * When this goes negative, we need to request more. */
1050 fifo_free = 0;
1051
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001052 /* Send four bytes at a time of data */
1053 for (i = (size >> 2); i > 0; i--) {
1054 if (--fifo_free < 0) {
1055 fifo_free = 31;
1056 banshee_make_room(par, fifo_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001058 tdfx_outl(par, LAUNCH_2D, *(u32*)chardata);
1059 chardata += 4;
1060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001062 /* Send the leftovers now */
1063 banshee_make_room(par, 3);
1064 i = size % 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 switch (i) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001066 case 0:
1067 break;
1068 case 1:
1069 tdfx_outl(par, LAUNCH_2D, *chardata);
1070 break;
1071 case 2:
1072 tdfx_outl(par, LAUNCH_2D, *(u16*)chardata);
1073 break;
1074 case 3:
1075 tdfx_outl(par, LAUNCH_2D,
1076 *(u16*)chardata | ((chardata[3]) << 24));
1077 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
1079}
1080#endif /* CONFIG_FB_3DFX_ACCEL */
1081
1082#ifdef TDFX_HARDWARE_CURSOR
1083static int tdfxfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
1084{
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001085 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 unsigned long flags;
1087
1088 /*
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001089 * If the cursor is not be changed this means either we want the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 * current cursor state (if enable is set) or we want to query what
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001091 * we can do with the cursor (if enable is not set)
1092 */
1093 if (!cursor->set)
1094 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 /* Too large of a cursor :-( */
1097 if (cursor->image.width > 64 || cursor->image.height > 64)
1098 return -ENXIO;
1099
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001100 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 * If we are going to be changing things we should disable
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001102 * the cursor first
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 */
1104 if (info->cursor.enable) {
1105 spin_lock_irqsave(&par->DAClock, flags);
1106 info->cursor.enable = 0;
1107 del_timer(&(par->hwcursor.timer));
1108 tdfx_outl(par, VIDPROCCFG, par->hwcursor.disable);
1109 spin_unlock_irqrestore(&par->DAClock, flags);
1110 }
1111
1112 /* Disable the Cursor */
1113 if ((cursor->set && FB_CUR_SETCUR) && !cursor->enable)
1114 return 0;
1115
1116 /* fix cursor color - XFree86 forgets to restore it properly */
1117 if (cursor->set && FB_CUR_SETCMAP) {
1118 struct fb_cmap cmap = cursor->image.cmap;
1119 unsigned long bg_color, fg_color;
1120
1121 cmap.len = 2; /* Voodoo 3+ only support 2 color cursors */
1122 fg_color = ((cmap.red[cmap.start] << 16) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001123 (cmap.green[cmap.start] << 8) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 (cmap.blue[cmap.start]));
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001125 bg_color = ((cmap.red[cmap.start + 1] << 16) |
1126 (cmap.green[cmap.start + 1] << 8) |
1127 (cmap.blue[cmap.start + 1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 fb_copy_cmap(&cmap, &info->cursor.image.cmap);
1129 spin_lock_irqsave(&par->DAClock, flags);
1130 banshee_make_room(par, 2);
1131 tdfx_outl(par, HWCURC0, bg_color);
1132 tdfx_outl(par, HWCURC1, fg_color);
1133 spin_unlock_irqrestore(&par->DAClock, flags);
1134 }
1135
1136 if (cursor->set && FB_CUR_SETPOS) {
1137 int x, y;
1138
1139 x = cursor->image.dx;
1140 y = cursor->image.dy;
1141 y -= info->var.yoffset;
1142 info->cursor.image.dx = x;
1143 info->cursor.image.dy = y;
1144 x += 63;
1145 y += 63;
1146 spin_lock_irqsave(&par->DAClock, flags);
1147 banshee_make_room(par, 1);
1148 tdfx_outl(par, HWCURLOC, (y << 16) + x);
1149 spin_unlock_irqrestore(&par->DAClock, flags);
1150 }
1151
1152 /* Not supported so we fake it */
1153 if (cursor->set && FB_CUR_SETHOT) {
1154 info->cursor.hot.x = cursor->hot.x;
1155 info->cursor.hot.y = cursor->hot.y;
1156 }
1157
1158 if (cursor->set && FB_CUR_SETSHAPE) {
1159 /*
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001160 * Voodoo 3 and above cards use 2 monochrome cursor patterns.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 * The reason is so the card can fetch 8 words at a time
1162 * and are stored on chip for use for the next 8 scanlines.
1163 * This reduces the number of times for access to draw the
1164 * cursor for each screen refresh.
1165 * Each pattern is a bitmap of 64 bit wide and 64 bit high
1166 * (total of 8192 bits or 1024 Kbytes). The two patterns are
1167 * stored in such a way that pattern 0 always resides in the
1168 * lower half (least significant 64 bits) of a 128 bit word
1169 * and pattern 1 the upper half. If you examine the data of
1170 * the cursor image the graphics card uses then from the
1171 * begining you see line one of pattern 0, line one of
1172 * pattern 1, line two of pattern 0, line two of pattern 1,
1173 * etc etc. The linear stride for the cursor is always 16 bytes
1174 * (128 bits) which is the maximum cursor width times two for
1175 * the two monochrome patterns.
1176 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001177 u8 *cursorbase = (u8 *)info->cursor.image.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 char *bitmap = (char *)cursor->image.data;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001179 char *mask = (char *)cursor->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 int i, j, k, h = 0;
1181
1182 for (i = 0; i < 64; i++) {
1183 if (i < cursor->image.height) {
1184 j = (cursor->image.width + 7) >> 3;
1185 k = 8 - j;
1186
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001187 for (; j > 0; j--) {
1188 /* Pattern 0. Copy the cursor bitmap to it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 fb_writeb(*bitmap, cursorbase + h);
1190 bitmap++;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001191 /* Pattern 1. Copy the cursor mask to it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 fb_writeb(*mask, cursorbase + h + 8);
1193 mask++;
1194 h++;
1195 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001196 for (; k > 0; k--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 fb_writeb(0, cursorbase + h);
1198 fb_writeb(~0, cursorbase + h + 8);
1199 h++;
1200 }
1201 } else {
1202 fb_writel(0, cursorbase + h);
1203 fb_writel(0, cursorbase + h + 4);
1204 fb_writel(~0, cursorbase + h + 8);
1205 fb_writel(~0, cursorbase + h + 12);
1206 h += 16;
1207 }
1208 }
1209 }
1210 /* Turn the cursor on */
1211 cursor->enable = 1;
1212 info->cursor = *cursor;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001213 mod_timer(&par->hwcursor.timer, jiffies + HZ / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 spin_lock_irqsave(&par->DAClock, flags);
1215 banshee_make_room(par, 1);
1216 tdfx_outl(par, VIDPROCCFG, par->hwcursor.enable);
1217 spin_unlock_irqrestore(&par->DAClock, flags);
1218 return 0;
1219}
1220#endif
1221
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001222static struct fb_ops tdfxfb_ops = {
1223 .owner = THIS_MODULE,
1224 .fb_check_var = tdfxfb_check_var,
1225 .fb_set_par = tdfxfb_set_par,
1226 .fb_setcolreg = tdfxfb_setcolreg,
1227 .fb_blank = tdfxfb_blank,
1228 .fb_pan_display = tdfxfb_pan_display,
1229 .fb_sync = banshee_wait_idle,
1230#ifdef CONFIG_FB_3DFX_ACCEL
1231 .fb_fillrect = tdfxfb_fillrect,
1232 .fb_copyarea = tdfxfb_copyarea,
1233 .fb_imageblit = tdfxfb_imageblit,
1234#else
1235 .fb_fillrect = cfb_fillrect,
1236 .fb_copyarea = cfb_copyarea,
1237 .fb_imageblit = cfb_imageblit,
1238#endif
1239};
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241/**
1242 * tdfxfb_probe - Device Initializiation
1243 *
1244 * @pdev: PCI Device to initialize
1245 * @id: PCI Device ID
1246 *
1247 * Initializes and allocates resources for PCI device @pdev.
1248 *
1249 */
1250static int __devinit tdfxfb_probe(struct pci_dev *pdev,
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001251 const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
1253 struct tdfx_par *default_par;
1254 struct fb_info *info;
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001255 int err, lpitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 if ((err = pci_enable_device(pdev))) {
1258 printk(KERN_WARNING "tdfxfb: Can't enable pdev: %d\n", err);
1259 return err;
1260 }
1261
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001262 info = framebuffer_alloc(sizeof(struct tdfx_par), &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001264 if (!info)
1265 return -ENOMEM;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 default_par = info->par;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 /* Configure the default fb_fix_screeninfo first */
1270 switch (pdev->device) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001271 case PCI_DEVICE_ID_3DFX_BANSHEE:
1272 strcat(tdfx_fix.id, " Banshee");
1273 default_par->max_pixclock = BANSHEE_MAX_PIXCLOCK;
1274 break;
1275 case PCI_DEVICE_ID_3DFX_VOODOO3:
1276 strcat(tdfx_fix.id, " Voodoo3");
1277 default_par->max_pixclock = VOODOO3_MAX_PIXCLOCK;
1278 break;
1279 case PCI_DEVICE_ID_3DFX_VOODOO5:
1280 strcat(tdfx_fix.id, " Voodoo5");
1281 default_par->max_pixclock = VOODOO5_MAX_PIXCLOCK;
1282 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284
1285 tdfx_fix.mmio_start = pci_resource_start(pdev, 0);
1286 tdfx_fix.mmio_len = pci_resource_len(pdev, 0);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001287 if (!request_mem_region(tdfx_fix.mmio_start, tdfx_fix.mmio_len,
1288 "tdfx regbase")) {
1289 printk(KERN_WARNING "tdfxfb: Can't reserve regbase\n");
1290 goto out_err;
1291 }
1292
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001293 default_par->regbase_virt =
1294 ioremap_nocache(tdfx_fix.mmio_start, tdfx_fix.mmio_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (!default_par->regbase_virt) {
1296 printk("fb: Can't remap %s register area.\n", tdfx_fix.id);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001297 goto out_err_regbase;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 tdfx_fix.smem_start = pci_resource_start(pdev, 1);
1301 if (!(tdfx_fix.smem_len = do_lfb_size(default_par, pdev->device))) {
1302 printk("fb: Can't count %s memory.\n", tdfx_fix.id);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001303 goto out_err_regbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
1305
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001306 if (!request_mem_region(tdfx_fix.smem_start,
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001307 pci_resource_len(pdev, 1), "tdfx smem")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 printk(KERN_WARNING "tdfxfb: Can't reserve smem\n");
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001309 goto out_err_regbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
1311
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001312 info->screen_base = ioremap_nocache(tdfx_fix.smem_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 tdfx_fix.smem_len);
1314 if (!info->screen_base) {
1315 printk("fb: Can't remap %s framebuffer.\n", tdfx_fix.id);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001316 goto out_err_screenbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
1318
1319 default_par->iobase = pci_resource_start(pdev, 2);
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (!request_region(pci_resource_start(pdev, 2),
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001322 pci_resource_len(pdev, 2), "tdfx iobase")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 printk(KERN_WARNING "tdfxfb: Can't reserve iobase\n");
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001324 goto out_err_screenbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
1326
1327 printk("fb: %s memory = %dK\n", tdfx_fix.id, tdfx_fix.smem_len >> 10);
1328
1329 tdfx_fix.ypanstep = nopan ? 0 : 1;
1330 tdfx_fix.ywrapstep = nowrap ? 0 : 1;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 info->fbops = &tdfxfb_ops;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001333 info->fix = tdfx_fix;
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001334 info->pseudo_palette = default_par->palette;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
1336#ifdef CONFIG_FB_3DFX_ACCEL
1337 info->flags |= FBINFO_HWACCEL_FILLRECT |
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001338 FBINFO_HWACCEL_COPYAREA |
1339 FBINFO_HWACCEL_IMAGEBLIT |
1340 FBINFO_READS_FAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341#endif
1342
1343 if (!mode_option)
1344 mode_option = "640x480@60";
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001345
1346 err = fb_find_mode(&info->var, info, mode_option, NULL, 0, NULL, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (!err || err == 4)
1348 info->var = tdfx_var;
1349
1350 /* maximize virtual vertical length */
1351 lpitch = info->var.xres_virtual * ((info->var.bits_per_pixel + 7) >> 3);
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001352 info->var.yres_virtual = info->fix.smem_len / lpitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (info->var.yres_virtual < info->var.yres)
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001354 goto out_err_iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
1357 printk(KERN_WARNING "tdfxfb: Can't allocate color map\n");
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001358 goto out_err_iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 }
1360
1361 if (register_framebuffer(info) < 0) {
1362 printk("tdfxfb: can't register framebuffer\n");
1363 fb_dealloc_cmap(&info->cmap);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001364 goto out_err_iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
1366 /*
1367 * Our driver data
1368 */
1369 pci_set_drvdata(pdev, info);
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001370 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001372out_err_iobase:
1373 release_mem_region(pci_resource_start(pdev, 2),
1374 pci_resource_len(pdev, 2));
1375out_err_screenbase:
1376 if (info->screen_base)
1377 iounmap(info->screen_base);
1378 release_mem_region(tdfx_fix.smem_start, pci_resource_len(pdev, 1));
1379out_err_regbase:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 /*
1381 * Cleanup after anything that was remapped/allocated.
1382 */
1383 if (default_par->regbase_virt)
1384 iounmap(default_par->regbase_virt);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001385 release_mem_region(tdfx_fix.mmio_start, tdfx_fix.mmio_len);
1386out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 framebuffer_release(info);
1388 return -ENXIO;
1389}
1390
1391#ifndef MODULE
Adrian Bunka0aa7d02006-01-09 20:54:04 -08001392static void tdfxfb_setup(char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001394 char *this_opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 if (!options || !*options)
1397 return;
1398
1399 while ((this_opt = strsep(&options, ",")) != NULL) {
1400 if (!*this_opt)
1401 continue;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001402 if (!strcmp(this_opt, "nopan")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 nopan = 1;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001404 } else if (!strcmp(this_opt, "nowrap")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 nowrap = 1;
1406 } else {
1407 mode_option = this_opt;
1408 }
1409 }
1410}
1411#endif
1412
1413/**
1414 * tdfxfb_remove - Device removal
1415 *
1416 * @pdev: PCI Device to cleanup
1417 *
1418 * Releases all resources allocated during the course of the driver's
1419 * lifetime for the PCI device @pdev.
1420 *
1421 */
1422static void __devexit tdfxfb_remove(struct pci_dev *pdev)
1423{
1424 struct fb_info *info = pci_get_drvdata(pdev);
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001425 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 unregister_framebuffer(info);
1428 iounmap(par->regbase_virt);
1429 iounmap(info->screen_base);
1430
1431 /* Clean up after reserved regions */
1432 release_region(pci_resource_start(pdev, 2),
1433 pci_resource_len(pdev, 2));
1434 release_mem_region(pci_resource_start(pdev, 1),
1435 pci_resource_len(pdev, 1));
1436 release_mem_region(pci_resource_start(pdev, 0),
1437 pci_resource_len(pdev, 0));
1438 pci_set_drvdata(pdev, NULL);
1439 framebuffer_release(info);
1440}
1441
1442static int __init tdfxfb_init(void)
1443{
1444#ifndef MODULE
1445 char *option = NULL;
1446
1447 if (fb_get_options("tdfxfb", &option))
1448 return -ENODEV;
1449
1450 tdfxfb_setup(option);
1451#endif
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001452 return pci_register_driver(&tdfxfb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
1455static void __exit tdfxfb_exit(void)
1456{
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001457 pci_unregister_driver(&tdfxfb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
1459
1460MODULE_AUTHOR("Hannu Mallat <hmallat@cc.hut.fi>");
1461MODULE_DESCRIPTION("3Dfx framebuffer device driver");
1462MODULE_LICENSE("GPL");
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464module_init(tdfxfb_init);
1465module_exit(tdfxfb_exit);