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