Home | History | Annotate | Line # | Download | only in ic
mc146818reg.h revision 1.2.56.3
      1  1.2.56.3  skrll /*	$NetBSD: mc146818reg.h,v 1.2.56.3 2004/09/21 13:28:04 skrll Exp $	*/
      2       1.1    cgd 
      3       1.1    cgd /*
      4       1.1    cgd  * Copyright (c) 1995 Carnegie-Mellon University.
      5       1.1    cgd  * All rights reserved.
      6       1.1    cgd  *
      7       1.1    cgd  * Permission to use, copy, modify and distribute this software and
      8       1.1    cgd  * its documentation is hereby granted, provided that both the copyright
      9       1.1    cgd  * notice and this permission notice appear in all copies of the
     10       1.1    cgd  * software, derivative works or modified versions, and any portions
     11       1.1    cgd  * thereof, and that both notices appear in supporting documentation.
     12       1.1    cgd  *
     13       1.1    cgd  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     14       1.1    cgd  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     15       1.1    cgd  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     16       1.1    cgd  *
     17       1.1    cgd  * Carnegie Mellon requests users of this software to return to
     18       1.1    cgd  *
     19       1.1    cgd  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     20       1.1    cgd  *  School of Computer Science
     21       1.1    cgd  *  Carnegie Mellon University
     22       1.1    cgd  *  Pittsburgh PA 15213-3890
     23       1.1    cgd  *
     24       1.1    cgd  * any improvements or extensions that they make and grant Carnegie the
     25       1.1    cgd  * rights to redistribute these changes.
     26       1.1    cgd  */
     27       1.1    cgd 
     28       1.1    cgd /*
     29       1.1    cgd  * Definitions for the Motorola MC146818A Real Time Clock.
     30  1.2.56.1  skrll  * They also apply for the (compatible) Dallas Semiconductor DS1287A RTC.
     31       1.1    cgd  *
     32       1.1    cgd  * Though there are undoubtedly other (better) sources, this material was
     33       1.1    cgd  * culled from the DEC "KN121 System Module Programmer's Reference
     34       1.1    cgd  * Information."
     35       1.1    cgd  *
     36       1.1    cgd  * The MC146818A has 16 registers.  The first 10 contain time-of-year
     37       1.1    cgd  * and alarm data.  The rest contain various control and status bits.
     38       1.1    cgd  *
     39       1.1    cgd  * To read or write the registers, one writes the register number to
     40       1.1    cgd  * the RTC's control port, then either reads from or writes the new
     41       1.1    cgd  * data to the RTC's data port.  Since the locations of these ports
     42       1.1    cgd  * and the method used to access them can be machine-dependent, the
     43       1.1    cgd  * low-level details of reading and writing the RTC's registers are
     44       1.1    cgd  * handled by machine-specific functions.
     45       1.1    cgd  *
     46       1.1    cgd  * The time-of-year and alarm data can be expressed in either binary
     47       1.1    cgd  * or BCD, and they are selected by a bit in register B.
     48       1.1    cgd  *
     49       1.1    cgd  * The "hour" time-of-year and alarm fields can either be expressed in
     50       1.1    cgd  * AM/PM format, or in 24-hour format.  If AM/PM format is chosen, the
     51       1.1    cgd  * hour fields can have the values: 1-12 and 81-92 (the latter being
     52       1.1    cgd  * PM).  If the 24-hour format is chosen, they can have the values
     53       1.1    cgd  * 0-24.  The hour format is selectable by a bit in register B.
     54       1.1    cgd  * (XXX IS AM/PM MODE DESCRIPTION CORRECT?)
     55       1.1    cgd  *
     56       1.1    cgd  * It is assumed the if systems are going to use BCD (rather than
     57       1.1    cgd  * binary) mode, or AM/PM hour format, they'll do the appropriate
     58       1.1    cgd  * conversions in machine-dependent code.  Also, if the clock is
     59       1.1    cgd  * switched between BCD and binary mode, or between AM/PM mode and
     60       1.1    cgd  * 24-hour mode, the time-of-day and alarm registers are NOT
     61       1.1    cgd  * automatically reset; they must be reprogrammed with correct values.
     62       1.1    cgd  */
     63       1.1    cgd 
     64  1.2.56.1  skrll /* XXX not yet all port switch to MI mc146818(4) with todr(9) support */
     65  1.2.56.1  skrll #if defined(arc)
     66  1.2.56.1  skrll #define USE_TODR_MCCLOCK
     67  1.2.56.1  skrll #endif
     68  1.2.56.1  skrll 
     69       1.1    cgd /*
     70       1.1    cgd  * The registers, and the bits within each register.
     71       1.1    cgd  */
     72       1.1    cgd 
     73       1.1    cgd #define	MC_SEC		0x0	/* Time of year: seconds (0-59) */
     74       1.1    cgd #define	MC_ASEC		0x1	/* Alarm: seconds */
     75       1.1    cgd #define	MC_MIN		0x2	/* Time of year: minutes (0-59) */
     76       1.1    cgd #define	MC_AMIN		0x3	/* Alarm: minutes */
     77       1.1    cgd #define	MC_HOUR		0x4	/* Time of year: hour (see above) */
     78       1.1    cgd #define	MC_AHOUR	0x5	/* Alarm: hour */
     79       1.1    cgd #define	MC_DOW		0x6	/* Time of year: day of week (1-7) */
     80       1.1    cgd #define	MC_DOM		0x7	/* Time of year: day of month (1-31) */
     81       1.1    cgd #define	MC_MONTH	0x8	/* Time of year: month (1-12) */
     82       1.1    cgd #define	MC_YEAR		0x9	/* Time of year: year in century (0-99) */
     83       1.1    cgd 
     84       1.1    cgd #define	MC_REGA		0xa	/* Control register A */
     85       1.1    cgd 
     86       1.1    cgd #define	 MC_REGA_RSMASK	0x0f	/* Interrupt rate select mask (see below) */
     87       1.1    cgd #define	 MC_REGA_DVMASK	0x70	/* Divisor select mask (see below) */
     88       1.1    cgd #define	 MC_REGA_UIP	0x80	/* Update in progress; read only. */
     89       1.1    cgd 
     90       1.1    cgd #define	MC_REGB		0xb	/* Control register B */
     91       1.1    cgd 
     92       1.1    cgd #define	 MC_REGB_DSE	0x01	/* Daylight Savings Enable */
     93       1.1    cgd #define	 MC_REGB_24HR	0x02	/* 24-hour mode (AM/PM mode when clear) */
     94       1.1    cgd #define	 MC_REGB_BINARY	0x04	/* Binary mode (BCD mode when clear) */
     95       1.2    cgd #define	 MC_REGB_SQWE	0x08	/* Square Wave Enable */
     96       1.1    cgd #define	 MC_REGB_UIE	0x10	/* Update End interrupt enable */
     97       1.1    cgd #define	 MC_REGB_AIE	0x20	/* Alarm interrupt enable */
     98       1.1    cgd #define	 MC_REGB_PIE	0x40	/* Periodic interrupt enable */
     99       1.1    cgd #define	 MC_REGB_SET	0x80	/* Allow time to be set; stops updates */
    100       1.1    cgd 
    101       1.1    cgd #define	MC_REGC		0xc	/* Control register C */
    102       1.1    cgd 
    103       1.1    cgd /*	 MC_REGC_UNUSED	0x0f	UNUSED */
    104       1.1    cgd #define	 MC_REGC_UF	0x10	/* Update End interrupt flag */
    105       1.1    cgd #define	 MC_REGC_AF	0x20	/* Alarm interrupt flag */
    106       1.1    cgd #define	 MC_REGC_PF	0x40	/* Periodic interrupt flag */
    107       1.1    cgd #define	 MC_REGC_IRQF	0x80	/* Interrupt request pending flag */
    108       1.1    cgd 
    109       1.1    cgd #define	MC_REGD		0xd	/* Control register D */
    110       1.1    cgd 
    111       1.1    cgd /*	 MC_REGD_UNUSED	0x7f	UNUSED */
    112       1.1    cgd #define	 MC_REGD_VRT	0x80	/* Valid RAM and Time bit */
    113       1.1    cgd 
    114       1.1    cgd 
    115       1.1    cgd #define	MC_NREGS	0xe	/* 14 registers; CMOS follows */
    116       1.1    cgd #define	MC_NTODREGS	0xa	/* 10 of those regs are for TOD and alarm */
    117       1.1    cgd 
    118       1.1    cgd #define	MC_NVRAM_START	0xe	/* start of NVRAM: offset 14 */
    119       1.1    cgd #define	MC_NVRAM_SIZE	50	/* 50 bytes of NVRAM */
    120       1.1    cgd 
    121       1.1    cgd /*
    122       1.1    cgd  * Periodic Interrupt Rate Select constants (Control register A)
    123       1.1    cgd  */
    124       1.1    cgd #define	MC_RATE_NONE	0x0	/* No periodic interrupt */
    125       1.1    cgd #define	MC_RATE_1	0x1     /* 256 Hz if MC_BASE_32_KHz, else 32768 Hz */
    126       1.1    cgd #define	MC_RATE_2	0x2     /* 128 Hz if MC_BASE_32_KHz, else 16384 Hz */
    127       1.1    cgd #define	MC_RATE_8192_Hz	0x3	/* 122.070 us period */
    128       1.1    cgd #define	MC_RATE_4096_Hz	0x4	/* 244.141 us period */
    129       1.1    cgd #define	MC_RATE_2048_Hz	0x5	/* 488.281 us period */
    130       1.1    cgd #define	MC_RATE_1024_Hz	0x6	/* 976.562 us period */
    131       1.1    cgd #define	MC_RATE_512_Hz	0x7	/* 1.953125 ms period */
    132       1.1    cgd #define	MC_RATE_256_Hz	0x8	/* 3.90625 ms period */
    133       1.1    cgd #define	MC_RATE_128_Hz	0x9	/* 7.8125 ms period */
    134       1.1    cgd #define	MC_RATE_64_Hz	0xa	/* 15.625 ms period */
    135       1.1    cgd #define	MC_RATE_32_Hz	0xb	/* 31.25 ms period */
    136       1.1    cgd #define	MC_RATE_16_Hz	0xc	/* 62.5 ms period */
    137       1.1    cgd #define	MC_RATE_8_Hz	0xd	/* 125 ms period */
    138       1.1    cgd #define	MC_RATE_4_Hz	0xe	/* 250 ms period */
    139       1.1    cgd #define	MC_RATE_2_Hz	0xf	/* 500 ms period */
    140       1.1    cgd 
    141       1.1    cgd /*
    142       1.1    cgd  * Time base (divisor select) constants (Control register A)
    143       1.1    cgd  */
    144       1.1    cgd #define	MC_BASE_4_MHz	0x00		/* 4MHz crystal */
    145       1.1    cgd #define	MC_BASE_1_MHz	0x10		/* 1MHz crystal */
    146       1.1    cgd #define	MC_BASE_32_KHz	0x20		/* 32KHz crystal */
    147       1.1    cgd #define	MC_BASE_NONE	0x60		/* actually, both of these reset */
    148       1.1    cgd #define	MC_BASE_RESET	0x70
    149       1.1    cgd 
    150  1.2.56.1  skrll #ifndef USE_TODR_MCCLOCK
    151       1.1    cgd /*
    152       1.1    cgd  * RTC register/NVRAM read and write functions -- machine-dependent.
    153       1.1    cgd  * Appropriately manipulate RTC registers to get/put data values.
    154       1.1    cgd  */
    155  1.2.56.1  skrll u_int mc146818_read __P((void *, u_int));
    156  1.2.56.1  skrll void mc146818_write __P((void *, u_int, u_int));
    157       1.1    cgd 
    158       1.1    cgd /*
    159       1.1    cgd  * A collection of TOD/Alarm registers.
    160       1.1    cgd  */
    161       1.1    cgd typedef u_int mc_todregs[MC_NTODREGS];
    162       1.1    cgd 
    163       1.1    cgd /*
    164       1.1    cgd  * Get all of the TOD/Alarm registers
    165       1.1    cgd  * Must be called at splhigh(), and with the RTC properly set up.
    166       1.1    cgd  */
    167       1.1    cgd #define MC146818_GETTOD(sc, regs)					\
    168       1.1    cgd 	do {								\
    169       1.1    cgd 		int i;							\
    170       1.1    cgd 									\
    171       1.1    cgd 		/* update in progress; spin loop */			\
    172       1.1    cgd 		while (mc146818_read(sc, MC_REGA) & MC_REGA_UIP)	\
    173       1.1    cgd 			;						\
    174       1.1    cgd 									\
    175       1.1    cgd 		/* read all of the tod/alarm regs */			\
    176       1.1    cgd 		for (i = 0; i < MC_NTODREGS; i++)			\
    177       1.1    cgd 			(*regs)[i] = mc146818_read(sc, i);		\
    178       1.1    cgd 	} while (0);
    179       1.1    cgd 
    180       1.1    cgd /*
    181       1.1    cgd  * Set all of the TOD/Alarm registers
    182       1.1    cgd  * Must be called at splhigh(), and with the RTC properly set up.
    183       1.1    cgd  */
    184       1.1    cgd #define MC146818_PUTTOD(sc, regs)					\
    185       1.1    cgd 	do {								\
    186       1.1    cgd 		int i;							\
    187       1.1    cgd 									\
    188       1.1    cgd 		/* stop updates while setting */			\
    189       1.1    cgd 		mc146818_write(sc, MC_REGB,				\
    190       1.1    cgd 		    mc146818_read(sc, MC_REGB) | MC_REGB_SET);		\
    191       1.1    cgd 									\
    192       1.1    cgd 		/* write all of the tod/alarm regs */			\
    193       1.1    cgd 		for (i = 0; i < MC_NTODREGS; i++)			\
    194       1.1    cgd 			mc146818_write(sc, i, (*regs)[i]);		\
    195       1.1    cgd 									\
    196       1.1    cgd 		/* reenable updates */					\
    197       1.1    cgd 		mc146818_write(sc, MC_REGB,				\
    198       1.1    cgd 		    mc146818_read(sc, MC_REGB) & ~MC_REGB_SET);		\
    199       1.1    cgd 	} while (0);
    200  1.2.56.1  skrll #endif /* USE_TODR_MCCLOCK */
    201