blob: 76aab46e59a054e289373ab51f324ef91413ec12 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022unsigned long get_cmos_time(void)
23{
24 unsigned int year, mon, day, hour, min, sec;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26 spin_lock(&rtc_lock);
Matt Mackall356a9ce2006-03-28 01:56:08 -080027
28 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 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));
Matt Mackall356a9ce2006-03-28 01:56:08 -080036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
38 {
39 BCD_TO_BIN(sec);
40 BCD_TO_BIN(min);
41 BCD_TO_BIN(hour);
42 BCD_TO_BIN(day);
43 BCD_TO_BIN(mon);
44 BCD_TO_BIN(year);
45 }
46 spin_unlock(&rtc_lock);
47 if ((year += 1900) < 1970)
48 year += 100;
49 return mktime(year, mon, day, hour, min, sec);
50}
51
52void mpc1211_rtc_gettimeofday(struct timeval *tv)
53{
54
55 tv->tv_sec = get_cmos_time();
56 tv->tv_usec = 0;
57}
58
59/* arc/i386/kernel/time.c */
60/*
61 * In order to set the CMOS clock precisely, set_rtc_mmss has to be
62 * called 500 ms after the second nowtime has started, because when
63 * nowtime is written into the registers of the CMOS clock, it will
64 * jump to the next second precisely 500 ms later. Check the Motorola
65 * MC146818A or Dallas DS12887 data sheet for details.
66 *
67 * BUG: This routine does not handle hour overflow properly; it just
68 * sets the minutes. Usually you'll only notice that after reboot!
69 */
70static int set_rtc_mmss(unsigned long nowtime)
71{
72 int retval = 0;
73 int real_seconds, real_minutes, cmos_minutes;
74 unsigned char save_control, save_freq_select;
75
76 /* gets recalled with irq locally disabled */
77 spin_lock(&rtc_lock);
78 save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
79 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
80
81 save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
82 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
83
84 cmos_minutes = CMOS_READ(RTC_MINUTES);
85 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
86 BCD_TO_BIN(cmos_minutes);
87
88 /*
89 * since we're only adjusting minutes and seconds,
90 * don't interfere with hour overflow. This avoids
91 * messing with unknown time zones but requires your
92 * RTC not to be off by more than 15 minutes
93 */
94 real_seconds = nowtime % 60;
95 real_minutes = nowtime / 60;
96 if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
97 real_minutes += 30; /* correct for half hour time zone */
98 real_minutes %= 60;
99
100 if (abs(real_minutes - cmos_minutes) < 30) {
101 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
102 BIN_TO_BCD(real_seconds);
103 BIN_TO_BCD(real_minutes);
104 }
105 CMOS_WRITE(real_seconds,RTC_SECONDS);
106 CMOS_WRITE(real_minutes,RTC_MINUTES);
107 } else {
108 printk(KERN_WARNING
109 "set_rtc_mmss: can't update from %d to %d\n",
110 cmos_minutes, real_minutes);
111 retval = -1;
112 }
113
114 /* The following flags have to be released exactly in this order,
115 * otherwise the DS12887 (popular MC146818A clone with integrated
116 * battery and quartz) will not reset the oscillator and will not
117 * update precisely 500 ms later. You won't find this mentioned in
118 * the Dallas Semiconductor data sheets, but who believes data
119 * sheets anyway ... -- Markus Kuhn
120 */
121 CMOS_WRITE(save_control, RTC_CONTROL);
122 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
123 spin_unlock(&rtc_lock);
124
125 return retval;
126}
127
128int mpc1211_rtc_settimeofday(const struct timeval *tv)
129{
130 unsigned long nowtime = tv->tv_sec;
131
132 return set_rtc_mmss(nowtime);
133}
134
135void mpc1211_time_init(void)
136{
137 rtc_get_time = mpc1211_rtc_gettimeofday;
138 rtc_set_time = mpc1211_rtc_settimeofday;
139}
140