ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / mips / jmr3927 / common / rtc_ds1742.c
1 /*
2  * Copyright 2001 MontaVista Software Inc.
3  * Author: MontaVista Software, Inc.
4  *              ahennessy@mvista.com
5  *
6  * arch/mips/jmr3927/common/rtc_ds1742.c
7  * Based on arch/mips/ddb5xxx/common/rtc_ds1386.c
8  *     low-level RTC hookups for s for Dallas 1742 chip.
9  *
10  * Copyright (C) 2000-2001 Toshiba Corporation
11  *
12  *  This program is free software; you can redistribute  it and/or modify it
13  *  under  the terms of  the GNU General  Public License as published by the
14  *  Free Software Foundation;  either version 2 of the  License, or (at your
15  *  option) any later version.
16  *
17  *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
18  *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
19  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
20  *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
21  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
23  *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
25  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  *  You should have received a copy of the  GNU General Public License along
29  *  with this program; if not, write  to the Free Software Foundation, Inc.,
30  *  675 Mass Ave, Cambridge, MA 02139, USA.
31  */
32
33
34 /*
35  * This file exports a function, rtc_ds1386_init(), which expects an
36  * uncached base address as the argument.  It will set the two function
37  * pointers expected by the MIPS generic timer code.
38  */
39
40 #include <linux/bcd.h>
41 #include <linux/types.h>
42 #include <linux/time.h>
43 #include <linux/rtc.h>
44
45 #include <asm/time.h>
46 #include <asm/addrspace.h>
47
48 #include <asm/jmr3927/ds1742rtc.h>
49 #include <asm/debug.h>
50
51 #define EPOCH           2000
52
53 static unsigned long rtc_base;
54
55 static unsigned long
56 rtc_ds1742_get_time(void)
57 {
58         unsigned int year, month, day, hour, minute, second;
59         unsigned int century;
60
61         CMOS_WRITE(RTC_READ, RTC_CONTROL);
62         second = BCD2BIN(CMOS_READ(RTC_SECONDS) & RTC_SECONDS_MASK);
63         minute = BCD2BIN(CMOS_READ(RTC_MINUTES));
64         hour = BCD2BIN(CMOS_READ(RTC_HOURS));
65         day = BCD2BIN(CMOS_READ(RTC_DATE));
66         month = BCD2BIN(CMOS_READ(RTC_MONTH));
67         year = BCD2BIN(CMOS_READ(RTC_YEAR));
68         century = BCD2BIN(CMOS_READ(RTC_CENTURY) & RTC_CENTURY_MASK);
69         CMOS_WRITE(0, RTC_CONTROL);
70
71         year += century * 100;
72
73         return mktime(year, month, day, hour, minute, second);
74 }
75 extern void to_tm(unsigned long tim, struct rtc_time * tm);
76
77 static int
78 rtc_ds1742_set_time(unsigned long t)
79 {
80         struct rtc_time tm;
81         u8 year, month, day, hour, minute, second;
82         u8 cmos_year, cmos_month, cmos_day, cmos_hour, cmos_minute, cmos_second;
83         int cmos_century;
84
85         CMOS_WRITE(RTC_READ, RTC_CONTROL);
86         cmos_second = (u8)(CMOS_READ(RTC_SECONDS) & RTC_SECONDS_MASK);
87         cmos_minute = (u8)CMOS_READ(RTC_MINUTES);
88         cmos_hour = (u8)CMOS_READ(RTC_HOURS);
89         cmos_day = (u8)CMOS_READ(RTC_DATE);
90         cmos_month = (u8)CMOS_READ(RTC_MONTH);
91         cmos_year = (u8)CMOS_READ(RTC_YEAR);
92         cmos_century = CMOS_READ(RTC_CENTURY) & RTC_CENTURY_MASK;
93
94         CMOS_WRITE(RTC_WRITE, RTC_CONTROL);
95
96         /* convert */
97         to_tm(t, &tm);
98
99         /* check each field one by one */
100         year = BIN2BCD(tm.tm_year - EPOCH);
101         if (year != cmos_year) {
102                 CMOS_WRITE(year,RTC_YEAR);
103         }
104
105         month = BIN2BCD(tm.tm_mon);
106         if (month != (cmos_month & 0x1f)) {
107                 CMOS_WRITE((month & 0x1f) | (cmos_month & ~0x1f),RTC_MONTH);
108         }
109
110         day = BIN2BCD(tm.tm_mday);
111         if (day != cmos_day) {
112
113                 CMOS_WRITE(day, RTC_DATE);
114         }
115
116         if (cmos_hour & 0x40) {
117                 /* 12 hour format */
118                 hour = 0x40;
119                 if (tm.tm_hour > 12) {
120                         hour |= 0x20 | (BIN2BCD(hour-12) & 0x1f);
121                 } else {
122                         hour |= BIN2BCD(tm.tm_hour);
123                 }
124         } else {
125                 /* 24 hour format */
126                 hour = BIN2BCD(tm.tm_hour) & 0x3f;
127         }
128         if (hour != cmos_hour) CMOS_WRITE(hour, RTC_HOURS);
129
130         minute = BIN2BCD(tm.tm_min);
131         if (minute !=  cmos_minute) {
132                 CMOS_WRITE(minute, RTC_MINUTES);
133         }
134
135         second = BIN2BCD(tm.tm_sec);
136         if (second !=  cmos_second) {
137                 CMOS_WRITE(second & RTC_SECONDS_MASK,RTC_SECONDS);
138         }
139
140         /* RTC_CENTURY and RTC_CONTROL share same address... */
141         CMOS_WRITE(cmos_century, RTC_CONTROL);
142
143         return 0;
144 }
145
146 void
147 rtc_ds1742_init(unsigned long base)
148 {
149         u8  cmos_second;
150
151         /* remember the base */
152         rtc_base = base;
153         db_assert((rtc_base & 0xe0000000) == KSEG1);
154
155         /* set the function pointers */
156         rtc_get_time = rtc_ds1742_get_time;
157         rtc_set_time = rtc_ds1742_set_time;
158
159         /* clear oscillator stop bit */
160         CMOS_WRITE(RTC_READ, RTC_CONTROL);
161         cmos_second = (u8)(CMOS_READ(RTC_SECONDS) & RTC_SECONDS_MASK);
162         CMOS_WRITE(RTC_WRITE, RTC_CONTROL);
163         CMOS_WRITE(cmos_second, RTC_SECONDS); /* clear msb */
164         CMOS_WRITE(0, RTC_CONTROL);
165 }