2 * linux/arch/sh/kernel/rtc-mpc1211.c -- MPC-1211 on-chip RTC support
4 * Copyright (C) 2002 Saito.K & Jeanne
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>
15 #define BCD_TO_BIN(val) ((val)=((val)&15) + ((val)>>4)*10)
19 #define BIN_TO_BCD(val) ((val)=(((val)/10)<<4) + (val)%10)
22 /* arc/i386/kernel/time.c */
23 unsigned long get_cmos_time(void)
25 unsigned int year
, mon
, day
, hour
, min
, sec
;
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
)
38 for (i
= 0 ; i
< 1000000 ; i
++) /* must try at least 2.228 ms */
39 if (!(CMOS_READ(RTC_FREQ_SELECT
) & RTC_UIP
))
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
)
58 spin_unlock(&rtc_lock
);
59 if ((year
+= 1900) < 1970)
61 return mktime(year
, mon
, day
, hour
, min
, sec
);
64 void mpc1211_rtc_gettimeofday(struct timeval
*tv
)
67 tv
->tv_sec
= get_cmos_time();
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
)
85 int real_seconds
, real_minutes
, cmos_minutes
;
86 unsigned char save_control
, save_freq_select
;
88 /* gets recalled with irq locally disabled */
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 */
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
);
121 "set_rtc_mmss: can't update from %d to %d\n",
122 cmos_minutes
, real_minutes
);
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
);
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
;