Guennadi Liakhovetski | 7ce6370 | 2008-04-15 14:15:30 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008, Guennadi Liakhovetski <lg@denx.de> |
| 3 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Guennadi Liakhovetski | 7ce6370 | 2008-04-15 14:15:30 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <rtc.h> |
| 9 | #include <spi.h> |
Łukasz Majewski | c733681 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 10 | #include <power/pmic.h> |
Stefano Babic | dfe5e14 | 2010-04-16 17:11:19 +0200 | [diff] [blame] | 11 | #include <fsl_pmic.h> |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 12 | |
Guennadi Liakhovetski | 7ce6370 | 2008-04-15 14:15:30 +0200 | [diff] [blame] | 13 | int rtc_get(struct rtc_time *rtc) |
| 14 | { |
| 15 | u32 day1, day2, time; |
Stefano Babic | dfe5e14 | 2010-04-16 17:11:19 +0200 | [diff] [blame] | 16 | int tim, i = 0; |
Łukasz Majewski | c733681 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 17 | struct pmic *p = pmic_get("FSL_PMIC"); |
Stefano Babic | 4380075 | 2011-10-02 18:38:43 +0200 | [diff] [blame] | 18 | int ret; |
Guennadi Liakhovetski | 7ce6370 | 2008-04-15 14:15:30 +0200 | [diff] [blame] | 19 | |
Łukasz Majewski | c733681 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 20 | if (!p) |
| 21 | return -1; |
Guennadi Liakhovetski | 7ce6370 | 2008-04-15 14:15:30 +0200 | [diff] [blame] | 22 | do { |
Stefano Babic | 4380075 | 2011-10-02 18:38:43 +0200 | [diff] [blame] | 23 | ret = pmic_reg_read(p, REG_RTC_DAY, &day1); |
| 24 | if (ret < 0) |
Stefano Babic | dfe5e14 | 2010-04-16 17:11:19 +0200 | [diff] [blame] | 25 | return -1; |
Guennadi Liakhovetski | 7ce6370 | 2008-04-15 14:15:30 +0200 | [diff] [blame] | 26 | |
Stefano Babic | 4380075 | 2011-10-02 18:38:43 +0200 | [diff] [blame] | 27 | ret = pmic_reg_read(p, REG_RTC_TIME, &time); |
| 28 | if (ret < 0) |
Stefano Babic | dfe5e14 | 2010-04-16 17:11:19 +0200 | [diff] [blame] | 29 | return -1; |
Guennadi Liakhovetski | 7ce6370 | 2008-04-15 14:15:30 +0200 | [diff] [blame] | 30 | |
Stefano Babic | 4380075 | 2011-10-02 18:38:43 +0200 | [diff] [blame] | 31 | ret = pmic_reg_read(p, REG_RTC_DAY, &day2); |
| 32 | if (ret < 0) |
Stefano Babic | dfe5e14 | 2010-04-16 17:11:19 +0200 | [diff] [blame] | 33 | return -1; |
Guennadi Liakhovetski | 7ce6370 | 2008-04-15 14:15:30 +0200 | [diff] [blame] | 34 | |
Guennadi Liakhovetski | 7ce6370 | 2008-04-15 14:15:30 +0200 | [diff] [blame] | 35 | } while (day1 != day2 && i++ < 3); |
| 36 | |
| 37 | tim = day1 * 86400 + time; |
Stefano Babic | dfe5e14 | 2010-04-16 17:11:19 +0200 | [diff] [blame] | 38 | |
Simon Glass | 9f9276c | 2015-04-20 12:37:18 -0600 | [diff] [blame] | 39 | rtc_to_tm(tim, rtc); |
Guennadi Liakhovetski | 7ce6370 | 2008-04-15 14:15:30 +0200 | [diff] [blame] | 40 | |
| 41 | rtc->tm_yday = 0; |
| 42 | rtc->tm_isdst = 0; |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
Jean-Christophe PLAGNIOL-VILLARD | d1e2319 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 47 | int rtc_set(struct rtc_time *rtc) |
Guennadi Liakhovetski | 7ce6370 | 2008-04-15 14:15:30 +0200 | [diff] [blame] | 48 | { |
Stefano Babic | dfe5e14 | 2010-04-16 17:11:19 +0200 | [diff] [blame] | 49 | u32 time, day; |
Łukasz Majewski | c733681 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 50 | struct pmic *p = pmic_get("FSL_PMIC"); |
| 51 | if (!p) |
| 52 | return -1; |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 53 | |
Simon Glass | 7142098 | 2015-04-20 12:37:19 -0600 | [diff] [blame] | 54 | time = rtc_mktime(rtc); |
Guennadi Liakhovetski | 7ce6370 | 2008-04-15 14:15:30 +0200 | [diff] [blame] | 55 | day = time / 86400; |
| 56 | time %= 86400; |
| 57 | |
Stefano Babic | 4380075 | 2011-10-02 18:38:43 +0200 | [diff] [blame] | 58 | pmic_reg_write(p, REG_RTC_DAY, day); |
| 59 | pmic_reg_write(p, REG_RTC_TIME, time); |
Jean-Christophe PLAGNIOL-VILLARD | d1e2319 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 60 | |
Magnus Lilja | f3a7bdd | 2009-11-11 19:56:58 +0100 | [diff] [blame] | 61 | return 0; |
Guennadi Liakhovetski | 7ce6370 | 2008-04-15 14:15:30 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void rtc_reset(void) |
| 65 | { |
| 66 | } |