xref: /openbmc/u-boot/drivers/rtc/mx27rtc.c (revision f13606b7)
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 	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 = mktime(time->tm_year, time->tm_mon, time->tm_mday,
44 		time->tm_hour, time->tm_min, time->tm_sec);
45 
46 	day  = sec / (24 * 3600);
47 	sec  = sec % (24 * 3600);
48 	hour = sec / 3600;
49 	sec  = sec % 3600;
50 	min  = sec / 60;
51 	sec  = sec % 60;
52 
53 	hour  = (hour & HOUR_MASK) << HOUR_SHIFT;
54 	hour |= (min & MIN_MASK) << MIN_SHIFT;
55 
56 	writel(day, &rtc_regs->dayr);
57 	writel(hour, &rtc_regs->hourmin);
58 	writel(sec, &rtc_regs->seconds);
59 
60 	return 0;
61 }
62 
63 void rtc_reset(void)
64 {
65 	struct rtc_regs *rtc_regs = (struct rtc_regs *)IMX_RTC_BASE;
66 
67 	writel(0, &rtc_regs->dayr);
68 	writel(0, &rtc_regs->hourmin);
69 	writel(0, &rtc_regs->seconds);
70 }
71