blob: abd339076ad25c5b86232b87230a2fbc2915f5ac [file] [log] [blame]
Patrick Delaunayfeee9af2019-07-22 11:02:34 +02001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2/*
3 * Copyright (C) 2019, STMicroelectronics - All Rights Reserved
4 */
5#include <common.h>
6#include <clk.h>
7#include <dm.h>
8#include <rtc.h>
9#include <asm/io.h>
10#include <linux/iopoll.h>
11
12#define STM32_RTC_TR 0x00
13#define STM32_RTC_DR 0x04
14#define STM32_RTC_ISR 0x0C
15#define STM32_RTC_PRER 0x10
16#define STM32_RTC_CR 0x18
17#define STM32_RTC_WPR 0x24
18
19/* STM32_RTC_TR bit fields */
20#define STM32_RTC_SEC_SHIFT 0
21#define STM32_RTC_SEC GENMASK(6, 0)
22#define STM32_RTC_MIN_SHIFT 8
23#define STM32_RTC_MIN GENMASK(14, 8)
24#define STM32_RTC_HOUR_SHIFT 16
25#define STM32_RTC_HOUR GENMASK(21, 16)
26
27/* STM32_RTC_DR bit fields */
28#define STM32_RTC_DATE_SHIFT 0
29#define STM32_RTC_DATE GENMASK(5, 0)
30#define STM32_RTC_MONTH_SHIFT 8
31#define STM32_RTC_MONTH GENMASK(12, 8)
32#define STM32_RTC_WDAY_SHIFT 13
33#define STM32_RTC_WDAY GENMASK(15, 13)
34#define STM32_RTC_YEAR_SHIFT 16
35#define STM32_RTC_YEAR GENMASK(23, 16)
36
37/* STM32_RTC_CR bit fields */
38#define STM32_RTC_CR_FMT BIT(6)
39
40/* STM32_RTC_ISR/STM32_RTC_ICSR bit fields */
41#define STM32_RTC_ISR_INITS BIT(4)
42#define STM32_RTC_ISR_RSF BIT(5)
43#define STM32_RTC_ISR_INITF BIT(6)
44#define STM32_RTC_ISR_INIT BIT(7)
45
46/* STM32_RTC_PRER bit fields */
47#define STM32_RTC_PRER_PRED_S_SHIFT 0
48#define STM32_RTC_PRER_PRED_S GENMASK(14, 0)
49#define STM32_RTC_PRER_PRED_A_SHIFT 16
50#define STM32_RTC_PRER_PRED_A GENMASK(22, 16)
51
52/* STM32_RTC_WPR key constants */
53#define RTC_WPR_1ST_KEY 0xCA
54#define RTC_WPR_2ND_KEY 0x53
55#define RTC_WPR_WRONG_KEY 0xFF
56
57struct stm32_rtc_priv {
58 fdt_addr_t base;
59};
60
61static int stm32_rtc_get(struct udevice *dev, struct rtc_time *tm)
62{
63 struct stm32_rtc_priv *priv = dev_get_priv(dev);
64 u32 tr, dr;
65
66 tr = readl(priv->base + STM32_RTC_TR);
67 dr = readl(priv->base + STM32_RTC_DR);
68
69 tm->tm_sec = bcd2bin((tr & STM32_RTC_SEC) >> STM32_RTC_SEC_SHIFT);
70 tm->tm_min = bcd2bin((tr & STM32_RTC_MIN) >> STM32_RTC_MIN_SHIFT);
71 tm->tm_hour = bcd2bin((tr & STM32_RTC_HOUR) >> STM32_RTC_HOUR_SHIFT);
72
73 tm->tm_mday = bcd2bin((dr & STM32_RTC_DATE) >> STM32_RTC_DATE_SHIFT);
74 tm->tm_mon = bcd2bin((dr & STM32_RTC_MONTH) >> STM32_RTC_MONTH_SHIFT);
75 tm->tm_year = bcd2bin((dr & STM32_RTC_YEAR) >> STM32_RTC_YEAR_SHIFT);
76 tm->tm_wday = bcd2bin((dr & STM32_RTC_WDAY) >> STM32_RTC_WDAY_SHIFT);
77 tm->tm_yday = 0;
78 tm->tm_isdst = 0;
79
80 dev_dbg(dev, "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
81 tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
82 tm->tm_hour, tm->tm_min, tm->tm_sec);
83
84 return 0;
85}
86
87static void stm32_rtc_unlock(struct udevice *dev)
88{
89 struct stm32_rtc_priv *priv = dev_get_priv(dev);
90
91 writel(RTC_WPR_1ST_KEY, priv->base + STM32_RTC_WPR);
92 writel(RTC_WPR_2ND_KEY, priv->base + STM32_RTC_WPR);
93}
94
95static void stm32_rtc_lock(struct udevice *dev)
96{
97 struct stm32_rtc_priv *priv = dev_get_priv(dev);
98
99 writel(RTC_WPR_WRONG_KEY, priv->base + STM32_RTC_WPR);
100}
101
102static int stm32_rtc_enter_init_mode(struct udevice *dev)
103{
104 struct stm32_rtc_priv *priv = dev_get_priv(dev);
105 u32 isr = readl(priv->base + STM32_RTC_ISR);
106
107 if (!(isr & STM32_RTC_ISR_INITF)) {
108 isr |= STM32_RTC_ISR_INIT;
109 writel(isr, priv->base + STM32_RTC_ISR);
110
111 return readl_poll_timeout(priv->base + STM32_RTC_ISR,
112 isr,
113 (isr & STM32_RTC_ISR_INITF),
114 100000);
115 }
116
117 return 0;
118}
119
120static int stm32_rtc_wait_sync(struct udevice *dev)
121{
122 struct stm32_rtc_priv *priv = dev_get_priv(dev);
123 u32 isr = readl(priv->base + STM32_RTC_ISR);
124
125 isr &= ~STM32_RTC_ISR_RSF;
126 writel(isr, priv->base + STM32_RTC_ISR);
127
128 /*
129 * Wait for RSF to be set to ensure the calendar registers are
130 * synchronised, it takes around 2 rtc_ck clock cycles
131 */
132 return readl_poll_timeout(priv->base + STM32_RTC_ISR,
133 isr, (isr & STM32_RTC_ISR_RSF),
134 100000);
135}
136
137static void stm32_rtc_exit_init_mode(struct udevice *dev)
138{
139 struct stm32_rtc_priv *priv = dev_get_priv(dev);
140 u32 isr = readl(priv->base + STM32_RTC_ISR);
141
142 isr &= ~STM32_RTC_ISR_INIT;
143 writel(isr, priv->base + STM32_RTC_ISR);
144}
145
146static int stm32_rtc_set_time(struct udevice *dev, u32 time, u32 date)
147{
148 struct stm32_rtc_priv *priv = dev_get_priv(dev);
149 int ret;
150
151 stm32_rtc_unlock(dev);
152
153 ret = stm32_rtc_enter_init_mode(dev);
154 if (ret)
155 goto lock;
156
157 writel(time, priv->base + STM32_RTC_TR);
158 writel(date, priv->base + STM32_RTC_DR);
159
160 stm32_rtc_exit_init_mode(dev);
161
162 ret = stm32_rtc_wait_sync(dev);
163
164lock:
165 stm32_rtc_lock(dev);
166 return ret;
167}
168
169static int stm32_rtc_set(struct udevice *dev, const struct rtc_time *tm)
170{
171 u32 t, d;
172
173 dev_dbg(dev, "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
174 tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
175 tm->tm_hour, tm->tm_min, tm->tm_sec);
176
177 /* Time in BCD format */
178 t = (bin2bcd(tm->tm_sec) << STM32_RTC_SEC_SHIFT) & STM32_RTC_SEC;
179 t |= (bin2bcd(tm->tm_min) << STM32_RTC_MIN_SHIFT) & STM32_RTC_MIN;
180 t |= (bin2bcd(tm->tm_hour) << STM32_RTC_HOUR_SHIFT) & STM32_RTC_HOUR;
181
182 /* Date in BCD format */
183 d = (bin2bcd(tm->tm_mday) << STM32_RTC_DATE_SHIFT) & STM32_RTC_DATE;
184 d |= (bin2bcd(tm->tm_mon) << STM32_RTC_MONTH_SHIFT) & STM32_RTC_MONTH;
185 d |= (bin2bcd(tm->tm_year) << STM32_RTC_YEAR_SHIFT) & STM32_RTC_YEAR;
186 d |= (bin2bcd(tm->tm_wday) << STM32_RTC_WDAY_SHIFT) & STM32_RTC_WDAY;
187
188 return stm32_rtc_set_time(dev, t, d);
189}
190
191static int stm32_rtc_reset(struct udevice *dev)
192{
193 dev_dbg(dev, "Reset DATE\n");
194
195 return stm32_rtc_set_time(dev, 0, 0);
196}
197
198static int stm32_rtc_init(struct udevice *dev)
199{
200 struct stm32_rtc_priv *priv = dev_get_priv(dev);
201 unsigned int prer, pred_a, pred_s, pred_a_max, pred_s_max, cr;
202 unsigned int rate;
203 struct clk clk;
204 int ret;
205 u32 isr = readl(priv->base + STM32_RTC_ISR);
206
207 if (isr & STM32_RTC_ISR_INITS)
208 return 0;
209
210 ret = clk_get_by_index(dev, 1, &clk);
211 if (ret)
212 return ret;
213
214 ret = clk_enable(&clk);
215 if (ret) {
216 clk_free(&clk);
217 return ret;
218 }
219
220 rate = clk_get_rate(&clk);
221
222 /* Find prediv_a and prediv_s to obtain the 1Hz calendar clock */
223 pred_a_max = STM32_RTC_PRER_PRED_A >> STM32_RTC_PRER_PRED_A_SHIFT;
224 pred_s_max = STM32_RTC_PRER_PRED_S >> STM32_RTC_PRER_PRED_S_SHIFT;
225
226 for (pred_a = pred_a_max; pred_a + 1 > 0; pred_a--) {
227 pred_s = (rate / (pred_a + 1)) - 1;
228
229 if (((pred_s + 1) * (pred_a + 1)) == rate)
230 break;
231 }
232
233 /*
234 * Can't find a 1Hz, so give priority to RTC power consumption
235 * by choosing the higher possible value for prediv_a
236 */
237 if (pred_s > pred_s_max || pred_a > pred_a_max) {
238 pred_a = pred_a_max;
239 pred_s = (rate / (pred_a + 1)) - 1;
240 }
241
242 stm32_rtc_unlock(dev);
243
244 ret = stm32_rtc_enter_init_mode(dev);
245 if (ret) {
246 dev_err(dev,
247 "Can't enter in init mode. Prescaler config failed.\n");
248 goto unlock;
249 }
250
251 prer = (pred_s << STM32_RTC_PRER_PRED_S_SHIFT) & STM32_RTC_PRER_PRED_S;
252 prer |= (pred_a << STM32_RTC_PRER_PRED_A_SHIFT) & STM32_RTC_PRER_PRED_A;
253 writel(prer, priv->base + STM32_RTC_PRER);
254
255 /* Force 24h time format */
256 cr = readl(priv->base + STM32_RTC_CR);
257 cr &= ~STM32_RTC_CR_FMT;
258 writel(cr, priv->base + STM32_RTC_CR);
259
260 stm32_rtc_exit_init_mode(dev);
261
262 ret = stm32_rtc_wait_sync(dev);
263
264unlock:
265 stm32_rtc_lock(dev);
266
267 if (ret) {
268 clk_disable(&clk);
269 clk_free(&clk);
270 }
271
272 return ret;
273}
274
275static int stm32_rtc_probe(struct udevice *dev)
276{
277 struct stm32_rtc_priv *priv = dev_get_priv(dev);
278 struct clk clk;
279 int ret;
280
281 priv->base = dev_read_addr(dev);
282 if (priv->base == FDT_ADDR_T_NONE)
283 return -EINVAL;
284
285 ret = clk_get_by_index(dev, 0, &clk);
286 if (ret)
287 return ret;
288
289 ret = clk_enable(&clk);
290 if (ret) {
291 clk_free(&clk);
292 return ret;
293 }
294
295 ret = stm32_rtc_init(dev);
296
297 if (ret) {
298 clk_disable(&clk);
299 clk_free(&clk);
300 }
301
302 return ret;
303}
304
305static const struct rtc_ops stm32_rtc_ops = {
306 .get = stm32_rtc_get,
307 .set = stm32_rtc_set,
308 .reset = stm32_rtc_reset,
309};
310
311static const struct udevice_id stm32_rtc_ids[] = {
312 { .compatible = "st,stm32mp1-rtc" },
313 { }
314};
315
316U_BOOT_DRIVER(rtc_stm32) = {
317 .name = "rtc-stm32",
318 .id = UCLASS_RTC,
319 .probe = stm32_rtc_probe,
320 .of_match = stm32_rtc_ids,
321 .ops = &stm32_rtc_ops,
322 .priv_auto_alloc_size = sizeof(struct stm32_rtc_priv),
323};