Home | History | Annotate | Line # | Download | only in iq80310
iq80310_timer.c revision 1.13
      1 /*	$NetBSD: iq80310_timer.c,v 1.13 2003/07/26 05:55:03 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001, 2002 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed for the NetBSD Project by
     20  *	Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Timer/clock support for the Intel IQ80310.
     40  *
     41  * The IQ80310 has a 22-bit reloadable timer implemented in the CPLD.
     42  * We use it to provide a hardclock interrupt.  There is no RTC on
     43  * the IQ80310.
     44  *
     45  * The timer uses the SPCI clock.  The timer uses the 33MHz clock by
     46  * reading the SPCI_66EN signal and dividing the clock if necessary.
     47  */
     48 
     49 #include <sys/cdefs.h>
     50 __KERNEL_RCSID(0, "$NetBSD: iq80310_timer.c,v 1.13 2003/07/26 05:55:03 thorpej Exp $");
     51 
     52 #include <sys/param.h>
     53 #include <sys/systm.h>
     54 #include <sys/kernel.h>
     55 #include <sys/time.h>
     56 
     57 #include <dev/clock_subr.h>
     58 
     59 #include <machine/bus.h>
     60 #include <arm/cpufunc.h>
     61 
     62 #include <evbarm/iq80310/iq80310reg.h>
     63 #include <evbarm/iq80310/iq80310var.h>
     64 #include <evbarm/iq80310/obiovar.h>
     65 
     66 /*
     67  * Some IQ80310-based designs have fewer bits in the timer counter.
     68  * Deal with them here.
     69  */
     70 #if defined(IOP310_TEAMASA_NPWR)
     71 #define	COUNTER_MASK		0x0007ffff
     72 #else /* Default to stock IQ80310 */
     73 #define	COUNTER_MASK		0x003fffff
     74 #endif /* list of IQ80310-based designs */
     75 
     76 #define	COUNTS_PER_SEC		33000000	/* 33MHz */
     77 #define	COUNTS_PER_USEC		(COUNTS_PER_SEC / 1000000)
     78 
     79 static void *clock_ih;
     80 
     81 static uint32_t counts_per_hz;
     82 
     83 int	clockhandler(void *);
     84 
     85 static __inline void
     86 timer_enable(uint8_t bit)
     87 {
     88 
     89 	CPLD_WRITE(IQ80310_TIMER_ENABLE,
     90 	    CPLD_READ(IQ80310_TIMER_ENABLE) | bit);
     91 }
     92 
     93 static __inline void
     94 timer_disable(uint8_t bit)
     95 {
     96 
     97 	CPLD_WRITE(IQ80310_TIMER_ENABLE,
     98 	    CPLD_READ(IQ80310_TIMER_ENABLE) & ~bit);
     99 }
    100 
    101 static __inline uint32_t
    102 timer_read(void)
    103 {
    104 	uint32_t rv;
    105 	uint8_t la0, la1, la2, la3;
    106 
    107 	/*
    108 	 * First read latches count.
    109 	 *
    110 	 * From RedBoot: harware bug that causes invalid counts to be
    111 	 * latched.  The loop appears to work around the problem.
    112 	 */
    113 	do {
    114 		la0 = CPLD_READ(IQ80310_TIMER_LA0);
    115 	} while (la0 == 0);
    116 	la1 = CPLD_READ(IQ80310_TIMER_LA1);
    117 	la2 = CPLD_READ(IQ80310_TIMER_LA2);
    118 	la3 = CPLD_READ(IQ80310_TIMER_LA3);
    119 
    120 	rv  =  ((la0 & 0x40) >> 1) | (la0 & 0x1f);
    121 	rv |= (((la1 & 0x40) >> 1) | (la1 & 0x1f)) << 6;
    122 	rv |= (((la2 & 0x40) >> 1) | (la2 & 0x1f)) << 12;
    123 	rv |= (la3 & 0x0f) << 18;
    124 
    125 	return (rv);
    126 }
    127 
    128 static __inline void
    129 timer_write(uint32_t x)
    130 {
    131 
    132 	KASSERT((x & COUNTER_MASK) == x);
    133 
    134 	CPLD_WRITE(IQ80310_TIMER_LA0, x & 0xff);
    135 	CPLD_WRITE(IQ80310_TIMER_LA1, (x >> 8) & 0xff);
    136 	CPLD_WRITE(IQ80310_TIMER_LA2, (x >> 16) & 0x3f);
    137 }
    138 
    139 /*
    140  * iq80310_calibrate_delay:
    141  *
    142  *	Calibrate the delay loop.
    143  */
    144 void
    145 iq80310_calibrate_delay(void)
    146 {
    147 
    148 	/*
    149 	 * We'll use the CPLD timer for delay(), as well.  We go
    150 	 * ahead and start it up now, just don't enable interrupts
    151 	 * until cpu_initclocks().
    152 	 *
    153 	 * Just use hz=100 for now -- we'll adjust it, if necessary,
    154 	 * in cpu_initclocks().
    155 	 */
    156 	counts_per_hz = COUNTS_PER_SEC / 100;
    157 
    158 	timer_disable(TIMER_ENABLE_INTEN);
    159 	timer_disable(TIMER_ENABLE_EN);
    160 
    161 	timer_write(counts_per_hz);
    162 
    163 	timer_enable(TIMER_ENABLE_EN);
    164 }
    165 
    166 /*
    167  * cpu_initclocks:
    168  *
    169  *	Initialize the clock and get them going.
    170  */
    171 void
    172 cpu_initclocks(void)
    173 {
    174 	u_int oldirqstate;
    175 
    176 	if (hz < 50 || COUNTS_PER_SEC % hz) {
    177 		printf("Cannot get %d Hz clock; using 100 Hz\n", hz);
    178 		hz = 100;
    179 	}
    180 	tick = 1000000 / hz;	/* number of microseconds between interrupts */
    181 	tickfix = 1000000 - (hz * tick);
    182 	if (tickfix) {
    183 		int ftp;
    184 
    185 		ftp = min(ffs(tickfix), ffs(hz));
    186 		tickfix >>= (ftp - 1);
    187 		tickfixinterval = hz >> (ftp - 1);
    188 	}
    189 
    190 	/*
    191 	 * We only have one timer available; stathz and profhz are
    192 	 * always left as 0 (the upper-layer clock code deals with
    193 	 * this situation).
    194 	 */
    195 	if (stathz != 0)
    196 		printf("Cannot get %d Hz statclock\n", stathz);
    197 	stathz = 0;
    198 
    199 	if (profhz != 0)
    200 		printf("Cannot get %d Hz profclock\n", profhz);
    201 	profhz = 0;
    202 
    203 	/* Report the clock frequency. */
    204 	printf("clock: hz=%d stathz=%d profhz=%d\n", hz, stathz, profhz);
    205 
    206 	/* Hook up the clock interrupt handler. */
    207 	clock_ih = iq80310_intr_establish(XINT3_IRQ(XINT3_TIMER), IPL_CLOCK,
    208 	    clockhandler, NULL);
    209 	if (clock_ih == NULL)
    210 		panic("cpu_initclocks: unable to register timer interrupt");
    211 
    212 	/* Set up the new clock parameters. */
    213 	oldirqstate = disable_interrupts(I32_bit);
    214 
    215 	timer_disable(TIMER_ENABLE_EN);
    216 
    217 	counts_per_hz = COUNTS_PER_SEC / hz;
    218 	timer_write(counts_per_hz);
    219 
    220 	timer_enable(TIMER_ENABLE_INTEN);
    221 	timer_enable(TIMER_ENABLE_EN);
    222 
    223 	restore_interrupts(oldirqstate);
    224 }
    225 
    226 /*
    227  * setstatclockrate:
    228  *
    229  *	Set the rate of the statistics clock.
    230  *
    231  *	We assume that hz is either stathz or profhz, and that neither
    232  *	will change after being set by cpu_initclocks().  We could
    233  *	recalculate the intervals here, but that would be a pain.
    234  */
    235 void
    236 setstatclockrate(int hz)
    237 {
    238 
    239 	/*
    240 	 * Nothing to do, here; we can't change the statclock
    241 	 * rate on the IQ80310.
    242 	 */
    243 }
    244 
    245 /*
    246  * microtime:
    247  *
    248  *	Fill in the specified timeval struct with the current time
    249  *	accurate to the microsecond.
    250  */
    251 void
    252 microtime(struct timeval *tvp)
    253 {
    254 	static struct timeval lasttv;
    255 	u_int oldirqstate;
    256 	uint32_t counts;
    257 
    258 	oldirqstate = disable_interrupts(I32_bit);
    259 
    260 	counts = timer_read();
    261 
    262 	/* Fill in the timeval struct. */
    263 	*tvp = time;
    264 	tvp->tv_usec += (counts / COUNTS_PER_USEC);
    265 
    266 	/* Make sure microseconds doesn't overflow. */
    267 	while (tvp->tv_usec >= 1000000) {
    268 		tvp->tv_usec -= 1000000;
    269 		tvp->tv_sec++;
    270 	}
    271 
    272 	/* Make sure the time has advanced. */
    273 	if (tvp->tv_sec == lasttv.tv_sec &&
    274 	    tvp->tv_usec <= lasttv.tv_usec) {
    275 		tvp->tv_usec = lasttv.tv_usec + 1;
    276 		if (tvp->tv_usec >= 1000000) {
    277 			tvp->tv_usec -= 1000000;
    278 			tvp->tv_sec++;
    279 		}
    280 	}
    281 
    282 	lasttv = *tvp;
    283 
    284 	restore_interrupts(oldirqstate);
    285 }
    286 
    287 /*
    288  * delay:
    289  *
    290  *	Delay for at least N microseconds.
    291  */
    292 void
    293 delay(u_int n)
    294 {
    295 	uint32_t cur, last, delta, usecs;
    296 
    297 	/*
    298 	 * This works by polling the timer and counting the
    299 	 * number of microseconds that go by.
    300 	 */
    301 	last = timer_read();
    302 	delta = usecs = 0;
    303 
    304 	while (n > usecs) {
    305 		cur = timer_read();
    306 
    307 		/* Check to see if the timer has wrapped around. */
    308 		if (cur < last)
    309 			delta += ((counts_per_hz - last) + cur);
    310 		else
    311 			delta += (cur - last);
    312 
    313 		last = cur;
    314 
    315 		if (delta >= COUNTS_PER_USEC) {
    316 			usecs += delta / COUNTS_PER_USEC;
    317 			delta %= COUNTS_PER_USEC;
    318 		}
    319 	}
    320 }
    321 
    322 todr_chip_handle_t todr_handle;
    323 
    324 /*
    325  * todr_attach:
    326  *
    327  *	Set the specified time-of-day register as the system real-time clock.
    328  */
    329 void
    330 todr_attach(todr_chip_handle_t todr)
    331 {
    332 
    333 	if (todr_handle)
    334 		panic("todr_attach: rtc already configured");
    335 }
    336 
    337 /*
    338  * inittodr:
    339  *
    340  *	Initialize time from the time-of-day register.
    341  */
    342 #define	MINYEAR		2003	/* minimum plausible year */
    343 void
    344 inittodr(time_t base)
    345 {
    346 	time_t deltat;
    347 	int badbase;
    348 
    349 	if (base < (MINYEAR - 1970) * SECYR) {
    350 		printf("WARNING: preposterous time in file system");
    351 		/* read the system clock anyway */
    352 		base = (MINYEAR - 1970) * SECYR;
    353 		badbase = 1;
    354 	} else
    355 		badbase = 0;
    356 
    357 	if (todr_handle == NULL ||
    358 	    todr_gettime(todr_handle, (struct timeval *)&time) != 0 ||
    359 	    time.tv_sec == 0) {
    360 		/*
    361 		 * Believe the time in the file system for lack of
    362 		 * anything better, resetting the TODR.
    363 		 */
    364 		time.tv_sec = base;
    365 		time.tv_usec = 0;
    366 		if (todr_handle != NULL && !badbase) {
    367 			printf("WARNING: preposterous clock chip time\n");
    368 			resettodr();
    369 		}
    370 		goto bad;
    371 	}
    372 
    373 	if (!badbase) {
    374 		/*
    375 		 * See if we tained/lost two or more days; if
    376 		 * so, assume something is amiss.
    377 		 */
    378 		deltat = time.tv_sec - base;
    379 		if (deltat < 0)
    380 			deltat = -deltat;
    381 		if (deltat < 2 * SECDAY)
    382 			return;		/* all is well */
    383 		printf("WARNING: clock %s %ld days\n",
    384 		    time.tv_sec < base ? "lost" : "gained",
    385 		    (long)deltat / SECDAY);
    386 	}
    387  bad:
    388 	printf("WARNING: CHECK AND RESET THE DATE!\n");
    389 }
    390 
    391 /*
    392  * resettodr:
    393  *
    394  *	Reset the time-of-day register with the current time.
    395  */
    396 void
    397 resettodr(void)
    398 {
    399 
    400 	if (time.tv_sec == 0)
    401 		return;
    402 
    403 	if (todr_handle != NULL &&
    404 	    todr_settime(todr_handle, (struct timeval *)&time) != 0)
    405 		printf("resettodr: failed to set time\n");
    406 }
    407 
    408 /*
    409  * clockhandler:
    410  *
    411  *	Handle the hardclock interrupt.
    412  */
    413 int
    414 clockhandler(void *arg)
    415 {
    416 	struct clockframe *frame = arg;
    417 
    418 	timer_disable(TIMER_ENABLE_INTEN);
    419 	timer_enable(TIMER_ENABLE_INTEN);
    420 
    421 	hardclock(frame);
    422 
    423 	/*
    424 	 * Don't run the snake on IOP310-based systems that
    425 	 * don't have the 7-segment display.
    426 	 */
    427 #if !defined(IOP310_TEAMASA_NPWR)
    428 	{
    429 		static int snakefreq;
    430 
    431 		if ((snakefreq++ & 15) == 0)
    432 			iq80310_7seg_snake();
    433 	}
    434 #endif
    435 
    436 	return (1);
    437 }
    438