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.17 2022/09/25 18:43:32 thorpej 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.17 2022/09/25 18:43:32 thorpej Exp $");
   38 
   39 #include <sys/param.h>
   40 #include <sys/systm.h>
   41 #include <sys/errno.h>
   42 #include <sys/device.h>
   43 
   44 #include <sys/bus.h>
   45 #include <dev/clock_subr.h>
   46 #include <dev/ic/mm58167var.h>
   47 
   48 static int mm58167_gettime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
   49 static int mm58167_settime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
   50 
   51 /*
   52  * To quote SunOS's todreg.h:
   53  * "This brain damaged chip insists on keeping the time in
   54  *  MM/DD HH:MM:SS format, even though it doesn't know about
   55  *  leap years and Feb. 29, thus making it nearly worthless."
   56  */
   57 #define mm58167_read(sc, r)     \
   58         bus_space_read_1(sc->mm58167_regt, sc->mm58167_regh, sc-> r)
   59 #define mm58167_write(sc, r, v) \
   60         bus_space_write_1(sc->mm58167_regt, sc->mm58167_regh, sc-> r, v)
   61 
   62 todr_chip_handle_t
   63 mm58167_attach(struct mm58167_softc *sc)
   64 {
   65         struct todr_chip_handle *handle;
   66 
   67         aprint_normal(": mm58167");
   68 
   69         handle = &sc->_mm58167_todr_handle;
   70         memset(handle, 0, sizeof(*handle));
   71         handle->cookie = sc;
   72         handle->todr_gettime_ymdhms = mm58167_gettime_ymdhms;
   73         handle->todr_settime_ymdhms = mm58167_settime_ymdhms;
   74         return handle;
   75 }
   76 
   77 /*
   78  * Set up the system's time, given a `reasonable' time value.
   79  */
   80 int
   81 mm58167_gettime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
   82 {
   83         struct mm58167_softc *sc = handle->cookie;
   84         struct clock_ymdhms dt_reasonable;
   85         struct timeval now;
   86         int s;
   87         uint8_t byte_value;
   88         int leap_year, had_leap_day;
   89 
   90         /* First, read the date out of the chip. */
   91 
   92         /* No interrupts while we're in the chip. */
   93         s = splhigh();
   94 
   95         /* Reset the status bit: */
   96         byte_value = mm58167_read(sc, mm58167_status);
   97 
   98         /*
   99          * Read the date values until we get a coherent read (one
  100          * where the status stays zero, indicating no increment was
  101          * rippling through while we were reading).
  102          */
  103         do {
  104 #define _MM58167_GET(dt_f, mm_f)                                        \
  105         byte_value = mm58167_read(sc, mm_f);                            \
  106         dt->dt_f = bcdtobin(byte_value)
  107 
  108                 _MM58167_GET(dt_mon, mm58167_mon);
  109                 _MM58167_GET(dt_day, mm58167_day);
  110                 _MM58167_GET(dt_hour, mm58167_hour);
  111                 _MM58167_GET(dt_min, mm58167_min);
  112                 _MM58167_GET(dt_sec, mm58167_sec);
  113 #undef _MM58167_GET
  114         } while ((mm58167_read(sc, mm58167_status) & 1) == 0);
  115 
  116         splx(s);
  117 
  118         /* Convert the reasonable time into a date: */
  119         getmicrotime(&now);
  120         clock_secs_to_ymdhms(now.tv_sec, &dt_reasonable);
  121         if (dt_reasonable.dt_year == POSIX_BASE_YEAR) {
  122                 /*
  123                  * Not a reasonable year.
  124                  * Assume called from inittodr(9) on boot and
  125                  * use file system time set in inittodr(9).
  126                  */
  127                 clock_secs_to_ymdhms(handle->base_time, &dt_reasonable);
  128         }
  129 
  130         /*
  131          * We need to fake a hardware year.  if the hardware MM/DD
  132          * HH:MM:SS date is less than the reasonable MM/DD
  133          * HH:MM:SS, call it the reasonable year plus one, else call
  134          * it the reasonable year.
  135          */
  136         if (dt->dt_mon < dt_reasonable.dt_mon ||
  137             (dt->dt_mon == dt_reasonable.dt_mon &&
  138              (dt->dt_day < dt_reasonable.dt_day ||
  139               (dt->dt_day == dt_reasonable.dt_day &&
  140                (dt->dt_hour < dt_reasonable.dt_hour ||
  141                 (dt->dt_hour == dt_reasonable.dt_hour &&
  142                  (dt->dt_min < dt_reasonable.dt_min ||
  143                   (dt->dt_min == dt_reasonable.dt_min &&
  144                    (dt->dt_sec < dt_reasonable.dt_sec))))))))) {
  145                 dt->dt_year = dt_reasonable.dt_year + 1;
  146         } else {
  147                 dt->dt_year = dt_reasonable.dt_year;
  148         }
  149 
  150         /*
  151          * Make a reasonable effort to see if a leap day has passed
  152          * that we need to account for.  This does the right thing
  153          * only when the system was shut down before a leap day, and
  154          * it is now after that leap day.  It doesn't do the right
  155          * thing when a leap day happened while the machine was last
  156          * up.  When that happens, the hardware clock becomes
  157          * instantly wrong forever, until it gets fixed for some
  158          * reason.  Use NTP to deal.
  159          */
  160 
  161         /*
  162          * This may have happened if the hardware says we're into
  163          * March in the following year.  Check that following year for
  164          * a leap day.
  165          */
  166         if (dt->dt_year > dt_reasonable.dt_year &&
  167             dt->dt_mon >= 3) {
  168                 leap_year = dt->dt_year;
  169         }
  170 
  171         /*
  172          * This may have happened if the hardware says we're in the
  173          * following year, and the system was shut down before March
  174          * the previous year.  check that previous year for a leap
  175          * day.
  176          */
  177         else if (dt->dt_year > dt_reasonable.dt_year &&
  178             dt_reasonable.dt_mon < 3) {
  179                 leap_year = dt_reasonable.dt_year;
  180         }
  181 
  182         /*
  183          * This may have happened if the hardware says we're in the
  184          * same year, but we weren't to March before, and we're in or
  185          * past March now.  Check this year for a leap day.
  186          */
  187         else if (dt->dt_year == dt_reasonable.dt_year
  188             && dt_reasonable.dt_mon < 3
  189             && dt->dt_mon >= 3) {
  190                 leap_year = dt_reasonable.dt_year;
  191         }
  192 
  193         /*
  194          * Otherwise, no leap year to check.
  195          */
  196         else {
  197                 leap_year = 0;
  198         }
  199 
  200         /* Do the real leap day check. */
  201         had_leap_day = 0;
  202         if (leap_year > 0) {
  203                 if ((leap_year & 3) == 0) {
  204                         had_leap_day = 1;
  205                         if ((leap_year % 100) == 0) {
  206                                 had_leap_day = 0;
  207                                 if ((leap_year % 400) == 0)
  208                                         had_leap_day = 1;
  209                         }
  210                 }
  211         }
  212 
  213         /*
  214          * If we had a leap day, adjust the value we will return, and
  215          * also update the hardware clock.
  216          */
  217         /*
  218          * XXX - Since this update just writes back a corrected
  219          * version of what we read out above, we lose whatever
  220          * amount of time the clock has advanced since that read.
  221          * Use NTP to deal.
  222          */
  223         if (had_leap_day) {
  224                 mm58167_settime_ymdhms(handle, dt);
  225         }
  226 
  227         return 0;
  228 }
  229 
  230 int
  231 mm58167_settime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
  232 {
  233         struct mm58167_softc *sc = handle->cookie;
  234         int s;
  235         uint8_t byte_value;
  236 
  237         /* No interrupts while we're in the chip. */
  238         s = splhigh();
  239 
  240         /*
  241          * Issue a GO command to reset everything less significant
  242          * than the minutes to zero.
  243          */
  244         mm58167_write(sc, mm58167_go, 0xFF);
  245 
  246         /* Load everything. */
  247 #define _MM58167_PUT(dt_f, mm_f)                                        \
  248         byte_value = bintobcd(dt->dt_f);                                        \
  249         mm58167_write(sc, mm_f, byte_value)
  250 
  251         _MM58167_PUT(dt_mon, mm58167_mon);
  252         _MM58167_PUT(dt_day, mm58167_day);
  253         _MM58167_PUT(dt_hour, mm58167_hour);
  254         _MM58167_PUT(dt_min, mm58167_min);
  255         _MM58167_PUT(dt_sec, mm58167_sec);
  256 #undef _MM58167_PUT
  257 
  258         splx(s);
  259         return 0;
  260 }

Cache object: 8cf4fcce221b0c0ba5f16add26efb67e


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