--- Net2/arch/i386/isa/clock.c 2018/04/24 18:04:01 1.1 +++ Net2/arch/i386/isa/clock.c 2018/04/24 18:11:58 1.1.1.2 @@ -34,29 +34,47 @@ * SUCH DAMAGE. * * @(#)clock.c 7.2 (Berkeley) 5/12/91 + * + * PATCHES MAGIC LEVEL PATCH THAT GOT US HERE + * -------------------- ----- ---------------------- + * CURRENT PATCH LEVEL: 3 00082 + * -------------------- ----- ---------------------- + * + * 14 Aug 92 Arne Henrik Juul Added code in the kernel to + * allow for DST in the BIOS. + * 17 Jan 93 Bruce Evans Fixed leap year and second + * calculations + * 01 Feb 93 Julian Elischer Added code to for the cpu + * speed independent spinwait() + * function, (used by scsi and others) */ /* * Primitive clock interrupt routines. */ #include "param.h" +#include "systm.h" #include "time.h" #include "kernel.h" #include "machine/segments.h" #include "i386/isa/icu.h" #include "i386/isa/isa.h" #include "i386/isa/rtc.h" +#include "i386/isa/timerreg.h" #define DAYST 119 #define DAYEN 303 +#define XTALSPEED 1193182 startrtclock() { int s; + findcpuspeed(); /* use the clock (while it's free) + to find the cpu speed */ /* initialize 8253 clock */ - outb (IO_TIMER1+3, 0x36); - outb (IO_TIMER1, 1193182/hz); - outb (IO_TIMER1, (1193182/hz)/256); + outb(TIMER_MODE, TIMER_SEL0|TIMER_RATEGEN|TIMER_16BIT); + outb (IO_TIMER1, XTALSPEED/hz); + outb (IO_TIMER1, (XTALSPEED/hz)/256); /* initialize brain-dead battery powered clock */ outb (IO_RTC, RTC_STATUSA); @@ -71,6 +89,32 @@ startrtclock() { outb (IO_RTC+1, 0); } +unsigned int delaycount; /* calibrated loop variable (1 millisecond) */ + +#define FIRST_GUESS 0x2000 +findcpuspeed() +{ + unsigned char low; + unsigned int remainder; + + /* Put counter in count down mode */ + outb(IO_TIMER1+3, 0x34); + outb(IO_TIMER1, 0xff); + outb(IO_TIMER1, 0xff); + delaycount = FIRST_GUESS; + spinwait(1); + /* Read the value left in the counter */ + low = inb(IO_TIMER1); /* least siginifcant */ + remainder = inb(IO_TIMER1); /* most significant */ + remainder = (remainder<<8) + low ; + /* Formula for delaycount is : + * (loopcount * timer clock speed)/ (counter ticks * 1000) + */ + delaycount = (FIRST_GUESS * (XTALSPEED/1000)) / (0xffff-remainder); +} + + + /* convert 2 digit BCD number */ bcd(i) int i; @@ -86,8 +130,8 @@ int y; int i; unsigned long ret; - ret = 0; y = y - 70; - for(i=0;i= DAYST) && ( yd <= DAYEN)) { sec -= 60*60; } -#endif sec += tz.tz_minuteswest * 60; time.tv_sec = sec; @@ -204,3 +247,17 @@ enablertclock() { setidt(ICU_OFFSET+0, &V(clk), SDT_SYS386IGT, SEL_KPL); splnone(); } + + + + +spinwait(millisecs) +int millisecs; /* number of milliseconds to delay */ +{ + int i, j; + + for (i=0;i