3 * ARIO Data Networks, Inc. dchiu@ariodata.com
6 * The LEOX team <team@leox.org>, http://www.leox.org
8 * Based on MontaVista DS1743 code and U-Boot mc146818 code
10 * See file CREDITS for list of people who contributed to this
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.
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.
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,
30 * Date & Time support for the DS164x RTC
33 /* #define RTC_DEBUG */
40 #if defined(CONFIG_RTC_DS164x) && defined(CONFIG_CMD_DATE)
42 static uchar
rtc_read(unsigned int addr
);
43 static void rtc_write(unsigned int addr
, uchar val
);
44 static uchar
bin2bcd(unsigned int n
);
45 static unsigned bcd2bin(uchar c
);
47 #define RTC_EPOCH 2000 /* century */
50 * DS164x registers layout
52 #define RTC_BASE ( CFG_NVRAM_BASE_ADDR + CFG_NVRAM_SIZE )
54 #define RTC_YEAR ( RTC_BASE + 0x07 )
55 #define RTC_MONTH ( RTC_BASE + 0x06 )
56 #define RTC_DAY_OF_MONTH ( RTC_BASE + 0x05 )
57 #define RTC_DAY_OF_WEEK ( RTC_BASE + 0x04 )
58 #define RTC_HOURS ( RTC_BASE + 0x03 )
59 #define RTC_MINUTES ( RTC_BASE + 0x02 )
60 #define RTC_SECONDS ( RTC_BASE + 0x01 )
61 #define RTC_CONTROL ( RTC_BASE + 0x00 )
63 #define RTC_CONTROLA RTC_CONTROL /* W=bit6, R=bit5 */
64 #define RTC_CA_WRITE 0x80
65 #define RTC_CA_READ 0x40
66 #define RTC_CONTROLB RTC_SECONDS /* OSC=bit7 */
67 #define RTC_CB_OSC_DISABLE 0x80
68 #define RTC_CONTROLC RTC_DAY_OF_WEEK /* FT=bit6 */
69 #define RTC_CC_FREQ_TEST 0x40
71 /* ------------------------------------------------------------------------- */
73 int rtc_get( struct rtc_time
*tmp
)
76 uchar mday
, wday
, mon
, year
;
80 reg_a
= rtc_read( RTC_CONTROLA
);
81 /* lock clock registers for read */
82 rtc_write( RTC_CONTROLA
, ( reg_a
| RTC_CA_READ
));
84 sec
= rtc_read( RTC_SECONDS
);
85 min
= rtc_read( RTC_MINUTES
);
86 hour
= rtc_read( RTC_HOURS
);
87 mday
= rtc_read( RTC_DAY_OF_MONTH
);
88 wday
= rtc_read( RTC_DAY_OF_WEEK
);
89 mon
= rtc_read( RTC_MONTH
);
90 year
= rtc_read( RTC_YEAR
);
92 /* unlock clock registers after read */
93 rtc_write( RTC_CONTROLA
, ( reg_a
& ~RTC_CA_READ
));
96 printf( "Get RTC year: %02x mon: %02x mday: %02x wday: %02x "
97 "hr: %02x min: %02x sec: %02x\n",
98 year
, mon
, mday
, wday
,
101 tmp
->tm_sec
= bcd2bin( sec
& 0x7F );
102 tmp
->tm_min
= bcd2bin( min
& 0x7F );
103 tmp
->tm_hour
= bcd2bin( hour
& 0x3F );
104 tmp
->tm_mday
= bcd2bin( mday
& 0x3F );
105 tmp
->tm_mon
= bcd2bin( mon
& 0x1F );
106 tmp
->tm_wday
= bcd2bin( wday
& 0x07 );
108 /* glue year in century (2000) */
109 tmp
->tm_year
= bcd2bin( year
) + RTC_EPOCH
;
114 printf( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
115 tmp
->tm_year
, tmp
->tm_mon
, tmp
->tm_mday
, tmp
->tm_wday
,
116 tmp
->tm_hour
, tmp
->tm_min
, tmp
->tm_sec
);
122 void rtc_set( struct rtc_time
*tmp
)
127 printf( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
128 tmp
->tm_year
, tmp
->tm_mon
, tmp
->tm_mday
, tmp
->tm_wday
,
129 tmp
->tm_hour
, tmp
->tm_min
, tmp
->tm_sec
);
131 /* lock clock registers for write */
132 reg_a
= rtc_read( RTC_CONTROLA
);
133 rtc_write( RTC_CONTROLA
, ( reg_a
| RTC_CA_WRITE
));
135 rtc_write( RTC_MONTH
, bin2bcd( tmp
->tm_mon
));
137 rtc_write( RTC_DAY_OF_WEEK
, bin2bcd( tmp
->tm_wday
));
138 rtc_write( RTC_DAY_OF_MONTH
, bin2bcd( tmp
->tm_mday
));
139 rtc_write( RTC_HOURS
, bin2bcd( tmp
->tm_hour
));
140 rtc_write( RTC_MINUTES
, bin2bcd( tmp
->tm_min
));
141 rtc_write( RTC_SECONDS
, bin2bcd( tmp
->tm_sec
));
143 /* break year in century */
144 rtc_write( RTC_YEAR
, bin2bcd( tmp
->tm_year
% 100 ));
146 /* unlock clock registers after read */
147 rtc_write( RTC_CONTROLA
, ( reg_a
& ~RTC_CA_WRITE
));
150 void rtc_reset (void)
154 reg_a
= rtc_read( RTC_CONTROLA
);
155 reg_b
= rtc_read( RTC_CONTROLB
);
157 if ( reg_b
& RTC_CB_OSC_DISABLE
)
159 printf( "real-time-clock was stopped. Now starting...\n" );
160 reg_a
|= RTC_CA_WRITE
;
161 reg_b
&= ~RTC_CB_OSC_DISABLE
;
163 rtc_write( RTC_CONTROLA
, reg_a
);
164 rtc_write( RTC_CONTROLB
, reg_b
);
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
);
172 /* ------------------------------------------------------------------------- */
174 static uchar
rtc_read( unsigned int addr
)
176 uchar val
= *(volatile unsigned char*)(addr
);
179 printf( "rtc_read: %x:%x\n", addr
, val
);
184 static void rtc_write( unsigned int addr
, uchar val
)
187 printf( "rtc_write: %x:%x\n", addr
, val
);
189 *(volatile unsigned char*)(addr
) = val
;
192 static unsigned bcd2bin (uchar n
)
194 return ((((n
>> 4) & 0x0F) * 10) + (n
& 0x0F));
197 static unsigned char bin2bcd (unsigned int n
)
199 return (((n
/ 10) << 4) | (n
% 10));