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