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