pcrtc.c revision 147754
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 147754 2005-07-03 14:19:29Z nyan $ 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 wall_cmos_clock; /* wall CMOS clock assumed if != 0 */ 113struct mtx clock_lock; 114 115static int beeping = 0; 116static const u_char daysinmonth[] = {31,28,31,30,31,30,31,31,30,31,30,31}; 117static u_int hardclock_max_count; 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 clockframe *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 if (!using_lapic_timer) 167 hardclock(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_max_count; 476 477 i8254_timecounter.tc_frequency = freq; 478 mtx_lock_spin(&clock_lock); 479 timer_freq = freq; 480 new_timer0_max_count = hardclock_max_count = TIMER_DIV(intr_freq); 481 if (using_lapic_timer) { 482 outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT); 483 outb(TIMER_CNTR0, 0); 484 outb(TIMER_CNTR0, 0); 485 } else if (new_timer0_max_count != timer0_max_count) { 486 timer0_max_count = new_timer0_max_count; 487 outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT); 488 outb(TIMER_CNTR0, timer0_max_count & 0xff); 489 outb(TIMER_CNTR0, timer0_max_count >> 8); 490 } 491 mtx_unlock_spin(&clock_lock); 492} 493 494static void 495i8254_restore(void) 496{ 497 498 set_timer_freq(timer_freq, hz); 499} 500 501 502/* 503 * Restore all the timers non-atomically (XXX: should be atomically). 504 * 505 * This function is called from pmtimer_resume() to restore all the timers. 506 * This should not be necessary, but there are broken laptops that do not 507 * restore all the timers on resume. 508 */ 509void 510timer_restore(void) 511{ 512 513 i8254_restore(); /* restore timer_freq and hz */ 514} 515 516/* 517 * Initialize 8254 timer 0 early so that it can be used in DELAY(). 518 * XXX initialization of other timers is unintentionally left blank. 519 */ 520void 521startrtclock() 522{ 523 u_int delta, freq; 524 525 findcpuspeed(); 526 if (pc98_machine_type & M_8M) 527 timer_freq = 1996800L; /* 1.9968 MHz */ 528 else 529 timer_freq = 2457600L; /* 2.4576 MHz */ 530 531 set_timer_freq(timer_freq, hz); 532 freq = calibrate_clocks(); 533#ifdef CLK_CALIBRATION_LOOP 534 if (bootverbose) { 535 printf( 536 "Press a key on the console to abort clock calibration\n"); 537 while (cncheckc() == -1) 538 calibrate_clocks(); 539 } 540#endif 541 542 /* 543 * Use the calibrated i8254 frequency if it seems reasonable. 544 * Otherwise use the default, and don't use the calibrated i586 545 * frequency. 546 */ 547 delta = freq > timer_freq ? freq - timer_freq : timer_freq - freq; 548 if (delta < timer_freq / 100) { 549#ifndef CLK_USE_I8254_CALIBRATION 550 if (bootverbose) 551 printf( 552"CLK_USE_I8254_CALIBRATION not specified - using default frequency\n"); 553 freq = timer_freq; 554#endif 555 timer_freq = freq; 556 } else { 557 if (bootverbose) 558 printf( 559 "%d Hz differs from default of %d Hz by more than 1%%\n", 560 freq, timer_freq); 561 } 562 563 set_timer_freq(timer_freq, hz); 564 tc_init(&i8254_timecounter); 565 566 init_TSC(); 567} 568 569static void 570rtc_serialcombit(int i) 571{ 572 outb(IO_RTC, ((i&0x01)<<5)|0x07); 573 DELAY(1); 574 outb(IO_RTC, ((i&0x01)<<5)|0x17); 575 DELAY(1); 576 outb(IO_RTC, ((i&0x01)<<5)|0x07); 577 DELAY(1); 578} 579 580static void 581rtc_serialcom(int i) 582{ 583 rtc_serialcombit(i&0x01); 584 rtc_serialcombit((i&0x02)>>1); 585 rtc_serialcombit((i&0x04)>>2); 586 rtc_serialcombit((i&0x08)>>3); 587 outb(IO_RTC, 0x07); 588 DELAY(1); 589 outb(IO_RTC, 0x0f); 590 DELAY(1); 591 outb(IO_RTC, 0x07); 592 DELAY(1); 593} 594 595static void 596rtc_outb(int val) 597{ 598 int s; 599 int sa = 0; 600 601 for (s=0;s<8;s++) { 602 sa = ((val >> s) & 0x01) ? 0x27 : 0x07; 603 outb(IO_RTC, sa); /* set DI & CLK 0 */ 604 DELAY(1); 605 outb(IO_RTC, sa | 0x10); /* CLK 1 */ 606 DELAY(1); 607 } 608 outb(IO_RTC, sa & 0xef); /* CLK 0 */ 609} 610 611static int 612rtc_inb(void) 613{ 614 int s; 615 int sa = 0; 616 617 for (s=0;s<8;s++) { 618 sa |= ((inb(0x33) & 0x01) << s); 619 outb(IO_RTC, 0x17); /* CLK 1 */ 620 DELAY(1); 621 outb(IO_RTC, 0x07); /* CLK 0 */ 622 DELAY(2); 623 } 624 return sa; 625} 626 627/* 628 * Initialize the time of day register, based on the time base which is, e.g. 629 * from a filesystem. 630 */ 631void 632inittodr(time_t base) 633{ 634 unsigned long sec, days; 635 int year, month; 636 int y, m, s; 637 struct timespec ts; 638 int second, min, hour; 639 640 if (base) { 641 s = splclock(); 642 ts.tv_sec = base; 643 ts.tv_nsec = 0; 644 tc_setclock(&ts); 645 splx(s); 646 } 647 648 rtc_serialcom(0x03); /* Time Read */ 649 rtc_serialcom(0x01); /* Register shift command. */ 650 DELAY(20); 651 652 second = bcd2bin(rtc_inb() & 0xff); /* sec */ 653 min = bcd2bin(rtc_inb() & 0xff); /* min */ 654 hour = bcd2bin(rtc_inb() & 0xff); /* hour */ 655 days = bcd2bin(rtc_inb() & 0xff) - 1; /* date */ 656 657 month = (rtc_inb() >> 4) & 0x0f; /* month */ 658 for (m = 1; m < month; m++) 659 days += daysinmonth[m-1]; 660 year = bcd2bin(rtc_inb() & 0xff) + 1900; /* year */ 661 /* 2000 year problem */ 662 if (year < 1995) 663 year += 100; 664 if (year < 1970) 665 goto wrong_time; 666 for (y = 1970; y < year; y++) 667 days += DAYSPERYEAR + LEAPYEAR(y); 668 if ((month > 2) && LEAPYEAR(year)) 669 days ++; 670 sec = ((( days * 24 + 671 hour) * 60 + 672 min) * 60 + 673 second); 674 /* sec now contains the number of seconds, since Jan 1 1970, 675 in the local time zone */ 676 677 s = splhigh(); 678 679 sec += tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0); 680 681 y = time_second - sec; 682 if (y <= -2 || y >= 2) { 683 /* badly off, adjust it */ 684 ts.tv_sec = sec; 685 ts.tv_nsec = 0; 686 tc_setclock(&ts); 687 } 688 splx(s); 689 return; 690 691wrong_time: 692 printf("Invalid time in real time clock.\n"); 693 printf("Check and reset the date immediately!\n"); 694} 695 696/* 697 * Write system time back to RTC 698 */ 699void 700resettodr() 701{ 702 unsigned long tm; 703 int y, m, s; 704 int wd; 705 706 if (disable_rtc_set) 707 return; 708 709 s = splclock(); 710 tm = time_second; 711 splx(s); 712 713 rtc_serialcom(0x01); /* Register shift command. */ 714 715 /* Calculate local time to put in RTC */ 716 717 tm -= tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0); 718 719 rtc_outb(bin2bcd(tm%60)); tm /= 60; /* Write back Seconds */ 720 rtc_outb(bin2bcd(tm%60)); tm /= 60; /* Write back Minutes */ 721 rtc_outb(bin2bcd(tm%24)); tm /= 24; /* Write back Hours */ 722 723 /* We have now the days since 01-01-1970 in tm */ 724 wd = (tm + 4) % 7 + 1; /* Write back Weekday */ 725 for (y = 1970, m = DAYSPERYEAR + LEAPYEAR(y); 726 tm >= m; 727 y++, m = DAYSPERYEAR + LEAPYEAR(y)) 728 tm -= m; 729 730 /* Now we have the years in y and the day-of-the-year in tm */ 731 for (m = 0; ; m++) { 732 int ml; 733 734 ml = daysinmonth[m]; 735 if (m == 1 && LEAPYEAR(y)) 736 ml++; 737 if (tm < ml) 738 break; 739 tm -= ml; 740 } 741 742 m++; 743 rtc_outb(bin2bcd(tm+1)); /* Write back Day */ 744 rtc_outb((m << 4) | wd); /* Write back Month & Weekday */ 745 rtc_outb(bin2bcd(y%100)); /* Write back Year */ 746 747 rtc_serialcom(0x02); /* Time set & Counter hold command. */ 748 rtc_serialcom(0x00); /* Register hold command. */ 749} 750 751 752/* 753 * Start both clocks running. 754 */ 755void 756cpu_initclocks() 757{ 758 759#ifdef DEV_APIC 760 using_lapic_timer = lapic_setup_clock(); 761#endif 762 /* 763 * If we aren't using the local APIC timer to drive the kernel 764 * clocks, setup the interrupt handler for the 8254 timer 0 so 765 * that it can drive hardclock(). Otherwise, change the 8254 766 * timecounter to user a simpler algorithm. 767 */ 768 if (!using_lapic_timer || 1) { 769 intr_add_handler("clk", 0, (driver_intr_t *)clkintr, NULL, 770 INTR_TYPE_CLK | INTR_FAST, NULL); 771 i8254_intsrc = intr_lookup_source(0); 772 if (i8254_intsrc != NULL) 773 i8254_pending = 774 i8254_intsrc->is_pic->pic_source_pending; 775 } else { 776 i8254_timecounter.tc_get_timecount = 777 i8254_simple_get_timecount; 778 i8254_timecounter.tc_counter_mask = 0xffff; 779 set_timer_freq(timer_freq, hz); 780 } 781 782 init_TSC_tc(); 783} 784 785void 786cpu_startprofclock(void) 787{ 788} 789 790void 791cpu_stopprofclock(void) 792{ 793} 794 795static int 796sysctl_machdep_i8254_freq(SYSCTL_HANDLER_ARGS) 797{ 798 int error; 799 u_int freq; 800 801 /* 802 * Use `i8254' instead of `timer' in external names because `timer' 803 * is is too generic. Should use it everywhere. 804 */ 805 freq = timer_freq; 806 error = sysctl_handle_int(oidp, &freq, sizeof(freq), req); 807 if (error == 0 && req->newptr != NULL) 808 set_timer_freq(freq, hz); 809 return (error); 810} 811 812SYSCTL_PROC(_machdep, OID_AUTO, i8254_freq, CTLTYPE_INT | CTLFLAG_RW, 813 0, sizeof(u_int), sysctl_machdep_i8254_freq, "IU", ""); 814 815static unsigned 816i8254_simple_get_timecount(struct timecounter *tc) 817{ 818 u_int count; 819 u_int high, low; 820 821 mtx_lock_spin(&clock_lock); 822 823 /* Select timer0 and latch counter value. */ 824 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH); 825 826 low = inb(TIMER_CNTR0); 827 high = inb(TIMER_CNTR0); 828 count = 0xffff - ((high << 8) | low); 829 mtx_unlock_spin(&clock_lock); 830 return (count); 831} 832 833static unsigned 834i8254_get_timecount(struct timecounter *tc) 835{ 836 u_int count; 837 u_int high, low; 838 u_int eflags; 839 840 eflags = read_eflags(); 841 mtx_lock_spin(&clock_lock); 842 843 /* Select timer0 and latch counter value. */ 844 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH); 845 846 low = inb(TIMER_CNTR0); 847 high = inb(TIMER_CNTR0); 848 count = timer0_max_count - ((high << 8) | low); 849 if (count < i8254_lastcount || 850 (!i8254_ticked && (clkintr_pending || 851 ((count < 20 || (!(eflags & PSL_I) && count < timer0_max_count / 2u)) && 852 i8254_pending != NULL && i8254_pending(i8254_intsrc))))) { 853 i8254_ticked = 1; 854 i8254_offset += timer0_max_count; 855 } 856 i8254_lastcount = count; 857 count += i8254_offset; 858 mtx_unlock_spin(&clock_lock); 859 return (count); 860} 861 862#ifdef DEV_ISA 863/* 864 * Attach to the ISA PnP descriptors for the timer and realtime clock. 865 */ 866static struct isa_pnp_id attimer_ids[] = { 867 { 0x0001d041 /* PNP0100 */, "AT timer" }, 868 { 0x000bd041 /* PNP0B00 */, "AT realtime clock" }, 869 { 0 } 870}; 871 872static int 873attimer_probe(device_t dev) 874{ 875 int result; 876 877 if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, attimer_ids)) <= 0) 878 device_quiet(dev); 879 return(result); 880} 881 882static int 883attimer_attach(device_t dev) 884{ 885 return(0); 886} 887 888static device_method_t attimer_methods[] = { 889 /* Device interface */ 890 DEVMETHOD(device_probe, attimer_probe), 891 DEVMETHOD(device_attach, attimer_attach), 892 DEVMETHOD(device_detach, bus_generic_detach), 893 DEVMETHOD(device_shutdown, bus_generic_shutdown), 894 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX stop statclock? */ 895 DEVMETHOD(device_resume, bus_generic_resume), /* XXX restart statclock? */ 896 { 0, 0 } 897}; 898 899static driver_t attimer_driver = { 900 "attimer", 901 attimer_methods, 902 1, /* no softc */ 903}; 904 905static devclass_t attimer_devclass; 906 907DRIVER_MODULE(attimer, isa, attimer_driver, attimer_devclass, 0, 0); 908#endif /* DEV_ISA */ 909