xref: /openbmc/u-boot/drivers/rtc/davinci.c (revision 4e3349b6)
1 /*
2  * (C) Copyright 2011 DENX Software Engineering GmbH
3  * Heiko Schocher <hs@denx.de>
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23 #include <common.h>
24 #include <command.h>
25 #include <rtc.h>
26 #include <asm/io.h>
27 #include <asm/arch/hardware.h>
28 
29 #if defined(CONFIG_CMD_DATE)
30 int rtc_get(struct rtc_time *tmp)
31 {
32 	struct davinci_rtc *rtc = davinci_rtc_base;
33 	unsigned long sec, min, hour, mday, wday, mon_cent, year;
34 	unsigned long status;
35 
36 	status = readl(&rtc->status);
37 	if ((status & RTC_STATE_RUN) != RTC_STATE_RUN) {
38 		printf("RTC doesn't run\n");
39 		return -1;
40 	}
41 	if ((status & RTC_STATE_BUSY) == RTC_STATE_BUSY)
42 		udelay(20);
43 
44 	sec	= readl(&rtc->second);
45 	min	= readl(&rtc->minutes);
46 	hour	= readl(&rtc->hours);
47 	mday	= readl(&rtc->day);
48 	wday	= readl(&rtc->dotw);
49 	mon_cent = readl(&rtc->month);
50 	year	= readl(&rtc->year);
51 
52 	debug("Get RTC year: %02lx mon/cent: %02lx mday: %02lx wday: %02lx "
53 		"hr: %02lx min: %02lx sec: %02lx\n",
54 		year, mon_cent, mday, wday,
55 		hour, min, sec);
56 
57 	tmp->tm_sec  = bcd2bin(sec  & 0x7F);
58 	tmp->tm_min  = bcd2bin(min  & 0x7F);
59 	tmp->tm_hour = bcd2bin(hour & 0x3F);
60 	tmp->tm_mday = bcd2bin(mday & 0x3F);
61 	tmp->tm_mon  = bcd2bin(mon_cent & 0x1F);
62 	tmp->tm_year = bcd2bin(year) + 2000;
63 	tmp->tm_wday = bcd2bin(wday & 0x07);
64 	tmp->tm_yday = 0;
65 	tmp->tm_isdst = 0;
66 
67 	debug("Get 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);
70 
71 	return 0;
72 }
73 
74 int rtc_set(struct rtc_time *tmp)
75 {
76 	struct davinci_rtc *rtc = davinci_rtc_base;
77 
78 	debug("Set DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
79 		tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
80 		tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
81 	writel(bin2bcd(tmp->tm_year % 100), &rtc->year);
82 	writel(bin2bcd(tmp->tm_mon), &rtc->month);
83 
84 	writel(bin2bcd(tmp->tm_wday), &rtc->dotw);
85 	writel(bin2bcd(tmp->tm_mday), &rtc->day);
86 	writel(bin2bcd(tmp->tm_hour), &rtc->hours);
87 	writel(bin2bcd(tmp->tm_min), &rtc->minutes);
88 	writel(bin2bcd(tmp->tm_sec), &rtc->second);
89 	return 0;
90 }
91 
92 void rtc_reset(void)
93 {
94 	struct davinci_rtc *rtc = davinci_rtc_base;
95 
96 	/* run RTC counter */
97 	writel(0x01, &rtc->ctrl);
98 }
99 #endif
100