blob: ffefb91881ffcf0356cb8ee14fe150338b746064 [file] [log] [blame]
Marek Vasutaa711b12011-11-08 23:18:17 +00001/*
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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23#include <common.h>
24#include <rtc.h>
25#include <asm/io.h>
26#include <asm/arch/imx-regs.h>
27#include <asm/arch/sys_proto.h>
28
29#define MXS_RTC_MAX_TIMEOUT 1000000
30
31/* Set time in seconds since 1970-01-01 */
32int mxs_rtc_set_time(uint32_t secs)
33{
Otavio Salvador9c471142012-08-05 09:05:31 +000034 struct mxs_rtc_regs *rtc_regs = (struct mxs_rtc_regs *)MXS_RTC_BASE;
Marek Vasutaa711b12011-11-08 23:18:17 +000035 int ret;
36
37 writel(secs, &rtc_regs->hw_rtc_seconds);
38
39 /*
40 * The 0x80 here means seconds were copied to analog. This information
41 * is taken from the linux kernel driver for the STMP37xx RTC since
42 * documentation doesn't mention it.
43 */
Otavio Salvadorfa7a51c2012-08-13 09:53:12 +000044 ret = mxs_wait_mask_clr(&rtc_regs->hw_rtc_stat_reg,
Marek Vasutaa711b12011-11-08 23:18:17 +000045 0x80 << RTC_STAT_STALE_REGS_OFFSET, MXS_RTC_MAX_TIMEOUT);
46
47 if (ret)
48 printf("MXS RTC: Timeout waiting for update\n");
49
50 return ret;
51}
52
53int rtc_get(struct rtc_time *time)
54{
Otavio Salvador9c471142012-08-05 09:05:31 +000055 struct mxs_rtc_regs *rtc_regs = (struct mxs_rtc_regs *)MXS_RTC_BASE;
Marek Vasutaa711b12011-11-08 23:18:17 +000056 uint32_t secs;
57
58 secs = readl(&rtc_regs->hw_rtc_seconds);
59 to_tm(secs, time);
60
61 return 0;
62}
63
64int rtc_set(struct rtc_time *time)
65{
66 uint32_t secs;
67
68 secs = mktime(time->tm_year, time->tm_mon, time->tm_mday,
69 time->tm_hour, time->tm_min, time->tm_sec);
70
71 return mxs_rtc_set_time(secs);
72}
73
74void rtc_reset(void)
75{
Otavio Salvador9c471142012-08-05 09:05:31 +000076 struct mxs_rtc_regs *rtc_regs = (struct mxs_rtc_regs *)MXS_RTC_BASE;
Marek Vasutaa711b12011-11-08 23:18:17 +000077 int ret;
78
79 /* Set time to 1970-01-01 */
80 mxs_rtc_set_time(0);
81
82 /* Reset the RTC block */
Otavio Salvadorfa7a51c2012-08-13 09:53:12 +000083 ret = mxs_reset_block(&rtc_regs->hw_rtc_ctrl_reg);
Marek Vasutaa711b12011-11-08 23:18:17 +000084 if (ret)
85 printf("MXS RTC: Block reset timeout\n");
86}