xref: /openbmc/linux/drivers/video/fbdev/vesafb.c (revision 340bbe90)
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 
vesafb_pan_display(struct fb_var_screeninfo * var,struct fb_info * info)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 
vesa_setpalette(int regno,unsigned red,unsigned green,unsigned blue)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 
vesafb_setcolreg(unsigned regno,unsigned red,unsigned green,unsigned blue,unsigned transp,struct fb_info * info)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  */
vesafb_destroy(struct fb_info * info)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_DEFAULT_IOMEM_OPS,
205 	.fb_destroy     = vesafb_destroy,
206 	.fb_setcolreg	= vesafb_setcolreg,
207 	.fb_pan_display	= vesafb_pan_display,
208 };
209 
vesafb_setup(char * options)210 static int vesafb_setup(char *options)
211 {
212 	char *this_opt;
213 
214 	if (!options || !*options)
215 		return 0;
216 
217 	while ((this_opt = strsep(&options, ",")) != NULL) {
218 		if (!*this_opt) continue;
219 
220 		if (! strcmp(this_opt, "inverse"))
221 			inverse=1;
222 		else if (! strcmp(this_opt, "redraw"))
223 			ypan=0;
224 		else if (! strcmp(this_opt, "ypan"))
225 			ypan=1;
226 		else if (! strcmp(this_opt, "ywrap"))
227 			ypan=2;
228 		else if (! strcmp(this_opt, "vgapal"))
229 			pmi_setpal=0;
230 		else if (! strcmp(this_opt, "pmipal"))
231 			pmi_setpal=1;
232 		else if (! strncmp(this_opt, "mtrr:", 5))
233 			mtrr = simple_strtoul(this_opt+5, NULL, 0);
234 		else if (! strcmp(this_opt, "nomtrr"))
235 			mtrr=0;
236 		else if (! strncmp(this_opt, "vtotal:", 7))
237 			vram_total = simple_strtoul(this_opt+7, NULL, 0);
238 		else if (! strncmp(this_opt, "vremap:", 7))
239 			vram_remap = simple_strtoul(this_opt+7, NULL, 0);
240 	}
241 	return 0;
242 }
243 
vesafb_probe(struct platform_device * dev)244 static int vesafb_probe(struct platform_device *dev)
245 {
246 	struct screen_info *si = &screen_info;
247 	struct fb_info *info;
248 	struct vesafb_par *par;
249 	int i, err;
250 	unsigned int size_vmode;
251 	unsigned int size_remap;
252 	unsigned int size_total;
253 	char *option = NULL;
254 
255 	/* ignore error return of fb_get_options */
256 	fb_get_options("vesafb", &option);
257 	vesafb_setup(option);
258 
259 	if (si->orig_video_isVGA != VIDEO_TYPE_VLFB)
260 		return -ENODEV;
261 
262 	vga_compat = !__screen_info_vbe_mode_nonvga(si);
263 	vesafb_fix.smem_start = si->lfb_base;
264 	vesafb_defined.bits_per_pixel = si->lfb_depth;
265 	if (15 == vesafb_defined.bits_per_pixel)
266 		vesafb_defined.bits_per_pixel = 16;
267 	vesafb_defined.xres = si->lfb_width;
268 	vesafb_defined.yres = si->lfb_height;
269 	vesafb_fix.line_length = si->lfb_linelength;
270 	vesafb_fix.visual   = (vesafb_defined.bits_per_pixel == 8) ?
271 		FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
272 
273 	/*   size_vmode -- that is the amount of memory needed for the
274 	 *                 used video mode, i.e. the minimum amount of
275 	 *                 memory we need. */
276 	size_vmode = vesafb_defined.yres * vesafb_fix.line_length;
277 
278 	/*   size_total -- all video memory we have. Used for mtrr
279 	 *                 entries, resource allocation and bounds
280 	 *                 checking. */
281 	size_total = si->lfb_size * 65536;
282 	if (vram_total)
283 		size_total = vram_total * 1024 * 1024;
284 	if (size_total < size_vmode)
285 		size_total = size_vmode;
286 
287 	/*   size_remap -- the amount of video memory we are going to
288 	 *                 use for vesafb.  With modern cards it is no
289 	 *                 option to simply use size_total as that
290 	 *                 wastes plenty of kernel address space. */
291 	size_remap  = size_vmode * 2;
292 	if (vram_remap)
293 		size_remap = vram_remap * 1024 * 1024;
294 	if (size_remap < size_vmode)
295 		size_remap = size_vmode;
296 	if (size_remap > size_total)
297 		size_remap = size_total;
298 	vesafb_fix.smem_len = size_remap;
299 
300 #ifndef __i386__
301 	si->vesapm_seg = 0;
302 #endif
303 
304 	if (!request_mem_region(vesafb_fix.smem_start, size_total, "vesafb")) {
305 		printk(KERN_WARNING
306 		       "vesafb: cannot reserve video memory at 0x%lx\n",
307 			vesafb_fix.smem_start);
308 		/* We cannot make this fatal. Sometimes this comes from magic
309 		   spaces our resource handlers simply don't know about */
310 	}
311 
312 	info = framebuffer_alloc(sizeof(struct vesafb_par), &dev->dev);
313 	if (!info) {
314 		release_mem_region(vesafb_fix.smem_start, size_total);
315 		return -ENOMEM;
316 	}
317 	platform_set_drvdata(dev, info);
318 	par = info->par;
319 	info->pseudo_palette = par->pseudo_palette;
320 
321 	par->base = si->lfb_base;
322 	par->size = size_total;
323 
324 	printk(KERN_INFO "vesafb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
325 	       vesafb_defined.xres, vesafb_defined.yres, vesafb_defined.bits_per_pixel,
326 	       vesafb_fix.line_length, si->pages);
327 
328 	if (si->vesapm_seg) {
329 		printk(KERN_INFO "vesafb: protected mode interface info at %04x:%04x\n",
330 		       si->vesapm_seg, si->vesapm_off);
331 	}
332 
333 	if (si->vesapm_seg < 0xc000)
334 		ypan = pmi_setpal = 0; /* not available or some DOS TSR ... */
335 
336 	if (ypan || pmi_setpal) {
337 		unsigned long pmi_phys;
338 		unsigned short *pmi_base;
339 		pmi_phys  = ((unsigned long)si->vesapm_seg << 4) + si->vesapm_off;
340 		pmi_base  = (unsigned short *)phys_to_virt(pmi_phys);
341 		pmi_start = (void*)((char*)pmi_base + pmi_base[1]);
342 		pmi_pal   = (void*)((char*)pmi_base + pmi_base[2]);
343 		printk(KERN_INFO "vesafb: pmi: set display start = %p, set palette = %p\n",pmi_start,pmi_pal);
344 		if (pmi_base[3]) {
345 			printk(KERN_INFO "vesafb: pmi: ports = ");
346 			for (i = pmi_base[3]/2; pmi_base[i] != 0xffff; i++)
347 				printk("%x ", pmi_base[i]);
348 			printk("\n");
349 			if (pmi_base[i] != 0xffff) {
350 				/*
351 				 * memory areas not supported (yet?)
352 				 *
353 				 * Rules are: we have to set up a descriptor for the requested
354 				 * memory area and pass it in the ES register to the BIOS function.
355 				 */
356 				printk(KERN_INFO "vesafb: can't handle memory requests, pmi disabled\n");
357 				ypan = pmi_setpal = 0;
358 			}
359 		}
360 	}
361 
362 	if (vesafb_defined.bits_per_pixel == 8 && !pmi_setpal && !vga_compat) {
363 		printk(KERN_WARNING "vesafb: hardware palette is unchangeable,\n"
364 		                    "        colors may be incorrect\n");
365 		vesafb_fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
366 	}
367 
368 	vesafb_defined.xres_virtual = vesafb_defined.xres;
369 	vesafb_defined.yres_virtual = vesafb_fix.smem_len / vesafb_fix.line_length;
370 	if (ypan && vesafb_defined.yres_virtual > vesafb_defined.yres) {
371 		printk(KERN_INFO "vesafb: scrolling: %s using protected mode interface, yres_virtual=%d\n",
372 		       (ypan > 1) ? "ywrap" : "ypan",vesafb_defined.yres_virtual);
373 	} else {
374 		printk(KERN_INFO "vesafb: scrolling: redraw\n");
375 		vesafb_defined.yres_virtual = vesafb_defined.yres;
376 		ypan = 0;
377 	}
378 
379 	/* some dummy values for timing to make fbset happy */
380 	vesafb_defined.pixclock     = 10000000 / vesafb_defined.xres * 1000 / vesafb_defined.yres;
381 	vesafb_defined.left_margin  = (vesafb_defined.xres / 8) & 0xf8;
382 	vesafb_defined.hsync_len    = (vesafb_defined.xres / 8) & 0xf8;
383 
384 	vesafb_defined.red.offset    = si->red_pos;
385 	vesafb_defined.red.length    = si->red_size;
386 	vesafb_defined.green.offset  = si->green_pos;
387 	vesafb_defined.green.length  = si->green_size;
388 	vesafb_defined.blue.offset   = si->blue_pos;
389 	vesafb_defined.blue.length   = si->blue_size;
390 	vesafb_defined.transp.offset = si->rsvd_pos;
391 	vesafb_defined.transp.length = si->rsvd_size;
392 
393 	if (vesafb_defined.bits_per_pixel <= 8) {
394 		depth = vesafb_defined.green.length;
395 		vesafb_defined.red.length =
396 		vesafb_defined.green.length =
397 		vesafb_defined.blue.length =
398 		vesafb_defined.bits_per_pixel;
399 	}
400 
401 	printk(KERN_INFO "vesafb: %s: "
402 	       "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
403 	       (vesafb_defined.bits_per_pixel > 8) ?
404 	       "Truecolor" : (vga_compat || pmi_setpal) ?
405 	       "Pseudocolor" : "Static Pseudocolor",
406 	       si->rsvd_size,
407 	       si->red_size,
408 	       si->green_size,
409 	       si->blue_size,
410 	       si->rsvd_pos,
411 	       si->red_pos,
412 	       si->green_pos,
413 	       si->blue_pos);
414 
415 	vesafb_fix.ypanstep  = ypan     ? 1 : 0;
416 	vesafb_fix.ywrapstep = (ypan>1) ? 1 : 0;
417 
418 	/* request failure does not faze us, as vgacon probably has this
419 	 * region already (FIXME) */
420 	par->region = request_region(0x3c0, 32, "vesafb");
421 
422 	if (mtrr == 3) {
423 		unsigned int temp_size = size_total;
424 
425 		/* Find the largest power-of-two */
426 		temp_size = roundup_pow_of_two(temp_size);
427 
428 		/* Try and find a power of two to add */
429 		do {
430 			par->wc_cookie =
431 				arch_phys_wc_add(vesafb_fix.smem_start,
432 						 temp_size);
433 			temp_size >>= 1;
434 		} while (temp_size >= PAGE_SIZE && par->wc_cookie < 0);
435 
436 		info->screen_base = ioremap_wc(vesafb_fix.smem_start, vesafb_fix.smem_len);
437 	} else {
438 		if (mtrr && mtrr != 3)
439 			WARN_ONCE(1, "Only MTRR_TYPE_WRCOMB (3) make sense\n");
440 		info->screen_base = ioremap(vesafb_fix.smem_start, vesafb_fix.smem_len);
441 	}
442 
443 	if (!info->screen_base) {
444 		printk(KERN_ERR
445 		       "vesafb: abort, cannot ioremap video memory 0x%x @ 0x%lx\n",
446 			vesafb_fix.smem_len, vesafb_fix.smem_start);
447 		err = -EIO;
448 		goto err_release_region;
449 	}
450 
451 	printk(KERN_INFO "vesafb: framebuffer at 0x%lx, mapped to 0x%p, "
452 	       "using %dk, total %dk\n",
453 	       vesafb_fix.smem_start, info->screen_base,
454 	       size_remap/1024, size_total/1024);
455 
456 	if (!ypan)
457 		vesafb_ops.fb_pan_display = NULL;
458 
459 	info->fbops = &vesafb_ops;
460 	info->var = vesafb_defined;
461 	info->fix = vesafb_fix;
462 	info->flags = (ypan ? FBINFO_HWACCEL_YPAN : 0);
463 
464 	if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
465 		err = -ENOMEM;
466 		goto err_release_region;
467 	}
468 	err = devm_aperture_acquire_for_platform_device(dev, par->base, par->size);
469 	if (err)
470 		goto err_fb_dealloc_cmap;
471 	if (register_framebuffer(info)<0) {
472 		err = -EINVAL;
473 		goto err_fb_dealloc_cmap;
474 	}
475 	fb_info(info, "%s frame buffer device\n", info->fix.id);
476 	return 0;
477 err_fb_dealloc_cmap:
478 	fb_dealloc_cmap(&info->cmap);
479 err_release_region:
480 	arch_phys_wc_del(par->wc_cookie);
481 	if (info->screen_base)
482 		iounmap(info->screen_base);
483 	if (par->region)
484 		release_region(0x3c0, 32);
485 	framebuffer_release(info);
486 	release_mem_region(vesafb_fix.smem_start, size_total);
487 	return err;
488 }
489 
vesafb_remove(struct platform_device * pdev)490 static void vesafb_remove(struct platform_device *pdev)
491 {
492 	struct fb_info *info = platform_get_drvdata(pdev);
493 
494 	if (((struct vesafb_par *)(info->par))->region)
495 		release_region(0x3c0, 32);
496 
497 	/* vesafb_destroy takes care of info cleanup */
498 	unregister_framebuffer(info);
499 }
500 
501 static struct platform_driver vesafb_driver = {
502 	.driver = {
503 		.name = "vesa-framebuffer",
504 	},
505 	.probe = vesafb_probe,
506 	.remove_new = vesafb_remove,
507 };
508 
509 module_platform_driver(vesafb_driver);
510 MODULE_LICENSE("GPL");
511