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