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