Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2011 |
| 4 | * Jason Cooper <u-boot@lakedaemon.net> |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 5 | */ |
| 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 Gustschin | 0ac16bf | 2011-10-29 11:19:47 +0000 | [diff] [blame] | 14 | #include <asm/io.h> |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 15 | #include "mvrtc.h" |
| 16 | |
| 17 | /* This RTC does not support century, so we assume 20 */ |
| 18 | #define CENTURY 20 |
| 19 | |
Chris Packham | 942bb6e | 2018-05-28 23:39:57 +1200 | [diff] [blame^] | 20 | static int __mv_rtc_get(struct mvrtc_registers *regs, struct rtc_time *t) |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 21 | { |
| 22 | u32 time; |
| 23 | u32 date; |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 24 | |
| 25 | /* read the time register */ |
Chris Packham | 942bb6e | 2018-05-28 23:39:57 +1200 | [diff] [blame^] | 26 | time = readl(®s->time); |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 27 | |
| 28 | /* read the date register */ |
Chris Packham | 942bb6e | 2018-05-28 23:39:57 +1200 | [diff] [blame^] | 29 | date = readl(®s->date); |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 30 | |
| 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 Packham | 942bb6e | 2018-05-28 23:39:57 +1200 | [diff] [blame^] | 57 | #ifndef CONFIG_DM_RTC |
| 58 | int 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 | |
| 67 | static int __mv_rtc_set(struct mvrtc_registers *regs, const struct rtc_time *t) |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 68 | { |
| 69 | u32 time = 0; /* sets hour format bit to zero, 24hr format. */ |
| 70 | u32 date = 0; |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 71 | |
| 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 Packham | 942bb6e | 2018-05-28 23:39:57 +1200 | [diff] [blame^] | 88 | writel(time, ®s->time); |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 89 | |
| 90 | /* write the date register */ |
Chris Packham | 942bb6e | 2018-05-28 23:39:57 +1200 | [diff] [blame^] | 91 | writel(date, ®s->date); |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
Chris Packham | 942bb6e | 2018-05-28 23:39:57 +1200 | [diff] [blame^] | 96 | #ifndef CONFIG_DM_RTC |
| 97 | int 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 | |
| 106 | static void __mv_rtc_reset(struct mvrtc_registers *regs) |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 107 | { |
| 108 | u32 time; |
| 109 | u32 sec; |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 110 | |
| 111 | /* no init routine for this RTC needed, just check that it's working */ |
Chris Packham | 942bb6e | 2018-05-28 23:39:57 +1200 | [diff] [blame^] | 112 | time = readl(®s->time); |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 113 | sec = bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK); |
| 114 | udelay(1000000); |
Chris Packham | 942bb6e | 2018-05-28 23:39:57 +1200 | [diff] [blame^] | 115 | time = readl(®s->time); |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 116 | |
| 117 | if (sec == bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK)) |
| 118 | printf("Error: RTC did not increment.\n"); |
| 119 | } |
Chris Packham | 942bb6e | 2018-05-28 23:39:57 +1200 | [diff] [blame^] | 120 | |
| 121 | #ifndef CONFIG_DM_RTC |
| 122 | void 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 */ |