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