pcrtc.c revision 153666
1/*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * William Jolitz and Don Ahn. 7 * 8 * 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 153666 2005-12-22 22:16:09Z jhb $ 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_apic.h" 52#include "opt_clock.h" 53#include "opt_isa.h" 54#include "opt_mca.h" 55 56#include <sys/param.h> 57#include <sys/systm.h> 58#include <sys/bus.h> 59#include <sys/lock.h> 60#include <sys/kdb.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/limits.h> 67#include <sys/module.h> 68#include <sys/sysctl.h> 69#include <sys/cons.h> 70#include <sys/power.h> 71 72#include <machine/clock.h> 73#include <machine/cputypes.h> 74#include <machine/frame.h> 75#include <machine/intr_machdep.h> 76#include <machine/md_var.h> 77#include <machine/psl.h> 78#ifdef DEV_APIC 79#include <machine/apicvar.h> 80#endif 81#include <machine/specialreg.h> 82#include <machine/ppireg.h> 83#include <machine/timerreg.h> 84 85#include <i386/isa/icu.h> 86#include <pc98/cbus/cbus.h> 87#include <pc98/pc98/pc98_machdep.h> 88#ifdef DEV_ISA 89#include <isa/isavar.h> 90#endif 91 92/* 93 * 32-bit time_t's can't reach leap years before 1904 or after 2036, so we 94 * can use a simple formula for leap years. 95 */ 96#define LEAPYEAR(y) (((u_int)(y) % 4 == 0) ? 1 : 0) 97#define DAYSPERYEAR (31+28+31+30+31+30+31+31+30+31+30+31) 98 99#define TIMER_DIV(x) ((timer_freq + (x) / 2) / (x)) 100 101int adjkerntz; /* local offset from GMT in seconds */ 102int clkintr_pending; 103int disable_rtc_set; /* disable resettodr() if != 0 */ 104int pscnt = 1; 105int psdiv = 1; 106int statclock_disable; 107#ifndef TIMER_FREQ 108#define TIMER_FREQ 2457600 109#endif 110u_int timer_freq = TIMER_FREQ; 111int timer0_max_count; 112int timer0_real_max_count; 113int wall_cmos_clock; /* wall CMOS clock assumed if != 0 */ 114struct mtx clock_lock; 115 116static int beeping = 0; 117static const u_char daysinmonth[] = {31,28,31,30,31,30,31,31,30,31,30,31}; 118static struct intsrc *i8254_intsrc; 119static u_int32_t i8254_lastcount; 120static u_int32_t i8254_offset; 121static int (*i8254_pending)(struct intsrc *); 122static int i8254_ticked; 123static int using_lapic_timer; 124 125/* Values for timerX_state: */ 126#define RELEASED 0 127#define RELEASE_PENDING 1 128#define ACQUIRED 2 129#define ACQUIRE_PENDING 3 130 131static u_char timer1_state; 132static u_char timer2_state; 133static void rtc_serialcombit(int); 134static void rtc_serialcom(int); 135static int rtc_inb(void); 136static void rtc_outb(int); 137 138static unsigned i8254_get_timecount(struct timecounter *tc); 139static unsigned i8254_simple_get_timecount(struct timecounter *tc); 140static void set_timer_freq(u_int freq, int intr_freq); 141 142static struct timecounter i8254_timecounter = { 143 i8254_get_timecount, /* get_timecount */ 144 0, /* no poll_pps */ 145 ~0u, /* counter_mask */ 146 0, /* frequency */ 147 "i8254", /* name */ 148 0 /* quality */ 149}; 150 151static void 152clkintr(struct trapframe *frame) 153{ 154 155 if (timecounter->tc_get_timecount == i8254_get_timecount) { 156 mtx_lock_spin(&clock_lock); 157 if (i8254_ticked) 158 i8254_ticked = 0; 159 else { 160 i8254_offset += timer0_max_count; 161 i8254_lastcount = 0; 162 } 163 clkintr_pending = 0; 164 mtx_unlock_spin(&clock_lock); 165 } 166 KASSERT(!using_lapic_timer, ("clk interrupt enabled with lapic timer")); 167 hardclock(TRAPF_USERMODE(frame), TRAPF_PC(frame)); 168} 169 170int 171acquire_timer1(int mode) 172{ 173 174 if (timer1_state != RELEASED) 175 return (-1); 176 timer1_state = ACQUIRED; 177 178 /* 179 * This access to the timer registers is as atomic as possible 180 * because it is a single instruction. We could do better if we 181 * knew the rate. Use of splclock() limits glitches to 10-100us, 182 * and this is probably good enough for timer2, so we aren't as 183 * careful with it as with timer0. 184 */ 185 outb(TIMER_MODE, TIMER_SEL1 | (mode & 0x3f)); 186 187 return (0); 188} 189 190int 191acquire_timer2(int mode) 192{ 193 194 if (timer2_state != RELEASED) 195 return (-1); 196 timer2_state = ACQUIRED; 197 198 /* 199 * This access to the timer registers is as atomic as possible 200 * because it is a single instruction. We could do better if we 201 * knew the rate. Use of splclock() limits glitches to 10-100us, 202 * and this is probably good enough for timer2, so we aren't as 203 * careful with it as with timer0. 204 */ 205 outb(TIMER_MODE, TIMER_SEL2 | (mode & 0x3f)); 206 207 return (0); 208} 209 210int 211release_timer1() 212{ 213 214 if (timer1_state != ACQUIRED) 215 return (-1); 216 timer1_state = RELEASED; 217 outb(TIMER_MODE, TIMER_SEL1 | TIMER_SQWAVE | TIMER_16BIT); 218 return (0); 219} 220 221int 222release_timer2() 223{ 224 225 if (timer2_state != ACQUIRED) 226 return (-1); 227 timer2_state = RELEASED; 228 outb(TIMER_MODE, TIMER_SEL2 | TIMER_SQWAVE | TIMER_16BIT); 229 return (0); 230} 231 232 233static int 234getit(void) 235{ 236 int high, low; 237 238 mtx_lock_spin(&clock_lock); 239 240 /* Select timer0 and latch counter value. */ 241 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH); 242 243 low = inb(TIMER_CNTR0); 244 high = inb(TIMER_CNTR0); 245 246 mtx_unlock_spin(&clock_lock); 247 return ((high << 8) | low); 248} 249 250/* 251 * Wait "n" microseconds. 252 * Relies on timer 1 counting down from (timer_freq / hz) 253 * Note: timer had better have been programmed before this is first used! 254 */ 255void 256DELAY(int n) 257{ 258 int delta, prev_tick, tick, ticks_left; 259 260#ifdef DELAYDEBUG 261 int getit_calls = 1; 262 int n1; 263 static int state = 0; 264 265 if (state == 0) { 266 state = 1; 267 for (n1 = 1; n1 <= 10000000; n1 *= 10) 268 DELAY(n1); 269 state = 2; 270 } 271 if (state == 1) 272 printf("DELAY(%d)...", n); 273#endif 274 /* 275 * Guard against the timer being uninitialized if we are called 276 * early for console i/o. 277 */ 278 if (timer0_max_count == 0) 279 set_timer_freq(timer_freq, hz); 280 281 /* 282 * Read the counter first, so that the rest of the setup overhead is 283 * counted. Guess the initial overhead is 20 usec (on most systems it 284 * takes about 1.5 usec for each of the i/o's in getit(). The loop 285 * takes about 6 usec on a 486/33 and 13 usec on a 386/20. The 286 * multiplications and divisions to scale the count take a while). 287 * 288 * However, if ddb is active then use a fake counter since reading 289 * the i8254 counter involves acquiring a lock. ddb must not do 290 * locking for many reasons, but it calls here for at least atkbd 291 * input. 292 */ 293#ifdef KDB 294 if (kdb_active) 295 prev_tick = 1; 296 else 297#endif 298 prev_tick = getit(); 299 n -= 0; /* XXX actually guess no initial overhead */ 300 /* 301 * Calculate (n * (timer_freq / 1e6)) without using floating point 302 * and without any avoidable overflows. 303 */ 304 if (n <= 0) 305 ticks_left = 0; 306 else if (n < 256) 307 /* 308 * Use fixed point to avoid a slow division by 1000000. 309 * 39099 = 1193182 * 2^15 / 10^6 rounded to nearest. 310 * 2^15 is the first power of 2 that gives exact results 311 * for n between 0 and 256. 312 */ 313 ticks_left = ((u_int)n * 39099 + (1 << 15) - 1) >> 15; 314 else 315 /* 316 * Don't bother using fixed point, although gcc-2.7.2 317 * generates particularly poor code for the long long 318 * division, since even the slow way will complete long 319 * before the delay is up (unless we're interrupted). 320 */ 321 ticks_left = ((u_int)n * (long long)timer_freq + 999999) 322 / 1000000; 323 324 while (ticks_left > 0) { 325#ifdef KDB 326 if (kdb_active) { 327 outb(0x5f, 0); 328 tick = prev_tick - 1; 329 if (tick <= 0) 330 tick = timer0_max_count; 331 } else 332#endif 333 tick = getit(); 334#ifdef DELAYDEBUG 335 ++getit_calls; 336#endif 337 delta = prev_tick - tick; 338 prev_tick = tick; 339 if (delta < 0) { 340 delta += timer0_max_count; 341 /* 342 * Guard against timer0_max_count being wrong. 343 * This shouldn't happen in normal operation, 344 * but it may happen if set_timer_freq() is 345 * traced. 346 */ 347 if (delta < 0) 348 delta = 0; 349 } 350 ticks_left -= delta; 351 } 352#ifdef DELAYDEBUG 353 if (state == 1) 354 printf(" %d calls to getit() at %d usec each\n", 355 getit_calls, (n + 5) / getit_calls); 356#endif 357} 358 359static void 360sysbeepstop(void *chan) 361{ 362 ppi_spkr_off(); /* disable counter1 output to speaker */ 363 timer_spkr_release(); 364 beeping = 0; 365} 366 367int 368sysbeep(int pitch, int period) 369{ 370 int x = splclock(); 371 372 if (timer_spkr_acquire()) 373 if (!beeping) { 374 /* Something else owns it. */ 375 splx(x); 376 return (-1); /* XXX Should be EBUSY, but nobody cares anyway. */ 377 } 378 disable_intr(); 379 spkr_set_pitch(pitch); 380 enable_intr(); 381 if (!beeping) { 382 /* enable counter1 output to speaker */ 383 ppi_spkr_on(); 384 beeping = period; 385 timeout(sysbeepstop, (void *)NULL, period); 386 } 387 splx(x); 388 return (0); 389} 390 391 392unsigned int delaycount; 393#define FIRST_GUESS 0x2000 394static void findcpuspeed(void) 395{ 396 int i; 397 int remainder; 398 399 /* Put counter in count down mode */ 400 outb(TIMER_MODE, TIMER_SEL0 | TIMER_16BIT | TIMER_RATEGEN); 401 outb(TIMER_CNTR0, 0xff); 402 outb(TIMER_CNTR0, 0xff); 403 for (i = FIRST_GUESS; i; i--) 404 ; 405 remainder = getit(); 406 delaycount = (FIRST_GUESS * TIMER_DIV(1000)) / (0xffff - remainder); 407} 408 409static u_int 410calibrate_clocks(void) 411{ 412 int timeout; 413 u_int count, prev_count, tot_count; 414 u_short sec, start_sec; 415 416 if (bootverbose) 417 printf("Calibrating clock(s) ... "); 418 /* Check ARTIC. */ 419 if (!(PC98_SYSTEM_PARAMETER(0x458) & 0x80) && 420 !(PC98_SYSTEM_PARAMETER(0x45b) & 0x04)) 421 goto fail; 422 timeout = 100000000; 423 424 /* Read the ARTIC. */ 425 sec = inw(0x5e); 426 427 /* Wait for the ARTIC to changes. */ 428 start_sec = sec; 429 for (;;) { 430 sec = inw(0x5e); 431 if (sec != start_sec) 432 break; 433 if (--timeout == 0) 434 goto fail; 435 } 436 prev_count = getit(); 437 if (prev_count == 0 || prev_count > timer0_max_count) 438 goto fail; 439 tot_count = 0; 440 441 start_sec = sec; 442 for (;;) { 443 sec = inw(0x5e); 444 count = getit(); 445 if (count == 0 || count > timer0_max_count) 446 goto fail; 447 if (count > prev_count) 448 tot_count += prev_count - (count - timer0_max_count); 449 else 450 tot_count += prev_count - count; 451 prev_count = count; 452 if ((sec == start_sec + 1200) || /* 1200 = 307.2KHz >> 8 */ 453 (sec < start_sec && 454 (u_int)sec + 0x10000 == (u_int)start_sec + 1200)) 455 break; 456 if (--timeout == 0) 457 goto fail; 458 } 459 460 if (bootverbose) { 461 printf("i8254 clock: %u Hz\n", tot_count); 462 } 463 return (tot_count); 464 465fail: 466 if (bootverbose) 467 printf("failed, using default i8254 clock of %u Hz\n", 468 timer_freq); 469 return (timer_freq); 470} 471 472static void 473set_timer_freq(u_int freq, int intr_freq) 474{ 475 int new_timer0_real_max_count; 476 477 i8254_timecounter.tc_frequency = freq; 478 mtx_lock_spin(&clock_lock); 479 timer_freq = freq; 480 if (using_lapic_timer) 481 new_timer0_real_max_count = 0x10000; 482 else 483 new_timer0_real_max_count = TIMER_DIV(intr_freq); 484 if (new_timer0_real_max_count != timer0_real_max_count) { 485 timer0_real_max_count = new_timer0_real_max_count; 486 if (timer0_real_max_count == 0x10000) 487 timer0_max_count = 0xffff; 488 else 489 timer0_max_count = timer0_real_max_count; 490 outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT); 491 outb(TIMER_CNTR0, timer0_real_max_count & 0xff); 492 outb(TIMER_CNTR0, timer0_real_max_count >> 8); 493 } 494 mtx_unlock_spin(&clock_lock); 495} 496 497static void 498i8254_restore(void) 499{ 500 501 mtx_lock_spin(&clock_lock); 502 outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT); 503 outb(TIMER_CNTR0, timer0_real_max_count & 0xff); 504 outb(TIMER_CNTR0, timer0_real_max_count >> 8); 505 mtx_unlock_spin(&clock_lock); 506} 507 508 509/* 510 * Restore all the timers non-atomically (XXX: should be atomically). 511 * 512 * This function is called from pmtimer_resume() to restore all the timers. 513 * This should not be necessary, but there are broken laptops that do not 514 * restore all the timers on resume. 515 */ 516void 517timer_restore(void) 518{ 519 520 i8254_restore(); /* restore timer_freq and hz */ 521} 522 523/* 524 * Initialize 8254 timer 0 early so that it can be used in DELAY(). 525 * XXX initialization of other timers is unintentionally left blank. 526 */ 527void 528startrtclock() 529{ 530 u_int delta, freq; 531 532 findcpuspeed(); 533 if (pc98_machine_type & M_8M) 534 timer_freq = 1996800L; /* 1.9968 MHz */ 535 else 536 timer_freq = 2457600L; /* 2.4576 MHz */ 537 538 set_timer_freq(timer_freq, hz); 539 freq = calibrate_clocks(); 540#ifdef CLK_CALIBRATION_LOOP 541 if (bootverbose) { 542 printf( 543 "Press a key on the console to abort clock calibration\n"); 544 while (cncheckc() == -1) 545 calibrate_clocks(); 546 } 547#endif 548 549 /* 550 * Use the calibrated i8254 frequency if it seems reasonable. 551 * Otherwise use the default, and don't use the calibrated i586 552 * frequency. 553 */ 554 delta = freq > timer_freq ? freq - timer_freq : timer_freq - freq; 555 if (delta < timer_freq / 100) { 556#ifndef CLK_USE_I8254_CALIBRATION 557 if (bootverbose) 558 printf( 559"CLK_USE_I8254_CALIBRATION not specified - using default frequency\n"); 560 freq = timer_freq; 561#endif 562 timer_freq = freq; 563 } else { 564 if (bootverbose) 565 printf( 566 "%d Hz differs from default of %d Hz by more than 1%%\n", 567 freq, timer_freq); 568 } 569 570 set_timer_freq(timer_freq, hz); 571 tc_init(&i8254_timecounter); 572 573 init_TSC(); 574} 575 576static void 577rtc_serialcombit(int i) 578{ 579 outb(IO_RTC, ((i&0x01)<<5)|0x07); 580 DELAY(1); 581 outb(IO_RTC, ((i&0x01)<<5)|0x17); 582 DELAY(1); 583 outb(IO_RTC, ((i&0x01)<<5)|0x07); 584 DELAY(1); 585} 586 587static void 588rtc_serialcom(int i) 589{ 590 rtc_serialcombit(i&0x01); 591 rtc_serialcombit((i&0x02)>>1); 592 rtc_serialcombit((i&0x04)>>2); 593 rtc_serialcombit((i&0x08)>>3); 594 outb(IO_RTC, 0x07); 595 DELAY(1); 596 outb(IO_RTC, 0x0f); 597 DELAY(1); 598 outb(IO_RTC, 0x07); 599 DELAY(1); 600} 601 602static void 603rtc_outb(int val) 604{ 605 int s; 606 int sa = 0; 607 608 for (s=0;s<8;s++) { 609 sa = ((val >> s) & 0x01) ? 0x27 : 0x07; 610 outb(IO_RTC, sa); /* set DI & CLK 0 */ 611 DELAY(1); 612 outb(IO_RTC, sa | 0x10); /* CLK 1 */ 613 DELAY(1); 614 } 615 outb(IO_RTC, sa & 0xef); /* CLK 0 */ 616} 617 618static int 619rtc_inb(void) 620{ 621 int s; 622 int sa = 0; 623 624 for (s=0;s<8;s++) { 625 sa |= ((inb(0x33) & 0x01) << s); 626 outb(IO_RTC, 0x17); /* CLK 1 */ 627 DELAY(1); 628 outb(IO_RTC, 0x07); /* CLK 0 */ 629 DELAY(2); 630 } 631 return sa; 632} 633 634/* 635 * Initialize the time of day register, based on the time base which is, e.g. 636 * from a filesystem. 637 */ 638void 639inittodr(time_t base) 640{ 641 unsigned long sec, days; 642 int year, month; 643 int y, m, s; 644 struct timespec ts; 645 int second, min, hour; 646 647 if (base) { 648 s = splclock(); 649 ts.tv_sec = base; 650 ts.tv_nsec = 0; 651 tc_setclock(&ts); 652 splx(s); 653 } 654 655 rtc_serialcom(0x03); /* Time Read */ 656 rtc_serialcom(0x01); /* Register shift command. */ 657 DELAY(20); 658 659 second = bcd2bin(rtc_inb() & 0xff); /* sec */ 660 min = bcd2bin(rtc_inb() & 0xff); /* min */ 661 hour = bcd2bin(rtc_inb() & 0xff); /* hour */ 662 days = bcd2bin(rtc_inb() & 0xff) - 1; /* date */ 663 664 month = (rtc_inb() >> 4) & 0x0f; /* month */ 665 for (m = 1; m < month; m++) 666 days += daysinmonth[m-1]; 667 year = bcd2bin(rtc_inb() & 0xff) + 1900; /* year */ 668 /* 2000 year problem */ 669 if (year < 1995) 670 year += 100; 671 if (year < 1970) 672 goto wrong_time; 673 for (y = 1970; y < year; y++) 674 days += DAYSPERYEAR + LEAPYEAR(y); 675 if ((month > 2) && LEAPYEAR(year)) 676 days ++; 677 sec = ((( days * 24 + 678 hour) * 60 + 679 min) * 60 + 680 second); 681 /* sec now contains the number of seconds, since Jan 1 1970, 682 in the local time zone */ 683 684 s = splhigh(); 685 686 sec += tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0); 687 688 y = time_second - sec; 689 if (y <= -2 || y >= 2) { 690 /* badly off, adjust it */ 691 ts.tv_sec = sec; 692 ts.tv_nsec = 0; 693 tc_setclock(&ts); 694 } 695 splx(s); 696 return; 697 698wrong_time: 699 printf("Invalid time in real time clock.\n"); 700 printf("Check and reset the date immediately!\n"); 701} 702 703/* 704 * Write system time back to RTC 705 */ 706void 707resettodr() 708{ 709 unsigned long tm; 710 int y, m, s; 711 int wd; 712 713 if (disable_rtc_set) 714 return; 715 716 s = splclock(); 717 tm = time_second; 718 splx(s); 719 720 rtc_serialcom(0x01); /* Register shift command. */ 721 722 /* Calculate local time to put in RTC */ 723 724 tm -= tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0); 725 726 rtc_outb(bin2bcd(tm%60)); tm /= 60; /* Write back Seconds */ 727 rtc_outb(bin2bcd(tm%60)); tm /= 60; /* Write back Minutes */ 728 rtc_outb(bin2bcd(tm%24)); tm /= 24; /* Write back Hours */ 729 730 /* We have now the days since 01-01-1970 in tm */ 731 wd = (tm + 4) % 7 + 1; /* Write back Weekday */ 732 for (y = 1970, m = DAYSPERYEAR + LEAPYEAR(y); 733 tm >= m; 734 y++, m = DAYSPERYEAR + LEAPYEAR(y)) 735 tm -= m; 736 737 /* Now we have the years in y and the day-of-the-year in tm */ 738 for (m = 0; ; m++) { 739 int ml; 740 741 ml = daysinmonth[m]; 742 if (m == 1 && LEAPYEAR(y)) 743 ml++; 744 if (tm < ml) 745 break; 746 tm -= ml; 747 } 748 749 m++; 750 rtc_outb(bin2bcd(tm+1)); /* Write back Day */ 751 rtc_outb((m << 4) | wd); /* Write back Month & Weekday */ 752 rtc_outb(bin2bcd(y%100)); /* Write back Year */ 753 754 rtc_serialcom(0x02); /* Time set & Counter hold command. */ 755 rtc_serialcom(0x00); /* Register hold command. */ 756} 757 758 759/* 760 * Start both clocks running. 761 */ 762void 763cpu_initclocks() 764{ 765 766#ifdef DEV_APIC 767 using_lapic_timer = lapic_setup_clock(); 768#endif 769 /* 770 * If we aren't using the local APIC timer to drive the kernel 771 * clocks, setup the interrupt handler for the 8254 timer 0 so 772 * that it can drive hardclock(). Otherwise, change the 8254 773 * timecounter to user a simpler algorithm. 774 */ 775 if (!using_lapic_timer) { 776 intr_add_handler("clk", 0, (driver_intr_t *)clkintr, NULL, 777 INTR_TYPE_CLK | INTR_FAST, NULL); 778 i8254_intsrc = intr_lookup_source(0); 779 if (i8254_intsrc != NULL) 780 i8254_pending = 781 i8254_intsrc->is_pic->pic_source_pending; 782 } else { 783 i8254_timecounter.tc_get_timecount = 784 i8254_simple_get_timecount; 785 i8254_timecounter.tc_counter_mask = 0xffff; 786 set_timer_freq(timer_freq, hz); 787 } 788 789 init_TSC_tc(); 790} 791 792void 793cpu_startprofclock(void) 794{ 795} 796 797void 798cpu_stopprofclock(void) 799{ 800} 801 802static int 803sysctl_machdep_i8254_freq(SYSCTL_HANDLER_ARGS) 804{ 805 int error; 806 u_int freq; 807 808 /* 809 * Use `i8254' instead of `timer' in external names because `timer' 810 * is is too generic. Should use it everywhere. 811 */ 812 freq = timer_freq; 813 error = sysctl_handle_int(oidp, &freq, sizeof(freq), req); 814 if (error == 0 && req->newptr != NULL) 815 set_timer_freq(freq, hz); 816 return (error); 817} 818 819SYSCTL_PROC(_machdep, OID_AUTO, i8254_freq, CTLTYPE_INT | CTLFLAG_RW, 820 0, sizeof(u_int), sysctl_machdep_i8254_freq, "IU", ""); 821 822static unsigned 823i8254_simple_get_timecount(struct timecounter *tc) 824{ 825 826 return (timer0_max_count - getit()); 827} 828 829static unsigned 830i8254_get_timecount(struct timecounter *tc) 831{ 832 u_int count; 833 u_int high, low; 834 u_int eflags; 835 836 eflags = read_eflags(); 837 mtx_lock_spin(&clock_lock); 838 839 /* Select timer0 and latch counter value. */ 840 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH); 841 842 low = inb(TIMER_CNTR0); 843 high = inb(TIMER_CNTR0); 844 count = timer0_max_count - ((high << 8) | low); 845 if (count < i8254_lastcount || 846 (!i8254_ticked && (clkintr_pending || 847 ((count < 20 || (!(eflags & PSL_I) && count < timer0_max_count / 2u)) && 848 i8254_pending != NULL && i8254_pending(i8254_intsrc))))) { 849 i8254_ticked = 1; 850 i8254_offset += timer0_max_count; 851 } 852 i8254_lastcount = count; 853 count += i8254_offset; 854 mtx_unlock_spin(&clock_lock); 855 return (count); 856} 857 858#ifdef DEV_ISA 859/* 860 * Attach to the ISA PnP descriptors for the timer and realtime clock. 861 */ 862static struct isa_pnp_id attimer_ids[] = { 863 { 0x0001d041 /* PNP0100 */, "AT timer" }, 864 { 0x000bd041 /* PNP0B00 */, "AT realtime clock" }, 865 { 0 } 866}; 867 868static int 869attimer_probe(device_t dev) 870{ 871 int result; 872 873 if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, attimer_ids)) <= 0) 874 device_quiet(dev); 875 return(result); 876} 877 878static int 879attimer_attach(device_t dev) 880{ 881 return(0); 882} 883 884static device_method_t attimer_methods[] = { 885 /* Device interface */ 886 DEVMETHOD(device_probe, attimer_probe), 887 DEVMETHOD(device_attach, attimer_attach), 888 DEVMETHOD(device_detach, bus_generic_detach), 889 DEVMETHOD(device_shutdown, bus_generic_shutdown), 890 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX stop statclock? */ 891 DEVMETHOD(device_resume, bus_generic_resume), /* XXX restart statclock? */ 892 { 0, 0 } 893}; 894 895static driver_t attimer_driver = { 896 "attimer", 897 attimer_methods, 898 1, /* no softc */ 899}; 900 901static devclass_t attimer_devclass; 902 903DRIVER_MODULE(attimer, isa, attimer_driver, attimer_devclass, 0, 0); 904#endif /* DEV_ISA */ 905