blob: a2c2d2c2430329050b1bde7bf6b2d43f8cc5d1e0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
4 * for more details.
5 *
6 * Machine dependent access functions for RTC registers.
7 */
8#ifndef __ASM_MC146818_TIME_H
9#define __ASM_MC146818_TIME_H
10
11#include <linux/bcd.h>
12#include <linux/mc146818rtc.h>
13#include <linux/time.h>
14
15/*
16 * For check timing call set_rtc_mmss() 500ms; used in timer interrupt.
17 */
18#define USEC_AFTER 500000
19#define USEC_BEFORE 500000
20
21/*
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.
27 *
28 * BUG: This routine does not handle hour overflow properly; it just
29 * sets the minutes. Usually you'll only notice that after reboot!
30 */
31static inline int mc146818_set_rtc_mmss(unsigned long nowtime)
32{
33 int real_seconds, real_minutes, cmos_minutes;
34 unsigned char save_control, save_freq_select;
35 int retval = 0;
36
37 save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
38 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
39
40 save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
41 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
42
43 cmos_minutes = CMOS_READ(RTC_MINUTES);
44 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
45 BCD_TO_BIN(cmos_minutes);
46
47 /*
48 * since we're only adjusting minutes and seconds,
49 * don't interfere with hour overflow. This avoids
50 * messing with unknown time zones but requires your
51 * RTC not to be off by more than 15 minutes
52 */
53 real_seconds = nowtime % 60;
54 real_minutes = nowtime / 60;
55 if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
56 real_minutes += 30; /* correct for half hour time zone */
57 real_minutes %= 60;
58
59 if (abs(real_minutes - cmos_minutes) < 30) {
60 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
61 BIN_TO_BCD(real_seconds);
62 BIN_TO_BCD(real_minutes);
63 }
64 CMOS_WRITE(real_seconds,RTC_SECONDS);
65 CMOS_WRITE(real_minutes,RTC_MINUTES);
66 } else {
67 printk(KERN_WARNING
68 "set_rtc_mmss: can't update from %d to %d\n",
69 cmos_minutes, real_minutes);
70 retval = -1;
71 }
72
73 /* The following flags have to be released exactly in this order,
74 * otherwise the DS12887 (popular MC146818A clone with integrated
75 * battery and quartz) will not reset the oscillator and will not
76 * update precisely 500 ms later. You won't find this mentioned in
77 * the Dallas Semiconductor data sheets, but who believes data
78 * sheets anyway ... -- Markus Kuhn
79 */
80 CMOS_WRITE(save_control, RTC_CONTROL);
81 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
82
83 return retval;
84}
85
86static inline unsigned long mc146818_get_cmos_time(void)
87{
88 unsigned int year, mon, day, hour, min, sec;
89 int i;
90
91 /*
92 * The Linux interpretation of the CMOS clock register contents:
93 * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
94 * RTC registers show the second which has precisely just started.
95 * Let's hope other operating systems interpret the RTC the same way.
96 */
97
98 /* read RTC exactly on falling edge of update flag */
99 for (i = 0 ; i < 1000000 ; i++) /* may take up to 1 second... */
100 if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
101 break;
102 for (i = 0 ; i < 1000000 ; i++) /* must try at least 2.228 ms */
103 if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
104 break;
105
106 do { /* Isn't this overkill ? UIP above should guarantee consistency */
107 sec = CMOS_READ(RTC_SECONDS);
108 min = CMOS_READ(RTC_MINUTES);
109 hour = CMOS_READ(RTC_HOURS);
110 day = CMOS_READ(RTC_DAY_OF_MONTH);
111 mon = CMOS_READ(RTC_MONTH);
112 year = CMOS_READ(RTC_YEAR);
113 } while (sec != CMOS_READ(RTC_SECONDS));
114
115 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
116 BCD_TO_BIN(sec);
117 BCD_TO_BIN(min);
118 BCD_TO_BIN(hour);
119 BCD_TO_BIN(day);
120 BCD_TO_BIN(mon);
121 BCD_TO_BIN(year);
122 }
123 year = mc146818_decode_year(year);
124
125 return mktime(year, mon, day, hour, min, sec);
126}
127
128#endif /* __ASM_MC146818_TIME_H */