Home | History | Annotate | Line # | Download | only in sa11x0
sa11x0_ost.c revision 1.20
      1  1.20    peter /*	$NetBSD: sa11x0_ost.c,v 1.20 2006/09/24 15:40:14 peter 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.11    lukem 
     40  1.11    lukem #include <sys/cdefs.h>
     41  1.20    peter __KERNEL_RCSID(0, "$NetBSD: sa11x0_ost.c,v 1.20 2006/09/24 15:40:14 peter Exp $");
     42   1.1      rjs 
     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.19    peter #include <sys/timetc.h>
     49   1.1      rjs #include <sys/device.h>
     50   1.1      rjs 
     51   1.1      rjs #include <machine/bus.h>
     52   1.2     matt #include <machine/intr.h>
     53   1.4  thorpej 
     54   1.4  thorpej #include <arm/cpufunc.h>
     55   1.4  thorpej 
     56   1.1      rjs #include <arm/sa11x0/sa11x0_reg.h>
     57   1.1      rjs #include <arm/sa11x0/sa11x0_var.h>
     58   1.1      rjs #include <arm/sa11x0/sa11x0_ostreg.h>
     59   1.1      rjs 
     60   1.1      rjs static int	saost_match(struct device *, struct cfdata *, void *);
     61   1.1      rjs static void	saost_attach(struct device *, struct device *, void *);
     62   1.1      rjs 
     63  1.19    peter #ifdef __HAVE_TIMECOUNTER
     64  1.19    peter static void	saost_tc_init(void);
     65  1.19    peter #endif /* __HAVE_TIMECOUNTER */
     66  1.19    peter 
     67  1.19    peter static uint32_t	gettick(void);
     68   1.1      rjs static int	clockintr(void *);
     69   1.1      rjs static int	statintr(void *);
     70   1.1      rjs 
     71   1.1      rjs struct saost_softc {
     72   1.1      rjs 	struct device		sc_dev;
     73  1.20    peter 
     74   1.1      rjs 	bus_space_tag_t		sc_iot;
     75   1.1      rjs 	bus_space_handle_t	sc_ioh;
     76   1.1      rjs 
     77  1.20    peter 	uint32_t		sc_clock_count;
     78  1.20    peter 	uint32_t		sc_statclock_count;
     79  1.20    peter 	uint32_t		sc_statclock_step;
     80   1.1      rjs };
     81   1.1      rjs 
     82   1.1      rjs static struct saost_softc *saost_sc = NULL;
     83   1.1      rjs 
     84   1.1      rjs #define TIMER_FREQUENCY         3686400         /* 3.6864MHz */
     85   1.1      rjs 
     86   1.1      rjs #ifndef STATHZ
     87   1.1      rjs #define STATHZ	64
     88   1.1      rjs #endif
     89   1.1      rjs 
     90   1.9  thorpej CFATTACH_DECL(saost, sizeof(struct saost_softc),
     91   1.9  thorpej     saost_match, saost_attach, NULL, NULL);
     92   1.1      rjs 
     93   1.1      rjs static int
     94  1.15    peter saost_match(struct device *parent, struct cfdata *match, void *aux)
     95   1.1      rjs {
     96  1.18    peter 
     97  1.18    peter 	return 1;
     98   1.1      rjs }
     99   1.1      rjs 
    100  1.20    peter static void
    101  1.15    peter saost_attach(struct device *parent, struct device *self, void *aux)
    102   1.1      rjs {
    103  1.20    peter 	struct saost_softc *sc = (struct saost_softc *)self;
    104   1.1      rjs 	struct sa11x0_attach_args *sa = aux;
    105   1.1      rjs 
    106   1.1      rjs 	printf("\n");
    107   1.1      rjs 
    108   1.1      rjs 	sc->sc_iot = sa->sa_iot;
    109   1.1      rjs 
    110   1.1      rjs 	saost_sc = sc;
    111   1.1      rjs 
    112  1.18    peter 	if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0,
    113  1.18    peter 	    &sc->sc_ioh))
    114   1.7   provos 		panic("%s: Cannot map registers", self->dv_xname);
    115   1.1      rjs 
    116   1.1      rjs 	/* disable all channel and clear interrupt status */
    117  1.20    peter 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_IR, 0);
    118  1.20    peter 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_SR, 0xf);
    119   1.1      rjs 
    120  1.20    peter 	printf("%s: SA-11x0 OS Timer\n", sc->sc_dev.dv_xname);
    121   1.1      rjs }
    122   1.1      rjs 
    123   1.1      rjs static int
    124  1.15    peter clockintr(void *arg)
    125   1.1      rjs {
    126  1.20    peter 	struct saost_softc *sc = saost_sc;
    127   1.1      rjs 	struct clockframe *frame = arg;
    128  1.16    peter 	uint32_t oscr, nextmatch, oldmatch;
    129   1.1      rjs 	int s;
    130   1.1      rjs 
    131  1.20    peter 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_SR, 1);
    132   1.1      rjs 
    133   1.1      rjs 	/* schedule next clock intr */
    134  1.20    peter 	oldmatch = sc->sc_clock_count;
    135   1.1      rjs 	nextmatch = oldmatch + TIMER_FREQUENCY / hz;
    136   1.1      rjs 
    137  1.20    peter 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_MR0, nextmatch);
    138  1.20    peter 	oscr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SAOST_CR);
    139   1.1      rjs 
    140   1.1      rjs 	if ((nextmatch > oldmatch &&
    141   1.1      rjs 	     (oscr > nextmatch || oscr < oldmatch)) ||
    142   1.1      rjs 	    (nextmatch < oldmatch && oscr > nextmatch && oscr < oldmatch)) {
    143   1.1      rjs 		/*
    144   1.1      rjs 		 * we couldn't set the matching register in time.
    145   1.1      rjs 		 * just set it to some value so that next interrupt happens.
    146  1.18    peter 		 * XXX is it possible to compensate lost interrupts?
    147   1.1      rjs 		 */
    148   1.1      rjs 
    149   1.1      rjs 		s = splhigh();
    150  1.20    peter 		oscr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SAOST_CR);
    151   1.1      rjs 		nextmatch = oscr + 10;
    152  1.20    peter 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_MR0, nextmatch);
    153   1.1      rjs 		splx(s);
    154   1.1      rjs 	}
    155   1.1      rjs 
    156  1.20    peter 	sc->sc_clock_count = nextmatch;
    157   1.1      rjs 	hardclock(frame);
    158   1.1      rjs 
    159  1.18    peter 	return 1;
    160   1.1      rjs }
    161   1.1      rjs 
    162   1.1      rjs static int
    163  1.15    peter statintr(void *arg)
    164   1.1      rjs {
    165  1.20    peter 	struct saost_softc *sc = saost_sc;
    166   1.1      rjs 	struct clockframe *frame = arg;
    167  1.16    peter 	uint32_t oscr, nextmatch, oldmatch;
    168   1.1      rjs 	int s;
    169   1.1      rjs 
    170  1.20    peter 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_SR, 2);
    171   1.1      rjs 
    172   1.1      rjs 	/* schedule next clock intr */
    173  1.20    peter 	oldmatch = sc->sc_statclock_count;
    174  1.20    peter 	nextmatch = oldmatch + sc->sc_statclock_step;
    175   1.1      rjs 
    176  1.20    peter 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_MR1, nextmatch);
    177  1.20    peter 	oscr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SAOST_CR);
    178   1.1      rjs 
    179   1.1      rjs 	if ((nextmatch > oldmatch &&
    180   1.1      rjs 	     (oscr > nextmatch || oscr < oldmatch)) ||
    181   1.1      rjs 	    (nextmatch < oldmatch && oscr > nextmatch && oscr < oldmatch)) {
    182   1.1      rjs 		/*
    183   1.1      rjs 		 * we couldn't set the matching register in time.
    184   1.1      rjs 		 * just set it to some value so that next interrupt happens.
    185  1.18    peter 		 * XXX is it possible to compensate lost interrupts?
    186   1.1      rjs 		 */
    187   1.1      rjs 
    188   1.1      rjs 		s = splhigh();
    189  1.20    peter 		oscr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SAOST_CR);
    190   1.1      rjs 		nextmatch = oscr + 10;
    191  1.20    peter 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_MR1, nextmatch);
    192   1.1      rjs 		splx(s);
    193   1.1      rjs 	}
    194   1.1      rjs 
    195  1.20    peter 	sc->sc_statclock_count = nextmatch;
    196   1.1      rjs 	statclock(frame);
    197   1.1      rjs 
    198  1.18    peter 	return 1;
    199   1.1      rjs }
    200   1.1      rjs 
    201   1.1      rjs void
    202  1.15    peter setstatclockrate(int schz)
    203   1.1      rjs {
    204  1.20    peter 	struct saost_softc *sc = saost_sc;
    205  1.16    peter 	uint32_t count;
    206   1.1      rjs 
    207  1.20    peter 	sc->sc_statclock_step = TIMER_FREQUENCY / schz;
    208  1.20    peter 	count = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SAOST_CR);
    209  1.20    peter 	count += sc->sc_statclock_step;
    210  1.20    peter 	sc->sc_statclock_count = count;
    211  1.20    peter 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_MR1, count);
    212   1.1      rjs }
    213   1.1      rjs 
    214   1.1      rjs void
    215  1.15    peter cpu_initclocks(void)
    216   1.1      rjs {
    217  1.20    peter 	struct saost_softc *sc = saost_sc;
    218  1.20    peter 
    219   1.1      rjs 	stathz = STATHZ;
    220   1.1      rjs 	profhz = stathz;
    221  1.20    peter 	sc->sc_statclock_step = TIMER_FREQUENCY / stathz;
    222   1.1      rjs 
    223  1.17    peter 	printf("clock: hz=%d stathz=%d\n", hz, stathz);
    224   1.1      rjs 
    225   1.1      rjs 	/* Use the channels 0 and 1 for hardclock and statclock, respectively */
    226  1.20    peter 	sc->sc_clock_count = TIMER_FREQUENCY / hz;
    227  1.20    peter 	sc->sc_statclock_count = TIMER_FREQUENCY / stathz;
    228   1.1      rjs 
    229   1.6      rjs 	sa11x0_intr_establish(0, 26, 1, IPL_CLOCK, clockintr, 0);
    230   1.6      rjs 	sa11x0_intr_establish(0, 27, 1, IPL_CLOCK, statintr, 0);
    231   1.6      rjs 
    232  1.20    peter 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_SR, 0xf);
    233  1.20    peter 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_IR, 3);
    234  1.20    peter 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_MR0,
    235  1.20    peter 			  sc->sc_clock_count);
    236  1.20    peter 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_MR1,
    237  1.20    peter 			  sc->sc_statclock_count);
    238   1.1      rjs 
    239   1.6      rjs 	/* Zero the counter value */
    240  1.20    peter 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_CR, 0);
    241  1.19    peter 
    242  1.19    peter #ifdef __HAVE_TIMECOUNTER
    243  1.19    peter 	saost_tc_init();
    244  1.19    peter #endif /* __HAVE_TIMECOUNTER */
    245  1.19    peter }
    246  1.19    peter 
    247  1.19    peter #ifdef __HAVE_TIMECOUNTER
    248  1.19    peter static u_int
    249  1.19    peter saost_tc_get_timecount(struct timecounter *tc)
    250  1.19    peter {
    251  1.19    peter 	return (u_int)gettick();
    252  1.19    peter }
    253  1.19    peter 
    254  1.19    peter static void
    255  1.19    peter saost_tc_init(void)
    256  1.19    peter {
    257  1.19    peter 	static struct timecounter saost_tc = {
    258  1.19    peter 		.tc_get_timecount = saost_tc_get_timecount,
    259  1.19    peter 		.tc_frequency = TIMER_FREQUENCY,
    260  1.19    peter 		.tc_counter_mask = ~0,
    261  1.19    peter 		.tc_name = "saost_count",
    262  1.19    peter 		.tc_quality = 100,
    263  1.19    peter 	};
    264  1.19    peter 
    265  1.19    peter 	tc_init(&saost_tc);
    266   1.1      rjs }
    267  1.19    peter #endif /* __HAVE_TIMECOUNTER */
    268   1.1      rjs 
    269  1.19    peter static uint32_t
    270  1.15    peter gettick(void)
    271   1.1      rjs {
    272  1.20    peter 	struct saost_softc *sc = saost_sc;
    273  1.20    peter 	uint32_t counter;
    274  1.20    peter 	u_int saved_ints;
    275  1.20    peter 
    276  1.20    peter 	saved_ints = disable_interrupts(I32_bit);
    277  1.20    peter 	counter = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SAOST_CR);
    278  1.20    peter 	restore_interrupts(saved_ints);
    279   1.1      rjs 
    280   1.1      rjs 	return counter;
    281   1.1      rjs }
    282   1.1      rjs 
    283  1.19    peter #ifndef __HAVE_TIMECOUNTER
    284   1.1      rjs void
    285  1.15    peter microtime(struct timeval *tvp)
    286   1.1      rjs {
    287  1.20    peter 	struct saost_softc *sc = saost_sc;
    288   1.5      rjs 	int s, tm, deltatm;
    289   1.1      rjs 	static struct timeval lasttime;
    290   1.1      rjs 
    291  1.20    peter 	if (sc == NULL) {
    292   1.5      rjs 		tvp->tv_sec = 0;
    293   1.5      rjs 		tvp->tv_usec = 0;
    294   1.5      rjs 		return;
    295   1.5      rjs 	}
    296   1.5      rjs 
    297   1.5      rjs 	s = splhigh();
    298  1.20    peter 	tm = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SAOST_CR);
    299   1.1      rjs 
    300  1.20    peter 	deltatm = sc->sc_clock_count - tm;
    301   1.1      rjs 
    302   1.1      rjs 	*tvp = time;
    303   1.1      rjs 	tvp->tv_usec++;		/* XXX */
    304   1.1      rjs 	while (tvp->tv_usec >= 1000000) {
    305   1.1      rjs 		tvp->tv_sec++;
    306   1.1      rjs 		tvp->tv_usec -= 1000000;
    307   1.1      rjs 	}
    308   1.1      rjs 
    309   1.1      rjs 	if (tvp->tv_sec == lasttime.tv_sec &&
    310   1.1      rjs 		tvp->tv_usec <= lasttime.tv_usec &&
    311   1.1      rjs 		(tvp->tv_usec = lasttime.tv_usec + 1) >= 1000000)
    312   1.1      rjs 	{
    313   1.1      rjs 		tvp->tv_sec++;
    314   1.1      rjs 		tvp->tv_usec -= 1000000;
    315   1.1      rjs 	}
    316   1.1      rjs 	lasttime = *tvp;
    317   1.1      rjs 	splx(s);
    318   1.1      rjs }
    319  1.19    peter #endif /* !__HAVE_TIMECOUNTER */
    320   1.1      rjs 
    321   1.1      rjs void
    322  1.15    peter delay(u_int usecs)
    323   1.1      rjs {
    324  1.16    peter 	uint32_t xtick, otick, delta;
    325   1.1      rjs 	int j, csec, usec;
    326   1.1      rjs 
    327   1.1      rjs 	csec = usecs / 10000;
    328   1.1      rjs 	usec = usecs % 10000;
    329   1.1      rjs 
    330   1.1      rjs 	usecs = (TIMER_FREQUENCY / 100) * csec
    331   1.1      rjs 	    + (TIMER_FREQUENCY / 100) * usec / 10000;
    332   1.1      rjs 
    333  1.20    peter 	if (saost_sc == NULL) {
    334   1.1      rjs 		/* clock isn't initialized yet */
    335  1.18    peter 		for (; usecs > 0; usecs--)
    336  1.18    peter 			for (j = 100; j > 0; j--)
    337  1.18    peter 				continue;
    338   1.1      rjs 		return;
    339   1.1      rjs 	}
    340   1.1      rjs 
    341   1.1      rjs 	otick = gettick();
    342   1.1      rjs 
    343   1.1      rjs 	while (1) {
    344  1.18    peter 		for (j = 100; j > 0; j--)
    345  1.18    peter 			continue;
    346  1.12      uwe 		xtick = gettick();
    347  1.12      uwe 		delta = xtick - otick;
    348   1.1      rjs 		if (delta > usecs)
    349   1.1      rjs 			break;
    350   1.1      rjs 		usecs -= delta;
    351  1.12      uwe 		otick = xtick;
    352   1.1      rjs 	}
    353   1.1      rjs }
    354   1.1      rjs 
    355  1.19    peter #ifndef __HAVE_GENERIC_TODR
    356   1.1      rjs void
    357  1.15    peter resettodr(void)
    358   1.1      rjs {
    359   1.1      rjs }
    360   1.1      rjs 
    361   1.1      rjs void
    362  1.15    peter inittodr(time_t base)
    363   1.1      rjs {
    364   1.1      rjs 	time.tv_sec = base;
    365   1.1      rjs 	time.tv_usec = 0;
    366   1.1      rjs }
    367  1.19    peter #endif /* !__HAVE_GENERIC_TODR */
    368