wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2003 |
Albert ARIBAUD | 60fbc8d | 2011-08-04 18:45:45 +0200 | [diff] [blame] | 3 | * David Müller ELSOFT AG Switzerland. d.mueller@elsoft.ch |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Date & Time support for the built-in Samsung S3C24X0 RTC |
| 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <command.h> |
| 14 | |
Michal Simek | c3e6c55 | 2008-07-14 19:45:37 +0200 | [diff] [blame] | 15 | #if (defined(CONFIG_CMD_DATE)) |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 16 | |
kevin.morfitt@fearnside-systems.co.uk | e0d8131 | 2009-11-17 18:30:34 +0900 | [diff] [blame] | 17 | #include <asm/arch/s3c24x0_cpu.h> |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 18 | |
| 19 | #include <rtc.h> |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 20 | #include <asm/io.h> |
Anatolij Gustschin | 49a358b | 2011-11-15 13:21:03 +0000 | [diff] [blame] | 21 | #include <linux/compiler.h> |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 22 | |
| 23 | typedef enum { |
| 24 | RTC_ENABLE, |
| 25 | RTC_DISABLE |
| 26 | } RTC_ACCESS; |
| 27 | |
| 28 | |
| 29 | static inline void SetRTC_Access(RTC_ACCESS a) |
| 30 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 31 | struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc(); |
| 32 | |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 33 | switch (a) { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 34 | case RTC_ENABLE: |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 35 | writeb(readb(&rtc->rtccon) | 0x01, &rtc->rtccon); |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 36 | break; |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 37 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 38 | case RTC_DISABLE: |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 39 | writeb(readb(&rtc->rtccon) & ~0x01, &rtc->rtccon); |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 40 | break; |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 44 | /* ------------------------------------------------------------------------- */ |
| 45 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 46 | int rtc_get(struct rtc_time *tmp) |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 47 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 48 | struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc(); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 49 | uchar sec, min, hour, mday, wday, mon, year; |
Anatolij Gustschin | 49a358b | 2011-11-15 13:21:03 +0000 | [diff] [blame] | 50 | __maybe_unused uchar a_sec, a_min, a_hour, a_date, |
| 51 | a_mon, a_year, a_armed; |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 52 | |
| 53 | /* enable access to RTC registers */ |
| 54 | SetRTC_Access(RTC_ENABLE); |
| 55 | |
| 56 | /* read RTC registers */ |
wdenk | 2552190 | 2003-09-13 19:01:12 +0000 | [diff] [blame] | 57 | do { |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 58 | sec = readb(&rtc->bcdsec); |
| 59 | min = readb(&rtc->bcdmin); |
| 60 | hour = readb(&rtc->bcdhour); |
| 61 | mday = readb(&rtc->bcddate); |
| 62 | wday = readb(&rtc->bcdday); |
| 63 | mon = readb(&rtc->bcdmon); |
| 64 | year = readb(&rtc->bcdyear); |
| 65 | } while (sec != readb(&rtc->bcdsec)); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 66 | |
| 67 | /* read ALARM registers */ |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 68 | a_sec = readb(&rtc->almsec); |
| 69 | a_min = readb(&rtc->almmin); |
| 70 | a_hour = readb(&rtc->almhour); |
| 71 | a_date = readb(&rtc->almdate); |
| 72 | a_mon = readb(&rtc->almmon); |
| 73 | a_year = readb(&rtc->almyear); |
| 74 | a_armed = readb(&rtc->rtcalm); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 75 | |
| 76 | /* disable access to RTC registers */ |
| 77 | SetRTC_Access(RTC_DISABLE); |
| 78 | |
| 79 | #ifdef RTC_DEBUG |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 80 | printf("Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x " |
| 81 | "hr: %02x min: %02x sec: %02x\n", |
| 82 | year, mon, mday, wday, hour, min, sec); |
| 83 | printf("Alarms: %02x: year: %02x month: %02x date: %02x hour: " |
| 84 | "%02x min: %02x sec: %02x\n", |
| 85 | 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] | 86 | #endif |
| 87 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 88 | tmp->tm_sec = bcd2bin(sec & 0x7F); |
| 89 | tmp->tm_min = bcd2bin(min & 0x7F); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 90 | tmp->tm_hour = bcd2bin(hour & 0x3F); |
| 91 | tmp->tm_mday = bcd2bin(mday & 0x3F); |
| 92 | tmp->tm_mon = bcd2bin(mon & 0x1F); |
| 93 | tmp->tm_year = bcd2bin(year); |
| 94 | tmp->tm_wday = bcd2bin(wday & 0x07); |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 95 | if (tmp->tm_year < 70) |
| 96 | tmp->tm_year += 2000; |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 97 | else |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 98 | tmp->tm_year += 1900; |
| 99 | tmp->tm_yday = 0; |
| 100 | tmp->tm_isdst = 0; |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 101 | #ifdef RTC_DEBUG |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 102 | printf("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 103 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 104 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 105 | #endif |
Yuri Tikhonov | 9bacd94 | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 106 | |
| 107 | return 0; |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 108 | } |
| 109 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 110 | int rtc_set(struct rtc_time *tmp) |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 111 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 112 | struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc(); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 113 | uchar sec, min, hour, mday, wday, mon, year; |
| 114 | |
| 115 | #ifdef RTC_DEBUG |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 116 | printf("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 117 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 118 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 119 | #endif |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 120 | year = bin2bcd(tmp->tm_year % 100); |
| 121 | mon = bin2bcd(tmp->tm_mon); |
| 122 | wday = bin2bcd(tmp->tm_wday); |
| 123 | mday = bin2bcd(tmp->tm_mday); |
| 124 | hour = bin2bcd(tmp->tm_hour); |
| 125 | min = bin2bcd(tmp->tm_min); |
| 126 | sec = bin2bcd(tmp->tm_sec); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 127 | |
| 128 | /* enable access to RTC registers */ |
| 129 | SetRTC_Access(RTC_ENABLE); |
| 130 | |
| 131 | /* write RTC registers */ |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 132 | writeb(sec, &rtc->bcdsec); |
| 133 | writeb(min, &rtc->bcdmin); |
| 134 | writeb(hour, &rtc->bcdhour); |
| 135 | writeb(mday, &rtc->bcddate); |
| 136 | writeb(wday, &rtc->bcdday); |
| 137 | writeb(mon, &rtc->bcdmon); |
| 138 | writeb(year, &rtc->bcdyear); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 139 | |
| 140 | /* disable access to RTC registers */ |
| 141 | SetRTC_Access(RTC_DISABLE); |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 142 | |
| 143 | return 0; |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 144 | } |
| 145 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 146 | void rtc_reset(void) |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 147 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 148 | struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc(); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 149 | |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 150 | writeb((readb(&rtc->rtccon) & ~0x06) | 0x08, &rtc->rtccon); |
| 151 | writeb(readb(&rtc->rtccon) & ~(0x08 | 0x01), &rtc->rtccon); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Jon Loeliger | 07efe2a | 2007-07-10 10:27:39 -0500 | [diff] [blame] | 154 | #endif |