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