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 7 * 8 * History: 9 * 10 * - Revision 0.1.8 (23 Oct 2002): Ported to new framebuffer api. 11 * 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) 27 * 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. 31 */ 32 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/delay.h> 40#include <linux/fb.h> 41#include <linux/init.h> 42#include <linux/ioport.h> 43#include <linux/platform_device.h> 44#include <asm/io.h> 45#include <asm/vga.h> 46 47#define DPRINTK(args...) 48 49#define CHKINFO(ret) 50 51/* Description of the hardware layout */ 52 53static void __iomem *hga_vram; /* Base of video memory */ 54static unsigned long hga_vram_len; /* Size of video memory */ 55 56#define HGA_ROWADDR(row) ((row%4)*8192 + (row>>2)*90) 57#define HGA_TXT 0 58#define HGA_GFX 1 59 60static inline u8 __iomem * rowaddr(struct fb_info *info, u_int row) 61{ 62 return info->screen_base + HGA_ROWADDR(row); 63} 64 65static int hga_mode = -1; /* 0 = txt, 1 = gfx mode */ 66 67static enum { TYPE_HERC, TYPE_HERCPLUS, TYPE_HERCCOLOR } hga_type; 68static char *hga_type_name; 69 70#define HGA_INDEX_PORT 0x3b4 /* Register select port */ 71#define HGA_VALUE_PORT 0x3b5 /* Register value port */ 72#define HGA_MODE_PORT 0x3b8 /* Mode control port */ 73#define HGA_STATUS_PORT 0x3ba /* Status and Config port */ 74#define HGA_GFX_PORT 0x3bf /* Graphics control port */ 75 76/* HGA register values */ 77 78#define HGA_CURSOR_BLINKING 0x00 79#define HGA_CURSOR_OFF 0x20 80#define HGA_CURSOR_SLOWBLINK 0x60 81 82#define HGA_MODE_GRAPHICS 0x02 83#define HGA_MODE_VIDEO_EN 0x08 84#define HGA_MODE_BLINK_EN 0x20 85#define HGA_MODE_GFX_PAGE1 0x80 86 87#define HGA_STATUS_HSYNC 0x01 88#define HGA_STATUS_VSYNC 0x80 89#define HGA_STATUS_VIDEO 0x08 90 91#define HGA_CONFIG_COL132 0x08 92#define HGA_GFX_MODE_EN 0x01 93#define HGA_GFX_PAGE_EN 0x02 94 95/* Global locks */ 96 97static DEFINE_SPINLOCK(hga_reg_lock); 98 99/* Framebuffer driver structures */ 100 101static struct fb_var_screeninfo hga_default_var __devinitdata = { 102 .xres = 720, 103 .yres = 348, 104 .xres_virtual = 720, 105 .yres_virtual = 348, 106 .bits_per_pixel = 1, 107 .red = {0, 1, 0}, 108 .green = {0, 1, 0}, 109 .blue = {0, 1, 0}, 110 .transp = {0, 0, 0}, 111 .height = -1, 112 .width = -1, 113}; 114 115static struct fb_fix_screeninfo hga_fix __devinitdata = { 116 .id = "HGA", 117 .type = FB_TYPE_PACKED_PIXELS, /* (not sure) */ 118 .visual = FB_VISUAL_MONO10, 119 .xpanstep = 8, 120 .ypanstep = 8, 121 .line_length = 90, 122 .accel = FB_ACCEL_NONE 123}; 124 125/* Don't assume that tty1 will be the initial current console. */ 126static int release_io_port = 0; 127static int release_io_ports = 0; 128static int nologo = 0; 129 130/* ------------------------------------------------------------------------- 131 * 132 * Low level hardware functions 133 * 134 * ------------------------------------------------------------------------- */ 135 136static void write_hga_b(unsigned int val, unsigned char reg) 137{ 138 outb_p(reg, HGA_INDEX_PORT); 139 outb_p(val, HGA_VALUE_PORT); 140} 141 142static void write_hga_w(unsigned int val, unsigned char reg) 143{ 144 outb_p(reg, HGA_INDEX_PORT); outb_p(val >> 8, HGA_VALUE_PORT); 145 outb_p(reg+1, HGA_INDEX_PORT); outb_p(val & 0xff, HGA_VALUE_PORT); 146} 147 148static int test_hga_b(unsigned char val, unsigned char reg) 149{ 150 outb_p(reg, HGA_INDEX_PORT); 151 outb (val, HGA_VALUE_PORT); 152 udelay(20); val = (inb_p(HGA_VALUE_PORT) == val); 153 return val; 154} 155 156static void hga_clear_screen(void) 157{ 158 unsigned char fillchar = 0xbf; /* magic */ 159 unsigned long flags; 160 161 spin_lock_irqsave(&hga_reg_lock, flags); 162 if (hga_mode == HGA_TXT) 163 fillchar = ' '; 164 else if (hga_mode == HGA_GFX) 165 fillchar = 0x00; 166 spin_unlock_irqrestore(&hga_reg_lock, flags); 167 if (fillchar != 0xbf) 168 memset_io(hga_vram, fillchar, hga_vram_len); 169} 170 171static void hga_txt_mode(void) 172{ 173 unsigned long flags; 174 175 spin_lock_irqsave(&hga_reg_lock, flags); 176 outb_p(HGA_MODE_VIDEO_EN | HGA_MODE_BLINK_EN, HGA_MODE_PORT); 177 outb_p(0x00, HGA_GFX_PORT); 178 outb_p(0x00, HGA_STATUS_PORT); 179 180 write_hga_b(0x61, 0x00); /* horizontal total */ 181 write_hga_b(0x50, 0x01); /* horizontal displayed */ 182 write_hga_b(0x52, 0x02); /* horizontal sync pos */ 183 write_hga_b(0x0f, 0x03); /* horizontal sync width */ 184 185 write_hga_b(0x19, 0x04); /* vertical total */ 186 write_hga_b(0x06, 0x05); /* vertical total adjust */ 187 write_hga_b(0x19, 0x06); /* vertical displayed */ 188 write_hga_b(0x19, 0x07); /* vertical sync pos */ 189 190 write_hga_b(0x02, 0x08); /* interlace mode */ 191 write_hga_b(0x0d, 0x09); /* maximum scanline */ 192 write_hga_b(0x0c, 0x0a); /* cursor start */ 193 write_hga_b(0x0d, 0x0b); /* cursor end */ 194 195 write_hga_w(0x0000, 0x0c); /* start address */ 196 write_hga_w(0x0000, 0x0e); /* cursor location */ 197 198 hga_mode = HGA_TXT; 199 spin_unlock_irqrestore(&hga_reg_lock, flags); 200} 201 202static void hga_gfx_mode(void) 203{ 204 unsigned long flags; 205 206 spin_lock_irqsave(&hga_reg_lock, flags); 207 outb_p(0x00, HGA_STATUS_PORT); 208 outb_p(HGA_GFX_MODE_EN, HGA_GFX_PORT); 209 outb_p(HGA_MODE_VIDEO_EN | HGA_MODE_GRAPHICS, HGA_MODE_PORT); 210 211 write_hga_b(0x35, 0x00); /* horizontal total */ 212 write_hga_b(0x2d, 0x01); /* horizontal displayed */ 213 write_hga_b(0x2e, 0x02); /* horizontal sync pos */ 214 write_hga_b(0x07, 0x03); /* horizontal sync width */ 215 216 write_hga_b(0x5b, 0x04); /* vertical total */ 217 write_hga_b(0x02, 0x05); /* vertical total adjust */ 218 write_hga_b(0x57, 0x06); /* vertical displayed */ 219 write_hga_b(0x57, 0x07); /* vertical sync pos */ 220 221 write_hga_b(0x02, 0x08); /* interlace mode */ 222 write_hga_b(0x03, 0x09); /* maximum scanline */ 223 write_hga_b(0x00, 0x0a); /* cursor start */ 224 write_hga_b(0x00, 0x0b); /* cursor end */ 225 226 write_hga_w(0x0000, 0x0c); /* start address */ 227 write_hga_w(0x0000, 0x0e); /* cursor location */ 228 229 hga_mode = HGA_GFX; 230 spin_unlock_irqrestore(&hga_reg_lock, flags); 231} 232 233static void hga_show_logo(struct fb_info *info) 234{ 235/* 236 void __iomem *dest = hga_vram; 237 char *logo = linux_logo_bw; 238 int x, y; 239 240 for (y = 134; y < 134 + 80 ; y++) * this needs some cleanup * 241 for (x = 0; x < 10 ; x++) 242 writeb(~*(logo++),(dest + HGA_ROWADDR(y) + x + 40)); 243*/ 244} 245 246static void hga_pan(unsigned int xoffset, unsigned int yoffset) 247{ 248 unsigned int base; 249 unsigned long flags; 250 251 base = (yoffset / 8) * 90 + xoffset; 252 spin_lock_irqsave(&hga_reg_lock, flags); 253 write_hga_w(base, 0x0c); /* start address */ 254 spin_unlock_irqrestore(&hga_reg_lock, flags); 255 DPRINTK("hga_pan: base:%d\n", base); 256} 257 258static void hga_blank(int blank_mode) 259{ 260 unsigned long flags; 261 262 spin_lock_irqsave(&hga_reg_lock, flags); 263 if (blank_mode) { 264 outb_p(0x00, HGA_MODE_PORT); /* disable video */ 265 } else { 266 outb_p(HGA_MODE_VIDEO_EN | HGA_MODE_GRAPHICS, HGA_MODE_PORT); 267 } 268 spin_unlock_irqrestore(&hga_reg_lock, flags); 269} 270 271static int __devinit hga_card_detect(void) 272{ 273 int count = 0; 274 void __iomem *p, *q; 275 unsigned short p_save, q_save; 276 277 hga_vram_len = 0x08000; 278 279 hga_vram = ioremap(0xb0000, hga_vram_len); 280 281 if (request_region(0x3b0, 12, "hgafb")) 282 release_io_ports = 1; 283 if (request_region(0x3bf, 1, "hgafb")) 284 release_io_port = 1; 285 286 /* do a memory check */ 287 288 p = hga_vram; 289 q = hga_vram + 0x01000; 290 291 p_save = readw(p); q_save = readw(q); 292 293 writew(0xaa55, p); if (readw(p) == 0xaa55) count++; 294 writew(0x55aa, p); if (readw(p) == 0x55aa) count++; 295 writew(p_save, p); 296 297 if (count != 2) 298 goto error; 299 300 /* Ok, there is definitely a card registering at the correct 301 * memory location, so now we do an I/O port test. 302 */ 303 304 if (!test_hga_b(0x66, 0x0f)) /* cursor low register */ 305 goto error; 306 307 if (!test_hga_b(0x99, 0x0f)) /* cursor low register */ 308 goto error; 309 310 /* See if the card is a Hercules, by checking whether the vsync 311 * bit of the status register is changing. This test lasts for 312 * approximately 1/10th of a second. 313 */ 314 315 p_save = q_save = inb_p(HGA_STATUS_PORT) & HGA_STATUS_VSYNC; 316 317 for (count=0; count < 50000 && p_save == q_save; count++) { 318 q_save = inb(HGA_STATUS_PORT) & HGA_STATUS_VSYNC; 319 udelay(2); 320 } 321 322 if (p_save == q_save) 323 goto error; 324 325 switch (inb_p(HGA_STATUS_PORT) & 0x70) { 326 case 0x10: 327 hga_type = TYPE_HERCPLUS; 328 hga_type_name = "HerculesPlus"; 329 break; 330 case 0x50: 331 hga_type = TYPE_HERCCOLOR; 332 hga_type_name = "HerculesColor"; 333 break; 334 default: 335 hga_type = TYPE_HERC; 336 hga_type_name = "Hercules"; 337 break; 338 } 339 return 1; 340error: 341 if (release_io_ports) 342 release_region(0x3b0, 12); 343 if (release_io_port) 344 release_region(0x3bf, 1); 345 return 0; 346} 347 348/** 349 * hgafb_open - open the framebuffer device 350 * @info:pointer to fb_info object containing info for current hga board 351 * @int:open by console system or userland. 352 */ 353 354static int hgafb_open(struct fb_info *info, int init) 355{ 356 hga_gfx_mode(); 357 hga_clear_screen(); 358 if (!nologo) hga_show_logo(info); 359 return 0; 360} 361 362/** 363 * hgafb_open - open the framebuffer device 364 * @info:pointer to fb_info object containing info for current hga board 365 * @int:open by console system or userland. 366 */ 367 368static int hgafb_release(struct fb_info *info, int init) 369{ 370 hga_txt_mode(); 371 hga_clear_screen(); 372 return 0; 373} 374 375/** 376 * hgafb_setcolreg - set color registers 377 * @regno:register index to set 378 * @red:red value, unused 379 * @green:green value, unused 380 * @blue:blue value, unused 381 * @transp:transparency value, unused 382 * @info:unused 383 * 384 * This callback function is used to set the color registers of a HGA 385 * board. Since we have only two fixed colors only @regno is checked. 386 * A zero is returned on success and 1 for failure. 387 */ 388 389static int hgafb_setcolreg(u_int regno, u_int red, u_int green, u_int blue, 390 u_int transp, struct fb_info *info) 391{ 392 if (regno > 1) 393 return 1; 394 return 0; 395} 396 397/** 398 * hga_pan_display - pan or wrap the display 399 * @var:contains new xoffset, yoffset and vmode values 400 * @info:pointer to fb_info object containing info for current hga board 401 * 402 * This function looks only at xoffset, yoffset and the %FB_VMODE_YWRAP 403 * flag in @var. If input parameters are correct it calls hga_pan() to 404 * program the hardware. @info->var is updated to the new values. 405 * A zero is returned on success and %-EINVAL for failure. 406 */ 407 408static int hgafb_pan_display(struct fb_var_screeninfo *var, 409 struct fb_info *info) 410{ 411 if (var->vmode & FB_VMODE_YWRAP) { 412 if (var->yoffset < 0 || 413 var->yoffset >= info->var.yres_virtual || 414 var->xoffset) 415 return -EINVAL; 416 } else { 417 if (var->xoffset + var->xres > info->var.xres_virtual 418 || var->yoffset + var->yres > info->var.yres_virtual 419 || var->yoffset % 8) 420 return -EINVAL; 421 } 422 423 hga_pan(var->xoffset, var->yoffset); 424 return 0; 425} 426 427/** 428 * hgafb_blank - (un)blank the screen 429 * @blank_mode:blanking method to use 430 * @info:unused 431 * 432 * Blank the screen if blank_mode != 0, else unblank. 433 * Implements VESA suspend and powerdown modes on hardware that supports 434 * disabling hsync/vsync: 435 * @blank_mode == 2 means suspend vsync, 436 * @blank_mode == 3 means suspend hsync, 437 * @blank_mode == 4 means powerdown. 438 */ 439 440static int hgafb_blank(int blank_mode, struct fb_info *info) 441{ 442 hga_blank(blank_mode); 443 return 0; 444} 445 446/* 447 * Accel functions 448 */ 449#ifdef CONFIG_FB_HGA_ACCEL 450static void hgafb_fillrect(struct fb_info *info, const struct fb_fillrect *rect) 451{ 452 u_int rows, y; 453 u8 __iomem *dest; 454 455 y = rect->dy; 456 457 for (rows = rect->height; rows--; y++) { 458 dest = rowaddr(info, y) + (rect->dx >> 3); 459 switch (rect->rop) { 460 case ROP_COPY: 461 //fb_memset(dest, rect->color, (rect->width >> 3)); 462 break; 463 case ROP_XOR: 464 fb_writeb(~(fb_readb(dest)), dest); 465 break; 466 } 467 } 468} 469 470static void hgafb_copyarea(struct fb_info *info, const struct fb_copyarea *area) 471{ 472 u_int rows, y1, y2; 473 u8 __iomem *src; 474 u8 __iomem *dest; 475 476 if (area->dy <= area->sy) { 477 y1 = area->sy; 478 y2 = area->dy; 479 480 for (rows = area->height; rows--; ) { 481 src = rowaddr(info, y1) + (area->sx >> 3); 482 dest = rowaddr(info, y2) + (area->dx >> 3); 483 //fb_memmove(dest, src, (area->width >> 3)); 484 y1++; 485 y2++; 486 } 487 } else { 488 y1 = area->sy + area->height - 1; 489 y2 = area->dy + area->height - 1; 490 491 for (rows = area->height; rows--;) { 492 src = rowaddr(info, y1) + (area->sx >> 3); 493 dest = rowaddr(info, y2) + (area->dx >> 3); 494 //fb_memmove(dest, src, (area->width >> 3)); 495 y1--; 496 y2--; 497 } 498 } 499} 500 501static void hgafb_imageblit(struct fb_info *info, const struct fb_image *image) 502{ 503 u8 __iomem *dest; 504 u8 *cdat = (u8 *) image->data; 505 u_int rows, y = image->dy; 506 u8 d; 507 508 for (rows = image->height; rows--; y++) { 509 d = *cdat++; 510 dest = rowaddr(info, y) + (image->dx >> 3); 511 fb_writeb(d, dest); 512 } 513} 514#else /* !CONFIG_FB_HGA_ACCEL */ 515#define hgafb_fillrect cfb_fillrect 516#define hgafb_copyarea cfb_copyarea 517#define hgafb_imageblit cfb_imageblit 518#endif /* CONFIG_FB_HGA_ACCEL */ 519 520 521static struct fb_ops hgafb_ops = { 522 .owner = THIS_MODULE, 523 .fb_open = hgafb_open, 524 .fb_release = hgafb_release, 525 .fb_setcolreg = hgafb_setcolreg, 526 .fb_pan_display = hgafb_pan_display, 527 .fb_blank = hgafb_blank, 528 .fb_fillrect = hgafb_fillrect, 529 .fb_copyarea = hgafb_copyarea, 530 .fb_imageblit = hgafb_imageblit, 531}; 532 533/* ------------------------------------------------------------------------- * 534 * 535 * Functions in fb_info 536 * 537 * ------------------------------------------------------------------------- */ 538 539/* ------------------------------------------------------------------------- */ 540 541 /* 542 * Initialization 543 */ 544 545static int __devinit hgafb_probe(struct platform_device *pdev) 546{ 547 struct fb_info *info; 548 549 if (! hga_card_detect()) { 550 printk(KERN_INFO "hgafb: HGA card not detected.\n"); 551 if (hga_vram) 552 iounmap(hga_vram); 553 return -EINVAL; 554 } 555 556 printk(KERN_INFO "hgafb: %s with %ldK of memory detected.\n", 557 hga_type_name, hga_vram_len/1024); 558 559 info = framebuffer_alloc(0, &pdev->dev); 560 if (!info) { 561 iounmap(hga_vram); 562 return -ENOMEM; 563 } 564 565 hga_fix.smem_start = (unsigned long)hga_vram; 566 hga_fix.smem_len = hga_vram_len; 567 568 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN; 569 info->var = hga_default_var; 570 info->fix = hga_fix; 571 info->monspecs.hfmin = 0; 572 info->monspecs.hfmax = 0; 573 info->monspecs.vfmin = 10000; 574 info->monspecs.vfmax = 10000; 575 info->monspecs.dpms = 0; 576 info->fbops = &hgafb_ops; 577 info->screen_base = hga_vram; 578 579 if (register_framebuffer(info) < 0) { 580 framebuffer_release(info); 581 iounmap(hga_vram); 582 return -EINVAL; 583 } 584 585 printk(KERN_INFO "fb%d: %s frame buffer device\n", 586 info->node, info->fix.id); 587 platform_set_drvdata(pdev, info); 588 return 0; 589} 590 591static int __devexit hgafb_remove(struct platform_device *pdev) 592{ 593 struct fb_info *info = platform_get_drvdata(pdev); 594 595 hga_txt_mode(); 596 hga_clear_screen(); 597 598 if (info) { 599 unregister_framebuffer(info); 600 framebuffer_release(info); 601 } 602 603 iounmap(hga_vram); 604 605 if (release_io_ports) 606 release_region(0x3b0, 12); 607 608 if (release_io_port) 609 release_region(0x3bf, 1); 610 611 return 0; 612} 613 614static struct platform_driver hgafb_driver = { 615 .probe = hgafb_probe, 616 .remove = __devexit_p(hgafb_remove), 617 .driver = { 618 .name = "hgafb", 619 }, 620}; 621 622static struct platform_device *hgafb_device; 623 624static int __init hgafb_init(void) 625{ 626 int ret; 627 628 if (fb_get_options("hgafb", NULL)) 629 return -ENODEV; 630 631 ret = platform_driver_register(&hgafb_driver); 632 633 if (!ret) { 634 hgafb_device = platform_device_register_simple("hgafb", 0, NULL, 0); 635 636 if (IS_ERR(hgafb_device)) { 637 platform_driver_unregister(&hgafb_driver); 638 ret = PTR_ERR(hgafb_device); 639 } 640 } 641 642 return ret; 643} 644 645static void __exit hgafb_exit(void) 646{ 647 platform_device_unregister(hgafb_device); 648 platform_driver_unregister(&hgafb_driver); 649} 650 651/* ------------------------------------------------------------------------- 652 * 653 * Modularization 654 * 655 * ------------------------------------------------------------------------- */ 656 657MODULE_AUTHOR("Ferenc Bakonyi (fero@drama.obuda.kando.hu)"); 658MODULE_DESCRIPTION("FBDev driver for Hercules Graphics Adaptor"); 659MODULE_LICENSE("GPL"); 660 661module_param(nologo, bool, 0); 662MODULE_PARM_DESC(nologo, "Disables startup logo if != 0 (default=0)"); 663module_init(hgafb_init); 664module_exit(hgafb_exit); 665