blob: 29ccdf17301b174241feffadd7fb4e92084a4b18 [file] [log] [blame]
trem78befb62012-08-08 07:04:46 +00001/*
2 * Freescale i.MX27 RTC Driver
3 *
4 * Copyright (C) 2012 Philippe Reynes <tremyfr@yahoo.fr>
5 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
trem78befb62012-08-08 07:04:46 +00007 */
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
19int 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
Simon Glass9f9276c2015-04-20 12:37:18 -060033 rtc_to_tm(sec, time);
trem78befb62012-08-08 07:04:46 +000034
35 return 0;
36}
37
38int 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
Simon Glass71420982015-04-20 12:37:19 -060043 sec = rtc_mktime(time);
trem78befb62012-08-08 07:04:46 +000044
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
62void 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}