Home | History | Annotate | Line # | Download | only in ebus
clock_ebus.c revision 1.1
      1  1.1  pooka /*	$NetBSD: clock_ebus.c,v 1.1 2011/01/26 01:18:50 pooka Exp $	*/
      2  1.1  pooka 
      3  1.1  pooka /*-
      4  1.1  pooka  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  1.1  pooka  * All rights reserved.
      6  1.1  pooka  *
      7  1.1  pooka  * This code was written by Alessandro Forin and Neil Pittman
      8  1.1  pooka  * at Microsoft Research and contributed to The NetBSD Foundation
      9  1.1  pooka  * by Microsoft Corporation.
     10  1.1  pooka  *
     11  1.1  pooka  * Redistribution and use in source and binary forms, with or without
     12  1.1  pooka  * modification, are permitted provided that the following conditions
     13  1.1  pooka  * are met:
     14  1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     15  1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     16  1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  pooka  *    documentation and/or other materials provided with the distribution.
     19  1.1  pooka  *
     20  1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  1.1  pooka  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  1.1  pooka  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  1.1  pooka  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  1.1  pooka  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  1.1  pooka  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  1.1  pooka  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  1.1  pooka  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  1.1  pooka  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  1.1  pooka  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  1.1  pooka  * POSSIBILITY OF SUCH DAMAGE.
     31  1.1  pooka  */
     32  1.1  pooka 
     33  1.1  pooka #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     34  1.1  pooka __KERNEL_RCSID(0, "$NetBSD: clock_ebus.c,v 1.1 2011/01/26 01:18:50 pooka Exp $");
     35  1.1  pooka 
     36  1.1  pooka #include <sys/param.h>
     37  1.1  pooka #include <sys/kernel.h>
     38  1.1  pooka #include <sys/device.h>
     39  1.1  pooka #include <sys/systm.h>
     40  1.1  pooka #include <sys/timetc.h>
     41  1.1  pooka 
     42  1.1  pooka #include <dev/clock_subr.h>
     43  1.1  pooka 
     44  1.1  pooka #include <emips/ebus/ebusvar.h>
     45  1.1  pooka #include <emips/emips/machdep.h>
     46  1.1  pooka #include <machine/emipsreg.h>
     47  1.1  pooka 
     48  1.1  pooka /*
     49  1.1  pooka  * Device softc
     50  1.1  pooka  */
     51  1.1  pooka struct eclock_softc {
     52  1.1  pooka 	struct device sc_dev;
     53  1.1  pooka 	struct _Tc *sc_dp;
     54  1.1  pooka     uint32_t reload;
     55  1.1  pooka     struct timecounter sc_tc;
     56  1.1  pooka #ifdef __HAVE_GENERIC_TODR
     57  1.1  pooka     struct todr_chip_handle sc_todr;
     58  1.1  pooka #endif
     59  1.1  pooka };
     60  1.1  pooka 
     61  1.1  pooka static int	eclock_ebus_match (struct device *, struct cfdata *, void *);
     62  1.1  pooka static void	eclock_ebus_attach (struct device *, struct device *, void *);
     63  1.1  pooka 
     64  1.1  pooka CFATTACH_DECL(eclock_ebus, sizeof (struct eclock_softc),
     65  1.1  pooka     eclock_ebus_match, eclock_ebus_attach, NULL, NULL);
     66  1.1  pooka 
     67  1.1  pooka        void	eclock_init(struct device *);
     68  1.1  pooka static void	__eclock_init(struct device *);
     69  1.1  pooka static int	eclock_gettime(struct todr_chip_handle *,
     70  1.1  pooka                            struct timeval *);
     71  1.1  pooka static int	eclock_settime(struct todr_chip_handle *,
     72  1.1  pooka                            struct timeval *);
     73  1.1  pooka static int  eclock_ebus_intr(void *cookie, void *f);
     74  1.1  pooka static u_int eclock_counter(struct timecounter *tc);
     75  1.1  pooka 
     76  1.1  pooka struct device *clockdev = NULL; /* BUGBUG resolve the gap between cpu_initclocks() and eclock_init(x) */
     77  1.1  pooka 
     78  1.1  pooka void
     79  1.1  pooka eclock_init(struct device *dev)
     80  1.1  pooka {
     81  1.1  pooka     if (dev == NULL)
     82  1.1  pooka         dev = clockdev;
     83  1.1  pooka     if (dev == NULL)
     84  1.1  pooka         panic("eclock_init");
     85  1.1  pooka     __eclock_init(dev);
     86  1.1  pooka }
     87  1.1  pooka 
     88  1.1  pooka static void
     89  1.1  pooka __eclock_init(struct device *dev)
     90  1.1  pooka {
     91  1.1  pooka 	struct eclock_softc *sc = (struct eclock_softc *)dev;
     92  1.1  pooka     struct _Tc *tc = sc->sc_dp;
     93  1.1  pooka     uint32_t reload = 10*1000000; /* 1sec in 100ns units (10MHz clock) */
     94  1.1  pooka 
     95  1.1  pooka     /* Compute reload according to whatever value passed in, Warn if fractional */
     96  1.1  pooka     if (hz > 1) {
     97  1.1  pooka         uint32_t r = reload / hz;
     98  1.1  pooka         if ((r * hz) != reload)
     99  1.1  pooka             printf("%s: %d Hz clock will cause roundoffs with 10MHz xtal (%d)\n",
    100  1.1  pooka                    sc->sc_dev.dv_xname, hz, reload - (r * hz));
    101  1.1  pooka 		reload = r;
    102  1.1  pooka 	}
    103  1.1  pooka 
    104  1.1  pooka     sc->reload = reload;
    105  1.1  pooka 
    106  1.1  pooka     /* Start the counter */
    107  1.1  pooka     tc->DownCounterHigh = 0;
    108  1.1  pooka     tc->DownCounter = sc->reload;
    109  1.1  pooka     tc->Control = TCCT_ENABLE | TCCT_INT_ENABLE;
    110  1.1  pooka }
    111  1.1  pooka 
    112  1.1  pooka /*
    113  1.1  pooka  * Get the time of day, based on the clock's value and/or the base value.
    114  1.1  pooka  * NB: At 10MHz, our 64bits FreeRunning is worth 58,426 years.
    115  1.1  pooka  */
    116  1.1  pooka 
    117  1.1  pooka extern u_quad_t __qdivrem(u_quad_t uq, u_quad_t vq, u_quad_t *arq);
    118  1.1  pooka 
    119  1.1  pooka 
    120  1.1  pooka static int
    121  1.1  pooka eclock_gettime(struct todr_chip_handle *todr, struct timeval *tv)
    122  1.1  pooka {
    123  1.1  pooka 	struct eclock_softc *sc = (struct eclock_softc *) todr->cookie;
    124  1.1  pooka     struct _Tc *tc = sc->sc_dp;
    125  1.1  pooka     uint64_t free;
    126  1.1  pooka     int s;
    127  1.1  pooka 
    128  1.1  pooka     /* 32bit processor, guard against interrupts in the middle of reading this 64bit entity
    129  1.1  pooka      * BUGBUG Should read it "twice" to guard against rollover too.
    130  1.1  pooka      */
    131  1.1  pooka 	s = splhigh();
    132  1.1  pooka     free = tc->FreeRunning;
    133  1.1  pooka 	splx(s);
    134  1.1  pooka 
    135  1.1  pooka     /* Big fight with the compiler here, it gets very confused by 64bits.
    136  1.1  pooka      */
    137  1.1  pooka #if 0
    138  1.1  pooka     /* This is in C:
    139  1.1  pooka      */
    140  1.1  pooka     {
    141  1.1  pooka         uint64_t freeS, freeU;
    142  1.1  pooka         freeS = free / (10*1000*1000);
    143  1.1  pooka         freeU = free % (10*1000*1000);
    144  1.1  pooka         tv->tv_sec  = freeS;
    145  1.1  pooka         tv->tv_usec = freeU / 10;
    146  1.1  pooka         //printf("egt: s x%lx u x%lx (fs %lld fu %lld f %lld)\n",tv->tv_sec,tv->tv_usec,freeS,freeU,free);
    147  1.1  pooka     }
    148  1.1  pooka #else
    149  1.1  pooka     /* And this is in assembly :-)
    150  1.1  pooka      */
    151  1.1  pooka     {
    152  1.1  pooka         u_quad_t r;
    153  1.1  pooka         u_quad_t d = __qdivrem(free,(u_quad_t)10000000,&r);
    154  1.1  pooka         uint32_t su, uu;
    155  1.1  pooka         su = (uint32_t) d;
    156  1.1  pooka         uu = (uint32_t) r;
    157  1.1  pooka         uu = uu / 10; /* in usecs */
    158  1.1  pooka         tv->tv_sec  = su;
    159  1.1  pooka         tv->tv_usec = uu;
    160  1.1  pooka         //printf("egt: s x%lx u x%lx  (d %lld r %lld  f %lld)\n",tv->tv_sec,tv->tv_usec,d,r,free);
    161  1.1  pooka     }
    162  1.1  pooka #endif
    163  1.1  pooka 
    164  1.1  pooka     return 0;
    165  1.1  pooka }
    166  1.1  pooka 
    167  1.1  pooka /*
    168  1.1  pooka  * Reset the TODR based on the time value.
    169  1.1  pooka  */
    170  1.1  pooka static int
    171  1.1  pooka eclock_settime(struct todr_chip_handle *todr, struct timeval *tv)
    172  1.1  pooka {
    173  1.1  pooka 	struct eclock_softc *sc = (struct eclock_softc *) todr->cookie;
    174  1.1  pooka     struct _Tc *tc = sc->sc_dp;
    175  1.1  pooka     uint64_t free;
    176  1.1  pooka     uint32_t su, uu;
    177  1.1  pooka     int s;
    178  1.1  pooka 
    179  1.1  pooka     /* Careful with what we do here, else the compilerbugs hit hard */
    180  1.1  pooka 	s = splhigh();
    181  1.1  pooka 
    182  1.1  pooka     su = (uint32_t) tv->tv_sec;  //0(tv)
    183  1.1  pooka     uu = (uint32_t) tv->tv_usec; //4(tv)
    184  1.1  pooka 
    185  1.1  pooka 
    186  1.1  pooka     free  = 10*1000*1000 * (uint64_t)su;
    187  1.1  pooka     free += uu * 10;
    188  1.1  pooka 
    189  1.1  pooka     tc->FreeRunning = free;
    190  1.1  pooka 	splx(s);
    191  1.1  pooka 
    192  1.1  pooka #if 0
    193  1.1  pooka Should compile to something like this:
    194  1.1  pooka 80260c84 <eclock_settime>:
    195  1.1  pooka 80260c84:	27bdffc0 	addiu	sp,sp,-64
    196  1.1  pooka 80260c88:	afbf0038 	sw	ra,56(sp)
    197  1.1  pooka 80260c8c:	afb40030 	sw	s4,48(sp)
    198  1.1  pooka 80260c90:	afb3002c 	sw	s3,44(sp)
    199  1.1  pooka 80260c94:	afb20028 	sw	s2,40(sp)
    200  1.1  pooka 80260c98:	afb10024 	sw	s1,36(sp)
    201  1.1  pooka 80260c9c:	afb00020 	sw	s0,32(sp)
    202  1.1  pooka 80260ca0:	afb50034 	sw	s5,52(sp)
    203  1.1  pooka 80260ca4:	8c820000 	lw	v0,0(a0)
    204  1.1  pooka 80260ca8:	00a09021 	move	s2,a1
    205  1.1  pooka 80260cac:	8c55003c 	lw	s5,60(v0)        //s5=tc
    206  1.1  pooka 80260cb0:	0c004122 	jal	80010488 <_splraise>
    207  1.1  pooka 80260cb4:	3404ff00 	li	a0,0xff00
    208  1.1  pooka 80260cb8:	8e540000 	lw	s4,0(s2)         //s4=tv->tv_sec=us
    209  1.1  pooka 80260cbc:	3c060098 	lui	a2,0x98
    210  1.1  pooka 80260cc0:	34c69680 	ori	a2,a2,0x9680     //a2=10000000
    211  1.1  pooka 80260cc4:	02860019 	multu	s4,a2        //free=us*10000000
    212  1.1  pooka 80260cc8:	8e530004 	lw	s3,4(s2)         //s3=uu
    213  1.1  pooka 80260ccc:	00402021 	move	a0,v0        //s=splhigh()
    214  1.1  pooka 80260cd0:	001328c0 	sll	a1,s3,0x3
    215  1.1  pooka 80260cd4:	00131040 	sll	v0,s3,0x1
    216  1.1  pooka 80260cd8:	00451021 	addu	v0,v0,a1
    217  1.1  pooka 80260cdc:	00401821 	move	v1,v0        //v1 = uu*10
    218  1.1  pooka 80260ce0:	00001021 	move	v0,zero
    219  1.1  pooka 80260ce4:	00003812 	mflo	a3           //a3=low(free)
    220  1.1  pooka 80260ce8:	00e38821 	addu	s1,a3,v1     //s1=low(free)+(uu*10)
    221  1.1  pooka 80260cec:	0227282b 	sltu	a1,s1,a3     //a1=overflow bit
    222  1.1  pooka 80260cf0:	00003010 	mfhi	a2           //a2=high(free)
    223  1.1  pooka 80260cf4:	00c28021 	addu	s0,a2,v0     //s0=a2=high(free) [useless, v0=0]
    224  1.1  pooka 80260cf8:	00b08021 	addu	s0,a1,s0     //s0+=overflow bit
    225  1.1  pooka 80260cfc:	aeb1000c 	sw	s1,12(s5)
    226  1.1  pooka 80260d00:	aeb00008 	sw	s0,8(s5)
    227  1.1  pooka 80260d04:	0c00413f 	jal	800104fc <_splset>
    228  1.1  pooka 80260d08:	00000000 	nop
    229  1.1  pooka 
    230  1.1  pooka #endif
    231  1.1  pooka 
    232  1.1  pooka     //printf("est: s x%lx u x%lx (%d %d), free %lld\n",tv->tv_sec,tv->tv_usec,su,uu,free);
    233  1.1  pooka 
    234  1.1  pooka     return 0;
    235  1.1  pooka }
    236  1.1  pooka 
    237  1.1  pooka static int
    238  1.1  pooka eclock_ebus_intr(void *cookie, void *f)
    239  1.1  pooka {
    240  1.1  pooka 	struct eclock_softc *sc = cookie;
    241  1.1  pooka     struct _Tc *tc = sc->sc_dp;
    242  1.1  pooka     struct clockframe *cf = f;
    243  1.1  pooka     volatile uint32_t x;
    244  1.1  pooka 
    245  1.1  pooka     x = tc->Control;
    246  1.1  pooka     tc->DownCounterHigh = 0;
    247  1.1  pooka     tc->DownCounter = sc->reload;
    248  1.1  pooka 
    249  1.1  pooka     hardclock(cf);
    250  1.1  pooka     emips_clock_evcnt.ev_count++;
    251  1.1  pooka 
    252  1.1  pooka 	return (0);
    253  1.1  pooka }
    254  1.1  pooka 
    255  1.1  pooka static u_int
    256  1.1  pooka eclock_counter(struct timecounter *tc)
    257  1.1  pooka {
    258  1.1  pooka 	struct eclock_softc *sc = tc->tc_priv;
    259  1.1  pooka     struct _Tc *Tc = sc->sc_dp;
    260  1.1  pooka     return (u_int)Tc->FreeRunning; /* NB: chops to 32bits */
    261  1.1  pooka }
    262  1.1  pooka 
    263  1.1  pooka 
    264  1.1  pooka static int
    265  1.1  pooka eclock_ebus_match(struct device *parent, struct cfdata *match, void *aux)
    266  1.1  pooka {
    267  1.1  pooka 	struct ebus_attach_args *ia = aux;
    268  1.1  pooka     struct _Tc *mc = (struct _Tc *)ia->ia_vaddr;
    269  1.1  pooka 
    270  1.1  pooka 	if (strcmp("eclock", ia->ia_name) != 0)
    271  1.1  pooka 		return (0);
    272  1.1  pooka     if ((mc == NULL) ||
    273  1.1  pooka         (mc->Tag != PMTTAG_TIMER))
    274  1.1  pooka 		return (0);
    275  1.1  pooka 
    276  1.1  pooka 	return (1);
    277  1.1  pooka }
    278  1.1  pooka 
    279  1.1  pooka static void
    280  1.1  pooka eclock_ebus_attach(struct device *parent, struct device *self, void *aux)
    281  1.1  pooka {
    282  1.1  pooka 	struct ebus_attach_args *ia =aux;
    283  1.1  pooka 	struct eclock_softc *sc = (struct eclock_softc *)self;
    284  1.1  pooka 
    285  1.1  pooka 	sc->sc_dp = (struct _Tc*)ia->ia_vaddr;
    286  1.1  pooka 
    287  1.1  pooka     /* NB: We are chopping our 64bit free-running  down to 32bits */
    288  1.1  pooka     sc->sc_tc.tc_get_timecount = eclock_counter;
    289  1.1  pooka     sc->sc_tc.tc_poll_pps = 0;
    290  1.1  pooka     sc->sc_tc.tc_counter_mask = 0xffffffff;
    291  1.1  pooka     sc->sc_tc.tc_frequency = 10*1000*1000; /* 10 MHz */
    292  1.1  pooka     sc->sc_tc.tc_name = "eclock"; /* BUGBUG is it unique per instance?? */
    293  1.1  pooka     sc->sc_tc.tc_quality = 2000; /* uhu? */
    294  1.1  pooka     sc->sc_tc.tc_priv = sc;
    295  1.1  pooka     sc->sc_tc.tc_next = NULL;
    296  1.1  pooka 
    297  1.1  pooka #if DEBUG
    298  1.1  pooka     printf(" virt=%p ", (void*)sc->sc_dp);
    299  1.1  pooka #endif
    300  1.1  pooka 	printf(": eMIPS clock\n");
    301  1.1  pooka 
    302  1.1  pooka 	/* Turn interrupts off, just in case. */
    303  1.1  pooka     sc->sc_dp->Control &= ~(TCCT_INT_ENABLE|TCCT_INTERRUPT);
    304  1.1  pooka 
    305  1.1  pooka 	ebus_intr_establish(parent, (void *)ia->ia_cookie, IPL_CLOCK,
    306  1.1  pooka 	    eclock_ebus_intr, sc);
    307  1.1  pooka 
    308  1.1  pooka #ifdef EVCNT_COUNTERS
    309  1.1  pooka 	evcnt_attach_dynamic(&clock_intr_evcnt, EVCNT_TYPE_INTR, NULL,
    310  1.1  pooka 	    sc->sc_dev->dv_xname, "intr");
    311  1.1  pooka #endif
    312  1.1  pooka 
    313  1.1  pooka #ifdef __HAVE_GENERIC_TODR
    314  1.1  pooka     clockdev = self;
    315  1.1  pooka     memset(&sc->sc_todr,0,sizeof sc->sc_todr);
    316  1.1  pooka     sc->sc_todr.cookie = sc;
    317  1.1  pooka     sc->sc_todr.todr_gettime = eclock_gettime;
    318  1.1  pooka     sc->sc_todr.todr_settime = eclock_settime;
    319  1.1  pooka     todr_attach(&sc->sc_todr);
    320  1.1  pooka #endif
    321  1.1  pooka 
    322  1.1  pooka     tc_init(&sc->sc_tc);
    323  1.1  pooka }
    324