#ident "kit/COGENT/sbdtime.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.
 */

/*
 * mk48t02.c -- MK48T02 Real Time Clock and Non-Volatile RAM Chip
 */


#include <stddef.h>
#include <time.h>
#include <sys/clock.h>
#include <sys/errno.h>
#include <mips/cpu.h>
#include "kit_impl.h"

#include "sbd.h"
#include "mk48t02.h"

#define SECMIN		(60u)
#define SECHOUR		(60u * 60u)
#define SECDAY		(60u * 60u * 24u)
#define	SECYR		(60u * 60u * 24u * 365u)

#define	LEAPYEAR(y)	(((y) % 4) == 0)	/* good 'til 2100 AD */
#define YRREF		1970			/* start of *nix era */

static unsigned int norm_msecs[12] =
{
    31*SECDAY,	/* January */
    28*SECDAY,	/* February */
    31*SECDAY,	/* March */
    30*SECDAY,	/* April */
    31*SECDAY,	/* May */
    30*SECDAY,	/* June */
    31*SECDAY,	/* July */
    31*SECDAY,	/* August */
    30*SECDAY,	/* September */
    31*SECDAY,	/* October */
    30*SECDAY,	/* November */
    31*SECDAY	/* December */
};


static time_t
rtc_mktime (const struct tm *tm)
{
    time_t secs;
    int i;
    
    if (tm->tm_sec >= 60 || tm->tm_min >= 60 || 
	tm->tm_hour >= 24 || tm->tm_mday > 31 || tm->tm_mon > 12) {
	return (time_t)-1;
    }
    
    /*
     * Sum up seconds from beginning of year
     */
    secs = tm->tm_sec;
    secs += tm->tm_min * SECMIN;
    secs += tm->tm_hour * SECHOUR;
    for (i = 0; i < tm->tm_mon; i++)
      secs += norm_msecs[i];
    secs += (tm->tm_mday-1) * SECDAY;
    
    /* Correct current leap-year error. */
    if (LEAPYEAR(tm->tm_year) && tm->tm_mon > 1)
      secs += SECDAY;

    /* Add year contribution to seconds count */
    for (i = tm->tm_year - 1; i >= YRREF; i--)
      secs += LEAPYEAR(i) ? SECYR + SECDAY : SECYR;
    
    return secs;
}


static unsigned int
bcdtobin (unsigned int bcd)
{
    return ((bcd >> 4) & 0x0f) * 10 + (bcd & 0x0f);
}


#if 0
static unsigned int
bintobcd (unsigned int bin)
{
    return (((bin / 10) << 4) + bin % 10);
}
#endif

time_t
_sbd_gettod ()
{
    volatile struct td_clock *td = (struct td_clock *)PA_TO_KVA1(RTCLOCK_BASE);
    struct tm tm;
    
    td->td_control |= TDC_READ;
    mips_wbflush();

    tm.tm_sec = bcdtobin(td->td_secs);
    tm.tm_min = bcdtobin(td->td_mins);
    tm.tm_hour = bcdtobin(td->td_hours);
    tm.tm_mday = bcdtobin(td->td_date);
    tm.tm_mon = bcdtobin(td->td_month) - 1;
    tm.tm_year = bcdtobin(td->td_year);

    td->td_control &= ~TDC_READ;
    mips_wbflush();
    
    /* Have we entered the next century? */
    if ((tm.tm_year += 1900) < YRREF)
      tm.tm_year += 100;
    
    tm.tm_wday = 0;		/* ignored by mktime */
    tm.tm_yday = 0;		/* ignored by mktime */
    tm.tm_isdst = 0;		/* GMT in the RTC please */
    
    /* return time in seconds */
    return rtc_mktime (&tm);
}
