home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / sys / i386 / isa / clock.c next >
Encoding:
C/C++ Source or Header  |  1991-05-12  |  5.3 KB  |  207 lines

  1. /*-
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * William Jolitz and Don Ahn.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  *
  36.  *    @(#)clock.c    7.2 (Berkeley) 5/12/91
  37.  */
  38.  
  39. /*
  40.  * Primitive clock interrupt routines.
  41.  */
  42. #include "param.h"
  43. #include "time.h"
  44. #include "kernel.h"
  45. #include "machine/segments.h"
  46. #include "i386/isa/icu.h"
  47. #include "i386/isa/isa.h"
  48. #include "i386/isa/rtc.h"
  49.  
  50. #define DAYST 119
  51. #define DAYEN 303
  52.  
  53. startrtclock() {
  54.     int s;
  55.  
  56.     /* initialize 8253 clock */
  57.     outb (IO_TIMER1+3, 0x36);
  58.     outb (IO_TIMER1, 1193182/hz);
  59.     outb (IO_TIMER1, (1193182/hz)/256);
  60.  
  61.     /* initialize brain-dead battery powered clock */
  62.     outb (IO_RTC, RTC_STATUSA);
  63.     outb (IO_RTC+1, 0x26);
  64.     outb (IO_RTC, RTC_STATUSB);
  65.     outb (IO_RTC+1, 2);
  66.  
  67.     outb (IO_RTC, RTC_DIAG);
  68.     if (s = inb (IO_RTC+1))
  69.         printf("RTC BIOS diagnostic error %b\n", s, RTCDG_BITS);
  70.     outb (IO_RTC, RTC_DIAG);
  71.     outb (IO_RTC+1, 0);
  72. }
  73.  
  74. /* convert 2 digit BCD number */
  75. bcd(i)
  76. int i;
  77. {
  78.     return ((i/16)*10 + (i%16));
  79. }
  80.  
  81. /* convert years to seconds (from 1970) */
  82. unsigned long
  83. ytos(y)
  84. int y;
  85. {
  86.     int i;
  87.     unsigned long ret;
  88.  
  89.     ret = 0; y = y - 70;
  90.     for(i=0;i<y;i++) {
  91.         if (i % 4) ret += 365*24*60*60;
  92.         else ret += 366*24*60*60;
  93.     }
  94.     return ret;
  95. }
  96.  
  97. /* convert months to seconds */
  98. unsigned long
  99. mtos(m,leap)
  100. int m,leap;
  101. {
  102.     int i;
  103.     unsigned long ret;
  104.  
  105.     ret = 0;
  106.     for(i=1;i<m;i++) {
  107.         switch(i){
  108.         case 1: case 3: case 5: case 7: case 8: case 10: case 12:
  109.             ret += 31*24*60*60; break;
  110.         case 4: case 6: case 9: case 11:
  111.             ret += 30*24*60*60; break;
  112.         case 2:
  113.             if (leap) ret += 29*24*60*60;
  114.             else ret += 28*24*60*60;
  115.         }
  116.     }
  117.     return ret;
  118. }
  119.  
  120.  
  121. /*
  122.  * Initialize the time of day register, based on the time base which is, e.g.
  123.  * from a filesystem.
  124.  */
  125. inittodr(base)
  126.     time_t base;
  127. {
  128.     unsigned long sec;
  129.     int leap,day_week,t,yd;
  130.     int sa,s;
  131.  
  132.     /* do we have a realtime clock present? (otherwise we loop below) */
  133.     sa = rtcin(RTC_STATUSA);
  134.     if (sa == 0xff || sa == 0) return;
  135.  
  136.     /* ready for a read? */
  137.     while ((sa&RTCSA_TUP) == RTCSA_TUP)
  138.         sa = rtcin(RTC_STATUSA);
  139.  
  140.     sec = bcd(rtcin(RTC_YEAR));
  141.     leap = !(sec % 4); sec += ytos(sec); /* year    */
  142.     yd = mtos(bcd(rtcin(RTC_MONTH)),leap); sec += yd;    /* month   */
  143.     t = (bcd(rtcin(RTC_DAY))-1) * 24*60*60; sec += t; yd += t; /* date    */
  144.     day_week = rtcin(RTC_WDAY);                /* day     */
  145.     sec += bcd(rtcin(RTC_HRS)) * 60*60;            /* hour    */
  146.     sec += bcd(rtcin(RTC_MIN)) * 60;            /* minutes */
  147.     sec += bcd(rtcin(RTC_SEC));                /* seconds */
  148.     sec -= 24*60*60; /* XXX why ??? */
  149.  
  150. #ifdef notdef
  151.     /* XXX off by one? Need to calculate DST on SUNDAY */
  152.     /* Perhaps we should have the RTC hold GMT time to save */
  153.     /* us the bother of converting. */
  154.     yd = yd / 24*60*60;
  155.     if ((yd >= DAYST) && ( yd <= DAYEN)) {
  156.         sec -= 60*60;
  157.     }
  158. #endif
  159.     sec += tz.tz_minuteswest * 60;
  160.  
  161.     time.tv_sec = sec;
  162. }
  163.  
  164. #ifdef garbage
  165. /*
  166.  * Initialze the time of day register, based on the time base which is, e.g.
  167.  * from a filesystem.
  168.  */
  169. test_inittodr(base)
  170.     time_t base;
  171. {
  172.  
  173.     outb(IO_RTC,9); /* year    */
  174.     printf("%d ",bcd(inb(IO_RTC+1)));
  175.     outb(IO_RTC,8); /* month   */
  176.     printf("%d ",bcd(inb(IO_RTC+1)));
  177.     outb(IO_RTC,7); /* day     */
  178.     printf("%d ",bcd(inb(IO_RTC+1)));
  179.     outb(IO_RTC,4); /* hour    */
  180.     printf("%d ",bcd(inb(IO_RTC+1)));
  181.     outb(IO_RTC,2); /* minutes */
  182.     printf("%d ",bcd(inb(IO_RTC+1)));
  183.     outb(IO_RTC,0); /* seconds */
  184.     printf("%d\n",bcd(inb(IO_RTC+1)));
  185.  
  186.     time.tv_sec = base;
  187. }
  188. #endif
  189.  
  190. /*
  191.  * Restart the clock.
  192.  */
  193. resettodr()
  194. {
  195. }
  196.  
  197. /*
  198.  * Wire clock interrupt in.
  199.  */
  200. #define V(s)    __CONCAT(V, s)
  201. extern V(clk)();
  202. enablertclock() {
  203.     INTREN(IRQ0);
  204.     setidt(ICU_OFFSET+0, &V(clk), SDT_SYS386IGT, SEL_KPL);
  205.     splnone();
  206. }
  207.