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