1 // SPDX-License-Identifier: GPL-2.0-only
3 * framebuffer driver for VBE 2.0 compliant graphic boards
5 * switching to graphics mode happens at boot time (while
6 * running in real mode, see arch/i386/boot/video.S).
8 * (c) 1998 Gerd Knorr <kraxel@goldbach.in-berlin.de>
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>
18 #include <linux/delay.h>
20 #include <linux/ioport.h>
21 #include <linux/init.h>
22 #include <linux/platform_device.h>
23 #include <linux/screen_info.h>
26 #include <video/vga.h>
28 #define dac_reg (0x3c8)
29 #define dac_val (0x3c9)
31 /* --------------------------------------------------------------------- */
34 u32 pseudo_palette
[256];
38 struct resource
*region
;
41 static struct fb_var_screeninfo vesafb_defined
= {
42 .activate
= FB_ACTIVATE_NOW
,
49 .vmode
= FB_VMODE_NONINTERLACED
,
52 static struct fb_fix_screeninfo vesafb_fix
= {
54 .type
= FB_TYPE_PACKED_PIXELS
,
55 .accel
= FB_ACCEL_NONE
,
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 /* --------------------------------------------------------------------- */
70 static int vesafb_pan_display(struct fb_var_screeninfo
*var
,
76 offset
= (var
->yoffset
* info
->fix
.line_length
+ var
->xoffset
) / 4;
80 : /* no return value */
81 : "a" (0x4f07), /* EAX */
83 "c" (offset
), /* ECX */
84 "d" (offset
>> 16), /* EDX */
85 "D" (&pmi_start
)); /* EDI */
90 static int vesa_setpalette(int regno
, unsigned red
, unsigned green
,
93 int shift
= 16 - depth
;
97 * Try VGA registers first...
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
);
109 * Fallback to the PMI....
111 if (err
&& pmi_setpal
) {
112 struct { u_char blue
, green
, red
, pad
; } entry
;
114 entry
.red
= red
>> shift
;
115 entry
.green
= green
>> shift
;
116 entry
.blue
= blue
>> shift
;
118 __asm__
__volatile__(
120 : /* no return value */
121 : "a" (0x4f09), /* EAX */
124 "d" (regno
), /* EDX */
125 "D" (&entry
), /* EDI */
126 "S" (&pmi_pal
)); /* ESI */
134 static int vesafb_setcolreg(unsigned regno
, unsigned red
, unsigned green
,
135 unsigned blue
, unsigned transp
,
136 struct fb_info
*info
)
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.
147 if (regno
>= info
->cmap
.len
)
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
) {
155 if (info
->var
.red
.offset
== 10) {
157 ((u32
*) (info
->pseudo_palette
))[regno
] =
158 ((red
& 0xf800) >> 1) |
159 ((green
& 0xf800) >> 6) |
160 ((blue
& 0xf800) >> 11);
163 ((u32
*) (info
->pseudo_palette
))[regno
] =
165 ((green
& 0xfc00) >> 5) |
166 ((blue
& 0xf800) >> 11);
174 ((u32
*)(info
->pseudo_palette
))[regno
] =
175 (red
<< info
->var
.red
.offset
) |
176 (green
<< info
->var
.green
.offset
) |
177 (blue
<< info
->var
.blue
.offset
);
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.
189 static void vesafb_destroy(struct fb_info
*info
)
191 struct vesafb_par
*par
= info
->par
;
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
);
199 framebuffer_release(info
);
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
,
210 static int vesafb_setup(char *options
)
214 if (!options
|| !*options
)
217 while ((this_opt
= strsep(&options
, ",")) != NULL
) {
218 if (!*this_opt
) continue;
220 if (! strcmp(this_opt
, "inverse"))
222 else if (! strcmp(this_opt
, "redraw"))
224 else if (! strcmp(this_opt
, "ypan"))
226 else if (! strcmp(this_opt
, "ywrap"))
228 else if (! strcmp(this_opt
, "vgapal"))
230 else if (! strcmp(this_opt
, "pmipal"))
232 else if (! strncmp(this_opt
, "mtrr:", 5))
233 mtrr
= simple_strtoul(this_opt
+5, NULL
, 0);
234 else if (! strcmp(this_opt
, "nomtrr"))
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);
244 static int vesafb_probe(struct platform_device
*dev
)
246 struct screen_info
*si
;
247 struct fb_info
*info
;
248 struct vesafb_par
*par
;
250 unsigned int size_vmode
;
251 unsigned int size_remap
;
252 unsigned int size_total
;
256 * If we fail probing the device, the kernel might try a different
257 * driver. We get a copy of the attached screen_info, so that we can
258 * modify its values without affecting later drivers.
260 si
= dev_get_platdata(&dev
->dev
);
263 si
= devm_kmemdup(&dev
->dev
, si
, sizeof(*si
), GFP_KERNEL
);
267 /* ignore error return of fb_get_options */
268 fb_get_options("vesafb", &option
);
269 vesafb_setup(option
);
271 if (si
->orig_video_isVGA
!= VIDEO_TYPE_VLFB
)
274 vga_compat
= !__screen_info_vbe_mode_nonvga(si
);
275 vesafb_fix
.smem_start
= si
->lfb_base
;
276 vesafb_defined
.bits_per_pixel
= si
->lfb_depth
;
277 if (15 == vesafb_defined
.bits_per_pixel
)
278 vesafb_defined
.bits_per_pixel
= 16;
279 vesafb_defined
.xres
= si
->lfb_width
;
280 vesafb_defined
.yres
= si
->lfb_height
;
281 vesafb_fix
.line_length
= si
->lfb_linelength
;
282 vesafb_fix
.visual
= (vesafb_defined
.bits_per_pixel
== 8) ?
283 FB_VISUAL_PSEUDOCOLOR
: FB_VISUAL_TRUECOLOR
;
285 /* size_vmode -- that is the amount of memory needed for the
286 * used video mode, i.e. the minimum amount of
288 size_vmode
= vesafb_defined
.yres
* vesafb_fix
.line_length
;
290 /* size_total -- all video memory we have. Used for mtrr
291 * entries, resource allocation and bounds
293 size_total
= si
->lfb_size
* 65536;
295 size_total
= vram_total
* 1024 * 1024;
296 if (size_total
< size_vmode
)
297 size_total
= size_vmode
;
299 /* size_remap -- the amount of video memory we are going to
300 * use for vesafb. With modern cards it is no
301 * option to simply use size_total as that
302 * wastes plenty of kernel address space. */
303 size_remap
= size_vmode
* 2;
305 size_remap
= vram_remap
* 1024 * 1024;
306 if (size_remap
< size_vmode
)
307 size_remap
= size_vmode
;
308 if (size_remap
> size_total
)
309 size_remap
= size_total
;
310 vesafb_fix
.smem_len
= size_remap
;
316 if (!request_mem_region(vesafb_fix
.smem_start
, size_total
, "vesafb")) {
318 "vesafb: cannot reserve video memory at 0x%lx\n",
319 vesafb_fix
.smem_start
);
320 /* We cannot make this fatal. Sometimes this comes from magic
321 spaces our resource handlers simply don't know about */
324 info
= framebuffer_alloc(sizeof(struct vesafb_par
), &dev
->dev
);
326 release_mem_region(vesafb_fix
.smem_start
, size_total
);
329 platform_set_drvdata(dev
, info
);
331 info
->pseudo_palette
= par
->pseudo_palette
;
333 par
->base
= si
->lfb_base
;
334 par
->size
= size_total
;
336 printk(KERN_INFO
"vesafb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
337 vesafb_defined
.xres
, vesafb_defined
.yres
, vesafb_defined
.bits_per_pixel
,
338 vesafb_fix
.line_length
, si
->pages
);
340 if (si
->vesapm_seg
) {
341 printk(KERN_INFO
"vesafb: protected mode interface info at %04x:%04x\n",
342 si
->vesapm_seg
, si
->vesapm_off
);
345 if (si
->vesapm_seg
< 0xc000)
346 ypan
= pmi_setpal
= 0; /* not available or some DOS TSR ... */
348 if (ypan
|| pmi_setpal
) {
349 unsigned long pmi_phys
;
350 unsigned short *pmi_base
;
351 pmi_phys
= ((unsigned long)si
->vesapm_seg
<< 4) + si
->vesapm_off
;
352 pmi_base
= (unsigned short *)phys_to_virt(pmi_phys
);
353 pmi_start
= (void*)((char*)pmi_base
+ pmi_base
[1]);
354 pmi_pal
= (void*)((char*)pmi_base
+ pmi_base
[2]);
355 printk(KERN_INFO
"vesafb: pmi: set display start = %p, set palette = %p\n",pmi_start
,pmi_pal
);
357 printk(KERN_INFO
"vesafb: pmi: ports = ");
358 for (i
= pmi_base
[3]/2; pmi_base
[i
] != 0xffff; i
++)
359 printk("%x ", pmi_base
[i
]);
361 if (pmi_base
[i
] != 0xffff) {
363 * memory areas not supported (yet?)
365 * Rules are: we have to set up a descriptor for the requested
366 * memory area and pass it in the ES register to the BIOS function.
368 printk(KERN_INFO
"vesafb: can't handle memory requests, pmi disabled\n");
369 ypan
= pmi_setpal
= 0;
374 if (vesafb_defined
.bits_per_pixel
== 8 && !pmi_setpal
&& !vga_compat
) {
375 printk(KERN_WARNING
"vesafb: hardware palette is unchangeable,\n"
376 " colors may be incorrect\n");
377 vesafb_fix
.visual
= FB_VISUAL_STATIC_PSEUDOCOLOR
;
380 vesafb_defined
.xres_virtual
= vesafb_defined
.xres
;
381 vesafb_defined
.yres_virtual
= vesafb_fix
.smem_len
/ vesafb_fix
.line_length
;
382 if (ypan
&& vesafb_defined
.yres_virtual
> vesafb_defined
.yres
) {
383 printk(KERN_INFO
"vesafb: scrolling: %s using protected mode interface, yres_virtual=%d\n",
384 (ypan
> 1) ? "ywrap" : "ypan",vesafb_defined
.yres_virtual
);
386 printk(KERN_INFO
"vesafb: scrolling: redraw\n");
387 vesafb_defined
.yres_virtual
= vesafb_defined
.yres
;
391 /* some dummy values for timing to make fbset happy */
392 vesafb_defined
.pixclock
= 10000000 / vesafb_defined
.xres
* 1000 / vesafb_defined
.yres
;
393 vesafb_defined
.left_margin
= (vesafb_defined
.xres
/ 8) & 0xf8;
394 vesafb_defined
.hsync_len
= (vesafb_defined
.xres
/ 8) & 0xf8;
396 vesafb_defined
.red
.offset
= si
->red_pos
;
397 vesafb_defined
.red
.length
= si
->red_size
;
398 vesafb_defined
.green
.offset
= si
->green_pos
;
399 vesafb_defined
.green
.length
= si
->green_size
;
400 vesafb_defined
.blue
.offset
= si
->blue_pos
;
401 vesafb_defined
.blue
.length
= si
->blue_size
;
402 vesafb_defined
.transp
.offset
= si
->rsvd_pos
;
403 vesafb_defined
.transp
.length
= si
->rsvd_size
;
405 if (vesafb_defined
.bits_per_pixel
<= 8) {
406 depth
= vesafb_defined
.green
.length
;
407 vesafb_defined
.red
.length
=
408 vesafb_defined
.green
.length
=
409 vesafb_defined
.blue
.length
=
410 vesafb_defined
.bits_per_pixel
;
413 printk(KERN_INFO
"vesafb: %s: "
414 "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
415 (vesafb_defined
.bits_per_pixel
> 8) ?
416 "Truecolor" : (vga_compat
|| pmi_setpal
) ?
417 "Pseudocolor" : "Static Pseudocolor",
427 vesafb_fix
.ypanstep
= ypan
? 1 : 0;
428 vesafb_fix
.ywrapstep
= (ypan
>1) ? 1 : 0;
430 /* request failure does not faze us, as vgacon probably has this
431 * region already (FIXME) */
432 par
->region
= request_region(0x3c0, 32, "vesafb");
435 unsigned int temp_size
= size_total
;
437 /* Find the largest power-of-two */
438 temp_size
= roundup_pow_of_two(temp_size
);
440 /* Try and find a power of two to add */
443 arch_phys_wc_add(vesafb_fix
.smem_start
,
446 } while (temp_size
>= PAGE_SIZE
&& par
->wc_cookie
< 0);
448 info
->screen_base
= ioremap_wc(vesafb_fix
.smem_start
, vesafb_fix
.smem_len
);
450 if (mtrr
&& mtrr
!= 3)
451 WARN_ONCE(1, "Only MTRR_TYPE_WRCOMB (3) make sense\n");
452 info
->screen_base
= ioremap(vesafb_fix
.smem_start
, vesafb_fix
.smem_len
);
455 if (!info
->screen_base
) {
457 "vesafb: abort, cannot ioremap video memory 0x%x @ 0x%lx\n",
458 vesafb_fix
.smem_len
, vesafb_fix
.smem_start
);
460 goto err_release_region
;
463 printk(KERN_INFO
"vesafb: framebuffer at 0x%lx, mapped to 0x%p, "
464 "using %dk, total %dk\n",
465 vesafb_fix
.smem_start
, info
->screen_base
,
466 size_remap
/1024, size_total
/1024);
469 vesafb_ops
.fb_pan_display
= NULL
;
471 info
->fbops
= &vesafb_ops
;
472 info
->var
= vesafb_defined
;
473 info
->fix
= vesafb_fix
;
474 info
->flags
= (ypan
? FBINFO_HWACCEL_YPAN
: 0);
476 if (fb_alloc_cmap(&info
->cmap
, 256, 0) < 0) {
478 goto err_release_region
;
480 err
= devm_aperture_acquire_for_platform_device(dev
, par
->base
, par
->size
);
482 goto err_fb_dealloc_cmap
;
483 if (register_framebuffer(info
)<0) {
485 goto err_fb_dealloc_cmap
;
487 fb_info(info
, "%s frame buffer device\n", info
->fix
.id
);
490 fb_dealloc_cmap(&info
->cmap
);
492 arch_phys_wc_del(par
->wc_cookie
);
493 if (info
->screen_base
)
494 iounmap(info
->screen_base
);
496 release_region(0x3c0, 32);
497 framebuffer_release(info
);
498 release_mem_region(vesafb_fix
.smem_start
, size_total
);
502 static void vesafb_remove(struct platform_device
*pdev
)
504 struct fb_info
*info
= platform_get_drvdata(pdev
);
506 if (((struct vesafb_par
*)(info
->par
))->region
)
507 release_region(0x3c0, 32);
509 /* vesafb_destroy takes care of info cleanup */
510 unregister_framebuffer(info
);
513 static struct platform_driver vesafb_driver
= {
515 .name
= "vesa-framebuffer",
517 .probe
= vesafb_probe
,
518 .remove
= vesafb_remove
,
521 module_platform_driver(vesafb_driver
);
522 MODULE_LICENSE("GPL");