blob: 94a065379c91d7d4ba9d9373d8c1185c0832e684 [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>
Chris Packhamd3671df2018-05-28 23:39:58 +120013#include <dm.h>
Jason Cooperb608b952011-08-04 21:26:16 +053014#include <rtc.h>
Anatolij Gustschin0ac16bf2011-10-29 11:19:47 +000015#include <asm/io.h>
Jason Cooperb608b952011-08-04 21:26:16 +053016#include "mvrtc.h"
17
18/* This RTC does not support century, so we assume 20 */
19#define CENTURY 20
20
Chris Packham942bb6e2018-05-28 23:39:57 +120021static int __mv_rtc_get(struct mvrtc_registers *regs, struct rtc_time *t)
Jason Cooperb608b952011-08-04 21:26:16 +053022{
23 u32 time;
24 u32 date;
Jason Cooperb608b952011-08-04 21:26:16 +053025
26 /* read the time register */
Chris Packham942bb6e2018-05-28 23:39:57 +120027 time = readl(&regs->time);
Jason Cooperb608b952011-08-04 21:26:16 +053028
29 /* read the date register */
Chris Packham942bb6e2018-05-28 23:39:57 +120030 date = readl(&regs->date);
Jason Cooperb608b952011-08-04 21:26:16 +053031
32 /* test for 12 hour clock (can't tell if it's am/pm) */
33 if (time & MVRTC_HRFMT_MSK) {
34 printf("Error: RTC in 12 hour mode, can't determine AM/PM.\n");
35 return -1;
36 }
37
38 /* time */
39 t->tm_sec = bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK);
40 t->tm_min = bcd2bin((time >> MVRTC_MIN_SFT) & MVRTC_MIN_MSK);
41 t->tm_hour = bcd2bin((time >> MVRTC_HOUR_SFT) & MVRTC_HOUR_MSK);
42 t->tm_wday = bcd2bin((time >> MVRTC_DAY_SFT) & MVRTC_DAY_MSK);
43 t->tm_wday--;
44
45 /* date */
46 t->tm_mday = bcd2bin((date >> MVRTC_DATE_SFT) & MVRTC_DATE_MSK);
47 t->tm_mon = bcd2bin((date >> MVRTC_MON_SFT) & MVRTC_MON_MSK);
48 t->tm_year = bcd2bin((date >> MVRTC_YEAR_SFT) & MVRTC_YEAR_MSK);
49 t->tm_year += CENTURY * 100;
50
51 /* not supported in this RTC */
52 t->tm_yday = 0;
53 t->tm_isdst = 0;
54
55 return 0;
56}
57
Chris Packham942bb6e2018-05-28 23:39:57 +120058#ifndef CONFIG_DM_RTC
59int rtc_get(struct rtc_time *t)
60{
61 struct mvrtc_registers *regs;
62
63 regs = (struct mvrtc_registers *)KW_RTC_BASE;
64 return __mv_rtc_get(regs, t);
65}
66#endif /* !CONFIG_DM_RTC */
67
68static int __mv_rtc_set(struct mvrtc_registers *regs, const struct rtc_time *t)
Jason Cooperb608b952011-08-04 21:26:16 +053069{
70 u32 time = 0; /* sets hour format bit to zero, 24hr format. */
71 u32 date = 0;
Jason Cooperb608b952011-08-04 21:26:16 +053072
73 /* check that this code isn't 80+ years old ;-) */
74 if ((t->tm_year / 100) != CENTURY)
75 printf("Warning: Only century %d supported.\n", CENTURY);
76
77 /* time */
78 time |= (bin2bcd(t->tm_sec) & MVRTC_SEC_MSK) << MVRTC_SEC_SFT;
79 time |= (bin2bcd(t->tm_min) & MVRTC_MIN_MSK) << MVRTC_MIN_SFT;
80 time |= (bin2bcd(t->tm_hour) & MVRTC_HOUR_MSK) << MVRTC_HOUR_SFT;
81 time |= (bin2bcd(t->tm_wday + 1) & MVRTC_DAY_MSK) << MVRTC_DAY_SFT;
82
83 /* date */
84 date |= (bin2bcd(t->tm_mday) & MVRTC_DATE_MSK) << MVRTC_DATE_SFT;
85 date |= (bin2bcd(t->tm_mon) & MVRTC_MON_MSK) << MVRTC_MON_SFT;
86 date |= (bin2bcd(t->tm_year % 100) & MVRTC_YEAR_MSK) << MVRTC_YEAR_SFT;
87
88 /* write the time register */
Chris Packham942bb6e2018-05-28 23:39:57 +120089 writel(time, &regs->time);
Jason Cooperb608b952011-08-04 21:26:16 +053090
91 /* write the date register */
Chris Packham942bb6e2018-05-28 23:39:57 +120092 writel(date, &regs->date);
Jason Cooperb608b952011-08-04 21:26:16 +053093
94 return 0;
95}
96
Chris Packham942bb6e2018-05-28 23:39:57 +120097#ifndef CONFIG_DM_RTC
98int rtc_set(struct rtc_time *t)
99{
100 struct mvrtc_registers *regs;
101
102 regs = (struct mvrtc_registers *)KW_RTC_BASE;
103 return __mv_rtc_set(regs, t);
104}
105#endif /* !CONFIG_DM_RTC */
106
107static void __mv_rtc_reset(struct mvrtc_registers *regs)
Jason Cooperb608b952011-08-04 21:26:16 +0530108{
109 u32 time;
110 u32 sec;
Jason Cooperb608b952011-08-04 21:26:16 +0530111
112 /* no init routine for this RTC needed, just check that it's working */
Chris Packham942bb6e2018-05-28 23:39:57 +1200113 time = readl(&regs->time);
Jason Cooperb608b952011-08-04 21:26:16 +0530114 sec = bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK);
115 udelay(1000000);
Chris Packham942bb6e2018-05-28 23:39:57 +1200116 time = readl(&regs->time);
Jason Cooperb608b952011-08-04 21:26:16 +0530117
118 if (sec == bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK))
119 printf("Error: RTC did not increment.\n");
120}
Chris Packham942bb6e2018-05-28 23:39:57 +1200121
122#ifndef CONFIG_DM_RTC
123void rtc_reset(void)
124{
125 struct mvrtc_registers *regs;
126
127 regs = (struct mvrtc_registers *)KW_RTC_BASE;
128 __mv_rtc_reset(regs);
129}
130#endif /* !CONFIG_DM_RTC */
Chris Packhamd3671df2018-05-28 23:39:58 +1200131
132#ifdef CONFIG_DM_RTC
133static int mv_rtc_get(struct udevice *dev, struct rtc_time *tm)
134{
135 struct mvrtc_pdata *pdata = dev_get_platdata(dev);
136 struct mvrtc_registers *regs = (struct mvrtc_registers *)pdata->iobase;
137
138 return __mv_rtc_get(regs, tm);
139}
140
141static int mv_rtc_set(struct udevice *dev, const struct rtc_time *tm)
142{
143 struct mvrtc_pdata *pdata = dev_get_platdata(dev);
144 struct mvrtc_registers *regs = (struct mvrtc_registers *)pdata->iobase;
145
146 return __mv_rtc_set(regs, tm);
147}
148
149static int mv_rtc_reset(struct udevice *dev)
150{
151 struct mvrtc_pdata *pdata = dev_get_platdata(dev);
152 struct mvrtc_registers *regs = (struct mvrtc_registers *)pdata->iobase;
153
154 __mv_rtc_reset(regs);
155 return 0;
156}
157
158static const struct rtc_ops mv_rtc_ops = {
159 .get = mv_rtc_get,
160 .set = mv_rtc_set,
161 .reset = mv_rtc_reset,
162};
163
164static const struct udevice_id mv_rtc_ids[] = {
165 { .compatible = "marvell,kirkwood-rtc" },
166 { .compatible = "marvell,orion-rtc" },
167 { }
168};
169
170static int mv_rtc_ofdata_to_platdata(struct udevice *dev)
171{
172 struct mvrtc_pdata *pdata = dev_get_platdata(dev);
173
174 pdata->iobase = devfdt_get_addr(dev);
175 return 0;
176}
177
178U_BOOT_DRIVER(rtc_mv) = {
179 .name = "rtc-mv",
180 .id = UCLASS_RTC,
181 .ofdata_to_platdata = mv_rtc_ofdata_to_platdata,
182 .of_match = mv_rtc_ids,
183 .ops = &mv_rtc_ops,
184};
185#endif /* CONFIG_DM_RTC */