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