2 * SuperH On-Chip RTC Support
4 * Copyright (C) 2006, 2007, 2008 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/kernel.h>
19 #include <linux/bcd.h>
20 #include <linux/rtc.h>
21 #include <linux/init.h>
22 #include <linux/platform_device.h>
23 #include <linux/seq_file.h>
24 #include <linux/interrupt.h>
25 #include <linux/spinlock.h>
27 #include <linux/log2.h>
30 #define DRV_NAME "sh-rtc"
31 #define DRV_VERSION "0.2.0"
33 #define RTC_REG(r) ((r) * rtc_reg_size)
35 #define R64CNT RTC_REG(0)
37 #define RSECCNT RTC_REG(1) /* RTC sec */
38 #define RMINCNT RTC_REG(2) /* RTC min */
39 #define RHRCNT RTC_REG(3) /* RTC hour */
40 #define RWKCNT RTC_REG(4) /* RTC week */
41 #define RDAYCNT RTC_REG(5) /* RTC day */
42 #define RMONCNT RTC_REG(6) /* RTC month */
43 #define RYRCNT RTC_REG(7) /* RTC year */
44 #define RSECAR RTC_REG(8) /* ALARM sec */
45 #define RMINAR RTC_REG(9) /* ALARM min */
46 #define RHRAR RTC_REG(10) /* ALARM hour */
47 #define RWKAR RTC_REG(11) /* ALARM week */
48 #define RDAYAR RTC_REG(12) /* ALARM day */
49 #define RMONAR RTC_REG(13) /* ALARM month */
50 #define RCR1 RTC_REG(14) /* Control */
51 #define RCR2 RTC_REG(15) /* Control */
54 * Note on RYRAR and RCR3: Up until this point most of the register
55 * definitions are consistent across all of the available parts. However,
56 * the placement of the optional RYRAR and RCR3 (the RYRAR control
57 * register used to control RYRCNT/RYRAR compare) varies considerably
58 * across various parts, occasionally being mapped in to a completely
59 * unrelated address space. For proper RYRAR support a separate resource
60 * would have to be handed off, but as this is purely optional in
61 * practice, we simply opt not to support it, thereby keeping the code
62 * quite a bit more simplified.
65 /* ALARM Bits - or with BCD encoded value */
66 #define AR_ENB 0x80 /* Enable for alarm cmp */
69 #define PF_HP 0x100 /* Enable Half Period to support 8,32,128Hz */
70 #define PF_COUNT 0x200 /* Half periodic counter */
71 #define PF_OXS 0x400 /* Periodic One x Second */
72 #define PF_KOU 0x800 /* Kernel or User periodic request 1=kernel */
76 #define RCR1_CF 0x80 /* Carry Flag */
77 #define RCR1_CIE 0x10 /* Carry Interrupt Enable */
78 #define RCR1_AIE 0x08 /* Alarm Interrupt Enable */
79 #define RCR1_AF 0x01 /* Alarm Flag */
82 #define RCR2_PEF 0x80 /* PEriodic interrupt Flag */
83 #define RCR2_PESMASK 0x70 /* Periodic interrupt Set */
84 #define RCR2_RTCEN 0x08 /* ENable RTC */
85 #define RCR2_ADJ 0x04 /* ADJustment (30-second) */
86 #define RCR2_RESET 0x02 /* Reset bit */
87 #define RCR2_START 0x01 /* Start bit */
90 void __iomem *regbase;
91 unsigned long regsize;
96 struct rtc_device *rtc_dev;
98 unsigned long capabilities; /* See asm-sh/rtc.h for cap bits */
99 unsigned short periodic_freq;
102 static irqreturn_t sh_rtc_interrupt(int irq, void *dev_id)
104 struct sh_rtc *rtc = dev_id;
107 spin_lock(&rtc->lock);
109 tmp = readb(rtc->regbase + RCR1);
111 writeb(tmp, rtc->regbase + RCR1);
113 /* Users have requested One x Second IRQ */
114 if (rtc->periodic_freq & PF_OXS)
115 rtc_update_irq(rtc->rtc_dev, 1, RTC_UF | RTC_IRQF);
117 spin_unlock(&rtc->lock);
122 static irqreturn_t sh_rtc_alarm(int irq, void *dev_id)
124 struct sh_rtc *rtc = dev_id;
127 spin_lock(&rtc->lock);
129 tmp = readb(rtc->regbase + RCR1);
130 tmp &= ~(RCR1_AF | RCR1_AIE);
131 writeb(tmp, rtc->regbase + RCR1);
133 rtc_update_irq(rtc->rtc_dev, 1, RTC_AF | RTC_IRQF);
135 spin_unlock(&rtc->lock);
140 static irqreturn_t sh_rtc_periodic(int irq, void *dev_id)
142 struct sh_rtc *rtc = dev_id;
143 struct rtc_device *rtc_dev = rtc->rtc_dev;
146 spin_lock(&rtc->lock);
148 tmp = readb(rtc->regbase + RCR2);
150 writeb(tmp, rtc->regbase + RCR2);
152 /* Half period enabled than one skipped and the next notified */
153 if ((rtc->periodic_freq & PF_HP) && (rtc->periodic_freq & PF_COUNT))
154 rtc->periodic_freq &= ~PF_COUNT;
156 if (rtc->periodic_freq & PF_HP)
157 rtc->periodic_freq |= PF_COUNT;
158 if (rtc->periodic_freq & PF_KOU) {
159 spin_lock(&rtc_dev->irq_task_lock);
160 if (rtc_dev->irq_task)
161 rtc_dev->irq_task->func(rtc_dev->irq_task->private_data);
162 spin_unlock(&rtc_dev->irq_task_lock);
164 rtc_update_irq(rtc->rtc_dev, 1, RTC_PF | RTC_IRQF);
167 spin_unlock(&rtc->lock);
172 static inline void sh_rtc_setpie(struct device *dev, unsigned int enable)
174 struct sh_rtc *rtc = dev_get_drvdata(dev);
177 spin_lock_irq(&rtc->lock);
179 tmp = readb(rtc->regbase + RCR2);
182 tmp &= ~RCR2_PEF; /* Clear PES bit */
183 tmp |= (rtc->periodic_freq & ~PF_HP); /* Set PES2-0 */
185 tmp &= ~(RCR2_PESMASK | RCR2_PEF);
187 writeb(tmp, rtc->regbase + RCR2);
189 spin_unlock_irq(&rtc->lock);
192 static inline int sh_rtc_setfreq(struct device *dev, unsigned int freq)
194 struct sh_rtc *rtc = dev_get_drvdata(dev);
197 spin_lock_irq(&rtc->lock);
198 tmp = rtc->periodic_freq & PF_MASK;
202 rtc->periodic_freq = 0x00;
205 rtc->periodic_freq = 0x60;
208 rtc->periodic_freq = 0x50;
211 rtc->periodic_freq = 0x40;
214 rtc->periodic_freq = 0x30 | PF_HP;
217 rtc->periodic_freq = 0x30;
220 rtc->periodic_freq = 0x20 | PF_HP;
223 rtc->periodic_freq = 0x20;
226 rtc->periodic_freq = 0x10 | PF_HP;
229 rtc->periodic_freq = 0x10;
236 rtc->periodic_freq |= tmp;
237 rtc->rtc_dev->irq_freq = freq;
240 spin_unlock_irq(&rtc->lock);
244 static inline void sh_rtc_setaie(struct device *dev, unsigned int enable)
246 struct sh_rtc *rtc = dev_get_drvdata(dev);
249 spin_lock_irq(&rtc->lock);
251 tmp = readb(rtc->regbase + RCR1);
258 writeb(tmp, rtc->regbase + RCR1);
260 spin_unlock_irq(&rtc->lock);
263 static int sh_rtc_proc(struct device *dev, struct seq_file *seq)
265 struct sh_rtc *rtc = dev_get_drvdata(dev);
268 tmp = readb(rtc->regbase + RCR1);
269 seq_printf(seq, "carry_IRQ\t: %s\n", (tmp & RCR1_CIE) ? "yes" : "no");
271 tmp = readb(rtc->regbase + RCR2);
272 seq_printf(seq, "periodic_IRQ\t: %s\n",
273 (tmp & RCR2_PESMASK) ? "yes" : "no");
278 static int sh_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
280 struct sh_rtc *rtc = dev_get_drvdata(dev);
281 unsigned int ret = 0;
286 sh_rtc_setpie(dev, cmd == RTC_PIE_ON);
290 sh_rtc_setaie(dev, cmd == RTC_AIE_ON);
293 rtc->periodic_freq &= ~PF_OXS;
296 rtc->periodic_freq |= PF_OXS;
299 ret = put_user(rtc->rtc_dev->irq_freq,
300 (unsigned long __user *)arg);
303 ret = sh_rtc_setfreq(dev, arg);
312 static int sh_rtc_read_time(struct device *dev, struct rtc_time *tm)
314 struct platform_device *pdev = to_platform_device(dev);
315 struct sh_rtc *rtc = platform_get_drvdata(pdev);
316 unsigned int sec128, sec2, yr, yr100, cf_bit;
321 spin_lock_irq(&rtc->lock);
323 tmp = readb(rtc->regbase + RCR1);
324 tmp &= ~RCR1_CF; /* Clear CF-bit */
326 writeb(tmp, rtc->regbase + RCR1);
328 sec128 = readb(rtc->regbase + R64CNT);
330 tm->tm_sec = bcd2bin(readb(rtc->regbase + RSECCNT));
331 tm->tm_min = bcd2bin(readb(rtc->regbase + RMINCNT));
332 tm->tm_hour = bcd2bin(readb(rtc->regbase + RHRCNT));
333 tm->tm_wday = bcd2bin(readb(rtc->regbase + RWKCNT));
334 tm->tm_mday = bcd2bin(readb(rtc->regbase + RDAYCNT));
335 tm->tm_mon = bcd2bin(readb(rtc->regbase + RMONCNT)) - 1;
337 if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) {
338 yr = readw(rtc->regbase + RYRCNT);
339 yr100 = bcd2bin(yr >> 8);
342 yr = readb(rtc->regbase + RYRCNT);
343 yr100 = bcd2bin((yr == 0x99) ? 0x19 : 0x20);
346 tm->tm_year = (yr100 * 100 + bcd2bin(yr)) - 1900;
348 sec2 = readb(rtc->regbase + R64CNT);
349 cf_bit = readb(rtc->regbase + RCR1) & RCR1_CF;
351 spin_unlock_irq(&rtc->lock);
352 } while (cf_bit != 0 || ((sec128 ^ sec2) & RTC_BIT_INVERTED) != 0);
354 #if RTC_BIT_INVERTED != 0
355 if ((sec128 & RTC_BIT_INVERTED))
359 dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
360 "mday=%d, mon=%d, year=%d, wday=%d\n",
362 tm->tm_sec, tm->tm_min, tm->tm_hour,
363 tm->tm_mday, tm->tm_mon + 1, tm->tm_year, tm->tm_wday);
365 if (rtc_valid_tm(tm) < 0) {
366 dev_err(dev, "invalid date\n");
367 rtc_time_to_tm(0, tm);
373 static int sh_rtc_set_time(struct device *dev, struct rtc_time *tm)
375 struct platform_device *pdev = to_platform_device(dev);
376 struct sh_rtc *rtc = platform_get_drvdata(pdev);
380 spin_lock_irq(&rtc->lock);
382 /* Reset pre-scaler & stop RTC */
383 tmp = readb(rtc->regbase + RCR2);
386 writeb(tmp, rtc->regbase + RCR2);
388 writeb(bin2bcd(tm->tm_sec), rtc->regbase + RSECCNT);
389 writeb(bin2bcd(tm->tm_min), rtc->regbase + RMINCNT);
390 writeb(bin2bcd(tm->tm_hour), rtc->regbase + RHRCNT);
391 writeb(bin2bcd(tm->tm_wday), rtc->regbase + RWKCNT);
392 writeb(bin2bcd(tm->tm_mday), rtc->regbase + RDAYCNT);
393 writeb(bin2bcd(tm->tm_mon + 1), rtc->regbase + RMONCNT);
395 if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) {
396 year = (bin2bcd((tm->tm_year + 1900) / 100) << 8) |
397 bin2bcd(tm->tm_year % 100);
398 writew(year, rtc->regbase + RYRCNT);
400 year = tm->tm_year % 100;
401 writeb(bin2bcd(year), rtc->regbase + RYRCNT);
405 tmp = readb(rtc->regbase + RCR2);
407 tmp |= RCR2_RTCEN | RCR2_START;
408 writeb(tmp, rtc->regbase + RCR2);
410 spin_unlock_irq(&rtc->lock);
415 static inline int sh_rtc_read_alarm_value(struct sh_rtc *rtc, int reg_off)
418 int value = 0xff; /* return 0xff for ignored values */
420 byte = readb(rtc->regbase + reg_off);
422 byte &= ~AR_ENB; /* strip the enable bit */
423 value = bcd2bin(byte);
429 static int sh_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
431 struct platform_device *pdev = to_platform_device(dev);
432 struct sh_rtc *rtc = platform_get_drvdata(pdev);
433 struct rtc_time *tm = &wkalrm->time;
435 spin_lock_irq(&rtc->lock);
437 tm->tm_sec = sh_rtc_read_alarm_value(rtc, RSECAR);
438 tm->tm_min = sh_rtc_read_alarm_value(rtc, RMINAR);
439 tm->tm_hour = sh_rtc_read_alarm_value(rtc, RHRAR);
440 tm->tm_wday = sh_rtc_read_alarm_value(rtc, RWKAR);
441 tm->tm_mday = sh_rtc_read_alarm_value(rtc, RDAYAR);
442 tm->tm_mon = sh_rtc_read_alarm_value(rtc, RMONAR);
444 tm->tm_mon -= 1; /* RTC is 1-12, tm_mon is 0-11 */
445 tm->tm_year = 0xffff;
447 wkalrm->enabled = (readb(rtc->regbase + RCR1) & RCR1_AIE) ? 1 : 0;
449 spin_unlock_irq(&rtc->lock);
454 static inline void sh_rtc_write_alarm_value(struct sh_rtc *rtc,
455 int value, int reg_off)
457 /* < 0 for a value that is ignored */
459 writeb(0, rtc->regbase + reg_off);
461 writeb(bin2bcd(value) | AR_ENB, rtc->regbase + reg_off);
464 static int sh_rtc_check_alarm(struct rtc_time *tm)
467 * The original rtc says anything > 0xc0 is "don't care" or "match
468 * all" - most users use 0xff but rtc-dev uses -1 for the same thing.
469 * The original rtc doesn't support years - some things use -1 and
470 * some 0xffff. We use -1 to make out tests easier.
472 if (tm->tm_year == 0xffff)
474 if (tm->tm_mon >= 0xff)
476 if (tm->tm_mday >= 0xff)
478 if (tm->tm_wday >= 0xff)
480 if (tm->tm_hour >= 0xff)
482 if (tm->tm_min >= 0xff)
484 if (tm->tm_sec >= 0xff)
487 if (tm->tm_year > 9999 ||
489 tm->tm_mday == 0 || tm->tm_mday >= 32 ||
499 static int sh_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
501 struct platform_device *pdev = to_platform_device(dev);
502 struct sh_rtc *rtc = platform_get_drvdata(pdev);
504 struct rtc_time *tm = &wkalrm->time;
507 err = sh_rtc_check_alarm(tm);
508 if (unlikely(err < 0))
511 spin_lock_irq(&rtc->lock);
513 /* disable alarm interrupt and clear the alarm flag */
514 rcr1 = readb(rtc->regbase + RCR1);
515 rcr1 &= ~(RCR1_AF | RCR1_AIE);
516 writeb(rcr1, rtc->regbase + RCR1);
519 sh_rtc_write_alarm_value(rtc, tm->tm_sec, RSECAR);
520 sh_rtc_write_alarm_value(rtc, tm->tm_min, RMINAR);
521 sh_rtc_write_alarm_value(rtc, tm->tm_hour, RHRAR);
522 sh_rtc_write_alarm_value(rtc, tm->tm_wday, RWKAR);
523 sh_rtc_write_alarm_value(rtc, tm->tm_mday, RDAYAR);
527 sh_rtc_write_alarm_value(rtc, mon, RMONAR);
529 if (wkalrm->enabled) {
531 writeb(rcr1, rtc->regbase + RCR1);
534 spin_unlock_irq(&rtc->lock);
539 static int sh_rtc_irq_set_state(struct device *dev, int enabled)
541 struct platform_device *pdev = to_platform_device(dev);
542 struct sh_rtc *rtc = platform_get_drvdata(pdev);
545 rtc->periodic_freq |= PF_KOU;
546 return sh_rtc_ioctl(dev, RTC_PIE_ON, 0);
548 rtc->periodic_freq &= ~PF_KOU;
549 return sh_rtc_ioctl(dev, RTC_PIE_OFF, 0);
553 static int sh_rtc_irq_set_freq(struct device *dev, int freq)
555 if (!is_power_of_2(freq))
557 return sh_rtc_ioctl(dev, RTC_IRQP_SET, freq);
560 static struct rtc_class_ops sh_rtc_ops = {
561 .ioctl = sh_rtc_ioctl,
562 .read_time = sh_rtc_read_time,
563 .set_time = sh_rtc_set_time,
564 .read_alarm = sh_rtc_read_alarm,
565 .set_alarm = sh_rtc_set_alarm,
566 .irq_set_state = sh_rtc_irq_set_state,
567 .irq_set_freq = sh_rtc_irq_set_freq,
571 static int __devinit sh_rtc_probe(struct platform_device *pdev)
574 struct resource *res;
578 rtc = kzalloc(sizeof(struct sh_rtc), GFP_KERNEL);
582 spin_lock_init(&rtc->lock);
584 /* get periodic/carry/alarm irqs */
585 ret = platform_get_irq(pdev, 0);
586 if (unlikely(ret <= 0)) {
588 dev_err(&pdev->dev, "No IRQ for period\n");
591 rtc->periodic_irq = ret;
593 ret = platform_get_irq(pdev, 1);
594 if (unlikely(ret <= 0)) {
596 dev_err(&pdev->dev, "No IRQ for carry\n");
599 rtc->carry_irq = ret;
601 ret = platform_get_irq(pdev, 2);
602 if (unlikely(ret <= 0)) {
604 dev_err(&pdev->dev, "No IRQ for alarm\n");
607 rtc->alarm_irq = ret;
609 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
610 if (unlikely(res == NULL)) {
612 dev_err(&pdev->dev, "No IO resource\n");
616 rtc->regsize = res->end - res->start + 1;
618 rtc->res = request_mem_region(res->start, rtc->regsize, pdev->name);
619 if (unlikely(!rtc->res)) {
624 rtc->regbase = ioremap_nocache(rtc->res->start, rtc->regsize);
625 if (unlikely(!rtc->regbase)) {
630 rtc->rtc_dev = rtc_device_register("sh", &pdev->dev,
631 &sh_rtc_ops, THIS_MODULE);
632 if (IS_ERR(rtc->rtc_dev)) {
633 ret = PTR_ERR(rtc->rtc_dev);
637 rtc->capabilities = RTC_DEF_CAPABILITIES;
638 if (pdev->dev.platform_data) {
639 struct sh_rtc_platform_info *pinfo = pdev->dev.platform_data;
642 * Some CPUs have special capabilities in addition to the
643 * default set. Add those in here.
645 rtc->capabilities |= pinfo->capabilities;
648 rtc->rtc_dev->max_user_freq = 256;
649 rtc->rtc_dev->irq_freq = 1;
650 rtc->periodic_freq = 0x60;
652 platform_set_drvdata(pdev, rtc);
654 /* register periodic/carry/alarm irqs */
655 ret = request_irq(rtc->periodic_irq, sh_rtc_periodic, IRQF_DISABLED,
656 "sh-rtc period", rtc);
659 "request period IRQ failed with %d, IRQ %d\n", ret,
664 ret = request_irq(rtc->carry_irq, sh_rtc_interrupt, IRQF_DISABLED,
665 "sh-rtc carry", rtc);
668 "request carry IRQ failed with %d, IRQ %d\n", ret,
670 free_irq(rtc->periodic_irq, rtc);
674 ret = request_irq(rtc->alarm_irq, sh_rtc_alarm, IRQF_DISABLED,
675 "sh-rtc alarm", rtc);
678 "request alarm IRQ failed with %d, IRQ %d\n", ret,
680 free_irq(rtc->carry_irq, rtc);
681 free_irq(rtc->periodic_irq, rtc);
685 tmp = readb(rtc->regbase + RCR1);
688 writeb(tmp, rtc->regbase + RCR1);
693 iounmap(rtc->regbase);
695 release_resource(rtc->res);
702 static int __devexit sh_rtc_remove(struct platform_device *pdev)
704 struct sh_rtc *rtc = platform_get_drvdata(pdev);
706 if (likely(rtc->rtc_dev))
707 rtc_device_unregister(rtc->rtc_dev);
709 sh_rtc_setpie(&pdev->dev, 0);
710 sh_rtc_setaie(&pdev->dev, 0);
712 free_irq(rtc->carry_irq, rtc);
713 free_irq(rtc->periodic_irq, rtc);
714 free_irq(rtc->alarm_irq, rtc);
716 release_resource(rtc->res);
718 iounmap(rtc->regbase);
720 platform_set_drvdata(pdev, NULL);
726 static struct platform_driver sh_rtc_platform_driver = {
729 .owner = THIS_MODULE,
731 .probe = sh_rtc_probe,
732 .remove = __devexit_p(sh_rtc_remove),
735 static int __init sh_rtc_init(void)
737 return platform_driver_register(&sh_rtc_platform_driver);
740 static void __exit sh_rtc_exit(void)
742 platform_driver_unregister(&sh_rtc_platform_driver);
745 module_init(sh_rtc_init);
746 module_exit(sh_rtc_exit);
748 MODULE_DESCRIPTION("SuperH on-chip RTC driver");
749 MODULE_VERSION(DRV_VERSION);
750 MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, "
751 "Jamie Lenehan <lenehan@twibble.org>, "
752 "Angelo Castello <angelo.castello@st.com>");
753 MODULE_LICENSE("GPL");
754 MODULE_ALIAS("platform:" DRV_NAME);