xref: /openbmc/u-boot/drivers/rtc/mvrtc.c (revision c4f07be2)
1 /*
2  * Copyright (C) 2011
3  * Jason Cooper <u-boot@lakedaemon.net>
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 
24 /*
25  * Date & Time support for Marvell Integrated RTC
26  */
27 
28 #include <common.h>
29 #include <command.h>
30 #include <rtc.h>
31 #include "mvrtc.h"
32 
33 /* This RTC does not support century, so we assume 20 */
34 #define CENTURY 20
35 
36 int rtc_get(struct rtc_time *t)
37 {
38 	u32 time;
39 	u32 date;
40 	struct mvrtc_registers *mvrtc_regs;
41 
42 	mvrtc_regs = (struct mvrtc_registers *)KW_RTC_BASE;
43 
44 	/* read the time register */
45 	time = readl(&mvrtc_regs->time);
46 
47 	/* read the date register */
48 	date = readl(&mvrtc_regs->date);
49 
50 	/* test for 12 hour clock (can't tell if it's am/pm) */
51 	if (time & MVRTC_HRFMT_MSK) {
52 		printf("Error: RTC in 12 hour mode, can't determine AM/PM.\n");
53 		return -1;
54 	}
55 
56 	/* time */
57 	t->tm_sec  = bcd2bin((time >> MVRTC_SEC_SFT)  & MVRTC_SEC_MSK);
58 	t->tm_min  = bcd2bin((time >> MVRTC_MIN_SFT)  & MVRTC_MIN_MSK);
59 	t->tm_hour = bcd2bin((time >> MVRTC_HOUR_SFT) & MVRTC_HOUR_MSK);
60 	t->tm_wday = bcd2bin((time >> MVRTC_DAY_SFT)  & MVRTC_DAY_MSK);
61 	t->tm_wday--;
62 
63 	/* date */
64 	t->tm_mday = bcd2bin((date >> MVRTC_DATE_SFT) & MVRTC_DATE_MSK);
65 	t->tm_mon  = bcd2bin((date >> MVRTC_MON_SFT)  & MVRTC_MON_MSK);
66 	t->tm_year = bcd2bin((date >> MVRTC_YEAR_SFT) & MVRTC_YEAR_MSK);
67 	t->tm_year += CENTURY * 100;
68 
69 	/* not supported in this RTC */
70 	t->tm_yday  = 0;
71 	t->tm_isdst = 0;
72 
73 	return 0;
74 }
75 
76 int rtc_set(struct rtc_time *t)
77 {
78 	u32 time = 0; /* sets hour format bit to zero, 24hr format. */
79 	u32 date = 0;
80 	struct mvrtc_registers *mvrtc_regs;
81 
82 	mvrtc_regs = (struct mvrtc_registers *)KW_RTC_BASE;
83 
84 	/* check that this code isn't 80+ years old ;-) */
85 	if ((t->tm_year / 100) != CENTURY)
86 		printf("Warning: Only century %d supported.\n", CENTURY);
87 
88 	/* time */
89 	time |= (bin2bcd(t->tm_sec)      & MVRTC_SEC_MSK)  << MVRTC_SEC_SFT;
90 	time |= (bin2bcd(t->tm_min)      & MVRTC_MIN_MSK)  << MVRTC_MIN_SFT;
91 	time |= (bin2bcd(t->tm_hour)     & MVRTC_HOUR_MSK) << MVRTC_HOUR_SFT;
92 	time |= (bin2bcd(t->tm_wday + 1) & MVRTC_DAY_MSK)  << MVRTC_DAY_SFT;
93 
94 	/* date */
95 	date |= (bin2bcd(t->tm_mday)       & MVRTC_DATE_MSK) << MVRTC_DATE_SFT;
96 	date |= (bin2bcd(t->tm_mon)        & MVRTC_MON_MSK)  << MVRTC_MON_SFT;
97 	date |= (bin2bcd(t->tm_year % 100) & MVRTC_YEAR_MSK) << MVRTC_YEAR_SFT;
98 
99 	/* write the time register */
100 	writel(time, &mvrtc_regs->time);
101 
102 	/* write the date register */
103 	writel(date, &mvrtc_regs->date);
104 
105 	return 0;
106 }
107 
108 void rtc_reset(void)
109 {
110 	u32 time;
111 	u32 sec;
112 	struct mvrtc_registers *mvrtc_regs;
113 
114 	mvrtc_regs = (struct mvrtc_registers *)KW_RTC_BASE;
115 
116 	/* no init routine for this RTC needed, just check that it's working */
117 	time = readl(&mvrtc_regs->time);
118 	sec  = bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK);
119 	udelay(1000000);
120 	time = readl(&mvrtc_regs->time);
121 
122 	if (sec == bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK))
123 		printf("Error: RTC did not increment.\n");
124 }
125