]> git.ipfire.org Git - people/ms/u-boot.git/blame - drivers/rtc/ds1556.c
LWMON5: POST RTC fix
[people/ms/u-boot.git] / drivers / rtc / ds1556.c
CommitLineData
c609719b
WD
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 *
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 DS1556 RTC
31 */
32
33/*#define RTC_DEBUG */
34
35#include <common.h>
36#include <command.h>
37#include <rtc.h>
38
39
a593814f 40#if defined(CONFIG_RTC_DS1556) && defined(CONFIG_CMD_DATE)
c609719b
WD
41
42static uchar rtc_read( unsigned int addr );
43static void rtc_write( unsigned int addr, uchar val);
44static uchar bin2bcd (unsigned int n);
45static unsigned bcd2bin(uchar c);
46
47#define RTC_BASE ( CFG_NVRAM_SIZE + CFG_NVRAM_BASE_ADDR )
48
49#define RTC_YEAR ( RTC_BASE + 0xf )
50#define RTC_MONTH ( RTC_BASE + 0xe )
51#define RTC_DAY_OF_MONTH ( RTC_BASE + 0xd )
52#define RTC_DAY_OF_WEEK ( RTC_BASE + 0xc )
53#define RTC_HOURS ( RTC_BASE + 0xb )
54#define RTC_MINUTES ( RTC_BASE + 0xa )
55#define RTC_SECONDS ( RTC_BASE + 0x9 )
56#define RTC_CENTURY ( RTC_BASE + 0x8 )
57
58#define RTC_CONTROLA RTC_CENTURY
59#define RTC_CONTROLB RTC_SECONDS
60#define RTC_CONTROLC RTC_BASE
61
62#define RTC_CA_WRITE 0x80
63#define RTC_CA_READ 0x40
64
65#define RTC_CB_OSC_DISABLE 0x80
66
67#define RTC_CC_BATTERY_FLAG 0x10
68#define RTC_CC_FREQ_TEST 0x40
69
70/* ------------------------------------------------------------------------- */
71
b73a19e1 72int rtc_get( struct rtc_time *tmp )
c609719b
WD
73{
74 uchar sec, min, hour;
75 uchar mday, wday, mon, year;
76
77 int century;
78
79 uchar reg_a;
80
81 reg_a = rtc_read( RTC_CONTROLA );
82 /* lock clock registers for read */
83 rtc_write( RTC_CONTROLA, ( reg_a | RTC_CA_READ ));
84
85 sec = rtc_read( RTC_SECONDS );
86 min = rtc_read( RTC_MINUTES );
87 hour = rtc_read( RTC_HOURS );
88 mday = rtc_read( RTC_DAY_OF_MONTH );
89 wday = rtc_read( RTC_DAY_OF_WEEK );
90 mon = rtc_read( RTC_MONTH );
91 year = rtc_read( RTC_YEAR );
92 century = rtc_read( RTC_CENTURY );
93
94 /* unlock clock registers after read */
95 rtc_write( RTC_CONTROLA, ( reg_a & ~RTC_CA_READ ));
96
97#ifdef RTC_DEBUG
98 printf( "Get RTC year: %02x mon/cent: %02x mon: %02x mday: %02x wday: %02x "
99 "hr: %02x min: %02x sec: %02x\n",
100 year, century, mon, mday, wday,
101 hour, min, sec );
102#endif
103 tmp->tm_sec = bcd2bin( sec & 0x7F );
104 tmp->tm_min = bcd2bin( min & 0x7F );
105 tmp->tm_hour = bcd2bin( hour & 0x3F );
106 tmp->tm_mday = bcd2bin( mday & 0x3F );
107 tmp->tm_mon = bcd2bin( mon & 0x1F );
108 tmp->tm_wday = bcd2bin( wday & 0x07 );
109
110 /* glue year from century and year in century */
111 tmp->tm_year = bcd2bin( year ) +
112 ( bcd2bin( century & 0x3F ) * 100 );
113
114 tmp->tm_yday = 0;
115 tmp->tm_isdst= 0;
116#ifdef RTC_DEBUG
117 printf( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
118 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
119 tmp->tm_hour, tmp->tm_min, tmp->tm_sec );
120#endif
b73a19e1 121 return 0;
c609719b
WD
122}
123
124void rtc_set( struct rtc_time *tmp )
125{
126 uchar reg_a;
127#ifdef RTC_DEBUG
128 printf( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
129 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
130 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
131#endif
132 /* lock clock registers for write */
133 reg_a = rtc_read( RTC_CONTROLA );
134 rtc_write( RTC_CONTROLA, ( reg_a | RTC_CA_WRITE ));
135
136 rtc_write( RTC_MONTH, bin2bcd( tmp->tm_mon ));
137
138 rtc_write( RTC_DAY_OF_WEEK, bin2bcd( tmp->tm_wday ));
139 rtc_write( RTC_DAY_OF_MONTH, bin2bcd( tmp->tm_mday ));
140 rtc_write( RTC_HOURS, bin2bcd( tmp->tm_hour ));
141 rtc_write( RTC_MINUTES, bin2bcd( tmp->tm_min ));
142 rtc_write( RTC_SECONDS, bin2bcd( tmp->tm_sec ));
143
144 /* break year up into century and year in century */
145 rtc_write( RTC_YEAR, bin2bcd( tmp->tm_year % 100 ));
146 rtc_write( RTC_CENTURY, bin2bcd( tmp->tm_year / 100 ));
147
148 /* unlock clock registers after read */
149 rtc_write( RTC_CONTROLA, ( reg_a & ~RTC_CA_WRITE ));
150}
151
152void rtc_reset (void)
153{
154 uchar reg_a, reg_b, reg_c;
155
156 reg_a = rtc_read( RTC_CONTROLA );
157 reg_b = rtc_read( RTC_CONTROLB );
158
159 if ( reg_b & RTC_CB_OSC_DISABLE )
160 {
161 printf( "real-time-clock was stopped. Now starting...\n" );
162 reg_a |= RTC_CA_WRITE;
163 reg_b &= ~RTC_CB_OSC_DISABLE;
164
165 rtc_write( RTC_CONTROLA, reg_a );
166 rtc_write( RTC_CONTROLB, reg_b );
167 }
168
169 /* make sure read/write clock register bits are cleared */
170 reg_a &= ~( RTC_CA_WRITE | RTC_CA_READ );
171 rtc_write( RTC_CONTROLA, reg_a );
172
173 reg_c = rtc_read( RTC_CONTROLC );
174 if (( reg_c & RTC_CC_BATTERY_FLAG ) == 0 )
175 printf( "RTC battery low. Clock setting may not be reliable.\n" );
176}
177
178/* ------------------------------------------------------------------------- */
179
180static uchar rtc_read( unsigned int addr )
181{
182 uchar val = *(volatile unsigned char*)(addr);
183#ifdef RTC_DEBUG
184 printf( "rtc_read: %x:%x\n", addr, val );
185#endif
186 return( val );
187}
188
189static void rtc_write( unsigned int addr, uchar val )
190{
191#ifdef RTC_DEBUG
192 printf( "rtc_write: %x:%x\n", addr, val );
193#endif
194 *(volatile unsigned char*)(addr) = val;
195}
196
197static unsigned bcd2bin (uchar n)
198{
199 return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
200}
201
202static unsigned char bin2bcd (unsigned int n)
203{
204 return (((n / 10) << 4) | (n % 10));
205}
206
068b60a0 207#endif