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