3 * Jason Cooper <u-boot@lakedaemon.net>
5 * SPDX-License-Identifier: GPL-2.0+
9 * Date & Time support for Marvell Integrated RTC
18 /* This RTC does not support century, so we assume 20 */
21 int rtc_get(struct rtc_time *t)
25 struct mvrtc_registers *mvrtc_regs;
27 mvrtc_regs = (struct mvrtc_registers *)KW_RTC_BASE;
29 /* read the time register */
30 time = readl(&mvrtc_regs->time);
32 /* read the date register */
33 date = readl(&mvrtc_regs->date);
35 /* test for 12 hour clock (can't tell if it's am/pm) */
36 if (time & MVRTC_HRFMT_MSK) {
37 printf("Error: RTC in 12 hour mode, can't determine AM/PM.\n");
42 t->tm_sec = bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK);
43 t->tm_min = bcd2bin((time >> MVRTC_MIN_SFT) & MVRTC_MIN_MSK);
44 t->tm_hour = bcd2bin((time >> MVRTC_HOUR_SFT) & MVRTC_HOUR_MSK);
45 t->tm_wday = bcd2bin((time >> MVRTC_DAY_SFT) & MVRTC_DAY_MSK);
49 t->tm_mday = bcd2bin((date >> MVRTC_DATE_SFT) & MVRTC_DATE_MSK);
50 t->tm_mon = bcd2bin((date >> MVRTC_MON_SFT) & MVRTC_MON_MSK);
51 t->tm_year = bcd2bin((date >> MVRTC_YEAR_SFT) & MVRTC_YEAR_MSK);
52 t->tm_year += CENTURY * 100;
54 /* not supported in this RTC */
61 int rtc_set(struct rtc_time *t)
63 u32 time = 0; /* sets hour format bit to zero, 24hr format. */
65 struct mvrtc_registers *mvrtc_regs;
67 mvrtc_regs = (struct mvrtc_registers *)KW_RTC_BASE;
69 /* check that this code isn't 80+ years old ;-) */
70 if ((t->tm_year / 100) != CENTURY)
71 printf("Warning: Only century %d supported.\n", CENTURY);
74 time |= (bin2bcd(t->tm_sec) & MVRTC_SEC_MSK) << MVRTC_SEC_SFT;
75 time |= (bin2bcd(t->tm_min) & MVRTC_MIN_MSK) << MVRTC_MIN_SFT;
76 time |= (bin2bcd(t->tm_hour) & MVRTC_HOUR_MSK) << MVRTC_HOUR_SFT;
77 time |= (bin2bcd(t->tm_wday + 1) & MVRTC_DAY_MSK) << MVRTC_DAY_SFT;
80 date |= (bin2bcd(t->tm_mday) & MVRTC_DATE_MSK) << MVRTC_DATE_SFT;
81 date |= (bin2bcd(t->tm_mon) & MVRTC_MON_MSK) << MVRTC_MON_SFT;
82 date |= (bin2bcd(t->tm_year % 100) & MVRTC_YEAR_MSK) << MVRTC_YEAR_SFT;
84 /* write the time register */
85 writel(time, &mvrtc_regs->time);
87 /* write the date register */
88 writel(date, &mvrtc_regs->date);
97 struct mvrtc_registers *mvrtc_regs;
99 mvrtc_regs = (struct mvrtc_registers *)KW_RTC_BASE;
101 /* no init routine for this RTC needed, just check that it's working */
102 time = readl(&mvrtc_regs->time);
103 sec = bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK);
105 time = readl(&mvrtc_regs->time);
107 if (sec == bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK))
108 printf("Error: RTC did not increment.\n");