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/dev/dec/mcclock.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /* $NetBSD: mcclock.c,v 1.21 2008/04/08 07:39:11 cegger Exp $ */
    2 
    3 /*
    4  * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
    5  * All rights reserved.
    6  *
    7  * Author: Chris G. Demetriou
    8  *
    9  * Permission to use, copy, modify and distribute this software and
   10  * its documentation is hereby granted, provided that both the copyright
   11  * notice and this permission notice appear in all copies of the
   12  * software, derivative works or modified versions, and any portions
   13  * thereof, and that both notices appear in supporting documentation.
   14  *
   15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
   16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
   17  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
   18  *
   19  * Carnegie Mellon requests users of this software to return to
   20  *
   21  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
   22  *  School of Computer Science
   23  *  Carnegie Mellon University
   24  *  Pittsburgh PA 15213-3890
   25  *
   26  * any improvements or extensions that they make and grant Carnegie the
   27  * rights to redistribute these changes.
   28  */
   29 
   30 #include <sys/cdefs.h>
   31 __KERNEL_RCSID(0, "$NetBSD: mcclock.c,v 1.21 2008/04/08 07:39:11 cegger Exp $");
   32 
   33 #include <sys/param.h>
   34 #include <sys/kernel.h>
   35 #include <sys/systm.h>
   36 #include <sys/device.h>
   37 #include <dev/clock_subr.h>
   38 
   39 #include <dev/dec/clockvar.h>
   40 #include <dev/dec/mcclockvar.h>
   41 #include <dev/ic/mc146818reg.h>
   42 
   43 /*
   44  * XXX default rate is machine-dependent.
   45  */
   46 #ifdef __alpha__
   47 #define MC_DFEAULTHZ    1024
   48 #endif
   49 #ifdef pmax
   50 #define MC_DEFAULTHZ    256
   51 #endif
   52 
   53 
   54 void    mcclock_init(struct device *);
   55 int     mcclock_get(todr_chip_handle_t, volatile struct timeval *);
   56 int     mcclock_set(todr_chip_handle_t, volatile struct timeval *);
   57 
   58 const struct clockfns mcclock_clockfns = {
   59         mcclock_init, 
   60 };
   61 
   62 #define mc146818_write(dev, reg, datum)                                 \
   63             (*(dev)->sc_busfns->mc_bf_write)(dev, reg, datum)
   64 #define mc146818_read(dev, reg)                                         \
   65             (*(dev)->sc_busfns->mc_bf_read)(dev, reg)
   66 
   67 void
   68 mcclock_attach(sc, busfns)
   69         struct mcclock_softc *sc;
   70         const struct mcclock_busfns *busfns;
   71 {
   72 
   73         printf(": mc146818 or compatible");
   74 
   75         sc->sc_busfns = busfns;
   76 
   77         /* Turn interrupts off, just in case. */
   78         mc146818_write(sc, MC_REGB, MC_REGB_BINARY | MC_REGB_24HR);
   79 
   80         clockattach(&sc->sc_dev, &mcclock_clockfns);
   81 
   82         sc->sc_todr.todr_gettime = mcclock_get;
   83         sc->sc_todr.todr_settime = mcclock_set;
   84         sc->sc_todr.cookie = sc;
   85         todr_attach(&sc->sc_todr);
   86 }
   87 
   88 void
   89 mcclock_init(dev)
   90         struct device *dev;
   91 {
   92         struct mcclock_softc *sc = (struct mcclock_softc *)dev;
   93         int rate;
   94 
   95 again:
   96         switch (hz) {
   97         case 32:
   98                 rate = MC_BASE_32_KHz | MC_RATE_32_Hz;
   99                 break;
  100         case 64:
  101                 rate = MC_BASE_32_KHz | MC_RATE_64_Hz;
  102                 break;
  103         case 128:
  104                 rate = MC_BASE_32_KHz | MC_RATE_128_Hz;
  105                 break;
  106         case 256:
  107                 rate = MC_BASE_32_KHz | MC_RATE_256_Hz;
  108                 break;
  109         case 512:
  110                 rate = MC_BASE_32_KHz | MC_RATE_512_Hz;
  111                 break;
  112         case 1024:
  113                 rate = MC_BASE_32_KHz | MC_RATE_1024_Hz;
  114                 break;
  115         case 2048:
  116                 rate = MC_BASE_32_KHz | MC_RATE_2048_Hz;
  117                 break;
  118         case 4096:
  119                 rate = MC_BASE_32_KHz | MC_RATE_4096_Hz;
  120                 break;
  121         case 8192:
  122                 rate = MC_BASE_32_KHz | MC_RATE_8192_Hz;
  123                 break;
  124         case 16384:
  125                 rate = MC_BASE_4_MHz | MC_RATE_1;
  126                 break;
  127         case 32768:
  128                 rate = MC_BASE_4_MHz | MC_RATE_2;
  129                 break;
  130         default:
  131                 printf("%s: Cannot get %d Hz clock; using %d Hz\n",
  132                     device_xname(&sc->sc_dev), hz, MC_DEFAULTHZ);
  133                 hz = MC_DEFAULTHZ;
  134                 goto again;
  135         }
  136         mc146818_write(sc, MC_REGA, rate);
  137         mc146818_write(sc, MC_REGB,
  138             MC_REGB_PIE | MC_REGB_SQWE | MC_REGB_BINARY | MC_REGB_24HR);
  139 }
  140 
  141 /*
  142  * Experiments (and  passing years) show that Decstation PROMS
  143  * assume the kernel uses the clock chip as a time-of-year clock.
  144  * The PROM assumes the clock is always set to 1972 or 1973, and contains
  145  * time-of-year in seconds.   The PROM checks the clock at boot time,
  146  * and if it's outside that range, sets it to 1972-01-01.
  147  *
  148  * XXX should be at the mc146818 layer?
  149 */
  150 
  151 /*
  152  * Get the time of day, based on the clock's value and/or the base value.
  153  */
  154 int
  155 mcclock_get(todr_chip_handle_t tch, volatile struct timeval *tvp)
  156 {
  157         struct mcclock_softc *sc = (struct mcclock_softc *)tch->cookie;
  158         uint32_t yearsecs;
  159         mc_todregs regs;
  160         int s;
  161         struct clock_ymdhms dt;
  162 
  163         s = splclock();
  164         MC146818_GETTOD(sc, &regs)
  165         splx(s);
  166 
  167         dt.dt_sec = regs[MC_SEC];
  168         dt.dt_min = regs[MC_MIN];
  169         dt.dt_hour = regs[MC_HOUR];
  170         dt.dt_day = regs[MC_DOM];
  171         dt.dt_mon = regs[MC_MONTH];
  172         dt.dt_year = 1972;
  173 
  174         yearsecs = clock_ymdhms_to_secs(&dt) - (72 - 70) * SECYR;
  175 
  176         /*
  177          * Take the actual year from the filesystem if possible;
  178          * allow for 2 days of clock loss and 363 days of clock gain.
  179          */
  180         dt.dt_year = 1972; /* or MINYEAR or base/SECYR+1970 ... */
  181         dt.dt_mon = 1;
  182         dt.dt_day = 1;
  183         dt.dt_hour = 0;
  184         dt.dt_min = 0;
  185         dt.dt_sec = 0;
  186         for(;;) {
  187                 tvp->tv_sec = yearsecs + clock_ymdhms_to_secs(&dt);
  188                 if (tvp->tv_sec > tch->base_time - 2 * SECDAY)
  189                         break;
  190                 dt.dt_year++;
  191         }
  192 
  193         tvp->tv_usec = 0;
  194         return 0;
  195 }
  196 
  197 /*
  198  * Reset the TODR based on the time value.
  199  */
  200 int
  201 mcclock_set(todr_chip_handle_t tch, volatile struct timeval *tvp)
  202 {
  203         struct mcclock_softc *sc = (struct mcclock_softc *)tch->cookie;
  204         struct clock_ymdhms dt;
  205         uint32_t yearsecs;
  206         mc_todregs regs;
  207         int s;
  208 
  209         /*
  210          * calculate seconds relative to this year
  211          */
  212         clock_secs_to_ymdhms(tvp->tv_sec, &dt); /* get the year */
  213         dt.dt_mon = 1;
  214         dt.dt_day = 1;
  215         dt.dt_hour = 0;
  216         dt.dt_min = 0;
  217         dt.dt_sec = 0;
  218         yearsecs = tvp->tv_sec - clock_ymdhms_to_secs(&dt);
  219 
  220 #define first72 ((72 - 70) * SECYR)
  221         clock_secs_to_ymdhms(first72 + yearsecs, &dt);
  222 
  223 #ifdef DEBUG
  224         if (dt.dt_year != 1972)
  225                 printf("resettodr: botch (%d, %ld)\n", yearsecs, time_second);
  226 #endif
  227 
  228         s = splclock();
  229         MC146818_GETTOD(sc, &regs);
  230         splx(s);
  231 
  232         regs[MC_SEC] = dt.dt_sec;
  233         regs[MC_MIN] = dt.dt_min;
  234         regs[MC_HOUR] = dt.dt_hour;
  235         regs[MC_DOW] = dt.dt_wday;
  236         regs[MC_DOM] = dt.dt_day;
  237         regs[MC_MONTH] = dt.dt_mon;
  238         regs[MC_YEAR] = dt.dt_year;
  239 
  240         s = splclock();
  241         MC146818_PUTTOD(sc, &regs);
  242         splx(s);
  243 
  244         return 0;
  245 }

Cache object: be2ae28c826308ba7dc10f635ff0d3e4


[ 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.