2 * linux/drivers/video/hgafb.c -- Hercules graphics adaptor frame buffer device
4 * Created 25 Nov 1999 by Ferenc Bakonyi (fero@drama.obuda.kando.hu)
5 * Based on skeletonfb.c by Geert Uytterhoeven and
6 * mdacon.c by Andrew Apted
10 * - Revision 0.1.5 (13 Mar 2000): spinlocks instead of saveflags();cli();etc
12 * - Revision 0.1.4 (24 Jan 2000): fixed a bug in hga_card_detect() for
14 * - Revision 0.1.3 (22 Jan 2000): modified for the new fb_info structure
15 * screen is cleared after rmmod
17 * kernel parameter 'video=hga:font:{fontname}'
18 * module parameter 'font={fontname}'
19 * module parameter 'nologo={0|1}'
20 * the most important: boot logo :)
21 * - Revision 0.1.0 (6 Dec 1999): faster scrolling and minor fixes
22 * - First release (25 Nov 1999)
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of this archive
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/errno.h>
32 #include <linux/spinlock.h>
33 #include <linux/string.h>
35 #include <linux/tty.h>
36 #include <linux/malloc.h>
37 #include <linux/delay.h>
39 #include <linux/init.h>
40 #include <linux/ioport.h>
43 #include <video/fbcon.h>
44 #include <video/fbcon-hga.h>
48 #define INCLUDE_LINUX_LOGOBW
49 #include <linux/linux_logo.h>
54 #define DPRINTK(args...) printk(KERN_DEBUG __FILE__": " ##args)
56 #define DPRINTK(args...)
60 #define CHKINFO(ret) if (info != &fb_info) { printk(KERN_DEBUG __FILE__": This should never happen, line:%d \n", __LINE__); return ret; }
65 /* Description of the hardware layout */
67 static unsigned long hga_vram_base
; /* Base of video memory */
68 static unsigned long hga_vram_len
; /* Size of video memory */
73 static int hga_mode
= -1; /* 0 = txt, 1 = gfx mode */
75 static enum { TYPE_HERC
, TYPE_HERCPLUS
, TYPE_HERCCOLOR
} hga_type
;
76 static char *hga_type_name
;
78 #define HGA_INDEX_PORT 0x3b4 /* Register select port */
79 #define HGA_VALUE_PORT 0x3b5 /* Register value port */
80 #define HGA_MODE_PORT 0x3b8 /* Mode control port */
81 #define HGA_STATUS_PORT 0x3ba /* Status and Config port */
82 #define HGA_GFX_PORT 0x3bf /* Graphics control port */
84 /* HGA register values */
86 #define HGA_CURSOR_BLINKING 0x00
87 #define HGA_CURSOR_OFF 0x20
88 #define HGA_CURSOR_SLOWBLINK 0x60
90 #define HGA_MODE_GRAPHICS 0x02
91 #define HGA_MODE_VIDEO_EN 0x08
92 #define HGA_MODE_BLINK_EN 0x20
93 #define HGA_MODE_GFX_PAGE1 0x80
95 #define HGA_STATUS_HSYNC 0x01
96 #define HGA_STATUS_VSYNC 0x80
97 #define HGA_STATUS_VIDEO 0x08
99 #define HGA_CONFIG_COL132 0x08
100 #define HGA_GFX_MODE_EN 0x01
101 #define HGA_GFX_PAGE_EN 0x02
105 spinlock_t hga_reg_lock
= SPIN_LOCK_UNLOCKED
;
107 /* Framebuffer driver structures */
109 static struct fb_var_screeninfo hga_default_var
= {
110 720, 348, /* xres, yres */
111 720, 348, /* xres_virtual, yres_virtual */
112 0, 0, /* xoffset, yoffset */
113 1, /* bits_per_pixel */
116 {0, 1, 0}, /* green */
117 {0, 1, 0}, /* blue */
118 {0, 0, 0}, /* transp */
119 0, /* nonstd (FB_NONSTD_HGA ?) */
121 -1, -1, /* height, width */
124 /* left_margin, right_margin */
125 /* upper_margin, lower_margin */
126 /* hsync_len, vsync_len */
131 static struct fb_fix_screeninfo hga_fix
= {
133 (unsigned long) NULL
, /* smem_start */
135 FB_TYPE_PACKED_PIXELS
, /* type (not sure) */
136 0, /* type_aux (not sure) */
137 FB_VISUAL_MONO10
, /* visual */
141 90, /* line_length */
144 FB_ACCEL_NONE
/* accel */
147 static struct fb_info fb_info
;
148 static struct display disp
;
150 /* Don't assume that tty1 will be the initial current console. */
151 static int currcon
= -1;
152 static int release_io_port
= 0;
153 static int release_io_ports
= 0;
156 static char *font
= NULL
;
157 static int nologo
= 0;
160 /* -------------------------------------------------------------------------
162 * Low level hardware functions
164 * ------------------------------------------------------------------------- */
166 static void write_hga_b(unsigned int val
, unsigned char reg
)
168 outb_p(reg
, HGA_INDEX_PORT
);
169 outb_p(val
, HGA_VALUE_PORT
);
172 static void write_hga_w(unsigned int val
, unsigned char reg
)
174 outb_p(reg
, HGA_INDEX_PORT
); outb_p(val
>> 8, HGA_VALUE_PORT
);
175 outb_p(reg
+1, HGA_INDEX_PORT
); outb_p(val
& 0xff, HGA_VALUE_PORT
);
178 static int test_hga_b(unsigned char val
, unsigned char reg
)
180 outb_p(reg
, HGA_INDEX_PORT
);
181 outb (val
, HGA_VALUE_PORT
);
182 udelay(20); val
= (inb_p(HGA_VALUE_PORT
) == val
);
186 static void hga_clear_screen(void)
188 unsigned char fillchar
= 0xbf; /* magic */
191 spin_lock_irqsave(&hga_reg_lock
, flags
);
192 if (hga_mode
== HGA_TXT
)
194 else if (hga_mode
== HGA_GFX
)
196 spin_unlock_irqrestore(&hga_reg_lock
, flags
);
197 if (fillchar
!= 0xbf)
198 memset((char *)hga_vram_base
, fillchar
, hga_vram_len
);
203 static void hga_txt_mode(void)
207 spin_lock_irqsave(&hga_reg_lock
, flags
);
208 outb_p(HGA_MODE_VIDEO_EN
| HGA_MODE_BLINK_EN
, HGA_MODE_PORT
);
209 outb_p(0x00, HGA_GFX_PORT
);
210 outb_p(0x00, HGA_STATUS_PORT
);
212 write_hga_b(0x61, 0x00); /* horizontal total */
213 write_hga_b(0x50, 0x01); /* horizontal displayed */
214 write_hga_b(0x52, 0x02); /* horizontal sync pos */
215 write_hga_b(0x0f, 0x03); /* horizontal sync width */
217 write_hga_b(0x19, 0x04); /* vertical total */
218 write_hga_b(0x06, 0x05); /* vertical total adjust */
219 write_hga_b(0x19, 0x06); /* vertical displayed */
220 write_hga_b(0x19, 0x07); /* vertical sync pos */
222 write_hga_b(0x02, 0x08); /* interlace mode */
223 write_hga_b(0x0d, 0x09); /* maximum scanline */
224 write_hga_b(0x0c, 0x0a); /* cursor start */
225 write_hga_b(0x0d, 0x0b); /* cursor end */
227 write_hga_w(0x0000, 0x0c); /* start address */
228 write_hga_w(0x0000, 0x0e); /* cursor location */
231 spin_unlock_irqrestore(&hga_reg_lock
, flags
);
235 static void hga_gfx_mode(void)
239 spin_lock_irqsave(&hga_reg_lock
, flags
);
240 outb_p(0x00, HGA_STATUS_PORT
);
241 outb_p(HGA_GFX_MODE_EN
, HGA_GFX_PORT
);
242 outb_p(HGA_MODE_VIDEO_EN
| HGA_MODE_GRAPHICS
, HGA_MODE_PORT
);
244 write_hga_b(0x35, 0x00); /* horizontal total */
245 write_hga_b(0x2d, 0x01); /* horizontal displayed */
246 write_hga_b(0x2e, 0x02); /* horizontal sync pos */
247 write_hga_b(0x07, 0x03); /* horizontal sync width */
249 write_hga_b(0x5b, 0x04); /* vertical total */
250 write_hga_b(0x02, 0x05); /* vertical total adjust */
251 write_hga_b(0x57, 0x06); /* vertical displayed */
252 write_hga_b(0x57, 0x07); /* vertical sync pos */
254 write_hga_b(0x02, 0x08); /* interlace mode */
255 write_hga_b(0x03, 0x09); /* maximum scanline */
256 write_hga_b(0x00, 0x0a); /* cursor start */
257 write_hga_b(0x00, 0x0b); /* cursor end */
259 write_hga_w(0x0000, 0x0c); /* start address */
260 write_hga_w(0x0000, 0x0e); /* cursor location */
263 spin_unlock_irqrestore(&hga_reg_lock
, flags
);
267 static void hga_show_logo(void)
270 char *dest
= (char *)hga_vram_base
;
271 char *logo
= linux_logo_bw
;
272 for (y
= 134; y
< 134 + 80 ; y
++) /* this needs some cleanup */
273 for (x
= 0; x
< 10 ; x
++)
274 *(dest
+ (y
%4)*8192 + (y
>>2)*90 + x
+ 40) = ~*(logo
++);
278 static void hga_pan(unsigned int xoffset
, unsigned int yoffset
)
283 base
= (yoffset
/ 8) * 90 + xoffset
;
284 spin_lock_irqsave(&hga_reg_lock
, flags
);
285 write_hga_w(base
, 0x0c); /* start address */
286 spin_unlock_irqrestore(&hga_reg_lock
, flags
);
287 DPRINTK("hga_pan: base:%d\n", base
);
290 static void hga_blank(int blank_mode
)
294 spin_lock_irqsave(&hga_reg_lock
, flags
);
296 outb_p(0x00, HGA_MODE_PORT
); /* disable video */
298 outb_p(HGA_MODE_VIDEO_EN
| HGA_MODE_GRAPHICS
, HGA_MODE_PORT
);
300 spin_unlock_irqrestore(&hga_reg_lock
, flags
);
303 static int __init
hga_card_detect(void)
309 hga_vram_base
= VGA_MAP_MEM(0xb0000);
310 hga_vram_len
= 0x08000;
312 if (request_region(0x3b0, 12, "hgafb"))
313 release_io_ports
= 1;
314 if (request_region(0x3bf, 1, "hgafb"))
317 /* do a memory check */
319 p
= (u16
*) hga_vram_base
;
320 q
= (u16
*) (hga_vram_base
+ 0x01000);
322 p_save
= scr_readw(p
); q_save
= scr_readw(q
);
324 scr_writew(0xaa55, p
); if (scr_readw(p
) == 0xaa55) count
++;
325 scr_writew(0x55aa, p
); if (scr_readw(p
) == 0x55aa) count
++;
326 scr_writew(p_save
, p
);
332 /* Ok, there is definitely a card registering at the correct
333 * memory location, so now we do an I/O port test.
336 if (!test_hga_b(0x66, 0x0f)) { /* cursor low register */
339 if (!test_hga_b(0x99, 0x0f)) { /* cursor low register */
343 /* See if the card is a Hercules, by checking whether the vsync
344 * bit of the status register is changing. This test lasts for
345 * approximately 1/10th of a second.
348 p_save
= q_save
= inb_p(HGA_STATUS_PORT
) & HGA_STATUS_VSYNC
;
350 for (count
=0; count
< 50000 && p_save
== q_save
; count
++) {
351 q_save
= inb(HGA_STATUS_PORT
) & HGA_STATUS_VSYNC
;
355 if (p_save
!= q_save
) {
356 switch (inb_p(HGA_STATUS_PORT
) & 0x70) {
358 hga_type
= TYPE_HERCPLUS
;
359 hga_type_name
= "HerculesPlus";
362 hga_type
= TYPE_HERCCOLOR
;
363 hga_type_name
= "HerculesColor";
366 hga_type
= TYPE_HERC
;
367 hga_type_name
= "Hercules";
374 /* ------------------------------------------------------------------------- *
378 * ------------------------------------------------------------------------- */
381 * Get the Fixed Part of the Display
384 int hga_get_fix(struct fb_fix_screeninfo
*fix
, int con
, struct fb_info
*info
)
387 DPRINTK("hga_get_fix: con:%d, info:%x, fb_info:%x\n", con
, (unsigned)info
, (unsigned)&fb_info
);
395 * Get the User Defined Part of the Display
398 int hga_get_var(struct fb_var_screeninfo
*var
, int con
, struct fb_info
*info
)
401 DPRINTK("hga_get_var: con:%d, info:%x, fb_info:%x\n", con
, (unsigned)info
, (unsigned)&fb_info
);
408 * Set the User Defined Part of the Display
409 * This is the most mystical function (at least for me).
410 * What is the exact specification of xxx_set_var?
411 * Should it handle xoffset, yoffset? Should it do pannig?
412 * What does vmode mean?
415 int hga_set_var(struct fb_var_screeninfo
*var
, int con
, struct fb_info
*info
)
418 DPRINTK("hga_set_var: con:%d, activate:%x, info:0x%x, fb_info:%x\n", con
, var
->activate
, (unsigned)info
, (unsigned)&fb_info
);
420 if (var
->xres
!= 720 || var
->yres
!= 348 ||
421 var
->xres_virtual
!= 720 ||
422 var
->yres_virtual
< 348 || var
->yres_virtual
> 348 + 16 ||
423 var
->bits_per_pixel
!= 1 || var
->grayscale
!= 0) {
426 if ((var
->activate
& FB_ACTIVATE_MASK
) == FB_ACTIVATE_NOW
) {
429 (*info
->changevar
)(con
);
439 static int hga_getcolreg(u_int regno
, u_int
*red
, u_int
*green
, u_int
*blue
,
440 u_int
*transp
, struct fb_info
*info
)
443 *red
= *green
= *blue
= 0x0000;
445 } else if (regno
== 1) {
446 *red
= *green
= *blue
= 0xaaaa;
453 int hga_get_cmap(struct fb_cmap
*cmap
, int kspc
, int con
,
454 struct fb_info
*info
)
457 DPRINTK("hga_get_cmap: con:%d\n", con
);
458 return fb_get_cmap(cmap
, kspc
, hga_getcolreg
, info
);
465 static int hga_setcolreg(u_int regno
, u_int red
, u_int green
, u_int blue
,
466 u_int transp
, struct fb_info
*info
)
473 int hga_set_cmap(struct fb_cmap
*cmap
, int kspc
, int con
,
474 struct fb_info
*info
)
477 DPRINTK("hga_set_cmap: con:%d\n", con
);
478 return fb_set_cmap(cmap
, kspc
, hga_setcolreg
, info
);
482 * Pan or Wrap the Display
484 * This call looks only at xoffset, yoffset and the FB_VMODE_YWRAP flag
487 int hga_pan_display(struct fb_var_screeninfo
*var
, int con
,
488 struct fb_info
*info
)
491 DPRINTK("pan_disp: con:%d, wrap:%d, xoff:%d, yoff:%d\n", con
, var
->vmode
& FB_VMODE_YWRAP
, var
->xoffset
, var
->yoffset
);
493 if (var
->vmode
& FB_VMODE_YWRAP
) {
494 if (var
->yoffset
< 0 ||
495 var
->yoffset
>= info
->var
.yres_virtual
||
499 if (var
->xoffset
+ var
->xres
> info
->var
.xres_virtual
500 || var
->yoffset
+ var
->yres
> info
->var
.yres_virtual
505 hga_pan(var
->xoffset
, var
->yoffset
);
507 info
->var
.xoffset
= var
->xoffset
;
508 info
->var
.yoffset
= var
->yoffset
;
509 if (var
->vmode
& FB_VMODE_YWRAP
)
510 info
->var
.vmode
|= FB_VMODE_YWRAP
;
512 info
->var
.vmode
&= ~FB_VMODE_YWRAP
;
517 static struct fb_ops hgafb_ops
= {
519 fb_get_fix
: hga_get_fix
,
520 fb_get_var
: hga_get_var
,
521 fb_set_var
: hga_set_var
,
522 fb_get_cmap
: hga_get_cmap
,
523 fb_set_cmap
: hga_set_cmap
,
524 fb_pan_display
: hga_pan_display
,
528 /* ------------------------------------------------------------------------- *
530 * Functions in fb_info
532 * ------------------------------------------------------------------------- */
534 static int hgafbcon_switch(int con
, struct fb_info
*info
)
537 DPRINTK("hgafbcon_switch: currcon:%d, con:%d, info:%x, fb_info:%x\n", currcon
, con
, (unsigned)info
, (unsigned)&fb_info
);
539 /* Save the colormap and video mode */
540 #if 0 /* Not necessary in hgafb, we use fixed colormap */
541 fb_copy_cmap(&info
->cmap
, &fb_display
[currcon
].cmap
, 0);
544 if (currcon
!= -1) /* this check is absolute necessary! */
545 memcpy(&fb_display
[currcon
].var
, &info
->var
,
546 sizeof(struct fb_var_screeninfo
));
548 /* Install a new colormap and change the video mode. By default fbcon
549 * sets all the colormaps and video modes to the default values at
553 fb_copy_cmap(&fb_display
[con
].cmap
, &info
->cmap
, 0);
554 fb_set_cmap(&info
->cmap
, 1, hga_setcolreg
, info
);
557 memcpy(&info
->var
, &fb_display
[con
].var
,
558 sizeof(struct fb_var_screeninfo
));
559 /* hga_set_var(&info->var, con, &fb_info); is it necessary? */
562 /* Hack to work correctly with XF86_Mono */
567 static int hgafbcon_updatevar(int con
, struct fb_info
*info
)
570 DPRINTK("hga_update_var: con:%d, info:%x, fb_info:%x\n", con
, (unsigned)info
, (unsigned)&fb_info
);
571 return (con
< 0) ? -EINVAL
: hga_pan_display(&fb_display
[con
].var
, con
, info
);
574 static void hgafbcon_blank(int blank_mode
, struct fb_info
*info
)
577 * Blank the screen if blank_mode != 0, else unblank.
578 * Implements VESA suspend and powerdown modes on hardware
579 * that supports disabling hsync/vsync:
580 * blank_mode == 2: suspend vsync
581 * blank_mode == 3: suspend hsync
582 * blank_mode == 4: powerdown
586 DPRINTK("hga_blank: blank_mode:%d, info:%x, fb_info:%x\n", blank_mode
, (unsigned)info
, (unsigned)&fb_info
);
588 hga_blank(blank_mode
);
592 /* ------------------------------------------------------------------------- */
598 int __init
hgafb_init(void)
600 if (! hga_card_detect()) {
601 printk(KERN_ERR
"hgafb: HGA card not detected.\n");
605 printk(KERN_INFO
"hgafb: %s with %ldK of memory detected.\n",
606 hga_type_name
, hga_vram_len
/1024);
611 if (!nologo
) hga_show_logo();
614 hga_fix
.smem_start
= hga_vram_base
;
615 hga_fix
.smem_len
= hga_vram_len
;
617 disp
.var
= hga_default_var
;
618 /* disp.cmap = ???; */
619 disp
.screen_base
= (char*)hga_fix
.smem_start
;
620 disp
.visual
= hga_fix
.visual
;
621 disp
.type
= hga_fix
.type
;
622 disp
.type_aux
= hga_fix
.type_aux
;
623 disp
.ypanstep
= hga_fix
.ypanstep
;
624 disp
.ywrapstep
= hga_fix
.ywrapstep
;
625 disp
.line_length
= hga_fix
.line_length
;
626 disp
.can_soft_blank
= 1;
629 disp
.dispsw
= &fbcon_hga
;
631 #warning HGAFB will not work as a console!
632 disp
.dispsw
= &fbcon_dummy
;
634 disp
.dispsw_data
= NULL
;
636 disp
.scrollmode
= SCROLL_YREDRAW
;
638 strcpy (fb_info
.modename
, hga_fix
.id
);
640 fb_info
.flags
= FBINFO_FLAG_DEFAULT
;
641 /* fb_info.open = ??? */
642 fb_info
.var
= hga_default_var
;
643 fb_info
.fix
= hga_fix
;
644 fb_info
.monspecs
.hfmin
= 0;
645 fb_info
.monspecs
.hfmax
= 0;
646 fb_info
.monspecs
.vfmin
= 10000;
647 fb_info
.monspecs
.vfmax
= 10000;
648 fb_info
.monspecs
.dpms
= 0;
649 fb_info
.fbops
= &hgafb_ops
;
650 fb_info
.screen_base
= (char *)hga_fix
.smem_start
;
651 fb_info
.disp
= &disp
;
652 /* fb_info.display_fg = ??? */
653 /* fb_info.fontname initialized later */
654 fb_info
.changevar
= NULL
;
655 fb_info
.switch_con
= hgafbcon_switch
;
656 fb_info
.updatevar
= hgafbcon_updatevar
;
657 fb_info
.blank
= hgafbcon_blank
;
658 fb_info
.pseudo_palette
= NULL
; /* ??? */
661 if (register_framebuffer(&fb_info
) < 0)
664 printk(KERN_INFO
"fb%d: %s frame buffer device\n",
665 GET_FB_IDX(fb_info
.node
), fb_info
.modename
);
675 int __init
hgafb_setup(char *options
)
678 * Parse user speficied options
679 * `video=hga:font:VGA8x16' or
680 * `video=hga:font:SUN8x16' recommended
681 * Other supported fonts: VGA8x8, Acorn8x8, PEARL8x8
682 * More different fonts can be used with the `setfont' utility.
687 fb_info
.fontname
[0] = '\0';
689 if (!options
|| !*options
)
692 for (this_opt
= strtok(options
, ","); this_opt
;
693 this_opt
= strtok(NULL
, ",")) {
694 if (!strncmp(this_opt
, "font:", 5))
695 strcpy(fb_info
.fontname
, this_opt
+5);
707 static void hgafb_cleanup(struct fb_info
*info
)
711 unregister_framebuffer(info
);
712 if (release_io_ports
) release_region(0x3b0, 12);
713 if (release_io_port
) release_region(0x3bf, 1);
719 /* -------------------------------------------------------------------------
723 * ------------------------------------------------------------------------- */
726 int init_module(void)
729 strncpy(fb_info
.fontname
, font
, sizeof(fb_info
.fontname
)-1);
731 fb_info
.fontname
[0] = '\0';
736 void cleanup_module(void)
738 hgafb_cleanup(&fb_info
);
741 MODULE_AUTHOR("Ferenc Bakonyi (fero@drama.obuda.kando.hu)");
742 MODULE_DESCRIPTION("FBDev driver for Hercules Graphics Adaptor");
744 MODULE_PARM(font
, "s");
745 MODULE_PARM_DESC(font
, "Specifies one of the compiled-in fonts (VGA8x8, VGA8x16, SUN8x16, Acorn8x8, PEARL8x8) (default=none)");
746 MODULE_PARM(nologo
, "i");
747 MODULE_PARM_DESC(nologo
, "Disables startup logo if != 0 (default=0)");