2 * SuperH On-Chip RTC Support
4 * Copyright (C) 2006 Paul Mundt
6 * Based on the old arch/sh/kernel/cpu/rtc.c by:
8 * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
9 * Copyright (C) 1999 Tetsuya Okada & Niibe Yutaka
11 * This file is subject to the terms and conditions of the GNU General Public
12 * License. See the file "COPYING" in the main directory of this archive
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/bcd.h>
18 #include <linux/rtc.h>
19 #include <linux/init.h>
20 #include <linux/platform_device.h>
21 #include <linux/seq_file.h>
22 #include <linux/interrupt.h>
23 #include <linux/spinlock.h>
27 #define rtc_reg_size sizeof(u16)
28 #define RTC_BIT_INVERTED 0 /* No bug on SH7708, SH7709A */
29 #elif defined(CONFIG_CPU_SH4)
30 #define rtc_reg_size sizeof(u32)
31 #define RTC_BIT_INVERTED 0x40 /* bug on SH7750, SH7750S */
34 #define RTC_REG(r) ((r) * rtc_reg_size)
36 #define R64CNT RTC_REG(0)
37 #define RSECCNT RTC_REG(1)
38 #define RMINCNT RTC_REG(2)
39 #define RHRCNT RTC_REG(3)
40 #define RWKCNT RTC_REG(4)
41 #define RDAYCNT RTC_REG(5)
42 #define RMONCNT RTC_REG(6)
43 #define RYRCNT RTC_REG(7)
44 #define RSECAR RTC_REG(8)
45 #define RMINAR RTC_REG(9)
46 #define RHRAR RTC_REG(10)
47 #define RWKAR RTC_REG(11)
48 #define RDAYAR RTC_REG(12)
49 #define RMONAR RTC_REG(13)
50 #define RCR1 RTC_REG(14)
51 #define RCR2 RTC_REG(15)
54 #define RCR1_CF 0x80 /* Carry Flag */
55 #define RCR1_CIE 0x10 /* Carry Interrupt Enable */
56 #define RCR1_AIE 0x08 /* Alarm Interrupt Enable */
57 #define RCR1_AF 0x01 /* Alarm Flag */
60 #define RCR2_PEF 0x80 /* PEriodic interrupt Flag */
61 #define RCR2_PESMASK 0x70 /* Periodic interrupt Set */
62 #define RCR2_RTCEN 0x08 /* ENable RTC */
63 #define RCR2_ADJ 0x04 /* ADJustment (30-second) */
64 #define RCR2_RESET 0x02 /* Reset bit */
65 #define RCR2_START 0x01 /* Start bit */
68 void __iomem *regbase;
69 unsigned long regsize;
71 unsigned int alarm_irq, periodic_irq, carry_irq;
72 struct rtc_device *rtc_dev;
76 static irqreturn_t sh_rtc_interrupt(int irq, void *id, struct pt_regs *regs)
78 struct platform_device *pdev = id;
79 struct sh_rtc *rtc = platform_get_drvdata(pdev);
80 unsigned int tmp, events = 0;
82 spin_lock(&rtc->lock);
84 tmp = readb(rtc->regbase + RCR1);
87 events |= RTC_AF | RTC_IRQF;
89 tmp &= ~(RCR1_CF | RCR1_AF);
91 writeb(tmp, rtc->regbase + RCR1);
93 rtc_update_irq(&rtc->rtc_dev->class_dev, 1, events);
95 spin_unlock(&rtc->lock);
100 static irqreturn_t sh_rtc_periodic(int irq, void *id, struct pt_regs *regs)
102 struct sh_rtc *rtc = dev_get_drvdata(id);
104 spin_lock(&rtc->lock);
106 rtc_update_irq(&rtc->rtc_dev->class_dev, 1, RTC_PF | RTC_IRQF);
108 spin_unlock(&rtc->lock);
113 static inline void sh_rtc_setpie(struct device *dev, unsigned int enable)
115 struct sh_rtc *rtc = dev_get_drvdata(dev);
118 spin_lock_irq(&rtc->lock);
120 tmp = readb(rtc->regbase + RCR2);
123 tmp &= ~RCR2_PESMASK;
124 tmp |= RCR2_PEF | (2 << 4);
126 tmp &= ~(RCR2_PESMASK | RCR2_PEF);
128 writeb(tmp, rtc->regbase + RCR2);
130 spin_unlock_irq(&rtc->lock);
133 static inline void sh_rtc_setaie(struct device *dev, unsigned int enable)
135 struct sh_rtc *rtc = dev_get_drvdata(dev);
138 spin_lock_irq(&rtc->lock);
140 tmp = readb(rtc->regbase + RCR1);
147 writeb(tmp, rtc->regbase + RCR1);
149 spin_unlock_irq(&rtc->lock);
152 static int sh_rtc_open(struct device *dev)
154 struct sh_rtc *rtc = dev_get_drvdata(dev);
158 tmp = readb(rtc->regbase + RCR1);
161 writeb(tmp, rtc->regbase + RCR1);
163 ret = request_irq(rtc->periodic_irq, sh_rtc_periodic, SA_INTERRUPT,
164 "sh-rtc period", dev);
166 dev_err(dev, "request period IRQ failed with %d, IRQ %d\n",
167 ret, rtc->periodic_irq);
171 ret = request_irq(rtc->carry_irq, sh_rtc_interrupt, SA_INTERRUPT,
172 "sh-rtc carry", dev);
174 dev_err(dev, "request carry IRQ failed with %d, IRQ %d\n",
175 ret, rtc->carry_irq);
176 free_irq(rtc->periodic_irq, dev);
180 ret = request_irq(rtc->alarm_irq, sh_rtc_interrupt, SA_INTERRUPT,
181 "sh-rtc alarm", dev);
183 dev_err(dev, "request alarm IRQ failed with %d, IRQ %d\n",
184 ret, rtc->alarm_irq);
191 free_irq(rtc->carry_irq, dev);
193 free_irq(rtc->periodic_irq, dev);
198 static void sh_rtc_release(struct device *dev)
200 struct sh_rtc *rtc = dev_get_drvdata(dev);
202 sh_rtc_setpie(dev, 0);
204 free_irq(rtc->periodic_irq, dev);
205 free_irq(rtc->carry_irq, dev);
206 free_irq(rtc->alarm_irq, dev);
209 static int sh_rtc_proc(struct device *dev, struct seq_file *seq)
211 struct sh_rtc *rtc = dev_get_drvdata(dev);
214 tmp = readb(rtc->regbase + RCR1);
215 seq_printf(seq, "alarm_IRQ\t: %s\n",
216 (tmp & RCR1_AIE) ? "yes" : "no");
217 seq_printf(seq, "carry_IRQ\t: %s\n",
218 (tmp & RCR1_CIE) ? "yes" : "no");
220 tmp = readb(rtc->regbase + RCR2);
221 seq_printf(seq, "periodic_IRQ\t: %s\n",
222 (tmp & RCR2_PEF) ? "yes" : "no");
227 static int sh_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
229 unsigned int ret = -ENOIOCTLCMD;
234 sh_rtc_setpie(dev, cmd == RTC_PIE_ON);
239 sh_rtc_setaie(dev, cmd == RTC_AIE_ON);
247 static int sh_rtc_read_time(struct device *dev, struct rtc_time *tm)
249 struct platform_device *pdev = to_platform_device(dev);
250 struct sh_rtc *rtc = platform_get_drvdata(pdev);
251 unsigned int sec128, sec2, yr, yr100, cf_bit;
256 spin_lock_irq(&rtc->lock);
258 tmp = readb(rtc->regbase + RCR1);
259 tmp &= ~RCR1_CF; /* Clear CF-bit */
261 writeb(tmp, rtc->regbase + RCR1);
263 sec128 = readb(rtc->regbase + R64CNT);
265 tm->tm_sec = BCD2BIN(readb(rtc->regbase + RSECCNT));
266 tm->tm_min = BCD2BIN(readb(rtc->regbase + RMINCNT));
267 tm->tm_hour = BCD2BIN(readb(rtc->regbase + RHRCNT));
268 tm->tm_wday = BCD2BIN(readb(rtc->regbase + RWKCNT));
269 tm->tm_mday = BCD2BIN(readb(rtc->regbase + RDAYCNT));
270 tm->tm_mon = BCD2BIN(readb(rtc->regbase + RMONCNT));
272 #if defined(CONFIG_CPU_SH4)
273 yr = readw(rtc->regbase + RYRCNT);
274 yr100 = BCD2BIN(yr >> 8);
277 yr = readb(rtc->regbase + RYRCNT);
278 yr100 = BCD2BIN((yr == 0x99) ? 0x19 : 0x20);
281 tm->tm_year = (yr100 * 100 + BCD2BIN(yr)) - 1900;
283 sec2 = readb(rtc->regbase + R64CNT);
284 cf_bit = readb(rtc->regbase + RCR1) & RCR1_CF;
286 spin_unlock_irq(&rtc->lock);
287 } while (cf_bit != 0 || ((sec128 ^ sec2) & RTC_BIT_INVERTED) != 0);
289 #if RTC_BIT_INVERTED != 0
290 if ((sec128 & RTC_BIT_INVERTED))
294 dev_dbg(&dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
295 "mday=%d, mon=%d, year=%d, wday=%d\n",
297 tm->tm_sec, tm->tm_min, tm->tm_hour,
298 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
300 if (rtc_valid_tm(tm) < 0)
301 dev_err(dev, "invalid date\n");
306 static int sh_rtc_set_time(struct device *dev, struct rtc_time *tm)
308 struct platform_device *pdev = to_platform_device(dev);
309 struct sh_rtc *rtc = platform_get_drvdata(pdev);
313 spin_lock_irq(&rtc->lock);
315 /* Reset pre-scaler & stop RTC */
316 tmp = readb(rtc->regbase + RCR2);
318 writeb(tmp, rtc->regbase + RCR2);
320 writeb(BIN2BCD(tm->tm_sec), rtc->regbase + RSECCNT);
321 writeb(BIN2BCD(tm->tm_min), rtc->regbase + RMINCNT);
322 writeb(BIN2BCD(tm->tm_hour), rtc->regbase + RHRCNT);
323 writeb(BIN2BCD(tm->tm_wday), rtc->regbase + RWKCNT);
324 writeb(BIN2BCD(tm->tm_mday), rtc->regbase + RDAYCNT);
325 writeb(BIN2BCD(tm->tm_mon), rtc->regbase + RMONCNT);
327 #ifdef CONFIG_CPU_SH3
328 year = tm->tm_year % 100;
329 writeb(BIN2BCD(year), rtc->regbase + RYRCNT);
331 year = (BIN2BCD((tm->tm_year + 1900) / 100) << 8) |
332 BIN2BCD(tm->tm_year % 100);
333 writew(year, rtc->regbase + RYRCNT);
337 tmp = readb(rtc->regbase + RCR2);
339 tmp |= RCR2_RTCEN | RCR2_START;
340 writeb(tmp, rtc->regbase + RCR2);
342 spin_unlock_irq(&rtc->lock);
347 static struct rtc_class_ops sh_rtc_ops = {
349 .release = sh_rtc_release,
350 .ioctl = sh_rtc_ioctl,
351 .read_time = sh_rtc_read_time,
352 .set_time = sh_rtc_set_time,
356 static int __devinit sh_rtc_probe(struct platform_device *pdev)
359 struct resource *res;
362 rtc = kzalloc(sizeof(struct sh_rtc), GFP_KERNEL);
366 spin_lock_init(&rtc->lock);
368 rtc->periodic_irq = platform_get_irq(pdev, 0);
369 if (unlikely(rtc->periodic_irq < 0)) {
370 dev_err(&pdev->dev, "No IRQ for period\n");
374 rtc->carry_irq = platform_get_irq(pdev, 1);
375 if (unlikely(rtc->carry_irq < 0)) {
376 dev_err(&pdev->dev, "No IRQ for carry\n");
380 rtc->alarm_irq = platform_get_irq(pdev, 2);
381 if (unlikely(rtc->alarm_irq < 0)) {
382 dev_err(&pdev->dev, "No IRQ for alarm\n");
386 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
387 if (unlikely(res == NULL)) {
388 dev_err(&pdev->dev, "No IO resource\n");
392 rtc->regsize = res->end - res->start + 1;
394 rtc->res = request_mem_region(res->start, rtc->regsize, pdev->name);
395 if (unlikely(!rtc->res)) {
400 rtc->regbase = (void __iomem *)rtc->res->start;
401 if (unlikely(!rtc->regbase)) {
406 rtc->rtc_dev = rtc_device_register("sh", &pdev->dev,
407 &sh_rtc_ops, THIS_MODULE);
409 ret = PTR_ERR(rtc->rtc_dev);
413 platform_set_drvdata(pdev, rtc);
418 release_resource(rtc->res);
425 static int __devexit sh_rtc_remove(struct platform_device *pdev)
427 struct sh_rtc *rtc = platform_get_drvdata(pdev);
429 if (likely(rtc->rtc_dev))
430 rtc_device_unregister(rtc->rtc_dev);
432 sh_rtc_setpie(&pdev->dev, 0);
433 sh_rtc_setaie(&pdev->dev, 0);
435 release_resource(rtc->res);
437 platform_set_drvdata(pdev, NULL);
443 static struct platform_driver sh_rtc_platform_driver = {
446 .owner = THIS_MODULE,
448 .probe = sh_rtc_probe,
449 .remove = __devexit_p(sh_rtc_remove),
452 static int __init sh_rtc_init(void)
454 return platform_driver_register(&sh_rtc_platform_driver);
457 static void __exit sh_rtc_exit(void)
459 platform_driver_unregister(&sh_rtc_platform_driver);
462 module_init(sh_rtc_init);
463 module_exit(sh_rtc_exit);
465 MODULE_DESCRIPTION("SuperH on-chip RTC driver");
466 MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
467 MODULE_LICENSE("GPL");