rtc: rtc-s3c: fix access unit from byte to word on RTCCON
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / rtc / rtc-s3c.c
bloba87982d7cb70c2bcba21eff7c759086278a5a918
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>
18 #include <linux/fs.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>
31 #include <asm/io.h>
32 #include <asm/irq.h>
33 #include <plat/regs-rtc.h>
35 enum s3c_cpu_type {
36 TYPE_S3C2410,
37 TYPE_S3C64XX,
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);
53 /* IRQ Handlers */
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);
64 return IRQ_HANDLED;
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);
76 return IRQ_HANDLED;
79 /* Update control registers */
80 static void s3c_rtc_setaie(int to)
82 unsigned int tmp;
84 pr_debug("%s: aie=%d\n", __func__, to);
86 tmp = readb(s3c_rtc_base + S3C2410_RTCALM) & ~S3C2410_RTCALM_ALMEN;
88 if (to)
89 tmp |= S3C2410_RTCALM_ALMEN;
91 writeb(tmp, s3c_rtc_base + S3C2410_RTCALM);
94 static int s3c_rtc_setpie(struct device *dev, int enabled)
96 unsigned int tmp;
98 pr_debug("%s: pie=%d\n", __func__, enabled);
100 spin_lock_irq(&s3c_rtc_pie_lock);
102 if (s3c_rtc_cpu_type == TYPE_S3C64XX) {
103 tmp = readw(s3c_rtc_base + S3C2410_RTCCON);
104 tmp &= ~S3C64XX_RTCCON_TICEN;
106 if (enabled)
107 tmp |= S3C64XX_RTCCON_TICEN;
109 writew(tmp, s3c_rtc_base + S3C2410_RTCCON);
110 } else {
111 tmp = readb(s3c_rtc_base + S3C2410_TICNT);
112 tmp &= ~S3C2410_TICNT_ENABLE;
114 if (enabled)
115 tmp |= S3C2410_TICNT_ENABLE;
117 writeb(tmp, s3c_rtc_base + S3C2410_TICNT);
120 spin_unlock_irq(&s3c_rtc_pie_lock);
122 return 0;
125 static int s3c_rtc_setfreq(struct device *dev, int freq)
127 struct platform_device *pdev = to_platform_device(dev);
128 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
129 unsigned int tmp = 0;
131 if (!is_power_of_2(freq))
132 return -EINVAL;
134 spin_lock_irq(&s3c_rtc_pie_lock);
136 if (s3c_rtc_cpu_type == TYPE_S3C2410) {
137 tmp = readb(s3c_rtc_base + S3C2410_TICNT);
138 tmp &= S3C2410_TICNT_ENABLE;
141 tmp |= (rtc_dev->max_user_freq / freq)-1;
143 writel(tmp, s3c_rtc_base + S3C2410_TICNT);
144 spin_unlock_irq(&s3c_rtc_pie_lock);
146 return 0;
149 /* Time read/write */
151 static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
153 unsigned int have_retried = 0;
154 void __iomem *base = s3c_rtc_base;
156 retry_get_time:
157 rtc_tm->tm_min = readb(base + S3C2410_RTCMIN);
158 rtc_tm->tm_hour = readb(base + S3C2410_RTCHOUR);
159 rtc_tm->tm_mday = readb(base + S3C2410_RTCDATE);
160 rtc_tm->tm_mon = readb(base + S3C2410_RTCMON);
161 rtc_tm->tm_year = readb(base + S3C2410_RTCYEAR);
162 rtc_tm->tm_sec = readb(base + S3C2410_RTCSEC);
164 /* the only way to work out wether the system was mid-update
165 * when we read it is to check the second counter, and if it
166 * is zero, then we re-try the entire read
169 if (rtc_tm->tm_sec == 0 && !have_retried) {
170 have_retried = 1;
171 goto retry_get_time;
174 pr_debug("read time %02x.%02x.%02x %02x/%02x/%02x\n",
175 rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
176 rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
178 rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
179 rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
180 rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
181 rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
182 rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
183 rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
185 rtc_tm->tm_year += 100;
186 rtc_tm->tm_mon -= 1;
188 return 0;
191 static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
193 void __iomem *base = s3c_rtc_base;
194 int year = tm->tm_year - 100;
196 pr_debug("set time %02d.%02d.%02d %02d/%02d/%02d\n",
197 tm->tm_year, tm->tm_mon, tm->tm_mday,
198 tm->tm_hour, tm->tm_min, tm->tm_sec);
200 /* we get around y2k by simply not supporting it */
202 if (year < 0 || year >= 100) {
203 dev_err(dev, "rtc only supports 100 years\n");
204 return -EINVAL;
207 writeb(bin2bcd(tm->tm_sec), base + S3C2410_RTCSEC);
208 writeb(bin2bcd(tm->tm_min), base + S3C2410_RTCMIN);
209 writeb(bin2bcd(tm->tm_hour), base + S3C2410_RTCHOUR);
210 writeb(bin2bcd(tm->tm_mday), base + S3C2410_RTCDATE);
211 writeb(bin2bcd(tm->tm_mon + 1), base + S3C2410_RTCMON);
212 writeb(bin2bcd(year), base + S3C2410_RTCYEAR);
214 return 0;
217 static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
219 struct rtc_time *alm_tm = &alrm->time;
220 void __iomem *base = s3c_rtc_base;
221 unsigned int alm_en;
223 alm_tm->tm_sec = readb(base + S3C2410_ALMSEC);
224 alm_tm->tm_min = readb(base + S3C2410_ALMMIN);
225 alm_tm->tm_hour = readb(base + S3C2410_ALMHOUR);
226 alm_tm->tm_mon = readb(base + S3C2410_ALMMON);
227 alm_tm->tm_mday = readb(base + S3C2410_ALMDATE);
228 alm_tm->tm_year = readb(base + S3C2410_ALMYEAR);
230 alm_en = readb(base + S3C2410_RTCALM);
232 alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;
234 pr_debug("read alarm %02x %02x.%02x.%02x %02x/%02x/%02x\n",
235 alm_en,
236 alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
237 alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
240 /* decode the alarm enable field */
242 if (alm_en & S3C2410_RTCALM_SECEN)
243 alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec);
244 else
245 alm_tm->tm_sec = 0xff;
247 if (alm_en & S3C2410_RTCALM_MINEN)
248 alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
249 else
250 alm_tm->tm_min = 0xff;
252 if (alm_en & S3C2410_RTCALM_HOUREN)
253 alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
254 else
255 alm_tm->tm_hour = 0xff;
257 if (alm_en & S3C2410_RTCALM_DAYEN)
258 alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday);
259 else
260 alm_tm->tm_mday = 0xff;
262 if (alm_en & S3C2410_RTCALM_MONEN) {
263 alm_tm->tm_mon = bcd2bin(alm_tm->tm_mon);
264 alm_tm->tm_mon -= 1;
265 } else {
266 alm_tm->tm_mon = 0xff;
269 if (alm_en & S3C2410_RTCALM_YEAREN)
270 alm_tm->tm_year = bcd2bin(alm_tm->tm_year);
271 else
272 alm_tm->tm_year = 0xffff;
274 return 0;
277 static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
279 struct rtc_time *tm = &alrm->time;
280 void __iomem *base = s3c_rtc_base;
281 unsigned int alrm_en;
283 pr_debug("s3c_rtc_setalarm: %d, %02x/%02x/%02x %02x.%02x.%02x\n",
284 alrm->enabled,
285 tm->tm_mday & 0xff, tm->tm_mon & 0xff, tm->tm_year & 0xff,
286 tm->tm_hour & 0xff, tm->tm_min & 0xff, tm->tm_sec);
289 alrm_en = readb(base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
290 writeb(0x00, base + S3C2410_RTCALM);
292 if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
293 alrm_en |= S3C2410_RTCALM_SECEN;
294 writeb(bin2bcd(tm->tm_sec), base + S3C2410_ALMSEC);
297 if (tm->tm_min < 60 && tm->tm_min >= 0) {
298 alrm_en |= S3C2410_RTCALM_MINEN;
299 writeb(bin2bcd(tm->tm_min), base + S3C2410_ALMMIN);
302 if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
303 alrm_en |= S3C2410_RTCALM_HOUREN;
304 writeb(bin2bcd(tm->tm_hour), base + S3C2410_ALMHOUR);
307 pr_debug("setting S3C2410_RTCALM to %08x\n", alrm_en);
309 writeb(alrm_en, base + S3C2410_RTCALM);
311 s3c_rtc_setaie(alrm->enabled);
313 return 0;
316 static int s3c_rtc_proc(struct device *dev, struct seq_file *seq)
318 unsigned int ticnt;
320 if (s3c_rtc_cpu_type == TYPE_S3C64XX) {
321 ticnt = readw(s3c_rtc_base + S3C2410_RTCCON);
322 ticnt &= S3C64XX_RTCCON_TICEN;
323 } else {
324 ticnt = readb(s3c_rtc_base + S3C2410_TICNT);
325 ticnt &= S3C2410_TICNT_ENABLE;
328 seq_printf(seq, "periodic_IRQ\t: %s\n", ticnt ? "yes" : "no");
329 return 0;
332 static int s3c_rtc_open(struct device *dev)
334 struct platform_device *pdev = to_platform_device(dev);
335 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
336 int ret;
338 ret = request_irq(s3c_rtc_alarmno, s3c_rtc_alarmirq,
339 IRQF_DISABLED, "s3c2410-rtc alarm", rtc_dev);
341 if (ret) {
342 dev_err(dev, "IRQ%d error %d\n", s3c_rtc_alarmno, ret);
343 return ret;
346 ret = request_irq(s3c_rtc_tickno, s3c_rtc_tickirq,
347 IRQF_DISABLED, "s3c2410-rtc tick", rtc_dev);
349 if (ret) {
350 dev_err(dev, "IRQ%d error %d\n", s3c_rtc_tickno, ret);
351 goto tick_err;
354 return ret;
356 tick_err:
357 free_irq(s3c_rtc_alarmno, rtc_dev);
358 return ret;
361 static void s3c_rtc_release(struct device *dev)
363 struct platform_device *pdev = to_platform_device(dev);
364 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
366 /* do not clear AIE here, it may be needed for wake */
368 s3c_rtc_setpie(dev, 0);
369 free_irq(s3c_rtc_alarmno, rtc_dev);
370 free_irq(s3c_rtc_tickno, rtc_dev);
373 static const struct rtc_class_ops s3c_rtcops = {
374 .open = s3c_rtc_open,
375 .release = s3c_rtc_release,
376 .read_time = s3c_rtc_gettime,
377 .set_time = s3c_rtc_settime,
378 .read_alarm = s3c_rtc_getalarm,
379 .set_alarm = s3c_rtc_setalarm,
380 .irq_set_freq = s3c_rtc_setfreq,
381 .irq_set_state = s3c_rtc_setpie,
382 .proc = s3c_rtc_proc,
385 static void s3c_rtc_enable(struct platform_device *pdev, int en)
387 void __iomem *base = s3c_rtc_base;
388 unsigned int tmp;
390 if (s3c_rtc_base == NULL)
391 return;
393 if (!en) {
394 tmp = readw(base + S3C2410_RTCCON);
395 if (s3c_rtc_cpu_type == TYPE_S3C64XX)
396 tmp &= ~S3C64XX_RTCCON_TICEN;
397 tmp &= ~S3C2410_RTCCON_RTCEN;
398 writew(tmp, base + S3C2410_RTCCON);
400 if (s3c_rtc_cpu_type == TYPE_S3C2410) {
401 tmp = readb(base + S3C2410_TICNT);
402 tmp &= ~S3C2410_TICNT_ENABLE;
403 writeb(tmp, base + S3C2410_TICNT);
405 } else {
406 /* re-enable the device, and check it is ok */
408 if ((readw(base+S3C2410_RTCCON) & S3C2410_RTCCON_RTCEN) == 0) {
409 dev_info(&pdev->dev, "rtc disabled, re-enabling\n");
411 tmp = readw(base + S3C2410_RTCCON);
412 writew(tmp | S3C2410_RTCCON_RTCEN,
413 base + S3C2410_RTCCON);
416 if ((readw(base + S3C2410_RTCCON) & S3C2410_RTCCON_CNTSEL)) {
417 dev_info(&pdev->dev, "removing RTCCON_CNTSEL\n");
419 tmp = readw(base + S3C2410_RTCCON);
420 writew(tmp & ~S3C2410_RTCCON_CNTSEL,
421 base + S3C2410_RTCCON);
424 if ((readw(base + S3C2410_RTCCON) & S3C2410_RTCCON_CLKRST)) {
425 dev_info(&pdev->dev, "removing RTCCON_CLKRST\n");
427 tmp = readw(base + S3C2410_RTCCON);
428 writew(tmp & ~S3C2410_RTCCON_CLKRST,
429 base + S3C2410_RTCCON);
434 static int __devexit s3c_rtc_remove(struct platform_device *dev)
436 struct rtc_device *rtc = platform_get_drvdata(dev);
438 platform_set_drvdata(dev, NULL);
439 rtc_device_unregister(rtc);
441 s3c_rtc_setpie(&dev->dev, 0);
442 s3c_rtc_setaie(0);
444 clk_disable(rtc_clk);
445 clk_put(rtc_clk);
446 rtc_clk = NULL;
448 iounmap(s3c_rtc_base);
449 release_resource(s3c_rtc_mem);
450 kfree(s3c_rtc_mem);
452 return 0;
455 static int __devinit s3c_rtc_probe(struct platform_device *pdev)
457 struct rtc_device *rtc;
458 struct resource *res;
459 unsigned int tmp, i;
460 int ret;
462 pr_debug("%s: probe=%p\n", __func__, pdev);
464 /* find the IRQs */
466 s3c_rtc_tickno = platform_get_irq(pdev, 1);
467 if (s3c_rtc_tickno < 0) {
468 dev_err(&pdev->dev, "no irq for rtc tick\n");
469 return -ENOENT;
472 s3c_rtc_alarmno = platform_get_irq(pdev, 0);
473 if (s3c_rtc_alarmno < 0) {
474 dev_err(&pdev->dev, "no irq for alarm\n");
475 return -ENOENT;
478 pr_debug("s3c2410_rtc: tick irq %d, alarm irq %d\n",
479 s3c_rtc_tickno, s3c_rtc_alarmno);
481 /* get the memory region */
483 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
484 if (res == NULL) {
485 dev_err(&pdev->dev, "failed to get memory region resource\n");
486 return -ENOENT;
489 s3c_rtc_mem = request_mem_region(res->start,
490 res->end-res->start+1,
491 pdev->name);
493 if (s3c_rtc_mem == NULL) {
494 dev_err(&pdev->dev, "failed to reserve memory region\n");
495 ret = -ENOENT;
496 goto err_nores;
499 s3c_rtc_base = ioremap(res->start, res->end - res->start + 1);
500 if (s3c_rtc_base == NULL) {
501 dev_err(&pdev->dev, "failed ioremap()\n");
502 ret = -EINVAL;
503 goto err_nomap;
506 rtc_clk = clk_get(&pdev->dev, "rtc");
507 if (IS_ERR(rtc_clk)) {
508 dev_err(&pdev->dev, "failed to find rtc clock source\n");
509 ret = PTR_ERR(rtc_clk);
510 rtc_clk = NULL;
511 goto err_clk;
514 clk_enable(rtc_clk);
516 /* check to see if everything is setup correctly */
518 s3c_rtc_enable(pdev, 1);
520 pr_debug("s3c2410_rtc: RTCCON=%02x\n",
521 readw(s3c_rtc_base + S3C2410_RTCCON));
523 device_init_wakeup(&pdev->dev, 1);
525 /* register RTC and exit */
527 rtc = rtc_device_register("s3c", &pdev->dev, &s3c_rtcops,
528 THIS_MODULE);
530 if (IS_ERR(rtc)) {
531 dev_err(&pdev->dev, "cannot attach rtc\n");
532 ret = PTR_ERR(rtc);
533 goto err_nortc;
536 s3c_rtc_cpu_type = platform_get_device_id(pdev)->driver_data;
538 /* Check RTC Time */
540 for (i = S3C2410_RTCSEC; i <= S3C2410_RTCYEAR; i += 0x4) {
541 tmp = readb(s3c_rtc_base + i);
543 if ((tmp & 0xf) > 0x9 || ((tmp >> 4) & 0xf) > 0x9)
544 writeb(0, s3c_rtc_base + i);
547 if (s3c_rtc_cpu_type == TYPE_S3C64XX)
548 rtc->max_user_freq = 32768;
549 else
550 rtc->max_user_freq = 128;
552 platform_set_drvdata(pdev, rtc);
554 s3c_rtc_setfreq(&pdev->dev, 1);
556 return 0;
558 err_nortc:
559 s3c_rtc_enable(pdev, 0);
560 clk_disable(rtc_clk);
561 clk_put(rtc_clk);
563 err_clk:
564 iounmap(s3c_rtc_base);
566 err_nomap:
567 release_resource(s3c_rtc_mem);
569 err_nores:
570 return ret;
573 #ifdef CONFIG_PM
575 /* RTC Power management control */
577 static int ticnt_save, ticnt_en_save;
579 static int s3c_rtc_suspend(struct platform_device *pdev, pm_message_t state)
581 /* save TICNT for anyone using periodic interrupts */
582 ticnt_save = readb(s3c_rtc_base + S3C2410_TICNT);
583 if (s3c_rtc_cpu_type == TYPE_S3C64XX) {
584 ticnt_en_save = readw(s3c_rtc_base + S3C2410_RTCCON);
585 ticnt_en_save &= S3C64XX_RTCCON_TICEN;
587 s3c_rtc_enable(pdev, 0);
589 if (device_may_wakeup(&pdev->dev))
590 enable_irq_wake(s3c_rtc_alarmno);
592 return 0;
595 static int s3c_rtc_resume(struct platform_device *pdev)
597 unsigned int tmp;
599 s3c_rtc_enable(pdev, 1);
600 writeb(ticnt_save, s3c_rtc_base + S3C2410_TICNT);
601 if (s3c_rtc_cpu_type == TYPE_S3C64XX && ticnt_en_save) {
602 tmp = readw(s3c_rtc_base + S3C2410_RTCCON);
603 writew(tmp | ticnt_en_save, s3c_rtc_base + S3C2410_RTCCON);
606 if (device_may_wakeup(&pdev->dev))
607 disable_irq_wake(s3c_rtc_alarmno);
609 return 0;
611 #else
612 #define s3c_rtc_suspend NULL
613 #define s3c_rtc_resume NULL
614 #endif
616 static struct platform_device_id s3c_rtc_driver_ids[] = {
618 .name = "s3c2410-rtc",
619 .driver_data = TYPE_S3C2410,
620 }, {
621 .name = "s3c64xx-rtc",
622 .driver_data = TYPE_S3C64XX,
627 MODULE_DEVICE_TABLE(platform, s3c_rtc_driver_ids);
629 static struct platform_driver s3c_rtc_driver = {
630 .probe = s3c_rtc_probe,
631 .remove = __devexit_p(s3c_rtc_remove),
632 .suspend = s3c_rtc_suspend,
633 .resume = s3c_rtc_resume,
634 .id_table = s3c_rtc_driver_ids,
635 .driver = {
636 .name = "s3c-rtc",
637 .owner = THIS_MODULE,
641 static char __initdata banner[] = "S3C24XX RTC, (c) 2004,2006 Simtec Electronics\n";
643 static int __init s3c_rtc_init(void)
645 printk(banner);
646 return platform_driver_register(&s3c_rtc_driver);
649 static void __exit s3c_rtc_exit(void)
651 platform_driver_unregister(&s3c_rtc_driver);
654 module_init(s3c_rtc_init);
655 module_exit(s3c_rtc_exit);
657 MODULE_DESCRIPTION("Samsung S3C RTC Driver");
658 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
659 MODULE_LICENSE("GPL");
660 MODULE_ALIAS("platform:s3c2410-rtc");