/*
 * 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.h -- MK48T02 Real Time Clock and Non-Volatile RAM
 */


#ifndef _MK48T02_
#define _MK48T02_

/*
 * General time definitions
 */
#define	TD_YRREF	1970			/* start of Unix era */
#define	TD_LEAPYEAR(y) (((y) % 4) == 0 && ((y) % 100) != 0 || ((y) % 400) == 0)

/* default time if reprogrammed by ROM */
#define TDSTART	((1991-TD_YRREF)*SECYR + 166*SECDAY + 12*SECHOUR)

/* sensible time: reset if less then this */
#define TDSENSIBLE	(TDSTART + 7*SECDAY)

/*
 * To maintain endianess independence, make all accesses as 32-bit
 * words with appropriate shifting.
 */
#define TD_NVRAM_SIZE	0x7f8	

#ifdef MIPSEB
#define tdreg(x)	unsigned :32; unsigned :24; unsigned char x
#define TDREG(x)	((8*(x))+7)
#endif
#ifdef MIPSEL
#define tdreg(x)	unsigned char x; unsigned :24; unsigned :32
#define TDREG(x)	(8*(x))
#endif

/*
 * Definitions for use MK48T02/12 real time clock
 */
#ifndef __ASSEMBLER__
struct td_clock {
    struct td_mem {
	tdreg(td_value);
    } td_mem[TD_NVRAM_SIZE];	/* Non-volatile ram */
    tdreg(td_control);	/* control register */
    tdreg(td_secs);
    tdreg(td_mins);
    tdreg(td_hours);
    tdreg(td_day);
    tdreg(td_date);
    tdreg(td_month);
    tdreg(td_year);
};
#endif

/*
 * 'nvram' locations for clock registers
 */
#define TD_CONTROL	TDREG(2040)
#define TD_SECS		TDREG(2041)
#define TD_MINS		TDREG(2042)
#define	TD_HOURS	TDREG(2043)
#define TD_DAY		TDREG(2044)
#define TD_DATE		TDREG(2045)
#define TD_MONTH	TDREG(2046)
#define TD_YEAR		TDREG(2047)

/*
 * Control register bit definitions
 */
#define	TDC_WRITE	0x80	/* write lock */
#define	TDC_READ	0x40	/* read lock */
#define TDC_CALIBRATE	0x3f	/* oscillator calibration */

/*
 * Seconds register bit definitions
 */
#define	TDS_STOP	0x80	/* stop oscillator */

/*
 * Hours register bit definitions
 */
#define	TDH_KICK	0x80	/* kick oscillator */

/*
 * Day register bit definitions
 */
#define	TDD_FRQTST	0x40	/* frequency test */
#define TDD_SUNDAY	1

#define THURSDAY	4

#endif /* _MK48T02_ */

