1 /* $NetBSD: clock.c,v 1.19 1998/01/12 18:04:01 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1988 University of Utah. 5 * Copyright (c) 1982, 1990 The Regents of the University of California. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * the Systems Programming Group of the University of Utah Computer 10 * Science Department. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * from: Utah $Hdr: clock.c 1.18 91/01/21$ 41 * 42 * @(#)clock.c 7.6 (Berkeley) 5/7/91 43 */ 44 45 #include <sys/param.h> 46 #include <sys/kernel.h> 47 #include <sys/systm.h> 48 #include <sys/device.h> 49 #include <sys/uio.h> 50 #include <sys/conf.h> 51 52 #include <dev/clock_subr.h> 53 54 #include <machine/psl.h> 55 #include <machine/cpu.h> 56 #include <machine/iomap.h> 57 #include <machine/mfp.h> 58 #include <atari/dev/clockreg.h> 59 #include <atari/atari/device.h> 60 61 #if defined(GPROF) && defined(PROFTIMER) 62 #include <machine/profile.h> 63 #endif 64 65 /* 66 * The MFP clock runs at 2457600Hz. We use a {system,stat,prof}clock divider 67 * of 200. Therefore the timer runs at an effective rate of: 68 * 2457600/200 = 12288Hz. 69 */ 70 #define CLOCK_HZ 12288 71 72 /* 73 * Machine-dependent clock routines. 74 * 75 * Inittodr initializes the time of day hardware which provides 76 * date functions. 77 * 78 * Resettodr restores the time of day hardware after a time change. 79 */ 80 81 struct clock_softc { 82 struct device sc_dev; 83 int sc_flags; 84 }; 85 86 /* 87 * 'sc_flags' state info. Only used by the rtc-device functions. 88 */ 89 #define RTC_OPEN 1 90 91 /* {b,c}devsw[] function prototypes for rtc functions */ 92 dev_type_open(rtcopen); 93 dev_type_close(rtcclose); 94 dev_type_read(rtcread); 95 dev_type_write(rtcwrite); 96 97 static void clockattach __P((struct device *, struct device *, void *)); 98 static int clockmatch __P((struct device *, struct cfdata *, void *)); 99 100 struct cfattach clock_ca = { 101 sizeof(struct clock_softc), clockmatch, clockattach 102 }; 103 104 extern struct cfdriver clock_cd; 105 106 void statintr __P((struct clockframe)); 107 108 static u_long gettod __P((void)); 109 static int twodigits __P((char *, int)); 110 111 static int divisor; /* Systemclock divisor */ 112 113 /* 114 * Statistics and profile clock intervals and variances. Variance must 115 * be a power of 2. Since this gives us an even number, not an odd number, 116 * we discard one case and compensate. That is, a variance of 64 would 117 * give us offsets in [0..63]. Instead, we take offsets in [1..63]. 118 * This is symetric around the point 32, or statvar/2, and thus averages 119 * to that value (assuming uniform random numbers). 120 */ 121 #ifdef STATCLOCK 122 static int statvar = 32; /* {stat,prof}clock variance */ 123 static int statmin; /* statclock divisor - variance/2 */ 124 static int profmin; /* profclock divisor - variance/2 */ 125 static int clk2min; /* current, from above choises */ 126 #endif 127 128 int 129 clockmatch(pdp, cfp, auxp) 130 struct device *pdp; 131 struct cfdata *cfp; 132 void *auxp; 133 { 134 if (!atari_realconfig) { 135 /* 136 * Initialize Timer-B in the ST-MFP. This timer is used by 137 * the 'delay' function below. This timer is setup to be 138 * continueously counting from 255 back to zero at a 139 * frequency of 614400Hz. We do this *early* in the 140 * initialisation process. 141 */ 142 MFP->mf_tbcr = 0; /* Stop timer */ 143 MFP->mf_iera &= ~IA_TIMB; /* Disable timer interrupts */ 144 MFP->mf_tbdr = 0; 145 MFP->mf_tbcr = T_Q004; /* Start timer */ 146 147 /* 148 * Initialize the time structure 149 */ 150 time.tv_sec = 0; 151 time.tv_usec = 0; 152 153 return 0; 154 } 155 if(!strcmp("clock", auxp)) 156 return(1); 157 return(0); 158 } 159 160 /* 161 * Start the real-time clock. 162 */ 163 void clockattach(pdp, dp, auxp) 164 struct device *pdp, *dp; 165 void *auxp; 166 { 167 struct clock_softc *sc = (void *)dp; 168 169 sc->sc_flags = 0; 170 171 /* 172 * Initialize Timer-A in the ST-MFP. We use a divisor of 200. 173 * The MFP clock runs at 2457600Hz. Therefore the timer runs 174 * at an effective rate of: 2457600/200 = 12288Hz. The 175 * following expression works for 48, 64 or 96 hz. 176 */ 177 divisor = CLOCK_HZ/hz; 178 MFP->mf_tacr = 0; /* Stop timer */ 179 MFP->mf_iera &= ~IA_TIMA; /* Disable timer interrupts */ 180 MFP->mf_tadr = divisor; /* Set divisor */ 181 182 if (hz != 48 && hz != 64 && hz != 96) { /* XXX */ 183 printf (": illegal value %d for systemclock, reset to %d\n\t", 184 hz, 64); 185 hz = 64; 186 } 187 printf(": system hz %d timer-A divisor 200/%d\n", hz, divisor); 188 189 #ifdef STATCLOCK 190 if ((stathz == 0) || (stathz > hz) || (CLOCK_HZ % stathz)) 191 stathz = hz; 192 if ((profhz == 0) || (profhz > (hz << 1)) || (CLOCK_HZ % profhz)) 193 profhz = hz << 1; 194 195 MFP->mf_tcdcr &= 0x7; /* Stop timer */ 196 MFP->mf_ierb &= ~IB_TIMC; /* Disable timer inter. */ 197 MFP->mf_tcdr = CLOCK_HZ/stathz; /* Set divisor */ 198 199 statmin = (CLOCK_HZ/stathz) - (statvar >> 1); 200 profmin = (CLOCK_HZ/profhz) - (statvar >> 1); 201 clk2min = statmin; 202 #endif /* STATCLOCK */ 203 204 } 205 206 void cpu_initclocks() 207 { 208 MFP->mf_tacr = T_Q200; /* Start timer */ 209 MFP->mf_ipra &= ~IA_TIMA; /* Clear pending interrupts */ 210 MFP->mf_iera |= IA_TIMA; /* Enable timer interrupts */ 211 MFP->mf_imra |= IA_TIMA; /* ..... */ 212 213 #ifdef STATCLOCK 214 MFP->mf_tcdcr = (MFP->mf_tcdcr & 0x7) | (T_Q200<<4); /* Start */ 215 MFP->mf_iprb &= ~IB_TIMC; /* Clear pending interrupts */ 216 MFP->mf_ierb |= IB_TIMC; /* Enable timer interrupts */ 217 MFP->mf_imrb |= IB_TIMC; /* ..... */ 218 #endif /* STATCLOCK */ 219 } 220 221 void 222 setstatclockrate(newhz) 223 int newhz; 224 { 225 #ifdef STATCLOCK 226 if (newhz == stathz) 227 clk2min = statmin; 228 else clk2min = profmin; 229 #endif /* STATCLOCK */ 230 } 231 232 #ifdef STATCLOCK 233 void 234 statintr(frame) 235 struct clockframe frame; 236 { 237 register int var, r; 238 239 var = statvar - 1; 240 do { 241 r = random() & var; 242 } while(r == 0); 243 244 /* 245 * Note that we are always lagging behind as the new divisor 246 * value will not be loaded until the next interrupt. This 247 * shouldn't disturb the median frequency (I think ;-) ) as 248 * only the value used when switching frequencies is used 249 * twice. This shouldn't happen very often. 250 */ 251 MFP->mf_tcdr = clk2min + r; 252 253 statclock(&frame); 254 } 255 #endif /* STATCLOCK */ 256 257 /* 258 * Returns number of usec since last recorded clock "tick" 259 * (i.e. clock interrupt). 260 */ 261 long 262 clkread() 263 { 264 u_int delta; 265 266 delta = ((divisor - MFP->mf_tadr) * tick) / divisor; 267 /* 268 * Account for pending clock interrupts 269 */ 270 if(MFP->mf_iera & IA_TIMA) 271 return(delta + tick); 272 return(delta); 273 } 274 275 #define TIMB_FREQ 614400 276 #define TIMB_LIMIT 256 277 278 /* 279 * Wait "n" microseconds. 280 * Relies on MFP-Timer B counting down from TIMB_LIMIT at TIMB_FREQ Hz. 281 * Note: timer had better have been programmed before this is first used! 282 */ 283 void 284 delay(n) 285 int n; 286 { 287 int tick, otick; 288 289 /* 290 * Read the counter first, so that the rest of the setup overhead is 291 * counted. 292 */ 293 otick = MFP->mf_tbdr; 294 295 /* 296 * Calculate ((n * TIMER_FREQ) / 1e6) using explicit assembler code so 297 * we can take advantage of the intermediate 64-bit quantity to prevent 298 * loss of significance. 299 */ 300 n -= 5; 301 if(n < 0) 302 return; 303 { 304 u_int temp; 305 306 __asm __volatile ("mulul %2,%1:%0" : "=d" (n), "=d" (temp) 307 : "d" (TIMB_FREQ)); 308 __asm __volatile ("divul %1,%2:%0" : "=d" (n) 309 : "d"(1000000),"d"(temp),"0"(n)); 310 } 311 312 while(n > 0) { 313 tick = MFP->mf_tbdr; 314 if(tick > otick) 315 n -= TIMB_LIMIT - (tick - otick); 316 else n -= otick - tick; 317 otick = tick; 318 } 319 } 320 321 #ifdef GPROF 322 /* 323 * profclock() is expanded in line in lev6intr() unless profiling kernel. 324 * Assumes it is called with clock interrupts blocked. 325 */ 326 profclock(pc, ps) 327 caddr_t pc; 328 int ps; 329 { 330 /* 331 * Came from user mode. 332 * If this process is being profiled record the tick. 333 */ 334 if (USERMODE(ps)) { 335 if (p->p_stats.p_prof.pr_scale) 336 addupc(pc, &curproc->p_stats.p_prof, 1); 337 } 338 /* 339 * Came from kernel (supervisor) mode. 340 * If we are profiling the kernel, record the tick. 341 */ 342 else if (profiling < 2) { 343 register int s = pc - s_lowpc; 344 345 if (s < s_textsize) 346 kcount[s / (HISTFRACTION * sizeof (*kcount))]++; 347 } 348 /* 349 * Kernel profiling was on but has been disabled. 350 * Mark as no longer profiling kernel and if all profiling done, 351 * disable the clock. 352 */ 353 if (profiling && (profon & PRF_KERNEL)) { 354 profon &= ~PRF_KERNEL; 355 if (profon == PRF_NONE) 356 stopprofclock(); 357 } 358 } 359 #endif 360 361 /*********************************************************************** 362 * Real Time Clock support * 363 ***********************************************************************/ 364 365 u_int mc146818_read(rtc, regno) 366 void *rtc; 367 u_int regno; 368 { 369 ((struct rtc *)rtc)->rtc_regno = regno; 370 return(((struct rtc *)rtc)->rtc_data & 0377); 371 } 372 373 void mc146818_write(rtc, regno, value) 374 void *rtc; 375 u_int regno, value; 376 { 377 ((struct rtc *)rtc)->rtc_regno = regno; 378 ((struct rtc *)rtc)->rtc_data = value; 379 } 380 381 /* 382 * Initialize the time of day register, assuming the RTC runs in UTC. 383 * Since we've got the 'rtc' device, this functionality should be removed 384 * from the kernel. The only problem to be solved before that can happen 385 * is the possibility of init(1) providing a way (rc.boot?) to set 386 * the RTC before single-user mode is entered. 387 */ 388 void 389 inittodr(base) 390 time_t base; 391 { 392 /* Battery clock does not store usec's, so forget about it. */ 393 time.tv_sec = gettod(); 394 time.tv_usec = 0; 395 } 396 397 /* 398 * Function turned into a No-op. Use /dev/rtc to update the RTC. 399 */ 400 void 401 resettodr() 402 { 403 return; 404 } 405 406 static u_long 407 gettod() 408 { 409 int sps; 410 mc_todregs clkregs; 411 struct clock_ymdhms dt; 412 413 sps = splhigh(); 414 MC146818_GETTOD(RTC, &clkregs); 415 splx(sps); 416 417 if(clkregs[MC_SEC] > 59) 418 return(0); 419 if(clkregs[MC_MIN] > 59) 420 return(0); 421 if(clkregs[MC_HOUR] > 23) 422 return(0); 423 if(range_test(clkregs[MC_DOM], 1, 31)) 424 return(0); 425 if (range_test(clkregs[MC_MONTH], 1, 12)) 426 return(0); 427 if(clkregs[MC_YEAR] > (2000 - GEMSTARTOFTIME)) 428 return(0); 429 430 dt.dt_year = clkregs[MC_YEAR] + GEMSTARTOFTIME; 431 dt.dt_mon = clkregs[MC_MONTH]; 432 dt.dt_day = clkregs[MC_DOM]; 433 dt.dt_hour = clkregs[MC_HOUR]; 434 dt.dt_min = clkregs[MC_MIN]; 435 dt.dt_sec = clkregs[MC_SEC]; 436 437 return(clock_ymdhms_to_secs(&dt)); 438 } 439 /*********************************************************************** 440 * RTC-device support * 441 ***********************************************************************/ 442 int 443 rtcopen(dev, flag, mode, p) 444 dev_t dev; 445 int flag, mode; 446 struct proc *p; 447 { 448 int unit = minor(dev); 449 struct clock_softc *sc; 450 451 if (unit >= clock_cd.cd_ndevs) 452 return ENXIO; 453 sc = clock_cd.cd_devs[unit]; 454 if (!sc) 455 return ENXIO; 456 if (sc->sc_flags & RTC_OPEN) 457 return EBUSY; 458 459 sc->sc_flags = RTC_OPEN; 460 return 0; 461 } 462 463 int 464 rtcclose(dev, flag, mode, p) 465 dev_t dev; 466 int flag; 467 int mode; 468 struct proc *p; 469 { 470 int unit = minor(dev); 471 struct clock_softc *sc = clock_cd.cd_devs[unit]; 472 473 sc->sc_flags = 0; 474 return 0; 475 } 476 477 int 478 rtcread(dev, uio, flags) 479 dev_t dev; 480 struct uio *uio; 481 int flags; 482 { 483 struct clock_softc *sc; 484 mc_todregs clkregs; 485 int s, length; 486 char buffer[16]; 487 488 sc = clock_cd.cd_devs[minor(dev)]; 489 490 s = splhigh(); 491 MC146818_GETTOD(RTC, &clkregs); 492 splx(s); 493 494 sprintf(buffer, "%02d%02d%02d%02d%02d.%02d\n", 495 clkregs[MC_YEAR] + GEMSTARTOFTIME - 1900, 496 clkregs[MC_MONTH], clkregs[MC_DOM], 497 clkregs[MC_HOUR], clkregs[MC_MIN], clkregs[MC_SEC]); 498 499 if (uio->uio_offset > strlen(buffer)) 500 return 0; 501 502 length = strlen(buffer) - uio->uio_offset; 503 if (length > uio->uio_resid) 504 length = uio->uio_resid; 505 506 return(uiomove((caddr_t)buffer, length, uio)); 507 } 508 509 static int 510 twodigits(buffer, pos) 511 char *buffer; 512 int pos; 513 { 514 int result = 0; 515 516 if (buffer[pos] >= '0' && buffer[pos] <= '9') 517 result = (buffer[pos] - '0') * 10; 518 if (buffer[pos+1] >= '0' && buffer[pos+1] <= '9') 519 result += (buffer[pos+1] - '0'); 520 return(result); 521 } 522 523 int 524 rtcwrite(dev, uio, flags) 525 dev_t dev; 526 struct uio *uio; 527 int flags; 528 { 529 mc_todregs clkregs; 530 int s, length, error; 531 char buffer[14]; 532 533 /* 534 * We require atomic updates! 535 */ 536 length = uio->uio_resid; 537 if (uio->uio_offset || (length != sizeof(buffer) 538 && length != sizeof(buffer - 1))) 539 return(EINVAL); 540 541 if ((error = uiomove((caddr_t)buffer, sizeof(buffer), uio))) 542 return(error); 543 544 if (length == sizeof(buffer) && buffer[sizeof(buffer) - 1] != '\n') 545 return(EINVAL); 546 547 s = splclock(); 548 MC146818_GETTOD(RTC, &clkregs); 549 splx(s); 550 551 clkregs[MC_SEC] = twodigits(buffer, 11); 552 clkregs[MC_MIN] = twodigits(buffer, 8); 553 clkregs[MC_HOUR] = twodigits(buffer, 6); 554 clkregs[MC_DOM] = twodigits(buffer, 4); 555 clkregs[MC_MONTH] = twodigits(buffer, 2); 556 s = twodigits(buffer, 0); 557 s = (s < 70) ? s + 2000 : s + 1900; 558 clkregs[MC_YEAR] = s - GEMSTARTOFTIME; 559 560 s = splclock(); 561 MC146818_PUTTOD(RTC, &clkregs); 562 splx(s); 563 564 return(0); 565 } 566