rtc-tegra.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. #include <linux/pm.h>
  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. /* bits in INTR_MASK */
  43. #define TEGRA_RTC_INTR_MASK_MSEC_CDN_ALARM (1<<4)
  44. #define TEGRA_RTC_INTR_MASK_SEC_CDN_ALARM (1<<3)
  45. #define TEGRA_RTC_INTR_MASK_MSEC_ALARM (1<<2)
  46. #define TEGRA_RTC_INTR_MASK_SEC_ALARM1 (1<<1)
  47. #define TEGRA_RTC_INTR_MASK_SEC_ALARM0 (1<<0)
  48. /* bits in INTR_STATUS */
  49. #define TEGRA_RTC_INTR_STATUS_MSEC_CDN_ALARM (1<<4)
  50. #define TEGRA_RTC_INTR_STATUS_SEC_CDN_ALARM (1<<3)
  51. #define TEGRA_RTC_INTR_STATUS_MSEC_ALARM (1<<2)
  52. #define TEGRA_RTC_INTR_STATUS_SEC_ALARM1 (1<<1)
  53. #define TEGRA_RTC_INTR_STATUS_SEC_ALARM0 (1<<0)
  54. struct tegra_rtc_info {
  55. struct platform_device *pdev;
  56. struct rtc_device *rtc_dev;
  57. void __iomem *rtc_base; /* NULL if not initialized. */
  58. int tegra_rtc_irq; /* alarm and periodic irq */
  59. spinlock_t tegra_rtc_lock;
  60. };
  61. /* RTC hardware is busy when it is updating its values over AHB once
  62. * every eight 32kHz clocks (~250uS).
  63. * outside of these updates the CPU is free to write.
  64. * CPU is always free to read.
  65. */
  66. static inline u32 tegra_rtc_check_busy(struct tegra_rtc_info *info)
  67. {
  68. return readl(info->rtc_base + TEGRA_RTC_REG_BUSY) & 1;
  69. }
  70. /* Wait for hardware to be ready for writing.
  71. * This function tries to maximize the amount of time before the next update.
  72. * It does this by waiting for the RTC to become busy with its periodic update,
  73. * then returning once the RTC first becomes not busy.
  74. * This periodic update (where the seconds and milliseconds are copied to the
  75. * AHB side) occurs every eight 32kHz clocks (~250uS).
  76. * The behavior of this function allows us to make some assumptions without
  77. * introducing a race, because 250uS is plenty of time to read/write a value.
  78. */
  79. static int tegra_rtc_wait_while_busy(struct device *dev)
  80. {
  81. struct tegra_rtc_info *info = dev_get_drvdata(dev);
  82. int retries = 500; /* ~490 us is the worst case, ~250 us is best. */
  83. /* first wait for the RTC to become busy. this is when it
  84. * posts its updated seconds+msec registers to AHB side. */
  85. while (tegra_rtc_check_busy(info)) {
  86. if (!retries--)
  87. goto retry_failed;
  88. udelay(1);
  89. }
  90. /* now we have about 250 us to manipulate registers */
  91. return 0;
  92. retry_failed:
  93. dev_err(dev, "write failed:retry count exceeded.\n");
  94. return -ETIMEDOUT;
  95. }
  96. static int tegra_rtc_read_time(struct device *dev, struct rtc_time *tm)
  97. {
  98. struct tegra_rtc_info *info = dev_get_drvdata(dev);
  99. unsigned long sec, msec;
  100. unsigned long sl_irq_flags;
  101. /* RTC hardware copies seconds to shadow seconds when a read
  102. * of milliseconds occurs. use a lock to keep other threads out. */
  103. spin_lock_irqsave(&info->tegra_rtc_lock, sl_irq_flags);
  104. msec = readl(info->rtc_base + TEGRA_RTC_REG_MILLI_SECONDS);
  105. sec = readl(info->rtc_base + TEGRA_RTC_REG_SHADOW_SECONDS);
  106. spin_unlock_irqrestore(&info->tegra_rtc_lock, sl_irq_flags);
  107. rtc_time_to_tm(sec, tm);
  108. dev_vdbg(dev, "time read as %lu. %d/%d/%d %d:%02u:%02u\n",
  109. sec,
  110. tm->tm_mon + 1,
  111. tm->tm_mday,
  112. tm->tm_year + 1900,
  113. tm->tm_hour,
  114. tm->tm_min,
  115. tm->tm_sec
  116. );
  117. return 0;
  118. }
  119. static int tegra_rtc_set_time(struct device *dev, struct rtc_time *tm)
  120. {
  121. struct tegra_rtc_info *info = dev_get_drvdata(dev);
  122. unsigned long sec;
  123. int ret;
  124. /* convert tm to seconds. */
  125. ret = rtc_valid_tm(tm);
  126. if (ret)
  127. return ret;
  128. rtc_tm_to_time(tm, &sec);
  129. dev_vdbg(dev, "time set to %lu. %d/%d/%d %d:%02u:%02u\n",
  130. sec,
  131. tm->tm_mon+1,
  132. tm->tm_mday,
  133. tm->tm_year+1900,
  134. tm->tm_hour,
  135. tm->tm_min,
  136. tm->tm_sec
  137. );
  138. /* seconds only written if wait succeeded. */
  139. ret = tegra_rtc_wait_while_busy(dev);
  140. if (!ret)
  141. writel(sec, info->rtc_base + TEGRA_RTC_REG_SECONDS);
  142. dev_vdbg(dev, "time read back as %d\n",
  143. readl(info->rtc_base + TEGRA_RTC_REG_SECONDS));
  144. return ret;
  145. }
  146. static int tegra_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  147. {
  148. struct tegra_rtc_info *info = dev_get_drvdata(dev);
  149. unsigned long sec;
  150. unsigned tmp;
  151. sec = readl(info->rtc_base + TEGRA_RTC_REG_SECONDS_ALARM0);
  152. if (sec == 0) {
  153. /* alarm is disabled. */
  154. alarm->enabled = 0;
  155. alarm->time.tm_mon = -1;
  156. alarm->time.tm_mday = -1;
  157. alarm->time.tm_year = -1;
  158. alarm->time.tm_hour = -1;
  159. alarm->time.tm_min = -1;
  160. alarm->time.tm_sec = -1;
  161. } else {
  162. /* alarm is enabled. */
  163. alarm->enabled = 1;
  164. rtc_time_to_tm(sec, &alarm->time);
  165. }
  166. tmp = readl(info->rtc_base + TEGRA_RTC_REG_INTR_STATUS);
  167. alarm->pending = (tmp & TEGRA_RTC_INTR_STATUS_SEC_ALARM0) != 0;
  168. return 0;
  169. }
  170. static int tegra_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  171. {
  172. struct tegra_rtc_info *info = dev_get_drvdata(dev);
  173. unsigned status;
  174. unsigned long sl_irq_flags;
  175. tegra_rtc_wait_while_busy(dev);
  176. spin_lock_irqsave(&info->tegra_rtc_lock, sl_irq_flags);
  177. /* read the original value, and OR in the flag. */
  178. status = readl(info->rtc_base + TEGRA_RTC_REG_INTR_MASK);
  179. if (enabled)
  180. status |= TEGRA_RTC_INTR_MASK_SEC_ALARM0; /* set it */
  181. else
  182. status &= ~TEGRA_RTC_INTR_MASK_SEC_ALARM0; /* clear it */
  183. writel(status, info->rtc_base + TEGRA_RTC_REG_INTR_MASK);
  184. spin_unlock_irqrestore(&info->tegra_rtc_lock, sl_irq_flags);
  185. return 0;
  186. }
  187. static int tegra_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  188. {
  189. struct tegra_rtc_info *info = dev_get_drvdata(dev);
  190. unsigned long sec;
  191. if (alarm->enabled)
  192. rtc_tm_to_time(&alarm->time, &sec);
  193. else
  194. sec = 0;
  195. tegra_rtc_wait_while_busy(dev);
  196. writel(sec, info->rtc_base + TEGRA_RTC_REG_SECONDS_ALARM0);
  197. dev_vdbg(dev, "alarm read back as %d\n",
  198. readl(info->rtc_base + TEGRA_RTC_REG_SECONDS_ALARM0));
  199. /* if successfully written and alarm is enabled ... */
  200. if (sec) {
  201. tegra_rtc_alarm_irq_enable(dev, 1);
  202. dev_vdbg(dev, "alarm set as %lu. %d/%d/%d %d:%02u:%02u\n",
  203. sec,
  204. alarm->time.tm_mon+1,
  205. alarm->time.tm_mday,
  206. alarm->time.tm_year+1900,
  207. alarm->time.tm_hour,
  208. alarm->time.tm_min,
  209. alarm->time.tm_sec);
  210. } else {
  211. /* disable alarm if 0 or write error. */
  212. dev_vdbg(dev, "alarm disabled\n");
  213. tegra_rtc_alarm_irq_enable(dev, 0);
  214. }
  215. return 0;
  216. }
  217. static int tegra_rtc_proc(struct device *dev, struct seq_file *seq)
  218. {
  219. if (!dev || !dev->driver)
  220. return 0;
  221. seq_printf(seq, "name\t\t: %s\n", dev_name(dev));
  222. return 0;
  223. }
  224. static irqreturn_t tegra_rtc_irq_handler(int irq, void *data)
  225. {
  226. struct device *dev = data;
  227. struct tegra_rtc_info *info = dev_get_drvdata(dev);
  228. unsigned long events = 0;
  229. unsigned status;
  230. unsigned long sl_irq_flags;
  231. status = readl(info->rtc_base + TEGRA_RTC_REG_INTR_STATUS);
  232. if (status) {
  233. /* clear the interrupt masks and status on any irq. */
  234. tegra_rtc_wait_while_busy(dev);
  235. spin_lock_irqsave(&info->tegra_rtc_lock, sl_irq_flags);
  236. writel(0, info->rtc_base + TEGRA_RTC_REG_INTR_MASK);
  237. writel(status, info->rtc_base + TEGRA_RTC_REG_INTR_STATUS);
  238. spin_unlock_irqrestore(&info->tegra_rtc_lock, sl_irq_flags);
  239. }
  240. /* check if Alarm */
  241. if ((status & TEGRA_RTC_INTR_STATUS_SEC_ALARM0))
  242. events |= RTC_IRQF | RTC_AF;
  243. /* check if Periodic */
  244. if ((status & TEGRA_RTC_INTR_STATUS_SEC_CDN_ALARM))
  245. events |= RTC_IRQF | RTC_PF;
  246. rtc_update_irq(info->rtc_dev, 1, events);
  247. return IRQ_HANDLED;
  248. }
  249. static struct rtc_class_ops tegra_rtc_ops = {
  250. .read_time = tegra_rtc_read_time,
  251. .set_time = tegra_rtc_set_time,
  252. .read_alarm = tegra_rtc_read_alarm,
  253. .set_alarm = tegra_rtc_set_alarm,
  254. .proc = tegra_rtc_proc,
  255. .alarm_irq_enable = tegra_rtc_alarm_irq_enable,
  256. };
  257. static const struct of_device_id tegra_rtc_dt_match[] = {
  258. { .compatible = "nvidia,tegra20-rtc", },
  259. {}
  260. };
  261. MODULE_DEVICE_TABLE(of, tegra_rtc_dt_match);
  262. static int __init tegra_rtc_probe(struct platform_device *pdev)
  263. {
  264. struct tegra_rtc_info *info;
  265. struct resource *res;
  266. int ret;
  267. info = devm_kzalloc(&pdev->dev, sizeof(struct tegra_rtc_info),
  268. GFP_KERNEL);
  269. if (!info)
  270. return -ENOMEM;
  271. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  272. info->rtc_base = devm_ioremap_resource(&pdev->dev, res);
  273. if (IS_ERR(info->rtc_base))
  274. return PTR_ERR(info->rtc_base);
  275. info->tegra_rtc_irq = platform_get_irq(pdev, 0);
  276. if (info->tegra_rtc_irq <= 0)
  277. return -EBUSY;
  278. /* set context info. */
  279. info->pdev = pdev;
  280. spin_lock_init(&info->tegra_rtc_lock);
  281. platform_set_drvdata(pdev, info);
  282. /* clear out the hardware. */
  283. writel(0, info->rtc_base + TEGRA_RTC_REG_SECONDS_ALARM0);
  284. writel(0xffffffff, info->rtc_base + TEGRA_RTC_REG_INTR_STATUS);
  285. writel(0, info->rtc_base + TEGRA_RTC_REG_INTR_MASK);
  286. device_init_wakeup(&pdev->dev, 1);
  287. info->rtc_dev = devm_rtc_device_register(&pdev->dev,
  288. dev_name(&pdev->dev), &tegra_rtc_ops,
  289. THIS_MODULE);
  290. if (IS_ERR(info->rtc_dev)) {
  291. ret = PTR_ERR(info->rtc_dev);
  292. dev_err(&pdev->dev, "Unable to register device (err=%d).\n",
  293. ret);
  294. return ret;
  295. }
  296. ret = devm_request_irq(&pdev->dev, info->tegra_rtc_irq,
  297. tegra_rtc_irq_handler, IRQF_TRIGGER_HIGH,
  298. dev_name(&pdev->dev), &pdev->dev);
  299. if (ret) {
  300. dev_err(&pdev->dev,
  301. "Unable to request interrupt for device (err=%d).\n",
  302. ret);
  303. return ret;
  304. }
  305. dev_notice(&pdev->dev, "Tegra internal Real Time Clock\n");
  306. return 0;
  307. }
  308. #ifdef CONFIG_PM_SLEEP
  309. static int tegra_rtc_suspend(struct device *dev)
  310. {
  311. struct tegra_rtc_info *info = dev_get_drvdata(dev);
  312. tegra_rtc_wait_while_busy(dev);
  313. /* only use ALARM0 as a wake source. */
  314. writel(0xffffffff, info->rtc_base + TEGRA_RTC_REG_INTR_STATUS);
  315. writel(TEGRA_RTC_INTR_STATUS_SEC_ALARM0,
  316. info->rtc_base + TEGRA_RTC_REG_INTR_MASK);
  317. dev_vdbg(dev, "alarm sec = %d\n",
  318. readl(info->rtc_base + TEGRA_RTC_REG_SECONDS_ALARM0));
  319. dev_vdbg(dev, "Suspend (device_may_wakeup=%d) irq:%d\n",
  320. device_may_wakeup(dev), info->tegra_rtc_irq);
  321. /* leave the alarms on as a wake source. */
  322. if (device_may_wakeup(dev))
  323. enable_irq_wake(info->tegra_rtc_irq);
  324. return 0;
  325. }
  326. static int tegra_rtc_resume(struct device *dev)
  327. {
  328. struct tegra_rtc_info *info = dev_get_drvdata(dev);
  329. dev_vdbg(dev, "Resume (device_may_wakeup=%d)\n",
  330. device_may_wakeup(dev));
  331. /* alarms were left on as a wake source, turn them off. */
  332. if (device_may_wakeup(dev))
  333. disable_irq_wake(info->tegra_rtc_irq);
  334. return 0;
  335. }
  336. #endif
  337. static SIMPLE_DEV_PM_OPS(tegra_rtc_pm_ops, tegra_rtc_suspend, tegra_rtc_resume);
  338. static void tegra_rtc_shutdown(struct platform_device *pdev)
  339. {
  340. dev_vdbg(&pdev->dev, "disabling interrupts.\n");
  341. tegra_rtc_alarm_irq_enable(&pdev->dev, 0);
  342. }
  343. MODULE_ALIAS("platform:tegra_rtc");
  344. static struct platform_driver tegra_rtc_driver = {
  345. .shutdown = tegra_rtc_shutdown,
  346. .driver = {
  347. .name = "tegra_rtc",
  348. .of_match_table = tegra_rtc_dt_match,
  349. .pm = &tegra_rtc_pm_ops,
  350. },
  351. };
  352. module_platform_driver_probe(tegra_rtc_driver, tegra_rtc_probe);
  353. MODULE_AUTHOR("Jon Mayo <jmayo@nvidia.com>");
  354. MODULE_DESCRIPTION("driver for Tegra internal RTC");
  355. MODULE_LICENSE("GPL");