]> git.ipfire.org Git - people/ms/u-boot.git/blame - drivers/rtc/mxsrtc.c
dm: rtc: Rename to_tm() to rtc_to_tm() and add error code
[people/ms/u-boot.git] / drivers / rtc / mxsrtc.c
CommitLineData
aa711b11
MV
1/*
2 * Freescale i.MX28 RTC Driver
3 *
4 * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
5 * on behalf of DENX Software Engineering GmbH
6 *
1a459660 7 * SPDX-License-Identifier: GPL-2.0+
aa711b11
MV
8 */
9
10#include <common.h>
11#include <rtc.h>
12#include <asm/io.h>
13#include <asm/arch/imx-regs.h>
14#include <asm/arch/sys_proto.h>
15
16#define MXS_RTC_MAX_TIMEOUT 1000000
17
18/* Set time in seconds since 1970-01-01 */
19int mxs_rtc_set_time(uint32_t secs)
20{
9c471142 21 struct mxs_rtc_regs *rtc_regs = (struct mxs_rtc_regs *)MXS_RTC_BASE;
aa711b11
MV
22 int ret;
23
24 writel(secs, &rtc_regs->hw_rtc_seconds);
25
26 /*
27 * The 0x80 here means seconds were copied to analog. This information
28 * is taken from the linux kernel driver for the STMP37xx RTC since
29 * documentation doesn't mention it.
30 */
fa7a51cb 31 ret = mxs_wait_mask_clr(&rtc_regs->hw_rtc_stat_reg,
aa711b11
MV
32 0x80 << RTC_STAT_STALE_REGS_OFFSET, MXS_RTC_MAX_TIMEOUT);
33
34 if (ret)
35 printf("MXS RTC: Timeout waiting for update\n");
36
37 return ret;
38}
39
40int rtc_get(struct rtc_time *time)
41{
9c471142 42 struct mxs_rtc_regs *rtc_regs = (struct mxs_rtc_regs *)MXS_RTC_BASE;
aa711b11
MV
43 uint32_t secs;
44
45 secs = readl(&rtc_regs->hw_rtc_seconds);
9f9276c3 46 rtc_to_tm(secs, time);
aa711b11
MV
47
48 return 0;
49}
50
51int rtc_set(struct rtc_time *time)
52{
53 uint32_t secs;
54
55 secs = mktime(time->tm_year, time->tm_mon, time->tm_mday,
56 time->tm_hour, time->tm_min, time->tm_sec);
57
58 return mxs_rtc_set_time(secs);
59}
60
61void rtc_reset(void)
62{
9c471142 63 struct mxs_rtc_regs *rtc_regs = (struct mxs_rtc_regs *)MXS_RTC_BASE;
aa711b11
MV
64 int ret;
65
66 /* Set time to 1970-01-01 */
67 mxs_rtc_set_time(0);
68
69 /* Reset the RTC block */
fa7a51cb 70 ret = mxs_reset_block(&rtc_regs->hw_rtc_ctrl_reg);
aa711b11
MV
71 if (ret)
72 printf("MXS RTC: Block reset timeout\n");
73}