Home | History | Annotate | Line # | Download | only in xscale
ixp425_timer.c revision 1.17
      1  1.17  msaitoh /*	$NetBSD: ixp425_timer.c,v 1.17 2012/10/14 14:20:58 msaitoh 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  *
     17   1.1   ichiro  * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
     18   1.1   ichiro  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19   1.1   ichiro  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20   1.1   ichiro  * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
     21   1.1   ichiro  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22   1.1   ichiro  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23   1.1   ichiro  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24   1.1   ichiro  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25   1.1   ichiro  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26   1.1   ichiro  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27   1.1   ichiro  * SUCH DAMAGE.
     28   1.1   ichiro  */
     29   1.1   ichiro 
     30   1.1   ichiro #include <sys/cdefs.h>
     31  1.17  msaitoh __KERNEL_RCSID(0, "$NetBSD: ixp425_timer.c,v 1.17 2012/10/14 14:20:58 msaitoh Exp $");
     32   1.1   ichiro 
     33  1.10      scw #include "opt_ixp425.h"
     34   1.1   ichiro #include "opt_perfctrs.h"
     35   1.1   ichiro 
     36   1.1   ichiro #include <sys/types.h>
     37   1.1   ichiro #include <sys/param.h>
     38   1.1   ichiro #include <sys/systm.h>
     39   1.1   ichiro #include <sys/kernel.h>
     40  1.14    joerg #include <sys/atomic.h>
     41   1.1   ichiro #include <sys/time.h>
     42  1.14    joerg #include <sys/timetc.h>
     43   1.1   ichiro #include <sys/device.h>
     44   1.1   ichiro 
     45   1.2  thorpej #include <dev/clock_subr.h>
     46   1.2  thorpej 
     47  1.16   dyoung #include <sys/bus.h>
     48   1.1   ichiro #include <machine/intr.h>
     49   1.1   ichiro 
     50   1.1   ichiro #include <arm/cpufunc.h>
     51   1.1   ichiro 
     52   1.1   ichiro #include <arm/xscale/ixp425reg.h>
     53   1.1   ichiro #include <arm/xscale/ixp425var.h>
     54   1.1   ichiro #include <arm/xscale/ixp425_sipvar.h>
     55   1.1   ichiro 
     56  1.17  msaitoh static int	ixpclk_match(device_t, cfdata_t, void *);
     57  1.17  msaitoh static void	ixpclk_attach(device_t, device_t, void *);
     58  1.14    joerg static u_int	ixpclk_get_timecount(struct timecounter *);
     59   1.1   ichiro 
     60   1.1   ichiro static uint32_t counts_per_hz;
     61   1.1   ichiro 
     62   1.1   ichiro static void *clock_ih;
     63   1.1   ichiro 
     64   1.1   ichiro /* callback functions for intr_functions */
     65   1.1   ichiro int	ixpclk_intr(void *);
     66   1.1   ichiro 
     67   1.1   ichiro struct ixpclk_softc {
     68  1.11   simonb 	bus_addr_t		sc_baseaddr;
     69  1.11   simonb 	bus_space_tag_t		sc_iot;
     70  1.11   simonb 	bus_space_handle_t      sc_ioh;
     71   1.1   ichiro };
     72   1.1   ichiro 
     73  1.10      scw #ifndef IXP425_CLOCK_FREQ
     74   1.4      scw #define	COUNTS_PER_SEC		66666600	/* 66MHz */
     75  1.10      scw #else
     76  1.10      scw #define	COUNTS_PER_SEC		IXP425_CLOCK_FREQ
     77  1.10      scw #endif
     78   1.4      scw #define	COUNTS_PER_USEC		((COUNTS_PER_SEC / 1000000) + 1)
     79   1.1   ichiro 
     80   1.4      scw static struct ixpclk_softc *ixpclk_sc;
     81   1.1   ichiro 
     82  1.14    joerg static struct timecounter ixpclk_timecounter = {
     83  1.14    joerg 	ixpclk_get_timecount,	/* get_timecount */
     84  1.14    joerg 	0,			/* no poll_pps */
     85  1.14    joerg 	0xffffffff,		/* counter_mask */
     86  1.14    joerg 	COUNTS_PER_SEC,		/* frequency */
     87  1.14    joerg 	"ixpclk",		/* name */
     88  1.14    joerg 	100,			/* quality */
     89  1.14    joerg 	NULL,			/* prev */
     90  1.14    joerg 	NULL,			/* next */
     91  1.14    joerg };
     92  1.14    joerg 
     93  1.14    joerg static volatile uint32_t ixpclk_base;
     94  1.14    joerg 
     95  1.17  msaitoh CFATTACH_DECL_NEW(ixpclk, sizeof(struct ixpclk_softc),
     96   1.1   ichiro 		ixpclk_match, ixpclk_attach, NULL, NULL);
     97   1.1   ichiro 
     98   1.1   ichiro #define GET_TIMER_VALUE(sc)	(bus_space_read_4((sc)->sc_iot,		\
     99   1.1   ichiro 						  (sc)->sc_ioh,		\
    100   1.1   ichiro 						  IXP425_OST_TIM0))
    101   1.1   ichiro 
    102   1.5      scw #define GET_TS_VALUE(sc)	(*(volatile u_int32_t *) \
    103   1.5      scw 				  (IXP425_TIMER_VBASE + IXP425_OST_TS))
    104   1.5      scw 
    105   1.1   ichiro static int
    106  1.17  msaitoh ixpclk_match(device_t parent, cfdata_t match, void *aux)
    107   1.1   ichiro {
    108  1.11   simonb 	return 2;
    109   1.1   ichiro }
    110   1.1   ichiro 
    111   1.1   ichiro static void
    112  1.17  msaitoh ixpclk_attach(device_t parent, device_t self, void *aux)
    113   1.1   ichiro {
    114  1.17  msaitoh 	struct ixpclk_softc		*sc = device_private(self);
    115   1.1   ichiro 	struct ixpsip_attach_args	*sa = aux;
    116   1.1   ichiro 
    117   1.1   ichiro 	printf("\n");
    118   1.1   ichiro 
    119   1.5      scw 	ixpclk_sc = sc;
    120   1.5      scw 
    121   1.1   ichiro 	sc->sc_iot = sa->sa_iot;
    122   1.1   ichiro 	sc->sc_baseaddr = sa->sa_addr;
    123   1.1   ichiro 
    124   1.1   ichiro 	if (bus_space_map(sc->sc_iot, sa->sa_addr, sa->sa_size, 0,
    125   1.1   ichiro 			  &sc->sc_ioh))
    126  1.17  msaitoh 		panic("%s: Cannot map registers", device_xname(self));
    127   1.1   ichiro 
    128  1.17  msaitoh 	aprint_normal_dev(self, "IXP425 Interval Timer\n");
    129   1.1   ichiro }
    130   1.1   ichiro 
    131   1.1   ichiro /*
    132   1.1   ichiro  * cpu_initclocks:
    133   1.1   ichiro  *
    134   1.1   ichiro  *	Initialize the clock and get them going.
    135   1.1   ichiro  */
    136   1.1   ichiro void
    137   1.1   ichiro cpu_initclocks(void)
    138   1.1   ichiro {
    139  1.17  msaitoh 	struct ixpclk_softc *sc = ixpclk_sc;
    140   1.1   ichiro 	u_int oldirqstate;
    141   1.1   ichiro #if defined(PERFCTRS)
    142   1.1   ichiro 	void *pmu_ih;
    143   1.1   ichiro #endif
    144   1.1   ichiro 
    145   1.1   ichiro 	if (hz < 50 || COUNTS_PER_SEC % hz) {
    146   1.1   ichiro 		aprint_error("Cannot get %d Hz clock; using 100 Hz\n", hz);
    147   1.1   ichiro 		hz = 100;
    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.14    joerg 
    196  1.14    joerg 	tc_init(&ixpclk_timecounter);
    197   1.1   ichiro }
    198   1.1   ichiro 
    199   1.1   ichiro /*
    200   1.1   ichiro  * setstatclockrate:
    201   1.1   ichiro  *
    202   1.1   ichiro  *	Set the rate of the statistics clock.
    203   1.1   ichiro  *
    204   1.1   ichiro  *	We assume that hz is either stathz or profhz, and that neither
    205   1.1   ichiro  *	will change after being set by cpu_initclocks().  We could
    206   1.1   ichiro  *	recalculate the intervals here, but that would be a pain.
    207   1.1   ichiro  */
    208   1.1   ichiro void
    209   1.8       he setstatclockrate(int newhz)
    210   1.1   ichiro {
    211   1.1   ichiro 
    212   1.1   ichiro 	/*
    213   1.1   ichiro 	 * XXX Use TMR1?
    214   1.1   ichiro 	 */
    215   1.1   ichiro }
    216   1.1   ichiro 
    217  1.14    joerg static u_int
    218  1.14    joerg ixpclk_get_timecount(struct timecounter *tc)
    219   1.1   ichiro {
    220  1.14    joerg 	u_int	savedints, base, counter;
    221   1.1   ichiro 
    222  1.14    joerg 	savedints = disable_interrupts(I32_bit);
    223  1.14    joerg 	base = ixpclk_base;
    224  1.14    joerg 	counter = GET_TIMER_VALUE(ixpclk_sc);
    225  1.14    joerg 	restore_interrupts(savedints);
    226   1.1   ichiro 
    227  1.14    joerg 	return base - counter;
    228   1.1   ichiro }
    229   1.1   ichiro 
    230   1.1   ichiro /*
    231   1.1   ichiro  * delay:
    232   1.1   ichiro  *
    233   1.1   ichiro  *	Delay for at least N microseconds.
    234   1.1   ichiro  */
    235   1.1   ichiro void
    236   1.1   ichiro delay(u_int n)
    237   1.1   ichiro {
    238   1.5      scw 	u_int32_t first, last;
    239   1.5      scw 	int usecs;
    240   1.5      scw 
    241   1.5      scw 	if (n == 0)
    242   1.5      scw 		return;
    243   1.1   ichiro 
    244   1.1   ichiro 	/*
    245   1.5      scw 	 * Clamp the timeout at a maximum value (about 32 seconds with
    246   1.5      scw 	 * a 66MHz clock). *Nobody* should be delay()ing for anywhere
    247   1.5      scw 	 * near that length of time and if they are, they should be hung
    248   1.5      scw 	 * out to dry.
    249   1.1   ichiro 	 */
    250   1.5      scw 	if (n >= (0x80000000U / COUNTS_PER_USEC))
    251   1.5      scw 		usecs = (0x80000000U / COUNTS_PER_USEC) - 1;
    252   1.5      scw 	else
    253   1.5      scw 		usecs = n * COUNTS_PER_USEC;
    254   1.5      scw 
    255   1.5      scw 	/* Note: Timestamp timer counts *up*, unlike the other timers */
    256   1.5      scw 	first = GET_TS_VALUE();
    257   1.5      scw 
    258   1.5      scw 	while (usecs > 0) {
    259   1.5      scw 		last = GET_TS_VALUE();
    260   1.5      scw 		usecs -= (int)(last - first);
    261   1.5      scw 		first = last;
    262   1.1   ichiro 	}
    263   1.1   ichiro }
    264   1.1   ichiro 
    265   1.1   ichiro /*
    266   1.1   ichiro  * ixpclk_intr:
    267   1.1   ichiro  *
    268   1.1   ichiro  *	Handle the hardclock interrupt.
    269   1.1   ichiro  */
    270   1.1   ichiro int
    271   1.1   ichiro ixpclk_intr(void *arg)
    272   1.1   ichiro {
    273  1.17  msaitoh 	struct ixpclk_softc *sc = ixpclk_sc;
    274   1.1   ichiro 	struct clockframe *frame = arg;
    275   1.1   ichiro 
    276   1.1   ichiro 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_OST_STATUS,
    277   1.1   ichiro 			  OST_TIM0_INT);
    278   1.1   ichiro 
    279  1.14    joerg 	atomic_add_32(&ixpclk_base, counts_per_hz);
    280  1.14    joerg 
    281   1.1   ichiro 	hardclock(frame);
    282   1.1   ichiro 
    283   1.1   ichiro 	return (1);
    284   1.1   ichiro }
    285