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

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

/* 
 * IDT385/sbdclock.c: timer handling for IDT 79RS385 eval board
 */



#include <stddef.h>
#include <sys/time.h>
#include <sys/clock.h>
#include <mips/cpu.h>
#include <mips/xcpt.h>
#include <idt/r3041.h>
#include <assert.h>

#include "kit_impl.h"
#include "sbd.h"
#include "i8254.h"

/* 
 * The timer crystal frequency PT_CRYSTAL is 3.6864Mhz, 
 * this we prescale to give a counter frequency of 1600 Hz.  
 * This is a common factor of the PT_CRYSTAL and 1000000
 * (1 second of usecs), making the calculations easier.  
 * The 16-bit counter can count up to a maximum of
 * 65536/1600 * =~ 41 seconds before rolling over.
 */

#define HZ		1600			/* req clock hz (ticks/sec) */
#define PRESCALER 	(PT_CRYSTAL / HZ)	/* crystal prescaler */
#define USECPERCYC	(1000000 / HZ)		/* usecs per counter cycle */
#define MAXTICK		(USECPERCYC * 0x10000)

static clock_t		(*tickfunc)();
static clock_t		tick;
static unsigned int	counter;

/* real timer resolution in Hz */
clock_t			_clk_tck = HZ;

void	 _sbd_settimer (clock_t);


static int
t0intr (int arg, struct xcptcontext *xcp)
{
    volatile struct i8254 *pt = (struct i8254 *) PA_TO_KVA1 (TIMER_BASE);
    clock_t newtick;

    /* acknowledge interrupt */
    (void) pt->pt_t0ack;

    /* call timer tick function */
    newtick = (*tickfunc) (1, xcp);
    
    /* reset timer if a new interrupt frequency is required */
    if (newtick != tick)
	_sbd_settimer (newtick);

    return 0;
}


void
_sbd_settimer (clock_t newtick)
{
    volatile struct i8254 *pt = (struct i8254 *) PA_TO_KVA1 (TIMER_BASE);

    /* work out how many counter cycles are required for this tick length */
    assert (newtick <= MAXTICK);
    tick = newtick;
    counter = tick / USECPERCYC;

    /* program timer #0 - note this resets timer so don't do it too often */
    pt->pt_control = PTCW_SC(PT_TIMER0)|PTCW_16B|PTCW_MODE(MODE_RG);
    pt->pt_timer0 = counter & 0xff; mips_wbflush();
    pt->pt_timer0 = counter >> 8; mips_wbflush();

    /* wait for the counter to be loaded for the first time */
    do {
      pt->pt_control = PTCW_RBCMD | PTCW_RB_NCNT | PTCW_RB_SC(PT_TIMER0);
      mips_wbflush();
    } while (pt->pt_timer0 & PTSW_NULL);

    /* remove any pending interrupt */
    (void) pt->pt_t0ack;
}


clock_t
_sbd_gettimer ()
{
    volatile struct i8254 *pt = (struct i8254 *) PA_TO_KVA1 (TIMER_BASE);
    unsigned int timer;

    /* read timer */
    pt->pt_control = PTCW_SC(PT_TIMER0)|PTCW_CLCMD; mips_wbflush();
    timer = pt->pt_timer0 & 0xff; 
    timer |= (pt->pt_timer0 & 0xff) << 8;

    /* convert downcount to upcount */
    timer = counter - timer;

    if (timer == counter - 1 && !intrpending (INTR_TINT0))
      /* interrupt occurred already (at start of T==1) */
      timer = -1;
    else if (timer < counter / 2 && intrpending (INTR_TINT0))
      /* counter rolled over, but no interrupt taken yet */
      timer += counter;

    /* return time in usecs */
    return timer * USECPERCYC;
}


clock_t
_sbd_inittimer (clock_t (*func)())
{
    volatile struct i8254 *pt = (struct i8254 *) PA_TO_KVA1 (TIMER_BASE);

    /* program prescaler */
    pt->pt_control = PTCW_SC(PT_PRESCALER)|PTCW_16B|PTCW_MODE(MODE_RG);
    pt->pt_prescaler = PRESCALER & 0xff; mips_wbflush();
    pt->pt_prescaler = PRESCALER >> 8;

    /* stop timer #0 */
    pt->pt_control = PTCW_SC(PT_TIMER0)|PTCW_16B|PTCW_MODE(MODE_HROS);
    pt->pt_timer0 = 0; mips_wbflush();
    pt->pt_timer0 = 0; mips_wbflush();

    /* remove any pending interrupt */
    (void) pt->pt_t0ack;

    /* install interrupt handler to catch timer overflow */
    tickfunc = func;
    intrupt (INTR_TINT0, t0intr, 0);
    mips_bissr (SR_IM0 << INTR_TINT0);	/* enable interrupt now */

    /* return maximum time between ticks, in usecs */
    return MAXTICK;
}
