Module Name:    src
Committed By:   thorpej
Date:           Wed Jan  1 19:24:03 UTC 2020

Modified Files:
        src/sys/dev: clock_subr.h
        src/sys/dev/ic: mc146818.c mk48txx.c
        src/sys/kern: kern_todr.c

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.27 -r1.28 src/sys/dev/clock_subr.h
cvs rdiff -u -r1.19 -r1.20 src/sys/dev/ic/mc146818.c
cvs rdiff -u -r1.27 -r1.28 src/sys/dev/ic/mk48txx.c
cvs rdiff -u -r1.42 -r1.43 src/sys/kern/kern_todr.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/clock_subr.h
diff -u src/sys/dev/clock_subr.h:1.27 src/sys/dev/clock_subr.h:1.28
--- src/sys/dev/clock_subr.h:1.27	Wed Jan  1 18:08:11 2020
+++ src/sys/dev/clock_subr.h	Wed Jan  1 19:24:03 2020
@@ -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 @@ struct todr_chip_handle {
 };
 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().

Index: src/sys/dev/ic/mc146818.c
diff -u src/sys/dev/ic/mc146818.c:1.19 src/sys/dev/ic/mc146818.c:1.20
--- src/sys/dev/ic/mc146818.c:1.19	Thu Nov 20 16:34:26 2014
+++ src/sys/dev/ic/mc146818.c	Wed Jan  1 19:24:03 2020
@@ -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 @@ mc146818_gettime_ymdhms(todr_chip_handle
 
 	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 @@ mc146818_gettime_ymdhms(todr_chip_handle
 		year += 100;
 	dt->dt_year = year;
 
-	todr_wenable(handle, 0);
-
 	splx(s);
 
 	return 0;
@@ -141,8 +137,6 @@ mc146818_settime_ymdhms(todr_chip_handle
 
 	s = splclock();		/* XXX really needed? */
 
-	todr_wenable(handle, 1);
-
 	/*
 	 * Disable RTC updates during clock updates
 	 */
@@ -179,8 +173,6 @@ mc146818_settime_ymdhms(todr_chip_handle
 	(*sc->sc_mcwrite)(sc, MC_REGB,
 	    (*sc->sc_mcread)(sc, MC_REGB) & ~MC_REGB_SET);
 
-	todr_wenable(handle, 0);
-
 	splx(s);
 
 	return 0;

Index: src/sys/dev/ic/mk48txx.c
diff -u src/sys/dev/ic/mk48txx.c:1.27 src/sys/dev/ic/mk48txx.c:1.28
--- src/sys/dev/ic/mk48txx.c:1.27	Thu Nov 20 16:34:26 2014
+++ src/sys/dev/ic/mk48txx.c	Wed Jan  1 19:24:03 2020
@@ -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 @@ mk48txx_gettime_ymdhms(todr_chip_handle_
 	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 @@ mk48txx_gettime_ymdhms(todr_chip_handle_
 	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 @@ mk48txx_settime_ymdhms(todr_chip_handle_
 		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 @@ mk48txx_settime_ymdhms(todr_chip_handle_
 	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;
 }
 

Index: src/sys/kern/kern_todr.c
diff -u src/sys/kern/kern_todr.c:1.42 src/sys/kern/kern_todr.c:1.43
--- src/sys/kern/kern_todr.c:1.42	Wed Jan  1 18:08:11 2020
+++ src/sys/kern/kern_todr.c	Wed Jan  1 19:24:03 2020
@@ -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 @@ todr_debug(const char *prefix, int rv, s
 #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_gettime(todr_chip_handle_t tch, str
 		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 @@ todr_settime(todr_chip_handle_t tch, str
 		/* 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 @@ todr_settime(todr_chip_handle_t tch, str
 		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 {

Reply via email to