1 /* mc146818rtc.h - register definitions for the Real-Time-Clock / CMOS RAM
2 * Copyright Torsten Duwe <duwe@informatik.uni-erlangen.de> 1993
3 * derived from Data Sheet, Copyright Motorola 1984 (!).
4 * It was written to be part of the Linux operating system.
6 /* permission is hereby granted to copy, modify and redistribute this code
7 * in terms of the GNU Library General Public License, Version 2 or later,
11 #ifndef _MC146818RTC_H
12 #define _MC146818RTC_H
15 #include <linux/rtc.h> /* get the user-level API */
16 #include <asm/mc146818rtc.h> /* register access macros */
17 #include <linux/bcd.h>
18 #include <linux/delay.h>
21 #include <linux/acpi.h>
25 #include <linux/spinlock.h> /* spinlock_t */
26 extern spinlock_t rtc_lock; /* serialize CMOS RAM access */
28 /* Some RTCs extend the mc146818 register set to support alarms of more
29 * than 24 hours in the future; or dates that include a century code.
30 * This platform_data structure can pass this information to the driver.
32 * Also, some platforms need suspend()/resume() hooks to kick in special
33 * handling of wake alarms, e.g. activating ACPI BIOS hooks or setting up
34 * a separate wakeup alarm used by some almost-clone chips.
36 struct cmos_rtc_board_info {
37 void (*wake_on)(struct device *dev);
38 void (*wake_off)(struct device *dev);
41 #define CMOS_RTC_FLAGS_NOFREQ (1 << 0)
44 u8 rtc_day_alarm; /* zero, or register index */
45 u8 rtc_mon_alarm; /* zero, or register index */
46 u8 rtc_century; /* zero, or register index */
50 /**********************************************************************
52 **********************************************************************/
54 #define RTC_SECONDS_ALARM 1
56 #define RTC_MINUTES_ALARM 3
58 #define RTC_HOURS_ALARM 5
59 /* RTC_*_alarm is always true if 2 MSBs are set */
60 # define RTC_ALARM_DONT_CARE 0xC0
62 #define RTC_DAY_OF_WEEK 6
63 #define RTC_DAY_OF_MONTH 7
67 /* control registers - Moto names
74 /**********************************************************************
76 **********************************************************************/
77 #define RTC_FREQ_SELECT RTC_REG_A
79 /* update-in-progress - set to "1" 244 microsecs before RTC goes off the bus,
80 * reset after update (may take 1.984ms @ 32768Hz RefClock) is complete,
81 * totalling to a max high interval of 2.228 ms.
84 # define RTC_DIV_CTL 0x70
85 /* divider control: refclock values 4.194 / 1.049 MHz / 32.768 kHz */
86 # define RTC_REF_CLCK_4MHZ 0x00
87 # define RTC_REF_CLCK_1MHZ 0x10
88 # define RTC_REF_CLCK_32KHZ 0x20
89 /* 2 values for divider stage reset, others for "testing purposes only" */
90 # define RTC_DIV_RESET1 0x60
91 # define RTC_DIV_RESET2 0x70
92 /* Periodic intr. / Square wave rate select. 0=none, 1=32.8kHz,... 15=2Hz */
93 # define RTC_RATE_SELECT 0x0F
95 /**********************************************************************/
96 #define RTC_CONTROL RTC_REG_B
97 # define RTC_SET 0x80 /* disable updates for clock setting */
98 # define RTC_PIE 0x40 /* periodic interrupt enable */
99 # define RTC_AIE 0x20 /* alarm interrupt enable */
100 # define RTC_UIE 0x10 /* update-finished interrupt enable */
101 # define RTC_SQWE 0x08 /* enable square-wave output */
102 # define RTC_DM_BINARY 0x04 /* all time/date values are BCD if clear */
103 # define RTC_24H 0x02 /* 24 hour mode - else hours bit 7 means pm */
104 # define RTC_DST_EN 0x01 /* auto switch DST - works f. USA only */
106 /**********************************************************************/
107 #define RTC_INTR_FLAGS RTC_REG_C
108 /* caution - cleared by read */
109 # define RTC_IRQF 0x80 /* any of the following 3 is active */
114 /**********************************************************************/
115 #define RTC_VALID RTC_REG_D
116 # define RTC_VRT 0x80 /* valid RAM and time */
117 /**********************************************************************/
119 #ifndef ARCH_RTC_LOCATION /* Override by <asm/mc146818rtc.h>? */
121 #define RTC_IO_EXTENT 0x8
122 #define RTC_IO_EXTENT_USED 0x2
123 #define RTC_IOMAPPED 1 /* Default to I/O mapping. */
126 #define RTC_IO_EXTENT_USED RTC_IO_EXTENT
127 #endif /* ARCH_RTC_LOCATION */
130 * Returns true if a clock update is in progress
132 static inline unsigned char mc146818_is_updating(void)
137 spin_lock_irqsave(&rtc_lock, flags);
138 uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
139 spin_unlock_irqrestore(&rtc_lock, flags);
143 static inline unsigned int mc146818_get_time(struct rtc_time *time)
147 unsigned char century = 0;
149 #ifdef CONFIG_MACH_DECSTATION
150 unsigned int real_year;
154 * read RTC once any update in progress is done. The update
155 * can take just over 2ms. We wait 20ms. There is no need to
156 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
157 * If you need to know *exactly* when a second has started, enable
158 * periodic update complete interrupts, (via ioctl) and then
159 * immediately read /dev/rtc which will block until you get the IRQ.
160 * Once the read clears, read the RTC time (again via ioctl). Easy.
162 if (mc146818_is_updating())
166 * Only the values that we read from the RTC are set. We leave
167 * tm_wday, tm_yday and tm_isdst untouched. Even though the
168 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
169 * by the RTC when initially set to a non-zero value.
171 spin_lock_irqsave(&rtc_lock, flags);
172 time->tm_sec = CMOS_READ(RTC_SECONDS);
173 time->tm_min = CMOS_READ(RTC_MINUTES);
174 time->tm_hour = CMOS_READ(RTC_HOURS);
175 time->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
176 time->tm_mon = CMOS_READ(RTC_MONTH);
177 time->tm_year = CMOS_READ(RTC_YEAR);
178 #ifdef CONFIG_MACH_DECSTATION
179 real_year = CMOS_READ(RTC_DEC_YEAR);
182 if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
183 acpi_gbl_FADT.century)
184 century = CMOS_READ(acpi_gbl_FADT.century);
186 ctrl = CMOS_READ(RTC_CONTROL);
187 spin_unlock_irqrestore(&rtc_lock, flags);
189 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
191 time->tm_sec = bcd2bin(time->tm_sec);
192 time->tm_min = bcd2bin(time->tm_min);
193 time->tm_hour = bcd2bin(time->tm_hour);
194 time->tm_mday = bcd2bin(time->tm_mday);
195 time->tm_mon = bcd2bin(time->tm_mon);
196 time->tm_year = bcd2bin(time->tm_year);
197 century = bcd2bin(century);
200 #ifdef CONFIG_MACH_DECSTATION
201 time->tm_year += real_year - 72;
205 time->tm_year += (century - 19) * 100;
208 * Account for differences between how the RTC uses the values
209 * and how they are defined in a struct rtc_time;
211 if (time->tm_year <= 69)
212 time->tm_year += 100;
219 /* Set the current date and time in the real time clock. */
220 static inline int mc146818_set_time(struct rtc_time *time)
223 unsigned char mon, day, hrs, min, sec;
224 unsigned char save_control, save_freq_select;
226 #ifdef CONFIG_MACH_DECSTATION
227 unsigned int real_yrs, leap_yr;
229 unsigned char century = 0;
232 mon = time->tm_mon + 1; /* tm_mon starts at zero */
238 if (yrs > 255) /* They are unsigned */
241 spin_lock_irqsave(&rtc_lock, flags);
242 #ifdef CONFIG_MACH_DECSTATION
244 leap_yr = ((!((yrs + 1900) % 4) && ((yrs + 1900) % 100)) ||
245 !((yrs + 1900) % 400));
249 * We want to keep the year set to 73 until March
250 * for non-leap years, so that Feb, 29th is handled
253 if (!leap_yr && mon < 3) {
260 if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
261 acpi_gbl_FADT.century) {
262 century = (yrs + 1900) / 100;
267 /* These limits and adjustments are independent of
268 * whether the chip is in binary mode or not.
271 spin_unlock_irqrestore(&rtc_lock, flags);
278 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
286 century = bin2bcd(century);
289 save_control = CMOS_READ(RTC_CONTROL);
290 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
291 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
292 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
294 #ifdef CONFIG_MACH_DECSTATION
295 CMOS_WRITE(real_yrs, RTC_DEC_YEAR);
297 CMOS_WRITE(yrs, RTC_YEAR);
298 CMOS_WRITE(mon, RTC_MONTH);
299 CMOS_WRITE(day, RTC_DAY_OF_MONTH);
300 CMOS_WRITE(hrs, RTC_HOURS);
301 CMOS_WRITE(min, RTC_MINUTES);
302 CMOS_WRITE(sec, RTC_SECONDS);
304 if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
305 acpi_gbl_FADT.century)
306 CMOS_WRITE(century, acpi_gbl_FADT.century);
309 CMOS_WRITE(save_control, RTC_CONTROL);
310 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
312 spin_unlock_irqrestore(&rtc_lock, flags);
317 #endif /* _MC146818RTC_H */