blob: f862e2f951c451e1d7213e8d468232bcb262c281 [file] [log] [blame]
Heiko Schocher41d272d2011-09-14 19:48:23 +00001/*
2 * (C) Copyright 2011 DENX Software Engineering GmbH
3 * Heiko Schocher <hs@denx.de>
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Heiko Schocher41d272d2011-09-14 19:48:23 +00006 */
7#include <common.h>
8#include <command.h>
9#include <rtc.h>
10#include <asm/io.h>
Tom Rini155d4242013-08-28 09:00:28 -040011#include <asm/davinci_rtc.h>
Heiko Schocher41d272d2011-09-14 19:48:23 +000012
13#if defined(CONFIG_CMD_DATE)
Heiko Schocher41d272d2011-09-14 19:48:23 +000014int rtc_get(struct rtc_time *tmp)
15{
Tom Rini5d4d38d2013-08-28 09:00:27 -040016 struct davinci_rtc *rtc = (struct davinci_rtc *)DAVINCI_RTC_BASE;
Heiko Schocher41d272d2011-09-14 19:48:23 +000017 unsigned long sec, min, hour, mday, wday, mon_cent, year;
18 unsigned long status;
19
20 status = readl(&rtc->status);
21 if ((status & RTC_STATE_RUN) != RTC_STATE_RUN) {
22 printf("RTC doesn't run\n");
23 return -1;
24 }
25 if ((status & RTC_STATE_BUSY) == RTC_STATE_BUSY)
26 udelay(20);
27
28 sec = readl(&rtc->second);
29 min = readl(&rtc->minutes);
30 hour = readl(&rtc->hours);
31 mday = readl(&rtc->day);
32 wday = readl(&rtc->dotw);
33 mon_cent = readl(&rtc->month);
34 year = readl(&rtc->year);
35
36 debug("Get RTC year: %02lx mon/cent: %02lx mday: %02lx wday: %02lx "
37 "hr: %02lx min: %02lx sec: %02lx\n",
38 year, mon_cent, mday, wday,
39 hour, min, sec);
40
41 tmp->tm_sec = bcd2bin(sec & 0x7F);
42 tmp->tm_min = bcd2bin(min & 0x7F);
43 tmp->tm_hour = bcd2bin(hour & 0x3F);
44 tmp->tm_mday = bcd2bin(mday & 0x3F);
45 tmp->tm_mon = bcd2bin(mon_cent & 0x1F);
46 tmp->tm_year = bcd2bin(year) + 2000;
47 tmp->tm_wday = bcd2bin(wday & 0x07);
48 tmp->tm_yday = 0;
49 tmp->tm_isdst = 0;
50
51 debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
52 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
53 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
54
55 return 0;
56}
57
58int rtc_set(struct rtc_time *tmp)
59{
Tom Rini5d4d38d2013-08-28 09:00:27 -040060 struct davinci_rtc *rtc = (struct davinci_rtc *)DAVINCI_RTC_BASE;
Heiko Schocher41d272d2011-09-14 19:48:23 +000061
62 debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
63 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
64 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
65 writel(bin2bcd(tmp->tm_year % 100), &rtc->year);
66 writel(bin2bcd(tmp->tm_mon), &rtc->month);
67
68 writel(bin2bcd(tmp->tm_wday), &rtc->dotw);
69 writel(bin2bcd(tmp->tm_mday), &rtc->day);
70 writel(bin2bcd(tmp->tm_hour), &rtc->hours);
71 writel(bin2bcd(tmp->tm_min), &rtc->minutes);
72 writel(bin2bcd(tmp->tm_sec), &rtc->second);
73 return 0;
74}
75
76void rtc_reset(void)
77{
Tom Rini5d4d38d2013-08-28 09:00:27 -040078 struct davinci_rtc *rtc = (struct davinci_rtc *)DAVINCI_RTC_BASE;
Heiko Schocher41d272d2011-09-14 19:48:23 +000079
80 /* run RTC counter */
81 writel(0x01, &rtc->ctrl);
82}
83#endif