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