1 /* $NetBSD: clock.c,v 1.1.1.1 1998/06/09 07:53:05 dbj Exp $ */ 2 /* 3 * Copyright (c) 1998 Darrin B. Jewell 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Darrin B. Jewell 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/kernel.h> 36 #include <sys/tty.h> 37 38 #include <machine/psl.h> 39 #include <machine/cpu.h> 40 41 #include <next68k/dev/clockreg.h> 42 43 /* @@@ This is pretty bogus and will need fixing once 44 * things are working better. 45 * -- jewell@mit.edu 46 */ 47 48 /* 49 * Note that the value of delay_divisor is roughly 50 * 2048 / cpuspeed (where cpuspeed is in MHz) on 68020 51 * and 68030 systems. See clock.c for the delay 52 * calibration algorithm. 53 */ 54 int cpuspeed; /* relative cpu speed; XXX skewed on 68040 */ 55 int delay_divisor = 2048/25; /* delay constant */ 56 57 /* 58 * Calibrate the delay constant. 59 */ 60 void 61 next68k_calibrate_delay() 62 { 63 extern int delay_divisor; 64 65 /* @@@ write this once we know how to read 66 * a real time clock 67 */ 68 69 /* 70 * Sanity check the delay_divisor value. If we totally lost, 71 * assume a 25MHz CPU; 72 */ 73 if (delay_divisor == 0) 74 delay_divisor = 2048 / 25; 75 76 /* Calculate CPU speed. */ 77 cpuspeed = 2048 / delay_divisor; 78 } 79 80 #define SECDAY (24 * 60 * 60) 81 #define SECYR (SECDAY * 365) 82 83 /* 84 * Set up the system's time, given a `reasonable' time value. 85 */ 86 void 87 inittodr(base) 88 time_t base; 89 { 90 int badbase = 0; 91 92 if (base < 5*SECYR) { 93 printf("WARNING: preposterous time in file system"); 94 base = 6*SECYR + 186*SECDAY + SECDAY/2; 95 badbase = 1; 96 } 97 98 if ((time.tv_sec = getsecs()) == 0) { 99 printf("WARNING: bad date in battery clock"); 100 /* 101 * Believe the time in the file system for lack of 102 * anything better, resetting the clock. 103 */ 104 time.tv_sec = base; 105 if (!badbase) 106 resettodr(); 107 } else { 108 int deltat = time.tv_sec - base; 109 110 if (deltat < 0) 111 deltat = -deltat; 112 if (deltat < 2 * SECDAY) 113 return; 114 printf("WARNING: clock %s %d days\n", 115 time.tv_sec < base ? "lost" : "gained", deltat / SECDAY); 116 } 117 } 118 119 void 120 resettodr() 121 { 122 setsecs(time.tv_sec); 123 } 124 125 int clock_intr __P((void *)); 126 127 int 128 clock_intr(arg) 129 void *arg; 130 { 131 if (!INTR_OCCURRED(NEXT_I_TIMER)) return(0); 132 133 { 134 volatile struct timer_reg *timer = IIOV(NEXT_P_TIMER); 135 timer->csr |= TIMER_UPDATE; 136 } 137 138 hardclock(arg); 139 140 return(1); 141 } 142 143 /* 144 * Set up the real-time and statistics clocks. Leave stathz 0 only 145 * if no alternative timer is available. 146 * 147 * The frequencies of these clocks must be an even number of microseconds. 148 */ 149 void 150 cpu_initclocks() 151 { 152 rtc_init(); 153 154 printf("WARNING: cpu_initclocks() is mostly not yet implemented.\n"); 155 156 hz = 100; 157 158 { 159 int s; 160 s = splclock(); 161 162 { 163 volatile struct timer_reg *timer = IIOV(NEXT_P_TIMER); 164 int cnt = 1000000/hz; /* usec timer */ 165 timer->csr = 0; 166 timer->msb = (cnt>>8); 167 timer->lsb = cnt; 168 timer->csr = TIMER_ENABLE|TIMER_UPDATE; 169 } 170 171 isrlink_autovec(clock_intr, NULL, NEXT_I_IPL(NEXT_I_TIMER), 0); 172 INTR_ENABLE(NEXT_I_TIMER); 173 174 splx(s); 175 } 176 } 177 178 179 void 180 setstatclockrate(newhz) 181 int newhz; 182 { 183 184 /* XXX should we do something here? XXX */ 185 } 186 187 /* @@@ update this to use the usec timer 188 * Darrin B Jewell <jewell@mit.edu> Sun Feb 8 05:01:02 1998 189 */ 190 191 192 /* 193 * Return the best possible estimate of the time in the timeval 194 * to which tvp points. We do this by returning the current time 195 * plus the amount of time since the last clock interrupt (clock.c:clkread). 196 * 197 * Check that this time is no less than any previously-reported time, 198 * which could happen around the time of a clock adjustment. Just for fun, 199 * we guarantee that the time will be greater than the value obtained by a 200 * previous call. 201 */ 202 203 void 204 microtime(tvp) 205 register struct timeval *tvp; 206 { 207 int s = splhigh(); 208 static struct timeval lasttime; 209 210 *tvp = time; 211 tvp->tv_usec; 212 while (tvp->tv_usec > 1000000) { 213 tvp->tv_sec++; 214 tvp->tv_usec -= 1000000; 215 } 216 if (tvp->tv_sec == lasttime.tv_sec && 217 tvp->tv_usec <= lasttime.tv_usec && 218 (tvp->tv_usec = lasttime.tv_usec + 1) > 1000000) { 219 tvp->tv_sec++; 220 tvp->tv_usec -= 1000000; 221 } 222 lasttime = *tvp; 223 splx(s); 224 } 225