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> |
Chris Packham | d3671df | 2018-05-28 23:39:58 +1200 | [diff] [blame] | 13 | #include <dm.h> |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 14 | #include <rtc.h> |
Anatolij Gustschin | 0ac16bf | 2011-10-29 11:19:47 +0000 | [diff] [blame] | 15 | #include <asm/io.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 16 | #include <linux/delay.h> |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 17 | #include "mvrtc.h" |
| 18 | |
| 19 | /* This RTC does not support century, so we assume 20 */ |
| 20 | #define CENTURY 20 |
| 21 | |
Chris Packham | 942bb6e | 2018-05-28 23:39:57 +1200 | [diff] [blame] | 22 | 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] | 23 | { |
| 24 | u32 time; |
| 25 | u32 date; |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 26 | |
| 27 | /* read the time register */ |
Chris Packham | 942bb6e | 2018-05-28 23:39:57 +1200 | [diff] [blame] | 28 | time = readl(®s->time); |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 29 | |
| 30 | /* read the date register */ |
Chris Packham | 942bb6e | 2018-05-28 23:39:57 +1200 | [diff] [blame] | 31 | date = readl(®s->date); |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 32 | |
| 33 | /* test for 12 hour clock (can't tell if it's am/pm) */ |
| 34 | if (time & MVRTC_HRFMT_MSK) { |
| 35 | printf("Error: RTC in 12 hour mode, can't determine AM/PM.\n"); |
| 36 | return -1; |
| 37 | } |
| 38 | |
| 39 | /* time */ |
| 40 | t->tm_sec = bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK); |
| 41 | t->tm_min = bcd2bin((time >> MVRTC_MIN_SFT) & MVRTC_MIN_MSK); |
| 42 | t->tm_hour = bcd2bin((time >> MVRTC_HOUR_SFT) & MVRTC_HOUR_MSK); |
| 43 | t->tm_wday = bcd2bin((time >> MVRTC_DAY_SFT) & MVRTC_DAY_MSK); |
| 44 | t->tm_wday--; |
| 45 | |
| 46 | /* date */ |
| 47 | t->tm_mday = bcd2bin((date >> MVRTC_DATE_SFT) & MVRTC_DATE_MSK); |
| 48 | t->tm_mon = bcd2bin((date >> MVRTC_MON_SFT) & MVRTC_MON_MSK); |
| 49 | t->tm_year = bcd2bin((date >> MVRTC_YEAR_SFT) & MVRTC_YEAR_MSK); |
| 50 | t->tm_year += CENTURY * 100; |
| 51 | |
| 52 | /* not supported in this RTC */ |
| 53 | t->tm_yday = 0; |
| 54 | t->tm_isdst = 0; |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
Chris Packham | 942bb6e | 2018-05-28 23:39:57 +1200 | [diff] [blame] | 59 | #ifndef CONFIG_DM_RTC |
| 60 | int rtc_get(struct rtc_time *t) |
| 61 | { |
| 62 | struct mvrtc_registers *regs; |
| 63 | |
| 64 | regs = (struct mvrtc_registers *)KW_RTC_BASE; |
| 65 | return __mv_rtc_get(regs, t); |
| 66 | } |
| 67 | #endif /* !CONFIG_DM_RTC */ |
| 68 | |
| 69 | 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] | 70 | { |
| 71 | u32 time = 0; /* sets hour format bit to zero, 24hr format. */ |
| 72 | u32 date = 0; |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 73 | |
| 74 | /* check that this code isn't 80+ years old ;-) */ |
| 75 | if ((t->tm_year / 100) != CENTURY) |
| 76 | printf("Warning: Only century %d supported.\n", CENTURY); |
| 77 | |
| 78 | /* time */ |
| 79 | time |= (bin2bcd(t->tm_sec) & MVRTC_SEC_MSK) << MVRTC_SEC_SFT; |
| 80 | time |= (bin2bcd(t->tm_min) & MVRTC_MIN_MSK) << MVRTC_MIN_SFT; |
| 81 | time |= (bin2bcd(t->tm_hour) & MVRTC_HOUR_MSK) << MVRTC_HOUR_SFT; |
| 82 | time |= (bin2bcd(t->tm_wday + 1) & MVRTC_DAY_MSK) << MVRTC_DAY_SFT; |
| 83 | |
| 84 | /* date */ |
| 85 | date |= (bin2bcd(t->tm_mday) & MVRTC_DATE_MSK) << MVRTC_DATE_SFT; |
| 86 | date |= (bin2bcd(t->tm_mon) & MVRTC_MON_MSK) << MVRTC_MON_SFT; |
| 87 | date |= (bin2bcd(t->tm_year % 100) & MVRTC_YEAR_MSK) << MVRTC_YEAR_SFT; |
| 88 | |
| 89 | /* write the time register */ |
Chris Packham | 942bb6e | 2018-05-28 23:39:57 +1200 | [diff] [blame] | 90 | writel(time, ®s->time); |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 91 | |
| 92 | /* write the date register */ |
Chris Packham | 942bb6e | 2018-05-28 23:39:57 +1200 | [diff] [blame] | 93 | writel(date, ®s->date); |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
Chris Packham | 942bb6e | 2018-05-28 23:39:57 +1200 | [diff] [blame] | 98 | #ifndef CONFIG_DM_RTC |
| 99 | int rtc_set(struct rtc_time *t) |
| 100 | { |
| 101 | struct mvrtc_registers *regs; |
| 102 | |
| 103 | regs = (struct mvrtc_registers *)KW_RTC_BASE; |
| 104 | return __mv_rtc_set(regs, t); |
| 105 | } |
| 106 | #endif /* !CONFIG_DM_RTC */ |
| 107 | |
| 108 | static void __mv_rtc_reset(struct mvrtc_registers *regs) |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 109 | { |
| 110 | u32 time; |
| 111 | u32 sec; |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 112 | |
| 113 | /* 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] | 114 | time = readl(®s->time); |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 115 | sec = bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK); |
| 116 | udelay(1000000); |
Chris Packham | 942bb6e | 2018-05-28 23:39:57 +1200 | [diff] [blame] | 117 | time = readl(®s->time); |
Jason Cooper | b608b95 | 2011-08-04 21:26:16 +0530 | [diff] [blame] | 118 | |
| 119 | if (sec == bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK)) |
| 120 | printf("Error: RTC did not increment.\n"); |
| 121 | } |
Chris Packham | 942bb6e | 2018-05-28 23:39:57 +1200 | [diff] [blame] | 122 | |
| 123 | #ifndef CONFIG_DM_RTC |
| 124 | void rtc_reset(void) |
| 125 | { |
| 126 | struct mvrtc_registers *regs; |
| 127 | |
| 128 | regs = (struct mvrtc_registers *)KW_RTC_BASE; |
| 129 | __mv_rtc_reset(regs); |
| 130 | } |
| 131 | #endif /* !CONFIG_DM_RTC */ |
Chris Packham | d3671df | 2018-05-28 23:39:58 +1200 | [diff] [blame] | 132 | |
| 133 | #ifdef CONFIG_DM_RTC |
| 134 | static int mv_rtc_get(struct udevice *dev, struct rtc_time *tm) |
| 135 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 136 | struct mvrtc_pdata *pdata = dev_get_plat(dev); |
Chris Packham | d3671df | 2018-05-28 23:39:58 +1200 | [diff] [blame] | 137 | struct mvrtc_registers *regs = (struct mvrtc_registers *)pdata->iobase; |
| 138 | |
| 139 | return __mv_rtc_get(regs, tm); |
| 140 | } |
| 141 | |
| 142 | static int mv_rtc_set(struct udevice *dev, const struct rtc_time *tm) |
| 143 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 144 | struct mvrtc_pdata *pdata = dev_get_plat(dev); |
Chris Packham | d3671df | 2018-05-28 23:39:58 +1200 | [diff] [blame] | 145 | struct mvrtc_registers *regs = (struct mvrtc_registers *)pdata->iobase; |
| 146 | |
| 147 | return __mv_rtc_set(regs, tm); |
| 148 | } |
| 149 | |
| 150 | static int mv_rtc_reset(struct udevice *dev) |
| 151 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 152 | struct mvrtc_pdata *pdata = dev_get_plat(dev); |
Chris Packham | d3671df | 2018-05-28 23:39:58 +1200 | [diff] [blame] | 153 | struct mvrtc_registers *regs = (struct mvrtc_registers *)pdata->iobase; |
| 154 | |
| 155 | __mv_rtc_reset(regs); |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static const struct rtc_ops mv_rtc_ops = { |
| 160 | .get = mv_rtc_get, |
| 161 | .set = mv_rtc_set, |
| 162 | .reset = mv_rtc_reset, |
| 163 | }; |
| 164 | |
| 165 | static const struct udevice_id mv_rtc_ids[] = { |
| 166 | { .compatible = "marvell,kirkwood-rtc" }, |
| 167 | { .compatible = "marvell,orion-rtc" }, |
| 168 | { } |
| 169 | }; |
| 170 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 171 | static int mv_rtc_of_to_plat(struct udevice *dev) |
Chris Packham | d3671df | 2018-05-28 23:39:58 +1200 | [diff] [blame] | 172 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 173 | struct mvrtc_pdata *pdata = dev_get_plat(dev); |
Chris Packham | d3671df | 2018-05-28 23:39:58 +1200 | [diff] [blame] | 174 | |
Masahiro Yamada | 2548493 | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 175 | pdata->iobase = dev_read_addr(dev); |
Chris Packham | d3671df | 2018-05-28 23:39:58 +1200 | [diff] [blame] | 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | U_BOOT_DRIVER(rtc_mv) = { |
| 180 | .name = "rtc-mv", |
| 181 | .id = UCLASS_RTC, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 182 | .of_to_plat = mv_rtc_of_to_plat, |
Chris Packham | d3671df | 2018-05-28 23:39:58 +1200 | [diff] [blame] | 183 | .of_match = mv_rtc_ids, |
| 184 | .ops = &mv_rtc_ops, |
| 185 | }; |
| 186 | #endif /* CONFIG_DM_RTC */ |