2 * SuperH On-Chip RTC Support
4 * Copyright (C) 2006 - 2009 Paul Mundt
5 * Copyright (C) 2006 Jamie Lenehan
6 * Copyright (C) 2008 Angelo Castello
8 * Based on the old arch/sh/kernel/cpu/rtc.c by:
10 * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
11 * Copyright (C) 1999 Tetsuya Okada & Niibe Yutaka
13 * This file is subject to the terms and conditions of the GNU General Public
14 * License. See the file "COPYING" in the main directory of this archive
17 #include <linux/module.h>
18 #include <linux/mod_devicetable.h>
19 #include <linux/kernel.h>
20 #include <linux/bcd.h>
21 #include <linux/rtc.h>
22 #include <linux/init.h>
23 #include <linux/platform_device.h>
24 #include <linux/seq_file.h>
25 #include <linux/interrupt.h>
26 #include <linux/spinlock.h>
28 #include <linux/log2.h>
29 #include <linux/clk.h>
30 #include <linux/slab.h>
34 /* Default values for RZ/A RTC */
35 #define rtc_reg_size sizeof(u16)
36 #define RTC_BIT_INVERTED 0 /* no chip bugs */
37 #define RTC_CAP_4_DIGIT_YEAR (1 << 0)
38 #define RTC_DEF_CAPABILITIES RTC_CAP_4_DIGIT_YEAR
41 #define DRV_NAME "sh-rtc"
43 #define RTC_REG(r) ((r) * rtc_reg_size)
45 #define R64CNT RTC_REG(0)
47 #define RSECCNT RTC_REG(1) /* RTC sec */
48 #define RMINCNT RTC_REG(2) /* RTC min */
49 #define RHRCNT RTC_REG(3) /* RTC hour */
50 #define RWKCNT RTC_REG(4) /* RTC week */
51 #define RDAYCNT RTC_REG(5) /* RTC day */
52 #define RMONCNT RTC_REG(6) /* RTC month */
53 #define RYRCNT RTC_REG(7) /* RTC year */
54 #define RSECAR RTC_REG(8) /* ALARM sec */
55 #define RMINAR RTC_REG(9) /* ALARM min */
56 #define RHRAR RTC_REG(10) /* ALARM hour */
57 #define RWKAR RTC_REG(11) /* ALARM week */
58 #define RDAYAR RTC_REG(12) /* ALARM day */
59 #define RMONAR RTC_REG(13) /* ALARM month */
60 #define RCR1 RTC_REG(14) /* Control */
61 #define RCR2 RTC_REG(15) /* Control */
64 * Note on RYRAR and RCR3: Up until this point most of the register
65 * definitions are consistent across all of the available parts. However,
66 * the placement of the optional RYRAR and RCR3 (the RYRAR control
67 * register used to control RYRCNT/RYRAR compare) varies considerably
68 * across various parts, occasionally being mapped in to a completely
69 * unrelated address space. For proper RYRAR support a separate resource
70 * would have to be handed off, but as this is purely optional in
71 * practice, we simply opt not to support it, thereby keeping the code
72 * quite a bit more simplified.
75 /* ALARM Bits - or with BCD encoded value */
76 #define AR_ENB 0x80 /* Enable for alarm cmp */
79 #define PF_HP 0x100 /* Enable Half Period to support 8,32,128Hz */
80 #define PF_COUNT 0x200 /* Half periodic counter */
81 #define PF_OXS 0x400 /* Periodic One x Second */
82 #define PF_KOU 0x800 /* Kernel or User periodic request 1=kernel */
86 #define RCR1_CF 0x80 /* Carry Flag */
87 #define RCR1_CIE 0x10 /* Carry Interrupt Enable */
88 #define RCR1_AIE 0x08 /* Alarm Interrupt Enable */
89 #define RCR1_AF 0x01 /* Alarm Flag */
92 #define RCR2_PEF 0x80 /* PEriodic interrupt Flag */
93 #define RCR2_PESMASK 0x70 /* Periodic interrupt Set */
94 #define RCR2_RTCEN 0x08 /* ENable RTC */
95 #define RCR2_ADJ 0x04 /* ADJustment (30-second) */
96 #define RCR2_RESET 0x02 /* Reset bit */
97 #define RCR2_START 0x01 /* Start bit */
100 void __iomem
*regbase
;
101 unsigned long regsize
;
102 struct resource
*res
;
107 struct rtc_device
*rtc_dev
;
109 unsigned long capabilities
; /* See asm/rtc.h for cap bits */
110 unsigned short periodic_freq
;
113 static int __sh_rtc_interrupt(struct sh_rtc
*rtc
)
115 unsigned int tmp
, pending
;
117 tmp
= readb(rtc
->regbase
+ RCR1
);
118 pending
= tmp
& RCR1_CF
;
120 writeb(tmp
, rtc
->regbase
+ RCR1
);
122 /* Users have requested One x Second IRQ */
123 if (pending
&& rtc
->periodic_freq
& PF_OXS
)
124 rtc_update_irq(rtc
->rtc_dev
, 1, RTC_UF
| RTC_IRQF
);
129 static int __sh_rtc_alarm(struct sh_rtc
*rtc
)
131 unsigned int tmp
, pending
;
133 tmp
= readb(rtc
->regbase
+ RCR1
);
134 pending
= tmp
& RCR1_AF
;
135 tmp
&= ~(RCR1_AF
| RCR1_AIE
);
136 writeb(tmp
, rtc
->regbase
+ RCR1
);
139 rtc_update_irq(rtc
->rtc_dev
, 1, RTC_AF
| RTC_IRQF
);
144 static int __sh_rtc_periodic(struct sh_rtc
*rtc
)
146 unsigned int tmp
, pending
;
148 tmp
= readb(rtc
->regbase
+ RCR2
);
149 pending
= tmp
& RCR2_PEF
;
151 writeb(tmp
, rtc
->regbase
+ RCR2
);
156 /* Half period enabled than one skipped and the next notified */
157 if ((rtc
->periodic_freq
& PF_HP
) && (rtc
->periodic_freq
& PF_COUNT
))
158 rtc
->periodic_freq
&= ~PF_COUNT
;
160 if (rtc
->periodic_freq
& PF_HP
)
161 rtc
->periodic_freq
|= PF_COUNT
;
162 rtc_update_irq(rtc
->rtc_dev
, 1, RTC_PF
| RTC_IRQF
);
168 static irqreturn_t
sh_rtc_interrupt(int irq
, void *dev_id
)
170 struct sh_rtc
*rtc
= dev_id
;
173 spin_lock(&rtc
->lock
);
174 ret
= __sh_rtc_interrupt(rtc
);
175 spin_unlock(&rtc
->lock
);
177 return IRQ_RETVAL(ret
);
180 static irqreturn_t
sh_rtc_alarm(int irq
, void *dev_id
)
182 struct sh_rtc
*rtc
= dev_id
;
185 spin_lock(&rtc
->lock
);
186 ret
= __sh_rtc_alarm(rtc
);
187 spin_unlock(&rtc
->lock
);
189 return IRQ_RETVAL(ret
);
192 static irqreturn_t
sh_rtc_periodic(int irq
, void *dev_id
)
194 struct sh_rtc
*rtc
= dev_id
;
197 spin_lock(&rtc
->lock
);
198 ret
= __sh_rtc_periodic(rtc
);
199 spin_unlock(&rtc
->lock
);
201 return IRQ_RETVAL(ret
);
204 static irqreturn_t
sh_rtc_shared(int irq
, void *dev_id
)
206 struct sh_rtc
*rtc
= dev_id
;
209 spin_lock(&rtc
->lock
);
210 ret
= __sh_rtc_interrupt(rtc
);
211 ret
|= __sh_rtc_alarm(rtc
);
212 ret
|= __sh_rtc_periodic(rtc
);
213 spin_unlock(&rtc
->lock
);
215 return IRQ_RETVAL(ret
);
218 static inline void sh_rtc_setaie(struct device
*dev
, unsigned int enable
)
220 struct sh_rtc
*rtc
= dev_get_drvdata(dev
);
223 spin_lock_irq(&rtc
->lock
);
225 tmp
= readb(rtc
->regbase
+ RCR1
);
232 writeb(tmp
, rtc
->regbase
+ RCR1
);
234 spin_unlock_irq(&rtc
->lock
);
237 static int sh_rtc_proc(struct device
*dev
, struct seq_file
*seq
)
239 struct sh_rtc
*rtc
= dev_get_drvdata(dev
);
242 tmp
= readb(rtc
->regbase
+ RCR1
);
243 seq_printf(seq
, "carry_IRQ\t: %s\n", (tmp
& RCR1_CIE
) ? "yes" : "no");
245 tmp
= readb(rtc
->regbase
+ RCR2
);
246 seq_printf(seq
, "periodic_IRQ\t: %s\n",
247 (tmp
& RCR2_PESMASK
) ? "yes" : "no");
252 static inline void sh_rtc_setcie(struct device
*dev
, unsigned int enable
)
254 struct sh_rtc
*rtc
= dev_get_drvdata(dev
);
257 spin_lock_irq(&rtc
->lock
);
259 tmp
= readb(rtc
->regbase
+ RCR1
);
266 writeb(tmp
, rtc
->regbase
+ RCR1
);
268 spin_unlock_irq(&rtc
->lock
);
271 static int sh_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
273 sh_rtc_setaie(dev
, enabled
);
277 static int sh_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
279 struct sh_rtc
*rtc
= dev_get_drvdata(dev
);
280 unsigned int sec128
, sec2
, yr
, yr100
, cf_bit
;
285 spin_lock_irq(&rtc
->lock
);
287 tmp
= readb(rtc
->regbase
+ RCR1
);
288 tmp
&= ~RCR1_CF
; /* Clear CF-bit */
290 writeb(tmp
, rtc
->regbase
+ RCR1
);
292 sec128
= readb(rtc
->regbase
+ R64CNT
);
294 tm
->tm_sec
= bcd2bin(readb(rtc
->regbase
+ RSECCNT
));
295 tm
->tm_min
= bcd2bin(readb(rtc
->regbase
+ RMINCNT
));
296 tm
->tm_hour
= bcd2bin(readb(rtc
->regbase
+ RHRCNT
));
297 tm
->tm_wday
= bcd2bin(readb(rtc
->regbase
+ RWKCNT
));
298 tm
->tm_mday
= bcd2bin(readb(rtc
->regbase
+ RDAYCNT
));
299 tm
->tm_mon
= bcd2bin(readb(rtc
->regbase
+ RMONCNT
)) - 1;
301 if (rtc
->capabilities
& RTC_CAP_4_DIGIT_YEAR
) {
302 yr
= readw(rtc
->regbase
+ RYRCNT
);
303 yr100
= bcd2bin(yr
>> 8);
306 yr
= readb(rtc
->regbase
+ RYRCNT
);
307 yr100
= bcd2bin((yr
== 0x99) ? 0x19 : 0x20);
310 tm
->tm_year
= (yr100
* 100 + bcd2bin(yr
)) - 1900;
312 sec2
= readb(rtc
->regbase
+ R64CNT
);
313 cf_bit
= readb(rtc
->regbase
+ RCR1
) & RCR1_CF
;
315 spin_unlock_irq(&rtc
->lock
);
316 } while (cf_bit
!= 0 || ((sec128
^ sec2
) & RTC_BIT_INVERTED
) != 0);
318 #if RTC_BIT_INVERTED != 0
319 if ((sec128
& RTC_BIT_INVERTED
))
323 /* only keep the carry interrupt enabled if UIE is on */
324 if (!(rtc
->periodic_freq
& PF_OXS
))
325 sh_rtc_setcie(dev
, 0);
327 dev_dbg(dev
, "%s: tm is secs=%d, mins=%d, hours=%d, "
328 "mday=%d, mon=%d, year=%d, wday=%d\n",
330 tm
->tm_sec
, tm
->tm_min
, tm
->tm_hour
,
331 tm
->tm_mday
, tm
->tm_mon
+ 1, tm
->tm_year
, tm
->tm_wday
);
336 static int sh_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
338 struct sh_rtc
*rtc
= dev_get_drvdata(dev
);
342 spin_lock_irq(&rtc
->lock
);
344 /* Reset pre-scaler & stop RTC */
345 tmp
= readb(rtc
->regbase
+ RCR2
);
348 writeb(tmp
, rtc
->regbase
+ RCR2
);
350 writeb(bin2bcd(tm
->tm_sec
), rtc
->regbase
+ RSECCNT
);
351 writeb(bin2bcd(tm
->tm_min
), rtc
->regbase
+ RMINCNT
);
352 writeb(bin2bcd(tm
->tm_hour
), rtc
->regbase
+ RHRCNT
);
353 writeb(bin2bcd(tm
->tm_wday
), rtc
->regbase
+ RWKCNT
);
354 writeb(bin2bcd(tm
->tm_mday
), rtc
->regbase
+ RDAYCNT
);
355 writeb(bin2bcd(tm
->tm_mon
+ 1), rtc
->regbase
+ RMONCNT
);
357 if (rtc
->capabilities
& RTC_CAP_4_DIGIT_YEAR
) {
358 year
= (bin2bcd((tm
->tm_year
+ 1900) / 100) << 8) |
359 bin2bcd(tm
->tm_year
% 100);
360 writew(year
, rtc
->regbase
+ RYRCNT
);
362 year
= tm
->tm_year
% 100;
363 writeb(bin2bcd(year
), rtc
->regbase
+ RYRCNT
);
367 tmp
= readb(rtc
->regbase
+ RCR2
);
369 tmp
|= RCR2_RTCEN
| RCR2_START
;
370 writeb(tmp
, rtc
->regbase
+ RCR2
);
372 spin_unlock_irq(&rtc
->lock
);
377 static inline int sh_rtc_read_alarm_value(struct sh_rtc
*rtc
, int reg_off
)
380 int value
= 0xff; /* return 0xff for ignored values */
382 byte
= readb(rtc
->regbase
+ reg_off
);
384 byte
&= ~AR_ENB
; /* strip the enable bit */
385 value
= bcd2bin(byte
);
391 static int sh_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*wkalrm
)
393 struct sh_rtc
*rtc
= dev_get_drvdata(dev
);
394 struct rtc_time
*tm
= &wkalrm
->time
;
396 spin_lock_irq(&rtc
->lock
);
398 tm
->tm_sec
= sh_rtc_read_alarm_value(rtc
, RSECAR
);
399 tm
->tm_min
= sh_rtc_read_alarm_value(rtc
, RMINAR
);
400 tm
->tm_hour
= sh_rtc_read_alarm_value(rtc
, RHRAR
);
401 tm
->tm_wday
= sh_rtc_read_alarm_value(rtc
, RWKAR
);
402 tm
->tm_mday
= sh_rtc_read_alarm_value(rtc
, RDAYAR
);
403 tm
->tm_mon
= sh_rtc_read_alarm_value(rtc
, RMONAR
);
405 tm
->tm_mon
-= 1; /* RTC is 1-12, tm_mon is 0-11 */
407 wkalrm
->enabled
= (readb(rtc
->regbase
+ RCR1
) & RCR1_AIE
) ? 1 : 0;
409 spin_unlock_irq(&rtc
->lock
);
414 static inline void sh_rtc_write_alarm_value(struct sh_rtc
*rtc
,
415 int value
, int reg_off
)
417 /* < 0 for a value that is ignored */
419 writeb(0, rtc
->regbase
+ reg_off
);
421 writeb(bin2bcd(value
) | AR_ENB
, rtc
->regbase
+ reg_off
);
424 static int sh_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*wkalrm
)
426 struct sh_rtc
*rtc
= dev_get_drvdata(dev
);
428 struct rtc_time
*tm
= &wkalrm
->time
;
431 spin_lock_irq(&rtc
->lock
);
433 /* disable alarm interrupt and clear the alarm flag */
434 rcr1
= readb(rtc
->regbase
+ RCR1
);
435 rcr1
&= ~(RCR1_AF
| RCR1_AIE
);
436 writeb(rcr1
, rtc
->regbase
+ RCR1
);
439 sh_rtc_write_alarm_value(rtc
, tm
->tm_sec
, RSECAR
);
440 sh_rtc_write_alarm_value(rtc
, tm
->tm_min
, RMINAR
);
441 sh_rtc_write_alarm_value(rtc
, tm
->tm_hour
, RHRAR
);
442 sh_rtc_write_alarm_value(rtc
, tm
->tm_wday
, RWKAR
);
443 sh_rtc_write_alarm_value(rtc
, tm
->tm_mday
, RDAYAR
);
447 sh_rtc_write_alarm_value(rtc
, mon
, RMONAR
);
449 if (wkalrm
->enabled
) {
451 writeb(rcr1
, rtc
->regbase
+ RCR1
);
454 spin_unlock_irq(&rtc
->lock
);
459 static const struct rtc_class_ops sh_rtc_ops
= {
460 .read_time
= sh_rtc_read_time
,
461 .set_time
= sh_rtc_set_time
,
462 .read_alarm
= sh_rtc_read_alarm
,
463 .set_alarm
= sh_rtc_set_alarm
,
465 .alarm_irq_enable
= sh_rtc_alarm_irq_enable
,
468 static int __init
sh_rtc_probe(struct platform_device
*pdev
)
471 struct resource
*res
;
476 rtc
= devm_kzalloc(&pdev
->dev
, sizeof(*rtc
), GFP_KERNEL
);
480 spin_lock_init(&rtc
->lock
);
482 /* get periodic/carry/alarm irqs */
483 ret
= platform_get_irq(pdev
, 0);
484 if (unlikely(ret
<= 0)) {
485 dev_err(&pdev
->dev
, "No IRQ resource\n");
489 rtc
->periodic_irq
= ret
;
490 rtc
->carry_irq
= platform_get_irq(pdev
, 1);
491 rtc
->alarm_irq
= platform_get_irq(pdev
, 2);
493 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
495 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
496 if (unlikely(res
== NULL
)) {
497 dev_err(&pdev
->dev
, "No IO resource\n");
501 rtc
->regsize
= resource_size(res
);
503 rtc
->res
= devm_request_mem_region(&pdev
->dev
, res
->start
,
504 rtc
->regsize
, pdev
->name
);
505 if (unlikely(!rtc
->res
))
508 rtc
->regbase
= devm_ioremap_nocache(&pdev
->dev
, rtc
->res
->start
,
510 if (unlikely(!rtc
->regbase
))
513 if (!pdev
->dev
.of_node
) {
515 /* With a single device, the clock id is still "rtc0" */
519 snprintf(clk_name
, sizeof(clk_name
), "rtc%d", clk_id
);
521 snprintf(clk_name
, sizeof(clk_name
), "fck");
523 rtc
->clk
= devm_clk_get(&pdev
->dev
, clk_name
);
524 if (IS_ERR(rtc
->clk
)) {
526 * No error handling for rtc->clk intentionally, not all
527 * platforms will have a unique clock for the RTC, and
528 * the clk API can handle the struct clk pointer being
534 clk_enable(rtc
->clk
);
536 rtc
->capabilities
= RTC_DEF_CAPABILITIES
;
539 if (dev_get_platdata(&pdev
->dev
)) {
540 struct sh_rtc_platform_info
*pinfo
=
541 dev_get_platdata(&pdev
->dev
);
544 * Some CPUs have special capabilities in addition to the
545 * default set. Add those in here.
547 rtc
->capabilities
|= pinfo
->capabilities
;
551 if (rtc
->carry_irq
<= 0) {
552 /* register shared periodic/carry/alarm irq */
553 ret
= devm_request_irq(&pdev
->dev
, rtc
->periodic_irq
,
554 sh_rtc_shared
, 0, "sh-rtc", rtc
);
557 "request IRQ failed with %d, IRQ %d\n", ret
,
562 /* register periodic/carry/alarm irqs */
563 ret
= devm_request_irq(&pdev
->dev
, rtc
->periodic_irq
,
564 sh_rtc_periodic
, 0, "sh-rtc period", rtc
);
567 "request period IRQ failed with %d, IRQ %d\n",
568 ret
, rtc
->periodic_irq
);
572 ret
= devm_request_irq(&pdev
->dev
, rtc
->carry_irq
,
573 sh_rtc_interrupt
, 0, "sh-rtc carry", rtc
);
576 "request carry IRQ failed with %d, IRQ %d\n",
577 ret
, rtc
->carry_irq
);
581 ret
= devm_request_irq(&pdev
->dev
, rtc
->alarm_irq
,
582 sh_rtc_alarm
, 0, "sh-rtc alarm", rtc
);
585 "request alarm IRQ failed with %d, IRQ %d\n",
586 ret
, rtc
->alarm_irq
);
591 platform_set_drvdata(pdev
, rtc
);
593 /* everything disabled by default */
594 sh_rtc_setaie(&pdev
->dev
, 0);
595 sh_rtc_setcie(&pdev
->dev
, 0);
597 rtc
->rtc_dev
= devm_rtc_device_register(&pdev
->dev
, "sh",
598 &sh_rtc_ops
, THIS_MODULE
);
599 if (IS_ERR(rtc
->rtc_dev
)) {
600 ret
= PTR_ERR(rtc
->rtc_dev
);
604 rtc
->rtc_dev
->max_user_freq
= 256;
606 /* reset rtc to epoch 0 if time is invalid */
607 if (rtc_read_time(rtc
->rtc_dev
, &r
) < 0) {
608 rtc_time_to_tm(0, &r
);
609 rtc_set_time(rtc
->rtc_dev
, &r
);
612 device_init_wakeup(&pdev
->dev
, 1);
616 clk_disable(rtc
->clk
);
621 static int __exit
sh_rtc_remove(struct platform_device
*pdev
)
623 struct sh_rtc
*rtc
= platform_get_drvdata(pdev
);
625 sh_rtc_setaie(&pdev
->dev
, 0);
626 sh_rtc_setcie(&pdev
->dev
, 0);
628 clk_disable(rtc
->clk
);
633 static void sh_rtc_set_irq_wake(struct device
*dev
, int enabled
)
635 struct sh_rtc
*rtc
= dev_get_drvdata(dev
);
637 irq_set_irq_wake(rtc
->periodic_irq
, enabled
);
639 if (rtc
->carry_irq
> 0) {
640 irq_set_irq_wake(rtc
->carry_irq
, enabled
);
641 irq_set_irq_wake(rtc
->alarm_irq
, enabled
);
645 static int __maybe_unused
sh_rtc_suspend(struct device
*dev
)
647 if (device_may_wakeup(dev
))
648 sh_rtc_set_irq_wake(dev
, 1);
653 static int __maybe_unused
sh_rtc_resume(struct device
*dev
)
655 if (device_may_wakeup(dev
))
656 sh_rtc_set_irq_wake(dev
, 0);
661 static SIMPLE_DEV_PM_OPS(sh_rtc_pm_ops
, sh_rtc_suspend
, sh_rtc_resume
);
663 static const struct of_device_id sh_rtc_of_match
[] = {
664 { .compatible
= "renesas,sh-rtc", },
667 MODULE_DEVICE_TABLE(of
, sh_rtc_of_match
);
669 static struct platform_driver sh_rtc_platform_driver
= {
672 .pm
= &sh_rtc_pm_ops
,
673 .of_match_table
= sh_rtc_of_match
,
675 .remove
= __exit_p(sh_rtc_remove
),
678 module_platform_driver_probe(sh_rtc_platform_driver
, sh_rtc_probe
);
680 MODULE_DESCRIPTION("SuperH on-chip RTC driver");
681 MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, "
682 "Jamie Lenehan <lenehan@twibble.org>, "
683 "Angelo Castello <angelo.castello@st.com>");
684 MODULE_LICENSE("GPL");
685 MODULE_ALIAS("platform:" DRV_NAME
);