initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / video / hgafb.c
blob87d008af2a19283b204618cc1951f4b88291273e
1 /*
2 * linux/drivers/video/hgafb.c -- Hercules graphics adaptor frame buffer device
3 *
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
8 * History:
10 * - Revision 0.1.8 (23 Oct 2002): Ported to new framebuffer api.
12 * - Revision 0.1.7 (23 Jan 2001): fix crash resulting from MDA only cards
13 * being detected as Hercules. (Paul G.)
14 * - Revision 0.1.6 (17 Aug 2000): new style structs
15 * documentation
16 * - Revision 0.1.5 (13 Mar 2000): spinlocks instead of saveflags();cli();etc
17 * minor fixes
18 * - Revision 0.1.4 (24 Jan 2000): fixed a bug in hga_card_detect() for
19 * HGA-only systems
20 * - Revision 0.1.3 (22 Jan 2000): modified for the new fb_info structure
21 * screen is cleared after rmmod
22 * virtual resolutions
23 * module parameter 'nologo={0|1}'
24 * the most important: boot logo :)
25 * - Revision 0.1.0 (6 Dec 1999): faster scrolling and minor fixes
26 * - First release (25 Nov 1999)
28 * This file is subject to the terms and conditions of the GNU General Public
29 * License. See the file COPYING in the main directory of this archive
30 * for more details.
33 #include <linux/module.h>
34 #include <linux/kernel.h>
35 #include <linux/errno.h>
36 #include <linux/spinlock.h>
37 #include <linux/string.h>
38 #include <linux/mm.h>
39 #include <linux/tty.h>
40 #include <linux/slab.h>
41 #include <linux/delay.h>
42 #include <linux/fb.h>
43 #include <linux/init.h>
44 #include <linux/ioport.h>
45 #include <asm/io.h>
46 #include <asm/vga.h>
48 #if 0
49 #define DPRINTK(args...) printk(KERN_DEBUG __FILE__": " ##args)
50 #else
51 #define DPRINTK(args...)
52 #endif
54 #if 0
55 #define CHKINFO(ret) if (info != &fb_info) { printk(KERN_DEBUG __FILE__": This should never happen, line:%d \n", __LINE__); return ret; }
56 #else
57 #define CHKINFO(ret)
58 #endif
60 /* Description of the hardware layout */
62 static unsigned long hga_vram_base; /* Base of video memory */
63 static unsigned long hga_vram_len; /* Size of video memory */
65 #define HGA_ROWADDR(row) ((row%4)*8192 + (row>>2)*90)
66 #define HGA_TXT 0
67 #define HGA_GFX 1
69 static inline u8* rowaddr(struct fb_info *info, u_int row)
71 return info->screen_base + HGA_ROWADDR(row);
74 static int hga_mode = -1; /* 0 = txt, 1 = gfx mode */
76 static enum { TYPE_HERC, TYPE_HERCPLUS, TYPE_HERCCOLOR } hga_type;
77 static char *hga_type_name;
79 #define HGA_INDEX_PORT 0x3b4 /* Register select port */
80 #define HGA_VALUE_PORT 0x3b5 /* Register value port */
81 #define HGA_MODE_PORT 0x3b8 /* Mode control port */
82 #define HGA_STATUS_PORT 0x3ba /* Status and Config port */
83 #define HGA_GFX_PORT 0x3bf /* Graphics control port */
85 /* HGA register values */
87 #define HGA_CURSOR_BLINKING 0x00
88 #define HGA_CURSOR_OFF 0x20
89 #define HGA_CURSOR_SLOWBLINK 0x60
91 #define HGA_MODE_GRAPHICS 0x02
92 #define HGA_MODE_VIDEO_EN 0x08
93 #define HGA_MODE_BLINK_EN 0x20
94 #define HGA_MODE_GFX_PAGE1 0x80
96 #define HGA_STATUS_HSYNC 0x01
97 #define HGA_STATUS_VSYNC 0x80
98 #define HGA_STATUS_VIDEO 0x08
100 #define HGA_CONFIG_COL132 0x08
101 #define HGA_GFX_MODE_EN 0x01
102 #define HGA_GFX_PAGE_EN 0x02
104 /* Global locks */
106 static spinlock_t hga_reg_lock = SPIN_LOCK_UNLOCKED;
108 /* Framebuffer driver structures */
110 static struct fb_var_screeninfo hga_default_var = {
111 .xres = 720,
112 .yres = 348,
113 .xres_virtual = 720,
114 .yres_virtual = 348,
115 .bits_per_pixel = 1,
116 .red = {0, 1, 0},
117 .green = {0, 1, 0},
118 .blue = {0, 1, 0},
119 .transp = {0, 0, 0},
120 .height = -1,
121 .width = -1,
124 static struct fb_fix_screeninfo hga_fix = {
125 .id = "HGA",
126 .type = FB_TYPE_PACKED_PIXELS, /* (not sure) */
127 .visual = FB_VISUAL_MONO10,
128 .xpanstep = 8,
129 .ypanstep = 8,
130 .line_length = 90,
131 .accel = FB_ACCEL_NONE
134 static struct fb_info fb_info;
136 /* Don't assume that tty1 will be the initial current console. */
137 static int release_io_port = 0;
138 static int release_io_ports = 0;
139 static int nologo = 0;
141 /* -------------------------------------------------------------------------
143 * Low level hardware functions
145 * ------------------------------------------------------------------------- */
147 static void write_hga_b(unsigned int val, unsigned char reg)
149 outb_p(reg, HGA_INDEX_PORT);
150 outb_p(val, HGA_VALUE_PORT);
153 static void write_hga_w(unsigned int val, unsigned char reg)
155 outb_p(reg, HGA_INDEX_PORT); outb_p(val >> 8, HGA_VALUE_PORT);
156 outb_p(reg+1, HGA_INDEX_PORT); outb_p(val & 0xff, HGA_VALUE_PORT);
159 static int test_hga_b(unsigned char val, unsigned char reg)
161 outb_p(reg, HGA_INDEX_PORT);
162 outb (val, HGA_VALUE_PORT);
163 udelay(20); val = (inb_p(HGA_VALUE_PORT) == val);
164 return val;
167 static void hga_clear_screen(void)
169 unsigned char fillchar = 0xbf; /* magic */
170 unsigned long flags;
172 spin_lock_irqsave(&hga_reg_lock, flags);
173 if (hga_mode == HGA_TXT)
174 fillchar = ' ';
175 else if (hga_mode == HGA_GFX)
176 fillchar = 0x00;
177 spin_unlock_irqrestore(&hga_reg_lock, flags);
178 if (fillchar != 0xbf)
179 isa_memset_io(hga_vram_base, fillchar, hga_vram_len);
182 static void hga_txt_mode(void)
184 unsigned long flags;
186 spin_lock_irqsave(&hga_reg_lock, flags);
187 outb_p(HGA_MODE_VIDEO_EN | HGA_MODE_BLINK_EN, HGA_MODE_PORT);
188 outb_p(0x00, HGA_GFX_PORT);
189 outb_p(0x00, HGA_STATUS_PORT);
191 write_hga_b(0x61, 0x00); /* horizontal total */
192 write_hga_b(0x50, 0x01); /* horizontal displayed */
193 write_hga_b(0x52, 0x02); /* horizontal sync pos */
194 write_hga_b(0x0f, 0x03); /* horizontal sync width */
196 write_hga_b(0x19, 0x04); /* vertical total */
197 write_hga_b(0x06, 0x05); /* vertical total adjust */
198 write_hga_b(0x19, 0x06); /* vertical displayed */
199 write_hga_b(0x19, 0x07); /* vertical sync pos */
201 write_hga_b(0x02, 0x08); /* interlace mode */
202 write_hga_b(0x0d, 0x09); /* maximum scanline */
203 write_hga_b(0x0c, 0x0a); /* cursor start */
204 write_hga_b(0x0d, 0x0b); /* cursor end */
206 write_hga_w(0x0000, 0x0c); /* start address */
207 write_hga_w(0x0000, 0x0e); /* cursor location */
209 hga_mode = HGA_TXT;
210 spin_unlock_irqrestore(&hga_reg_lock, flags);
213 static void hga_gfx_mode(void)
215 unsigned long flags;
217 spin_lock_irqsave(&hga_reg_lock, flags);
218 outb_p(0x00, HGA_STATUS_PORT);
219 outb_p(HGA_GFX_MODE_EN, HGA_GFX_PORT);
220 outb_p(HGA_MODE_VIDEO_EN | HGA_MODE_GRAPHICS, HGA_MODE_PORT);
222 write_hga_b(0x35, 0x00); /* horizontal total */
223 write_hga_b(0x2d, 0x01); /* horizontal displayed */
224 write_hga_b(0x2e, 0x02); /* horizontal sync pos */
225 write_hga_b(0x07, 0x03); /* horizontal sync width */
227 write_hga_b(0x5b, 0x04); /* vertical total */
228 write_hga_b(0x02, 0x05); /* vertical total adjust */
229 write_hga_b(0x57, 0x06); /* vertical displayed */
230 write_hga_b(0x57, 0x07); /* vertical sync pos */
232 write_hga_b(0x02, 0x08); /* interlace mode */
233 write_hga_b(0x03, 0x09); /* maximum scanline */
234 write_hga_b(0x00, 0x0a); /* cursor start */
235 write_hga_b(0x00, 0x0b); /* cursor end */
237 write_hga_w(0x0000, 0x0c); /* start address */
238 write_hga_w(0x0000, 0x0e); /* cursor location */
240 hga_mode = HGA_GFX;
241 spin_unlock_irqrestore(&hga_reg_lock, flags);
244 static void hga_show_logo(struct fb_info *info)
247 unsigned long dest = hga_vram_base;
248 char *logo = linux_logo_bw;
249 int x, y;
251 for (y = 134; y < 134 + 80 ; y++) * this needs some cleanup *
252 for (x = 0; x < 10 ; x++)
253 isa_writeb(~*(logo++),(dest + HGA_ROWADDR(y) + x + 40));
257 static void hga_pan(unsigned int xoffset, unsigned int yoffset)
259 unsigned int base;
260 unsigned long flags;
262 base = (yoffset / 8) * 90 + xoffset;
263 spin_lock_irqsave(&hga_reg_lock, flags);
264 write_hga_w(base, 0x0c); /* start address */
265 spin_unlock_irqrestore(&hga_reg_lock, flags);
266 DPRINTK("hga_pan: base:%d\n", base);
269 static void hga_blank(int blank_mode)
271 unsigned long flags;
273 spin_lock_irqsave(&hga_reg_lock, flags);
274 if (blank_mode) {
275 outb_p(0x00, HGA_MODE_PORT); /* disable video */
276 } else {
277 outb_p(HGA_MODE_VIDEO_EN | HGA_MODE_GRAPHICS, HGA_MODE_PORT);
279 spin_unlock_irqrestore(&hga_reg_lock, flags);
282 static int __init hga_card_detect(void)
284 int count=0;
285 unsigned long p, q;
286 unsigned short p_save, q_save;
288 hga_vram_base = 0xb0000;
289 hga_vram_len = 0x08000;
291 if (request_region(0x3b0, 12, "hgafb"))
292 release_io_ports = 1;
293 if (request_region(0x3bf, 1, "hgafb"))
294 release_io_port = 1;
296 /* do a memory check */
298 p = hga_vram_base;
299 q = hga_vram_base + 0x01000;
301 p_save = isa_readw(p); q_save = isa_readw(q);
303 isa_writew(0xaa55, p); if (isa_readw(p) == 0xaa55) count++;
304 isa_writew(0x55aa, p); if (isa_readw(p) == 0x55aa) count++;
305 isa_writew(p_save, p);
307 if (count != 2) {
308 return 0;
311 /* Ok, there is definitely a card registering at the correct
312 * memory location, so now we do an I/O port test.
315 if (!test_hga_b(0x66, 0x0f)) { /* cursor low register */
316 return 0;
318 if (!test_hga_b(0x99, 0x0f)) { /* cursor low register */
319 return 0;
322 /* See if the card is a Hercules, by checking whether the vsync
323 * bit of the status register is changing. This test lasts for
324 * approximately 1/10th of a second.
327 p_save = q_save = inb_p(HGA_STATUS_PORT) & HGA_STATUS_VSYNC;
329 for (count=0; count < 50000 && p_save == q_save; count++) {
330 q_save = inb(HGA_STATUS_PORT) & HGA_STATUS_VSYNC;
331 udelay(2);
334 if (p_save == q_save)
335 return 0;
337 switch (inb_p(HGA_STATUS_PORT) & 0x70) {
338 case 0x10:
339 hga_type = TYPE_HERCPLUS;
340 hga_type_name = "HerculesPlus";
341 break;
342 case 0x50:
343 hga_type = TYPE_HERCCOLOR;
344 hga_type_name = "HerculesColor";
345 break;
346 default:
347 hga_type = TYPE_HERC;
348 hga_type_name = "Hercules";
349 break;
351 return 1;
355 * hgafb_open - open the framebuffer device
356 * @info:pointer to fb_info object containing info for current hga board
357 * @int:open by console system or userland.
360 static int hgafb_open(struct fb_info *info, int init)
362 hga_gfx_mode();
363 hga_clear_screen();
364 if (!nologo) hga_show_logo(info);
365 return 0;
369 * hgafb_open - open the framebuffer device
370 * @info:pointer to fb_info object containing info for current hga board
371 * @int:open by console system or userland.
374 static int hgafb_release(struct fb_info *info, int init)
376 hga_txt_mode();
377 hga_clear_screen();
378 return 0;
382 * hgafb_setcolreg - set color registers
383 * @regno:register index to set
384 * @red:red value, unused
385 * @green:green value, unused
386 * @blue:blue value, unused
387 * @transp:transparency value, unused
388 * @info:unused
390 * This callback function is used to set the color registers of a HGA
391 * board. Since we have only two fixed colors only @regno is checked.
392 * A zero is returned on success and 1 for failure.
395 static int hgafb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
396 u_int transp, struct fb_info *info)
398 if (regno > 1)
399 return 1;
400 return 0;
404 * hga_pan_display - pan or wrap the display
405 * @var:contains new xoffset, yoffset and vmode values
406 * @info:pointer to fb_info object containing info for current hga board
408 * This function looks only at xoffset, yoffset and the %FB_VMODE_YWRAP
409 * flag in @var. If input parameters are correct it calls hga_pan() to
410 * program the hardware. @info->var is updated to the new values.
411 * A zero is returned on success and %-EINVAL for failure.
414 int hgafb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
416 if (var->vmode & FB_VMODE_YWRAP) {
417 if (var->yoffset < 0 ||
418 var->yoffset >= info->var.yres_virtual ||
419 var->xoffset)
420 return -EINVAL;
421 } else {
422 if (var->xoffset + var->xres > info->var.xres_virtual
423 || var->yoffset + var->yres > info->var.yres_virtual
424 || var->yoffset % 8)
425 return -EINVAL;
428 hga_pan(var->xoffset, var->yoffset);
429 return 0;
433 * hgafb_blank - (un)blank the screen
434 * @blank_mode:blanking method to use
435 * @info:unused
437 * Blank the screen if blank_mode != 0, else unblank.
438 * Implements VESA suspend and powerdown modes on hardware that supports
439 * disabling hsync/vsync:
440 * @blank_mode == 2 means suspend vsync,
441 * @blank_mode == 3 means suspend hsync,
442 * @blank_mode == 4 means powerdown.
445 static int hgafb_blank(int blank_mode, struct fb_info *info)
447 hga_blank(blank_mode);
448 return 0;
452 * Accel functions
454 #ifdef CONFIG_FB_HGA_ACCEL
455 static void hgafb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
457 u_int rows, y;
458 u8 *dest;
460 y = rect->dy;
462 for (rows = rect->height; rows--; y++) {
463 dest = rowaddr(info, y) + (rect->dx >> 3);
464 switch (rect->rop) {
465 case ROP_COPY:
466 //fb_memset(dest, rect->color, (rect->width >> 3));
467 break;
468 case ROP_XOR:
469 *dest = ~*dest;
470 break;
475 static void hgafb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
477 u_int rows, y1, y2;
478 u8 *src, *dest;
480 if (area->dy <= area->sy) {
481 y1 = area->sy;
482 y2 = area->dy;
484 for (rows = area->height; rows--; ) {
485 src = rowaddr(info, y1) + (area->sx >> 3);
486 dest = rowaddr(info, y2) + (area->dx >> 3);
487 //fb_memmove(dest, src, (area->width >> 3));
488 y1++;
489 y2++;
491 } else {
492 y1 = area->sy + area->height - 1;
493 y2 = area->dy + area->height - 1;
495 for (rows = area->height; rows--;) {
496 src = rowaddr(info, y1) + (area->sx >> 3);
497 dest = rowaddr(info, y2) + (area->dx >> 3);
498 //fb_memmove(dest, src, (area->width >> 3));
499 y1--;
500 y2--;
505 static void hgafb_imageblit(struct fb_info *info, const struct fb_image *image)
507 u8 *dest, *cdat = (u8 *) image->data;
508 u_int rows, y = image->dy;
509 u8 d;
511 for (rows = image->height; rows--; y++) {
512 d = *cdat++;
513 dest = rowaddr(info, y) + (image->dx >> 3);
514 *dest = d;
517 #else /* !CONFIG_FB_HGA_ACCEL */
518 #define hgafb_fillrect cfb_fillrect
519 #define hgafb_copyarea cfb_copyarea
520 #define hgafb_imageblit cfb_imageblit
521 #endif /* CONFIG_FB_HGA_ACCEL */
524 static struct fb_ops hgafb_ops = {
525 .owner = THIS_MODULE,
526 .fb_open = hgafb_open,
527 .fb_release = hgafb_release,
528 .fb_setcolreg = hgafb_setcolreg,
529 .fb_pan_display = hgafb_pan_display,
530 .fb_blank = hgafb_blank,
531 .fb_fillrect = hgafb_fillrect,
532 .fb_copyarea = hgafb_copyarea,
533 .fb_imageblit = hgafb_imageblit,
536 /* ------------------------------------------------------------------------- *
538 * Functions in fb_info
540 * ------------------------------------------------------------------------- */
542 /* ------------------------------------------------------------------------- */
545 * Initialization
548 int __init hgafb_init(void)
550 if (fb_get_options("hgafb", NULL))
551 return -ENODEV;
553 if (! hga_card_detect()) {
554 printk(KERN_INFO "hgafb: HGA card not detected.\n");
555 return -EINVAL;
558 printk(KERN_INFO "hgafb: %s with %ldK of memory detected.\n",
559 hga_type_name, hga_vram_len/1024);
561 hga_fix.smem_start = VGA_MAP_MEM(hga_vram_base);
562 hga_fix.smem_len = hga_vram_len;
564 fb_info.flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
565 fb_info.var = hga_default_var;
566 fb_info.fix = hga_fix;
567 fb_info.monspecs.hfmin = 0;
568 fb_info.monspecs.hfmax = 0;
569 fb_info.monspecs.vfmin = 10000;
570 fb_info.monspecs.vfmax = 10000;
571 fb_info.monspecs.dpms = 0;
572 fb_info.fbops = &hgafb_ops;
573 fb_info.screen_base = (char *)hga_fix.smem_start;
575 if (register_framebuffer(&fb_info) < 0)
576 return -EINVAL;
578 printk(KERN_INFO "fb%d: %s frame buffer device\n",
579 fb_info.node, fb_info.fix.id);
580 return 0;
584 * Setup
587 int __init hgafb_setup(char *options)
589 return 0;
592 #ifdef MODULE
593 static void __exit hgafb_exit(void)
595 hga_txt_mode();
596 hga_clear_screen();
597 unregister_framebuffer(&fb_info);
598 if (release_io_ports) release_region(0x3b0, 12);
599 if (release_io_port) release_region(0x3bf, 1);
601 #endif
603 /* -------------------------------------------------------------------------
605 * Modularization
607 * ------------------------------------------------------------------------- */
609 MODULE_AUTHOR("Ferenc Bakonyi (fero@drama.obuda.kando.hu)");
610 MODULE_DESCRIPTION("FBDev driver for Hercules Graphics Adaptor");
611 MODULE_LICENSE("GPL");
613 MODULE_PARM(nologo, "i");
614 MODULE_PARM_DESC(nologo, "Disables startup logo if != 0 (default=0)");
615 module_init(hgafb_init);
617 #ifdef MODULE
618 module_exit(hgafb_exit);
619 #endif