Subject: Re: hardclock(9) for cobalt
To: None <tsutsui@ceres.dti.ne.jp>
From: KIYOHARA Takashi <kiyohara@kk.iij4u.or.jp>
List: port-cobalt
Date: 08/17/2004 11:06:41
----Next_Part(Tue_Aug_17_11:06:41_2004_031)--
Content-Type: Text/Plain; charset=us-ascii
Content-Transfer-Encoding: 7bit


From: Izumi Tsutsui <tsutsui@ceres.dti.ne.jp>
Date: Fri, 13 Aug 2004 14:52:07 +0900

> In article <20040812.213749.74744454.kiyohara@kk.iij4u.or.jp>
> kiyohara@kk.iij4u.or.jp wrote:
> 
> > > - gttmr.c should use bus_space(9) functions rather than
> > >   MIPS_PHYS_TO_KSEG1(). I guess some GT64xxx devices are
> > >   also used by some evbppc.
> > >   (In this case, INTR_CAUSE register can't be mapped here,
> > >    so we have to have a function to access it in gt.c??)
> > 
> > I also thought so. However, since it was the fragmentized small space,
> > bus_space(9) was not used. # 0x10byte + 4bytes.
> > # Also deer There is a crevice between 4 bytes.
> 
> Hmm, now I notice that GT64111 has a weird register map...
> (The timer uses 0x850-0x85c/0x864 but 0x860 is used for DMA Arbiter)
> 
> "In the Perfect world," we should prepare two bus space handles
> for each region and map both of them (like sys/dev/isa/fdc_isa.c),
> but for now the DMA Arbiter register is not used on other place
> and -current bus_space_map(9) function for cobalt doesn't manage
> mapped regions (actually it just returns an address of KSEG0 or KSEG1),
> so maybe it's okay to map whole 0x850-0x864 region in the attachment
> of the timer.

The former is performed. ;-)


> > However, other port If it uses also bus_space(9) It is required.
> 
> Well, it's not required so strictly but still recommended because
> MI APIs will also help readability and maintainability in the future.
> Using "magic" statements like:
> >>	/* XXX */
> >>	*((volatile u_int32_t *)0xb4000c00) =
> >>		(*((volatile u_int32_t *)0xb4000c00) & ~0x6) | 0x2;
> in -current gt_attach() looks weird (especially for non-mips guys),
> doesn't it? ;-)

Of course, that is right. It is made to spread. bus_space(9) It is MI
function which can operate also by different port.
I thought that it was the meaning to say. ;-)

> > Is it good in the following proccess ?
> > 
> >   1. attach gt
> >   2.   attach gttmr
> >   3.   disable timer0
> >   4.   set func pointers
> >   5. clear INTR_CAUSE register
> 
> Looks good. Maybe 5. could be done by calling a special
> function in gt.c.

Is doing by gt_attach() useless?

However, now, gt(4) is not very well. Should gt(4) also be corrected?
--
kiyohara


----Next_Part(Tue_Aug_17_11:06:41_2004_031)--
Content-Type: Text/Plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="gttmr.2.diff"

Index: arch/cobalt/cobalt/clock.c
===================================================================
RCS file: /cvsroot/src/sys/arch/cobalt/cobalt/clock.c,v
retrieving revision 1.7
diff -c -r1.7 clock.c
*** arch/cobalt/cobalt/clock.c	5 Jul 2004 07:28:45 -0000	1.7
--- arch/cobalt/cobalt/clock.c	17 Aug 2004 01:29:21 -0000
***************
*** 34,39 ****
--- 34,41 ----
  #include <sys/kernel.h>
  #include <sys/device.h>
  
+ #include <machine/autoconf.h>
+ 
  #include <dev/clock_subr.h>
  
  #include <dev/ic/mc146818reg.h>
***************
*** 46,51 ****
--- 48,55 ----
  void
  cpu_initclocks()
  {
+ 	/* start timer0 */
+ 	timer_enable();
  
  	return;
  }
Index: arch/cobalt/cobalt/machdep.c
===================================================================
RCS file: /cvsroot/src/sys/arch/cobalt/cobalt/machdep.c,v
retrieving revision 1.51
diff -c -r1.51 machdep.c
*** arch/cobalt/cobalt/machdep.c	2 Apr 2004 23:18:09 -0000	1.51
--- arch/cobalt/cobalt/machdep.c	17 Aug 2004 01:29:21 -0000
***************
*** 365,371 ****
  
  	*tvp = time;
  
! 	counter0 = *(volatile u_int32_t *)MIPS_PHYS_TO_KSEG1(0x14000850);
  
  	/*
  	 * XXX
--- 365,371 ----
  
  	*tvp = time;
  
! 	counter0 = timer_read();
  
  	/*
  	 * XXX
Index: arch/cobalt/conf/GENERIC
===================================================================
RCS file: /cvsroot/src/sys/arch/cobalt/conf/GENERIC,v
retrieving revision 1.61
diff -c -r1.61 GENERIC
*** arch/cobalt/conf/GENERIC	15 Jul 2004 03:53:46 -0000	1.61
--- arch/cobalt/conf/GENERIC	17 Aug 2004 01:29:22 -0000
***************
*** 151,156 ****
--- 151,157 ----
  panel0 		at mainbus? addr 0x1f000000
  
  gt0 		at mainbus? addr 0x14000000
+ gttmr0		at gt0
  
  pci* 		at gt0
  pchb* 		at pci? dev ? function ?
Index: arch/cobalt/conf/files.cobalt
===================================================================
RCS file: /cvsroot/src/sys/arch/cobalt/conf/files.cobalt,v
retrieving revision 1.19
diff -c -r1.19 files.cobalt
*** arch/cobalt/conf/files.cobalt	17 Oct 2003 18:20:10 -0000	1.19
--- arch/cobalt/conf/files.cobalt	17 Aug 2004 01:29:22 -0000
***************
*** 19,28 ****
  attach panel at mainbus
  file arch/cobalt/dev/panel.c		panel
  
! device gt: pcibus
  attach gt at mainbus
  file arch/cobalt/dev/gt.c		gt
  
  file arch/cobalt/cobalt/autoconf.c
  file arch/cobalt/cobalt/bus.c
  file arch/cobalt/cobalt/clock.c
--- 19,33 ----
  attach panel at mainbus
  file arch/cobalt/dev/panel.c		panel
  
! define gt64111 {}
! device gt: gt64111, pcibus
  attach gt at mainbus
  file arch/cobalt/dev/gt.c		gt
  
+ device gttmr
+ attach gttmr at gt64111
+ file arch/cobalt/dev/gttmr.c		gttmr
+ 
  file arch/cobalt/cobalt/autoconf.c
  file arch/cobalt/cobalt/bus.c
  file arch/cobalt/cobalt/clock.c
Index: arch/cobalt/dev/gt.c
===================================================================
RCS file: /cvsroot/src/sys/arch/cobalt/dev/gt.c,v
retrieving revision 1.9
diff -c -r1.9 gt.c
*** arch/cobalt/dev/gt.c	15 Jul 2003 01:29:23 -0000	1.9
--- arch/cobalt/dev/gt.c	17 Aug 2004 01:29:22 -0000
***************
*** 42,47 ****
--- 42,50 ----
  #include <dev/pci/pcivar.h>
  #include "pci.h"
  
+ #include <dev/ic/gt64111reg.h>
+ #include <dev/ic/gt64111var.h>
+ 
  struct gt_softc {
  	struct device	sc_dev;
  };
***************
*** 76,81 ****
--- 86,92 ----
  	void *aux;
  {
  	struct pcibus_attach_args pba;
+ 	struct gt_attach_args gta;
  
  	printf("\n");
  
***************
*** 83,88 ****
--- 98,113 ----
  	*((volatile u_int32_t *)0xb4000c00) =
  		(*((volatile u_int32_t *)0xb4000c00) & ~0x6) | 0x2;
  
+ 	/*
+ 	 * timer0 is working at the time of BIOS. It might be got blocked
+ 	 * and timer interruption may already have generated it. A timer0
+ 	 * interruption must be cleared.
+ 	 */
+ 	gta.name = "gttmr";
+ 	gta.bst = (bus_space_tag_t)NULL;
+ 	config_found(self, &gta, gt_print);
+ 	*(u_int32_t *)MIPS_PHYS_TO_KSEG1(INTR_CAUSE) &= ~T0EXP;
+ 
  #if NPCI > 0
  	pba.pba_busname = "pci";
  	pba.pba_dmat = &pci_bus_dma_tag;
Index: arch/cobalt/include/autoconf.h
===================================================================
RCS file: /cvsroot/src/sys/arch/cobalt/include/autoconf.h,v
retrieving revision 1.2
diff -c -r1.2 autoconf.h
*** arch/cobalt/include/autoconf.h	5 May 2000 03:27:22 -0000	1.2
--- arch/cobalt/include/autoconf.h	17 Aug 2004 01:29:22 -0000
***************
*** 27,32 ****
--- 27,36 ----
  
  #include <machine/bus.h>
  
+ void (*timer_enable)(void);
+ void (*timer_disable)(void);
+ u_int32_t (*timer_read)(void);
+ 
  struct mainbus_attach_args {
  	char		*ma_name;
  	unsigned long	ma_addr;

----Next_Part(Tue_Aug_17_11:06:41_2004_031)--
Content-Type: Text/Plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="gttmr.c.2.diff"

*** arch/cobalt/dev/gttmr.c.orig	Wed Aug 11 02:13:24 2004
--- arch/cobalt/dev/gttmr.c	Tue Aug 17 03:05:19 2004
***************
*** 0 ****
--- 1,128 ----
+ /*
+  * Copyright (c) 2003
+  *     KIYOHARA Takashi.  All rights reserved.
+  *
+  * 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.
+  *
+  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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/param.h>
+ #include <sys/device.h>
+ #include <sys/errno.h>
+ #include <sys/systm.h>
+ 
+ #include <machine/autoconf.h>
+ #include <machine/bus.h>
+ 
+ #include <dev/ic/gt64111reg.h>
+ #include <dev/ic/gt64111var.h>
+ 
+ #define TIMER0_INIT_VALUE	500000
+ 
+ 
+ static struct gttmr_core {
+ 	bus_space_tag_t bst;
+ 	bus_space_handle_t tmr_bsh;
+ 	bus_space_handle_t tmrc_bsh;
+ } tmr;
+ struct gttmr_softc {
+ 	struct device   sc_dev;
+ 
+ 	struct gttmr_core *tmr;
+ };
+ 
+ static int gttmr_match(struct device *, struct cfdata *, void *);
+ static void gttmr_attach(struct device *, struct device *, void *);
+ u_int32_t timer0_read(void);
+ void timer0_enable(void);
+ void timer0_disable(void);
+ 
+ 
+ CFATTACH_DECL(gttmr, sizeof(struct gttmr_softc),
+ 	gttmr_match, gttmr_attach, NULL, NULL);
+ 
+ 
+ void
+ timer0_enable()
+ {
+ 	int tcc;
+ 
+ 	tcc = bus_space_read_4(tmr.bst, tmr.tmrc_bsh, 0);
+ 	tcc |= ENTC0;
+ 	bus_space_write_4(tmr.bst, tmr.tmrc_bsh, 0, tcc);
+ }
+ 
+ void
+ timer0_disable()
+ {
+ 	int tcc;
+ 
+ 	tcc = bus_space_read_4(tmr.bst, tmr.tmrc_bsh, 0);
+ 	tcc &= ~ENTC0;
+ 	bus_space_write_4(tmr.bst, tmr.tmrc_bsh, 0, tcc);
+ }
+ 
+ u_int32_t
+ timer0_read()
+ {
+ 	return (bus_space_read_4(tmr.bst, tmr.tmr_bsh, TIMER_COUNT_T0));
+ }
+ 
+ static int
+ gttmr_match(struct device *parent, struct cfdata *match, void *aux)
+ {
+ 	char **name = aux;
+ 
+ 	if (strcmp(*name, "gttmr") == 0)
+ 		return (1);
+ 	return (0);
+ }
+ 
+ static void
+ gttmr_attach(struct device *parent, struct device *self, void *aux)
+ {
+ 	struct gttmr_softc *sc = (struct gttmr_softc *)self;
+ 	struct gt_attach_args *gta = (struct gt_attach_args *)aux;
+ 
+ 	printf(": 32/24bit-Wide Timer/Counter on GT-64111\n");
+ 
+ 	tmr.bst = gta->bst;
+ 	if (bus_space_map(tmr.bst, TIMER_COUNT, 16, 0, &tmr.tmr_bsh)) {
+ 		printf("%s: unable to map space\n", sc->sc_dev.dv_xname);
+ 		return;
+ 	}
+ 	if (bus_space_map(tmr.bst, TIMER_COUNT_CTRL, 4, 0, &tmr.tmrc_bsh)) {
+ 		printf("%s: unable to map control space\n",
+ 		    sc->sc_dev.dv_xname);
+ 		bus_space_unmap(tmr.bst, tmr.tmrc_bsh, 16);
+ 		return;
+ 	}
+ 	sc->tmr = &tmr;
+ 
+ 	timer0_disable();
+ 
+ 	/* initialize timer0 */
+ 	bus_space_write_4(
+ 	    tmr.bst, tmr.tmr_bsh, TIMER_COUNT_T0, TIMER0_INIT_VALUE);
+ 
+ 	timer_enable = timer0_enable;
+ 	timer_disable = timer0_disable;
+ 	timer_read = timer0_read;
+ }
*** dev/ic/gt64111reg.h.orig	Tue Aug 17 10:42:26 2004
--- dev/ic/gt64111reg.h	Mon Aug 16 17:01:12 2004
***************
*** 0 ****
--- 1,42 ----
+ /*
+  * Copyright (c) 2003
+  *     KIYOHARA Takashi.  All rights reserved.
+  *
+  * 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.
+  *
+  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 TIMER_COUNT		0x14000850
+ #define TIMER_COUNT_T0		0x0
+ #define TIMER_COUNT_T1		0x4
+ #define TIMER_COUNT_T2		0x8
+ #define TIMER_COUNT_T3		0xc
+ #define TIMER_COUNT_CTRL	0x14000864
+ #define ENTC0	(1 << 0)
+ #define TCSEL0	(1 << 1)
+ #define ENTC1	(1 << 2)
+ #define TCSEL1	(1 << 3)
+ #define ENTC2	(1 << 4)
+ #define TCSEL2	(1 << 5)
+ #define ENTC3	(1 << 6)
+ #define TCSEL3	(1 << 7)
+ 
+ #define INTR_CAUSE		0x14000c18
+ #define T0EXP   (1 << 8)
*** dev/ic/gt64111var.h.orig	Tue Aug 17 10:42:41 2004
--- dev/ic/gt64111var.h	Tue Aug 17 02:57:37 2004
***************
*** 0 ****
--- 1,29 ----
+ /*
+  * Copyright (c) 2003
+  *     KIYOHARA Takashi.  All rights reserved.
+  *
+  * 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.
+  *
+  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
+  */
+ 
+ struct gt_attach_args {
+ 	char		*name;		/* device name */
+ 	bus_space_tag_t	bst;		/* bus space tag */
+ };

----Next_Part(Tue_Aug_17_11:06:41_2004_031)----