blob: 9f306d196152892a5a976e3d192260421a12461f [file] [log] [blame]
wdenk3bac3512003-03-12 10:41:04 +00001/*
2 * (C) Copyright 2002
3 * ARIO Data Networks, Inc. dchiu@ariodata.com
wdenk8bde7f72003-06-27 21:31:46 +00004 *
wdenk3bac3512003-03-12 10:41:04 +00005 * modified for DS164x:
6 * The LEOX team <team@leox.org>, http://www.leox.org
7 *
8 * Based on MontaVista DS1743 code and U-Boot mc146818 code
9 *
10 * See file CREDITS for list of people who contributed to this
11 * project.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 * MA 02111-1307 USA
27 */
28
29/*
30 * Date & Time support for the DS164x RTC
31 */
32
33/* #define RTC_DEBUG */
34
35#include <common.h>
36#include <command.h>
37#include <rtc.h>
38
39
Michal Simek871c18d2008-07-14 19:45:37 +020040#if defined(CONFIG_CMD_DATE)
wdenk3bac3512003-03-12 10:41:04 +000041
42static uchar rtc_read(unsigned int addr );
43static void rtc_write(unsigned int addr, uchar val);
wdenk3bac3512003-03-12 10:41:04 +000044
45#define RTC_EPOCH 2000 /* century */
46
47/*
48 * DS164x registers layout
49 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020050#define RTC_BASE ( CONFIG_SYS_NVRAM_BASE_ADDR + CONFIG_SYS_NVRAM_SIZE )
wdenk3bac3512003-03-12 10:41:04 +000051
52#define RTC_YEAR ( RTC_BASE + 0x07 )
53#define RTC_MONTH ( RTC_BASE + 0x06 )
54#define RTC_DAY_OF_MONTH ( RTC_BASE + 0x05 )
55#define RTC_DAY_OF_WEEK ( RTC_BASE + 0x04 )
56#define RTC_HOURS ( RTC_BASE + 0x03 )
57#define RTC_MINUTES ( RTC_BASE + 0x02 )
58#define RTC_SECONDS ( RTC_BASE + 0x01 )
59#define RTC_CONTROL ( RTC_BASE + 0x00 )
60
61#define RTC_CONTROLA RTC_CONTROL /* W=bit6, R=bit5 */
62#define RTC_CA_WRITE 0x80
63#define RTC_CA_READ 0x40
64#define RTC_CONTROLB RTC_SECONDS /* OSC=bit7 */
65#define RTC_CB_OSC_DISABLE 0x80
66#define RTC_CONTROLC RTC_DAY_OF_WEEK /* FT=bit6 */
67#define RTC_CC_FREQ_TEST 0x40
68
69/* ------------------------------------------------------------------------- */
70
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +030071int rtc_get( struct rtc_time *tmp )
wdenk3bac3512003-03-12 10:41:04 +000072{
73 uchar sec, min, hour;
74 uchar mday, wday, mon, year;
75
76 uchar reg_a;
77
78 reg_a = rtc_read( RTC_CONTROLA );
79 /* lock clock registers for read */
80 rtc_write( RTC_CONTROLA, ( reg_a | RTC_CA_READ ));
81
82 sec = rtc_read( RTC_SECONDS );
83 min = rtc_read( RTC_MINUTES );
84 hour = rtc_read( RTC_HOURS );
85 mday = rtc_read( RTC_DAY_OF_MONTH );
86 wday = rtc_read( RTC_DAY_OF_WEEK );
87 mon = rtc_read( RTC_MONTH );
88 year = rtc_read( RTC_YEAR );
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: %02x mday: %02x wday: %02x "
95 "hr: %02x min: %02x sec: %02x\n",
96 year, mon, 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 in century (2000) */
107 tmp->tm_year = bcd2bin( year ) + RTC_EPOCH;
108
109 tmp->tm_yday = 0;
110 tmp->tm_isdst= 0;
111#ifdef RTC_DEBUG
112 printf( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
113 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
114 tmp->tm_hour, tmp->tm_min, tmp->tm_sec );
115#endif
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +0300116
117 return 0;
wdenk3bac3512003-03-12 10:41:04 +0000118}
119
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200120int rtc_set( struct rtc_time *tmp )
wdenk3bac3512003-03-12 10:41:04 +0000121{
122 uchar reg_a;
123
124#ifdef RTC_DEBUG
125 printf( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
126 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
127 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
128#endif
129 /* lock clock registers for write */
130 reg_a = rtc_read( RTC_CONTROLA );
131 rtc_write( RTC_CONTROLA, ( reg_a | RTC_CA_WRITE ));
132
133 rtc_write( RTC_MONTH, bin2bcd( tmp->tm_mon ));
134
135 rtc_write( RTC_DAY_OF_WEEK, bin2bcd( tmp->tm_wday ));
136 rtc_write( RTC_DAY_OF_MONTH, bin2bcd( tmp->tm_mday ));
137 rtc_write( RTC_HOURS, bin2bcd( tmp->tm_hour ));
138 rtc_write( RTC_MINUTES, bin2bcd( tmp->tm_min ));
139 rtc_write( RTC_SECONDS, bin2bcd( tmp->tm_sec ));
140
141 /* break year in century */
142 rtc_write( RTC_YEAR, bin2bcd( tmp->tm_year % 100 ));
143
144 /* unlock clock registers after read */
145 rtc_write( RTC_CONTROLA, ( reg_a & ~RTC_CA_WRITE ));
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200146
147 return 0;
wdenk3bac3512003-03-12 10:41:04 +0000148}
149
150void rtc_reset (void)
151{
152 uchar reg_a, reg_b;
153
154 reg_a = rtc_read( RTC_CONTROLA );
155 reg_b = rtc_read( RTC_CONTROLB );
156
157 if ( reg_b & RTC_CB_OSC_DISABLE )
158 {
159 printf( "real-time-clock was stopped. Now starting...\n" );
160 reg_a |= RTC_CA_WRITE;
161 reg_b &= ~RTC_CB_OSC_DISABLE;
162
163 rtc_write( RTC_CONTROLA, reg_a );
164 rtc_write( RTC_CONTROLB, reg_b );
165 }
166
167 /* make sure read/write clock register bits are cleared */
168 reg_a &= ~( RTC_CA_WRITE | RTC_CA_READ );
169 rtc_write( RTC_CONTROLA, reg_a );
170}
171
172/* ------------------------------------------------------------------------- */
173
174static uchar rtc_read( unsigned int addr )
175{
176 uchar val = *(volatile unsigned char*)(addr);
177
178#ifdef RTC_DEBUG
179 printf( "rtc_read: %x:%x\n", addr, val );
180#endif
181 return( val );
182}
183
184static 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 *(volatile unsigned char*)(addr) = val;
190}
191
Jon Loeliger068b60a2007-07-10 10:27:39 -0500192#endif