1 /* drivers/rtc/rtc-s3c.c
3 * Copyright (c) 2010 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com/
6 * Copyright (c) 2004,2006 Simtec Electronics
7 * Ben Dooks, <ben@simtec.co.uk>
8 * http://armlinux.simtec.co.uk/
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * S3C2410/S3C2440/S3C24XX Internal RTC Driver
17 #include <linux/module.h>
19 #include <linux/string.h>
20 #include <linux/init.h>
21 #include <linux/platform_device.h>
22 #include <linux/interrupt.h>
23 #include <linux/rtc.h>
24 #include <linux/bcd.h>
25 #include <linux/clk.h>
26 #include <linux/log2.h>
27 #include <linux/slab.h>
29 #include <mach/hardware.h>
30 #include <asm/uaccess.h>
33 #include <plat/regs-rtc.h>
40 /* I have yet to find an S3C implementation with more than one
41 * of these rtc blocks in */
43 static struct resource *s3c_rtc_mem;
45 static struct clk *rtc_clk;
46 static void __iomem *s3c_rtc_base;
47 static int s3c_rtc_alarmno = NO_IRQ;
48 static int s3c_rtc_tickno = NO_IRQ;
49 static enum s3c_cpu_type s3c_rtc_cpu_type;
51 static DEFINE_SPINLOCK(s3c_rtc_pie_lock);
55 static irqreturn_t s3c_rtc_alarmirq(int irq, void *id)
57 struct rtc_device *rdev = id;
59 rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
61 if (s3c_rtc_cpu_type == TYPE_S3C64XX)
62 writeb(S3C2410_INTP_ALM, s3c_rtc_base + S3C2410_INTP);
67 static irqreturn_t s3c_rtc_tickirq(int irq, void *id)
69 struct rtc_device *rdev = id;
71 rtc_update_irq(rdev, 1, RTC_PF | RTC_IRQF);
73 if (s3c_rtc_cpu_type == TYPE_S3C64XX)
74 writeb(S3C2410_INTP_TIC, s3c_rtc_base + S3C2410_INTP);
79 /* Update control registers */
80 static int s3c_rtc_setaie(struct device *dev, unsigned int enabled)
84 pr_debug("%s: aie=%d\n", __func__, enabled);
86 tmp = readb(s3c_rtc_base + S3C2410_RTCALM) & ~S3C2410_RTCALM_ALMEN;
89 tmp |= S3C2410_RTCALM_ALMEN;
91 writeb(tmp, s3c_rtc_base + S3C2410_RTCALM);
96 static int s3c_rtc_setpie(struct device *dev, int enabled)
100 pr_debug("%s: pie=%d\n", __func__, enabled);
102 spin_lock_irq(&s3c_rtc_pie_lock);
104 if (s3c_rtc_cpu_type == TYPE_S3C64XX) {
105 tmp = readw(s3c_rtc_base + S3C2410_RTCCON);
106 tmp &= ~S3C64XX_RTCCON_TICEN;
109 tmp |= S3C64XX_RTCCON_TICEN;
111 writew(tmp, s3c_rtc_base + S3C2410_RTCCON);
113 tmp = readb(s3c_rtc_base + S3C2410_TICNT);
114 tmp &= ~S3C2410_TICNT_ENABLE;
117 tmp |= S3C2410_TICNT_ENABLE;
119 writeb(tmp, s3c_rtc_base + S3C2410_TICNT);
122 spin_unlock_irq(&s3c_rtc_pie_lock);
127 static int s3c_rtc_setfreq(struct device *dev, int freq)
129 struct platform_device *pdev = to_platform_device(dev);
130 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
131 unsigned int tmp = 0;
133 if (!is_power_of_2(freq))
136 spin_lock_irq(&s3c_rtc_pie_lock);
138 if (s3c_rtc_cpu_type == TYPE_S3C2410) {
139 tmp = readb(s3c_rtc_base + S3C2410_TICNT);
140 tmp &= S3C2410_TICNT_ENABLE;
143 tmp |= (rtc_dev->max_user_freq / freq)-1;
145 writel(tmp, s3c_rtc_base + S3C2410_TICNT);
146 spin_unlock_irq(&s3c_rtc_pie_lock);
151 /* Time read/write */
153 static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
155 unsigned int have_retried = 0;
156 void __iomem *base = s3c_rtc_base;
159 rtc_tm->tm_min = readb(base + S3C2410_RTCMIN);
160 rtc_tm->tm_hour = readb(base + S3C2410_RTCHOUR);
161 rtc_tm->tm_mday = readb(base + S3C2410_RTCDATE);
162 rtc_tm->tm_mon = readb(base + S3C2410_RTCMON);
163 rtc_tm->tm_year = readb(base + S3C2410_RTCYEAR);
164 rtc_tm->tm_sec = readb(base + S3C2410_RTCSEC);
166 /* the only way to work out wether the system was mid-update
167 * when we read it is to check the second counter, and if it
168 * is zero, then we re-try the entire read
171 if (rtc_tm->tm_sec == 0 && !have_retried) {
176 pr_debug("read time %04d.%02d.%02d %02d:%02d:%02d\n",
177 1900 + rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
178 rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
180 rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
181 rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
182 rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
183 rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
184 rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
185 rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
187 rtc_tm->tm_year += 100;
190 return rtc_valid_tm(rtc_tm);
193 static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
195 void __iomem *base = s3c_rtc_base;
196 int year = tm->tm_year - 100;
198 pr_debug("set time %04d.%02d.%02d %02d:%02d:%02d\n",
199 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
200 tm->tm_hour, tm->tm_min, tm->tm_sec);
202 /* we get around y2k by simply not supporting it */
204 if (year < 0 || year >= 100) {
205 dev_err(dev, "rtc only supports 100 years\n");
209 writeb(bin2bcd(tm->tm_sec), base + S3C2410_RTCSEC);
210 writeb(bin2bcd(tm->tm_min), base + S3C2410_RTCMIN);
211 writeb(bin2bcd(tm->tm_hour), base + S3C2410_RTCHOUR);
212 writeb(bin2bcd(tm->tm_mday), base + S3C2410_RTCDATE);
213 writeb(bin2bcd(tm->tm_mon + 1), base + S3C2410_RTCMON);
214 writeb(bin2bcd(year), base + S3C2410_RTCYEAR);
219 static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
221 struct rtc_time *alm_tm = &alrm->time;
222 void __iomem *base = s3c_rtc_base;
225 alm_tm->tm_sec = readb(base + S3C2410_ALMSEC);
226 alm_tm->tm_min = readb(base + S3C2410_ALMMIN);
227 alm_tm->tm_hour = readb(base + S3C2410_ALMHOUR);
228 alm_tm->tm_mon = readb(base + S3C2410_ALMMON);
229 alm_tm->tm_mday = readb(base + S3C2410_ALMDATE);
230 alm_tm->tm_year = readb(base + S3C2410_ALMYEAR);
232 alm_en = readb(base + S3C2410_RTCALM);
234 alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;
236 pr_debug("read alarm %d, %04d.%02d.%02d %02d:%02d:%02d\n",
238 1900 + alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
239 alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
242 /* decode the alarm enable field */
244 if (alm_en & S3C2410_RTCALM_SECEN)
245 alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec);
249 if (alm_en & S3C2410_RTCALM_MINEN)
250 alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
254 if (alm_en & S3C2410_RTCALM_HOUREN)
255 alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
257 alm_tm->tm_hour = -1;
259 if (alm_en & S3C2410_RTCALM_DAYEN)
260 alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday);
262 alm_tm->tm_mday = -1;
264 if (alm_en & S3C2410_RTCALM_MONEN) {
265 alm_tm->tm_mon = bcd2bin(alm_tm->tm_mon);
271 if (alm_en & S3C2410_RTCALM_YEAREN)
272 alm_tm->tm_year = bcd2bin(alm_tm->tm_year);
274 alm_tm->tm_year = -1;
279 static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
281 struct rtc_time *tm = &alrm->time;
282 void __iomem *base = s3c_rtc_base;
283 unsigned int alrm_en;
285 pr_debug("s3c_rtc_setalarm: %d, %04d.%02d.%02d %02d:%02d:%02d\n",
287 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
288 tm->tm_hour, tm->tm_min, tm->tm_sec);
291 alrm_en = readb(base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
292 writeb(0x00, base + S3C2410_RTCALM);
294 if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
295 alrm_en |= S3C2410_RTCALM_SECEN;
296 writeb(bin2bcd(tm->tm_sec), base + S3C2410_ALMSEC);
299 if (tm->tm_min < 60 && tm->tm_min >= 0) {
300 alrm_en |= S3C2410_RTCALM_MINEN;
301 writeb(bin2bcd(tm->tm_min), base + S3C2410_ALMMIN);
304 if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
305 alrm_en |= S3C2410_RTCALM_HOUREN;
306 writeb(bin2bcd(tm->tm_hour), base + S3C2410_ALMHOUR);
309 pr_debug("setting S3C2410_RTCALM to %08x\n", alrm_en);
311 writeb(alrm_en, base + S3C2410_RTCALM);
313 s3c_rtc_setaie(dev, alrm->enabled);
318 static int s3c_rtc_proc(struct device *dev, struct seq_file *seq)
322 if (s3c_rtc_cpu_type == TYPE_S3C64XX) {
323 ticnt = readw(s3c_rtc_base + S3C2410_RTCCON);
324 ticnt &= S3C64XX_RTCCON_TICEN;
326 ticnt = readb(s3c_rtc_base + S3C2410_TICNT);
327 ticnt &= S3C2410_TICNT_ENABLE;
330 seq_printf(seq, "periodic_IRQ\t: %s\n", ticnt ? "yes" : "no");
334 static int s3c_rtc_open(struct device *dev)
336 struct platform_device *pdev = to_platform_device(dev);
337 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
340 ret = request_irq(s3c_rtc_alarmno, s3c_rtc_alarmirq,
341 IRQF_DISABLED, "s3c2410-rtc alarm", rtc_dev);
344 dev_err(dev, "IRQ%d error %d\n", s3c_rtc_alarmno, ret);
348 ret = request_irq(s3c_rtc_tickno, s3c_rtc_tickirq,
349 IRQF_DISABLED, "s3c2410-rtc tick", rtc_dev);
352 dev_err(dev, "IRQ%d error %d\n", s3c_rtc_tickno, ret);
359 free_irq(s3c_rtc_alarmno, rtc_dev);
363 static void s3c_rtc_release(struct device *dev)
365 struct platform_device *pdev = to_platform_device(dev);
366 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
368 /* do not clear AIE here, it may be needed for wake */
370 s3c_rtc_setpie(dev, 0);
371 free_irq(s3c_rtc_alarmno, rtc_dev);
372 free_irq(s3c_rtc_tickno, rtc_dev);
375 static const struct rtc_class_ops s3c_rtcops = {
376 .open = s3c_rtc_open,
377 .release = s3c_rtc_release,
378 .read_time = s3c_rtc_gettime,
379 .set_time = s3c_rtc_settime,
380 .read_alarm = s3c_rtc_getalarm,
381 .set_alarm = s3c_rtc_setalarm,
382 .irq_set_freq = s3c_rtc_setfreq,
383 .irq_set_state = s3c_rtc_setpie,
384 .proc = s3c_rtc_proc,
385 .alarm_irq_enable = s3c_rtc_setaie,
388 static void s3c_rtc_enable(struct platform_device *pdev, int en)
390 void __iomem *base = s3c_rtc_base;
393 if (s3c_rtc_base == NULL)
397 tmp = readw(base + S3C2410_RTCCON);
398 if (s3c_rtc_cpu_type == TYPE_S3C64XX)
399 tmp &= ~S3C64XX_RTCCON_TICEN;
400 tmp &= ~S3C2410_RTCCON_RTCEN;
401 writew(tmp, base + S3C2410_RTCCON);
403 if (s3c_rtc_cpu_type == TYPE_S3C2410) {
404 tmp = readb(base + S3C2410_TICNT);
405 tmp &= ~S3C2410_TICNT_ENABLE;
406 writeb(tmp, base + S3C2410_TICNT);
409 /* re-enable the device, and check it is ok */
411 if ((readw(base+S3C2410_RTCCON) & S3C2410_RTCCON_RTCEN) == 0) {
412 dev_info(&pdev->dev, "rtc disabled, re-enabling\n");
414 tmp = readw(base + S3C2410_RTCCON);
415 writew(tmp | S3C2410_RTCCON_RTCEN,
416 base + S3C2410_RTCCON);
419 if ((readw(base + S3C2410_RTCCON) & S3C2410_RTCCON_CNTSEL)) {
420 dev_info(&pdev->dev, "removing RTCCON_CNTSEL\n");
422 tmp = readw(base + S3C2410_RTCCON);
423 writew(tmp & ~S3C2410_RTCCON_CNTSEL,
424 base + S3C2410_RTCCON);
427 if ((readw(base + S3C2410_RTCCON) & S3C2410_RTCCON_CLKRST)) {
428 dev_info(&pdev->dev, "removing RTCCON_CLKRST\n");
430 tmp = readw(base + S3C2410_RTCCON);
431 writew(tmp & ~S3C2410_RTCCON_CLKRST,
432 base + S3C2410_RTCCON);
437 static int __devexit s3c_rtc_remove(struct platform_device *dev)
439 struct rtc_device *rtc = platform_get_drvdata(dev);
441 platform_set_drvdata(dev, NULL);
442 rtc_device_unregister(rtc);
444 s3c_rtc_setpie(&dev->dev, 0);
445 s3c_rtc_setaie(&dev->dev, 0);
447 clk_disable(rtc_clk);
451 iounmap(s3c_rtc_base);
452 release_resource(s3c_rtc_mem);
458 static int __devinit s3c_rtc_probe(struct platform_device *pdev)
460 struct rtc_device *rtc;
461 struct rtc_time rtc_tm;
462 struct resource *res;
465 pr_debug("%s: probe=%p\n", __func__, pdev);
469 s3c_rtc_tickno = platform_get_irq(pdev, 1);
470 if (s3c_rtc_tickno < 0) {
471 dev_err(&pdev->dev, "no irq for rtc tick\n");
475 s3c_rtc_alarmno = platform_get_irq(pdev, 0);
476 if (s3c_rtc_alarmno < 0) {
477 dev_err(&pdev->dev, "no irq for alarm\n");
481 pr_debug("s3c2410_rtc: tick irq %d, alarm irq %d\n",
482 s3c_rtc_tickno, s3c_rtc_alarmno);
484 /* get the memory region */
486 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
488 dev_err(&pdev->dev, "failed to get memory region resource\n");
492 s3c_rtc_mem = request_mem_region(res->start,
493 res->end-res->start+1,
496 if (s3c_rtc_mem == NULL) {
497 dev_err(&pdev->dev, "failed to reserve memory region\n");
502 s3c_rtc_base = ioremap(res->start, res->end - res->start + 1);
503 if (s3c_rtc_base == NULL) {
504 dev_err(&pdev->dev, "failed ioremap()\n");
509 rtc_clk = clk_get(&pdev->dev, "rtc");
510 if (IS_ERR(rtc_clk)) {
511 dev_err(&pdev->dev, "failed to find rtc clock source\n");
512 ret = PTR_ERR(rtc_clk);
519 /* check to see if everything is setup correctly */
521 s3c_rtc_enable(pdev, 1);
523 pr_debug("s3c2410_rtc: RTCCON=%02x\n",
524 readw(s3c_rtc_base + S3C2410_RTCCON));
526 device_init_wakeup(&pdev->dev, 1);
528 /* register RTC and exit */
530 rtc = rtc_device_register("s3c", &pdev->dev, &s3c_rtcops,
534 dev_err(&pdev->dev, "cannot attach rtc\n");
539 s3c_rtc_cpu_type = platform_get_device_id(pdev)->driver_data;
543 s3c_rtc_gettime(NULL, &rtc_tm);
545 if (rtc_valid_tm(&rtc_tm)) {
546 rtc_tm.tm_year = 100;
553 s3c_rtc_settime(NULL, &rtc_tm);
555 dev_warn(&pdev->dev, "warning: invalid RTC value so initializing it\n");
558 if (s3c_rtc_cpu_type == TYPE_S3C64XX)
559 rtc->max_user_freq = 32768;
561 rtc->max_user_freq = 128;
563 platform_set_drvdata(pdev, rtc);
565 s3c_rtc_setfreq(&pdev->dev, 1);
570 s3c_rtc_enable(pdev, 0);
571 clk_disable(rtc_clk);
575 iounmap(s3c_rtc_base);
578 release_resource(s3c_rtc_mem);
586 /* RTC Power management control */
588 static int ticnt_save, ticnt_en_save;
590 static int s3c_rtc_suspend(struct platform_device *pdev, pm_message_t state)
592 /* save TICNT for anyone using periodic interrupts */
593 ticnt_save = readb(s3c_rtc_base + S3C2410_TICNT);
594 if (s3c_rtc_cpu_type == TYPE_S3C64XX) {
595 ticnt_en_save = readw(s3c_rtc_base + S3C2410_RTCCON);
596 ticnt_en_save &= S3C64XX_RTCCON_TICEN;
598 s3c_rtc_enable(pdev, 0);
600 if (device_may_wakeup(&pdev->dev))
601 enable_irq_wake(s3c_rtc_alarmno);
606 static int s3c_rtc_resume(struct platform_device *pdev)
610 s3c_rtc_enable(pdev, 1);
611 writeb(ticnt_save, s3c_rtc_base + S3C2410_TICNT);
612 if (s3c_rtc_cpu_type == TYPE_S3C64XX && ticnt_en_save) {
613 tmp = readw(s3c_rtc_base + S3C2410_RTCCON);
614 writew(tmp | ticnt_en_save, s3c_rtc_base + S3C2410_RTCCON);
617 if (device_may_wakeup(&pdev->dev))
618 disable_irq_wake(s3c_rtc_alarmno);
623 #define s3c_rtc_suspend NULL
624 #define s3c_rtc_resume NULL
627 static struct platform_device_id s3c_rtc_driver_ids[] = {
629 .name = "s3c2410-rtc",
630 .driver_data = TYPE_S3C2410,
632 .name = "s3c64xx-rtc",
633 .driver_data = TYPE_S3C64XX,
638 MODULE_DEVICE_TABLE(platform, s3c_rtc_driver_ids);
640 static struct platform_driver s3c_rtc_driver = {
641 .probe = s3c_rtc_probe,
642 .remove = __devexit_p(s3c_rtc_remove),
643 .suspend = s3c_rtc_suspend,
644 .resume = s3c_rtc_resume,
645 .id_table = s3c_rtc_driver_ids,
648 .owner = THIS_MODULE,
652 static char __initdata banner[] = "S3C24XX RTC, (c) 2004,2006 Simtec Electronics\n";
654 static int __init s3c_rtc_init(void)
657 return platform_driver_register(&s3c_rtc_driver);
660 static void __exit s3c_rtc_exit(void)
662 platform_driver_unregister(&s3c_rtc_driver);
665 module_init(s3c_rtc_init);
666 module_exit(s3c_rtc_exit);
668 MODULE_DESCRIPTION("Samsung S3C RTC Driver");
669 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
670 MODULE_LICENSE("GPL");
671 MODULE_ALIAS("platform:s3c2410-rtc");