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/tty.h>
17 #include <linux/slab.h>
18 #include <linux/delay.h>
20 #include <linux/ioport.h>
21 #include <linux/init.h>
22 #include <video/vga.h>
26 #define dac_reg (0x3c8)
27 #define dac_val (0x3c9)
29 /* --------------------------------------------------------------------- */
31 static struct fb_var_screeninfo vesafb_defined __initdata
= {
32 .activate
= FB_ACTIVATE_NOW
,
39 .vmode
= FB_VMODE_NONINTERLACED
,
42 static struct fb_fix_screeninfo vesafb_fix __initdata
= {
44 .type
= FB_TYPE_PACKED_PIXELS
,
45 .accel
= FB_ACCEL_NONE
,
48 static int inverse
= 0;
49 static int mtrr
= 3; /* default to write-combining */
50 static int vram_remap __initdata
= 0; /* Set amount of memory to be used */
51 static int vram_total __initdata
= 0; /* Set total amount of memory */
52 static int pmi_setpal
= 0; /* pmi for palette changes ??? */
53 static int ypan
= 0; /* 0..nothing, 1..ypan, 2..ywrap */
54 static unsigned short *pmi_base
= NULL
;
55 static void (*pmi_start
)(void);
56 static void (*pmi_pal
)(void);
58 static int vga_compat
;
60 /* --------------------------------------------------------------------- */
62 static int vesafb_pan_display(struct fb_var_screeninfo
*var
,
72 if (var
->yoffset
> var
->yres_virtual
)
74 if ((ypan
==1) && var
->yoffset
+var
->yres
> var
->yres_virtual
)
77 offset
= (var
->yoffset
* info
->fix
.line_length
+ var
->xoffset
) / 4;
81 : /* no return value */
82 : "a" (0x4f07), /* EAX */
84 "c" (offset
), /* ECX */
85 "d" (offset
>> 16), /* EDX */
86 "D" (&pmi_start
)); /* EDI */
91 static int vesafb_blank(int blank
, struct fb_info
*info
)
97 u8 seq
= 0, crtc17
= 0;
109 vga_wseq(NULL
, 0x00, 0x01);
110 seq
|= vga_rseq(NULL
, 0x01) & ~0x20;
111 vga_wseq(NULL
, 0x00, seq
);
113 crtc17
|= vga_rcrt(NULL
, 0x17) & ~0x80;
115 vga_wcrt(NULL
, 0x17, crtc17
);
116 vga_wseq(NULL
, 0x00, 0x03);
122 static void vesa_setpalette(int regno
, unsigned red
, unsigned green
,
126 struct { u_char blue
, green
, red
, pad
; } entry
;
127 int shift
= 16 - depth
;
130 entry
.red
= red
>> shift
;
131 entry
.green
= green
>> shift
;
132 entry
.blue
= blue
>> shift
;
134 __asm__
__volatile__(
136 : /* no return value */
137 : "a" (0x4f09), /* EAX */
140 "d" (regno
), /* EDX */
141 "D" (&entry
), /* EDI */
142 "S" (&pmi_pal
)); /* ESI */
144 /* without protected mode interface, try VGA registers... */
145 outb_p(regno
, dac_reg
);
146 outb_p(red
>> shift
, dac_val
);
147 outb_p(green
>> shift
, dac_val
);
148 outb_p(blue
>> shift
, dac_val
);
153 static int vesafb_setcolreg(unsigned regno
, unsigned red
, unsigned green
,
154 unsigned blue
, unsigned transp
,
155 struct fb_info
*info
)
158 * Set a single color register. The values supplied are
159 * already rounded down to the hardware's capabilities
160 * (according to the entries in the `var' structure). Return
161 * != 0 for invalid regno.
164 if (regno
>= info
->cmap
.len
)
167 switch (info
->var
.bits_per_pixel
) {
169 vesa_setpalette(regno
,red
,green
,blue
);
172 if (info
->var
.red
.offset
== 10) {
174 ((u32
*) (info
->pseudo_palette
))[regno
] =
175 ((red
& 0xf800) >> 1) |
176 ((green
& 0xf800) >> 6) |
177 ((blue
& 0xf800) >> 11);
180 ((u32
*) (info
->pseudo_palette
))[regno
] =
182 ((green
& 0xfc00) >> 5) |
183 ((blue
& 0xf800) >> 11);
190 ((u32
*)(info
->pseudo_palette
))[regno
] =
191 (red
<< info
->var
.red
.offset
) |
192 (green
<< info
->var
.green
.offset
) |
193 (blue
<< info
->var
.blue
.offset
);
199 ((u32
*)(info
->pseudo_palette
))[regno
] =
200 (red
<< info
->var
.red
.offset
) |
201 (green
<< info
->var
.green
.offset
) |
202 (blue
<< info
->var
.blue
.offset
);
208 static struct fb_ops vesafb_ops
= {
209 .owner
= THIS_MODULE
,
210 .fb_setcolreg
= vesafb_setcolreg
,
211 .fb_pan_display
= vesafb_pan_display
,
212 .fb_blank
= vesafb_blank
,
213 .fb_fillrect
= cfb_fillrect
,
214 .fb_copyarea
= cfb_copyarea
,
215 .fb_imageblit
= cfb_imageblit
,
216 .fb_cursor
= soft_cursor
,
219 static int __init
vesafb_setup(char *options
)
223 if (!options
|| !*options
)
226 while ((this_opt
= strsep(&options
, ",")) != NULL
) {
227 if (!*this_opt
) continue;
229 if (! strcmp(this_opt
, "inverse"))
231 else if (! strcmp(this_opt
, "redraw"))
233 else if (! strcmp(this_opt
, "ypan"))
235 else if (! strcmp(this_opt
, "ywrap"))
237 else if (! strcmp(this_opt
, "vgapal"))
239 else if (! strcmp(this_opt
, "pmipal"))
241 else if (! strncmp(this_opt
, "mtrr:", 5))
242 mtrr
= simple_strtoul(this_opt
+5, NULL
, 0);
243 else if (! strcmp(this_opt
, "nomtrr"))
245 else if (! strncmp(this_opt
, "vtotal:", 7))
246 vram_total
= simple_strtoul(this_opt
+7, NULL
, 0);
247 else if (! strncmp(this_opt
, "vremap:", 7))
248 vram_remap
= simple_strtoul(this_opt
+7, NULL
, 0);
253 static int __init
vesafb_probe(struct device
*device
)
255 struct platform_device
*dev
= to_platform_device(device
);
256 struct fb_info
*info
;
258 unsigned int size_vmode
;
259 unsigned int size_remap
;
260 unsigned int size_total
;
262 if (screen_info
.orig_video_isVGA
!= VIDEO_TYPE_VLFB
)
265 vesafb_fix
.smem_start
= screen_info
.lfb_base
;
266 vesafb_defined
.bits_per_pixel
= screen_info
.lfb_depth
;
267 if (15 == vesafb_defined
.bits_per_pixel
)
268 vesafb_defined
.bits_per_pixel
= 16;
269 vesafb_defined
.xres
= screen_info
.lfb_width
;
270 vesafb_defined
.yres
= screen_info
.lfb_height
;
271 vesafb_fix
.line_length
= screen_info
.lfb_linelength
;
272 vesafb_fix
.visual
= (vesafb_defined
.bits_per_pixel
== 8) ?
273 FB_VISUAL_PSEUDOCOLOR
: FB_VISUAL_TRUECOLOR
;
275 /* size_vmode -- that is the amount of memory needed for the
276 * used video mode, i.e. the minimum amount of
278 size_vmode
= vesafb_defined
.yres
* vesafb_fix
.line_length
;
280 /* size_total -- all video memory we have. Used for mtrr
281 * entries, ressource allocation and bounds
283 size_total
= screen_info
.lfb_size
* 65536;
285 size_total
= vram_total
* 1024 * 1024;
286 if (size_total
< size_vmode
)
287 size_total
= size_vmode
;
289 /* size_remap -- the amount of video memory we are going to
290 * use for vesafb. With modern cards it is no
291 * option to simply use size_total as that
292 * wastes plenty of kernel address space. */
293 size_remap
= size_vmode
* 2;
295 size_remap
= vram_remap
* 1024 * 1024;
296 if (size_remap
< size_vmode
)
297 size_remap
= size_vmode
;
298 if (size_remap
> size_total
)
299 size_remap
= size_total
;
300 vesafb_fix
.smem_len
= size_remap
;
303 screen_info
.vesapm_seg
= 0;
306 if (!request_mem_region(vesafb_fix
.smem_start
, size_total
, "vesafb")) {
308 "vesafb: cannot reserve video memory at 0x%lx\n",
309 vesafb_fix
.smem_start
);
310 /* We cannot make this fatal. Sometimes this comes from magic
311 spaces our resource handlers simply don't know about */
314 info
= framebuffer_alloc(sizeof(u32
) * 256, &dev
->dev
);
316 release_mem_region(vesafb_fix
.smem_start
, size_total
);
319 info
->pseudo_palette
= info
->par
;
322 info
->screen_base
= ioremap(vesafb_fix
.smem_start
, vesafb_fix
.smem_len
);
323 if (!info
->screen_base
) {
325 "vesafb: abort, cannot ioremap video memory 0x%x @ 0x%lx\n",
326 vesafb_fix
.smem_len
, vesafb_fix
.smem_start
);
331 printk(KERN_INFO
"vesafb: framebuffer at 0x%lx, mapped to 0x%p, "
332 "using %dk, total %dk\n",
333 vesafb_fix
.smem_start
, info
->screen_base
,
334 size_remap
/1024, size_total
/1024);
335 printk(KERN_INFO
"vesafb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
336 vesafb_defined
.xres
, vesafb_defined
.yres
, vesafb_defined
.bits_per_pixel
, vesafb_fix
.line_length
, screen_info
.pages
);
338 if (screen_info
.vesapm_seg
) {
339 printk(KERN_INFO
"vesafb: protected mode interface info at %04x:%04x\n",
340 screen_info
.vesapm_seg
,screen_info
.vesapm_off
);
343 if (screen_info
.vesapm_seg
< 0xc000)
344 ypan
= pmi_setpal
= 0; /* not available or some DOS TSR ... */
346 if (ypan
|| pmi_setpal
) {
347 pmi_base
= (unsigned short*)phys_to_virt(((unsigned long)screen_info
.vesapm_seg
<< 4) + screen_info
.vesapm_off
);
348 pmi_start
= (void*)((char*)pmi_base
+ pmi_base
[1]);
349 pmi_pal
= (void*)((char*)pmi_base
+ pmi_base
[2]);
350 printk(KERN_INFO
"vesafb: pmi: set display start = %p, set palette = %p\n",pmi_start
,pmi_pal
);
352 printk(KERN_INFO
"vesafb: pmi: ports = ");
353 for (i
= pmi_base
[3]/2; pmi_base
[i
] != 0xffff; i
++)
354 printk("%x ",pmi_base
[i
]);
356 if (pmi_base
[i
] != 0xffff) {
358 * memory areas not supported (yet?)
360 * Rules are: we have to set up a descriptor for the requested
361 * memory area and pass it in the ES register to the BIOS function.
363 printk(KERN_INFO
"vesafb: can't handle memory requests, pmi disabled\n");
364 ypan
= pmi_setpal
= 0;
369 vesafb_defined
.xres_virtual
= vesafb_defined
.xres
;
370 vesafb_defined
.yres_virtual
= vesafb_fix
.smem_len
/ vesafb_fix
.line_length
;
371 if (ypan
&& vesafb_defined
.yres_virtual
> vesafb_defined
.yres
) {
372 printk(KERN_INFO
"vesafb: scrolling: %s using protected mode interface, yres_virtual=%d\n",
373 (ypan
> 1) ? "ywrap" : "ypan",vesafb_defined
.yres_virtual
);
375 printk(KERN_INFO
"vesafb: scrolling: redraw\n");
376 vesafb_defined
.yres_virtual
= vesafb_defined
.yres
;
380 /* some dummy values for timing to make fbset happy */
381 vesafb_defined
.pixclock
= 10000000 / vesafb_defined
.xres
* 1000 / vesafb_defined
.yres
;
382 vesafb_defined
.left_margin
= (vesafb_defined
.xres
/ 8) & 0xf8;
383 vesafb_defined
.hsync_len
= (vesafb_defined
.xres
/ 8) & 0xf8;
385 vesafb_defined
.red
.offset
= screen_info
.red_pos
;
386 vesafb_defined
.red
.length
= screen_info
.red_size
;
387 vesafb_defined
.green
.offset
= screen_info
.green_pos
;
388 vesafb_defined
.green
.length
= screen_info
.green_size
;
389 vesafb_defined
.blue
.offset
= screen_info
.blue_pos
;
390 vesafb_defined
.blue
.length
= screen_info
.blue_size
;
391 vesafb_defined
.transp
.offset
= screen_info
.rsvd_pos
;
392 vesafb_defined
.transp
.length
= screen_info
.rsvd_size
;
394 if (vesafb_defined
.bits_per_pixel
<= 8) {
395 depth
= vesafb_defined
.green
.length
;
396 vesafb_defined
.red
.length
=
397 vesafb_defined
.green
.length
=
398 vesafb_defined
.blue
.length
=
399 vesafb_defined
.bits_per_pixel
;
402 printk(KERN_INFO
"vesafb: %s: "
403 "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
404 (vesafb_defined
.bits_per_pixel
> 8) ?
405 "Truecolor" : "Pseudocolor",
406 screen_info
.rsvd_size
,
407 screen_info
.red_size
,
408 screen_info
.green_size
,
409 screen_info
.blue_size
,
410 screen_info
.rsvd_pos
,
412 screen_info
.green_pos
,
413 screen_info
.blue_pos
);
415 vesafb_fix
.ypanstep
= ypan
? 1 : 0;
416 vesafb_fix
.ywrapstep
= (ypan
>1) ? 1 : 0;
418 /* request failure does not faze us, as vgacon probably has this
419 * region already (FIXME) */
420 request_region(0x3c0, 32, "vesafb");
423 unsigned int temp_size
= size_total
;
424 unsigned int type
= 0;
428 type
= MTRR_TYPE_UNCACHABLE
;
431 type
= MTRR_TYPE_WRBACK
;
434 type
= MTRR_TYPE_WRCOMB
;
437 type
= MTRR_TYPE_WRTHROUGH
;
447 /* Find the largest power-of-two */
448 while (temp_size
& (temp_size
- 1))
449 temp_size
&= (temp_size
- 1);
451 /* Try and find a power of two to add */
453 rc
= mtrr_add(vesafb_fix
.smem_start
, temp_size
,
456 } while (temp_size
>= PAGE_SIZE
&& rc
== -EINVAL
);
460 info
->fbops
= &vesafb_ops
;
461 info
->var
= vesafb_defined
;
462 info
->fix
= vesafb_fix
;
463 info
->flags
= FBINFO_FLAG_DEFAULT
|
464 (ypan
) ? FBINFO_HWACCEL_YPAN
: 0;
466 vga_compat
= (screen_info
.capabilities
& 2) ? 0 : 1;
467 printk("vesafb: Mode is %sVGA compatible\n",
468 (vga_compat
) ? "" : "not ");
470 if (fb_alloc_cmap(&info
->cmap
, 256, 0) < 0) {
474 if (register_framebuffer(info
)<0) {
476 fb_dealloc_cmap(&info
->cmap
);
479 printk(KERN_INFO
"fb%d: %s frame buffer device\n",
480 info
->node
, info
->fix
.id
);
483 framebuffer_release(info
);
484 release_mem_region(vesafb_fix
.smem_start
, size_total
);
488 static struct device_driver vesafb_driver
= {
490 .bus
= &platform_bus_type
,
491 .probe
= vesafb_probe
,
494 static struct platform_device vesafb_device
= {
498 static int __init
vesafb_init(void)
503 /* ignore error return of fb_get_options */
504 fb_get_options("vesafb", &option
);
505 vesafb_setup(option
);
506 ret
= driver_register(&vesafb_driver
);
509 ret
= platform_device_register(&vesafb_device
);
511 driver_unregister(&vesafb_driver
);
515 module_init(vesafb_init
);
517 MODULE_LICENSE("GPL");