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/mc146818.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: mc146818.c,v 1.4 2003/11/24 06:20:40 tsutsui Exp $     */
    2 
    3 /*
    4  * Copyright (c) 2003 Izumi Tsutsui.  All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  * 3. The name of the author may not be used to endorse or promote products
   15  *    derived from this software without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   27  */
   28 
   29 /*
   30  * mc146818 and compatible time of day chip subroutines
   31  */
   32 
   33 #include <sys/cdefs.h>
   34 __KERNEL_RCSID(0, "$NetBSD: mc146818.c,v 1.4 2003/11/24 06:20:40 tsutsui Exp $");
   35 
   36 #include <sys/param.h>
   37 #include <sys/systm.h>
   38 #include <sys/device.h>
   39 #include <sys/errno.h>
   40 
   41 #include <machine/bus.h>
   42 
   43 #include <dev/clock_subr.h>
   44 
   45 #include <dev/ic/mc146818reg.h>
   46 #include <dev/ic/mc146818var.h>
   47 
   48 int mc146818_gettime(todr_chip_handle_t, struct timeval *);
   49 int mc146818_settime(todr_chip_handle_t, struct timeval *);
   50 int mc146818_getcal(todr_chip_handle_t, int *);
   51 int mc146818_setcal(todr_chip_handle_t, int);
   52 
   53 void
   54 mc146818_attach(sc)
   55         struct mc146818_softc *sc;
   56 {
   57         todr_chip_handle_t handle;
   58 
   59 #ifdef DIAGNOSTIC
   60         if (sc->sc_mcread == NULL ||
   61             sc->sc_mcwrite == NULL)
   62                 panic("mc146818_attach: invalid read/write functions");
   63 #endif
   64 
   65         printf(": mc146818 compatible time-of-day clock");
   66 
   67         handle = &sc->sc_handle;
   68         handle->cookie = sc;
   69         handle->todr_gettime = mc146818_gettime;
   70         handle->todr_settime = mc146818_settime;
   71         handle->todr_getcal  = mc146818_getcal;
   72         handle->todr_setcal  = mc146818_setcal;
   73         handle->todr_setwen  = NULL;
   74 }
   75 
   76 /*
   77  * todr_gettime function:
   78  *  Get time of day and convert it to a struct timeval.
   79  *  Return 0 on success, an error number othersize.
   80  */
   81 int
   82 mc146818_gettime(handle, tv)
   83         todr_chip_handle_t handle;
   84         struct timeval *tv;
   85 {
   86         struct mc146818_softc *sc;
   87         struct clock_ymdhms dt;
   88         int s, timeout, cent, year;
   89 
   90         sc = handle->cookie;
   91 
   92         s = splclock();         /* XXX really needed? */
   93 
   94         todr_wenable(handle, 1);
   95 
   96         timeout = 1000000;      /* XXX how long should we wait? */
   97         for (;;) {
   98                 if ((*sc->sc_mcread)(sc, MC_REGA) & MC_REGA_UIP)
   99                         break;
  100                 if (--timeout < 0) {
  101                         printf("mc146818_gettime: timeout\n");
  102                         return EBUSY;
  103                 }
  104         }
  105 
  106 #define FROMREG(x)      ((sc->sc_flag & MC146818_BCD) ? FROMBCD(x) : (x))
  107 
  108         dt.dt_sec  = FROMREG((*sc->sc_mcread)(sc, MC_SEC));
  109         dt.dt_min  = FROMREG((*sc->sc_mcread)(sc, MC_MIN));
  110         dt.dt_hour = FROMREG((*sc->sc_mcread)(sc, MC_HOUR));
  111         dt.dt_wday = FROMREG((*sc->sc_mcread)(sc, MC_DOW));
  112         dt.dt_day  = FROMREG((*sc->sc_mcread)(sc, MC_DOM));
  113         dt.dt_mon  = FROMREG((*sc->sc_mcread)(sc, MC_MONTH));
  114         year       = FROMREG((*sc->sc_mcread)(sc, MC_YEAR));
  115         if (sc->sc_getcent) {
  116                 cent = (*sc->sc_getcent)(sc);
  117                 year += cent * 100;
  118         }
  119 
  120 #undef FROMREG
  121 
  122         year += sc->sc_year0;
  123         if (year < POSIX_BASE_YEAR &&
  124             (sc->sc_flag & MC146818_NO_CENT_ADJUST) == 0)
  125                 year += 100;
  126         dt.dt_year = year;
  127 
  128         todr_wenable(handle, 0);
  129 
  130         splx(s);
  131 
  132         /* simple sanity checks */
  133         if (dt.dt_mon > 12 || dt.dt_day > 31 ||
  134             dt.dt_hour >= 24 || dt.dt_min >= 60 || dt.dt_sec >= 60)
  135                 return EIO;
  136 
  137         tv->tv_sec = clock_ymdhms_to_secs(&dt);
  138         tv->tv_usec = 0;
  139         return 0;
  140 }
  141 
  142 /*
  143  * todr_settime function:
  144  *  Set the time of day clock based on the value of the struct timeval arg.
  145  *  Return 0 on success, an error number othersize.
  146  */
  147 int
  148 mc146818_settime(handle, tv)
  149         todr_chip_handle_t handle;
  150         struct timeval *tv;
  151 {
  152         struct mc146818_softc *sc;
  153         struct clock_ymdhms dt;
  154         int s, timeout, cent, year;
  155 
  156         sc = handle->cookie;
  157 
  158         /* Note: we ignore `tv_usec' */
  159         clock_secs_to_ymdhms(tv->tv_sec, &dt);
  160 
  161         s = splclock();         /* XXX really needed? */
  162 
  163         todr_wenable(handle, 1);
  164 
  165         timeout = 1000000;      /* XXX how long should we wait? */
  166         for (;;) {
  167                 if ((*sc->sc_mcread)(sc, MC_REGA) & MC_REGA_UIP)
  168                         break;
  169                 if (--timeout < 0) {
  170                         printf("mc146818_settime: timeout\n");
  171                         return EBUSY;
  172                 }
  173         }
  174 
  175 #define TOREG(x)        ((sc->sc_flag & MC146818_BCD) ? TOBCD(x) : (x))
  176 
  177         (*sc->sc_mcwrite)(sc, MC_SEC, TOREG(dt.dt_sec));
  178         (*sc->sc_mcwrite)(sc, MC_MIN, TOREG(dt.dt_min));
  179         (*sc->sc_mcwrite)(sc, MC_HOUR, TOREG(dt.dt_hour));
  180         (*sc->sc_mcwrite)(sc, MC_DOW, TOREG(dt.dt_wday));
  181         (*sc->sc_mcwrite)(sc, MC_DOM, TOREG(dt.dt_day));
  182         (*sc->sc_mcwrite)(sc, MC_MONTH, TOREG(dt.dt_mon));
  183 
  184         year = dt.dt_year - sc->sc_year0;
  185         if (sc->sc_setcent) {
  186                 cent = year / 100;
  187                 (*sc->sc_setcent)(sc, cent);
  188                 year -= cent * 100;
  189         }
  190         if (year > 99 &&
  191             (sc->sc_flag & MC146818_NO_CENT_ADJUST) == 0)
  192                 year -= 100;
  193         (*sc->sc_mcwrite)(sc, MC_YEAR, TOREG(year));
  194 
  195 #undef TOREG
  196 
  197         todr_wenable(handle, 0);
  198 
  199         splx(s);
  200 
  201         return 0;
  202 }
  203 
  204 int
  205 mc146818_getcal(handle, vp)
  206         todr_chip_handle_t handle;
  207         int *vp;
  208 {
  209 
  210         return EOPNOTSUPP;
  211 }
  212 
  213 int
  214 mc146818_setcal(handle, v)
  215         todr_chip_handle_t handle;
  216         int v;
  217 {
  218 
  219         return EOPNOTSUPP;
  220 }

Cache object: 3590c42986f3d4e584b157f24be13eec


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