IB/mthca: Mark error paths as unlikely() in post_srq_recv functions
[linux-2.6] / drivers / video / neofb.c
1 /*
2  * linux/drivers/video/neofb.c -- NeoMagic Framebuffer Driver
3  *
4  * Copyright (c) 2001-2002  Denis Oliver Kropp <dok@directfb.org>
5  *
6  *
7  * Card specific code is based on XFree86's neomagic driver.
8  * Framebuffer framework code is based on code of cyber2000fb.
9  *
10  * This file is subject to the terms and conditions of the GNU General
11  * Public License.  See the file COPYING in the main directory of this
12  * archive for more details.
13  *
14  *
15  * 0.4.1
16  *  - Cosmetic changes (dok)
17  *
18  * 0.4
19  *  - Toshiba Libretto support, allow modes larger than LCD size if
20  *    LCD is disabled, keep BIOS settings if internal/external display
21  *    haven't been enabled explicitly
22  *                          (Thomas J. Moore <dark@mama.indstate.edu>)
23  *
24  * 0.3.3
25  *  - Porting over to new fbdev api. (jsimmons)
26  *  
27  * 0.3.2
28  *  - got rid of all floating point (dok) 
29  *
30  * 0.3.1
31  *  - added module license (dok)
32  *
33  * 0.3
34  *  - hardware accelerated clear and move for 2200 and above (dok)
35  *  - maximum allowed dotclock is handled now (dok)
36  *
37  * 0.2.1
38  *  - correct panning after X usage (dok)
39  *  - added module and kernel parameters (dok)
40  *  - no stretching if external display is enabled (dok)
41  *
42  * 0.2
43  *  - initial version (dok)
44  *
45  *
46  * TODO
47  * - ioctl for internal/external switching
48  * - blanking
49  * - 32bit depth support, maybe impossible
50  * - disable pan-on-sync, need specs
51  *
52  * BUGS
53  * - white margin on bootup like with tdfxfb (colormap problem?)
54  *
55  */
56
57 #include <linux/module.h>
58 #include <linux/kernel.h>
59 #include <linux/errno.h>
60 #include <linux/string.h>
61 #include <linux/mm.h>
62 #include <linux/slab.h>
63 #include <linux/delay.h>
64 #include <linux/fb.h>
65 #include <linux/pci.h>
66 #include <linux/init.h>
67 #ifdef CONFIG_TOSHIBA
68 #include <linux/toshiba.h>
69 #endif
70
71 #include <asm/io.h>
72 #include <asm/irq.h>
73 #include <asm/pgtable.h>
74 #include <asm/system.h>
75 #include <asm/uaccess.h>
76
77 #ifdef CONFIG_MTRR
78 #include <asm/mtrr.h>
79 #endif
80
81 #include <video/vga.h>
82 #include <video/neomagic.h>
83
84 #define NEOFB_VERSION "0.4.2"
85
86 /* --------------------------------------------------------------------- */
87
88 static int internal;
89 static int external;
90 static int libretto;
91 static int nostretch;
92 static int nopciburst;
93 static char *mode_option __devinitdata = NULL;
94
95 #ifdef MODULE
96
97 MODULE_AUTHOR("(c) 2001-2002  Denis Oliver Kropp <dok@convergence.de>");
98 MODULE_LICENSE("GPL");
99 MODULE_DESCRIPTION("FBDev driver for NeoMagic PCI Chips");
100 module_param(internal, bool, 0);
101 MODULE_PARM_DESC(internal, "Enable output on internal LCD Display.");
102 module_param(external, bool, 0);
103 MODULE_PARM_DESC(external, "Enable output on external CRT.");
104 module_param(libretto, bool, 0);
105 MODULE_PARM_DESC(libretto, "Force Libretto 100/110 800x480 LCD.");
106 module_param(nostretch, bool, 0);
107 MODULE_PARM_DESC(nostretch,
108                  "Disable stretching of modes smaller than LCD.");
109 module_param(nopciburst, bool, 0);
110 MODULE_PARM_DESC(nopciburst, "Disable PCI burst mode.");
111 module_param(mode_option, charp, 0);
112 MODULE_PARM_DESC(mode_option, "Preferred video mode ('640x480-8@60', etc)");
113
114 #endif
115
116
117 /* --------------------------------------------------------------------- */
118
119 static biosMode bios8[] = {
120         {320, 240, 0x40},
121         {300, 400, 0x42},
122         {640, 400, 0x20},
123         {640, 480, 0x21},
124         {800, 600, 0x23},
125         {1024, 768, 0x25},
126 };
127
128 static biosMode bios16[] = {
129         {320, 200, 0x2e},
130         {320, 240, 0x41},
131         {300, 400, 0x43},
132         {640, 480, 0x31},
133         {800, 600, 0x34},
134         {1024, 768, 0x37},
135 };
136
137 static biosMode bios24[] = {
138         {640, 480, 0x32},
139         {800, 600, 0x35},
140         {1024, 768, 0x38}
141 };
142
143 #ifdef NO_32BIT_SUPPORT_YET
144 /* FIXME: guessed values, wrong */
145 static biosMode bios32[] = {
146         {640, 480, 0x33},
147         {800, 600, 0x36},
148         {1024, 768, 0x39}
149 };
150 #endif
151
152 static inline void write_le32(int regindex, u32 val, const struct neofb_par *par)
153 {
154         writel(val, par->neo2200 + par->cursorOff + regindex);
155 }
156
157 static int neoFindMode(int xres, int yres, int depth)
158 {
159         int xres_s;
160         int i, size;
161         biosMode *mode;
162
163         switch (depth) {
164         case 8:
165                 size = ARRAY_SIZE(bios8);
166                 mode = bios8;
167                 break;
168         case 16:
169                 size = ARRAY_SIZE(bios16);
170                 mode = bios16;
171                 break;
172         case 24:
173                 size = ARRAY_SIZE(bios24);
174                 mode = bios24;
175                 break;
176 #ifdef NO_32BIT_SUPPORT_YET
177         case 32:
178                 size = ARRAY_SIZE(bios32);
179                 mode = bios32;
180                 break;
181 #endif
182         default:
183                 return 0;
184         }
185
186         for (i = 0; i < size; i++) {
187                 if (xres <= mode[i].x_res) {
188                         xres_s = mode[i].x_res;
189                         for (; i < size; i++) {
190                                 if (mode[i].x_res != xres_s)
191                                         return mode[i - 1].mode;
192                                 if (yres <= mode[i].y_res)
193                                         return mode[i].mode;
194                         }
195                 }
196         }
197         return mode[size - 1].mode;
198 }
199
200 /*
201  * neoCalcVCLK --
202  *
203  * Determine the closest clock frequency to the one requested.
204  */
205 #define REF_FREQ 0xe517         /* 14.31818 in 20.12 fixed point */
206 #define MAX_N 127
207 #define MAX_D 31
208 #define MAX_F 1
209
210 static void neoCalcVCLK(const struct fb_info *info,
211                         struct neofb_par *par, long freq)
212 {
213         int n, d, f;
214         int n_best = 0, d_best = 0, f_best = 0;
215         long f_best_diff = (0x7ffff << 12);     /* 20.12 */
216         long f_target = (freq << 12) / 1000;    /* 20.12 */
217
218         for (f = 0; f <= MAX_F; f++)
219                 for (n = 0; n <= MAX_N; n++)
220                         for (d = 0; d <= MAX_D; d++) {
221                                 long f_out;     /* 20.12 */
222                                 long f_diff;    /* 20.12 */
223
224                                 f_out =
225                                     ((((n + 1) << 12) / ((d +
226                                                           1) *
227                                                          (1 << f))) >> 12)
228                                     * REF_FREQ;
229                                 f_diff = abs(f_out - f_target);
230                                 if (f_diff < f_best_diff) {
231                                         f_best_diff = f_diff;
232                                         n_best = n;
233                                         d_best = d;
234                                         f_best = f;
235                                 }
236                         }
237
238         if (info->fix.accel == FB_ACCEL_NEOMAGIC_NM2200 ||
239             info->fix.accel == FB_ACCEL_NEOMAGIC_NM2230 ||
240             info->fix.accel == FB_ACCEL_NEOMAGIC_NM2360 ||
241             info->fix.accel == FB_ACCEL_NEOMAGIC_NM2380) {
242                 /* NOT_DONE:  We are trying the full range of the 2200 clock.
243                    We should be able to try n up to 2047 */
244                 par->VCLK3NumeratorLow = n_best;
245                 par->VCLK3NumeratorHigh = (f_best << 7);
246         } else
247                 par->VCLK3NumeratorLow = n_best | (f_best << 7);
248
249         par->VCLK3Denominator = d_best;
250
251 #ifdef NEOFB_DEBUG
252         printk("neoVCLK: f:%d NumLow=%d NumHi=%d Den=%d Df=%d\n",
253                f_target >> 12,
254                par->VCLK3NumeratorLow,
255                par->VCLK3NumeratorHigh,
256                par->VCLK3Denominator, f_best_diff >> 12);
257 #endif
258 }
259
260 /*
261  * vgaHWInit --
262  *      Handle the initialization, etc. of a screen.
263  *      Return FALSE on failure.
264  */
265
266 static int vgaHWInit(const struct fb_var_screeninfo *var,
267                      const struct fb_info *info,
268                      struct neofb_par *par, struct xtimings *timings)
269 {
270         par->MiscOutReg = 0x23;
271
272         if (!(timings->sync & FB_SYNC_HOR_HIGH_ACT))
273                 par->MiscOutReg |= 0x40;
274
275         if (!(timings->sync & FB_SYNC_VERT_HIGH_ACT))
276                 par->MiscOutReg |= 0x80;
277
278         /*
279          * Time Sequencer
280          */
281         par->Sequencer[0] = 0x00;
282         par->Sequencer[1] = 0x01;
283         par->Sequencer[2] = 0x0F;
284         par->Sequencer[3] = 0x00;       /* Font select */
285         par->Sequencer[4] = 0x0E;       /* Misc */
286
287         /*
288          * CRTC Controller
289          */
290         par->CRTC[0] = (timings->HTotal >> 3) - 5;
291         par->CRTC[1] = (timings->HDisplay >> 3) - 1;
292         par->CRTC[2] = (timings->HDisplay >> 3) - 1;
293         par->CRTC[3] = (((timings->HTotal >> 3) - 1) & 0x1F) | 0x80;
294         par->CRTC[4] = (timings->HSyncStart >> 3);
295         par->CRTC[5] = ((((timings->HTotal >> 3) - 1) & 0x20) << 2)
296             | (((timings->HSyncEnd >> 3)) & 0x1F);
297         par->CRTC[6] = (timings->VTotal - 2) & 0xFF;
298         par->CRTC[7] = (((timings->VTotal - 2) & 0x100) >> 8)
299             | (((timings->VDisplay - 1) & 0x100) >> 7)
300             | ((timings->VSyncStart & 0x100) >> 6)
301             | (((timings->VDisplay - 1) & 0x100) >> 5)
302             | 0x10 | (((timings->VTotal - 2) & 0x200) >> 4)
303             | (((timings->VDisplay - 1) & 0x200) >> 3)
304             | ((timings->VSyncStart & 0x200) >> 2);
305         par->CRTC[8] = 0x00;
306         par->CRTC[9] = (((timings->VDisplay - 1) & 0x200) >> 4) | 0x40;
307
308         if (timings->dblscan)
309                 par->CRTC[9] |= 0x80;
310
311         par->CRTC[10] = 0x00;
312         par->CRTC[11] = 0x00;
313         par->CRTC[12] = 0x00;
314         par->CRTC[13] = 0x00;
315         par->CRTC[14] = 0x00;
316         par->CRTC[15] = 0x00;
317         par->CRTC[16] = timings->VSyncStart & 0xFF;
318         par->CRTC[17] = (timings->VSyncEnd & 0x0F) | 0x20;
319         par->CRTC[18] = (timings->VDisplay - 1) & 0xFF;
320         par->CRTC[19] = var->xres_virtual >> 4;
321         par->CRTC[20] = 0x00;
322         par->CRTC[21] = (timings->VDisplay - 1) & 0xFF;
323         par->CRTC[22] = (timings->VTotal - 1) & 0xFF;
324         par->CRTC[23] = 0xC3;
325         par->CRTC[24] = 0xFF;
326
327         /*
328          * are these unnecessary?
329          * vgaHWHBlankKGA(mode, regp, 0, KGA_FIX_OVERSCAN | KGA_ENABLE_ON_ZERO);
330          * vgaHWVBlankKGA(mode, regp, 0, KGA_FIX_OVERSCAN | KGA_ENABLE_ON_ZERO);
331          */
332
333         /*
334          * Graphics Display Controller
335          */
336         par->Graphics[0] = 0x00;
337         par->Graphics[1] = 0x00;
338         par->Graphics[2] = 0x00;
339         par->Graphics[3] = 0x00;
340         par->Graphics[4] = 0x00;
341         par->Graphics[5] = 0x40;
342         par->Graphics[6] = 0x05;        /* only map 64k VGA memory !!!! */
343         par->Graphics[7] = 0x0F;
344         par->Graphics[8] = 0xFF;
345
346
347         par->Attribute[0] = 0x00;       /* standard colormap translation */
348         par->Attribute[1] = 0x01;
349         par->Attribute[2] = 0x02;
350         par->Attribute[3] = 0x03;
351         par->Attribute[4] = 0x04;
352         par->Attribute[5] = 0x05;
353         par->Attribute[6] = 0x06;
354         par->Attribute[7] = 0x07;
355         par->Attribute[8] = 0x08;
356         par->Attribute[9] = 0x09;
357         par->Attribute[10] = 0x0A;
358         par->Attribute[11] = 0x0B;
359         par->Attribute[12] = 0x0C;
360         par->Attribute[13] = 0x0D;
361         par->Attribute[14] = 0x0E;
362         par->Attribute[15] = 0x0F;
363         par->Attribute[16] = 0x41;
364         par->Attribute[17] = 0xFF;
365         par->Attribute[18] = 0x0F;
366         par->Attribute[19] = 0x00;
367         par->Attribute[20] = 0x00;
368         return 0;
369 }
370
371 static void vgaHWLock(struct vgastate *state)
372 {
373         /* Protect CRTC[0-7] */
374         vga_wcrt(state->vgabase, 0x11, vga_rcrt(state->vgabase, 0x11) | 0x80);
375 }
376
377 static void vgaHWUnlock(void)
378 {
379         /* Unprotect CRTC[0-7] */
380         vga_wcrt(NULL, 0x11, vga_rcrt(NULL, 0x11) & ~0x80);
381 }
382
383 static void neoLock(struct vgastate *state)
384 {
385         vga_wgfx(state->vgabase, 0x09, 0x00);
386         vgaHWLock(state);
387 }
388
389 static void neoUnlock(void)
390 {
391         vgaHWUnlock();
392         vga_wgfx(NULL, 0x09, 0x26);
393 }
394
395 /*
396  * VGA Palette management
397  */
398 static int paletteEnabled = 0;
399
400 static inline void VGAenablePalette(void)
401 {
402         vga_r(NULL, VGA_IS1_RC);
403         vga_w(NULL, VGA_ATT_W, 0x00);
404         paletteEnabled = 1;
405 }
406
407 static inline void VGAdisablePalette(void)
408 {
409         vga_r(NULL, VGA_IS1_RC);
410         vga_w(NULL, VGA_ATT_W, 0x20);
411         paletteEnabled = 0;
412 }
413
414 static inline void VGAwATTR(u8 index, u8 value)
415 {
416         if (paletteEnabled)
417                 index &= ~0x20;
418         else
419                 index |= 0x20;
420
421         vga_r(NULL, VGA_IS1_RC);
422         vga_wattr(NULL, index, value);
423 }
424
425 static void vgaHWProtect(int on)
426 {
427         unsigned char tmp;
428
429         if (on) {
430                 /*
431                  * Turn off screen and disable sequencer.
432                  */
433                 tmp = vga_rseq(NULL, 0x01);
434                 vga_wseq(NULL, 0x00, 0x01);             /* Synchronous Reset */
435                 vga_wseq(NULL, 0x01, tmp | 0x20);       /* disable the display */
436
437                 VGAenablePalette();
438         } else {
439                 /*
440                  * Reenable sequencer, then turn on screen.
441                  */
442                 tmp = vga_rseq(NULL, 0x01);
443                 vga_wseq(NULL, 0x01, tmp & ~0x20);      /* reenable display */
444                 vga_wseq(NULL, 0x00, 0x03);             /* clear synchronousreset */
445
446                 VGAdisablePalette();
447         }
448 }
449
450 static void vgaHWRestore(const struct fb_info *info,
451                          const struct neofb_par *par)
452 {
453         int i;
454
455         vga_w(NULL, VGA_MIS_W, par->MiscOutReg);
456
457         for (i = 1; i < 5; i++)
458                 vga_wseq(NULL, i, par->Sequencer[i]);
459
460         /* Ensure CRTC registers 0-7 are unlocked by clearing bit 7 or CRTC[17] */
461         vga_wcrt(NULL, 17, par->CRTC[17] & ~0x80);
462
463         for (i = 0; i < 25; i++)
464                 vga_wcrt(NULL, i, par->CRTC[i]);
465
466         for (i = 0; i < 9; i++)
467                 vga_wgfx(NULL, i, par->Graphics[i]);
468
469         VGAenablePalette();
470
471         for (i = 0; i < 21; i++)
472                 VGAwATTR(i, par->Attribute[i]);
473
474         VGAdisablePalette();
475 }
476
477
478 /* -------------------- Hardware specific routines ------------------------- */
479
480 /*
481  * Hardware Acceleration for Neo2200+
482  */
483 static inline int neo2200_sync(struct fb_info *info)
484 {
485         struct neofb_par *par = info->par;
486
487         while (readl(&par->neo2200->bltStat) & 1);
488         return 0;
489 }
490
491 static inline void neo2200_wait_fifo(struct fb_info *info,
492                                      int requested_fifo_space)
493 {
494         //  ndev->neo.waitfifo_calls++;
495         //  ndev->neo.waitfifo_sum += requested_fifo_space;
496
497         /* FIXME: does not work
498            if (neo_fifo_space < requested_fifo_space)
499            {
500            neo_fifo_waitcycles++;
501
502            while (1)
503            {
504            neo_fifo_space = (neo2200->bltStat >> 8);
505            if (neo_fifo_space >= requested_fifo_space)
506            break;
507            }
508            }
509            else
510            {
511            neo_fifo_cache_hits++;
512            }
513
514            neo_fifo_space -= requested_fifo_space;
515          */
516
517         neo2200_sync(info);
518 }
519
520 static inline void neo2200_accel_init(struct fb_info *info,
521                                       struct fb_var_screeninfo *var)
522 {
523         struct neofb_par *par = info->par;
524         Neo2200 __iomem *neo2200 = par->neo2200;
525         u32 bltMod, pitch;
526
527         neo2200_sync(info);
528
529         switch (var->bits_per_pixel) {
530         case 8:
531                 bltMod = NEO_MODE1_DEPTH8;
532                 pitch = var->xres_virtual;
533                 break;
534         case 15:
535         case 16:
536                 bltMod = NEO_MODE1_DEPTH16;
537                 pitch = var->xres_virtual * 2;
538                 break;
539         case 24:
540                 bltMod = NEO_MODE1_DEPTH24;
541                 pitch = var->xres_virtual * 3;
542                 break;
543         default:
544                 printk(KERN_ERR
545                        "neofb: neo2200_accel_init: unexpected bits per pixel!\n");
546                 return;
547         }
548
549         writel(bltMod << 16, &neo2200->bltStat);
550         writel((pitch << 16) | pitch, &neo2200->pitch);
551 }
552
553 /* --------------------------------------------------------------------- */
554
555 static int
556 neofb_open(struct fb_info *info, int user)
557 {
558         struct neofb_par *par = info->par;
559
560         mutex_lock(&par->open_lock);
561         if (!par->ref_count) {
562                 memset(&par->state, 0, sizeof(struct vgastate));
563                 par->state.flags = VGA_SAVE_MODE | VGA_SAVE_FONTS;
564                 save_vga(&par->state);
565         }
566         par->ref_count++;
567         mutex_unlock(&par->open_lock);
568
569         return 0;
570 }
571
572 static int
573 neofb_release(struct fb_info *info, int user)
574 {
575         struct neofb_par *par = info->par;
576
577         mutex_lock(&par->open_lock);
578         if (!par->ref_count) {
579                 mutex_unlock(&par->open_lock);
580                 return -EINVAL;
581         }
582         if (par->ref_count == 1) {
583                 restore_vga(&par->state);
584         }
585         par->ref_count--;
586         mutex_unlock(&par->open_lock);
587
588         return 0;
589 }
590
591 static int
592 neofb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
593 {
594         struct neofb_par *par = info->par;
595         unsigned int pixclock = var->pixclock;
596         struct xtimings timings;
597         int memlen, vramlen;
598         int mode_ok = 0;
599
600         DBG("neofb_check_var");
601
602         if (!pixclock)
603                 pixclock = 10000;       /* 10ns = 100MHz */
604         timings.pixclock = 1000000000 / pixclock;
605         if (timings.pixclock < 1)
606                 timings.pixclock = 1;
607
608         if (timings.pixclock > par->maxClock)
609                 return -EINVAL;
610
611         timings.dblscan = var->vmode & FB_VMODE_DOUBLE;
612         timings.interlaced = var->vmode & FB_VMODE_INTERLACED;
613         timings.HDisplay = var->xres;
614         timings.HSyncStart = timings.HDisplay + var->right_margin;
615         timings.HSyncEnd = timings.HSyncStart + var->hsync_len;
616         timings.HTotal = timings.HSyncEnd + var->left_margin;
617         timings.VDisplay = var->yres;
618         timings.VSyncStart = timings.VDisplay + var->lower_margin;
619         timings.VSyncEnd = timings.VSyncStart + var->vsync_len;
620         timings.VTotal = timings.VSyncEnd + var->upper_margin;
621         timings.sync = var->sync;
622
623         /* Is the mode larger than the LCD panel? */
624         if (par->internal_display &&
625             ((var->xres > par->NeoPanelWidth) ||
626              (var->yres > par->NeoPanelHeight))) {
627                 printk(KERN_INFO
628                        "Mode (%dx%d) larger than the LCD panel (%dx%d)\n",
629                        var->xres, var->yres, par->NeoPanelWidth,
630                        par->NeoPanelHeight);
631                 return -EINVAL;
632         }
633
634         /* Is the mode one of the acceptable sizes? */
635         if (!par->internal_display)
636                 mode_ok = 1;
637         else {
638                 switch (var->xres) {
639                 case 1280:
640                         if (var->yres == 1024)
641                                 mode_ok = 1;
642                         break;
643                 case 1024:
644                         if (var->yres == 768)
645                                 mode_ok = 1;
646                         break;
647                 case 800:
648                         if (var->yres == (par->libretto ? 480 : 600))
649                                 mode_ok = 1;
650                         break;
651                 case 640:
652                         if (var->yres == 480)
653                                 mode_ok = 1;
654                         break;
655                 }
656         }
657
658         if (!mode_ok) {
659                 printk(KERN_INFO
660                        "Mode (%dx%d) won't display properly on LCD\n",
661                        var->xres, var->yres);
662                 return -EINVAL;
663         }
664
665         var->red.msb_right = 0;
666         var->green.msb_right = 0;
667         var->blue.msb_right = 0;
668         var->transp.msb_right = 0;
669
670         switch (var->bits_per_pixel) {
671         case 8:         /* PSEUDOCOLOUR, 256 */
672                 var->transp.offset = 0;
673                 var->transp.length = 0;
674                 var->red.offset = 0;
675                 var->red.length = 8;
676                 var->green.offset = 0;
677                 var->green.length = 8;
678                 var->blue.offset = 0;
679                 var->blue.length = 8;
680                 break;
681
682         case 16:                /* DIRECTCOLOUR, 64k */
683                 var->transp.offset = 0;
684                 var->transp.length = 0;
685                 var->red.offset = 11;
686                 var->red.length = 5;
687                 var->green.offset = 5;
688                 var->green.length = 6;
689                 var->blue.offset = 0;
690                 var->blue.length = 5;
691                 break;
692
693         case 24:                /* TRUECOLOUR, 16m */
694                 var->transp.offset = 0;
695                 var->transp.length = 0;
696                 var->red.offset = 16;
697                 var->red.length = 8;
698                 var->green.offset = 8;
699                 var->green.length = 8;
700                 var->blue.offset = 0;
701                 var->blue.length = 8;
702                 break;
703
704 #ifdef NO_32BIT_SUPPORT_YET
705         case 32:                /* TRUECOLOUR, 16m */
706                 var->transp.offset = 24;
707                 var->transp.length = 8;
708                 var->red.offset = 16;
709                 var->red.length = 8;
710                 var->green.offset = 8;
711                 var->green.length = 8;
712                 var->blue.offset = 0;
713                 var->blue.length = 8;
714                 break;
715 #endif
716         default:
717                 printk(KERN_WARNING "neofb: no support for %dbpp\n",
718                        var->bits_per_pixel);
719                 return -EINVAL;
720         }
721
722         vramlen = info->fix.smem_len;
723         if (vramlen > 4 * 1024 * 1024)
724                 vramlen = 4 * 1024 * 1024;
725
726         if (var->yres_virtual < var->yres)
727                 var->yres_virtual = var->yres;
728         if (var->xres_virtual < var->xres)
729                 var->xres_virtual = var->xres;
730
731         memlen = var->xres_virtual * var->bits_per_pixel * var->yres_virtual >> 3;
732
733         if (memlen > vramlen) {
734                 var->yres_virtual =  vramlen * 8 / (var->xres_virtual *
735                                         var->bits_per_pixel);
736                 memlen = var->xres_virtual * var->bits_per_pixel *
737                                 var->yres_virtual / 8;
738         }
739
740         /* we must round yres/xres down, we already rounded y/xres_virtual up
741            if it was possible. We should return -EINVAL, but I disagree */
742         if (var->yres_virtual < var->yres)
743                 var->yres = var->yres_virtual;
744         if (var->xres_virtual < var->xres)
745                 var->xres = var->xres_virtual;
746         if (var->xoffset + var->xres > var->xres_virtual)
747                 var->xoffset = var->xres_virtual - var->xres;
748         if (var->yoffset + var->yres > var->yres_virtual)
749                 var->yoffset = var->yres_virtual - var->yres;
750
751         var->nonstd = 0;
752         var->height = -1;
753         var->width = -1;
754
755         if (var->bits_per_pixel >= 24 || !par->neo2200)
756                 var->accel_flags &= ~FB_ACCELF_TEXT;
757         return 0;
758 }
759
760 static int neofb_set_par(struct fb_info *info)
761 {
762         struct neofb_par *par = info->par;
763         struct xtimings timings;
764         unsigned char temp;
765         int i, clock_hi = 0;
766         int lcd_stretch;
767         int hoffset, voffset;
768
769         DBG("neofb_set_par");
770
771         neoUnlock();
772
773         vgaHWProtect(1);        /* Blank the screen */
774
775         timings.dblscan = info->var.vmode & FB_VMODE_DOUBLE;
776         timings.interlaced = info->var.vmode & FB_VMODE_INTERLACED;
777         timings.HDisplay = info->var.xres;
778         timings.HSyncStart = timings.HDisplay + info->var.right_margin;
779         timings.HSyncEnd = timings.HSyncStart + info->var.hsync_len;
780         timings.HTotal = timings.HSyncEnd + info->var.left_margin;
781         timings.VDisplay = info->var.yres;
782         timings.VSyncStart = timings.VDisplay + info->var.lower_margin;
783         timings.VSyncEnd = timings.VSyncStart + info->var.vsync_len;
784         timings.VTotal = timings.VSyncEnd + info->var.upper_margin;
785         timings.sync = info->var.sync;
786         timings.pixclock = PICOS2KHZ(info->var.pixclock);
787
788         if (timings.pixclock < 1)
789                 timings.pixclock = 1;
790
791         /*
792          * This will allocate the datastructure and initialize all of the
793          * generic VGA registers.
794          */
795
796         if (vgaHWInit(&info->var, info, par, &timings))
797                 return -EINVAL;
798
799         /*
800          * The default value assigned by vgaHW.c is 0x41, but this does
801          * not work for NeoMagic.
802          */
803         par->Attribute[16] = 0x01;
804
805         switch (info->var.bits_per_pixel) {
806         case 8:
807                 par->CRTC[0x13] = info->var.xres_virtual >> 3;
808                 par->ExtCRTOffset = info->var.xres_virtual >> 11;
809                 par->ExtColorModeSelect = 0x11;
810                 break;
811         case 16:
812                 par->CRTC[0x13] = info->var.xres_virtual >> 2;
813                 par->ExtCRTOffset = info->var.xres_virtual >> 10;
814                 par->ExtColorModeSelect = 0x13;
815                 break;
816         case 24:
817                 par->CRTC[0x13] = (info->var.xres_virtual * 3) >> 3;
818                 par->ExtCRTOffset = (info->var.xres_virtual * 3) >> 11;
819                 par->ExtColorModeSelect = 0x14;
820                 break;
821 #ifdef NO_32BIT_SUPPORT_YET
822         case 32:                /* FIXME: guessed values */
823                 par->CRTC[0x13] = info->var.xres_virtual >> 1;
824                 par->ExtCRTOffset = info->var.xres_virtual >> 9;
825                 par->ExtColorModeSelect = 0x15;
826                 break;
827 #endif
828         default:
829                 break;
830         }
831
832         par->ExtCRTDispAddr = 0x10;
833
834         /* Vertical Extension */
835         par->VerticalExt = (((timings.VTotal - 2) & 0x400) >> 10)
836             | (((timings.VDisplay - 1) & 0x400) >> 9)
837             | (((timings.VSyncStart) & 0x400) >> 8)
838             | (((timings.VSyncStart) & 0x400) >> 7);
839
840         /* Fast write bursts on unless disabled. */
841         if (par->pci_burst)
842                 par->SysIfaceCntl1 = 0x30;
843         else
844                 par->SysIfaceCntl1 = 0x00;
845
846         par->SysIfaceCntl2 = 0xc0;      /* VESA Bios sets this to 0x80! */
847
848         /* Initialize: by default, we want display config register to be read */
849         par->PanelDispCntlRegRead = 1;
850
851         /* Enable any user specified display devices. */
852         par->PanelDispCntlReg1 = 0x00;
853         if (par->internal_display)
854                 par->PanelDispCntlReg1 |= 0x02;
855         if (par->external_display)
856                 par->PanelDispCntlReg1 |= 0x01;
857
858         /* If the user did not specify any display devices, then... */
859         if (par->PanelDispCntlReg1 == 0x00) {
860                 /* Default to internal (i.e., LCD) only. */
861                 par->PanelDispCntlReg1 = vga_rgfx(NULL, 0x20) & 0x03;
862         }
863
864         /* If we are using a fixed mode, then tell the chip we are. */
865         switch (info->var.xres) {
866         case 1280:
867                 par->PanelDispCntlReg1 |= 0x60;
868                 break;
869         case 1024:
870                 par->PanelDispCntlReg1 |= 0x40;
871                 break;
872         case 800:
873                 par->PanelDispCntlReg1 |= 0x20;
874                 break;
875         case 640:
876         default:
877                 break;
878         }
879
880         /* Setup shadow register locking. */
881         switch (par->PanelDispCntlReg1 & 0x03) {
882         case 0x01:              /* External CRT only mode: */
883                 par->GeneralLockReg = 0x00;
884                 /* We need to program the VCLK for external display only mode. */
885                 par->ProgramVCLK = 1;
886                 break;
887         case 0x02:              /* Internal LCD only mode: */
888         case 0x03:              /* Simultaneous internal/external (LCD/CRT) mode: */
889                 par->GeneralLockReg = 0x01;
890                 /* Don't program the VCLK when using the LCD. */
891                 par->ProgramVCLK = 0;
892                 break;
893         }
894
895         /*
896          * If the screen is to be stretched, turn on stretching for the
897          * various modes.
898          *
899          * OPTION_LCD_STRETCH means stretching should be turned off!
900          */
901         par->PanelDispCntlReg2 = 0x00;
902         par->PanelDispCntlReg3 = 0x00;
903
904         if (par->lcd_stretch && (par->PanelDispCntlReg1 == 0x02) &&     /* LCD only */
905             (info->var.xres != par->NeoPanelWidth)) {
906                 switch (info->var.xres) {
907                 case 320:       /* Needs testing.  KEM -- 24 May 98 */
908                 case 400:       /* Needs testing.  KEM -- 24 May 98 */
909                 case 640:
910                 case 800:
911                 case 1024:
912                         lcd_stretch = 1;
913                         par->PanelDispCntlReg2 |= 0xC6;
914                         break;
915                 default:
916                         lcd_stretch = 0;
917                         /* No stretching in these modes. */
918                 }
919         } else
920                 lcd_stretch = 0;
921
922         /*
923          * If the screen is to be centerd, turn on the centering for the
924          * various modes.
925          */
926         par->PanelVertCenterReg1 = 0x00;
927         par->PanelVertCenterReg2 = 0x00;
928         par->PanelVertCenterReg3 = 0x00;
929         par->PanelVertCenterReg4 = 0x00;
930         par->PanelVertCenterReg5 = 0x00;
931         par->PanelHorizCenterReg1 = 0x00;
932         par->PanelHorizCenterReg2 = 0x00;
933         par->PanelHorizCenterReg3 = 0x00;
934         par->PanelHorizCenterReg4 = 0x00;
935         par->PanelHorizCenterReg5 = 0x00;
936
937
938         if (par->PanelDispCntlReg1 & 0x02) {
939                 if (info->var.xres == par->NeoPanelWidth) {
940                         /*
941                          * No centering required when the requested display width
942                          * equals the panel width.
943                          */
944                 } else {
945                         par->PanelDispCntlReg2 |= 0x01;
946                         par->PanelDispCntlReg3 |= 0x10;
947
948                         /* Calculate the horizontal and vertical offsets. */
949                         if (!lcd_stretch) {
950                                 hoffset =
951                                     ((par->NeoPanelWidth -
952                                       info->var.xres) >> 4) - 1;
953                                 voffset =
954                                     ((par->NeoPanelHeight -
955                                       info->var.yres) >> 1) - 2;
956                         } else {
957                                 /* Stretched modes cannot be centered. */
958                                 hoffset = 0;
959                                 voffset = 0;
960                         }
961
962                         switch (info->var.xres) {
963                         case 320:       /* Needs testing.  KEM -- 24 May 98 */
964                                 par->PanelHorizCenterReg3 = hoffset;
965                                 par->PanelVertCenterReg2 = voffset;
966                                 break;
967                         case 400:       /* Needs testing.  KEM -- 24 May 98 */
968                                 par->PanelHorizCenterReg4 = hoffset;
969                                 par->PanelVertCenterReg1 = voffset;
970                                 break;
971                         case 640:
972                                 par->PanelHorizCenterReg1 = hoffset;
973                                 par->PanelVertCenterReg3 = voffset;
974                                 break;
975                         case 800:
976                                 par->PanelHorizCenterReg2 = hoffset;
977                                 par->PanelVertCenterReg4 = voffset;
978                                 break;
979                         case 1024:
980                                 par->PanelHorizCenterReg5 = hoffset;
981                                 par->PanelVertCenterReg5 = voffset;
982                                 break;
983                         case 1280:
984                         default:
985                                 /* No centering in these modes. */
986                                 break;
987                         }
988                 }
989         }
990
991         par->biosMode =
992             neoFindMode(info->var.xres, info->var.yres,
993                         info->var.bits_per_pixel);
994
995         /*
996          * Calculate the VCLK that most closely matches the requested dot
997          * clock.
998          */
999         neoCalcVCLK(info, par, timings.pixclock);
1000
1001         /* Since we program the clocks ourselves, always use VCLK3. */
1002         par->MiscOutReg |= 0x0C;
1003
1004         /* alread unlocked above */
1005         /* BOGUS  vga_wgfx(NULL, 0x09, 0x26); */
1006
1007         /* don't know what this is, but it's 0 from bootup anyway */
1008         vga_wgfx(NULL, 0x15, 0x00);
1009
1010         /* was set to 0x01 by my bios in text and vesa modes */
1011         vga_wgfx(NULL, 0x0A, par->GeneralLockReg);
1012
1013         /*
1014          * The color mode needs to be set before calling vgaHWRestore
1015          * to ensure the DAC is initialized properly.
1016          *
1017          * NOTE: Make sure we don't change bits make sure we don't change
1018          * any reserved bits.
1019          */
1020         temp = vga_rgfx(NULL, 0x90);
1021         switch (info->fix.accel) {
1022         case FB_ACCEL_NEOMAGIC_NM2070:
1023                 temp &= 0xF0;   /* Save bits 7:4 */
1024                 temp |= (par->ExtColorModeSelect & ~0xF0);
1025                 break;
1026         case FB_ACCEL_NEOMAGIC_NM2090:
1027         case FB_ACCEL_NEOMAGIC_NM2093:
1028         case FB_ACCEL_NEOMAGIC_NM2097:
1029         case FB_ACCEL_NEOMAGIC_NM2160:
1030         case FB_ACCEL_NEOMAGIC_NM2200:
1031         case FB_ACCEL_NEOMAGIC_NM2230:
1032         case FB_ACCEL_NEOMAGIC_NM2360:
1033         case FB_ACCEL_NEOMAGIC_NM2380:
1034                 temp &= 0x70;   /* Save bits 6:4 */
1035                 temp |= (par->ExtColorModeSelect & ~0x70);
1036                 break;
1037         }
1038
1039         vga_wgfx(NULL, 0x90, temp);
1040
1041         /*
1042          * In some rare cases a lockup might occur if we don't delay
1043          * here. (Reported by Miles Lane)
1044          */
1045         //mdelay(200);
1046
1047         /*
1048          * Disable horizontal and vertical graphics and text expansions so
1049          * that vgaHWRestore works properly.
1050          */
1051         temp = vga_rgfx(NULL, 0x25);
1052         temp &= 0x39;
1053         vga_wgfx(NULL, 0x25, temp);
1054
1055         /*
1056          * Sleep for 200ms to make sure that the two operations above have
1057          * had time to take effect.
1058          */
1059         mdelay(200);
1060
1061         /*
1062          * This function handles restoring the generic VGA registers.  */
1063         vgaHWRestore(info, par);
1064
1065         /* linear colormap for non palettized modes */
1066         switch (info->var.bits_per_pixel) {
1067         case 8:
1068                 /* PseudoColor, 256 */
1069                 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
1070                 break;
1071         case 16:
1072                 /* TrueColor, 64k */
1073                 info->fix.visual = FB_VISUAL_TRUECOLOR;
1074
1075                 for (i = 0; i < 64; i++) {
1076                         outb(i, 0x3c8);
1077
1078                         outb(i << 1, 0x3c9);
1079                         outb(i, 0x3c9);
1080                         outb(i << 1, 0x3c9);
1081                 }
1082                 break;
1083         case 24:
1084 #ifdef NO_32BIT_SUPPORT_YET
1085         case 32:
1086 #endif
1087                 /* TrueColor, 16m */
1088                 info->fix.visual = FB_VISUAL_TRUECOLOR;
1089
1090                 for (i = 0; i < 256; i++) {
1091                         outb(i, 0x3c8);
1092
1093                         outb(i, 0x3c9);
1094                         outb(i, 0x3c9);
1095                         outb(i, 0x3c9);
1096                 }
1097                 break;
1098         }
1099
1100         vga_wgfx(NULL, 0x0E, par->ExtCRTDispAddr);
1101         vga_wgfx(NULL, 0x0F, par->ExtCRTOffset);
1102         temp = vga_rgfx(NULL, 0x10);
1103         temp &= 0x0F;           /* Save bits 3:0 */
1104         temp |= (par->SysIfaceCntl1 & ~0x0F);   /* VESA Bios sets bit 1! */
1105         vga_wgfx(NULL, 0x10, temp);
1106
1107         vga_wgfx(NULL, 0x11, par->SysIfaceCntl2);
1108         vga_wgfx(NULL, 0x15, 0 /*par->SingleAddrPage */ );
1109         vga_wgfx(NULL, 0x16, 0 /*par->DualAddrPage */ );
1110
1111         temp = vga_rgfx(NULL, 0x20);
1112         switch (info->fix.accel) {
1113         case FB_ACCEL_NEOMAGIC_NM2070:
1114                 temp &= 0xFC;   /* Save bits 7:2 */
1115                 temp |= (par->PanelDispCntlReg1 & ~0xFC);
1116                 break;
1117         case FB_ACCEL_NEOMAGIC_NM2090:
1118         case FB_ACCEL_NEOMAGIC_NM2093:
1119         case FB_ACCEL_NEOMAGIC_NM2097:
1120         case FB_ACCEL_NEOMAGIC_NM2160:
1121                 temp &= 0xDC;   /* Save bits 7:6,4:2 */
1122                 temp |= (par->PanelDispCntlReg1 & ~0xDC);
1123                 break;
1124         case FB_ACCEL_NEOMAGIC_NM2200:
1125         case FB_ACCEL_NEOMAGIC_NM2230:
1126         case FB_ACCEL_NEOMAGIC_NM2360:
1127         case FB_ACCEL_NEOMAGIC_NM2380:
1128                 temp &= 0x98;   /* Save bits 7,4:3 */
1129                 temp |= (par->PanelDispCntlReg1 & ~0x98);
1130                 break;
1131         }
1132         vga_wgfx(NULL, 0x20, temp);
1133
1134         temp = vga_rgfx(NULL, 0x25);
1135         temp &= 0x38;           /* Save bits 5:3 */
1136         temp |= (par->PanelDispCntlReg2 & ~0x38);
1137         vga_wgfx(NULL, 0x25, temp);
1138
1139         if (info->fix.accel != FB_ACCEL_NEOMAGIC_NM2070) {
1140                 temp = vga_rgfx(NULL, 0x30);
1141                 temp &= 0xEF;   /* Save bits 7:5 and bits 3:0 */
1142                 temp |= (par->PanelDispCntlReg3 & ~0xEF);
1143                 vga_wgfx(NULL, 0x30, temp);
1144         }
1145
1146         vga_wgfx(NULL, 0x28, par->PanelVertCenterReg1);
1147         vga_wgfx(NULL, 0x29, par->PanelVertCenterReg2);
1148         vga_wgfx(NULL, 0x2a, par->PanelVertCenterReg3);
1149
1150         if (info->fix.accel != FB_ACCEL_NEOMAGIC_NM2070) {
1151                 vga_wgfx(NULL, 0x32, par->PanelVertCenterReg4);
1152                 vga_wgfx(NULL, 0x33, par->PanelHorizCenterReg1);
1153                 vga_wgfx(NULL, 0x34, par->PanelHorizCenterReg2);
1154                 vga_wgfx(NULL, 0x35, par->PanelHorizCenterReg3);
1155         }
1156
1157         if (info->fix.accel == FB_ACCEL_NEOMAGIC_NM2160)
1158                 vga_wgfx(NULL, 0x36, par->PanelHorizCenterReg4);
1159
1160         if (info->fix.accel == FB_ACCEL_NEOMAGIC_NM2200 ||
1161             info->fix.accel == FB_ACCEL_NEOMAGIC_NM2230 ||
1162             info->fix.accel == FB_ACCEL_NEOMAGIC_NM2360 ||
1163             info->fix.accel == FB_ACCEL_NEOMAGIC_NM2380) {
1164                 vga_wgfx(NULL, 0x36, par->PanelHorizCenterReg4);
1165                 vga_wgfx(NULL, 0x37, par->PanelVertCenterReg5);
1166                 vga_wgfx(NULL, 0x38, par->PanelHorizCenterReg5);
1167
1168                 clock_hi = 1;
1169         }
1170
1171         /* Program VCLK3 if needed. */
1172         if (par->ProgramVCLK && ((vga_rgfx(NULL, 0x9B) != par->VCLK3NumeratorLow)
1173                                  || (vga_rgfx(NULL, 0x9F) != par->VCLK3Denominator)
1174                                  || (clock_hi && ((vga_rgfx(NULL, 0x8F) & ~0x0f)
1175                                                   != (par->VCLK3NumeratorHigh &
1176                                                       ~0x0F))))) {
1177                 vga_wgfx(NULL, 0x9B, par->VCLK3NumeratorLow);
1178                 if (clock_hi) {
1179                         temp = vga_rgfx(NULL, 0x8F);
1180                         temp &= 0x0F;   /* Save bits 3:0 */
1181                         temp |= (par->VCLK3NumeratorHigh & ~0x0F);
1182                         vga_wgfx(NULL, 0x8F, temp);
1183                 }
1184                 vga_wgfx(NULL, 0x9F, par->VCLK3Denominator);
1185         }
1186
1187         if (par->biosMode)
1188                 vga_wcrt(NULL, 0x23, par->biosMode);
1189
1190         vga_wgfx(NULL, 0x93, 0xc0);     /* Gives 5x faster framebuffer writes !!! */
1191
1192         /* Program vertical extension register */
1193         if (info->fix.accel == FB_ACCEL_NEOMAGIC_NM2200 ||
1194             info->fix.accel == FB_ACCEL_NEOMAGIC_NM2230 ||
1195             info->fix.accel == FB_ACCEL_NEOMAGIC_NM2360 ||
1196             info->fix.accel == FB_ACCEL_NEOMAGIC_NM2380) {
1197                 vga_wcrt(NULL, 0x70, par->VerticalExt);
1198         }
1199
1200         vgaHWProtect(0);        /* Turn on screen */
1201
1202         /* Calling this also locks offset registers required in update_start */
1203         neoLock(&par->state);
1204
1205         info->fix.line_length =
1206             info->var.xres_virtual * (info->var.bits_per_pixel >> 3);
1207
1208         switch (info->fix.accel) {
1209                 case FB_ACCEL_NEOMAGIC_NM2200:
1210                 case FB_ACCEL_NEOMAGIC_NM2230: 
1211                 case FB_ACCEL_NEOMAGIC_NM2360: 
1212                 case FB_ACCEL_NEOMAGIC_NM2380: 
1213                         neo2200_accel_init(info, &info->var);
1214                         break;
1215                 default:
1216                         break;
1217         }       
1218         return 0;
1219 }
1220
1221 static void neofb_update_start(struct fb_info *info,
1222                                struct fb_var_screeninfo *var)
1223 {
1224         struct neofb_par *par = info->par;
1225         struct vgastate *state = &par->state;
1226         int oldExtCRTDispAddr;
1227         int Base;
1228
1229         DBG("neofb_update_start");
1230
1231         Base = (var->yoffset * var->xres_virtual + var->xoffset) >> 2;
1232         Base *= (var->bits_per_pixel + 7) / 8;
1233
1234         neoUnlock();
1235
1236         /*
1237          * These are the generic starting address registers.
1238          */
1239         vga_wcrt(state->vgabase, 0x0C, (Base & 0x00FF00) >> 8);
1240         vga_wcrt(state->vgabase, 0x0D, (Base & 0x00FF));
1241
1242         /*
1243          * Make sure we don't clobber some other bits that might already
1244          * have been set. NOTE: NM2200 has a writable bit 3, but it shouldn't
1245          * be needed.
1246          */
1247         oldExtCRTDispAddr = vga_rgfx(NULL, 0x0E);
1248         vga_wgfx(state->vgabase, 0x0E, (((Base >> 16) & 0x0f) | (oldExtCRTDispAddr & 0xf0)));
1249
1250         neoLock(state);
1251 }
1252
1253 /*
1254  *    Pan or Wrap the Display
1255  */
1256 static int neofb_pan_display(struct fb_var_screeninfo *var,
1257                              struct fb_info *info)
1258 {
1259         u_int y_bottom;
1260
1261         y_bottom = var->yoffset;
1262
1263         if (!(var->vmode & FB_VMODE_YWRAP))
1264                 y_bottom += var->yres;
1265
1266         if (var->xoffset > (var->xres_virtual - var->xres))
1267                 return -EINVAL;
1268         if (y_bottom > info->var.yres_virtual)
1269                 return -EINVAL;
1270
1271         neofb_update_start(info, var);
1272
1273         info->var.xoffset = var->xoffset;
1274         info->var.yoffset = var->yoffset;
1275
1276         if (var->vmode & FB_VMODE_YWRAP)
1277                 info->var.vmode |= FB_VMODE_YWRAP;
1278         else
1279                 info->var.vmode &= ~FB_VMODE_YWRAP;
1280         return 0;
1281 }
1282
1283 static int neofb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
1284                            u_int transp, struct fb_info *fb)
1285 {
1286         if (regno >= fb->cmap.len || regno > 255)
1287                 return -EINVAL;
1288
1289         if (fb->var.bits_per_pixel <= 8) {
1290                 outb(regno, 0x3c8);
1291
1292                 outb(red >> 10, 0x3c9);
1293                 outb(green >> 10, 0x3c9);
1294                 outb(blue >> 10, 0x3c9);
1295         } else if (regno < 16) {
1296                 switch (fb->var.bits_per_pixel) {
1297                 case 16:
1298                         ((u32 *) fb->pseudo_palette)[regno] =
1299                                 ((red & 0xf800)) | ((green & 0xfc00) >> 5) |
1300                                 ((blue & 0xf800) >> 11);
1301                         break;
1302                 case 24:
1303                         ((u32 *) fb->pseudo_palette)[regno] =
1304                                 ((red & 0xff00) << 8) | ((green & 0xff00)) |
1305                                 ((blue & 0xff00) >> 8);
1306                         break;
1307 #ifdef NO_32BIT_SUPPORT_YET
1308                 case 32:
1309                         ((u32 *) fb->pseudo_palette)[regno] =
1310                                 ((transp & 0xff00) << 16) | ((red & 0xff00) << 8) |
1311                                 ((green & 0xff00)) | ((blue & 0xff00) >> 8);
1312                         break;
1313 #endif
1314                 default:
1315                         return 1;
1316                 }
1317         }
1318
1319         return 0;
1320 }
1321
1322 /*
1323  *    (Un)Blank the display.
1324  */
1325 static int neofb_blank(int blank_mode, struct fb_info *info)
1326 {
1327         /*
1328          *  Blank the screen if blank_mode != 0, else unblank.
1329          *  Return 0 if blanking succeeded, != 0 if un-/blanking failed due to
1330          *  e.g. a video mode which doesn't support it. Implements VESA suspend
1331          *  and powerdown modes for monitors, and backlight control on LCDs.
1332          *    blank_mode == 0: unblanked (backlight on)
1333          *    blank_mode == 1: blank (backlight on)
1334          *    blank_mode == 2: suspend vsync (backlight off)
1335          *    blank_mode == 3: suspend hsync (backlight off)
1336          *    blank_mode == 4: powerdown (backlight off)
1337          *
1338          *  wms...Enable VESA DPMS compatible powerdown mode
1339          *  run "setterm -powersave powerdown" to take advantage
1340          */
1341         struct neofb_par *par = info->par;
1342         int seqflags, lcdflags, dpmsflags, reg, tmpdisp;
1343
1344         /*
1345          * Read back the register bits related to display configuration. They might
1346          * have been changed underneath the driver via Fn key stroke.
1347          */
1348         neoUnlock();
1349         tmpdisp = vga_rgfx(NULL, 0x20) & 0x03;
1350         neoLock(&par->state);
1351
1352         /* In case we blank the screen, we want to store the possibly new
1353          * configuration in the driver. During un-blank, we re-apply this setting,
1354          * since the LCD bit will be cleared in order to switch off the backlight.
1355          */
1356         if (par->PanelDispCntlRegRead) {
1357                 par->PanelDispCntlReg1 = tmpdisp;
1358         }
1359         par->PanelDispCntlRegRead = !blank_mode;
1360
1361         switch (blank_mode) {
1362         case FB_BLANK_POWERDOWN:        /* powerdown - both sync lines down */
1363                 seqflags = VGA_SR01_SCREEN_OFF; /* Disable sequencer */
1364                 lcdflags = 0;                   /* LCD off */
1365                 dpmsflags = NEO_GR01_SUPPRESS_HSYNC |
1366                             NEO_GR01_SUPPRESS_VSYNC;
1367 #ifdef CONFIG_TOSHIBA
1368                 /* Do we still need this ? */
1369                 /* attempt to turn off backlight on toshiba; also turns off external */
1370                 {
1371                         SMMRegisters regs;
1372
1373                         regs.eax = 0xff00; /* HCI_SET */
1374                         regs.ebx = 0x0002; /* HCI_BACKLIGHT */
1375                         regs.ecx = 0x0000; /* HCI_DISABLE */
1376                         tosh_smm(&regs);
1377                 }
1378 #endif
1379                 break;
1380         case FB_BLANK_HSYNC_SUSPEND:            /* hsync off */
1381                 seqflags = VGA_SR01_SCREEN_OFF; /* Disable sequencer */
1382                 lcdflags = 0;                   /* LCD off */
1383                 dpmsflags = NEO_GR01_SUPPRESS_HSYNC;
1384                 break;
1385         case FB_BLANK_VSYNC_SUSPEND:            /* vsync off */
1386                 seqflags = VGA_SR01_SCREEN_OFF; /* Disable sequencer */
1387                 lcdflags = 0;                   /* LCD off */
1388                 dpmsflags = NEO_GR01_SUPPRESS_VSYNC;
1389                 break;
1390         case FB_BLANK_NORMAL:           /* just blank screen (backlight stays on) */
1391                 seqflags = VGA_SR01_SCREEN_OFF; /* Disable sequencer */
1392                 /*
1393                  * During a blank operation with the LID shut, we might store "LCD off"
1394                  * by mistake. Due to timing issues, the BIOS may switch the lights
1395                  * back on, and we turn it back off once we "unblank".
1396                  *
1397                  * So here is an attempt to implement ">=" - if we are in the process
1398                  * of unblanking, and the LCD bit is unset in the driver but set in the
1399                  * register, we must keep it.
1400                  */
1401                 lcdflags = ((par->PanelDispCntlReg1 | tmpdisp) & 0x02); /* LCD normal */
1402                 dpmsflags = 0x00;       /* no hsync/vsync suppression */
1403                 break;
1404         case FB_BLANK_UNBLANK:          /* unblank */
1405                 seqflags = 0;                   /* Enable sequencer */
1406                 lcdflags = ((par->PanelDispCntlReg1 | tmpdisp) & 0x02); /* LCD normal */
1407                 dpmsflags = 0x00;       /* no hsync/vsync suppression */
1408 #ifdef CONFIG_TOSHIBA
1409                 /* Do we still need this ? */
1410                 /* attempt to re-enable backlight/external on toshiba */
1411                 {
1412                         SMMRegisters regs;
1413
1414                         regs.eax = 0xff00; /* HCI_SET */
1415                         regs.ebx = 0x0002; /* HCI_BACKLIGHT */
1416                         regs.ecx = 0x0001; /* HCI_ENABLE */
1417                         tosh_smm(&regs);
1418                 }
1419 #endif
1420                 break;
1421         default:        /* Anything else we don't understand; return 1 to tell
1422                          * fb_blank we didn't aactually do anything */
1423                 return 1;
1424         }
1425
1426         neoUnlock();
1427         reg = (vga_rseq(NULL, 0x01) & ~0x20) | seqflags;
1428         vga_wseq(NULL, 0x01, reg);
1429         reg = (vga_rgfx(NULL, 0x20) & ~0x02) | lcdflags;
1430         vga_wgfx(NULL, 0x20, reg);
1431         reg = (vga_rgfx(NULL, 0x01) & ~0xF0) | 0x80 | dpmsflags;
1432         vga_wgfx(NULL, 0x01, reg);
1433         neoLock(&par->state);
1434         return 0;
1435 }
1436
1437 static void
1438 neo2200_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
1439 {
1440         struct neofb_par *par = info->par;
1441         u_long dst, rop;
1442
1443         dst = rect->dx + rect->dy * info->var.xres_virtual;
1444         rop = rect->rop ? 0x060000 : 0x0c0000;
1445
1446         neo2200_wait_fifo(info, 4);
1447
1448         /* set blt control */
1449         writel(NEO_BC3_FIFO_EN |
1450                NEO_BC0_SRC_IS_FG | NEO_BC3_SKIP_MAPPING |
1451                //               NEO_BC3_DST_XY_ADDR  |
1452                //               NEO_BC3_SRC_XY_ADDR  |
1453                rop, &par->neo2200->bltCntl);
1454
1455         switch (info->var.bits_per_pixel) {
1456         case 8:
1457                 writel(rect->color, &par->neo2200->fgColor);
1458                 break;
1459         case 16:
1460         case 24:
1461                 writel(((u32 *) (info->pseudo_palette))[rect->color],
1462                        &par->neo2200->fgColor);
1463                 break;
1464         }
1465
1466         writel(dst * ((info->var.bits_per_pixel + 7) >> 3),
1467                &par->neo2200->dstStart);
1468         writel((rect->height << 16) | (rect->width & 0xffff),
1469                &par->neo2200->xyExt);
1470 }
1471
1472 static void
1473 neo2200_copyarea(struct fb_info *info, const struct fb_copyarea *area)
1474 {
1475         u32 sx = area->sx, sy = area->sy, dx = area->dx, dy = area->dy;
1476         struct neofb_par *par = info->par;
1477         u_long src, dst, bltCntl;
1478
1479         bltCntl = NEO_BC3_FIFO_EN | NEO_BC3_SKIP_MAPPING | 0x0C0000;
1480
1481         if ((dy > sy) || ((dy == sy) && (dx > sx))) {
1482                 /* Start with the lower right corner */
1483                 sy += (area->height - 1);
1484                 dy += (area->height - 1);
1485                 sx += (area->width - 1);
1486                 dx += (area->width - 1);
1487
1488                 bltCntl |= NEO_BC0_X_DEC | NEO_BC0_DST_Y_DEC | NEO_BC0_SRC_Y_DEC;
1489         }
1490
1491         src = sx * (info->var.bits_per_pixel >> 3) + sy*info->fix.line_length;
1492         dst = dx * (info->var.bits_per_pixel >> 3) + dy*info->fix.line_length;
1493
1494         neo2200_wait_fifo(info, 4);
1495
1496         /* set blt control */
1497         writel(bltCntl, &par->neo2200->bltCntl);
1498
1499         writel(src, &par->neo2200->srcStart);
1500         writel(dst, &par->neo2200->dstStart);
1501         writel((area->height << 16) | (area->width & 0xffff),
1502                &par->neo2200->xyExt);
1503 }
1504
1505 static void
1506 neo2200_imageblit(struct fb_info *info, const struct fb_image *image)
1507 {
1508         struct neofb_par *par = info->par;
1509         int s_pitch = (image->width * image->depth + 7) >> 3;
1510         int scan_align = info->pixmap.scan_align - 1;
1511         int buf_align = info->pixmap.buf_align - 1;
1512         int bltCntl_flags, d_pitch, data_len;
1513
1514         // The data is padded for the hardware
1515         d_pitch = (s_pitch + scan_align) & ~scan_align;
1516         data_len = ((d_pitch * image->height) + buf_align) & ~buf_align;
1517
1518         neo2200_sync(info);
1519
1520         if (image->depth == 1) {
1521                 if (info->var.bits_per_pixel == 24 && image->width < 16) {
1522                         /* FIXME. There is a bug with accelerated color-expanded
1523                          * transfers in 24 bit mode if the image being transferred
1524                          * is less than 16 bits wide. This is due to insufficient
1525                          * padding when writing the image. We need to adjust
1526                          * struct fb_pixmap. Not yet done. */
1527                         return cfb_imageblit(info, image);
1528                 }
1529                 bltCntl_flags = NEO_BC0_SRC_MONO;
1530         } else if (image->depth == info->var.bits_per_pixel) {
1531                 bltCntl_flags = 0;
1532         } else {
1533                 /* We don't currently support hardware acceleration if image
1534                  * depth is different from display */
1535                 return cfb_imageblit(info, image);
1536         }
1537
1538         switch (info->var.bits_per_pixel) {
1539         case 8:
1540                 writel(image->fg_color, &par->neo2200->fgColor);
1541                 writel(image->bg_color, &par->neo2200->bgColor);
1542                 break;
1543         case 16:
1544         case 24:
1545                 writel(((u32 *) (info->pseudo_palette))[image->fg_color],
1546                        &par->neo2200->fgColor);
1547                 writel(((u32 *) (info->pseudo_palette))[image->bg_color],
1548                        &par->neo2200->bgColor);
1549                 break;
1550         }
1551
1552         writel(NEO_BC0_SYS_TO_VID |
1553                 NEO_BC3_SKIP_MAPPING | bltCntl_flags |
1554                 // NEO_BC3_DST_XY_ADDR |
1555                 0x0c0000, &par->neo2200->bltCntl);
1556
1557         writel(0, &par->neo2200->srcStart);
1558 //      par->neo2200->dstStart = (image->dy << 16) | (image->dx & 0xffff);
1559         writel(((image->dx & 0xffff) * (info->var.bits_per_pixel >> 3) +
1560                 image->dy * info->fix.line_length), &par->neo2200->dstStart);
1561         writel((image->height << 16) | (image->width & 0xffff),
1562                &par->neo2200->xyExt);
1563
1564         memcpy_toio(par->mmio_vbase + 0x100000, image->data, data_len);
1565 }
1566
1567 static void
1568 neofb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
1569 {
1570         switch (info->fix.accel) {
1571                 case FB_ACCEL_NEOMAGIC_NM2200:
1572                 case FB_ACCEL_NEOMAGIC_NM2230: 
1573                 case FB_ACCEL_NEOMAGIC_NM2360: 
1574                 case FB_ACCEL_NEOMAGIC_NM2380:
1575                         neo2200_fillrect(info, rect);
1576                         break;
1577                 default:
1578                         cfb_fillrect(info, rect);
1579                         break;
1580         }       
1581 }
1582
1583 static void
1584 neofb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
1585 {
1586         switch (info->fix.accel) {
1587                 case FB_ACCEL_NEOMAGIC_NM2200:
1588                 case FB_ACCEL_NEOMAGIC_NM2230: 
1589                 case FB_ACCEL_NEOMAGIC_NM2360: 
1590                 case FB_ACCEL_NEOMAGIC_NM2380: 
1591                         neo2200_copyarea(info, area);
1592                         break;
1593                 default:
1594                         cfb_copyarea(info, area);
1595                         break;
1596         }       
1597 }
1598
1599 static void
1600 neofb_imageblit(struct fb_info *info, const struct fb_image *image)
1601 {
1602         switch (info->fix.accel) {
1603                 case FB_ACCEL_NEOMAGIC_NM2200:
1604                 case FB_ACCEL_NEOMAGIC_NM2230:
1605                 case FB_ACCEL_NEOMAGIC_NM2360:
1606                 case FB_ACCEL_NEOMAGIC_NM2380:
1607                         neo2200_imageblit(info, image);
1608                         break;
1609                 default:
1610                         cfb_imageblit(info, image);
1611                         break;
1612         }
1613 }
1614
1615 static int 
1616 neofb_sync(struct fb_info *info)
1617 {
1618         switch (info->fix.accel) {
1619                 case FB_ACCEL_NEOMAGIC_NM2200:
1620                 case FB_ACCEL_NEOMAGIC_NM2230: 
1621                 case FB_ACCEL_NEOMAGIC_NM2360: 
1622                 case FB_ACCEL_NEOMAGIC_NM2380: 
1623                         neo2200_sync(info);
1624                         break;
1625                 default:
1626                         break;
1627         }
1628         return 0;               
1629 }
1630
1631 /*
1632 static void
1633 neofb_draw_cursor(struct fb_info *info, u8 *dst, u8 *src, unsigned int width)
1634 {
1635         //memset_io(info->sprite.addr, 0xff, 1);
1636 }
1637
1638 static int
1639 neofb_cursor(struct fb_info *info, struct fb_cursor *cursor)
1640 {
1641         struct neofb_par *par = (struct neofb_par *) info->par;
1642
1643         * Disable cursor *
1644         write_le32(NEOREG_CURSCNTL, ~NEO_CURS_ENABLE, par);
1645
1646         if (cursor->set & FB_CUR_SETPOS) {
1647                 u32 x = cursor->image.dx;
1648                 u32 y = cursor->image.dy;
1649
1650                 info->cursor.image.dx = x;
1651                 info->cursor.image.dy = y;
1652                 write_le32(NEOREG_CURSX, x, par);
1653                 write_le32(NEOREG_CURSY, y, par);
1654         }
1655
1656         if (cursor->set & FB_CUR_SETSIZE) {
1657                 info->cursor.image.height = cursor->image.height;
1658                 info->cursor.image.width = cursor->image.width;
1659         }
1660
1661         if (cursor->set & FB_CUR_SETHOT)
1662                 info->cursor.hot = cursor->hot;
1663
1664         if (cursor->set & FB_CUR_SETCMAP) {
1665                 if (cursor->image.depth == 1) {
1666                         u32 fg = cursor->image.fg_color;
1667                         u32 bg = cursor->image.bg_color;
1668
1669                         info->cursor.image.fg_color = fg;
1670                         info->cursor.image.bg_color = bg;
1671
1672                         fg = ((fg & 0xff0000) >> 16) | ((fg & 0xff) << 16) | (fg & 0xff00);
1673                         bg = ((bg & 0xff0000) >> 16) | ((bg & 0xff) << 16) | (bg & 0xff00);
1674                         write_le32(NEOREG_CURSFGCOLOR, fg, par);
1675                         write_le32(NEOREG_CURSBGCOLOR, bg, par);
1676                 }
1677         }
1678
1679         if (cursor->set & FB_CUR_SETSHAPE)
1680                 fb_load_cursor_image(info);
1681
1682         if (info->cursor.enable)
1683                 write_le32(NEOREG_CURSCNTL, NEO_CURS_ENABLE, par);
1684         return 0;
1685 }
1686 */
1687
1688 static struct fb_ops neofb_ops = {
1689         .owner          = THIS_MODULE,
1690         .fb_open        = neofb_open,
1691         .fb_release     = neofb_release,
1692         .fb_check_var   = neofb_check_var,
1693         .fb_set_par     = neofb_set_par,
1694         .fb_setcolreg   = neofb_setcolreg,
1695         .fb_pan_display = neofb_pan_display,
1696         .fb_blank       = neofb_blank,
1697         .fb_sync        = neofb_sync,
1698         .fb_fillrect    = neofb_fillrect,
1699         .fb_copyarea    = neofb_copyarea,
1700         .fb_imageblit   = neofb_imageblit,
1701 };
1702
1703 /* --------------------------------------------------------------------- */
1704
1705 static struct fb_videomode __devinitdata mode800x480 = {
1706         .xres           = 800,
1707         .yres           = 480,
1708         .pixclock       = 25000,
1709         .left_margin    = 88,
1710         .right_margin   = 40,
1711         .upper_margin   = 23,
1712         .lower_margin   = 1,
1713         .hsync_len      = 128,
1714         .vsync_len      = 4,
1715         .sync           = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
1716         .vmode          = FB_VMODE_NONINTERLACED
1717 };
1718
1719 static int __devinit neo_map_mmio(struct fb_info *info,
1720                                   struct pci_dev *dev)
1721 {
1722         struct neofb_par *par = info->par;
1723
1724         DBG("neo_map_mmio");
1725
1726         switch (info->fix.accel) {
1727                 case FB_ACCEL_NEOMAGIC_NM2070:
1728                         info->fix.mmio_start = pci_resource_start(dev, 0)+
1729                                 0x100000;
1730                         break;
1731                 case FB_ACCEL_NEOMAGIC_NM2090:
1732                 case FB_ACCEL_NEOMAGIC_NM2093:
1733                         info->fix.mmio_start = pci_resource_start(dev, 0)+
1734                                 0x200000;
1735                         break;
1736                 case FB_ACCEL_NEOMAGIC_NM2160:
1737                 case FB_ACCEL_NEOMAGIC_NM2097:
1738                 case FB_ACCEL_NEOMAGIC_NM2200:
1739                 case FB_ACCEL_NEOMAGIC_NM2230:
1740                 case FB_ACCEL_NEOMAGIC_NM2360:
1741                 case FB_ACCEL_NEOMAGIC_NM2380:
1742                         info->fix.mmio_start = pci_resource_start(dev, 1);
1743                         break;
1744                 default:
1745                         info->fix.mmio_start = pci_resource_start(dev, 0);
1746         }
1747         info->fix.mmio_len = MMIO_SIZE;
1748
1749         if (!request_mem_region
1750             (info->fix.mmio_start, MMIO_SIZE, "memory mapped I/O")) {
1751                 printk("neofb: memory mapped IO in use\n");
1752                 return -EBUSY;
1753         }
1754
1755         par->mmio_vbase = ioremap(info->fix.mmio_start, MMIO_SIZE);
1756         if (!par->mmio_vbase) {
1757                 printk("neofb: unable to map memory mapped IO\n");
1758                 release_mem_region(info->fix.mmio_start,
1759                                    info->fix.mmio_len);
1760                 return -ENOMEM;
1761         } else
1762                 printk(KERN_INFO "neofb: mapped io at %p\n",
1763                        par->mmio_vbase);
1764         return 0;
1765 }
1766
1767 static void neo_unmap_mmio(struct fb_info *info)
1768 {
1769         struct neofb_par *par = info->par;
1770
1771         DBG("neo_unmap_mmio");
1772
1773         iounmap(par->mmio_vbase);
1774         par->mmio_vbase = NULL;
1775
1776         release_mem_region(info->fix.mmio_start,
1777                            info->fix.mmio_len);
1778 }
1779
1780 static int __devinit neo_map_video(struct fb_info *info,
1781                                    struct pci_dev *dev, int video_len)
1782 {
1783         //unsigned long addr;
1784
1785         DBG("neo_map_video");
1786
1787         info->fix.smem_start = pci_resource_start(dev, 0);
1788         info->fix.smem_len = video_len;
1789
1790         if (!request_mem_region(info->fix.smem_start, info->fix.smem_len,
1791                                 "frame buffer")) {
1792                 printk("neofb: frame buffer in use\n");
1793                 return -EBUSY;
1794         }
1795
1796         info->screen_base =
1797             ioremap(info->fix.smem_start, info->fix.smem_len);
1798         if (!info->screen_base) {
1799                 printk("neofb: unable to map screen memory\n");
1800                 release_mem_region(info->fix.smem_start,
1801                                    info->fix.smem_len);
1802                 return -ENOMEM;
1803         } else
1804                 printk(KERN_INFO "neofb: mapped framebuffer at %p\n",
1805                        info->screen_base);
1806
1807 #ifdef CONFIG_MTRR
1808         ((struct neofb_par *)(info->par))->mtrr =
1809                 mtrr_add(info->fix.smem_start, pci_resource_len(dev, 0),
1810                                 MTRR_TYPE_WRCOMB, 1);
1811 #endif
1812
1813         /* Clear framebuffer, it's all white in memory after boot */
1814         memset_io(info->screen_base, 0, info->fix.smem_len);
1815
1816         /* Allocate Cursor drawing pad.
1817         info->fix.smem_len -= PAGE_SIZE;
1818         addr = info->fix.smem_start + info->fix.smem_len;
1819         write_le32(NEOREG_CURSMEMPOS, ((0x000f & (addr >> 10)) << 8) |
1820                                         ((0x0ff0 & (addr >> 10)) >> 4), par);
1821         addr = (unsigned long) info->screen_base + info->fix.smem_len;
1822         info->sprite.addr = (u8 *) addr; */
1823         return 0;
1824 }
1825
1826 static void neo_unmap_video(struct fb_info *info)
1827 {
1828         DBG("neo_unmap_video");
1829
1830 #ifdef CONFIG_MTRR
1831         {
1832                 struct neofb_par *par = info->par;
1833
1834                 mtrr_del(par->mtrr, info->fix.smem_start,
1835                          info->fix.smem_len);
1836         }
1837 #endif
1838         iounmap(info->screen_base);
1839         info->screen_base = NULL;
1840
1841         release_mem_region(info->fix.smem_start,
1842                            info->fix.smem_len);
1843 }
1844
1845 static int __devinit neo_scan_monitor(struct fb_info *info)
1846 {
1847         struct neofb_par *par = info->par;
1848         unsigned char type, display;
1849         int w;
1850
1851         // Eventually we will have i2c support.
1852         info->monspecs.modedb = kmalloc(sizeof(struct fb_videomode), GFP_KERNEL);
1853         if (!info->monspecs.modedb)
1854                 return -ENOMEM;
1855         info->monspecs.modedb_len = 1;
1856
1857         /* Determine the panel type */
1858         vga_wgfx(NULL, 0x09, 0x26);
1859         type = vga_rgfx(NULL, 0x21);
1860         display = vga_rgfx(NULL, 0x20);
1861         if (!par->internal_display && !par->external_display) {
1862                 par->internal_display = display & 2 || !(display & 3) ? 1 : 0;
1863                 par->external_display = display & 1;
1864                 printk (KERN_INFO "Autodetected %s display\n",
1865                         par->internal_display && par->external_display ? "simultaneous" :
1866                         par->internal_display ? "internal" : "external");
1867         }
1868
1869         /* Determine panel width -- used in NeoValidMode. */
1870         w = vga_rgfx(NULL, 0x20);
1871         vga_wgfx(NULL, 0x09, 0x00);
1872         switch ((w & 0x18) >> 3) {
1873         case 0x00:
1874                 // 640x480@60
1875                 par->NeoPanelWidth = 640;
1876                 par->NeoPanelHeight = 480;
1877                 memcpy(info->monspecs.modedb, &vesa_modes[3], sizeof(struct fb_videomode));
1878                 break;
1879         case 0x01:
1880                 par->NeoPanelWidth = 800;
1881                 if (par->libretto) {
1882                         par->NeoPanelHeight = 480;
1883                         memcpy(info->monspecs.modedb, &mode800x480, sizeof(struct fb_videomode));
1884                 } else {
1885                         // 800x600@60
1886                         par->NeoPanelHeight = 600;
1887                         memcpy(info->monspecs.modedb, &vesa_modes[8], sizeof(struct fb_videomode));
1888                 }
1889                 break;
1890         case 0x02:
1891                 // 1024x768@60
1892                 par->NeoPanelWidth = 1024;
1893                 par->NeoPanelHeight = 768;
1894                 memcpy(info->monspecs.modedb, &vesa_modes[13], sizeof(struct fb_videomode));
1895                 break;
1896         case 0x03:
1897                 /* 1280x1024@60 panel support needs to be added */
1898 #ifdef NOT_DONE
1899                 par->NeoPanelWidth = 1280;
1900                 par->NeoPanelHeight = 1024;
1901                 memcpy(info->monspecs.modedb, &vesa_modes[20], sizeof(struct fb_videomode));
1902                 break;
1903 #else
1904                 printk(KERN_ERR
1905                        "neofb: Only 640x480, 800x600/480 and 1024x768 panels are currently supported\n");
1906                 return -1;
1907 #endif
1908         default:
1909                 // 640x480@60
1910                 par->NeoPanelWidth = 640;
1911                 par->NeoPanelHeight = 480;
1912                 memcpy(info->monspecs.modedb, &vesa_modes[3], sizeof(struct fb_videomode));
1913                 break;
1914         }
1915
1916         printk(KERN_INFO "Panel is a %dx%d %s %s display\n",
1917                par->NeoPanelWidth,
1918                par->NeoPanelHeight,
1919                (type & 0x02) ? "color" : "monochrome",
1920                (type & 0x10) ? "TFT" : "dual scan");
1921         return 0;
1922 }
1923
1924 static int __devinit neo_init_hw(struct fb_info *info)
1925 {
1926         struct neofb_par *par = info->par;
1927         int videoRam = 896;
1928         int maxClock = 65000;
1929         int CursorMem = 1024;
1930         int CursorOff = 0x100;
1931         int linearSize = 1024;
1932         int maxWidth = 1024;
1933         int maxHeight = 1024;
1934
1935         DBG("neo_init_hw");
1936
1937         neoUnlock();
1938
1939 #if 0
1940         printk(KERN_DEBUG "--- Neo extended register dump ---\n");
1941         for (int w = 0; w < 0x85; w++)
1942                 printk(KERN_DEBUG "CR %p: %p\n", (void *) w,
1943                        (void *) vga_rcrt(NULL, w));
1944         for (int w = 0; w < 0xC7; w++)
1945                 printk(KERN_DEBUG "GR %p: %p\n", (void *) w,
1946                        (void *) vga_rgfx(NULL, w));
1947 #endif
1948         switch (info->fix.accel) {
1949         case FB_ACCEL_NEOMAGIC_NM2070:
1950                 videoRam = 896;
1951                 maxClock = 65000;
1952                 CursorMem = 2048;
1953                 CursorOff = 0x100;
1954                 linearSize = 1024;
1955                 maxWidth = 1024;
1956                 maxHeight = 1024;
1957                 break;
1958         case FB_ACCEL_NEOMAGIC_NM2090:
1959         case FB_ACCEL_NEOMAGIC_NM2093:
1960                 videoRam = 1152;
1961                 maxClock = 80000;
1962                 CursorMem = 2048;
1963                 CursorOff = 0x100;
1964                 linearSize = 2048;
1965                 maxWidth = 1024;
1966                 maxHeight = 1024;
1967                 break;
1968         case FB_ACCEL_NEOMAGIC_NM2097:
1969                 videoRam = 1152;
1970                 maxClock = 80000;
1971                 CursorMem = 1024;
1972                 CursorOff = 0x100;
1973                 linearSize = 2048;
1974                 maxWidth = 1024;
1975                 maxHeight = 1024;
1976                 break;
1977         case FB_ACCEL_NEOMAGIC_NM2160:
1978                 videoRam = 2048;
1979                 maxClock = 90000;
1980                 CursorMem = 1024;
1981                 CursorOff = 0x100;
1982                 linearSize = 2048;
1983                 maxWidth = 1024;
1984                 maxHeight = 1024;
1985                 break;
1986         case FB_ACCEL_NEOMAGIC_NM2200:
1987                 videoRam = 2560;
1988                 maxClock = 110000;
1989                 CursorMem = 1024;
1990                 CursorOff = 0x1000;
1991                 linearSize = 4096;
1992                 maxWidth = 1280;
1993                 maxHeight = 1024;       /* ???? */
1994
1995                 par->neo2200 = (Neo2200 __iomem *) par->mmio_vbase;
1996                 break;
1997         case FB_ACCEL_NEOMAGIC_NM2230:
1998                 videoRam = 3008;
1999                 maxClock = 110000;
2000                 CursorMem = 1024;
2001                 CursorOff = 0x1000;
2002                 linearSize = 4096;
2003                 maxWidth = 1280;
2004                 maxHeight = 1024;       /* ???? */
2005
2006                 par->neo2200 = (Neo2200 __iomem *) par->mmio_vbase;
2007                 break;
2008         case FB_ACCEL_NEOMAGIC_NM2360:
2009                 videoRam = 4096;
2010                 maxClock = 110000;
2011                 CursorMem = 1024;
2012                 CursorOff = 0x1000;
2013                 linearSize = 4096;
2014                 maxWidth = 1280;
2015                 maxHeight = 1024;       /* ???? */
2016
2017                 par->neo2200 = (Neo2200 __iomem *) par->mmio_vbase;
2018                 break;
2019         case FB_ACCEL_NEOMAGIC_NM2380:
2020                 videoRam = 6144;
2021                 maxClock = 110000;
2022                 CursorMem = 1024;
2023                 CursorOff = 0x1000;
2024                 linearSize = 8192;
2025                 maxWidth = 1280;
2026                 maxHeight = 1024;       /* ???? */
2027
2028                 par->neo2200 = (Neo2200 __iomem *) par->mmio_vbase;
2029                 break;
2030         }
2031 /*
2032         info->sprite.size = CursorMem;
2033         info->sprite.scan_align = 1;
2034         info->sprite.buf_align = 1;
2035         info->sprite.flags = FB_PIXMAP_IO;
2036         info->sprite.outbuf = neofb_draw_cursor;
2037 */
2038         par->maxClock = maxClock;
2039         par->cursorOff = CursorOff;
2040         return ((videoRam * 1024));
2041 }
2042
2043
2044 static struct fb_info *__devinit neo_alloc_fb_info(struct pci_dev *dev, const struct
2045                                                    pci_device_id *id)
2046 {
2047         struct fb_info *info;
2048         struct neofb_par *par;
2049
2050         info = framebuffer_alloc(sizeof(struct neofb_par), &dev->dev);
2051
2052         if (!info)
2053                 return NULL;
2054
2055         par = info->par;
2056
2057         info->fix.accel = id->driver_data;
2058
2059         mutex_init(&par->open_lock);
2060         par->pci_burst = !nopciburst;
2061         par->lcd_stretch = !nostretch;
2062         par->libretto = libretto;
2063
2064         par->internal_display = internal;
2065         par->external_display = external;
2066         info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
2067
2068         switch (info->fix.accel) {
2069         case FB_ACCEL_NEOMAGIC_NM2070:
2070                 sprintf(info->fix.id, "MagicGraph 128");
2071                 break;
2072         case FB_ACCEL_NEOMAGIC_NM2090:
2073                 sprintf(info->fix.id, "MagicGraph 128V");
2074                 break;
2075         case FB_ACCEL_NEOMAGIC_NM2093:
2076                 sprintf(info->fix.id, "MagicGraph 128ZV");
2077                 break;
2078         case FB_ACCEL_NEOMAGIC_NM2097:
2079                 sprintf(info->fix.id, "MagicGraph 128ZV+");
2080                 break;
2081         case FB_ACCEL_NEOMAGIC_NM2160:
2082                 sprintf(info->fix.id, "MagicGraph 128XD");
2083                 break;
2084         case FB_ACCEL_NEOMAGIC_NM2200:
2085                 sprintf(info->fix.id, "MagicGraph 256AV");
2086                 info->flags |= FBINFO_HWACCEL_IMAGEBLIT |
2087                                FBINFO_HWACCEL_COPYAREA |
2088                                FBINFO_HWACCEL_FILLRECT;
2089                 break;
2090         case FB_ACCEL_NEOMAGIC_NM2230:
2091                 sprintf(info->fix.id, "MagicGraph 256AV+");
2092                 info->flags |= FBINFO_HWACCEL_IMAGEBLIT |
2093                                FBINFO_HWACCEL_COPYAREA |
2094                                FBINFO_HWACCEL_FILLRECT;
2095                 break;
2096         case FB_ACCEL_NEOMAGIC_NM2360:
2097                 sprintf(info->fix.id, "MagicGraph 256ZX");
2098                 info->flags |= FBINFO_HWACCEL_IMAGEBLIT |
2099                                FBINFO_HWACCEL_COPYAREA |
2100                                FBINFO_HWACCEL_FILLRECT;
2101                 break;
2102         case FB_ACCEL_NEOMAGIC_NM2380:
2103                 sprintf(info->fix.id, "MagicGraph 256XL+");
2104                 info->flags |= FBINFO_HWACCEL_IMAGEBLIT |
2105                                FBINFO_HWACCEL_COPYAREA |
2106                                FBINFO_HWACCEL_FILLRECT;
2107                 break;
2108         }
2109
2110         info->fix.type = FB_TYPE_PACKED_PIXELS;
2111         info->fix.type_aux = 0;
2112         info->fix.xpanstep = 0;
2113         info->fix.ypanstep = 4;
2114         info->fix.ywrapstep = 0;
2115         info->fix.accel = id->driver_data;
2116
2117         info->fbops = &neofb_ops;
2118         info->pseudo_palette = par->palette;
2119         return info;
2120 }
2121
2122 static void neo_free_fb_info(struct fb_info *info)
2123 {
2124         if (info) {
2125                 /*
2126                  * Free the colourmap
2127                  */
2128                 fb_dealloc_cmap(&info->cmap);
2129                 framebuffer_release(info);
2130         }
2131 }
2132
2133 /* --------------------------------------------------------------------- */
2134
2135 static int __devinit neofb_probe(struct pci_dev *dev,
2136                                  const struct pci_device_id *id)
2137 {
2138         struct fb_info *info;
2139         u_int h_sync, v_sync;
2140         int video_len, err;
2141
2142         DBG("neofb_probe");
2143
2144         err = pci_enable_device(dev);
2145         if (err)
2146                 return err;
2147
2148         err = -ENOMEM;
2149         info = neo_alloc_fb_info(dev, id);
2150         if (!info)
2151                 return err;
2152
2153         err = neo_map_mmio(info, dev);
2154         if (err)
2155                 goto err_map_mmio;
2156
2157         err = neo_scan_monitor(info);
2158         if (err)
2159                 goto err_scan_monitor;
2160
2161         video_len = neo_init_hw(info);
2162         if (video_len < 0) {
2163                 err = video_len;
2164                 goto err_init_hw;
2165         }
2166
2167         err = neo_map_video(info, dev, video_len);
2168         if (err)
2169                 goto err_init_hw;
2170
2171         if (!fb_find_mode(&info->var, info, mode_option, NULL, 0,
2172                         info->monspecs.modedb, 16)) {
2173                 printk(KERN_ERR "neofb: Unable to find usable video mode.\n");
2174                 goto err_map_video;
2175         }
2176
2177         /*
2178          * Calculate the hsync and vsync frequencies.  Note that
2179          * we split the 1e12 constant up so that we can preserve
2180          * the precision and fit the results into 32-bit registers.
2181          *  (1953125000 * 512 = 1e12)
2182          */
2183         h_sync = 1953125000 / info->var.pixclock;
2184         h_sync =
2185             h_sync * 512 / (info->var.xres + info->var.left_margin +
2186                             info->var.right_margin + info->var.hsync_len);
2187         v_sync =
2188             h_sync / (info->var.yres + info->var.upper_margin +
2189                       info->var.lower_margin + info->var.vsync_len);
2190
2191         printk(KERN_INFO "neofb v" NEOFB_VERSION
2192                ": %dkB VRAM, using %dx%d, %d.%03dkHz, %dHz\n",
2193                info->fix.smem_len >> 10, info->var.xres,
2194                info->var.yres, h_sync / 1000, h_sync % 1000, v_sync);
2195
2196         if (fb_alloc_cmap(&info->cmap, 256, 0) < 0)
2197                 goto err_map_video;
2198
2199         err = register_framebuffer(info);
2200         if (err < 0)
2201                 goto err_reg_fb;
2202
2203         printk(KERN_INFO "fb%d: %s frame buffer device\n",
2204                info->node, info->fix.id);
2205
2206         /*
2207          * Our driver data
2208          */
2209         pci_set_drvdata(dev, info);
2210         return 0;
2211
2212 err_reg_fb:
2213         fb_dealloc_cmap(&info->cmap);
2214 err_map_video:
2215         neo_unmap_video(info);
2216 err_init_hw:
2217         fb_destroy_modedb(info->monspecs.modedb);
2218 err_scan_monitor:
2219         neo_unmap_mmio(info);
2220 err_map_mmio:
2221         neo_free_fb_info(info);
2222         return err;
2223 }
2224
2225 static void __devexit neofb_remove(struct pci_dev *dev)
2226 {
2227         struct fb_info *info = pci_get_drvdata(dev);
2228
2229         DBG("neofb_remove");
2230
2231         if (info) {
2232                 /*
2233                  * If unregister_framebuffer fails, then
2234                  * we will be leaving hooks that could cause
2235                  * oopsen laying around.
2236                  */
2237                 if (unregister_framebuffer(info))
2238                         printk(KERN_WARNING
2239                                "neofb: danger danger!  Oopsen imminent!\n");
2240
2241                 neo_unmap_video(info);
2242                 fb_destroy_modedb(info->monspecs.modedb);
2243                 neo_unmap_mmio(info);
2244                 neo_free_fb_info(info);
2245
2246                 /*
2247                  * Ensure that the driver data is no longer
2248                  * valid.
2249                  */
2250                 pci_set_drvdata(dev, NULL);
2251         }
2252 }
2253
2254 static struct pci_device_id neofb_devices[] = {
2255         {PCI_VENDOR_ID_NEOMAGIC, PCI_CHIP_NM2070,
2256          PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_NEOMAGIC_NM2070},
2257
2258         {PCI_VENDOR_ID_NEOMAGIC, PCI_CHIP_NM2090,
2259          PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_NEOMAGIC_NM2090},
2260
2261         {PCI_VENDOR_ID_NEOMAGIC, PCI_CHIP_NM2093,
2262          PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_NEOMAGIC_NM2093},
2263
2264         {PCI_VENDOR_ID_NEOMAGIC, PCI_CHIP_NM2097,
2265          PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_NEOMAGIC_NM2097},
2266
2267         {PCI_VENDOR_ID_NEOMAGIC, PCI_CHIP_NM2160,
2268          PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_NEOMAGIC_NM2160},
2269
2270         {PCI_VENDOR_ID_NEOMAGIC, PCI_CHIP_NM2200,
2271          PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_NEOMAGIC_NM2200},
2272
2273         {PCI_VENDOR_ID_NEOMAGIC, PCI_CHIP_NM2230,
2274          PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_NEOMAGIC_NM2230},
2275
2276         {PCI_VENDOR_ID_NEOMAGIC, PCI_CHIP_NM2360,
2277          PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_NEOMAGIC_NM2360},
2278
2279         {PCI_VENDOR_ID_NEOMAGIC, PCI_CHIP_NM2380,
2280          PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_NEOMAGIC_NM2380},
2281
2282         {0, 0, 0, 0, 0, 0, 0}
2283 };
2284
2285 MODULE_DEVICE_TABLE(pci, neofb_devices);
2286
2287 static struct pci_driver neofb_driver = {
2288         .name =         "neofb",
2289         .id_table =     neofb_devices,
2290         .probe =        neofb_probe,
2291         .remove =       __devexit_p(neofb_remove)
2292 };
2293
2294 /* ************************* init in-kernel code ************************** */
2295
2296 #ifndef MODULE
2297 static int __init neofb_setup(char *options)
2298 {
2299         char *this_opt;
2300
2301         DBG("neofb_setup");
2302
2303         if (!options || !*options)
2304                 return 0;
2305
2306         while ((this_opt = strsep(&options, ",")) != NULL) {
2307                 if (!*this_opt)
2308                         continue;
2309
2310                 if (!strncmp(this_opt, "internal", 8))
2311                         internal = 1;
2312                 else if (!strncmp(this_opt, "external", 8))
2313                         external = 1;
2314                 else if (!strncmp(this_opt, "nostretch", 9))
2315                         nostretch = 1;
2316                 else if (!strncmp(this_opt, "nopciburst", 10))
2317                         nopciburst = 1;
2318                 else if (!strncmp(this_opt, "libretto", 8))
2319                         libretto = 1;
2320                 else
2321                         mode_option = this_opt;
2322         }
2323         return 0;
2324 }
2325 #endif  /*  MODULE  */
2326
2327 static int __init neofb_init(void)
2328 {
2329 #ifndef MODULE
2330         char *option = NULL;
2331
2332         if (fb_get_options("neofb", &option))
2333                 return -ENODEV;
2334         neofb_setup(option);
2335 #endif
2336         return pci_register_driver(&neofb_driver);
2337 }
2338
2339 module_init(neofb_init);
2340
2341 #ifdef MODULE
2342 static void __exit neofb_exit(void)
2343 {
2344         pci_unregister_driver(&neofb_driver);
2345 }
2346
2347 module_exit(neofb_exit);
2348 #endif                          /* MODULE */