Home | History | Annotate | Line # | Download | only in xscale
i80321_timer.c revision 1.5
      1 /*	$NetBSD: i80321_timer.c,v 1.5 2003/07/15 00:24:54 lukem 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 i80321 I/O processor.
     40  */
     41 
     42 #include <sys/cdefs.h>
     43 __KERNEL_RCSID(0, "$NetBSD: i80321_timer.c,v 1.5 2003/07/15 00:24:54 lukem Exp $");
     44 
     45 #include "opt_perfctrs.h"
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/kernel.h>
     50 #include <sys/time.h>
     51 
     52 #include <machine/bus.h>
     53 #include <arm/cpufunc.h>
     54 
     55 #include <arm/xscale/i80321reg.h>
     56 #include <arm/xscale/i80321var.h>
     57 
     58 #include <arm/xscale/xscalevar.h>
     59 
     60 void	(*i80321_hardclock_hook)(void);
     61 
     62 #define	COUNTS_PER_SEC		200000000	/* 200MHz */
     63 #define	COUNTS_PER_USEC		(COUNTS_PER_SEC / 1000000)
     64 
     65 static void *clock_ih;
     66 
     67 static uint32_t counts_per_hz;
     68 
     69 int	clockhandler(void *);
     70 
     71 static __inline uint32_t
     72 tmr0_read(void)
     73 {
     74 	uint32_t rv;
     75 
     76 	__asm __volatile("mrc p6, 0, %0, c0, c1, 0"
     77 		: "=r" (rv));
     78 	return (rv);
     79 }
     80 
     81 static __inline void
     82 tmr0_write(uint32_t val)
     83 {
     84 
     85 	__asm __volatile("mcr p6, 0, %0, c0, c1, 0"
     86 		:
     87 		: "r" (val));
     88 }
     89 
     90 static __inline uint32_t
     91 tcr0_read(void)
     92 {
     93 	uint32_t rv;
     94 
     95 	__asm __volatile("mrc p6, 0, %0, c2, c1, 0"
     96 		: "=r" (rv));
     97 	return (rv);
     98 }
     99 
    100 static __inline void
    101 tcr0_write(uint32_t val)
    102 {
    103 
    104 	__asm __volatile("mcr p6, 0, %0, c2, c1, 0"
    105 		:
    106 		: "r" (val));
    107 }
    108 
    109 static __inline void
    110 trr0_write(uint32_t val)
    111 {
    112 
    113 	__asm __volatile("mcr p6, 0, %0, c4, c1, 0"
    114 		:
    115 		: "r" (val));
    116 }
    117 
    118 static __inline void
    119 tisr_write(uint32_t val)
    120 {
    121 
    122 	__asm __volatile("mcr p6, 0, %0, c6, c1, 0"
    123 		:
    124 		: "r" (val));
    125 }
    126 
    127 /*
    128  * i80321_calibrate_delay:
    129  *
    130  *	Calibrate the delay loop.
    131  */
    132 void
    133 i80321_calibrate_delay(void)
    134 {
    135 
    136 	/*
    137 	 * Just use hz=100 for now -- we'll adjust it, if necessary,
    138 	 * in cpu_initclocks().
    139 	 */
    140 	counts_per_hz = COUNTS_PER_SEC / 100;
    141 
    142 	tmr0_write(0);			/* stop timer */
    143 	tisr_write(TISR_TMR0);		/* clear interrupt */
    144 	trr0_write(counts_per_hz);	/* reload value */
    145 	tcr0_write(counts_per_hz);	/* current value */
    146 
    147 	tmr0_write(TMRx_ENABLE|TMRx_RELOAD|TMRx_CSEL_CORE);
    148 }
    149 
    150 /*
    151  * cpu_initclocks:
    152  *
    153  *	Initialize the clock and get them going.
    154  */
    155 void
    156 cpu_initclocks(void)
    157 {
    158 	u_int oldirqstate;
    159 #if defined(PERFCTRS)
    160 	void *pmu_ih;
    161 #endif
    162 
    163 	if (hz < 50 || COUNTS_PER_SEC % hz) {
    164 		aprint_error("Cannot get %d Hz clock; using 100 Hz\n", hz);
    165 		hz = 100;
    166 	}
    167 	tick = 1000000 / hz;	/* number of microseconds between interrupts */
    168 	tickfix = 1000000 - (hz * tick);
    169 	if (tickfix) {
    170 		int ftp;
    171 
    172 		ftp = min(ffs(tickfix), ffs(hz));
    173 		tickfix >>= (ftp - 1);
    174 		tickfixinterval = hz >> (ftp - 1);
    175 	}
    176 
    177 	/*
    178 	 * We only have one timer available; stathz and profhz are
    179 	 * always left as 0 (the upper-layer clock code deals with
    180 	 * this situation).
    181 	 */
    182 	if (stathz != 0)
    183 		aprint_error("Cannot get %d Hz statclock\n", stathz);
    184 	stathz = 0;
    185 
    186 	if (profhz != 0)
    187 		aprint_error("Cannot get %d Hz profclock\n", profhz);
    188 	profhz = 0;
    189 
    190 	/* Report the clock frequency. */
    191 	aprint_normal("clock: hz=%d stathz=%d profhz=%d\n", hz, stathz, profhz);
    192 
    193 	oldirqstate = disable_interrupts(I32_bit);
    194 
    195 	/* Hook up the clock interrupt handler. */
    196 	clock_ih = i80321_intr_establish(ICU_INT_TMR0, IPL_CLOCK,
    197 	    clockhandler, NULL);
    198 	if (clock_ih == NULL)
    199 		panic("cpu_initclocks: unable to register timer interrupt");
    200 
    201 #if defined(PERFCTRS)
    202 	pmu_ih = i80321_intr_establish(ICU_INT_PMU, IPL_STATCLOCK,
    203 	    xscale_pmc_dispatch, NULL);
    204 	if (pmu_ih == NULL)
    205 		panic("cpu_initclocks: unable to register timer interrupt");
    206 #endif
    207 
    208 	/* Set up the new clock parameters. */
    209 
    210 	tmr0_write(0);			/* stop timer */
    211 	tisr_write(TISR_TMR0);		/* clear interrupt */
    212 
    213 	counts_per_hz = COUNTS_PER_SEC / hz;
    214 
    215 	trr0_write(counts_per_hz);	/* reload value */
    216 	tcr0_write(counts_per_hz);	/* current value */
    217 
    218 	tmr0_write(TMRx_ENABLE|TMRx_RELOAD|TMRx_CSEL_CORE);
    219 
    220 	restore_interrupts(oldirqstate);
    221 }
    222 
    223 /*
    224  * setstatclockrate:
    225  *
    226  *	Set the rate of the statistics clock.
    227  *
    228  *	We assume that hz is either stathz or profhz, and that neither
    229  *	will change after being set by cpu_initclocks().  We could
    230  *	recalculate the intervals here, but that would be a pain.
    231  */
    232 void
    233 setstatclockrate(int hz)
    234 {
    235 
    236 	/*
    237 	 * XXX Use TMR1?
    238 	 */
    239 }
    240 
    241 /*
    242  * microtime:
    243  *
    244  *	Fill in the specified timeval struct with the current time
    245  *	accurate to the microsecond.
    246  */
    247 void
    248 microtime(struct timeval *tvp)
    249 {
    250 	static struct timeval lasttv;
    251 	u_int oldirqstate;
    252 	uint32_t counts;
    253 
    254 	oldirqstate = disable_interrupts(I32_bit);
    255 
    256 	counts = counts_per_hz - tcr0_read();
    257 
    258 	/* Fill in the timeval struct. */
    259 	*tvp = time;
    260 	tvp->tv_usec += (counts / COUNTS_PER_USEC);
    261 
    262 	/* Make sure microseconds doesn't overflow. */
    263 	while (tvp->tv_usec >= 1000000) {
    264 		tvp->tv_usec -= 1000000;
    265 		tvp->tv_sec++;
    266 	}
    267 
    268 	/* Make sure the time has advanced. */
    269 	if (tvp->tv_sec == lasttv.tv_sec &&
    270 	    tvp->tv_usec <= lasttv.tv_usec) {
    271 		tvp->tv_usec = lasttv.tv_usec + 1;
    272 		if (tvp->tv_usec >= 1000000) {
    273 			tvp->tv_usec -= 1000000;
    274 			tvp->tv_sec++;
    275 		}
    276 	}
    277 
    278 	lasttv = *tvp;
    279 
    280 	restore_interrupts(oldirqstate);
    281 }
    282 
    283 /*
    284  * delay:
    285  *
    286  *	Delay for at least N microseconds.
    287  */
    288 void
    289 delay(u_int n)
    290 {
    291 	uint32_t cur, last, delta, usecs;
    292 
    293 	/*
    294 	 * This works by polling the timer and counting the
    295 	 * number of microseconds that go by.
    296 	 */
    297 	last = tcr0_read();
    298 	delta = usecs = 0;
    299 
    300 	while (n > usecs) {
    301 		cur = tcr0_read();
    302 
    303 		/* Check to see if the timer has wrapped around. */
    304 		if (last < cur)
    305 			delta += (last + (counts_per_hz - cur));
    306 		else
    307 			delta += (last - cur);
    308 
    309 		last = cur;
    310 
    311 		if (delta >= COUNTS_PER_USEC) {
    312 			usecs += delta / COUNTS_PER_USEC;
    313 			delta %= COUNTS_PER_USEC;
    314 		}
    315 	}
    316 }
    317 
    318 /*
    319  * inittodr:
    320  *
    321  *	Initialize time from the time-of-day register.
    322  */
    323 void
    324 inittodr(time_t base)
    325 {
    326 
    327 	time.tv_sec = base;
    328 	time.tv_usec = 0;
    329 }
    330 
    331 /*
    332  * resettodr:
    333  *
    334  *	Reset the time-of-day register with the current time.
    335  */
    336 void
    337 resettodr(void)
    338 {
    339 }
    340 
    341 /*
    342  * clockhandler:
    343  *
    344  *	Handle the hardclock interrupt.
    345  */
    346 int
    347 clockhandler(void *arg)
    348 {
    349 	struct clockframe *frame = arg;
    350 
    351 	tisr_write(TISR_TMR0);
    352 
    353 	hardclock(frame);
    354 
    355 	if (i80321_hardclock_hook != NULL)
    356 		(*i80321_hardclock_hook)();
    357 
    358 	return (1);
    359 }
    360