wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * ARIO Data Networks, Inc. dchiu@ariodata.com |
| 4 | * |
| 5 | * modified for DS1556: |
| 6 | * Frank Panno <fpanno@delphintech.com>, Delphin Technology AG |
| 7 | * |
| 8 | * Based on MontaVista DS1743 code and U-Boot mc146818 code |
| 9 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 10 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | /* |
| 14 | * Date & Time support for the DS1556 RTC |
| 15 | */ |
| 16 | |
| 17 | /*#define RTC_DEBUG */ |
| 18 | |
| 19 | #include <common.h> |
| 20 | #include <command.h> |
| 21 | #include <rtc.h> |
| 22 | |
Michal Simek | 871c18d | 2008-07-14 19:45:37 +0200 | [diff] [blame] | 23 | #if defined(CONFIG_CMD_DATE) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 24 | |
| 25 | static uchar rtc_read( unsigned int addr ); |
| 26 | static void rtc_write( unsigned int addr, uchar val); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 27 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 28 | #define RTC_BASE ( CONFIG_SYS_NVRAM_SIZE + CONFIG_SYS_NVRAM_BASE_ADDR ) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 29 | |
| 30 | #define RTC_YEAR ( RTC_BASE + 0xf ) |
| 31 | #define RTC_MONTH ( RTC_BASE + 0xe ) |
| 32 | #define RTC_DAY_OF_MONTH ( RTC_BASE + 0xd ) |
| 33 | #define RTC_DAY_OF_WEEK ( RTC_BASE + 0xc ) |
| 34 | #define RTC_HOURS ( RTC_BASE + 0xb ) |
| 35 | #define RTC_MINUTES ( RTC_BASE + 0xa ) |
| 36 | #define RTC_SECONDS ( RTC_BASE + 0x9 ) |
| 37 | #define RTC_CENTURY ( RTC_BASE + 0x8 ) |
| 38 | |
| 39 | #define RTC_CONTROLA RTC_CENTURY |
| 40 | #define RTC_CONTROLB RTC_SECONDS |
| 41 | #define RTC_CONTROLC RTC_BASE |
| 42 | |
| 43 | #define RTC_CA_WRITE 0x80 |
| 44 | #define RTC_CA_READ 0x40 |
| 45 | |
| 46 | #define RTC_CB_OSC_DISABLE 0x80 |
| 47 | |
| 48 | #define RTC_CC_BATTERY_FLAG 0x10 |
| 49 | #define RTC_CC_FREQ_TEST 0x40 |
| 50 | |
| 51 | /* ------------------------------------------------------------------------- */ |
| 52 | |
Yuri Tikhonov | b73a19e | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 53 | int rtc_get( struct rtc_time *tmp ) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 54 | { |
| 55 | uchar sec, min, hour; |
| 56 | uchar mday, wday, mon, year; |
| 57 | |
| 58 | int century; |
| 59 | |
| 60 | uchar reg_a; |
| 61 | |
| 62 | reg_a = rtc_read( RTC_CONTROLA ); |
| 63 | /* lock clock registers for read */ |
| 64 | rtc_write( RTC_CONTROLA, ( reg_a | RTC_CA_READ )); |
| 65 | |
| 66 | sec = rtc_read( RTC_SECONDS ); |
| 67 | min = rtc_read( RTC_MINUTES ); |
| 68 | hour = rtc_read( RTC_HOURS ); |
| 69 | mday = rtc_read( RTC_DAY_OF_MONTH ); |
| 70 | wday = rtc_read( RTC_DAY_OF_WEEK ); |
| 71 | mon = rtc_read( RTC_MONTH ); |
| 72 | year = rtc_read( RTC_YEAR ); |
| 73 | century = rtc_read( RTC_CENTURY ); |
| 74 | |
| 75 | /* unlock clock registers after read */ |
| 76 | rtc_write( RTC_CONTROLA, ( reg_a & ~RTC_CA_READ )); |
| 77 | |
| 78 | #ifdef RTC_DEBUG |
| 79 | printf( "Get RTC year: %02x mon/cent: %02x mon: %02x mday: %02x wday: %02x " |
| 80 | "hr: %02x min: %02x sec: %02x\n", |
| 81 | year, century, mon, mday, wday, |
| 82 | hour, min, sec ); |
| 83 | #endif |
| 84 | tmp->tm_sec = bcd2bin( sec & 0x7F ); |
| 85 | tmp->tm_min = bcd2bin( min & 0x7F ); |
| 86 | tmp->tm_hour = bcd2bin( hour & 0x3F ); |
| 87 | tmp->tm_mday = bcd2bin( mday & 0x3F ); |
| 88 | tmp->tm_mon = bcd2bin( mon & 0x1F ); |
| 89 | tmp->tm_wday = bcd2bin( wday & 0x07 ); |
| 90 | |
| 91 | /* glue year from century and year in century */ |
| 92 | tmp->tm_year = bcd2bin( year ) + |
| 93 | ( bcd2bin( century & 0x3F ) * 100 ); |
| 94 | |
| 95 | tmp->tm_yday = 0; |
| 96 | tmp->tm_isdst= 0; |
| 97 | #ifdef RTC_DEBUG |
| 98 | printf( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 99 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 100 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec ); |
| 101 | #endif |
Yuri Tikhonov | b73a19e | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 102 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Jean-Christophe PLAGNIOL-VILLARD | d1e2319 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 105 | int rtc_set( struct rtc_time *tmp ) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 106 | { |
| 107 | uchar reg_a; |
| 108 | #ifdef RTC_DEBUG |
| 109 | printf( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 110 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 111 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
| 112 | #endif |
| 113 | /* lock clock registers for write */ |
| 114 | reg_a = rtc_read( RTC_CONTROLA ); |
| 115 | rtc_write( RTC_CONTROLA, ( reg_a | RTC_CA_WRITE )); |
| 116 | |
| 117 | rtc_write( RTC_MONTH, bin2bcd( tmp->tm_mon )); |
| 118 | |
| 119 | rtc_write( RTC_DAY_OF_WEEK, bin2bcd( tmp->tm_wday )); |
| 120 | rtc_write( RTC_DAY_OF_MONTH, bin2bcd( tmp->tm_mday )); |
| 121 | rtc_write( RTC_HOURS, bin2bcd( tmp->tm_hour )); |
| 122 | rtc_write( RTC_MINUTES, bin2bcd( tmp->tm_min )); |
| 123 | rtc_write( RTC_SECONDS, bin2bcd( tmp->tm_sec )); |
| 124 | |
| 125 | /* break year up into century and year in century */ |
| 126 | rtc_write( RTC_YEAR, bin2bcd( tmp->tm_year % 100 )); |
| 127 | rtc_write( RTC_CENTURY, bin2bcd( tmp->tm_year / 100 )); |
| 128 | |
| 129 | /* unlock clock registers after read */ |
| 130 | rtc_write( RTC_CONTROLA, ( reg_a & ~RTC_CA_WRITE )); |
Jean-Christophe PLAGNIOL-VILLARD | d1e2319 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 131 | |
| 132 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void rtc_reset (void) |
| 136 | { |
| 137 | uchar reg_a, reg_b, reg_c; |
| 138 | |
| 139 | reg_a = rtc_read( RTC_CONTROLA ); |
| 140 | reg_b = rtc_read( RTC_CONTROLB ); |
| 141 | |
| 142 | if ( reg_b & RTC_CB_OSC_DISABLE ) |
| 143 | { |
| 144 | printf( "real-time-clock was stopped. Now starting...\n" ); |
| 145 | reg_a |= RTC_CA_WRITE; |
| 146 | reg_b &= ~RTC_CB_OSC_DISABLE; |
| 147 | |
| 148 | rtc_write( RTC_CONTROLA, reg_a ); |
| 149 | rtc_write( RTC_CONTROLB, reg_b ); |
| 150 | } |
| 151 | |
| 152 | /* make sure read/write clock register bits are cleared */ |
| 153 | reg_a &= ~( RTC_CA_WRITE | RTC_CA_READ ); |
| 154 | rtc_write( RTC_CONTROLA, reg_a ); |
| 155 | |
| 156 | reg_c = rtc_read( RTC_CONTROLC ); |
| 157 | if (( reg_c & RTC_CC_BATTERY_FLAG ) == 0 ) |
| 158 | printf( "RTC battery low. Clock setting may not be reliable.\n" ); |
| 159 | } |
| 160 | |
| 161 | /* ------------------------------------------------------------------------- */ |
| 162 | |
| 163 | static uchar rtc_read( unsigned int addr ) |
| 164 | { |
| 165 | uchar val = *(volatile unsigned char*)(addr); |
| 166 | #ifdef RTC_DEBUG |
| 167 | printf( "rtc_read: %x:%x\n", addr, val ); |
| 168 | #endif |
| 169 | return( val ); |
| 170 | } |
| 171 | |
| 172 | static void rtc_write( unsigned int addr, uchar val ) |
| 173 | { |
| 174 | #ifdef RTC_DEBUG |
| 175 | printf( "rtc_write: %x:%x\n", addr, val ); |
| 176 | #endif |
| 177 | *(volatile unsigned char*)(addr) = val; |
| 178 | } |
| 179 | |
Jon Loeliger | 068b60a | 2007-07-10 10:27:39 -0500 | [diff] [blame] | 180 | #endif |