1/*
2 * framebuffer driver for VBE 2.0 compliant graphic boards
3 *
4 * switching to graphics mode happens at boot time (while
5 * running in real mode, see arch/i386/boot/video.S).
6 *
7 * (c) 1998 Gerd Knorr <kraxel@goldbach.in-berlin.de>
8 *
9 */
10
11#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/errno.h>
14#include <linux/string.h>
15#include <linux/mm.h>
16#include <linux/delay.h>
17#include <linux/fb.h>
18#include <linux/ioport.h>
19#include <linux/init.h>
20#include <linux/platform_device.h>
21#include <linux/screen_info.h>
22#include <linux/io.h>
23
24#include <video/vga.h>
25
26#define dac_reg	(0x3c8)
27#define dac_val	(0x3c9)
28
29/* --------------------------------------------------------------------- */
30
31struct vesafb_par {
32	u32 pseudo_palette[256];
33	int wc_cookie;
34};
35
36static struct fb_var_screeninfo vesafb_defined = {
37	.activate	= FB_ACTIVATE_NOW,
38	.height		= -1,
39	.width		= -1,
40	.right_margin	= 32,
41	.upper_margin	= 16,
42	.lower_margin	= 4,
43	.vsync_len	= 4,
44	.vmode		= FB_VMODE_NONINTERLACED,
45};
46
47static struct fb_fix_screeninfo vesafb_fix = {
48	.id	= "VESA VGA",
49	.type	= FB_TYPE_PACKED_PIXELS,
50	.accel	= FB_ACCEL_NONE,
51};
52
53static int   inverse    __read_mostly;
54static int   mtrr       __read_mostly;		/* disable mtrr */
55static int   vram_remap;			/* Set amount of memory to be used */
56static int   vram_total;			/* Set total amount of memory */
57static int   pmi_setpal __read_mostly = 1;	/* pmi for palette changes ??? */
58static int   ypan       __read_mostly;		/* 0..nothing, 1..ypan, 2..ywrap */
59static void  (*pmi_start)(void) __read_mostly;
60static void  (*pmi_pal)  (void) __read_mostly;
61static int   depth      __read_mostly;
62static int   vga_compat __read_mostly;
63/* --------------------------------------------------------------------- */
64
65static int vesafb_pan_display(struct fb_var_screeninfo *var,
66                              struct fb_info *info)
67{
68#ifdef __i386__
69	int offset;
70
71	offset = (var->yoffset * info->fix.line_length + var->xoffset) / 4;
72
73        __asm__ __volatile__(
74                "call *(%%edi)"
75                : /* no return value */
76                : "a" (0x4f07),         /* EAX */
77                  "b" (0),              /* EBX */
78                  "c" (offset),         /* ECX */
79                  "d" (offset >> 16),   /* EDX */
80                  "D" (&pmi_start));    /* EDI */
81#endif
82	return 0;
83}
84
85static int vesa_setpalette(int regno, unsigned red, unsigned green,
86			    unsigned blue)
87{
88	int shift = 16 - depth;
89	int err = -EINVAL;
90
91/*
92 * Try VGA registers first...
93 */
94	if (vga_compat) {
95		outb_p(regno,       dac_reg);
96		outb_p(red   >> shift, dac_val);
97		outb_p(green >> shift, dac_val);
98		outb_p(blue  >> shift, dac_val);
99		err = 0;
100	}
101
102#ifdef __i386__
103/*
104 * Fallback to the PMI....
105 */
106	if (err && pmi_setpal) {
107		struct { u_char blue, green, red, pad; } entry;
108
109		entry.red   = red   >> shift;
110		entry.green = green >> shift;
111		entry.blue  = blue  >> shift;
112		entry.pad   = 0;
113	        __asm__ __volatile__(
114                "call *(%%esi)"
115                : /* no return value */
116                : "a" (0x4f09),         /* EAX */
117                  "b" (0),              /* EBX */
118                  "c" (1),              /* ECX */
119                  "d" (regno),          /* EDX */
120                  "D" (&entry),         /* EDI */
121                  "S" (&pmi_pal));      /* ESI */
122		err = 0;
123	}
124#endif
125
126	return err;
127}
128
129static int vesafb_setcolreg(unsigned regno, unsigned red, unsigned green,
130			    unsigned blue, unsigned transp,
131			    struct fb_info *info)
132{
133	int err = 0;
134
135	/*
136	 *  Set a single color register. The values supplied are
137	 *  already rounded down to the hardware's capabilities
138	 *  (according to the entries in the `var' structure). Return
139	 *  != 0 for invalid regno.
140	 */
141
142	if (regno >= info->cmap.len)
143		return 1;
144
145	if (info->var.bits_per_pixel == 8)
146		err = vesa_setpalette(regno,red,green,blue);
147	else if (regno < 16) {
148		switch (info->var.bits_per_pixel) {
149		case 16:
150			if (info->var.red.offset == 10) {
151				/* 1:5:5:5 */
152				((u32*) (info->pseudo_palette))[regno] =
153					((red   & 0xf800) >>  1) |
154					((green & 0xf800) >>  6) |
155					((blue  & 0xf800) >> 11);
156			} else {
157				/* 0:5:6:5 */
158				((u32*) (info->pseudo_palette))[regno] =
159					((red   & 0xf800)      ) |
160					((green & 0xfc00) >>  5) |
161					((blue  & 0xf800) >> 11);
162			}
163			break;
164		case 24:
165		case 32:
166			red   >>= 8;
167			green >>= 8;
168			blue  >>= 8;
169			((u32 *)(info->pseudo_palette))[regno] =
170				(red   << info->var.red.offset)   |
171				(green << info->var.green.offset) |
172				(blue  << info->var.blue.offset);
173			break;
174		}
175	}
176
177	return err;
178}
179
180static void vesafb_destroy(struct fb_info *info)
181{
182	struct vesafb_par *par = info->par;
183
184	fb_dealloc_cmap(&info->cmap);
185	arch_phys_wc_del(par->wc_cookie);
186	if (info->screen_base)
187		iounmap(info->screen_base);
188	release_mem_region(info->apertures->ranges[0].base, info->apertures->ranges[0].size);
189}
190
191static struct fb_ops vesafb_ops = {
192	.owner		= THIS_MODULE,
193	.fb_destroy     = vesafb_destroy,
194	.fb_setcolreg	= vesafb_setcolreg,
195	.fb_pan_display	= vesafb_pan_display,
196	.fb_fillrect	= cfb_fillrect,
197	.fb_copyarea	= cfb_copyarea,
198	.fb_imageblit	= cfb_imageblit,
199};
200
201static int vesafb_setup(char *options)
202{
203	char *this_opt;
204
205	if (!options || !*options)
206		return 0;
207
208	while ((this_opt = strsep(&options, ",")) != NULL) {
209		if (!*this_opt) continue;
210
211		if (! strcmp(this_opt, "inverse"))
212			inverse=1;
213		else if (! strcmp(this_opt, "redraw"))
214			ypan=0;
215		else if (! strcmp(this_opt, "ypan"))
216			ypan=1;
217		else if (! strcmp(this_opt, "ywrap"))
218			ypan=2;
219		else if (! strcmp(this_opt, "vgapal"))
220			pmi_setpal=0;
221		else if (! strcmp(this_opt, "pmipal"))
222			pmi_setpal=1;
223		else if (! strncmp(this_opt, "mtrr:", 5))
224			mtrr = simple_strtoul(this_opt+5, NULL, 0);
225		else if (! strcmp(this_opt, "nomtrr"))
226			mtrr=0;
227		else if (! strncmp(this_opt, "vtotal:", 7))
228			vram_total = simple_strtoul(this_opt+7, NULL, 0);
229		else if (! strncmp(this_opt, "vremap:", 7))
230			vram_remap = simple_strtoul(this_opt+7, NULL, 0);
231	}
232	return 0;
233}
234
235static int vesafb_probe(struct platform_device *dev)
236{
237	struct fb_info *info;
238	struct vesafb_par *par;
239	int i, err;
240	unsigned int size_vmode;
241	unsigned int size_remap;
242	unsigned int size_total;
243	char *option = NULL;
244
245	/* ignore error return of fb_get_options */
246	fb_get_options("vesafb", &option);
247	vesafb_setup(option);
248
249	if (screen_info.orig_video_isVGA != VIDEO_TYPE_VLFB)
250		return -ENODEV;
251
252	vga_compat = (screen_info.capabilities & 2) ? 0 : 1;
253	vesafb_fix.smem_start = screen_info.lfb_base;
254	vesafb_defined.bits_per_pixel = screen_info.lfb_depth;
255	if (15 == vesafb_defined.bits_per_pixel)
256		vesafb_defined.bits_per_pixel = 16;
257	vesafb_defined.xres = screen_info.lfb_width;
258	vesafb_defined.yres = screen_info.lfb_height;
259	vesafb_fix.line_length = screen_info.lfb_linelength;
260	vesafb_fix.visual   = (vesafb_defined.bits_per_pixel == 8) ?
261		FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
262
263	/*   size_vmode -- that is the amount of memory needed for the
264	 *                 used video mode, i.e. the minimum amount of
265	 *                 memory we need. */
266	size_vmode = vesafb_defined.yres * vesafb_fix.line_length;
267
268	/*   size_total -- all video memory we have. Used for mtrr
269	 *                 entries, resource allocation and bounds
270	 *                 checking. */
271	size_total = screen_info.lfb_size * 65536;
272	if (vram_total)
273		size_total = vram_total * 1024 * 1024;
274	if (size_total < size_vmode)
275		size_total = size_vmode;
276
277	/*   size_remap -- the amount of video memory we are going to
278	 *                 use for vesafb.  With modern cards it is no
279	 *                 option to simply use size_total as that
280	 *                 wastes plenty of kernel address space. */
281	size_remap  = size_vmode * 2;
282	if (vram_remap)
283		size_remap = vram_remap * 1024 * 1024;
284	if (size_remap < size_vmode)
285		size_remap = size_vmode;
286	if (size_remap > size_total)
287		size_remap = size_total;
288	vesafb_fix.smem_len = size_remap;
289
290#ifndef __i386__
291	screen_info.vesapm_seg = 0;
292#endif
293
294	if (!request_mem_region(vesafb_fix.smem_start, size_total, "vesafb")) {
295		printk(KERN_WARNING
296		       "vesafb: cannot reserve video memory at 0x%lx\n",
297			vesafb_fix.smem_start);
298		/* We cannot make this fatal. Sometimes this comes from magic
299		   spaces our resource handlers simply don't know about */
300	}
301
302	info = framebuffer_alloc(sizeof(struct vesafb_par), &dev->dev);
303	if (!info) {
304		release_mem_region(vesafb_fix.smem_start, size_total);
305		return -ENOMEM;
306	}
307	platform_set_drvdata(dev, info);
308	par = info->par;
309	info->pseudo_palette = par->pseudo_palette;
310
311	/* set vesafb aperture size for generic probing */
312	info->apertures = alloc_apertures(1);
313	if (!info->apertures) {
314		err = -ENOMEM;
315		goto err;
316	}
317	info->apertures->ranges[0].base = screen_info.lfb_base;
318	info->apertures->ranges[0].size = size_total;
319
320	printk(KERN_INFO "vesafb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
321	       vesafb_defined.xres, vesafb_defined.yres, vesafb_defined.bits_per_pixel, vesafb_fix.line_length, screen_info.pages);
322
323	if (screen_info.vesapm_seg) {
324		printk(KERN_INFO "vesafb: protected mode interface info at %04x:%04x\n",
325		       screen_info.vesapm_seg,screen_info.vesapm_off);
326	}
327
328	if (screen_info.vesapm_seg < 0xc000)
329		ypan = pmi_setpal = 0; /* not available or some DOS TSR ... */
330
331	if (ypan || pmi_setpal) {
332		unsigned short *pmi_base;
333		pmi_base  = (unsigned short*)phys_to_virt(((unsigned long)screen_info.vesapm_seg << 4) + screen_info.vesapm_off);
334		pmi_start = (void*)((char*)pmi_base + pmi_base[1]);
335		pmi_pal   = (void*)((char*)pmi_base + pmi_base[2]);
336		printk(KERN_INFO "vesafb: pmi: set display start = %p, set palette = %p\n",pmi_start,pmi_pal);
337		if (pmi_base[3]) {
338			printk(KERN_INFO "vesafb: pmi: ports = ");
339				for (i = pmi_base[3]/2; pmi_base[i] != 0xffff; i++)
340					printk("%x ",pmi_base[i]);
341			printk("\n");
342			if (pmi_base[i] != 0xffff) {
343				/*
344				 * memory areas not supported (yet?)
345				 *
346				 * Rules are: we have to set up a descriptor for the requested
347				 * memory area and pass it in the ES register to the BIOS function.
348				 */
349				printk(KERN_INFO "vesafb: can't handle memory requests, pmi disabled\n");
350				ypan = pmi_setpal = 0;
351			}
352		}
353	}
354
355	if (vesafb_defined.bits_per_pixel == 8 && !pmi_setpal && !vga_compat) {
356		printk(KERN_WARNING "vesafb: hardware palette is unchangeable,\n"
357		                    "        colors may be incorrect\n");
358		vesafb_fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
359	}
360
361	vesafb_defined.xres_virtual = vesafb_defined.xres;
362	vesafb_defined.yres_virtual = vesafb_fix.smem_len / vesafb_fix.line_length;
363	if (ypan && vesafb_defined.yres_virtual > vesafb_defined.yres) {
364		printk(KERN_INFO "vesafb: scrolling: %s using protected mode interface, yres_virtual=%d\n",
365		       (ypan > 1) ? "ywrap" : "ypan",vesafb_defined.yres_virtual);
366	} else {
367		printk(KERN_INFO "vesafb: scrolling: redraw\n");
368		vesafb_defined.yres_virtual = vesafb_defined.yres;
369		ypan = 0;
370	}
371
372	/* some dummy values for timing to make fbset happy */
373	vesafb_defined.pixclock     = 10000000 / vesafb_defined.xres * 1000 / vesafb_defined.yres;
374	vesafb_defined.left_margin  = (vesafb_defined.xres / 8) & 0xf8;
375	vesafb_defined.hsync_len    = (vesafb_defined.xres / 8) & 0xf8;
376
377	vesafb_defined.red.offset    = screen_info.red_pos;
378	vesafb_defined.red.length    = screen_info.red_size;
379	vesafb_defined.green.offset  = screen_info.green_pos;
380	vesafb_defined.green.length  = screen_info.green_size;
381	vesafb_defined.blue.offset   = screen_info.blue_pos;
382	vesafb_defined.blue.length   = screen_info.blue_size;
383	vesafb_defined.transp.offset = screen_info.rsvd_pos;
384	vesafb_defined.transp.length = screen_info.rsvd_size;
385
386	if (vesafb_defined.bits_per_pixel <= 8) {
387		depth = vesafb_defined.green.length;
388		vesafb_defined.red.length =
389		vesafb_defined.green.length =
390		vesafb_defined.blue.length =
391		vesafb_defined.bits_per_pixel;
392	}
393
394	printk(KERN_INFO "vesafb: %s: "
395	       "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
396	       (vesafb_defined.bits_per_pixel > 8) ?
397	       "Truecolor" : (vga_compat || pmi_setpal) ?
398	       "Pseudocolor" : "Static Pseudocolor",
399	       screen_info.rsvd_size,
400	       screen_info.red_size,
401	       screen_info.green_size,
402	       screen_info.blue_size,
403	       screen_info.rsvd_pos,
404	       screen_info.red_pos,
405	       screen_info.green_pos,
406	       screen_info.blue_pos);
407
408	vesafb_fix.ypanstep  = ypan     ? 1 : 0;
409	vesafb_fix.ywrapstep = (ypan>1) ? 1 : 0;
410
411	/* request failure does not faze us, as vgacon probably has this
412	 * region already (FIXME) */
413	request_region(0x3c0, 32, "vesafb");
414
415	if (mtrr == 3) {
416		unsigned int temp_size = size_total;
417
418		/* Find the largest power-of-two */
419		temp_size = roundup_pow_of_two(temp_size);
420
421		/* Try and find a power of two to add */
422		do {
423			par->wc_cookie =
424				arch_phys_wc_add(vesafb_fix.smem_start,
425						 temp_size);
426			temp_size >>= 1;
427		} while (temp_size >= PAGE_SIZE && par->wc_cookie < 0);
428
429		info->screen_base = ioremap_wc(vesafb_fix.smem_start, vesafb_fix.smem_len);
430	} else {
431		if (mtrr && mtrr != 3)
432			WARN_ONCE(1, "Only MTRR_TYPE_WRCOMB (3) make sense\n");
433		info->screen_base = ioremap(vesafb_fix.smem_start, vesafb_fix.smem_len);
434	}
435
436	if (!info->screen_base) {
437		printk(KERN_ERR
438		       "vesafb: abort, cannot ioremap video memory 0x%x @ 0x%lx\n",
439			vesafb_fix.smem_len, vesafb_fix.smem_start);
440		err = -EIO;
441		goto err;
442	}
443
444	printk(KERN_INFO "vesafb: framebuffer at 0x%lx, mapped to 0x%p, "
445	       "using %dk, total %dk\n",
446	       vesafb_fix.smem_start, info->screen_base,
447	       size_remap/1024, size_total/1024);
448
449	info->fbops = &vesafb_ops;
450	info->var = vesafb_defined;
451	info->fix = vesafb_fix;
452	info->flags = FBINFO_FLAG_DEFAULT | FBINFO_MISC_FIRMWARE |
453		(ypan ? FBINFO_HWACCEL_YPAN : 0);
454
455	if (!ypan)
456		info->fbops->fb_pan_display = NULL;
457
458	if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
459		err = -ENOMEM;
460		goto err;
461	}
462	if (register_framebuffer(info)<0) {
463		err = -EINVAL;
464		fb_dealloc_cmap(&info->cmap);
465		goto err;
466	}
467	fb_info(info, "%s frame buffer device\n", info->fix.id);
468	return 0;
469err:
470	arch_phys_wc_del(par->wc_cookie);
471	if (info->screen_base)
472		iounmap(info->screen_base);
473	framebuffer_release(info);
474	release_mem_region(vesafb_fix.smem_start, size_total);
475	return err;
476}
477
478static int vesafb_remove(struct platform_device *pdev)
479{
480	struct fb_info *info = platform_get_drvdata(pdev);
481
482	unregister_framebuffer(info);
483	framebuffer_release(info);
484
485	return 0;
486}
487
488static struct platform_driver vesafb_driver = {
489	.driver = {
490		.name = "vesa-framebuffer",
491	},
492	.probe = vesafb_probe,
493	.remove = vesafb_remove,
494};
495
496module_platform_driver(vesafb_driver);
497MODULE_LICENSE("GPL");
498