Home | History | Annotate | Line # | Download | only in xscale
ixp425_timer.c revision 1.4
      1  1.4      scw /*	$NetBSD: ixp425_timer.c,v 1.4 2003/10/08 14:55:04 scw 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.4      scw __KERNEL_RCSID(0, "$NetBSD: ixp425_timer.c,v 1.4 2003/10/08 14:55:04 scw Exp $");
     38  1.1   ichiro 
     39  1.1   ichiro #include "opt_perfctrs.h"
     40  1.1   ichiro 
     41  1.1   ichiro #include <sys/types.h>
     42  1.1   ichiro #include <sys/param.h>
     43  1.1   ichiro #include <sys/systm.h>
     44  1.1   ichiro #include <sys/kernel.h>
     45  1.1   ichiro #include <sys/time.h>
     46  1.1   ichiro #include <sys/device.h>
     47  1.1   ichiro 
     48  1.2  thorpej #include <dev/clock_subr.h>
     49  1.2  thorpej 
     50  1.1   ichiro #include <machine/bus.h>
     51  1.1   ichiro #include <machine/intr.h>
     52  1.1   ichiro 
     53  1.1   ichiro #include <arm/cpufunc.h>
     54  1.1   ichiro 
     55  1.1   ichiro #include <arm/xscale/ixp425reg.h>
     56  1.1   ichiro #include <arm/xscale/ixp425var.h>
     57  1.1   ichiro #include <arm/xscale/ixp425_sipvar.h>
     58  1.1   ichiro 
     59  1.1   ichiro static int	ixpclk_match(struct device *, struct cfdata *, void *);
     60  1.1   ichiro static void	ixpclk_attach(struct device *, struct device *, void *);
     61  1.1   ichiro 
     62  1.1   ichiro static uint32_t counts_per_hz;
     63  1.1   ichiro 
     64  1.1   ichiro static void *clock_ih;
     65  1.1   ichiro 
     66  1.1   ichiro /* callback functions for intr_functions */
     67  1.1   ichiro int	ixpclk_intr(void *);
     68  1.1   ichiro 
     69  1.1   ichiro struct ixpclk_softc {
     70  1.1   ichiro         struct device           sc_dev;
     71  1.1   ichiro         bus_addr_t              sc_baseaddr;
     72  1.1   ichiro         bus_space_tag_t         sc_iot;
     73  1.1   ichiro         bus_space_handle_t      sc_ioh;
     74  1.1   ichiro };
     75  1.1   ichiro 
     76  1.4      scw #define	COUNTS_PER_SEC		66666600	/* 66MHz */
     77  1.4      scw #define	COUNTS_PER_USEC		((COUNTS_PER_SEC / 1000000) + 1)
     78  1.1   ichiro 
     79  1.4      scw static struct ixpclk_softc *ixpclk_sc;
     80  1.4      scw static int ixpclk_first_timer;
     81  1.1   ichiro 
     82  1.1   ichiro CFATTACH_DECL(ixpclk, sizeof(struct ixpclk_softc),
     83  1.1   ichiro 		ixpclk_match, ixpclk_attach, NULL, NULL);
     84  1.1   ichiro 
     85  1.1   ichiro #define GET_TIMER_VALUE(sc)	(bus_space_read_4((sc)->sc_iot,		\
     86  1.1   ichiro 						  (sc)->sc_ioh,		\
     87  1.1   ichiro 						  IXP425_OST_TIM0))
     88  1.1   ichiro 
     89  1.1   ichiro static int
     90  1.1   ichiro ixpclk_match(struct device *parent, struct cfdata *match, void *aux)
     91  1.1   ichiro {
     92  1.1   ichiro         return 2;
     93  1.1   ichiro }
     94  1.1   ichiro 
     95  1.1   ichiro static void
     96  1.1   ichiro ixpclk_attach(struct device *parent, struct device *self, void *aux)
     97  1.1   ichiro {
     98  1.1   ichiro 	struct ixpclk_softc		*sc = (struct ixpclk_softc*) self;
     99  1.1   ichiro 	struct ixpsip_attach_args	*sa = aux;
    100  1.1   ichiro 
    101  1.1   ichiro 	printf("\n");
    102  1.1   ichiro 
    103  1.1   ichiro 	sc->sc_iot = sa->sa_iot;
    104  1.1   ichiro 	sc->sc_baseaddr = sa->sa_addr;
    105  1.1   ichiro 
    106  1.1   ichiro 	/* using first timer for system ticks */
    107  1.4      scw 	if (!ixpclk_first_timer) {
    108  1.4      scw 		ixpclk_first_timer = 1;
    109  1.1   ichiro 		ixpclk_sc = sc;
    110  1.4      scw 	}
    111  1.1   ichiro 	if (bus_space_map(sc->sc_iot, sa->sa_addr, sa->sa_size, 0,
    112  1.1   ichiro 			  &sc->sc_ioh))
    113  1.1   ichiro 		panic("%s: Cannot map registers", self->dv_xname);
    114  1.1   ichiro 
    115  1.1   ichiro 	 aprint_normal("%s: IXP425 Interval Timer\n", sc->sc_dev.dv_xname);
    116  1.4      scw }
    117  1.4      scw 
    118  1.4      scw void
    119  1.4      scw ixp425_clk_bootstrap(bus_space_tag_t bt)
    120  1.4      scw {
    121  1.4      scw 	static struct ixpclk_softc sc;
    122  1.4      scw 	ixpclk_sc = &sc;
    123  1.4      scw 
    124  1.4      scw 	sc.sc_iot = bt;
    125  1.4      scw 	sc.sc_baseaddr = IXP425_TIMER_HWBASE;
    126  1.4      scw 
    127  1.4      scw 	if (bus_space_map(sc.sc_iot, sc.sc_baseaddr, 0x30, 0, &sc.sc_ioh))
    128  1.4      scw 		panic("ixp425_clk_bootstrap: Cannot map registers");
    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.1   ichiro 	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 	tick = 1000000 / hz;	/* number of microseconds between interrupts */
    150  1.1   ichiro 	tickfix = 1000000 - (hz * tick);
    151  1.1   ichiro 	if (tickfix) {
    152  1.1   ichiro 		int ftp;
    153  1.1   ichiro 
    154  1.1   ichiro 		ftp = min(ffs(tickfix), ffs(hz));
    155  1.1   ichiro 		tickfix >>= (ftp - 1);
    156  1.1   ichiro 		tickfixinterval = hz >> (ftp - 1);
    157  1.1   ichiro 	}
    158  1.1   ichiro 
    159  1.1   ichiro 	/*
    160  1.1   ichiro 	 * We only have one timer available; stathz and profhz are
    161  1.1   ichiro 	 * always left as 0 (the upper-layer clock code deals with
    162  1.1   ichiro 	 * this situation).
    163  1.1   ichiro 	 */
    164  1.1   ichiro 	if (stathz != 0)
    165  1.1   ichiro 		aprint_error("Cannot get %d Hz statclock\n", stathz);
    166  1.1   ichiro 	stathz = 0;
    167  1.1   ichiro 
    168  1.1   ichiro 	if (profhz != 0)
    169  1.1   ichiro 		aprint_error("Cannot get %d Hz profclock\n", profhz);
    170  1.1   ichiro 	profhz = 0;
    171  1.1   ichiro 
    172  1.1   ichiro 	/* Report the clock frequency. */
    173  1.1   ichiro 	aprint_normal("clock: hz=%d stathz=%d profhz=%d\n", hz, stathz, profhz);
    174  1.1   ichiro 
    175  1.1   ichiro 	oldirqstate = disable_interrupts(I32_bit);
    176  1.1   ichiro 
    177  1.1   ichiro 	/* Hook up the clock interrupt handler. */
    178  1.1   ichiro 	clock_ih = ixp425_intr_establish(IXP425_INT_TMR0, IPL_CLOCK,
    179  1.1   ichiro 					 ixpclk_intr, NULL);
    180  1.1   ichiro 	if (clock_ih == NULL)
    181  1.1   ichiro 		panic("cpu_initclocks: unable to register timer interrupt");
    182  1.1   ichiro 
    183  1.1   ichiro #if defined(PERFCTRS)
    184  1.1   ichiro 	pmu_ih = ixp425_intr_establish(IXP425_INT_XPMU, IPL_STATCLOCK,
    185  1.1   ichiro 					xscale_pmc_dispatch, NULL);
    186  1.1   ichiro 	if (pmu_ih == NULL)
    187  1.1   ichiro 		panic("cpu_initclocks: unable to register timer interrupt");
    188  1.1   ichiro #endif
    189  1.1   ichiro 
    190  1.1   ichiro 	/* Set up the new clock parameters. */
    191  1.1   ichiro 
    192  1.1   ichiro 	/* clear interrupt */
    193  1.1   ichiro 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_OST_STATUS,
    194  1.1   ichiro 			  OST_WARM_RESET | OST_WDOG_INT | OST_TS_INT |
    195  1.1   ichiro 			  OST_TIM1_INT | OST_TIM0_INT);
    196  1.1   ichiro 
    197  1.1   ichiro 	counts_per_hz = COUNTS_PER_SEC / hz;
    198  1.1   ichiro 
    199  1.1   ichiro 	/* reload value & Timer enable */
    200  1.1   ichiro 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_OST_TIM0_RELOAD,
    201  1.1   ichiro 			  (counts_per_hz & TIMERRELOAD_MASK) | OST_TIMER_EN);
    202  1.1   ichiro 
    203  1.1   ichiro 	restore_interrupts(oldirqstate);
    204  1.1   ichiro }
    205  1.1   ichiro 
    206  1.1   ichiro /*
    207  1.1   ichiro  * setstatclockrate:
    208  1.1   ichiro  *
    209  1.1   ichiro  *	Set the rate of the statistics clock.
    210  1.1   ichiro  *
    211  1.1   ichiro  *	We assume that hz is either stathz or profhz, and that neither
    212  1.1   ichiro  *	will change after being set by cpu_initclocks().  We could
    213  1.1   ichiro  *	recalculate the intervals here, but that would be a pain.
    214  1.1   ichiro  */
    215  1.1   ichiro void
    216  1.1   ichiro setstatclockrate(int hz)
    217  1.1   ichiro {
    218  1.1   ichiro 
    219  1.1   ichiro 	/*
    220  1.1   ichiro 	 * XXX Use TMR1?
    221  1.1   ichiro 	 */
    222  1.1   ichiro }
    223  1.1   ichiro 
    224  1.1   ichiro /*
    225  1.1   ichiro  * microtime:
    226  1.1   ichiro  *
    227  1.1   ichiro  *	Fill in the specified timeval struct with the current time
    228  1.1   ichiro  *	accurate to the microsecond.
    229  1.1   ichiro  */
    230  1.1   ichiro void
    231  1.1   ichiro microtime(struct timeval *tvp)
    232  1.1   ichiro {
    233  1.1   ichiro 	struct ixpclk_softc* sc = ixpclk_sc;
    234  1.1   ichiro 	static struct timeval lasttv;
    235  1.1   ichiro 	u_int oldirqstate;
    236  1.1   ichiro 	uint32_t counts;
    237  1.1   ichiro 
    238  1.1   ichiro 	oldirqstate = disable_interrupts(I32_bit);
    239  1.1   ichiro 
    240  1.1   ichiro 	counts = counts_per_hz - GET_TIMER_VALUE(sc);
    241  1.1   ichiro 
    242  1.1   ichiro 	/* Fill in the timeval struct. */
    243  1.1   ichiro 	*tvp = time;
    244  1.1   ichiro 	tvp->tv_usec += (counts / COUNTS_PER_USEC);
    245  1.1   ichiro 
    246  1.1   ichiro 	/* Make sure microseconds doesn't overflow. */
    247  1.1   ichiro 	while (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 	/* Make sure the time has advanced. */
    253  1.1   ichiro 	if (tvp->tv_sec == lasttv.tv_sec &&
    254  1.1   ichiro 	    tvp->tv_usec <= lasttv.tv_usec) {
    255  1.1   ichiro 		tvp->tv_usec = lasttv.tv_usec + 1;
    256  1.1   ichiro 		if (tvp->tv_usec >= 1000000) {
    257  1.1   ichiro 			tvp->tv_usec -= 1000000;
    258  1.1   ichiro 			tvp->tv_sec++;
    259  1.1   ichiro 		}
    260  1.1   ichiro 	}
    261  1.1   ichiro 
    262  1.1   ichiro 	lasttv = *tvp;
    263  1.1   ichiro 
    264  1.1   ichiro 	restore_interrupts(oldirqstate);
    265  1.1   ichiro }
    266  1.1   ichiro 
    267  1.1   ichiro /*
    268  1.1   ichiro  * delay:
    269  1.1   ichiro  *
    270  1.1   ichiro  *	Delay for at least N microseconds.
    271  1.1   ichiro  */
    272  1.1   ichiro void
    273  1.1   ichiro delay(u_int n)
    274  1.1   ichiro {
    275  1.1   ichiro 	struct ixpclk_softc* sc = ixpclk_sc;
    276  1.1   ichiro 	uint32_t cur, last, delta, usecs;
    277  1.1   ichiro 
    278  1.1   ichiro 	/*
    279  1.1   ichiro 	 * This works by polling the timer and counting the
    280  1.1   ichiro 	 * number of microseconds that go by.
    281  1.1   ichiro 	 */
    282  1.1   ichiro 	last = GET_TIMER_VALUE(sc);
    283  1.1   ichiro 	delta = usecs = 0;
    284  1.1   ichiro 
    285  1.1   ichiro 	while (n > usecs) {
    286  1.1   ichiro 		cur = GET_TIMER_VALUE(sc);
    287  1.1   ichiro 
    288  1.1   ichiro 		/* Check to see if the timer has wrapped around. */
    289  1.1   ichiro 		if (last < cur)
    290  1.1   ichiro 			delta += (last + (counts_per_hz - cur));
    291  1.1   ichiro 		else
    292  1.1   ichiro 			delta += (last - cur);
    293  1.1   ichiro 
    294  1.1   ichiro 		last = cur;
    295  1.1   ichiro 
    296  1.1   ichiro 		if (delta >= COUNTS_PER_USEC) {
    297  1.1   ichiro 			usecs += delta / COUNTS_PER_USEC;
    298  1.1   ichiro 			delta %= COUNTS_PER_USEC;
    299  1.1   ichiro 		}
    300  1.1   ichiro 	}
    301  1.1   ichiro }
    302  1.1   ichiro 
    303  1.2  thorpej todr_chip_handle_t todr_handle;
    304  1.2  thorpej 
    305  1.2  thorpej /*
    306  1.2  thorpej  * todr_attach:
    307  1.2  thorpej  *
    308  1.2  thorpej  *	Set the specified time-of-day register as the system real-time clock.
    309  1.2  thorpej  */
    310  1.2  thorpej void
    311  1.2  thorpej todr_attach(todr_chip_handle_t todr)
    312  1.2  thorpej {
    313  1.2  thorpej 
    314  1.2  thorpej 	if (todr_handle)
    315  1.2  thorpej 		panic("todr_attach: rtc already configured");
    316  1.3  thorpej 	todr_handle = todr;
    317  1.2  thorpej }
    318  1.2  thorpej 
    319  1.1   ichiro /*
    320  1.1   ichiro  * inittodr:
    321  1.1   ichiro  *
    322  1.1   ichiro  *	Initialize time from the time-of-day register.
    323  1.1   ichiro  */
    324  1.2  thorpej #define	MINYEAR		2003	/* minimum plausible year */
    325  1.1   ichiro void
    326  1.1   ichiro inittodr(time_t base)
    327  1.1   ichiro {
    328  1.2  thorpej 	time_t deltat;
    329  1.2  thorpej 	int badbase;
    330  1.2  thorpej 
    331  1.2  thorpej 	if (base < (MINYEAR - 1970) * SECYR) {
    332  1.2  thorpej 		printf("WARNING: preposterous time in file system");
    333  1.2  thorpej 		/* read the system clock anyway */
    334  1.2  thorpej 		base = (MINYEAR - 1970) * SECYR;
    335  1.2  thorpej 		badbase = 1;
    336  1.2  thorpej 	} else
    337  1.2  thorpej 		badbase = 0;
    338  1.2  thorpej 
    339  1.2  thorpej 	if (todr_handle == NULL ||
    340  1.2  thorpej 	    todr_gettime(todr_handle, (struct timeval *)&time) != 0 ||
    341  1.2  thorpej 	    time.tv_sec == 0) {
    342  1.2  thorpej 		/*
    343  1.2  thorpej 		 * Believe the time in the file system for lack of
    344  1.2  thorpej 		 * anything better, resetting the TODR.
    345  1.2  thorpej 		 */
    346  1.2  thorpej 		time.tv_sec = base;
    347  1.2  thorpej 		time.tv_usec = 0;
    348  1.2  thorpej 		if (todr_handle != NULL && !badbase) {
    349  1.2  thorpej 			printf("WARNING: preposterous clock chip time\n");
    350  1.2  thorpej 			resettodr();
    351  1.2  thorpej 		}
    352  1.2  thorpej 		goto bad;
    353  1.2  thorpej 	}
    354  1.1   ichiro 
    355  1.2  thorpej 	if (!badbase) {
    356  1.2  thorpej 		/*
    357  1.2  thorpej 		 * See if we tained/lost two or more days; if
    358  1.2  thorpej 		 * so, assume something is amiss.
    359  1.2  thorpej 		 */
    360  1.2  thorpej 		deltat = time.tv_sec - base;
    361  1.2  thorpej 		if (deltat < 0)
    362  1.2  thorpej 			deltat = -deltat;
    363  1.2  thorpej 		if (deltat < 2 * SECDAY)
    364  1.2  thorpej 			return;		/* all is well */
    365  1.2  thorpej 		printf("WARNING: clock %s %ld days\n",
    366  1.2  thorpej 		    time.tv_sec < base ? "lost" : "gained",
    367  1.2  thorpej 		    (long)deltat / SECDAY);
    368  1.2  thorpej 	}
    369  1.2  thorpej  bad:
    370  1.2  thorpej 	printf("WARNING: CHECK AND RESET THE DATE!\n");
    371  1.1   ichiro }
    372  1.1   ichiro 
    373  1.1   ichiro /*
    374  1.1   ichiro  * resettodr:
    375  1.1   ichiro  *
    376  1.1   ichiro  *	Reset the time-of-day register with the current time.
    377  1.1   ichiro  */
    378  1.1   ichiro void
    379  1.1   ichiro resettodr(void)
    380  1.1   ichiro {
    381  1.2  thorpej 
    382  1.2  thorpej 	if (time.tv_sec == 0)
    383  1.2  thorpej 		return;
    384  1.2  thorpej 
    385  1.2  thorpej 	if (todr_handle != NULL &&
    386  1.2  thorpej 	    todr_settime(todr_handle, (struct timeval *)&time) != 0)
    387  1.2  thorpej 		printf("resettodr: failed to set time\n");
    388  1.1   ichiro }
    389  1.1   ichiro 
    390  1.1   ichiro /*
    391  1.1   ichiro  * ixpclk_intr:
    392  1.1   ichiro  *
    393  1.1   ichiro  *	Handle the hardclock interrupt.
    394  1.1   ichiro  */
    395  1.1   ichiro int
    396  1.1   ichiro ixpclk_intr(void *arg)
    397  1.1   ichiro {
    398  1.1   ichiro 	struct ixpclk_softc* sc = ixpclk_sc;
    399  1.1   ichiro 	struct clockframe *frame = arg;
    400  1.1   ichiro 
    401  1.1   ichiro 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_OST_STATUS,
    402  1.1   ichiro 			  OST_TIM0_INT);
    403  1.1   ichiro 
    404  1.1   ichiro 	hardclock(frame);
    405  1.1   ichiro 
    406  1.1   ichiro 	return (1);
    407  1.1   ichiro }
    408