home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / SVGALIB / SVGALIB1.TAR / svgalib / src / ati.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-11  |  6.1 KB  |  257 lines

  1. /* VGAlib version 1.2 - (c) 1993 Tommy Frandsen            */
  2. /*                                   */
  3. /* This library is free software; you can redistribute it and/or   */
  4. /* modify it without any restrictions. This library is distributed */
  5. /* in the hope that it will be useful, but without any warranty.   */
  6.  
  7. /* Multi-chipset support Copyright (C) 1993 Harm Hanemaayer */
  8.  
  9. /*
  10.  * Initial ATI Driver    January 1995, Scott D. Heavner (sdh@po.cwru.edu)
  11.  */
  12.  
  13. /* ==== SO FAR THE ONLY SPECIAL THING THIS ATI DRIVER DOES IS ALLOW 
  14.  * ==== GRAPHICS MODES TO FUNCTION WITH A 132col TERMINAL, THERE ARE NO
  15.  * ==== NEW MODES YET (there will be).  No the XF86 driver won't help as
  16.  * ==== XF863.1 doesn't work properly with my ATI Graphics Ultra, 
  17.  * ==== I will be having a look at the XF86 drivers now that I've found
  18.  * ==== the problem an implemented a fix here.
  19.  *
  20.  * ---- If this driver detects a mach32 chip, it does not init the ATI
  21.  * ---- driver, hopefully, the autodetect will find the Mach32 driver.
  22.  * ---- We should probably change this to run the Mach32 as an ATI SVGA
  23.  * ---- board, but probe for the Mach32 first, this would allow the user
  24.  * ---- to force ATI mode in a config file.
  25.  */
  26.  
  27. #define ATI_UNKNOWN    0
  28. #define    ATI_18800    1
  29. #define    ATI_18800_1    2
  30. #define    ATI_28800_2    3
  31. #define    ATI_28800_4    4
  32. #define    ATI_28800_5    5
  33. #define ATI_68800    'a'
  34.  
  35. #define ATIREG(s)    (EXT+(s)-0xa0) 
  36.  
  37. #define MIN(a,b)    (((a)<(b))?(a):(b))
  38. #define ABS(a)        (((a)<0)?(-(a)):(a))
  39.  
  40. #include <stdio.h>
  41. #include <stdlib.h>     /* for NULL */
  42. #include <string.h>
  43. #include <sys/types.h>
  44. #include <sys/mman.h>
  45. #include <fcntl.h>
  46. #include <unistd.h>
  47.  
  48. #include "vga.h"
  49. #include "libvga.h"
  50. #include "driver.h"
  51.  
  52. static unsigned char ati_flags_42, ati_flags_44, ati_gate;
  53. static short ati_base=0;
  54.  
  55. static int nothing() { return 0; }
  56.  
  57. /* Initialize chipset (called after detection) */
  58. static int ati_init( int force, int par1, int par2) {
  59.         if (__svgalib_driver_report)
  60.                 printf("Using ATI (mostly VGA) driver.\n");
  61.         driverspecs = &ati_driverspecs;
  62.  
  63.     /* Have to give ourselves some more permissions -- last port currently used is 0x3df */
  64.     if (ioperm(MIN(ati_base,0x3df), ABS(0x3df-ati_base), 1) ) {
  65.             printf("IOPERM FAILED IN ATI\n");
  66.             exit(-2);
  67.     }
  68.  
  69.         return 1;
  70. }
  71.  
  72. /* Read and store chipset-specific registers */
  73. static int ati_saveregs(unsigned char regs[])
  74. {
  75.     outb(ati_base,0xb8);
  76.     regs[ATIREG(0xb8)]=inb(1+ati_base);
  77.  
  78.     if (ati_gate>ATI_18800) {
  79.         outb(ati_base,0xbe);
  80.         regs[ATIREG(0xbe)]=inb(1+ati_base);
  81.     }
  82.  
  83.     return 0;
  84. }
  85.  
  86. /* Set chipset-specific registers */
  87. static int ati_setregs( const unsigned char regs[], int mode )
  88. {
  89.     unsigned char c;
  90.  
  91.         if (mode==TEXT) {    /* Death request */
  92.                 outb(ati_base,0xb8);
  93.         outb(1+ati_base, regs[ATIREG(0xb8)]);
  94.  
  95.         if (ati_gate>ATI_18800) {
  96.                    outb(ati_base,0xbe);
  97.             outb(1+ati_base, regs[ATIREG(0xbe)]);
  98.         }
  99.     } else {
  100.                 /* Mess with the timings before passing it off to the vga driver */
  101.                 outb(ati_base,0xb8);
  102.         c = inb(1+ati_base) | 0x40;
  103.         outb(1+ati_base, c);
  104.  
  105.         if (ati_gate>ATI_18800) {
  106.                     outb(ati_base,0xbe);
  107.             c = inb(1+ati_base) | 0x10;
  108.             outb(1+ati_base, c);
  109.         }
  110.  
  111.         }
  112.     return 0;
  113. }
  114.  
  115. /* Fill in chipset specific mode information */
  116. static void ati_getmodeinfo( int mode, vga_modeinfo *modeinfo ) {
  117.     vga_driverspecs.getmodeinfo(mode, modeinfo);
  118. }
  119.  
  120.  
  121. /* Return non-zero if mode is available */
  122. static void ati_modeavailable( int mode ) {
  123.     vga_driverspecs.modeavailable(mode);
  124. }
  125.  
  126.  
  127. /* Set a mode */
  128. static int ati_setmode( int mode, int prv_mode ) {
  129.     const unsigned char *regs = NULL;
  130.     ati_setregs(regs, mode);
  131.     return vga_driverspecs.setmode(mode, prv_mode);
  132. }
  133.  
  134. /* Lifted from the genoa driver, maybe we should let everyone do the checks while we have
  135.  * the vbios mmapped */
  136. static unsigned char *map_vbios()
  137. {
  138.     unsigned char *vga_bios;
  139.  
  140.         /* Changed to use valloc(). */
  141.         if ((vga_bios = valloc(4096)) == NULL)
  142.         {
  143.                 fprintf (stderr, "svgalib: malloc error\n");
  144.                 exit (-1);
  145.         }
  146.  
  147.         vga_bios = (unsigned char *) mmap
  148.         (
  149.                 (caddr_t) vga_bios,
  150.                 4096,
  151.                 PROT_READ,
  152.                 MAP_SHARED | MAP_FIXED,
  153.                 __svgalib_mem_fd,
  154.                 0xc0000
  155.         );
  156.  
  157.         if ((long) vga_bios < 0)
  158.         {
  159.                 fprintf (stderr, "svgalib: mmap error\n");
  160.                 exit (-1);
  161.         }
  162.  
  163.     return vga_bios;
  164. }
  165.  
  166. static int ati_test()
  167. {
  168.         int  result;
  169.     unsigned char *vga_bios;
  170.  
  171.     vga_bios = map_vbios();
  172.  
  173.     if ( strncmp("761295520",(vga_bios+0x31),9)||     /* Identify as an ATI product */
  174.          strncmp("31",(vga_bios+0x40),2)) {           /* Identify as an ATI super vga (vs EGA/Basic-16) */
  175.         result=0;
  176.     } else {
  177.         result=1;
  178.  
  179.         memcpy(&ati_base,(vga_bios+0x10),2);
  180.  
  181.         switch (vga_bios[0x43]) {        /* Identify Gate revision */
  182.         case '1':
  183.             ati_gate = ATI_18800;
  184.             break;
  185.         case '2':
  186.             ati_gate = ATI_18800_1;
  187.             break;
  188.         case '3':
  189.             ati_gate = ATI_28800_2;
  190.             break;
  191.         case '4':
  192.             ati_gate = ATI_28800_4;
  193.             break;
  194.         case '5':
  195.             ati_gate = ATI_28800_5;
  196.             break;
  197.         case 'a':
  198.             ati_gate = ATI_68800;        /* Mach 32, should someone else handle this ? */
  199.             return 0;
  200.         default:
  201.             ati_gate = ATI_UNKNOWN;
  202.         }
  203.  
  204.         ati_flags_42 = vga_bios[0x42];
  205.         ati_flags_44 = vga_bios[0x44];
  206.         
  207.     }
  208.  
  209.     if (result)
  210.         result = ati_init(0,0,0);
  211.  
  212.         munmap ((caddr_t) vga_bios, 4096);
  213.  
  214.         return (result);
  215.  
  216. }
  217.  
  218.  
  219. /* Set display start */
  220. static void ati_setdisplaystart( int address ) {
  221.     vga_driverspecs.setdisplaystart(address);
  222. }
  223.  
  224.  
  225. /* Set logical scanline length (usually multiple of 8) */
  226. static void ati_setlogicalwidth( int width ) { 
  227.     vga_driverspecs.setlogicalwidth(width);
  228. }
  229.  
  230.  
  231. /* Function table (exported) */
  232. DriverSpecs ati_driverspecs = {
  233.         ati_saveregs,
  234.         ati_setregs,
  235.         nothing, /* unlock */
  236.         nothing, /* lock */
  237.         ati_test,
  238.         ati_init,
  239.         nothing, /* setpage */
  240.         nothing, /* setrdpage */
  241.         nothing, /* setwrpage */
  242.         ati_setmode,
  243.         ati_modeavailable,
  244.         ati_setdisplaystart,
  245.         ati_setlogicalwidth,
  246.         ati_getmodeinfo,
  247.     0,    /* bitblt */
  248.     0,    /* imageblt */
  249.     0,    /* fillblt */
  250.     0,    /* hlinelistblt */
  251.     0,    /* bltwait */
  252.     0,    /* extset */
  253.     0,
  254.     0,    /* linear */
  255.     NULL    /* accelspecs */
  256. };
  257.