2 * (C) Copyright 2008, 2009
3 * Andreas Pfefferle, DENX Software Engineering, ap@denx.de.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
32 #if defined(CONFIG_CMD_DATE)
35 * Note: The acrobatics below is due to the hideously ingenius idea of
36 * the chip designers. As the chip does not allow register
37 * addressing, all values need to be read and written in one go. Sure
38 * enough, the 'wday' field (0-6) is transferred using the economic
39 * number of 4 bits right in the middle of the packet.....
42 int rtc_get(struct rtc_time *tm)
49 /* Read 52 bits into our buffer */
52 tm->tm_sec = BCD2BIN( buffer[0] & 0x7F);
53 tm->tm_min = BCD2BIN( buffer[1] & 0x7F);
54 tm->tm_hour = BCD2BIN( buffer[2] & 0x3F);
55 tm->tm_wday = BCD2BIN( buffer[3] & 0x07);
56 tm->tm_mday = BCD2BIN((buffer[3] & 0xF0) >> 4 | (buffer[4] & 0x0F) << 4);
57 tm->tm_mon = BCD2BIN((buffer[4] & 0x30) >> 4 | (buffer[5] & 0x0F) << 4);
58 tm->tm_year = BCD2BIN((buffer[5] & 0xF0) >> 4 | (buffer[6] & 0x0F) << 4) + 2000;
62 if (tm->tm_sec & 0x80) {
63 puts("### Warning: RTC Low Voltage - date/time not reliable\n");
67 debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
68 tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
69 tm->tm_hour, tm->tm_min, tm->tm_sec);
74 int rtc_set(struct rtc_time *tm)
79 debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
80 tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
81 tm->tm_hour, tm->tm_min, tm->tm_sec);
84 buffer[0] = BIN2BCD(tm->tm_sec);
85 buffer[1] = BIN2BCD(tm->tm_min);
86 buffer[2] = BIN2BCD(tm->tm_hour);
87 buffer[3] = BIN2BCD(tm->tm_wday);
88 tmp = BIN2BCD(tm->tm_mday);
89 buffer[3] |= (tmp & 0x0F) << 4;
90 buffer[4] = (tmp & 0xF0) >> 4;
91 tmp = BIN2BCD(tm->tm_mon);
92 buffer[4] |= (tmp & 0x0F) << 4;
93 buffer[5] = (tmp & 0xF0) >> 4;
94 tmp = BIN2BCD(tm->tm_year % 100);
95 buffer[5] |= (tmp & 0x0F) << 4;
96 buffer[6] = (tmp & 0xF0) >> 4;
98 /* Write the resulting 52 bits to device */
99 tws_write(buffer, 52);