3 * ARIO Data Networks, Inc. dchiu@ariodata.com
5 * Based on MontaVista DS1743 code and U-Boot mc146818 code
7 * See file CREDITS for list of people who contributed to this
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.
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.
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,
27 * Date & Time support for the DS174x RTC
36 #if defined(CONFIG_CMD_DATE)
38 static uchar rtc_read( unsigned int addr );
39 static void rtc_write( unsigned int addr, uchar val);
41 #define RTC_BASE ( CONFIG_SYS_NVRAM_SIZE + CONFIG_SYS_NVRAM_BASE_ADDR )
43 #define RTC_YEAR ( RTC_BASE + 7 )
44 #define RTC_MONTH ( RTC_BASE + 6 )
45 #define RTC_DAY_OF_MONTH ( RTC_BASE + 5 )
46 #define RTC_DAY_OF_WEEK ( RTC_BASE + 4 )
47 #define RTC_HOURS ( RTC_BASE + 3 )
48 #define RTC_MINUTES ( RTC_BASE + 2 )
49 #define RTC_SECONDS ( RTC_BASE + 1 )
50 #define RTC_CENTURY ( RTC_BASE + 0 )
52 #define RTC_CONTROLA RTC_CENTURY
53 #define RTC_CONTROLB RTC_SECONDS
54 #define RTC_CONTROLC RTC_DAY_OF_WEEK
56 #define RTC_CA_WRITE 0x80
57 #define RTC_CA_READ 0x40
59 #define RTC_CB_OSC_DISABLE 0x80
61 #define RTC_CC_BATTERY_FLAG 0x80
62 #define RTC_CC_FREQ_TEST 0x40
64 /* ------------------------------------------------------------------------- */
66 int rtc_get( struct rtc_time *tmp )
69 uchar mday, wday, mon, year;
75 reg_a = rtc_read( RTC_CONTROLA );
76 /* lock clock registers for read */
77 rtc_write( RTC_CONTROLA, ( reg_a | RTC_CA_READ ));
79 sec = rtc_read( RTC_SECONDS );
80 min = rtc_read( RTC_MINUTES );
81 hour = rtc_read( RTC_HOURS );
82 mday = rtc_read( RTC_DAY_OF_MONTH );
83 wday = rtc_read( RTC_DAY_OF_WEEK );
84 mon = rtc_read( RTC_MONTH );
85 year = rtc_read( RTC_YEAR );
86 century = rtc_read( RTC_CENTURY );
88 /* unlock clock registers after read */
89 rtc_write( RTC_CONTROLA, ( reg_a & ~RTC_CA_READ ));
92 printf( "Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
93 "hr: %02x min: %02x sec: %02x\n",
94 year, mon_cent, mday, wday,
97 tmp->tm_sec = bcd2bin( sec & 0x7F );
98 tmp->tm_min = bcd2bin( min & 0x7F );
99 tmp->tm_hour = bcd2bin( hour & 0x3F );
100 tmp->tm_mday = bcd2bin( mday & 0x3F );
101 tmp->tm_mon = bcd2bin( mon & 0x1F );
102 tmp->tm_wday = bcd2bin( wday & 0x07 );
104 /* glue year from century and year in century */
105 tmp->tm_year = bcd2bin( year ) +
106 ( bcd2bin( century & 0x3F ) * 100 );
111 printf( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
112 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
113 tmp->tm_hour, tmp->tm_min, tmp->tm_sec );
118 int rtc_set( struct rtc_time *tmp )
122 printf( "Set 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);
126 /* lock clock registers for write */
127 reg_a = rtc_read( RTC_CONTROLA );
128 rtc_write( RTC_CONTROLA, ( reg_a | RTC_CA_WRITE ));
130 rtc_write( RTC_MONTH, bin2bcd( tmp->tm_mon ));
132 rtc_write( RTC_DAY_OF_WEEK, bin2bcd( tmp->tm_wday ));
133 rtc_write( RTC_DAY_OF_MONTH, bin2bcd( tmp->tm_mday ));
134 rtc_write( RTC_HOURS, bin2bcd( tmp->tm_hour ));
135 rtc_write( RTC_MINUTES, bin2bcd( tmp->tm_min ));
136 rtc_write( RTC_SECONDS, bin2bcd( tmp->tm_sec ));
138 /* break year up into century and year in century */
139 rtc_write( RTC_YEAR, bin2bcd( tmp->tm_year % 100 ));
140 rtc_write( RTC_CENTURY, bin2bcd( tmp->tm_year / 100 ));
142 /* unlock clock registers after read */
143 rtc_write( RTC_CONTROLA, ( reg_a & ~RTC_CA_WRITE ));
148 void rtc_reset (void)
150 uchar reg_a, reg_b, reg_c;
152 reg_a = rtc_read( RTC_CONTROLA );
153 reg_b = rtc_read( RTC_CONTROLB );
155 if ( reg_b & RTC_CB_OSC_DISABLE )
157 printf( "real-time-clock was stopped. Now starting...\n" );
158 reg_a |= RTC_CA_WRITE;
159 reg_b &= ~RTC_CB_OSC_DISABLE;
161 rtc_write( RTC_CONTROLA, reg_a );
162 rtc_write( RTC_CONTROLB, reg_b );
165 /* make sure read/write clock register bits are cleared */
166 reg_a &= ~( RTC_CA_WRITE | RTC_CA_READ );
167 rtc_write( RTC_CONTROLA, reg_a );
169 reg_c = rtc_read( RTC_CONTROLC );
170 if (( reg_c & RTC_CC_BATTERY_FLAG ) == 0 )
171 printf( "RTC battery low. Clock setting may not be reliable.\n" );
174 /* ------------------------------------------------------------------------- */
176 static uchar rtc_read( unsigned int addr )
178 uchar val = in8( addr );
180 printf( "rtc_read: %x:%x\n", addr, val );
185 static void rtc_write( unsigned int addr, uchar val )
188 printf( "rtc_write: %x:%x\n", addr, val );