Subject: updated diff (again)
To: None <port-sbmips@netbsd.org>
From: Garrett D'Amore <garrett@damore.org>
List: port-sbmips
Date: 09/13/2006 09:15:00
This is a multi-part message in MIME format.
--------------010806070901000801000109
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

This diff is the same, except I've converted the rtc functions to use
the clock_ymdhms variants of the TODR entry points, saving some more
redundant code and inheriting some additional "checks."

(Also, the fewer functions using the old volatile struct timval *, the
better, as I intend to change the signature of that function soon, to
use either struct bintime or struct timespec_t.)

    -- Garrett


--------------010806070901000801000109
Content-Type: text/x-patch;
 name="sbmips.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="sbmips.diff"

Index: sys/arch/sbmips/include/systemsw.h
===================================================================
RCS file: /cvsroot/src/sys/arch/sbmips/include/systemsw.h,v
retrieving revision 1.5
diff -d -p -u -r1.5 systemsw.h
--- sys/arch/sbmips/include/systemsw.h	11 Nov 2005 23:45:56 -0000	1.5
+++ sys/arch/sbmips/include/systemsw.h	13 Sep 2006 16:11:08 -0000
@@ -51,10 +51,6 @@ struct systemsw {
 	void	(*s_statclock_init)(void *);
 	void	(*s_statclock_setrate)(void *, int);
 
-	void	*s_todr_arg;
-	void	(*s_inittodr)(void *, time_t);
-	void	(*s_resettodr)(void *);
-
 	void	*(*s_intr_establish)(u_int, u_int,
 		    void (*fun)(void *, uint32_t, uint32_t), void *);
 };
Index: sys/arch/sbmips/include/types.h
===================================================================
RCS file: /cvsroot/src/sys/arch/sbmips/include/types.h,v
retrieving revision 1.2
diff -d -p -u -r1.2 types.h
--- sys/arch/sbmips/include/types.h	11 Nov 2005 23:45:56 -0000	1.2
+++ sys/arch/sbmips/include/types.h	13 Sep 2006 16:11:08 -0000
@@ -4,3 +4,4 @@
 
 #define	__HAVE_DEVICE_REGISTER
 #define	__HAVE_GENERIC_SOFT_INTERRUPTS
+#define	__HAVE_GENERIC_TODR
Index: sys/arch/sbmips/sbmips/rtc.c
===================================================================
RCS file: /cvsroot/src/sys/arch/sbmips/sbmips/rtc.c,v
retrieving revision 1.13
diff -d -p -u -r1.13 rtc.c
--- sys/arch/sbmips/sbmips/rtc.c	4 Sep 2006 23:45:30 -0000	1.13
+++ sys/arch/sbmips/sbmips/rtc.c	13 Sep 2006 16:11:09 -0000
@@ -68,16 +68,14 @@ struct rtc_softc {
 
 static int xirtc_match(struct device *, struct cfdata *, void *);
 static void xirtc_attach(struct device *, struct device *, void *);
-static int xirtc_gettime(todr_chip_handle_t, volatile struct timeval *);
-static int xirtc_settime(todr_chip_handle_t, volatile struct timeval *);
+static int xirtc_gettime(todr_chip_handle_t, struct clock_ymdhms *);
+static int xirtc_settime(todr_chip_handle_t, struct clock_ymdhms *);
 
 static int strtc_match(struct device *, struct cfdata *, void *);
 static void strtc_attach(struct device *, struct device *, void *);
-static int strtc_gettime(todr_chip_handle_t, volatile struct timeval *);
-static int strtc_settime(todr_chip_handle_t, volatile struct timeval *);
+static int strtc_gettime(todr_chip_handle_t, struct clock_ymdhms *);
+static int strtc_settime(todr_chip_handle_t, struct clock_ymdhms *);
 
-static void rtc_inittodr(void *, time_t base);
-static void rtc_resettodr(void *);
 static void rtc_cal_timer(void);
 
 static void time_smbus_init(int);
@@ -138,24 +136,21 @@ xirtc_attach(struct device *parent, stru
 
 	/* Set up MI todr(9) stuff */
 	sc->sc_ct.cookie = sc;
-	sc->sc_ct.todr_settime = xirtc_settime;
-	sc->sc_ct.todr_gettime = xirtc_gettime;
+	sc->sc_ct.todr_settime_ymdhms = xirtc_settime;
+	sc->sc_ct.todr_gettime_ymdhms = xirtc_gettime;
 
-	system_set_todrfns(sc, rtc_inittodr, rtc_resettodr);
+	todr_attach(&sc->sc_ct);
 
 	printf("\n");
 	rtc_cal_timer();	/* XXX */
 }
 
 static int
-xirtc_settime(todr_chip_handle_t handle, volatile struct timeval *tv)
+xirtc_settime(todr_chip_handle_t handle, struct clock_ymdhms *ymdhms)
 {
 	struct rtc_softc *sc = handle->cookie;
-	struct clock_ymdhms ymdhms;
 	uint8_t year, y2k;
 
-	clock_secs_to_ymdhms(tv->tv_sec, &ymdhms);
-
 	time_smbus_init(sc->sc_smbus_chan);
 
 	/* unlock writes to the CCR */
@@ -163,16 +158,16 @@ xirtc_settime(todr_chip_handle_t handle,
 	WRITERTC(sc, X1241REG_SR, X1241REG_SR_WEL | X1241REG_SR_RWEL);
 
 	/* set the time */
-	WRITERTC(sc, X1241REG_HR, TOBCD(ymdhms.dt_hour) | X1241REG_HR_MIL);
-	WRITERTC(sc, X1241REG_MN, TOBCD(ymdhms.dt_min));
-	WRITERTC(sc, X1241REG_SC, TOBCD(ymdhms.dt_sec));
+	WRITERTC(sc, X1241REG_HR, TOBCD(ymdhms->dt_hour) | X1241REG_HR_MIL);
+	WRITERTC(sc, X1241REG_MN, TOBCD(ymdhms->dt_min));
+	WRITERTC(sc, X1241REG_SC, TOBCD(ymdhms->dt_sec));
 
 	/* set the date */
-	y2k = (ymdhms.dt_year >= 2000) ? 0x20 : 0x19;
-	year = ymdhms.dt_year % 100;
+	y2k = (ymdhms->dt_year >= 2000) ? 0x20 : 0x19;
+	year = ymdhms->dt_year % 100;
 
-	WRITERTC(sc, X1241REG_MO, TOBCD(ymdhms.dt_mon));
-	WRITERTC(sc, X1241REG_DT, TOBCD(ymdhms.dt_day));
+	WRITERTC(sc, X1241REG_MO, TOBCD(ymdhms->dt_mon));
+	WRITERTC(sc, X1241REG_DT, TOBCD(ymdhms->dt_day));
 	WRITERTC(sc, X1241REG_YR, TOBCD(year));
 	WRITERTC(sc, X1241REG_Y2K, TOBCD(y2k));
 
@@ -183,37 +178,33 @@ xirtc_settime(todr_chip_handle_t handle,
 }
 
 static int
-xirtc_gettime(todr_chip_handle_t handle, volatile struct timeval *tv)
+xirtc_gettime(todr_chip_handle_t handle, struct clock_ymdhms *ymdhms)
 {
 	struct rtc_softc *sc = handle->cookie;
-	struct clock_ymdhms ymdhms;
 	uint8_t hour, year, y2k;
 	uint8_t status;
 
 	time_smbus_init(sc->sc_smbus_chan);
-	ymdhms.dt_day = FROMBCD(READRTC(sc, X1241REG_DT));
-	ymdhms.dt_mon =  FROMBCD(READRTC(sc, X1241REG_MO));
+	ymdhms->dt_day = FROMBCD(READRTC(sc, X1241REG_DT));
+	ymdhms->dt_mon =  FROMBCD(READRTC(sc, X1241REG_MO));
 	year =  READRTC(sc, X1241REG_YR);
 	y2k = READRTC(sc, X1241REG_Y2K);
-	ymdhms.dt_year = FROMBCD(y2k) * 100 + FROMBCD(year);
+	ymdhms->dt_year = FROMBCD(y2k) * 100 + FROMBCD(year);
 
 
-	ymdhms.dt_sec = FROMBCD(READRTC(sc, X1241REG_SC));
-	ymdhms.dt_min = FROMBCD(READRTC(sc, X1241REG_MN));
+	ymdhms->dt_sec = FROMBCD(READRTC(sc, X1241REG_SC));
+	ymdhms->dt_min = FROMBCD(READRTC(sc, X1241REG_MN));
 	hour = READRTC(sc, X1241REG_HR);
-	ymdhms.dt_hour = FROMBCD(hour & ~X1241REG_HR_MIL);
+	ymdhms->dt_hour = FROMBCD(hour & ~X1241REG_HR_MIL);
 
 	status = READRTC(sc, X1241REG_SR);
 
 	if (status & X1241REG_SR_RTCF) {
 		printf("%s: battery has failed, clock setting is not accurate\n",
 		    sc->sc_dev.dv_xname);
-		return (-1);
+		return (EIO);
 	}
 
-	tv->tv_sec = clock_ymdhms_to_secs(&ymdhms);
-	tv->tv_usec = 0;
-
 	return (0);
 }
 
@@ -254,120 +245,62 @@ strtc_attach(struct device *parent, stru
 
 	/* Set up MI todr(9) stuff */
 	sc->sc_ct.cookie = sc;
-	sc->sc_ct.todr_settime = strtc_settime;
-	sc->sc_ct.todr_gettime = strtc_gettime;
+	sc->sc_ct.todr_settime_ymdhms = strtc_settime;
+	sc->sc_ct.todr_gettime_ymdhms = strtc_gettime;
 
-	system_set_todrfns(sc, rtc_inittodr, rtc_resettodr);
+	todr_attach(&sc->sc_ct);
 
 	printf("\n");
 	rtc_cal_timer();	/* XXX */
 }
 
 static int
-strtc_settime(todr_chip_handle_t handle, volatile struct timeval *tv)
+strtc_settime(todr_chip_handle_t handle, struct clock_ymdhms *ymdhms)
 {
 	struct rtc_softc *sc = handle->cookie;
-	struct clock_ymdhms ymdhms;
 	uint8_t hour;
 
-	clock_secs_to_ymdhms(tv->tv_sec, &ymdhms);
-
 	time_smbus_init(sc->sc_smbus_chan);
 
-	hour = TOBCD(ymdhms.dt_hour);
-	if (ymdhms.dt_year >= 2000)	/* Should be always true! */
+	hour = TOBCD(ymdhms->dt_hour);
+	if (ymdhms->dt_year >= 2000)	/* Should be always true! */
 		hour |= M41T81_HOUR_CB | M41T81_HOUR_CEB;
 
 	/* set the time */
-	WRITERTC(sc, M41T81_SEC, TOBCD(ymdhms.dt_sec));
-	WRITERTC(sc, M41T81_MIN, TOBCD(ymdhms.dt_min));
+	WRITERTC(sc, M41T81_SEC, TOBCD(ymdhms->dt_sec));
+	WRITERTC(sc, M41T81_MIN, TOBCD(ymdhms->dt_min));
 	WRITERTC(sc, M41T81_HOUR, hour);
 
 	/* set the date */
-	WRITERTC(sc, M41T81_DATE, TOBCD(ymdhms.dt_day));
-	WRITERTC(sc, M41T81_MON, TOBCD(ymdhms.dt_mon));
-	WRITERTC(sc, M41T81_YEAR, TOBCD(ymdhms.dt_year % 100));
+	WRITERTC(sc, M41T81_DATE, TOBCD(ymdhms->dt_day));
+	WRITERTC(sc, M41T81_MON, TOBCD(ymdhms->dt_mon));
+	WRITERTC(sc, M41T81_YEAR, TOBCD(ymdhms->dt_year % 100));
 
 	return (0);
 }
 
 static int
-strtc_gettime(todr_chip_handle_t handle, volatile struct timeval *tv)
+strtc_gettime(todr_chip_handle_t handle, struct clock_ymdhms *ymdhms)
 {
 	struct rtc_softc *sc = handle->cookie;
-	struct clock_ymdhms ymdhms;
 	uint8_t hour;
 
 	time_smbus_init(sc->sc_smbus_chan);
 
-	ymdhms.dt_sec = FROMBCD(READRTC(sc, M41T81_SEC));
-	ymdhms.dt_min = FROMBCD(READRTC(sc, M41T81_MIN));
+	ymdhms->dt_sec = FROMBCD(READRTC(sc, M41T81_SEC));
+	ymdhms->dt_min = FROMBCD(READRTC(sc, M41T81_MIN));
 	hour = READRTC(sc, M41T81_HOUR & M41T81_HOUR_MASK);
-	ymdhms.dt_hour = FROMBCD(hour & M41T81_HOUR_MASK);
+	ymdhms->dt_hour = FROMBCD(hour & M41T81_HOUR_MASK);
 
-	ymdhms.dt_day = FROMBCD(READRTC(sc, M41T81_DATE));
-	ymdhms.dt_mon =  FROMBCD(READRTC(sc, M41T81_MON));
-	ymdhms.dt_year =  1900 + FROMBCD(READRTC(sc, M41T81_YEAR));
+	ymdhms->dt_day = FROMBCD(READRTC(sc, M41T81_DATE));
+	ymdhms->dt_mon =  FROMBCD(READRTC(sc, M41T81_MON));
+	ymdhms->dt_year =  1900 + FROMBCD(READRTC(sc, M41T81_YEAR));
 	if (hour & M41T81_HOUR_CB)
-		ymdhms.dt_year += 100;
-
-	tv->tv_sec = clock_ymdhms_to_secs(&ymdhms);
-	tv->tv_usec = 0;
+		ymdhms->dt_year += 100;
 
 	return (0);
 }
 
-static void
-rtc_inittodr(void *cookie, time_t base)
-{
-	struct timeval todrtime;
-	todr_chip_handle_t chip;
-	struct rtc_softc *sc = cookie;
-	int check;
-
-	check = 0;
-	if (sc == NULL) {
-		printf("inittodr: rtc0 not present");
-		time.tv_sec = base;
-		time.tv_usec = 0;
-		check = 1;
-	} else {
-		chip = &sc->sc_ct;
-		if (todr_gettime(chip, &todrtime) != 0) {
-			printf("inittodr: Error reading clock");
-			time.tv_sec = base;
-			time.tv_usec = 0;
-			check = 1;
-		} else {
-			time = todrtime;
-			if (time.tv_sec > base + 3 * SECDAY) {
-				printf("inittodr: Clock has gained %ld days",
-				    (time.tv_sec - base) / SECDAY);
-				check = 1;
-			} else if (time.tv_sec + SECDAY < base) {
-				printf("inittodr: Clock has lost %ld day(s)",
-				    (base - time.tv_sec) / SECDAY);
-				check = 1;
-			}
-		}
-	}
-	if (check)
-		printf(" - CHECK AND RESET THE DATE.\n");
-
-}
-
-static void
-rtc_resettodr(void *cookie)
-{
-	struct rtc_softc *sc = cookie;
-
-	if (time.tv_sec == 0)
-		return;
-
-	if (todr_settime(&sc->sc_ct, &time) != 0)
-		printf("resettodr: cannot set time in time-of-day clock\n");
-}
-
 #define	NITERS			3
 #define	RTC_SECONDS(rtc)	FROMBCD(READRTC((rtc), X1241REG_SC))
 
Index: sys/arch/sbmips/sbmips/systemsw.c
===================================================================
RCS file: /cvsroot/src/sys/arch/sbmips/sbmips/systemsw.c,v
retrieving revision 1.10
diff -d -p -u -r1.10 systemsw.c
--- sys/arch/sbmips/sbmips/systemsw.c	11 Nov 2005 23:45:56 -0000	1.10
+++ sys/arch/sbmips/sbmips/systemsw.c	13 Sep 2006 16:11:09 -0000
@@ -48,9 +48,7 @@ static void	clock_init_triv(void *);
 static uint32_t	clkread_triv(void);
 static void	cpu_intr_triv(uint32_t, uint32_t, uint32_t, uint32_t);
 static void	delay_triv(u_long);
-static void	inittodr_triv(void *, time_t);
 static void	microtime_triv(struct timeval *);
-static void	resettodr_triv(void *);
 
 /* system function switch */
 struct systemsw systemsw = {
@@ -66,9 +64,6 @@ struct systemsw systemsw = {
 	NULL,			/* s_statclock_init: dflt no-op */
 	NULL,			/* s_statclock_setrate: dflt no-op */
 
-	NULL,			/* todr functions arg */
-	inittodr_triv,
-	resettodr_triv,
 	NULL,			/* intr_establish */
 };
 
@@ -83,21 +78,6 @@ system_set_clockfns(void *arg, void (*in
 	return 0;
 }
 
-int
-system_set_todrfns(void *arg, void (*init)(void *, time_t),
-    void (*reset)(void *))
-{
-
-	if (systemsw.s_inittodr != inittodr_triv ||
-	    systemsw.s_resettodr != resettodr_triv)
-		return 1;
-	systemsw.s_todr_arg = arg;
-	systemsw.s_inittodr = init;
-	systemsw.s_resettodr = reset;
-	return 0;
-}
-
-
 /* trivial microtime() implementation */
 static void
 microtime_triv(struct timeval *tvp)
@@ -178,20 +158,6 @@ clock_init_triv(void *arg)
 	panic("clock_init_triv");
 }
 
-static void
-inittodr_triv(void *arg, time_t t)
-{
-
-	time.tv_sec = t;
-}
-
-static void
-resettodr_triv(void *arg)
-{
-
-	/* do nothing */
-}
-
 void
 cpu_initclocks(void)
 {
@@ -218,17 +184,3 @@ setstatclockrate(int hzrate)
 		(*systemsw.s_statclock_setrate)(systemsw.s_statclock_arg,
 		    hzrate);
 }
-
-void
-inittodr(time_t t)
-{
-
-	(*systemsw.s_inittodr)(systemsw.s_todr_arg, t);
-}
-
-void
-resettodr(void)
-{
-
-	(*systemsw.s_resettodr)(systemsw.s_todr_arg);
-}

--------------010806070901000801000109--