ds1687reg.h revision 1.8
1/*	$NetBSD: ds1687reg.h,v 1.8 2005/12/11 12:21:26 christos Exp $ 	*/
2
3/*
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Rafal K. Boni.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the NetBSD
21 *	Foundation, Inc. and its contributors.
22 * 4. The name of the author may not be used to endorse or promote products
23 *    derived from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37/*
38 * Originally based on mc146818reg.h, with the following license:
39 *
40 * Copyright (c) 1995 Carnegie-Mellon University.
41 * All rights reserved.
42 *
43 * Permission to use, copy, modify and distribute this software and
44 * its documentation is hereby granted, provided that both the copyright
45 * notice and this permission notice appear in all copies of the
46 * software, derivative works or modified versions, and any portions
47 * thereof, and that both notices appear in supporting documentation.
48 *
49 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
50 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
51 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
52 *
53 * Carnegie Mellon requests users of this software to return to
54 *
55 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
56 *  School of Computer Science
57 *  Carnegie Mellon University
58 *  Pittsburgh PA 15213-3890
59 *
60 * any improvements or extensions that they make and grant Carnegie the
61 * rights to redistribute these changes.
62 */
63
64/*
65 * Definitions for the Dallas Semiconductor DS1687 Real Time Clock.
66 *
67 * The DS1687 and follow-on RTC chips are Y2k-compliant successors to the
68 * DS1287, which in turn is register-compatible with the MC146818 and/or
69 * MC146818A RTCs.
70 *
71 * Plucked right from the Dallas Semicomductor specs available at:
72 *	http://pdfserv.maxim-ic.com/arpdf/DS1685-DS1687.pdf
73 *
74 * The DS1686 contains 14 basic clock-related registers and 50 bytes of
75 * user RAM laid out for compatibility with the register layout of the
76 * DS1287/MC14818 chips.  It also includes an extended mode which allows
77 * access to these same basic registers as well an an extended register
78 * set and NVRAM area; this extended register set includes a century
79 * register for Y2k compliant date storage.
80 *
81 * Since the locations of these ports and the method used to access them
82 * can be machine-dependent, the low-level details of reading and writing
83 * writing the RTC's registers are handled by machine-specific functions.
84 *
85 * The Dallas chip can store time-of-day and alarm data in BCD or binary;
86 * this setting applies to *all* values stored in the clock chip and a
87 * change from one mode to the other requires *all* of the clock data to
88 * be re-written.   The "hours" time-of-year and alarm registers can be
89 * stored either in an AM/PM or a 24-hour format; the format is set
90 * globally and changing it requires re-writing both the hours time-of-
91 * year and alarm registers.  In AM/PM mode, the hour must be in the
92 * range of 1-12 (and stored as either BCD or binary), with the high-
93 * bit cleared to indicate AM and set to indicate PM.  In 24-hour mode,
94 * hours must be in the range 0-23.
95 *
96 * In order to support extended features like the century register and
97 * an embedded silicon serial number while keeping backwards compatibility
98 * with the DS1287/MC146818, the DS1687 provides a bank-switching method
99 * which allows the user to switch the RTC between a "compatible" mode in
100 * bank 0 and an extended mode in bank 1.
101 *
102 * Both banks provide access to the 14 timekeeping/alarm registers and
103 * to 50 bytes of user RAM.  In addition, bank 0 provides access to an
104 * additional 64 bytes of user RAM in the upper half of the RTC address
105 * space.
106 *
107 * Bank 1, on the other hand, provides access to an extended register set,
108 * including a silicon serial number -- including a model ID byte, century
109 * register for Y2k compatibility and memory address/data registers which
110 * allow indirect access to a larger extended user RAM address space.  It
111 * is worth noting that the extended user RAM is distinct from the "basic"
112 * 114 bytes of user RAM which are accesible in bank 0.
113 */
114
115/*
116 * The registers, and the bits within each register.
117 */
118
119#define	DS1687_SEC	0x00	/* Time of year: seconds (0-59) */
120#define DS1687_ASEC	0x01	/* Alarm: seconds */
121#define	DS1687_MIN	0x02	/* Time of year: minutes (0-59) */
122#define	DS1687_AMIN	0x03	/* Alarm: minutes */
123#define	DS1687_HOUR	0x04	/* Time of year: hour (see above) */
124#define	DS1687_AHOUR	0x05	/* Alarm: hour (see above) */
125#define	DS1687_DOW	0x06	/* Time of year: day of week (1-7, 1 = Sun) */
126#define	DS1687_DOM	0x07	/* Time of year: day of month (1-31) */
127#define	DS1687_MONTH	0x08	/* Time of year: month (1-12) */
128#define	DS1687_YEAR	0x09	/* Time of year: year in century (0-99) */
129
130#define DS1687_CONTROLA	0x0a	/* Control Register A */
131
132#define DS1687_UIP	0x80	/* Update in progress: RO */
133#define DS1687_DV2	0x40	/* Countdown chain: 0 = on,  1 = reset if DV1 */
134#define DS1687_DV1	0x20	/* Oscillator enable */
135#define DS1687_BANK1	0x10	/* Bank select: 0 = bank0, 1 = bank1 */
136#define DS1687_RATEMASK 0x0f	/* Rate select bits for sq. wave and PIE */
137
138#define DS1687_CONTROLB	0x0b	/* Control Register B */
139
140#define DS1687_SET	0x80	/* Clock update control: 1 = disable update */
141#define DS1687_PIE	0x40	/* Periodic interrupt enable */
142#define DS1687_AIE	0x20	/* Alarm interrupt enable */
143#define DS1687_UIE	0x10	/* Update-ended interrupt enable */
144#define DS1687_SQWE	0x08	/* Enable sq. wave output on SQW pin */
145#define DS1687_BINARY	0x04	/* Data mode: 0 = BCD, 1 = binary data */
146#define DS1687_24HRS	0x02	/* Hour format: 1 = 24hrs, 0 = 12hrs */
147#define DS1687_DSE	0x01	/* Daylight savings enable */
148
149#define DS1687_CONTROLC	0x0c	/* Control register C: Read-only */
150				/* Note: PF, AF, UF cleared on read */
151
152#define DS1687_IRQF	0x80	/* IRQ present: set when any IRQ is active */
153#define DS1687_PF	0x40	/* Periodic interrupt: independent of PIE */
154#define DS1687_AF	0x20	/* Alarm reached: independent of AIE */
155#define DS1687_UF	0x10	/* Update ended: independent of UIE */
156
157#define DS1687_CONTROLD	0x0d	/* Control register D: Read-only */
158
159#define DS1687_VRT	0x80	/* Valid RAM and time: battery bad if 0 */
160
161#define DS1687_NVRAM_START	0x0e	/* Start of user ram: offset 14 */
162#define DS1687_NVRAM_SIZE	0x72	/* 114 bytes of user RAM */
163
164#define DS1687_BANK1_START	0x40	/* BANK1: Start of BANK1 registers */
165#define	DS1687_BANK1_CENTURY	0x48 	/* BANK1: Time of yr: Century (0-99) */
166#define	DS1687_BANK1_ADATE	0x49	/* BANK1: Alarm: Date (1-31) */
167
168#define	DS1687_NBASEREGS	0x0d	/* 14 registers; CMOS follows */
169
170/* Layout of software shadow copy of TOD registers */
171#define DS1687_NHDW_TODREGS	0x0a	/* 10 basic TOD registers */
172#define DS1687_NSOFT_TODREGS	0x0c	/* ...plus shadow CENTURY, ADATE */
173
174#define	DS1687_SOFT_SEC		0x00
175#define DS1687_SOFT_ASEC	0x01
176#define	DS1687_SOFT_MIN		0x02
177#define	DS1687_SOFT_AMIN	0x03
178#define	DS1687_SOFT_HOUR	0x04
179#define	DS1687_SOFT_AHOUR	0x05
180#define	DS1687_SOFT_DOW		0x06
181#define	DS1687_SOFT_DOM		0x07
182#define	DS1687_SOFT_MONTH	0x08
183#define	DS1687_SOFT_YEAR	0x09
184#define DS1687_SOFT_CENTURY	0x0a
185#define DS1687_SOFT_ADATE	0x0b
186
187/*
188 * RTC register/NVRAM read and write functions -- machine-dependent.
189 * Appropriately manipulate RTC registers to get/put data values.
190 */
191u_int ds1687_read(void *, u_int);
192void ds1687_write(void *, u_int, u_int);
193
194/*
195 * A collection of TOD/Alarm registers.
196 */
197typedef u_int ds1687_todregs[DS1687_NSOFT_TODREGS];
198
199/*
200 * Get all of the TOD/Alarm registers
201 * Must be called at splhigh(), and with the RTC properly set up.
202 */
203#define DS1687_GETTOD(sc, regs)						\
204	do {								\
205		int i;							\
206		u_int ctl;						\
207									\
208		/* turn off update for now */				\
209		ctl = ds1687_read(sc, DS1687_CONTROLB);			\
210		ds1687_write(sc, DS1687_CONTROLB, ctl | DS1687_SET);	\
211									\
212		/* read all of the tod/alarm regs */			\
213		for (i = 0; i < DS1687_NHDW_TODREGS; i++) 		\
214			(*regs)[i] = ds1687_read(sc, i);		\
215									\
216		(*regs)[DS1687_SOFT_CENTURY] = 				\
217				ds1687_read(sc, DS1687_BANK1_CENTURY);	\
218		(*regs)[DS1687_SOFT_ADATE] = 				\
219				ds1687_read(sc, DS1687_BANK1_ADATE);	\
220									\
221		/* turn update back on */				\
222		ds1687_write(sc, DS1687_CONTROLB, ctl);			\
223	} while (0);
224
225/*
226 * Set all of the TOD/Alarm registers
227 * Must be called at splhigh(), and with the RTC properly set up.
228 */
229#define DS1687_PUTTOD(sc, regs)						\
230	do {								\
231		int i;							\
232		u_int ctl;						\
233									\
234		/* turn off update for now */				\
235		ctl = ds1687_read(sc, DS1687_CONTROLB);			\
236		ds1687_write(sc, DS1687_CONTROLB, ctl | DS1687_SET);	\
237									\
238		/* write all of the tod/alarm regs */			\
239		for (i = 0; i < DS1687_NHDW_TODREGS; i++) 		\
240			ds1687_write(sc, i, (*regs)[i]);		\
241									\
242		ds1687_write(sc, DS1687_BANK1_CENTURY,			\
243					(*regs)[DS1687_SOFT_CENTURY]);	\
244		ds1687_write(sc, DS1687_BANK1_ADATE,			\
245					(*regs)[DS1687_SOFT_ADATE]);	\
246									\
247		/* turn update back on */				\
248		ds1687_write(sc, DS1687_CONTROLB, ctl);			\
249	} while (0);
250