blob: 528247ac86e09399dc1ad7f9ddf10cded566edd9 [file] [log] [blame]
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +02001/*
2 * Copyright (C) 2008, Guennadi Liakhovetski <lg@denx.de>
3 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +02005 */
6
7#include <common.h>
8#include <rtc.h>
9#include <spi.h>
Łukasz Majewski1c6dba12012-11-13 03:21:55 +000010#include <power/pmic.h>
Stefano Babice0432032010-04-16 17:11:19 +020011#include <fsl_pmic.h>
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020012
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020013int rtc_get(struct rtc_time *rtc)
14{
15 u32 day1, day2, time;
Stefano Babice0432032010-04-16 17:11:19 +020016 int tim, i = 0;
Łukasz Majewski1c6dba12012-11-13 03:21:55 +000017 struct pmic *p = pmic_get("FSL_PMIC");
Stefano Babic07735d12011-10-02 18:38:43 +020018 int ret;
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020019
Łukasz Majewski1c6dba12012-11-13 03:21:55 +000020 if (!p)
21 return -1;
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020022 do {
Stefano Babic07735d12011-10-02 18:38:43 +020023 ret = pmic_reg_read(p, REG_RTC_DAY, &day1);
24 if (ret < 0)
Stefano Babice0432032010-04-16 17:11:19 +020025 return -1;
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020026
Stefano Babic07735d12011-10-02 18:38:43 +020027 ret = pmic_reg_read(p, REG_RTC_TIME, &time);
28 if (ret < 0)
Stefano Babice0432032010-04-16 17:11:19 +020029 return -1;
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020030
Stefano Babic07735d12011-10-02 18:38:43 +020031 ret = pmic_reg_read(p, REG_RTC_DAY, &day2);
32 if (ret < 0)
Stefano Babice0432032010-04-16 17:11:19 +020033 return -1;
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020034
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020035 } while (day1 != day2 && i++ < 3);
36
37 tim = day1 * 86400 + time;
Stefano Babice0432032010-04-16 17:11:19 +020038
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020039 to_tm(tim, rtc);
40
41 rtc->tm_yday = 0;
42 rtc->tm_isdst = 0;
43
44 return 0;
45}
46
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +020047int rtc_set(struct rtc_time *rtc)
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020048{
Stefano Babice0432032010-04-16 17:11:19 +020049 u32 time, day;
Łukasz Majewski1c6dba12012-11-13 03:21:55 +000050 struct pmic *p = pmic_get("FSL_PMIC");
51 if (!p)
52 return -1;
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020053
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020054 time = mktime(rtc->tm_year, rtc->tm_mon, rtc->tm_mday,
55 rtc->tm_hour, rtc->tm_min, rtc->tm_sec);
56 day = time / 86400;
57 time %= 86400;
58
Stefano Babic07735d12011-10-02 18:38:43 +020059 pmic_reg_write(p, REG_RTC_DAY, day);
60 pmic_reg_write(p, REG_RTC_TIME, time);
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +020061
Magnus Liljade7c7a72009-11-11 19:56:58 +010062 return 0;
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020063}
64
65void rtc_reset(void)
66{
67}