blob: 046aa67d9aa2f90974612ef4d9416bbe7eea9ac4 [file] [log] [blame]
Detlev Zundel58d0bff2009-03-30 00:31:33 +02001/*
2 * (C) Copyright 2008, 2009
3 * Andreas Pfefferle, DENX Software Engineering, ap@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <asm/io.h>
25#include <common.h>
26#include <command.h>
27#include <config.h>
Detlev Zundel58d0bff2009-03-30 00:31:33 +020028#include <rtc.h>
29#include <tws.h>
30
31#if defined(CONFIG_CMD_DATE)
32
33/*
34 * Note: The acrobatics below is due to the hideously ingenius idea of
35 * the chip designers. As the chip does not allow register
36 * addressing, all values need to be read and written in one go. Sure
37 * enough, the 'wday' field (0-6) is transferred using the economic
38 * number of 4 bits right in the middle of the packet.....
39 */
40
41int rtc_get(struct rtc_time *tm)
42{
43 int rel = 0;
44 uchar buffer[7];
45
46 memset(buffer, 0, 7);
47
48 /* Read 52 bits into our buffer */
49 tws_read(buffer, 52);
50
Albin Tonnerre8aca7202009-08-13 15:31:11 +020051 tm->tm_sec = bcd2bin( buffer[0] & 0x7F);
52 tm->tm_min = bcd2bin( buffer[1] & 0x7F);
53 tm->tm_hour = bcd2bin( buffer[2] & 0x3F);
54 tm->tm_wday = bcd2bin( buffer[3] & 0x07);
55 tm->tm_mday = bcd2bin((buffer[3] & 0xF0) >> 4 | (buffer[4] & 0x0F) << 4);
56 tm->tm_mon = bcd2bin((buffer[4] & 0x30) >> 4 | (buffer[5] & 0x0F) << 4);
57 tm->tm_year = bcd2bin((buffer[5] & 0xF0) >> 4 | (buffer[6] & 0x0F) << 4) + 2000;
Detlev Zundel58d0bff2009-03-30 00:31:33 +020058 tm->tm_yday = 0;
59 tm->tm_isdst = 0;
60
61 if (tm->tm_sec & 0x80) {
62 puts("### Warning: RTC Low Voltage - date/time not reliable\n");
63 rel = -1;
64 }
65
66 debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
67 tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
68 tm->tm_hour, tm->tm_min, tm->tm_sec);
69
70 return rel;
71}
72
73int rtc_set(struct rtc_time *tm)
74{
75 uchar buffer[7];
76 uchar tmp;
77
78 debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
79 tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
80 tm->tm_hour, tm->tm_min, tm->tm_sec);
81
82 memset(buffer, 0, 7);
Albin Tonnerre8aca7202009-08-13 15:31:11 +020083 buffer[0] = bin2bcd(tm->tm_sec);
84 buffer[1] = bin2bcd(tm->tm_min);
85 buffer[2] = bin2bcd(tm->tm_hour);
86 buffer[3] = bin2bcd(tm->tm_wday);
87 tmp = bin2bcd(tm->tm_mday);
Detlev Zundel58d0bff2009-03-30 00:31:33 +020088 buffer[3] |= (tmp & 0x0F) << 4;
89 buffer[4] = (tmp & 0xF0) >> 4;
Albin Tonnerre8aca7202009-08-13 15:31:11 +020090 tmp = bin2bcd(tm->tm_mon);
Detlev Zundel58d0bff2009-03-30 00:31:33 +020091 buffer[4] |= (tmp & 0x0F) << 4;
92 buffer[5] = (tmp & 0xF0) >> 4;
Albin Tonnerre8aca7202009-08-13 15:31:11 +020093 tmp = bin2bcd(tm->tm_year % 100);
Detlev Zundel58d0bff2009-03-30 00:31:33 +020094 buffer[5] |= (tmp & 0x0F) << 4;
95 buffer[6] = (tmp & 0xF0) >> 4;
96
97 /* Write the resulting 52 bits to device */
98 tws_write(buffer, 52);
99
100 return 0;
101}
102
103void rtc_reset(void)
104{
105 struct rtc_time tmp;
106
107 tmp.tm_sec = 0;
108 tmp.tm_min = 0;
109 tmp.tm_hour = 0;
110 tmp.tm_wday = 4;
111 tmp.tm_mday = 1;
112 tmp.tm_mon = 1;
113 tmp.tm_year = 2000;
114 rtc_set(&tmp);
115}
116
117#endif