wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2003 |
| 3 | * David Müller ELSOFT AG Switzerland. d.mueller@elsoft.ch |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * Date & Time support for the built-in Samsung S3C24X0 RTC |
| 26 | */ |
| 27 | |
| 28 | #include <common.h> |
| 29 | #include <command.h> |
| 30 | |
Michal Simek | c3e6c55 | 2008-07-14 19:45:37 +0200 | [diff] [blame] | 31 | #if (defined(CONFIG_CMD_DATE)) |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 32 | |
| 33 | #if defined(CONFIG_S3C2400) |
kevin.morfitt@fearnside-systems.co.uk | 1e01705 | 2009-11-03 18:08:41 +0900 | [diff] [blame] | 34 | #include <asm/arch/s3c2400.h> |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 35 | #elif defined(CONFIG_S3C2410) |
kevin.morfitt@fearnside-systems.co.uk | 1e01705 | 2009-11-03 18:08:41 +0900 | [diff] [blame] | 36 | #include <asm/arch/s3c2410.h> |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 37 | #endif |
| 38 | |
| 39 | #include <rtc.h> |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 40 | #include <asm/io.h> |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 41 | |
| 42 | /*#define DEBUG*/ |
| 43 | |
| 44 | typedef enum { |
| 45 | RTC_ENABLE, |
| 46 | RTC_DISABLE |
| 47 | } RTC_ACCESS; |
| 48 | |
| 49 | |
| 50 | static inline void SetRTC_Access(RTC_ACCESS a) |
| 51 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 52 | struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc(); |
| 53 | |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 54 | switch (a) { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 55 | case RTC_ENABLE: |
| 56 | writeb(readb(&rtc->RTCCON) | 0x01, &rtc->RTCCON); |
| 57 | break; |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 58 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 59 | case RTC_DISABLE: |
| 60 | writeb(readb(&rtc->RTCCON) & ~0x01, &rtc->RTCCON); |
| 61 | break; |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 65 | /* ------------------------------------------------------------------------- */ |
| 66 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 67 | int rtc_get(struct rtc_time *tmp) |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 68 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 69 | struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc(); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 70 | uchar sec, min, hour, mday, wday, mon, year; |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 71 | uchar a_sec, a_min, a_hour, a_date, a_mon, a_year, a_armed; |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 72 | |
| 73 | /* enable access to RTC registers */ |
| 74 | SetRTC_Access(RTC_ENABLE); |
| 75 | |
| 76 | /* read RTC registers */ |
wdenk | 2552190 | 2003-09-13 19:01:12 +0000 | [diff] [blame] | 77 | do { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 78 | sec = readb(&rtc->BCDSEC); |
| 79 | min = readb(&rtc->BCDMIN); |
| 80 | hour = readb(&rtc->BCDHOUR); |
| 81 | mday = readb(&rtc->BCDDATE); |
| 82 | wday = readb(&rtc->BCDDAY); |
| 83 | mon = readb(&rtc->BCDMON); |
| 84 | year = readb(&rtc->BCDYEAR); |
| 85 | } while (sec != readb(&rtc->BCDSEC)); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 86 | |
| 87 | /* read ALARM registers */ |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 88 | a_sec = readb(&rtc->ALMSEC); |
| 89 | a_min = readb(&rtc->ALMMIN); |
| 90 | a_hour = readb(&rtc->ALMHOUR); |
| 91 | a_date = readb(&rtc->ALMDATE); |
| 92 | a_mon = readb(&rtc->ALMMON); |
| 93 | a_year = readb(&rtc->ALMYEAR); |
| 94 | a_armed = readb(&rtc->RTCALM); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 95 | |
| 96 | /* disable access to RTC registers */ |
| 97 | SetRTC_Access(RTC_DISABLE); |
| 98 | |
| 99 | #ifdef RTC_DEBUG |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 100 | printf("Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x " |
| 101 | "hr: %02x min: %02x sec: %02x\n", |
| 102 | year, mon, mday, wday, hour, min, sec); |
| 103 | printf("Alarms: %02x: year: %02x month: %02x date: %02x hour: " |
| 104 | "%02x min: %02x sec: %02x\n", |
| 105 | a_armed, a_year, a_mon, a_date, a_hour, a_min, a_sec); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 106 | #endif |
| 107 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 108 | tmp->tm_sec = bcd2bin(sec & 0x7F); |
| 109 | tmp->tm_min = bcd2bin(min & 0x7F); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 110 | tmp->tm_hour = bcd2bin(hour & 0x3F); |
| 111 | tmp->tm_mday = bcd2bin(mday & 0x3F); |
| 112 | tmp->tm_mon = bcd2bin(mon & 0x1F); |
| 113 | tmp->tm_year = bcd2bin(year); |
| 114 | tmp->tm_wday = bcd2bin(wday & 0x07); |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 115 | if (tmp->tm_year < 70) |
| 116 | tmp->tm_year += 2000; |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 117 | else |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 118 | tmp->tm_year += 1900; |
| 119 | tmp->tm_yday = 0; |
| 120 | tmp->tm_isdst = 0; |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 121 | #ifdef RTC_DEBUG |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 122 | printf("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 123 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 124 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 125 | #endif |
Yuri Tikhonov | 9bacd94 | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 126 | |
| 127 | return 0; |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 128 | } |
| 129 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 130 | int rtc_set(struct rtc_time *tmp) |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 131 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 132 | struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc(); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 133 | uchar sec, min, hour, mday, wday, mon, year; |
| 134 | |
| 135 | #ifdef RTC_DEBUG |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 136 | printf("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 137 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 138 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 139 | #endif |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 140 | year = bin2bcd(tmp->tm_year % 100); |
| 141 | mon = bin2bcd(tmp->tm_mon); |
| 142 | wday = bin2bcd(tmp->tm_wday); |
| 143 | mday = bin2bcd(tmp->tm_mday); |
| 144 | hour = bin2bcd(tmp->tm_hour); |
| 145 | min = bin2bcd(tmp->tm_min); |
| 146 | sec = bin2bcd(tmp->tm_sec); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 147 | |
| 148 | /* enable access to RTC registers */ |
| 149 | SetRTC_Access(RTC_ENABLE); |
| 150 | |
| 151 | /* write RTC registers */ |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 152 | writeb(sec, &rtc->BCDSEC); |
| 153 | writeb(min, &rtc->BCDMIN); |
| 154 | writeb(hour, &rtc->BCDHOUR); |
| 155 | writeb(mday, &rtc->BCDDATE); |
| 156 | writeb(wday, &rtc->BCDDAY); |
| 157 | writeb(mon, &rtc->BCDMON); |
| 158 | writeb(year, &rtc->BCDYEAR); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 159 | |
| 160 | /* disable access to RTC registers */ |
| 161 | SetRTC_Access(RTC_DISABLE); |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 162 | |
| 163 | return 0; |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 164 | } |
| 165 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 166 | void rtc_reset(void) |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 167 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 168 | struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc(); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 169 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 170 | writeb((readb(&rtc->RTCCON) & ~0x06) | 0x08, &rtc->RTCCON); |
| 171 | writeb(readb(&rtc->RTCCON) & ~(0x08 | 0x01), &rtc->RTCCON); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Jon Loeliger | 07efe2a | 2007-07-10 10:27:39 -0500 | [diff] [blame] | 174 | #endif |