1 // SPDX-License-Identifier: GPL-2.0+
4 * Jason Cooper <u-boot@lakedaemon.net>
8 * Date & Time support for Marvell Integrated RTC
17 /* This RTC does not support century, so we assume 20 */
20 int rtc_get(struct rtc_time *t)
24 struct mvrtc_registers *mvrtc_regs;
26 mvrtc_regs = (struct mvrtc_registers *)KW_RTC_BASE;
28 /* read the time register */
29 time = readl(&mvrtc_regs->time);
31 /* read the date register */
32 date = readl(&mvrtc_regs->date);
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");
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);
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;
53 /* not supported in this RTC */
60 int rtc_set(struct rtc_time *t)
62 u32 time = 0; /* sets hour format bit to zero, 24hr format. */
64 struct mvrtc_registers *mvrtc_regs;
66 mvrtc_regs = (struct mvrtc_registers *)KW_RTC_BASE;
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);
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;
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;
83 /* write the time register */
84 writel(time, &mvrtc_regs->time);
86 /* write the date register */
87 writel(date, &mvrtc_regs->date);
96 struct mvrtc_registers *mvrtc_regs;
98 mvrtc_regs = (struct mvrtc_registers *)KW_RTC_BASE;
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);
104 time = readl(&mvrtc_regs->time);
106 if (sec == bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK))
107 printf("Error: RTC did not increment.\n");