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