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