Home | History | Annotate | Line # | Download | only in alchemy
au_timer.c revision 1.1
      1  1.1  simonb /* $NetBSD: au_timer.c,v 1.1 2002/07/29 15:39:12 simonb Exp $ */
      2  1.1  simonb 
      3  1.1  simonb /*
      4  1.1  simonb  * Copyright 2002 Wasabi Systems, Inc.
      5  1.1  simonb  * All rights reserved.
      6  1.1  simonb  *
      7  1.1  simonb  * Written by Simon Burge for Wasabi Systems, Inc.
      8  1.1  simonb  *
      9  1.1  simonb  * Redistribution and use in source and binary forms, with or without
     10  1.1  simonb  * modification, are permitted provided that the following conditions
     11  1.1  simonb  * are met:
     12  1.1  simonb  * 1. Redistributions of source code must retain the above copyright
     13  1.1  simonb  *    notice, this list of conditions and the following disclaimer.
     14  1.1  simonb  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  simonb  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  simonb  *    documentation and/or other materials provided with the distribution.
     17  1.1  simonb  * 3. All advertising materials mentioning features or use of this software
     18  1.1  simonb  *    must display the following acknowledgement:
     19  1.1  simonb  *      This product includes software developed for the NetBSD Project by
     20  1.1  simonb  *      Wasabi Systems, Inc.
     21  1.1  simonb  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  1.1  simonb  *    or promote products derived from this software without specific prior
     23  1.1  simonb  *    written permission.
     24  1.1  simonb  *
     25  1.1  simonb  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  1.1  simonb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  1.1  simonb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  1.1  simonb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  1.1  simonb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  1.1  simonb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  1.1  simonb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  1.1  simonb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  1.1  simonb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  1.1  simonb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  1.1  simonb  * POSSIBILITY OF SUCH DAMAGE.
     36  1.1  simonb  */
     37  1.1  simonb 
     38  1.1  simonb #include <sys/param.h>
     39  1.1  simonb #include <sys/kernel.h>
     40  1.1  simonb 
     41  1.1  simonb #include <machine/bus.h>
     42  1.1  simonb #include <mips/locore.h>
     43  1.1  simonb 
     44  1.1  simonb #include <evbmips/evbmips/clockvar.h>
     45  1.1  simonb #include <mips/alchemy/include/aureg.h>
     46  1.1  simonb #include <mips/alchemy/include/auvar.h>
     47  1.1  simonb 
     48  1.1  simonb /*
     49  1.1  simonb  * Set a programmable clock register.
     50  1.1  simonb  * If "wait" is non-zero, wait for that bit to become 0 in the
     51  1.1  simonb  * counter control register before and after writing to the
     52  1.1  simonb  * specified clock register.
     53  1.1  simonb  */
     54  1.1  simonb #define	SET_PC_REG(reg, wait, val)					\
     55  1.1  simonb do {									\
     56  1.1  simonb 	if (wait)							\
     57  1.1  simonb 		while (bus_space_read_4(st, sh, PC_COUNTER_CONTROL)	\
     58  1.1  simonb 		    & (wait))						\
     59  1.1  simonb 			/* nothing */;					\
     60  1.1  simonb 	bus_space_write_4(st, sh, (reg), (val));			\
     61  1.1  simonb 	if (wait)							\
     62  1.1  simonb 		while (bus_space_read_4(st, sh, (reg)) & (wait))	\
     63  1.1  simonb 			/* nothing */;					\
     64  1.1  simonb } while (0)
     65  1.1  simonb 
     66  1.1  simonb void
     67  1.1  simonb au_cal_timers(bus_space_tag_t st, bus_space_handle_t sh)
     68  1.1  simonb {
     69  1.1  simonb 	uint32_t ctrdiff[4], startctr, endctr;
     70  1.1  simonb 	uint32_t ctl, ctr, octr;
     71  1.1  simonb 	int i;
     72  1.1  simonb 
     73  1.1  simonb 	/* Enable the programmable counter 1. */
     74  1.1  simonb 	ctl = bus_space_read_4(st, sh, PC_COUNTER_CONTROL);
     75  1.1  simonb 	if ((ctl & (CC_EO | CC_EN1)) != (CC_EO | CC_EN1));
     76  1.1  simonb 		SET_PC_REG(PC_COUNTER_CONTROL, 0, ctl | CC_EO | CC_EN1);
     77  1.1  simonb 
     78  1.1  simonb 	/* Initialize for 16Hz. */
     79  1.1  simonb 	SET_PC_REG(PC_TRIM1, CC_T1S, PC_RATE / 16 - 1);
     80  1.1  simonb 
     81  1.1  simonb 	/* Run the loop an extra time to prime the cache. */
     82  1.1  simonb 	for (i = 0; i < 4; i++) {
     83  1.1  simonb 		/* Reset the counter. */
     84  1.1  simonb 		SET_PC_REG(PC_COUNTER_WRITE1, CC_C1S, 0);
     85  1.1  simonb 
     86  1.1  simonb 		/* Wait for 1/16th of a second. */
     87  1.1  simonb 		//startctr = mips3_cp0_count_read();
     88  1.1  simonb 
     89  1.1  simonb 		/* Wait for the PC to tick over. */
     90  1.1  simonb 		ctr = bus_space_read_4(st, sh, PC_COUNTER_READ_1);
     91  1.1  simonb 		do {
     92  1.1  simonb 			octr = bus_space_read_4(st, sh, PC_COUNTER_READ_1);
     93  1.1  simonb 		} while (ctr == octr);
     94  1.1  simonb 
     95  1.1  simonb 		startctr = mips3_cp0_count_read();
     96  1.1  simonb 		do {
     97  1.1  simonb 			ctr = bus_space_read_4(st, sh, PC_COUNTER_READ_1);
     98  1.1  simonb 		} while (ctr == octr);	// while (ctr <= octr + 1);
     99  1.1  simonb 		endctr = mips3_cp0_count_read();
    100  1.1  simonb 		ctrdiff[i] = endctr - startctr;
    101  1.1  simonb 	}
    102  1.1  simonb 
    103  1.1  simonb 	/* Disable the counter (if it wasn't enabled already). */
    104  1.1  simonb 	if ((ctl & (CC_EO | CC_EN1)) != (CC_EO | CC_EN1));
    105  1.1  simonb 		SET_PC_REG(PC_COUNTER_CONTROL, 0, ctl);
    106  1.1  simonb 
    107  1.1  simonb 	/* Compute the number of cycles per second. */
    108  1.1  simonb 	curcpu()->ci_cpu_freq = ((ctrdiff[2] + ctrdiff[3]) / 2) * 16;
    109  1.1  simonb 
    110  1.1  simonb 	/* Compute the number of ticks for hz. */
    111  1.1  simonb 	curcpu()->ci_cycles_per_hz = (curcpu()->ci_cpu_freq + hz / 2) / hz;
    112  1.1  simonb 
    113  1.1  simonb 	/* Compute the delay divisor. */
    114  1.1  simonb 	curcpu()->ci_divisor_delay =
    115  1.1  simonb 	    ((curcpu()->ci_cpu_freq + 500000) / 1000000);
    116  1.1  simonb 
    117  1.1  simonb 	/*
    118  1.1  simonb 	 * To implement a more accurate microtime using the CP0 COUNT
    119  1.1  simonb 	 * register we need to divide that register by the number of
    120  1.1  simonb 	 * cycles per MHz.  But...
    121  1.1  simonb 	 *
    122  1.1  simonb 	 * DIV and DIVU are expensive on MIPS (eg 75 clocks on the
    123  1.1  simonb 	 * R4000).  MULT and MULTU are only 12 clocks on the same CPU.
    124  1.1  simonb 	 * On the SB1 these appear to be 40-72 clocks for DIV/DIVU and 3
    125  1.1  simonb 	 * clocks for MUL/MULTU.
    126  1.1  simonb 	 *
    127  1.1  simonb 	 * The strategy we use to to calculate the reciprical of cycles
    128  1.1  simonb 	 * per MHz, scaled by 1<<32.  Then we can simply issue a MULTU
    129  1.1  simonb 	 * and pluck of the HI register and have the results of the
    130  1.1  simonb 	 * division.
    131  1.1  simonb 	 */
    132  1.1  simonb 	curcpu()->ci_divisor_recip =
    133  1.1  simonb 	    0x100000000ULL / curcpu()->ci_divisor_delay;
    134  1.1  simonb 
    135  1.1  simonb 	/*
    136  1.1  simonb 	 * Get correct cpu frequency if the CPU runs at twice the
    137  1.1  simonb 	 * external/cp0-count frequency.
    138  1.1  simonb 	 */
    139  1.1  simonb 	if (mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
    140  1.1  simonb 		curcpu()->ci_cpu_freq *= 2;
    141  1.1  simonb 
    142  1.1  simonb #ifdef DEBUG
    143  1.1  simonb 	printf("Timer calibration: %lu cycles/sec [(%u, %u) * 16]\n",
    144  1.1  simonb 	    curcpu()->ci_cpu_freq, ctrdiff[2], ctrdiff[3]);
    145  1.1  simonb #endif
    146  1.1  simonb }
    147