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 unsigned long get_cmos_time(void)
24 unsigned int year, mon, day, hour, min, sec;
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));
37 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
46 spin_unlock(&rtc_lock);
52 return mktime(year, mon, day, hour, min, sec);
55 void mpc1211_rtc_gettimeofday(struct timeval *tv)
58 tv->tv_sec = get_cmos_time();
62 /* arc/i386/kernel/time.c */
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.
70 * BUG: This routine does not handle hour overflow properly; it just
71 * sets the minutes. Usually you'll only notice that after reboot!
73 static int set_rtc_mmss(unsigned long nowtime)
76 int real_seconds, real_minutes, cmos_minutes;
77 unsigned char save_control, save_freq_select;
79 /* gets recalled with irq locally disabled */
81 save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
82 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
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);
87 cmos_minutes = CMOS_READ(RTC_MINUTES);
88 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
89 BCD_TO_BIN(cmos_minutes);
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
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 */
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);
108 CMOS_WRITE(real_seconds,RTC_SECONDS);
109 CMOS_WRITE(real_minutes,RTC_MINUTES);
112 "set_rtc_mmss: can't update from %d to %d\n",
113 cmos_minutes, real_minutes);
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
124 CMOS_WRITE(save_control, RTC_CONTROL);
125 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
126 spin_unlock(&rtc_lock);
131 int mpc1211_rtc_settimeofday(const struct timeval *tv)
133 unsigned long nowtime = tv->tv_sec;
135 return set_rtc_mmss(nowtime);
138 void mpc1211_time_init(void)
140 rtc_get_time = mpc1211_rtc_gettimeofday;
141 rtc_set_time = mpc1211_rtc_settimeofday;