Home | History | Annotate | Line # | Download | only in sa11x0
sa11x0_ost.c revision 1.10.2.2
      1  1.10.2.1    skrll /*	$NetBSD: sa11x0_ost.c,v 1.10.2.2 2004/09/18 14:32:39 skrll Exp $	*/
      2       1.1      rjs 
      3       1.1      rjs /*
      4       1.1      rjs  * Copyright (c) 1997 Mark Brinicombe.
      5       1.1      rjs  * Copyright (c) 1997 Causality Limited.
      6       1.1      rjs  * All rights reserved.
      7       1.1      rjs  *
      8       1.1      rjs  * This code is derived from software contributed to The NetBSD Foundation
      9       1.1      rjs  * by IWAMOTO Toshihiro and Ichiro FUKUHARA.
     10       1.1      rjs  *
     11       1.1      rjs  * Redistribution and use in source and binary forms, with or without
     12       1.1      rjs  * modification, are permitted provided that the following conditions
     13       1.1      rjs  * are met:
     14       1.1      rjs  * 1. Redistributions of source code must retain the above copyright
     15       1.1      rjs  *    notice, this list of conditions and the following disclaimer.
     16       1.1      rjs  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.1      rjs  *    notice, this list of conditions and the following disclaimer in the
     18       1.1      rjs  *    documentation and/or other materials provided with the distribution.
     19       1.1      rjs  * 3. All advertising materials mentioning features or use of this software
     20       1.1      rjs  *    must display the following acknowledgement:
     21       1.1      rjs  *	This product includes software developed by the NetBSD
     22       1.1      rjs  *	Foundation, Inc. and its contributors.
     23       1.1      rjs  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24       1.1      rjs  *    contributors may be used to endorse or promote products derived
     25       1.1      rjs  *    from this software without specific prior written permission.
     26       1.1      rjs  *
     27       1.1      rjs  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28       1.1      rjs  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29       1.1      rjs  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30       1.1      rjs  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31       1.1      rjs  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32       1.1      rjs  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33       1.1      rjs  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34       1.1      rjs  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35       1.1      rjs  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36       1.1      rjs  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37       1.1      rjs  * POSSIBILITY OF SUCH DAMAGE.
     38       1.1      rjs  */
     39       1.1      rjs 
     40  1.10.2.1    skrll #include <sys/cdefs.h>
     41  1.10.2.1    skrll __KERNEL_RCSID(0, "$NetBSD: sa11x0_ost.c,v 1.10.2.2 2004/09/18 14:32:39 skrll Exp $");
     42  1.10.2.1    skrll 
     43       1.1      rjs #include <sys/types.h>
     44       1.1      rjs #include <sys/param.h>
     45       1.1      rjs #include <sys/systm.h>
     46       1.1      rjs #include <sys/kernel.h>
     47       1.1      rjs #include <sys/time.h>
     48       1.1      rjs #include <sys/device.h>
     49       1.1      rjs 
     50       1.1      rjs #include <machine/bus.h>
     51       1.2     matt #include <machine/intr.h>
     52       1.4  thorpej 
     53       1.4  thorpej #include <arm/cpufunc.h>
     54       1.4  thorpej 
     55       1.3  thorpej #include <arm/arm32/katelib.h>
     56       1.4  thorpej 
     57       1.1      rjs #include <arm/sa11x0/sa11x0_reg.h>
     58       1.1      rjs #include <arm/sa11x0/sa11x0_var.h>
     59       1.1      rjs #include <arm/sa11x0/sa11x0_ostreg.h>
     60       1.1      rjs 
     61       1.1      rjs static int	saost_match(struct device *, struct cfdata *, void *);
     62       1.1      rjs static void	saost_attach(struct device *, struct device *, void *);
     63       1.1      rjs 
     64       1.1      rjs int		gettick(void);
     65       1.1      rjs static int	clockintr(void *);
     66       1.1      rjs static int	statintr(void *);
     67       1.1      rjs void		rtcinit(void);
     68       1.1      rjs 
     69       1.1      rjs struct saost_softc {
     70       1.1      rjs 	struct device		sc_dev;
     71       1.1      rjs 	bus_addr_t		sc_baseaddr;
     72       1.1      rjs 	bus_space_tag_t		sc_iot;
     73       1.1      rjs 	bus_space_handle_t	sc_ioh;
     74       1.1      rjs 
     75       1.1      rjs 	u_int32_t	sc_clock_count;
     76       1.1      rjs 	u_int32_t	sc_statclock_count;
     77       1.1      rjs 	u_int32_t	sc_statclock_step;
     78       1.1      rjs };
     79       1.1      rjs 
     80       1.1      rjs static struct saost_softc *saost_sc = NULL;
     81       1.1      rjs 
     82       1.1      rjs #define TIMER_FREQUENCY         3686400         /* 3.6864MHz */
     83       1.1      rjs #define TICKS_PER_MICROSECOND   (TIMER_FREQUENCY/1000000)
     84       1.1      rjs 
     85       1.1      rjs #ifndef STATHZ
     86       1.1      rjs #define STATHZ	64
     87       1.1      rjs #endif
     88       1.1      rjs 
     89       1.9  thorpej CFATTACH_DECL(saost, sizeof(struct saost_softc),
     90       1.9  thorpej     saost_match, saost_attach, NULL, NULL);
     91       1.1      rjs 
     92       1.1      rjs static int
     93       1.1      rjs saost_match(parent, match, aux)
     94       1.1      rjs 	struct device *parent;
     95       1.1      rjs 	struct cfdata *match;
     96       1.1      rjs 	void *aux;
     97       1.1      rjs {
     98       1.1      rjs 	return (1);
     99       1.1      rjs }
    100       1.1      rjs 
    101       1.1      rjs void
    102       1.1      rjs saost_attach(parent, self, aux)
    103       1.1      rjs 	struct device *parent;
    104       1.1      rjs 	struct device *self;
    105       1.1      rjs 	void *aux;
    106       1.1      rjs {
    107       1.1      rjs 	struct saost_softc *sc = (struct saost_softc*)self;
    108       1.1      rjs 	struct sa11x0_attach_args *sa = aux;
    109       1.1      rjs 
    110       1.1      rjs 	printf("\n");
    111       1.1      rjs 
    112       1.1      rjs 	sc->sc_iot = sa->sa_iot;
    113       1.1      rjs 	sc->sc_baseaddr = sa->sa_addr;
    114       1.1      rjs 
    115       1.1      rjs 	saost_sc = sc;
    116       1.1      rjs 
    117       1.1      rjs 	if(bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0,
    118       1.1      rjs 			&sc->sc_ioh))
    119       1.7   provos 		panic("%s: Cannot map registers", self->dv_xname);
    120       1.1      rjs 
    121       1.1      rjs 	/* disable all channel and clear interrupt status */
    122       1.1      rjs 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_IR, 0);
    123       1.1      rjs 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_SR, 0xf);
    124       1.1      rjs 
    125       1.1      rjs 	printf("%s: SA-11x0 OS Timer\n",  sc->sc_dev.dv_xname);
    126       1.1      rjs }
    127       1.1      rjs 
    128       1.1      rjs static int
    129       1.1      rjs clockintr(arg)
    130       1.1      rjs 	void *arg;
    131       1.1      rjs {
    132       1.1      rjs 	struct clockframe *frame = arg;
    133       1.1      rjs 	u_int32_t oscr, nextmatch, oldmatch;
    134       1.1      rjs 	int s;
    135       1.1      rjs 
    136       1.1      rjs 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    137       1.1      rjs 			SAOST_SR, 1);
    138       1.1      rjs 
    139       1.1      rjs 	/* schedule next clock intr */
    140       1.1      rjs 	oldmatch = saost_sc->sc_clock_count;
    141       1.1      rjs 	nextmatch = oldmatch + TIMER_FREQUENCY / hz;
    142       1.1      rjs 
    143       1.1      rjs 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR0,
    144       1.1      rjs 			  nextmatch);
    145       1.1      rjs 	oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    146       1.1      rjs 				SAOST_CR);
    147       1.1      rjs 
    148       1.1      rjs 	if ((nextmatch > oldmatch &&
    149       1.1      rjs 	     (oscr > nextmatch || oscr < oldmatch)) ||
    150       1.1      rjs 	    (nextmatch < oldmatch && oscr > nextmatch && oscr < oldmatch)) {
    151       1.1      rjs 		/*
    152       1.1      rjs 		 * we couldn't set the matching register in time.
    153       1.1      rjs 		 * just set it to some value so that next interrupt happens.
    154       1.1      rjs 		 * XXX is it possible to compansate lost interrupts?
    155       1.1      rjs 		 */
    156       1.1      rjs 
    157       1.1      rjs 		s = splhigh();
    158       1.1      rjs 		oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    159       1.1      rjs 					SAOST_CR);
    160       1.1      rjs 		nextmatch = oscr + 10;
    161       1.1      rjs 		bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    162       1.1      rjs 				  SAOST_MR0, nextmatch);
    163       1.1      rjs 		splx(s);
    164       1.1      rjs 	}
    165       1.1      rjs 
    166       1.1      rjs 	saost_sc->sc_clock_count = nextmatch;
    167       1.1      rjs 	hardclock(frame);
    168       1.1      rjs 
    169       1.1      rjs 	return(1);
    170       1.1      rjs }
    171       1.1      rjs 
    172       1.1      rjs static int
    173       1.1      rjs statintr(arg)
    174       1.1      rjs 	void *arg;
    175       1.1      rjs {
    176       1.1      rjs 	struct clockframe *frame = arg;
    177       1.1      rjs 	u_int32_t oscr, nextmatch, oldmatch;
    178       1.1      rjs 	int s;
    179       1.1      rjs 
    180       1.1      rjs 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    181       1.1      rjs 			SAOST_SR, 2);
    182       1.1      rjs 
    183       1.1      rjs 	/* schedule next clock intr */
    184       1.1      rjs 	oldmatch = saost_sc->sc_statclock_count;
    185       1.1      rjs 	nextmatch = oldmatch + saost_sc->sc_statclock_step;
    186       1.1      rjs 
    187       1.1      rjs 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR1,
    188       1.1      rjs 			  nextmatch);
    189       1.1      rjs 	oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    190       1.1      rjs 				SAOST_CR);
    191       1.1      rjs 
    192       1.1      rjs 	if ((nextmatch > oldmatch &&
    193       1.1      rjs 	     (oscr > nextmatch || oscr < oldmatch)) ||
    194       1.1      rjs 	    (nextmatch < oldmatch && oscr > nextmatch && oscr < oldmatch)) {
    195       1.1      rjs 		/*
    196       1.1      rjs 		 * we couldn't set the matching register in time.
    197       1.1      rjs 		 * just set it to some value so that next interrupt happens.
    198       1.1      rjs 		 * XXX is it possible to compansate lost interrupts?
    199       1.1      rjs 		 */
    200       1.1      rjs 
    201       1.1      rjs 		s = splhigh();
    202       1.1      rjs 		oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    203       1.1      rjs 					SAOST_CR);
    204       1.1      rjs 		nextmatch = oscr + 10;
    205       1.1      rjs 		bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    206       1.1      rjs 				  SAOST_MR1, nextmatch);
    207       1.1      rjs 		splx(s);
    208       1.1      rjs 	}
    209       1.1      rjs 
    210       1.1      rjs 	saost_sc->sc_statclock_count = nextmatch;
    211       1.1      rjs 	statclock(frame);
    212       1.1      rjs 
    213       1.1      rjs 	return(1);
    214       1.1      rjs }
    215       1.1      rjs 
    216       1.1      rjs 
    217       1.1      rjs void
    218       1.1      rjs setstatclockrate(hz)
    219       1.1      rjs 	int hz;
    220       1.1      rjs {
    221       1.1      rjs 	u_int32_t count;
    222       1.1      rjs 
    223       1.1      rjs 	saost_sc->sc_statclock_step = TIMER_FREQUENCY / hz;
    224       1.1      rjs 	count = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_CR);
    225       1.1      rjs 	count += saost_sc->sc_statclock_step;
    226       1.1      rjs 	saost_sc->sc_statclock_count = count;
    227       1.1      rjs 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    228       1.1      rjs 			SAOST_MR1, count);
    229       1.1      rjs }
    230       1.1      rjs 
    231       1.1      rjs void
    232       1.1      rjs cpu_initclocks()
    233       1.1      rjs {
    234       1.1      rjs 	stathz = STATHZ;
    235       1.1      rjs 	profhz = stathz;
    236       1.1      rjs 	saost_sc->sc_statclock_step = TIMER_FREQUENCY / stathz;
    237       1.1      rjs 
    238       1.1      rjs 	printf("clock: hz=%d stathz = %d\n", hz, stathz);
    239       1.1      rjs 
    240       1.1      rjs 	/* Use the channels 0 and 1 for hardclock and statclock, respectively */
    241       1.1      rjs 	saost_sc->sc_clock_count = TIMER_FREQUENCY / hz;
    242       1.1      rjs 	saost_sc->sc_statclock_count = TIMER_FREQUENCY / stathz;
    243       1.1      rjs 
    244       1.6      rjs 	sa11x0_intr_establish(0, 26, 1, IPL_CLOCK, clockintr, 0);
    245       1.6      rjs 	sa11x0_intr_establish(0, 27, 1, IPL_CLOCK, statintr, 0);
    246       1.6      rjs 
    247       1.6      rjs 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_SR, 0xf);
    248       1.1      rjs 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_IR, 3);
    249       1.1      rjs 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR0,
    250       1.1      rjs 			  saost_sc->sc_clock_count);
    251       1.1      rjs 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR1,
    252       1.1      rjs 			  saost_sc->sc_statclock_count);
    253       1.1      rjs 
    254       1.6      rjs 	/* Zero the counter value */
    255       1.6      rjs 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_CR, 0);
    256       1.1      rjs }
    257       1.1      rjs 
    258       1.1      rjs int
    259       1.1      rjs gettick()
    260       1.1      rjs {
    261       1.1      rjs 	int counter;
    262       1.1      rjs 	u_int savedints;
    263       1.1      rjs 	savedints = disable_interrupts(I32_bit);
    264       1.1      rjs 
    265       1.1      rjs 	counter = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    266       1.1      rjs 			SAOST_CR);
    267       1.1      rjs 
    268       1.1      rjs 	restore_interrupts(savedints);
    269       1.1      rjs 	return counter;
    270       1.1      rjs }
    271       1.1      rjs 
    272       1.1      rjs void
    273       1.1      rjs microtime(tvp)
    274       1.1      rjs 	register struct timeval *tvp;
    275       1.1      rjs {
    276       1.5      rjs 	int s, tm, deltatm;
    277       1.1      rjs 	static struct timeval lasttime;
    278       1.1      rjs 
    279       1.5      rjs 	if(saost_sc == NULL) {
    280       1.5      rjs 		tvp->tv_sec = 0;
    281       1.5      rjs 		tvp->tv_usec = 0;
    282       1.5      rjs 		return;
    283       1.5      rjs 	}
    284       1.5      rjs 
    285       1.5      rjs 	s = splhigh();
    286       1.1      rjs 	tm = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    287       1.1      rjs 			SAOST_CR);
    288       1.1      rjs 
    289       1.1      rjs 	deltatm = saost_sc->sc_clock_count - tm;
    290       1.1      rjs 
    291      1.10      bsh #ifdef OST_DEBUG
    292       1.1      rjs 	printf("deltatm = %d\n",deltatm);
    293       1.1      rjs #endif
    294       1.1      rjs 
    295       1.1      rjs 	*tvp = time;
    296       1.1      rjs 	tvp->tv_usec++;		/* XXX */
    297       1.1      rjs 	while (tvp->tv_usec >= 1000000) {
    298       1.1      rjs 		tvp->tv_sec++;
    299       1.1      rjs 		tvp->tv_usec -= 1000000;
    300       1.1      rjs 	}
    301       1.1      rjs 
    302       1.1      rjs 	if (tvp->tv_sec == lasttime.tv_sec &&
    303       1.1      rjs 		tvp->tv_usec <= lasttime.tv_usec &&
    304       1.1      rjs 		(tvp->tv_usec = lasttime.tv_usec + 1) >= 1000000)
    305       1.1      rjs 	{
    306       1.1      rjs 		tvp->tv_sec++;
    307       1.1      rjs 		tvp->tv_usec -= 1000000;
    308       1.1      rjs 	}
    309       1.1      rjs 	lasttime = *tvp;
    310       1.1      rjs 	splx(s);
    311       1.1      rjs }
    312       1.1      rjs 
    313       1.1      rjs void
    314       1.1      rjs delay(usecs)
    315       1.1      rjs 	u_int usecs;
    316       1.1      rjs {
    317       1.1      rjs 	u_int32_t tick, otick, delta;
    318       1.1      rjs 	int j, csec, usec;
    319       1.1      rjs 
    320       1.1      rjs 	csec = usecs / 10000;
    321       1.1      rjs 	usec = usecs % 10000;
    322       1.1      rjs 
    323       1.1      rjs 	usecs = (TIMER_FREQUENCY / 100) * csec
    324       1.1      rjs 	    + (TIMER_FREQUENCY / 100) * usec / 10000;
    325       1.1      rjs 
    326       1.1      rjs 	if (! saost_sc) {
    327       1.1      rjs 		/* clock isn't initialized yet */
    328       1.1      rjs 		for(; usecs > 0; usecs--)
    329       1.1      rjs 			for(j = 100; j > 0; j--)
    330       1.1      rjs 				;
    331       1.1      rjs 		return;
    332       1.1      rjs 	}
    333       1.1      rjs 
    334       1.1      rjs 	otick = gettick();
    335       1.1      rjs 
    336       1.1      rjs 	while (1) {
    337       1.1      rjs 		for(j = 100; j > 0; j--)
    338       1.1      rjs 			;
    339       1.1      rjs 		tick = gettick();
    340       1.1      rjs 		delta = tick - otick;
    341       1.1      rjs 		if (delta > usecs)
    342       1.1      rjs 			break;
    343       1.1      rjs 		usecs -= delta;
    344       1.1      rjs 		otick = tick;
    345       1.1      rjs 	}
    346       1.1      rjs }
    347       1.1      rjs 
    348       1.1      rjs void
    349       1.1      rjs resettodr()
    350       1.1      rjs {
    351       1.1      rjs }
    352       1.1      rjs 
    353       1.1      rjs void
    354       1.1      rjs inittodr(base)
    355       1.1      rjs 	time_t base;
    356       1.1      rjs {
    357       1.1      rjs 	time.tv_sec = base;
    358       1.1      rjs 	time.tv_usec = 0;
    359       1.1      rjs }
    360