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