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/ic/mm58167.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: mm58167.c,v 1.8 2006/09/04 23:45:30 gdamore Exp $      */
    2 
    3 /*
    4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
    5  * All rights reserved.
    6  *
    7  * This code is derived from software contributed to The NetBSD Foundation
    8  * by Matthew Fredette.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  * 3. All advertising materials mentioning features or use of this software
   19  *    must display the following acknowledgement:
   20  *        This product includes software developed by the NetBSD
   21  *        Foundation, Inc. and its contributors.
   22  * 4. Neither the name of The NetBSD Foundation nor the names of its
   23  *    contributors may be used to endorse or promote products derived
   24  *    from this software without specific prior written permission.
   25  *
   26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   36  * POSSIBILITY OF SUCH DAMAGE.
   37  */
   38 
   39 /*
   40  * National Semiconductor MM58167 time-of-day chip subroutines.
   41  */
   42 
   43 #include <sys/cdefs.h>
   44 __KERNEL_RCSID(0, "$NetBSD: mm58167.c,v 1.8 2006/09/04 23:45:30 gdamore Exp $");
   45 
   46 #include <sys/param.h>
   47 #include <sys/malloc.h>
   48 #include <sys/systm.h>
   49 #include <sys/errno.h>
   50 #include <sys/device.h>
   51 
   52 #include <machine/bus.h>
   53 #include <dev/clock_subr.h>
   54 #include <dev/ic/mm58167var.h>
   55 
   56 int mm58167_gettime(todr_chip_handle_t, volatile struct timeval *);
   57 int mm58167_settime(todr_chip_handle_t, volatile struct timeval *);
   58 
   59 /*
   60  * To quote SunOS's todreg.h:
   61  * "This brain damaged chip insists on keeping the time in
   62  *  MM/DD HH:MM:SS format, even though it doesn't know about
   63  *  leap years and Feb. 29, thus making it nearly worthless."
   64  */
   65 #define mm58167_read(sc, r) bus_space_read_1(sc->mm58167_regt, sc->mm58167_regh, sc-> r)
   66 #define mm58167_write(sc, r, v) bus_space_write_1(sc->mm58167_regt, sc->mm58167_regh, sc-> r, v)
   67 
   68 todr_chip_handle_t
   69 mm58167_attach(sc)
   70         struct mm58167_softc *sc;
   71 {
   72         struct todr_chip_handle *handle;
   73 
   74         printf(": mm58167");
   75 
   76         handle = &sc->_mm58167_todr_handle;
   77         memset(handle, 0, sizeof(handle));
   78         handle->cookie = sc;
   79         handle->todr_gettime = mm58167_gettime;
   80         handle->todr_settime = mm58167_settime;
   81         return (handle);
   82 }
   83 
   84 /*
   85  * Set up the system's time, given a `reasonable' time value.
   86  */
   87 int
   88 mm58167_gettime(handle, tv)
   89         todr_chip_handle_t handle;
   90         volatile struct timeval *tv;
   91 {
   92         struct mm58167_softc *sc = handle->cookie;
   93         struct clock_ymdhms dt_hardware;
   94         struct clock_ymdhms dt_reasonable;
   95         int s;
   96         u_int8_t byte_value;
   97         int leap_year, had_leap_day;
   98 
   99         /* First, read the date out of the chip. */
  100 
  101         /* No interrupts while we're in the chip. */
  102         s = splhigh();
  103 
  104         /* Reset the status bit: */
  105         byte_value = mm58167_read(sc, mm58167_status);
  106 
  107         /*
  108          * Read the date values until we get a coherent read (one
  109          * where the status stays zero, indicating no increment was
  110          * rippling through while we were reading).
  111          */
  112         do {
  113 #define _MM58167_GET(dt_f, mm_f) byte_value = mm58167_read(sc, mm_f); dt_hardware.dt_f = FROMBCD(byte_value)
  114                 _MM58167_GET(dt_mon, mm58167_mon);
  115                 _MM58167_GET(dt_day, mm58167_day);
  116                 _MM58167_GET(dt_hour, mm58167_hour);
  117                 _MM58167_GET(dt_min, mm58167_min);
  118                 _MM58167_GET(dt_sec, mm58167_sec);
  119 #undef _MM58167_GET
  120         } while ((mm58167_read(sc, mm58167_status) & 1) == 0);
  121 
  122         splx(s);
  123 
  124         /* Convert the reasonable time into a date: */
  125         clock_secs_to_ymdhms(tv->tv_sec, &dt_reasonable);
  126 
  127         /*
  128          * We need to fake a hardware year.  if the hardware MM/DD
  129          * HH:MM:SS date is less than the reasonable MM/DD
  130          * HH:MM:SS, call it the reasonable year plus one, else call
  131          * it the reasonable year.
  132          */
  133         if (dt_hardware.dt_mon < dt_reasonable.dt_mon ||
  134             (dt_hardware.dt_mon == dt_reasonable.dt_mon &&
  135                 (dt_hardware.dt_day < dt_reasonable.dt_day ||
  136                     (dt_hardware.dt_day == dt_reasonable.dt_day &&
  137                         (dt_hardware.dt_hour < dt_reasonable.dt_hour ||
  138                             (dt_hardware.dt_hour == dt_reasonable.dt_hour &&
  139                                 (dt_hardware.dt_min < dt_reasonable.dt_min ||
  140                                     (dt_hardware.dt_min == dt_reasonable.dt_min &&
  141                                         (dt_hardware.dt_sec < dt_reasonable.dt_sec))))))))) {
  142           dt_hardware.dt_year = dt_reasonable.dt_year + 1;
  143         } else {
  144           dt_hardware.dt_year = dt_reasonable.dt_year;
  145         }
  146 
  147         /* convert the hardware date into a time: */
  148         tv->tv_sec = clock_ymdhms_to_secs(&dt_hardware);
  149         tv->tv_usec = 0;
  150 
  151         /*
  152          * Make a reasonable effort to see if a leap day has passed
  153          * that we need to account for.  This does the right thing
  154          * only when the system was shut down before a leap day, and
  155          * it is now after that leap day.  It doesn't do the right
  156          * thing when a leap day happened while the machine was last
  157          * up.  When that happens, the hardware clock becomes
  158          * instantly wrong forever, until it gets fixed for some
  159          * reason.  Use NTP to deal.
  160          */
  161 
  162         /*
  163          * This may have happened if the hardware says we're into
  164          * March in the following year.  Check that following year for
  165          * a leap day.
  166          */
  167         if (dt_hardware.dt_year > dt_reasonable.dt_year &&
  168             dt_hardware.dt_mon >= 3) {
  169           leap_year = dt_hardware.dt_year;
  170         }
  171 
  172         /*
  173          * This may have happened if the hardware says we're in the
  174          * following year, and the system was shut down before March
  175          * the previous year.  check that previous year for a leap
  176          * day.
  177          */
  178         else if (dt_hardware.dt_year > dt_reasonable.dt_year &&
  179                  dt_reasonable.dt_mon < 3) {
  180           leap_year = dt_reasonable.dt_year;
  181         }
  182 
  183         /*
  184          * This may have happened if the hardware says we're in the
  185          * same year, but we weren't to March before, and we're in or
  186          * past March now.  Check this year for a leap day.
  187          */
  188         else if (dt_hardware.dt_year == dt_reasonable.dt_year
  189                  && dt_reasonable.dt_mon < 3
  190                  && dt_hardware.dt_mon >= 3) {
  191           leap_year = dt_reasonable.dt_year;
  192         }
  193 
  194         /*
  195          * Otherwise, no leap year to check.
  196          */
  197         else {
  198           leap_year = 0;
  199         }
  200 
  201         /* Do the real leap day check. */
  202         had_leap_day = 0;
  203         if (leap_year > 0) {
  204                 if ((leap_year & 3) == 0) {
  205                         had_leap_day = 1;
  206                         if ((leap_year % 100) == 0) {
  207                                 had_leap_day = 0;
  208                                 if ((leap_year % 400) == 0)
  209                                         had_leap_day = 1;
  210                         }
  211                 }
  212         }
  213 
  214         /*
  215          * If we had a leap day, adjust the value we will return, and
  216          * also update the hardware clock.
  217          */
  218         /*
  219          * XXX - Since this update just writes back a corrected
  220          * version of what we read out above, we lose whatever
  221          * amount of time the clock has advanced since that read.
  222          * Use NTP to deal.
  223          */
  224         if (had_leap_day) {
  225           tv->tv_sec += SECDAY;
  226           todr_settime(handle, tv);
  227         }
  228 
  229         return (0);
  230 }
  231 
  232 int
  233 mm58167_settime(handle, tv)
  234         todr_chip_handle_t handle;
  235         volatile struct timeval *tv;
  236 {
  237         struct mm58167_softc *sc = handle->cookie;
  238         struct clock_ymdhms dt_hardware;
  239         int s;
  240         u_int8_t byte_value;
  241 
  242         /* Convert the seconds into ymdhms. */
  243         clock_secs_to_ymdhms(tv->tv_sec, &dt_hardware);
  244 
  245         /* No interrupts while we're in the chip. */
  246         s = splhigh();
  247 
  248         /*
  249          * Issue a GO command to reset everything less significant
  250          * than the minutes to zero.
  251          */
  252         mm58167_write(sc, mm58167_go, 0xFF);
  253 
  254         /* Load everything. */
  255 #define _MM58167_PUT(dt_f, mm_f) byte_value = TOBCD(dt_hardware.dt_f); mm58167_write(sc, mm_f, byte_value)
  256         _MM58167_PUT(dt_mon, mm58167_mon);
  257         _MM58167_PUT(dt_day, mm58167_day);
  258         _MM58167_PUT(dt_hour, mm58167_hour);
  259         _MM58167_PUT(dt_min, mm58167_min);
  260         _MM58167_PUT(dt_sec, mm58167_sec);
  261 #undef _MM58167_PUT
  262 
  263         splx(s);
  264         return (0);
  265 }

Cache object: 5f5171aa5e6d48f6db4ac17eac295591


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