GNU Linux-libre 4.9.333-gnu1
[releases.git] / drivers / video / fbdev / 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  * I have a Creative Graphics Blaster Exxtreme card - pm2fb on x86. I
15  * have no access to other pm2fb implementations. Sparc (and thus
16  * hopefully other big-endian) devices now work, thanks to a lot of
17  * testing work by Ron Murray. I have no access to CVision hardware,
18  * and therefore for now I am omitting the 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/module.h>
31 #include <linux/moduleparam.h>
32 #include <linux/kernel.h>
33 #include <linux/errno.h>
34 #include <linux/string.h>
35 #include <linux/mm.h>
36 #include <linux/slab.h>
37 #include <linux/delay.h>
38 #include <linux/fb.h>
39 #include <linux/init.h>
40 #include <linux/pci.h>
41 #include <video/permedia2.h>
42 #include <video/cvisionppc.h>
43
44 #if !defined(__LITTLE_ENDIAN) && !defined(__BIG_ENDIAN)
45 #error  "The endianness of the target host has not been defined."
46 #endif
47
48 #if !defined(CONFIG_PCI)
49 #error "Only generic PCI cards supported."
50 #endif
51
52 #undef PM2FB_MASTER_DEBUG
53 #ifdef PM2FB_MASTER_DEBUG
54 #define DPRINTK(a, b...)        \
55         printk(KERN_DEBUG "pm2fb: %s: " a, __func__ , ## b)
56 #else
57 #define DPRINTK(a, b...)
58 #endif
59
60 #define PM2_PIXMAP_SIZE (1600 * 4)
61
62 /*
63  * Driver data
64  */
65 static int hwcursor = 1;
66 static char *mode_option;
67
68 /*
69  * The XFree GLINT driver will (I think to implement hardware cursor
70  * support on TVP4010 and similar where there is no RAMDAC - see
71  * comment in set_video) always request +ve sync regardless of what
72  * the mode requires. This screws me because I have a Sun
73  * fixed-frequency monitor which absolutely has to have -ve sync. So
74  * these flags allow the user to specify that requests for +ve sync
75  * should be silently turned in -ve sync.
76  */
77 static bool lowhsync;
78 static bool lowvsync;
79 static bool noaccel;
80 static bool nomtrr;
81
82 /*
83  * The hardware state of the graphics card that isn't part of the
84  * screeninfo.
85  */
86 struct pm2fb_par
87 {
88         pm2type_t       type;           /* Board type */
89         unsigned char   __iomem *v_regs;/* virtual address of p_regs */
90         u32             memclock;       /* memclock */
91         u32             video;          /* video flags before blanking */
92         u32             mem_config;     /* MemConfig reg at probe */
93         u32             mem_control;    /* MemControl reg at probe */
94         u32             boot_address;   /* BootAddress reg at probe */
95         u32             palette[16];
96         int             wc_cookie;
97 };
98
99 /*
100  * Here we define the default structs fb_fix_screeninfo and fb_var_screeninfo
101  * if we don't use modedb.
102  */
103 static struct fb_fix_screeninfo pm2fb_fix = {
104         .id =           "",
105         .type =         FB_TYPE_PACKED_PIXELS,
106         .visual =       FB_VISUAL_PSEUDOCOLOR,
107         .xpanstep =     1,
108         .ypanstep =     1,
109         .ywrapstep =    0,
110         .accel =        FB_ACCEL_3DLABS_PERMEDIA2,
111 };
112
113 /*
114  * Default video mode. In case the modedb doesn't work.
115  */
116 static const struct fb_var_screeninfo pm2fb_var = {
117         /* "640x480, 8 bpp @ 60 Hz */
118         .xres =                 640,
119         .yres =                 480,
120         .xres_virtual =         640,
121         .yres_virtual =         480,
122         .bits_per_pixel =       8,
123         .red =                  {0, 8, 0},
124         .blue =                 {0, 8, 0},
125         .green =                {0, 8, 0},
126         .activate =             FB_ACTIVATE_NOW,
127         .height =               -1,
128         .width =                -1,
129         .accel_flags =          0,
130         .pixclock =             39721,
131         .left_margin =          40,
132         .right_margin =         24,
133         .upper_margin =         32,
134         .lower_margin =         11,
135         .hsync_len =            96,
136         .vsync_len =            2,
137         .vmode =                FB_VMODE_NONINTERLACED
138 };
139
140 /*
141  * Utility functions
142  */
143
144 static inline u32 pm2_RD(struct pm2fb_par *p, s32 off)
145 {
146         return fb_readl(p->v_regs + off);
147 }
148
149 static inline void pm2_WR(struct pm2fb_par *p, s32 off, u32 v)
150 {
151         fb_writel(v, p->v_regs + off);
152 }
153
154 static inline u32 pm2_RDAC_RD(struct pm2fb_par *p, s32 idx)
155 {
156         pm2_WR(p, PM2R_RD_PALETTE_WRITE_ADDRESS, idx);
157         mb();
158         return pm2_RD(p, PM2R_RD_INDEXED_DATA);
159 }
160
161 static inline u32 pm2v_RDAC_RD(struct pm2fb_par *p, s32 idx)
162 {
163         pm2_WR(p, PM2VR_RD_INDEX_LOW, idx & 0xff);
164         mb();
165         return pm2_RD(p,  PM2VR_RD_INDEXED_DATA);
166 }
167
168 static inline void pm2_RDAC_WR(struct pm2fb_par *p, s32 idx, u32 v)
169 {
170         pm2_WR(p, PM2R_RD_PALETTE_WRITE_ADDRESS, idx);
171         wmb();
172         pm2_WR(p, PM2R_RD_INDEXED_DATA, v);
173         wmb();
174 }
175
176 static inline void pm2v_RDAC_WR(struct pm2fb_par *p, s32 idx, u32 v)
177 {
178         pm2_WR(p, PM2VR_RD_INDEX_LOW, idx & 0xff);
179         wmb();
180         pm2_WR(p, PM2VR_RD_INDEXED_DATA, v);
181         wmb();
182 }
183
184 #ifdef CONFIG_FB_PM2_FIFO_DISCONNECT
185 #define WAIT_FIFO(p, a)
186 #else
187 static inline void WAIT_FIFO(struct pm2fb_par *p, u32 a)
188 {
189         while (pm2_RD(p, PM2R_IN_FIFO_SPACE) < a)
190                 cpu_relax();
191 }
192 #endif
193
194 /*
195  * partial products for the supported horizontal resolutions.
196  */
197 #define PACKPP(p0, p1, p2)      (((p2) << 6) | ((p1) << 3) | (p0))
198 static const struct {
199         u16 width;
200         u16 pp;
201 } pp_table[] = {
202         { 32,   PACKPP(1, 0, 0) }, { 64,        PACKPP(1, 1, 0) },
203         { 96,   PACKPP(1, 1, 1) }, { 128,       PACKPP(2, 1, 1) },
204         { 160,  PACKPP(2, 2, 1) }, { 192,       PACKPP(2, 2, 2) },
205         { 224,  PACKPP(3, 2, 1) }, { 256,       PACKPP(3, 2, 2) },
206         { 288,  PACKPP(3, 3, 1) }, { 320,       PACKPP(3, 3, 2) },
207         { 384,  PACKPP(3, 3, 3) }, { 416,       PACKPP(4, 3, 1) },
208         { 448,  PACKPP(4, 3, 2) }, { 512,       PACKPP(4, 3, 3) },
209         { 544,  PACKPP(4, 4, 1) }, { 576,       PACKPP(4, 4, 2) },
210         { 640,  PACKPP(4, 4, 3) }, { 768,       PACKPP(4, 4, 4) },
211         { 800,  PACKPP(5, 4, 1) }, { 832,       PACKPP(5, 4, 2) },
212         { 896,  PACKPP(5, 4, 3) }, { 1024,      PACKPP(5, 4, 4) },
213         { 1056, PACKPP(5, 5, 1) }, { 1088,      PACKPP(5, 5, 2) },
214         { 1152, PACKPP(5, 5, 3) }, { 1280,      PACKPP(5, 5, 4) },
215         { 1536, PACKPP(5, 5, 5) }, { 1568,      PACKPP(6, 5, 1) },
216         { 1600, PACKPP(6, 5, 2) }, { 1664,      PACKPP(6, 5, 3) },
217         { 1792, PACKPP(6, 5, 4) }, { 2048,      PACKPP(6, 5, 5) },
218         { 0,    0 } };
219
220 static u32 partprod(u32 xres)
221 {
222         int i;
223
224         for (i = 0; pp_table[i].width && pp_table[i].width != xres; i++)
225                 ;
226         if (pp_table[i].width == 0)
227                 DPRINTK("invalid width %u\n", xres);
228         return pp_table[i].pp;
229 }
230
231 static u32 to3264(u32 timing, int bpp, int is64)
232 {
233         switch (bpp) {
234         case 24:
235                 timing *= 3;
236         case 8:
237                 timing >>= 1;
238         case 16:
239                 timing >>= 1;
240         case 32:
241                 break;
242         }
243         if (is64)
244                 timing >>= 1;
245         return timing;
246 }
247
248 static void pm2_mnp(u32 clk, unsigned char *mm, unsigned char *nn,
249                     unsigned char *pp)
250 {
251         unsigned char m;
252         unsigned char n;
253         unsigned char p;
254         u32 f;
255         s32 curr;
256         s32 delta = 100000;
257
258         *mm = *nn = *pp = 0;
259         for (n = 2; n < 15; n++) {
260                 for (m = 2; m; m++) {
261                         f = PM2_REFERENCE_CLOCK * m / n;
262                         if (f >= 150000 && f <= 300000) {
263                                 for (p = 0; p < 5; p++, f >>= 1) {
264                                         curr = (clk > f) ? clk - f : f - clk;
265                                         if (curr < delta) {
266                                                 delta = curr;
267                                                 *mm = m;
268                                                 *nn = n;
269                                                 *pp = p;
270                                         }
271                                 }
272                         }
273                 }
274         }
275 }
276
277 static void pm2v_mnp(u32 clk, unsigned char *mm, unsigned char *nn,
278                      unsigned char *pp)
279 {
280         unsigned char m;
281         unsigned char n;
282         unsigned char p;
283         u32 f;
284         s32 delta = 1000;
285
286         *mm = *nn = *pp = 0;
287         for (m = 1; m < 128; m++) {
288                 for (n = 2 * m + 1; n; n++) {
289                         for (p = 0; p < 2; p++) {
290                                 f = (PM2_REFERENCE_CLOCK >> (p + 1)) * n / m;
291                                 if (clk > f - delta && clk < f + delta) {
292                                         delta = (clk > f) ? clk - f : f - clk;
293                                         *mm = m;
294                                         *nn = n;
295                                         *pp = p;
296                                 }
297                         }
298                 }
299         }
300 }
301
302 static void clear_palette(struct pm2fb_par *p)
303 {
304         int i = 256;
305
306         WAIT_FIFO(p, 1);
307         pm2_WR(p, PM2R_RD_PALETTE_WRITE_ADDRESS, 0);
308         wmb();
309         while (i--) {
310                 WAIT_FIFO(p, 3);
311                 pm2_WR(p, PM2R_RD_PALETTE_DATA, 0);
312                 pm2_WR(p, PM2R_RD_PALETTE_DATA, 0);
313                 pm2_WR(p, PM2R_RD_PALETTE_DATA, 0);
314         }
315 }
316
317 static void reset_card(struct pm2fb_par *p)
318 {
319         if (p->type == PM2_TYPE_PERMEDIA2V)
320                 pm2_WR(p, PM2VR_RD_INDEX_HIGH, 0);
321         pm2_WR(p, PM2R_RESET_STATUS, 0);
322         mb();
323         while (pm2_RD(p, PM2R_RESET_STATUS) & PM2F_BEING_RESET)
324                 cpu_relax();
325         mb();
326 #ifdef CONFIG_FB_PM2_FIFO_DISCONNECT
327         DPRINTK("FIFO disconnect enabled\n");
328         pm2_WR(p, PM2R_FIFO_DISCON, 1);
329         mb();
330 #endif
331
332         /* Restore stashed memory config information from probe */
333         WAIT_FIFO(p, 3);
334         pm2_WR(p, PM2R_MEM_CONTROL, p->mem_control);
335         pm2_WR(p, PM2R_BOOT_ADDRESS, p->boot_address);
336         wmb();
337         pm2_WR(p, PM2R_MEM_CONFIG, p->mem_config);
338 }
339
340 static void reset_config(struct pm2fb_par *p)
341 {
342         WAIT_FIFO(p, 53);
343         pm2_WR(p, PM2R_CHIP_CONFIG, pm2_RD(p, PM2R_CHIP_CONFIG) &
344                         ~(PM2F_VGA_ENABLE | PM2F_VGA_FIXED));
345         pm2_WR(p, PM2R_BYPASS_WRITE_MASK, ~(0L));
346         pm2_WR(p, PM2R_FRAMEBUFFER_WRITE_MASK, ~(0L));
347         pm2_WR(p, PM2R_FIFO_CONTROL, 0);
348         pm2_WR(p, PM2R_APERTURE_ONE, 0);
349         pm2_WR(p, PM2R_APERTURE_TWO, 0);
350         pm2_WR(p, PM2R_RASTERIZER_MODE, 0);
351         pm2_WR(p, PM2R_DELTA_MODE, PM2F_DELTA_ORDER_RGB);
352         pm2_WR(p, PM2R_LB_READ_FORMAT, 0);
353         pm2_WR(p, PM2R_LB_WRITE_FORMAT, 0);
354         pm2_WR(p, PM2R_LB_READ_MODE, 0);
355         pm2_WR(p, PM2R_LB_SOURCE_OFFSET, 0);
356         pm2_WR(p, PM2R_FB_SOURCE_OFFSET, 0);
357         pm2_WR(p, PM2R_FB_PIXEL_OFFSET, 0);
358         pm2_WR(p, PM2R_FB_WINDOW_BASE, 0);
359         pm2_WR(p, PM2R_LB_WINDOW_BASE, 0);
360         pm2_WR(p, PM2R_FB_SOFT_WRITE_MASK, ~(0L));
361         pm2_WR(p, PM2R_FB_HARD_WRITE_MASK, ~(0L));
362         pm2_WR(p, PM2R_FB_READ_PIXEL, 0);
363         pm2_WR(p, PM2R_DITHER_MODE, 0);
364         pm2_WR(p, PM2R_AREA_STIPPLE_MODE, 0);
365         pm2_WR(p, PM2R_DEPTH_MODE, 0);
366         pm2_WR(p, PM2R_STENCIL_MODE, 0);
367         pm2_WR(p, PM2R_TEXTURE_ADDRESS_MODE, 0);
368         pm2_WR(p, PM2R_TEXTURE_READ_MODE, 0);
369         pm2_WR(p, PM2R_TEXEL_LUT_MODE, 0);
370         pm2_WR(p, PM2R_YUV_MODE, 0);
371         pm2_WR(p, PM2R_COLOR_DDA_MODE, 0);
372         pm2_WR(p, PM2R_TEXTURE_COLOR_MODE, 0);
373         pm2_WR(p, PM2R_FOG_MODE, 0);
374         pm2_WR(p, PM2R_ALPHA_BLEND_MODE, 0);
375         pm2_WR(p, PM2R_LOGICAL_OP_MODE, 0);
376         pm2_WR(p, PM2R_STATISTICS_MODE, 0);
377         pm2_WR(p, PM2R_SCISSOR_MODE, 0);
378         pm2_WR(p, PM2R_FILTER_MODE, PM2F_SYNCHRONIZATION);
379         pm2_WR(p, PM2R_RD_PIXEL_MASK, 0xff);
380         switch (p->type) {
381         case PM2_TYPE_PERMEDIA2:
382                 pm2_RDAC_WR(p, PM2I_RD_MODE_CONTROL, 0); /* no overlay */
383                 pm2_RDAC_WR(p, PM2I_RD_CURSOR_CONTROL, 0);
384                 pm2_RDAC_WR(p, PM2I_RD_MISC_CONTROL, PM2F_RD_PALETTE_WIDTH_8);
385                 pm2_RDAC_WR(p, PM2I_RD_COLOR_KEY_CONTROL, 0);
386                 pm2_RDAC_WR(p, PM2I_RD_OVERLAY_KEY, 0);
387                 pm2_RDAC_WR(p, PM2I_RD_RED_KEY, 0);
388                 pm2_RDAC_WR(p, PM2I_RD_GREEN_KEY, 0);
389                 pm2_RDAC_WR(p, PM2I_RD_BLUE_KEY, 0);
390                 break;
391         case PM2_TYPE_PERMEDIA2V:
392                 pm2v_RDAC_WR(p, PM2VI_RD_MISC_CONTROL, 1); /* 8bit */
393                 break;
394         }
395 }
396
397 static void set_aperture(struct pm2fb_par *p, u32 depth)
398 {
399         /*
400          * The hardware is little-endian. When used in big-endian
401          * hosts, the on-chip aperture settings are used where
402          * possible to translate from host to card byte order.
403          */
404         WAIT_FIFO(p, 2);
405 #ifdef __LITTLE_ENDIAN
406         pm2_WR(p, PM2R_APERTURE_ONE, PM2F_APERTURE_STANDARD);
407 #else
408         switch (depth) {
409         case 24:        /* RGB->BGR */
410                 /*
411                  * We can't use the aperture to translate host to
412                  * card byte order here, so we switch to BGR mode
413                  * in pm2fb_set_par().
414                  */
415         case 8:         /* B->B */
416                 pm2_WR(p, PM2R_APERTURE_ONE, PM2F_APERTURE_STANDARD);
417                 break;
418         case 16:        /* HL->LH */
419                 pm2_WR(p, PM2R_APERTURE_ONE, PM2F_APERTURE_HALFWORDSWAP);
420                 break;
421         case 32:        /* RGBA->ABGR */
422                 pm2_WR(p, PM2R_APERTURE_ONE, PM2F_APERTURE_BYTESWAP);
423                 break;
424         }
425 #endif
426
427         /* We don't use aperture two, so this may be superflous */
428         pm2_WR(p, PM2R_APERTURE_TWO, PM2F_APERTURE_STANDARD);
429 }
430
431 static void set_color(struct pm2fb_par *p, unsigned char regno,
432                       unsigned char r, unsigned char g, unsigned char b)
433 {
434         WAIT_FIFO(p, 4);
435         pm2_WR(p, PM2R_RD_PALETTE_WRITE_ADDRESS, regno);
436         wmb();
437         pm2_WR(p, PM2R_RD_PALETTE_DATA, r);
438         wmb();
439         pm2_WR(p, PM2R_RD_PALETTE_DATA, g);
440         wmb();
441         pm2_WR(p, PM2R_RD_PALETTE_DATA, b);
442 }
443
444 static void set_memclock(struct pm2fb_par *par, u32 clk)
445 {
446         int i;
447         unsigned char m, n, p;
448
449         switch (par->type) {
450         case PM2_TYPE_PERMEDIA2V:
451                 pm2v_mnp(clk/2, &m, &n, &p);
452                 WAIT_FIFO(par, 12);
453                 pm2_WR(par, PM2VR_RD_INDEX_HIGH, PM2VI_RD_MCLK_CONTROL >> 8);
454                 pm2v_RDAC_WR(par, PM2VI_RD_MCLK_CONTROL, 0);
455                 pm2v_RDAC_WR(par, PM2VI_RD_MCLK_PRESCALE, m);
456                 pm2v_RDAC_WR(par, PM2VI_RD_MCLK_FEEDBACK, n);
457                 pm2v_RDAC_WR(par, PM2VI_RD_MCLK_POSTSCALE, p);
458                 pm2v_RDAC_WR(par, PM2VI_RD_MCLK_CONTROL, 1);
459                 rmb();
460                 for (i = 256; i; i--)
461                         if (pm2v_RDAC_RD(par, PM2VI_RD_MCLK_CONTROL) & 2)
462                                 break;
463                 pm2_WR(par, PM2VR_RD_INDEX_HIGH, 0);
464                 break;
465         case PM2_TYPE_PERMEDIA2:
466                 pm2_mnp(clk, &m, &n, &p);
467                 WAIT_FIFO(par, 10);
468                 pm2_RDAC_WR(par, PM2I_RD_MEMORY_CLOCK_3, 6);
469                 pm2_RDAC_WR(par, PM2I_RD_MEMORY_CLOCK_1, m);
470                 pm2_RDAC_WR(par, PM2I_RD_MEMORY_CLOCK_2, n);
471                 pm2_RDAC_WR(par, PM2I_RD_MEMORY_CLOCK_3, 8|p);
472                 pm2_RDAC_RD(par, PM2I_RD_MEMORY_CLOCK_STATUS);
473                 rmb();
474                 for (i = 256; i; i--)
475                         if (pm2_RD(par, PM2R_RD_INDEXED_DATA) & PM2F_PLL_LOCKED)
476                                 break;
477                 break;
478         }
479 }
480
481 static void set_pixclock(struct pm2fb_par *par, u32 clk)
482 {
483         int i;
484         unsigned char m, n, p;
485
486         switch (par->type) {
487         case PM2_TYPE_PERMEDIA2:
488                 pm2_mnp(clk, &m, &n, &p);
489                 WAIT_FIFO(par, 10);
490                 pm2_RDAC_WR(par, PM2I_RD_PIXEL_CLOCK_A3, 0);
491                 pm2_RDAC_WR(par, PM2I_RD_PIXEL_CLOCK_A1, m);
492                 pm2_RDAC_WR(par, PM2I_RD_PIXEL_CLOCK_A2, n);
493                 pm2_RDAC_WR(par, PM2I_RD_PIXEL_CLOCK_A3, 8|p);
494                 pm2_RDAC_RD(par, PM2I_RD_PIXEL_CLOCK_STATUS);
495                 rmb();
496                 for (i = 256; i; i--)
497                         if (pm2_RD(par, PM2R_RD_INDEXED_DATA) & PM2F_PLL_LOCKED)
498                                 break;
499                 break;
500         case PM2_TYPE_PERMEDIA2V:
501                 pm2v_mnp(clk/2, &m, &n, &p);
502                 WAIT_FIFO(par, 8);
503                 pm2_WR(par, PM2VR_RD_INDEX_HIGH, PM2VI_RD_CLK0_PRESCALE >> 8);
504                 pm2v_RDAC_WR(par, PM2VI_RD_CLK0_PRESCALE, m);
505                 pm2v_RDAC_WR(par, PM2VI_RD_CLK0_FEEDBACK, n);
506                 pm2v_RDAC_WR(par, PM2VI_RD_CLK0_POSTSCALE, p);
507                 pm2_WR(par, PM2VR_RD_INDEX_HIGH, 0);
508                 break;
509         }
510 }
511
512 static void set_video(struct pm2fb_par *p, u32 video)
513 {
514         u32 tmp;
515         u32 vsync = video;
516
517         DPRINTK("video = 0x%x\n", video);
518
519         /*
520          * The hardware cursor needs +vsync to recognise vert retrace.
521          * We may not be using the hardware cursor, but the X Glint
522          * driver may well. So always set +hsync/+vsync and then set
523          * the RAMDAC to invert the sync if necessary.
524          */
525         vsync &= ~(PM2F_HSYNC_MASK | PM2F_VSYNC_MASK);
526         vsync |= PM2F_HSYNC_ACT_HIGH | PM2F_VSYNC_ACT_HIGH;
527
528         WAIT_FIFO(p, 3);
529         pm2_WR(p, PM2R_VIDEO_CONTROL, vsync);
530
531         switch (p->type) {
532         case PM2_TYPE_PERMEDIA2:
533                 tmp = PM2F_RD_PALETTE_WIDTH_8;
534                 if ((video & PM2F_HSYNC_MASK) == PM2F_HSYNC_ACT_LOW)
535                         tmp |= 4; /* invert hsync */
536                 if ((video & PM2F_VSYNC_MASK) == PM2F_VSYNC_ACT_LOW)
537                         tmp |= 8; /* invert vsync */
538                 pm2_RDAC_WR(p, PM2I_RD_MISC_CONTROL, tmp);
539                 break;
540         case PM2_TYPE_PERMEDIA2V:
541                 tmp = 0;
542                 if ((video & PM2F_HSYNC_MASK) == PM2F_HSYNC_ACT_LOW)
543                         tmp |= 1; /* invert hsync */
544                 if ((video & PM2F_VSYNC_MASK) == PM2F_VSYNC_ACT_LOW)
545                         tmp |= 4; /* invert vsync */
546                 pm2v_RDAC_WR(p, PM2VI_RD_SYNC_CONTROL, tmp);
547                 break;
548         }
549 }
550
551 /*
552  *      pm2fb_check_var - Optional function. Validates a var passed in.
553  *      @var: frame buffer variable screen structure
554  *      @info: frame buffer structure that represents a single frame buffer
555  *
556  *      Checks to see if the hardware supports the state requested by
557  *      var passed in.
558  *
559  *      Returns negative errno on error, or zero on success.
560  */
561 static int pm2fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
562 {
563         u32 lpitch;
564
565         if (var->bits_per_pixel != 8  && var->bits_per_pixel != 16 &&
566             var->bits_per_pixel != 24 && var->bits_per_pixel != 32) {
567                 DPRINTK("depth not supported: %u\n", var->bits_per_pixel);
568                 return -EINVAL;
569         }
570
571         if (var->xres != var->xres_virtual) {
572                 DPRINTK("virtual x resolution != "
573                         "physical x resolution not supported\n");
574                 return -EINVAL;
575         }
576
577         if (var->yres > var->yres_virtual) {
578                 DPRINTK("virtual y resolution < "
579                         "physical y resolution not possible\n");
580                 return -EINVAL;
581         }
582
583         /* permedia cannot blit over 2048 */
584         if (var->yres_virtual > 2047) {
585                 var->yres_virtual = 2047;
586         }
587
588         if (var->xoffset) {
589                 DPRINTK("xoffset not supported\n");
590                 return -EINVAL;
591         }
592
593         if ((var->vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) {
594                 DPRINTK("interlace not supported\n");
595                 return -EINVAL;
596         }
597
598         var->xres = (var->xres + 15) & ~15; /* could sometimes be 8 */
599         lpitch = var->xres * ((var->bits_per_pixel + 7) >> 3);
600
601         if (var->xres < 320 || var->xres > 1600) {
602                 DPRINTK("width not supported: %u\n", var->xres);
603                 return -EINVAL;
604         }
605
606         if (var->yres < 200 || var->yres > 1200) {
607                 DPRINTK("height not supported: %u\n", var->yres);
608                 return -EINVAL;
609         }
610
611         if (lpitch * var->yres_virtual > info->fix.smem_len) {
612                 DPRINTK("no memory for screen (%ux%ux%u)\n",
613                         var->xres, var->yres_virtual, var->bits_per_pixel);
614                 return -EINVAL;
615         }
616
617         if (!var->pixclock) {
618                 DPRINTK("pixclock is zero\n");
619                 return -EINVAL;
620         }
621
622         if (!var->pixclock) {
623                 DPRINTK("pixclock is zero\n");
624                 return -EINVAL;
625         }
626
627         if (PICOS2KHZ(var->pixclock) > PM2_MAX_PIXCLOCK) {
628                 DPRINTK("pixclock too high (%ldKHz)\n",
629                         PICOS2KHZ(var->pixclock));
630                 return -EINVAL;
631         }
632
633         var->transp.offset = 0;
634         var->transp.length = 0;
635         switch (var->bits_per_pixel) {
636         case 8:
637                 var->red.length = 8;
638                 var->green.length = 8;
639                 var->blue.length = 8;
640                 break;
641         case 16:
642                 var->red.offset   = 11;
643                 var->red.length   = 5;
644                 var->green.offset = 5;
645                 var->green.length = 6;
646                 var->blue.offset  = 0;
647                 var->blue.length  = 5;
648                 break;
649         case 32:
650                 var->transp.offset = 24;
651                 var->transp.length = 8;
652                 var->red.offset   = 16;
653                 var->green.offset = 8;
654                 var->blue.offset  = 0;
655                 var->red.length = 8;
656                 var->green.length = 8;
657                 var->blue.length = 8;
658                 break;
659         case 24:
660 #ifdef __BIG_ENDIAN
661                 var->red.offset   = 0;
662                 var->blue.offset  = 16;
663 #else
664                 var->red.offset   = 16;
665                 var->blue.offset  = 0;
666 #endif
667                 var->green.offset = 8;
668                 var->red.length = 8;
669                 var->green.length = 8;
670                 var->blue.length = 8;
671                 break;
672         }
673         var->height = -1;
674         var->width = -1;
675
676         var->accel_flags = 0;   /* Can't mmap if this is on */
677
678         DPRINTK("Checking graphics mode at %dx%d depth %d\n",
679                 var->xres, var->yres, var->bits_per_pixel);
680         return 0;
681 }
682
683 /**
684  *      pm2fb_set_par - Alters the hardware state.
685  *      @info: frame buffer structure that represents a single frame buffer
686  *
687  *      Using the fb_var_screeninfo in fb_info we set the resolution of the
688  *      this particular framebuffer.
689  */
690 static int pm2fb_set_par(struct fb_info *info)
691 {
692         struct pm2fb_par *par = info->par;
693         u32 pixclock;
694         u32 width = (info->var.xres_virtual + 7) & ~7;
695         u32 height = info->var.yres_virtual;
696         u32 depth = (info->var.bits_per_pixel + 7) & ~7;
697         u32 hsstart, hsend, hbend, htotal;
698         u32 vsstart, vsend, vbend, vtotal;
699         u32 stride;
700         u32 base;
701         u32 video = 0;
702         u32 clrmode = PM2F_RD_COLOR_MODE_RGB | PM2F_RD_GUI_ACTIVE;
703         u32 txtmap = 0;
704         u32 pixsize = 0;
705         u32 clrformat = 0;
706         u32 misc = 1; /* 8-bit DAC */
707         u32 xres = (info->var.xres + 31) & ~31;
708         int data64;
709
710         reset_card(par);
711         reset_config(par);
712         clear_palette(par);
713         if (par->memclock)
714                 set_memclock(par, par->memclock);
715
716         depth = (depth > 32) ? 32 : depth;
717         data64 = depth > 8 || par->type == PM2_TYPE_PERMEDIA2V;
718
719         pixclock = PICOS2KHZ(info->var.pixclock);
720         if (pixclock > PM2_MAX_PIXCLOCK) {
721                 DPRINTK("pixclock too high (%uKHz)\n", pixclock);
722                 return -EINVAL;
723         }
724
725         hsstart = to3264(info->var.right_margin, depth, data64);
726         hsend = hsstart + to3264(info->var.hsync_len, depth, data64);
727         hbend = hsend + to3264(info->var.left_margin, depth, data64);
728         htotal = to3264(xres, depth, data64) + hbend - 1;
729         vsstart = (info->var.lower_margin)
730                 ? info->var.lower_margin - 1
731                 : 0;    /* FIXME! */
732         vsend = info->var.lower_margin + info->var.vsync_len - 1;
733         vbend = info->var.lower_margin + info->var.vsync_len +
734                 info->var.upper_margin;
735         vtotal = info->var.yres + vbend - 1;
736         stride = to3264(width, depth, 1);
737         base = to3264(info->var.yoffset * xres + info->var.xoffset, depth, 1);
738         if (data64)
739                 video |= PM2F_DATA_64_ENABLE;
740
741         if (info->var.sync & FB_SYNC_HOR_HIGH_ACT) {
742                 if (lowhsync) {
743                         DPRINTK("ignoring +hsync, using -hsync.\n");
744                         video |= PM2F_HSYNC_ACT_LOW;
745                 } else
746                         video |= PM2F_HSYNC_ACT_HIGH;
747         } else
748                 video |= PM2F_HSYNC_ACT_LOW;
749
750         if (info->var.sync & FB_SYNC_VERT_HIGH_ACT) {
751                 if (lowvsync) {
752                         DPRINTK("ignoring +vsync, using -vsync.\n");
753                         video |= PM2F_VSYNC_ACT_LOW;
754                 } else
755                         video |= PM2F_VSYNC_ACT_HIGH;
756         } else
757                 video |= PM2F_VSYNC_ACT_LOW;
758
759         if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) {
760                 DPRINTK("interlaced not supported\n");
761                 return -EINVAL;
762         }
763         if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE)
764                 video |= PM2F_LINE_DOUBLE;
765         if ((info->var.activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW)
766                 video |= PM2F_VIDEO_ENABLE;
767         par->video = video;
768
769         info->fix.visual =
770                 (depth == 8) ? FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
771         info->fix.line_length = info->var.xres * depth / 8;
772         info->cmap.len = 256;
773
774         /*
775          * Settings calculated. Now write them out.
776          */
777         if (par->type == PM2_TYPE_PERMEDIA2V) {
778                 WAIT_FIFO(par, 1);
779                 pm2_WR(par, PM2VR_RD_INDEX_HIGH, 0);
780         }
781
782         set_aperture(par, depth);
783
784         mb();
785         WAIT_FIFO(par, 19);
786         switch (depth) {
787         case 8:
788                 pm2_WR(par, PM2R_FB_READ_PIXEL, 0);
789                 clrformat = 0x2e;
790                 break;
791         case 16:
792                 pm2_WR(par, PM2R_FB_READ_PIXEL, 1);
793                 clrmode |= PM2F_RD_TRUECOLOR | PM2F_RD_PIXELFORMAT_RGB565;
794                 txtmap = PM2F_TEXTEL_SIZE_16;
795                 pixsize = 1;
796                 clrformat = 0x70;
797                 misc |= 8;
798                 break;
799         case 32:
800                 pm2_WR(par, PM2R_FB_READ_PIXEL, 2);
801                 clrmode |= PM2F_RD_TRUECOLOR | PM2F_RD_PIXELFORMAT_RGBA8888;
802                 txtmap = PM2F_TEXTEL_SIZE_32;
803                 pixsize = 2;
804                 clrformat = 0x20;
805                 misc |= 8;
806                 break;
807         case 24:
808                 pm2_WR(par, PM2R_FB_READ_PIXEL, 4);
809                 clrmode |= PM2F_RD_TRUECOLOR | PM2F_RD_PIXELFORMAT_RGB888;
810                 txtmap = PM2F_TEXTEL_SIZE_24;
811                 pixsize = 4;
812                 clrformat = 0x20;
813                 misc |= 8;
814                 break;
815         }
816         pm2_WR(par, PM2R_FB_WRITE_MODE, PM2F_FB_WRITE_ENABLE);
817         pm2_WR(par, PM2R_FB_READ_MODE, partprod(xres));
818         pm2_WR(par, PM2R_LB_READ_MODE, partprod(xres));
819         pm2_WR(par, PM2R_TEXTURE_MAP_FORMAT, txtmap | partprod(xres));
820         pm2_WR(par, PM2R_H_TOTAL, htotal);
821         pm2_WR(par, PM2R_HS_START, hsstart);
822         pm2_WR(par, PM2R_HS_END, hsend);
823         pm2_WR(par, PM2R_HG_END, hbend);
824         pm2_WR(par, PM2R_HB_END, hbend);
825         pm2_WR(par, PM2R_V_TOTAL, vtotal);
826         pm2_WR(par, PM2R_VS_START, vsstart);
827         pm2_WR(par, PM2R_VS_END, vsend);
828         pm2_WR(par, PM2R_VB_END, vbend);
829         pm2_WR(par, PM2R_SCREEN_STRIDE, stride);
830         wmb();
831         pm2_WR(par, PM2R_WINDOW_ORIGIN, 0);
832         pm2_WR(par, PM2R_SCREEN_SIZE, (height << 16) | width);
833         pm2_WR(par, PM2R_SCISSOR_MODE, PM2F_SCREEN_SCISSOR_ENABLE);
834         wmb();
835         pm2_WR(par, PM2R_SCREEN_BASE, base);
836         wmb();
837         set_video(par, video);
838         WAIT_FIFO(par, 10);
839         switch (par->type) {
840         case PM2_TYPE_PERMEDIA2:
841                 pm2_RDAC_WR(par, PM2I_RD_COLOR_MODE, clrmode);
842                 pm2_RDAC_WR(par, PM2I_RD_COLOR_KEY_CONTROL,
843                                 (depth == 8) ? 0 : PM2F_COLOR_KEY_TEST_OFF);
844                 break;
845         case PM2_TYPE_PERMEDIA2V:
846                 pm2v_RDAC_WR(par, PM2VI_RD_DAC_CONTROL, 0);
847                 pm2v_RDAC_WR(par, PM2VI_RD_PIXEL_SIZE, pixsize);
848                 pm2v_RDAC_WR(par, PM2VI_RD_COLOR_FORMAT, clrformat);
849                 pm2v_RDAC_WR(par, PM2VI_RD_MISC_CONTROL, misc);
850                 pm2v_RDAC_WR(par, PM2VI_RD_OVERLAY_KEY, 0);
851                 break;
852         }
853         set_pixclock(par, pixclock);
854         DPRINTK("Setting graphics mode at %dx%d depth %d\n",
855                 info->var.xres, info->var.yres, info->var.bits_per_pixel);
856         return 0;
857 }
858
859 /**
860  *      pm2fb_setcolreg - Sets a color register.
861  *      @regno: boolean, 0 copy local, 1 get_user() function
862  *      @red: frame buffer colormap structure
863  *      @green: The green value which can be up to 16 bits wide
864  *      @blue:  The blue value which can be up to 16 bits wide.
865  *      @transp: If supported the alpha value which can be up to 16 bits wide.
866  *      @info: frame buffer info structure
867  *
868  *      Set a single color register. The values supplied have a 16 bit
869  *      magnitude which needs to be scaled in this function for the hardware.
870  *      Pretty much a direct lift from tdfxfb.c.
871  *
872  *      Returns negative errno on error, or zero on success.
873  */
874 static int pm2fb_setcolreg(unsigned regno, unsigned red, unsigned green,
875                            unsigned blue, unsigned transp,
876                            struct fb_info *info)
877 {
878         struct pm2fb_par *par = info->par;
879
880         if (regno >= info->cmap.len)  /* no. of hw registers */
881                 return -EINVAL;
882         /*
883          * Program hardware... do anything you want with transp
884          */
885
886         /* grayscale works only partially under directcolor */
887         /* grayscale = 0.30*R + 0.59*G + 0.11*B */
888         if (info->var.grayscale)
889                 red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
890
891         /* Directcolor:
892          *   var->{color}.offset contains start of bitfield
893          *   var->{color}.length contains length of bitfield
894          *   {hardwarespecific} contains width of DAC
895          *   cmap[X] is programmed to
896          *   (X << red.offset) | (X << green.offset) | (X << blue.offset)
897          *   RAMDAC[X] is programmed to (red, green, blue)
898          *
899          * Pseudocolor:
900          *    uses offset = 0 && length = DAC register width.
901          *    var->{color}.offset is 0
902          *    var->{color}.length contains width of DAC
903          *    cmap is not used
904          *    DAC[X] is programmed to (red, green, blue)
905          * Truecolor:
906          *    does not use RAMDAC (usually has 3 of them).
907          *    var->{color}.offset contains start of bitfield
908          *    var->{color}.length contains length of bitfield
909          *    cmap is programmed to
910          *    (red << red.offset) | (green << green.offset) |
911          *    (blue << blue.offset) | (transp << transp.offset)
912          *    RAMDAC does not exist
913          */
914 #define CNVT_TOHW(val, width) ((((val) << (width)) + 0x7FFF -(val)) >> 16)
915         switch (info->fix.visual) {
916         case FB_VISUAL_TRUECOLOR:
917         case FB_VISUAL_PSEUDOCOLOR:
918                 red = CNVT_TOHW(red, info->var.red.length);
919                 green = CNVT_TOHW(green, info->var.green.length);
920                 blue = CNVT_TOHW(blue, info->var.blue.length);
921                 transp = CNVT_TOHW(transp, info->var.transp.length);
922                 break;
923         case FB_VISUAL_DIRECTCOLOR:
924                 /* example here assumes 8 bit DAC. Might be different
925                  * for your hardware */
926                 red = CNVT_TOHW(red, 8);
927                 green = CNVT_TOHW(green, 8);
928                 blue = CNVT_TOHW(blue, 8);
929                 /* hey, there is bug in transp handling... */
930                 transp = CNVT_TOHW(transp, 8);
931                 break;
932         }
933 #undef CNVT_TOHW
934         /* Truecolor has hardware independent palette */
935         if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
936                 u32 v;
937
938                 if (regno >= 16)
939                         return -EINVAL;
940
941                 v = (red << info->var.red.offset) |
942                         (green << info->var.green.offset) |
943                         (blue << info->var.blue.offset) |
944                         (transp << info->var.transp.offset);
945
946                 switch (info->var.bits_per_pixel) {
947                 case 8:
948                         break;
949                 case 16:
950                 case 24:
951                 case 32:
952                         par->palette[regno] = v;
953                         break;
954                 }
955                 return 0;
956         } else if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR)
957                 set_color(par, regno, red, green, blue);
958
959         return 0;
960 }
961
962 /**
963  *      pm2fb_pan_display - Pans the display.
964  *      @var: frame buffer variable screen structure
965  *      @info: frame buffer structure that represents a single frame buffer
966  *
967  *      Pan (or wrap, depending on the `vmode' field) the display using the
968  *      `xoffset' and `yoffset' fields of the `var' structure.
969  *      If the values don't fit, return -EINVAL.
970  *
971  *      Returns negative errno on error, or zero on success.
972  *
973  */
974 static int pm2fb_pan_display(struct fb_var_screeninfo *var,
975                              struct fb_info *info)
976 {
977         struct pm2fb_par *p = info->par;
978         u32 base;
979         u32 depth = (info->var.bits_per_pixel + 7) & ~7;
980         u32 xres = (info->var.xres + 31) & ~31;
981
982         depth = (depth > 32) ? 32 : depth;
983         base = to3264(var->yoffset * xres + var->xoffset, depth, 1);
984         WAIT_FIFO(p, 1);
985         pm2_WR(p, PM2R_SCREEN_BASE, base);
986         return 0;
987 }
988
989 /**
990  *      pm2fb_blank - Blanks the display.
991  *      @blank_mode: the blank mode we want.
992  *      @info: frame buffer structure that represents a single frame buffer
993  *
994  *      Blank the screen if blank_mode != 0, else unblank. Return 0 if
995  *      blanking succeeded, != 0 if un-/blanking failed due to e.g. a
996  *      video mode which doesn't support it. Implements VESA suspend
997  *      and powerdown modes on hardware that supports disabling hsync/vsync:
998  *      blank_mode == 2: suspend vsync
999  *      blank_mode == 3: suspend hsync
1000  *      blank_mode == 4: powerdown
1001  *
1002  *      Returns negative errno on error, or zero on success.
1003  *
1004  */
1005 static int pm2fb_blank(int blank_mode, struct fb_info *info)
1006 {
1007         struct pm2fb_par *par = info->par;
1008         u32 video = par->video;
1009
1010         DPRINTK("blank_mode %d\n", blank_mode);
1011
1012         switch (blank_mode) {
1013         case FB_BLANK_UNBLANK:
1014                 /* Screen: On */
1015                 video |= PM2F_VIDEO_ENABLE;
1016                 break;
1017         case FB_BLANK_NORMAL:
1018                 /* Screen: Off */
1019                 video &= ~PM2F_VIDEO_ENABLE;
1020                 break;
1021         case FB_BLANK_VSYNC_SUSPEND:
1022                 /* VSync: Off */
1023                 video &= ~(PM2F_VSYNC_MASK | PM2F_BLANK_LOW);
1024                 break;
1025         case FB_BLANK_HSYNC_SUSPEND:
1026                 /* HSync: Off */
1027                 video &= ~(PM2F_HSYNC_MASK | PM2F_BLANK_LOW);
1028                 break;
1029         case FB_BLANK_POWERDOWN:
1030                 /* HSync: Off, VSync: Off */
1031                 video &= ~(PM2F_VSYNC_MASK | PM2F_HSYNC_MASK | PM2F_BLANK_LOW);
1032                 break;
1033         }
1034         set_video(par, video);
1035         return 0;
1036 }
1037
1038 static int pm2fb_sync(struct fb_info *info)
1039 {
1040         struct pm2fb_par *par = info->par;
1041
1042         WAIT_FIFO(par, 1);
1043         pm2_WR(par, PM2R_SYNC, 0);
1044         mb();
1045         do {
1046                 while (pm2_RD(par, PM2R_OUT_FIFO_WORDS) == 0)
1047                         cpu_relax();
1048         } while (pm2_RD(par, PM2R_OUT_FIFO) != PM2TAG(PM2R_SYNC));
1049
1050         return 0;
1051 }
1052
1053 static void pm2fb_fillrect(struct fb_info *info,
1054                                 const struct fb_fillrect *region)
1055 {
1056         struct pm2fb_par *par = info->par;
1057         struct fb_fillrect modded;
1058         int vxres, vyres;
1059         u32 color = (info->fix.visual == FB_VISUAL_TRUECOLOR) ?
1060                 ((u32 *)info->pseudo_palette)[region->color] : region->color;
1061
1062         if (info->state != FBINFO_STATE_RUNNING)
1063                 return;
1064         if ((info->flags & FBINFO_HWACCEL_DISABLED) ||
1065                 region->rop != ROP_COPY ) {
1066                 cfb_fillrect(info, region);
1067                 return;
1068         }
1069
1070         vxres = info->var.xres_virtual;
1071         vyres = info->var.yres_virtual;
1072
1073         memcpy(&modded, region, sizeof(struct fb_fillrect));
1074
1075         if (!modded.width || !modded.height ||
1076             modded.dx >= vxres || modded.dy >= vyres)
1077                 return;
1078
1079         if (modded.dx + modded.width  > vxres)
1080                 modded.width  = vxres - modded.dx;
1081         if (modded.dy + modded.height > vyres)
1082                 modded.height = vyres - modded.dy;
1083
1084         if (info->var.bits_per_pixel == 8)
1085                 color |= color << 8;
1086         if (info->var.bits_per_pixel <= 16)
1087                 color |= color << 16;
1088
1089         WAIT_FIFO(par, 3);
1090         pm2_WR(par, PM2R_CONFIG, PM2F_CONFIG_FB_WRITE_ENABLE);
1091         pm2_WR(par, PM2R_RECTANGLE_ORIGIN, (modded.dy << 16) | modded.dx);
1092         pm2_WR(par, PM2R_RECTANGLE_SIZE, (modded.height << 16) | modded.width);
1093         if (info->var.bits_per_pixel != 24) {
1094                 WAIT_FIFO(par, 2);
1095                 pm2_WR(par, PM2R_FB_BLOCK_COLOR, color);
1096                 wmb();
1097                 pm2_WR(par, PM2R_RENDER,
1098                                 PM2F_RENDER_RECTANGLE | PM2F_RENDER_FASTFILL);
1099         } else {
1100                 WAIT_FIFO(par, 4);
1101                 pm2_WR(par, PM2R_COLOR_DDA_MODE, 1);
1102                 pm2_WR(par, PM2R_CONSTANT_COLOR, color);
1103                 wmb();
1104                 pm2_WR(par, PM2R_RENDER,
1105                                 PM2F_RENDER_RECTANGLE |
1106                                 PM2F_INCREASE_X | PM2F_INCREASE_Y );
1107                 pm2_WR(par, PM2R_COLOR_DDA_MODE, 0);
1108         }
1109 }
1110
1111 static void pm2fb_copyarea(struct fb_info *info,
1112                                 const struct fb_copyarea *area)
1113 {
1114         struct pm2fb_par *par = info->par;
1115         struct fb_copyarea modded;
1116         u32 vxres, vyres;
1117
1118         if (info->state != FBINFO_STATE_RUNNING)
1119                 return;
1120         if (info->flags & FBINFO_HWACCEL_DISABLED) {
1121                 cfb_copyarea(info, area);
1122                 return;
1123         }
1124
1125         memcpy(&modded, area, sizeof(struct fb_copyarea));
1126
1127         vxres = info->var.xres_virtual;
1128         vyres = info->var.yres_virtual;
1129
1130         if (!modded.width || !modded.height ||
1131             modded.sx >= vxres || modded.sy >= vyres ||
1132             modded.dx >= vxres || modded.dy >= vyres)
1133                 return;
1134
1135         if (modded.sx + modded.width > vxres)
1136                 modded.width = vxres - modded.sx;
1137         if (modded.dx + modded.width > vxres)
1138                 modded.width = vxres - modded.dx;
1139         if (modded.sy + modded.height > vyres)
1140                 modded.height = vyres - modded.sy;
1141         if (modded.dy + modded.height > vyres)
1142                 modded.height = vyres - modded.dy;
1143
1144         WAIT_FIFO(par, 5);
1145         pm2_WR(par, PM2R_CONFIG, PM2F_CONFIG_FB_WRITE_ENABLE |
1146                 PM2F_CONFIG_FB_READ_SOURCE_ENABLE);
1147         pm2_WR(par, PM2R_FB_SOURCE_DELTA,
1148                         ((modded.sy - modded.dy) & 0xfff) << 16 |
1149                         ((modded.sx - modded.dx) & 0xfff));
1150         pm2_WR(par, PM2R_RECTANGLE_ORIGIN, (modded.dy << 16) | modded.dx);
1151         pm2_WR(par, PM2R_RECTANGLE_SIZE, (modded.height << 16) | modded.width);
1152         wmb();
1153         pm2_WR(par, PM2R_RENDER, PM2F_RENDER_RECTANGLE |
1154                                 (modded.dx < modded.sx ? PM2F_INCREASE_X : 0) |
1155                                 (modded.dy < modded.sy ? PM2F_INCREASE_Y : 0));
1156 }
1157
1158 static void pm2fb_imageblit(struct fb_info *info, const struct fb_image *image)
1159 {
1160         struct pm2fb_par *par = info->par;
1161         u32 height = image->height;
1162         u32 fgx, bgx;
1163         const u32 *src = (const u32 *)image->data;
1164         u32 xres = (info->var.xres + 31) & ~31;
1165         int raster_mode = 1; /* invert bits */
1166
1167 #ifdef __LITTLE_ENDIAN
1168         raster_mode |= 3 << 7; /* reverse byte order */
1169 #endif
1170
1171         if (info->state != FBINFO_STATE_RUNNING)
1172                 return;
1173         if (info->flags & FBINFO_HWACCEL_DISABLED || image->depth != 1) {
1174                 cfb_imageblit(info, image);
1175                 return;
1176         }
1177         switch (info->fix.visual) {
1178         case FB_VISUAL_PSEUDOCOLOR:
1179                 fgx = image->fg_color;
1180                 bgx = image->bg_color;
1181                 break;
1182         case FB_VISUAL_TRUECOLOR:
1183         default:
1184                 fgx = par->palette[image->fg_color];
1185                 bgx = par->palette[image->bg_color];
1186                 break;
1187         }
1188         if (info->var.bits_per_pixel == 8) {
1189                 fgx |= fgx << 8;
1190                 bgx |= bgx << 8;
1191         }
1192         if (info->var.bits_per_pixel <= 16) {
1193                 fgx |= fgx << 16;
1194                 bgx |= bgx << 16;
1195         }
1196
1197         WAIT_FIFO(par, 13);
1198         pm2_WR(par, PM2R_FB_READ_MODE, partprod(xres));
1199         pm2_WR(par, PM2R_SCISSOR_MIN_XY,
1200                         ((image->dy & 0xfff) << 16) | (image->dx & 0x0fff));
1201         pm2_WR(par, PM2R_SCISSOR_MAX_XY,
1202                         (((image->dy + image->height) & 0x0fff) << 16) |
1203                         ((image->dx + image->width) & 0x0fff));
1204         pm2_WR(par, PM2R_SCISSOR_MODE, 1);
1205         /* GXcopy & UNIT_ENABLE */
1206         pm2_WR(par, PM2R_LOGICAL_OP_MODE, (0x3 << 1) | 1);
1207         pm2_WR(par, PM2R_RECTANGLE_ORIGIN,
1208                         ((image->dy & 0xfff) << 16) | (image->dx & 0x0fff));
1209         pm2_WR(par, PM2R_RECTANGLE_SIZE,
1210                         ((image->height & 0x0fff) << 16) |
1211                         ((image->width) & 0x0fff));
1212         if (info->var.bits_per_pixel == 24) {
1213                 pm2_WR(par, PM2R_COLOR_DDA_MODE, 1);
1214                 /* clear area */
1215                 pm2_WR(par, PM2R_CONSTANT_COLOR, bgx);
1216                 pm2_WR(par, PM2R_RENDER,
1217                         PM2F_RENDER_RECTANGLE |
1218                         PM2F_INCREASE_X | PM2F_INCREASE_Y);
1219                 /* BitMapPackEachScanline */
1220                 pm2_WR(par, PM2R_RASTERIZER_MODE, raster_mode | (1 << 9));
1221                 pm2_WR(par, PM2R_CONSTANT_COLOR, fgx);
1222                 pm2_WR(par, PM2R_RENDER,
1223                         PM2F_RENDER_RECTANGLE |
1224                         PM2F_INCREASE_X | PM2F_INCREASE_Y |
1225                         PM2F_RENDER_SYNC_ON_BIT_MASK);
1226         } else {
1227                 pm2_WR(par, PM2R_COLOR_DDA_MODE, 0);
1228                 /* clear area */
1229                 pm2_WR(par, PM2R_FB_BLOCK_COLOR, bgx);
1230                 pm2_WR(par, PM2R_RENDER,
1231                         PM2F_RENDER_RECTANGLE |
1232                         PM2F_RENDER_FASTFILL |
1233                         PM2F_INCREASE_X | PM2F_INCREASE_Y);
1234                 pm2_WR(par, PM2R_RASTERIZER_MODE, raster_mode);
1235                 pm2_WR(par, PM2R_FB_BLOCK_COLOR, fgx);
1236                 pm2_WR(par, PM2R_RENDER,
1237                         PM2F_RENDER_RECTANGLE |
1238                         PM2F_INCREASE_X | PM2F_INCREASE_Y |
1239                         PM2F_RENDER_FASTFILL |
1240                         PM2F_RENDER_SYNC_ON_BIT_MASK);
1241         }
1242
1243         while (height--) {
1244                 int width = ((image->width + 7) >> 3)
1245                                 + info->pixmap.scan_align - 1;
1246                 width >>= 2;
1247                 WAIT_FIFO(par, width);
1248                 while (width--) {
1249                         pm2_WR(par, PM2R_BIT_MASK_PATTERN, *src);
1250                         src++;
1251                 }
1252         }
1253         WAIT_FIFO(par, 3);
1254         pm2_WR(par, PM2R_RASTERIZER_MODE, 0);
1255         pm2_WR(par, PM2R_COLOR_DDA_MODE, 0);
1256         pm2_WR(par, PM2R_SCISSOR_MODE, 0);
1257 }
1258
1259 /*
1260  *      Hardware cursor support.
1261  */
1262 static const u8 cursor_bits_lookup[16] = {
1263         0x00, 0x40, 0x10, 0x50, 0x04, 0x44, 0x14, 0x54,
1264         0x01, 0x41, 0x11, 0x51, 0x05, 0x45, 0x15, 0x55
1265 };
1266
1267 static int pm2vfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
1268 {
1269         struct pm2fb_par *par = info->par;
1270         u8 mode = PM2F_CURSORMODE_TYPE_X;
1271         int x = cursor->image.dx - info->var.xoffset;
1272         int y = cursor->image.dy - info->var.yoffset;
1273
1274         if (cursor->enable)
1275                 mode |= PM2F_CURSORMODE_CURSOR_ENABLE;
1276
1277         pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_MODE, mode);
1278
1279         if (!cursor->enable)
1280                 x = 2047;       /* push it outside display */
1281         pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_X_LOW, x & 0xff);
1282         pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_X_HIGH, (x >> 8) & 0xf);
1283         pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_Y_LOW, y & 0xff);
1284         pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_Y_HIGH, (y >> 8) & 0xf);
1285
1286         /*
1287          * If the cursor is not be changed this means either we want the
1288          * current cursor state (if enable is set) or we want to query what
1289          * we can do with the cursor (if enable is not set)
1290          */
1291         if (!cursor->set)
1292                 return 0;
1293
1294         if (cursor->set & FB_CUR_SETHOT) {
1295                 pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_X_HOT,
1296                              cursor->hot.x & 0x3f);
1297                 pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_Y_HOT,
1298                              cursor->hot.y & 0x3f);
1299         }
1300
1301         if (cursor->set & FB_CUR_SETCMAP) {
1302                 u32 fg_idx = cursor->image.fg_color;
1303                 u32 bg_idx = cursor->image.bg_color;
1304                 struct fb_cmap cmap = info->cmap;
1305
1306                 /* the X11 driver says one should use these color registers */
1307                 pm2_WR(par, PM2VR_RD_INDEX_HIGH, PM2VI_RD_CURSOR_PALETTE >> 8);
1308                 pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_PALETTE + 0,
1309                              cmap.red[bg_idx] >> 8 );
1310                 pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_PALETTE + 1,
1311                              cmap.green[bg_idx] >> 8 );
1312                 pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_PALETTE + 2,
1313                              cmap.blue[bg_idx] >> 8 );
1314
1315                 pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_PALETTE + 3,
1316                              cmap.red[fg_idx] >> 8 );
1317                 pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_PALETTE + 4,
1318                              cmap.green[fg_idx] >> 8 );
1319                 pm2v_RDAC_WR(par, PM2VI_RD_CURSOR_PALETTE + 5,
1320                              cmap.blue[fg_idx] >> 8 );
1321                 pm2_WR(par, PM2VR_RD_INDEX_HIGH, 0);
1322         }
1323
1324         if (cursor->set & (FB_CUR_SETSHAPE | FB_CUR_SETIMAGE)) {
1325                 u8 *bitmap = (u8 *)cursor->image.data;
1326                 u8 *mask = (u8 *)cursor->mask;
1327                 int i;
1328                 int pos = PM2VI_RD_CURSOR_PATTERN;
1329
1330                 for (i = 0; i < cursor->image.height; i++) {
1331                         int j = (cursor->image.width + 7) >> 3;
1332                         int k = 8 - j;
1333
1334                         pm2_WR(par, PM2VR_RD_INDEX_HIGH, pos >> 8);
1335
1336                         for (; j > 0; j--) {
1337                                 u8 data = *bitmap ^ *mask;
1338
1339                                 if (cursor->rop == ROP_COPY)
1340                                         data = *mask & *bitmap;
1341                                 /* Upper 4 bits of bitmap data */
1342                                 pm2v_RDAC_WR(par, pos++,
1343                                         cursor_bits_lookup[data >> 4] |
1344                                         (cursor_bits_lookup[*mask >> 4] << 1));
1345                                 /* Lower 4 bits of bitmap */
1346                                 pm2v_RDAC_WR(par, pos++,
1347                                         cursor_bits_lookup[data & 0xf] |
1348                                         (cursor_bits_lookup[*mask & 0xf] << 1));
1349                                 bitmap++;
1350                                 mask++;
1351                         }
1352                         for (; k > 0; k--) {
1353                                 pm2v_RDAC_WR(par, pos++, 0);
1354                                 pm2v_RDAC_WR(par, pos++, 0);
1355                         }
1356                 }
1357
1358                 while (pos < (1024 + PM2VI_RD_CURSOR_PATTERN)) {
1359                         pm2_WR(par, PM2VR_RD_INDEX_HIGH, pos >> 8);
1360                         pm2v_RDAC_WR(par, pos++, 0);
1361                 }
1362
1363                 pm2_WR(par, PM2VR_RD_INDEX_HIGH, 0);
1364         }
1365         return 0;
1366 }
1367
1368 static int pm2fb_cursor(struct fb_info *info, struct fb_cursor *cursor)
1369 {
1370         struct pm2fb_par *par = info->par;
1371         u8 mode;
1372
1373         if (!hwcursor)
1374                 return -EINVAL; /* just to force soft_cursor() call */
1375
1376         /* Too large of a cursor or wrong bpp :-( */
1377         if (cursor->image.width > 64 ||
1378             cursor->image.height > 64 ||
1379             cursor->image.depth > 1)
1380                 return -EINVAL;
1381
1382         if (par->type == PM2_TYPE_PERMEDIA2V)
1383                 return pm2vfb_cursor(info, cursor);
1384
1385         mode = 0x40;
1386         if (cursor->enable)
1387                  mode = 0x43;
1388
1389         pm2_RDAC_WR(par, PM2I_RD_CURSOR_CONTROL, mode);
1390
1391         /*
1392          * If the cursor is not be changed this means either we want the
1393          * current cursor state (if enable is set) or we want to query what
1394          * we can do with the cursor (if enable is not set)
1395          */
1396         if (!cursor->set)
1397                 return 0;
1398
1399         if (cursor->set & FB_CUR_SETPOS) {
1400                 int x = cursor->image.dx - info->var.xoffset + 63;
1401                 int y = cursor->image.dy - info->var.yoffset + 63;
1402
1403                 WAIT_FIFO(par, 4);
1404                 pm2_WR(par, PM2R_RD_CURSOR_X_LSB, x & 0xff);
1405                 pm2_WR(par, PM2R_RD_CURSOR_X_MSB, (x >> 8) & 0x7);
1406                 pm2_WR(par, PM2R_RD_CURSOR_Y_LSB, y & 0xff);
1407                 pm2_WR(par, PM2R_RD_CURSOR_Y_MSB, (y >> 8) & 0x7);
1408         }
1409
1410         if (cursor->set & FB_CUR_SETCMAP) {
1411                 u32 fg_idx = cursor->image.fg_color;
1412                 u32 bg_idx = cursor->image.bg_color;
1413
1414                 WAIT_FIFO(par, 7);
1415                 pm2_WR(par, PM2R_RD_CURSOR_COLOR_ADDRESS, 1);
1416                 pm2_WR(par, PM2R_RD_CURSOR_COLOR_DATA,
1417                         info->cmap.red[bg_idx] >> 8);
1418                 pm2_WR(par, PM2R_RD_CURSOR_COLOR_DATA,
1419                         info->cmap.green[bg_idx] >> 8);
1420                 pm2_WR(par, PM2R_RD_CURSOR_COLOR_DATA,
1421                         info->cmap.blue[bg_idx] >> 8);
1422
1423                 pm2_WR(par, PM2R_RD_CURSOR_COLOR_DATA,
1424                         info->cmap.red[fg_idx] >> 8);
1425                 pm2_WR(par, PM2R_RD_CURSOR_COLOR_DATA,
1426                         info->cmap.green[fg_idx] >> 8);
1427                 pm2_WR(par, PM2R_RD_CURSOR_COLOR_DATA,
1428                         info->cmap.blue[fg_idx] >> 8);
1429         }
1430
1431         if (cursor->set & (FB_CUR_SETSHAPE | FB_CUR_SETIMAGE)) {
1432                 u8 *bitmap = (u8 *)cursor->image.data;
1433                 u8 *mask = (u8 *)cursor->mask;
1434                 int i;
1435
1436                 WAIT_FIFO(par, 1);
1437                 pm2_WR(par, PM2R_RD_PALETTE_WRITE_ADDRESS, 0);
1438
1439                 for (i = 0; i < cursor->image.height; i++) {
1440                         int j = (cursor->image.width + 7) >> 3;
1441                         int k = 8 - j;
1442
1443                         WAIT_FIFO(par, 8);
1444                         for (; j > 0; j--) {
1445                                 u8 data = *bitmap ^ *mask;
1446
1447                                 if (cursor->rop == ROP_COPY)
1448                                         data = *mask & *bitmap;
1449                                 /* bitmap data */
1450                                 pm2_WR(par, PM2R_RD_CURSOR_DATA, data);
1451                                 bitmap++;
1452                                 mask++;
1453                         }
1454                         for (; k > 0; k--)
1455                                 pm2_WR(par, PM2R_RD_CURSOR_DATA, 0);
1456                 }
1457                 for (; i < 64; i++) {
1458                         int j = 8;
1459                         WAIT_FIFO(par, 8);
1460                         while (j-- > 0)
1461                                 pm2_WR(par, PM2R_RD_CURSOR_DATA, 0);
1462                 }
1463
1464                 mask = (u8 *)cursor->mask;
1465                 for (i = 0; i < cursor->image.height; i++) {
1466                         int j = (cursor->image.width + 7) >> 3;
1467                         int k = 8 - j;
1468
1469                         WAIT_FIFO(par, 8);
1470                         for (; j > 0; j--) {
1471                                 /* mask */
1472                                 pm2_WR(par, PM2R_RD_CURSOR_DATA, *mask);
1473                                 mask++;
1474                         }
1475                         for (; k > 0; k--)
1476                                 pm2_WR(par, PM2R_RD_CURSOR_DATA, 0);
1477                 }
1478                 for (; i < 64; i++) {
1479                         int j = 8;
1480                         WAIT_FIFO(par, 8);
1481                         while (j-- > 0)
1482                                 pm2_WR(par, PM2R_RD_CURSOR_DATA, 0);
1483                 }
1484         }
1485         return 0;
1486 }
1487
1488 /* ------------ Hardware Independent Functions ------------ */
1489
1490 /*
1491  *  Frame buffer operations
1492  */
1493
1494 static struct fb_ops pm2fb_ops = {
1495         .owner          = THIS_MODULE,
1496         .fb_check_var   = pm2fb_check_var,
1497         .fb_set_par     = pm2fb_set_par,
1498         .fb_setcolreg   = pm2fb_setcolreg,
1499         .fb_blank       = pm2fb_blank,
1500         .fb_pan_display = pm2fb_pan_display,
1501         .fb_fillrect    = pm2fb_fillrect,
1502         .fb_copyarea    = pm2fb_copyarea,
1503         .fb_imageblit   = pm2fb_imageblit,
1504         .fb_sync        = pm2fb_sync,
1505         .fb_cursor      = pm2fb_cursor,
1506 };
1507
1508 /*
1509  * PCI stuff
1510  */
1511
1512
1513 /**
1514  * Device initialisation
1515  *
1516  * Initialise and allocate resource for PCI device.
1517  *
1518  * @param       pdev    PCI device.
1519  * @param       id      PCI device ID.
1520  */
1521 static int pm2fb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1522 {
1523         struct pm2fb_par *default_par;
1524         struct fb_info *info;
1525         int err;
1526         int retval = -ENXIO;
1527
1528         err = pci_enable_device(pdev);
1529         if (err) {
1530                 printk(KERN_WARNING "pm2fb: Can't enable pdev: %d\n", err);
1531                 return err;
1532         }
1533
1534         info = framebuffer_alloc(sizeof(struct pm2fb_par), &pdev->dev);
1535         if (!info)
1536                 return -ENOMEM;
1537         default_par = info->par;
1538
1539         switch (pdev->device) {
1540         case  PCI_DEVICE_ID_TI_TVP4020:
1541                 strcpy(pm2fb_fix.id, "TVP4020");
1542                 default_par->type = PM2_TYPE_PERMEDIA2;
1543                 break;
1544         case  PCI_DEVICE_ID_3DLABS_PERMEDIA2:
1545                 strcpy(pm2fb_fix.id, "Permedia2");
1546                 default_par->type = PM2_TYPE_PERMEDIA2;
1547                 break;
1548         case  PCI_DEVICE_ID_3DLABS_PERMEDIA2V:
1549                 strcpy(pm2fb_fix.id, "Permedia2v");
1550                 default_par->type = PM2_TYPE_PERMEDIA2V;
1551                 break;
1552         }
1553
1554         pm2fb_fix.mmio_start = pci_resource_start(pdev, 0);
1555         pm2fb_fix.mmio_len = PM2_REGS_SIZE;
1556
1557 #if defined(__BIG_ENDIAN)
1558         /*
1559          * PM2 has a 64k register file, mapped twice in 128k. Lower
1560          * map is little-endian, upper map is big-endian.
1561          */
1562         pm2fb_fix.mmio_start += PM2_REGS_SIZE;
1563         DPRINTK("Adjusting register base for big-endian.\n");
1564 #endif
1565         DPRINTK("Register base at 0x%lx\n", pm2fb_fix.mmio_start);
1566
1567         /* Registers - request region and map it. */
1568         if (!request_mem_region(pm2fb_fix.mmio_start, pm2fb_fix.mmio_len,
1569                                 "pm2fb regbase")) {
1570                 printk(KERN_WARNING "pm2fb: Can't reserve regbase.\n");
1571                 goto err_exit_neither;
1572         }
1573         default_par->v_regs =
1574                 ioremap_nocache(pm2fb_fix.mmio_start, pm2fb_fix.mmio_len);
1575         if (!default_par->v_regs) {
1576                 printk(KERN_WARNING "pm2fb: Can't remap %s register area.\n",
1577                        pm2fb_fix.id);
1578                 release_mem_region(pm2fb_fix.mmio_start, pm2fb_fix.mmio_len);
1579                 goto err_exit_neither;
1580         }
1581
1582         /* Stash away memory register info for use when we reset the board */
1583         default_par->mem_control = pm2_RD(default_par, PM2R_MEM_CONTROL);
1584         default_par->boot_address = pm2_RD(default_par, PM2R_BOOT_ADDRESS);
1585         default_par->mem_config = pm2_RD(default_par, PM2R_MEM_CONFIG);
1586         DPRINTK("MemControl 0x%x BootAddress 0x%x MemConfig 0x%x\n",
1587                 default_par->mem_control, default_par->boot_address,
1588                 default_par->mem_config);
1589
1590         if (default_par->mem_control == 0 &&
1591                 default_par->boot_address == 0x31 &&
1592                 default_par->mem_config == 0x259fffff) {
1593                 default_par->memclock = CVPPC_MEMCLOCK;
1594                 default_par->mem_control = 0;
1595                 default_par->boot_address = 0x20;
1596                 default_par->mem_config = 0xe6002021;
1597                 if (pdev->subsystem_vendor == 0x1048 &&
1598                         pdev->subsystem_device == 0x0a31) {
1599                         DPRINTK("subsystem_vendor: %04x, "
1600                                 "subsystem_device: %04x\n",
1601                                 pdev->subsystem_vendor, pdev->subsystem_device);
1602                         DPRINTK("We have not been initialized by VGA BIOS and "
1603                                 "are running on an Elsa Winner 2000 Office\n");
1604                         DPRINTK("Initializing card timings manually...\n");
1605                         default_par->memclock = 100000;
1606                 }
1607                 if (pdev->subsystem_vendor == 0x3d3d &&
1608                         pdev->subsystem_device == 0x0100) {
1609                         DPRINTK("subsystem_vendor: %04x, "
1610                                 "subsystem_device: %04x\n",
1611                                 pdev->subsystem_vendor, pdev->subsystem_device);
1612                         DPRINTK("We have not been initialized by VGA BIOS and "
1613                                 "are running on an 3dlabs reference board\n");
1614                         DPRINTK("Initializing card timings manually...\n");
1615                         default_par->memclock = 74894;
1616                 }
1617         }
1618
1619         /* Now work out how big lfb is going to be. */
1620         switch (default_par->mem_config & PM2F_MEM_CONFIG_RAM_MASK) {
1621         case PM2F_MEM_BANKS_1:
1622                 pm2fb_fix.smem_len = 0x200000;
1623                 break;
1624         case PM2F_MEM_BANKS_2:
1625                 pm2fb_fix.smem_len = 0x400000;
1626                 break;
1627         case PM2F_MEM_BANKS_3:
1628                 pm2fb_fix.smem_len = 0x600000;
1629                 break;
1630         case PM2F_MEM_BANKS_4:
1631                 pm2fb_fix.smem_len = 0x800000;
1632                 break;
1633         }
1634         pm2fb_fix.smem_start = pci_resource_start(pdev, 1);
1635
1636         /* Linear frame buffer - request region and map it. */
1637         if (!request_mem_region(pm2fb_fix.smem_start, pm2fb_fix.smem_len,
1638                                 "pm2fb smem")) {
1639                 printk(KERN_WARNING "pm2fb: Can't reserve smem.\n");
1640                 goto err_exit_mmio;
1641         }
1642         info->screen_base =
1643                 ioremap_wc(pm2fb_fix.smem_start, pm2fb_fix.smem_len);
1644         if (!info->screen_base) {
1645                 printk(KERN_WARNING "pm2fb: Can't ioremap smem area.\n");
1646                 release_mem_region(pm2fb_fix.smem_start, pm2fb_fix.smem_len);
1647                 goto err_exit_mmio;
1648         }
1649
1650         if (!nomtrr)
1651                 default_par->wc_cookie = arch_phys_wc_add(pm2fb_fix.smem_start,
1652                                                           pm2fb_fix.smem_len);
1653
1654         info->fbops             = &pm2fb_ops;
1655         info->fix               = pm2fb_fix;
1656         info->pseudo_palette    = default_par->palette;
1657         info->flags             = FBINFO_DEFAULT |
1658                                   FBINFO_HWACCEL_YPAN |
1659                                   FBINFO_HWACCEL_COPYAREA |
1660                                   FBINFO_HWACCEL_IMAGEBLIT |
1661                                   FBINFO_HWACCEL_FILLRECT;
1662
1663         info->pixmap.addr = kmalloc(PM2_PIXMAP_SIZE, GFP_KERNEL);
1664         if (!info->pixmap.addr) {
1665                 retval = -ENOMEM;
1666                 goto err_exit_pixmap;
1667         }
1668         info->pixmap.size = PM2_PIXMAP_SIZE;
1669         info->pixmap.buf_align = 4;
1670         info->pixmap.scan_align = 4;
1671         info->pixmap.access_align = 32;
1672         info->pixmap.flags = FB_PIXMAP_SYSTEM;
1673
1674         if (noaccel) {
1675                 printk(KERN_DEBUG "disabling acceleration\n");
1676                 info->flags |= FBINFO_HWACCEL_DISABLED;
1677                 info->pixmap.scan_align = 1;
1678         }
1679
1680         if (!mode_option)
1681                 mode_option = "640x480@60";
1682
1683         err = fb_find_mode(&info->var, info, mode_option, NULL, 0, NULL, 8);
1684         if (!err || err == 4)
1685                 info->var = pm2fb_var;
1686
1687         retval = fb_alloc_cmap(&info->cmap, 256, 0);
1688         if (retval < 0)
1689                 goto err_exit_both;
1690
1691         retval = register_framebuffer(info);
1692         if (retval < 0)
1693                 goto err_exit_all;
1694
1695         fb_info(info, "%s frame buffer device, memory = %dK\n",
1696                 info->fix.id, pm2fb_fix.smem_len / 1024);
1697
1698         /*
1699          * Our driver data
1700          */
1701         pci_set_drvdata(pdev, info);
1702
1703         return 0;
1704
1705  err_exit_all:
1706         fb_dealloc_cmap(&info->cmap);
1707  err_exit_both:
1708         kfree(info->pixmap.addr);
1709  err_exit_pixmap:
1710         iounmap(info->screen_base);
1711         release_mem_region(pm2fb_fix.smem_start, pm2fb_fix.smem_len);
1712  err_exit_mmio:
1713         iounmap(default_par->v_regs);
1714         release_mem_region(pm2fb_fix.mmio_start, pm2fb_fix.mmio_len);
1715  err_exit_neither:
1716         framebuffer_release(info);
1717         return retval;
1718 }
1719
1720 /**
1721  * Device removal.
1722  *
1723  * Release all device resources.
1724  *
1725  * @param       pdev    PCI device to clean up.
1726  */
1727 static void pm2fb_remove(struct pci_dev *pdev)
1728 {
1729         struct fb_info *info = pci_get_drvdata(pdev);
1730         struct fb_fix_screeninfo *fix = &info->fix;
1731         struct pm2fb_par *par = info->par;
1732
1733         unregister_framebuffer(info);
1734         arch_phys_wc_del(par->wc_cookie);
1735         iounmap(info->screen_base);
1736         release_mem_region(fix->smem_start, fix->smem_len);
1737         iounmap(par->v_regs);
1738         release_mem_region(fix->mmio_start, fix->mmio_len);
1739
1740         fb_dealloc_cmap(&info->cmap);
1741         kfree(info->pixmap.addr);
1742         framebuffer_release(info);
1743 }
1744
1745 static struct pci_device_id pm2fb_id_table[] = {
1746         { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_TVP4020,
1747           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1748         { PCI_VENDOR_ID_3DLABS, PCI_DEVICE_ID_3DLABS_PERMEDIA2,
1749           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1750         { PCI_VENDOR_ID_3DLABS, PCI_DEVICE_ID_3DLABS_PERMEDIA2V,
1751           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1752         { 0, }
1753 };
1754
1755 static struct pci_driver pm2fb_driver = {
1756         .name           = "pm2fb",
1757         .id_table       = pm2fb_id_table,
1758         .probe          = pm2fb_probe,
1759         .remove         = pm2fb_remove,
1760 };
1761
1762 MODULE_DEVICE_TABLE(pci, pm2fb_id_table);
1763
1764
1765 #ifndef MODULE
1766 /**
1767  * Parse user specified options.
1768  *
1769  * This is, comma-separated options following `video=pm2fb:'.
1770  */
1771 static int __init pm2fb_setup(char *options)
1772 {
1773         char *this_opt;
1774
1775         if (!options || !*options)
1776                 return 0;
1777
1778         while ((this_opt = strsep(&options, ",")) != NULL) {
1779                 if (!*this_opt)
1780                         continue;
1781                 if (!strcmp(this_opt, "lowhsync"))
1782                         lowhsync = 1;
1783                 else if (!strcmp(this_opt, "lowvsync"))
1784                         lowvsync = 1;
1785                 else if (!strncmp(this_opt, "hwcursor=", 9))
1786                         hwcursor = simple_strtoul(this_opt + 9, NULL, 0);
1787                 else if (!strncmp(this_opt, "nomtrr", 6))
1788                         nomtrr = 1;
1789                 else if (!strncmp(this_opt, "noaccel", 7))
1790                         noaccel = 1;
1791                 else
1792                         mode_option = this_opt;
1793         }
1794         return 0;
1795 }
1796 #endif
1797
1798
1799 static int __init pm2fb_init(void)
1800 {
1801 #ifndef MODULE
1802         char *option = NULL;
1803
1804         if (fb_get_options("pm2fb", &option))
1805                 return -ENODEV;
1806         pm2fb_setup(option);
1807 #endif
1808
1809         return pci_register_driver(&pm2fb_driver);
1810 }
1811
1812 module_init(pm2fb_init);
1813
1814 #ifdef MODULE
1815 /*
1816  *  Cleanup
1817  */
1818
1819 static void __exit pm2fb_exit(void)
1820 {
1821         pci_unregister_driver(&pm2fb_driver);
1822 }
1823 #endif
1824
1825 #ifdef MODULE
1826 module_exit(pm2fb_exit);
1827
1828 module_param(mode_option, charp, 0);
1829 MODULE_PARM_DESC(mode_option, "Initial video mode e.g. '648x480-8@60'");
1830 module_param_named(mode, mode_option, charp, 0);
1831 MODULE_PARM_DESC(mode, "Initial video mode e.g. '648x480-8@60' (deprecated)");
1832 module_param(lowhsync, bool, 0);
1833 MODULE_PARM_DESC(lowhsync, "Force horizontal sync low regardless of mode");
1834 module_param(lowvsync, bool, 0);
1835 MODULE_PARM_DESC(lowvsync, "Force vertical sync low regardless of mode");
1836 module_param(noaccel, bool, 0);
1837 MODULE_PARM_DESC(noaccel, "Disable acceleration");
1838 module_param(hwcursor, int, 0644);
1839 MODULE_PARM_DESC(hwcursor, "Enable hardware cursor "
1840                         "(1=enable, 0=disable, default=1)");
1841 module_param(nomtrr, bool, 0);
1842 MODULE_PARM_DESC(nomtrr, "Disable MTRR support (0 or 1=disabled) (default=0)");
1843
1844 MODULE_AUTHOR("Jim Hague <jim.hague@acm.org>");
1845 MODULE_DESCRIPTION("Permedia2 framebuffer device driver");
1846 MODULE_LICENSE("GPL");
1847 #endif