2 * drivers/rtc/rtc-pl031.c
4 * Real Time Clock interface for ARM AMBA PrimeCell 031 RTC
6 * Author: Deepak Saxena <dsaxena@plexity.net>
8 * Copyright 2006 (c) MontaVista Software, Inc.
10 * Author: Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
11 * Copyright 2010 (c) ST-Ericsson AB
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.
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>
24 #include <linux/bcd.h>
25 #include <linux/delay.h>
26 #include <linux/version.h>
27 #include <linux/slab.h>
30 * Register definitions
32 #define RTC_DR 0x00 /* Data read register */
33 #define RTC_MR 0x04 /* Match register */
34 #define RTC_LR 0x08 /* Data load register */
35 #define RTC_CR 0x0c /* Control register */
36 #define RTC_IMSC 0x10 /* Interrupt mask and set register */
37 #define RTC_RIS 0x14 /* Raw interrupt status register */
38 #define RTC_MIS 0x18 /* Masked interrupt status register */
39 #define RTC_ICR 0x1c /* Interrupt clear register */
40 /* ST variants have additional timer functionality */
41 #define RTC_TDR 0x20 /* Timer data read register */
42 #define RTC_TLR 0x24 /* Timer data load register */
43 #define RTC_TCR 0x28 /* Timer control register */
44 #define RTC_YDR 0x30 /* Year data read register */
45 #define RTC_YMR 0x34 /* Year match register */
46 #define RTC_YLR 0x38 /* Year data load register */
48 #define RTC_CR_CWEN (1 << 26) /* Clockwatch enable bit */
50 #define RTC_TCR_EN (1 << 1) /* Periodic timer enable bit */
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. */
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 */
70 #define RTC_TIMER_FREQ 32768
73 struct rtc_device
*rtc
;
79 static int pl031_alarm_irq_enable(struct device
*dev
,
82 struct pl031_local
*ldata
= dev_get_drvdata(dev
);
85 /* Clear any pending alarm interrupts. */
86 writel(RTC_BIT_AI
, ldata
->base
+ RTC_ICR
);
88 imsc
= readl(ldata
->base
+ RTC_IMSC
);
91 writel(imsc
| RTC_BIT_AI
, ldata
->base
+ RTC_IMSC
);
93 writel(imsc
& ~RTC_BIT_AI
, ldata
->base
+ RTC_IMSC
);
99 * Convert Gregorian date to ST v2 RTC format.
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
)
105 int year
= tm
->tm_year
+ 1900;
106 int wday
= tm
->tm_wday
;
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
);
112 } else if (wday
== -1) {
113 /* wday is not provided, calculate it here */
115 struct rtc_time calc_tm
;
117 rtc_tm_to_time(tm
, &time
);
118 rtc_time_to_tm(time
, &calc_tm
);
119 wday
= calc_tm
.tm_wday
;
122 *bcd_year
= (bin2bcd(year
% 100) | bin2bcd(year
/ 100) << 8);
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
);
135 * Convert ST v2 RTC format to Gregorian date.
137 static int pl031_stv2_time_to_tm(unsigned long st_time
, unsigned long bcd_year
,
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
);
148 tm
->tm_yday
= rtc_year_days(tm
->tm_mday
, tm
->tm_mon
, tm
->tm_year
);
154 static int pl031_stv2_read_time(struct device
*dev
, struct rtc_time
*tm
)
156 struct pl031_local
*ldata
= dev_get_drvdata(dev
);
158 pl031_stv2_time_to_tm(readl(ldata
->base
+ RTC_DR
),
159 readl(ldata
->base
+ RTC_YDR
), tm
);
164 static int pl031_stv2_set_time(struct device
*dev
, struct rtc_time
*tm
)
167 unsigned long bcd_year
;
168 struct pl031_local
*ldata
= dev_get_drvdata(dev
);
171 ret
= pl031_stv2_tm_to_time(dev
, tm
, &time
, &bcd_year
);
173 writel(bcd_year
, ldata
->base
+ RTC_YLR
);
174 writel(time
, ldata
->base
+ RTC_LR
);
180 static int pl031_stv2_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
182 struct pl031_local
*ldata
= dev_get_drvdata(dev
);
185 ret
= pl031_stv2_time_to_tm(readl(ldata
->base
+ RTC_MR
),
186 readl(ldata
->base
+ RTC_YMR
), &alarm
->time
);
188 alarm
->pending
= readl(ldata
->base
+ RTC_RIS
) & RTC_BIT_AI
;
189 alarm
->enabled
= readl(ldata
->base
+ RTC_IMSC
) & RTC_BIT_AI
;
194 static int pl031_stv2_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
196 struct pl031_local
*ldata
= dev_get_drvdata(dev
);
198 unsigned long bcd_year
;
201 /* At the moment, we can only deal with non-wildcarded alarm times. */
202 ret
= rtc_valid_tm(&alarm
->time
);
204 ret
= pl031_stv2_tm_to_time(dev
, &alarm
->time
,
207 writel(bcd_year
, ldata
->base
+ RTC_YMR
);
208 writel(time
, ldata
->base
+ RTC_MR
);
210 pl031_alarm_irq_enable(dev
, alarm
->enabled
);
217 static irqreturn_t
pl031_interrupt(int irq
, void *dev_id
)
219 struct pl031_local
*ldata
= dev_id
;
220 unsigned long rtcmis
;
221 unsigned long events
= 0;
223 rtcmis
= readl(ldata
->base
+ RTC_MIS
);
225 writel(rtcmis
, ldata
->base
+ RTC_ICR
);
227 if (rtcmis
& RTC_BIT_AI
)
228 events
|= (RTC_AF
| RTC_IRQF
);
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
);
235 rtc_update_irq(ldata
->rtc
, 1, events
);
243 static int pl031_read_time(struct device
*dev
, struct rtc_time
*tm
)
245 struct pl031_local
*ldata
= dev_get_drvdata(dev
);
247 rtc_time_to_tm(readl(ldata
->base
+ RTC_DR
), tm
);
252 static int pl031_set_time(struct device
*dev
, struct rtc_time
*tm
)
255 struct pl031_local
*ldata
= dev_get_drvdata(dev
);
258 ret
= rtc_tm_to_time(tm
, &time
);
261 writel(time
, ldata
->base
+ RTC_LR
);
266 static int pl031_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
268 struct pl031_local
*ldata
= dev_get_drvdata(dev
);
270 rtc_time_to_tm(readl(ldata
->base
+ RTC_MR
), &alarm
->time
);
272 alarm
->pending
= readl(ldata
->base
+ RTC_RIS
) & RTC_BIT_AI
;
273 alarm
->enabled
= readl(ldata
->base
+ RTC_IMSC
) & RTC_BIT_AI
;
278 static int pl031_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
280 struct pl031_local
*ldata
= dev_get_drvdata(dev
);
284 /* At the moment, we can only deal with non-wildcarded alarm times. */
285 ret
= rtc_valid_tm(&alarm
->time
);
287 ret
= rtc_tm_to_time(&alarm
->time
, &time
);
289 writel(time
, ldata
->base
+ RTC_MR
);
290 pl031_alarm_irq_enable(dev
, alarm
->enabled
);
297 /* Periodic interrupt is only available in ST variants. */
298 static int pl031_irq_set_state(struct device
*dev
, int enabled
)
300 struct pl031_local
*ldata
= dev_get_drvdata(dev
);
303 /* Clear any pending timer interrupt. */
304 writel(RTC_BIT_PI
, ldata
->base
+ RTC_ICR
);
306 writel(readl(ldata
->base
+ RTC_IMSC
) | RTC_BIT_PI
,
307 ldata
->base
+ RTC_IMSC
);
309 /* Now start the timer */
310 writel(readl(ldata
->base
+ RTC_TCR
) | RTC_TCR_EN
,
311 ldata
->base
+ RTC_TCR
);
314 writel(readl(ldata
->base
+ RTC_IMSC
) & (~RTC_BIT_PI
),
315 ldata
->base
+ RTC_IMSC
);
317 /* Also stop the timer */
318 writel(readl(ldata
->base
+ RTC_TCR
) & (~RTC_TCR_EN
),
319 ldata
->base
+ RTC_TCR
);
321 /* Wait at least 1 RTC32 clock cycle to ensure next access
322 * to RTC_TCR will succeed.
329 static int pl031_irq_set_freq(struct device
*dev
, int freq
)
331 struct pl031_local
*ldata
= dev_get_drvdata(dev
);
333 /* Cant set timer if it is already enabled */
334 if (readl(ldata
->base
+ RTC_TCR
) & RTC_TCR_EN
) {
335 dev_err(dev
, "can't change frequency while timer enabled\n");
339 /* If self start bit in RTC_TCR is set timer will start here,
340 * but we never set that bit. Instead we start the timer when
341 * set_state is called with enabled == 1.
343 writel(RTC_TIMER_FREQ
/ freq
, ldata
->base
+ RTC_TLR
);
348 static int pl031_remove(struct amba_device
*adev
)
350 struct pl031_local
*ldata
= dev_get_drvdata(&adev
->dev
);
352 amba_set_drvdata(adev
, NULL
);
353 free_irq(adev
->irq
[0], ldata
->rtc
);
354 rtc_device_unregister(ldata
->rtc
);
355 iounmap(ldata
->base
);
357 amba_release_regions(adev
);
362 static int pl031_probe(struct amba_device
*adev
, struct amba_id
*id
)
365 struct pl031_local
*ldata
;
366 struct rtc_class_ops
*ops
= id
->data
;
368 ret
= amba_request_regions(adev
, NULL
);
372 ldata
= kzalloc(sizeof(struct pl031_local
), GFP_KERNEL
);
378 ldata
->base
= ioremap(adev
->res
.start
, resource_size(&adev
->res
));
385 amba_set_drvdata(adev
, ldata
);
387 ldata
->hw_designer
= amba_manf(adev
);
388 ldata
->hw_revision
= amba_rev(adev
);
390 dev_dbg(&adev
->dev
, "designer ID = 0x%02x\n", ldata
->hw_designer
);
391 dev_dbg(&adev
->dev
, "revision = 0x%01x\n", ldata
->hw_revision
);
393 /* Enable the clockwatch on ST Variants */
394 if ((ldata
->hw_designer
== AMBA_VENDOR_ST
) &&
395 (ldata
->hw_revision
> 1))
396 writel(readl(ldata
->base
+ RTC_CR
) | RTC_CR_CWEN
,
397 ldata
->base
+ RTC_CR
);
399 ldata
->rtc
= rtc_device_register("pl031", &adev
->dev
, ops
,
401 if (IS_ERR(ldata
->rtc
)) {
402 ret
= PTR_ERR(ldata
->rtc
);
406 if (request_irq(adev
->irq
[0], pl031_interrupt
,
407 IRQF_DISABLED
| IRQF_SHARED
, "rtc-pl031", ldata
)) {
415 rtc_device_unregister(ldata
->rtc
);
417 iounmap(ldata
->base
);
418 amba_set_drvdata(adev
, NULL
);
422 amba_release_regions(adev
);
428 /* Operations for the original ARM version */
429 static struct rtc_class_ops arm_pl031_ops
= {
430 .read_time
= pl031_read_time
,
431 .set_time
= pl031_set_time
,
432 .read_alarm
= pl031_read_alarm
,
433 .set_alarm
= pl031_set_alarm
,
434 .alarm_irq_enable
= pl031_alarm_irq_enable
,
437 /* The First ST derivative */
438 static struct rtc_class_ops stv1_pl031_ops
= {
439 .read_time
= pl031_read_time
,
440 .set_time
= pl031_set_time
,
441 .read_alarm
= pl031_read_alarm
,
442 .set_alarm
= pl031_set_alarm
,
443 .alarm_irq_enable
= pl031_alarm_irq_enable
,
444 .irq_set_state
= pl031_irq_set_state
,
445 .irq_set_freq
= pl031_irq_set_freq
,
448 /* And the second ST derivative */
449 static struct rtc_class_ops stv2_pl031_ops
= {
450 .read_time
= pl031_stv2_read_time
,
451 .set_time
= pl031_stv2_set_time
,
452 .read_alarm
= pl031_stv2_read_alarm
,
453 .set_alarm
= pl031_stv2_set_alarm
,
454 .alarm_irq_enable
= pl031_alarm_irq_enable
,
455 .irq_set_state
= pl031_irq_set_state
,
456 .irq_set_freq
= pl031_irq_set_freq
,
459 static struct amba_id pl031_ids
[] __initdata
= {
463 .data
= &arm_pl031_ops
,
465 /* ST Micro variants */
469 .data
= &stv1_pl031_ops
,
474 .data
= &stv2_pl031_ops
,
479 static struct amba_driver pl031_driver
= {
483 .id_table
= pl031_ids
,
484 .probe
= pl031_probe
,
485 .remove
= pl031_remove
,
488 static int __init
pl031_init(void)
490 return amba_driver_register(&pl031_driver
);
493 static void __exit
pl031_exit(void)
495 amba_driver_unregister(&pl031_driver
);
498 module_init(pl031_init
);
499 module_exit(pl031_exit
);
501 MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net");
502 MODULE_DESCRIPTION("ARM AMBA PL031 RTC Driver");
503 MODULE_LICENSE("GPL");