patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / video / pm2fb.c
1 /*
2  * Permedia2 framebuffer driver.
3  *
4  * 2.5/2.6 driver:
5  * Copyright (c) 2003 Jim Hague (jim.hague@acm.org)
6  *
7  * based on 2.4 driver:
8  * Copyright (c) 1998-2000 Ilario Nardinocchi (nardinoc@CS.UniBO.IT)
9  * Copyright (c) 1999 Jakub Jelinek (jakub@redhat.com)
10  *
11  * and additional input from James Simmon's port of Hannu Mallat's tdfx
12  * driver.
13  *
14  * $Id$
15  *
16  * I have a Creative Graphics Blaster Exxtreme card - pm2fb on x86.
17  * I have no access to other pm2fb implementations, and cannot test
18  * on them. Therefore for now I am omitting Sparc and CVision code.
19  *
20  * Multiple boards support has been on the TODO list for ages.
21  * Don't expect this to change.
22  *
23  * This file is subject to the terms and conditions of the GNU General Public
24  * License. See the file COPYING in the main directory of this archive for
25  * more details.
26  *
27  * 
28  */
29
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/errno.h>
34 #include <linux/string.h>
35 #include <linux/mm.h>
36 #include <linux/tty.h>
37 #include <linux/slab.h>
38 #include <linux/delay.h>
39 #include <linux/fb.h>
40 #include <linux/init.h>
41 #include <linux/pci.h>
42
43 #include <video/permedia2.h>
44 #include <video/cvisionppc.h>
45
46 #if !defined(__LITTLE_ENDIAN) && !defined(__BIG_ENDIAN)
47 #error  "The endianness of the target host has not been defined."
48 #endif
49
50 #if defined(__BIG_ENDIAN) && !defined(__sparc__)
51 #define PM2FB_BE_APERTURE
52 #endif
53
54 #if !defined(CONFIG_PCI)
55 #error "Only generic PCI cards supported."
56 #endif
57
58 #undef PM2FB_MASTER_DEBUG
59 #ifdef PM2FB_MASTER_DEBUG
60 #define DPRINTK(a,b...) printk(KERN_DEBUG "pm2fb: %s: " a, __FUNCTION__ , ## b)
61 #else
62 #define DPRINTK(a,b...)
63 #endif
64
65 /*
66  * The 2.4 driver calls reset_card() at init time, where it also sets the
67  * initial mode. I don't think the driver should touch the chip until
68  * the console sets a video mode. So I was calling this at the start
69  * of setting a mode. However, certainly on 1280x1024 depth 16 on my
70  * PCI Graphics Blaster Exxtreme this causes the display to smear
71  * slightly.  I don't know why. Guesses to jim.hague@acm.org.
72  */
73 #undef RESET_CARD_ON_MODE_SET
74
75 /*
76  * Driver data 
77  */
78 static char *mode __initdata = NULL;
79
80 /*
81  * The XFree GLINT driver will (I think to implement hardware cursor
82  * support on TVP4010 and similar where there is no RAMDAC - see
83  * comment in set_video) always request +ve sync regardless of what
84  * the mode requires. This screws me because I have a Sun
85  * fixed-frequency monitor which absolutely has to have -ve sync. So
86  * these flags allow the user to specify that requests for +ve sync
87  * should be silently turned in -ve sync.
88  */
89 static int lowhsync __initdata = 0;
90 static int lowvsync __initdata = 0;     
91
92 /*
93  * The hardware state of the graphics card that isn't part of the
94  * screeninfo.
95  */
96 struct pm2fb_par
97 {
98         pm2type_t       type;           /* Board type */
99         u32             fb_size;        /* framebuffer memory size */
100         unsigned char*  v_fb;           /* virtual address of frame buffer */
101         unsigned char*  v_regs;         /* virtual address of p_regs */
102         u32             memclock;       /* memclock */
103         u32             video;          /* video flags before blanking */
104 };
105
106 /*
107  * Here we define the default structs fb_fix_screeninfo and fb_var_screeninfo
108  * if we don't use modedb.
109  */
110 static struct fb_fix_screeninfo pm2fb_fix __initdata = {
111         .id =           "", 
112         .type =         FB_TYPE_PACKED_PIXELS,
113         .visual =       FB_VISUAL_PSEUDOCOLOR,
114         .xpanstep =     1,
115         .ypanstep =     1,
116         .ywrapstep =    0, 
117         .accel =        FB_ACCEL_NONE,
118 };
119
120 /*
121  * Default video mode. In case the modedb doesn't work, or we're
122  * a module (in which case modedb doesn't really work).
123  */
124 static struct fb_var_screeninfo pm2fb_var __initdata = {
125         /* "640x480, 8 bpp @ 60 Hz */
126         .xres =         640,
127         .yres =         480,
128         .xres_virtual = 640,
129         .yres_virtual = 480,
130         .bits_per_pixel =8,
131         .red =          {0, 8, 0},
132         .blue =         {0, 8, 0},
133         .green =        {0, 8, 0},
134         .activate =     FB_ACTIVATE_NOW,
135         .height =       -1,
136         .width =        -1,
137         .accel_flags =  0,
138         .pixclock =     39721,
139         .left_margin =  40,
140         .right_margin = 24,
141         .upper_margin = 32,
142         .lower_margin = 11,
143         .hsync_len =    96,
144         .vsync_len =    2,
145         .vmode =        FB_VMODE_NONINTERLACED
146 };
147
148 /*
149  * Utility functions
150  */
151
152 inline static u32 RD32(unsigned char* base, s32 off)
153 {
154         return fb_readl(base + off);
155 }
156
157 inline static void WR32(unsigned char* base, s32 off, u32 v)
158 {
159         fb_writel(v, base + off);
160 }
161
162 inline static u32 pm2_RD(struct pm2fb_par* p, s32 off)
163 {
164         return RD32(p->v_regs, off);
165 }
166
167 inline static void pm2_WR(struct pm2fb_par* p, s32 off, u32 v)
168 {
169         WR32(p->v_regs, off, v);
170 }
171
172 inline static u32 pm2_RDAC_RD(struct pm2fb_par* p, s32 idx)
173 {
174         int index = PM2R_RD_INDEXED_DATA;
175         switch (p->type) {
176         case PM2_TYPE_PERMEDIA2:
177                 pm2_WR(p, PM2R_RD_PALETTE_WRITE_ADDRESS, idx);
178                 break;
179         case PM2_TYPE_PERMEDIA2V:
180                 pm2_WR(p, PM2VR_RD_INDEX_LOW, idx & 0xff);
181                 index = PM2VR_RD_INDEXED_DATA;
182                 break;
183         }       
184         mb();
185         return pm2_RD(p, index);
186 }
187
188 inline static void pm2_RDAC_WR(struct pm2fb_par* p, s32 idx, u32 v)
189 {
190         int index = PM2R_RD_INDEXED_DATA;
191         switch (p->type) {
192         case PM2_TYPE_PERMEDIA2:
193                 pm2_WR(p, PM2R_RD_PALETTE_WRITE_ADDRESS, idx);
194                 break;
195         case PM2_TYPE_PERMEDIA2V:
196                 pm2_WR(p, PM2VR_RD_INDEX_LOW, idx & 0xff);
197                 index = PM2VR_RD_INDEXED_DATA;
198                 break;
199         }       
200         mb();
201         pm2_WR(p, index, v);
202 }
203
204 inline static u32 pm2v_RDAC_RD(struct pm2fb_par* p, s32 idx)
205 {
206         pm2_WR(p, PM2VR_RD_INDEX_LOW, idx & 0xff);
207         mb();
208         return pm2_RD(p, PM2VR_RD_INDEXED_DATA);
209 }
210
211 inline static void pm2v_RDAC_WR(struct pm2fb_par* p, s32 idx, u32 v)
212 {
213         pm2_WR(p, PM2VR_RD_INDEX_LOW, idx & 0xff);
214         mb();
215         pm2_WR(p, PM2VR_RD_INDEXED_DATA, v);
216 }
217
218 #ifdef CONFIG_FB_PM2_FIFO_DISCONNECT
219 #define WAIT_FIFO(p,a)
220 #else
221 inline static void WAIT_FIFO(struct pm2fb_par* p, u32 a)
222 {
223         while( pm2_RD(p, PM2R_IN_FIFO_SPACE) < a );
224         mb();
225 }
226 #endif
227
228 /*
229  * partial products for the supported horizontal resolutions.
230  */
231 #define PACKPP(p0,p1,p2)        (((p2) << 6) | ((p1) << 3) | (p0))
232 static const struct {
233         u16 width;
234         u16 pp;
235 } pp_table[] = {
236         { 32,   PACKPP(1, 0, 0) }, { 64,        PACKPP(1, 1, 0) },
237         { 96,   PACKPP(1, 1, 1) }, { 128,       PACKPP(2, 1, 1) },
238         { 160,  PACKPP(2, 2, 1) }, { 192,       PACKPP(2, 2, 2) },
239         { 224,  PACKPP(3, 2, 1) }, { 256,       PACKPP(3, 2, 2) },
240         { 288,  PACKPP(3, 3, 1) }, { 320,       PACKPP(3, 3, 2) },
241         { 384,  PACKPP(3, 3, 3) }, { 416,       PACKPP(4, 3, 1) },
242         { 448,  PACKPP(4, 3, 2) }, { 512,       PACKPP(4, 3, 3) },
243         { 544,  PACKPP(4, 4, 1) }, { 576,       PACKPP(4, 4, 2) },
244         { 640,  PACKPP(4, 4, 3) }, { 768,       PACKPP(4, 4, 4) },
245         { 800,  PACKPP(5, 4, 1) }, { 832,       PACKPP(5, 4, 2) },
246         { 896,  PACKPP(5, 4, 3) }, { 1024,      PACKPP(5, 4, 4) },
247         { 1056, PACKPP(5, 5, 1) }, { 1088,      PACKPP(5, 5, 2) },
248         { 1152, PACKPP(5, 5, 3) }, { 1280,      PACKPP(5, 5, 4) },
249         { 1536, PACKPP(5, 5, 5) }, { 1568,      PACKPP(6, 5, 1) },
250         { 1600, PACKPP(6, 5, 2) }, { 1664,      PACKPP(6, 5, 3) },
251         { 1792, PACKPP(6, 5, 4) }, { 2048,      PACKPP(6, 5, 5) },
252         { 0,    0 } };
253
254 static u32 partprod(u32 xres)
255 {
256         int i;
257
258         for (i = 0; pp_table[i].width && pp_table[i].width != xres; i++)
259                 ;
260         if ( pp_table[i].width == 0 )
261                 DPRINTK("invalid width %u\n", xres);
262         return pp_table[i].pp;
263 }
264
265 static u32 to3264(u32 timing, int bpp, int is64)
266 {
267         switch (bpp) {
268         case 8:
269                 timing >>= 2 + is64;
270                 break;
271         case 16:
272                 timing >>= 1 + is64;
273                 break;
274         case 24:
275                 timing = (timing * 3) >> (2 + is64);
276                 break;
277         case 32:
278                 if (is64)
279                         timing >>= 1;
280                 break;
281         }
282         return timing;
283 }
284
285 static void pm2_mnp(u32 clk, unsigned char* mm, unsigned char* nn,
286                     unsigned char* pp)
287 {
288         unsigned char m;
289         unsigned char n;
290         unsigned char p;
291         u32 f;
292         s32 curr;
293         s32 delta = 100000;
294
295         *mm = *nn = *pp = 0;
296         for (n = 2; n < 15; n++) {
297                 for (m = 2; m; m++) {
298                         f = PM2_REFERENCE_CLOCK * m / n;
299                         if (f >= 150000 && f <= 300000) {
300                                 for ( p = 0; p < 5; p++, f >>= 1) {
301                                         curr = ( clk > f ) ? clk - f : f - clk;
302                                         if ( curr < delta ) {
303                                                 delta=curr;
304                                                 *mm=m;
305                                                 *nn=n;
306                                                 *pp=p;
307                                         }
308                                 }
309                         }
310                 }
311         }
312 }
313
314 static void pm2v_mnp(u32 clk, unsigned char* mm, unsigned char* nn,
315                      unsigned char* pp)
316 {
317         unsigned char m;
318         unsigned char n;
319         unsigned char p;
320         u32 f;
321         s32 delta = 1000;
322
323         *mm = *nn = *pp = 0;
324         for (n = 1; n; n++) {
325                 for ( m = 1; m; m++) {
326                         for ( p = 0; p < 2; p++) {
327                                 f = PM2_REFERENCE_CLOCK * n / (m * (1 << (p + 1)));
328                                 if ( clk > f - delta && clk < f + delta ) {
329                                         delta = ( clk > f ) ? clk - f : f - clk;
330                                         *mm=m;
331                                         *nn=n;
332                                         *pp=p;
333                                 }
334                         }
335                 }
336         }
337 }
338
339 static void clear_palette(struct pm2fb_par* p) {
340         int i=256;
341
342         WAIT_FIFO(p, 1);
343         pm2_WR(p, PM2R_RD_PALETTE_WRITE_ADDRESS, 0);
344         wmb();
345         while (i--) {
346                 WAIT_FIFO(p, 3);
347                 pm2_WR(p, PM2R_RD_PALETTE_DATA, 0);
348                 pm2_WR(p, PM2R_RD_PALETTE_DATA, 0);
349                 pm2_WR(p, PM2R_RD_PALETTE_DATA, 0);
350         }
351 }
352
353 #ifdef RESET_CARD_ON_MODE_SET
354 static void reset_card(struct pm2fb_par* p)
355 {
356         if (p->type == PM2_TYPE_PERMEDIA2V)
357                 pm2_WR(p, PM2VR_RD_INDEX_HIGH, 0);
358         pm2_WR(p, PM2R_RESET_STATUS, 0);
359         mb();
360         while (pm2_RD(p, PM2R_RESET_STATUS) & PM2F_BEING_RESET)
361                 ;
362         mb();
363 #ifdef CONFIG_FB_PM2_FIFO_DISCONNECT
364         DPRINTK("FIFO disconnect enabled\n");
365         pm2_WR(p, PM2R_FIFO_DISCON, 1);
366         mb();
367 #endif
368 }
369 #endif
370
371 static void reset_config(struct pm2fb_par* p)
372 {
373         WAIT_FIFO(p, 52);
374         pm2_WR(p, PM2R_CHIP_CONFIG, pm2_RD(p, PM2R_CHIP_CONFIG)&
375                ~(PM2F_VGA_ENABLE|PM2F_VGA_FIXED));
376         pm2_WR(p, PM2R_BYPASS_WRITE_MASK, ~(0L));
377         pm2_WR(p, PM2R_FRAMEBUFFER_WRITE_MASK, ~(0L));
378         pm2_WR(p, PM2R_FIFO_CONTROL, 0);
379         pm2_WR(p, PM2R_APERTURE_ONE, 0);
380         pm2_WR(p, PM2R_APERTURE_TWO, 0);
381         pm2_WR(p, PM2R_RASTERIZER_MODE, 0);
382         pm2_WR(p, PM2R_DELTA_MODE, PM2F_DELTA_ORDER_RGB);
383         pm2_WR(p, PM2R_LB_READ_FORMAT, 0);
384         pm2_WR(p, PM2R_LB_WRITE_FORMAT, 0); 
385         pm2_WR(p, PM2R_LB_READ_MODE, 0);
386         pm2_WR(p, PM2R_LB_SOURCE_OFFSET, 0);
387         pm2_WR(p, PM2R_FB_SOURCE_OFFSET, 0);
388         pm2_WR(p, PM2R_FB_PIXEL_OFFSET, 0);
389         pm2_WR(p, PM2R_FB_WINDOW_BASE, 0);
390         pm2_WR(p, PM2R_LB_WINDOW_BASE, 0);
391         pm2_WR(p, PM2R_FB_SOFT_WRITE_MASK, ~(0L));
392         pm2_WR(p, PM2R_FB_HARD_WRITE_MASK, ~(0L));
393         pm2_WR(p, PM2R_FB_READ_PIXEL, 0);
394         pm2_WR(p, PM2R_DITHER_MODE, 0);
395         pm2_WR(p, PM2R_AREA_STIPPLE_MODE, 0);
396         pm2_WR(p, PM2R_DEPTH_MODE, 0);
397         pm2_WR(p, PM2R_STENCIL_MODE, 0);
398         pm2_WR(p, PM2R_TEXTURE_ADDRESS_MODE, 0);
399         pm2_WR(p, PM2R_TEXTURE_READ_MODE, 0);
400         pm2_WR(p, PM2R_TEXEL_LUT_MODE, 0);
401         pm2_WR(p, PM2R_YUV_MODE, 0);
402         pm2_WR(p, PM2R_COLOR_DDA_MODE, 0);
403         pm2_WR(p, PM2R_TEXTURE_COLOR_MODE, 0);
404         pm2_WR(p, PM2R_FOG_MODE, 0);
405         pm2_WR(p, PM2R_ALPHA_BLEND_MODE, 0);
406         pm2_WR(p, PM2R_LOGICAL_OP_MODE, 0);
407         pm2_WR(p, PM2R_STATISTICS_MODE, 0);
408         pm2_WR(p, PM2R_SCISSOR_MODE, 0);
409         pm2_WR(p, PM2R_FILTER_MODE, PM2F_SYNCHRONIZATION);
410         switch (p->type) {
411         case PM2_TYPE_PERMEDIA2:
412                 pm2_RDAC_WR(p, PM2I_RD_MODE_CONTROL, 0); /* no overlay */
413                 pm2_RDAC_WR(p, PM2I_RD_CURSOR_CONTROL, 0);
414                 pm2_RDAC_WR(p, PM2I_RD_MISC_CONTROL, PM2F_RD_PALETTE_WIDTH_8);
415                 break;
416         case PM2_TYPE_PERMEDIA2V:
417                 pm2v_RDAC_WR(p, PM2VI_RD_MISC_CONTROL, 1); /* 8bit */
418                 break;
419         }
420         pm2_RDAC_WR(p, PM2I_RD_COLOR_KEY_CONTROL, 0);
421         pm2_RDAC_WR(p, PM2I_RD_OVERLAY_KEY, 0);
422         pm2_RDAC_WR(p, PM2I_RD_RED_KEY, 0);
423         pm2_RDAC_WR(p, PM2I_RD_GREEN_KEY, 0);
424         pm2_RDAC_WR(p, PM2I_RD_BLUE_KEY, 0);
425 }
426
427 static void set_aperture(struct pm2fb_par* p, u32 depth)
428 {
429         WAIT_FIFO(p, 4);
430 #ifdef __LITTLE_ENDIAN
431         pm2_WR(p, PM2R_APERTURE_ONE, 0);
432         pm2_WR(p, PM2R_APERTURE_TWO, 0);
433 #else
434         switch (depth) {
435         case 8:
436         case 24:
437                 pm2_WR(p, PM2R_APERTURE_ONE, 0);
438                 pm2_WR(p, PM2R_APERTURE_TWO, 1);
439                 break;
440         case 16:
441                 pm2_WR(p, PM2R_APERTURE_ONE, 2);
442                 pm2_WR(p, PM2R_APERTURE_TWO, 1);
443                 break;
444         case 32:
445                 pm2_WR(p, PM2R_APERTURE_ONE, 1);
446                 pm2_WR(p, PM2R_APERTURE_TWO, 1);
447                 break;
448         }
449 #endif
450 }
451
452 static void set_color(struct pm2fb_par* p, unsigned char regno,
453                       unsigned char r, unsigned char g, unsigned char b)
454 {
455         WAIT_FIFO(p, 4);
456         pm2_WR(p, PM2R_RD_PALETTE_WRITE_ADDRESS, regno);
457         wmb();
458         pm2_WR(p, PM2R_RD_PALETTE_DATA, r);
459         wmb();
460         pm2_WR(p, PM2R_RD_PALETTE_DATA, g);
461         wmb();
462         pm2_WR(p, PM2R_RD_PALETTE_DATA, b);
463 }
464
465 static void set_pixclock(struct pm2fb_par* par, u32 clk)
466 {
467         int i;
468         unsigned char m, n, p;
469
470         switch (par->type) {
471         case PM2_TYPE_PERMEDIA2:
472                 pm2_mnp(clk, &m, &n, &p);
473                 WAIT_FIFO(par, 8);
474                 pm2_RDAC_WR(par, PM2I_RD_PIXEL_CLOCK_A3, 0);
475                 wmb();
476                 pm2_RDAC_WR(par, PM2I_RD_PIXEL_CLOCK_A1, m);
477                 pm2_RDAC_WR(par, PM2I_RD_PIXEL_CLOCK_A2, n);
478                 wmb();
479                 pm2_RDAC_WR(par, PM2I_RD_PIXEL_CLOCK_A3, 8|p);
480                 wmb();
481                 pm2_RDAC_RD(par, PM2I_RD_PIXEL_CLOCK_STATUS);
482                 rmb();
483                 for (i = 256;
484                      i && !(pm2_RD(par, PM2R_RD_INDEXED_DATA) & PM2F_PLL_LOCKED);
485                      i--)
486                         ;
487                 break;
488         case PM2_TYPE_PERMEDIA2V:
489                 pm2v_mnp(clk/2, &m, &n, &p);
490                 WAIT_FIFO(par, 8);
491                 pm2_WR(par, PM2VR_RD_INDEX_HIGH, PM2VI_RD_CLK0_PRESCALE >> 8);
492                 pm2v_RDAC_WR(par, PM2VI_RD_CLK0_PRESCALE, m);
493                 pm2v_RDAC_WR(par, PM2VI_RD_CLK0_FEEDBACK, n);
494                 pm2v_RDAC_WR(par, PM2VI_RD_CLK0_POSTSCALE, p);
495                 pm2_WR(par, PM2VR_RD_INDEX_HIGH, 0);
496                 break;
497         }
498 }
499
500 static void set_video(struct pm2fb_par* p, u32 video) {
501         u32 tmp;
502         u32 vsync;
503
504         vsync = video;
505
506         DPRINTK("video = 0x%x\n", video);
507         
508         /*
509          * The hardware cursor needs +vsync to recognise vert retrace.
510          * We may not be using the hardware cursor, but the X Glint
511          * driver may well. So always set +hsync/+vsync and then set
512          * the RAMDAC to invert the sync if necessary.
513          */
514         vsync &= ~(PM2F_HSYNC_MASK|PM2F_VSYNC_MASK);
515         vsync |= PM2F_HSYNC_ACT_HIGH|PM2F_VSYNC_ACT_HIGH;
516
517         WAIT_FIFO(p, 5);
518         pm2_WR(p, PM2R_VIDEO_CONTROL, vsync);
519
520         switch (p->type) {
521         case PM2_TYPE_PERMEDIA2:
522                 tmp = PM2F_RD_PALETTE_WIDTH_8;
523                 if ((video & PM2F_HSYNC_MASK) == PM2F_HSYNC_ACT_LOW)
524                         tmp |= 4; /* invert hsync */
525                 if ((video & PM2F_VSYNC_MASK) == PM2F_VSYNC_ACT_LOW)
526                         tmp |= 8; /* invert vsync */
527                 pm2_RDAC_WR(p, PM2I_RD_MISC_CONTROL, tmp);
528                 break;
529         case PM2_TYPE_PERMEDIA2V:
530                 tmp = 0;
531                 if ((video & PM2F_HSYNC_MASK) == PM2F_HSYNC_ACT_LOW)
532                         tmp |= 1; /* invert hsync */
533                 if ((video & PM2F_VSYNC_MASK) == PM2F_VSYNC_ACT_LOW)
534                         tmp |= 4; /* invert vsync */
535                 pm2v_RDAC_WR(p, PM2VI_RD_SYNC_CONTROL, tmp);
536                 pm2v_RDAC_WR(p, PM2VI_RD_MISC_CONTROL, 1);
537                 break;
538         }
539 }
540
541 /*
542  *
543  */
544
545 /**
546  *      pm2fb_check_var - Optional function. Validates a var passed in. 
547  *      @var: frame buffer variable screen structure
548  *      @info: frame buffer structure that represents a single frame buffer 
549  *
550  *      Checks to see if the hardware supports the state requested by
551  *      var passed in.
552  *
553  *      Returns negative errno on error, or zero on success.
554  */
555 static int pm2fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
556 {
557         u32 lpitch;
558
559         if (var->bits_per_pixel != 8  && var->bits_per_pixel != 16 &&
560             var->bits_per_pixel != 24 && var->bits_per_pixel != 32) {
561                 DPRINTK("depth not supported: %u\n", var->bits_per_pixel);
562                 return -EINVAL;
563         }
564
565         if (var->xres != var->xres_virtual) {
566                 DPRINTK("virtual x resolution != physical x resolution not supported\n");
567                 return -EINVAL;
568         }
569
570         if (var->yres > var->yres_virtual) {
571                 DPRINTK("virtual y resolution < physical y resolution not possible\n");
572                 return -EINVAL;
573         }
574
575         if (var->xoffset) {
576                 DPRINTK("xoffset not supported\n");
577                 return -EINVAL;
578         }
579
580         if ((var->vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) {
581                 DPRINTK("interlace not supported\n");
582                 return -EINVAL;
583         }
584
585         var->xres = (var->xres + 15) & ~15; /* could sometimes be 8 */
586         lpitch = var->xres * ((var->bits_per_pixel + 7)>>3);
587   
588         if (var->xres < 320 || var->xres > 1600) {
589                 DPRINTK("width not supported: %u\n", var->xres);
590                 return -EINVAL;
591         }
592   
593         if (var->yres < 200 || var->yres > 1200) {
594                 DPRINTK("height not supported: %u\n", var->yres);
595                 return -EINVAL;
596         }
597   
598         if (lpitch * var->yres_virtual > info->fix.smem_len) {
599                 DPRINTK("no memory for screen (%ux%ux%u)\n",
600                         var->xres, var->yres_virtual, var->bits_per_pixel);
601                 return -EINVAL;
602         }
603   
604         if (PICOS2KHZ(var->pixclock) > PM2_MAX_PIXCLOCK) {
605                 DPRINTK("pixclock too high (%ldKHz)\n", PICOS2KHZ(var->pixclock));
606                 return -EINVAL;
607         }
608
609         switch(var->bits_per_pixel) {
610         case 8:
611                 var->red.length = var->green.length = var->blue.length = 8;
612                 break;
613         case 16:
614                 var->red.offset   = 11;
615                 var->red.length   = 5;
616                 var->green.offset = 5;
617                 var->green.length = 6;
618                 var->blue.offset  = 0;
619                 var->blue.length  = 5;
620                 break;
621         case 24:
622                 var->red.offset   = 16;
623                 var->green.offset = 8;
624                 var->blue.offset  = 0;
625                 var->red.length = var->green.length = var->blue.length = 8;
626         case 32:
627                 var->red.offset   = 16;
628                 var->green.offset = 8;
629                 var->blue.offset  = 0;
630                 var->red.length = var->green.length = var->blue.length = 8;
631                 break;
632         }
633         var->height = var->width = -1;
634   
635         var->accel_flags = 0;   /* Can't mmap if this is on */
636         
637         DPRINTK("Checking graphics mode at %dx%d depth %d\n",
638                 var->xres, var->yres, var->bits_per_pixel);
639         return 0;
640 }
641
642 /**
643  *      pm2fb_set_par - Alters the hardware state.
644  *      @info: frame buffer structure that represents a single frame buffer
645  *
646  *      Using the fb_var_screeninfo in fb_info we set the resolution of the
647  *      this particular framebuffer.
648  */
649 static int pm2fb_set_par(struct fb_info *info)
650 {
651         struct pm2fb_par *par = (struct pm2fb_par *) info->par;
652         u32 pixclock;
653         u32 width, height, depth;
654         u32 hsstart, hsend, hbend, htotal;
655         u32 vsstart, vsend, vbend, vtotal;
656         u32 stride;
657         u32 base;
658         u32 video = 0;
659         u32 clrmode = PM2F_RD_COLOR_MODE_RGB;
660         u32 txtmap = 0;
661         u32 pixsize = 0;
662         u32 clrformat = 0;
663         u32 xres;
664         int data64;
665
666 #ifdef RESET_CARD_ON_MODE_SET
667         reset_card(par);
668 #endif
669         reset_config(par);
670         clear_palette(par);
671     
672         width = (info->var.xres_virtual + 7) & ~7;
673         height = info->var.yres_virtual;
674         depth = (info->var.bits_per_pixel + 7) & ~7;
675         depth = (depth > 32) ? 32 : depth;
676         data64 = depth > 8 || par->type == PM2_TYPE_PERMEDIA2V;
677
678         xres = (info->var.xres + 31) & ~31;
679         pixclock = PICOS2KHZ(info->var.pixclock);
680         if (pixclock > PM2_MAX_PIXCLOCK) {
681                 DPRINTK("pixclock too high (%uKHz)\n", pixclock);
682                 return -EINVAL;
683         }
684     
685         hsstart = to3264(info->var.right_margin, depth, data64);
686         hsend = hsstart + to3264(info->var.hsync_len, depth, data64);
687         hbend = hsend + to3264(info->var.left_margin, depth, data64);
688         htotal = to3264(xres, depth, data64) + hbend - 1;
689         vsstart = (info->var.lower_margin)
690                 ? info->var.lower_margin - 1
691                 : 0;    /* FIXME! */
692         vsend = info->var.lower_margin + info->var.vsync_len - 1;
693         vbend = info->var.lower_margin + info->var.vsync_len + info->var.upper_margin;
694         vtotal = info->var.yres + vbend - 1;
695         stride = to3264(width, depth, 1);
696         base = to3264(info->var.yoffset * xres + info->var.xoffset, depth, 1);
697         if (data64)
698                 video |= PM2F_DATA_64_ENABLE;
699     
700         if (info->var.sync & FB_SYNC_HOR_HIGH_ACT) {
701                 if (lowhsync) {
702                         DPRINTK("ignoring +hsync, using -hsync.\n");
703                         video |= PM2F_HSYNC_ACT_LOW;
704                 } else
705                         video |= PM2F_HSYNC_ACT_HIGH;
706         }
707         else
708                 video |= PM2F_HSYNC_ACT_LOW;
709         if (info->var.sync & FB_SYNC_VERT_HIGH_ACT) {
710                 if (lowvsync) {
711                         DPRINTK("ignoring +vsync, using -vsync.\n");
712                         video |= PM2F_VSYNC_ACT_LOW;
713                 } else
714                         video |= PM2F_VSYNC_ACT_HIGH;
715         }
716         else
717                 video |= PM2F_VSYNC_ACT_LOW;
718         if ((info->var.vmode & FB_VMODE_MASK)==FB_VMODE_INTERLACED) {
719                 DPRINTK("interlaced not supported\n");
720                 return -EINVAL;
721         }
722         if ((info->var.vmode & FB_VMODE_MASK)==FB_VMODE_DOUBLE)
723                 video |= PM2F_LINE_DOUBLE;
724         if (info->var.activate==FB_ACTIVATE_NOW)
725                 video |= PM2F_VIDEO_ENABLE;
726         par->video = video;
727
728         info->fix.visual =
729                 (depth == 8) ? FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
730         info->fix.line_length = info->var.xres * depth / 8;
731         info->cmap.len = 256;
732
733         /*
734          * Settings calculated. Now write them out.
735          */
736         if (par->type == PM2_TYPE_PERMEDIA2V) {
737                 WAIT_FIFO(par, 1);
738                 pm2_WR(par, PM2VR_RD_INDEX_HIGH, 0);
739         }
740     
741         set_aperture(par, depth);
742     
743         mb();
744         WAIT_FIFO(par, 19);
745         pm2_RDAC_WR(par, PM2I_RD_COLOR_KEY_CONTROL,
746                     ( depth == 8 ) ? 0 : PM2F_COLOR_KEY_TEST_OFF);
747         switch (depth) {
748         case 8:
749                 pm2_WR(par, PM2R_FB_READ_PIXEL, 0);
750                 clrformat = 0x0e;
751                 break;
752         case 16:
753                 pm2_WR(par, PM2R_FB_READ_PIXEL, 1);
754                 clrmode |= PM2F_RD_TRUECOLOR | 0x06;
755                 txtmap = PM2F_TEXTEL_SIZE_16;
756                 pixsize = 1;
757                 clrformat = 0x70;
758                 break;
759         case 32:
760                 pm2_WR(par, PM2R_FB_READ_PIXEL, 2);
761                 clrmode |= PM2F_RD_TRUECOLOR | 0x08;
762                 txtmap = PM2F_TEXTEL_SIZE_32;
763                 pixsize = 2;
764                 clrformat = 0x20;
765                 break;
766         case 24:
767                 pm2_WR(par, PM2R_FB_READ_PIXEL, 4);
768                 clrmode |= PM2F_RD_TRUECOLOR | 0x09;
769 #ifndef PM2FB_BE_APERTURE
770                 clrmode &= ~PM2F_RD_COLOR_MODE_RGB;
771 #endif
772                 txtmap = PM2F_TEXTEL_SIZE_24;
773                 pixsize = 4;
774                 clrformat = 0x20;
775                 break;
776         }
777         pm2_WR(par, PM2R_FB_WRITE_MODE, PM2F_FB_WRITE_ENABLE);
778         pm2_WR(par, PM2R_FB_READ_MODE, partprod(xres));
779         pm2_WR(par, PM2R_LB_READ_MODE, partprod(xres));
780         pm2_WR(par, PM2R_TEXTURE_MAP_FORMAT, txtmap | partprod(xres));
781         pm2_WR(par, PM2R_H_TOTAL, htotal);
782         pm2_WR(par, PM2R_HS_START, hsstart);
783         pm2_WR(par, PM2R_HS_END, hsend);
784         pm2_WR(par, PM2R_HG_END, hbend);
785         pm2_WR(par, PM2R_HB_END, hbend);
786         pm2_WR(par, PM2R_V_TOTAL, vtotal);
787         pm2_WR(par, PM2R_VS_START, vsstart);
788         pm2_WR(par, PM2R_VS_END, vsend);
789         pm2_WR(par, PM2R_VB_END, vbend);
790         pm2_WR(par, PM2R_SCREEN_STRIDE, stride);
791         wmb();
792         pm2_WR(par, PM2R_WINDOW_ORIGIN, 0);
793         pm2_WR(par, PM2R_SCREEN_SIZE, (height << 16) | width);
794         pm2_WR(par, PM2R_SCISSOR_MODE, PM2F_SCREEN_SCISSOR_ENABLE);
795         wmb();
796         pm2_WR(par, PM2R_SCREEN_BASE, base);
797         wmb();
798         set_video(par, video);
799         WAIT_FIFO(par, 4);
800         switch (par->type) {
801         case PM2_TYPE_PERMEDIA2:
802                 pm2_RDAC_WR(par, PM2I_RD_COLOR_MODE,
803                             PM2F_RD_COLOR_MODE_RGB | PM2F_RD_GUI_ACTIVE | clrmode);
804                 break;
805         case PM2_TYPE_PERMEDIA2V:
806                 pm2v_RDAC_WR(par, PM2VI_RD_PIXEL_SIZE, pixsize);
807                 pm2v_RDAC_WR(par, PM2VI_RD_COLOR_FORMAT, clrformat);
808                 break;
809         }
810         set_pixclock(par, pixclock);
811         DPRINTK("Setting graphics mode at %dx%d depth %d\n",
812                 info->var.xres, info->var.yres, info->var.bits_per_pixel);
813         return 0;       
814 }
815
816 /**
817  *      pm2fb_setcolreg - Sets a color register.
818  *      @regno: boolean, 0 copy local, 1 get_user() function
819  *      @red: frame buffer colormap structure
820  *      @green: The green value which can be up to 16 bits wide 
821  *      @blue:  The blue value which can be up to 16 bits wide.
822  *      @transp: If supported the alpha value which can be up to 16 bits wide.  
823  *      @info: frame buffer info structure
824  * 
825  *      Set a single color register. The values supplied have a 16 bit
826  *      magnitude which needs to be scaled in this function for the hardware.
827  *      Pretty much a direct lift from tdfxfb.c.
828  * 
829  *      Returns negative errno on error, or zero on success.
830  */
831 static int pm2fb_setcolreg(unsigned regno, unsigned red, unsigned green,
832                            unsigned blue, unsigned transp,
833                            struct fb_info *info)
834 {
835         struct pm2fb_par *par = (struct pm2fb_par *) info->par;
836
837         if (regno >= info->cmap.len)  /* no. of hw registers */
838                 return 1;
839         /*
840          * Program hardware... do anything you want with transp
841          */
842
843         /* grayscale works only partially under directcolor */
844         if (info->var.grayscale) {
845                 /* grayscale = 0.30*R + 0.59*G + 0.11*B */
846                 red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
847         }
848
849         /* Directcolor:
850          *   var->{color}.offset contains start of bitfield
851          *   var->{color}.length contains length of bitfield
852          *   {hardwarespecific} contains width of DAC
853          *   cmap[X] is programmed to
854          *   (X << red.offset) | (X << green.offset) | (X << blue.offset)
855          *   RAMDAC[X] is programmed to (red, green, blue)
856          *
857          * Pseudocolor:
858          *    uses offset = 0 && length = DAC register width.
859          *    var->{color}.offset is 0
860          *    var->{color}.length contains widht of DAC
861          *    cmap is not used
862          *    DAC[X] is programmed to (red, green, blue)
863          * Truecolor:
864          *    does not use RAMDAC (usually has 3 of them).
865          *    var->{color}.offset contains start of bitfield
866          *    var->{color}.length contains length of bitfield
867          *    cmap is programmed to
868          *    (red << red.offset) | (green << green.offset) |
869          *    (blue << blue.offset) | (transp << transp.offset)
870          *    RAMDAC does not exist
871          */
872 #define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
873         switch (info->fix.visual) {
874         case FB_VISUAL_TRUECOLOR:
875         case FB_VISUAL_PSEUDOCOLOR:
876                 red = CNVT_TOHW(red, info->var.red.length);
877                 green = CNVT_TOHW(green, info->var.green.length);
878                 blue = CNVT_TOHW(blue, info->var.blue.length);
879                 transp = CNVT_TOHW(transp, info->var.transp.length);
880                 set_color(par, regno, red, green, blue);
881                 break;
882         case FB_VISUAL_DIRECTCOLOR:
883                 /* example here assumes 8 bit DAC. Might be different 
884                  * for your hardware */ 
885                 red = CNVT_TOHW(red, 8);       
886                 green = CNVT_TOHW(green, 8);
887                 blue = CNVT_TOHW(blue, 8);
888                 /* hey, there is bug in transp handling... */
889                 transp = CNVT_TOHW(transp, 8);
890                 break;
891         }
892 #undef CNVT_TOHW
893         /* Truecolor has hardware independent palette */
894         if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
895                 u32 v;
896
897                 if (regno >= 16)
898                         return 1;
899
900                 v = (red << info->var.red.offset) |
901                         (green << info->var.green.offset) |
902                         (blue << info->var.blue.offset) |
903                         (transp << info->var.transp.offset);
904
905                 switch (info->var.bits_per_pixel) {
906                 case 8:
907                         /* Yes some hand held devices have this. */ 
908                         ((u8*)(info->pseudo_palette))[regno] = v;
909                         break;  
910                 case 16:
911                         ((u16*)(info->pseudo_palette))[regno] = v;
912                         break;
913                 case 24:
914                 case 32:        
915                         ((u32*)(info->pseudo_palette))[regno] = v;
916                         break;
917                 }
918                 return 0;
919         }
920         /* ... */
921         return 0;
922 }
923
924 /**
925  *      pm2fb_pan_display - Pans the display.
926  *      @var: frame buffer variable screen structure
927  *      @info: frame buffer structure that represents a single frame buffer
928  *
929  *      Pan (or wrap, depending on the `vmode' field) the display using the
930  *      `xoffset' and `yoffset' fields of the `var' structure.
931  *      If the values don't fit, return -EINVAL.
932  *
933  *      Returns negative errno on error, or zero on success.
934  *
935  */
936 static int pm2fb_pan_display(struct fb_var_screeninfo *var,
937                              struct fb_info *info)
938 {
939         struct pm2fb_par *p = (struct pm2fb_par *) info->par;
940         u32 base;
941         u32 depth;
942         u32 xres;
943
944         xres = (var->xres + 31) & ~31;
945         depth = (var->bits_per_pixel + 7) & ~7;
946         depth = (depth > 32) ? 32 : depth;
947         base = to3264(var->yoffset * xres + var->xoffset, depth, 1);
948         WAIT_FIFO(p, 1);
949         pm2_WR(p, PM2R_SCREEN_BASE, base);    
950         return 0;
951 }
952
953 /**
954  *      pm2fb_blank - Blanks the display.
955  *      @blank_mode: the blank mode we want. 
956  *      @info: frame buffer structure that represents a single frame buffer
957  *
958  *      Blank the screen if blank_mode != 0, else unblank. Return 0 if
959  *      blanking succeeded, != 0 if un-/blanking failed due to e.g. a 
960  *      video mode which doesn't support it. Implements VESA suspend
961  *      and powerdown modes on hardware that supports disabling hsync/vsync:
962  *      blank_mode == 2: suspend vsync
963  *      blank_mode == 3: suspend hsync
964  *      blank_mode == 4: powerdown
965  *
966  *      Returns negative errno on error, or zero on success.
967  *
968  */
969 static int pm2fb_blank(int blank_mode, struct fb_info *info)
970 {
971         struct pm2fb_par *par = (struct pm2fb_par *) info->par;
972         u32 video = par->video;
973
974         DPRINTK("blank_mode %d\n", blank_mode);
975
976         /* Turn everything on, then disable as requested. */
977         video |= (PM2F_VIDEO_ENABLE | PM2F_HSYNC_MASK | PM2F_VSYNC_MASK);
978
979         switch (blank_mode) {
980         case 0:         /* Screen: On; HSync: On, VSync: On */
981                 break;
982         case 1:         /* Screen: Off; HSync: On, VSync: On */
983                 video &= ~PM2F_VIDEO_ENABLE;
984                 break;
985         case 2: /* Screen: Off; HSync: On, VSync: Off */
986                 video &= ~(PM2F_VIDEO_ENABLE | PM2F_VSYNC_MASK | PM2F_BLANK_LOW );
987                 break;
988         case 3: /* Screen: Off; HSync: Off, VSync: On */
989                 video &= ~(PM2F_VIDEO_ENABLE | PM2F_HSYNC_MASK | PM2F_BLANK_LOW );
990                 break;
991         case 4: /* Screen: Off; HSync: Off, VSync: Off */
992                 video &= ~(PM2F_VIDEO_ENABLE | PM2F_VSYNC_MASK | PM2F_HSYNC_MASK|
993                            PM2F_BLANK_LOW);
994                 break;
995         }
996         set_video(par, video);
997         return 0;
998 }
999
1000 /* ------------ Hardware Independent Functions ------------ */
1001
1002 /*
1003  *  Frame buffer operations
1004  */
1005
1006 static struct fb_ops pm2fb_ops = {
1007         .owner          = THIS_MODULE,
1008         .fb_check_var   = pm2fb_check_var,
1009         .fb_set_par     = pm2fb_set_par,
1010         .fb_setcolreg   = pm2fb_setcolreg,
1011         .fb_blank       = pm2fb_blank,
1012         .fb_pan_display = pm2fb_pan_display,
1013         .fb_fillrect    = cfb_fillrect,
1014         .fb_copyarea    = cfb_copyarea,
1015         .fb_imageblit   = cfb_imageblit,
1016         .fb_cursor      = soft_cursor,
1017 };
1018
1019 /*
1020  * PCI stuff
1021  */
1022
1023
1024 /**
1025  * Device initialisation
1026  *
1027  * Initialise and allocate resource for PCI device.
1028  *
1029  * @param       pdev    PCI device.
1030  * @param       id      PCI device ID.
1031  */
1032 static int __devinit pm2fb_probe(struct pci_dev *pdev,
1033                                  const struct pci_device_id *id)
1034 {
1035         struct pm2fb_par *default_par;
1036         struct fb_info *info;
1037         int size, err;
1038         u32 pci_mem_config;
1039         int err_retval = -ENXIO;
1040
1041         err = pci_enable_device(pdev);
1042         if ( err ) {
1043                 printk(KERN_WARNING "pm2fb: Can't enable pdev: %d\n", err);
1044                 return err;
1045         }
1046
1047         size = sizeof(struct pm2fb_par) + 256 * sizeof(u32);
1048         info = framebuffer_alloc(size, &pdev->dev);
1049         if ( !info )
1050                 return -ENOMEM;
1051         default_par = (struct pm2fb_par *) info->par;
1052
1053         switch (pdev->device) {
1054         case  PCI_DEVICE_ID_TI_TVP4020:
1055                 strcpy(pm2fb_fix.id, "TVP4020");
1056                 default_par->type = PM2_TYPE_PERMEDIA2;
1057                 break;
1058         case  PCI_DEVICE_ID_3DLABS_PERMEDIA2:
1059                 strcpy(pm2fb_fix.id, "Permedia2");
1060                 default_par->type = PM2_TYPE_PERMEDIA2;
1061                 break;
1062         case  PCI_DEVICE_ID_3DLABS_PERMEDIA2V:
1063                 strcpy(pm2fb_fix.id, "Permedia2v");
1064                 default_par->type = PM2_TYPE_PERMEDIA2V;
1065                 break;
1066         }
1067
1068         pm2fb_fix.mmio_start = pci_resource_start(pdev, 0);
1069         pm2fb_fix.mmio_len = PM2_REGS_SIZE;
1070
1071 #ifdef PM2FB_BE_APERTURE
1072         pm2fb_fix.mmio_start += PM2_REGS_SIZE;
1073 #endif
1074     
1075         /* Registers - request region and map it. */
1076         if ( !request_mem_region(pm2fb_fix.mmio_start, pm2fb_fix.mmio_len,
1077                                  "pm2fb regbase") ) {
1078                 printk(KERN_WARNING "pm2fb: Can't reserve regbase.\n");
1079                 goto err_exit_neither;
1080         }
1081         default_par->v_regs =
1082                 ioremap_nocache(pm2fb_fix.mmio_start, pm2fb_fix.mmio_len);
1083         if ( !default_par->v_regs ) {
1084                 printk(KERN_WARNING "pm2fb: Can't remap %s register area.\n",
1085                        pm2fb_fix.id);
1086                 release_mem_region(pm2fb_fix.mmio_start, pm2fb_fix.mmio_len);
1087                 goto err_exit_neither;
1088         }
1089
1090         /* Now work out how big lfb is going to be. */
1091         pci_mem_config = RD32(default_par->v_regs, PM2R_MEM_CONFIG);
1092         switch(pci_mem_config & PM2F_MEM_CONFIG_RAM_MASK) {
1093         case PM2F_MEM_BANKS_1:
1094                 default_par->fb_size=0x200000;
1095                 break;
1096         case PM2F_MEM_BANKS_2:
1097                 default_par->fb_size=0x400000;
1098                 break;
1099         case PM2F_MEM_BANKS_3:
1100                 default_par->fb_size=0x600000;
1101                 break;
1102         case PM2F_MEM_BANKS_4:
1103                 default_par->fb_size=0x800000;
1104                 break;
1105         }
1106         default_par->memclock = CVPPC_MEMCLOCK;
1107         pm2fb_fix.smem_start = pci_resource_start(pdev, 1);
1108         pm2fb_fix.smem_len = default_par->fb_size;
1109
1110         /* Linear frame buffer - request region and map it. */
1111         if ( !request_mem_region(pm2fb_fix.smem_start, pm2fb_fix.smem_len,
1112                                  "pm2fb smem") ) {
1113                 printk(KERN_WARNING "pm2fb: Can't reserve smem.\n");
1114                 goto err_exit_mmio;
1115         }
1116         info->screen_base = default_par->v_fb =
1117                 ioremap_nocache(pm2fb_fix.smem_start, pm2fb_fix.smem_len);
1118         if ( !default_par->v_fb ) {
1119                 printk(KERN_WARNING "pm2fb: Can't ioremap smem area.\n");
1120                 release_mem_region(pm2fb_fix.smem_start, pm2fb_fix.smem_len);
1121                 goto err_exit_mmio;
1122         }
1123
1124         info->fbops             = &pm2fb_ops;
1125         info->fix               = pm2fb_fix;    
1126         info->pseudo_palette    = (void *)(default_par + 1); 
1127         info->flags             = FBINFO_FLAG_DEFAULT;
1128
1129 #ifndef MODULE
1130         if (!mode)
1131                 mode = "640x480@60";
1132          
1133         err = fb_find_mode(&info->var, info, mode, NULL, 0, NULL, 8); 
1134         if (!err || err == 4)
1135 #endif
1136                 info->var = pm2fb_var;
1137
1138         if (fb_alloc_cmap(&info->cmap, 256, 0) < 0)
1139                 goto err_exit_all;
1140
1141         if (register_framebuffer(info) < 0)
1142                 goto err_exit_both;
1143
1144         printk(KERN_INFO "fb%d: %s frame buffer device, memory = %dK.\n",
1145                info->node, info->fix.id, default_par->fb_size / 1024);
1146
1147         /*
1148          * Our driver data
1149          */
1150         pci_set_drvdata(pdev, info);
1151
1152         return 0;
1153
1154  err_exit_all:
1155         fb_dealloc_cmap(&info->cmap);   
1156  err_exit_both:    
1157         iounmap((void*) pm2fb_fix.smem_start);
1158         release_mem_region(pm2fb_fix.smem_start, pm2fb_fix.smem_len);
1159  err_exit_mmio:
1160         iounmap((void*) pm2fb_fix.mmio_start);
1161         release_mem_region(pm2fb_fix.mmio_start, pm2fb_fix.mmio_len);
1162  err_exit_neither:
1163         framebuffer_release(info);
1164         return err_retval;
1165 }
1166
1167 /**
1168  * Device removal.
1169  *
1170  * Release all device resources.
1171  *
1172  * @param       pdev    PCI device to clean up.
1173  */
1174 static void __devexit pm2fb_remove(struct pci_dev *pdev)
1175 {
1176         struct fb_info* info = pci_get_drvdata(pdev);
1177         struct fb_fix_screeninfo* fix = &info->fix;
1178     
1179         unregister_framebuffer(info);
1180     
1181         iounmap((void*) fix->smem_start);
1182         release_mem_region(fix->smem_start, fix->smem_len);
1183         iounmap((void*) fix->mmio_start);
1184         release_mem_region(fix->mmio_start, fix->mmio_len);
1185
1186         pci_set_drvdata(pdev, NULL);
1187         kfree(info);
1188 }
1189
1190 static struct pci_device_id pm2fb_id_table[] = {
1191         { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_TVP4020,
1192           PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
1193           0xff0000, 0 },
1194         { PCI_VENDOR_ID_3DLABS, PCI_DEVICE_ID_3DLABS_PERMEDIA2,
1195           PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
1196           0xff0000, 0 },
1197         { PCI_VENDOR_ID_3DLABS, PCI_DEVICE_ID_3DLABS_PERMEDIA2V,
1198           PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
1199           0xff0000, 0 },
1200         { 0, }
1201 };
1202
1203 static struct pci_driver pm2fb_driver = {
1204         .name           = "pm2fb",
1205         .id_table       = pm2fb_id_table,
1206         .probe          = pm2fb_probe,
1207         .remove         = __devexit_p(pm2fb_remove),
1208 };
1209
1210 MODULE_DEVICE_TABLE(pci, pm2fb_id_table);
1211
1212
1213 /*
1214  *  Initialization
1215  */
1216
1217 int __init pm2fb_init(void)
1218 {
1219         return pci_module_init(&pm2fb_driver);
1220 }
1221
1222 /*
1223  *  Cleanup
1224  */
1225
1226 static void __exit pm2fb_exit(void)
1227 {
1228         pci_unregister_driver(&pm2fb_driver);
1229 }
1230
1231 /*
1232  *  Setup
1233  */
1234
1235 /**
1236  * Parse user speficied options.
1237  *
1238  * This is, comma-separated options following `video=pm2fb:'.
1239  */
1240 int __init pm2fb_setup(char *options)
1241 {
1242         char* this_opt;
1243
1244         if (!options || !*options)
1245                 return 0;
1246
1247         while ((this_opt = strsep(&options, ",")) != NULL) {    
1248                 if (!*this_opt)
1249                         continue;
1250                 if(!strcmp(this_opt, "lowhsync")) {
1251                         lowhsync = 1;
1252                 } else if(!strcmp(this_opt, "lowvsync")) {
1253                         lowvsync = 1;
1254                 } else {
1255                         mode = this_opt;
1256                 }
1257         }
1258         return 0;
1259 }
1260
1261
1262 /* ------------------------------------------------------------------------- */
1263
1264 /* ------------------------------------------------------------------------- */
1265
1266
1267
1268 #ifdef MODULE
1269 module_init(pm2fb_init);
1270 #endif 
1271 module_exit(pm2fb_exit);
1272
1273 MODULE_PARM(mode,"s");
1274 MODULE_PARM(lowhsync,"i");
1275 MODULE_PARM(lowvsync,"i");
1276
1277 MODULE_AUTHOR("Jim Hague <jim.hague@acm.org>");
1278 MODULE_DESCRIPTION("Permedia2 framebuffer device driver");
1279 MODULE_LICENSE("GPL");