xref: /openbmc/u-boot/drivers/rtc/davinci.c (revision 406fd7e2)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2011 DENX Software Engineering GmbH
4  * Heiko Schocher <hs@denx.de>
5  */
6 #include <common.h>
7 #include <command.h>
8 #include <rtc.h>
9 #include <asm/io.h>
10 #include <asm/davinci_rtc.h>
11 
12 int rtc_get(struct rtc_time *tmp)
13 {
14 	struct davinci_rtc *rtc = (struct davinci_rtc *)DAVINCI_RTC_BASE;
15 	unsigned long sec, min, hour, mday, wday, mon_cent, year;
16 	unsigned long status;
17 
18 	status = readl(&rtc->status);
19 	if ((status & RTC_STATE_RUN) != RTC_STATE_RUN) {
20 		printf("RTC doesn't run\n");
21 		return -1;
22 	}
23 	if ((status & RTC_STATE_BUSY) == RTC_STATE_BUSY)
24 		udelay(20);
25 
26 	sec	= readl(&rtc->second);
27 	min	= readl(&rtc->minutes);
28 	hour	= readl(&rtc->hours);
29 	mday	= readl(&rtc->day);
30 	wday	= readl(&rtc->dotw);
31 	mon_cent = readl(&rtc->month);
32 	year	= readl(&rtc->year);
33 
34 	debug("Get RTC year: %02lx mon/cent: %02lx mday: %02lx wday: %02lx "
35 		"hr: %02lx min: %02lx sec: %02lx\n",
36 		year, mon_cent, mday, wday,
37 		hour, min, sec);
38 
39 	tmp->tm_sec  = bcd2bin(sec  & 0x7F);
40 	tmp->tm_min  = bcd2bin(min  & 0x7F);
41 	tmp->tm_hour = bcd2bin(hour & 0x3F);
42 	tmp->tm_mday = bcd2bin(mday & 0x3F);
43 	tmp->tm_mon  = bcd2bin(mon_cent & 0x1F);
44 	tmp->tm_year = bcd2bin(year) + 2000;
45 	tmp->tm_wday = bcd2bin(wday & 0x07);
46 	tmp->tm_yday = 0;
47 	tmp->tm_isdst = 0;
48 
49 	debug("Get DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
50 		tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
51 		tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
52 
53 	return 0;
54 }
55 
56 int rtc_set(struct rtc_time *tmp)
57 {
58 	struct davinci_rtc *rtc = (struct davinci_rtc *)DAVINCI_RTC_BASE;
59 
60 	debug("Set DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
61 		tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
62 		tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
63 	writel(bin2bcd(tmp->tm_year % 100), &rtc->year);
64 	writel(bin2bcd(tmp->tm_mon), &rtc->month);
65 
66 	writel(bin2bcd(tmp->tm_wday), &rtc->dotw);
67 	writel(bin2bcd(tmp->tm_mday), &rtc->day);
68 	writel(bin2bcd(tmp->tm_hour), &rtc->hours);
69 	writel(bin2bcd(tmp->tm_min), &rtc->minutes);
70 	writel(bin2bcd(tmp->tm_sec), &rtc->second);
71 	return 0;
72 }
73 
74 void rtc_reset(void)
75 {
76 	struct davinci_rtc *rtc = (struct davinci_rtc *)DAVINCI_RTC_BASE;
77 
78 	/* run RTC counter */
79 	writel(0x01, &rtc->ctrl);
80 }
81