2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Machine dependent access functions for RTC registers.
8 #ifndef __ASM_MC146818_TIME_H
9 #define __ASM_MC146818_TIME_H
11 #include <linux/bcd.h>
12 #include <linux/mc146818rtc.h>
13 #include <linux/time.h>
16 * For check timing call set_rtc_mmss() 500ms; used in timer interrupt.
18 #define USEC_AFTER 500000
19 #define USEC_BEFORE 500000
22 * In order to set the CMOS clock precisely, set_rtc_mmss has to be
23 * called 500 ms after the second nowtime has started, because when
24 * nowtime is written into the registers of the CMOS clock, it will
25 * jump to the next second precisely 500 ms later. Check the Motorola
26 * MC146818A or Dallas DS12887 data sheet for details.
28 * BUG: This routine does not handle hour overflow properly; it just
29 * sets the minutes. Usually you'll only notice that after reboot!
31 static inline int mc146818_set_rtc_mmss(unsigned long nowtime
)
33 int real_seconds
, real_minutes
, cmos_minutes
;
34 unsigned char save_control
, save_freq_select
;
38 spin_lock_irqsave(&rtc_lock
, flags
);
39 save_control
= CMOS_READ(RTC_CONTROL
); /* tell the clock it's being set */
40 CMOS_WRITE((save_control
|RTC_SET
), RTC_CONTROL
);
42 save_freq_select
= CMOS_READ(RTC_FREQ_SELECT
); /* stop and reset prescaler */
43 CMOS_WRITE((save_freq_select
|RTC_DIV_RESET2
), RTC_FREQ_SELECT
);
45 cmos_minutes
= CMOS_READ(RTC_MINUTES
);
46 if (!(save_control
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
)
47 BCD_TO_BIN(cmos_minutes
);
50 * since we're only adjusting minutes and seconds,
51 * don't interfere with hour overflow. This avoids
52 * messing with unknown time zones but requires your
53 * RTC not to be off by more than 15 minutes
55 real_seconds
= nowtime
% 60;
56 real_minutes
= nowtime
/ 60;
57 if (((abs(real_minutes
- cmos_minutes
) + 15)/30) & 1)
58 real_minutes
+= 30; /* correct for half hour time zone */
61 if (abs(real_minutes
- cmos_minutes
) < 30) {
62 if (!(save_control
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
) {
63 BIN_TO_BCD(real_seconds
);
64 BIN_TO_BCD(real_minutes
);
66 CMOS_WRITE(real_seconds
,RTC_SECONDS
);
67 CMOS_WRITE(real_minutes
,RTC_MINUTES
);
70 "set_rtc_mmss: can't update from %d to %d\n",
71 cmos_minutes
, real_minutes
);
75 /* The following flags have to be released exactly in this order,
76 * otherwise the DS12887 (popular MC146818A clone with integrated
77 * battery and quartz) will not reset the oscillator and will not
78 * update precisely 500 ms later. You won't find this mentioned in
79 * the Dallas Semiconductor data sheets, but who believes data
80 * sheets anyway ... -- Markus Kuhn
82 CMOS_WRITE(save_control
, RTC_CONTROL
);
83 CMOS_WRITE(save_freq_select
, RTC_FREQ_SELECT
);
84 spin_unlock_irqrestore(&rtc_lock
, flags
);
90 * Returns true if a clock update is in progress
92 static inline unsigned char rtc_is_updating(void)
97 spin_lock_irqsave(&rtc_lock
, flags
);
98 uip
= (CMOS_READ(RTC_FREQ_SELECT
) & RTC_UIP
);
99 spin_unlock_irqrestore(&rtc_lock
, flags
);
103 static inline unsigned long mc146818_get_cmos_time(void)
105 unsigned int year
, mon
, day
, hour
, min
, sec
;
110 * The Linux interpretation of the CMOS clock register contents:
111 * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
112 * RTC registers show the second which has precisely just started.
113 * Let's hope other operating systems interpret the RTC the same way.
116 /* read RTC exactly on falling edge of update flag */
117 for (i
= 0 ; i
< 1000000 ; i
++) /* may take up to 1 second... */
118 if (rtc_is_updating())
120 for (i
= 0 ; i
< 1000000 ; i
++) /* must try at least 2.228 ms */
121 if (!rtc_is_updating())
124 spin_lock_irqsave(&rtc_lock
, flags
);
125 do { /* Isn't this overkill ? UIP above should guarantee consistency */
126 sec
= CMOS_READ(RTC_SECONDS
);
127 min
= CMOS_READ(RTC_MINUTES
);
128 hour
= CMOS_READ(RTC_HOURS
);
129 day
= CMOS_READ(RTC_DAY_OF_MONTH
);
130 mon
= CMOS_READ(RTC_MONTH
);
131 year
= CMOS_READ(RTC_YEAR
);
132 } while (sec
!= CMOS_READ(RTC_SECONDS
));
134 if (!(CMOS_READ(RTC_CONTROL
) & RTC_DM_BINARY
) || RTC_ALWAYS_BCD
) {
142 spin_unlock_irqrestore(&rtc_lock
, flags
);
143 year
= mc146818_decode_year(year
);
145 return mktime(year
, mon
, day
, hour
, min
, sec
);
148 #endif /* __ASM_MC146818_TIME_H */