linux/drivers/rtc/rtc-mv.c
<<
>>
Prefs
   1/*
   2 * Driver for the RTC in Marvell SoCs.
   3 *
   4 * This file is licensed under the terms of the GNU General Public
   5 * License version 2.  This program is licensed "as is" without any
   6 * warranty of any kind, whether express or implied.
   7 */
   8
   9#include <linux/init.h>
  10#include <linux/kernel.h>
  11#include <linux/rtc.h>
  12#include <linux/bcd.h>
  13#include <linux/io.h>
  14#include <linux/platform_device.h>
  15#include <linux/delay.h>
  16#include <linux/gfp.h>
  17#include <linux/module.h>
  18
  19
  20#define RTC_TIME_REG_OFFS       0
  21#define RTC_SECONDS_OFFS        0
  22#define RTC_MINUTES_OFFS        8
  23#define RTC_HOURS_OFFS          16
  24#define RTC_WDAY_OFFS           24
  25#define RTC_HOURS_12H_MODE              (1 << 22) /* 12 hours mode */
  26
  27#define RTC_DATE_REG_OFFS       4
  28#define RTC_MDAY_OFFS           0
  29#define RTC_MONTH_OFFS          8
  30#define RTC_YEAR_OFFS           16
  31
  32#define RTC_ALARM_TIME_REG_OFFS 8
  33#define RTC_ALARM_DATE_REG_OFFS 0xc
  34#define RTC_ALARM_VALID         (1 << 7)
  35
  36#define RTC_ALARM_INTERRUPT_MASK_REG_OFFS       0x10
  37#define RTC_ALARM_INTERRUPT_CASUE_REG_OFFS      0x14
  38
  39struct rtc_plat_data {
  40        struct rtc_device *rtc;
  41        void __iomem *ioaddr;
  42        int             irq;
  43};
  44
  45static int mv_rtc_set_time(struct device *dev, struct rtc_time *tm)
  46{
  47        struct rtc_plat_data *pdata = dev_get_drvdata(dev);
  48        void __iomem *ioaddr = pdata->ioaddr;
  49        u32     rtc_reg;
  50
  51        rtc_reg = (bin2bcd(tm->tm_sec) << RTC_SECONDS_OFFS) |
  52                (bin2bcd(tm->tm_min) << RTC_MINUTES_OFFS) |
  53                (bin2bcd(tm->tm_hour) << RTC_HOURS_OFFS) |
  54                (bin2bcd(tm->tm_wday) << RTC_WDAY_OFFS);
  55        writel(rtc_reg, ioaddr + RTC_TIME_REG_OFFS);
  56
  57        rtc_reg = (bin2bcd(tm->tm_mday) << RTC_MDAY_OFFS) |
  58                (bin2bcd(tm->tm_mon + 1) << RTC_MONTH_OFFS) |
  59                (bin2bcd(tm->tm_year % 100) << RTC_YEAR_OFFS);
  60        writel(rtc_reg, ioaddr + RTC_DATE_REG_OFFS);
  61
  62        return 0;
  63}
  64
  65static int mv_rtc_read_time(struct device *dev, struct rtc_time *tm)
  66{
  67        struct rtc_plat_data *pdata = dev_get_drvdata(dev);
  68        void __iomem *ioaddr = pdata->ioaddr;
  69        u32     rtc_time, rtc_date;
  70        unsigned int year, month, day, hour, minute, second, wday;
  71
  72        rtc_time = readl(ioaddr + RTC_TIME_REG_OFFS);
  73        rtc_date = readl(ioaddr + RTC_DATE_REG_OFFS);
  74
  75        second = rtc_time & 0x7f;
  76        minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f;
  77        hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hours mode */
  78        wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7;
  79
  80        day = rtc_date & 0x3f;
  81        month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f;
  82        year = (rtc_date >> RTC_YEAR_OFFS) & 0xff;
  83
  84        tm->tm_sec = bcd2bin(second);
  85        tm->tm_min = bcd2bin(minute);
  86        tm->tm_hour = bcd2bin(hour);
  87        tm->tm_mday = bcd2bin(day);
  88        tm->tm_wday = bcd2bin(wday);
  89        tm->tm_mon = bcd2bin(month) - 1;
  90        /* hw counts from year 2000, but tm_year is relative to 1900 */
  91        tm->tm_year = bcd2bin(year) + 100;
  92
  93        return rtc_valid_tm(tm);
  94}
  95
  96static int mv_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
  97{
  98        struct rtc_plat_data *pdata = dev_get_drvdata(dev);
  99        void __iomem *ioaddr = pdata->ioaddr;
 100        u32     rtc_time, rtc_date;
 101        unsigned int year, month, day, hour, minute, second, wday;
 102
 103        rtc_time = readl(ioaddr + RTC_ALARM_TIME_REG_OFFS);
 104        rtc_date = readl(ioaddr + RTC_ALARM_DATE_REG_OFFS);
 105
 106        second = rtc_time & 0x7f;
 107        minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f;
 108        hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hours mode */
 109        wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7;
 110
 111        day = rtc_date & 0x3f;
 112        month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f;
 113        year = (rtc_date >> RTC_YEAR_OFFS) & 0xff;
 114
 115        alm->time.tm_sec = bcd2bin(second);
 116        alm->time.tm_min = bcd2bin(minute);
 117        alm->time.tm_hour = bcd2bin(hour);
 118        alm->time.tm_mday = bcd2bin(day);
 119        alm->time.tm_wday = bcd2bin(wday);
 120        alm->time.tm_mon = bcd2bin(month) - 1;
 121        /* hw counts from year 2000, but tm_year is relative to 1900 */
 122        alm->time.tm_year = bcd2bin(year) + 100;
 123
 124        if (rtc_valid_tm(&alm->time) < 0) {
 125                dev_err(dev, "retrieved alarm date/time is not valid.\n");
 126                rtc_time_to_tm(0, &alm->time);
 127        }
 128
 129        alm->enabled = !!readl(ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
 130        return 0;
 131}
 132
 133static int mv_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
 134{
 135        struct rtc_plat_data *pdata = dev_get_drvdata(dev);
 136        void __iomem *ioaddr = pdata->ioaddr;
 137        u32 rtc_reg = 0;
 138
 139        if (alm->time.tm_sec >= 0)
 140                rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_sec))
 141                        << RTC_SECONDS_OFFS;
 142        if (alm->time.tm_min >= 0)
 143                rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_min))
 144                        << RTC_MINUTES_OFFS;
 145        if (alm->time.tm_hour >= 0)
 146                rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_hour))
 147                        << RTC_HOURS_OFFS;
 148
 149        writel(rtc_reg, ioaddr + RTC_ALARM_TIME_REG_OFFS);
 150
 151        if (alm->time.tm_mday >= 0)
 152                rtc_reg = (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mday))
 153                        << RTC_MDAY_OFFS;
 154        else
 155                rtc_reg = 0;
 156
 157        if (alm->time.tm_mon >= 0)
 158                rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mon + 1))
 159                        << RTC_MONTH_OFFS;
 160
 161        if (alm->time.tm_year >= 0)
 162                rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_year % 100))
 163                        << RTC_YEAR_OFFS;
 164
 165        writel(rtc_reg, ioaddr + RTC_ALARM_DATE_REG_OFFS);
 166        writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS);
 167        writel(alm->enabled ? 1 : 0,
 168               ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
 169
 170        return 0;
 171}
 172
 173static int mv_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
 174{
 175        struct platform_device *pdev = to_platform_device(dev);
 176        struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
 177        void __iomem *ioaddr = pdata->ioaddr;
 178
 179        if (pdata->irq < 0)
 180                return -EINVAL; /* fall back into rtc-dev's emulation */
 181
 182        if (enabled)
 183                writel(1, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
 184        else
 185                writel(0, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
 186        return 0;
 187}
 188
 189static irqreturn_t mv_rtc_interrupt(int irq, void *data)
 190{
 191        struct rtc_plat_data *pdata = data;
 192        void __iomem *ioaddr = pdata->ioaddr;
 193
 194        /* alarm irq? */
 195        if (!readl(ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS))
 196                return IRQ_NONE;
 197
 198        /* clear interrupt */
 199        writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS);
 200        rtc_update_irq(pdata->rtc, 1, RTC_IRQF | RTC_AF);
 201        return IRQ_HANDLED;
 202}
 203
 204static const struct rtc_class_ops mv_rtc_ops = {
 205        .read_time      = mv_rtc_read_time,
 206        .set_time       = mv_rtc_set_time,
 207};
 208
 209static const struct rtc_class_ops mv_rtc_alarm_ops = {
 210        .read_time      = mv_rtc_read_time,
 211        .set_time       = mv_rtc_set_time,
 212        .read_alarm     = mv_rtc_read_alarm,
 213        .set_alarm      = mv_rtc_set_alarm,
 214        .alarm_irq_enable = mv_rtc_alarm_irq_enable,
 215};
 216
 217static int __devinit mv_rtc_probe(struct platform_device *pdev)
 218{
 219        struct resource *res;
 220        struct rtc_plat_data *pdata;
 221        resource_size_t size;
 222        u32 rtc_time;
 223
 224        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 225        if (!res)
 226                return -ENODEV;
 227
 228        pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
 229        if (!pdata)
 230                return -ENOMEM;
 231
 232        size = resource_size(res);
 233        if (!devm_request_mem_region(&pdev->dev, res->start, size,
 234                                     pdev->name))
 235                return -EBUSY;
 236
 237        pdata->ioaddr = devm_ioremap(&pdev->dev, res->start, size);
 238        if (!pdata->ioaddr)
 239                return -ENOMEM;
 240
 241        /* make sure the 24 hours mode is enabled */
 242        rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
 243        if (rtc_time & RTC_HOURS_12H_MODE) {
 244                dev_err(&pdev->dev, "24 Hours mode not supported.\n");
 245                return -EINVAL;
 246        }
 247
 248        /* make sure it is actually functional */
 249        if (rtc_time == 0x01000000) {
 250                ssleep(1);
 251                rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
 252                if (rtc_time == 0x01000000) {
 253                        dev_err(&pdev->dev, "internal RTC not ticking\n");
 254                        return -ENODEV;
 255                }
 256        }
 257
 258        pdata->irq = platform_get_irq(pdev, 0);
 259
 260        platform_set_drvdata(pdev, pdata);
 261
 262        if (pdata->irq >= 0) {
 263                device_init_wakeup(&pdev->dev, 1);
 264                pdata->rtc = rtc_device_register(pdev->name, &pdev->dev,
 265                                                 &mv_rtc_alarm_ops,
 266                                                 THIS_MODULE);
 267        } else
 268                pdata->rtc = rtc_device_register(pdev->name, &pdev->dev,
 269                                                 &mv_rtc_ops, THIS_MODULE);
 270        if (IS_ERR(pdata->rtc))
 271                return PTR_ERR(pdata->rtc);
 272
 273        if (pdata->irq >= 0) {
 274                writel(0, pdata->ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
 275                if (devm_request_irq(&pdev->dev, pdata->irq, mv_rtc_interrupt,
 276                                     IRQF_DISABLED | IRQF_SHARED,
 277                                     pdev->name, pdata) < 0) {
 278                        dev_warn(&pdev->dev, "interrupt not available.\n");
 279                        pdata->irq = -1;
 280                }
 281        }
 282
 283        return 0;
 284}
 285
 286static int __exit mv_rtc_remove(struct platform_device *pdev)
 287{
 288        struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
 289
 290        if (pdata->irq >= 0)
 291                device_init_wakeup(&pdev->dev, 0);
 292
 293        rtc_device_unregister(pdata->rtc);
 294        return 0;
 295}
 296
 297static struct platform_driver mv_rtc_driver = {
 298        .remove         = __exit_p(mv_rtc_remove),
 299        .driver         = {
 300                .name   = "rtc-mv",
 301                .owner  = THIS_MODULE,
 302        },
 303};
 304
 305static __init int mv_init(void)
 306{
 307        return platform_driver_probe(&mv_rtc_driver, mv_rtc_probe);
 308}
 309
 310static __exit void mv_exit(void)
 311{
 312        platform_driver_unregister(&mv_rtc_driver);
 313}
 314
 315module_init(mv_init);
 316module_exit(mv_exit);
 317
 318MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
 319MODULE_DESCRIPTION("Marvell RTC driver");
 320MODULE_LICENSE("GPL");
 321MODULE_ALIAS("platform:rtc-mv");
 322