Subject: Re: cleanup todclock attachments (Re: CVS commit: src/sys/arch/sparc64)
To: None <port-sparc64@NetBSD.org>
From: Izumi Tsutsui <tsutsui@ceres.dti.ne.jp>
List: port-sparc64
Date: 10/10/2006 21:52:00
I wrote:

> > this leaves a bunch of stuff in arch/sparc64/sparc64/clock.c?
> > looks pretty good.
> 
> Only mk48txx TOD functions (match/attach etc.) are moved into
> (new) dev/mkclock.c in my patch. Then clock.c contains stuff for
> hardclock(9), statclock(9), cpu_initclocks(9), timecounter(9) etc.

The attached is a new diff which keeps device names in config files.
If there is no objection, I'll commit it in a few days.

---
Index: conf/GENERIC
===================================================================
RCS file: /cvsroot/src/sys/arch/sparc64/conf/GENERIC,v
retrieving revision 1.70
diff -u -r1.70 GENERIC
--- conf/GENERIC	6 Oct 2006 08:44:59 -0000	1.70
+++ conf/GENERIC	10 Oct 2006 12:46:59 -0000
@@ -260,10 +260,12 @@
 bpp*	at sbus? slot ? offset ?		# parallel port
 lpt*	at ebus?				# parallel port
 
-## Mostek clock found on 4/300, sun4c, sun4m and sun4u systems.
-## The Mostek clock NVRAM is the "eeprom" on sun4/300 systems.
+## Mostek clock found on sbus on Ultra-1,2 systems
+## and found on ebus on Ultra-5 and other systems.
 clock*	at sbus? slot ? offset ?
 clock*	at ebus?
+
+## DS1287 compatible clock found on ebus on Netra X1 and other systems.
 rtc*	at ebus?
 
 ## Timer chip found on 4/300, sun4c, sun4m and (some) sun4u systems.
Index: conf/files.sparc64
===================================================================
RCS file: /cvsroot/src/sys/arch/sparc64/conf/files.sparc64,v
retrieving revision 1.106
diff -u -r1.106 files.sparc64
--- conf/files.sparc64	10 Oct 2006 12:43:11 -0000	1.106
+++ conf/files.sparc64	10 Oct 2006 12:46:59 -0000
@@ -41,8 +41,9 @@
 file	arch/sparc64/dev/ebus.c			ebus
 
 device clock: mk48txx
-attach clock at sbus with clock_sbus
-attach clock at ebus with clock_ebus
+attach clock at sbus with mkclock_sbus
+attach clock at ebus with mkclock_ebus
+file	arch/sparc64/dev/mkclock.c		clock
 
 device rtc: mc146818
 attach rtc at ebus with rtc_ebus
Index: dev/rtc.c
===================================================================
RCS file: /cvsroot/src/sys/arch/sparc64/dev/rtc.c,v
retrieving revision 1.1
diff -u -r1.1 rtc.c
--- dev/rtc.c	9 Oct 2006 03:34:56 -0000	1.1
+++ dev/rtc.c	10 Oct 2006 12:46:59 -0000
@@ -77,11 +77,11 @@
 #include <dev/ebus/ebusreg.h>
 #include <dev/ebus/ebusvar.h>
 
-static int	clockmatch_rtc(struct device *, struct cfdata *, void *);
-static void	clockattach_rtc(struct device *, struct device *, void *);
+static int	rtc_ebus_match(struct device *, struct cfdata *, void *);
+static void	rtc_ebus_attach(struct device *, struct device *, void *);
 
 CFATTACH_DECL(rtc_ebus, sizeof(struct mc146818_softc),
-    clockmatch_rtc, clockattach_rtc, NULL, NULL);
+    rtc_ebus_match, rtc_ebus_attach, NULL, NULL);
 
 u_int rtc_read_reg(struct mc146818_softc *, u_int);
 void rtc_write_reg(struct mc146818_softc *, u_int, u_int);
@@ -89,7 +89,7 @@
 void rtc_setcent(struct mc146818_softc *, u_int);
 
 static int
-clockmatch_rtc(struct device *parent, struct cfdata *cf, void *aux)
+rtc_ebus_match(struct device *parent, struct cfdata *cf, void *aux)
 {
 	struct ebus_attach_args *ea = aux;
 
@@ -129,7 +129,7 @@
 
 /* ARGSUSED */
 static void
-clockattach_rtc(struct device *parent, struct device *self, void *aux)
+rtc_ebus_attach(struct device *parent, struct device *self, void *aux)
 {
 	struct mc146818_softc *sc = (void *)self;
 	struct ebus_attach_args *ea = aux;
Index: sparc64/clock.c
===================================================================
RCS file: /cvsroot/src/sys/arch/sparc64/sparc64/clock.c,v
retrieving revision 1.83
diff -u -r1.83 clock.c
--- sparc64/clock.c	10 Oct 2006 12:43:11 -0000	1.83
+++ sparc64/clock.c	10 Oct 2006 12:46:59 -0000
@@ -87,10 +87,6 @@
 #include <machine/cpu.h>
 #include <machine/cpu_counter.h>
 
-#include <dev/clock_subr.h>
-#include <dev/ic/mk48txxreg.h>
-#include <dev/ic/mk48txxvar.h>
-
 #include <sparc64/sparc64/intreg.h>
 #include <sparc64/sparc64/timerreg.h>
 #include <sparc64/dev/iommureg.h>
@@ -121,24 +117,6 @@
 static struct intrhand level14 = { .ih_fun = statintr };
 static struct intrhand schedint = { .ih_fun = schedintr };
 
-/*
- * clock (eeprom) attaches at the sbus or the ebus (PCI)
- */
-static int	clockmatch_sbus(struct device *, struct cfdata *, void *);
-static void	clockattach_sbus(struct device *, struct device *, void *);
-static int	clockmatch_ebus(struct device *, struct cfdata *, void *);
-static void	clockattach_ebus(struct device *, struct device *, void *);
-static void	clockattach(struct mk48txx_softc *, int);
-
-
-CFATTACH_DECL(clock_sbus, sizeof(struct mk48txx_softc),
-    clockmatch_sbus, clockattach_sbus, NULL, NULL);
-
-CFATTACH_DECL(clock_ebus, sizeof(struct mk48txx_softc),
-    clockmatch_ebus, clockattach_ebus, NULL, NULL);
-
-extern struct cfdriver clock_cd;
-
 static int	timermatch(struct device *, struct cfdata *, void *);
 static void	timerattach(struct device *, struct device *, void *);
 
@@ -147,166 +125,11 @@
 CFATTACH_DECL(timer, sizeof(struct device),
     timermatch, timerattach, NULL, NULL);
 
-int clock_wenable(struct todr_chip_handle *, int);
 struct chiptime;
 void stopcounter(struct timer_4u *);
 
 int timerblurb = 10; /* Guess a value; used before clock is attached */
 
-/*
- * The OPENPROM calls the clock the "eeprom", so we have to have our
- * own special match function to call it the "clock".
- */
-static int
-clockmatch_sbus(struct device *parent, struct cfdata *cf, void *aux)
-{
-	struct sbus_attach_args *sa = aux;
-
-	return (strcmp("eeprom", sa->sa_name) == 0);
-}
-
-static int
-clockmatch_ebus(struct device *parent, struct cfdata *cf, void *aux)
-{
-	struct ebus_attach_args *ea = aux;
-
-	return (strcmp("eeprom", ea->ea_name) == 0);
-}
-
-/*
- * Attach a clock (really `eeprom') to the sbus or ebus.
- *
- * We ignore any existing virtual address as we need to map
- * this read-only and make it read-write only temporarily,
- * whenever we read or write the clock chip.  The clock also
- * contains the ID ``PROM'', and I have already had the pleasure
- * of reloading the CPU type, Ethernet address, etc, by hand from
- * the console FORTH interpreter.  I intend not to enjoy it again.
- *
- * the MK48T02 is 2K.  the MK48T08 is 8K, and the MK48T59 is
- * supposed to be identical to it.
- *
- * This is *UGLY*!  We probably have multiple mappings.  But I do
- * know that this all fits inside an 8K page, so I'll just map in
- * once.
- *
- * What we really need is some way to record the bus attach args
- * so we can call *_bus_map() later with BUS_SPACE_MAP_READONLY
- * or not to write enable/disable the device registers.  This is
- * a non-trivial operation.  
- */
-
-/* ARGSUSED */
-static void
-clockattach_sbus(struct device *parent, struct device *self, void *aux)
-{
-	struct mk48txx_softc *sc = (void *)self;
-	struct sbus_attach_args *sa = aux;
-	int sz;
-
-	sc->sc_bst = sa->sa_bustag;
-
-	/* use sa->sa_regs[0].size? */
-	sz = 8192;
-
-	if (sbus_bus_map(sc->sc_bst,
-			 sa->sa_slot,
-			 (sa->sa_offset & ~(PAGE_SIZE - 1)),
-			 sz,
-			 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_READONLY,
-			 &sc->sc_bsh) != 0) {
-		printf("%s: can't map register\n", self->dv_xname);
-		return;
-	}
-	clockattach(sc, sa->sa_node);
-
-	/* Save info for the clock wenable call. */
-	sc->sc_handle.todr_setwen = clock_wenable;
-
-	todr_attach(&sc->sc_handle);
-}
-
-/*
- * Write en/dis-able clock registers.  We coordinate so that several
- * writers can run simultaneously.
- */
-int
-clock_wenable(struct todr_chip_handle *handle, int onoff)
-{
-	struct mk48txx_softc *sc;
-	vm_prot_t prot;
-	vaddr_t va;
-	int s, err = 0;
-	static int writers;
-
-	s = splhigh();
-	if (onoff)
-		prot = writers++ == 0 ? VM_PROT_READ|VM_PROT_WRITE : 0;
-	else
-		prot = --writers == 0 ? VM_PROT_READ : 0;
-	splx(s);
-	if (prot == VM_PROT_NONE) {
-		return 0;
-	}
-	sc = handle->cookie;
-	va = (vaddr_t)bus_space_vaddr(sc->sc_bst, sc->sc_bsh);
-	if (va == 0UL) {
-		printf("clock_wenable: WARNING -- cannot get va\n");
-		return EIO;
-	}
-	pmap_kprotect(va, prot);
-	return (err);
-}
-
-
-/* ARGSUSED */
-static void
-clockattach_ebus(struct device *parent, struct device *self, void *aux)
-{
-	struct mk48txx_softc *sc = (void *)self;
-	struct ebus_attach_args *ea = aux;
-	int sz;
-
-	sc->sc_bst = ea->ea_bustag;
-
-	/* hard code to 8K? */
-	sz = ea->ea_reg[0].size;
-
-	if (bus_space_map(sc->sc_bst,
-			 EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
-			 sz,
-			 BUS_SPACE_MAP_LINEAR,
-			 &sc->sc_bsh) != 0) {
-		printf("%s: can't map register\n", self->dv_xname);
-		return;
-	}
-	clockattach(sc, ea->ea_node);
-
-	/* Save info for the clock wenable call. */
-	sc->sc_handle.todr_setwen = clock_wenable;
-
-	todr_attach(&sc->sc_handle);
-}
-
-
-static void
-clockattach(struct mk48txx_softc *sc, int node)
-{
-
-	sc->sc_model = prom_getpropstring(node, "model");
-
-#ifdef DIAGNOSTIC
-	if (sc->sc_model == NULL)
-		panic("clockattach: no model property");
-#endif
-
-	/* Our TOD clock year 0 is 1968 */
-	sc->sc_year0 = 1968;
-	mk48txx_attach(sc);
-
-	printf("\n");
-}
-
 static u_int timer_get_timecount(struct timecounter *);
 
 /*
--- /dev/null	2006-10-10 21:13:27.000000000 +0900
+++ dev/mkclock.c	2006-10-10 20:06:52.000000000 +0900
@@ -0,0 +1,256 @@
+/*	$NetBSD: clock.c,v 1.81 2006/10/09 03:34:56 mrg Exp $ */
+
+/*
+ * Copyright (c) 1992, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ * Copyright (c) 1994 Gordon W. Ross
+ * Copyright (c) 1993 Adam Glass
+ * Copyright (c) 1996 Paul Kranenburg
+ * Copyright (c) 1996
+ * 	The President and Fellows of Harvard College. 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 Harvard University.
+ *	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.
+ *	This product includes software developed by Paul Kranenburg.
+ *	This product includes software developed by Harvard University.
+ * 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/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.81 2006/10/09 03:34:56 mrg Exp $");
+
+/*    
+ * Clock driver for 'mkclock' - Mostek MK48Txx TOD clock.
+ */
+
+#include <sys/param.h>
+#include <sys/kernel.h>
+#include <sys/device.h>
+#include <sys/proc.h>
+#include <sys/resourcevar.h>
+#include <sys/malloc.h>
+#include <sys/systm.h>
+
+#include <uvm/uvm_extern.h>
+
+#include <machine/bus.h>
+#include <machine/autoconf.h>
+#include <machine/eeprom.h>
+#include <machine/cpu.h>
+
+#include <dev/clock_subr.h>
+#include <dev/ic/mk48txxreg.h>
+#include <dev/ic/mk48txxvar.h>
+
+#include <sparc64/dev/iommureg.h>
+#include <sparc64/dev/sbusreg.h>
+#include <dev/sbus/sbusvar.h>
+#include <dev/ebus/ebusreg.h>
+#include <dev/ebus/ebusvar.h>
+
+/*
+ * clock (eeprom) attaches at the sbus or the ebus (PCI)
+ */
+static int	mkclock_sbus_match(struct device *, struct cfdata *, void *);
+static void	mkclock_sbus_attach(struct device *, struct device *, void *);
+
+static int	mkclock_ebus_match(struct device *, struct cfdata *, void *);
+static void	mkclock_ebus_attach(struct device *, struct device *, void *);
+
+static void	mkclock_attach(struct mk48txx_softc *, int);
+
+static int	mkclock_wenable(struct todr_chip_handle *, int);
+
+
+CFATTACH_DECL(mkclock_sbus, sizeof(struct mk48txx_softc),
+    mkclock_sbus_match, mkclock_sbus_attach, NULL, NULL);
+
+CFATTACH_DECL(mkclock_ebus, sizeof(struct mk48txx_softc),
+    mkclock_ebus_match, mkclock_ebus_attach, NULL, NULL);
+
+/*
+ * The OPENPROM calls the clock the "eeprom", so we have to have our
+ * own special match function to call it the "clock".
+ */
+static int
+mkclock_sbus_match(struct device *parent, struct cfdata *cf, void *aux)
+{
+	struct sbus_attach_args *sa = aux;
+
+	return (strcmp("eeprom", sa->sa_name) == 0);
+}
+
+static int
+mkclock_ebus_match(struct device *parent, struct cfdata *cf, void *aux)
+{
+	struct ebus_attach_args *ea = aux;
+
+	return (strcmp("eeprom", ea->ea_name) == 0);
+}
+
+/*
+ * Attach a clock (really `eeprom') to the sbus or ebus.
+ *
+ * We ignore any existing virtual address as we need to map
+ * this read-only and make it read-write only temporarily,
+ * whenever we read or write the clock chip.  The clock also
+ * contains the ID ``PROM'', and I have already had the pleasure
+ * of reloading the CPU type, Ethernet address, etc, by hand from
+ * the console FORTH interpreter.  I intend not to enjoy it again.
+ *
+ * the MK48T02 is 2K.  the MK48T08 is 8K, and the MK48T59 is
+ * supposed to be identical to it.
+ *
+ * This is *UGLY*!  We probably have multiple mappings.  But I do
+ * know that this all fits inside an 8K page, so I'll just map in
+ * once.
+ *
+ * What we really need is some way to record the bus attach args
+ * so we can call *_bus_map() later with BUS_SPACE_MAP_READONLY
+ * or not to write enable/disable the device registers.  This is
+ * a non-trivial operation.  
+ */
+
+/* ARGSUSED */
+static void
+mkclock_sbus_attach(struct device *parent, struct device *self, void *aux)
+{
+	struct mk48txx_softc *sc = (void *)self;
+	struct sbus_attach_args *sa = aux;
+	int sz;
+
+	sc->sc_bst = sa->sa_bustag;
+
+	/* use sa->sa_regs[0].size? */
+	sz = 8192;
+
+	if (sbus_bus_map(sc->sc_bst,
+			 sa->sa_slot,
+			 (sa->sa_offset & ~(PAGE_SIZE - 1)),
+			 sz,
+			 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_READONLY,
+			 &sc->sc_bsh) != 0) {
+		printf("%s: can't map register\n", self->dv_xname);
+		return;
+	}
+	mkclock_attach(sc, sa->sa_node);
+}
+
+
+/* ARGSUSED */
+static void
+mkclock_ebus_attach(struct device *parent, struct device *self, void *aux)
+{
+	struct mk48txx_softc *sc = (void *)self;
+	struct ebus_attach_args *ea = aux;
+	int sz;
+
+	sc->sc_bst = ea->ea_bustag;
+
+	/* hard code to 8K? */
+	sz = ea->ea_reg[0].size;
+
+	if (bus_space_map(sc->sc_bst,
+			 EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
+			 sz,
+			 BUS_SPACE_MAP_LINEAR,
+			 &sc->sc_bsh) != 0) {
+		printf("%s: can't map register\n", self->dv_xname);
+		return;
+	}
+	mkclock_attach(sc, ea->ea_node);
+}
+
+
+static void
+mkclock_attach(struct mk48txx_softc *sc, int node)
+{
+
+	sc->sc_model = prom_getpropstring(node, "model");
+
+#ifdef DIAGNOSTIC
+	if (sc->sc_model == NULL)
+		panic("clockattach: no model property");
+#endif
+
+	/* Our TOD clock year 0 is 1968 */
+	sc->sc_year0 = 1968;
+	mk48txx_attach(sc);
+
+	printf("\n");
+
+	/* Save info for the clock wenable call. */
+	sc->sc_handle.todr_setwen = mkclock_wenable;
+
+	todr_attach(&sc->sc_handle);
+}
+
+/*
+ * Write en/dis-able clock registers.  We coordinate so that several
+ * writers can run simultaneously.
+ */
+static int
+mkclock_wenable(struct todr_chip_handle *handle, int onoff)
+{
+	struct mk48txx_softc *sc;
+	vm_prot_t prot;
+	vaddr_t va;
+	int s, err = 0;
+	static int writers;
+
+	s = splhigh();
+	if (onoff)
+		prot = writers++ == 0 ? VM_PROT_READ|VM_PROT_WRITE : 0;
+	else
+		prot = --writers == 0 ? VM_PROT_READ : 0;
+	splx(s);
+	if (prot == VM_PROT_NONE) {
+		return 0;
+	}
+	sc = handle->cookie;
+	va = (vaddr_t)bus_space_vaddr(sc->sc_bst, sc->sc_bsh);
+	if (va == 0UL) {
+		printf("clock_wenable: WARNING -- cannot get va\n");
+		return EIO;
+	}
+	pmap_kprotect(va, prot);
+	return (err);
+}

---
Izumi Tsutsui