Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / arch / sh / boards / sh03 / rtc.c
1 /*
2  * linux/arch/sh/boards/sh03/rtc.c -- CTP/PCI-SH03 on-chip RTC support
3  *
4  *  Copyright (C) 2004  Saito.K & Jeanne(ksaito@interface.co.jp)
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/bcd.h>
13 #include <asm/io.h>
14 #include <linux/rtc.h>
15 #include <linux/spinlock.h>
16
17 #define RTC_BASE        0xb0000000
18 #define RTC_SEC1        (RTC_BASE + 0)
19 #define RTC_SEC10       (RTC_BASE + 1)
20 #define RTC_MIN1        (RTC_BASE + 2)
21 #define RTC_MIN10       (RTC_BASE + 3)
22 #define RTC_HOU1        (RTC_BASE + 4)
23 #define RTC_HOU10       (RTC_BASE + 5)
24 #define RTC_WEE1        (RTC_BASE + 6)
25 #define RTC_DAY1        (RTC_BASE + 7)
26 #define RTC_DAY10       (RTC_BASE + 8)
27 #define RTC_MON1        (RTC_BASE + 9)
28 #define RTC_MON10       (RTC_BASE + 10)
29 #define RTC_YEA1        (RTC_BASE + 11)
30 #define RTC_YEA10       (RTC_BASE + 12)
31 #define RTC_YEA100      (RTC_BASE + 13)
32 #define RTC_YEA1000     (RTC_BASE + 14)
33 #define RTC_CTL         (RTC_BASE + 15)
34 #define RTC_BUSY        1
35 #define RTC_STOP        2
36
37 extern void (*rtc_get_time)(struct timespec *);
38 extern int (*rtc_set_time)(const time_t);
39 extern spinlock_t rtc_lock;
40
41 unsigned long get_cmos_time(void)
42 {
43         unsigned int year, mon, day, hour, min, sec;
44
45         spin_lock(&rtc_lock);
46  again:
47         do {
48                 sec  = (ctrl_inb(RTC_SEC1) & 0xf) + (ctrl_inb(RTC_SEC10) & 0x7) * 10;
49                 min  = (ctrl_inb(RTC_MIN1) & 0xf) + (ctrl_inb(RTC_MIN10) & 0xf) * 10;
50                 hour = (ctrl_inb(RTC_HOU1) & 0xf) + (ctrl_inb(RTC_HOU10) & 0xf) * 10;
51                 day  = (ctrl_inb(RTC_DAY1) & 0xf) + (ctrl_inb(RTC_DAY10) & 0xf) * 10;
52                 mon  = (ctrl_inb(RTC_MON1) & 0xf) + (ctrl_inb(RTC_MON10) & 0xf) * 10;
53                 year = (ctrl_inb(RTC_YEA1) & 0xf) + (ctrl_inb(RTC_YEA10) & 0xf) * 10
54                      + (ctrl_inb(RTC_YEA100 ) & 0xf) * 100
55                      + (ctrl_inb(RTC_YEA1000) & 0xf) * 1000;
56         } while (sec != (ctrl_inb(RTC_SEC1) & 0xf) + (ctrl_inb(RTC_SEC10) & 0x7) * 10);
57         if (year == 0 || mon < 1 || mon > 12 || day > 31 || day < 1 ||
58             hour > 23 || min > 59 || sec > 59) {
59                 printk(KERN_ERR
60                        "SH-03 RTC: invalid value, resetting to 1 Jan 2000\n");
61                 printk("year=%d, mon=%d, day=%d, hour=%d, min=%d, sec=%d\n",
62                        year, mon, day, hour, min, sec);
63
64                 ctrl_outb(0, RTC_SEC1); ctrl_outb(0, RTC_SEC10);
65                 ctrl_outb(0, RTC_MIN1); ctrl_outb(0, RTC_MIN10);
66                 ctrl_outb(0, RTC_HOU1); ctrl_outb(0, RTC_HOU10);
67                 ctrl_outb(6, RTC_WEE1);
68                 ctrl_outb(1, RTC_DAY1); ctrl_outb(0, RTC_DAY10);
69                 ctrl_outb(1, RTC_MON1); ctrl_outb(0, RTC_MON10);
70                 ctrl_outb(0, RTC_YEA1); ctrl_outb(0, RTC_YEA10);
71                 ctrl_outb(0, RTC_YEA100);
72                 ctrl_outb(2, RTC_YEA1000);
73                 ctrl_outb(0, RTC_CTL);
74                 goto again;
75         }
76
77         spin_unlock(&rtc_lock);
78         return mktime(year, mon, day, hour, min, sec);
79 }
80
81 void sh03_rtc_gettimeofday(struct timespec *tv)
82 {
83
84         tv->tv_sec = get_cmos_time();
85         tv->tv_nsec = 0;
86 }
87
88 static int set_rtc_mmss(unsigned long nowtime)
89 {
90         int retval = 0;
91         int real_seconds, real_minutes, cmos_minutes;
92         int i;
93
94         /* gets recalled with irq locally disabled */
95         spin_lock(&rtc_lock);
96         for (i = 0 ; i < 1000000 ; i++) /* may take up to 1 second... */
97                 if (!(ctrl_inb(RTC_CTL) & RTC_BUSY))
98                         break;
99         cmos_minutes = (ctrl_inb(RTC_MIN1) & 0xf) + (ctrl_inb(RTC_MIN10) & 0xf) * 10;
100         real_seconds = nowtime % 60;
101         real_minutes = nowtime / 60;
102         if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
103                 real_minutes += 30;             /* correct for half hour time zone */
104         real_minutes %= 60;
105
106         if (abs(real_minutes - cmos_minutes) < 30) {
107                 ctrl_outb(real_seconds % 10, RTC_SEC1);
108                 ctrl_outb(real_seconds / 10, RTC_SEC10);
109                 ctrl_outb(real_minutes % 10, RTC_MIN1);
110                 ctrl_outb(real_minutes / 10, RTC_MIN10);
111         } else {
112                 printk(KERN_WARNING
113                        "set_rtc_mmss: can't update from %d to %d\n",
114                        cmos_minutes, real_minutes);
115                 retval = -1;
116         }
117         spin_unlock(&rtc_lock);
118
119         return retval;
120 }
121
122 int sh03_rtc_settimeofday(const time_t secs)
123 {
124         unsigned long nowtime = secs;
125
126         return set_rtc_mmss(nowtime);
127 }
128
129 void sh03_time_init(void)
130 {
131         rtc_get_time = sh03_rtc_gettimeofday;
132         rtc_set_time = sh03_rtc_settimeofday;
133 }