blob: ea9f19d2559799155c0eb330d395f8f7f3786f92 [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 *
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -07007 * Copyright © 1999 Hannu Mallat
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 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 *
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -070045 * 0.1.4 (released 2002-05-28) ported over to new fbdev api by James Simmons
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 *
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -070047 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 *
58 */
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/module.h>
61#include <linux/kernel.h>
62#include <linux/errno.h>
63#include <linux/string.h>
64#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/fb.h>
67#include <linux/init.h>
68#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71#include <video/tdfx.h>
72
Harvey Harrison5ae12172008-04-28 02:15:47 -070073#define DPRINTK(a, b...) pr_debug("fb: %s: " a, __func__ , ## b)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Krzysztof Helt0960bd32007-10-16 01:28:49 -070075#ifdef CONFIG_MTRR
76#include <asm/mtrr.h>
77#else
78/* duplicate asm/mtrr.h defines to work on archs without mtrr */
79#define MTRR_TYPE_WRCOMB 1
80
81static inline int mtrr_add(unsigned long base, unsigned long size,
82 unsigned int type, char increment)
83{
84 return -ENODEV;
85}
86static inline int mtrr_del(int reg, unsigned long base,
87 unsigned long size)
88{
89 return -ENODEV;
90}
91#endif
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#define BANSHEE_MAX_PIXCLOCK 270000
94#define VOODOO3_MAX_PIXCLOCK 300000
95#define VOODOO5_MAX_PIXCLOCK 350000
96
97static struct fb_fix_screeninfo tdfx_fix __devinitdata = {
98 .id = "3Dfx",
99 .type = FB_TYPE_PACKED_PIXELS,
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700100 .visual = FB_VISUAL_PSEUDOCOLOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 .ypanstep = 1,
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700102 .ywrapstep = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 .accel = FB_ACCEL_3DFX_BANSHEE
104};
105
106static struct fb_var_screeninfo tdfx_var __devinitdata = {
107 /* "640x480, 8 bpp @ 60 Hz */
108 .xres = 640,
109 .yres = 480,
110 .xres_virtual = 640,
111 .yres_virtual = 1024,
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700112 .bits_per_pixel = 8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 .red = {0, 8, 0},
114 .blue = {0, 8, 0},
115 .green = {0, 8, 0},
116 .activate = FB_ACTIVATE_NOW,
117 .height = -1,
118 .width = -1,
119 .accel_flags = FB_ACCELF_TEXT,
120 .pixclock = 39722,
121 .left_margin = 40,
122 .right_margin = 24,
123 .upper_margin = 32,
124 .lower_margin = 11,
125 .hsync_len = 96,
126 .vsync_len = 2,
127 .vmode = FB_VMODE_NONINTERLACED
128};
129
130/*
131 * PCI driver prototypes
132 */
133static int __devinit tdfxfb_probe(struct pci_dev *pdev,
134 const struct pci_device_id *id);
135static void __devexit tdfxfb_remove(struct pci_dev *pdev);
136
137static struct pci_device_id tdfxfb_id_table[] = {
138 { PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_BANSHEE,
139 PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
140 0xff0000, 0 },
141 { PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_VOODOO3,
142 PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
143 0xff0000, 0 },
144 { PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_VOODOO5,
145 PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
146 0xff0000, 0 },
147 { 0, }
148};
149
150static struct pci_driver tdfxfb_driver = {
151 .name = "tdfxfb",
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700152 .id_table = tdfxfb_id_table,
153 .probe = tdfxfb_probe,
154 .remove = __devexit_p(tdfxfb_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155};
156
157MODULE_DEVICE_TABLE(pci, tdfxfb_id_table);
158
159/*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700160 * Driver data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 */
Krzysztof Helt90b0f082007-10-16 01:28:48 -0700162static int nopan;
163static int nowrap = 1; /* not implemented (yet) */
164static int hwcursor = 1;
Krzysztof Helt0960bd32007-10-16 01:28:49 -0700165static char *mode_option __devinitdata;
166/* mtrr option */
167static int nomtrr __devinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700169/* -------------------------------------------------------------------------
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -0700170 * Hardware-specific funcions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * ------------------------------------------------------------------------- */
172
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700173static inline u8 vga_inb(struct tdfx_par *par, u32 reg)
174{
175 return inb(par->iobase + reg - 0x300);
176}
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700177
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700178static inline void vga_outb(struct tdfx_par *par, u32 reg, u8 val)
179{
180 outb(val, par->iobase + reg - 0x300);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
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 Heltf67fd7c2007-10-16 01:29:26 -0700271 while ((tdfx_inl(par, STATUS) & 0x1f) < size - 1)
272 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -0700274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275static int banshee_wait_idle(struct fb_info *info)
276{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800277 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 int i = 0;
279
280 banshee_make_room(par, 1);
281 tdfx_outl(par, COMMAND_3D, COMMAND_3D_NOP);
282
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700283 do {
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -0700284 if ((tdfx_inl(par, STATUS) & STATUS_BUSY) == 0)
285 i++;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700286 } while (i < 3);
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return 0;
289}
290
291/*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700292 * Set the color of a palette entry in 8bpp mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 */
294static inline void do_setpalentry(struct tdfx_par *par, unsigned regno, u32 c)
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -0700295{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 banshee_make_room(par, 2);
297 tdfx_outl(par, DACADDR, regno);
Krzysztof Helt254c9472007-10-16 01:28:46 -0700298 /* read after write makes it working */
299 tdfx_inl(par, DACADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 tdfx_outl(par, DACDATA, c);
301}
302
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700303static u32 do_calc_pll(int freq, int *freq_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700305 int m, n, k, best_m, best_n, best_k, best_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 int fref = 14318;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 best_error = freq;
309 best_n = best_m = best_k = 0;
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700310
311 for (k = 3; k >= 0; k--) {
312 for (m = 63; m >= 0; m--) {
313 /*
314 * Estimate value of n that produces target frequency
315 * with current m and k
316 */
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700317 int n_estimated = ((freq * (m + 2) << k) / fref) - 2;
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700318
319 /* Search neighborhood of estimated n */
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700320 for (n = max(0, n_estimated);
321 n <= min(255, n_estimated + 1);
322 n++) {
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700323 /*
324 * Calculate PLL freqency with current m, k and
325 * estimated n
326 */
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700327 int f = (fref * (n + 2) / (m + 2)) >> k;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700328 int error = abs(f - freq);
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700329
330 /*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700331 * If this is the closest we've come to the
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700332 * target frequency then remember n, m and k
333 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700334 if (error < best_error) {
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700335 best_error = error;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700336 best_n = n;
337 best_m = m;
338 best_k = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340 }
341 }
342 }
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 n = best_n;
345 m = best_m;
346 k = best_k;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700347 *freq_out = (fref * (n + 2) / (m + 2)) >> k;
Richard Drummond0fbe9ca2005-05-01 08:59:24 -0700348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return (n << 8) | (m << 2) | k;
350}
351
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700352static void do_write_regs(struct fb_info *info, struct banshee_reg *reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800354 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 int i;
356
357 banshee_wait_idle(info);
358
359 tdfx_outl(par, MISCINIT1, tdfx_inl(par, MISCINIT1) | 0x01);
360
361 crt_outb(par, 0x11, crt_inb(par, 0x11) & 0x7f); /* CRT unprotect */
362
363 banshee_make_room(par, 3);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700364 tdfx_outl(par, VGAINIT1, reg->vgainit1 & 0x001FFFFF);
365 tdfx_outl(par, VIDPROCCFG, reg->vidcfg & ~0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366#if 0
367 tdfx_outl(par, PLLCTRL1, reg->mempll);
368 tdfx_outl(par, PLLCTRL2, reg->gfxpll);
369#endif
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700370 tdfx_outl(par, PLLCTRL0, reg->vidpll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 vga_outb(par, MISC_W, reg->misc[0x00] | 0x01);
373
374 for (i = 0; i < 5; i++)
375 seq_outb(par, i, reg->seq[i]);
376
377 for (i = 0; i < 25; i++)
378 crt_outb(par, i, reg->crt[i]);
379
380 for (i = 0; i < 9; i++)
381 gra_outb(par, i, reg->gra[i]);
382
383 for (i = 0; i < 21; i++)
384 att_outb(par, i, reg->att[i]);
385
386 crt_outb(par, 0x1a, reg->ext[0]);
387 crt_outb(par, 0x1b, reg->ext[1]);
388
389 vga_enable_palette(par);
390 vga_enable_video(par);
391
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700392 banshee_make_room(par, 9);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700393 tdfx_outl(par, VGAINIT0, reg->vgainit0);
394 tdfx_outl(par, DACMODE, reg->dacmode);
395 tdfx_outl(par, VIDDESKSTRIDE, reg->stride);
Krzysztof Helt90b0f082007-10-16 01:28:48 -0700396 tdfx_outl(par, HWCURPATADDR, reg->curspataddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700398 tdfx_outl(par, VIDSCREENSIZE, reg->screensize);
399 tdfx_outl(par, VIDDESKSTART, reg->startaddr);
400 tdfx_outl(par, VIDPROCCFG, reg->vidcfg);
401 tdfx_outl(par, VGAINIT1, reg->vgainit1);
402 tdfx_outl(par, MISCINIT0, reg->miscinit0);
403
404 banshee_make_room(par, 8);
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700405 tdfx_outl(par, SRCBASE, reg->startaddr);
406 tdfx_outl(par, DSTBASE, reg->startaddr);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700407 tdfx_outl(par, COMMANDEXTRA_2D, 0);
408 tdfx_outl(par, CLIP0MIN, 0);
409 tdfx_outl(par, CLIP0MAX, 0x0fff0fff);
410 tdfx_outl(par, CLIP1MIN, 0);
411 tdfx_outl(par, CLIP1MAX, 0x0fff0fff);
412 tdfx_outl(par, SRCXY, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 banshee_wait_idle(info);
415}
416
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700417static unsigned long do_lfb_size(struct tdfx_par *par, unsigned short dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700419 u32 draminit0 = tdfx_inl(par, DRAMINIT0);
420 u32 draminit1 = tdfx_inl(par, DRAMINIT1);
Richard Drummond333f9812005-05-01 08:59:25 -0700421 u32 miscinit1;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700422 int num_chips = (draminit0 & DRAMINIT0_SGRAM_NUM) ? 8 : 4;
Richard Drummond333f9812005-05-01 08:59:25 -0700423 int chip_size; /* in MB */
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700424 int has_sgram = draminit1 & DRAMINIT1_MEM_SDRAM;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700425
Richard Drummond333f9812005-05-01 08:59:25 -0700426 if (dev_id < PCI_DEVICE_ID_3DFX_VOODOO5) {
427 /* Banshee/Voodoo3 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700428 chip_size = 2;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700429 if (has_sgram && (draminit0 & DRAMINIT0_SGRAM_TYPE))
430 chip_size = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 } else {
432 /* Voodoo4/5 */
Richard Drummond333f9812005-05-01 08:59:25 -0700433 has_sgram = 0;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700434 chip_size = draminit0 & DRAMINIT0_SGRAM_TYPE_MASK;
435 chip_size = 1 << (chip_size >> DRAMINIT0_SGRAM_TYPE_SHIFT);
Richard Drummond333f9812005-05-01 08:59:25 -0700436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Richard Drummond333f9812005-05-01 08:59:25 -0700438 /* disable block writes for SDRAM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 miscinit1 = tdfx_inl(par, MISCINIT1);
Richard Drummond333f9812005-05-01 08:59:25 -0700440 miscinit1 |= has_sgram ? 0 : MISCINIT1_2DBLOCK_DIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 miscinit1 |= MISCINIT1_CLUT_INV;
442
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700443 banshee_make_room(par, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 tdfx_outl(par, MISCINIT1, miscinit1);
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700445 return num_chips * chip_size * 1024l * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
448/* ------------------------------------------------------------------------- */
449
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700450static int tdfxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800452 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 u32 lpitch;
454
455 if (var->bits_per_pixel != 8 && var->bits_per_pixel != 16 &&
456 var->bits_per_pixel != 24 && var->bits_per_pixel != 32) {
457 DPRINTK("depth not supported: %u\n", var->bits_per_pixel);
458 return -EINVAL;
459 }
460
461 if (var->xres != var->xres_virtual)
462 var->xres_virtual = var->xres;
463
464 if (var->yres > var->yres_virtual)
465 var->yres_virtual = var->yres;
466
467 if (var->xoffset) {
468 DPRINTK("xoffset not supported\n");
469 return -EINVAL;
470 }
Krzysztof Helt90b0f082007-10-16 01:28:48 -0700471 var->yoffset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -0700473 /*
474 * Banshee doesn't support interlace, but Voodoo4/5 and probably
475 * Voodoo3 do.
476 * no direct information about device id now?
477 * use max_pixclock for this...
478 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (((var->vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) &&
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700480 (par->max_pixclock < VOODOO3_MAX_PIXCLOCK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 DPRINTK("interlace not supported\n");
482 return -EINVAL;
483 }
484
485 var->xres = (var->xres + 15) & ~15; /* could sometimes be 8 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700486 lpitch = var->xres * ((var->bits_per_pixel + 7) >> 3);
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (var->xres < 320 || var->xres > 2048) {
489 DPRINTK("width not supported: %u\n", var->xres);
490 return -EINVAL;
491 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (var->yres < 200 || var->yres > 2048) {
494 DPRINTK("height not supported: %u\n", var->yres);
495 return -EINVAL;
496 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (lpitch * var->yres_virtual > info->fix.smem_len) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700499 var->yres_virtual = info->fix.smem_len / lpitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (var->yres_virtual < var->yres) {
501 DPRINTK("no memory for screen (%ux%ux%u)\n",
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700502 var->xres, var->yres_virtual,
503 var->bits_per_pixel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return -EINVAL;
505 }
506 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (PICOS2KHZ(var->pixclock) > par->max_pixclock) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700509 DPRINTK("pixclock too high (%ldKHz)\n",
510 PICOS2KHZ(var->pixclock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return -EINVAL;
512 }
513
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700514 var->transp.offset = 0;
515 var->transp.length = 0;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700516 switch (var->bits_per_pixel) {
517 case 8:
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -0700518 var->red.length = 8;
519 var->red.offset = 0;
520 var->green = var->red;
521 var->blue = var->red;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700522 break;
523 case 16:
524 var->red.offset = 11;
525 var->red.length = 5;
526 var->green.offset = 5;
527 var->green.length = 6;
528 var->blue.offset = 0;
529 var->blue.length = 5;
530 break;
531 case 32:
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700532 var->transp.offset = 24;
533 var->transp.length = 8;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700534 case 24:
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700535 var->red.offset = 16;
536 var->green.offset = 8;
537 var->blue.offset = 0;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700538 var->red.length = var->green.length = var->blue.length = 8;
539 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -0700541 var->width = -1;
542 var->height = -1;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 var->accel_flags = FB_ACCELF_TEXT;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700545
546 DPRINTK("Checking graphics mode at %dx%d depth %d\n",
547 var->xres, var->yres, var->bits_per_pixel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return 0;
549}
550
551static int tdfxfb_set_par(struct fb_info *info)
552{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800553 struct tdfx_par *par = info->par;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700554 u32 hdispend = info->var.xres;
555 u32 hsyncsta = hdispend + info->var.right_margin;
556 u32 hsyncend = hsyncsta + info->var.hsync_len;
557 u32 htotal = hsyncend + info->var.left_margin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 u32 hd, hs, he, ht, hbs, hbe;
559 u32 vd, vs, ve, vt, vbs, vbe;
560 struct banshee_reg reg;
561 int fout, freq;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700562 u32 wd;
563 u32 cpp = (info->var.bits_per_pixel + 7) >> 3;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 memset(&reg, 0, sizeof(reg));
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700566
567 reg.vidcfg = VIDCFG_VIDPROC_ENABLE | VIDCFG_DESK_ENABLE |
568 VIDCFG_CURS_X11 |
569 ((cpp - 1) << VIDCFG_PIXFMT_SHIFT) |
570 (cpp != 1 ? VIDCFG_CLUT_BYPASS : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 /* PLL settings */
573 freq = PICOS2KHZ(info->var.pixclock);
574
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700575 reg.vidcfg &= ~VIDCFG_2X;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700577 if (freq > par->max_pixclock / 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 freq = freq > par->max_pixclock ? par->max_pixclock : freq;
579 reg.dacmode |= DACMODE_2X;
580 reg.vidcfg |= VIDCFG_2X;
581 hdispend >>= 1;
582 hsyncsta >>= 1;
583 hsyncend >>= 1;
584 htotal >>= 1;
585 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700586
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -0700587 wd = (hdispend >> 3) - 1;
588 hd = wd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 hs = (hsyncsta >> 3) - 1;
590 he = (hsyncend >> 3) - 1;
591 ht = (htotal >> 3) - 1;
592 hbs = hd;
593 hbe = ht;
594
595 if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE) {
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -0700596 vd = (info->var.yres << 1) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 vs = vd + (info->var.lower_margin << 1);
598 ve = vs + (info->var.vsync_len << 1);
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -0700599 vt = ve + (info->var.upper_margin << 1) - 1;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700600 reg.screensize = info->var.xres | (info->var.yres << 13);
601 reg.vidcfg |= VIDCFG_HALF_MODE;
602 reg.crt[0x09] = 0x80;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 } else {
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -0700604 vd = info->var.yres - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 vs = vd + info->var.lower_margin;
606 ve = vs + info->var.vsync_len;
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -0700607 vt = ve + info->var.upper_margin - 1;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700608 reg.screensize = info->var.xres | (info->var.yres << 12);
609 reg.vidcfg &= ~VIDCFG_HALF_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -0700611 vbs = vd;
612 vbe = vt;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /* this is all pretty standard VGA register stuffing */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700615 reg.misc[0x00] = 0x0f |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 (info->var.xres < 400 ? 0xa0 :
617 info->var.xres < 480 ? 0x60 :
618 info->var.xres < 768 ? 0xe0 : 0x20);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 reg.gra[0x05] = 0x40;
621 reg.gra[0x06] = 0x05;
622 reg.gra[0x07] = 0x0f;
623 reg.gra[0x08] = 0xff;
624
625 reg.att[0x00] = 0x00;
626 reg.att[0x01] = 0x01;
627 reg.att[0x02] = 0x02;
628 reg.att[0x03] = 0x03;
629 reg.att[0x04] = 0x04;
630 reg.att[0x05] = 0x05;
631 reg.att[0x06] = 0x06;
632 reg.att[0x07] = 0x07;
633 reg.att[0x08] = 0x08;
634 reg.att[0x09] = 0x09;
635 reg.att[0x0a] = 0x0a;
636 reg.att[0x0b] = 0x0b;
637 reg.att[0x0c] = 0x0c;
638 reg.att[0x0d] = 0x0d;
639 reg.att[0x0e] = 0x0e;
640 reg.att[0x0f] = 0x0f;
641 reg.att[0x10] = 0x41;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 reg.att[0x12] = 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 reg.seq[0x00] = 0x03;
645 reg.seq[0x01] = 0x01; /* fixme: clkdiv2? */
646 reg.seq[0x02] = 0x0f;
647 reg.seq[0x03] = 0x00;
648 reg.seq[0x04] = 0x0e;
649
650 reg.crt[0x00] = ht - 4;
651 reg.crt[0x01] = hd;
652 reg.crt[0x02] = hbs;
653 reg.crt[0x03] = 0x80 | (hbe & 0x1f);
654 reg.crt[0x04] = hs;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700655 reg.crt[0x05] = ((hbe & 0x20) << 2) | (he & 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 reg.crt[0x06] = vt;
657 reg.crt[0x07] = ((vs & 0x200) >> 2) |
658 ((vd & 0x200) >> 3) |
659 ((vt & 0x200) >> 4) | 0x10 |
660 ((vbs & 0x100) >> 5) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700661 ((vs & 0x100) >> 6) |
662 ((vd & 0x100) >> 7) |
663 ((vt & 0x100) >> 8);
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700664 reg.crt[0x09] |= 0x40 | ((vbs & 0x200) >> 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 reg.crt[0x10] = vs;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700666 reg.crt[0x11] = (ve & 0x0f) | 0x20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 reg.crt[0x12] = vd;
668 reg.crt[0x13] = wd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 reg.crt[0x15] = vbs;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700670 reg.crt[0x16] = vbe + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 reg.crt[0x17] = 0xc3;
672 reg.crt[0x18] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700674 /* Banshee's nonvga stuff */
675 reg.ext[0x00] = (((ht & 0x100) >> 8) |
676 ((hd & 0x100) >> 6) |
677 ((hbs & 0x100) >> 4) |
678 ((hbe & 0x40) >> 1) |
679 ((hs & 0x100) >> 2) |
680 ((he & 0x20) << 2));
681 reg.ext[0x01] = (((vt & 0x400) >> 10) |
682 ((vd & 0x400) >> 8) |
683 ((vbs & 0x400) >> 6) |
684 ((vbe & 0x400) >> 4));
685
686 reg.vgainit0 = VGAINIT0_8BIT_DAC |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 VGAINIT0_EXT_ENABLE |
688 VGAINIT0_WAKEUP_3C3 |
689 VGAINIT0_ALT_READBACK |
690 VGAINIT0_EXTSHIFTOUT;
691 reg.vgainit1 = tdfx_inl(par, VGAINIT1) & 0x1fffff;
692
Krzysztof Helt90b0f082007-10-16 01:28:48 -0700693 if (hwcursor)
694 reg.curspataddr = info->fix.smem_len;
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 reg.cursloc = 0;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700697
698 reg.cursc0 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 reg.cursc1 = 0xffffff;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 reg.stride = info->var.xres * cpp;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700702 reg.startaddr = info->var.yoffset * reg.stride
703 + info->var.xoffset * cpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 reg.vidpll = do_calc_pll(freq, &fout);
706#if 0
707 reg.mempll = do_calc_pll(..., &fout);
708 reg.gfxpll = do_calc_pll(..., &fout);
709#endif
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED)
712 reg.vidcfg |= VIDCFG_INTERLACE;
713 reg.miscinit0 = tdfx_inl(par, MISCINIT0);
714
715#if defined(__BIG_ENDIAN)
716 switch (info->var.bits_per_pixel) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700717 case 8:
718 case 24:
719 reg.miscinit0 &= ~(1 << 30);
720 reg.miscinit0 &= ~(1 << 31);
721 break;
722 case 16:
723 reg.miscinit0 |= (1 << 30);
724 reg.miscinit0 |= (1 << 31);
725 break;
726 case 32:
727 reg.miscinit0 |= (1 << 30);
728 reg.miscinit0 &= ~(1 << 31);
729 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700731#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 do_write_regs(info, &reg);
733
734 /* Now change fb_fix_screeninfo according to changes in par */
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700735 info->fix.line_length = reg.stride;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700736 info->fix.visual = (info->var.bits_per_pixel == 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 ? FB_VISUAL_PSEUDOCOLOR
738 : FB_VISUAL_TRUECOLOR;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700739 DPRINTK("Graphics mode is now set at %dx%d depth %d\n",
740 info->var.xres, info->var.yres, info->var.bits_per_pixel);
741 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
744/* A handy macro shamelessly pinched from matroxfb */
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -0700745#define CNVT_TOHW(val, width) ((((val) << (width)) + 0x7FFF - (val)) >> 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700747static int tdfxfb_setcolreg(unsigned regno, unsigned red, unsigned green,
748 unsigned blue, unsigned transp,
749 struct fb_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800751 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 u32 rgbcol;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700753
754 if (regno >= info->cmap.len || regno > 255)
755 return 1;
756
Krzysztof Helt254c9472007-10-16 01:28:46 -0700757 /* grayscale works only partially under directcolor */
758 if (info->var.grayscale) {
759 /* grayscale = 0.30*R + 0.59*G + 0.11*B */
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -0700760 blue = (red * 77 + green * 151 + blue * 28) >> 8;
761 green = blue;
762 red = blue;
Krzysztof Helt254c9472007-10-16 01:28:46 -0700763 }
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 switch (info->fix.visual) {
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800766 case FB_VISUAL_PSEUDOCOLOR:
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -0700767 rgbcol = (((u32)red & 0xff00) << 8) |
768 (((u32)green & 0xff00) << 0) |
769 (((u32)blue & 0xff00) >> 8);
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800770 do_setpalentry(par, regno, rgbcol);
771 break;
772 /* Truecolor has no hardware color palettes. */
773 case FB_VISUAL_TRUECOLOR:
774 if (regno < 16) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700775 rgbcol = (CNVT_TOHW(red, info->var.red.length) <<
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800776 info->var.red.offset) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700777 (CNVT_TOHW(green, info->var.green.length) <<
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800778 info->var.green.offset) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700779 (CNVT_TOHW(blue, info->var.blue.length) <<
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800780 info->var.blue.offset) |
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700781 (CNVT_TOHW(transp, info->var.transp.length) <<
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800782 info->var.transp.offset);
783 par->palette[regno] = rgbcol;
784 }
785
786 break;
787 default:
788 DPRINTK("bad depth %u\n", info->var.bits_per_pixel);
789 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
Antonino A. Daplas54243ce2006-03-11 03:27:26 -0800791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return 0;
793}
794
795/* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */
796static int tdfxfb_blank(int blank, struct fb_info *info)
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700797{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800798 struct tdfx_par *par = info->par;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700799 int vgablank = 1;
800 u32 dacmode = tdfx_inl(par, DACMODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700802 dacmode &= ~(BIT(1) | BIT(3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 switch (blank) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700805 case FB_BLANK_UNBLANK: /* Screen: On; HSync: On, VSync: On */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700806 vgablank = 0;
807 break;
808 case FB_BLANK_NORMAL: /* Screen: Off; HSync: On, VSync: On */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700809 break;
810 case FB_BLANK_VSYNC_SUSPEND: /* Screen: Off; HSync: On, VSync: Off */
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700811 dacmode |= BIT(3);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700812 break;
813 case FB_BLANK_HSYNC_SUSPEND: /* Screen: Off; HSync: Off, VSync: On */
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700814 dacmode |= BIT(1);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700815 break;
816 case FB_BLANK_POWERDOWN: /* Screen: Off; HSync: Off, VSync: Off */
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700817 dacmode |= BIT(1) | BIT(3);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700818 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700821 banshee_make_room(par, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 tdfx_outl(par, DACMODE, dacmode);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700823 if (vgablank)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 vga_disable_video(par);
825 else
826 vga_enable_video(par);
827 return 0;
828}
829
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700830/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 * Set the starting position of the visible screen to var->yoffset
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700832 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833static int tdfxfb_pan_display(struct fb_var_screeninfo *var,
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700834 struct fb_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800836 struct tdfx_par *par = info->par;
Krzysztof Helt4f05b532007-10-16 01:28:48 -0700837 u32 addr = var->yoffset * info->fix.line_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 if (nopan || var->xoffset || (var->yoffset > var->yres_virtual))
840 return -EINVAL;
841 if ((var->yoffset + var->yres > var->yres_virtual && nowrap))
842 return -EINVAL;
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 banshee_make_room(par, 1);
845 tdfx_outl(par, VIDDESKSTART, addr);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 info->var.xoffset = var->xoffset;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700848 info->var.yoffset = var->yoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return 0;
850}
851
852#ifdef CONFIG_FB_3DFX_ACCEL
853/*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700854 * FillRect 2D command (solidfill or invert (via ROP_XOR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700856static void tdfxfb_fillrect(struct fb_info *info,
857 const struct fb_fillrect *rect)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800859 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 u32 bpp = info->var.bits_per_pixel;
861 u32 stride = info->fix.line_length;
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -0700862 u32 fmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 int tdfx_rop;
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700864 u32 dx = rect->dx;
865 u32 dy = rect->dy;
866 u32 dstbase = 0;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700867
868 if (rect->rop == ROP_COPY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 tdfx_rop = TDFX_ROP_COPY;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700870 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 tdfx_rop = TDFX_ROP_XOR;
872
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700873 /* asume always rect->height < 4096 */
874 if (dy + rect->height > 4095) {
875 dstbase = stride * dy;
876 dy = 0;
877 }
878 /* asume always rect->width < 4096 */
879 if (dx + rect->width > 4095) {
880 dstbase += dx * bpp >> 3;
881 dx = 0;
882 }
883 banshee_make_room(par, 6);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700884 tdfx_outl(par, DSTFORMAT, fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700886 tdfx_outl(par, COLORFORE, rect->color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 } else { /* FB_VISUAL_TRUECOLOR */
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800888 tdfx_outl(par, COLORFORE, par->palette[rect->color]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700890 tdfx_outl(par, COMMAND_2D, COMMAND_2D_FILLRECT | (tdfx_rop << 24));
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700891 tdfx_outl(par, DSTBASE, dstbase);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700892 tdfx_outl(par, DSTSIZE, rect->width | (rect->height << 16));
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700893 tdfx_outl(par, LAUNCH_2D, dx | (dy << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
896/*
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700897 * Screen-to-Screen BitBlt 2D command (for the bmove fb op.)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 */
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700899static void tdfxfb_copyarea(struct fb_info *info,
900 const struct fb_copyarea *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800902 struct tdfx_par *par = info->par;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700903 u32 sx = area->sx, sy = area->sy, dx = area->dx, dy = area->dy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 u32 bpp = info->var.bits_per_pixel;
905 u32 stride = info->fix.line_length;
906 u32 blitcmd = COMMAND_2D_S2S_BITBLT | (TDFX_ROP_COPY << 24);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700907 u32 fmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700908 u32 dstbase = 0;
909 u32 srcbase = 0;
910
911 /* asume always area->height < 4096 */
912 if (sy + area->height > 4095) {
913 srcbase = stride * sy;
914 sy = 0;
915 }
916 /* asume always area->width < 4096 */
917 if (sx + area->width > 4095) {
918 srcbase += sx * bpp >> 3;
919 sx = 0;
920 }
921 /* asume always area->height < 4096 */
922 if (dy + area->height > 4095) {
923 dstbase = stride * dy;
924 dy = 0;
925 }
926 /* asume always area->width < 4096 */
927 if (dx + area->width > 4095) {
928 dstbase += dx * bpp >> 3;
929 dx = 0;
930 }
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (area->sx <= area->dx) {
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -0700933 /* -X */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 blitcmd |= BIT(14);
935 sx += area->width - 1;
936 dx += area->width - 1;
937 }
938 if (area->sy <= area->dy) {
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -0700939 /* -Y */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 blitcmd |= BIT(15);
941 sy += area->height - 1;
942 dy += area->height - 1;
943 }
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700944
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700945 banshee_make_room(par, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700947 tdfx_outl(par, SRCFORMAT, fmt);
948 tdfx_outl(par, DSTFORMAT, fmt);
949 tdfx_outl(par, COMMAND_2D, blitcmd);
950 tdfx_outl(par, DSTSIZE, area->width | (area->height << 16));
951 tdfx_outl(par, DSTXY, dx | (dy << 16));
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700952 tdfx_outl(par, SRCBASE, srcbase);
953 tdfx_outl(par, DSTBASE, dstbase);
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700954 tdfx_outl(par, LAUNCH_2D, sx | (sy << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700957static void tdfxfb_imageblit(struct fb_info *info, const struct fb_image *image)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Antonino A. Daplasa807f612006-01-09 20:53:11 -0800959 struct tdfx_par *par = info->par;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700960 int size = image->height * ((image->width * image->depth + 7) >> 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 int fifo_free;
962 int i, stride = info->fix.line_length;
963 u32 bpp = info->var.bits_per_pixel;
Krzysztof Helt8af1d502007-10-16 01:28:43 -0700964 u32 dstfmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 u8 *chardata = (u8 *) image->data;
966 u32 srcfmt;
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700967 u32 dx = image->dx;
968 u32 dy = image->dy;
969 u32 dstbase = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 if (image->depth != 1) {
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -0700972#ifdef BROKEN_CODE
973 banshee_make_room(par, 6 + ((size + 3) >> 2));
974 srcfmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13) |
975 0x400000;
976#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 cfb_imageblit(info, image);
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -0700978#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return;
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700980 }
981 banshee_make_room(par, 9);
982 switch (info->fix.visual) {
983 case FB_VISUAL_PSEUDOCOLOR:
984 tdfx_outl(par, COLORFORE, image->fg_color);
985 tdfx_outl(par, COLORBACK, image->bg_color);
986 break;
987 case FB_VISUAL_TRUECOLOR:
988 default:
989 tdfx_outl(par, COLORFORE,
990 par->palette[image->fg_color]);
991 tdfx_outl(par, COLORBACK,
992 par->palette[image->bg_color]);
993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994#ifdef __BIG_ENDIAN
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700995 srcfmt = 0x400000 | BIT(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996#else
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700997 srcfmt = 0x400000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998#endif
Krzysztof Helt92744dd2007-10-16 01:28:45 -0700999 /* asume always image->height < 4096 */
1000 if (dy + image->height > 4095) {
1001 dstbase = stride * dy;
1002 dy = 0;
1003 }
1004 /* asume always image->width < 4096 */
1005 if (dx + image->width > 4095) {
1006 dstbase += dx * bpp >> 3;
1007 dx = 0;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001010 tdfx_outl(par, DSTBASE, dstbase);
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001011 tdfx_outl(par, SRCXY, 0);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001012 tdfx_outl(par, DSTXY, dx | (dy << 16));
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -07001013 tdfx_outl(par, COMMAND_2D,
1014 COMMAND_2D_H2S_BITBLT | (TDFX_ROP_COPY << 24));
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001015 tdfx_outl(par, SRCFORMAT, srcfmt);
1016 tdfx_outl(par, DSTFORMAT, dstfmt);
1017 tdfx_outl(par, DSTSIZE, image->width | (image->height << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 /* A count of how many free FIFO entries we've requested.
1020 * When this goes negative, we need to request more. */
1021 fifo_free = 0;
1022
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001023 /* Send four bytes at a time of data */
1024 for (i = (size >> 2); i > 0; i--) {
1025 if (--fifo_free < 0) {
1026 fifo_free = 31;
1027 banshee_make_room(par, fifo_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -07001029 tdfx_outl(par, LAUNCH_2D, *(u32 *)chardata);
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001030 chardata += 4;
1031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001033 /* Send the leftovers now */
1034 banshee_make_room(par, 3);
Krzysztof Helt4f05b532007-10-16 01:28:48 -07001035 switch (size % 4) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001036 case 0:
1037 break;
1038 case 1:
1039 tdfx_outl(par, LAUNCH_2D, *chardata);
1040 break;
1041 case 2:
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -07001042 tdfx_outl(par, LAUNCH_2D, *(u16 *)chardata);
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001043 break;
1044 case 3:
1045 tdfx_outl(par, LAUNCH_2D,
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -07001046 *(u16 *)chardata | (chardata[3] << 24));
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001047 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049}
1050#endif /* CONFIG_FB_3DFX_ACCEL */
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052static int tdfxfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
1053{
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001054 struct tdfx_par *par = info->par;
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001055 u32 vidcfg;
1056
1057 if (!hwcursor)
1058 return -EINVAL; /* just to force soft_cursor() call */
1059
1060 /* Too large of a cursor or wrong bpp :-( */
1061 if (cursor->image.width > 64 ||
1062 cursor->image.height > 64 ||
1063 cursor->image.depth > 1)
1064 return -EINVAL;
1065
1066 vidcfg = tdfx_inl(par, VIDPROCCFG);
1067 if (cursor->enable)
1068 tdfx_outl(par, VIDPROCCFG, vidcfg | VIDCFG_HWCURSOR_ENABLE);
1069 else
1070 tdfx_outl(par, VIDPROCCFG, vidcfg & ~VIDCFG_HWCURSOR_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 /*
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001073 * If the cursor is not be changed this means either we want the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 * current cursor state (if enable is set) or we want to query what
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001075 * we can do with the cursor (if enable is not set)
1076 */
1077 if (!cursor->set)
1078 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 /* fix cursor color - XFree86 forgets to restore it properly */
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001081 if (cursor->set & FB_CUR_SETCMAP) {
1082 struct fb_cmap cmap = info->cmap;
1083 u32 bg_idx = cursor->image.bg_color;
1084 u32 fg_idx = cursor->image.fg_color;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 unsigned long bg_color, fg_color;
1086
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001087 fg_color = (((u32)cmap.red[fg_idx] & 0xff00) << 8) |
1088 (((u32)cmap.green[fg_idx] & 0xff00) << 0) |
1089 (((u32)cmap.blue[fg_idx] & 0xff00) >> 8);
1090 bg_color = (((u32)cmap.red[bg_idx] & 0xff00) << 8) |
1091 (((u32)cmap.green[bg_idx] & 0xff00) << 0) |
1092 (((u32)cmap.blue[bg_idx] & 0xff00) >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 banshee_make_room(par, 2);
1094 tdfx_outl(par, HWCURC0, bg_color);
1095 tdfx_outl(par, HWCURC1, fg_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001098 if (cursor->set & FB_CUR_SETPOS) {
1099 int x = cursor->image.dx;
1100 int y = cursor->image.dy - info->var.yoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 x += 63;
1103 y += 63;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 banshee_make_room(par, 1);
1105 tdfx_outl(par, HWCURLOC, (y << 16) + x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001107 if (cursor->set & (FB_CUR_SETIMAGE | FB_CUR_SETSHAPE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 /*
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001109 * Voodoo 3 and above cards use 2 monochrome cursor patterns.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 * The reason is so the card can fetch 8 words at a time
1111 * and are stored on chip for use for the next 8 scanlines.
1112 * This reduces the number of times for access to draw the
1113 * cursor for each screen refresh.
1114 * Each pattern is a bitmap of 64 bit wide and 64 bit high
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001115 * (total of 8192 bits or 1024 bytes). The two patterns are
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 * stored in such a way that pattern 0 always resides in the
1117 * lower half (least significant 64 bits) of a 128 bit word
1118 * and pattern 1 the upper half. If you examine the data of
1119 * the cursor image the graphics card uses then from the
1120 * begining you see line one of pattern 0, line one of
1121 * pattern 1, line two of pattern 0, line two of pattern 1,
1122 * etc etc. The linear stride for the cursor is always 16 bytes
1123 * (128 bits) which is the maximum cursor width times two for
1124 * the two monochrome patterns.
1125 */
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001126 u8 __iomem *cursorbase = info->screen_base + info->fix.smem_len;
1127 u8 *bitmap = (u8 *)cursor->image.data;
1128 u8 *mask = (u8 *)cursor->mask;
1129 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001131 fb_memset(cursorbase, 0, 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001133 for (i = 0; i < cursor->image.height; i++) {
1134 int h = 0;
1135 int j = (cursor->image.width + 7) >> 3;
1136
1137 for (; j > 0; j--) {
1138 u8 data = *mask ^ *bitmap;
1139 if (cursor->rop == ROP_COPY)
1140 data = *mask & *bitmap;
1141 /* Pattern 0. Copy the cursor mask to it */
1142 fb_writeb(*mask, cursorbase + h);
1143 mask++;
1144 /* Pattern 1. Copy the cursor bitmap to it */
1145 fb_writeb(data, cursorbase + h + 8);
1146 bitmap++;
1147 h++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001149 cursorbase += 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
1151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return 0;
1153}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001155static struct fb_ops tdfxfb_ops = {
1156 .owner = THIS_MODULE,
1157 .fb_check_var = tdfxfb_check_var,
1158 .fb_set_par = tdfxfb_set_par,
1159 .fb_setcolreg = tdfxfb_setcolreg,
1160 .fb_blank = tdfxfb_blank,
1161 .fb_pan_display = tdfxfb_pan_display,
1162 .fb_sync = banshee_wait_idle,
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001163 .fb_cursor = tdfxfb_cursor,
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001164#ifdef CONFIG_FB_3DFX_ACCEL
1165 .fb_fillrect = tdfxfb_fillrect,
1166 .fb_copyarea = tdfxfb_copyarea,
1167 .fb_imageblit = tdfxfb_imageblit,
1168#else
1169 .fb_fillrect = cfb_fillrect,
1170 .fb_copyarea = cfb_copyarea,
1171 .fb_imageblit = cfb_imageblit,
1172#endif
1173};
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175/**
1176 * tdfxfb_probe - Device Initializiation
1177 *
1178 * @pdev: PCI Device to initialize
1179 * @id: PCI Device ID
1180 *
1181 * Initializes and allocates resources for PCI device @pdev.
1182 *
1183 */
1184static int __devinit tdfxfb_probe(struct pci_dev *pdev,
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001185 const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
1187 struct tdfx_par *default_par;
1188 struct fb_info *info;
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001189 int err, lpitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -07001191 err = pci_enable_device(pdev);
1192 if (err) {
1193 printk(KERN_ERR "tdfxfb: Can't enable pdev: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return err;
1195 }
1196
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001197 info = framebuffer_alloc(sizeof(struct tdfx_par), &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001199 if (!info)
1200 return -ENOMEM;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 default_par = info->par;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 /* Configure the default fb_fix_screeninfo first */
1205 switch (pdev->device) {
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001206 case PCI_DEVICE_ID_3DFX_BANSHEE:
1207 strcat(tdfx_fix.id, " Banshee");
1208 default_par->max_pixclock = BANSHEE_MAX_PIXCLOCK;
1209 break;
1210 case PCI_DEVICE_ID_3DFX_VOODOO3:
1211 strcat(tdfx_fix.id, " Voodoo3");
1212 default_par->max_pixclock = VOODOO3_MAX_PIXCLOCK;
1213 break;
1214 case PCI_DEVICE_ID_3DFX_VOODOO5:
1215 strcat(tdfx_fix.id, " Voodoo5");
1216 default_par->max_pixclock = VOODOO5_MAX_PIXCLOCK;
1217 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219
1220 tdfx_fix.mmio_start = pci_resource_start(pdev, 0);
1221 tdfx_fix.mmio_len = pci_resource_len(pdev, 0);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001222 if (!request_mem_region(tdfx_fix.mmio_start, tdfx_fix.mmio_len,
1223 "tdfx regbase")) {
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -07001224 printk(KERN_ERR "tdfxfb: Can't reserve regbase\n");
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001225 goto out_err;
1226 }
1227
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001228 default_par->regbase_virt =
1229 ioremap_nocache(tdfx_fix.mmio_start, tdfx_fix.mmio_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (!default_par->regbase_virt) {
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -07001231 printk(KERN_ERR "fb: Can't remap %s register area.\n",
1232 tdfx_fix.id);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001233 goto out_err_regbase;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 tdfx_fix.smem_start = pci_resource_start(pdev, 1);
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -07001237 tdfx_fix.smem_len = do_lfb_size(default_par, pdev->device);
1238 if (!tdfx_fix.smem_len) {
1239 printk(KERN_ERR "fb: Can't count %s memory.\n", tdfx_fix.id);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001240 goto out_err_regbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
1242
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001243 if (!request_mem_region(tdfx_fix.smem_start,
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001244 pci_resource_len(pdev, 1), "tdfx smem")) {
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -07001245 printk(KERN_ERR "tdfxfb: Can't reserve smem\n");
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001246 goto out_err_regbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
1248
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001249 info->screen_base = ioremap_nocache(tdfx_fix.smem_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 tdfx_fix.smem_len);
1251 if (!info->screen_base) {
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -07001252 printk(KERN_ERR "fb: Can't remap %s framebuffer.\n",
1253 tdfx_fix.id);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001254 goto out_err_screenbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
1256
1257 default_par->iobase = pci_resource_start(pdev, 2);
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (!request_region(pci_resource_start(pdev, 2),
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001260 pci_resource_len(pdev, 2), "tdfx iobase")) {
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -07001261 printk(KERN_ERR "tdfxfb: Can't reserve iobase\n");
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001262 goto out_err_screenbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
1264
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -07001265 printk(KERN_INFO "fb: %s memory = %dK\n", tdfx_fix.id,
1266 tdfx_fix.smem_len >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Krzysztof Helt0960bd32007-10-16 01:28:49 -07001268 default_par->mtrr_handle = -1;
1269 if (!nomtrr)
1270 default_par->mtrr_handle =
1271 mtrr_add(tdfx_fix.smem_start, tdfx_fix.smem_len,
1272 MTRR_TYPE_WRCOMB, 1);
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 tdfx_fix.ypanstep = nopan ? 0 : 1;
1275 tdfx_fix.ywrapstep = nowrap ? 0 : 1;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 info->fbops = &tdfxfb_ops;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001278 info->fix = tdfx_fix;
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001279 info->pseudo_palette = default_par->palette;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
1281#ifdef CONFIG_FB_3DFX_ACCEL
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -07001282 info->flags |= FBINFO_HWACCEL_FILLRECT |
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001283 FBINFO_HWACCEL_COPYAREA |
1284 FBINFO_HWACCEL_IMAGEBLIT |
1285 FBINFO_READS_FAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286#endif
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001287 /* reserve 8192 bits for cursor */
1288 /* the 2.4 driver says PAGE_MASK boundary is not enough for Voodoo4 */
1289 if (hwcursor)
1290 info->fix.smem_len = (info->fix.smem_len - 1024) &
1291 (PAGE_MASK << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 if (!mode_option)
1294 mode_option = "640x480@60";
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001295
1296 err = fb_find_mode(&info->var, info, mode_option, NULL, 0, NULL, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 if (!err || err == 4)
1298 info->var = tdfx_var;
1299
1300 /* maximize virtual vertical length */
1301 lpitch = info->var.xres_virtual * ((info->var.bits_per_pixel + 7) >> 3);
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001302 info->var.yres_virtual = info->fix.smem_len / lpitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (info->var.yres_virtual < info->var.yres)
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001304 goto out_err_iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -07001307 printk(KERN_ERR "tdfxfb: Can't allocate color map\n");
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001308 goto out_err_iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
1310
1311 if (register_framebuffer(info) < 0) {
Krzysztof Helt3cbe9cf2007-10-16 01:29:28 -07001312 printk(KERN_ERR "tdfxfb: can't register framebuffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 fb_dealloc_cmap(&info->cmap);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001314 goto out_err_iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
1316 /*
1317 * Our driver data
1318 */
1319 pci_set_drvdata(pdev, info);
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001320 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001322out_err_iobase:
Krzysztof Helt0960bd32007-10-16 01:28:49 -07001323 if (default_par->mtrr_handle >= 0)
1324 mtrr_del(default_par->mtrr_handle, info->fix.smem_start,
1325 info->fix.smem_len);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001326 release_mem_region(pci_resource_start(pdev, 2),
1327 pci_resource_len(pdev, 2));
1328out_err_screenbase:
1329 if (info->screen_base)
1330 iounmap(info->screen_base);
1331 release_mem_region(tdfx_fix.smem_start, pci_resource_len(pdev, 1));
1332out_err_regbase:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 /*
1334 * Cleanup after anything that was remapped/allocated.
1335 */
1336 if (default_par->regbase_virt)
1337 iounmap(default_par->regbase_virt);
Krzysztof Helt92744dd2007-10-16 01:28:45 -07001338 release_mem_region(tdfx_fix.mmio_start, tdfx_fix.mmio_len);
1339out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 framebuffer_release(info);
1341 return -ENXIO;
1342}
1343
1344#ifndef MODULE
Randy Dunlap0ce85eb2008-02-06 01:39:19 -08001345static void __init tdfxfb_setup(char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001347 char *this_opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 if (!options || !*options)
1350 return;
1351
1352 while ((this_opt = strsep(&options, ",")) != NULL) {
1353 if (!*this_opt)
1354 continue;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001355 if (!strcmp(this_opt, "nopan")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 nopan = 1;
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001357 } else if (!strcmp(this_opt, "nowrap")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 nowrap = 1;
Krzysztof Helt0960bd32007-10-16 01:28:49 -07001359 } else if (!strncmp(this_opt, "hwcursor=", 9)) {
1360 hwcursor = simple_strtoul(this_opt + 9, NULL, 0);
1361#ifdef CONFIG_MTRR
1362 } else if (!strncmp(this_opt, "nomtrr", 6)) {
1363 nomtrr = 1;
1364#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 } else {
1366 mode_option = this_opt;
1367 }
1368 }
1369}
1370#endif
1371
1372/**
1373 * tdfxfb_remove - Device removal
1374 *
1375 * @pdev: PCI Device to cleanup
1376 *
1377 * Releases all resources allocated during the course of the driver's
1378 * lifetime for the PCI device @pdev.
1379 *
1380 */
1381static void __devexit tdfxfb_remove(struct pci_dev *pdev)
1382{
1383 struct fb_info *info = pci_get_drvdata(pdev);
Antonino A. Daplasa807f612006-01-09 20:53:11 -08001384 struct tdfx_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 unregister_framebuffer(info);
Krzysztof Helt0960bd32007-10-16 01:28:49 -07001387 if (par->mtrr_handle >= 0)
1388 mtrr_del(par->mtrr_handle, info->fix.smem_start,
1389 info->fix.smem_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 iounmap(par->regbase_virt);
1391 iounmap(info->screen_base);
1392
1393 /* Clean up after reserved regions */
1394 release_region(pci_resource_start(pdev, 2),
1395 pci_resource_len(pdev, 2));
1396 release_mem_region(pci_resource_start(pdev, 1),
1397 pci_resource_len(pdev, 1));
1398 release_mem_region(pci_resource_start(pdev, 0),
1399 pci_resource_len(pdev, 0));
1400 pci_set_drvdata(pdev, NULL);
1401 framebuffer_release(info);
1402}
1403
1404static int __init tdfxfb_init(void)
1405{
1406#ifndef MODULE
1407 char *option = NULL;
1408
1409 if (fb_get_options("tdfxfb", &option))
1410 return -ENODEV;
1411
1412 tdfxfb_setup(option);
1413#endif
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001414 return pci_register_driver(&tdfxfb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
1417static void __exit tdfxfb_exit(void)
1418{
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001419 pci_unregister_driver(&tdfxfb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
1422MODULE_AUTHOR("Hannu Mallat <hmallat@cc.hut.fi>");
1423MODULE_DESCRIPTION("3Dfx framebuffer device driver");
1424MODULE_LICENSE("GPL");
Krzysztof Helt8af1d502007-10-16 01:28:43 -07001425
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001426module_param(hwcursor, int, 0644);
1427MODULE_PARM_DESC(hwcursor, "Enable hardware cursor "
1428 "(1=enable, 0=disable, default=1)");
Krzysztof Helt0960bd32007-10-16 01:28:49 -07001429#ifdef CONFIG_MTRR
1430module_param(nomtrr, bool, 0);
1431MODULE_PARM_DESC(nomtrr, "Disable MTRR support (default: enabled)");
1432#endif
Krzysztof Helt90b0f082007-10-16 01:28:48 -07001433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434module_init(tdfxfb_init);
1435module_exit(tdfxfb_exit);