2 * SuperH On-Chip RTC Support
4 * Copyright (C) 2006, 2007 Paul Mundt
5 * Copyright (C) 2006 Jamie Lenehan
7 * Based on the old arch/sh/kernel/cpu/rtc.c by:
9 * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
10 * Copyright (C) 1999 Tetsuya Okada & Niibe Yutaka
12 * This file is subject to the terms and conditions of the GNU General Public
13 * License. See the file "COPYING" in the main directory of this archive
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/bcd.h>
19 #include <linux/rtc.h>
20 #include <linux/init.h>
21 #include <linux/platform_device.h>
22 #include <linux/seq_file.h>
23 #include <linux/interrupt.h>
24 #include <linux/spinlock.h>
28 #define DRV_NAME "sh-rtc"
29 #define DRV_VERSION "0.1.5"
31 #ifdef CONFIG_CPU_SH2A
32 #define rtc_reg_size sizeof(u16)
33 #define RTC_BIT_INVERTED 0
34 #define RTC_DEF_CAPABILITIES RTC_CAP_4_DIGIT_YEAR
35 #elif defined(CONFIG_CPU_SH3)
36 #define rtc_reg_size sizeof(u16)
37 #define RTC_BIT_INVERTED 0 /* No bug on SH7708, SH7709A */
38 #define RTC_DEF_CAPABILITIES 0UL
39 #elif defined(CONFIG_CPU_SH4)
40 #define rtc_reg_size sizeof(u32)
41 #define RTC_BIT_INVERTED 0x40 /* bug on SH7750, SH7750S */
42 #define RTC_DEF_CAPABILITIES RTC_CAP_4_DIGIT_YEAR
43 #elif defined(CONFIG_CPU_SH5)
44 #define rtc_reg_size sizeof(u32)
45 #define RTC_BIT_INVERTED 0 /* The SH-5 RTC is surprisingly sane! */
46 #define RTC_DEF_CAPABILITIES RTC_CAP_4_DIGIT_YEAR
49 #define RTC_REG(r) ((r) * rtc_reg_size)
51 #define R64CNT RTC_REG(0)
53 #define RSECCNT RTC_REG(1) /* RTC sec */
54 #define RMINCNT RTC_REG(2) /* RTC min */
55 #define RHRCNT RTC_REG(3) /* RTC hour */
56 #define RWKCNT RTC_REG(4) /* RTC week */
57 #define RDAYCNT RTC_REG(5) /* RTC day */
58 #define RMONCNT RTC_REG(6) /* RTC month */
59 #define RYRCNT RTC_REG(7) /* RTC year */
60 #define RSECAR RTC_REG(8) /* ALARM sec */
61 #define RMINAR RTC_REG(9) /* ALARM min */
62 #define RHRAR RTC_REG(10) /* ALARM hour */
63 #define RWKAR RTC_REG(11) /* ALARM week */
64 #define RDAYAR RTC_REG(12) /* ALARM day */
65 #define RMONAR RTC_REG(13) /* ALARM month */
66 #define RCR1 RTC_REG(14) /* Control */
67 #define RCR2 RTC_REG(15) /* Control */
70 * Note on RYRAR and RCR3: Up until this point most of the register
71 * definitions are consistent across all of the available parts. However,
72 * the placement of the optional RYRAR and RCR3 (the RYRAR control
73 * register used to control RYRCNT/RYRAR compare) varies considerably
74 * across various parts, occasionally being mapped in to a completely
75 * unrelated address space. For proper RYRAR support a separate resource
76 * would have to be handed off, but as this is purely optional in
77 * practice, we simply opt not to support it, thereby keeping the code
78 * quite a bit more simplified.
81 /* ALARM Bits - or with BCD encoded value */
82 #define AR_ENB 0x80 /* Enable for alarm cmp */
85 #define RCR1_CF 0x80 /* Carry Flag */
86 #define RCR1_CIE 0x10 /* Carry Interrupt Enable */
87 #define RCR1_AIE 0x08 /* Alarm Interrupt Enable */
88 #define RCR1_AF 0x01 /* Alarm Flag */
91 #define RCR2_PEF 0x80 /* PEriodic interrupt Flag */
92 #define RCR2_PESMASK 0x70 /* Periodic interrupt Set */
93 #define RCR2_RTCEN 0x08 /* ENable RTC */
94 #define RCR2_ADJ 0x04 /* ADJustment (30-second) */
95 #define RCR2_RESET 0x02 /* Reset bit */
96 #define RCR2_START 0x01 /* Start bit */
99 void __iomem *regbase;
100 unsigned long regsize;
101 struct resource *res;
102 unsigned int alarm_irq, periodic_irq, carry_irq;
103 struct rtc_device *rtc_dev;
106 unsigned long capabilities; /* See asm-sh/rtc.h for cap bits */
109 static irqreturn_t sh_rtc_interrupt(int irq, void *dev_id)
111 struct platform_device *pdev = to_platform_device(dev_id);
112 struct sh_rtc *rtc = platform_get_drvdata(pdev);
113 unsigned int tmp, events = 0;
115 spin_lock(&rtc->lock);
117 tmp = readb(rtc->regbase + RCR1);
120 if (rtc->rearm_aie) {
122 tmp &= ~RCR1_AF; /* try to clear AF again */
124 tmp |= RCR1_AIE; /* AF has cleared, rearm IRQ */
129 writeb(tmp, rtc->regbase + RCR1);
131 rtc_update_irq(rtc->rtc_dev, 1, events);
133 spin_unlock(&rtc->lock);
138 static irqreturn_t sh_rtc_alarm(int irq, void *dev_id)
140 struct platform_device *pdev = to_platform_device(dev_id);
141 struct sh_rtc *rtc = platform_get_drvdata(pdev);
142 unsigned int tmp, events = 0;
144 spin_lock(&rtc->lock);
146 tmp = readb(rtc->regbase + RCR1);
149 * If AF is set then the alarm has triggered. If we clear AF while
150 * the alarm time still matches the RTC time then AF will
151 * immediately be set again, and if AIE is enabled then the alarm
152 * interrupt will immediately be retrigger. So we clear AIE here
153 * and use rtc->rearm_aie so that the carry interrupt will keep
154 * trying to clear AF and once it stays cleared it'll re-enable
158 events |= RTC_AF | RTC_IRQF;
160 tmp &= ~(RCR1_AF|RCR1_AIE);
162 writeb(tmp, rtc->regbase + RCR1);
166 rtc_update_irq(rtc->rtc_dev, 1, events);
169 spin_unlock(&rtc->lock);
173 static irqreturn_t sh_rtc_periodic(int irq, void *dev_id)
175 struct platform_device *pdev = to_platform_device(dev_id);
176 struct sh_rtc *rtc = platform_get_drvdata(pdev);
178 spin_lock(&rtc->lock);
180 rtc_update_irq(rtc->rtc_dev, 1, RTC_PF | RTC_IRQF);
182 spin_unlock(&rtc->lock);
187 static inline void sh_rtc_setpie(struct device *dev, unsigned int enable)
189 struct sh_rtc *rtc = dev_get_drvdata(dev);
192 spin_lock_irq(&rtc->lock);
194 tmp = readb(rtc->regbase + RCR2);
197 tmp &= ~RCR2_PESMASK;
198 tmp |= RCR2_PEF | (2 << 4);
200 tmp &= ~(RCR2_PESMASK | RCR2_PEF);
202 writeb(tmp, rtc->regbase + RCR2);
204 spin_unlock_irq(&rtc->lock);
207 static inline void sh_rtc_setaie(struct device *dev, unsigned int enable)
209 struct sh_rtc *rtc = dev_get_drvdata(dev);
212 spin_lock_irq(&rtc->lock);
214 tmp = readb(rtc->regbase + RCR1);
219 } else if (rtc->rearm_aie == 0)
222 writeb(tmp, rtc->regbase + RCR1);
224 spin_unlock_irq(&rtc->lock);
227 static int sh_rtc_open(struct device *dev)
229 struct sh_rtc *rtc = dev_get_drvdata(dev);
233 tmp = readb(rtc->regbase + RCR1);
236 writeb(tmp, rtc->regbase + RCR1);
238 ret = request_irq(rtc->periodic_irq, sh_rtc_periodic, IRQF_DISABLED,
239 "sh-rtc period", dev);
241 dev_err(dev, "request period IRQ failed with %d, IRQ %d\n",
242 ret, rtc->periodic_irq);
246 ret = request_irq(rtc->carry_irq, sh_rtc_interrupt, IRQF_DISABLED,
247 "sh-rtc carry", dev);
249 dev_err(dev, "request carry IRQ failed with %d, IRQ %d\n",
250 ret, rtc->carry_irq);
251 free_irq(rtc->periodic_irq, dev);
255 ret = request_irq(rtc->alarm_irq, sh_rtc_alarm, IRQF_DISABLED,
256 "sh-rtc alarm", dev);
258 dev_err(dev, "request alarm IRQ failed with %d, IRQ %d\n",
259 ret, rtc->alarm_irq);
266 free_irq(rtc->carry_irq, dev);
268 free_irq(rtc->periodic_irq, dev);
273 static void sh_rtc_release(struct device *dev)
275 struct sh_rtc *rtc = dev_get_drvdata(dev);
277 sh_rtc_setpie(dev, 0);
278 sh_rtc_setaie(dev, 0);
280 free_irq(rtc->periodic_irq, dev);
281 free_irq(rtc->carry_irq, dev);
282 free_irq(rtc->alarm_irq, dev);
285 static int sh_rtc_proc(struct device *dev, struct seq_file *seq)
287 struct sh_rtc *rtc = dev_get_drvdata(dev);
290 tmp = readb(rtc->regbase + RCR1);
291 seq_printf(seq, "carry_IRQ\t: %s\n",
292 (tmp & RCR1_CIE) ? "yes" : "no");
294 tmp = readb(rtc->regbase + RCR2);
295 seq_printf(seq, "periodic_IRQ\t: %s\n",
296 (tmp & RCR2_PEF) ? "yes" : "no");
301 static int sh_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
303 unsigned int ret = -ENOIOCTLCMD;
308 sh_rtc_setpie(dev, cmd == RTC_PIE_ON);
313 sh_rtc_setaie(dev, cmd == RTC_AIE_ON);
321 static int sh_rtc_read_time(struct device *dev, struct rtc_time *tm)
323 struct platform_device *pdev = to_platform_device(dev);
324 struct sh_rtc *rtc = platform_get_drvdata(pdev);
325 unsigned int sec128, sec2, yr, yr100, cf_bit;
330 spin_lock_irq(&rtc->lock);
332 tmp = readb(rtc->regbase + RCR1);
333 tmp &= ~RCR1_CF; /* Clear CF-bit */
335 writeb(tmp, rtc->regbase + RCR1);
337 sec128 = readb(rtc->regbase + R64CNT);
339 tm->tm_sec = BCD2BIN(readb(rtc->regbase + RSECCNT));
340 tm->tm_min = BCD2BIN(readb(rtc->regbase + RMINCNT));
341 tm->tm_hour = BCD2BIN(readb(rtc->regbase + RHRCNT));
342 tm->tm_wday = BCD2BIN(readb(rtc->regbase + RWKCNT));
343 tm->tm_mday = BCD2BIN(readb(rtc->regbase + RDAYCNT));
344 tm->tm_mon = BCD2BIN(readb(rtc->regbase + RMONCNT)) - 1;
346 if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) {
347 yr = readw(rtc->regbase + RYRCNT);
348 yr100 = BCD2BIN(yr >> 8);
351 yr = readb(rtc->regbase + RYRCNT);
352 yr100 = BCD2BIN((yr == 0x99) ? 0x19 : 0x20);
355 tm->tm_year = (yr100 * 100 + BCD2BIN(yr)) - 1900;
357 sec2 = readb(rtc->regbase + R64CNT);
358 cf_bit = readb(rtc->regbase + RCR1) & RCR1_CF;
360 spin_unlock_irq(&rtc->lock);
361 } while (cf_bit != 0 || ((sec128 ^ sec2) & RTC_BIT_INVERTED) != 0);
363 #if RTC_BIT_INVERTED != 0
364 if ((sec128 & RTC_BIT_INVERTED))
368 dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
369 "mday=%d, mon=%d, year=%d, wday=%d\n",
371 tm->tm_sec, tm->tm_min, tm->tm_hour,
372 tm->tm_mday, tm->tm_mon + 1, tm->tm_year, tm->tm_wday);
374 if (rtc_valid_tm(tm) < 0) {
375 dev_err(dev, "invalid date\n");
376 rtc_time_to_tm(0, tm);
382 static int sh_rtc_set_time(struct device *dev, struct rtc_time *tm)
384 struct platform_device *pdev = to_platform_device(dev);
385 struct sh_rtc *rtc = platform_get_drvdata(pdev);
389 spin_lock_irq(&rtc->lock);
391 /* Reset pre-scaler & stop RTC */
392 tmp = readb(rtc->regbase + RCR2);
395 writeb(tmp, rtc->regbase + RCR2);
397 writeb(BIN2BCD(tm->tm_sec), rtc->regbase + RSECCNT);
398 writeb(BIN2BCD(tm->tm_min), rtc->regbase + RMINCNT);
399 writeb(BIN2BCD(tm->tm_hour), rtc->regbase + RHRCNT);
400 writeb(BIN2BCD(tm->tm_wday), rtc->regbase + RWKCNT);
401 writeb(BIN2BCD(tm->tm_mday), rtc->regbase + RDAYCNT);
402 writeb(BIN2BCD(tm->tm_mon + 1), rtc->regbase + RMONCNT);
404 if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) {
405 year = (BIN2BCD((tm->tm_year + 1900) / 100) << 8) |
406 BIN2BCD(tm->tm_year % 100);
407 writew(year, rtc->regbase + RYRCNT);
409 year = tm->tm_year % 100;
410 writeb(BIN2BCD(year), rtc->regbase + RYRCNT);
414 tmp = readb(rtc->regbase + RCR2);
416 tmp |= RCR2_RTCEN | RCR2_START;
417 writeb(tmp, rtc->regbase + RCR2);
419 spin_unlock_irq(&rtc->lock);
424 static inline int sh_rtc_read_alarm_value(struct sh_rtc *rtc, int reg_off)
427 int value = 0xff; /* return 0xff for ignored values */
429 byte = readb(rtc->regbase + reg_off);
431 byte &= ~AR_ENB; /* strip the enable bit */
432 value = BCD2BIN(byte);
438 static int sh_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
440 struct platform_device *pdev = to_platform_device(dev);
441 struct sh_rtc *rtc = platform_get_drvdata(pdev);
442 struct rtc_time* tm = &wkalrm->time;
444 spin_lock_irq(&rtc->lock);
446 tm->tm_sec = sh_rtc_read_alarm_value(rtc, RSECAR);
447 tm->tm_min = sh_rtc_read_alarm_value(rtc, RMINAR);
448 tm->tm_hour = sh_rtc_read_alarm_value(rtc, RHRAR);
449 tm->tm_wday = sh_rtc_read_alarm_value(rtc, RWKAR);
450 tm->tm_mday = sh_rtc_read_alarm_value(rtc, RDAYAR);
451 tm->tm_mon = sh_rtc_read_alarm_value(rtc, RMONAR);
453 tm->tm_mon -= 1; /* RTC is 1-12, tm_mon is 0-11 */
454 tm->tm_year = 0xffff;
456 wkalrm->enabled = (readb(rtc->regbase + RCR1) & RCR1_AIE) ? 1 : 0;
458 spin_unlock_irq(&rtc->lock);
463 static inline void sh_rtc_write_alarm_value(struct sh_rtc *rtc,
464 int value, int reg_off)
466 /* < 0 for a value that is ignored */
468 writeb(0, rtc->regbase + reg_off);
470 writeb(BIN2BCD(value) | AR_ENB, rtc->regbase + reg_off);
473 static int sh_rtc_check_alarm(struct rtc_time* tm)
476 * The original rtc says anything > 0xc0 is "don't care" or "match
477 * all" - most users use 0xff but rtc-dev uses -1 for the same thing.
478 * The original rtc doesn't support years - some things use -1 and
479 * some 0xffff. We use -1 to make out tests easier.
481 if (tm->tm_year == 0xffff)
483 if (tm->tm_mon >= 0xff)
485 if (tm->tm_mday >= 0xff)
487 if (tm->tm_wday >= 0xff)
489 if (tm->tm_hour >= 0xff)
491 if (tm->tm_min >= 0xff)
493 if (tm->tm_sec >= 0xff)
496 if (tm->tm_year > 9999 ||
498 tm->tm_mday == 0 || tm->tm_mday >= 32 ||
508 static int sh_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
510 struct platform_device *pdev = to_platform_device(dev);
511 struct sh_rtc *rtc = platform_get_drvdata(pdev);
513 struct rtc_time *tm = &wkalrm->time;
516 err = sh_rtc_check_alarm(tm);
517 if (unlikely(err < 0))
520 spin_lock_irq(&rtc->lock);
522 /* disable alarm interrupt and clear the alarm flag */
523 rcr1 = readb(rtc->regbase + RCR1);
524 rcr1 &= ~(RCR1_AF|RCR1_AIE);
525 writeb(rcr1, rtc->regbase + RCR1);
530 sh_rtc_write_alarm_value(rtc, tm->tm_sec, RSECAR);
531 sh_rtc_write_alarm_value(rtc, tm->tm_min, RMINAR);
532 sh_rtc_write_alarm_value(rtc, tm->tm_hour, RHRAR);
533 sh_rtc_write_alarm_value(rtc, tm->tm_wday, RWKAR);
534 sh_rtc_write_alarm_value(rtc, tm->tm_mday, RDAYAR);
538 sh_rtc_write_alarm_value(rtc, mon, RMONAR);
540 if (wkalrm->enabled) {
542 writeb(rcr1, rtc->regbase + RCR1);
545 spin_unlock_irq(&rtc->lock);
550 static struct rtc_class_ops sh_rtc_ops = {
552 .release = sh_rtc_release,
553 .ioctl = sh_rtc_ioctl,
554 .read_time = sh_rtc_read_time,
555 .set_time = sh_rtc_set_time,
556 .read_alarm = sh_rtc_read_alarm,
557 .set_alarm = sh_rtc_set_alarm,
561 static int __devinit sh_rtc_probe(struct platform_device *pdev)
564 struct resource *res;
567 rtc = kzalloc(sizeof(struct sh_rtc), GFP_KERNEL);
571 spin_lock_init(&rtc->lock);
573 rtc->periodic_irq = platform_get_irq(pdev, 0);
574 if (unlikely(rtc->periodic_irq < 0)) {
575 dev_err(&pdev->dev, "No IRQ for period\n");
579 rtc->carry_irq = platform_get_irq(pdev, 1);
580 if (unlikely(rtc->carry_irq < 0)) {
581 dev_err(&pdev->dev, "No IRQ for carry\n");
585 rtc->alarm_irq = platform_get_irq(pdev, 2);
586 if (unlikely(rtc->alarm_irq < 0)) {
587 dev_err(&pdev->dev, "No IRQ for alarm\n");
591 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
592 if (unlikely(res == NULL)) {
593 dev_err(&pdev->dev, "No IO resource\n");
597 rtc->regsize = res->end - res->start + 1;
599 rtc->res = request_mem_region(res->start, rtc->regsize, pdev->name);
600 if (unlikely(!rtc->res)) {
605 rtc->regbase = (void __iomem *)rtc->res->start;
606 if (unlikely(!rtc->regbase)) {
611 rtc->rtc_dev = rtc_device_register("sh", &pdev->dev,
612 &sh_rtc_ops, THIS_MODULE);
613 if (IS_ERR(rtc->rtc_dev)) {
614 ret = PTR_ERR(rtc->rtc_dev);
618 rtc->capabilities = RTC_DEF_CAPABILITIES;
619 if (pdev->dev.platform_data) {
620 struct sh_rtc_platform_info *pinfo = pdev->dev.platform_data;
623 * Some CPUs have special capabilities in addition to the
624 * default set. Add those in here.
626 rtc->capabilities |= pinfo->capabilities;
629 platform_set_drvdata(pdev, rtc);
634 release_resource(rtc->res);
641 static int __devexit sh_rtc_remove(struct platform_device *pdev)
643 struct sh_rtc *rtc = platform_get_drvdata(pdev);
645 if (likely(rtc->rtc_dev))
646 rtc_device_unregister(rtc->rtc_dev);
648 sh_rtc_setpie(&pdev->dev, 0);
649 sh_rtc_setaie(&pdev->dev, 0);
651 release_resource(rtc->res);
653 platform_set_drvdata(pdev, NULL);
659 static struct platform_driver sh_rtc_platform_driver = {
662 .owner = THIS_MODULE,
664 .probe = sh_rtc_probe,
665 .remove = __devexit_p(sh_rtc_remove),
668 static int __init sh_rtc_init(void)
670 return platform_driver_register(&sh_rtc_platform_driver);
673 static void __exit sh_rtc_exit(void)
675 platform_driver_unregister(&sh_rtc_platform_driver);
678 module_init(sh_rtc_init);
679 module_exit(sh_rtc_exit);
681 MODULE_DESCRIPTION("SuperH on-chip RTC driver");
682 MODULE_VERSION(DRV_VERSION);
683 MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, Jamie Lenehan <lenehan@twibble.org>");
684 MODULE_LICENSE("GPL");