Subject: Re: O2 sync almost done
To: Christopher SEKIYA <wileyc@rezrov.net>
From: Rafal Boni <rafal@attbi.com>
List: port-sgimips
Date: 01/08/2003 23:23:33
In message <20030108065735.GA8607@rezrov.net>, you write: 

-> On Wed, Jan 08, 2003 at 12:29:41AM -0500, Rafal Boni wrote:
-> 
-> > I am not sure where the century value gets tucked; it could be in DOW or
-> > maybe in the NVRAM?
-> 
-> According to the DS17287 docs, the century byte is located in bank 1, offset
-> 0x48.  Probably should initialize the clock to bank 1 and leave it there,
-> possibly split off ds17287-specific clock get/set routines from the stock
-> mcclock routines as there's no century provision in the base code.

Ah, yes; I actually took the time to look at the ds17287.h header file in
IRIX and the DS17287 docs, and you are indeed correct.  I hacked in some
code real quick to (a) keep the clock in bank1 all the time, and (b) to
read/write the year as an absolute value split amongst the year and the
century registers.  Now at least the PROM and the new kernel agree; I
was too lazy to boot IRIX each time (I can't imagine the PROM and IRIX
disagreeing), but I'll give that a spot-check as well to make sure..

As you say, we should split out the ds17287 code into it's own header, so
for example the century update can happen inside the macro where we turn
off the RTC's update and before we turn it back on, and so we can include
proper definitions for the extended registers needed wake-up/kickstart.

A quick'n'dirty patch is attached,
--rafal

Index: mcclock_mace.c
===================================================================
RCS file: /cvsroot/src/sys/arch/sgimips/dev/mcclock_mace.c,v
retrieving revision 1.7
diff -b -u -p -r1.7 mcclock_mace.c
--- mcclock_mace.c	2002/10/02 15:52:33	1.7
+++ mcclock_mace.c	2003/01/09 04:20:28
@@ -89,6 +89,14 @@
  */
 #define MC_REGA_DV1	0x20
 
+/*
+ * The clock on the IP32 is actually a DS17287, which stores the
+ * century value in the DS_BANK1_CENTURY register; this register
+ * is only available in BANK1 (as the name implies).
+ */
+#define DS_REGA_BANK1_SEL	0x10
+#define DS_BANK1_CENTURY	0x48
+
 struct mcclock_mace_softc {
 	struct device		sc_dev;
 
@@ -128,8 +136,8 @@ mcclock_mace_attach(struct device *paren
 	sc->sc_st = maa->maa_st;
 	sc->sc_sh = maa->maa_sh;
 
-	mc146818_write(sc, MC_REGA, MC_REGA_DV1);
-	mc146818_write(sc, MC_REGB, MC_REGB_BINARY | MC_REGB_24HR);
+	mc146818_write(sc, MC_REGA, DS_REGA_BANK1_SEL | MC_REGA_DV1);
+	mc146818_write(sc, MC_REGB, MC_REGB_24HR);
 	if (!(mc146818_read(sc, MC_REGD) & MC_REGD_VRT))
 		printf(": lithium cell is dead, RTC unreliable");
 	printf("\n");
@@ -168,19 +176,28 @@ mcclock_mace_get(struct device *dev, str
 {
 	struct mcclock_mace_softc *sc = (struct mcclock_mace_softc *)dev;
 	mc_todregs regs;
-	int s;
+	int century;
+	int s, i;
 
 	s = splhigh();
 	MC146818_GETTOD(sc, &regs);
+
+	/* XXXrkb: should be guarded against updates as well! */
+	century = mc146818_read(sc, DS_BANK1_CENTURY);
 	splx(s);
 
+	for (i = 0; i < MC_NTODREGS; i++)
+		printf("read reg %d: %d\n", i, regs[i]);
+
+	printf("read century reg: %d\n", century);
+
 	dt->dt_sec = FROMBCD(regs[MC_SEC]);
 	dt->dt_min = FROMBCD(regs[MC_MIN]);
 	dt->dt_hour = FROMBCD(regs[MC_HOUR]);
 	dt->dt_wday = FROMBCD(regs[MC_DOW]);
 	dt->dt_day = FROMBCD(regs[MC_DOM]);
 	dt->dt_mon = FROMBCD(regs[MC_MONTH]);
-	dt->dt_year = FROM_IRIX_YEAR(FROMBCD(regs[MC_YEAR]));
+	dt->dt_year = (100 * FROMBCD(century)) + FROMBCD(regs[MC_YEAR]);
 }
 
 static void
@@ -188,7 +205,7 @@ mcclock_mace_set(struct device *dev, str
 {
 	struct mcclock_mace_softc *sc = (struct mcclock_mace_softc *)dev;
 	mc_todregs regs;
-	int s;
+	int s, i;
 
 	memset(&regs, 0, sizeof(regs));
 
@@ -198,9 +215,16 @@ mcclock_mace_set(struct device *dev, str
 	regs[MC_DOW] = TOBCD(dt->dt_wday);
 	regs[MC_DOM] = TOBCD(dt->dt_day);
 	regs[MC_MONTH] = TOBCD(dt->dt_mon);
-	regs[MC_YEAR] = TOBCD(TO_IRIX_YEAR(dt->dt_year));
+	regs[MC_YEAR] = TOBCD(dt->dt_year % 100);
 
+	for (i = 0; i < MC_NTODREGS; i++)
+		printf("wrote reg %d: %d\n", i, regs[i]);
+	printf("wrote century: %d\n", TOBCD(dt->dt_year / 100));
+
 	s = splhigh();
 	MC146818_PUTTOD(sc, &regs);
+
+	/* XXXrkb: should be guarded against updates as well! */
+	mc146818_write(sc, DS_BANK1_CENTURY, TOBCD(dt->dt_year / 100));
 	splx(s);
 }

----
Rafal Boni                                                     rafal@attbi.com
  We are all worms.  But I do believe I am a glowworm.  -- Winston Churchill