Merge branch 'devel' of master.kernel.org:/home/rmk/linux-2.6-arm
[linux-2.6] / drivers / video / kyro / fbdev.c
1 /*
2  *  linux/drivers/video/kyro/fbdev.c
3  *
4  *  Copyright (C) 2002 STMicroelectronics
5  *  Copyright (C) 2003, 2004 Paul Mundt
6  *
7  * This file is subject to the terms and conditions of the GNU General Public
8  * License.  See the file COPYING in the main directory of this archive
9  * for more details.
10  */
11
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/mm.h>
17 #include <linux/errno.h>
18 #include <linux/string.h>
19 #include <linux/tty.h>
20 #include <linux/delay.h>
21 #include <linux/fb.h>
22 #include <linux/ioctl.h>
23 #include <linux/init.h>
24 #include <linux/pci.h>
25 #include <asm/io.h>
26 #include <asm/uaccess.h>
27 #ifdef CONFIG_MTRR
28 #include <asm/mtrr.h>
29 #endif
30
31 #include <video/kyro.h>
32
33 #include "STG4000Reg.h"
34 #include "STG4000Interface.h"
35
36 /*
37  * PCI Definitions
38  */
39 #define PCI_VENDOR_ID_ST        0x104a
40 #define PCI_DEVICE_ID_STG4000   0x0010
41
42 #define KHZ2PICOS(a) (1000000000UL/(a))
43
44 /****************************************************************************/
45 static struct fb_fix_screeninfo kyro_fix __devinitdata = {
46         .id             = "ST Kyro",
47         .type           = FB_TYPE_PACKED_PIXELS,
48         .visual         = FB_VISUAL_TRUECOLOR,
49         .accel          = FB_ACCEL_NONE,
50 };
51
52 static struct fb_var_screeninfo kyro_var __devinitdata = {
53         /* 640x480, 16bpp @ 60 Hz */
54         .xres           = 640,
55         .yres           = 480,
56         .xres_virtual   = 640,
57         .yres_virtual   = 480,
58         .bits_per_pixel = 16,
59         .red            = { 11, 5, 0 },
60         .green          = {  5, 6, 0 },
61         .blue           = {  0, 5, 0 },
62         .activate       = FB_ACTIVATE_NOW,
63         .height         = -1,
64         .width          = -1,
65         .pixclock       = KHZ2PICOS(25175),
66         .left_margin    = 48,
67         .right_margin   = 16,
68         .upper_margin   = 33,
69         .lower_margin   = 10,
70         .hsync_len      = 96,
71         .vsync_len      = 2,
72         .vmode          = FB_VMODE_NONINTERLACED,
73 };
74
75 typedef struct {
76         STG4000REG __iomem *pSTGReg;    /* Virtual address of PCI register region */
77         u32 ulNextFreeVidMem;   /* Offset from start of vid mem to next free region */
78         u32 ulOverlayOffset;    /* Offset from start of vid mem to overlay */
79         u32 ulOverlayStride;    /* Interleaved YUV and 422 mode Y stride */
80         u32 ulOverlayUVStride;  /* 422 mode U & V stride */
81 } device_info_t;
82
83 /* global graphics card info structure (one per card) */
84 static device_info_t deviceInfo;
85
86 static char *mode_option __devinitdata = NULL;
87 static int nopan __devinitdata = 0;
88 static int nowrap __devinitdata = 1;
89 #ifdef CONFIG_MTRR
90 static int nomtrr __devinitdata = 0;
91 #endif
92
93 /* PCI driver prototypes */
94 static int kyrofb_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
95 static void kyrofb_remove(struct pci_dev *pdev);
96
97 static struct fb_videomode kyro_modedb[] __devinitdata = {
98         {
99                 /* 640x350 @ 85Hz */
100                 NULL, 85, 640, 350, KHZ2PICOS(31500),
101                 96, 32, 60, 32, 64, 3,
102                 FB_SYNC_HOR_HIGH_ACT, FB_VMODE_NONINTERLACED
103         }, {
104                 /* 640x400 @ 85Hz */
105                 NULL, 85, 640, 400, KHZ2PICOS(31500),
106                 96, 32, 41, 1, 64, 3,
107                 FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
108         }, {
109                 /* 720x400 @ 85Hz */
110                 NULL, 85, 720, 400, KHZ2PICOS(35500),
111                 108, 36, 42, 1, 72, 3,
112                 FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
113         }, {
114                 /* 640x480 @ 60Hz */
115                 NULL, 60, 640, 480, KHZ2PICOS(25175),
116                 48, 16, 33, 10, 96, 2,
117                 0, FB_VMODE_NONINTERLACED
118         }, {
119                 /* 640x480 @ 72Hz */
120                 NULL, 72, 640, 480, KHZ2PICOS(31500),
121                 128, 24, 28, 9, 40, 3,
122                 0, FB_VMODE_NONINTERLACED
123         }, {
124                 /* 640x480 @ 75Hz */
125                 NULL, 75, 640, 480, KHZ2PICOS(31500),
126                 120, 16, 16, 1, 64, 3,
127                 0, FB_VMODE_NONINTERLACED
128         }, {
129                 /* 640x480 @ 85Hz */
130                 NULL, 85, 640, 480, KHZ2PICOS(36000),
131                 80, 56, 25, 1, 56, 3,
132                 0, FB_VMODE_NONINTERLACED
133         }, {
134                 /* 800x600 @ 56Hz */
135                 NULL, 56, 800, 600, KHZ2PICOS(36000),
136                 128, 24, 22, 1, 72, 2,
137                 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
138         }, {
139                 /* 800x600 @ 60Hz */
140                 NULL, 60, 800, 600, KHZ2PICOS(40000),
141                 88, 40, 23, 1, 128, 4,
142                 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
143         }, {
144                 /* 800x600 @ 72Hz */
145                 NULL, 72, 800, 600, KHZ2PICOS(50000),
146                 64, 56, 23, 37, 120, 6,
147                 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
148         }, {
149                 /* 800x600 @ 75Hz */
150                 NULL, 75, 800, 600, KHZ2PICOS(49500),
151                 160, 16, 21, 1, 80, 3,
152                 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
153         }, {
154                 /* 800x600 @ 85Hz */
155                 NULL, 85, 800, 600, KHZ2PICOS(56250),
156                 152, 32, 27, 1, 64, 3,
157                 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
158         }, {
159                 /* 1024x768 @ 60Hz */
160                 NULL, 60, 1024, 768, KHZ2PICOS(65000),
161                 160, 24, 29, 3, 136, 6,
162                 0, FB_VMODE_NONINTERLACED
163         }, {
164                 /* 1024x768 @ 70Hz */
165                 NULL, 70, 1024, 768, KHZ2PICOS(75000),
166                 144, 24, 29, 3, 136, 6,
167                 0, FB_VMODE_NONINTERLACED
168         }, {
169                 /* 1024x768 @ 75Hz */
170                 NULL, 75, 1024, 768, KHZ2PICOS(78750),
171                 176, 16, 28, 1, 96, 3,
172                 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
173         }, {
174                 /* 1024x768 @ 85Hz */
175                 NULL, 85, 1024, 768, KHZ2PICOS(94500),
176                 208, 48, 36, 1, 96, 3,
177                 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
178         }, {
179                 /* 1152x864 @ 75Hz */
180                 NULL, 75, 1152, 864, KHZ2PICOS(108000),
181                 256, 64, 32, 1, 128, 3,
182                 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
183         }, {
184                 /* 1280x960 @ 60Hz */
185                 NULL, 60, 1280, 960, KHZ2PICOS(108000),
186                 312, 96, 36, 1, 112, 3,
187                 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
188         }, {
189                 /* 1280x960 @ 85Hz */
190                 NULL, 85, 1280, 960, KHZ2PICOS(148500),
191                 224, 64, 47, 1, 160, 3,
192                 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
193         }, {
194                 /* 1280x1024 @ 60Hz */
195                 NULL, 60, 1280, 1024, KHZ2PICOS(108000),
196                 248, 48, 38, 1, 112, 3,
197                 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
198         }, {
199                 /* 1280x1024 @ 75Hz */
200                 NULL, 75, 1280, 1024, KHZ2PICOS(135000),
201                 248, 16, 38, 1, 144, 3,
202                 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
203         }, {
204                 /* 1280x1024 @ 85Hz */
205                 NULL, 85, 1280, 1024, KHZ2PICOS(157500),
206                 224, 64, 44, 1, 160, 3,
207                 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
208         }, {
209                 /* 1600x1200 @ 60Hz */
210                 NULL, 60, 1600, 1200, KHZ2PICOS(162000),
211                 304, 64, 46, 1, 192, 3,
212                 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
213         }, {
214                 /* 1600x1200 @ 65Hz */
215                 NULL, 65, 1600, 1200, KHZ2PICOS(175500),
216                 304, 64, 46, 1, 192, 3,
217                 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
218         }, {
219                 /* 1600x1200 @ 70Hz */
220                 NULL, 70, 1600, 1200, KHZ2PICOS(189000),
221                 304, 64, 46, 1, 192, 3,
222                 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
223         }, {
224                 /* 1600x1200 @ 75Hz */
225                 NULL, 75, 1600, 1200, KHZ2PICOS(202500),
226                 304, 64, 46, 1, 192, 3,
227                 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
228         }, {
229                 /* 1600x1200 @ 85Hz */
230                 NULL, 85, 1600, 1200, KHZ2PICOS(229500),
231                 304, 64, 46, 1, 192, 3,
232                 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
233         }, {
234                 /* 1792x1344 @ 60Hz */
235                 NULL, 60, 1792, 1344, KHZ2PICOS(204750),
236                 328, 128, 46, 1, 200, 3,
237                 FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
238         }, {
239                 /* 1792x1344 @ 75Hz */
240                 NULL, 75, 1792, 1344, KHZ2PICOS(261000),
241                 352, 96, 69, 1, 216, 3,
242                 FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
243         }, {
244                 /* 1856x1392 @ 60Hz */
245                 NULL, 60, 1856, 1392, KHZ2PICOS(218250),
246                 352, 96, 43, 1, 224, 3,
247                 FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
248         }, {
249                 /* 1856x1392 @ 75Hz */
250                 NULL, 75, 1856, 1392, KHZ2PICOS(288000),
251                 352, 128, 104, 1, 224, 3,
252                 FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
253         }, {
254                 /* 1920x1440 @ 60Hz */
255                 NULL, 60, 1920, 1440, KHZ2PICOS(234000),
256                 344, 128, 56, 1, 208, 3,
257                 FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
258         }, {
259                 /* 1920x1440 @ 75Hz */
260                 NULL, 75, 1920, 1440, KHZ2PICOS(297000),
261                 352, 144, 56, 1, 224, 3,
262                 FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
263         },
264 };
265 #define NUM_TOTAL_MODES ARRAY_SIZE(kyro_modedb)
266
267 /*
268  * This needs to be kept ordered corresponding to kyro_modedb.
269  */
270 enum {
271         VMODE_640_350_85,
272         VMODE_640_400_85,
273         VMODE_720_400_85,
274         VMODE_640_480_60,
275         VMODE_640_480_72,
276         VMODE_640_480_75,
277         VMODE_640_480_85,
278         VMODE_800_600_56,
279         VMODE_800_600_60,
280         VMODE_800_600_72,
281         VMODE_800_600_75,
282         VMODE_800_600_85,
283         VMODE_1024_768_60,
284         VMODE_1024_768_70,
285         VMODE_1024_768_75,
286         VMODE_1024_768_85,
287         VMODE_1152_864_75,
288         VMODE_1280_960_60,
289         VMODE_1280_960_85,
290         VMODE_1280_1024_60,
291         VMODE_1280_1024_75,
292         VMODE_1280_1024_85,
293         VMODE_1600_1200_60,
294         VMODE_1600_1200_65,
295         VMODE_1600_1200_70,
296         VMODE_1600_1200_75,
297         VMODE_1600_1200_85,
298         VMODE_1792_1344_60,
299         VMODE_1792_1344_75,
300         VMODE_1856_1392_60,
301         VMODE_1856_1392_75,
302         VMODE_1920_1440_60,
303         VMODE_1920_1440_75,
304 };
305
306 /* Accessors */
307 static int kyro_dev_video_mode_set(struct fb_info *info)
308 {
309         struct kyrofb_info *par = info->par;
310
311         /* Turn off display */
312         StopVTG(deviceInfo.pSTGReg);
313         DisableRamdacOutput(deviceInfo.pSTGReg);
314
315         /* Bring us out of VGA and into Hi-Res mode, if not already. */
316         DisableVGA(deviceInfo.pSTGReg);
317
318         if (InitialiseRamdac(deviceInfo.pSTGReg,
319                              info->var.bits_per_pixel,
320                              info->var.xres, info->var.yres,
321                              par->HSP, par->VSP, &par->PIXCLK) < 0)
322                 return -EINVAL;
323
324         SetupVTG(deviceInfo.pSTGReg, par);
325
326         ResetOverlayRegisters(deviceInfo.pSTGReg);
327
328         /* Turn on display in new mode */
329         EnableRamdacOutput(deviceInfo.pSTGReg);
330         StartVTG(deviceInfo.pSTGReg);
331
332         deviceInfo.ulNextFreeVidMem = info->var.xres * info->var.yres *
333                                       info->var.bits_per_pixel;
334         deviceInfo.ulOverlayOffset = 0;
335
336         return 0;
337 }
338
339 static int kyro_dev_overlay_create(u32 ulWidth,
340                                    u32 ulHeight, int bLinear)
341 {
342         u32 offset;
343         u32 stride, uvStride;
344
345         if (deviceInfo.ulOverlayOffset != 0)
346                 /*
347                  * Can only create one overlay without resetting the card or
348                  * changing display mode
349                  */
350                 return -EINVAL;
351
352         ResetOverlayRegisters(deviceInfo.pSTGReg);
353
354         /* Overlays are addressed in multiples of 16bytes or 32bytes, so make
355          * sure the start offset is on an appropriate boundary.
356          */
357         offset = deviceInfo.ulNextFreeVidMem;
358         if ((offset & 0x1f) != 0) {
359                 offset = (offset + 32L) & 0xffffffE0L;
360         }
361
362         if (CreateOverlaySurface(deviceInfo.pSTGReg, ulWidth, ulHeight,
363                                  bLinear, offset, &stride, &uvStride) < 0)
364                 return -EINVAL;
365
366         deviceInfo.ulOverlayOffset = offset;
367         deviceInfo.ulOverlayStride = stride;
368         deviceInfo.ulOverlayUVStride = uvStride;
369         deviceInfo.ulNextFreeVidMem = offset + (ulHeight * stride) + (ulHeight * 2 * uvStride);
370
371         SetOverlayBlendMode(deviceInfo.pSTGReg, GLOBAL_ALPHA, 0xf, 0x0);
372
373         return 0;
374 }
375
376 static int kyro_dev_overlay_viewport_set(u32 x, u32 y, u32 ulWidth, u32 ulHeight)
377 {
378         if (deviceInfo.ulOverlayOffset == 0)
379                 /* probably haven't called CreateOverlay yet */
380                 return -EINVAL;
381
382         /* Stop Ramdac Output */
383         DisableRamdacOutput(deviceInfo.pSTGReg);
384
385         SetOverlayViewPort(deviceInfo.pSTGReg,
386                            x, y, x + ulWidth - 1, y + ulHeight - 1);
387
388         EnableOverlayPlane(deviceInfo.pSTGReg);
389         /* Start Ramdac Output */
390         EnableRamdacOutput(deviceInfo.pSTGReg);
391
392         return 0;
393 }
394
395 static inline unsigned long get_line_length(int x, int bpp)
396 {
397         return (unsigned long)((((x*bpp)+31)&~31) >> 3);
398 }
399
400 static int kyrofb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
401 {
402         struct kyrofb_info *par = info->par;
403
404         if (var->bits_per_pixel != 16 && var->bits_per_pixel != 32) {
405                 printk(KERN_WARNING "kyrofb: depth not supported: %u\n", var->bits_per_pixel);
406                 return -EINVAL;
407         }
408
409         switch (var->bits_per_pixel) {
410         case 16:
411                 var->red.offset = 11;
412                 var->red.length = 5;
413                 var->green.offset = 5;
414                 var->green.length = 6;
415                 var->blue.length = 5;
416                 break;
417         case 32:
418                 var->transp.offset = 24;
419                 var->red.offset = 16;
420                 var->green.offset = 8;
421                 var->blue.offset = 0;
422
423                 var->red.length = 8;
424                 var->green.length = 8;
425                 var->blue.length = 8;
426                 var->transp.length = 8;
427                 break;
428         }
429
430         /* Height/Width of picture in mm */
431         var->height = var->width = -1;
432
433         /* Timing information. All values are in picoseconds */
434
435         /* par->PIXCLK is in 100Hz units. Convert to picoseconds -
436          * ensuring we do not exceed 32 bit precision
437          */
438         /*
439          * XXX: Enabling this really screws over the pixclock value when we
440          * read it back with fbset. As such, leaving this commented out appears
441          * to do the right thing (at least for now) .. bearing in mind that we
442          * have infact already done the KHZ2PICOS conversion in both the modedb
443          * and kyro_var. -- PFM.
444          */
445 //      var->pixclock = 1000000000 / (par->PIXCLK / 10);
446
447         /* the header file claims we should use picoseconds
448          * - nobody else does though, the all use pixels and lines
449          * of h and v sizes. Both options here.
450          */
451
452         /*
453          * If we're being called by __fb_try_mode(), then we don't want to
454          * override any of the var settings that we've already parsed
455          * from our modedb. -- PFM.
456          */
457         if ((var->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_TEST)
458                 return 0;
459
460         var->left_margin = par->HBP;
461         var->hsync_len = par->HST;
462         var->right_margin = par->HFP;
463
464         var->upper_margin = par->VBP;
465         var->vsync_len = par->VST;
466         var->lower_margin = par->VFP;
467
468         if (par->HSP == 1)
469                 var->sync |= FB_SYNC_HOR_HIGH_ACT;
470         if (par->VSP == 1)
471                 var->sync |= FB_SYNC_VERT_HIGH_ACT;
472
473         return 0;
474 }
475
476 static int kyrofb_set_par(struct fb_info *info)
477 {
478         struct kyrofb_info *par = info->par;
479         unsigned long lineclock;
480         unsigned long frameclock;
481
482         /* Actual resolution */
483         par->XRES = info->var.xres;
484         par->YRES = info->var.yres;
485
486         /* pixel depth */
487         par->PIXDEPTH = info->var.bits_per_pixel;
488
489         /* Refresh rate */
490         /* time for a line in ns */
491         lineclock = (info->var.pixclock * (info->var.xres +
492                                     info->var.right_margin +
493                                     info->var.hsync_len +
494                                     info->var.left_margin)) / 1000;
495
496
497         /* time for a frame in ns (precision in 32bpp) */
498         frameclock = lineclock * (info->var.yres +
499                                   info->var.lower_margin +
500                                   info->var.vsync_len +
501                                   info->var.upper_margin);
502
503         /* Calculate refresh rate and horrizontal clocks */
504         par->VFREQ = (1000000000 + (frameclock / 2)) / frameclock;
505         par->HCLK = (1000000000 + (lineclock / 2)) / lineclock;
506         par->PIXCLK = ((1000000000 + (info->var.pixclock / 2))
507                                         / info->var.pixclock) * 10;
508
509         /* calculate horizontal timings */
510         par->HFP = info->var.right_margin;
511         par->HST = info->var.hsync_len;
512         par->HBP = info->var.left_margin;
513         par->HTot = par->XRES + par->HBP + par->HST + par->HFP;
514
515         /* calculate vertical timings */
516         par->VFP = info->var.lower_margin;
517         par->VST = info->var.vsync_len;
518         par->VBP = info->var.upper_margin;
519         par->VTot = par->YRES + par->VBP + par->VST + par->VFP;
520
521         par->HSP = (info->var.sync & FB_SYNC_HOR_HIGH_ACT) ? 1 : 0;
522         par->VSP = (info->var.sync & FB_SYNC_VERT_HIGH_ACT) ? 1 : 0;
523
524         kyro_dev_video_mode_set(info);
525
526         /* length of a line in bytes    */
527         info->fix.line_length = get_line_length(par->XRES, par->PIXDEPTH);
528         info->fix.visual = FB_VISUAL_TRUECOLOR;
529
530         return 0;
531 }
532
533 static int kyrofb_setcolreg(u_int regno, u_int red, u_int green,
534                             u_int blue, u_int transp, struct fb_info *info)
535 {
536         struct kyrofb_info *par = info->par;
537
538         if (regno > 255)
539                 return 1;       /* Invalid register */
540
541         if (regno < 16) {
542                 switch (info->var.bits_per_pixel) {
543                 case 16:
544                         par->palette[regno] =
545                              (red   & 0xf800) |
546                             ((green & 0xfc00) >> 5) |
547                             ((blue  & 0xf800) >> 11);
548                         break;
549                 case 32:
550                         red >>= 8; green >>= 8; blue >>= 8; transp >>= 8;
551                         par->palette[regno] =
552                             (transp << 24) | (red << 16) | (green << 8) | blue;
553                         break;
554                 }
555         }
556
557         return 0;
558 }
559
560 #ifndef MODULE
561 static int __init kyrofb_setup(char *options)
562 {
563         char *this_opt;
564
565         if (!options || !*options)
566                 return 0;
567
568         while ((this_opt = strsep(&options, ","))) {
569                 if (!*this_opt)
570                         continue;
571                 if (strcmp(this_opt, "nopan") == 0) {
572                         nopan = 1;
573                 } else if (strcmp(this_opt, "nowrap") == 0) {
574                         nowrap = 1;
575 #ifdef CONFIG_MTRR
576                 } else if (strcmp(this_opt, "nomtrr") == 0) {
577                         nomtrr = 1;
578 #endif
579                 } else {
580                         mode_option = this_opt;
581                 }
582         }
583
584         return 0;
585 }
586 #endif
587
588 static int kyrofb_ioctl(struct fb_info *info,
589                         unsigned int cmd, unsigned long arg)
590 {
591         overlay_create ol_create;
592         overlay_viewport_set ol_viewport_set;
593         void __user *argp = (void __user *)arg;
594
595         switch (cmd) {
596         case KYRO_IOCTL_OVERLAY_CREATE:
597                 if (copy_from_user(&ol_create, argp, sizeof(overlay_create)))
598                         return -EFAULT;
599
600                 if (kyro_dev_overlay_create(ol_create.ulWidth,
601                                             ol_create.ulHeight, 0) < 0) {
602                         printk(KERN_ERR "Kyro FB: failed to create overlay surface.\n");
603
604                         return -EINVAL;
605                 }
606                 break;
607         case KYRO_IOCTL_OVERLAY_VIEWPORT_SET:
608                 if (copy_from_user(&ol_viewport_set, argp,
609                                sizeof(overlay_viewport_set)))
610                         return -EFAULT;
611
612                 if (kyro_dev_overlay_viewport_set(ol_viewport_set.xOrgin,
613                                                   ol_viewport_set.yOrgin,
614                                                   ol_viewport_set.xSize,
615                                                   ol_viewport_set.ySize) != 0)
616                 {
617                         printk(KERN_ERR "Kyro FB: failed to create overlay viewport.\n");
618                         return -EINVAL;
619                 }
620                 break;
621         case KYRO_IOCTL_SET_VIDEO_MODE:
622                 {
623                         printk(KERN_ERR "Kyro FB: KYRO_IOCTL_SET_VIDEO_MODE is"
624                                 "obsolete, use the appropriate fb_ioctl()"
625                                 "command instead.\n");
626                         return -EINVAL;
627                 }
628                 break;
629         case KYRO_IOCTL_UVSTRIDE:
630                 if (copy_to_user(argp, &deviceInfo.ulOverlayUVStride, sizeof(unsigned long)))
631                         return -EFAULT;
632                 break;
633         case KYRO_IOCTL_STRIDE:
634                 if (copy_to_user(argp, &deviceInfo.ulOverlayStride, sizeof(unsigned long)))
635                         return -EFAULT;
636                 break;
637         case KYRO_IOCTL_OVERLAY_OFFSET:
638                 if (copy_to_user(argp, &deviceInfo.ulOverlayOffset, sizeof(unsigned long)))
639                         return -EFAULT;
640                 break;
641         }
642
643         return 0;
644 }
645
646 static struct pci_device_id kyrofb_pci_tbl[] = {
647         { PCI_VENDOR_ID_ST, PCI_DEVICE_ID_STG4000,
648           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
649         { 0, }
650 };
651
652 MODULE_DEVICE_TABLE(pci, kyrofb_pci_tbl);
653
654 static struct pci_driver kyrofb_pci_driver = {
655         .name           = "kyrofb",
656         .id_table       = kyrofb_pci_tbl,
657         .probe          = kyrofb_probe,
658         .remove         = __devexit_p(kyrofb_remove),
659 };
660
661 static struct fb_ops kyrofb_ops = {
662         .owner          = THIS_MODULE,
663         .fb_check_var   = kyrofb_check_var,
664         .fb_set_par     = kyrofb_set_par,
665         .fb_setcolreg   = kyrofb_setcolreg,
666         .fb_ioctl       = kyrofb_ioctl,
667         .fb_fillrect    = cfb_fillrect,
668         .fb_copyarea    = cfb_copyarea,
669         .fb_imageblit   = cfb_imageblit,
670 };
671
672 static int __devinit kyrofb_probe(struct pci_dev *pdev,
673                                   const struct pci_device_id *ent)
674 {
675         struct fb_info *info;
676         struct kyrofb_info *currentpar;
677         unsigned long size;
678         int err;
679
680         if ((err = pci_enable_device(pdev))) {
681                 printk(KERN_WARNING "kyrofb: Can't enable pdev: %d\n", err);
682                 return err;
683         }
684
685         info = framebuffer_alloc(sizeof(struct kyrofb_info), &pdev->dev);
686         if (!info)
687                 return -ENOMEM;
688
689         currentpar = info->par;
690
691         kyro_fix.smem_start = pci_resource_start(pdev, 0);
692         kyro_fix.smem_len   = pci_resource_len(pdev, 0);
693         kyro_fix.mmio_start = pci_resource_start(pdev, 1);
694         kyro_fix.mmio_len   = pci_resource_len(pdev, 1);
695
696         currentpar->regbase = deviceInfo.pSTGReg =
697                 ioremap_nocache(kyro_fix.mmio_start, kyro_fix.mmio_len);
698
699         info->screen_base = ioremap_nocache(kyro_fix.smem_start,
700                                             kyro_fix.smem_len);
701
702 #ifdef CONFIG_MTRR
703         if (!nomtrr)
704                 currentpar->mtrr_handle =
705                         mtrr_add(kyro_fix.smem_start,
706                                  kyro_fix.smem_len,
707                                  MTRR_TYPE_WRCOMB, 1);
708 #endif
709
710         kyro_fix.ypanstep       = nopan ? 0 : 1;
711         kyro_fix.ywrapstep      = nowrap ? 0 : 1;
712
713         info->fbops             = &kyrofb_ops;
714         info->fix               = kyro_fix;
715         info->pseudo_palette    = currentpar->palette;
716         info->flags             = FBINFO_DEFAULT;
717
718         SetCoreClockPLL(deviceInfo.pSTGReg, pdev);
719
720         deviceInfo.ulNextFreeVidMem = 0;
721         deviceInfo.ulOverlayOffset = 0;
722
723         /* This should give a reasonable default video mode */
724         if (!fb_find_mode(&info->var, info, mode_option, kyro_modedb,
725                           NUM_TOTAL_MODES, &kyro_modedb[VMODE_1024_768_75], 32))
726                 info->var = kyro_var;
727
728         fb_alloc_cmap(&info->cmap, 256, 0);
729
730         kyrofb_set_par(info);
731         kyrofb_check_var(&info->var, info);
732
733         size = get_line_length(info->var.xres_virtual,
734                                info->var.bits_per_pixel);
735         size *= info->var.yres_virtual;
736
737         fb_memset(info->screen_base, 0, size);
738
739         if (register_framebuffer(info) < 0)
740                 goto out_unmap;
741
742         printk("fb%d: %s frame buffer device, at %dx%d@%d using %ldk/%ldk of VRAM\n",
743                info->node, info->fix.id, info->var.xres,
744                info->var.yres, info->var.bits_per_pixel, size >> 10,
745                (unsigned long)info->fix.smem_len >> 10);
746
747         pci_set_drvdata(pdev, info);
748
749         return 0;
750
751 out_unmap:
752         iounmap(currentpar->regbase);
753         iounmap(info->screen_base);
754         framebuffer_release(info);
755
756         return -EINVAL;
757 }
758
759 static void __devexit kyrofb_remove(struct pci_dev *pdev)
760 {
761         struct fb_info *info = pci_get_drvdata(pdev);
762         struct kyrofb_info *par = info->par;
763
764         /* Reset the board */
765         StopVTG(deviceInfo.pSTGReg);
766         DisableRamdacOutput(deviceInfo.pSTGReg);
767
768         /* Sync up the PLL */
769         SetCoreClockPLL(deviceInfo.pSTGReg, pdev);
770
771         deviceInfo.ulNextFreeVidMem = 0;
772         deviceInfo.ulOverlayOffset = 0;
773
774         iounmap(info->screen_base);
775         iounmap(par->regbase);
776
777 #ifdef CONFIG_MTRR
778         if (par->mtrr_handle)
779                 mtrr_del(par->mtrr_handle,
780                          info->fix.smem_start,
781                          info->fix.smem_len);
782 #endif
783
784         unregister_framebuffer(info);
785         pci_set_drvdata(pdev, NULL);
786         framebuffer_release(info);
787 }
788
789 static int __init kyrofb_init(void)
790 {
791 #ifndef MODULE
792         char *option = NULL;
793
794         if (fb_get_options("kyrofb", &option))
795                 return -ENODEV;
796         kyrofb_setup(option);
797 #endif
798         return pci_register_driver(&kyrofb_pci_driver);
799 }
800
801 static void __exit kyrofb_exit(void)
802 {
803         pci_unregister_driver(&kyrofb_pci_driver);
804 }
805
806 module_init(kyrofb_init);
807
808 #ifdef MODULE
809 module_exit(kyrofb_exit);
810 #endif
811
812 MODULE_AUTHOR("STMicroelectronics; Paul Mundt <lethal@linux-sh.org>");
813 MODULE_LICENSE("GPL");