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