]> git.ipfire.org Git - people/ms/u-boot.git/blame - drivers/rtc/rtc4543.c
Add GPL-2.0+ SPDX-License-Identifier to source files
[people/ms/u-boot.git] / drivers / rtc / rtc4543.c
CommitLineData
04e11cf3
DZ
1/*
2 * (C) Copyright 2008, 2009
3 * Andreas Pfefferle, DENX Software Engineering, ap@denx.de.
4 *
1a459660 5 * SPDX-License-Identifier: GPL-2.0+
04e11cf3
DZ
6 */
7
8#include <asm/io.h>
9#include <common.h>
10#include <command.h>
11#include <config.h>
04e11cf3
DZ
12#include <rtc.h>
13#include <tws.h>
14
15#if defined(CONFIG_CMD_DATE)
16
17/*
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.....
23 */
24
25int rtc_get(struct rtc_time *tm)
26{
27 int rel = 0;
28 uchar buffer[7];
29
30 memset(buffer, 0, 7);
31
32 /* Read 52 bits into our buffer */
33 tws_read(buffer, 52);
34
e84aba13
AT
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;
04e11cf3
DZ
42 tm->tm_yday = 0;
43 tm->tm_isdst = 0;
44
45 if (tm->tm_sec & 0x80) {
46 puts("### Warning: RTC Low Voltage - date/time not reliable\n");
47 rel = -1;
48 }
49
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);
53
54 return rel;
55}
56
57int rtc_set(struct rtc_time *tm)
58{
59 uchar buffer[7];
60 uchar tmp;
61
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);
65
66 memset(buffer, 0, 7);
e84aba13
AT
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);
04e11cf3
DZ
72 buffer[3] |= (tmp & 0x0F) << 4;
73 buffer[4] = (tmp & 0xF0) >> 4;
e84aba13 74 tmp = bin2bcd(tm->tm_mon);
04e11cf3
DZ
75 buffer[4] |= (tmp & 0x0F) << 4;
76 buffer[5] = (tmp & 0xF0) >> 4;
e84aba13 77 tmp = bin2bcd(tm->tm_year % 100);
04e11cf3
DZ
78 buffer[5] |= (tmp & 0x0F) << 4;
79 buffer[6] = (tmp & 0xF0) >> 4;
80
81 /* Write the resulting 52 bits to device */
82 tws_write(buffer, 52);
83
84 return 0;
85}
86
87void rtc_reset(void)
88{
89 struct rtc_time tmp;
90
91 tmp.tm_sec = 0;
92 tmp.tm_min = 0;
93 tmp.tm_hour = 0;
94 tmp.tm_wday = 4;
95 tmp.tm_mday = 1;
96 tmp.tm_mon = 1;
97 tmp.tm_year = 2000;
98 rtc_set(&tmp);
99}
100
101#endif