1 /*
2 * drivers/rtc/rtc-pl031.c
3 *
4 * Real Time Clock interface for ARM AMBA PrimeCell 031 RTC
5 *
6 * Author: Deepak Saxena <dsaxena@plexity.net>
7 *
8 * Copyright 2006 (c) MontaVista Software, Inc.
9 *
10 * Author: Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
11 * Copyright 2010 (c) ST-Ericsson AB
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
17 */
18 #include <linux/module.h>
19 #include <linux/rtc.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
22 #include <linux/amba/bus.h>
23 #include <linux/io.h>
24 #include <linux/bcd.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27
28 /*
29 * Register definitions
30 */
31 #define RTC_DR 0x00 /* Data read register */
32 #define RTC_MR 0x04 /* Match register */
33 #define RTC_LR 0x08 /* Data load register */
34 #define RTC_CR 0x0c /* Control register */
35 #define RTC_IMSC 0x10 /* Interrupt mask and set register */
36 #define RTC_RIS 0x14 /* Raw interrupt status register */
37 #define RTC_MIS 0x18 /* Masked interrupt status register */
38 #define RTC_ICR 0x1c /* Interrupt clear register */
39 /* ST variants have additional timer functionality */
40 #define RTC_TDR 0x20 /* Timer data read register */
41 #define RTC_TLR 0x24 /* Timer data load register */
42 #define RTC_TCR 0x28 /* Timer control register */
43 #define RTC_YDR 0x30 /* Year data read register */
44 #define RTC_YMR 0x34 /* Year match register */
45 #define RTC_YLR 0x38 /* Year data load register */
46
47 #define RTC_CR_EN (1 << 0) /* counter enable bit */
48 #define RTC_CR_CWEN (1 << 26) /* Clockwatch enable bit */
49
50 #define RTC_TCR_EN (1 << 1) /* Periodic timer enable bit */
51
52 /* Common bit definitions for Interrupt status and control registers */
53 #define RTC_BIT_AI (1 << 0) /* Alarm interrupt bit */
54 #define RTC_BIT_PI (1 << 1) /* Periodic interrupt bit. ST variants only. */
55
56 /* Common bit definations for ST v2 for reading/writing time */
57 #define RTC_SEC_SHIFT 0
58 #define RTC_SEC_MASK (0x3F << RTC_SEC_SHIFT) /* Second [0-59] */
59 #define RTC_MIN_SHIFT 6
60 #define RTC_MIN_MASK (0x3F << RTC_MIN_SHIFT) /* Minute [0-59] */
61 #define RTC_HOUR_SHIFT 12
62 #define RTC_HOUR_MASK (0x1F << RTC_HOUR_SHIFT) /* Hour [0-23] */
63 #define RTC_WDAY_SHIFT 17
64 #define RTC_WDAY_MASK (0x7 << RTC_WDAY_SHIFT) /* Day of Week [1-7] 1=Sunday */
65 #define RTC_MDAY_SHIFT 20
66 #define RTC_MDAY_MASK (0x1F << RTC_MDAY_SHIFT) /* Day of Month [1-31] */
67 #define RTC_MON_SHIFT 25
68 #define RTC_MON_MASK (0xF << RTC_MON_SHIFT) /* Month [1-12] 1=January */
69
70 #define RTC_TIMER_FREQ 32768
71
72 struct pl031_local {
73 struct rtc_device *rtc;
74 void __iomem *base;
75 u8 hw_designer;
76 u8 hw_revision:4;
77 };
78
pl031_alarm_irq_enable(struct device * dev,unsigned int enabled)79 static int pl031_alarm_irq_enable(struct device *dev,
80 unsigned int enabled)
81 {
82 struct pl031_local *ldata = dev_get_drvdata(dev);
83 unsigned long imsc;
84
85 /* Clear any pending alarm interrupts. */
86 writel(RTC_BIT_AI, ldata->base + RTC_ICR);
87
88 imsc = readl(ldata->base + RTC_IMSC);
89
90 if (enabled == 1)
91 writel(imsc | RTC_BIT_AI, ldata->base + RTC_IMSC);
92 else
93 writel(imsc & ~RTC_BIT_AI, ldata->base + RTC_IMSC);
94
95 return 0;
96 }
97
98 /*
99 * Convert Gregorian date to ST v2 RTC format.
100 */
pl031_stv2_tm_to_time(struct device * dev,struct rtc_time * tm,unsigned long * st_time,unsigned long * bcd_year)101 static int pl031_stv2_tm_to_time(struct device *dev,
102 struct rtc_time *tm, unsigned long *st_time,
103 unsigned long *bcd_year)
104 {
105 int year = tm->tm_year + 1900;
106 int wday = tm->tm_wday;
107
108 /* wday masking is not working in hardware so wday must be valid */
109 if (wday < -1 || wday > 6) {
110 dev_err(dev, "invalid wday value %d\n", tm->tm_wday);
111 return -EINVAL;
112 } else if (wday == -1) {
113 /* wday is not provided, calculate it here */
114 unsigned long time;
115 struct rtc_time calc_tm;
116
117 rtc_tm_to_time(tm, &time);
118 rtc_time_to_tm(time, &calc_tm);
119 wday = calc_tm.tm_wday;
120 }
121
122 *bcd_year = (bin2bcd(year % 100) | bin2bcd(year / 100) << 8);
123
124 *st_time = ((tm->tm_mon + 1) << RTC_MON_SHIFT)
125 | (tm->tm_mday << RTC_MDAY_SHIFT)
126 | ((wday + 1) << RTC_WDAY_SHIFT)
127 | (tm->tm_hour << RTC_HOUR_SHIFT)
128 | (tm->tm_min << RTC_MIN_SHIFT)
129 | (tm->tm_sec << RTC_SEC_SHIFT);
130
131 return 0;
132 }
133
134 /*
135 * Convert ST v2 RTC format to Gregorian date.
136 */
pl031_stv2_time_to_tm(unsigned long st_time,unsigned long bcd_year,struct rtc_time * tm)137 static int pl031_stv2_time_to_tm(unsigned long st_time, unsigned long bcd_year,
138 struct rtc_time *tm)
139 {
140 tm->tm_year = bcd2bin(bcd_year) + (bcd2bin(bcd_year >> 8) * 100);
141 tm->tm_mon = ((st_time & RTC_MON_MASK) >> RTC_MON_SHIFT) - 1;
142 tm->tm_mday = ((st_time & RTC_MDAY_MASK) >> RTC_MDAY_SHIFT);
143 tm->tm_wday = ((st_time & RTC_WDAY_MASK) >> RTC_WDAY_SHIFT) - 1;
144 tm->tm_hour = ((st_time & RTC_HOUR_MASK) >> RTC_HOUR_SHIFT);
145 tm->tm_min = ((st_time & RTC_MIN_MASK) >> RTC_MIN_SHIFT);
146 tm->tm_sec = ((st_time & RTC_SEC_MASK) >> RTC_SEC_SHIFT);
147
148 tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
149 tm->tm_year -= 1900;
150
151 return 0;
152 }
153
pl031_stv2_read_time(struct device * dev,struct rtc_time * tm)154 static int pl031_stv2_read_time(struct device *dev, struct rtc_time *tm)
155 {
156 struct pl031_local *ldata = dev_get_drvdata(dev);
157
158 pl031_stv2_time_to_tm(readl(ldata->base + RTC_DR),
159 readl(ldata->base + RTC_YDR), tm);
160
161 return 0;
162 }
163
pl031_stv2_set_time(struct device * dev,struct rtc_time * tm)164 static int pl031_stv2_set_time(struct device *dev, struct rtc_time *tm)
165 {
166 unsigned long time;
167 unsigned long bcd_year;
168 struct pl031_local *ldata = dev_get_drvdata(dev);
169 int ret;
170
171 ret = pl031_stv2_tm_to_time(dev, tm, &time, &bcd_year);
172 if (ret == 0) {
173 writel(bcd_year, ldata->base + RTC_YLR);
174 writel(time, ldata->base + RTC_LR);
175 }
176
177 return ret;
178 }
179
pl031_stv2_read_alarm(struct device * dev,struct rtc_wkalrm * alarm)180 static int pl031_stv2_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
181 {
182 struct pl031_local *ldata = dev_get_drvdata(dev);
183 int ret;
184
185 ret = pl031_stv2_time_to_tm(readl(ldata->base + RTC_MR),
186 readl(ldata->base + RTC_YMR), &alarm->time);
187
188 alarm->pending = readl(ldata->base + RTC_RIS) & RTC_BIT_AI;
189 alarm->enabled = readl(ldata->base + RTC_IMSC) & RTC_BIT_AI;
190
191 return ret;
192 }
193
pl031_stv2_set_alarm(struct device * dev,struct rtc_wkalrm * alarm)194 static int pl031_stv2_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
195 {
196 struct pl031_local *ldata = dev_get_drvdata(dev);
197 unsigned long time;
198 unsigned long bcd_year;
199 int ret;
200
201 /* At the moment, we can only deal with non-wildcarded alarm times. */
202 ret = rtc_valid_tm(&alarm->time);
203 if (ret == 0) {
204 ret = pl031_stv2_tm_to_time(dev, &alarm->time,
205 &time, &bcd_year);
206 if (ret == 0) {
207 writel(bcd_year, ldata->base + RTC_YMR);
208 writel(time, ldata->base + RTC_MR);
209
210 pl031_alarm_irq_enable(dev, alarm->enabled);
211 }
212 }
213
214 return ret;
215 }
216
pl031_interrupt(int irq,void * dev_id)217 static irqreturn_t pl031_interrupt(int irq, void *dev_id)
218 {
219 struct pl031_local *ldata = dev_id;
220 unsigned long rtcmis;
221 unsigned long events = 0;
222
223 rtcmis = readl(ldata->base + RTC_MIS);
224 if (rtcmis) {
225 writel(rtcmis, ldata->base + RTC_ICR);
226
227 if (rtcmis & RTC_BIT_AI)
228 events |= (RTC_AF | RTC_IRQF);
229
230 /* Timer interrupt is only available in ST variants */
231 if ((rtcmis & RTC_BIT_PI) &&
232 (ldata->hw_designer == AMBA_VENDOR_ST))
233 events |= (RTC_PF | RTC_IRQF);
234
235 rtc_update_irq(ldata->rtc, 1, events);
236
237 return IRQ_HANDLED;
238 }
239
240 return IRQ_NONE;
241 }
242
pl031_read_time(struct device * dev,struct rtc_time * tm)243 static int pl031_read_time(struct device *dev, struct rtc_time *tm)
244 {
245 struct pl031_local *ldata = dev_get_drvdata(dev);
246
247 rtc_time_to_tm(readl(ldata->base + RTC_DR), tm);
248
249 return 0;
250 }
251
pl031_set_time(struct device * dev,struct rtc_time * tm)252 static int pl031_set_time(struct device *dev, struct rtc_time *tm)
253 {
254 unsigned long time;
255 struct pl031_local *ldata = dev_get_drvdata(dev);
256 int ret;
257
258 ret = rtc_tm_to_time(tm, &time);
259
260 if (ret == 0)
261 writel(time, ldata->base + RTC_LR);
262
263 return ret;
264 }
265
pl031_read_alarm(struct device * dev,struct rtc_wkalrm * alarm)266 static int pl031_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
267 {
268 struct pl031_local *ldata = dev_get_drvdata(dev);
269
270 rtc_time_to_tm(readl(ldata->base + RTC_MR), &alarm->time);
271
272 alarm->pending = readl(ldata->base + RTC_RIS) & RTC_BIT_AI;
273 alarm->enabled = readl(ldata->base + RTC_IMSC) & RTC_BIT_AI;
274
275 return 0;
276 }
277
pl031_set_alarm(struct device * dev,struct rtc_wkalrm * alarm)278 static int pl031_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
279 {
280 struct pl031_local *ldata = dev_get_drvdata(dev);
281 unsigned long time;
282 int ret;
283
284 /* At the moment, we can only deal with non-wildcarded alarm times. */
285 ret = rtc_valid_tm(&alarm->time);
286 if (ret == 0) {
287 ret = rtc_tm_to_time(&alarm->time, &time);
288 if (ret == 0) {
289 writel(time, ldata->base + RTC_MR);
290 pl031_alarm_irq_enable(dev, alarm->enabled);
291 }
292 }
293
294 return ret;
295 }
296
pl031_remove(struct amba_device * adev)297 static int pl031_remove(struct amba_device *adev)
298 {
299 struct pl031_local *ldata = dev_get_drvdata(&adev->dev);
300
301 amba_set_drvdata(adev, NULL);
302 free_irq(adev->irq[0], ldata->rtc);
303 rtc_device_unregister(ldata->rtc);
304 iounmap(ldata->base);
305 kfree(ldata);
306 amba_release_regions(adev);
307
308 return 0;
309 }
310
pl031_probe(struct amba_device * adev,const struct amba_id * id)311 static int pl031_probe(struct amba_device *adev, const struct amba_id *id)
312 {
313 int ret;
314 struct pl031_local *ldata;
315 struct rtc_class_ops *ops = id->data;
316 unsigned long time, data;
317
318 ret = amba_request_regions(adev, NULL);
319 if (ret)
320 goto err_req;
321
322 ldata = kzalloc(sizeof(struct pl031_local), GFP_KERNEL);
323 if (!ldata) {
324 ret = -ENOMEM;
325 goto out;
326 }
327
328 ldata->base = ioremap(adev->res.start, resource_size(&adev->res));
329
330 if (!ldata->base) {
331 ret = -ENOMEM;
332 goto out_no_remap;
333 }
334
335 amba_set_drvdata(adev, ldata);
336
337 ldata->hw_designer = amba_manf(adev);
338 ldata->hw_revision = amba_rev(adev);
339
340 dev_dbg(&adev->dev, "designer ID = 0x%02x\n", ldata->hw_designer);
341 dev_dbg(&adev->dev, "revision = 0x%01x\n", ldata->hw_revision);
342
343 data = readl(ldata->base + RTC_CR);
344 /* Enable the clockwatch on ST Variants */
345 if (ldata->hw_designer == AMBA_VENDOR_ST)
346 data |= RTC_CR_CWEN;
347 else
348 data |= RTC_CR_EN;
349 writel(data, ldata->base + RTC_CR);
350
351 /*
352 * On ST PL031 variants, the RTC reset value does not provide correct
353 * weekday for 2000-01-01. Correct the erroneous sunday to saturday.
354 */
355 if (ldata->hw_designer == AMBA_VENDOR_ST) {
356 if (readl(ldata->base + RTC_YDR) == 0x2000) {
357 time = readl(ldata->base + RTC_DR);
358 if ((time &
359 (RTC_MON_MASK | RTC_MDAY_MASK | RTC_WDAY_MASK))
360 == 0x02120000) {
361 time = time | (0x7 << RTC_WDAY_SHIFT);
362 writel(0x2000, ldata->base + RTC_YLR);
363 writel(time, ldata->base + RTC_LR);
364 }
365 }
366 }
367
368 ldata->rtc = rtc_device_register("pl031", &adev->dev, ops,
369 THIS_MODULE);
370 if (IS_ERR(ldata->rtc)) {
371 ret = PTR_ERR(ldata->rtc);
372 goto out_no_rtc;
373 }
374
375 if (request_irq(adev->irq[0], pl031_interrupt,
376 0, "rtc-pl031", ldata)) {
377 ret = -EIO;
378 goto out_no_irq;
379 }
380
381 return 0;
382
383 out_no_irq:
384 rtc_device_unregister(ldata->rtc);
385 out_no_rtc:
386 iounmap(ldata->base);
387 amba_set_drvdata(adev, NULL);
388 out_no_remap:
389 kfree(ldata);
390 out:
391 amba_release_regions(adev);
392 err_req:
393
394 return ret;
395 }
396
397 /* Operations for the original ARM version */
398 static struct rtc_class_ops arm_pl031_ops = {
399 .read_time = pl031_read_time,
400 .set_time = pl031_set_time,
401 .read_alarm = pl031_read_alarm,
402 .set_alarm = pl031_set_alarm,
403 .alarm_irq_enable = pl031_alarm_irq_enable,
404 };
405
406 /* The First ST derivative */
407 static struct rtc_class_ops stv1_pl031_ops = {
408 .read_time = pl031_read_time,
409 .set_time = pl031_set_time,
410 .read_alarm = pl031_read_alarm,
411 .set_alarm = pl031_set_alarm,
412 .alarm_irq_enable = pl031_alarm_irq_enable,
413 };
414
415 /* And the second ST derivative */
416 static struct rtc_class_ops stv2_pl031_ops = {
417 .read_time = pl031_stv2_read_time,
418 .set_time = pl031_stv2_set_time,
419 .read_alarm = pl031_stv2_read_alarm,
420 .set_alarm = pl031_stv2_set_alarm,
421 .alarm_irq_enable = pl031_alarm_irq_enable,
422 };
423
424 static struct amba_id pl031_ids[] = {
425 {
426 .id = 0x00041031,
427 .mask = 0x000fffff,
428 .data = &arm_pl031_ops,
429 },
430 /* ST Micro variants */
431 {
432 .id = 0x00180031,
433 .mask = 0x00ffffff,
434 .data = &stv1_pl031_ops,
435 },
436 {
437 .id = 0x00280031,
438 .mask = 0x00ffffff,
439 .data = &stv2_pl031_ops,
440 },
441 {0, 0},
442 };
443
444 MODULE_DEVICE_TABLE(amba, pl031_ids);
445
446 static struct amba_driver pl031_driver = {
447 .drv = {
448 .name = "rtc-pl031",
449 },
450 .id_table = pl031_ids,
451 .probe = pl031_probe,
452 .remove = pl031_remove,
453 };
454
455 module_amba_driver(pl031_driver);
456
457 MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net");
458 MODULE_DESCRIPTION("ARM AMBA PL031 RTC Driver");
459 MODULE_LICENSE("GPL");
460