wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * ARIO Data Networks, Inc. dchiu@ariodata.com |
| 4 | * |
| 5 | * Based on MontaVista DS1743 code and U-Boot mc146818 code |
| 6 | * |
| 7 | * See file CREDITS for list of people who contributed to this |
| 8 | * project. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 of |
| 13 | * the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 23 | * MA 02111-1307 USA |
| 24 | */ |
| 25 | |
| 26 | /* |
| 27 | * Date & Time support for the DS174x RTC |
| 28 | */ |
| 29 | |
| 30 | /*#define DEBUG*/ |
| 31 | |
| 32 | #include <common.h> |
| 33 | #include <command.h> |
| 34 | #include <rtc.h> |
| 35 | |
Jon Loeliger | a593814 | 2007-07-09 18:10:50 -0500 | [diff] [blame] | 36 | #if defined(CONFIG_RTC_DS174x) && defined(CONFIG_CMD_DATE) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 37 | |
| 38 | static uchar rtc_read( unsigned int addr ); |
| 39 | static void rtc_write( unsigned int addr, uchar val); |
| 40 | static uchar bin2bcd (unsigned int n); |
| 41 | static unsigned bcd2bin(uchar c); |
| 42 | |
| 43 | #define RTC_BASE ( CFG_NVRAM_SIZE + CFG_NVRAM_BASE_ADDR ) |
| 44 | |
| 45 | #define RTC_YEAR ( RTC_BASE + 7 ) |
| 46 | #define RTC_MONTH ( RTC_BASE + 6 ) |
| 47 | #define RTC_DAY_OF_MONTH ( RTC_BASE + 5 ) |
| 48 | #define RTC_DAY_OF_WEEK ( RTC_BASE + 4 ) |
| 49 | #define RTC_HOURS ( RTC_BASE + 3 ) |
| 50 | #define RTC_MINUTES ( RTC_BASE + 2 ) |
| 51 | #define RTC_SECONDS ( RTC_BASE + 1 ) |
| 52 | #define RTC_CENTURY ( RTC_BASE + 0 ) |
| 53 | |
| 54 | #define RTC_CONTROLA RTC_CENTURY |
| 55 | #define RTC_CONTROLB RTC_SECONDS |
| 56 | #define RTC_CONTROLC RTC_DAY_OF_WEEK |
| 57 | |
| 58 | #define RTC_CA_WRITE 0x80 |
| 59 | #define RTC_CA_READ 0x40 |
| 60 | |
| 61 | #define RTC_CB_OSC_DISABLE 0x80 |
| 62 | |
| 63 | #define RTC_CC_BATTERY_FLAG 0x80 |
| 64 | #define RTC_CC_FREQ_TEST 0x40 |
| 65 | |
| 66 | /* ------------------------------------------------------------------------- */ |
| 67 | |
| 68 | void rtc_get( struct rtc_time *tmp ) |
| 69 | { |
| 70 | uchar sec, min, hour; |
| 71 | uchar mday, wday, mon, year; |
| 72 | |
| 73 | int century; |
| 74 | |
| 75 | uchar reg_a; |
| 76 | |
| 77 | reg_a = rtc_read( RTC_CONTROLA ); |
| 78 | /* lock clock registers for read */ |
| 79 | rtc_write( RTC_CONTROLA, ( reg_a | RTC_CA_READ )); |
| 80 | |
| 81 | sec = rtc_read( RTC_SECONDS ); |
| 82 | min = rtc_read( RTC_MINUTES ); |
| 83 | hour = rtc_read( RTC_HOURS ); |
| 84 | mday = rtc_read( RTC_DAY_OF_MONTH ); |
| 85 | wday = rtc_read( RTC_DAY_OF_WEEK ); |
| 86 | mon = rtc_read( RTC_MONTH ); |
| 87 | year = rtc_read( RTC_YEAR ); |
| 88 | century = rtc_read( RTC_CENTURY ); |
| 89 | |
| 90 | /* unlock clock registers after read */ |
| 91 | rtc_write( RTC_CONTROLA, ( reg_a & ~RTC_CA_READ )); |
| 92 | |
| 93 | #ifdef RTC_DEBUG |
| 94 | printf( "Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x " |
| 95 | "hr: %02x min: %02x sec: %02x\n", |
| 96 | year, mon_cent, mday, wday, |
| 97 | hour, min, sec ); |
| 98 | #endif |
| 99 | tmp->tm_sec = bcd2bin( sec & 0x7F ); |
| 100 | tmp->tm_min = bcd2bin( min & 0x7F ); |
| 101 | tmp->tm_hour = bcd2bin( hour & 0x3F ); |
| 102 | tmp->tm_mday = bcd2bin( mday & 0x3F ); |
| 103 | tmp->tm_mon = bcd2bin( mon & 0x1F ); |
| 104 | tmp->tm_wday = bcd2bin( wday & 0x07 ); |
| 105 | |
| 106 | /* glue year from century and year in century */ |
| 107 | tmp->tm_year = bcd2bin( year ) + |
| 108 | ( bcd2bin( century & 0x3F ) * 100 ); |
| 109 | |
| 110 | tmp->tm_yday = 0; |
| 111 | tmp->tm_isdst= 0; |
| 112 | #ifdef RTC_DEBUG |
| 113 | printf( "Get 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 ); |
| 116 | #endif |
| 117 | } |
| 118 | |
| 119 | void rtc_set( struct rtc_time *tmp ) |
| 120 | { |
| 121 | uchar reg_a; |
| 122 | #ifdef RTC_DEBUG |
| 123 | printf( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 124 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 125 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
| 126 | #endif |
| 127 | /* lock clock registers for write */ |
| 128 | reg_a = rtc_read( RTC_CONTROLA ); |
| 129 | rtc_write( RTC_CONTROLA, ( reg_a | RTC_CA_WRITE )); |
| 130 | |
| 131 | rtc_write( RTC_MONTH, bin2bcd( tmp->tm_mon )); |
| 132 | |
| 133 | rtc_write( RTC_DAY_OF_WEEK, bin2bcd( tmp->tm_wday )); |
| 134 | rtc_write( RTC_DAY_OF_MONTH, bin2bcd( tmp->tm_mday )); |
| 135 | rtc_write( RTC_HOURS, bin2bcd( tmp->tm_hour )); |
| 136 | rtc_write( RTC_MINUTES, bin2bcd( tmp->tm_min )); |
| 137 | rtc_write( RTC_SECONDS, bin2bcd( tmp->tm_sec )); |
| 138 | |
| 139 | /* break year up into century and year in century */ |
| 140 | rtc_write( RTC_YEAR, bin2bcd( tmp->tm_year % 100 )); |
| 141 | rtc_write( RTC_CENTURY, bin2bcd( tmp->tm_year / 100 )); |
| 142 | |
| 143 | /* unlock clock registers after read */ |
| 144 | rtc_write( RTC_CONTROLA, ( reg_a & ~RTC_CA_WRITE )); |
| 145 | } |
| 146 | |
| 147 | void rtc_reset (void) |
| 148 | { |
| 149 | uchar reg_a, reg_b, reg_c; |
| 150 | |
| 151 | reg_a = rtc_read( RTC_CONTROLA ); |
| 152 | reg_b = rtc_read( RTC_CONTROLB ); |
| 153 | |
| 154 | if ( reg_b & RTC_CB_OSC_DISABLE ) |
| 155 | { |
| 156 | printf( "real-time-clock was stopped. Now starting...\n" ); |
| 157 | reg_a |= RTC_CA_WRITE; |
| 158 | reg_b &= ~RTC_CB_OSC_DISABLE; |
| 159 | |
| 160 | rtc_write( RTC_CONTROLA, reg_a ); |
| 161 | rtc_write( RTC_CONTROLB, reg_b ); |
| 162 | } |
| 163 | |
| 164 | /* make sure read/write clock register bits are cleared */ |
| 165 | reg_a &= ~( RTC_CA_WRITE | RTC_CA_READ ); |
| 166 | rtc_write( RTC_CONTROLA, reg_a ); |
| 167 | |
| 168 | reg_c = rtc_read( RTC_CONTROLC ); |
| 169 | if (( reg_c & RTC_CC_BATTERY_FLAG ) == 0 ) |
| 170 | printf( "RTC battery low. Clock setting may not be reliable.\n" ); |
| 171 | } |
| 172 | |
| 173 | /* ------------------------------------------------------------------------- */ |
| 174 | |
| 175 | static uchar rtc_read( unsigned int addr ) |
| 176 | { |
| 177 | uchar val = in8( addr ); |
| 178 | #ifdef RTC_DEBUG |
| 179 | printf( "rtc_read: %x:%x\n", addr, val ); |
| 180 | #endif |
| 181 | return( val ); |
| 182 | } |
| 183 | |
| 184 | static void rtc_write( unsigned int addr, uchar val ) |
| 185 | { |
| 186 | #ifdef RTC_DEBUG |
| 187 | printf( "rtc_write: %x:%x\n", addr, val ); |
| 188 | #endif |
| 189 | out8( addr, val ); |
| 190 | } |
| 191 | |
| 192 | static unsigned bcd2bin (uchar n) |
| 193 | { |
| 194 | return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F)); |
| 195 | } |
| 196 | |
| 197 | static unsigned char bin2bcd (unsigned int n) |
| 198 | { |
| 199 | return (((n / 10) << 4) | (n % 10)); |
| 200 | } |
| 201 | |
Jon Loeliger | 068b60a | 2007-07-10 10:27:39 -0500 | [diff] [blame] | 202 | #endif |