]>
git.ipfire.org Git - people/ms/u-boot.git/blob - board/etin/debris/phantom.c
4 * Phantom RTC device driver for EVA
9 * Copyright 2002 Etinsys Inc.
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
21 #if defined(CONFIG_CMD_DATE)
23 #define RTC_BASE (CONFIG_SYS_NVRAM_BASE_ADDR + 0x7fff8)
25 #define RTC_YEAR ( RTC_BASE + 7 )
26 #define RTC_MONTH ( RTC_BASE + 6 )
27 #define RTC_DAY_OF_MONTH ( RTC_BASE + 5 )
28 #define RTC_DAY_OF_WEEK ( RTC_BASE + 4 )
29 #define RTC_HOURS ( RTC_BASE + 3 )
30 #define RTC_MINUTES ( RTC_BASE + 2 )
31 #define RTC_SECONDS ( RTC_BASE + 1 )
32 #define RTC_CENTURY ( RTC_BASE + 0 )
34 #define RTC_CONTROLA RTC_CENTURY
35 #define RTC_CONTROLB RTC_SECONDS
36 #define RTC_CONTROLC RTC_DAY_OF_WEEK
38 #define RTC_CA_WRITE 0x80
39 #define RTC_CA_READ 0x40
41 #define RTC_CB_OSC_DISABLE 0x80
43 #define RTC_CC_BATTERY_FLAG 0x80
44 #define RTC_CC_FREQ_TEST 0x40
47 static int phantom_flag
= -1;
48 static int century_flag
= -1;
50 static uchar
rtc_read(unsigned int addr
)
52 return *(volatile unsigned char *)(addr
);
55 static void rtc_write(unsigned int addr
, uchar val
)
57 *(volatile unsigned char *)(addr
) = val
;
60 static unsigned char phantom_rtc_sequence
[] = {
61 0xc5, 0x3a, 0xa3, 0x5c, 0xc5, 0x3a, 0xa3, 0x5c
64 static unsigned char* phantom_rtc_read(int addr
, unsigned char rtc
[8])
68 unsigned char save
= rtc_read(addr
);
70 for (j
= 0; j
< 8; j
++) {
71 v
= phantom_rtc_sequence
[j
];
72 for (i
= 0; i
< 8; i
++) {
73 rtc_write(addr
, v
& 1);
77 for (j
= 0; j
< 8; j
++) {
79 for (i
= 0; i
< 8; i
++) {
80 if(rtc_read(addr
) & 1)
85 rtc_write(addr
, save
);
89 static void phantom_rtc_write(int addr
, unsigned char rtc
[8])
93 unsigned char save
= rtc_read(addr
);
94 for (j
= 0; j
< 8; j
++) {
95 v
= phantom_rtc_sequence
[j
];
96 for (i
= 0; i
< 8; i
++) {
97 rtc_write(addr
, v
& 1);
101 for (j
= 0; j
< 8; j
++) {
103 for (i
= 0; i
< 8; i
++) {
104 rtc_write(addr
, v
& 1);
108 rtc_write(addr
, save
);
111 static int get_phantom_flag(void)
114 unsigned char rtc
[8];
116 phantom_rtc_read(RTC_BASE
, rtc
);
118 for(i
= 1; i
< 8; i
++) {
119 if (rtc
[i
] != rtc
[0])
127 if (phantom_flag
< 0)
128 phantom_flag
= get_phantom_flag();
131 unsigned char rtc
[8];
132 phantom_rtc_read(RTC_BASE
, rtc
);
134 printf( "real-time-clock was stopped. Now starting...\n" );
136 phantom_rtc_write(RTC_BASE
, rtc
);
139 uchar reg_a
, reg_b
, reg_c
;
140 reg_a
= rtc_read( RTC_CONTROLA
);
141 reg_b
= rtc_read( RTC_CONTROLB
);
143 if ( reg_b
& RTC_CB_OSC_DISABLE
)
145 printf( "real-time-clock was stopped. Now starting...\n" );
146 reg_a
|= RTC_CA_WRITE
;
147 reg_b
&= ~RTC_CB_OSC_DISABLE
;
148 rtc_write( RTC_CONTROLA
, reg_a
);
149 rtc_write( RTC_CONTROLB
, reg_b
);
152 /* make sure read/write clock register bits are cleared */
153 reg_a
&= ~( RTC_CA_WRITE
| RTC_CA_READ
);
154 rtc_write( RTC_CONTROLA
, reg_a
);
156 reg_c
= rtc_read( RTC_CONTROLC
);
157 if (( reg_c
& RTC_CC_BATTERY_FLAG
) == 0 )
158 printf( "RTC battery low. Clock setting may not be reliable.\n");
162 inline unsigned bcd2bin (uchar n
)
164 return ((((n
>> 4) & 0x0F) * 10) + (n
& 0x0F));
167 inline unsigned char bin2bcd (unsigned int n
)
169 return (((n
/ 10) << 4) | (n
% 10));
172 static int get_century_flag(void)
176 bcd
= rtc_read( RTC_CENTURY
);
177 century
= bcd2bin( bcd
& 0x3F );
178 rtc_write( RTC_CENTURY
, bin2bcd(century
+1));
179 if (bcd
== rtc_read( RTC_CENTURY
))
181 rtc_write( RTC_CENTURY
, bcd
);
185 int rtc_get( struct rtc_time
*tmp
)
187 if (phantom_flag
< 0)
188 phantom_flag
= get_phantom_flag();
192 unsigned char rtc
[8];
194 phantom_rtc_read(RTC_BASE
, rtc
);
196 tmp
->tm_sec
= bcd2bin(rtc
[1] & 0x7f);
197 tmp
->tm_min
= bcd2bin(rtc
[2] & 0x7f);
198 tmp
->tm_hour
= bcd2bin(rtc
[3] & 0x1f);
199 tmp
->tm_wday
= bcd2bin(rtc
[4] & 0x7);
200 tmp
->tm_mday
= bcd2bin(rtc
[5] & 0x3f);
201 tmp
->tm_mon
= bcd2bin(rtc
[6] & 0x1f);
202 tmp
->tm_year
= bcd2bin(rtc
[7]) + 1900;
206 if( (rtc
[3] & 0x80) && (rtc
[3] & 0x40) ) tmp
->tm_hour
+= 12;
207 if (tmp
->tm_year
< 1970) tmp
->tm_year
+= 100;
209 uchar sec
, min
, hour
;
210 uchar mday
, wday
, mon
, year
;
216 if (century_flag
< 0)
217 century_flag
= get_century_flag();
219 reg_a
= rtc_read( RTC_CONTROLA
);
220 /* lock clock registers for read */
221 rtc_write( RTC_CONTROLA
, ( reg_a
| RTC_CA_READ
));
223 sec
= rtc_read( RTC_SECONDS
);
224 min
= rtc_read( RTC_MINUTES
);
225 hour
= rtc_read( RTC_HOURS
);
226 mday
= rtc_read( RTC_DAY_OF_MONTH
);
227 wday
= rtc_read( RTC_DAY_OF_WEEK
);
228 mon
= rtc_read( RTC_MONTH
);
229 year
= rtc_read( RTC_YEAR
);
230 century
= rtc_read( RTC_CENTURY
);
232 /* unlock clock registers after read */
233 rtc_write( RTC_CONTROLA
, ( reg_a
& ~RTC_CA_READ
));
235 tmp
->tm_sec
= bcd2bin( sec
& 0x7F );
236 tmp
->tm_min
= bcd2bin( min
& 0x7F );
237 tmp
->tm_hour
= bcd2bin( hour
& 0x3F );
238 tmp
->tm_mday
= bcd2bin( mday
& 0x3F );
239 tmp
->tm_mon
= bcd2bin( mon
& 0x1F );
240 tmp
->tm_wday
= bcd2bin( wday
& 0x07 );
243 tmp
->tm_year
= bcd2bin( year
) +
244 ( bcd2bin( century
& 0x3F ) * 100 );
246 tmp
->tm_year
= bcd2bin( year
) + 1900;
247 if (tmp
->tm_year
< 1970) tmp
->tm_year
+= 100;
257 int rtc_set( struct rtc_time
*tmp
)
259 if (phantom_flag
< 0)
260 phantom_flag
= get_phantom_flag();
264 unsigned char rtc
[8];
267 year
-= (year
< 2000) ? 1900 : 2000;
270 rtc
[1] = bin2bcd(tmp
->tm_sec
);
271 rtc
[2] = bin2bcd(tmp
->tm_min
);
272 rtc
[3] = bin2bcd(tmp
->tm_hour
);
273 rtc
[4] = bin2bcd(tmp
->tm_wday
);
274 rtc
[5] = bin2bcd(tmp
->tm_mday
);
275 rtc
[6] = bin2bcd(tmp
->tm_mon
);
276 rtc
[7] = bin2bcd(year
);
278 phantom_rtc_write(RTC_BASE
, rtc
);
281 if (century_flag
< 0)
282 century_flag
= get_century_flag();
284 /* lock clock registers for write */
285 reg_a
= rtc_read( RTC_CONTROLA
);
286 rtc_write( RTC_CONTROLA
, ( reg_a
| RTC_CA_WRITE
));
288 rtc_write( RTC_MONTH
, bin2bcd( tmp
->tm_mon
));
290 rtc_write( RTC_DAY_OF_WEEK
, bin2bcd( tmp
->tm_wday
));
291 rtc_write( RTC_DAY_OF_MONTH
, bin2bcd( tmp
->tm_mday
));
292 rtc_write( RTC_HOURS
, bin2bcd( tmp
->tm_hour
));
293 rtc_write( RTC_MINUTES
, bin2bcd( tmp
->tm_min
));
294 rtc_write( RTC_SECONDS
, bin2bcd( tmp
->tm_sec
));
296 /* break year up into century and year in century */
298 rtc_write( RTC_YEAR
, bin2bcd( tmp
->tm_year
% 100 ));
299 rtc_write( RTC_CENTURY
, bin2bcd( tmp
->tm_year
/ 100 ));
301 reg_a
|= bin2bcd( tmp
->tm_year
/ 100 );
303 rtc_write(RTC_YEAR
, bin2bcd(tmp
->tm_year
-
304 ((tmp
->tm_year
< 2000) ? 1900 : 2000)));
307 /* unlock clock registers after read */
308 rtc_write( RTC_CONTROLA
, ( reg_a
& ~RTC_CA_WRITE
));