Merge branch 'intelfb-patches' of master.kernel.org:/pub/scm/linux/kernel/git/airlied...
[linux-2.6] / arch / sh / boards / landisk / rtc.c
1 /*
2  * arch/sh/boards/landisk/rtc.c --  RTC support
3  *
4  *  Copyright (C) 2000  Philipp Rumpf <prumpf@tux.org>
5  *  Copyright (C) 1999  Tetsuya Okada & Niibe Yutaka
6  */
7 /*
8  * modifed by kogiidena
9  * 2005.09.16
10  */
11
12 #include <linux/config.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/time.h>
17 #include <linux/delay.h>
18 #include <linux/spinlock.h>
19 #include <linux/bcd.h>
20 #include <asm/rtc.h>
21
22 extern spinlock_t rtc_lock;
23
24 extern void
25 rs5c313_set_cmos_time(unsigned int BCD_yr, unsigned int BCD_mon,
26                       unsigned int BCD_day, unsigned int BCD_hr,
27                       unsigned int BCD_min, unsigned int BCD_sec);
28
29 extern unsigned long
30 rs5c313_get_cmos_time(unsigned int *BCD_yr, unsigned int *BCD_mon,
31                       unsigned int *BCD_day, unsigned int *BCD_hr,
32                       unsigned int *BCD_min, unsigned int *BCD_sec);
33
34 void landisk_rtc_gettimeofday(struct timespec *tv)
35 {
36         unsigned int BCD_yr, BCD_mon, BCD_day, BCD_hr, BCD_min, BCD_sec;
37         unsigned long flags;
38
39         spin_lock_irqsave(&rtc_lock, flags);
40         tv->tv_sec = rs5c313_get_cmos_time
41             (&BCD_yr, &BCD_mon, &BCD_day, &BCD_hr, &BCD_min, &BCD_sec);
42         tv->tv_nsec = 0;
43         spin_unlock_irqrestore(&rtc_lock, flags);
44 }
45
46 int landisk_rtc_settimeofday(const time_t secs)
47 {
48         int retval = 0;
49         int real_seconds, real_minutes, cmos_minutes;
50         unsigned long flags;
51         unsigned long nowtime = secs;
52         unsigned int BCD_yr, BCD_mon, BCD_day, BCD_hr, BCD_min, BCD_sec;
53
54         spin_lock_irqsave(&rtc_lock, flags);
55
56         rs5c313_get_cmos_time
57           (&BCD_yr, &BCD_mon, &BCD_day, &BCD_hr, &BCD_min, &BCD_sec);
58         cmos_minutes = BCD_min;
59         BCD_TO_BIN(cmos_minutes);
60
61         /*
62          * since we're only adjusting minutes and seconds,
63          * don't interfere with hour overflow. This avoids
64          * messing with unknown time zones but requires your
65          * RTC not to be off by more than 15 minutes
66          */
67         real_seconds = nowtime % 60;
68         real_minutes = nowtime / 60;
69         if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1)
70                 real_minutes += 30;     /* correct for half hour time zone */
71         real_minutes %= 60;
72
73         if (abs(real_minutes - cmos_minutes) < 30) {
74                 BIN_TO_BCD(real_seconds);
75                 BIN_TO_BCD(real_minutes);
76                 rs5c313_set_cmos_time(BCD_yr, BCD_mon, BCD_day, BCD_hr,
77                                       real_minutes, real_seconds);
78         } else {
79                 printk(KERN_WARNING
80                        "set_rtc_time: can't update from %d to %d\n",
81                        cmos_minutes, real_minutes);
82                 retval = -1;
83         }
84
85         spin_unlock_irqrestore(&rtc_lock, flags);
86         return retval;
87 }
88
89 void landisk_time_init(void)
90 {
91         rtc_sh_get_time = landisk_rtc_gettimeofday;
92         rtc_sh_set_time = landisk_rtc_settimeofday;
93 }