]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/sh/boards/mpc1211/rtc.c
8ae2dc11d8e56a38147628d235811c43a660f70c
[linux-2.6-omap-h63xx.git] / arch / sh / boards / mpc1211 / rtc.c
1 /*
2  * linux/arch/sh/kernel/rtc-mpc1211.c -- MPC-1211 on-chip RTC support
3  *
4  *  Copyright (C) 2002  Saito.K & Jeanne
5  *
6  */
7
8 #include <linux/init.h>
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/time.h>
12 #include <linux/mc146818rtc.h>
13
14 #ifndef BCD_TO_BIN
15 #define BCD_TO_BIN(val) ((val)=((val)&15) + ((val)>>4)*10)
16 #endif
17
18 #ifndef BIN_TO_BCD
19 #define BIN_TO_BCD(val) ((val)=(((val)/10)<<4) + (val)%10)
20 #endif
21
22 unsigned long get_cmos_time(void)
23 {
24         unsigned int year, mon, day, hour, min, sec;
25
26         spin_lock(&rtc_lock);
27
28         do {
29                 sec = CMOS_READ(RTC_SECONDS);
30                 min = CMOS_READ(RTC_MINUTES);
31                 hour = CMOS_READ(RTC_HOURS);
32                 day = CMOS_READ(RTC_DAY_OF_MONTH);
33                 mon = CMOS_READ(RTC_MONTH);
34                 year = CMOS_READ(RTC_YEAR);
35         } while (sec != CMOS_READ(RTC_SECONDS));
36
37         if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
38                 BCD_TO_BIN(sec);
39                 BCD_TO_BIN(min);
40                 BCD_TO_BIN(hour);
41                 BCD_TO_BIN(day);
42                 BCD_TO_BIN(mon);
43                 BCD_TO_BIN(year);
44         }
45
46         spin_unlock(&rtc_lock);
47
48         year += 1900;
49         if (year < 1970)
50                 year += 100;
51
52         return mktime(year, mon, day, hour, min, sec);
53 }
54
55 void mpc1211_rtc_gettimeofday(struct timeval *tv)
56 {
57
58         tv->tv_sec = get_cmos_time();
59         tv->tv_usec = 0;
60 }
61
62 /* arc/i386/kernel/time.c */
63 /*
64  * In order to set the CMOS clock precisely, set_rtc_mmss has to be
65  * called 500 ms after the second nowtime has started, because when
66  * nowtime is written into the registers of the CMOS clock, it will
67  * jump to the next second precisely 500 ms later. Check the Motorola
68  * MC146818A or Dallas DS12887 data sheet for details.
69  *
70  * BUG: This routine does not handle hour overflow properly; it just
71  *      sets the minutes. Usually you'll only notice that after reboot!
72  */
73 static int set_rtc_mmss(unsigned long nowtime)
74 {
75         int retval = 0;
76         int real_seconds, real_minutes, cmos_minutes;
77         unsigned char save_control, save_freq_select;
78
79         /* gets recalled with irq locally disabled */
80         spin_lock(&rtc_lock);
81         save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
82         CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
83
84         save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
85         CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
86
87         cmos_minutes = CMOS_READ(RTC_MINUTES);
88         if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
89                 BCD_TO_BIN(cmos_minutes);
90
91         /*
92          * since we're only adjusting minutes and seconds,
93          * don't interfere with hour overflow. This avoids
94          * messing with unknown time zones but requires your
95          * RTC not to be off by more than 15 minutes
96          */
97         real_seconds = nowtime % 60;
98         real_minutes = nowtime / 60;
99         if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
100                 real_minutes += 30;             /* correct for half hour time zone */
101         real_minutes %= 60;
102
103         if (abs(real_minutes - cmos_minutes) < 30) {
104                 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
105                         BIN_TO_BCD(real_seconds);
106                         BIN_TO_BCD(real_minutes);
107                 }
108                 CMOS_WRITE(real_seconds,RTC_SECONDS);
109                 CMOS_WRITE(real_minutes,RTC_MINUTES);
110         } else {
111                 printk(KERN_WARNING
112                        "set_rtc_mmss: can't update from %d to %d\n",
113                        cmos_minutes, real_minutes);
114                 retval = -1;
115         }
116
117         /* The following flags have to be released exactly in this order,
118          * otherwise the DS12887 (popular MC146818A clone with integrated
119          * battery and quartz) will not reset the oscillator and will not
120          * update precisely 500 ms later. You won't find this mentioned in
121          * the Dallas Semiconductor data sheets, but who believes data
122          * sheets anyway ...                           -- Markus Kuhn
123          */
124         CMOS_WRITE(save_control, RTC_CONTROL);
125         CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
126         spin_unlock(&rtc_lock);
127
128         return retval;
129 }
130
131 int mpc1211_rtc_settimeofday(const struct timeval *tv)
132 {
133         unsigned long nowtime = tv->tv_sec;
134
135         return set_rtc_mmss(nowtime);
136 }
137
138 void mpc1211_time_init(void)
139 {
140         rtc_get_time = mpc1211_rtc_gettimeofday;
141         rtc_set_time = mpc1211_rtc_settimeofday;
142 }
143