SPI driver hotplug/coldplug fixes
[wrt350n-kernel.git] / drivers / rtc / rtc-sh.c
blob93ee05eeaeba810f6bda495c5655cccc4031de7f
1 /*
2 * SuperH On-Chip RTC Support
4 * Copyright (C) 2006 Paul Mundt
5 * Copyright (C) 2006 Jamie Lenehan
7 * Based on the old arch/sh/kernel/cpu/rtc.c by:
9 * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
10 * Copyright (C) 1999 Tetsuya Okada & Niibe Yutaka
12 * This file is subject to the terms and conditions of the GNU General Public
13 * License. See the file "COPYING" in the main directory of this archive
14 * for more details.
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/bcd.h>
19 #include <linux/rtc.h>
20 #include <linux/init.h>
21 #include <linux/platform_device.h>
22 #include <linux/seq_file.h>
23 #include <linux/interrupt.h>
24 #include <linux/spinlock.h>
25 #include <linux/io.h>
27 #define DRV_NAME "sh-rtc"
28 #define DRV_VERSION "0.1.2"
30 #ifdef CONFIG_CPU_SH3
31 #define rtc_reg_size sizeof(u16)
32 #define RTC_BIT_INVERTED 0 /* No bug on SH7708, SH7709A */
33 #elif defined(CONFIG_CPU_SH4)
34 #define rtc_reg_size sizeof(u32)
35 #define RTC_BIT_INVERTED 0x40 /* bug on SH7750, SH7750S */
36 #endif
38 #define RTC_REG(r) ((r) * rtc_reg_size)
40 #define R64CNT RTC_REG(0)
42 #define RSECCNT RTC_REG(1) /* RTC sec */
43 #define RMINCNT RTC_REG(2) /* RTC min */
44 #define RHRCNT RTC_REG(3) /* RTC hour */
45 #define RWKCNT RTC_REG(4) /* RTC week */
46 #define RDAYCNT RTC_REG(5) /* RTC day */
47 #define RMONCNT RTC_REG(6) /* RTC month */
48 #define RYRCNT RTC_REG(7) /* RTC year */
49 #define RSECAR RTC_REG(8) /* ALARM sec */
50 #define RMINAR RTC_REG(9) /* ALARM min */
51 #define RHRAR RTC_REG(10) /* ALARM hour */
52 #define RWKAR RTC_REG(11) /* ALARM week */
53 #define RDAYAR RTC_REG(12) /* ALARM day */
54 #define RMONAR RTC_REG(13) /* ALARM month */
55 #define RCR1 RTC_REG(14) /* Control */
56 #define RCR2 RTC_REG(15) /* Control */
58 /* ALARM Bits - or with BCD encoded value */
59 #define AR_ENB 0x80 /* Enable for alarm cmp */
61 /* RCR1 Bits */
62 #define RCR1_CF 0x80 /* Carry Flag */
63 #define RCR1_CIE 0x10 /* Carry Interrupt Enable */
64 #define RCR1_AIE 0x08 /* Alarm Interrupt Enable */
65 #define RCR1_AF 0x01 /* Alarm Flag */
67 /* RCR2 Bits */
68 #define RCR2_PEF 0x80 /* PEriodic interrupt Flag */
69 #define RCR2_PESMASK 0x70 /* Periodic interrupt Set */
70 #define RCR2_RTCEN 0x08 /* ENable RTC */
71 #define RCR2_ADJ 0x04 /* ADJustment (30-second) */
72 #define RCR2_RESET 0x02 /* Reset bit */
73 #define RCR2_START 0x01 /* Start bit */
75 struct sh_rtc {
76 void __iomem *regbase;
77 unsigned long regsize;
78 struct resource *res;
79 unsigned int alarm_irq, periodic_irq, carry_irq;
80 struct rtc_device *rtc_dev;
81 spinlock_t lock;
82 int rearm_aie;
85 static irqreturn_t sh_rtc_interrupt(int irq, void *dev_id)
87 struct platform_device *pdev = to_platform_device(dev_id);
88 struct sh_rtc *rtc = platform_get_drvdata(pdev);
89 unsigned int tmp, events = 0;
91 spin_lock(&rtc->lock);
93 tmp = readb(rtc->regbase + RCR1);
94 tmp &= ~RCR1_CF;
96 if (rtc->rearm_aie) {
97 if (tmp & RCR1_AF)
98 tmp &= ~RCR1_AF; /* try to clear AF again */
99 else {
100 tmp |= RCR1_AIE; /* AF has cleared, rearm IRQ */
101 rtc->rearm_aie = 0;
105 writeb(tmp, rtc->regbase + RCR1);
107 rtc_update_irq(rtc->rtc_dev, 1, events);
109 spin_unlock(&rtc->lock);
111 return IRQ_HANDLED;
114 static irqreturn_t sh_rtc_alarm(int irq, void *dev_id)
116 struct platform_device *pdev = to_platform_device(dev_id);
117 struct sh_rtc *rtc = platform_get_drvdata(pdev);
118 unsigned int tmp, events = 0;
120 spin_lock(&rtc->lock);
122 tmp = readb(rtc->regbase + RCR1);
125 * If AF is set then the alarm has triggered. If we clear AF while
126 * the alarm time still matches the RTC time then AF will
127 * immediately be set again, and if AIE is enabled then the alarm
128 * interrupt will immediately be retrigger. So we clear AIE here
129 * and use rtc->rearm_aie so that the carry interrupt will keep
130 * trying to clear AF and once it stays cleared it'll re-enable
131 * AIE.
133 if (tmp & RCR1_AF) {
134 events |= RTC_AF | RTC_IRQF;
136 tmp &= ~(RCR1_AF|RCR1_AIE);
138 writeb(tmp, rtc->regbase + RCR1);
140 rtc->rearm_aie = 1;
142 rtc_update_irq(rtc->rtc_dev, 1, events);
145 spin_unlock(&rtc->lock);
146 return IRQ_HANDLED;
149 static irqreturn_t sh_rtc_periodic(int irq, void *dev_id)
151 struct platform_device *pdev = to_platform_device(dev_id);
152 struct sh_rtc *rtc = platform_get_drvdata(pdev);
154 spin_lock(&rtc->lock);
156 rtc_update_irq(rtc->rtc_dev, 1, RTC_PF | RTC_IRQF);
158 spin_unlock(&rtc->lock);
160 return IRQ_HANDLED;
163 static inline void sh_rtc_setpie(struct device *dev, unsigned int enable)
165 struct sh_rtc *rtc = dev_get_drvdata(dev);
166 unsigned int tmp;
168 spin_lock_irq(&rtc->lock);
170 tmp = readb(rtc->regbase + RCR2);
172 if (enable) {
173 tmp &= ~RCR2_PESMASK;
174 tmp |= RCR2_PEF | (2 << 4);
175 } else
176 tmp &= ~(RCR2_PESMASK | RCR2_PEF);
178 writeb(tmp, rtc->regbase + RCR2);
180 spin_unlock_irq(&rtc->lock);
183 static inline void sh_rtc_setaie(struct device *dev, unsigned int enable)
185 struct sh_rtc *rtc = dev_get_drvdata(dev);
186 unsigned int tmp;
188 spin_lock_irq(&rtc->lock);
190 tmp = readb(rtc->regbase + RCR1);
192 if (!enable) {
193 tmp &= ~RCR1_AIE;
194 rtc->rearm_aie = 0;
195 } else if (rtc->rearm_aie == 0)
196 tmp |= RCR1_AIE;
198 writeb(tmp, rtc->regbase + RCR1);
200 spin_unlock_irq(&rtc->lock);
203 static int sh_rtc_open(struct device *dev)
205 struct sh_rtc *rtc = dev_get_drvdata(dev);
206 unsigned int tmp;
207 int ret;
209 tmp = readb(rtc->regbase + RCR1);
210 tmp &= ~RCR1_CF;
211 tmp |= RCR1_CIE;
212 writeb(tmp, rtc->regbase + RCR1);
214 ret = request_irq(rtc->periodic_irq, sh_rtc_periodic, IRQF_DISABLED,
215 "sh-rtc period", dev);
216 if (unlikely(ret)) {
217 dev_err(dev, "request period IRQ failed with %d, IRQ %d\n",
218 ret, rtc->periodic_irq);
219 return ret;
222 ret = request_irq(rtc->carry_irq, sh_rtc_interrupt, IRQF_DISABLED,
223 "sh-rtc carry", dev);
224 if (unlikely(ret)) {
225 dev_err(dev, "request carry IRQ failed with %d, IRQ %d\n",
226 ret, rtc->carry_irq);
227 free_irq(rtc->periodic_irq, dev);
228 goto err_bad_carry;
231 ret = request_irq(rtc->alarm_irq, sh_rtc_alarm, IRQF_DISABLED,
232 "sh-rtc alarm", dev);
233 if (unlikely(ret)) {
234 dev_err(dev, "request alarm IRQ failed with %d, IRQ %d\n",
235 ret, rtc->alarm_irq);
236 goto err_bad_alarm;
239 return 0;
241 err_bad_alarm:
242 free_irq(rtc->carry_irq, dev);
243 err_bad_carry:
244 free_irq(rtc->periodic_irq, dev);
246 return ret;
249 static void sh_rtc_release(struct device *dev)
251 struct sh_rtc *rtc = dev_get_drvdata(dev);
253 sh_rtc_setpie(dev, 0);
254 sh_rtc_setaie(dev, 0);
256 free_irq(rtc->periodic_irq, dev);
257 free_irq(rtc->carry_irq, dev);
258 free_irq(rtc->alarm_irq, dev);
261 static int sh_rtc_proc(struct device *dev, struct seq_file *seq)
263 struct sh_rtc *rtc = dev_get_drvdata(dev);
264 unsigned int tmp;
266 tmp = readb(rtc->regbase + RCR1);
267 seq_printf(seq, "carry_IRQ\t: %s\n",
268 (tmp & RCR1_CIE) ? "yes" : "no");
270 tmp = readb(rtc->regbase + RCR2);
271 seq_printf(seq, "periodic_IRQ\t: %s\n",
272 (tmp & RCR2_PEF) ? "yes" : "no");
274 return 0;
277 static int sh_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
279 unsigned int ret = -ENOIOCTLCMD;
281 switch (cmd) {
282 case RTC_PIE_OFF:
283 case RTC_PIE_ON:
284 sh_rtc_setpie(dev, cmd == RTC_PIE_ON);
285 ret = 0;
286 break;
287 case RTC_AIE_OFF:
288 case RTC_AIE_ON:
289 sh_rtc_setaie(dev, cmd == RTC_AIE_ON);
290 ret = 0;
291 break;
294 return ret;
297 static int sh_rtc_read_time(struct device *dev, struct rtc_time *tm)
299 struct platform_device *pdev = to_platform_device(dev);
300 struct sh_rtc *rtc = platform_get_drvdata(pdev);
301 unsigned int sec128, sec2, yr, yr100, cf_bit;
303 do {
304 unsigned int tmp;
306 spin_lock_irq(&rtc->lock);
308 tmp = readb(rtc->regbase + RCR1);
309 tmp &= ~RCR1_CF; /* Clear CF-bit */
310 tmp |= RCR1_CIE;
311 writeb(tmp, rtc->regbase + RCR1);
313 sec128 = readb(rtc->regbase + R64CNT);
315 tm->tm_sec = BCD2BIN(readb(rtc->regbase + RSECCNT));
316 tm->tm_min = BCD2BIN(readb(rtc->regbase + RMINCNT));
317 tm->tm_hour = BCD2BIN(readb(rtc->regbase + RHRCNT));
318 tm->tm_wday = BCD2BIN(readb(rtc->regbase + RWKCNT));
319 tm->tm_mday = BCD2BIN(readb(rtc->regbase + RDAYCNT));
320 tm->tm_mon = BCD2BIN(readb(rtc->regbase + RMONCNT)) - 1;
322 #if defined(CONFIG_CPU_SH4)
323 yr = readw(rtc->regbase + RYRCNT);
324 yr100 = BCD2BIN(yr >> 8);
325 yr &= 0xff;
326 #else
327 yr = readb(rtc->regbase + RYRCNT);
328 yr100 = BCD2BIN((yr == 0x99) ? 0x19 : 0x20);
329 #endif
331 tm->tm_year = (yr100 * 100 + BCD2BIN(yr)) - 1900;
333 sec2 = readb(rtc->regbase + R64CNT);
334 cf_bit = readb(rtc->regbase + RCR1) & RCR1_CF;
336 spin_unlock_irq(&rtc->lock);
337 } while (cf_bit != 0 || ((sec128 ^ sec2) & RTC_BIT_INVERTED) != 0);
339 #if RTC_BIT_INVERTED != 0
340 if ((sec128 & RTC_BIT_INVERTED))
341 tm->tm_sec--;
342 #endif
344 dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
345 "mday=%d, mon=%d, year=%d, wday=%d\n",
346 __FUNCTION__,
347 tm->tm_sec, tm->tm_min, tm->tm_hour,
348 tm->tm_mday, tm->tm_mon + 1, tm->tm_year, tm->tm_wday);
350 if (rtc_valid_tm(tm) < 0)
351 dev_err(dev, "invalid date\n");
353 return 0;
356 static int sh_rtc_set_time(struct device *dev, struct rtc_time *tm)
358 struct platform_device *pdev = to_platform_device(dev);
359 struct sh_rtc *rtc = platform_get_drvdata(pdev);
360 unsigned int tmp;
361 int year;
363 spin_lock_irq(&rtc->lock);
365 /* Reset pre-scaler & stop RTC */
366 tmp = readb(rtc->regbase + RCR2);
367 tmp |= RCR2_RESET;
368 tmp &= ~RCR2_START;
369 writeb(tmp, rtc->regbase + RCR2);
371 writeb(BIN2BCD(tm->tm_sec), rtc->regbase + RSECCNT);
372 writeb(BIN2BCD(tm->tm_min), rtc->regbase + RMINCNT);
373 writeb(BIN2BCD(tm->tm_hour), rtc->regbase + RHRCNT);
374 writeb(BIN2BCD(tm->tm_wday), rtc->regbase + RWKCNT);
375 writeb(BIN2BCD(tm->tm_mday), rtc->regbase + RDAYCNT);
376 writeb(BIN2BCD(tm->tm_mon + 1), rtc->regbase + RMONCNT);
378 #ifdef CONFIG_CPU_SH3
379 year = tm->tm_year % 100;
380 writeb(BIN2BCD(year), rtc->regbase + RYRCNT);
381 #else
382 year = (BIN2BCD((tm->tm_year + 1900) / 100) << 8) |
383 BIN2BCD(tm->tm_year % 100);
384 writew(year, rtc->regbase + RYRCNT);
385 #endif
387 /* Start RTC */
388 tmp = readb(rtc->regbase + RCR2);
389 tmp &= ~RCR2_RESET;
390 tmp |= RCR2_RTCEN | RCR2_START;
391 writeb(tmp, rtc->regbase + RCR2);
393 spin_unlock_irq(&rtc->lock);
395 return 0;
398 static inline int sh_rtc_read_alarm_value(struct sh_rtc *rtc, int reg_off)
400 unsigned int byte;
401 int value = 0xff; /* return 0xff for ignored values */
403 byte = readb(rtc->regbase + reg_off);
404 if (byte & AR_ENB) {
405 byte &= ~AR_ENB; /* strip the enable bit */
406 value = BCD2BIN(byte);
409 return value;
412 static int sh_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
414 struct platform_device *pdev = to_platform_device(dev);
415 struct sh_rtc *rtc = platform_get_drvdata(pdev);
416 struct rtc_time* tm = &wkalrm->time;
418 spin_lock_irq(&rtc->lock);
420 tm->tm_sec = sh_rtc_read_alarm_value(rtc, RSECAR);
421 tm->tm_min = sh_rtc_read_alarm_value(rtc, RMINAR);
422 tm->tm_hour = sh_rtc_read_alarm_value(rtc, RHRAR);
423 tm->tm_wday = sh_rtc_read_alarm_value(rtc, RWKAR);
424 tm->tm_mday = sh_rtc_read_alarm_value(rtc, RDAYAR);
425 tm->tm_mon = sh_rtc_read_alarm_value(rtc, RMONAR);
426 if (tm->tm_mon > 0)
427 tm->tm_mon -= 1; /* RTC is 1-12, tm_mon is 0-11 */
428 tm->tm_year = 0xffff;
430 wkalrm->enabled = (readb(rtc->regbase + RCR1) & RCR1_AIE) ? 1 : 0;
432 spin_unlock_irq(&rtc->lock);
434 return 0;
437 static inline void sh_rtc_write_alarm_value(struct sh_rtc *rtc,
438 int value, int reg_off)
440 /* < 0 for a value that is ignored */
441 if (value < 0)
442 writeb(0, rtc->regbase + reg_off);
443 else
444 writeb(BIN2BCD(value) | AR_ENB, rtc->regbase + reg_off);
447 static int sh_rtc_check_alarm(struct rtc_time* tm)
450 * The original rtc says anything > 0xc0 is "don't care" or "match
451 * all" - most users use 0xff but rtc-dev uses -1 for the same thing.
452 * The original rtc doesn't support years - some things use -1 and
453 * some 0xffff. We use -1 to make out tests easier.
455 if (tm->tm_year == 0xffff)
456 tm->tm_year = -1;
457 if (tm->tm_mon >= 0xff)
458 tm->tm_mon = -1;
459 if (tm->tm_mday >= 0xff)
460 tm->tm_mday = -1;
461 if (tm->tm_wday >= 0xff)
462 tm->tm_wday = -1;
463 if (tm->tm_hour >= 0xff)
464 tm->tm_hour = -1;
465 if (tm->tm_min >= 0xff)
466 tm->tm_min = -1;
467 if (tm->tm_sec >= 0xff)
468 tm->tm_sec = -1;
470 if (tm->tm_year > 9999 ||
471 tm->tm_mon >= 12 ||
472 tm->tm_mday == 0 || tm->tm_mday >= 32 ||
473 tm->tm_wday >= 7 ||
474 tm->tm_hour >= 24 ||
475 tm->tm_min >= 60 ||
476 tm->tm_sec >= 60)
477 return -EINVAL;
479 return 0;
482 static int sh_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
484 struct platform_device *pdev = to_platform_device(dev);
485 struct sh_rtc *rtc = platform_get_drvdata(pdev);
486 unsigned int rcr1;
487 struct rtc_time *tm = &wkalrm->time;
488 int mon, err;
490 err = sh_rtc_check_alarm(tm);
491 if (unlikely(err < 0))
492 return err;
494 spin_lock_irq(&rtc->lock);
496 /* disable alarm interrupt and clear the alarm flag */
497 rcr1 = readb(rtc->regbase + RCR1);
498 rcr1 &= ~(RCR1_AF|RCR1_AIE);
499 writeb(rcr1, rtc->regbase + RCR1);
501 rtc->rearm_aie = 0;
503 /* set alarm time */
504 sh_rtc_write_alarm_value(rtc, tm->tm_sec, RSECAR);
505 sh_rtc_write_alarm_value(rtc, tm->tm_min, RMINAR);
506 sh_rtc_write_alarm_value(rtc, tm->tm_hour, RHRAR);
507 sh_rtc_write_alarm_value(rtc, tm->tm_wday, RWKAR);
508 sh_rtc_write_alarm_value(rtc, tm->tm_mday, RDAYAR);
509 mon = tm->tm_mon;
510 if (mon >= 0)
511 mon += 1;
512 sh_rtc_write_alarm_value(rtc, mon, RMONAR);
514 if (wkalrm->enabled) {
515 rcr1 |= RCR1_AIE;
516 writeb(rcr1, rtc->regbase + RCR1);
519 spin_unlock_irq(&rtc->lock);
521 return 0;
524 static struct rtc_class_ops sh_rtc_ops = {
525 .open = sh_rtc_open,
526 .release = sh_rtc_release,
527 .ioctl = sh_rtc_ioctl,
528 .read_time = sh_rtc_read_time,
529 .set_time = sh_rtc_set_time,
530 .read_alarm = sh_rtc_read_alarm,
531 .set_alarm = sh_rtc_set_alarm,
532 .proc = sh_rtc_proc,
535 static int __devinit sh_rtc_probe(struct platform_device *pdev)
537 struct sh_rtc *rtc;
538 struct resource *res;
539 int ret = -ENOENT;
541 rtc = kzalloc(sizeof(struct sh_rtc), GFP_KERNEL);
542 if (unlikely(!rtc))
543 return -ENOMEM;
545 spin_lock_init(&rtc->lock);
547 rtc->periodic_irq = platform_get_irq(pdev, 0);
548 if (unlikely(rtc->periodic_irq < 0)) {
549 dev_err(&pdev->dev, "No IRQ for period\n");
550 goto err_badres;
553 rtc->carry_irq = platform_get_irq(pdev, 1);
554 if (unlikely(rtc->carry_irq < 0)) {
555 dev_err(&pdev->dev, "No IRQ for carry\n");
556 goto err_badres;
559 rtc->alarm_irq = platform_get_irq(pdev, 2);
560 if (unlikely(rtc->alarm_irq < 0)) {
561 dev_err(&pdev->dev, "No IRQ for alarm\n");
562 goto err_badres;
565 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
566 if (unlikely(res == NULL)) {
567 dev_err(&pdev->dev, "No IO resource\n");
568 goto err_badres;
571 rtc->regsize = res->end - res->start + 1;
573 rtc->res = request_mem_region(res->start, rtc->regsize, pdev->name);
574 if (unlikely(!rtc->res)) {
575 ret = -EBUSY;
576 goto err_badres;
579 rtc->regbase = (void __iomem *)rtc->res->start;
580 if (unlikely(!rtc->regbase)) {
581 ret = -EINVAL;
582 goto err_badmap;
585 rtc->rtc_dev = rtc_device_register("sh", &pdev->dev,
586 &sh_rtc_ops, THIS_MODULE);
587 if (IS_ERR(rtc)) {
588 ret = PTR_ERR(rtc->rtc_dev);
589 goto err_badmap;
592 platform_set_drvdata(pdev, rtc);
594 return 0;
596 err_badmap:
597 release_resource(rtc->res);
598 err_badres:
599 kfree(rtc);
601 return ret;
604 static int __devexit sh_rtc_remove(struct platform_device *pdev)
606 struct sh_rtc *rtc = platform_get_drvdata(pdev);
608 if (likely(rtc->rtc_dev))
609 rtc_device_unregister(rtc->rtc_dev);
611 sh_rtc_setpie(&pdev->dev, 0);
612 sh_rtc_setaie(&pdev->dev, 0);
614 release_resource(rtc->res);
616 platform_set_drvdata(pdev, NULL);
618 kfree(rtc);
620 return 0;
622 static struct platform_driver sh_rtc_platform_driver = {
623 .driver = {
624 .name = DRV_NAME,
625 .owner = THIS_MODULE,
627 .probe = sh_rtc_probe,
628 .remove = __devexit_p(sh_rtc_remove),
631 static int __init sh_rtc_init(void)
633 return platform_driver_register(&sh_rtc_platform_driver);
636 static void __exit sh_rtc_exit(void)
638 platform_driver_unregister(&sh_rtc_platform_driver);
641 module_init(sh_rtc_init);
642 module_exit(sh_rtc_exit);
644 MODULE_DESCRIPTION("SuperH on-chip RTC driver");
645 MODULE_VERSION(DRV_VERSION);
646 MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, Jamie Lenehan <lenehan@twibble.org>");
647 MODULE_LICENSE("GPL");