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