xref: /openbmc/u-boot/drivers/rtc/mvrtc.c (revision ae485b54)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2011
4  * Jason Cooper <u-boot@lakedaemon.net>
5  */
6 
7 /*
8  * Date & Time support for Marvell Integrated RTC
9  */
10 
11 #include <common.h>
12 #include <command.h>
13 #include <dm.h>
14 #include <rtc.h>
15 #include <asm/io.h>
16 #include "mvrtc.h"
17 
18 /* This RTC does not support century, so we assume 20 */
19 #define CENTURY 20
20 
21 static int __mv_rtc_get(struct mvrtc_registers *regs, struct rtc_time *t)
22 {
23 	u32 time;
24 	u32 date;
25 
26 	/* read the time register */
27 	time = readl(&regs->time);
28 
29 	/* read the date register */
30 	date = readl(&regs->date);
31 
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 
58 #ifndef CONFIG_DM_RTC
59 int 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 
68 static int __mv_rtc_set(struct mvrtc_registers *regs, const struct rtc_time *t)
69 {
70 	u32 time = 0; /* sets hour format bit to zero, 24hr format. */
71 	u32 date = 0;
72 
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 */
89 	writel(time, &regs->time);
90 
91 	/* write the date register */
92 	writel(date, &regs->date);
93 
94 	return 0;
95 }
96 
97 #ifndef CONFIG_DM_RTC
98 int 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 
107 static void __mv_rtc_reset(struct mvrtc_registers *regs)
108 {
109 	u32 time;
110 	u32 sec;
111 
112 	/* no init routine for this RTC needed, just check that it's working */
113 	time = readl(&regs->time);
114 	sec  = bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK);
115 	udelay(1000000);
116 	time = readl(&regs->time);
117 
118 	if (sec == bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK))
119 		printf("Error: RTC did not increment.\n");
120 }
121 
122 #ifndef CONFIG_DM_RTC
123 void 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 */
131 
132 #ifdef CONFIG_DM_RTC
133 static 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 
141 static 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 
149 static 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 
158 static const struct rtc_ops mv_rtc_ops = {
159 	.get = mv_rtc_get,
160 	.set = mv_rtc_set,
161 	.reset = mv_rtc_reset,
162 };
163 
164 static const struct udevice_id mv_rtc_ids[] = {
165 	{ .compatible = "marvell,kirkwood-rtc" },
166 	{ .compatible = "marvell,orion-rtc" },
167 	{ }
168 };
169 
170 static 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 
178 U_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 */
186