Home | History | Annotate | Line # | Download | only in sa11x0
sa11x0_ost.c revision 1.24
      1  1.24   martin /*	$NetBSD: sa11x0_ost.c,v 1.24 2008/04/28 20:23:14 martin 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  *
     20   1.1      rjs  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.1      rjs  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.1      rjs  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.1      rjs  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.1      rjs  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.1      rjs  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.1      rjs  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.1      rjs  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.1      rjs  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.1      rjs  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.1      rjs  * POSSIBILITY OF SUCH DAMAGE.
     31   1.1      rjs  */
     32  1.11    lukem 
     33  1.11    lukem #include <sys/cdefs.h>
     34  1.24   martin __KERNEL_RCSID(0, "$NetBSD: sa11x0_ost.c,v 1.24 2008/04/28 20:23:14 martin Exp $");
     35   1.1      rjs 
     36   1.1      rjs #include <sys/types.h>
     37   1.1      rjs #include <sys/param.h>
     38   1.1      rjs #include <sys/systm.h>
     39   1.1      rjs #include <sys/kernel.h>
     40   1.1      rjs #include <sys/time.h>
     41  1.19    peter #include <sys/timetc.h>
     42   1.1      rjs #include <sys/device.h>
     43   1.1      rjs 
     44   1.1      rjs #include <machine/bus.h>
     45   1.2     matt #include <machine/intr.h>
     46   1.4  thorpej 
     47   1.4  thorpej #include <arm/cpufunc.h>
     48   1.4  thorpej 
     49   1.1      rjs #include <arm/sa11x0/sa11x0_reg.h>
     50   1.1      rjs #include <arm/sa11x0/sa11x0_var.h>
     51   1.1      rjs #include <arm/sa11x0/sa11x0_ostreg.h>
     52   1.1      rjs 
     53   1.1      rjs static int	saost_match(struct device *, struct cfdata *, void *);
     54   1.1      rjs static void	saost_attach(struct device *, struct device *, void *);
     55   1.1      rjs 
     56  1.19    peter static void	saost_tc_init(void);
     57  1.19    peter 
     58  1.19    peter static uint32_t	gettick(void);
     59   1.1      rjs static int	clockintr(void *);
     60   1.1      rjs static int	statintr(void *);
     61   1.1      rjs 
     62   1.1      rjs struct saost_softc {
     63   1.1      rjs 	struct device		sc_dev;
     64  1.20    peter 
     65   1.1      rjs 	bus_space_tag_t		sc_iot;
     66   1.1      rjs 	bus_space_handle_t	sc_ioh;
     67   1.1      rjs 
     68  1.20    peter 	uint32_t		sc_clock_count;
     69  1.20    peter 	uint32_t		sc_statclock_count;
     70  1.20    peter 	uint32_t		sc_statclock_step;
     71   1.1      rjs };
     72   1.1      rjs 
     73   1.1      rjs static struct saost_softc *saost_sc = NULL;
     74   1.1      rjs 
     75  1.23    chris #if defined(CPU_XSCALE_PXA270) && defined(CPU_XSCALE_PXA250)
     76  1.23    chris #error ost needs to dynamically configure the frequency
     77  1.23    chris #elif defined(CPU_XSCALE_PXA270)
     78  1.23    chris #define TIMER_FREQUENCY         3250000         /* PXA270 uses 3.25MHz */
     79  1.23    chris #else
     80   1.1      rjs #define TIMER_FREQUENCY         3686400         /* 3.6864MHz */
     81  1.23    chris #endif
     82   1.1      rjs 
     83   1.1      rjs #ifndef STATHZ
     84   1.1      rjs #define STATHZ	64
     85   1.1      rjs #endif
     86   1.1      rjs 
     87   1.9  thorpej CFATTACH_DECL(saost, sizeof(struct saost_softc),
     88   1.9  thorpej     saost_match, saost_attach, NULL, NULL);
     89   1.1      rjs 
     90   1.1      rjs static int
     91  1.15    peter saost_match(struct device *parent, struct cfdata *match, void *aux)
     92   1.1      rjs {
     93  1.18    peter 
     94  1.18    peter 	return 1;
     95   1.1      rjs }
     96   1.1      rjs 
     97  1.20    peter static void
     98  1.15    peter saost_attach(struct device *parent, struct device *self, void *aux)
     99   1.1      rjs {
    100  1.20    peter 	struct saost_softc *sc = (struct saost_softc *)self;
    101   1.1      rjs 	struct sa11x0_attach_args *sa = aux;
    102   1.1      rjs 
    103   1.1      rjs 	printf("\n");
    104   1.1      rjs 
    105   1.1      rjs 	sc->sc_iot = sa->sa_iot;
    106   1.1      rjs 
    107   1.1      rjs 	saost_sc = sc;
    108   1.1      rjs 
    109  1.18    peter 	if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0,
    110  1.18    peter 	    &sc->sc_ioh))
    111   1.7   provos 		panic("%s: Cannot map registers", self->dv_xname);
    112   1.1      rjs 
    113   1.1      rjs 	/* disable all channel and clear interrupt status */
    114  1.20    peter 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_IR, 0);
    115  1.20    peter 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_SR, 0xf);
    116   1.1      rjs 
    117  1.20    peter 	printf("%s: SA-11x0 OS Timer\n", sc->sc_dev.dv_xname);
    118   1.1      rjs }
    119   1.1      rjs 
    120   1.1      rjs static int
    121  1.15    peter clockintr(void *arg)
    122   1.1      rjs {
    123  1.20    peter 	struct saost_softc *sc = saost_sc;
    124   1.1      rjs 	struct clockframe *frame = arg;
    125  1.16    peter 	uint32_t oscr, nextmatch, oldmatch;
    126   1.1      rjs 	int s;
    127   1.1      rjs 
    128  1.20    peter 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_SR, 1);
    129   1.1      rjs 
    130   1.1      rjs 	/* schedule next clock intr */
    131  1.20    peter 	oldmatch = sc->sc_clock_count;
    132   1.1      rjs 	nextmatch = oldmatch + TIMER_FREQUENCY / hz;
    133   1.1      rjs 
    134  1.20    peter 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_MR0, nextmatch);
    135  1.20    peter 	oscr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SAOST_CR);
    136   1.1      rjs 
    137   1.1      rjs 	if ((nextmatch > oldmatch &&
    138   1.1      rjs 	     (oscr > nextmatch || oscr < oldmatch)) ||
    139   1.1      rjs 	    (nextmatch < oldmatch && oscr > nextmatch && oscr < oldmatch)) {
    140   1.1      rjs 		/*
    141   1.1      rjs 		 * we couldn't set the matching register in time.
    142   1.1      rjs 		 * just set it to some value so that next interrupt happens.
    143  1.18    peter 		 * XXX is it possible to compensate lost interrupts?
    144   1.1      rjs 		 */
    145   1.1      rjs 
    146   1.1      rjs 		s = splhigh();
    147  1.20    peter 		oscr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SAOST_CR);
    148   1.1      rjs 		nextmatch = oscr + 10;
    149  1.20    peter 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_MR0, nextmatch);
    150   1.1      rjs 		splx(s);
    151   1.1      rjs 	}
    152   1.1      rjs 
    153  1.20    peter 	sc->sc_clock_count = nextmatch;
    154   1.1      rjs 	hardclock(frame);
    155   1.1      rjs 
    156  1.18    peter 	return 1;
    157   1.1      rjs }
    158   1.1      rjs 
    159   1.1      rjs static int
    160  1.15    peter statintr(void *arg)
    161   1.1      rjs {
    162  1.20    peter 	struct saost_softc *sc = saost_sc;
    163   1.1      rjs 	struct clockframe *frame = arg;
    164  1.16    peter 	uint32_t oscr, nextmatch, oldmatch;
    165   1.1      rjs 	int s;
    166   1.1      rjs 
    167  1.20    peter 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_SR, 2);
    168   1.1      rjs 
    169   1.1      rjs 	/* schedule next clock intr */
    170  1.20    peter 	oldmatch = sc->sc_statclock_count;
    171  1.20    peter 	nextmatch = oldmatch + sc->sc_statclock_step;
    172   1.1      rjs 
    173  1.20    peter 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_MR1, nextmatch);
    174  1.20    peter 	oscr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SAOST_CR);
    175   1.1      rjs 
    176   1.1      rjs 	if ((nextmatch > oldmatch &&
    177   1.1      rjs 	     (oscr > nextmatch || oscr < oldmatch)) ||
    178   1.1      rjs 	    (nextmatch < oldmatch && oscr > nextmatch && oscr < oldmatch)) {
    179   1.1      rjs 		/*
    180   1.1      rjs 		 * we couldn't set the matching register in time.
    181   1.1      rjs 		 * just set it to some value so that next interrupt happens.
    182  1.18    peter 		 * XXX is it possible to compensate lost interrupts?
    183   1.1      rjs 		 */
    184   1.1      rjs 
    185   1.1      rjs 		s = splhigh();
    186  1.20    peter 		oscr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SAOST_CR);
    187   1.1      rjs 		nextmatch = oscr + 10;
    188  1.20    peter 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_MR1, nextmatch);
    189   1.1      rjs 		splx(s);
    190   1.1      rjs 	}
    191   1.1      rjs 
    192  1.20    peter 	sc->sc_statclock_count = nextmatch;
    193   1.1      rjs 	statclock(frame);
    194   1.1      rjs 
    195  1.18    peter 	return 1;
    196   1.1      rjs }
    197   1.1      rjs 
    198   1.1      rjs void
    199  1.15    peter setstatclockrate(int schz)
    200   1.1      rjs {
    201  1.20    peter 	struct saost_softc *sc = saost_sc;
    202  1.16    peter 	uint32_t count;
    203   1.1      rjs 
    204  1.20    peter 	sc->sc_statclock_step = TIMER_FREQUENCY / schz;
    205  1.20    peter 	count = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SAOST_CR);
    206  1.20    peter 	count += sc->sc_statclock_step;
    207  1.20    peter 	sc->sc_statclock_count = count;
    208  1.20    peter 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_MR1, count);
    209   1.1      rjs }
    210   1.1      rjs 
    211   1.1      rjs void
    212  1.15    peter cpu_initclocks(void)
    213   1.1      rjs {
    214  1.20    peter 	struct saost_softc *sc = saost_sc;
    215  1.20    peter 
    216   1.1      rjs 	stathz = STATHZ;
    217   1.1      rjs 	profhz = stathz;
    218  1.20    peter 	sc->sc_statclock_step = TIMER_FREQUENCY / stathz;
    219   1.1      rjs 
    220  1.17    peter 	printf("clock: hz=%d stathz=%d\n", hz, stathz);
    221   1.1      rjs 
    222   1.1      rjs 	/* Use the channels 0 and 1 for hardclock and statclock, respectively */
    223  1.20    peter 	sc->sc_clock_count = TIMER_FREQUENCY / hz;
    224  1.20    peter 	sc->sc_statclock_count = TIMER_FREQUENCY / stathz;
    225   1.1      rjs 
    226   1.6      rjs 	sa11x0_intr_establish(0, 26, 1, IPL_CLOCK, clockintr, 0);
    227   1.6      rjs 	sa11x0_intr_establish(0, 27, 1, IPL_CLOCK, statintr, 0);
    228   1.6      rjs 
    229  1.20    peter 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_SR, 0xf);
    230  1.20    peter 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_IR, 3);
    231  1.20    peter 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_MR0,
    232  1.20    peter 			  sc->sc_clock_count);
    233  1.20    peter 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_MR1,
    234  1.20    peter 			  sc->sc_statclock_count);
    235   1.1      rjs 
    236   1.6      rjs 	/* Zero the counter value */
    237  1.20    peter 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_CR, 0);
    238  1.19    peter 
    239  1.19    peter 	saost_tc_init();
    240  1.19    peter }
    241  1.19    peter 
    242  1.19    peter static u_int
    243  1.19    peter saost_tc_get_timecount(struct timecounter *tc)
    244  1.19    peter {
    245  1.19    peter 	return (u_int)gettick();
    246  1.19    peter }
    247  1.19    peter 
    248  1.19    peter static void
    249  1.19    peter saost_tc_init(void)
    250  1.19    peter {
    251  1.19    peter 	static struct timecounter saost_tc = {
    252  1.19    peter 		.tc_get_timecount = saost_tc_get_timecount,
    253  1.19    peter 		.tc_frequency = TIMER_FREQUENCY,
    254  1.19    peter 		.tc_counter_mask = ~0,
    255  1.19    peter 		.tc_name = "saost_count",
    256  1.19    peter 		.tc_quality = 100,
    257  1.19    peter 	};
    258  1.19    peter 
    259  1.19    peter 	tc_init(&saost_tc);
    260   1.1      rjs }
    261   1.1      rjs 
    262  1.19    peter static uint32_t
    263  1.15    peter gettick(void)
    264   1.1      rjs {
    265  1.20    peter 	struct saost_softc *sc = saost_sc;
    266  1.20    peter 	uint32_t counter;
    267  1.20    peter 	u_int saved_ints;
    268  1.20    peter 
    269  1.20    peter 	saved_ints = disable_interrupts(I32_bit);
    270  1.20    peter 	counter = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SAOST_CR);
    271  1.20    peter 	restore_interrupts(saved_ints);
    272   1.1      rjs 
    273   1.1      rjs 	return counter;
    274   1.1      rjs }
    275   1.1      rjs 
    276   1.1      rjs void
    277  1.15    peter delay(u_int usecs)
    278   1.1      rjs {
    279  1.16    peter 	uint32_t xtick, otick, delta;
    280  1.23    chris 	int csec, usec;
    281   1.1      rjs 
    282   1.1      rjs 	csec = usecs / 10000;
    283   1.1      rjs 	usec = usecs % 10000;
    284   1.1      rjs 
    285   1.1      rjs 	usecs = (TIMER_FREQUENCY / 100) * csec
    286   1.1      rjs 	    + (TIMER_FREQUENCY / 100) * usec / 10000;
    287   1.1      rjs 
    288  1.20    peter 	if (saost_sc == NULL) {
    289  1.23    chris 		volatile int k;
    290  1.23    chris 		int j;
    291   1.1      rjs 		/* clock isn't initialized yet */
    292  1.18    peter 		for (; usecs > 0; usecs--)
    293  1.23    chris 			for (j = 100; j > 0; j--, k--)
    294  1.18    peter 				continue;
    295   1.1      rjs 		return;
    296   1.1      rjs 	}
    297   1.1      rjs 
    298   1.1      rjs 	otick = gettick();
    299   1.1      rjs 
    300   1.1      rjs 	while (1) {
    301  1.12      uwe 		xtick = gettick();
    302  1.12      uwe 		delta = xtick - otick;
    303   1.1      rjs 		if (delta > usecs)
    304   1.1      rjs 			break;
    305   1.1      rjs 		usecs -= delta;
    306  1.12      uwe 		otick = xtick;
    307   1.1      rjs 	}
    308   1.1      rjs }
    309