Marek Vasut | 8648726 | 2011-11-08 23:18:17 +0000 | [diff] [blame] | 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 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | 8648726 | 2011-11-08 23:18:17 +0000 | [diff] [blame] | 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 */ |
| 19 | int mxs_rtc_set_time(uint32_t secs) |
| 20 | { |
Otavio Salvador | 22f4ff9 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 21 | 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] | 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 | */ |
Otavio Salvador | cbf0bf2 | 2012-08-13 09:53:12 +0000 | [diff] [blame] | 31 | ret = mxs_wait_mask_clr(&rtc_regs->hw_rtc_stat_reg, |
Marek Vasut | 8648726 | 2011-11-08 23:18:17 +0000 | [diff] [blame] | 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 | |
| 40 | int rtc_get(struct rtc_time *time) |
| 41 | { |
Otavio Salvador | 22f4ff9 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 42 | 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] | 43 | uint32_t secs; |
| 44 | |
| 45 | secs = readl(&rtc_regs->hw_rtc_seconds); |
Simon Glass | 1299466 | 2015-04-20 12:37:18 -0600 | [diff] [blame] | 46 | rtc_to_tm(secs, time); |
Marek Vasut | 8648726 | 2011-11-08 23:18:17 +0000 | [diff] [blame] | 47 | |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | int rtc_set(struct rtc_time *time) |
| 52 | { |
| 53 | uint32_t secs; |
| 54 | |
Simon Glass | 4283e84 | 2015-04-20 12:37:19 -0600 | [diff] [blame] | 55 | secs = rtc_mktime(time); |
Marek Vasut | 8648726 | 2011-11-08 23:18:17 +0000 | [diff] [blame] | 56 | |
| 57 | return mxs_rtc_set_time(secs); |
| 58 | } |
| 59 | |
| 60 | void rtc_reset(void) |
| 61 | { |
Otavio Salvador | 22f4ff9 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 62 | 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] | 63 | int ret; |
| 64 | |
| 65 | /* Set time to 1970-01-01 */ |
| 66 | mxs_rtc_set_time(0); |
| 67 | |
| 68 | /* Reset the RTC block */ |
Otavio Salvador | cbf0bf2 | 2012-08-13 09:53:12 +0000 | [diff] [blame] | 69 | ret = mxs_reset_block(&rtc_regs->hw_rtc_ctrl_reg); |
Marek Vasut | 8648726 | 2011-11-08 23:18:17 +0000 | [diff] [blame] | 70 | if (ret) |
| 71 | printf("MXS RTC: Block reset timeout\n"); |
| 72 | } |