summaryrefslogtreecommitdiffstats
path: root/qemu/roms/u-boot/drivers/rtc/mx27rtc.c
blob: ae6595b86037744f2153051410c6d4c215132add (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
 * Freescale i.MX27 RTC Driver
 *
 * Copyright (C) 2012 Philippe Reynes <tremyfr@yahoo.fr>
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include <common.h>
#include <rtc.h>
#include <asm/io.h>
#include <asm/arch/imx-regs.h>

#define HOUR_SHIFT 8
#define HOUR_MASK  0x1f
#define MIN_SHIFT  0
#define MIN_MASK   0x3f

int rtc_get(struct rtc_time *time)
{
	struct rtc_regs *rtc_regs = (struct rtc_regs *)IMX_RTC_BASE;
	uint32_t day, hour, min, sec;

	day  = readl(&rtc_regs->dayr);
	hour = readl(&rtc_regs->hourmin);
	sec  = readl(&rtc_regs->seconds);

	min  = (hour >> MIN_SHIFT) & MIN_MASK;
	hour = (hour >> HOUR_SHIFT) & HOUR_MASK;

	sec += min * 60 + hour * 3600 + day * 24 * 3600;

	to_tm(sec, time);

	return 0;
}

int rtc_set(struct rtc_time *time)
{
	struct rtc_regs *rtc_regs = (struct rtc_regs *)IMX_RTC_BASE;
	uint32_t day, hour, min, sec;

	sec = mktime(time->tm_year, time->tm_mon, time->tm_mday,
		time->tm_hour, time->tm_min, time->tm_sec);

	day  = sec / (24 * 3600);
	sec  = sec % (24 * 3600);
	hour = sec / 3600;
	sec  = sec % 3600;
	min  = sec / 60;
	sec  = sec % 60;

	hour  = (hour & HOUR_MASK) << HOUR_SHIFT;
	hour |= (min & MIN_MASK) << MIN_SHIFT;

	writel(day, &rtc_regs->dayr);
	writel(hour, &rtc_regs->hourmin);
	writel(sec, &rtc_regs->seconds);

	return 0;
}

void rtc_reset(void)
{
	struct rtc_regs *rtc_regs = (struct rtc_regs *)IMX_RTC_BASE;

	writel(0, &rtc_regs->dayr);
	writel(0, &rtc_regs->hourmin);
	writel(0, &rtc_regs->seconds);
}