Home | History | Annotate | Line # | Download | only in iq80310
iq80310_timer.c revision 1.4
      1 /*	$NetBSD: iq80310_timer.c,v 1.4 2001/11/23 19:36:49 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001 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/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/kernel.h>
     52 #include <sys/time.h>
     53 
     54 #include <machine/bus.h>
     55 #include <arm/cpufunc.h>
     56 
     57 #include <evbarm/iq80310/iq80310reg.h>
     58 #include <evbarm/iq80310/iq80310var.h>
     59 #include <evbarm/iq80310/obiovar.h>
     60 
     61 #define	COUNTS_PER_SEC		33000000	/* 33MHz */
     62 #define	COUNTS_PER_USEC		(COUNTS_PER_SEC / 1000000)
     63 
     64 static void *clock_ih;
     65 
     66 static uint32_t counts_per_hz;
     67 
     68 int	clockhandler(void *);
     69 
     70 static __inline void
     71 timer_enable(uint8_t bit)
     72 {
     73 
     74 	CPLD_WRITE(IQ80310_TIMER_ENABLE,
     75 	    CPLD_READ(IQ80310_TIMER_ENABLE) | bit);
     76 }
     77 
     78 static __inline void
     79 timer_disable(uint8_t bit)
     80 {
     81 
     82 	CPLD_WRITE(IQ80310_TIMER_ENABLE,
     83 	    CPLD_READ(IQ80310_TIMER_ENABLE) & ~bit);
     84 }
     85 
     86 static __inline uint32_t
     87 timer_read(void)
     88 {
     89 	uint32_t rv;
     90 	uint8_t la[4];
     91 
     92 	/*
     93 	 * First read latches count.
     94 	 *
     95 	 * From RedBoot: harware bug that causes invalid counts to be
     96 	 * latched.  The loop appears to work around the problem.
     97 	 */
     98 	do {
     99 		la[0] = CPLD_READ(IQ80310_TIMER_LA0) & 0x5f;
    100 	} while (la[0] == 0);
    101 	la[1] = CPLD_READ(IQ80310_TIMER_LA1) & 0x5f;
    102 	la[2] = CPLD_READ(IQ80310_TIMER_LA2) & 0x5f;
    103 	la[3] = CPLD_READ(IQ80310_TIMER_LA3) & 0x0f;
    104 
    105 	rv  =  ((la[0] & 0x40) >> 1) | (la[0] & 0x1f);
    106 	rv |= (((la[1] & 0x40) >> 1) | (la[1] & 0x1f)) << 6;
    107 	rv |= (((la[2] & 0x40) >> 1) | (la[2] & 0x1f)) << 12;
    108 	rv |= la[3] << 18;
    109 
    110 	return (rv);
    111 }
    112 
    113 static __inline void
    114 timer_write(uint32_t x)
    115 {
    116 
    117 	CPLD_WRITE(IQ80310_TIMER_LA0, x & 0xff);
    118 	CPLD_WRITE(IQ80310_TIMER_LA1, (x >> 8) & 0xff);
    119 	CPLD_WRITE(IQ80310_TIMER_LA2, (x >> 16) & 0x3f);
    120 }
    121 
    122 /*
    123  * iq80310_calibrate_delay:
    124  *
    125  *	Calibrate the delay loop.
    126  */
    127 void
    128 iq80310_calibrate_delay(void)
    129 {
    130 
    131 	/*
    132 	 * We'll use the CPLD timer for delay(), as well.  We go
    133 	 * ahead and start it up now, just don't enable interrupts
    134 	 * until cpu_initclocks().
    135 	 *
    136 	 * Just use hz=100 for now -- we'll adjust it, if necessary,
    137 	 * in cpu_initclocks().
    138 	 */
    139 	counts_per_hz = COUNTS_PER_SEC / 100;
    140 
    141 	timer_disable(TIMER_ENABLE_INTEN);
    142 	timer_disable(TIMER_ENABLE_EN);
    143 
    144 	timer_write(counts_per_hz);
    145 
    146 	timer_enable(TIMER_ENABLE_EN);
    147 }
    148 
    149 /*
    150  * cpu_initclocks:
    151  *
    152  *	Initialize the clock and get them going.
    153  */
    154 void
    155 cpu_initclocks(void)
    156 {
    157 	u_int oldirqstate;
    158 
    159 	if (hz < 50 || COUNTS_PER_SEC % hz) {
    160 		printf("Cannot get %d Hz clock; using 100 Hz\n", hz);
    161 		hz = 100;
    162 		tick = 1000000 / hz;
    163 	}
    164 
    165 	/*
    166 	 * We only have one timer available; stathz and profhz are
    167 	 * always equal to hz.
    168 	 */
    169 	if (stathz != 0)
    170 		printf("Cannot get %d Hz statclock; using %d Hz\n",
    171 		    stathz, hz);
    172 	stathz = hz;
    173 
    174 	if (profhz != 0)
    175 		printf("Cannot get %d Hz profclock; using %d Hz\n",
    176 		    profhz, hz);
    177 	profhz = hz;
    178 
    179 	/* Report the clock frequency. */
    180 	printf("clock: hz=%d stathz=%d profhz=%d\n", hz, stathz, profhz);
    181 
    182 	/* Hook up the clock interrupt handler. */
    183 	clock_ih = iq80310_intr_establish(XINT3_IRQ(XINT3_TIMER), IPL_CLOCK,
    184 	    clockhandler, NULL);
    185 	if (clock_ih == NULL)
    186 		panic("cpu_initclocks: unable to register timer interrupt");
    187 
    188 	/* Set up the new clock parameters. */
    189 	oldirqstate = disable_interrupts(I32_bit);
    190 
    191 	timer_disable(TIMER_ENABLE_EN);
    192 
    193 	counts_per_hz = COUNTS_PER_SEC / hz;
    194 	timer_write(counts_per_hz);
    195 
    196 	timer_enable(TIMER_ENABLE_INTEN);
    197 	timer_enable(TIMER_ENABLE_EN);
    198 
    199 	restore_interrupts(oldirqstate);
    200 }
    201 
    202 /*
    203  * setstatclockrate:
    204  *
    205  *	Set the rate of the statistics clock.
    206  *
    207  *	We assume that hz is either stathz or profhz, and that neither
    208  *	will change after being set by cpu_initclocks().  We could
    209  *	recalculate the intervals here, but that would be a pain.
    210  */
    211 void
    212 setstatclockrate(int hz)
    213 {
    214 
    215 	/*
    216 	 * Nothing to do, here; we can't change the statclock
    217 	 * rate on the IQ80310.
    218 	 */
    219 }
    220 
    221 /*
    222  * microtime:
    223  *
    224  *	Fill in the specified timeval struct with the current time
    225  *	accurate to the microsecond.
    226  */
    227 void
    228 microtime(struct timeval *tvp)
    229 {
    230 	static struct timeval lasttv;
    231 	u_int oldirqstate;
    232 	uint32_t counts;
    233 
    234 	oldirqstate = disable_interrupts(I32_bit);
    235 
    236 	counts = timer_read();
    237 
    238 	/* Fill in the timeval struct. */
    239 	*tvp = time;
    240 	tvp->tv_usec += (counts / COUNTS_PER_USEC);
    241 
    242 	/* Make sure microseconds doesn't overflow. */
    243 	while (tvp->tv_usec >= 1000000) {
    244 		tvp->tv_usec -= 1000000;
    245 		tvp->tv_sec++;
    246 	}
    247 
    248 	/* Make sure the time has advanced. */
    249 	if (tvp->tv_sec == lasttv.tv_sec &&
    250 	    tvp->tv_usec <= lasttv.tv_usec) {
    251 		tvp->tv_usec = lasttv.tv_usec + 1;
    252 		if (tvp->tv_usec >= 1000000) {
    253 			tvp->tv_usec -= 1000000;
    254 			tvp->tv_sec++;
    255 		}
    256 	}
    257 
    258 	lasttv = *tvp;
    259 
    260 	restore_interrupts(oldirqstate);
    261 }
    262 
    263 /*
    264  * delay:
    265  *
    266  *	Delay for at least N microseconds.
    267  */
    268 void
    269 delay(u_int n)
    270 {
    271 	uint32_t cur, last, delta, usecs;
    272 
    273 	/*
    274 	 * This works by polling the timer and counting the
    275 	 * number of microseconds that go by.
    276 	 */
    277 	last = timer_read();
    278 	delta = usecs = 0;
    279 
    280 	while (n > usecs) {
    281 		cur = timer_read();
    282 
    283 		/* Check to see if the timer has wrapped around. */
    284 		if (cur < last)
    285 			delta += ((counts_per_hz - last) + cur);
    286 		else
    287 			delta += (cur - last);
    288 
    289 		last = cur;
    290 
    291 		if (delta >= COUNTS_PER_USEC) {
    292 			usecs += delta / COUNTS_PER_USEC;
    293 			delta %= COUNTS_PER_USEC;
    294 		}
    295 	}
    296 }
    297 
    298 /*
    299  * inittodr:
    300  *
    301  *	Initialize time from the time-of-day register.
    302  */
    303 void
    304 inittodr(time_t base)
    305 {
    306 }
    307 
    308 /*
    309  * resettodr:
    310  *
    311  *	Reset the time-of-day register with the current time.
    312  */
    313 void
    314 resettodr(void)
    315 {
    316 }
    317 
    318 /*
    319  * clockhandler:
    320  *
    321  *	Handle the hardclock interrupt.
    322  */
    323 int
    324 clockhandler(void *arg)
    325 {
    326 	struct clockframe *frame = arg;
    327 
    328 	timer_disable(TIMER_ENABLE_INTEN);
    329 	timer_enable(TIMER_ENABLE_INTEN);
    330 
    331 	hardclock(frame);
    332 
    333 	return (1);
    334 }
    335