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

Cache object: f0233bf68e57b3ba0f01c8a32556c15a


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