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