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>
24 #include <asm/hardware.h>
25 #include <asm/uaccess.h>
30 #include <asm/mach/time.h>
32 #include <asm/arch/regs-rtc.h>
34 /* I have yet to find an S3C implementation with more than one
35 * of these rtc blocks in */
37 static struct resource
*s3c_rtc_mem
;
39 static void __iomem
*s3c_rtc_base
;
40 static int s3c_rtc_alarmno
= NO_IRQ
;
41 static int s3c_rtc_tickno
= NO_IRQ
;
42 static int s3c_rtc_freq
= 1;
44 static DEFINE_SPINLOCK(s3c_rtc_pie_lock
);
45 static unsigned int tick_count
;
49 static irqreturn_t
s3c_rtc_alarmirq(int irq
, void *id
, struct pt_regs
*r
)
51 struct rtc_device
*rdev
= id
;
53 rtc_update_irq(&rdev
->class_dev
, 1, RTC_AF
| RTC_IRQF
);
57 static irqreturn_t
s3c_rtc_tickirq(int irq
, void *id
, struct pt_regs
*r
)
59 struct rtc_device
*rdev
= id
;
61 rtc_update_irq(&rdev
->class_dev
, tick_count
++, RTC_PF
| RTC_IRQF
);
65 /* Update control registers */
66 static void s3c_rtc_setaie(int to
)
70 pr_debug("%s: aie=%d\n", __FUNCTION__
, to
);
72 tmp
= readb(S3C2410_RTCALM
) & ~S3C2410_RTCALM_ALMEN
;
75 tmp
|= S3C2410_RTCALM_ALMEN
;
77 writeb(tmp
, S3C2410_RTCALM
);
80 static void s3c_rtc_setpie(int to
)
84 pr_debug("%s: pie=%d\n", __FUNCTION__
, to
);
86 spin_lock_irq(&s3c_rtc_pie_lock
);
87 tmp
= readb(S3C2410_TICNT
) & ~S3C2410_TICNT_ENABLE
;
90 tmp
|= S3C2410_TICNT_ENABLE
;
92 writeb(tmp
, S3C2410_TICNT
);
93 spin_unlock_irq(&s3c_rtc_pie_lock
);
96 static void s3c_rtc_setfreq(int freq
)
100 spin_lock_irq(&s3c_rtc_pie_lock
);
101 tmp
= readb(S3C2410_TICNT
) & S3C2410_TICNT_ENABLE
;
105 tmp
|= (128 / freq
)-1;
107 writeb(tmp
, S3C2410_TICNT
);
108 spin_unlock_irq(&s3c_rtc_pie_lock
);
111 /* Time read/write */
113 static int s3c_rtc_gettime(struct device
*dev
, struct rtc_time
*rtc_tm
)
115 unsigned int have_retried
= 0;
118 rtc_tm
->tm_min
= readb(S3C2410_RTCMIN
);
119 rtc_tm
->tm_hour
= readb(S3C2410_RTCHOUR
);
120 rtc_tm
->tm_mday
= readb(S3C2410_RTCDATE
);
121 rtc_tm
->tm_mon
= readb(S3C2410_RTCMON
);
122 rtc_tm
->tm_year
= readb(S3C2410_RTCYEAR
);
123 rtc_tm
->tm_sec
= readb(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 /* the rtc gets round the y2k problem by just not supporting it */
156 if (tm
->tm_year
< 100)
159 writeb(BIN2BCD(tm
->tm_sec
), S3C2410_RTCSEC
);
160 writeb(BIN2BCD(tm
->tm_min
), S3C2410_RTCMIN
);
161 writeb(BIN2BCD(tm
->tm_hour
), S3C2410_RTCHOUR
);
162 writeb(BIN2BCD(tm
->tm_mday
), S3C2410_RTCDATE
);
163 writeb(BIN2BCD(tm
->tm_mon
+ 1), S3C2410_RTCMON
);
164 writeb(BIN2BCD(tm
->tm_year
- 100), S3C2410_RTCYEAR
);
169 static int s3c_rtc_getalarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
171 struct rtc_time
*alm_tm
= &alrm
->time
;
174 alm_tm
->tm_sec
= readb(S3C2410_ALMSEC
);
175 alm_tm
->tm_min
= readb(S3C2410_ALMMIN
);
176 alm_tm
->tm_hour
= readb(S3C2410_ALMHOUR
);
177 alm_tm
->tm_mon
= readb(S3C2410_ALMMON
);
178 alm_tm
->tm_mday
= readb(S3C2410_ALMDATE
);
179 alm_tm
->tm_year
= readb(S3C2410_ALMYEAR
);
181 alm_en
= readb(S3C2410_RTCALM
);
183 pr_debug("read alarm %02x %02x.%02x.%02x %02x/%02x/%02x\n",
185 alm_tm
->tm_year
, alm_tm
->tm_mon
, alm_tm
->tm_mday
,
186 alm_tm
->tm_hour
, alm_tm
->tm_min
, alm_tm
->tm_sec
);
189 /* decode the alarm enable field */
191 if (alm_en
& S3C2410_RTCALM_SECEN
)
192 BCD_TO_BIN(alm_tm
->tm_sec
);
194 alm_tm
->tm_sec
= 0xff;
196 if (alm_en
& S3C2410_RTCALM_MINEN
)
197 BCD_TO_BIN(alm_tm
->tm_min
);
199 alm_tm
->tm_min
= 0xff;
201 if (alm_en
& S3C2410_RTCALM_HOUREN
)
202 BCD_TO_BIN(alm_tm
->tm_hour
);
204 alm_tm
->tm_hour
= 0xff;
206 if (alm_en
& S3C2410_RTCALM_DAYEN
)
207 BCD_TO_BIN(alm_tm
->tm_mday
);
209 alm_tm
->tm_mday
= 0xff;
211 if (alm_en
& S3C2410_RTCALM_MONEN
) {
212 BCD_TO_BIN(alm_tm
->tm_mon
);
215 alm_tm
->tm_mon
= 0xff;
218 if (alm_en
& S3C2410_RTCALM_YEAREN
)
219 BCD_TO_BIN(alm_tm
->tm_year
);
221 alm_tm
->tm_year
= 0xffff;
226 static int s3c_rtc_setalarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
228 struct rtc_time
*tm
= &alrm
->time
;
229 unsigned int alrm_en
;
231 pr_debug("s3c_rtc_setalarm: %d, %02x/%02x/%02x %02x.%02x.%02x\n",
233 tm
->tm_mday
& 0xff, tm
->tm_mon
& 0xff, tm
->tm_year
& 0xff,
234 tm
->tm_hour
& 0xff, tm
->tm_min
& 0xff, tm
->tm_sec
);
237 alrm_en
= readb(S3C2410_RTCALM
) & S3C2410_RTCALM_ALMEN
;
238 writeb(0x00, S3C2410_RTCALM
);
240 if (tm
->tm_sec
< 60 && tm
->tm_sec
>= 0) {
241 alrm_en
|= S3C2410_RTCALM_SECEN
;
242 writeb(BIN2BCD(tm
->tm_sec
), S3C2410_ALMSEC
);
245 if (tm
->tm_min
< 60 && tm
->tm_min
>= 0) {
246 alrm_en
|= S3C2410_RTCALM_MINEN
;
247 writeb(BIN2BCD(tm
->tm_min
), S3C2410_ALMMIN
);
250 if (tm
->tm_hour
< 24 && tm
->tm_hour
>= 0) {
251 alrm_en
|= S3C2410_RTCALM_HOUREN
;
252 writeb(BIN2BCD(tm
->tm_hour
), S3C2410_ALMHOUR
);
255 pr_debug("setting S3C2410_RTCALM to %08x\n", alrm_en
);
257 writeb(alrm_en
, S3C2410_RTCALM
);
260 alrm_en
= readb(S3C2410_RTCALM
);
261 alrm_en
&= ~S3C2410_RTCALM_ALMEN
;
262 writeb(alrm_en
, S3C2410_RTCALM
);
263 disable_irq_wake(s3c_rtc_alarmno
);
267 enable_irq_wake(s3c_rtc_alarmno
);
269 disable_irq_wake(s3c_rtc_alarmno
);
274 static int s3c_rtc_ioctl(struct device
*dev
,
275 unsigned int cmd
, unsigned long arg
)
277 unsigned int ret
= -ENOIOCTLCMD
;
282 s3c_rtc_setaie((cmd
== RTC_AIE_ON
) ? 1 : 0);
289 s3c_rtc_setpie((cmd
== RTC_PIE_ON
) ? 1 : 0);
294 ret
= put_user(s3c_rtc_freq
, (unsigned long __user
*)arg
);
298 /* check for power of 2 */
300 if ((arg
& (arg
-1)) != 0 || arg
< 1) {
305 pr_debug("s3c2410_rtc: setting frequency %ld\n", arg
);
307 s3c_rtc_setfreq(arg
);
320 static int s3c_rtc_proc(struct device
*dev
, struct seq_file
*seq
)
322 unsigned int rtcalm
= readb(S3C2410_RTCALM
);
323 unsigned int ticnt
= readb (S3C2410_TICNT
);
325 seq_printf(seq
, "alarm_IRQ\t: %s\n",
326 (rtcalm
& S3C2410_RTCALM_ALMEN
) ? "yes" : "no" );
328 seq_printf(seq
, "periodic_IRQ\t: %s\n",
329 (ticnt
& S3C2410_TICNT_ENABLE
) ? "yes" : "no" );
331 seq_printf(seq
, "periodic_freq\t: %d\n", s3c_rtc_freq
);
336 static int s3c_rtc_open(struct device
*dev
)
338 struct platform_device
*pdev
= to_platform_device(dev
);
339 struct rtc_device
*rtc_dev
= platform_get_drvdata(pdev
);
342 ret
= request_irq(s3c_rtc_alarmno
, s3c_rtc_alarmirq
,
343 SA_INTERRUPT
, "s3c2410-rtc alarm", rtc_dev
);
346 dev_err(dev
, "IRQ%d error %d\n", s3c_rtc_alarmno
, ret
);
350 ret
= request_irq(s3c_rtc_tickno
, s3c_rtc_tickirq
,
351 SA_INTERRUPT
, "s3c2410-rtc tick", rtc_dev
);
354 dev_err(dev
, "IRQ%d error %d\n", s3c_rtc_tickno
, ret
);
361 free_irq(s3c_rtc_alarmno
, rtc_dev
);
365 static void s3c_rtc_release(struct device
*dev
)
367 struct platform_device
*pdev
= to_platform_device(dev
);
368 struct rtc_device
*rtc_dev
= platform_get_drvdata(pdev
);
370 /* do not clear AIE here, it may be needed for wake */
373 free_irq(s3c_rtc_alarmno
, rtc_dev
);
374 free_irq(s3c_rtc_tickno
, rtc_dev
);
377 static struct rtc_class_ops s3c_rtcops
= {
378 .open
= s3c_rtc_open
,
379 .release
= s3c_rtc_release
,
380 .ioctl
= s3c_rtc_ioctl
,
381 .read_time
= s3c_rtc_gettime
,
382 .set_time
= s3c_rtc_settime
,
383 .read_alarm
= s3c_rtc_getalarm
,
384 .set_alarm
= s3c_rtc_setalarm
,
385 .proc
= s3c_rtc_proc
,
388 static void s3c_rtc_enable(struct platform_device
*pdev
, int en
)
392 if (s3c_rtc_base
== NULL
)
396 tmp
= readb(S3C2410_RTCCON
);
397 writeb(tmp
& ~S3C2410_RTCCON_RTCEN
, S3C2410_RTCCON
);
399 tmp
= readb(S3C2410_TICNT
);
400 writeb(tmp
& ~S3C2410_TICNT_ENABLE
, S3C2410_TICNT
);
402 /* re-enable the device, and check it is ok */
404 if ((readb(S3C2410_RTCCON
) & S3C2410_RTCCON_RTCEN
) == 0){
405 dev_info(&pdev
->dev
, "rtc disabled, re-enabling\n");
407 tmp
= readb(S3C2410_RTCCON
);
408 writeb(tmp
| S3C2410_RTCCON_RTCEN
, S3C2410_RTCCON
);
411 if ((readb(S3C2410_RTCCON
) & S3C2410_RTCCON_CNTSEL
)){
412 dev_info(&pdev
->dev
, "removing RTCCON_CNTSEL\n");
414 tmp
= readb(S3C2410_RTCCON
);
415 writeb(tmp
& ~S3C2410_RTCCON_CNTSEL
, S3C2410_RTCCON
);
418 if ((readb(S3C2410_RTCCON
) & S3C2410_RTCCON_CLKRST
)){
419 dev_info(&pdev
->dev
, "removing RTCCON_CLKRST\n");
421 tmp
= readb(S3C2410_RTCCON
);
422 writeb(tmp
& ~S3C2410_RTCCON_CLKRST
, S3C2410_RTCCON
);
427 static int s3c_rtc_remove(struct platform_device
*dev
)
429 struct rtc_device
*rtc
= platform_get_drvdata(dev
);
431 platform_set_drvdata(dev
, NULL
);
432 rtc_device_unregister(rtc
);
437 iounmap(s3c_rtc_base
);
438 release_resource(s3c_rtc_mem
);
444 static int s3c_rtc_probe(struct platform_device
*pdev
)
446 struct rtc_device
*rtc
;
447 struct resource
*res
;
450 pr_debug("%s: probe=%p\n", __FUNCTION__
, pdev
);
454 s3c_rtc_tickno
= platform_get_irq(pdev
, 1);
455 if (s3c_rtc_tickno
< 0) {
456 dev_err(&pdev
->dev
, "no irq for rtc tick\n");
460 s3c_rtc_alarmno
= platform_get_irq(pdev
, 0);
461 if (s3c_rtc_alarmno
< 0) {
462 dev_err(&pdev
->dev
, "no irq for alarm\n");
466 pr_debug("s3c2410_rtc: tick irq %d, alarm irq %d\n",
467 s3c_rtc_tickno
, s3c_rtc_alarmno
);
469 /* get the memory region */
471 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
473 dev_err(&pdev
->dev
, "failed to get memory region resource\n");
477 s3c_rtc_mem
= request_mem_region(res
->start
,
478 res
->end
-res
->start
+1,
481 if (s3c_rtc_mem
== NULL
) {
482 dev_err(&pdev
->dev
, "failed to reserve memory region\n");
487 s3c_rtc_base
= ioremap(res
->start
, res
->end
- res
->start
+ 1);
488 if (s3c_rtc_base
== NULL
) {
489 dev_err(&pdev
->dev
, "failed ioremap()\n");
494 /* check to see if everything is setup correctly */
496 s3c_rtc_enable(pdev
, 1);
498 pr_debug("s3c2410_rtc: RTCCON=%02x\n", readb(S3C2410_RTCCON
));
500 s3c_rtc_setfreq(s3c_rtc_freq
);
502 /* register RTC and exit */
504 rtc
= rtc_device_register("s3c", &pdev
->dev
, &s3c_rtcops
,
508 dev_err(&pdev
->dev
, "cannot attach rtc\n");
513 rtc
->max_user_freq
= 128;
515 platform_set_drvdata(pdev
, rtc
);
519 s3c_rtc_enable(pdev
, 0);
520 iounmap(s3c_rtc_base
);
523 release_resource(s3c_rtc_mem
);
531 /* RTC Power management control */
533 static struct timespec s3c_rtc_delta
;
535 static int ticnt_save
;
537 static int s3c_rtc_suspend(struct platform_device
*pdev
, pm_message_t state
)
540 struct timespec time
;
544 /* save TICNT for anyone using periodic interrupts */
546 ticnt_save
= readb(S3C2410_TICNT
);
548 /* calculate time delta for suspend */
550 s3c_rtc_gettime(&pdev
->dev
, &tm
);
551 rtc_tm_to_time(&tm
, &time
.tv_sec
);
552 save_time_delta(&s3c_rtc_delta
, &time
);
553 s3c_rtc_enable(pdev
, 0);
558 static int s3c_rtc_resume(struct platform_device
*pdev
)
561 struct timespec time
;
565 s3c_rtc_enable(pdev
, 1);
566 s3c_rtc_gettime(&pdev
->dev
, &tm
);
567 rtc_tm_to_time(&tm
, &time
.tv_sec
);
568 restore_time_delta(&s3c_rtc_delta
, &time
);
570 writeb(ticnt_save
, S3C2410_TICNT
);
574 #define s3c_rtc_suspend NULL
575 #define s3c_rtc_resume NULL
578 static struct platform_driver s3c2410_rtcdrv
= {
579 .probe
= s3c_rtc_probe
,
580 .remove
= s3c_rtc_remove
,
581 .suspend
= s3c_rtc_suspend
,
582 .resume
= s3c_rtc_resume
,
584 .name
= "s3c2410-rtc",
585 .owner
= THIS_MODULE
,
589 static char __initdata banner
[] = "S3C24XX RTC, (c) 2004,2006 Simtec Electronics\n";
591 static int __init
s3c_rtc_init(void)
594 return platform_driver_register(&s3c2410_rtcdrv
);
597 static void __exit
s3c_rtc_exit(void)
599 platform_driver_unregister(&s3c2410_rtcdrv
);
602 module_init(s3c_rtc_init
);
603 module_exit(s3c_rtc_exit
);
605 MODULE_DESCRIPTION("Samsung S3C RTC Driver");
606 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
607 MODULE_LICENSE("GPL");