Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Ralf Baechle | c32cf78 | 2005-11-15 13:01:50 +0000 | [diff] [blame] | 44 | #include <linux/ds1742rtc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | #include <asm/time.h> |
| 47 | #include <asm/addrspace.h> |
| 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Atsushi Nemoto | 53c2df2 | 2005-11-03 01:01:15 +0900 | [diff] [blame] | 60 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Atsushi Nemoto | 53c2df2 | 2005-11-03 01:01:15 +0900 | [diff] [blame] | 62 | spin_lock_irqsave(&rtc_lock, flags); |
Ralf Baechle | ba8990f | 2006-04-03 00:21:30 +0100 | [diff] [blame] | 63 | rtc_write(RTC_READ, RTC_CONTROL); |
| 64 | second = BCD2BIN(rtc_read(RTC_SECONDS) & RTC_SECONDS_MASK); |
| 65 | minute = BCD2BIN(rtc_read(RTC_MINUTES)); |
| 66 | hour = BCD2BIN(rtc_read(RTC_HOURS)); |
| 67 | day = BCD2BIN(rtc_read(RTC_DATE)); |
| 68 | month = BCD2BIN(rtc_read(RTC_MONTH)); |
| 69 | year = BCD2BIN(rtc_read(RTC_YEAR)); |
| 70 | century = BCD2BIN(rtc_read(RTC_CENTURY) & RTC_CENTURY_MASK); |
| 71 | rtc_write(0, RTC_CONTROL); |
Atsushi Nemoto | 53c2df2 | 2005-11-03 01:01:15 +0900 | [diff] [blame] | 72 | spin_unlock_irqrestore(&rtc_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | year += century * 100; |
| 75 | |
| 76 | return mktime(year, month, day, hour, minute, second); |
| 77 | } |
| 78 | extern void to_tm(unsigned long tim, struct rtc_time * tm); |
| 79 | |
| 80 | static int |
| 81 | rtc_ds1742_set_time(unsigned long t) |
| 82 | { |
| 83 | struct rtc_time tm; |
| 84 | u8 year, month, day, hour, minute, second; |
| 85 | u8 cmos_year, cmos_month, cmos_day, cmos_hour, cmos_minute, cmos_second; |
| 86 | int cmos_century; |
Atsushi Nemoto | 53c2df2 | 2005-11-03 01:01:15 +0900 | [diff] [blame] | 87 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Atsushi Nemoto | 53c2df2 | 2005-11-03 01:01:15 +0900 | [diff] [blame] | 89 | spin_lock_irqsave(&rtc_lock, flags); |
Ralf Baechle | ba8990f | 2006-04-03 00:21:30 +0100 | [diff] [blame] | 90 | rtc_write(RTC_READ, RTC_CONTROL); |
| 91 | cmos_second = (u8)(rtc_read(RTC_SECONDS) & RTC_SECONDS_MASK); |
| 92 | cmos_minute = (u8)rtc_read(RTC_MINUTES); |
| 93 | cmos_hour = (u8)rtc_read(RTC_HOURS); |
| 94 | cmos_day = (u8)rtc_read(RTC_DATE); |
| 95 | cmos_month = (u8)rtc_read(RTC_MONTH); |
| 96 | cmos_year = (u8)rtc_read(RTC_YEAR); |
| 97 | cmos_century = rtc_read(RTC_CENTURY) & RTC_CENTURY_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Ralf Baechle | ba8990f | 2006-04-03 00:21:30 +0100 | [diff] [blame] | 99 | rtc_write(RTC_WRITE, RTC_CONTROL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
| 101 | /* convert */ |
| 102 | to_tm(t, &tm); |
| 103 | |
| 104 | /* check each field one by one */ |
| 105 | year = BIN2BCD(tm.tm_year - EPOCH); |
| 106 | if (year != cmos_year) { |
Ralf Baechle | ba8990f | 2006-04-03 00:21:30 +0100 | [diff] [blame] | 107 | rtc_write(year,RTC_YEAR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | month = BIN2BCD(tm.tm_mon); |
| 111 | if (month != (cmos_month & 0x1f)) { |
Ralf Baechle | ba8990f | 2006-04-03 00:21:30 +0100 | [diff] [blame] | 112 | rtc_write((month & 0x1f) | (cmos_month & ~0x1f),RTC_MONTH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | day = BIN2BCD(tm.tm_mday); |
| 116 | if (day != cmos_day) { |
| 117 | |
Ralf Baechle | ba8990f | 2006-04-03 00:21:30 +0100 | [diff] [blame] | 118 | rtc_write(day, RTC_DATE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | if (cmos_hour & 0x40) { |
| 122 | /* 12 hour format */ |
| 123 | hour = 0x40; |
| 124 | if (tm.tm_hour > 12) { |
| 125 | hour |= 0x20 | (BIN2BCD(hour-12) & 0x1f); |
| 126 | } else { |
| 127 | hour |= BIN2BCD(tm.tm_hour); |
| 128 | } |
| 129 | } else { |
| 130 | /* 24 hour format */ |
| 131 | hour = BIN2BCD(tm.tm_hour) & 0x3f; |
| 132 | } |
Ralf Baechle | ba8990f | 2006-04-03 00:21:30 +0100 | [diff] [blame] | 133 | if (hour != cmos_hour) rtc_write(hour, RTC_HOURS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
| 135 | minute = BIN2BCD(tm.tm_min); |
| 136 | if (minute != cmos_minute) { |
Ralf Baechle | ba8990f | 2006-04-03 00:21:30 +0100 | [diff] [blame] | 137 | rtc_write(minute, RTC_MINUTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | second = BIN2BCD(tm.tm_sec); |
| 141 | if (second != cmos_second) { |
Ralf Baechle | ba8990f | 2006-04-03 00:21:30 +0100 | [diff] [blame] | 142 | rtc_write(second & RTC_SECONDS_MASK,RTC_SECONDS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* RTC_CENTURY and RTC_CONTROL share same address... */ |
Ralf Baechle | ba8990f | 2006-04-03 00:21:30 +0100 | [diff] [blame] | 146 | rtc_write(cmos_century, RTC_CONTROL); |
Atsushi Nemoto | 53c2df2 | 2005-11-03 01:01:15 +0900 | [diff] [blame] | 147 | spin_unlock_irqrestore(&rtc_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | void |
| 153 | rtc_ds1742_init(unsigned long base) |
| 154 | { |
| 155 | u8 cmos_second; |
| 156 | |
| 157 | /* remember the base */ |
| 158 | rtc_base = base; |
| 159 | db_assert((rtc_base & 0xe0000000) == KSEG1); |
| 160 | |
| 161 | /* set the function pointers */ |
Yoichi Yuasa | d23ee8f | 2006-03-27 01:16:33 -0800 | [diff] [blame] | 162 | rtc_mips_get_time = rtc_ds1742_get_time; |
| 163 | rtc_mips_set_time = rtc_ds1742_set_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | /* clear oscillator stop bit */ |
Ralf Baechle | ba8990f | 2006-04-03 00:21:30 +0100 | [diff] [blame] | 166 | rtc_write(RTC_READ, RTC_CONTROL); |
| 167 | cmos_second = (u8)(rtc_read(RTC_SECONDS) & RTC_SECONDS_MASK); |
| 168 | rtc_write(RTC_WRITE, RTC_CONTROL); |
| 169 | rtc_write(cmos_second, RTC_SECONDS); /* clear msb */ |
| 170 | rtc_write(0, RTC_CONTROL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |