Subject: Re: generic todr implementation for hp700
To: Izumi Tsutsui <tsutsui@ceres.dti.ne.jp>
From: Nick Hudson <nick.hudson@dsl.pipex.com>
List: port-hp700
Date: 09/14/2006 22:16:52
--Boundary-00=_FbcCFnajXoUdl7Q
Content-Type: text/plain;
  charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On Thursday 14 September 2006 18:29, Izumi Tsutsui wrote:
> garrett_damore@tadpole.com wrote:
> > Attached is the code to convert hp700 to generic TODR.  Testing and
> > commit approval desired. :-)
>
> I can't test it right now, but it seems better to move
> settod() and gettod() from dev/clock.c to dev/pdc.c
> and to call todr_attach() in pdc_init().

Like this?

> ---
> Izumi Tsutsui

Nick

--Boundary-00=_FbcCFnajXoUdl7Q
Content-Type: text/x-diff;
  charset="iso-8859-1";
  name="hp700.todr.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="hp700.todr.diff"

Index: sys/arch/hp700/dev/clock.c
===================================================================
RCS file: /cvsroot/src/sys/arch/hp700/dev/clock.c,v
retrieving revision 1.4
diff -u -p -u -r1.4 clock.c
--- sys/arch/hp700/dev/clock.c	11 Dec 2005 12:17:24 -0000	1.4
+++ sys/arch/hp700/dev/clock.c	14 Sep 2006 21:10:28 -0000
@@ -40,8 +40,6 @@ __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.
 #include <sys/kernel.h>
 #include <sys/time.h>
 
-#include <dev/clock_subr.h>
-
 #include <machine/pdc.h>
 #include <machine/iomod.h>
 #include <machine/psl.h>
@@ -58,12 +56,6 @@ __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.
 #include <ddb/db_extern.h>
 #endif
 
-volatile struct timeval time;
-
-void startrtclock(void);
-
-static struct pdc_tod tod PDC_ALIGNMENT;
-
 void
 cpu_initclocks(void)
 {
@@ -97,60 +89,6 @@ clock_intr(void *v)
 	return 1;
 }
 
-
-/*
- * initialize the system time from the time of day clock
- */
-void
-inittodr(time_t t)
-{
-	int 	tbad = 0;
-	int pagezero_cookie;
-
-	if (t < 5*SECYR) {
-		printf ("WARNING: preposterous time in file system");
-		t = 6*SECYR + 186*SECDAY + SECDAY/2;
-		tbad = 1;
-	}
-
-	pagezero_cookie = hp700_pagezero_map();
-	pdc_call((iodcio_t)PAGE0->mem_pdc, 1, PDC_TOD, PDC_TOD_READ,
-		&tod, 0, 0, 0, 0, 0);
-	hp700_pagezero_unmap(pagezero_cookie);
-
-	time.tv_sec = tod.sec;
-	time.tv_usec = tod.usec;
-
-	if (!tbad) {
-		u_long	dt;
-
-		dt = (time.tv_sec < t)?  t - time.tv_sec : time.tv_sec - t;
-
-		if (dt < 2 * SECDAY)
-			return;
-		printf("WARNING: clock %s %ld days",
-		    time.tv_sec < t? "lost" : "gained", dt / SECDAY);
-	}
-
-	printf (" -- CHECK AND RESET THE DATE!\n");
-}
-
-/*
- * reset the time of day clock to the value in time
- */
-void
-resettodr(void)
-{
-	int pagezero_cookie;
-
-	tod.sec = time.tv_sec;
-	tod.usec = time.tv_usec;
-
-	pagezero_cookie = hp700_pagezero_map();
-	pdc_call((iodcio_t)PAGE0->mem_pdc, 1, PDC_TOD, PDC_TOD_WRITE, &tod);
-	hp700_pagezero_unmap(pagezero_cookie);
-}
-
 void
 setstatclockrate(int newhz)
 {
Index: sys/arch/hp700/dev/pdc.c
===================================================================
RCS file: /cvsroot/src/sys/arch/hp700/dev/pdc.c,v
retrieving revision 1.17
diff -u -p -u -r1.17 pdc.c
--- sys/arch/hp700/dev/pdc.c	23 Jul 2006 22:06:05 -0000	1.17
+++ sys/arch/hp700/dev/pdc.c	14 Sep 2006 21:10:28 -0000
@@ -46,6 +46,7 @@ __KERNEL_RCSID(0, "$NetBSD: pdc.c,v 1.17
 #include <sys/kauth.h>
 
 #include <dev/cons.h>
+#include <dev/clock_subr.h>
 
 #include <machine/pdc.h>
 #include <machine/iomod.h>
@@ -98,9 +99,18 @@ int pdccnlookc(dev_t, int *);
 
 static struct cnm_state pdc_cnm_state;
 
+static int pdcgettod(todr_chip_handle_t, volatile struct timeval *);
+static int pdcsettod(todr_chip_handle_t, volatile struct timeval *);
+
+static struct pdc_tod tod PDC_ALIGNMENT;
+
 void
 pdc_init(void)
 {
+	static struct todr_chip_handle todr = {
+		.todr_settime = pdcsettod,
+		.todr_gettime = pdcgettod,
+	};
 	static int kbd_iodc[IODC_MAXSIZE/sizeof(int)];
 	static int cn_iodc[IODC_MAXSIZE/sizeof(int)];
 	int err;
@@ -134,6 +144,9 @@ pdc_init(void)
 	cn_set_magic("+++++");
 
 	hp700_pagezero_unmap(pagezero_cookie);
+
+	/* attach the TOD clock */
+	todr_attach(&todr);
 }
 
 int
@@ -442,3 +455,32 @@ void
 pdccnpollc(dev_t dev, int on)
 {
 }
+
+static int
+pdcgettod(todr_chip_handle_t tch, volatile struct timeval *tvp)
+{
+	int pagezero_cookie;
+
+	pagezero_cookie = hp700_pagezero_map();
+	pdc_call((iodcio_t)PAGE0->mem_pdc, 1, PDC_TOD, PDC_TOD_READ,
+	    &tod, 0, 0, 0, 0, 0);
+	hp700_pagezero_unmap(pagezero_cookie);
+
+	tvp->tv_sec = tod.sec;
+	tvp->tv_usec = tod.usec;
+	return 0;
+}
+
+static int
+pdcsettod(todr_chip_handle_t tch, volatile struct timeval *tvp)
+{
+	int pagezero_cookie;
+
+	tod.sec = tvp->tv_sec;
+	tod.usec = tvp->tv_usec;
+
+	pagezero_cookie = hp700_pagezero_map();
+	pdc_call((iodcio_t)PAGE0->mem_pdc, 1, PDC_TOD, PDC_TOD_WRITE, &tod);
+	hp700_pagezero_unmap(pagezero_cookie);
+	return 0;
+}
Index: sys/arch/hp700/include/types.h
===================================================================
RCS file: /cvsroot/src/sys/arch/hp700/include/types.h,v
retrieving revision 1.5
diff -u -p -u -r1.5 types.h
--- sys/arch/hp700/include/types.h	3 Sep 2006 13:51:23 -0000	1.5
+++ sys/arch/hp700/include/types.h	14 Sep 2006 21:10:28 -0000
@@ -7,5 +7,6 @@
 
 #define	__HAVE_GENERIC_SOFT_INTERRUPTS
 #define	__HAVE_DEVICE_REGISTER
+#define	__HAVE_GENERIC_TODR
 
 #endif	/* _HP700_TYPES_H_ */
Index: sys/arch/hp700/hp700/machdep.c
===================================================================
RCS file: /cvsroot/src/sys/arch/hp700/hp700/machdep.c,v
retrieving revision 1.30
diff -u -p -u -r1.30 machdep.c
--- sys/arch/hp700/hp700/machdep.c	26 Aug 2006 06:17:48 -0000	1.30
+++ sys/arch/hp700/hp700/machdep.c	14 Sep 2006 21:10:30 -0000
@@ -1361,10 +1361,12 @@ cpu_reboot(int howto, char *user_boot_st
 	if (!(howto & RB_NOSYNC) && waittime < 0) {
 		waittime = 0;
 		vfs_shutdown();
-#if 0
+
+		/*
+		 * If we've been adjusting the clock, the todr
+		 * will be out of synch; adjust it now.
+		 */
 		resettodr();
-#endif
-		printf("WARNING: not updating battery clock\n");
 	}
 
 	/* XXX probably save howto into stable storage */

--Boundary-00=_FbcCFnajXoUdl7Q--