xref: /openbmc/u-boot/drivers/rtc/mx27rtc.c (revision 1d2c0506)
1 /*
2  * Freescale i.MX27 RTC Driver
3  *
4  * Copyright (C) 2012 Philippe Reynes <tremyfr@yahoo.fr>
5  *
6  * SPDX-License-Identifier:	GPL-2.0+
7  */
8 
9 #include <common.h>
10 #include <rtc.h>
11 #include <asm/io.h>
12 #include <asm/arch/imx-regs.h>
13 
14 #define HOUR_SHIFT 8
15 #define HOUR_MASK  0x1f
16 #define MIN_SHIFT  0
17 #define MIN_MASK   0x3f
18 
19 int rtc_get(struct rtc_time *time)
20 {
21 	struct rtc_regs *rtc_regs = (struct rtc_regs *)IMX_RTC_BASE;
22 	uint32_t day, hour, min, sec;
23 
24 	day  = readl(&rtc_regs->dayr);
25 	hour = readl(&rtc_regs->hourmin);
26 	sec  = readl(&rtc_regs->seconds);
27 
28 	min  = (hour >> MIN_SHIFT) & MIN_MASK;
29 	hour = (hour >> HOUR_SHIFT) & HOUR_MASK;
30 
31 	sec += min * 60 + hour * 3600 + day * 24 * 3600;
32 
33 	rtc_to_tm(sec, time);
34 
35 	return 0;
36 }
37 
38 int rtc_set(struct rtc_time *time)
39 {
40 	struct rtc_regs *rtc_regs = (struct rtc_regs *)IMX_RTC_BASE;
41 	uint32_t day, hour, min, sec;
42 
43 	sec = rtc_mktime(time);
44 
45 	day  = sec / (24 * 3600);
46 	sec  = sec % (24 * 3600);
47 	hour = sec / 3600;
48 	sec  = sec % 3600;
49 	min  = sec / 60;
50 	sec  = sec % 60;
51 
52 	hour  = (hour & HOUR_MASK) << HOUR_SHIFT;
53 	hour |= (min & MIN_MASK) << MIN_SHIFT;
54 
55 	writel(day, &rtc_regs->dayr);
56 	writel(hour, &rtc_regs->hourmin);
57 	writel(sec, &rtc_regs->seconds);
58 
59 	return 0;
60 }
61 
62 void rtc_reset(void)
63 {
64 	struct rtc_regs *rtc_regs = (struct rtc_regs *)IMX_RTC_BASE;
65 
66 	writel(0, &rtc_regs->dayr);
67 	writel(0, &rtc_regs->hourmin);
68 	writel(0, &rtc_regs->seconds);
69 }
70