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/delay.h>
18 #include <linux/ioport.h>
19 #include <linux/init.h>
20 #include <linux/platform_device.h>
21 #include <linux/screen_info.h>
23 #include <video/vga.h>
27 #define dac_reg (0x3c8)
28 #define dac_val (0x3c9)
30 /* --------------------------------------------------------------------- */
32 static struct fb_var_screeninfo vesafb_defined
= {
33 .activate
= FB_ACTIVATE_NOW
,
40 .vmode
= FB_VMODE_NONINTERLACED
,
43 static struct fb_fix_screeninfo vesafb_fix
= {
45 .type
= FB_TYPE_PACKED_PIXELS
,
46 .accel
= FB_ACCEL_NONE
,
49 static int inverse __read_mostly
;
50 static int mtrr __read_mostly
; /* disable mtrr */
51 static int vram_remap
; /* Set amount of memory to be used */
52 static int vram_total
; /* Set total amount of memory */
53 static int pmi_setpal __read_mostly
= 1; /* pmi for palette changes ??? */
54 static int ypan __read_mostly
; /* 0..nothing, 1..ypan, 2..ywrap */
55 static void (*pmi_start
)(void) __read_mostly
;
56 static void (*pmi_pal
) (void) __read_mostly
;
57 static int depth __read_mostly
;
58 static int vga_compat __read_mostly
;
59 /* --------------------------------------------------------------------- */
61 static int vesafb_pan_display(struct fb_var_screeninfo
*var
,
67 offset
= (var
->yoffset
* info
->fix
.line_length
+ var
->xoffset
) / 4;
71 : /* no return value */
72 : "a" (0x4f07), /* EAX */
74 "c" (offset
), /* ECX */
75 "d" (offset
>> 16), /* EDX */
76 "D" (&pmi_start
)); /* EDI */
81 static int vesa_setpalette(int regno
, unsigned red
, unsigned green
,
84 int shift
= 16 - depth
;
88 * Try VGA registers first...
91 outb_p(regno
, dac_reg
);
92 outb_p(red
>> shift
, dac_val
);
93 outb_p(green
>> shift
, dac_val
);
94 outb_p(blue
>> shift
, dac_val
);
100 * Fallback to the PMI....
102 if (err
&& pmi_setpal
) {
103 struct { u_char blue
, green
, red
, pad
; } entry
;
105 entry
.red
= red
>> shift
;
106 entry
.green
= green
>> shift
;
107 entry
.blue
= blue
>> shift
;
109 __asm__
__volatile__(
111 : /* no return value */
112 : "a" (0x4f09), /* EAX */
115 "d" (regno
), /* EDX */
116 "D" (&entry
), /* EDI */
117 "S" (&pmi_pal
)); /* ESI */
125 static int vesafb_setcolreg(unsigned regno
, unsigned red
, unsigned green
,
126 unsigned blue
, unsigned transp
,
127 struct fb_info
*info
)
132 * Set a single color register. The values supplied are
133 * already rounded down to the hardware's capabilities
134 * (according to the entries in the `var' structure). Return
135 * != 0 for invalid regno.
138 if (regno
>= info
->cmap
.len
)
141 if (info
->var
.bits_per_pixel
== 8)
142 err
= vesa_setpalette(regno
,red
,green
,blue
);
143 else if (regno
< 16) {
144 switch (info
->var
.bits_per_pixel
) {
146 if (info
->var
.red
.offset
== 10) {
148 ((u32
*) (info
->pseudo_palette
))[regno
] =
149 ((red
& 0xf800) >> 1) |
150 ((green
& 0xf800) >> 6) |
151 ((blue
& 0xf800) >> 11);
154 ((u32
*) (info
->pseudo_palette
))[regno
] =
156 ((green
& 0xfc00) >> 5) |
157 ((blue
& 0xf800) >> 11);
165 ((u32
*)(info
->pseudo_palette
))[regno
] =
166 (red
<< info
->var
.red
.offset
) |
167 (green
<< info
->var
.green
.offset
) |
168 (blue
<< info
->var
.blue
.offset
);
176 static void vesafb_destroy(struct fb_info
*info
)
178 fb_dealloc_cmap(&info
->cmap
);
179 if (info
->screen_base
)
180 iounmap(info
->screen_base
);
181 release_mem_region(info
->apertures
->ranges
[0].base
, info
->apertures
->ranges
[0].size
);
182 framebuffer_release(info
);
185 static struct fb_ops vesafb_ops
= {
186 .owner
= THIS_MODULE
,
187 .fb_destroy
= vesafb_destroy
,
188 .fb_setcolreg
= vesafb_setcolreg
,
189 .fb_pan_display
= vesafb_pan_display
,
190 .fb_fillrect
= cfb_fillrect
,
191 .fb_copyarea
= cfb_copyarea
,
192 .fb_imageblit
= cfb_imageblit
,
195 static int vesafb_setup(char *options
)
199 if (!options
|| !*options
)
202 while ((this_opt
= strsep(&options
, ",")) != NULL
) {
203 if (!*this_opt
) continue;
205 if (! strcmp(this_opt
, "inverse"))
207 else if (! strcmp(this_opt
, "redraw"))
209 else if (! strcmp(this_opt
, "ypan"))
211 else if (! strcmp(this_opt
, "ywrap"))
213 else if (! strcmp(this_opt
, "vgapal"))
215 else if (! strcmp(this_opt
, "pmipal"))
217 else if (! strncmp(this_opt
, "mtrr:", 5))
218 mtrr
= simple_strtoul(this_opt
+5, NULL
, 0);
219 else if (! strcmp(this_opt
, "nomtrr"))
221 else if (! strncmp(this_opt
, "vtotal:", 7))
222 vram_total
= simple_strtoul(this_opt
+7, NULL
, 0);
223 else if (! strncmp(this_opt
, "vremap:", 7))
224 vram_remap
= simple_strtoul(this_opt
+7, NULL
, 0);
229 static int vesafb_probe(struct platform_device
*dev
)
231 struct fb_info
*info
;
233 unsigned int size_vmode
;
234 unsigned int size_remap
;
235 unsigned int size_total
;
238 /* ignore error return of fb_get_options */
239 fb_get_options("vesafb", &option
);
240 vesafb_setup(option
);
242 if (screen_info
.orig_video_isVGA
!= VIDEO_TYPE_VLFB
)
245 vga_compat
= (screen_info
.capabilities
& 2) ? 0 : 1;
246 vesafb_fix
.smem_start
= screen_info
.lfb_base
;
247 vesafb_defined
.bits_per_pixel
= screen_info
.lfb_depth
;
248 if (15 == vesafb_defined
.bits_per_pixel
)
249 vesafb_defined
.bits_per_pixel
= 16;
250 vesafb_defined
.xres
= screen_info
.lfb_width
;
251 vesafb_defined
.yres
= screen_info
.lfb_height
;
252 vesafb_fix
.line_length
= screen_info
.lfb_linelength
;
253 vesafb_fix
.visual
= (vesafb_defined
.bits_per_pixel
== 8) ?
254 FB_VISUAL_PSEUDOCOLOR
: FB_VISUAL_TRUECOLOR
;
256 /* size_vmode -- that is the amount of memory needed for the
257 * used video mode, i.e. the minimum amount of
259 size_vmode
= vesafb_defined
.yres
* vesafb_fix
.line_length
;
261 /* size_total -- all video memory we have. Used for mtrr
262 * entries, resource allocation and bounds
264 size_total
= screen_info
.lfb_size
* 65536;
266 size_total
= vram_total
* 1024 * 1024;
267 if (size_total
< size_vmode
)
268 size_total
= size_vmode
;
270 /* size_remap -- the amount of video memory we are going to
271 * use for vesafb. With modern cards it is no
272 * option to simply use size_total as that
273 * wastes plenty of kernel address space. */
274 size_remap
= size_vmode
* 2;
276 size_remap
= vram_remap
* 1024 * 1024;
277 if (size_remap
< size_vmode
)
278 size_remap
= size_vmode
;
279 if (size_remap
> size_total
)
280 size_remap
= size_total
;
281 vesafb_fix
.smem_len
= size_remap
;
284 screen_info
.vesapm_seg
= 0;
287 if (!request_mem_region(vesafb_fix
.smem_start
, size_total
, "vesafb")) {
289 "vesafb: cannot reserve video memory at 0x%lx\n",
290 vesafb_fix
.smem_start
);
291 /* We cannot make this fatal. Sometimes this comes from magic
292 spaces our resource handlers simply don't know about */
295 info
= framebuffer_alloc(sizeof(u32
) * 256, &dev
->dev
);
297 release_mem_region(vesafb_fix
.smem_start
, size_total
);
300 info
->pseudo_palette
= info
->par
;
303 /* set vesafb aperture size for generic probing */
304 info
->apertures
= alloc_apertures(1);
305 if (!info
->apertures
) {
309 info
->apertures
->ranges
[0].base
= screen_info
.lfb_base
;
310 info
->apertures
->ranges
[0].size
= size_total
;
312 printk(KERN_INFO
"vesafb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
313 vesafb_defined
.xres
, vesafb_defined
.yres
, vesafb_defined
.bits_per_pixel
, vesafb_fix
.line_length
, screen_info
.pages
);
315 if (screen_info
.vesapm_seg
) {
316 printk(KERN_INFO
"vesafb: protected mode interface info at %04x:%04x\n",
317 screen_info
.vesapm_seg
,screen_info
.vesapm_off
);
320 if (screen_info
.vesapm_seg
< 0xc000)
321 ypan
= pmi_setpal
= 0; /* not available or some DOS TSR ... */
323 if (ypan
|| pmi_setpal
) {
324 unsigned short *pmi_base
;
325 pmi_base
= (unsigned short*)phys_to_virt(((unsigned long)screen_info
.vesapm_seg
<< 4) + screen_info
.vesapm_off
);
326 pmi_start
= (void*)((char*)pmi_base
+ pmi_base
[1]);
327 pmi_pal
= (void*)((char*)pmi_base
+ pmi_base
[2]);
328 printk(KERN_INFO
"vesafb: pmi: set display start = %p, set palette = %p\n",pmi_start
,pmi_pal
);
330 printk(KERN_INFO
"vesafb: pmi: ports = ");
331 for (i
= pmi_base
[3]/2; pmi_base
[i
] != 0xffff; i
++)
332 printk("%x ",pmi_base
[i
]);
334 if (pmi_base
[i
] != 0xffff) {
336 * memory areas not supported (yet?)
338 * Rules are: we have to set up a descriptor for the requested
339 * memory area and pass it in the ES register to the BIOS function.
341 printk(KERN_INFO
"vesafb: can't handle memory requests, pmi disabled\n");
342 ypan
= pmi_setpal
= 0;
347 if (vesafb_defined
.bits_per_pixel
== 8 && !pmi_setpal
&& !vga_compat
) {
348 printk(KERN_WARNING
"vesafb: hardware palette is unchangeable,\n"
349 " colors may be incorrect\n");
350 vesafb_fix
.visual
= FB_VISUAL_STATIC_PSEUDOCOLOR
;
353 vesafb_defined
.xres_virtual
= vesafb_defined
.xres
;
354 vesafb_defined
.yres_virtual
= vesafb_fix
.smem_len
/ vesafb_fix
.line_length
;
355 if (ypan
&& vesafb_defined
.yres_virtual
> vesafb_defined
.yres
) {
356 printk(KERN_INFO
"vesafb: scrolling: %s using protected mode interface, yres_virtual=%d\n",
357 (ypan
> 1) ? "ywrap" : "ypan",vesafb_defined
.yres_virtual
);
359 printk(KERN_INFO
"vesafb: scrolling: redraw\n");
360 vesafb_defined
.yres_virtual
= vesafb_defined
.yres
;
364 /* some dummy values for timing to make fbset happy */
365 vesafb_defined
.pixclock
= 10000000 / vesafb_defined
.xres
* 1000 / vesafb_defined
.yres
;
366 vesafb_defined
.left_margin
= (vesafb_defined
.xres
/ 8) & 0xf8;
367 vesafb_defined
.hsync_len
= (vesafb_defined
.xres
/ 8) & 0xf8;
369 vesafb_defined
.red
.offset
= screen_info
.red_pos
;
370 vesafb_defined
.red
.length
= screen_info
.red_size
;
371 vesafb_defined
.green
.offset
= screen_info
.green_pos
;
372 vesafb_defined
.green
.length
= screen_info
.green_size
;
373 vesafb_defined
.blue
.offset
= screen_info
.blue_pos
;
374 vesafb_defined
.blue
.length
= screen_info
.blue_size
;
375 vesafb_defined
.transp
.offset
= screen_info
.rsvd_pos
;
376 vesafb_defined
.transp
.length
= screen_info
.rsvd_size
;
378 if (vesafb_defined
.bits_per_pixel
<= 8) {
379 depth
= vesafb_defined
.green
.length
;
380 vesafb_defined
.red
.length
=
381 vesafb_defined
.green
.length
=
382 vesafb_defined
.blue
.length
=
383 vesafb_defined
.bits_per_pixel
;
386 printk(KERN_INFO
"vesafb: %s: "
387 "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
388 (vesafb_defined
.bits_per_pixel
> 8) ?
389 "Truecolor" : (vga_compat
|| pmi_setpal
) ?
390 "Pseudocolor" : "Static Pseudocolor",
391 screen_info
.rsvd_size
,
392 screen_info
.red_size
,
393 screen_info
.green_size
,
394 screen_info
.blue_size
,
395 screen_info
.rsvd_pos
,
397 screen_info
.green_pos
,
398 screen_info
.blue_pos
);
400 vesafb_fix
.ypanstep
= ypan
? 1 : 0;
401 vesafb_fix
.ywrapstep
= (ypan
>1) ? 1 : 0;
403 /* request failure does not faze us, as vgacon probably has this
404 * region already (FIXME) */
405 request_region(0x3c0, 32, "vesafb");
409 unsigned int temp_size
= size_total
;
410 unsigned int type
= 0;
414 type
= MTRR_TYPE_UNCACHABLE
;
417 type
= MTRR_TYPE_WRBACK
;
420 type
= MTRR_TYPE_WRCOMB
;
423 type
= MTRR_TYPE_WRTHROUGH
;
433 /* Find the largest power-of-two */
434 temp_size
= roundup_pow_of_two(temp_size
);
436 /* Try and find a power of two to add */
438 rc
= mtrr_add(vesafb_fix
.smem_start
, temp_size
,
441 } while (temp_size
>= PAGE_SIZE
&& rc
== -EINVAL
);
447 case 1: /* uncachable */
448 info
->screen_base
= ioremap_nocache(vesafb_fix
.smem_start
, vesafb_fix
.smem_len
);
450 case 2: /* write-back */
451 info
->screen_base
= ioremap_cache(vesafb_fix
.smem_start
, vesafb_fix
.smem_len
);
453 case 3: /* write-combining */
454 info
->screen_base
= ioremap_wc(vesafb_fix
.smem_start
, vesafb_fix
.smem_len
);
456 case 4: /* write-through */
458 info
->screen_base
= ioremap(vesafb_fix
.smem_start
, vesafb_fix
.smem_len
);
461 if (!info
->screen_base
) {
463 "vesafb: abort, cannot ioremap video memory 0x%x @ 0x%lx\n",
464 vesafb_fix
.smem_len
, vesafb_fix
.smem_start
);
469 printk(KERN_INFO
"vesafb: framebuffer at 0x%lx, mapped to 0x%p, "
470 "using %dk, total %dk\n",
471 vesafb_fix
.smem_start
, info
->screen_base
,
472 size_remap
/1024, size_total
/1024);
474 info
->fbops
= &vesafb_ops
;
475 info
->var
= vesafb_defined
;
476 info
->fix
= vesafb_fix
;
477 info
->flags
= FBINFO_FLAG_DEFAULT
| FBINFO_MISC_FIRMWARE
|
478 (ypan
? FBINFO_HWACCEL_YPAN
: 0);
481 info
->fbops
->fb_pan_display
= NULL
;
483 if (fb_alloc_cmap(&info
->cmap
, 256, 0) < 0) {
487 if (register_framebuffer(info
)<0) {
489 fb_dealloc_cmap(&info
->cmap
);
492 fb_info(info
, "%s frame buffer device\n", info
->fix
.id
);
495 if (info
->screen_base
)
496 iounmap(info
->screen_base
);
497 framebuffer_release(info
);
498 release_mem_region(vesafb_fix
.smem_start
, size_total
);
502 static struct platform_driver vesafb_driver
= {
504 .name
= "vesa-framebuffer",
505 .owner
= THIS_MODULE
,
507 .probe
= vesafb_probe
,
510 module_platform_driver(vesafb_driver
);
511 MODULE_LICENSE("GPL");