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