blob: 3e463368b09c864c7c05e8ef30d983cc8645193a [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
Simon Glass12994662015-04-20 12:37:18 -060039 rtc_to_tm(tim, rtc);
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020040
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
Simon Glass4283e842015-04-20 12:37:19 -060054 time = rtc_mktime(rtc);
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020055 day = time / 86400;
56 time %= 86400;
57
Stefano Babic07735d12011-10-02 18:38:43 +020058 pmic_reg_write(p, REG_RTC_DAY, day);
59 pmic_reg_write(p, REG_RTC_TIME, time);
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +020060
Magnus Liljade7c7a72009-11-11 19:56:58 +010061 return 0;
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020062}
63
64void rtc_reset(void)
65{
66}