home *** CD-ROM | disk | FTP | other *** search
/ Jason Aller Floppy Collection / 206.img / SCO386N3.TD0 / usr / sys / io / rtc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-07-26  |  4.5 KB  |  221 lines

  1. /*
  2.  *    %Z% %M% %I% %D% %Q%
  3.  *
  4.  *    Copyright (C) The Santa Cruz Operation, 1985, 1986.
  5.  *    This Module contains Proprietary Information of
  6.  *    The Santa Cruz Operation and should be treated as Confidential.
  7.  *
  8.  */
  9.  
  10. /*
  11.  * Real Time Clock driver for the SCO Generic 286 port.
  12.  *
  13.  * This driver is the real time clock interface into the
  14.  * Motorola MC146818 Real-time Clock/CMOS Ram Chip.
  15.  *
  16.  * The bytes in the RT/CMOS chip hold date entries in packed binary
  17.  * coded decimal form.  The driver interface to the user is via a
  18.  * ascii characters which are converted to/from packed bcd storage.
  19.  *
  20.  * cmos.h and rtc.h merged for linkkit purposes
  21.  */
  22.  
  23. #include "../h/param.h"
  24. #include "../h/types.h"
  25. #include "../h/sysmacros.h"
  26. #include "../h/signal.h"
  27. #include "../h/errno.h"
  28. #include "../h/dir.h"
  29. #include "../h/seg.h"
  30. #include "../h/page.h"
  31. #include "../h/user.h"
  32.  
  33. /* real time clock byte specification defines, used to be rtc.h */
  34.  
  35. #define    SECONDS        0x0
  36. #define    SECONDALARM    0x1
  37. #define    MINUTES        0x2
  38. #define    MINUTEALARM    0x3
  39. #define    HOURS        0x4
  40. #define    HOURALARM    0x5
  41. #define    DAYOFWEEK    0x6
  42. #define    DATEOFMONTH    0x7
  43. #define    MONTH        0x8
  44. #define    YEAR        0x9
  45. #define    STATUSA        0xA
  46. #define    STATUSB        0xB
  47. #define    STATUSC        0xC
  48. #define    STATUSD        0xD
  49.  
  50. /* packed binary coded decimal size of date string */
  51.  
  52. #define PCKBCDSIZ    5
  53.  
  54. /* character string size of date string */
  55.  
  56. #define CHRSTRSIZ    (PCKBCDSIZ * 2)
  57.  
  58. /* parts (in order) that make up the date string read/written by driver */
  59.  
  60. char dateparts[PCKBCDSIZ] = {
  61.     MONTH,
  62.     DATEOFMONTH,
  63.     HOURS,
  64.     MINUTES,
  65.     YEAR,
  66. };
  67.  
  68. /* real time clock status register A defines */
  69.  
  70. #define RTCUIP        0x80    /* update in progress */
  71.  
  72. /* real time clock status register B defines */
  73.  
  74. #define    CLKOFF        0x80    /* disable time updating */
  75. #define    MODE24        0x02    /* 24 hour mode */
  76. #define    DAYSAV        0x01    /* daylight savings time */
  77.  
  78. /* ascii defines */
  79.  
  80. #define ASCIIZERO    0x30
  81.  
  82. /* nibble from byte extraction */
  83.  
  84. #define LONIBBLE(ch)    ((ch) & 0xF)
  85. #define HINIBBLE(ch)    (((ch) >> 4) & 0xF)
  86.  
  87. /* bcd nibble packing */
  88.  
  89. #define PACKLOBCD(ch)    (((ch) - ASCIIZERO) & 0xF)
  90. #define PACKHIBCD(ch)    ((((ch) - ASCIIZERO) << 4) & 0xF0)
  91.  
  92. /* end of rtc.h definitions */
  93.  
  94. /* MOTOROLA MC146818 RealTime Clock/RAM System Information, ie cmos.h */
  95.  
  96. /* Ports for interacting with chip at */
  97.  
  98. #define CMOSADDR    0x70     /* Use to select RAM address */
  99. #define CMOSDATA    0x71    /* R/W data */
  100.  
  101. /* Number of cmos bytes */
  102.  
  103. #define CMOSSIZE    0x40    /* 64 addressable bytes in chip */
  104.  
  105. /* Addresses of interest */
  106.  
  107. #define CMOSFDT        0x10    /* Floppy Disk Type */
  108. #define    CMOSHDT        0x12    /* Hard Disk Type; bits 7-4 are 1st drive */
  109. #define CMOSEQP        0x14    /* Diskette, Video, and CoProcessor info */
  110.  
  111. /* Shifts of interest */
  112.  
  113. #define VID_SHFT    4    /* Shift display type bits into 0-3 */
  114.  
  115. /* end of cmos.h */
  116.  
  117. unsigned char rtcgetb();
  118.  
  119. /*
  120.  * rtcread - read real time clock.
  121.  */
  122. rtcread()
  123. {
  124.     register int i;
  125.     char datestr[CHRSTRSIZ];
  126.  
  127.     rtcgets(datestr);
  128.     while ((i = u.u_offset) < CHRSTRSIZ && u.u_count > 0) {
  129.         if (passc(datestr[i]) == -1)
  130.             return;
  131.     }
  132. }
  133.  
  134. /*
  135.  * rtcwrite - write real time clock.
  136.  */
  137. rtcwrite()
  138. {
  139.     register int i;
  140.     char datestr[CHRSTRSIZ];
  141.  
  142.     if (u.u_count < CHRSTRSIZ) {
  143.         u.u_error = EFAULT;
  144.         return;
  145.     }
  146.     for (i = 0; i < CHRSTRSIZ; i++) {
  147.         if ((datestr[i] = cpass()) == -1)
  148.             return;
  149.     }
  150.     rtcputs(datestr);
  151. }
  152.  
  153. /*
  154.  * rtcgets - real time clock get string.
  155.  *
  156.  * read the real time clock bcd chars and convert
  157.  * to an ascii string datestr.
  158.  */
  159. rtcgets(datestr)
  160. register char *datestr;
  161. {
  162.     register char    bcdch;
  163.     register int    i;
  164.  
  165.     for (i = 0; i < PCKBCDSIZ; i++) {
  166.         bcdch = rtcgetb(dateparts[i]);
  167.         *(datestr++) = HINIBBLE(bcdch) + ASCIIZERO;
  168.         *(datestr++) = LONIBBLE(bcdch) + ASCIIZERO;
  169.     }
  170. }
  171.  
  172. /*
  173.  * rtcputs - real time clock put string.
  174.  *
  175.  * take ascii string datestr, convert to packed bcd, and set the clock.
  176.  */
  177. rtcputs(datestr)
  178. char *datestr;
  179. {
  180.     register char    bcdch;
  181.     register int    i;
  182.  
  183.     rtcputb(STATUSB, CLKOFF|MODE24|DAYSAV);    /* stop the clock */
  184.  
  185.     for (i = 0; i < PCKBCDSIZ; i++) {
  186.         bcdch  = PACKHIBCD(*(datestr++));
  187.         bcdch |= PACKLOBCD(*(datestr++));
  188.         rtcputb(dateparts[i],bcdch);
  189.     }
  190.     rtcputb(STATUSB, MODE24|DAYSAV);        /* start the clock */
  191. }
  192.  
  193. /*
  194.  * rtcgetb - real time clock get byte.
  195.  *
  196.  * get a byte from the real time clock specified by rtcbyte.
  197.  */
  198. unsigned char
  199. rtcgetb(rtcbyte)
  200. unsigned char rtcbyte;
  201. {
  202.     do {
  203.         outb(CMOSADDR, STATUSA);
  204.     } while ((inb(CMOSDATA)) & RTCUIP);
  205.  
  206.     outb(CMOSADDR, rtcbyte);
  207.     return(inb(CMOSDATA));
  208. }
  209.  
  210. /*
  211.  * rtcputb - real time clock put byte.
  212.  *
  213.  * set the packed bcd byte "ch" specified by rtcbyte in the clock.
  214.  */
  215. rtcputb(rtcbyte, ch)
  216. unsigned char rtcbyte, ch;
  217. {
  218.     outb(CMOSADDR, rtcbyte);
  219.     outb(CMOSDATA, ch);
  220. }
  221.