Subject: todr patch for macppc
To: None <port-macppc@netbsd.org, garbled@netbsd.org>
From: Garrett D'Amore <garrett_damore@tadpole.com>
List: port-macppc
Date: 09/12/2006 14:24:18
This is a multi-part message in MIME format.
--------------030604060201090000030909
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

The attached patch adds todr support.  Hopefully it will be quickly
approved, and then Tim (garbled) can do the timecounter conversion as well.

-- 
Garrett D'Amore, Principal Software Engineer
Tadpole Computer / Computing Technologies Division,
General Dynamics C4 Systems
http://www.tadpolecomputer.com/
Phone: 951 325-2134  Fax: 951 325-2191


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

Index: sys/arch/macppc/dev/adb.c
===================================================================
RCS file: /cvsroot/src/sys/arch/macppc/dev/adb.c,v
retrieving revision 1.19
diff -d -p -u -r1.19 adb.c
--- sys/arch/macppc/dev/adb.c	11 Dec 2005 12:18:03 -0000	1.19
+++ sys/arch/macppc/dev/adb.c	12 Sep 2006 21:18:10 -0000
@@ -49,6 +49,7 @@ __KERNEL_RCSID(0, "$NetBSD: adb.c,v 1.19
 #include <macppc/dev/pm_direct.h>
 #include <macppc/dev/viareg.h>
 
+#include <dev/clock_subr.h>
 #include <dev/ofw/openfirm.h>
 
 #include "aed.h"
@@ -60,6 +61,7 @@ __KERNEL_RCSID(0, "$NetBSD: adb.c,v 1.19
 static int	adbmatch __P((struct device *, struct cfdata *, void *));
 static void	adbattach __P((struct device *, struct device *, void *));
 static int	adbprint __P((void *, const char *));
+static void	adb_todr_init(void);
 
 /*
  * Global variables.
@@ -184,6 +186,8 @@ adbattach(parent, self, aux)
 	if (adbHardware == ADB_HW_CUDA)
 		adb_cuda_autopoll();
 	adb_polling = 0;
+
+	adb_todr_init();
 }
 
 int
@@ -258,3 +262,37 @@ adbprint(args, name)
 
 	return rv;
 }
+
+#define DIFF19041970 2082844800
+
+static int
+adb_todr_get(todr_chip_handle_t tch, volatile struct timeval *tvp)
+{
+	unsigned long sec;
+
+	if (adb_read_date_time(&sec) != 0)
+		return EIO;
+	tvp->tv_sec = sec - DIFF19041970;
+	tvp->tv_usec = 0;
+	return 0;
+}
+
+static int
+adb_todr_set(todr_chip_handle_t tch, volatile struct timeval *tvp)
+{
+	unsigned long sec;
+
+	sec = tvp->tv_sec + DIFF19041970;
+	return adb_set_date_time(sec) ? EIO : 0;
+}
+
+void
+adb_todr_init(void)
+{
+	static struct todr_chip_handle tch = {
+		.todr_gettime = adb_todr_get,
+		.todr_settime = adb_todr_set
+	};
+
+	todr_attach(&tch);
+}
Index: sys/arch/macppc/include/types.h
===================================================================
RCS file: /cvsroot/src/sys/arch/macppc/include/types.h,v
retrieving revision 1.8
diff -d -p -u -r1.8 types.h
--- sys/arch/macppc/include/types.h	11 Dec 2005 12:18:06 -0000	1.8
+++ sys/arch/macppc/include/types.h	12 Sep 2006 21:18:10 -0000
@@ -4,3 +4,4 @@
 
 #define	__HAVE_DEVICE_REGISTER
 #define	__HAVE_GENERIC_SOFT_INTERRUPTS
+#define	__HAVE_GENERIC_TODR
Index: sys/arch/macppc/macppc/clock.c
===================================================================
RCS file: /cvsroot/src/sys/arch/macppc/macppc/clock.c,v
retrieving revision 1.27
diff -d -p -u -r1.27 clock.c
--- sys/arch/macppc/macppc/clock.c	24 Dec 2005 22:45:35 -0000	1.27
+++ sys/arch/macppc/macppc/clock.c	12 Sep 2006 21:18:10 -0000
@@ -54,7 +54,6 @@ __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.
 static u_long ticks_per_sec = 50*1000*1000/4;
 static u_long ns_per_tick = 80;
 long ticks_per_intr;
-static int clockinitted;
 static int clockrunning;
 
 #ifdef TIMEBASE_FREQ
@@ -63,65 +62,6 @@ u_int timebase_freq = TIMEBASE_FREQ;
 u_int timebase_freq = 0;
 #endif
 
-#define SECDAY 86400
-#define DIFF19041970 2082844800
-
-#if NADB > 0
-extern int adb_read_date_time __P((int *));
-extern int adb_set_date_time __P((int));
-#endif
-
-void
-inittodr(base)
-	time_t base;
-{
-	time_t deltat;
-	u_int rtc_time;
-
-	/*
-	 * If we can't read from RTC, use the fs time.
-	 */
-#if NADB > 0
-	if (adb_read_date_time(&rtc_time) < 0)
-#endif
-	{
-		time.tv_sec = base;
-		return;
-	}
-	clockinitted = 1;
-	time.tv_sec = rtc_time - DIFF19041970 + rtc_offset * 60;
-
-	deltat = time.tv_sec - base;
-	if (deltat < 0)
-		deltat = -deltat;
-	if (deltat < 2 * SECDAY)
-		return;
-
-	printf("WARNING: clock %s %d days",
-	    time.tv_sec < base ? "lost" : "gained", (int)(deltat / SECDAY));
-	if (time.tv_sec < base && deltat > 1000 * SECDAY) {
-		printf(", using FS time");
-		time.tv_sec = base;
-	}
-	printf(" -- CHECK AND RESET THE DATE!\n");
-}
-
-/*
- * Similar to the above
- */
-void
-resettodr()
-{
-#if NADB > 0
-	u_int rtc_time;
-
-	if (clockinitted) {
-		rtc_time = time.tv_sec + DIFF19041970 - rtc_offset * 60;
-		adb_set_date_time(rtc_time);
-	}
-#endif
-}
-
 void
 decr_intr(struct clockframe *frame)
 {

--------------030604060201090000030909--