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-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 /* $NetBSD: mcclock.c,v 1.28 2014/11/17 02:15:49 christos 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.28 2014/11/17 02:15:49 christos 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(device_t);
   55 int     mcclock_get(todr_chip_handle_t, struct timeval *);
   56 int     mcclock_set(todr_chip_handle_t, struct timeval *);
   57 
   58 const struct clockfns mcclock_clockfns = {
   59         mcclock_init, 
   60 };
   61 
   62 #define mc146818_write(sc, reg, datum)                                  \
   63             (*(sc)->sc_busfns->mc_bf_write)(sc, reg, datum)
   64 #define mc146818_read(sc, reg)                                          \
   65             (*(sc)->sc_busfns->mc_bf_read)(sc, reg)
   66 
   67 void
   68 mcclock_attach(struct mcclock_softc *sc, const struct mcclock_busfns *busfns)
   69 {
   70 
   71         printf(": mc146818 or compatible");
   72 
   73         sc->sc_busfns = busfns;
   74 
   75         /* Turn interrupts off, just in case. */
   76         mc146818_write(sc, MC_REGB, MC_REGB_BINARY | MC_REGB_24HR);
   77 
   78         clockattach(sc->sc_dev, &mcclock_clockfns);
   79 
   80         sc->sc_todr.todr_gettime = mcclock_get;
   81         sc->sc_todr.todr_settime = mcclock_set;
   82         sc->sc_todr.cookie = sc;
   83         todr_attach(&sc->sc_todr);
   84 }
   85 
   86 void
   87 mcclock_init(device_t dev)
   88 {
   89         struct mcclock_softc *sc = device_private(dev);
   90         int rate;
   91 
   92 again:
   93         switch (hz) {
   94         case 32:
   95                 rate = MC_BASE_32_KHz | MC_RATE_32_Hz;
   96                 break;
   97         case 64:
   98                 rate = MC_BASE_32_KHz | MC_RATE_64_Hz;
   99                 break;
  100         case 128:
  101                 rate = MC_BASE_32_KHz | MC_RATE_128_Hz;
  102                 break;
  103         case 256:
  104                 rate = MC_BASE_32_KHz | MC_RATE_256_Hz;
  105                 break;
  106         case 512:
  107                 rate = MC_BASE_32_KHz | MC_RATE_512_Hz;
  108                 break;
  109         case 1024:
  110                 rate = MC_BASE_32_KHz | MC_RATE_1024_Hz;
  111                 break;
  112         case 2048:
  113                 rate = MC_BASE_32_KHz | MC_RATE_2048_Hz;
  114                 break;
  115         case 4096:
  116                 rate = MC_BASE_32_KHz | MC_RATE_4096_Hz;
  117                 break;
  118         case 8192:
  119                 rate = MC_BASE_32_KHz | MC_RATE_8192_Hz;
  120                 break;
  121         case 16384:
  122                 rate = MC_BASE_4_MHz | MC_RATE_1;
  123                 break;
  124         case 32768:
  125                 rate = MC_BASE_4_MHz | MC_RATE_2;
  126                 break;
  127         default:
  128                 printf("%s: Cannot get %d Hz clock; using %d Hz\n",
  129                     device_xname(dev), hz, MC_DEFAULTHZ);
  130                 hz = MC_DEFAULTHZ;
  131                 goto again;
  132         }
  133         mc146818_write(sc, MC_REGA, rate);
  134         mc146818_write(sc, MC_REGB,
  135             MC_REGB_PIE | MC_REGB_SQWE | MC_REGB_BINARY | MC_REGB_24HR);
  136 }
  137 
  138 /*
  139  * Experiments (and  passing years) show that Decstation PROMS
  140  * assume the kernel uses the clock chip as a time-of-year clock.
  141  * The PROM assumes the clock is always set to 1972 or 1973, and contains
  142  * time-of-year in seconds.   The PROM checks the clock at boot time,
  143  * and if it's outside that range, sets it to 1972-01-01.
  144  *
  145  * XXX should be at the mc146818 layer?
  146 */
  147 
  148 /*
  149  * Get the time of day, based on the clock's value and/or the base value.
  150  */
  151 int
  152 mcclock_get(todr_chip_handle_t tch, struct timeval *tvp)
  153 {
  154         struct mcclock_softc *sc = tch->cookie;
  155         uint32_t yearsecs;
  156         mc_todregs regs;
  157         int s;
  158         struct clock_ymdhms dt;
  159 
  160         s = splclock();
  161         MC146818_GETTOD(sc, &regs)
  162         splx(s);
  163 
  164         dt.dt_sec = regs[MC_SEC];
  165         dt.dt_min = regs[MC_MIN];
  166         dt.dt_hour = regs[MC_HOUR];
  167         dt.dt_day = regs[MC_DOM];
  168         dt.dt_mon = regs[MC_MONTH];
  169         dt.dt_year = 1972;
  170 
  171         yearsecs = clock_ymdhms_to_secs(&dt) - (72 - 70) * SECS_PER_COMMON_YEAR;
  172 
  173         /*
  174          * Take the actual year from the filesystem if possible;
  175          * allow for 2 days of clock loss and 363 days of clock gain.
  176          */
  177         dt.dt_year = 1972; /* or MINYEAR or base/SECS_PER_COMMON_YEAR+1970... */
  178         dt.dt_mon = 1;
  179         dt.dt_day = 1;
  180         dt.dt_hour = 0;
  181         dt.dt_min = 0;
  182         dt.dt_sec = 0;
  183         for(;;) {
  184                 tvp->tv_sec = yearsecs + clock_ymdhms_to_secs(&dt);
  185                 if (tvp->tv_sec > tch->base_time - 2 * SECS_PER_DAY)
  186                         break;
  187                 dt.dt_year++;
  188         }
  189 
  190         tvp->tv_usec = 0;
  191         return 0;
  192 }
  193 
  194 /*
  195  * Reset the TODR based on the time value.
  196  */
  197 int
  198 mcclock_set(todr_chip_handle_t tch, struct timeval *tvp)
  199 {
  200         struct mcclock_softc *sc = tch->cookie;
  201         struct clock_ymdhms dt;
  202         uint32_t yearsecs;
  203         mc_todregs regs;
  204         int s;
  205 
  206         /*
  207          * calculate seconds relative to this year
  208          */
  209         clock_secs_to_ymdhms(tvp->tv_sec, &dt); /* get the year */
  210         dt.dt_mon = 1;
  211         dt.dt_day = 1;
  212         dt.dt_hour = 0;
  213         dt.dt_min = 0;
  214         dt.dt_sec = 0;
  215         yearsecs = tvp->tv_sec - clock_ymdhms_to_secs(&dt);
  216 
  217 #define first72 ((72 - 70) * SECS_PER_COMMON_YEAR)
  218         clock_secs_to_ymdhms(first72 + yearsecs, &dt);
  219 
  220 #ifdef DEBUG
  221         if (dt.dt_year != 1972)
  222                 printf("resettodr: botch (%d, %" PRId64 ")\n",
  223                     yearsecs, time_second);
  224 #endif
  225 
  226         s = splclock();
  227         MC146818_GETTOD(sc, &regs);
  228         splx(s);
  229 
  230         regs[MC_SEC] = dt.dt_sec;
  231         regs[MC_MIN] = dt.dt_min;
  232         regs[MC_HOUR] = dt.dt_hour;
  233         regs[MC_DOW] = dt.dt_wday;
  234         regs[MC_DOM] = dt.dt_day;
  235         regs[MC_MONTH] = dt.dt_mon;
  236         regs[MC_YEAR] = dt.dt_year - 1900;      /* rt clock wants 2 digits */
  237 
  238         s = splclock();
  239         MC146818_PUTTOD(sc, &regs);
  240         splx(s);
  241 
  242         return 0;
  243 }

Cache object: 6e7ddcf9f155abd823ab83a5cd22f916


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