2 * (C) Copyright 2008, 2009
3 * Andreas Pfefferle, DENX Software Engineering, ap@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
15 #if defined(CONFIG_CMD_DATE)
18 * Note: The acrobatics below is due to the hideously ingenius idea of
19 * the chip designers. As the chip does not allow register
20 * addressing, all values need to be read and written in one go. Sure
21 * enough, the 'wday' field (0-6) is transferred using the economic
22 * number of 4 bits right in the middle of the packet.....
25 int rtc_get(struct rtc_time *tm)
32 /* Read 52 bits into our buffer */
35 tm->tm_sec = bcd2bin( buffer[0] & 0x7F);
36 tm->tm_min = bcd2bin( buffer[1] & 0x7F);
37 tm->tm_hour = bcd2bin( buffer[2] & 0x3F);
38 tm->tm_wday = bcd2bin( buffer[3] & 0x07);
39 tm->tm_mday = bcd2bin((buffer[3] & 0xF0) >> 4 | (buffer[4] & 0x0F) << 4);
40 tm->tm_mon = bcd2bin((buffer[4] & 0x30) >> 4 | (buffer[5] & 0x0F) << 4);
41 tm->tm_year = bcd2bin((buffer[5] & 0xF0) >> 4 | (buffer[6] & 0x0F) << 4) + 2000;
45 if (tm->tm_sec & 0x80) {
46 puts("### Warning: RTC Low Voltage - date/time not reliable\n");
50 debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
51 tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
52 tm->tm_hour, tm->tm_min, tm->tm_sec);
57 int rtc_set(struct rtc_time *tm)
62 debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
63 tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
64 tm->tm_hour, tm->tm_min, tm->tm_sec);
67 buffer[0] = bin2bcd(tm->tm_sec);
68 buffer[1] = bin2bcd(tm->tm_min);
69 buffer[2] = bin2bcd(tm->tm_hour);
70 buffer[3] = bin2bcd(tm->tm_wday);
71 tmp = bin2bcd(tm->tm_mday);
72 buffer[3] |= (tmp & 0x0F) << 4;
73 buffer[4] = (tmp & 0xF0) >> 4;
74 tmp = bin2bcd(tm->tm_mon);
75 buffer[4] |= (tmp & 0x0F) << 4;
76 buffer[5] = (tmp & 0xF0) >> 4;
77 tmp = bin2bcd(tm->tm_year % 100);
78 buffer[5] |= (tmp & 0x0F) << 4;
79 buffer[6] = (tmp & 0xF0) >> 4;
81 /* Write the resulting 52 bits to device */
82 tws_write(buffer, 52);