blob: be4af0029ac0b451cd89c91f239d3eca231a2e62 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * include/asm-generic/rtc.h
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Author: Tom Rini <trini@mvista.com>
5 *
6 * Based on:
7 * drivers/char/rtc.c
8 *
9 * Please read the COPYING file for all license details.
10 */
11
12#ifndef __ASM_RTC_H__
13#define __ASM_RTC_H__
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/mc146818rtc.h>
16#include <linux/rtc.h>
17#include <linux/bcd.h>
18
19#define RTC_PIE 0x40 /* periodic interrupt enable */
20#define RTC_AIE 0x20 /* alarm interrupt enable */
21#define RTC_UIE 0x10 /* update-finished interrupt enable */
22
23/* some dummy definitions */
24#define RTC_BATT_BAD 0x100 /* battery bad */
25#define RTC_SQWE 0x08 /* enable square-wave output */
26#define RTC_DM_BINARY 0x04 /* all time/date values are BCD if clear */
27#define RTC_24H 0x02 /* 24 hour mode - else hours bit 7 means pm */
28#define RTC_DST_EN 0x01 /* auto switch DST - works f. USA only */
29
30/*
31 * Returns true if a clock update is in progress
32 */
33static inline unsigned char rtc_is_updating(void)
34{
35 unsigned char uip;
Andrew Morton795d45b2008-02-04 16:48:10 +010036 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Andrew Morton795d45b2008-02-04 16:48:10 +010038 spin_lock_irqsave(&rtc_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
Andrew Morton795d45b2008-02-04 16:48:10 +010040 spin_unlock_irqrestore(&rtc_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 return uip;
42}
43
44static inline unsigned int get_rtc_time(struct rtc_time *time)
45{
46 unsigned long uip_watchdog = jiffies;
47 unsigned char ctrl;
Andrew Morton795d45b2008-02-04 16:48:10 +010048 unsigned long flags;
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#ifdef CONFIG_MACH_DECSTATION
51 unsigned int real_year;
52#endif
53
54 /*
55 * read RTC once any update in progress is done. The update
56 * can take just over 2ms. We wait 10 to 20ms. There is no need to
57 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
58 * If you need to know *exactly* when a second has started, enable
59 * periodic update complete interrupts, (via ioctl) and then
60 * immediately read /dev/rtc which will block until you get the IRQ.
61 * Once the read clears, read the RTC time (again via ioctl). Easy.
62 */
63
64 if (rtc_is_updating() != 0)
65 while (jiffies - uip_watchdog < 2*HZ/100) {
66 barrier();
67 cpu_relax();
68 }
69
70 /*
71 * Only the values that we read from the RTC are set. We leave
72 * tm_wday, tm_yday and tm_isdst untouched. Even though the
73 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
74 * by the RTC when initially set to a non-zero value.
75 */
Andrew Morton795d45b2008-02-04 16:48:10 +010076 spin_lock_irqsave(&rtc_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 time->tm_sec = CMOS_READ(RTC_SECONDS);
78 time->tm_min = CMOS_READ(RTC_MINUTES);
79 time->tm_hour = CMOS_READ(RTC_HOURS);
80 time->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
81 time->tm_mon = CMOS_READ(RTC_MONTH);
82 time->tm_year = CMOS_READ(RTC_YEAR);
83#ifdef CONFIG_MACH_DECSTATION
84 real_year = CMOS_READ(RTC_DEC_YEAR);
85#endif
86 ctrl = CMOS_READ(RTC_CONTROL);
Andrew Morton795d45b2008-02-04 16:48:10 +010087 spin_unlock_irqrestore(&rtc_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
90 {
91 BCD_TO_BIN(time->tm_sec);
92 BCD_TO_BIN(time->tm_min);
93 BCD_TO_BIN(time->tm_hour);
94 BCD_TO_BIN(time->tm_mday);
95 BCD_TO_BIN(time->tm_mon);
96 BCD_TO_BIN(time->tm_year);
97 }
98
99#ifdef CONFIG_MACH_DECSTATION
100 time->tm_year += real_year - 72;
101#endif
102
103 /*
104 * Account for differences between how the RTC uses the values
105 * and how they are defined in a struct rtc_time;
106 */
107 if (time->tm_year <= 69)
108 time->tm_year += 100;
109
110 time->tm_mon--;
111
112 return RTC_24H;
113}
114
115/* Set the current date and time in the real time clock. */
116static inline int set_rtc_time(struct rtc_time *time)
117{
Linus Torvaldseb71c872006-06-24 14:27:42 -0700118 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 unsigned char mon, day, hrs, min, sec;
120 unsigned char save_control, save_freq_select;
121 unsigned int yrs;
122#ifdef CONFIG_MACH_DECSTATION
123 unsigned int real_yrs, leap_yr;
124#endif
125
126 yrs = time->tm_year;
127 mon = time->tm_mon + 1; /* tm_mon starts at zero */
128 day = time->tm_mday;
129 hrs = time->tm_hour;
130 min = time->tm_min;
131 sec = time->tm_sec;
132
133 if (yrs > 255) /* They are unsigned */
134 return -EINVAL;
135
Linus Torvaldseb71c872006-06-24 14:27:42 -0700136 spin_lock_irqsave(&rtc_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#ifdef CONFIG_MACH_DECSTATION
138 real_yrs = yrs;
139 leap_yr = ((!((yrs + 1900) % 4) && ((yrs + 1900) % 100)) ||
140 !((yrs + 1900) % 400));
141 yrs = 72;
142
143 /*
144 * We want to keep the year set to 73 until March
145 * for non-leap years, so that Feb, 29th is handled
146 * correctly.
147 */
148 if (!leap_yr && mon < 3) {
149 real_yrs--;
150 yrs = 73;
151 }
152#endif
153 /* These limits and adjustments are independent of
154 * whether the chip is in binary mode or not.
155 */
156 if (yrs > 169) {
Linus Torvaldseb71c872006-06-24 14:27:42 -0700157 spin_unlock_irqrestore(&rtc_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return -EINVAL;
159 }
160
161 if (yrs >= 100)
162 yrs -= 100;
163
164 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
165 || RTC_ALWAYS_BCD) {
166 BIN_TO_BCD(sec);
167 BIN_TO_BCD(min);
168 BIN_TO_BCD(hrs);
169 BIN_TO_BCD(day);
170 BIN_TO_BCD(mon);
171 BIN_TO_BCD(yrs);
172 }
173
174 save_control = CMOS_READ(RTC_CONTROL);
175 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
176 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
177 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
178
179#ifdef CONFIG_MACH_DECSTATION
180 CMOS_WRITE(real_yrs, RTC_DEC_YEAR);
181#endif
182 CMOS_WRITE(yrs, RTC_YEAR);
183 CMOS_WRITE(mon, RTC_MONTH);
184 CMOS_WRITE(day, RTC_DAY_OF_MONTH);
185 CMOS_WRITE(hrs, RTC_HOURS);
186 CMOS_WRITE(min, RTC_MINUTES);
187 CMOS_WRITE(sec, RTC_SECONDS);
188
189 CMOS_WRITE(save_control, RTC_CONTROL);
190 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
191
Linus Torvaldseb71c872006-06-24 14:27:42 -0700192 spin_unlock_irqrestore(&rtc_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 return 0;
195}
196
197static inline unsigned int get_rtc_ss(void)
198{
199 struct rtc_time h;
200
201 get_rtc_time(&h);
202 return h.tm_sec;
203}
204
205static inline int get_rtc_pll(struct rtc_pll_info *pll)
206{
207 return -EINVAL;
208}
209static inline int set_rtc_pll(struct rtc_pll_info *pll)
210{
211 return -EINVAL;
212}
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214#endif /* __ASM_RTC_H__ */