#ident "kit/IDT364B/sbdfreq.c: Copyright (c) MIPS Technologies, Inc. All rights reserved."

/*
 * Copyright (c) 1998-2003 MIPS Technologies, Inc.
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 *      * Redistributions of source code must retain the above copyright
 *        notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above
 *      copyright
 *        notice, this list of conditions and the following disclaimer
 *        in the documentation and/or other materials provided with
 *        the distribution.
 *      * Neither the name of MIPS Technologies Inc. nor the names of its
 *        contributors may be used to endorse or promote products derived
 *        from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* 
 * IDT364/sbdfreq.c: CPU frequency determination for IDT364 board
 */


#include <stddef.h>
#include <time.h>
#include <errno.h>
#include <mips/cpu.h>

#include "sbd.h"
#include "sonic.h"

#include "kit_impl.h"

#define SONIC_FREQ	5000000		/* clock is 5 MHz (half ethernet) */
#define	TEST_PERIOD	500		/* 500us test period */


/*
 * Dynamically determine CPU clock frequency by running the R4000
 * on-chip timer against the Sonic timer (a known frequency).
 */
long
_sbd_pipefreq (void)
{
    volatile struct sonic_reg *snr = PA_TO_KVA1 (SONIC_BASE);
    static long pipefreq = 0;
    unsigned long start, cnt;

    if (pipefreq > 0)
	return pipefreq;

    /* Wait for sonic timer to reach TEST_PERIOD usecs and
     * and then examine CPU cycle count to determine clock freq. */
    cnt = TEST_PERIOD * (SONIC_FREQ / 1000000);
    snr->s_wt1 = 0; snr->s_wt0 = cnt; mips_wbflush();
    snr->s_isr = ISR_TC; mips_wbflush();

    snr->s_cr = CR_ST; mips_wbflush();
    start = mips_getcount ();
    do {
	cnt = mips_getcount ();
    } while (!(snr->s_isr & ISR_TC));

    cnt -= start;
    snr->s_cr = CR_STP; mips_wbflush();
    snr->s_isr = ISR_TC; mips_wbflush();

    /* work out number of cpu sysclk cycles per usec (round to nearest) */
    cnt = (cnt + TEST_PERIOD / 2) / TEST_PERIOD;

    /* convert to cycles per sec */
    pipefreq = cnt * 1000000;

    /* pipeline runs at twice counter frequency */
    pipefreq *= 2;

    return pipefreq;
}


long
_sbd_cpufreq (void)
{
    unsigned int ratio = (mips_getconfig() & CFG_ECMASK) >> CFG_ECSHIFT;
    long freq = _sbd_pipefreq ();
    freq /= (ratio + 2); 
    return freq;
}
