2 * framebuffer driver for VBE 2.0 compliant graphic boards
4 * switching to graphics mode happens at boot time (while
5 * running in real mode, see arch/i386/boot/video.S).
7 * (c) 1998 Gerd Knorr <kraxel@goldbach.in-berlin.de>
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/string.h>
16 #include <linux/slab.h>
17 #include <linux/delay.h>
19 #include <linux/ioport.h>
20 #include <linux/init.h>
21 #include <linux/platform_device.h>
22 #include <linux/screen_info.h>
24 #include <video/vga.h>
28 #define dac_reg (0x3c8)
29 #define dac_val (0x3c9)
31 /* --------------------------------------------------------------------- */
33 static struct fb_var_screeninfo vesafb_defined __initdata
= {
34 .activate
= FB_ACTIVATE_NOW
,
41 .vmode
= FB_VMODE_NONINTERLACED
,
44 static struct fb_fix_screeninfo vesafb_fix __initdata
= {
46 .type
= FB_TYPE_PACKED_PIXELS
,
47 .accel
= FB_ACCEL_NONE
,
50 static int inverse
= 0;
51 static int mtrr
= 0; /* disable mtrr */
52 static int vram_remap __initdata
= 0; /* Set amount of memory to be used */
53 static int vram_total __initdata
= 0; /* Set total amount of memory */
54 static int pmi_setpal
= 1; /* pmi for palette changes ??? */
55 static int ypan
= 0; /* 0..nothing, 1..ypan, 2..ywrap */
56 static unsigned short *pmi_base
= NULL
;
57 static void (*pmi_start
)(void);
58 static void (*pmi_pal
)(void);
60 static int vga_compat
;
61 /* --------------------------------------------------------------------- */
63 static int vesafb_pan_display(struct fb_var_screeninfo
*var
,
69 offset
= (var
->yoffset
* info
->fix
.line_length
+ var
->xoffset
) / 4;
73 : /* no return value */
74 : "a" (0x4f07), /* EAX */
76 "c" (offset
), /* ECX */
77 "d" (offset
>> 16), /* EDX */
78 "D" (&pmi_start
)); /* EDI */
83 static int vesa_setpalette(int regno
, unsigned red
, unsigned green
,
86 int shift
= 16 - depth
;
90 * Try VGA registers first...
93 outb_p(regno
, dac_reg
);
94 outb_p(red
>> shift
, dac_val
);
95 outb_p(green
>> shift
, dac_val
);
96 outb_p(blue
>> shift
, dac_val
);
102 * Fallback to the PMI....
104 if (err
&& pmi_setpal
) {
105 struct { u_char blue
, green
, red
, pad
; } entry
;
107 entry
.red
= red
>> shift
;
108 entry
.green
= green
>> shift
;
109 entry
.blue
= blue
>> shift
;
111 __asm__
__volatile__(
113 : /* no return value */
114 : "a" (0x4f09), /* EAX */
117 "d" (regno
), /* EDX */
118 "D" (&entry
), /* EDI */
119 "S" (&pmi_pal
)); /* ESI */
127 static int vesafb_setcolreg(unsigned regno
, unsigned red
, unsigned green
,
128 unsigned blue
, unsigned transp
,
129 struct fb_info
*info
)
134 * Set a single color register. The values supplied are
135 * already rounded down to the hardware's capabilities
136 * (according to the entries in the `var' structure). Return
137 * != 0 for invalid regno.
140 if (regno
>= info
->cmap
.len
)
143 if (info
->var
.bits_per_pixel
== 8)
144 err
= vesa_setpalette(regno
,red
,green
,blue
);
145 else if (regno
< 16) {
146 switch (info
->var
.bits_per_pixel
) {
148 if (info
->var
.red
.offset
== 10) {
150 ((u32
*) (info
->pseudo_palette
))[regno
] =
151 ((red
& 0xf800) >> 1) |
152 ((green
& 0xf800) >> 6) |
153 ((blue
& 0xf800) >> 11);
156 ((u32
*) (info
->pseudo_palette
))[regno
] =
158 ((green
& 0xfc00) >> 5) |
159 ((blue
& 0xf800) >> 11);
167 ((u32
*)(info
->pseudo_palette
))[regno
] =
168 (red
<< info
->var
.red
.offset
) |
169 (green
<< info
->var
.green
.offset
) |
170 (blue
<< info
->var
.blue
.offset
);
178 static struct fb_ops vesafb_ops
= {
179 .owner
= THIS_MODULE
,
180 .fb_setcolreg
= vesafb_setcolreg
,
181 .fb_pan_display
= vesafb_pan_display
,
182 .fb_fillrect
= cfb_fillrect
,
183 .fb_copyarea
= cfb_copyarea
,
184 .fb_imageblit
= cfb_imageblit
,
187 static int __init
vesafb_setup(char *options
)
191 if (!options
|| !*options
)
194 while ((this_opt
= strsep(&options
, ",")) != NULL
) {
195 if (!*this_opt
) continue;
197 if (! strcmp(this_opt
, "inverse"))
199 else if (! strcmp(this_opt
, "redraw"))
201 else if (! strcmp(this_opt
, "ypan"))
203 else if (! strcmp(this_opt
, "ywrap"))
205 else if (! strcmp(this_opt
, "vgapal"))
207 else if (! strcmp(this_opt
, "pmipal"))
209 else if (! strncmp(this_opt
, "mtrr:", 5))
210 mtrr
= simple_strtoul(this_opt
+5, NULL
, 0);
211 else if (! strcmp(this_opt
, "nomtrr"))
213 else if (! strncmp(this_opt
, "vtotal:", 7))
214 vram_total
= simple_strtoul(this_opt
+7, NULL
, 0);
215 else if (! strncmp(this_opt
, "vremap:", 7))
216 vram_remap
= simple_strtoul(this_opt
+7, NULL
, 0);
221 static int __init
vesafb_probe(struct platform_device
*dev
)
223 struct fb_info
*info
;
225 unsigned int size_vmode
;
226 unsigned int size_remap
;
227 unsigned int size_total
;
229 if (screen_info
.orig_video_isVGA
!= VIDEO_TYPE_VLFB
)
232 vga_compat
= (screen_info
.capabilities
& 2) ? 0 : 1;
233 vesafb_fix
.smem_start
= screen_info
.lfb_base
;
234 vesafb_defined
.bits_per_pixel
= screen_info
.lfb_depth
;
235 if (15 == vesafb_defined
.bits_per_pixel
)
236 vesafb_defined
.bits_per_pixel
= 16;
237 vesafb_defined
.xres
= screen_info
.lfb_width
;
238 vesafb_defined
.yres
= screen_info
.lfb_height
;
239 vesafb_fix
.line_length
= screen_info
.lfb_linelength
;
240 vesafb_fix
.visual
= (vesafb_defined
.bits_per_pixel
== 8) ?
241 FB_VISUAL_PSEUDOCOLOR
: FB_VISUAL_TRUECOLOR
;
243 /* size_vmode -- that is the amount of memory needed for the
244 * used video mode, i.e. the minimum amount of
246 size_vmode
= vesafb_defined
.yres
* vesafb_fix
.line_length
;
248 /* size_total -- all video memory we have. Used for mtrr
249 * entries, ressource allocation and bounds
251 size_total
= screen_info
.lfb_size
* 65536;
253 size_total
= vram_total
* 1024 * 1024;
254 if (size_total
< size_vmode
)
255 size_total
= size_vmode
;
257 /* size_remap -- the amount of video memory we are going to
258 * use for vesafb. With modern cards it is no
259 * option to simply use size_total as that
260 * wastes plenty of kernel address space. */
261 size_remap
= size_vmode
* 2;
263 size_remap
= vram_remap
* 1024 * 1024;
264 if (size_remap
< size_vmode
)
265 size_remap
= size_vmode
;
266 if (size_remap
> size_total
)
267 size_remap
= size_total
;
268 vesafb_fix
.smem_len
= size_remap
;
271 screen_info
.vesapm_seg
= 0;
274 if (!request_mem_region(vesafb_fix
.smem_start
, size_total
, "vesafb")) {
276 "vesafb: cannot reserve video memory at 0x%lx\n",
277 vesafb_fix
.smem_start
);
278 /* We cannot make this fatal. Sometimes this comes from magic
279 spaces our resource handlers simply don't know about */
282 info
= framebuffer_alloc(sizeof(u32
) * 256, &dev
->dev
);
284 release_mem_region(vesafb_fix
.smem_start
, size_total
);
287 info
->pseudo_palette
= info
->par
;
290 info
->screen_base
= ioremap(vesafb_fix
.smem_start
, vesafb_fix
.smem_len
);
291 if (!info
->screen_base
) {
293 "vesafb: abort, cannot ioremap video memory 0x%x @ 0x%lx\n",
294 vesafb_fix
.smem_len
, vesafb_fix
.smem_start
);
299 printk(KERN_INFO
"vesafb: framebuffer at 0x%lx, mapped to 0x%p, "
300 "using %dk, total %dk\n",
301 vesafb_fix
.smem_start
, info
->screen_base
,
302 size_remap
/1024, size_total
/1024);
303 printk(KERN_INFO
"vesafb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
304 vesafb_defined
.xres
, vesafb_defined
.yres
, vesafb_defined
.bits_per_pixel
, vesafb_fix
.line_length
, screen_info
.pages
);
306 if (screen_info
.vesapm_seg
) {
307 printk(KERN_INFO
"vesafb: protected mode interface info at %04x:%04x\n",
308 screen_info
.vesapm_seg
,screen_info
.vesapm_off
);
311 if (screen_info
.vesapm_seg
< 0xc000)
312 ypan
= pmi_setpal
= 0; /* not available or some DOS TSR ... */
314 if (ypan
|| pmi_setpal
) {
315 pmi_base
= (unsigned short*)phys_to_virt(((unsigned long)screen_info
.vesapm_seg
<< 4) + screen_info
.vesapm_off
);
316 pmi_start
= (void*)((char*)pmi_base
+ pmi_base
[1]);
317 pmi_pal
= (void*)((char*)pmi_base
+ pmi_base
[2]);
318 printk(KERN_INFO
"vesafb: pmi: set display start = %p, set palette = %p\n",pmi_start
,pmi_pal
);
320 printk(KERN_INFO
"vesafb: pmi: ports = ");
321 for (i
= pmi_base
[3]/2; pmi_base
[i
] != 0xffff; i
++)
322 printk("%x ",pmi_base
[i
]);
324 if (pmi_base
[i
] != 0xffff) {
326 * memory areas not supported (yet?)
328 * Rules are: we have to set up a descriptor for the requested
329 * memory area and pass it in the ES register to the BIOS function.
331 printk(KERN_INFO
"vesafb: can't handle memory requests, pmi disabled\n");
332 ypan
= pmi_setpal
= 0;
337 if (vesafb_defined
.bits_per_pixel
== 8 && !pmi_setpal
&& !vga_compat
) {
338 printk(KERN_WARNING
"vesafb: hardware palette is unchangeable,\n"
339 " colors may be incorrect\n");
340 vesafb_fix
.visual
= FB_VISUAL_STATIC_PSEUDOCOLOR
;
343 vesafb_defined
.xres_virtual
= vesafb_defined
.xres
;
344 vesafb_defined
.yres_virtual
= vesafb_fix
.smem_len
/ vesafb_fix
.line_length
;
345 if (ypan
&& vesafb_defined
.yres_virtual
> vesafb_defined
.yres
) {
346 printk(KERN_INFO
"vesafb: scrolling: %s using protected mode interface, yres_virtual=%d\n",
347 (ypan
> 1) ? "ywrap" : "ypan",vesafb_defined
.yres_virtual
);
349 printk(KERN_INFO
"vesafb: scrolling: redraw\n");
350 vesafb_defined
.yres_virtual
= vesafb_defined
.yres
;
354 /* some dummy values for timing to make fbset happy */
355 vesafb_defined
.pixclock
= 10000000 / vesafb_defined
.xres
* 1000 / vesafb_defined
.yres
;
356 vesafb_defined
.left_margin
= (vesafb_defined
.xres
/ 8) & 0xf8;
357 vesafb_defined
.hsync_len
= (vesafb_defined
.xres
/ 8) & 0xf8;
359 vesafb_defined
.red
.offset
= screen_info
.red_pos
;
360 vesafb_defined
.red
.length
= screen_info
.red_size
;
361 vesafb_defined
.green
.offset
= screen_info
.green_pos
;
362 vesafb_defined
.green
.length
= screen_info
.green_size
;
363 vesafb_defined
.blue
.offset
= screen_info
.blue_pos
;
364 vesafb_defined
.blue
.length
= screen_info
.blue_size
;
365 vesafb_defined
.transp
.offset
= screen_info
.rsvd_pos
;
366 vesafb_defined
.transp
.length
= screen_info
.rsvd_size
;
368 if (vesafb_defined
.bits_per_pixel
<= 8) {
369 depth
= vesafb_defined
.green
.length
;
370 vesafb_defined
.red
.length
=
371 vesafb_defined
.green
.length
=
372 vesafb_defined
.blue
.length
=
373 vesafb_defined
.bits_per_pixel
;
376 printk(KERN_INFO
"vesafb: %s: "
377 "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
378 (vesafb_defined
.bits_per_pixel
> 8) ?
379 "Truecolor" : (vga_compat
|| pmi_setpal
) ?
380 "Pseudocolor" : "Static Pseudocolor",
381 screen_info
.rsvd_size
,
382 screen_info
.red_size
,
383 screen_info
.green_size
,
384 screen_info
.blue_size
,
385 screen_info
.rsvd_pos
,
387 screen_info
.green_pos
,
388 screen_info
.blue_pos
);
390 vesafb_fix
.ypanstep
= ypan
? 1 : 0;
391 vesafb_fix
.ywrapstep
= (ypan
>1) ? 1 : 0;
393 /* request failure does not faze us, as vgacon probably has this
394 * region already (FIXME) */
395 request_region(0x3c0, 32, "vesafb");
399 unsigned int temp_size
= size_total
;
400 unsigned int type
= 0;
404 type
= MTRR_TYPE_UNCACHABLE
;
407 type
= MTRR_TYPE_WRBACK
;
410 type
= MTRR_TYPE_WRCOMB
;
413 type
= MTRR_TYPE_WRTHROUGH
;
423 /* Find the largest power-of-two */
424 while (temp_size
& (temp_size
- 1))
425 temp_size
&= (temp_size
- 1);
427 /* Try and find a power of two to add */
429 rc
= mtrr_add(vesafb_fix
.smem_start
, temp_size
,
432 } while (temp_size
>= PAGE_SIZE
&& rc
== -EINVAL
);
437 info
->fbops
= &vesafb_ops
;
438 info
->var
= vesafb_defined
;
439 info
->fix
= vesafb_fix
;
440 info
->flags
= FBINFO_FLAG_DEFAULT
|
441 (ypan
) ? FBINFO_HWACCEL_YPAN
: 0;
444 info
->fbops
->fb_pan_display
= NULL
;
446 if (fb_alloc_cmap(&info
->cmap
, 256, 0) < 0) {
450 if (register_framebuffer(info
)<0) {
452 fb_dealloc_cmap(&info
->cmap
);
455 printk(KERN_INFO
"fb%d: %s frame buffer device\n",
456 info
->node
, info
->fix
.id
);
459 framebuffer_release(info
);
460 release_mem_region(vesafb_fix
.smem_start
, size_total
);
464 static struct platform_driver vesafb_driver
= {
465 .probe
= vesafb_probe
,
471 static struct platform_device
*vesafb_device
;
473 static int __init
vesafb_init(void)
478 /* ignore error return of fb_get_options */
479 fb_get_options("vesafb", &option
);
480 vesafb_setup(option
);
481 ret
= platform_driver_register(&vesafb_driver
);
484 vesafb_device
= platform_device_alloc("vesafb", 0);
487 ret
= platform_device_add(vesafb_device
);
492 platform_device_put(vesafb_device
);
493 platform_driver_unregister(&vesafb_driver
);
499 module_init(vesafb_init
);
501 MODULE_LICENSE("GPL");