blob: 99ae31e2a5f219868c7854076291e32b5e3b361d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocher62f3ec72011-09-14 19:48:23 +00002/*
3 * (C) Copyright 2011 DENX Software Engineering GmbH
4 * Heiko Schocher <hs@denx.de>
Heiko Schocher62f3ec72011-09-14 19:48:23 +00005 */
6#include <common.h>
7#include <command.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Heiko Schocher62f3ec72011-09-14 19:48:23 +00009#include <rtc.h>
10#include <asm/io.h>
Tom Rini56424eb2013-08-28 09:00:28 -040011#include <asm/davinci_rtc.h>
Dario Binacchid898c8b2021-06-02 22:37:59 +020012#include <asm/arch/hardware.h>
Simon Glassdbd79542020-05-10 11:40:11 -060013#include <linux/delay.h>
Heiko Schocher62f3ec72011-09-14 19:48:23 +000014
Dario Binacchid898c8b2021-06-02 22:37:59 +020015#if !defined(RTC_BASE) && defined(DAVINCI_RTC_BASE)
16#define RTC_BASE DAVINCI_RTC_BASE
17#endif
18
Heiko Schocher62f3ec72011-09-14 19:48:23 +000019int rtc_get(struct rtc_time *tmp)
20{
Dario Binacchid898c8b2021-06-02 22:37:59 +020021 struct davinci_rtc *rtc = (struct davinci_rtc *)RTC_BASE;
Heiko Schocher62f3ec72011-09-14 19:48:23 +000022 unsigned long sec, min, hour, mday, wday, mon_cent, year;
23 unsigned long status;
24
Dario Binacchi0eb3a622021-06-02 22:38:00 +020025 status = readb(&rtc->status);
Heiko Schocher62f3ec72011-09-14 19:48:23 +000026 if ((status & RTC_STATE_RUN) != RTC_STATE_RUN) {
27 printf("RTC doesn't run\n");
28 return -1;
29 }
30 if ((status & RTC_STATE_BUSY) == RTC_STATE_BUSY)
31 udelay(20);
32
Dario Binacchi0eb3a622021-06-02 22:38:00 +020033 sec = readb(&rtc->second);
34 min = readb(&rtc->minutes);
35 hour = readb(&rtc->hours);
36 mday = readb(&rtc->day);
37 wday = readb(&rtc->dotw);
38 mon_cent = readb(&rtc->month);
39 year = readb(&rtc->year);
Heiko Schocher62f3ec72011-09-14 19:48:23 +000040
41 debug("Get RTC year: %02lx mon/cent: %02lx mday: %02lx wday: %02lx "
42 "hr: %02lx min: %02lx sec: %02lx\n",
43 year, mon_cent, mday, wday,
44 hour, min, sec);
45
46 tmp->tm_sec = bcd2bin(sec & 0x7F);
47 tmp->tm_min = bcd2bin(min & 0x7F);
48 tmp->tm_hour = bcd2bin(hour & 0x3F);
49 tmp->tm_mday = bcd2bin(mday & 0x3F);
50 tmp->tm_mon = bcd2bin(mon_cent & 0x1F);
51 tmp->tm_year = bcd2bin(year) + 2000;
52 tmp->tm_wday = bcd2bin(wday & 0x07);
53 tmp->tm_yday = 0;
54 tmp->tm_isdst = 0;
55
56 debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
57 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
58 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
59
60 return 0;
61}
62
63int rtc_set(struct rtc_time *tmp)
64{
Dario Binacchid898c8b2021-06-02 22:37:59 +020065 struct davinci_rtc *rtc = (struct davinci_rtc *)RTC_BASE;
Heiko Schocher62f3ec72011-09-14 19:48:23 +000066
67 debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
68 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
69 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
Dario Binacchi0eb3a622021-06-02 22:38:00 +020070 writeb(bin2bcd(tmp->tm_year % 100), &rtc->year);
71 writeb(bin2bcd(tmp->tm_mon), &rtc->month);
Heiko Schocher62f3ec72011-09-14 19:48:23 +000072
Dario Binacchi0eb3a622021-06-02 22:38:00 +020073 writeb(bin2bcd(tmp->tm_wday), &rtc->dotw);
74 writeb(bin2bcd(tmp->tm_mday), &rtc->day);
75 writeb(bin2bcd(tmp->tm_hour), &rtc->hours);
76 writeb(bin2bcd(tmp->tm_min), &rtc->minutes);
77 writeb(bin2bcd(tmp->tm_sec), &rtc->second);
Heiko Schocher62f3ec72011-09-14 19:48:23 +000078 return 0;
79}
80
81void rtc_reset(void)
82{
Dario Binacchid898c8b2021-06-02 22:37:59 +020083 struct davinci_rtc *rtc = (struct davinci_rtc *)RTC_BASE;
Heiko Schocher62f3ec72011-09-14 19:48:23 +000084
85 /* run RTC counter */
Dario Binacchi0eb3a622021-06-02 22:38:00 +020086 writeb(0x01, &rtc->ctrl);
Heiko Schocher62f3ec72011-09-14 19:48:23 +000087}