Home | History | Annotate | Line # | Download | only in xscale
ixp425_timer.c revision 1.13
      1  1.13  christos /*	$NetBSD: ixp425_timer.c,v 1.13 2007/01/06 16:18:18 christos Exp $ */
      2   1.1    ichiro 
      3   1.1    ichiro /*
      4   1.1    ichiro  * Copyright (c) 2003
      5   1.1    ichiro  *	Ichiro FUKUHARA <ichiro (at) ichiro.org>.
      6   1.1    ichiro  * All rights reserved.
      7   1.1    ichiro  *
      8   1.1    ichiro  * Redistribution and use in source and binary forms, with or without
      9   1.1    ichiro  * modification, are permitted provided that the following conditions
     10   1.1    ichiro  * are met:
     11   1.1    ichiro  * 1. Redistributions of source code must retain the above copyright
     12   1.1    ichiro  *    notice, this list of conditions and the following disclaimer.
     13   1.1    ichiro  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1    ichiro  *    notice, this list of conditions and the following disclaimer in the
     15   1.1    ichiro  *    documentation and/or other materials provided with the distribution.
     16   1.1    ichiro  * 3. All advertising materials mentioning features or use of this software
     17   1.1    ichiro  *    must display the following acknowledgement:
     18   1.1    ichiro  *	This product includes software developed by Ichiro FUKUHARA.
     19   1.1    ichiro  * 4. The name of the company nor the name of the author may be used to
     20   1.1    ichiro  *    endorse or promote products derived from this software without specific
     21   1.1    ichiro  *    prior written permission.
     22   1.1    ichiro  *
     23   1.1    ichiro  * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
     24   1.1    ichiro  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25   1.1    ichiro  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26   1.1    ichiro  * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
     27   1.1    ichiro  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1    ichiro  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1    ichiro  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1    ichiro  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1    ichiro  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1    ichiro  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1    ichiro  * SUCH DAMAGE.
     34   1.1    ichiro  */
     35   1.1    ichiro 
     36   1.1    ichiro #include <sys/cdefs.h>
     37  1.13  christos __KERNEL_RCSID(0, "$NetBSD: ixp425_timer.c,v 1.13 2007/01/06 16:18:18 christos Exp $");
     38   1.1    ichiro 
     39  1.10       scw #include "opt_ixp425.h"
     40   1.1    ichiro #include "opt_perfctrs.h"
     41   1.1    ichiro 
     42   1.1    ichiro #include <sys/types.h>
     43   1.1    ichiro #include <sys/param.h>
     44   1.1    ichiro #include <sys/systm.h>
     45   1.1    ichiro #include <sys/kernel.h>
     46   1.1    ichiro #include <sys/time.h>
     47   1.1    ichiro #include <sys/device.h>
     48   1.1    ichiro 
     49   1.2   thorpej #include <dev/clock_subr.h>
     50   1.2   thorpej 
     51   1.1    ichiro #include <machine/bus.h>
     52   1.1    ichiro #include <machine/intr.h>
     53   1.1    ichiro 
     54   1.1    ichiro #include <arm/cpufunc.h>
     55   1.1    ichiro 
     56   1.1    ichiro #include <arm/xscale/ixp425reg.h>
     57   1.1    ichiro #include <arm/xscale/ixp425var.h>
     58   1.1    ichiro #include <arm/xscale/ixp425_sipvar.h>
     59   1.1    ichiro 
     60   1.1    ichiro static int	ixpclk_match(struct device *, struct cfdata *, void *);
     61   1.1    ichiro static void	ixpclk_attach(struct device *, struct device *, void *);
     62   1.1    ichiro 
     63   1.1    ichiro static uint32_t counts_per_hz;
     64   1.1    ichiro 
     65   1.1    ichiro static void *clock_ih;
     66   1.1    ichiro 
     67   1.1    ichiro /* callback functions for intr_functions */
     68   1.1    ichiro int	ixpclk_intr(void *);
     69   1.1    ichiro 
     70   1.1    ichiro struct ixpclk_softc {
     71  1.11    simonb 	struct device		sc_dev;
     72  1.11    simonb 	bus_addr_t		sc_baseaddr;
     73  1.11    simonb 	bus_space_tag_t		sc_iot;
     74  1.11    simonb 	bus_space_handle_t      sc_ioh;
     75   1.1    ichiro };
     76   1.1    ichiro 
     77  1.10       scw #ifndef IXP425_CLOCK_FREQ
     78   1.4       scw #define	COUNTS_PER_SEC		66666600	/* 66MHz */
     79  1.10       scw #else
     80  1.10       scw #define	COUNTS_PER_SEC		IXP425_CLOCK_FREQ
     81  1.10       scw #endif
     82   1.4       scw #define	COUNTS_PER_USEC		((COUNTS_PER_SEC / 1000000) + 1)
     83   1.1    ichiro 
     84   1.4       scw static struct ixpclk_softc *ixpclk_sc;
     85   1.1    ichiro 
     86   1.1    ichiro CFATTACH_DECL(ixpclk, sizeof(struct ixpclk_softc),
     87   1.1    ichiro 		ixpclk_match, ixpclk_attach, NULL, NULL);
     88   1.1    ichiro 
     89   1.1    ichiro #define GET_TIMER_VALUE(sc)	(bus_space_read_4((sc)->sc_iot,		\
     90   1.1    ichiro 						  (sc)->sc_ioh,		\
     91   1.1    ichiro 						  IXP425_OST_TIM0))
     92   1.1    ichiro 
     93   1.5       scw #define GET_TS_VALUE(sc)	(*(volatile u_int32_t *) \
     94   1.5       scw 				  (IXP425_TIMER_VBASE + IXP425_OST_TS))
     95   1.5       scw 
     96   1.1    ichiro static int
     97   1.1    ichiro ixpclk_match(struct device *parent, struct cfdata *match, void *aux)
     98   1.1    ichiro {
     99  1.11    simonb 	return 2;
    100   1.1    ichiro }
    101   1.1    ichiro 
    102   1.1    ichiro static void
    103   1.1    ichiro ixpclk_attach(struct device *parent, struct device *self, void *aux)
    104   1.1    ichiro {
    105   1.1    ichiro 	struct ixpclk_softc		*sc = (struct ixpclk_softc*) self;
    106   1.1    ichiro 	struct ixpsip_attach_args	*sa = aux;
    107   1.1    ichiro 
    108   1.1    ichiro 	printf("\n");
    109   1.1    ichiro 
    110   1.5       scw 	ixpclk_sc = sc;
    111   1.5       scw 
    112   1.1    ichiro 	sc->sc_iot = sa->sa_iot;
    113   1.1    ichiro 	sc->sc_baseaddr = sa->sa_addr;
    114   1.1    ichiro 
    115   1.1    ichiro 	if (bus_space_map(sc->sc_iot, sa->sa_addr, sa->sa_size, 0,
    116   1.1    ichiro 			  &sc->sc_ioh))
    117   1.1    ichiro 		panic("%s: Cannot map registers", self->dv_xname);
    118   1.1    ichiro 
    119   1.5       scw 	aprint_normal("%s: IXP425 Interval Timer\n", sc->sc_dev.dv_xname);
    120   1.1    ichiro }
    121   1.1    ichiro 
    122   1.1    ichiro /*
    123   1.1    ichiro  * cpu_initclocks:
    124   1.1    ichiro  *
    125   1.1    ichiro  *	Initialize the clock and get them going.
    126   1.1    ichiro  */
    127   1.1    ichiro void
    128   1.1    ichiro cpu_initclocks(void)
    129   1.1    ichiro {
    130   1.1    ichiro 	struct ixpclk_softc* sc = ixpclk_sc;
    131   1.1    ichiro 	u_int oldirqstate;
    132   1.1    ichiro #if defined(PERFCTRS)
    133   1.1    ichiro 	void *pmu_ih;
    134   1.1    ichiro #endif
    135   1.1    ichiro 
    136   1.1    ichiro 	if (hz < 50 || COUNTS_PER_SEC % hz) {
    137   1.1    ichiro 		aprint_error("Cannot get %d Hz clock; using 100 Hz\n", hz);
    138   1.1    ichiro 		hz = 100;
    139   1.1    ichiro 	}
    140   1.1    ichiro 	tick = 1000000 / hz;	/* number of microseconds between interrupts */
    141   1.1    ichiro 	tickfix = 1000000 - (hz * tick);
    142   1.1    ichiro 	if (tickfix) {
    143   1.1    ichiro 		int ftp;
    144   1.1    ichiro 
    145   1.1    ichiro 		ftp = min(ffs(tickfix), ffs(hz));
    146   1.1    ichiro 		tickfix >>= (ftp - 1);
    147   1.1    ichiro 		tickfixinterval = hz >> (ftp - 1);
    148   1.1    ichiro 	}
    149   1.1    ichiro 
    150   1.1    ichiro 	/*
    151   1.1    ichiro 	 * We only have one timer available; stathz and profhz are
    152   1.1    ichiro 	 * always left as 0 (the upper-layer clock code deals with
    153   1.1    ichiro 	 * this situation).
    154   1.1    ichiro 	 */
    155   1.1    ichiro 	if (stathz != 0)
    156   1.1    ichiro 		aprint_error("Cannot get %d Hz statclock\n", stathz);
    157   1.1    ichiro 	stathz = 0;
    158   1.1    ichiro 
    159   1.1    ichiro 	if (profhz != 0)
    160   1.1    ichiro 		aprint_error("Cannot get %d Hz profclock\n", profhz);
    161   1.1    ichiro 	profhz = 0;
    162   1.1    ichiro 
    163   1.1    ichiro 	/* Report the clock frequency. */
    164   1.1    ichiro 	aprint_normal("clock: hz=%d stathz=%d profhz=%d\n", hz, stathz, profhz);
    165   1.1    ichiro 
    166   1.1    ichiro 	oldirqstate = disable_interrupts(I32_bit);
    167   1.1    ichiro 
    168   1.1    ichiro 	/* Hook up the clock interrupt handler. */
    169   1.1    ichiro 	clock_ih = ixp425_intr_establish(IXP425_INT_TMR0, IPL_CLOCK,
    170   1.1    ichiro 					 ixpclk_intr, NULL);
    171   1.1    ichiro 	if (clock_ih == NULL)
    172   1.1    ichiro 		panic("cpu_initclocks: unable to register timer interrupt");
    173   1.1    ichiro 
    174   1.1    ichiro #if defined(PERFCTRS)
    175   1.1    ichiro 	pmu_ih = ixp425_intr_establish(IXP425_INT_XPMU, IPL_STATCLOCK,
    176   1.1    ichiro 					xscale_pmc_dispatch, NULL);
    177   1.1    ichiro 	if (pmu_ih == NULL)
    178   1.1    ichiro 		panic("cpu_initclocks: unable to register timer interrupt");
    179   1.1    ichiro #endif
    180   1.1    ichiro 
    181   1.1    ichiro 	/* Set up the new clock parameters. */
    182   1.1    ichiro 
    183   1.1    ichiro 	/* clear interrupt */
    184   1.1    ichiro 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_OST_STATUS,
    185   1.1    ichiro 			  OST_WARM_RESET | OST_WDOG_INT | OST_TS_INT |
    186   1.1    ichiro 			  OST_TIM1_INT | OST_TIM0_INT);
    187   1.1    ichiro 
    188   1.1    ichiro 	counts_per_hz = COUNTS_PER_SEC / hz;
    189   1.1    ichiro 
    190   1.1    ichiro 	/* reload value & Timer enable */
    191   1.1    ichiro 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_OST_TIM0_RELOAD,
    192   1.1    ichiro 			  (counts_per_hz & TIMERRELOAD_MASK) | OST_TIMER_EN);
    193   1.1    ichiro 
    194   1.1    ichiro 	restore_interrupts(oldirqstate);
    195   1.1    ichiro }
    196   1.1    ichiro 
    197   1.1    ichiro /*
    198   1.1    ichiro  * setstatclockrate:
    199   1.1    ichiro  *
    200   1.1    ichiro  *	Set the rate of the statistics clock.
    201   1.1    ichiro  *
    202   1.1    ichiro  *	We assume that hz is either stathz or profhz, and that neither
    203   1.1    ichiro  *	will change after being set by cpu_initclocks().  We could
    204   1.1    ichiro  *	recalculate the intervals here, but that would be a pain.
    205   1.1    ichiro  */
    206   1.1    ichiro void
    207   1.8        he setstatclockrate(int newhz)
    208   1.1    ichiro {
    209   1.1    ichiro 
    210   1.1    ichiro 	/*
    211   1.1    ichiro 	 * XXX Use TMR1?
    212   1.1    ichiro 	 */
    213   1.1    ichiro }
    214   1.1    ichiro 
    215   1.1    ichiro /*
    216   1.1    ichiro  * microtime:
    217   1.1    ichiro  *
    218   1.1    ichiro  *	Fill in the specified timeval struct with the current time
    219   1.1    ichiro  *	accurate to the microsecond.
    220   1.1    ichiro  */
    221   1.1    ichiro void
    222   1.1    ichiro microtime(struct timeval *tvp)
    223   1.1    ichiro {
    224   1.1    ichiro 	struct ixpclk_softc* sc = ixpclk_sc;
    225   1.1    ichiro 	static struct timeval lasttv;
    226   1.1    ichiro 	u_int oldirqstate;
    227   1.1    ichiro 	uint32_t counts;
    228   1.1    ichiro 
    229   1.1    ichiro 	oldirqstate = disable_interrupts(I32_bit);
    230   1.1    ichiro 
    231   1.1    ichiro 	counts = counts_per_hz - GET_TIMER_VALUE(sc);
    232   1.1    ichiro 
    233   1.1    ichiro 	/* Fill in the timeval struct. */
    234   1.1    ichiro 	*tvp = time;
    235   1.1    ichiro 	tvp->tv_usec += (counts / COUNTS_PER_USEC);
    236   1.1    ichiro 
    237   1.1    ichiro 	/* Make sure microseconds doesn't overflow. */
    238   1.1    ichiro 	while (tvp->tv_usec >= 1000000) {
    239   1.1    ichiro 		tvp->tv_usec -= 1000000;
    240   1.1    ichiro 		tvp->tv_sec++;
    241   1.1    ichiro 	}
    242   1.1    ichiro 
    243   1.1    ichiro 	/* Make sure the time has advanced. */
    244   1.1    ichiro 	if (tvp->tv_sec == lasttv.tv_sec &&
    245   1.1    ichiro 	    tvp->tv_usec <= lasttv.tv_usec) {
    246   1.1    ichiro 		tvp->tv_usec = lasttv.tv_usec + 1;
    247   1.1    ichiro 		if (tvp->tv_usec >= 1000000) {
    248   1.1    ichiro 			tvp->tv_usec -= 1000000;
    249   1.1    ichiro 			tvp->tv_sec++;
    250   1.1    ichiro 		}
    251   1.1    ichiro 	}
    252   1.1    ichiro 
    253   1.1    ichiro 	lasttv = *tvp;
    254   1.1    ichiro 
    255   1.1    ichiro 	restore_interrupts(oldirqstate);
    256   1.1    ichiro }
    257   1.1    ichiro 
    258   1.1    ichiro /*
    259   1.1    ichiro  * delay:
    260   1.1    ichiro  *
    261   1.1    ichiro  *	Delay for at least N microseconds.
    262   1.1    ichiro  */
    263   1.1    ichiro void
    264   1.1    ichiro delay(u_int n)
    265   1.1    ichiro {
    266   1.5       scw 	u_int32_t first, last;
    267   1.5       scw 	int usecs;
    268   1.5       scw 
    269   1.5       scw 	if (n == 0)
    270   1.5       scw 		return;
    271   1.1    ichiro 
    272   1.1    ichiro 	/*
    273   1.5       scw 	 * Clamp the timeout at a maximum value (about 32 seconds with
    274   1.5       scw 	 * a 66MHz clock). *Nobody* should be delay()ing for anywhere
    275   1.5       scw 	 * near that length of time and if they are, they should be hung
    276   1.5       scw 	 * out to dry.
    277   1.1    ichiro 	 */
    278   1.5       scw 	if (n >= (0x80000000U / COUNTS_PER_USEC))
    279   1.5       scw 		usecs = (0x80000000U / COUNTS_PER_USEC) - 1;
    280   1.5       scw 	else
    281   1.5       scw 		usecs = n * COUNTS_PER_USEC;
    282   1.5       scw 
    283   1.5       scw 	/* Note: Timestamp timer counts *up*, unlike the other timers */
    284   1.5       scw 	first = GET_TS_VALUE();
    285   1.5       scw 
    286   1.5       scw 	while (usecs > 0) {
    287   1.5       scw 		last = GET_TS_VALUE();
    288   1.5       scw 		usecs -= (int)(last - first);
    289   1.5       scw 		first = last;
    290   1.1    ichiro 	}
    291   1.1    ichiro }
    292   1.1    ichiro 
    293   1.1    ichiro /*
    294   1.1    ichiro  * ixpclk_intr:
    295   1.1    ichiro  *
    296   1.1    ichiro  *	Handle the hardclock interrupt.
    297   1.1    ichiro  */
    298   1.1    ichiro int
    299   1.1    ichiro ixpclk_intr(void *arg)
    300   1.1    ichiro {
    301   1.1    ichiro 	struct ixpclk_softc* sc = ixpclk_sc;
    302   1.1    ichiro 	struct clockframe *frame = arg;
    303   1.1    ichiro 
    304   1.1    ichiro 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_OST_STATUS,
    305   1.1    ichiro 			  OST_TIM0_INT);
    306   1.1    ichiro 
    307   1.1    ichiro 	hardclock(frame);
    308   1.1    ichiro 
    309   1.1    ichiro 	return (1);
    310   1.1    ichiro }
    311