blob: 3e463368b09c864c7c05e8ef30d983cc8645193a [file] [log] [blame]
Guennadi Liakhovetski7ce63702008-04-15 14:15:30 +02001/*
2 * Copyright (C) 2008, Guennadi Liakhovetski <lg@denx.de>
3 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Guennadi Liakhovetski7ce63702008-04-15 14:15:30 +02005 */
6
7#include <common.h>
8#include <rtc.h>
9#include <spi.h>
Łukasz Majewskic7336812012-11-13 03:21:55 +000010#include <power/pmic.h>
Stefano Babicdfe5e142010-04-16 17:11:19 +020011#include <fsl_pmic.h>
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020012
Guennadi Liakhovetski7ce63702008-04-15 14:15:30 +020013int rtc_get(struct rtc_time *rtc)
14{
15 u32 day1, day2, time;
Stefano Babicdfe5e142010-04-16 17:11:19 +020016 int tim, i = 0;
Łukasz Majewskic7336812012-11-13 03:21:55 +000017 struct pmic *p = pmic_get("FSL_PMIC");
Stefano Babic43800752011-10-02 18:38:43 +020018 int ret;
Guennadi Liakhovetski7ce63702008-04-15 14:15:30 +020019
Łukasz Majewskic7336812012-11-13 03:21:55 +000020 if (!p)
21 return -1;
Guennadi Liakhovetski7ce63702008-04-15 14:15:30 +020022 do {
Stefano Babic43800752011-10-02 18:38:43 +020023 ret = pmic_reg_read(p, REG_RTC_DAY, &day1);
24 if (ret < 0)
Stefano Babicdfe5e142010-04-16 17:11:19 +020025 return -1;
Guennadi Liakhovetski7ce63702008-04-15 14:15:30 +020026
Stefano Babic43800752011-10-02 18:38:43 +020027 ret = pmic_reg_read(p, REG_RTC_TIME, &time);
28 if (ret < 0)
Stefano Babicdfe5e142010-04-16 17:11:19 +020029 return -1;
Guennadi Liakhovetski7ce63702008-04-15 14:15:30 +020030
Stefano Babic43800752011-10-02 18:38:43 +020031 ret = pmic_reg_read(p, REG_RTC_DAY, &day2);
32 if (ret < 0)
Stefano Babicdfe5e142010-04-16 17:11:19 +020033 return -1;
Guennadi Liakhovetski7ce63702008-04-15 14:15:30 +020034
Guennadi Liakhovetski7ce63702008-04-15 14:15:30 +020035 } while (day1 != day2 && i++ < 3);
36
37 tim = day1 * 86400 + time;
Stefano Babicdfe5e142010-04-16 17:11:19 +020038
Simon Glass9f9276c2015-04-20 12:37:18 -060039 rtc_to_tm(tim, rtc);
Guennadi Liakhovetski7ce63702008-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-VILLARDd1e23192008-09-01 23:06:23 +020047int rtc_set(struct rtc_time *rtc)
Guennadi Liakhovetski7ce63702008-04-15 14:15:30 +020048{
Stefano Babicdfe5e142010-04-16 17:11:19 +020049 u32 time, day;
Łukasz Majewskic7336812012-11-13 03:21:55 +000050 struct pmic *p = pmic_get("FSL_PMIC");
51 if (!p)
52 return -1;
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020053
Simon Glass71420982015-04-20 12:37:19 -060054 time = rtc_mktime(rtc);
Guennadi Liakhovetski7ce63702008-04-15 14:15:30 +020055 day = time / 86400;
56 time %= 86400;
57
Stefano Babic43800752011-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-VILLARDd1e23192008-09-01 23:06:23 +020060
Magnus Liljaf3a7bdd2009-11-11 19:56:58 +010061 return 0;
Guennadi Liakhovetski7ce63702008-04-15 14:15:30 +020062}
63
64void rtc_reset(void)
65{
66}