xref: /openbmc/u-boot/drivers/rtc/mx27rtc.c (revision 8f082d78)
1 /*
2  * Freescale i.MX27 RTC Driver
3  *
4  * Copyright (C) 2012 Philippe Reynes <tremyfr@yahoo.fr>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */
21 
22 #include <common.h>
23 #include <rtc.h>
24 #include <asm/io.h>
25 #include <asm/arch/imx-regs.h>
26 
27 #define HOUR_SHIFT 8
28 #define HOUR_MASK  0x1f
29 #define MIN_SHIFT  0
30 #define MIN_MASK   0x3f
31 
32 int rtc_get(struct rtc_time *time)
33 {
34 	struct rtc_regs *rtc_regs = (struct rtc_regs *)IMX_RTC_BASE;
35 	uint32_t day, hour, min, sec;
36 
37 	day  = readl(&rtc_regs->dayr);
38 	hour = readl(&rtc_regs->hourmin);
39 	sec  = readl(&rtc_regs->seconds);
40 
41 	min  = (hour >> MIN_SHIFT) & MIN_MASK;
42 	hour = (hour >> HOUR_SHIFT) & HOUR_MASK;
43 
44 	sec += min * 60 + hour * 3600 + day * 24 * 3600;
45 
46 	to_tm(sec, time);
47 
48 	return 0;
49 }
50 
51 int rtc_set(struct rtc_time *time)
52 {
53 	struct rtc_regs *rtc_regs = (struct rtc_regs *)IMX_RTC_BASE;
54 	uint32_t day, hour, min, sec;
55 
56 	sec = mktime(time->tm_year, time->tm_mon, time->tm_mday,
57 		time->tm_hour, time->tm_min, time->tm_sec);
58 
59 	day  = sec / (24 * 3600);
60 	sec  = sec % (24 * 3600);
61 	hour = sec / 3600;
62 	sec  = sec % 3600;
63 	min  = sec / 60;
64 	sec  = sec % 60;
65 
66 	hour  = (hour & HOUR_MASK) << HOUR_SHIFT;
67 	hour |= (min & MIN_MASK) << MIN_SHIFT;
68 
69 	writel(day, &rtc_regs->dayr);
70 	writel(hour, &rtc_regs->hourmin);
71 	writel(sec, &rtc_regs->seconds);
72 
73 	return 0;
74 }
75 
76 void rtc_reset(void)
77 {
78 	struct rtc_regs *rtc_regs = (struct rtc_regs *)IMX_RTC_BASE;
79 
80 	writel(0, &rtc_regs->dayr);
81 	writel(0, &rtc_regs->hourmin);
82 	writel(0, &rtc_regs->seconds);
83 }
84