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

/*
 * Copyright (c) 2000-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.
 */

/* 
 * IDT134/sbdclock.c: timer handling for IDT 79S124 board
 */


/*
 * Note: Only the code to handle the RC32364 CPU timer is currently
 * written - if the DIP Switch S4/7 is chenged to disable timer i/us
 * then this code will fail. 
 */


#ifndef IDT134_NOCPUTIMER

#include "../share/r4kclock.c"

#else 

#include <stddef.h>
#include <sys/time.h>
#include <sys/clock.h>
#include <mips/cpu.h>
#include <mips/xcpt.h>
#include <mips/limits.h>
#include <assert.h>
#include "kit_impl.h"

#include "sbd.h"
#include "rc32134.h"

/* The interrupt function to call, and the interval (in microseconds) */
static clock_t		(*tickfunc)();
static clock_t		tick;

static clock_t		maxtick;
static unsigned long	cycpertick;
static unsigned long	cycperusec;

static struct intraction oldiact;

clock_t			_clk_tck;

static volatile struct rc32134 * const rc = PA_TO_KVA1 (RC32134_BASE);

void
_sbd_settimer (clock_t newtick)
{
    assert (newtick <= maxtick);
    if (newtick != tick) {
	/* Program the timer to interrupt every NEWTICK microseconds, */
	cycpertick = (tick = newtick) * cycperusec - 1;
	/* restart clock (XXX accuracy is lost) */
	rc->rc_t2ctrl = 0;
	rc->rc_t2cntr = cycpertick;
	rc->rc_t2ctrl = RC_TnCTRL_EN | cycpertick;
    }
}


static int 
clkintr (int arg, struct xcptcontext *xcp)
{
    clock_t newtick;

    if (intpending) {

	/* acknowledge and clear timer interrupt */
	/* XXX */

	/* Call the generic timer code's tick function, passing
	   the number of interrupt times that have elapsed since
	   the last call (usually 1). */
	newtick = (*tickfunc) (1, xcp);
	
	/* You may be asked to vary the interrupt rate now. */
	if (newtick != tick)
	    _sbd_settimer (newtick);
    }

    /* other devices interrupts may be routed to this interrupt level */
    if (oldiact.ia_handler != XCPT_DFL)
	return (*oldiact.ia_handler) (oldiact.ia_arg, xcp);

    return 0;
}


clock_t
_sbd_gettimer ()
{
    /* Return how many microseconds have passed since last interrupt,
       as accurately as possible. */
    unsigned long cycles;

    /* get timer */
    cycles = xxx;

    /* convert downcount to upcount */
    cycles = (cycpertick - cycles);

    /* check for possible missed interrupt */
    if (cycles < cycpertick / 2 
	&& (xxx intpending))
      cycles += cycpertick;

    return cycles / cycperusec;
}


clock_t
_sbd_inittimer (clock_t (*func)())
{
    struct intraction ia;

    /* Initialise the timer, and arrange for it to call FUNC on
       every interrupt.  Install the interrupt handler and
       enable the interrupt. */
    
    if (!_clk_tck) {
	/* get CPU sys clock frequency */
	_clk_tck = _sbd_cpufreq ();
	if (_clk_tck < 1000000)
	    return 0;
    }

    /* work out timing parameters based on that frequency */
    cycperusec = _clk_tck / 1000000;
    maxtick = LONG_MAX / cycperusec;
    tickfunc = func;

    /* stop general purpose timer #2 (started in settimer) */
    /* XXX */

    /* clear timer interrupt */
    /* XXX */

    /* connect timer interrupt */
    /* XXX */

    /* install rc32134 interrupt handler */
    ia.ia_handler = clkintr;
    ia.ia_arg = 0;
    ia.ia_ipl = 8;
    intraction (5, &ia, &oldiact);

    return maxtick;
}

#endif /* IDT134_NOCPUTIMER */
