Home | History | Annotate | Line # | Download | only in xscale
ixp425_timer.c revision 1.2
      1 /*	$NetBSD: ixp425_timer.c,v 1.2 2003/07/26 05:51:11 thorpej Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2003
      5  *	Ichiro FUKUHARA <ichiro (at) ichiro.org>.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by Ichiro FUKUHARA.
     19  * 4. The name of the company nor the name of the author may be used to
     20  *    endorse or promote products derived from this software without specific
     21  *    prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
     27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: ixp425_timer.c,v 1.2 2003/07/26 05:51:11 thorpej Exp $");
     38 
     39 #include "opt_perfctrs.h"
     40 
     41 #include <sys/types.h>
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/kernel.h>
     45 #include <sys/time.h>
     46 #include <sys/device.h>
     47 
     48 #include <dev/clock_subr.h>
     49 
     50 #include <machine/bus.h>
     51 #include <machine/intr.h>
     52 
     53 #include <arm/cpufunc.h>
     54 
     55 #include <arm/xscale/ixp425reg.h>
     56 #include <arm/xscale/ixp425var.h>
     57 #include <arm/xscale/ixp425_sipvar.h>
     58 
     59 static int	ixpclk_match(struct device *, struct cfdata *, void *);
     60 static void	ixpclk_attach(struct device *, struct device *, void *);
     61 
     62 static uint32_t counts_per_hz;
     63 
     64 static void *clock_ih;
     65 
     66 /* callback functions for intr_functions */
     67 int	ixpclk_intr(void *);
     68 
     69 struct ixpclk_softc {
     70         struct device           sc_dev;
     71         bus_addr_t              sc_baseaddr;
     72         bus_space_tag_t         sc_iot;
     73         bus_space_handle_t      sc_ioh;
     74 };
     75 
     76 #define	COUNTS_PER_SEC		66000000	/* 66MHz */
     77 #define	COUNTS_PER_USEC		(COUNTS_PER_SEC / 1000000)
     78 
     79 static struct ixpclk_softc *ixpclk_sc = NULL;
     80 
     81 CFATTACH_DECL(ixpclk, sizeof(struct ixpclk_softc),
     82 		ixpclk_match, ixpclk_attach, NULL, NULL);
     83 
     84 #define GET_TIMER_VALUE(sc)	(bus_space_read_4((sc)->sc_iot,		\
     85 						  (sc)->sc_ioh,		\
     86 						  IXP425_OST_TIM0))
     87 
     88 static int
     89 ixpclk_match(struct device *parent, struct cfdata *match, void *aux)
     90 {
     91         return 2;
     92 }
     93 
     94 static void
     95 ixpclk_attach(struct device *parent, struct device *self, void *aux)
     96 {
     97 	struct ixpclk_softc		*sc = (struct ixpclk_softc*) self;
     98 	struct ixpsip_attach_args	*sa = aux;
     99 
    100 	printf("\n");
    101 
    102 	sc->sc_iot = sa->sa_iot;
    103 	sc->sc_baseaddr = sa->sa_addr;
    104 
    105 	/* using first timer for system ticks */
    106 	if (ixpclk_sc == NULL)
    107 		ixpclk_sc = sc;
    108 	if (bus_space_map(sc->sc_iot, sa->sa_addr, sa->sa_size, 0,
    109 			  &sc->sc_ioh))
    110 		panic("%s: Cannot map registers", self->dv_xname);
    111 
    112 	 aprint_normal("%s: IXP425 Interval Timer\n", sc->sc_dev.dv_xname);
    113 }
    114 
    115 /*
    116  * cpu_initclocks:
    117  *
    118  *	Initialize the clock and get them going.
    119  */
    120 void
    121 cpu_initclocks(void)
    122 {
    123 	struct ixpclk_softc* sc = ixpclk_sc;
    124 	u_int oldirqstate;
    125 #if defined(PERFCTRS)
    126 	void *pmu_ih;
    127 #endif
    128 
    129 	if (hz < 50 || COUNTS_PER_SEC % hz) {
    130 		aprint_error("Cannot get %d Hz clock; using 100 Hz\n", hz);
    131 		hz = 100;
    132 	}
    133 	tick = 1000000 / hz;	/* number of microseconds between interrupts */
    134 	tickfix = 1000000 - (hz * tick);
    135 	if (tickfix) {
    136 		int ftp;
    137 
    138 		ftp = min(ffs(tickfix), ffs(hz));
    139 		tickfix >>= (ftp - 1);
    140 		tickfixinterval = hz >> (ftp - 1);
    141 	}
    142 
    143 	/*
    144 	 * We only have one timer available; stathz and profhz are
    145 	 * always left as 0 (the upper-layer clock code deals with
    146 	 * this situation).
    147 	 */
    148 	if (stathz != 0)
    149 		aprint_error("Cannot get %d Hz statclock\n", stathz);
    150 	stathz = 0;
    151 
    152 	if (profhz != 0)
    153 		aprint_error("Cannot get %d Hz profclock\n", profhz);
    154 	profhz = 0;
    155 
    156 	/* Report the clock frequency. */
    157 	aprint_normal("clock: hz=%d stathz=%d profhz=%d\n", hz, stathz, profhz);
    158 
    159 	oldirqstate = disable_interrupts(I32_bit);
    160 
    161 	/* Hook up the clock interrupt handler. */
    162 	clock_ih = ixp425_intr_establish(IXP425_INT_TMR0, IPL_CLOCK,
    163 					 ixpclk_intr, NULL);
    164 	if (clock_ih == NULL)
    165 		panic("cpu_initclocks: unable to register timer interrupt");
    166 
    167 #if defined(PERFCTRS)
    168 	pmu_ih = ixp425_intr_establish(IXP425_INT_XPMU, IPL_STATCLOCK,
    169 					xscale_pmc_dispatch, NULL);
    170 	if (pmu_ih == NULL)
    171 		panic("cpu_initclocks: unable to register timer interrupt");
    172 #endif
    173 
    174 	/* Set up the new clock parameters. */
    175 
    176 	/* clear interrupt */
    177 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_OST_STATUS,
    178 			  OST_WARM_RESET | OST_WDOG_INT | OST_TS_INT |
    179 			  OST_TIM1_INT | OST_TIM0_INT);
    180 
    181 	counts_per_hz = COUNTS_PER_SEC / hz;
    182 
    183 	/* reload value & Timer enable */
    184 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_OST_TIM0_RELOAD,
    185 			  (counts_per_hz & TIMERRELOAD_MASK) | OST_TIMER_EN);
    186 
    187 	restore_interrupts(oldirqstate);
    188 }
    189 
    190 /*
    191  * setstatclockrate:
    192  *
    193  *	Set the rate of the statistics clock.
    194  *
    195  *	We assume that hz is either stathz or profhz, and that neither
    196  *	will change after being set by cpu_initclocks().  We could
    197  *	recalculate the intervals here, but that would be a pain.
    198  */
    199 void
    200 setstatclockrate(int hz)
    201 {
    202 
    203 	/*
    204 	 * XXX Use TMR1?
    205 	 */
    206 }
    207 
    208 /*
    209  * microtime:
    210  *
    211  *	Fill in the specified timeval struct with the current time
    212  *	accurate to the microsecond.
    213  */
    214 void
    215 microtime(struct timeval *tvp)
    216 {
    217 	struct ixpclk_softc* sc = ixpclk_sc;
    218 	static struct timeval lasttv;
    219 	u_int oldirqstate;
    220 	uint32_t counts;
    221 
    222 	oldirqstate = disable_interrupts(I32_bit);
    223 
    224 	counts = counts_per_hz - GET_TIMER_VALUE(sc);
    225 
    226 	/* Fill in the timeval struct. */
    227 	*tvp = time;
    228 	tvp->tv_usec += (counts / COUNTS_PER_USEC);
    229 
    230 	/* Make sure microseconds doesn't overflow. */
    231 	while (tvp->tv_usec >= 1000000) {
    232 		tvp->tv_usec -= 1000000;
    233 		tvp->tv_sec++;
    234 	}
    235 
    236 	/* Make sure the time has advanced. */
    237 	if (tvp->tv_sec == lasttv.tv_sec &&
    238 	    tvp->tv_usec <= lasttv.tv_usec) {
    239 		tvp->tv_usec = lasttv.tv_usec + 1;
    240 		if (tvp->tv_usec >= 1000000) {
    241 			tvp->tv_usec -= 1000000;
    242 			tvp->tv_sec++;
    243 		}
    244 	}
    245 
    246 	lasttv = *tvp;
    247 
    248 	restore_interrupts(oldirqstate);
    249 }
    250 
    251 /*
    252  * delay:
    253  *
    254  *	Delay for at least N microseconds.
    255  */
    256 void
    257 delay(u_int n)
    258 {
    259 	struct ixpclk_softc* sc = ixpclk_sc;
    260 	uint32_t cur, last, delta, usecs;
    261 
    262 	/*
    263 	 * This works by polling the timer and counting the
    264 	 * number of microseconds that go by.
    265 	 */
    266 	last = GET_TIMER_VALUE(sc);
    267 	delta = usecs = 0;
    268 
    269 	while (n > usecs) {
    270 		cur = GET_TIMER_VALUE(sc);
    271 
    272 		/* Check to see if the timer has wrapped around. */
    273 		if (last < cur)
    274 			delta += (last + (counts_per_hz - cur));
    275 		else
    276 			delta += (last - cur);
    277 
    278 		last = cur;
    279 
    280 		if (delta >= COUNTS_PER_USEC) {
    281 			usecs += delta / COUNTS_PER_USEC;
    282 			delta %= COUNTS_PER_USEC;
    283 		}
    284 	}
    285 }
    286 
    287 todr_chip_handle_t todr_handle;
    288 
    289 /*
    290  * todr_attach:
    291  *
    292  *	Set the specified time-of-day register as the system real-time clock.
    293  */
    294 void
    295 todr_attach(todr_chip_handle_t todr)
    296 {
    297 
    298 	if (todr_handle)
    299 		panic("todr_attach: rtc already configured");
    300 }
    301 
    302 /*
    303  * inittodr:
    304  *
    305  *	Initialize time from the time-of-day register.
    306  */
    307 #define	MINYEAR		2003	/* minimum plausible year */
    308 void
    309 inittodr(time_t base)
    310 {
    311 	time_t deltat;
    312 	int badbase;
    313 
    314 	if (base < (MINYEAR - 1970) * SECYR) {
    315 		printf("WARNING: preposterous time in file system");
    316 		/* read the system clock anyway */
    317 		base = (MINYEAR - 1970) * SECYR;
    318 		badbase = 1;
    319 	} else
    320 		badbase = 0;
    321 
    322 	if (todr_handle == NULL ||
    323 	    todr_gettime(todr_handle, (struct timeval *)&time) != 0 ||
    324 	    time.tv_sec == 0) {
    325 		/*
    326 		 * Believe the time in the file system for lack of
    327 		 * anything better, resetting the TODR.
    328 		 */
    329 		time.tv_sec = base;
    330 		time.tv_usec = 0;
    331 		if (todr_handle != NULL && !badbase) {
    332 			printf("WARNING: preposterous clock chip time\n");
    333 			resettodr();
    334 		}
    335 		goto bad;
    336 	}
    337 
    338 	if (!badbase) {
    339 		/*
    340 		 * See if we tained/lost two or more days; if
    341 		 * so, assume something is amiss.
    342 		 */
    343 		deltat = time.tv_sec - base;
    344 		if (deltat < 0)
    345 			deltat = -deltat;
    346 		if (deltat < 2 * SECDAY)
    347 			return;		/* all is well */
    348 		printf("WARNING: clock %s %ld days\n",
    349 		    time.tv_sec < base ? "lost" : "gained",
    350 		    (long)deltat / SECDAY);
    351 	}
    352  bad:
    353 	printf("WARNING: CHECK AND RESET THE DATE!\n");
    354 }
    355 
    356 /*
    357  * resettodr:
    358  *
    359  *	Reset the time-of-day register with the current time.
    360  */
    361 void
    362 resettodr(void)
    363 {
    364 
    365 	if (time.tv_sec == 0)
    366 		return;
    367 
    368 	if (todr_handle != NULL &&
    369 	    todr_settime(todr_handle, (struct timeval *)&time) != 0)
    370 		printf("resettodr: failed to set time\n");
    371 }
    372 
    373 /*
    374  * ixpclk_intr:
    375  *
    376  *	Handle the hardclock interrupt.
    377  */
    378 int
    379 ixpclk_intr(void *arg)
    380 {
    381 	struct ixpclk_softc* sc = ixpclk_sc;
    382 	struct clockframe *frame = arg;
    383 
    384 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_OST_STATUS,
    385 			  OST_TIM0_INT);
    386 
    387 	hardclock(frame);
    388 
    389 	return (1);
    390 }
    391