Merge branch 'for-linus' of git://neil.brown.name/md
[safe/jmp/linux-2.6] / drivers / rtc / rtc-bfin.c
index b14a9c4..aafd3e6 100644 (file)
@@ -1,8 +1,8 @@
 /*
  * Blackfin On-Chip Real Time Clock Driver
- *  Supports BF53[123]/BF53[467]/BF54[2489]
+ *  Supports BF52[257]/BF53[123]/BF53[467]/BF54[24789]
  *
- * Copyright 2004-2007 Analog Devices Inc.
+ * Copyright 2004-2008 Analog Devices Inc.
  *
  * Enter bugs at http://blackfin.uclinux.org/
  *
  * writes to clear status registers complete immediately.
  */
 
-#include <linux/module.h>
-#include <linux/kernel.h>
+/* It may seem odd that there is no SWCNT code in here (which would be exposed
+ * via the periodic interrupt event, or PIE).  Since the Blackfin RTC peripheral
+ * runs in units of seconds (N/HZ) but the Linux framework runs in units of HZ
+ * (2^N HZ), there is no point in keeping code that only provides 1 HZ PIEs.
+ * The same exact behavior can be accomplished by using the update interrupt
+ * event (UIE).  Maybe down the line the RTC peripheral will suck less in which
+ * case we can re-introduce PIE support.
+ */
+
 #include <linux/bcd.h>
-#include <linux/rtc.h>
+#include <linux/completion.h>
+#include <linux/delay.h>
 #include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
 #include <linux/platform_device.h>
+#include <linux/rtc.h>
 #include <linux/seq_file.h>
-#include <linux/interrupt.h>
-#include <linux/spinlock.h>
-#include <linux/delay.h>
 
 #include <asm/blackfin.h>
 
@@ -50,7 +59,7 @@
 struct bfin_rtc {
        struct rtc_device *rtc_dev;
        struct rtc_time rtc_alarm;
-       spinlock_t lock;
+       u16 rtc_wrote_regs;
 };
 
 /* Bit values for the ISTAT / ICTL registers */
@@ -96,7 +105,10 @@ static inline void rtc_bfin_to_tm(u32 rtc_bfin, struct rtc_time *tm)
        rtc_time_to_tm(rtc_bfin_to_time(rtc_bfin), tm);
 }
 
-/* Wait for the previous write to a RTC register to complete.
+/**
+ *     bfin_rtc_sync_pending - make sure pending writes have complete
+ *
+ * Wait for the previous write to a RTC register to complete.
  * Unfortunately, we can't sleep here as that introduces a race condition when
  * turning on interrupt events.  Consider this:
  *  - process sets alarm
@@ -117,173 +129,145 @@ static inline void rtc_bfin_to_tm(u32 rtc_bfin, struct rtc_time *tm)
  *    inc rate for all RTC registers from 1HZ to 32.768kHZ ...
  *  - use the write complete IRQ
  */
-static void rtc_bfin_sync_pending(void)
+/*
+static void bfin_rtc_sync_pending_polled(void)
 {
-       while (!(bfin_read_RTC_ISTAT() & RTC_ISTAT_WRITE_COMPLETE)) {
+       while (!(bfin_read_RTC_ISTAT() & RTC_ISTAT_WRITE_COMPLETE))
                if (!(bfin_read_RTC_ISTAT() & RTC_ISTAT_WRITE_PENDING))
                        break;
-       }
        bfin_write_RTC_ISTAT(RTC_ISTAT_WRITE_COMPLETE);
 }
+*/
+static DECLARE_COMPLETION(bfin_write_complete);
+static void bfin_rtc_sync_pending(struct device *dev)
+{
+       dev_dbg_stamp(dev);
+       while (bfin_read_RTC_ISTAT() & RTC_ISTAT_WRITE_PENDING)
+               wait_for_completion_timeout(&bfin_write_complete, HZ * 5);
+       dev_dbg_stamp(dev);
+}
 
-static void rtc_bfin_reset(struct device *dev)
+/**
+ *     bfin_rtc_reset - set RTC to sane/known state
+ *
+ * Initialize the RTC.  Enable pre-scaler to scale RTC clock
+ * to 1Hz and clear interrupt/status registers.
+ */
+static void bfin_rtc_reset(struct device *dev, u16 rtc_ictl)
 {
        struct bfin_rtc *rtc = dev_get_drvdata(dev);
-       /* Initialize the RTC. Enable pre-scaler to scale RTC clock
-        * to 1Hz and clear interrupt/status registers. */
-       spin_lock_irq(&rtc->lock);
-       rtc_bfin_sync_pending();
+       dev_dbg_stamp(dev);
+       bfin_rtc_sync_pending(dev);
        bfin_write_RTC_PREN(0x1);
-       bfin_write_RTC_ICTL(0);
-       bfin_write_RTC_SWCNT(0);
+       bfin_write_RTC_ICTL(rtc_ictl);
        bfin_write_RTC_ALARM(0);
        bfin_write_RTC_ISTAT(0xFFFF);
-       spin_unlock_irq(&rtc->lock);
+       rtc->rtc_wrote_regs = 0;
 }
 
+/**
+ *     bfin_rtc_interrupt - handle interrupt from RTC
+ *
+ * Since we handle all RTC events here, we have to make sure the requested
+ * interrupt is enabled (in RTC_ICTL) as the event status register (RTC_ISTAT)
+ * always gets updated regardless of the interrupt being enabled.  So when one
+ * even we care about (e.g. stopwatch) goes off, we don't want to turn around
+ * and say that other events have happened as well (e.g. second).  We do not
+ * have to worry about pending writes to the RTC_ICTL register as interrupts
+ * only fire if they are enabled in the RTC_ICTL register.
+ */
 static irqreturn_t bfin_rtc_interrupt(int irq, void *dev_id)
 {
        struct device *dev = dev_id;
        struct bfin_rtc *rtc = dev_get_drvdata(dev);
        unsigned long events = 0;
-       u16 rtc_istat;
+       bool write_complete = false;
+       u16 rtc_istat, rtc_ictl;
 
        dev_dbg_stamp(dev);
 
-       spin_lock_irq(&rtc->lock);
-
        rtc_istat = bfin_read_RTC_ISTAT();
+       rtc_ictl = bfin_read_RTC_ICTL();
 
-       if (rtc_istat & (RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY)) {
-               bfin_write_RTC_ISTAT(RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY);
-               events |= RTC_AF | RTC_IRQF;
+       if (rtc_istat & RTC_ISTAT_WRITE_COMPLETE) {
+               bfin_write_RTC_ISTAT(RTC_ISTAT_WRITE_COMPLETE);
+               write_complete = true;
+               complete(&bfin_write_complete);
        }
 
-       if (rtc_istat & RTC_ISTAT_STOPWATCH) {
-               bfin_write_RTC_ISTAT(RTC_ISTAT_STOPWATCH);
-               events |= RTC_PF | RTC_IRQF;
-               bfin_write_RTC_SWCNT(rtc->rtc_dev->irq_freq);
+       if (rtc_ictl & (RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY)) {
+               if (rtc_istat & (RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY)) {
+                       bfin_write_RTC_ISTAT(RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY);
+                       events |= RTC_AF | RTC_IRQF;
+               }
        }
 
-       if (rtc_istat & RTC_ISTAT_SEC) {
-               bfin_write_RTC_ISTAT(RTC_ISTAT_SEC);
-               events |= RTC_UF | RTC_IRQF;
+       if (rtc_ictl & RTC_ISTAT_SEC) {
+               if (rtc_istat & RTC_ISTAT_SEC) {
+                       bfin_write_RTC_ISTAT(RTC_ISTAT_SEC);
+                       events |= RTC_UF | RTC_IRQF;
+               }
        }
 
-       rtc_update_irq(rtc->rtc_dev, 1, events);
-
-       spin_unlock_irq(&rtc->lock);
+       if (events)
+               rtc_update_irq(rtc->rtc_dev, 1, events);
 
-       return IRQ_HANDLED;
+       if (write_complete || events)
+               return IRQ_HANDLED;
+       else
+               return IRQ_NONE;
 }
 
-static int bfin_rtc_open(struct device *dev)
+static void bfin_rtc_int_set(u16 rtc_int)
 {
-       int ret;
-
-       dev_dbg_stamp(dev);
-
-       ret = request_irq(IRQ_RTC, bfin_rtc_interrupt, IRQF_DISABLED, "rtc-bfin", dev);
-       if (unlikely(ret)) {
-               dev_err(dev, "request RTC IRQ failed with %d\n", ret);
-               return ret;
-       }
-
-       rtc_bfin_reset(dev);
-
-       return ret;
+       bfin_write_RTC_ISTAT(rtc_int);
+       bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() | rtc_int);
 }
-
-static void bfin_rtc_release(struct device *dev)
+static void bfin_rtc_int_clear(u16 rtc_int)
 {
-       dev_dbg_stamp(dev);
-       rtc_bfin_reset(dev);
-       free_irq(IRQ_RTC, dev);
+       bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() & rtc_int);
+}
+static void bfin_rtc_int_set_alarm(struct bfin_rtc *rtc)
+{
+       /* Blackfin has different bits for whether the alarm is
+        * more than 24 hours away.
+        */
+       bfin_rtc_int_set(rtc->rtc_alarm.tm_yday == -1 ? RTC_ISTAT_ALARM : RTC_ISTAT_ALARM_DAY);
 }
-
 static int bfin_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
 {
        struct bfin_rtc *rtc = dev_get_drvdata(dev);
+       int ret = 0;
 
        dev_dbg_stamp(dev);
 
-       switch (cmd) {
-       case RTC_PIE_ON:
-               dev_dbg_stamp(dev);
-               spin_lock_irq(&rtc->lock);
-               rtc_bfin_sync_pending();
-               bfin_write_RTC_ISTAT(RTC_ISTAT_STOPWATCH);
-               bfin_write_RTC_SWCNT(rtc->rtc_dev->irq_freq);
-               bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() | RTC_ISTAT_STOPWATCH);
-               spin_unlock_irq(&rtc->lock);
-               return 0;
-       case RTC_PIE_OFF:
-               dev_dbg_stamp(dev);
-               spin_lock_irq(&rtc->lock);
-               rtc_bfin_sync_pending();
-               bfin_write_RTC_SWCNT(0);
-               bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() & ~RTC_ISTAT_STOPWATCH);
-               spin_unlock_irq(&rtc->lock);
-               return 0;
+       bfin_rtc_sync_pending(dev);
 
+       switch (cmd) {
        case RTC_UIE_ON:
                dev_dbg_stamp(dev);
-               spin_lock_irq(&rtc->lock);
-               rtc_bfin_sync_pending();
-               bfin_write_RTC_ISTAT(RTC_ISTAT_SEC);
-               bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() | RTC_ISTAT_SEC);
-               spin_unlock_irq(&rtc->lock);
-               return 0;
+               bfin_rtc_int_set(RTC_ISTAT_SEC);
+               break;
        case RTC_UIE_OFF:
                dev_dbg_stamp(dev);
-               spin_lock_irq(&rtc->lock);
-               rtc_bfin_sync_pending();
-               bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() & ~RTC_ISTAT_SEC);
-               spin_unlock_irq(&rtc->lock);
-               return 0;
-
-       case RTC_AIE_ON: {
-               unsigned long rtc_alarm;
-               u16 which_alarm;
-               int ret = 0;
+               bfin_rtc_int_clear(~RTC_ISTAT_SEC);
+               break;
 
+       case RTC_AIE_ON:
                dev_dbg_stamp(dev);
-
-               spin_lock_irq(&rtc->lock);
-
-               rtc_bfin_sync_pending();
-               if (rtc->rtc_alarm.tm_yday == -1) {
-                       struct rtc_time now;
-                       rtc_bfin_to_tm(bfin_read_RTC_STAT(), &now);
-                       now.tm_sec = rtc->rtc_alarm.tm_sec;
-                       now.tm_min = rtc->rtc_alarm.tm_min;
-                       now.tm_hour = rtc->rtc_alarm.tm_hour;
-                       ret = rtc_tm_to_time(&now, &rtc_alarm);
-                       which_alarm = RTC_ISTAT_ALARM;
-               } else {
-                       ret = rtc_tm_to_time(&rtc->rtc_alarm, &rtc_alarm);
-                       which_alarm = RTC_ISTAT_ALARM_DAY;
-               }
-               if (ret == 0) {
-                       bfin_write_RTC_ISTAT(which_alarm);
-                       bfin_write_RTC_ALARM(rtc_time_to_bfin(rtc_alarm));
-                       bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() | which_alarm);
-               }
-
-               spin_unlock_irq(&rtc->lock);
-
-               return ret;
-       }
+               bfin_rtc_int_set_alarm(rtc);
+               break;
        case RTC_AIE_OFF:
                dev_dbg_stamp(dev);
-               spin_lock_irq(&rtc->lock);
-               rtc_bfin_sync_pending();
-               bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() & ~(RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY));
-               spin_unlock_irq(&rtc->lock);
-               return 0;
+               bfin_rtc_int_clear(~(RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY));
+               break;
+
+       default:
+               dev_dbg_stamp(dev);
+               ret = -ENOIOCTLCMD;
        }
 
-       return -ENOIOCTLCMD;
+       return ret;
 }
 
 static int bfin_rtc_read_time(struct device *dev, struct rtc_time *tm)
@@ -292,10 +276,10 @@ static int bfin_rtc_read_time(struct device *dev, struct rtc_time *tm)
 
        dev_dbg_stamp(dev);
 
-       spin_lock_irq(&rtc->lock);
-       rtc_bfin_sync_pending();
+       if (rtc->rtc_wrote_regs & 0x1)
+               bfin_rtc_sync_pending(dev);
+
        rtc_bfin_to_tm(bfin_read_RTC_STAT(), tm);
-       spin_unlock_irq(&rtc->lock);
 
        return 0;
 }
@@ -308,16 +292,14 @@ static int bfin_rtc_set_time(struct device *dev, struct rtc_time *tm)
 
        dev_dbg_stamp(dev);
 
-       spin_lock_irq(&rtc->lock);
-
        ret = rtc_tm_to_time(tm, &now);
        if (ret == 0) {
-               rtc_bfin_sync_pending();
+               if (rtc->rtc_wrote_regs & 0x1)
+                       bfin_rtc_sync_pending(dev);
                bfin_write_RTC_STAT(rtc_time_to_bfin(now));
+               rtc->rtc_wrote_regs = 0x1;
        }
 
-       spin_unlock_irq(&rtc->lock);
-
        return ret;
 }
 
@@ -325,16 +307,29 @@ static int bfin_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 {
        struct bfin_rtc *rtc = dev_get_drvdata(dev);
        dev_dbg_stamp(dev);
-       memcpy(&alrm->time, &rtc->rtc_alarm, sizeof(struct rtc_time));
-       alrm->pending = !!(bfin_read_RTC_ICTL() & (RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY));
+       alrm->time = rtc->rtc_alarm;
+       bfin_rtc_sync_pending(dev);
+       alrm->enabled = !!(bfin_read_RTC_ICTL() & (RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY));
        return 0;
 }
 
 static int bfin_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 {
        struct bfin_rtc *rtc = dev_get_drvdata(dev);
+       unsigned long rtc_alarm;
+
        dev_dbg_stamp(dev);
-       memcpy(&rtc->rtc_alarm, &alrm->time, sizeof(struct rtc_time));
+
+       if (rtc_tm_to_time(&alrm->time, &rtc_alarm))
+               return -EINVAL;
+
+       rtc->rtc_alarm = alrm->time;
+
+       bfin_rtc_sync_pending(dev);
+       bfin_write_RTC_ALARM(rtc_time_to_bfin(rtc_alarm));
+       if (alrm->enabled)
+               bfin_rtc_int_set_alarm(rtc);
+
        return 0;
 }
 
@@ -346,66 +341,64 @@ static int bfin_rtc_proc(struct device *dev, struct seq_file *seq)
        seq_printf(seq,
                "alarm_IRQ\t: %s\n"
                "wkalarm_IRQ\t: %s\n"
-               "seconds_IRQ\t: %s\n"
-               "periodic_IRQ\t: %s\n",
+               "seconds_IRQ\t: %s\n",
                yesno(ictl & RTC_ISTAT_ALARM),
                yesno(ictl & RTC_ISTAT_ALARM_DAY),
-               yesno(ictl & RTC_ISTAT_SEC),
-               yesno(ictl & RTC_ISTAT_STOPWATCH));
+               yesno(ictl & RTC_ISTAT_SEC));
        return 0;
 #undef yesno
 }
 
-/**
- *     bfin_irq_set_freq - make sure hardware supports requested freq
- *     @dev: pointer to RTC device structure
- *     @freq: requested frequency rate
- *
- *     The Blackfin RTC can only generate periodic events at 1 per
- *     second (1 Hz), so reject any attempt at changing it.
- */
-static int bfin_irq_set_freq(struct device *dev, int freq)
-{
-       dev_dbg_stamp(dev);
-       return -ENOTTY;
-}
-
 static struct rtc_class_ops bfin_rtc_ops = {
-       .open          = bfin_rtc_open,
-       .release       = bfin_rtc_release,
        .ioctl         = bfin_rtc_ioctl,
        .read_time     = bfin_rtc_read_time,
        .set_time      = bfin_rtc_set_time,
        .read_alarm    = bfin_rtc_read_alarm,
        .set_alarm     = bfin_rtc_set_alarm,
        .proc          = bfin_rtc_proc,
-       .irq_set_freq  = bfin_irq_set_freq,
 };
 
 static int __devinit bfin_rtc_probe(struct platform_device *pdev)
 {
        struct bfin_rtc *rtc;
+       struct device *dev = &pdev->dev;
        int ret = 0;
+       unsigned long timeout;
 
-       dev_dbg_stamp(&pdev->dev);
+       dev_dbg_stamp(dev);
 
+       /* Allocate memory for our RTC struct */
        rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
        if (unlikely(!rtc))
                return -ENOMEM;
+       platform_set_drvdata(pdev, rtc);
+       device_init_wakeup(dev, 1);
 
-       spin_lock_init(&rtc->lock);
+       /* Grab the IRQ and init the hardware */
+       ret = request_irq(IRQ_RTC, bfin_rtc_interrupt, IRQF_SHARED, pdev->name, dev);
+       if (unlikely(ret))
+               goto err;
+       /* sometimes the bootloader touched things, but the write complete was not
+        * enabled, so let's just do a quick timeout here since the IRQ will not fire ...
+        */
+       timeout = jiffies + HZ;
+       while (bfin_read_RTC_ISTAT() & RTC_ISTAT_WRITE_PENDING)
+               if (time_after(jiffies, timeout))
+                       break;
+       bfin_rtc_reset(dev, RTC_ISTAT_WRITE_COMPLETE);
+       bfin_write_RTC_SWCNT(0);
 
-       rtc->rtc_dev = rtc_device_register(pdev->name, &pdev->dev, &bfin_rtc_ops, THIS_MODULE);
-       if (unlikely(IS_ERR(rtc))) {
+       /* Register our RTC with the RTC framework */
+       rtc->rtc_dev = rtc_device_register(pdev->name, dev, &bfin_rtc_ops, THIS_MODULE);
+       if (unlikely(IS_ERR(rtc->rtc_dev))) {
                ret = PTR_ERR(rtc->rtc_dev);
-               goto err;
+               goto err_irq;
        }
-       rtc->rtc_dev->irq_freq = 1;
-
-       platform_set_drvdata(pdev, rtc);
 
        return 0;
 
+ err_irq:
+       free_irq(IRQ_RTC, dev);
  err:
        kfree(rtc);
        return ret;
@@ -414,7 +407,10 @@ static int __devinit bfin_rtc_probe(struct platform_device *pdev)
 static int __devexit bfin_rtc_remove(struct platform_device *pdev)
 {
        struct bfin_rtc *rtc = platform_get_drvdata(pdev);
+       struct device *dev = &pdev->dev;
 
+       bfin_rtc_reset(dev, 0);
+       free_irq(IRQ_RTC, dev);
        rtc_device_unregister(rtc->rtc_dev);
        platform_set_drvdata(pdev, NULL);
        kfree(rtc);
@@ -422,6 +418,32 @@ static int __devexit bfin_rtc_remove(struct platform_device *pdev)
        return 0;
 }
 
+#ifdef CONFIG_PM
+static int bfin_rtc_suspend(struct platform_device *pdev, pm_message_t state)
+{
+       if (device_may_wakeup(&pdev->dev)) {
+               enable_irq_wake(IRQ_RTC);
+               bfin_rtc_sync_pending(&pdev->dev);
+       } else
+               bfin_rtc_int_clear(-1);
+
+       return 0;
+}
+
+static int bfin_rtc_resume(struct platform_device *pdev)
+{
+       if (device_may_wakeup(&pdev->dev))
+               disable_irq_wake(IRQ_RTC);
+       else
+               bfin_write_RTC_ISTAT(-1);
+
+       return 0;
+}
+#else
+# define bfin_rtc_suspend NULL
+# define bfin_rtc_resume  NULL
+#endif
+
 static struct platform_driver bfin_rtc_driver = {
        .driver         = {
                .name   = "rtc-bfin",
@@ -429,6 +451,8 @@ static struct platform_driver bfin_rtc_driver = {
        },
        .probe          = bfin_rtc_probe,
        .remove         = __devexit_p(bfin_rtc_remove),
+       .suspend        = bfin_rtc_suspend,
+       .resume         = bfin_rtc_resume,
 };
 
 static int __init bfin_rtc_init(void)
@@ -447,3 +471,4 @@ module_exit(bfin_rtc_exit);
 MODULE_DESCRIPTION("Blackfin On-Chip Real Time Clock Driver");
 MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>");
 MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:rtc-bfin");