1 /* drivers/rtc/rtc-s3c.c
3 * Copyright (c) 2004,2006 Simtec Electronics
4 * Ben Dooks, <ben@simtec.co.uk>
5 * http://armlinux.simtec.co.uk/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * S3C2410/S3C2440/S3C24XX Internal RTC Driver
14 #include <linux/module.h>
16 #include <linux/string.h>
17 #include <linux/init.h>
18 #include <linux/platform_device.h>
19 #include <linux/interrupt.h>
20 #include <linux/rtc.h>
21 #include <linux/bcd.h>
22 #include <linux/clk.h>
23 #include <linux/log2.h>
25 #include <asm/hardware.h>
26 #include <asm/uaccess.h>
31 #include <asm/plat-s3c/regs-rtc.h>
33 /* I have yet to find an S3C implementation with more than one
34 * of these rtc blocks in */
36 static struct resource *s3c_rtc_mem;
38 static void __iomem *s3c_rtc_base;
39 static int s3c_rtc_alarmno = NO_IRQ;
40 static int s3c_rtc_tickno = NO_IRQ;
41 static int s3c_rtc_freq = 1;
43 static DEFINE_SPINLOCK(s3c_rtc_pie_lock);
44 static unsigned int tick_count;
48 static irqreturn_t s3c_rtc_alarmirq(int irq, void *id)
50 struct rtc_device *rdev = id;
52 rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
56 static irqreturn_t s3c_rtc_tickirq(int irq, void *id)
58 struct rtc_device *rdev = id;
60 rtc_update_irq(rdev, tick_count++, RTC_PF | RTC_IRQF);
64 /* Update control registers */
65 static void s3c_rtc_setaie(int to)
69 pr_debug("%s: aie=%d\n", __func__, to);
71 tmp = readb(s3c_rtc_base + S3C2410_RTCALM) & ~S3C2410_RTCALM_ALMEN;
74 tmp |= S3C2410_RTCALM_ALMEN;
76 writeb(tmp, s3c_rtc_base + S3C2410_RTCALM);
79 static void s3c_rtc_setpie(int to)
83 pr_debug("%s: pie=%d\n", __func__, to);
85 spin_lock_irq(&s3c_rtc_pie_lock);
86 tmp = readb(s3c_rtc_base + S3C2410_TICNT) & ~S3C2410_TICNT_ENABLE;
89 tmp |= S3C2410_TICNT_ENABLE;
91 writeb(tmp, s3c_rtc_base + S3C2410_TICNT);
92 spin_unlock_irq(&s3c_rtc_pie_lock);
95 static void s3c_rtc_setfreq(int freq)
99 spin_lock_irq(&s3c_rtc_pie_lock);
100 tmp = readb(s3c_rtc_base + S3C2410_TICNT) & S3C2410_TICNT_ENABLE;
104 tmp |= (128 / freq)-1;
106 writeb(tmp, s3c_rtc_base + S3C2410_TICNT);
107 spin_unlock_irq(&s3c_rtc_pie_lock);
110 /* Time read/write */
112 static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
114 unsigned int have_retried = 0;
115 void __iomem *base = s3c_rtc_base;
118 rtc_tm->tm_min = readb(base + S3C2410_RTCMIN);
119 rtc_tm->tm_hour = readb(base + S3C2410_RTCHOUR);
120 rtc_tm->tm_mday = readb(base + S3C2410_RTCDATE);
121 rtc_tm->tm_mon = readb(base + S3C2410_RTCMON);
122 rtc_tm->tm_year = readb(base + S3C2410_RTCYEAR);
123 rtc_tm->tm_sec = readb(base + S3C2410_RTCSEC);
125 /* the only way to work out wether the system was mid-update
126 * when we read it is to check the second counter, and if it
127 * is zero, then we re-try the entire read
130 if (rtc_tm->tm_sec == 0 && !have_retried) {
135 pr_debug("read time %02x.%02x.%02x %02x/%02x/%02x\n",
136 rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
137 rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
139 BCD_TO_BIN(rtc_tm->tm_sec);
140 BCD_TO_BIN(rtc_tm->tm_min);
141 BCD_TO_BIN(rtc_tm->tm_hour);
142 BCD_TO_BIN(rtc_tm->tm_mday);
143 BCD_TO_BIN(rtc_tm->tm_mon);
144 BCD_TO_BIN(rtc_tm->tm_year);
146 rtc_tm->tm_year += 100;
152 static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
154 void __iomem *base = s3c_rtc_base;
155 int year = tm->tm_year - 100;
157 pr_debug("set time %02d.%02d.%02d %02d/%02d/%02d\n",
158 tm->tm_year, tm->tm_mon, tm->tm_mday,
159 tm->tm_hour, tm->tm_min, tm->tm_sec);
161 /* we get around y2k by simply not supporting it */
163 if (year < 0 || year >= 100) {
164 dev_err(dev, "rtc only supports 100 years\n");
168 writeb(BIN2BCD(tm->tm_sec), base + S3C2410_RTCSEC);
169 writeb(BIN2BCD(tm->tm_min), base + S3C2410_RTCMIN);
170 writeb(BIN2BCD(tm->tm_hour), base + S3C2410_RTCHOUR);
171 writeb(BIN2BCD(tm->tm_mday), base + S3C2410_RTCDATE);
172 writeb(BIN2BCD(tm->tm_mon + 1), base + S3C2410_RTCMON);
173 writeb(BIN2BCD(year), base + S3C2410_RTCYEAR);
178 static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
180 struct rtc_time *alm_tm = &alrm->time;
181 void __iomem *base = s3c_rtc_base;
184 alm_tm->tm_sec = readb(base + S3C2410_ALMSEC);
185 alm_tm->tm_min = readb(base + S3C2410_ALMMIN);
186 alm_tm->tm_hour = readb(base + S3C2410_ALMHOUR);
187 alm_tm->tm_mon = readb(base + S3C2410_ALMMON);
188 alm_tm->tm_mday = readb(base + S3C2410_ALMDATE);
189 alm_tm->tm_year = readb(base + S3C2410_ALMYEAR);
191 alm_en = readb(base + S3C2410_RTCALM);
193 alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;
195 pr_debug("read alarm %02x %02x.%02x.%02x %02x/%02x/%02x\n",
197 alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
198 alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
201 /* decode the alarm enable field */
203 if (alm_en & S3C2410_RTCALM_SECEN)
204 BCD_TO_BIN(alm_tm->tm_sec);
206 alm_tm->tm_sec = 0xff;
208 if (alm_en & S3C2410_RTCALM_MINEN)
209 BCD_TO_BIN(alm_tm->tm_min);
211 alm_tm->tm_min = 0xff;
213 if (alm_en & S3C2410_RTCALM_HOUREN)
214 BCD_TO_BIN(alm_tm->tm_hour);
216 alm_tm->tm_hour = 0xff;
218 if (alm_en & S3C2410_RTCALM_DAYEN)
219 BCD_TO_BIN(alm_tm->tm_mday);
221 alm_tm->tm_mday = 0xff;
223 if (alm_en & S3C2410_RTCALM_MONEN) {
224 BCD_TO_BIN(alm_tm->tm_mon);
227 alm_tm->tm_mon = 0xff;
230 if (alm_en & S3C2410_RTCALM_YEAREN)
231 BCD_TO_BIN(alm_tm->tm_year);
233 alm_tm->tm_year = 0xffff;
238 static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
240 struct rtc_time *tm = &alrm->time;
241 void __iomem *base = s3c_rtc_base;
242 unsigned int alrm_en;
244 pr_debug("s3c_rtc_setalarm: %d, %02x/%02x/%02x %02x.%02x.%02x\n",
246 tm->tm_mday & 0xff, tm->tm_mon & 0xff, tm->tm_year & 0xff,
247 tm->tm_hour & 0xff, tm->tm_min & 0xff, tm->tm_sec);
250 alrm_en = readb(base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
251 writeb(0x00, base + S3C2410_RTCALM);
253 if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
254 alrm_en |= S3C2410_RTCALM_SECEN;
255 writeb(BIN2BCD(tm->tm_sec), base + S3C2410_ALMSEC);
258 if (tm->tm_min < 60 && tm->tm_min >= 0) {
259 alrm_en |= S3C2410_RTCALM_MINEN;
260 writeb(BIN2BCD(tm->tm_min), base + S3C2410_ALMMIN);
263 if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
264 alrm_en |= S3C2410_RTCALM_HOUREN;
265 writeb(BIN2BCD(tm->tm_hour), base + S3C2410_ALMHOUR);
268 pr_debug("setting S3C2410_RTCALM to %08x\n", alrm_en);
270 writeb(alrm_en, base + S3C2410_RTCALM);
273 alrm_en = readb(base + S3C2410_RTCALM);
274 alrm_en &= ~S3C2410_RTCALM_ALMEN;
275 writeb(alrm_en, base + S3C2410_RTCALM);
276 disable_irq_wake(s3c_rtc_alarmno);
280 enable_irq_wake(s3c_rtc_alarmno);
282 disable_irq_wake(s3c_rtc_alarmno);
287 static int s3c_rtc_ioctl(struct device *dev,
288 unsigned int cmd, unsigned long arg)
290 unsigned int ret = -ENOIOCTLCMD;
295 s3c_rtc_setaie((cmd == RTC_AIE_ON) ? 1 : 0);
302 s3c_rtc_setpie((cmd == RTC_PIE_ON) ? 1 : 0);
307 ret = put_user(s3c_rtc_freq, (unsigned long __user *)arg);
311 if (!is_power_of_2(arg)) {
316 pr_debug("s3c2410_rtc: setting frequency %ld\n", arg);
318 s3c_rtc_setfreq(arg);
331 static int s3c_rtc_proc(struct device *dev, struct seq_file *seq)
333 unsigned int ticnt = readb(s3c_rtc_base + S3C2410_TICNT);
335 seq_printf(seq, "periodic_IRQ\t: %s\n",
336 (ticnt & S3C2410_TICNT_ENABLE) ? "yes" : "no" );
338 seq_printf(seq, "periodic_freq\t: %d\n", s3c_rtc_freq);
343 static int s3c_rtc_open(struct device *dev)
345 struct platform_device *pdev = to_platform_device(dev);
346 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
349 ret = request_irq(s3c_rtc_alarmno, s3c_rtc_alarmirq,
350 IRQF_DISABLED, "s3c2410-rtc alarm", rtc_dev);
353 dev_err(dev, "IRQ%d error %d\n", s3c_rtc_alarmno, ret);
357 ret = request_irq(s3c_rtc_tickno, s3c_rtc_tickirq,
358 IRQF_DISABLED, "s3c2410-rtc tick", rtc_dev);
361 dev_err(dev, "IRQ%d error %d\n", s3c_rtc_tickno, ret);
368 free_irq(s3c_rtc_alarmno, rtc_dev);
372 static void s3c_rtc_release(struct device *dev)
374 struct platform_device *pdev = to_platform_device(dev);
375 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
377 /* do not clear AIE here, it may be needed for wake */
380 free_irq(s3c_rtc_alarmno, rtc_dev);
381 free_irq(s3c_rtc_tickno, rtc_dev);
384 static const struct rtc_class_ops s3c_rtcops = {
385 .open = s3c_rtc_open,
386 .release = s3c_rtc_release,
387 .ioctl = s3c_rtc_ioctl,
388 .read_time = s3c_rtc_gettime,
389 .set_time = s3c_rtc_settime,
390 .read_alarm = s3c_rtc_getalarm,
391 .set_alarm = s3c_rtc_setalarm,
392 .proc = s3c_rtc_proc,
395 static void s3c_rtc_enable(struct platform_device *pdev, int en)
397 void __iomem *base = s3c_rtc_base;
400 if (s3c_rtc_base == NULL)
404 tmp = readb(base + S3C2410_RTCCON);
405 writeb(tmp & ~S3C2410_RTCCON_RTCEN, base + S3C2410_RTCCON);
407 tmp = readb(base + S3C2410_TICNT);
408 writeb(tmp & ~S3C2410_TICNT_ENABLE, base + S3C2410_TICNT);
410 /* re-enable the device, and check it is ok */
412 if ((readb(base+S3C2410_RTCCON) & S3C2410_RTCCON_RTCEN) == 0){
413 dev_info(&pdev->dev, "rtc disabled, re-enabling\n");
415 tmp = readb(base + S3C2410_RTCCON);
416 writeb(tmp|S3C2410_RTCCON_RTCEN, base+S3C2410_RTCCON);
419 if ((readb(base + S3C2410_RTCCON) & S3C2410_RTCCON_CNTSEL)){
420 dev_info(&pdev->dev, "removing RTCCON_CNTSEL\n");
422 tmp = readb(base + S3C2410_RTCCON);
423 writeb(tmp& ~S3C2410_RTCCON_CNTSEL, base+S3C2410_RTCCON);
426 if ((readb(base + S3C2410_RTCCON) & S3C2410_RTCCON_CLKRST)){
427 dev_info(&pdev->dev, "removing RTCCON_CLKRST\n");
429 tmp = readb(base + S3C2410_RTCCON);
430 writeb(tmp & ~S3C2410_RTCCON_CLKRST, base+S3C2410_RTCCON);
435 static int s3c_rtc_remove(struct platform_device *dev)
437 struct rtc_device *rtc = platform_get_drvdata(dev);
439 platform_set_drvdata(dev, NULL);
440 rtc_device_unregister(rtc);
445 iounmap(s3c_rtc_base);
446 release_resource(s3c_rtc_mem);
452 static int s3c_rtc_probe(struct platform_device *pdev)
454 struct rtc_device *rtc;
455 struct resource *res;
458 pr_debug("%s: probe=%p\n", __func__, pdev);
462 s3c_rtc_tickno = platform_get_irq(pdev, 1);
463 if (s3c_rtc_tickno < 0) {
464 dev_err(&pdev->dev, "no irq for rtc tick\n");
468 s3c_rtc_alarmno = platform_get_irq(pdev, 0);
469 if (s3c_rtc_alarmno < 0) {
470 dev_err(&pdev->dev, "no irq for alarm\n");
474 pr_debug("s3c2410_rtc: tick irq %d, alarm irq %d\n",
475 s3c_rtc_tickno, s3c_rtc_alarmno);
477 /* get the memory region */
479 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
481 dev_err(&pdev->dev, "failed to get memory region resource\n");
485 s3c_rtc_mem = request_mem_region(res->start,
486 res->end-res->start+1,
489 if (s3c_rtc_mem == NULL) {
490 dev_err(&pdev->dev, "failed to reserve memory region\n");
495 s3c_rtc_base = ioremap(res->start, res->end - res->start + 1);
496 if (s3c_rtc_base == NULL) {
497 dev_err(&pdev->dev, "failed ioremap()\n");
502 /* check to see if everything is setup correctly */
504 s3c_rtc_enable(pdev, 1);
506 pr_debug("s3c2410_rtc: RTCCON=%02x\n",
507 readb(s3c_rtc_base + S3C2410_RTCCON));
509 s3c_rtc_setfreq(s3c_rtc_freq);
511 /* register RTC and exit */
513 rtc = rtc_device_register("s3c", &pdev->dev, &s3c_rtcops,
517 dev_err(&pdev->dev, "cannot attach rtc\n");
522 rtc->max_user_freq = 128;
524 platform_set_drvdata(pdev, rtc);
528 s3c_rtc_enable(pdev, 0);
529 iounmap(s3c_rtc_base);
532 release_resource(s3c_rtc_mem);
540 /* RTC Power management control */
542 static int ticnt_save;
544 static int s3c_rtc_suspend(struct platform_device *pdev, pm_message_t state)
546 /* save TICNT for anyone using periodic interrupts */
547 ticnt_save = readb(s3c_rtc_base + S3C2410_TICNT);
548 s3c_rtc_enable(pdev, 0);
552 static int s3c_rtc_resume(struct platform_device *pdev)
554 s3c_rtc_enable(pdev, 1);
555 writeb(ticnt_save, s3c_rtc_base + S3C2410_TICNT);
559 #define s3c_rtc_suspend NULL
560 #define s3c_rtc_resume NULL
563 static struct platform_driver s3c2410_rtcdrv = {
564 .probe = s3c_rtc_probe,
565 .remove = s3c_rtc_remove,
566 .suspend = s3c_rtc_suspend,
567 .resume = s3c_rtc_resume,
569 .name = "s3c2410-rtc",
570 .owner = THIS_MODULE,
574 static char __initdata banner[] = "S3C24XX RTC, (c) 2004,2006 Simtec Electronics\n";
576 static int __init s3c_rtc_init(void)
579 return platform_driver_register(&s3c2410_rtcdrv);
582 static void __exit s3c_rtc_exit(void)
584 platform_driver_unregister(&s3c2410_rtcdrv);
587 module_init(s3c_rtc_init);
588 module_exit(s3c_rtc_exit);
590 MODULE_DESCRIPTION("Samsung S3C RTC Driver");
591 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
592 MODULE_LICENSE("GPL");
593 MODULE_ALIAS("platform:s3c2410-rtc");