pcrtc.c revision 130174
193023Snsouch/*- 293023Snsouch * Copyright (c) 1990 The Regents of the University of California. 393023Snsouch * All rights reserved. 493023Snsouch * 593023Snsouch * This code is derived from software contributed to Berkeley by 693023Snsouch * William Jolitz and Don Ahn. 793023Snsouch * 893023Snsouch * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * from: @(#)clock.c 7.2 (Berkeley) 5/12/91 33 * $FreeBSD: head/sys/pc98/cbus/pcrtc.c 130174 2004-06-07 06:04:27Z phk $ 34 */ 35 36/* 37 * Routines to handle clock hardware. 38 */ 39 40/* 41 * inittodr, settodr and support routines written 42 * by Christoph Robitschko <chmr@edvz.tu-graz.ac.at> 43 * 44 * reintroduced and updated by Chris Stenton <chris@gnome.co.uk> 8/10/94 45 */ 46 47/* 48 * modified for PC98 by Kakefuda 49 */ 50 51#include "opt_clock.h" 52#include "opt_isa.h" 53#include "opt_mca.h" 54 55#include <sys/param.h> 56#include <sys/systm.h> 57#include <sys/bus.h> 58#include <sys/limits.h> 59#include <sys/lock.h> 60#include <sys/module.h> 61#include <sys/mutex.h> 62#include <sys/proc.h> 63#include <sys/time.h> 64#include <sys/timetc.h> 65#include <sys/kernel.h> 66#include <sys/sysctl.h> 67#include <sys/cons.h> 68#include <sys/power.h> 69 70#include <machine/clock.h> 71#include <machine/cputypes.h> 72#include <machine/frame.h> 73#include <machine/intr_machdep.h> 74#include <machine/md_var.h> 75#include <machine/psl.h> 76 77#if defined(SMP) 78#include <machine/smp.h> 79#endif 80#include <machine/specialreg.h> 81 82#include <i386/isa/icu.h> 83#include <pc98/pc98/pc98.h> 84#include <pc98/pc98/pc98_machdep.h> 85#ifdef DEV_ISA 86#include <isa/isavar.h> 87#endif 88#include <i386/isa/timerreg.h> 89 90/* 91 * 32-bit time_t's can't reach leap years before 1904 or after 2036, so we 92 * can use a simple formula for leap years. 93 */ 94#define LEAPYEAR(y) (((u_int)(y) % 4 == 0) ? 1 : 0) 95#define DAYSPERYEAR (31+28+31+30+31+30+31+31+30+31+30+31) 96 97#define TIMER_DIV(x) ((timer_freq + (x) / 2) / (x)) 98 99#ifndef BURN_BRIDGES 100/* 101 * Time in timer cycles that it takes for microtime() to disable interrupts 102 * and latch the count. microtime() currently uses "cli; outb ..." so it 103 * normally takes less than 2 timer cycles. Add a few for cache misses. 104 * Add a few more to allow for latency in bogus calls to microtime() with 105 * interrupts already disabled. 106 */ 107#define TIMER0_LATCH_COUNT 20 108 109/* 110 * Maximum frequency that we are willing to allow for timer0. Must be 111 * low enough to guarantee that the timer interrupt handler returns 112 * before the next timer interrupt. 113 */ 114#define TIMER0_MAX_FREQ 20000 115#endif 116 117int adjkerntz; /* local offset from GMT in seconds */ 118int clkintr_pending; 119int disable_rtc_set; /* disable resettodr() if != 0 */ 120int pscnt = 1; 121int psdiv = 1; 122int statclock_disable; 123#ifndef TIMER_FREQ 124#define TIMER_FREQ 2457600 125#endif 126u_int timer_freq = TIMER_FREQ; 127int timer0_max_count; 128int wall_cmos_clock; /* wall CMOS clock assumed if != 0 */ 129struct mtx clock_lock; 130 131static int beeping = 0; 132static const u_char daysinmonth[] = {31,28,31,30,31,30,31,31,30,31,30,31}; 133static u_int hardclock_max_count; 134static u_int32_t i8254_lastcount; 135static u_int32_t i8254_offset; 136static int i8254_ticked; 137static struct intsrc *i8254_intsrc; 138#ifndef BURN_BRIDGES 139/* 140 * XXX new_function and timer_func should not handle clockframes, but 141 * timer_func currently needs to hold hardclock to handle the 142 * timer0_state == 0 case. We should use inthand_add()/inthand_remove() 143 * to switch between clkintr() and a slightly different timerintr(). 144 */ 145static void (*new_function)(struct clockframe *frame); 146static u_int new_rate; 147static u_int timer0_prescaler_count; 148static u_char timer0_state; 149#endif 150 151/* Values for timerX_state: */ 152#define RELEASED 0 153#define RELEASE_PENDING 1 154#define ACQUIRED 2 155#define ACQUIRE_PENDING 3 156 157static u_char timer1_state; 158static u_char timer2_state; 159static void (*timer_func)(struct clockframe *frame) = hardclock; 160static void rtc_serialcombit(int); 161static void rtc_serialcom(int); 162static int rtc_inb(void); 163static void rtc_outb(int); 164 165static unsigned i8254_get_timecount(struct timecounter *tc); 166static void set_timer_freq(u_int freq, int intr_freq); 167 168static struct timecounter i8254_timecounter = { 169 i8254_get_timecount, /* get_timecount */ 170 0, /* no poll_pps */ 171 ~0u, /* counter_mask */ 172 0, /* frequency */ 173 "i8254", /* name */ 174 0 /* quality */ 175}; 176 177static void 178clkintr(struct clockframe *frame) 179{ 180 181 if (timecounter->tc_get_timecount == i8254_get_timecount) { 182 mtx_lock_spin(&clock_lock); 183 if (i8254_ticked) 184 i8254_ticked = 0; 185 else { 186 i8254_offset += timer0_max_count; 187 i8254_lastcount = 0; 188 } 189 clkintr_pending = 0; 190 mtx_unlock_spin(&clock_lock); 191 } 192 timer_func(frame); 193#ifdef SMP 194 if (timer_func == hardclock) 195 forward_hardclock(); 196#endif 197#ifndef BURN_BRIDGES 198 switch (timer0_state) { 199 200 case RELEASED: 201 break; 202 203 case ACQUIRED: 204 if ((timer0_prescaler_count += timer0_max_count) 205 >= hardclock_max_count) { 206 timer0_prescaler_count -= hardclock_max_count; 207 hardclock(frame); 208#ifdef SMP 209 forward_hardclock(); 210#endif 211 } 212 break; 213 214 case ACQUIRE_PENDING: 215 mtx_lock_spin(&clock_lock); 216 i8254_offset = i8254_get_timecount(NULL); 217 i8254_lastcount = 0; 218 timer0_max_count = TIMER_DIV(new_rate); 219 outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT); 220 outb(TIMER_CNTR0, timer0_max_count & 0xff); 221 outb(TIMER_CNTR0, timer0_max_count >> 8); 222 mtx_unlock_spin(&clock_lock); 223 timer_func = new_function; 224 timer0_state = ACQUIRED; 225 break; 226 227 case RELEASE_PENDING: 228 if ((timer0_prescaler_count += timer0_max_count) 229 >= hardclock_max_count) { 230 mtx_lock_spin(&clock_lock); 231 i8254_offset = i8254_get_timecount(NULL); 232 i8254_lastcount = 0; 233 timer0_max_count = hardclock_max_count; 234 outb(TIMER_MODE, 235 TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT); 236 outb(TIMER_CNTR0, timer0_max_count & 0xff); 237 outb(TIMER_CNTR0, timer0_max_count >> 8); 238 mtx_unlock_spin(&clock_lock); 239 timer0_prescaler_count = 0; 240 timer_func = hardclock; 241 timer0_state = RELEASED; 242 hardclock(frame); 243#ifdef SMP 244 forward_hardclock(); 245#endif 246 } 247 break; 248 } 249#endif 250} 251 252#ifndef BURN_BRIDGES 253/* 254 * The acquire and release functions must be called at ipl >= splclock(). 255 */ 256int 257acquire_timer0(int rate, void (*function)(struct clockframe *frame)) 258{ 259 static int old_rate; 260 261 if (rate <= 0 || rate > TIMER0_MAX_FREQ) 262 return (-1); 263 switch (timer0_state) { 264 265 case RELEASED: 266 timer0_state = ACQUIRE_PENDING; 267 break; 268 269 case RELEASE_PENDING: 270 if (rate != old_rate) 271 return (-1); 272 /* 273 * The timer has been released recently, but is being 274 * re-acquired before the release completed. In this 275 * case, we simply reclaim it as if it had not been 276 * released at all. 277 */ 278 timer0_state = ACQUIRED; 279 break; 280 281 default: 282 return (-1); /* busy */ 283 } 284 new_function = function; 285 old_rate = new_rate = rate; 286 return (0); 287} 288#endif 289 290int 291acquire_timer1(int mode) 292{ 293 294 if (timer1_state != RELEASED) 295 return (-1); 296 timer1_state = ACQUIRED; 297 298 /* 299 * This access to the timer registers is as atomic as possible 300 * because it is a single instruction. We could do better if we 301 * knew the rate. Use of splclock() limits glitches to 10-100us, 302 * and this is probably good enough for timer2, so we aren't as 303 * careful with it as with timer0. 304 */ 305 outb(TIMER_MODE, TIMER_SEL1 | (mode & 0x3f)); 306 307 return (0); 308} 309 310int 311acquire_timer2(int mode) 312{ 313 314 if (timer2_state != RELEASED) 315 return (-1); 316 timer2_state = ACQUIRED; 317 318 /* 319 * This access to the timer registers is as atomic as possible 320 * because it is a single instruction. We could do better if we 321 * knew the rate. Use of splclock() limits glitches to 10-100us, 322 * and this is probably good enough for timer2, so we aren't as 323 * careful with it as with timer0. 324 */ 325 outb(TIMER_MODE, TIMER_SEL2 | (mode & 0x3f)); 326 327 return (0); 328} 329 330#ifndef BURN_BRIDGES 331int 332release_timer0() 333{ 334 switch (timer0_state) { 335 336 case ACQUIRED: 337 timer0_state = RELEASE_PENDING; 338 break; 339 340 case ACQUIRE_PENDING: 341 /* Nothing happened yet, release quickly. */ 342 timer0_state = RELEASED; 343 break; 344 345 default: 346 return (-1); 347 } 348 return (0); 349} 350#endif 351 352int 353release_timer1() 354{ 355 356 if (timer1_state != ACQUIRED) 357 return (-1); 358 timer1_state = RELEASED; 359 outb(TIMER_MODE, TIMER_SEL1 | TIMER_SQWAVE | TIMER_16BIT); 360 return (0); 361} 362 363int 364release_timer2() 365{ 366 367 if (timer2_state != ACQUIRED) 368 return (-1); 369 timer2_state = RELEASED; 370 outb(TIMER_MODE, TIMER_SEL2 | TIMER_SQWAVE | TIMER_16BIT); 371 return (0); 372} 373 374 375static int 376getit(void) 377{ 378 int high, low; 379 380 mtx_lock_spin(&clock_lock); 381 382 /* Select timer0 and latch counter value. */ 383 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH); 384 385 low = inb(TIMER_CNTR0); 386 high = inb(TIMER_CNTR0); 387 388 mtx_unlock_spin(&clock_lock); 389 return ((high << 8) | low); 390} 391 392/* 393 * Wait "n" microseconds. 394 * Relies on timer 1 counting down from (timer_freq / hz) 395 * Note: timer had better have been programmed before this is first used! 396 */ 397void 398DELAY(int n) 399{ 400 int delta, prev_tick, tick, ticks_left; 401 402#ifdef DELAYDEBUG 403 int getit_calls = 1; 404 int n1; 405 static int state = 0; 406 407 if (state == 0) { 408 state = 1; 409 for (n1 = 1; n1 <= 10000000; n1 *= 10) 410 DELAY(n1); 411 state = 2; 412 } 413 if (state == 1) 414 printf("DELAY(%d)...", n); 415#endif 416 /* 417 * Guard against the timer being uninitialized if we are called 418 * early for console i/o. 419 */ 420 if (timer0_max_count == 0) 421 set_timer_freq(timer_freq, hz); 422 423 /* 424 * Read the counter first, so that the rest of the setup overhead is 425 * counted. Guess the initial overhead is 20 usec (on most systems it 426 * takes about 1.5 usec for each of the i/o's in getit(). The loop 427 * takes about 6 usec on a 486/33 and 13 usec on a 386/20. The 428 * multiplications and divisions to scale the count take a while). 429 * 430 * However, if ddb is active then use a fake counter since reading 431 * the i8254 counter involves acquiring a lock. ddb must not go 432 * locking for many reasons, but it calls here for at least atkbd 433 * input. 434 */ 435#ifdef DDB 436 if (db_active) 437 prev_tick = 0; 438 else 439#endif 440 prev_tick = getit(); 441 n -= 0; /* XXX actually guess no initial overhead */ 442 /* 443 * Calculate (n * (timer_freq / 1e6)) without using floating point 444 * and without any avoidable overflows. 445 */ 446 if (n <= 0) 447 ticks_left = 0; 448 else if (n < 256) 449 /* 450 * Use fixed point to avoid a slow division by 1000000. 451 * 39099 = 1193182 * 2^15 / 10^6 rounded to nearest. 452 * 2^15 is the first power of 2 that gives exact results 453 * for n between 0 and 256. 454 */ 455 ticks_left = ((u_int)n * 39099 + (1 << 15) - 1) >> 15; 456 else 457 /* 458 * Don't bother using fixed point, although gcc-2.7.2 459 * generates particularly poor code for the long long 460 * division, since even the slow way will complete long 461 * before the delay is up (unless we're interrupted). 462 */ 463 ticks_left = ((u_int)n * (long long)timer_freq + 999999) 464 / 1000000; 465 466 while (ticks_left > 0) { 467#ifdef DDB 468 if (db_active) { 469 outb(0x5f, 0); 470 tick = prev_tick + 1; 471 } else 472#endif 473 tick = getit(); 474#ifdef DELAYDEBUG 475 ++getit_calls; 476#endif 477 delta = prev_tick - tick; 478 prev_tick = tick; 479 if (delta < 0) { 480 delta += timer0_max_count; 481 /* 482 * Guard against timer0_max_count being wrong. 483 * This shouldn't happen in normal operation, 484 * but it may happen if set_timer_freq() is 485 * traced. 486 */ 487 if (delta < 0) 488 delta = 0; 489 } 490 ticks_left -= delta; 491 } 492#ifdef DELAYDEBUG 493 if (state == 1) 494 printf(" %d calls to getit() at %d usec each\n", 495 getit_calls, (n + 5) / getit_calls); 496#endif 497} 498 499static void 500sysbeepstop(void *chan) 501{ 502 outb(IO_PPI, inb(IO_PPI)|0x08); /* disable counter1 output to speaker */ 503 release_timer1(); 504 beeping = 0; 505} 506 507int 508sysbeep(int pitch, int period) 509{ 510 int x = splclock(); 511 512 if (acquire_timer1(TIMER_SQWAVE|TIMER_16BIT)) 513 if (!beeping) { 514 /* Something else owns it. */ 515 splx(x); 516 return (-1); /* XXX Should be EBUSY, but nobody cares anyway. */ 517 } 518 disable_intr(); 519 outb(0x3fdb, pitch); 520 outb(0x3fdb, (pitch>>8)); 521 enable_intr(); 522 if (!beeping) { 523 /* enable counter1 output to speaker */ 524 outb(IO_PPI, (inb(IO_PPI) & 0xf7)); 525 beeping = period; 526 timeout(sysbeepstop, (void *)NULL, period); 527 } 528 splx(x); 529 return (0); 530} 531 532 533unsigned int delaycount; 534#define FIRST_GUESS 0x2000 535static void findcpuspeed(void) 536{ 537 int i; 538 int remainder; 539 540 /* Put counter in count down mode */ 541 outb(TIMER_MODE, TIMER_SEL0 | TIMER_16BIT | TIMER_RATEGEN); 542 outb(TIMER_CNTR0, 0xff); 543 outb(TIMER_CNTR0, 0xff); 544 for (i = FIRST_GUESS; i; i--) 545 ; 546 remainder = getit(); 547 delaycount = (FIRST_GUESS * TIMER_DIV(1000)) / (0xffff - remainder); 548} 549 550static u_int 551calibrate_clocks(void) 552{ 553 int timeout; 554 u_int count, prev_count, tot_count; 555 u_short sec, start_sec; 556 557 if (bootverbose) 558 printf("Calibrating clock(s) ... "); 559 /* Check ARTIC. */ 560 if (!(PC98_SYSTEM_PARAMETER(0x458) & 0x80) && 561 !(PC98_SYSTEM_PARAMETER(0x45b) & 0x04)) 562 goto fail; 563 timeout = 100000000; 564 565 /* Read the ARTIC. */ 566 sec = inw(0x5e); 567 568 /* Wait for the ARTIC to changes. */ 569 start_sec = sec; 570 for (;;) { 571 sec = inw(0x5e); 572 if (sec != start_sec) 573 break; 574 if (--timeout == 0) 575 goto fail; 576 } 577 prev_count = getit(); 578 if (prev_count == 0 || prev_count > timer0_max_count) 579 goto fail; 580 tot_count = 0; 581 582 start_sec = sec; 583 for (;;) { 584 sec = inw(0x5e); 585 count = getit(); 586 if (count == 0 || count > timer0_max_count) 587 goto fail; 588 if (count > prev_count) 589 tot_count += prev_count - (count - timer0_max_count); 590 else 591 tot_count += prev_count - count; 592 prev_count = count; 593 if ((sec == start_sec + 1200) || /* 1200 = 307.2KHz >> 8 */ 594 (sec < start_sec && 595 (u_int)sec + 0x10000 == (u_int)start_sec + 1200)) 596 break; 597 if (--timeout == 0) 598 goto fail; 599 } 600 601 if (bootverbose) { 602 printf("i8254 clock: %u Hz\n", tot_count); 603 } 604 return (tot_count); 605 606fail: 607 if (bootverbose) 608 printf("failed, using default i8254 clock of %u Hz\n", 609 timer_freq); 610 return (timer_freq); 611} 612 613static void 614set_timer_freq(u_int freq, int intr_freq) 615{ 616 int new_timer0_max_count; 617 618 mtx_lock_spin(&clock_lock); 619 timer_freq = freq; 620 new_timer0_max_count = hardclock_max_count = TIMER_DIV(intr_freq); 621 if (new_timer0_max_count != timer0_max_count) { 622 timer0_max_count = new_timer0_max_count; 623 outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT); 624 outb(TIMER_CNTR0, timer0_max_count & 0xff); 625 outb(TIMER_CNTR0, timer0_max_count >> 8); 626 } 627 mtx_unlock_spin(&clock_lock); 628} 629 630static void 631i8254_restore(void) 632{ 633 634 mtx_lock_spin(&clock_lock); 635 outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT); 636 outb(TIMER_CNTR0, timer0_max_count & 0xff); 637 outb(TIMER_CNTR0, timer0_max_count >> 8); 638 mtx_unlock_spin(&clock_lock); 639} 640 641 642/* 643 * Restore all the timers non-atomically (XXX: should be atomically). 644 * 645 * This function is called from pmtimer_resume() to restore all the timers. 646 * This should not be necessary, but there are broken laptops that do not 647 * restore all the timers on resume. 648 */ 649void 650timer_restore(void) 651{ 652 653 i8254_restore(); /* restore timer_freq and hz */ 654} 655 656/* 657 * Initialize 8254 timer 0 early so that it can be used in DELAY(). 658 * XXX initialization of other timers is unintentionally left blank. 659 */ 660void 661startrtclock() 662{ 663 u_int delta, freq; 664 665 findcpuspeed(); 666 if (pc98_machine_type & M_8M) 667 timer_freq = 1996800L; /* 1.9968 MHz */ 668 else 669 timer_freq = 2457600L; /* 2.4576 MHz */ 670 671 set_timer_freq(timer_freq, hz); 672 freq = calibrate_clocks(); 673#ifdef CLK_CALIBRATION_LOOP 674 if (bootverbose) { 675 printf( 676 "Press a key on the console to abort clock calibration\n"); 677 while (cncheckc() == -1) 678 calibrate_clocks(); 679 } 680#endif 681 682 /* 683 * Use the calibrated i8254 frequency if it seems reasonable. 684 * Otherwise use the default, and don't use the calibrated i586 685 * frequency. 686 */ 687 delta = freq > timer_freq ? freq - timer_freq : timer_freq - freq; 688 if (delta < timer_freq / 100) { 689#ifndef CLK_USE_I8254_CALIBRATION 690 if (bootverbose) 691 printf( 692"CLK_USE_I8254_CALIBRATION not specified - using default frequency\n"); 693 freq = timer_freq; 694#endif 695 timer_freq = freq; 696 } else { 697 if (bootverbose) 698 printf( 699 "%d Hz differs from default of %d Hz by more than 1%%\n", 700 freq, timer_freq); 701 } 702 703 set_timer_freq(timer_freq, hz); 704 i8254_timecounter.tc_frequency = timer_freq; 705 tc_init(&i8254_timecounter); 706 707 init_TSC(); 708} 709 710static void 711rtc_serialcombit(int i) 712{ 713 outb(IO_RTC, ((i&0x01)<<5)|0x07); 714 DELAY(1); 715 outb(IO_RTC, ((i&0x01)<<5)|0x17); 716 DELAY(1); 717 outb(IO_RTC, ((i&0x01)<<5)|0x07); 718 DELAY(1); 719} 720 721static void 722rtc_serialcom(int i) 723{ 724 rtc_serialcombit(i&0x01); 725 rtc_serialcombit((i&0x02)>>1); 726 rtc_serialcombit((i&0x04)>>2); 727 rtc_serialcombit((i&0x08)>>3); 728 outb(IO_RTC, 0x07); 729 DELAY(1); 730 outb(IO_RTC, 0x0f); 731 DELAY(1); 732 outb(IO_RTC, 0x07); 733 DELAY(1); 734} 735 736static void 737rtc_outb(int val) 738{ 739 int s; 740 int sa = 0; 741 742 for (s=0;s<8;s++) { 743 sa = ((val >> s) & 0x01) ? 0x27 : 0x07; 744 outb(IO_RTC, sa); /* set DI & CLK 0 */ 745 DELAY(1); 746 outb(IO_RTC, sa | 0x10); /* CLK 1 */ 747 DELAY(1); 748 } 749 outb(IO_RTC, sa & 0xef); /* CLK 0 */ 750} 751 752static int 753rtc_inb(void) 754{ 755 int s; 756 int sa = 0; 757 758 for (s=0;s<8;s++) { 759 sa |= ((inb(0x33) & 0x01) << s); 760 outb(IO_RTC, 0x17); /* CLK 1 */ 761 DELAY(1); 762 outb(IO_RTC, 0x07); /* CLK 0 */ 763 DELAY(2); 764 } 765 return sa; 766} 767 768/* 769 * Initialize the time of day register, based on the time base which is, e.g. 770 * from a filesystem. 771 */ 772void 773inittodr(time_t base) 774{ 775 unsigned long sec, days; 776 int year, month; 777 int y, m, s; 778 struct timespec ts; 779 int second, min, hour; 780 781 if (base) { 782 s = splclock(); 783 ts.tv_sec = base; 784 ts.tv_nsec = 0; 785 tc_setclock(&ts); 786 splx(s); 787 } 788 789 rtc_serialcom(0x03); /* Time Read */ 790 rtc_serialcom(0x01); /* Register shift command. */ 791 DELAY(20); 792 793 second = bcd2bin(rtc_inb() & 0xff); /* sec */ 794 min = bcd2bin(rtc_inb() & 0xff); /* min */ 795 hour = bcd2bin(rtc_inb() & 0xff); /* hour */ 796 days = bcd2bin(rtc_inb() & 0xff) - 1; /* date */ 797 798 month = (rtc_inb() >> 4) & 0x0f; /* month */ 799 for (m = 1; m < month; m++) 800 days += daysinmonth[m-1]; 801 year = bcd2bin(rtc_inb() & 0xff) + 1900; /* year */ 802 /* 2000 year problem */ 803 if (year < 1995) 804 year += 100; 805 if (year < 1970) 806 goto wrong_time; 807 for (y = 1970; y < year; y++) 808 days += DAYSPERYEAR + LEAPYEAR(y); 809 if ((month > 2) && LEAPYEAR(year)) 810 days ++; 811 sec = ((( days * 24 + 812 hour) * 60 + 813 min) * 60 + 814 second); 815 /* sec now contains the number of seconds, since Jan 1 1970, 816 in the local time zone */ 817 818 s = splhigh(); 819 820 sec += tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0); 821 822 y = time_second - sec; 823 if (y <= -2 || y >= 2) { 824 /* badly off, adjust it */ 825 ts.tv_sec = sec; 826 ts.tv_nsec = 0; 827 tc_setclock(&ts); 828 } 829 splx(s); 830 return; 831 832wrong_time: 833 printf("Invalid time in real time clock.\n"); 834 printf("Check and reset the date immediately!\n"); 835} 836 837/* 838 * Write system time back to RTC 839 */ 840void 841resettodr() 842{ 843 unsigned long tm; 844 int y, m, s; 845 int wd; 846 847 if (disable_rtc_set) 848 return; 849 850 s = splclock(); 851 tm = time_second; 852 splx(s); 853 854 rtc_serialcom(0x01); /* Register shift command. */ 855 856 /* Calculate local time to put in RTC */ 857 858 tm -= tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0); 859 860 rtc_outb(bin2bcd(tm%60)); tm /= 60; /* Write back Seconds */ 861 rtc_outb(bin2bcd(tm%60)); tm /= 60; /* Write back Minutes */ 862 rtc_outb(bin2bcd(tm%24)); tm /= 24; /* Write back Hours */ 863 864 /* We have now the days since 01-01-1970 in tm */ 865 wd = (tm + 4) % 7 + 1; /* Write back Weekday */ 866 for (y = 1970, m = DAYSPERYEAR + LEAPYEAR(y); 867 tm >= m; 868 y++, m = DAYSPERYEAR + LEAPYEAR(y)) 869 tm -= m; 870 871 /* Now we have the years in y and the day-of-the-year in tm */ 872 for (m = 0; ; m++) { 873 int ml; 874 875 ml = daysinmonth[m]; 876 if (m == 1 && LEAPYEAR(y)) 877 ml++; 878 if (tm < ml) 879 break; 880 tm -= ml; 881 } 882 883 m++; 884 rtc_outb(bin2bcd(tm+1)); /* Write back Day */ 885 rtc_outb((m << 4) | wd); /* Write back Month & Weekday */ 886 rtc_outb(bin2bcd(y%100)); /* Write back Year */ 887 888 rtc_serialcom(0x02); /* Time set & Counter hold command. */ 889 rtc_serialcom(0x00); /* Register hold command. */ 890} 891 892 893/* 894 * Start both clocks running. 895 */ 896void 897cpu_initclocks() 898{ 899 900 /* Finish initializing 8254 timer 0. */ 901 intr_add_handler("clk", 0, (driver_intr_t *)clkintr, NULL, 902 INTR_TYPE_CLK | INTR_FAST, NULL); 903 904 init_TSC_tc(); 905} 906 907void 908cpu_startprofclock(void) 909{ 910} 911 912void 913cpu_stopprofclock(void) 914{ 915} 916 917static int 918sysctl_machdep_i8254_freq(SYSCTL_HANDLER_ARGS) 919{ 920 int error; 921 u_int freq; 922 923 /* 924 * Use `i8254' instead of `timer' in external names because `timer' 925 * is is too generic. Should use it everywhere. 926 */ 927 freq = timer_freq; 928 error = sysctl_handle_int(oidp, &freq, sizeof(freq), req); 929 if (error == 0 && req->newptr != NULL) { 930#ifndef BURN_BRIDGES 931 if (timer0_state != RELEASED) 932 return (EBUSY); /* too much trouble to handle */ 933#endif 934 set_timer_freq(freq, hz); 935 i8254_timecounter.tc_frequency = freq; 936 } 937 return (error); 938} 939 940SYSCTL_PROC(_machdep, OID_AUTO, i8254_freq, CTLTYPE_INT | CTLFLAG_RW, 941 0, sizeof(u_int), sysctl_machdep_i8254_freq, "IU", ""); 942 943static unsigned 944i8254_get_timecount(struct timecounter *tc) 945{ 946 u_int count; 947 u_int high, low; 948 u_int eflags; 949 950 eflags = read_eflags(); 951 mtx_lock_spin(&clock_lock); 952 953 /* Select timer0 and latch counter value. */ 954 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH); 955 956 low = inb(TIMER_CNTR0); 957 high = inb(TIMER_CNTR0); 958 count = timer0_max_count - ((high << 8) | low); 959 if (count < i8254_lastcount || 960 (!i8254_ticked && (clkintr_pending || 961 ((count < 20 || (!(eflags & PSL_I) && count < timer0_max_count / 2u)) && 962 i8254_intsrc != NULL && 963 i8254_intsrc->is_pic->pic_source_pending(i8254_intsrc))))) { 964 i8254_ticked = 1; 965 i8254_offset += timer0_max_count; 966 } 967 i8254_lastcount = count; 968 count += i8254_offset; 969 mtx_unlock_spin(&clock_lock); 970 return (count); 971} 972 973#ifdef DEV_ISA 974/* 975 * Attach to the ISA PnP descriptors for the timer and realtime clock. 976 */ 977static struct isa_pnp_id attimer_ids[] = { 978 { 0x0001d041 /* PNP0100 */, "AT timer" }, 979 { 0x000bd041 /* PNP0B00 */, "AT realtime clock" }, 980 { 0 } 981}; 982 983static int 984attimer_probe(device_t dev) 985{ 986 int result; 987 988 if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, attimer_ids)) <= 0) 989 device_quiet(dev); 990 return(result); 991} 992 993static int 994attimer_attach(device_t dev) 995{ 996 return(0); 997} 998 999static device_method_t attimer_methods[] = { 1000 /* Device interface */ 1001 DEVMETHOD(device_probe, attimer_probe), 1002 DEVMETHOD(device_attach, attimer_attach), 1003 DEVMETHOD(device_detach, bus_generic_detach), 1004 DEVMETHOD(device_shutdown, bus_generic_shutdown), 1005 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX stop statclock? */ 1006 DEVMETHOD(device_resume, bus_generic_resume), /* XXX restart statclock? */ 1007 { 0, 0 } 1008}; 1009 1010static driver_t attimer_driver = { 1011 "attimer", 1012 attimer_methods, 1013 1, /* no softc */ 1014}; 1015 1016static devclass_t attimer_devclass; 1017 1018DRIVER_MODULE(attimer, isa, attimer_driver, attimer_devclass, 0, 0); 1019#endif /* DEV_ISA */ 1020