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