blob: f7bf95c30292fd93d298f7588020835607756f01 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jason Cooperb608b952011-08-04 21:26:16 +05302/*
3 * Copyright (C) 2011
4 * Jason Cooper <u-boot@lakedaemon.net>
Jason Cooperb608b952011-08-04 21:26:16 +05305 */
6
7/*
8 * Date & Time support for Marvell Integrated RTC
9 */
10
11#include <common.h>
12#include <command.h>
13#include <rtc.h>
Anatolij Gustschin0ac16bf2011-10-29 11:19:47 +000014#include <asm/io.h>
Jason Cooperb608b952011-08-04 21:26:16 +053015#include "mvrtc.h"
16
17/* This RTC does not support century, so we assume 20 */
18#define CENTURY 20
19
Chris Packham942bb6e2018-05-28 23:39:57 +120020static int __mv_rtc_get(struct mvrtc_registers *regs, struct rtc_time *t)
Jason Cooperb608b952011-08-04 21:26:16 +053021{
22 u32 time;
23 u32 date;
Jason Cooperb608b952011-08-04 21:26:16 +053024
25 /* read the time register */
Chris Packham942bb6e2018-05-28 23:39:57 +120026 time = readl(&regs->time);
Jason Cooperb608b952011-08-04 21:26:16 +053027
28 /* read the date register */
Chris Packham942bb6e2018-05-28 23:39:57 +120029 date = readl(&regs->date);
Jason Cooperb608b952011-08-04 21:26:16 +053030
31 /* test for 12 hour clock (can't tell if it's am/pm) */
32 if (time & MVRTC_HRFMT_MSK) {
33 printf("Error: RTC in 12 hour mode, can't determine AM/PM.\n");
34 return -1;
35 }
36
37 /* time */
38 t->tm_sec = bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK);
39 t->tm_min = bcd2bin((time >> MVRTC_MIN_SFT) & MVRTC_MIN_MSK);
40 t->tm_hour = bcd2bin((time >> MVRTC_HOUR_SFT) & MVRTC_HOUR_MSK);
41 t->tm_wday = bcd2bin((time >> MVRTC_DAY_SFT) & MVRTC_DAY_MSK);
42 t->tm_wday--;
43
44 /* date */
45 t->tm_mday = bcd2bin((date >> MVRTC_DATE_SFT) & MVRTC_DATE_MSK);
46 t->tm_mon = bcd2bin((date >> MVRTC_MON_SFT) & MVRTC_MON_MSK);
47 t->tm_year = bcd2bin((date >> MVRTC_YEAR_SFT) & MVRTC_YEAR_MSK);
48 t->tm_year += CENTURY * 100;
49
50 /* not supported in this RTC */
51 t->tm_yday = 0;
52 t->tm_isdst = 0;
53
54 return 0;
55}
56
Chris Packham942bb6e2018-05-28 23:39:57 +120057#ifndef CONFIG_DM_RTC
58int rtc_get(struct rtc_time *t)
59{
60 struct mvrtc_registers *regs;
61
62 regs = (struct mvrtc_registers *)KW_RTC_BASE;
63 return __mv_rtc_get(regs, t);
64}
65#endif /* !CONFIG_DM_RTC */
66
67static int __mv_rtc_set(struct mvrtc_registers *regs, const struct rtc_time *t)
Jason Cooperb608b952011-08-04 21:26:16 +053068{
69 u32 time = 0; /* sets hour format bit to zero, 24hr format. */
70 u32 date = 0;
Jason Cooperb608b952011-08-04 21:26:16 +053071
72 /* check that this code isn't 80+ years old ;-) */
73 if ((t->tm_year / 100) != CENTURY)
74 printf("Warning: Only century %d supported.\n", CENTURY);
75
76 /* time */
77 time |= (bin2bcd(t->tm_sec) & MVRTC_SEC_MSK) << MVRTC_SEC_SFT;
78 time |= (bin2bcd(t->tm_min) & MVRTC_MIN_MSK) << MVRTC_MIN_SFT;
79 time |= (bin2bcd(t->tm_hour) & MVRTC_HOUR_MSK) << MVRTC_HOUR_SFT;
80 time |= (bin2bcd(t->tm_wday + 1) & MVRTC_DAY_MSK) << MVRTC_DAY_SFT;
81
82 /* date */
83 date |= (bin2bcd(t->tm_mday) & MVRTC_DATE_MSK) << MVRTC_DATE_SFT;
84 date |= (bin2bcd(t->tm_mon) & MVRTC_MON_MSK) << MVRTC_MON_SFT;
85 date |= (bin2bcd(t->tm_year % 100) & MVRTC_YEAR_MSK) << MVRTC_YEAR_SFT;
86
87 /* write the time register */
Chris Packham942bb6e2018-05-28 23:39:57 +120088 writel(time, &regs->time);
Jason Cooperb608b952011-08-04 21:26:16 +053089
90 /* write the date register */
Chris Packham942bb6e2018-05-28 23:39:57 +120091 writel(date, &regs->date);
Jason Cooperb608b952011-08-04 21:26:16 +053092
93 return 0;
94}
95
Chris Packham942bb6e2018-05-28 23:39:57 +120096#ifndef CONFIG_DM_RTC
97int rtc_set(struct rtc_time *t)
98{
99 struct mvrtc_registers *regs;
100
101 regs = (struct mvrtc_registers *)KW_RTC_BASE;
102 return __mv_rtc_set(regs, t);
103}
104#endif /* !CONFIG_DM_RTC */
105
106static void __mv_rtc_reset(struct mvrtc_registers *regs)
Jason Cooperb608b952011-08-04 21:26:16 +0530107{
108 u32 time;
109 u32 sec;
Jason Cooperb608b952011-08-04 21:26:16 +0530110
111 /* no init routine for this RTC needed, just check that it's working */
Chris Packham942bb6e2018-05-28 23:39:57 +1200112 time = readl(&regs->time);
Jason Cooperb608b952011-08-04 21:26:16 +0530113 sec = bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK);
114 udelay(1000000);
Chris Packham942bb6e2018-05-28 23:39:57 +1200115 time = readl(&regs->time);
Jason Cooperb608b952011-08-04 21:26:16 +0530116
117 if (sec == bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK))
118 printf("Error: RTC did not increment.\n");
119}
Chris Packham942bb6e2018-05-28 23:39:57 +1200120
121#ifndef CONFIG_DM_RTC
122void rtc_reset(void)
123{
124 struct mvrtc_registers *regs;
125
126 regs = (struct mvrtc_registers *)KW_RTC_BASE;
127 __mv_rtc_reset(regs);
128}
129#endif /* !CONFIG_DM_RTC */