diff options
| author | Andrew Chew <achew@nvidia.com> | 2011-03-22 19:34:55 -0400 |
|---|---|---|
| committer | Linus Torvalds <torvalds@linux-foundation.org> | 2011-03-22 20:44:16 -0400 |
| commit | ff859ba6d166202d6fd8d6344a41be54e4c8a2b6 (patch) | |
| tree | 9a38c5e9645ab970ae3a8b0495b4d07d91e7cbff /drivers | |
| parent | 49d50fb1c28738ef6bad0c2b87d5355a1653fed5 (diff) | |
rtc: add real-time clock driver for NVIDIA Tegra
This is a platform driver that supports the built-in real-time clock on
Tegra SOCs.
Signed-off-by: Andrew Chew <achew@nvidia.com>
Acked-by: Alessandro Zummo <a.zummo@towertech.it>
Acked-by: Wan ZongShun <mcuos.com@gmail.com>
Acked-by: Jon Mayo <jmayo@nvidia.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'drivers')
| -rw-r--r-- | drivers/rtc/Kconfig | 10 | ||||
| -rw-r--r-- | drivers/rtc/Makefile | 1 | ||||
| -rw-r--r-- | drivers/rtc/rtc-tegra.c | 488 |
3 files changed, 499 insertions, 0 deletions
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index 4941cade319..e1878877399 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig | |||
| @@ -985,4 +985,14 @@ config RTC_DRV_LPC32XX | |||
| 985 | This driver can also be buillt as a module. If so, the module | 985 | This driver can also be buillt as a module. If so, the module |
| 986 | will be called rtc-lpc32xx. | 986 | will be called rtc-lpc32xx. |
| 987 | 987 | ||
| 988 | config RTC_DRV_TEGRA | ||
| 989 | tristate "NVIDIA Tegra Internal RTC driver" | ||
| 990 | depends on RTC_CLASS && ARCH_TEGRA | ||
| 991 | help | ||
| 992 | If you say yes here you get support for the | ||
| 993 | Tegra 200 series internal RTC module. | ||
| 994 | |||
| 995 | This drive can also be built as a module. If so, the module | ||
| 996 | will be called rtc-tegra. | ||
| 997 | |||
| 988 | endif # RTC_CLASS | 998 | endif # RTC_CLASS |
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index 5f6c3838dcf..ca91c3c42e9 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile | |||
| @@ -91,6 +91,7 @@ obj-$(CONFIG_RTC_DRV_STARFIRE) += rtc-starfire.o | |||
| 91 | obj-$(CONFIG_RTC_DRV_STK17TA8) += rtc-stk17ta8.o | 91 | obj-$(CONFIG_RTC_DRV_STK17TA8) += rtc-stk17ta8.o |
| 92 | obj-$(CONFIG_RTC_DRV_STMP) += rtc-stmp3xxx.o | 92 | obj-$(CONFIG_RTC_DRV_STMP) += rtc-stmp3xxx.o |
| 93 | obj-$(CONFIG_RTC_DRV_SUN4V) += rtc-sun4v.o | 93 | obj-$(CONFIG_RTC_DRV_SUN4V) += rtc-sun4v.o |
| 94 | obj-$(CONFIG_RTC_DRV_TEGRA) += rtc-tegra.o | ||
| 94 | obj-$(CONFIG_RTC_DRV_TEST) += rtc-test.o | 95 | obj-$(CONFIG_RTC_DRV_TEST) += rtc-test.o |
| 95 | obj-$(CONFIG_RTC_DRV_TWL4030) += rtc-twl.o | 96 | obj-$(CONFIG_RTC_DRV_TWL4030) += rtc-twl.o |
| 96 | obj-$(CONFIG_RTC_DRV_TX4939) += rtc-tx4939.o | 97 | obj-$(CONFIG_RTC_DRV_TX4939) += rtc-tx4939.o |
diff --git a/drivers/rtc/rtc-tegra.c b/drivers/rtc/rtc-tegra.c new file mode 100644 index 00000000000..2fc31aac3f4 --- /dev/null +++ b/drivers/rtc/rtc-tegra.c | |||
| @@ -0,0 +1,488 @@ | |||
| 1 | /* | ||
| 2 | * An RTC driver for the NVIDIA Tegra 200 series internal RTC. | ||
| 3 | * | ||
| 4 | * Copyright (c) 2010, NVIDIA Corporation. | ||
| 5 | * | ||
| 6 | * This program is free software; you can redistribute it and/or modify | ||
| 7 | * it under the terms of the GNU General Public License as published by | ||
| 8 | * the Free Software Foundation; either version 2 of the License, or | ||
| 9 | * (at your option) any later version. | ||
| 10 | * | ||
| 11 | * This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
| 14 | * more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU General Public License along | ||
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., | ||
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
| 19 | */ | ||
| 20 | #include <linux/kernel.h> | ||
| 21 | #include <linux/init.h> | ||
| 22 | #include <linux/module.h> | ||
| 23 | #include <linux/slab.h> | ||
| 24 | #include <linux/irq.h> | ||
| 25 | #include <linux/io.h> | ||
| 26 | #include <linux/delay.h> | ||
| 27 | #include <linux/rtc.h> | ||
| 28 | #include <linux/platform_device.h> | ||
| 29 | |||
| 30 | /* set to 1 = busy every eight 32kHz clocks during copy of sec+msec to AHB */ | ||
| 31 | #define TEGRA_RTC_REG_BUSY 0x004 | ||
| 32 | #define TEGRA_RTC_REG_SECONDS 0x008 | ||
| 33 | /* when msec is read, the seconds are buffered into shadow seconds. */ | ||
| 34 | #define TEGRA_RTC_REG_SHADOW_SECONDS 0x00c | ||
| 35 | #define TEGRA_RTC_REG_MILLI_SECONDS 0x010 | ||
| 36 | #define TEGRA_RTC_REG_SECONDS_ALARM0 0x014 | ||
| 37 | #define TEGRA_RTC_REG_SECONDS_ALARM1 0x018 | ||
| 38 | #define TEGRA_RTC_REG_MILLI_SECONDS_ALARM0 0x01c | ||
| 39 | #define TEGRA_RTC_REG_INTR_MASK 0x028 | ||
| 40 | /* write 1 bits to clear status bits */ | ||
| 41 | #define TEGRA_RTC_REG_INTR_STATUS 0x02c | ||
| 42 | |||
| 43 | /* bits in INTR_MASK */ | ||
| 44 | #define TEGRA_RTC_INTR_MASK_MSEC_CDN_ALARM (1<<4) | ||
| 45 | #define TEGRA_RTC_INTR_MASK_SEC_CDN_ALARM (1<<3) | ||
| 46 | #define TEGRA_RTC_INTR_MASK_MSEC_ALARM (1<<2) | ||
| 47 | #define TEGRA_RTC_INTR_MASK_SEC_ALARM1 (1<<1) | ||
| 48 | #define TEGRA_RTC_INTR_MASK_SEC_ALARM0 (1<<0) | ||
| 49 | |||
| 50 | /* bits in INTR_STATUS */ | ||
| 51 | #define TEGRA_RTC_INTR_STATUS_MSEC_CDN_ALARM (1<<4) | ||
| 52 | #define TEGRA_RTC_INTR_STATUS_SEC_CDN_ALARM (1<<3) | ||
| 53 | #define TEGRA_RTC_INTR_STATUS_MSEC_ALARM (1<<2) | ||
| 54 | #define TEGRA_RTC_INTR_STATUS_SEC_ALARM1 (1<<1) | ||
| 55 | #define TEGRA_RTC_INTR_STATUS_SEC_ALARM0 (1<<0) | ||
| 56 | |||
| 57 | struct tegra_rtc_info { | ||
| 58 | struct platform_device *pdev; | ||
| 59 | struct rtc_device *rtc_dev; | ||
| 60 | void __iomem *rtc_base; /* NULL if not initialized. */ | ||
| 61 | int tegra_rtc_irq; /* alarm and periodic irq */ | ||
| 62 | spinlock_t tegra_rtc_lock; | ||
| 63 | }; | ||
| 64 | |||
| 65 | /* RTC hardware is busy when it is updating its values over AHB once | ||
| 66 | * every eight 32kHz clocks (~250uS). | ||
| 67 | * outside of these updates the CPU is free to write. | ||
| 68 | * CPU is always free to read. | ||
| 69 | */ | ||
| 70 | static inline u32 tegra_rtc_check_busy(struct tegra_rtc_info *info) | ||
| 71 | { | ||
| 72 | return readl(info->rtc_base + TEGRA_RTC_REG_BUSY) & 1; | ||
| 73 | } | ||
| 74 | |||
| 75 | /* Wait for hardware to be ready for writing. | ||
| 76 | * This function tries to maximize the amount of time before the next update. | ||
| 77 | * It does this by waiting for the RTC to become busy with its periodic update, | ||
| 78 | * then returning once the RTC first becomes not busy. | ||
| 79 | * This periodic update (where the seconds and milliseconds are copied to the | ||
| 80 | * AHB side) occurs every eight 32kHz clocks (~250uS). | ||
| 81 | * The behavior of this function allows us to make some assumptions without | ||
| 82 | * introducing a race, because 250uS is plenty of time to read/write a value. | ||
| 83 | */ | ||
| 84 | static int tegra_rtc_wait_while_busy(struct device *dev) | ||
| 85 | { | ||
| 86 | struct tegra_rtc_info *info = dev_get_drvdata(dev); | ||
| 87 | |||
| 88 | int retries = 500; /* ~490 us is the worst case, ~250 us is best. */ | ||
| 89 | |||
| 90 | /* first wait for the RTC to become busy. this is when it | ||
| 91 | * posts its updated seconds+msec registers to AHB side. */ | ||
| 92 | while (tegra_rtc_check_busy(info)) { | ||
| 93 | if (!retries--) | ||
| 94 | goto retry_failed; | ||
| 95 | udelay(1); | ||
| 96 | } | ||
| 97 | |||
| 98 | /* now we have about 250 us to manipulate registers */ | ||
| 99 | return 0; | ||
| 100 | |||
| 101 | retry_failed: | ||
| 102 | dev_err(dev, "write failed:retry count exceeded.\n"); | ||
| 103 | return -ETIMEDOUT; | ||
| 104 | } | ||
| 105 | |||
| 106 | static int tegra_rtc_read_time(struct device *dev, struct rtc_time *tm) | ||
| 107 | { | ||
| 108 | struct tegra_rtc_info *info = dev_get_drvdata(dev); | ||
| 109 | unsigned long sec, msec; | ||
| 110 | unsigned long sl_irq_flags; | ||
| 111 | |||
| 112 | /* RTC hardware copies seconds to shadow seconds when a read | ||
| 113 | * of milliseconds occurs. use a lock to keep other threads out. */ | ||
| 114 | spin_lock_irqsave(&info->tegra_rtc_lock, sl_irq_flags); | ||
| 115 | |||
| 116 | msec = readl(info->rtc_base + TEGRA_RTC_REG_MILLI_SECONDS); | ||
| 117 | sec = readl(info->rtc_base + TEGRA_RTC_REG_SHADOW_SECONDS); | ||
| 118 | |||
| 119 | spin_unlock_irqrestore(&info->tegra_rtc_lock, sl_irq_flags); | ||
| 120 | |||
| 121 | rtc_time_to_tm(sec, tm); | ||
| 122 | |||
| 123 | dev_vdbg(dev, "time read as %lu. %d/%d/%d %d:%02u:%02u\n", | ||
| 124 | sec, | ||
| 125 | tm->tm_mon + 1, | ||
| 126 | tm->tm_mday, | ||
| 127 | tm->tm_year + 1900, | ||
| 128 | tm->tm_hour, | ||
| 129 | tm->tm_min, | ||
| 130 | tm->tm_sec | ||
| 131 | ); | ||
| 132 | |||
| 133 | return 0; | ||
| 134 | } | ||
| 135 | |||
| 136 | static int tegra_rtc_set_time(struct device *dev, struct rtc_time *tm) | ||
| 137 | { | ||
| 138 | struct tegra_rtc_info *info = dev_get_drvdata(dev); | ||
| 139 | unsigned long sec; | ||
| 140 | int ret; | ||
| 141 | |||
| 142 | /* convert tm to seconds. */ | ||
| 143 | ret = rtc_valid_tm(tm); | ||
| 144 | if (ret) | ||
| 145 | return ret; | ||
| 146 | |||
| 147 | rtc_tm_to_time(tm, &sec); | ||
| 148 | |||
| 149 | dev_vdbg(dev, "time set to %lu. %d/%d/%d %d:%02u:%02u\n", | ||
| 150 | sec, | ||
| 151 | tm->tm_mon+1, | ||
| 152 | tm->tm_mday, | ||
| 153 | tm->tm_year+1900, | ||
| 154 | tm->tm_hour, | ||
| 155 | tm->tm_min, | ||
| 156 | tm->tm_sec | ||
| 157 | ); | ||
| 158 | |||
| 159 | /* seconds only written if wait succeeded. */ | ||
| 160 | ret = tegra_rtc_wait_while_busy(dev); | ||
| 161 | if (!ret) | ||
| 162 | writel(sec, info->rtc_base + TEGRA_RTC_REG_SECONDS); | ||
| 163 | |||
| 164 | dev_vdbg(dev, "time read back as %d\n", | ||
| 165 | readl(info->rtc_base + TEGRA_RTC_REG_SECONDS)); | ||
| 166 | |||
| 167 | return ret; | ||
| 168 | } | ||
| 169 | |||
| 170 | static int tegra_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) | ||
| 171 | { | ||
| 172 | struct tegra_rtc_info *info = dev_get_drvdata(dev); | ||
| 173 | unsigned long sec; | ||
| 174 | unsigned tmp; | ||
| 175 | |||
| 176 | sec = readl(info->rtc_base + TEGRA_RTC_REG_SECONDS_ALARM0); | ||
| 177 | |||
| 178 | if (sec == 0) { | ||
