The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/pc98/cbus/clock.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 56448f8a73fed565afed7d9bf6c02e93


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.