--- Net2/arch/i386/isa/clock.c 2018/04/24 18:04:01 1.1 +++ Net2/arch/i386/isa/clock.c 2018/04/24 18:19:59 1.1.1.3 @@ -33,30 +33,43 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * @(#)clock.c 7.2 (Berkeley) 5/12/91 + * from: @(#)clock.c 7.2 (Berkeley) 5/12/91 + * clock.c,v 1.11 1993/07/06 06:06:28 deraadt Exp */ /* * 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" +void spinwait __P((int)); + +/* XXX all timezone stuff should be moved out of the kernel */ +#if 1 #define DAYST 119 #define DAYEN 303 +#endif +void 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); + + /* Correct rounding will buy us a better precision in timekeeping */ + outb (IO_TIMER1, TIMER_DIV(hz)%256); + outb (IO_TIMER1, TIMER_DIV(hz)/256); /* initialize brain-dead battery powered clock */ outb (IO_RTC, RTC_STATUSA); @@ -71,6 +84,31 @@ 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 * TIMER_DIV(1000)) / (0xffff-remainder); +} + + /* convert 2 digit BCD number */ bcd(i) int i; @@ -86,8 +124,8 @@ int y; int i; unsigned long ret; - ret = 0; y = y - 70; - for(i=0;i= DAYST) && ( yd <= DAYEN)) { sec -= 60*60; } @@ -190,6 +229,7 @@ test_inittodr(base) /* * Restart the clock. */ +void resettodr() { } @@ -197,10 +237,20 @@ resettodr() /* * Wire clock interrupt in. */ -#define V(s) __CONCAT(V, s) -extern V(clk)(); +#define VEC(s) __CONCAT(X, s) +extern VEC(clk)(); +void enablertclock() { + setidt(ICU_OFFSET+0, &VEC(clk), SDT_SYS386IGT, SEL_KPL); INTREN(IRQ0); - setidt(ICU_OFFSET+0, &V(clk), SDT_SYS386IGT, SEL_KPL); - splnone(); +} + +/* + * Delay for some number of milliseconds. + */ +void +spinwait(millisecs) + int millisecs; +{ + DELAY(1000 * millisecs); }