blob: 6c2aef89758bbf5a65ef79fa91ebbcc5f9fed195 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +02002/*
3 * Copyright (C) 2008, Guennadi Liakhovetski <lg@denx.de>
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +02004 */
5
6#include <common.h>
7#include <rtc.h>
8#include <spi.h>
Łukasz Majewski1c6dba12012-11-13 03:21:55 +00009#include <power/pmic.h>
Stefano Babice0432032010-04-16 17:11:19 +020010#include <fsl_pmic.h>
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020011
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020012int rtc_get(struct rtc_time *rtc)
13{
14 u32 day1, day2, time;
Stefano Babice0432032010-04-16 17:11:19 +020015 int tim, i = 0;
Łukasz Majewski1c6dba12012-11-13 03:21:55 +000016 struct pmic *p = pmic_get("FSL_PMIC");
Stefano Babic07735d12011-10-02 18:38:43 +020017 int ret;
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020018
Łukasz Majewski1c6dba12012-11-13 03:21:55 +000019 if (!p)
20 return -1;
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020021 do {
Stefano Babic07735d12011-10-02 18:38:43 +020022 ret = pmic_reg_read(p, REG_RTC_DAY, &day1);
23 if (ret < 0)
Stefano Babice0432032010-04-16 17:11:19 +020024 return -1;
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020025
Stefano Babic07735d12011-10-02 18:38:43 +020026 ret = pmic_reg_read(p, REG_RTC_TIME, &time);
27 if (ret < 0)
Stefano Babice0432032010-04-16 17:11:19 +020028 return -1;
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020029
Stefano Babic07735d12011-10-02 18:38:43 +020030 ret = pmic_reg_read(p, REG_RTC_DAY, &day2);
31 if (ret < 0)
Stefano Babice0432032010-04-16 17:11:19 +020032 return -1;
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020033
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020034 } while (day1 != day2 && i++ < 3);
35
36 tim = day1 * 86400 + time;
Stefano Babice0432032010-04-16 17:11:19 +020037
Simon Glass12994662015-04-20 12:37:18 -060038 rtc_to_tm(tim, rtc);
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020039
40 rtc->tm_yday = 0;
41 rtc->tm_isdst = 0;
42
43 return 0;
44}
45
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +020046int rtc_set(struct rtc_time *rtc)
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020047{
Stefano Babice0432032010-04-16 17:11:19 +020048 u32 time, day;
Łukasz Majewski1c6dba12012-11-13 03:21:55 +000049 struct pmic *p = pmic_get("FSL_PMIC");
50 if (!p)
51 return -1;
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020052
Simon Glass4283e842015-04-20 12:37:19 -060053 time = rtc_mktime(rtc);
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020054 day = time / 86400;
55 time %= 86400;
56
Stefano Babic07735d12011-10-02 18:38:43 +020057 pmic_reg_write(p, REG_RTC_DAY, day);
58 pmic_reg_write(p, REG_RTC_TIME, time);
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +020059
Magnus Liljade7c7a72009-11-11 19:56:58 +010060 return 0;
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020061}
62
63void rtc_reset(void)
64{
65}