Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / sh / boards / mpc1211 / rtc.c
blob4d100f0480723ed8d7382901bdb358519376f475
1 /*
2 * linux/arch/sh/kernel/rtc-mpc1211.c -- MPC-1211 on-chip RTC support
4 * Copyright (C) 2002 Saito.K & Jeanne
6 */
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>
14 #ifndef BCD_TO_BIN
15 #define BCD_TO_BIN(val) ((val)=((val)&15) + ((val)>>4)*10)
16 #endif
18 #ifndef BIN_TO_BCD
19 #define BIN_TO_BCD(val) ((val)=(((val)/10)<<4) + (val)%10)
20 #endif
22 /* arc/i386/kernel/time.c */
23 unsigned long get_cmos_time(void)
25 unsigned int year, mon, day, hour, min, sec;
26 int i;
28 spin_lock(&rtc_lock);
29 /* The Linux interpretation of the CMOS clock register contents:
30 * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
31 * RTC registers show the second which has precisely just started.
32 * Let's hope other operating systems interpret the RTC the same way.
34 /* read RTC exactly on falling edge of update flag */
35 for (i = 0 ; i < 1000000 ; i++) /* may take up to 1 second... */
36 if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
37 break;
38 for (i = 0 ; i < 1000000 ; i++) /* must try at least 2.228 ms */
39 if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
40 break;
41 do { /* Isn't this overkill ? UIP above should guarantee consistency */
42 sec = CMOS_READ(RTC_SECONDS);
43 min = CMOS_READ(RTC_MINUTES);
44 hour = CMOS_READ(RTC_HOURS);
45 day = CMOS_READ(RTC_DAY_OF_MONTH);
46 mon = CMOS_READ(RTC_MONTH);
47 year = CMOS_READ(RTC_YEAR);
48 } while (sec != CMOS_READ(RTC_SECONDS));
49 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
51 BCD_TO_BIN(sec);
52 BCD_TO_BIN(min);
53 BCD_TO_BIN(hour);
54 BCD_TO_BIN(day);
55 BCD_TO_BIN(mon);
56 BCD_TO_BIN(year);
58 spin_unlock(&rtc_lock);
59 if ((year += 1900) < 1970)
60 year += 100;
61 return mktime(year, mon, day, hour, min, sec);
64 void mpc1211_rtc_gettimeofday(struct timeval *tv)
67 tv->tv_sec = get_cmos_time();
68 tv->tv_usec = 0;
71 /* arc/i386/kernel/time.c */
73 * In order to set the CMOS clock precisely, set_rtc_mmss has to be
74 * called 500 ms after the second nowtime has started, because when
75 * nowtime is written into the registers of the CMOS clock, it will
76 * jump to the next second precisely 500 ms later. Check the Motorola
77 * MC146818A or Dallas DS12887 data sheet for details.
79 * BUG: This routine does not handle hour overflow properly; it just
80 * sets the minutes. Usually you'll only notice that after reboot!
82 static int set_rtc_mmss(unsigned long nowtime)
84 int retval = 0;
85 int real_seconds, real_minutes, cmos_minutes;
86 unsigned char save_control, save_freq_select;
88 /* gets recalled with irq locally disabled */
89 spin_lock(&rtc_lock);
90 save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
91 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
93 save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
94 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
96 cmos_minutes = CMOS_READ(RTC_MINUTES);
97 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
98 BCD_TO_BIN(cmos_minutes);
101 * since we're only adjusting minutes and seconds,
102 * don't interfere with hour overflow. This avoids
103 * messing with unknown time zones but requires your
104 * RTC not to be off by more than 15 minutes
106 real_seconds = nowtime % 60;
107 real_minutes = nowtime / 60;
108 if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
109 real_minutes += 30; /* correct for half hour time zone */
110 real_minutes %= 60;
112 if (abs(real_minutes - cmos_minutes) < 30) {
113 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
114 BIN_TO_BCD(real_seconds);
115 BIN_TO_BCD(real_minutes);
117 CMOS_WRITE(real_seconds,RTC_SECONDS);
118 CMOS_WRITE(real_minutes,RTC_MINUTES);
119 } else {
120 printk(KERN_WARNING
121 "set_rtc_mmss: can't update from %d to %d\n",
122 cmos_minutes, real_minutes);
123 retval = -1;
126 /* The following flags have to be released exactly in this order,
127 * otherwise the DS12887 (popular MC146818A clone with integrated
128 * battery and quartz) will not reset the oscillator and will not
129 * update precisely 500 ms later. You won't find this mentioned in
130 * the Dallas Semiconductor data sheets, but who believes data
131 * sheets anyway ... -- Markus Kuhn
133 CMOS_WRITE(save_control, RTC_CONTROL);
134 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
135 spin_unlock(&rtc_lock);
137 return retval;
140 int mpc1211_rtc_settimeofday(const struct timeval *tv)
142 unsigned long nowtime = tv->tv_sec;
144 return set_rtc_mmss(nowtime);
147 void mpc1211_time_init(void)
149 rtc_get_time = mpc1211_rtc_gettimeofday;
150 rtc_set_time = mpc1211_rtc_settimeofday;