blob: f2a226650fc5edc08a44e5148ea81bf30bb967d0 [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
20int rtc_get(struct rtc_time *t)
21{
22 u32 time;
23 u32 date;
24 struct mvrtc_registers *mvrtc_regs;
25
26 mvrtc_regs = (struct mvrtc_registers *)KW_RTC_BASE;
27
28 /* read the time register */
29 time = readl(&mvrtc_regs->time);
30
31 /* read the date register */
32 date = readl(&mvrtc_regs->date);
33
34 /* test for 12 hour clock (can't tell if it's am/pm) */
35 if (time & MVRTC_HRFMT_MSK) {
36 printf("Error: RTC in 12 hour mode, can't determine AM/PM.\n");
37 return -1;
38 }
39
40 /* time */
41 t->tm_sec = bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK);
42 t->tm_min = bcd2bin((time >> MVRTC_MIN_SFT) & MVRTC_MIN_MSK);
43 t->tm_hour = bcd2bin((time >> MVRTC_HOUR_SFT) & MVRTC_HOUR_MSK);
44 t->tm_wday = bcd2bin((time >> MVRTC_DAY_SFT) & MVRTC_DAY_MSK);
45 t->tm_wday--;
46
47 /* date */
48 t->tm_mday = bcd2bin((date >> MVRTC_DATE_SFT) & MVRTC_DATE_MSK);
49 t->tm_mon = bcd2bin((date >> MVRTC_MON_SFT) & MVRTC_MON_MSK);
50 t->tm_year = bcd2bin((date >> MVRTC_YEAR_SFT) & MVRTC_YEAR_MSK);
51 t->tm_year += CENTURY * 100;
52
53 /* not supported in this RTC */
54 t->tm_yday = 0;
55 t->tm_isdst = 0;
56
57 return 0;
58}
59
60int rtc_set(struct rtc_time *t)
61{
62 u32 time = 0; /* sets hour format bit to zero, 24hr format. */
63 u32 date = 0;
64 struct mvrtc_registers *mvrtc_regs;
65
66 mvrtc_regs = (struct mvrtc_registers *)KW_RTC_BASE;
67
68 /* check that this code isn't 80+ years old ;-) */
69 if ((t->tm_year / 100) != CENTURY)
70 printf("Warning: Only century %d supported.\n", CENTURY);
71
72 /* time */
73 time |= (bin2bcd(t->tm_sec) & MVRTC_SEC_MSK) << MVRTC_SEC_SFT;
74 time |= (bin2bcd(t->tm_min) & MVRTC_MIN_MSK) << MVRTC_MIN_SFT;
75 time |= (bin2bcd(t->tm_hour) & MVRTC_HOUR_MSK) << MVRTC_HOUR_SFT;
76 time |= (bin2bcd(t->tm_wday + 1) & MVRTC_DAY_MSK) << MVRTC_DAY_SFT;
77
78 /* date */
79 date |= (bin2bcd(t->tm_mday) & MVRTC_DATE_MSK) << MVRTC_DATE_SFT;
80 date |= (bin2bcd(t->tm_mon) & MVRTC_MON_MSK) << MVRTC_MON_SFT;
81 date |= (bin2bcd(t->tm_year % 100) & MVRTC_YEAR_MSK) << MVRTC_YEAR_SFT;
82
83 /* write the time register */
84 writel(time, &mvrtc_regs->time);
85
86 /* write the date register */
87 writel(date, &mvrtc_regs->date);
88
89 return 0;
90}
91
92void rtc_reset(void)
93{
94 u32 time;
95 u32 sec;
96 struct mvrtc_registers *mvrtc_regs;
97
98 mvrtc_regs = (struct mvrtc_registers *)KW_RTC_BASE;
99
100 /* no init routine for this RTC needed, just check that it's working */
101 time = readl(&mvrtc_regs->time);
102 sec = bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK);
103 udelay(1000000);
104 time = readl(&mvrtc_regs->time);
105
106 if (sec == bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK))
107 printf("Error: RTC did not increment.\n");
108}