Subject: Re: CATS todclock reorganization (Re: CVS commit: syssrc/sys/arch)
To: None <port-cats@netbsd.org>
From: Izumi Tsutsui <tsutsui@ceres.dti.ne.jp>
List: port-cats
Date: 02/11/2002 02:15:06
In article <200202101309.g1AD9jM10377@mirage.ceres.dti.ne.jp>
I wrote:

> Anyway, I think we should reorganize todclock attachment on cats port.
> My idea is:
 :

Now I've made a patch for this change.

One problem is footbridge's RTC, but currently it is not
implemented anyway so I just disable attachment for it.

> Comments?

---
Izumi Tsutsui
tsutsui@ceres.dti.ne.jp

Index: arch/arm/footbridge/footbridge.c
===================================================================
RCS file: /cvsroot/syssrc/sys/arch/arm/footbridge/footbridge.c,v
retrieving revision 1.4
diff -u -r1.4 footbridge.c
--- arch/arm/footbridge/footbridge.c	2002/01/05 22:41:47	1.4
+++ arch/arm/footbridge/footbridge.c	2002/02/10 16:43:49
@@ -214,6 +214,7 @@
 	fba.fba_pba.pba_bus = 0;
 	config_found(self, &fba.fba_pba, footbridge_print);
 
+#if 0
 	/* Attach a time-of-day clock device */
 	fba.fba_tca.ta_name = "todclock";
 	fba.fba_tca.ta_rtc_arg = NULL;
@@ -221,6 +222,7 @@
 	fba.fba_tca.ta_rtc_read = NULL;
 	fba.fba_tca.ta_flags = TODCLOCK_FLAG_FAKE;
 	config_found(self, &fba.fba_tca, footbridge_print); 
+#endif
 
 	/* Attach uart device */
 	fba.fba_fca.fca_name = "fcom";
Index: arch/arm/footbridge/footbridgevar.h
===================================================================
RCS file: /cvsroot/syssrc/sys/arch/arm/footbridge/footbridgevar.h,v
retrieving revision 1.2
diff -u -r1.2 footbridgevar.h
--- arch/arm/footbridge/footbridgevar.h	2002/02/10 12:26:00	1.2
+++ arch/arm/footbridge/footbridgevar.h	2002/02/10 16:43:49
@@ -37,7 +37,7 @@
 #include <machine/bus.h>
 #include <machine/rtc.h>
 #include <dev/pci/pcivar.h>
-#include <arm/footbridge/todclockvar.h>
+/*#include <arm/footbridge/todclockvar.h>*/
 
 /*
  * DC21285 softc structure.
@@ -77,7 +77,7 @@
 		bus_space_handle_t fba_ioh;	/* Bus handle */
 	} fba_fba;
 	struct pcibus_attach_args fba_pba;	/* pci attach args */
-	struct todclock_attach_args fba_tca;
+/*	struct todclock_attach_args fba_tca;*/
 	struct fcom_attach_args {
 		char *fca_name;
 		bus_space_tag_t fca_iot;
Index: arch/arm/footbridge/isa/dsrtc.c
===================================================================
RCS file: /cvsroot/syssrc/sys/arch/arm/footbridge/isa/dsrtc.c,v
retrieving revision 1.1
diff -u -r1.1 dsrtc.c
--- arch/arm/footbridge/isa/dsrtc.c	2002/02/10 12:26:01	1.1
+++ arch/arm/footbridge/isa/dsrtc.c	2002/02/10 16:43:49
@@ -42,160 +42,20 @@
 #include <sys/conf.h>
 #include <sys/device.h>
 
-#include <machine/rtc.h>
+#include <dev/clock_subr.h>
+#include <cats/cats/todclockvar.h>
 
-#include <arm/footbridge/todclockvar.h>
-#include <arm/footbridge/isa/ds1687reg.h>
-
 #include <dev/isa/isavar.h>
+#include <dev/ic/ds1687reg.h>
+#include <dev/ic/ds1687var.h>
 
-#define NRTC_PORTS	2
-
-struct dsrtc_softc {
-	struct device	sc_dev;
-	bus_space_tag_t	sc_iot;
-	bus_space_handle_t sc_ioh;
-};
 
-void dsrtcattach __P((struct device *parent, struct device *self, void *aux));
 int dsrtcmatch __P((struct device *parent, struct cfdata *cf, void *aux));
-int ds1687_read __P((struct dsrtc_softc *sc, int addr));
-void ds1687_write __P((struct dsrtc_softc *sc, int addr, int data));
-int ds1687_ram_read __P((struct dsrtc_softc *sc, int addr));
-void ds1687_ram_write __P((struct dsrtc_softc *sc, int addr, int data));
-static void ds1687_bank_select __P((struct dsrtc_softc *, int));
-static int dsrtc_write __P((void *, rtc_t *));
-static int dsrtc_read __P((void *, rtc_t *));
-
-int
-ds1687_read(sc, addr)
-	struct dsrtc_softc *sc;
-	int addr;
-{
-
-	bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_ADDR_REG, addr);
-	return(bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_DATA_REG));
-}
-
-void
-ds1687_write(sc, addr, data)
-	struct dsrtc_softc *sc;
-	int addr;
-	int data;
-{
-
-	bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_ADDR_REG, addr);
-	bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_DATA_REG, data);
-}
-
-static void
-ds1687_bank_select(sc, bank)
-	struct dsrtc_softc *sc;
-	int bank;
-{
-	int data;
-
-	data = ds1687_read(sc, RTC_REG_A);
-	data &= ~RTC_REG_A_BANK_MASK;
-	if (bank)
-		data |= RTC_REG_A_BANK1;
-	ds1687_write(sc, RTC_REG_A, data);
-}
-
-#if 0
-/* Nothing uses these yet */
-int
-ds1687_ram_read(sc, addr)
-	struct dsrtc_softc *sc;
-	int addr;
-{
-	if (addr < RTC_PC_RAM_SIZE)
-		return(ds1687_read(sc, RTC_PC_RAM_START + addr));
-
-	addr -= RTC_PC_RAM_SIZE;
-	if (addr < RTC_BANK0_RAM_SIZE)
-		return(ds1687_read(sc, RTC_BANK0_RAM_START + addr));		
-
-	addr -= RTC_BANK0_RAM_SIZE;
-	if (addr < RTC_EXT_RAM_SIZE) {
-		int data;
-
-		ds1687_bank_select(sc, 1);
-		ds1687_write(sc, RTC_EXT_RAM_ADDRESS, addr);
-		data = ds1687_read(sc, RTC_EXT_RAM_DATA);
-		ds1687_bank_select(sc, 0);
-		return(data);
-	}
-	return(-1);
-}
-
-void
-ds1687_ram_write(sc, addr, val)
-	struct dsrtc_softc *sc;
-	int addr;
-	int val;
-{
-	if (addr < RTC_PC_RAM_SIZE)
-		return(ds1687_write(sc, RTC_PC_RAM_START + addr, val));
-
-	addr -= RTC_PC_RAM_SIZE;
-	if (addr < RTC_BANK0_RAM_SIZE)
-		return(ds1687_write(sc, RTC_BANK0_RAM_START + addr, val));
-
-	addr -= RTC_BANK0_RAM_SIZE;
-	if (addr < RTC_EXT_RAM_SIZE) {
-		ds1687_bank_select(sc, 1);
-		ds1687_write(sc, RTC_EXT_RAM_ADDRESS, addr);
-		ds1687_write(sc, RTC_EXT_RAM_DATA, val);
-		ds1687_bank_select(sc, 0);
-	}
-}
-#endif
-
-static int
-dsrtc_write(arg, rtc)
-	void *arg;
-	rtc_t *rtc;
-{
-	struct dsrtc_softc *sc = arg;
-
-	ds1687_write(sc, RTC_SECONDS, rtc->rtc_sec);
-	ds1687_write(sc, RTC_MINUTES, rtc->rtc_min);
-	ds1687_write(sc, RTC_HOURS, rtc->rtc_hour);
-	ds1687_write(sc, RTC_DAYOFMONTH, rtc->rtc_day);
-	ds1687_write(sc, RTC_MONTH, rtc->rtc_mon);
-	ds1687_write(sc, RTC_YEAR, rtc->rtc_year);
-	ds1687_bank_select(sc, 1);
-	ds1687_write(sc, RTC_CENTURY, rtc->rtc_cen);
-	ds1687_bank_select(sc, 0);
-	return(1);
-}
-
-static int
-dsrtc_read(arg, rtc)
-	void *arg;
-	rtc_t *rtc;
-{
-	struct dsrtc_softc *sc = arg;
-
-	rtc->rtc_micro = 0;
-	rtc->rtc_centi = 0;
-	rtc->rtc_sec   = ds1687_read(sc, RTC_SECONDS);
-	rtc->rtc_min   = ds1687_read(sc, RTC_MINUTES);
-	rtc->rtc_hour  = ds1687_read(sc, RTC_HOURS);
-	rtc->rtc_day   = ds1687_read(sc, RTC_DAYOFMONTH);
-	rtc->rtc_mon   = ds1687_read(sc, RTC_MONTH);
-	rtc->rtc_year  = ds1687_read(sc, RTC_YEAR);
-	ds1687_bank_select(sc, 1);
-	rtc->rtc_cen   = ds1687_read(sc, RTC_CENTURY); 
-	ds1687_bank_select(sc, 0);
-
-	return(1);
-}
+void dsrtcattach __P((struct device *parent, struct device *self, void *aux));
 
 /* device and attach structures */
 struct cfattach dsrtc_ca = {
-	sizeof(struct dsrtc_softc), dsrtcmatch, dsrtcattach
+	sizeof(struct ds1687_softc), dsrtcmatch, dsrtcattach
 };
 
 /*
@@ -238,9 +98,9 @@
 	struct device *self;
 	void *aux;
 {
-	struct dsrtc_softc *sc = (struct dsrtc_softc *)self;
+	struct ds1687_softc *sc = (struct ds1687_softc *)self;
 	struct isa_attach_args *ia = aux;
-	struct todclock_attach_args ta;
+	todr_chip_handle_t handle;
 	
 	sc->sc_iot = ia->ia_iot;
 	if (bus_space_map(sc->sc_iot, ia->ia_io[0].ir_addr,
@@ -249,19 +109,12 @@
 		return;
 	}
 
-	ds1687_write(sc, RTC_REG_A, RTC_REG_A_DV1);
-	ds1687_write(sc, RTC_REG_B, RTC_REG_B_BINARY | RTC_REG_B_24_HOUR);
+	if ((handle = ds1687_attach(sc)) == NULL)
+		panic("can't attach tod clock");
 
-	if (!(ds1687_read(sc, RTC_REG_D) & RTC_REG_D_VRT))
-		printf(": lithium cell is dead, RTC unreliable");
+	handle->bus_cookie = NULL;
 	printf("\n");
 
-	ta.ta_name = "todclock";
-	ta.ta_rtc_arg = sc;
-	ta.ta_rtc_write = dsrtc_write; 
-	ta.ta_rtc_read = dsrtc_read;
-	ta.ta_flags = 0;
-	config_found(self, &ta, NULL);
+	todclock_config(handle);
 }
 
-/* End of dsrtc.c */
Index: arch/cats/conf/GENERIC
===================================================================
RCS file: /cvsroot/syssrc/sys/arch/cats/conf/GENERIC,v
retrieving revision 1.17
diff -u -r1.17 GENERIC
--- arch/cats/conf/GENERIC	2002/01/27 13:23:13	1.17
+++ arch/cats/conf/GENERIC	2002/02/10 16:43:50
@@ -180,9 +180,6 @@
 # system clock via footbridge
 #clock*	at footbridge?
 
-# time-of-day device via footbridge or RTC
-todclock0	at todservice?
-
 # PCI bus support
 # PCI bus via footbridge
 pci0	at footbridge?			# PCI bus
Index: arch/cats/conf/files.cats
===================================================================
RCS file: /cvsroot/syssrc/sys/arch/cats/conf/files.cats,v
retrieving revision 1.19
diff -u -r1.19 files.cats
--- arch/cats/conf/files.cats	2002/02/10 12:26:03	1.19
+++ arch/cats/conf/files.cats	2002/02/10 16:43:50
@@ -31,9 +31,8 @@
 #
 # time of day clock
 #
-device	todclock
-attach	todclock at todservice
-file	arch/arm/footbridge/todclock.c			todclock	needs-count
+file	dev/clock_subr.c
+file	arch/cats/cats/todclock.c
 
 # ISA DMA glue
 file	arch/arm/footbridge/isa/isadma_machdep.c	isadma
@@ -85,9 +84,12 @@
 attach	sysbeep at pcppi with sysbeep_isa
 file	arch/arm/footbridge/isa/sysbeep_isa.c		sysbeep_isa
 
-device dsrtc: todservice
+define	ds1687
+file	dev/ic/ds1687.c					ds1687
+device	dsrtc: ds1687
 attach dsrtc at isa
 file	arch/arm/footbridge/isa/dsrtc.c			dsrtc
+
 # Machine-independent I2O drivers.
 include "dev/i2o/files.i2o"
 
--- /dev/null	Sun Feb 10 02:16:46 2002
+++ dev/ic/ds1687.c	Mon Feb 11 01:32:21 2002
@@ -0,0 +1,249 @@
+/*	$NetBSD$	*/
+
+/*
+ * Copyright (c) 1998 Mark Brinicombe.
+ * Copyright (c) 1998 Causality Limited.
+ * All rights reserved.
+ *
+ * Written by Mark Brinicombe, Causality Limited
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *	This product includes software developed by Mark Brinicombe
+ *	for the NetBSD Project.
+ * 4. The name of the company nor the name of the author may be used to
+ *    endorse or promote products derived from this software without specific
+ *    prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY CAUASLITY LIMITED ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL CAUSALITY LIMITED OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD$");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/device.h>
+#include <sys/errno.h>
+#include <sys/malloc.h>
+
+#include <machine/bus.h>
+
+#include <dev/clock_subr.h>
+#include <dev/ic/ds1687reg.h>
+#include <dev/ic/ds1687var.h>
+
+static int ds1687_gettime(todr_chip_handle_t, struct timeval *);
+static int ds1687_settime(todr_chip_handle_t, struct timeval *);
+static int ds1687_getcal(todr_chip_handle_t, int *);
+static int ds1687_setcal(todr_chip_handle_t, int);
+static int ds1687_read(struct ds1687_softc *sc, int addr);
+static void ds1687_write(struct ds1687_softc *sc, int addr, int data);
+static void ds1687_bank_select(struct ds1687_softc *, int);
+#if 0
+int ds1687_ram_read(struct ds1687_softc *sc, int addr);
+void ds1687_ram_write(struct ds1687_softc *sc, int addr, int data);
+#endif
+
+todr_chip_handle_t
+ds1687_attach(sc)
+	struct ds1687_softc *sc;
+{
+	todr_chip_handle_t handle;
+
+	ds1687_write(sc, RTC_REG_A, RTC_REG_A_DV1);
+	ds1687_write(sc, RTC_REG_B, RTC_REG_B_BINARY | RTC_REG_B_24_HOUR);
+
+	if ((ds1687_read(sc, RTC_REG_D) & RTC_REG_D_VRT) == 0)
+		printf(": lithium cell is dead, RTC unriliable");
+
+	handle = malloc(sizeof(struct todr_chip_handle), M_DEVBUF, M_NOWAIT);
+
+	handle->cookie = sc;
+	handle->todr_gettime = ds1687_gettime;
+	handle->todr_settime = ds1687_settime;
+	handle->todr_getcal  = ds1687_getcal;
+	handle->todr_setcal  = ds1687_setcal;
+	handle->todr_setwen  = NULL;
+
+	return handle;
+}
+
+static int
+ds1687_gettime(handle, tv)
+	todr_chip_handle_t handle;
+	struct timeval *tv;
+{
+	struct ds1687_softc *sc = (struct ds1687_softc *)handle->cookie;
+	struct clock_ymdhms dt;
+	int century, year;
+
+	dt.dt_sec  = ds1687_read(sc, RTC_SECONDS);
+	dt.dt_min  = ds1687_read(sc, RTC_MINUTES);
+	dt.dt_hour = ds1687_read(sc, RTC_HOURS);
+	dt.dt_day  = ds1687_read(sc, RTC_DAYOFMONTH);
+	dt.dt_mon  = ds1687_read(sc, RTC_MONTH);
+	year       = ds1687_read(sc, RTC_YEAR);
+	ds1687_bank_select(sc, 1);
+	century    = ds1687_read(sc, RTC_CENTURY); 
+	ds1687_bank_select(sc, 0);
+	dt.dt_year = century * 100 + year;
+
+	/* simple sanity checks */
+	if (dt.dt_year < POSIX_BASE_YEAR || dt.dt_mon > 12 || dt.dt_day > 31 ||
+	    dt.dt_hour >= 24 || dt.dt_min >= 60 || dt.dt_sec >= 60)
+		return 1;
+
+	tv->tv_sec = clock_ymdhms_to_secs(&dt);
+	tv->tv_usec = 0;
+	return 0;
+}
+
+static int
+ds1687_settime(handle, tv)
+	todr_chip_handle_t handle;
+	struct timeval *tv;
+{
+	struct ds1687_softc *sc = (struct ds1687_softc *)handle->cookie;
+	struct clock_ymdhms dt;
+	int century, year;
+
+	/* Note: we ignore `tv_usec' */
+	clock_secs_to_ymdhms(tv->tv_sec, &dt);
+	century = dt.dt_year / 100;
+	year    = dt.dt_year % 100;
+
+	ds1687_write(sc, RTC_SECONDS, dt.dt_sec);
+	ds1687_write(sc, RTC_MINUTES, dt.dt_min);
+	ds1687_write(sc, RTC_HOURS, dt.dt_hour);
+	ds1687_write(sc, RTC_DAYOFMONTH, dt.dt_day);
+	ds1687_write(sc, RTC_MONTH, dt.dt_mon);
+	ds1687_write(sc, RTC_YEAR, year);
+	ds1687_bank_select(sc, 1);
+	ds1687_write(sc, RTC_CENTURY, century);
+	ds1687_bank_select(sc, 0);
+
+	return 0;
+}
+
+static int
+ds1687_getcal(handle, vp)
+	todr_chip_handle_t handle;
+	int *vp;
+{
+
+	return EOPNOTSUPP;
+}
+
+static int
+ds1687_setcal(handle, v)
+	todr_chip_handle_t handle;
+	int v;
+{
+
+	return EOPNOTSUPP;
+}
+
+static int
+ds1687_read(sc, addr)
+	struct ds1687_softc *sc;
+	int addr;
+{
+
+	bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_ADDR_REG, addr);
+	return bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_DATA_REG);
+}
+
+static void
+ds1687_write(sc, addr, data)
+	struct ds1687_softc *sc;
+	int addr;
+	int data;
+{
+
+	bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_ADDR_REG, addr);
+	bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_DATA_REG, data);
+}
+
+static void
+ds1687_bank_select(sc, bank)
+	struct ds1687_softc *sc;
+	int bank;
+{
+	int data;
+
+	data = ds1687_read(sc, RTC_REG_A);
+	data &= ~RTC_REG_A_BANK_MASK;
+	if (bank)
+		data |= RTC_REG_A_BANK1;
+	ds1687_write(sc, RTC_REG_A, data);
+}
+
+#if 0
+/* Nothing uses these yet */
+static int
+ds1687_ram_read(sc, addr)
+	struct ds1687_softc *sc;
+	int addr;
+{
+	if (addr < RTC_PC_RAM_SIZE)
+		return ds1687_read(sc, RTC_PC_RAM_START + addr);
+
+	addr -= RTC_PC_RAM_SIZE;
+	if (addr < RTC_BANK0_RAM_SIZE)
+		return ds1687_read(sc, RTC_BANK0_RAM_START + addr);
+
+	addr -= RTC_BANK0_RAM_SIZE;
+	if (addr < RTC_EXT_RAM_SIZE) {
+		int data;
+
+		ds1687_bank_select(sc, 1);
+		ds1687_write(sc, RTC_EXT_RAM_ADDRESS, addr);
+		data = ds1687_read(sc, RTC_EXT_RAM_DATA);
+		ds1687_bank_select(sc, 0);
+		return data ;
+	}
+	return -1;
+}
+
+static void
+ds1687_ram_write(sc, addr, val)
+	struct ds1687_softc *sc;
+	int addr;
+	int val;
+{
+	if (addr < RTC_PC_RAM_SIZE)
+		return ds1687_write(sc, RTC_PC_RAM_START + addr, val);
+
+	addr -= RTC_PC_RAM_SIZE;
+	if (addr < RTC_BANK0_RAM_SIZE)
+		return ds1687_write(sc, RTC_BANK0_RAM_START + addr, val);
+
+	addr -= RTC_BANK0_RAM_SIZE;
+	if (addr < RTC_EXT_RAM_SIZE) {
+		ds1687_bank_select(sc, 1);
+		ds1687_write(sc, RTC_EXT_RAM_ADDRESS, addr);
+		ds1687_write(sc, RTC_EXT_RAM_DATA, val);
+		ds1687_bank_select(sc, 0);
+	}
+}
+#endif
+
--- /dev/null	Sun Feb 10 02:16:46 2002
+++ dev/ic/ds1687reg.h	Sun Feb 10 22:53:20 2002
@@ -0,0 +1,130 @@
+/*	$NetBSD: ds1687reg.h,v 1.1 1998/10/05 01:20:57 mark Exp $	*/
+
+/*
+ * Copyright (c) 1998 Mark Brinicombe.
+ * Copyright (c) 1998 Causality Limited.
+ * All rights reserved.
+ *
+ * Written by Mark Brinicombe, Causality Limited
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *	This product includes software developed by Mark Brinicombe
+ *	for the NetBSD Project.
+ * 4. The name of the company nor the name of the author may be used to
+ *    endorse or promote products derived from this software without specific
+ *    prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY CAUASLITY LIMITED ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL CAUSALITY LIMITED OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#define NRTC_PORTS		2
+
+#define RTC_ADDR		0x72
+#define RTC_ADDR_REG		0x00
+#define RTC_DATA_REG		0x01
+
+#define	RTC_SECONDS		0x00
+#define	RTC_SECONDS_ALARM	0x01
+#define RTC_MINUTES		0x02
+#define RTC_MINUTES_ALARM	0x03
+#define	RTC_HOURS		0x04
+#define	RTC_HOURS_ALARM		0x05
+#define	RTC_DAYOFWEEK		0x06
+#define	RTC_DAYOFMONTH		0x07
+#define	RTC_MONTH		0x08
+#define RTC_YEAR		0x09
+
+#define RTC_REG_A		0x0a
+#define  RTC_REG_A_UIP		0x80	/* Update In Progress */
+#define  RTC_REG_A_DV2		0x40	/* Countdown CHain */
+#define  RTC_REG_A_DV1		0x20	/* Oscillator Enable */
+#define  RTC_REG_A_DV0		0x10	/* Bank Select */
+#define  RTC_REG_A_BANK_MASK	RTC_REG_A_DV0
+#define  RTC_REG_A_BANK1	RTC_REG_A_DV0
+#define  RTC_REG_A_BANK0	0x00
+#define  RTC_REG_A_RS_MASK	0x0f	/* Rate select mask */
+#define  RTC_REG_A_RS_NONE	0x00
+#define  RTC_REG_A_RS_256HZ_1	0x01
+#define  RTC_REG_A_RS_128HZ_1	0x02
+#define  RTC_REG_A_RS_8192HZ	0x03
+#define  RTC_REG_A_RS_4096HZ	0x04
+#define  RTC_REG_A_RS_2048HZ	0x05
+#define  RTC_REG_A_RS_1024HZ	0x06
+#define  RTC_REG_A_RS_512HZ	0x07
+#define  RTC_REG_A_RS_256HZ	0x08
+#define  RTC_REG_A_RS_128HZ	0x09
+#define  RTC_REG_A_RS_64HZ	0x0A
+#define  RTC_REG_A_RS_32HZ	0x0B
+#define  RTC_REG_A_RS_16HZ	0x0C
+#define  RTC_REG_A_RS_8HZ	0x0D
+#define  RTC_REG_A_RS_4HZ	0x0E
+#define  RTC_REG_A_RS_2HZ	0x0F
+
+#define RTC_REG_B		0x0b
+#define  RTC_REG_B_SET		0x80	/* Inhibit update */
+#define  RTC_REG_B_PIE		0x40	/* Periodic Interrupt Enable */
+#define  RTC_REG_B_AIE		0x20	/* Alarm Interrupt Enable */
+#define  RTC_REG_B_UIE		0x10	/* Updated Ended Interrupt Enable */
+#define  RTC_REG_B_SQWE		0x08	/* Square Wave Enable */
+#define  RTC_REG_B_DM		0x04	/* Data Mode */
+#define  RTC_REG_B_BINARY	RTC_REG_B_DM
+#define  RTC_REG_B_BCD		0
+#define  RTC_REG_B_24_12	0x02	/* Hour format */
+#define  RTC_REG_B_24_HOUR	RTC_REG_B_24_12
+#define  RTC_REG_B_12_HOUR	0
+#define  RTC_REG_B_DSE		0x01	/* Daylight Savings Enable */
+
+#define RTC_REG_C		0x0c
+#define  RTC_REG_C_IRQF		0x80	/* Interrupt Request Flag */
+#define  RTC_REG_C_PF		0x40	/* Periodic Interrupt Flag */
+#define  RTC_REG_C_AF		0x20	/* Alarm Interrupt Flag */
+#define  RTC_REG_C_UF		0x10	/* Update Ended Flags */
+
+#define RTC_REG_D		0x0d
+#define  RTC_REG_D_VRT		0x80	/* Valid RAM and Time */
+
+#define RTC_PC_RAM_START	0x0e
+#define RTC_PC_RAM_SIZE		50
+
+#define RTC_BANK0_RAM_START	0x40
+#define RTC_BANK0_RAM_SIZE	0x40
+
+#define RTC_MODEL		0x40
+#define RTC_SERIAL_1		0x41
+#define RTC_SERIAL_2		0x42
+#define RTC_SERIAL_3		0x43
+#define RTC_SERIAL_4		0x44
+#define RTC_SERIAL_5		0x45
+#define RTC_SERIAL_6		0x46
+#define RTC_CRC			0x47
+#define RTC_CENTURY		0x48
+#define RTC_DATE_ALARM		0x49
+#define RTC_REG_4A		0x4a
+#define  RTC_REG_4A_VRT2	0x80
+#define  RTC_REG_4A_INCR	0x40
+#define  RTC_REG_4A_PAB		0x08
+#define  RTC_REG_4A_RF		0x04
+#define RTC_REG_4B		0x4b
+#define RTC_EXT_RAM_ADDRESS	0x50
+#define RTC_EXT_RAM_DATA	0x53
+#define RTC_EXT_RAM_START	0x00
+#define RTC_EXT_RAM_SIZE	0x80
--- /dev/null	Sun Feb 10 02:16:46 2002
+++ dev/ic/ds1687var.h	Mon Feb 11 01:28:57 2002
@@ -0,0 +1,9 @@
+/*	$NetBSD$	*/
+
+struct ds1687_softc {
+	struct device sc_dev;
+	bus_space_tag_t sc_iot;
+	bus_space_handle_t sc_ioh;
+};
+
+todr_chip_handle_t ds1687_attach(struct ds1687_softc *);
--- /dev/null	Sun Feb 10 02:16:46 2002
+++ arch/cats/cats/todclock.c	Sun Feb 10 21:05:48 2002
@@ -0,0 +1,132 @@
+/*      $NetBSD$	*/
+
+/*
+ * Copyright (c) 1992, 1993
+ *      The Regents of the University of California.  All rights reserved.
+ *
+ * This software was developed by the Computer Systems Engineering group
+ * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
+ * contributed to Berkeley.
+ *
+ * All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ *      This product includes software developed by the University of
+ *      California, Lawrence Berkeley Laboratory.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *      This product includes software developed by the University of
+ *      California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *      @(#)clock.c     8.1 (Berkeley) 6/11/93
+ */
+
+#include <sys/param.h>
+#include <sys/kernel.h>
+#include <sys/systm.h>
+#include <sys/device.h>
+
+#include <dev/clock_subr.h>
+
+#include <cats/cats/todclockvar.h>
+
+static todr_chip_handle_t todr_handle;
+
+/*
+ * Common parts of todclock autoconfiguration.
+ */
+void
+todclock_config(handle)
+	todr_chip_handle_t handle;
+{
+
+	if (todr_handle)
+		panic("todclock_config: too many todclocks configured");
+
+	todr_handle = handle;
+}
+
+/*
+ * Set up the system's time, given a `reasonable' time value.
+ */
+void
+inittodr(base)
+        time_t base;
+{
+	int badbase = 0, waszero = (base == 0);
+
+	if (base < 5 * SECYR) {
+		/*
+		 * If base is 0, assume filesystem time is just unknown
+		 * in stead of preposterous. Don't bark.
+		 */
+		if (base != 0)
+			printf("WARNING: preposterous time in file system\n");
+		/* not going to use it anyway, if the chip is readable */
+		/* 1991/07/01	12:00:00 */
+		base = 21*SECYR + 186*SECDAY + SECDAY/2;
+		badbase = 1;
+	}
+
+	if (todr_gettime(todr_handle, (struct timeval *)&time) != 0 ||
+	    time.tv_sec == 0) {
+		printf("WARNING: bad date in battery clock");
+		/*
+		 * Believe the time in the file system for lack of
+		 * anything better, resetting the clock.
+		 */
+		time.tv_sec = base;
+		if (!badbase)
+			resettodr();
+	} else {
+		int deltat = time.tv_sec - base;
+
+		if (deltat < 0)
+			deltat = -deltat;
+		if (waszero || deltat < 2 * SECDAY)
+			return;
+		printf("WARNING: clock %s %d days",
+		    time.tv_sec < base ? "lost" : "gained", deltat / SECDAY);
+	}
+	printf(" -- CHECK AND RESET THE DATE!\n");
+}
+
+/*
+ * Reset the clock based on the current time.
+ * Used when the current clock is preposterous, when the time is changed,
+ * and when rebooting.  Do nothing if the time is not yet known, e.g.,
+ * when crashing during autoconfig.
+ */
+void
+resettodr()
+{
+
+	if (time.tv_sec == 0)
+		return;
+
+	if (todr_settime(todr_handle, (struct timeval *)&time) != 0)
+		printf("resettodr: cannot set time in time-of-day clock\n");
+}
--- /dev/null	Sun Feb 10 02:16:46 2002
+++ arch/cats/cats/todclockvar.h	Sun Feb 10 21:04:23 2002
@@ -0,0 +1,39 @@
+/*	$NetBSD$	*/
+
+/*-
+ * Copyright (c) 1996 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jason R. Thorpe.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *        This product includes software developed by the NetBSD
+ *        Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+void todclock_config(todr_chip_handle_t);