Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
trem | 78befb6 | 2012-08-08 07:04:46 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Freescale i.MX27 RTC Driver |
| 4 | * |
| 5 | * Copyright (C) 2012 Philippe Reynes <tremyfr@yahoo.fr> |
trem | 78befb6 | 2012-08-08 07:04:46 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <rtc.h> |
| 10 | #include <asm/io.h> |
| 11 | #include <asm/arch/imx-regs.h> |
| 12 | |
| 13 | #define HOUR_SHIFT 8 |
| 14 | #define HOUR_MASK 0x1f |
| 15 | #define MIN_SHIFT 0 |
| 16 | #define MIN_MASK 0x3f |
| 17 | |
| 18 | int rtc_get(struct rtc_time *time) |
| 19 | { |
| 20 | struct rtc_regs *rtc_regs = (struct rtc_regs *)IMX_RTC_BASE; |
| 21 | uint32_t day, hour, min, sec; |
| 22 | |
| 23 | day = readl(&rtc_regs->dayr); |
| 24 | hour = readl(&rtc_regs->hourmin); |
| 25 | sec = readl(&rtc_regs->seconds); |
| 26 | |
| 27 | min = (hour >> MIN_SHIFT) & MIN_MASK; |
| 28 | hour = (hour >> HOUR_SHIFT) & HOUR_MASK; |
| 29 | |
| 30 | sec += min * 60 + hour * 3600 + day * 24 * 3600; |
| 31 | |
Simon Glass | 9f9276c | 2015-04-20 12:37:18 -0600 | [diff] [blame] | 32 | rtc_to_tm(sec, time); |
trem | 78befb6 | 2012-08-08 07:04:46 +0000 | [diff] [blame] | 33 | |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | int rtc_set(struct rtc_time *time) |
| 38 | { |
| 39 | struct rtc_regs *rtc_regs = (struct rtc_regs *)IMX_RTC_BASE; |
| 40 | uint32_t day, hour, min, sec; |
| 41 | |
Simon Glass | 7142098 | 2015-04-20 12:37:19 -0600 | [diff] [blame] | 42 | sec = rtc_mktime(time); |
trem | 78befb6 | 2012-08-08 07:04:46 +0000 | [diff] [blame] | 43 | |
| 44 | day = sec / (24 * 3600); |
| 45 | sec = sec % (24 * 3600); |
| 46 | hour = sec / 3600; |
| 47 | sec = sec % 3600; |
| 48 | min = sec / 60; |
| 49 | sec = sec % 60; |
| 50 | |
| 51 | hour = (hour & HOUR_MASK) << HOUR_SHIFT; |
| 52 | hour |= (min & MIN_MASK) << MIN_SHIFT; |
| 53 | |
| 54 | writel(day, &rtc_regs->dayr); |
| 55 | writel(hour, &rtc_regs->hourmin); |
| 56 | writel(sec, &rtc_regs->seconds); |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | void rtc_reset(void) |
| 62 | { |
Chris Packham | fbd8179 | 2018-03-21 15:40:35 +1300 | [diff] [blame] | 63 | /* nothing to do */ |
trem | 78befb6 | 2012-08-08 07:04:46 +0000 | [diff] [blame] | 64 | } |