blob: 528247ac86e09399dc1ad7f9ddf10cded566edd9 [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
Guennadi Liakhovetski7ce63702008-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-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
Guennadi Liakhovetski7ce63702008-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 Babic43800752011-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-VILLARDd1e23192008-09-01 23:06:23 +020061
Magnus Liljaf3a7bdd2009-11-11 19:56:58 +010062 return 0;
Guennadi Liakhovetski7ce63702008-04-15 14:15:30 +020063}
64
65void rtc_reset(void)
66{
67}