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

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

/* 
 * NEC5074L/sbdclock.c: timer handling for NEC DDB-Vrc5074 board
 */


/*
 * Sadly the R5000 on-chip timer interrupt is disabled by default,
 * so we have to use the timer on the Nile4.  This is not nice,
 * because every time we reprogram the timer we lose time.
 */


#ifdef NEC5074_CPUTIMER

#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 "vrc5074.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 vrc5074 * const n4 = PA_TO_KVA1 (VRC5074_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) */
	n4->n4_t2ctrl = 0;
	n4->n4_t2cntr = cycpertick;
	n4->n4_t2ctrl = N4_TnCTRL_EN | cycpertick;
    }
}


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

    if (n4->n4_intstat1 & N4_INTSTAT1_IL4 (N4_INT_GPT)) {

	/* acknowledge gpt interrupt */
	n4->n4_intclr = N4_INTCLR_DEV (N4_INT_GPT);

	/* 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 same 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 = n4->n4_t2cntr;

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

    /* check for possible missed interrupt */
    if (cycles < cycpertick / 2 
	&& (n4->n4_intstat1 & N4_INTSTAT1_IL4 (N4_INT_GPT)))
      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;

	/* for some reason NEC's PMON enables h/w int 5 by
	   default - switch this off to avoid spurious i/us */
	{
	    extern unsigned int _sr_base __attribute__((common));
	    _sr_base &= ~SR_HINT5;
	}
    }

    /* 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) */
    n4->n4_t2ctrl = 0;

    /* clear gpt interrupt */
    n4->n4_intclr = N4_INTCLR_DEV (N4_INT_GPT);

    /* connect gpt interrupt to CPU HINT4 */
    n4->n4_intctrl &= ~N4_INTCTRL_MASK (N4_INT_GPT);
    n4->n4_intctrl |= (N4_INTCTRL_EN (N4_INT_GPT) 
		       | N4_INTCTRL_PRI (N4_INT_GPT, 4));

    /* enable HINT4 output driver */
    n4->n4_intstat1 |= N4_INTSTAT1_OE (4);

    /* install interrupt handler on HINT4 = Int6 */
    ia.ia_handler = clkintr;
    ia.ia_arg = 0;
    ia.ia_ipl = 8;
    intraction (6, &ia, &oldiact);

    return maxtick;
}

#endif /* !NEC5074_CPUTIMER */
