Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys Fix some issues around todr_wenable():



details:   https://anonhg.NetBSD.org/src/rev/68703e5a1f96
branches:  trunk
changeset: 968067:68703e5a1f96
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Wed Jan 01 19:24:03 2020 +0000

description:
Fix some issues around todr_wenable():
- As previously defined, it was not possible to return an error from the
  back-end RTC driver.  Make it a real function so that it can do so.
- Only the mc146818 and mk48txx drivers used it (for historical reasons).
  Centralize the logic for how it's used in kern_todr.c (and make it private
  to that file) for consistency.

diffstat:

 sys/dev/clock_subr.h  |   5 +----
 sys/dev/ic/mc146818.c |  12 ++----------
 sys/dev/ic/mk48txx.c  |  10 +++-------
 sys/kern/kern_todr.c  |  42 ++++++++++++++++++++++++++++++++++++++++--
 4 files changed, 46 insertions(+), 23 deletions(-)

diffs (219 lines):

diff -r fa85c205d9fb -r 68703e5a1f96 sys/dev/clock_subr.h
--- a/sys/dev/clock_subr.h      Wed Jan 01 18:40:12 2020 +0000
+++ b/sys/dev/clock_subr.h      Wed Jan 01 19:24:03 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: clock_subr.h,v 1.27 2020/01/01 18:08:11 thorpej Exp $  */
+/*     $NetBSD: clock_subr.h,v 1.28 2020/01/01 19:24:03 thorpej Exp $  */
 
 /*-
  * Copyright (c) 1996 The NetBSD Foundation, Inc.
@@ -93,9 +93,6 @@
 };
 typedef struct todr_chip_handle *todr_chip_handle_t;
 
-#define todr_wenable(ct, v)    if ((ct)->todr_setwen) \
-                                       ((*(ct)->todr_setwen)(ct, v))
-
 /*
  * Machine-dependent function that machine-independent RTC drivers can
  * use to register their todr_chip_handle_t with inittodr()/resettodr().
diff -r fa85c205d9fb -r 68703e5a1f96 sys/dev/ic/mc146818.c
--- a/sys/dev/ic/mc146818.c     Wed Jan 01 18:40:12 2020 +0000
+++ b/sys/dev/ic/mc146818.c     Wed Jan 01 19:24:03 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: mc146818.c,v 1.19 2014/11/20 16:34:26 christos Exp $   */
+/*     $NetBSD: mc146818.c,v 1.20 2020/01/01 19:24:03 thorpej Exp $    */
 
 /*-
  * Copyright (c) 2003 Izumi Tsutsui.  All rights reserved.
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: mc146818.c,v 1.19 2014/11/20 16:34:26 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: mc146818.c,v 1.20 2020/01/01 19:24:03 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -85,8 +85,6 @@
 
        s = splclock();         /* XXX really needed? */
 
-       todr_wenable(handle, 1);
-
        timeout = 1000000;      /* XXX how long should we wait? */
        for (;;) {
                if (((*sc->sc_mcread)(sc, MC_REGA) & MC_REGA_UIP) == 0)
@@ -119,8 +117,6 @@
                year += 100;
        dt->dt_year = year;
 
-       todr_wenable(handle, 0);
-
        splx(s);
 
        return 0;
@@ -141,8 +137,6 @@
 
        s = splclock();         /* XXX really needed? */
 
-       todr_wenable(handle, 1);
-
        /*
         * Disable RTC updates during clock updates
         */
@@ -179,8 +173,6 @@
        (*sc->sc_mcwrite)(sc, MC_REGB,
            (*sc->sc_mcread)(sc, MC_REGB) & ~MC_REGB_SET);
 
-       todr_wenable(handle, 0);
-
        splx(s);
 
        return 0;
diff -r fa85c205d9fb -r 68703e5a1f96 sys/dev/ic/mk48txx.c
--- a/sys/dev/ic/mk48txx.c      Wed Jan 01 18:40:12 2020 +0000
+++ b/sys/dev/ic/mk48txx.c      Wed Jan 01 19:24:03 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: mk48txx.c,v 1.27 2014/11/20 16:34:26 christos Exp $ */
+/*     $NetBSD: mk48txx.c,v 1.28 2020/01/01 19:24:03 thorpej Exp $ */
 /*-
  * Copyright (c) 2000 The NetBSD Foundation, Inc.
  * All rights reserved.
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: mk48txx.c,v 1.27 2014/11/20 16:34:26 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: mk48txx.c,v 1.28 2020/01/01 19:24:03 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -113,8 +113,6 @@
        sc = handle->cookie;
        clkoff = sc->sc_clkoffset;
 
-       todr_wenable(handle, 1);
-
        /* enable read (stop time) */
        csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR);
        csr |= MK48TXX_CSR_READ;
@@ -143,7 +141,6 @@
        csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR);
        csr &= ~MK48TXX_CSR_READ;
        (*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr);
-       todr_wenable(handle, 0);
 
        return 0;
 }
@@ -175,7 +172,6 @@
                year = dt->dt_year % 100;
        }
 
-       todr_wenable(handle, 1);
        /* enable write */
        csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR);
        csr |= MK48TXX_CSR_WRITE;
@@ -204,7 +200,7 @@
        csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR);
        csr &= ~MK48TXX_CSR_WRITE;
        (*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr);
-       todr_wenable(handle, 0);
+
        return 0;
 }
 
diff -r fa85c205d9fb -r 68703e5a1f96 sys/kern/kern_todr.c
--- a/sys/kern/kern_todr.c      Wed Jan 01 18:40:12 2020 +0000
+++ b/sys/kern/kern_todr.c      Wed Jan 01 19:24:03 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_todr.c,v 1.42 2020/01/01 18:08:11 thorpej Exp $   */
+/*     $NetBSD: kern_todr.c,v 1.43 2020/01/01 19:24:03 thorpej Exp $   */
 
 /*
  * Copyright (c) 1988 University of Utah.
@@ -41,7 +41,7 @@
 #include "opt_todr.h"
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_todr.c,v 1.42 2020/01/01 18:08:11 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_todr.c,v 1.43 2020/01/01 19:24:03 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/kernel.h>
@@ -239,13 +239,45 @@
 #endif /* TODR_DEBUG */
 
 static int
+todr_wenable(todr_chip_handle_t todr, int onoff)
+{
+
+       if (todr->todr_setwen != NULL)
+               return todr->todr_setwen(todr, onoff);
+       
+       return 0;
+}
+
+#define        ENABLE_TODR_WRITES()                                            \
+do {                                                                   \
+       if ((rv = todr_wenable(tch, 1)) != 0) {                         \
+               printf("%s: cannot enable TODR writes\n", __func__);    \
+               return rv;                                              \
+       }                                                               \
+} while (/*CONSTCOND*/0)
+
+#define        DISABLE_TODR_WRITES()                                           \
+do {                                                                   \
+       if (todr_wenable(tch, 0) != 0)                                  \
+               printf("%s: WARNING: cannot disable TODR writes\n",     \
+                   __func__);                                          \
+} while (/*CONSTCOND*/0)
+
+static int
 todr_gettime(todr_chip_handle_t tch, struct timeval *tvp)
 {
        struct clock_ymdhms     dt;
        int                     rv;
 
+       /*
+        * Write-enable is used even when reading the TODR because
+        * writing to registers may be required in order to do so.
+        */
+
        if (tch->todr_gettime) {
+               ENABLE_TODR_WRITES();
                rv = tch->todr_gettime(tch, tvp);
+               DISABLE_TODR_WRITES();
                /*
                 * Some unconverted ports have their own references to
                 * rtc_offset.   A converted port must not do that.
@@ -255,7 +287,9 @@
                todr_debug("TODR-GET-SECS", rv, NULL, tvp);
                return rv;
        } else if (tch->todr_gettime_ymdhms) {
+               ENABLE_TODR_WRITES();
                rv = tch->todr_gettime_ymdhms(tch, &dt);
+               DISABLE_TODR_WRITES();
                todr_debug("TODR-GET-YMDHMS", rv, &dt, NULL);
                if (rv)
                        return rv;
@@ -297,7 +331,9 @@
                /* See comments above in gettime why this is ifdef'd */
                struct timeval  copy = *tvp;
                copy.tv_sec -= rtc_offset * 60;
+               ENABLE_TODR_WRITES();
                rv = tch->todr_settime(tch, &copy);
+               DISABLE_TODR_WRITES();
                todr_debug("TODR-SET-SECS", rv, NULL, tvp);
                return rv;
        } else if (tch->todr_settime_ymdhms) {
@@ -305,7 +341,9 @@
                if (tvp->tv_usec >= 500000)
                        sec++;
                clock_secs_to_ymdhms(sec, &dt);
+               ENABLE_TODR_WRITES();
                rv = tch->todr_settime_ymdhms(tch, &dt);
+               DISABLE_TODR_WRITES();
                todr_debug("TODR-SET-YMDHMS", rv, &dt, NULL);
                return rv;
        } else {



Home | Main Index | Thread Index | Old Index