rtc-tegra.c 13 KB

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